1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Ada
.Characters
.Latin_1
; use Ada
.Characters
.Latin_1
;
28 with Aspects
; use Aspects
;
29 with Atree
; use Atree
;
30 with Casing
; use Casing
;
31 with Checks
; use Checks
;
32 with Debug
; use Debug
;
33 with Einfo
; use Einfo
;
34 with Elists
; use Elists
;
35 with Errout
; use Errout
;
37 with Exp_Dist
; use Exp_Dist
;
38 with Exp_Util
; use Exp_Util
;
39 with Expander
; use Expander
;
40 with Freeze
; use Freeze
;
41 with Gnatvsn
; use Gnatvsn
;
42 with Itypes
; use Itypes
;
44 with Lib
.Xref
; use Lib
.Xref
;
45 with Nlists
; use Nlists
;
46 with Nmake
; use Nmake
;
48 with Restrict
; use Restrict
;
49 with Rident
; use Rident
;
50 with Rtsfind
; use Rtsfind
;
53 with Sem_Aux
; use Sem_Aux
;
54 with Sem_Cat
; use Sem_Cat
;
55 with Sem_Ch6
; use Sem_Ch6
;
56 with Sem_Ch8
; use Sem_Ch8
;
57 with Sem_Ch10
; use Sem_Ch10
;
58 with Sem_Dim
; use Sem_Dim
;
59 with Sem_Dist
; use Sem_Dist
;
60 with Sem_Elab
; use Sem_Elab
;
61 with Sem_Elim
; use Sem_Elim
;
62 with Sem_Eval
; use Sem_Eval
;
63 with Sem_Prag
; use Sem_Prag
;
64 with Sem_Res
; use Sem_Res
;
65 with Sem_Type
; use Sem_Type
;
66 with Sem_Util
; use Sem_Util
;
68 with Stand
; use Stand
;
69 with Sinfo
; use Sinfo
;
70 with Sinput
; use Sinput
;
72 with Stringt
; use Stringt
;
74 with Stylesw
; use Stylesw
;
75 with Targparm
; use Targparm
;
76 with Ttypes
; use Ttypes
;
77 with Tbuild
; use Tbuild
;
78 with Uintp
; use Uintp
;
79 with Uname
; use Uname
;
80 with Urealp
; use Urealp
;
82 with System
.CRC32
; use System
.CRC32
;
84 package body Sem_Attr
is
86 True_Value
: constant Uint
:= Uint_1
;
87 False_Value
: constant Uint
:= Uint_0
;
88 -- Synonyms to be used when these constants are used as Boolean values
90 Bad_Attribute
: exception;
91 -- Exception raised if an error is detected during attribute processing,
92 -- used so that we can abandon the processing so we don't run into
93 -- trouble with cascaded errors.
95 -- The following array is the list of attributes defined in the Ada 83 RM.
96 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
97 -- modes all these attributes are recognized, even if removed in Ada 95.
99 Attribute_83
: constant Attribute_Class_Array
:= Attribute_Class_Array
'(
102 Attribute_Alignment |
105 Attribute_Constrained |
112 Attribute_First_Bit |
118 Attribute_Leading_Part |
120 Attribute_Machine_Emax |
121 Attribute_Machine_Emin |
122 Attribute_Machine_Mantissa |
123 Attribute_Machine_Overflows |
124 Attribute_Machine_Radix |
125 Attribute_Machine_Rounds |
131 Attribute_Safe_Emax |
132 Attribute_Safe_Large |
133 Attribute_Safe_Small |
136 Attribute_Storage_Size |
138 Attribute_Terminated |
141 Attribute_Width => True,
144 -- The following array is the list of attributes defined in the Ada 2005
145 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
146 -- but in Ada 95 they are considered to be implementation defined.
148 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
149 Attribute_Machine_Rounding |
152 Attribute_Stream_Size |
153 Attribute_Wide_Wide_Width
=> True,
156 -- The following array is the list of attributes defined in the Ada 2012
157 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
158 -- and Ada 2005 modes, but are considered to be implementation defined.
160 Attribute_12
: constant Attribute_Class_Array
:= Attribute_Class_Array
'(
161 Attribute_First_Valid |
162 Attribute_Has_Same_Storage |
163 Attribute_Last_Valid |
164 Attribute_Max_Alignment_For_Allocation => True,
167 -- The following array is the list of attributes defined in the Ada 2020
168 -- RM which are not defined in Ada 2012. These are recognized in Ada
169 -- 95/2005/2012 modes, but are considered to be implementation defined.
171 Attribute_20 : constant Attribute_Class_Array := Attribute_Class_Array'(
173 Attribute_Enum_Val
=> True,
176 -- The following array contains all attributes that imply a modification
177 -- of their prefixes or result in an access value. Such prefixes can be
178 -- considered as lvalues.
180 Attribute_Name_Implies_Lvalue_Prefix
: constant Attribute_Class_Array
:=
181 Attribute_Class_Array
'(
186 Attribute_Unchecked_Access |
187 Attribute_Unrestricted_Access => True,
190 -----------------------
191 -- Local_Subprograms --
192 -----------------------
194 procedure Eval_Attribute (N : Node_Id);
195 -- Performs compile time evaluation of attributes where possible, leaving
196 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
197 -- set, and replacing the node with a literal node if the value can be
198 -- computed at compile time. All static attribute references are folded,
199 -- as well as a number of cases of non-static attributes that can always
200 -- be computed at compile time (e.g. floating-point model attributes that
201 -- are applied to non-static subtypes). Of course in such cases, the
202 -- Is_Static_Expression flag will not be set on the resulting literal.
203 -- Note that the only required action of this procedure is to catch the
204 -- static expression cases as described in the RM. Folding of other cases
205 -- is done where convenient, but some additional non-static folding is in
206 -- Expand_N_Attribute_Reference in cases where this is more convenient.
208 function Is_Anonymous_Tagged_Base
210 Typ : Entity_Id) return Boolean;
211 -- For derived tagged types that constrain parent discriminants we build
212 -- an anonymous unconstrained base type. We need to recognize the relation
213 -- between the two when analyzing an access attribute for a constrained
214 -- component, before the full declaration for Typ has been analyzed, and
215 -- where therefore the prefix of the attribute does not match the enclosing
218 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
219 -- Rewrites node N with an occurrence of either Standard_False or
220 -- Standard_True, depending on the value of the parameter B. The
221 -- result is marked as a static expression.
223 -----------------------
224 -- Analyze_Attribute --
225 -----------------------
227 procedure Analyze_Attribute (N : Node_Id) is
228 Loc : constant Source_Ptr := Sloc (N);
229 Aname : constant Name_Id := Attribute_Name (N);
230 P : constant Node_Id := Prefix (N);
231 Exprs : constant List_Id := Expressions (N);
232 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
236 P_Type : Entity_Id := Empty;
237 -- Type of prefix after analysis
239 P_Base_Type : Entity_Id := Empty;
240 -- Base type of prefix after analysis
242 -----------------------
243 -- Local Subprograms --
244 -----------------------
246 procedure Address_Checks;
247 -- Semantic checks for valid use of Address attribute. This was made
248 -- a separate routine with the idea of using it for unrestricted access
249 -- which seems like it should follow the same rules, but that turned
250 -- out to be impractical. So now this is only used for Address.
252 procedure Analyze_Access_Attribute;
253 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
254 -- Internally, Id distinguishes which of the three cases is involved.
256 procedure Analyze_Attribute_Old_Result
257 (Legal : out Boolean;
258 Spec_Id : out Entity_Id);
259 -- Common processing for attributes 'Old
and 'Result. The routine checks
260 -- that the attribute appears in a postcondition-like aspect or pragma
261 -- associated with a suitable subprogram or a body. Flag Legal is set
262 -- when the above criteria are met. Spec_Id denotes the entity of the
263 -- subprogram [body] or Empty if the attribute is illegal.
265 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id);
266 -- Common processing for attributes 'Img
, 'Image, 'Wide_Image
, and
267 -- 'Wide_Wide_Image. The routine checks that the prefix is valid and
268 -- sets the type of the attribute to the one specified by Str_Typ (e.g.
269 -- Standard_String for 'Image and Standard_Wide_String for 'Wide_Image).
271 procedure Bad_Attribute_For_Predicate
;
272 -- Output error message for use of a predicate (First, Last, Range) not
273 -- allowed with a type that has predicates. If the type is a generic
274 -- actual, then the message is a warning, and we generate code to raise
275 -- program error with an appropriate reason. No error message is given
276 -- for internally generated uses of the attributes. This legality rule
277 -- only applies to scalar types.
279 procedure Check_Array_Or_Scalar_Type
;
280 -- Common procedure used by First, Last, Range attribute to check
281 -- that the prefix is a constrained array or scalar type, or a name
282 -- of an array object, and that an argument appears only if appropriate
283 -- (i.e. only in the array case).
285 procedure Check_Array_Type
;
286 -- Common semantic checks for all array attributes. Checks that the
287 -- prefix is a constrained array type or the name of an array object.
288 -- The error message for non-arrays is specialized appropriately.
290 procedure Check_Asm_Attribute
;
291 -- Common semantic checks for Asm_Input and Asm_Output attributes
293 procedure Check_Component
;
294 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
295 -- Position. Checks prefix is an appropriate selected component.
297 procedure Check_Decimal_Fixed_Point_Type
;
298 -- Check that prefix of attribute N is a decimal fixed-point type
300 procedure Check_Dereference
;
301 -- If the prefix of attribute is an object of an access type, then
302 -- introduce an explicit dereference, and adjust P_Type accordingly.
304 procedure Check_Discrete_Type
;
305 -- Verify that prefix of attribute N is a discrete type
308 -- Check that no attribute arguments are present
310 procedure Check_Either_E0_Or_E1
;
311 -- Check that there are zero or one attribute arguments present
314 -- Check that exactly one attribute argument is present
317 -- Check that two attribute arguments are present
319 procedure Check_Enum_Image
;
320 -- If the prefix type of 'Image is an enumeration type, set all its
321 -- literals as referenced, since the image function could possibly end
322 -- up referencing any of the literals indirectly. Same for Enum_Val.
323 -- Set the flag only if the reference is in the main code unit. Same
324 -- restriction when resolving 'Value; otherwise an improperly set
325 -- reference when analyzing an inlined body will lose a proper
326 -- warning on a useless with_clause.
328 procedure Check_First_Last_Valid
;
329 -- Perform all checks for First_Valid and Last_Valid attributes
331 procedure Check_Fixed_Point_Type
;
332 -- Verify that prefix of attribute N is a fixed type
334 procedure Check_Fixed_Point_Type_0
;
335 -- Verify that prefix of attribute N is a fixed type and that
336 -- no attribute expressions are present.
338 procedure Check_Floating_Point_Type
;
339 -- Verify that prefix of attribute N is a float type
341 procedure Check_Floating_Point_Type_0
;
342 -- Verify that prefix of attribute N is a float type and that
343 -- no attribute expressions are present.
345 procedure Check_Floating_Point_Type_1
;
346 -- Verify that prefix of attribute N is a float type and that
347 -- exactly one attribute expression is present.
349 procedure Check_Floating_Point_Type_2
;
350 -- Verify that prefix of attribute N is a float type and that
351 -- two attribute expressions are present
353 procedure Check_Integer_Type
;
354 -- Verify that prefix of attribute N is an integer type
356 procedure Check_Modular_Integer_Type
;
357 -- Verify that prefix of attribute N is a modular integer type
359 procedure Check_Not_CPP_Type
;
360 -- Check that P (the prefix of the attribute) is not an CPP type
361 -- for which no Ada predefined primitive is available.
363 procedure Check_Not_Incomplete_Type
;
364 -- Check that P (the prefix of the attribute) is not an incomplete
365 -- type or a private type for which no full view has been given.
367 procedure Check_Object_Reference
(P
: Node_Id
);
368 -- Check that P is an object reference
370 procedure Check_PolyORB_Attribute
;
371 -- Validity checking for PolyORB/DSA attribute
373 procedure Check_Program_Unit
;
374 -- Verify that prefix of attribute N is a program unit
376 procedure Check_Real_Type
;
377 -- Verify that prefix of attribute N is fixed or float type
379 procedure Check_Scalar_Type
;
380 -- Verify that prefix of attribute N is a scalar type
382 procedure Check_Standard_Prefix
;
383 -- Verify that prefix of attribute N is package Standard. Also checks
384 -- that there are no arguments.
386 procedure Check_Stream_Attribute
(Nam
: TSS_Name_Type
);
387 -- Validity checking for stream attribute. Nam is the TSS name of the
388 -- corresponding possible defined attribute function (e.g. for the
389 -- Read attribute, Nam will be TSS_Stream_Read).
391 procedure Check_Put_Image_Attribute
;
392 -- Validity checking for Put_Image attribute
394 procedure Check_System_Prefix
;
395 -- Verify that prefix of attribute N is package System
397 procedure Check_Task_Prefix
;
398 -- Verify that prefix of attribute N is a task or task type
400 procedure Check_Type
;
401 -- Verify that the prefix of attribute N is a type
403 procedure Check_Unit_Name
(Nod
: Node_Id
);
404 -- Check that Nod is of the form of a library unit name, i.e that
405 -- it is an identifier, or a selected component whose prefix is
406 -- itself of the form of a library unit name. Note that this is
407 -- quite different from Check_Program_Unit, since it only checks
408 -- the syntactic form of the name, not the semantic identity. This
409 -- is because it is used with attributes (Elab_Body, Elab_Spec and
410 -- Elaborated) which can refer to non-visible unit.
412 procedure Error_Attr
(Msg
: String; Error_Node
: Node_Id
);
413 pragma No_Return
(Error_Attr
);
414 procedure Error_Attr
;
415 pragma No_Return
(Error_Attr
);
416 -- Posts error using Error_Msg_N at given node, sets type of attribute
417 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
418 -- semantic processing. The message typically contains a % insertion
419 -- character which is replaced by the attribute name. The call with
420 -- no arguments is used when the caller has already generated the
421 -- required error messages.
423 procedure Error_Attr_P
(Msg
: String);
424 pragma No_Return
(Error_Attr_P
);
425 -- Like Error_Attr, but error is posted at the start of the prefix
427 procedure Legal_Formal_Attribute
;
428 -- Common processing for attributes Definite and Has_Discriminants.
429 -- Checks that prefix is generic indefinite formal type.
431 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
432 -- Common processing for attributes Max_Alignment_For_Allocation and
433 -- Max_Size_In_Storage_Elements.
436 -- Common processing for attributes Max and Min
438 procedure Standard_Attribute
(Val
: Int
);
439 -- Used to process attributes whose prefix is package Standard which
440 -- yield values of type Universal_Integer. The attribute reference
441 -- node is rewritten with an integer literal of the given value which
442 -- is marked as static.
444 procedure Uneval_Old_Msg
;
445 -- Called when Loop_Entry or Old is used in a potentially unevaluated
446 -- expression. Generates appropriate message or warning depending on
447 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
448 -- node in the aspect case).
450 procedure Unexpected_Argument
(En
: Node_Id
);
451 pragma No_Return
(Unexpected_Argument
);
452 -- Signal unexpected attribute argument (En is the argument), and then
453 -- raises Bad_Attribute to avoid any further semantic processing.
455 procedure Validate_Non_Static_Attribute_Function_Call
;
456 -- Called when processing an attribute that is a function call to a
457 -- non-static function, i.e. an attribute function that either takes
458 -- non-scalar arguments or returns a non-scalar result. Verifies that
459 -- such a call does not appear in a preelaborable context.
465 procedure Address_Checks
is
467 -- An Address attribute created by expansion is legal even when it
468 -- applies to other entity-denoting expressions.
470 if not Comes_From_Source
(N
) then
473 -- Address attribute on a protected object self reference is legal
475 elsif Is_Protected_Self_Reference
(P
) then
478 -- Address applied to an entity
480 elsif Is_Entity_Name
(P
) then
482 Ent
: constant Entity_Id
:= Entity
(P
);
485 if Is_Subprogram
(Ent
) then
486 Set_Address_Taken
(Ent
);
487 Kill_Current_Values
(Ent
);
489 -- An Address attribute is accepted when generated by the
490 -- compiler for dispatching operation, and an error is
491 -- issued once the subprogram is frozen (to avoid confusing
492 -- errors about implicit uses of Address in the dispatch
493 -- table initialization).
495 if Has_Pragma_Inline_Always
(Entity
(P
))
496 and then Comes_From_Source
(P
)
499 ("prefix of % attribute cannot be Inline_Always "
502 -- It is illegal to apply 'Address to an intrinsic
503 -- subprogram. This is now formalized in AI05-0095.
504 -- In an instance, an attempt to obtain 'Address of an
505 -- intrinsic subprogram (e.g the renaming of a predefined
506 -- operator that is an actual) raises Program_Error.
508 elsif Convention
(Ent
) = Convention_Intrinsic
then
511 Make_Raise_Program_Error
(Loc
,
512 Reason
=> PE_Address_Of_Intrinsic
));
515 Error_Msg_Name_1
:= Aname
;
517 ("cannot take % of intrinsic subprogram", N
);
520 -- Issue an error if prefix denotes an eliminated subprogram
523 Check_For_Eliminated_Subprogram
(P
, Ent
);
526 -- Object or label reference
528 elsif Is_Object_Reference
(P
) or else Ekind
(Ent
) = E_Label
then
529 Set_Address_Taken
(Ent
);
531 -- Deal with No_Implicit_Aliasing restriction
533 if Restriction_Check_Required
(No_Implicit_Aliasing
) then
534 if not Is_Aliased_View
(P
) then
535 Check_Restriction
(No_Implicit_Aliasing
, P
);
537 Check_No_Implicit_Aliasing
(P
);
541 -- If we have an address of an object, and the attribute
542 -- comes from source, then set the object as potentially
543 -- source modified. We do this because the resulting address
544 -- can potentially be used to modify the variable and we
545 -- might not detect this, leading to some junk warnings.
547 Set_Never_Set_In_Source
(Ent
, False);
549 -- Allow Address to be applied to task or protected type,
550 -- returning null address (what is that about???)
552 elsif (Is_Concurrent_Type
(Etype
(Ent
))
553 and then Etype
(Ent
) = Base_Type
(Ent
))
554 or else Ekind
(Ent
) = E_Package
555 or else Is_Generic_Unit
(Ent
)
558 New_Occurrence_Of
(RTE
(RE_Null_Address
), Sloc
(N
)));
560 -- Anything else is illegal
563 Error_Attr
("invalid prefix for % attribute", P
);
569 elsif Is_Object_Reference
(P
) then
572 -- Subprogram called using dot notation
574 elsif Nkind
(P
) = N_Selected_Component
575 and then Is_Subprogram
(Entity
(Selector_Name
(P
)))
579 -- What exactly are we allowing here ??? and is this properly
580 -- documented in the sinfo documentation for this node ???
582 elsif Relaxed_RM_Semantics
583 and then Nkind
(P
) = N_Attribute_Reference
587 -- All other non-entity name cases are illegal
590 Error_Attr
("invalid prefix for % attribute", P
);
594 ------------------------------
595 -- Analyze_Access_Attribute --
596 ------------------------------
598 procedure Analyze_Access_Attribute
is
599 Acc_Type
: Entity_Id
;
604 function Build_Access_Object_Type
(DT
: Entity_Id
) return Entity_Id
;
605 -- Build an access-to-object type whose designated type is DT,
606 -- and whose Ekind is appropriate to the attribute type. The
607 -- type that is constructed is returned as the result.
609 procedure Build_Access_Subprogram_Type
(P
: Node_Id
);
610 -- Build an access to subprogram whose designated type is the type of
611 -- the prefix. If prefix is overloaded, so is the node itself. The
612 -- result is stored in Acc_Type.
614 function OK_Self_Reference
return Boolean;
615 -- An access reference whose prefix is a type can legally appear
616 -- within an aggregate, where it is obtained by expansion of
617 -- a defaulted aggregate. The enclosing aggregate that contains
618 -- the self-referenced is flagged so that the self-reference can
619 -- be expanded into a reference to the target object (see exp_aggr).
621 ------------------------------
622 -- Build_Access_Object_Type --
623 ------------------------------
625 function Build_Access_Object_Type
(DT
: Entity_Id
) return Entity_Id
is
626 Typ
: constant Entity_Id
:=
628 (E_Access_Attribute_Type
, Current_Scope
, Loc
, 'A');
630 Set_Etype
(Typ
, Typ
);
632 Set_Associated_Node_For_Itype
(Typ
, N
);
633 Set_Directly_Designated_Type
(Typ
, DT
);
635 end Build_Access_Object_Type
;
637 ----------------------------------
638 -- Build_Access_Subprogram_Type --
639 ----------------------------------
641 procedure Build_Access_Subprogram_Type
(P
: Node_Id
) is
642 Index
: Interp_Index
;
645 procedure Check_Local_Access
(E
: Entity_Id
);
646 -- Deal with possible access to local subprogram. If we have such
647 -- an access, we set a flag to kill all tracked values on any call
648 -- because this access value may be passed around, and any called
649 -- code might use it to access a local procedure which clobbers a
650 -- tracked value. If the scope is a loop or block, indicate that
651 -- value tracking is disabled for the enclosing subprogram.
653 function Get_Convention
(E
: Entity_Id
) return Convention_Id
;
654 function Get_Kind
(E
: Entity_Id
) return Entity_Kind
;
655 -- Distinguish between access to regular/protected subprograms
657 ------------------------
658 -- Check_Local_Access --
659 ------------------------
661 procedure Check_Local_Access
(E
: Entity_Id
) is
663 if not Is_Library_Level_Entity
(E
) then
664 Set_Suppress_Value_Tracking_On_Call
(Current_Scope
);
665 Set_Suppress_Value_Tracking_On_Call
666 (Nearest_Dynamic_Scope
(Current_Scope
));
668 end Check_Local_Access
;
674 function Get_Convention
(E
: Entity_Id
) return Convention_Id
is
676 -- Restrict handling by_protected_procedure access subprograms
677 -- to source entities; required to avoid building access to
678 -- subprogram types with convention protected when building
681 if Comes_From_Source
(P
)
682 and then Is_By_Protected_Procedure
(E
)
684 return Convention_Protected
;
686 return Convention
(E
);
694 function Get_Kind
(E
: Entity_Id
) return Entity_Kind
is
696 if Get_Convention
(E
) = Convention_Protected
then
697 return E_Access_Protected_Subprogram_Type
;
699 return E_Access_Subprogram_Type
;
703 -- Start of processing for Build_Access_Subprogram_Type
706 -- In the case of an access to subprogram, use the name of the
707 -- subprogram itself as the designated type. Type-checking in
708 -- this case compares the signatures of the designated types.
710 -- Note: This fragment of the tree is temporarily malformed
711 -- because the correct tree requires an E_Subprogram_Type entity
712 -- as the designated type. In most cases this designated type is
713 -- later overridden by the semantics with the type imposed by the
714 -- context during the resolution phase. In the specific case of
715 -- the expression Address!(Prim'Unrestricted_Access), used to
716 -- initialize slots of dispatch tables, this work will be done by
717 -- the expander (see Exp_Aggr).
719 -- The reason to temporarily add this kind of node to the tree
720 -- instead of a proper E_Subprogram_Type itype, is the following:
721 -- in case of errors found in the source file we report better
722 -- error messages. For example, instead of generating the
725 -- "expected access to subprogram with profile
726 -- defined at line X"
728 -- we currently generate:
730 -- "expected access to function Z defined at line X"
732 Set_Etype
(N
, Any_Type
);
734 if not Is_Overloaded
(P
) then
735 Check_Local_Access
(Entity
(P
));
737 if not Is_Intrinsic_Subprogram
(Entity
(P
)) then
738 Acc_Type
:= Create_Itype
(Get_Kind
(Entity
(P
)), N
);
739 Set_Is_Public
(Acc_Type
, False);
740 Set_Etype
(Acc_Type
, Acc_Type
);
741 Set_Convention
(Acc_Type
, Get_Convention
(Entity
(P
)));
742 Set_Directly_Designated_Type
(Acc_Type
, Entity
(P
));
743 Set_Etype
(N
, Acc_Type
);
744 Freeze_Before
(N
, Acc_Type
);
748 Get_First_Interp
(P
, Index
, It
);
749 while Present
(It
.Nam
) loop
750 Check_Local_Access
(It
.Nam
);
752 if not Is_Intrinsic_Subprogram
(It
.Nam
) then
753 Acc_Type
:= Create_Itype
(Get_Kind
(It
.Nam
), N
);
754 Set_Is_Public
(Acc_Type
, False);
755 Set_Etype
(Acc_Type
, Acc_Type
);
756 Set_Convention
(Acc_Type
, Get_Convention
(It
.Nam
));
757 Set_Directly_Designated_Type
(Acc_Type
, It
.Nam
);
758 Add_One_Interp
(N
, Acc_Type
, Acc_Type
);
759 Freeze_Before
(N
, Acc_Type
);
762 Get_Next_Interp
(Index
, It
);
766 -- Cannot be applied to intrinsic. Looking at the tests above,
767 -- the only way Etype (N) can still be set to Any_Type is if
768 -- Is_Intrinsic_Subprogram was True for some referenced entity.
770 if Etype
(N
) = Any_Type
then
771 Error_Attr_P
("prefix of % attribute cannot be intrinsic");
773 end Build_Access_Subprogram_Type
;
775 ----------------------
776 -- OK_Self_Reference --
777 ----------------------
779 function OK_Self_Reference
return Boolean is
786 (Nkind
(Par
) = N_Component_Association
787 or else Nkind
(Par
) in N_Subexpr
)
789 if Nkind
(Par
) in N_Aggregate | N_Extension_Aggregate
then
790 if Etype
(Par
) = Typ
then
791 Set_Has_Self_Reference
(Par
);
793 -- Check the context: the aggregate must be part of the
794 -- initialization of a type or component, or it is the
795 -- resulting expansion in an initialization procedure.
797 if Is_Init_Proc
(Current_Scope
) then
801 while Present
(Par
) loop
802 if Nkind
(Par
) = N_Full_Type_Declaration
then
817 -- No enclosing aggregate, or not a self-reference
820 end OK_Self_Reference
;
822 -- Start of processing for Analyze_Access_Attribute
825 -- Access and Unchecked_Access are illegal in declare_expressions,
826 -- according to the RM. We also make the GNAT-specific
827 -- Unrestricted_Access attribute illegal.
829 if In_Declare_Expr
> 0 then
830 Error_Attr
("% attribute cannot occur in a declare_expression", N
);
835 if Nkind
(P
) = N_Character_Literal
then
837 ("prefix of % attribute cannot be enumeration literal");
840 -- Preserve relevant elaboration-related attributes of the context
841 -- which are no longer available or very expensive to recompute once
842 -- analysis, resolution, and expansion are over.
844 Mark_Elaboration_Attributes
850 -- Save the scenario for later examination by the ABE Processing
853 Record_Elaboration_Scenario
(N
);
855 -- Case of access to subprogram
857 if Is_Entity_Name
(P
) and then Is_Overloadable
(Entity
(P
)) then
858 if Has_Pragma_Inline_Always
(Entity
(P
)) then
860 ("prefix of % attribute cannot be Inline_Always subprogram");
862 elsif Aname
= Name_Unchecked_Access
then
863 Error_Attr
("attribute% cannot be applied to a subprogram", P
);
866 -- Issue an error if the prefix denotes an eliminated subprogram
868 Check_For_Eliminated_Subprogram
(P
, Entity
(P
));
870 -- Check for obsolescent subprogram reference
872 Check_Obsolescent_2005_Entity
(Entity
(P
), P
);
874 -- Build the appropriate subprogram type
876 Build_Access_Subprogram_Type
(P
);
878 -- For P'Access or P'Unrestricted_Access, where P is a nested
879 -- subprogram, we might be passing P to another subprogram (but we
880 -- don't check that here), which might call P. P could modify
881 -- local variables, so we need to kill current values. It is
882 -- important not to do this for library-level subprograms, because
883 -- Kill_Current_Values is very inefficient in the case of library
884 -- level packages with lots of tagged types.
886 if Is_Library_Level_Entity
(Entity
(Prefix
(N
))) then
889 -- Do not kill values on nodes initializing dispatch tables
890 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
891 -- is currently generated by the expander only for this
892 -- purpose. Done to keep the quality of warnings currently
893 -- generated by the compiler (otherwise any declaration of
894 -- a tagged type cleans constant indications from its scope).
896 elsif Nkind
(Parent
(N
)) = N_Unchecked_Type_Conversion
897 and then (Etype
(Parent
(N
)) = RTE
(RE_Prim_Ptr
)
899 Etype
(Parent
(N
)) = RTE
(RE_Size_Ptr
))
900 and then Is_Dispatching_Operation
901 (Directly_Designated_Type
(Etype
(N
)))
909 -- In the static elaboration model, treat the attribute reference
910 -- as a subprogram call for elaboration purposes. Suppress this
911 -- treatment under debug flag. In any case, we are all done.
913 if Legacy_Elaboration_Checks
914 and not Dynamic_Elaboration_Checks
915 and not Debug_Flag_Dot_UU
922 -- Component is an operation of a protected type
924 elsif Nkind
(P
) = N_Selected_Component
925 and then Is_Overloadable
(Entity
(Selector_Name
(P
)))
927 if Ekind
(Entity
(Selector_Name
(P
))) = E_Entry
then
928 Error_Attr_P
("prefix of % attribute must be subprogram");
931 Build_Access_Subprogram_Type
(Selector_Name
(P
));
935 -- Deal with incorrect reference to a type, but note that some
936 -- accesses are allowed: references to the current type instance,
937 -- or in Ada 2005 self-referential pointer in a default-initialized
940 if Is_Entity_Name
(P
) then
943 -- The reference may appear in an aggregate that has been expanded
944 -- into a loop. Locate scope of type definition, if any.
946 Scop
:= Current_Scope
;
947 while Ekind
(Scop
) = E_Loop
loop
948 Scop
:= Scope
(Scop
);
951 if Is_Type
(Typ
) then
953 -- OK if we are within the scope of a limited type
954 -- let's mark the component as having per object constraint
956 if Is_Anonymous_Tagged_Base
(Scop
, Typ
) then
964 Q
: Node_Id
:= Parent
(N
);
968 and then Nkind
(Q
) /= N_Component_Declaration
974 Set_Has_Per_Object_Constraint
975 (Defining_Identifier
(Q
), True);
979 if Nkind
(P
) = N_Expanded_Name
then
981 ("current instance prefix must be a direct name", P
);
984 -- If a current instance attribute appears in a component
985 -- constraint it must appear alone; other contexts (spec-
986 -- expressions, within a task body) are not subject to this
989 if not In_Spec_Expression
990 and then not Has_Completion
(Scop
)
992 Nkind
(Parent
(N
)) not in
993 N_Discriminant_Association |
994 N_Index_Or_Discriminant_Constraint
997 ("current instance attribute must appear alone", N
);
1000 if Is_CPP_Class
(Root_Type
(Typ
)) then
1002 ("??current instance unsupported for derivations of "
1003 & "'C'P'P types", N
);
1006 -- OK if we are in initialization procedure for the type
1007 -- in question, in which case the reference to the type
1008 -- is rewritten as a reference to the current object.
1010 elsif Ekind
(Scop
) = E_Procedure
1011 and then Is_Init_Proc
(Scop
)
1012 and then Etype
(First_Formal
(Scop
)) = Typ
1015 Make_Attribute_Reference
(Loc
,
1016 Prefix
=> Make_Identifier
(Loc
, Name_uInit
),
1017 Attribute_Name
=> Name_Unrestricted_Access
));
1021 -- OK if a task type, this test needs sharpening up ???
1023 elsif Is_Task_Type
(Typ
) then
1026 -- OK if self-reference in an aggregate in Ada 2005, and
1027 -- the reference comes from a copied default expression.
1029 -- Note that we check legality of self-reference even if the
1030 -- expression comes from source, e.g. when a single component
1031 -- association in an aggregate has a box association.
1033 elsif Ada_Version
>= Ada_2005
1034 and then OK_Self_Reference
1038 -- OK if reference to current instance of a protected object
1040 elsif Is_Protected_Self_Reference
(P
) then
1043 -- Otherwise we have an error case
1046 Error_Attr
("% attribute cannot be applied to type", P
);
1052 -- If we fall through, we have a normal access to object case
1054 -- Unrestricted_Access is (for now) legal wherever an allocator would
1055 -- be legal, so its Etype is set to E_Allocator. The expected type
1056 -- of the other attributes is a general access type, and therefore
1057 -- we label them with E_Access_Attribute_Type.
1059 if not Is_Overloaded
(P
) then
1060 Acc_Type
:= Build_Access_Object_Type
(P_Type
);
1061 Set_Etype
(N
, Acc_Type
);
1065 Index
: Interp_Index
;
1068 Set_Etype
(N
, Any_Type
);
1069 Get_First_Interp
(P
, Index
, It
);
1070 while Present
(It
.Typ
) loop
1071 Acc_Type
:= Build_Access_Object_Type
(It
.Typ
);
1072 Add_One_Interp
(N
, Acc_Type
, Acc_Type
);
1073 Get_Next_Interp
(Index
, It
);
1078 -- Special cases when we can find a prefix that is an entity name
1087 if Is_Entity_Name
(PP
) then
1090 -- If we have an access to an object, and the attribute
1091 -- comes from source, then set the object as potentially
1092 -- source modified. We do this because the resulting access
1093 -- pointer can be used to modify the variable, and we might
1094 -- not detect this, leading to some junk warnings.
1096 -- We only do this for source references, since otherwise
1097 -- we can suppress warnings, e.g. from the unrestricted
1098 -- access generated for validity checks in -gnatVa mode.
1100 if Comes_From_Source
(N
) then
1101 Set_Never_Set_In_Source
(Ent
, False);
1104 -- Mark entity as address taken in the case of
1105 -- 'Unrestricted_Access or subprograms, and kill current
1108 if Aname
= Name_Unrestricted_Access
1109 or else Is_Subprogram
(Ent
)
1111 Set_Address_Taken
(Ent
);
1114 Kill_Current_Values
(Ent
);
1117 elsif Nkind
(PP
) in N_Selected_Component | N_Indexed_Component
1126 end Analyze_Access_Attribute
;
1128 ----------------------------------
1129 -- Analyze_Attribute_Old_Result --
1130 ----------------------------------
1132 procedure Analyze_Attribute_Old_Result
1133 (Legal
: out Boolean;
1134 Spec_Id
: out Entity_Id
)
1136 procedure Check_Placement_In_Check
(Prag
: Node_Id
);
1137 -- Verify that the attribute appears within pragma Check that mimics
1140 procedure Check_Placement_In_Contract_Cases
(Prag
: Node_Id
);
1141 -- Verify that the attribute appears within a consequence of aspect
1142 -- or pragma Contract_Cases denoted by Prag.
1144 procedure Check_Placement_In_Test_Case
(Prag
: Node_Id
);
1145 -- Verify that the attribute appears within the "Ensures" argument of
1146 -- aspect or pragma Test_Case denoted by Prag.
1150 Encl_Nod
: Node_Id
) return Boolean;
1151 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1152 -- node Nod is within enclosing node Encl_Nod.
1154 procedure Placement_Error
;
1155 pragma No_Return
(Placement_Error
);
1156 -- Emit a general error when the attributes does not appear in a
1157 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1158 -- to avoid any further semantic processing.
1160 ------------------------------
1161 -- Check_Placement_In_Check --
1162 ------------------------------
1164 procedure Check_Placement_In_Check
(Prag
: Node_Id
) is
1165 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
1166 Nam
: constant Name_Id
:= Chars
(Get_Pragma_Arg
(First
(Args
)));
1169 -- The "Name" argument of pragma Check denotes a postcondition
1173 | Name_Postcondition
1178 -- Otherwise the placement of the attribute is illegal
1183 end Check_Placement_In_Check
;
1185 ---------------------------------------
1186 -- Check_Placement_In_Contract_Cases --
1187 ---------------------------------------
1189 procedure Check_Placement_In_Contract_Cases
(Prag
: Node_Id
) is
1195 -- Obtain the argument of the aspect or pragma
1197 if Nkind
(Prag
) = N_Aspect_Specification
then
1200 Arg
:= First
(Pragma_Argument_Associations
(Prag
));
1203 Cases
:= Expression
(Arg
);
1205 if Present
(Component_Associations
(Cases
)) then
1206 CCase
:= First
(Component_Associations
(Cases
));
1207 while Present
(CCase
) loop
1209 -- Detect whether the attribute appears within the
1210 -- consequence of the current contract case.
1212 if Nkind
(CCase
) = N_Component_Association
1213 and then Is_Within
(N
, Expression
(CCase
))
1222 -- Otherwise aspect or pragma Contract_Cases is either malformed
1223 -- or the attribute does not appear within a consequence.
1226 ("attribute % must appear in the consequence of a contract case",
1228 end Check_Placement_In_Contract_Cases
;
1230 ----------------------------------
1231 -- Check_Placement_In_Test_Case --
1232 ----------------------------------
1234 procedure Check_Placement_In_Test_Case
(Prag
: Node_Id
) is
1235 Arg
: constant Node_Id
:=
1238 Arg_Nam
=> Name_Ensures
,
1239 From_Aspect
=> Nkind
(Prag
) = N_Aspect_Specification
);
1242 -- Detect whether the attribute appears within the "Ensures"
1243 -- expression of aspect or pragma Test_Case.
1245 if Present
(Arg
) and then Is_Within
(N
, Arg
) then
1250 ("attribute % must appear in the ensures expression of a "
1253 end Check_Placement_In_Test_Case
;
1261 Encl_Nod
: Node_Id
) return Boolean
1267 while Present
(Par
) loop
1268 if Par
= Encl_Nod
then
1271 -- Prevent the search from going too far
1273 elsif Is_Body_Or_Package_Declaration
(Par
) then
1277 Par
:= Parent
(Par
);
1283 ---------------------
1284 -- Placement_Error --
1285 ---------------------
1287 procedure Placement_Error
is
1289 if Aname
= Name_Old
then
1290 Error_Attr
("attribute % can only appear in postcondition", P
);
1292 -- Specialize the error message for attribute 'Result
1296 ("attribute % can only appear in postcondition of function",
1299 end Placement_Error
;
1305 Subp_Decl
: Node_Id
;
1307 -- Start of processing for Analyze_Attribute_Old_Result
1310 -- Assume that the attribute is illegal
1315 -- Traverse the parent chain to find the aspect or pragma where the
1316 -- attribute resides.
1319 while Present
(Prag
) loop
1320 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1323 -- Prevent the search from going too far
1325 elsif Is_Body_Or_Package_Declaration
(Prag
) then
1329 Prag
:= Parent
(Prag
);
1332 -- The attribute is allowed to appear only in postcondition-like
1333 -- aspects or pragmas.
1335 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1336 if Nkind
(Prag
) = N_Aspect_Specification
then
1337 Prag_Nam
:= Chars
(Identifier
(Prag
));
1339 Prag_Nam
:= Pragma_Name
(Prag
);
1342 if Prag_Nam
= Name_Check
then
1343 Check_Placement_In_Check
(Prag
);
1345 elsif Prag_Nam
= Name_Contract_Cases
then
1346 Check_Placement_In_Contract_Cases
(Prag
);
1348 -- Attribute 'Result is allowed to appear in aspect or pragma
1349 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1351 elsif Prag_Nam
in Name_Depends | Name_Refined_Depends
1352 and then Aname
= Name_Result
1356 -- Attribute 'Result is allowed to appear in aspect
1357 -- Relaxed_Initialization (SPARK RM 6.10).
1359 elsif Prag_Nam
= Name_Relaxed_Initialization
1360 and then Aname
= Name_Result
1364 elsif Prag_Nam
in Name_Post
1366 | Name_Postcondition
1371 elsif Prag_Nam
= Name_Test_Case
then
1372 Check_Placement_In_Test_Case
(Prag
);
1379 -- Otherwise the placement of the attribute is illegal
1386 -- Find the related subprogram subject to the aspect or pragma
1388 if Nkind
(Prag
) = N_Aspect_Specification
then
1389 Subp_Decl
:= Parent
(Prag
);
1391 Subp_Decl
:= Find_Related_Declaration_Or_Body
(Prag
);
1394 -- The aspect or pragma where the attribute resides should be
1395 -- associated with a subprogram declaration or a body. If this is not
1396 -- the case, then the aspect or pragma is illegal. Return as analysis
1397 -- cannot be carried out. Note that it is legal to have the aspect
1398 -- appear on a subprogram renaming, when the renamed entity is an
1399 -- attribute reference.
1401 -- Generating C code the internally built nested _postcondition
1402 -- subprograms are inlined; after expanded, inlined aspects are
1403 -- located in the internal block generated by the frontend.
1405 if Nkind
(Subp_Decl
) = N_Block_Statement
1406 and then Modify_Tree_For_C
1407 and then In_Inlined_Body
1411 elsif Nkind
(Subp_Decl
) not in N_Abstract_Subprogram_Declaration
1412 | N_Entry_Declaration
1413 | N_Expression_Function
1414 | N_Generic_Subprogram_Declaration
1416 | N_Subprogram_Body_Stub
1417 | N_Subprogram_Declaration
1418 | N_Subprogram_Renaming_Declaration
1423 -- If we get here, then the attribute is legal
1426 Spec_Id
:= Unique_Defining_Entity
(Subp_Decl
);
1428 -- When generating C code, nested _postcondition subprograms are
1429 -- inlined by the front end to avoid problems (when unnested) with
1430 -- referenced itypes. Handle that here, since as part of inlining the
1431 -- expander nests subprogram within a dummy procedure named _parent
1432 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1433 -- Hence, in this context, the spec_id of _postconditions is the
1436 if Modify_Tree_For_C
1437 and then Chars
(Spec_Id
) = Name_uParent
1438 and then Chars
(Scope
(Spec_Id
)) = Name_uPostconditions
1440 -- This situation occurs only when preanalyzing the inlined body
1442 pragma Assert
(not Full_Analysis
);
1444 Spec_Id
:= Scope
(Spec_Id
);
1445 pragma Assert
(Is_Inlined
(Spec_Id
));
1447 end Analyze_Attribute_Old_Result
;
1449 -----------------------------
1450 -- Analyze_Image_Attribute --
1451 -----------------------------
1453 procedure Analyze_Image_Attribute
(Str_Typ
: Entity_Id
) is
1454 procedure Check_Image_Type
(Image_Type
: Entity_Id
);
1455 -- Check that Image_Type is legal as the type of a prefix of 'Image.
1456 -- Legality depends on the Ada language version.
1458 procedure Check_Image_Type
(Image_Type
: Entity_Id
) is
1460 if Ada_Version
< Ada_2020
1461 and then not Is_Scalar_Type
(Image_Type
)
1463 Error_Msg_Ada_2020_Feature
("nonscalar ''Image", Sloc
(P
));
1466 end Check_Image_Type
;
1468 -- Start of processing for Analyze_Image_Attribute
1471 -- AI12-0124: The ARG has adopted the GNAT semantics of 'Img for
1472 -- scalar types, so that the prefix can be an object, a named value,
1473 -- or a type. If the prefix is an object, there is no argument.
1475 if Is_Object_Image
(P
) then
1477 Set_Etype
(N
, Str_Typ
);
1478 Check_Image_Type
(Etype
(P
));
1480 if Attr_Id
/= Attribute_Img
and then Ada_Version
< Ada_2012
then
1481 Error_Msg_Ada_2012_Feature
("|Object''Image", Sloc
(P
));
1485 Set_Etype
(N
, Str_Typ
);
1487 -- ???It's not clear why 'Img should behave any differently than
1490 if Attr_Id
= Attribute_Img
then
1492 ("prefix of % attribute must be a scalar object name");
1495 pragma Assert
(Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)));
1497 if Ekind
(Entity
(P
)) = E_Incomplete_Type
1498 and then Present
(Full_View
(Entity
(P
)))
1500 P_Type
:= Full_View
(Entity
(P
));
1501 P_Base_Type
:= Base_Type
(P_Type
);
1502 Set_Entity
(P
, P_Type
);
1505 Check_Image_Type
(P_Type
);
1506 Resolve
(E1
, P_Base_Type
);
1507 Validate_Non_Static_Attribute_Function_Call
;
1512 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1513 -- to avoid giving a duplicate message for when Image attributes
1514 -- applied to object references get expanded into type-based Image
1517 if Restriction_Check_Required
(No_Fixed_IO
)
1518 and then Comes_From_Source
(N
)
1519 and then Is_Fixed_Point_Type
(P_Type
)
1521 Check_Restriction
(No_Fixed_IO
, P
);
1523 end Analyze_Image_Attribute
;
1525 ---------------------------------
1526 -- Bad_Attribute_For_Predicate --
1527 ---------------------------------
1529 procedure Bad_Attribute_For_Predicate
is
1531 if Is_Scalar_Type
(P_Type
)
1532 and then Comes_From_Source
(N
)
1534 Error_Msg_Name_1
:= Aname
;
1535 Bad_Predicated_Subtype_Use
1536 ("type& has predicates, attribute % not allowed", N
, P_Type
);
1538 end Bad_Attribute_For_Predicate
;
1540 --------------------------------
1541 -- Check_Array_Or_Scalar_Type --
1542 --------------------------------
1544 procedure Check_Array_Or_Scalar_Type
is
1545 function In_Aspect_Specification
return Boolean;
1546 -- A current instance of a type in an aspect specification is an
1547 -- object and not a type, and therefore cannot be of a scalar type
1548 -- in the prefix of one of the array attributes if the attribute
1549 -- reference is part of an aspect expression.
1551 -----------------------------
1552 -- In_Aspect_Specification --
1553 -----------------------------
1555 function In_Aspect_Specification
return Boolean is
1560 while Present
(P
) loop
1561 if Nkind
(P
) = N_Aspect_Specification
then
1562 return P_Type
= Entity
(P
);
1564 elsif Nkind
(P
) in N_Declaration
then
1572 end In_Aspect_Specification
;
1579 -- Start of processing for Check_Array_Or_Scalar_Type
1582 -- Case of string literal or string literal subtype. These cases
1583 -- cannot arise from legal Ada code, but the expander is allowed
1584 -- to generate them. They require special handling because string
1585 -- literal subtypes do not have standard bounds (the whole idea
1586 -- of these subtypes is to avoid having to generate the bounds)
1588 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1589 Set_Etype
(N
, Etype
(First_Index
(P_Base_Type
)));
1594 elsif Is_Scalar_Type
(P_Type
) then
1597 if Present
(E1
) then
1598 Error_Attr
("invalid argument in % attribute", E1
);
1600 elsif In_Aspect_Specification
then
1602 ("prefix of % attribute cannot be the current instance of a "
1603 & "scalar type", P
);
1606 Set_Etype
(N
, P_Base_Type
);
1610 -- The following is a special test to allow 'First to apply to
1611 -- private scalar types if the attribute comes from generated
1612 -- code. This occurs in the case of Normalize_Scalars code.
1614 elsif Is_Private_Type
(P_Type
)
1615 and then Present
(Full_View
(P_Type
))
1616 and then Is_Scalar_Type
(Full_View
(P_Type
))
1617 and then not Comes_From_Source
(N
)
1619 Set_Etype
(N
, Implementation_Base_Type
(P_Type
));
1621 -- Array types other than string literal subtypes handled above
1626 -- We know prefix is an array type, or the name of an array
1627 -- object, and that the expression, if present, is static
1628 -- and within the range of the dimensions of the type.
1630 pragma Assert
(Is_Array_Type
(P_Type
));
1631 Index
:= First_Index
(P_Base_Type
);
1635 -- First dimension assumed
1637 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1640 Dims
:= UI_To_Int
(Intval
(E1
));
1642 for J
in 1 .. Dims
- 1 loop
1646 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1647 Set_Etype
(E1
, Standard_Integer
);
1650 end Check_Array_Or_Scalar_Type
;
1652 ----------------------
1653 -- Check_Array_Type --
1654 ----------------------
1656 procedure Check_Array_Type
is
1658 -- Dimension number for array attributes
1661 -- If the type is a string literal type, then this must be generated
1662 -- internally, and no further check is required on its legality.
1664 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1667 -- If the type is a composite, it is an illegal aggregate, no point
1670 elsif P_Type
= Any_Composite
then
1671 raise Bad_Attribute
;
1674 -- Normal case of array type or subtype. Note that if the
1675 -- prefix is a current instance of a type declaration it
1676 -- appears within an aspect specification and is legal.
1678 Check_Either_E0_Or_E1
;
1681 if Is_Array_Type
(P_Type
) then
1682 if not Is_Constrained
(P_Type
)
1683 and then Is_Entity_Name
(P
)
1684 and then Is_Type
(Entity
(P
))
1685 and then not Is_Current_Instance
(P
)
1687 -- Note: we do not call Error_Attr here, since we prefer to
1688 -- continue, using the relevant index type of the array,
1689 -- even though it is unconstrained. This gives better error
1690 -- recovery behavior.
1692 Error_Msg_Name_1
:= Aname
;
1694 ("prefix for % attribute must be constrained array", P
);
1697 -- The attribute reference freezes the type, and thus the
1698 -- component type, even if the attribute may not depend on the
1699 -- component. Diagnose arrays with incomplete components now.
1700 -- If the prefix is an access to array, this does not freeze
1701 -- the designated type.
1703 if Nkind
(P
) /= N_Explicit_Dereference
then
1704 Check_Fully_Declared
(Component_Type
(P_Type
), P
);
1707 D
:= Number_Dimensions
(P_Type
);
1710 if Is_Private_Type
(P_Type
) then
1711 Error_Attr_P
("prefix for % attribute may not be private type");
1713 elsif Is_Access_Type
(P_Type
)
1714 and then Is_Array_Type
(Designated_Type
(P_Type
))
1715 and then Is_Entity_Name
(P
)
1716 and then Is_Type
(Entity
(P
))
1718 Error_Attr_P
("prefix of % attribute cannot be access type");
1720 elsif Attr_Id
= Attribute_First
1722 Attr_Id
= Attribute_Last
1724 Error_Attr
("invalid prefix for % attribute", P
);
1727 Error_Attr_P
("prefix for % attribute must be array");
1731 if Present
(E1
) then
1732 Resolve
(E1
, Any_Integer
);
1733 Set_Etype
(E1
, Standard_Integer
);
1735 if not Is_OK_Static_Expression
(E1
)
1736 or else Raises_Constraint_Error
(E1
)
1738 Flag_Non_Static_Expr
1739 ("expression for dimension must be static!", E1
);
1742 elsif UI_To_Int
(Expr_Value
(E1
)) > D
1743 or else UI_To_Int
(Expr_Value
(E1
)) < 1
1745 Error_Attr
("invalid dimension number for array type", E1
);
1749 if (Style_Check
and Style_Check_Array_Attribute_Index
)
1750 and then Comes_From_Source
(N
)
1752 Style
.Check_Array_Attribute_Index
(N
, E1
, D
);
1754 end Check_Array_Type
;
1756 -------------------------
1757 -- Check_Asm_Attribute --
1758 -------------------------
1760 procedure Check_Asm_Attribute
is
1765 -- Check first argument is static string expression
1767 Analyze_And_Resolve
(E1
, Standard_String
);
1769 if Etype
(E1
) = Any_Type
then
1772 elsif not Is_OK_Static_Expression
(E1
) then
1773 Flag_Non_Static_Expr
1774 ("constraint argument must be static string expression!", E1
);
1778 -- Check second argument is right type
1780 Analyze_And_Resolve
(E2
, Entity
(P
));
1782 -- Note: that is all we need to do, we don't need to check
1783 -- that it appears in a correct context. The Ada type system
1784 -- will do that for us.
1786 end Check_Asm_Attribute
;
1788 ---------------------
1789 -- Check_Component --
1790 ---------------------
1792 procedure Check_Component
is
1796 if Nkind
(P
) /= N_Selected_Component
1798 (Ekind
(Entity
(Selector_Name
(P
))) /= E_Component
1800 Ekind
(Entity
(Selector_Name
(P
))) /= E_Discriminant
)
1802 Error_Attr_P
("prefix for % attribute must be selected component");
1804 end Check_Component
;
1806 ------------------------------------
1807 -- Check_Decimal_Fixed_Point_Type --
1808 ------------------------------------
1810 procedure Check_Decimal_Fixed_Point_Type
is
1814 if not Is_Decimal_Fixed_Point_Type
(P_Type
) then
1815 Error_Attr_P
("prefix of % attribute must be decimal type");
1817 end Check_Decimal_Fixed_Point_Type
;
1819 -----------------------
1820 -- Check_Dereference --
1821 -----------------------
1823 procedure Check_Dereference
is
1826 -- Case of a subtype mark
1828 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
1832 -- Case of an expression
1836 if Is_Access_Type
(P_Type
) then
1838 -- If there is an implicit dereference, then we must freeze the
1839 -- designated type of the access type, since the type of the
1840 -- referenced array is this type (see AI95-00106).
1842 -- As done elsewhere, freezing must not happen when preanalyzing
1843 -- a pre- or postcondition or a default value for an object or for
1844 -- a formal parameter.
1846 if not In_Spec_Expression
then
1847 Freeze_Before
(N
, Designated_Type
(P_Type
));
1851 Make_Explicit_Dereference
(Sloc
(P
),
1852 Prefix
=> Relocate_Node
(P
)));
1854 Analyze_And_Resolve
(P
);
1855 P_Type
:= Etype
(P
);
1857 if P_Type
= Any_Type
then
1858 raise Bad_Attribute
;
1861 P_Base_Type
:= Base_Type
(P_Type
);
1863 end Check_Dereference
;
1865 -------------------------
1866 -- Check_Discrete_Type --
1867 -------------------------
1869 procedure Check_Discrete_Type
is
1873 if not Is_Discrete_Type
(P_Type
) then
1874 Error_Attr_P
("prefix of % attribute must be discrete type");
1876 end Check_Discrete_Type
;
1882 procedure Check_E0
is
1884 if Present
(E1
) then
1885 Unexpected_Argument
(E1
);
1893 procedure Check_E1
is
1895 Check_Either_E0_Or_E1
;
1899 -- Special-case attributes that are functions and that appear as
1900 -- the prefix of another attribute. Error is posted on parent.
1902 if Nkind
(Parent
(N
)) = N_Attribute_Reference
1903 and then Attribute_Name
(Parent
(N
)) in Name_Address
1907 Error_Msg_Name_1
:= Attribute_Name
(Parent
(N
));
1908 Error_Msg_N
("illegal prefix for % attribute", Parent
(N
));
1909 Set_Etype
(Parent
(N
), Any_Type
);
1910 Set_Entity
(Parent
(N
), Any_Type
);
1911 raise Bad_Attribute
;
1914 Error_Attr
("missing argument for % attribute", N
);
1923 procedure Check_E2
is
1926 Error_Attr
("missing arguments for % attribute (2 required)", N
);
1928 Error_Attr
("missing argument for % attribute (2 required)", N
);
1932 ---------------------------
1933 -- Check_Either_E0_Or_E1 --
1934 ---------------------------
1936 procedure Check_Either_E0_Or_E1
is
1938 if Present
(E2
) then
1939 Unexpected_Argument
(E2
);
1941 end Check_Either_E0_Or_E1
;
1943 ----------------------
1944 -- Check_Enum_Image --
1945 ----------------------
1947 procedure Check_Enum_Image
is
1951 -- When an enumeration type appears in an attribute reference, all
1952 -- literals of the type are marked as referenced. This must only be
1953 -- done if the attribute reference appears in the current source.
1954 -- Otherwise the information on references may differ between a
1955 -- normal compilation and one that performs inlining.
1957 if Is_Enumeration_Type
(P_Base_Type
)
1958 and then In_Extended_Main_Code_Unit
(N
)
1960 Lit
:= First_Literal
(P_Base_Type
);
1961 while Present
(Lit
) loop
1962 Set_Referenced
(Lit
);
1966 end Check_Enum_Image
;
1968 ----------------------------
1969 -- Check_First_Last_Valid --
1970 ----------------------------
1972 procedure Check_First_Last_Valid
is
1974 Check_Discrete_Type
;
1976 -- Freeze the subtype now, so that the following test for predicates
1977 -- works (we set the predicates stuff up at freeze time)
1979 Insert_Actions
(N
, Freeze_Entity
(P_Type
, P
));
1981 -- Now test for dynamic predicate
1983 if Has_Predicates
(P_Type
)
1984 and then not (Has_Static_Predicate
(P_Type
))
1987 ("prefix of % attribute may not have dynamic predicate");
1990 -- Check non-static subtype
1992 if not Is_OK_Static_Subtype
(P_Type
) then
1993 Error_Attr_P
("prefix of % attribute must be a static subtype");
1996 -- Test case for no values
1998 if Expr_Value
(Type_Low_Bound
(P_Type
)) >
1999 Expr_Value
(Type_High_Bound
(P_Type
))
2000 or else (Has_Predicates
(P_Type
)
2002 Is_Empty_List
(Static_Discrete_Predicate
(P_Type
)))
2005 ("prefix of % attribute must be subtype with at least one "
2008 end Check_First_Last_Valid
;
2010 ----------------------------
2011 -- Check_Fixed_Point_Type --
2012 ----------------------------
2014 procedure Check_Fixed_Point_Type
is
2018 if not Is_Fixed_Point_Type
(P_Type
) then
2019 Error_Attr_P
("prefix of % attribute must be fixed point type");
2021 end Check_Fixed_Point_Type
;
2023 ------------------------------
2024 -- Check_Fixed_Point_Type_0 --
2025 ------------------------------
2027 procedure Check_Fixed_Point_Type_0
is
2029 Check_Fixed_Point_Type
;
2031 end Check_Fixed_Point_Type_0
;
2033 -------------------------------
2034 -- Check_Floating_Point_Type --
2035 -------------------------------
2037 procedure Check_Floating_Point_Type
is
2041 if not Is_Floating_Point_Type
(P_Type
) then
2042 Error_Attr_P
("prefix of % attribute must be float type");
2044 end Check_Floating_Point_Type
;
2046 ---------------------------------
2047 -- Check_Floating_Point_Type_0 --
2048 ---------------------------------
2050 procedure Check_Floating_Point_Type_0
is
2052 Check_Floating_Point_Type
;
2054 end Check_Floating_Point_Type_0
;
2056 ---------------------------------
2057 -- Check_Floating_Point_Type_1 --
2058 ---------------------------------
2060 procedure Check_Floating_Point_Type_1
is
2062 Check_Floating_Point_Type
;
2064 end Check_Floating_Point_Type_1
;
2066 ---------------------------------
2067 -- Check_Floating_Point_Type_2 --
2068 ---------------------------------
2070 procedure Check_Floating_Point_Type_2
is
2072 Check_Floating_Point_Type
;
2074 end Check_Floating_Point_Type_2
;
2076 ------------------------
2077 -- Check_Integer_Type --
2078 ------------------------
2080 procedure Check_Integer_Type
is
2084 if not Is_Integer_Type
(P_Type
) then
2085 Error_Attr_P
("prefix of % attribute must be integer type");
2087 end Check_Integer_Type
;
2089 --------------------------------
2090 -- Check_Modular_Integer_Type --
2091 --------------------------------
2093 procedure Check_Modular_Integer_Type
is
2097 if not Is_Modular_Integer_Type
(P_Type
) then
2099 ("prefix of % attribute must be modular integer type");
2101 end Check_Modular_Integer_Type
;
2103 ------------------------
2104 -- Check_Not_CPP_Type --
2105 ------------------------
2107 procedure Check_Not_CPP_Type
is
2109 if Is_Tagged_Type
(Etype
(P
))
2110 and then Convention
(Etype
(P
)) = Convention_CPP
2111 and then Is_CPP_Class
(Root_Type
(Etype
(P
)))
2114 ("invalid use of % attribute with 'C'P'P tagged type");
2116 end Check_Not_CPP_Type
;
2118 -------------------------------
2119 -- Check_Not_Incomplete_Type --
2120 -------------------------------
2122 procedure Check_Not_Incomplete_Type
is
2127 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2128 -- dereference we have to check wrong uses of incomplete types
2129 -- (other wrong uses are checked at their freezing point).
2131 -- In Ada 2012, incomplete types can appear in subprogram
2132 -- profiles, but formals with incomplete types cannot be the
2133 -- prefix of attributes.
2135 -- Example 1: Limited-with
2137 -- limited with Pkg;
2139 -- type Acc is access Pkg.T;
2141 -- S : Integer := X.all'Size; -- ERROR
2144 -- Example 2: Tagged incomplete
2146 -- type T is tagged;
2147 -- type Acc is access all T;
2149 -- S : constant Integer := X.all'Size; -- ERROR
2150 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2152 if Ada_Version
>= Ada_2005
2153 and then Nkind
(P
) = N_Explicit_Dereference
2156 while Nkind
(E
) = N_Explicit_Dereference
loop
2162 if From_Limited_With
(Typ
) then
2164 ("prefix of % attribute cannot be an incomplete type");
2166 -- If the prefix is an access type check the designated type
2168 elsif Is_Access_Type
(Typ
)
2169 and then Nkind
(P
) = N_Explicit_Dereference
2171 Typ
:= Directly_Designated_Type
(Typ
);
2174 if Is_Class_Wide_Type
(Typ
) then
2175 Typ
:= Root_Type
(Typ
);
2178 -- A legal use of a shadow entity occurs only when the unit where
2179 -- the non-limited view resides is imported via a regular with
2180 -- clause in the current body. Such references to shadow entities
2181 -- may occur in subprogram formals.
2183 if Is_Incomplete_Type
(Typ
)
2184 and then From_Limited_With
(Typ
)
2185 and then Present
(Non_Limited_View
(Typ
))
2186 and then Is_Legal_Shadow_Entity_In_Body
(Typ
)
2188 Typ
:= Non_Limited_View
(Typ
);
2191 -- If still incomplete, it can be a local incomplete type, or a
2192 -- limited view whose scope is also a limited view.
2194 if Ekind
(Typ
) = E_Incomplete_Type
then
2195 if not From_Limited_With
(Typ
)
2196 and then No
(Full_View
(Typ
))
2199 ("prefix of % attribute cannot be an incomplete type");
2201 -- The limited view may be available indirectly through
2202 -- an intermediate unit. If the non-limited view is available
2203 -- the attribute reference is legal.
2205 elsif From_Limited_With
(Typ
)
2207 (No
(Non_Limited_View
(Typ
))
2208 or else Is_Incomplete_Type
(Non_Limited_View
(Typ
)))
2211 ("prefix of % attribute cannot be an incomplete type");
2215 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2218 elsif Is_Entity_Name
(P
)
2219 and then Is_Formal
(Entity
(P
))
2220 and then Is_Incomplete_Type
(Etype
(Etype
(P
)))
2223 ("prefix of % attribute cannot be an incomplete type");
2226 if not Is_Entity_Name
(P
)
2227 or else not Is_Type
(Entity
(P
))
2228 or else In_Spec_Expression
2232 Check_Fully_Declared
(P_Type
, P
);
2234 end Check_Not_Incomplete_Type
;
2236 ----------------------------
2237 -- Check_Object_Reference --
2238 ----------------------------
2240 procedure Check_Object_Reference
(P
: Node_Id
) is
2244 -- If we need an object, and we have a prefix that is the name of a
2245 -- function entity, convert it into a function call.
2247 if Is_Entity_Name
(P
)
2248 and then Ekind
(Entity
(P
)) = E_Function
2250 Rtyp
:= Etype
(Entity
(P
));
2253 Make_Function_Call
(Sloc
(P
),
2254 Name
=> Relocate_Node
(P
)));
2256 Analyze_And_Resolve
(P
, Rtyp
);
2258 -- Otherwise we must have an object reference
2260 elsif not Is_Object_Reference
(P
) then
2261 Error_Attr_P
("prefix of % attribute must be object");
2263 end Check_Object_Reference
;
2265 ----------------------------
2266 -- Check_PolyORB_Attribute --
2267 ----------------------------
2269 procedure Check_PolyORB_Attribute
is
2271 Validate_Non_Static_Attribute_Function_Call
;
2276 if Get_PCS_Name
/= Name_PolyORB_DSA
then
2278 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N
);
2280 end Check_PolyORB_Attribute
;
2282 ------------------------
2283 -- Check_Program_Unit --
2284 ------------------------
2286 procedure Check_Program_Unit
is
2288 if Is_Entity_Name
(P
) then
2290 K
: constant Entity_Kind
:= Ekind
(Entity
(P
));
2291 T
: constant Entity_Id
:= Etype
(Entity
(P
));
2294 if K
in Subprogram_Kind
2295 or else K
in Task_Kind
2296 or else K
in Protected_Kind
2297 or else K
= E_Package
2298 or else K
in Generic_Unit_Kind
2299 or else (K
= E_Variable
2303 Is_Protected_Type
(T
)))
2310 Error_Attr_P
("prefix of % attribute must be program unit");
2311 end Check_Program_Unit
;
2313 ---------------------
2314 -- Check_Real_Type --
2315 ---------------------
2317 procedure Check_Real_Type
is
2321 if not Is_Real_Type
(P_Type
) then
2322 Error_Attr_P
("prefix of % attribute must be real type");
2324 end Check_Real_Type
;
2326 -----------------------
2327 -- Check_Scalar_Type --
2328 -----------------------
2330 procedure Check_Scalar_Type
is
2334 if not Is_Scalar_Type
(P_Type
) then
2335 Error_Attr_P
("prefix of % attribute must be scalar type");
2337 end Check_Scalar_Type
;
2339 ---------------------------
2340 -- Check_Standard_Prefix --
2341 ---------------------------
2343 procedure Check_Standard_Prefix
is
2347 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_Standard
then
2348 Error_Attr
("only allowed prefix for % attribute is Standard", P
);
2350 end Check_Standard_Prefix
;
2352 -------------------------------
2353 -- Check_Put_Image_Attribute --
2354 -------------------------------
2356 procedure Check_Put_Image_Attribute
is
2358 -- Put_Image is a procedure, and can only appear at the position of a
2359 -- procedure call. If it's a list member and it's parent is a
2360 -- procedure call or aggregate, then this is appearing as an actual
2361 -- parameter or component association, which is wrong.
2363 if Is_List_Member
(N
)
2364 and then Nkind
(Parent
(N
)) not in
2365 N_Procedure_Call_Statement | N_Aggregate
2370 ("invalid context for attribute%, which is a procedure", N
);
2374 Analyze_And_Resolve
(E1
);
2376 -- Check that the first argument is
2377 -- Ada.Strings.Text_Output.Sink'Class.
2379 -- Note: the double call to Root_Type here is needed because the
2380 -- root type of a class-wide type is the corresponding type (e.g.
2381 -- X for X'Class, and we really want to go to the root.)
2383 if Root_Type
(Root_Type
(Etype
(E1
))) /= RTE
(RE_Sink
) then
2385 ("expected Ada.Strings.Text_Output.Sink''Class", E1
);
2388 -- Check that the second argument is of the right type
2391 Resolve
(E2
, P_Type
);
2392 end Check_Put_Image_Attribute
;
2394 ----------------------------
2395 -- Check_Stream_Attribute --
2396 ----------------------------
2398 procedure Check_Stream_Attribute
(Nam
: TSS_Name_Type
) is
2402 In_Shared_Var_Procs
: Boolean;
2403 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2404 -- For this runtime package (always compiled in GNAT mode), we allow
2405 -- stream attributes references for limited types for the case where
2406 -- shared passive objects are implemented using stream attributes,
2407 -- which is the default in GNAT's persistent storage implementation.
2410 Validate_Non_Static_Attribute_Function_Call
;
2412 -- With the exception of 'Input, Stream attributes are procedures,
2413 -- and can only appear at the position of procedure calls. We check
2414 -- for this here, before they are rewritten, to give a more precise
2417 if Nam
= TSS_Stream_Input
then
2420 elsif Is_List_Member
(N
)
2421 and then Nkind
(Parent
(N
)) not in
2422 N_Procedure_Call_Statement | N_Aggregate
2428 ("invalid context for attribute%, which is a procedure", N
);
2432 Btyp
:= Implementation_Base_Type
(P_Type
);
2434 -- Stream attributes not allowed on limited types unless the
2435 -- attribute reference was generated by the expander (in which
2436 -- case the underlying type will be used, as described in Sinfo),
2437 -- or the attribute was specified explicitly for the type itself
2438 -- or one of its ancestors (taking visibility rules into account if
2439 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2440 -- (with no visibility restriction).
2443 Gen_Body
: constant Node_Id
:= Enclosing_Generic_Body
(N
);
2445 if Present
(Gen_Body
) then
2446 In_Shared_Var_Procs
:=
2447 Is_RTE
(Corresponding_Spec
(Gen_Body
), RE_Shared_Var_Procs
);
2449 In_Shared_Var_Procs
:= False;
2453 if (Comes_From_Source
(N
)
2454 and then not (In_Shared_Var_Procs
or In_Instance
))
2455 and then not Stream_Attribute_Available
(P_Type
, Nam
)
2456 and then not Has_Rep_Pragma
(Btyp
, Name_Stream_Convert
)
2458 Error_Msg_Name_1
:= Aname
;
2460 if Is_Limited_Type
(P_Type
) then
2462 ("limited type& has no% attribute", P
, P_Type
);
2463 Explain_Limited_Type
(P_Type
, P
);
2466 ("attribute% for type& is not available", P
, P_Type
);
2470 -- Check for no stream operations allowed from No_Tagged_Streams
2472 if Is_Tagged_Type
(P_Type
)
2473 and then Present
(No_Tagged_Streams_Pragma
(P_Type
))
2475 Error_Msg_Sloc
:= Sloc
(No_Tagged_Streams_Pragma
(P_Type
));
2477 ("no stream operations for & (No_Tagged_Streams #)", N
, P_Type
);
2481 -- Check restriction violations
2483 -- First check the No_Streams restriction, which prohibits the use
2484 -- of explicit stream attributes in the source program. We do not
2485 -- prevent the occurrence of stream attributes in generated code,
2486 -- for instance those generated implicitly for dispatching purposes.
2488 if Comes_From_Source
(N
) then
2489 Check_Restriction
(No_Streams
, P
);
2492 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2493 -- it is illegal to use a predefined elementary type stream attribute
2494 -- either by itself, or more importantly as part of the attribute
2495 -- subprogram for a composite type. However, if the broader
2496 -- restriction No_Streams is active, stream operations are not
2497 -- generated, and there is no error.
2499 if Restriction_Active
(No_Default_Stream_Attributes
)
2500 and then not Restriction_Active
(No_Streams
)
2506 if Nam
= TSS_Stream_Input
2508 Nam
= TSS_Stream_Read
2511 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Read
);
2514 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Write
);
2518 Check_Restriction
(No_Default_Stream_Attributes
, N
);
2521 ("missing user-defined Stream Read or Write for type&",
2523 if not Is_Elementary_Type
(P_Type
) then
2525 ("\which is a component of type&", N
, P_Type
);
2531 -- Check special case of Exception_Id and Exception_Occurrence which
2532 -- are not allowed for restriction No_Exception_Registration.
2534 if Restriction_Check_Required
(No_Exception_Registration
)
2535 and then (Is_RTE
(P_Type
, RE_Exception_Id
)
2537 Is_RTE
(P_Type
, RE_Exception_Occurrence
))
2539 Check_Restriction
(No_Exception_Registration
, P
);
2542 -- Here we must check that the first argument is an access type
2543 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2545 Analyze_And_Resolve
(E1
);
2548 -- Note: the double call to Root_Type here is needed because the
2549 -- root type of a class-wide type is the corresponding type (e.g.
2550 -- X for X'Class, and we really want to go to the root.)
2552 if not Is_Access_Type
(Etyp
)
2553 or else Root_Type
(Root_Type
(Designated_Type
(Etyp
))) /=
2554 RTE
(RE_Root_Stream_Type
)
2557 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1
);
2560 -- Check that the second argument is of the right type if there is
2561 -- one (the Input attribute has only one argument so this is skipped)
2563 if Present
(E2
) then
2566 if Nam
= TSS_Stream_Read
2567 and then not Is_OK_Variable_For_Out_Formal
(E2
)
2570 ("second argument of % attribute must be a variable", E2
);
2573 Resolve
(E2
, P_Type
);
2577 end Check_Stream_Attribute
;
2579 -------------------------
2580 -- Check_System_Prefix --
2581 -------------------------
2583 procedure Check_System_Prefix
is
2585 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_System
then
2586 Error_Attr
("only allowed prefix for % attribute is System", P
);
2588 end Check_System_Prefix
;
2590 -----------------------
2591 -- Check_Task_Prefix --
2592 -----------------------
2594 procedure Check_Task_Prefix
is
2598 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2599 -- task interface class-wide types.
2601 if Is_Task_Type
(Etype
(P
))
2602 or else (Is_Access_Type
(Etype
(P
))
2603 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
2604 or else (Ada_Version
>= Ada_2005
2605 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
2606 and then Is_Interface
(Etype
(P
))
2607 and then Is_Task_Interface
(Etype
(P
)))
2612 if Ada_Version
>= Ada_2005
then
2614 ("prefix of % attribute must be a task or a task " &
2615 "interface class-wide object");
2618 Error_Attr_P
("prefix of % attribute must be a task");
2621 end Check_Task_Prefix
;
2627 -- The possibilities are an entity name denoting a type, or an
2628 -- attribute reference that denotes a type (Base or Class). If
2629 -- the type is incomplete, replace it with its full view.
2631 procedure Check_Type
is
2633 if not Is_Entity_Name
(P
)
2634 or else not Is_Type
(Entity
(P
))
2636 Error_Attr_P
("prefix of % attribute must be a type");
2638 elsif Is_Protected_Self_Reference
(P
) then
2640 ("prefix of % attribute denotes current instance "
2641 & "(RM 9.4(21/2))");
2643 elsif Ekind
(Entity
(P
)) = E_Incomplete_Type
2644 and then Present
(Full_View
(Entity
(P
)))
2646 P_Type
:= Full_View
(Entity
(P
));
2647 Set_Entity
(P
, P_Type
);
2651 ---------------------
2652 -- Check_Unit_Name --
2653 ---------------------
2655 procedure Check_Unit_Name
(Nod
: Node_Id
) is
2657 if Nkind
(Nod
) = N_Identifier
then
2660 elsif Nkind
(Nod
) in N_Selected_Component | N_Expanded_Name
then
2661 Check_Unit_Name
(Prefix
(Nod
));
2663 if Nkind
(Selector_Name
(Nod
)) = N_Identifier
then
2668 Error_Attr
("argument for % attribute must be unit name", P
);
2669 end Check_Unit_Name
;
2675 procedure Error_Attr
is
2677 Set_Etype
(N
, Any_Type
);
2678 Set_Entity
(N
, Any_Type
);
2679 raise Bad_Attribute
;
2682 procedure Error_Attr
(Msg
: String; Error_Node
: Node_Id
) is
2684 Error_Msg_Name_1
:= Aname
;
2685 Error_Msg_N
(Msg
, Error_Node
);
2693 procedure Error_Attr_P
(Msg
: String) is
2695 Error_Msg_Name_1
:= Aname
;
2696 Error_Msg_F
(Msg
, P
);
2700 ----------------------------
2701 -- Legal_Formal_Attribute --
2702 ----------------------------
2704 procedure Legal_Formal_Attribute
is
2708 if not Is_Entity_Name
(P
)
2709 or else not Is_Type
(Entity
(P
))
2711 Error_Attr_P
("prefix of % attribute must be generic type");
2713 elsif Is_Generic_Actual_Type
(Entity
(P
))
2715 or else In_Inlined_Body
2719 elsif Is_Generic_Type
(Entity
(P
)) then
2720 if Is_Definite_Subtype
(Entity
(P
)) then
2722 ("prefix of % attribute must be indefinite generic type");
2727 ("prefix of % attribute must be indefinite generic type");
2730 Set_Etype
(N
, Standard_Boolean
);
2731 end Legal_Formal_Attribute
;
2733 ---------------------------------------------------------------
2734 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2735 ---------------------------------------------------------------
2737 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
is
2741 Check_Not_Incomplete_Type
;
2742 Set_Etype
(N
, Universal_Integer
);
2743 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
2749 procedure Min_Max
is
2751 -- Attribute can appear as function name in a reduction.
2752 -- Semantic checks are performed later.
2754 if Nkind
(Parent
(N
)) = N_Attribute_Reference
2755 and then Attribute_Name
(Parent
(N
)) = Name_Reduce
2757 Set_Etype
(N
, P_Base_Type
);
2763 Resolve
(E1
, P_Base_Type
);
2764 Resolve
(E2
, P_Base_Type
);
2765 Set_Etype
(N
, P_Base_Type
);
2767 -- Check for comparison on unordered enumeration type
2769 if Bad_Unordered_Enumeration_Reference
(N
, P_Base_Type
) then
2770 Error_Msg_Sloc
:= Sloc
(P_Base_Type
);
2772 ("comparison on unordered enumeration type& declared#?U?",
2777 ------------------------
2778 -- Standard_Attribute --
2779 ------------------------
2781 procedure Standard_Attribute
(Val
: Int
) is
2783 Check_Standard_Prefix
;
2784 Rewrite
(N
, Make_Integer_Literal
(Loc
, Val
));
2786 Set_Is_Static_Expression
(N
, True);
2787 end Standard_Attribute
;
2789 --------------------
2790 -- Uneval_Old_Msg --
2791 --------------------
2793 procedure Uneval_Old_Msg
is
2794 Uneval_Old_Setting
: Character;
2798 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2799 -- N_Aspect_Specification node that corresponds to the attribute.
2801 -- First find the pragma in which we appear (note that at this stage,
2802 -- even if we appeared originally within an aspect specification, we
2803 -- are now within the corresponding pragma).
2807 Prag
:= Parent
(Prag
);
2808 exit when No
(Prag
) or else Nkind
(Prag
) = N_Pragma
;
2811 if Present
(Prag
) then
2812 if Uneval_Old_Accept
(Prag
) then
2813 Uneval_Old_Setting
:= 'A';
2814 elsif Uneval_Old_Warn
(Prag
) then
2815 Uneval_Old_Setting
:= 'W';
2817 Uneval_Old_Setting
:= 'E';
2820 -- If we did not find the pragma, that's odd, just use the setting
2821 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2824 Uneval_Old_Setting
:= Opt
.Uneval_Old
;
2827 -- Processing depends on the setting of Uneval_Old
2829 case Uneval_Old_Setting
is
2831 -- ??? In the case where Ada_Version is < Ada_2020 and
2832 -- an illegal 'Old prefix would be legal in Ada_2020,
2833 -- we'd like to call Error_Msg_Ada_2020_Feature.
2834 -- Identifying that case involves some work.
2837 ("prefix of attribute % that is potentially "
2838 & "unevaluated must statically name an entity"
2840 -- further text needed for accuracy if Ada_2020
2841 & (if Ada_Version
>= Ada_2020
2842 and then Attr_Id
= Attribute_Old
2843 then " or be eligible for conditional evaluation"
2844 & " (RM 6.1.1 (27))"
2848 Error_Msg_Name_1
:= Aname
;
2850 ("??prefix of attribute % appears in potentially "
2851 & "unevaluated context, exception may be raised", P
);
2857 raise Program_Error
;
2861 -------------------------
2862 -- Unexpected Argument --
2863 -------------------------
2865 procedure Unexpected_Argument
(En
: Node_Id
) is
2867 Error_Attr
("unexpected argument for % attribute", En
);
2868 end Unexpected_Argument
;
2870 -------------------------------------------------
2871 -- Validate_Non_Static_Attribute_Function_Call --
2872 -------------------------------------------------
2874 -- This function should be moved to Sem_Dist ???
2876 procedure Validate_Non_Static_Attribute_Function_Call
is
2878 if In_Preelaborated_Unit
2879 and then not In_Subprogram_Or_Concurrent_Unit
2881 Flag_Non_Static_Expr
2882 ("non-static function call in preelaborated unit!", N
);
2884 end Validate_Non_Static_Attribute_Function_Call
;
2886 -- Start of processing for Analyze_Attribute
2889 -- Immediate return if unrecognized attribute (already diagnosed by
2890 -- parser, so there is nothing more that we need to do).
2892 if not Is_Attribute_Name
(Aname
) then
2893 raise Bad_Attribute
;
2896 Check_Restriction_No_Use_Of_Attribute
(N
);
2898 -- Deal with Ada 83 issues
2900 if Comes_From_Source
(N
) then
2901 if not Attribute_83
(Attr_Id
) then
2902 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
2903 Error_Msg_Name_1
:= Aname
;
2904 Error_Msg_N
("(Ada 83) attribute% is not standard??", N
);
2907 if Attribute_Impl_Def
(Attr_Id
) then
2908 Check_Restriction
(No_Implementation_Attributes
, N
);
2913 -- Deal with Ada 2005 attributes that are implementation attributes
2914 -- because they appear in a version of Ada before Ada 2005, ditto for
2915 -- Ada 2012 and Ada 2020 attributes appearing in an earlier version.
2917 if (Attribute_05
(Attr_Id
) and then Ada_Version
< Ada_2005
)
2919 (Attribute_12
(Attr_Id
) and then Ada_Version
< Ada_2012
)
2921 (Attribute_20
(Attr_Id
) and then Ada_Version
< Ada_2020
)
2923 Check_Restriction
(No_Implementation_Attributes
, N
);
2926 -- Remote access to subprogram type access attribute reference needs
2927 -- unanalyzed copy for tree transformation. The analyzed copy is used
2928 -- for its semantic information (whether prefix is a remote subprogram
2929 -- name), the unanalyzed copy is used to construct new subtree rooted
2930 -- with N_Aggregate which represents a fat pointer aggregate.
2932 if Aname
= Name_Access
then
2933 Discard_Node
(Copy_Separate_Tree
(N
));
2936 -- Analyze prefix and exit if error in analysis. If the prefix is an
2937 -- incomplete type, use full view if available. Note that there are
2938 -- some attributes for which we do not analyze the prefix, since the
2939 -- prefix is not a normal name, or else needs special handling.
2941 if Aname
/= Name_Elab_Body
and then
2942 Aname
/= Name_Elab_Spec
and then
2943 Aname
/= Name_Elab_Subp_Body
and then
2944 Aname
/= Name_Enabled
and then
2948 P_Type
:= Etype
(P
);
2950 if Is_Entity_Name
(P
)
2951 and then Present
(Entity
(P
))
2952 and then Is_Type
(Entity
(P
))
2954 if Ekind
(Entity
(P
)) = E_Incomplete_Type
then
2955 P_Type
:= Get_Full_View
(P_Type
);
2956 Set_Entity
(P
, P_Type
);
2957 Set_Etype
(P
, P_Type
);
2959 elsif Entity
(P
) = Current_Scope
2960 and then Is_Record_Type
(Entity
(P
))
2962 -- Use of current instance within the type. Verify that if the
2963 -- attribute appears within a constraint, it yields an access
2964 -- type, other uses are illegal.
2972 and then Nkind
(Parent
(Par
)) /= N_Component_Definition
2974 Par
:= Parent
(Par
);
2978 and then Nkind
(Par
) = N_Subtype_Indication
2980 if Attr_Id
/= Attribute_Access
2981 and then Attr_Id
/= Attribute_Unchecked_Access
2982 and then Attr_Id
/= Attribute_Unrestricted_Access
2985 ("in a constraint the current instance can only "
2986 & "be used with an access attribute", N
);
2993 if P_Type
= Any_Type
then
2994 raise Bad_Attribute
;
2997 P_Base_Type
:= Base_Type
(P_Type
);
3000 -- Analyze expressions that may be present, exiting if an error occurs
3007 E1
:= First
(Exprs
);
3009 -- Skip analysis for case of Restriction_Set, we do not expect
3010 -- the argument to be analyzed in this case.
3012 if Aname
/= Name_Restriction_Set
then
3015 -- Check for missing/bad expression (result of previous error)
3017 if No
(E1
) or else Etype
(E1
) = Any_Type
then
3018 raise Bad_Attribute
;
3024 if Present
(E2
) then
3027 if Etype
(E2
) = Any_Type
then
3028 raise Bad_Attribute
;
3031 if Present
(Next
(E2
)) then
3032 Unexpected_Argument
(Next
(E2
));
3037 -- Cases where prefix must be resolvable by itself
3039 if Is_Overloaded
(P
)
3040 and then Aname
/= Name_Access
3041 and then Aname
/= Name_Address
3042 and then Aname
/= Name_Code_Address
3043 and then Aname
/= Name_Result
3044 and then Aname
/= Name_Unchecked_Access
3046 -- The prefix must be resolvable by itself, without reference to the
3047 -- attribute. One case that requires special handling is a prefix
3048 -- that is a function name, where one interpretation may be a
3049 -- parameterless call. Entry attributes are handled specially below.
3051 if Is_Entity_Name
(P
)
3052 and then Aname
not in Name_Count | Name_Caller
3054 Check_Parameterless_Call
(P
);
3057 if Is_Overloaded
(P
) then
3059 -- Ada 2005 (AI-345): Since protected and task types have
3060 -- primitive entry wrappers, the attributes Count, and Caller
3061 -- require a context check
3063 if Aname
in Name_Count | Name_Caller
then
3065 Count
: Natural := 0;
3070 Get_First_Interp
(P
, I
, It
);
3071 while Present
(It
.Nam
) loop
3072 if Comes_From_Source
(It
.Nam
) then
3078 Get_Next_Interp
(I
, It
);
3082 Error_Attr
("ambiguous prefix for % attribute", P
);
3084 Set_Is_Overloaded
(P
, False);
3089 Error_Attr
("ambiguous prefix for % attribute", P
);
3094 -- Remaining processing depends on attribute
3098 -- Attributes related to Ada 2012 iterators. Attribute specifications
3099 -- exist for these, but they cannot be queried.
3101 when Attribute_Constant_Indexing
3102 | Attribute_Default_Iterator
3103 | Attribute_Implicit_Dereference
3104 | Attribute_Iterator_Element
3105 | Attribute_Iterable
3106 | Attribute_Variable_Indexing
3108 Error_Msg_N
("illegal attribute", N
);
3110 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3111 -- were already rejected by the parser. Thus they shouldn't appear here.
3113 when Internal_Attribute_Id
=>
3114 raise Program_Error
;
3120 when Attribute_Abort_Signal
=>
3121 Check_Standard_Prefix
;
3122 Rewrite
(N
, New_Occurrence_Of
(Stand
.Abort_Signal
, Loc
));
3129 when Attribute_Access
=>
3130 Analyze_Access_Attribute
;
3131 Check_Not_Incomplete_Type
;
3137 when Attribute_Address
=>
3140 Check_Not_Incomplete_Type
;
3141 Set_Etype
(N
, RTE
(RE_Address
));
3147 when Attribute_Address_Size
=>
3148 Standard_Attribute
(System_Address_Size
);
3154 when Attribute_Adjacent
=>
3155 Check_Floating_Point_Type_2
;
3156 Set_Etype
(N
, P_Base_Type
);
3157 Resolve
(E1
, P_Base_Type
);
3158 Resolve
(E2
, P_Base_Type
);
3164 when Attribute_Aft
=>
3165 Check_Fixed_Point_Type_0
;
3166 Set_Etype
(N
, Universal_Integer
);
3172 when Attribute_Alignment
=>
3174 -- Don't we need more checking here, cf Size ???
3177 Check_Not_Incomplete_Type
;
3179 Set_Etype
(N
, Universal_Integer
);
3185 when Attribute_Asm_Input
=>
3186 Check_Asm_Attribute
;
3188 -- The back end may need to take the address of E2
3190 if Is_Entity_Name
(E2
) then
3191 Set_Address_Taken
(Entity
(E2
));
3194 Set_Etype
(N
, RTE
(RE_Asm_Input_Operand
));
3200 when Attribute_Asm_Output
=>
3201 Check_Asm_Attribute
;
3203 if Etype
(E2
) = Any_Type
then
3206 elsif Aname
= Name_Asm_Output
then
3207 if not Is_Variable
(E2
) then
3209 ("second argument for Asm_Output is not variable", E2
);
3213 Note_Possible_Modification
(E2
, Sure
=> True);
3215 -- The back end may need to take the address of E2
3217 if Is_Entity_Name
(E2
) then
3218 Set_Address_Taken
(Entity
(E2
));
3221 Set_Etype
(N
, RTE
(RE_Asm_Output_Operand
));
3223 -----------------------------
3224 -- Atomic_Always_Lock_Free --
3225 -----------------------------
3227 when Attribute_Atomic_Always_Lock_Free
=>
3230 Set_Etype
(N
, Standard_Boolean
);
3236 -- Note: when the base attribute appears in the context of a subtype
3237 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3238 -- the following circuit.
3240 when Attribute_Base
=> Base
: declare
3248 if Ada_Version
>= Ada_95
3249 and then not Is_Scalar_Type
(Typ
)
3250 and then not Is_Generic_Type
(Typ
)
3252 Error_Attr_P
("prefix of Base attribute must be scalar type");
3254 elsif Sloc
(Typ
) = Standard_Location
3255 and then Base_Type
(Typ
) = Typ
3256 and then Warn_On_Redundant_Constructs
3258 Error_Msg_NE
-- CODEFIX
3259 ("?r?redundant attribute, & is its own base type", N
, Typ
);
3262 Set_Etype
(N
, Base_Type
(Entity
(P
)));
3263 Set_Entity
(N
, Base_Type
(Entity
(P
)));
3264 Rewrite
(N
, New_Occurrence_Of
(Entity
(N
), Loc
));
3272 when Attribute_Bit
=>
3275 if not Is_Object_Reference
(P
) then
3276 Error_Attr_P
("prefix for % attribute must be object");
3278 -- What about the access object cases ???
3284 Set_Etype
(N
, Universal_Integer
);
3290 when Attribute_Bit_Order
=>
3294 if not Is_Record_Type
(P_Type
) then
3295 Error_Attr_P
("prefix of % attribute must be record type");
3298 if Bytes_Big_Endian
xor Reverse_Bit_Order
(P_Type
) then
3300 New_Occurrence_Of
(RTE
(RE_High_Order_First
), Loc
));
3303 New_Occurrence_Of
(RTE
(RE_Low_Order_First
), Loc
));
3306 Set_Etype
(N
, RTE
(RE_Bit_Order
));
3309 -- Reset incorrect indication of staticness
3311 Set_Is_Static_Expression
(N
, False);
3317 -- Note: in generated code, we can have a Bit_Position attribute
3318 -- applied to a (naked) record component (i.e. the prefix is an
3319 -- identifier that references an E_Component or E_Discriminant
3320 -- entity directly, and this is interpreted as expected by Gigi.
3321 -- The following code will not tolerate such usage, but when the
3322 -- expander creates this special case, it marks it as analyzed
3323 -- immediately and sets an appropriate type.
3325 when Attribute_Bit_Position
=>
3326 if Comes_From_Source
(N
) then
3330 Set_Etype
(N
, Universal_Integer
);
3336 when Attribute_Body_Version
=>
3339 Set_Etype
(N
, RTE
(RE_Version_String
));
3345 when Attribute_Callable
=>
3347 Set_Etype
(N
, Standard_Boolean
);
3354 when Attribute_Caller
=> Caller
: declare
3361 if Nkind
(P
) in N_Identifier | N_Expanded_Name
then
3364 if not Is_Entry
(Ent
) then
3365 Error_Attr
("invalid entry name", N
);
3369 Error_Attr
("invalid entry name", N
);
3373 for J
in reverse 0 .. Scope_Stack
.Last
loop
3374 S
:= Scope_Stack
.Table
(J
).Entity
;
3376 if S
= Scope
(Ent
) then
3377 Error_Attr
("Caller must appear in matching accept or body", N
);
3383 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
3390 when Attribute_Ceiling
=>
3391 Check_Floating_Point_Type_1
;
3392 Set_Etype
(N
, P_Base_Type
);
3393 Resolve
(E1
, P_Base_Type
);
3399 when Attribute_Class
=>
3400 Check_Restriction
(No_Dispatch
, N
);
3404 -- Applying Class to untagged incomplete type is obsolescent in Ada
3405 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3406 -- this flag gets set by Find_Type in this situation.
3408 if Restriction_Check_Required
(No_Obsolescent_Features
)
3409 and then Ada_Version
>= Ada_2005
3410 and then Ekind
(P_Type
) = E_Incomplete_Type
3413 DN
: constant Node_Id
:= Declaration_Node
(P_Type
);
3415 if Nkind
(DN
) = N_Incomplete_Type_Declaration
3416 and then not Tagged_Present
(DN
)
3418 Check_Restriction
(No_Obsolescent_Features
, P
);
3427 when Attribute_Code_Address
=>
3430 if Nkind
(P
) = N_Attribute_Reference
3431 and then Attribute_Name
(P
) in Name_Elab_Body | Name_Elab_Spec
3435 elsif not Is_Entity_Name
(P
)
3436 or else (Ekind
(Entity
(P
)) /= E_Function
3438 Ekind
(Entity
(P
)) /= E_Procedure
)
3440 Error_Attr
("invalid prefix for % attribute", P
);
3441 Set_Address_Taken
(Entity
(P
));
3443 -- Issue an error if the prefix denotes an eliminated subprogram
3446 Check_For_Eliminated_Subprogram
(P
, Entity
(P
));
3449 Set_Etype
(N
, RTE
(RE_Address
));
3451 ----------------------
3452 -- Compiler_Version --
3453 ----------------------
3455 when Attribute_Compiler_Version
=>
3457 Check_Standard_Prefix
;
3458 Rewrite
(N
, Make_String_Literal
(Loc
, "GNAT " & Gnat_Version_String
));
3459 Analyze_And_Resolve
(N
, Standard_String
);
3460 Set_Is_Static_Expression
(N
, True);
3462 --------------------
3463 -- Component_Size --
3464 --------------------
3466 when Attribute_Component_Size
=>
3468 Set_Etype
(N
, Universal_Integer
);
3470 -- Note: unlike other array attributes, unconstrained arrays are OK
3472 if Is_Array_Type
(P_Type
) and then not Is_Constrained
(P_Type
) then
3482 when Attribute_Compose
=>
3483 Check_Floating_Point_Type_2
;
3484 Set_Etype
(N
, P_Base_Type
);
3485 Resolve
(E1
, P_Base_Type
);
3486 Resolve
(E2
, Any_Integer
);
3492 when Attribute_Constrained
=>
3494 Set_Etype
(N
, Standard_Boolean
);
3496 -- Case from RM J.4(2) of constrained applied to private type
3498 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
3499 Check_Restriction
(No_Obsolescent_Features
, P
);
3501 if Warn_On_Obsolescent_Feature
then
3503 ("constrained for private type is an obsolescent feature "
3504 & "(RM J.4)?j?", N
);
3507 -- If we are within an instance, the attribute must be legal
3508 -- because it was valid in the generic unit. Ditto if this is
3509 -- an inlining of a function declared in an instance.
3511 if In_Instance
or else In_Inlined_Body
then
3514 -- For sure OK if we have a real private type itself, but must
3515 -- be completed, cannot apply Constrained to incomplete type.
3517 elsif Is_Private_Type
(Entity
(P
)) then
3519 -- Note: this is one of the Annex J features that does not
3520 -- generate a warning from -gnatwj, since in fact it seems
3521 -- very useful, and is used in the GNAT runtime.
3523 Check_Not_Incomplete_Type
;
3527 -- Normal (non-obsolescent case) of application to object or value of
3528 -- a discriminated type.
3531 -- AI12-0068: In a type or subtype aspect, a prefix denoting the
3532 -- current instance of the (sub)type is defined to be a value,
3533 -- not an object, so the Constrained attribute is always True
3534 -- (see RM 8.6(18/5) and RM 3.7.2(3/5)). We issue a warning about
3535 -- this unintuitive result, to help avoid confusion.
3537 if Is_Current_Instance_Reference_In_Type_Aspect
(P
) then
3538 Error_Msg_Name_1
:= Aname
;
3540 ("current instance attribute % in subtype aspect always " &
3544 Check_Object_Reference
(P
);
3547 -- If N does not come from source, then we allow the
3548 -- the attribute prefix to be of a private type whose
3549 -- full type has discriminants. This occurs in cases
3550 -- involving expanded calls to stream attributes.
3552 if not Comes_From_Source
(N
) then
3553 P_Type
:= Underlying_Type
(P_Type
);
3556 -- Must have discriminants or be an access type designating a type
3557 -- with discriminants. If it is a class-wide type it has unknown
3560 if Has_Discriminants
(P_Type
)
3561 or else Has_Unknown_Discriminants
(P_Type
)
3563 (Is_Access_Type
(P_Type
)
3564 and then Has_Discriminants
(Designated_Type
(P_Type
)))
3568 -- The rule given in 3.7.2 is part of static semantics, but the
3569 -- intent is clearly that it be treated as a legality rule, and
3570 -- rechecked in the visible part of an instance. Nevertheless
3571 -- the intent also seems to be it should legally apply to the
3572 -- actual of a formal with unknown discriminants, regardless of
3573 -- whether the actual has discriminants, in which case the value
3574 -- of the attribute is determined using the J.4 rules. This choice
3575 -- seems the most useful, and is compatible with existing tests.
3577 elsif In_Instance
then
3580 -- Also allow an object of a generic type if extensions allowed
3581 -- and allow this for any type at all.
3583 elsif (Is_Generic_Type
(P_Type
)
3584 or else Is_Generic_Actual_Type
(P_Type
))
3585 and then Extensions_Allowed
3591 -- Fall through if bad prefix
3594 ("prefix of % attribute must be object of discriminated type");
3600 when Attribute_Copy_Sign
=>
3601 Check_Floating_Point_Type_2
;
3602 Set_Etype
(N
, P_Base_Type
);
3603 Resolve
(E1
, P_Base_Type
);
3604 Resolve
(E2
, P_Base_Type
);
3610 when Attribute_Count
=> Count
: declare
3618 if Nkind
(P
) in N_Identifier | N_Expanded_Name
then
3621 if Ekind
(Ent
) /= E_Entry
then
3622 Error_Attr
("invalid entry name", N
);
3625 elsif Nkind
(P
) = N_Indexed_Component
then
3626 if not Is_Entity_Name
(Prefix
(P
))
3627 or else No
(Entity
(Prefix
(P
)))
3628 or else Ekind
(Entity
(Prefix
(P
))) /= E_Entry_Family
3630 if Nkind
(Prefix
(P
)) = N_Selected_Component
3631 and then Present
(Entity
(Selector_Name
(Prefix
(P
))))
3632 and then Ekind
(Entity
(Selector_Name
(Prefix
(P
)))) =
3636 ("attribute % must apply to entry of current task", P
);
3639 Error_Attr
("invalid entry family name", P
);
3644 Ent
:= Entity
(Prefix
(P
));
3647 elsif Nkind
(P
) = N_Selected_Component
3648 and then Present
(Entity
(Selector_Name
(P
)))
3649 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Entry
3652 ("attribute % must apply to entry of current task", P
);
3655 Error_Attr
("invalid entry name", N
);
3659 for J
in reverse 0 .. Scope_Stack
.Last
loop
3660 S
:= Scope_Stack
.Table
(J
).Entity
;
3662 if S
= Scope
(Ent
) then
3663 if Nkind
(P
) = N_Expanded_Name
then
3664 Tsk
:= Entity
(Prefix
(P
));
3666 -- The prefix denotes either the task type, or else a
3667 -- single task whose task type is being analyzed.
3669 if (Is_Type
(Tsk
) and then Tsk
= S
)
3670 or else (not Is_Type
(Tsk
)
3671 and then Etype
(Tsk
) = S
3672 and then not (Comes_From_Source
(S
)))
3677 ("Attribute % must apply to entry of current task", N
);
3683 elsif Ekind
(Scope
(Ent
)) in Task_Kind
3684 and then Ekind
(S
) not in E_Block
3689 Error_Attr
("Attribute % cannot appear in inner unit", N
);
3691 elsif Ekind
(Scope
(Ent
)) = E_Protected_Type
3692 and then not Has_Completion
(Scope
(Ent
))
3694 Error_Attr
("attribute % can only be used inside body", N
);
3698 if Is_Overloaded
(P
) then
3700 Index
: Interp_Index
;
3704 Get_First_Interp
(P
, Index
, It
);
3705 while Present
(It
.Nam
) loop
3706 if It
.Nam
= Ent
then
3709 -- Ada 2005 (AI-345): Do not consider primitive entry
3710 -- wrappers generated for task or protected types.
3712 elsif Ada_Version
>= Ada_2005
3713 and then not Comes_From_Source
(It
.Nam
)
3718 Error_Attr
("ambiguous entry name", N
);
3721 Get_Next_Interp
(Index
, It
);
3726 Set_Etype
(N
, Universal_Integer
);
3729 -----------------------
3730 -- Default_Bit_Order --
3731 -----------------------
3733 when Attribute_Default_Bit_Order
=> Default_Bit_Order
: declare
3734 Target_Default_Bit_Order
: System
.Bit_Order
;
3737 Check_Standard_Prefix
;
3739 if Bytes_Big_Endian
then
3740 Target_Default_Bit_Order
:= System
.High_Order_First
;
3742 Target_Default_Bit_Order
:= System
.Low_Order_First
;
3746 Make_Integer_Literal
(Loc
,
3747 UI_From_Int
(System
.Bit_Order
'Pos (Target_Default_Bit_Order
))));
3749 Set_Etype
(N
, Universal_Integer
);
3750 Set_Is_Static_Expression
(N
);
3751 end Default_Bit_Order
;
3753 ----------------------------------
3754 -- Default_Scalar_Storage_Order --
3755 ----------------------------------
3757 when Attribute_Default_Scalar_Storage_Order
=> Default_SSO
: declare
3758 RE_Default_SSO
: RE_Id
;
3761 Check_Standard_Prefix
;
3763 case Opt
.Default_SSO
is
3765 if Bytes_Big_Endian
then
3766 RE_Default_SSO
:= RE_High_Order_First
;
3768 RE_Default_SSO
:= RE_Low_Order_First
;
3772 RE_Default_SSO
:= RE_High_Order_First
;
3775 RE_Default_SSO
:= RE_Low_Order_First
;
3778 raise Program_Error
;
3781 Rewrite
(N
, New_Occurrence_Of
(RTE
(RE_Default_SSO
), Loc
));
3788 when Attribute_Definite
=>
3789 Legal_Formal_Attribute
;
3795 when Attribute_Delta
=>
3796 Check_Fixed_Point_Type_0
;
3797 Set_Etype
(N
, Universal_Real
);
3803 when Attribute_Denorm
=>
3804 Check_Floating_Point_Type_0
;
3805 Set_Etype
(N
, Standard_Boolean
);
3811 when Attribute_Deref
=>
3814 Resolve
(E1
, RTE
(RE_Address
));
3815 Set_Etype
(N
, P_Type
);
3817 ---------------------
3818 -- Descriptor_Size --
3819 ---------------------
3821 when Attribute_Descriptor_Size
=>
3824 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
3825 Error_Attr_P
("prefix of attribute % must denote a type");
3828 Set_Etype
(N
, Universal_Integer
);
3834 when Attribute_Digits
=>
3838 if not Is_Floating_Point_Type
(P_Type
)
3839 and then not Is_Decimal_Fixed_Point_Type
(P_Type
)
3842 ("prefix of % attribute must be float or decimal type");
3845 Set_Etype
(N
, Universal_Integer
);
3851 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3853 when Attribute_Elab_Body
3854 | Attribute_Elab_Spec
3855 | Attribute_Elab_Subp_Body
3858 Check_Unit_Name
(P
);
3859 Set_Etype
(N
, Standard_Void_Type
);
3861 -- We have to manually call the expander in this case to get
3862 -- the necessary expansion (normally attributes that return
3863 -- entities are not expanded).
3871 -- Shares processing with Elab_Body
3877 when Attribute_Elaborated
=>
3879 Check_Unit_Name
(P
);
3880 Set_Etype
(N
, Standard_Boolean
);
3886 when Attribute_Emax
=>
3887 Check_Floating_Point_Type_0
;
3888 Set_Etype
(N
, Universal_Integer
);
3894 when Attribute_Enabled
=>
3895 Check_Either_E0_Or_E1
;
3897 if Present
(E1
) then
3898 if not Is_Entity_Name
(E1
) or else No
(Entity
(E1
)) then
3899 Error_Msg_N
("entity name expected for Enabled attribute", E1
);
3904 if Nkind
(P
) /= N_Identifier
then
3905 Error_Msg_N
("identifier expected (check name)", P
);
3906 elsif Get_Check_Id
(Chars
(P
)) = No_Check_Id
then
3907 Error_Msg_N
("& is not a recognized check name", P
);
3910 Set_Etype
(N
, Standard_Boolean
);
3916 when Attribute_Enum_Rep
=>
3918 -- T'Enum_Rep (X) case
3920 if Present
(E1
) then
3922 Check_Discrete_Type
;
3923 Resolve
(E1
, P_Base_Type
);
3925 -- X'Enum_Rep case. X must be an object or enumeration literal
3926 -- (including an attribute reference), and it must be of a
3930 ((Is_Object_Reference
(P
)
3933 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
3934 or else Nkind
(P
) = N_Attribute_Reference
)
3935 and then Is_Discrete_Type
(Etype
(P
)))
3937 Error_Attr_P
("prefix of % attribute must be discrete object");
3940 Set_Etype
(N
, Universal_Integer
);
3946 when Attribute_Enum_Val
=>
3950 if not Is_Enumeration_Type
(P_Type
) then
3951 Error_Attr_P
("prefix of % attribute must be enumeration type");
3954 -- If the enumeration type has a standard representation, the effect
3955 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3957 if not Has_Non_Standard_Rep
(P_Base_Type
) then
3959 Make_Attribute_Reference
(Loc
,
3960 Prefix
=> Relocate_Node
(Prefix
(N
)),
3961 Attribute_Name
=> Name_Val
,
3962 Expressions
=> New_List
(Relocate_Node
(E1
))));
3963 Analyze_And_Resolve
(N
, P_Base_Type
);
3965 -- Non-standard representation case (enumeration with holes)
3969 Resolve
(E1
, Any_Integer
);
3970 Set_Etype
(N
, P_Base_Type
);
3977 when Attribute_Epsilon
=>
3978 Check_Floating_Point_Type_0
;
3979 Set_Etype
(N
, Universal_Real
);
3985 when Attribute_Exponent
=>
3986 Check_Floating_Point_Type_1
;
3987 Set_Etype
(N
, Universal_Integer
);
3988 Resolve
(E1
, P_Base_Type
);
3994 when Attribute_External_Tag
=>
3998 Set_Etype
(N
, Standard_String
);
4000 if not Is_Tagged_Type
(P_Type
) then
4001 Error_Attr_P
("prefix of % attribute must be tagged");
4008 when Attribute_Fast_Math
=>
4009 Check_Standard_Prefix
;
4010 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(Fast_Math
), Loc
));
4012 -----------------------
4013 -- Finalization_Size --
4014 -----------------------
4016 when Attribute_Finalization_Size
=>
4019 -- The prefix denotes an object
4021 if Is_Object_Reference
(P
) then
4022 Check_Object_Reference
(P
);
4024 -- The prefix denotes a type
4026 elsif Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
4028 Check_Not_Incomplete_Type
;
4030 -- Attribute 'Finalization_Size is not defined for class-wide
4031 -- types because it is not possible to know statically whether
4032 -- a definite type will have controlled components or not.
4034 if Is_Class_Wide_Type
(Etype
(P
)) then
4036 ("prefix of % attribute cannot denote a class-wide type");
4039 -- The prefix denotes an illegal construct
4043 ("prefix of % attribute must be a definite type or an object");
4046 Set_Etype
(N
, Universal_Integer
);
4052 when Attribute_First
=>
4053 Check_Array_Or_Scalar_Type
;
4054 Bad_Attribute_For_Predicate
;
4060 when Attribute_First_Bit
=>
4062 Set_Etype
(N
, Universal_Integer
);
4068 when Attribute_First_Valid
=>
4069 Check_First_Last_Valid
;
4070 Set_Etype
(N
, P_Type
);
4076 when Attribute_Fixed_Value
=>
4078 Check_Fixed_Point_Type
;
4079 Resolve
(E1
, Any_Integer
);
4080 Set_Etype
(N
, P_Base_Type
);
4086 when Attribute_Floor
=>
4087 Check_Floating_Point_Type_1
;
4088 Set_Etype
(N
, P_Base_Type
);
4089 Resolve
(E1
, P_Base_Type
);
4095 when Attribute_Fore
=>
4096 Check_Fixed_Point_Type_0
;
4097 Set_Etype
(N
, Universal_Integer
);
4103 when Attribute_Fraction
=>
4104 Check_Floating_Point_Type_1
;
4105 Set_Etype
(N
, P_Base_Type
);
4106 Resolve
(E1
, P_Base_Type
);
4112 when Attribute_From_Any
=>
4114 Check_PolyORB_Attribute
;
4115 Set_Etype
(N
, P_Base_Type
);
4117 -----------------------
4118 -- Has_Access_Values --
4119 -----------------------
4121 when Attribute_Has_Access_Values
=>
4124 Set_Etype
(N
, Standard_Boolean
);
4126 ----------------------
4127 -- Has_Same_Storage --
4128 ----------------------
4130 when Attribute_Has_Same_Storage
=>
4133 -- The arguments must be objects of any type
4135 Analyze_And_Resolve
(P
);
4136 Analyze_And_Resolve
(E1
);
4137 Check_Object_Reference
(P
);
4138 Check_Object_Reference
(E1
);
4139 Set_Etype
(N
, Standard_Boolean
);
4141 -----------------------
4142 -- Has_Tagged_Values --
4143 -----------------------
4145 when Attribute_Has_Tagged_Values
=>
4148 Set_Etype
(N
, Standard_Boolean
);
4150 -----------------------
4151 -- Has_Discriminants --
4152 -----------------------
4154 when Attribute_Has_Discriminants
=>
4155 Legal_Formal_Attribute
;
4161 when Attribute_Identity
=>
4165 if Etype
(P
) = Standard_Exception_Type
then
4166 Set_Etype
(N
, RTE
(RE_Exception_Id
));
4168 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4169 -- interface class-wide types.
4171 elsif Is_Task_Type
(Etype
(P
))
4172 or else (Is_Access_Type
(Etype
(P
))
4173 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
4174 or else (Ada_Version
>= Ada_2005
4175 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
4176 and then Is_Interface
(Etype
(P
))
4177 and then Is_Task_Interface
(Etype
(P
)))
4180 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
4183 if Ada_Version
>= Ada_2005
then
4185 ("prefix of % attribute must be an exception, a task or a "
4186 & "task interface class-wide object");
4189 ("prefix of % attribute must be a task or an exception");
4197 when Attribute_Image
=>
4198 if Is_Real_Type
(P_Type
) then
4199 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
4200 Error_Msg_Name_1
:= Aname
;
4202 ("(Ada 83) % attribute not allowed for real types", N
);
4206 Analyze_Image_Attribute
(Standard_String
);
4212 when Attribute_Img
=>
4213 Analyze_Image_Attribute
(Standard_String
);
4219 when Attribute_Initialized
=>
4222 if Comes_From_Source
(N
) then
4224 -- This attribute be prefixed with references to objects or
4225 -- values (such as a current instance value given within a type
4226 -- or subtype aspect).
4228 if not Is_Object_Reference
(P
)
4229 and then not Is_Current_Instance_Reference_In_Type_Aspect
(P
)
4231 Error_Attr_P
("prefix of % attribute must be object");
4235 Set_Etype
(N
, Standard_Boolean
);
4241 when Attribute_Input
=>
4243 Check_Stream_Attribute
(TSS_Stream_Input
);
4244 Set_Etype
(N
, P_Base_Type
);
4250 when Attribute_Integer_Value
=>
4253 Resolve
(E1
, Any_Fixed
);
4255 -- Signal an error if argument type is not a specific fixed-point
4256 -- subtype. An error has been signalled already if the argument
4257 -- was not of a fixed-point type.
4259 if Etype
(E1
) = Any_Fixed
and then not Error_Posted
(E1
) then
4260 Error_Attr
("argument of % must be of a fixed-point type", E1
);
4263 Set_Etype
(N
, P_Base_Type
);
4269 when Attribute_Invalid_Value
=>
4272 Set_Etype
(N
, P_Base_Type
);
4273 Invalid_Value_Used
:= True;
4279 when Attribute_Large
=>
4282 Set_Etype
(N
, Universal_Real
);
4288 when Attribute_Last
=>
4289 Check_Array_Or_Scalar_Type
;
4290 Bad_Attribute_For_Predicate
;
4296 when Attribute_Last_Bit
=>
4298 Set_Etype
(N
, Universal_Integer
);
4304 when Attribute_Last_Valid
=>
4305 Check_First_Last_Valid
;
4306 Set_Etype
(N
, P_Type
);
4312 when Attribute_Leading_Part
=>
4313 Check_Floating_Point_Type_2
;
4314 Set_Etype
(N
, P_Base_Type
);
4315 Resolve
(E1
, P_Base_Type
);
4316 Resolve
(E2
, Any_Integer
);
4322 when Attribute_Length
=>
4324 Set_Etype
(N
, Universal_Integer
);
4330 when Attribute_Library_Level
=>
4333 if not Is_Entity_Name
(P
) then
4334 Error_Attr_P
("prefix of % attribute must be an entity name");
4337 if not Inside_A_Generic
then
4338 Set_Boolean_Result
(N
,
4339 Is_Library_Level_Entity
(Entity
(P
)));
4342 Set_Etype
(N
, Standard_Boolean
);
4348 when Attribute_Lock_Free
=>
4350 Set_Etype
(N
, Standard_Boolean
);
4352 if not Is_Protected_Type
(P_Type
) then
4354 ("prefix of % attribute must be a protected object");
4361 when Attribute_Loop_Entry
=> Loop_Entry
: declare
4362 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
);
4363 -- Inspect the prefix for any uses of entities declared within the
4364 -- related loop. Loop_Id denotes the loop identifier.
4366 --------------------------------
4367 -- Check_References_In_Prefix --
4368 --------------------------------
4370 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
) is
4371 Loop_Decl
: constant Node_Id
:= Label_Construct
(Parent
(Loop_Id
));
4373 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
4374 -- Determine whether a reference mentions an entity declared
4375 -- within the related loop.
4377 function Declared_Within
(Nod
: Node_Id
) return Boolean;
4378 -- Determine whether Nod appears in the subtree of Loop_Decl
4380 ---------------------
4381 -- Check_Reference --
4382 ---------------------
4384 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
4386 if Nkind
(Nod
) = N_Identifier
4387 and then Present
(Entity
(Nod
))
4388 and then Declared_Within
(Declaration_Node
(Entity
(Nod
)))
4391 ("prefix of attribute % cannot reference local entities",
4397 end Check_Reference
;
4399 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
4401 ---------------------
4402 -- Declared_Within --
4403 ---------------------
4405 function Declared_Within
(Nod
: Node_Id
) return Boolean is
4410 while Present
(Stmt
) loop
4411 if Stmt
= Loop_Decl
then
4414 -- Prevent the search from going too far
4416 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
4420 Stmt
:= Parent
(Stmt
);
4424 end Declared_Within
;
4426 -- Start of processing for Check_Prefix_For_Local_References
4429 Check_References
(P
);
4430 end Check_References_In_Prefix
;
4434 Context
: constant Node_Id
:= Parent
(N
);
4436 Encl_Loop
: Node_Id
:= Empty
;
4437 Encl_Prag
: Node_Id
:= Empty
;
4438 Loop_Id
: Entity_Id
:= Empty
;
4442 -- Start of processing for Loop_Entry
4447 -- Set the type of the attribute now to ensure the successful
4448 -- continuation of analysis even if the attribute is misplaced.
4450 Set_Etype
(Attr
, P_Type
);
4452 -- Attribute 'Loop_Entry may appear in several flavors:
4454 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4455 -- nearest enclosing loop.
4457 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4458 -- attribute may be related to a loop denoted by label Expr or
4459 -- the prefix may denote an array object and Expr may act as an
4460 -- indexed component.
4462 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4463 -- to the nearest enclosing loop, all expressions are part of
4464 -- an indexed component.
4466 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4467 -- denotes, the attribute may be related to a loop denoted by
4468 -- label Expr or the prefix may denote a multidimensional array
4469 -- array object and Expr along with the rest of the expressions
4470 -- may act as indexed components.
4472 -- Regardless of variations, the attribute reference does not have an
4473 -- expression list. Instead, all available expressions are stored as
4474 -- indexed components.
4476 -- When the attribute is part of an indexed component, find the first
4477 -- expression as it will determine the semantics of 'Loop_Entry.
4479 -- If the attribute is itself an index in an indexed component, i.e.
4480 -- a member of a list, the context itself is not relevant (the code
4481 -- below would lead to an infinite loop) and the attribute applies
4482 -- to the enclosing loop.
4484 if Nkind
(Context
) = N_Indexed_Component
4485 and then not Is_List_Member
(N
)
4487 E1
:= First
(Expressions
(Context
));
4490 -- The attribute reference appears in the following form:
4492 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4494 -- In this case, the loop name is omitted and no rewriting is
4497 if Present
(E2
) then
4500 -- The form of the attribute is:
4502 -- Prefix'Loop_Entry (Expr) [(...)]
4504 -- If Expr denotes a loop entry, the whole attribute and indexed
4505 -- component will have to be rewritten to reflect this relation.
4508 pragma Assert
(Present
(E1
));
4510 -- Do not expand the expression as it may have side effects.
4511 -- Simply preanalyze to determine whether it is a loop name or
4514 Preanalyze_And_Resolve
(E1
);
4516 if Is_Entity_Name
(E1
)
4517 and then Present
(Entity
(E1
))
4518 and then Ekind
(Entity
(E1
)) = E_Loop
4520 Loop_Id
:= Entity
(E1
);
4522 -- Transform the attribute and enclosing indexed component
4524 Set_Expressions
(N
, Expressions
(Context
));
4525 Rewrite
(Context
, N
);
4526 Set_Etype
(Context
, P_Type
);
4533 -- The prefix must denote an object
4535 if not Is_Object_Reference
(P
) then
4536 Error_Attr_P
("prefix of attribute % must denote an object");
4539 -- The prefix cannot be of a limited type because the expansion of
4540 -- Loop_Entry must create a constant initialized by the evaluated
4543 if Is_Limited_View
(Etype
(P
)) then
4544 Error_Attr_P
("prefix of attribute % cannot be limited");
4547 -- Climb the parent chain to verify the location of the attribute and
4548 -- find the enclosing loop.
4551 while Present
(Stmt
) loop
4553 -- Locate the corresponding enclosing pragma. Note that in the
4554 -- case of Assert[And_Cut] and Assume, we have already checked
4555 -- that the pragma appears in an appropriate loop location.
4557 if Nkind
(Original_Node
(Stmt
)) = N_Pragma
4559 Pragma_Name_Unmapped
(Original_Node
(Stmt
))
4560 in Name_Loop_Invariant
4563 | Name_Assert_And_Cut
4566 Encl_Prag
:= Original_Node
(Stmt
);
4568 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4569 -- iteration may be expanded into several nested loops, we are
4570 -- interested in the outermost one which has the loop identifier,
4571 -- and comes from source.
4573 elsif Nkind
(Stmt
) = N_Loop_Statement
4574 and then Present
(Identifier
(Stmt
))
4575 and then Comes_From_Source
(Original_Node
(Stmt
))
4576 and then Nkind
(Original_Node
(Stmt
)) = N_Loop_Statement
4580 -- The original attribute reference may lack a loop name. Use
4581 -- the name of the enclosing loop because it is the related
4584 if No
(Loop_Id
) then
4585 Loop_Id
:= Entity
(Identifier
(Encl_Loop
));
4590 -- Prevent the search from going too far
4592 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
4596 Stmt
:= Parent
(Stmt
);
4599 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4600 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4601 -- purpose if they appear in an appropriate location in a loop,
4602 -- which was already checked by the top level pragma circuit).
4604 -- Loop_Entry also denotes a value and as such can appear within an
4605 -- expression that is an argument for another loop aspect. In that
4606 -- case it will have been expanded into the corresponding assignment.
4609 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
4610 and then not Comes_From_Source
(Parent
(N
))
4614 elsif No
(Encl_Prag
) then
4615 Error_Attr
("attribute% must appear within appropriate pragma", N
);
4618 -- A Loop_Entry that applies to a given loop statement must not
4619 -- appear within a body of accept statement, if this construct is
4620 -- itself enclosed by the given loop statement.
4622 for Index
in reverse 0 .. Scope_Stack
.Last
loop
4623 Scop
:= Scope_Stack
.Table
(Index
).Entity
;
4625 if Ekind
(Scop
) = E_Loop
and then Scop
= Loop_Id
then
4627 elsif Ekind
(Scop
) in E_Block | E_Loop | E_Return_Statement
then
4631 ("attribute % cannot appear in body or accept statement", N
);
4636 -- The prefix cannot mention entities declared within the related
4637 -- loop because they will not be visible once the prefix is moved
4638 -- outside the loop.
4640 Check_References_In_Prefix
(Loop_Id
);
4642 -- The prefix must statically name an object if the pragma does not
4643 -- apply to the innermost enclosing loop statement, or if it appears
4644 -- within a potentially unevaluated expression.
4646 if Is_Entity_Name
(P
)
4647 or else Nkind
(Parent
(P
)) = N_Object_Renaming_Declaration
4648 or else Statically_Names_Object
(P
)
4652 elsif Present
(Encl_Loop
)
4653 and then Entity
(Identifier
(Encl_Loop
)) /= Loop_Id
4656 ("prefix of attribute % that applies to outer loop must denote "
4659 elsif Is_Potentially_Unevaluated
(P
) then
4663 -- Replace the Loop_Entry attribute reference by its prefix if the
4664 -- related pragma is ignored. This transformation is OK with respect
4665 -- to typing because Loop_Entry's type is that of its prefix. This
4666 -- early transformation also avoids the generation of a useless loop
4669 if Present
(Encl_Prag
) and then Is_Ignored
(Encl_Prag
) then
4670 Rewrite
(N
, Relocate_Node
(P
));
4671 Preanalyze_And_Resolve
(N
);
4674 Preanalyze_And_Resolve
(P
);
4682 when Attribute_Machine
=>
4683 Check_Floating_Point_Type_1
;
4684 Set_Etype
(N
, P_Base_Type
);
4685 Resolve
(E1
, P_Base_Type
);
4691 when Attribute_Machine_Emax
=>
4692 Check_Floating_Point_Type_0
;
4693 Set_Etype
(N
, Universal_Integer
);
4699 when Attribute_Machine_Emin
=>
4700 Check_Floating_Point_Type_0
;
4701 Set_Etype
(N
, Universal_Integer
);
4703 ----------------------
4704 -- Machine_Mantissa --
4705 ----------------------
4707 when Attribute_Machine_Mantissa
=>
4708 Check_Floating_Point_Type_0
;
4709 Set_Etype
(N
, Universal_Integer
);
4711 -----------------------
4712 -- Machine_Overflows --
4713 -----------------------
4715 when Attribute_Machine_Overflows
=>
4718 Set_Etype
(N
, Standard_Boolean
);
4724 when Attribute_Machine_Radix
=>
4727 Set_Etype
(N
, Universal_Integer
);
4729 ----------------------
4730 -- Machine_Rounding --
4731 ----------------------
4733 when Attribute_Machine_Rounding
=>
4734 Check_Floating_Point_Type_1
;
4735 Set_Etype
(N
, P_Base_Type
);
4736 Resolve
(E1
, P_Base_Type
);
4738 --------------------
4739 -- Machine_Rounds --
4740 --------------------
4742 when Attribute_Machine_Rounds
=>
4745 Set_Etype
(N
, Standard_Boolean
);
4751 when Attribute_Machine_Size
=>
4754 Check_Not_Incomplete_Type
;
4755 Set_Etype
(N
, Universal_Integer
);
4761 when Attribute_Mantissa
=>
4764 Set_Etype
(N
, Universal_Integer
);
4770 when Attribute_Max
=>
4773 ----------------------------------
4774 -- Max_Alignment_For_Allocation --
4775 ----------------------------------
4777 when Attribute_Max_Size_In_Storage_Elements
=>
4778 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
4780 ----------------------
4781 -- Max_Integer_Size --
4782 ----------------------
4784 when Attribute_Max_Integer_Size
=>
4785 Standard_Attribute
(System_Max_Integer_Size
);
4787 ----------------------------------
4788 -- Max_Size_In_Storage_Elements --
4789 ----------------------------------
4791 when Attribute_Max_Alignment_For_Allocation
=>
4792 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
4794 -----------------------
4795 -- Maximum_Alignment --
4796 -----------------------
4798 when Attribute_Maximum_Alignment
=>
4799 Standard_Attribute
(Ttypes
.Maximum_Alignment
);
4801 --------------------
4802 -- Mechanism_Code --
4803 --------------------
4805 when Attribute_Mechanism_Code
=>
4806 if not Is_Entity_Name
(P
)
4807 or else not Is_Subprogram
(Entity
(P
))
4809 Error_Attr_P
("prefix of % attribute must be subprogram");
4812 Check_Either_E0_Or_E1
;
4814 if Present
(E1
) then
4815 Resolve
(E1
, Any_Integer
);
4816 Set_Etype
(E1
, Standard_Integer
);
4818 if not Is_OK_Static_Expression
(E1
) then
4819 Flag_Non_Static_Expr
4820 ("expression for parameter number must be static!", E1
);
4823 elsif UI_To_Int
(Intval
(E1
)) > Number_Formals
(Entity
(P
))
4824 or else UI_To_Int
(Intval
(E1
)) < 0
4826 Error_Attr
("invalid parameter number for % attribute", E1
);
4830 Set_Etype
(N
, Universal_Integer
);
4836 when Attribute_Min
=>
4843 when Attribute_Mod
=>
4845 -- Note: this attribute is only allowed in Ada 2005 mode, but
4846 -- we do not need to test that here, since Mod is only recognized
4847 -- as an attribute name in Ada 2005 mode during the parse.
4850 Check_Modular_Integer_Type
;
4851 Resolve
(E1
, Any_Integer
);
4852 Set_Etype
(N
, P_Base_Type
);
4858 when Attribute_Model
=>
4859 Check_Floating_Point_Type_1
;
4860 Set_Etype
(N
, P_Base_Type
);
4861 Resolve
(E1
, P_Base_Type
);
4867 when Attribute_Model_Emin
=>
4868 Check_Floating_Point_Type_0
;
4869 Set_Etype
(N
, Universal_Integer
);
4875 when Attribute_Model_Epsilon
=>
4876 Check_Floating_Point_Type_0
;
4877 Set_Etype
(N
, Universal_Real
);
4879 --------------------
4880 -- Model_Mantissa --
4881 --------------------
4883 when Attribute_Model_Mantissa
=>
4884 Check_Floating_Point_Type_0
;
4885 Set_Etype
(N
, Universal_Integer
);
4891 when Attribute_Model_Small
=>
4892 Check_Floating_Point_Type_0
;
4893 Set_Etype
(N
, Universal_Real
);
4899 when Attribute_Modulus
=>
4901 Check_Modular_Integer_Type
;
4902 Set_Etype
(N
, Universal_Integer
);
4904 --------------------
4905 -- Null_Parameter --
4906 --------------------
4908 when Attribute_Null_Parameter
=> Null_Parameter
: declare
4909 Parnt
: constant Node_Id
:= Parent
(N
);
4910 GParnt
: constant Node_Id
:= Parent
(Parnt
);
4912 procedure Bad_Null_Parameter
(Msg
: String);
4913 -- Used if bad Null parameter attribute node is found. Issues
4914 -- given error message, and also sets the type to Any_Type to
4915 -- avoid blowups later on from dealing with a junk node.
4917 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
);
4918 -- Called to check that Proc_Ent is imported subprogram
4920 ------------------------
4921 -- Bad_Null_Parameter --
4922 ------------------------
4924 procedure Bad_Null_Parameter
(Msg
: String) is
4926 Error_Msg_N
(Msg
, N
);
4927 Set_Etype
(N
, Any_Type
);
4928 end Bad_Null_Parameter
;
4930 ----------------------
4931 -- Must_Be_Imported --
4932 ----------------------
4934 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
) is
4935 Pent
: constant Entity_Id
:= Ultimate_Alias
(Proc_Ent
);
4938 -- Ignore check if procedure not frozen yet (we will get
4939 -- another chance when the default parameter is reanalyzed)
4941 if not Is_Frozen
(Pent
) then
4944 elsif not Is_Imported
(Pent
) then
4946 ("Null_Parameter can only be used with imported subprogram");
4951 end Must_Be_Imported
;
4953 -- Start of processing for Null_Parameter
4958 Set_Etype
(N
, P_Type
);
4960 -- Case of attribute used as default expression
4962 if Nkind
(Parnt
) = N_Parameter_Specification
then
4963 Must_Be_Imported
(Defining_Entity
(GParnt
));
4965 -- Case of attribute used as actual for subprogram (positional)
4967 elsif Nkind
(Parnt
) in N_Subprogram_Call
4968 and then Is_Entity_Name
(Name
(Parnt
))
4970 Must_Be_Imported
(Entity
(Name
(Parnt
)));
4972 -- Case of attribute used as actual for subprogram (named)
4974 elsif Nkind
(Parnt
) = N_Parameter_Association
4975 and then Nkind
(GParnt
) in N_Subprogram_Call
4976 and then Is_Entity_Name
(Name
(GParnt
))
4978 Must_Be_Imported
(Entity
(Name
(GParnt
)));
4980 -- Not an allowed case
4984 ("Null_Parameter must be actual or default parameter");
4992 when Attribute_Object_Size
=>
4995 Check_Not_Incomplete_Type
;
4996 Set_Etype
(N
, Universal_Integer
);
5002 when Attribute_Old
=> Old
: declare
5003 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
);
5004 -- Inspect the contents of the prefix and detect illegal uses of a
5005 -- nested 'Old, attribute 'Result or a use of an entity declared in
5006 -- the related postcondition expression. Subp_Id is the subprogram to
5007 -- which the related postcondition applies.
5009 --------------------------------
5010 -- Check_References_In_Prefix --
5011 --------------------------------
5013 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
) is
5014 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
5015 -- Detect attribute 'Old, attribute 'Result of a use of an entity
5016 -- and perform the appropriate semantic check.
5018 ---------------------
5019 -- Check_Reference --
5020 ---------------------
5022 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
5024 -- Attributes 'Old and 'Result cannot appear in the prefix of
5025 -- another attribute 'Old.
5027 if Nkind
(Nod
) = N_Attribute_Reference
5028 and then Attribute_Name
(Nod
) in Name_Old | Name_Result
5030 Error_Msg_Name_1
:= Attribute_Name
(Nod
);
5031 Error_Msg_Name_2
:= Name_Old
;
5033 ("attribute % cannot appear in the prefix of attribute %",
5037 -- Entities mentioned within the prefix of attribute 'Old must
5038 -- be global to the related postcondition. If this is not the
5039 -- case, then the scope of the local entity is nested within
5040 -- that of the subprogram.
5042 elsif Is_Entity_Name
(Nod
)
5043 and then Present
(Entity
(Nod
))
5044 and then Scope_Within
(Scope
(Entity
(Nod
)), Subp_Id
)
5047 ("prefix of attribute % cannot reference local entities",
5051 -- Otherwise keep inspecting the prefix
5056 end Check_Reference
;
5058 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
5060 -- Start of processing for Check_References_In_Prefix
5063 Check_References
(P
);
5064 end Check_References_In_Prefix
;
5069 Pref_Id
: Entity_Id
;
5070 Pref_Typ
: Entity_Id
;
5071 Spec_Id
: Entity_Id
;
5073 -- Start of processing for Old
5076 -- The attribute reference is a primary. If any expressions follow,
5077 -- then the attribute reference is an indexable object. Transform the
5078 -- attribute into an indexed component and analyze it.
5080 if Present
(E1
) then
5082 Make_Indexed_Component
(Loc
,
5084 Make_Attribute_Reference
(Loc
,
5085 Prefix
=> Relocate_Node
(P
),
5086 Attribute_Name
=> Name_Old
),
5087 Expressions
=> Expressions
(N
)));
5092 Analyze_Attribute_Old_Result
(Legal
, Spec_Id
);
5094 -- The aspect or pragma where attribute 'Old resides should be
5095 -- associated with a subprogram declaration or a body. If this is not
5096 -- the case, then the aspect or pragma is illegal. Return as analysis
5097 -- cannot be carried out.
5099 -- The exception to this rule is when generating C since in this case
5100 -- postconditions are inlined.
5103 and then Modify_Tree_For_C
5104 and then In_Inlined_Body
5106 Spec_Id
:= Entity
(P
);
5108 elsif not Legal
then
5112 -- The prefix must be preanalyzed as the full analysis will take
5113 -- place during expansion.
5115 Preanalyze_And_Resolve
(P
);
5117 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5119 Check_References_In_Prefix
(Spec_Id
);
5121 -- Set the type of the attribute now to prevent cascaded errors
5123 Pref_Typ
:= Etype
(P
);
5124 Set_Etype
(N
, Pref_Typ
);
5128 if Is_Limited_Type
(Pref_Typ
) then
5129 Error_Attr
("attribute % cannot apply to limited objects", P
);
5132 -- The prefix is a simple name
5134 if Is_Entity_Name
(P
) and then Present
(Entity
(P
)) then
5135 Pref_Id
:= Entity
(P
);
5137 -- Emit a warning when the prefix is a constant. Note that the use
5138 -- of Error_Attr would reset the type of N to Any_Type even though
5139 -- this is a warning. Use Error_Msg_XXX instead.
5141 if Is_Constant_Object
(Pref_Id
) then
5142 Error_Msg_Name_1
:= Name_Old
;
5144 ("??attribute % applied to constant has no effect", P
);
5147 -- Otherwise the prefix is not a simple name
5150 -- Ensure that the prefix of attribute 'Old is an entity when it
5151 -- is potentially unevaluated (6.1.1 (27/3)). This rule is
5152 -- relaxed in Ada2020 - this relaxation is reflected in the
5153 -- call (below) to Eligible_For_Conditional_Evaluation.
5155 if Is_Potentially_Unevaluated
(N
)
5156 and then not Statically_Names_Object
(P
)
5158 Old_Attr_Util
.Conditional_Evaluation
5159 .Eligible_For_Conditional_Evaluation
(N
)
5163 -- Detect a possible infinite recursion when the prefix denotes
5164 -- the related function.
5166 -- function Func (...) return ...
5167 -- with Post => Func'Old ...;
5169 -- The function may be specified in qualified form X.Y where X is
5170 -- a protected object and Y is a protected function. In that case
5171 -- ensure that the qualified form has an entity.
5173 elsif Nkind
(P
) = N_Function_Call
5174 and then Nkind
(Name
(P
)) in N_Has_Entity
5176 Pref_Id
:= Entity
(Name
(P
));
5178 if Ekind
(Spec_Id
) in E_Function | E_Generic_Function
5179 and then Pref_Id
= Spec_Id
5181 Error_Msg_Warn
:= SPARK_Mode
/= On
;
5182 Error_Msg_N
("!possible infinite recursion<<", P
);
5183 Error_Msg_N
("\!??Storage_Error ]<<", P
);
5187 -- The prefix of attribute 'Old may refer to a component of a
5188 -- formal parameter. In this case its expansion may generate
5189 -- actual subtypes that are referenced in an inner context and
5190 -- that must be elaborated within the subprogram itself. If the
5191 -- prefix includes a function call, it may involve finalization
5192 -- actions that should be inserted when the attribute has been
5193 -- rewritten as a declaration. Create a declaration for the prefix
5194 -- and insert it at the start of the enclosing subprogram. This is
5195 -- an expansion activity that has to be performed now to prevent
5196 -- out-of-order issues.
5198 -- This expansion is both harmful and not needed in SPARK mode,
5199 -- since the formal verification back end relies on the types of
5200 -- nodes (hence is not robust w.r.t. a change to base type here),
5201 -- and does not suffer from the out-of-order issue described
5202 -- above. Thus, this expansion is skipped in SPARK mode.
5204 -- The expansion is not relevant for discrete types, which will
5205 -- not generate extra declarations, and where use of the base type
5206 -- may lead to spurious errors if context is a case.
5208 if not GNATprove_Mode
then
5209 if not Is_Discrete_Type
(Pref_Typ
) then
5210 Pref_Typ
:= Base_Type
(Pref_Typ
);
5213 Set_Etype
(N
, Pref_Typ
);
5214 Set_Etype
(P
, Pref_Typ
);
5216 Analyze_Dimension
(N
);
5222 ----------------------
5223 -- Overlaps_Storage --
5224 ----------------------
5226 when Attribute_Overlaps_Storage
=>
5229 -- Both arguments must be objects of any type
5231 Analyze_And_Resolve
(P
);
5232 Analyze_And_Resolve
(E1
);
5233 Check_Object_Reference
(P
);
5234 Check_Object_Reference
(E1
);
5235 Set_Etype
(N
, Standard_Boolean
);
5241 when Attribute_Output
=>
5243 Check_Stream_Attribute
(TSS_Stream_Output
);
5244 Set_Etype
(N
, Standard_Void_Type
);
5245 Resolve
(N
, Standard_Void_Type
);
5251 when Attribute_Partition_ID
=>
5254 if P_Type
/= Any_Type
then
5255 if not Is_Library_Level_Entity
(Entity
(P
)) then
5257 ("prefix of % attribute must be library-level entity");
5259 -- The defining entity of prefix should not be declared inside a
5260 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5262 elsif Is_Entity_Name
(P
)
5263 and then Is_Pure
(Entity
(P
))
5265 Error_Attr_P
("prefix of% attribute must not be declared pure");
5269 Set_Etype
(N
, Universal_Integer
);
5271 -------------------------
5272 -- Passed_By_Reference --
5273 -------------------------
5275 when Attribute_Passed_By_Reference
=>
5278 Check_Not_Incomplete_Type
;
5279 Set_Etype
(N
, Standard_Boolean
);
5285 when Attribute_Pool_Address
=>
5287 Set_Etype
(N
, RTE
(RE_Address
));
5293 when Attribute_Pos
=>
5294 Check_Discrete_Type
;
5296 Resolve
(E1
, P_Base_Type
);
5297 Set_Etype
(N
, Universal_Integer
);
5303 when Attribute_Position
=>
5305 Set_Etype
(N
, Universal_Integer
);
5311 when Attribute_Pred
=>
5314 Resolve
(E1
, P_Base_Type
);
5315 Set_Etype
(N
, P_Base_Type
);
5317 -- Since Pred works on the base type, we normally do no check for the
5318 -- floating-point case, since the base type is unconstrained. But we
5319 -- make an exception in Check_Float_Overflow mode.
5321 if Is_Floating_Point_Type
(P_Type
) then
5322 if not Range_Checks_Suppressed
(P_Base_Type
) then
5323 Set_Do_Range_Check
(E1
);
5326 -- If not modular type, test for overflow check required
5329 if not Is_Modular_Integer_Type
(P_Type
)
5330 and then not Range_Checks_Suppressed
(P_Base_Type
)
5332 Enable_Range_Check
(E1
);
5340 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5342 when Attribute_Priority
=>
5343 if Ada_Version
< Ada_2005
then
5344 Error_Attr
("% attribute is allowed only in Ada 2005 mode", P
);
5349 Check_Restriction
(No_Dynamic_Priorities
, N
);
5351 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5355 if Is_Protected_Type
(Etype
(P
))
5356 or else (Is_Access_Type
(Etype
(P
))
5357 and then Is_Protected_Type
(Designated_Type
(Etype
(P
))))
5359 Resolve
(P
, Etype
(P
));
5361 Error_Attr_P
("prefix of % attribute must be a protected object");
5364 Set_Etype
(N
, Standard_Integer
);
5366 -- Must be called from within a protected procedure or entry of the
5367 -- protected object.
5374 while S
/= Etype
(P
)
5375 and then S
/= Standard_Standard
5380 if S
= Standard_Standard
then
5381 Error_Attr
("the attribute % is only allowed inside protected "
5386 Validate_Non_Static_Attribute_Function_Call
;
5392 when Attribute_Put_Image
=>
5394 Check_Put_Image_Attribute
;
5395 Set_Etype
(N
, Standard_Void_Type
);
5396 Resolve
(N
, Standard_Void_Type
);
5402 when Attribute_Range
=>
5403 Check_Array_Or_Scalar_Type
;
5404 Bad_Attribute_For_Predicate
;
5406 if Ada_Version
= Ada_83
5407 and then Is_Scalar_Type
(P_Type
)
5408 and then Comes_From_Source
(N
)
5411 ("(Ada 83) % attribute not allowed for scalar type", P
);
5418 when Attribute_Result
=> Result
: declare
5419 function Denote_Same_Function
5420 (Pref_Id
: Entity_Id
;
5421 Spec_Id
: Entity_Id
) return Boolean;
5422 -- Determine whether the entity of the prefix Pref_Id denotes the
5423 -- same entity as that of the related subprogram Spec_Id.
5425 --------------------------
5426 -- Denote_Same_Function --
5427 --------------------------
5429 function Denote_Same_Function
5430 (Pref_Id
: Entity_Id
;
5431 Spec_Id
: Entity_Id
) return Boolean
5433 Over_Id
: constant Entity_Id
:= Overridden_Operation
(Spec_Id
);
5434 Subp_Spec
: constant Node_Id
:= Parent
(Spec_Id
);
5437 -- The prefix denotes the related subprogram
5439 if Pref_Id
= Spec_Id
then
5442 -- Account for a special case when attribute 'Result appears in
5443 -- the postcondition of a generic function.
5446 -- function Gen_Func return ...
5447 -- with Post => Gen_Func'Result ...;
5449 -- When the generic function is instantiated, the Chars field of
5450 -- the instantiated prefix still denotes the name of the generic
5451 -- function. Note that any preemptive transformation is impossible
5452 -- without a proper analysis. The structure of the wrapper package
5455 -- package Anon_Gen_Pack is
5456 -- <subtypes and renamings>
5457 -- function Subp_Decl return ...; -- (!)
5458 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5459 -- function Gen_Func ... renames Subp_Decl;
5460 -- end Anon_Gen_Pack;
5462 elsif Nkind
(Subp_Spec
) = N_Function_Specification
5463 and then Present
(Generic_Parent
(Subp_Spec
))
5464 and then Ekind
(Pref_Id
) in E_Generic_Function | E_Function
5466 if Generic_Parent
(Subp_Spec
) = Pref_Id
then
5469 elsif Present
(Alias
(Pref_Id
))
5470 and then Alias
(Pref_Id
) = Spec_Id
5475 -- Account for a special case where a primitive of a tagged type
5476 -- inherits a class-wide postcondition from a parent type. In this
5477 -- case the prefix of attribute 'Result denotes the overriding
5480 elsif Present
(Over_Id
) and then Pref_Id
= Over_Id
then
5484 -- Otherwise the prefix does not denote the related subprogram
5487 end Denote_Same_Function
;
5491 In_Inlined_C_Postcondition
: constant Boolean :=
5493 and then In_Inlined_Body
;
5496 Pref_Id
: Entity_Id
;
5497 Spec_Id
: Entity_Id
;
5499 -- Start of processing for Result
5502 -- The attribute reference is a primary. If any expressions follow,
5503 -- then the attribute reference is an indexable object. Transform the
5504 -- attribute into an indexed component and analyze it.
5506 if Present
(E1
) then
5508 Make_Indexed_Component
(Loc
,
5510 Make_Attribute_Reference
(Loc
,
5511 Prefix
=> Relocate_Node
(P
),
5512 Attribute_Name
=> Name_Result
),
5513 Expressions
=> Expressions
(N
)));
5518 Analyze_Attribute_Old_Result
(Legal
, Spec_Id
);
5520 -- The aspect or pragma where attribute 'Result resides should be
5521 -- associated with a subprogram declaration or a body. If this is not
5522 -- the case, then the aspect or pragma is illegal. Return as analysis
5523 -- cannot be carried out.
5525 -- The exception to this rule is when generating C since in this case
5526 -- postconditions are inlined.
5528 if No
(Spec_Id
) and then In_Inlined_C_Postcondition
then
5529 Spec_Id
:= Entity
(P
);
5531 elsif not Legal
then
5532 Error_Attr
("prefix of % attribute must be a function", P
);
5536 -- Attribute 'Result is part of a _Postconditions procedure. There is
5537 -- no need to perform the semantic checks below as they were already
5538 -- verified when the attribute was analyzed in its original context.
5539 -- Instead, rewrite the attribute as a reference to formal parameter
5540 -- _Result of the _Postconditions procedure.
5542 if Chars
(Spec_Id
) = Name_uPostconditions
5544 (In_Inlined_C_Postcondition
5545 and then Nkind
(Parent
(Spec_Id
)) = N_Block_Statement
)
5547 Rewrite
(N
, Make_Identifier
(Loc
, Name_uResult
));
5549 -- The type of formal parameter _Result is that of the function
5550 -- encapsulating the _Postconditions procedure. Resolution must
5551 -- be carried out against the function return type.
5553 Analyze_And_Resolve
(N
, Etype
(Scope
(Spec_Id
)));
5555 -- Otherwise attribute 'Result appears in its original context and
5556 -- all semantic checks should be carried out.
5559 -- Verify the legality of the prefix. It must denotes the entity
5560 -- of the related [generic] function.
5562 if Is_Entity_Name
(P
) then
5563 Pref_Id
:= Entity
(P
);
5565 -- Either both the prefix and the annotated spec must be
5566 -- generic functions, or they both must be nongeneric
5567 -- functions, or the prefix must be generic and the spec
5568 -- must be nongeneric (i.e. it must denote an instance).
5570 if (Ekind
(Pref_Id
) in E_Function | E_Generic_Function
5571 and then Ekind
(Pref_Id
) = Ekind
(Spec_Id
))
5573 (Ekind
(Pref_Id
) = E_Generic_Function
5574 and then Ekind
(Spec_Id
) = E_Function
)
5576 if Denote_Same_Function
(Pref_Id
, Spec_Id
) then
5578 -- Correct the prefix of the attribute when the context
5579 -- is a generic function.
5581 if Pref_Id
/= Spec_Id
then
5582 Rewrite
(P
, New_Occurrence_Of
(Spec_Id
, Loc
));
5586 Set_Etype
(N
, Etype
(Spec_Id
));
5588 -- Otherwise the prefix denotes some unrelated function
5591 Error_Msg_Name_2
:= Chars
(Spec_Id
);
5593 ("incorrect prefix for attribute %, expected %", P
);
5596 -- Otherwise the prefix denotes some other form of subprogram
5601 ("attribute % can only appear in postcondition of "
5605 -- Otherwise the prefix is illegal
5608 Error_Msg_Name_2
:= Chars
(Spec_Id
);
5609 Error_Attr
("incorrect prefix for attribute %, expected %", P
);
5618 when Attribute_Range_Length
=>
5620 Check_Discrete_Type
;
5621 Set_Etype
(N
, Universal_Integer
);
5627 when Attribute_Reduce
=>
5630 if not Extensions_Allowed
then
5632 ("% attribute only supported under -gnatX", P
);
5636 Stream
: constant Node_Id
:= Prefix
(N
);
5639 if Nkind
(Stream
) /= N_Aggregate
then
5640 -- Prefix is a name, as for other attributes.
5642 -- If the object is a function we asume that it is not
5643 -- overloaded. AI12-242 does not suggest a name resolution
5644 -- rule for that case, but we can suppose that the expected
5645 -- type of the reduction is the expected type of the component
5648 Analyze_And_Resolve
(Stream
);
5649 Typ
:= Etype
(Stream
);
5651 -- Verify that prefix can be iterated upon.
5653 if Is_Array_Type
(Typ
)
5654 or else Present
(Find_Aspect
(Typ
, Aspect_Default_Iterator
))
5655 or else Present
(Find_Aspect
(Typ
, Aspect_Iterable
))
5660 ("cannot apply reduce to object of type$", N
, Typ
);
5663 elsif Present
(Expressions
(Stream
))
5664 or else No
(Component_Associations
(Stream
))
5665 or else Nkind
(First
(Component_Associations
(Stream
))) /=
5666 N_Iterated_Component_Association
5669 ("Prefix of reduce must be an iterated component", N
);
5674 Set_Etype
(N
, Etype
(E2
));
5681 when Attribute_Read
=>
5683 Check_Stream_Attribute
(TSS_Stream_Read
);
5684 Set_Etype
(N
, Standard_Void_Type
);
5685 Resolve
(N
, Standard_Void_Type
);
5686 Note_Possible_Modification
(E2
, Sure
=> True);
5692 when Attribute_Ref
=>
5696 if Nkind
(P
) /= N_Expanded_Name
5697 or else not Is_RTE
(P_Type
, RE_Address
)
5699 Error_Attr_P
("prefix of % attribute must be System.Address");
5702 Analyze_And_Resolve
(E1
, Any_Integer
);
5703 Set_Etype
(N
, RTE
(RE_Address
));
5709 when Attribute_Remainder
=>
5710 Check_Floating_Point_Type_2
;
5711 Set_Etype
(N
, P_Base_Type
);
5712 Resolve
(E1
, P_Base_Type
);
5713 Resolve
(E2
, P_Base_Type
);
5715 ---------------------
5716 -- Restriction_Set --
5717 ---------------------
5719 when Attribute_Restriction_Set
=> Restriction_Set
: declare
5722 Unam
: Unit_Name_Type
;
5727 Check_System_Prefix
;
5729 -- No_Dependence case
5731 if Nkind
(E1
) = N_Parameter_Association
then
5732 pragma Assert
(Chars
(Selector_Name
(E1
)) = Name_No_Dependence
);
5733 U
:= Explicit_Actual_Parameter
(E1
);
5735 if not OK_No_Dependence_Unit_Name
(U
) then
5736 Set_Boolean_Result
(N
, False);
5740 -- See if there is an entry already in the table. That's the
5741 -- case in which we can return True.
5743 for J
in No_Dependences
.First
.. No_Dependences
.Last
loop
5744 if Designate_Same_Unit
(U
, No_Dependences
.Table
(J
).Unit
)
5745 and then No_Dependences
.Table
(J
).Warn
= False
5747 Set_Boolean_Result
(N
, True);
5752 -- If not in the No_Dependence table, result is False
5754 Set_Boolean_Result
(N
, False);
5756 -- In this case, we must ensure that the binder will reject any
5757 -- other unit in the partition that sets No_Dependence for this
5758 -- unit. We do that by making an entry in the special table kept
5759 -- for this purpose (if the entry is not there already).
5761 Unam
:= Get_Spec_Name
(Get_Unit_Name
(U
));
5763 for J
in Restriction_Set_Dependences
.First
..
5764 Restriction_Set_Dependences
.Last
5766 if Restriction_Set_Dependences
.Table
(J
) = Unam
then
5771 Restriction_Set_Dependences
.Append
(Unam
);
5773 -- Normal restriction case
5776 if Nkind
(E1
) /= N_Identifier
then
5777 Set_Boolean_Result
(N
, False);
5778 Error_Attr
("attribute % requires restriction identifier", E1
);
5781 R
:= Get_Restriction_Id
(Process_Restriction_Synonyms
(E1
));
5783 if R
= Not_A_Restriction_Id
then
5784 Set_Boolean_Result
(N
, False);
5785 Error_Msg_Node_1
:= E1
;
5786 Error_Attr
("invalid restriction identifier &", E1
);
5788 elsif R
not in Partition_Boolean_Restrictions
then
5789 Set_Boolean_Result
(N
, False);
5790 Error_Msg_Node_1
:= E1
;
5792 ("& is not a boolean partition-wide restriction", E1
);
5795 if Restriction_Active
(R
) then
5796 Set_Boolean_Result
(N
, True);
5798 Check_Restriction
(R
, N
);
5799 Set_Boolean_Result
(N
, False);
5803 end Restriction_Set
;
5809 when Attribute_Round
=>
5811 Check_Decimal_Fixed_Point_Type
;
5812 Set_Etype
(N
, P_Base_Type
);
5814 -- Because the context is universal_real (3.5.10(12)) it is a
5815 -- legal context for a universal fixed expression. This is the
5816 -- only attribute whose functional description involves U_R.
5818 if Etype
(E1
) = Universal_Fixed
then
5820 Conv
: constant Node_Id
:= Make_Type_Conversion
(Loc
,
5821 Subtype_Mark
=> New_Occurrence_Of
(Universal_Real
, Loc
),
5822 Expression
=> Relocate_Node
(E1
));
5830 Resolve
(E1
, Any_Real
);
5836 when Attribute_Rounding
=>
5837 Check_Floating_Point_Type_1
;
5838 Set_Etype
(N
, P_Base_Type
);
5839 Resolve
(E1
, P_Base_Type
);
5845 when Attribute_Safe_Emax
=>
5846 Check_Floating_Point_Type_0
;
5847 Set_Etype
(N
, Universal_Integer
);
5853 when Attribute_Safe_First
=>
5854 Check_Floating_Point_Type_0
;
5855 Set_Etype
(N
, Universal_Real
);
5861 when Attribute_Safe_Large
=>
5864 Set_Etype
(N
, Universal_Real
);
5870 when Attribute_Safe_Last
=>
5871 Check_Floating_Point_Type_0
;
5872 Set_Etype
(N
, Universal_Real
);
5878 when Attribute_Safe_Small
=>
5881 Set_Etype
(N
, Universal_Real
);
5883 --------------------------
5884 -- Scalar_Storage_Order --
5885 --------------------------
5887 when Attribute_Scalar_Storage_Order
=> Scalar_Storage_Order
: declare
5888 Ent
: Entity_Id
:= Empty
;
5894 if not (Is_Record_Type
(P_Type
) or else Is_Array_Type
(P_Type
)) then
5896 -- The attribute applies to generic private types (in which case
5897 -- the legality rule is applied in the instance) as well as to
5898 -- composite types. For noncomposite types it always returns the
5899 -- default bit order for the target.
5900 -- Allowing formal private types was originally introduced in
5901 -- GNAT_Mode only, to compile instances of Sequential_IO, but
5902 -- users find it more generally useful in generic units.
5904 if not (Is_Generic_Type
(P_Type
) and then Is_Private_Type
(P_Type
))
5905 and then not In_Instance
5908 ("prefix of % attribute must be record or array type");
5910 elsif not Is_Generic_Type
(P_Type
) then
5911 if Bytes_Big_Endian
then
5912 Ent
:= RTE
(RE_High_Order_First
);
5914 Ent
:= RTE
(RE_Low_Order_First
);
5918 elsif Bytes_Big_Endian
xor Reverse_Storage_Order
(P_Type
) then
5919 Ent
:= RTE
(RE_High_Order_First
);
5922 Ent
:= RTE
(RE_Low_Order_First
);
5925 if Present
(Ent
) then
5926 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
5929 Set_Etype
(N
, RTE
(RE_Bit_Order
));
5932 -- Reset incorrect indication of staticness
5934 Set_Is_Static_Expression
(N
, False);
5935 end Scalar_Storage_Order
;
5941 when Attribute_Scale
=>
5943 Check_Decimal_Fixed_Point_Type
;
5944 Set_Etype
(N
, Universal_Integer
);
5950 when Attribute_Scaling
=>
5951 Check_Floating_Point_Type_2
;
5952 Set_Etype
(N
, P_Base_Type
);
5953 Resolve
(E1
, P_Base_Type
);
5959 when Attribute_Signed_Zeros
=>
5960 Check_Floating_Point_Type_0
;
5961 Set_Etype
(N
, Standard_Boolean
);
5968 | Attribute_VADS_Size
5972 -- If prefix is parameterless function call, rewrite and resolve
5975 if Is_Entity_Name
(P
)
5976 and then Ekind
(Entity
(P
)) = E_Function
5980 -- Similar processing for a protected function call
5982 elsif Nkind
(P
) = N_Selected_Component
5983 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Function
5988 if Is_Object_Reference
(P
) then
5989 Check_Object_Reference
(P
);
5991 elsif Is_Entity_Name
(P
)
5992 and then (Is_Type
(Entity
(P
))
5993 or else Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
5997 elsif Nkind
(P
) = N_Type_Conversion
5998 and then not Comes_From_Source
(P
)
6002 -- Some other compilers allow dubious use of X'???'Size
6004 elsif Relaxed_RM_Semantics
6005 and then Nkind
(P
) = N_Attribute_Reference
6010 Error_Attr_P
("invalid prefix for % attribute");
6013 Check_Not_Incomplete_Type
;
6015 Set_Etype
(N
, Universal_Integer
);
6017 -- If we are processing pragmas Compile_Time_Warning and Compile_
6018 -- Time_Errors after the back end has been called and this occurrence
6019 -- of 'Size is known at compile time then it is safe to perform this
6020 -- evaluation. Needed to perform the static evaluation of the full
6021 -- boolean expression of these pragmas. Note that Known_RM_Size is
6022 -- sometimes True when Size_Known_At_Compile_Time is False, when the
6023 -- back end has computed it.
6025 if In_Compile_Time_Warning_Or_Error
6026 and then Is_Entity_Name
(P
)
6027 and then (Is_Type
(Entity
(P
))
6028 or else Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
6029 and then (Known_RM_Size
(Entity
(P
))
6030 or else Size_Known_At_Compile_Time
(Entity
(P
)))
6036 if Known_Static_RM_Size
(Entity
(P
)) then
6037 Siz
:= RM_Size
(Entity
(P
));
6039 Siz
:= Esize
(Entity
(P
));
6042 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Siz
));
6051 when Attribute_Small
=>
6054 Set_Etype
(N
, Universal_Real
);
6060 when Attribute_Storage_Pool
6061 | Attribute_Simple_Storage_Pool
6065 if Is_Access_Type
(P_Type
) then
6066 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
6068 ("cannot use % attribute for access-to-subprogram type");
6071 -- Set appropriate entity
6073 if Present
(Associated_Storage_Pool
(Root_Type
(P_Type
))) then
6074 Set_Entity
(N
, Associated_Storage_Pool
(Root_Type
(P_Type
)));
6076 Set_Entity
(N
, RTE
(RE_Global_Pool_Object
));
6079 if Attr_Id
= Attribute_Storage_Pool
then
6080 if Present
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
6081 Name_Simple_Storage_Pool_Type
))
6083 Error_Msg_Name_1
:= Aname
;
6084 Error_Msg_Warn
:= SPARK_Mode
/= On
;
6086 ("cannot use % attribute for type with simple storage "
6088 Error_Msg_N
("\Program_Error [<<", N
);
6091 (N
, Make_Raise_Program_Error
6092 (Sloc
(N
), Reason
=> PE_Explicit_Raise
));
6095 Set_Etype
(N
, Class_Wide_Type
(RTE
(RE_Root_Storage_Pool
)));
6097 -- In the Simple_Storage_Pool case, verify that the pool entity is
6098 -- actually of a simple storage pool type, and set the attribute's
6099 -- type to the pool object's type.
6102 if not Present
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
6103 Name_Simple_Storage_Pool_Type
))
6106 ("cannot use % attribute for type without simple " &
6110 Set_Etype
(N
, Etype
(Entity
(N
)));
6113 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6114 -- Storage_Pool since this attribute is not defined for such
6115 -- types (RM E.2.2(17)).
6117 Validate_Remote_Access_To_Class_Wide_Type
(N
);
6120 Error_Attr_P
("prefix of % attribute must be access type");
6127 when Attribute_Storage_Size
=>
6130 if Is_Task_Type
(P_Type
) then
6131 Set_Etype
(N
, Universal_Integer
);
6133 -- Use with tasks is an obsolescent feature
6135 Check_Restriction
(No_Obsolescent_Features
, P
);
6137 elsif Is_Access_Type
(P_Type
) then
6138 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
6140 ("cannot use % attribute for access-to-subprogram type");
6143 if Is_Entity_Name
(P
)
6144 and then Is_Type
(Entity
(P
))
6147 Set_Etype
(N
, Universal_Integer
);
6149 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6150 -- Storage_Size since this attribute is not defined for
6151 -- such types (RM E.2.2(17)).
6153 Validate_Remote_Access_To_Class_Wide_Type
(N
);
6155 -- The prefix is allowed to be an implicit dereference of an
6156 -- access value designating a task.
6160 Set_Etype
(N
, Universal_Integer
);
6164 Error_Attr_P
("prefix of % attribute must be access or task type");
6171 when Attribute_Storage_Unit
=>
6172 Standard_Attribute
(Ttypes
.System_Storage_Unit
);
6178 when Attribute_Stream_Size
=>
6182 if Is_Entity_Name
(P
)
6183 and then Is_Elementary_Type
(Entity
(P
))
6185 Set_Etype
(N
, Universal_Integer
);
6187 Error_Attr_P
("invalid prefix for % attribute");
6194 when Attribute_Stub_Type
=>
6198 if Is_Remote_Access_To_Class_Wide_Type
(Base_Type
(P_Type
)) then
6200 -- For a real RACW [sub]type, use corresponding stub type
6202 if not Is_Generic_Type
(P_Type
) then
6205 (Corresponding_Stub_Type
(Base_Type
(P_Type
)), Loc
));
6207 -- For a generic type (that has been marked as an RACW using the
6208 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6209 -- type. Note that if the actual is not a remote access type, the
6210 -- instantiation will fail.
6213 -- Note: we go to the underlying type here because the view
6214 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6218 (Underlying_Type
(RTE
(RE_RACW_Stub_Type
)), Loc
));
6223 ("prefix of% attribute must be remote access-to-class-wide");
6230 when Attribute_Succ
=>
6233 Resolve
(E1
, P_Base_Type
);
6234 Set_Etype
(N
, P_Base_Type
);
6236 -- Since Pred works on the base type, we normally do no check for the
6237 -- floating-point case, since the base type is unconstrained. But we
6238 -- make an exception in Check_Float_Overflow mode.
6240 if Is_Floating_Point_Type
(P_Type
) then
6241 if not Range_Checks_Suppressed
(P_Base_Type
) then
6242 Set_Do_Range_Check
(E1
);
6245 -- If not modular type, test for overflow check required
6248 if not Is_Modular_Integer_Type
(P_Type
)
6249 and then not Range_Checks_Suppressed
(P_Base_Type
)
6251 Enable_Range_Check
(E1
);
6255 --------------------------------
6256 -- System_Allocator_Alignment --
6257 --------------------------------
6259 when Attribute_System_Allocator_Alignment
=>
6260 Standard_Attribute
(Ttypes
.System_Allocator_Alignment
);
6266 when Attribute_Tag
=>
6270 if not Is_Tagged_Type
(P_Type
) then
6271 Error_Attr_P
("prefix of % attribute must be tagged");
6273 -- Next test does not apply to generated code why not, and what does
6274 -- the illegal reference mean???
6276 elsif Is_Object_Reference
(P
)
6277 and then not Is_Class_Wide_Type
(P_Type
)
6278 and then Comes_From_Source
(N
)
6281 ("% attribute can only be applied to objects " &
6282 "of class - wide type");
6285 -- The prefix cannot be an incomplete type. However, references to
6286 -- 'Tag can be generated when expanding interface conversions, and
6289 if Comes_From_Source
(N
) then
6290 Check_Not_Incomplete_Type
;
6292 -- 'Tag requires visibility on the corresponding package holding
6293 -- the tag, so record a reference here, to avoid spurious unused
6294 -- with_clause reported when compiling the main unit.
6296 if In_Extended_Main_Source_Unit
(Current_Scope
) then
6297 Set_Referenced
(P_Type
, True);
6298 Set_Referenced
(Scope
(P_Type
), True);
6302 -- Set appropriate type
6304 Set_Etype
(N
, RTE
(RE_Tag
));
6310 when Attribute_Target_Name
=> Target_Name
: declare
6311 TN
: constant String := Sdefault
.Target_Name
.all;
6315 Check_Standard_Prefix
;
6319 if TN
(TL
) = '/' or else TN
(TL
) = '\' then
6324 Make_String_Literal
(Loc
,
6325 Strval
=> TN
(TN
'First .. TL
)));
6326 Analyze_And_Resolve
(N
, Standard_String
);
6327 Set_Is_Static_Expression
(N
, True);
6334 when Attribute_Terminated
=>
6336 Set_Etype
(N
, Standard_Boolean
);
6343 when Attribute_To_Address
=> To_Address
: declare
6348 Check_System_Prefix
;
6350 Generate_Reference
(RTE
(RE_Address
), P
);
6351 Analyze_And_Resolve
(E1
, Any_Integer
);
6352 Set_Etype
(N
, RTE
(RE_Address
));
6353 Set_Is_Static_Expression
(N
, Is_Static_Expression
(E1
));
6355 -- OK static expression case, check range and set appropriate type
6357 if Is_OK_Static_Expression
(E1
) then
6358 Val
:= Expr_Value
(E1
);
6360 if Val
< -(Uint_2
** (System_Address_Size
- 1))
6362 Val
> Uint_2
** System_Address_Size
- 1
6364 Error_Attr
("address value out of range for % attribute", E1
);
6367 -- In most cases the expression is a numeric literal or some other
6368 -- address expression, but if it is a declared constant it may be
6369 -- of a compatible type that must be left on the node.
6371 if Is_Entity_Name
(E1
) then
6374 -- Set type to universal integer if negative
6377 Set_Etype
(E1
, Universal_Integer
);
6379 -- Otherwise set type to Unsigned_64 to accommodate large values
6382 Set_Etype
(E1
, Standard_Unsigned_64
);
6391 when Attribute_To_Any
=>
6393 Check_PolyORB_Attribute
;
6394 Set_Etype
(N
, RTE
(RE_Any
));
6400 when Attribute_Truncation
=>
6401 Check_Floating_Point_Type_1
;
6402 Resolve
(E1
, P_Base_Type
);
6403 Set_Etype
(N
, P_Base_Type
);
6409 when Attribute_Type_Class
=>
6412 Check_Not_Incomplete_Type
;
6413 Set_Etype
(N
, RTE
(RE_Type_Class
));
6419 when Attribute_TypeCode
=>
6421 Check_PolyORB_Attribute
;
6422 Set_Etype
(N
, RTE
(RE_TypeCode
));
6428 when Attribute_Type_Key
=> Type_Key
: declare
6429 Full_Name
: constant String_Id
:=
6430 Fully_Qualified_Name_String
(Entity
(P
));
6433 -- The computed signature for the type
6436 -- To simplify the handling of mutually recursive types, follow a
6437 -- single dereference link in a composite type.
6439 procedure Compute_Type_Key
(T
: Entity_Id
);
6440 -- Create a CRC integer from the declaration of the type. For a
6441 -- composite type, fold in the representation of its components in
6442 -- recursive fashion. We use directly the source representation of
6443 -- the types involved.
6445 ----------------------
6446 -- Compute_Type_Key --
6447 ----------------------
6449 procedure Compute_Type_Key
(T
: Entity_Id
) is
6450 Buffer
: Source_Buffer_Ptr
;
6454 SFI
: Source_File_Index
;
6456 procedure Process_One_Declaration
;
6457 -- Update CRC with the characters of one type declaration, or a
6458 -- representation pragma that applies to the type.
6460 -----------------------------
6461 -- Process_One_Declaration --
6462 -----------------------------
6464 procedure Process_One_Declaration
is
6466 -- Scan type declaration, skipping blanks
6468 for Ptr
in P_Min
.. P_Max
loop
6469 if Buffer
(Ptr
) /= ' ' then
6470 System
.CRC32
.Update
(CRC
, Buffer
(Ptr
));
6473 end Process_One_Declaration
;
6475 -- Start of processing for Compute_Type_Key
6478 if Is_Itype
(T
) then
6482 -- If the type is declared in Standard, there is no source, so
6483 -- just use its name.
6485 if Scope
(T
) = Standard_Standard
then
6487 Name
: constant String := Get_Name_String
(Chars
(T
));
6489 for J
in Name
'Range loop
6490 System
.CRC32
.Update
(CRC
, Name
(J
));
6497 Sloc_Range
(Enclosing_Declaration
(T
), P_Min
, P_Max
);
6498 SFI
:= Get_Source_File_Index
(P_Min
);
6499 pragma Assert
(SFI
= Get_Source_File_Index
(P_Max
));
6500 Buffer
:= Source_Text
(SFI
);
6502 Process_One_Declaration
;
6504 -- Recurse on relevant component types
6506 if Is_Array_Type
(T
) then
6507 Compute_Type_Key
(Component_Type
(T
));
6509 elsif Is_Access_Type
(T
) then
6512 Compute_Type_Key
(Designated_Type
(T
));
6515 elsif Is_Derived_Type
(T
) then
6516 Compute_Type_Key
(Etype
(T
));
6518 elsif Is_Record_Type
(T
) then
6522 Comp
:= First_Component
(T
);
6523 while Present
(Comp
) loop
6524 Compute_Type_Key
(Etype
(Comp
));
6525 Next_Component
(Comp
);
6530 if Is_First_Subtype
(T
) then
6532 -- Fold in representation aspects for the type, which appear in
6533 -- the same source buffer. If the representation aspects are in
6534 -- a different source file, then skip them; they apply to some
6535 -- other type, perhaps one we're derived from.
6537 Rep
:= First_Rep_Item
(T
);
6539 while Present
(Rep
) loop
6540 if Comes_From_Source
(Rep
) then
6541 Sloc_Range
(Rep
, P_Min
, P_Max
);
6543 if SFI
= Get_Source_File_Index
(P_Min
) then
6544 pragma Assert
(SFI
= Get_Source_File_Index
(P_Max
));
6545 Process_One_Declaration
;
6549 Next_Rep_Item
(Rep
);
6552 end Compute_Type_Key
;
6554 -- Start of processing for Type_Key
6563 -- Copy all characters in Full_Name but the trailing NUL
6565 for J
in 1 .. String_Length
(Full_Name
) - 1 loop
6566 Store_String_Char
(Get_String_Char
(Full_Name
, Pos
(J
)));
6569 -- Compute CRC and convert it to string one character at a time, so
6570 -- as not to use Image within the compiler.
6573 Compute_Type_Key
(Entity
(P
));
6575 if not Is_Frozen
(Entity
(P
)) then
6576 Error_Msg_N
("premature usage of Type_Key?", N
);
6580 Store_String_Char
(Character'Val (48 + (CRC
rem 10)));
6584 Rewrite
(N
, Make_String_Literal
(Loc
, End_String
));
6585 Analyze_And_Resolve
(N
, Standard_String
);
6588 -----------------------
6589 -- Unbiased_Rounding --
6590 -----------------------
6592 when Attribute_Unbiased_Rounding
=>
6593 Check_Floating_Point_Type_1
;
6594 Set_Etype
(N
, P_Base_Type
);
6595 Resolve
(E1
, P_Base_Type
);
6597 ----------------------
6598 -- Unchecked_Access --
6599 ----------------------
6601 when Attribute_Unchecked_Access
=>
6602 if Comes_From_Source
(N
) then
6603 Check_Restriction
(No_Unchecked_Access
, N
);
6606 Analyze_Access_Attribute
;
6607 Check_Not_Incomplete_Type
;
6609 -------------------------
6610 -- Unconstrained_Array --
6611 -------------------------
6613 when Attribute_Unconstrained_Array
=>
6616 Check_Not_Incomplete_Type
;
6617 Set_Etype
(N
, Standard_Boolean
);
6618 Set_Is_Static_Expression
(N
, True);
6620 ------------------------------
6621 -- Universal_Literal_String --
6622 ------------------------------
6624 -- This is a GNAT specific attribute whose prefix must be a named
6625 -- number where the expression is either a single numeric literal,
6626 -- or a numeric literal immediately preceded by a minus sign. The
6627 -- result is equivalent to a string literal containing the text of
6628 -- the literal as it appeared in the source program with a possible
6629 -- leading minus sign.
6631 when Attribute_Universal_Literal_String
=>
6634 if not Is_Entity_Name
(P
)
6635 or else Ekind
(Entity
(P
)) not in Named_Kind
6637 Error_Attr_P
("prefix for % attribute must be named number");
6644 Src
: Source_Buffer_Ptr
;
6647 Expr
:= Original_Node
(Expression
(Parent
(Entity
(P
))));
6649 if Nkind
(Expr
) = N_Op_Minus
then
6651 Expr
:= Original_Node
(Right_Opnd
(Expr
));
6656 if Nkind
(Expr
) not in N_Integer_Literal | N_Real_Literal
then
6658 ("named number for % attribute must be simple literal", N
);
6661 -- Build string literal corresponding to source literal text
6666 Store_String_Char
(Get_Char_Code
('-'));
6670 Src
:= Source_Text
(Get_Source_File_Index
(S
));
6672 while Src
(S
) /= ';' and then Src
(S
) /= ' ' loop
6673 Store_String_Char
(Get_Char_Code
(Src
(S
)));
6677 -- Now we rewrite the attribute with the string literal
6680 Make_String_Literal
(Loc
, End_String
));
6682 Set_Is_Static_Expression
(N
, True);
6686 -------------------------
6687 -- Unrestricted_Access --
6688 -------------------------
6690 -- This is a GNAT specific attribute which is like Access except that
6691 -- all scope checks and checks for aliased views are omitted. It is
6692 -- documented as being equivalent to the use of the Address attribute
6693 -- followed by an unchecked conversion to the target access type.
6695 when Attribute_Unrestricted_Access
=>
6697 -- If from source, deal with relevant restrictions
6699 if Comes_From_Source
(N
) then
6700 Check_Restriction
(No_Unchecked_Access
, N
);
6702 if Nkind
(P
) in N_Has_Entity
6703 and then Present
(Entity
(P
))
6704 and then Is_Object
(Entity
(P
))
6706 Check_Restriction
(No_Implicit_Aliasing
, N
);
6710 if Is_Entity_Name
(P
) then
6711 Set_Address_Taken
(Entity
(P
));
6714 -- It might seem reasonable to call Address_Checks here to apply the
6715 -- same set of semantic checks that we enforce for 'Address (after
6716 -- all we document Unrestricted_Access as being equivalent to the
6717 -- use of Address followed by an Unchecked_Conversion). However, if
6718 -- we do enable these checks, we get multiple failures in both the
6719 -- compiler run-time and in our regression test suite, so we leave
6720 -- out these checks for now. To be investigated further some time???
6724 -- Now complete analysis using common access processing
6726 Analyze_Access_Attribute
;
6732 when Attribute_Update
=> Update
: declare
6733 Common_Typ
: Entity_Id
;
6734 -- The common type of a multiple component update for a record
6736 Comps
: Elist_Id
:= No_Elist
;
6737 -- A list used in the resolution of a record update. It contains the
6738 -- entities of all record components processed so far.
6740 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
);
6741 -- Analyze and resolve array_component_association Assoc against the
6742 -- index of array type P_Type.
6744 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
);
6745 -- Analyze and resolve record_component_association Comp against
6746 -- record type P_Type.
6748 ------------------------------------
6749 -- Analyze_Array_Component_Update --
6750 ------------------------------------
6752 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
) is
6756 Index_Typ
: Entity_Id
;
6760 -- The current association contains a sequence of indexes denoting
6761 -- an element of a multidimensional array:
6763 -- (Index_1, ..., Index_N)
6765 -- Examine each individual index and resolve it against the proper
6766 -- index type of the array.
6768 if Nkind
(First
(Choices
(Assoc
))) = N_Aggregate
then
6769 Expr
:= First
(Choices
(Assoc
));
6770 while Present
(Expr
) loop
6772 -- The use of others is illegal (SPARK RM 4.4.1(12))
6774 if Nkind
(Expr
) = N_Others_Choice
then
6776 ("others choice not allowed in attribute %", Expr
);
6778 -- Otherwise analyze and resolve all indexes
6781 Index
:= First
(Expressions
(Expr
));
6782 Index_Typ
:= First_Index
(P_Type
);
6783 while Present
(Index
) and then Present
(Index_Typ
) loop
6784 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
6786 Next_Index
(Index_Typ
);
6789 -- Detect a case where the association either lacks an
6790 -- index or contains an extra index.
6792 if Present
(Index
) or else Present
(Index_Typ
) then
6794 ("dimension mismatch in index list", Assoc
);
6801 -- The current association denotes either a single component or a
6802 -- range of components of a one dimensional array:
6806 -- Resolve the index or its high and low bounds (if range) against
6807 -- the proper index type of the array.
6810 Index
:= First
(Choices
(Assoc
));
6811 Index_Typ
:= First_Index
(P_Type
);
6813 if Present
(Next_Index
(Index_Typ
)) then
6814 Error_Msg_N
("too few subscripts in array reference", Assoc
);
6817 while Present
(Index
) loop
6819 -- The use of others is illegal (SPARK RM 4.4.1(12))
6821 if Nkind
(Index
) = N_Others_Choice
then
6823 ("others choice not allowed in attribute %", Index
);
6825 -- The index denotes a range of elements
6827 elsif Nkind
(Index
) = N_Range
then
6828 Low
:= Low_Bound
(Index
);
6829 High
:= High_Bound
(Index
);
6831 Analyze_And_Resolve
(Low
, Etype
(Index_Typ
));
6832 Analyze_And_Resolve
(High
, Etype
(Index_Typ
));
6834 -- Otherwise the index denotes a single element
6837 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
6843 end Analyze_Array_Component_Update
;
6845 -------------------------------------
6846 -- Analyze_Record_Component_Update --
6847 -------------------------------------
6849 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
) is
6850 Comp_Name
: constant Name_Id
:= Chars
(Comp
);
6851 Base_Typ
: Entity_Id
;
6852 Comp_Or_Discr
: Entity_Id
;
6855 -- Find the discriminant or component whose name corresponds to
6856 -- Comp. A simple character comparison is sufficient because all
6857 -- visible names within a record type are unique.
6859 Comp_Or_Discr
:= First_Entity
(P_Type
);
6860 while Present
(Comp_Or_Discr
) loop
6861 if Chars
(Comp_Or_Discr
) = Comp_Name
then
6863 -- Decorate the component reference by setting its entity
6864 -- and type for resolution purposes.
6866 Set_Entity
(Comp
, Comp_Or_Discr
);
6867 Set_Etype
(Comp
, Etype
(Comp_Or_Discr
));
6871 Next_Entity
(Comp_Or_Discr
);
6874 -- Diagnose an illegal reference
6876 if Present
(Comp_Or_Discr
) then
6877 if Ekind
(Comp_Or_Discr
) = E_Discriminant
then
6879 ("attribute % may not modify record discriminants", Comp
);
6881 else pragma Assert
(Ekind
(Comp_Or_Discr
) = E_Component
);
6882 if Contains
(Comps
, Comp_Or_Discr
) then
6883 Error_Msg_N
("component & already updated", Comp
);
6885 -- Mark this component as processed
6888 Append_New_Elmt
(Comp_Or_Discr
, Comps
);
6892 -- The update aggregate mentions an entity that does not belong to
6896 Error_Msg_N
("& is not a component of aggregate subtype", Comp
);
6899 -- Verify the consistency of types when the current component is
6900 -- part of a multiple component update.
6902 -- Comp_1 | ... | Comp_N => <value>
6904 if Present
(Etype
(Comp
)) then
6905 Base_Typ
:= Base_Type
(Etype
(Comp
));
6907 -- Save the type of the first component reference as the
6908 -- remaning references (if any) must resolve to this type.
6910 if No
(Common_Typ
) then
6911 Common_Typ
:= Base_Typ
;
6913 elsif Base_Typ
/= Common_Typ
then
6915 ("components in choice list must have same type", Comp
);
6918 end Analyze_Record_Component_Update
;
6925 -- Start of processing for Update
6928 if Warn_On_Obsolescent_Feature
then
6929 Error_Msg_N
("?j?attribute Update is an obsolescent feature", N
);
6930 Error_Msg_N
("\?j?use a delta aggregate instead", N
);
6935 if not Is_Object_Reference
(P
) then
6936 Error_Attr_P
("prefix of attribute % must denote an object");
6938 elsif not Is_Array_Type
(P_Type
)
6939 and then not Is_Record_Type
(P_Type
)
6941 Error_Attr_P
("prefix of attribute % must be a record or array");
6943 elsif Is_Limited_View
(P_Type
) then
6944 Error_Attr
("prefix of attribute % cannot be limited", N
);
6946 elsif Nkind
(E1
) /= N_Aggregate
then
6947 Error_Attr
("attribute % requires component association list", N
);
6949 elsif Present
(Expressions
(E1
)) then
6950 Error_Attr
("attribute % requires named component associations",
6951 First
(Expressions
(E1
)));
6955 -- Inspect the update aggregate, looking at all the associations and
6956 -- choices. Perform the following checks:
6958 -- 1) Legality of "others" in all cases
6959 -- 2) Legality of <>
6960 -- 3) Component legality for arrays
6961 -- 4) Component legality for records
6963 -- The remaining checks are performed on the expanded attribute
6965 Assoc
:= First
(Component_Associations
(E1
));
6966 while Present
(Assoc
) loop
6968 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6970 if Box_Present
(Assoc
) then
6972 ("default initialization not allowed in attribute %", Assoc
);
6974 -- Otherwise process the association
6977 Analyze
(Expression
(Assoc
));
6979 if Is_Array_Type
(P_Type
) then
6980 Analyze_Array_Component_Update
(Assoc
);
6982 elsif Is_Record_Type
(P_Type
) then
6984 -- Reset the common type used in a multiple component update
6985 -- as we are processing the contents of a new association.
6987 Common_Typ
:= Empty
;
6989 Comp
:= First
(Choices
(Assoc
));
6990 while Present
(Comp
) loop
6991 if Nkind
(Comp
) = N_Identifier
then
6992 Analyze_Record_Component_Update
(Comp
);
6994 -- The use of others is illegal (SPARK RM 4.4.1(5))
6996 elsif Nkind
(Comp
) = N_Others_Choice
then
6998 ("others choice not allowed in attribute %", Comp
);
7000 -- The name of a record component cannot appear in any
7005 ("name should be identifier or OTHERS", Comp
);
7016 -- The type of attribute 'Update is that of the prefix
7018 Set_Etype
(N
, P_Type
);
7020 Sem_Warn
.Warn_On_Suspicious_Update
(N
);
7027 when Attribute_Val
=>
7029 Check_Discrete_Type
;
7031 -- Note, we need a range check in general, but we wait for the
7032 -- Resolve call to do this, since we want to let Eval_Attribute
7033 -- have a chance to find an static illegality first.
7035 Resolve
(E1
, Any_Integer
);
7036 Set_Etype
(N
, P_Base_Type
);
7042 when Attribute_Valid
=> Valid
: declare
7043 Pred_Func
: constant Entity_Id
:= Predicate_Function
(P_Type
);
7048 -- Ignore check for object if we have a 'Valid reference generated
7049 -- by the expanded code, since in some cases valid checks can occur
7050 -- on items that are names, but are not objects (e.g. attributes).
7052 if Comes_From_Source
(N
) then
7053 Check_Object_Reference
(P
);
7055 if not Is_Scalar_Type
(P_Type
) then
7056 Error_Attr_P
("object for % attribute must be of scalar type");
7059 -- If the attribute appears within the subtype's own predicate
7060 -- function, then issue a warning that this will cause infinite
7063 if Present
(Pred_Func
) and then Current_Scope
= Pred_Func
then
7064 Error_Msg_N
("attribute Valid requires a predicate check??", N
);
7065 Error_Msg_N
("\and will result in infinite recursion??", N
);
7069 Set_Etype
(N
, Standard_Boolean
);
7076 when Attribute_Valid_Scalars
=> Valid_Scalars
: declare
7080 if Comes_From_Source
(N
) then
7081 Check_Object_Reference
(P
);
7083 -- Do not emit any diagnostics related to private types to avoid
7084 -- disclosing the structure of the type.
7086 if Is_Private_Type
(P_Type
) then
7088 -- Attribute 'Valid_Scalars is not supported on private tagged
7089 -- types due to a code generation issue. Is_Visible_Component
7090 -- does not allow for a component of a private tagged type to
7091 -- be successfully retrieved.
7092 -- ??? This attribute should simply ignore type privacy
7093 -- (see Validated_View). It should examine components of the
7094 -- tagged type extensions (if any) and recursively examine
7095 -- 'Valid_Scalars of the parent's type (if any).
7097 -- Do not use Error_Attr_P because this bypasses any subsequent
7098 -- processing and leaves the attribute with type Any_Type. This
7099 -- in turn prevents the proper expansion of the attribute into
7102 if Is_Tagged_Type
(P_Type
) then
7103 Error_Msg_Name_1
:= Aname
;
7104 Error_Msg_N
("??effects of attribute % are ignored", N
);
7107 -- Otherwise the type is not private
7110 if not Scalar_Part_Present
(P_Type
) then
7111 Error_Msg_Name_1
:= Aname
;
7113 ("??attribute % always True, no scalars to check", P
);
7114 Set_Boolean_Result
(N
, True);
7117 -- Attribute 'Valid_Scalars is illegal on unchecked union types
7118 -- because it is not always guaranteed that the components are
7119 -- retrievable based on whether the discriminants are inferable
7121 if Has_Unchecked_Union
(P_Type
) then
7123 ("attribute % not allowed for Unchecked_Union type");
7128 Set_Etype
(N
, Standard_Boolean
);
7135 when Attribute_Value
=>
7139 -- Case of enumeration type
7141 -- When an enumeration type appears in an attribute reference, all
7142 -- literals of the type are marked as referenced. This must only be
7143 -- done if the attribute reference appears in the current source.
7144 -- Otherwise the information on references may differ between a
7145 -- normal compilation and one that performs inlining.
7147 if Is_Enumeration_Type
(P_Type
)
7148 and then In_Extended_Main_Code_Unit
(N
)
7150 Check_Restriction
(No_Enumeration_Maps
, N
);
7152 -- Mark all enumeration literals as referenced, since the use of
7153 -- the Value attribute can implicitly reference any of the
7154 -- literals of the enumeration base type.
7157 Ent
: Entity_Id
:= First_Literal
(P_Base_Type
);
7159 while Present
(Ent
) loop
7160 Set_Referenced
(Ent
);
7166 -- Set Etype before resolving expression because expansion of
7167 -- expression may require enclosing type. Note that the type
7168 -- returned by 'Value is the base type of the prefix type.
7170 Set_Etype
(N
, P_Base_Type
);
7171 Validate_Non_Static_Attribute_Function_Call
;
7173 -- Check restriction No_Fixed_IO
7175 if Restriction_Check_Required
(No_Fixed_IO
)
7176 and then Is_Fixed_Point_Type
(P_Type
)
7178 Check_Restriction
(No_Fixed_IO
, P
);
7185 when Attribute_Value_Size
=>
7188 Check_Not_Incomplete_Type
;
7189 Set_Etype
(N
, Universal_Integer
);
7195 when Attribute_Version
=>
7198 Set_Etype
(N
, RTE
(RE_Version_String
));
7204 when Attribute_Wchar_T_Size
=>
7205 Standard_Attribute
(Interfaces_Wchar_T_Size
);
7211 when Attribute_Wide_Image
=>
7212 Analyze_Image_Attribute
(Standard_Wide_String
);
7214 ---------------------
7215 -- Wide_Wide_Image --
7216 ---------------------
7218 when Attribute_Wide_Wide_Image
=>
7219 Analyze_Image_Attribute
(Standard_Wide_Wide_String
);
7225 when Attribute_Wide_Value
=>
7229 -- Set Etype before resolving expression because expansion
7230 -- of expression may require enclosing type.
7232 Set_Etype
(N
, P_Type
);
7233 Validate_Non_Static_Attribute_Function_Call
;
7235 -- Check restriction No_Fixed_IO
7237 if Restriction_Check_Required
(No_Fixed_IO
)
7238 and then Is_Fixed_Point_Type
(P_Type
)
7240 Check_Restriction
(No_Fixed_IO
, P
);
7243 ---------------------
7244 -- Wide_Wide_Value --
7245 ---------------------
7247 when Attribute_Wide_Wide_Value
=>
7251 -- Set Etype before resolving expression because expansion
7252 -- of expression may require enclosing type.
7254 Set_Etype
(N
, P_Type
);
7255 Validate_Non_Static_Attribute_Function_Call
;
7257 -- Check restriction No_Fixed_IO
7259 if Restriction_Check_Required
(No_Fixed_IO
)
7260 and then Is_Fixed_Point_Type
(P_Type
)
7262 Check_Restriction
(No_Fixed_IO
, P
);
7265 ---------------------
7266 -- Wide_Wide_Width --
7267 ---------------------
7269 when Attribute_Wide_Wide_Width
=>
7272 Set_Etype
(N
, Universal_Integer
);
7278 when Attribute_Wide_Width
=>
7281 Set_Etype
(N
, Universal_Integer
);
7287 when Attribute_Width
=>
7290 Set_Etype
(N
, Universal_Integer
);
7296 when Attribute_Word_Size
=>
7297 Standard_Attribute
(System_Word_Size
);
7303 when Attribute_Write
=>
7305 Check_Stream_Attribute
(TSS_Stream_Write
);
7306 Set_Etype
(N
, Standard_Void_Type
);
7307 Resolve
(N
, Standard_Void_Type
);
7311 -- In SPARK certain attributes (see below) depend on Tasking_State.
7312 -- Ensure that the entity is available for gnat2why by loading it.
7313 -- See SPARK RM 9(18) for the relevant rule.
7315 if GNATprove_Mode
then
7317 when Attribute_Callable
7320 | Attribute_Terminated
7322 SPARK_Implicit_Load
(RE_Tasking_State
);
7329 -- All errors raise Bad_Attribute, so that we get out before any further
7330 -- damage occurs when an error is detected (for example, if we check for
7331 -- one attribute expression, and the check succeeds, we want to be able
7332 -- to proceed securely assuming that an expression is in fact present.
7334 -- Note: we set the attribute analyzed in this case to prevent any
7335 -- attempt at reanalysis which could generate spurious error msgs.
7338 when Bad_Attribute
=>
7340 Set_Etype
(N
, Any_Type
);
7342 end Analyze_Attribute
;
7344 --------------------
7345 -- Eval_Attribute --
7346 --------------------
7348 procedure Eval_Attribute
(N
: Node_Id
) is
7349 Loc
: constant Source_Ptr
:= Sloc
(N
);
7351 C_Type
: constant Entity_Id
:= Etype
(N
);
7352 -- The type imposed by the context
7355 -- Attribute_Name (N) after verification of validity of N
7358 -- Get_Attribute_Id (Aname) after Aname is set
7361 -- Prefix (N) after verification of validity of N
7364 -- First expression, or Empty if none
7367 -- Second expression, or Empty if none
7369 P_Entity
: Entity_Id
;
7370 -- Entity denoted by prefix
7373 -- The type of the prefix
7375 P_Base_Type
: Entity_Id
;
7376 -- The base type of the prefix type
7378 P_Root_Type
: Entity_Id
;
7379 -- The root type of the prefix type
7381 Static
: Boolean := False;
7382 -- True if the result is Static. This is set by the general processing
7383 -- to true if the prefix is static, and all expressions are static. It
7384 -- can be reset as processing continues for particular attributes. This
7385 -- flag can still be True if the reference raises a constraint error.
7386 -- Is_Static_Expression (N) is set to follow this value as it is set
7387 -- and we could always reference this, but it is convenient to have a
7388 -- simple short name to use, since it is frequently referenced.
7390 Lo_Bound
, Hi_Bound
: Node_Id
;
7391 -- Expressions for low and high bounds of type or array index referenced
7392 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7395 -- Constraint error node used if we have an attribute reference has
7396 -- an argument that raises a constraint error. In this case we replace
7397 -- the attribute with a raise constraint_error node. This is important
7398 -- processing, since otherwise gigi might see an attribute which it is
7399 -- unprepared to deal with.
7401 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
);
7402 -- If Bound is a reference to a discriminant of a task or protected type
7403 -- occurring within the object's body, rewrite attribute reference into
7404 -- a reference to the corresponding discriminal. Use for the expansion
7405 -- of checks against bounds of entry family index subtypes.
7407 procedure Check_Expressions
;
7408 -- In case where the attribute is not foldable, the expressions, if
7409 -- any, of the attribute, are in a non-static context. This procedure
7410 -- performs the required additional checks.
7412 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean;
7413 -- Determines if the given type has compile time known bounds. Note
7414 -- that we enter the case statement even in cases where the prefix
7415 -- type does NOT have known bounds, so it is important to guard any
7416 -- attempt to evaluate both bounds with a call to this function.
7418 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
);
7419 -- This procedure is called when the attribute N has a non-static
7420 -- but compile time known value given by Val. It includes the
7421 -- necessary checks for out of range values.
7423 function Fore_Value
return Nat
;
7424 -- Computes the Fore value for the current attribute prefix, which is
7425 -- known to be a static fixed-point type. Used by Fore and Width.
7427 function Mantissa
return Uint
;
7428 -- Returns the Mantissa value for the prefix type
7430 procedure Set_Bounds
;
7431 -- Used for First, Last and Length attributes applied to an array or
7432 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7433 -- and high bound expressions for the index referenced by the attribute
7434 -- designator (i.e. the first index if no expression is present, and the
7435 -- N'th index if the value N is present as an expression). Also used for
7436 -- First and Last of scalar types and for First_Valid and Last_Valid.
7437 -- Static is reset to False if the type or index type is not statically
7440 -----------------------------------
7441 -- Check_Concurrent_Discriminant --
7442 -----------------------------------
7444 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
) is
7446 -- The concurrent (task or protected) type
7449 if Nkind
(Bound
) = N_Identifier
7450 and then Ekind
(Entity
(Bound
)) = E_Discriminant
7451 and then Is_Concurrent_Record_Type
(Scope
(Entity
(Bound
)))
7453 Tsk
:= Corresponding_Concurrent_Type
(Scope
(Entity
(Bound
)));
7455 if In_Open_Scopes
(Tsk
) and then Has_Completion
(Tsk
) then
7457 -- Find discriminant of original concurrent type, and use
7458 -- its current discriminal, which is the renaming within
7459 -- the task/protected body.
7463 (Find_Body_Discriminal
(Entity
(Bound
)), Loc
));
7466 end Check_Concurrent_Discriminant
;
7468 -----------------------
7469 -- Check_Expressions --
7470 -----------------------
7472 procedure Check_Expressions
is
7476 while Present
(E
) loop
7477 Check_Non_Static_Context
(E
);
7480 end Check_Expressions
;
7482 ----------------------------------
7483 -- Compile_Time_Known_Attribute --
7484 ----------------------------------
7486 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
) is
7487 T
: constant Entity_Id
:= Etype
(N
);
7490 Fold_Uint
(N
, Val
, False);
7492 -- Check that result is in bounds of the type if it is static
7494 if Is_In_Range
(N
, T
, Assume_Valid
=> False) then
7497 elsif Is_Out_Of_Range
(N
, T
) then
7498 Apply_Compile_Time_Constraint_Error
7499 (N
, "value not in range of}??", CE_Range_Check_Failed
);
7501 elsif not Range_Checks_Suppressed
(T
) then
7502 Enable_Range_Check
(N
);
7505 Set_Do_Range_Check
(N
, False);
7507 end Compile_Time_Known_Attribute
;
7509 -------------------------------
7510 -- Compile_Time_Known_Bounds --
7511 -------------------------------
7513 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean is
7516 Compile_Time_Known_Value
(Type_Low_Bound
(Typ
))
7518 Compile_Time_Known_Value
(Type_High_Bound
(Typ
));
7519 end Compile_Time_Known_Bounds
;
7525 -- Note that the Fore calculation is based on the actual values
7526 -- of the bounds, and does not take into account possible rounding.
7528 function Fore_Value
return Nat
is
7529 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
7530 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
7531 Small
: constant Ureal
:= Small_Value
(P_Type
);
7532 Lo_Real
: constant Ureal
:= Lo
* Small
;
7533 Hi_Real
: constant Ureal
:= Hi
* Small
;
7538 -- Bounds are given in terms of small units, so first compute
7539 -- proper values as reals.
7541 T
:= UR_Max
(abs Lo_Real
, abs Hi_Real
);
7544 -- Loop to compute proper value if more than one digit required
7546 while T
>= Ureal_10
loop
7558 -- Table of mantissa values accessed by function Computed using
7561 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7563 -- where D is T'Digits (RM83 3.5.7)
7565 Mantissa_Value
: constant array (Nat
range 1 .. 40) of Nat
:= (
7607 function Mantissa
return Uint
is
7610 UI_From_Int
(Mantissa_Value
(UI_To_Int
(Digits_Value
(P_Type
))));
7617 procedure Set_Bounds
is
7623 -- For a string literal subtype, we have to construct the bounds.
7624 -- Valid Ada code never applies attributes to string literals, but
7625 -- it is convenient to allow the expander to generate attribute
7626 -- references of this type (e.g. First and Last applied to a string
7629 -- Note that the whole point of the E_String_Literal_Subtype is to
7630 -- avoid this construction of bounds, but the cases in which we
7631 -- have to materialize them are rare enough that we don't worry.
7633 -- The low bound is simply the low bound of the base type. The
7634 -- high bound is computed from the length of the string and this
7637 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
7638 Ityp
:= Etype
(First_Index
(Base_Type
(P_Type
)));
7639 Lo_Bound
:= Type_Low_Bound
(Ityp
);
7642 Make_Integer_Literal
(Sloc
(P
),
7644 Expr_Value
(Lo_Bound
) + String_Literal_Length
(P_Type
) - 1);
7646 Set_Parent
(Hi_Bound
, P
);
7647 Analyze_And_Resolve
(Hi_Bound
, Etype
(Lo_Bound
));
7650 -- For non-array case, just get bounds of scalar type
7652 elsif Is_Scalar_Type
(P_Type
) then
7655 -- For a fixed-point type, we must freeze to get the attributes
7656 -- of the fixed-point type set now so we can reference them.
7658 if Is_Fixed_Point_Type
(P_Type
)
7659 and then not Is_Frozen
(Base_Type
(P_Type
))
7660 and then Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
7661 and then Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
7663 Freeze_Fixed_Point_Type
(Base_Type
(P_Type
));
7666 -- For array case, get type of proper index
7672 Ndim
:= UI_To_Int
(Expr_Value
(E1
));
7675 Indx
:= First_Index
(P_Type
);
7676 for J
in 1 .. Ndim
- 1 loop
7680 -- If no index type, get out (some other error occurred, and
7681 -- we don't have enough information to complete the job).
7689 Ityp
:= Etype
(Indx
);
7692 -- A discrete range in an index constraint is allowed to be a
7693 -- subtype indication. This is syntactically a pain, but should
7694 -- not propagate to the entity for the corresponding index subtype.
7695 -- After checking that the subtype indication is legal, the range
7696 -- of the subtype indication should be transfered to the entity.
7697 -- The attributes for the bounds should remain the simple retrievals
7698 -- that they are now.
7700 Lo_Bound
:= Type_Low_Bound
(Ityp
);
7701 Hi_Bound
:= Type_High_Bound
(Ityp
);
7703 -- If subtype is non-static, result is definitely non-static
7705 if not Is_Static_Subtype
(Ityp
) then
7707 Set_Is_Static_Expression
(N
, False);
7709 -- Subtype is static, does it raise CE?
7711 elsif not Is_OK_Static_Subtype
(Ityp
) then
7712 Set_Raises_Constraint_Error
(N
);
7716 -- Start of processing for Eval_Attribute
7719 -- Return immediately if e.g. N has been rewritten or is malformed due
7720 -- to previous errors.
7722 if Nkind
(N
) /= N_Attribute_Reference
then
7726 Aname
:= Attribute_Name
(N
);
7727 Id
:= Get_Attribute_Id
(Aname
);
7730 -- The To_Address attribute can be static, but it cannot be evaluated at
7731 -- compile time, so just return.
7733 if Id
= Attribute_To_Address
then
7737 -- Initialize result as non-static, will be reset if appropriate
7739 Set_Is_Static_Expression
(N
, False);
7741 -- Acquire first two expressions (at the moment, no attributes take more
7742 -- than two expressions in any case).
7744 if Present
(Expressions
(N
)) then
7745 E1
:= First
(Expressions
(N
));
7752 -- Special processing for Enabled attribute. This attribute has a very
7753 -- special prefix, and the easiest way to avoid lots of special checks
7754 -- to protect this special prefix from causing trouble is to deal with
7755 -- this attribute immediately and be done with it.
7757 if Id
= Attribute_Enabled
then
7759 -- We skip evaluation if the expander is not active. This is not just
7760 -- an optimization. It is of key importance that we not rewrite the
7761 -- attribute in a generic template, since we want to pick up the
7762 -- setting of the check in the instance.
7764 if not Inside_A_Generic
then
7766 C
: constant Check_Id
:= Get_Check_Id
(Chars
(P
));
7771 if C
in Predefined_Check_Id
then
7772 R
:= Scope_Suppress
.Suppress
(C
);
7774 R
:= Is_Check_Suppressed
(Empty
, C
);
7778 R
:= Is_Check_Suppressed
(Entity
(E1
), C
);
7781 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(not R
), Loc
));
7788 -- Attribute 'Img applied to a static enumeration value is static, and
7789 -- we will do the folding right here (things get confused if we let this
7790 -- case go through the normal circuitry).
7792 if Attribute_Name
(N
) = Name_Img
7793 and then Is_Entity_Name
(P
)
7794 and then Is_Enumeration_Type
(Etype
(Entity
(P
)))
7795 and then Is_OK_Static_Expression
(P
)
7798 Lit
: constant Entity_Id
:= Expr_Value_E
(P
);
7803 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
7804 Set_Casing
(All_Upper_Case
);
7805 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
7808 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
7809 Analyze_And_Resolve
(N
, Standard_String
);
7810 Set_Is_Static_Expression
(N
, True);
7816 -- Special processing for cases where the prefix is an object or value,
7817 -- including string literals (attributes of string literals can only
7818 -- appear in generated code) and current instance prefixes in type or
7821 if Is_Object_Reference
(P
)
7822 or else Is_Current_Instance_Reference_In_Type_Aspect
(P
)
7823 or else Nkind
(P
) = N_String_Literal
7824 or else (Is_Entity_Name
(P
)
7825 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
7827 -- For Alignment, give alignment of object if available, otherwise we
7828 -- cannot fold Alignment.
7830 if Id
= Attribute_Alignment
then
7831 if Is_Entity_Name
(P
) and then Known_Alignment
(Entity
(P
)) then
7832 Compile_Time_Known_Attribute
(N
, Alignment
(Entity
(P
)));
7839 -- For Component_Size, the prefix is an array object, and we apply
7840 -- the attribute to the type of the object. This is allowed for both
7841 -- unconstrained and constrained arrays, since the bounds have no
7842 -- influence on the value of this attribute.
7844 elsif Id
= Attribute_Component_Size
then
7845 P_Entity
:= Etype
(P
);
7847 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7848 -- the optional argument.
7850 elsif Id
= Attribute_Enum_Rep
then
7851 if Is_Entity_Name
(P
) then
7854 Enum_Expr
: Node_Id
;
7855 -- The enumeration-type expression of interest
7860 if Ekind
(Entity
(P
)) in E_Constant | E_Enumeration_Literal
7864 -- Enum_Type'Enum_Rep (E1) case
7866 elsif Is_Enumeration_Type
(Entity
(P
)) then
7869 -- Otherwise the attribute must be expanded into a
7870 -- conversion and evaluated at run time.
7877 -- We can fold if the expression is an enumeration
7878 -- literal, or if it denotes a constant whose value
7879 -- is known at compile time.
7881 if Nkind
(Enum_Expr
) in N_Has_Entity
7882 and then (Ekind
(Entity
(Enum_Expr
)) =
7883 E_Enumeration_Literal
7885 (Ekind
(Entity
(Enum_Expr
)) = E_Constant
7886 and then Nkind
(Parent
(Entity
(Enum_Expr
))) =
7887 N_Object_Declaration
7889 (Expression
(Parent
(Entity
(P
))))
7890 and then Compile_Time_Known_Value
7891 (Expression
(Parent
(Entity
(P
))))))
7893 P_Entity
:= Etype
(P
);
7900 -- Otherwise the attribute is illegal, do not attempt to perform
7901 -- any kind of folding.
7907 -- For Bit_Position, give Component_Bit_Offset of object if available
7908 -- otherwise we cannot fold Bit_Position. Note that the attribute can
7909 -- be applied to a naked record component in generated code, in which
7910 -- case the prefix is an identifier that references the component or
7911 -- discriminant entity.
7913 elsif Id
= Attribute_Bit_Position
then
7918 if Is_Entity_Name
(P
) then
7921 CE
:= Entity
(Selector_Name
(P
));
7924 if Known_Static_Component_Bit_Offset
(CE
) then
7925 Compile_Time_Known_Attribute
7926 (N
, Component_Bit_Offset
(CE
));
7934 -- For Position, in Ada 2005 (or later) if we have the non-default
7935 -- bit order, we return the original value as given in the component
7936 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7937 -- default bit order) return the value if it is known statically.
7939 elsif Id
= Attribute_Position
then
7941 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
7944 if Present
(Component_Clause
(CE
))
7945 and then Ada_Version
>= Ada_2005
7946 and then Reverse_Bit_Order
(Scope
(CE
))
7948 Compile_Time_Known_Attribute
7949 (N
, Expr_Value
(Position
(Component_Clause
(CE
))));
7951 elsif Known_Static_Component_Bit_Offset
(CE
) then
7952 Compile_Time_Known_Attribute
7953 (N
, Component_Bit_Offset
(CE
) / System_Storage_Unit
);
7962 -- For First_Bit, in Ada 2005 (or later) if we have the non-default
7963 -- bit order, we return the original value as given in the component
7964 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7965 -- default bit order) return the value if it is known statically.
7967 elsif Id
= Attribute_First_Bit
then
7969 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
7972 if Present
(Component_Clause
(CE
))
7973 and then Ada_Version
>= Ada_2005
7974 and then Reverse_Bit_Order
(Scope
(CE
))
7976 Compile_Time_Known_Attribute
7977 (N
, Expr_Value
(First_Bit
(Component_Clause
(CE
))));
7979 elsif Known_Static_Component_Bit_Offset
(CE
) then
7980 Compile_Time_Known_Attribute
7981 (N
, Component_Bit_Offset
(CE
) mod System_Storage_Unit
);
7990 -- For Last_Bit, in Ada 2005 (or later) if we have the non-default
7991 -- bit order, we return the original value as given in the component
7992 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7993 -- default bit order) return the value if it is known statically.
7995 elsif Id
= Attribute_Last_Bit
then
7997 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
8000 if Present
(Component_Clause
(CE
))
8001 and then Ada_Version
>= Ada_2005
8002 and then Reverse_Bit_Order
(Scope
(CE
))
8004 Compile_Time_Known_Attribute
8005 (N
, Expr_Value
(Last_Bit
(Component_Clause
(CE
))));
8007 elsif Known_Static_Component_Bit_Offset
(CE
)
8008 and then Known_Static_Esize
(CE
)
8010 Compile_Time_Known_Attribute
8011 (N
, (Component_Bit_Offset
(CE
) mod System_Storage_Unit
)
8020 -- For First, Last and Length, the prefix is an array object, and we
8021 -- apply the attribute to its type, but we need a constrained type
8022 -- for this, so we use the actual subtype if available.
8024 elsif Id
= Attribute_First
8025 or else Id
= Attribute_Last
8026 or else Id
= Attribute_Length
8029 AS
: constant Entity_Id
:= Get_Actual_Subtype_If_Available
(P
);
8032 if Present
(AS
) and then Is_Constrained
(AS
) then
8035 -- If we have an unconstrained type we cannot fold
8043 -- For Size, give size of object if available, otherwise we
8044 -- cannot fold Size.
8046 elsif Id
= Attribute_Size
then
8047 if Is_Entity_Name
(P
)
8048 and then Known_Static_Esize
(Entity
(P
))
8050 Compile_Time_Known_Attribute
(N
, Esize
(Entity
(P
)));
8057 -- For Lock_Free, we apply the attribute to the type of the object.
8058 -- This is allowed since we have already verified that the type is a
8061 elsif Id
= Attribute_Lock_Free
then
8062 P_Entity
:= Etype
(P
);
8064 -- No other attributes for objects are folded
8071 -- Cases where P is not an object. Cannot do anything if P is not the
8072 -- name of an entity.
8074 elsif not Is_Entity_Name
(P
) then
8078 -- Otherwise get prefix entity
8081 P_Entity
:= Entity
(P
);
8084 -- If we are asked to evaluate an attribute where the prefix is a
8085 -- non-frozen generic actual type whose RM_Size is still set to zero,
8086 -- then abandon the effort.
8088 if Is_Type
(P_Entity
)
8089 and then (not Is_Frozen
(P_Entity
)
8090 and then Is_Generic_Actual_Type
(P_Entity
)
8091 and then RM_Size
(P_Entity
) = 0)
8093 -- However, the attribute Unconstrained_Array must be evaluated,
8094 -- since it is documented to be a static attribute (and can for
8095 -- example appear in a Compile_Time_Warning pragma). The frozen
8096 -- status of the type does not affect its evaluation.
8098 and then Id
/= Attribute_Unconstrained_Array
8103 -- At this stage P_Entity is the entity to which the attribute
8104 -- is to be applied. This is usually simply the entity of the
8105 -- prefix, except in some cases of attributes for objects, where
8106 -- as described above, we apply the attribute to the object type.
8108 -- Here is where we make sure that static attributes are properly
8109 -- marked as such. These are attributes whose prefix is a static
8110 -- scalar subtype, whose result is scalar, and whose arguments, if
8111 -- present, are static scalar expressions. Note that such references
8112 -- are static expressions even if they raise Constraint_Error.
8114 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
8115 -- though evaluating it raises constraint error. This means that a
8116 -- declaration like:
8118 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
8120 -- is legal, since here this expression appears in a statically
8121 -- unevaluated position, so it does not actually raise an exception.
8123 -- T'Descriptor_Size is never static, even if T is static.
8125 if Is_Scalar_Type
(P_Entity
)
8126 and then (not Is_Generic_Type
(P_Entity
))
8127 and then Is_Static_Subtype
(P_Entity
)
8128 and then Is_Scalar_Type
(Etype
(N
))
8131 or else (Is_Static_Expression
(E1
)
8132 and then Is_Scalar_Type
(Etype
(E1
))))
8135 or else (Is_Static_Expression
(E2
)
8136 and then Is_Scalar_Type
(Etype
(E1
))))
8137 and then Id
/= Attribute_Descriptor_Size
8140 Set_Is_Static_Expression
(N
, True);
8143 -- First foldable possibility is a scalar or array type (RM 4.9(7))
8144 -- that is not generic (generic types are eliminated by RM 4.9(25)).
8145 -- Note we allow nonstatic nongeneric types at this stage as further
8148 if Is_Type
(P_Entity
)
8149 and then (Is_Scalar_Type
(P_Entity
) or Is_Array_Type
(P_Entity
))
8150 and then (not Is_Generic_Type
(P_Entity
))
8154 -- Second foldable possibility is an array object (RM 4.9(8))
8156 elsif Ekind
(P_Entity
) in E_Variable | E_Constant
8157 and then Is_Array_Type
(Etype
(P_Entity
))
8158 and then (not Is_Generic_Type
(Etype
(P_Entity
)))
8160 P_Type
:= Etype
(P_Entity
);
8162 -- If the entity is an array constant with an unconstrained nominal
8163 -- subtype then get the type from the initial value. If the value has
8164 -- been expanded into assignments, there is no expression and the
8165 -- attribute reference remains dynamic.
8167 -- We could do better here and retrieve the type ???
8169 if Ekind
(P_Entity
) = E_Constant
8170 and then not Is_Constrained
(P_Type
)
8172 if No
(Constant_Value
(P_Entity
)) then
8175 P_Type
:= Etype
(Constant_Value
(P_Entity
));
8179 -- Definite must be folded if the prefix is not a generic type, that
8180 -- is to say if we are within an instantiation. Same processing applies
8181 -- to selected GNAT attributes.
8183 elsif (Id
= Attribute_Atomic_Always_Lock_Free
or else
8184 Id
= Attribute_Definite
or else
8185 Id
= Attribute_Descriptor_Size
or else
8186 Id
= Attribute_Has_Access_Values
or else
8187 Id
= Attribute_Has_Discriminants
or else
8188 Id
= Attribute_Has_Tagged_Values
or else
8189 Id
= Attribute_Lock_Free
or else
8190 Id
= Attribute_Type_Class
or else
8191 Id
= Attribute_Unconstrained_Array
or else
8192 Id
= Attribute_Max_Alignment_For_Allocation
)
8193 and then not Is_Generic_Type
(P_Entity
)
8197 -- We can fold 'Size applied to a type if the size is known (as happens
8198 -- for a size from an attribute definition clause). At this stage, this
8199 -- can happen only for types (e.g. record types) for which the size is
8200 -- always non-static. We exclude generic types from consideration (since
8201 -- they have bogus sizes set within templates). We can also fold
8202 -- Max_Size_In_Storage_Elements in the same cases.
8204 elsif (Id
= Attribute_Size
or
8205 Id
= Attribute_Max_Size_In_Storage_Elements
)
8206 and then Is_Type
(P_Entity
)
8207 and then (not Is_Generic_Type
(P_Entity
))
8208 and then Known_Static_RM_Size
(P_Entity
)
8211 Attr_Value
: Uint
:= RM_Size
(P_Entity
);
8213 if Id
= Attribute_Max_Size_In_Storage_Elements
then
8214 Attr_Value
:= (Attr_Value
+ System_Storage_Unit
- 1)
8215 / System_Storage_Unit
;
8217 Compile_Time_Known_Attribute
(N
, Attr_Value
);
8221 -- We can fold 'Alignment applied to a type if the alignment is known
8222 -- (as happens for an alignment from an attribute definition clause).
8223 -- At this stage, this can happen only for types (e.g. record types) for
8224 -- which the size is always non-static. We exclude generic types from
8225 -- consideration (since they have bogus sizes set within templates).
8227 elsif Id
= Attribute_Alignment
8228 and then Is_Type
(P_Entity
)
8229 and then (not Is_Generic_Type
(P_Entity
))
8230 and then Known_Alignment
(P_Entity
)
8232 Compile_Time_Known_Attribute
(N
, Alignment
(P_Entity
));
8235 -- If this is an access attribute that is known to fail accessibility
8236 -- check, rewrite accordingly.
8238 elsif Attribute_Name
(N
) = Name_Access
8239 and then Raises_Constraint_Error
(N
)
8242 Make_Raise_Program_Error
(Loc
,
8243 Reason
=> PE_Accessibility_Check_Failed
));
8244 Set_Etype
(N
, C_Type
);
8247 -- No other cases are foldable (they certainly aren't static, and at
8248 -- the moment we don't try to fold any cases other than the ones above).
8255 -- If either attribute or the prefix is Any_Type, then propagate
8256 -- Any_Type to the result and don't do anything else at all.
8258 if P_Type
= Any_Type
8259 or else (Present
(E1
) and then Etype
(E1
) = Any_Type
)
8260 or else (Present
(E2
) and then Etype
(E2
) = Any_Type
)
8262 Set_Etype
(N
, Any_Type
);
8266 -- Scalar subtype case. We have not yet enforced the static requirement
8267 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
8268 -- of non-static attribute references (e.g. S'Digits for a non-static
8269 -- floating-point type, which we can compute at compile time).
8271 -- Note: this folding of non-static attributes is not simply a case of
8272 -- optimization. For many of the attributes affected, Gigi cannot handle
8273 -- the attribute and depends on the front end having folded them away.
8275 -- Note: although we don't require staticness at this stage, we do set
8276 -- the Static variable to record the staticness, for easy reference by
8277 -- those attributes where it matters (e.g. Succ and Pred), and also to
8278 -- be used to ensure that non-static folded things are not marked as
8279 -- being static (a check that is done right at the end).
8281 P_Root_Type
:= Root_Type
(P_Type
);
8282 P_Base_Type
:= Base_Type
(P_Type
);
8284 -- If the root type or base type is generic, then we cannot fold. This
8285 -- test is needed because subtypes of generic types are not always
8286 -- marked as being generic themselves (which seems odd???)
8288 if Is_Generic_Type
(P_Root_Type
)
8289 or else Is_Generic_Type
(P_Base_Type
)
8294 if Is_Scalar_Type
(P_Type
) then
8295 if not Is_Static_Subtype
(P_Type
) then
8297 Set_Is_Static_Expression
(N
, False);
8298 elsif not Is_OK_Static_Subtype
(P_Type
) then
8299 Set_Raises_Constraint_Error
(N
);
8302 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8303 -- since we can't do anything with unconstrained arrays. In addition,
8304 -- only the First, Last and Length attributes are possibly static.
8306 -- Atomic_Always_Lock_Free, Definite, Descriptor_Size, Has_Access_Values
8307 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
8308 -- Unconstrained_Array are again exceptions, because they apply as well
8309 -- to unconstrained types.
8311 -- In addition Component_Size is an exception since it is possibly
8312 -- foldable, even though it is never static, and it does apply to
8313 -- unconstrained arrays. Furthermore, it is essential to fold this
8314 -- in the packed case, since otherwise the value will be incorrect.
8316 elsif Id
= Attribute_Atomic_Always_Lock_Free
or else
8317 Id
= Attribute_Definite
or else
8318 Id
= Attribute_Descriptor_Size
or else
8319 Id
= Attribute_Has_Access_Values
or else
8320 Id
= Attribute_Has_Discriminants
or else
8321 Id
= Attribute_Has_Tagged_Values
or else
8322 Id
= Attribute_Lock_Free
or else
8323 Id
= Attribute_Type_Class
or else
8324 Id
= Attribute_Unconstrained_Array
or else
8325 Id
= Attribute_Component_Size
8328 Set_Is_Static_Expression
(N
, False);
8330 elsif Id
/= Attribute_Max_Alignment_For_Allocation
then
8331 if not Is_Constrained
(P_Type
)
8332 or else (Id
/= Attribute_First
and then
8333 Id
/= Attribute_Last
and then
8334 Id
/= Attribute_Length
)
8340 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8341 -- scalar case, we hold off on enforcing staticness, since there are
8342 -- cases which we can fold at compile time even though they are not
8343 -- static (e.g. 'Length applied to a static index, even though other
8344 -- non-static indexes make the array type non-static). This is only
8345 -- an optimization, but it falls out essentially free, so why not.
8346 -- Again we compute the variable Static for easy reference later
8347 -- (note that no array attributes are static in Ada 83).
8349 -- We also need to set Static properly for subsequent legality checks
8350 -- which might otherwise accept non-static constants in contexts
8351 -- where they are not legal.
8354 Ada_Version
>= Ada_95
and then Statically_Denotes_Entity
(P
);
8355 Set_Is_Static_Expression
(N
, Static
);
8361 Nod
:= First_Index
(P_Type
);
8363 -- The expression is static if the array type is constrained
8364 -- by given bounds, and not by an initial expression. Constant
8365 -- strings are static in any case.
8367 if Root_Type
(P_Type
) /= Standard_String
then
8369 Static
and then not Is_Constr_Subt_For_U_Nominal
(P_Type
);
8370 Set_Is_Static_Expression
(N
, Static
);
8373 while Present
(Nod
) loop
8374 if not Is_Static_Subtype
(Etype
(Nod
)) then
8376 Set_Is_Static_Expression
(N
, False);
8378 elsif not Is_OK_Static_Subtype
(Etype
(Nod
)) then
8379 Set_Raises_Constraint_Error
(N
);
8381 Set_Is_Static_Expression
(N
, False);
8384 -- If however the index type is generic, or derived from
8385 -- one, attributes cannot be folded.
8387 if Is_Generic_Type
(Root_Type
(Etype
(Nod
)))
8388 and then Id
/= Attribute_Component_Size
8398 -- Check any expressions that are present. Note that these expressions,
8399 -- depending on the particular attribute type, are either part of the
8400 -- attribute designator, or they are arguments in a case where the
8401 -- attribute reference returns a function. In the latter case, the
8402 -- rule in (RM 4.9(22)) applies and in particular requires the type
8403 -- of the expressions to be scalar in order for the attribute to be
8404 -- considered to be static.
8412 while Present
(E
) loop
8414 -- If expression is not static, then the attribute reference
8415 -- result certainly cannot be static.
8417 if not Is_Static_Expression
(E
) then
8419 Set_Is_Static_Expression
(N
, False);
8422 if Raises_Constraint_Error
(E
) then
8423 Set_Raises_Constraint_Error
(N
);
8426 -- If the result is not known at compile time, or is not of
8427 -- a scalar type, then the result is definitely not static,
8428 -- so we can quit now.
8430 if not Compile_Time_Known_Value
(E
)
8431 or else not Is_Scalar_Type
(Etype
(E
))
8436 -- If the expression raises a constraint error, then so does
8437 -- the attribute reference. We keep going in this case because
8438 -- we are still interested in whether the attribute reference
8439 -- is static even if it is not static.
8441 elsif Raises_Constraint_Error
(E
) then
8442 Set_Raises_Constraint_Error
(N
);
8448 if Raises_Constraint_Error
(Prefix
(N
)) then
8449 Set_Is_Static_Expression
(N
, False);
8454 -- Deal with the case of a static attribute reference that raises
8455 -- constraint error. The Raises_Constraint_Error flag will already
8456 -- have been set, and the Static flag shows whether the attribute
8457 -- reference is static. In any case we certainly can't fold such an
8458 -- attribute reference.
8460 -- Note that the rewriting of the attribute node with the constraint
8461 -- error node is essential in this case, because otherwise Gigi might
8462 -- blow up on one of the attributes it never expects to see.
8464 -- The constraint_error node must have the type imposed by the context,
8465 -- to avoid spurious errors in the enclosing expression.
8467 if Raises_Constraint_Error
(N
) then
8469 Make_Raise_Constraint_Error
(Sloc
(N
),
8470 Reason
=> CE_Range_Check_Failed
);
8471 Set_Etype
(CE_Node
, Etype
(N
));
8472 Set_Raises_Constraint_Error
(CE_Node
);
8474 Rewrite
(N
, Relocate_Node
(CE_Node
));
8475 Set_Raises_Constraint_Error
(N
, True);
8479 -- At this point we have a potentially foldable attribute reference.
8480 -- If Static is set, then the attribute reference definitely obeys
8481 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8482 -- folded. If Static is not set, then the attribute may or may not
8483 -- be foldable, and the individual attribute processing routines
8484 -- test Static as required in cases where it makes a difference.
8486 -- In the case where Static is not set, we do know that all the
8487 -- expressions present are at least known at compile time (we assumed
8488 -- above that if this was not the case, then there was no hope of static
8489 -- evaluation). However, we did not require that the bounds of the
8490 -- prefix type be compile time known, let alone static). That's because
8491 -- there are many attributes that can be computed at compile time on
8492 -- non-static subtypes, even though such references are not static
8495 -- For VAX float, the root type is an IEEE type. So make sure to use the
8496 -- base type instead of the root-type for floating point attributes.
8500 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
8503 when Attribute_Constant_Indexing
8504 | Attribute_Default_Iterator
8505 | Attribute_Implicit_Dereference
8506 | Attribute_Iterator_Element
8507 | Attribute_Iterable
8509 | Attribute_Variable_Indexing
8513 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8514 -- These were already rejected by the parser. Thus they shouldn't
8517 when Internal_Attribute_Id
=>
8518 raise Program_Error
;
8524 when Attribute_Adjacent
=>
8528 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
8535 when Attribute_Aft
=>
8536 Fold_Uint
(N
, Aft_Value
(P_Type
), Static
);
8542 when Attribute_Alignment
=> Alignment_Block
: declare
8543 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
8546 -- Fold if alignment is set and not otherwise
8548 if Known_Alignment
(P_TypeA
) then
8549 Fold_Uint
(N
, Alignment
(P_TypeA
), Static
);
8551 end Alignment_Block
;
8553 -----------------------------
8554 -- Atomic_Always_Lock_Free --
8555 -----------------------------
8557 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8560 when Attribute_Atomic_Always_Lock_Free
=> Atomic_Always_Lock_Free
:
8562 V
: constant Entity_Id
:=
8564 (Support_Atomic_Primitives_On_Target
8565 and then Support_Atomic_Primitives
(P_Type
));
8568 Rewrite
(N
, New_Occurrence_Of
(V
, Loc
));
8570 -- Analyze and resolve as boolean. Note that this attribute is a
8571 -- static attribute in GNAT.
8573 Analyze_And_Resolve
(N
, Standard_Boolean
);
8575 Set_Is_Static_Expression
(N
);
8576 end Atomic_Always_Lock_Free
;
8582 -- Bit can never be folded
8584 when Attribute_Bit
=>
8591 -- Body_version can never be static
8593 when Attribute_Body_Version
=>
8600 when Attribute_Ceiling
=>
8602 (N
, Eval_Fat
.Ceiling
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8604 --------------------
8605 -- Component_Size --
8606 --------------------
8608 -- Fold Component_Size if it is known at compile time, which is always
8609 -- true in the packed array case. It is important that the packed array
8610 -- case is handled here since the back end would otherwise get confused
8611 -- by the equivalent packed array type.
8613 when Attribute_Component_Size
=>
8614 if Known_Static_Component_Size
(P_Type
) then
8615 Fold_Uint
(N
, Component_Size
(P_Type
), Static
);
8622 when Attribute_Compose
=>
8625 Eval_Fat
.Compose
(P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
8632 -- Constrained is never folded for now, there may be cases that
8633 -- could be handled at compile time. To be looked at later.
8635 when Attribute_Constrained
=>
8637 -- The expander might fold it and set the static flag accordingly,
8638 -- but with expansion disabled, it remains as an attribute reference,
8639 -- and this reference is not static.
8641 Set_Is_Static_Expression
(N
, False);
8647 when Attribute_Copy_Sign
=>
8651 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
8658 when Attribute_Definite
=>
8659 Rewrite
(N
, New_Occurrence_Of
(
8660 Boolean_Literals
(Is_Definite_Subtype
(P_Entity
)), Loc
));
8661 Analyze_And_Resolve
(N
, Standard_Boolean
);
8667 when Attribute_Delta
=>
8668 Fold_Ureal
(N
, Delta_Value
(P_Type
), True);
8674 when Attribute_Denorm
=>
8676 (N
, UI_From_Int
(Boolean'Pos (Has_Denormals
(P_Type
))), Static
);
8678 ---------------------
8679 -- Descriptor_Size --
8680 ---------------------
8682 -- Descriptor_Size is nonnull only for unconstrained array types
8684 when Attribute_Descriptor_Size
=>
8685 if not Is_Array_Type
(P_Type
) or else Is_Constrained
(P_Type
) then
8686 Fold_Uint
(N
, Uint_0
, Static
);
8693 when Attribute_Digits
=>
8694 Fold_Uint
(N
, Digits_Value
(P_Type
), Static
);
8700 when Attribute_Emax
=>
8702 -- Ada 83 attribute is defined as (RM83 3.5.8)
8704 -- T'Emax = 4 * T'Mantissa
8706 Fold_Uint
(N
, 4 * Mantissa
, Static
);
8712 when Attribute_Enum_Rep
=> Enum_Rep
: declare
8716 -- The attribute appears in the form:
8718 -- Enum_Typ'Enum_Rep (Const)
8719 -- Enum_Typ'Enum_Rep (Enum_Lit)
8721 if Present
(E1
) then
8724 -- Otherwise the prefix denotes a constant or enumeration literal:
8727 -- Enum_Lit'Enum_Rep
8733 -- For an enumeration type with a non-standard representation use
8734 -- the Enumeration_Rep field of the proper constant. Note that this
8735 -- will not work for types Character/Wide_[Wide-]Character, since no
8736 -- real entities are created for the enumeration literals, but that
8737 -- does not matter since these two types do not have non-standard
8738 -- representations anyway.
8740 if Is_Enumeration_Type
(P_Type
)
8741 and then Has_Non_Standard_Rep
(P_Type
)
8743 Fold_Uint
(N
, Enumeration_Rep
(Expr_Value_E
(Val
)), Static
);
8745 -- For enumeration types with standard representations and all other
8746 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8750 Fold_Uint
(N
, Expr_Value
(Val
), Static
);
8758 when Attribute_Enum_Val
=> Enum_Val
: declare
8762 -- We have something like Enum_Type'Enum_Val (23), so search for a
8763 -- corresponding value in the list of Enum_Rep values for the type.
8765 Lit
:= First_Literal
(P_Base_Type
);
8767 if Enumeration_Rep
(Lit
) = Expr_Value
(E1
) then
8768 Fold_Uint
(N
, Enumeration_Pos
(Lit
), Static
);
8775 Apply_Compile_Time_Constraint_Error
8776 (N
, "no representation value matches",
8777 CE_Range_Check_Failed
,
8778 Warn
=> not Static
);
8788 when Attribute_Epsilon
=>
8790 -- Ada 83 attribute is defined as (RM83 3.5.8)
8792 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8794 Fold_Ureal
(N
, Ureal_2
** (1 - Mantissa
), True);
8800 when Attribute_Exponent
=>
8802 Eval_Fat
.Exponent
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8804 -----------------------
8805 -- Finalization_Size --
8806 -----------------------
8808 when Attribute_Finalization_Size
=>
8815 when Attribute_First
=>
8818 if Compile_Time_Known_Value
(Lo_Bound
) then
8819 if Is_Real_Type
(P_Type
) then
8820 Fold_Ureal
(N
, Expr_Value_R
(Lo_Bound
), Static
);
8822 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
8826 Check_Concurrent_Discriminant
(Lo_Bound
);
8833 when Attribute_First_Valid
=>
8834 if Has_Predicates
(P_Type
)
8835 and then Has_Static_Predicate
(P_Type
)
8838 FirstN
: constant Node_Id
:=
8839 First
(Static_Discrete_Predicate
(P_Type
));
8841 if Nkind
(FirstN
) = N_Range
then
8842 Fold_Uint
(N
, Expr_Value
(Low_Bound
(FirstN
)), Static
);
8844 Fold_Uint
(N
, Expr_Value
(FirstN
), Static
);
8850 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
8857 when Attribute_Fixed_Value
=>
8864 when Attribute_Floor
=>
8866 (N
, Eval_Fat
.Floor
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8872 when Attribute_Fore
=>
8873 if Compile_Time_Known_Bounds
(P_Type
) then
8874 Fold_Uint
(N
, UI_From_Int
(Fore_Value
), Static
);
8881 when Attribute_Fraction
=>
8883 (N
, Eval_Fat
.Fraction
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8885 -----------------------
8886 -- Has_Access_Values --
8887 -----------------------
8889 when Attribute_Has_Access_Values
=>
8890 Rewrite
(N
, New_Occurrence_Of
8891 (Boolean_Literals
(Has_Access_Values
(P_Root_Type
)), Loc
));
8892 Analyze_And_Resolve
(N
, Standard_Boolean
);
8894 -----------------------
8895 -- Has_Discriminants --
8896 -----------------------
8898 when Attribute_Has_Discriminants
=>
8899 Rewrite
(N
, New_Occurrence_Of
(
8900 Boolean_Literals
(Has_Discriminants
(P_Entity
)), Loc
));
8901 Analyze_And_Resolve
(N
, Standard_Boolean
);
8903 ----------------------
8904 -- Has_Same_Storage --
8905 ----------------------
8907 when Attribute_Has_Same_Storage
=>
8910 -----------------------
8911 -- Has_Tagged_Values --
8912 -----------------------
8914 when Attribute_Has_Tagged_Values
=>
8915 Rewrite
(N
, New_Occurrence_Of
8916 (Boolean_Literals
(Has_Tagged_Component
(P_Root_Type
)), Loc
));
8917 Analyze_And_Resolve
(N
, Standard_Boolean
);
8923 when Attribute_Identity
=>
8930 -- Image is a scalar attribute, but is never static, because it is
8931 -- not a static function (having a non-scalar argument (RM 4.9(22))
8932 -- However, we can constant-fold the image of an enumeration literal
8933 -- if names are available.
8935 when Attribute_Image
=>
8936 if Is_Entity_Name
(E1
)
8937 and then Ekind
(Entity
(E1
)) = E_Enumeration_Literal
8938 and then not Discard_Names
(First_Subtype
(Etype
(E1
)))
8939 and then not Global_Discard_Names
8942 Lit
: constant Entity_Id
:= Entity
(E1
);
8946 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
8947 Set_Casing
(All_Upper_Case
);
8948 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
8950 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
8951 Analyze_And_Resolve
(N
, Standard_String
);
8952 Set_Is_Static_Expression
(N
, False);
8960 -- We never try to fold Integer_Value (though perhaps we could???)
8962 when Attribute_Integer_Value
=>
8969 -- Invalid_Value is a scalar attribute that is never static, because
8970 -- the value is by design out of range.
8972 when Attribute_Invalid_Value
=>
8979 when Attribute_Large
=>
8981 -- For fixed-point, we use the identity:
8983 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8985 if Is_Fixed_Point_Type
(P_Type
) then
8987 Make_Op_Multiply
(Loc
,
8989 Make_Op_Subtract
(Loc
,
8993 Make_Real_Literal
(Loc
, Ureal_2
),
8995 Make_Attribute_Reference
(Loc
,
8997 Attribute_Name
=> Name_Mantissa
)),
8998 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_1
)),
9001 Make_Real_Literal
(Loc
, Small_Value
(Entity
(P
)))));
9003 Analyze_And_Resolve
(N
, C_Type
);
9005 -- Floating-point (Ada 83 compatibility)
9008 -- Ada 83 attribute is defined as (RM83 3.5.8)
9010 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
9014 -- T'Emax = 4 * T'Mantissa
9018 Ureal_2
** (4 * Mantissa
) * (Ureal_1
- Ureal_2
** (-Mantissa
)),
9026 when Attribute_Lock_Free
=> Lock_Free
: declare
9027 V
: constant Entity_Id
:= Boolean_Literals
(Uses_Lock_Free
(P_Type
));
9030 Rewrite
(N
, New_Occurrence_Of
(V
, Loc
));
9032 -- Analyze and resolve as boolean. Note that this attribute is a
9033 -- static attribute in GNAT.
9035 Analyze_And_Resolve
(N
, Standard_Boolean
);
9037 Set_Is_Static_Expression
(N
);
9044 when Attribute_Last
=>
9047 if Compile_Time_Known_Value
(Hi_Bound
) then
9048 if Is_Real_Type
(P_Type
) then
9049 Fold_Ureal
(N
, Expr_Value_R
(Hi_Bound
), Static
);
9051 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
9055 Check_Concurrent_Discriminant
(Hi_Bound
);
9062 when Attribute_Last_Valid
=>
9063 if Has_Predicates
(P_Type
)
9064 and then Has_Static_Predicate
(P_Type
)
9067 LastN
: constant Node_Id
:=
9068 Last
(Static_Discrete_Predicate
(P_Type
));
9070 if Nkind
(LastN
) = N_Range
then
9071 Fold_Uint
(N
, Expr_Value
(High_Bound
(LastN
)), Static
);
9073 Fold_Uint
(N
, Expr_Value
(LastN
), Static
);
9079 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
9086 when Attribute_Leading_Part
=>
9089 Eval_Fat
.Leading_Part
9090 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
9097 when Attribute_Length
=> Length
: declare
9101 -- If any index type is a formal type, or derived from one, the
9102 -- bounds are not static. Treating them as static can produce
9103 -- spurious warnings or improper constant folding.
9105 Ind
:= First_Index
(P_Type
);
9106 while Present
(Ind
) loop
9107 if Is_Generic_Type
(Root_Type
(Etype
(Ind
))) then
9116 -- For two compile time values, we can compute length
9118 if Compile_Time_Known_Value
(Lo_Bound
)
9119 and then Compile_Time_Known_Value
(Hi_Bound
)
9122 UI_Max
(0, 1 + (Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
))),
9126 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9127 -- comparable, and we can figure out the difference between them.
9130 Diff
: aliased Uint
;
9134 Compile_Time_Compare
9135 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
9138 Fold_Uint
(N
, Uint_1
, Static
);
9141 Fold_Uint
(N
, Uint_0
, Static
);
9144 if Diff
/= No_Uint
then
9145 Fold_Uint
(N
, Diff
+ 1, Static
);
9158 -- Loop_Entry acts as an alias of a constant initialized to the prefix
9159 -- of the said attribute at the point of entry into the related loop. As
9160 -- such, the attribute reference does not need to be evaluated because
9161 -- the prefix is the one that is evaluted.
9163 when Attribute_Loop_Entry
=>
9170 when Attribute_Machine
=>
9174 (P_Base_Type
, Expr_Value_R
(E1
), Eval_Fat
.Round
, N
),
9181 when Attribute_Machine_Emax
=>
9182 Fold_Uint
(N
, Machine_Emax_Value
(P_Type
), Static
);
9188 when Attribute_Machine_Emin
=>
9189 Fold_Uint
(N
, Machine_Emin_Value
(P_Type
), Static
);
9191 ----------------------
9192 -- Machine_Mantissa --
9193 ----------------------
9195 when Attribute_Machine_Mantissa
=>
9196 Fold_Uint
(N
, Machine_Mantissa_Value
(P_Type
), Static
);
9198 -----------------------
9199 -- Machine_Overflows --
9200 -----------------------
9202 when Attribute_Machine_Overflows
=>
9204 -- Always true for fixed-point
9206 if Is_Fixed_Point_Type
(P_Type
) then
9207 Fold_Uint
(N
, True_Value
, Static
);
9209 -- Floating point case
9213 UI_From_Int
(Boolean'Pos (Machine_Overflows_On_Target
)),
9221 when Attribute_Machine_Radix
=>
9222 if Is_Fixed_Point_Type
(P_Type
) then
9223 if Is_Decimal_Fixed_Point_Type
(P_Type
)
9224 and then Machine_Radix_10
(P_Type
)
9226 Fold_Uint
(N
, Uint_10
, Static
);
9228 Fold_Uint
(N
, Uint_2
, Static
);
9231 -- All floating-point type always have radix 2
9234 Fold_Uint
(N
, Uint_2
, Static
);
9237 ----------------------
9238 -- Machine_Rounding --
9239 ----------------------
9241 -- Note: for the folding case, it is fine to treat Machine_Rounding
9242 -- exactly the same way as Rounding, since this is one of the allowed
9243 -- behaviors, and performance is not an issue here. It might be a bit
9244 -- better to give the same result as it would give at run time, even
9245 -- though the non-determinism is certainly permitted.
9247 when Attribute_Machine_Rounding
=>
9249 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9251 --------------------
9252 -- Machine_Rounds --
9253 --------------------
9255 when Attribute_Machine_Rounds
=>
9257 -- Always False for fixed-point
9259 if Is_Fixed_Point_Type
(P_Type
) then
9260 Fold_Uint
(N
, False_Value
, Static
);
9262 -- Else yield proper floating-point result
9266 (N
, UI_From_Int
(Boolean'Pos (Machine_Rounds_On_Target
)),
9274 -- Note: Machine_Size is identical to Object_Size
9276 when Attribute_Machine_Size
=> Machine_Size
: declare
9277 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9280 if Known_Esize
(P_TypeA
) then
9281 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9289 when Attribute_Mantissa
=>
9291 -- Fixed-point mantissa
9293 if Is_Fixed_Point_Type
(P_Type
) then
9295 -- Compile time foldable case
9297 if Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
9299 Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
9301 -- The calculation of the obsolete Ada 83 attribute Mantissa
9302 -- is annoying, because of AI00143, quoted here:
9304 -- !question 84-01-10
9306 -- Consider the model numbers for F:
9308 -- type F is delta 1.0 range -7.0 .. 8.0;
9310 -- The wording requires that F'MANTISSA be the SMALLEST
9311 -- integer number for which each bound of the specified
9312 -- range is either a model number or lies at most small
9313 -- distant from a model number. This means F'MANTISSA
9314 -- is required to be 3 since the range -7.0 .. 7.0 fits
9315 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9316 -- number, namely, 7. Is this analysis correct? Note that
9317 -- this implies the upper bound of the range is not
9318 -- represented as a model number.
9320 -- !response 84-03-17
9322 -- The analysis is correct. The upper and lower bounds for
9323 -- a fixed point type can lie outside the range of model
9334 LBound
:= Expr_Value_R
(Type_Low_Bound
(P_Type
));
9335 UBound
:= Expr_Value_R
(Type_High_Bound
(P_Type
));
9336 Bound
:= UR_Max
(UR_Abs
(LBound
), UR_Abs
(UBound
));
9337 Max_Man
:= UR_Trunc
(Bound
/ Small_Value
(P_Type
));
9339 -- If the Bound is exactly a model number, i.e. a multiple
9340 -- of Small, then we back it off by one to get the integer
9341 -- value that must be representable.
9343 if Small_Value
(P_Type
) * Max_Man
= Bound
then
9344 Max_Man
:= Max_Man
- 1;
9347 -- Now find corresponding size = Mantissa value
9350 while 2 ** Siz
< Max_Man
loop
9354 Fold_Uint
(N
, Siz
, Static
);
9358 -- The case of dynamic bounds cannot be evaluated at compile
9359 -- time. Instead we use a runtime routine (see Exp_Attr).
9364 -- Floating-point Mantissa
9367 Fold_Uint
(N
, Mantissa
, Static
);
9374 when Attribute_Max
=>
9375 if Is_Real_Type
(P_Type
) then
9377 (N
, UR_Max
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
9379 Fold_Uint
(N
, UI_Max
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
9382 ----------------------------------
9383 -- Max_Alignment_For_Allocation --
9384 ----------------------------------
9386 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9387 -- arrays are allocated with dope, so we need to take into account both
9388 -- the alignment of the array, which comes from the component alignment,
9389 -- and the alignment of the dope. Also, if the alignment is unknown, we
9390 -- use the max (it's OK to be pessimistic).
9392 when Attribute_Max_Alignment_For_Allocation
=> Max_Align
: declare
9393 A
: Uint
:= UI_From_Int
(Ttypes
.Maximum_Alignment
);
9395 if Known_Alignment
(P_Type
)
9396 and then (not Is_Array_Type
(P_Type
) or else Alignment
(P_Type
) > A
)
9398 A
:= Alignment
(P_Type
);
9401 Fold_Uint
(N
, A
, Static
);
9404 ----------------------------------
9405 -- Max_Size_In_Storage_Elements --
9406 ----------------------------------
9408 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9409 -- Storage_Unit boundary. We can fold any cases for which the size
9410 -- is known by the front end.
9412 when Attribute_Max_Size_In_Storage_Elements
=>
9413 if Known_Esize
(P_Type
) then
9415 (Esize
(P_Type
) + System_Storage_Unit
- 1) /
9416 System_Storage_Unit
,
9420 --------------------
9421 -- Mechanism_Code --
9422 --------------------
9424 when Attribute_Mechanism_Code
=> Mechanism_Code
: declare
9426 Mech
: Mechanism_Type
;
9431 Mech
:= Mechanism
(P_Entity
);
9434 Val
:= UI_To_Int
(Expr_Value
(E1
));
9436 Formal
:= First_Formal
(P_Entity
);
9437 for J
in 1 .. Val
- 1 loop
9438 Next_Formal
(Formal
);
9441 Mech
:= Mechanism
(Formal
);
9445 Fold_Uint
(N
, UI_From_Int
(Int
(-Mech
)), Static
);
9453 when Attribute_Min
=>
9454 if Is_Real_Type
(P_Type
) then
9456 (N
, UR_Min
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
9459 (N
, UI_Min
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
9466 when Attribute_Mod
=>
9468 (N
, UI_Mod
(Expr_Value
(E1
), Modulus
(P_Base_Type
)), Static
);
9474 when Attribute_Model
=>
9476 (N
, Eval_Fat
.Model
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9482 when Attribute_Model_Emin
=>
9483 Fold_Uint
(N
, Model_Emin_Value
(P_Base_Type
), Static
);
9489 when Attribute_Model_Epsilon
=>
9490 Fold_Ureal
(N
, Model_Epsilon_Value
(P_Base_Type
), Static
);
9492 --------------------
9493 -- Model_Mantissa --
9494 --------------------
9496 when Attribute_Model_Mantissa
=>
9497 Fold_Uint
(N
, Model_Mantissa_Value
(P_Base_Type
), Static
);
9503 when Attribute_Model_Small
=>
9504 Fold_Ureal
(N
, Model_Small_Value
(P_Base_Type
), Static
);
9510 when Attribute_Modulus
=>
9511 Fold_Uint
(N
, Modulus
(P_Type
), Static
);
9513 --------------------
9514 -- Null_Parameter --
9515 --------------------
9517 -- Cannot fold, we know the value sort of, but the whole point is
9518 -- that there is no way to talk about this imaginary value except
9519 -- by using the attribute, so we leave it the way it is.
9521 when Attribute_Null_Parameter
=>
9528 -- The Object_Size attribute for a type returns the Esize of the
9529 -- type and can be folded if this value is known.
9531 when Attribute_Object_Size
=> Object_Size
: declare
9532 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9535 if Known_Esize
(P_TypeA
) then
9536 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9540 ----------------------
9541 -- Overlaps_Storage --
9542 ----------------------
9544 when Attribute_Overlaps_Storage
=>
9547 -------------------------
9548 -- Passed_By_Reference --
9549 -------------------------
9551 -- Scalar types are never passed by reference
9553 when Attribute_Passed_By_Reference
=>
9554 Fold_Uint
(N
, False_Value
, Static
);
9560 when Attribute_Pos
=>
9561 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
9567 when Attribute_Pred
=>
9569 -- Floating-point case
9571 if Is_Floating_Point_Type
(P_Type
) then
9573 (N
, Eval_Fat
.Pred
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9577 elsif Is_Fixed_Point_Type
(P_Type
) then
9579 (N
, Expr_Value_R
(E1
) - Small_Value
(P_Type
), True);
9581 -- Modular integer case (wraps)
9583 elsif Is_Modular_Integer_Type
(P_Type
) then
9584 Fold_Uint
(N
, (Expr_Value
(E1
) - 1) mod Modulus
(P_Type
), Static
);
9586 -- Other scalar cases
9589 pragma Assert
(Is_Scalar_Type
(P_Type
));
9591 if Is_Enumeration_Type
(P_Type
)
9592 and then Expr_Value
(E1
) =
9593 Expr_Value
(Type_Low_Bound
(P_Base_Type
))
9595 Apply_Compile_Time_Constraint_Error
9596 (N
, "Pred of `&''First`",
9597 CE_Overflow_Check_Failed
,
9599 Warn
=> not Static
);
9605 Fold_Uint
(N
, Expr_Value
(E1
) - 1, Static
);
9612 -- No processing required, because by this stage, Range has been
9613 -- replaced by First .. Last, so this branch can never be taken.
9615 when Attribute_Range
=>
9616 raise Program_Error
;
9622 when Attribute_Range_Length
=> Range_Length
: declare
9623 Diff
: aliased Uint
;
9628 -- Can fold if both bounds are compile time known
9630 if Compile_Time_Known_Value
(Hi_Bound
)
9631 and then Compile_Time_Known_Value
(Lo_Bound
)
9635 (0, Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
) + 1),
9639 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9640 -- comparable, and we can figure out the difference between them.
9642 case Compile_Time_Compare
9643 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
9646 Fold_Uint
(N
, Uint_1
, Static
);
9649 Fold_Uint
(N
, Uint_0
, Static
);
9652 if Diff
/= No_Uint
then
9653 Fold_Uint
(N
, Diff
+ 1, Static
);
9665 when Attribute_Ref
=>
9666 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
9672 when Attribute_Remainder
=> Remainder
: declare
9673 X
: constant Ureal
:= Expr_Value_R
(E1
);
9674 Y
: constant Ureal
:= Expr_Value_R
(E2
);
9677 if UR_Is_Zero
(Y
) then
9678 Apply_Compile_Time_Constraint_Error
9679 (N
, "division by zero in Remainder",
9680 CE_Overflow_Check_Failed
,
9681 Warn
=> not Static
);
9687 Fold_Ureal
(N
, Eval_Fat
.Remainder
(P_Base_Type
, X
, Y
), Static
);
9694 when Attribute_Restriction_Set
=>
9695 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
9696 Set_Is_Static_Expression
(N
);
9702 when Attribute_Round
=> Round
: declare
9707 -- First we get the (exact result) in units of small
9709 Sr
:= Expr_Value_R
(E1
) / Small_Value
(C_Type
);
9711 -- Now round that exactly to an integer
9713 Si
:= UR_To_Uint
(Sr
);
9715 -- Finally the result is obtained by converting back to real
9717 Fold_Ureal
(N
, Si
* Small_Value
(C_Type
), Static
);
9724 when Attribute_Rounding
=>
9726 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9732 when Attribute_Safe_Emax
=>
9733 Fold_Uint
(N
, Safe_Emax_Value
(P_Type
), Static
);
9739 when Attribute_Safe_First
=>
9740 Fold_Ureal
(N
, Safe_First_Value
(P_Type
), Static
);
9746 when Attribute_Safe_Large
=>
9747 if Is_Fixed_Point_Type
(P_Type
) then
9749 (N
, Expr_Value_R
(Type_High_Bound
(P_Base_Type
)), Static
);
9751 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
9758 when Attribute_Safe_Last
=>
9759 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
9765 when Attribute_Safe_Small
=>
9767 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9768 -- for fixed-point, since is the same as Small, but we implement
9769 -- it for backwards compatibility.
9771 if Is_Fixed_Point_Type
(P_Type
) then
9772 Fold_Ureal
(N
, Small_Value
(P_Type
), Static
);
9774 -- Ada 83 Safe_Small for floating-point cases
9777 Fold_Ureal
(N
, Model_Small_Value
(P_Type
), Static
);
9784 when Attribute_Scale
=>
9785 Fold_Uint
(N
, Scale_Value
(P_Type
), Static
);
9791 when Attribute_Scaling
=>
9795 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
9802 when Attribute_Signed_Zeros
=>
9804 (N
, UI_From_Int
(Boolean'Pos (Has_Signed_Zeros
(P_Type
))), Static
);
9810 -- Size attribute returns the RM size. All scalar types can be folded,
9811 -- as well as any types for which the size is known by the front end,
9812 -- including any type for which a size attribute is specified. This is
9813 -- one of the places where it is annoying that a size of zero means two
9814 -- things (zero size for scalars, unspecified size for non-scalars).
9817 | Attribute_VADS_Size
9820 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9823 if Is_Scalar_Type
(P_TypeA
)
9824 or else RM_Size
(P_TypeA
) /= Uint_0
9828 if Id
= Attribute_VADS_Size
or else Use_VADS_Size
then
9830 S
: constant Node_Id
:= Size_Clause
(P_TypeA
);
9833 -- If a size clause applies, then use the size from it.
9834 -- This is one of the rare cases where we can use the
9835 -- Size_Clause field for a subtype when Has_Size_Clause
9836 -- is False. Consider:
9838 -- type x is range 1 .. 64;
9839 -- for x'size use 12;
9840 -- subtype y is x range 0 .. 3;
9842 -- Here y has a size clause inherited from x, but
9843 -- normally it does not apply, and y'size is 2. However,
9844 -- y'VADS_Size is indeed 12 and not 2.
9847 and then Is_OK_Static_Expression
(Expression
(S
))
9849 Fold_Uint
(N
, Expr_Value
(Expression
(S
)), Static
);
9851 -- If no size is specified, then we simply use the object
9852 -- size in the VADS_Size case (e.g. Natural'Size is equal
9853 -- to Integer'Size, not one less).
9856 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9860 -- Normal case (Size) in which case we want the RM_Size
9863 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
9872 when Attribute_Small
=>
9874 -- The floating-point case is present only for Ada 83 compatibility.
9875 -- Note that strictly this is an illegal addition, since we are
9876 -- extending an Ada 95 defined attribute, but we anticipate an
9877 -- ARG ruling that will permit this.
9879 if Is_Floating_Point_Type
(P_Type
) then
9881 -- Ada 83 attribute is defined as (RM83 3.5.8)
9883 -- T'Small = 2.0**(-T'Emax - 1)
9887 -- T'Emax = 4 * T'Mantissa
9889 Fold_Ureal
(N
, Ureal_2
** ((-(4 * Mantissa
)) - 1), Static
);
9891 -- Normal Ada 95 fixed-point case
9894 Fold_Ureal
(N
, Small_Value
(P_Type
), True);
9901 when Attribute_Stream_Size
=>
9908 when Attribute_Succ
=>
9909 -- Floating-point case
9911 if Is_Floating_Point_Type
(P_Type
) then
9913 (N
, Eval_Fat
.Succ
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9917 elsif Is_Fixed_Point_Type
(P_Type
) then
9918 Fold_Ureal
(N
, Expr_Value_R
(E1
) + Small_Value
(P_Type
), Static
);
9920 -- Modular integer case (wraps)
9922 elsif Is_Modular_Integer_Type
(P_Type
) then
9923 Fold_Uint
(N
, (Expr_Value
(E1
) + 1) mod Modulus
(P_Type
), Static
);
9925 -- Other scalar cases
9928 pragma Assert
(Is_Scalar_Type
(P_Type
));
9930 if Is_Enumeration_Type
(P_Type
)
9931 and then Expr_Value
(E1
) =
9932 Expr_Value
(Type_High_Bound
(P_Base_Type
))
9934 Apply_Compile_Time_Constraint_Error
9935 (N
, "Succ of `&''Last`",
9936 CE_Overflow_Check_Failed
,
9938 Warn
=> not Static
);
9943 Fold_Uint
(N
, Expr_Value
(E1
) + 1, Static
);
9951 when Attribute_Truncation
=>
9954 Eval_Fat
.Truncation
(P_Base_Type
, Expr_Value_R
(E1
)),
9961 when Attribute_Type_Class
=> Type_Class
: declare
9962 Typ
: constant Entity_Id
:= Underlying_Type
(P_Base_Type
);
9966 if Is_Descendant_Of_Address
(Typ
) then
9967 Id
:= RE_Type_Class_Address
;
9969 elsif Is_Enumeration_Type
(Typ
) then
9970 Id
:= RE_Type_Class_Enumeration
;
9972 elsif Is_Integer_Type
(Typ
) then
9973 Id
:= RE_Type_Class_Integer
;
9975 elsif Is_Fixed_Point_Type
(Typ
) then
9976 Id
:= RE_Type_Class_Fixed_Point
;
9978 elsif Is_Floating_Point_Type
(Typ
) then
9979 Id
:= RE_Type_Class_Floating_Point
;
9981 elsif Is_Array_Type
(Typ
) then
9982 Id
:= RE_Type_Class_Array
;
9984 elsif Is_Record_Type
(Typ
) then
9985 Id
:= RE_Type_Class_Record
;
9987 elsif Is_Access_Type
(Typ
) then
9988 Id
:= RE_Type_Class_Access
;
9990 elsif Is_Task_Type
(Typ
) then
9991 Id
:= RE_Type_Class_Task
;
9993 -- We treat protected types like task types. It would make more
9994 -- sense to have another enumeration value, but after all the
9995 -- whole point of this feature is to be exactly DEC compatible,
9996 -- and changing the type Type_Class would not meet this requirement.
9998 elsif Is_Protected_Type
(Typ
) then
9999 Id
:= RE_Type_Class_Task
;
10001 -- Not clear if there are any other possibilities, but if there
10002 -- are, then we will treat them as the address case.
10005 Id
:= RE_Type_Class_Address
;
10008 Rewrite
(N
, New_Occurrence_Of
(RTE
(Id
), Loc
));
10011 -----------------------
10012 -- Unbiased_Rounding --
10013 -----------------------
10015 when Attribute_Unbiased_Rounding
=>
10018 Eval_Fat
.Unbiased_Rounding
(P_Base_Type
, Expr_Value_R
(E1
)),
10021 -------------------------
10022 -- Unconstrained_Array --
10023 -------------------------
10025 when Attribute_Unconstrained_Array
=> Unconstrained_Array
: declare
10026 Typ
: constant Entity_Id
:= Underlying_Type
(P_Type
);
10029 Rewrite
(N
, New_Occurrence_Of
(
10031 Is_Array_Type
(P_Type
)
10032 and then not Is_Constrained
(Typ
)), Loc
));
10034 -- Analyze and resolve as boolean, note that this attribute is
10035 -- a static attribute in GNAT.
10037 Analyze_And_Resolve
(N
, Standard_Boolean
);
10039 Set_Is_Static_Expression
(N
, True);
10040 end Unconstrained_Array
;
10042 -- Attribute Update is never static
10044 when Attribute_Update
=>
10051 -- Processing is shared with Size
10057 when Attribute_Val
=>
10058 if Expr_Value
(E1
) < Expr_Value
(Type_Low_Bound
(P_Base_Type
))
10060 Expr_Value
(E1
) > Expr_Value
(Type_High_Bound
(P_Base_Type
))
10062 Apply_Compile_Time_Constraint_Error
10063 (N
, "Val expression out of range",
10064 CE_Range_Check_Failed
,
10065 Warn
=> not Static
);
10071 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
10078 -- The Value_Size attribute for a type returns the RM size of the type.
10079 -- This an always be folded for scalar types, and can also be folded for
10080 -- non-scalar types if the size is set. This is one of the places where
10081 -- it is annoying that a size of zero means two things!
10083 when Attribute_Value_Size
=> Value_Size
: declare
10084 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
10087 if Is_Scalar_Type
(P_TypeA
) or else RM_Size
(P_TypeA
) /= Uint_0
then
10088 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
10096 -- Version can never be static
10098 when Attribute_Version
=>
10105 -- Wide_Image is a scalar attribute, but is never static, because it
10106 -- is not a static function (having a non-scalar argument (RM 4.9(22))
10108 when Attribute_Wide_Image
=>
10111 ---------------------
10112 -- Wide_Wide_Image --
10113 ---------------------
10115 -- Wide_Wide_Image is a scalar attribute but is never static, because it
10116 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
10118 when Attribute_Wide_Wide_Image
=>
10121 ---------------------
10122 -- Wide_Wide_Width --
10123 ---------------------
10125 -- Processing for Wide_Wide_Width is combined with Width
10131 -- Processing for Wide_Width is combined with Width
10137 -- This processing also handles the case of Wide_[Wide_]Width
10139 when Attribute_Width
10140 | Attribute_Wide_Width
10141 | Attribute_Wide_Wide_Width
10143 if Compile_Time_Known_Bounds
(P_Type
) then
10145 -- Floating-point types
10147 if Is_Floating_Point_Type
(P_Type
) then
10149 -- Width is zero for a null range (RM 3.5 (38))
10151 if Expr_Value_R
(Type_High_Bound
(P_Type
)) <
10152 Expr_Value_R
(Type_Low_Bound
(P_Type
))
10154 Fold_Uint
(N
, Uint_0
, Static
);
10157 -- For floating-point, we have +N.dddE+nnn where length
10158 -- of ddd is determined by type'Digits - 1, but is one
10159 -- if Digits is one (RM 3.5 (33)).
10161 -- nnn is set to 2 for Short_Float and Float (32 bit
10162 -- floats), and 3 for Long_Float and Long_Long_Float.
10163 -- For machines where Long_Long_Float is the IEEE
10164 -- extended precision type, the exponent takes 4 digits.
10168 Int
'Max (2, UI_To_Int
(Digits_Value
(P_Type
)));
10171 if Esize
(P_Type
) <= 32 then
10173 elsif Esize
(P_Type
) = 64 then
10179 Fold_Uint
(N
, UI_From_Int
(Len
), Static
);
10183 -- Fixed-point types
10185 elsif Is_Fixed_Point_Type
(P_Type
) then
10187 -- Width is zero for a null range (RM 3.5 (38))
10189 if Expr_Value
(Type_High_Bound
(P_Type
)) <
10190 Expr_Value
(Type_Low_Bound
(P_Type
))
10192 Fold_Uint
(N
, Uint_0
, Static
);
10194 -- The non-null case depends on the specific real type
10197 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
10200 (N
, UI_From_Int
(Fore_Value
+ 1) + Aft_Value
(P_Type
),
10208 R
: constant Entity_Id
:= Root_Type
(P_Type
);
10209 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
10210 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
10223 -- Width for types derived from Standard.Character
10224 -- and Standard.Wide_[Wide_]Character.
10226 elsif Is_Standard_Character_Type
(P_Type
) then
10229 -- Set W larger if needed
10231 for J
in UI_To_Int
(Lo
) .. UI_To_Int
(Hi
) loop
10233 -- All wide characters look like Hex_hhhhhhhh
10237 -- No need to compute this more than once
10242 C
:= Character'Val (J
);
10244 -- Test for all cases where Character'Image
10245 -- yields an image that is longer than three
10246 -- characters. First the cases of Reserved_xxx
10247 -- names (length = 12).
10324 when Space
.. Tilde
10325 | No_Break_Space
.. LC_Y_Diaeresis
10327 -- Special case of soft hyphen in Ada 2005
10329 if C
= Character'Val (16#AD#
)
10330 and then Ada_Version
>= Ada_2005
10338 W
:= Int
'Max (W
, Wt
);
10342 -- Width for types derived from Standard.Boolean
10344 elsif R
= Standard_Boolean
then
10351 -- Width for integer types
10353 elsif Is_Integer_Type
(P_Type
) then
10354 T
:= UI_Max
(abs Lo
, abs Hi
);
10362 -- User declared enum type with discard names
10364 elsif Discard_Names
(R
) then
10366 -- If range is null, result is zero, that has already
10367 -- been dealt with, so what we need is the power of ten
10368 -- that accommodates the Pos of the largest value, which
10369 -- is the high bound of the range + one for the space.
10378 -- Only remaining possibility is user declared enum type
10379 -- with normal case of Discard_Names not active.
10382 pragma Assert
(Is_Enumeration_Type
(P_Type
));
10385 L
:= First_Literal
(P_Type
);
10386 while Present
(L
) loop
10388 -- Only pay attention to in range characters
10390 if Lo
<= Enumeration_Pos
(L
)
10391 and then Enumeration_Pos
(L
) <= Hi
10393 -- For Width case, use decoded name
10395 if Id
= Attribute_Width
then
10396 Get_Decoded_Name_String
(Chars
(L
));
10397 Wt
:= Nat
(Name_Len
);
10399 -- For Wide_[Wide_]Width, use encoded name, and
10400 -- then adjust for the encoding.
10403 Get_Name_String
(Chars
(L
));
10405 -- Character literals are always of length 3
10407 if Name_Buffer
(1) = 'Q' then
10410 -- Otherwise loop to adjust for upper/wide chars
10413 Wt
:= Nat
(Name_Len
);
10415 for J
in 1 .. Name_Len
loop
10416 if Name_Buffer
(J
) = 'U' then
10418 elsif Name_Buffer
(J
) = 'W' then
10425 W
:= Int
'Max (W
, Wt
);
10432 Fold_Uint
(N
, UI_From_Int
(W
), Static
);
10437 -- The following attributes denote functions that cannot be folded
10439 when Attribute_From_Any
10441 | Attribute_TypeCode
10445 -- The following attributes can never be folded, and furthermore we
10446 -- should not even have entered the case statement for any of these.
10447 -- Note that in some cases, the values have already been folded as
10448 -- a result of the processing in Analyze_Attribute or earlier in
10451 when Attribute_Abort_Signal
10453 | Attribute_Address
10454 | Attribute_Address_Size
10455 | Attribute_Asm_Input
10456 | Attribute_Asm_Output
10458 | Attribute_Bit_Order
10459 | Attribute_Bit_Position
10460 | Attribute_Callable
10463 | Attribute_Code_Address
10464 | Attribute_Compiler_Version
10466 | Attribute_Default_Bit_Order
10467 | Attribute_Default_Scalar_Storage_Order
10469 | Attribute_Elaborated
10470 | Attribute_Elab_Body
10471 | Attribute_Elab_Spec
10472 | Attribute_Elab_Subp_Body
10473 | Attribute_Enabled
10474 | Attribute_External_Tag
10475 | Attribute_Fast_Math
10476 | Attribute_First_Bit
10479 | Attribute_Initialized
10480 | Attribute_Last_Bit
10481 | Attribute_Library_Level
10482 | Attribute_Max_Integer_Size
10483 | Attribute_Maximum_Alignment
10486 | Attribute_Partition_ID
10487 | Attribute_Pool_Address
10488 | Attribute_Position
10489 | Attribute_Priority
10490 | Attribute_Put_Image
10493 | Attribute_Scalar_Storage_Order
10494 | Attribute_Simple_Storage_Pool
10495 | Attribute_Storage_Pool
10496 | Attribute_Storage_Size
10497 | Attribute_Storage_Unit
10498 | Attribute_Stub_Type
10499 | Attribute_System_Allocator_Alignment
10501 | Attribute_Target_Name
10502 | Attribute_Terminated
10503 | Attribute_To_Address
10504 | Attribute_Type_Key
10505 | Attribute_Unchecked_Access
10506 | Attribute_Universal_Literal_String
10507 | Attribute_Unrestricted_Access
10509 | Attribute_Valid_Scalars
10511 | Attribute_Wchar_T_Size
10512 | Attribute_Wide_Value
10513 | Attribute_Wide_Wide_Value
10514 | Attribute_Word_Size
10517 raise Program_Error
;
10520 -- At the end of the case, one more check. If we did a static evaluation
10521 -- so that the result is now a literal, then set Is_Static_Expression
10522 -- in the constant only if the prefix type is a static subtype. For
10523 -- non-static subtypes, the folding is still OK, but not static.
10525 -- An exception is the GNAT attribute Constrained_Array which is
10526 -- defined to be a static attribute in all cases.
10528 if Nkind
(N
) in N_Integer_Literal
10530 | N_Character_Literal
10532 or else (Is_Entity_Name
(N
)
10533 and then Ekind
(Entity
(N
)) = E_Enumeration_Literal
)
10535 Set_Is_Static_Expression
(N
, Static
);
10537 -- If this is still an attribute reference, then it has not been folded
10538 -- and that means that its expressions are in a non-static context.
10540 elsif Nkind
(N
) = N_Attribute_Reference
then
10543 -- Note: the else case not covered here are odd cases where the
10544 -- processing has transformed the attribute into something other
10545 -- than a constant. Nothing more to do in such cases.
10550 end Eval_Attribute
;
10552 ------------------------------
10553 -- Is_Anonymous_Tagged_Base --
10554 ------------------------------
10556 function Is_Anonymous_Tagged_Base
10558 Typ
: Entity_Id
) return Boolean
10562 Anon
= Current_Scope
10563 and then Is_Itype
(Anon
)
10564 and then Associated_Node_For_Itype
(Anon
) = Parent
(Typ
);
10565 end Is_Anonymous_Tagged_Base
;
10567 --------------------------------
10568 -- Name_Implies_Lvalue_Prefix --
10569 --------------------------------
10571 function Name_Implies_Lvalue_Prefix
(Nam
: Name_Id
) return Boolean is
10572 pragma Assert
(Is_Attribute_Name
(Nam
));
10574 return Attribute_Name_Implies_Lvalue_Prefix
(Get_Attribute_Id
(Nam
));
10575 end Name_Implies_Lvalue_Prefix
;
10577 -----------------------
10578 -- Resolve_Attribute --
10579 -----------------------
10581 procedure Resolve_Attribute
(N
: Node_Id
; Typ
: Entity_Id
) is
10582 Loc
: constant Source_Ptr
:= Sloc
(N
);
10583 P
: constant Node_Id
:= Prefix
(N
);
10584 Aname
: constant Name_Id
:= Attribute_Name
(N
);
10585 Attr_Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
10586 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
10587 Des_Btyp
: Entity_Id
;
10588 Index
: Interp_Index
;
10590 Nom_Subt
: Entity_Id
;
10592 procedure Accessibility_Message
;
10593 -- Error, or warning within an instance, if the static accessibility
10594 -- rules of 3.10.2 are violated.
10596 function Declared_Within_Generic_Unit
10597 (Entity
: Entity_Id
;
10598 Generic_Unit
: Node_Id
) return Boolean;
10599 -- Returns True if Declared_Entity is declared within the declarative
10600 -- region of Generic_Unit; otherwise returns False.
10602 function Prefix_With_Safe_Accessibility_Level
return Boolean;
10603 -- Return True if the prefix does not have a value conversion of an
10604 -- array because a value conversion is like an aggregate with respect
10605 -- to determining accessibility level (RM 3.10.2); even if evaluation
10606 -- of a value conversion is guaranteed to not create a new object,
10607 -- accessibility rules are defined as if it might.
10609 ---------------------------
10610 -- Accessibility_Message --
10611 ---------------------------
10613 procedure Accessibility_Message
is
10614 Indic
: Node_Id
:= Parent
(Parent
(N
));
10617 -- In an instance, this is a runtime check, but one we
10618 -- know will fail, so generate an appropriate warning.
10620 if In_Instance_Body
then
10621 Error_Msg_Warn
:= SPARK_Mode
/= On
;
10623 ("non-local pointer cannot point to local object<<", P
);
10624 Error_Msg_F
("\Program_Error [<<", P
);
10626 Make_Raise_Program_Error
(Loc
,
10627 Reason
=> PE_Accessibility_Check_Failed
));
10628 Set_Etype
(N
, Typ
);
10632 Error_Msg_F
("non-local pointer cannot point to local object", P
);
10634 -- Check for case where we have a missing access definition
10636 if Is_Record_Type
(Current_Scope
)
10638 Nkind
(Parent
(N
)) in N_Discriminant_Association
10639 | N_Index_Or_Discriminant_Constraint
10641 Indic
:= Parent
(Parent
(N
));
10642 while Present
(Indic
)
10643 and then Nkind
(Indic
) /= N_Subtype_Indication
10645 Indic
:= Parent
(Indic
);
10648 if Present
(Indic
) then
10650 ("\use an access definition for" &
10651 " the access discriminant of&",
10652 N
, Entity
(Subtype_Mark
(Indic
)));
10656 end Accessibility_Message
;
10658 ----------------------------------
10659 -- Declared_Within_Generic_Unit --
10660 ----------------------------------
10662 function Declared_Within_Generic_Unit
10663 (Entity
: Entity_Id
;
10664 Generic_Unit
: Node_Id
) return Boolean
10666 Generic_Encloser
: Node_Id
:= Enclosing_Generic_Unit
(Entity
);
10669 while Present
(Generic_Encloser
) loop
10670 if Generic_Encloser
= Generic_Unit
then
10674 -- We have to step to the scope of the generic's entity, because
10675 -- otherwise we'll just get back the same generic.
10677 Generic_Encloser
:=
10678 Enclosing_Generic_Unit
10679 (Scope
(Defining_Entity
(Generic_Encloser
)));
10683 end Declared_Within_Generic_Unit
;
10685 ------------------------------------------
10686 -- Prefix_With_Safe_Accessibility_Level --
10687 ------------------------------------------
10689 function Prefix_With_Safe_Accessibility_Level
return Boolean is
10690 function Safe_Value_Conversions
return Boolean;
10691 -- Return False if the prefix has a value conversion of an array type
10693 ----------------------------
10694 -- Safe_Value_Conversions --
10695 ----------------------------
10697 function Safe_Value_Conversions
return Boolean is
10702 if Nkind
(PP
) in N_Selected_Component | N_Indexed_Component
then
10705 elsif Comes_From_Source
(PP
)
10706 and then Nkind
(PP
) in N_Type_Conversion
10707 | N_Unchecked_Type_Conversion
10708 and then Is_Array_Type
(Etype
(PP
))
10712 elsif Comes_From_Source
(PP
)
10713 and then Nkind
(PP
) = N_Qualified_Expression
10714 and then Is_Array_Type
(Etype
(PP
))
10715 and then Nkind
(Original_Node
(Expression
(PP
))) in
10716 N_Aggregate | N_Extension_Aggregate
10726 end Safe_Value_Conversions
;
10728 -- Start of processing for Prefix_With_Safe_Accessibility_Level
10731 -- No check required for unchecked and unrestricted access
10733 if Attr_Id
= Attribute_Unchecked_Access
10734 or else Attr_Id
= Attribute_Unrestricted_Access
10738 -- Check value conversions
10740 elsif Ekind
(Btyp
) = E_General_Access_Type
10741 and then not Safe_Value_Conversions
10747 end Prefix_With_Safe_Accessibility_Level
;
10749 -- Start of processing for Resolve_Attribute
10752 -- If error during analysis, no point in continuing, except for array
10753 -- types, where we get better recovery by using unconstrained indexes
10754 -- than nothing at all (see Check_Array_Type).
10756 if Error_Posted
(N
)
10757 and then Attr_Id
/= Attribute_First
10758 and then Attr_Id
/= Attribute_Last
10759 and then Attr_Id
/= Attribute_Length
10760 and then Attr_Id
/= Attribute_Range
10765 -- If attribute was universal type, reset to actual type
10767 if Etype
(N
) = Universal_Integer
10768 or else Etype
(N
) = Universal_Real
10770 Set_Etype
(N
, Typ
);
10773 -- Remaining processing depends on attribute
10781 -- For access attributes, if the prefix denotes an entity, it is
10782 -- interpreted as a name, never as a call. It may be overloaded,
10783 -- in which case resolution uses the profile of the context type.
10784 -- Otherwise prefix must be resolved.
10786 when Attribute_Access
10787 | Attribute_Unchecked_Access
10788 | Attribute_Unrestricted_Access
10790 -- Note possible modification if we have a variable
10792 if Is_Variable
(P
) then
10794 PN
: constant Node_Id
:= Parent
(N
);
10797 Note
: Boolean := True;
10798 -- Skip this for the case of Unrestricted_Access occuring in
10799 -- the context of a Valid check, since this otherwise leads
10800 -- to a missed warning (the Valid check does not really
10801 -- modify!) If this case, Note will be reset to False.
10803 -- Skip it as well if the type is an Access_To_Constant,
10804 -- given that no use of the value can modify the prefix.
10807 if Attr_Id
= Attribute_Unrestricted_Access
10808 and then Nkind
(PN
) = N_Function_Call
10812 if Nkind
(Nm
) = N_Expanded_Name
10813 and then Chars
(Nm
) = Name_Valid
10814 and then Nkind
(Prefix
(Nm
)) = N_Identifier
10815 and then Chars
(Prefix
(Nm
)) = Name_Attr_Long_Float
10820 elsif Is_Access_Constant
(Typ
) then
10825 Note_Possible_Modification
(P
, Sure
=> False);
10830 -- Case where prefix is an entity name
10832 if Is_Entity_Name
(P
) then
10834 -- Deal with case where prefix itself is overloaded
10836 if Is_Overloaded
(P
) then
10837 Get_First_Interp
(P
, Index
, It
);
10838 while Present
(It
.Nam
) loop
10839 if Type_Conformant
(Designated_Type
(Typ
), It
.Nam
) then
10840 Set_Entity
(P
, It
.Nam
);
10842 -- The prefix is definitely NOT overloaded anymore at
10843 -- this point, so we reset the Is_Overloaded flag to
10844 -- avoid any confusion when reanalyzing the node.
10846 Set_Is_Overloaded
(P
, False);
10847 Set_Is_Overloaded
(N
, False);
10848 Generate_Reference
(Entity
(P
), P
);
10852 Get_Next_Interp
(Index
, It
);
10855 -- If Prefix is a subprogram name, this reference freezes,
10856 -- but not if within spec expression mode. The profile of
10857 -- the subprogram is not frozen at this point.
10859 if not In_Spec_Expression
then
10860 Freeze_Before
(N
, Entity
(P
), Do_Freeze_Profile
=> False);
10863 -- If it is a type, there is nothing to resolve.
10864 -- If it is a subprogram, do not freeze its profile.
10865 -- If it is an object, complete its resolution.
10867 elsif Is_Overloadable
(Entity
(P
)) then
10868 if not In_Spec_Expression
then
10869 Freeze_Before
(N
, Entity
(P
), Do_Freeze_Profile
=> False);
10872 -- Nothing to do if prefix is a type name
10874 elsif Is_Type
(Entity
(P
)) then
10877 -- Otherwise non-overloaded other case, resolve the prefix
10883 -- Some further error checks
10885 Error_Msg_Name_1
:= Aname
;
10887 if not Is_Entity_Name
(P
) then
10890 elsif Is_Overloadable
(Entity
(P
))
10891 and then Is_Abstract_Subprogram
(Entity
(P
))
10893 Error_Msg_F
("prefix of % attribute cannot be abstract", P
);
10894 Set_Etype
(N
, Any_Type
);
10896 elsif Ekind
(Entity
(P
)) = E_Enumeration_Literal
then
10898 ("prefix of % attribute cannot be enumeration literal", P
);
10899 Set_Etype
(N
, Any_Type
);
10901 -- An attempt to take 'Access of a function that renames an
10902 -- enumeration literal. Issue a specialized error message.
10904 elsif Ekind
(Entity
(P
)) = E_Function
10905 and then Present
(Alias
(Entity
(P
)))
10906 and then Ekind
(Alias
(Entity
(P
))) = E_Enumeration_Literal
10909 ("prefix of % attribute cannot be function renaming "
10910 & "an enumeration literal", P
);
10911 Set_Etype
(N
, Any_Type
);
10913 elsif Convention
(Entity
(P
)) = Convention_Intrinsic
then
10914 Error_Msg_F
("prefix of % attribute cannot be intrinsic", P
);
10915 Set_Etype
(N
, Any_Type
);
10918 -- Assignments, return statements, components of aggregates,
10919 -- generic instantiations will require convention checks if
10920 -- the type is an access to subprogram. Given that there will
10921 -- also be accessibility checks on those, this is where the
10922 -- checks can eventually be centralized ???
10924 if Ekind
(Btyp
) in E_Access_Protected_Subprogram_Type
10925 | E_Access_Subprogram_Type
10926 | E_Anonymous_Access_Protected_Subprogram_Type
10927 | E_Anonymous_Access_Subprogram_Type
10929 -- Deal with convention mismatch
10931 if Convention
(Designated_Type
(Btyp
)) /=
10932 Convention
(Entity
(P
))
10934 -- The rule in 6.3.1 (8) deserves a special error
10937 if Convention
(Btyp
) = Convention_Intrinsic
10938 and then Nkind
(Parent
(N
)) = N_Procedure_Call_Statement
10939 and then Is_Entity_Name
(Name
(Parent
(N
)))
10940 and then Inside_A_Generic
10943 Subp
: constant Entity_Id
:=
10944 Entity
(Name
(Parent
(N
)));
10946 if Convention
(Subp
) = Convention_Intrinsic
then
10948 ("?subprogram and its formal access "
10949 & "parameters have convention Intrinsic",
10952 ("actual cannot be access attribute", N
);
10958 ("subprogram & has wrong convention", P
, Entity
(P
));
10959 Error_Msg_Sloc
:= Sloc
(Btyp
);
10960 Error_Msg_FE
("\does not match & declared#", P
, Btyp
);
10963 if not Is_Itype
(Btyp
)
10964 and then not Has_Convention_Pragma
(Btyp
)
10965 and then Convention
(Entity
(P
)) /= Convention_Intrinsic
10968 ("\probable missing pragma Convention for &",
10973 Check_Subtype_Conformant
10974 (New_Id
=> Entity
(P
),
10975 Old_Id
=> Designated_Type
(Btyp
),
10979 if Attr_Id
= Attribute_Unchecked_Access
then
10980 Error_Msg_Name_1
:= Aname
;
10982 ("attribute% cannot be applied to a subprogram", P
);
10984 elsif Aname
= Name_Unrestricted_Access
then
10985 null; -- Nothing to check
10987 -- Check the static accessibility rule of 3.10.2(32).
10988 -- This rule also applies within the private part of an
10989 -- instantiation. This rule does not apply to anonymous
10990 -- access-to-subprogram types in access parameters.
10992 elsif Attr_Id
= Attribute_Access
10993 and then not In_Instance_Body
10995 (Ekind
(Btyp
) = E_Access_Subprogram_Type
10996 or else Is_Local_Anonymous_Access
(Btyp
))
10997 and then Subprogram_Access_Level
(Entity
(P
)) >
10998 Type_Access_Level
(Btyp
)
11001 ("subprogram must not be deeper than access type", P
);
11003 -- Check the restriction of 3.10.2(32) that disallows the
11004 -- access attribute within a generic body when the ultimate
11005 -- ancestor of the type of the attribute is declared outside
11006 -- of the generic unit and the subprogram is declared within
11007 -- that generic unit. This includes any such attribute that
11008 -- occurs within the body of a generic unit that is a child
11009 -- of the generic unit where the subprogram is declared.
11011 -- The rule also prohibits applying the attribute when the
11012 -- access type is a generic formal access type (since the
11013 -- level of the actual type is not known). This restriction
11014 -- does not apply when the attribute type is an anonymous
11015 -- access-to-subprogram type. Note that this check was
11016 -- revised by AI-229, because the original Ada 95 rule
11017 -- was too lax. The original rule only applied when the
11018 -- subprogram was declared within the body of the generic,
11019 -- which allowed the possibility of dangling references).
11020 -- The rule was also too strict in some cases, in that it
11021 -- didn't permit the access to be declared in the generic
11022 -- spec, whereas the revised rule does (as long as it's not
11025 -- There are a couple of subtleties of the test for applying
11026 -- the check that are worth noting. First, we only apply it
11027 -- when the levels of the subprogram and access type are the
11028 -- same (the case where the subprogram is statically deeper
11029 -- was applied above, and the case where the type is deeper
11030 -- is always safe). Second, we want the check to apply
11031 -- within nested generic bodies and generic child unit
11032 -- bodies, but not to apply to an attribute that appears in
11033 -- the generic unit's specification. This is done by testing
11034 -- that the attribute's innermost enclosing generic body is
11035 -- not the same as the innermost generic body enclosing the
11036 -- generic unit where the subprogram is declared (we don't
11037 -- want the check to apply when the access attribute is in
11038 -- the spec and there's some other generic body enclosing
11039 -- generic). Finally, there's no point applying the check
11040 -- when within an instance, because any violations will have
11041 -- been caught by the compilation of the generic unit.
11043 -- We relax this check in Relaxed_RM_Semantics mode for
11044 -- compatibility with legacy code for use by Ada source
11045 -- code analyzers (e.g. CodePeer).
11047 elsif Attr_Id
= Attribute_Access
11048 and then not Relaxed_RM_Semantics
11049 and then not In_Instance
11050 and then Present
(Enclosing_Generic_Unit
(Entity
(P
)))
11051 and then Present
(Enclosing_Generic_Body
(N
))
11052 and then Enclosing_Generic_Body
(N
) /=
11053 Enclosing_Generic_Body
11054 (Enclosing_Generic_Unit
(Entity
(P
)))
11055 and then Subprogram_Access_Level
(Entity
(P
)) =
11056 Type_Access_Level
(Btyp
)
11057 and then Ekind
(Btyp
) /=
11058 E_Anonymous_Access_Subprogram_Type
11059 and then Ekind
(Btyp
) /=
11060 E_Anonymous_Access_Protected_Subprogram_Type
11062 -- The attribute type's ultimate ancestor must be
11063 -- declared within the same generic unit as the
11064 -- subprogram is declared (including within another
11065 -- nested generic unit). The error message is
11066 -- specialized to say "ancestor" for the case where the
11067 -- access type is not its own ancestor, since saying
11068 -- simply "access type" would be very confusing.
11070 if not Declared_Within_Generic_Unit
11072 Enclosing_Generic_Unit
(Entity
(P
)))
11075 ("''Access attribute not allowed in generic body",
11078 if Root_Type
(Btyp
) = Btyp
then
11081 "access type & is declared outside " &
11082 "generic unit (RM 3.10.2(32))", N
, Btyp
);
11085 ("\because ancestor of " &
11086 "access type & is declared outside " &
11087 "generic unit (RM 3.10.2(32))", N
, Btyp
);
11091 ("\move ''Access to private part, or " &
11092 "(Ada 2005) use anonymous access type instead of &",
11095 -- If the ultimate ancestor of the attribute's type is
11096 -- a formal type, then the attribute is illegal because
11097 -- the actual type might be declared at a higher level.
11098 -- The error message is specialized to say "ancestor"
11099 -- for the case where the access type is not its own
11100 -- ancestor, since saying simply "access type" would be
11103 elsif Is_Generic_Type
(Root_Type
(Btyp
)) then
11104 if Root_Type
(Btyp
) = Btyp
then
11106 ("access type must not be a generic formal type",
11110 ("ancestor access type must not be a generic " &
11117 -- If this is a renaming, an inherited operation, or a
11118 -- subprogram instance, use the original entity. This may make
11119 -- the node type-inconsistent, so this transformation can only
11120 -- be done if the node will not be reanalyzed. In particular,
11121 -- if it is within a default expression, the transformation
11122 -- must be delayed until the default subprogram is created for
11123 -- it, when the enclosing subprogram is frozen.
11125 if Is_Entity_Name
(P
)
11126 and then Is_Overloadable
(Entity
(P
))
11127 and then Present
(Alias
(Entity
(P
)))
11128 and then Expander_Active
11131 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
11134 elsif Nkind
(P
) = N_Selected_Component
11135 and then Is_Overloadable
(Entity
(Selector_Name
(P
)))
11137 -- Protected operation. If operation is overloaded, must
11138 -- disambiguate. Prefix that denotes protected object itself
11139 -- is resolved with its own type.
11141 if Attr_Id
= Attribute_Unchecked_Access
then
11142 Error_Msg_Name_1
:= Aname
;
11144 ("attribute% cannot be applied to protected operation", P
);
11147 Resolve
(Prefix
(P
));
11149 if not Is_Overloaded
(P
) then
11150 Generate_Reference
(Entity
(Selector_Name
(P
)), P
);
11153 Get_First_Interp
(P
, Index
, It
);
11154 while Present
(It
.Nam
) loop
11155 if Type_Conformant
(Designated_Type
(Typ
), It
.Nam
) then
11156 Set_Entity
(Selector_Name
(P
), It
.Nam
);
11158 -- The prefix is definitely NOT overloaded anymore at
11159 -- this point, so we reset the Is_Overloaded flag to
11160 -- avoid any confusion when reanalyzing the node.
11162 Set_Is_Overloaded
(P
, False);
11163 Set_Is_Overloaded
(N
, False);
11164 Generate_Reference
(Entity
(Selector_Name
(P
)), P
);
11168 Get_Next_Interp
(Index
, It
);
11172 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
11173 -- statically illegal if F is an anonymous access to subprogram.
11175 elsif Nkind
(P
) = N_Explicit_Dereference
11176 and then Is_Entity_Name
(Prefix
(P
))
11177 and then Ekind
(Etype
(Entity
(Prefix
(P
)))) =
11178 E_Anonymous_Access_Subprogram_Type
11180 Error_Msg_N
("anonymous access to subprogram "
11181 & "has deeper accessibility than any master", P
);
11183 elsif Is_Overloaded
(P
) then
11185 -- Use the designated type of the context to disambiguate
11186 -- Note that this was not strictly conformant to Ada 95,
11187 -- but was the implementation adopted by most Ada 95 compilers.
11188 -- The use of the context type to resolve an Access attribute
11189 -- reference is now mandated in AI-235 for Ada 2005.
11192 Index
: Interp_Index
;
11196 Get_First_Interp
(P
, Index
, It
);
11197 while Present
(It
.Typ
) loop
11198 if Covers
(Designated_Type
(Typ
), It
.Typ
) then
11199 Resolve
(P
, It
.Typ
);
11203 Get_Next_Interp
(Index
, It
);
11210 -- X'Access is illegal if X denotes a constant and the access type
11211 -- is access-to-variable. Same for 'Unchecked_Access. The rule
11212 -- does not apply to 'Unrestricted_Access. If the reference is a
11213 -- default-initialized aggregate component for a self-referential
11214 -- type the reference is legal.
11216 if not (Ekind
(Btyp
) = E_Access_Subprogram_Type
11217 or else Ekind
(Btyp
) = E_Anonymous_Access_Subprogram_Type
11218 or else (Is_Record_Type
(Btyp
)
11220 Present
(Corresponding_Remote_Type
(Btyp
)))
11221 or else Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
11222 or else Ekind
(Btyp
)
11223 = E_Anonymous_Access_Protected_Subprogram_Type
11224 or else Is_Access_Constant
(Btyp
)
11225 or else Is_Variable
(P
)
11226 or else Attr_Id
= Attribute_Unrestricted_Access
)
11228 if Is_Entity_Name
(P
)
11229 and then Is_Type
(Entity
(P
))
11231 -- Legality of a self-reference through an access
11232 -- attribute has been verified in Analyze_Access_Attribute.
11236 elsif Comes_From_Source
(N
) then
11237 Error_Msg_F
("access-to-variable designates constant", P
);
11241 Des_Btyp
:= Designated_Type
(Btyp
);
11243 if Ada_Version
>= Ada_2005
11244 and then Is_Incomplete_Type
(Des_Btyp
)
11246 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
11247 -- imported entity, and the non-limited view is visible, make
11248 -- use of it. If it is an incomplete subtype, use the base type
11251 if From_Limited_With
(Des_Btyp
)
11252 and then Present
(Non_Limited_View
(Des_Btyp
))
11254 Des_Btyp
:= Non_Limited_View
(Des_Btyp
);
11256 elsif Ekind
(Des_Btyp
) = E_Incomplete_Subtype
then
11257 Des_Btyp
:= Etype
(Des_Btyp
);
11261 if (Attr_Id
= Attribute_Access
11263 Attr_Id
= Attribute_Unchecked_Access
)
11264 and then (Ekind
(Btyp
) = E_General_Access_Type
11265 or else Ekind
(Btyp
) = E_Anonymous_Access_Type
)
11267 -- Ada 2005 (AI-230): Check the accessibility of anonymous
11268 -- access types for stand-alone objects, record and array
11269 -- components, and return objects. For a component definition
11270 -- the level is the same of the enclosing composite type.
11272 if Ada_Version
>= Ada_2005
11273 and then (Is_Local_Anonymous_Access
(Btyp
)
11275 -- Handle cases where Btyp is the anonymous access
11276 -- type of an Ada 2012 stand-alone object.
11278 or else Nkind
(Associated_Node_For_Itype
(Btyp
)) =
11279 N_Object_Declaration
)
11280 and then Attr_Id
= Attribute_Access
11282 -- Verify that static checking is OK (namely that we aren't
11283 -- in a specific context requiring dynamic checks on
11284 -- expicitly aliased parameters), and then check the level.
11286 -- Otherwise a check will be generated later when the return
11287 -- statement gets expanded.
11289 and then not Is_Special_Aliased_Formal_Access
(N
)
11291 Static_Accessibility_Level
(N
, Zero_On_Dynamic_Level
) >
11292 Deepest_Type_Access_Level
(Btyp
)
11294 -- In an instance, this is a runtime check, but one we know
11295 -- will fail, so generate an appropriate warning. As usual,
11296 -- this kind of warning is an error in SPARK mode.
11298 if In_Instance_Body
then
11299 Error_Msg_Warn
:= SPARK_Mode
/= On
;
11301 ("non-local pointer cannot point to local object<<", P
);
11302 Error_Msg_F
("\Program_Error [<<", P
);
11305 Make_Raise_Program_Error
(Loc
,
11306 Reason
=> PE_Accessibility_Check_Failed
));
11307 Set_Etype
(N
, Typ
);
11311 ("non-local pointer cannot point to local object", P
);
11315 if Is_Dependent_Component_Of_Mutable_Object
(P
) then
11317 ("illegal attribute for discriminant-dependent component",
11321 -- Check static matching rule of 3.10.2(27). Nominal subtype
11322 -- of the prefix must statically match the designated type.
11324 Nom_Subt
:= Etype
(P
);
11326 if Is_Constr_Subt_For_U_Nominal
(Nom_Subt
) then
11327 Nom_Subt
:= Base_Type
(Nom_Subt
);
11330 if Is_Tagged_Type
(Designated_Type
(Typ
)) then
11332 -- If the attribute is in the context of an access
11333 -- parameter, then the prefix is allowed to be of
11334 -- the class-wide type (by AI-127).
11336 if Ekind
(Typ
) = E_Anonymous_Access_Type
then
11337 if not Covers
(Designated_Type
(Typ
), Nom_Subt
)
11338 and then not Covers
(Nom_Subt
, Designated_Type
(Typ
))
11344 Desig
:= Designated_Type
(Typ
);
11346 if Is_Class_Wide_Type
(Desig
) then
11347 Desig
:= Etype
(Desig
);
11350 if Is_Anonymous_Tagged_Base
(Nom_Subt
, Desig
) then
11355 ("type of prefix: & not compatible",
11358 ("\with &, the expected designated type",
11359 P
, Designated_Type
(Typ
));
11364 elsif not Covers
(Designated_Type
(Typ
), Nom_Subt
)
11366 (not Is_Class_Wide_Type
(Designated_Type
(Typ
))
11367 and then Is_Class_Wide_Type
(Nom_Subt
))
11370 ("type of prefix: & is not covered", P
, Nom_Subt
);
11372 ("\by &, the expected designated type" &
11373 " (RM 3.10.2 (27))", P
, Designated_Type
(Typ
));
11376 if Is_Class_Wide_Type
(Designated_Type
(Typ
))
11377 and then Has_Discriminants
(Etype
(Designated_Type
(Typ
)))
11378 and then Is_Constrained
(Etype
(Designated_Type
(Typ
)))
11379 and then Designated_Type
(Typ
) /= Nom_Subt
11381 Apply_Discriminant_Check
11382 (N
, Etype
(Designated_Type
(Typ
)));
11385 -- Ada 2005 (AI-363): Require static matching when designated
11386 -- type has discriminants and a constrained partial view, since
11387 -- in general objects of such types are mutable, so we can't
11388 -- allow the access value to designate a constrained object
11389 -- (because access values must be assumed to designate mutable
11390 -- objects when designated type does not impose a constraint).
11392 elsif Subtypes_Statically_Match
(Des_Btyp
, Nom_Subt
) then
11395 elsif Has_Discriminants
(Designated_Type
(Typ
))
11396 and then not Is_Constrained
(Des_Btyp
)
11398 (Ada_Version
< Ada_2005
11400 not Object_Type_Has_Constrained_Partial_View
11401 (Typ
=> Designated_Type
(Base_Type
(Typ
)),
11402 Scop
=> Current_Scope
))
11408 ("object subtype must statically match "
11409 & "designated subtype", P
);
11411 if Is_Entity_Name
(P
)
11412 and then Is_Array_Type
(Designated_Type
(Typ
))
11415 D
: constant Node_Id
:= Declaration_Node
(Entity
(P
));
11418 ("aliased object has explicit bounds??", D
);
11420 ("\declare without bounds (and with explicit "
11421 & "initialization)??", D
);
11423 ("\for use with unconstrained access??", D
);
11428 -- Check the static accessibility rule of 3.10.2(28). Note that
11429 -- this check is not performed for the case of an anonymous
11430 -- access type, since the access attribute is always legal
11431 -- in such a context.
11433 if Attr_Id
/= Attribute_Unchecked_Access
11434 and then Ekind
(Btyp
) = E_General_Access_Type
11436 -- Call Accessibility_Level directly to avoid returning zero
11437 -- on cases where the prefix is an explicitly aliased
11438 -- parameter in a return statement, instead of using the
11439 -- normal Static_Accessibility_Level function.
11441 -- Shouldn't this be handled somehow in
11442 -- Static_Accessibility_Level ???
11444 and then Nkind
(Accessibility_Level
(P
, Dynamic_Level
))
11445 = N_Integer_Literal
11447 Intval
(Accessibility_Level
(P
, Dynamic_Level
))
11448 > Deepest_Type_Access_Level
(Btyp
)
11450 Accessibility_Message
;
11455 if Ekind
(Btyp
) in E_Access_Protected_Subprogram_Type
11456 | E_Anonymous_Access_Protected_Subprogram_Type
11458 if Is_Entity_Name
(P
)
11459 and then not Is_Protected_Type
(Scope
(Entity
(P
)))
11461 Error_Msg_F
("context requires a protected subprogram", P
);
11463 -- Check accessibility of protected object against that of the
11464 -- access type, but only on user code, because the expander
11465 -- creates access references for handlers. If the context is an
11466 -- anonymous_access_to_protected, there are no accessibility
11467 -- checks either. Omit check entirely for Unrestricted_Access.
11469 elsif Static_Accessibility_Level
(P
, Zero_On_Dynamic_Level
)
11470 > Deepest_Type_Access_Level
(Btyp
)
11471 and then Comes_From_Source
(N
)
11472 and then Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
11473 and then Attr_Id
/= Attribute_Unrestricted_Access
11475 Accessibility_Message
;
11478 -- AI05-0225: If the context is not an access to protected
11479 -- function, the prefix must be a variable, given that it may
11480 -- be used subsequently in a protected call.
11482 elsif Nkind
(P
) = N_Selected_Component
11483 and then not Is_Variable
(Prefix
(P
))
11484 and then Ekind
(Entity
(Selector_Name
(P
))) /= E_Function
11487 ("target object of access to protected procedure "
11488 & "must be variable", N
);
11490 elsif Is_Entity_Name
(P
) then
11491 Check_Internal_Protected_Use
(N
, Entity
(P
));
11494 elsif Ekind
(Btyp
) in E_Access_Subprogram_Type
11495 | E_Anonymous_Access_Subprogram_Type
11496 and then Ekind
(Etype
(N
)) = E_Access_Protected_Subprogram_Type
11498 Error_Msg_F
("context requires a non-protected subprogram", P
);
11501 -- The context cannot be a pool-specific type, but this is a
11502 -- legality rule, not a resolution rule, so it must be checked
11503 -- separately, after possibly disambiguation (see AI-245).
11505 if Ekind
(Btyp
) = E_Access_Type
11506 and then Attr_Id
/= Attribute_Unrestricted_Access
11508 Wrong_Type
(N
, Typ
);
11511 -- The context may be a constrained access type (however ill-
11512 -- advised such subtypes might be) so in order to generate a
11513 -- constraint check we need to set the type of the attribute
11514 -- reference to the base type of the context.
11516 Set_Etype
(N
, Btyp
);
11518 -- Check for incorrect atomic/volatile/VFA reference (RM C.6(12))
11520 if Attr_Id
/= Attribute_Unrestricted_Access
then
11521 if Is_Atomic_Object
(P
)
11522 and then not Is_Atomic
(Designated_Type
(Typ
))
11525 ("access to atomic object cannot yield access-to-" &
11526 "non-atomic type", P
);
11528 elsif Is_Volatile_Object
(P
)
11529 and then not Is_Volatile
(Designated_Type
(Typ
))
11532 ("access to volatile object cannot yield access-to-" &
11533 "non-volatile type", P
);
11535 elsif Is_Volatile_Full_Access_Object
(P
)
11536 and then not Is_Volatile_Full_Access
(Designated_Type
(Typ
))
11539 ("access to full access object cannot yield access-to-" &
11540 "non-full-access type", P
);
11543 -- Check for nonatomic subcomponent of a full access object
11544 -- in Ada 2020 (RM C.6 (12)).
11546 if Ada_Version
>= Ada_2020
11547 and then Is_Subcomponent_Of_Full_Access_Object
(P
)
11548 and then not Is_Atomic_Object
(P
)
11551 ("cannot have access attribute with prefix &", N
, P
);
11553 ("\nonatomic subcomponent of full access object "
11554 & "(RM C.6(12))", N
);
11558 -- Check for aliased view. We allow a nonaliased prefix when in
11559 -- an instance because the prefix may have been a tagged formal
11560 -- object, which is defined to be aliased even when the actual
11561 -- might not be (other instance cases will have been caught in
11562 -- the generic). Similarly, within an inlined body we know that
11563 -- the attribute is legal in the original subprogram, therefore
11564 -- legal in the expansion.
11566 if not (Is_Entity_Name
(P
)
11567 and then Is_Overloadable
(Entity
(P
)))
11568 and then not (Nkind
(P
) = N_Selected_Component
11570 Is_Overloadable
(Entity
(Selector_Name
(P
))))
11571 and then not Is_Aliased_View
(Original_Node
(P
))
11572 and then not In_Instance
11573 and then not In_Inlined_Body
11574 and then Comes_From_Source
(N
)
11576 -- Here we have a non-aliased view. This is illegal unless we
11577 -- have the case of Unrestricted_Access, where for now we allow
11578 -- this (we will reject later if expected type is access to an
11579 -- unconstrained array with a thin pointer).
11581 -- No need for an error message on a generated access reference
11582 -- for the controlling argument in a dispatching call: error
11583 -- will be reported when resolving the call.
11585 if Attr_Id
/= Attribute_Unrestricted_Access
then
11586 Error_Msg_Name_1
:= Aname
;
11587 Error_Msg_N
("prefix of % attribute must be aliased", P
);
11589 -- Check for unrestricted access where expected type is a thin
11590 -- pointer to an unconstrained array.
11592 elsif Has_Size_Clause
(Typ
)
11593 and then RM_Size
(Typ
) = System_Address_Size
11596 DT
: constant Entity_Id
:= Designated_Type
(Typ
);
11598 if Is_Array_Type
(DT
)
11599 and then not Is_Constrained
(DT
)
11602 ("illegal use of Unrestricted_Access attribute", P
);
11604 ("\attempt to generate thin pointer to unaliased "
11611 -- Check that the prefix does not have a value conversion of an
11612 -- array type since a value conversion is like an aggregate with
11613 -- respect to determining accessibility level (RM 3.10.2).
11615 if not Prefix_With_Safe_Accessibility_Level
then
11616 Accessibility_Message
;
11620 -- Mark that address of entity is taken in case of
11621 -- 'Unrestricted_Access or in case of a subprogram.
11623 if Is_Entity_Name
(P
)
11624 and then (Attr_Id
= Attribute_Unrestricted_Access
11625 or else Is_Subprogram
(Entity
(P
)))
11627 Set_Address_Taken
(Entity
(P
));
11630 -- Deal with possible elaboration check
11632 if Is_Entity_Name
(P
) and then Is_Subprogram
(Entity
(P
)) then
11634 Subp_Id
: constant Entity_Id
:= Entity
(P
);
11635 Scop
: constant Entity_Id
:= Scope
(Subp_Id
);
11636 Subp_Decl
: constant Node_Id
:=
11637 Unit_Declaration_Node
(Subp_Id
);
11638 Flag_Id
: Entity_Id
;
11639 Subp_Body
: Node_Id
;
11641 -- If the access has been taken and the body of the subprogram
11642 -- has not been see yet, indirect calls must be protected with
11643 -- elaboration checks. We have the proper elaboration machinery
11644 -- for subprograms declared in packages, but within a block or
11645 -- a subprogram the body will appear in the same declarative
11646 -- part, and we must insert a check in the eventual body itself
11647 -- using the elaboration flag that we generate now. The check
11648 -- is then inserted when the body is expanded. This processing
11649 -- is not needed for a stand alone expression function because
11650 -- the internally generated spec and body are always inserted
11651 -- as a pair in the same declarative list.
11655 and then Comes_From_Source
(Subp_Id
)
11656 and then Comes_From_Source
(N
)
11657 and then In_Open_Scopes
(Scop
)
11658 and then Ekind
(Scop
) in E_Block | E_Procedure | E_Function
11659 and then not Has_Completion
(Subp_Id
)
11660 and then No
(Elaboration_Entity
(Subp_Id
))
11661 and then Nkind
(Subp_Decl
) = N_Subprogram_Declaration
11662 and then Nkind
(Original_Node
(Subp_Decl
)) /=
11663 N_Expression_Function
11665 -- Create elaboration variable for it
11667 Flag_Id
:= Make_Temporary
(Loc
, 'E');
11668 Set_Elaboration_Entity
(Subp_Id
, Flag_Id
);
11669 Set_Is_Frozen
(Flag_Id
);
11671 -- Insert declaration for flag after subprogram
11672 -- declaration. Note that attribute reference may
11673 -- appear within a nested scope.
11675 Insert_After_And_Analyze
(Subp_Decl
,
11676 Make_Object_Declaration
(Loc
,
11677 Defining_Identifier
=> Flag_Id
,
11678 Object_Definition
=>
11679 New_Occurrence_Of
(Standard_Short_Integer
, Loc
),
11681 Make_Integer_Literal
(Loc
, Uint_0
)));
11683 -- The above sets the Scope of the flag entity to the
11684 -- current scope, in which the attribute appears, but
11685 -- the flag declaration has been inserted after that
11686 -- of Subp_Id, so the scope of the flag is the same as
11687 -- that of Subp_Id. This is relevant when unnesting,
11688 -- where processing depends on correct scope setting.
11690 Set_Scope
(Flag_Id
, Scop
);
11693 -- Taking the 'Access of an expression function freezes its
11694 -- expression (RM 13.14 10.3/3). This does not apply to an
11695 -- expression function that acts as a completion because the
11696 -- generated body is immediately analyzed and the expression
11697 -- is automatically frozen.
11699 if Is_Expression_Function
(Subp_Id
)
11700 and then Present
(Corresponding_Body
(Subp_Decl
))
11703 Unit_Declaration_Node
(Corresponding_Body
(Subp_Decl
));
11705 -- The body has already been analyzed when the expression
11706 -- function acts as a completion.
11708 if Analyzed
(Subp_Body
) then
11711 -- Attribute 'Access may appear within the generated body
11712 -- of the expression function subject to the attribute:
11714 -- function F is (... F'Access ...);
11716 -- If the expression function is on the scope stack, then
11717 -- the body is currently being analyzed. Do not reanalyze
11718 -- it because this will lead to infinite recursion.
11720 elsif In_Open_Scopes
(Subp_Id
) then
11723 -- If reference to the expression function appears in an
11724 -- inner scope, for example as an actual in an instance,
11725 -- this is not a freeze point either.
11727 elsif Scope
(Subp_Id
) /= Current_Scope
then
11730 -- Analyze the body of the expression function to freeze
11731 -- the expression. This takes care of the case where the
11732 -- 'Access is part of dispatch table initialization and
11733 -- the generated body of the expression function has not
11734 -- been analyzed yet.
11737 Analyze
(Subp_Body
);
11747 -- Deal with resolving the type for Address attribute, overloading
11748 -- is not permitted here, since there is no context to resolve it.
11750 when Attribute_Address
11751 | Attribute_Code_Address
11753 -- To be safe, assume that if the address of a variable is taken,
11754 -- it may be modified via this address, so note modification.
11756 if Is_Variable
(P
) then
11757 Note_Possible_Modification
(P
, Sure
=> False);
11760 if Nkind
(P
) in N_Subexpr
11761 and then Is_Overloaded
(P
)
11763 Get_First_Interp
(P
, Index
, It
);
11764 Get_Next_Interp
(Index
, It
);
11766 if Present
(It
.Nam
) then
11767 Error_Msg_Name_1
:= Aname
;
11769 ("prefix of % attribute cannot be overloaded", P
);
11773 if not Is_Entity_Name
(P
)
11774 or else not Is_Overloadable
(Entity
(P
))
11776 if not Is_Task_Type
(Etype
(P
))
11777 or else Nkind
(P
) = N_Explicit_Dereference
11783 -- If this is the name of a derived subprogram, or that of a
11784 -- generic actual, the address is that of the original entity.
11786 if Is_Entity_Name
(P
)
11787 and then Is_Overloadable
(Entity
(P
))
11788 and then Present
(Alias
(Entity
(P
)))
11791 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
11794 if Is_Entity_Name
(P
) then
11795 Set_Address_Taken
(Entity
(P
));
11798 if Nkind
(P
) = N_Slice
then
11800 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11801 -- even if the array is packed and the slice itself is not
11802 -- addressable. Transform the prefix into an indexed component.
11804 -- Note that the transformation is safe only if we know that
11805 -- the slice is non-null. That is because a null slice can have
11806 -- an out of bounds index value.
11808 -- Right now, gigi blows up if given 'Address on a slice as a
11809 -- result of some incorrect freeze nodes generated by the front
11810 -- end, and this covers up that bug in one case, but the bug is
11811 -- likely still there in the cases not handled by this code ???
11813 -- It's not clear what 'Address *should* return for a null
11814 -- slice with out of bounds indexes, this might be worth an ARG
11817 -- One approach would be to do a length check unconditionally,
11818 -- and then do the transformation below unconditionally, but
11819 -- analyze with checks off, avoiding the problem of the out of
11820 -- bounds index. This approach would interpret the address of
11821 -- an out of bounds null slice as being the address where the
11822 -- array element would be if there was one, which is probably
11823 -- as reasonable an interpretation as any ???
11826 Loc
: constant Source_Ptr
:= Sloc
(P
);
11827 D
: constant Node_Id
:= Discrete_Range
(P
);
11831 if Is_Entity_Name
(D
)
11834 (Type_Low_Bound
(Entity
(D
)),
11835 Type_High_Bound
(Entity
(D
)))
11838 Make_Attribute_Reference
(Loc
,
11839 Prefix
=> (New_Occurrence_Of
(Entity
(D
), Loc
)),
11840 Attribute_Name
=> Name_First
);
11842 elsif Nkind
(D
) = N_Range
11843 and then Not_Null_Range
(Low_Bound
(D
), High_Bound
(D
))
11845 Lo
:= Low_Bound
(D
);
11851 if Present
(Lo
) then
11853 Make_Indexed_Component
(Loc
,
11854 Prefix
=> Relocate_Node
(Prefix
(P
)),
11855 Expressions
=> New_List
(Lo
)));
11857 Analyze_And_Resolve
(P
);
11866 -- Prefix of Body_Version attribute can be a subprogram name which
11867 -- must not be resolved, since this is not a call.
11869 when Attribute_Body_Version
=>
11876 -- Prefix of Caller attribute is an entry name which must not
11877 -- be resolved, since this is definitely not an entry call.
11879 when Attribute_Caller
=>
11886 -- Shares processing with Address attribute
11892 -- If the prefix of the Count attribute is an entry name it must not
11893 -- be resolved, since this is definitely not an entry call. However,
11894 -- if it is an element of an entry family, the index itself may
11895 -- have to be resolved because it can be a general expression.
11897 when Attribute_Count
=>
11898 if Nkind
(P
) = N_Indexed_Component
11899 and then Is_Entity_Name
(Prefix
(P
))
11902 Indx
: constant Node_Id
:= First
(Expressions
(P
));
11903 Fam
: constant Entity_Id
:= Entity
(Prefix
(P
));
11905 Resolve
(Indx
, Entry_Index_Type
(Fam
));
11906 Apply_Scalar_Range_Check
(Indx
, Entry_Index_Type
(Fam
));
11914 -- Prefix of the Elaborated attribute is a subprogram name which
11915 -- must not be resolved, since this is definitely not a call. Note
11916 -- that it is a library unit, so it cannot be overloaded here.
11918 when Attribute_Elaborated
=>
11925 -- Prefix of Enabled attribute is a check name, which must be treated
11926 -- specially and not touched by Resolve.
11928 when Attribute_Enabled
=>
11935 -- Do not resolve the prefix of Loop_Entry, instead wait until the
11936 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
11937 -- The delay ensures that any generated checks or temporaries are
11938 -- inserted before the relocated prefix.
11940 when Attribute_Loop_Entry
=>
11943 --------------------
11944 -- Mechanism_Code --
11945 --------------------
11947 -- Prefix of the Mechanism_Code attribute is a function name
11948 -- which must not be resolved. Should we check for overloaded ???
11950 when Attribute_Mechanism_Code
=>
11957 -- Most processing is done in sem_dist, after determining the
11958 -- context type. Node is rewritten as a conversion to a runtime call.
11960 when Attribute_Partition_ID
=>
11961 Process_Partition_Id
(N
);
11968 when Attribute_Pool_Address
=>
11975 -- We replace the Range attribute node with a range expression whose
11976 -- bounds are the 'First and 'Last attributes applied to the same
11977 -- prefix. The reason that we do this transformation here instead of
11978 -- in the expander is that it simplifies other parts of the semantic
11979 -- analysis which assume that the Range has been replaced; thus it
11980 -- must be done even when in semantic-only mode (note that the RM
11981 -- specifically mentions this equivalence, we take care that the
11982 -- prefix is only evaluated once).
11984 when Attribute_Range
=> Range_Attribute
: declare
11990 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
11993 -- If the prefix is a function call returning on the secondary
11994 -- stack, we must make sure to mark/release the stack.
11996 if Nkind
(P
) = N_Function_Call
11997 and then Nkind
(Parent
(N
)) = N_Loop_Parameter_Specification
11998 and then Requires_Transient_Scope
(Etype
(P
))
12000 Set_Uses_Sec_Stack
(Scope
(Current_Scope
));
12004 Dims
:= Expressions
(N
);
12007 Make_Attribute_Reference
(Loc
,
12008 Prefix
=> Duplicate_Subexpr
(P
, Name_Req
=> True),
12009 Attribute_Name
=> Name_Last
,
12010 Expressions
=> Dims
);
12013 Make_Attribute_Reference
(Loc
,
12015 Attribute_Name
=> Name_First
,
12016 Expressions
=> (Dims
));
12018 -- Do not share the dimension indicator, if present. Even though
12019 -- it is a static constant, its source location may be modified
12020 -- when printing expanded code and node sharing will lead to chaos
12023 if Present
(Dims
) then
12024 Set_Expressions
(LB
, New_List
(New_Copy_Tree
(First
(Dims
))));
12027 -- If the original was marked as Must_Not_Freeze (see code in
12028 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
12031 if Must_Not_Freeze
(N
) then
12032 Set_Must_Not_Freeze
(HB
);
12033 Set_Must_Not_Freeze
(LB
);
12034 Set_Must_Not_Freeze
(Prefix
(HB
));
12035 Set_Must_Not_Freeze
(Prefix
(LB
));
12038 if Raises_Constraint_Error
(Prefix
(N
)) then
12040 -- Preserve Sloc of prefix in the new bounds, so that the
12041 -- posted warning can be removed if we are within unreachable
12044 Set_Sloc
(LB
, Sloc
(Prefix
(N
)));
12045 Set_Sloc
(HB
, Sloc
(Prefix
(N
)));
12048 Rewrite
(N
, Make_Range
(Loc
, LB
, HB
));
12049 Analyze_And_Resolve
(N
, Typ
);
12051 -- Ensure that the expanded range does not have side effects
12053 Force_Evaluation
(LB
);
12054 Force_Evaluation
(HB
);
12056 -- Normally after resolving attribute nodes, Eval_Attribute
12057 -- is called to do any possible static evaluation of the node.
12058 -- However, here since the Range attribute has just been
12059 -- transformed into a range expression it is no longer an
12060 -- attribute node and therefore the call needs to be avoided
12061 -- and is accomplished by simply returning from the procedure.
12064 end Range_Attribute
;
12070 when Attribute_Reduce
=>
12072 E1
: constant Node_Id
:= First
(Expressions
(N
));
12073 E2
: constant Node_Id
:= Next
(E1
);
12074 Op
: Entity_Id
:= Empty
;
12076 Index
: Interp_Index
;
12078 function Proper_Op
(Op
: Entity_Id
) return Boolean;
12084 function Proper_Op
(Op
: Entity_Id
) return Boolean is
12085 F1
, F2
: Entity_Id
;
12088 F1
:= First_Formal
(Op
);
12092 F2
:= Next_Formal
(F1
);
12094 or else Present
(Next_Formal
(F2
))
12098 elsif Ekind
(Op
) = E_Procedure
then
12099 return Ekind
(F1
) = E_In_Out_Parameter
12100 and then Covers
(Typ
, Etype
(F1
));
12104 (Ekind
(Op
) = E_Operator
12105 and then Scope
(Op
) = Standard_Standard
)
12106 or else Covers
(Typ
, Etype
(Op
));
12113 if Is_Overloaded
(E1
) then
12114 Get_First_Interp
(E1
, Index
, It
);
12115 while Present
(It
.Nam
) loop
12116 if Proper_Op
(It
.Nam
) then
12118 Set_Entity
(E1
, Op
);
12122 Get_Next_Interp
(Index
, It
);
12125 elsif Nkind
(E1
) = N_Attribute_Reference
12126 and then (Attribute_Name
(E1
) = Name_Max
12127 or else Attribute_Name
(E1
) = Name_Min
)
12131 elsif Proper_Op
(Entity
(E1
)) then
12133 Set_Etype
(N
, Typ
);
12137 Error_Msg_N
("No visible subprogram for reduction", E1
);
12145 -- We will only come here during the prescan of a spec expression
12146 -- containing a Result attribute. In that case the proper Etype has
12147 -- already been set, and nothing more needs to be done here.
12149 when Attribute_Result
=>
12152 ----------------------
12153 -- Unchecked_Access --
12154 ----------------------
12156 -- Processing is shared with Access
12158 -------------------------
12159 -- Unrestricted_Access --
12160 -------------------------
12162 -- Processing is shared with Access
12168 -- Resolve aggregate components in component associations
12170 when Attribute_Update
=> Update
: declare
12171 Aggr
: constant Node_Id
:= First
(Expressions
(N
));
12172 Typ
: constant Entity_Id
:= Etype
(Prefix
(N
));
12178 -- Set the Etype of the aggregate to that of the prefix, even
12179 -- though the aggregate may not be a proper representation of a
12180 -- value of the type (missing or duplicated associations, etc.)
12181 -- Complete resolution of the prefix. Note that in Ada 2012 it
12182 -- can be a qualified expression that is e.g. an aggregate.
12184 Set_Etype
(Aggr
, Typ
);
12185 Resolve
(Prefix
(N
), Typ
);
12187 -- For an array type, resolve expressions with the component type
12188 -- of the array, and apply constraint checks when needed.
12190 if Is_Array_Type
(Typ
) then
12191 Assoc
:= First
(Component_Associations
(Aggr
));
12192 while Present
(Assoc
) loop
12193 Expr
:= Expression
(Assoc
);
12194 Resolve
(Expr
, Component_Type
(Typ
));
12196 -- The choices in the association are static constants,
12197 -- or static aggregates each of whose components belongs
12198 -- to the proper index type. However, they must also
12199 -- belong to the index subtype (s) of the prefix, which
12200 -- may be a subtype (e.g. given by a slice).
12202 -- Choices may also be identifiers with no staticness
12203 -- requirements, in which case they must resolve to the
12212 C
:= First
(Choices
(Assoc
));
12213 while Present
(C
) loop
12214 Indx
:= First_Index
(Etype
(Prefix
(N
)));
12216 if Nkind
(C
) /= N_Aggregate
then
12217 Analyze_And_Resolve
(C
, Etype
(Indx
));
12219 C_E
:= First
(Expressions
(C
));
12220 while Present
(C_E
) loop
12221 Analyze_And_Resolve
(C_E
, Etype
(Indx
));
12235 -- For a record type, use type of each component, which is
12236 -- recorded during analysis.
12239 Assoc
:= First
(Component_Associations
(Aggr
));
12240 while Present
(Assoc
) loop
12241 Comp
:= First
(Choices
(Assoc
));
12242 Expr
:= Expression
(Assoc
);
12244 if Nkind
(Comp
) /= N_Others_Choice
12245 and then not Error_Posted
(Comp
)
12247 Resolve
(Expr
, Etype
(Entity
(Comp
)));
12259 -- Apply range check. Note that we did not do this during the
12260 -- analysis phase, since we wanted Eval_Attribute to have a
12261 -- chance at finding an illegal out of range value.
12263 when Attribute_Val
=>
12265 -- Note that we do our own Eval_Attribute call here rather than
12266 -- use the common one, because we need to do processing after
12267 -- the call, as per above comment.
12269 Eval_Attribute
(N
);
12271 -- Eval_Attribute may replace the node with a raise CE, or
12272 -- fold it to a constant. Obviously we only apply a scalar
12273 -- range check if this did not happen.
12275 if Nkind
(N
) = N_Attribute_Reference
12276 and then Attribute_Name
(N
) = Name_Val
12278 Apply_Scalar_Range_Check
(First
(Expressions
(N
)), Btyp
);
12287 -- Prefix of Version attribute can be a subprogram name which
12288 -- must not be resolved, since this is not a call.
12290 when Attribute_Version
=>
12293 ----------------------
12294 -- Other Attributes --
12295 ----------------------
12297 -- For other attributes, resolve prefix unless it is a type. If
12298 -- the attribute reference itself is a type name ('Base and 'Class)
12299 -- then this is only legal within a task or protected record.
12302 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
12306 -- If the attribute reference itself is a type name ('Base,
12307 -- 'Class) then this is only legal within a task or protected
12308 -- record. What is this all about ???
12310 if Is_Entity_Name
(N
) and then Is_Type
(Entity
(N
)) then
12311 if Is_Concurrent_Type
(Entity
(N
))
12312 and then In_Open_Scopes
(Entity
(P
))
12317 ("invalid use of subtype name in expression or call", N
);
12321 -- For attributes whose argument may be a string, complete
12322 -- resolution of argument now. This avoids premature expansion
12323 -- (and the creation of transient scopes) before the attribute
12324 -- reference is resolved.
12327 when Attribute_Value
=>
12328 Resolve
(First
(Expressions
(N
)), Standard_String
);
12330 when Attribute_Wide_Value
=>
12331 Resolve
(First
(Expressions
(N
)), Standard_Wide_String
);
12333 when Attribute_Wide_Wide_Value
=>
12334 Resolve
(First
(Expressions
(N
)), Standard_Wide_Wide_String
);
12336 when others => null;
12339 -- If the prefix of the attribute is a class-wide type then it
12340 -- will be expanded into a dispatching call to a predefined
12341 -- primitive. Therefore we must check for potential violation
12342 -- of such restriction.
12344 if Is_Class_Wide_Type
(Etype
(P
)) then
12345 Check_Restriction
(No_Dispatching_Calls
, N
);
12349 -- Mark use clauses of the original prefix if the attribute is applied
12352 if Nkind
(Original_Node
(P
)) in N_Has_Entity
12353 and then Present
(Entity
(Original_Node
(P
)))
12355 Mark_Use_Clauses
(Original_Node
(P
));
12358 -- Normally the Freezing is done by Resolve but sometimes the Prefix
12359 -- is not resolved, in which case the freezing must be done now.
12361 -- For an elaboration check on a subprogram, we do not freeze its type.
12362 -- It may be declared in an unrelated scope, in particular in the case
12363 -- of a generic function whose type may remain unelaborated.
12365 if Attr_Id
= Attribute_Elaborated
then
12368 -- Should this be restricted to Expander_Active???
12371 Freeze_Expression
(P
);
12374 -- Finally perform static evaluation on the attribute reference
12376 Analyze_Dimension
(N
);
12377 Eval_Attribute
(N
);
12378 end Resolve_Attribute
;
12380 ------------------------
12381 -- Set_Boolean_Result --
12382 ------------------------
12384 procedure Set_Boolean_Result
(N
: Node_Id
; B
: Boolean) is
12385 Loc
: constant Source_Ptr
:= Sloc
(N
);
12388 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
12390 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
12392 end Set_Boolean_Result
;
12394 --------------------------------
12395 -- Stream_Attribute_Available --
12396 --------------------------------
12398 function Stream_Attribute_Available
12400 Nam
: TSS_Name_Type
;
12401 Partial_View
: Node_Id
:= Empty
) return Boolean
12403 Etyp
: Entity_Id
:= Typ
;
12405 -- Start of processing for Stream_Attribute_Available
12408 -- We need some comments in this body ???
12410 if Has_Stream_Attribute_Definition
(Typ
, Nam
) then
12414 if Is_Class_Wide_Type
(Typ
) then
12415 return not Is_Limited_Type
(Typ
)
12416 or else Stream_Attribute_Available
(Etype
(Typ
), Nam
);
12419 if Nam
= TSS_Stream_Input
12420 and then Is_Abstract_Type
(Typ
)
12421 and then not Is_Class_Wide_Type
(Typ
)
12426 if not (Is_Limited_Type
(Typ
)
12427 or else (Present
(Partial_View
)
12428 and then Is_Limited_Type
(Partial_View
)))
12433 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
12435 if Nam
= TSS_Stream_Input
12436 and then Ada_Version
>= Ada_2005
12437 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Read
)
12441 elsif Nam
= TSS_Stream_Output
12442 and then Ada_Version
>= Ada_2005
12443 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Write
)
12448 -- Case of Read and Write: check for attribute definition clause that
12449 -- applies to an ancestor type.
12451 while Etype
(Etyp
) /= Etyp
loop
12453 Derived_Type
: constant Entity_Id
:= Etyp
;
12455 Etyp
:= Etype
(Etyp
);
12457 if Has_Stream_Attribute_Definition
(Etyp
, Nam
) then
12458 if not Derivation_Too_Early_To_Inherit
(Derived_Type
, Nam
) then
12465 if Ada_Version
< Ada_2005
then
12467 -- In Ada 95 mode, also consider a non-visible definition
12470 Btyp
: constant Entity_Id
:= Implementation_Base_Type
(Typ
);
12473 and then Stream_Attribute_Available
12474 (Btyp
, Nam
, Partial_View
=> Typ
);
12479 end Stream_Attribute_Available
;