1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, 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 Accessibility
; use Accessibility
;
29 with Aspects
; use Aspects
;
30 with Atree
; use Atree
;
31 with Casing
; use Casing
;
32 with Checks
; use Checks
;
33 with Debug
; use Debug
;
34 with Einfo
; use Einfo
;
35 with Einfo
.Entities
; use Einfo
.Entities
;
36 with Einfo
.Utils
; use Einfo
.Utils
;
37 with Elists
; use Elists
;
38 with Errout
; use Errout
;
40 with Exp_Dist
; use Exp_Dist
;
41 with Exp_Util
; use Exp_Util
;
42 with Expander
; use Expander
;
43 with Freeze
; use Freeze
;
44 with Ghost
; use Ghost
;
45 with Gnatvsn
; use Gnatvsn
;
46 with Itypes
; use Itypes
;
48 with Lib
.Xref
; use Lib
.Xref
;
49 with Mutably_Tagged
; use Mutably_Tagged
;
50 with Nlists
; use Nlists
;
51 with Nmake
; use Nmake
;
53 with Restrict
; use Restrict
;
54 with Rident
; use Rident
;
55 with Rtsfind
; use Rtsfind
;
58 with Sem_Aggr
; use Sem_Aggr
;
59 with Sem_Aux
; use Sem_Aux
;
60 with Sem_Cat
; use Sem_Cat
;
61 with Sem_Ch6
; use Sem_Ch6
;
62 with Sem_Ch8
; use Sem_Ch8
;
63 with Sem_Ch10
; use Sem_Ch10
;
64 with Sem_Dim
; use Sem_Dim
;
65 with Sem_Dist
; use Sem_Dist
;
66 with Sem_Elab
; use Sem_Elab
;
67 with Sem_Elim
; use Sem_Elim
;
68 with Sem_Eval
; use Sem_Eval
;
69 with Sem_Prag
; use Sem_Prag
;
70 with Sem_Res
; use Sem_Res
;
71 with Sem_Type
; use Sem_Type
;
72 with Sem_Util
; use Sem_Util
;
74 with Stand
; use Stand
;
75 with Sinfo
; use Sinfo
;
76 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
77 with Sinfo
.Utils
; use Sinfo
.Utils
;
78 with Sinput
; use Sinput
;
80 with Stringt
; use Stringt
;
81 with Strub
; use Strub
;
83 with Stylesw
; use Stylesw
;
84 with Targparm
; use Targparm
;
85 with Ttypes
; use Ttypes
;
86 with Tbuild
; use Tbuild
;
87 with Uintp
; use Uintp
;
88 with Uname
; use Uname
;
89 with Urealp
; use Urealp
;
90 with Warnsw
; use Warnsw
;
92 with System
.CRC32
; use System
.CRC32
;
94 package body Sem_Attr
is
96 True_Value
: constant Uint
:= Uint_1
;
97 False_Value
: constant Uint
:= Uint_0
;
98 -- Synonyms to be used when these constants are used as Boolean values
100 Bad_Attribute
: exception;
101 -- Exception raised if an error is detected during attribute processing,
102 -- used so that we can abandon the processing so we don't run into
103 -- trouble with cascaded errors.
105 -- The following array is the list of attributes defined in the Ada 83 RM.
106 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
107 -- modes all these attributes are recognized, even if removed in Ada 95.
109 Attribute_83
: constant Attribute_Set
:=
112 Attribute_Alignment |
115 Attribute_Constrained |
122 Attribute_First_Bit |
128 Attribute_Leading_Part |
130 Attribute_Machine_Emax |
131 Attribute_Machine_Emin |
132 Attribute_Machine_Mantissa |
133 Attribute_Machine_Overflows |
134 Attribute_Machine_Radix |
135 Attribute_Machine_Rounds |
141 Attribute_Safe_Emax |
142 Attribute_Safe_Large |
143 Attribute_Safe_Small |
146 Attribute_Storage_Size |
148 Attribute_Terminated |
151 Attribute_Width
=> True,
154 -- The following array is the list of attributes defined in the Ada 2005
155 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
156 -- but in Ada 95 they are considered to be implementation defined.
158 Attribute_05
: constant Attribute_Set
:=
159 (Attribute_Machine_Rounding |
162 Attribute_Stream_Size |
163 Attribute_Wide_Wide_Width
=> True,
166 -- The following array is the list of attributes defined in the Ada 2012
167 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
168 -- and Ada 2005 modes, but are considered to be implementation defined.
170 Attribute_12
: constant Attribute_Set
:=
171 (Attribute_First_Valid |
172 Attribute_Has_Same_Storage |
173 Attribute_Last_Valid |
174 Attribute_Max_Alignment_For_Allocation |
176 Attribute_Overlaps_Storage |
177 Attribute_Result
=> True,
180 -- The following array is the list of attributes defined in the Ada 2022
181 -- RM which are not defined in Ada 2012. These are recognized in Ada
182 -- 95/2005/2012 modes, but are considered to be implementation defined.
184 Attribute_22
: constant Attribute_Set
:=
185 (Attribute_Enum_Rep |
188 Attribute_Object_Size |
189 Attribute_Preelaborable_Initialization |
190 Attribute_Put_Image
=> True,
193 -- The following array contains all attributes that imply a modification
194 -- of their prefixes or result in an access value. Such prefixes can be
195 -- considered as lvalues.
197 Attribute_Name_Implies_Lvalue_Prefix
: constant Attribute_Set
:=
202 Attribute_Unchecked_Access |
203 Attribute_Unrestricted_Access
=> True,
206 -----------------------
207 -- Local_Subprograms --
208 -----------------------
210 procedure Eval_Attribute
(N
: Node_Id
);
211 -- Performs compile time evaluation of attributes where possible, leaving
212 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
213 -- set, and replacing the node with a literal node if the value can be
214 -- computed at compile time. All static attribute references are folded,
215 -- as well as a number of cases of non-static attributes that can always
216 -- be computed at compile time (e.g. floating-point model attributes that
217 -- are applied to non-static subtypes). Of course in such cases, the
218 -- Is_Static_Expression flag will not be set on the resulting literal.
219 -- Note that the only required action of this procedure is to catch the
220 -- static expression cases as described in the RM. Folding of other cases
221 -- is done where convenient, but some additional non-static folding is in
222 -- Expand_N_Attribute_Reference in cases where this is more convenient.
224 function Is_Anonymous_Tagged_Base
226 Typ
: Entity_Id
) return Boolean;
227 -- For derived tagged types that constrain parent discriminants we build
228 -- an anonymous unconstrained base type. We need to recognize the relation
229 -- between the two when analyzing an access attribute for a constrained
230 -- component, before the full declaration for Typ has been analyzed, and
231 -- where therefore the prefix of the attribute does not match the enclosing
234 procedure Set_Boolean_Result
(N
: Node_Id
; B
: Boolean);
235 -- Rewrites node N with an occurrence of either Standard_False or
236 -- Standard_True, depending on the value of the parameter B. The
237 -- result is marked as a static expression.
239 -----------------------
240 -- Analyze_Attribute --
241 -----------------------
243 procedure Analyze_Attribute
(N
: Node_Id
) is
244 Loc
: constant Source_Ptr
:= Sloc
(N
);
245 Aname
: constant Name_Id
:= Attribute_Name
(N
);
246 Exprs
: constant List_Id
:= Expressions
(N
);
247 Attr_Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
248 P_Old
: constant Node_Id
:= Prefix
(N
);
250 P
: Node_Id
:= P_Old
;
254 P_Type
: Entity_Id
:= Empty
;
255 -- Type of prefix after analysis
257 P_Base_Type
: Entity_Id
:= Empty
;
258 -- Base type of prefix after analysis
260 -----------------------
261 -- Local Subprograms --
262 -----------------------
264 procedure Address_Checks
;
265 -- Semantic checks for valid use of Address attribute. This was made
266 -- a separate routine with the idea of using it for unrestricted access
267 -- which seems like it should follow the same rules, but that turned
268 -- out to be impractical. So now this is only used for Address.
270 procedure Analyze_Access_Attribute
;
271 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
272 -- Internally, Id distinguishes which of the three cases is involved.
274 procedure Analyze_Attribute_Old_Result
275 (Legal
: out Boolean;
276 Spec_Id
: out Entity_Id
);
277 -- Common processing for attributes 'Old and 'Result. The routine checks
278 -- that the attribute appears in a postcondition-like aspect or pragma
279 -- associated with a suitable subprogram or a body. Flag Legal is set
280 -- when the above criteria are met. Spec_Id denotes the entity of the
281 -- subprogram [body] or Empty if the attribute is illegal.
283 procedure Analyze_Image_Attribute
(Str_Typ
: Entity_Id
);
284 -- Common processing for attributes 'Img, 'Image, 'Wide_Image, and
285 -- 'Wide_Wide_Image. The routine checks that the prefix is valid and
286 -- sets the type of the attribute to the one specified by Str_Typ (e.g.
287 -- Standard_String for 'Image and Standard_Wide_String for 'Wide_Image).
289 procedure Analyze_Index_Attribute
290 (Legal
: out Boolean;
291 Spec_Id
: out Entity_Id
);
292 -- Processing for attribute 'Index. It checks that the attribute appears
293 -- in a pre/postcondition-like aspect or pragma associated with an entry
294 -- family. Flag Legal is set when the above criteria are met. Spec_Id
295 -- denotes the entity of the wrapper of the entry family or Empty if
296 -- the attribute is illegal.
298 procedure Bad_Attribute_For_Predicate
;
299 -- Output error message for use of a predicate (First, Last, Range) not
300 -- allowed with a type that has predicates. If the type is a generic
301 -- actual, then the message is a warning, and we generate code to raise
302 -- program error with an appropriate reason. No error message is given
303 -- for internally generated uses of the attributes. This legality rule
304 -- only applies to scalar types.
306 procedure Check_Array_Or_Scalar_Type
;
307 -- Common procedure used by First, Last, Range attribute to check
308 -- that the prefix is a constrained array or scalar type, or a name
309 -- of an array object, and that an argument appears only if appropriate
310 -- (i.e. only in the array case).
312 procedure Check_Array_Type
;
313 -- Common semantic checks for all array attributes. Checks that the
314 -- prefix is a constrained array type or the name of an array object.
315 -- The error message for non-arrays is specialized appropriately.
317 procedure Check_Asm_Attribute
;
318 -- Common semantic checks for Asm_Input and Asm_Output attributes
320 procedure Check_Component
;
321 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
322 -- Position. Checks prefix is an appropriate selected component.
324 procedure Check_Decimal_Fixed_Point_Type
;
325 -- Check that prefix of attribute N is a decimal fixed-point type
327 procedure Check_Dereference
;
328 -- If the prefix of attribute is an object of an access type, then
329 -- introduce an explicit dereference, and adjust P_Type accordingly.
331 procedure Check_Discrete_Type
;
332 -- Verify that prefix of attribute N is a discrete type
335 -- Check that no attribute arguments are present
337 procedure Check_Either_E0_Or_E1
;
338 -- Check that there are zero or one attribute arguments present
341 -- Check that exactly one attribute argument is present
344 -- Check that two attribute arguments are present
346 procedure Check_Enum_Image
(Check_Enumeration_Maps
: Boolean := False);
347 -- Common processing for the Image and Value family of attributes,
348 -- including their Wide and Wide_Wide versions, Enum_Val, Img,
351 -- If the prefix type of an attribute is an enumeration type, set all
352 -- its literals as referenced, since the attribute function can
353 -- indirectly reference any of the literals. Set the referenced flag
354 -- only if the attribute is in the main code unit; otherwise an
355 -- improperly set reference when analyzing an inlined body will lose a
356 -- proper warning on a useless with_clause.
358 -- If Check_Enumeration_Maps is True, then the attribute expansion
359 -- requires enumeration maps, so check whether restriction
360 -- No_Enumeration_Maps is active.
362 procedure Check_First_Last_Valid
;
363 -- Perform all checks for First_Valid and Last_Valid attributes
365 procedure Check_Fixed_Point_Type
;
366 -- Verify that prefix of attribute N is a fixed type
368 procedure Check_Fixed_Point_Type_0
;
369 -- Verify that prefix of attribute N is a fixed type and that
370 -- no attribute expressions are present.
372 procedure Check_Floating_Point_Type
;
373 -- Verify that prefix of attribute N is a float type
375 procedure Check_Floating_Point_Type_0
;
376 -- Verify that prefix of attribute N is a float type and that
377 -- no attribute expressions are present.
379 procedure Check_Floating_Point_Type_1
;
380 -- Verify that prefix of attribute N is a float type and that
381 -- exactly one attribute expression is present.
383 procedure Check_Floating_Point_Type_2
;
384 -- Verify that prefix of attribute N is a float type and that
385 -- two attribute expressions are present.
387 procedure Check_Integer_Type
;
388 -- Verify that prefix of attribute N is an integer type
390 procedure Check_Modular_Integer_Type
;
391 -- Verify that prefix of attribute N is a modular integer type
393 procedure Check_Not_CPP_Type
;
394 -- Check that P (the prefix of the attribute) is not an CPP type
395 -- for which no Ada predefined primitive is available.
397 procedure Check_Not_Incomplete_Type
;
398 -- Check that P (the prefix of the attribute) is not an incomplete
399 -- type or a private type for which no full view has been given.
401 procedure Check_Object_Reference
(P
: Node_Id
);
402 -- Check that P is an object reference
404 procedure Check_PolyORB_Attribute
;
405 -- Validity checking for PolyORB/DSA attribute
407 procedure Check_Program_Unit
;
408 -- Verify that prefix of attribute N is a program unit
410 procedure Check_Real_Type
;
411 -- Verify that prefix of attribute N is fixed or float type
413 procedure Check_Enumeration_Type
;
414 -- Verify that prefix of attribute N is an enumeration type
416 procedure Check_Scalar_Type
;
417 -- Verify that prefix of attribute N is a scalar type
419 procedure Check_Standard_Prefix
;
420 -- Verify that prefix of attribute N is package Standard. Also checks
421 -- that there are no arguments.
423 procedure Check_Stream_Attribute
(Nam
: TSS_Name_Type
);
424 -- Validity checking for stream attribute. Nam is the TSS name of the
425 -- corresponding possible defined attribute function (e.g. for the
426 -- Read attribute, Nam will be TSS_Stream_Read).
428 procedure Check_Put_Image_Attribute
;
429 -- Validity checking for Put_Image attribute
431 procedure Check_System_Prefix
;
432 -- Verify that prefix of attribute N is package System
434 procedure Check_Task_Prefix
;
435 -- Verify that prefix of attribute N is a task or task type
437 procedure Check_Type
;
438 -- Verify that the prefix of attribute N is a type
440 procedure Check_Unit_Name
(Nod
: Node_Id
);
441 -- Check that Nod is of the form of a library unit name, i.e that
442 -- it is an identifier, or a selected component whose prefix is
443 -- itself of the form of a library unit name. Note that this is
444 -- quite different from Check_Program_Unit, since it only checks
445 -- the syntactic form of the name, not the semantic identity. This
446 -- is because it is used with attributes (Elab_Body, Elab_Spec and
447 -- Elaborated) which can refer to non-visible unit.
449 procedure Error_Attr
(Msg
: String; Error_Node
: Node_Id
);
450 pragma No_Return
(Error_Attr
);
451 procedure Error_Attr
;
452 pragma No_Return
(Error_Attr
);
453 -- Posts error using Error_Msg_N at given node, sets type of attribute
454 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
455 -- semantic processing. The message typically contains a % insertion
456 -- character which is replaced by the attribute name. The call with
457 -- no arguments is used when the caller has already generated the
458 -- required error messages.
460 procedure Error_Attr_P
(Msg
: String; Msg_Cont
: String := "");
461 pragma No_Return
(Error_Attr_P
);
462 -- Like Error_Attr, but error is posted at the start of the prefix. The
463 -- second message Msg_Cont is useful to issue a continuation message
464 -- before raising Bad_Attribute.
466 procedure Legal_Formal_Attribute
;
467 -- Common processing for attributes Definite and Has_Discriminants.
468 -- Checks that prefix is generic indefinite formal type.
470 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
471 -- Common processing for attributes Max_Alignment_For_Allocation and
472 -- Max_Size_In_Storage_Elements.
475 -- Common processing for attributes Max and Min
477 procedure Standard_Attribute
(Val
: Int
);
478 -- Used to process attributes whose prefix is package Standard which
479 -- yield values of type Universal_Integer. The attribute reference
480 -- node is rewritten with an integer literal of the given value which
481 -- is marked as static.
483 procedure Uneval_Old_Msg
;
484 -- Called when Loop_Entry or Old is used in a potentially unevaluated
485 -- expression. Generates appropriate message or warning depending on
486 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
487 -- node in the aspect case).
489 procedure Unexpected_Argument
(En
: Node_Id
);
490 pragma No_Return
(Unexpected_Argument
);
491 -- Signal unexpected attribute argument (En is the argument), and then
492 -- raises Bad_Attribute to avoid any further semantic processing.
494 procedure Validate_Non_Static_Attribute_Function_Call
;
495 -- Called when processing an attribute that is a function call to a
496 -- non-static function, i.e. an attribute function that either takes
497 -- non-scalar arguments or returns a non-scalar result. Verifies that
498 -- such a call does not appear in a preelaborable context.
504 procedure Address_Checks
is
506 -- An Address attribute created by expansion is legal even when it
507 -- applies to other entity-denoting expressions.
509 if not Comes_From_Source
(N
) then
512 -- Address attribute on a protected object self reference is legal
514 elsif Is_Protected_Self_Reference
(P
) then
517 -- Address applied to an entity
519 elsif Is_Entity_Name
(P
) then
521 Ent
: constant Entity_Id
:= Entity
(P
);
524 if Is_Subprogram
(Ent
) then
525 Set_Address_Taken
(Ent
);
527 -- An Address attribute is accepted when generated by the
528 -- compiler for dispatching operation, and an error is
529 -- issued once the subprogram is frozen (to avoid confusing
530 -- errors about implicit uses of Address in the dispatch
531 -- table initialization).
533 if Has_Pragma_Inline_Always
(Entity
(P
))
534 and then Comes_From_Source
(P
)
537 ("prefix of % attribute cannot be Inline_Always "
540 -- It is illegal to apply 'Address to an intrinsic
541 -- subprogram. This is now formalized in AI05-0095.
542 -- In an instance, an attempt to obtain 'Address of an
543 -- intrinsic subprogram (e.g the renaming of a predefined
544 -- operator that is an actual) raises Program_Error.
546 elsif Convention
(Ent
) = Convention_Intrinsic
then
549 Make_Raise_Program_Error
(Loc
,
550 Reason
=> PE_Address_Of_Intrinsic
));
553 Error_Msg_Name_1
:= Aname
;
555 ("cannot take % of intrinsic subprogram", N
);
558 -- Issue an error if prefix denotes an eliminated subprogram
561 Check_For_Eliminated_Subprogram
(P
, Ent
);
564 -- Object or label reference
566 elsif Is_Object_Reference
(P
) or else Ekind
(Ent
) = E_Label
then
567 Set_Address_Taken
(Ent
);
569 -- Deal with No_Implicit_Aliasing restriction
571 if Restriction_Check_Required
(No_Implicit_Aliasing
) then
572 if not Is_Aliased_View
(P
) then
573 Check_Restriction
(No_Implicit_Aliasing
, P
);
575 Check_No_Implicit_Aliasing
(P
);
579 -- If we have an address of an object, and the attribute
580 -- comes from source, then set the object as potentially
581 -- source modified. We do this because the resulting address
582 -- can potentially be used to modify the variable and we
583 -- might not detect this, leading to some junk warnings.
585 Set_Never_Set_In_Source
(Ent
, False);
587 -- Allow Address to be applied to task or protected type,
588 -- returning null address (what is that about???)
590 elsif (Is_Concurrent_Type
(Etype
(Ent
))
591 and then Etype
(Ent
) = Base_Type
(Ent
))
592 or else Ekind
(Ent
) = E_Package
593 or else Is_Generic_Unit
(Ent
)
596 New_Occurrence_Of
(RTE
(RE_Null_Address
), Sloc
(N
)));
598 -- Anything else is illegal
601 Error_Attr
("invalid prefix for % attribute", P
);
607 elsif Is_Object_Reference
(P
) then
610 -- Subprogram called using dot notation
612 elsif Nkind
(P
) = N_Selected_Component
613 and then Is_Subprogram
(Entity
(Selector_Name
(P
)))
617 -- What exactly are we allowing here ??? and is this properly
618 -- documented in the sinfo documentation for this node ???
620 elsif Relaxed_RM_Semantics
621 and then Nkind
(P
) = N_Attribute_Reference
625 -- All other non-entity name cases are illegal
628 Error_Attr
("invalid prefix for % attribute", P
);
632 ------------------------------
633 -- Analyze_Access_Attribute --
634 ------------------------------
636 procedure Analyze_Access_Attribute
is
637 Acc_Type
: Entity_Id
;
642 function Build_Access_Object_Type
(DT
: Entity_Id
) return Entity_Id
;
643 -- Build an access-to-object type whose designated type is DT,
644 -- and whose Ekind is appropriate to the attribute type. The
645 -- type that is constructed is returned as the result.
647 procedure Build_Access_Subprogram_Type
(P
: Node_Id
);
648 -- Build an access to subprogram whose designated type is the type of
649 -- the prefix. If prefix is overloaded, so is the node itself. The
650 -- result is stored in Acc_Type.
652 function OK_Self_Reference
return Boolean;
653 -- An access reference whose prefix is a type can legally appear
654 -- within an aggregate, where it is obtained by expansion of
655 -- a defaulted aggregate. The enclosing aggregate that contains
656 -- the self-referenced is flagged so that the self-reference can
657 -- be expanded into a reference to the target object (see exp_aggr).
659 ------------------------------
660 -- Build_Access_Object_Type --
661 ------------------------------
663 function Build_Access_Object_Type
(DT
: Entity_Id
) return Entity_Id
is
664 Typ
: constant Entity_Id
:=
666 (E_Access_Attribute_Type
, Current_Scope
, Loc
, 'A');
668 Set_Etype
(Typ
, Typ
);
670 Set_Associated_Node_For_Itype
(Typ
, N
);
671 Set_Directly_Designated_Type
(Typ
, DT
);
673 end Build_Access_Object_Type
;
675 ----------------------------------
676 -- Build_Access_Subprogram_Type --
677 ----------------------------------
679 procedure Build_Access_Subprogram_Type
(P
: Node_Id
) is
680 Index
: Interp_Index
;
683 procedure Check_Local_Access
(E
: Entity_Id
);
684 -- Deal with possible access to local subprogram. If we have such
685 -- an access, we set a flag to kill all tracked values on any call
686 -- because this access value may be passed around, and any called
687 -- code might use it to access a local procedure which clobbers a
688 -- tracked value. If the scope is a loop or block, indicate that
689 -- value tracking is disabled for the enclosing subprogram.
691 function Get_Convention
(E
: Entity_Id
) return Convention_Id
;
692 function Get_Kind
(E
: Entity_Id
) return Entity_Kind
;
693 -- Distinguish between access to regular/protected subprograms
695 ------------------------
696 -- Check_Local_Access --
697 ------------------------
699 procedure Check_Local_Access
(E
: Entity_Id
) is
701 if not Is_Library_Level_Entity
(E
) then
702 Set_Suppress_Value_Tracking_On_Call
(Current_Scope
);
703 Set_Suppress_Value_Tracking_On_Call
704 (Nearest_Dynamic_Scope
(Current_Scope
));
706 end Check_Local_Access
;
712 function Get_Convention
(E
: Entity_Id
) return Convention_Id
is
714 -- Restrict handling by_protected_procedure access subprograms
715 -- to source entities; required to avoid building access to
716 -- subprogram types with convention protected when building
719 if Comes_From_Source
(P
)
720 and then Is_By_Protected_Procedure
(E
)
722 return Convention_Protected
;
724 return Convention
(E
);
732 function Get_Kind
(E
: Entity_Id
) return Entity_Kind
is
734 if Get_Convention
(E
) = Convention_Protected
then
735 return E_Access_Protected_Subprogram_Type
;
737 return E_Access_Subprogram_Type
;
741 -- Start of processing for Build_Access_Subprogram_Type
744 -- In the case of an access to subprogram, use the name of the
745 -- subprogram itself as the designated type. Type-checking in
746 -- this case compares the signatures of the designated types.
748 -- Note: This fragment of the tree is temporarily malformed
749 -- because the correct tree requires an E_Subprogram_Type entity
750 -- as the designated type. In most cases this designated type is
751 -- later overridden by the semantics with the type imposed by the
752 -- context during the resolution phase. In the specific case of
753 -- the expression Address!(Prim'Unrestricted_Access), used to
754 -- initialize slots of dispatch tables, this work will be done by
755 -- the expander (see Exp_Aggr).
757 -- The reason to temporarily add this kind of node to the tree
758 -- instead of a proper E_Subprogram_Type itype, is the following:
759 -- in case of errors found in the source file we report better
760 -- error messages. For example, instead of generating the
763 -- "expected access to subprogram with profile
764 -- defined at line X"
766 -- we currently generate:
768 -- "expected access to function Z defined at line X"
770 Set_Etype
(N
, Any_Type
);
772 if not Is_Overloaded
(P
) then
773 Check_Local_Access
(Entity
(P
));
775 if not Is_Intrinsic_Subprogram
(Entity
(P
)) then
776 Acc_Type
:= Create_Itype
(Get_Kind
(Entity
(P
)), N
);
777 Set_Is_Public
(Acc_Type
, False);
778 Set_Etype
(Acc_Type
, Acc_Type
);
779 Set_Convention
(Acc_Type
, Get_Convention
(Entity
(P
)));
780 Set_Directly_Designated_Type
(Acc_Type
, Entity
(P
));
781 Set_Etype
(N
, Acc_Type
);
782 Freeze_Before
(N
, Acc_Type
);
786 Get_First_Interp
(P
, Index
, It
);
787 while Present
(It
.Nam
) loop
788 Check_Local_Access
(It
.Nam
);
790 if not Is_Intrinsic_Subprogram
(It
.Nam
) then
791 Acc_Type
:= Create_Itype
(Get_Kind
(It
.Nam
), N
);
792 Set_Is_Public
(Acc_Type
, False);
793 Set_Etype
(Acc_Type
, Acc_Type
);
794 Set_Convention
(Acc_Type
, Get_Convention
(It
.Nam
));
795 Set_Directly_Designated_Type
(Acc_Type
, It
.Nam
);
796 Add_One_Interp
(N
, Acc_Type
, Acc_Type
);
797 Freeze_Before
(N
, Acc_Type
);
800 Get_Next_Interp
(Index
, It
);
804 -- Cannot be applied to intrinsic. Looking at the tests above,
805 -- the only way Etype (N) can still be set to Any_Type is if
806 -- Is_Intrinsic_Subprogram was True for some referenced entity.
808 if Etype
(N
) = Any_Type
then
809 Error_Attr_P
("prefix of % attribute cannot be intrinsic");
811 end Build_Access_Subprogram_Type
;
813 ----------------------
814 -- OK_Self_Reference --
815 ----------------------
817 function OK_Self_Reference
return Boolean is
821 -- If N does not come from source, the reference is assumed to be
824 if not Comes_From_Source
(N
) then
831 (Nkind
(Par
) = N_Component_Association
832 or else Nkind
(Par
) in N_Subexpr
)
834 if Nkind
(Par
) in N_Aggregate | N_Extension_Aggregate
then
835 if Etype
(Par
) = Typ
then
836 Set_Has_Self_Reference
(Par
);
838 -- Check the context: the aggregate must be part of the
839 -- initialization of a type or component, or it is the
840 -- resulting expansion in an initialization procedure.
842 if Is_Init_Proc
(Current_Scope
) then
846 while Present
(Par
) loop
847 if Nkind
(Par
) = N_Full_Type_Declaration
then
862 -- No enclosing aggregate, or not a self-reference
865 end OK_Self_Reference
;
867 -- Start of processing for Analyze_Access_Attribute
870 -- Access and Unchecked_Access are illegal in declare_expressions,
871 -- according to the RM. We also make the GNAT Unrestricted_Access
872 -- attribute illegal if it comes from source.
874 if In_Declare_Expr
> 0
875 and then (Attr_Id
/= Attribute_Unrestricted_Access
876 or else Comes_From_Source
(N
))
878 Error_Attr
("% attribute cannot occur in a declare_expression", N
);
883 if Nkind
(P
) = N_Character_Literal
then
885 ("prefix of % attribute cannot be enumeration literal");
888 -- Preserve relevant elaboration-related attributes of the context
889 -- which are no longer available or very expensive to recompute once
890 -- analysis, resolution, and expansion are over.
892 Mark_Elaboration_Attributes
898 -- Save the scenario for later examination by the ABE Processing
901 Record_Elaboration_Scenario
(N
);
903 -- Case of access to subprogram
905 if Is_Entity_Name
(P
) and then Is_Overloadable
(Entity
(P
)) then
906 if Has_Pragma_Inline_Always
(Entity
(P
)) then
908 ("prefix of % attribute cannot be Inline_Always subprogram");
910 elsif Aname
= Name_Unchecked_Access
then
911 Error_Attr
("attribute% cannot be applied to a subprogram", P
);
914 -- Issue an error if the prefix denotes an eliminated subprogram
916 Check_For_Eliminated_Subprogram
(P
, Entity
(P
));
918 -- Check for obsolescent subprogram reference
920 Check_Obsolescent_2005_Entity
(Entity
(P
), P
);
922 -- Build the appropriate subprogram type
924 Build_Access_Subprogram_Type
(P
);
926 -- For P'Access or P'Unrestricted_Access, where P is a nested
927 -- subprogram, we might be passing P to another subprogram (but we
928 -- don't check that here), which might call P. P could modify
929 -- local variables, so we need to kill current values. It is
930 -- important not to do this for library-level subprograms, because
931 -- Kill_Current_Values is very inefficient in the case of library
932 -- level packages with lots of tagged types.
934 if Is_Library_Level_Entity
(Entity
(Prefix
(N
))) then
937 -- Do not kill values on nodes initializing dispatch tables
938 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
939 -- is currently generated by the expander only for this
940 -- purpose. Done to keep the quality of warnings currently
941 -- generated by the compiler (otherwise any declaration of
942 -- a tagged type cleans constant indications from its scope).
944 elsif Nkind
(Parent
(N
)) = N_Unchecked_Type_Conversion
945 and then (Is_RTE
(Etype
(Parent
(N
)), RE_Prim_Ptr
)
947 Is_RTE
(Etype
(Parent
(N
)), RE_Size_Ptr
))
948 and then Is_Dispatching_Operation
949 (Directly_Designated_Type
(Etype
(N
)))
957 -- In the static elaboration model, treat the attribute reference
958 -- as a subprogram call for elaboration purposes. Suppress this
959 -- treatment under debug flag. In any case, we are all done.
961 if Legacy_Elaboration_Checks
962 and not Dynamic_Elaboration_Checks
963 and not Debug_Flag_Dot_UU
970 -- Component is an operation of a protected type
972 elsif Nkind
(P
) = N_Selected_Component
973 and then Is_Overloadable
(Entity
(Selector_Name
(P
)))
975 if Ekind
(Entity
(Selector_Name
(P
))) = E_Entry
then
976 Error_Attr_P
("prefix of % attribute must be subprogram");
979 Build_Access_Subprogram_Type
(Selector_Name
(P
));
983 -- Deal with incorrect reference to a type, but note that some
984 -- accesses are allowed: references to the current type instance,
985 -- or in Ada 2005 self-referential pointer in a default-initialized
988 if Is_Entity_Name
(P
) then
991 if Is_Type
(Typ
) then
993 -- The reference may appear in an aggregate that has been
994 -- expanded into a loop. Locate scope of type definition,
997 Scop
:= Current_Scope_No_Loops
;
999 -- OK if we are within the scope of a limited type
1000 -- let's mark the component as having per object constraint
1002 if Is_Anonymous_Tagged_Base
(Scop
, Typ
) then
1004 Set_Entity
(P
, Typ
);
1008 -- A current instance typically appears immediately within
1009 -- the type declaration, but may be nested within an internally
1010 -- generated temporary scope - as for an aggregate of a
1011 -- discriminated component.
1014 or else (In_Open_Scopes
(Typ
)
1015 and then not Comes_From_Source
(Scop
))
1018 Q
: Node_Id
:= Parent
(N
);
1021 while Present
(Q
) loop
1022 if Nkind
(Q
) = N_Component_Declaration
then
1023 Set_Has_Per_Object_Constraint
1024 (Defining_Identifier
(Q
), True);
1027 -- Prevent the search from going too far
1029 elsif Is_Body_Or_Package_Declaration
(Q
) then
1037 if Nkind
(P
) = N_Expanded_Name
then
1039 ("current instance prefix must be a direct name", P
);
1042 -- If a current instance attribute appears in a component
1043 -- constraint it must appear alone; other contexts (spec-
1044 -- expressions, within a task body) are not subject to this
1047 if not In_Spec_Expression
1048 and then not Has_Completion
(Scop
)
1050 Nkind
(Parent
(N
)) not in
1051 N_Discriminant_Association |
1052 N_Index_Or_Discriminant_Constraint
1055 ("current instance attribute must appear alone", N
);
1058 if Is_CPP_Class
(Root_Type
(Typ
)) then
1060 ("??current instance unsupported for derivations of "
1061 & "'C'P'P types", N
);
1064 -- OK if we are in initialization procedure for the type
1065 -- in question, in which case the reference to the type
1066 -- is rewritten as a reference to the current object.
1068 elsif Ekind
(Scop
) = E_Procedure
1069 and then Is_Init_Proc
(Scop
)
1070 and then Etype
(First_Formal
(Scop
)) = Typ
1073 Make_Attribute_Reference
(Loc
,
1074 Prefix
=> Make_Identifier
(Loc
, Name_uInit
),
1075 Attribute_Name
=> Name_Unrestricted_Access
));
1079 -- OK if current task.
1081 elsif Is_Task_Type
(Typ
)
1082 and then In_Open_Scopes
(Typ
)
1086 -- OK if self-reference in an aggregate in Ada 2005, and
1087 -- the reference comes from a copied default expression.
1089 -- Note that we check legality of self-reference even if the
1090 -- expression comes from source, e.g. when a single component
1091 -- association in an aggregate has a box association.
1093 elsif Ada_Version
>= Ada_2005
and then OK_Self_Reference
then
1096 -- OK if reference to current instance of a protected object
1098 elsif Is_Protected_Self_Reference
(P
) then
1101 -- Otherwise we have an error case
1104 Error_Attr
("% attribute cannot be applied to type", P
);
1109 -- If we fall through, we have a normal access to object case
1111 -- Unrestricted_Access is (for now) legal wherever an allocator would
1112 -- be legal, so its Etype is set to E_Allocator. The expected type
1113 -- of the other attributes is a general access type, and therefore
1114 -- we label them with E_Access_Attribute_Type.
1116 if not Is_Overloaded
(P
) then
1117 Acc_Type
:= Build_Access_Object_Type
(P_Type
);
1118 Set_Etype
(N
, Acc_Type
);
1122 Index
: Interp_Index
;
1125 Set_Etype
(N
, Any_Type
);
1126 Get_First_Interp
(P
, Index
, It
);
1127 while Present
(It
.Typ
) loop
1128 Acc_Type
:= Build_Access_Object_Type
(It
.Typ
);
1129 Add_One_Interp
(N
, Acc_Type
, Acc_Type
);
1130 Get_Next_Interp
(Index
, It
);
1135 -- Special cases when we can find a prefix that is an entity name
1144 if Is_Entity_Name
(PP
) then
1147 -- If we have an access to an object, and the attribute
1148 -- comes from source, then set the object as potentially
1149 -- source modified. We do this because the resulting access
1150 -- pointer can be used to modify the variable, and we might
1151 -- not detect this, leading to some junk warnings.
1153 -- We do this only for source references, since otherwise
1154 -- we can suppress warnings, e.g. from the unrestricted
1155 -- access generated for validity checks in -gnatVa mode.
1157 if Comes_From_Source
(N
) then
1158 Set_Never_Set_In_Source
(Ent
, False);
1161 -- Mark entity as address taken in the case of
1162 -- 'Unrestricted_Access or subprograms, and kill current
1165 if Aname
= Name_Unrestricted_Access
1166 or else Is_Subprogram
(Ent
)
1168 Set_Address_Taken
(Ent
);
1171 Kill_Current_Values
(Ent
);
1174 elsif Nkind
(PP
) in N_Selected_Component | N_Indexed_Component
1183 end Analyze_Access_Attribute
;
1185 ----------------------------------
1186 -- Analyze_Attribute_Old_Result --
1187 ----------------------------------
1189 procedure Analyze_Attribute_Old_Result
1190 (Legal
: out Boolean;
1191 Spec_Id
: out Entity_Id
)
1193 procedure Check_Placement_In_Check
(Prag
: Node_Id
);
1194 -- Verify that the attribute appears within pragma Check that mimics
1197 procedure Check_Placement_In_Contract_Cases
(Prag
: Node_Id
);
1198 -- Verify that the attribute appears within a consequence of aspect
1199 -- or pragma Contract_Cases denoted by Prag.
1201 procedure Check_Placement_In_Test_Case
(Prag
: Node_Id
);
1202 -- Verify that the attribute appears within the "Ensures" argument of
1203 -- aspect or pragma Test_Case denoted by Prag.
1207 Encl_Nod
: Node_Id
) return Boolean;
1208 -- Subsidiary to Check_Placement_In_XXX. Determine whether arbitrary
1209 -- node Nod is within enclosing node Encl_Nod.
1211 procedure Placement_Error
;
1212 pragma No_Return
(Placement_Error
);
1213 -- Emit a general error when the attributes does not appear in a
1214 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1215 -- to avoid any further semantic processing.
1217 ------------------------------
1218 -- Check_Placement_In_Check --
1219 ------------------------------
1221 procedure Check_Placement_In_Check
(Prag
: Node_Id
) is
1222 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
1223 Nam
: constant Name_Id
:= Chars
(Get_Pragma_Arg
(First
(Args
)));
1226 -- The "Name" argument of pragma Check denotes a postcondition
1230 | Name_Postcondition
1235 -- Otherwise the placement of the attribute is illegal
1240 end Check_Placement_In_Check
;
1242 ---------------------------------------
1243 -- Check_Placement_In_Contract_Cases --
1244 ---------------------------------------
1246 procedure Check_Placement_In_Contract_Cases
(Prag
: Node_Id
) is
1252 -- Obtain the argument of the aspect or pragma
1254 if Nkind
(Prag
) = N_Aspect_Specification
then
1257 Arg
:= First
(Pragma_Argument_Associations
(Prag
));
1260 Cases
:= Expression
(Arg
);
1262 if Present
(Component_Associations
(Cases
)) then
1263 CCase
:= First
(Component_Associations
(Cases
));
1264 while Present
(CCase
) loop
1266 -- Detect whether the attribute appears within the
1267 -- consequence of the current contract case.
1269 if Nkind
(CCase
) = N_Component_Association
1270 and then Is_Within
(N
, Expression
(CCase
))
1279 -- Otherwise aspect or pragma Contract_Cases is either malformed
1280 -- or the attribute does not appear within a consequence.
1283 ("attribute % must appear in the consequence of a contract case",
1285 end Check_Placement_In_Contract_Cases
;
1287 ----------------------------------
1288 -- Check_Placement_In_Test_Case --
1289 ----------------------------------
1291 procedure Check_Placement_In_Test_Case
(Prag
: Node_Id
) is
1292 Arg
: constant Node_Id
:=
1295 Arg_Nam
=> Name_Ensures
,
1296 From_Aspect
=> Nkind
(Prag
) = N_Aspect_Specification
);
1299 -- Detect whether the attribute appears within the "Ensures"
1300 -- expression of aspect or pragma Test_Case.
1302 if Present
(Arg
) and then Is_Within
(N
, Arg
) then
1307 ("attribute % must appear in the ensures expression of a "
1310 end Check_Placement_In_Test_Case
;
1318 Encl_Nod
: Node_Id
) return Boolean
1324 while Present
(Par
) loop
1325 if Par
= Encl_Nod
then
1328 -- Prevent the search from going too far
1330 elsif Is_Body_Or_Package_Declaration
(Par
) then
1334 Par
:= Parent
(Par
);
1340 ---------------------
1341 -- Placement_Error --
1342 ---------------------
1344 procedure Placement_Error
is
1346 if Aname
= Name_Old
then
1347 Error_Attr
("attribute % can only appear in postcondition", P
);
1349 -- Specialize the error message for attribute 'Result
1353 ("attribute % can only appear in postcondition of function",
1356 end Placement_Error
;
1362 Subp_Decl
: Node_Id
;
1364 -- Start of processing for Analyze_Attribute_Old_Result
1367 -- Assume that the attribute is illegal
1372 -- Skip processing during preanalysis of class-wide preconditions and
1373 -- postconditions since at this stage the expression is not installed
1374 -- yet on its definite context.
1376 if Inside_Class_Condition_Preanalysis
then
1379 -- Search for the subprogram that has this class-wide condition;
1380 -- required to avoid reporting spurious errors since the current
1381 -- scope may not be appropriate because the attribute may be
1382 -- referenced from the inner scope of, for example, quantified
1385 -- Although the expression is not installed on its definite
1386 -- context, we know that the subprogram has been placed in the
1387 -- scope stack by Preanalyze_Condition; we also know that it is
1388 -- not a generic subprogram since class-wide pre/postconditions
1389 -- can only be applied for primitive operations of tagged types.
1391 if Is_Subprogram
(Current_Scope
) then
1392 Spec_Id
:= Current_Scope
;
1394 Spec_Id
:= Enclosing_Subprogram
(Current_Scope
);
1397 pragma Assert
(Is_Dispatching_Operation
(Spec_Id
));
1401 -- Traverse the parent chain to find the aspect or pragma where the
1402 -- attribute resides.
1405 while Present
(Prag
) loop
1406 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1409 -- Prevent the search from going too far
1411 elsif Is_Body_Or_Package_Declaration
(Prag
) then
1415 Prag
:= Parent
(Prag
);
1418 -- The attribute is allowed to appear only in postcondition-like
1419 -- aspects or pragmas.
1421 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1422 if Nkind
(Prag
) = N_Aspect_Specification
then
1423 Prag_Nam
:= Chars
(Identifier
(Prag
));
1425 Prag_Nam
:= Pragma_Name
(Prag
);
1428 if Prag_Nam
= Name_Check
then
1429 Check_Placement_In_Check
(Prag
);
1431 elsif Prag_Nam
= Name_Contract_Cases
then
1432 Check_Placement_In_Contract_Cases
(Prag
);
1434 -- Attributes 'Old and 'Result are allowed to appear in
1435 -- consequence of aspect or pragma Exceptional_Cases. We already
1436 -- examined the exception_choice part of contract syntax, so we
1437 -- can accept all remaining occurrences within the pragma.
1439 elsif Prag_Nam
= Name_Exceptional_Cases
then
1442 -- Attribute 'Result is allowed to appear in aspect or pragma
1443 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1445 elsif Prag_Nam
in Name_Depends | Name_Refined_Depends
1446 and then Aname
= Name_Result
1450 -- Attribute 'Result is allowed to appear in aspect
1451 -- Relaxed_Initialization (SPARK RM 6.10).
1453 elsif Prag_Nam
= Name_Relaxed_Initialization
1454 and then Aname
= Name_Result
1458 elsif Prag_Nam
in Name_Post
1460 | Name_Postcondition
1465 elsif Prag_Nam
= Name_Test_Case
then
1466 Check_Placement_In_Test_Case
(Prag
);
1472 -- 'Old attribute reference ok in a _Wrapped_Statements procedure
1474 elsif Nkind
(Prag
) = N_Subprogram_Body
1475 and then Ekind
(Defining_Entity
(Prag
)) in Subprogram_Kind
1476 and then Present
(Wrapped_Statements
(Defining_Entity
(Prag
)))
1480 -- Otherwise the placement of the attribute is illegal
1486 -- Find the related subprogram subject to the aspect or pragma
1488 if Nkind
(Prag
) = N_Aspect_Specification
then
1489 Subp_Decl
:= Parent
(Prag
);
1490 elsif Nkind
(Prag
) = N_Subprogram_Body
then
1493 Subp_Decl
:= Find_Related_Declaration_Or_Body
(Prag
);
1496 -- 'Old objects appear in block and extended return statements as
1497 -- part of the expansion of contract wrappers.
1499 if Nkind
(Subp_Decl
) in N_Block_Statement
1500 | N_Extended_Return_Statement
1502 Subp_Decl
:= Parent
(Parent
(Subp_Decl
));
1505 -- The aspect or pragma where the attribute resides should be
1506 -- associated with a subprogram declaration or a body. If this is not
1507 -- the case, then the aspect or pragma is illegal. Return as analysis
1508 -- cannot be carried out. Note that it is legal to have the aspect
1509 -- appear on a subprogram renaming, when the renamed entity is an
1510 -- attribute reference.
1512 if Nkind
(Subp_Decl
) not in N_Abstract_Subprogram_Declaration
1513 | N_Entry_Declaration
1514 | N_Expression_Function
1515 | N_Full_Type_Declaration
1516 | N_Generic_Subprogram_Declaration
1518 | N_Subprogram_Body_Stub
1519 | N_Subprogram_Declaration
1520 | N_Subprogram_Renaming_Declaration
1525 -- If we get here, then the attribute is legal
1528 Spec_Id
:= Unique_Defining_Entity
(Subp_Decl
);
1529 end Analyze_Attribute_Old_Result
;
1531 -----------------------------
1532 -- Analyze_Image_Attribute --
1533 -----------------------------
1535 procedure Analyze_Image_Attribute
(Str_Typ
: Entity_Id
) is
1536 procedure Check_Image_Type
(Image_Type
: Entity_Id
);
1537 -- Check that Image_Type is legal as the type of a prefix of 'Image.
1538 -- Legality depends on the Ada language version.
1540 ----------------------
1541 -- Check_Image_Type --
1542 ----------------------
1544 procedure Check_Image_Type
(Image_Type
: Entity_Id
) is
1546 -- Image_Type may be empty in case of another error detected,
1547 -- or if an N_Raise_xxx_Error node is a parent of N.
1549 if Ada_Version
< Ada_2022
1550 and then Present
(Image_Type
)
1551 and then not Is_Scalar_Type
(Image_Type
)
1553 Error_Msg_Ada_2022_Feature
("nonscalar ''Image", Sloc
(P
));
1556 elsif Present
(Image_Type
)
1557 and then Ekind
(Image_Type
) = E_Access_Attribute_Type
1559 -- reject Some_Object'[Unchecked_]Access'[Wide_[Wide_]]Image
1560 Error_Msg_N
("illegal Image attribute prefix", N
);
1563 end Check_Image_Type
;
1565 -- Start of processing for Analyze_Image_Attribute
1568 -- AI12-0124: The ARG has adopted the GNAT semantics of 'Img for
1569 -- scalar types, so that the prefix can be an object, a named value,
1570 -- or a type. If the prefix is an object, there is no argument.
1572 if Is_Object_Image
(P
) then
1574 Set_Etype
(N
, Str_Typ
);
1575 Check_Image_Type
(Etype
(P
));
1577 if Attr_Id
/= Attribute_Img
then
1578 Error_Msg_Ada_2012_Feature
("|Object''Image", Sloc
(P
));
1582 Set_Etype
(N
, Str_Typ
);
1584 pragma Assert
(Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)));
1586 if Ekind
(Entity
(P
)) = E_Incomplete_Type
1587 and then Present
(Full_View
(Entity
(P
)))
1589 P_Type
:= Full_View
(Entity
(P
));
1590 P_Base_Type
:= Base_Type
(P_Type
);
1591 Set_Entity
(P
, P_Type
);
1594 Check_Image_Type
(P_Type
);
1595 Resolve
(E1
, P_Base_Type
);
1596 Validate_Non_Static_Attribute_Function_Call
;
1599 Check_Enum_Image
(Check_Enumeration_Maps
=> True);
1601 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1602 -- to avoid giving a duplicate message for when Image attributes
1603 -- applied to object references get expanded into type-based Image
1606 if Restriction_Check_Required
(No_Fixed_IO
)
1607 and then Comes_From_Source
(N
)
1608 and then Is_Fixed_Point_Type
(P_Type
)
1610 Check_Restriction
(No_Fixed_IO
, P
);
1612 end Analyze_Image_Attribute
;
1614 -----------------------------
1615 -- Analyze_Index_Attribute --
1616 -----------------------------
1618 procedure Analyze_Index_Attribute
1619 (Legal
: out Boolean;
1620 Spec_Id
: out Entity_Id
)
1622 procedure Check_Placement_In_Check
(Prag
: Node_Id
);
1623 -- Verify that the attribute appears within pragma Check that mimics
1626 procedure Placement_Error
;
1627 pragma No_Return
(Placement_Error
);
1628 -- Emit a general error when the attributes does not appear in a
1629 -- precondition or postcondition aspect or pragma, and then raises
1630 -- Bad_Attribute to avoid any further semantic processing.
1632 ------------------------------
1633 -- Check_Placement_In_Check --
1634 ------------------------------
1636 procedure Check_Placement_In_Check
(Prag
: Node_Id
) is
1637 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
1638 Nam
: constant Name_Id
:= Chars
(Get_Pragma_Arg
(First
(Args
)));
1641 -- The "Name" argument of pragma Check denotes a precondition or
1645 | Name_Postcondition
1652 -- Otherwise the placement of the attribute is illegal
1657 end Check_Placement_In_Check
;
1659 ---------------------
1660 -- Placement_Error --
1661 ---------------------
1663 procedure Placement_Error
is
1666 ("attribute % can only appear in pre- or postcondition", P
);
1667 end Placement_Error
;
1673 Subp_Decl
: Node_Id
;
1675 -- Start of processing for Analyze_Index_Attribute
1678 -- Assume that the attribute is illegal
1683 -- Skip processing during preanalysis of class-wide preconditions and
1684 -- postconditions since at this stage the expression is not installed
1685 -- yet on its definite context.
1687 if Inside_Class_Condition_Preanalysis
then
1689 Spec_Id
:= Current_Scope
;
1693 -- Traverse the parent chain to find the aspect or pragma where the
1694 -- attribute resides.
1697 while Present
(Prag
) loop
1698 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1701 -- Prevent the search from going too far
1703 elsif Is_Body_Or_Package_Declaration
(Prag
) then
1707 Prag
:= Parent
(Prag
);
1710 -- The attribute is allowed to appear only in precondition and
1711 -- postcondition-like aspects or pragmas.
1713 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1714 if Nkind
(Prag
) = N_Aspect_Specification
then
1715 Prag_Nam
:= Chars
(Identifier
(Prag
));
1717 Prag_Nam
:= Pragma_Name
(Prag
);
1720 if Prag_Nam
= Name_Check
then
1721 Check_Placement_In_Check
(Prag
);
1723 elsif Prag_Nam
in Name_Post
1724 | Name_Postcondition
1735 -- Otherwise the placement of the attribute is illegal
1741 -- Find the related subprogram subject to the aspect or pragma
1743 if Nkind
(Prag
) = N_Aspect_Specification
then
1744 Subp_Decl
:= Parent
(Prag
);
1746 Subp_Decl
:= Find_Related_Declaration_Or_Body
(Prag
);
1749 -- The aspect or pragma where the attribute resides should be
1750 -- associated with a subprogram declaration or a body since the
1751 -- analysis of pre-/postconditions of entry and entry families is
1752 -- performed in their wrapper subprogram. If this is not the case,
1753 -- then the aspect or pragma is illegal and no further analysis is
1756 if Nkind
(Subp_Decl
) not in N_Subprogram_Body
1757 | N_Subprogram_Declaration
1762 Spec_Id
:= Unique_Defining_Entity
(Subp_Decl
);
1764 -- If we get here and Spec_Id denotes the entity of the entry wrapper
1765 -- (or the postcondition procedure of the entry wrapper) then the
1766 -- attribute is legal.
1768 if Is_Entry_Wrapper
(Spec_Id
) then
1771 elsif Chars
(Spec_Id
) = Name_uWrapped_Statements
1772 and then Is_Entry_Wrapper
(Scope
(Spec_Id
))
1774 Spec_Id
:= Scope
(Spec_Id
);
1777 -- Otherwise the attribute is illegal and we return Empty
1782 end Analyze_Index_Attribute
;
1784 ---------------------------------
1785 -- Bad_Attribute_For_Predicate --
1786 ---------------------------------
1788 procedure Bad_Attribute_For_Predicate
is
1790 if Is_Scalar_Type
(P_Type
)
1791 and then Comes_From_Source
(N
)
1793 Error_Msg_Name_1
:= Aname
;
1794 Bad_Predicated_Subtype_Use
1795 ("type& has predicates, attribute % not allowed", N
, P_Type
);
1797 end Bad_Attribute_For_Predicate
;
1799 --------------------------------
1800 -- Check_Array_Or_Scalar_Type --
1801 --------------------------------
1803 procedure Check_Array_Or_Scalar_Type
is
1804 function In_Aspect_Specification
return Boolean;
1805 -- A current instance of a type in an aspect specification is an
1806 -- object and not a type, and therefore cannot be of a scalar type
1807 -- in the prefix of one of the array attributes if the attribute
1808 -- reference is part of an aspect expression.
1810 -----------------------------
1811 -- In_Aspect_Specification --
1812 -----------------------------
1814 function In_Aspect_Specification
return Boolean is
1819 while Present
(P
) loop
1820 if Nkind
(P
) = N_Aspect_Specification
then
1821 return P_Type
= Entity
(P
);
1823 -- Prevent the search from going too far
1825 elsif Is_Body_Or_Package_Declaration
(P
) then
1833 end In_Aspect_Specification
;
1839 -- Start of processing for Check_Array_Or_Scalar_Type
1842 -- Case of string literal or string literal subtype. These cases
1843 -- cannot arise from legal Ada code, but the expander is allowed
1844 -- to generate them. They require special handling because string
1845 -- literal subtypes do not have standard bounds (the whole idea
1846 -- of these subtypes is to avoid having to generate the bounds)
1848 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1849 Set_Etype
(N
, Etype
(First_Index
(P_Base_Type
)));
1854 elsif Is_Scalar_Type
(P_Type
) then
1857 if Present
(E1
) then
1858 Error_Attr
("invalid argument in % attribute", E1
);
1860 elsif In_Aspect_Specification
then
1862 ("prefix of % attribute cannot be the current instance of a "
1863 & "scalar type", P
);
1866 Set_Etype
(N
, P_Base_Type
);
1870 -- The following is a special test to allow 'First to apply to
1871 -- private scalar types if the attribute comes from generated
1872 -- code. This occurs in the case of Normalize_Scalars code.
1874 elsif Is_Private_Type
(P_Type
)
1875 and then Present
(Full_View
(P_Type
))
1876 and then Is_Scalar_Type
(Full_View
(P_Type
))
1877 and then not Comes_From_Source
(N
)
1879 Set_Etype
(N
, Implementation_Base_Type
(P_Type
));
1881 -- Array types other than string literal subtypes handled above
1886 -- We know prefix is an array type, or the name of an array
1887 -- object, and that the expression, if present, is static
1888 -- and within the range of the dimensions of the type.
1890 pragma Assert
(Is_Array_Type
(P_Type
));
1891 Index
:= First_Index
(P_Base_Type
);
1895 -- First dimension assumed
1897 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1901 Udims
: constant Uint
:= Expr_Value
(E1
);
1902 Dims
: constant Int
:= UI_To_Int
(Udims
);
1904 for J
in 1 .. Dims
- 1 loop
1909 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1912 end Check_Array_Or_Scalar_Type
;
1914 ----------------------
1915 -- Check_Array_Type --
1916 ----------------------
1918 procedure Check_Array_Type
is
1920 -- Dimension number for array attributes
1923 -- If the type is a string literal type, then this must be generated
1924 -- internally, and no further check is required on its legality.
1926 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1929 -- If the type is a composite, it is an illegal aggregate, no point
1932 elsif P_Type
= Any_Composite
then
1933 raise Bad_Attribute
;
1936 -- Normal case of array type or subtype. Note that if the
1937 -- prefix is a current instance of a type declaration it
1938 -- appears within an aspect specification and is legal.
1940 Check_Either_E0_Or_E1
;
1943 if Is_Array_Type
(P_Type
) then
1944 if not Is_Constrained
(P_Type
)
1945 and then Is_Entity_Name
(P
)
1946 and then Is_Type
(Entity
(P
))
1947 and then not Is_Current_Instance
(P
)
1949 -- Note: we do not call Error_Attr here, since we prefer to
1950 -- continue, using the relevant index type of the array,
1951 -- even though it is unconstrained. This gives better error
1952 -- recovery behavior.
1954 Error_Msg_Name_1
:= Aname
;
1956 ("prefix for % attribute must be constrained array", P
);
1959 -- The attribute reference freezes the type, and thus the
1960 -- component type, even if the attribute may not depend on the
1961 -- component. Diagnose arrays with incomplete components now.
1962 -- If the prefix is an access to array, this does not freeze
1963 -- the designated type.
1965 if Nkind
(P
) /= N_Explicit_Dereference
then
1966 Check_Fully_Declared
(Component_Type
(P_Type
), P
);
1969 D
:= Number_Dimensions
(P_Type
);
1972 if Is_Private_Type
(P_Type
) then
1973 Error_Attr_P
("prefix for % attribute may not be private type");
1975 elsif Is_Access_Type
(P_Type
)
1976 and then Is_Array_Type
(Designated_Type
(P_Type
))
1977 and then Is_Entity_Name
(P
)
1978 and then Is_Type
(Entity
(P
))
1980 Error_Attr_P
("prefix of % attribute cannot be access type");
1982 elsif Attr_Id
= Attribute_First
1984 Attr_Id
= Attribute_Last
1986 Error_Attr
("invalid prefix for % attribute", P
);
1989 Error_Attr_P
("prefix for % attribute must be array");
1993 if Present
(E1
) then
1994 Resolve
(E1
, Any_Integer
);
1995 Set_Etype
(E1
, Standard_Integer
);
1997 if not Is_OK_Static_Expression
(E1
)
1998 or else Raises_Constraint_Error
(E1
)
2000 Flag_Non_Static_Expr
2001 ("expression for dimension must be static!", E1
);
2006 Value
: constant Uint
:= Expr_Value
(E1
);
2009 if Value
> D
or else Value
< 1 then
2010 Error_Attr
("invalid dimension number for array type", E1
);
2013 -- Replace the static value to simplify the tree for gigi
2014 Fold_Uint
(E1
, Value
, True);
2019 if (Style_Check
and Style_Check_Array_Attribute_Index
)
2020 and then Comes_From_Source
(N
)
2022 Style
.Check_Array_Attribute_Index
(N
, E1
, D
);
2024 end Check_Array_Type
;
2026 -------------------------
2027 -- Check_Asm_Attribute --
2028 -------------------------
2030 procedure Check_Asm_Attribute
is
2035 -- Check first argument is static string expression
2037 Analyze_And_Resolve
(E1
, Standard_String
);
2039 if Etype
(E1
) = Any_Type
then
2042 elsif not Is_OK_Static_Expression
(E1
) then
2043 Flag_Non_Static_Expr
2044 ("constraint argument must be static string expression!", E1
);
2048 -- Check second argument is right type
2050 Analyze_And_Resolve
(E2
, Entity
(P
));
2052 -- Note: that is all we need to do, we don't need to check
2053 -- that it appears in a correct context. The Ada type system
2054 -- will do that for us.
2056 end Check_Asm_Attribute
;
2058 ---------------------
2059 -- Check_Component --
2060 ---------------------
2062 procedure Check_Component
is
2066 if Nkind
(P
) /= N_Selected_Component
2068 (Ekind
(Entity
(Selector_Name
(P
))) /= E_Component
2070 Ekind
(Entity
(Selector_Name
(P
))) /= E_Discriminant
)
2072 Error_Attr_P
("prefix for % attribute must be selected component");
2074 end Check_Component
;
2076 ------------------------------------
2077 -- Check_Decimal_Fixed_Point_Type --
2078 ------------------------------------
2080 procedure Check_Decimal_Fixed_Point_Type
is
2084 if not Is_Decimal_Fixed_Point_Type
(P_Type
) then
2085 Error_Attr_P
("prefix of % attribute must be decimal type");
2087 end Check_Decimal_Fixed_Point_Type
;
2089 -----------------------
2090 -- Check_Dereference --
2091 -----------------------
2093 procedure Check_Dereference
is
2095 -- Case of a subtype mark
2097 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
2101 -- Case of an expression
2105 if Is_Access_Type
(P_Type
) then
2107 -- If there is an implicit dereference, then we must freeze the
2108 -- designated type of the access type, since the type of the
2109 -- referenced array is this type (see AI95-00106).
2111 -- As done elsewhere, freezing must not happen when preanalyzing
2112 -- a pre- or postcondition or a default value for an object or for
2113 -- a formal parameter.
2115 if not In_Spec_Expression
then
2116 Freeze_Before
(N
, Designated_Type
(P_Type
));
2120 Make_Explicit_Dereference
(Sloc
(P_Old
),
2121 Prefix
=> Relocate_Node
(P_Old
)));
2123 Analyze_And_Resolve
(P_Old
);
2124 P_Type
:= Etype
(P_Old
);
2126 if P_Type
= Any_Type
then
2127 raise Bad_Attribute
;
2130 P_Base_Type
:= Base_Type
(P_Type
);
2132 end Check_Dereference
;
2134 -------------------------
2135 -- Check_Discrete_Type --
2136 -------------------------
2138 procedure Check_Discrete_Type
is
2142 if not Is_Discrete_Type
(P_Type
) then
2143 Error_Attr_P
("prefix of % attribute must be discrete type");
2145 end Check_Discrete_Type
;
2151 procedure Check_E0
is
2153 if Present
(E1
) then
2154 Unexpected_Argument
(E1
);
2162 procedure Check_E1
is
2164 Check_Either_E0_Or_E1
;
2168 -- Special-case attributes that are functions and that appear as
2169 -- the prefix of another attribute. Error is posted on parent.
2171 if Nkind
(Parent
(N
)) = N_Attribute_Reference
2172 and then Attribute_Name
(Parent
(N
)) in Name_Address
2176 Error_Msg_Name_1
:= Attribute_Name
(Parent
(N
));
2177 Error_Msg_N
("illegal prefix for % attribute", Parent
(N
));
2178 Set_Etype
(Parent
(N
), Any_Type
);
2179 Set_Entity
(Parent
(N
), Any_Type
);
2180 raise Bad_Attribute
;
2183 Error_Attr
("missing argument for % attribute", N
);
2192 procedure Check_E2
is
2195 Error_Attr
("missing arguments for % attribute (2 required)", N
);
2197 Error_Attr
("missing argument for % attribute (2 required)", N
);
2201 ---------------------------
2202 -- Check_Either_E0_Or_E1 --
2203 ---------------------------
2205 procedure Check_Either_E0_Or_E1
is
2207 if Present
(E2
) then
2208 Unexpected_Argument
(E2
);
2210 end Check_Either_E0_Or_E1
;
2212 ----------------------
2213 -- Check_Enum_Image --
2214 ----------------------
2216 procedure Check_Enum_Image
(Check_Enumeration_Maps
: Boolean := False) is
2220 -- Ensure that Check_Enumeration_Maps parameter is set precisely for
2221 -- attributes whose implementation requires enumeration maps.
2224 (Check_Enumeration_Maps
= (Attr_Id
in Attribute_Image
2226 | Attribute_Valid_Value
2228 | Attribute_Wide_Image
2229 | Attribute_Wide_Value
2230 | Attribute_Wide_Wide_Image
2231 | Attribute_Wide_Wide_Value
));
2233 -- When an enumeration type appears in an attribute reference, all
2234 -- literals of the type are marked as referenced. This must only be
2235 -- done if the attribute reference appears in the current source.
2236 -- Otherwise the information on references may differ between a
2237 -- normal compilation and one that performs inlining.
2239 if Is_Enumeration_Type
(P_Base_Type
)
2240 and then In_Extended_Main_Code_Unit
(N
)
2242 if Check_Enumeration_Maps
then
2243 Check_Restriction
(No_Enumeration_Maps
, N
);
2246 Lit
:= First_Literal
(P_Base_Type
);
2247 while Present
(Lit
) loop
2248 Set_Referenced
(Lit
);
2252 end Check_Enum_Image
;
2254 ----------------------------
2255 -- Check_First_Last_Valid --
2256 ----------------------------
2258 procedure Check_First_Last_Valid
is
2260 Check_Discrete_Type
;
2262 -- Freeze the subtype now, so that the following test for predicates
2263 -- works (we set the predicates stuff up at freeze time)
2265 Insert_Actions
(N
, Freeze_Entity
(P_Type
, P
));
2267 -- Now test for dynamic predicate
2269 if Has_Predicates
(P_Type
)
2270 and then not (Has_Static_Predicate
(P_Type
))
2273 ("prefix of % attribute may not have dynamic predicate");
2276 -- Check non-static subtype
2278 if not Is_OK_Static_Subtype
(P_Type
) then
2279 Error_Attr_P
("prefix of % attribute must be a static subtype");
2282 -- Test case for no values
2284 if Expr_Value
(Type_Low_Bound
(P_Type
)) >
2285 Expr_Value
(Type_High_Bound
(P_Type
))
2286 or else (Has_Predicates
(P_Type
)
2288 Is_Empty_List
(Static_Discrete_Predicate
(P_Type
)))
2291 ("prefix of % attribute must be subtype with at least one "
2294 end Check_First_Last_Valid
;
2296 ----------------------------
2297 -- Check_Fixed_Point_Type --
2298 ----------------------------
2300 procedure Check_Fixed_Point_Type
is
2304 if not Is_Fixed_Point_Type
(P_Type
) then
2305 Error_Attr_P
("prefix of % attribute must be fixed point type");
2307 end Check_Fixed_Point_Type
;
2309 ------------------------------
2310 -- Check_Fixed_Point_Type_0 --
2311 ------------------------------
2313 procedure Check_Fixed_Point_Type_0
is
2315 Check_Fixed_Point_Type
;
2317 end Check_Fixed_Point_Type_0
;
2319 -------------------------------
2320 -- Check_Floating_Point_Type --
2321 -------------------------------
2323 procedure Check_Floating_Point_Type
is
2327 if not Is_Floating_Point_Type
(P_Type
) then
2328 Error_Attr_P
("prefix of % attribute must be float type");
2330 end Check_Floating_Point_Type
;
2332 ---------------------------------
2333 -- Check_Floating_Point_Type_0 --
2334 ---------------------------------
2336 procedure Check_Floating_Point_Type_0
is
2338 Check_Floating_Point_Type
;
2340 end Check_Floating_Point_Type_0
;
2342 ---------------------------------
2343 -- Check_Floating_Point_Type_1 --
2344 ---------------------------------
2346 procedure Check_Floating_Point_Type_1
is
2348 Check_Floating_Point_Type
;
2350 end Check_Floating_Point_Type_1
;
2352 ---------------------------------
2353 -- Check_Floating_Point_Type_2 --
2354 ---------------------------------
2356 procedure Check_Floating_Point_Type_2
is
2358 Check_Floating_Point_Type
;
2360 end Check_Floating_Point_Type_2
;
2362 ------------------------
2363 -- Check_Integer_Type --
2364 ------------------------
2366 procedure Check_Integer_Type
is
2370 if not Is_Integer_Type
(P_Type
) then
2371 Error_Attr_P
("prefix of % attribute must be integer type");
2373 end Check_Integer_Type
;
2375 --------------------------------
2376 -- Check_Modular_Integer_Type --
2377 --------------------------------
2379 procedure Check_Modular_Integer_Type
is
2383 if not Is_Modular_Integer_Type
(P_Type
) then
2385 ("prefix of % attribute must be modular integer type");
2387 end Check_Modular_Integer_Type
;
2389 ------------------------
2390 -- Check_Not_CPP_Type --
2391 ------------------------
2393 procedure Check_Not_CPP_Type
is
2395 if Is_Tagged_Type
(Etype
(P
))
2396 and then Convention
(Etype
(P
)) = Convention_CPP
2397 and then Is_CPP_Class
(Root_Type
(Etype
(P
)))
2400 ("invalid use of % attribute with 'C'P'P tagged type");
2402 end Check_Not_CPP_Type
;
2404 -------------------------------
2405 -- Check_Not_Incomplete_Type --
2406 -------------------------------
2408 procedure Check_Not_Incomplete_Type
is
2413 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2414 -- dereference we have to check wrong uses of incomplete types
2415 -- (other wrong uses are checked at their freezing point).
2417 -- In Ada 2012, incomplete types can appear in subprogram
2418 -- profiles, but formals with incomplete types cannot be the
2419 -- prefix of attributes.
2421 -- Example 1: Limited-with
2423 -- limited with Pkg;
2425 -- type Acc is access Pkg.T;
2427 -- S : Integer := X.all'Size; -- ERROR
2430 -- Example 2: Tagged incomplete
2432 -- type T is tagged;
2433 -- type Acc is access all T;
2435 -- S : constant Integer := X.all'Size; -- ERROR
2436 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2438 if Ada_Version
>= Ada_2005
2439 and then Nkind
(P
) = N_Explicit_Dereference
2442 while Nkind
(E
) = N_Explicit_Dereference
loop
2448 if From_Limited_With
(Typ
) then
2450 ("prefix of % attribute cannot be an incomplete type");
2452 -- If the prefix is an access type check the designated type
2454 elsif Is_Access_Type
(Typ
)
2455 and then Nkind
(P
) = N_Explicit_Dereference
2457 Typ
:= Directly_Designated_Type
(Typ
);
2460 if Is_Class_Wide_Type
(Typ
) then
2461 Typ
:= Root_Type
(Typ
);
2464 -- A legal use of a shadow entity occurs only when the unit where
2465 -- the non-limited view resides is imported via a regular with
2466 -- clause in the current body. Such references to shadow entities
2467 -- may occur in subprogram formals.
2469 if Is_Incomplete_Type
(Typ
)
2470 and then From_Limited_With
(Typ
)
2471 and then Present
(Non_Limited_View
(Typ
))
2472 and then Is_Legal_Shadow_Entity_In_Body
(Typ
)
2474 Typ
:= Non_Limited_View
(Typ
);
2477 -- If still incomplete, it can be a local incomplete type, or a
2478 -- limited view whose scope is also a limited view.
2480 if Ekind
(Typ
) = E_Incomplete_Type
then
2481 if not From_Limited_With
(Typ
)
2482 and then No
(Full_View
(Typ
))
2485 ("prefix of % attribute cannot be an incomplete type");
2487 -- The limited view may be available indirectly through
2488 -- an intermediate unit. If the non-limited view is available
2489 -- the attribute reference is legal.
2491 elsif From_Limited_With
(Typ
)
2493 (No
(Non_Limited_View
(Typ
))
2494 or else Is_Incomplete_Type
(Non_Limited_View
(Typ
)))
2497 ("prefix of % attribute cannot be an incomplete type");
2501 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2504 elsif Is_Entity_Name
(P
)
2505 and then Is_Formal
(Entity
(P
))
2506 and then Is_Incomplete_Type
(Etype
(Etype
(P
)))
2509 ("prefix of % attribute cannot be an incomplete type");
2512 if not Is_Entity_Name
(P
)
2513 or else not Is_Type
(Entity
(P
))
2514 or else In_Spec_Expression
2517 elsif not Is_Current_Instance
(P
) then
2518 Check_Fully_Declared
(P_Type
, P
);
2520 end Check_Not_Incomplete_Type
;
2522 ----------------------------
2523 -- Check_Object_Reference --
2524 ----------------------------
2526 procedure Check_Object_Reference
(P
: Node_Id
) is
2530 -- If we need an object, and we have a prefix that is the name of a
2531 -- function entity, convert it into a function call.
2533 if Is_Entity_Name
(P
)
2534 and then Ekind
(Entity
(P
)) = E_Function
2536 Rtyp
:= Etype
(Entity
(P
));
2539 Make_Function_Call
(Sloc
(P
),
2540 Name
=> Relocate_Node
(P
)));
2542 Analyze_And_Resolve
(P
, Rtyp
);
2544 -- Otherwise we must have an object reference
2546 elsif not Is_Object_Reference
(P
) then
2547 Error_Attr_P
("prefix of % attribute must be object");
2549 end Check_Object_Reference
;
2551 ----------------------------
2552 -- Check_PolyORB_Attribute --
2553 ----------------------------
2555 procedure Check_PolyORB_Attribute
is
2557 Validate_Non_Static_Attribute_Function_Call
;
2562 if Get_PCS_Name
/= Name_PolyORB_DSA
then
2564 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N
);
2566 end Check_PolyORB_Attribute
;
2568 ------------------------
2569 -- Check_Program_Unit --
2570 ------------------------
2572 procedure Check_Program_Unit
is
2574 if Is_Entity_Name
(P
) then
2576 E
: constant Entity_Id
:= Entity
(P
);
2578 if Ekind
(E
) in E_Protected_Type
2584 or else Is_Single_Concurrent_Object
(E
)
2591 Error_Attr_P
("prefix of % attribute must be program unit");
2592 end Check_Program_Unit
;
2594 ---------------------
2595 -- Check_Real_Type --
2596 ---------------------
2598 procedure Check_Real_Type
is
2602 if not Is_Real_Type
(P_Type
) then
2603 Error_Attr_P
("prefix of % attribute must be real type");
2605 end Check_Real_Type
;
2607 ----------------------------
2608 -- Check_Enumeration_Type --
2609 ----------------------------
2611 procedure Check_Enumeration_Type
is
2615 if not Is_Enumeration_Type
(P_Type
) then
2616 Error_Attr_P
("prefix of % attribute must be enumeration type");
2618 end Check_Enumeration_Type
;
2620 -----------------------
2621 -- Check_Scalar_Type --
2622 -----------------------
2624 procedure Check_Scalar_Type
is
2628 if not Is_Scalar_Type
(P_Type
) then
2629 Error_Attr_P
("prefix of % attribute must be scalar type");
2631 end Check_Scalar_Type
;
2633 ---------------------------
2634 -- Check_Standard_Prefix --
2635 ---------------------------
2637 procedure Check_Standard_Prefix
is
2641 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_Standard
then
2642 Error_Attr
("only allowed prefix for % attribute is Standard", P
);
2644 end Check_Standard_Prefix
;
2646 -------------------------------
2647 -- Check_Put_Image_Attribute --
2648 -------------------------------
2650 procedure Check_Put_Image_Attribute
is
2652 -- Put_Image is a procedure, and can only appear at the position of a
2653 -- procedure call. If it's a list member and it's parent is a
2654 -- procedure call or aggregate, then this is appearing as an actual
2655 -- parameter or component association, which is wrong.
2657 if Is_List_Member
(N
)
2658 and then Nkind
(Parent
(N
)) not in
2659 N_Procedure_Call_Statement | N_Aggregate
2664 ("invalid context for attribute%, which is a procedure", N
);
2668 Analyze_And_Resolve
(E1
);
2670 -- Check that the first argument is
2671 -- Ada.Strings.Text_Buffers.Root_Buffer_Type'Class.
2673 -- Note: the double call to Root_Type here is needed because the
2674 -- root type of a class-wide type is the corresponding type (e.g.
2675 -- X for X'Class, and we really want to go to the root.)
2677 if not Is_RTE
(Root_Type
(Root_Type
(Etype
(E1
))),
2678 RE_Root_Buffer_Type
)
2681 ("expected Ada.Strings.Text_Buffers.Root_Buffer_Type''Class",
2685 -- Generate a conversion from a class-wide equivalent type (if
2686 -- present) to the relevant actual type E2.
2688 if Is_Mutably_Tagged_CW_Equivalent_Type
(Etype
(E2
)) then
2689 Make_Mutably_Tagged_Conversion
(E2
);
2692 -- Check that the second argument is of the right type
2695 Resolve
(E2
, P_Type
);
2696 end Check_Put_Image_Attribute
;
2698 ----------------------------
2699 -- Check_Stream_Attribute --
2700 ----------------------------
2702 procedure Check_Stream_Attribute
(Nam
: TSS_Name_Type
) is
2706 In_Shared_Var_Procs
: Boolean;
2707 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2708 -- For this runtime package (always compiled in GNAT mode), we allow
2709 -- stream attributes references for limited types for the case where
2710 -- shared passive objects are implemented using stream attributes,
2711 -- which is the default in GNAT's persistent storage implementation.
2714 Validate_Non_Static_Attribute_Function_Call
;
2716 -- With the exception of 'Input, Stream attributes are procedures,
2717 -- and can only appear at the position of procedure calls. We check
2718 -- for this here, before they are rewritten, to give a more precise
2721 if Nam
= TSS_Stream_Input
then
2724 elsif Is_List_Member
(N
)
2725 and then Nkind
(Parent
(N
)) not in
2726 N_Procedure_Call_Statement | N_Aggregate
2732 ("invalid context for attribute%, which is a procedure", N
);
2736 Btyp
:= Implementation_Base_Type
(P_Type
);
2738 -- Stream attributes not allowed on limited types unless the
2739 -- attribute reference was generated by the expander (in which
2740 -- case the underlying type will be used, as described in Sinfo),
2741 -- or the attribute was specified explicitly for the type itself
2742 -- or one of its ancestors (taking visibility rules into account if
2743 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2744 -- (with no visibility restriction).
2747 Gen_Body
: constant Node_Id
:= Enclosing_Generic_Body
(N
);
2749 if Present
(Gen_Body
) then
2750 In_Shared_Var_Procs
:=
2751 Is_RTE
(Corresponding_Spec
(Gen_Body
), RE_Shared_Var_Procs
);
2753 In_Shared_Var_Procs
:= False;
2757 if (Comes_From_Source
(N
)
2758 and then not (In_Shared_Var_Procs
or In_Instance
))
2759 and then not Stream_Attribute_Available
(P_Type
, Nam
)
2760 and then not Has_Rep_Pragma
(Btyp
, Name_Stream_Convert
)
2762 Error_Msg_Name_1
:= Aname
;
2764 if Is_Limited_Type
(P_Type
) then
2766 ("limited type& has no% attribute", P
, P_Type
);
2767 Explain_Limited_Type
(P_Type
, P
);
2770 ("attribute% for type& is not available", P
, P_Type
);
2774 -- Check for no stream operations allowed from No_Tagged_Streams
2776 if Is_Tagged_Type
(P_Type
)
2777 and then Present
(No_Tagged_Streams_Pragma
(P_Type
))
2779 Error_Msg_Sloc
:= Sloc
(No_Tagged_Streams_Pragma
(P_Type
));
2781 ("no stream operations for & (No_Tagged_Streams #)", N
, P_Type
);
2785 -- Check restriction violations
2787 -- First check the No_Streams restriction, which prohibits the use
2788 -- of explicit stream attributes in the source program. We do not
2789 -- prevent the occurrence of stream attributes in generated code,
2790 -- for instance those generated implicitly for dispatching purposes.
2792 if Comes_From_Source
(N
) then
2793 Check_Restriction
(No_Streams
, P
);
2796 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2797 -- it is illegal to use a predefined elementary type stream attribute
2798 -- either by itself, or more importantly as part of the attribute
2799 -- subprogram for a composite type. However, if the broader
2800 -- restriction No_Streams is active, stream operations are not
2801 -- generated, and there is no error.
2803 if Restriction_Active
(No_Default_Stream_Attributes
)
2804 and then not Restriction_Active
(No_Streams
)
2810 if Nam
= TSS_Stream_Input
2812 Nam
= TSS_Stream_Read
2815 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Read
);
2818 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Write
);
2822 Check_Restriction
(No_Default_Stream_Attributes
, N
);
2825 ("missing user-defined Stream Read or Write for type&",
2827 if not Is_Elementary_Type
(P_Type
) then
2829 ("\which is a component of type&", N
, P_Type
);
2835 -- Check special case of Exception_Id and Exception_Occurrence which
2836 -- are not allowed for restriction No_Exception_Registration.
2838 if Restriction_Check_Required
(No_Exception_Registration
)
2839 and then (Is_RTE
(P_Type
, RE_Exception_Id
)
2841 Is_RTE
(P_Type
, RE_Exception_Occurrence
))
2843 Check_Restriction
(No_Exception_Registration
, P
);
2846 -- If the No_Tagged_Type_Registration restriction is active, then
2847 -- class-wide streaming attributes are not allowed.
2849 if Restriction_Check_Required
(No_Tagged_Type_Registration
)
2850 and then Is_Class_Wide_Type
(P_Type
)
2852 Check_Restriction
(No_Tagged_Type_Registration
, P
);
2855 -- Here we must check that the first argument is an access type
2856 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2858 Analyze_And_Resolve
(E1
);
2861 -- Note: the double call to Root_Type here is needed because the
2862 -- root type of a class-wide type is the corresponding type (e.g.
2863 -- X for X'Class, and we really want to go to the root.)
2865 if not Is_Access_Type
(Etyp
)
2866 or else not Is_RTE
(Root_Type
(Root_Type
(Designated_Type
(Etyp
))),
2867 RE_Root_Stream_Type
)
2870 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1
);
2873 -- Check that the second argument is of the right type if there is
2874 -- one (the Input attribute has only one argument so this is skipped)
2876 if Present
(E2
) then
2879 if Nam
= TSS_Stream_Read
2880 and then not Is_OK_Variable_For_Out_Formal
(E2
)
2883 ("second argument of % attribute must be a variable", E2
);
2886 Resolve
(E2
, P_Type
);
2890 end Check_Stream_Attribute
;
2892 -------------------------
2893 -- Check_System_Prefix --
2894 -------------------------
2896 procedure Check_System_Prefix
is
2898 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_System
then
2899 Error_Attr
("only allowed prefix for % attribute is System", P
);
2901 end Check_System_Prefix
;
2903 -----------------------
2904 -- Check_Task_Prefix --
2905 -----------------------
2907 procedure Check_Task_Prefix
is
2909 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2910 -- task interface class-wide types.
2912 if Is_Task_Type
(Etype
(P
))
2913 or else (Is_Access_Type
(Etype
(P
))
2914 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
2915 or else (Ada_Version
>= Ada_2005
2916 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
2917 and then Is_Interface
(Etype
(P
))
2918 and then Is_Task_Interface
(Etype
(P
)))
2923 if Ada_Version
>= Ada_2005
then
2925 ("prefix of % attribute must be a task or a task " &
2926 "interface class-wide object");
2929 Error_Attr_P
("prefix of % attribute must be a task");
2932 end Check_Task_Prefix
;
2938 -- The possibilities are an entity name denoting a type, or an
2939 -- attribute reference that denotes a type (Base or Class). If
2940 -- the type is incomplete, replace it with its full view.
2942 procedure Check_Type
is
2944 if not Is_Entity_Name
(P
)
2945 or else not Is_Type
(Entity
(P
))
2947 Error_Attr_P
("prefix of % attribute must be a type");
2949 elsif Is_Protected_Self_Reference
(P
) then
2951 ("prefix of % attribute denotes current instance "
2952 & "(RM 9.4(21/2))");
2954 elsif Ekind
(Entity
(P
)) = E_Incomplete_Type
2955 and then Present
(Full_View
(Entity
(P
)))
2957 P_Type
:= Full_View
(Entity
(P
));
2958 Set_Entity
(P
, P_Type
);
2962 ---------------------
2963 -- Check_Unit_Name --
2964 ---------------------
2966 procedure Check_Unit_Name
(Nod
: Node_Id
) is
2968 if Nkind
(Nod
) = N_Identifier
then
2971 elsif Nkind
(Nod
) in N_Selected_Component | N_Expanded_Name
then
2972 Check_Unit_Name
(Prefix
(Nod
));
2974 if Nkind
(Selector_Name
(Nod
)) = N_Identifier
then
2979 Error_Attr
("argument for % attribute must be unit name", P
);
2980 end Check_Unit_Name
;
2986 procedure Error_Attr
is
2988 Set_Etype
(N
, Any_Type
);
2989 Set_Entity
(N
, Any_Type
);
2990 raise Bad_Attribute
;
2993 procedure Error_Attr
(Msg
: String; Error_Node
: Node_Id
) is
2995 Error_Msg_Name_1
:= Aname
;
2996 Error_Msg_N
(Msg
, Error_Node
);
3004 procedure Error_Attr_P
(Msg
: String; Msg_Cont
: String := "") is
3006 Error_Msg_Name_1
:= Aname
;
3007 Error_Msg_F
(Msg
, P
);
3008 if Msg_Cont
/= "" then
3009 Error_Msg_F
(Msg_Cont
, P
);
3014 ----------------------------
3015 -- Legal_Formal_Attribute --
3016 ----------------------------
3018 procedure Legal_Formal_Attribute
is
3022 if not Is_Entity_Name
(P
)
3023 or else not Is_Type
(Entity
(P
))
3025 Error_Attr_P
("prefix of % attribute must be generic type");
3027 elsif Is_Generic_Actual_Type
(Entity
(P
))
3029 or else In_Inlined_Body
3033 elsif Is_Generic_Type
(Entity
(P
)) then
3034 if Is_Definite_Subtype
(Entity
(P
)) then
3036 ("prefix of % attribute must be indefinite generic type");
3041 ("prefix of % attribute must be indefinite generic type");
3044 Set_Etype
(N
, Standard_Boolean
);
3045 end Legal_Formal_Attribute
;
3047 ---------------------------------------------------------------
3048 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
3049 ---------------------------------------------------------------
3051 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
is
3055 Check_Not_Incomplete_Type
;
3056 Set_Etype
(N
, Universal_Integer
);
3057 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
3063 procedure Min_Max
is
3065 -- Attribute can appear as function name in a reduction.
3066 -- Semantic checks are performed later.
3068 if Nkind
(Parent
(N
)) = N_Attribute_Reference
3069 and then Attribute_Name
(Parent
(N
)) = Name_Reduce
3071 Set_Etype
(N
, P_Base_Type
);
3077 Resolve
(E1
, P_Base_Type
);
3078 Resolve
(E2
, P_Base_Type
);
3079 Set_Etype
(N
, P_Base_Type
);
3081 -- Check for comparison on unordered enumeration type
3083 if Bad_Unordered_Enumeration_Reference
(N
, P_Base_Type
) then
3084 Error_Msg_Sloc
:= Sloc
(P_Base_Type
);
3086 ("comparison on unordered enumeration type& declared#?.u?",
3091 ------------------------
3092 -- Standard_Attribute --
3093 ------------------------
3095 procedure Standard_Attribute
(Val
: Int
) is
3097 Check_Standard_Prefix
;
3098 Rewrite
(N
, Make_Integer_Literal
(Loc
, Val
));
3100 Set_Is_Static_Expression
(N
, True);
3101 end Standard_Attribute
;
3103 --------------------
3104 -- Uneval_Old_Msg --
3105 --------------------
3107 procedure Uneval_Old_Msg
is
3108 Uneval_Old_Setting
: Character;
3112 -- If from aspect, then Uneval_Old_Setting comes from flags in the
3113 -- N_Aspect_Specification node that corresponds to the attribute.
3115 -- First find the pragma in which we appear (note that at this stage,
3116 -- even if we appeared originally within an aspect specification, we
3117 -- are now within the corresponding pragma).
3121 Prag
:= Parent
(Prag
);
3122 exit when No
(Prag
) or else Nkind
(Prag
) = N_Pragma
;
3125 if Present
(Prag
) then
3126 if Uneval_Old_Accept
(Prag
) then
3127 Uneval_Old_Setting
:= 'A';
3128 elsif Uneval_Old_Warn
(Prag
) then
3129 Uneval_Old_Setting
:= 'W';
3131 Uneval_Old_Setting
:= 'E';
3134 -- If we did not find the pragma, that's odd, just use the setting
3135 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
3138 Uneval_Old_Setting
:= Opt
.Uneval_Old
;
3141 -- Processing depends on the setting of Uneval_Old
3143 case Uneval_Old_Setting
is
3145 -- ??? In the case where Ada_Version is < Ada_2022 and
3146 -- an illegal 'Old prefix would be legal in Ada_2022,
3147 -- we'd like to call Error_Msg_Ada_2022_Feature.
3148 -- Identifying that case involves some work.
3151 ("prefix of attribute % that is potentially "
3152 & "unevaluated must statically name an entity"
3154 -- further text needed for accuracy if Ada_2022
3155 & (if Ada_Version
>= Ada_2022
3156 and then Attr_Id
= Attribute_Old
3157 then " or be eligible for conditional evaluation"
3158 & " (RM 6.1.1 (27))"
3161 "\using pragma Unevaluated_Use_Of_Old (Allow) will make "
3165 Error_Msg_Name_1
:= Aname
;
3167 ("??prefix of attribute % appears in potentially "
3168 & "unevaluated context, exception may be raised", P
);
3174 raise Program_Error
;
3178 -------------------------
3179 -- Unexpected Argument --
3180 -------------------------
3182 procedure Unexpected_Argument
(En
: Node_Id
) is
3184 Error_Attr
("unexpected argument for % attribute", En
);
3185 end Unexpected_Argument
;
3187 -------------------------------------------------
3188 -- Validate_Non_Static_Attribute_Function_Call --
3189 -------------------------------------------------
3191 -- This function should be moved to Sem_Dist ???
3193 procedure Validate_Non_Static_Attribute_Function_Call
is
3195 if In_Preelaborated_Unit
3196 and then not In_Subprogram_Or_Concurrent_Unit
3198 Flag_Non_Static_Expr
3199 ("non-static function call in preelaborated unit!", N
);
3201 end Validate_Non_Static_Attribute_Function_Call
;
3203 -- Start of processing for Analyze_Attribute
3206 -- Immediate return if unrecognized attribute (already diagnosed by
3207 -- parser, so there is nothing more that we need to do).
3209 if not Is_Attribute_Name
(Aname
) then
3210 raise Bad_Attribute
;
3213 Check_Restriction_No_Use_Of_Attribute
(N
);
3215 if Comes_From_Source
(N
) then
3217 -- Deal with Ada 83 issues
3219 if not Attribute_83
(Attr_Id
) then
3220 if Ada_Version
= Ada_83
then
3221 Error_Msg_Name_1
:= Aname
;
3222 Error_Msg_N
("(Ada 83) attribute% is not standard??", N
);
3225 if Attribute_Impl_Def
(Attr_Id
) then
3226 Check_Restriction
(No_Implementation_Attributes
, N
);
3230 -- Deal with Ada 2005 attributes that are implementation attributes
3231 -- because they appear in a version of Ada before Ada 2005, ditto for
3232 -- Ada 2012 and Ada 2022 attributes appearing in an earlier version.
3234 if (Attribute_05
(Attr_Id
) and then Ada_Version
< Ada_2005
)
3236 (Attribute_12
(Attr_Id
) and then Ada_Version
< Ada_2012
)
3238 (Attribute_22
(Attr_Id
) and then Ada_Version
< Ada_2022
)
3240 Check_Restriction
(No_Implementation_Attributes
, N
);
3244 -- Remote access to subprogram type access attribute reference needs
3245 -- unanalyzed copy for tree transformation. The analyzed copy is used
3246 -- for its semantic information (whether prefix is a remote subprogram
3247 -- name), the unanalyzed copy is used to construct new subtree rooted
3248 -- with N_Aggregate which represents a fat pointer aggregate.
3250 if Aname
= Name_Access
then
3251 Discard_Node
(Copy_Separate_Tree
(N
));
3254 -- Analyze prefix and exit if error in analysis. If the prefix is an
3255 -- incomplete type, use full view if available. Note that there are
3256 -- some attributes for which we do not analyze the prefix, since the
3257 -- prefix is not a normal name, or else needs special handling.
3259 if Aname
/= Name_Elab_Body
and then
3260 Aname
/= Name_Elab_Spec
and then
3261 Aname
/= Name_Elab_Subp_Body
and then
3262 Aname
/= Name_Enabled
and then
3266 P_Type
:= Etype
(P
);
3268 if Is_Entity_Name
(P
)
3269 and then Present
(Entity
(P
))
3270 and then Is_Type
(Entity
(P
))
3272 if Ekind
(Entity
(P
)) = E_Incomplete_Type
then
3273 P_Type
:= Get_Full_View
(P_Type
);
3274 Set_Entity
(P
, P_Type
);
3275 Set_Etype
(P
, P_Type
);
3277 elsif Entity
(P
) = Current_Scope
3278 and then Is_Record_Type
(Entity
(P
))
3280 -- Use of current instance within the type. Verify that if the
3281 -- attribute appears within a constraint, it yields an access
3282 -- type, other uses are illegal.
3290 and then Nkind
(Parent
(Par
)) /= N_Component_Definition
3292 Par
:= Parent
(Par
);
3296 and then Nkind
(Par
) = N_Subtype_Indication
3298 if Attr_Id
/= Attribute_Access
3299 and then Attr_Id
/= Attribute_Unchecked_Access
3300 and then Attr_Id
/= Attribute_Unrestricted_Access
3303 ("in a constraint the current instance can only "
3304 & "be used with an access attribute", N
);
3311 if P_Type
= Any_Type
then
3312 raise Bad_Attribute
;
3315 P_Base_Type
:= Base_Type
(P_Type
);
3318 -- Analyze expressions that may be present, exiting if an error occurs
3325 E1
:= First
(Exprs
);
3327 -- Skip analysis for case of Restriction_Set, we do not expect
3328 -- the argument to be analyzed in this case.
3330 if Aname
/= Name_Restriction_Set
then
3333 -- Check for missing/bad expression (result of previous error)
3336 or else (Etype
(E1
) = Any_Type
and then Full_Analysis
)
3338 Check_Error_Detected
;
3339 raise Bad_Attribute
;
3345 if Present
(E2
) then
3348 if Etype
(E2
) = Any_Type
then
3349 raise Bad_Attribute
;
3352 if Present
(Next
(E2
)) then
3353 Unexpected_Argument
(Next
(E2
));
3358 -- Cases where prefix must be resolvable by itself
3360 if Is_Overloaded
(P
)
3361 and then Aname
/= Name_Access
3362 and then Aname
/= Name_Address
3363 and then Aname
/= Name_Code_Address
3364 and then Aname
/= Name_Result
3365 and then Aname
/= Name_Unchecked_Access
3367 -- The prefix must be resolvable by itself, without reference to the
3368 -- attribute. One case that requires special handling is a prefix
3369 -- that is a function name, where one interpretation may be a
3370 -- parameterless call. Entry attributes are handled specially below.
3372 if Is_Entity_Name
(P
)
3373 and then Aname
not in Name_Count | Name_Caller
3375 Check_Parameterless_Call
(P
);
3378 if Is_Overloaded
(P
) then
3380 -- Ada 2005 (AI-345): Since protected and task types have
3381 -- primitive entry wrappers, the attributes Count, and Caller
3382 -- require a context check
3384 if Aname
in Name_Count | Name_Caller
then
3386 Count
: Natural := 0;
3391 Get_First_Interp
(P
, I
, It
);
3392 while Present
(It
.Nam
) loop
3393 if Comes_From_Source
(It
.Nam
) then
3399 Get_Next_Interp
(I
, It
);
3403 Error_Attr
("ambiguous prefix for % attribute", P
);
3405 Set_Is_Overloaded
(P
, False);
3410 Error_Attr
("ambiguous prefix for % attribute", P
);
3415 -- If the prefix was rewritten as a raise node, then rewrite N as a
3416 -- raise node, to avoid creating inconsistent trees. We still need to
3417 -- perform legality checks on the original tree.
3419 if Nkind
(P
) in N_Raise_xxx_Error
then
3421 P
:= Original_Node
(P_Old
);
3424 -- Remaining processing depends on attribute
3428 -- Attributes related to Ada 2012 iterators. Attribute specifications
3429 -- exist for these, but they cannot be queried.
3431 when Attribute_Constant_Indexing
3432 | Attribute_Default_Iterator
3433 | Attribute_Implicit_Dereference
3434 | Attribute_Iterator_Element
3435 | Attribute_Iterable
3436 | Attribute_Variable_Indexing
3438 Error_Msg_N
("illegal attribute", N
);
3440 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3441 -- were already rejected by the parser. Thus they shouldn't appear here.
3443 when Internal_Attribute_Id
=>
3444 raise Program_Error
;
3450 when Attribute_Abort_Signal
=>
3451 Check_Standard_Prefix
;
3452 Rewrite
(N
, New_Occurrence_Of
(Stand
.Abort_Signal
, Loc
));
3459 when Attribute_Access
=>
3460 Analyze_Access_Attribute
;
3461 Check_Not_Incomplete_Type
;
3467 when Attribute_Address
=>
3470 Check_Not_Incomplete_Type
;
3472 -- If the prefix is a dereference of a value whose associated access
3473 -- type has been specified with aspect Designated_Storage_Model, then
3474 -- use the associated Storage_Model_Type's address type as the type
3475 -- of the attribute. Otherwise we use System.Address as usual. This
3476 -- isn't normally legit for a predefined attribute, but this is for
3477 -- our own extension to addressing and currently requires extensions
3478 -- to be enabled (such as with -gnatX0).
3481 Prefix_Obj
: constant Node_Id
:= Get_Referenced_Object
(P
);
3482 Addr_Type
: Entity_Id
:= RTE
(RE_Address
);
3484 if Nkind
(Prefix_Obj
) = N_Explicit_Dereference
then
3486 P_Type
: constant Entity_Id
:= Etype
(Prefix
(Prefix_Obj
));
3488 use Storage_Model_Support
;
3490 if Has_Designated_Storage_Model_Aspect
(P_Type
) then
3491 Addr_Type
:= Storage_Model_Address_Type
3492 (Storage_Model_Object
(P_Type
));
3497 Set_Etype
(N
, Addr_Type
);
3504 when Attribute_Address_Size
=>
3505 Standard_Attribute
(System_Address_Size
);
3511 when Attribute_Adjacent
3512 | Attribute_Copy_Sign
3513 | Attribute_Remainder
3515 Check_Floating_Point_Type_2
;
3516 Set_Etype
(N
, P_Base_Type
);
3517 Resolve
(E1
, P_Base_Type
);
3518 Resolve
(E2
, P_Base_Type
);
3524 when Attribute_Aft
=>
3525 Check_Fixed_Point_Type_0
;
3526 Set_Etype
(N
, Universal_Integer
);
3532 when Attribute_Alignment
=>
3534 -- Don't we need more checking here, cf Size ???
3537 Check_Not_Incomplete_Type
;
3539 Set_Etype
(N
, Universal_Integer
);
3545 when Attribute_Asm_Input
=>
3546 Check_Asm_Attribute
;
3548 -- The back end may need to take the address of E2
3550 if Is_Entity_Name
(E2
) then
3551 Set_Address_Taken
(Entity
(E2
));
3554 Set_Etype
(N
, RTE
(RE_Asm_Input_Operand
));
3560 when Attribute_Asm_Output
=>
3561 Check_Asm_Attribute
;
3563 if Etype
(E2
) = Any_Type
then
3566 elsif Aname
= Name_Asm_Output
then
3567 if not Is_Variable
(E2
) then
3569 ("second argument for Asm_Output is not variable", E2
);
3573 Note_Possible_Modification
(E2
, Sure
=> True);
3575 -- The back end may need to take the address of E2
3577 if Is_Entity_Name
(E2
) then
3578 Set_Address_Taken
(Entity
(E2
));
3581 Set_Etype
(N
, RTE
(RE_Asm_Output_Operand
));
3583 -----------------------------
3584 -- Atomic_Always_Lock_Free --
3585 -----------------------------
3587 when Attribute_Atomic_Always_Lock_Free
=>
3590 Set_Etype
(N
, Standard_Boolean
);
3596 -- Note: when the base attribute appears in the context of a subtype
3597 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3598 -- the following circuit.
3600 when Attribute_Base
=> Base
: declare
3608 if Ada_Version
>= Ada_95
3609 and then not Is_Scalar_Type
(Typ
)
3610 and then not Is_Generic_Type
(Typ
)
3612 Error_Attr_P
("prefix of Base attribute must be scalar type");
3614 elsif Sloc
(Typ
) = Standard_Location
3615 and then Base_Type
(Typ
) = Typ
3616 and then Warn_On_Redundant_Constructs
3618 Error_Msg_NE
-- CODEFIX
3619 ("?r?redundant attribute, & is its own base type", N
, Typ
);
3622 Set_Etype
(N
, Base_Type
(Entity
(P
)));
3623 Set_Entity
(N
, Base_Type
(Entity
(P
)));
3624 Rewrite
(N
, New_Occurrence_Of
(Entity
(N
), Loc
));
3632 when Attribute_Bit
=>
3635 if not Is_Object_Reference
(P
) then
3636 Error_Attr_P
("prefix of % attribute must be object");
3638 -- What about the access object cases ???
3644 Set_Etype
(N
, Universal_Integer
);
3650 when Attribute_Bit_Order
=>
3654 if not Is_Record_Type
(P_Type
) then
3655 Error_Attr_P
("prefix of % attribute must be record type");
3658 if Bytes_Big_Endian
xor Reverse_Bit_Order
(P_Type
) then
3660 New_Occurrence_Of
(RTE
(RE_High_Order_First
), Loc
));
3663 New_Occurrence_Of
(RTE
(RE_Low_Order_First
), Loc
));
3668 -- Reset incorrect indication of staticness
3670 Set_Is_Static_Expression
(N
, False);
3676 -- Note: in generated code, we can have a Bit_Position attribute
3677 -- applied to a (naked) record component (i.e. the prefix is an
3678 -- identifier that references an E_Component or E_Discriminant
3679 -- entity directly, and this is interpreted as expected by Gigi.
3680 -- The following code will not tolerate such usage, but when the
3681 -- expander creates this special case, it marks it as analyzed
3682 -- immediately and sets an appropriate type.
3684 when Attribute_Bit_Position
=>
3685 if Comes_From_Source
(N
) then
3689 Set_Etype
(N
, Universal_Integer
);
3695 when Attribute_Body_Version
=>
3698 Set_Etype
(N
, RTE
(RE_Version_String
));
3704 when Attribute_Callable
3705 | Attribute_Terminated
3708 Set_Etype
(N
, Standard_Boolean
);
3715 when Attribute_Caller
=> Caller
: declare
3722 if Nkind
(P
) in N_Identifier | N_Expanded_Name
then
3725 if not Is_Entry
(Ent
) then
3726 Error_Attr
("invalid entry name", N
);
3730 Error_Attr
("invalid entry name", N
);
3733 for J
in reverse 0 .. Scope_Stack
.Last
loop
3734 S
:= Scope_Stack
.Table
(J
).Entity
;
3736 if S
= Scope
(Ent
) then
3737 Error_Attr
("Caller must appear in matching accept or body", N
);
3743 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
3750 when Attribute_Ceiling
3752 | Attribute_Fraction
3754 | Attribute_Machine_Rounding
3756 | Attribute_Rounding
3757 | Attribute_Truncation
3758 | Attribute_Unbiased_Rounding
3760 Check_Floating_Point_Type_1
;
3761 Set_Etype
(N
, P_Base_Type
);
3762 Resolve
(E1
, P_Base_Type
);
3768 when Attribute_Class
=>
3769 Check_Restriction
(No_Dispatch
, N
);
3773 -- Applying Class to untagged incomplete type is obsolescent in Ada
3774 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3775 -- this flag gets set by Find_Type in this situation.
3777 if Restriction_Check_Required
(No_Obsolescent_Features
)
3778 and then Ada_Version
>= Ada_2005
3779 and then Ekind
(P_Type
) = E_Incomplete_Type
3782 DN
: constant Node_Id
:= Declaration_Node
(P_Type
);
3784 if Nkind
(DN
) = N_Incomplete_Type_Declaration
3785 and then not Tagged_Present
(DN
)
3787 Check_Restriction
(No_Obsolescent_Features
, P
);
3796 when Attribute_Code_Address
=>
3799 if Nkind
(P
) = N_Attribute_Reference
3800 and then Attribute_Name
(P
) in Name_Elab_Body | Name_Elab_Spec
3804 elsif not Is_Entity_Name
(P
)
3805 or else (Ekind
(Entity
(P
)) /= E_Function
3807 Ekind
(Entity
(P
)) /= E_Procedure
)
3809 Error_Attr
("invalid prefix for % attribute", P
);
3811 -- Issue an error if the prefix denotes an eliminated subprogram
3814 Set_Address_Taken
(Entity
(P
));
3815 Check_For_Eliminated_Subprogram
(P
, Entity
(P
));
3818 Set_Etype
(N
, RTE
(RE_Address
));
3820 ----------------------
3821 -- Compiler_Version --
3822 ----------------------
3824 when Attribute_Compiler_Version
=>
3826 Check_Standard_Prefix
;
3827 Rewrite
(N
, Make_String_Literal
(Loc
, "GNAT " & Gnat_Version_String
));
3828 Analyze_And_Resolve
(N
, Standard_String
);
3829 Set_Is_Static_Expression
(N
, True);
3831 --------------------
3832 -- Component_Size --
3833 --------------------
3835 when Attribute_Component_Size
=>
3837 Set_Etype
(N
, Universal_Integer
);
3839 -- Note: unlike other array attributes, unconstrained arrays are OK
3841 if Is_Array_Type
(P_Type
) and then not Is_Constrained
(P_Type
) then
3851 when Attribute_Compose
3852 | Attribute_Leading_Part
3855 Check_Floating_Point_Type_2
;
3856 Set_Etype
(N
, P_Base_Type
);
3857 Resolve
(E1
, P_Base_Type
);
3858 Resolve
(E2
, Any_Integer
);
3864 when Attribute_Constrained
=>
3866 Set_Etype
(N
, Standard_Boolean
);
3868 -- Case from RM J.4(2) of constrained applied to private type
3870 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
3871 Check_Restriction
(No_Obsolescent_Features
, P
);
3873 if Warn_On_Obsolescent_Feature
then
3875 ("constrained for private type is an obsolescent feature "
3876 & "(RM J.4)?j?", N
);
3879 -- If we are within an instance, the attribute must be legal
3880 -- because it was valid in the generic unit. Ditto if this is
3881 -- an inlining of a function declared in an instance.
3883 if In_Instance
or else In_Inlined_Body
then
3886 -- For sure OK if we have a real private type itself, but must
3887 -- be completed, cannot apply Constrained to incomplete type.
3889 elsif Is_Private_Type
(Entity
(P
)) then
3891 -- Note: this is one of the Annex J features that does not
3892 -- generate a warning from -gnatwj, since in fact it seems
3893 -- very useful, and is used in the GNAT runtime.
3895 Check_Not_Incomplete_Type
;
3899 -- Normal (non-obsolescent case) of application to object or value of
3900 -- a discriminated type.
3903 -- AI12-0068: In a type or subtype aspect, a prefix denoting the
3904 -- current instance of the (sub)type is defined to be a value,
3905 -- not an object, so the Constrained attribute is always True
3906 -- (see RM 8.6(18/5) and RM 3.7.2(3/5)). We issue a warning about
3907 -- this unintuitive result, to help avoid confusion.
3909 if Is_Current_Instance_Reference_In_Type_Aspect
(P
) then
3910 Error_Msg_Name_1
:= Aname
;
3912 ("current instance attribute % in subtype aspect always " &
3916 Check_Object_Reference
(P
);
3919 -- If N does not come from source, then we allow the
3920 -- the attribute prefix to be of a private type whose
3921 -- full type has discriminants. This occurs in cases
3922 -- involving expanded calls to stream attributes.
3924 if not Comes_From_Source
(N
) then
3925 P_Type
:= Underlying_Type
(P_Type
);
3928 -- Must have discriminants or be an access type designating a type
3929 -- with discriminants. If it is a class-wide type it has unknown
3932 if Has_Discriminants
(P_Type
)
3933 or else Has_Unknown_Discriminants
(P_Type
)
3935 (Is_Access_Type
(P_Type
)
3936 and then Has_Discriminants
(Designated_Type
(P_Type
)))
3940 -- The rule given in 3.7.2 is part of static semantics, but the
3941 -- intent is clearly that it be treated as a legality rule, and
3942 -- rechecked in the visible part of an instance. Nevertheless
3943 -- the intent also seems to be it should legally apply to the
3944 -- actual of a formal with unknown discriminants, regardless of
3945 -- whether the actual has discriminants, in which case the value
3946 -- of the attribute is determined using the J.4 rules. This choice
3947 -- seems the most useful, and is compatible with existing tests.
3949 elsif In_Instance
then
3952 -- Also allow an object of a generic type if extensions allowed
3953 -- and allow this for any type at all.
3955 elsif (Is_Generic_Type
(P_Type
)
3956 or else Is_Generic_Actual_Type
(P_Type
))
3957 and then All_Extensions_Allowed
3963 -- Fall through if bad prefix
3966 ("prefix of % attribute must be object of discriminated type");
3972 -- Shares processing with Adjacent attribute
3978 when Attribute_Count
=> Count
: declare
3986 if Nkind
(P
) in N_Identifier | N_Expanded_Name
then
3989 if Ekind
(Ent
) /= E_Entry
then
3990 Error_Attr
("invalid entry name", N
);
3993 elsif Nkind
(P
) = N_Indexed_Component
then
3994 if not Is_Entity_Name
(Prefix
(P
))
3995 or else No
(Entity
(Prefix
(P
)))
3996 or else Ekind
(Entity
(Prefix
(P
))) /= E_Entry_Family
3998 if Nkind
(Prefix
(P
)) = N_Selected_Component
3999 and then Present
(Entity
(Selector_Name
(Prefix
(P
))))
4000 and then Ekind
(Entity
(Selector_Name
(Prefix
(P
)))) =
4004 ("attribute % must apply to entry of current task", P
);
4007 Error_Attr
("invalid entry family name", P
);
4011 Ent
:= Entity
(Prefix
(P
));
4014 elsif Nkind
(P
) = N_Selected_Component
4015 and then Present
(Entity
(Selector_Name
(P
)))
4016 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Entry
4019 ("attribute % must apply to entry of current task", P
);
4022 Error_Attr
("invalid entry name", N
);
4025 for J
in reverse 0 .. Scope_Stack
.Last
loop
4026 S
:= Scope_Stack
.Table
(J
).Entity
;
4028 if S
= Scope
(Ent
) then
4029 if Nkind
(P
) = N_Expanded_Name
then
4030 Tsk
:= Entity
(Prefix
(P
));
4032 -- The prefix denotes either the task type, or else a
4033 -- single task whose task type is being analyzed.
4035 if (Is_Type
(Tsk
) and then Tsk
= S
)
4036 or else (not Is_Type
(Tsk
)
4037 and then Etype
(Tsk
) = S
4038 and then not (Comes_From_Source
(S
)))
4043 ("attribute % must apply to entry of current task", N
);
4049 elsif Ekind
(Scope
(Ent
)) in Task_Kind
4050 and then Ekind
(S
) not in E_Block
4055 Error_Attr
("attribute % cannot appear in inner unit", N
);
4057 elsif Ekind
(Scope
(Ent
)) = E_Protected_Type
4058 and then not Has_Completion
(Scope
(Ent
))
4060 Error_Attr
("attribute % can only be used inside body", N
);
4064 if Is_Overloaded
(P
) then
4066 Index
: Interp_Index
;
4070 Get_First_Interp
(P
, Index
, It
);
4071 while Present
(It
.Nam
) loop
4072 if It
.Nam
= Ent
then
4075 -- Ada 2005 (AI-345): Do not consider primitive entry
4076 -- wrappers generated for task or protected types.
4078 elsif Ada_Version
>= Ada_2005
4079 and then not Comes_From_Source
(It
.Nam
)
4084 Error_Attr
("ambiguous entry name", N
);
4087 Get_Next_Interp
(Index
, It
);
4092 Set_Etype
(N
, Universal_Integer
);
4095 -----------------------
4096 -- Default_Bit_Order --
4097 -----------------------
4099 when Attribute_Default_Bit_Order
=> Default_Bit_Order
: declare
4100 Target_Default_Bit_Order
: System
.Bit_Order
;
4103 Check_Standard_Prefix
;
4105 if Bytes_Big_Endian
then
4106 Target_Default_Bit_Order
:= System
.High_Order_First
;
4108 Target_Default_Bit_Order
:= System
.Low_Order_First
;
4112 Make_Integer_Literal
(Loc
,
4113 UI_From_Int
(System
.Bit_Order
'Pos (Target_Default_Bit_Order
))));
4115 Set_Etype
(N
, Universal_Integer
);
4116 Set_Is_Static_Expression
(N
);
4117 end Default_Bit_Order
;
4119 ----------------------------------
4120 -- Default_Scalar_Storage_Order --
4121 ----------------------------------
4123 when Attribute_Default_Scalar_Storage_Order
=> Default_SSO
: declare
4124 RE_Default_SSO
: RE_Id
;
4127 Check_Standard_Prefix
;
4129 case Opt
.Default_SSO
is
4131 if Bytes_Big_Endian
then
4132 RE_Default_SSO
:= RE_High_Order_First
;
4134 RE_Default_SSO
:= RE_Low_Order_First
;
4138 RE_Default_SSO
:= RE_High_Order_First
;
4141 RE_Default_SSO
:= RE_Low_Order_First
;
4144 raise Program_Error
;
4147 Rewrite
(N
, New_Occurrence_Of
(RTE
(RE_Default_SSO
), Loc
));
4154 when Attribute_Definite
=>
4155 Legal_Formal_Attribute
;
4161 when Attribute_Delta
=>
4162 Check_Fixed_Point_Type_0
;
4163 Set_Etype
(N
, Universal_Real
);
4169 when Attribute_Denorm
4170 | Attribute_Signed_Zeros
4172 Check_Floating_Point_Type_0
;
4173 Set_Etype
(N
, Standard_Boolean
);
4179 when Attribute_Deref
=>
4182 Resolve
(E1
, RTE
(RE_Address
));
4183 Set_Etype
(N
, P_Type
);
4185 ---------------------
4186 -- Descriptor_Size --
4187 ---------------------
4189 when Attribute_Descriptor_Size
=>
4192 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
4193 Error_Attr_P
("prefix of attribute % must denote a type");
4196 Set_Etype
(N
, Universal_Integer
);
4202 when Attribute_Digits
=>
4206 if not Is_Floating_Point_Type
(P_Type
)
4207 and then not Is_Decimal_Fixed_Point_Type
(P_Type
)
4210 ("prefix of % attribute must be float or decimal type");
4213 Set_Etype
(N
, Universal_Integer
);
4219 -- Also handles processing for Elab_Spec and Elab_Subp_Body
4221 when Attribute_Elab_Body
4222 | Attribute_Elab_Spec
4223 | Attribute_Elab_Subp_Body
4226 Check_Unit_Name
(P
);
4227 Set_Etype
(N
, Standard_Void_Type
);
4229 -- We have to manually call the expander in this case to get
4230 -- the necessary expansion (normally attributes that return
4231 -- entities are not expanded).
4239 -- Shares processing with Elab_Body attribute
4245 when Attribute_Elaborated
=>
4247 Check_Unit_Name
(P
);
4248 Set_Etype
(N
, Standard_Boolean
);
4255 | Attribute_Machine_Emax
4256 | Attribute_Machine_Emin
4257 | Attribute_Machine_Mantissa
4258 | Attribute_Model_Emin
4259 | Attribute_Model_Mantissa
4260 | Attribute_Safe_Emax
4262 Check_Floating_Point_Type_0
;
4263 Set_Etype
(N
, Universal_Integer
);
4269 when Attribute_Enabled
=>
4270 Check_Either_E0_Or_E1
;
4272 if Present
(E1
) then
4273 if not Is_Entity_Name
(E1
) or else No
(Entity
(E1
)) then
4274 Error_Msg_N
("entity name expected for Enabled attribute", E1
);
4279 if Nkind
(P
) /= N_Identifier
then
4280 Error_Msg_N
("identifier expected (check name)", P
);
4281 elsif Get_Check_Id
(Chars
(P
)) = No_Check_Id
then
4282 Error_Msg_N
("& is not a recognized check name", P
);
4285 Set_Etype
(N
, Standard_Boolean
);
4291 when Attribute_Enum_Rep
=>
4293 -- T'Enum_Rep (X) case
4295 if Present
(E1
) then
4297 Check_Discrete_Type
;
4298 Resolve
(E1
, P_Base_Type
);
4300 -- X'Enum_Rep case. X must be an object or enumeration literal
4301 -- (including an attribute reference), and it must be of a
4305 ((Is_Object_Reference
(P
)
4308 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
4309 or else Nkind
(P
) = N_Attribute_Reference
)
4310 and then Is_Discrete_Type
(Etype
(P
)))
4312 Error_Attr_P
("prefix of % attribute must be discrete object");
4315 Set_Etype
(N
, Universal_Integer
);
4321 when Attribute_Enum_Val
=>
4325 if not Is_Enumeration_Type
(P_Type
) then
4326 Error_Attr_P
("prefix of % attribute must be enumeration type");
4329 -- If the enumeration type has a standard representation, the effect
4330 -- is the same as 'Val, so rewrite the attribute as a 'Val.
4332 if not Has_Non_Standard_Rep
(P_Base_Type
) then
4334 Make_Attribute_Reference
(Loc
,
4335 Prefix
=> Relocate_Node
(Prefix
(N
)),
4336 Attribute_Name
=> Name_Val
,
4337 Expressions
=> New_List
(Relocate_Node
(E1
))));
4338 Analyze_And_Resolve
(N
, P_Base_Type
);
4340 -- Non-standard representation case (enumeration with holes)
4344 Resolve
(E1
, Any_Integer
);
4345 Set_Etype
(N
, P_Base_Type
);
4352 when Attribute_Epsilon
4353 | Attribute_Model_Epsilon
4354 | Attribute_Model_Small
4355 | Attribute_Safe_First
4356 | Attribute_Safe_Last
4358 Check_Floating_Point_Type_0
;
4359 Set_Etype
(N
, Universal_Real
);
4365 when Attribute_Exponent
=>
4366 Check_Floating_Point_Type_1
;
4367 Set_Etype
(N
, Universal_Integer
);
4368 Resolve
(E1
, P_Base_Type
);
4374 when Attribute_External_Tag
=>
4378 Set_Etype
(N
, Standard_String
);
4380 if not Is_Tagged_Type
(P_Type
) then
4381 Error_Attr_P
("prefix of % attribute must be tagged");
4388 when Attribute_Fast_Math
=>
4389 Check_Standard_Prefix
;
4390 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(Fast_Math
), Loc
));
4392 -----------------------
4393 -- Finalization_Size --
4394 -----------------------
4396 when Attribute_Finalization_Size
=>
4399 -- The prefix denotes an object
4401 if Is_Object_Reference
(P
) then
4402 Check_Object_Reference
(P
);
4404 -- The prefix denotes a type
4406 elsif Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
4408 Check_Not_Incomplete_Type
;
4410 -- Attribute 'Finalization_Size is not defined for class-wide
4411 -- types because it is not possible to know statically whether
4412 -- a definite type will have controlled components or not.
4414 if Is_Class_Wide_Type
(Etype
(P
)) then
4416 ("prefix of % attribute cannot denote a class-wide type");
4419 -- The prefix denotes an illegal construct
4423 ("prefix of % attribute must be a definite type or an object");
4426 Set_Etype
(N
, Universal_Integer
);
4432 when Attribute_First
4435 Check_Array_Or_Scalar_Type
;
4436 Bad_Attribute_For_Predicate
;
4442 when Attribute_First_Bit
4443 | Attribute_Last_Bit
4444 | Attribute_Position
4447 Set_Etype
(N
, Universal_Integer
);
4453 when Attribute_First_Valid
4454 | Attribute_Last_Valid
4456 Check_First_Last_Valid
;
4457 Set_Etype
(N
, P_Type
);
4463 when Attribute_Fixed_Value
=>
4464 Check_Fixed_Point_Type
;
4466 Resolve
(E1
, Any_Integer
);
4467 Set_Etype
(N
, P_Base_Type
);
4473 -- Shares processing with Ceiling attribute
4479 when Attribute_Fore
=>
4480 Check_Fixed_Point_Type_0
;
4481 Set_Etype
(N
, Universal_Integer
);
4487 -- Shares processing with Ceiling attribute
4493 when Attribute_From_Any
=>
4495 Check_PolyORB_Attribute
;
4496 Set_Etype
(N
, P_Base_Type
);
4498 -----------------------
4499 -- Has_Access_Values --
4500 -----------------------
4502 when Attribute_Has_Access_Values
4503 | Attribute_Has_Tagged_Values
4507 Set_Etype
(N
, Standard_Boolean
);
4509 ----------------------
4510 -- Has_Same_Storage --
4511 ----------------------
4513 when Attribute_Has_Same_Storage
4514 | Attribute_Overlaps_Storage
4518 -- The arguments must be objects of any type
4520 Analyze_And_Resolve
(P
);
4521 Analyze_And_Resolve
(E1
);
4522 Check_Object_Reference
(P
);
4523 Check_Object_Reference
(E1
);
4524 Set_Etype
(N
, Standard_Boolean
);
4530 when Attribute_Index
=> Index
: declare
4533 Spec_Id
: Entity_Id
;
4537 Analyze_Index_Attribute
(Legal
, Spec_Id
);
4539 if not Legal
or else No
(Spec_Id
) then
4540 Error_Attr
("attribute % must apply to entry family", P
);
4545 if Nkind
(P
) in N_Identifier | N_Expanded_Name
then
4548 if Ekind
(Ent
) /= E_Entry_Family
then
4550 ("attribute % must apply to entry family", P
);
4552 -- Analysis of pre/postconditions of an entry [family] occurs when
4553 -- the conditions are relocated to the contract wrapper procedure
4554 -- (see subprogram Build_Contract_Wrapper).
4556 elsif Contract_Wrapper
(Ent
) /= Spec_Id
then
4558 ("attribute % must apply to current entry family", P
);
4561 elsif Nkind
(P
) in N_Indexed_Component
4562 | N_Selected_Component
4565 ("attribute % must apply to current entry family", P
);
4568 Error_Attr
("invalid entry family name", N
);
4571 Set_Etype
(N
, Entry_Index_Type
(Ent
));
4574 -----------------------
4575 -- Has_Tagged_Values --
4576 -----------------------
4578 -- Shares processing with Has_Access_Values attribute
4580 -----------------------
4581 -- Has_Discriminants --
4582 -----------------------
4584 when Attribute_Has_Discriminants
=>
4585 Legal_Formal_Attribute
;
4591 when Attribute_Identity
=>
4594 if Etype
(P
) = Standard_Exception_Type
then
4595 Set_Etype
(N
, RTE
(RE_Exception_Id
));
4597 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4598 -- interface class-wide types.
4600 elsif Is_Task_Type
(Etype
(P
))
4601 or else (Is_Access_Type
(Etype
(P
))
4602 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
4603 or else (Ada_Version
>= Ada_2005
4604 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
4605 and then Is_Interface
(Etype
(P
))
4606 and then Is_Task_Interface
(Etype
(P
)))
4609 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
4612 if Ada_Version
>= Ada_2005
then
4614 ("prefix of % attribute must be an exception, a task or a "
4615 & "task interface class-wide object");
4618 ("prefix of % attribute must be a task or an exception");
4626 when Attribute_Image
=>
4627 if Is_Real_Type
(P_Type
) then
4628 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
4629 Error_Msg_Name_1
:= Aname
;
4631 ("(Ada 83) % attribute not allowed for real types", N
);
4635 Analyze_Image_Attribute
(Standard_String
);
4641 when Attribute_Img
=>
4642 Analyze_Image_Attribute
(Standard_String
);
4648 when Attribute_Initialized
=>
4651 if Comes_From_Source
(N
) then
4653 -- This attribute can be prefixed with references to objects or
4654 -- values (such as a current instance value given within a type
4655 -- or subtype aspect).
4657 if not Is_Object_Reference
(P
)
4658 and then not Is_Current_Instance_Reference_In_Type_Aspect
(P
)
4660 Error_Attr_P
("prefix of % attribute must be object");
4662 -- Just like attribute 'Valid_Scalars this attribute is illegal
4663 -- on unchecked union types.
4665 elsif Has_Unchecked_Union
(Validated_View
(P_Type
)) then
4667 ("attribute % not allowed for Unchecked_Union type");
4671 Set_Etype
(N
, Standard_Boolean
);
4677 when Attribute_Input
=>
4679 Check_Stream_Attribute
(TSS_Stream_Input
);
4680 Set_Etype
(N
, P_Base_Type
);
4686 when Attribute_Integer_Value
=>
4689 Resolve
(E1
, Any_Fixed
);
4691 -- Signal an error if argument type is not a specific fixed-point
4692 -- subtype. An error has been signalled already if the argument
4693 -- was not of a fixed-point type.
4695 if Etype
(E1
) = Any_Fixed
and then not Error_Posted
(E1
) then
4696 Error_Attr
("argument of % must be of a fixed-point type", E1
);
4699 Set_Etype
(N
, P_Base_Type
);
4705 when Attribute_Invalid_Value
=>
4708 Set_Etype
(N
, P_Base_Type
);
4709 Invalid_Value_Used
:= True;
4715 when Attribute_Large
4717 | Attribute_Safe_Large
4718 | Attribute_Safe_Small
4722 Set_Etype
(N
, Universal_Real
);
4728 -- Shares processing with First attribute
4734 -- Shares processing with First_Bit attribute
4740 -- Shares processing with First_Valid attribute
4746 -- Shares processing with Compose attribute
4752 when Attribute_Length
=>
4754 Set_Etype
(N
, Universal_Integer
);
4760 when Attribute_Library_Level
=>
4763 if not Is_Entity_Name
(P
) then
4764 Error_Attr_P
("prefix of % attribute must be an entity name");
4767 if not Inside_A_Generic
then
4768 Set_Boolean_Result
(N
,
4769 Is_Library_Level_Entity
(Entity
(P
)));
4772 Set_Etype
(N
, Standard_Boolean
);
4778 when Attribute_Loop_Entry
=> Loop_Entry
: declare
4779 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
);
4780 -- Inspect the prefix for any uses of entities declared within the
4781 -- related loop. Loop_Id denotes the loop identifier.
4783 --------------------------------
4784 -- Check_References_In_Prefix --
4785 --------------------------------
4787 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
) is
4788 Loop_Decl
: constant Node_Id
:= Label_Construct
(Parent
(Loop_Id
));
4790 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
4791 -- Detect attribute 'Loop_Entry in prefix P and determine whether
4792 -- a reference mentions an entity declared within the related
4795 function Declared_Within
(Nod
: Node_Id
) return Boolean;
4796 -- Determine whether Nod appears in the subtree of Loop_Decl but
4797 -- not within the subtree of the prefix P itself.
4799 ---------------------
4800 -- Check_Reference --
4801 ---------------------
4803 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
4804 Orig_Nod
: constant Node_Id
:= Original_Node
(Nod
);
4805 -- Check presence of Loop_Entry in the prefix P by looking at
4806 -- the original node for Nod, as it will have been rewritten
4807 -- into its own prefix if the assertion is ignored (see code
4811 if Is_Attribute_Loop_Entry
(Orig_Nod
) then
4812 Error_Msg_Name_1
:= Name_Loop_Entry
;
4813 Error_Msg_Name_2
:= Name_Loop_Entry
;
4815 ("attribute % cannot appear in the prefix of attribute %",
4819 elsif Nkind
(Nod
) = N_Identifier
4820 and then Present
(Entity
(Nod
))
4821 and then Declared_Within
(Declaration_Node
(Entity
(Nod
)))
4824 ("prefix of attribute % cannot reference local entities",
4829 end Check_Reference
;
4831 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
4833 ---------------------
4834 -- Declared_Within --
4835 ---------------------
4837 function Declared_Within
(Nod
: Node_Id
) return Boolean is
4842 while Present
(Stmt
) loop
4843 if Stmt
= Loop_Decl
then
4849 -- Prevent the search from going too far
4851 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
4855 Stmt
:= Parent
(Stmt
);
4859 end Declared_Within
;
4861 -- Start of processing for Check_Prefix_For_Local_References
4864 Check_References
(P
);
4865 end Check_References_In_Prefix
;
4869 Context
: constant Node_Id
:= Parent
(N
);
4871 Encl_Loop
: Node_Id
:= Empty
;
4872 Encl_Prag
: Node_Id
:= Empty
;
4873 Loop_Id
: Entity_Id
:= Empty
;
4877 -- Start of processing for Loop_Entry
4882 -- Set the type of the attribute now to ensure the successful
4883 -- continuation of analysis even if the attribute is misplaced.
4885 Set_Etype
(Attr
, P_Type
);
4887 -- Attribute 'Loop_Entry may appear in several flavors:
4889 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4890 -- nearest enclosing loop.
4892 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4893 -- attribute may be related to a loop denoted by label Expr or
4894 -- the prefix may denote an array object and Expr may act as an
4895 -- indexed component.
4897 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4898 -- to the nearest enclosing loop, all expressions are part of
4899 -- an indexed component.
4901 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4902 -- denotes, the attribute may be related to a loop denoted by
4903 -- label Expr or the prefix may denote a multidimensional array
4904 -- array object and Expr along with the rest of the expressions
4905 -- may act as indexed components.
4907 -- Regardless of variations, the attribute reference does not have an
4908 -- expression list. Instead, all available expressions are stored as
4909 -- indexed components.
4911 -- When the attribute is part of an indexed component, find the first
4912 -- expression as it will determine the semantics of 'Loop_Entry.
4914 -- If the attribute is itself an index in an indexed component, i.e.
4915 -- a member of a list, the context itself is not relevant (the code
4916 -- below would lead to an infinite loop) and the attribute applies
4917 -- to the enclosing loop.
4919 if Nkind
(Context
) = N_Indexed_Component
4920 and then not Is_List_Member
(N
)
4922 E1
:= First
(Expressions
(Context
));
4925 -- The attribute reference appears in the following form:
4927 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4929 -- In this case, the loop name is omitted and no rewriting is
4932 if Present
(E2
) then
4935 -- The form of the attribute is:
4937 -- Prefix'Loop_Entry (Expr) [(...)]
4939 -- If Expr denotes a loop entry, the whole attribute and indexed
4940 -- component will have to be rewritten to reflect this relation.
4943 pragma Assert
(Present
(E1
));
4945 -- Do not expand the expression as it may have side effects.
4946 -- Simply preanalyze to determine whether it is a loop name or
4949 Preanalyze_And_Resolve
(E1
);
4951 if Is_Entity_Name
(E1
)
4952 and then Present
(Entity
(E1
))
4953 and then Ekind
(Entity
(E1
)) = E_Loop
4955 Loop_Id
:= Entity
(E1
);
4957 -- Transform the attribute and enclosing indexed component
4959 Set_Expressions
(N
, Expressions
(Context
));
4960 Rewrite
(Context
, N
);
4961 Set_Etype
(Context
, P_Type
);
4968 -- The prefix must denote an object
4970 if not Is_Object_Reference
(P
) then
4971 Error_Attr_P
("prefix of attribute % must denote an object");
4974 -- The prefix cannot be of a limited type because the expansion of
4975 -- Loop_Entry must create a constant initialized by the evaluated
4978 if Is_Inherently_Limited_Type
(Etype
(P
)) then
4979 Error_Attr_P
("prefix of attribute % cannot be limited");
4982 -- Climb the parent chain to verify the location of the attribute and
4983 -- find the enclosing loop.
4986 while Present
(Stmt
) loop
4988 -- Locate the corresponding enclosing pragma. Note that in the
4989 -- case of Assert[And_Cut] and Assume, we have already checked
4990 -- that the pragma appears in an appropriate loop location.
4992 if Nkind
(Original_Node
(Stmt
)) = N_Pragma
4994 Pragma_Name_Unmapped
(Original_Node
(Stmt
))
4995 in Name_Loop_Invariant
4998 | Name_Assert_And_Cut
5001 Encl_Prag
:= Original_Node
(Stmt
);
5003 -- Locate the enclosing loop (if any). Note that Ada 2012 array
5004 -- iteration may be expanded into several nested loops, we are
5005 -- interested in the outermost one which has the loop identifier,
5006 -- and comes from source.
5008 elsif Nkind
(Stmt
) = N_Loop_Statement
5009 and then Present
(Identifier
(Stmt
))
5010 and then Comes_From_Source
(Original_Node
(Stmt
))
5011 and then Nkind
(Original_Node
(Stmt
)) = N_Loop_Statement
5015 -- The original attribute reference may lack a loop name. Use
5016 -- the name of the enclosing loop because it is the related
5019 if No
(Loop_Id
) then
5020 Loop_Id
:= Entity
(Identifier
(Encl_Loop
));
5025 -- Prevent the search from going too far
5027 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
5031 Stmt
:= Parent
(Stmt
);
5034 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
5035 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
5036 -- purpose if they appear in an appropriate location in a loop,
5037 -- which was already checked by the top level pragma circuit).
5039 -- Loop_Entry also denotes a value and as such can appear within an
5040 -- expression that is an argument for another loop aspect. In that
5041 -- case it will have been expanded into the corresponding assignment.
5044 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
5045 and then not Comes_From_Source
(Parent
(N
))
5049 elsif No
(Encl_Prag
) then
5050 Error_Attr
("attribute% must appear within appropriate pragma", N
);
5053 -- A Loop_Entry that applies to a given loop statement must not
5054 -- appear within a body of accept statement, if this construct is
5055 -- itself enclosed by the given loop statement.
5057 for Index
in reverse 0 .. Scope_Stack
.Last
loop
5058 Scop
:= Scope_Stack
.Table
(Index
).Entity
;
5060 if Ekind
(Scop
) = E_Loop
and then Scop
= Loop_Id
then
5062 elsif Ekind
(Scop
) in E_Block | E_Loop | E_Return_Statement
then
5066 ("attribute % cannot appear in body or accept statement", N
);
5070 -- The prefix cannot mention entities declared within the related
5071 -- loop because they will not be visible once the prefix is moved
5072 -- outside the loop.
5074 Check_References_In_Prefix
(Loop_Id
);
5076 -- The prefix must statically name an object if the pragma does not
5077 -- apply to the innermost enclosing loop statement, or if it appears
5078 -- within a potentially unevaluated expression.
5080 if Is_Entity_Name
(P
)
5081 or else Nkind
(Parent
(P
)) = N_Object_Renaming_Declaration
5082 or else Statically_Names_Object
(P
)
5086 elsif Present
(Encl_Loop
)
5087 and then Entity
(Identifier
(Encl_Loop
)) /= Loop_Id
5090 ("prefix of attribute % that applies to outer loop must denote "
5093 elsif Is_Potentially_Unevaluated
(P
) then
5097 -- Replace the Loop_Entry attribute reference by its prefix if the
5098 -- related pragma is ignored. This transformation is OK with respect
5099 -- to typing because Loop_Entry's type is that of its prefix. This
5100 -- early transformation also avoids the generation of a useless loop
5103 if Present
(Encl_Prag
) and then Is_Ignored
(Encl_Prag
) then
5104 Rewrite
(N
, Relocate_Node
(P
));
5105 Preanalyze_And_Resolve
(N
);
5108 Preanalyze_And_Resolve
(P
);
5116 -- Shares processing with Ceiling attribute
5122 -- Shares processing with Emax attribute
5128 -- Shares processing with Emax attribute
5130 ----------------------
5131 -- Machine_Mantissa --
5132 ----------------------
5134 -- Shares processing with Emax attribute
5136 -----------------------
5137 -- Machine_Overflows --
5138 -----------------------
5140 when Attribute_Machine_Overflows
5141 | Attribute_Machine_Rounds
5145 Set_Etype
(N
, Standard_Boolean
);
5151 when Attribute_Machine_Radix
5152 | Attribute_Mantissa
5156 Set_Etype
(N
, Universal_Integer
);
5158 ----------------------
5159 -- Machine_Rounding --
5160 ----------------------
5162 -- Shares processing with Ceiling attribute
5164 --------------------
5165 -- Machine_Rounds --
5166 --------------------
5168 -- Shares processing with Machine_Overflows attribute
5174 when Attribute_Machine_Size
5175 | Attribute_Object_Size
5176 | Attribute_Value_Size
5180 Check_Not_Incomplete_Type
;
5181 Set_Etype
(N
, Universal_Integer
);
5187 -- Shares processing with Machine_Radix attribute
5193 when Attribute_Max
=>
5196 ----------------------------------
5197 -- Max_Alignment_For_Allocation --
5198 ----------------------------------
5200 when Attribute_Max_Size_In_Storage_Elements
=>
5201 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
5203 ----------------------
5204 -- Max_Integer_Size --
5205 ----------------------
5207 when Attribute_Max_Integer_Size
=>
5208 Standard_Attribute
(System_Max_Integer_Size
);
5210 ----------------------------------
5211 -- Max_Size_In_Storage_Elements --
5212 ----------------------------------
5214 when Attribute_Max_Alignment_For_Allocation
=>
5215 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
5217 -----------------------
5218 -- Maximum_Alignment --
5219 -----------------------
5221 when Attribute_Maximum_Alignment
=>
5222 Standard_Attribute
(Ttypes
.Maximum_Alignment
);
5224 --------------------
5225 -- Mechanism_Code --
5226 --------------------
5228 when Attribute_Mechanism_Code
=>
5229 if not Is_Entity_Name
(P
)
5230 or else not Is_Subprogram
(Entity
(P
))
5232 Error_Attr_P
("prefix of % attribute must be subprogram");
5235 Check_Either_E0_Or_E1
;
5237 if Present
(E1
) then
5238 Resolve
(E1
, Any_Integer
);
5239 Set_Etype
(E1
, Standard_Integer
);
5241 if not Is_OK_Static_Expression
(E1
) then
5242 Flag_Non_Static_Expr
5243 ("expression for parameter number must be static!", E1
);
5246 elsif UI_To_Int
(Intval
(E1
)) > Number_Formals
(Entity
(P
))
5247 or else Intval
(E1
) < 0
5249 Error_Attr
("invalid parameter number for % attribute", E1
);
5253 Set_Etype
(N
, Universal_Integer
);
5259 when Attribute_Min
=>
5266 when Attribute_Mod
=>
5268 -- Note: this attribute is only allowed in Ada 2005 mode, but
5269 -- we do not need to test that here, since Mod is only recognized
5270 -- as an attribute name in Ada 2005 mode during the parse.
5273 Check_Modular_Integer_Type
;
5274 Resolve
(E1
, Any_Integer
);
5275 Set_Etype
(N
, P_Base_Type
);
5281 -- Shares processing with Ceiling attribute
5287 -- Shares processing with Emax attribute
5293 -- Shares processing with Epsilon attribute
5295 --------------------
5296 -- Model_Mantissa --
5297 --------------------
5299 -- Shares processing with Emax attribute
5305 -- Shares processing with Epsilon attribute
5311 when Attribute_Modulus
=>
5313 Check_Modular_Integer_Type
;
5314 Set_Etype
(N
, Universal_Integer
);
5316 --------------------
5317 -- Null_Parameter --
5318 --------------------
5320 when Attribute_Null_Parameter
=> Null_Parameter
: declare
5321 Parnt
: constant Node_Id
:= Parent
(N
);
5322 GParnt
: constant Node_Id
:= Parent
(Parnt
);
5324 procedure Bad_Null_Parameter
(Msg
: String);
5325 -- Used if bad Null parameter attribute node is found. Issues
5326 -- given error message, and also sets the type to Any_Type to
5327 -- avoid blowups later on from dealing with a junk node.
5329 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
);
5330 -- Called to check that Proc_Ent is imported subprogram
5332 ------------------------
5333 -- Bad_Null_Parameter --
5334 ------------------------
5336 procedure Bad_Null_Parameter
(Msg
: String) is
5338 Error_Msg_N
(Msg
, N
);
5339 Set_Etype
(N
, Any_Type
);
5340 end Bad_Null_Parameter
;
5342 ----------------------
5343 -- Must_Be_Imported --
5344 ----------------------
5346 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
) is
5347 Pent
: constant Entity_Id
:= Ultimate_Alias
(Proc_Ent
);
5350 -- Ignore check if procedure not frozen yet (we will get
5351 -- another chance when the default parameter is reanalyzed)
5353 if not Is_Frozen
(Pent
) then
5356 elsif not Is_Imported
(Pent
) then
5358 ("Null_Parameter can only be used with imported subprogram");
5363 end Must_Be_Imported
;
5365 -- Start of processing for Null_Parameter
5370 Set_Etype
(N
, P_Type
);
5372 -- Case of attribute used as default expression
5374 if Nkind
(Parnt
) = N_Parameter_Specification
then
5375 Must_Be_Imported
(Defining_Entity
(GParnt
));
5377 -- Case of attribute used as actual for subprogram (positional)
5379 elsif Nkind
(Parnt
) in N_Subprogram_Call
5380 and then Is_Entity_Name
(Name
(Parnt
))
5382 Must_Be_Imported
(Entity
(Name
(Parnt
)));
5384 -- Case of attribute used as actual for subprogram (named)
5386 elsif Nkind
(Parnt
) = N_Parameter_Association
5387 and then Nkind
(GParnt
) in N_Subprogram_Call
5388 and then Is_Entity_Name
(Name
(GParnt
))
5390 Must_Be_Imported
(Entity
(Name
(GParnt
)));
5392 -- Not an allowed case
5396 ("Null_Parameter must be actual or default parameter");
5404 -- Shares processing with Machine_Size attribute
5410 when Attribute_Old
=> Old
: declare
5411 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
);
5412 -- Inspect the contents of the prefix and detect illegal uses of a
5413 -- nested 'Old, attribute 'Result or a use of an entity declared in
5414 -- the related postcondition expression. Subp_Id is the subprogram to
5415 -- which the related postcondition applies.
5417 --------------------------------
5418 -- Check_References_In_Prefix --
5419 --------------------------------
5421 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
) is
5422 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
5423 -- Detect attribute 'Old, attribute 'Result of a use of an entity
5424 -- and perform the appropriate semantic check.
5426 ---------------------
5427 -- Check_Reference --
5428 ---------------------
5430 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
5432 -- Attributes 'Old and 'Result cannot appear in the prefix of
5433 -- another attribute 'Old.
5435 if Nkind
(Nod
) = N_Attribute_Reference
5436 and then Attribute_Name
(Nod
) in Name_Old | Name_Result
5438 Error_Msg_Name_1
:= Attribute_Name
(Nod
);
5439 Error_Msg_Name_2
:= Name_Old
;
5441 ("attribute % cannot appear in the prefix of attribute %",
5445 -- Entities mentioned within the prefix of attribute 'Old must
5446 -- be global to the related postcondition. If this is not the
5447 -- case, then the scope of the local entity is nested within
5448 -- that of the subprogram. Moreover, we need to know whether
5449 -- Entity (Nod) occurs in the tree rooted at the prefix to
5450 -- ensure the entity is not declared within then prefix itself.
5452 elsif Is_Entity_Name
(Nod
)
5453 and then Present
(Entity
(Nod
))
5454 and then Scope_Within
(Scope
(Entity
(Nod
)), Subp_Id
)
5455 and then not In_Subtree
(Entity
(Nod
), P
)
5458 ("prefix of attribute % cannot reference local entities",
5461 -- Otherwise keep inspecting the prefix
5466 end Check_Reference
;
5468 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
5470 -- Start of processing for Check_References_In_Prefix
5473 Check_References
(P
);
5474 end Check_References_In_Prefix
;
5479 Pref_Id
: Entity_Id
;
5480 Pref_Typ
: Entity_Id
;
5481 Spec_Id
: Entity_Id
;
5483 -- Start of processing for Old
5486 -- The attribute reference is a primary. If any expressions follow,
5487 -- then the attribute reference is an indexable object. Transform the
5488 -- attribute into an indexed component and analyze it.
5490 if Present
(E1
) then
5492 Make_Indexed_Component
(Loc
,
5494 Make_Attribute_Reference
(Loc
,
5495 Prefix
=> Relocate_Node
(P
),
5496 Attribute_Name
=> Name_Old
),
5497 Expressions
=> Expressions
(N
)));
5502 Analyze_Attribute_Old_Result
(Legal
, Spec_Id
);
5504 -- The aspect or pragma where attribute 'Old resides should be
5505 -- associated with a subprogram declaration or a body. If this is not
5506 -- the case, then the aspect or pragma is illegal. Return as analysis
5507 -- cannot be carried out.
5513 -- The prefix must be preanalyzed as the full analysis will take
5514 -- place during expansion.
5516 -- If the attribute reference has an expected type or shall resolve
5517 -- to a given type, the same applies to the prefix; otherwise the
5518 -- prefix shall be resolved independently of context (RM 6.1.1(8/5)).
5520 if Nkind
(Parent
(N
)) = N_Qualified_Expression
then
5521 Preanalyze_And_Resolve
(P
, Etype
(Parent
(N
)));
5523 -- An special case occurs when the prefix is an overloaded function
5524 -- call without formals; in order to identify such case we preanalyze
5525 -- a duplicate of the prefix ignoring errors.
5529 P_Copy
: constant Node_Id
:= New_Copy_Tree
(P
);
5532 Set_Parent
(P_Copy
, Parent
(P
));
5534 Preanalyze_And_Resolve_Without_Errors
(P_Copy
);
5536 -- In the special case of a call to an overloaded function
5537 -- without extra formals we resolve it using its returned
5538 -- type (which is the unique valid call); if this not the
5539 -- case we will report the error later, as part of the
5540 -- regular analysis of the full expression.
5542 if Nkind
(P_Copy
) = N_Function_Call
5543 and then Is_Overloaded
(Name
(P_Copy
))
5544 and then No
(First_Formal
(Entity
(Name
(P_Copy
))))
5546 Preanalyze_And_Resolve
(P
, Etype
(Name
(P_Copy
)));
5548 Preanalyze_And_Resolve
(P
);
5553 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5555 Check_References_In_Prefix
(Spec_Id
);
5557 -- Set the type of the attribute now to prevent cascaded errors
5559 Pref_Typ
:= Etype
(P
);
5560 Set_Etype
(N
, Pref_Typ
);
5564 if Is_Limited_Type
(Pref_Typ
) then
5565 Error_Attr
("attribute % cannot apply to limited objects", P
);
5568 -- The prefix is a simple name
5570 if Is_Entity_Name
(P
) and then Present
(Entity
(P
)) then
5571 Pref_Id
:= Entity
(P
);
5573 -- Emit a warning when the prefix is a constant. Note that the use
5574 -- of Error_Attr would reset the type of N to Any_Type even though
5575 -- this is a warning. Use Error_Msg_XXX instead.
5577 if Is_Constant_Object
(Pref_Id
) then
5578 Error_Msg_Name_1
:= Name_Old
;
5580 ("??attribute % applied to constant has no effect", P
);
5583 -- Otherwise the prefix is not a simple name
5586 -- Ensure that the prefix of attribute 'Old is an entity when it
5587 -- is potentially unevaluated (6.1.1 (27/3)). This rule is
5588 -- relaxed in Ada 2022 - this relaxation is reflected in the
5589 -- call (below) to Eligible_For_Conditional_Evaluation.
5591 if Is_Potentially_Unevaluated
(N
)
5592 and then not Statically_Names_Object
(P
)
5594 Old_Attr_Util
.Conditional_Evaluation
5595 .Eligible_For_Conditional_Evaluation
(N
)
5599 -- Detect a possible infinite recursion when the prefix denotes
5600 -- the related function.
5602 -- function Func (...) return ...
5603 -- with Post => Func'Old ...;
5605 -- The function may be specified in qualified form X.Y where X is
5606 -- a protected object and Y is a protected function. In that case
5607 -- ensure that the qualified form has an entity.
5609 elsif Nkind
(P
) = N_Function_Call
5610 and then Nkind
(Name
(P
)) in N_Has_Entity
5612 Pref_Id
:= Entity
(Name
(P
));
5614 if Ekind
(Spec_Id
) in E_Function | E_Generic_Function
5615 and then Pref_Id
= Spec_Id
5617 Error_Msg_Warn
:= SPARK_Mode
/= On
;
5618 Error_Msg_N
("!possible infinite recursion<<", P
);
5619 Error_Msg_N
("\!??Storage_Error ]<<", P
);
5623 -- The prefix of attribute 'Old may refer to a component of a
5624 -- formal parameter. In this case its expansion may generate
5625 -- actual subtypes that are referenced in an inner context and
5626 -- that must be elaborated within the subprogram itself. If the
5627 -- prefix includes a function call, it may involve finalization
5628 -- actions that should be inserted when the attribute has been
5629 -- rewritten as a declaration. Create a declaration for the prefix
5630 -- and insert it at the start of the enclosing subprogram. This is
5631 -- an expansion activity that has to be performed now to prevent
5632 -- out-of-order issues.
5634 -- This expansion is both harmful and not needed in SPARK mode,
5635 -- since the formal verification back end relies on the types of
5636 -- nodes (hence is not robust w.r.t. a change to base type here),
5637 -- and does not suffer from the out-of-order issue described
5638 -- above. Thus, this expansion is skipped in SPARK mode.
5640 -- The expansion is not relevant for discrete types, which will
5641 -- not generate extra declarations, and where use of the base type
5642 -- may lead to spurious errors if context is a case.
5644 if not GNATprove_Mode
then
5645 if not Is_Discrete_Type
(Pref_Typ
) then
5646 Pref_Typ
:= Base_Type
(Pref_Typ
);
5649 Set_Etype
(N
, Pref_Typ
);
5650 Set_Etype
(P
, Pref_Typ
);
5652 Analyze_Dimension
(N
);
5662 when Attribute_Output
=>
5664 Check_Stream_Attribute
(TSS_Stream_Output
);
5665 Set_Etype
(N
, Standard_Void_Type
);
5666 Resolve
(N
, Standard_Void_Type
);
5672 when Attribute_Partition_ID
=>
5675 if P_Type
/= Any_Type
then
5676 if not Is_Library_Level_Entity
(Entity
(P
)) then
5678 ("prefix of % attribute must be library-level entity");
5680 -- The defining entity of prefix should not be declared inside a
5681 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5683 elsif Is_Entity_Name
(P
)
5684 and then Is_Pure
(Entity
(P
))
5686 Error_Attr_P
("prefix of% attribute must not be declared pure");
5690 Set_Etype
(N
, Universal_Integer
);
5692 -------------------------
5693 -- Passed_By_Reference --
5694 -------------------------
5696 when Attribute_Passed_By_Reference
=>
5699 Check_Not_Incomplete_Type
;
5700 Set_Etype
(N
, Standard_Boolean
);
5706 when Attribute_Pool_Address
=>
5708 Set_Etype
(N
, RTE
(RE_Address
));
5714 when Attribute_Pos
=>
5715 Check_Discrete_Type
;
5717 Resolve
(E1
, P_Base_Type
);
5718 Set_Etype
(N
, Universal_Integer
);
5724 -- Shares processing with First_Bit attribute
5735 Resolve
(E1
, P_Base_Type
);
5736 Set_Etype
(N
, P_Base_Type
);
5738 -- Since Pred/Succ work on the base type, we normally do no check for
5739 -- the floating-point case, since the base type is unconstrained. But
5740 -- we make an exception in Check_Float_Overflow mode.
5742 if Is_Floating_Point_Type
(P_Type
) then
5743 if not Range_Checks_Suppressed
(P_Base_Type
) then
5744 Set_Do_Range_Check
(E1
);
5747 -- If not modular type, test for overflow check required
5750 if not Is_Modular_Integer_Type
(P_Type
)
5751 and then not Range_Checks_Suppressed
(P_Base_Type
)
5753 Enable_Range_Check
(E1
);
5757 ----------------------------------
5758 -- Preelaborable_Initialization --
5759 ----------------------------------
5761 when Attribute_Preelaborable_Initialization
=>
5765 -- If we're in an instance, we know that the legality of the
5766 -- attribute prefix type was already checked in the generic.
5768 if not In_Instance
then
5770 -- If the prefix type is a generic formal type, then it must be
5771 -- either a formal private type or a formal derived type.
5773 if Is_Generic_Type
(P_Type
) then
5774 if not Is_Private_Type
(P_Type
)
5775 and then not Is_Derived_Type
(P_Type
)
5777 Error_Attr_P
("formal type prefix of % attribute must be "
5778 & "formal private or formal derived type");
5781 -- Otherwise, the prefix type must be a nonformal composite
5782 -- type declared within the visible part of a package or
5785 elsif not Is_Composite_Type
(P_Type
)
5786 or else not Original_View_In_Visible_Part
(P_Type
)
5789 ("prefix of % attribute must be composite type declared "
5790 & "in visible part of a package or generic package");
5794 Set_Etype
(N
, Standard_Boolean
);
5800 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5802 when Attribute_Priority
=>
5803 if Ada_Version
< Ada_2005
then
5804 Error_Attr
("% attribute is allowed only in Ada 2005 mode", P
);
5809 Check_Restriction
(No_Dynamic_Priorities
, N
);
5811 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5813 if Is_Protected_Type
(Etype
(P
))
5814 or else (Is_Access_Type
(Etype
(P
))
5815 and then Is_Protected_Type
(Designated_Type
(Etype
(P
))))
5819 Error_Attr_P
("prefix of % attribute must be a protected object");
5822 Set_Etype
(N
, Standard_Integer
);
5824 -- Must be called from within a protected procedure or entry of the
5825 -- protected object.
5832 while S
/= Etype
(P
)
5833 and then S
/= Standard_Standard
5838 if S
= Standard_Standard
then
5839 Error_Attr
("the attribute % is only allowed inside protected "
5844 Validate_Non_Static_Attribute_Function_Call
;
5850 when Attribute_Put_Image
=>
5852 Check_Put_Image_Attribute
;
5853 Set_Etype
(N
, Standard_Void_Type
);
5854 Resolve
(N
, Standard_Void_Type
);
5860 when Attribute_Range
=>
5861 Check_Array_Or_Scalar_Type
;
5862 Bad_Attribute_For_Predicate
;
5864 if Ada_Version
= Ada_83
5865 and then Is_Scalar_Type
(P_Type
)
5866 and then Comes_From_Source
(N
)
5869 ("(Ada 83) % attribute not allowed for scalar type", P
);
5876 when Attribute_Result
=> Result
: declare
5877 function Denote_Same_Function
5878 (Pref_Id
: Entity_Id
;
5879 Spec_Id
: Entity_Id
) return Boolean;
5880 -- Determine whether the entity of the prefix Pref_Id denotes the
5881 -- same entity as that of the related subprogram Spec_Id.
5883 --------------------------
5884 -- Denote_Same_Function --
5885 --------------------------
5887 function Denote_Same_Function
5888 (Pref_Id
: Entity_Id
;
5889 Spec_Id
: Entity_Id
) return Boolean
5891 Over_Id
: constant Entity_Id
:= Overridden_Operation
(Spec_Id
);
5892 Subp_Spec
: constant Node_Id
:= Parent
(Spec_Id
);
5895 -- The prefix denotes the related subprogram
5897 if Pref_Id
= Spec_Id
then
5900 -- Account for a special case when attribute 'Result appears in
5901 -- the postcondition of a generic function.
5904 -- function Gen_Func return ...
5905 -- with Post => Gen_Func'Result ...;
5907 -- When the generic function is instantiated, the Chars field of
5908 -- the instantiated prefix still denotes the name of the generic
5909 -- function. Note that any preemptive transformation is impossible
5910 -- without a proper analysis. The structure of the wrapper package
5913 -- package Anon_Gen_Pack is
5914 -- <subtypes and renamings>
5915 -- function Subp_Decl return ...; -- (!)
5916 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5917 -- function Gen_Func ... renames Subp_Decl;
5918 -- end Anon_Gen_Pack;
5920 elsif Nkind
(Subp_Spec
) = N_Function_Specification
5921 and then Present
(Generic_Parent
(Subp_Spec
))
5922 and then Ekind
(Pref_Id
) in E_Generic_Function | E_Function
5924 if Generic_Parent
(Subp_Spec
) = Pref_Id
then
5927 elsif Present
(Alias
(Pref_Id
))
5928 and then Alias
(Pref_Id
) = Spec_Id
5933 -- Account for a special case where a primitive of a tagged type
5934 -- inherits a class-wide postcondition from a parent type. In this
5935 -- case the prefix of attribute 'Result denotes the overriding
5938 elsif Present
(Over_Id
) and then Pref_Id
= Over_Id
then
5941 -- When a qualified name is used for the prefix, homonyms may come
5942 -- before the current function in the homonym chain.
5944 elsif Has_Homonym
(Pref_Id
)
5945 and then Present
(Homonym
(Pref_Id
))
5947 return Denote_Same_Function
(Homonym
(Pref_Id
), Spec_Id
);
5950 -- Otherwise the prefix does not denote the related subprogram
5953 end Denote_Same_Function
;
5958 Pref_Id
: Entity_Id
;
5959 Spec_Id
: Entity_Id
;
5961 -- Start of processing for Result
5964 -- The attribute reference is a primary. If any expressions follow,
5965 -- then the attribute reference is an indexable object. Transform the
5966 -- attribute into an indexed component and analyze it.
5968 if Present
(E1
) then
5970 Make_Indexed_Component
(Loc
,
5972 Make_Attribute_Reference
(Loc
,
5973 Prefix
=> Relocate_Node
(P
),
5974 Attribute_Name
=> Name_Result
),
5975 Expressions
=> Expressions
(N
)));
5980 Analyze_Attribute_Old_Result
(Legal
, Spec_Id
);
5982 -- The aspect or pragma where attribute 'Result resides should be
5983 -- associated with a subprogram declaration or a body. If this is not
5984 -- the case, then the aspect or pragma is illegal. Return as analysis
5985 -- cannot be carried out.
5988 Error_Attr
("prefix of % attribute must be a function", P
);
5991 -- Attribute 'Result is part of postconditions expansion. There is
5992 -- no need to perform the semantic checks below as they were already
5993 -- verified when the attribute was analyzed in its original context.
5994 -- Instead, rewrite the attribute as a reference to formal parameter
5995 -- _Result of the _Wrapped_Statements procedure.
5997 if Chars
(Spec_Id
) = Name_uWrapped_Statements
then
5998 Rewrite
(N
, Make_Identifier
(Loc
, Name_uResult
));
6000 -- The type of formal parameter _Result is that of the function
6001 -- encapsulating the _Postconditions procedure. Resolution must
6002 -- be carried out against the function return type.
6004 Analyze_And_Resolve
(N
, Etype
(Scope
(Spec_Id
)));
6006 -- Otherwise attribute 'Result appears in its original context and
6007 -- all semantic checks should be carried out.
6010 -- Verify the legality of the prefix. It must denotes the entity
6011 -- of the related [generic] function.
6013 if Is_Entity_Name
(P
) then
6014 Pref_Id
:= Entity
(P
);
6016 -- Either both the prefix and the annotated spec must be
6017 -- generic functions, or they both must be nongeneric
6018 -- functions, or the prefix must be generic and the spec
6019 -- must be nongeneric (i.e. it must denote an instance).
6021 if (Ekind
(Pref_Id
) in E_Function | E_Generic_Function
6022 and then Ekind
(Pref_Id
) = Ekind
(Spec_Id
))
6024 (Ekind
(Pref_Id
) = E_Generic_Function
6025 and then Ekind
(Spec_Id
) = E_Function
)
6027 if Denote_Same_Function
(Pref_Id
, Spec_Id
) then
6029 -- Correct the prefix of the attribute when the context
6030 -- is a generic function.
6032 if Pref_Id
/= Spec_Id
then
6033 Rewrite
(P
, New_Occurrence_Of
(Spec_Id
, Loc
));
6037 Set_Etype
(N
, Etype
(Spec_Id
));
6039 -- Otherwise the prefix denotes some unrelated function
6042 Error_Msg_Name_2
:= Chars
(Spec_Id
);
6044 ("incorrect prefix for attribute %, expected %", P
);
6047 -- If the prefix is an access-to-subprogram type, then it must
6048 -- be the same as the annotated type.
6050 elsif Is_Access_Subprogram_Type
(Pref_Id
) then
6051 if Pref_Id
= Spec_Id
then
6052 Set_Etype
(N
, Etype
(Designated_Type
(Spec_Id
)));
6054 Error_Msg_Name_2
:= Chars
(Spec_Id
);
6056 ("incorrect prefix for attribute %, expected %", P
);
6059 -- Otherwise the prefix denotes some other form of subprogram
6064 ("attribute % can only appear in postcondition of "
6068 -- Otherwise the prefix is illegal
6071 Error_Msg_Name_2
:= Chars
(Spec_Id
);
6072 Error_Attr
("incorrect prefix for attribute %, expected %", P
);
6081 when Attribute_Range_Length
=>
6083 Check_Discrete_Type
;
6084 Set_Etype
(N
, Universal_Integer
);
6090 when Attribute_Reduce
=>
6092 Error_Msg_Ada_2022_Feature
("Reduce attribute", Sloc
(N
));
6095 Stream
: constant Node_Id
:= Prefix
(N
);
6098 if Nkind
(Stream
) /= N_Aggregate
then
6099 -- Prefix is a name, as for other attributes.
6101 -- If the object is a function we asume that it is not
6102 -- overloaded. AI12-242 does not suggest a name resolution
6103 -- rule for that case, but we can suppose that the expected
6104 -- type of the reduction is the expected type of the component
6107 Analyze_And_Resolve
(Stream
);
6108 Typ
:= Etype
(Stream
);
6110 -- Verify that prefix can be iterated upon.
6112 if Is_Array_Type
(Typ
)
6113 or else Has_Aspect
(Typ
, Aspect_Default_Iterator
)
6114 or else Has_Aspect
(Typ
, Aspect_Iterable
)
6119 ("cannot apply Reduce to object of type&", N
, Typ
);
6122 elsif Present
(Expressions
(Stream
))
6123 or else No
(Component_Associations
(Stream
))
6124 or else Nkind
(First
(Component_Associations
(Stream
))) /=
6125 N_Iterated_Component_Association
6128 ("prefix of Reduce must be an iterated component", N
);
6133 Set_Etype
(N
, Etype
(E2
));
6140 when Attribute_Read
=>
6142 Check_Stream_Attribute
(TSS_Stream_Read
);
6143 Set_Etype
(N
, Standard_Void_Type
);
6144 Resolve
(N
, Standard_Void_Type
);
6145 Note_Possible_Modification
(E2
, Sure
=> True);
6151 when Attribute_Ref
=>
6154 if Nkind
(P
) /= N_Expanded_Name
6155 or else not Is_RTE
(P_Type
, RE_Address
)
6157 Error_Attr_P
("prefix of % attribute must be System.Address");
6160 Analyze_And_Resolve
(E1
, Any_Integer
);
6161 Set_Etype
(N
, RTE
(RE_Address
));
6167 -- Shares processing with Adjacent attribute
6169 ---------------------
6170 -- Restriction_Set --
6171 ---------------------
6173 when Attribute_Restriction_Set
=> Restriction_Set
: declare
6176 Unam
: Unit_Name_Type
;
6180 Check_System_Prefix
;
6182 -- No_Dependence case
6184 if Nkind
(E1
) = N_Parameter_Association
then
6185 pragma Assert
(Chars
(Selector_Name
(E1
)) = Name_No_Dependence
);
6186 U
:= Explicit_Actual_Parameter
(E1
);
6188 if not OK_No_Dependence_Unit_Name
(U
) then
6189 Set_Boolean_Result
(N
, False);
6193 -- See if there is an entry already in the table. That's the
6194 -- case in which we can return True.
6196 for J
in No_Dependences
.First
.. No_Dependences
.Last
loop
6197 if Designate_Same_Unit
(U
, No_Dependences
.Table
(J
).Unit
)
6198 and then No_Dependences
.Table
(J
).Warn
= False
6200 Set_Boolean_Result
(N
, True);
6205 -- If not in the No_Dependence table, result is False
6207 Set_Boolean_Result
(N
, False);
6209 -- In this case, we must ensure that the binder will reject any
6210 -- other unit in the partition that sets No_Dependence for this
6211 -- unit. We do that by making an entry in the special table kept
6212 -- for this purpose (if the entry is not there already).
6214 Unam
:= Get_Spec_Name
(Get_Unit_Name
(U
));
6216 for J
in Restriction_Set_Dependences
.First
..
6217 Restriction_Set_Dependences
.Last
6219 if Restriction_Set_Dependences
.Table
(J
) = Unam
then
6224 Restriction_Set_Dependences
.Append
(Unam
);
6226 -- Normal restriction case
6229 if Nkind
(E1
) /= N_Identifier
then
6230 Set_Boolean_Result
(N
, False);
6231 Error_Attr
("attribute % requires restriction identifier", E1
);
6234 R
:= Get_Restriction_Id
(Process_Restriction_Synonyms
(E1
));
6236 if R
= Not_A_Restriction_Id
then
6237 Set_Boolean_Result
(N
, False);
6238 Error_Msg_Node_1
:= E1
;
6239 Error_Attr
("invalid restriction identifier &", E1
);
6241 elsif R
not in Partition_Boolean_Restrictions
then
6242 Set_Boolean_Result
(N
, False);
6243 Error_Msg_Node_1
:= E1
;
6245 ("& is not a boolean partition-wide restriction", E1
);
6248 if Restriction_Active
(R
) then
6249 Set_Boolean_Result
(N
, True);
6251 Check_Restriction
(R
, N
);
6252 Set_Boolean_Result
(N
, False);
6256 end Restriction_Set
;
6262 when Attribute_Round
=>
6266 if not Is_Fixed_Point_Type
(P_Type
) then
6267 Error_Attr_P
("prefix of % attribute must be fixed point type");
6270 Set_Etype
(N
, P_Base_Type
);
6272 -- Because the context is universal_real (3.5.10(12)) it is a
6273 -- legal context for a universal fixed expression. This is the
6274 -- only attribute whose functional description involves U_R.
6276 if Etype
(E1
) = Universal_Fixed
then
6278 Conv
: constant Node_Id
:= Make_Type_Conversion
(Loc
,
6279 Subtype_Mark
=> New_Occurrence_Of
(Universal_Real
, Loc
),
6280 Expression
=> Relocate_Node
(E1
));
6288 Resolve
(E1
, Any_Real
);
6294 -- Shares processing with Ceiling attribute
6300 -- Shares processing with Emax attribute
6306 -- Shares processing with Epsilon attribute
6312 -- Shares processing with Large attribute
6318 -- Shares processing with Epsilon attribute
6324 -- Shares processing with Large attribute
6326 --------------------------
6327 -- Scalar_Storage_Order --
6328 --------------------------
6330 when Attribute_Scalar_Storage_Order
=> Scalar_Storage_Order
: declare
6331 Ent
: Entity_Id
:= Empty
;
6337 if not (Is_Record_Type
(P_Type
) or else Is_Array_Type
(P_Type
)) then
6339 -- The attribute applies to generic private types (in which case
6340 -- the legality rule is applied in the instance) as well as to
6341 -- composite types. For noncomposite types it always returns the
6342 -- default bit order for the target.
6343 -- Allowing formal private types was originally introduced in
6344 -- GNAT_Mode only, to compile instances of Sequential_IO, but
6345 -- users find it more generally useful in generic units.
6347 if not (Is_Generic_Type
(P_Type
) and then Is_Private_Type
(P_Type
))
6348 and then not In_Instance
6351 ("prefix of % attribute must be record or array type");
6353 elsif not Is_Generic_Type
(P_Type
) then
6354 if Bytes_Big_Endian
then
6355 Ent
:= RTE
(RE_High_Order_First
);
6357 Ent
:= RTE
(RE_Low_Order_First
);
6361 elsif Bytes_Big_Endian
xor Reverse_Storage_Order
(P_Type
) then
6362 Ent
:= RTE
(RE_High_Order_First
);
6365 Ent
:= RTE
(RE_Low_Order_First
);
6368 if Present
(Ent
) then
6369 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
6372 Set_Etype
(N
, RTE
(RE_Bit_Order
));
6375 -- Reset incorrect indication of staticness
6377 Set_Is_Static_Expression
(N
, False);
6378 end Scalar_Storage_Order
;
6384 when Attribute_Scale
=>
6386 Check_Decimal_Fixed_Point_Type
;
6387 Set_Etype
(N
, Universal_Integer
);
6393 -- Shares processing with Compose attribute
6399 -- Shares processing with Denorm attribute
6406 | Attribute_VADS_Size
6410 -- If prefix is parameterless function call, rewrite and resolve
6413 if Is_Entity_Name
(P
)
6414 and then Ekind
(Entity
(P
)) = E_Function
6418 -- Similar processing for a protected function call
6420 elsif Nkind
(P
) = N_Selected_Component
6421 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Function
6426 if Is_Object_Reference
(P
) then
6427 Check_Object_Reference
(P
);
6429 elsif Is_Entity_Name
(P
)
6430 and then (Is_Type
(Entity
(P
))
6431 or else Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
6435 elsif Nkind
(P
) = N_Type_Conversion
6436 and then not Comes_From_Source
(P
)
6440 -- Some other compilers allow dubious use of X'???'Size
6442 elsif Relaxed_RM_Semantics
6443 and then Nkind
(P
) = N_Attribute_Reference
6448 Error_Attr_P
("invalid prefix for % attribute");
6451 Check_Not_Incomplete_Type
;
6453 Set_Etype
(N
, Universal_Integer
);
6459 -- Shares processing with Large attribute
6461 ---------------------------------------
6462 -- Small_Denominator/Small_Numerator --
6463 ---------------------------------------
6465 when Attribute_Small_Denominator
6466 | Attribute_Small_Numerator
6468 Check_Fixed_Point_Type_0
;
6469 Set_Etype
(N
, Universal_Integer
);
6475 when Attribute_Storage_Pool
6476 | Attribute_Simple_Storage_Pool
6480 if Is_Access_Type
(P_Type
) then
6481 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
6483 ("cannot use % attribute for access-to-subprogram type");
6486 -- Set appropriate entity
6488 if Present
(Associated_Storage_Pool
(Root_Type
(P_Type
))) then
6489 Set_Entity
(N
, Associated_Storage_Pool
(Root_Type
(P_Type
)));
6491 Set_Entity
(N
, RTE
(RE_Global_Pool_Object
));
6494 if Attr_Id
= Attribute_Storage_Pool
then
6495 if Present
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
6496 Name_Simple_Storage_Pool_Type
))
6498 Error_Msg_Name_1
:= Aname
;
6499 Error_Msg_Warn
:= SPARK_Mode
/= On
;
6501 ("cannot use % attribute for type with simple storage "
6503 Error_Msg_N
("\Program_Error [<<", N
);
6506 (N
, Make_Raise_Program_Error
6507 (Sloc
(N
), Reason
=> PE_Explicit_Raise
));
6510 Set_Etype
(N
, Class_Wide_Type
(RTE
(RE_Root_Storage_Pool
)));
6512 -- In the Simple_Storage_Pool case, verify that the pool entity is
6513 -- actually of a simple storage pool type, and set the attribute's
6514 -- type to the pool object's type.
6517 if No
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
6518 Name_Simple_Storage_Pool_Type
))
6521 ("cannot use % attribute for type without simple " &
6525 Set_Etype
(N
, Etype
(Entity
(N
)));
6528 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6529 -- Storage_Pool since this attribute is not defined for such
6530 -- types (RM E.2.2(17)).
6532 Validate_Remote_Access_To_Class_Wide_Type
(N
);
6535 Error_Attr_P
("prefix of % attribute must be access type");
6542 when Attribute_Storage_Size
=>
6545 if Is_Task_Type
(P_Type
) then
6546 Set_Etype
(N
, Universal_Integer
);
6548 -- Use with tasks is an obsolescent feature
6550 Check_Restriction
(No_Obsolescent_Features
, P
);
6552 elsif Is_Access_Type
(P_Type
) then
6553 Set_Etype
(N
, Universal_Integer
);
6555 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
6557 ("cannot use % attribute for access-to-subprogram type");
6560 if Is_Entity_Name
(P
)
6561 and then Is_Type
(Entity
(P
))
6565 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6566 -- Storage_Size since this attribute is not defined for
6567 -- such types (RM E.2.2(17)).
6569 Validate_Remote_Access_To_Class_Wide_Type
(N
);
6571 -- The prefix is allowed to be an implicit dereference of an
6572 -- access value designating a task.
6579 Error_Attr_P
("prefix of % attribute must be access or task type");
6586 when Attribute_Storage_Unit
=>
6587 Standard_Attribute
(Ttypes
.System_Storage_Unit
);
6593 when Attribute_Stream_Size
=>
6597 if Is_Entity_Name
(P
)
6598 and then Is_Elementary_Type
(Entity
(P
))
6600 Set_Etype
(N
, Universal_Integer
);
6602 Error_Attr_P
("invalid prefix for % attribute");
6609 when Attribute_Stub_Type
=>
6613 if Is_Remote_Access_To_Class_Wide_Type
(Base_Type
(P_Type
)) then
6615 -- For a real RACW [sub]type, use corresponding stub type
6617 if not Is_Generic_Type
(P_Type
) then
6620 (Corresponding_Stub_Type
(Base_Type
(P_Type
)), Loc
));
6622 -- For a generic type (that has been marked as an RACW using the
6623 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6624 -- type. Note that if the actual is not a remote access type, the
6625 -- instantiation will fail.
6628 -- Note: we go to the underlying type here because the view
6629 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6633 (Underlying_Type
(RTE
(RE_RACW_Stub_Type
)), Loc
));
6638 ("prefix of% attribute must be remote access-to-class-wide");
6645 -- Shares processing with Pred attribute
6651 when Attribute_Super
=>
6652 Error_Msg_Name_1
:= Aname
;
6653 Error_Msg_GNAT_Extension
("attribute %", Sloc
(N
));
6658 -- Verify that we are looking at a type with ancestors
6660 if not Is_Record_Type
(P_Type
)
6661 or else not Is_Tagged_Type
(P_Type
)
6664 ("prefix type of % attribute must be tagged or class-wide");
6667 -- Verify that the immediate parent type is suitable for 'Super
6670 Parents
: constant Elist_Id
:=
6671 -- Grab all immediate ancestor types of the prefix's type
6674 ((if Ekind
(P_Type
) = E_Class_Wide_Type
then Etype
(P_Type
)
6677 -- No parent type to reference
6679 if Is_Empty_Elmt_List
(Parents
) then
6680 Error_Attr_P
("prefix type of % must be type extension");
6682 -- We can't grant access of a child to a parent's private part
6684 elsif Depends_On_Private
(P_Type
) then
6685 Error_Attr_P
("prefix type of % is a private extension");
6687 -- Disallow view conversions to interfaces in order to avoid
6688 -- depending on whether an interface type is used as a parent
6689 -- or progenitor type.
6691 elsif Is_Interface
(Node
(First_Elmt
(Parents
))) then
6692 Error_Attr_P
("type of % cannot be an interface");
6695 -- Generate a view conversion and analyze it
6698 Make_Type_Conversion
(Loc
,
6699 Expression
=> Relocate_Node
(P
),
6701 New_Occurrence_Of
(Node
(First_Elmt
(Parents
)), Loc
)));
6703 Analyze_And_Resolve
(N
);
6706 --------------------------------
6707 -- System_Allocator_Alignment --
6708 --------------------------------
6710 when Attribute_System_Allocator_Alignment
=>
6711 Standard_Attribute
(Ttypes
.System_Allocator_Alignment
);
6717 when Attribute_Tag
=>
6721 if Is_Mutably_Tagged_CW_Equivalent_Type
(P_Type
) then
6724 elsif not Is_Tagged_Type
(P_Type
) then
6725 Error_Attr_P
("prefix of % attribute must be tagged");
6727 -- Next test does not apply to generated code why not, and what does
6728 -- the illegal reference mean???
6730 elsif Is_Object_Reference
(P
)
6731 and then not Is_Class_Wide_Type
(P_Type
)
6732 and then Comes_From_Source
(N
)
6735 ("% attribute can only be applied to objects " &
6736 "of class-wide type");
6739 -- The prefix cannot be an incomplete type. However, references to
6740 -- 'Tag can be generated when expanding interface conversions, and
6743 if Comes_From_Source
(N
) then
6744 Check_Not_Incomplete_Type
;
6746 -- 'Tag requires visibility on the corresponding package holding
6747 -- the tag, so record a reference here, to avoid spurious unused
6748 -- with_clause reported when compiling the main unit.
6750 if In_Extended_Main_Source_Unit
(Current_Scope
) then
6751 Set_Referenced
(P_Type
, True);
6752 Set_Referenced
(Scope
(P_Type
), True);
6756 -- Set appropriate type
6758 Set_Etype
(N
, RTE
(RE_Tag
));
6764 when Attribute_Target_Name
=> Target_Name
: declare
6765 TN
: constant String := Sdefault
.Target_Name
.all;
6769 Check_Standard_Prefix
;
6773 if TN
(TL
) = '/' or else TN
(TL
) = '\' then
6778 Make_String_Literal
(Loc
,
6779 Strval
=> TN
(TN
'First .. TL
)));
6780 Analyze_And_Resolve
(N
, Standard_String
);
6781 Set_Is_Static_Expression
(N
, True);
6788 -- Shares processing with Callable attribute
6794 when Attribute_To_Address
=> To_Address
: declare
6798 Check_System_Prefix
;
6800 Generate_Reference
(RTE
(RE_Address
), P
);
6801 Analyze_And_Resolve
(E1
, Any_Integer
);
6802 Set_Etype
(N
, RTE
(RE_Address
));
6803 Set_Is_Static_Expression
(N
, Is_Static_Expression
(E1
));
6805 -- OK static expression case, check range and set appropriate type
6807 if Is_OK_Static_Expression
(E1
) then
6808 Val
:= Expr_Value
(E1
);
6810 if Val
< -(Uint_2
** (System_Address_Size
- 1))
6812 Val
> Uint_2
** System_Address_Size
- 1
6814 Error_Attr
("address value out of range for % attribute", E1
);
6817 -- In most cases the expression is a numeric literal or some other
6818 -- address expression, but if it is a declared constant it may be
6819 -- of a compatible type that must be left on the node.
6821 if Is_Entity_Name
(E1
) then
6824 -- Set type to universal integer if negative
6827 Set_Etype
(E1
, Universal_Integer
);
6829 -- Otherwise set type to Unsigned_64 to accommodate large values
6832 Set_Etype
(E1
, Standard_Unsigned_64
);
6841 when Attribute_To_Any
=>
6843 Check_PolyORB_Attribute
;
6844 Set_Etype
(N
, RTE
(RE_Any
));
6850 -- Shares processing with Ceiling attribute
6856 when Attribute_Type_Class
=>
6859 Check_Not_Incomplete_Type
;
6860 Set_Etype
(N
, RTE
(RE_Type_Class
));
6866 when Attribute_TypeCode
=>
6868 Check_PolyORB_Attribute
;
6869 Set_Etype
(N
, RTE
(RE_TypeCode
));
6875 when Attribute_Type_Key
=> Type_Key
: declare
6876 Full_Name
: constant String_Id
:=
6877 Fully_Qualified_Name_String
(Entity
(P
), Append_NUL
=> False);
6880 -- The computed signature for the type
6883 -- To simplify the handling of mutually recursive types, follow a
6884 -- single dereference link in a composite type.
6886 procedure Compute_Type_Key
(T
: Entity_Id
);
6887 -- Create a CRC integer from the declaration of the type. For a
6888 -- composite type, fold in the representation of its components in
6889 -- recursive fashion. We use directly the source representation of
6890 -- the types involved.
6892 ----------------------
6893 -- Compute_Type_Key --
6894 ----------------------
6896 procedure Compute_Type_Key
(T
: Entity_Id
) is
6897 Buffer
: Source_Buffer_Ptr
;
6901 SFI
: Source_File_Index
;
6903 procedure Process_One_Declaration
;
6904 -- Update CRC with the characters of one type declaration, or a
6905 -- representation pragma that applies to the type.
6907 -----------------------------
6908 -- Process_One_Declaration --
6909 -----------------------------
6911 procedure Process_One_Declaration
is
6913 -- Scan type declaration, skipping blanks
6915 for Ptr
in P_Min
.. P_Max
loop
6916 if Buffer
(Ptr
) /= ' ' then
6917 System
.CRC32
.Update
(CRC
, Buffer
(Ptr
));
6920 end Process_One_Declaration
;
6922 -- Start of processing for Compute_Type_Key
6925 if Is_Itype
(T
) then
6929 -- If the type is declared in Standard, there is no source, so
6930 -- just use its name.
6932 if Scope
(T
) = Standard_Standard
then
6934 Name
: constant String := Get_Name_String
(Chars
(T
));
6936 for J
in Name
'Range loop
6937 System
.CRC32
.Update
(CRC
, Name
(J
));
6944 Sloc_Range
(Enclosing_Declaration
(T
), P_Min
, P_Max
);
6945 SFI
:= Get_Source_File_Index
(P_Min
);
6946 pragma Assert
(SFI
= Get_Source_File_Index
(P_Max
));
6947 Buffer
:= Source_Text
(SFI
);
6949 Process_One_Declaration
;
6951 -- Recurse on relevant component types
6953 if Is_Array_Type
(T
) then
6954 Compute_Type_Key
(Component_Type
(T
));
6956 elsif Is_Access_Type
(T
) then
6959 Compute_Type_Key
(Designated_Type
(T
));
6962 elsif Is_Derived_Type
(T
) then
6963 Compute_Type_Key
(Etype
(T
));
6965 elsif Is_Record_Type
(T
) then
6969 Comp
:= First_Component
(T
);
6970 while Present
(Comp
) loop
6971 Compute_Type_Key
(Etype
(Comp
));
6972 Next_Component
(Comp
);
6977 if Is_First_Subtype
(T
) then
6979 -- Fold in representation aspects for the type, which appear in
6980 -- the same source buffer. If the representation aspects are in
6981 -- a different source file, then skip them; they apply to some
6982 -- other type, perhaps one we're derived from.
6984 Rep
:= First_Rep_Item
(T
);
6986 while Present
(Rep
) loop
6987 if Comes_From_Source
(Rep
) then
6988 Sloc_Range
(Rep
, P_Min
, P_Max
);
6990 if SFI
= Get_Source_File_Index
(P_Min
) then
6991 pragma Assert
(SFI
= Get_Source_File_Index
(P_Max
));
6992 Process_One_Declaration
;
6996 Next_Rep_Item
(Rep
);
6999 end Compute_Type_Key
;
7001 -- Start of processing for Type_Key
7010 -- Copy all characters in Full_Name
7012 for J
in 1 .. String_Length
(Full_Name
) loop
7013 Store_String_Char
(Get_String_Char
(Full_Name
, Pos
(J
)));
7016 -- Compute CRC and convert it to string one character at a time, so
7017 -- as not to use Image within the compiler.
7020 Compute_Type_Key
(Entity
(P
));
7022 if not Is_Frozen
(Entity
(P
))
7023 and then not Is_Generic_Type
(Entity
(P
))
7024 and then not Is_Generic_Actual_Type
(Entity
(P
))
7026 Error_Msg_N
("premature usage of Type_Key?", N
);
7030 Store_String_Char
(Character'Val (48 + (CRC
rem 10)));
7034 Rewrite
(N
, Make_String_Literal
(Loc
, End_String
));
7035 Analyze_And_Resolve
(N
, Standard_String
);
7038 -----------------------
7039 -- Unbiased_Rounding --
7040 -----------------------
7042 -- Shares processing with Ceiling attribute
7044 ----------------------
7045 -- Unchecked_Access --
7046 ----------------------
7048 when Attribute_Unchecked_Access
=>
7049 if Comes_From_Source
(N
) then
7050 Check_Restriction
(No_Unchecked_Access
, N
);
7053 Analyze_Access_Attribute
;
7054 Check_Not_Incomplete_Type
;
7056 -------------------------
7057 -- Unconstrained_Array --
7058 -------------------------
7060 when Attribute_Unconstrained_Array
=>
7063 Check_Not_Incomplete_Type
;
7064 Set_Etype
(N
, Standard_Boolean
);
7065 Set_Is_Static_Expression
(N
, True);
7067 ------------------------------
7068 -- Universal_Literal_String --
7069 ------------------------------
7071 -- This is a GNAT specific attribute whose prefix must be a named
7072 -- number where the expression is either a single numeric literal,
7073 -- or a numeric literal immediately preceded by a minus sign. The
7074 -- result is equivalent to a string literal containing the text of
7075 -- the literal as it appeared in the source program with a possible
7076 -- leading minus sign.
7078 when Attribute_Universal_Literal_String
=>
7081 if not Is_Entity_Name
(P
)
7082 or else not Is_Named_Number
(Entity
(P
))
7084 Error_Attr_P
("prefix for % attribute must be named number");
7091 Src
: Source_Buffer_Ptr
;
7094 Expr
:= Original_Node
(Expression
(Parent
(Entity
(P
))));
7096 if Nkind
(Expr
) = N_Op_Minus
then
7098 Expr
:= Original_Node
(Right_Opnd
(Expr
));
7103 if Nkind
(Expr
) not in N_Integer_Literal | N_Real_Literal
then
7105 ("named number for % attribute must be simple literal", N
);
7108 -- Build string literal corresponding to source literal text
7113 Store_String_Char
(Get_Char_Code
('-'));
7117 Src
:= Source_Text
(Get_Source_File_Index
(S
));
7119 while Src
(S
) /= ';' and then Src
(S
) /= ' ' loop
7120 Store_String_Char
(Get_Char_Code
(Src
(S
)));
7124 -- Now we rewrite the attribute with the string literal
7127 Make_String_Literal
(Loc
, End_String
));
7129 Set_Is_Static_Expression
(N
, True);
7133 -------------------------
7134 -- Unrestricted_Access --
7135 -------------------------
7137 -- This is a GNAT specific attribute which is like Access except that
7138 -- all scope checks and checks for aliased views are omitted. It is
7139 -- documented as being equivalent to the use of the Address attribute
7140 -- followed by an unchecked conversion to the target access type.
7142 when Attribute_Unrestricted_Access
=>
7144 -- If from source, deal with relevant restrictions
7146 if Comes_From_Source
(N
) then
7147 Check_Restriction
(No_Unchecked_Access
, N
);
7149 if Nkind
(P
) in N_Has_Entity
7150 and then Present
(Entity
(P
))
7151 and then Is_Object
(Entity
(P
))
7153 Check_Restriction
(No_Implicit_Aliasing
, N
);
7157 if Is_Entity_Name
(P
) then
7158 Set_Address_Taken
(Entity
(P
));
7161 -- It might seem reasonable to call Address_Checks here to apply the
7162 -- same set of semantic checks that we enforce for 'Address (after
7163 -- all we document Unrestricted_Access as being equivalent to the
7164 -- use of Address followed by an Unchecked_Conversion). However, if
7165 -- we do enable these checks, we get multiple failures in both the
7166 -- compiler run-time and in our regression test suite, so we leave
7167 -- out these checks for now. To be investigated further some time???
7171 -- Now complete analysis using common access processing
7173 Analyze_Access_Attribute
;
7179 when Attribute_Update
=> Update
: declare
7180 Common_Typ
: Entity_Id
;
7181 -- The common type of a multiple component update for a record
7183 Comps
: Elist_Id
:= No_Elist
;
7184 -- A list used in the resolution of a record update. It contains the
7185 -- entities of all record components processed so far.
7187 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
);
7188 -- Analyze and resolve array_component_association Assoc against the
7189 -- index of array type P_Type.
7191 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
);
7192 -- Analyze and resolve record_component_association Comp against
7193 -- record type P_Type.
7195 ------------------------------------
7196 -- Analyze_Array_Component_Update --
7197 ------------------------------------
7199 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
) is
7203 Index_Typ
: Entity_Id
;
7207 -- The current association contains a sequence of indexes denoting
7208 -- an element of a multidimensional array:
7210 -- (Index_1, ..., Index_N)
7212 -- Examine each individual index and resolve it against the proper
7213 -- index type of the array.
7215 if Nkind
(First
(Choices
(Assoc
))) = N_Aggregate
then
7216 Expr
:= First
(Choices
(Assoc
));
7217 while Present
(Expr
) loop
7219 -- The use of others is illegal (SPARK RM 4.4.1(12))
7221 if Nkind
(Expr
) = N_Others_Choice
then
7223 ("OTHERS choice not allowed in attribute %", Expr
);
7225 -- Otherwise analyze and resolve all indexes
7228 Index
:= First
(Expressions
(Expr
));
7229 Index_Typ
:= First_Index
(P_Type
);
7230 while Present
(Index
) and then Present
(Index_Typ
) loop
7231 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
7233 Next_Index
(Index_Typ
);
7236 -- Detect a case where the association either lacks an
7237 -- index or contains an extra index.
7239 if Present
(Index
) or else Present
(Index_Typ
) then
7241 ("dimension mismatch in index list", Assoc
);
7248 -- The current association denotes either a single component or a
7249 -- range of components of a one dimensional array:
7253 -- Resolve the index or its high and low bounds (if range) against
7254 -- the proper index type of the array.
7257 Index
:= First
(Choices
(Assoc
));
7258 Index_Typ
:= First_Index
(P_Type
);
7260 if Present
(Next_Index
(Index_Typ
)) then
7261 Error_Msg_N
("too few subscripts in array reference", Assoc
);
7264 while Present
(Index
) loop
7266 -- The use of others is illegal (SPARK RM 4.4.1(12))
7268 if Nkind
(Index
) = N_Others_Choice
then
7270 ("OTHERS choice not allowed in attribute %", Index
);
7272 -- The index denotes a range of elements
7274 elsif Nkind
(Index
) = N_Range
then
7275 Low
:= Low_Bound
(Index
);
7276 High
:= High_Bound
(Index
);
7278 Analyze_And_Resolve
(Low
, Etype
(Index_Typ
));
7279 Analyze_And_Resolve
(High
, Etype
(Index_Typ
));
7281 -- Otherwise the index denotes a single element
7284 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
7290 end Analyze_Array_Component_Update
;
7292 -------------------------------------
7293 -- Analyze_Record_Component_Update --
7294 -------------------------------------
7296 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
) is
7297 Comp_Name
: constant Name_Id
:= Chars
(Comp
);
7298 Base_Typ
: Entity_Id
;
7299 Comp_Or_Discr
: Entity_Id
;
7302 -- Find the discriminant or component whose name corresponds to
7303 -- Comp. A simple character comparison is sufficient because all
7304 -- visible names within a record type are unique.
7306 Comp_Or_Discr
:= First_Entity
(P_Type
);
7307 while Present
(Comp_Or_Discr
) loop
7308 if Chars
(Comp_Or_Discr
) = Comp_Name
then
7310 -- Decorate the component reference by setting its entity
7311 -- and type for resolution purposes.
7313 Set_Entity
(Comp
, Comp_Or_Discr
);
7314 Set_Etype
(Comp
, Etype
(Comp_Or_Discr
));
7318 Next_Entity
(Comp_Or_Discr
);
7321 -- Diagnose an illegal reference
7323 if Present
(Comp_Or_Discr
) then
7324 if Ekind
(Comp_Or_Discr
) = E_Discriminant
then
7326 ("attribute % may not modify record discriminants", Comp
);
7328 else pragma Assert
(Ekind
(Comp_Or_Discr
) = E_Component
);
7329 if Contains
(Comps
, Comp_Or_Discr
) then
7330 Error_Msg_N
("component & already updated", Comp
);
7332 -- Mark this component as processed
7335 Append_New_Elmt
(Comp_Or_Discr
, Comps
);
7339 -- The update aggregate mentions an entity that does not belong to
7343 Error_Msg_N
("& is not a component of aggregate subtype", Comp
);
7346 -- Verify the consistency of types when the current component is
7347 -- part of a multiple component update.
7349 -- Comp_1 | ... | Comp_N => <value>
7351 if Present
(Etype
(Comp
)) then
7352 Base_Typ
:= Base_Type
(Etype
(Comp
));
7354 -- Save the type of the first component reference as the
7355 -- remaning references (if any) must resolve to this type.
7357 if No
(Common_Typ
) then
7358 Common_Typ
:= Base_Typ
;
7360 elsif Base_Typ
/= Common_Typ
then
7362 ("components in choice list must have same type", Comp
);
7365 end Analyze_Record_Component_Update
;
7372 -- Start of processing for Update
7375 if Warn_On_Obsolescent_Feature
then
7376 Error_Msg_N
("?j?attribute Update is an obsolescent feature", N
);
7377 Error_Msg_N
("\?j?use a delta aggregate instead", N
);
7382 if not Is_Object_Reference
(P
) then
7383 Error_Attr_P
("prefix of attribute % must denote an object");
7385 elsif not Is_Array_Type
(P_Type
)
7386 and then not Is_Record_Type
(P_Type
)
7388 Error_Attr_P
("prefix of attribute % must be a record or array");
7390 elsif Is_Inherently_Limited_Type
(P_Type
) then
7391 Error_Attr
("prefix of attribute % cannot be limited", N
);
7393 elsif Nkind
(E1
) /= N_Aggregate
then
7394 Error_Attr
("attribute % requires component association list", N
);
7396 elsif Present
(Expressions
(E1
)) then
7397 Error_Attr
("attribute % requires named component associations",
7398 First
(Expressions
(E1
)));
7402 -- Inspect the update aggregate, looking at all the associations and
7403 -- choices. Perform the following checks:
7405 -- 1) Legality of "others" in all cases
7406 -- 2) Legality of <>
7407 -- 3) Component legality for arrays
7408 -- 4) Component legality for records
7410 -- The remaining checks are performed on the expanded attribute
7412 Assoc
:= First
(Component_Associations
(E1
));
7413 while Present
(Assoc
) loop
7415 -- The use of <> is illegal (SPARK RM 4.4.1(1))
7417 if Box_Present
(Assoc
) then
7419 ("default initialization not allowed in attribute %", Assoc
);
7421 -- Otherwise process the association
7424 Analyze
(Expression
(Assoc
));
7426 if Is_Array_Type
(P_Type
) then
7427 Analyze_Array_Component_Update
(Assoc
);
7429 elsif Is_Record_Type
(P_Type
) then
7431 -- Reset the common type used in a multiple component update
7432 -- as we are processing the contents of a new association.
7434 Common_Typ
:= Empty
;
7436 Comp
:= First
(Choices
(Assoc
));
7437 while Present
(Comp
) loop
7438 if Nkind
(Comp
) = N_Identifier
then
7439 Analyze_Record_Component_Update
(Comp
);
7441 -- The use of others is illegal (SPARK RM 4.4.1(5))
7443 elsif Nkind
(Comp
) = N_Others_Choice
then
7445 ("OTHERS choice not allowed in attribute %", Comp
);
7447 -- The name of a record component cannot appear in any
7452 ("name should be identifier or OTHERS", Comp
);
7463 -- The type of attribute 'Update is that of the prefix
7465 Set_Etype
(N
, P_Type
);
7467 Sem_Warn
.Warn_On_Suspicious_Update
(N
);
7474 when Attribute_Val
=>
7476 Check_Discrete_Type
;
7478 -- Note, we need a range check in general, but we wait for the
7479 -- Resolve call to do this, since we want to let Eval_Attribute
7480 -- have a chance to find an static illegality first.
7482 Resolve
(E1
, Any_Integer
);
7483 Set_Etype
(N
, P_Base_Type
);
7489 when Attribute_Valid
=> Valid
: declare
7490 Pred_Func
: constant Entity_Id
:= Predicate_Function
(P_Type
);
7495 -- Ignore check for object if we have a 'Valid reference generated
7496 -- by the expanded code, since in some cases valid checks can occur
7497 -- on items that are names, but are not objects (e.g. attributes).
7499 if Comes_From_Source
(N
) then
7500 Check_Object_Reference
(P
);
7502 if not Is_Scalar_Type
(P_Type
) then
7503 Error_Attr_P
("object for % attribute must be of scalar type");
7506 -- If the attribute appears within the subtype's own predicate
7507 -- function, then issue a warning that this will cause infinite
7510 if Present
(Pred_Func
) and then Current_Scope
= Pred_Func
then
7511 Error_Msg_N
("attribute Valid requires a predicate check??", N
);
7512 Error_Msg_N
("\and will result in infinite recursion??", N
);
7516 Set_Etype
(N
, Standard_Boolean
);
7523 when Attribute_Valid_Value
=>
7525 Check_Enumeration_Type
;
7526 Check_Enum_Image
(Check_Enumeration_Maps
=> True);
7527 Set_Etype
(N
, Standard_Boolean
);
7528 Validate_Non_Static_Attribute_Function_Call
;
7530 if P_Type
in Standard_Boolean
7531 | Standard_Character
7532 | Standard_Wide_Character
7533 | Standard_Wide_Wide_Character
7536 ("prefix of % attribute must not be a type in Standard");
7539 if Discard_Names
(First_Subtype
(P_Type
)) then
7541 ("prefix of % attribute must not have Discard_Names");
7548 when Attribute_Valid_Scalars
=> Valid_Scalars
: declare
7552 if Comes_From_Source
(N
) then
7553 Check_Object_Reference
(P
);
7555 -- Attribute 'Valid_Scalars is illegal on unchecked union types
7556 -- regardles of the privacy, because it is not always guaranteed
7557 -- that the components are retrievable based on whether the
7558 -- discriminants are inferable.
7560 if Has_Unchecked_Union
(Validated_View
(P_Type
)) then
7562 ("attribute % not allowed for Unchecked_Union type");
7564 -- Do not emit any diagnostics related to private types to avoid
7565 -- disclosing the structure of the type.
7567 elsif Is_Private_Type
(P_Type
) then
7569 -- Attribute 'Valid_Scalars is not supported on private tagged
7570 -- types due to a code generation issue. Is_Visible_Component
7571 -- does not allow for a component of a private tagged type to
7572 -- be successfully retrieved.
7573 -- ??? This attribute should simply ignore type privacy
7574 -- (see Validated_View). It should examine components of the
7575 -- tagged type extensions (if any) and recursively examine
7576 -- 'Valid_Scalars of the parent's type (if any).
7578 -- Do not use Error_Attr_P because this bypasses any subsequent
7579 -- processing and leaves the attribute with type Any_Type. This
7580 -- in turn prevents the proper expansion of the attribute into
7583 if Is_Tagged_Type
(P_Type
) then
7584 Error_Msg_Name_1
:= Aname
;
7585 Error_Msg_N
("??effects of attribute % are ignored", N
);
7588 -- Otherwise the type is not private
7591 if not Scalar_Part_Present
(P_Type
) then
7592 Error_Msg_Name_1
:= Aname
;
7594 ("??attribute % always True, no scalars to check", P
);
7595 Set_Boolean_Result
(N
, True);
7600 Set_Etype
(N
, Standard_Boolean
);
7607 when Attribute_Value
7608 | Attribute_Wide_Value
7609 | Attribute_Wide_Wide_Value
7613 Check_Enum_Image
(Check_Enumeration_Maps
=> True);
7615 -- Set Etype before resolving expression because expansion of
7616 -- expression may require enclosing type. Note that the type
7617 -- returned by 'Value is the base type of the prefix type.
7619 Set_Etype
(N
, P_Base_Type
);
7620 Validate_Non_Static_Attribute_Function_Call
;
7622 -- Check restriction No_Fixed_IO
7624 if Restriction_Check_Required
(No_Fixed_IO
)
7625 and then Is_Fixed_Point_Type
(P_Type
)
7627 Check_Restriction
(No_Fixed_IO
, P
);
7634 -- Shares processing with Machine_Size attribute
7640 when Attribute_Version
=>
7643 Set_Etype
(N
, RTE
(RE_Version_String
));
7649 when Attribute_Wchar_T_Size
=>
7650 Standard_Attribute
(Interfaces_Wchar_T_Size
);
7656 when Attribute_Wide_Image
=>
7657 Analyze_Image_Attribute
(Standard_Wide_String
);
7659 ---------------------
7660 -- Wide_Wide_Image --
7661 ---------------------
7663 when Attribute_Wide_Wide_Image
=>
7664 Analyze_Image_Attribute
(Standard_Wide_Wide_String
);
7670 -- Shares processing with Value attribute
7672 ---------------------
7673 -- Wide_Wide_Value --
7674 ---------------------
7676 -- Shares processing with Value attribute
7678 ---------------------
7679 -- Wide_Wide_Width --
7680 ---------------------
7682 when Attribute_Wide_Wide_Width
7683 | Attribute_Wide_Width
7688 Set_Etype
(N
, Universal_Integer
);
7694 -- Shares processing with Wide_Wide_Width attribute
7700 -- Shares processing with Wide_Wide_Width attribute
7706 when Attribute_Word_Size
=>
7707 Standard_Attribute
(System_Word_Size
);
7713 when Attribute_Write
=>
7715 Check_Stream_Attribute
(TSS_Stream_Write
);
7716 Set_Etype
(N
, Standard_Void_Type
);
7717 Resolve
(N
, Standard_Void_Type
);
7721 -- In SPARK certain attributes (see below) depend on Tasking_State.
7722 -- Ensure that the entity is available for gnat2why by loading it.
7723 -- See SPARK RM 9(19) for the relevant rule.
7725 if GNATprove_Mode
then
7727 when Attribute_Callable
7730 | Attribute_Terminated
7732 SPARK_Implicit_Load
(RE_Tasking_State
);
7739 -- All errors raise Bad_Attribute, so that we get out before any further
7740 -- damage occurs when an error is detected (for example, if we check for
7741 -- one attribute expression, and the check succeeds, we want to be able
7742 -- to proceed securely assuming that an expression is in fact present.
7744 -- Note: we set the attribute analyzed in this case to prevent any
7745 -- attempt at reanalysis which could generate spurious error msgs.
7748 when Bad_Attribute
=>
7750 Set_Etype
(N
, Any_Type
);
7752 end Analyze_Attribute
;
7754 --------------------
7755 -- Eval_Attribute --
7756 --------------------
7758 procedure Eval_Attribute
(N
: Node_Id
) is
7759 Loc
: constant Source_Ptr
:= Sloc
(N
);
7761 C_Type
: constant Entity_Id
:= Etype
(N
);
7762 -- The type imposed by the context
7765 -- Attribute_Name (N) after verification of validity of N
7768 -- Get_Attribute_Id (Aname) after Aname is set
7771 -- Prefix (N) after verification of validity of N
7774 -- First expression, or Empty if none
7777 -- Second expression, or Empty if none
7779 P_Entity
: Entity_Id
;
7780 -- Entity denoted by prefix
7783 -- The type of the prefix
7785 P_Base_Type
: Entity_Id
;
7786 -- The base type of the prefix type
7788 P_Root_Type
: Entity_Id
;
7789 -- The root type of the prefix type
7791 Static
: Boolean := False;
7792 -- True if the result is Static. This is set by the general processing
7793 -- to true if the prefix is static, and all expressions are static. It
7794 -- can be reset as processing continues for particular attributes. This
7795 -- flag can still be True if the reference raises a constraint error.
7796 -- Is_Static_Expression (N) is set to follow this value as it is set
7797 -- and we could always reference this, but it is convenient to have a
7798 -- simple short name to use, since it is frequently referenced.
7800 Lo_Bound
, Hi_Bound
: Node_Id
;
7801 -- Expressions for low and high bounds of type or array index referenced
7802 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7805 -- Constraint error node used if we have an attribute reference has
7806 -- an argument that raises a constraint error. In this case we replace
7807 -- the attribute with a raise constraint_error node. This is important
7808 -- processing, since otherwise gigi might see an attribute which it is
7809 -- unprepared to deal with.
7811 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
);
7812 -- If Bound is a reference to a discriminant of a task or protected type
7813 -- occurring within the object's body, rewrite attribute reference into
7814 -- a reference to the corresponding discriminal. Use for the expansion
7815 -- of checks against bounds of entry family index subtypes.
7817 procedure Check_Expressions
;
7818 -- In case where the attribute is not foldable, the expressions, if
7819 -- any, of the attribute, are in a non-static context. This procedure
7820 -- performs the required additional checks.
7822 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean;
7823 -- Determines if the given type has compile time known bounds. Note
7824 -- that we enter the case statement even in cases where the prefix
7825 -- type does NOT have known bounds, so it is important to guard any
7826 -- attempt to evaluate both bounds with a call to this function.
7828 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
);
7829 -- This procedure is called when the attribute N has a non-static
7830 -- but compile time known value given by Val. It includes the
7831 -- necessary checks for out of range values.
7833 function Fore_Value
return Nat
;
7834 -- Computes the Fore value for the current attribute prefix, which is
7835 -- known to be a static fixed-point type. Used by Fore and Width.
7837 function Full_Type
(Typ
: Entity_Id
) return Entity_Id
;
7838 -- Return the Underlying_Type of Typ if it exists, otherwise return Typ
7840 function Mantissa
return Uint
;
7841 -- Returns the Mantissa value for the prefix type
7843 procedure Set_Bounds
;
7844 -- Used for First, Last and Length attributes applied to an array or
7845 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7846 -- and high bound expressions for the index referenced by the attribute
7847 -- designator (i.e. the first index if no expression is present, and the
7848 -- N'th index if the value N is present as an expression). Also used for
7849 -- First and Last of scalar types and for First_Valid and Last_Valid.
7850 -- Static is reset to False if the type or index type is not statically
7853 -----------------------------------
7854 -- Check_Concurrent_Discriminant --
7855 -----------------------------------
7857 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
) is
7859 -- The concurrent (task or protected) type
7862 if Nkind
(Bound
) = N_Identifier
7863 and then Ekind
(Entity
(Bound
)) = E_Discriminant
7864 and then Is_Concurrent_Record_Type
(Scope
(Entity
(Bound
)))
7866 Tsk
:= Corresponding_Concurrent_Type
(Scope
(Entity
(Bound
)));
7868 if In_Open_Scopes
(Tsk
) and then Has_Completion
(Tsk
) then
7870 -- Find discriminant of original concurrent type, and use
7871 -- its current discriminal, which is the renaming within
7872 -- the task/protected body.
7876 (Find_Body_Discriminal
(Entity
(Bound
)), Loc
));
7879 end Check_Concurrent_Discriminant
;
7881 -----------------------
7882 -- Check_Expressions --
7883 -----------------------
7885 procedure Check_Expressions
is
7889 while Present
(E
) loop
7890 Check_Non_Static_Context
(E
);
7893 end Check_Expressions
;
7895 ----------------------------------
7896 -- Compile_Time_Known_Attribute --
7897 ----------------------------------
7899 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
) is
7900 T
: constant Entity_Id
:= Etype
(N
);
7903 -- If we are processing a pragma Compile_Time_{Warning,Error} after
7904 -- the back end has been called and the value of this attribute is
7905 -- known at compile time, then it is safe to perform its evaluation
7906 -- as static. This is needed to perform the evaluation of the full
7907 -- boolean expression of these pragmas.
7909 Fold_Uint
(N
, Val
, Static
=> In_Compile_Time_Warning_Or_Error
);
7911 -- Check that result is in bounds of the type if it is static
7913 if Is_In_Range
(N
, T
, Assume_Valid
=> False) then
7916 elsif Is_Out_Of_Range
(N
, T
) then
7917 Apply_Compile_Time_Constraint_Error
7918 (N
, "value not in range of}??", CE_Range_Check_Failed
);
7920 elsif not Range_Checks_Suppressed
(T
) then
7921 Enable_Range_Check
(N
);
7924 Set_Do_Range_Check
(N
, False);
7926 end Compile_Time_Known_Attribute
;
7928 -------------------------------
7929 -- Compile_Time_Known_Bounds --
7930 -------------------------------
7932 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean is
7935 Compile_Time_Known_Value
(Type_Low_Bound
(Typ
))
7937 Compile_Time_Known_Value
(Type_High_Bound
(Typ
));
7938 end Compile_Time_Known_Bounds
;
7944 -- Note that the Fore calculation is based on the actual values
7945 -- of the bounds, and does not take into account possible rounding.
7947 function Fore_Value
return Nat
is
7948 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
7949 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
7950 Small
: constant Ureal
:= Small_Value
(P_Type
);
7951 Lo_Real
: constant Ureal
:= Lo
* Small
;
7952 Hi_Real
: constant Ureal
:= Hi
* Small
;
7957 -- Bounds are given in terms of small units, so first compute
7958 -- proper values as reals.
7960 T
:= UR_Max
(abs Lo_Real
, abs Hi_Real
);
7963 -- Loop to compute proper value if more than one digit required
7965 while T
>= Ureal_10
loop
7977 function Full_Type
(Typ
: Entity_Id
) return Entity_Id
is
7978 Underlying_Typ
: constant Entity_Id
:= Underlying_Type
(Typ
);
7981 if Present
(Underlying_Typ
) then
7982 return Underlying_Typ
;
7993 -- Table of mantissa values accessed by function Computed using
7996 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7998 -- where D is T'Digits (RM83 3.5.7)
8000 Mantissa_Value
: constant array (Nat
range 1 .. 40) of Nat
:= (
8042 function Mantissa
return Uint
is
8045 UI_From_Int
(Mantissa_Value
(UI_To_Int
(Digits_Value
(P_Type
))));
8052 procedure Set_Bounds
is
8058 -- For a string literal subtype, we have to construct the bounds.
8059 -- Valid Ada code never applies attributes to string literals, but
8060 -- it is convenient to allow the expander to generate attribute
8061 -- references of this type (e.g. First and Last applied to a string
8064 -- Note that the whole point of the E_String_Literal_Subtype is to
8065 -- avoid this construction of bounds, but the cases in which we
8066 -- have to materialize them are rare enough that we don't worry.
8068 -- The low bound is simply the low bound of the base type. The
8069 -- high bound is computed from the length of the string and this
8072 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
8073 Ityp
:= Etype
(First_Index
(Base_Type
(P_Type
)));
8074 Lo_Bound
:= Type_Low_Bound
(Ityp
);
8077 Make_Integer_Literal
(Sloc
(P
),
8079 Expr_Value
(Lo_Bound
) + String_Literal_Length
(P_Type
) - 1);
8081 Set_Parent
(Hi_Bound
, P
);
8082 Analyze_And_Resolve
(Hi_Bound
, Etype
(Lo_Bound
));
8085 -- For non-array case, just get bounds of scalar type
8087 elsif Is_Scalar_Type
(P_Type
) then
8090 -- For a fixed-point type, we must freeze to get the attributes
8091 -- of the fixed-point type set now so we can reference them.
8093 if Is_Fixed_Point_Type
(P_Type
)
8094 and then not Is_Frozen
(Base_Type
(P_Type
))
8095 and then Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
8096 and then Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
8098 Freeze_Fixed_Point_Type
(Base_Type
(P_Type
));
8101 -- For array case, get type of proper index
8107 Ndim
:= UI_To_Int
(Expr_Value
(E1
));
8110 Indx
:= First_Index
(P_Type
);
8111 for J
in 1 .. Ndim
- 1 loop
8115 -- If no index type, get out (some other error occurred, and
8116 -- we don't have enough information to complete the job).
8124 Ityp
:= Etype
(Indx
);
8127 -- A discrete range in an index constraint is allowed to be a
8128 -- subtype indication. This is syntactically a pain, but should
8129 -- not propagate to the entity for the corresponding index subtype.
8130 -- After checking that the subtype indication is legal, the range
8131 -- of the subtype indication should be transfered to the entity.
8132 -- The attributes for the bounds should remain the simple retrievals
8133 -- that they are now.
8135 Lo_Bound
:= Type_Low_Bound
(Ityp
);
8136 Hi_Bound
:= Type_High_Bound
(Ityp
);
8138 -- If subtype is non-static, result is definitely non-static
8140 if not Is_Static_Subtype
(Ityp
) then
8142 Set_Is_Static_Expression
(N
, False);
8144 -- Subtype is static, does it raise CE?
8146 elsif not Is_OK_Static_Subtype
(Ityp
) then
8147 Set_Raises_Constraint_Error
(N
);
8151 -- Start of processing for Eval_Attribute
8154 -- Return immediately if e.g. N has been rewritten or is malformed due
8155 -- to previous errors.
8157 if Nkind
(N
) /= N_Attribute_Reference
then
8161 Aname
:= Attribute_Name
(N
);
8162 Id
:= Get_Attribute_Id
(Aname
);
8165 -- The To_Address attribute can be static, but it cannot be evaluated at
8166 -- compile time, so just return.
8168 if Id
= Attribute_To_Address
then
8172 -- Initialize result as non-static, will be reset if appropriate
8174 Set_Is_Static_Expression
(N
, False);
8176 -- Acquire first two expressions (at the moment, no attributes take more
8177 -- than two expressions in any case).
8179 if Present
(Expressions
(N
)) then
8180 E1
:= First
(Expressions
(N
));
8187 -- Special processing for Enabled attribute. This attribute has a very
8188 -- special prefix, and the easiest way to avoid lots of special checks
8189 -- to protect this special prefix from causing trouble is to deal with
8190 -- this attribute immediately and be done with it.
8192 if Id
= Attribute_Enabled
then
8194 -- We skip evaluation if the expander is not active. This is not just
8195 -- an optimization. It is of key importance that we not rewrite the
8196 -- attribute in a generic template, since we want to pick up the
8197 -- setting of the check in the instance.
8199 if not Inside_A_Generic
then
8201 C
: constant Check_Id
:= Get_Check_Id
(Chars
(P
));
8206 if C
in Predefined_Check_Id
then
8207 R
:= Scope_Suppress
.Suppress
(C
);
8209 R
:= Is_Check_Suppressed
(Empty
, C
);
8213 R
:= Is_Check_Suppressed
(Entity
(E1
), C
);
8216 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(not R
), Loc
));
8223 -- Attribute 'Img applied to a static enumeration value is static, and
8224 -- we will do the folding right here (things get confused if we let this
8225 -- case go through the normal circuitry).
8227 if Id
= Attribute_Img
8228 and then Is_Entity_Name
(P
)
8229 and then Is_Enumeration_Type
(Etype
(Entity
(P
)))
8230 and then Is_OK_Static_Expression
(P
)
8233 Lit
: constant Entity_Id
:= Expr_Value_E
(P
);
8234 Typ
: constant Entity_Id
:= Etype
(Entity
(P
));
8240 -- If Discard_Names is in effect for the type, then we emit the
8241 -- numeric representation of the prefix literal 'Pos attribute,
8242 -- prefixed with a single space.
8244 if Discard_Names
(Typ
) then
8245 UI_Image
(Enumeration_Pos
(Lit
), Decimal
);
8246 Store_String_Char
(' ');
8247 Store_String_Chars
(UI_Image_Buffer
(1 .. UI_Image_Length
));
8249 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
8250 Set_Casing
(All_Upper_Case
);
8251 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
8256 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
8257 Analyze_And_Resolve
(N
, Standard_String
);
8258 Set_Is_Static_Expression
(N
, True);
8264 -- Special processing for cases where the prefix is an object or value,
8265 -- including string literals (attributes of string literals can only
8266 -- appear in generated code) and current instance prefixes in type or
8269 if Is_Object_Reference
(P
)
8270 or else Is_Current_Instance_Reference_In_Type_Aspect
(P
)
8271 or else Nkind
(P
) = N_String_Literal
8272 or else (Is_Entity_Name
(P
)
8273 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
8275 -- For Alignment, give alignment of object if available, otherwise we
8276 -- cannot fold Alignment.
8278 if Id
= Attribute_Alignment
then
8279 if Is_Entity_Name
(P
) and then Known_Alignment
(Entity
(P
)) then
8280 Compile_Time_Known_Attribute
(N
, Alignment
(Entity
(P
)));
8287 -- For Component_Size, the prefix is an array object, and we apply
8288 -- the attribute to the type of the object. This is allowed for both
8289 -- unconstrained and constrained arrays, since the bounds have no
8290 -- influence on the value of this attribute.
8292 elsif Id
= Attribute_Component_Size
then
8293 P_Entity
:= Etype
(P
);
8295 -- For Enum_Rep, evaluation depends on the nature of the prefix and
8296 -- the optional argument.
8298 elsif Id
= Attribute_Enum_Rep
then
8299 if Is_Entity_Name
(P
) then
8302 Enum_Expr
: Node_Id
;
8303 -- The enumeration-type expression of interest
8308 if Ekind
(Entity
(P
)) in E_Constant | E_Enumeration_Literal
8312 -- Enum_Type'Enum_Rep (E1) case
8314 elsif Is_Enumeration_Type
(Entity
(P
)) then
8317 -- Otherwise the attribute must be expanded into a
8318 -- conversion and evaluated at run time.
8325 -- We can fold if the expression is an enumeration
8326 -- literal, or if it denotes a constant whose value
8327 -- is known at compile time.
8329 if Nkind
(Enum_Expr
) in N_Has_Entity
8330 and then (Ekind
(Entity
(Enum_Expr
)) =
8331 E_Enumeration_Literal
8333 (Ekind
(Entity
(Enum_Expr
)) = E_Constant
8334 and then Nkind
(Parent
(Entity
(Enum_Expr
))) =
8335 N_Object_Declaration
8337 (Expression
(Parent
(Entity
(P
))))
8338 and then Compile_Time_Known_Value
8339 (Expression
(Parent
(Entity
(P
))))))
8341 P_Entity
:= Etype
(P
);
8348 -- Otherwise the attribute is illegal, do not attempt to perform
8349 -- any kind of folding.
8355 -- For Bit_Position, give Component_Bit_Offset of object if available
8356 -- otherwise we cannot fold Bit_Position. Note that the attribute can
8357 -- be applied to a naked record component in generated code, in which
8358 -- case the prefix is an identifier that references the component or
8359 -- discriminant entity.
8361 elsif Id
= Attribute_Bit_Position
then
8366 if Is_Entity_Name
(P
) then
8369 CE
:= Entity
(Selector_Name
(P
));
8372 if Known_Static_Component_Bit_Offset
(CE
) then
8373 Compile_Time_Known_Attribute
8374 (N
, Component_Bit_Offset
(CE
));
8382 -- For Position, in Ada 2005 (or later) if we have the non-default
8383 -- bit order, we return the original value as given in the component
8384 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8385 -- default bit order) return the value if it is known statically.
8387 elsif Id
= Attribute_Position
then
8389 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
8392 if Present
(Component_Clause
(CE
))
8393 and then Ada_Version
>= Ada_2005
8394 and then Reverse_Bit_Order
(Scope
(CE
))
8396 Compile_Time_Known_Attribute
8397 (N
, Expr_Value
(Position
(Component_Clause
(CE
))));
8399 elsif Known_Static_Component_Bit_Offset
(CE
) then
8400 Compile_Time_Known_Attribute
8401 (N
, Component_Bit_Offset
(CE
) / System_Storage_Unit
);
8410 -- For First_Bit, in Ada 2005 (or later) if we have the non-default
8411 -- bit order, we return the original value as given in the component
8412 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8413 -- default bit order) return the value if it is known statically.
8415 elsif Id
= Attribute_First_Bit
then
8417 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
8420 if Present
(Component_Clause
(CE
))
8421 and then Ada_Version
>= Ada_2005
8422 and then Reverse_Bit_Order
(Scope
(CE
))
8424 Compile_Time_Known_Attribute
8425 (N
, Expr_Value
(First_Bit
(Component_Clause
(CE
))));
8427 elsif Known_Static_Component_Bit_Offset
(CE
) then
8428 Compile_Time_Known_Attribute
8429 (N
, Component_Bit_Offset
(CE
) mod System_Storage_Unit
);
8438 -- For Last_Bit, in Ada 2005 (or later) if we have the non-default
8439 -- bit order, we return the original value as given in the component
8440 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8441 -- default bit order) return the value if it is known statically.
8443 elsif Id
= Attribute_Last_Bit
then
8445 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
8448 if Present
(Component_Clause
(CE
))
8449 and then Ada_Version
>= Ada_2005
8450 and then Reverse_Bit_Order
(Scope
(CE
))
8452 Compile_Time_Known_Attribute
8453 (N
, Expr_Value
(Last_Bit
(Component_Clause
(CE
))));
8455 elsif Known_Static_Component_Bit_Offset
(CE
)
8456 and then Known_Static_Esize
(CE
)
8458 Compile_Time_Known_Attribute
8459 (N
, (Component_Bit_Offset
(CE
) mod System_Storage_Unit
)
8468 -- For First, Last and Length, the prefix is an array object, and we
8469 -- apply the attribute to its type, but we need a constrained type
8470 -- for this, so we use the actual subtype if available.
8472 elsif Id
= Attribute_First
8473 or else Id
= Attribute_Last
8474 or else Id
= Attribute_Length
8477 AS
: constant Entity_Id
:= Get_Actual_Subtype_If_Available
(P
);
8480 if Present
(AS
) and then Is_Constrained
(AS
) then
8483 -- If we have an unconstrained type we cannot fold
8491 elsif Id
= Attribute_Size
then
8492 -- For Enum_Lit'Size, use Enum_Type'Object_Size. Taking the 'Size
8493 -- of a literal is kind of a strange thing to do, so we don't want
8494 -- to pass this oddity on to the back end. Note that Etype of an
8495 -- enumeration literal is always a (base) type, never a
8496 -- constrained subtype, so the Esize is always known.
8498 if Is_Entity_Name
(P
)
8499 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
8501 pragma Assert
(Known_Static_Esize
(Etype
(P
)));
8502 Compile_Time_Known_Attribute
(N
, Esize
(Etype
(P
)));
8504 -- Otherwise, if Size is available, use that
8506 elsif Is_Entity_Name
(P
) and then Known_Static_Esize
(Entity
(P
))
8508 Compile_Time_Known_Attribute
(N
, Esize
(Entity
(P
)));
8510 -- Otherwise, we cannot fold
8523 -- Cases where P is not an object. Cannot do anything if P is not the
8524 -- name of an entity.
8526 elsif not Is_Entity_Name
(P
) then
8530 -- Otherwise get prefix entity
8533 P_Entity
:= Entity
(P
);
8536 -- If we are asked to evaluate an attribute where the prefix is a
8537 -- non-frozen generic actual type whose RM_Size has not been set,
8538 -- then abandon the effort.
8540 if Is_Type
(P_Entity
)
8541 and then (not Is_Frozen
(P_Entity
)
8542 and then Is_Generic_Actual_Type
(P_Entity
)
8543 and then not Known_RM_Size
(P_Entity
))
8545 -- However, the attribute Unconstrained_Array must be evaluated,
8546 -- since it is documented to be a static attribute (and can for
8547 -- example appear in a Compile_Time_Warning pragma). The frozen
8548 -- status of the type does not affect its evaluation. Likewise
8549 -- for attributes intended to be used with generic definitions.
8551 and then Id
not in Attribute_Unconstrained_Array
8552 | Attribute_Has_Access_Values
8553 | Attribute_Has_Discriminants
8554 | Attribute_Has_Tagged_Values
8559 -- At this stage P_Entity is the entity to which the attribute
8560 -- is to be applied. This is usually simply the entity of the
8561 -- prefix, except in some cases of attributes for objects, where
8562 -- as described above, we apply the attribute to the object type.
8564 -- Here is where we make sure that static attributes are properly
8565 -- marked as such. These are attributes whose prefix is a static
8566 -- scalar subtype, whose result is scalar, and whose arguments, if
8567 -- present, are static scalar expressions. Note that such references
8568 -- are static expressions even if they raise Constraint_Error.
8570 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
8571 -- though evaluating it raises constraint error. This means that a
8572 -- declaration like:
8574 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
8576 -- is legal, since here this expression appears in a statically
8577 -- unevaluated position, so it does not actually raise an exception.
8579 -- T'Descriptor_Size is never static, even if T is static.
8581 if Is_Scalar_Type
(P_Entity
)
8582 and then not Is_Generic_Type
(P_Entity
)
8583 and then Is_Static_Subtype
(P_Entity
)
8584 and then Is_Scalar_Type
(Etype
(N
))
8587 or else (Is_Static_Expression
(E1
)
8588 and then Is_Scalar_Type
(Etype
(E1
))))
8591 or else (Is_Static_Expression
(E2
)
8592 and then Is_Scalar_Type
(Etype
(E1
))))
8593 and then Id
/= Attribute_Descriptor_Size
8595 -- If the front-end conjures up Integer'Pred (Integer'First)
8596 -- as the high bound of a null array aggregate, then we don't
8597 -- want to reject that as an illegal static expression.
8599 and then not Is_Null_Array_Aggregate_High_Bound
(N
)
8602 Set_Is_Static_Expression
(N
, True);
8605 -- First foldable possibility is a scalar or array type (RM 4.9(7))
8606 -- that is not generic (generic types are eliminated by RM 4.9(25)).
8607 -- Note we allow nonstatic nongeneric types at this stage as further
8610 if Is_Type
(P_Entity
)
8611 and then (Is_Scalar_Type
(P_Entity
) or Is_Array_Type
(P_Entity
))
8612 and then not Is_Generic_Type
(P_Entity
)
8616 -- Second foldable possibility is an array object (RM 4.9(8))
8618 elsif Ekind
(P_Entity
) in E_Variable | E_Constant
8619 and then Is_Array_Type
(Etype
(P_Entity
))
8620 and then not Is_Generic_Type
(Etype
(P_Entity
))
8622 P_Type
:= Etype
(P_Entity
);
8624 -- If the entity is an array constant with an unconstrained nominal
8625 -- subtype then get the type from the initial value. If the value has
8626 -- been expanded into assignments, there is no expression and the
8627 -- attribute reference remains dynamic.
8629 -- We could do better here and retrieve the type ???
8631 if Ekind
(P_Entity
) = E_Constant
8632 and then not Is_Constrained
(P_Type
)
8634 if No
(Constant_Value
(P_Entity
)) then
8637 P_Type
:= Etype
(Constant_Value
(P_Entity
));
8641 -- Definite must be folded if the prefix is not a generic type, that
8642 -- is to say if we are within an instantiation. Same processing applies
8643 -- to selected GNAT attributes.
8645 elsif (Id
= Attribute_Atomic_Always_Lock_Free
or else
8646 Id
= Attribute_Definite
or else
8647 Id
= Attribute_Descriptor_Size
or else
8648 Id
= Attribute_Has_Access_Values
or else
8649 Id
= Attribute_Has_Discriminants
or else
8650 Id
= Attribute_Has_Tagged_Values
or else
8651 Id
= Attribute_Preelaborable_Initialization
or else
8652 Id
= Attribute_Type_Class
or else
8653 Id
= Attribute_Unconstrained_Array
or else
8654 Id
= Attribute_Max_Alignment_For_Allocation
)
8655 and then not Is_Generic_Type
(P_Entity
)
8659 -- We can fold 'Size applied to a type if the size is known (as happens
8660 -- for a size from an attribute definition clause). At this stage, this
8661 -- can happen only for types (e.g. record types) for which the size is
8662 -- always non-static. We exclude generic types from consideration (since
8663 -- they have bogus sizes set within templates).
8665 elsif Id
= Attribute_Size
8666 and then Is_Type
(P_Entity
)
8667 and then not Is_Generic_Type
(P_Entity
)
8668 and then Known_Static_RM_Size
(Full_Type
(P_Entity
))
8670 Compile_Time_Known_Attribute
(N
, RM_Size
(Full_Type
(P_Entity
)));
8673 -- We can also fold 'Object_Size applied to a type if the object size is
8674 -- known (as happens for a size from an attribute definition clause). At
8675 -- this stage, this can happen only for types (e.g. record types) for
8676 -- which the size is always non-static. We exclude generic types from
8677 -- consideration (since they have bogus sizes set within templates).
8678 -- We can also fold Max_Size_In_Storage_Elements in the same cases.
8680 elsif (Id
= Attribute_Object_Size
or
8681 Id
= Attribute_Max_Size_In_Storage_Elements
)
8682 and then Is_Type
(P_Entity
)
8683 and then not Is_Generic_Type
(P_Entity
)
8684 and then Known_Static_Esize
(Full_Type
(P_Entity
))
8687 Attr_Value
: Uint
:= Esize
(Full_Type
(P_Entity
));
8690 if Id
= Attribute_Max_Size_In_Storage_Elements
then
8691 Attr_Value
:= (Attr_Value
+ System_Storage_Unit
- 1) /
8692 System_Storage_Unit
;
8694 Compile_Time_Known_Attribute
(N
, Attr_Value
);
8698 -- We can fold 'Alignment applied to a type if the alignment is known
8699 -- (as happens for an alignment from an attribute definition clause).
8700 -- At this stage, this can happen only for types (e.g. record types) for
8701 -- which the size is always non-static. We exclude generic types from
8702 -- consideration (since they have bogus sizes set within templates).
8704 elsif Id
= Attribute_Alignment
8705 and then Is_Type
(P_Entity
)
8706 and then not Is_Generic_Type
(P_Entity
)
8707 and then Known_Alignment
(Full_Type
(P_Entity
))
8709 Compile_Time_Known_Attribute
(N
, Alignment
(Full_Type
(P_Entity
)));
8712 -- If this is an access attribute that is known to fail accessibility
8713 -- check, rewrite accordingly.
8715 elsif Id
= Attribute_Address
8716 and then Raises_Constraint_Error
(N
)
8719 Make_Raise_Program_Error
(Loc
,
8720 Reason
=> PE_Accessibility_Check_Failed
));
8721 Set_Etype
(N
, C_Type
);
8724 -- No other cases are foldable (they certainly aren't static, and at
8725 -- the moment we don't try to fold any cases other than the ones above).
8732 -- If either attribute or the prefix is Any_Type, then propagate
8733 -- Any_Type to the result and don't do anything else at all.
8735 if P_Type
= Any_Type
8736 or else (Present
(E1
) and then Etype
(E1
) = Any_Type
)
8737 or else (Present
(E2
) and then Etype
(E2
) = Any_Type
)
8739 Set_Etype
(N
, Any_Type
);
8743 -- Scalar subtype case. We have not yet enforced the static requirement
8744 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
8745 -- of non-static attribute references (e.g. S'Digits for a non-static
8746 -- floating-point type, which we can compute at compile time).
8748 -- Note: this folding of non-static attributes is not simply a case of
8749 -- optimization. For many of the attributes affected, Gigi cannot handle
8750 -- the attribute and depends on the front end having folded them away.
8752 -- Note: although we don't require staticness at this stage, we do set
8753 -- the Static variable to record the staticness, for easy reference by
8754 -- those attributes where it matters (e.g. Succ and Pred), and also to
8755 -- be used to ensure that non-static folded things are not marked as
8756 -- being static (a check that is done right at the end).
8758 P_Root_Type
:= Root_Type
(P_Type
);
8759 P_Base_Type
:= Base_Type
(P_Type
);
8761 -- If the root type or base type is generic, then we cannot fold. This
8762 -- test is needed because subtypes of generic types are not always
8763 -- marked as being generic themselves (which seems odd???)
8765 -- Should this situation be addressed instead by either
8766 -- a) setting Is_Generic_Type in more cases
8767 -- or b) replacing preceding calls to Is_Generic_Type with calls to
8768 -- Sem_Util.Some_New_Function
8769 -- so that we wouldn't have to deal with these cases here ???
8771 if Is_Generic_Type
(P_Root_Type
)
8772 or else Is_Generic_Type
(P_Base_Type
)
8773 or else (Present
(Associated_Node_For_Itype
(P_Base_Type
))
8774 and then Is_Generic_Type
(Defining_Identifier
8775 (Associated_Node_For_Itype
(P_Base_Type
))))
8777 Set_Is_Static_Expression
(N
, False);
8781 if Is_Scalar_Type
(P_Type
) then
8782 if not Is_Static_Subtype
(P_Type
) then
8784 Set_Is_Static_Expression
(N
, False);
8785 elsif not Is_OK_Static_Subtype
(P_Type
) then
8786 Set_Raises_Constraint_Error
(N
);
8789 -- RM 13.14(8/4): a nonstatic expression in a spec expression does
8790 -- not cause freezing, so the representation attributes cannot be
8791 -- evaluated at this point if the type is not already frozen.
8794 and then In_Spec_Expression
8795 and then Id
in Attribute_Alignment
8796 | Attribute_Component_Size
8797 | Attribute_Max_Alignment_For_Allocation
8798 | Attribute_Max_Size_In_Storage_Elements
8799 | Attribute_Object_Size
8802 | Attribute_VADS_Size
8803 | Attribute_Value_Size
8804 and then not Is_Frozen
(P_Type
)
8809 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8810 -- since we can't do anything with unconstrained arrays. In addition,
8811 -- only the First, Last and Length attributes are possibly static.
8813 -- Atomic_Always_Lock_Free, Definite, Descriptor_Size, Has_Access_Values
8814 -- Has_Discriminants, Has_Tagged_Values, Type_Class, and
8815 -- Unconstrained_Array are again exceptions, because they apply as well
8816 -- to unconstrained types.
8818 -- Folding can also be done for Preelaborable_Initialization based on
8819 -- whether the prefix type has preelaborable initialization, even though
8820 -- the attribute is nonstatic.
8822 -- In addition Component_Size is an exception since it is possibly
8823 -- foldable, even though it is never static, and it does apply to
8824 -- unconstrained arrays. Furthermore, it is essential to fold this
8825 -- in the packed case, since otherwise the value will be incorrect.
8826 -- Moreover, the exact same reasoning can be applied to Alignment.
8828 elsif Id
= Attribute_Atomic_Always_Lock_Free
or else
8829 Id
= Attribute_Definite
or else
8830 Id
= Attribute_Descriptor_Size
or else
8831 Id
= Attribute_Has_Access_Values
or else
8832 Id
= Attribute_Has_Discriminants
or else
8833 Id
= Attribute_Has_Tagged_Values
or else
8834 Id
= Attribute_Preelaborable_Initialization
or else
8835 Id
= Attribute_Type_Class
or else
8836 Id
= Attribute_Unconstrained_Array
or else
8837 Id
= Attribute_Component_Size
or else
8838 Id
= Attribute_Alignment
8841 Set_Is_Static_Expression
(N
, False);
8843 elsif Id
/= Attribute_Max_Alignment_For_Allocation
then
8844 if not Is_Constrained
(P_Type
)
8845 or else (Id
/= Attribute_First
and then
8846 Id
/= Attribute_Last
and then
8847 Id
/= Attribute_Length
)
8853 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8854 -- scalar case, we hold off on enforcing staticness, since there are
8855 -- cases which we can fold at compile time even though they are not
8856 -- static (e.g. 'Length applied to a static index, even though other
8857 -- non-static indexes make the array type non-static). This is only
8858 -- an optimization, but it falls out essentially free, so why not.
8859 -- Again we compute the variable Static for easy reference later
8860 -- (note that no array attributes are static in Ada 83).
8862 -- We also need to set Static properly for subsequent legality checks
8863 -- which might otherwise accept non-static constants in contexts
8864 -- where they are not legal.
8867 Ada_Version
>= Ada_95
and then Statically_Denotes_Entity
(P
);
8868 Set_Is_Static_Expression
(N
, Static
);
8874 Nod
:= First_Index
(P_Type
);
8876 -- The expression is static if the array type is constrained
8877 -- by given bounds, and not by an initial expression. Constant
8878 -- strings are static in any case.
8880 if Root_Type
(P_Type
) /= Standard_String
then
8882 Static
and then not Is_Constr_Subt_For_U_Nominal
(P_Type
);
8883 Set_Is_Static_Expression
(N
, Static
);
8886 while Present
(Nod
) loop
8887 if not Is_Static_Subtype
(Etype
(Nod
)) then
8889 Set_Is_Static_Expression
(N
, False);
8891 elsif not Is_OK_Static_Subtype
(Etype
(Nod
)) then
8892 Set_Raises_Constraint_Error
(N
);
8894 Set_Is_Static_Expression
(N
, False);
8897 -- If however the index type is generic, or derived from
8898 -- one, attributes cannot be folded.
8900 if Is_Generic_Type
(Root_Type
(Etype
(Nod
)))
8901 and then Id
/= Attribute_Component_Size
8911 -- Check any expressions that are present. Note that these expressions,
8912 -- depending on the particular attribute type, are either part of the
8913 -- attribute designator, or they are arguments in a case where the
8914 -- attribute reference returns a function. In the latter case, the
8915 -- rule in (RM 4.9(22)) applies and in particular requires the type
8916 -- of the expressions to be scalar in order for the attribute to be
8917 -- considered to be static.
8925 while Present
(E
) loop
8927 -- If expression is not static, then the attribute reference
8928 -- result certainly cannot be static.
8930 if not Is_Static_Expression
(E
) then
8932 Set_Is_Static_Expression
(N
, False);
8935 if Raises_Constraint_Error
(E
) then
8936 Set_Raises_Constraint_Error
(N
);
8939 -- If the result is not known at compile time, or is not of
8940 -- a scalar type, then the result is definitely not static,
8941 -- so we can quit now.
8943 if not Compile_Time_Known_Value
(E
)
8944 or else not Is_Scalar_Type
(Etype
(E
))
8949 -- If the expression raises a constraint error, then so does
8950 -- the attribute reference. We keep going in this case because
8951 -- we are still interested in whether the attribute reference
8952 -- is static even if it is not static.
8954 elsif Raises_Constraint_Error
(E
) then
8955 Set_Raises_Constraint_Error
(N
);
8961 if Raises_Constraint_Error
(Prefix
(N
)) then
8962 Set_Is_Static_Expression
(N
, False);
8967 -- Deal with the case of a static attribute reference that raises
8968 -- constraint error. The Raises_Constraint_Error flag will already
8969 -- have been set, and the Static flag shows whether the attribute
8970 -- reference is static. In any case we certainly can't fold such an
8971 -- attribute reference.
8973 -- Note that the rewriting of the attribute node with the constraint
8974 -- error node is essential in this case, because otherwise Gigi might
8975 -- blow up on one of the attributes it never expects to see.
8977 -- The constraint_error node must have the type imposed by the context,
8978 -- to avoid spurious errors in the enclosing expression.
8980 if Raises_Constraint_Error
(N
) then
8982 Make_Raise_Constraint_Error
(Sloc
(N
),
8983 Reason
=> CE_Range_Check_Failed
);
8984 Set_Etype
(CE_Node
, Etype
(N
));
8985 Set_Raises_Constraint_Error
(CE_Node
);
8987 Rewrite
(N
, Relocate_Node
(CE_Node
));
8988 Set_Raises_Constraint_Error
(N
, True);
8992 -- At this point we have a potentially foldable attribute reference.
8993 -- If Static is set, then the attribute reference definitely obeys
8994 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8995 -- folded. If Static is not set, then the attribute may or may not
8996 -- be foldable, and the individual attribute processing routines
8997 -- test Static as required in cases where it makes a difference.
8999 -- In the case where Static is not set, we do know that all the
9000 -- expressions present are at least known at compile time (we assumed
9001 -- above that if this was not the case, then there was no hope of static
9002 -- evaluation). However, we did not require that the bounds of the
9003 -- prefix type be compile time known, let alone static). That's because
9004 -- there are many attributes that can be computed at compile time on
9005 -- non-static subtypes, even though such references are not static
9008 -- For VAX float, the root type is an IEEE type. So make sure to use the
9009 -- base type instead of the root-type for floating point attributes.
9013 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
9016 when Attribute_Constant_Indexing
9017 | Attribute_Default_Iterator
9018 | Attribute_Implicit_Dereference
9019 | Attribute_Iterator_Element
9020 | Attribute_Iterable
9022 | Attribute_Variable_Indexing
9026 -- Internal attributes used to deal with Ada 2012 delayed aspects.
9027 -- These were already rejected by the parser. Thus they shouldn't
9030 when Internal_Attribute_Id
=>
9031 raise Program_Error
;
9037 when Attribute_Adjacent
=>
9041 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
9048 when Attribute_Aft
=>
9049 Fold_Uint
(N
, Aft_Value
(P_Type
), Static
);
9055 when Attribute_Alignment
=> Alignment_Block
: declare
9056 P_TypeA
: constant Entity_Id
:= Full_Type
(P_Type
);
9059 -- Fold if alignment is set and not otherwise
9061 if Known_Alignment
(P_TypeA
) then
9062 Fold_Uint
(N
, Alignment
(P_TypeA
), Static
);
9064 end Alignment_Block
;
9066 -----------------------------
9067 -- Atomic_Always_Lock_Free --
9068 -----------------------------
9070 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
9073 when Attribute_Atomic_Always_Lock_Free
=> Atomic_Always_Lock_Free
:
9075 V
: constant Entity_Id
:=
9077 (Support_Atomic_Primitives_On_Target
9078 and then Support_Atomic_Primitives
(P_Type
));
9081 Rewrite
(N
, New_Occurrence_Of
(V
, Loc
));
9083 -- Analyze and resolve as boolean. Note that this attribute is a
9084 -- static attribute in GNAT.
9086 Analyze_And_Resolve
(N
, Standard_Boolean
);
9088 Set_Is_Static_Expression
(N
);
9089 end Atomic_Always_Lock_Free
;
9095 -- Bit can never be folded
9097 when Attribute_Bit
=>
9104 -- Body_version can never be static
9106 when Attribute_Body_Version
=>
9113 when Attribute_Ceiling
=>
9115 (N
, Eval_Fat
.Ceiling
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9117 --------------------
9118 -- Component_Size --
9119 --------------------
9121 -- Fold Component_Size if it is known at compile time, which is always
9122 -- true in the packed array case. It is important that the packed array
9123 -- case is handled here since the back end would otherwise get confused
9124 -- by the equivalent packed array type.
9126 when Attribute_Component_Size
=>
9127 if Known_Static_Component_Size
(P_Type
) then
9128 Fold_Uint
(N
, Component_Size
(P_Type
), Static
);
9135 when Attribute_Compose
=>
9138 Eval_Fat
.Compose
(P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
9145 -- Constrained is never folded for now, there may be cases that
9146 -- could be handled at compile time. To be looked at later.
9148 when Attribute_Constrained
=>
9150 -- The expander might fold it and set the static flag accordingly,
9151 -- but with expansion disabled, it remains as an attribute reference,
9152 -- and this reference is not static.
9154 Set_Is_Static_Expression
(N
, False);
9160 when Attribute_Copy_Sign
=>
9164 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
9171 when Attribute_Definite
=>
9172 Rewrite
(N
, New_Occurrence_Of
(
9173 Boolean_Literals
(Is_Definite_Subtype
(P_Entity
)), Loc
));
9174 Analyze_And_Resolve
(N
, Standard_Boolean
);
9180 when Attribute_Delta
=>
9181 Fold_Ureal
(N
, Delta_Value
(P_Type
), True);
9187 when Attribute_Denorm
=>
9189 (N
, UI_From_Int
(Boolean'Pos (Has_Denormals
(P_Type
))), Static
);
9191 ---------------------
9192 -- Descriptor_Size --
9193 ---------------------
9195 -- Descriptor_Size is nonnull only for unconstrained array types
9197 when Attribute_Descriptor_Size
=>
9198 if not Is_Array_Type
(P_Type
) or else Is_Constrained
(P_Type
) then
9199 Fold_Uint
(N
, Uint_0
, Static
);
9206 when Attribute_Digits
=>
9207 Fold_Uint
(N
, Digits_Value
(P_Type
), Static
);
9213 when Attribute_Emax
=>
9215 -- Ada 83 attribute is defined as (RM83 3.5.8)
9217 -- T'Emax = 4 * T'Mantissa
9219 Fold_Uint
(N
, 4 * Mantissa
, Static
);
9225 when Attribute_Enum_Rep
=> Enum_Rep
: declare
9229 -- The attribute appears in the form:
9231 -- Enum_Typ'Enum_Rep (Const)
9232 -- Enum_Typ'Enum_Rep (Enum_Lit)
9234 if Present
(E1
) then
9237 -- Otherwise the prefix denotes a constant or enumeration literal:
9240 -- Enum_Lit'Enum_Rep
9246 -- For an enumeration type with a non-standard representation use
9247 -- the Enumeration_Rep field of the proper constant. Note that this
9248 -- will not work for types Character/Wide_[Wide-]Character, since no
9249 -- real entities are created for the enumeration literals, but that
9250 -- does not matter since these two types do not have non-standard
9251 -- representations anyway.
9253 if Is_Enumeration_Type
(P_Type
)
9254 and then Has_Non_Standard_Rep
(P_Type
)
9256 Fold_Uint
(N
, Enumeration_Rep
(Expr_Value_E
(Val
)), Static
);
9258 -- For enumeration types with standard representations and all other
9259 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
9263 Fold_Uint
(N
, Expr_Value
(Val
), Static
);
9271 when Attribute_Enum_Val
=> Enum_Val
: declare
9275 -- We have something like Enum_Type'Enum_Val (23), so search for a
9276 -- corresponding value in the list of Enum_Rep values for the type.
9278 Lit
:= First_Literal
(P_Base_Type
);
9280 if Enumeration_Rep
(Lit
) = Expr_Value
(E1
) then
9281 Fold_Uint
(N
, Enumeration_Pos
(Lit
), Static
);
9288 Apply_Compile_Time_Constraint_Error
9289 (N
, "no representation value matches",
9290 CE_Range_Check_Failed
,
9291 Warn
=> not Static
);
9301 when Attribute_Epsilon
=>
9303 -- Ada 83 attribute is defined as (RM83 3.5.8)
9305 -- T'Epsilon = 2.0**(1 - T'Mantissa)
9307 Fold_Ureal
(N
, Ureal_2
** (1 - Mantissa
), True);
9313 when Attribute_Exponent
=>
9315 Eval_Fat
.Exponent
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9317 -----------------------
9318 -- Finalization_Size --
9319 -----------------------
9321 when Attribute_Finalization_Size
=>
9328 when Attribute_First
=>
9331 if Compile_Time_Known_Value
(Lo_Bound
) then
9332 if Is_Real_Type
(P_Type
) then
9333 Fold_Ureal
(N
, Expr_Value_R
(Lo_Bound
), Static
);
9335 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
9339 Check_Concurrent_Discriminant
(Lo_Bound
);
9346 when Attribute_First_Valid
=>
9347 if Has_Predicates
(P_Type
)
9348 and then Has_Static_Predicate
(P_Type
)
9351 FirstN
: constant Node_Id
:=
9352 First
(Static_Discrete_Predicate
(P_Type
));
9354 if Nkind
(FirstN
) = N_Range
then
9355 Fold_Uint
(N
, Expr_Value
(Low_Bound
(FirstN
)), Static
);
9357 Fold_Uint
(N
, Expr_Value
(FirstN
), Static
);
9363 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
9370 when Attribute_Fixed_Value
=>
9377 when Attribute_Floor
=>
9379 (N
, Eval_Fat
.Floor
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9385 when Attribute_Fore
=>
9386 if Compile_Time_Known_Bounds
(P_Type
) then
9387 Fold_Uint
(N
, UI_From_Int
(Fore_Value
), Static
);
9394 when Attribute_Fraction
=>
9396 (N
, Eval_Fat
.Fraction
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9398 -----------------------
9399 -- Has_Access_Values --
9400 -----------------------
9402 when Attribute_Has_Access_Values
=>
9403 Rewrite
(N
, New_Occurrence_Of
9404 (Boolean_Literals
(Has_Access_Values
(P_Root_Type
)), Loc
));
9405 Analyze_And_Resolve
(N
, Standard_Boolean
);
9407 -----------------------
9408 -- Has_Discriminants --
9409 -----------------------
9411 when Attribute_Has_Discriminants
=>
9412 Rewrite
(N
, New_Occurrence_Of
(
9413 Boolean_Literals
(Has_Discriminants
(P_Entity
)), Loc
));
9414 Analyze_And_Resolve
(N
, Standard_Boolean
);
9416 ----------------------
9417 -- Has_Same_Storage --
9418 ----------------------
9420 when Attribute_Has_Same_Storage
=>
9423 -----------------------
9424 -- Has_Tagged_Values --
9425 -----------------------
9427 when Attribute_Has_Tagged_Values
=>
9428 Rewrite
(N
, New_Occurrence_Of
9429 (Boolean_Literals
(Has_Tagged_Component
(P_Root_Type
)), Loc
));
9430 Analyze_And_Resolve
(N
, Standard_Boolean
);
9436 when Attribute_Identity
=>
9443 -- Image is a scalar attribute, but is never static, because it is
9444 -- not a static function (having a non-scalar argument (RM 4.9(22))
9445 -- However, we can constant-fold the image of an enumeration literal
9446 -- if names are available and default Image implementation has not
9449 when Attribute_Image
=>
9450 if Is_Entity_Name
(E1
)
9451 and then Ekind
(Entity
(E1
)) = E_Enumeration_Literal
9452 and then not Discard_Names
(First_Subtype
(Etype
(E1
)))
9453 and then not Global_Discard_Names
9454 and then not Has_Aspect
(Etype
(E1
), Aspect_Put_Image
)
9457 Lit
: constant Entity_Id
:= Entity
(E1
);
9461 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
9462 Set_Casing
(All_Upper_Case
);
9463 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
9465 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
9466 Analyze_And_Resolve
(N
, Standard_String
);
9467 Set_Is_Static_Expression
(N
, False);
9475 -- We never try to fold Integer_Value (though perhaps we could???)
9477 when Attribute_Integer_Value
=>
9484 -- Invalid_Value is a scalar attribute that is never static, because
9485 -- the value is by design out of range.
9487 when Attribute_Invalid_Value
=>
9494 when Attribute_Large
=>
9496 -- For fixed-point, we use the identity:
9498 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
9500 if Is_Fixed_Point_Type
(P_Type
) then
9502 Make_Op_Multiply
(Loc
,
9504 Make_Op_Subtract
(Loc
,
9508 Make_Real_Literal
(Loc
, Ureal_2
),
9510 Make_Attribute_Reference
(Loc
,
9512 Attribute_Name
=> Name_Mantissa
)),
9513 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_1
)),
9516 Make_Real_Literal
(Loc
, Small_Value
(Entity
(P
)))));
9518 Analyze_And_Resolve
(N
, C_Type
);
9520 -- Floating-point (Ada 83 compatibility)
9523 -- Ada 83 attribute is defined as (RM83 3.5.8)
9525 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
9529 -- T'Emax = 4 * T'Mantissa
9533 Ureal_2
** (4 * Mantissa
) * (Ureal_1
- Ureal_2
** (-Mantissa
)),
9541 when Attribute_Last
=>
9544 if Compile_Time_Known_Value
(Hi_Bound
) then
9545 if Is_Real_Type
(P_Type
) then
9546 Fold_Ureal
(N
, Expr_Value_R
(Hi_Bound
), Static
);
9548 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
9552 Check_Concurrent_Discriminant
(Hi_Bound
);
9559 when Attribute_Last_Valid
=>
9560 if Has_Predicates
(P_Type
)
9561 and then Has_Static_Predicate
(P_Type
)
9564 LastN
: constant Node_Id
:=
9565 Last
(Static_Discrete_Predicate
(P_Type
));
9567 if Nkind
(LastN
) = N_Range
then
9568 Fold_Uint
(N
, Expr_Value
(High_Bound
(LastN
)), Static
);
9570 Fold_Uint
(N
, Expr_Value
(LastN
), Static
);
9576 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
9583 when Attribute_Leading_Part
=> Leading_Part
: declare
9584 Radix_Digits
: constant Uint
:= Expr_Value
(E2
);
9587 if UI_Le
(Radix_Digits
, Uint_0
) then
9588 Apply_Compile_Time_Constraint_Error
9589 (N
, "Radix_Digits in Leading_Part is zero or negative",
9591 Warn
=> not Static
);
9599 Eval_Fat
.Leading_Part
9600 (P_Base_Type
, Expr_Value_R
(E1
), Radix_Digits
),
9608 when Attribute_Length
=> Length
: declare
9612 -- If any index type is a formal type, or derived from one, the
9613 -- bounds are not static. Treating them as static can produce
9614 -- spurious warnings or improper constant folding.
9616 Ind
:= First_Index
(P_Type
);
9617 while Present
(Ind
) loop
9618 if Is_Generic_Type
(Root_Type
(Etype
(Ind
))) then
9627 -- For two compile time values, we can compute length
9629 if Compile_Time_Known_Value
(Lo_Bound
)
9630 and then Compile_Time_Known_Value
(Hi_Bound
)
9633 UI_Max
(0, 1 + (Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
))),
9637 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9638 -- comparable, and we can figure out the difference between them.
9641 Diff
: aliased Uint
;
9645 Compile_Time_Compare
9646 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
9649 Fold_Uint
(N
, Uint_1
, Static
);
9652 Fold_Uint
(N
, Uint_0
, Static
);
9655 if Present
(Diff
) then
9656 Fold_Uint
(N
, Diff
+ 1, Static
);
9669 -- Loop_Entry acts as an alias of a constant initialized to the prefix
9670 -- of the said attribute at the point of entry into the related loop. As
9671 -- such, the attribute reference does not need to be evaluated because
9672 -- the prefix is the one that is evaluted.
9674 when Attribute_Loop_Entry
=>
9681 -- We use the same rounding as the one used for RM 4.9(38/2)
9683 when Attribute_Machine
=>
9685 (N
, Machine_Number
(P_Base_Type
, Expr_Value_R
(E1
), N
), Static
);
9686 Set_Is_Machine_Number
(N
);
9692 when Attribute_Machine_Emax
=>
9693 Fold_Uint
(N
, Machine_Emax_Value
(P_Type
), Static
);
9699 when Attribute_Machine_Emin
=>
9700 Fold_Uint
(N
, Machine_Emin_Value
(P_Type
), Static
);
9702 ----------------------
9703 -- Machine_Mantissa --
9704 ----------------------
9706 when Attribute_Machine_Mantissa
=>
9707 Fold_Uint
(N
, Machine_Mantissa_Value
(P_Type
), Static
);
9709 -----------------------
9710 -- Machine_Overflows --
9711 -----------------------
9713 when Attribute_Machine_Overflows
=>
9715 -- Always true for fixed-point
9717 if Is_Fixed_Point_Type
(P_Type
) then
9718 Fold_Uint
(N
, True_Value
, Static
);
9720 -- Floating point case
9724 UI_From_Int
(Boolean'Pos (Machine_Overflows_On_Target
)),
9732 when Attribute_Machine_Radix
=>
9733 if Is_Fixed_Point_Type
(P_Type
) then
9734 if Is_Decimal_Fixed_Point_Type
(P_Type
)
9735 and then Machine_Radix_10
(P_Type
)
9737 Fold_Uint
(N
, Uint_10
, Static
);
9739 Fold_Uint
(N
, Uint_2
, Static
);
9742 -- All floating-point type always have radix 2
9745 Fold_Uint
(N
, Uint_2
, Static
);
9748 ----------------------
9749 -- Machine_Rounding --
9750 ----------------------
9752 -- Note: for the folding case, it is fine to treat Machine_Rounding
9753 -- exactly the same way as Rounding, since this is one of the allowed
9754 -- behaviors, and performance is not an issue here. It might be a bit
9755 -- better to give the same result as it would give at run time, even
9756 -- though the non-determinism is certainly permitted.
9758 when Attribute_Machine_Rounding
=>
9760 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9762 --------------------
9763 -- Machine_Rounds --
9764 --------------------
9766 when Attribute_Machine_Rounds
=>
9768 -- Always False for fixed-point
9770 if Is_Fixed_Point_Type
(P_Type
) then
9771 Fold_Uint
(N
, False_Value
, Static
);
9773 -- Else yield proper floating-point result
9777 (N
, UI_From_Int
(Boolean'Pos (Machine_Rounds_On_Target
)),
9785 -- Note: Machine_Size is identical to Object_Size
9787 when Attribute_Machine_Size
=> Machine_Size
: declare
9788 P_TypeA
: constant Entity_Id
:= Full_Type
(P_Type
);
9791 if Known_Esize
(P_TypeA
) then
9792 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9800 when Attribute_Mantissa
=>
9802 -- Fixed-point mantissa
9804 if Is_Fixed_Point_Type
(P_Type
) then
9806 -- Compile time foldable case
9808 if Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
9810 Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
9812 -- The calculation of the obsolete Ada 83 attribute Mantissa
9813 -- is annoying, because of AI00143, quoted here:
9815 -- !question 84-01-10
9817 -- Consider the model numbers for F:
9819 -- type F is delta 1.0 range -7.0 .. 8.0;
9821 -- The wording requires that F'MANTISSA be the SMALLEST
9822 -- integer number for which each bound of the specified
9823 -- range is either a model number or lies at most small
9824 -- distant from a model number. This means F'MANTISSA
9825 -- is required to be 3 since the range -7.0 .. 7.0 fits
9826 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9827 -- number, namely, 7. Is this analysis correct? Note that
9828 -- this implies the upper bound of the range is not
9829 -- represented as a model number.
9831 -- !response 84-03-17
9833 -- The analysis is correct. The upper and lower bounds for
9834 -- a fixed point type can lie outside the range of model
9845 LBound
:= Expr_Value_R
(Type_Low_Bound
(P_Type
));
9846 UBound
:= Expr_Value_R
(Type_High_Bound
(P_Type
));
9847 Bound
:= UR_Max
(UR_Abs
(LBound
), UR_Abs
(UBound
));
9848 Max_Man
:= UR_Trunc
(Bound
/ Small_Value
(P_Type
));
9850 -- If the Bound is exactly a model number, i.e. a multiple
9851 -- of Small, then we back it off by one to get the integer
9852 -- value that must be representable.
9854 if Small_Value
(P_Type
) * Max_Man
= Bound
then
9855 Max_Man
:= Max_Man
- 1;
9858 -- Now find corresponding size = Mantissa value
9861 while 2 ** Siz
< Max_Man
loop
9865 Fold_Uint
(N
, Siz
, Static
);
9869 -- The case of dynamic bounds cannot be evaluated at compile
9870 -- time. Instead we use a runtime routine (see Exp_Attr).
9875 -- Floating-point Mantissa
9878 Fold_Uint
(N
, Mantissa
, Static
);
9885 when Attribute_Max
=>
9886 if Is_Real_Type
(P_Type
) then
9888 (N
, UR_Max
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
9890 Fold_Uint
(N
, UI_Max
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
9893 ----------------------------------
9894 -- Max_Alignment_For_Allocation --
9895 ----------------------------------
9897 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9898 -- arrays are allocated with dope, so we need to take into account both
9899 -- the alignment of the array, which comes from the component alignment,
9900 -- and the alignment of the dope. Also, if the alignment is unknown, we
9901 -- use the max (it's OK to be pessimistic).
9903 when Attribute_Max_Alignment_For_Allocation
=> Max_Align
: declare
9904 A
: Uint
:= UI_From_Int
(Ttypes
.Maximum_Alignment
);
9906 if Known_Alignment
(P_Type
)
9907 and then (not Is_Array_Type
(P_Type
) or else Alignment
(P_Type
) > A
)
9909 A
:= Alignment
(P_Type
);
9912 Fold_Uint
(N
, A
, Static
);
9915 ----------------------------------
9916 -- Max_Size_In_Storage_Elements --
9917 ----------------------------------
9919 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9920 -- Storage_Unit boundary. We can fold any cases for which the size
9921 -- is known by the front end.
9923 when Attribute_Max_Size_In_Storage_Elements
=> Max_Size
: declare
9924 P_TypeA
: constant Entity_Id
:= Full_Type
(P_Type
);
9927 if Known_Esize
(P_TypeA
) then
9929 (Esize
(P_TypeA
) + System_Storage_Unit
- 1) /
9930 System_Storage_Unit
,
9935 --------------------
9936 -- Mechanism_Code --
9937 --------------------
9939 when Attribute_Mechanism_Code
=> Mechanism_Code
: declare
9941 Mech
: Mechanism_Type
;
9946 Mech
:= Mechanism
(P_Entity
);
9949 Val
:= UI_To_Int
(Expr_Value
(E1
));
9951 Formal
:= First_Formal
(P_Entity
);
9952 for J
in 1 .. Val
- 1 loop
9953 Next_Formal
(Formal
);
9956 Mech
:= Mechanism
(Formal
);
9960 Fold_Uint
(N
, UI_From_Int
(Int
(-Mech
)), Static
);
9968 when Attribute_Min
=>
9969 if Is_Real_Type
(P_Type
) then
9971 (N
, UR_Min
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
9974 (N
, UI_Min
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
9981 when Attribute_Mod
=>
9983 (N
, UI_Mod
(Expr_Value
(E1
), Modulus
(P_Base_Type
)), Static
);
9989 when Attribute_Model
=>
9991 (N
, Eval_Fat
.Model
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9997 when Attribute_Model_Emin
=>
9998 Fold_Uint
(N
, Model_Emin_Value
(P_Base_Type
), Static
);
10000 -------------------
10001 -- Model_Epsilon --
10002 -------------------
10004 when Attribute_Model_Epsilon
=>
10005 Fold_Ureal
(N
, Model_Epsilon_Value
(P_Base_Type
), Static
);
10007 --------------------
10008 -- Model_Mantissa --
10009 --------------------
10011 when Attribute_Model_Mantissa
=>
10012 Fold_Uint
(N
, Model_Mantissa_Value
(P_Base_Type
), Static
);
10018 when Attribute_Model_Small
=>
10019 Fold_Ureal
(N
, Model_Small_Value
(P_Base_Type
), Static
);
10025 when Attribute_Modulus
=>
10026 Fold_Uint
(N
, Modulus
(P_Type
), Static
);
10028 --------------------
10029 -- Null_Parameter --
10030 --------------------
10032 -- Cannot fold, we know the value sort of, but the whole point is
10033 -- that there is no way to talk about this imaginary value except
10034 -- by using the attribute, so we leave it the way it is.
10036 when Attribute_Null_Parameter
=>
10043 -- The Object_Size attribute for a type returns the Esize of the
10044 -- type and can be folded if this value is known.
10046 when Attribute_Object_Size
=> Object_Size
: declare
10047 P_TypeA
: constant Entity_Id
:= Full_Type
(P_Type
);
10050 if Known_Esize
(P_TypeA
) then
10051 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
10055 ----------------------
10056 -- Overlaps_Storage --
10057 ----------------------
10059 when Attribute_Overlaps_Storage
=>
10062 -------------------------
10063 -- Passed_By_Reference --
10064 -------------------------
10066 -- Scalar types are never passed by reference
10068 when Attribute_Passed_By_Reference
=>
10069 Fold_Uint
(N
, False_Value
, Static
);
10075 when Attribute_Pos
=>
10076 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
10082 when Attribute_Pred
=>
10084 -- Floating-point case
10086 if Is_Floating_Point_Type
(P_Type
) then
10088 (N
, Eval_Fat
.Pred
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
10090 -- Fixed-point case
10092 elsif Is_Fixed_Point_Type
(P_Type
) then
10094 (N
, Expr_Value_R
(E1
) - Small_Value
(P_Type
), True);
10096 -- Modular integer case (wraps)
10098 elsif Is_Modular_Integer_Type
(P_Type
) then
10099 Fold_Uint
(N
, (Expr_Value
(E1
) - 1) mod Modulus
(P_Type
), Static
);
10101 -- Other scalar cases
10104 pragma Assert
(Is_Scalar_Type
(P_Type
));
10106 if Is_Enumeration_Type
(P_Type
)
10107 and then Expr_Value
(E1
) =
10108 Expr_Value
(Type_Low_Bound
(P_Base_Type
))
10110 Apply_Compile_Time_Constraint_Error
10111 (N
, "Pred of `&''First`",
10112 CE_Overflow_Check_Failed
,
10113 Ent
=> P_Base_Type
,
10114 Warn
=> not Static
);
10119 -- Rewrite the FE-constructed high bound of a null array
10120 -- aggregate to raise CE.
10122 elsif Is_Signed_Integer_Type
(P_Type
)
10123 and then Expr_Value
(E1
) =
10124 Expr_Value
(Type_Low_Bound
(P_Base_Type
))
10125 and then Is_Null_Array_Aggregate_High_Bound
(N
)
10127 Apply_Compile_Time_Constraint_Error
10128 (N
, "Pred of `&''First`",
10129 CE_Overflow_Check_Failed
,
10130 Ent
=> P_Base_Type
,
10133 Rewrite
(N
, Make_Raise_Constraint_Error
(Sloc
(N
),
10134 Reason
=> CE_Overflow_Check_Failed
));
10135 Set_Etype
(N
, P_Base_Type
);
10139 Fold_Uint
(N
, Expr_Value
(E1
) - 1, Static
);
10142 ----------------------------------
10143 -- Preelaborable_Initialization --
10144 ----------------------------------
10146 when Attribute_Preelaborable_Initialization
=>
10150 (Boolean'Pos (Has_Preelaborable_Initialization
(P_Type
))),
10157 -- No processing required, because by this stage, Range has been
10158 -- replaced by First .. Last, so this branch can never be taken.
10160 when Attribute_Range
=>
10161 raise Program_Error
;
10167 when Attribute_Range_Length
=> Range_Length
: declare
10168 Diff
: aliased Uint
;
10173 -- Can fold if both bounds are compile time known
10175 if Compile_Time_Known_Value
(Hi_Bound
)
10176 and then Compile_Time_Known_Value
(Lo_Bound
)
10180 (0, Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
) + 1),
10184 -- One more case is where Hi_Bound and Lo_Bound are compile-time
10185 -- comparable, and we can figure out the difference between them.
10187 case Compile_Time_Compare
10188 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
10191 Fold_Uint
(N
, Uint_1
, Static
);
10194 Fold_Uint
(N
, Uint_0
, Static
);
10197 if Present
(Diff
) then
10198 Fold_Uint
(N
, Diff
+ 1, Static
);
10210 when Attribute_Ref
=>
10211 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
10217 when Attribute_Remainder
=> Remainder
: declare
10218 X
: constant Ureal
:= Expr_Value_R
(E1
);
10219 Y
: constant Ureal
:= Expr_Value_R
(E2
);
10222 if UR_Is_Zero
(Y
) then
10223 Apply_Compile_Time_Constraint_Error
10224 (N
, "division by zero in Remainder",
10225 CE_Overflow_Check_Failed
,
10226 Warn
=> not Static
);
10232 Fold_Ureal
(N
, Eval_Fat
.Remainder
(P_Base_Type
, X
, Y
), Static
);
10239 when Attribute_Restriction_Set
=>
10240 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
10241 Set_Is_Static_Expression
(N
);
10247 when Attribute_Round
=> Round
: declare
10252 -- First we get the (exact result) in units of small
10254 Sr
:= Expr_Value_R
(E1
) / Small_Value
(C_Type
);
10256 -- Now round that exactly to an integer
10258 Si
:= UR_To_Uint
(Sr
);
10260 -- Finally the result is obtained by converting back to real
10262 Fold_Ureal
(N
, Si
* Small_Value
(C_Type
), Static
);
10269 when Attribute_Rounding
=>
10271 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
10277 when Attribute_Safe_Emax
=>
10278 Fold_Uint
(N
, Safe_Emax_Value
(P_Type
), Static
);
10284 when Attribute_Safe_First
=>
10285 Fold_Ureal
(N
, Safe_First_Value
(P_Type
), Static
);
10291 when Attribute_Safe_Large
=>
10292 if Is_Fixed_Point_Type
(P_Type
) then
10294 (N
, Expr_Value_R
(Type_High_Bound
(P_Base_Type
)), Static
);
10296 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
10303 when Attribute_Safe_Last
=>
10304 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
10310 when Attribute_Safe_Small
=>
10312 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
10313 -- for fixed-point, since is the same as Small, but we implement
10314 -- it for backwards compatibility.
10316 if Is_Fixed_Point_Type
(P_Type
) then
10317 Fold_Ureal
(N
, Small_Value
(P_Type
), Static
);
10319 -- Ada 83 Safe_Small for floating-point cases
10322 Fold_Ureal
(N
, Model_Small_Value
(P_Type
), Static
);
10329 when Attribute_Scale
=>
10330 Fold_Uint
(N
, Scale_Value
(P_Type
), Static
);
10336 when Attribute_Scaling
=>
10340 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
10347 when Attribute_Signed_Zeros
=>
10349 (N
, UI_From_Int
(Boolean'Pos (Has_Signed_Zeros
(P_Type
))), Static
);
10355 -- Size attribute returns the RM size. All scalar types can be folded,
10356 -- as well as any types for which the size is known by the front end,
10357 -- including any type for which a size attribute is specified. This is
10358 -- one of the places where it is annoying that a size of zero means two
10359 -- things (zero size for scalars, unspecified size for non-scalars).
10361 when Attribute_Size
10362 | Attribute_VADS_Size
10365 P_TypeA
: constant Entity_Id
:= Full_Type
(P_Type
);
10369 (if Is_Scalar_Type
(P_TypeA
) then Known_RM_Size
(P_TypeA
));
10370 if Known_RM_Size
(P_TypeA
) then
10373 if Id
= Attribute_VADS_Size
or else Use_VADS_Size
then
10375 S
: constant Node_Id
:= Size_Clause
(P_TypeA
);
10378 -- If a size clause applies, then use the size from it.
10379 -- This is one of the rare cases where we can use the
10380 -- Size_Clause field for a subtype when Has_Size_Clause
10381 -- is False. Consider:
10383 -- type x is range 1 .. 64;
10384 -- for x'size use 12;
10385 -- subtype y is x range 0 .. 3;
10387 -- Here y has a size clause inherited from x, but
10388 -- normally it does not apply, and y'size is 2. However,
10389 -- y'VADS_Size is indeed 12 and not 2.
10392 and then Is_OK_Static_Expression
(Expression
(S
))
10394 Fold_Uint
(N
, Expr_Value
(Expression
(S
)), Static
);
10396 -- If no size is specified, then we simply use the object
10397 -- size in the VADS_Size case (e.g. Natural'Size is equal
10398 -- to Integer'Size, not one less).
10401 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
10405 -- Normal case (Size) in which case we want the RM_Size
10408 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
10417 when Attribute_Small
=>
10419 -- The floating-point case is present only for Ada 83 compatibility.
10420 -- Note that strictly this is an illegal addition, since we are
10421 -- extending an Ada 95 defined attribute, but we anticipate an
10422 -- ARG ruling that will permit this.
10424 if Is_Floating_Point_Type
(P_Type
) then
10426 -- Ada 83 attribute is defined as (RM83 3.5.8)
10428 -- T'Small = 2.0**(-T'Emax - 1)
10432 -- T'Emax = 4 * T'Mantissa
10434 Fold_Ureal
(N
, Ureal_2
** ((-(4 * Mantissa
)) - 1), Static
);
10436 -- Normal Ada 95 fixed-point case
10439 Fold_Ureal
(N
, Small_Value
(P_Type
), True);
10442 -----------------------
10443 -- Small_Denominator --
10444 -----------------------
10446 when Attribute_Small_Denominator
=>
10447 Fold_Uint
(N
, Norm_Den
(Small_Value
(P_Type
)), True);
10449 ---------------------
10450 -- Small_Numerator --
10451 ---------------------
10453 when Attribute_Small_Numerator
=>
10454 Fold_Uint
(N
, Norm_Num
(Small_Value
(P_Type
)), True);
10460 when Attribute_Stream_Size
=>
10467 when Attribute_Succ
=>
10468 -- Floating-point case
10470 if Is_Floating_Point_Type
(P_Type
) then
10472 (N
, Eval_Fat
.Succ
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
10474 -- Fixed-point case
10476 elsif Is_Fixed_Point_Type
(P_Type
) then
10477 Fold_Ureal
(N
, Expr_Value_R
(E1
) + Small_Value
(P_Type
), Static
);
10479 -- Modular integer case (wraps)
10481 elsif Is_Modular_Integer_Type
(P_Type
) then
10482 Fold_Uint
(N
, (Expr_Value
(E1
) + 1) mod Modulus
(P_Type
), Static
);
10484 -- Other scalar cases
10487 pragma Assert
(Is_Scalar_Type
(P_Type
));
10489 if Is_Enumeration_Type
(P_Type
)
10490 and then Expr_Value
(E1
) =
10491 Expr_Value
(Type_High_Bound
(P_Base_Type
))
10493 Apply_Compile_Time_Constraint_Error
10494 (N
, "Succ of `&''Last`",
10495 CE_Overflow_Check_Failed
,
10496 Ent
=> P_Base_Type
,
10497 Warn
=> not Static
);
10502 Fold_Uint
(N
, Expr_Value
(E1
) + 1, Static
);
10510 when Attribute_Truncation
=>
10513 Eval_Fat
.Truncation
(P_Base_Type
, Expr_Value_R
(E1
)),
10520 when Attribute_Type_Class
=> Type_Class
: declare
10521 Typ
: constant Entity_Id
:= Full_Type
(P_Base_Type
);
10525 if Is_Descendant_Of_Address
(Typ
) then
10526 Id
:= RE_Type_Class_Address
;
10528 elsif Is_Enumeration_Type
(Typ
) then
10529 Id
:= RE_Type_Class_Enumeration
;
10531 elsif Is_Integer_Type
(Typ
) then
10532 Id
:= RE_Type_Class_Integer
;
10534 elsif Is_Fixed_Point_Type
(Typ
) then
10535 Id
:= RE_Type_Class_Fixed_Point
;
10537 elsif Is_Floating_Point_Type
(Typ
) then
10538 Id
:= RE_Type_Class_Floating_Point
;
10540 elsif Is_Array_Type
(Typ
) then
10541 Id
:= RE_Type_Class_Array
;
10543 elsif Is_Record_Type
(Typ
) then
10544 Id
:= RE_Type_Class_Record
;
10546 elsif Is_Access_Type
(Typ
) then
10547 Id
:= RE_Type_Class_Access
;
10549 elsif Is_Task_Type
(Typ
) then
10550 Id
:= RE_Type_Class_Task
;
10552 -- We treat protected types like task types. It would make more
10553 -- sense to have another enumeration value, but after all the
10554 -- whole point of this feature is to be exactly DEC compatible,
10555 -- and changing the type Type_Class would not meet this requirement.
10557 elsif Is_Protected_Type
(Typ
) then
10558 Id
:= RE_Type_Class_Task
;
10560 -- Not clear if there are any other possibilities, but if there
10561 -- are, then we will treat them as the address case.
10564 Id
:= RE_Type_Class_Address
;
10567 Rewrite
(N
, New_Occurrence_Of
(RTE
(Id
), Loc
));
10570 -----------------------
10571 -- Unbiased_Rounding --
10572 -----------------------
10574 when Attribute_Unbiased_Rounding
=>
10577 Eval_Fat
.Unbiased_Rounding
(P_Base_Type
, Expr_Value_R
(E1
)),
10580 -------------------------
10581 -- Unconstrained_Array --
10582 -------------------------
10584 when Attribute_Unconstrained_Array
=> Unconstrained_Array
: declare
10585 Typ
: constant Entity_Id
:= Full_Type
(P_Type
);
10588 Rewrite
(N
, New_Occurrence_Of
(
10590 Is_Array_Type
(P_Type
)
10591 and then not Is_Constrained
(Typ
)), Loc
));
10593 -- Analyze and resolve as boolean, note that this attribute is
10594 -- a static attribute in GNAT.
10596 Analyze_And_Resolve
(N
, Standard_Boolean
);
10598 Set_Is_Static_Expression
(N
, True);
10599 end Unconstrained_Array
;
10601 -- Attribute Update is never static
10603 when Attribute_Update
=>
10610 -- Processing is shared with Size
10616 when Attribute_Val
=>
10617 if Expr_Value
(E1
) < Expr_Value
(Type_Low_Bound
(P_Base_Type
))
10619 Expr_Value
(E1
) > Expr_Value
(Type_High_Bound
(P_Base_Type
))
10621 Apply_Compile_Time_Constraint_Error
10622 (N
, "Val expression out of range",
10623 CE_Range_Check_Failed
,
10624 Warn
=> not Static
);
10630 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
10637 -- The Value_Size attribute for a type returns the RM size of the type.
10638 -- This an always be folded for scalar types, and can also be folded for
10639 -- non-scalar types if the size is set. This is one of the places where
10640 -- it is annoying that a size of zero means two things!
10642 when Attribute_Value_Size
=> Value_Size
: declare
10643 P_TypeA
: constant Entity_Id
:= Full_Type
(P_Type
);
10647 (if Is_Scalar_Type
(P_TypeA
) then Known_RM_Size
(P_TypeA
));
10648 if Known_RM_Size
(P_TypeA
) then
10649 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
10657 -- Version can never be static
10659 when Attribute_Version
=>
10666 -- Wide_Image is a scalar attribute, but is never static, because it
10667 -- is not a static function (having a non-scalar argument (RM 4.9(22))
10669 when Attribute_Wide_Image
=>
10672 ---------------------
10673 -- Wide_Wide_Image --
10674 ---------------------
10676 -- Wide_Wide_Image is a scalar attribute but is never static, because it
10677 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
10679 when Attribute_Wide_Wide_Image
=>
10682 ---------------------
10683 -- Wide_Wide_Width --
10684 ---------------------
10686 -- Processing for Wide_Wide_Width is combined with Width
10692 -- Processing for Wide_Width is combined with Width
10698 -- This processing also handles the case of Wide_[Wide_]Width
10700 when Attribute_Width
10701 | Attribute_Wide_Width
10702 | Attribute_Wide_Wide_Width
10704 if Compile_Time_Known_Bounds
(P_Type
) then
10706 -- Floating-point types
10708 if Is_Floating_Point_Type
(P_Type
) then
10710 -- Width is zero for a null range (RM 3.5 (38))
10712 if Expr_Value_R
(Type_High_Bound
(P_Type
)) <
10713 Expr_Value_R
(Type_Low_Bound
(P_Type
))
10715 Fold_Uint
(N
, Uint_0
, Static
);
10718 -- For floating-point, we have +N.dddE+nnn where length
10719 -- of ddd is determined by type'Digits - 1, but is one
10720 -- if Digits is one (RM 3.5 (33)).
10722 -- nnn is set to 2 for Short_Float and Float (32 bit
10723 -- floats), and 3 for Long_Float and Long_Long_Float.
10724 -- For machines where Long_Long_Float is the IEEE
10725 -- extended precision type, the exponent takes 4 digits.
10729 Int
'Max (2, UI_To_Int
(Digits_Value
(P_Type
)));
10732 if Esize
(P_Type
) <= 32 then
10734 elsif Esize
(P_Type
) = 64 then
10740 Fold_Uint
(N
, UI_From_Int
(Len
), Static
);
10744 -- Fixed-point types
10746 elsif Is_Fixed_Point_Type
(P_Type
) then
10748 -- Width is zero for a null range (RM 3.5 (38))
10750 if Expr_Value
(Type_High_Bound
(P_Type
)) <
10751 Expr_Value
(Type_Low_Bound
(P_Type
))
10753 Fold_Uint
(N
, Uint_0
, Static
);
10755 -- The non-null case depends on the specific real type
10758 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
10761 (N
, UI_From_Int
(Fore_Value
+ 1) + Aft_Value
(P_Type
),
10769 R
: constant Entity_Id
:= Root_Type
(P_Type
);
10770 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
10771 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
10784 -- Width for types derived from Standard.Character
10785 -- and Standard.Wide_[Wide_]Character.
10787 elsif Is_Standard_Character_Type
(P_Type
) then
10790 -- Set W larger if needed
10792 for J
in UI_To_Int
(Lo
) .. UI_To_Int
(Hi
) loop
10794 -- All wide characters look like Hex_hhhhhhhh
10798 -- No need to compute this more than once
10803 C
:= Character'Val (J
);
10805 -- Test for all cases where Character'Image
10806 -- yields an image that is longer than three
10807 -- characters. First the cases of Reserved_xxx
10808 -- names (length = 12).
10885 when Space
.. Tilde
10886 | No_Break_Space
.. LC_Y_Diaeresis
10888 -- Special case of soft hyphen in Ada 2005
10890 if C
= Character'Val (16#AD#
)
10891 and then Ada_Version
>= Ada_2005
10899 W
:= Int
'Max (W
, Wt
);
10903 -- Width for types derived from Standard.Boolean
10905 elsif R
= Standard_Boolean
then
10912 -- Width for integer types
10914 elsif Is_Integer_Type
(P_Type
) then
10915 T
:= UI_Max
(abs Lo
, abs Hi
);
10923 -- User declared enum type with discard names
10925 elsif Discard_Names
(R
) then
10927 -- If range is null, result is zero, that has already
10928 -- been dealt with, so what we need is the power of ten
10929 -- that accommodates the Pos of the largest value, which
10930 -- is the high bound of the range + one for the space.
10932 W
:= 1; -- one character for the leading space
10933 W
:= W
+ 1; -- one character for the 0 .. 9 digit
10934 T
:= Hi
; -- one character for every decimal digit
10940 -- Only remaining possibility is user declared enum type
10941 -- with normal case of Discard_Names not active.
10944 pragma Assert
(Is_Enumeration_Type
(P_Type
));
10947 L
:= First_Literal
(P_Type
);
10948 while Present
(L
) loop
10950 -- Only pay attention to in range characters
10952 if Lo
<= Enumeration_Pos
(L
)
10953 and then Enumeration_Pos
(L
) <= Hi
10955 -- For Width case, use decoded name
10957 if Id
= Attribute_Width
then
10958 Get_Decoded_Name_String
(Chars
(L
));
10959 Wt
:= Nat
(Name_Len
);
10961 -- For Wide_[Wide_]Width, use encoded name, and
10962 -- then adjust for the encoding.
10965 Get_Name_String
(Chars
(L
));
10967 -- Character literals are always of length 3
10969 if Name_Buffer
(1) = 'Q' then
10972 -- Otherwise loop to adjust for upper/wide chars
10975 Wt
:= Nat
(Name_Len
);
10977 for J
in 1 .. Name_Len
loop
10978 if Name_Buffer
(J
) = 'U' then
10980 elsif Name_Buffer
(J
) = 'W' then
10987 W
:= Int
'Max (W
, Wt
);
10994 Fold_Uint
(N
, UI_From_Int
(W
), Static
);
10999 -- The following attributes denote functions that cannot be folded
11001 when Attribute_From_Any
11003 | Attribute_TypeCode
11007 -- The following attributes can never be folded, and furthermore we
11008 -- should not even have entered the case statement for any of these.
11009 -- Note that in some cases, the values have already been folded as
11010 -- a result of the processing in Analyze_Attribute or earlier in
11013 when Attribute_Abort_Signal
11015 | Attribute_Address
11016 | Attribute_Address_Size
11017 | Attribute_Asm_Input
11018 | Attribute_Asm_Output
11020 | Attribute_Bit_Order
11021 | Attribute_Bit_Position
11022 | Attribute_Callable
11025 | Attribute_Code_Address
11026 | Attribute_Compiler_Version
11028 | Attribute_Default_Bit_Order
11029 | Attribute_Default_Scalar_Storage_Order
11031 | Attribute_Elaborated
11032 | Attribute_Elab_Body
11033 | Attribute_Elab_Spec
11034 | Attribute_Elab_Subp_Body
11035 | Attribute_Enabled
11036 | Attribute_External_Tag
11037 | Attribute_Fast_Math
11038 | Attribute_First_Bit
11042 | Attribute_Initialized
11043 | Attribute_Last_Bit
11044 | Attribute_Library_Level
11045 | Attribute_Max_Integer_Size
11046 | Attribute_Maximum_Alignment
11049 | Attribute_Partition_ID
11050 | Attribute_Pool_Address
11051 | Attribute_Position
11052 | Attribute_Priority
11053 | Attribute_Put_Image
11056 | Attribute_Scalar_Storage_Order
11057 | Attribute_Simple_Storage_Pool
11058 | Attribute_Storage_Pool
11059 | Attribute_Storage_Size
11060 | Attribute_Storage_Unit
11061 | Attribute_Stub_Type
11063 | Attribute_System_Allocator_Alignment
11065 | Attribute_Target_Name
11066 | Attribute_Terminated
11067 | Attribute_To_Address
11068 | Attribute_Type_Key
11069 | Attribute_Unchecked_Access
11070 | Attribute_Universal_Literal_String
11071 | Attribute_Unrestricted_Access
11073 | Attribute_Valid_Scalars
11074 | Attribute_Valid_Value
11076 | Attribute_Wchar_T_Size
11077 | Attribute_Wide_Value
11078 | Attribute_Wide_Wide_Value
11079 | Attribute_Word_Size
11082 raise Program_Error
;
11085 -- At the end of the case, one more check. If we did a static evaluation
11086 -- so that the result is now a literal, then set Is_Static_Expression
11087 -- in the constant only if the prefix type is a static subtype. For
11088 -- non-static subtypes, the folding is still OK, but not static.
11090 -- An exception is the GNAT attribute Constrained_Array which is
11091 -- defined to be a static attribute in all cases.
11093 if Nkind
(N
) in N_Integer_Literal
11095 | N_Character_Literal
11097 or else (Is_Entity_Name
(N
)
11098 and then Ekind
(Entity
(N
)) = E_Enumeration_Literal
)
11100 Set_Is_Static_Expression
(N
, Static
);
11102 -- If this is still an attribute reference, then it has not been folded
11103 -- and that means that its expressions are in a non-static context.
11105 elsif Nkind
(N
) = N_Attribute_Reference
then
11108 -- Note: the else case not covered here are odd cases where the
11109 -- processing has transformed the attribute into something other
11110 -- than a constant. Nothing more to do in such cases.
11115 end Eval_Attribute
;
11117 ------------------------------
11118 -- Is_Anonymous_Tagged_Base --
11119 ------------------------------
11121 function Is_Anonymous_Tagged_Base
11123 Typ
: Entity_Id
) return Boolean
11127 Anon
= Current_Scope
11128 and then Is_Itype
(Anon
)
11129 and then Associated_Node_For_Itype
(Anon
) = Parent
(Typ
);
11130 end Is_Anonymous_Tagged_Base
;
11132 --------------------------------
11133 -- Name_Implies_Lvalue_Prefix --
11134 --------------------------------
11136 function Name_Implies_Lvalue_Prefix
(Nam
: Name_Id
) return Boolean is
11137 pragma Assert
(Is_Attribute_Name
(Nam
));
11139 return Attribute_Name_Implies_Lvalue_Prefix
(Get_Attribute_Id
(Nam
));
11140 end Name_Implies_Lvalue_Prefix
;
11142 -----------------------
11143 -- Resolve_Attribute --
11144 -----------------------
11146 procedure Resolve_Attribute
(N
: Node_Id
; Typ
: Entity_Id
) is
11147 Loc
: constant Source_Ptr
:= Sloc
(N
);
11148 P
: constant Node_Id
:= Prefix
(N
);
11149 Aname
: constant Name_Id
:= Attribute_Name
(N
);
11150 Attr_Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
11151 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
11152 Des_Btyp
: Entity_Id
;
11153 Index
: Interp_Index
;
11155 Nom_Subt
: Entity_Id
;
11157 function Declared_Within_Generic_Unit
11158 (Entity
: Entity_Id
;
11159 Generic_Unit
: Node_Id
) return Boolean;
11160 -- Returns True if Declared_Entity is declared within the declarative
11161 -- region of Generic_Unit; otherwise returns False.
11163 function Is_Thin_Pointer_To_Unc_Array
(T
: Entity_Id
) return Boolean;
11164 -- Return True if T is a thin pointer to an unconstrained array type
11166 ----------------------------------
11167 -- Declared_Within_Generic_Unit --
11168 ----------------------------------
11170 function Declared_Within_Generic_Unit
11171 (Entity
: Entity_Id
;
11172 Generic_Unit
: Node_Id
) return Boolean
11174 Generic_Encloser
: Node_Id
:= Enclosing_Generic_Unit
(Entity
);
11177 while Present
(Generic_Encloser
) loop
11178 if Generic_Encloser
= Generic_Unit
then
11182 -- We have to step to the scope of the generic's entity, because
11183 -- otherwise we'll just get back the same generic.
11185 Generic_Encloser
:=
11186 Enclosing_Generic_Unit
11187 (Scope
(Defining_Entity
(Generic_Encloser
)));
11191 end Declared_Within_Generic_Unit
;
11193 ----------------------------------
11194 -- Is_Thin_Pointer_To_Unc_Array --
11195 ----------------------------------
11197 function Is_Thin_Pointer_To_Unc_Array
(T
: Entity_Id
) return Boolean is
11199 if Is_Access_Type
(T
)
11200 and then Has_Size_Clause
(T
)
11201 and then RM_Size
(T
) = System_Address_Size
11204 DT
: constant Entity_Id
:= Designated_Type
(T
);
11207 return Is_Array_Type
(DT
) and then not Is_Constrained
(DT
);
11213 end Is_Thin_Pointer_To_Unc_Array
;
11215 -- Start of processing for Resolve_Attribute
11218 -- If error during analysis, no point in continuing, except for array
11219 -- types, where we get better recovery by using unconstrained indexes
11220 -- than nothing at all (see Check_Array_Type).
11222 if Error_Posted
(N
)
11223 and then Attr_Id
/= Attribute_First
11224 and then Attr_Id
/= Attribute_Last
11225 and then Attr_Id
/= Attribute_Length
11226 and then Attr_Id
/= Attribute_Range
11231 -- If attribute was universal type, reset to actual type
11233 if Is_Universal_Numeric_Type
(Etype
(N
)) then
11234 Set_Etype
(N
, Typ
);
11237 -- A Ghost attribute must appear in a specific context
11239 if Is_Ghost_Attribute_Reference
(N
) then
11240 Check_Ghost_Context
(Empty
, N
);
11243 -- Remaining processing depends on attribute
11251 -- For access attributes, if the prefix denotes an entity, it is
11252 -- interpreted as a name, never as a call. It may be overloaded,
11253 -- in which case resolution uses the profile of the context type.
11254 -- Otherwise prefix must be resolved.
11256 when Attribute_Access
11257 | Attribute_Unchecked_Access
11258 | Attribute_Unrestricted_Access
11260 -- Note possible modification if we have a variable
11263 and then not Is_Access_Constant
(Typ
)
11265 Note_Possible_Modification
(P
, Sure
=> False);
11268 -- Case where prefix is an entity name
11270 if Is_Entity_Name
(P
) then
11272 -- Deal with case where prefix itself is overloaded
11274 if Is_Overloaded
(P
) then
11275 Get_First_Interp
(P
, Index
, It
);
11276 while Present
(It
.Nam
) loop
11278 -- Overloaded object names can occur when user-defined
11279 -- references are involved. The prefix can be interpreted
11280 -- as either just an object of the reference type, or an
11281 -- implicit dereferencing of such an object.
11283 if Is_Object
(It
.Nam
) then
11284 if Covers
(Designated_Type
(Typ
), Etype
(It
.Typ
)) then
11286 -- If the interpretation is a discriminant for an
11287 -- implicit dereference, then build the dereference
11288 -- and resolve the rewritten attribute recursively.
11290 if Ekind
(It
.Nam
) = E_Discriminant
11291 and then Has_Implicit_Dereference
(It
.Nam
)
11293 Build_Explicit_Dereference
11294 (P
, Get_Reference_Discriminant
(Etype
(P
)));
11295 Resolve_Attribute
(N
, Typ
);
11300 -- The prefix is definitely NOT overloaded anymore
11301 -- at this point, so we reset the Is_Overloaded
11302 -- flag to avoid any confusion when reanalyzing
11305 Set_Is_Overloaded
(P
, False);
11306 Set_Is_Overloaded
(N
, False);
11307 Generate_Reference
(Entity
(P
), P
);
11312 elsif Type_Conformant
(Designated_Type
(Typ
), It
.Nam
) then
11313 Set_Entity
(P
, It
.Nam
);
11315 -- The prefix is definitely NOT overloaded anymore at
11316 -- this point, so we reset the Is_Overloaded flag to
11317 -- avoid any confusion when reanalyzing the node.
11319 Set_Is_Overloaded
(P
, False);
11320 Set_Is_Overloaded
(N
, False);
11321 Generate_Reference
(Entity
(P
), P
);
11325 Get_Next_Interp
(Index
, It
);
11328 -- If Prefix is a subprogram name, this reference freezes,
11329 -- but not if within spec expression mode. The profile of
11330 -- the subprogram is not frozen at this point.
11332 if not In_Spec_Expression
then
11333 Freeze_Before
(N
, Entity
(P
), Do_Freeze_Profile
=> False);
11336 -- If it is a type, there is nothing to resolve.
11337 -- If it is a subprogram, do not freeze its profile.
11338 -- If it is an object, complete its resolution.
11340 elsif Is_Overloadable
(Entity
(P
)) then
11341 if not In_Spec_Expression
then
11342 Freeze_Before
(N
, Entity
(P
), Do_Freeze_Profile
=> False);
11345 -- Nothing to do if prefix is a type name
11347 elsif Is_Type
(Entity
(P
)) then
11350 -- Otherwise non-overloaded other case, resolve the prefix
11356 -- Some further error checks
11358 Error_Msg_Name_1
:= Aname
;
11360 if not Is_Entity_Name
(P
) then
11363 elsif Is_Overloadable
(Entity
(P
))
11364 and then Is_Abstract_Subprogram
(Entity
(P
))
11366 Error_Msg_F
("prefix of % attribute cannot be abstract", P
);
11367 Set_Etype
(N
, Any_Type
);
11369 elsif Ekind
(Entity
(P
)) = E_Enumeration_Literal
then
11371 ("prefix of % attribute cannot be enumeration literal", P
);
11372 Set_Etype
(N
, Any_Type
);
11374 -- An attempt to take 'Access of a function that renames an
11375 -- enumeration literal. Issue a specialized error message.
11377 elsif Ekind
(Entity
(P
)) = E_Function
11378 and then Present
(Alias
(Entity
(P
)))
11379 and then Ekind
(Alias
(Entity
(P
))) = E_Enumeration_Literal
11382 ("prefix of % attribute cannot be function renaming "
11383 & "an enumeration literal", P
);
11384 Set_Etype
(N
, Any_Type
);
11386 elsif Convention
(Entity
(P
)) = Convention_Intrinsic
then
11387 Error_Msg_F
("prefix of % attribute cannot be intrinsic", P
);
11388 Set_Etype
(N
, Any_Type
);
11391 -- Assignments, return statements, components of aggregates,
11392 -- generic instantiations will require convention checks if
11393 -- the type is an access to subprogram. Given that there will
11394 -- also be accessibility checks on those, this is where the
11395 -- checks can eventually be centralized ???
11397 if Ekind
(Btyp
) in E_Access_Protected_Subprogram_Type
11398 | E_Access_Subprogram_Type
11399 | E_Anonymous_Access_Protected_Subprogram_Type
11400 | E_Anonymous_Access_Subprogram_Type
11402 -- Deal with convention mismatch
11404 if Convention
(Designated_Type
(Btyp
)) /=
11405 Convention
(Entity
(P
))
11408 ("subprogram & has wrong convention", P
, Entity
(P
));
11409 Error_Msg_Sloc
:= Sloc
(Btyp
);
11410 Error_Msg_FE
("\does not match & declared#", P
, Btyp
);
11412 if not Is_Itype
(Btyp
)
11413 and then not Has_Convention_Pragma
(Btyp
)
11414 and then Convention
(Entity
(P
)) /= Convention_Intrinsic
11417 ("\probable missing pragma Convention for &",
11422 Check_Subtype_Conformant
11423 (New_Id
=> Entity
(P
),
11424 Old_Id
=> Designated_Type
(Btyp
),
11428 if Attr_Id
= Attribute_Unchecked_Access
then
11429 Error_Msg_Name_1
:= Aname
;
11431 ("attribute% cannot be applied to a subprogram", P
);
11433 elsif Aname
= Name_Unrestricted_Access
then
11434 null; -- Nothing to check
11436 -- Check the static accessibility rule of 3.10.2(32).
11437 -- This rule also applies within the private part of an
11438 -- instantiation. This rule does not apply to anonymous
11439 -- access-to-subprogram types in access parameters.
11441 elsif Attr_Id
= Attribute_Access
11442 and then not In_Instance_Body
11444 (Ekind
(Btyp
) = E_Access_Subprogram_Type
11445 or else Is_Local_Anonymous_Access
(Btyp
))
11446 and then Subprogram_Access_Level
(Entity
(P
)) >
11447 Type_Access_Level
(Btyp
)
11450 ("subprogram must not be deeper than access type", P
);
11452 -- Check the restriction of 3.10.2(32) that disallows the
11453 -- access attribute within a generic body when the ultimate
11454 -- ancestor of the type of the attribute is declared outside
11455 -- of the generic unit and the subprogram is declared within
11456 -- that generic unit. This includes any such attribute that
11457 -- occurs within the body of a generic unit that is a child
11458 -- of the generic unit where the subprogram is declared.
11460 -- The rule also prohibits applying the attribute when the
11461 -- access type is a generic formal access type (since the
11462 -- level of the actual type is not known). This restriction
11463 -- does not apply when the attribute type is an anonymous
11464 -- access-to-subprogram type. Note that this check was
11465 -- revised by AI-229, because the original Ada 95 rule
11466 -- was too lax. The original rule only applied when the
11467 -- subprogram was declared within the body of the generic,
11468 -- which allowed the possibility of dangling references).
11469 -- The rule was also too strict in some cases, in that it
11470 -- didn't permit the access to be declared in the generic
11471 -- spec, whereas the revised rule does (as long as it's not
11474 -- There are a couple of subtleties of the test for applying
11475 -- the check that are worth noting. First, we only apply it
11476 -- when the levels of the subprogram and access type are the
11477 -- same (the case where the subprogram is statically deeper
11478 -- was applied above, and the case where the type is deeper
11479 -- is always safe). Second, we want the check to apply
11480 -- within nested generic bodies and generic child unit
11481 -- bodies, but not to apply to an attribute that appears in
11482 -- the generic unit's specification. This is done by testing
11483 -- that the attribute's innermost enclosing generic body is
11484 -- not the same as the innermost generic body enclosing the
11485 -- generic unit where the subprogram is declared (we don't
11486 -- want the check to apply when the access attribute is in
11487 -- the spec and there's some other generic body enclosing
11488 -- generic). Finally, there's no point applying the check
11489 -- when within an instance, because any violations will have
11490 -- been caught by the compilation of the generic unit.
11492 -- We relax this check in Relaxed_RM_Semantics mode for
11493 -- compatibility with legacy code for use by Ada source
11494 -- code analyzers (e.g. CodePeer).
11496 elsif Attr_Id
= Attribute_Access
11497 and then not Relaxed_RM_Semantics
11498 and then not In_Instance
11499 and then Present
(Enclosing_Generic_Unit
(Entity
(P
)))
11500 and then Present
(Enclosing_Generic_Body
(N
))
11501 and then Enclosing_Generic_Body
(N
) /=
11502 Enclosing_Generic_Body
11503 (Enclosing_Generic_Unit
(Entity
(P
)))
11504 and then Subprogram_Access_Level
(Entity
(P
)) =
11505 Type_Access_Level
(Btyp
)
11506 and then Ekind
(Btyp
) /=
11507 E_Anonymous_Access_Subprogram_Type
11508 and then Ekind
(Btyp
) /=
11509 E_Anonymous_Access_Protected_Subprogram_Type
11511 -- The attribute type's ultimate ancestor must be
11512 -- declared within the same generic unit as the
11513 -- subprogram is declared (including within another
11514 -- nested generic unit). The error message is
11515 -- specialized to say "ancestor" for the case where the
11516 -- access type is not its own ancestor, since saying
11517 -- simply "access type" would be very confusing.
11519 if not Declared_Within_Generic_Unit
11521 Enclosing_Generic_Unit
(Entity
(P
)))
11524 ("''Access attribute not allowed in generic body",
11527 if Root_Type
(Btyp
) = Btyp
then
11530 "access type & is declared outside " &
11531 "generic unit (RM 3.10.2(32))", N
, Btyp
);
11534 ("\because ancestor of " &
11535 "access type & is declared outside " &
11536 "generic unit (RM 3.10.2(32))", N
, Btyp
);
11540 ("\move ''Access to private part, or " &
11541 "(Ada 2005) use anonymous access type instead of &",
11544 -- If the ultimate ancestor of the attribute's type is
11545 -- a formal type, then the attribute is illegal because
11546 -- the actual type might be declared at a higher level.
11547 -- The error message is specialized to say "ancestor"
11548 -- for the case where the access type is not its own
11549 -- ancestor, since saying simply "access type" would be
11552 elsif Is_Generic_Type
(Root_Type
(Btyp
)) then
11553 if Root_Type
(Btyp
) = Btyp
then
11555 ("access type must not be a generic formal type",
11559 ("ancestor access type must not be a generic " &
11566 -- If this is a renaming, an inherited operation, or a
11567 -- subprogram instance, use the original entity. This may make
11568 -- the node type-inconsistent, so this transformation can only
11569 -- be done if the node will not be reanalyzed. In particular,
11570 -- if it is within a default expression, the transformation
11571 -- must be delayed until the default subprogram is created for
11572 -- it, when the enclosing subprogram is frozen.
11574 if Is_Entity_Name
(P
)
11575 and then Is_Overloadable
(Entity
(P
))
11576 and then Present
(Alias
(Entity
(P
)))
11577 and then Expander_Active
11580 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
11583 elsif Nkind
(P
) = N_Selected_Component
11584 and then Is_Overloadable
(Entity
(Selector_Name
(P
)))
11586 -- Protected operation. If operation is overloaded, must
11587 -- disambiguate. Prefix that denotes protected object itself
11588 -- is resolved with its own type.
11590 if Attr_Id
= Attribute_Unchecked_Access
then
11591 Error_Msg_Name_1
:= Aname
;
11593 ("attribute% cannot be applied to protected operation", P
);
11596 Resolve
(Prefix
(P
));
11598 if not Is_Overloaded
(P
) then
11599 Generate_Reference
(Entity
(Selector_Name
(P
)), P
);
11602 Get_First_Interp
(P
, Index
, It
);
11603 while Present
(It
.Nam
) loop
11604 if Type_Conformant
(Designated_Type
(Typ
), It
.Nam
) then
11605 Set_Entity
(Selector_Name
(P
), It
.Nam
);
11607 -- The prefix is definitely NOT overloaded anymore at
11608 -- this point, so we reset the Is_Overloaded flag to
11609 -- avoid any confusion when reanalyzing the node.
11611 Set_Is_Overloaded
(P
, False);
11612 Set_Is_Overloaded
(N
, False);
11613 Generate_Reference
(Entity
(Selector_Name
(P
)), P
);
11617 Get_Next_Interp
(Index
, It
);
11621 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
11622 -- statically illegal if F is an anonymous access to subprogram.
11624 elsif Nkind
(P
) = N_Explicit_Dereference
11625 and then Is_Entity_Name
(Prefix
(P
))
11626 and then Ekind
(Etype
(Entity
(Prefix
(P
)))) =
11627 E_Anonymous_Access_Subprogram_Type
11629 Error_Msg_N
("anonymous access to subprogram "
11630 & "has deeper accessibility than any master", P
);
11632 elsif Is_Overloaded
(P
) then
11634 -- Use the designated type of the context to disambiguate
11635 -- Note that this was not strictly conformant to Ada 95,
11636 -- but was the implementation adopted by most Ada 95 compilers.
11637 -- The use of the context type to resolve an Access attribute
11638 -- reference is now mandated in AI-235 for Ada 2005.
11641 Index
: Interp_Index
;
11645 Get_First_Interp
(P
, Index
, It
);
11646 while Present
(It
.Typ
) loop
11647 if Covers
(Designated_Type
(Typ
), It
.Typ
) then
11648 Resolve
(P
, It
.Typ
);
11652 Get_Next_Interp
(Index
, It
);
11659 -- Refuse to compute access to variables and constants when that
11660 -- would drop the strub mode associated with them, unless they're
11661 -- unchecked conversions. We don't have to do this when the types
11662 -- of the data objects are annotated: then the access type
11663 -- designates the annotated type, and there's no loss. Only when
11664 -- the variable is annotated directly that the pragma gets
11665 -- attached to the variable, rather than to its type, and then,
11666 -- expressing an access-to-annotated-type type to hold the 'Access
11667 -- result is not possible without resorting to that very annotated
11670 if Attr_Id
/= Attribute_Unchecked_Access
11671 and then Comes_From_Source
(N
)
11672 and then Is_Entity_Name
(P
)
11673 and then Explicit_Strub_Mode
(Entity
(P
)) = Enabled
11675 Explicit_Strub_Mode
(Designated_Type
(Btyp
)) = Unspecified
11677 Error_Msg_F
("target access type drops `strub` mode from &", P
);
11680 -- X'Access is illegal if X denotes a constant and the access type
11681 -- is access-to-variable. Same for 'Unchecked_Access. The rule
11682 -- does not apply to 'Unrestricted_Access. If the reference is a
11683 -- default-initialized aggregate component for a self-referential
11684 -- type the reference is legal.
11686 if not (Ekind
(Btyp
) = E_Access_Subprogram_Type
11687 or else Ekind
(Btyp
) = E_Anonymous_Access_Subprogram_Type
11688 or else (Is_Record_Type
(Btyp
)
11690 Present
(Corresponding_Remote_Type
(Btyp
)))
11691 or else Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
11692 or else Ekind
(Btyp
)
11693 = E_Anonymous_Access_Protected_Subprogram_Type
11694 or else Is_Access_Constant
(Btyp
)
11695 or else Is_Variable
(P
)
11696 or else Attr_Id
= Attribute_Unrestricted_Access
)
11698 if Is_Entity_Name
(P
)
11699 and then Is_Type
(Entity
(P
))
11701 -- Legality of a self-reference through an access
11702 -- attribute has been verified in Analyze_Access_Attribute.
11706 elsif Comes_From_Source
(N
) then
11707 Error_Msg_F
("access-to-variable designates constant", P
);
11711 Des_Btyp
:= Designated_Type
(Btyp
);
11713 if Ada_Version
>= Ada_2005
11714 and then Is_Incomplete_Type
(Des_Btyp
)
11716 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
11717 -- imported entity, and the non-limited view is visible, make
11718 -- use of it. If it is an incomplete subtype, use the base type
11721 if From_Limited_With
(Des_Btyp
)
11722 and then Present
(Non_Limited_View
(Des_Btyp
))
11724 Des_Btyp
:= Non_Limited_View
(Des_Btyp
);
11726 elsif Ekind
(Des_Btyp
) = E_Incomplete_Subtype
then
11727 Des_Btyp
:= Etype
(Des_Btyp
);
11731 if Ekind
(Btyp
) in E_General_Access_Type | E_Anonymous_Access_Type
11733 -- Ada 2005 (AI-230): Check the accessibility of anonymous
11734 -- access types for stand-alone objects, record and array
11735 -- components, and return objects. For a component definition
11736 -- the level is the same of the enclosing composite type.
11738 if Ada_Version
>= Ada_2005
11739 and then Attr_Id
= Attribute_Access
11740 and then (Is_Local_Anonymous_Access
(Btyp
)
11742 -- Handle cases where Btyp is the anonymous access
11743 -- type of an Ada 2012 stand-alone object.
11745 or else Nkind
(Associated_Node_For_Itype
(Btyp
)) =
11746 N_Object_Declaration
)
11748 -- Verify that static checking is OK (namely that we aren't
11749 -- in a specific context requiring dynamic checks on
11750 -- expicitly aliased parameters), and then check the level.
11752 -- Otherwise a check will be generated later when the return
11753 -- statement gets expanded.
11755 and then not Is_Special_Aliased_Formal_Access
(N
)
11757 Static_Accessibility_Level
(N
, Zero_On_Dynamic_Level
) >
11758 Deepest_Type_Access_Level
(Btyp
)
11760 -- In an instance, this is a runtime check, but one we know
11761 -- will fail, so generate an appropriate warning. As usual,
11762 -- this kind of warning is an error in SPARK mode.
11764 if In_Instance_Body
then
11768 not No_Dynamic_Accessibility_Checks_Enabled
(P
);
11771 ("non-local pointer cannot point to local object<<", P
);
11772 Error_Msg_F
("\Program_Error [<<", P
);
11775 Make_Raise_Program_Error
(Loc
,
11776 Reason
=> PE_Accessibility_Check_Failed
));
11777 Set_Etype
(N
, Typ
);
11781 ("non-local pointer cannot point to local object", P
);
11785 if Attr_Id
/= Attribute_Unrestricted_Access
11786 and then Is_Dependent_Component_Of_Mutable_Object
(P
)
11789 ("illegal attribute for discriminant-dependent component",
11792 elsif Depends_On_Mutably_Tagged_Ext_Comp
(P
) then
11794 ("illegal attribute for mutably tagged component", P
);
11797 -- Check static matching rule of 3.10.2(27). Nominal subtype
11798 -- of the prefix must statically match the designated type.
11800 Nom_Subt
:= Etype
(P
);
11802 if Is_Constr_Subt_For_U_Nominal
(Nom_Subt
) then
11803 Nom_Subt
:= Base_Type
(Nom_Subt
);
11806 -- We do not enforce static matching for Unrestricted_Access
11807 -- except for a thin pointer to an unconstrained array type,
11808 -- because, in this case, the designated object must contain
11809 -- its bounds, which means that it must have an unconstrained
11810 -- nominal subtype (and be aliased, as will be checked below).
11812 if Attr_Id
= Attribute_Unrestricted_Access
11813 and then not (Is_Thin_Pointer_To_Unc_Array
(Typ
)
11814 and then Is_Aliased_View
(Original_Node
(P
)))
11818 elsif Is_Tagged_Type
(Designated_Type
(Typ
)) then
11820 -- If the attribute is in the context of an access
11821 -- parameter, then the prefix is allowed to be of
11822 -- the class-wide type (by AI-127).
11824 if Ekind
(Typ
) = E_Anonymous_Access_Type
then
11825 if not Covers
(Designated_Type
(Typ
), Nom_Subt
)
11826 and then not Covers
(Nom_Subt
, Designated_Type
(Typ
))
11832 Desig
:= Designated_Type
(Typ
);
11834 if Is_Class_Wide_Type
(Desig
) then
11835 Desig
:= Etype
(Desig
);
11838 if Is_Anonymous_Tagged_Base
(Nom_Subt
, Desig
) then
11843 ("type of prefix: & not compatible",
11846 ("\with &, the expected designated type",
11847 P
, Designated_Type
(Typ
));
11852 elsif not Covers
(Designated_Type
(Typ
), Nom_Subt
)
11854 (not Is_Class_Wide_Type
(Designated_Type
(Typ
))
11855 and then Is_Class_Wide_Type
(Nom_Subt
))
11858 ("type of prefix: & is not covered", P
, Nom_Subt
);
11860 ("\by &, the expected designated type" &
11861 " (RM 3.10.2 (27))", P
, Designated_Type
(Typ
));
11864 if Is_Class_Wide_Type
(Designated_Type
(Typ
))
11865 and then Has_Discriminants
(Etype
(Designated_Type
(Typ
)))
11866 and then Is_Constrained
(Etype
(Designated_Type
(Typ
)))
11867 and then Designated_Type
(Typ
) /= Nom_Subt
11869 Apply_Discriminant_Check
11870 (N
, Etype
(Designated_Type
(Typ
)));
11873 -- Ada 2005 (AI-363): Require static matching when designated
11874 -- type has discriminants and a constrained partial view, since
11875 -- in general objects of such types are mutable, so we can't
11876 -- allow the access value to designate a constrained object
11877 -- (because access values must be assumed to designate mutable
11878 -- objects when designated type does not impose a constraint).
11880 elsif Subtypes_Statically_Match
(Des_Btyp
, Nom_Subt
) then
11883 elsif Has_Discriminants
(Designated_Type
(Typ
))
11884 and then not Is_Constrained
(Des_Btyp
)
11886 (Ada_Version
< Ada_2005
11888 not Object_Type_Has_Constrained_Partial_View
11889 (Typ
=> Designated_Type
(Base_Type
(Typ
)),
11890 Scop
=> Current_Scope
))
11896 ("object subtype must statically match "
11897 & "designated subtype", P
);
11899 if Is_Entity_Name
(P
)
11900 and then Is_Array_Type
(Designated_Type
(Typ
))
11903 D
: constant Node_Id
:= Declaration_Node
(Entity
(P
));
11906 ("aliased object has explicit bounds??", D
);
11908 ("\declare without bounds (and with explicit "
11909 & "initialization)??", D
);
11911 ("\for use with unconstrained access??", D
);
11916 -- Check the static accessibility rule of 3.10.2(28). Note that
11917 -- this check is not performed for the case of an anonymous
11918 -- access type, since the access attribute is always legal
11919 -- in such a context - unless the restriction
11920 -- No_Dynamic_Accessibility_Checks is active.
11923 No_Dynamic_Acc_Checks
: constant Boolean :=
11924 No_Dynamic_Accessibility_Checks_Enabled
(Btyp
);
11926 Compatible_Alt_Checks
: constant Boolean :=
11927 No_Dynamic_Acc_Checks
and then not Debug_Flag_Underscore_B
;
11930 if Attr_Id
= Attribute_Access
11931 and then (Ekind
(Btyp
) = E_General_Access_Type
11932 or else No_Dynamic_Acc_Checks
)
11934 -- In the case of the alternate "compatibility"
11935 -- accessibility model we do not perform a static
11936 -- accessibility check on actuals for anonymous access
11937 -- types - so exclude them here.
11939 and then not (Compatible_Alt_Checks
11940 and then Is_Actual_Parameter
(N
)
11941 and then Ekind
(Btyp
)
11942 = E_Anonymous_Access_Type
)
11944 -- Call Accessibility_Level directly to avoid returning
11945 -- zero on cases where the prefix is an explicitly aliased
11946 -- parameter in a return statement, instead of using the
11947 -- normal Static_Accessibility_Level function.
11949 -- Shouldn't this be handled somehow in
11950 -- Static_Accessibility_Level ???
11952 and then Nkind
(Accessibility_Level
(P
, Dynamic_Level
))
11953 = N_Integer_Literal
11955 Intval
(Accessibility_Level
(P
, Dynamic_Level
))
11956 > Deepest_Type_Access_Level
(Btyp
)
11958 Accessibility_Message
(N
, Typ
);
11964 if Ekind
(Btyp
) in E_Access_Protected_Subprogram_Type
11965 | E_Anonymous_Access_Protected_Subprogram_Type
11967 if Is_Entity_Name
(P
)
11968 and then not Is_Protected_Type
(Scope
(Entity
(P
)))
11970 Error_Msg_F
("context requires a protected subprogram", P
);
11972 -- Check accessibility of protected object against that of the
11973 -- access type, but only on user code, because the expander
11974 -- creates access references for handlers. If the context is an
11975 -- anonymous_access_to_protected, there are no accessibility
11976 -- checks either. Omit check entirely for Unrestricted_Access.
11978 elsif Static_Accessibility_Level
(P
, Zero_On_Dynamic_Level
)
11979 > Deepest_Type_Access_Level
(Btyp
)
11980 and then Comes_From_Source
(N
)
11981 and then Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
11982 and then Attr_Id
/= Attribute_Unrestricted_Access
11984 Accessibility_Message
(N
, Typ
);
11987 -- AI05-0225: If the context is not an access to protected
11988 -- function, the prefix must be a variable, given that it may
11989 -- be used subsequently in a protected call.
11991 elsif Nkind
(P
) = N_Selected_Component
11992 and then not Is_Variable
(Prefix
(P
))
11993 and then Ekind
(Entity
(Selector_Name
(P
))) /= E_Function
11996 ("target object of access to protected procedure "
11997 & "must be variable", N
);
11999 elsif Is_Entity_Name
(P
) then
12000 Check_Internal_Protected_Use
(N
, Entity
(P
));
12003 elsif Ekind
(Btyp
) in E_Access_Subprogram_Type
12004 | E_Anonymous_Access_Subprogram_Type
12005 and then Ekind
(Etype
(N
)) = E_Access_Protected_Subprogram_Type
12007 Error_Msg_F
("context requires a non-protected subprogram", P
);
12010 -- AI12-0412: The rule in RM 6.1.1(18.2/5) disallows applying
12011 -- attribute Access to a primitive of an abstract type when the
12012 -- primitive has any Pre'Class or Post'Class aspects specified
12013 -- with nonstatic expressions.
12015 if Attr_Id
= Attribute_Access
12016 and then Ekind
(Btyp
) in E_Access_Subprogram_Type
12017 | E_Anonymous_Access_Subprogram_Type
12018 and then Is_Entity_Name
(P
)
12019 and then Is_Dispatching_Operation
(Entity
(P
))
12021 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post
(Entity
(P
))
12024 ("attribute not allowed for primitive of abstract type with "
12025 & "nonstatic class-wide pre/postconditions",
12029 -- The context cannot be a pool-specific type, but this is a
12030 -- legality rule, not a resolution rule, so it must be checked
12031 -- separately, after possibly disambiguation (see AI-245).
12033 if Ekind
(Btyp
) = E_Access_Type
12034 and then Attr_Id
/= Attribute_Unrestricted_Access
12036 Wrong_Type
(N
, Typ
);
12039 -- The context may be a constrained access type (however ill-
12040 -- advised such subtypes might be) so in order to generate a
12041 -- constraint check we need to set the type of the attribute
12042 -- reference to the base type of the context.
12044 Set_Etype
(N
, Btyp
);
12046 -- Check for incorrect atomic/volatile/VFA reference (RM C.6(12))
12048 if Attr_Id
/= Attribute_Unrestricted_Access
then
12049 if Is_Atomic_Object
(P
)
12050 and then not Is_Atomic
(Designated_Type
(Typ
))
12053 ("access to atomic object cannot yield access-to-" &
12054 "non-atomic type", P
);
12056 elsif Is_Volatile_Object_Ref
(P
)
12057 and then not Is_Volatile
(Designated_Type
(Typ
))
12060 ("access to volatile object cannot yield access-to-" &
12061 "non-volatile type", P
);
12063 elsif Is_Volatile_Full_Access_Object_Ref
(P
)
12064 and then not Is_Volatile_Full_Access
(Designated_Type
(Typ
))
12067 ("access to full access object cannot yield access-to-" &
12068 "non-full-access type", P
);
12071 -- Check for nonatomic subcomponent of a full access object
12072 -- in Ada 2022 (RM C.6 (12)).
12074 if Ada_Version
>= Ada_2022
12075 and then Is_Subcomponent_Of_Full_Access_Object
(P
)
12076 and then not Is_Atomic_Object
(P
)
12079 ("cannot have access attribute with prefix &", N
, P
);
12081 ("\nonatomic subcomponent of full access object "
12082 & "(RM C.6(12))", N
);
12086 -- Check for aliased view. We allow a nonaliased prefix when in
12087 -- an instance because the prefix may have been a tagged formal
12088 -- object, which is defined to be aliased even when the actual
12089 -- might not be (other instance cases will have been caught in
12090 -- the generic). Similarly, within an inlined body we know that
12091 -- the attribute is legal in the original subprogram, therefore
12092 -- legal in the expansion.
12094 if not (Is_Entity_Name
(P
)
12095 and then Is_Overloadable
(Entity
(P
)))
12096 and then not (Nkind
(P
) = N_Selected_Component
12098 Is_Overloadable
(Entity
(Selector_Name
(P
))))
12099 and then not Is_Aliased_View
(Original_Node
(P
))
12100 and then not In_Instance
12101 and then not In_Inlined_Body
12102 and then Comes_From_Source
(N
)
12104 -- Here we have a non-aliased view. This is illegal unless we
12105 -- have the case of Unrestricted_Access, where for now we allow
12106 -- this (we will reject later if expected type is access to an
12107 -- unconstrained array with a thin pointer).
12109 -- No need for an error message on a generated access reference
12110 -- for the controlling argument in a dispatching call: error
12111 -- will be reported when resolving the call.
12113 if Attr_Id
/= Attribute_Unrestricted_Access
then
12114 Error_Msg_Name_1
:= Aname
;
12115 Error_Msg_N
("prefix of % attribute must be aliased", P
);
12117 -- Check for unrestricted access where expected type is a thin
12118 -- pointer to an unconstrained array.
12120 elsif Is_Thin_Pointer_To_Unc_Array
(Typ
) then
12122 ("illegal use of Unrestricted_Access attribute", P
);
12124 ("\attempt to generate thin pointer to unaliased "
12129 -- Check that the prefix does not have a value conversion of an
12130 -- array type since a value conversion is like an aggregate with
12131 -- respect to determining accessibility level (RM 3.10.2).
12133 if not Prefix_With_Safe_Accessibility_Level
(N
, Typ
) then
12134 Accessibility_Message
(N
, Typ
);
12138 -- Mark that address of entity is taken in case of
12139 -- 'Unrestricted_Access or in case of a subprogram.
12141 if Is_Entity_Name
(P
)
12142 and then (Attr_Id
= Attribute_Unrestricted_Access
12143 or else Is_Subprogram
(Entity
(P
)))
12145 Set_Address_Taken
(Entity
(P
));
12148 -- Deal with possible elaboration check
12150 if Is_Entity_Name
(P
) and then Is_Subprogram
(Entity
(P
)) then
12152 Subp_Id
: constant Entity_Id
:= Entity
(P
);
12153 Scop
: constant Entity_Id
:= Scope
(Subp_Id
);
12154 Subp_Decl
: constant Node_Id
:=
12155 Unit_Declaration_Node
(Subp_Id
);
12156 Flag_Id
: Entity_Id
;
12157 Subp_Body
: Node_Id
;
12159 -- If the access has been taken and the body of the subprogram
12160 -- has not been see yet, indirect calls must be protected with
12161 -- elaboration checks. We have the proper elaboration machinery
12162 -- for subprograms declared in packages, but within a block or
12163 -- a subprogram the body will appear in the same declarative
12164 -- part, and we must insert a check in the eventual body itself
12165 -- using the elaboration flag that we generate now. The check
12166 -- is then inserted when the body is expanded. This processing
12167 -- is not needed for a stand alone expression function because
12168 -- the internally generated spec and body are always inserted
12169 -- as a pair in the same declarative list.
12173 and then Comes_From_Source
(Subp_Id
)
12174 and then Comes_From_Source
(N
)
12175 and then In_Open_Scopes
(Scop
)
12176 and then Ekind
(Scop
) in E_Block | E_Procedure | E_Function
12177 and then not Has_Completion
(Subp_Id
)
12178 and then No
(Elaboration_Entity
(Subp_Id
))
12179 and then Nkind
(Subp_Decl
) = N_Subprogram_Declaration
12180 and then Nkind
(Original_Node
(Subp_Decl
)) /=
12181 N_Expression_Function
12183 -- Create elaboration variable for it
12185 Flag_Id
:= Make_Temporary
(Loc
, 'E');
12186 Set_Elaboration_Entity
(Subp_Id
, Flag_Id
);
12187 Set_Is_Frozen
(Flag_Id
);
12189 -- Insert declaration for flag after subprogram
12190 -- declaration. Note that attribute reference may
12191 -- appear within a nested scope.
12193 Insert_After_And_Analyze
(Subp_Decl
,
12194 Make_Object_Declaration
(Loc
,
12195 Defining_Identifier
=> Flag_Id
,
12196 Object_Definition
=>
12197 New_Occurrence_Of
(Standard_Short_Integer
, Loc
),
12199 Make_Integer_Literal
(Loc
, Uint_0
)));
12201 -- The above sets the Scope of the flag entity to the
12202 -- current scope, in which the attribute appears, but
12203 -- the flag declaration has been inserted after that
12204 -- of Subp_Id, so the scope of the flag is the same as
12205 -- that of Subp_Id. This is relevant when unnesting,
12206 -- where processing depends on correct scope setting.
12208 Set_Scope
(Flag_Id
, Scop
);
12211 -- Taking the 'Access of an expression function freezes its
12212 -- expression (RM 13.14 10.3/3). This does not apply to an
12213 -- expression function that acts as a completion because the
12214 -- generated body is immediately analyzed and the expression
12215 -- is automatically frozen.
12217 if Is_Expression_Function
(Subp_Id
)
12218 and then Present
(Corresponding_Body
(Subp_Decl
))
12221 Unit_Declaration_Node
(Corresponding_Body
(Subp_Decl
));
12223 -- The body has already been analyzed when the expression
12224 -- function acts as a completion.
12226 if Analyzed
(Subp_Body
) then
12229 -- Attribute 'Access may appear within the generated body
12230 -- of the expression function subject to the attribute:
12232 -- function F is (... F'Access ...);
12234 -- If the expression function is on the scope stack, then
12235 -- the body is currently being analyzed. Do not reanalyze
12236 -- it because this will lead to infinite recursion.
12238 elsif In_Open_Scopes
(Subp_Id
) then
12241 -- If reference to the expression function appears in an
12242 -- inner scope, for example as an actual in an instance,
12243 -- this is not a freeze point either.
12245 elsif Scope
(Subp_Id
) /= Current_Scope
then
12248 -- Dispatch tables are not a freeze point either
12250 elsif Nkind
(Parent
(N
)) = N_Unchecked_Type_Conversion
12251 and then Is_Dispatch_Table_Entity
(Etype
(Parent
(N
)))
12255 -- Analyze the body of the expression function to freeze
12259 Analyze
(Subp_Body
);
12269 -- Deal with resolving the type for Address attribute, overloading
12270 -- is not permitted here, since there is no context to resolve it.
12272 when Attribute_Address
12273 | Attribute_Code_Address
12275 -- To be safe, assume that if the address of a variable is taken,
12276 -- it may be modified via this address, so note modification,
12277 -- unless the address is compared directly, which should not be
12278 -- considered a modification.
12281 and then Nkind
(Parent
(N
)) not in N_Op_Compare
12283 Note_Possible_Modification
(P
, Sure
=> False);
12286 if Nkind
(P
) in N_Subexpr
and then Is_Overloaded
(P
) then
12287 Get_First_Interp
(P
, Index
, It
);
12288 Get_Next_Interp
(Index
, It
);
12290 if Present
(It
.Nam
) then
12291 Error_Msg_Name_1
:= Aname
;
12293 ("prefix of % attribute cannot be overloaded", P
);
12297 if not Is_Entity_Name
(P
)
12298 or else not Is_Overloadable
(Entity
(P
))
12303 -- If this is the name of a derived subprogram, or that of a
12304 -- generic actual, the address is that of the original entity.
12306 if Is_Entity_Name
(P
)
12307 and then Is_Overloadable
(Entity
(P
))
12308 and then Present
(Alias
(Entity
(P
)))
12311 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
12314 if Is_Entity_Name
(P
) then
12315 Set_Address_Taken
(Entity
(P
));
12318 if Nkind
(P
) = N_Slice
then
12320 -- Arr (X .. Y)'address is identical to Arr (X)'address,
12321 -- even if the array is packed and the slice itself is not
12322 -- addressable. Transform the prefix into an indexed component.
12324 -- Note that the transformation is safe only if we know that
12325 -- the slice is non-null. That is because a null slice can have
12326 -- an out of bounds index value.
12328 -- Right now, gigi blows up if given 'Address on a slice as a
12329 -- result of some incorrect freeze nodes generated by the front
12330 -- end, and this covers up that bug in one case, but the bug is
12331 -- likely still there in the cases not handled by this code ???
12333 -- It's not clear what 'Address *should* return for a null
12334 -- slice with out of bounds indexes, this might be worth an ARG
12337 -- One approach would be to do a length check unconditionally,
12338 -- and then do the transformation below unconditionally, but
12339 -- analyze with checks off, avoiding the problem of the out of
12340 -- bounds index. This approach would interpret the address of
12341 -- an out of bounds null slice as being the address where the
12342 -- array element would be if there was one, which is probably
12343 -- as reasonable an interpretation as any ???
12346 Loc
: constant Source_Ptr
:= Sloc
(P
);
12347 D
: constant Node_Id
:= Discrete_Range
(P
);
12351 if Is_Entity_Name
(D
)
12354 (Type_Low_Bound
(Entity
(D
)),
12355 Type_High_Bound
(Entity
(D
)))
12358 Make_Attribute_Reference
(Loc
,
12359 Prefix
=> (New_Occurrence_Of
(Entity
(D
), Loc
)),
12360 Attribute_Name
=> Name_First
);
12362 elsif Nkind
(D
) = N_Range
12363 and then Not_Null_Range
(Low_Bound
(D
), High_Bound
(D
))
12365 Lo
:= Low_Bound
(D
);
12371 if Present
(Lo
) then
12373 Make_Indexed_Component
(Loc
,
12374 Prefix
=> Relocate_Node
(Prefix
(P
)),
12375 Expressions
=> New_List
(Lo
)));
12377 Analyze_And_Resolve
(P
);
12386 -- Prefix of Body_Version attribute can be a subprogram name which
12387 -- must not be resolved, since this is not a call.
12389 when Attribute_Body_Version
=>
12396 -- Prefix of Caller attribute is an entry name which must not
12397 -- be resolved, since this is definitely not an entry call.
12399 when Attribute_Caller
=>
12406 -- Shares processing with Address attribute
12412 -- If the prefix of the Count attribute is an entry name it must not
12413 -- be resolved, since this is definitely not an entry call. However,
12414 -- if it is an element of an entry family, the index itself may
12415 -- have to be resolved because it can be a general expression.
12417 when Attribute_Count
12420 if Nkind
(P
) = N_Indexed_Component
12421 and then Is_Entity_Name
(Prefix
(P
))
12424 Indx
: constant Node_Id
:= First
(Expressions
(P
));
12425 Fam
: constant Entity_Id
:= Entity
(Prefix
(P
));
12427 Resolve
(Indx
, Entry_Index_Type
(Fam
));
12428 Apply_Scalar_Range_Check
(Indx
, Entry_Index_Type
(Fam
));
12436 -- Prefix of the Elaborated attribute is a subprogram name which
12437 -- must not be resolved, since this is definitely not a call. Note
12438 -- that it is a library unit, so it cannot be overloaded here.
12440 when Attribute_Elaborated
=>
12447 -- Prefix of Enabled attribute is a check name, which must be treated
12448 -- specially and not touched by Resolve.
12450 when Attribute_Enabled
=>
12457 -- Processing is shared with Count
12463 -- Do not resolve the prefix of Loop_Entry, instead wait until the
12464 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
12465 -- The delay ensures that any generated checks or temporaries are
12466 -- inserted before the relocated prefix.
12468 when Attribute_Loop_Entry
=>
12471 --------------------
12472 -- Mechanism_Code --
12473 --------------------
12475 -- Prefix of the Mechanism_Code attribute is a function name
12476 -- which must not be resolved. Should we check for overloaded ???
12478 when Attribute_Mechanism_Code
=>
12485 -- Most processing is done in sem_dist, after determining the
12486 -- context type. Node is rewritten as a conversion to a runtime call.
12488 when Attribute_Partition_ID
=>
12489 Process_Partition_Id
(N
);
12496 when Attribute_Pool_Address
=>
12503 -- We replace the Range attribute node with a range expression whose
12504 -- bounds are the 'First and 'Last attributes applied to the same
12505 -- prefix. The reason that we do this transformation here instead of
12506 -- in the expander is that it simplifies other parts of the semantic
12507 -- analysis which assume that the Range has been replaced; thus it
12508 -- must be done even when in semantic-only mode (note that the RM
12509 -- specifically mentions this equivalence, we take care that the
12510 -- prefix is only evaluated once).
12512 when Attribute_Range
=> Range_Attribute
: declare
12518 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
12521 -- If the prefix is a function call returning on the secondary
12522 -- stack, we must make sure to mark/release the stack.
12524 if Nkind
(P
) = N_Function_Call
12525 and then Nkind
(Parent
(N
)) = N_Loop_Parameter_Specification
12526 and then Requires_Transient_Scope
(Etype
(P
))
12528 Set_Uses_Sec_Stack
(Scope
(Current_Scope
));
12532 Dims
:= Expressions
(N
);
12535 Make_Attribute_Reference
(Loc
,
12536 Prefix
=> Duplicate_Subexpr
(P
, Name_Req
=> True),
12537 Attribute_Name
=> Name_Last
,
12538 Expressions
=> Dims
);
12541 Make_Attribute_Reference
(Loc
,
12543 Attribute_Name
=> Name_First
,
12544 Expressions
=> (Dims
));
12546 -- Do not share the dimension indicator, if present. Even though
12547 -- it is a static constant, its source location may be modified
12548 -- when printing expanded code and node sharing will lead to chaos
12551 if Present
(Dims
) then
12552 Set_Expressions
(LB
, New_List
(New_Copy_Tree
(First
(Dims
))));
12555 -- If the original was marked as Must_Not_Freeze (see code in
12556 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
12559 if Must_Not_Freeze
(N
) then
12560 Set_Must_Not_Freeze
(HB
);
12561 Set_Must_Not_Freeze
(LB
);
12562 Set_Must_Not_Freeze
(Prefix
(HB
));
12563 Set_Must_Not_Freeze
(Prefix
(LB
));
12566 if Raises_Constraint_Error
(Prefix
(N
)) then
12568 -- Preserve Sloc of prefix in the new bounds, so that the
12569 -- posted warning can be removed if we are within unreachable
12572 Set_Sloc
(LB
, Sloc
(Prefix
(N
)));
12573 Set_Sloc
(HB
, Sloc
(Prefix
(N
)));
12576 Rewrite
(N
, Make_Range
(Loc
, LB
, HB
));
12577 Analyze_And_Resolve
(N
, Typ
);
12579 -- Ensure that the expanded range does not have side effects
12581 Force_Evaluation
(LB
);
12582 Force_Evaluation
(HB
);
12584 -- Normally after resolving attribute nodes, Eval_Attribute
12585 -- is called to do any possible static evaluation of the node.
12586 -- However, here since the Range attribute has just been
12587 -- transformed into a range expression it is no longer an
12588 -- attribute node and therefore the call needs to be avoided
12589 -- and is accomplished by simply returning from the procedure.
12592 end Range_Attribute
;
12598 when Attribute_Reduce
=>
12600 Reducer_Subp_Name
: constant Node_Id
:= First
(Expressions
(N
));
12601 Init_Value_Exp
: constant Node_Id
:=
12602 Next
(Reducer_Subp_Name
);
12603 Op
: Entity_Id
:= Empty
;
12605 Index
: Interp_Index
;
12610 Strict
: Boolean := False) return Boolean;
12611 -- Is Op a suitable reducer subprogram?
12612 -- Strict indicates whether ops found in Standard should be
12613 -- considered even if Typ is not a predefined type.
12621 Strict
: Boolean := False) return Boolean
12623 F1
, F2
: Entity_Id
;
12625 F1
:= First_Formal
(Op
);
12629 F2
:= Next_Formal
(F1
);
12631 or else Present
(Next_Formal
(F2
))
12635 elsif Ekind
(Op
) = E_Procedure
then
12636 return Ekind
(F1
) = E_In_Out_Parameter
12637 and then Covers
(Typ
, Etype
(F1
));
12639 elsif Covers
(Typ
, Etype
(Op
)) then
12642 elsif Ekind
(Op
) = E_Operator
12643 and then Scope
(Op
) = Standard_Standard
12644 and then not Strict
12647 Op_Chars
: constant Any_Operator_Name
:= Chars
(Op
);
12648 -- Nonassociative ops like division are unlikely
12649 -- to come up in practice, but they are legal.
12658 return Is_Numeric_Type
(Typ
);
12660 when Name_Op_Mod | Name_Op_Rem
=>
12661 return Is_Numeric_Type
(Typ
)
12662 and then Is_Discrete_Type
(Typ
);
12664 when Name_Op_And | Name_Op_Or | Name_Op_Xor
=>
12665 -- No Boolean array operators in Standard
12666 return Is_Boolean_Type
(Typ
)
12667 or else Is_Modular_Integer_Type
(Typ
);
12669 when Name_Op_Concat
=>
12670 return Is_Array_Type
(Typ
)
12671 and then Number_Dimensions
(Typ
) = 1;
12673 when Name_Op_Eq | Name_Op_Ne
12674 | Name_Op_Lt | Name_Op_Le
12675 | Name_Op_Gt | Name_Op_Ge
12677 return Is_Boolean_Type
(Typ
);
12679 when Name_Op_Abs | Name_Op_Not
=>
12680 -- unary ops were already handled
12681 pragma Assert
(False);
12682 raise Program_Error
;
12692 Resolve
(Init_Value_Exp
, Typ
);
12693 if Is_Overloaded
(Reducer_Subp_Name
) then
12695 for Retry
in Boolean loop
12696 Get_First_Interp
(Reducer_Subp_Name
, Index
, It
);
12697 while Present
(It
.Nam
) loop
12698 if Proper_Op
(It
.Nam
, Strict
=> not Retry
) then
12700 Set_Entity
(Reducer_Subp_Name
, Op
);
12704 Get_Next_Interp
(Index
, It
);
12708 elsif Nkind
(Reducer_Subp_Name
) = N_Attribute_Reference
12709 and then (Attribute_Name
(Reducer_Subp_Name
) = Name_Max
12710 or else Attribute_Name
(Reducer_Subp_Name
) = Name_Min
)
12712 Op
:= Reducer_Subp_Name
;
12714 elsif Proper_Op
(Entity
(Reducer_Subp_Name
)) then
12715 Op
:= Entity
(Reducer_Subp_Name
);
12716 Set_Etype
(N
, Typ
);
12720 Error_Msg_N
("No suitable reducer subprogram found",
12721 Reducer_Subp_Name
);
12729 -- We will only come here during the prescan of a spec expression
12730 -- containing a Result attribute. In that case the proper Etype has
12731 -- already been set, and nothing more needs to be done here.
12733 when Attribute_Result
=>
12736 ----------------------
12737 -- Unchecked_Access --
12738 ----------------------
12740 -- Processing is shared with Access
12742 -------------------------
12743 -- Unrestricted_Access --
12744 -------------------------
12746 -- Processing is shared with Access
12752 -- Resolve aggregate components in component associations
12754 when Attribute_Update
=> Update
: declare
12755 Aggr
: constant Node_Id
:= First
(Expressions
(N
));
12756 Typ
: constant Entity_Id
:= Etype
(Prefix
(N
));
12762 -- Set the Etype of the aggregate to that of the prefix, even
12763 -- though the aggregate may not be a proper representation of a
12764 -- value of the type (missing or duplicated associations, etc.)
12765 -- Complete resolution of the prefix. Note that in Ada 2012 it
12766 -- can be a qualified expression that is e.g. an aggregate.
12768 Set_Etype
(Aggr
, Typ
);
12769 Resolve
(Prefix
(N
), Typ
);
12771 -- For an array type, resolve expressions with the component type
12772 -- of the array, and apply constraint checks when needed.
12774 if Is_Array_Type
(Typ
) then
12775 Assoc
:= First
(Component_Associations
(Aggr
));
12776 while Present
(Assoc
) loop
12777 Expr
:= Expression
(Assoc
);
12778 Resolve
(Expr
, Component_Type
(Typ
));
12780 -- The choices in the association are static constants,
12781 -- or static aggregates each of whose components belongs
12782 -- to the proper index type. However, they must also
12783 -- belong to the index subtype (s) of the prefix, which
12784 -- may be a subtype (e.g. given by a slice).
12786 -- Choices may also be identifiers with no staticness
12787 -- requirements, in which case they must resolve to the
12796 C
:= First
(Choices
(Assoc
));
12797 while Present
(C
) loop
12798 Indx
:= First_Index
(Etype
(Prefix
(N
)));
12800 if Nkind
(C
) /= N_Aggregate
then
12801 Analyze_And_Resolve
(C
, Etype
(Indx
));
12803 C_E
:= First
(Expressions
(C
));
12804 while Present
(C_E
) loop
12805 Analyze_And_Resolve
(C_E
, Etype
(Indx
));
12819 -- For a record type, use type of each component, which is
12820 -- recorded during analysis.
12823 Assoc
:= First
(Component_Associations
(Aggr
));
12824 while Present
(Assoc
) loop
12825 Comp
:= First
(Choices
(Assoc
));
12826 Expr
:= Expression
(Assoc
);
12828 if Nkind
(Comp
) /= N_Others_Choice
12829 and then not Error_Posted
(Comp
)
12831 Resolve
(Expr
, Etype
(Entity
(Comp
)));
12843 -- Apply range check. Note that we did not do this during the
12844 -- analysis phase, since we wanted Eval_Attribute to have a
12845 -- chance at finding an illegal out of range value.
12847 when Attribute_Val
=>
12849 -- Note that we do our own Eval_Attribute call here rather than
12850 -- use the common one, because we need to do processing after
12851 -- the call, as per above comment.
12853 Eval_Attribute
(N
);
12855 -- Eval_Attribute may replace the node with a raise CE, or
12856 -- fold it to a constant. Obviously we only apply a scalar
12857 -- range check if this did not happen.
12859 if Nkind
(N
) = N_Attribute_Reference
12860 and then Attribute_Name
(N
) = Name_Val
12862 Apply_Scalar_Range_Check
(First
(Expressions
(N
)), Btyp
);
12871 -- Prefix of Version attribute can be a subprogram name which
12872 -- must not be resolved, since this is not a call.
12874 when Attribute_Version
=>
12877 ----------------------
12878 -- Other Attributes --
12879 ----------------------
12881 -- For other attributes, resolve prefix unless it is a type. If
12882 -- the attribute reference itself is a type name ('Base and 'Class)
12883 -- then this is only legal within a task or protected record.
12886 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
12890 -- If the attribute reference itself is a type name ('Base,
12891 -- 'Class) then this is only legal within a task or protected
12892 -- record. What is this all about ???
12894 if Is_Entity_Name
(N
) and then Is_Type
(Entity
(N
)) then
12895 if Is_Concurrent_Type
(Entity
(N
))
12896 and then In_Open_Scopes
(Entity
(P
))
12901 ("invalid use of subtype name in expression or call", N
);
12905 -- For attributes whose argument may be a string, complete
12906 -- resolution of argument now. This avoids premature expansion
12907 -- (and the creation of transient scopes) before the attribute
12908 -- reference is resolved.
12911 when Attribute_Valid_Value | Attribute_Value
=>
12912 Resolve
(First
(Expressions
(N
)), Standard_String
);
12914 when Attribute_Wide_Value
=>
12915 Resolve
(First
(Expressions
(N
)), Standard_Wide_String
);
12917 when Attribute_Wide_Wide_Value
=>
12918 Resolve
(First
(Expressions
(N
)), Standard_Wide_Wide_String
);
12920 when others => null;
12923 -- Ensure that attribute expressions are resolved at this stage;
12924 -- required for preanalyzed references to discriminants since
12925 -- their resolution (and expansion) will take care of updating
12926 -- their Entity attribute to reference their discriminal.
12929 and then Present
(Expressions
(N
))
12932 Expr
: Node_Id
:= First
(Expressions
(N
));
12935 while Present
(Expr
) loop
12936 if not Analyzed
(Expr
) then
12937 Resolve
(Expr
, Etype
(Expr
));
12945 -- If the prefix of the attribute is a class-wide type then it
12946 -- will be expanded into a dispatching call to a predefined
12947 -- primitive. Therefore we must check for potential violation
12948 -- of such restriction.
12950 if Is_Class_Wide_Type
(Etype
(P
)) then
12951 Check_Restriction
(No_Dispatching_Calls
, N
);
12955 -- Mark use clauses of the original prefix if the attribute is applied
12958 if Nkind
(Original_Node
(P
)) in N_Has_Entity
12959 and then Present
(Entity
(Original_Node
(P
)))
12961 Mark_Use_Clauses
(Original_Node
(P
));
12964 -- Normally the Freezing is done by Resolve but sometimes the Prefix
12965 -- is not resolved, in which case the freezing must be done now.
12967 -- For an elaboration check on a subprogram, we do not freeze its type.
12968 -- It may be declared in an unrelated scope, in particular in the case
12969 -- of a generic function whose type may remain unelaborated.
12971 if Attr_Id
= Attribute_Elaborated
then
12974 -- Should this be restricted to Expander_Active???
12977 Freeze_Expression
(P
);
12980 -- Finally perform static evaluation on the attribute reference
12982 Analyze_Dimension
(N
);
12983 Eval_Attribute
(N
);
12984 end Resolve_Attribute
;
12986 ------------------------
12987 -- Set_Boolean_Result --
12988 ------------------------
12990 procedure Set_Boolean_Result
(N
: Node_Id
; B
: Boolean) is
12992 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(B
), Sloc
(N
)));
12993 end Set_Boolean_Result
;
12995 --------------------------------
12996 -- Stream_Attribute_Available --
12997 --------------------------------
12999 function Stream_Attribute_Available
13001 Nam
: TSS_Name_Type
;
13002 Partial_View
: Entity_Id
:= Empty
) return Boolean
13004 Etyp
: Entity_Id
:= Typ
;
13006 Real_Rep
: Node_Id
;
13008 -- Start of processing for Stream_Attribute_Available
13011 -- Test if the attribute is specified directly on the type
13013 if Has_Stream_Attribute_Definition
(Typ
, Nam
, Real_Rep
) then
13017 -- We assume class-wide types have stream attributes
13018 -- when they are not limited. Otherwise we recurse on the
13021 if Is_Class_Wide_Type
(Typ
) then
13022 return not Is_Limited_Type
(Typ
)
13023 or else Stream_Attribute_Available
(Etype
(Typ
), Nam
);
13026 -- Non-class-wide abstract types cannot have Input streams
13029 if Nam
= TSS_Stream_Input
13030 and then Is_Abstract_Type
(Typ
)
13031 and then not Is_Class_Wide_Type
(Typ
)
13036 -- Otherwise, nonlimited types have stream attributes
13038 if not (Is_Limited_Type
(Typ
)
13039 or else (Present
(Partial_View
)
13040 and then Is_Limited_Type
(Partial_View
)))
13045 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
13047 if Nam
= TSS_Stream_Input
13048 and then Ada_Version
>= Ada_2005
13049 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Read
, Real_Rep
)
13053 elsif Nam
= TSS_Stream_Output
13054 and then Ada_Version
>= Ada_2005
13055 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Write
, Real_Rep
)
13060 -- Case of Read and Write: check for attribute definition clause that
13061 -- applies to an ancestor type.
13063 while Etype
(Etyp
) /= Etyp
loop
13065 Derived_Type
: constant Entity_Id
:= Etyp
;
13067 Etyp
:= Etype
(Etyp
);
13069 if Has_Stream_Attribute_Definition
(Etyp
, Nam
, Real_Rep
) then
13070 if not Derivation_Too_Early_To_Inherit
(Derived_Type
, Nam
) then
13077 if Ada_Version
< Ada_2005
then
13079 -- In Ada 95 mode, also consider a non-visible definition
13082 Btyp
: constant Entity_Id
:= Implementation_Base_Type
(Typ
);
13085 and then Stream_Attribute_Available
13086 (Btyp
, Nam
, Partial_View
=> Typ
);
13091 end Stream_Attribute_Available
;