1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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 Atree
; use Atree
;
29 with Casing
; use Casing
;
30 with Checks
; use Checks
;
31 with Debug
; use Debug
;
32 with Einfo
; use Einfo
;
33 with Elists
; use Elists
;
34 with Errout
; use Errout
;
36 with Exp_Dist
; use Exp_Dist
;
37 with Exp_Util
; use Exp_Util
;
38 with Expander
; use Expander
;
39 with Freeze
; use Freeze
;
40 with Gnatvsn
; use Gnatvsn
;
41 with Itypes
; use Itypes
;
43 with Lib
.Xref
; use Lib
.Xref
;
44 with Nlists
; use Nlists
;
45 with Nmake
; use Nmake
;
47 with Restrict
; use Restrict
;
48 with Rident
; use Rident
;
49 with Rtsfind
; use Rtsfind
;
50 with Sdefault
; use Sdefault
;
52 with Sem_Aux
; use Sem_Aux
;
53 with Sem_Cat
; use Sem_Cat
;
54 with Sem_Ch6
; use Sem_Ch6
;
55 with Sem_Ch8
; use Sem_Ch8
;
56 with Sem_Ch10
; use Sem_Ch10
;
57 with Sem_Dim
; use Sem_Dim
;
58 with Sem_Dist
; use Sem_Dist
;
59 with Sem_Elab
; use Sem_Elab
;
60 with Sem_Elim
; use Sem_Elim
;
61 with Sem_Eval
; use Sem_Eval
;
62 with Sem_Res
; use Sem_Res
;
63 with Sem_Type
; use Sem_Type
;
64 with Sem_Util
; use Sem_Util
;
66 with Stand
; use Stand
;
67 with Sinfo
; use Sinfo
;
68 with Sinput
; use Sinput
;
70 with Stringt
; use Stringt
;
72 with Stylesw
; use Stylesw
;
73 with Targparm
; use Targparm
;
74 with Ttypes
; use Ttypes
;
75 with Tbuild
; use Tbuild
;
76 with Uintp
; use Uintp
;
77 with Uname
; use Uname
;
78 with Urealp
; use Urealp
;
80 package body Sem_Attr
is
82 True_Value
: constant Uint
:= Uint_1
;
83 False_Value
: constant Uint
:= Uint_0
;
84 -- Synonyms to be used when these constants are used as Boolean values
86 Bad_Attribute
: exception;
87 -- Exception raised if an error is detected during attribute processing,
88 -- used so that we can abandon the processing so we don't run into
89 -- trouble with cascaded errors.
91 -- The following array is the list of attributes defined in the Ada 83 RM.
92 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
93 -- modes all these attributes are recognized, even if removed in Ada 95.
95 Attribute_83
: constant Attribute_Class_Array
:= Attribute_Class_Array
'(
101 Attribute_Constrained |
108 Attribute_First_Bit |
114 Attribute_Leading_Part |
116 Attribute_Machine_Emax |
117 Attribute_Machine_Emin |
118 Attribute_Machine_Mantissa |
119 Attribute_Machine_Overflows |
120 Attribute_Machine_Radix |
121 Attribute_Machine_Rounds |
127 Attribute_Safe_Emax |
128 Attribute_Safe_Large |
129 Attribute_Safe_Small |
132 Attribute_Storage_Size |
134 Attribute_Terminated |
137 Attribute_Width => True,
140 -- The following array is the list of attributes defined in the Ada 2005
141 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
142 -- but in Ada 95 they are considered to be implementation defined.
144 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
145 Attribute_Machine_Rounding |
148 Attribute_Stream_Size |
149 Attribute_Wide_Wide_Width
=> True,
152 -- The following array is the list of attributes defined in the Ada 2012
153 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
154 -- and Ada 2005 modes, but are considered to be implementation defined.
156 Attribute_12
: constant Attribute_Class_Array
:= Attribute_Class_Array
'(
157 Attribute_First_Valid |
158 Attribute_Has_Same_Storage |
159 Attribute_Last_Valid |
160 Attribute_Max_Alignment_For_Allocation => True,
163 -- The following array contains all attributes that imply a modification
164 -- of their prefixes or result in an access value. Such prefixes can be
165 -- considered as lvalues.
167 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
168 Attribute_Class_Array'(
173 Attribute_Unchecked_Access |
174 Attribute_Unrestricted_Access
=> True,
177 -----------------------
178 -- Local_Subprograms --
179 -----------------------
181 procedure Eval_Attribute
(N
: Node_Id
);
182 -- Performs compile time evaluation of attributes where possible, leaving
183 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
184 -- set, and replacing the node with a literal node if the value can be
185 -- computed at compile time. All static attribute references are folded,
186 -- as well as a number of cases of non-static attributes that can always
187 -- be computed at compile time (e.g. floating-point model attributes that
188 -- are applied to non-static subtypes). Of course in such cases, the
189 -- Is_Static_Expression flag will not be set on the resulting literal.
190 -- Note that the only required action of this procedure is to catch the
191 -- static expression cases as described in the RM. Folding of other cases
192 -- is done where convenient, but some additional non-static folding is in
193 -- Expand_N_Attribute_Reference in cases where this is more convenient.
195 function Is_Anonymous_Tagged_Base
197 Typ
: Entity_Id
) return Boolean;
198 -- For derived tagged types that constrain parent discriminants we build
199 -- an anonymous unconstrained base type. We need to recognize the relation
200 -- between the two when analyzing an access attribute for a constrained
201 -- component, before the full declaration for Typ has been analyzed, and
202 -- where therefore the prefix of the attribute does not match the enclosing
205 procedure Set_Boolean_Result
(N
: Node_Id
; B
: Boolean);
206 -- Rewrites node N with an occurrence of either Standard_False or
207 -- Standard_True, depending on the value of the parameter B. The
208 -- result is marked as a static expression.
210 -----------------------
211 -- Analyze_Attribute --
212 -----------------------
214 procedure Analyze_Attribute
(N
: Node_Id
) is
215 Loc
: constant Source_Ptr
:= Sloc
(N
);
216 Aname
: constant Name_Id
:= Attribute_Name
(N
);
217 P
: constant Node_Id
:= Prefix
(N
);
218 Exprs
: constant List_Id
:= Expressions
(N
);
219 Attr_Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
224 -- Type of prefix after analysis
226 P_Base_Type
: Entity_Id
;
227 -- Base type of prefix after analysis
229 -----------------------
230 -- Local Subprograms --
231 -----------------------
233 procedure Address_Checks
;
234 -- Semantic checks for valid use of Address attribute. This was made
235 -- a separate routine with the idea of using it for unrestricted access
236 -- which seems like it should follow the same rules, but that turned
237 -- out to be impractical. So now this is only used for Address.
239 procedure Analyze_Access_Attribute
;
240 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
241 -- Internally, Id distinguishes which of the three cases is involved.
243 procedure Bad_Attribute_For_Predicate
;
244 -- Output error message for use of a predicate (First, Last, Range) not
245 -- allowed with a type that has predicates. If the type is a generic
246 -- actual, then the message is a warning, and we generate code to raise
247 -- program error with an appropriate reason. No error message is given
248 -- for internally generated uses of the attributes. This legality rule
249 -- only applies to scalar types.
251 procedure Check_Array_Or_Scalar_Type
;
252 -- Common procedure used by First, Last, Range attribute to check
253 -- that the prefix is a constrained array or scalar type, or a name
254 -- of an array object, and that an argument appears only if appropriate
255 -- (i.e. only in the array case).
257 procedure Check_Array_Type
;
258 -- Common semantic checks for all array attributes. Checks that the
259 -- prefix is a constrained array type or the name of an array object.
260 -- The error message for non-arrays is specialized appropriately.
262 procedure Check_Asm_Attribute
;
263 -- Common semantic checks for Asm_Input and Asm_Output attributes
265 procedure Check_Component
;
266 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
267 -- Position. Checks prefix is an appropriate selected component.
269 procedure Check_Decimal_Fixed_Point_Type
;
270 -- Check that prefix of attribute N is a decimal fixed-point type
272 procedure Check_Dereference
;
273 -- If the prefix of attribute is an object of an access type, then
274 -- introduce an explicit dereference, and adjust P_Type accordingly.
276 procedure Check_Discrete_Type
;
277 -- Verify that prefix of attribute N is a discrete type
280 -- Check that no attribute arguments are present
282 procedure Check_Either_E0_Or_E1
;
283 -- Check that there are zero or one attribute arguments present
286 -- Check that exactly one attribute argument is present
289 -- Check that two attribute arguments are present
291 procedure Check_Enum_Image
;
292 -- If the prefix type of 'Image is an enumeration type, set all its
293 -- literals as referenced, since the image function could possibly end
294 -- up referencing any of the literals indirectly. Same for Enum_Val.
295 -- Set the flag only if the reference is in the main code unit. Same
296 -- restriction when resolving 'Value; otherwise an improperly set
297 -- reference when analyzing an inlined body will lose a proper
298 -- warning on a useless with_clause.
300 procedure Check_First_Last_Valid
;
301 -- Perform all checks for First_Valid and Last_Valid attributes
303 procedure Check_Fixed_Point_Type
;
304 -- Verify that prefix of attribute N is a fixed type
306 procedure Check_Fixed_Point_Type_0
;
307 -- Verify that prefix of attribute N is a fixed type and that
308 -- no attribute expressions are present
310 procedure Check_Floating_Point_Type
;
311 -- Verify that prefix of attribute N is a float type
313 procedure Check_Floating_Point_Type_0
;
314 -- Verify that prefix of attribute N is a float type and that
315 -- no attribute expressions are present
317 procedure Check_Floating_Point_Type_1
;
318 -- Verify that prefix of attribute N is a float type and that
319 -- exactly one attribute expression is present
321 procedure Check_Floating_Point_Type_2
;
322 -- Verify that prefix of attribute N is a float type and that
323 -- two attribute expressions are present
325 procedure Check_SPARK_05_Restriction_On_Attribute
;
326 -- Issue an error in formal mode because attribute N is allowed
328 procedure Check_Integer_Type
;
329 -- Verify that prefix of attribute N is an integer type
331 procedure Check_Modular_Integer_Type
;
332 -- Verify that prefix of attribute N is a modular integer type
334 procedure Check_Not_CPP_Type
;
335 -- Check that P (the prefix of the attribute) is not an CPP type
336 -- for which no Ada predefined primitive is available.
338 procedure Check_Not_Incomplete_Type
;
339 -- Check that P (the prefix of the attribute) is not an incomplete
340 -- type or a private type for which no full view has been given.
342 procedure Check_Object_Reference
(P
: Node_Id
);
343 -- Check that P is an object reference
345 procedure Check_Program_Unit
;
346 -- Verify that prefix of attribute N is a program unit
348 procedure Check_Real_Type
;
349 -- Verify that prefix of attribute N is fixed or float type
351 procedure Check_Scalar_Type
;
352 -- Verify that prefix of attribute N is a scalar type
354 procedure Check_Standard_Prefix
;
355 -- Verify that prefix of attribute N is package Standard. Also checks
356 -- that there are no arguments.
358 procedure Check_Stream_Attribute
(Nam
: TSS_Name_Type
);
359 -- Validity checking for stream attribute. Nam is the TSS name of the
360 -- corresponding possible defined attribute function (e.g. for the
361 -- Read attribute, Nam will be TSS_Stream_Read).
363 procedure Check_System_Prefix
;
364 -- Verify that prefix of attribute N is package System
366 procedure Check_PolyORB_Attribute
;
367 -- Validity checking for PolyORB/DSA attribute
369 procedure Check_Task_Prefix
;
370 -- Verify that prefix of attribute N is a task or task type
372 procedure Check_Type
;
373 -- Verify that the prefix of attribute N is a type
375 procedure Check_Unit_Name
(Nod
: Node_Id
);
376 -- Check that Nod is of the form of a library unit name, i.e that
377 -- it is an identifier, or a selected component whose prefix is
378 -- itself of the form of a library unit name. Note that this is
379 -- quite different from Check_Program_Unit, since it only checks
380 -- the syntactic form of the name, not the semantic identity. This
381 -- is because it is used with attributes (Elab_Body, Elab_Spec,
382 -- UET_Address and Elaborated) which can refer to non-visible unit.
384 procedure Error_Attr
(Msg
: String; Error_Node
: Node_Id
);
385 pragma No_Return
(Error_Attr
);
386 procedure Error_Attr
;
387 pragma No_Return
(Error_Attr
);
388 -- Posts error using Error_Msg_N at given node, sets type of attribute
389 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
390 -- semantic processing. The message typically contains a % insertion
391 -- character which is replaced by the attribute name. The call with
392 -- no arguments is used when the caller has already generated the
393 -- required error messages.
395 procedure Error_Attr_P
(Msg
: String);
396 pragma No_Return
(Error_Attr
);
397 -- Like Error_Attr, but error is posted at the start of the prefix
399 function In_Refined_Post
return Boolean;
400 -- Determine whether the current attribute appears in pragma
403 procedure Legal_Formal_Attribute
;
404 -- Common processing for attributes Definite and Has_Discriminants.
405 -- Checks that prefix is generic indefinite formal type.
407 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
408 -- Common processing for attributes Max_Alignment_For_Allocation and
409 -- Max_Size_In_Storage_Elements.
412 -- Common processing for attributes Max and Min
414 procedure Standard_Attribute
(Val
: Int
);
415 -- Used to process attributes whose prefix is package Standard which
416 -- yield values of type Universal_Integer. The attribute reference
417 -- node is rewritten with an integer literal of the given value which
418 -- is marked as static.
420 procedure Uneval_Old_Msg
;
421 -- Called when Loop_Entry or Old is used in a potentially unevaluated
422 -- expression. Generates appropriate message or warning depending on
423 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
424 -- node in the aspect case).
426 procedure Unexpected_Argument
(En
: Node_Id
);
427 -- Signal unexpected attribute argument (En is the argument)
429 procedure Validate_Non_Static_Attribute_Function_Call
;
430 -- Called when processing an attribute that is a function call to a
431 -- non-static function, i.e. an attribute function that either takes
432 -- non-scalar arguments or returns a non-scalar result. Verifies that
433 -- such a call does not appear in a preelaborable context.
439 procedure Address_Checks
is
441 -- An Address attribute created by expansion is legal even when it
442 -- applies to other entity-denoting expressions.
444 if not Comes_From_Source
(N
) then
447 -- Address attribute on a protected object self reference is legal
449 elsif Is_Protected_Self_Reference
(P
) then
452 -- Address applied to an entity
454 elsif Is_Entity_Name
(P
) then
456 Ent
: constant Entity_Id
:= Entity
(P
);
459 if Is_Subprogram
(Ent
) then
460 Set_Address_Taken
(Ent
);
461 Kill_Current_Values
(Ent
);
463 -- An Address attribute is accepted when generated by the
464 -- compiler for dispatching operation, and an error is
465 -- issued once the subprogram is frozen (to avoid confusing
466 -- errors about implicit uses of Address in the dispatch
467 -- table initialization).
469 if Has_Pragma_Inline_Always
(Entity
(P
))
470 and then Comes_From_Source
(P
)
473 ("prefix of % attribute cannot be Inline_Always "
476 -- It is illegal to apply 'Address to an intrinsic
477 -- subprogram. This is now formalized in AI05-0095.
478 -- In an instance, an attempt to obtain 'Address of an
479 -- intrinsic subprogram (e.g the renaming of a predefined
480 -- operator that is an actual) raises Program_Error.
482 elsif Convention
(Ent
) = Convention_Intrinsic
then
485 Make_Raise_Program_Error
(Loc
,
486 Reason
=> PE_Address_Of_Intrinsic
));
489 Error_Msg_Name_1
:= Aname
;
491 ("cannot take % of intrinsic subprogram", N
);
494 -- Issue an error if prefix denotes an eliminated subprogram
497 Check_For_Eliminated_Subprogram
(P
, Ent
);
500 -- Object or label reference
502 elsif Is_Object
(Ent
) or else Ekind
(Ent
) = E_Label
then
503 Set_Address_Taken
(Ent
);
505 -- Deal with No_Implicit_Aliasing restriction
507 if Restriction_Check_Required
(No_Implicit_Aliasing
) then
508 if not Is_Aliased_View
(P
) then
509 Check_Restriction
(No_Implicit_Aliasing
, P
);
511 Check_No_Implicit_Aliasing
(P
);
515 -- If we have an address of an object, and the attribute
516 -- comes from source, then set the object as potentially
517 -- source modified. We do this because the resulting address
518 -- can potentially be used to modify the variable and we
519 -- might not detect this, leading to some junk warnings.
521 Set_Never_Set_In_Source
(Ent
, False);
523 -- Allow Address to be applied to task or protected type,
524 -- returning null address (what is that about???)
526 elsif (Is_Concurrent_Type
(Etype
(Ent
))
527 and then Etype
(Ent
) = Base_Type
(Ent
))
528 or else Ekind
(Ent
) = E_Package
529 or else Is_Generic_Unit
(Ent
)
532 New_Occurrence_Of
(RTE
(RE_Null_Address
), Sloc
(N
)));
534 -- Anything else is illegal
537 Error_Attr
("invalid prefix for % attribute", P
);
543 elsif Is_Object_Reference
(P
) then
546 -- Subprogram called using dot notation
548 elsif Nkind
(P
) = N_Selected_Component
549 and then Is_Subprogram
(Entity
(Selector_Name
(P
)))
553 -- What exactly are we allowing here ??? and is this properly
554 -- documented in the sinfo documentation for this node ???
556 elsif Relaxed_RM_Semantics
557 and then Nkind
(P
) = N_Attribute_Reference
561 -- All other non-entity name cases are illegal
564 Error_Attr
("invalid prefix for % attribute", P
);
568 ------------------------------
569 -- Analyze_Access_Attribute --
570 ------------------------------
572 procedure Analyze_Access_Attribute
is
573 Acc_Type
: Entity_Id
;
578 function Build_Access_Object_Type
(DT
: Entity_Id
) return Entity_Id
;
579 -- Build an access-to-object type whose designated type is DT,
580 -- and whose Ekind is appropriate to the attribute type. The
581 -- type that is constructed is returned as the result.
583 procedure Build_Access_Subprogram_Type
(P
: Node_Id
);
584 -- Build an access to subprogram whose designated type is the type of
585 -- the prefix. If prefix is overloaded, so is the node itself. The
586 -- result is stored in Acc_Type.
588 function OK_Self_Reference
return Boolean;
589 -- An access reference whose prefix is a type can legally appear
590 -- within an aggregate, where it is obtained by expansion of
591 -- a defaulted aggregate. The enclosing aggregate that contains
592 -- the self-referenced is flagged so that the self-reference can
593 -- be expanded into a reference to the target object (see exp_aggr).
595 ------------------------------
596 -- Build_Access_Object_Type --
597 ------------------------------
599 function Build_Access_Object_Type
(DT
: Entity_Id
) return Entity_Id
is
600 Typ
: constant Entity_Id
:=
602 (E_Access_Attribute_Type
, Current_Scope
, Loc
, 'A');
604 Set_Etype
(Typ
, Typ
);
606 Set_Associated_Node_For_Itype
(Typ
, N
);
607 Set_Directly_Designated_Type
(Typ
, DT
);
609 end Build_Access_Object_Type
;
611 ----------------------------------
612 -- Build_Access_Subprogram_Type --
613 ----------------------------------
615 procedure Build_Access_Subprogram_Type
(P
: Node_Id
) is
616 Index
: Interp_Index
;
619 procedure Check_Local_Access
(E
: Entity_Id
);
620 -- Deal with possible access to local subprogram. If we have such
621 -- an access, we set a flag to kill all tracked values on any call
622 -- because this access value may be passed around, and any called
623 -- code might use it to access a local procedure which clobbers a
624 -- tracked value. If the scope is a loop or block, indicate that
625 -- value tracking is disabled for the enclosing subprogram.
627 function Get_Kind
(E
: Entity_Id
) return Entity_Kind
;
628 -- Distinguish between access to regular/protected subprograms
630 ------------------------
631 -- Check_Local_Access --
632 ------------------------
634 procedure Check_Local_Access
(E
: Entity_Id
) is
636 if not Is_Library_Level_Entity
(E
) then
637 Set_Suppress_Value_Tracking_On_Call
(Current_Scope
);
638 Set_Suppress_Value_Tracking_On_Call
639 (Nearest_Dynamic_Scope
(Current_Scope
));
641 end Check_Local_Access
;
647 function Get_Kind
(E
: Entity_Id
) return Entity_Kind
is
649 if Convention
(E
) = Convention_Protected
then
650 return E_Access_Protected_Subprogram_Type
;
652 return E_Access_Subprogram_Type
;
656 -- Start of processing for Build_Access_Subprogram_Type
659 -- In the case of an access to subprogram, use the name of the
660 -- subprogram itself as the designated type. Type-checking in
661 -- this case compares the signatures of the designated types.
663 -- Note: This fragment of the tree is temporarily malformed
664 -- because the correct tree requires an E_Subprogram_Type entity
665 -- as the designated type. In most cases this designated type is
666 -- later overridden by the semantics with the type imposed by the
667 -- context during the resolution phase. In the specific case of
668 -- the expression Address!(Prim'Unrestricted_Access), used to
669 -- initialize slots of dispatch tables, this work will be done by
670 -- the expander (see Exp_Aggr).
672 -- The reason to temporarily add this kind of node to the tree
673 -- instead of a proper E_Subprogram_Type itype, is the following:
674 -- in case of errors found in the source file we report better
675 -- error messages. For example, instead of generating the
678 -- "expected access to subprogram with profile
679 -- defined at line X"
681 -- we currently generate:
683 -- "expected access to function Z defined at line X"
685 Set_Etype
(N
, Any_Type
);
687 if not Is_Overloaded
(P
) then
688 Check_Local_Access
(Entity
(P
));
690 if not Is_Intrinsic_Subprogram
(Entity
(P
)) then
691 Acc_Type
:= Create_Itype
(Get_Kind
(Entity
(P
)), N
);
692 Set_Is_Public
(Acc_Type
, False);
693 Set_Etype
(Acc_Type
, Acc_Type
);
694 Set_Convention
(Acc_Type
, Convention
(Entity
(P
)));
695 Set_Directly_Designated_Type
(Acc_Type
, Entity
(P
));
696 Set_Etype
(N
, Acc_Type
);
697 Freeze_Before
(N
, Acc_Type
);
701 Get_First_Interp
(P
, Index
, It
);
702 while Present
(It
.Nam
) loop
703 Check_Local_Access
(It
.Nam
);
705 if not Is_Intrinsic_Subprogram
(It
.Nam
) then
706 Acc_Type
:= Create_Itype
(Get_Kind
(It
.Nam
), N
);
707 Set_Is_Public
(Acc_Type
, False);
708 Set_Etype
(Acc_Type
, Acc_Type
);
709 Set_Convention
(Acc_Type
, Convention
(It
.Nam
));
710 Set_Directly_Designated_Type
(Acc_Type
, It
.Nam
);
711 Add_One_Interp
(N
, Acc_Type
, Acc_Type
);
712 Freeze_Before
(N
, Acc_Type
);
715 Get_Next_Interp
(Index
, It
);
719 -- Cannot be applied to intrinsic. Looking at the tests above,
720 -- the only way Etype (N) can still be set to Any_Type is if
721 -- Is_Intrinsic_Subprogram was True for some referenced entity.
723 if Etype
(N
) = Any_Type
then
724 Error_Attr_P
("prefix of % attribute cannot be intrinsic");
726 end Build_Access_Subprogram_Type
;
728 ----------------------
729 -- OK_Self_Reference --
730 ----------------------
732 function OK_Self_Reference
return Boolean is
739 (Nkind
(Par
) = N_Component_Association
740 or else Nkind
(Par
) in N_Subexpr
)
742 if Nkind_In
(Par
, N_Aggregate
, N_Extension_Aggregate
) then
743 if Etype
(Par
) = Typ
then
744 Set_Has_Self_Reference
(Par
);
752 -- No enclosing aggregate, or not a self-reference
755 end OK_Self_Reference
;
757 -- Start of processing for Analyze_Access_Attribute
760 Check_SPARK_05_Restriction_On_Attribute
;
763 if Nkind
(P
) = N_Character_Literal
then
765 ("prefix of % attribute cannot be enumeration literal");
768 -- Case of access to subprogram
770 if Is_Entity_Name
(P
) and then Is_Overloadable
(Entity
(P
)) then
771 if Has_Pragma_Inline_Always
(Entity
(P
)) then
773 ("prefix of % attribute cannot be Inline_Always subprogram");
775 elsif Aname
= Name_Unchecked_Access
then
776 Error_Attr
("attribute% cannot be applied to a subprogram", P
);
779 -- Issue an error if the prefix denotes an eliminated subprogram
781 Check_For_Eliminated_Subprogram
(P
, Entity
(P
));
783 -- Check for obsolescent subprogram reference
785 Check_Obsolescent_2005_Entity
(Entity
(P
), P
);
787 -- Build the appropriate subprogram type
789 Build_Access_Subprogram_Type
(P
);
791 -- For P'Access or P'Unrestricted_Access, where P is a nested
792 -- subprogram, we might be passing P to another subprogram (but we
793 -- don't check that here), which might call P. P could modify
794 -- local variables, so we need to kill current values. It is
795 -- important not to do this for library-level subprograms, because
796 -- Kill_Current_Values is very inefficient in the case of library
797 -- level packages with lots of tagged types.
799 if Is_Library_Level_Entity
(Entity
(Prefix
(N
))) then
802 -- Do not kill values on nodes initializing dispatch tables
803 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
804 -- is currently generated by the expander only for this
805 -- purpose. Done to keep the quality of warnings currently
806 -- generated by the compiler (otherwise any declaration of
807 -- a tagged type cleans constant indications from its scope).
809 elsif Nkind
(Parent
(N
)) = N_Unchecked_Type_Conversion
810 and then (Etype
(Parent
(N
)) = RTE
(RE_Prim_Ptr
)
812 Etype
(Parent
(N
)) = RTE
(RE_Size_Ptr
))
813 and then Is_Dispatching_Operation
814 (Directly_Designated_Type
(Etype
(N
)))
822 -- In the static elaboration model, treat the attribute reference
823 -- as a call for elaboration purposes. Suppress this treatment
824 -- under debug flag. In any case, we are all done.
826 if not Dynamic_Elaboration_Checks
and not Debug_Flag_Dot_UU
then
832 -- Component is an operation of a protected type
834 elsif Nkind
(P
) = N_Selected_Component
835 and then Is_Overloadable
(Entity
(Selector_Name
(P
)))
837 if Ekind
(Entity
(Selector_Name
(P
))) = E_Entry
then
838 Error_Attr_P
("prefix of % attribute must be subprogram");
841 Build_Access_Subprogram_Type
(Selector_Name
(P
));
845 -- Deal with incorrect reference to a type, but note that some
846 -- accesses are allowed: references to the current type instance,
847 -- or in Ada 2005 self-referential pointer in a default-initialized
850 if Is_Entity_Name
(P
) then
853 -- The reference may appear in an aggregate that has been expanded
854 -- into a loop. Locate scope of type definition, if any.
856 Scop
:= Current_Scope
;
857 while Ekind
(Scop
) = E_Loop
loop
858 Scop
:= Scope
(Scop
);
861 if Is_Type
(Typ
) then
863 -- OK if we are within the scope of a limited type
864 -- let's mark the component as having per object constraint
866 if Is_Anonymous_Tagged_Base
(Scop
, Typ
) then
874 Q
: Node_Id
:= Parent
(N
);
878 and then Nkind
(Q
) /= N_Component_Declaration
884 Set_Has_Per_Object_Constraint
885 (Defining_Identifier
(Q
), True);
889 if Nkind
(P
) = N_Expanded_Name
then
891 ("current instance prefix must be a direct name", P
);
894 -- If a current instance attribute appears in a component
895 -- constraint it must appear alone; other contexts (spec-
896 -- expressions, within a task body) are not subject to this
899 if not In_Spec_Expression
900 and then not Has_Completion
(Scop
)
902 Nkind_In
(Parent
(N
), N_Discriminant_Association
,
903 N_Index_Or_Discriminant_Constraint
)
906 ("current instance attribute must appear alone", N
);
909 if Is_CPP_Class
(Root_Type
(Typ
)) then
911 ("??current instance unsupported for derivations of "
912 & "'C'P'P types", N
);
915 -- OK if we are in initialization procedure for the type
916 -- in question, in which case the reference to the type
917 -- is rewritten as a reference to the current object.
919 elsif Ekind
(Scop
) = E_Procedure
920 and then Is_Init_Proc
(Scop
)
921 and then Etype
(First_Formal
(Scop
)) = Typ
924 Make_Attribute_Reference
(Loc
,
925 Prefix
=> Make_Identifier
(Loc
, Name_uInit
),
926 Attribute_Name
=> Name_Unrestricted_Access
));
930 -- OK if a task type, this test needs sharpening up ???
932 elsif Is_Task_Type
(Typ
) then
935 -- OK if self-reference in an aggregate in Ada 2005, and
936 -- the reference comes from a copied default expression.
938 -- Note that we check legality of self-reference even if the
939 -- expression comes from source, e.g. when a single component
940 -- association in an aggregate has a box association.
942 elsif Ada_Version
>= Ada_2005
943 and then OK_Self_Reference
947 -- OK if reference to current instance of a protected object
949 elsif Is_Protected_Self_Reference
(P
) then
952 -- Otherwise we have an error case
955 Error_Attr
("% attribute cannot be applied to type", P
);
961 -- If we fall through, we have a normal access to object case
963 -- Unrestricted_Access is (for now) legal wherever an allocator would
964 -- be legal, so its Etype is set to E_Allocator. The expected type
965 -- of the other attributes is a general access type, and therefore
966 -- we label them with E_Access_Attribute_Type.
968 if not Is_Overloaded
(P
) then
969 Acc_Type
:= Build_Access_Object_Type
(P_Type
);
970 Set_Etype
(N
, Acc_Type
);
974 Index
: Interp_Index
;
977 Set_Etype
(N
, Any_Type
);
978 Get_First_Interp
(P
, Index
, It
);
979 while Present
(It
.Typ
) loop
980 Acc_Type
:= Build_Access_Object_Type
(It
.Typ
);
981 Add_One_Interp
(N
, Acc_Type
, Acc_Type
);
982 Get_Next_Interp
(Index
, It
);
987 -- Special cases when we can find a prefix that is an entity name
996 if Is_Entity_Name
(PP
) then
999 -- If we have an access to an object, and the attribute
1000 -- comes from source, then set the object as potentially
1001 -- source modified. We do this because the resulting access
1002 -- pointer can be used to modify the variable, and we might
1003 -- not detect this, leading to some junk warnings.
1005 -- We only do this for source references, since otherwise
1006 -- we can suppress warnings, e.g. from the unrestricted
1007 -- access generated for validity checks in -gnatVa mode.
1009 if Comes_From_Source
(N
) then
1010 Set_Never_Set_In_Source
(Ent
, False);
1013 -- Mark entity as address taken, and kill current values
1015 Set_Address_Taken
(Ent
);
1016 Kill_Current_Values
(Ent
);
1019 elsif Nkind_In
(PP
, N_Selected_Component
,
1020 N_Indexed_Component
)
1030 -- Check for aliased view.. We allow a nonaliased prefix when within
1031 -- an instance because the prefix may have been a tagged formal
1032 -- object, which is defined to be aliased even when the actual
1033 -- might not be (other instance cases will have been caught in the
1034 -- generic). Similarly, within an inlined body we know that the
1035 -- attribute is legal in the original subprogram, and therefore
1036 -- legal in the expansion.
1038 if not Is_Aliased_View
(P
)
1039 and then not In_Instance
1040 and then not In_Inlined_Body
1041 and then Comes_From_Source
(N
)
1043 -- Here we have a non-aliased view. This is illegal unless we
1044 -- have the case of Unrestricted_Access, where for now we allow
1045 -- this (we will reject later if expected type is access to an
1046 -- unconstrained array with a thin pointer).
1048 -- No need for an error message on a generated access reference
1049 -- for the controlling argument in a dispatching call: error will
1050 -- be reported when resolving the call.
1052 if Aname
/= Name_Unrestricted_Access
then
1053 Error_Attr_P
("prefix of % attribute must be aliased");
1054 Check_No_Implicit_Aliasing
(P
);
1056 -- For Unrestricted_Access, record that prefix is not aliased
1057 -- to simplify legality check later on.
1060 Set_Non_Aliased_Prefix
(N
);
1063 -- If we have an aliased view, and we have Unrestricted_Access, then
1064 -- output a warning that Unchecked_Access would have been fine, and
1065 -- change the node to be Unchecked_Access.
1068 -- For now, hold off on this change ???
1072 end Analyze_Access_Attribute
;
1074 ---------------------------------
1075 -- Bad_Attribute_For_Predicate --
1076 ---------------------------------
1078 procedure Bad_Attribute_For_Predicate
is
1080 if Is_Scalar_Type
(P_Type
)
1081 and then Comes_From_Source
(N
)
1083 Error_Msg_Name_1
:= Aname
;
1084 Bad_Predicated_Subtype_Use
1085 ("type& has predicates, attribute % not allowed", N
, P_Type
);
1087 end Bad_Attribute_For_Predicate
;
1089 --------------------------------
1090 -- Check_Array_Or_Scalar_Type --
1091 --------------------------------
1093 procedure Check_Array_Or_Scalar_Type
is
1097 -- Dimension number for array attributes
1100 -- Case of string literal or string literal subtype. These cases
1101 -- cannot arise from legal Ada code, but the expander is allowed
1102 -- to generate them. They require special handling because string
1103 -- literal subtypes do not have standard bounds (the whole idea
1104 -- of these subtypes is to avoid having to generate the bounds)
1106 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1107 Set_Etype
(N
, Etype
(First_Index
(P_Base_Type
)));
1112 elsif Is_Scalar_Type
(P_Type
) then
1115 if Present
(E1
) then
1116 Error_Attr
("invalid argument in % attribute", E1
);
1118 Set_Etype
(N
, P_Base_Type
);
1122 -- The following is a special test to allow 'First to apply to
1123 -- private scalar types if the attribute comes from generated
1124 -- code. This occurs in the case of Normalize_Scalars code.
1126 elsif Is_Private_Type
(P_Type
)
1127 and then Present
(Full_View
(P_Type
))
1128 and then Is_Scalar_Type
(Full_View
(P_Type
))
1129 and then not Comes_From_Source
(N
)
1131 Set_Etype
(N
, Implementation_Base_Type
(P_Type
));
1133 -- Array types other than string literal subtypes handled above
1138 -- We know prefix is an array type, or the name of an array
1139 -- object, and that the expression, if present, is static
1140 -- and within the range of the dimensions of the type.
1142 pragma Assert
(Is_Array_Type
(P_Type
));
1143 Index
:= First_Index
(P_Base_Type
);
1147 -- First dimension assumed
1149 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1152 D
:= UI_To_Int
(Intval
(E1
));
1154 for J
in 1 .. D
- 1 loop
1158 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1159 Set_Etype
(E1
, Standard_Integer
);
1162 end Check_Array_Or_Scalar_Type
;
1164 ----------------------
1165 -- Check_Array_Type --
1166 ----------------------
1168 procedure Check_Array_Type
is
1170 -- Dimension number for array attributes
1173 -- If the type is a string literal type, then this must be generated
1174 -- internally, and no further check is required on its legality.
1176 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1179 -- If the type is a composite, it is an illegal aggregate, no point
1182 elsif P_Type
= Any_Composite
then
1183 raise Bad_Attribute
;
1186 -- Normal case of array type or subtype
1188 Check_Either_E0_Or_E1
;
1191 if Is_Array_Type
(P_Type
) then
1192 if not Is_Constrained
(P_Type
)
1193 and then Is_Entity_Name
(P
)
1194 and then Is_Type
(Entity
(P
))
1196 -- Note: we do not call Error_Attr here, since we prefer to
1197 -- continue, using the relevant index type of the array,
1198 -- even though it is unconstrained. This gives better error
1199 -- recovery behavior.
1201 Error_Msg_Name_1
:= Aname
;
1203 ("prefix for % attribute must be constrained array", P
);
1206 -- The attribute reference freezes the type, and thus the
1207 -- component type, even if the attribute may not depend on the
1208 -- component. Diagnose arrays with incomplete components now.
1209 -- If the prefix is an access to array, this does not freeze
1210 -- the designated type.
1212 if Nkind
(P
) /= N_Explicit_Dereference
then
1213 Check_Fully_Declared
(Component_Type
(P_Type
), P
);
1216 D
:= Number_Dimensions
(P_Type
);
1219 if Is_Private_Type
(P_Type
) then
1220 Error_Attr_P
("prefix for % attribute may not be private type");
1222 elsif Is_Access_Type
(P_Type
)
1223 and then Is_Array_Type
(Designated_Type
(P_Type
))
1224 and then Is_Entity_Name
(P
)
1225 and then Is_Type
(Entity
(P
))
1227 Error_Attr_P
("prefix of % attribute cannot be access type");
1229 elsif Attr_Id
= Attribute_First
1231 Attr_Id
= Attribute_Last
1233 Error_Attr
("invalid prefix for % attribute", P
);
1236 Error_Attr_P
("prefix for % attribute must be array");
1240 if Present
(E1
) then
1241 Resolve
(E1
, Any_Integer
);
1242 Set_Etype
(E1
, Standard_Integer
);
1244 if not Is_OK_Static_Expression
(E1
)
1245 or else Raises_Constraint_Error
(E1
)
1247 Flag_Non_Static_Expr
1248 ("expression for dimension must be static!", E1
);
1251 elsif UI_To_Int
(Expr_Value
(E1
)) > D
1252 or else UI_To_Int
(Expr_Value
(E1
)) < 1
1254 Error_Attr
("invalid dimension number for array type", E1
);
1258 if (Style_Check
and Style_Check_Array_Attribute_Index
)
1259 and then Comes_From_Source
(N
)
1261 Style
.Check_Array_Attribute_Index
(N
, E1
, D
);
1263 end Check_Array_Type
;
1265 -------------------------
1266 -- Check_Asm_Attribute --
1267 -------------------------
1269 procedure Check_Asm_Attribute
is
1274 -- Check first argument is static string expression
1276 Analyze_And_Resolve
(E1
, Standard_String
);
1278 if Etype
(E1
) = Any_Type
then
1281 elsif not Is_OK_Static_Expression
(E1
) then
1282 Flag_Non_Static_Expr
1283 ("constraint argument must be static string expression!", E1
);
1287 -- Check second argument is right type
1289 Analyze_And_Resolve
(E2
, Entity
(P
));
1291 -- Note: that is all we need to do, we don't need to check
1292 -- that it appears in a correct context. The Ada type system
1293 -- will do that for us.
1295 end Check_Asm_Attribute
;
1297 ---------------------
1298 -- Check_Component --
1299 ---------------------
1301 procedure Check_Component
is
1305 if Nkind
(P
) /= N_Selected_Component
1307 (Ekind
(Entity
(Selector_Name
(P
))) /= E_Component
1309 Ekind
(Entity
(Selector_Name
(P
))) /= E_Discriminant
)
1311 Error_Attr_P
("prefix for % attribute must be selected component");
1313 end Check_Component
;
1315 ------------------------------------
1316 -- Check_Decimal_Fixed_Point_Type --
1317 ------------------------------------
1319 procedure Check_Decimal_Fixed_Point_Type
is
1323 if not Is_Decimal_Fixed_Point_Type
(P_Type
) then
1324 Error_Attr_P
("prefix of % attribute must be decimal type");
1326 end Check_Decimal_Fixed_Point_Type
;
1328 -----------------------
1329 -- Check_Dereference --
1330 -----------------------
1332 procedure Check_Dereference
is
1335 -- Case of a subtype mark
1337 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
1341 -- Case of an expression
1345 if Is_Access_Type
(P_Type
) then
1347 -- If there is an implicit dereference, then we must freeze the
1348 -- designated type of the access type, since the type of the
1349 -- referenced array is this type (see AI95-00106).
1351 -- As done elsewhere, freezing must not happen when pre-analyzing
1352 -- a pre- or postcondition or a default value for an object or for
1353 -- a formal parameter.
1355 if not In_Spec_Expression
then
1356 Freeze_Before
(N
, Designated_Type
(P_Type
));
1360 Make_Explicit_Dereference
(Sloc
(P
),
1361 Prefix
=> Relocate_Node
(P
)));
1363 Analyze_And_Resolve
(P
);
1364 P_Type
:= Etype
(P
);
1366 if P_Type
= Any_Type
then
1367 raise Bad_Attribute
;
1370 P_Base_Type
:= Base_Type
(P_Type
);
1372 end Check_Dereference
;
1374 -------------------------
1375 -- Check_Discrete_Type --
1376 -------------------------
1378 procedure Check_Discrete_Type
is
1382 if not Is_Discrete_Type
(P_Type
) then
1383 Error_Attr_P
("prefix of % attribute must be discrete type");
1385 end Check_Discrete_Type
;
1391 procedure Check_E0
is
1393 if Present
(E1
) then
1394 Unexpected_Argument
(E1
);
1402 procedure Check_E1
is
1404 Check_Either_E0_Or_E1
;
1408 -- Special-case attributes that are functions and that appear as
1409 -- the prefix of another attribute. Error is posted on parent.
1411 if Nkind
(Parent
(N
)) = N_Attribute_Reference
1412 and then Nam_In
(Attribute_Name
(Parent
(N
)), Name_Address
,
1416 Error_Msg_Name_1
:= Attribute_Name
(Parent
(N
));
1417 Error_Msg_N
("illegal prefix for % attribute", Parent
(N
));
1418 Set_Etype
(Parent
(N
), Any_Type
);
1419 Set_Entity
(Parent
(N
), Any_Type
);
1420 raise Bad_Attribute
;
1423 Error_Attr
("missing argument for % attribute", N
);
1432 procedure Check_E2
is
1435 Error_Attr
("missing arguments for % attribute (2 required)", N
);
1437 Error_Attr
("missing argument for % attribute (2 required)", N
);
1441 ---------------------------
1442 -- Check_Either_E0_Or_E1 --
1443 ---------------------------
1445 procedure Check_Either_E0_Or_E1
is
1447 if Present
(E2
) then
1448 Unexpected_Argument
(E2
);
1450 end Check_Either_E0_Or_E1
;
1452 ----------------------
1453 -- Check_Enum_Image --
1454 ----------------------
1456 procedure Check_Enum_Image
is
1460 -- When an enumeration type appears in an attribute reference, all
1461 -- literals of the type are marked as referenced. This must only be
1462 -- done if the attribute reference appears in the current source.
1463 -- Otherwise the information on references may differ between a
1464 -- normal compilation and one that performs inlining.
1466 if Is_Enumeration_Type
(P_Base_Type
)
1467 and then In_Extended_Main_Code_Unit
(N
)
1469 Lit
:= First_Literal
(P_Base_Type
);
1470 while Present
(Lit
) loop
1471 Set_Referenced
(Lit
);
1475 end Check_Enum_Image
;
1477 ----------------------------
1478 -- Check_First_Last_Valid --
1479 ----------------------------
1481 procedure Check_First_Last_Valid
is
1483 Check_Discrete_Type
;
1485 -- Freeze the subtype now, so that the following test for predicates
1486 -- works (we set the predicates stuff up at freeze time)
1488 Insert_Actions
(N
, Freeze_Entity
(P_Type
, P
));
1490 -- Now test for dynamic predicate
1492 if Has_Predicates
(P_Type
)
1493 and then not (Has_Static_Predicate
(P_Type
))
1496 ("prefix of % attribute may not have dynamic predicate");
1499 -- Check non-static subtype
1501 if not Is_OK_Static_Subtype
(P_Type
) then
1502 Error_Attr_P
("prefix of % attribute must be a static subtype");
1505 -- Test case for no values
1507 if Expr_Value
(Type_Low_Bound
(P_Type
)) >
1508 Expr_Value
(Type_High_Bound
(P_Type
))
1509 or else (Has_Predicates
(P_Type
)
1511 Is_Empty_List
(Static_Discrete_Predicate
(P_Type
)))
1514 ("prefix of % attribute must be subtype with at least one "
1517 end Check_First_Last_Valid
;
1519 ----------------------------
1520 -- Check_Fixed_Point_Type --
1521 ----------------------------
1523 procedure Check_Fixed_Point_Type
is
1527 if not Is_Fixed_Point_Type
(P_Type
) then
1528 Error_Attr_P
("prefix of % attribute must be fixed point type");
1530 end Check_Fixed_Point_Type
;
1532 ------------------------------
1533 -- Check_Fixed_Point_Type_0 --
1534 ------------------------------
1536 procedure Check_Fixed_Point_Type_0
is
1538 Check_Fixed_Point_Type
;
1540 end Check_Fixed_Point_Type_0
;
1542 -------------------------------
1543 -- Check_Floating_Point_Type --
1544 -------------------------------
1546 procedure Check_Floating_Point_Type
is
1550 if not Is_Floating_Point_Type
(P_Type
) then
1551 Error_Attr_P
("prefix of % attribute must be float type");
1553 end Check_Floating_Point_Type
;
1555 ---------------------------------
1556 -- Check_Floating_Point_Type_0 --
1557 ---------------------------------
1559 procedure Check_Floating_Point_Type_0
is
1561 Check_Floating_Point_Type
;
1563 end Check_Floating_Point_Type_0
;
1565 ---------------------------------
1566 -- Check_Floating_Point_Type_1 --
1567 ---------------------------------
1569 procedure Check_Floating_Point_Type_1
is
1571 Check_Floating_Point_Type
;
1573 end Check_Floating_Point_Type_1
;
1575 ---------------------------------
1576 -- Check_Floating_Point_Type_2 --
1577 ---------------------------------
1579 procedure Check_Floating_Point_Type_2
is
1581 Check_Floating_Point_Type
;
1583 end Check_Floating_Point_Type_2
;
1585 ------------------------
1586 -- Check_Integer_Type --
1587 ------------------------
1589 procedure Check_Integer_Type
is
1593 if not Is_Integer_Type
(P_Type
) then
1594 Error_Attr_P
("prefix of % attribute must be integer type");
1596 end Check_Integer_Type
;
1598 --------------------------------
1599 -- Check_Modular_Integer_Type --
1600 --------------------------------
1602 procedure Check_Modular_Integer_Type
is
1606 if not Is_Modular_Integer_Type
(P_Type
) then
1608 ("prefix of % attribute must be modular integer type");
1610 end Check_Modular_Integer_Type
;
1612 ------------------------
1613 -- Check_Not_CPP_Type --
1614 ------------------------
1616 procedure Check_Not_CPP_Type
is
1618 if Is_Tagged_Type
(Etype
(P
))
1619 and then Convention
(Etype
(P
)) = Convention_CPP
1620 and then Is_CPP_Class
(Root_Type
(Etype
(P
)))
1623 ("invalid use of % attribute with 'C'P'P tagged type");
1625 end Check_Not_CPP_Type
;
1627 -------------------------------
1628 -- Check_Not_Incomplete_Type --
1629 -------------------------------
1631 procedure Check_Not_Incomplete_Type
is
1636 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1637 -- dereference we have to check wrong uses of incomplete types
1638 -- (other wrong uses are checked at their freezing point).
1640 -- Example 1: Limited-with
1642 -- limited with Pkg;
1644 -- type Acc is access Pkg.T;
1646 -- S : Integer := X.all'Size; -- ERROR
1649 -- Example 2: Tagged incomplete
1651 -- type T is tagged;
1652 -- type Acc is access all T;
1654 -- S : constant Integer := X.all'Size; -- ERROR
1655 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1657 if Ada_Version
>= Ada_2005
1658 and then Nkind
(P
) = N_Explicit_Dereference
1661 while Nkind
(E
) = N_Explicit_Dereference
loop
1667 if From_Limited_With
(Typ
) then
1669 ("prefix of % attribute cannot be an incomplete type");
1672 if Is_Access_Type
(Typ
) then
1673 Typ
:= Directly_Designated_Type
(Typ
);
1676 if Is_Class_Wide_Type
(Typ
) then
1677 Typ
:= Root_Type
(Typ
);
1680 -- A legal use of a shadow entity occurs only when the unit
1681 -- where the non-limited view resides is imported via a regular
1682 -- with clause in the current body. Such references to shadow
1683 -- entities may occur in subprogram formals.
1685 if Is_Incomplete_Type
(Typ
)
1686 and then From_Limited_With
(Typ
)
1687 and then Present
(Non_Limited_View
(Typ
))
1688 and then Is_Legal_Shadow_Entity_In_Body
(Typ
)
1690 Typ
:= Non_Limited_View
(Typ
);
1693 if Ekind
(Typ
) = E_Incomplete_Type
1694 and then No
(Full_View
(Typ
))
1697 ("prefix of % attribute cannot be an incomplete type");
1702 if not Is_Entity_Name
(P
)
1703 or else not Is_Type
(Entity
(P
))
1704 or else In_Spec_Expression
1708 Check_Fully_Declared
(P_Type
, P
);
1710 end Check_Not_Incomplete_Type
;
1712 ----------------------------
1713 -- Check_Object_Reference --
1714 ----------------------------
1716 procedure Check_Object_Reference
(P
: Node_Id
) is
1720 -- If we need an object, and we have a prefix that is the name of
1721 -- a function entity, convert it into a function call.
1723 if Is_Entity_Name
(P
)
1724 and then Ekind
(Entity
(P
)) = E_Function
1726 Rtyp
:= Etype
(Entity
(P
));
1729 Make_Function_Call
(Sloc
(P
),
1730 Name
=> Relocate_Node
(P
)));
1732 Analyze_And_Resolve
(P
, Rtyp
);
1734 -- Otherwise we must have an object reference
1736 elsif not Is_Object_Reference
(P
) then
1737 Error_Attr_P
("prefix of % attribute must be object");
1739 end Check_Object_Reference
;
1741 ----------------------------
1742 -- Check_PolyORB_Attribute --
1743 ----------------------------
1745 procedure Check_PolyORB_Attribute
is
1747 Validate_Non_Static_Attribute_Function_Call
;
1752 if Get_PCS_Name
/= Name_PolyORB_DSA
then
1754 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N
);
1756 end Check_PolyORB_Attribute
;
1758 ------------------------
1759 -- Check_Program_Unit --
1760 ------------------------
1762 procedure Check_Program_Unit
is
1764 if Is_Entity_Name
(P
) then
1766 K
: constant Entity_Kind
:= Ekind
(Entity
(P
));
1767 T
: constant Entity_Id
:= Etype
(Entity
(P
));
1770 if K
in Subprogram_Kind
1771 or else K
in Task_Kind
1772 or else K
in Protected_Kind
1773 or else K
= E_Package
1774 or else K
in Generic_Unit_Kind
1775 or else (K
= E_Variable
1779 Is_Protected_Type
(T
)))
1786 Error_Attr_P
("prefix of % attribute must be program unit");
1787 end Check_Program_Unit
;
1789 ---------------------
1790 -- Check_Real_Type --
1791 ---------------------
1793 procedure Check_Real_Type
is
1797 if not Is_Real_Type
(P_Type
) then
1798 Error_Attr_P
("prefix of % attribute must be real type");
1800 end Check_Real_Type
;
1802 -----------------------
1803 -- Check_Scalar_Type --
1804 -----------------------
1806 procedure Check_Scalar_Type
is
1810 if not Is_Scalar_Type
(P_Type
) then
1811 Error_Attr_P
("prefix of % attribute must be scalar type");
1813 end Check_Scalar_Type
;
1815 ------------------------------------------
1816 -- Check_SPARK_05_Restriction_On_Attribute --
1817 ------------------------------------------
1819 procedure Check_SPARK_05_Restriction_On_Attribute
is
1821 Error_Msg_Name_1
:= Aname
;
1822 Check_SPARK_05_Restriction
("attribute % is not allowed", P
);
1823 end Check_SPARK_05_Restriction_On_Attribute
;
1825 ---------------------------
1826 -- Check_Standard_Prefix --
1827 ---------------------------
1829 procedure Check_Standard_Prefix
is
1833 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_Standard
then
1834 Error_Attr
("only allowed prefix for % attribute is Standard", P
);
1836 end Check_Standard_Prefix
;
1838 ----------------------------
1839 -- Check_Stream_Attribute --
1840 ----------------------------
1842 procedure Check_Stream_Attribute
(Nam
: TSS_Name_Type
) is
1846 In_Shared_Var_Procs
: Boolean;
1847 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
1848 -- For this runtime package (always compiled in GNAT mode), we allow
1849 -- stream attributes references for limited types for the case where
1850 -- shared passive objects are implemented using stream attributes,
1851 -- which is the default in GNAT's persistent storage implementation.
1854 Validate_Non_Static_Attribute_Function_Call
;
1856 -- With the exception of 'Input, Stream attributes are procedures,
1857 -- and can only appear at the position of procedure calls. We check
1858 -- for this here, before they are rewritten, to give a more precise
1861 if Nam
= TSS_Stream_Input
then
1864 elsif Is_List_Member
(N
)
1865 and then not Nkind_In
(Parent
(N
), N_Procedure_Call_Statement
,
1872 ("invalid context for attribute%, which is a procedure", N
);
1876 Btyp
:= Implementation_Base_Type
(P_Type
);
1878 -- Stream attributes not allowed on limited types unless the
1879 -- attribute reference was generated by the expander (in which
1880 -- case the underlying type will be used, as described in Sinfo),
1881 -- or the attribute was specified explicitly for the type itself
1882 -- or one of its ancestors (taking visibility rules into account if
1883 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1884 -- (with no visibility restriction).
1887 Gen_Body
: constant Node_Id
:= Enclosing_Generic_Body
(N
);
1889 if Present
(Gen_Body
) then
1890 In_Shared_Var_Procs
:=
1891 Is_RTE
(Corresponding_Spec
(Gen_Body
), RE_Shared_Var_Procs
);
1893 In_Shared_Var_Procs
:= False;
1897 if (Comes_From_Source
(N
)
1898 and then not (In_Shared_Var_Procs
or In_Instance
))
1899 and then not Stream_Attribute_Available
(P_Type
, Nam
)
1900 and then not Has_Rep_Pragma
(Btyp
, Name_Stream_Convert
)
1902 Error_Msg_Name_1
:= Aname
;
1904 if Is_Limited_Type
(P_Type
) then
1906 ("limited type& has no% attribute", P
, P_Type
);
1907 Explain_Limited_Type
(P_Type
, P
);
1910 ("attribute% for type& is not available", P
, P_Type
);
1914 -- Check for no stream operations allowed from No_Tagged_Streams
1916 if Is_Tagged_Type
(P_Type
)
1917 and then Present
(No_Tagged_Streams_Pragma
(P_Type
))
1919 Error_Msg_Sloc
:= Sloc
(No_Tagged_Streams_Pragma
(P_Type
));
1921 ("no stream operations for & (No_Tagged_Streams #)", N
, P_Type
);
1925 -- Check restriction violations
1927 -- First check the No_Streams restriction, which prohibits the use
1928 -- of explicit stream attributes in the source program. We do not
1929 -- prevent the occurrence of stream attributes in generated code,
1930 -- for instance those generated implicitly for dispatching purposes.
1932 if Comes_From_Source
(N
) then
1933 Check_Restriction
(No_Streams
, P
);
1936 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1937 -- it is illegal to use a predefined elementary type stream attribute
1938 -- either by itself, or more importantly as part of the attribute
1939 -- subprogram for a composite type. However, if the broader
1940 -- restriction No_Streams is active, stream operations are not
1941 -- generated, and there is no error.
1943 if Restriction_Active
(No_Default_Stream_Attributes
)
1944 and then not Restriction_Active
(No_Streams
)
1950 if Nam
= TSS_Stream_Input
1952 Nam
= TSS_Stream_Read
1955 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Read
);
1958 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Write
);
1962 Check_Restriction
(No_Default_Stream_Attributes
, N
);
1965 ("missing user-defined Stream Read or Write for type&",
1967 if not Is_Elementary_Type
(P_Type
) then
1969 ("\which is a component of type&", N
, P_Type
);
1975 -- Check special case of Exception_Id and Exception_Occurrence which
1976 -- are not allowed for restriction No_Exception_Registration.
1978 if Restriction_Check_Required
(No_Exception_Registration
)
1979 and then (Is_RTE
(P_Type
, RE_Exception_Id
)
1981 Is_RTE
(P_Type
, RE_Exception_Occurrence
))
1983 Check_Restriction
(No_Exception_Registration
, P
);
1986 -- Here we must check that the first argument is an access type
1987 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1989 Analyze_And_Resolve
(E1
);
1992 -- Note: the double call to Root_Type here is needed because the
1993 -- root type of a class-wide type is the corresponding type (e.g.
1994 -- X for X'Class, and we really want to go to the root.)
1996 if not Is_Access_Type
(Etyp
)
1997 or else Root_Type
(Root_Type
(Designated_Type
(Etyp
))) /=
1998 RTE
(RE_Root_Stream_Type
)
2001 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1
);
2004 -- Check that the second argument is of the right type if there is
2005 -- one (the Input attribute has only one argument so this is skipped)
2007 if Present
(E2
) then
2010 if Nam
= TSS_Stream_Read
2011 and then not Is_OK_Variable_For_Out_Formal
(E2
)
2014 ("second argument of % attribute must be a variable", E2
);
2017 Resolve
(E2
, P_Type
);
2021 end Check_Stream_Attribute
;
2023 -------------------------
2024 -- Check_System_Prefix --
2025 -------------------------
2027 procedure Check_System_Prefix
is
2029 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_System
then
2030 Error_Attr
("only allowed prefix for % attribute is System", P
);
2032 end Check_System_Prefix
;
2034 -----------------------
2035 -- Check_Task_Prefix --
2036 -----------------------
2038 procedure Check_Task_Prefix
is
2042 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2043 -- task interface class-wide types.
2045 if Is_Task_Type
(Etype
(P
))
2046 or else (Is_Access_Type
(Etype
(P
))
2047 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
2048 or else (Ada_Version
>= Ada_2005
2049 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
2050 and then Is_Interface
(Etype
(P
))
2051 and then Is_Task_Interface
(Etype
(P
)))
2056 if Ada_Version
>= Ada_2005
then
2058 ("prefix of % attribute must be a task or a task " &
2059 "interface class-wide object");
2062 Error_Attr_P
("prefix of % attribute must be a task");
2065 end Check_Task_Prefix
;
2071 -- The possibilities are an entity name denoting a type, or an
2072 -- attribute reference that denotes a type (Base or Class). If
2073 -- the type is incomplete, replace it with its full view.
2075 procedure Check_Type
is
2077 if not Is_Entity_Name
(P
)
2078 or else not Is_Type
(Entity
(P
))
2080 Error_Attr_P
("prefix of % attribute must be a type");
2082 elsif Is_Protected_Self_Reference
(P
) then
2084 ("prefix of % attribute denotes current instance "
2085 & "(RM 9.4(21/2))");
2087 elsif Ekind
(Entity
(P
)) = E_Incomplete_Type
2088 and then Present
(Full_View
(Entity
(P
)))
2090 P_Type
:= Full_View
(Entity
(P
));
2091 Set_Entity
(P
, P_Type
);
2095 ---------------------
2096 -- Check_Unit_Name --
2097 ---------------------
2099 procedure Check_Unit_Name
(Nod
: Node_Id
) is
2101 if Nkind
(Nod
) = N_Identifier
then
2104 elsif Nkind_In
(Nod
, N_Selected_Component
, N_Expanded_Name
) then
2105 Check_Unit_Name
(Prefix
(Nod
));
2107 if Nkind
(Selector_Name
(Nod
)) = N_Identifier
then
2112 Error_Attr
("argument for % attribute must be unit name", P
);
2113 end Check_Unit_Name
;
2119 procedure Error_Attr
is
2121 Set_Etype
(N
, Any_Type
);
2122 Set_Entity
(N
, Any_Type
);
2123 raise Bad_Attribute
;
2126 procedure Error_Attr
(Msg
: String; Error_Node
: Node_Id
) is
2128 Error_Msg_Name_1
:= Aname
;
2129 Error_Msg_N
(Msg
, Error_Node
);
2137 procedure Error_Attr_P
(Msg
: String) is
2139 Error_Msg_Name_1
:= Aname
;
2140 Error_Msg_F
(Msg
, P
);
2144 ---------------------
2145 -- In_Refined_Post --
2146 ---------------------
2148 function In_Refined_Post
return Boolean is
2149 function Is_Refined_Post
(Prag
: Node_Id
) return Boolean;
2150 -- Determine whether Prag denotes one of the incarnations of pragma
2151 -- Refined_Post (either as is or pragma Check (Refined_Post, ...).
2153 ---------------------
2154 -- Is_Refined_Post --
2155 ---------------------
2157 function Is_Refined_Post
(Prag
: Node_Id
) return Boolean is
2158 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
2159 Nam
: constant Name_Id
:= Pragma_Name
(Prag
);
2162 if Nam
= Name_Refined_Post
then
2165 elsif Nam
= Name_Check
then
2166 pragma Assert
(Present
(Args
));
2168 return Chars
(Expression
(First
(Args
))) = Name_Refined_Post
;
2172 end Is_Refined_Post
;
2178 -- Start of processing for In_Refined_Post
2182 while Present
(Stmt
) loop
2183 if Nkind
(Stmt
) = N_Pragma
and then Is_Refined_Post
(Stmt
) then
2186 -- Prevent the search from going too far
2188 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
2192 Stmt
:= Parent
(Stmt
);
2196 end In_Refined_Post
;
2198 ----------------------------
2199 -- Legal_Formal_Attribute --
2200 ----------------------------
2202 procedure Legal_Formal_Attribute
is
2206 if not Is_Entity_Name
(P
)
2207 or else not Is_Type
(Entity
(P
))
2209 Error_Attr_P
("prefix of % attribute must be generic type");
2211 elsif Is_Generic_Actual_Type
(Entity
(P
))
2213 or else In_Inlined_Body
2217 elsif Is_Generic_Type
(Entity
(P
)) then
2218 if not Is_Indefinite_Subtype
(Entity
(P
)) then
2220 ("prefix of % attribute must be indefinite generic type");
2225 ("prefix of % attribute must be indefinite generic type");
2228 Set_Etype
(N
, Standard_Boolean
);
2229 end Legal_Formal_Attribute
;
2231 ---------------------------------------------------------------
2232 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2233 ---------------------------------------------------------------
2235 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
is
2239 Check_Not_Incomplete_Type
;
2240 Set_Etype
(N
, Universal_Integer
);
2241 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
2247 procedure Min_Max
is
2251 Resolve
(E1
, P_Base_Type
);
2252 Resolve
(E2
, P_Base_Type
);
2253 Set_Etype
(N
, P_Base_Type
);
2255 -- Check for comparison on unordered enumeration type
2257 if Bad_Unordered_Enumeration_Reference
(N
, P_Base_Type
) then
2258 Error_Msg_Sloc
:= Sloc
(P_Base_Type
);
2260 ("comparison on unordered enumeration type& declared#?U?",
2265 ------------------------
2266 -- Standard_Attribute --
2267 ------------------------
2269 procedure Standard_Attribute
(Val
: Int
) is
2271 Check_Standard_Prefix
;
2272 Rewrite
(N
, Make_Integer_Literal
(Loc
, Val
));
2274 Set_Is_Static_Expression
(N
, True);
2275 end Standard_Attribute
;
2277 --------------------
2278 -- Uneval_Old_Msg --
2279 --------------------
2281 procedure Uneval_Old_Msg
is
2282 Uneval_Old_Setting
: Character;
2286 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2287 -- N_Aspect_Specification node that corresponds to the attribute.
2289 -- First find the pragma in which we appear (note that at this stage,
2290 -- even if we appeared originally within an aspect specification, we
2291 -- are now within the corresponding pragma).
2295 Prag
:= Parent
(Prag
);
2296 exit when No
(Prag
) or else Nkind
(Prag
) = N_Pragma
;
2299 if Present
(Prag
) then
2300 if Uneval_Old_Accept
(Prag
) then
2301 Uneval_Old_Setting
:= 'A';
2302 elsif Uneval_Old_Warn
(Prag
) then
2303 Uneval_Old_Setting
:= 'W';
2305 Uneval_Old_Setting
:= 'E';
2308 -- If we did not find the pragma, that's odd, just use the setting
2309 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2312 Uneval_Old_Setting
:= Opt
.Uneval_Old
;
2315 -- Processing depends on the setting of Uneval_Old
2317 case Uneval_Old_Setting
is
2320 ("prefix of attribute % that is potentially "
2321 & "unevaluated must denote an entity");
2324 Error_Msg_Name_1
:= Aname
;
2326 ("??prefix of attribute % appears in potentially "
2327 & "unevaluated context, exception may be raised", P
);
2333 raise Program_Error
;
2337 -------------------------
2338 -- Unexpected Argument --
2339 -------------------------
2341 procedure Unexpected_Argument
(En
: Node_Id
) is
2343 Error_Attr
("unexpected argument for % attribute", En
);
2344 end Unexpected_Argument
;
2346 -------------------------------------------------
2347 -- Validate_Non_Static_Attribute_Function_Call --
2348 -------------------------------------------------
2350 -- This function should be moved to Sem_Dist ???
2352 procedure Validate_Non_Static_Attribute_Function_Call
is
2354 if In_Preelaborated_Unit
2355 and then not In_Subprogram_Or_Concurrent_Unit
2357 Flag_Non_Static_Expr
2358 ("non-static function call in preelaborated unit!", N
);
2360 end Validate_Non_Static_Attribute_Function_Call
;
2362 -- Start of processing for Analyze_Attribute
2365 -- Immediate return if unrecognized attribute (already diagnosed
2366 -- by parser, so there is nothing more that we need to do)
2368 if not Is_Attribute_Name
(Aname
) then
2369 raise Bad_Attribute
;
2372 -- Deal with Ada 83 issues
2374 if Comes_From_Source
(N
) then
2375 if not Attribute_83
(Attr_Id
) then
2376 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
2377 Error_Msg_Name_1
:= Aname
;
2378 Error_Msg_N
("(Ada 83) attribute% is not standard??", N
);
2381 if Attribute_Impl_Def
(Attr_Id
) then
2382 Check_Restriction
(No_Implementation_Attributes
, N
);
2387 -- Deal with Ada 2005 attributes that are implementation attributes
2388 -- because they appear in a version of Ada before Ada 2005, and
2389 -- similarly for Ada 2012 attributes appearing in an earlier version.
2391 if (Attribute_05
(Attr_Id
) and then Ada_Version
< Ada_2005
)
2393 (Attribute_12
(Attr_Id
) and then Ada_Version
< Ada_2012
)
2395 Check_Restriction
(No_Implementation_Attributes
, N
);
2398 -- Remote access to subprogram type access attribute reference needs
2399 -- unanalyzed copy for tree transformation. The analyzed copy is used
2400 -- for its semantic information (whether prefix is a remote subprogram
2401 -- name), the unanalyzed copy is used to construct new subtree rooted
2402 -- with N_Aggregate which represents a fat pointer aggregate.
2404 if Aname
= Name_Access
then
2405 Discard_Node
(Copy_Separate_Tree
(N
));
2408 -- Analyze prefix and exit if error in analysis. If the prefix is an
2409 -- incomplete type, use full view if available. Note that there are
2410 -- some attributes for which we do not analyze the prefix, since the
2411 -- prefix is not a normal name, or else needs special handling.
2413 if Aname
/= Name_Elab_Body
and then
2414 Aname
/= Name_Elab_Spec
and then
2415 Aname
/= Name_Elab_Subp_Body
and then
2416 Aname
/= Name_UET_Address
and then
2417 Aname
/= Name_Enabled
and then
2421 P_Type
:= Etype
(P
);
2423 if Is_Entity_Name
(P
)
2424 and then Present
(Entity
(P
))
2425 and then Is_Type
(Entity
(P
))
2427 if Ekind
(Entity
(P
)) = E_Incomplete_Type
then
2428 P_Type
:= Get_Full_View
(P_Type
);
2429 Set_Entity
(P
, P_Type
);
2430 Set_Etype
(P
, P_Type
);
2432 elsif Entity
(P
) = Current_Scope
2433 and then Is_Record_Type
(Entity
(P
))
2435 -- Use of current instance within the type. Verify that if the
2436 -- attribute appears within a constraint, it yields an access
2437 -- type, other uses are illegal.
2445 and then Nkind
(Parent
(Par
)) /= N_Component_Definition
2447 Par
:= Parent
(Par
);
2451 and then Nkind
(Par
) = N_Subtype_Indication
2453 if Attr_Id
/= Attribute_Access
2454 and then Attr_Id
/= Attribute_Unchecked_Access
2455 and then Attr_Id
/= Attribute_Unrestricted_Access
2458 ("in a constraint the current instance can only "
2459 & "be used with an access attribute", N
);
2466 if P_Type
= Any_Type
then
2467 raise Bad_Attribute
;
2470 P_Base_Type
:= Base_Type
(P_Type
);
2473 -- Analyze expressions that may be present, exiting if an error occurs
2480 E1
:= First
(Exprs
);
2482 -- Skip analysis for case of Restriction_Set, we do not expect
2483 -- the argument to be analyzed in this case.
2485 if Aname
/= Name_Restriction_Set
then
2488 -- Check for missing/bad expression (result of previous error)
2490 if No
(E1
) or else Etype
(E1
) = Any_Type
then
2491 raise Bad_Attribute
;
2497 if Present
(E2
) then
2500 if Etype
(E2
) = Any_Type
then
2501 raise Bad_Attribute
;
2504 if Present
(Next
(E2
)) then
2505 Unexpected_Argument
(Next
(E2
));
2510 -- Cases where prefix must be resolvable by itself
2512 if Is_Overloaded
(P
)
2513 and then Aname
/= Name_Access
2514 and then Aname
/= Name_Address
2515 and then Aname
/= Name_Code_Address
2516 and then Aname
/= Name_Result
2517 and then Aname
/= Name_Unchecked_Access
2519 -- The prefix must be resolvable by itself, without reference to the
2520 -- attribute. One case that requires special handling is a prefix
2521 -- that is a function name, where one interpretation may be a
2522 -- parameterless call. Entry attributes are handled specially below.
2524 if Is_Entity_Name
(P
)
2525 and then not Nam_In
(Aname
, Name_Count
, Name_Caller
)
2527 Check_Parameterless_Call
(P
);
2530 if Is_Overloaded
(P
) then
2532 -- Ada 2005 (AI-345): Since protected and task types have
2533 -- primitive entry wrappers, the attributes Count, and Caller
2534 -- require a context check
2536 if Nam_In
(Aname
, Name_Count
, Name_Caller
) then
2538 Count
: Natural := 0;
2543 Get_First_Interp
(P
, I
, It
);
2544 while Present
(It
.Nam
) loop
2545 if Comes_From_Source
(It
.Nam
) then
2551 Get_Next_Interp
(I
, It
);
2555 Error_Attr
("ambiguous prefix for % attribute", P
);
2557 Set_Is_Overloaded
(P
, False);
2562 Error_Attr
("ambiguous prefix for % attribute", P
);
2567 -- In SPARK, attributes of private types are only allowed if the full
2568 -- type declaration is visible.
2570 -- Note: the check for Present (Entity (P)) defends against some error
2571 -- conditions where the Entity field is not set.
2573 if Is_Entity_Name
(P
) and then Present
(Entity
(P
))
2574 and then Is_Type
(Entity
(P
))
2575 and then Is_Private_Type
(P_Type
)
2576 and then not In_Open_Scopes
(Scope
(P_Type
))
2577 and then not In_Spec_Expression
2579 Check_SPARK_05_Restriction
("invisible attribute of type", N
);
2582 -- Remaining processing depends on attribute
2586 -- Attributes related to Ada 2012 iterators. Attribute specifications
2587 -- exist for these, but they cannot be queried.
2589 when Attribute_Constant_Indexing |
2590 Attribute_Default_Iterator |
2591 Attribute_Implicit_Dereference |
2592 Attribute_Iterator_Element |
2593 Attribute_Iterable |
2594 Attribute_Variable_Indexing
=>
2595 Error_Msg_N
("illegal attribute", N
);
2597 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2598 -- were already rejected by the parser. Thus they shouldn't appear here.
2600 when Internal_Attribute_Id
=>
2601 raise Program_Error
;
2607 when Attribute_Abort_Signal
=>
2608 Check_Standard_Prefix
;
2609 Rewrite
(N
, New_Occurrence_Of
(Stand
.Abort_Signal
, Loc
));
2616 when Attribute_Access
=>
2617 Analyze_Access_Attribute
;
2623 when Attribute_Address
=>
2626 Set_Etype
(N
, RTE
(RE_Address
));
2632 when Attribute_Address_Size
=>
2633 Standard_Attribute
(System_Address_Size
);
2639 when Attribute_Adjacent
=>
2640 Check_Floating_Point_Type_2
;
2641 Set_Etype
(N
, P_Base_Type
);
2642 Resolve
(E1
, P_Base_Type
);
2643 Resolve
(E2
, P_Base_Type
);
2649 when Attribute_Aft
=>
2650 Check_Fixed_Point_Type_0
;
2651 Set_Etype
(N
, Universal_Integer
);
2657 when Attribute_Alignment
=>
2659 -- Don't we need more checking here, cf Size ???
2662 Check_Not_Incomplete_Type
;
2664 Set_Etype
(N
, Universal_Integer
);
2670 when Attribute_Asm_Input
=>
2671 Check_Asm_Attribute
;
2673 -- The back-end may need to take the address of E2
2675 if Is_Entity_Name
(E2
) then
2676 Set_Address_Taken
(Entity
(E2
));
2679 Set_Etype
(N
, RTE
(RE_Asm_Input_Operand
));
2685 when Attribute_Asm_Output
=>
2686 Check_Asm_Attribute
;
2688 if Etype
(E2
) = Any_Type
then
2691 elsif Aname
= Name_Asm_Output
then
2692 if not Is_Variable
(E2
) then
2694 ("second argument for Asm_Output is not variable", E2
);
2698 Note_Possible_Modification
(E2
, Sure
=> True);
2700 -- The back-end may need to take the address of E2
2702 if Is_Entity_Name
(E2
) then
2703 Set_Address_Taken
(Entity
(E2
));
2706 Set_Etype
(N
, RTE
(RE_Asm_Output_Operand
));
2708 -----------------------------
2709 -- Atomic_Always_Lock_Free --
2710 -----------------------------
2712 when Attribute_Atomic_Always_Lock_Free
=>
2715 Set_Etype
(N
, Standard_Boolean
);
2721 -- Note: when the base attribute appears in the context of a subtype
2722 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2723 -- the following circuit.
2725 when Attribute_Base
=> Base
: declare
2733 if Ada_Version
>= Ada_95
2734 and then not Is_Scalar_Type
(Typ
)
2735 and then not Is_Generic_Type
(Typ
)
2737 Error_Attr_P
("prefix of Base attribute must be scalar type");
2739 elsif Sloc
(Typ
) = Standard_Location
2740 and then Base_Type
(Typ
) = Typ
2741 and then Warn_On_Redundant_Constructs
2743 Error_Msg_NE
-- CODEFIX
2744 ("?r?redundant attribute, & is its own base type", N
, Typ
);
2747 if Nkind
(Parent
(N
)) /= N_Attribute_Reference
then
2748 Error_Msg_Name_1
:= Aname
;
2749 Check_SPARK_05_Restriction
2750 ("attribute% is only allowed as prefix of another attribute", P
);
2753 Set_Etype
(N
, Base_Type
(Entity
(P
)));
2754 Set_Entity
(N
, Base_Type
(Entity
(P
)));
2755 Rewrite
(N
, New_Occurrence_Of
(Entity
(N
), Loc
));
2763 when Attribute_Bit
=> Bit
:
2767 if not Is_Object_Reference
(P
) then
2768 Error_Attr_P
("prefix for % attribute must be object");
2770 -- What about the access object cases ???
2776 Set_Etype
(N
, Universal_Integer
);
2783 when Attribute_Bit_Order
=> Bit_Order
:
2788 if not Is_Record_Type
(P_Type
) then
2789 Error_Attr_P
("prefix of % attribute must be record type");
2792 if Bytes_Big_Endian
xor Reverse_Bit_Order
(P_Type
) then
2794 New_Occurrence_Of
(RTE
(RE_High_Order_First
), Loc
));
2797 New_Occurrence_Of
(RTE
(RE_Low_Order_First
), Loc
));
2800 Set_Etype
(N
, RTE
(RE_Bit_Order
));
2803 -- Reset incorrect indication of staticness
2805 Set_Is_Static_Expression
(N
, False);
2812 -- Note: in generated code, we can have a Bit_Position attribute
2813 -- applied to a (naked) record component (i.e. the prefix is an
2814 -- identifier that references an E_Component or E_Discriminant
2815 -- entity directly, and this is interpreted as expected by Gigi.
2816 -- The following code will not tolerate such usage, but when the
2817 -- expander creates this special case, it marks it as analyzed
2818 -- immediately and sets an appropriate type.
2820 when Attribute_Bit_Position
=>
2821 if Comes_From_Source
(N
) then
2825 Set_Etype
(N
, Universal_Integer
);
2831 when Attribute_Body_Version
=>
2834 Set_Etype
(N
, RTE
(RE_Version_String
));
2840 when Attribute_Callable
=>
2842 Set_Etype
(N
, Standard_Boolean
);
2849 when Attribute_Caller
=> Caller
: declare
2856 if Nkind_In
(P
, N_Identifier
, N_Expanded_Name
) then
2859 if not Is_Entry
(Ent
) then
2860 Error_Attr
("invalid entry name", N
);
2864 Error_Attr
("invalid entry name", N
);
2868 for J
in reverse 0 .. Scope_Stack
.Last
loop
2869 S
:= Scope_Stack
.Table
(J
).Entity
;
2871 if S
= Scope
(Ent
) then
2872 Error_Attr
("Caller must appear in matching accept or body", N
);
2878 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
2885 when Attribute_Ceiling
=>
2886 Check_Floating_Point_Type_1
;
2887 Set_Etype
(N
, P_Base_Type
);
2888 Resolve
(E1
, P_Base_Type
);
2894 when Attribute_Class
=>
2895 Check_Restriction
(No_Dispatch
, N
);
2899 -- Applying Class to untagged incomplete type is obsolescent in Ada
2900 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2901 -- this flag gets set by Find_Type in this situation.
2903 if Restriction_Check_Required
(No_Obsolescent_Features
)
2904 and then Ada_Version
>= Ada_2005
2905 and then Ekind
(P_Type
) = E_Incomplete_Type
2908 DN
: constant Node_Id
:= Declaration_Node
(P_Type
);
2910 if Nkind
(DN
) = N_Incomplete_Type_Declaration
2911 and then not Tagged_Present
(DN
)
2913 Check_Restriction
(No_Obsolescent_Features
, P
);
2922 when Attribute_Code_Address
=>
2925 if Nkind
(P
) = N_Attribute_Reference
2926 and then Nam_In
(Attribute_Name
(P
), Name_Elab_Body
, Name_Elab_Spec
)
2930 elsif not Is_Entity_Name
(P
)
2931 or else (Ekind
(Entity
(P
)) /= E_Function
2933 Ekind
(Entity
(P
)) /= E_Procedure
)
2935 Error_Attr
("invalid prefix for % attribute", P
);
2936 Set_Address_Taken
(Entity
(P
));
2938 -- Issue an error if the prefix denotes an eliminated subprogram
2941 Check_For_Eliminated_Subprogram
(P
, Entity
(P
));
2944 Set_Etype
(N
, RTE
(RE_Address
));
2946 ----------------------
2947 -- Compiler_Version --
2948 ----------------------
2950 when Attribute_Compiler_Version
=>
2952 Check_Standard_Prefix
;
2953 Rewrite
(N
, Make_String_Literal
(Loc
, "GNAT " & Gnat_Version_String
));
2954 Analyze_And_Resolve
(N
, Standard_String
);
2955 Set_Is_Static_Expression
(N
, True);
2957 --------------------
2958 -- Component_Size --
2959 --------------------
2961 when Attribute_Component_Size
=>
2963 Set_Etype
(N
, Universal_Integer
);
2965 -- Note: unlike other array attributes, unconstrained arrays are OK
2967 if Is_Array_Type
(P_Type
) and then not Is_Constrained
(P_Type
) then
2977 when Attribute_Compose
=>
2978 Check_Floating_Point_Type_2
;
2979 Set_Etype
(N
, P_Base_Type
);
2980 Resolve
(E1
, P_Base_Type
);
2981 Resolve
(E2
, Any_Integer
);
2987 when Attribute_Constrained
=>
2989 Set_Etype
(N
, Standard_Boolean
);
2991 -- Case from RM J.4(2) of constrained applied to private type
2993 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
2994 Check_Restriction
(No_Obsolescent_Features
, P
);
2996 if Warn_On_Obsolescent_Feature
then
2998 ("constrained for private type is an " &
2999 "obsolescent feature (RM J.4)?j?", N
);
3002 -- If we are within an instance, the attribute must be legal
3003 -- because it was valid in the generic unit. Ditto if this is
3004 -- an inlining of a function declared in an instance.
3006 if In_Instance
or else In_Inlined_Body
then
3009 -- For sure OK if we have a real private type itself, but must
3010 -- be completed, cannot apply Constrained to incomplete type.
3012 elsif Is_Private_Type
(Entity
(P
)) then
3014 -- Note: this is one of the Annex J features that does not
3015 -- generate a warning from -gnatwj, since in fact it seems
3016 -- very useful, and is used in the GNAT runtime.
3018 Check_Not_Incomplete_Type
;
3022 -- Normal (non-obsolescent case) of application to object of
3023 -- a discriminated type.
3026 Check_Object_Reference
(P
);
3028 -- If N does not come from source, then we allow the
3029 -- the attribute prefix to be of a private type whose
3030 -- full type has discriminants. This occurs in cases
3031 -- involving expanded calls to stream attributes.
3033 if not Comes_From_Source
(N
) then
3034 P_Type
:= Underlying_Type
(P_Type
);
3037 -- Must have discriminants or be an access type designating
3038 -- a type with discriminants. If it is a classwide type it
3039 -- has unknown discriminants.
3041 if Has_Discriminants
(P_Type
)
3042 or else Has_Unknown_Discriminants
(P_Type
)
3044 (Is_Access_Type
(P_Type
)
3045 and then Has_Discriminants
(Designated_Type
(P_Type
)))
3049 -- The rule given in 3.7.2 is part of static semantics, but the
3050 -- intent is clearly that it be treated as a legality rule, and
3051 -- rechecked in the visible part of an instance. Nevertheless
3052 -- the intent also seems to be it should legally apply to the
3053 -- actual of a formal with unknown discriminants, regardless of
3054 -- whether the actual has discriminants, in which case the value
3055 -- of the attribute is determined using the J.4 rules. This choice
3056 -- seems the most useful, and is compatible with existing tests.
3058 elsif In_Instance
then
3061 -- Also allow an object of a generic type if extensions allowed
3062 -- and allow this for any type at all. (this may be obsolete ???)
3064 elsif (Is_Generic_Type
(P_Type
)
3065 or else Is_Generic_Actual_Type
(P_Type
))
3066 and then Extensions_Allowed
3072 -- Fall through if bad prefix
3075 ("prefix of % attribute must be object of discriminated type");
3081 when Attribute_Copy_Sign
=>
3082 Check_Floating_Point_Type_2
;
3083 Set_Etype
(N
, P_Base_Type
);
3084 Resolve
(E1
, P_Base_Type
);
3085 Resolve
(E2
, P_Base_Type
);
3091 when Attribute_Count
=> Count
:
3100 if Nkind_In
(P
, N_Identifier
, N_Expanded_Name
) then
3103 if Ekind
(Ent
) /= E_Entry
then
3104 Error_Attr
("invalid entry name", N
);
3107 elsif Nkind
(P
) = N_Indexed_Component
then
3108 if not Is_Entity_Name
(Prefix
(P
))
3109 or else No
(Entity
(Prefix
(P
)))
3110 or else Ekind
(Entity
(Prefix
(P
))) /= E_Entry_Family
3112 if Nkind
(Prefix
(P
)) = N_Selected_Component
3113 and then Present
(Entity
(Selector_Name
(Prefix
(P
))))
3114 and then Ekind
(Entity
(Selector_Name
(Prefix
(P
)))) =
3118 ("attribute % must apply to entry of current task", P
);
3121 Error_Attr
("invalid entry family name", P
);
3126 Ent
:= Entity
(Prefix
(P
));
3129 elsif Nkind
(P
) = N_Selected_Component
3130 and then Present
(Entity
(Selector_Name
(P
)))
3131 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Entry
3134 ("attribute % must apply to entry of current task", P
);
3137 Error_Attr
("invalid entry name", N
);
3141 for J
in reverse 0 .. Scope_Stack
.Last
loop
3142 S
:= Scope_Stack
.Table
(J
).Entity
;
3144 if S
= Scope
(Ent
) then
3145 if Nkind
(P
) = N_Expanded_Name
then
3146 Tsk
:= Entity
(Prefix
(P
));
3148 -- The prefix denotes either the task type, or else a
3149 -- single task whose task type is being analyzed.
3151 if (Is_Type
(Tsk
) and then Tsk
= S
)
3152 or else (not Is_Type
(Tsk
)
3153 and then Etype
(Tsk
) = S
3154 and then not (Comes_From_Source
(S
)))
3159 ("Attribute % must apply to entry of current task", N
);
3165 elsif Ekind
(Scope
(Ent
)) in Task_Kind
3167 not Ekind_In
(S
, E_Loop
, E_Block
, E_Entry
, E_Entry_Family
)
3169 Error_Attr
("Attribute % cannot appear in inner unit", N
);
3171 elsif Ekind
(Scope
(Ent
)) = E_Protected_Type
3172 and then not Has_Completion
(Scope
(Ent
))
3174 Error_Attr
("attribute % can only be used inside body", N
);
3178 if Is_Overloaded
(P
) then
3180 Index
: Interp_Index
;
3184 Get_First_Interp
(P
, Index
, It
);
3185 while Present
(It
.Nam
) loop
3186 if It
.Nam
= Ent
then
3189 -- Ada 2005 (AI-345): Do not consider primitive entry
3190 -- wrappers generated for task or protected types.
3192 elsif Ada_Version
>= Ada_2005
3193 and then not Comes_From_Source
(It
.Nam
)
3198 Error_Attr
("ambiguous entry name", N
);
3201 Get_Next_Interp
(Index
, It
);
3206 Set_Etype
(N
, Universal_Integer
);
3209 -----------------------
3210 -- Default_Bit_Order --
3211 -----------------------
3213 when Attribute_Default_Bit_Order
=> Default_Bit_Order
: declare
3214 Target_Default_Bit_Order
: System
.Bit_Order
;
3217 Check_Standard_Prefix
;
3219 if Bytes_Big_Endian
then
3220 Target_Default_Bit_Order
:= System
.High_Order_First
;
3222 Target_Default_Bit_Order
:= System
.Low_Order_First
;
3226 Make_Integer_Literal
(Loc
,
3227 UI_From_Int
(System
.Bit_Order
'Pos (Target_Default_Bit_Order
))));
3229 Set_Etype
(N
, Universal_Integer
);
3230 Set_Is_Static_Expression
(N
);
3231 end Default_Bit_Order
;
3233 ----------------------------------
3234 -- Default_Scalar_Storage_Order --
3235 ----------------------------------
3237 when Attribute_Default_Scalar_Storage_Order
=> Default_SSO
: declare
3238 RE_Default_SSO
: RE_Id
;
3241 Check_Standard_Prefix
;
3243 case Opt
.Default_SSO
is
3245 if Bytes_Big_Endian
then
3246 RE_Default_SSO
:= RE_High_Order_First
;
3248 RE_Default_SSO
:= RE_Low_Order_First
;
3252 RE_Default_SSO
:= RE_High_Order_First
;
3255 RE_Default_SSO
:= RE_Low_Order_First
;
3258 raise Program_Error
;
3261 Rewrite
(N
, New_Occurrence_Of
(RTE
(RE_Default_SSO
), Loc
));
3268 when Attribute_Definite
=>
3269 Legal_Formal_Attribute
;
3275 when Attribute_Delta
=>
3276 Check_Fixed_Point_Type_0
;
3277 Set_Etype
(N
, Universal_Real
);
3283 when Attribute_Denorm
=>
3284 Check_Floating_Point_Type_0
;
3285 Set_Etype
(N
, Standard_Boolean
);
3287 ---------------------
3288 -- Descriptor_Size --
3289 ---------------------
3291 when Attribute_Descriptor_Size
=>
3294 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
3295 Error_Attr_P
("prefix of attribute % must denote a type");
3298 Set_Etype
(N
, Universal_Integer
);
3304 when Attribute_Digits
=>
3308 if not Is_Floating_Point_Type
(P_Type
)
3309 and then not Is_Decimal_Fixed_Point_Type
(P_Type
)
3312 ("prefix of % attribute must be float or decimal type");
3315 Set_Etype
(N
, Universal_Integer
);
3321 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3323 when Attribute_Elab_Body |
3324 Attribute_Elab_Spec |
3325 Attribute_Elab_Subp_Body
=>
3328 Check_Unit_Name
(P
);
3329 Set_Etype
(N
, Standard_Void_Type
);
3331 -- We have to manually call the expander in this case to get
3332 -- the necessary expansion (normally attributes that return
3333 -- entities are not expanded).
3341 -- Shares processing with Elab_Body
3347 when Attribute_Elaborated
=>
3349 Check_Unit_Name
(P
);
3350 Set_Etype
(N
, Standard_Boolean
);
3356 when Attribute_Emax
=>
3357 Check_Floating_Point_Type_0
;
3358 Set_Etype
(N
, Universal_Integer
);
3364 when Attribute_Enabled
=>
3365 Check_Either_E0_Or_E1
;
3367 if Present
(E1
) then
3368 if not Is_Entity_Name
(E1
) or else No
(Entity
(E1
)) then
3369 Error_Msg_N
("entity name expected for Enabled attribute", E1
);
3374 if Nkind
(P
) /= N_Identifier
then
3375 Error_Msg_N
("identifier expected (check name)", P
);
3376 elsif Get_Check_Id
(Chars
(P
)) = No_Check_Id
then
3377 Error_Msg_N
("& is not a recognized check name", P
);
3380 Set_Etype
(N
, Standard_Boolean
);
3386 when Attribute_Enum_Rep
=> Enum_Rep
: declare
3388 if Present
(E1
) then
3390 Check_Discrete_Type
;
3391 Resolve
(E1
, P_Base_Type
);
3394 if not Is_Entity_Name
(P
)
3395 or else (not Is_Object
(Entity
(P
))
3396 and then Ekind
(Entity
(P
)) /= E_Enumeration_Literal
)
3399 ("prefix of % attribute must be " &
3400 "discrete type/object or enum literal");
3404 Set_Etype
(N
, Universal_Integer
);
3411 when Attribute_Enum_Val
=> Enum_Val
: begin
3415 if not Is_Enumeration_Type
(P_Type
) then
3416 Error_Attr_P
("prefix of % attribute must be enumeration type");
3419 -- If the enumeration type has a standard representation, the effect
3420 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3422 if not Has_Non_Standard_Rep
(P_Base_Type
) then
3424 Make_Attribute_Reference
(Loc
,
3425 Prefix
=> Relocate_Node
(Prefix
(N
)),
3426 Attribute_Name
=> Name_Val
,
3427 Expressions
=> New_List
(Relocate_Node
(E1
))));
3428 Analyze_And_Resolve
(N
, P_Base_Type
);
3430 -- Non-standard representation case (enumeration with holes)
3434 Resolve
(E1
, Any_Integer
);
3435 Set_Etype
(N
, P_Base_Type
);
3443 when Attribute_Epsilon
=>
3444 Check_Floating_Point_Type_0
;
3445 Set_Etype
(N
, Universal_Real
);
3451 when Attribute_Exponent
=>
3452 Check_Floating_Point_Type_1
;
3453 Set_Etype
(N
, Universal_Integer
);
3454 Resolve
(E1
, P_Base_Type
);
3460 when Attribute_External_Tag
=>
3464 Set_Etype
(N
, Standard_String
);
3466 if not Is_Tagged_Type
(P_Type
) then
3467 Error_Attr_P
("prefix of % attribute must be tagged");
3474 when Attribute_Fast_Math
=>
3475 Check_Standard_Prefix
;
3476 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(Fast_Math
), Loc
));
3482 when Attribute_First
=>
3483 Check_Array_Or_Scalar_Type
;
3484 Bad_Attribute_For_Predicate
;
3490 when Attribute_First_Bit
=>
3492 Set_Etype
(N
, Universal_Integer
);
3498 when Attribute_First_Valid
=>
3499 Check_First_Last_Valid
;
3500 Set_Etype
(N
, P_Type
);
3506 when Attribute_Fixed_Value
=>
3508 Check_Fixed_Point_Type
;
3509 Resolve
(E1
, Any_Integer
);
3510 Set_Etype
(N
, P_Base_Type
);
3516 when Attribute_Floor
=>
3517 Check_Floating_Point_Type_1
;
3518 Set_Etype
(N
, P_Base_Type
);
3519 Resolve
(E1
, P_Base_Type
);
3525 when Attribute_Fore
=>
3526 Check_Fixed_Point_Type_0
;
3527 Set_Etype
(N
, Universal_Integer
);
3533 when Attribute_Fraction
=>
3534 Check_Floating_Point_Type_1
;
3535 Set_Etype
(N
, P_Base_Type
);
3536 Resolve
(E1
, P_Base_Type
);
3542 when Attribute_From_Any
=>
3544 Check_PolyORB_Attribute
;
3545 Set_Etype
(N
, P_Base_Type
);
3547 -----------------------
3548 -- Has_Access_Values --
3549 -----------------------
3551 when Attribute_Has_Access_Values
=>
3554 Set_Etype
(N
, Standard_Boolean
);
3556 ----------------------
3557 -- Has_Same_Storage --
3558 ----------------------
3560 when Attribute_Has_Same_Storage
=>
3563 -- The arguments must be objects of any type
3565 Analyze_And_Resolve
(P
);
3566 Analyze_And_Resolve
(E1
);
3567 Check_Object_Reference
(P
);
3568 Check_Object_Reference
(E1
);
3569 Set_Etype
(N
, Standard_Boolean
);
3571 -----------------------
3572 -- Has_Tagged_Values --
3573 -----------------------
3575 when Attribute_Has_Tagged_Values
=>
3578 Set_Etype
(N
, Standard_Boolean
);
3580 -----------------------
3581 -- Has_Discriminants --
3582 -----------------------
3584 when Attribute_Has_Discriminants
=>
3585 Legal_Formal_Attribute
;
3591 when Attribute_Identity
=>
3595 if Etype
(P
) = Standard_Exception_Type
then
3596 Set_Etype
(N
, RTE
(RE_Exception_Id
));
3598 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3599 -- interface class-wide types.
3601 elsif Is_Task_Type
(Etype
(P
))
3602 or else (Is_Access_Type
(Etype
(P
))
3603 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
3604 or else (Ada_Version
>= Ada_2005
3605 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
3606 and then Is_Interface
(Etype
(P
))
3607 and then Is_Task_Interface
(Etype
(P
)))
3610 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
3613 if Ada_Version
>= Ada_2005
then
3615 ("prefix of % attribute must be an exception, a " &
3616 "task or a task interface class-wide object");
3619 ("prefix of % attribute must be a task or an exception");
3627 when Attribute_Image
=> Image
:
3629 Check_SPARK_05_Restriction_On_Attribute
;
3631 Set_Etype
(N
, Standard_String
);
3633 if Is_Real_Type
(P_Type
) then
3634 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
3635 Error_Msg_Name_1
:= Aname
;
3637 ("(Ada 83) % attribute not allowed for real types", N
);
3641 if Is_Enumeration_Type
(P_Type
) then
3642 Check_Restriction
(No_Enumeration_Maps
, N
);
3646 Resolve
(E1
, P_Base_Type
);
3648 Validate_Non_Static_Attribute_Function_Call
;
3650 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
3651 -- to avoid giving a duplicate message for Img expanded into Image.
3653 if Restriction_Check_Required
(No_Fixed_IO
)
3654 and then Comes_From_Source
(N
)
3655 and then Is_Fixed_Point_Type
(P_Type
)
3657 Check_Restriction
(No_Fixed_IO
, P
);
3665 when Attribute_Img
=> Img
:
3668 Set_Etype
(N
, Standard_String
);
3670 if not Is_Scalar_Type
(P_Type
)
3671 or else (Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)))
3674 ("prefix of % attribute must be scalar object name");
3679 -- Check restriction No_Fixed_IO
3681 if Restriction_Check_Required
(No_Fixed_IO
)
3682 and then Is_Fixed_Point_Type
(P_Type
)
3684 Check_Restriction
(No_Fixed_IO
, P
);
3692 when Attribute_Input
=>
3694 Check_Stream_Attribute
(TSS_Stream_Input
);
3695 Set_Etype
(N
, P_Base_Type
);
3701 when Attribute_Integer_Value
=>
3704 Resolve
(E1
, Any_Fixed
);
3706 -- Signal an error if argument type is not a specific fixed-point
3707 -- subtype. An error has been signalled already if the argument
3708 -- was not of a fixed-point type.
3710 if Etype
(E1
) = Any_Fixed
and then not Error_Posted
(E1
) then
3711 Error_Attr
("argument of % must be of a fixed-point type", E1
);
3714 Set_Etype
(N
, P_Base_Type
);
3720 when Attribute_Invalid_Value
=>
3723 Set_Etype
(N
, P_Base_Type
);
3724 Invalid_Value_Used
:= True;
3730 when Attribute_Large
=>
3733 Set_Etype
(N
, Universal_Real
);
3739 when Attribute_Last
=>
3740 Check_Array_Or_Scalar_Type
;
3741 Bad_Attribute_For_Predicate
;
3747 when Attribute_Last_Bit
=>
3749 Set_Etype
(N
, Universal_Integer
);
3755 when Attribute_Last_Valid
=>
3756 Check_First_Last_Valid
;
3757 Set_Etype
(N
, P_Type
);
3763 when Attribute_Leading_Part
=>
3764 Check_Floating_Point_Type_2
;
3765 Set_Etype
(N
, P_Base_Type
);
3766 Resolve
(E1
, P_Base_Type
);
3767 Resolve
(E2
, Any_Integer
);
3773 when Attribute_Length
=>
3775 Set_Etype
(N
, Universal_Integer
);
3781 when Attribute_Library_Level
=>
3784 if not Is_Entity_Name
(P
) then
3785 Error_Attr_P
("prefix of % attribute must be an entity name");
3788 if not Inside_A_Generic
then
3789 Set_Boolean_Result
(N
,
3790 Is_Library_Level_Entity
(Entity
(P
)));
3793 Set_Etype
(N
, Standard_Boolean
);
3799 when Attribute_Lock_Free
=>
3801 Set_Etype
(N
, Standard_Boolean
);
3803 if not Is_Protected_Type
(P_Type
) then
3805 ("prefix of % attribute must be a protected object");
3812 when Attribute_Loop_Entry
=> Loop_Entry
: declare
3813 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
);
3814 -- Inspect the prefix for any uses of entities declared within the
3815 -- related loop. Loop_Id denotes the loop identifier.
3817 --------------------------------
3818 -- Check_References_In_Prefix --
3819 --------------------------------
3821 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
) is
3822 Loop_Decl
: constant Node_Id
:= Label_Construct
(Parent
(Loop_Id
));
3824 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
3825 -- Determine whether a reference mentions an entity declared
3826 -- within the related loop.
3828 function Declared_Within
(Nod
: Node_Id
) return Boolean;
3829 -- Determine whether Nod appears in the subtree of Loop_Decl
3831 ---------------------
3832 -- Check_Reference --
3833 ---------------------
3835 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
3837 if Nkind
(Nod
) = N_Identifier
3838 and then Present
(Entity
(Nod
))
3839 and then Declared_Within
(Declaration_Node
(Entity
(Nod
)))
3842 ("prefix of attribute % cannot reference local entities",
3848 end Check_Reference
;
3850 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
3852 ---------------------
3853 -- Declared_Within --
3854 ---------------------
3856 function Declared_Within
(Nod
: Node_Id
) return Boolean is
3861 while Present
(Stmt
) loop
3862 if Stmt
= Loop_Decl
then
3865 -- Prevent the search from going too far
3867 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
3871 Stmt
:= Parent
(Stmt
);
3875 end Declared_Within
;
3877 -- Start of processing for Check_Prefix_For_Local_References
3880 Check_References
(P
);
3881 end Check_References_In_Prefix
;
3885 Context
: constant Node_Id
:= Parent
(N
);
3887 Enclosing_Loop
: Node_Id
;
3888 Loop_Id
: Entity_Id
:= Empty
;
3891 Enclosing_Pragma
: Node_Id
:= Empty
;
3893 -- Start of processing for Loop_Entry
3898 -- Set the type of the attribute now to ensure the successfull
3899 -- continuation of analysis even if the attribute is misplaced.
3901 Set_Etype
(Attr
, P_Type
);
3903 -- Attribute 'Loop_Entry may appear in several flavors:
3905 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
3906 -- nearest enclosing loop.
3908 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
3909 -- attribute may be related to a loop denoted by label Expr or
3910 -- the prefix may denote an array object and Expr may act as an
3911 -- indexed component.
3913 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
3914 -- to the nearest enclosing loop, all expressions are part of
3915 -- an indexed component.
3917 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
3918 -- denotes, the attribute may be related to a loop denoted by
3919 -- label Expr or the prefix may denote a multidimensional array
3920 -- array object and Expr along with the rest of the expressions
3921 -- may act as indexed components.
3923 -- Regardless of variations, the attribute reference does not have an
3924 -- expression list. Instead, all available expressions are stored as
3925 -- indexed components.
3927 -- When the attribute is part of an indexed component, find the first
3928 -- expression as it will determine the semantics of 'Loop_Entry.
3930 if Nkind
(Context
) = N_Indexed_Component
then
3931 E1
:= First
(Expressions
(Context
));
3934 -- The attribute reference appears in the following form:
3936 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
3938 -- In this case, the loop name is omitted and no rewriting is
3941 if Present
(E2
) then
3944 -- The form of the attribute is:
3946 -- Prefix'Loop_Entry (Expr) [(...)]
3948 -- If Expr denotes a loop entry, the whole attribute and indexed
3949 -- component will have to be rewritten to reflect this relation.
3952 pragma Assert
(Present
(E1
));
3954 -- Do not expand the expression as it may have side effects.
3955 -- Simply preanalyze to determine whether it is a loop name or
3958 Preanalyze_And_Resolve
(E1
);
3960 if Is_Entity_Name
(E1
)
3961 and then Present
(Entity
(E1
))
3962 and then Ekind
(Entity
(E1
)) = E_Loop
3964 Loop_Id
:= Entity
(E1
);
3966 -- Transform the attribute and enclosing indexed component
3968 Set_Expressions
(N
, Expressions
(Context
));
3969 Rewrite
(Context
, N
);
3970 Set_Etype
(Context
, P_Type
);
3977 -- The prefix must denote an object
3979 if not Is_Object_Reference
(P
) then
3980 Error_Attr_P
("prefix of attribute % must denote an object");
3983 -- The prefix cannot be of a limited type because the expansion of
3984 -- Loop_Entry must create a constant initialized by the evaluated
3987 if Is_Limited_View
(Etype
(P
)) then
3988 Error_Attr_P
("prefix of attribute % cannot be limited");
3991 -- Climb the parent chain to verify the location of the attribute and
3992 -- find the enclosing loop.
3995 while Present
(Stmt
) loop
3997 -- Locate the corresponding enclosing pragma. Note that in the
3998 -- case of Assert[And_Cut] and Assume, we have already checked
3999 -- that the pragma appears in an appropriate loop location.
4001 if Nkind
(Original_Node
(Stmt
)) = N_Pragma
4002 and then Nam_In
(Pragma_Name
(Original_Node
(Stmt
)),
4003 Name_Loop_Invariant
,
4006 Name_Assert_And_Cut
,
4009 Enclosing_Pragma
:= Original_Node
(Stmt
);
4011 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4012 -- iteration may be expanded into several nested loops, we are
4013 -- interested in the outermost one which has the loop identifier.
4015 elsif Nkind
(Stmt
) = N_Loop_Statement
4016 and then Present
(Identifier
(Stmt
))
4018 Enclosing_Loop
:= Stmt
;
4020 -- The original attribute reference may lack a loop name. Use
4021 -- the name of the enclosing loop because it is the related
4024 if No
(Loop_Id
) then
4025 Loop_Id
:= Entity
(Identifier
(Enclosing_Loop
));
4030 -- Prevent the search from going too far
4032 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
4036 Stmt
:= Parent
(Stmt
);
4039 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4040 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4041 -- purpose if they appear in an appropriate location in a loop,
4042 -- which was already checked by the top level pragma circuit).
4044 if No
(Enclosing_Pragma
) then
4045 Error_Attr
("attribute% must appear within appropriate pragma", N
);
4048 -- A Loop_Entry that applies to a given loop statement must not
4049 -- appear within a body of accept statement, if this construct is
4050 -- itself enclosed by the given loop statement.
4052 for Index
in reverse 0 .. Scope_Stack
.Last
loop
4053 Scop
:= Scope_Stack
.Table
(Index
).Entity
;
4055 if Ekind
(Scop
) = E_Loop
and then Scop
= Loop_Id
then
4057 elsif Ekind_In
(Scop
, E_Block
, E_Loop
, E_Return_Statement
) then
4061 ("attribute % cannot appear in body or accept statement", N
);
4066 -- The prefix cannot mention entities declared within the related
4067 -- loop because they will not be visible once the prefix is moved
4068 -- outside the loop.
4070 Check_References_In_Prefix
(Loop_Id
);
4072 -- The prefix must denote a static entity if the pragma does not
4073 -- apply to the innermost enclosing loop statement, or if it appears
4074 -- within a potentially unevaluated epxression.
4076 if Is_Entity_Name
(P
)
4077 or else Nkind
(Parent
(P
)) = N_Object_Renaming_Declaration
4081 elsif Present
(Enclosing_Loop
)
4082 and then Entity
(Identifier
(Enclosing_Loop
)) /= Loop_Id
4085 ("prefix of attribute % that applies to outer loop must denote "
4088 elsif Is_Potentially_Unevaluated
(P
) then
4092 -- Replace the Loop_Entry attribute reference by its prefix if the
4093 -- related pragma is ignored. This transformation is OK with respect
4094 -- to typing because Loop_Entry's type is that of its prefix. This
4095 -- early transformation also avoids the generation of a useless loop
4098 if Is_Ignored
(Enclosing_Pragma
) then
4099 Rewrite
(N
, Relocate_Node
(P
));
4102 Preanalyze_And_Resolve
(P
);
4109 when Attribute_Machine
=>
4110 Check_Floating_Point_Type_1
;
4111 Set_Etype
(N
, P_Base_Type
);
4112 Resolve
(E1
, P_Base_Type
);
4118 when Attribute_Machine_Emax
=>
4119 Check_Floating_Point_Type_0
;
4120 Set_Etype
(N
, Universal_Integer
);
4126 when Attribute_Machine_Emin
=>
4127 Check_Floating_Point_Type_0
;
4128 Set_Etype
(N
, Universal_Integer
);
4130 ----------------------
4131 -- Machine_Mantissa --
4132 ----------------------
4134 when Attribute_Machine_Mantissa
=>
4135 Check_Floating_Point_Type_0
;
4136 Set_Etype
(N
, Universal_Integer
);
4138 -----------------------
4139 -- Machine_Overflows --
4140 -----------------------
4142 when Attribute_Machine_Overflows
=>
4145 Set_Etype
(N
, Standard_Boolean
);
4151 when Attribute_Machine_Radix
=>
4154 Set_Etype
(N
, Universal_Integer
);
4156 ----------------------
4157 -- Machine_Rounding --
4158 ----------------------
4160 when Attribute_Machine_Rounding
=>
4161 Check_Floating_Point_Type_1
;
4162 Set_Etype
(N
, P_Base_Type
);
4163 Resolve
(E1
, P_Base_Type
);
4165 --------------------
4166 -- Machine_Rounds --
4167 --------------------
4169 when Attribute_Machine_Rounds
=>
4172 Set_Etype
(N
, Standard_Boolean
);
4178 when Attribute_Machine_Size
=>
4181 Check_Not_Incomplete_Type
;
4182 Set_Etype
(N
, Universal_Integer
);
4188 when Attribute_Mantissa
=>
4191 Set_Etype
(N
, Universal_Integer
);
4197 when Attribute_Max
=>
4200 ----------------------------------
4201 -- Max_Alignment_For_Allocation --
4202 ----------------------------------
4204 when Attribute_Max_Size_In_Storage_Elements
=>
4205 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
4207 ----------------------------------
4208 -- Max_Size_In_Storage_Elements --
4209 ----------------------------------
4211 when Attribute_Max_Alignment_For_Allocation
=>
4212 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
4214 -----------------------
4215 -- Maximum_Alignment --
4216 -----------------------
4218 when Attribute_Maximum_Alignment
=>
4219 Standard_Attribute
(Ttypes
.Maximum_Alignment
);
4221 --------------------
4222 -- Mechanism_Code --
4223 --------------------
4225 when Attribute_Mechanism_Code
=>
4226 if not Is_Entity_Name
(P
)
4227 or else not Is_Subprogram
(Entity
(P
))
4229 Error_Attr_P
("prefix of % attribute must be subprogram");
4232 Check_Either_E0_Or_E1
;
4234 if Present
(E1
) then
4235 Resolve
(E1
, Any_Integer
);
4236 Set_Etype
(E1
, Standard_Integer
);
4238 if not Is_OK_Static_Expression
(E1
) then
4239 Flag_Non_Static_Expr
4240 ("expression for parameter number must be static!", E1
);
4243 elsif UI_To_Int
(Intval
(E1
)) > Number_Formals
(Entity
(P
))
4244 or else UI_To_Int
(Intval
(E1
)) < 0
4246 Error_Attr
("invalid parameter number for % attribute", E1
);
4250 Set_Etype
(N
, Universal_Integer
);
4256 when Attribute_Min
=>
4263 when Attribute_Mod
=>
4265 -- Note: this attribute is only allowed in Ada 2005 mode, but
4266 -- we do not need to test that here, since Mod is only recognized
4267 -- as an attribute name in Ada 2005 mode during the parse.
4270 Check_Modular_Integer_Type
;
4271 Resolve
(E1
, Any_Integer
);
4272 Set_Etype
(N
, P_Base_Type
);
4278 when Attribute_Model
=>
4279 Check_Floating_Point_Type_1
;
4280 Set_Etype
(N
, P_Base_Type
);
4281 Resolve
(E1
, P_Base_Type
);
4287 when Attribute_Model_Emin
=>
4288 Check_Floating_Point_Type_0
;
4289 Set_Etype
(N
, Universal_Integer
);
4295 when Attribute_Model_Epsilon
=>
4296 Check_Floating_Point_Type_0
;
4297 Set_Etype
(N
, Universal_Real
);
4299 --------------------
4300 -- Model_Mantissa --
4301 --------------------
4303 when Attribute_Model_Mantissa
=>
4304 Check_Floating_Point_Type_0
;
4305 Set_Etype
(N
, Universal_Integer
);
4311 when Attribute_Model_Small
=>
4312 Check_Floating_Point_Type_0
;
4313 Set_Etype
(N
, Universal_Real
);
4319 when Attribute_Modulus
=>
4321 Check_Modular_Integer_Type
;
4322 Set_Etype
(N
, Universal_Integer
);
4324 --------------------
4325 -- Null_Parameter --
4326 --------------------
4328 when Attribute_Null_Parameter
=> Null_Parameter
: declare
4329 Parnt
: constant Node_Id
:= Parent
(N
);
4330 GParnt
: constant Node_Id
:= Parent
(Parnt
);
4332 procedure Bad_Null_Parameter
(Msg
: String);
4333 -- Used if bad Null parameter attribute node is found. Issues
4334 -- given error message, and also sets the type to Any_Type to
4335 -- avoid blowups later on from dealing with a junk node.
4337 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
);
4338 -- Called to check that Proc_Ent is imported subprogram
4340 ------------------------
4341 -- Bad_Null_Parameter --
4342 ------------------------
4344 procedure Bad_Null_Parameter
(Msg
: String) is
4346 Error_Msg_N
(Msg
, N
);
4347 Set_Etype
(N
, Any_Type
);
4348 end Bad_Null_Parameter
;
4350 ----------------------
4351 -- Must_Be_Imported --
4352 ----------------------
4354 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
) is
4355 Pent
: constant Entity_Id
:= Ultimate_Alias
(Proc_Ent
);
4358 -- Ignore check if procedure not frozen yet (we will get
4359 -- another chance when the default parameter is reanalyzed)
4361 if not Is_Frozen
(Pent
) then
4364 elsif not Is_Imported
(Pent
) then
4366 ("Null_Parameter can only be used with imported subprogram");
4371 end Must_Be_Imported
;
4373 -- Start of processing for Null_Parameter
4378 Set_Etype
(N
, P_Type
);
4380 -- Case of attribute used as default expression
4382 if Nkind
(Parnt
) = N_Parameter_Specification
then
4383 Must_Be_Imported
(Defining_Entity
(GParnt
));
4385 -- Case of attribute used as actual for subprogram (positional)
4387 elsif Nkind
(Parnt
) in N_Subprogram_Call
4388 and then Is_Entity_Name
(Name
(Parnt
))
4390 Must_Be_Imported
(Entity
(Name
(Parnt
)));
4392 -- Case of attribute used as actual for subprogram (named)
4394 elsif Nkind
(Parnt
) = N_Parameter_Association
4395 and then Nkind
(GParnt
) in N_Subprogram_Call
4396 and then Is_Entity_Name
(Name
(GParnt
))
4398 Must_Be_Imported
(Entity
(Name
(GParnt
)));
4400 -- Not an allowed case
4404 ("Null_Parameter must be actual or default parameter");
4412 when Attribute_Object_Size
=>
4415 Check_Not_Incomplete_Type
;
4416 Set_Etype
(N
, Universal_Integer
);
4422 when Attribute_Old
=> Old
: declare
4423 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
);
4424 -- Inspect the contents of the prefix and detect illegal uses of a
4425 -- nested 'Old, attribute 'Result or a use of an entity declared in
4426 -- the related postcondition expression. Subp_Id is the subprogram to
4427 -- which the related postcondition applies.
4429 procedure Check_Use_In_Contract_Cases
(Prag
: Node_Id
);
4430 -- Perform various semantic checks related to the placement of the
4431 -- attribute in pragma Contract_Cases.
4433 procedure Check_Use_In_Test_Case
(Prag
: Node_Id
);
4434 -- Perform various semantic checks related to the placement of the
4435 -- attribute in pragma Contract_Cases.
4437 --------------------------------
4438 -- Check_References_In_Prefix --
4439 --------------------------------
4441 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
) is
4442 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
4443 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4444 -- and perform the appropriate semantic check.
4446 ---------------------
4447 -- Check_Reference --
4448 ---------------------
4450 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
4452 -- Attributes 'Old and 'Result cannot appear in the prefix of
4453 -- another attribute 'Old.
4455 if Nkind
(Nod
) = N_Attribute_Reference
4456 and then Nam_In
(Attribute_Name
(Nod
), Name_Old
,
4459 Error_Msg_Name_1
:= Attribute_Name
(Nod
);
4460 Error_Msg_Name_2
:= Name_Old
;
4462 ("attribute % cannot appear in the prefix of attribute %",
4466 -- Entities mentioned within the prefix of attribute 'Old must
4467 -- be global to the related postcondition. If this is not the
4468 -- case, then the scope of the local entity is nested within
4469 -- that of the subprogram.
4471 elsif Nkind
(Nod
) = N_Identifier
4472 and then Present
(Entity
(Nod
))
4473 and then Scope_Within
(Scope
(Entity
(Nod
)), Subp_Id
)
4476 ("prefix of attribute % cannot reference local entities",
4482 end Check_Reference
;
4484 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
4486 -- Start of processing for Check_References_In_Prefix
4489 Check_References
(P
);
4490 end Check_References_In_Prefix
;
4492 ---------------------------------
4493 -- Check_Use_In_Contract_Cases --
4494 ---------------------------------
4496 procedure Check_Use_In_Contract_Cases
(Prag
: Node_Id
) is
4497 Cases
: constant Node_Id
:=
4499 (First
(Pragma_Argument_Associations
(Prag
)));
4503 -- Climb the parent chain to reach the top of the expression where
4504 -- attribute 'Old resides.
4507 while Parent
(Parent
(Expr
)) /= Cases
loop
4508 Expr
:= Parent
(Expr
);
4511 -- Ensure that the obtained expression is the consequence of a
4512 -- contract case as this is the only postcondition-like part of
4513 -- the pragma. Otherwise, attribute 'Old appears in the condition
4514 -- of a contract case. Emit an error since this is not a
4515 -- postcondition-like context. (SPARK RM 6.1.3(2))
4517 if Expr
/= Expression
(Parent
(Expr
)) then
4519 ("attribute % cannot appear in the condition "
4520 & "of a contract case", P
);
4522 end Check_Use_In_Contract_Cases
;
4524 ----------------------------
4525 -- Check_Use_In_Test_Case --
4526 ----------------------------
4528 procedure Check_Use_In_Test_Case
(Prag
: Node_Id
) is
4529 Ensures
: constant Node_Id
:= Get_Ensures_From_CTC_Pragma
(Prag
);
4533 -- Climb the parent chain to reach the top of the Ensures part of
4534 -- pragma Test_Case.
4537 while Expr
/= Prag
loop
4538 if Expr
= Ensures
then
4542 Expr
:= Parent
(Expr
);
4545 -- If we get there, then attribute 'Old appears in the requires
4546 -- expression of pragma Test_Case which is not a postcondition-
4550 ("attribute % cannot appear in the requires expression of a "
4552 end Check_Use_In_Test_Case
;
4557 -- The enclosing scope, excluding loops for quantified expressions.
4558 -- During analysis, it is the postcondition subprogram. During
4559 -- pre-analysis, it is the scope of the subprogram declaration.
4562 -- During pre-analysis, Prag is the enclosing pragma node if any
4564 -- Start of processing for Old
4569 -- Find enclosing scopes, excluding loops
4571 CS
:= Current_Scope
;
4572 while Ekind
(CS
) = E_Loop
loop
4576 -- Check the legality of attribute 'Old when it appears inside pragma
4577 -- Refined_Post. These specialized checks are required only when code
4578 -- generation is disabled. In the general case pragma Refined_Post is
4579 -- transformed into pragma Check by Process_PPCs which in turn is
4580 -- relocated to procedure _Postconditions. From then on the legality
4581 -- of 'Old is determined as usual.
4583 if not Expander_Active
and then In_Refined_Post
then
4584 Preanalyze_And_Resolve
(P
);
4585 Check_References_In_Prefix
(CS
);
4586 P_Type
:= Etype
(P
);
4587 Set_Etype
(N
, P_Type
);
4589 if Is_Limited_Type
(P_Type
) then
4590 Error_Attr
("attribute % cannot apply to limited objects", P
);
4593 if Is_Entity_Name
(P
)
4594 and then Is_Constant_Object
(Entity
(P
))
4597 ("??attribute Old applied to constant has no effect", P
);
4602 -- A Contract_Cases, Postcondition or Test_Case pragma is in the
4603 -- process of being preanalyzed. Perform the semantic checks now
4604 -- before the pragma is relocated and/or expanded.
4606 -- For a generic subprogram, postconditions are preanalyzed as well
4607 -- for name capture, and still appear within an aspect spec.
4609 elsif In_Spec_Expression
or Inside_A_Generic
then
4611 while Present
(Prag
)
4612 and then not Nkind_In
(Prag
, N_Aspect_Specification
,
4613 N_Function_Specification
,
4615 N_Procedure_Specification
,
4618 Prag
:= Parent
(Prag
);
4621 -- In ASIS mode, the aspect itself is analyzed, in addition to the
4622 -- corresponding pragma. Don't issue errors when analyzing aspect.
4624 if Nkind
(Prag
) = N_Aspect_Specification
4625 and then Nam_In
(Chars
(Identifier
(Prag
)), Name_Post
,
4630 -- In all other cases the related context must be a pragma
4632 elsif Nkind
(Prag
) /= N_Pragma
then
4633 Error_Attr
("% attribute can only appear in postcondition", P
);
4635 -- Verify the placement of the attribute with respect to the
4639 case Get_Pragma_Id
(Prag
) is
4640 when Pragma_Contract_Cases
=>
4641 Check_Use_In_Contract_Cases
(Prag
);
4643 when Pragma_Postcondition | Pragma_Refined_Post
=>
4646 when Pragma_Test_Case
=>
4647 Check_Use_In_Test_Case
(Prag
);
4651 ("% attribute can only appear in postcondition", P
);
4655 -- Body case, where we must be inside a generated _Postconditions
4656 -- procedure, or else the attribute use is definitely misplaced. The
4657 -- postcondition itself may have generated transient scopes, and is
4658 -- not necessarily the current one.
4661 while Present
(CS
) and then CS
/= Standard_Standard
loop
4662 if Chars
(CS
) = Name_uPostconditions
then
4669 if Chars
(CS
) /= Name_uPostconditions
then
4670 Error_Attr
("% attribute can only appear in postcondition", P
);
4674 -- If the attribute reference is generated for a Requires clause,
4675 -- then no expressions follow. Otherwise it is a primary, in which
4676 -- case, if expressions follow, the attribute reference must be an
4677 -- indexable object, so rewrite the node accordingly.
4679 if Present
(E1
) then
4681 Make_Indexed_Component
(Loc
,
4683 Make_Attribute_Reference
(Loc
,
4684 Prefix
=> Relocate_Node
(Prefix
(N
)),
4685 Attribute_Name
=> Name_Old
),
4686 Expressions
=> Expressions
(N
)));
4694 -- Prefix has not been analyzed yet, and its full analysis will take
4695 -- place during expansion (see below).
4697 Preanalyze_And_Resolve
(P
);
4698 Check_References_In_Prefix
(CS
);
4699 P_Type
:= Etype
(P
);
4700 Set_Etype
(N
, P_Type
);
4702 if Is_Limited_Type
(P_Type
) then
4703 Error_Attr
("attribute % cannot apply to limited objects", P
);
4706 if Is_Entity_Name
(P
)
4707 and then Is_Constant_Object
(Entity
(P
))
4710 ("??attribute Old applied to constant has no effect", P
);
4713 -- Check that the prefix of 'Old is an entity when it may be
4714 -- potentially unevaluated (6.1.1 (27/3)).
4717 and then Is_Potentially_Unevaluated
(N
)
4718 and then not Is_Entity_Name
(P
)
4723 -- The attribute appears within a pre/postcondition, but refers to
4724 -- an entity in the enclosing subprogram. If it is a component of
4725 -- a formal its expansion might generate actual subtypes that may
4726 -- be referenced in an inner context, and which must be elaborated
4727 -- within the subprogram itself. If the prefix includes a function
4728 -- call it may involve finalization actions that should only be
4729 -- inserted when the attribute has been rewritten as a declarations.
4730 -- As a result, if the prefix is not a simple name we create
4731 -- a declaration for it now, and insert it at the start of the
4732 -- enclosing subprogram. This is properly an expansion activity
4733 -- but it has to be performed now to prevent out-of-order issues.
4735 -- This expansion is both harmful and not needed in SPARK mode, since
4736 -- the formal verification backend relies on the types of nodes
4737 -- (hence is not robust w.r.t. a change to base type here), and does
4738 -- not suffer from the out-of-order issue described above. Thus, this
4739 -- expansion is skipped in SPARK mode.
4741 if not Is_Entity_Name
(P
) and then not GNATprove_Mode
then
4742 P_Type
:= Base_Type
(P_Type
);
4743 Set_Etype
(N
, P_Type
);
4744 Set_Etype
(P
, P_Type
);
4745 Analyze_Dimension
(N
);
4750 ----------------------
4751 -- Overlaps_Storage --
4752 ----------------------
4754 when Attribute_Overlaps_Storage
=>
4757 -- Both arguments must be objects of any type
4759 Analyze_And_Resolve
(P
);
4760 Analyze_And_Resolve
(E1
);
4761 Check_Object_Reference
(P
);
4762 Check_Object_Reference
(E1
);
4763 Set_Etype
(N
, Standard_Boolean
);
4769 when Attribute_Output
=>
4771 Check_Stream_Attribute
(TSS_Stream_Output
);
4772 Set_Etype
(N
, Standard_Void_Type
);
4773 Resolve
(N
, Standard_Void_Type
);
4779 when Attribute_Partition_ID
=> Partition_Id
:
4783 if P_Type
/= Any_Type
then
4784 if not Is_Library_Level_Entity
(Entity
(P
)) then
4786 ("prefix of % attribute must be library-level entity");
4788 -- The defining entity of prefix should not be declared inside a
4789 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4791 elsif Is_Entity_Name
(P
)
4792 and then Is_Pure
(Entity
(P
))
4794 Error_Attr_P
("prefix of% attribute must not be declared pure");
4798 Set_Etype
(N
, Universal_Integer
);
4801 -------------------------
4802 -- Passed_By_Reference --
4803 -------------------------
4805 when Attribute_Passed_By_Reference
=>
4808 Set_Etype
(N
, Standard_Boolean
);
4814 when Attribute_Pool_Address
=>
4816 Set_Etype
(N
, RTE
(RE_Address
));
4822 when Attribute_Pos
=>
4823 Check_Discrete_Type
;
4826 if Is_Boolean_Type
(P_Type
) then
4827 Error_Msg_Name_1
:= Aname
;
4828 Error_Msg_Name_2
:= Chars
(P_Type
);
4829 Check_SPARK_05_Restriction
4830 ("attribute% is not allowed for type%", P
);
4833 Resolve
(E1
, P_Base_Type
);
4834 Set_Etype
(N
, Universal_Integer
);
4840 when Attribute_Position
=>
4842 Set_Etype
(N
, Universal_Integer
);
4848 when Attribute_Pred
=>
4852 if Is_Real_Type
(P_Type
) or else Is_Boolean_Type
(P_Type
) then
4853 Error_Msg_Name_1
:= Aname
;
4854 Error_Msg_Name_2
:= Chars
(P_Type
);
4855 Check_SPARK_05_Restriction
4856 ("attribute% is not allowed for type%", P
);
4859 Resolve
(E1
, P_Base_Type
);
4860 Set_Etype
(N
, P_Base_Type
);
4862 -- Since Pred works on the base type, we normally do no check for the
4863 -- floating-point case, since the base type is unconstrained. But we
4864 -- make an exception in Check_Float_Overflow mode.
4866 if Is_Floating_Point_Type
(P_Type
) then
4867 if not Range_Checks_Suppressed
(P_Base_Type
) then
4868 Set_Do_Range_Check
(E1
);
4871 -- If not modular type, test for overflow check required
4874 if not Is_Modular_Integer_Type
(P_Type
)
4875 and then not Range_Checks_Suppressed
(P_Base_Type
)
4877 Enable_Range_Check
(E1
);
4885 -- Ada 2005 (AI-327): Dynamic ceiling priorities
4887 when Attribute_Priority
=>
4888 if Ada_Version
< Ada_2005
then
4889 Error_Attr
("% attribute is allowed only in Ada 2005 mode", P
);
4894 -- The prefix must be a protected object (AARM D.5.2 (2/2))
4898 if Is_Protected_Type
(Etype
(P
))
4899 or else (Is_Access_Type
(Etype
(P
))
4900 and then Is_Protected_Type
(Designated_Type
(Etype
(P
))))
4902 Resolve
(P
, Etype
(P
));
4904 Error_Attr_P
("prefix of % attribute must be a protected object");
4907 Set_Etype
(N
, Standard_Integer
);
4909 -- Must be called from within a protected procedure or entry of the
4910 -- protected object.
4917 while S
/= Etype
(P
)
4918 and then S
/= Standard_Standard
4923 if S
= Standard_Standard
then
4924 Error_Attr
("the attribute % is only allowed inside protected "
4929 Validate_Non_Static_Attribute_Function_Call
;
4935 when Attribute_Range
=>
4936 Check_Array_Or_Scalar_Type
;
4937 Bad_Attribute_For_Predicate
;
4939 if Ada_Version
= Ada_83
4940 and then Is_Scalar_Type
(P_Type
)
4941 and then Comes_From_Source
(N
)
4944 ("(Ada 83) % attribute not allowed for scalar type", P
);
4951 when Attribute_Result
=> Result
: declare
4952 Post_Id
: Entity_Id
;
4953 -- The entity of the _Postconditions procedure
4956 -- During pre-analysis, Prag is the enclosing pragma node if any
4958 Subp_Id
: Entity_Id
;
4959 -- The entity of the enclosing subprogram
4962 -- Find the proper enclosing scope
4964 Post_Id
:= Current_Scope
;
4965 while Present
(Post_Id
) loop
4967 -- Skip generated loops
4969 if Ekind
(Post_Id
) = E_Loop
then
4970 Post_Id
:= Scope
(Post_Id
);
4972 -- Skip the special _Parent scope generated to capture references
4973 -- to formals during the process of subprogram inlining.
4975 elsif Ekind
(Post_Id
) = E_Function
4976 and then Chars
(Post_Id
) = Name_uParent
4978 Post_Id
:= Scope
(Post_Id
);
4980 -- Otherwise this must be _Postconditions
4987 Subp_Id
:= Scope
(Post_Id
);
4989 -- If the enclosing subprogram is always inlined, the enclosing
4990 -- postcondition will not be propagated to the expanded call.
4992 if not In_Spec_Expression
4993 and then Has_Pragma_Inline_Always
(Subp_Id
)
4994 and then Warn_On_Redundant_Constructs
4997 ("postconditions on inlined functions not enforced?r?", N
);
5000 -- If we are in the scope of a function and in Spec_Expression mode,
5001 -- this is likely the prescan of the postcondition (or contract case,
5002 -- or test case) pragma, and we just set the proper type. If there is
5003 -- an error it will be caught when the real Analyze call is done.
5005 if Ekind
(Post_Id
) = E_Function
and then In_Spec_Expression
then
5009 if Chars
(Post_Id
) /= Chars
(P
) then
5010 Error_Msg_Name_1
:= Name_Result
;
5012 ("incorrect prefix for % attribute, expected &", P
, Post_Id
);
5016 -- Check in postcondition, Test_Case or Contract_Cases of function
5019 while Present
(Prag
)
5020 and then not Nkind_In
(Prag
, N_Pragma
,
5021 N_Function_Specification
,
5022 N_Aspect_Specification
,
5025 Prag
:= Parent
(Prag
);
5028 -- In ASIS mode, the aspect itself is analyzed, in addition to the
5029 -- corresponding pragma. Do not issue errors when analyzing the
5032 if Nkind
(Prag
) = N_Aspect_Specification
then
5035 -- Must have a pragma
5037 elsif Nkind
(Prag
) /= N_Pragma
then
5039 ("% attribute can only appear in postcondition of function",
5042 -- Processing depends on which pragma we have
5045 case Get_Pragma_Id
(Prag
) is
5046 when Pragma_Test_Case
=>
5048 Arg_Ens
: constant Node_Id
:=
5049 Get_Ensures_From_CTC_Pragma
(Prag
);
5054 while Arg
/= Prag
and then Arg
/= Arg_Ens
loop
5055 Arg
:= Parent
(Arg
);
5058 if Arg
/= Arg_Ens
then
5060 ("% attribute misplaced inside test case", P
);
5064 when Pragma_Contract_Cases
=>
5066 Aggr
: constant Node_Id
:=
5068 (Pragma_Argument_Associations
(Prag
)));
5074 and then Parent
(Parent
(Arg
)) /= Aggr
5076 Arg
:= Parent
(Arg
);
5079 -- At this point, Parent (Arg) should be a component
5080 -- association. Attribute Result is only allowed in
5081 -- the expression part of this association.
5083 if Nkind
(Parent
(Arg
)) /= N_Component_Association
5084 or else Arg
/= Expression
(Parent
(Arg
))
5087 ("% attribute misplaced inside contract cases",
5092 when Pragma_Postcondition | Pragma_Refined_Post
=>
5097 ("% attribute can only appear in postcondition "
5098 & "of function", P
);
5102 -- The attribute reference is a primary. If expressions follow,
5103 -- the attribute reference is really an indexable object, so
5104 -- rewrite and analyze as an indexed component.
5106 if Present
(E1
) then
5108 Make_Indexed_Component
(Loc
,
5110 Make_Attribute_Reference
(Loc
,
5111 Prefix
=> Relocate_Node
(Prefix
(N
)),
5112 Attribute_Name
=> Name_Result
),
5113 Expressions
=> Expressions
(N
)));
5118 Set_Etype
(N
, Etype
(Post_Id
));
5120 -- If several functions with that name are visible, the intended
5121 -- one is the current scope.
5123 if Is_Overloaded
(P
) then
5124 Set_Entity
(P
, Post_Id
);
5125 Set_Is_Overloaded
(P
, False);
5128 -- Check the legality of attribute 'Result when it appears inside
5129 -- pragma Refined_Post. These specialized checks are required only
5130 -- when code generation is disabled. In the general case pragma
5131 -- Refined_Post is transformed into pragma Check by Process_PPCs
5132 -- which in turn is relocated to procedure _Postconditions. From
5133 -- then on the legality of 'Result is determined as usual.
5135 elsif not Expander_Active
and then In_Refined_Post
then
5137 -- Routine _Postconditions has not been generated yet, the nearest
5138 -- enclosing subprogram is denoted by the current scope.
5140 if Ekind
(Post_Id
) /= E_Procedure
5141 or else Chars
(Post_Id
) /= Name_uPostconditions
5143 Subp_Id
:= Current_Scope
;
5146 -- The prefix denotes the nearest enclosing function
5148 if Is_Entity_Name
(P
)
5149 and then Ekind
(Entity
(P
)) = E_Function
5150 and then Entity
(P
) = Subp_Id
5154 -- Otherwise the use of 'Result is illegal
5157 Error_Msg_Name_2
:= Chars
(Subp_Id
);
5158 Error_Attr
("incorrect prefix for % attribute, expected %", P
);
5161 Set_Etype
(N
, Etype
(Subp_Id
));
5163 -- Body case, where we must be inside a generated _Postconditions
5164 -- procedure, and the prefix must be on the scope stack, or else the
5165 -- attribute use is definitely misplaced. The postcondition itself
5166 -- may have generated transient scopes, and is not necessarily the
5170 while Present
(Post_Id
)
5171 and then Post_Id
/= Standard_Standard
5173 if Chars
(Post_Id
) = Name_uPostconditions
then
5176 Post_Id
:= Scope
(Post_Id
);
5180 Subp_Id
:= Scope
(Post_Id
);
5182 if Chars
(Post_Id
) = Name_uPostconditions
5183 and then Ekind
(Subp_Id
) = E_Function
5187 if Nkind_In
(P
, N_Identifier
, N_Operator_Symbol
)
5188 and then Chars
(P
) = Chars
(Subp_Id
)
5192 -- Within an instance, the prefix designates the local renaming
5193 -- of the original generic.
5195 elsif Is_Entity_Name
(P
)
5196 and then Ekind
(Entity
(P
)) = E_Function
5197 and then Present
(Alias
(Entity
(P
)))
5198 and then Chars
(Alias
(Entity
(P
))) = Chars
(Subp_Id
)
5203 Error_Msg_Name_2
:= Chars
(Subp_Id
);
5205 ("incorrect prefix for % attribute, expected %", P
);
5208 Rewrite
(N
, Make_Identifier
(Sloc
(N
), Name_uResult
));
5209 Analyze_And_Resolve
(N
, Etype
(Subp_Id
));
5213 ("% attribute can only appear in postcondition of function",
5223 when Attribute_Range_Length
=>
5225 Check_Discrete_Type
;
5226 Set_Etype
(N
, Universal_Integer
);
5232 when Attribute_Read
=>
5234 Check_Stream_Attribute
(TSS_Stream_Read
);
5235 Set_Etype
(N
, Standard_Void_Type
);
5236 Resolve
(N
, Standard_Void_Type
);
5237 Note_Possible_Modification
(E2
, Sure
=> True);
5243 when Attribute_Ref
=>
5247 if Nkind
(P
) /= N_Expanded_Name
5248 or else not Is_RTE
(P_Type
, RE_Address
)
5250 Error_Attr_P
("prefix of % attribute must be System.Address");
5253 Analyze_And_Resolve
(E1
, Any_Integer
);
5254 Set_Etype
(N
, RTE
(RE_Address
));
5260 when Attribute_Remainder
=>
5261 Check_Floating_Point_Type_2
;
5262 Set_Etype
(N
, P_Base_Type
);
5263 Resolve
(E1
, P_Base_Type
);
5264 Resolve
(E2
, P_Base_Type
);
5266 ---------------------
5267 -- Restriction_Set --
5268 ---------------------
5270 when Attribute_Restriction_Set
=> Restriction_Set
: declare
5273 Unam
: Unit_Name_Type
;
5278 Check_System_Prefix
;
5280 -- No_Dependence case
5282 if Nkind
(E1
) = N_Parameter_Association
then
5283 pragma Assert
(Chars
(Selector_Name
(E1
)) = Name_No_Dependence
);
5284 U
:= Explicit_Actual_Parameter
(E1
);
5286 if not OK_No_Dependence_Unit_Name
(U
) then
5287 Set_Boolean_Result
(N
, False);
5291 -- See if there is an entry already in the table. That's the
5292 -- case in which we can return True.
5294 for J
in No_Dependences
.First
.. No_Dependences
.Last
loop
5295 if Designate_Same_Unit
(U
, No_Dependences
.Table
(J
).Unit
)
5296 and then No_Dependences
.Table
(J
).Warn
= False
5298 Set_Boolean_Result
(N
, True);
5303 -- If not in the No_Dependence table, result is False
5305 Set_Boolean_Result
(N
, False);
5307 -- In this case, we must ensure that the binder will reject any
5308 -- other unit in the partition that sets No_Dependence for this
5309 -- unit. We do that by making an entry in the special table kept
5310 -- for this purpose (if the entry is not there already).
5312 Unam
:= Get_Spec_Name
(Get_Unit_Name
(U
));
5314 for J
in Restriction_Set_Dependences
.First
..
5315 Restriction_Set_Dependences
.Last
5317 if Restriction_Set_Dependences
.Table
(J
) = Unam
then
5322 Restriction_Set_Dependences
.Append
(Unam
);
5324 -- Normal restriction case
5327 if Nkind
(E1
) /= N_Identifier
then
5328 Set_Boolean_Result
(N
, False);
5329 Error_Attr
("attribute % requires restriction identifier", E1
);
5332 R
:= Get_Restriction_Id
(Process_Restriction_Synonyms
(E1
));
5334 if R
= Not_A_Restriction_Id
then
5335 Set_Boolean_Result
(N
, False);
5336 Error_Msg_Node_1
:= E1
;
5337 Error_Attr
("invalid restriction identifier &", E1
);
5339 elsif R
not in Partition_Boolean_Restrictions
then
5340 Set_Boolean_Result
(N
, False);
5341 Error_Msg_Node_1
:= E1
;
5343 ("& is not a boolean partition-wide restriction", E1
);
5346 if Restriction_Active
(R
) then
5347 Set_Boolean_Result
(N
, True);
5349 Check_Restriction
(R
, N
);
5350 Set_Boolean_Result
(N
, False);
5354 end Restriction_Set
;
5360 when Attribute_Round
=>
5362 Check_Decimal_Fixed_Point_Type
;
5363 Set_Etype
(N
, P_Base_Type
);
5365 -- Because the context is universal_real (3.5.10(12)) it is a
5366 -- legal context for a universal fixed expression. This is the
5367 -- only attribute whose functional description involves U_R.
5369 if Etype
(E1
) = Universal_Fixed
then
5371 Conv
: constant Node_Id
:= Make_Type_Conversion
(Loc
,
5372 Subtype_Mark
=> New_Occurrence_Of
(Universal_Real
, Loc
),
5373 Expression
=> Relocate_Node
(E1
));
5381 Resolve
(E1
, Any_Real
);
5387 when Attribute_Rounding
=>
5388 Check_Floating_Point_Type_1
;
5389 Set_Etype
(N
, P_Base_Type
);
5390 Resolve
(E1
, P_Base_Type
);
5396 when Attribute_Safe_Emax
=>
5397 Check_Floating_Point_Type_0
;
5398 Set_Etype
(N
, Universal_Integer
);
5404 when Attribute_Safe_First
=>
5405 Check_Floating_Point_Type_0
;
5406 Set_Etype
(N
, Universal_Real
);
5412 when Attribute_Safe_Large
=>
5415 Set_Etype
(N
, Universal_Real
);
5421 when Attribute_Safe_Last
=>
5422 Check_Floating_Point_Type_0
;
5423 Set_Etype
(N
, Universal_Real
);
5429 when Attribute_Safe_Small
=>
5432 Set_Etype
(N
, Universal_Real
);
5434 --------------------------
5435 -- Scalar_Storage_Order --
5436 --------------------------
5438 when Attribute_Scalar_Storage_Order
=> Scalar_Storage_Order
:
5440 Ent
: Entity_Id
:= Empty
;
5446 if not (Is_Record_Type
(P_Type
) or else Is_Array_Type
(P_Type
)) then
5448 -- In GNAT mode, the attribute applies to generic types as well
5449 -- as composite types, and for non-composite types always returns
5450 -- the default bit order for the target.
5452 if not (GNAT_Mode
and then Is_Generic_Type
(P_Type
))
5453 and then not In_Instance
5456 ("prefix of % attribute must be record or array type");
5458 elsif not Is_Generic_Type
(P_Type
) then
5459 if Bytes_Big_Endian
then
5460 Ent
:= RTE
(RE_High_Order_First
);
5462 Ent
:= RTE
(RE_Low_Order_First
);
5466 elsif Bytes_Big_Endian
xor Reverse_Storage_Order
(P_Type
) then
5467 Ent
:= RTE
(RE_High_Order_First
);
5470 Ent
:= RTE
(RE_Low_Order_First
);
5473 if Present
(Ent
) then
5474 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
5477 Set_Etype
(N
, RTE
(RE_Bit_Order
));
5480 -- Reset incorrect indication of staticness
5482 Set_Is_Static_Expression
(N
, False);
5483 end Scalar_Storage_Order
;
5489 when Attribute_Scale
=>
5491 Check_Decimal_Fixed_Point_Type
;
5492 Set_Etype
(N
, Universal_Integer
);
5498 when Attribute_Scaling
=>
5499 Check_Floating_Point_Type_2
;
5500 Set_Etype
(N
, P_Base_Type
);
5501 Resolve
(E1
, P_Base_Type
);
5507 when Attribute_Signed_Zeros
=>
5508 Check_Floating_Point_Type_0
;
5509 Set_Etype
(N
, Standard_Boolean
);
5515 when Attribute_Size | Attribute_VADS_Size
=> Size
:
5519 -- If prefix is parameterless function call, rewrite and resolve
5522 if Is_Entity_Name
(P
)
5523 and then Ekind
(Entity
(P
)) = E_Function
5527 -- Similar processing for a protected function call
5529 elsif Nkind
(P
) = N_Selected_Component
5530 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Function
5535 if Is_Object_Reference
(P
) then
5536 Check_Object_Reference
(P
);
5538 elsif Is_Entity_Name
(P
)
5539 and then (Is_Type
(Entity
(P
))
5540 or else Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
5544 elsif Nkind
(P
) = N_Type_Conversion
5545 and then not Comes_From_Source
(P
)
5549 -- Some other compilers allow dubious use of X'???'Size
5551 elsif Relaxed_RM_Semantics
5552 and then Nkind
(P
) = N_Attribute_Reference
5557 Error_Attr_P
("invalid prefix for % attribute");
5560 Check_Not_Incomplete_Type
;
5562 Set_Etype
(N
, Universal_Integer
);
5569 when Attribute_Small
=>
5572 Set_Etype
(N
, Universal_Real
);
5578 when Attribute_Storage_Pool |
5579 Attribute_Simple_Storage_Pool
=> Storage_Pool
:
5583 if Is_Access_Type
(P_Type
) then
5584 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
5586 ("cannot use % attribute for access-to-subprogram type");
5589 -- Set appropriate entity
5591 if Present
(Associated_Storage_Pool
(Root_Type
(P_Type
))) then
5592 Set_Entity
(N
, Associated_Storage_Pool
(Root_Type
(P_Type
)));
5594 Set_Entity
(N
, RTE
(RE_Global_Pool_Object
));
5597 if Attr_Id
= Attribute_Storage_Pool
then
5598 if Present
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
5599 Name_Simple_Storage_Pool_Type
))
5601 Error_Msg_Name_1
:= Aname
;
5602 Error_Msg_Warn
:= SPARK_Mode
/= On
;
5603 Error_Msg_N
("cannot use % attribute for type with simple "
5604 & "storage pool<<", N
);
5605 Error_Msg_N
("\Program_Error [<<", N
);
5608 (N
, Make_Raise_Program_Error
5609 (Sloc
(N
), Reason
=> PE_Explicit_Raise
));
5612 Set_Etype
(N
, Class_Wide_Type
(RTE
(RE_Root_Storage_Pool
)));
5614 -- In the Simple_Storage_Pool case, verify that the pool entity is
5615 -- actually of a simple storage pool type, and set the attribute's
5616 -- type to the pool object's type.
5619 if not Present
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
5620 Name_Simple_Storage_Pool_Type
))
5623 ("cannot use % attribute for type without simple " &
5627 Set_Etype
(N
, Etype
(Entity
(N
)));
5630 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5631 -- Storage_Pool since this attribute is not defined for such
5632 -- types (RM E.2.3(22)).
5634 Validate_Remote_Access_To_Class_Wide_Type
(N
);
5637 Error_Attr_P
("prefix of % attribute must be access type");
5645 when Attribute_Storage_Size
=> Storage_Size
:
5649 if Is_Task_Type
(P_Type
) then
5650 Set_Etype
(N
, Universal_Integer
);
5652 -- Use with tasks is an obsolescent feature
5654 Check_Restriction
(No_Obsolescent_Features
, P
);
5656 elsif Is_Access_Type
(P_Type
) then
5657 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
5659 ("cannot use % attribute for access-to-subprogram type");
5662 if Is_Entity_Name
(P
)
5663 and then Is_Type
(Entity
(P
))
5666 Set_Etype
(N
, Universal_Integer
);
5668 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5669 -- Storage_Size since this attribute is not defined for
5670 -- such types (RM E.2.3(22)).
5672 Validate_Remote_Access_To_Class_Wide_Type
(N
);
5674 -- The prefix is allowed to be an implicit dereference of an
5675 -- access value designating a task.
5679 Set_Etype
(N
, Universal_Integer
);
5683 Error_Attr_P
("prefix of % attribute must be access or task type");
5691 when Attribute_Storage_Unit
=>
5692 Standard_Attribute
(Ttypes
.System_Storage_Unit
);
5698 when Attribute_Stream_Size
=>
5702 if Is_Entity_Name
(P
)
5703 and then Is_Elementary_Type
(Entity
(P
))
5705 Set_Etype
(N
, Universal_Integer
);
5707 Error_Attr_P
("invalid prefix for % attribute");
5714 when Attribute_Stub_Type
=>
5718 if Is_Remote_Access_To_Class_Wide_Type
(Base_Type
(P_Type
)) then
5720 -- For a real RACW [sub]type, use corresponding stub type
5722 if not Is_Generic_Type
(P_Type
) then
5725 (Corresponding_Stub_Type
(Base_Type
(P_Type
)), Loc
));
5727 -- For a generic type (that has been marked as an RACW using the
5728 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5729 -- type. Note that if the actual is not a remote access type, the
5730 -- instantiation will fail.
5733 -- Note: we go to the underlying type here because the view
5734 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5738 (Underlying_Type
(RTE
(RE_RACW_Stub_Type
)), Loc
));
5743 ("prefix of% attribute must be remote access to classwide");
5750 when Attribute_Succ
=>
5754 if Is_Real_Type
(P_Type
) or else Is_Boolean_Type
(P_Type
) then
5755 Error_Msg_Name_1
:= Aname
;
5756 Error_Msg_Name_2
:= Chars
(P_Type
);
5757 Check_SPARK_05_Restriction
5758 ("attribute% is not allowed for type%", P
);
5761 Resolve
(E1
, P_Base_Type
);
5762 Set_Etype
(N
, P_Base_Type
);
5764 -- Since Pred works on the base type, we normally do no check for the
5765 -- floating-point case, since the base type is unconstrained. But we
5766 -- make an exception in Check_Float_Overflow mode.
5768 if Is_Floating_Point_Type
(P_Type
) then
5769 if not Range_Checks_Suppressed
(P_Base_Type
) then
5770 Set_Do_Range_Check
(E1
);
5773 -- If not modular type, test for overflow check required
5776 if not Is_Modular_Integer_Type
(P_Type
)
5777 and then not Range_Checks_Suppressed
(P_Base_Type
)
5779 Enable_Range_Check
(E1
);
5783 --------------------------------
5784 -- System_Allocator_Alignment --
5785 --------------------------------
5787 when Attribute_System_Allocator_Alignment
=>
5788 Standard_Attribute
(Ttypes
.System_Allocator_Alignment
);
5794 when Attribute_Tag
=> Tag
:
5799 if not Is_Tagged_Type
(P_Type
) then
5800 Error_Attr_P
("prefix of % attribute must be tagged");
5802 -- Next test does not apply to generated code why not, and what does
5803 -- the illegal reference mean???
5805 elsif Is_Object_Reference
(P
)
5806 and then not Is_Class_Wide_Type
(P_Type
)
5807 and then Comes_From_Source
(N
)
5810 ("% attribute can only be applied to objects " &
5811 "of class - wide type");
5814 -- The prefix cannot be an incomplete type. However, references to
5815 -- 'Tag can be generated when expanding interface conversions, and
5818 if Comes_From_Source
(N
) then
5819 Check_Not_Incomplete_Type
;
5822 -- Set appropriate type
5824 Set_Etype
(N
, RTE
(RE_Tag
));
5831 when Attribute_Target_Name
=> Target_Name
: declare
5832 TN
: constant String := Sdefault
.Target_Name
.all;
5836 Check_Standard_Prefix
;
5840 if TN
(TL
) = '/' or else TN
(TL
) = '\' then
5845 Make_String_Literal
(Loc
,
5846 Strval
=> TN
(TN
'First .. TL
)));
5847 Analyze_And_Resolve
(N
, Standard_String
);
5848 Set_Is_Static_Expression
(N
, True);
5855 when Attribute_Terminated
=>
5857 Set_Etype
(N
, Standard_Boolean
);
5864 when Attribute_To_Address
=> To_Address
: declare
5870 Check_System_Prefix
;
5872 Generate_Reference
(RTE
(RE_Address
), P
);
5873 Analyze_And_Resolve
(E1
, Any_Integer
);
5874 Set_Etype
(N
, RTE
(RE_Address
));
5876 if Is_Static_Expression
(E1
) then
5877 Set_Is_Static_Expression
(N
, True);
5880 -- OK static expression case, check range and set appropriate type
5882 if Is_OK_Static_Expression
(E1
) then
5883 Val
:= Expr_Value
(E1
);
5885 if Val
< -(2 ** UI_From_Int
(Standard
'Address_Size - 1))
5887 Val
> 2 ** UI_From_Int
(Standard
'Address_Size) - 1
5889 Error_Attr
("address value out of range for % attribute", E1
);
5892 -- In most cases the expression is a numeric literal or some other
5893 -- address expression, but if it is a declared constant it may be
5894 -- of a compatible type that must be left on the node.
5896 if Is_Entity_Name
(E1
) then
5899 -- Set type to universal integer if negative
5902 Set_Etype
(E1
, Universal_Integer
);
5904 -- Otherwise set type to Unsigned_64 to accomodate max values
5907 Set_Etype
(E1
, Standard_Unsigned_64
);
5911 Set_Is_Static_Expression
(N
, True);
5918 when Attribute_To_Any
=>
5920 Check_PolyORB_Attribute
;
5921 Set_Etype
(N
, RTE
(RE_Any
));
5927 when Attribute_Truncation
=>
5928 Check_Floating_Point_Type_1
;
5929 Resolve
(E1
, P_Base_Type
);
5930 Set_Etype
(N
, P_Base_Type
);
5936 when Attribute_Type_Class
=>
5939 Check_Not_Incomplete_Type
;
5940 Set_Etype
(N
, RTE
(RE_Type_Class
));
5946 when Attribute_TypeCode
=>
5948 Check_PolyORB_Attribute
;
5949 Set_Etype
(N
, RTE
(RE_TypeCode
));
5955 when Attribute_Type_Key
=>
5959 -- This processing belongs in Eval_Attribute ???
5962 function Type_Key
return String_Id
;
5963 -- A very preliminary implementation. For now, a signature
5964 -- consists of only the type name. This is clearly incomplete
5965 -- (e.g., adding a new field to a record type should change the
5966 -- type's Type_Key attribute).
5972 function Type_Key
return String_Id
is
5973 Full_Name
: constant String_Id
:=
5974 Fully_Qualified_Name_String
(Entity
(P
));
5977 -- Copy all characters in Full_Name but the trailing NUL
5980 for J
in 1 .. String_Length
(Full_Name
) - 1 loop
5981 Store_String_Char
(Get_String_Char
(Full_Name
, Int
(J
)));
5984 Store_String_Chars
("'Type_Key");
5989 Rewrite
(N
, Make_String_Literal
(Loc
, Type_Key
));
5992 Analyze_And_Resolve
(N
, Standard_String
);
5998 when Attribute_UET_Address
=>
6000 Check_Unit_Name
(P
);
6001 Set_Etype
(N
, RTE
(RE_Address
));
6003 -----------------------
6004 -- Unbiased_Rounding --
6005 -----------------------
6007 when Attribute_Unbiased_Rounding
=>
6008 Check_Floating_Point_Type_1
;
6009 Set_Etype
(N
, P_Base_Type
);
6010 Resolve
(E1
, P_Base_Type
);
6012 ----------------------
6013 -- Unchecked_Access --
6014 ----------------------
6016 when Attribute_Unchecked_Access
=>
6017 if Comes_From_Source
(N
) then
6018 Check_Restriction
(No_Unchecked_Access
, N
);
6021 Analyze_Access_Attribute
;
6023 -------------------------
6024 -- Unconstrained_Array --
6025 -------------------------
6027 when Attribute_Unconstrained_Array
=>
6030 Check_Not_Incomplete_Type
;
6031 Set_Etype
(N
, Standard_Boolean
);
6032 Set_Is_Static_Expression
(N
, True);
6034 ------------------------------
6035 -- Universal_Literal_String --
6036 ------------------------------
6038 -- This is a GNAT specific attribute whose prefix must be a named
6039 -- number where the expression is either a single numeric literal,
6040 -- or a numeric literal immediately preceded by a minus sign. The
6041 -- result is equivalent to a string literal containing the text of
6042 -- the literal as it appeared in the source program with a possible
6043 -- leading minus sign.
6045 when Attribute_Universal_Literal_String
=> Universal_Literal_String
:
6049 if not Is_Entity_Name
(P
)
6050 or else Ekind
(Entity
(P
)) not in Named_Kind
6052 Error_Attr_P
("prefix for % attribute must be named number");
6059 Src
: Source_Buffer_Ptr
;
6062 Expr
:= Original_Node
(Expression
(Parent
(Entity
(P
))));
6064 if Nkind
(Expr
) = N_Op_Minus
then
6066 Expr
:= Original_Node
(Right_Opnd
(Expr
));
6071 if not Nkind_In
(Expr
, N_Integer_Literal
, N_Real_Literal
) then
6073 ("named number for % attribute must be simple literal", N
);
6076 -- Build string literal corresponding to source literal text
6081 Store_String_Char
(Get_Char_Code
('-'));
6085 Src
:= Source_Text
(Get_Source_File_Index
(S
));
6087 while Src
(S
) /= ';' and then Src
(S
) /= ' ' loop
6088 Store_String_Char
(Get_Char_Code
(Src
(S
)));
6092 -- Now we rewrite the attribute with the string literal
6095 Make_String_Literal
(Loc
, End_String
));
6097 Set_Is_Static_Expression
(N
, True);
6100 end Universal_Literal_String
;
6102 -------------------------
6103 -- Unrestricted_Access --
6104 -------------------------
6106 -- This is a GNAT specific attribute which is like Access except that
6107 -- all scope checks and checks for aliased views are omitted. It is
6108 -- documented as being equivalent to the use of the Address attribute
6109 -- followed by an unchecked conversion to the target access type.
6111 when Attribute_Unrestricted_Access
=>
6113 -- If from source, deal with relevant restrictions
6115 if Comes_From_Source
(N
) then
6116 Check_Restriction
(No_Unchecked_Access
, N
);
6118 if Nkind
(P
) in N_Has_Entity
6119 and then Present
(Entity
(P
))
6120 and then Is_Object
(Entity
(P
))
6122 Check_Restriction
(No_Implicit_Aliasing
, N
);
6126 if Is_Entity_Name
(P
) then
6127 Set_Address_Taken
(Entity
(P
));
6130 -- It might seem reasonable to call Address_Checks here to apply the
6131 -- same set of semantic checks that we enforce for 'Address (after
6132 -- all we document Unrestricted_Access as being equivalent to the
6133 -- use of Address followed by an Unchecked_Conversion). However, if
6134 -- we do enable these checks, we get multiple failures in both the
6135 -- compiler run-time and in our regression test suite, so we leave
6136 -- out these checks for now. To be investigated further some time???
6140 -- Now complete analysis using common access processing
6142 Analyze_Access_Attribute
;
6148 when Attribute_Update
=> Update
: declare
6149 Common_Typ
: Entity_Id
;
6150 -- The common type of a multiple component update for a record
6152 Comps
: Elist_Id
:= No_Elist
;
6153 -- A list used in the resolution of a record update. It contains the
6154 -- entities of all record components processed so far.
6156 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
);
6157 -- Analyze and resolve array_component_association Assoc against the
6158 -- index of array type P_Type.
6160 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
);
6161 -- Analyze and resolve record_component_association Comp against
6162 -- record type P_Type.
6164 ------------------------------------
6165 -- Analyze_Array_Component_Update --
6166 ------------------------------------
6168 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
) is
6172 Index_Typ
: Entity_Id
;
6176 -- The current association contains a sequence of indexes denoting
6177 -- an element of a multidimensional array:
6179 -- (Index_1, ..., Index_N)
6181 -- Examine each individual index and resolve it against the proper
6182 -- index type of the array.
6184 if Nkind
(First
(Choices
(Assoc
))) = N_Aggregate
then
6185 Expr
:= First
(Choices
(Assoc
));
6186 while Present
(Expr
) loop
6188 -- The use of others is illegal (SPARK RM 4.4.1(12))
6190 if Nkind
(Expr
) = N_Others_Choice
then
6192 ("others choice not allowed in attribute %", Expr
);
6194 -- Otherwise analyze and resolve all indexes
6197 Index
:= First
(Expressions
(Expr
));
6198 Index_Typ
:= First_Index
(P_Type
);
6199 while Present
(Index
) and then Present
(Index_Typ
) loop
6200 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
6202 Next_Index
(Index_Typ
);
6205 -- Detect a case where the association either lacks an
6206 -- index or contains an extra index.
6208 if Present
(Index
) or else Present
(Index_Typ
) then
6210 ("dimension mismatch in index list", Assoc
);
6217 -- The current association denotes either a single component or a
6218 -- range of components of a one dimensional array:
6222 -- Resolve the index or its high and low bounds (if range) against
6223 -- the proper index type of the array.
6226 Index
:= First
(Choices
(Assoc
));
6227 Index_Typ
:= First_Index
(P_Type
);
6229 if Present
(Next_Index
(Index_Typ
)) then
6230 Error_Msg_N
("too few subscripts in array reference", Assoc
);
6233 while Present
(Index
) loop
6235 -- The use of others is illegal (SPARK RM 4.4.1(12))
6237 if Nkind
(Index
) = N_Others_Choice
then
6239 ("others choice not allowed in attribute %", Index
);
6241 -- The index denotes a range of elements
6243 elsif Nkind
(Index
) = N_Range
then
6244 Low
:= Low_Bound
(Index
);
6245 High
:= High_Bound
(Index
);
6247 Analyze_And_Resolve
(Low
, Etype
(Index_Typ
));
6248 Analyze_And_Resolve
(High
, Etype
(Index_Typ
));
6250 -- Add a range check to ensure that the bounds of the
6251 -- range are within the index type when this cannot be
6252 -- determined statically.
6254 if not Is_OK_Static_Expression
(Low
) then
6255 Set_Do_Range_Check
(Low
);
6258 if not Is_OK_Static_Expression
(High
) then
6259 Set_Do_Range_Check
(High
);
6262 -- Otherwise the index denotes a single element
6265 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
6267 -- Add a range check to ensure that the index is within
6268 -- the index type when it is not possible to determine
6271 if not Is_OK_Static_Expression
(Index
) then
6272 Set_Do_Range_Check
(Index
);
6279 end Analyze_Array_Component_Update
;
6281 -------------------------------------
6282 -- Analyze_Record_Component_Update --
6283 -------------------------------------
6285 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
) is
6286 Comp_Name
: constant Name_Id
:= Chars
(Comp
);
6287 Base_Typ
: Entity_Id
;
6288 Comp_Or_Discr
: Entity_Id
;
6291 -- Find the discriminant or component whose name corresponds to
6292 -- Comp. A simple character comparison is sufficient because all
6293 -- visible names within a record type are unique.
6295 Comp_Or_Discr
:= First_Entity
(P_Type
);
6296 while Present
(Comp_Or_Discr
) loop
6297 if Chars
(Comp_Or_Discr
) = Comp_Name
then
6299 -- Decorate the component reference by setting its entity
6300 -- and type for resolution purposes.
6302 Set_Entity
(Comp
, Comp_Or_Discr
);
6303 Set_Etype
(Comp
, Etype
(Comp_Or_Discr
));
6307 Comp_Or_Discr
:= Next_Entity
(Comp_Or_Discr
);
6310 -- Diagnose an illegal reference
6312 if Present
(Comp_Or_Discr
) then
6313 if Ekind
(Comp_Or_Discr
) = E_Discriminant
then
6315 ("attribute % may not modify record discriminants", Comp
);
6317 else pragma Assert
(Ekind
(Comp_Or_Discr
) = E_Component
);
6318 if Contains
(Comps
, Comp_Or_Discr
) then
6319 Error_Msg_N
("component & already updated", Comp
);
6321 -- Mark this component as processed
6324 Append_New_Elmt
(Comp_Or_Discr
, Comps
);
6328 -- The update aggregate mentions an entity that does not belong to
6332 Error_Msg_N
("& is not a component of aggregate subtype", Comp
);
6335 -- Verify the consistency of types when the current component is
6336 -- part of a miltiple component update.
6338 -- Comp_1, ..., Comp_N => <value>
6340 if Present
(Etype
(Comp
)) then
6341 Base_Typ
:= Base_Type
(Etype
(Comp
));
6343 -- Save the type of the first component reference as the
6344 -- remaning references (if any) must resolve to this type.
6346 if No
(Common_Typ
) then
6347 Common_Typ
:= Base_Typ
;
6349 elsif Base_Typ
/= Common_Typ
then
6351 ("components in choice list must have same type", Comp
);
6354 end Analyze_Record_Component_Update
;
6361 -- Start of processing for Update
6366 if not Is_Object_Reference
(P
) then
6367 Error_Attr_P
("prefix of attribute % must denote an object");
6369 elsif not Is_Array_Type
(P_Type
)
6370 and then not Is_Record_Type
(P_Type
)
6372 Error_Attr_P
("prefix of attribute % must be a record or array");
6374 elsif Is_Limited_View
(P_Type
) then
6375 Error_Attr
("prefix of attribute % cannot be limited", N
);
6377 elsif Nkind
(E1
) /= N_Aggregate
then
6378 Error_Attr
("attribute % requires component association list", N
);
6381 -- Inspect the update aggregate, looking at all the associations and
6382 -- choices. Perform the following checks:
6384 -- 1) Legality of "others" in all cases
6385 -- 2) Legality of <>
6386 -- 3) Component legality for arrays
6387 -- 4) Component legality for records
6389 -- The remaining checks are performed on the expanded attribute
6391 Assoc
:= First
(Component_Associations
(E1
));
6392 while Present
(Assoc
) loop
6394 -- The use of <> is illegal (SPARK RM 4.4.1(1))
6396 if Box_Present
(Assoc
) then
6398 ("default initialization not allowed in attribute %", Assoc
);
6400 -- Otherwise process the association
6403 Analyze
(Expression
(Assoc
));
6405 if Is_Array_Type
(P_Type
) then
6406 Analyze_Array_Component_Update
(Assoc
);
6408 elsif Is_Record_Type
(P_Type
) then
6410 -- Reset the common type used in a multiple component update
6411 -- as we are processing the contents of a new association.
6413 Common_Typ
:= Empty
;
6415 Comp
:= First
(Choices
(Assoc
));
6416 while Present
(Comp
) loop
6417 if Nkind
(Comp
) = N_Identifier
then
6418 Analyze_Record_Component_Update
(Comp
);
6420 -- The use of others is illegal (SPARK RM 4.4.1(5))
6422 elsif Nkind
(Comp
) = N_Others_Choice
then
6424 ("others choice not allowed in attribute %", Comp
);
6426 -- The name of a record component cannot appear in any
6431 ("name should be identifier or OTHERS", Comp
);
6442 -- The type of attribute 'Update is that of the prefix
6444 Set_Etype
(N
, P_Type
);
6446 Sem_Warn
.Warn_On_Suspicious_Update
(N
);
6453 when Attribute_Val
=> Val
: declare
6456 Check_Discrete_Type
;
6458 if Is_Boolean_Type
(P_Type
) then
6459 Error_Msg_Name_1
:= Aname
;
6460 Error_Msg_Name_2
:= Chars
(P_Type
);
6461 Check_SPARK_05_Restriction
6462 ("attribute% is not allowed for type%", P
);
6465 Resolve
(E1
, Any_Integer
);
6466 Set_Etype
(N
, P_Base_Type
);
6468 -- Note, we need a range check in general, but we wait for the
6469 -- Resolve call to do this, since we want to let Eval_Attribute
6470 -- have a chance to find an static illegality first.
6477 when Attribute_Valid
=>
6480 -- Ignore check for object if we have a 'Valid reference generated
6481 -- by the expanded code, since in some cases valid checks can occur
6482 -- on items that are names, but are not objects (e.g. attributes).
6484 if Comes_From_Source
(N
) then
6485 Check_Object_Reference
(P
);
6488 if not Is_Scalar_Type
(P_Type
) then
6489 Error_Attr_P
("object for % attribute must be of scalar type");
6492 -- If the attribute appears within the subtype's own predicate
6493 -- function, then issue a warning that this will cause infinite
6497 Pred_Func
: constant Entity_Id
:= Predicate_Function
(P_Type
);
6500 if Present
(Pred_Func
) and then Current_Scope
= Pred_Func
then
6502 ("attribute Valid requires a predicate check??", N
);
6503 Error_Msg_N
("\and will result in infinite recursion??", N
);
6507 Set_Etype
(N
, Standard_Boolean
);
6513 when Attribute_Valid_Scalars
=>
6515 Check_Object_Reference
(P
);
6516 Set_Etype
(N
, Standard_Boolean
);
6518 -- Following checks are only for source types
6520 if Comes_From_Source
(N
) then
6521 if not Scalar_Part_Present
(P_Type
) then
6523 ("??attribute % always True, no scalars to check");
6526 -- Not allowed for unchecked union type
6528 if Has_Unchecked_Union
(P_Type
) then
6530 ("attribute % not allowed for Unchecked_Union type");
6538 when Attribute_Value
=> Value
:
6540 Check_SPARK_05_Restriction_On_Attribute
;
6544 -- Case of enumeration type
6546 -- When an enumeration type appears in an attribute reference, all
6547 -- literals of the type are marked as referenced. This must only be
6548 -- done if the attribute reference appears in the current source.
6549 -- Otherwise the information on references may differ between a
6550 -- normal compilation and one that performs inlining.
6552 if Is_Enumeration_Type
(P_Type
)
6553 and then In_Extended_Main_Code_Unit
(N
)
6555 Check_Restriction
(No_Enumeration_Maps
, N
);
6557 -- Mark all enumeration literals as referenced, since the use of
6558 -- the Value attribute can implicitly reference any of the
6559 -- literals of the enumeration base type.
6562 Ent
: Entity_Id
:= First_Literal
(P_Base_Type
);
6564 while Present
(Ent
) loop
6565 Set_Referenced
(Ent
);
6571 -- Set Etype before resolving expression because expansion of
6572 -- expression may require enclosing type. Note that the type
6573 -- returned by 'Value is the base type of the prefix type.
6575 Set_Etype
(N
, P_Base_Type
);
6576 Validate_Non_Static_Attribute_Function_Call
;
6578 -- Check restriction No_Fixed_IO
6580 if Restriction_Check_Required
(No_Fixed_IO
)
6581 and then Is_Fixed_Point_Type
(P_Type
)
6583 Check_Restriction
(No_Fixed_IO
, P
);
6591 when Attribute_Value_Size
=>
6594 Check_Not_Incomplete_Type
;
6595 Set_Etype
(N
, Universal_Integer
);
6601 when Attribute_Version
=>
6604 Set_Etype
(N
, RTE
(RE_Version_String
));
6610 when Attribute_Wchar_T_Size
=>
6611 Standard_Attribute
(Interfaces_Wchar_T_Size
);
6617 when Attribute_Wide_Image
=> Wide_Image
:
6619 Check_SPARK_05_Restriction_On_Attribute
;
6621 Set_Etype
(N
, Standard_Wide_String
);
6623 Resolve
(E1
, P_Base_Type
);
6624 Validate_Non_Static_Attribute_Function_Call
;
6626 -- Check restriction No_Fixed_IO
6628 if Restriction_Check_Required
(No_Fixed_IO
)
6629 and then Is_Fixed_Point_Type
(P_Type
)
6631 Check_Restriction
(No_Fixed_IO
, P
);
6635 ---------------------
6636 -- Wide_Wide_Image --
6637 ---------------------
6639 when Attribute_Wide_Wide_Image
=> Wide_Wide_Image
:
6642 Set_Etype
(N
, Standard_Wide_Wide_String
);
6644 Resolve
(E1
, P_Base_Type
);
6645 Validate_Non_Static_Attribute_Function_Call
;
6647 -- Check restriction No_Fixed_IO
6649 if Restriction_Check_Required
(No_Fixed_IO
)
6650 and then Is_Fixed_Point_Type
(P_Type
)
6652 Check_Restriction
(No_Fixed_IO
, P
);
6654 end Wide_Wide_Image
;
6660 when Attribute_Wide_Value
=> Wide_Value
:
6662 Check_SPARK_05_Restriction_On_Attribute
;
6666 -- Set Etype before resolving expression because expansion
6667 -- of expression may require enclosing type.
6669 Set_Etype
(N
, P_Type
);
6670 Validate_Non_Static_Attribute_Function_Call
;
6672 -- Check restriction No_Fixed_IO
6674 if Restriction_Check_Required
(No_Fixed_IO
)
6675 and then Is_Fixed_Point_Type
(P_Type
)
6677 Check_Restriction
(No_Fixed_IO
, P
);
6681 ---------------------
6682 -- Wide_Wide_Value --
6683 ---------------------
6685 when Attribute_Wide_Wide_Value
=> Wide_Wide_Value
:
6690 -- Set Etype before resolving expression because expansion
6691 -- of expression may require enclosing type.
6693 Set_Etype
(N
, P_Type
);
6694 Validate_Non_Static_Attribute_Function_Call
;
6696 -- Check restriction No_Fixed_IO
6698 if Restriction_Check_Required
(No_Fixed_IO
)
6699 and then Is_Fixed_Point_Type
(P_Type
)
6701 Check_Restriction
(No_Fixed_IO
, P
);
6703 end Wide_Wide_Value
;
6705 ---------------------
6706 -- Wide_Wide_Width --
6707 ---------------------
6709 when Attribute_Wide_Wide_Width
=>
6712 Set_Etype
(N
, Universal_Integer
);
6718 when Attribute_Wide_Width
=>
6719 Check_SPARK_05_Restriction_On_Attribute
;
6722 Set_Etype
(N
, Universal_Integer
);
6728 when Attribute_Width
=>
6729 Check_SPARK_05_Restriction_On_Attribute
;
6732 Set_Etype
(N
, Universal_Integer
);
6738 when Attribute_Word_Size
=>
6739 Standard_Attribute
(System_Word_Size
);
6745 when Attribute_Write
=>
6747 Check_Stream_Attribute
(TSS_Stream_Write
);
6748 Set_Etype
(N
, Standard_Void_Type
);
6749 Resolve
(N
, Standard_Void_Type
);
6753 -- All errors raise Bad_Attribute, so that we get out before any further
6754 -- damage occurs when an error is detected (for example, if we check for
6755 -- one attribute expression, and the check succeeds, we want to be able
6756 -- to proceed securely assuming that an expression is in fact present.
6758 -- Note: we set the attribute analyzed in this case to prevent any
6759 -- attempt at reanalysis which could generate spurious error msgs.
6762 when Bad_Attribute
=>
6764 Set_Etype
(N
, Any_Type
);
6766 end Analyze_Attribute
;
6768 --------------------
6769 -- Eval_Attribute --
6770 --------------------
6772 procedure Eval_Attribute
(N
: Node_Id
) is
6773 Loc
: constant Source_Ptr
:= Sloc
(N
);
6774 Aname
: constant Name_Id
:= Attribute_Name
(N
);
6775 Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
6776 P
: constant Node_Id
:= Prefix
(N
);
6778 C_Type
: constant Entity_Id
:= Etype
(N
);
6779 -- The type imposed by the context
6782 -- First expression, or Empty if none
6785 -- Second expression, or Empty if none
6787 P_Entity
: Entity_Id
;
6788 -- Entity denoted by prefix
6791 -- The type of the prefix
6793 P_Base_Type
: Entity_Id
;
6794 -- The base type of the prefix type
6796 P_Root_Type
: Entity_Id
;
6797 -- The root type of the prefix type
6800 -- True if the result is Static. This is set by the general processing
6801 -- to true if the prefix is static, and all expressions are static. It
6802 -- can be reset as processing continues for particular attributes. This
6803 -- flag can still be True if the reference raises a constraint error.
6804 -- Is_Static_Expression (N) is set to follow this value as it is set
6805 -- and we could always reference this, but it is convenient to have a
6806 -- simple short name to use, since it is frequently referenced.
6808 Lo_Bound
, Hi_Bound
: Node_Id
;
6809 -- Expressions for low and high bounds of type or array index referenced
6810 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6813 -- Constraint error node used if we have an attribute reference has
6814 -- an argument that raises a constraint error. In this case we replace
6815 -- the attribute with a raise constraint_error node. This is important
6816 -- processing, since otherwise gigi might see an attribute which it is
6817 -- unprepared to deal with.
6819 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
);
6820 -- If Bound is a reference to a discriminant of a task or protected type
6821 -- occurring within the object's body, rewrite attribute reference into
6822 -- a reference to the corresponding discriminal. Use for the expansion
6823 -- of checks against bounds of entry family index subtypes.
6825 procedure Check_Expressions
;
6826 -- In case where the attribute is not foldable, the expressions, if
6827 -- any, of the attribute, are in a non-static context. This procedure
6828 -- performs the required additional checks.
6830 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean;
6831 -- Determines if the given type has compile time known bounds. Note
6832 -- that we enter the case statement even in cases where the prefix
6833 -- type does NOT have known bounds, so it is important to guard any
6834 -- attempt to evaluate both bounds with a call to this function.
6836 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
);
6837 -- This procedure is called when the attribute N has a non-static
6838 -- but compile time known value given by Val. It includes the
6839 -- necessary checks for out of range values.
6841 function Fore_Value
return Nat
;
6842 -- Computes the Fore value for the current attribute prefix, which is
6843 -- known to be a static fixed-point type. Used by Fore and Width.
6845 function Mantissa
return Uint
;
6846 -- Returns the Mantissa value for the prefix type
6848 procedure Set_Bounds
;
6849 -- Used for First, Last and Length attributes applied to an array or
6850 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
6851 -- and high bound expressions for the index referenced by the attribute
6852 -- designator (i.e. the first index if no expression is present, and the
6853 -- N'th index if the value N is present as an expression). Also used for
6854 -- First and Last of scalar types and for First_Valid and Last_Valid.
6855 -- Static is reset to False if the type or index type is not statically
6858 function Statically_Denotes_Entity
(N
: Node_Id
) return Boolean;
6859 -- Verify that the prefix of a potentially static array attribute
6860 -- satisfies the conditions of 4.9 (14).
6862 -----------------------------------
6863 -- Check_Concurrent_Discriminant --
6864 -----------------------------------
6866 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
) is
6868 -- The concurrent (task or protected) type
6871 if Nkind
(Bound
) = N_Identifier
6872 and then Ekind
(Entity
(Bound
)) = E_Discriminant
6873 and then Is_Concurrent_Record_Type
(Scope
(Entity
(Bound
)))
6875 Tsk
:= Corresponding_Concurrent_Type
(Scope
(Entity
(Bound
)));
6877 if In_Open_Scopes
(Tsk
) and then Has_Completion
(Tsk
) then
6879 -- Find discriminant of original concurrent type, and use
6880 -- its current discriminal, which is the renaming within
6881 -- the task/protected body.
6885 (Find_Body_Discriminal
(Entity
(Bound
)), Loc
));
6888 end Check_Concurrent_Discriminant
;
6890 -----------------------
6891 -- Check_Expressions --
6892 -----------------------
6894 procedure Check_Expressions
is
6898 while Present
(E
) loop
6899 Check_Non_Static_Context
(E
);
6902 end Check_Expressions
;
6904 ----------------------------------
6905 -- Compile_Time_Known_Attribute --
6906 ----------------------------------
6908 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
) is
6909 T
: constant Entity_Id
:= Etype
(N
);
6912 Fold_Uint
(N
, Val
, False);
6914 -- Check that result is in bounds of the type if it is static
6916 if Is_In_Range
(N
, T
, Assume_Valid
=> False) then
6919 elsif Is_Out_Of_Range
(N
, T
) then
6920 Apply_Compile_Time_Constraint_Error
6921 (N
, "value not in range of}??", CE_Range_Check_Failed
);
6923 elsif not Range_Checks_Suppressed
(T
) then
6924 Enable_Range_Check
(N
);
6927 Set_Do_Range_Check
(N
, False);
6929 end Compile_Time_Known_Attribute
;
6931 -------------------------------
6932 -- Compile_Time_Known_Bounds --
6933 -------------------------------
6935 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean is
6938 Compile_Time_Known_Value
(Type_Low_Bound
(Typ
))
6940 Compile_Time_Known_Value
(Type_High_Bound
(Typ
));
6941 end Compile_Time_Known_Bounds
;
6947 -- Note that the Fore calculation is based on the actual values
6948 -- of the bounds, and does not take into account possible rounding.
6950 function Fore_Value
return Nat
is
6951 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
6952 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
6953 Small
: constant Ureal
:= Small_Value
(P_Type
);
6954 Lo_Real
: constant Ureal
:= Lo
* Small
;
6955 Hi_Real
: constant Ureal
:= Hi
* Small
;
6960 -- Bounds are given in terms of small units, so first compute
6961 -- proper values as reals.
6963 T
:= UR_Max
(abs Lo_Real
, abs Hi_Real
);
6966 -- Loop to compute proper value if more than one digit required
6968 while T
>= Ureal_10
loop
6980 -- Table of mantissa values accessed by function Computed using
6983 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
6985 -- where D is T'Digits (RM83 3.5.7)
6987 Mantissa_Value
: constant array (Nat
range 1 .. 40) of Nat
:= (
7029 function Mantissa
return Uint
is
7032 UI_From_Int
(Mantissa_Value
(UI_To_Int
(Digits_Value
(P_Type
))));
7039 procedure Set_Bounds
is
7045 -- For a string literal subtype, we have to construct the bounds.
7046 -- Valid Ada code never applies attributes to string literals, but
7047 -- it is convenient to allow the expander to generate attribute
7048 -- references of this type (e.g. First and Last applied to a string
7051 -- Note that the whole point of the E_String_Literal_Subtype is to
7052 -- avoid this construction of bounds, but the cases in which we
7053 -- have to materialize them are rare enough that we don't worry.
7055 -- The low bound is simply the low bound of the base type. The
7056 -- high bound is computed from the length of the string and this
7059 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
7060 Ityp
:= Etype
(First_Index
(Base_Type
(P_Type
)));
7061 Lo_Bound
:= Type_Low_Bound
(Ityp
);
7064 Make_Integer_Literal
(Sloc
(P
),
7066 Expr_Value
(Lo_Bound
) + String_Literal_Length
(P_Type
) - 1);
7068 Set_Parent
(Hi_Bound
, P
);
7069 Analyze_And_Resolve
(Hi_Bound
, Etype
(Lo_Bound
));
7072 -- For non-array case, just get bounds of scalar type
7074 elsif Is_Scalar_Type
(P_Type
) then
7077 -- For a fixed-point type, we must freeze to get the attributes
7078 -- of the fixed-point type set now so we can reference them.
7080 if Is_Fixed_Point_Type
(P_Type
)
7081 and then not Is_Frozen
(Base_Type
(P_Type
))
7082 and then Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
7083 and then Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
7085 Freeze_Fixed_Point_Type
(Base_Type
(P_Type
));
7088 -- For array case, get type of proper index
7094 Ndim
:= UI_To_Int
(Expr_Value
(E1
));
7097 Indx
:= First_Index
(P_Type
);
7098 for J
in 1 .. Ndim
- 1 loop
7102 -- If no index type, get out (some other error occurred, and
7103 -- we don't have enough information to complete the job).
7111 Ityp
:= Etype
(Indx
);
7114 -- A discrete range in an index constraint is allowed to be a
7115 -- subtype indication. This is syntactically a pain, but should
7116 -- not propagate to the entity for the corresponding index subtype.
7117 -- After checking that the subtype indication is legal, the range
7118 -- of the subtype indication should be transfered to the entity.
7119 -- The attributes for the bounds should remain the simple retrievals
7120 -- that they are now.
7122 Lo_Bound
:= Type_Low_Bound
(Ityp
);
7123 Hi_Bound
:= Type_High_Bound
(Ityp
);
7125 -- If subtype is non-static, result is definitely non-static
7127 if not Is_Static_Subtype
(Ityp
) then
7129 Set_Is_Static_Expression
(N
, False);
7131 -- Subtype is static, does it raise CE?
7133 elsif not Is_OK_Static_Subtype
(Ityp
) then
7134 Set_Raises_Constraint_Error
(N
);
7138 -------------------------------
7139 -- Statically_Denotes_Entity --
7140 -------------------------------
7142 function Statically_Denotes_Entity
(N
: Node_Id
) return Boolean is
7146 if not Is_Entity_Name
(N
) then
7153 Nkind
(Parent
(E
)) /= N_Object_Renaming_Declaration
7154 or else Statically_Denotes_Entity
(Renamed_Object
(E
));
7155 end Statically_Denotes_Entity
;
7157 -- Start of processing for Eval_Attribute
7160 -- Initialize result as non-static, will be reset if appropriate
7162 Set_Is_Static_Expression
(N
, False);
7165 -- Acquire first two expressions (at the moment, no attributes take more
7166 -- than two expressions in any case).
7168 if Present
(Expressions
(N
)) then
7169 E1
:= First
(Expressions
(N
));
7176 -- Special processing for Enabled attribute. This attribute has a very
7177 -- special prefix, and the easiest way to avoid lots of special checks
7178 -- to protect this special prefix from causing trouble is to deal with
7179 -- this attribute immediately and be done with it.
7181 if Id
= Attribute_Enabled
then
7183 -- We skip evaluation if the expander is not active. This is not just
7184 -- an optimization. It is of key importance that we not rewrite the
7185 -- attribute in a generic template, since we want to pick up the
7186 -- setting of the check in the instance, and testing expander active
7187 -- is as easy way of doing this as any.
7189 if Expander_Active
then
7191 C
: constant Check_Id
:= Get_Check_Id
(Chars
(P
));
7196 if C
in Predefined_Check_Id
then
7197 R
:= Scope_Suppress
.Suppress
(C
);
7199 R
:= Is_Check_Suppressed
(Empty
, C
);
7203 R
:= Is_Check_Suppressed
(Entity
(E1
), C
);
7206 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(not R
), Loc
));
7213 -- Attribute 'Img applied to a static enumeration value is static, and
7214 -- we will do the folding right here (things get confused if we let this
7215 -- case go through the normal circuitry).
7217 if Attribute_Name
(N
) = Name_Img
7218 and then Is_Entity_Name
(P
)
7219 and then Is_Enumeration_Type
(Etype
(Entity
(P
)))
7220 and then Is_OK_Static_Expression
(P
)
7223 Lit
: constant Entity_Id
:= Expr_Value_E
(P
);
7228 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
7229 Set_Casing
(All_Upper_Case
);
7230 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
7233 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
7234 Analyze_And_Resolve
(N
, Standard_String
);
7235 Set_Is_Static_Expression
(N
, True);
7241 -- Special processing for cases where the prefix is an object. For
7242 -- this purpose, a string literal counts as an object (attributes
7243 -- of string literals can only appear in generated code).
7245 if Is_Object_Reference
(P
) or else Nkind
(P
) = N_String_Literal
then
7247 -- For Component_Size, the prefix is an array object, and we apply
7248 -- the attribute to the type of the object. This is allowed for
7249 -- both unconstrained and constrained arrays, since the bounds
7250 -- have no influence on the value of this attribute.
7252 if Id
= Attribute_Component_Size
then
7253 P_Entity
:= Etype
(P
);
7255 -- For First and Last, the prefix is an array object, and we apply
7256 -- the attribute to the type of the array, but we need a constrained
7257 -- type for this, so we use the actual subtype if available.
7259 elsif Id
= Attribute_First
or else
7260 Id
= Attribute_Last
or else
7261 Id
= Attribute_Length
7264 AS
: constant Entity_Id
:= Get_Actual_Subtype_If_Available
(P
);
7267 if Present
(AS
) and then Is_Constrained
(AS
) then
7270 -- If we have an unconstrained type we cannot fold
7278 -- For Size, give size of object if available, otherwise we
7279 -- cannot fold Size.
7281 elsif Id
= Attribute_Size
then
7282 if Is_Entity_Name
(P
)
7283 and then Known_Esize
(Entity
(P
))
7285 Compile_Time_Known_Attribute
(N
, Esize
(Entity
(P
)));
7293 -- For Alignment, give size of object if available, otherwise we
7294 -- cannot fold Alignment.
7296 elsif Id
= Attribute_Alignment
then
7297 if Is_Entity_Name
(P
)
7298 and then Known_Alignment
(Entity
(P
))
7300 Fold_Uint
(N
, Alignment
(Entity
(P
)), Static
);
7308 -- For Lock_Free, we apply the attribute to the type of the object.
7309 -- This is allowed since we have already verified that the type is a
7312 elsif Id
= Attribute_Lock_Free
then
7313 P_Entity
:= Etype
(P
);
7315 -- No other attributes for objects are folded
7322 -- Cases where P is not an object. Cannot do anything if P is not the
7323 -- name of an entity.
7325 elsif not Is_Entity_Name
(P
) then
7329 -- Otherwise get prefix entity
7332 P_Entity
:= Entity
(P
);
7335 -- If we are asked to evaluate an attribute where the prefix is a
7336 -- non-frozen generic actual type whose RM_Size is still set to zero,
7337 -- then abandon the effort.
7339 if Is_Type
(P_Entity
)
7340 and then (not Is_Frozen
(P_Entity
)
7341 and then Is_Generic_Actual_Type
(P_Entity
)
7342 and then RM_Size
(P_Entity
) = 0)
7344 -- However, the attribute Unconstrained_Array must be evaluated,
7345 -- since it is documented to be a static attribute (and can for
7346 -- example appear in a Compile_Time_Warning pragma). The frozen
7347 -- status of the type does not affect its evaluation.
7349 and then Id
/= Attribute_Unconstrained_Array
7354 -- At this stage P_Entity is the entity to which the attribute
7355 -- is to be applied. This is usually simply the entity of the
7356 -- prefix, except in some cases of attributes for objects, where
7357 -- as described above, we apply the attribute to the object type.
7359 -- Here is where we make sure that static attributes are properly
7360 -- marked as such. These are attributes whose prefix is a static
7361 -- scalar subtype, whose result is scalar, and whose arguments, if
7362 -- present, are static scalar expressions. Note that such references
7363 -- are static expressions even if they raise Constraint_Error.
7365 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7366 -- though evaluating it raises constraint error. This means that a
7367 -- declaration like:
7369 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7371 -- is legal, since here this expression appears in a statically
7372 -- unevaluated position, so it does not actually raise an exception.
7374 if Is_Scalar_Type
(P_Entity
)
7375 and then (not Is_Generic_Type
(P_Entity
))
7376 and then Is_Static_Subtype
(P_Entity
)
7377 and then Is_Scalar_Type
(Etype
(N
))
7380 or else (Is_Static_Expression
(E1
)
7381 and then Is_Scalar_Type
(Etype
(E1
))))
7384 or else (Is_Static_Expression
(E2
)
7385 and then Is_Scalar_Type
(Etype
(E1
))))
7388 Set_Is_Static_Expression
(N
, True);
7391 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7392 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7393 -- Note we allow non-static non-generic types at this stage as further
7396 if Is_Type
(P_Entity
)
7397 and then (Is_Scalar_Type
(P_Entity
) or Is_Array_Type
(P_Entity
))
7398 and then (not Is_Generic_Type
(P_Entity
))
7402 -- Second foldable possibility is an array object (RM 4.9(8))
7404 elsif Ekind_In
(P_Entity
, E_Variable
, E_Constant
)
7405 and then Is_Array_Type
(Etype
(P_Entity
))
7406 and then (not Is_Generic_Type
(Etype
(P_Entity
)))
7408 P_Type
:= Etype
(P_Entity
);
7410 -- If the entity is an array constant with an unconstrained nominal
7411 -- subtype then get the type from the initial value. If the value has
7412 -- been expanded into assignments, there is no expression and the
7413 -- attribute reference remains dynamic.
7415 -- We could do better here and retrieve the type ???
7417 if Ekind
(P_Entity
) = E_Constant
7418 and then not Is_Constrained
(P_Type
)
7420 if No
(Constant_Value
(P_Entity
)) then
7423 P_Type
:= Etype
(Constant_Value
(P_Entity
));
7427 -- Definite must be folded if the prefix is not a generic type, that
7428 -- is to say if we are within an instantiation. Same processing applies
7429 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7430 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
7432 elsif (Id
= Attribute_Atomic_Always_Lock_Free
or else
7433 Id
= Attribute_Definite
or else
7434 Id
= Attribute_Has_Access_Values
or else
7435 Id
= Attribute_Has_Discriminants
or else
7436 Id
= Attribute_Has_Tagged_Values
or else
7437 Id
= Attribute_Lock_Free
or else
7438 Id
= Attribute_Type_Class
or else
7439 Id
= Attribute_Unconstrained_Array
or else
7440 Id
= Attribute_Max_Alignment_For_Allocation
)
7441 and then not Is_Generic_Type
(P_Entity
)
7445 -- We can fold 'Size applied to a type if the size is known (as happens
7446 -- for a size from an attribute definition clause). At this stage, this
7447 -- can happen only for types (e.g. record types) for which the size is
7448 -- always non-static. We exclude generic types from consideration (since
7449 -- they have bogus sizes set within templates).
7451 elsif Id
= Attribute_Size
7452 and then Is_Type
(P_Entity
)
7453 and then (not Is_Generic_Type
(P_Entity
))
7454 and then Known_Static_RM_Size
(P_Entity
)
7456 Compile_Time_Known_Attribute
(N
, RM_Size
(P_Entity
));
7459 -- We can fold 'Alignment applied to a type if the alignment is known
7460 -- (as happens for an alignment from an attribute definition clause).
7461 -- At this stage, this can happen only for types (e.g. record types) for
7462 -- which the size is always non-static. We exclude generic types from
7463 -- consideration (since they have bogus sizes set within templates).
7465 elsif Id
= Attribute_Alignment
7466 and then Is_Type
(P_Entity
)
7467 and then (not Is_Generic_Type
(P_Entity
))
7468 and then Known_Alignment
(P_Entity
)
7470 Compile_Time_Known_Attribute
(N
, Alignment
(P_Entity
));
7473 -- If this is an access attribute that is known to fail accessibility
7474 -- check, rewrite accordingly.
7476 elsif Attribute_Name
(N
) = Name_Access
7477 and then Raises_Constraint_Error
(N
)
7480 Make_Raise_Program_Error
(Loc
,
7481 Reason
=> PE_Accessibility_Check_Failed
));
7482 Set_Etype
(N
, C_Type
);
7485 -- No other cases are foldable (they certainly aren't static, and at
7486 -- the moment we don't try to fold any cases other than the ones above).
7493 -- If either attribute or the prefix is Any_Type, then propagate
7494 -- Any_Type to the result and don't do anything else at all.
7496 if P_Type
= Any_Type
7497 or else (Present
(E1
) and then Etype
(E1
) = Any_Type
)
7498 or else (Present
(E2
) and then Etype
(E2
) = Any_Type
)
7500 Set_Etype
(N
, Any_Type
);
7504 -- Scalar subtype case. We have not yet enforced the static requirement
7505 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7506 -- of non-static attribute references (e.g. S'Digits for a non-static
7507 -- floating-point type, which we can compute at compile time).
7509 -- Note: this folding of non-static attributes is not simply a case of
7510 -- optimization. For many of the attributes affected, Gigi cannot handle
7511 -- the attribute and depends on the front end having folded them away.
7513 -- Note: although we don't require staticness at this stage, we do set
7514 -- the Static variable to record the staticness, for easy reference by
7515 -- those attributes where it matters (e.g. Succ and Pred), and also to
7516 -- be used to ensure that non-static folded things are not marked as
7517 -- being static (a check that is done right at the end).
7519 P_Root_Type
:= Root_Type
(P_Type
);
7520 P_Base_Type
:= Base_Type
(P_Type
);
7522 -- If the root type or base type is generic, then we cannot fold. This
7523 -- test is needed because subtypes of generic types are not always
7524 -- marked as being generic themselves (which seems odd???)
7526 if Is_Generic_Type
(P_Root_Type
)
7527 or else Is_Generic_Type
(P_Base_Type
)
7532 if Is_Scalar_Type
(P_Type
) then
7533 if not Is_Static_Subtype
(P_Type
) then
7535 Set_Is_Static_Expression
(N
, False);
7536 elsif not Is_OK_Static_Subtype
(P_Type
) then
7537 Set_Raises_Constraint_Error
(N
);
7540 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7541 -- since we can't do anything with unconstrained arrays. In addition,
7542 -- only the First, Last and Length attributes are possibly static.
7544 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7545 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7546 -- Unconstrained_Array are again exceptions, because they apply as well
7547 -- to unconstrained types.
7549 -- In addition Component_Size is an exception since it is possibly
7550 -- foldable, even though it is never static, and it does apply to
7551 -- unconstrained arrays. Furthermore, it is essential to fold this
7552 -- in the packed case, since otherwise the value will be incorrect.
7554 elsif Id
= Attribute_Atomic_Always_Lock_Free
or else
7555 Id
= Attribute_Definite
or else
7556 Id
= Attribute_Has_Access_Values
or else
7557 Id
= Attribute_Has_Discriminants
or else
7558 Id
= Attribute_Has_Tagged_Values
or else
7559 Id
= Attribute_Lock_Free
or else
7560 Id
= Attribute_Type_Class
or else
7561 Id
= Attribute_Unconstrained_Array
or else
7562 Id
= Attribute_Component_Size
7565 Set_Is_Static_Expression
(N
, False);
7567 elsif Id
/= Attribute_Max_Alignment_For_Allocation
then
7568 if not Is_Constrained
(P_Type
)
7569 or else (Id
/= Attribute_First
and then
7570 Id
/= Attribute_Last
and then
7571 Id
/= Attribute_Length
)
7577 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7578 -- scalar case, we hold off on enforcing staticness, since there are
7579 -- cases which we can fold at compile time even though they are not
7580 -- static (e.g. 'Length applied to a static index, even though other
7581 -- non-static indexes make the array type non-static). This is only
7582 -- an optimization, but it falls out essentially free, so why not.
7583 -- Again we compute the variable Static for easy reference later
7584 -- (note that no array attributes are static in Ada 83).
7586 -- We also need to set Static properly for subsequent legality checks
7587 -- which might otherwise accept non-static constants in contexts
7588 -- where they are not legal.
7591 Ada_Version
>= Ada_95
and then Statically_Denotes_Entity
(P
);
7592 Set_Is_Static_Expression
(N
, Static
);
7598 Nod
:= First_Index
(P_Type
);
7600 -- The expression is static if the array type is constrained
7601 -- by given bounds, and not by an initial expression. Constant
7602 -- strings are static in any case.
7604 if Root_Type
(P_Type
) /= Standard_String
then
7606 Static
and then not Is_Constr_Subt_For_U_Nominal
(P_Type
);
7607 Set_Is_Static_Expression
(N
, Static
);
7610 while Present
(Nod
) loop
7611 if not Is_Static_Subtype
(Etype
(Nod
)) then
7613 Set_Is_Static_Expression
(N
, False);
7615 elsif not Is_OK_Static_Subtype
(Etype
(Nod
)) then
7616 Set_Raises_Constraint_Error
(N
);
7618 Set_Is_Static_Expression
(N
, False);
7621 -- If however the index type is generic, or derived from
7622 -- one, attributes cannot be folded.
7624 if Is_Generic_Type
(Root_Type
(Etype
(Nod
)))
7625 and then Id
/= Attribute_Component_Size
7635 -- Check any expressions that are present. Note that these expressions,
7636 -- depending on the particular attribute type, are either part of the
7637 -- attribute designator, or they are arguments in a case where the
7638 -- attribute reference returns a function. In the latter case, the
7639 -- rule in (RM 4.9(22)) applies and in particular requires the type
7640 -- of the expressions to be scalar in order for the attribute to be
7641 -- considered to be static.
7649 while Present
(E
) loop
7651 -- If expression is not static, then the attribute reference
7652 -- result certainly cannot be static.
7654 if not Is_Static_Expression
(E
) then
7656 Set_Is_Static_Expression
(N
, False);
7659 if Raises_Constraint_Error
(E
) then
7660 Set_Raises_Constraint_Error
(N
);
7663 -- If the result is not known at compile time, or is not of
7664 -- a scalar type, then the result is definitely not static,
7665 -- so we can quit now.
7667 if not Compile_Time_Known_Value
(E
)
7668 or else not Is_Scalar_Type
(Etype
(E
))
7670 -- An odd special case, if this is a Pos attribute, this
7671 -- is where we need to apply a range check since it does
7672 -- not get done anywhere else.
7674 if Id
= Attribute_Pos
then
7675 if Is_Integer_Type
(Etype
(E
)) then
7676 Apply_Range_Check
(E
, Etype
(N
));
7683 -- If the expression raises a constraint error, then so does
7684 -- the attribute reference. We keep going in this case because
7685 -- we are still interested in whether the attribute reference
7686 -- is static even if it is not static.
7688 elsif Raises_Constraint_Error
(E
) then
7689 Set_Raises_Constraint_Error
(N
);
7695 if Raises_Constraint_Error
(Prefix
(N
)) then
7696 Set_Is_Static_Expression
(N
, False);
7701 -- Deal with the case of a static attribute reference that raises
7702 -- constraint error. The Raises_Constraint_Error flag will already
7703 -- have been set, and the Static flag shows whether the attribute
7704 -- reference is static. In any case we certainly can't fold such an
7705 -- attribute reference.
7707 -- Note that the rewriting of the attribute node with the constraint
7708 -- error node is essential in this case, because otherwise Gigi might
7709 -- blow up on one of the attributes it never expects to see.
7711 -- The constraint_error node must have the type imposed by the context,
7712 -- to avoid spurious errors in the enclosing expression.
7714 if Raises_Constraint_Error
(N
) then
7716 Make_Raise_Constraint_Error
(Sloc
(N
),
7717 Reason
=> CE_Range_Check_Failed
);
7718 Set_Etype
(CE_Node
, Etype
(N
));
7719 Set_Raises_Constraint_Error
(CE_Node
);
7721 Rewrite
(N
, Relocate_Node
(CE_Node
));
7722 Set_Raises_Constraint_Error
(N
, True);
7726 -- At this point we have a potentially foldable attribute reference.
7727 -- If Static is set, then the attribute reference definitely obeys
7728 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7729 -- folded. If Static is not set, then the attribute may or may not
7730 -- be foldable, and the individual attribute processing routines
7731 -- test Static as required in cases where it makes a difference.
7733 -- In the case where Static is not set, we do know that all the
7734 -- expressions present are at least known at compile time (we assumed
7735 -- above that if this was not the case, then there was no hope of static
7736 -- evaluation). However, we did not require that the bounds of the
7737 -- prefix type be compile time known, let alone static). That's because
7738 -- there are many attributes that can be computed at compile time on
7739 -- non-static subtypes, even though such references are not static
7742 -- For VAX float, the root type is an IEEE type. So make sure to use the
7743 -- base type instead of the root-type for floating point attributes.
7747 -- Attributes related to Ada 2012 iterators (placeholder ???)
7749 when Attribute_Constant_Indexing |
7750 Attribute_Default_Iterator |
7751 Attribute_Implicit_Dereference |
7752 Attribute_Iterator_Element |
7753 Attribute_Iterable |
7754 Attribute_Variable_Indexing
=> null;
7756 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7757 -- These were already rejected by the parser. Thus they shouldn't
7760 when Internal_Attribute_Id
=>
7761 raise Program_Error
;
7767 when Attribute_Adjacent
=>
7771 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
7778 when Attribute_Aft
=>
7779 Fold_Uint
(N
, Aft_Value
(P_Type
), Static
);
7785 when Attribute_Alignment
=> Alignment_Block
: declare
7786 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
7789 -- Fold if alignment is set and not otherwise
7791 if Known_Alignment
(P_TypeA
) then
7792 Fold_Uint
(N
, Alignment
(P_TypeA
), Static
);
7794 end Alignment_Block
;
7796 -----------------------------
7797 -- Atomic_Always_Lock_Free --
7798 -----------------------------
7800 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
7803 when Attribute_Atomic_Always_Lock_Free
=> Atomic_Always_Lock_Free
:
7805 V
: constant Entity_Id
:=
7807 (Support_Atomic_Primitives_On_Target
7808 and then Support_Atomic_Primitives
(P_Type
));
7811 Rewrite
(N
, New_Occurrence_Of
(V
, Loc
));
7813 -- Analyze and resolve as boolean. Note that this attribute is a
7814 -- static attribute in GNAT.
7816 Analyze_And_Resolve
(N
, Standard_Boolean
);
7818 Set_Is_Static_Expression
(N
, True);
7819 end Atomic_Always_Lock_Free
;
7825 -- Bit can never be folded
7827 when Attribute_Bit
=>
7834 -- Body_version can never be static
7836 when Attribute_Body_Version
=>
7843 when Attribute_Ceiling
=>
7845 (N
, Eval_Fat
.Ceiling
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
7847 --------------------
7848 -- Component_Size --
7849 --------------------
7851 when Attribute_Component_Size
=>
7852 if Known_Static_Component_Size
(P_Type
) then
7853 Fold_Uint
(N
, Component_Size
(P_Type
), Static
);
7860 when Attribute_Compose
=>
7863 Eval_Fat
.Compose
(P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
7870 -- Constrained is never folded for now, there may be cases that
7871 -- could be handled at compile time. To be looked at later.
7873 when Attribute_Constrained
=>
7875 -- The expander might fold it and set the static flag accordingly,
7876 -- but with expansion disabled (as in ASIS), it remains as an
7877 -- attribute reference, and this reference is not static.
7879 Set_Is_Static_Expression
(N
, False);
7886 when Attribute_Copy_Sign
=>
7890 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
7897 when Attribute_Definite
=>
7898 Rewrite
(N
, New_Occurrence_Of
(
7899 Boolean_Literals
(not Is_Indefinite_Subtype
(P_Entity
)), Loc
));
7900 Analyze_And_Resolve
(N
, Standard_Boolean
);
7906 when Attribute_Delta
=>
7907 Fold_Ureal
(N
, Delta_Value
(P_Type
), True);
7913 when Attribute_Denorm
=>
7915 (N
, UI_From_Int
(Boolean'Pos (Has_Denormals
(P_Type
))), Static
);
7917 ---------------------
7918 -- Descriptor_Size --
7919 ---------------------
7921 when Attribute_Descriptor_Size
=>
7928 when Attribute_Digits
=>
7929 Fold_Uint
(N
, Digits_Value
(P_Type
), Static
);
7935 when Attribute_Emax
=>
7937 -- Ada 83 attribute is defined as (RM83 3.5.8)
7939 -- T'Emax = 4 * T'Mantissa
7941 Fold_Uint
(N
, 4 * Mantissa
, Static
);
7947 when Attribute_Enum_Rep
=>
7949 -- For an enumeration type with a non-standard representation use
7950 -- the Enumeration_Rep field of the proper constant. Note that this
7951 -- will not work for types Character/Wide_[Wide-]Character, since no
7952 -- real entities are created for the enumeration literals, but that
7953 -- does not matter since these two types do not have non-standard
7954 -- representations anyway.
7956 if Is_Enumeration_Type
(P_Type
)
7957 and then Has_Non_Standard_Rep
(P_Type
)
7959 Fold_Uint
(N
, Enumeration_Rep
(Expr_Value_E
(E1
)), Static
);
7961 -- For enumeration types with standard representations and all
7962 -- other cases (i.e. all integer and modular types), Enum_Rep
7963 -- is equivalent to Pos.
7966 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
7973 when Attribute_Enum_Val
=> Enum_Val
: declare
7977 -- We have something like Enum_Type'Enum_Val (23), so search for a
7978 -- corresponding value in the list of Enum_Rep values for the type.
7980 Lit
:= First_Literal
(P_Base_Type
);
7982 if Enumeration_Rep
(Lit
) = Expr_Value
(E1
) then
7983 Fold_Uint
(N
, Enumeration_Pos
(Lit
), Static
);
7990 Apply_Compile_Time_Constraint_Error
7991 (N
, "no representation value matches",
7992 CE_Range_Check_Failed
,
7993 Warn
=> not Static
);
8003 when Attribute_Epsilon
=>
8005 -- Ada 83 attribute is defined as (RM83 3.5.8)
8007 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8009 Fold_Ureal
(N
, Ureal_2
** (1 - Mantissa
), True);
8015 when Attribute_Exponent
=>
8017 Eval_Fat
.Exponent
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8023 when Attribute_First
=> First_Attr
:
8027 if Compile_Time_Known_Value
(Lo_Bound
) then
8028 if Is_Real_Type
(P_Type
) then
8029 Fold_Ureal
(N
, Expr_Value_R
(Lo_Bound
), Static
);
8031 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
8035 Check_Concurrent_Discriminant
(Lo_Bound
);
8043 when Attribute_First_Valid
=> First_Valid
:
8045 if Has_Predicates
(P_Type
)
8046 and then Has_Static_Predicate
(P_Type
)
8049 FirstN
: constant Node_Id
:=
8050 First
(Static_Discrete_Predicate
(P_Type
));
8052 if Nkind
(FirstN
) = N_Range
then
8053 Fold_Uint
(N
, Expr_Value
(Low_Bound
(FirstN
)), Static
);
8055 Fold_Uint
(N
, Expr_Value
(FirstN
), Static
);
8061 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
8069 when Attribute_Fixed_Value
=>
8076 when Attribute_Floor
=>
8078 (N
, Eval_Fat
.Floor
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8084 when Attribute_Fore
=>
8085 if Compile_Time_Known_Bounds
(P_Type
) then
8086 Fold_Uint
(N
, UI_From_Int
(Fore_Value
), Static
);
8093 when Attribute_Fraction
=>
8095 (N
, Eval_Fat
.Fraction
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8097 -----------------------
8098 -- Has_Access_Values --
8099 -----------------------
8101 when Attribute_Has_Access_Values
=>
8102 Rewrite
(N
, New_Occurrence_Of
8103 (Boolean_Literals
(Has_Access_Values
(P_Root_Type
)), Loc
));
8104 Analyze_And_Resolve
(N
, Standard_Boolean
);
8106 -----------------------
8107 -- Has_Discriminants --
8108 -----------------------
8110 when Attribute_Has_Discriminants
=>
8111 Rewrite
(N
, New_Occurrence_Of
(
8112 Boolean_Literals
(Has_Discriminants
(P_Entity
)), Loc
));
8113 Analyze_And_Resolve
(N
, Standard_Boolean
);
8115 ----------------------
8116 -- Has_Same_Storage --
8117 ----------------------
8119 when Attribute_Has_Same_Storage
=>
8122 -----------------------
8123 -- Has_Tagged_Values --
8124 -----------------------
8126 when Attribute_Has_Tagged_Values
=>
8127 Rewrite
(N
, New_Occurrence_Of
8128 (Boolean_Literals
(Has_Tagged_Component
(P_Root_Type
)), Loc
));
8129 Analyze_And_Resolve
(N
, Standard_Boolean
);
8135 when Attribute_Identity
=>
8142 -- Image is a scalar attribute, but is never static, because it is
8143 -- not a static function (having a non-scalar argument (RM 4.9(22))
8144 -- However, we can constant-fold the image of an enumeration literal
8145 -- if names are available.
8147 when Attribute_Image
=>
8148 if Is_Entity_Name
(E1
)
8149 and then Ekind
(Entity
(E1
)) = E_Enumeration_Literal
8150 and then not Discard_Names
(First_Subtype
(Etype
(E1
)))
8151 and then not Global_Discard_Names
8154 Lit
: constant Entity_Id
:= Entity
(E1
);
8158 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
8159 Set_Casing
(All_Upper_Case
);
8160 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
8162 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
8163 Analyze_And_Resolve
(N
, Standard_String
);
8164 Set_Is_Static_Expression
(N
, False);
8172 -- We never try to fold Integer_Value (though perhaps we could???)
8174 when Attribute_Integer_Value
=>
8181 -- Invalid_Value is a scalar attribute that is never static, because
8182 -- the value is by design out of range.
8184 when Attribute_Invalid_Value
=>
8191 when Attribute_Large
=>
8193 -- For fixed-point, we use the identity:
8195 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8197 if Is_Fixed_Point_Type
(P_Type
) then
8199 Make_Op_Multiply
(Loc
,
8201 Make_Op_Subtract
(Loc
,
8205 Make_Real_Literal
(Loc
, Ureal_2
),
8207 Make_Attribute_Reference
(Loc
,
8209 Attribute_Name
=> Name_Mantissa
)),
8210 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_1
)),
8213 Make_Real_Literal
(Loc
, Small_Value
(Entity
(P
)))));
8215 Analyze_And_Resolve
(N
, C_Type
);
8217 -- Floating-point (Ada 83 compatibility)
8220 -- Ada 83 attribute is defined as (RM83 3.5.8)
8222 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8226 -- T'Emax = 4 * T'Mantissa
8230 Ureal_2
** (4 * Mantissa
) * (Ureal_1
- Ureal_2
** (-Mantissa
)),
8238 when Attribute_Lock_Free
=> Lock_Free
: declare
8239 V
: constant Entity_Id
:= Boolean_Literals
(Uses_Lock_Free
(P_Type
));
8242 Rewrite
(N
, New_Occurrence_Of
(V
, Loc
));
8244 -- Analyze and resolve as boolean. Note that this attribute is a
8245 -- static attribute in GNAT.
8247 Analyze_And_Resolve
(N
, Standard_Boolean
);
8249 Set_Is_Static_Expression
(N
, True);
8256 when Attribute_Last
=> Last_Attr
:
8260 if Compile_Time_Known_Value
(Hi_Bound
) then
8261 if Is_Real_Type
(P_Type
) then
8262 Fold_Ureal
(N
, Expr_Value_R
(Hi_Bound
), Static
);
8264 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
8268 Check_Concurrent_Discriminant
(Hi_Bound
);
8276 when Attribute_Last_Valid
=> Last_Valid
:
8278 if Has_Predicates
(P_Type
)
8279 and then Has_Static_Predicate
(P_Type
)
8282 LastN
: constant Node_Id
:=
8283 Last
(Static_Discrete_Predicate
(P_Type
));
8285 if Nkind
(LastN
) = N_Range
then
8286 Fold_Uint
(N
, Expr_Value
(High_Bound
(LastN
)), Static
);
8288 Fold_Uint
(N
, Expr_Value
(LastN
), Static
);
8294 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
8302 when Attribute_Leading_Part
=>
8305 Eval_Fat
.Leading_Part
8306 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
8313 when Attribute_Length
=> Length
: declare
8317 -- If any index type is a formal type, or derived from one, the
8318 -- bounds are not static. Treating them as static can produce
8319 -- spurious warnings or improper constant folding.
8321 Ind
:= First_Index
(P_Type
);
8322 while Present
(Ind
) loop
8323 if Is_Generic_Type
(Root_Type
(Etype
(Ind
))) then
8332 -- For two compile time values, we can compute length
8334 if Compile_Time_Known_Value
(Lo_Bound
)
8335 and then Compile_Time_Known_Value
(Hi_Bound
)
8338 UI_Max
(0, 1 + (Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
))),
8342 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8343 -- comparable, and we can figure out the difference between them.
8346 Diff
: aliased Uint
;
8350 Compile_Time_Compare
8351 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
8354 Fold_Uint
(N
, Uint_1
, Static
);
8357 Fold_Uint
(N
, Uint_0
, Static
);
8360 if Diff
/= No_Uint
then
8361 Fold_Uint
(N
, Diff
+ 1, Static
);
8374 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8375 -- of the said attribute at the point of entry into the related loop. As
8376 -- such, the attribute reference does not need to be evaluated because
8377 -- the prefix is the one that is evaluted.
8379 when Attribute_Loop_Entry
=>
8386 when Attribute_Machine
=>
8390 (P_Base_Type
, Expr_Value_R
(E1
), Eval_Fat
.Round
, N
),
8397 when Attribute_Machine_Emax
=>
8398 Fold_Uint
(N
, Machine_Emax_Value
(P_Type
), Static
);
8404 when Attribute_Machine_Emin
=>
8405 Fold_Uint
(N
, Machine_Emin_Value
(P_Type
), Static
);
8407 ----------------------
8408 -- Machine_Mantissa --
8409 ----------------------
8411 when Attribute_Machine_Mantissa
=>
8412 Fold_Uint
(N
, Machine_Mantissa_Value
(P_Type
), Static
);
8414 -----------------------
8415 -- Machine_Overflows --
8416 -----------------------
8418 when Attribute_Machine_Overflows
=>
8420 -- Always true for fixed-point
8422 if Is_Fixed_Point_Type
(P_Type
) then
8423 Fold_Uint
(N
, True_Value
, Static
);
8425 -- Floating point case
8429 UI_From_Int
(Boolean'Pos (Machine_Overflows_On_Target
)),
8437 when Attribute_Machine_Radix
=>
8438 if Is_Fixed_Point_Type
(P_Type
) then
8439 if Is_Decimal_Fixed_Point_Type
(P_Type
)
8440 and then Machine_Radix_10
(P_Type
)
8442 Fold_Uint
(N
, Uint_10
, Static
);
8444 Fold_Uint
(N
, Uint_2
, Static
);
8447 -- All floating-point type always have radix 2
8450 Fold_Uint
(N
, Uint_2
, Static
);
8453 ----------------------
8454 -- Machine_Rounding --
8455 ----------------------
8457 -- Note: for the folding case, it is fine to treat Machine_Rounding
8458 -- exactly the same way as Rounding, since this is one of the allowed
8459 -- behaviors, and performance is not an issue here. It might be a bit
8460 -- better to give the same result as it would give at run time, even
8461 -- though the non-determinism is certainly permitted.
8463 when Attribute_Machine_Rounding
=>
8465 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8467 --------------------
8468 -- Machine_Rounds --
8469 --------------------
8471 when Attribute_Machine_Rounds
=>
8473 -- Always False for fixed-point
8475 if Is_Fixed_Point_Type
(P_Type
) then
8476 Fold_Uint
(N
, False_Value
, Static
);
8478 -- Else yield proper floating-point result
8482 (N
, UI_From_Int
(Boolean'Pos (Machine_Rounds_On_Target
)),
8490 -- Note: Machine_Size is identical to Object_Size
8492 when Attribute_Machine_Size
=> Machine_Size
: declare
8493 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
8496 if Known_Esize
(P_TypeA
) then
8497 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
8505 when Attribute_Mantissa
=>
8507 -- Fixed-point mantissa
8509 if Is_Fixed_Point_Type
(P_Type
) then
8511 -- Compile time foldable case
8513 if Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
8515 Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
8517 -- The calculation of the obsolete Ada 83 attribute Mantissa
8518 -- is annoying, because of AI00143, quoted here:
8520 -- !question 84-01-10
8522 -- Consider the model numbers for F:
8524 -- type F is delta 1.0 range -7.0 .. 8.0;
8526 -- The wording requires that F'MANTISSA be the SMALLEST
8527 -- integer number for which each bound of the specified
8528 -- range is either a model number or lies at most small
8529 -- distant from a model number. This means F'MANTISSA
8530 -- is required to be 3 since the range -7.0 .. 7.0 fits
8531 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8532 -- number, namely, 7. Is this analysis correct? Note that
8533 -- this implies the upper bound of the range is not
8534 -- represented as a model number.
8536 -- !response 84-03-17
8538 -- The analysis is correct. The upper and lower bounds for
8539 -- a fixed point type can lie outside the range of model
8550 LBound
:= Expr_Value_R
(Type_Low_Bound
(P_Type
));
8551 UBound
:= Expr_Value_R
(Type_High_Bound
(P_Type
));
8552 Bound
:= UR_Max
(UR_Abs
(LBound
), UR_Abs
(UBound
));
8553 Max_Man
:= UR_Trunc
(Bound
/ Small_Value
(P_Type
));
8555 -- If the Bound is exactly a model number, i.e. a multiple
8556 -- of Small, then we back it off by one to get the integer
8557 -- value that must be representable.
8559 if Small_Value
(P_Type
) * Max_Man
= Bound
then
8560 Max_Man
:= Max_Man
- 1;
8563 -- Now find corresponding size = Mantissa value
8566 while 2 ** Siz
< Max_Man
loop
8570 Fold_Uint
(N
, Siz
, Static
);
8574 -- The case of dynamic bounds cannot be evaluated at compile
8575 -- time. Instead we use a runtime routine (see Exp_Attr).
8580 -- Floating-point Mantissa
8583 Fold_Uint
(N
, Mantissa
, Static
);
8590 when Attribute_Max
=> Max
:
8592 if Is_Real_Type
(P_Type
) then
8594 (N
, UR_Max
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
8596 Fold_Uint
(N
, UI_Max
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
8600 ----------------------------------
8601 -- Max_Alignment_For_Allocation --
8602 ----------------------------------
8604 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8605 -- arrays are allocated with dope, so we need to take into account both
8606 -- the alignment of the array, which comes from the component alignment,
8607 -- and the alignment of the dope. Also, if the alignment is unknown, we
8608 -- use the max (it's OK to be pessimistic).
8610 when Attribute_Max_Alignment_For_Allocation
=>
8612 A
: Uint
:= UI_From_Int
(Ttypes
.Maximum_Alignment
);
8614 if Known_Alignment
(P_Type
) and then
8615 (not Is_Array_Type
(P_Type
) or else Alignment
(P_Type
) > A
)
8617 A
:= Alignment
(P_Type
);
8620 Fold_Uint
(N
, A
, Static
);
8623 ----------------------------------
8624 -- Max_Size_In_Storage_Elements --
8625 ----------------------------------
8627 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8628 -- Storage_Unit boundary. We can fold any cases for which the size
8629 -- is known by the front end.
8631 when Attribute_Max_Size_In_Storage_Elements
=>
8632 if Known_Esize
(P_Type
) then
8634 (Esize
(P_Type
) + System_Storage_Unit
- 1) /
8635 System_Storage_Unit
,
8639 --------------------
8640 -- Mechanism_Code --
8641 --------------------
8643 when Attribute_Mechanism_Code
=>
8647 Mech
: Mechanism_Type
;
8651 Mech
:= Mechanism
(P_Entity
);
8654 Val
:= UI_To_Int
(Expr_Value
(E1
));
8656 Formal
:= First_Formal
(P_Entity
);
8657 for J
in 1 .. Val
- 1 loop
8658 Next_Formal
(Formal
);
8660 Mech
:= Mechanism
(Formal
);
8664 Fold_Uint
(N
, UI_From_Int
(Int
(-Mech
)), Static
);
8672 when Attribute_Min
=> Min
:
8674 if Is_Real_Type
(P_Type
) then
8676 (N
, UR_Min
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
8679 (N
, UI_Min
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
8687 when Attribute_Mod
=>
8689 (N
, UI_Mod
(Expr_Value
(E1
), Modulus
(P_Base_Type
)), Static
);
8695 when Attribute_Model
=>
8697 (N
, Eval_Fat
.Model
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8703 when Attribute_Model_Emin
=>
8704 Fold_Uint
(N
, Model_Emin_Value
(P_Base_Type
), Static
);
8710 when Attribute_Model_Epsilon
=>
8711 Fold_Ureal
(N
, Model_Epsilon_Value
(P_Base_Type
), Static
);
8713 --------------------
8714 -- Model_Mantissa --
8715 --------------------
8717 when Attribute_Model_Mantissa
=>
8718 Fold_Uint
(N
, Model_Mantissa_Value
(P_Base_Type
), Static
);
8724 when Attribute_Model_Small
=>
8725 Fold_Ureal
(N
, Model_Small_Value
(P_Base_Type
), Static
);
8731 when Attribute_Modulus
=>
8732 Fold_Uint
(N
, Modulus
(P_Type
), Static
);
8734 --------------------
8735 -- Null_Parameter --
8736 --------------------
8738 -- Cannot fold, we know the value sort of, but the whole point is
8739 -- that there is no way to talk about this imaginary value except
8740 -- by using the attribute, so we leave it the way it is.
8742 when Attribute_Null_Parameter
=>
8749 -- The Object_Size attribute for a type returns the Esize of the
8750 -- type and can be folded if this value is known.
8752 when Attribute_Object_Size
=> Object_Size
: declare
8753 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
8756 if Known_Esize
(P_TypeA
) then
8757 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
8761 ----------------------
8762 -- Overlaps_Storage --
8763 ----------------------
8765 when Attribute_Overlaps_Storage
=>
8768 -------------------------
8769 -- Passed_By_Reference --
8770 -------------------------
8772 -- Scalar types are never passed by reference
8774 when Attribute_Passed_By_Reference
=>
8775 Fold_Uint
(N
, False_Value
, Static
);
8781 when Attribute_Pos
=>
8782 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
8788 when Attribute_Pred
=> Pred
:
8790 -- Floating-point case
8792 if Is_Floating_Point_Type
(P_Type
) then
8794 (N
, Eval_Fat
.Pred
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8798 elsif Is_Fixed_Point_Type
(P_Type
) then
8800 (N
, Expr_Value_R
(E1
) - Small_Value
(P_Type
), True);
8802 -- Modular integer case (wraps)
8804 elsif Is_Modular_Integer_Type
(P_Type
) then
8805 Fold_Uint
(N
, (Expr_Value
(E1
) - 1) mod Modulus
(P_Type
), Static
);
8807 -- Other scalar cases
8810 pragma Assert
(Is_Scalar_Type
(P_Type
));
8812 if Is_Enumeration_Type
(P_Type
)
8813 and then Expr_Value
(E1
) =
8814 Expr_Value
(Type_Low_Bound
(P_Base_Type
))
8816 Apply_Compile_Time_Constraint_Error
8817 (N
, "Pred of `&''First`",
8818 CE_Overflow_Check_Failed
,
8820 Warn
=> not Static
);
8826 Fold_Uint
(N
, Expr_Value
(E1
) - 1, Static
);
8834 -- No processing required, because by this stage, Range has been
8835 -- replaced by First .. Last, so this branch can never be taken.
8837 when Attribute_Range
=>
8838 raise Program_Error
;
8844 when Attribute_Range_Length
=>
8847 -- Can fold if both bounds are compile time known
8849 if Compile_Time_Known_Value
(Hi_Bound
)
8850 and then Compile_Time_Known_Value
(Lo_Bound
)
8854 (0, Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
) + 1),
8858 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8859 -- comparable, and we can figure out the difference between them.
8862 Diff
: aliased Uint
;
8866 Compile_Time_Compare
8867 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
8870 Fold_Uint
(N
, Uint_1
, Static
);
8873 Fold_Uint
(N
, Uint_0
, Static
);
8876 if Diff
/= No_Uint
then
8877 Fold_Uint
(N
, Diff
+ 1, Static
);
8889 when Attribute_Ref
=>
8890 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
8896 when Attribute_Remainder
=> Remainder
: declare
8897 X
: constant Ureal
:= Expr_Value_R
(E1
);
8898 Y
: constant Ureal
:= Expr_Value_R
(E2
);
8901 if UR_Is_Zero
(Y
) then
8902 Apply_Compile_Time_Constraint_Error
8903 (N
, "division by zero in Remainder",
8904 CE_Overflow_Check_Failed
,
8905 Warn
=> not Static
);
8911 Fold_Ureal
(N
, Eval_Fat
.Remainder
(P_Base_Type
, X
, Y
), Static
);
8918 when Attribute_Restriction_Set
=> Restriction_Set
: declare
8920 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
8921 Set_Is_Static_Expression
(N
);
8922 end Restriction_Set
;
8928 when Attribute_Round
=> Round
:
8934 -- First we get the (exact result) in units of small
8936 Sr
:= Expr_Value_R
(E1
) / Small_Value
(C_Type
);
8938 -- Now round that exactly to an integer
8940 Si
:= UR_To_Uint
(Sr
);
8942 -- Finally the result is obtained by converting back to real
8944 Fold_Ureal
(N
, Si
* Small_Value
(C_Type
), Static
);
8951 when Attribute_Rounding
=>
8953 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8959 when Attribute_Safe_Emax
=>
8960 Fold_Uint
(N
, Safe_Emax_Value
(P_Type
), Static
);
8966 when Attribute_Safe_First
=>
8967 Fold_Ureal
(N
, Safe_First_Value
(P_Type
), Static
);
8973 when Attribute_Safe_Large
=>
8974 if Is_Fixed_Point_Type
(P_Type
) then
8976 (N
, Expr_Value_R
(Type_High_Bound
(P_Base_Type
)), Static
);
8978 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
8985 when Attribute_Safe_Last
=>
8986 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
8992 when Attribute_Safe_Small
=>
8994 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
8995 -- for fixed-point, since is the same as Small, but we implement
8996 -- it for backwards compatibility.
8998 if Is_Fixed_Point_Type
(P_Type
) then
8999 Fold_Ureal
(N
, Small_Value
(P_Type
), Static
);
9001 -- Ada 83 Safe_Small for floating-point cases
9004 Fold_Ureal
(N
, Model_Small_Value
(P_Type
), Static
);
9011 when Attribute_Scale
=>
9012 Fold_Uint
(N
, Scale_Value
(P_Type
), Static
);
9018 when Attribute_Scaling
=>
9022 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
9029 when Attribute_Signed_Zeros
=>
9031 (N
, UI_From_Int
(Boolean'Pos (Has_Signed_Zeros
(P_Type
))), Static
);
9037 -- Size attribute returns the RM size. All scalar types can be folded,
9038 -- as well as any types for which the size is known by the front end,
9039 -- including any type for which a size attribute is specified. This is
9040 -- one of the places where it is annoying that a size of zero means two
9041 -- things (zero size for scalars, unspecified size for non-scalars).
9043 when Attribute_Size | Attribute_VADS_Size
=> Size
: declare
9044 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9047 if Is_Scalar_Type
(P_TypeA
) or else RM_Size
(P_TypeA
) /= Uint_0
then
9051 if Id
= Attribute_VADS_Size
or else Use_VADS_Size
then
9053 S
: constant Node_Id
:= Size_Clause
(P_TypeA
);
9056 -- If a size clause applies, then use the size from it.
9057 -- This is one of the rare cases where we can use the
9058 -- Size_Clause field for a subtype when Has_Size_Clause
9059 -- is False. Consider:
9061 -- type x is range 1 .. 64;
9062 -- for x'size use 12;
9063 -- subtype y is x range 0 .. 3;
9065 -- Here y has a size clause inherited from x, but normally
9066 -- it does not apply, and y'size is 2. However, y'VADS_Size
9067 -- is indeed 12 and not 2.
9070 and then Is_OK_Static_Expression
(Expression
(S
))
9072 Fold_Uint
(N
, Expr_Value
(Expression
(S
)), Static
);
9074 -- If no size is specified, then we simply use the object
9075 -- size in the VADS_Size case (e.g. Natural'Size is equal
9076 -- to Integer'Size, not one less).
9079 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9083 -- Normal case (Size) in which case we want the RM_Size
9086 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
9095 when Attribute_Small
=>
9097 -- The floating-point case is present only for Ada 83 compatibility.
9098 -- Note that strictly this is an illegal addition, since we are
9099 -- extending an Ada 95 defined attribute, but we anticipate an
9100 -- ARG ruling that will permit this.
9102 if Is_Floating_Point_Type
(P_Type
) then
9104 -- Ada 83 attribute is defined as (RM83 3.5.8)
9106 -- T'Small = 2.0**(-T'Emax - 1)
9110 -- T'Emax = 4 * T'Mantissa
9112 Fold_Ureal
(N
, Ureal_2
** ((-(4 * Mantissa
)) - 1), Static
);
9114 -- Normal Ada 95 fixed-point case
9117 Fold_Ureal
(N
, Small_Value
(P_Type
), True);
9124 when Attribute_Stream_Size
=>
9131 when Attribute_Succ
=> Succ
:
9133 -- Floating-point case
9135 if Is_Floating_Point_Type
(P_Type
) then
9137 (N
, Eval_Fat
.Succ
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9141 elsif Is_Fixed_Point_Type
(P_Type
) then
9142 Fold_Ureal
(N
, Expr_Value_R
(E1
) + Small_Value
(P_Type
), Static
);
9144 -- Modular integer case (wraps)
9146 elsif Is_Modular_Integer_Type
(P_Type
) then
9147 Fold_Uint
(N
, (Expr_Value
(E1
) + 1) mod Modulus
(P_Type
), Static
);
9149 -- Other scalar cases
9152 pragma Assert
(Is_Scalar_Type
(P_Type
));
9154 if Is_Enumeration_Type
(P_Type
)
9155 and then Expr_Value
(E1
) =
9156 Expr_Value
(Type_High_Bound
(P_Base_Type
))
9158 Apply_Compile_Time_Constraint_Error
9159 (N
, "Succ of `&''Last`",
9160 CE_Overflow_Check_Failed
,
9162 Warn
=> not Static
);
9167 Fold_Uint
(N
, Expr_Value
(E1
) + 1, Static
);
9176 when Attribute_Truncation
=>
9179 Eval_Fat
.Truncation
(P_Base_Type
, Expr_Value_R
(E1
)),
9186 when Attribute_Type_Class
=> Type_Class
: declare
9187 Typ
: constant Entity_Id
:= Underlying_Type
(P_Base_Type
);
9191 if Is_Descendent_Of_Address
(Typ
) then
9192 Id
:= RE_Type_Class_Address
;
9194 elsif Is_Enumeration_Type
(Typ
) then
9195 Id
:= RE_Type_Class_Enumeration
;
9197 elsif Is_Integer_Type
(Typ
) then
9198 Id
:= RE_Type_Class_Integer
;
9200 elsif Is_Fixed_Point_Type
(Typ
) then
9201 Id
:= RE_Type_Class_Fixed_Point
;
9203 elsif Is_Floating_Point_Type
(Typ
) then
9204 Id
:= RE_Type_Class_Floating_Point
;
9206 elsif Is_Array_Type
(Typ
) then
9207 Id
:= RE_Type_Class_Array
;
9209 elsif Is_Record_Type
(Typ
) then
9210 Id
:= RE_Type_Class_Record
;
9212 elsif Is_Access_Type
(Typ
) then
9213 Id
:= RE_Type_Class_Access
;
9215 elsif Is_Enumeration_Type
(Typ
) then
9216 Id
:= RE_Type_Class_Enumeration
;
9218 elsif Is_Task_Type
(Typ
) then
9219 Id
:= RE_Type_Class_Task
;
9221 -- We treat protected types like task types. It would make more
9222 -- sense to have another enumeration value, but after all the
9223 -- whole point of this feature is to be exactly DEC compatible,
9224 -- and changing the type Type_Class would not meet this requirement.
9226 elsif Is_Protected_Type
(Typ
) then
9227 Id
:= RE_Type_Class_Task
;
9229 -- Not clear if there are any other possibilities, but if there
9230 -- are, then we will treat them as the address case.
9233 Id
:= RE_Type_Class_Address
;
9236 Rewrite
(N
, New_Occurrence_Of
(RTE
(Id
), Loc
));
9239 -----------------------
9240 -- Unbiased_Rounding --
9241 -----------------------
9243 when Attribute_Unbiased_Rounding
=>
9246 Eval_Fat
.Unbiased_Rounding
(P_Base_Type
, Expr_Value_R
(E1
)),
9249 -------------------------
9250 -- Unconstrained_Array --
9251 -------------------------
9253 when Attribute_Unconstrained_Array
=> Unconstrained_Array
: declare
9254 Typ
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9257 Rewrite
(N
, New_Occurrence_Of
(
9259 Is_Array_Type
(P_Type
)
9260 and then not Is_Constrained
(Typ
)), Loc
));
9262 -- Analyze and resolve as boolean, note that this attribute is
9263 -- a static attribute in GNAT.
9265 Analyze_And_Resolve
(N
, Standard_Boolean
);
9267 Set_Is_Static_Expression
(N
, True);
9268 end Unconstrained_Array
;
9270 -- Attribute Update is never static
9272 when Attribute_Update
=>
9279 -- Processing is shared with Size
9285 when Attribute_Val
=> Val
:
9287 if Expr_Value
(E1
) < Expr_Value
(Type_Low_Bound
(P_Base_Type
))
9289 Expr_Value
(E1
) > Expr_Value
(Type_High_Bound
(P_Base_Type
))
9291 Apply_Compile_Time_Constraint_Error
9292 (N
, "Val expression out of range",
9293 CE_Range_Check_Failed
,
9294 Warn
=> not Static
);
9300 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
9308 -- The Value_Size attribute for a type returns the RM size of the type.
9309 -- This an always be folded for scalar types, and can also be folded for
9310 -- non-scalar types if the size is set. This is one of the places where
9311 -- it is annoying that a size of zero means two things!
9313 when Attribute_Value_Size
=> Value_Size
: declare
9314 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9316 if Is_Scalar_Type
(P_TypeA
) or else RM_Size
(P_TypeA
) /= Uint_0
then
9317 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
9325 -- Version can never be static
9327 when Attribute_Version
=>
9334 -- Wide_Image is a scalar attribute, but is never static, because it
9335 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9337 when Attribute_Wide_Image
=>
9340 ---------------------
9341 -- Wide_Wide_Image --
9342 ---------------------
9344 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9345 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9347 when Attribute_Wide_Wide_Image
=>
9350 ---------------------
9351 -- Wide_Wide_Width --
9352 ---------------------
9354 -- Processing for Wide_Wide_Width is combined with Width
9360 -- Processing for Wide_Width is combined with Width
9366 -- This processing also handles the case of Wide_[Wide_]Width
9368 when Attribute_Width |
9369 Attribute_Wide_Width |
9370 Attribute_Wide_Wide_Width
=> Width
:
9372 if Compile_Time_Known_Bounds
(P_Type
) then
9374 -- Floating-point types
9376 if Is_Floating_Point_Type
(P_Type
) then
9378 -- Width is zero for a null range (RM 3.5 (38))
9380 if Expr_Value_R
(Type_High_Bound
(P_Type
)) <
9381 Expr_Value_R
(Type_Low_Bound
(P_Type
))
9383 Fold_Uint
(N
, Uint_0
, Static
);
9386 -- For floating-point, we have +N.dddE+nnn where length
9387 -- of ddd is determined by type'Digits - 1, but is one
9388 -- if Digits is one (RM 3.5 (33)).
9390 -- nnn is set to 2 for Short_Float and Float (32 bit
9391 -- floats), and 3 for Long_Float and Long_Long_Float.
9392 -- For machines where Long_Long_Float is the IEEE
9393 -- extended precision type, the exponent takes 4 digits.
9397 Int
'Max (2, UI_To_Int
(Digits_Value
(P_Type
)));
9400 if Esize
(P_Type
) <= 32 then
9402 elsif Esize
(P_Type
) = 64 then
9408 Fold_Uint
(N
, UI_From_Int
(Len
), Static
);
9412 -- Fixed-point types
9414 elsif Is_Fixed_Point_Type
(P_Type
) then
9416 -- Width is zero for a null range (RM 3.5 (38))
9418 if Expr_Value
(Type_High_Bound
(P_Type
)) <
9419 Expr_Value
(Type_Low_Bound
(P_Type
))
9421 Fold_Uint
(N
, Uint_0
, Static
);
9423 -- The non-null case depends on the specific real type
9426 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9429 (N
, UI_From_Int
(Fore_Value
+ 1) + Aft_Value
(P_Type
),
9437 R
: constant Entity_Id
:= Root_Type
(P_Type
);
9438 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
9439 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
9452 -- Width for types derived from Standard.Character
9453 -- and Standard.Wide_[Wide_]Character.
9455 elsif Is_Standard_Character_Type
(P_Type
) then
9458 -- Set W larger if needed
9460 for J
in UI_To_Int
(Lo
) .. UI_To_Int
(Hi
) loop
9462 -- All wide characters look like Hex_hhhhhhhh
9466 -- No need to compute this more than once
9471 C
:= Character'Val (J
);
9473 -- Test for all cases where Character'Image
9474 -- yields an image that is longer than three
9475 -- characters. First the cases of Reserved_xxx
9476 -- names (length = 12).
9479 when Reserved_128 | Reserved_129 |
9480 Reserved_132 | Reserved_153
9483 when BS | HT | LF | VT | FF | CR |
9484 SO | SI | EM | FS | GS | RS |
9485 US | RI | MW | ST | PM
9488 when NUL | SOH | STX | ETX | EOT |
9489 ENQ | ACK | BEL | DLE | DC1 |
9490 DC2 | DC3 | DC4 | NAK | SYN |
9491 ETB | CAN | SUB | ESC | DEL |
9492 BPH | NBH | NEL | SSA | ESA |
9493 HTS | HTJ | VTS | PLD | PLU |
9494 SS2 | SS3 | DCS | PU1 | PU2 |
9495 STS | CCH | SPA | EPA | SOS |
9496 SCI | CSI | OSC | APC
9499 when Space
.. Tilde |
9500 No_Break_Space
.. LC_Y_Diaeresis
9502 -- Special case of soft hyphen in Ada 2005
9504 if C
= Character'Val (16#AD#
)
9505 and then Ada_Version
>= Ada_2005
9513 W
:= Int
'Max (W
, Wt
);
9517 -- Width for types derived from Standard.Boolean
9519 elsif R
= Standard_Boolean
then
9526 -- Width for integer types
9528 elsif Is_Integer_Type
(P_Type
) then
9529 T
:= UI_Max
(abs Lo
, abs Hi
);
9537 -- User declared enum type with discard names
9539 elsif Discard_Names
(R
) then
9541 -- If range is null, result is zero, that has already
9542 -- been dealt with, so what we need is the power of ten
9543 -- that accomodates the Pos of the largest value, which
9544 -- is the high bound of the range + one for the space.
9553 -- Only remaining possibility is user declared enum type
9554 -- with normal case of Discard_Names not active.
9557 pragma Assert
(Is_Enumeration_Type
(P_Type
));
9560 L
:= First_Literal
(P_Type
);
9561 while Present
(L
) loop
9563 -- Only pay attention to in range characters
9565 if Lo
<= Enumeration_Pos
(L
)
9566 and then Enumeration_Pos
(L
) <= Hi
9568 -- For Width case, use decoded name
9570 if Id
= Attribute_Width
then
9571 Get_Decoded_Name_String
(Chars
(L
));
9572 Wt
:= Nat
(Name_Len
);
9574 -- For Wide_[Wide_]Width, use encoded name, and
9575 -- then adjust for the encoding.
9578 Get_Name_String
(Chars
(L
));
9580 -- Character literals are always of length 3
9582 if Name_Buffer
(1) = 'Q' then
9585 -- Otherwise loop to adjust for upper/wide chars
9588 Wt
:= Nat
(Name_Len
);
9590 for J
in 1 .. Name_Len
loop
9591 if Name_Buffer
(J
) = 'U' then
9593 elsif Name_Buffer
(J
) = 'W' then
9600 W
:= Int
'Max (W
, Wt
);
9607 Fold_Uint
(N
, UI_From_Int
(W
), Static
);
9613 -- The following attributes denote functions that cannot be folded
9615 when Attribute_From_Any |
9617 Attribute_TypeCode
=>
9620 -- The following attributes can never be folded, and furthermore we
9621 -- should not even have entered the case statement for any of these.
9622 -- Note that in some cases, the values have already been folded as
9623 -- a result of the processing in Analyze_Attribute or earlier in
9626 when Attribute_Abort_Signal |
9629 Attribute_Address_Size |
9630 Attribute_Asm_Input |
9631 Attribute_Asm_Output |
9633 Attribute_Bit_Order |
9634 Attribute_Bit_Position |
9635 Attribute_Callable |
9638 Attribute_Code_Address |
9639 Attribute_Compiler_Version |
9641 Attribute_Default_Bit_Order |
9642 Attribute_Default_Scalar_Storage_Order |
9643 Attribute_Elaborated |
9644 Attribute_Elab_Body |
9645 Attribute_Elab_Spec |
9646 Attribute_Elab_Subp_Body |
9648 Attribute_External_Tag |
9649 Attribute_Fast_Math |
9650 Attribute_First_Bit |
9653 Attribute_Last_Bit |
9654 Attribute_Library_Level |
9655 Attribute_Maximum_Alignment |
9658 Attribute_Partition_ID |
9659 Attribute_Pool_Address |
9660 Attribute_Position |
9661 Attribute_Priority |
9664 Attribute_Scalar_Storage_Order |
9665 Attribute_Simple_Storage_Pool |
9666 Attribute_Storage_Pool |
9667 Attribute_Storage_Size |
9668 Attribute_Storage_Unit |
9669 Attribute_Stub_Type |
9670 Attribute_System_Allocator_Alignment |
9672 Attribute_Target_Name |
9673 Attribute_Terminated |
9674 Attribute_To_Address |
9675 Attribute_Type_Key |
9676 Attribute_UET_Address |
9677 Attribute_Unchecked_Access |
9678 Attribute_Universal_Literal_String |
9679 Attribute_Unrestricted_Access |
9681 Attribute_Valid_Scalars |
9683 Attribute_Wchar_T_Size |
9684 Attribute_Wide_Value |
9685 Attribute_Wide_Wide_Value |
9686 Attribute_Word_Size |
9689 raise Program_Error
;
9692 -- At the end of the case, one more check. If we did a static evaluation
9693 -- so that the result is now a literal, then set Is_Static_Expression
9694 -- in the constant only if the prefix type is a static subtype. For
9695 -- non-static subtypes, the folding is still OK, but not static.
9697 -- An exception is the GNAT attribute Constrained_Array which is
9698 -- defined to be a static attribute in all cases.
9700 if Nkind_In
(N
, N_Integer_Literal
,
9702 N_Character_Literal
,
9704 or else (Is_Entity_Name
(N
)
9705 and then Ekind
(Entity
(N
)) = E_Enumeration_Literal
)
9707 Set_Is_Static_Expression
(N
, Static
);
9709 -- If this is still an attribute reference, then it has not been folded
9710 -- and that means that its expressions are in a non-static context.
9712 elsif Nkind
(N
) = N_Attribute_Reference
then
9715 -- Note: the else case not covered here are odd cases where the
9716 -- processing has transformed the attribute into something other
9717 -- than a constant. Nothing more to do in such cases.
9724 ------------------------------
9725 -- Is_Anonymous_Tagged_Base --
9726 ------------------------------
9728 function Is_Anonymous_Tagged_Base
9730 Typ
: Entity_Id
) return Boolean
9734 Anon
= Current_Scope
9735 and then Is_Itype
(Anon
)
9736 and then Associated_Node_For_Itype
(Anon
) = Parent
(Typ
);
9737 end Is_Anonymous_Tagged_Base
;
9739 --------------------------------
9740 -- Name_Implies_Lvalue_Prefix --
9741 --------------------------------
9743 function Name_Implies_Lvalue_Prefix
(Nam
: Name_Id
) return Boolean is
9744 pragma Assert
(Is_Attribute_Name
(Nam
));
9746 return Attribute_Name_Implies_Lvalue_Prefix
(Get_Attribute_Id
(Nam
));
9747 end Name_Implies_Lvalue_Prefix
;
9749 -----------------------
9750 -- Resolve_Attribute --
9751 -----------------------
9753 procedure Resolve_Attribute
(N
: Node_Id
; Typ
: Entity_Id
) is
9754 Loc
: constant Source_Ptr
:= Sloc
(N
);
9755 P
: constant Node_Id
:= Prefix
(N
);
9756 Aname
: constant Name_Id
:= Attribute_Name
(N
);
9757 Attr_Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
9758 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
9759 Des_Btyp
: Entity_Id
;
9760 Index
: Interp_Index
;
9762 Nom_Subt
: Entity_Id
;
9764 procedure Accessibility_Message
;
9765 -- Error, or warning within an instance, if the static accessibility
9766 -- rules of 3.10.2 are violated.
9768 function Declared_Within_Generic_Unit
9769 (Entity
: Entity_Id
;
9770 Generic_Unit
: Node_Id
) return Boolean;
9771 -- Returns True if Declared_Entity is declared within the declarative
9772 -- region of Generic_Unit; otherwise returns False.
9774 ---------------------------
9775 -- Accessibility_Message --
9776 ---------------------------
9778 procedure Accessibility_Message
is
9779 Indic
: Node_Id
:= Parent
(Parent
(N
));
9782 -- In an instance, this is a runtime check, but one we
9783 -- know will fail, so generate an appropriate warning.
9785 if In_Instance_Body
then
9786 Error_Msg_Warn
:= SPARK_Mode
/= On
;
9788 ("non-local pointer cannot point to local object<<", P
);
9789 Error_Msg_F
("\Program_Error [<<", P
);
9791 Make_Raise_Program_Error
(Loc
,
9792 Reason
=> PE_Accessibility_Check_Failed
));
9797 Error_Msg_F
("non-local pointer cannot point to local object", P
);
9799 -- Check for case where we have a missing access definition
9801 if Is_Record_Type
(Current_Scope
)
9803 Nkind_In
(Parent
(N
), N_Discriminant_Association
,
9804 N_Index_Or_Discriminant_Constraint
)
9806 Indic
:= Parent
(Parent
(N
));
9807 while Present
(Indic
)
9808 and then Nkind
(Indic
) /= N_Subtype_Indication
9810 Indic
:= Parent
(Indic
);
9813 if Present
(Indic
) then
9815 ("\use an access definition for" &
9816 " the access discriminant of&",
9817 N
, Entity
(Subtype_Mark
(Indic
)));
9821 end Accessibility_Message
;
9823 ----------------------------------
9824 -- Declared_Within_Generic_Unit --
9825 ----------------------------------
9827 function Declared_Within_Generic_Unit
9828 (Entity
: Entity_Id
;
9829 Generic_Unit
: Node_Id
) return Boolean
9831 Generic_Encloser
: Node_Id
:= Enclosing_Generic_Unit
(Entity
);
9834 while Present
(Generic_Encloser
) loop
9835 if Generic_Encloser
= Generic_Unit
then
9839 -- We have to step to the scope of the generic's entity, because
9840 -- otherwise we'll just get back the same generic.
9843 Enclosing_Generic_Unit
9844 (Scope
(Defining_Entity
(Generic_Encloser
)));
9848 end Declared_Within_Generic_Unit
;
9850 -- Start of processing for Resolve_Attribute
9853 -- If error during analysis, no point in continuing, except for array
9854 -- types, where we get better recovery by using unconstrained indexes
9855 -- than nothing at all (see Check_Array_Type).
9858 and then Attr_Id
/= Attribute_First
9859 and then Attr_Id
/= Attribute_Last
9860 and then Attr_Id
/= Attribute_Length
9861 and then Attr_Id
/= Attribute_Range
9866 -- If attribute was universal type, reset to actual type
9868 if Etype
(N
) = Universal_Integer
9869 or else Etype
(N
) = Universal_Real
9874 -- Remaining processing depends on attribute
9882 -- For access attributes, if the prefix denotes an entity, it is
9883 -- interpreted as a name, never as a call. It may be overloaded,
9884 -- in which case resolution uses the profile of the context type.
9885 -- Otherwise prefix must be resolved.
9887 when Attribute_Access
9888 | Attribute_Unchecked_Access
9889 | Attribute_Unrestricted_Access
=>
9893 -- Note possible modification if we have a variable
9895 if Is_Variable
(P
) then
9897 PN
: constant Node_Id
:= Parent
(N
);
9900 Note
: Boolean := True;
9901 -- Skip this for the case of Unrestricted_Access occuring in
9902 -- the context of a Valid check, since this otherwise leads
9903 -- to a missed warning (the Valid check does not really
9904 -- modify!) If this case, Note will be reset to False.
9907 if Attr_Id
= Attribute_Unrestricted_Access
9908 and then Nkind
(PN
) = N_Function_Call
9912 if Nkind
(Nm
) = N_Expanded_Name
9913 and then Chars
(Nm
) = Name_Valid
9914 and then Nkind
(Prefix
(Nm
)) = N_Identifier
9915 and then Chars
(Prefix
(Nm
)) = Name_Attr_Long_Float
9922 Note_Possible_Modification
(P
, Sure
=> False);
9927 -- The following comes from a query concerning improper use of
9928 -- universal_access in equality tests involving anonymous access
9929 -- types. Another good reason for 'Ref, but for now disable the
9930 -- test, which breaks several filed tests???
9932 if Ekind
(Typ
) = E_Anonymous_Access_Type
9933 and then Nkind_In
(Parent
(N
), N_Op_Eq
, N_Op_Ne
)
9936 Error_Msg_N
("need unique type to resolve 'Access", N
);
9937 Error_Msg_N
("\qualify attribute with some access type", N
);
9940 -- Case where prefix is an entity name
9942 if Is_Entity_Name
(P
) then
9944 -- Deal with case where prefix itself is overloaded
9946 if Is_Overloaded
(P
) then
9947 Get_First_Interp
(P
, Index
, It
);
9948 while Present
(It
.Nam
) loop
9949 if Type_Conformant
(Designated_Type
(Typ
), It
.Nam
) then
9950 Set_Entity
(P
, It
.Nam
);
9952 -- The prefix is definitely NOT overloaded anymore at
9953 -- this point, so we reset the Is_Overloaded flag to
9954 -- avoid any confusion when reanalyzing the node.
9956 Set_Is_Overloaded
(P
, False);
9957 Set_Is_Overloaded
(N
, False);
9958 Generate_Reference
(Entity
(P
), P
);
9962 Get_Next_Interp
(Index
, It
);
9965 -- If Prefix is a subprogram name, this reference freezes:
9967 -- If it is a type, there is nothing to resolve.
9968 -- If it is an object, complete its resolution.
9970 elsif Is_Overloadable
(Entity
(P
)) then
9972 -- Avoid insertion of freeze actions in spec expression mode
9974 if not In_Spec_Expression
then
9975 Freeze_Before
(N
, Entity
(P
));
9978 -- Nothing to do if prefix is a type name
9980 elsif Is_Type
(Entity
(P
)) then
9983 -- Otherwise non-overloaded other case, resolve the prefix
9989 -- Some further error checks
9991 Error_Msg_Name_1
:= Aname
;
9993 if not Is_Entity_Name
(P
) then
9996 elsif Is_Overloadable
(Entity
(P
))
9997 and then Is_Abstract_Subprogram
(Entity
(P
))
9999 Error_Msg_F
("prefix of % attribute cannot be abstract", P
);
10000 Set_Etype
(N
, Any_Type
);
10002 elsif Ekind
(Entity
(P
)) = E_Enumeration_Literal
then
10004 ("prefix of % attribute cannot be enumeration literal", P
);
10005 Set_Etype
(N
, Any_Type
);
10007 -- An attempt to take 'Access of a function that renames an
10008 -- enumeration literal. Issue a specialized error message.
10010 elsif Ekind
(Entity
(P
)) = E_Function
10011 and then Present
(Alias
(Entity
(P
)))
10012 and then Ekind
(Alias
(Entity
(P
))) = E_Enumeration_Literal
10015 ("prefix of % attribute cannot be function renaming "
10016 & "an enumeration literal", P
);
10017 Set_Etype
(N
, Any_Type
);
10019 elsif Convention
(Entity
(P
)) = Convention_Intrinsic
then
10020 Error_Msg_F
("prefix of % attribute cannot be intrinsic", P
);
10021 Set_Etype
(N
, Any_Type
);
10024 -- Assignments, return statements, components of aggregates,
10025 -- generic instantiations will require convention checks if
10026 -- the type is an access to subprogram. Given that there will
10027 -- also be accessibility checks on those, this is where the
10028 -- checks can eventually be centralized ???
10030 if Ekind_In
(Btyp
, E_Access_Subprogram_Type
,
10031 E_Anonymous_Access_Subprogram_Type
,
10032 E_Access_Protected_Subprogram_Type
,
10033 E_Anonymous_Access_Protected_Subprogram_Type
)
10035 -- Deal with convention mismatch
10037 if Convention
(Designated_Type
(Btyp
)) /=
10038 Convention
(Entity
(P
))
10041 ("subprogram & has wrong convention", P
, Entity
(P
));
10042 Error_Msg_Sloc
:= Sloc
(Btyp
);
10043 Error_Msg_FE
("\does not match & declared#", P
, Btyp
);
10045 if not Is_Itype
(Btyp
)
10046 and then not Has_Convention_Pragma
(Btyp
)
10049 ("\probable missing pragma Convention for &",
10054 Check_Subtype_Conformant
10055 (New_Id
=> Entity
(P
),
10056 Old_Id
=> Designated_Type
(Btyp
),
10060 if Attr_Id
= Attribute_Unchecked_Access
then
10061 Error_Msg_Name_1
:= Aname
;
10063 ("attribute% cannot be applied to a subprogram", P
);
10065 elsif Aname
= Name_Unrestricted_Access
then
10066 null; -- Nothing to check
10068 -- Check the static accessibility rule of 3.10.2(32).
10069 -- This rule also applies within the private part of an
10070 -- instantiation. This rule does not apply to anonymous
10071 -- access-to-subprogram types in access parameters.
10073 elsif Attr_Id
= Attribute_Access
10074 and then not In_Instance_Body
10076 (Ekind
(Btyp
) = E_Access_Subprogram_Type
10077 or else Is_Local_Anonymous_Access
(Btyp
))
10078 and then Subprogram_Access_Level
(Entity
(P
)) >
10079 Type_Access_Level
(Btyp
)
10082 ("subprogram must not be deeper than access type", P
);
10084 -- Check the restriction of 3.10.2(32) that disallows the
10085 -- access attribute within a generic body when the ultimate
10086 -- ancestor of the type of the attribute is declared outside
10087 -- of the generic unit and the subprogram is declared within
10088 -- that generic unit. This includes any such attribute that
10089 -- occurs within the body of a generic unit that is a child
10090 -- of the generic unit where the subprogram is declared.
10092 -- The rule also prohibits applying the attribute when the
10093 -- access type is a generic formal access type (since the
10094 -- level of the actual type is not known). This restriction
10095 -- does not apply when the attribute type is an anonymous
10096 -- access-to-subprogram type. Note that this check was
10097 -- revised by AI-229, because the original Ada 95 rule
10098 -- was too lax. The original rule only applied when the
10099 -- subprogram was declared within the body of the generic,
10100 -- which allowed the possibility of dangling references).
10101 -- The rule was also too strict in some cases, in that it
10102 -- didn't permit the access to be declared in the generic
10103 -- spec, whereas the revised rule does (as long as it's not
10106 -- There are a couple of subtleties of the test for applying
10107 -- the check that are worth noting. First, we only apply it
10108 -- when the levels of the subprogram and access type are the
10109 -- same (the case where the subprogram is statically deeper
10110 -- was applied above, and the case where the type is deeper
10111 -- is always safe). Second, we want the check to apply
10112 -- within nested generic bodies and generic child unit
10113 -- bodies, but not to apply to an attribute that appears in
10114 -- the generic unit's specification. This is done by testing
10115 -- that the attribute's innermost enclosing generic body is
10116 -- not the same as the innermost generic body enclosing the
10117 -- generic unit where the subprogram is declared (we don't
10118 -- want the check to apply when the access attribute is in
10119 -- the spec and there's some other generic body enclosing
10120 -- generic). Finally, there's no point applying the check
10121 -- when within an instance, because any violations will have
10122 -- been caught by the compilation of the generic unit.
10124 -- We relax this check in Relaxed_RM_Semantics mode for
10125 -- compatibility with legacy code for use by Ada source
10126 -- code analyzers (e.g. CodePeer).
10128 elsif Attr_Id
= Attribute_Access
10129 and then not Relaxed_RM_Semantics
10130 and then not In_Instance
10131 and then Present
(Enclosing_Generic_Unit
(Entity
(P
)))
10132 and then Present
(Enclosing_Generic_Body
(N
))
10133 and then Enclosing_Generic_Body
(N
) /=
10134 Enclosing_Generic_Body
10135 (Enclosing_Generic_Unit
(Entity
(P
)))
10136 and then Subprogram_Access_Level
(Entity
(P
)) =
10137 Type_Access_Level
(Btyp
)
10138 and then Ekind
(Btyp
) /=
10139 E_Anonymous_Access_Subprogram_Type
10140 and then Ekind
(Btyp
) /=
10141 E_Anonymous_Access_Protected_Subprogram_Type
10143 -- The attribute type's ultimate ancestor must be
10144 -- declared within the same generic unit as the
10145 -- subprogram is declared (including within another
10146 -- nested generic unit). The error message is
10147 -- specialized to say "ancestor" for the case where the
10148 -- access type is not its own ancestor, since saying
10149 -- simply "access type" would be very confusing.
10151 if not Declared_Within_Generic_Unit
10153 Enclosing_Generic_Unit
(Entity
(P
)))
10156 ("''Access attribute not allowed in generic body",
10159 if Root_Type
(Btyp
) = Btyp
then
10162 "access type & is declared outside " &
10163 "generic unit (RM 3.10.2(32))", N
, Btyp
);
10166 ("\because ancestor of " &
10167 "access type & is declared outside " &
10168 "generic unit (RM 3.10.2(32))", N
, Btyp
);
10172 ("\move ''Access to private part, or " &
10173 "(Ada 2005) use anonymous access type instead of &",
10176 -- If the ultimate ancestor of the attribute's type is
10177 -- a formal type, then the attribute is illegal because
10178 -- the actual type might be declared at a higher level.
10179 -- The error message is specialized to say "ancestor"
10180 -- for the case where the access type is not its own
10181 -- ancestor, since saying simply "access type" would be
10184 elsif Is_Generic_Type
(Root_Type
(Btyp
)) then
10185 if Root_Type
(Btyp
) = Btyp
then
10187 ("access type must not be a generic formal type",
10191 ("ancestor access type must not be a generic " &
10198 -- If this is a renaming, an inherited operation, or a
10199 -- subprogram instance, use the original entity. This may make
10200 -- the node type-inconsistent, so this transformation can only
10201 -- be done if the node will not be reanalyzed. In particular,
10202 -- if it is within a default expression, the transformation
10203 -- must be delayed until the default subprogram is created for
10204 -- it, when the enclosing subprogram is frozen.
10206 if Is_Entity_Name
(P
)
10207 and then Is_Overloadable
(Entity
(P
))
10208 and then Present
(Alias
(Entity
(P
)))
10209 and then Expander_Active
10212 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
10215 elsif Nkind
(P
) = N_Selected_Component
10216 and then Is_Overloadable
(Entity
(Selector_Name
(P
)))
10218 -- Protected operation. If operation is overloaded, must
10219 -- disambiguate. Prefix that denotes protected object itself
10220 -- is resolved with its own type.
10222 if Attr_Id
= Attribute_Unchecked_Access
then
10223 Error_Msg_Name_1
:= Aname
;
10225 ("attribute% cannot be applied to protected operation", P
);
10228 Resolve
(Prefix
(P
));
10229 Generate_Reference
(Entity
(Selector_Name
(P
)), P
);
10231 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10232 -- statically illegal if F is an anonymous access to subprogram.
10234 elsif Nkind
(P
) = N_Explicit_Dereference
10235 and then Is_Entity_Name
(Prefix
(P
))
10236 and then Ekind
(Etype
(Entity
(Prefix
(P
)))) =
10237 E_Anonymous_Access_Subprogram_Type
10239 Error_Msg_N
("anonymous access to subprogram "
10240 & "has deeper accessibility than any master", P
);
10242 elsif Is_Overloaded
(P
) then
10244 -- Use the designated type of the context to disambiguate
10245 -- Note that this was not strictly conformant to Ada 95,
10246 -- but was the implementation adopted by most Ada 95 compilers.
10247 -- The use of the context type to resolve an Access attribute
10248 -- reference is now mandated in AI-235 for Ada 2005.
10251 Index
: Interp_Index
;
10255 Get_First_Interp
(P
, Index
, It
);
10256 while Present
(It
.Typ
) loop
10257 if Covers
(Designated_Type
(Typ
), It
.Typ
) then
10258 Resolve
(P
, It
.Typ
);
10262 Get_Next_Interp
(Index
, It
);
10269 -- X'Access is illegal if X denotes a constant and the access type
10270 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10271 -- does not apply to 'Unrestricted_Access. If the reference is a
10272 -- default-initialized aggregate component for a self-referential
10273 -- type the reference is legal.
10275 if not (Ekind
(Btyp
) = E_Access_Subprogram_Type
10276 or else Ekind
(Btyp
) = E_Anonymous_Access_Subprogram_Type
10277 or else (Is_Record_Type
(Btyp
)
10279 Present
(Corresponding_Remote_Type
(Btyp
)))
10280 or else Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
10281 or else Ekind
(Btyp
)
10282 = E_Anonymous_Access_Protected_Subprogram_Type
10283 or else Is_Access_Constant
(Btyp
)
10284 or else Is_Variable
(P
)
10285 or else Attr_Id
= Attribute_Unrestricted_Access
)
10287 if Is_Entity_Name
(P
)
10288 and then Is_Type
(Entity
(P
))
10290 -- Legality of a self-reference through an access
10291 -- attribute has been verified in Analyze_Access_Attribute.
10295 elsif Comes_From_Source
(N
) then
10296 Error_Msg_F
("access-to-variable designates constant", P
);
10300 Des_Btyp
:= Designated_Type
(Btyp
);
10302 if Ada_Version
>= Ada_2005
10303 and then Is_Incomplete_Type
(Des_Btyp
)
10305 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10306 -- imported entity, and the non-limited view is visible, make
10307 -- use of it. If it is an incomplete subtype, use the base type
10310 if From_Limited_With
(Des_Btyp
)
10311 and then Present
(Non_Limited_View
(Des_Btyp
))
10313 Des_Btyp
:= Non_Limited_View
(Des_Btyp
);
10315 elsif Ekind
(Des_Btyp
) = E_Incomplete_Subtype
then
10316 Des_Btyp
:= Etype
(Des_Btyp
);
10320 if (Attr_Id
= Attribute_Access
10322 Attr_Id
= Attribute_Unchecked_Access
)
10323 and then (Ekind
(Btyp
) = E_General_Access_Type
10324 or else Ekind
(Btyp
) = E_Anonymous_Access_Type
)
10326 -- Ada 2005 (AI-230): Check the accessibility of anonymous
10327 -- access types for stand-alone objects, record and array
10328 -- components, and return objects. For a component definition
10329 -- the level is the same of the enclosing composite type.
10331 if Ada_Version
>= Ada_2005
10332 and then (Is_Local_Anonymous_Access
(Btyp
)
10334 -- Handle cases where Btyp is the anonymous access
10335 -- type of an Ada 2012 stand-alone object.
10337 or else Nkind
(Associated_Node_For_Itype
(Btyp
)) =
10338 N_Object_Declaration
)
10340 Object_Access_Level
(P
) > Deepest_Type_Access_Level
(Btyp
)
10341 and then Attr_Id
= Attribute_Access
10343 -- In an instance, this is a runtime check, but one we know
10344 -- will fail, so generate an appropriate warning. As usual,
10345 -- this kind of warning is an error in SPARK mode.
10347 if In_Instance_Body
then
10348 Error_Msg_Warn
:= SPARK_Mode
/= On
;
10350 ("non-local pointer cannot point to local object<<", P
);
10351 Error_Msg_F
("\Program_Error [<<", P
);
10354 Make_Raise_Program_Error
(Loc
,
10355 Reason
=> PE_Accessibility_Check_Failed
));
10356 Set_Etype
(N
, Typ
);
10360 ("non-local pointer cannot point to local object", P
);
10364 if Is_Dependent_Component_Of_Mutable_Object
(P
) then
10366 ("illegal attribute for discriminant-dependent component",
10370 -- Check static matching rule of 3.10.2(27). Nominal subtype
10371 -- of the prefix must statically match the designated type.
10373 Nom_Subt
:= Etype
(P
);
10375 if Is_Constr_Subt_For_U_Nominal
(Nom_Subt
) then
10376 Nom_Subt
:= Base_Type
(Nom_Subt
);
10379 if Is_Tagged_Type
(Designated_Type
(Typ
)) then
10381 -- If the attribute is in the context of an access
10382 -- parameter, then the prefix is allowed to be of
10383 -- the class-wide type (by AI-127).
10385 if Ekind
(Typ
) = E_Anonymous_Access_Type
then
10386 if not Covers
(Designated_Type
(Typ
), Nom_Subt
)
10387 and then not Covers
(Nom_Subt
, Designated_Type
(Typ
))
10393 Desig
:= Designated_Type
(Typ
);
10395 if Is_Class_Wide_Type
(Desig
) then
10396 Desig
:= Etype
(Desig
);
10399 if Is_Anonymous_Tagged_Base
(Nom_Subt
, Desig
) then
10404 ("type of prefix: & not compatible",
10407 ("\with &, the expected designated type",
10408 P
, Designated_Type
(Typ
));
10413 elsif not Covers
(Designated_Type
(Typ
), Nom_Subt
)
10415 (not Is_Class_Wide_Type
(Designated_Type
(Typ
))
10416 and then Is_Class_Wide_Type
(Nom_Subt
))
10419 ("type of prefix: & is not covered", P
, Nom_Subt
);
10421 ("\by &, the expected designated type" &
10422 " (RM 3.10.2 (27))", P
, Designated_Type
(Typ
));
10425 if Is_Class_Wide_Type
(Designated_Type
(Typ
))
10426 and then Has_Discriminants
(Etype
(Designated_Type
(Typ
)))
10427 and then Is_Constrained
(Etype
(Designated_Type
(Typ
)))
10428 and then Designated_Type
(Typ
) /= Nom_Subt
10430 Apply_Discriminant_Check
10431 (N
, Etype
(Designated_Type
(Typ
)));
10434 -- Ada 2005 (AI-363): Require static matching when designated
10435 -- type has discriminants and a constrained partial view, since
10436 -- in general objects of such types are mutable, so we can't
10437 -- allow the access value to designate a constrained object
10438 -- (because access values must be assumed to designate mutable
10439 -- objects when designated type does not impose a constraint).
10441 elsif Subtypes_Statically_Match
(Des_Btyp
, Nom_Subt
) then
10444 elsif Has_Discriminants
(Designated_Type
(Typ
))
10445 and then not Is_Constrained
(Des_Btyp
)
10447 (Ada_Version
< Ada_2005
10449 not Object_Type_Has_Constrained_Partial_View
10450 (Typ
=> Designated_Type
(Base_Type
(Typ
)),
10451 Scop
=> Current_Scope
))
10457 ("object subtype must statically match "
10458 & "designated subtype", P
);
10460 if Is_Entity_Name
(P
)
10461 and then Is_Array_Type
(Designated_Type
(Typ
))
10464 D
: constant Node_Id
:= Declaration_Node
(Entity
(P
));
10467 ("aliased object has explicit bounds??", D
);
10469 ("\declare without bounds (and with explicit "
10470 & "initialization)??", D
);
10472 ("\for use with unconstrained access??", D
);
10477 -- Check the static accessibility rule of 3.10.2(28). Note that
10478 -- this check is not performed for the case of an anonymous
10479 -- access type, since the access attribute is always legal
10480 -- in such a context.
10482 if Attr_Id
/= Attribute_Unchecked_Access
10483 and then Ekind
(Btyp
) = E_General_Access_Type
10485 Object_Access_Level
(P
) > Deepest_Type_Access_Level
(Btyp
)
10487 Accessibility_Message
;
10492 if Ekind_In
(Btyp
, E_Access_Protected_Subprogram_Type
,
10493 E_Anonymous_Access_Protected_Subprogram_Type
)
10495 if Is_Entity_Name
(P
)
10496 and then not Is_Protected_Type
(Scope
(Entity
(P
)))
10498 Error_Msg_F
("context requires a protected subprogram", P
);
10500 -- Check accessibility of protected object against that of the
10501 -- access type, but only on user code, because the expander
10502 -- creates access references for handlers. If the context is an
10503 -- anonymous_access_to_protected, there are no accessibility
10504 -- checks either. Omit check entirely for Unrestricted_Access.
10506 elsif Object_Access_Level
(P
) > Deepest_Type_Access_Level
(Btyp
)
10507 and then Comes_From_Source
(N
)
10508 and then Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
10509 and then Attr_Id
/= Attribute_Unrestricted_Access
10511 Accessibility_Message
;
10514 -- AI05-0225: If the context is not an access to protected
10515 -- function, the prefix must be a variable, given that it may
10516 -- be used subsequently in a protected call.
10518 elsif Nkind
(P
) = N_Selected_Component
10519 and then not Is_Variable
(Prefix
(P
))
10520 and then Ekind
(Entity
(Selector_Name
(P
))) /= E_Function
10523 ("target object of access to protected procedure "
10524 & "must be variable", N
);
10526 elsif Is_Entity_Name
(P
) then
10527 Check_Internal_Protected_Use
(N
, Entity
(P
));
10530 elsif Ekind_In
(Btyp
, E_Access_Subprogram_Type
,
10531 E_Anonymous_Access_Subprogram_Type
)
10532 and then Ekind
(Etype
(N
)) = E_Access_Protected_Subprogram_Type
10534 Error_Msg_F
("context requires a non-protected subprogram", P
);
10537 -- The context cannot be a pool-specific type, but this is a
10538 -- legality rule, not a resolution rule, so it must be checked
10539 -- separately, after possibly disambiguation (see AI-245).
10541 if Ekind
(Btyp
) = E_Access_Type
10542 and then Attr_Id
/= Attribute_Unrestricted_Access
10544 Wrong_Type
(N
, Typ
);
10547 -- The context may be a constrained access type (however ill-
10548 -- advised such subtypes might be) so in order to generate a
10549 -- constraint check when needed set the type of the attribute
10550 -- reference to the base type of the context.
10552 Set_Etype
(N
, Btyp
);
10554 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10556 if Attr_Id
/= Attribute_Unrestricted_Access
then
10557 if Is_Atomic_Object
(P
)
10558 and then not Is_Atomic
(Designated_Type
(Typ
))
10561 ("access to atomic object cannot yield access-to-" &
10562 "non-atomic type", P
);
10564 elsif Is_Volatile_Object
(P
)
10565 and then not Is_Volatile
(Designated_Type
(Typ
))
10568 ("access to volatile object cannot yield access-to-" &
10569 "non-volatile type", P
);
10573 -- Check for unrestricted access where expected type is a thin
10574 -- pointer to an unconstrained array.
10576 if Non_Aliased_Prefix
(N
)
10577 and then Has_Size_Clause
(Typ
)
10578 and then RM_Size
(Typ
) = System_Address_Size
10581 DT
: constant Entity_Id
:= Designated_Type
(Typ
);
10583 if Is_Array_Type
(DT
) and then not Is_Constrained
(DT
) then
10585 ("illegal use of Unrestricted_Access attribute", P
);
10587 ("\attempt to generate thin pointer to unaliased "
10593 -- Mark that address of entity is taken
10595 if Is_Entity_Name
(P
) then
10596 Set_Address_Taken
(Entity
(P
));
10599 -- Deal with possible elaboration check
10601 if Is_Entity_Name
(P
) and then Is_Subprogram
(Entity
(P
)) then
10603 Subp_Id
: constant Entity_Id
:= Entity
(P
);
10604 Scop
: constant Entity_Id
:= Scope
(Subp_Id
);
10605 Subp_Decl
: constant Node_Id
:=
10606 Unit_Declaration_Node
(Subp_Id
);
10607 Flag_Id
: Entity_Id
;
10608 Subp_Body
: Node_Id
;
10610 -- If the access has been taken and the body of the subprogram
10611 -- has not been see yet, indirect calls must be protected with
10612 -- elaboration checks. We have the proper elaboration machinery
10613 -- for subprograms declared in packages, but within a block or
10614 -- a subprogram the body will appear in the same declarative
10615 -- part, and we must insert a check in the eventual body itself
10616 -- using the elaboration flag that we generate now. The check
10617 -- is then inserted when the body is expanded. This processing
10618 -- is not needed for a stand alone expression function because
10619 -- the internally generated spec and body are always inserted
10620 -- as a pair in the same declarative list.
10624 and then Comes_From_Source
(Subp_Id
)
10625 and then Comes_From_Source
(N
)
10626 and then In_Open_Scopes
(Scop
)
10627 and then Ekind_In
(Scop
, E_Block
, E_Procedure
, E_Function
)
10628 and then not Has_Completion
(Subp_Id
)
10629 and then No
(Elaboration_Entity
(Subp_Id
))
10630 and then Nkind
(Subp_Decl
) = N_Subprogram_Declaration
10631 and then Nkind
(Original_Node
(Subp_Decl
)) /=
10632 N_Expression_Function
10634 -- Create elaboration variable for it
10636 Flag_Id
:= Make_Temporary
(Loc
, 'E');
10637 Set_Elaboration_Entity
(Subp_Id
, Flag_Id
);
10638 Set_Is_Frozen
(Flag_Id
);
10640 -- Insert declaration for flag after subprogram
10641 -- declaration. Note that attribute reference may
10642 -- appear within a nested scope.
10644 Insert_After_And_Analyze
(Subp_Decl
,
10645 Make_Object_Declaration
(Loc
,
10646 Defining_Identifier
=> Flag_Id
,
10647 Object_Definition
=>
10648 New_Occurrence_Of
(Standard_Short_Integer
, Loc
),
10650 Make_Integer_Literal
(Loc
, Uint_0
)));
10653 -- Taking the 'Access of an expression function freezes its
10654 -- expression (RM 13.14 10.3/3). This does not apply to an
10655 -- expression function that acts as a completion because the
10656 -- generated body is immediately analyzed and the expression
10657 -- is automatically frozen.
10659 if Is_Expression_Function
(Subp_Id
)
10660 and then Present
(Corresponding_Body
(Subp_Decl
))
10663 Unit_Declaration_Node
(Corresponding_Body
(Subp_Decl
));
10665 -- Analyze the body of the expression function to freeze
10666 -- the expression. This takes care of the case where the
10667 -- 'Access is part of dispatch table initialization and
10668 -- the generated body of the expression function has not
10669 -- been analyzed yet.
10671 if not Analyzed
(Subp_Body
) then
10672 Analyze
(Subp_Body
);
10677 end Access_Attribute
;
10683 -- Deal with resolving the type for Address attribute, overloading
10684 -- is not permitted here, since there is no context to resolve it.
10686 when Attribute_Address | Attribute_Code_Address
=>
10687 Address_Attribute
: begin
10689 -- To be safe, assume that if the address of a variable is taken,
10690 -- it may be modified via this address, so note modification.
10692 if Is_Variable
(P
) then
10693 Note_Possible_Modification
(P
, Sure
=> False);
10696 if Nkind
(P
) in N_Subexpr
10697 and then Is_Overloaded
(P
)
10699 Get_First_Interp
(P
, Index
, It
);
10700 Get_Next_Interp
(Index
, It
);
10702 if Present
(It
.Nam
) then
10703 Error_Msg_Name_1
:= Aname
;
10705 ("prefix of % attribute cannot be overloaded", P
);
10709 if not Is_Entity_Name
(P
)
10710 or else not Is_Overloadable
(Entity
(P
))
10712 if not Is_Task_Type
(Etype
(P
))
10713 or else Nkind
(P
) = N_Explicit_Dereference
10719 -- If this is the name of a derived subprogram, or that of a
10720 -- generic actual, the address is that of the original entity.
10722 if Is_Entity_Name
(P
)
10723 and then Is_Overloadable
(Entity
(P
))
10724 and then Present
(Alias
(Entity
(P
)))
10727 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
10730 if Is_Entity_Name
(P
) then
10731 Set_Address_Taken
(Entity
(P
));
10734 if Nkind
(P
) = N_Slice
then
10736 -- Arr (X .. Y)'address is identical to Arr (X)'address,
10737 -- even if the array is packed and the slice itself is not
10738 -- addressable. Transform the prefix into an indexed component.
10740 -- Note that the transformation is safe only if we know that
10741 -- the slice is non-null. That is because a null slice can have
10742 -- an out of bounds index value.
10744 -- Right now, gigi blows up if given 'Address on a slice as a
10745 -- result of some incorrect freeze nodes generated by the front
10746 -- end, and this covers up that bug in one case, but the bug is
10747 -- likely still there in the cases not handled by this code ???
10749 -- It's not clear what 'Address *should* return for a null
10750 -- slice with out of bounds indexes, this might be worth an ARG
10753 -- One approach would be to do a length check unconditionally,
10754 -- and then do the transformation below unconditionally, but
10755 -- analyze with checks off, avoiding the problem of the out of
10756 -- bounds index. This approach would interpret the address of
10757 -- an out of bounds null slice as being the address where the
10758 -- array element would be if there was one, which is probably
10759 -- as reasonable an interpretation as any ???
10762 Loc
: constant Source_Ptr
:= Sloc
(P
);
10763 D
: constant Node_Id
:= Discrete_Range
(P
);
10767 if Is_Entity_Name
(D
)
10770 (Type_Low_Bound
(Entity
(D
)),
10771 Type_High_Bound
(Entity
(D
)))
10774 Make_Attribute_Reference
(Loc
,
10775 Prefix
=> (New_Occurrence_Of
(Entity
(D
), Loc
)),
10776 Attribute_Name
=> Name_First
);
10778 elsif Nkind
(D
) = N_Range
10779 and then Not_Null_Range
(Low_Bound
(D
), High_Bound
(D
))
10781 Lo
:= Low_Bound
(D
);
10787 if Present
(Lo
) then
10789 Make_Indexed_Component
(Loc
,
10790 Prefix
=> Relocate_Node
(Prefix
(P
)),
10791 Expressions
=> New_List
(Lo
)));
10793 Analyze_And_Resolve
(P
);
10797 end Address_Attribute
;
10803 -- Prefix of Body_Version attribute can be a subprogram name which
10804 -- must not be resolved, since this is not a call.
10806 when Attribute_Body_Version
=>
10813 -- Prefix of Caller attribute is an entry name which must not
10814 -- be resolved, since this is definitely not an entry call.
10816 when Attribute_Caller
=>
10823 -- Shares processing with Address attribute
10829 -- If the prefix of the Count attribute is an entry name it must not
10830 -- be resolved, since this is definitely not an entry call. However,
10831 -- if it is an element of an entry family, the index itself may
10832 -- have to be resolved because it can be a general expression.
10834 when Attribute_Count
=>
10835 if Nkind
(P
) = N_Indexed_Component
10836 and then Is_Entity_Name
(Prefix
(P
))
10839 Indx
: constant Node_Id
:= First
(Expressions
(P
));
10840 Fam
: constant Entity_Id
:= Entity
(Prefix
(P
));
10842 Resolve
(Indx
, Entry_Index_Type
(Fam
));
10843 Apply_Range_Check
(Indx
, Entry_Index_Type
(Fam
));
10851 -- Prefix of the Elaborated attribute is a subprogram name which
10852 -- must not be resolved, since this is definitely not a call. Note
10853 -- that it is a library unit, so it cannot be overloaded here.
10855 when Attribute_Elaborated
=>
10862 -- Prefix of Enabled attribute is a check name, which must be treated
10863 -- specially and not touched by Resolve.
10865 when Attribute_Enabled
=>
10872 -- Do not resolve the prefix of Loop_Entry, instead wait until the
10873 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
10874 -- The delay ensures that any generated checks or temporaries are
10875 -- inserted before the relocated prefix.
10877 when Attribute_Loop_Entry
=>
10880 --------------------
10881 -- Mechanism_Code --
10882 --------------------
10884 -- Prefix of the Mechanism_Code attribute is a function name
10885 -- which must not be resolved. Should we check for overloaded ???
10887 when Attribute_Mechanism_Code
=>
10894 -- Most processing is done in sem_dist, after determining the
10895 -- context type. Node is rewritten as a conversion to a runtime call.
10897 when Attribute_Partition_ID
=>
10898 Process_Partition_Id
(N
);
10905 when Attribute_Pool_Address
=>
10912 -- We replace the Range attribute node with a range expression whose
10913 -- bounds are the 'First and 'Last attributes applied to the same
10914 -- prefix. The reason that we do this transformation here instead of
10915 -- in the expander is that it simplifies other parts of the semantic
10916 -- analysis which assume that the Range has been replaced; thus it
10917 -- must be done even when in semantic-only mode (note that the RM
10918 -- specifically mentions this equivalence, we take care that the
10919 -- prefix is only evaluated once).
10921 when Attribute_Range
=> Range_Attribute
:
10928 if not Is_Entity_Name
(P
)
10929 or else not Is_Type
(Entity
(P
))
10934 Dims
:= Expressions
(N
);
10937 Make_Attribute_Reference
(Loc
,
10938 Prefix
=> Duplicate_Subexpr
(P
, Name_Req
=> True),
10939 Attribute_Name
=> Name_Last
,
10940 Expressions
=> Dims
);
10943 Make_Attribute_Reference
(Loc
,
10945 Attribute_Name
=> Name_First
,
10946 Expressions
=> (Dims
));
10948 -- Do not share the dimension indicator, if present. Even
10949 -- though it is a static constant, its source location
10950 -- may be modified when printing expanded code and node
10951 -- sharing will lead to chaos in Sprint.
10953 if Present
(Dims
) then
10954 Set_Expressions
(LB
,
10955 New_List
(New_Copy_Tree
(First
(Dims
))));
10958 -- If the original was marked as Must_Not_Freeze (see code
10959 -- in Sem_Ch3.Make_Index), then make sure the rewriting
10960 -- does not freeze either.
10962 if Must_Not_Freeze
(N
) then
10963 Set_Must_Not_Freeze
(HB
);
10964 Set_Must_Not_Freeze
(LB
);
10965 Set_Must_Not_Freeze
(Prefix
(HB
));
10966 Set_Must_Not_Freeze
(Prefix
(LB
));
10969 if Raises_Constraint_Error
(Prefix
(N
)) then
10971 -- Preserve Sloc of prefix in the new bounds, so that
10972 -- the posted warning can be removed if we are within
10973 -- unreachable code.
10975 Set_Sloc
(LB
, Sloc
(Prefix
(N
)));
10976 Set_Sloc
(HB
, Sloc
(Prefix
(N
)));
10979 Rewrite
(N
, Make_Range
(Loc
, LB
, HB
));
10980 Analyze_And_Resolve
(N
, Typ
);
10982 -- Ensure that the expanded range does not have side effects
10984 Force_Evaluation
(LB
);
10985 Force_Evaluation
(HB
);
10987 -- Normally after resolving attribute nodes, Eval_Attribute
10988 -- is called to do any possible static evaluation of the node.
10989 -- However, here since the Range attribute has just been
10990 -- transformed into a range expression it is no longer an
10991 -- attribute node and therefore the call needs to be avoided
10992 -- and is accomplished by simply returning from the procedure.
10995 end Range_Attribute
;
11001 -- We will only come here during the prescan of a spec expression
11002 -- containing a Result attribute. In that case the proper Etype has
11003 -- already been set, and nothing more needs to be done here.
11005 when Attribute_Result
=>
11012 -- Prefix must not be resolved in this case, since it is not a
11013 -- real entity reference. No action of any kind is require.
11015 when Attribute_UET_Address
=>
11018 ----------------------
11019 -- Unchecked_Access --
11020 ----------------------
11022 -- Processing is shared with Access
11024 -------------------------
11025 -- Unrestricted_Access --
11026 -------------------------
11028 -- Processing is shared with Access
11034 -- Resolve aggregate components in component associations
11036 when Attribute_Update
=>
11038 Aggr
: constant Node_Id
:= First
(Expressions
(N
));
11039 Typ
: constant Entity_Id
:= Etype
(Prefix
(N
));
11045 -- Set the Etype of the aggregate to that of the prefix, even
11046 -- though the aggregate may not be a proper representation of a
11047 -- value of the type (missing or duplicated associations, etc.)
11048 -- Complete resolution of the prefix. Note that in Ada 2012 it
11049 -- can be a qualified expression that is e.g. an aggregate.
11051 Set_Etype
(Aggr
, Typ
);
11052 Resolve
(Prefix
(N
), Typ
);
11054 -- For an array type, resolve expressions with the component
11055 -- type of the array, and apply constraint checks when needed.
11057 if Is_Array_Type
(Typ
) then
11058 Assoc
:= First
(Component_Associations
(Aggr
));
11059 while Present
(Assoc
) loop
11060 Expr
:= Expression
(Assoc
);
11061 Resolve
(Expr
, Component_Type
(Typ
));
11063 -- For scalar array components set Do_Range_Check when
11064 -- needed. Constraint checking on non-scalar components
11065 -- is done in Aggregate_Constraint_Checks, but only if
11066 -- full analysis is enabled. These flags are not set in
11067 -- the front-end in GnatProve mode.
11069 if Is_Scalar_Type
(Component_Type
(Typ
))
11070 and then not Is_OK_Static_Expression
(Expr
)
11072 if Is_Entity_Name
(Expr
)
11073 and then Etype
(Expr
) = Component_Type
(Typ
)
11078 Set_Do_Range_Check
(Expr
);
11082 -- The choices in the association are static constants,
11083 -- or static aggregates each of whose components belongs
11084 -- to the proper index type. However, they must also
11085 -- belong to the index subtype (s) of the prefix, which
11086 -- may be a subtype (e.g. given by a slice).
11088 -- Choices may also be identifiers with no staticness
11089 -- requirements, in which case they must resolve to the
11098 C
:= First
(Choices
(Assoc
));
11099 while Present
(C
) loop
11100 Indx
:= First_Index
(Etype
(Prefix
(N
)));
11102 if Nkind
(C
) /= N_Aggregate
then
11103 Analyze_And_Resolve
(C
, Etype
(Indx
));
11104 Apply_Constraint_Check
(C
, Etype
(Indx
));
11105 Check_Non_Static_Context
(C
);
11108 C_E
:= First
(Expressions
(C
));
11109 while Present
(C_E
) loop
11110 Analyze_And_Resolve
(C_E
, Etype
(Indx
));
11111 Apply_Constraint_Check
(C_E
, Etype
(Indx
));
11112 Check_Non_Static_Context
(C_E
);
11126 -- For a record type, use type of each component, which is
11127 -- recorded during analysis.
11130 Assoc
:= First
(Component_Associations
(Aggr
));
11131 while Present
(Assoc
) loop
11132 Comp
:= First
(Choices
(Assoc
));
11133 Expr
:= Expression
(Assoc
);
11135 if Nkind
(Comp
) /= N_Others_Choice
11136 and then not Error_Posted
(Comp
)
11138 Resolve
(Expr
, Etype
(Entity
(Comp
)));
11140 if Is_Scalar_Type
(Etype
(Entity
(Comp
)))
11141 and then not Is_OK_Static_Expression
(Expr
)
11143 Set_Do_Range_Check
(Expr
);
11156 -- Apply range check. Note that we did not do this during the
11157 -- analysis phase, since we wanted Eval_Attribute to have a
11158 -- chance at finding an illegal out of range value.
11160 when Attribute_Val
=>
11162 -- Note that we do our own Eval_Attribute call here rather than
11163 -- use the common one, because we need to do processing after
11164 -- the call, as per above comment.
11166 Eval_Attribute
(N
);
11168 -- Eval_Attribute may replace the node with a raise CE, or
11169 -- fold it to a constant. Obviously we only apply a scalar
11170 -- range check if this did not happen.
11172 if Nkind
(N
) = N_Attribute_Reference
11173 and then Attribute_Name
(N
) = Name_Val
11175 Apply_Scalar_Range_Check
(First
(Expressions
(N
)), Btyp
);
11184 -- Prefix of Version attribute can be a subprogram name which
11185 -- must not be resolved, since this is not a call.
11187 when Attribute_Version
=>
11190 ----------------------
11191 -- Other Attributes --
11192 ----------------------
11194 -- For other attributes, resolve prefix unless it is a type. If
11195 -- the attribute reference itself is a type name ('Base and 'Class)
11196 -- then this is only legal within a task or protected record.
11199 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
11203 -- If the attribute reference itself is a type name ('Base,
11204 -- 'Class) then this is only legal within a task or protected
11205 -- record. What is this all about ???
11207 if Is_Entity_Name
(N
) and then Is_Type
(Entity
(N
)) then
11208 if Is_Concurrent_Type
(Entity
(N
))
11209 and then In_Open_Scopes
(Entity
(P
))
11214 ("invalid use of subtype name in expression or call", N
);
11218 -- For attributes whose argument may be a string, complete
11219 -- resolution of argument now. This avoids premature expansion
11220 -- (and the creation of transient scopes) before the attribute
11221 -- reference is resolved.
11224 when Attribute_Value
=>
11225 Resolve
(First
(Expressions
(N
)), Standard_String
);
11227 when Attribute_Wide_Value
=>
11228 Resolve
(First
(Expressions
(N
)), Standard_Wide_String
);
11230 when Attribute_Wide_Wide_Value
=>
11231 Resolve
(First
(Expressions
(N
)), Standard_Wide_Wide_String
);
11233 when others => null;
11236 -- If the prefix of the attribute is a class-wide type then it
11237 -- will be expanded into a dispatching call to a predefined
11238 -- primitive. Therefore we must check for potential violation
11239 -- of such restriction.
11241 if Is_Class_Wide_Type
(Etype
(P
)) then
11242 Check_Restriction
(No_Dispatching_Calls
, N
);
11246 -- Normally the Freezing is done by Resolve but sometimes the Prefix
11247 -- is not resolved, in which case the freezing must be done now.
11249 -- For an elaboration check on a subprogram, we do not freeze its type.
11250 -- It may be declared in an unrelated scope, in particular in the case
11251 -- of a generic function whose type may remain unelaborated.
11253 if Attr_Id
= Attribute_Elaborated
then
11257 Freeze_Expression
(P
);
11260 -- Finally perform static evaluation on the attribute reference
11262 Analyze_Dimension
(N
);
11263 Eval_Attribute
(N
);
11264 end Resolve_Attribute
;
11266 ------------------------
11267 -- Set_Boolean_Result --
11268 ------------------------
11270 procedure Set_Boolean_Result
(N
: Node_Id
; B
: Boolean) is
11271 Loc
: constant Source_Ptr
:= Sloc
(N
);
11274 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
11276 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
11278 end Set_Boolean_Result
;
11280 --------------------------------
11281 -- Stream_Attribute_Available --
11282 --------------------------------
11284 function Stream_Attribute_Available
11286 Nam
: TSS_Name_Type
;
11287 Partial_View
: Node_Id
:= Empty
) return Boolean
11289 Etyp
: Entity_Id
:= Typ
;
11291 -- Start of processing for Stream_Attribute_Available
11294 -- We need some comments in this body ???
11296 if Has_Stream_Attribute_Definition
(Typ
, Nam
) then
11300 if Is_Class_Wide_Type
(Typ
) then
11301 return not Is_Limited_Type
(Typ
)
11302 or else Stream_Attribute_Available
(Etype
(Typ
), Nam
);
11305 if Nam
= TSS_Stream_Input
11306 and then Is_Abstract_Type
(Typ
)
11307 and then not Is_Class_Wide_Type
(Typ
)
11312 if not (Is_Limited_Type
(Typ
)
11313 or else (Present
(Partial_View
)
11314 and then Is_Limited_Type
(Partial_View
)))
11319 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11321 if Nam
= TSS_Stream_Input
11322 and then Ada_Version
>= Ada_2005
11323 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Read
)
11327 elsif Nam
= TSS_Stream_Output
11328 and then Ada_Version
>= Ada_2005
11329 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Write
)
11334 -- Case of Read and Write: check for attribute definition clause that
11335 -- applies to an ancestor type.
11337 while Etype
(Etyp
) /= Etyp
loop
11338 Etyp
:= Etype
(Etyp
);
11340 if Has_Stream_Attribute_Definition
(Etyp
, Nam
) then
11345 if Ada_Version
< Ada_2005
then
11347 -- In Ada 95 mode, also consider a non-visible definition
11350 Btyp
: constant Entity_Id
:= Implementation_Base_Type
(Typ
);
11353 and then Stream_Attribute_Available
11354 (Btyp
, Nam
, Partial_View
=> Typ
);
11359 end Stream_Attribute_Available
;