]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_attr.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / sem_attr.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ A T T R --
6-- --
7-- B o d y --
8-- --
6dc87f5f 9-- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
996ae0b0
RK
10-- --
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
b5c84c3c
RD
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. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
23-- --
24------------------------------------------------------------------------------
25
26with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
27
28with Atree; use Atree;
442ade9d 29with Casing; use Casing;
996ae0b0 30with Checks; use Checks;
1f163ef7 31with Debug; use Debug;
996ae0b0 32with Einfo; use Einfo;
150ac76e 33with Elists; use Elists;
996ae0b0
RK
34with Errout; use Errout;
35with Eval_Fat;
468c6c8a 36with Exp_Dist; use Exp_Dist;
996ae0b0
RK
37with Exp_Util; use Exp_Util;
38with Expander; use Expander;
39with Freeze; use Freeze;
e0bf7d65 40with Gnatvsn; use Gnatvsn;
e10dab7f 41with Itypes; use Itypes;
fbf5a39b 42with Lib; use Lib;
996ae0b0 43with Lib.Xref; use Lib.Xref;
996ae0b0
RK
44with Nlists; use Nlists;
45with Nmake; use Nmake;
46with Opt; use Opt;
47with Restrict; use Restrict;
6e937c1c 48with Rident; use Rident;
996ae0b0 49with Rtsfind; use Rtsfind;
fbf5a39b 50with Sdefault; use Sdefault;
996ae0b0 51with Sem; use Sem;
a4100e55 52with Sem_Aux; use Sem_Aux;
996ae0b0
RK
53with Sem_Cat; use Sem_Cat;
54with Sem_Ch6; use Sem_Ch6;
55with Sem_Ch8; use Sem_Ch8;
c0985d4e 56with Sem_Ch10; use Sem_Ch10;
dec6faf1 57with Sem_Dim; use Sem_Dim;
996ae0b0 58with Sem_Dist; use Sem_Dist;
1f163ef7 59with Sem_Elab; use Sem_Elab;
16212e89 60with Sem_Elim; use Sem_Elim;
996ae0b0 61with Sem_Eval; use Sem_Eval;
c9d70ab1 62with Sem_Prag; use Sem_Prag;
996ae0b0
RK
63with Sem_Res; use Sem_Res;
64with Sem_Type; use Sem_Type;
65with Sem_Util; use Sem_Util;
71140fc6 66with Sem_Warn;
996ae0b0
RK
67with Stand; use Stand;
68with Sinfo; use Sinfo;
69with Sinput; use Sinput;
7ed57189 70with System;
996ae0b0 71with Stringt; use Stringt;
fea9e956
ES
72with Style;
73with Stylesw; use Stylesw;
996ae0b0
RK
74with Targparm; use Targparm;
75with Ttypes; use Ttypes;
996ae0b0
RK
76with Tbuild; use Tbuild;
77with Uintp; use Uintp;
2cbac6c6 78with Uname; use Uname;
996ae0b0 79with Urealp; use Urealp;
996ae0b0
RK
80
81package body Sem_Attr is
82
83 True_Value : constant Uint := Uint_1;
84 False_Value : constant Uint := Uint_0;
85 -- Synonyms to be used when these constants are used as Boolean values
86
87 Bad_Attribute : exception;
88 -- Exception raised if an error is detected during attribute processing,
89 -- used so that we can abandon the processing so we don't run into
90 -- trouble with cascaded errors.
91
b8e6830b
AC
92 -- The following array is the list of attributes defined in the Ada 83 RM.
93 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
94 -- modes all these attributes are recognized, even if removed in Ada 95.
996ae0b0 95
fbf5a39b 96 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
155562cb
AC
97 Attribute_Address |
98 Attribute_Aft |
99 Attribute_Alignment |
100 Attribute_Base |
101 Attribute_Callable |
102 Attribute_Constrained |
103 Attribute_Count |
104 Attribute_Delta |
105 Attribute_Digits |
106 Attribute_Emax |
107 Attribute_Epsilon |
108 Attribute_First |
109 Attribute_First_Bit |
110 Attribute_Fore |
111 Attribute_Image |
112 Attribute_Large |
113 Attribute_Last |
114 Attribute_Last_Bit |
115 Attribute_Leading_Part |
116 Attribute_Length |
117 Attribute_Machine_Emax |
118 Attribute_Machine_Emin |
119 Attribute_Machine_Mantissa |
120 Attribute_Machine_Overflows |
121 Attribute_Machine_Radix |
122 Attribute_Machine_Rounds |
123 Attribute_Mantissa |
124 Attribute_Pos |
125 Attribute_Position |
126 Attribute_Pred |
127 Attribute_Range |
128 Attribute_Safe_Emax |
129 Attribute_Safe_Large |
130 Attribute_Safe_Small |
131 Attribute_Size |
132 Attribute_Small |
133 Attribute_Storage_Size |
134 Attribute_Succ |
135 Attribute_Terminated |
136 Attribute_Val |
137 Attribute_Value |
138 Attribute_Width => True,
139 others => False);
996ae0b0 140
822033eb
HK
141 -- The following array is the list of attributes defined in the Ada 2005
142 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
143 -- but in Ada 95 they are considered to be implementation defined.
144
145 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
155562cb
AC
146 Attribute_Machine_Rounding |
147 Attribute_Mod |
148 Attribute_Priority |
149 Attribute_Stream_Size |
150 Attribute_Wide_Wide_Width => True,
151 others => False);
152
153 -- The following array is the list of attributes defined in the Ada 2012
154 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
155 -- and Ada 2005 modes, but are considered to be implementation defined.
156
157 Attribute_12 : constant Attribute_Class_Array := Attribute_Class_Array'(
158 Attribute_First_Valid |
159 Attribute_Has_Same_Storage |
160 Attribute_Last_Valid |
161 Attribute_Max_Alignment_For_Allocation => True,
162 others => False);
822033eb 163
442ade9d
RD
164 -- The following array contains all attributes that imply a modification
165 -- of their prefixes or result in an access value. Such prefixes can be
166 -- considered as lvalues.
822033eb 167
442ade9d 168 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
822033eb 169 Attribute_Class_Array'(
155562cb
AC
170 Attribute_Access |
171 Attribute_Address |
172 Attribute_Input |
173 Attribute_Read |
174 Attribute_Unchecked_Access |
175 Attribute_Unrestricted_Access => True,
176 others => False);
822033eb 177
996ae0b0
RK
178 -----------------------
179 -- Local_Subprograms --
180 -----------------------
181
182 procedure Eval_Attribute (N : Node_Id);
183 -- Performs compile time evaluation of attributes where possible, leaving
184 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
185 -- set, and replacing the node with a literal node if the value can be
186 -- computed at compile time. All static attribute references are folded,
187 -- as well as a number of cases of non-static attributes that can always
188 -- be computed at compile time (e.g. floating-point model attributes that
189 -- are applied to non-static subtypes). Of course in such cases, the
190 -- Is_Static_Expression flag will not be set on the resulting literal.
191 -- Note that the only required action of this procedure is to catch the
192 -- static expression cases as described in the RM. Folding of other cases
193 -- is done where convenient, but some additional non-static folding is in
97b9064f 194 -- Expand_N_Attribute_Reference in cases where this is more convenient.
996ae0b0
RK
195
196 function Is_Anonymous_Tagged_Base
197 (Anon : Entity_Id;
155562cb 198 Typ : Entity_Id) return Boolean;
996ae0b0
RK
199 -- For derived tagged types that constrain parent discriminants we build
200 -- an anonymous unconstrained base type. We need to recognize the relation
201 -- between the two when analyzing an access attribute for a constrained
202 -- component, before the full declaration for Typ has been analyzed, and
203 -- where therefore the prefix of the attribute does not match the enclosing
204 -- scope.
205
f7ea2603
RD
206 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
207 -- Rewrites node N with an occurrence of either Standard_False or
208 -- Standard_True, depending on the value of the parameter B. The
209 -- result is marked as a static expression.
210
996ae0b0
RK
211 -----------------------
212 -- Analyze_Attribute --
213 -----------------------
214
215 procedure Analyze_Attribute (N : Node_Id) is
216 Loc : constant Source_Ptr := Sloc (N);
217 Aname : constant Name_Id := Attribute_Name (N);
218 P : constant Node_Id := Prefix (N);
219 Exprs : constant List_Id := Expressions (N);
220 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
221 E1 : Node_Id;
222 E2 : Node_Id;
223
224 P_Type : Entity_Id;
225 -- Type of prefix after analysis
226
227 P_Base_Type : Entity_Id;
228 -- Base type of prefix after analysis
229
996ae0b0
RK
230 -----------------------
231 -- Local Subprograms --
232 -----------------------
233
3cd4a210
AC
234 procedure Address_Checks;
235 -- Semantic checks for valid use of Address attribute. This was made
236 -- a separate routine with the idea of using it for unrestricted access
237 -- which seems like it should follow the same rules, but that turned
238 -- out to be impractical. So now this is only used for Address.
239
fbf5a39b 240 procedure Analyze_Access_Attribute;
996ae0b0
RK
241 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
242 -- Internally, Id distinguishes which of the three cases is involved.
243
b6a56408
AC
244 procedure Analyze_Attribute_Old_Result
245 (Legal : out Boolean;
246 Spec_Id : out Entity_Id);
247 -- Common processing for attributes 'Old and 'Result. The routine checks
248 -- that the attribute appears in a postcondition-like aspect or pragma
249 -- associated with a suitable subprogram or a body. Flag Legal is set
488f9623 250 -- when the above criteria are met. Spec_Id denotes the entity of the
b6a56408
AC
251 -- subprogram [body] or Empty if the attribute is illegal.
252
26df19ce
AC
253 procedure Bad_Attribute_For_Predicate;
254 -- Output error message for use of a predicate (First, Last, Range) not
255 -- allowed with a type that has predicates. If the type is a generic
256 -- actual, then the message is a warning, and we generate code to raise
86200f66 257 -- program error with an appropriate reason. No error message is given
011f9d5d
AC
258 -- for internally generated uses of the attributes. This legality rule
259 -- only applies to scalar types.
260
996ae0b0
RK
261 procedure Check_Array_Or_Scalar_Type;
262 -- Common procedure used by First, Last, Range attribute to check
263 -- that the prefix is a constrained array or scalar type, or a name
264 -- of an array object, and that an argument appears only if appropriate
265 -- (i.e. only in the array case).
266
267 procedure Check_Array_Type;
268 -- Common semantic checks for all array attributes. Checks that the
269 -- prefix is a constrained array type or the name of an array object.
270 -- The error message for non-arrays is specialized appropriately.
271
272 procedure Check_Asm_Attribute;
273 -- Common semantic checks for Asm_Input and Asm_Output attributes
274
275 procedure Check_Component;
276 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
277 -- Position. Checks prefix is an appropriate selected component.
278
279 procedure Check_Decimal_Fixed_Point_Type;
280 -- Check that prefix of attribute N is a decimal fixed-point type
281
282 procedure Check_Dereference;
283 -- If the prefix of attribute is an object of an access type, then
09494c32 284 -- introduce an explicit dereference, and adjust P_Type accordingly.
996ae0b0
RK
285
286 procedure Check_Discrete_Type;
287 -- Verify that prefix of attribute N is a discrete type
288
289 procedure Check_E0;
290 -- Check that no attribute arguments are present
291
292 procedure Check_Either_E0_Or_E1;
293 -- Check that there are zero or one attribute arguments present
294
295 procedure Check_E1;
296 -- Check that exactly one attribute argument is present
297
298 procedure Check_E2;
299 -- Check that two attribute arguments are present
300
301 procedure Check_Enum_Image;
4199e8c6
RD
302 -- If the prefix type of 'Image is an enumeration type, set all its
303 -- literals as referenced, since the image function could possibly end
304 -- up referencing any of the literals indirectly. Same for Enum_Val.
8097203f
AC
305 -- Set the flag only if the reference is in the main code unit. Same
306 -- restriction when resolving 'Value; otherwise an improperly set
4199e8c6
RD
307 -- reference when analyzing an inlined body will lose a proper
308 -- warning on a useless with_clause.
996ae0b0 309
011f9d5d
AC
310 procedure Check_First_Last_Valid;
311 -- Perform all checks for First_Valid and Last_Valid attributes
312
996ae0b0
RK
313 procedure Check_Fixed_Point_Type;
314 -- Verify that prefix of attribute N is a fixed type
315
316 procedure Check_Fixed_Point_Type_0;
317 -- Verify that prefix of attribute N is a fixed type and that
318 -- no attribute expressions are present
319
320 procedure Check_Floating_Point_Type;
321 -- Verify that prefix of attribute N is a float type
322
323 procedure Check_Floating_Point_Type_0;
324 -- Verify that prefix of attribute N is a float type and that
325 -- no attribute expressions are present
326
327 procedure Check_Floating_Point_Type_1;
328 -- Verify that prefix of attribute N is a float type and that
329 -- exactly one attribute expression is present
330
331 procedure Check_Floating_Point_Type_2;
332 -- Verify that prefix of attribute N is a float type and that
333 -- two attribute expressions are present
334
ce5ba43a 335 procedure Check_SPARK_05_Restriction_On_Attribute;
7a489a2b
AC
336 -- Issue an error in formal mode because attribute N is allowed
337
996ae0b0
RK
338 procedure Check_Integer_Type;
339 -- Verify that prefix of attribute N is an integer type
340
5f3ab6fb
AC
341 procedure Check_Modular_Integer_Type;
342 -- Verify that prefix of attribute N is a modular integer type
343
21d27997
RD
344 procedure Check_Not_CPP_Type;
345 -- Check that P (the prefix of the attribute) is not an CPP type
346 -- for which no Ada predefined primitive is available.
347
996ae0b0
RK
348 procedure Check_Not_Incomplete_Type;
349 -- Check that P (the prefix of the attribute) is not an incomplete
350 -- type or a private type for which no full view has been given.
351
352 procedure Check_Object_Reference (P : Node_Id);
99fc068e 353 -- Check that P is an object reference
996ae0b0 354
b6a56408
AC
355 procedure Check_PolyORB_Attribute;
356 -- Validity checking for PolyORB/DSA attribute
357
996ae0b0
RK
358 procedure Check_Program_Unit;
359 -- Verify that prefix of attribute N is a program unit
360
361 procedure Check_Real_Type;
362 -- Verify that prefix of attribute N is fixed or float type
363
364 procedure Check_Scalar_Type;
365 -- Verify that prefix of attribute N is a scalar type
366
367 procedure Check_Standard_Prefix;
f7ea2603
RD
368 -- Verify that prefix of attribute N is package Standard. Also checks
369 -- that there are no arguments.
996ae0b0 370
fbf5a39b
AC
371 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
372 -- Validity checking for stream attribute. Nam is the TSS name of the
996ae0b0 373 -- corresponding possible defined attribute function (e.g. for the
fbf5a39b 374 -- Read attribute, Nam will be TSS_Stream_Read).
996ae0b0 375
f7ea2603
RD
376 procedure Check_System_Prefix;
377 -- Verify that prefix of attribute N is package System
378
996ae0b0
RK
379 procedure Check_Task_Prefix;
380 -- Verify that prefix of attribute N is a task or task type
381
382 procedure Check_Type;
383 -- Verify that the prefix of attribute N is a type
384
385 procedure Check_Unit_Name (Nod : Node_Id);
386 -- Check that Nod is of the form of a library unit name, i.e that
387 -- it is an identifier, or a selected component whose prefix is
388 -- itself of the form of a library unit name. Note that this is
389 -- quite different from Check_Program_Unit, since it only checks
390 -- the syntactic form of the name, not the semantic identity. This
a015ef67
AC
391 -- is because it is used with attributes (Elab_Body, Elab_Spec and
392 -- Elaborated) which can refer to non-visible unit.
996ae0b0
RK
393
394 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
395 pragma No_Return (Error_Attr);
fbf5a39b
AC
396 procedure Error_Attr;
397 pragma No_Return (Error_Attr);
996ae0b0
RK
398 -- Posts error using Error_Msg_N at given node, sets type of attribute
399 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
400 -- semantic processing. The message typically contains a % insertion
fbf5a39b
AC
401 -- character which is replaced by the attribute name. The call with
402 -- no arguments is used when the caller has already generated the
403 -- required error messages.
996ae0b0 404
822033eb
HK
405 procedure Error_Attr_P (Msg : String);
406 pragma No_Return (Error_Attr);
407 -- Like Error_Attr, but error is posted at the start of the prefix
408
e7f23f06
AC
409 procedure Legal_Formal_Attribute;
410 -- Common processing for attributes Definite and Has_Discriminants.
411 -- Checks that prefix is generic indefinite formal type.
412
85d6bf87
AC
413 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
414 -- Common processing for attributes Max_Alignment_For_Allocation and
415 -- Max_Size_In_Storage_Elements.
416
417 procedure Min_Max;
418 -- Common processing for attributes Max and Min
419
996ae0b0
RK
420 procedure Standard_Attribute (Val : Int);
421 -- Used to process attributes whose prefix is package Standard which
422 -- yield values of type Universal_Integer. The attribute reference
edab6088
RD
423 -- node is rewritten with an integer literal of the given value which
424 -- is marked as static.
996ae0b0 425
96e90ac1
RD
426 procedure Uneval_Old_Msg;
427 -- Called when Loop_Entry or Old is used in a potentially unevaluated
428 -- expression. Generates appropriate message or warning depending on
1773d80b
AC
429 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
430 -- node in the aspect case).
96e90ac1 431
996ae0b0
RK
432 procedure Unexpected_Argument (En : Node_Id);
433 -- Signal unexpected attribute argument (En is the argument)
434
435 procedure Validate_Non_Static_Attribute_Function_Call;
436 -- Called when processing an attribute that is a function call to a
437 -- non-static function, i.e. an attribute function that either takes
438 -- non-scalar arguments or returns a non-scalar result. Verifies that
439 -- such a call does not appear in a preelaborable context.
440
3cd4a210
AC
441 --------------------
442 -- Address_Checks --
443 --------------------
444
445 procedure Address_Checks is
446 begin
447 -- An Address attribute created by expansion is legal even when it
448 -- applies to other entity-denoting expressions.
449
450 if not Comes_From_Source (N) then
451 return;
452
453 -- Address attribute on a protected object self reference is legal
454
455 elsif Is_Protected_Self_Reference (P) then
456 return;
457
458 -- Address applied to an entity
459
460 elsif Is_Entity_Name (P) then
461 declare
462 Ent : constant Entity_Id := Entity (P);
463
464 begin
465 if Is_Subprogram (Ent) then
466 Set_Address_Taken (Ent);
467 Kill_Current_Values (Ent);
468
469 -- An Address attribute is accepted when generated by the
470 -- compiler for dispatching operation, and an error is
471 -- issued once the subprogram is frozen (to avoid confusing
472 -- errors about implicit uses of Address in the dispatch
473 -- table initialization).
474
475 if Has_Pragma_Inline_Always (Entity (P))
476 and then Comes_From_Source (P)
477 then
478 Error_Attr_P
479 ("prefix of % attribute cannot be Inline_Always "
480 & "subprogram");
481
482 -- It is illegal to apply 'Address to an intrinsic
483 -- subprogram. This is now formalized in AI05-0095.
484 -- In an instance, an attempt to obtain 'Address of an
485 -- intrinsic subprogram (e.g the renaming of a predefined
486 -- operator that is an actual) raises Program_Error.
487
488 elsif Convention (Ent) = Convention_Intrinsic then
489 if In_Instance then
490 Rewrite (N,
491 Make_Raise_Program_Error (Loc,
492 Reason => PE_Address_Of_Intrinsic));
493
494 else
82893775 495 Error_Msg_Name_1 := Aname;
3cd4a210
AC
496 Error_Msg_N
497 ("cannot take % of intrinsic subprogram", N);
498 end if;
499
500 -- Issue an error if prefix denotes an eliminated subprogram
501
502 else
503 Check_For_Eliminated_Subprogram (P, Ent);
504 end if;
505
506 -- Object or label reference
507
508 elsif Is_Object (Ent) or else Ekind (Ent) = E_Label then
509 Set_Address_Taken (Ent);
510
511 -- Deal with No_Implicit_Aliasing restriction
512
513 if Restriction_Check_Required (No_Implicit_Aliasing) then
514 if not Is_Aliased_View (P) then
515 Check_Restriction (No_Implicit_Aliasing, P);
516 else
517 Check_No_Implicit_Aliasing (P);
518 end if;
519 end if;
520
521 -- If we have an address of an object, and the attribute
522 -- comes from source, then set the object as potentially
523 -- source modified. We do this because the resulting address
524 -- can potentially be used to modify the variable and we
525 -- might not detect this, leading to some junk warnings.
526
527 Set_Never_Set_In_Source (Ent, False);
528
529 -- Allow Address to be applied to task or protected type,
530 -- returning null address (what is that about???)
531
532 elsif (Is_Concurrent_Type (Etype (Ent))
533 and then Etype (Ent) = Base_Type (Ent))
534 or else Ekind (Ent) = E_Package
535 or else Is_Generic_Unit (Ent)
536 then
537 Rewrite (N,
538 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
539
540 -- Anything else is illegal
541
542 else
543 Error_Attr ("invalid prefix for % attribute", P);
544 end if;
545 end;
546
3cd4a210
AC
547 -- Object is OK
548
549 elsif Is_Object_Reference (P) then
550 return;
551
552 -- Subprogram called using dot notation
553
554 elsif Nkind (P) = N_Selected_Component
555 and then Is_Subprogram (Entity (Selector_Name (P)))
556 then
557 return;
558
559 -- What exactly are we allowing here ??? and is this properly
560 -- documented in the sinfo documentation for this node ???
561
562 elsif Relaxed_RM_Semantics
563 and then Nkind (P) = N_Attribute_Reference
564 then
565 return;
566
567 -- All other non-entity name cases are illegal
568
569 else
570 Error_Attr ("invalid prefix for % attribute", P);
571 end if;
572 end Address_Checks;
573
fbf5a39b
AC
574 ------------------------------
575 -- Analyze_Access_Attribute --
576 ------------------------------
996ae0b0 577
fbf5a39b 578 procedure Analyze_Access_Attribute is
996ae0b0
RK
579 Acc_Type : Entity_Id;
580
581 Scop : Entity_Id;
582 Typ : Entity_Id;
583
584 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
585 -- Build an access-to-object type whose designated type is DT,
586 -- and whose Ekind is appropriate to the attribute type. The
587 -- type that is constructed is returned as the result.
588
589 procedure Build_Access_Subprogram_Type (P : Node_Id);
21d27997
RD
590 -- Build an access to subprogram whose designated type is the type of
591 -- the prefix. If prefix is overloaded, so is the node itself. The
592 -- result is stored in Acc_Type.
996ae0b0 593
468c6c8a
ES
594 function OK_Self_Reference return Boolean;
595 -- An access reference whose prefix is a type can legally appear
596 -- within an aggregate, where it is obtained by expansion of
822033eb
HK
597 -- a defaulted aggregate. The enclosing aggregate that contains
598 -- the self-referenced is flagged so that the self-reference can
599 -- be expanded into a reference to the target object (see exp_aggr).
468c6c8a 600
996ae0b0
RK
601 ------------------------------
602 -- Build_Access_Object_Type --
603 ------------------------------
604
605 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
fea9e956
ES
606 Typ : constant Entity_Id :=
607 New_Internal_Entity
608 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
996ae0b0 609 begin
996ae0b0 610 Set_Etype (Typ, Typ);
996ae0b0
RK
611 Set_Is_Itype (Typ);
612 Set_Associated_Node_For_Itype (Typ, N);
613 Set_Directly_Designated_Type (Typ, DT);
614 return Typ;
615 end Build_Access_Object_Type;
616
617 ----------------------------------
618 -- Build_Access_Subprogram_Type --
619 ----------------------------------
620
621 procedure Build_Access_Subprogram_Type (P : Node_Id) is
12e0c41c
AC
622 Index : Interp_Index;
623 It : Interp;
996ae0b0 624
822033eb
HK
625 procedure Check_Local_Access (E : Entity_Id);
626 -- Deal with possible access to local subprogram. If we have such
627 -- an access, we set a flag to kill all tracked values on any call
628 -- because this access value may be passed around, and any called
629 -- code might use it to access a local procedure which clobbers a
70b70ce8
AC
630 -- tracked value. If the scope is a loop or block, indicate that
631 -- value tracking is disabled for the enclosing subprogram.
822033eb 632
996ae0b0 633 function Get_Kind (E : Entity_Id) return Entity_Kind;
edd63e9b 634 -- Distinguish between access to regular/protected subprograms
996ae0b0 635
822033eb
HK
636 ------------------------
637 -- Check_Local_Access --
638 ------------------------
639
640 procedure Check_Local_Access (E : Entity_Id) is
641 begin
642 if not Is_Library_Level_Entity (E) then
643 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
70b70ce8
AC
644 Set_Suppress_Value_Tracking_On_Call
645 (Nearest_Dynamic_Scope (Current_Scope));
822033eb
HK
646 end if;
647 end Check_Local_Access;
648
fbf5a39b
AC
649 --------------
650 -- Get_Kind --
651 --------------
652
996ae0b0
RK
653 function Get_Kind (E : Entity_Id) return Entity_Kind is
654 begin
655 if Convention (E) = Convention_Protected then
656 return E_Access_Protected_Subprogram_Type;
657 else
658 return E_Access_Subprogram_Type;
659 end if;
660 end Get_Kind;
661
662 -- Start of processing for Build_Access_Subprogram_Type
663
664 begin
12e0c41c
AC
665 -- In the case of an access to subprogram, use the name of the
666 -- subprogram itself as the designated type. Type-checking in
667 -- this case compares the signatures of the designated types.
668
e10dab7f
JM
669 -- Note: This fragment of the tree is temporarily malformed
670 -- because the correct tree requires an E_Subprogram_Type entity
671 -- as the designated type. In most cases this designated type is
12a13f01 672 -- later overridden by the semantics with the type imposed by the
e10dab7f
JM
673 -- context during the resolution phase. In the specific case of
674 -- the expression Address!(Prim'Unrestricted_Access), used to
675 -- initialize slots of dispatch tables, this work will be done by
676 -- the expander (see Exp_Aggr).
677
678 -- The reason to temporarily add this kind of node to the tree
679 -- instead of a proper E_Subprogram_Type itype, is the following:
680 -- in case of errors found in the source file we report better
681 -- error messages. For example, instead of generating the
682 -- following error:
683
684 -- "expected access to subprogram with profile
685 -- defined at line X"
686
687 -- we currently generate:
688
689 -- "expected access to function Z defined at line X"
690
edd63e9b
ES
691 Set_Etype (N, Any_Type);
692
996ae0b0 693 if not Is_Overloaded (P) then
822033eb
HK
694 Check_Local_Access (Entity (P));
695
edd63e9b 696 if not Is_Intrinsic_Subprogram (Entity (P)) then
e10dab7f
JM
697 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
698 Set_Is_Public (Acc_Type, False);
edd63e9b 699 Set_Etype (Acc_Type, Acc_Type);
e10dab7f 700 Set_Convention (Acc_Type, Convention (Entity (P)));
edd63e9b
ES
701 Set_Directly_Designated_Type (Acc_Type, Entity (P));
702 Set_Etype (N, Acc_Type);
e10dab7f 703 Freeze_Before (N, Acc_Type);
edd63e9b 704 end if;
996ae0b0
RK
705
706 else
707 Get_First_Interp (P, Index, It);
996ae0b0 708 while Present (It.Nam) loop
822033eb
HK
709 Check_Local_Access (It.Nam);
710
996ae0b0 711 if not Is_Intrinsic_Subprogram (It.Nam) then
e10dab7f
JM
712 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
713 Set_Is_Public (Acc_Type, False);
996ae0b0 714 Set_Etype (Acc_Type, Acc_Type);
e10dab7f 715 Set_Convention (Acc_Type, Convention (It.Nam));
996ae0b0
RK
716 Set_Directly_Designated_Type (Acc_Type, It.Nam);
717 Add_One_Interp (N, Acc_Type, Acc_Type);
e10dab7f 718 Freeze_Before (N, Acc_Type);
996ae0b0
RK
719 end if;
720
721 Get_Next_Interp (Index, It);
722 end loop;
edd63e9b 723 end if;
996ae0b0 724
822033eb
HK
725 -- Cannot be applied to intrinsic. Looking at the tests above,
726 -- the only way Etype (N) can still be set to Any_Type is if
727 -- Is_Intrinsic_Subprogram was True for some referenced entity.
728
edd63e9b 729 if Etype (N) = Any_Type then
822033eb 730 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
996ae0b0
RK
731 end if;
732 end Build_Access_Subprogram_Type;
733
468c6c8a
ES
734 ----------------------
735 -- OK_Self_Reference --
736 ----------------------
737
738 function OK_Self_Reference return Boolean is
739 Par : Node_Id;
740
741 begin
742 Par := Parent (N);
743 while Present (Par)
822033eb
HK
744 and then
745 (Nkind (Par) = N_Component_Association
746 or else Nkind (Par) in N_Subexpr)
468c6c8a 747 loop
e10dab7f 748 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
822033eb
HK
749 if Etype (Par) = Typ then
750 Set_Has_Self_Reference (Par);
b35e5dcb
AC
751
752 -- Check the context: the aggregate must be part of the
753 -- initialization of a type or component, or it is the
754 -- resulting expansion in an initialization procedure.
755
756 if Is_Init_Proc (Current_Scope) then
757 return True;
758 else
759 Par := Parent (Par);
760 while Present (Par) loop
761 if Nkind (Par) = N_Full_Type_Declaration then
762 return True;
763 end if;
764
765 Par := Parent (Par);
766 end loop;
767 end if;
768
769 return False;
822033eb
HK
770 end if;
771 end if;
772
468c6c8a
ES
773 Par := Parent (Par);
774 end loop;
775
822033eb
HK
776 -- No enclosing aggregate, or not a self-reference
777
778 return False;
468c6c8a
ES
779 end OK_Self_Reference;
780
fbf5a39b 781 -- Start of processing for Analyze_Access_Attribute
996ae0b0
RK
782
783 begin
ce5ba43a 784 Check_SPARK_05_Restriction_On_Attribute;
996ae0b0
RK
785 Check_E0;
786
787 if Nkind (P) = N_Character_Literal then
822033eb
HK
788 Error_Attr_P
789 ("prefix of % attribute cannot be enumeration literal");
fbf5a39b 790 end if;
996ae0b0 791
12e0c41c 792 -- Case of access to subprogram
996ae0b0 793
b07b7ace 794 if Is_Entity_Name (P) and then Is_Overloadable (Entity (P)) then
7b76e805 795 if Has_Pragma_Inline_Always (Entity (P)) then
822033eb
HK
796 Error_Attr_P
797 ("prefix of % attribute cannot be Inline_Always subprogram");
edd63e9b 798
a7e68e7f 799 elsif Aname = Name_Unchecked_Access then
468c6c8a
ES
800 Error_Attr ("attribute% cannot be applied to a subprogram", P);
801 end if;
802
16212e89
GD
803 -- Issue an error if the prefix denotes an eliminated subprogram
804
805 Check_For_Eliminated_Subprogram (P, Entity (P));
806
b5c739f9
RD
807 -- Check for obsolescent subprogram reference
808
809 Check_Obsolescent_2005_Entity (Entity (P), P);
810
12e0c41c
AC
811 -- Build the appropriate subprogram type
812
996ae0b0 813 Build_Access_Subprogram_Type (P);
fbf5a39b 814
6b81741c
AC
815 -- For P'Access or P'Unrestricted_Access, where P is a nested
816 -- subprogram, we might be passing P to another subprogram (but we
817 -- don't check that here), which might call P. P could modify
818 -- local variables, so we need to kill current values. It is
819 -- important not to do this for library-level subprograms, because
820 -- Kill_Current_Values is very inefficient in the case of library
821 -- level packages with lots of tagged types.
822
823 if Is_Library_Level_Entity (Entity (Prefix (N))) then
824 null;
825
826 -- Do not kill values on nodes initializing dispatch tables
827 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
828 -- is currently generated by the expander only for this
829 -- purpose. Done to keep the quality of warnings currently
830 -- generated by the compiler (otherwise any declaration of
831 -- a tagged type cleans constant indications from its scope).
832
833 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
834 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
835 or else
836 Etype (Parent (N)) = RTE (RE_Size_Ptr))
837 and then Is_Dispatching_Operation
838 (Directly_Designated_Type (Etype (N)))
839 then
840 null;
841
842 else
843 Kill_Current_Values;
fbf5a39b
AC
844 end if;
845
97b9064f
BD
846 -- In the static elaboration model, treat the attribute reference
847 -- as a call for elaboration purposes. Suppress this treatment
848 -- under debug flag. In any case, we are all done.
1f163ef7 849
97b9064f 850 if not Dynamic_Elaboration_Checks and not Debug_Flag_Dot_UU then
1f163ef7
AC
851 Check_Elab_Call (N);
852 end if;
853
996ae0b0
RK
854 return;
855
12e0c41c 856 -- Component is an operation of a protected type
996ae0b0 857
fbf5a39b
AC
858 elsif Nkind (P) = N_Selected_Component
859 and then Is_Overloadable (Entity (Selector_Name (P)))
996ae0b0
RK
860 then
861 if Ekind (Entity (Selector_Name (P))) = E_Entry then
822033eb 862 Error_Attr_P ("prefix of % attribute must be subprogram");
996ae0b0
RK
863 end if;
864
865 Build_Access_Subprogram_Type (Selector_Name (P));
866 return;
867 end if;
868
869 -- Deal with incorrect reference to a type, but note that some
468c6c8a
ES
870 -- accesses are allowed: references to the current type instance,
871 -- or in Ada 2005 self-referential pointer in a default-initialized
872 -- aggregate.
996ae0b0
RK
873
874 if Is_Entity_Name (P) then
996ae0b0
RK
875 Typ := Entity (P);
876
65f01153
RD
877 -- The reference may appear in an aggregate that has been expanded
878 -- into a loop. Locate scope of type definition, if any.
879
880 Scop := Current_Scope;
881 while Ekind (Scop) = E_Loop loop
882 Scop := Scope (Scop);
883 end loop;
884
996ae0b0
RK
885 if Is_Type (Typ) then
886
887 -- OK if we are within the scope of a limited type
888 -- let's mark the component as having per object constraint
889
890 if Is_Anonymous_Tagged_Base (Scop, Typ) then
891 Typ := Scop;
892 Set_Entity (P, Typ);
893 Set_Etype (P, Typ);
894 end if;
895
896 if Typ = Scop then
897 declare
898 Q : Node_Id := Parent (N);
899
900 begin
901 while Present (Q)
902 and then Nkind (Q) /= N_Component_Declaration
903 loop
904 Q := Parent (Q);
905 end loop;
65f01153 906
996ae0b0 907 if Present (Q) then
b87971f3
AC
908 Set_Has_Per_Object_Constraint
909 (Defining_Identifier (Q), True);
996ae0b0
RK
910 end if;
911 end;
912
913 if Nkind (P) = N_Expanded_Name then
822033eb 914 Error_Msg_F
996ae0b0
RK
915 ("current instance prefix must be a direct name", P);
916 end if;
917
21d27997
RD
918 -- If a current instance attribute appears in a component
919 -- constraint it must appear alone; other contexts (spec-
920 -- expressions, within a task body) are not subject to this
921 -- restriction.
996ae0b0 922
21d27997 923 if not In_Spec_Expression
996ae0b0 924 and then not Has_Completion (Scop)
e10dab7f
JM
925 and then not
926 Nkind_In (Parent (N), N_Discriminant_Association,
927 N_Index_Or_Discriminant_Constraint)
996ae0b0
RK
928 then
929 Error_Msg_N
930 ("current instance attribute must appear alone", N);
931 end if;
cefce34c
JM
932
933 if Is_CPP_Class (Root_Type (Typ)) then
934 Error_Msg_N
324ac540 935 ("??current instance unsupported for derivations of "
cefce34c
JM
936 & "'C'P'P types", N);
937 end if;
996ae0b0
RK
938
939 -- OK if we are in initialization procedure for the type
940 -- in question, in which case the reference to the type
941 -- is rewritten as a reference to the current object.
942
943 elsif Ekind (Scop) = E_Procedure
fbf5a39b 944 and then Is_Init_Proc (Scop)
996ae0b0
RK
945 and then Etype (First_Formal (Scop)) = Typ
946 then
947 Rewrite (N,
948 Make_Attribute_Reference (Loc,
949 Prefix => Make_Identifier (Loc, Name_uInit),
950 Attribute_Name => Name_Unrestricted_Access));
951 Analyze (N);
952 return;
953
954 -- OK if a task type, this test needs sharpening up ???
955
956 elsif Is_Task_Type (Typ) then
957 null;
958
468c6c8a
ES
959 -- OK if self-reference in an aggregate in Ada 2005, and
960 -- the reference comes from a copied default expression.
961
822033eb
HK
962 -- Note that we check legality of self-reference even if the
963 -- expression comes from source, e.g. when a single component
964 -- association in an aggregate has a box association.
965
0791fbe9 966 elsif Ada_Version >= Ada_2005
468c6c8a
ES
967 and then OK_Self_Reference
968 then
969 null;
970
ae8c7d87 971 -- OK if reference to current instance of a protected object
2d14501c
ST
972
973 elsif Is_Protected_Self_Reference (P) then
974 null;
975
996ae0b0
RK
976 -- Otherwise we have an error case
977
978 else
979 Error_Attr ("% attribute cannot be applied to type", P);
980 return;
981 end if;
982 end if;
983 end if;
984
b07b7ace
AC
985 -- If we fall through, we have a normal access to object case
986
987 -- Unrestricted_Access is (for now) legal wherever an allocator would
988 -- be legal, so its Etype is set to E_Allocator. The expected type
996ae0b0
RK
989 -- of the other attributes is a general access type, and therefore
990 -- we label them with E_Access_Attribute_Type.
991
992 if not Is_Overloaded (P) then
993 Acc_Type := Build_Access_Object_Type (P_Type);
994 Set_Etype (N, Acc_Type);
b07b7ace 995
996ae0b0
RK
996 else
997 declare
998 Index : Interp_Index;
999 It : Interp;
996ae0b0
RK
1000 begin
1001 Set_Etype (N, Any_Type);
1002 Get_First_Interp (P, Index, It);
996ae0b0
RK
1003 while Present (It.Typ) loop
1004 Acc_Type := Build_Access_Object_Type (It.Typ);
1005 Add_One_Interp (N, Acc_Type, Acc_Type);
1006 Get_Next_Interp (Index, It);
1007 end loop;
1008 end;
1009 end if;
1010
6f31a9d7 1011 -- Special cases when we can find a prefix that is an entity name
fbf5a39b 1012
6f31a9d7
RD
1013 declare
1014 PP : Node_Id;
1015 Ent : Entity_Id;
1016
1017 begin
1018 PP := P;
1019 loop
1020 if Is_Entity_Name (PP) then
1021 Ent := Entity (PP);
822033eb 1022
6f31a9d7
RD
1023 -- If we have an access to an object, and the attribute
1024 -- comes from source, then set the object as potentially
1025 -- source modified. We do this because the resulting access
1026 -- pointer can be used to modify the variable, and we might
1027 -- not detect this, leading to some junk warnings.
822033eb 1028
74014283
RD
1029 -- We only do this for source references, since otherwise
1030 -- we can suppress warnings, e.g. from the unrestricted
1031 -- access generated for validity checks in -gnatVa mode.
1032
1033 if Comes_From_Source (N) then
1034 Set_Never_Set_In_Source (Ent, False);
1035 end if;
822033eb 1036
6f31a9d7 1037 -- Mark entity as address taken, and kill current values
822033eb 1038
6f31a9d7
RD
1039 Set_Address_Taken (Ent);
1040 Kill_Current_Values (Ent);
1041 exit;
1042
e10dab7f
JM
1043 elsif Nkind_In (PP, N_Selected_Component,
1044 N_Indexed_Component)
6f31a9d7
RD
1045 then
1046 PP := Prefix (PP);
1047
1048 else
1049 exit;
1050 end if;
1051 end loop;
1052 end;
fbf5a39b 1053
b07b7ace
AC
1054 -- Check for aliased view.. We allow a nonaliased prefix when within
1055 -- an instance because the prefix may have been a tagged formal
1056 -- object, which is defined to be aliased even when the actual
1057 -- might not be (other instance cases will have been caught in the
1058 -- generic). Similarly, within an inlined body we know that the
1059 -- attribute is legal in the original subprogram, and therefore
1060 -- legal in the expansion.
996ae0b0 1061
b07b7ace 1062 if not Is_Aliased_View (P)
996ae0b0 1063 and then not In_Instance
a41ea816 1064 and then not In_Inlined_Body
715e529d 1065 and then Comes_From_Source (N)
996ae0b0 1066 then
b07b7ace
AC
1067 -- Here we have a non-aliased view. This is illegal unless we
1068 -- have the case of Unrestricted_Access, where for now we allow
1069 -- this (we will reject later if expected type is access to an
1070 -- unconstrained array with a thin pointer).
1071
715e529d
AC
1072 -- No need for an error message on a generated access reference
1073 -- for the controlling argument in a dispatching call: error will
1074 -- be reported when resolving the call.
1075
b07b7ace
AC
1076 if Aname /= Name_Unrestricted_Access then
1077 Error_Attr_P ("prefix of % attribute must be aliased");
1078 Check_No_Implicit_Aliasing (P);
1079
1080 -- For Unrestricted_Access, record that prefix is not aliased
1081 -- to simplify legality check later on.
1082
1083 else
1084 Set_Non_Aliased_Prefix (N);
1085 end if;
1086
1087 -- If we have an aliased view, and we have Unrestricted_Access, then
1088 -- output a warning that Unchecked_Access would have been fine, and
1089 -- change the node to be Unchecked_Access.
1090
1091 else
1092 -- For now, hold off on this change ???
1093
1094 null;
996ae0b0 1095 end if;
fbf5a39b 1096 end Analyze_Access_Attribute;
996ae0b0 1097
b6a56408
AC
1098 ----------------------------------
1099 -- Analyze_Attribute_Old_Result --
1100 ----------------------------------
1101
1102 procedure Analyze_Attribute_Old_Result
1103 (Legal : out Boolean;
1104 Spec_Id : out Entity_Id)
1105 is
1106 procedure Check_Placement_In_Check (Prag : Node_Id);
1107 -- Verify that the attribute appears within pragma Check that mimics
1108 -- a postcondition.
1109
1110 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id);
1111 -- Verify that the attribute appears within a consequence of aspect
1112 -- or pragma Contract_Cases denoted by Prag.
1113
1114 procedure Check_Placement_In_Test_Case (Prag : Node_Id);
1115 -- Verify that the attribute appears within the "Ensures" argument of
1116 -- aspect or pragma Test_Case denoted by Prag.
1117
1118 function Is_Within
1119 (Nod : Node_Id;
1120 Encl_Nod : Node_Id) return Boolean;
1121 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1122 -- node Nod is within enclosing node Encl_Nod.
1123
26f36fc9
AC
1124 procedure Placement_Error;
1125 -- Emit a general error when the attributes does not appear in a
1126 -- postcondition-like aspect or pragma.
1127
b6a56408
AC
1128 ------------------------------
1129 -- Check_Placement_In_Check --
1130 ------------------------------
1131
1132 procedure Check_Placement_In_Check (Prag : Node_Id) is
1133 Args : constant List_Id := Pragma_Argument_Associations (Prag);
1134 Nam : constant Name_Id := Chars (Get_Pragma_Arg (First (Args)));
1135
1136 begin
1137 -- The "Name" argument of pragma Check denotes a postcondition
1138
1139 if Nam_In (Nam, Name_Post,
1140 Name_Post_Class,
1141 Name_Postcondition,
1142 Name_Refined_Post)
1143 then
1144 null;
1145
1146 -- Otherwise the placement of the attribute is illegal
1147
1148 else
26f36fc9 1149 Placement_Error;
b6a56408
AC
1150 end if;
1151 end Check_Placement_In_Check;
1152
1153 ---------------------------------------
1154 -- Check_Placement_In_Contract_Cases --
1155 ---------------------------------------
1156
1157 procedure Check_Placement_In_Contract_Cases (Prag : Node_Id) is
1158 Arg : Node_Id;
1159 Cases : Node_Id;
1160 CCase : Node_Id;
1161
1162 begin
1163 -- Obtain the argument of the aspect or pragma
1164
1165 if Nkind (Prag) = N_Aspect_Specification then
1166 Arg := Prag;
1167 else
1168 Arg := First (Pragma_Argument_Associations (Prag));
1169 end if;
1170
1171 Cases := Expression (Arg);
1172
1173 if Present (Component_Associations (Cases)) then
1174 CCase := First (Component_Associations (Cases));
1175 while Present (CCase) loop
1176
1177 -- Detect whether the attribute appears within the
1178 -- consequence of the current contract case.
1179
1180 if Nkind (CCase) = N_Component_Association
1181 and then Is_Within (N, Expression (CCase))
1182 then
1183 return;
1184 end if;
1185
1186 Next (CCase);
1187 end loop;
1188 end if;
1189
1190 -- Otherwise aspect or pragma Contract_Cases is either malformed
1191 -- or the attribute does not appear within a consequence.
1192
1193 Error_Attr
1194 ("attribute % must appear in the consequence of a contract case",
1195 P);
1196 end Check_Placement_In_Contract_Cases;
1197
1198 ----------------------------------
1199 -- Check_Placement_In_Test_Case --
1200 ----------------------------------
1201
1202 procedure Check_Placement_In_Test_Case (Prag : Node_Id) is
1203 Arg : constant Node_Id :=
1204 Test_Case_Arg
1205 (Prag => Prag,
1206 Arg_Nam => Name_Ensures,
1207 From_Aspect => Nkind (Prag) = N_Aspect_Specification);
1208
1209 begin
1210 -- Detect whether the attribute appears within the "Ensures"
1211 -- expression of aspect or pragma Test_Case.
1212
1213 if Present (Arg) and then Is_Within (N, Arg) then
1214 null;
1215
1216 else
1217 Error_Attr
1218 ("attribute % must appear in the ensures expression of a "
1219 & "test case", P);
1220 end if;
1221 end Check_Placement_In_Test_Case;
1222
1223 ---------------
1224 -- Is_Within --
1225 ---------------
1226
1227 function Is_Within
1228 (Nod : Node_Id;
1229 Encl_Nod : Node_Id) return Boolean
1230 is
1231 Par : Node_Id;
1232
1233 begin
1234 Par := Nod;
1235 while Present (Par) loop
1236 if Par = Encl_Nod then
1237 return True;
1238
1239 -- Prevent the search from going too far
1240
1241 elsif Is_Body_Or_Package_Declaration (Par) then
1242 exit;
1243 end if;
1244
1245 Par := Parent (Par);
1246 end loop;
1247
1248 return False;
1249 end Is_Within;
1250
26f36fc9
AC
1251 ---------------------
1252 -- Placement_Error --
1253 ---------------------
1254
1255 procedure Placement_Error is
1256 begin
1257 if Aname = Name_Old then
1258 Error_Attr ("attribute % can only appear in postcondition", P);
1259
1260 -- Specialize the error message for attribute 'Result
1261
1262 else
1263 Error_Attr
1264 ("attribute % can only appear in postcondition of function",
1265 P);
1266 end if;
1267 end Placement_Error;
1268
b6a56408
AC
1269 -- Local variables
1270
1271 Prag : Node_Id;
1272 Prag_Nam : Name_Id;
1273 Subp_Decl : Node_Id;
1274
1275 -- Start of processing for Analyze_Attribute_Old_Result
1276
1277 begin
1278 -- Assume that the attribute is illegal
1279
1280 Legal := False;
1281 Spec_Id := Empty;
1282
1283 -- Traverse the parent chain to find the aspect or pragma where the
1284 -- attribute resides.
1285
1286 Prag := N;
1287 while Present (Prag) loop
1288 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1289 exit;
1290
1291 -- Prevent the search from going too far
1292
1293 elsif Is_Body_Or_Package_Declaration (Prag) then
1294 exit;
1295 end if;
1296
1297 Prag := Parent (Prag);
1298 end loop;
1299
1300 -- The attribute is allowed to appear only in postcondition-like
1301 -- aspects or pragmas.
1302
1303 if Nkind_In (Prag, N_Aspect_Specification, N_Pragma) then
1304 if Nkind (Prag) = N_Aspect_Specification then
1305 Prag_Nam := Chars (Identifier (Prag));
1306 else
1307 Prag_Nam := Pragma_Name (Prag);
1308 end if;
1309
1310 if Prag_Nam = Name_Check then
1311 Check_Placement_In_Check (Prag);
1312
1313 elsif Prag_Nam = Name_Contract_Cases then
1314 Check_Placement_In_Contract_Cases (Prag);
1315
caf07df9
AC
1316 -- Attribute 'Result is allowed to appear in aspect or pragma
1317 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1318
1319 elsif Nam_In (Prag_Nam, Name_Depends, Name_Refined_Depends)
1320 and then Aname = Name_Result
1321 then
1322 null;
1323
b6a56408
AC
1324 elsif Nam_In (Prag_Nam, Name_Post,
1325 Name_Post_Class,
1326 Name_Postcondition,
1327 Name_Refined_Post)
1328 then
1329 null;
1330
1331 elsif Prag_Nam = Name_Test_Case then
1332 Check_Placement_In_Test_Case (Prag);
1333
1334 else
26f36fc9 1335 Placement_Error;
b6a56408
AC
1336 return;
1337 end if;
1338
1339 -- Otherwise the placement of the attribute is illegal
1340
1341 else
26f36fc9 1342 Placement_Error;
b6a56408
AC
1343 return;
1344 end if;
1345
1346 -- Find the related subprogram subject to the aspect or pragma
1347
1348 if Nkind (Prag) = N_Aspect_Specification then
1349 Subp_Decl := Parent (Prag);
1350 else
f99ff327 1351 Subp_Decl := Find_Related_Declaration_Or_Body (Prag);
b6a56408
AC
1352 end if;
1353
1354 -- The aspect or pragma where the attribute resides should be
1355 -- associated with a subprogram declaration or a body. If this is not
1356 -- the case, then the aspect or pragma is illegal. Return as analysis
0c3f76ba
AC
1357 -- cannot be carried out. Note that it is legal to have the aspect
1358 -- appear on a subprogram renaming, when the renamed entity is an
1359 -- attribute reference.
b6a56408
AC
1360
1361 if not Nkind_In (Subp_Decl, N_Abstract_Subprogram_Declaration,
1362 N_Entry_Declaration,
1363 N_Generic_Subprogram_Declaration,
1364 N_Subprogram_Body,
1365 N_Subprogram_Body_Stub,
0c3f76ba
AC
1366 N_Subprogram_Declaration,
1367 N_Subprogram_Renaming_Declaration)
b6a56408
AC
1368 then
1369 return;
1370 end if;
1371
1372 -- If we get here, then the attribute is legal
1373
1374 Legal := True;
877a5a12 1375 Spec_Id := Unique_Defining_Entity (Subp_Decl);
1ba563f5
AC
1376
1377 -- When generating C code, nested _postcondition subprograms are
1378 -- inlined by the front end to avoid problems (when unnested) with
1379 -- referenced itypes. Handle that here, since as part of inlining the
1380 -- expander nests subprogram within a dummy procedure named _parent
1381 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1382 -- Hence, in this context, the spec_id of _postconditions is the
1383 -- enclosing scope.
1384
64f5d139 1385 if Modify_Tree_For_C
1ba563f5
AC
1386 and then Chars (Spec_Id) = Name_uParent
1387 and then Chars (Scope (Spec_Id)) = Name_uPostconditions
1388 then
64f5d139 1389 -- This situation occurs only when preanalyzing the inlined body
fb757f7d 1390
64f5d139
JM
1391 pragma Assert (not Full_Analysis);
1392
1ba563f5
AC
1393 Spec_Id := Scope (Spec_Id);
1394 pragma Assert (Is_Inlined (Spec_Id));
1395 end if;
b6a56408
AC
1396 end Analyze_Attribute_Old_Result;
1397
26df19ce
AC
1398 ---------------------------------
1399 -- Bad_Attribute_For_Predicate --
1400 ---------------------------------
1401
1402 procedure Bad_Attribute_For_Predicate is
1403 begin
9515740f 1404 if Is_Scalar_Type (P_Type)
739e7bbf 1405 and then Comes_From_Source (N)
9515740f 1406 then
26df19ce 1407 Error_Msg_Name_1 := Aname;
86200f66 1408 Bad_Predicated_Subtype_Use
ed00f472 1409 ("type& has predicates, attribute % not allowed", N, P_Type);
26df19ce
AC
1410 end if;
1411 end Bad_Attribute_For_Predicate;
1412
996ae0b0
RK
1413 --------------------------------
1414 -- Check_Array_Or_Scalar_Type --
1415 --------------------------------
1416
1417 procedure Check_Array_Or_Scalar_Type is
268aeaa9
AC
1418 function In_Aspect_Specification return Boolean;
1419 -- A current instance of a type in an aspect specification is an
1420 -- object and not a type, and therefore cannot be of a scalar type
1421 -- in the prefix of one of the array attributes if the attribute
1422 -- reference is part of an aspect expression.
1423
1424 -----------------------------
1425 -- In_Aspect_Specification --
1426 -----------------------------
1427
1428 function In_Aspect_Specification return Boolean is
1429 P : Node_Id;
1430
1431 begin
1432 P := Parent (N);
1433 while Present (P) loop
1434 if Nkind (P) = N_Aspect_Specification then
1435 return P_Type = Entity (P);
1436
1437 elsif Nkind (P) in N_Declaration then
1438 return False;
1439 end if;
1440
1441 P := Parent (P);
1442 end loop;
1443
1444 return False;
1445 end In_Aspect_Specification;
1446
1447 -- Local variables
1448
1449 Dims : Int;
996ae0b0
RK
1450 Index : Entity_Id;
1451
268aeaa9 1452 -- Start of processing for Check_Array_Or_Scalar_Type
996ae0b0
RK
1453
1454 begin
1455 -- Case of string literal or string literal subtype. These cases
1456 -- cannot arise from legal Ada code, but the expander is allowed
1457 -- to generate them. They require special handling because string
1458 -- literal subtypes do not have standard bounds (the whole idea
1459 -- of these subtypes is to avoid having to generate the bounds)
1460
1461 if Ekind (P_Type) = E_String_Literal_Subtype then
1462 Set_Etype (N, Etype (First_Index (P_Base_Type)));
1463 return;
1464
1465 -- Scalar types
1466
1467 elsif Is_Scalar_Type (P_Type) then
1468 Check_Type;
1469
1470 if Present (E1) then
1471 Error_Attr ("invalid argument in % attribute", E1);
268aeaa9
AC
1472
1473 elsif In_Aspect_Specification then
1474 Error_Attr
1475 ("prefix of % attribute cannot be the current instance of a "
1476 & "scalar type", P);
1477
996ae0b0
RK
1478 else
1479 Set_Etype (N, P_Base_Type);
1480 return;
1481 end if;
1482
1483 -- The following is a special test to allow 'First to apply to
1484 -- private scalar types if the attribute comes from generated
1485 -- code. This occurs in the case of Normalize_Scalars code.
1486
1487 elsif Is_Private_Type (P_Type)
1488 and then Present (Full_View (P_Type))
1489 and then Is_Scalar_Type (Full_View (P_Type))
1490 and then not Comes_From_Source (N)
1491 then
1492 Set_Etype (N, Implementation_Base_Type (P_Type));
1493
1494 -- Array types other than string literal subtypes handled above
1495
1496 else
1497 Check_Array_Type;
1498
1499 -- We know prefix is an array type, or the name of an array
1500 -- object, and that the expression, if present, is static
1501 -- and within the range of the dimensions of the type.
1502
5453d5bd
AC
1503 pragma Assert (Is_Array_Type (P_Type));
1504 Index := First_Index (P_Base_Type);
996ae0b0
RK
1505
1506 if No (E1) then
1507
1508 -- First dimension assumed
1509
1510 Set_Etype (N, Base_Type (Etype (Index)));
1511
1512 else
268aeaa9 1513 Dims := UI_To_Int (Intval (E1));
996ae0b0 1514
268aeaa9 1515 for J in 1 .. Dims - 1 loop
996ae0b0
RK
1516 Next_Index (Index);
1517 end loop;
1518
1519 Set_Etype (N, Base_Type (Etype (Index)));
1520 Set_Etype (E1, Standard_Integer);
1521 end if;
1522 end if;
1523 end Check_Array_Or_Scalar_Type;
1524
1525 ----------------------
1526 -- Check_Array_Type --
1527 ----------------------
1528
1529 procedure Check_Array_Type is
1530 D : Int;
edd63e9b 1531 -- Dimension number for array attributes
996ae0b0
RK
1532
1533 begin
1534 -- If the type is a string literal type, then this must be generated
1535 -- internally, and no further check is required on its legality.
1536
1537 if Ekind (P_Type) = E_String_Literal_Subtype then
1538 return;
1539
1540 -- If the type is a composite, it is an illegal aggregate, no point
1541 -- in going on.
1542
1543 elsif P_Type = Any_Composite then
1544 raise Bad_Attribute;
1545 end if;
1546
1547 -- Normal case of array type or subtype
1548
1549 Check_Either_E0_Or_E1;
5453d5bd 1550 Check_Dereference;
996ae0b0
RK
1551
1552 if Is_Array_Type (P_Type) then
1553 if not Is_Constrained (P_Type)
1554 and then Is_Entity_Name (P)
1555 and then Is_Type (Entity (P))
1556 then
1557 -- Note: we do not call Error_Attr here, since we prefer to
1558 -- continue, using the relevant index type of the array,
1559 -- even though it is unconstrained. This gives better error
1560 -- recovery behavior.
1561
1562 Error_Msg_Name_1 := Aname;
822033eb 1563 Error_Msg_F
996ae0b0
RK
1564 ("prefix for % attribute must be constrained array", P);
1565 end if;
1566
ca1ffed0
AC
1567 -- The attribute reference freezes the type, and thus the
1568 -- component type, even if the attribute may not depend on the
1569 -- component. Diagnose arrays with incomplete components now.
1570 -- If the prefix is an access to array, this does not freeze
1571 -- the designated type.
1572
1573 if Nkind (P) /= N_Explicit_Dereference then
1574 Check_Fully_Declared (Component_Type (P_Type), P);
1575 end if;
1576
996ae0b0
RK
1577 D := Number_Dimensions (P_Type);
1578
996ae0b0
RK
1579 else
1580 if Is_Private_Type (P_Type) then
822033eb 1581 Error_Attr_P ("prefix for % attribute may not be private type");
996ae0b0 1582
5453d5bd
AC
1583 elsif Is_Access_Type (P_Type)
1584 and then Is_Array_Type (Designated_Type (P_Type))
1585 and then Is_Entity_Name (P)
1586 and then Is_Type (Entity (P))
1587 then
822033eb 1588 Error_Attr_P ("prefix of % attribute cannot be access type");
5453d5bd 1589
996ae0b0
RK
1590 elsif Attr_Id = Attribute_First
1591 or else
1592 Attr_Id = Attribute_Last
1593 then
1594 Error_Attr ("invalid prefix for % attribute", P);
1595
1596 else
822033eb 1597 Error_Attr_P ("prefix for % attribute must be array");
996ae0b0
RK
1598 end if;
1599 end if;
1600
1601 if Present (E1) then
1602 Resolve (E1, Any_Integer);
1603 Set_Etype (E1, Standard_Integer);
1604
edab6088 1605 if not Is_OK_Static_Expression (E1)
996ae0b0
RK
1606 or else Raises_Constraint_Error (E1)
1607 then
fbf5a39b
AC
1608 Flag_Non_Static_Expr
1609 ("expression for dimension must be static!", E1);
1610 Error_Attr;
996ae0b0 1611
9fe696a3 1612 elsif UI_To_Int (Expr_Value (E1)) > D
996ae0b0
RK
1613 or else UI_To_Int (Expr_Value (E1)) < 1
1614 then
1615 Error_Attr ("invalid dimension number for array type", E1);
1616 end if;
1617 end if;
fea9e956
ES
1618
1619 if (Style_Check and Style_Check_Array_Attribute_Index)
1620 and then Comes_From_Source (N)
1621 then
1622 Style.Check_Array_Attribute_Index (N, E1, D);
1623 end if;
996ae0b0
RK
1624 end Check_Array_Type;
1625
1626 -------------------------
1627 -- Check_Asm_Attribute --
1628 -------------------------
1629
1630 procedure Check_Asm_Attribute is
1631 begin
1632 Check_Type;
1633 Check_E2;
1634
1635 -- Check first argument is static string expression
1636
1637 Analyze_And_Resolve (E1, Standard_String);
1638
1639 if Etype (E1) = Any_Type then
1640 return;
1641
1642 elsif not Is_OK_Static_Expression (E1) then
fbf5a39b
AC
1643 Flag_Non_Static_Expr
1644 ("constraint argument must be static string expression!", E1);
1645 Error_Attr;
996ae0b0
RK
1646 end if;
1647
1648 -- Check second argument is right type
1649
1650 Analyze_And_Resolve (E2, Entity (P));
1651
1652 -- Note: that is all we need to do, we don't need to check
1653 -- that it appears in a correct context. The Ada type system
1654 -- will do that for us.
1655
1656 end Check_Asm_Attribute;
1657
1658 ---------------------
1659 -- Check_Component --
1660 ---------------------
1661
1662 procedure Check_Component is
1663 begin
1664 Check_E0;
1665
1666 if Nkind (P) /= N_Selected_Component
1667 or else
1668 (Ekind (Entity (Selector_Name (P))) /= E_Component
1669 and then
1670 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1671 then
822033eb 1672 Error_Attr_P ("prefix for % attribute must be selected component");
996ae0b0
RK
1673 end if;
1674 end Check_Component;
1675
1676 ------------------------------------
1677 -- Check_Decimal_Fixed_Point_Type --
1678 ------------------------------------
1679
1680 procedure Check_Decimal_Fixed_Point_Type is
1681 begin
1682 Check_Type;
1683
1684 if not Is_Decimal_Fixed_Point_Type (P_Type) then
822033eb 1685 Error_Attr_P ("prefix of % attribute must be decimal type");
996ae0b0
RK
1686 end if;
1687 end Check_Decimal_Fixed_Point_Type;
1688
1689 -----------------------
1690 -- Check_Dereference --
1691 -----------------------
1692
1693 procedure Check_Dereference is
1694 begin
6d11af89
AC
1695
1696 -- Case of a subtype mark
1697
b285815e 1698 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
6d11af89
AC
1699 return;
1700 end if;
1701
1702 -- Case of an expression
1703
1704 Resolve (P);
cc335f43 1705
6d11af89 1706 if Is_Access_Type (P_Type) then
5453d5bd 1707
b285815e
RD
1708 -- If there is an implicit dereference, then we must freeze the
1709 -- designated type of the access type, since the type of the
1710 -- referenced array is this type (see AI95-00106).
b9262540 1711
3249690d 1712 -- As done elsewhere, freezing must not happen when pre-analyzing
b285815e
RD
1713 -- a pre- or postcondition or a default value for an object or for
1714 -- a formal parameter.
5453d5bd 1715
3249690d
AC
1716 if not In_Spec_Expression then
1717 Freeze_Before (N, Designated_Type (P_Type));
1718 end if;
5453d5bd 1719
996ae0b0
RK
1720 Rewrite (P,
1721 Make_Explicit_Dereference (Sloc (P),
1722 Prefix => Relocate_Node (P)));
1723
1724 Analyze_And_Resolve (P);
1725 P_Type := Etype (P);
1726
1727 if P_Type = Any_Type then
1728 raise Bad_Attribute;
1729 end if;
1730
1731 P_Base_Type := Base_Type (P_Type);
996ae0b0
RK
1732 end if;
1733 end Check_Dereference;
1734
1735 -------------------------
1736 -- Check_Discrete_Type --
1737 -------------------------
1738
1739 procedure Check_Discrete_Type is
1740 begin
1741 Check_Type;
1742
1743 if not Is_Discrete_Type (P_Type) then
822033eb 1744 Error_Attr_P ("prefix of % attribute must be discrete type");
996ae0b0
RK
1745 end if;
1746 end Check_Discrete_Type;
1747
1748 --------------
1749 -- Check_E0 --
1750 --------------
1751
1752 procedure Check_E0 is
1753 begin
1754 if Present (E1) then
1755 Unexpected_Argument (E1);
1756 end if;
1757 end Check_E0;
1758
1759 --------------
1760 -- Check_E1 --
1761 --------------
1762
1763 procedure Check_E1 is
1764 begin
1765 Check_Either_E0_Or_E1;
1766
1767 if No (E1) then
1768
1769 -- Special-case attributes that are functions and that appear as
1770 -- the prefix of another attribute. Error is posted on parent.
1771
1772 if Nkind (Parent (N)) = N_Attribute_Reference
b69cd36a
AC
1773 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1774 Name_Code_Address,
1775 Name_Access)
996ae0b0
RK
1776 then
1777 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1778 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1779 Set_Etype (Parent (N), Any_Type);
1780 Set_Entity (Parent (N), Any_Type);
1781 raise Bad_Attribute;
1782
1783 else
1784 Error_Attr ("missing argument for % attribute", N);
1785 end if;
1786 end if;
1787 end Check_E1;
1788
1789 --------------
1790 -- Check_E2 --
1791 --------------
1792
1793 procedure Check_E2 is
1794 begin
1795 if No (E1) then
1796 Error_Attr ("missing arguments for % attribute (2 required)", N);
1797 elsif No (E2) then
1798 Error_Attr ("missing argument for % attribute (2 required)", N);
1799 end if;
1800 end Check_E2;
1801
1802 ---------------------------
1803 -- Check_Either_E0_Or_E1 --
1804 ---------------------------
1805
1806 procedure Check_Either_E0_Or_E1 is
1807 begin
1808 if Present (E2) then
1809 Unexpected_Argument (E2);
1810 end if;
1811 end Check_Either_E0_Or_E1;
1812
1813 ----------------------
1814 -- Check_Enum_Image --
1815 ----------------------
1816
1817 procedure Check_Enum_Image is
1818 Lit : Entity_Id;
3e7302c3 1819
996ae0b0 1820 begin
3e7302c3
AC
1821 -- When an enumeration type appears in an attribute reference, all
1822 -- literals of the type are marked as referenced. This must only be
1823 -- done if the attribute reference appears in the current source.
1824 -- Otherwise the information on references may differ between a
1825 -- normal compilation and one that performs inlining.
1826
8097203f
AC
1827 if Is_Enumeration_Type (P_Base_Type)
1828 and then In_Extended_Main_Code_Unit (N)
1829 then
996ae0b0
RK
1830 Lit := First_Literal (P_Base_Type);
1831 while Present (Lit) loop
1832 Set_Referenced (Lit);
1833 Next_Literal (Lit);
1834 end loop;
1835 end if;
1836 end Check_Enum_Image;
1837
011f9d5d
AC
1838 ----------------------------
1839 -- Check_First_Last_Valid --
1840 ----------------------------
1841
1842 procedure Check_First_Last_Valid is
1843 begin
011f9d5d
AC
1844 Check_Discrete_Type;
1845
2a1b208c
RD
1846 -- Freeze the subtype now, so that the following test for predicates
1847 -- works (we set the predicates stuff up at freeze time)
1848
1849 Insert_Actions (N, Freeze_Entity (P_Type, P));
1850
1851 -- Now test for dynamic predicate
011f9d5d
AC
1852
1853 if Has_Predicates (P_Type)
60f908dd 1854 and then not (Has_Static_Predicate (P_Type))
011f9d5d
AC
1855 then
1856 Error_Attr_P
1857 ("prefix of % attribute may not have dynamic predicate");
1858 end if;
1859
2a1b208c
RD
1860 -- Check non-static subtype
1861
edab6088 1862 if not Is_OK_Static_Subtype (P_Type) then
2a1b208c
RD
1863 Error_Attr_P ("prefix of % attribute must be a static subtype");
1864 end if;
1865
1866 -- Test case for no values
1867
011f9d5d
AC
1868 if Expr_Value (Type_Low_Bound (P_Type)) >
1869 Expr_Value (Type_High_Bound (P_Type))
1870 or else (Has_Predicates (P_Type)
60f908dd
RD
1871 and then
1872 Is_Empty_List (Static_Discrete_Predicate (P_Type)))
011f9d5d
AC
1873 then
1874 Error_Attr_P
a905304c
AC
1875 ("prefix of % attribute must be subtype with at least one "
1876 & "value");
011f9d5d
AC
1877 end if;
1878 end Check_First_Last_Valid;
1879
996ae0b0
RK
1880 ----------------------------
1881 -- Check_Fixed_Point_Type --
1882 ----------------------------
1883
1884 procedure Check_Fixed_Point_Type is
1885 begin
1886 Check_Type;
1887
1888 if not Is_Fixed_Point_Type (P_Type) then
822033eb 1889 Error_Attr_P ("prefix of % attribute must be fixed point type");
996ae0b0
RK
1890 end if;
1891 end Check_Fixed_Point_Type;
1892
1893 ------------------------------
1894 -- Check_Fixed_Point_Type_0 --
1895 ------------------------------
1896
1897 procedure Check_Fixed_Point_Type_0 is
1898 begin
1899 Check_Fixed_Point_Type;
1900 Check_E0;
1901 end Check_Fixed_Point_Type_0;
1902
1903 -------------------------------
1904 -- Check_Floating_Point_Type --
1905 -------------------------------
1906
1907 procedure Check_Floating_Point_Type is
1908 begin
1909 Check_Type;
1910
1911 if not Is_Floating_Point_Type (P_Type) then
822033eb 1912 Error_Attr_P ("prefix of % attribute must be float type");
996ae0b0
RK
1913 end if;
1914 end Check_Floating_Point_Type;
1915
1916 ---------------------------------
1917 -- Check_Floating_Point_Type_0 --
1918 ---------------------------------
1919
1920 procedure Check_Floating_Point_Type_0 is
1921 begin
1922 Check_Floating_Point_Type;
1923 Check_E0;
1924 end Check_Floating_Point_Type_0;
1925
1926 ---------------------------------
1927 -- Check_Floating_Point_Type_1 --
1928 ---------------------------------
1929
1930 procedure Check_Floating_Point_Type_1 is
1931 begin
1932 Check_Floating_Point_Type;
1933 Check_E1;
1934 end Check_Floating_Point_Type_1;
1935
1936 ---------------------------------
1937 -- Check_Floating_Point_Type_2 --
1938 ---------------------------------
1939
1940 procedure Check_Floating_Point_Type_2 is
1941 begin
1942 Check_Floating_Point_Type;
1943 Check_E2;
1944 end Check_Floating_Point_Type_2;
1945
1946 ------------------------
1947 -- Check_Integer_Type --
1948 ------------------------
1949
1950 procedure Check_Integer_Type is
1951 begin
1952 Check_Type;
1953
1954 if not Is_Integer_Type (P_Type) then
822033eb 1955 Error_Attr_P ("prefix of % attribute must be integer type");
996ae0b0
RK
1956 end if;
1957 end Check_Integer_Type;
1958
5f3ab6fb
AC
1959 --------------------------------
1960 -- Check_Modular_Integer_Type --
1961 --------------------------------
1962
1963 procedure Check_Modular_Integer_Type is
1964 begin
1965 Check_Type;
1966
1967 if not Is_Modular_Integer_Type (P_Type) then
822033eb
HK
1968 Error_Attr_P
1969 ("prefix of % attribute must be modular integer type");
5f3ab6fb
AC
1970 end if;
1971 end Check_Modular_Integer_Type;
1972
21d27997
RD
1973 ------------------------
1974 -- Check_Not_CPP_Type --
1975 ------------------------
1976
1977 procedure Check_Not_CPP_Type is
1978 begin
1979 if Is_Tagged_Type (Etype (P))
1980 and then Convention (Etype (P)) = Convention_CPP
1981 and then Is_CPP_Class (Root_Type (Etype (P)))
1982 then
7052f54e
KP
1983 Error_Attr_P
1984 ("invalid use of % attribute with 'C'P'P tagged type");
21d27997
RD
1985 end if;
1986 end Check_Not_CPP_Type;
1987
996ae0b0
RK
1988 -------------------------------
1989 -- Check_Not_Incomplete_Type --
1990 -------------------------------
1991
1992 procedure Check_Not_Incomplete_Type is
d8db0bca
JM
1993 E : Entity_Id;
1994 Typ : Entity_Id;
1995
996ae0b0 1996 begin
d8db0bca
JM
1997 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1998 -- dereference we have to check wrong uses of incomplete types
1999 -- (other wrong uses are checked at their freezing point).
2000
22efcab7
AC
2001 -- In Ada 2012, incomplete types can appear in subprogram
2002 -- profiles, but formals with incomplete types cannot be the
2003 -- prefix of attributes.
2004
d8db0bca
JM
2005 -- Example 1: Limited-with
2006
2007 -- limited with Pkg;
2008 -- package P is
2009 -- type Acc is access Pkg.T;
2010 -- X : Acc;
2011 -- S : Integer := X.all'Size; -- ERROR
2012 -- end P;
2013
2014 -- Example 2: Tagged incomplete
2015
2016 -- type T is tagged;
2017 -- type Acc is access all T;
2018 -- X : Acc;
2019 -- S : constant Integer := X.all'Size; -- ERROR
2020 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2021
0791fbe9 2022 if Ada_Version >= Ada_2005
d8db0bca
JM
2023 and then Nkind (P) = N_Explicit_Dereference
2024 then
2025 E := P;
2026 while Nkind (E) = N_Explicit_Dereference loop
2027 E := Prefix (E);
2028 end loop;
2029
c0985d4e
HK
2030 Typ := Etype (E);
2031
7b56a91b 2032 if From_Limited_With (Typ) then
822033eb
HK
2033 Error_Attr_P
2034 ("prefix of % attribute cannot be an incomplete type");
d8db0bca 2035
22efcab7 2036 -- If the prefix is an access type check the designated type
c0985d4e 2037
22efcab7
AC
2038 elsif Is_Access_Type (Typ)
2039 and then Nkind (P) = N_Explicit_Dereference
2040 then
2041 Typ := Directly_Designated_Type (Typ);
2042 end if;
2043
2044 if Is_Class_Wide_Type (Typ) then
2045 Typ := Root_Type (Typ);
2046 end if;
2047
2048 -- A legal use of a shadow entity occurs only when the unit where
2049 -- the non-limited view resides is imported via a regular with
2050 -- clause in the current body. Such references to shadow entities
2051 -- may occur in subprogram formals.
2052
2053 if Is_Incomplete_Type (Typ)
2054 and then From_Limited_With (Typ)
2055 and then Present (Non_Limited_View (Typ))
2056 and then Is_Legal_Shadow_Entity_In_Body (Typ)
2057 then
2058 Typ := Non_Limited_View (Typ);
2059 end if;
c0985d4e 2060
22efcab7
AC
2061 -- If still incomplete, it can be a local incomplete type, or a
2062 -- limited view whose scope is also a limited view.
c0985d4e 2063
22efcab7
AC
2064 if Ekind (Typ) = E_Incomplete_Type then
2065 if not From_Limited_With (Typ)
2066 and then No (Full_View (Typ))
c0985d4e 2067 then
22efcab7
AC
2068 Error_Attr_P
2069 ("prefix of % attribute cannot be an incomplete type");
d8db0bca 2070
22efcab7
AC
2071 -- The limited view may be available indirectly through
2072 -- an intermediate unit. If the non-limited view is available
2073 -- the attribute reference is legal.
2074
2075 elsif From_Limited_With (Typ)
2076 and then
2077 (No (Non_Limited_View (Typ))
2078 or else Is_Incomplete_Type (Non_Limited_View (Typ)))
d8db0bca 2079 then
822033eb
HK
2080 Error_Attr_P
2081 ("prefix of % attribute cannot be an incomplete type");
d8db0bca
JM
2082 end if;
2083 end if;
22efcab7
AC
2084
2085 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2086 -- legally applies.
2087
2088 elsif Is_Entity_Name (P)
2089 and then Is_Formal (Entity (P))
2090 and then Is_Incomplete_Type (Etype (Etype (P)))
2091 then
2092 Error_Attr_P
2093 ("prefix of % attribute cannot be an incomplete type");
d8db0bca
JM
2094 end if;
2095
996ae0b0
RK
2096 if not Is_Entity_Name (P)
2097 or else not Is_Type (Entity (P))
21d27997 2098 or else In_Spec_Expression
996ae0b0
RK
2099 then
2100 return;
996ae0b0
RK
2101 else
2102 Check_Fully_Declared (P_Type, P);
2103 end if;
2104 end Check_Not_Incomplete_Type;
2105
2106 ----------------------------
2107 -- Check_Object_Reference --
2108 ----------------------------
2109
2110 procedure Check_Object_Reference (P : Node_Id) is
2111 Rtyp : Entity_Id;
2112
2113 begin
2114 -- If we need an object, and we have a prefix that is the name of
2115 -- a function entity, convert it into a function call.
2116
2117 if Is_Entity_Name (P)
2118 and then Ekind (Entity (P)) = E_Function
2119 then
2120 Rtyp := Etype (Entity (P));
2121
2122 Rewrite (P,
2123 Make_Function_Call (Sloc (P),
2124 Name => Relocate_Node (P)));
2125
2126 Analyze_And_Resolve (P, Rtyp);
2127
2128 -- Otherwise we must have an object reference
2129
2130 elsif not Is_Object_Reference (P) then
822033eb 2131 Error_Attr_P ("prefix of % attribute must be object");
996ae0b0
RK
2132 end if;
2133 end Check_Object_Reference;
2134
54838d1f
AC
2135 ----------------------------
2136 -- Check_PolyORB_Attribute --
2137 ----------------------------
2138
2139 procedure Check_PolyORB_Attribute is
2140 begin
2141 Validate_Non_Static_Attribute_Function_Call;
2142
2143 Check_Type;
2144 Check_Not_CPP_Type;
2145
2146 if Get_PCS_Name /= Name_PolyORB_DSA then
2147 Error_Attr
2148 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
2149 end if;
2150 end Check_PolyORB_Attribute;
2151
996ae0b0
RK
2152 ------------------------
2153 -- Check_Program_Unit --
2154 ------------------------
2155
2156 procedure Check_Program_Unit is
2157 begin
2158 if Is_Entity_Name (P) then
2159 declare
2160 K : constant Entity_Kind := Ekind (Entity (P));
2161 T : constant Entity_Id := Etype (Entity (P));
2162
2163 begin
2164 if K in Subprogram_Kind
2165 or else K in Task_Kind
2166 or else K in Protected_Kind
2167 or else K = E_Package
2168 or else K in Generic_Unit_Kind
2169 or else (K = E_Variable
2170 and then
2171 (Is_Task_Type (T)
2172 or else
2173 Is_Protected_Type (T)))
2174 then
2175 return;
2176 end if;
2177 end;
2178 end if;
2179
822033eb 2180 Error_Attr_P ("prefix of % attribute must be program unit");
996ae0b0
RK
2181 end Check_Program_Unit;
2182
2183 ---------------------
2184 -- Check_Real_Type --
2185 ---------------------
2186
2187 procedure Check_Real_Type is
2188 begin
2189 Check_Type;
2190
2191 if not Is_Real_Type (P_Type) then
822033eb 2192 Error_Attr_P ("prefix of % attribute must be real type");
996ae0b0
RK
2193 end if;
2194 end Check_Real_Type;
2195
2196 -----------------------
2197 -- Check_Scalar_Type --
2198 -----------------------
2199
2200 procedure Check_Scalar_Type is
2201 begin
2202 Check_Type;
2203
2204 if not Is_Scalar_Type (P_Type) then
822033eb 2205 Error_Attr_P ("prefix of % attribute must be scalar type");
996ae0b0
RK
2206 end if;
2207 end Check_Scalar_Type;
2208
e917aec2 2209 ------------------------------------------
ce5ba43a 2210 -- Check_SPARK_05_Restriction_On_Attribute --
e917aec2
RD
2211 ------------------------------------------
2212
ce5ba43a 2213 procedure Check_SPARK_05_Restriction_On_Attribute is
e917aec2
RD
2214 begin
2215 Error_Msg_Name_1 := Aname;
ce5ba43a
AC
2216 Check_SPARK_05_Restriction ("attribute % is not allowed", P);
2217 end Check_SPARK_05_Restriction_On_Attribute;
e917aec2 2218
996ae0b0
RK
2219 ---------------------------
2220 -- Check_Standard_Prefix --
2221 ---------------------------
2222
2223 procedure Check_Standard_Prefix is
2224 begin
2225 Check_E0;
2226
2cbac6c6 2227 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_Standard then
996ae0b0
RK
2228 Error_Attr ("only allowed prefix for % attribute is Standard", P);
2229 end if;
996ae0b0
RK
2230 end Check_Standard_Prefix;
2231
2232 ----------------------------
2233 -- Check_Stream_Attribute --
2234 ----------------------------
2235
fbf5a39b 2236 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
996ae0b0
RK
2237 Etyp : Entity_Id;
2238 Btyp : Entity_Id;
468c6c8a 2239
7052f54e 2240 In_Shared_Var_Procs : Boolean;
2cbac6c6
AC
2241 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2242 -- For this runtime package (always compiled in GNAT mode), we allow
2243 -- stream attributes references for limited types for the case where
2244 -- shared passive objects are implemented using stream attributes,
2245 -- which is the default in GNAT's persistent storage implementation.
7052f54e 2246
996ae0b0
RK
2247 begin
2248 Validate_Non_Static_Attribute_Function_Call;
2249
2250 -- With the exception of 'Input, Stream attributes are procedures,
2251 -- and can only appear at the position of procedure calls. We check
2252 -- for this here, before they are rewritten, to give a more precise
2253 -- diagnostic.
2254
fbf5a39b 2255 if Nam = TSS_Stream_Input then
996ae0b0
RK
2256 null;
2257
2258 elsif Is_List_Member (N)
e10dab7f
JM
2259 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
2260 N_Aggregate)
996ae0b0
RK
2261 then
2262 null;
2263
2264 else
2265 Error_Attr
fbf5a39b 2266 ("invalid context for attribute%, which is a procedure", N);
996ae0b0
RK
2267 end if;
2268
2269 Check_Type;
2270 Btyp := Implementation_Base_Type (P_Type);
2271
2272 -- Stream attributes not allowed on limited types unless the
d2d3604c
TQ
2273 -- attribute reference was generated by the expander (in which
2274 -- case the underlying type will be used, as described in Sinfo),
2275 -- or the attribute was specified explicitly for the type itself
edd63e9b
ES
2276 -- or one of its ancestors (taking visibility rules into account if
2277 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2278 -- (with no visibility restriction).
996ae0b0 2279
7052f54e
KP
2280 declare
2281 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
2282 begin
2283 if Present (Gen_Body) then
2284 In_Shared_Var_Procs :=
2285 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
2286 else
2287 In_Shared_Var_Procs := False;
2288 end if;
2289 end;
2290
2291 if (Comes_From_Source (N)
2292 and then not (In_Shared_Var_Procs or In_Instance))
edd63e9b 2293 and then not Stream_Attribute_Available (P_Type, Nam)
1d571f3b 2294 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
996ae0b0 2295 then
fbf5a39b 2296 Error_Msg_Name_1 := Aname;
edd63e9b
ES
2297
2298 if Is_Limited_Type (P_Type) then
2299 Error_Msg_NE
2300 ("limited type& has no% attribute", P, P_Type);
2301 Explain_Limited_Type (P_Type, P);
2302 else
2303 Error_Msg_NE
2304 ("attribute% for type& is not available", P, P_Type);
2305 end if;
fbf5a39b 2306 end if;
996ae0b0 2307
49d41397
RD
2308 -- Check for no stream operations allowed from No_Tagged_Streams
2309
2310 if Is_Tagged_Type (P_Type)
2311 and then Present (No_Tagged_Streams_Pragma (P_Type))
2312 then
2313 Error_Msg_Sloc := Sloc (No_Tagged_Streams_Pragma (P_Type));
2314 Error_Msg_NE
2315 ("no stream operations for & (No_Tagged_Streams #)", N, P_Type);
2316 return;
2317 end if;
2318
95b89f1b
AC
2319 -- Check restriction violations
2320
93bcda23
AC
2321 -- First check the No_Streams restriction, which prohibits the use
2322 -- of explicit stream attributes in the source program. We do not
2323 -- prevent the occurrence of stream attributes in generated code,
2324 -- for instance those generated implicitly for dispatching purposes.
2325
2326 if Comes_From_Source (N) then
2327 Check_Restriction (No_Streams, P);
2328 end if;
2329
276e7ed0
AC
2330 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2331 -- it is illegal to use a predefined elementary type stream attribute
2332 -- either by itself, or more importantly as part of the attribute
beaa97ab
AC
2333 -- subprogram for a composite type. However, if the broader
2334 -- restriction No_Streams is active, stream operations are not
2335 -- generated, and there is no error.
276e7ed0 2336
beaa97ab
AC
2337 if Restriction_Active (No_Default_Stream_Attributes)
2338 and then not Restriction_Active (No_Streams)
2339 then
276e7ed0
AC
2340 declare
2341 T : Entity_Id;
9aff36e9 2342
276e7ed0
AC
2343 begin
2344 if Nam = TSS_Stream_Input
9aff36e9
RD
2345 or else
2346 Nam = TSS_Stream_Read
276e7ed0
AC
2347 then
2348 T :=
2349 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
2350 else
2351 T :=
2352 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
2353 end if;
2354
2355 if Present (T) then
2356 Check_Restriction (No_Default_Stream_Attributes, N);
2357
2358 Error_Msg_NE
2359 ("missing user-defined Stream Read or Write for type&",
2360 N, T);
2361 if not Is_Elementary_Type (P_Type) then
2362 Error_Msg_NE
2363 ("\which is a component of type&", N, P_Type);
2364 end if;
2365 end if;
2366 end;
2367 end if;
2368
93bcda23 2369 -- Check special case of Exception_Id and Exception_Occurrence which
308e6f3a 2370 -- are not allowed for restriction No_Exception_Registration.
fbf5a39b 2371
273adcdf
AC
2372 if Restriction_Check_Required (No_Exception_Registration)
2373 and then (Is_RTE (P_Type, RE_Exception_Id)
2374 or else
2375 Is_RTE (P_Type, RE_Exception_Occurrence))
fbf5a39b
AC
2376 then
2377 Check_Restriction (No_Exception_Registration, P);
996ae0b0
RK
2378 end if;
2379
2380 -- Here we must check that the first argument is an access type
2381 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2382
2383 Analyze_And_Resolve (E1);
2384 Etyp := Etype (E1);
2385
2386 -- Note: the double call to Root_Type here is needed because the
2387 -- root type of a class-wide type is the corresponding type (e.g.
fea9e956 2388 -- X for X'Class, and we really want to go to the root.)
996ae0b0
RK
2389
2390 if not Is_Access_Type (Etyp)
2391 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
2392 RTE (RE_Root_Stream_Type)
2393 then
2394 Error_Attr
2395 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
2396 end if;
2397
2398 -- Check that the second argument is of the right type if there is
2399 -- one (the Input attribute has only one argument so this is skipped)
2400
2401 if Present (E2) then
2402 Analyze (E2);
2403
fbf5a39b 2404 if Nam = TSS_Stream_Read
996ae0b0
RK
2405 and then not Is_OK_Variable_For_Out_Formal (E2)
2406 then
2407 Error_Attr
2408 ("second argument of % attribute must be a variable", E2);
2409 end if;
2410
2411 Resolve (E2, P_Type);
2412 end if;
21d27997
RD
2413
2414 Check_Not_CPP_Type;
996ae0b0
RK
2415 end Check_Stream_Attribute;
2416
f7ea2603
RD
2417 -------------------------
2418 -- Check_System_Prefix --
2419 -------------------------
2420
2421 procedure Check_System_Prefix is
2422 begin
2423 if Nkind (P) /= N_Identifier or else Chars (P) /= Name_System then
2424 Error_Attr ("only allowed prefix for % attribute is System", P);
2425 end if;
2426 end Check_System_Prefix;
2427
996ae0b0
RK
2428 -----------------------
2429 -- Check_Task_Prefix --
2430 -----------------------
2431
2432 procedure Check_Task_Prefix is
2433 begin
2434 Analyze (P);
2435
65f01153
RD
2436 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2437 -- task interface class-wide types.
2438
996ae0b0
RK
2439 if Is_Task_Type (Etype (P))
2440 or else (Is_Access_Type (Etype (P))
65f01153 2441 and then Is_Task_Type (Designated_Type (Etype (P))))
0791fbe9 2442 or else (Ada_Version >= Ada_2005
65f01153
RD
2443 and then Ekind (Etype (P)) = E_Class_Wide_Type
2444 and then Is_Interface (Etype (P))
2445 and then Is_Task_Interface (Etype (P)))
996ae0b0 2446 then
fbf5a39b 2447 Resolve (P);
65f01153 2448
996ae0b0 2449 else
0791fbe9 2450 if Ada_Version >= Ada_2005 then
822033eb
HK
2451 Error_Attr_P
2452 ("prefix of % attribute must be a task or a task " &
2453 "interface class-wide object");
65f01153
RD
2454
2455 else
822033eb 2456 Error_Attr_P ("prefix of % attribute must be a task");
65f01153 2457 end if;
996ae0b0
RK
2458 end if;
2459 end Check_Task_Prefix;
2460
2461 ----------------
2462 -- Check_Type --
2463 ----------------
2464
2465 -- The possibilities are an entity name denoting a type, or an
2466 -- attribute reference that denotes a type (Base or Class). If
2467 -- the type is incomplete, replace it with its full view.
2468
2469 procedure Check_Type is
2470 begin
2471 if not Is_Entity_Name (P)
2472 or else not Is_Type (Entity (P))
2473 then
822033eb 2474 Error_Attr_P ("prefix of % attribute must be a type");
996ae0b0 2475
2d14501c
ST
2476 elsif Is_Protected_Self_Reference (P) then
2477 Error_Attr_P
ae8c7d87
RD
2478 ("prefix of % attribute denotes current instance "
2479 & "(RM 9.4(21/2))");
2d14501c 2480
996ae0b0
RK
2481 elsif Ekind (Entity (P)) = E_Incomplete_Type
2482 and then Present (Full_View (Entity (P)))
2483 then
2484 P_Type := Full_View (Entity (P));
2485 Set_Entity (P, P_Type);
2486 end if;
2487 end Check_Type;
2488
2489 ---------------------
2490 -- Check_Unit_Name --
2491 ---------------------
2492
2493 procedure Check_Unit_Name (Nod : Node_Id) is
2494 begin
2495 if Nkind (Nod) = N_Identifier then
2496 return;
2497
2c1b72d7 2498 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
996ae0b0
RK
2499 Check_Unit_Name (Prefix (Nod));
2500
2501 if Nkind (Selector_Name (Nod)) = N_Identifier then
2502 return;
2503 end if;
2504 end if;
2505
2506 Error_Attr ("argument for % attribute must be unit name", P);
2507 end Check_Unit_Name;
2508
2509 ----------------
2510 -- Error_Attr --
2511 ----------------
2512
fbf5a39b 2513 procedure Error_Attr is
996ae0b0 2514 begin
996ae0b0
RK
2515 Set_Etype (N, Any_Type);
2516 Set_Entity (N, Any_Type);
2517 raise Bad_Attribute;
2518 end Error_Attr;
2519
fbf5a39b
AC
2520 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
2521 begin
2522 Error_Msg_Name_1 := Aname;
2523 Error_Msg_N (Msg, Error_Node);
2524 Error_Attr;
2525 end Error_Attr;
2526
822033eb
HK
2527 ------------------
2528 -- Error_Attr_P --
2529 ------------------
2530
2531 procedure Error_Attr_P (Msg : String) is
2532 begin
2533 Error_Msg_Name_1 := Aname;
2534 Error_Msg_F (Msg, P);
2535 Error_Attr;
2536 end Error_Attr_P;
2537
996ae0b0
RK
2538 ----------------------------
2539 -- Legal_Formal_Attribute --
2540 ----------------------------
2541
2542 procedure Legal_Formal_Attribute is
2543 begin
2544 Check_E0;
2545
2546 if not Is_Entity_Name (P)
2547 or else not Is_Type (Entity (P))
2548 then
822033eb 2549 Error_Attr_P ("prefix of % attribute must be generic type");
996ae0b0
RK
2550
2551 elsif Is_Generic_Actual_Type (Entity (P))
efdfd311
AC
2552 or else In_Instance
2553 or else In_Inlined_Body
996ae0b0
RK
2554 then
2555 null;
2556
2557 elsif Is_Generic_Type (Entity (P)) then
83496138 2558 if Is_Definite_Subtype (Entity (P)) then
822033eb
HK
2559 Error_Attr_P
2560 ("prefix of % attribute must be indefinite generic type");
996ae0b0
RK
2561 end if;
2562
2563 else
822033eb
HK
2564 Error_Attr_P
2565 ("prefix of % attribute must be indefinite generic type");
996ae0b0
RK
2566 end if;
2567
2568 Set_Etype (N, Standard_Boolean);
2569 end Legal_Formal_Attribute;
2570
85d6bf87
AC
2571 ---------------------------------------------------------------
2572 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2573 ---------------------------------------------------------------
2574
2575 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements is
2576 begin
2577 Check_E0;
2578 Check_Type;
2579 Check_Not_Incomplete_Type;
2580 Set_Etype (N, Universal_Integer);
2581 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
2582
2583 -------------
2584 -- Min_Max --
2585 -------------
2586
2587 procedure Min_Max is
2588 begin
2589 Check_E2;
2590 Check_Scalar_Type;
2591 Resolve (E1, P_Base_Type);
2592 Resolve (E2, P_Base_Type);
2593 Set_Etype (N, P_Base_Type);
2594
2595 -- Check for comparison on unordered enumeration type
2596
2597 if Bad_Unordered_Enumeration_Reference (N, P_Base_Type) then
2598 Error_Msg_Sloc := Sloc (P_Base_Type);
2599 Error_Msg_NE
2600 ("comparison on unordered enumeration type& declared#?U?",
2601 N, P_Base_Type);
2602 end if;
2603 end Min_Max;
2604
996ae0b0
RK
2605 ------------------------
2606 -- Standard_Attribute --
2607 ------------------------
2608
2609 procedure Standard_Attribute (Val : Int) is
2610 begin
2611 Check_Standard_Prefix;
fbf5a39b 2612 Rewrite (N, Make_Integer_Literal (Loc, Val));
996ae0b0 2613 Analyze (N);
edab6088 2614 Set_Is_Static_Expression (N, True);
996ae0b0
RK
2615 end Standard_Attribute;
2616
96e90ac1
RD
2617 --------------------
2618 -- Uneval_Old_Msg --
2619 --------------------
2620
2621 procedure Uneval_Old_Msg is
effdbb7d 2622 Uneval_Old_Setting : Character;
1773d80b
AC
2623 Prag : Node_Id;
2624
96e90ac1 2625 begin
1773d80b
AC
2626 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2627 -- N_Aspect_Specification node that corresponds to the attribute.
2628
2629 -- First find the pragma in which we appear (note that at this stage,
2630 -- even if we appeared originally within an aspect specification, we
2631 -- are now within the corresponding pragma).
2632
2633 Prag := N;
2634 loop
2635 Prag := Parent (Prag);
2636 exit when No (Prag) or else Nkind (Prag) = N_Pragma;
2637 end loop;
2638
effdbb7d
AC
2639 if Present (Prag) then
2640 if Uneval_Old_Accept (Prag) then
1773d80b 2641 Uneval_Old_Setting := 'A';
effdbb7d 2642 elsif Uneval_Old_Warn (Prag) then
1773d80b
AC
2643 Uneval_Old_Setting := 'W';
2644 else
2645 Uneval_Old_Setting := 'E';
2646 end if;
effdbb7d
AC
2647
2648 -- If we did not find the pragma, that's odd, just use the setting
2649 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2650
2651 else
2652 Uneval_Old_Setting := Opt.Uneval_Old;
1773d80b
AC
2653 end if;
2654
2655 -- Processing depends on the setting of Uneval_Old
2656
2657 case Uneval_Old_Setting is
96e90ac1
RD
2658 when 'E' =>
2659 Error_Attr_P
2660 ("prefix of attribute % that is potentially "
2661 & "unevaluated must denote an entity");
2662
2663 when 'W' =>
b8b2d982
AC
2664 Error_Msg_Name_1 := Aname;
2665 Error_Msg_F
96e90ac1 2666 ("??prefix of attribute % appears in potentially "
b8b2d982 2667 & "unevaluated context, exception may be raised", P);
96e90ac1
RD
2668
2669 when 'A' =>
2670 null;
2671
2672 when others =>
2673 raise Program_Error;
2674 end case;
2675 end Uneval_Old_Msg;
2676
996ae0b0
RK
2677 -------------------------
2678 -- Unexpected Argument --
2679 -------------------------
2680
2681 procedure Unexpected_Argument (En : Node_Id) is
2682 begin
2683 Error_Attr ("unexpected argument for % attribute", En);
2684 end Unexpected_Argument;
2685
2686 -------------------------------------------------
2687 -- Validate_Non_Static_Attribute_Function_Call --
2688 -------------------------------------------------
2689
2690 -- This function should be moved to Sem_Dist ???
2691
2692 procedure Validate_Non_Static_Attribute_Function_Call is
2693 begin
2694 if In_Preelaborated_Unit
2695 and then not In_Subprogram_Or_Concurrent_Unit
2696 then
fbf5a39b
AC
2697 Flag_Non_Static_Expr
2698 ("non-static function call in preelaborated unit!", N);
996ae0b0
RK
2699 end if;
2700 end Validate_Non_Static_Attribute_Function_Call;
2701
cb3d8731 2702 -- Start of processing for Analyze_Attribute
996ae0b0
RK
2703
2704 begin
58ba2415
HK
2705 -- Immediate return if unrecognized attribute (already diagnosed by
2706 -- parser, so there is nothing more that we need to do).
996ae0b0
RK
2707
2708 if not Is_Attribute_Name (Aname) then
2709 raise Bad_Attribute;
2710 end if;
2711
58ba2415
HK
2712 Check_Restriction_No_Use_Of_Attribute (N);
2713
822033eb 2714 -- Deal with Ada 83 issues
996ae0b0 2715
fbf5a39b
AC
2716 if Comes_From_Source (N) then
2717 if not Attribute_83 (Attr_Id) then
0ab80019 2718 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
fbf5a39b 2719 Error_Msg_Name_1 := Aname;
324ac540 2720 Error_Msg_N ("(Ada 83) attribute% is not standard??", N);
fbf5a39b 2721 end if;
996ae0b0 2722
fbf5a39b
AC
2723 if Attribute_Impl_Def (Attr_Id) then
2724 Check_Restriction (No_Implementation_Attributes, N);
2725 end if;
996ae0b0
RK
2726 end if;
2727 end if;
2728
edab6088 2729 -- Deal with Ada 2005 attributes that are implementation attributes
155562cb
AC
2730 -- because they appear in a version of Ada before Ada 2005, and
2731 -- similarly for Ada 2012 attributes appearing in an earlier version.
822033eb 2732
155562cb
AC
2733 if (Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005)
2734 or else
2735 (Attribute_12 (Attr_Id) and then Ada_Version < Ada_2012)
2736 then
822033eb
HK
2737 Check_Restriction (No_Implementation_Attributes, N);
2738 end if;
2739
996ae0b0
RK
2740 -- Remote access to subprogram type access attribute reference needs
2741 -- unanalyzed copy for tree transformation. The analyzed copy is used
2742 -- for its semantic information (whether prefix is a remote subprogram
2743 -- name), the unanalyzed copy is used to construct new subtree rooted
f529bac5 2744 -- with N_Aggregate which represents a fat pointer aggregate.
996ae0b0
RK
2745
2746 if Aname = Name_Access then
fbf5a39b 2747 Discard_Node (Copy_Separate_Tree (N));
996ae0b0
RK
2748 end if;
2749
2750 -- Analyze prefix and exit if error in analysis. If the prefix is an
442ade9d
RD
2751 -- incomplete type, use full view if available. Note that there are
2752 -- some attributes for which we do not analyze the prefix, since the
d2b4b3da 2753 -- prefix is not a normal name, or else needs special handling.
996ae0b0 2754
2cbac6c6
AC
2755 if Aname /= Name_Elab_Body and then
2756 Aname /= Name_Elab_Spec and then
2757 Aname /= Name_Elab_Subp_Body and then
2cbac6c6 2758 Aname /= Name_Enabled and then
d2b4b3da 2759 Aname /= Name_Old
996ae0b0
RK
2760 then
2761 Analyze (P);
2762 P_Type := Etype (P);
2763
2764 if Is_Entity_Name (P)
2765 and then Present (Entity (P))
2766 and then Is_Type (Entity (P))
996ae0b0 2767 then
b8dc622e
JM
2768 if Ekind (Entity (P)) = E_Incomplete_Type then
2769 P_Type := Get_Full_View (P_Type);
2770 Set_Entity (P, P_Type);
2771 Set_Etype (P, P_Type);
2772
2773 elsif Entity (P) = Current_Scope
2774 and then Is_Record_Type (Entity (P))
2775 then
b8dc622e
JM
2776 -- Use of current instance within the type. Verify that if the
2777 -- attribute appears within a constraint, it yields an access
2778 -- type, other uses are illegal.
2779
2780 declare
2781 Par : Node_Id;
2782
2783 begin
2784 Par := Parent (N);
2785 while Present (Par)
2786 and then Nkind (Parent (Par)) /= N_Component_Definition
2787 loop
2788 Par := Parent (Par);
2789 end loop;
2790
2791 if Present (Par)
2792 and then Nkind (Par) = N_Subtype_Indication
2793 then
2794 if Attr_Id /= Attribute_Access
2795 and then Attr_Id /= Attribute_Unchecked_Access
2796 and then Attr_Id /= Attribute_Unrestricted_Access
2797 then
2798 Error_Msg_N
21db8699
RD
2799 ("in a constraint the current instance can only "
2800 & "be used with an access attribute", N);
b8dc622e
JM
2801 end if;
2802 end if;
2803 end;
2804 end if;
996ae0b0
RK
2805 end if;
2806
2807 if P_Type = Any_Type then
2808 raise Bad_Attribute;
2809 end if;
2810
2811 P_Base_Type := Base_Type (P_Type);
996ae0b0
RK
2812 end if;
2813
2814 -- Analyze expressions that may be present, exiting if an error occurs
2815
2816 if No (Exprs) then
2817 E1 := Empty;
2818 E2 := Empty;
2819
2820 else
2821 E1 := First (Exprs);
996ae0b0 2822
2cbac6c6
AC
2823 -- Skip analysis for case of Restriction_Set, we do not expect
2824 -- the argument to be analyzed in this case.
457b6274 2825
2cbac6c6
AC
2826 if Aname /= Name_Restriction_Set then
2827 Analyze (E1);
2828
2829 -- Check for missing/bad expression (result of previous error)
2830
2831 if No (E1) or else Etype (E1) = Any_Type then
2832 raise Bad_Attribute;
2833 end if;
996ae0b0
RK
2834 end if;
2835
2836 E2 := Next (E1);
2837
2838 if Present (E2) then
2839 Analyze (E2);
2840
2841 if Etype (E2) = Any_Type then
2842 raise Bad_Attribute;
2843 end if;
2844
2845 if Present (Next (E2)) then
2846 Unexpected_Argument (Next (E2));
2847 end if;
2848 end if;
2849 end if;
2850
0083dd66
AC
2851 -- Cases where prefix must be resolvable by itself
2852
446ebdbb 2853 if Is_Overloaded (P)
edd63e9b
ES
2854 and then Aname /= Name_Access
2855 and then Aname /= Name_Address
2856 and then Aname /= Name_Code_Address
21d27997 2857 and then Aname /= Name_Result
edd63e9b
ES
2858 and then Aname /= Name_Unchecked_Access
2859 then
890f1954 2860 -- The prefix must be resolvable by itself, without reference to the
446ebdbb
AC
2861 -- attribute. One case that requires special handling is a prefix
2862 -- that is a function name, where one interpretation may be a
2863 -- parameterless call. Entry attributes are handled specially below.
edd63e9b 2864
446ebdbb 2865 if Is_Entity_Name (P)
ba0c6e47 2866 and then not Nam_In (Aname, Name_Count, Name_Caller)
edd63e9b 2867 then
446ebdbb
AC
2868 Check_Parameterless_Call (P);
2869 end if;
edd63e9b 2870
890f1954 2871 if Is_Overloaded (P) then
edd63e9b 2872
890f1954 2873 -- Ada 2005 (AI-345): Since protected and task types have
ba0c6e47
RD
2874 -- primitive entry wrappers, the attributes Count, and Caller
2875 -- require a context check
edd63e9b 2876
ba0c6e47 2877 if Nam_In (Aname, Name_Count, Name_Caller) then
890f1954
RD
2878 declare
2879 Count : Natural := 0;
2880 I : Interp_Index;
2881 It : Interp;
446ebdbb 2882
890f1954
RD
2883 begin
2884 Get_First_Interp (P, I, It);
2885 while Present (It.Nam) loop
2886 if Comes_From_Source (It.Nam) then
2887 Count := Count + 1;
446ebdbb 2888 else
890f1954 2889 Remove_Interp (I);
446ebdbb 2890 end if;
edd63e9b 2891
890f1954
RD
2892 Get_Next_Interp (I, It);
2893 end loop;
2894
2895 if Count > 1 then
2896 Error_Attr ("ambiguous prefix for % attribute", P);
2897 else
2898 Set_Is_Overloaded (P, False);
2899 end if;
2900 end;
2901
2902 else
2903 Error_Attr ("ambiguous prefix for % attribute", P);
2904 end if;
edd63e9b 2905 end if;
996ae0b0
RK
2906 end if;
2907
2ba431e5
YM
2908 -- In SPARK, attributes of private types are only allowed if the full
2909 -- type declaration is visible.
8ed68165 2910
446ebdbb
AC
2911 -- Note: the check for Present (Entity (P)) defends against some error
2912 -- conditions where the Entity field is not set.
2913
2914 if Is_Entity_Name (P) and then Present (Entity (P))
8ed68165
AC
2915 and then Is_Type (Entity (P))
2916 and then Is_Private_Type (P_Type)
2917 and then not In_Open_Scopes (Scope (P_Type))
2918 and then not In_Spec_Expression
2919 then
ce5ba43a 2920 Check_SPARK_05_Restriction ("invisible attribute of type", N);
8ed68165
AC
2921 end if;
2922
996ae0b0
RK
2923 -- Remaining processing depends on attribute
2924
2925 case Attr_Id is
2926
de94a7e7
AC
2927 -- Attributes related to Ada 2012 iterators. Attribute specifications
2928 -- exist for these, but they cannot be queried.
2929
2930 when Attribute_Constant_Indexing |
2931 Attribute_Default_Iterator |
2932 Attribute_Implicit_Dereference |
2933 Attribute_Iterator_Element |
dd2bf554 2934 Attribute_Iterable |
de94a7e7
AC
2935 Attribute_Variable_Indexing =>
2936 Error_Msg_N ("illegal attribute", N);
0da80d7d 2937
d27f3ff4
AC
2938 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2939 -- were already rejected by the parser. Thus they shouldn't appear here.
b98e2969 2940
c1107fa3 2941 when Internal_Attribute_Id =>
d48f3dca 2942 raise Program_Error;
b98e2969 2943
996ae0b0
RK
2944 ------------------
2945 -- Abort_Signal --
2946 ------------------
2947
2948 when Attribute_Abort_Signal =>
2949 Check_Standard_Prefix;
e4494292 2950 Rewrite (N, New_Occurrence_Of (Stand.Abort_Signal, Loc));
996ae0b0
RK
2951 Analyze (N);
2952
2953 ------------
2954 -- Access --
2955 ------------
2956
2957 when Attribute_Access =>
fbf5a39b 2958 Analyze_Access_Attribute;
22efcab7 2959 Check_Not_Incomplete_Type;
996ae0b0
RK
2960
2961 -------------
2962 -- Address --
2963 -------------
2964
2965 when Attribute_Address =>
2966 Check_E0;
3cd4a210 2967 Address_Checks;
22efcab7 2968 Check_Not_Incomplete_Type;
996ae0b0
RK
2969 Set_Etype (N, RTE (RE_Address));
2970
2971 ------------------
2972 -- Address_Size --
2973 ------------------
2974
2975 when Attribute_Address_Size =>
2976 Standard_Attribute (System_Address_Size);
2977
2978 --------------
2979 -- Adjacent --
2980 --------------
2981
2982 when Attribute_Adjacent =>
2983 Check_Floating_Point_Type_2;
2984 Set_Etype (N, P_Base_Type);
2985 Resolve (E1, P_Base_Type);
2986 Resolve (E2, P_Base_Type);
2987
2988 ---------
2989 -- Aft --
2990 ---------
2991
2992 when Attribute_Aft =>
2993 Check_Fixed_Point_Type_0;
2994 Set_Etype (N, Universal_Integer);
2995
2996 ---------------
2997 -- Alignment --
2998 ---------------
2999
3000 when Attribute_Alignment =>
3001
3002 -- Don't we need more checking here, cf Size ???
3003
3004 Check_E0;
3005 Check_Not_Incomplete_Type;
21d27997 3006 Check_Not_CPP_Type;
996ae0b0
RK
3007 Set_Etype (N, Universal_Integer);
3008
3009 ---------------
3010 -- Asm_Input --
3011 ---------------
3012
3013 when Attribute_Asm_Input =>
3014 Check_Asm_Attribute;
7e4680c1
EB
3015
3016 -- The back-end may need to take the address of E2
3017
3018 if Is_Entity_Name (E2) then
3019 Set_Address_Taken (Entity (E2));
3020 end if;
3021
996ae0b0
RK
3022 Set_Etype (N, RTE (RE_Asm_Input_Operand));
3023
3024 ----------------
3025 -- Asm_Output --
3026 ----------------
3027
3028 when Attribute_Asm_Output =>
3029 Check_Asm_Attribute;
3030
3031 if Etype (E2) = Any_Type then
3032 return;
3033
3034 elsif Aname = Name_Asm_Output then
3035 if not Is_Variable (E2) then
3036 Error_Attr
3037 ("second argument for Asm_Output is not variable", E2);
3038 end if;
3039 end if;
3040
21d27997 3041 Note_Possible_Modification (E2, Sure => True);
7e4680c1
EB
3042
3043 -- The back-end may need to take the address of E2
3044
3045 if Is_Entity_Name (E2) then
3046 Set_Address_Taken (Entity (E2));
3047 end if;
3048
996ae0b0
RK
3049 Set_Etype (N, RTE (RE_Asm_Output_Operand));
3050
0ebc109a
VP
3051 -----------------------------
3052 -- Atomic_Always_Lock_Free --
3053 -----------------------------
3054
3055 when Attribute_Atomic_Always_Lock_Free =>
3056 Check_E0;
3057 Check_Type;
3058 Set_Etype (N, Standard_Boolean);
3059
996ae0b0
RK
3060 ----------
3061 -- Base --
3062 ----------
3063
fbf5a39b
AC
3064 -- Note: when the base attribute appears in the context of a subtype
3065 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3066 -- the following circuit.
3067
996ae0b0
RK
3068 when Attribute_Base => Base : declare
3069 Typ : Entity_Id;
3070
3071 begin
9c5a3a8d 3072 Check_E0;
996ae0b0
RK
3073 Find_Type (P);
3074 Typ := Entity (P);
3075
0ab80019 3076 if Ada_Version >= Ada_95
fbf5a39b
AC
3077 and then not Is_Scalar_Type (Typ)
3078 and then not Is_Generic_Type (Typ)
3079 then
822033eb 3080 Error_Attr_P ("prefix of Base attribute must be scalar type");
fbf5a39b
AC
3081
3082 elsif Sloc (Typ) = Standard_Location
996ae0b0
RK
3083 and then Base_Type (Typ) = Typ
3084 and then Warn_On_Redundant_Constructs
3085 then
ed2233dc 3086 Error_Msg_NE -- CODEFIX
324ac540 3087 ("?r?redundant attribute, & is its own base type", N, Typ);
996ae0b0
RK
3088 end if;
3089
7a489a2b
AC
3090 if Nkind (Parent (N)) /= N_Attribute_Reference then
3091 Error_Msg_Name_1 := Aname;
ce5ba43a 3092 Check_SPARK_05_Restriction
7a489a2b
AC
3093 ("attribute% is only allowed as prefix of another attribute", P);
3094 end if;
3095
996ae0b0 3096 Set_Etype (N, Base_Type (Entity (P)));
9c5a3a8d 3097 Set_Entity (N, Base_Type (Entity (P)));
e4494292 3098 Rewrite (N, New_Occurrence_Of (Entity (N), Loc));
9c5a3a8d 3099 Analyze (N);
996ae0b0
RK
3100 end Base;
3101
3102 ---------
3103 -- Bit --
3104 ---------
3105
3106 when Attribute_Bit => Bit :
3107 begin
3108 Check_E0;
3109
3110 if not Is_Object_Reference (P) then
822033eb 3111 Error_Attr_P ("prefix for % attribute must be object");
996ae0b0
RK
3112
3113 -- What about the access object cases ???
3114
3115 else
3116 null;
3117 end if;
3118
3119 Set_Etype (N, Universal_Integer);
3120 end Bit;
3121
3122 ---------------
3123 -- Bit_Order --
3124 ---------------
3125
3126 when Attribute_Bit_Order => Bit_Order :
3127 begin
3128 Check_E0;
3129 Check_Type;
3130
3131 if not Is_Record_Type (P_Type) then
822033eb 3132 Error_Attr_P ("prefix of % attribute must be record type");
996ae0b0
RK
3133 end if;
3134
3135 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
3136 Rewrite (N,
3137 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
3138 else
3139 Rewrite (N,
3140 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
3141 end if;
3142
3143 Set_Etype (N, RTE (RE_Bit_Order));
fbf5a39b 3144 Resolve (N);
996ae0b0
RK
3145
3146 -- Reset incorrect indication of staticness
3147
3148 Set_Is_Static_Expression (N, False);
3149 end Bit_Order;
3150
3151 ------------------
3152 -- Bit_Position --
3153 ------------------
3154
3155 -- Note: in generated code, we can have a Bit_Position attribute
3156 -- applied to a (naked) record component (i.e. the prefix is an
3157 -- identifier that references an E_Component or E_Discriminant
3158 -- entity directly, and this is interpreted as expected by Gigi.
3159 -- The following code will not tolerate such usage, but when the
3160 -- expander creates this special case, it marks it as analyzed
3161 -- immediately and sets an appropriate type.
3162
3163 when Attribute_Bit_Position =>
996ae0b0
RK
3164 if Comes_From_Source (N) then
3165 Check_Component;
3166 end if;
3167
3168 Set_Etype (N, Universal_Integer);
3169
3170 ------------------
3171 -- Body_Version --
3172 ------------------
3173
3174 when Attribute_Body_Version =>
3175 Check_E0;
3176 Check_Program_Unit;
3177 Set_Etype (N, RTE (RE_Version_String));
3178
3179 --------------
3180 -- Callable --
3181 --------------
3182
3183 when Attribute_Callable =>
3184 Check_E0;
3185 Set_Etype (N, Standard_Boolean);
3186 Check_Task_Prefix;
3187
3188 ------------
3189 -- Caller --
3190 ------------
3191
3192 when Attribute_Caller => Caller : declare
3193 Ent : Entity_Id;
3194 S : Entity_Id;
3195
3196 begin
3197 Check_E0;
3198
e10dab7f 3199 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
996ae0b0
RK
3200 Ent := Entity (P);
3201
3202 if not Is_Entry (Ent) then
3203 Error_Attr ("invalid entry name", N);
3204 end if;
3205
3206 else
3207 Error_Attr ("invalid entry name", N);
3208 return;
3209 end if;
3210
3211 for J in reverse 0 .. Scope_Stack.Last loop
3212 S := Scope_Stack.Table (J).Entity;
3213
3214 if S = Scope (Ent) then
3215 Error_Attr ("Caller must appear in matching accept or body", N);
3216 elsif S = Ent then
3217 exit;
3218 end if;
3219 end loop;
3220
b5e792e2 3221 Set_Etype (N, RTE (RO_AT_Task_Id));
996ae0b0
RK
3222 end Caller;
3223
3224 -------------
3225 -- Ceiling --
3226 -------------
3227
3228 when Attribute_Ceiling =>
3229 Check_Floating_Point_Type_1;
3230 Set_Etype (N, P_Base_Type);
3231 Resolve (E1, P_Base_Type);
3232
3233 -----------
3234 -- Class --
3235 -----------
3236
9c5a3a8d 3237 when Attribute_Class =>
996ae0b0 3238 Check_Restriction (No_Dispatch, N);
9c5a3a8d
ST
3239 Check_E0;
3240 Find_Type (N);
996ae0b0 3241
b5c739f9
RD
3242 -- Applying Class to untagged incomplete type is obsolescent in Ada
3243 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3244 -- this flag gets set by Find_Type in this situation.
3245
7a963087 3246 if Restriction_Check_Required (No_Obsolescent_Features)
b5c739f9
RD
3247 and then Ada_Version >= Ada_2005
3248 and then Ekind (P_Type) = E_Incomplete_Type
3249 then
3250 declare
3251 DN : constant Node_Id := Declaration_Node (P_Type);
3252 begin
3253 if Nkind (DN) = N_Incomplete_Type_Declaration
3254 and then not Tagged_Present (DN)
3255 then
3256 Check_Restriction (No_Obsolescent_Features, P);
3257 end if;
3258 end;
3259 end if;
3260
996ae0b0
RK
3261 ------------------
3262 -- Code_Address --
3263 ------------------
3264
3265 when Attribute_Code_Address =>
3266 Check_E0;
3267
3268 if Nkind (P) = N_Attribute_Reference
b69cd36a 3269 and then Nam_In (Attribute_Name (P), Name_Elab_Body, Name_Elab_Spec)
996ae0b0
RK
3270 then
3271 null;
3272
3273 elsif not Is_Entity_Name (P)
3274 or else (Ekind (Entity (P)) /= E_Function
3275 and then
3276 Ekind (Entity (P)) /= E_Procedure)
3277 then
3278 Error_Attr ("invalid prefix for % attribute", P);
3279 Set_Address_Taken (Entity (P));
16212e89
GD
3280
3281 -- Issue an error if the prefix denotes an eliminated subprogram
3282
3283 else
3284 Check_For_Eliminated_Subprogram (P, Entity (P));
996ae0b0
RK
3285 end if;
3286
3287 Set_Etype (N, RTE (RE_Address));
3288
e0bf7d65
RD
3289 ----------------------
3290 -- Compiler_Version --
3291 ----------------------
3292
3293 when Attribute_Compiler_Version =>
3294 Check_E0;
3295 Check_Standard_Prefix;
1f110335 3296 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
e0bf7d65 3297 Analyze_And_Resolve (N, Standard_String);
edab6088 3298 Set_Is_Static_Expression (N, True);
e0bf7d65 3299
996ae0b0
RK
3300 --------------------
3301 -- Component_Size --
3302 --------------------
3303
3304 when Attribute_Component_Size =>
3305 Check_E0;
3306 Set_Etype (N, Universal_Integer);
3307
3308 -- Note: unlike other array attributes, unconstrained arrays are OK
3309
3310 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
3311 null;
3312 else
3313 Check_Array_Type;
3314 end if;
3315
3316 -------------
3317 -- Compose --
3318 -------------
3319
3320 when Attribute_Compose =>
3321 Check_Floating_Point_Type_2;
3322 Set_Etype (N, P_Base_Type);
3323 Resolve (E1, P_Base_Type);
3324 Resolve (E2, Any_Integer);
3325
3326 -----------------
3327 -- Constrained --
3328 -----------------
3329
3330 when Attribute_Constrained =>
3331 Check_E0;
3332 Set_Etype (N, Standard_Boolean);
3333
3334 -- Case from RM J.4(2) of constrained applied to private type
3335
3336 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
b5c739f9 3337 Check_Restriction (No_Obsolescent_Features, P);
5f3ab6fb
AC
3338
3339 if Warn_On_Obsolescent_Feature then
3340 Error_Msg_N
3341 ("constrained for private type is an " &
324ac540 3342 "obsolescent feature (RM J.4)?j?", N);
5f3ab6fb 3343 end if;
996ae0b0
RK
3344
3345 -- If we are within an instance, the attribute must be legal
19f0526a
AC
3346 -- because it was valid in the generic unit. Ditto if this is
3347 -- an inlining of a function declared in an instance.
996ae0b0 3348
b80a2b4b 3349 if In_Instance or else In_Inlined_Body then
996ae0b0
RK
3350 return;
3351
3352 -- For sure OK if we have a real private type itself, but must
3353 -- be completed, cannot apply Constrained to incomplete type.
3354
3355 elsif Is_Private_Type (Entity (P)) then
fbf5a39b
AC
3356
3357 -- Note: this is one of the Annex J features that does not
3358 -- generate a warning from -gnatwj, since in fact it seems
3359 -- very useful, and is used in the GNAT runtime.
3360
996ae0b0
RK
3361 Check_Not_Incomplete_Type;
3362 return;
3363 end if;
3364
fbf5a39b
AC
3365 -- Normal (non-obsolescent case) of application to object of
3366 -- a discriminated type.
3367
996ae0b0
RK
3368 else
3369 Check_Object_Reference (P);
3370
3371 -- If N does not come from source, then we allow the
3372 -- the attribute prefix to be of a private type whose
3373 -- full type has discriminants. This occurs in cases
3374 -- involving expanded calls to stream attributes.
3375
3376 if not Comes_From_Source (N) then
3377 P_Type := Underlying_Type (P_Type);
3378 end if;
3379
3380 -- Must have discriminants or be an access type designating
a6152428 3381 -- a type with discriminants. If it is a classwide type it
996ae0b0
RK
3382 -- has unknown discriminants.
3383
3384 if Has_Discriminants (P_Type)
a6152428
AC
3385 or else Has_Unknown_Discriminants (P_Type)
3386 or else
3387 (Is_Access_Type (P_Type)
3388 and then Has_Discriminants (Designated_Type (P_Type)))
996ae0b0
RK
3389 then
3390 return;
3391
a6152428
AC
3392 -- The rule given in 3.7.2 is part of static semantics, but the
3393 -- intent is clearly that it be treated as a legality rule, and
3394 -- rechecked in the visible part of an instance. Nevertheless
3395 -- the intent also seems to be it should legally apply to the
3396 -- actual of a formal with unknown discriminants, regardless of
3397 -- whether the actual has discriminants, in which case the value
3398 -- of the attribute is determined using the J.4 rules. This choice
3399 -- seems the most useful, and is compatible with existing tests.
3400
3401 elsif In_Instance then
3402 return;
3403
996ae0b0 3404 -- Also allow an object of a generic type if extensions allowed
a6152428 3405 -- and allow this for any type at all. (this may be obsolete ???)
996ae0b0
RK
3406
3407 elsif (Is_Generic_Type (P_Type)
a6152428 3408 or else Is_Generic_Actual_Type (P_Type))
996ae0b0
RK
3409 and then Extensions_Allowed
3410 then
3411 return;
3412 end if;
3413 end if;
3414
3415 -- Fall through if bad prefix
3416
822033eb
HK
3417 Error_Attr_P
3418 ("prefix of % attribute must be object of discriminated type");
996ae0b0
RK
3419
3420 ---------------
3421 -- Copy_Sign --
3422 ---------------
3423
3424 when Attribute_Copy_Sign =>
3425 Check_Floating_Point_Type_2;
3426 Set_Etype (N, P_Base_Type);
3427 Resolve (E1, P_Base_Type);
3428 Resolve (E2, P_Base_Type);
3429
3430 -----------
3431 -- Count --
3432 -----------
3433
3434 when Attribute_Count => Count :
3435 declare
3436 Ent : Entity_Id;
3437 S : Entity_Id;
3438 Tsk : Entity_Id;
3439
3440 begin
3441 Check_E0;
3442
e10dab7f 3443 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
996ae0b0
RK
3444 Ent := Entity (P);
3445
3446 if Ekind (Ent) /= E_Entry then
3447 Error_Attr ("invalid entry name", N);
3448 end if;
3449
3450 elsif Nkind (P) = N_Indexed_Component then
07fc65c4
GB
3451 if not Is_Entity_Name (Prefix (P))
3452 or else No (Entity (Prefix (P)))
3453 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
3454 then
3455 if Nkind (Prefix (P)) = N_Selected_Component
3456 and then Present (Entity (Selector_Name (Prefix (P))))
3457 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
3458 E_Entry_Family
3459 then
3460 Error_Attr
3461 ("attribute % must apply to entry of current task", P);
996ae0b0 3462
07fc65c4
GB
3463 else
3464 Error_Attr ("invalid entry family name", P);
3465 end if;
996ae0b0 3466 return;
07fc65c4
GB
3467
3468 else
3469 Ent := Entity (Prefix (P));
996ae0b0
RK
3470 end if;
3471
07fc65c4
GB
3472 elsif Nkind (P) = N_Selected_Component
3473 and then Present (Entity (Selector_Name (P)))
3474 and then Ekind (Entity (Selector_Name (P))) = E_Entry
3475 then
3476 Error_Attr
3477 ("attribute % must apply to entry of current task", P);
3478
996ae0b0
RK
3479 else
3480 Error_Attr ("invalid entry name", N);
3481 return;
3482 end if;
3483
3484 for J in reverse 0 .. Scope_Stack.Last loop
3485 S := Scope_Stack.Table (J).Entity;
3486
3487 if S = Scope (Ent) then
3488 if Nkind (P) = N_Expanded_Name then
3489 Tsk := Entity (Prefix (P));
3490
3491 -- The prefix denotes either the task type, or else a
3492 -- single task whose task type is being analyzed.
3493
b80a2b4b 3494 if (Is_Type (Tsk) and then Tsk = S)
996ae0b0 3495 or else (not Is_Type (Tsk)
b80a2b4b
AC
3496 and then Etype (Tsk) = S
3497 and then not (Comes_From_Source (S)))
996ae0b0
RK
3498 then
3499 null;
3500 else
07fc65c4
GB
3501 Error_Attr
3502 ("Attribute % must apply to entry of current task", N);
996ae0b0
RK
3503 end if;
3504 end if;
3505
3506 exit;
3507
3508 elsif Ekind (Scope (Ent)) in Task_Kind
e1b871e9
AC
3509 and then
3510 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
996ae0b0 3511 then
07fc65c4 3512 Error_Attr ("Attribute % cannot appear in inner unit", N);
996ae0b0
RK
3513
3514 elsif Ekind (Scope (Ent)) = E_Protected_Type
3515 and then not Has_Completion (Scope (Ent))
3516 then
3517 Error_Attr ("attribute % can only be used inside body", N);
3518 end if;
3519 end loop;
3520
3521 if Is_Overloaded (P) then
3522 declare
3523 Index : Interp_Index;
3524 It : Interp;
3525
3526 begin
3527 Get_First_Interp (P, Index, It);
996ae0b0
RK
3528 while Present (It.Nam) loop
3529 if It.Nam = Ent then
3530 null;
3531
edd63e9b
ES
3532 -- Ada 2005 (AI-345): Do not consider primitive entry
3533 -- wrappers generated for task or protected types.
3534
0791fbe9 3535 elsif Ada_Version >= Ada_2005
edd63e9b
ES
3536 and then not Comes_From_Source (It.Nam)
3537 then
3538 null;
3539
996ae0b0 3540 else
fbf5a39b 3541 Error_Attr ("ambiguous entry name", N);
996ae0b0
RK
3542 end if;
3543
3544 Get_Next_Interp (Index, It);
3545 end loop;
3546 end;
3547 end if;
3548
3549 Set_Etype (N, Universal_Integer);
3550 end Count;
3551
3552 -----------------------
3553 -- Default_Bit_Order --
3554 -----------------------
3555
e477d718 3556 when Attribute_Default_Bit_Order => Default_Bit_Order : declare
7ed57189 3557 Target_Default_Bit_Order : System.Bit_Order;
e477d718 3558
996ae0b0
RK
3559 begin
3560 Check_Standard_Prefix;
996ae0b0
RK
3561
3562 if Bytes_Big_Endian then
7ed57189 3563 Target_Default_Bit_Order := System.High_Order_First;
996ae0b0 3564 else
7ed57189 3565 Target_Default_Bit_Order := System.Low_Order_First;
996ae0b0
RK
3566 end if;
3567
7ed57189
AC
3568 Rewrite (N,
3569 Make_Integer_Literal (Loc,
3570 UI_From_Int (System.Bit_Order'Pos (Target_Default_Bit_Order))));
3571
996ae0b0
RK
3572 Set_Etype (N, Universal_Integer);
3573 Set_Is_Static_Expression (N);
3574 end Default_Bit_Order;
3575
7ed57189
AC
3576 ----------------------------------
3577 -- Default_Scalar_Storage_Order --
3578 ----------------------------------
3579
3580 when Attribute_Default_Scalar_Storage_Order => Default_SSO : declare
3581 RE_Default_SSO : RE_Id;
e477d718 3582
7ed57189
AC
3583 begin
3584 Check_Standard_Prefix;
3585
3586 case Opt.Default_SSO is
3587 when ' ' =>
3588 if Bytes_Big_Endian then
3589 RE_Default_SSO := RE_High_Order_First;
3590 else
3591 RE_Default_SSO := RE_Low_Order_First;
3592 end if;
e477d718 3593
7ed57189
AC
3594 when 'H' =>
3595 RE_Default_SSO := RE_High_Order_First;
e477d718 3596
7ed57189
AC
3597 when 'L' =>
3598 RE_Default_SSO := RE_Low_Order_First;
e477d718 3599
7ed57189
AC
3600 when others =>
3601 raise Program_Error;
3602 end case;
3603
3604 Rewrite (N, New_Occurrence_Of (RTE (RE_Default_SSO), Loc));
3605 end Default_SSO;
3606
996ae0b0
RK
3607 --------------
3608 -- Definite --
3609 --------------
3610
3611 when Attribute_Definite =>
3612 Legal_Formal_Attribute;
3613
3614 -----------
3615 -- Delta --
3616 -----------
3617
3618 when Attribute_Delta =>
3619 Check_Fixed_Point_Type_0;
3620 Set_Etype (N, Universal_Real);
3621
3622 ------------
3623 -- Denorm --
3624 ------------
3625
3626 when Attribute_Denorm =>
3627 Check_Floating_Point_Type_0;
3628 Set_Etype (N, Standard_Boolean);
3629
eaed2a2c
AC
3630 -----------
3631 -- Deref --
3632 -----------
3633
3634 when Attribute_Deref =>
3635 Check_Type;
3636 Check_E1;
3637 Resolve (E1, RTE (RE_Address));
3638 Set_Etype (N, P_Type);
3639
203ddcea
AC
3640 ---------------------
3641 -- Descriptor_Size --
3642 ---------------------
3643
3644 when Attribute_Descriptor_Size =>
3645 Check_E0;
3646
b80a2b4b 3647 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
cb3d8731 3648 Error_Attr_P ("prefix of attribute % must denote a type");
203ddcea
AC
3649 end if;
3650
3651 Set_Etype (N, Universal_Integer);
3652
996ae0b0
RK
3653 ------------
3654 -- Digits --
3655 ------------
3656
3657 when Attribute_Digits =>
3658 Check_E0;
3659 Check_Type;
3660
3661 if not Is_Floating_Point_Type (P_Type)
3662 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3663 then
822033eb
HK
3664 Error_Attr_P
3665 ("prefix of % attribute must be float or decimal type");
996ae0b0
RK
3666 end if;
3667
3668 Set_Etype (N, Universal_Integer);
3669
3670 ---------------
3671 -- Elab_Body --
3672 ---------------
3673
2c1a2cf3 3674 -- Also handles processing for Elab_Spec and Elab_Subp_Body
996ae0b0 3675
0bfc9a64
AC
3676 when Attribute_Elab_Body |
3677 Attribute_Elab_Spec |
3678 Attribute_Elab_Subp_Body =>
3679
996ae0b0
RK
3680 Check_E0;
3681 Check_Unit_Name (P);
3682 Set_Etype (N, Standard_Void_Type);
3683
3684 -- We have to manually call the expander in this case to get
3685 -- the necessary expansion (normally attributes that return
3686 -- entities are not expanded).
3687
3688 Expand (N);
3689
3690 ---------------
3691 -- Elab_Spec --
3692 ---------------
3693
3694 -- Shares processing with Elab_Body
3695
3696 ----------------
3697 -- Elaborated --
3698 ----------------
3699
3700 when Attribute_Elaborated =>
3701 Check_E0;
824e9320 3702 Check_Unit_Name (P);
996ae0b0
RK
3703 Set_Etype (N, Standard_Boolean);
3704
3705 ----------
3706 -- Emax --
3707 ----------
3708
3709 when Attribute_Emax =>
3710 Check_Floating_Point_Type_0;
3711 Set_Etype (N, Universal_Integer);
3712
442ade9d
RD
3713 -------------
3714 -- Enabled --
3715 -------------
3716
3717 when Attribute_Enabled =>
3718 Check_Either_E0_Or_E1;
3719
3720 if Present (E1) then
3721 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3722 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3723 E1 := Empty;
3724 end if;
3725 end if;
3726
3727 if Nkind (P) /= N_Identifier then
3728 Error_Msg_N ("identifier expected (check name)", P);
442ade9d
RD
3729 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3730 Error_Msg_N ("& is not a recognized check name", P);
3731 end if;
3732
3733 Set_Etype (N, Standard_Boolean);
3734
996ae0b0
RK
3735 --------------
3736 -- Enum_Rep --
3737 --------------
3738
3739 when Attribute_Enum_Rep => Enum_Rep : declare
3740 begin
3741 if Present (E1) then
3742 Check_E1;
3743 Check_Discrete_Type;
3744 Resolve (E1, P_Base_Type);
75e4e36d 3745
1956beb8
BD
3746 elsif not Is_Discrete_Type (Etype (P)) then
3747 Error_Attr_P ("prefix of % attribute must be of discrete type");
996ae0b0
RK
3748 end if;
3749
3750 Set_Etype (N, Universal_Integer);
3751 end Enum_Rep;
3752
21d27997
RD
3753 --------------
3754 -- Enum_Val --
3755 --------------
3756
3757 when Attribute_Enum_Val => Enum_Val : begin
3758 Check_E1;
3759 Check_Type;
3760
3761 if not Is_Enumeration_Type (P_Type) then
3762 Error_Attr_P ("prefix of % attribute must be enumeration type");
3763 end if;
3764
3765 -- If the enumeration type has a standard representation, the effect
3766 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3767
3768 if not Has_Non_Standard_Rep (P_Base_Type) then
3769 Rewrite (N,
3770 Make_Attribute_Reference (Loc,
3771 Prefix => Relocate_Node (Prefix (N)),
3772 Attribute_Name => Name_Val,
3773 Expressions => New_List (Relocate_Node (E1))));
3774 Analyze_And_Resolve (N, P_Base_Type);
3775
3776 -- Non-standard representation case (enumeration with holes)
3777
3778 else
3779 Check_Enum_Image;
3780 Resolve (E1, Any_Integer);
3781 Set_Etype (N, P_Base_Type);
3782 end if;
3783 end Enum_Val;
3784
996ae0b0
RK
3785 -------------
3786 -- Epsilon --
3787 -------------
3788
3789 when Attribute_Epsilon =>
3790 Check_Floating_Point_Type_0;
3791 Set_Etype (N, Universal_Real);
3792
3793 --------------
3794 -- Exponent --
3795 --------------
3796
3797 when Attribute_Exponent =>
3798 Check_Floating_Point_Type_1;
3799 Set_Etype (N, Universal_Integer);
3800 Resolve (E1, P_Base_Type);
3801
3802 ------------------
3803 -- External_Tag --
3804 ------------------
3805
3806 when Attribute_External_Tag =>
3807 Check_E0;
3808 Check_Type;
3809
3810 Set_Etype (N, Standard_String);
3811
3812 if not Is_Tagged_Type (P_Type) then
822033eb 3813 Error_Attr_P ("prefix of % attribute must be tagged");
996ae0b0
RK
3814 end if;
3815
7b76e805
RD
3816 ---------------
3817 -- Fast_Math --
3818 ---------------
3819
3820 when Attribute_Fast_Math =>
7b76e805 3821 Check_Standard_Prefix;
21791d97 3822 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Fast_Math), Loc));
7b76e805 3823
996ae0b0
RK
3824 -----------
3825 -- First --
3826 -----------
3827
3828 when Attribute_First =>
3829 Check_Array_Or_Scalar_Type;
26df19ce 3830 Bad_Attribute_For_Predicate;
996ae0b0
RK
3831
3832 ---------------
3833 -- First_Bit --
3834 ---------------
3835
3836 when Attribute_First_Bit =>
3837 Check_Component;
3838 Set_Etype (N, Universal_Integer);
3839
011f9d5d
AC
3840 -----------------
3841 -- First_Valid --
3842 -----------------
3843
3844 when Attribute_First_Valid =>
3845 Check_First_Last_Valid;
3846 Set_Etype (N, P_Type);
3847
996ae0b0
RK
3848 -----------------
3849 -- Fixed_Value --
3850 -----------------
3851
3852 when Attribute_Fixed_Value =>
3853 Check_E1;
3854 Check_Fixed_Point_Type;
3855 Resolve (E1, Any_Integer);
3856 Set_Etype (N, P_Base_Type);
3857
3858 -----------
3859 -- Floor --
3860 -----------
3861
3862 when Attribute_Floor =>
3863 Check_Floating_Point_Type_1;
3864 Set_Etype (N, P_Base_Type);
3865 Resolve (E1, P_Base_Type);
3866
3867 ----------
3868 -- Fore --
3869 ----------
3870
3871 when Attribute_Fore =>
3872 Check_Fixed_Point_Type_0;
3873 Set_Etype (N, Universal_Integer);
3874
3875 --------------
3876 -- Fraction --
3877 --------------
3878
3879 when Attribute_Fraction =>
3880 Check_Floating_Point_Type_1;
3881 Set_Etype (N, P_Base_Type);
3882 Resolve (E1, P_Base_Type);
3883
54838d1f
AC
3884 --------------
3885 -- From_Any --
3886 --------------
3887
3888 when Attribute_From_Any =>
3889 Check_E1;
3890 Check_PolyORB_Attribute;
3891 Set_Etype (N, P_Base_Type);
3892
15ce9ca2
AC
3893 -----------------------
3894 -- Has_Access_Values --
3895 -----------------------
3896
3897 when Attribute_Has_Access_Values =>
3898 Check_Type;
3899 Check_E0;
3900 Set_Etype (N, Standard_Boolean);
3901
ea70f3d0
RD
3902 ----------------------
3903 -- Has_Same_Storage --
3904 ----------------------
3905
3906 when Attribute_Has_Same_Storage =>
ea70f3d0
RD
3907 Check_E1;
3908
3909 -- The arguments must be objects of any type
3910
3911 Analyze_And_Resolve (P);
3912 Analyze_And_Resolve (E1);
3913 Check_Object_Reference (P);
3914 Check_Object_Reference (E1);
3915 Set_Etype (N, Standard_Boolean);
3916
21d27997
RD
3917 -----------------------
3918 -- Has_Tagged_Values --
3919 -----------------------
3920
3921 when Attribute_Has_Tagged_Values =>
3922 Check_Type;
3923 Check_E0;
3924 Set_Etype (N, Standard_Boolean);
3925
996ae0b0
RK
3926 -----------------------
3927 -- Has_Discriminants --
3928 -----------------------
3929
3930 when Attribute_Has_Discriminants =>
3931 Legal_Formal_Attribute;
3932
3933 --------------
3934 -- Identity --
3935 --------------
3936
3937 when Attribute_Identity =>
3938 Check_E0;
3939 Analyze (P);
3940
1b1d88b1 3941 if Etype (P) = Standard_Exception_Type then
996ae0b0
RK
3942 Set_Etype (N, RTE (RE_Exception_Id));
3943
b80a2b4b
AC
3944 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
3945 -- interface class-wide types.
65f01153 3946
996ae0b0
RK
3947 elsif Is_Task_Type (Etype (P))
3948 or else (Is_Access_Type (Etype (P))
65f01153 3949 and then Is_Task_Type (Designated_Type (Etype (P))))
0791fbe9 3950 or else (Ada_Version >= Ada_2005
65f01153
RD
3951 and then Ekind (Etype (P)) = E_Class_Wide_Type
3952 and then Is_Interface (Etype (P))
3953 and then Is_Task_Interface (Etype (P)))
996ae0b0 3954 then
fbf5a39b 3955 Resolve (P);
b5e792e2 3956 Set_Etype (N, RTE (RO_AT_Task_Id));
996ae0b0
RK
3957
3958 else
0791fbe9 3959 if Ada_Version >= Ada_2005 then
822033eb
HK
3960 Error_Attr_P
3961 ("prefix of % attribute must be an exception, a " &
3962 "task or a task interface class-wide object");
65f01153 3963 else
822033eb
HK
3964 Error_Attr_P
3965 ("prefix of % attribute must be a task or an exception");
65f01153 3966 end if;
996ae0b0
RK
3967 end if;
3968
3969 -----------
3970 -- Image --
3971 -----------
3972
e361e9a1 3973 when Attribute_Image => Image : begin
ce5ba43a 3974 Check_SPARK_05_Restriction_On_Attribute;
e361e9a1
AC
3975
3976 -- AI12-00124-1 : The ARG has adopted the GNAT semantics of 'Img
3977 -- for scalar types, so that the prefix can be an object and not
3978 -- a type, and there is no need for an argument. Given this vote
3979 -- of confidence from the ARG, simplest is to transform this new
3980 -- usage of 'Image into a reference to 'Img.
3981
3982 if Ada_Version > Ada_2005
3983 and then Is_Object_Reference (P)
3984 and then Is_Scalar_Type (P_Type)
3985 then
3986 Rewrite (N,
3987 Make_Attribute_Reference (Loc,
3988 Prefix => Relocate_Node (P),
3989 Attribute_Name => Name_Img));
3990 Analyze (N);
3991 return;
3992
3993 else
3994 Check_Scalar_Type;
3995 end if;
3996
7a489a2b 3997 Set_Etype (N, Standard_String);
996ae0b0
RK
3998
3999 if Is_Real_Type (P_Type) then
0ab80019 4000 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
996ae0b0
RK
4001 Error_Msg_Name_1 := Aname;
4002 Error_Msg_N
4003 ("(Ada 83) % attribute not allowed for real types", N);
4004 end if;
4005 end if;
4006
4007 if Is_Enumeration_Type (P_Type) then
4008 Check_Restriction (No_Enumeration_Maps, N);
4009 end if;
4010
4011 Check_E1;
4012 Resolve (E1, P_Base_Type);
4013 Check_Enum_Image;
4014 Validate_Non_Static_Attribute_Function_Call;
0688dac8
RD
4015
4016 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
4017 -- to avoid giving a duplicate message for Img expanded into Image.
4018
4019 if Restriction_Check_Required (No_Fixed_IO)
4020 and then Comes_From_Source (N)
4021 and then Is_Fixed_Point_Type (P_Type)
4022 then
4023 Check_Restriction (No_Fixed_IO, P);
4024 end if;
996ae0b0
RK
4025 end Image;
4026
4027 ---------
4028 -- Img --
4029 ---------
4030
4031 when Attribute_Img => Img :
4032 begin
9c5a3a8d 4033 Check_E0;
996ae0b0
RK
4034 Set_Etype (N, Standard_String);
4035
4036 if not Is_Scalar_Type (P_Type)
4037 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
4038 then
822033eb
HK
4039 Error_Attr_P
4040 ("prefix of % attribute must be scalar object name");
996ae0b0
RK
4041 end if;
4042
4043 Check_Enum_Image;
0688dac8
RD
4044
4045 -- Check restriction No_Fixed_IO
4046
4047 if Restriction_Check_Required (No_Fixed_IO)
4048 and then Is_Fixed_Point_Type (P_Type)
4049 then
4050 Check_Restriction (No_Fixed_IO, P);
4051 end if;
996ae0b0
RK
4052 end Img;
4053
4054 -----------
4055 -- Input --
4056 -----------
4057
4058 when Attribute_Input =>
4059 Check_E1;
fbf5a39b 4060 Check_Stream_Attribute (TSS_Stream_Input);
996ae0b0
RK
4061 Set_Etype (N, P_Base_Type);
4062
4063 -------------------
4064 -- Integer_Value --
4065 -------------------
4066
4067 when Attribute_Integer_Value =>
4068 Check_E1;
4069 Check_Integer_Type;
4070 Resolve (E1, Any_Fixed);
5a218498
ST
4071
4072 -- Signal an error if argument type is not a specific fixed-point
4073 -- subtype. An error has been signalled already if the argument
4074 -- was not of a fixed-point type.
4075
4076 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
4077 Error_Attr ("argument of % must be of a fixed-point type", E1);
4078 end if;
4079
996ae0b0
RK
4080 Set_Etype (N, P_Base_Type);
4081
21d27997
RD
4082 -------------------
4083 -- Invalid_Value --
4084 -------------------
4085
4086 when Attribute_Invalid_Value =>
4087 Check_E0;
4088 Check_Scalar_Type;
4089 Set_Etype (N, P_Base_Type);
4090 Invalid_Value_Used := True;
4091
996ae0b0
RK
4092 -----------
4093 -- Large --
4094 -----------
4095
4096 when Attribute_Large =>
4097 Check_E0;
4098 Check_Real_Type;
4099 Set_Etype (N, Universal_Real);
4100
4101 ----------
4102 -- Last --
4103 ----------
4104
4105 when Attribute_Last =>
4106 Check_Array_Or_Scalar_Type;
26df19ce 4107 Bad_Attribute_For_Predicate;
996ae0b0
RK
4108
4109 --------------
4110 -- Last_Bit --
4111 --------------
4112
4113 when Attribute_Last_Bit =>
4114 Check_Component;
4115 Set_Etype (N, Universal_Integer);
4116
011f9d5d
AC
4117 ----------------
4118 -- Last_Valid --
4119 ----------------
4120
4121 when Attribute_Last_Valid =>
4122 Check_First_Last_Valid;
4123 Set_Etype (N, P_Type);
4124
996ae0b0
RK
4125 ------------------
4126 -- Leading_Part --
4127 ------------------
4128
4129 when Attribute_Leading_Part =>
4130 Check_Floating_Point_Type_2;
4131 Set_Etype (N, P_Base_Type);
4132 Resolve (E1, P_Base_Type);
4133 Resolve (E2, Any_Integer);
4134
4135 ------------
4136 -- Length --
4137 ------------
4138
4139 when Attribute_Length =>
4140 Check_Array_Type;
4141 Set_Etype (N, Universal_Integer);
4142
f7ea2603
RD
4143 -------------------
4144 -- Library_Level --
4145 -------------------
4146
4147 when Attribute_Library_Level =>
4148 Check_E0;
cf3b97ef
AC
4149
4150 if not Is_Entity_Name (P) then
4151 Error_Attr_P ("prefix of % attribute must be an entity name");
4152 end if;
f7ea2603
RD
4153
4154 if not Inside_A_Generic then
4155 Set_Boolean_Result (N,
cf3b97ef 4156 Is_Library_Level_Entity (Entity (P)));
f7ea2603
RD
4157 end if;
4158
4159 Set_Etype (N, Standard_Boolean);
4160
2a290fec
AC
4161 ---------------
4162 -- Lock_Free --
4163 ---------------
4164
4165 when Attribute_Lock_Free =>
4166 Check_E0;
4167 Set_Etype (N, Standard_Boolean);
4168
4169 if not Is_Protected_Type (P_Type) then
4170 Error_Attr_P
4171 ("prefix of % attribute must be a protected object");
4172 end if;
4173
150ac76e
AC
4174 ----------------
4175 -- Loop_Entry --
4176 ----------------
4177
4178 when Attribute_Loop_Entry => Loop_Entry : declare
4179 procedure Check_References_In_Prefix (Loop_Id : Entity_Id);
4180 -- Inspect the prefix for any uses of entities declared within the
4181 -- related loop. Loop_Id denotes the loop identifier.
4182
4183 --------------------------------
4184 -- Check_References_In_Prefix --
4185 --------------------------------
4186
4187 procedure Check_References_In_Prefix (Loop_Id : Entity_Id) is
4188 Loop_Decl : constant Node_Id := Label_Construct (Parent (Loop_Id));
4189
4190 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4191 -- Determine whether a reference mentions an entity declared
4192 -- within the related loop.
4193
4194 function Declared_Within (Nod : Node_Id) return Boolean;
4195 -- Determine whether Nod appears in the subtree of Loop_Decl
4196
4197 ---------------------
4198 -- Check_Reference --
4199 ---------------------
4200
4201 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4202 begin
4203 if Nkind (Nod) = N_Identifier
4204 and then Present (Entity (Nod))
4205 and then Declared_Within (Declaration_Node (Entity (Nod)))
4206 then
4207 Error_Attr
4208 ("prefix of attribute % cannot reference local entities",
4209 Nod);
4210 return Abandon;
4211 else
4212 return OK;
4213 end if;
4214 end Check_Reference;
4215
4216 procedure Check_References is new Traverse_Proc (Check_Reference);
4217
4218 ---------------------
4219 -- Declared_Within --
4220 ---------------------
4221
4222 function Declared_Within (Nod : Node_Id) return Boolean is
4223 Stmt : Node_Id;
4224
4225 begin
4226 Stmt := Nod;
4227 while Present (Stmt) loop
4228 if Stmt = Loop_Decl then
4229 return True;
4230
4231 -- Prevent the search from going too far
4232
a7e68e7f 4233 elsif Is_Body_Or_Package_Declaration (Stmt) then
150ac76e
AC
4234 exit;
4235 end if;
4236
4237 Stmt := Parent (Stmt);
4238 end loop;
4239
4240 return False;
4241 end Declared_Within;
4242
4243 -- Start of processing for Check_Prefix_For_Local_References
4244
4245 begin
4246 Check_References (P);
4247 end Check_References_In_Prefix;
4248
4249 -- Local variables
4250
24778dbb 4251 Context : constant Node_Id := Parent (N);
3d67b239 4252 Attr : Node_Id;
150ac76e 4253 Enclosing_Loop : Node_Id;
150ac76e
AC
4254 Loop_Id : Entity_Id := Empty;
4255 Scop : Entity_Id;
4256 Stmt : Node_Id;
6782b1ef 4257 Enclosing_Pragma : Node_Id := Empty;
150ac76e
AC
4258
4259 -- Start of processing for Loop_Entry
4260
4261 begin
3d67b239
AC
4262 Attr := N;
4263
4264 -- Set the type of the attribute now to ensure the successfull
4265 -- continuation of analysis even if the attribute is misplaced.
4266
4267 Set_Etype (Attr, P_Type);
4268
24778dbb 4269 -- Attribute 'Loop_Entry may appear in several flavors:
739e7bbf 4270
24778dbb
AC
4271 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4272 -- nearest enclosing loop.
739e7bbf 4273
24778dbb
AC
4274 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4275 -- attribute may be related to a loop denoted by label Expr or
4276 -- the prefix may denote an array object and Expr may act as an
4277 -- indexed component.
739e7bbf 4278
24778dbb
AC
4279 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4280 -- to the nearest enclosing loop, all expressions are part of
4281 -- an indexed component.
739e7bbf 4282
24778dbb
AC
4283 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4284 -- denotes, the attribute may be related to a loop denoted by
4285 -- label Expr or the prefix may denote a multidimensional array
4286 -- array object and Expr along with the rest of the expressions
4287 -- may act as indexed components.
739e7bbf 4288
24778dbb
AC
4289 -- Regardless of variations, the attribute reference does not have an
4290 -- expression list. Instead, all available expressions are stored as
4291 -- indexed components.
739e7bbf 4292
24778dbb
AC
4293 -- When the attribute is part of an indexed component, find the first
4294 -- expression as it will determine the semantics of 'Loop_Entry.
739e7bbf 4295
24778dbb
AC
4296 if Nkind (Context) = N_Indexed_Component then
4297 E1 := First (Expressions (Context));
4298 E2 := Next (E1);
739e7bbf 4299
24778dbb
AC
4300 -- The attribute reference appears in the following form:
4301
4302 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4303
4304 -- In this case, the loop name is omitted and no rewriting is
4305 -- required.
4306
4307 if Present (E2) then
4308 null;
4309
4310 -- The form of the attribute is:
739e7bbf 4311
24778dbb
AC
4312 -- Prefix'Loop_Entry (Expr) [(...)]
4313
4314 -- If Expr denotes a loop entry, the whole attribute and indexed
4315 -- component will have to be rewritten to reflect this relation.
739e7bbf
AC
4316
4317 else
24778dbb
AC
4318 pragma Assert (Present (E1));
4319
4320 -- Do not expand the expression as it may have side effects.
4321 -- Simply preanalyze to determine whether it is a loop name or
4322 -- something else.
4323
4324 Preanalyze_And_Resolve (E1);
4325
4326 if Is_Entity_Name (E1)
4327 and then Present (Entity (E1))
4328 and then Ekind (Entity (E1)) = E_Loop
4329 then
4330 Loop_Id := Entity (E1);
4331
4332 -- Transform the attribute and enclosing indexed component
4333
4334 Set_Expressions (N, Expressions (Context));
4335 Rewrite (Context, N);
4336 Set_Etype (Context, P_Type);
3d67b239
AC
4337
4338 Attr := Context;
24778dbb 4339 end if;
739e7bbf
AC
4340 end if;
4341 end if;
150ac76e
AC
4342
4343 -- The prefix must denote an object
4344
4345 if not Is_Object_Reference (P) then
4346 Error_Attr_P ("prefix of attribute % must denote an object");
4347 end if;
4348
4349 -- The prefix cannot be of a limited type because the expansion of
4350 -- Loop_Entry must create a constant initialized by the evaluated
4351 -- prefix.
4352
51245e2d 4353 if Is_Limited_View (Etype (P)) then
150ac76e
AC
4354 Error_Attr_P ("prefix of attribute % cannot be limited");
4355 end if;
4356
150ac76e
AC
4357 -- Climb the parent chain to verify the location of the attribute and
4358 -- find the enclosing loop.
4359
3d67b239 4360 Stmt := Attr;
150ac76e
AC
4361 while Present (Stmt) loop
4362
65441a1e
RD
4363 -- Locate the corresponding enclosing pragma. Note that in the
4364 -- case of Assert[And_Cut] and Assume, we have already checked
4365 -- that the pragma appears in an appropriate loop location.
150ac76e
AC
4366
4367 if Nkind (Original_Node (Stmt)) = N_Pragma
65441a1e
RD
4368 and then Nam_In (Pragma_Name (Original_Node (Stmt)),
4369 Name_Loop_Invariant,
4370 Name_Loop_Variant,
4371 Name_Assert,
4372 Name_Assert_And_Cut,
4373 Name_Assume)
150ac76e 4374 then
6782b1ef 4375 Enclosing_Pragma := Original_Node (Stmt);
150ac76e
AC
4376
4377 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4378 -- iteration may be expanded into several nested loops, we are
03a72cd3
AC
4379 -- interested in the outermost one which has the loop identifier,
4380 -- and comes from source.
150ac76e
AC
4381
4382 elsif Nkind (Stmt) = N_Loop_Statement
4383 and then Present (Identifier (Stmt))
03a72cd3
AC
4384 and then Comes_From_Source (Original_Node (Stmt))
4385 and then Nkind (Original_Node (Stmt)) = N_Loop_Statement
150ac76e
AC
4386 then
4387 Enclosing_Loop := Stmt;
739e7bbf
AC
4388
4389 -- The original attribute reference may lack a loop name. Use
4390 -- the name of the enclosing loop because it is the related
4391 -- loop.
4392
4393 if No (Loop_Id) then
4394 Loop_Id := Entity (Identifier (Enclosing_Loop));
4395 end if;
4396
150ac76e
AC
4397 exit;
4398
4399 -- Prevent the search from going too far
4400
a7e68e7f 4401 elsif Is_Body_Or_Package_Declaration (Stmt) then
150ac76e
AC
4402 exit;
4403 end if;
4404
4405 Stmt := Parent (Stmt);
4406 end loop;
4407
f3bf0d9a
HK
4408 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4409 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4410 -- purpose if they appear in an appropriate location in a loop,
4411 -- which was already checked by the top level pragma circuit).
150ac76e 4412
6782b1ef
AC
4413 if No (Enclosing_Pragma) then
4414 Error_Attr ("attribute% must appear within appropriate pragma", N);
150ac76e
AC
4415 end if;
4416
6782b1ef 4417 -- A Loop_Entry that applies to a given loop statement must not
150ac76e
AC
4418 -- appear within a body of accept statement, if this construct is
4419 -- itself enclosed by the given loop statement.
4420
3d67b239
AC
4421 for Index in reverse 0 .. Scope_Stack.Last loop
4422 Scop := Scope_Stack.Table (Index).Entity;
150ac76e
AC
4423
4424 if Ekind (Scop) = E_Loop and then Scop = Loop_Id then
4425 exit;
150ac76e
AC
4426 elsif Ekind_In (Scop, E_Block, E_Loop, E_Return_Statement) then
4427 null;
150ac76e
AC
4428 else
4429 Error_Attr
739e7bbf 4430 ("attribute % cannot appear in body or accept statement", N);
150ac76e
AC
4431 exit;
4432 end if;
4433 end loop;
4434
4435 -- The prefix cannot mention entities declared within the related
4436 -- loop because they will not be visible once the prefix is moved
4437 -- outside the loop.
4438
4439 Check_References_In_Prefix (Loop_Id);
4440
4441 -- The prefix must denote a static entity if the pragma does not
ea0f1fc8
AC
4442 -- apply to the innermost enclosing loop statement, or if it appears
4443 -- within a potentially unevaluated epxression.
150ac76e 4444
ea0f1fc8
AC
4445 if Is_Entity_Name (P)
4446 or else Nkind (Parent (P)) = N_Object_Renaming_Declaration
150ac76e 4447 then
ea0f1fc8
AC
4448 null;
4449
4450 elsif Present (Enclosing_Loop)
6782b1ef 4451 and then Entity (Identifier (Enclosing_Loop)) /= Loop_Id
ea0f1fc8 4452 then
6782b1ef 4453 Error_Attr_P
bf0b0e5e
AC
4454 ("prefix of attribute % that applies to outer loop must denote "
4455 & "an entity");
ea0f1fc8
AC
4456
4457 elsif Is_Potentially_Unevaluated (P) then
96e90ac1 4458 Uneval_Old_Msg;
6782b1ef
AC
4459 end if;
4460
bf0b0e5e
AC
4461 -- Replace the Loop_Entry attribute reference by its prefix if the
4462 -- related pragma is ignored. This transformation is OK with respect
4463 -- to typing because Loop_Entry's type is that of its prefix. This
4464 -- early transformation also avoids the generation of a useless loop
4465 -- entry constant.
6782b1ef
AC
4466
4467 if Is_Ignored (Enclosing_Pragma) then
4468 Rewrite (N, Relocate_Node (P));
150ac76e 4469 end if;
bf0b0e5e
AC
4470
4471 Preanalyze_And_Resolve (P);
150ac76e
AC
4472 end Loop_Entry;
4473
996ae0b0
RK
4474 -------------
4475 -- Machine --
4476 -------------
4477
4478 when Attribute_Machine =>
4479 Check_Floating_Point_Type_1;
4480 Set_Etype (N, P_Base_Type);
4481 Resolve (E1, P_Base_Type);
4482
4483 ------------------
4484 -- Machine_Emax --
4485 ------------------
4486
4487 when Attribute_Machine_Emax =>
4488 Check_Floating_Point_Type_0;
4489 Set_Etype (N, Universal_Integer);
4490
4491 ------------------
4492 -- Machine_Emin --
4493 ------------------
4494
4495 when Attribute_Machine_Emin =>
4496 Check_Floating_Point_Type_0;
4497 Set_Etype (N, Universal_Integer);
4498
4499 ----------------------
4500 -- Machine_Mantissa --
4501 ----------------------
4502
4503 when Attribute_Machine_Mantissa =>
4504 Check_Floating_Point_Type_0;
4505 Set_Etype (N, Universal_Integer);
4506
4507 -----------------------
4508 -- Machine_Overflows --
4509 -----------------------
4510
4511 when Attribute_Machine_Overflows =>
4512 Check_Real_Type;
4513 Check_E0;
4514 Set_Etype (N, Standard_Boolean);
4515
4516 -------------------
4517 -- Machine_Radix --
4518 -------------------
4519
4520 when Attribute_Machine_Radix =>
4521 Check_Real_Type;
4522 Check_E0;
4523 Set_Etype (N, Universal_Integer);
4524
65f01153
RD
4525 ----------------------
4526 -- Machine_Rounding --
4527 ----------------------
4528
4529 when Attribute_Machine_Rounding =>
4530 Check_Floating_Point_Type_1;
4531 Set_Etype (N, P_Base_Type);
4532 Resolve (E1, P_Base_Type);
4533
996ae0b0
RK
4534 --------------------
4535 -- Machine_Rounds --
4536 --------------------
4537
4538 when Attribute_Machine_Rounds =>
4539 Check_Real_Type;
4540 Check_E0;
4541 Set_Etype (N, Standard_Boolean);
4542
4543 ------------------
4544 -- Machine_Size --
4545 ------------------
4546
4547 when Attribute_Machine_Size =>
4548 Check_E0;
4549 Check_Type;
4550 Check_Not_Incomplete_Type;
4551 Set_Etype (N, Universal_Integer);
4552
4553 --------------
4554 -- Mantissa --
4555 --------------
4556
4557 when Attribute_Mantissa =>
4558 Check_E0;
4559 Check_Real_Type;
4560 Set_Etype (N, Universal_Integer);
4561
4562 ---------
4563 -- Max --
4564 ---------
4565
4566 when Attribute_Max =>
85d6bf87 4567 Min_Max;
996ae0b0 4568
85d6bf87
AC
4569 ----------------------------------
4570 -- Max_Alignment_For_Allocation --
4571 ----------------------------------
428684fd 4572
85d6bf87
AC
4573 when Attribute_Max_Size_In_Storage_Elements =>
4574 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
428684fd 4575
996ae0b0
RK
4576 ----------------------------------
4577 -- Max_Size_In_Storage_Elements --
4578 ----------------------------------
4579
85d6bf87
AC
4580 when Attribute_Max_Alignment_For_Allocation =>
4581 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements;
996ae0b0
RK
4582
4583 -----------------------
4584 -- Maximum_Alignment --
4585 -----------------------
4586
4587 when Attribute_Maximum_Alignment =>
4588 Standard_Attribute (Ttypes.Maximum_Alignment);
4589
4590 --------------------
4591 -- Mechanism_Code --
4592 --------------------
4593
4594 when Attribute_Mechanism_Code =>
996ae0b0
RK
4595 if not Is_Entity_Name (P)
4596 or else not Is_Subprogram (Entity (P))
4597 then
822033eb 4598 Error_Attr_P ("prefix of % attribute must be subprogram");
996ae0b0
RK
4599 end if;
4600
4601 Check_Either_E0_Or_E1;
4602
4603 if Present (E1) then
4604 Resolve (E1, Any_Integer);
4605 Set_Etype (E1, Standard_Integer);
4606
edab6088 4607 if not Is_OK_Static_Expression (E1) then
fbf5a39b
AC
4608 Flag_Non_Static_Expr
4609 ("expression for parameter number must be static!", E1);
4610 Error_Attr;
996ae0b0
RK
4611
4612 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
4613 or else UI_To_Int (Intval (E1)) < 0
4614 then
1b0b0f18 4615 Error_Attr ("invalid parameter number for % attribute", E1);
996ae0b0
RK
4616 end if;
4617 end if;
4618
4619 Set_Etype (N, Universal_Integer);
4620
4621 ---------
4622 -- Min --
4623 ---------
4624
4625 when Attribute_Min =>
85d6bf87 4626 Min_Max;
428684fd 4627
5f3ab6fb
AC
4628 ---------
4629 -- Mod --
4630 ---------
4631
4632 when Attribute_Mod =>
4633
4634 -- Note: this attribute is only allowed in Ada 2005 mode, but
4635 -- we do not need to test that here, since Mod is only recognized
4636 -- as an attribute name in Ada 2005 mode during the parse.
4637
4638 Check_E1;
4639 Check_Modular_Integer_Type;
4640 Resolve (E1, Any_Integer);
4641 Set_Etype (N, P_Base_Type);
4642
996ae0b0
RK
4643 -----------
4644 -- Model --
4645 -----------
4646
4647 when Attribute_Model =>
4648 Check_Floating_Point_Type_1;
4649 Set_Etype (N, P_Base_Type);
4650 Resolve (E1, P_Base_Type);
4651
4652 ----------------
4653 -- Model_Emin --
4654 ----------------
4655
4656 when Attribute_Model_Emin =>
4657 Check_Floating_Point_Type_0;
4658 Set_Etype (N, Universal_Integer);
4659
4660 -------------------
4661 -- Model_Epsilon --
4662 -------------------
4663
4664 when Attribute_Model_Epsilon =>
4665 Check_Floating_Point_Type_0;
4666 Set_Etype (N, Universal_Real);
4667
4668 --------------------
4669 -- Model_Mantissa --
4670 --------------------
4671
4672 when Attribute_Model_Mantissa =>
4673 Check_Floating_Point_Type_0;
4674 Set_Etype (N, Universal_Integer);
4675
4676 -----------------
4677 -- Model_Small --
4678 -----------------
4679
4680 when Attribute_Model_Small =>
4681 Check_Floating_Point_Type_0;
4682 Set_Etype (N, Universal_Real);
4683
4684 -------------
4685 -- Modulus --
4686 -------------
4687
4688 when Attribute_Modulus =>
4689 Check_E0;
5f3ab6fb 4690 Check_Modular_Integer_Type;
996ae0b0
RK
4691 Set_Etype (N, Universal_Integer);
4692
4693 --------------------
4694 -- Null_Parameter --
4695 --------------------
4696
4697 when Attribute_Null_Parameter => Null_Parameter : declare
4698 Parnt : constant Node_Id := Parent (N);
4699 GParnt : constant Node_Id := Parent (Parnt);
4700
4701 procedure Bad_Null_Parameter (Msg : String);
4702 -- Used if bad Null parameter attribute node is found. Issues
4703 -- given error message, and also sets the type to Any_Type to
4704 -- avoid blowups later on from dealing with a junk node.
4705
4706 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
4707 -- Called to check that Proc_Ent is imported subprogram
4708
4709 ------------------------
4710 -- Bad_Null_Parameter --
4711 ------------------------
4712
4713 procedure Bad_Null_Parameter (Msg : String) is
4714 begin
4715 Error_Msg_N (Msg, N);
4716 Set_Etype (N, Any_Type);
4717 end Bad_Null_Parameter;
4718
4719 ----------------------
4720 -- Must_Be_Imported --
4721 ----------------------
4722
4723 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
b81a5940 4724 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
996ae0b0
RK
4725
4726 begin
996ae0b0
RK
4727 -- Ignore check if procedure not frozen yet (we will get
4728 -- another chance when the default parameter is reanalyzed)
4729
4730 if not Is_Frozen (Pent) then
4731 return;
4732
4733 elsif not Is_Imported (Pent) then
4734 Bad_Null_Parameter
4735 ("Null_Parameter can only be used with imported subprogram");
4736
4737 else
4738 return;
4739 end if;
4740 end Must_Be_Imported;
4741
4742 -- Start of processing for Null_Parameter
4743
4744 begin
4745 Check_Type;
4746 Check_E0;
4747 Set_Etype (N, P_Type);
4748
4749 -- Case of attribute used as default expression
4750
4751 if Nkind (Parnt) = N_Parameter_Specification then
4752 Must_Be_Imported (Defining_Entity (GParnt));
4753
4754 -- Case of attribute used as actual for subprogram (positional)
4755
d3b00ce3 4756 elsif Nkind (Parnt) in N_Subprogram_Call
996ae0b0
RK
4757 and then Is_Entity_Name (Name (Parnt))
4758 then
4759 Must_Be_Imported (Entity (Name (Parnt)));
4760
4761 -- Case of attribute used as actual for subprogram (named)
4762
4763 elsif Nkind (Parnt) = N_Parameter_Association
d3b00ce3 4764 and then Nkind (GParnt) in N_Subprogram_Call
996ae0b0
RK
4765 and then Is_Entity_Name (Name (GParnt))
4766 then
4767 Must_Be_Imported (Entity (Name (GParnt)));
4768
4769 -- Not an allowed case
4770
4771 else
4772 Bad_Null_Parameter
4773 ("Null_Parameter must be actual or default parameter");
4774 end if;
996ae0b0
RK
4775 end Null_Parameter;
4776
4777 -----------------
4778 -- Object_Size --
4779 -----------------
4780
4781 when Attribute_Object_Size =>
4782 Check_E0;
4783 Check_Type;
4784 Check_Not_Incomplete_Type;
4785 Set_Etype (N, Universal_Integer);
4786
e10dab7f
JM
4787 ---------
4788 -- Old --
4789 ---------
4790
8a0320ad 4791 when Attribute_Old => Old : declare
2c9f8c0a
AC
4792 procedure Check_References_In_Prefix (Subp_Id : Entity_Id);
4793 -- Inspect the contents of the prefix and detect illegal uses of a
4794 -- nested 'Old, attribute 'Result or a use of an entity declared in
4795 -- the related postcondition expression. Subp_Id is the subprogram to
4796 -- which the related postcondition applies.
4797
2c9f8c0a
AC
4798 --------------------------------
4799 -- Check_References_In_Prefix --
4800 --------------------------------
4801
4802 procedure Check_References_In_Prefix (Subp_Id : Entity_Id) is
4803 function Check_Reference (Nod : Node_Id) return Traverse_Result;
4804 -- Detect attribute 'Old, attribute 'Result of a use of an entity
4805 -- and perform the appropriate semantic check.
4806
4807 ---------------------
4808 -- Check_Reference --
4809 ---------------------
4810
4811 function Check_Reference (Nod : Node_Id) return Traverse_Result is
4812 begin
4813 -- Attributes 'Old and 'Result cannot appear in the prefix of
4814 -- another attribute 'Old.
4815
4816 if Nkind (Nod) = N_Attribute_Reference
4817 and then Nam_In (Attribute_Name (Nod), Name_Old,
4818 Name_Result)
4819 then
4820 Error_Msg_Name_1 := Attribute_Name (Nod);
4821 Error_Msg_Name_2 := Name_Old;
4822 Error_Msg_N
4823 ("attribute % cannot appear in the prefix of attribute %",
4824 Nod);
4825 return Abandon;
4826
4827 -- Entities mentioned within the prefix of attribute 'Old must
4828 -- be global to the related postcondition. If this is not the
e0f63680 4829 -- case, then the scope of the local entity is nested within
2c9f8c0a
AC
4830 -- that of the subprogram.
4831
b6a56408 4832 elsif Is_Entity_Name (Nod)
2c9f8c0a
AC
4833 and then Present (Entity (Nod))
4834 and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
4835 then
4836 Error_Attr
4837 ("prefix of attribute % cannot reference local entities",
4838 Nod);
4839 return Abandon;
b6a56408
AC
4840
4841 -- Otherwise keep inspecting the prefix
4842
2c9f8c0a
AC
4843 else
4844 return OK;
4845 end if;
4846 end Check_Reference;
4847
4848 procedure Check_References is new Traverse_Proc (Check_Reference);
4849
4850 -- Start of processing for Check_References_In_Prefix
4851
4852 begin
4853 Check_References (P);
4854 end Check_References_In_Prefix;
4855
2c9f8c0a
AC
4856 -- Local variables
4857
b6a56408
AC
4858 Legal : Boolean;
4859 Pref_Id : Entity_Id;
4860 Pref_Typ : Entity_Id;
4861 Spec_Id : Entity_Id;
8a0320ad 4862
2c9f8c0a
AC
4863 -- Start of processing for Old
4864
8a0320ad 4865 begin
b6a56408
AC
4866 -- The attribute reference is a primary. If any expressions follow,
4867 -- then the attribute reference is an indexable object. Transform the
4868 -- attribute into an indexed component and analyze it.
8a0320ad 4869
b6a56408
AC
4870 if Present (E1) then
4871 Rewrite (N,
4872 Make_Indexed_Component (Loc,
4873 Prefix =>
4874 Make_Attribute_Reference (Loc,
4875 Prefix => Relocate_Node (P),
4876 Attribute_Name => Name_Old),
4877 Expressions => Expressions (N)));
4878 Analyze (N);
4879 return;
4880 end if;
26df19ce 4881
b6a56408 4882 Analyze_Attribute_Old_Result (Legal, Spec_Id);
48b0da2d 4883
b6a56408
AC
4884 -- The aspect or pragma where attribute 'Old resides should be
4885 -- associated with a subprogram declaration or a body. If this is not
4886 -- the case, then the aspect or pragma is illegal. Return as analysis
4887 -- cannot be carried out.
48b0da2d 4888
64f5d139
JM
4889 -- The exception to this rule is when generating C since in this case
4890 -- postconditions are inlined.
4891
4892 if No (Spec_Id)
4893 and then Modify_Tree_For_C
4894 and then In_Inlined_Body
4895 then
4896 Spec_Id := Entity (P);
4897
4898 elsif not Legal then
48b0da2d 4899 return;
b6a56408 4900 end if;
48b0da2d 4901
b6a56408
AC
4902 -- The prefix must be preanalyzed as the full analysis will take
4903 -- place during expansion.
f40f731b 4904
b6a56408 4905 Preanalyze_And_Resolve (P);
f40f731b 4906
b6a56408 4907 -- Ensure that the prefix does not contain attributes 'Old or 'Result
36eef04a 4908
b6a56408 4909 Check_References_In_Prefix (Spec_Id);
8a0320ad 4910
b6a56408 4911 -- Set the type of the attribute now to prevent cascaded errors
2eb87017 4912
b6a56408
AC
4913 Pref_Typ := Etype (P);
4914 Set_Etype (N, Pref_Typ);
36eef04a 4915
b6a56408 4916 -- Legality checks
2eb87017 4917
b6a56408
AC
4918 if Is_Limited_Type (Pref_Typ) then
4919 Error_Attr ("attribute % cannot apply to limited objects", P);
4920 end if;
2c9f8c0a 4921
b6a56408 4922 -- The prefix is a simple name
8a0320ad 4923
b6a56408
AC
4924 if Is_Entity_Name (P) and then Present (Entity (P)) then
4925 Pref_Id := Entity (P);
8a0320ad 4926
b6a56408
AC
4927 -- Emit a warning when the prefix is a constant. Note that the use
4928 -- of Error_Attr would reset the type of N to Any_Type even though
4929 -- this is a warning. Use Error_Msg_XXX instead.
8a0320ad 4930
b6a56408
AC
4931 if Is_Constant_Object (Pref_Id) then
4932 Error_Msg_Name_1 := Name_Old;
4933 Error_Msg_N
26f36fc9 4934 ("??attribute % applied to constant has no effect", P);
8a0320ad 4935 end if;
8a0320ad 4936
b6a56408 4937 -- Otherwise the prefix is not a simple name
229db351 4938
b6a56408
AC
4939 else
4940 -- Ensure that the prefix of attribute 'Old is an entity when it
4941 -- is potentially unevaluated (6.1.1 (27/3)).
d2b4b3da 4942
b6a56408
AC
4943 if Is_Potentially_Unevaluated (N) then
4944 Uneval_Old_Msg;
d2b4b3da 4945
b6a56408
AC
4946 -- Detect a possible infinite recursion when the prefix denotes
4947 -- the related function.
e10dab7f 4948
b6a56408
AC
4949 -- function Func (...) return ...
4950 -- with Post => Func'Old ...;
71a555b3 4951
08f52d9f
AC
4952 -- The function may be specified in qualified form X.Y where X is
4953 -- a protected object and Y is a protected function. In that case
4954 -- ensure that the qualified form has an entity.
4955
4956 elsif Nkind (P) = N_Function_Call
4957 and then Nkind (Name (P)) in N_Has_Entity
4958 then
b6a56408 4959 Pref_Id := Entity (Name (P));
c733429f 4960
b6a56408
AC
4961 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
4962 and then Pref_Id = Spec_Id
4963 then
4964 Error_Msg_Warn := SPARK_Mode /= On;
4965 Error_Msg_N ("!possible infinite recursion<<", P);
4966 Error_Msg_N ("\!??Storage_Error ]<<", P);
4967 end if;
4968 end if;
c733429f 4969
b6a56408
AC
4970 -- The prefix of attribute 'Old may refer to a component of a
4971 -- formal parameter. In this case its expansion may generate
4972 -- actual subtypes that are referenced in an inner context and
4973 -- that must be elaborated within the subprogram itself. If the
4974 -- prefix includes a function call, it may involve finalization
4975 -- actions that should be inserted when the attribute has been
4976 -- rewritten as a declaration. Create a declaration for the prefix
4977 -- and insert it at the start of the enclosing subprogram. This is
4978 -- an expansion activity that has to be performed now to prevent
4979 -- out-of-order issues.
4980
4981 -- This expansion is both harmful and not needed in SPARK mode,
4982 -- since the formal verification backend relies on the types of
4983 -- nodes (hence is not robust w.r.t. a change to base type here),
4984 -- and does not suffer from the out-of-order issue described
4985 -- above. Thus, this expansion is skipped in SPARK mode.
4986
2cc2e964
AC
4987 -- The expansion is not relevant for discrete types, which will
4988 -- not generate extra declarations, and where use of the base type
4989 -- may lead to spurious errors if context is a case.
3ba1a9eb 4990
b6a56408 4991 if not GNATprove_Mode then
3ba1a9eb
AC
4992 if not Is_Discrete_Type (Pref_Typ) then
4993 Pref_Typ := Base_Type (Pref_Typ);
4994 end if;
4995
b6a56408
AC
4996 Set_Etype (N, Pref_Typ);
4997 Set_Etype (P, Pref_Typ);
4998
4999 Analyze_Dimension (N);
5000 Expand (N);
5001 end if;
d2b4b3da 5002 end if;
8a0320ad 5003 end Old;
d2b4b3da 5004
2d42e881
ES
5005 ----------------------
5006 -- Overlaps_Storage --
5007 ----------------------
5008
5009 when Attribute_Overlaps_Storage =>
5010 Check_E1;
5011
5012 -- Both arguments must be objects of any type
5013
5014 Analyze_And_Resolve (P);
5015 Analyze_And_Resolve (E1);
5016 Check_Object_Reference (P);
5017 Check_Object_Reference (E1);
5018 Set_Etype (N, Standard_Boolean);
5019
996ae0b0
RK
5020 ------------
5021 -- Output --
5022 ------------
5023
5024 when Attribute_Output =>
5025 Check_E2;
fbf5a39b 5026 Check_Stream_Attribute (TSS_Stream_Output);
996ae0b0 5027 Set_Etype (N, Standard_Void_Type);
996ae0b0
RK
5028 Resolve (N, Standard_Void_Type);
5029
5030 ------------------
5031 -- Partition_ID --
5032 ------------------
5033
e10dab7f
JM
5034 when Attribute_Partition_ID => Partition_Id :
5035 begin
996ae0b0
RK
5036 Check_E0;
5037
5038 if P_Type /= Any_Type then
5039 if not Is_Library_Level_Entity (Entity (P)) then
822033eb
HK
5040 Error_Attr_P
5041 ("prefix of % attribute must be library-level entity");
996ae0b0 5042
e10dab7f
JM
5043 -- The defining entity of prefix should not be declared inside a
5044 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
996ae0b0
RK
5045
5046 elsif Is_Entity_Name (P)
5047 and then Is_Pure (Entity (P))
5048 then
229db351 5049 Error_Attr_P ("prefix of% attribute must not be declared pure");
996ae0b0
RK
5050 end if;
5051 end if;
5052
5053 Set_Etype (N, Universal_Integer);
e10dab7f 5054 end Partition_Id;
996ae0b0
RK
5055
5056 -------------------------
5057 -- Passed_By_Reference --
5058 -------------------------
5059
5060 when Attribute_Passed_By_Reference =>
5061 Check_E0;
5062 Check_Type;
5063 Set_Etype (N, Standard_Boolean);
5064
fbf5a39b
AC
5065 ------------------
5066 -- Pool_Address --
5067 ------------------
5068
5069 when Attribute_Pool_Address =>
5070 Check_E0;
5071 Set_Etype (N, RTE (RE_Address));
5072
996ae0b0
RK
5073 ---------
5074 -- Pos --
5075 ---------
5076
5077 when Attribute_Pos =>
5078 Check_Discrete_Type;
5079 Check_E1;
7a489a2b
AC
5080
5081 if Is_Boolean_Type (P_Type) then
5082 Error_Msg_Name_1 := Aname;
5083 Error_Msg_Name_2 := Chars (P_Type);
ce5ba43a 5084 Check_SPARK_05_Restriction
7a489a2b
AC
5085 ("attribute% is not allowed for type%", P);
5086 end if;
5087
996ae0b0
RK
5088 Resolve (E1, P_Base_Type);
5089 Set_Etype (N, Universal_Integer);
5090
5091 --------------
5092 -- Position --
5093 --------------
5094
5095 when Attribute_Position =>
5096 Check_Component;
5097 Set_Etype (N, Universal_Integer);
5098
5099 ----------
5100 -- Pred --
5101 ----------
5102
5103 when Attribute_Pred =>
5104 Check_Scalar_Type;
5105 Check_E1;
7a489a2b
AC
5106
5107 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5108 Error_Msg_Name_1 := Aname;
5109 Error_Msg_Name_2 := Chars (P_Type);
ce5ba43a
AC
5110 Check_SPARK_05_Restriction
5111 ("attribute% is not allowed for type%", P);
7a489a2b
AC
5112 end if;
5113
996ae0b0
RK
5114 Resolve (E1, P_Base_Type);
5115 Set_Etype (N, P_Base_Type);
5116
9ab5d86b
RD
5117 -- Since Pred works on the base type, we normally do no check for the
5118 -- floating-point case, since the base type is unconstrained. But we
5119 -- make an exception in Check_Float_Overflow mode.
996ae0b0 5120
6cade1b0 5121 if Is_Floating_Point_Type (P_Type) then
d26d790d
AC
5122 if not Range_Checks_Suppressed (P_Base_Type) then
5123 Set_Do_Range_Check (E1);
0083dd66 5124 end if;
996ae0b0
RK
5125
5126 -- If not modular type, test for overflow check required
5127
5128 else
5129 if not Is_Modular_Integer_Type (P_Type)
5130 and then not Range_Checks_Suppressed (P_Base_Type)
5131 then
5132 Enable_Range_Check (E1);
5133 end if;
5134 end if;
5135
468c6c8a
ES
5136 --------------
5137 -- Priority --
5138 --------------
5139
5140 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5141
5142 when Attribute_Priority =>
0791fbe9 5143 if Ada_Version < Ada_2005 then
468c6c8a
ES
5144 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
5145 end if;
5146
5147 Check_E0;
5148
5149 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5150
5151 Analyze (P);
5152
5153 if Is_Protected_Type (Etype (P))
5154 or else (Is_Access_Type (Etype (P))
5155 and then Is_Protected_Type (Designated_Type (Etype (P))))
5156 then
5157 Resolve (P, Etype (P));
5158 else
822033eb 5159 Error_Attr_P ("prefix of % attribute must be a protected object");
468c6c8a
ES
5160 end if;
5161
5162 Set_Etype (N, Standard_Integer);
5163
5164 -- Must be called from within a protected procedure or entry of the
5165 -- protected object.
5166
5167 declare
5168 S : Entity_Id;
5169
5170 begin
5171 S := Current_Scope;
5172 while S /= Etype (P)
5173 and then S /= Standard_Standard
5174 loop
5175 S := Scope (S);
5176 end loop;
5177
5178 if S = Standard_Standard then
5179 Error_Attr ("the attribute % is only allowed inside protected "
5180 & "operations", P);
5181 end if;
5182 end;
5183
5184 Validate_Non_Static_Attribute_Function_Call;
5185
996ae0b0
RK
5186 -----------
5187 -- Range --
5188 -----------
5189
5190 when Attribute_Range =>
5191 Check_Array_Or_Scalar_Type;
26df19ce 5192 Bad_Attribute_For_Predicate;
996ae0b0 5193
0ab80019 5194 if Ada_Version = Ada_83
996ae0b0
RK
5195 and then Is_Scalar_Type (P_Type)
5196 and then Comes_From_Source (N)
5197 then
5198 Error_Attr
5199 ("(Ada 83) % attribute not allowed for scalar type", P);
5200 end if;
5201
21d27997
RD
5202 ------------
5203 -- Result --
5204 ------------
5205
5206 when Attribute_Result => Result : declare
7c76aa3f
HK
5207 function Denote_Same_Function
5208 (Pref_Id : Entity_Id;
5209 Spec_Id : Entity_Id) return Boolean;
5210 -- Determine whether the entity of the prefix Pref_Id denotes the
5211 -- same entity as that of the related subprogram Spec_Id.
5212
7c76aa3f
HK
5213 --------------------------
5214 -- Denote_Same_Function --
5215 --------------------------
5216
5217 function Denote_Same_Function
5218 (Pref_Id : Entity_Id;
5219 Spec_Id : Entity_Id) return Boolean
5220 is
4afcf3a5
AC
5221 Over_Id : constant Entity_Id := Overridden_Operation (Spec_Id);
5222 Subp_Spec : constant Node_Id := Parent (Spec_Id);
7c76aa3f
HK
5223
5224 begin
5225 -- The prefix denotes the related subprogram
5226
5227 if Pref_Id = Spec_Id then
5228 return True;
5229
5230 -- Account for a special case when attribute 'Result appears in
5231 -- the postcondition of a generic function.
5232
5233 -- generic
5234 -- function Gen_Func return ...
5235 -- with Post => Gen_Func'Result ...;
5236
5237 -- When the generic function is instantiated, the Chars field of
5238 -- the instantiated prefix still denotes the name of the generic
5239 -- function. Note that any preemptive transformation is impossible
5240 -- without a proper analysis. The structure of the wrapper package
5241 -- is as follows:
5242
5243 -- package Anon_Gen_Pack is
5244 -- <subtypes and renamings>
5245 -- function Subp_Decl return ...; -- (!)
5246 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5247 -- function Gen_Func ... renames Subp_Decl;
5248 -- end Anon_Gen_Pack;
5249
5250 elsif Nkind (Subp_Spec) = N_Function_Specification
5251 and then Present (Generic_Parent (Subp_Spec))
caf07df9 5252 and then Ekind_In (Pref_Id, E_Generic_Function, E_Function)
7c76aa3f 5253 then
caf07df9
AC
5254 if Generic_Parent (Subp_Spec) = Pref_Id then
5255 return True;
5256
5257 elsif Present (Alias (Pref_Id))
5258 and then Alias (Pref_Id) = Spec_Id
5259 then
5260 return True;
5261 end if;
4afcf3a5
AC
5262
5263 -- Account for a special case where a primitive of a tagged type
5264 -- inherits a class-wide postcondition from a parent type. In this
5265 -- case the prefix of attribute 'Result denotes the overriding
5266 -- primitive.
5267
5268 elsif Present (Over_Id) and then Pref_Id = Over_Id then
5269 return True;
caf07df9 5270 end if;
7c76aa3f
HK
5271
5272 -- Otherwise the prefix does not denote the related subprogram
5273
caf07df9 5274 return False;
7c76aa3f
HK
5275 end Denote_Same_Function;
5276
c9d70ab1 5277 -- Local variables
f40f731b 5278
b6a56408
AC
5279 Legal : Boolean;
5280 Pref_Id : Entity_Id;
5281 Spec_Id : Entity_Id;
36eef04a 5282
c9d70ab1 5283 -- Start of processing for Result
dac3bede 5284
c9d70ab1
AC
5285 begin
5286 -- The attribute reference is a primary. If any expressions follow,
5287 -- then the attribute reference is an indexable object. Transform the
5288 -- attribute into an indexed component and analyze it.
dac3bede 5289
c9d70ab1
AC
5290 if Present (E1) then
5291 Rewrite (N,
5292 Make_Indexed_Component (Loc,
5293 Prefix =>
5294 Make_Attribute_Reference (Loc,
5295 Prefix => Relocate_Node (P),
5296 Attribute_Name => Name_Result),
5297 Expressions => Expressions (N)));
5298 Analyze (N);
5299 return;
5300 end if;
36eef04a 5301
b6a56408 5302 Analyze_Attribute_Old_Result (Legal, Spec_Id);
21d27997 5303
b6a56408
AC
5304 -- The aspect or pragma where attribute 'Result resides should be
5305 -- associated with a subprogram declaration or a body. If this is not
5306 -- the case, then the aspect or pragma is illegal. Return as analysis
5307 -- cannot be carried out.
7c76aa3f 5308
64f5d139
JM
5309 -- The exception to this rule is when generating C since in this case
5310 -- postconditions are inlined.
5311
5312 if No (Spec_Id)
5313 and then Modify_Tree_For_C
5314 and then In_Inlined_Body
5315 then
5316 Spec_Id := Entity (P);
5317
5318 elsif not Legal then
7c76aa3f
HK
5319 return;
5320 end if;
a905304c 5321
7c76aa3f
HK
5322 -- Attribute 'Result is part of a _Postconditions procedure. There is
5323 -- no need to perform the semantic checks below as they were already
5324 -- verified when the attribute was analyzed in its original context.
5325 -- Instead, rewrite the attribute as a reference to formal parameter
5326 -- _Result of the _Postconditions procedure.
e7f23f06 5327
7c76aa3f
HK
5328 if Chars (Spec_Id) = Name_uPostconditions then
5329 Rewrite (N, Make_Identifier (Loc, Name_uResult));
a905304c 5330
7c76aa3f
HK
5331 -- The type of formal parameter _Result is that of the function
5332 -- encapsulating the _Postconditions procedure. Resolution must
5333 -- be carried out against the function return type.
e7f23f06 5334
7c76aa3f 5335 Analyze_And_Resolve (N, Etype (Scope (Spec_Id)));
e7f23f06 5336
7c76aa3f
HK
5337 -- Otherwise attribute 'Result appears in its original context and
5338 -- all semantic checks should be carried out.
21d27997 5339
7c76aa3f
HK
5340 else
5341 -- Verify the legality of the prefix. It must denotes the entity
5342 -- of the related [generic] function.
c9d70ab1
AC
5343
5344 if Is_Entity_Name (P) then
5345 Pref_Id := Entity (P);
5346
3386e3ae
AC
5347 if Ekind_In (Pref_Id, E_Function, E_Generic_Function)
5348 and then Ekind (Spec_Id) = Ekind (Pref_Id)
5349 then
7c76aa3f 5350 if Denote_Same_Function (Pref_Id, Spec_Id) then
caf07df9
AC
5351
5352 -- Correct the prefix of the attribute when the context
5353 -- is a generic function.
5354
5355 if Pref_Id /= Spec_Id then
5356 Rewrite (P, New_Occurrence_Of (Spec_Id, Loc));
5357 Analyze (P);
5358 end if;
5359
7c76aa3f 5360 Set_Etype (N, Etype (Spec_Id));
8d9509fd 5361
7c76aa3f 5362 -- Otherwise the prefix denotes some unrelated function
c9d70ab1 5363
7c76aa3f
HK
5364 else
5365 Error_Msg_Name_2 := Chars (Spec_Id);
5366 Error_Attr
b6a56408 5367 ("incorrect prefix for attribute %, expected %", P);
c9d70ab1
AC
5368 end if;
5369
7c76aa3f
HK
5370 -- Otherwise the prefix denotes some other form of subprogram
5371 -- entity.
21d27997 5372
8d9509fd 5373 else
e7f23f06 5374 Error_Attr
b6a56408 5375 ("attribute % can only appear in postcondition of "
c9d70ab1 5376 & "function", P);
8d9509fd 5377 end if;
21d27997 5378
c9d70ab1
AC
5379 -- Otherwise the prefix is illegal
5380
5381 else
5382 Error_Msg_Name_2 := Chars (Spec_Id);
b6a56408 5383 Error_Attr ("incorrect prefix for attribute %, expected %", P);
c9d70ab1 5384 end if;
21d27997
RD
5385 end if;
5386 end Result;
5387
996ae0b0
RK
5388 ------------------
5389 -- Range_Length --
5390 ------------------
5391
5392 when Attribute_Range_Length =>
e10dab7f 5393 Check_E0;
996ae0b0
RK
5394 Check_Discrete_Type;
5395 Set_Etype (N, Universal_Integer);
5396
5397 ----------
5398 -- Read --
5399 ----------
5400
5401 when Attribute_Read =>
5402 Check_E2;
fbf5a39b 5403 Check_Stream_Attribute (TSS_Stream_Read);
996ae0b0
RK
5404 Set_Etype (N, Standard_Void_Type);
5405 Resolve (N, Standard_Void_Type);
21d27997 5406 Note_Possible_Modification (E2, Sure => True);
996ae0b0 5407
1b0b0f18
AC
5408 ---------
5409 -- Ref --
5410 ---------
5411
5412 when Attribute_Ref =>
5413 Check_E1;
5414 Analyze (P);
5415
5416 if Nkind (P) /= N_Expanded_Name
5417 or else not Is_RTE (P_Type, RE_Address)
5418 then
5419 Error_Attr_P ("prefix of % attribute must be System.Address");
5420 end if;
5421
5422 Analyze_And_Resolve (E1, Any_Integer);
5423 Set_Etype (N, RTE (RE_Address));
5424
996ae0b0
RK
5425 ---------------
5426 -- Remainder --
5427 ---------------
5428
5429 when Attribute_Remainder =>
5430 Check_Floating_Point_Type_2;
5431 Set_Etype (N, P_Base_Type);
5432 Resolve (E1, P_Base_Type);
5433 Resolve (E2, P_Base_Type);
5434
2cbac6c6
AC
5435 ---------------------
5436 -- Restriction_Set --
5437 ---------------------
5438
5439 when Attribute_Restriction_Set => Restriction_Set : declare
5440 R : Restriction_Id;
5441 U : Node_Id;
5442 Unam : Unit_Name_Type;
5443
2cbac6c6
AC
5444 begin
5445 Check_E1;
5446 Analyze (P);
f7ea2603 5447 Check_System_Prefix;
2cbac6c6
AC
5448
5449 -- No_Dependence case
5450
5451 if Nkind (E1) = N_Parameter_Association then
5452 pragma Assert (Chars (Selector_Name (E1)) = Name_No_Dependence);
5453 U := Explicit_Actual_Parameter (E1);
5454
5455 if not OK_No_Dependence_Unit_Name (U) then
f7ea2603 5456 Set_Boolean_Result (N, False);
2cbac6c6
AC
5457 Error_Attr;
5458 end if;
5459
5460 -- See if there is an entry already in the table. That's the
5461 -- case in which we can return True.
5462
5463 for J in No_Dependences.First .. No_Dependences.Last loop
5464 if Designate_Same_Unit (U, No_Dependences.Table (J).Unit)
5465 and then No_Dependences.Table (J).Warn = False
5466 then
f7ea2603 5467 Set_Boolean_Result (N, True);
2cbac6c6
AC
5468 return;
5469 end if;
5470 end loop;
5471
5472 -- If not in the No_Dependence table, result is False
5473
f7ea2603 5474 Set_Boolean_Result (N, False);
2cbac6c6
AC
5475
5476 -- In this case, we must ensure that the binder will reject any
5477 -- other unit in the partition that sets No_Dependence for this
5478 -- unit. We do that by making an entry in the special table kept
5479 -- for this purpose (if the entry is not there already).
5480
5481 Unam := Get_Spec_Name (Get_Unit_Name (U));
5482
5483 for J in Restriction_Set_Dependences.First ..
5484 Restriction_Set_Dependences.Last
5485 loop
5486 if Restriction_Set_Dependences.Table (J) = Unam then
5487 return;
5488 end if;
5489 end loop;
5490
5491 Restriction_Set_Dependences.Append (Unam);
5492
5493 -- Normal restriction case
5494
5495 else
5496 if Nkind (E1) /= N_Identifier then
f7ea2603 5497 Set_Boolean_Result (N, False);
2cbac6c6
AC
5498 Error_Attr ("attribute % requires restriction identifier", E1);
5499
5500 else
5501 R := Get_Restriction_Id (Process_Restriction_Synonyms (E1));
5502
5503 if R = Not_A_Restriction_Id then
f7ea2603 5504 Set_Boolean_Result (N, False);
2cbac6c6
AC
5505 Error_Msg_Node_1 := E1;
5506 Error_Attr ("invalid restriction identifier &", E1);
5507
5508 elsif R not in Partition_Boolean_Restrictions then
f7ea2603 5509 Set_Boolean_Result (N, False);
2cbac6c6
AC
5510 Error_Msg_Node_1 := E1;
5511 Error_Attr
5512 ("& is not a boolean partition-wide restriction", E1);
5513 end if;
5514
5515 if Restriction_Active (R) then
f7ea2603 5516 Set_Boolean_Result (N, True);
2cbac6c6
AC
5517 else
5518 Check_Restriction (R, N);
f7ea2603 5519 Set_Boolean_Result (N, False);
2cbac6c6
AC
5520 end if;
5521 end if;
5522 end if;
5523 end Restriction_Set;
5524
996ae0b0
RK
5525 -----------
5526 -- Round --
5527 -----------
5528
5529 when Attribute_Round =>
5530 Check_E1;
5531 Check_Decimal_Fixed_Point_Type;
5532 Set_Etype (N, P_Base_Type);
5533
29ba9f52
RD
5534 -- Because the context is universal_real (3.5.10(12)) it is a
5535 -- legal context for a universal fixed expression. This is the
5536 -- only attribute whose functional description involves U_R.
996ae0b0
RK
5537
5538 if Etype (E1) = Universal_Fixed then
5539 declare
5540 Conv : constant Node_Id := Make_Type_Conversion (Loc,
5541 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
5542 Expression => Relocate_Node (E1));
5543
5544 begin
5545 Rewrite (E1, Conv);
5546 Analyze (E1);
5547 end;
5548 end if;
5549
5550 Resolve (E1, Any_Real);
5551
5552 --------------
5553 -- Rounding --
5554 --------------
5555
5556 when Attribute_Rounding =>
5557 Check_Floating_Point_Type_1;
5558 Set_Etype (N, P_Base_Type);
5559 Resolve (E1, P_Base_Type);
5560
5561 ---------------
5562 -- Safe_Emax --
5563 ---------------
5564
5565 when Attribute_Safe_Emax =>
5566 Check_Floating_Point_Type_0;
5567 Set_Etype (N, Universal_Integer);
5568
5569 ----------------
5570 -- Safe_First --
5571 ----------------
5572
5573 when Attribute_Safe_First =>
5574 Check_Floating_Point_Type_0;
5575 Set_Etype (N, Universal_Real);
5576
5577 ----------------
5578 -- Safe_Large --
5579 ----------------
5580
5581 when Attribute_Safe_Large =>
5582 Check_E0;
5583 Check_Real_Type;
5584 Set_Etype (N, Universal_Real);
5585
5586 ---------------
5587 -- Safe_Last --
5588 ---------------
5589
5590 when Attribute_Safe_Last =>
5591 Check_Floating_Point_Type_0;
5592 Set_Etype (N, Universal_Real);
5593
5594 ----------------
5595 -- Safe_Small --
5596 ----------------
5597
5598 when Attribute_Safe_Small =>
5599 Check_E0;
5600 Check_Real_Type;
5601 Set_Etype (N, Universal_Real);
5602
f91510fc
AC
5603 --------------------------
5604 -- Scalar_Storage_Order --
5605 --------------------------
5606
5607 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
83553466 5608 declare
02bb0765 5609 Ent : Entity_Id := Empty;
15918371 5610
f91510fc
AC
5611 begin
5612 Check_E0;
5613 Check_Type;
5614
83553466
AC
5615 if not (Is_Record_Type (P_Type) or else Is_Array_Type (P_Type)) then
5616
5617 -- In GNAT mode, the attribute applies to generic types as well
5618 -- as composite types, and for non-composite types always returns
5619 -- the default bit order for the target.
5620
5621 if not (GNAT_Mode and then Is_Generic_Type (P_Type))
02bb0765 5622 and then not In_Instance
83553466
AC
5623 then
5624 Error_Attr_P
5625 ("prefix of % attribute must be record or array type");
5626
5627 elsif not Is_Generic_Type (P_Type) then
5628 if Bytes_Big_Endian then
5629 Ent := RTE (RE_High_Order_First);
5630 else
5631 Ent := RTE (RE_Low_Order_First);
5632 end if;
5633 end if;
5634
5635 elsif Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
5636 Ent := RTE (RE_High_Order_First);
f91510fc 5637
f91510fc 5638 else
83553466
AC
5639 Ent := RTE (RE_Low_Order_First);
5640 end if;
5641
5642 if Present (Ent) then
5643 Rewrite (N, New_Occurrence_Of (Ent, Loc));
f91510fc
AC
5644 end if;
5645
5646 Set_Etype (N, RTE (RE_Bit_Order));
5647 Resolve (N);
5648
5649 -- Reset incorrect indication of staticness
5650
5651 Set_Is_Static_Expression (N, False);
5652 end Scalar_Storage_Order;
5653
996ae0b0
RK
5654 -----------
5655 -- Scale --
5656 -----------
5657
5658 when Attribute_Scale =>
5659 Check_E0;
5660 Check_Decimal_Fixed_Point_Type;
5661 Set_Etype (N, Universal_Integer);
5662
5663 -------------
5664 -- Scaling --
5665 -------------
5666
5667 when Attribute_Scaling =>
5668 Check_Floating_Point_Type_2;
5669 Set_Etype (N, P_Base_Type);
5670 Resolve (E1, P_Base_Type);
5671
5672 ------------------
5673 -- Signed_Zeros --
5674 ------------------
5675
5676 when Attribute_Signed_Zeros =>
5677 Check_Floating_Point_Type_0;
5678 Set_Etype (N, Standard_Boolean);
5679
5680 ----------
5681 -- Size --
5682 ----------
5683
e10dab7f
JM
5684 when Attribute_Size | Attribute_VADS_Size => Size :
5685 begin
996ae0b0
RK
5686 Check_E0;
5687
5d09245e
AC
5688 -- If prefix is parameterless function call, rewrite and resolve
5689 -- as such.
5690
5691 if Is_Entity_Name (P)
5692 and then Ekind (Entity (P)) = E_Function
996ae0b0 5693 then
5d09245e
AC
5694 Resolve (P);
5695
5696 -- Similar processing for a protected function call
5697
5698 elsif Nkind (P) = N_Selected_Component
5699 and then Ekind (Entity (Selector_Name (P))) = E_Function
5700 then
5701 Resolve (P);
5702 end if;
5703
5704 if Is_Object_Reference (P) then
996ae0b0
RK
5705 Check_Object_Reference (P);
5706
0873bafc 5707 elsif Is_Entity_Name (P)
c4e5e10f
TQ
5708 and then (Is_Type (Entity (P))
5709 or else Ekind (Entity (P)) = E_Enumeration_Literal)
996ae0b0 5710 then
0873bafc
GB
5711 null;
5712
5713 elsif Nkind (P) = N_Type_Conversion
5714 and then not Comes_From_Source (P)
5715 then
5716 null;
5717
5b75bf57
AC
5718 -- Some other compilers allow dubious use of X'???'Size
5719
303fbb20
AC
5720 elsif Relaxed_RM_Semantics
5721 and then Nkind (P) = N_Attribute_Reference
5722 then
5723 null;
5724
0873bafc 5725 else
822033eb 5726 Error_Attr_P ("invalid prefix for % attribute");
996ae0b0
RK
5727 end if;
5728
5729 Check_Not_Incomplete_Type;
21d27997 5730 Check_Not_CPP_Type;
996ae0b0 5731 Set_Etype (N, Universal_Integer);
e10dab7f 5732 end Size;
996ae0b0
RK
5733
5734 -----------
5735 -- Small --
5736 -----------
5737
5738 when Attribute_Small =>
5739 Check_E0;
5740 Check_Real_Type;
5741 Set_Etype (N, Universal_Real);
5742
5743 ------------------
5744 -- Storage_Pool --
5745 ------------------
5746
a8551b5f
AC
5747 when Attribute_Storage_Pool |
5748 Attribute_Simple_Storage_Pool => Storage_Pool :
e10dab7f
JM
5749 begin
5750 Check_E0;
996ae0b0 5751
e10dab7f 5752 if Is_Access_Type (P_Type) then
468c6c8a 5753 if Ekind (P_Type) = E_Access_Subprogram_Type then
822033eb
HK
5754 Error_Attr_P
5755 ("cannot use % attribute for access-to-subprogram type");
468c6c8a
ES
5756 end if;
5757
996ae0b0
RK
5758 -- Set appropriate entity
5759
5760 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
5761 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
5762 else
5763 Set_Entity (N, RTE (RE_Global_Pool_Object));
5764 end if;
5765
a8551b5f
AC
5766 if Attr_Id = Attribute_Storage_Pool then
5767 if Present (Get_Rep_Pragma (Etype (Entity (N)),
f6205414 5768 Name_Simple_Storage_Pool_Type))
a8551b5f
AC
5769 then
5770 Error_Msg_Name_1 := Aname;
43417b90 5771 Error_Msg_Warn := SPARK_Mode /= On;
324ac540 5772 Error_Msg_N ("cannot use % attribute for type with simple "
4a28b181
AC
5773 & "storage pool<<", N);
5774 Error_Msg_N ("\Program_Error [<<", N);
a8551b5f
AC
5775
5776 Rewrite
5777 (N, Make_Raise_Program_Error
5778 (Sloc (N), Reason => PE_Explicit_Raise));
5779 end if;
5780
5781 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5782
5783 -- In the Simple_Storage_Pool case, verify that the pool entity is
5784 -- actually of a simple storage pool type, and set the attribute's
5785 -- type to the pool object's type.
5786
5787 else
5788 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
f6205414 5789 Name_Simple_Storage_Pool_Type))
a8551b5f
AC
5790 then
5791 Error_Attr_P
5792 ("cannot use % attribute for type without simple " &
5793 "storage pool");
5794 end if;
5795
5796 Set_Etype (N, Etype (Entity (N)));
5797 end if;
996ae0b0
RK
5798
5799 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5800 -- Storage_Pool since this attribute is not defined for such
5801 -- types (RM E.2.3(22)).
5802
5803 Validate_Remote_Access_To_Class_Wide_Type (N);
5804
5805 else
822033eb 5806 Error_Attr_P ("prefix of % attribute must be access type");
996ae0b0 5807 end if;
e10dab7f 5808 end Storage_Pool;
996ae0b0
RK
5809
5810 ------------------
5811 -- Storage_Size --
5812 ------------------
5813
e10dab7f
JM
5814 when Attribute_Storage_Size => Storage_Size :
5815 begin
5816 Check_E0;
5817
996ae0b0 5818 if Is_Task_Type (P_Type) then
996ae0b0
RK
5819 Set_Etype (N, Universal_Integer);
5820
b5c739f9
RD
5821 -- Use with tasks is an obsolescent feature
5822
5823 Check_Restriction (No_Obsolescent_Features, P);
5824
996ae0b0 5825 elsif Is_Access_Type (P_Type) then
468c6c8a 5826 if Ekind (P_Type) = E_Access_Subprogram_Type then
822033eb
HK
5827 Error_Attr_P
5828 ("cannot use % attribute for access-to-subprogram type");
468c6c8a
ES
5829 end if;
5830
996ae0b0
RK
5831 if Is_Entity_Name (P)
5832 and then Is_Type (Entity (P))
5833 then
996ae0b0
RK
5834 Check_Type;
5835 Set_Etype (N, Universal_Integer);
5836
5837 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
5838 -- Storage_Size since this attribute is not defined for
5839 -- such types (RM E.2.3(22)).
5840
5841 Validate_Remote_Access_To_Class_Wide_Type (N);
5842
29ba9f52
RD
5843 -- The prefix is allowed to be an implicit dereference of an
5844 -- access value designating a task.
996ae0b0
RK
5845
5846 else
996ae0b0
RK
5847 Check_Task_Prefix;
5848 Set_Etype (N, Universal_Integer);
5849 end if;
5850
5851 else
822033eb 5852 Error_Attr_P ("prefix of % attribute must be access or task type");
996ae0b0 5853 end if;
e10dab7f 5854 end Storage_Size;
996ae0b0
RK
5855
5856 ------------------
5857 -- Storage_Unit --
5858 ------------------
5859
5860 when Attribute_Storage_Unit =>
5861 Standard_Attribute (Ttypes.System_Storage_Unit);
5862
82c80734
RD
5863 -----------------
5864 -- Stream_Size --
5865 -----------------
5866
5867 when Attribute_Stream_Size =>
5868 Check_E0;
5869 Check_Type;
5870
5871 if Is_Entity_Name (P)
5872 and then Is_Elementary_Type (Entity (P))
5873 then
5874 Set_Etype (N, Universal_Integer);
5875 else
822033eb 5876 Error_Attr_P ("invalid prefix for % attribute");
82c80734
RD
5877 end if;
5878
468c6c8a
ES
5879 ---------------
5880 -- Stub_Type --
5881 ---------------
5882
5883 when Attribute_Stub_Type =>
5884 Check_Type;
5885 Check_E0;
5886
25081892
AC
5887 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
5888
22243c12 5889 -- For a real RACW [sub]type, use corresponding stub type
25081892 5890
22243c12 5891 if not Is_Generic_Type (P_Type) then
25081892
AC
5892 Rewrite (N,
5893 New_Occurrence_Of
5894 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
5895
22243c12
RD
5896 -- For a generic type (that has been marked as an RACW using the
5897 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
5898 -- type. Note that if the actual is not a remote access type, the
5899 -- instantiation will fail.
25081892 5900
22243c12 5901 else
25081892
AC
5902 -- Note: we go to the underlying type here because the view
5903 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
5904
5905 Rewrite (N,
5906 New_Occurrence_Of
5907 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
5908 end if;
5909
468c6c8a 5910 else
822033eb
HK
5911 Error_Attr_P
5912 ("prefix of% attribute must be remote access to classwide");
468c6c8a
ES
5913 end if;
5914
996ae0b0
RK
5915 ----------
5916 -- Succ --
5917 ----------
5918
5919 when Attribute_Succ =>
5920 Check_Scalar_Type;
5921 Check_E1;
7a489a2b
AC
5922
5923 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
5924 Error_Msg_Name_1 := Aname;
5925 Error_Msg_Name_2 := Chars (P_Type);
ce5ba43a
AC
5926 Check_SPARK_05_Restriction
5927 ("attribute% is not allowed for type%", P);
7a489a2b
AC
5928 end if;
5929
996ae0b0
RK
5930 Resolve (E1, P_Base_Type);
5931 Set_Etype (N, P_Base_Type);
5932
9ab5d86b
RD
5933 -- Since Pred works on the base type, we normally do no check for the
5934 -- floating-point case, since the base type is unconstrained. But we
5935 -- make an exception in Check_Float_Overflow mode.
996ae0b0 5936
6cade1b0 5937 if Is_Floating_Point_Type (P_Type) then
d26d790d
AC
5938 if not Range_Checks_Suppressed (P_Base_Type) then
5939 Set_Do_Range_Check (E1);
0083dd66 5940 end if;
996ae0b0 5941
edd63e9b 5942 -- If not modular type, test for overflow check required
996ae0b0
RK
5943
5944 else
5945 if not Is_Modular_Integer_Type (P_Type)
5946 and then not Range_Checks_Suppressed (P_Base_Type)
5947 then
5948 Enable_Range_Check (E1);
5949 end if;
5950 end if;
5951
c5ecd6b7
AC
5952 --------------------------------
5953 -- System_Allocator_Alignment --
5954 --------------------------------
5955
5956 when Attribute_System_Allocator_Alignment =>
5957 Standard_Attribute (Ttypes.System_Allocator_Alignment);
5958
996ae0b0
RK
5959 ---------
5960 -- Tag --
5961 ---------
5962
e10dab7f
JM
5963 when Attribute_Tag => Tag :
5964 begin
996ae0b0
RK
5965 Check_E0;
5966 Check_Dereference;
5967
5968 if not Is_Tagged_Type (P_Type) then
822033eb 5969 Error_Attr_P ("prefix of % attribute must be tagged");
996ae0b0 5970
324ac540
AC
5971 -- Next test does not apply to generated code why not, and what does
5972 -- the illegal reference mean???
996ae0b0
RK
5973
5974 elsif Is_Object_Reference (P)
5975 and then not Is_Class_Wide_Type (P_Type)
5976 and then Comes_From_Source (N)
5977 then
822033eb
HK
5978 Error_Attr_P
5979 ("% attribute can only be applied to objects " &
5980 "of class - wide type");
996ae0b0
RK
5981 end if;
5982
324ac540
AC
5983 -- The prefix cannot be an incomplete type. However, references to
5984 -- 'Tag can be generated when expanding interface conversions, and
5985 -- this is legal.
822033eb
HK
5986
5987 if Comes_From_Source (N) then
5988 Check_Not_Incomplete_Type;
5989 end if;
7b76e805
RD
5990
5991 -- Set appropriate type
5992
996ae0b0 5993 Set_Etype (N, RTE (RE_Tag));
e10dab7f 5994 end Tag;
996ae0b0 5995
fbf5a39b
AC
5996 -----------------
5997 -- Target_Name --
5998 -----------------
5999
6000 when Attribute_Target_Name => Target_Name : declare
6001 TN : constant String := Sdefault.Target_Name.all;
1d571f3b 6002 TL : Natural;
fbf5a39b
AC
6003
6004 begin
6005 Check_Standard_Prefix;
1d571f3b
AC
6006
6007 TL := TN'Last;
fbf5a39b
AC
6008
6009 if TN (TL) = '/' or else TN (TL) = '\' then
6010 TL := TL - 1;
6011 end if;
6012
fbf5a39b
AC
6013 Rewrite (N,
6014 Make_String_Literal (Loc,
1d571f3b 6015 Strval => TN (TN'First .. TL)));
fbf5a39b 6016 Analyze_And_Resolve (N, Standard_String);
edab6088 6017 Set_Is_Static_Expression (N, True);
fbf5a39b
AC
6018 end Target_Name;
6019
996ae0b0
RK
6020 ----------------
6021 -- Terminated --
6022 ----------------
6023
6024 when Attribute_Terminated =>
6025 Check_E0;
6026 Set_Etype (N, Standard_Boolean);
6027 Check_Task_Prefix;
6028
996ae0b0
RK
6029 ----------------
6030 -- To_Address --
6031 ----------------
6032
c1645ac8
AC
6033 when Attribute_To_Address => To_Address : declare
6034 Val : Uint;
6035
6036 begin
996ae0b0
RK
6037 Check_E1;
6038 Analyze (P);
f7ea2603 6039 Check_System_Prefix;
996ae0b0
RK
6040
6041 Generate_Reference (RTE (RE_Address), P);
6042 Analyze_And_Resolve (E1, Any_Integer);
6043 Set_Etype (N, RTE (RE_Address));
6044
edab6088
RD
6045 if Is_Static_Expression (E1) then
6046 Set_Is_Static_Expression (N, True);
6047 end if;
6048
6049 -- OK static expression case, check range and set appropriate type
c1645ac8
AC
6050
6051 if Is_OK_Static_Expression (E1) then
6052 Val := Expr_Value (E1);
6053
6054 if Val < -(2 ** UI_From_Int (Standard'Address_Size - 1))
6055 or else
6056 Val > 2 ** UI_From_Int (Standard'Address_Size) - 1
6057 then
6058 Error_Attr ("address value out of range for % attribute", E1);
6059 end if;
6060
7569f697
AC
6061 -- In most cases the expression is a numeric literal or some other
6062 -- address expression, but if it is a declared constant it may be
6063 -- of a compatible type that must be left on the node.
6064
6065 if Is_Entity_Name (E1) then
6066 null;
6067
c1645ac8
AC
6068 -- Set type to universal integer if negative
6069
7569f697 6070 elsif Val < 0 then
c1645ac8
AC
6071 Set_Etype (E1, Universal_Integer);
6072
6073 -- Otherwise set type to Unsigned_64 to accomodate max values
6074
6075 else
6076 Set_Etype (E1, Standard_Unsigned_64);
6077 end if;
6078 end if;
edab6088
RD
6079
6080 Set_Is_Static_Expression (N, True);
c1645ac8
AC
6081 end To_Address;
6082
54838d1f
AC
6083 ------------
6084 -- To_Any --
6085 ------------
6086
6087 when Attribute_To_Any =>
6088 Check_E1;
6089 Check_PolyORB_Attribute;
6090 Set_Etype (N, RTE (RE_Any));
6091
996ae0b0
RK
6092 ----------------
6093 -- Truncation --
6094 ----------------
6095
6096 when Attribute_Truncation =>
6097 Check_Floating_Point_Type_1;
6098 Resolve (E1, P_Base_Type);
6099 Set_Etype (N, P_Base_Type);
6100
6101 ----------------
6102 -- Type_Class --
6103 ----------------
6104
6105 when Attribute_Type_Class =>
6106 Check_E0;
6107 Check_Type;
6108 Check_Not_Incomplete_Type;
6109 Set_Etype (N, RTE (RE_Type_Class));
6110
09494c32
AC
6111 --------------
6112 -- TypeCode --
6113 --------------
54838d1f
AC
6114
6115 when Attribute_TypeCode =>
6116 Check_E0;
6117 Check_PolyORB_Attribute;
6118 Set_Etype (N, RTE (RE_TypeCode));
6119
9c870c90
AC
6120 --------------
6121 -- Type_Key --
6122 --------------
6123
6124 when Attribute_Type_Key =>
6125 Check_E0;
6126 Check_Type;
ddc1515a
AC
6127
6128 -- This processing belongs in Eval_Attribute ???
6129
9c870c90 6130 declare
1aa23421 6131 function Type_Key return String_Id;
ddc1515a
AC
6132 -- A very preliminary implementation. For now, a signature
6133 -- consists of only the type name. This is clearly incomplete
6134 -- (e.g., adding a new field to a record type should change the
6135 -- type's Type_Key attribute).
9c870c90
AC
6136
6137 --------------
6138 -- Type_Key --
6139 --------------
6140
1aa23421 6141 function Type_Key return String_Id is
9c870c90 6142 Full_Name : constant String_Id :=
1aa23421 6143 Fully_Qualified_Name_String (Entity (P));
ddc1515a 6144
9c870c90 6145 begin
1aa23421 6146 -- Copy all characters in Full_Name but the trailing NUL
9c870c90 6147
1aa23421
AC
6148 Start_String;
6149 for J in 1 .. String_Length (Full_Name) - 1 loop
b3143037 6150 Store_String_Char (Get_String_Char (Full_Name, Pos (J)));
1aa23421 6151 end loop;
ddc1515a 6152
1aa23421
AC
6153 Store_String_Chars ("'Type_Key");
6154 return End_String;
9c870c90
AC
6155 end Type_Key;
6156
6157 begin
6158 Rewrite (N, Make_String_Literal (Loc, Type_Key));
6159 end;
6160
6161 Analyze_And_Resolve (N, Standard_String);
6162
996ae0b0
RK
6163 -----------------------
6164 -- Unbiased_Rounding --
6165 -----------------------
6166
6167 when Attribute_Unbiased_Rounding =>
6168 Check_Floating_Point_Type_1;
6169 Set_Etype (N, P_Base_Type);
6170 Resolve (E1, P_Base_Type);
6171
6172 ----------------------
6173 -- Unchecked_Access --
6174 ----------------------
6175
6176 when Attribute_Unchecked_Access =>
6177 if Comes_From_Source (N) then
6178 Check_Restriction (No_Unchecked_Access, N);
6179 end if;
6180
fbf5a39b 6181 Analyze_Access_Attribute;
22efcab7 6182 Check_Not_Incomplete_Type;
fbf5a39b
AC
6183
6184 -------------------------
6185 -- Unconstrained_Array --
6186 -------------------------
6187
6188 when Attribute_Unconstrained_Array =>
6189 Check_E0;
6190 Check_Type;
6191 Check_Not_Incomplete_Type;
6192 Set_Etype (N, Standard_Boolean);
edab6088 6193 Set_Is_Static_Expression (N, True);
996ae0b0
RK
6194
6195 ------------------------------
6196 -- Universal_Literal_String --
6197 ------------------------------
6198
6199 -- This is a GNAT specific attribute whose prefix must be a named
6200 -- number where the expression is either a single numeric literal,
6201 -- or a numeric literal immediately preceded by a minus sign. The
6202 -- result is equivalent to a string literal containing the text of
6203 -- the literal as it appeared in the source program with a possible
6204 -- leading minus sign.
6205
6206 when Attribute_Universal_Literal_String => Universal_Literal_String :
6207 begin
6208 Check_E0;
6209
6210 if not Is_Entity_Name (P)
6211 or else Ekind (Entity (P)) not in Named_Kind
6212 then
822033eb 6213 Error_Attr_P ("prefix for % attribute must be named number");
996ae0b0
RK
6214
6215 else
6216 declare
6217 Expr : Node_Id;
6218 Negative : Boolean;
6219 S : Source_Ptr;
6220 Src : Source_Buffer_Ptr;
6221
6222 begin
6223 Expr := Original_Node (Expression (Parent (Entity (P))));
6224
6225 if Nkind (Expr) = N_Op_Minus then
6226 Negative := True;
6227 Expr := Original_Node (Right_Opnd (Expr));
6228 else
6229 Negative := False;
6230 end if;
6231
e10dab7f 6232 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
996ae0b0
RK
6233 Error_Attr
6234 ("named number for % attribute must be simple literal", N);
6235 end if;
6236
6237 -- Build string literal corresponding to source literal text
6238
6239 Start_String;
6240
6241 if Negative then
6242 Store_String_Char (Get_Char_Code ('-'));
6243 end if;
6244
6245 S := Sloc (Expr);
6246 Src := Source_Text (Get_Source_File_Index (S));
6247
6248 while Src (S) /= ';' and then Src (S) /= ' ' loop
6249 Store_String_Char (Get_Char_Code (Src (S)));
6250 S := S + 1;
6251 end loop;
6252
6253 -- Now we rewrite the attribute with the string literal
6254
6255 Rewrite (N,
6256 Make_String_Literal (Loc, End_String));
6257 Analyze (N);
edab6088 6258 Set_Is_Static_Expression (N, True);
996ae0b0
RK
6259 end;
6260 end if;
6261 end Universal_Literal_String;
6262
6263 -------------------------
6264 -- Unrestricted_Access --
6265 -------------------------
6266
6267 -- This is a GNAT specific attribute which is like Access except that
3cd4a210
AC
6268 -- all scope checks and checks for aliased views are omitted. It is
6269 -- documented as being equivalent to the use of the Address attribute
6270 -- followed by an unchecked conversion to the target access type.
996ae0b0
RK
6271
6272 when Attribute_Unrestricted_Access =>
dc36a7e3
RD
6273
6274 -- If from source, deal with relevant restrictions
6275
996ae0b0
RK
6276 if Comes_From_Source (N) then
6277 Check_Restriction (No_Unchecked_Access, N);
83de674b 6278
67c86178 6279 if Nkind (P) in N_Has_Entity
dc36a7e3
RD
6280 and then Present (Entity (P))
6281 and then Is_Object (Entity (P))
67c86178 6282 then
83de674b
AC
6283 Check_Restriction (No_Implicit_Aliasing, N);
6284 end if;
996ae0b0
RK
6285 end if;
6286
6287 if Is_Entity_Name (P) then
6288 Set_Address_Taken (Entity (P));
6289 end if;
6290
3cd4a210
AC
6291 -- It might seem reasonable to call Address_Checks here to apply the
6292 -- same set of semantic checks that we enforce for 'Address (after
6293 -- all we document Unrestricted_Access as being equivalent to the
6294 -- use of Address followed by an Unchecked_Conversion). However, if
6295 -- we do enable these checks, we get multiple failures in both the
6296 -- compiler run-time and in our regression test suite, so we leave
6297 -- out these checks for now. To be investigated further some time???
6298
6299 -- Address_Checks;
6300
6301 -- Now complete analysis using common access processing
6302
fbf5a39b 6303 Analyze_Access_Attribute;
996ae0b0 6304
18a2ad5d
AC
6305 ------------
6306 -- Update --
6307 ------------
6308
6309 when Attribute_Update => Update : declare
cc6f5d75
AC
6310 Common_Typ : Entity_Id;
6311 -- The common type of a multiple component update for a record
6312
18a2ad5d 6313 Comps : Elist_Id := No_Elist;
cc6f5d75
AC
6314 -- A list used in the resolution of a record update. It contains the
6315 -- entities of all record components processed so far.
18a2ad5d 6316
cc6f5d75
AC
6317 procedure Analyze_Array_Component_Update (Assoc : Node_Id);
6318 -- Analyze and resolve array_component_association Assoc against the
6319 -- index of array type P_Type.
18a2ad5d 6320
cc6f5d75
AC
6321 procedure Analyze_Record_Component_Update (Comp : Node_Id);
6322 -- Analyze and resolve record_component_association Comp against
6323 -- record type P_Type.
18a2ad5d 6324
cc6f5d75
AC
6325 ------------------------------------
6326 -- Analyze_Array_Component_Update --
6327 ------------------------------------
18a2ad5d 6328
cc6f5d75
AC
6329 procedure Analyze_Array_Component_Update (Assoc : Node_Id) is
6330 Expr : Node_Id;
6331 High : Node_Id;
6332 Index : Node_Id;
6333 Index_Typ : Entity_Id;
6334 Low : Node_Id;
18a2ad5d 6335
cc6f5d75
AC
6336 begin
6337 -- The current association contains a sequence of indexes denoting
6338 -- an element of a multidimensional array:
18a2ad5d 6339
cc6f5d75 6340 -- (Index_1, ..., Index_N)
18a2ad5d 6341
cc6f5d75
AC
6342 -- Examine each individual index and resolve it against the proper
6343 -- index type of the array.
6344
6345 if Nkind (First (Choices (Assoc))) = N_Aggregate then
6346 Expr := First (Choices (Assoc));
6347 while Present (Expr) loop
6348
6349 -- The use of others is illegal (SPARK RM 4.4.1(12))
6350
6351 if Nkind (Expr) = N_Others_Choice then
6352 Error_Attr
6353 ("others choice not allowed in attribute %", Expr);
6354
6355 -- Otherwise analyze and resolve all indexes
6356
6357 else
6358 Index := First (Expressions (Expr));
6359 Index_Typ := First_Index (P_Type);
6360 while Present (Index) and then Present (Index_Typ) loop
6361 Analyze_And_Resolve (Index, Etype (Index_Typ));
6362 Next (Index);
6363 Next_Index (Index_Typ);
6364 end loop;
6365
6366 -- Detect a case where the association either lacks an
6367 -- index or contains an extra index.
6368
6369 if Present (Index) or else Present (Index_Typ) then
6370 Error_Msg_N
6371 ("dimension mismatch in index list", Assoc);
18a2ad5d 6372 end if;
cc6f5d75 6373 end if;
18a2ad5d 6374
cc6f5d75
AC
6375 Next (Expr);
6376 end loop;
6377
6378 -- The current association denotes either a single component or a
6379 -- range of components of a one dimensional array:
6380
6381 -- 1, 2 .. 5
6382
6383 -- Resolve the index or its high and low bounds (if range) against
6384 -- the proper index type of the array.
6385
6386 else
6387 Index := First (Choices (Assoc));
6388 Index_Typ := First_Index (P_Type);
6389
6390 if Present (Next_Index (Index_Typ)) then
6391 Error_Msg_N ("too few subscripts in array reference", Assoc);
18a2ad5d
AC
6392 end if;
6393
cc6f5d75 6394 while Present (Index) loop
18a2ad5d 6395
cc6f5d75 6396 -- The use of others is illegal (SPARK RM 4.4.1(12))
18a2ad5d 6397
cc6f5d75
AC
6398 if Nkind (Index) = N_Others_Choice then
6399 Error_Attr
6400 ("others choice not allowed in attribute %", Index);
6401
6402 -- The index denotes a range of elements
6403
6404 elsif Nkind (Index) = N_Range then
6405 Low := Low_Bound (Index);
6406 High := High_Bound (Index);
6407
6408 Analyze_And_Resolve (Low, Etype (Index_Typ));
6409 Analyze_And_Resolve (High, Etype (Index_Typ));
6410
6411 -- Add a range check to ensure that the bounds of the
6412 -- range are within the index type when this cannot be
6413 -- determined statically.
6414
6415 if not Is_OK_Static_Expression (Low) then
6416 Set_Do_Range_Check (Low);
6417 end if;
6418
6419 if not Is_OK_Static_Expression (High) then
6420 Set_Do_Range_Check (High);
6421 end if;
6422
6423 -- Otherwise the index denotes a single element
6424
6425 else
6426 Analyze_And_Resolve (Index, Etype (Index_Typ));
6427
6428 -- Add a range check to ensure that the index is within
6429 -- the index type when it is not possible to determine
6430 -- this statically.
6431
6432 if not Is_OK_Static_Expression (Index) then
6433 Set_Do_Range_Check (Index);
6434 end if;
6435 end if;
6436
6437 Next (Index);
6438 end loop;
6439 end if;
6440 end Analyze_Array_Component_Update;
6441
6442 -------------------------------------
6443 -- Analyze_Record_Component_Update --
6444 -------------------------------------
18a2ad5d 6445
cc6f5d75
AC
6446 procedure Analyze_Record_Component_Update (Comp : Node_Id) is
6447 Comp_Name : constant Name_Id := Chars (Comp);
6448 Base_Typ : Entity_Id;
6449 Comp_Or_Discr : Entity_Id;
18a2ad5d
AC
6450
6451 begin
6452 -- Find the discriminant or component whose name corresponds to
6453 -- Comp. A simple character comparison is sufficient because all
6454 -- visible names within a record type are unique.
6455
cc6f5d75 6456 Comp_Or_Discr := First_Entity (P_Type);
18a2ad5d
AC
6457 while Present (Comp_Or_Discr) loop
6458 if Chars (Comp_Or_Discr) = Comp_Name then
08cd7c2f 6459
cc6f5d75
AC
6460 -- Decorate the component reference by setting its entity
6461 -- and type for resolution purposes.
08cd7c2f
AC
6462
6463 Set_Entity (Comp, Comp_Or_Discr);
d950f051 6464 Set_Etype (Comp, Etype (Comp_Or_Discr));
18a2ad5d
AC
6465 exit;
6466 end if;
6467
6468 Comp_Or_Discr := Next_Entity (Comp_Or_Discr);
6469 end loop;
6470
cc6f5d75 6471 -- Diagnose an illegal reference
18a2ad5d
AC
6472
6473 if Present (Comp_Or_Discr) then
6474 if Ekind (Comp_Or_Discr) = E_Discriminant then
6475 Error_Attr
6476 ("attribute % may not modify record discriminants", Comp);
6477
6478 else pragma Assert (Ekind (Comp_Or_Discr) = E_Component);
cc6f5d75
AC
6479 if Contains (Comps, Comp_Or_Discr) then
6480 Error_Msg_N ("component & already updated", Comp);
18a2ad5d
AC
6481
6482 -- Mark this component as processed
6483
6484 else
21c51f53 6485 Append_New_Elmt (Comp_Or_Discr, Comps);
18a2ad5d
AC
6486 end if;
6487 end if;
6488
6489 -- The update aggregate mentions an entity that does not belong to
6490 -- the record type.
6491
6492 else
cc6f5d75
AC
6493 Error_Msg_N ("& is not a component of aggregate subtype", Comp);
6494 end if;
6495
6496 -- Verify the consistency of types when the current component is
6497 -- part of a miltiple component update.
6498
6499 -- Comp_1, ..., Comp_N => <value>
6500
6501 if Present (Etype (Comp)) then
6502 Base_Typ := Base_Type (Etype (Comp));
6503
6504 -- Save the type of the first component reference as the
6505 -- remaning references (if any) must resolve to this type.
6506
6507 if No (Common_Typ) then
6508 Common_Typ := Base_Typ;
6509
6510 elsif Base_Typ /= Common_Typ then
6511 Error_Msg_N
6512 ("components in choice list must have same type", Comp);
6513 end if;
18a2ad5d 6514 end if;
cc6f5d75 6515 end Analyze_Record_Component_Update;
18a2ad5d
AC
6516
6517 -- Local variables
6518
cc6f5d75
AC
6519 Assoc : Node_Id;
6520 Comp : Node_Id;
18a2ad5d
AC
6521
6522 -- Start of processing for Update
6523
6524 begin
18a2ad5d
AC
6525 Check_E1;
6526
6527 if not Is_Object_Reference (P) then
6528 Error_Attr_P ("prefix of attribute % must denote an object");
6529
6530 elsif not Is_Array_Type (P_Type)
6531 and then not Is_Record_Type (P_Type)
6532 then
6533 Error_Attr_P ("prefix of attribute % must be a record or array");
6534
51245e2d 6535 elsif Is_Limited_View (P_Type) then
18a2ad5d
AC
6536 Error_Attr ("prefix of attribute % cannot be limited", N);
6537
6538 elsif Nkind (E1) /= N_Aggregate then
6539 Error_Attr ("attribute % requires component association list", N);
6540 end if;
6541
6542 -- Inspect the update aggregate, looking at all the associations and
6543 -- choices. Perform the following checks:
6544
6545 -- 1) Legality of "others" in all cases
cc6f5d75
AC
6546 -- 2) Legality of <>
6547 -- 3) Component legality for arrays
6548 -- 4) Component legality for records
18a2ad5d
AC
6549
6550 -- The remaining checks are performed on the expanded attribute
6551
6552 Assoc := First (Component_Associations (E1));
6553 while Present (Assoc) loop
162c21d9 6554
cc6f5d75 6555 -- The use of <> is illegal (SPARK RM 4.4.1(1))
162c21d9 6556
cc6f5d75
AC
6557 if Box_Present (Assoc) then
6558 Error_Attr
6559 ("default initialization not allowed in attribute %", Assoc);
a6abfd78 6560
cc6f5d75 6561 -- Otherwise process the association
d0ef7921 6562
cc6f5d75
AC
6563 else
6564 Analyze (Expression (Assoc));
6907542d 6565
cc6f5d75
AC
6566 if Is_Array_Type (P_Type) then
6567 Analyze_Array_Component_Update (Assoc);
cf28c974 6568
cc6f5d75 6569 elsif Is_Record_Type (P_Type) then
6907542d 6570
cc6f5d75
AC
6571 -- Reset the common type used in a multiple component update
6572 -- as we are processing the contents of a new association.
cf28c974 6573
cc6f5d75 6574 Common_Typ := Empty;
162c21d9 6575
cc6f5d75
AC
6576 Comp := First (Choices (Assoc));
6577 while Present (Comp) loop
6578 if Nkind (Comp) = N_Identifier then
6579 Analyze_Record_Component_Update (Comp);
50ea6357 6580
cc6f5d75 6581 -- The use of others is illegal (SPARK RM 4.4.1(5))
d0ef7921 6582
cc6f5d75
AC
6583 elsif Nkind (Comp) = N_Others_Choice then
6584 Error_Attr
6585 ("others choice not allowed in attribute %", Comp);
162c21d9 6586
cc6f5d75
AC
6587 -- The name of a record component cannot appear in any
6588 -- other form.
162c21d9 6589
d0ef7921 6590 else
46de64ca 6591 Error_Msg_N
cc6f5d75 6592 ("name should be identifier or OTHERS", Comp);
46de64ca 6593 end if;
18a2ad5d 6594
cc6f5d75
AC
6595 Next (Comp);
6596 end loop;
6597 end if;
6598 end if;
18a2ad5d
AC
6599
6600 Next (Assoc);
6601 end loop;
6602
cc6f5d75 6603 -- The type of attribute 'Update is that of the prefix
18a2ad5d
AC
6604
6605 Set_Etype (N, P_Type);
71140fc6
YM
6606
6607 Sem_Warn.Warn_On_Suspicious_Update (N);
18a2ad5d
AC
6608 end Update;
6609
996ae0b0
RK
6610 ---------
6611 -- Val --
6612 ---------
6613
6614 when Attribute_Val => Val : declare
6615 begin
6616 Check_E1;
6617 Check_Discrete_Type;
7a489a2b
AC
6618
6619 if Is_Boolean_Type (P_Type) then
6620 Error_Msg_Name_1 := Aname;
6621 Error_Msg_Name_2 := Chars (P_Type);
ce5ba43a 6622 Check_SPARK_05_Restriction
7a489a2b
AC
6623 ("attribute% is not allowed for type%", P);
6624 end if;
6625
996ae0b0
RK
6626 Resolve (E1, Any_Integer);
6627 Set_Etype (N, P_Base_Type);
6628
6629 -- Note, we need a range check in general, but we wait for the
6630 -- Resolve call to do this, since we want to let Eval_Attribute
a90bd866 6631 -- have a chance to find an static illegality first.
996ae0b0
RK
6632 end Val;
6633
6634 -----------
6635 -- Valid --
6636 -----------
6637
6638 when Attribute_Valid =>
6639 Check_E0;
6640
6641 -- Ignore check for object if we have a 'Valid reference generated
6642 -- by the expanded code, since in some cases valid checks can occur
6643 -- on items that are names, but are not objects (e.g. attributes).
6644
6645 if Comes_From_Source (N) then
6646 Check_Object_Reference (P);
6647 end if;
6648
6649 if not Is_Scalar_Type (P_Type) then
822033eb 6650 Error_Attr_P ("object for % attribute must be of scalar type");
996ae0b0
RK
6651 end if;
6652
97948f41
AC
6653 -- If the attribute appears within the subtype's own predicate
6654 -- function, then issue a warning that this will cause infinite
6655 -- recursion.
6656
6657 declare
6658 Pred_Func : constant Entity_Id := Predicate_Function (P_Type);
6659
6660 begin
6661 if Present (Pred_Func) and then Current_Scope = Pred_Func then
6662 Error_Msg_N
324ac540
AC
6663 ("attribute Valid requires a predicate check??", N);
6664 Error_Msg_N ("\and will result in infinite recursion??", N);
97948f41
AC
6665 end if;
6666 end;
6667
996ae0b0
RK
6668 Set_Etype (N, Standard_Boolean);
6669
2a1f6a1f
AC
6670 -------------------
6671 -- Valid_Scalars --
6672 -------------------
6673
6674 when Attribute_Valid_Scalars =>
6675 Check_E0;
99fc068e 6676 Check_Object_Reference (P);
45ec05e1 6677 Set_Etype (N, Standard_Boolean);
99fc068e 6678
45ec05e1 6679 -- Following checks are only for source types
99fc068e 6680
45ec05e1
RD
6681 if Comes_From_Source (N) then
6682 if not Scalar_Part_Present (P_Type) then
6683 Error_Attr_P
6684 ("??attribute % always True, no scalars to check");
6685 end if;
6686
6687 -- Not allowed for unchecked union type
6688
6689 if Has_Unchecked_Union (P_Type) then
6690 Error_Attr_P
6691 ("attribute % not allowed for Unchecked_Union type");
6692 end if;
6693 end if;
2a1f6a1f 6694
996ae0b0
RK
6695 -----------
6696 -- Value --
6697 -----------
6698
6699 when Attribute_Value => Value :
6700 begin
ce5ba43a 6701 Check_SPARK_05_Restriction_On_Attribute;
996ae0b0
RK
6702 Check_E1;
6703 Check_Scalar_Type;
6704
442ade9d
RD
6705 -- Case of enumeration type
6706
3e7302c3
AC
6707 -- When an enumeration type appears in an attribute reference, all
6708 -- literals of the type are marked as referenced. This must only be
6709 -- done if the attribute reference appears in the current source.
6710 -- Otherwise the information on references may differ between a
6711 -- normal compilation and one that performs inlining.
6712
8097203f
AC
6713 if Is_Enumeration_Type (P_Type)
6714 and then In_Extended_Main_Code_Unit (N)
6715 then
996ae0b0 6716 Check_Restriction (No_Enumeration_Maps, N);
442ade9d
RD
6717
6718 -- Mark all enumeration literals as referenced, since the use of
6719 -- the Value attribute can implicitly reference any of the
6720 -- literals of the enumeration base type.
6721
6722 declare
6723 Ent : Entity_Id := First_Literal (P_Base_Type);
6724 begin
6725 while Present (Ent) loop
6726 Set_Referenced (Ent);
6727 Next_Literal (Ent);
6728 end loop;
6729 end;
996ae0b0
RK
6730 end if;
6731
fbf5a39b
AC
6732 -- Set Etype before resolving expression because expansion of
6733 -- expression may require enclosing type. Note that the type
6734 -- returned by 'Value is the base type of the prefix type.
996ae0b0 6735
fbf5a39b 6736 Set_Etype (N, P_Base_Type);
996ae0b0 6737 Validate_Non_Static_Attribute_Function_Call;
0688dac8
RD
6738
6739 -- Check restriction No_Fixed_IO
6740
6741 if Restriction_Check_Required (No_Fixed_IO)
6742 and then Is_Fixed_Point_Type (P_Type)
6743 then
6744 Check_Restriction (No_Fixed_IO, P);
6745 end if;
996ae0b0
RK
6746 end Value;
6747
6748 ----------------
6749 -- Value_Size --
6750 ----------------
6751
6752 when Attribute_Value_Size =>
6753 Check_E0;
6754 Check_Type;
6755 Check_Not_Incomplete_Type;
6756 Set_Etype (N, Universal_Integer);
6757
6758 -------------
6759 -- Version --
6760 -------------
6761
6762 when Attribute_Version =>
6763 Check_E0;
6764 Check_Program_Unit;
6765 Set_Etype (N, RTE (RE_Version_String));
6766
6767 ------------------
6768 -- Wchar_T_Size --
6769 ------------------
6770
6771 when Attribute_Wchar_T_Size =>
6772 Standard_Attribute (Interfaces_Wchar_T_Size);
6773
6774 ----------------
6775 -- Wide_Image --
6776 ----------------
6777
6778 when Attribute_Wide_Image => Wide_Image :
6779 begin
ce5ba43a 6780 Check_SPARK_05_Restriction_On_Attribute;
996ae0b0
RK
6781 Check_Scalar_Type;
6782 Set_Etype (N, Standard_Wide_String);
6783 Check_E1;
6784 Resolve (E1, P_Base_Type);
6785 Validate_Non_Static_Attribute_Function_Call;
0688dac8
RD
6786
6787 -- Check restriction No_Fixed_IO
6788
6789 if Restriction_Check_Required (No_Fixed_IO)
6790 and then Is_Fixed_Point_Type (P_Type)
6791 then
6792 Check_Restriction (No_Fixed_IO, P);
6793 end if;
996ae0b0
RK
6794 end Wide_Image;
6795
82c80734
RD
6796 ---------------------
6797 -- Wide_Wide_Image --
6798 ---------------------
6799
6800 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
6801 begin
6802 Check_Scalar_Type;
6803 Set_Etype (N, Standard_Wide_Wide_String);
6804 Check_E1;
6805 Resolve (E1, P_Base_Type);
6806 Validate_Non_Static_Attribute_Function_Call;
0688dac8
RD
6807
6808 -- Check restriction No_Fixed_IO
6809
6810 if Restriction_Check_Required (No_Fixed_IO)
6811 and then Is_Fixed_Point_Type (P_Type)
6812 then
6813 Check_Restriction (No_Fixed_IO, P);
6814 end if;
82c80734
RD
6815 end Wide_Wide_Image;
6816
996ae0b0
RK
6817 ----------------
6818 -- Wide_Value --
6819 ----------------
6820
6821 when Attribute_Wide_Value => Wide_Value :
6822 begin
ce5ba43a 6823 Check_SPARK_05_Restriction_On_Attribute;
996ae0b0
RK
6824 Check_E1;
6825 Check_Scalar_Type;
6826
6827 -- Set Etype before resolving expression because expansion
6828 -- of expression may require enclosing type.
6829
6830 Set_Etype (N, P_Type);
6831 Validate_Non_Static_Attribute_Function_Call;
0688dac8
RD
6832
6833 -- Check restriction No_Fixed_IO
6834
6835 if Restriction_Check_Required (No_Fixed_IO)
6836 and then Is_Fixed_Point_Type (P_Type)
6837 then
6838 Check_Restriction (No_Fixed_IO, P);
6839 end if;
996ae0b0
RK
6840 end Wide_Value;
6841
82c80734
RD
6842 ---------------------
6843 -- Wide_Wide_Value --
6844 ---------------------
6845
6846 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6847 begin
6848 Check_E1;
6849 Check_Scalar_Type;
6850
6851 -- Set Etype before resolving expression because expansion
6852 -- of expression may require enclosing type.
6853
6854 Set_Etype (N, P_Type);
6855 Validate_Non_Static_Attribute_Function_Call;
0688dac8
RD
6856
6857 -- Check restriction No_Fixed_IO
6858
6859 if Restriction_Check_Required (No_Fixed_IO)
6860 and then Is_Fixed_Point_Type (P_Type)
6861 then
6862 Check_Restriction (No_Fixed_IO, P);
6863 end if;
82c80734
RD
6864 end Wide_Wide_Value;
6865
6866 ---------------------
6867 -- Wide_Wide_Width --
6868 ---------------------
6869
6870 when Attribute_Wide_Wide_Width =>
6871 Check_E0;
6872 Check_Scalar_Type;
6873 Set_Etype (N, Universal_Integer);
6874
996ae0b0
RK
6875 ----------------
6876 -- Wide_Width --
6877 ----------------
6878
6879 when Attribute_Wide_Width =>
ce5ba43a 6880 Check_SPARK_05_Restriction_On_Attribute;
996ae0b0
RK
6881 Check_E0;
6882 Check_Scalar_Type;
6883 Set_Etype (N, Universal_Integer);
6884
6885 -----------
6886 -- Width --
6887 -----------
6888
6889 when Attribute_Width =>
ce5ba43a 6890 Check_SPARK_05_Restriction_On_Attribute;
996ae0b0
RK
6891 Check_E0;
6892 Check_Scalar_Type;
6893 Set_Etype (N, Universal_Integer);
6894
6895 ---------------
6896 -- Word_Size --
6897 ---------------
6898
6899 when Attribute_Word_Size =>
6900 Standard_Attribute (System_Word_Size);
6901
6902 -----------
6903 -- Write --
6904 -----------
6905
6906 when Attribute_Write =>
6907 Check_E2;
fbf5a39b 6908 Check_Stream_Attribute (TSS_Stream_Write);
996ae0b0 6909 Set_Etype (N, Standard_Void_Type);
996ae0b0
RK
6910 Resolve (N, Standard_Void_Type);
6911
6912 end case;
6913
6914 -- All errors raise Bad_Attribute, so that we get out before any further
6915 -- damage occurs when an error is detected (for example, if we check for
6916 -- one attribute expression, and the check succeeds, we want to be able
6917 -- to proceed securely assuming that an expression is in fact present.
6918
0da2c8ac
AC
6919 -- Note: we set the attribute analyzed in this case to prevent any
6920 -- attempt at reanalysis which could generate spurious error msgs.
6921
996ae0b0
RK
6922 exception
6923 when Bad_Attribute =>
0da2c8ac 6924 Set_Analyzed (N);
996ae0b0
RK
6925 Set_Etype (N, Any_Type);
6926 return;
996ae0b0
RK
6927 end Analyze_Attribute;
6928
6929 --------------------
6930 -- Eval_Attribute --
6931 --------------------
6932
6933 procedure Eval_Attribute (N : Node_Id) is
6934 Loc : constant Source_Ptr := Sloc (N);
6935 Aname : constant Name_Id := Attribute_Name (N);
6936 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
6937 P : constant Node_Id := Prefix (N);
6938
6939 C_Type : constant Entity_Id := Etype (N);
edd63e9b 6940 -- The type imposed by the context
996ae0b0
RK
6941
6942 E1 : Node_Id;
6943 -- First expression, or Empty if none
6944
6945 E2 : Node_Id;
6946 -- Second expression, or Empty if none
6947
6948 P_Entity : Entity_Id;
6949 -- Entity denoted by prefix
6950
6951 P_Type : Entity_Id;
6952 -- The type of the prefix
6953
6954 P_Base_Type : Entity_Id;
6955 -- The base type of the prefix type
6956
6957 P_Root_Type : Entity_Id;
6958 -- The root type of the prefix type
6959
6960 Static : Boolean;
fbf5a39b
AC
6961 -- True if the result is Static. This is set by the general processing
6962 -- to true if the prefix is static, and all expressions are static. It
edab6088
RD
6963 -- can be reset as processing continues for particular attributes. This
6964 -- flag can still be True if the reference raises a constraint error.
6965 -- Is_Static_Expression (N) is set to follow this value as it is set
6966 -- and we could always reference this, but it is convenient to have a
6967 -- simple short name to use, since it is frequently referenced.
996ae0b0
RK
6968
6969 Lo_Bound, Hi_Bound : Node_Id;
6970 -- Expressions for low and high bounds of type or array index referenced
6971 -- by First, Last, or Length attribute for array, set by Set_Bounds.
6972
6973 CE_Node : Node_Id;
6974 -- Constraint error node used if we have an attribute reference has
6975 -- an argument that raises a constraint error. In this case we replace
6976 -- the attribute with a raise constraint_error node. This is important
6977 -- processing, since otherwise gigi might see an attribute which it is
6978 -- unprepared to deal with.
6979
5a153b27
AC
6980 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
6981 -- If Bound is a reference to a discriminant of a task or protected type
6982 -- occurring within the object's body, rewrite attribute reference into
6983 -- a reference to the corresponding discriminal. Use for the expansion
6984 -- of checks against bounds of entry family index subtypes.
6985
996ae0b0
RK
6986 procedure Check_Expressions;
6987 -- In case where the attribute is not foldable, the expressions, if
6988 -- any, of the attribute, are in a non-static context. This procedure
6989 -- performs the required additional checks.
6990
fbf5a39b
AC
6991 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
6992 -- Determines if the given type has compile time known bounds. Note
6993 -- that we enter the case statement even in cases where the prefix
6994 -- type does NOT have known bounds, so it is important to guard any
6995 -- attempt to evaluate both bounds with a call to this function.
6996
32213142
RD
6997 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
6998 -- This procedure is called when the attribute N has a non-static
6999 -- but compile time known value given by Val. It includes the
7000 -- necessary checks for out of range values.
7001
996ae0b0
RK
7002 function Fore_Value return Nat;
7003 -- Computes the Fore value for the current attribute prefix, which is
7004 -- known to be a static fixed-point type. Used by Fore and Width.
7005
7006 function Mantissa return Uint;
7007 -- Returns the Mantissa value for the prefix type
7008
7009 procedure Set_Bounds;
7010 -- Used for First, Last and Length attributes applied to an array or
fbf5a39b 7011 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
996ae0b0 7012 -- and high bound expressions for the index referenced by the attribute
011f9d5d
AC
7013 -- designator (i.e. the first index if no expression is present, and the
7014 -- N'th index if the value N is present as an expression). Also used for
7015 -- First and Last of scalar types and for First_Valid and Last_Valid.
7016 -- Static is reset to False if the type or index type is not statically
7017 -- constrained.
996ae0b0 7018
c4e5e10f
TQ
7019 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
7020 -- Verify that the prefix of a potentially static array attribute
7021 -- satisfies the conditions of 4.9 (14).
7022
5a153b27
AC
7023 -----------------------------------
7024 -- Check_Concurrent_Discriminant --
7025 -----------------------------------
7026
7027 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
6ca9ec9c 7028 Tsk : Entity_Id;
5a153b27 7029 -- The concurrent (task or protected) type
6ca9ec9c 7030
5a153b27
AC
7031 begin
7032 if Nkind (Bound) = N_Identifier
7033 and then Ekind (Entity (Bound)) = E_Discriminant
7034 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
7035 then
7036 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
6ca9ec9c
AC
7037
7038 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
7039
5a153b27
AC
7040 -- Find discriminant of original concurrent type, and use
7041 -- its current discriminal, which is the renaming within
7042 -- the task/protected body.
7043
7044 Rewrite (N,
7045 New_Occurrence_Of
7046 (Find_Body_Discriminal (Entity (Bound)), Loc));
7047 end if;
7048 end if;
7049 end Check_Concurrent_Discriminant;
7050
996ae0b0
RK
7051 -----------------------
7052 -- Check_Expressions --
7053 -----------------------
7054
7055 procedure Check_Expressions is
442ade9d 7056 E : Node_Id;
996ae0b0 7057 begin
442ade9d 7058 E := E1;
996ae0b0
RK
7059 while Present (E) loop
7060 Check_Non_Static_Context (E);
7061 Next (E);
7062 end loop;
7063 end Check_Expressions;
7064
32213142
RD
7065 ----------------------------------
7066 -- Compile_Time_Known_Attribute --
7067 ----------------------------------
7068
7069 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
7070 T : constant Entity_Id := Etype (N);
7071
7072 begin
fbf5a39b 7073 Fold_Uint (N, Val, False);
32213142
RD
7074
7075 -- Check that result is in bounds of the type if it is static
7076
c800f862 7077 if Is_In_Range (N, T, Assume_Valid => False) then
32213142
RD
7078 null;
7079
7080 elsif Is_Out_Of_Range (N, T) then
7081 Apply_Compile_Time_Constraint_Error
324ac540 7082 (N, "value not in range of}??", CE_Range_Check_Failed);
32213142
RD
7083
7084 elsif not Range_Checks_Suppressed (T) then
7085 Enable_Range_Check (N);
7086
7087 else
7088 Set_Do_Range_Check (N, False);
7089 end if;
7090 end Compile_Time_Known_Attribute;
7091
fbf5a39b
AC
7092 -------------------------------
7093 -- Compile_Time_Known_Bounds --
7094 -------------------------------
7095
7096 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
7097 begin
7098 return
7099 Compile_Time_Known_Value (Type_Low_Bound (Typ))
7100 and then
7101 Compile_Time_Known_Value (Type_High_Bound (Typ));
7102 end Compile_Time_Known_Bounds;
7103
996ae0b0
RK
7104 ----------------
7105 -- Fore_Value --
7106 ----------------
7107
7108 -- Note that the Fore calculation is based on the actual values
7109 -- of the bounds, and does not take into account possible rounding.
7110
7111 function Fore_Value return Nat is
7112 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7113 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7114 Small : constant Ureal := Small_Value (P_Type);
7115 Lo_Real : constant Ureal := Lo * Small;
7116 Hi_Real : constant Ureal := Hi * Small;
7117 T : Ureal;
7118 R : Nat;
7119
7120 begin
7121 -- Bounds are given in terms of small units, so first compute
7122 -- proper values as reals.
7123
7124 T := UR_Max (abs Lo_Real, abs Hi_Real);
7125 R := 2;
7126
7127 -- Loop to compute proper value if more than one digit required
7128
7129 while T >= Ureal_10 loop
7130 R := R + 1;
7131 T := T / Ureal_10;
7132 end loop;
7133
7134 return R;
7135 end Fore_Value;
7136
7137 --------------
7138 -- Mantissa --
7139 --------------
7140
7141 -- Table of mantissa values accessed by function Computed using
7142 -- the relation:
7143
7144 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7145
7146 -- where D is T'Digits (RM83 3.5.7)
7147
7148 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
7149 1 => 5,
7150 2 => 8,
7151 3 => 11,
7152 4 => 15,
7153 5 => 18,
7154 6 => 21,
7155 7 => 25,
7156 8 => 28,
7157 9 => 31,
7158 10 => 35,
7159 11 => 38,
7160 12 => 41,
7161 13 => 45,
7162 14 => 48,
7163 15 => 51,
7164 16 => 55,
7165 17 => 58,
7166 18 => 61,
7167 19 => 65,
7168 20 => 68,
7169 21 => 71,
7170 22 => 75,
7171 23 => 78,
7172 24 => 81,
7173 25 => 85,
7174 26 => 88,
7175 27 => 91,
7176 28 => 95,
7177 29 => 98,
7178 30 => 101,
7179 31 => 104,
7180 32 => 108,
7181 33 => 111,
7182 34 => 114,
7183 35 => 118,
7184 36 => 121,
7185 37 => 124,
7186 38 => 128,
7187 39 => 131,
7188 40 => 134);
7189
7190 function Mantissa return Uint is
7191 begin
7192 return
7193 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
7194 end Mantissa;
7195
7196 ----------------
7197 -- Set_Bounds --
7198 ----------------
7199
7200 procedure Set_Bounds is
7201 Ndim : Nat;
7202 Indx : Node_Id;
7203 Ityp : Entity_Id;
7204
7205 begin
7206 -- For a string literal subtype, we have to construct the bounds.
7207 -- Valid Ada code never applies attributes to string literals, but
7208 -- it is convenient to allow the expander to generate attribute
7209 -- references of this type (e.g. First and Last applied to a string
7210 -- literal).
7211
7212 -- Note that the whole point of the E_String_Literal_Subtype is to
7213 -- avoid this construction of bounds, but the cases in which we
a90bd866 7214 -- have to materialize them are rare enough that we don't worry.
996ae0b0
RK
7215
7216 -- The low bound is simply the low bound of the base type. The
7217 -- high bound is computed from the length of the string and this
7218 -- low bound.
7219
7220 if Ekind (P_Type) = E_String_Literal_Subtype then
fbf5a39b
AC
7221 Ityp := Etype (First_Index (Base_Type (P_Type)));
7222 Lo_Bound := Type_Low_Bound (Ityp);
996ae0b0
RK
7223
7224 Hi_Bound :=
7225 Make_Integer_Literal (Sloc (P),
7226 Intval =>
7227 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
7228
7229 Set_Parent (Hi_Bound, P);
7230 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
7231 return;
7232
7233 -- For non-array case, just get bounds of scalar type
7234
7235 elsif Is_Scalar_Type (P_Type) then
7236 Ityp := P_Type;
7237
fbf5a39b
AC
7238 -- For a fixed-point type, we must freeze to get the attributes
7239 -- of the fixed-point type set now so we can reference them.
7240
7fb754a1
GB
7241 if Is_Fixed_Point_Type (P_Type)
7242 and then not Is_Frozen (Base_Type (P_Type))
7243 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7244 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
7245 then
7246 Freeze_Fixed_Point_Type (Base_Type (P_Type));
7247 end if;
7248
996ae0b0
RK
7249 -- For array case, get type of proper index
7250
7251 else
7252 if No (E1) then
7253 Ndim := 1;
7254 else
7255 Ndim := UI_To_Int (Expr_Value (E1));
7256 end if;
7257
7258 Indx := First_Index (P_Type);
7259 for J in 1 .. Ndim - 1 loop
7260 Next_Index (Indx);
7261 end loop;
7262
7263 -- If no index type, get out (some other error occurred, and
a90bd866 7264 -- we don't have enough information to complete the job).
996ae0b0
RK
7265
7266 if No (Indx) then
7267 Lo_Bound := Error;
7268 Hi_Bound := Error;
7269 return;
7270 end if;
7271
7272 Ityp := Etype (Indx);
7273 end if;
7274
7275 -- A discrete range in an index constraint is allowed to be a
7276 -- subtype indication. This is syntactically a pain, but should
7277 -- not propagate to the entity for the corresponding index subtype.
7278 -- After checking that the subtype indication is legal, the range
7279 -- of the subtype indication should be transfered to the entity.
7280 -- The attributes for the bounds should remain the simple retrievals
7281 -- that they are now.
7282
7283 Lo_Bound := Type_Low_Bound (Ityp);
7284 Hi_Bound := Type_High_Bound (Ityp);
7285
edab6088
RD
7286 -- If subtype is non-static, result is definitely non-static
7287
fbf5a39b
AC
7288 if not Is_Static_Subtype (Ityp) then
7289 Static := False;
edab6088
RD
7290 Set_Is_Static_Expression (N, False);
7291
7292 -- Subtype is static, does it raise CE?
7293
7294 elsif not Is_OK_Static_Subtype (Ityp) then
7295 Set_Raises_Constraint_Error (N);
fbf5a39b 7296 end if;
996ae0b0
RK
7297 end Set_Bounds;
7298
c4e5e10f
TQ
7299 -------------------------------
7300 -- Statically_Denotes_Entity --
7301 -------------------------------
7302
7303 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
7304 E : Entity_Id;
7305
7306 begin
7307 if not Is_Entity_Name (N) then
7308 return False;
7309 else
7310 E := Entity (N);
7311 end if;
7312
7313 return
7314 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7315 or else Statically_Denotes_Entity (Renamed_Object (E));
7316 end Statically_Denotes_Entity;
7317
996ae0b0
RK
7318 -- Start of processing for Eval_Attribute
7319
7320 begin
edab6088
RD
7321 -- Initialize result as non-static, will be reset if appropriate
7322
7323 Set_Is_Static_Expression (N, False);
7324 Static := False;
7325
c159409f
AC
7326 -- Acquire first two expressions (at the moment, no attributes take more
7327 -- than two expressions in any case).
996ae0b0
RK
7328
7329 if Present (Expressions (N)) then
7330 E1 := First (Expressions (N));
7331 E2 := Next (E1);
7332 else
7333 E1 := Empty;
7334 E2 := Empty;
7335 end if;
7336
442ade9d
RD
7337 -- Special processing for Enabled attribute. This attribute has a very
7338 -- special prefix, and the easiest way to avoid lots of special checks
7339 -- to protect this special prefix from causing trouble is to deal with
7340 -- this attribute immediately and be done with it.
7341
7342 if Id = Attribute_Enabled then
7343
442ade9d
RD
7344 -- We skip evaluation if the expander is not active. This is not just
7345 -- an optimization. It is of key importance that we not rewrite the
7346 -- attribute in a generic template, since we want to pick up the
774454ac
AC
7347 -- setting of the check in the instance, Testing Expander_Active
7348 -- might seem an easy way of doing this, but we need to account for
7349 -- ASIS needs, so check explicitly for a generic context.
442ade9d 7350
774454ac 7351 if not Inside_A_Generic then
442ade9d
RD
7352 declare
7353 C : constant Check_Id := Get_Check_Id (Chars (P));
7354 R : Boolean;
7355
7356 begin
7357 if No (E1) then
7358 if C in Predefined_Check_Id then
3217f71e 7359 R := Scope_Suppress.Suppress (C);
442ade9d
RD
7360 else
7361 R := Is_Check_Suppressed (Empty, C);
7362 end if;
7363
7364 else
7365 R := Is_Check_Suppressed (Entity (E1), C);
7366 end if;
7367
21791d97 7368 Rewrite (N, New_Occurrence_Of (Boolean_Literals (not R), Loc));
442ade9d
RD
7369 end;
7370 end if;
7371
7372 return;
7373 end if;
7374
21db8699
RD
7375 -- Attribute 'Img applied to a static enumeration value is static, and
7376 -- we will do the folding right here (things get confused if we let this
7377 -- case go through the normal circuitry).
7378
7379 if Attribute_Name (N) = Name_Img
7380 and then Is_Entity_Name (P)
7381 and then Is_Enumeration_Type (Etype (Entity (P)))
7382 and then Is_OK_Static_Expression (P)
7383 then
7384 declare
7385 Lit : constant Entity_Id := Expr_Value_E (P);
7386 Str : String_Id;
7387
7388 begin
7389 Start_String;
7390 Get_Unqualified_Decoded_Name_String (Chars (Lit));
7391 Set_Casing (All_Upper_Case);
7392 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7393 Str := End_String;
7394
7395 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
7396 Analyze_And_Resolve (N, Standard_String);
7397 Set_Is_Static_Expression (N, True);
7398 end;
7399
7400 return;
7401 end if;
7402
ca7e6c26
AC
7403 -- Special processing for cases where the prefix is an object. For this
7404 -- purpose, a string literal counts as an object (attributes of string
7405 -- literals can only appear in generated code).
996ae0b0 7406
fbf5a39b 7407 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
996ae0b0
RK
7408
7409 -- For Component_Size, the prefix is an array object, and we apply
ca7e6c26
AC
7410 -- the attribute to the type of the object. This is allowed for both
7411 -- unconstrained and constrained arrays, since the bounds have no
7412 -- influence on the value of this attribute.
996ae0b0
RK
7413
7414 if Id = Attribute_Component_Size then
7415 P_Entity := Etype (P);
7416
ca7e6c26
AC
7417 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7418 -- the optional argument.
7419
7420 elsif Id = Attribute_Enum_Rep then
7421 if Is_Entity_Name (P) then
7422
f3e0f7c3
BD
7423 declare
7424 Enum_Expr : Node_Id;
7425 -- The enumeration-type expression of interest
0c3f76ba 7426
f3e0f7c3
BD
7427 begin
7428 -- P'Enum_Rep case
ca7e6c26 7429
0c3f76ba
AC
7430 if Ekind_In (Entity (P), E_Constant,
7431 E_Enumeration_Literal)
f3e0f7c3
BD
7432 then
7433 Enum_Expr := P;
ca7e6c26 7434
f3e0f7c3 7435 -- Enum_Type'Enum_Rep (E1) case
ca7e6c26 7436
f3e0f7c3
BD
7437 elsif Is_Enumeration_Type (Entity (P)) then
7438 Enum_Expr := E1;
ca7e6c26 7439
f3e0f7c3
BD
7440 -- Otherwise the attribute must be expanded into a
7441 -- conversion and evaluated at run time.
ca7e6c26 7442
f3e0f7c3
BD
7443 else
7444 Check_Expressions;
7445 return;
7446 end if;
7447
7448 -- We can fold if the expression is an enumeration
0c3f76ba
AC
7449 -- literal, or if it denotes a constant whose value
7450 -- is known at compile time.
f3e0f7c3
BD
7451
7452 if Nkind (Enum_Expr) in N_Has_Entity
7453 and then (Ekind (Entity (Enum_Expr)) =
7454 E_Enumeration_Literal
7455 or else
7456 (Ekind (Entity (Enum_Expr)) = E_Constant
7457 and then Nkind (Parent (Entity (Enum_Expr))) =
7458 N_Object_Declaration
0c3f76ba 7459 and then Compile_Time_Known_Value
f3e0f7c3
BD
7460 (Expression (Parent (Entity (P))))))
7461 then
7462 P_Entity := Etype (P);
7463 else
7464 Check_Expressions;
7465 return;
7466 end if;
7467 end;
ca7e6c26
AC
7468
7469 -- Otherwise the attribute is illegal, do not attempt to perform
7470 -- any kind of folding.
7471
7472 else
7473 return;
7474 end if;
7475
996ae0b0
RK
7476 -- For First and Last, the prefix is an array object, and we apply
7477 -- the attribute to the type of the array, but we need a constrained
7478 -- type for this, so we use the actual subtype if available.
7479
edab6088
RD
7480 elsif Id = Attribute_First or else
7481 Id = Attribute_Last or else
996ae0b0
RK
7482 Id = Attribute_Length
7483 then
7484 declare
7485 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
7486
7487 begin
fbf5a39b 7488 if Present (AS) and then Is_Constrained (AS) then
996ae0b0
RK
7489 P_Entity := AS;
7490
df46b832 7491 -- If we have an unconstrained type we cannot fold
996ae0b0
RK
7492
7493 else
7494 Check_Expressions;
7495 return;
7496 end if;
7497 end;
7498
7499 -- For Size, give size of object if available, otherwise we
7500 -- cannot fold Size.
7501
7502 elsif Id = Attribute_Size then
996ae0b0
RK
7503 if Is_Entity_Name (P)
7504 and then Known_Esize (Entity (P))
7505 then
32213142 7506 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
996ae0b0
RK
7507 return;
7508
7509 else
7510 Check_Expressions;
7511 return;
7512 end if;
7513
7514 -- For Alignment, give size of object if available, otherwise we
7515 -- cannot fold Alignment.
7516
7517 elsif Id = Attribute_Alignment then
996ae0b0
RK
7518 if Is_Entity_Name (P)
7519 and then Known_Alignment (Entity (P))
7520 then
edab6088 7521 Fold_Uint (N, Alignment (Entity (P)), Static);
996ae0b0
RK
7522 return;
7523
7524 else
7525 Check_Expressions;
7526 return;
7527 end if;
7528
0ebc109a
VP
7529 -- For Lock_Free, we apply the attribute to the type of the object.
7530 -- This is allowed since we have already verified that the type is a
7531 -- protected type.
7532
7533 elsif Id = Attribute_Lock_Free then
7534 P_Entity := Etype (P);
7535
996ae0b0
RK
7536 -- No other attributes for objects are folded
7537
7538 else
7539 Check_Expressions;
7540 return;
7541 end if;
7542
f7ea2603
RD
7543 -- Cases where P is not an object. Cannot do anything if P is not the
7544 -- name of an entity.
996ae0b0
RK
7545
7546 elsif not Is_Entity_Name (P) then
7547 Check_Expressions;
7548 return;
7549
7550 -- Otherwise get prefix entity
7551
7552 else
7553 P_Entity := Entity (P);
7554 end if;
7555
edab6088
RD
7556 -- If we are asked to evaluate an attribute where the prefix is a
7557 -- non-frozen generic actual type whose RM_Size is still set to zero,
4bd4bb7f 7558 -- then abandon the effort.
edab6088
RD
7559
7560 if Is_Type (P_Entity)
7561 and then (not Is_Frozen (P_Entity)
7562 and then Is_Generic_Actual_Type (P_Entity)
7563 and then RM_Size (P_Entity) = 0)
4bd4bb7f
AC
7564
7565 -- However, the attribute Unconstrained_Array must be evaluated,
7566 -- since it is documented to be a static attribute (and can for
7567 -- example appear in a Compile_Time_Warning pragma). The frozen
7568 -- status of the type does not affect its evaluation.
7569
7570 and then Id /= Attribute_Unconstrained_Array
edab6088
RD
7571 then
7572 return;
7573 end if;
7574
996ae0b0
RK
7575 -- At this stage P_Entity is the entity to which the attribute
7576 -- is to be applied. This is usually simply the entity of the
7577 -- prefix, except in some cases of attributes for objects, where
7578 -- as described above, we apply the attribute to the object type.
7579
edab6088
RD
7580 -- Here is where we make sure that static attributes are properly
7581 -- marked as such. These are attributes whose prefix is a static
7582 -- scalar subtype, whose result is scalar, and whose arguments, if
7583 -- present, are static scalar expressions. Note that such references
7584 -- are static expressions even if they raise Constraint_Error.
7585
7586 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
7587 -- though evaluating it raises constraint error. This means that a
7588 -- declaration like:
7589
7590 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
7591
7592 -- is legal, since here this expression appears in a statically
7593 -- unevaluated position, so it does not actually raise an exception.
7594
7595 if Is_Scalar_Type (P_Entity)
7596 and then (not Is_Generic_Type (P_Entity))
7597 and then Is_Static_Subtype (P_Entity)
7598 and then Is_Scalar_Type (Etype (N))
7599 and then
7600 (No (E1)
7601 or else (Is_Static_Expression (E1)
7602 and then Is_Scalar_Type (Etype (E1))))
7603 and then
7604 (No (E2)
7605 or else (Is_Static_Expression (E2)
7606 and then Is_Scalar_Type (Etype (E1))))
7607 then
7608 Static := True;
7609 Set_Is_Static_Expression (N, True);
7610 end if;
7611
996ae0b0
RK
7612 -- First foldable possibility is a scalar or array type (RM 4.9(7))
7613 -- that is not generic (generic types are eliminated by RM 4.9(25)).
7614 -- Note we allow non-static non-generic types at this stage as further
7615 -- described below.
7616
7617 if Is_Type (P_Entity)
7618 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
7619 and then (not Is_Generic_Type (P_Entity))
7620 then
7621 P_Type := P_Entity;
7622
7623 -- Second foldable possibility is an array object (RM 4.9(8))
7624
21db8699 7625 elsif Ekind_In (P_Entity, E_Variable, E_Constant)
996ae0b0
RK
7626 and then Is_Array_Type (Etype (P_Entity))
7627 and then (not Is_Generic_Type (Etype (P_Entity)))
7628 then
7629 P_Type := Etype (P_Entity);
7630
b8dc622e
JM
7631 -- If the entity is an array constant with an unconstrained nominal
7632 -- subtype then get the type from the initial value. If the value has
7633 -- been expanded into assignments, there is no expression and the
7634 -- attribute reference remains dynamic.
4c4e9ad2 7635
996ae0b0
RK
7636 -- We could do better here and retrieve the type ???
7637
7638 if Ekind (P_Entity) = E_Constant
7639 and then not Is_Constrained (P_Type)
7640 then
7641 if No (Constant_Value (P_Entity)) then
7642 return;
7643 else
7644 P_Type := Etype (Constant_Value (P_Entity));
7645 end if;
7646 end if;
7647
edab6088
RD
7648 -- Definite must be folded if the prefix is not a generic type, that
7649 -- is to say if we are within an instantiation. Same processing applies
7650 -- to the GNAT attributes Atomic_Always_Lock_Free, Has_Discriminants,
7651 -- Lock_Free, Type_Class, Has_Tagged_Value, and Unconstrained_Array.
996ae0b0 7652
edab6088
RD
7653 elsif (Id = Attribute_Atomic_Always_Lock_Free or else
7654 Id = Attribute_Definite or else
7655 Id = Attribute_Has_Access_Values or else
7656 Id = Attribute_Has_Discriminants or else
7657 Id = Attribute_Has_Tagged_Values or else
7658 Id = Attribute_Lock_Free or else
7659 Id = Attribute_Type_Class or else
7660 Id = Attribute_Unconstrained_Array or else
4adf3c50 7661 Id = Attribute_Max_Alignment_For_Allocation)
996ae0b0
RK
7662 and then not Is_Generic_Type (P_Entity)
7663 then
7664 P_Type := P_Entity;
7665
e10dab7f
JM
7666 -- We can fold 'Size applied to a type if the size is known (as happens
7667 -- for a size from an attribute definition clause). At this stage, this
7668 -- can happen only for types (e.g. record types) for which the size is
7669 -- always non-static. We exclude generic types from consideration (since
7670 -- they have bogus sizes set within templates).
996ae0b0
RK
7671
7672 elsif Id = Attribute_Size
7673 and then Is_Type (P_Entity)
7674 and then (not Is_Generic_Type (P_Entity))
7675 and then Known_Static_RM_Size (P_Entity)
7676 then
32213142 7677 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
996ae0b0
RK
7678 return;
7679
fbf5a39b
AC
7680 -- We can fold 'Alignment applied to a type if the alignment is known
7681 -- (as happens for an alignment from an attribute definition clause).
f7ea2603
RD
7682 -- At this stage, this can happen only for types (e.g. record types) for
7683 -- which the size is always non-static. We exclude generic types from
7684 -- consideration (since they have bogus sizes set within templates).
fbf5a39b
AC
7685
7686 elsif Id = Attribute_Alignment
7687 and then Is_Type (P_Entity)
7688 and then (not Is_Generic_Type (P_Entity))
7689 and then Known_Alignment (P_Entity)
7690 then
7691 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
7692 return;
7693
acd47d2a
AC
7694 -- If this is an access attribute that is known to fail accessibility
7695 -- check, rewrite accordingly.
7696
7697 elsif Attribute_Name (N) = Name_Access
7698 and then Raises_Constraint_Error (N)
7699 then
7700 Rewrite (N,
c01a9391
AC
7701 Make_Raise_Program_Error (Loc,
7702 Reason => PE_Accessibility_Check_Failed));
acd47d2a
AC
7703 Set_Etype (N, C_Type);
7704 return;
7705
996ae0b0 7706 -- No other cases are foldable (they certainly aren't static, and at
2a1f6a1f 7707 -- the moment we don't try to fold any cases other than the ones above).
996ae0b0
RK
7708
7709 else
7710 Check_Expressions;
7711 return;
7712 end if;
7713
7714 -- If either attribute or the prefix is Any_Type, then propagate
7715 -- Any_Type to the result and don't do anything else at all.
7716
7717 if P_Type = Any_Type
7718 or else (Present (E1) and then Etype (E1) = Any_Type)
7719 or else (Present (E2) and then Etype (E2) = Any_Type)
7720 then
7721 Set_Etype (N, Any_Type);
7722 return;
7723 end if;
7724
7725 -- Scalar subtype case. We have not yet enforced the static requirement
7726 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
7727 -- of non-static attribute references (e.g. S'Digits for a non-static
7728 -- floating-point type, which we can compute at compile time).
7729
7730 -- Note: this folding of non-static attributes is not simply a case of
7731 -- optimization. For many of the attributes affected, Gigi cannot handle
7732 -- the attribute and depends on the front end having folded them away.
7733
7734 -- Note: although we don't require staticness at this stage, we do set
7735 -- the Static variable to record the staticness, for easy reference by
7736 -- those attributes where it matters (e.g. Succ and Pred), and also to
7737 -- be used to ensure that non-static folded things are not marked as
7738 -- being static (a check that is done right at the end).
7739
7740 P_Root_Type := Root_Type (P_Type);
7741 P_Base_Type := Base_Type (P_Type);
7742
7743 -- If the root type or base type is generic, then we cannot fold. This
7744 -- test is needed because subtypes of generic types are not always
7745 -- marked as being generic themselves (which seems odd???)
7746
7747 if Is_Generic_Type (P_Root_Type)
7748 or else Is_Generic_Type (P_Base_Type)
7749 then
7750 return;
7751 end if;
7752
7753 if Is_Scalar_Type (P_Type) then
edab6088
RD
7754 if not Is_Static_Subtype (P_Type) then
7755 Static := False;
7756 Set_Is_Static_Expression (N, False);
7757 elsif not Is_OK_Static_Subtype (P_Type) then
7758 Set_Raises_Constraint_Error (N);
7759 end if;
996ae0b0
RK
7760
7761 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
7762 -- since we can't do anything with unconstrained arrays. In addition,
7763 -- only the First, Last and Length attributes are possibly static.
996ae0b0 7764
0ebc109a
VP
7765 -- Atomic_Always_Lock_Free, Definite, Has_Access_Values,
7766 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
7767 -- Unconstrained_Array are again exceptions, because they apply as well
7768 -- to unconstrained types.
996ae0b0 7769
c4e5e10f
TQ
7770 -- In addition Component_Size is an exception since it is possibly
7771 -- foldable, even though it is never static, and it does apply to
7772 -- unconstrained arrays. Furthermore, it is essential to fold this
7773 -- in the packed case, since otherwise the value will be incorrect.
7774
edab6088
RD
7775 elsif Id = Attribute_Atomic_Always_Lock_Free or else
7776 Id = Attribute_Definite or else
7777 Id = Attribute_Has_Access_Values or else
7778 Id = Attribute_Has_Discriminants or else
7779 Id = Attribute_Has_Tagged_Values or else
7780 Id = Attribute_Lock_Free or else
7781 Id = Attribute_Type_Class or else
7782 Id = Attribute_Unconstrained_Array or else
c4e5e10f 7783 Id = Attribute_Component_Size
996ae0b0
RK
7784 then
7785 Static := False;
edab6088 7786 Set_Is_Static_Expression (N, False);
996ae0b0 7787
4adf3c50 7788 elsif Id /= Attribute_Max_Alignment_For_Allocation then
996ae0b0 7789 if not Is_Constrained (P_Type)
c4e5e10f
TQ
7790 or else (Id /= Attribute_First and then
7791 Id /= Attribute_Last and then
996ae0b0
RK
7792 Id /= Attribute_Length)
7793 then
7794 Check_Expressions;
7795 return;
7796 end if;
7797
7798 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
7799 -- scalar case, we hold off on enforcing staticness, since there are
7800 -- cases which we can fold at compile time even though they are not
7801 -- static (e.g. 'Length applied to a static index, even though other
7802 -- non-static indexes make the array type non-static). This is only
fbf5a39b 7803 -- an optimization, but it falls out essentially free, so why not.
996ae0b0
RK
7804 -- Again we compute the variable Static for easy reference later
7805 -- (note that no array attributes are static in Ada 83).
8dbd1460 7806
2ddc2000 7807 -- We also need to set Static properly for subsequent legality checks
df46b832
AC
7808 -- which might otherwise accept non-static constants in contexts
7809 -- where they are not legal.
996ae0b0 7810
edab6088
RD
7811 Static :=
7812 Ada_Version >= Ada_95 and then Statically_Denotes_Entity (P);
7813 Set_Is_Static_Expression (N, Static);
996ae0b0
RK
7814
7815 declare
edab6088 7816 Nod : Node_Id;
996ae0b0
RK
7817
7818 begin
edab6088 7819 Nod := First_Index (P_Type);
df46b832
AC
7820
7821 -- The expression is static if the array type is constrained
7822 -- by given bounds, and not by an initial expression. Constant
7823 -- strings are static in any case.
7824
7825 if Root_Type (P_Type) /= Standard_String then
7826 Static :=
7827 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
edab6088 7828 Set_Is_Static_Expression (N, Static);
df46b832
AC
7829 end if;
7830
edab6088
RD
7831 while Present (Nod) loop
7832 if not Is_Static_Subtype (Etype (Nod)) then
7833 Static := False;
7834 Set_Is_Static_Expression (N, False);
99425ec3 7835
edab6088
RD
7836 elsif not Is_OK_Static_Subtype (Etype (Nod)) then
7837 Set_Raises_Constraint_Error (N);
99425ec3
AC
7838 Static := False;
7839 Set_Is_Static_Expression (N, False);
edab6088 7840 end if;
fbf5a39b 7841
b4d7b435
AC
7842 -- If however the index type is generic, or derived from
7843 -- one, attributes cannot be folded.
fbf5a39b 7844
edab6088 7845 if Is_Generic_Type (Root_Type (Etype (Nod)))
fbf5a39b
AC
7846 and then Id /= Attribute_Component_Size
7847 then
7848 return;
7849 end if;
7850
edab6088 7851 Next_Index (Nod);
996ae0b0
RK
7852 end loop;
7853 end;
7854 end if;
7855
7856 -- Check any expressions that are present. Note that these expressions,
7857 -- depending on the particular attribute type, are either part of the
7858 -- attribute designator, or they are arguments in a case where the
7859 -- attribute reference returns a function. In the latter case, the
7860 -- rule in (RM 4.9(22)) applies and in particular requires the type
7861 -- of the expressions to be scalar in order for the attribute to be
7862 -- considered to be static.
7863
7864 declare
7865 E : Node_Id;
7866
7867 begin
7868 E := E1;
99425ec3 7869
996ae0b0
RK
7870 while Present (E) loop
7871
7872 -- If expression is not static, then the attribute reference
fbf5a39b
AC
7873 -- result certainly cannot be static.
7874
7875 if not Is_Static_Expression (E) then
7876 Static := False;
edab6088
RD
7877 Set_Is_Static_Expression (N, False);
7878 end if;
7879
7880 if Raises_Constraint_Error (E) then
7881 Set_Raises_Constraint_Error (N);
fbf5a39b 7882 end if;
996ae0b0 7883
fbf5a39b
AC
7884 -- If the result is not known at compile time, or is not of
7885 -- a scalar type, then the result is definitely not static,
7886 -- so we can quit now.
996ae0b0 7887
fbf5a39b 7888 if not Compile_Time_Known_Value (E)
996ae0b0
RK
7889 or else not Is_Scalar_Type (Etype (E))
7890 then
fbf5a39b
AC
7891 -- An odd special case, if this is a Pos attribute, this
7892 -- is where we need to apply a range check since it does
7893 -- not get done anywhere else.
7894
996ae0b0
RK
7895 if Id = Attribute_Pos then
7896 if Is_Integer_Type (Etype (E)) then
7897 Apply_Range_Check (E, Etype (N));
7898 end if;
7899 end if;
7900
7901 Check_Expressions;
7902 return;
7903
7904 -- If the expression raises a constraint error, then so does
7905 -- the attribute reference. We keep going in this case because
7906 -- we are still interested in whether the attribute reference
7907 -- is static even if it is not static.
7908
7909 elsif Raises_Constraint_Error (E) then
7910 Set_Raises_Constraint_Error (N);
7911 end if;
7912
7913 Next (E);
7914 end loop;
7915
7916 if Raises_Constraint_Error (Prefix (N)) then
99425ec3 7917 Set_Is_Static_Expression (N, False);
996ae0b0
RK
7918 return;
7919 end if;
7920 end;
7921
7922 -- Deal with the case of a static attribute reference that raises
7923 -- constraint error. The Raises_Constraint_Error flag will already
7924 -- have been set, and the Static flag shows whether the attribute
7925 -- reference is static. In any case we certainly can't fold such an
7926 -- attribute reference.
7927
7928 -- Note that the rewriting of the attribute node with the constraint
7929 -- error node is essential in this case, because otherwise Gigi might
7930 -- blow up on one of the attributes it never expects to see.
7931
7932 -- The constraint_error node must have the type imposed by the context,
7933 -- to avoid spurious errors in the enclosing expression.
7934
7935 if Raises_Constraint_Error (N) then
7936 CE_Node :=
07fc65c4
GB
7937 Make_Raise_Constraint_Error (Sloc (N),
7938 Reason => CE_Range_Check_Failed);
996ae0b0
RK
7939 Set_Etype (CE_Node, Etype (N));
7940 Set_Raises_Constraint_Error (CE_Node);
7941 Check_Expressions;
7942 Rewrite (N, Relocate_Node (CE_Node));
edab6088 7943 Set_Raises_Constraint_Error (N, True);
996ae0b0
RK
7944 return;
7945 end if;
7946
7947 -- At this point we have a potentially foldable attribute reference.
7948 -- If Static is set, then the attribute reference definitely obeys
7949 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
7950 -- folded. If Static is not set, then the attribute may or may not
7951 -- be foldable, and the individual attribute processing routines
7952 -- test Static as required in cases where it makes a difference.
7953
fbf5a39b 7954 -- In the case where Static is not set, we do know that all the
bb3c784c
AC
7955 -- expressions present are at least known at compile time (we assumed
7956 -- above that if this was not the case, then there was no hope of static
7957 -- evaluation). However, we did not require that the bounds of the
7958 -- prefix type be compile time known, let alone static). That's because
7959 -- there are many attributes that can be computed at compile time on
7960 -- non-static subtypes, even though such references are not static
7961 -- expressions.
fbf5a39b 7962
7b55fea6
AC
7963 -- For VAX float, the root type is an IEEE type. So make sure to use the
7964 -- base type instead of the root-type for floating point attributes.
7965
996ae0b0
RK
7966 case Id is
7967
4199e8c6 7968 -- Attributes related to Ada 2012 iterators (placeholder ???)
0da80d7d 7969
4199e8c6
RD
7970 when Attribute_Constant_Indexing |
7971 Attribute_Default_Iterator |
7972 Attribute_Implicit_Dereference |
7973 Attribute_Iterator_Element |
7974 Attribute_Iterable |
7975 Attribute_Variable_Indexing => null;
b98e2969 7976
4199e8c6
RD
7977 -- Internal attributes used to deal with Ada 2012 delayed aspects.
7978 -- These were already rejected by the parser. Thus they shouldn't
7979 -- appear here.
b98e2969 7980
4199e8c6
RD
7981 when Internal_Attribute_Id =>
7982 raise Program_Error;
0da80d7d 7983
996ae0b0
RK
7984 --------------
7985 -- Adjacent --
7986 --------------
7987
7988 when Attribute_Adjacent =>
af31bd57
AC
7989 Fold_Ureal
7990 (N,
7991 Eval_Fat.Adjacent
7992 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
7993 Static);
996ae0b0
RK
7994
7995 ---------
7996 -- Aft --
7997 ---------
7998
7999 when Attribute_Aft =>
edab6088 8000 Fold_Uint (N, Aft_Value (P_Type), Static);
996ae0b0
RK
8001
8002 ---------------
8003 -- Alignment --
8004 ---------------
8005
8006 when Attribute_Alignment => Alignment_Block : declare
8007 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8008
8009 begin
8010 -- Fold if alignment is set and not otherwise
8011
8012 if Known_Alignment (P_TypeA) then
edab6088 8013 Fold_Uint (N, Alignment (P_TypeA), Static);
996ae0b0
RK
8014 end if;
8015 end Alignment_Block;
8016
0ebc109a
VP
8017 -----------------------------
8018 -- Atomic_Always_Lock_Free --
8019 -----------------------------
8020
8021 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8022 -- here.
8023
8024 when Attribute_Atomic_Always_Lock_Free => Atomic_Always_Lock_Free :
8025 declare
8026 V : constant Entity_Id :=
8027 Boolean_Literals
8028 (Support_Atomic_Primitives_On_Target
8029 and then Support_Atomic_Primitives (P_Type));
8030
8031 begin
8032 Rewrite (N, New_Occurrence_Of (V, Loc));
8033
8034 -- Analyze and resolve as boolean. Note that this attribute is a
8035 -- static attribute in GNAT.
8036
8037 Analyze_And_Resolve (N, Standard_Boolean);
edab6088
RD
8038 Static := True;
8039 Set_Is_Static_Expression (N, True);
0ebc109a
VP
8040 end Atomic_Always_Lock_Free;
8041
996ae0b0
RK
8042 ---------
8043 -- Bit --
8044 ---------
8045
8046 -- Bit can never be folded
8047
8048 when Attribute_Bit =>
8049 null;
8050
8051 ------------------
8052 -- Body_Version --
8053 ------------------
8054
8055 -- Body_version can never be static
8056
8057 when Attribute_Body_Version =>
8058 null;
8059
8060 -------------
8061 -- Ceiling --
8062 -------------
8063
8064 when Attribute_Ceiling =>
af31bd57
AC
8065 Fold_Ureal
8066 (N, Eval_Fat.Ceiling (P_Base_Type, Expr_Value_R (E1)), Static);
996ae0b0
RK
8067
8068 --------------------
8069 -- Component_Size --
8070 --------------------
8071
8072 when Attribute_Component_Size =>
fbf5a39b 8073 if Known_Static_Component_Size (P_Type) then
edab6088 8074 Fold_Uint (N, Component_Size (P_Type), Static);
996ae0b0
RK
8075 end if;
8076
8077 -------------
8078 -- Compose --
8079 -------------
8080
8081 when Attribute_Compose =>
af31bd57
AC
8082 Fold_Ureal
8083 (N,
8084 Eval_Fat.Compose (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8085 Static);
996ae0b0
RK
8086
8087 -----------------
8088 -- Constrained --
8089 -----------------
8090
8091 -- Constrained is never folded for now, there may be cases that
12a13f01 8092 -- could be handled at compile time. To be looked at later.
996ae0b0
RK
8093
8094 when Attribute_Constrained =>
6c56d9b8
AC
8095
8096 -- The expander might fold it and set the static flag accordingly,
8097 -- but with expansion disabled (as in ASIS), it remains as an
8098 -- attribute reference, and this reference is not static.
8099
8100 Set_Is_Static_Expression (N, False);
996ae0b0
RK
8101 null;
8102
8103 ---------------
8104 -- Copy_Sign --
8105 ---------------
8106
8107 when Attribute_Copy_Sign =>
af31bd57
AC
8108 Fold_Ureal
8109 (N,
8110 Eval_Fat.Copy_Sign
8111 (P_Base_Type, Expr_Value_R (E1), Expr_Value_R (E2)),
8112 Static);
996ae0b0 8113
996ae0b0
RK
8114 --------------
8115 -- Definite --
8116 --------------
8117
8118 when Attribute_Definite =>
aa720a54 8119 Rewrite (N, New_Occurrence_Of (
83496138 8120 Boolean_Literals (Is_Definite_Subtype (P_Entity)), Loc));
aa720a54 8121 Analyze_And_Resolve (N, Standard_Boolean);
996ae0b0 8122
a01b9df6
AC
8123 -----------
8124 -- Delta --
8125 -----------
8126
8127 when Attribute_Delta =>
8128 Fold_Ureal (N, Delta_Value (P_Type), True);
8129
996ae0b0
RK
8130 ------------
8131 -- Denorm --
8132 ------------
8133
8134 when Attribute_Denorm =>
8135 Fold_Uint
edab6088 8136 (N, UI_From_Int (Boolean'Pos (Has_Denormals (P_Type))), Static);
996ae0b0 8137
203ddcea
AC
8138 ---------------------
8139 -- Descriptor_Size --
8140 ---------------------
8141
8142 when Attribute_Descriptor_Size =>
8143 null;
8144
996ae0b0
RK
8145 ------------
8146 -- Digits --
8147 ------------
8148
8149 when Attribute_Digits =>
edab6088 8150 Fold_Uint (N, Digits_Value (P_Type), Static);
996ae0b0
RK
8151
8152 ----------
8153 -- Emax --
8154 ----------
8155
8156 when Attribute_Emax =>
8157
8158 -- Ada 83 attribute is defined as (RM83 3.5.8)
8159
8160 -- T'Emax = 4 * T'Mantissa
8161
edab6088 8162 Fold_Uint (N, 4 * Mantissa, Static);
996ae0b0
RK
8163
8164 --------------
8165 -- Enum_Rep --
8166 --------------
8167
ca7e6c26
AC
8168 when Attribute_Enum_Rep => Enum_Rep : declare
8169 Val : Node_Id;
8170
8171 begin
bed87f4f 8172 -- The attribute appears in the form:
ca7e6c26
AC
8173
8174 -- Enum_Typ'Enum_Rep (Const)
8175 -- Enum_Typ'Enum_Rep (Enum_Lit)
8176
8177 if Present (E1) then
8178 Val := E1;
8179
bed87f4f 8180 -- Otherwise the prefix denotes a constant or enumeration literal:
ca7e6c26
AC
8181
8182 -- Const'Enum_Rep
8183 -- Enum_Lit'Enum_Rep
8184
8185 else
8186 Val := P;
8187 end if;
996ae0b0 8188
82c80734
RD
8189 -- For an enumeration type with a non-standard representation use
8190 -- the Enumeration_Rep field of the proper constant. Note that this
8191 -- will not work for types Character/Wide_[Wide-]Character, since no
8192 -- real entities are created for the enumeration literals, but that
8193 -- does not matter since these two types do not have non-standard
8194 -- representations anyway.
996ae0b0 8195
fbf5a39b
AC
8196 if Is_Enumeration_Type (P_Type)
8197 and then Has_Non_Standard_Rep (P_Type)
8198 then
ca7e6c26 8199 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (Val)), Static);
996ae0b0 8200
ca7e6c26
AC
8201 -- For enumeration types with standard representations and all other
8202 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8203 -- to Pos.
996ae0b0 8204
fbf5a39b 8205 else
ca7e6c26 8206 Fold_Uint (N, Expr_Value (Val), Static);
996ae0b0 8207 end if;
ca7e6c26 8208 end Enum_Rep;
996ae0b0 8209
21d27997
RD
8210 --------------
8211 -- Enum_Val --
8212 --------------
8213
8214 when Attribute_Enum_Val => Enum_Val : declare
8215 Lit : Node_Id;
8216
8217 begin
8218 -- We have something like Enum_Type'Enum_Val (23), so search for a
8219 -- corresponding value in the list of Enum_Rep values for the type.
8220
8221 Lit := First_Literal (P_Base_Type);
8222 loop
8223 if Enumeration_Rep (Lit) = Expr_Value (E1) then
8224 Fold_Uint (N, Enumeration_Pos (Lit), Static);
8225 exit;
8226 end if;
8227
8228 Next_Literal (Lit);
8229
8230 if No (Lit) then
8231 Apply_Compile_Time_Constraint_Error
8232 (N, "no representation value matches",
8233 CE_Range_Check_Failed,
8234 Warn => not Static);
8235 exit;
8236 end if;
8237 end loop;
8238 end Enum_Val;
8239
996ae0b0
RK
8240 -------------
8241 -- Epsilon --
8242 -------------
8243
8244 when Attribute_Epsilon =>
8245
8246 -- Ada 83 attribute is defined as (RM83 3.5.8)
8247
8248 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8249
fbf5a39b 8250 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
996ae0b0
RK
8251
8252 --------------
8253 -- Exponent --
8254 --------------
8255
8256 when Attribute_Exponent =>
fbf5a39b 8257 Fold_Uint (N,
7b55fea6 8258 Eval_Fat.Exponent (P_Base_Type, Expr_Value_R (E1)), Static);
996ae0b0
RK
8259
8260 -----------
8261 -- First --
8262 -----------
8263
8264 when Attribute_First => First_Attr :
8265 begin
8266 Set_Bounds;
8267
8268 if Compile_Time_Known_Value (Lo_Bound) then
8269 if Is_Real_Type (P_Type) then
fbf5a39b 8270 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
996ae0b0 8271 else
fbf5a39b 8272 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
996ae0b0 8273 end if;
6ca9ec9c 8274
5a153b27
AC
8275 else
8276 Check_Concurrent_Discriminant (Lo_Bound);
996ae0b0
RK
8277 end if;
8278 end First_Attr;
8279
011f9d5d
AC
8280 -----------------
8281 -- First_Valid --
8282 -----------------
8283
8284 when Attribute_First_Valid => First_Valid :
8285 begin
8286 if Has_Predicates (P_Type)
60f908dd 8287 and then Has_Static_Predicate (P_Type)
011f9d5d
AC
8288 then
8289 declare
60f908dd
RD
8290 FirstN : constant Node_Id :=
8291 First (Static_Discrete_Predicate (P_Type));
011f9d5d
AC
8292 begin
8293 if Nkind (FirstN) = N_Range then
8294 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
8295 else
8296 Fold_Uint (N, Expr_Value (FirstN), Static);
8297 end if;
8298 end;
8299
8300 else
8301 Set_Bounds;
8302 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
8303 end if;
8304 end First_Valid;
8305
996ae0b0
RK
8306 -----------------
8307 -- Fixed_Value --
8308 -----------------
8309
8310 when Attribute_Fixed_Value =>
8311 null;
8312
8313 -----------
8314 -- Floor --
8315 -----------
8316
8317 when Attribute_Floor =>
af31bd57
AC
8318 Fold_Ureal
8319 (N, Eval_Fat.Floor (P_Base_Type, Expr_Value_R (E1)), Static);
996ae0b0
RK
8320
8321 ----------
8322 -- Fore --
8323 ----------
8324
8325 when Attribute_Fore =>
fbf5a39b
AC
8326 if Compile_Time_Known_Bounds (P_Type) then
8327 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
996ae0b0
RK
8328 end if;
8329
8330 --------------
8331 -- Fraction --
8332 --------------
8333
8334 when Attribute_Fraction =>
af31bd57
AC
8335 Fold_Ureal
8336 (N, Eval_Fat.Fraction (P_Base_Type, Expr_Value_R (E1)), Static);
996ae0b0 8337
15ce9ca2
AC
8338 -----------------------
8339 -- Has_Access_Values --
8340 -----------------------
8341
8342 when Attribute_Has_Access_Values =>
8343 Rewrite (N, New_Occurrence_Of
8344 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
8345 Analyze_And_Resolve (N, Standard_Boolean);
8346
996ae0b0
RK
8347 -----------------------
8348 -- Has_Discriminants --
8349 -----------------------
8350
8351 when Attribute_Has_Discriminants =>
aa720a54
AC
8352 Rewrite (N, New_Occurrence_Of (
8353 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
8354 Analyze_And_Resolve (N, Standard_Boolean);
996ae0b0 8355
ea70f3d0
RD
8356 ----------------------
8357 -- Has_Same_Storage --
8358 ----------------------
8359
8360 when Attribute_Has_Same_Storage =>
8361 null;
8362
21d27997
RD
8363 -----------------------
8364 -- Has_Tagged_Values --
8365 -----------------------
8366
8367 when Attribute_Has_Tagged_Values =>
8368 Rewrite (N, New_Occurrence_Of
8369 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
8370 Analyze_And_Resolve (N, Standard_Boolean);
8371
996ae0b0
RK
8372 --------------
8373 -- Identity --
8374 --------------
8375
8376 when Attribute_Identity =>
8377 null;
8378
8379 -----------
8380 -- Image --
8381 -----------
8382
8383 -- Image is a scalar attribute, but is never static, because it is
8384 -- not a static function (having a non-scalar argument (RM 4.9(22))
442ade9d
RD
8385 -- However, we can constant-fold the image of an enumeration literal
8386 -- if names are available.
996ae0b0
RK
8387
8388 when Attribute_Image =>
442ade9d
RD
8389 if Is_Entity_Name (E1)
8390 and then Ekind (Entity (E1)) = E_Enumeration_Literal
8391 and then not Discard_Names (First_Subtype (Etype (E1)))
8392 and then not Global_Discard_Names
8393 then
8394 declare
8395 Lit : constant Entity_Id := Entity (E1);
8396 Str : String_Id;
8397 begin
8398 Start_String;
8399 Get_Unqualified_Decoded_Name_String (Chars (Lit));
8400 Set_Casing (All_Upper_Case);
8401 Store_String_Chars (Name_Buffer (1 .. Name_Len));
8402 Str := End_String;
8403 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
8404 Analyze_And_Resolve (N, Standard_String);
8405 Set_Is_Static_Expression (N, False);
8406 end;
8407 end if;
996ae0b0 8408
996ae0b0
RK
8409 -------------------
8410 -- Integer_Value --
8411 -------------------
8412
21d27997
RD
8413 -- We never try to fold Integer_Value (though perhaps we could???)
8414
996ae0b0
RK
8415 when Attribute_Integer_Value =>
8416 null;
8417
21d27997
RD
8418 -------------------
8419 -- Invalid_Value --
8420 -------------------
8421
8422 -- Invalid_Value is a scalar attribute that is never static, because
8423 -- the value is by design out of range.
8424
8425 when Attribute_Invalid_Value =>
8426 null;
8427
996ae0b0
RK
8428 -----------
8429 -- Large --
8430 -----------
8431
8432 when Attribute_Large =>
8433
8434 -- For fixed-point, we use the identity:
8435
8436 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
8437
8438 if Is_Fixed_Point_Type (P_Type) then
8439 Rewrite (N,
8440 Make_Op_Multiply (Loc,
8441 Left_Opnd =>
8442 Make_Op_Subtract (Loc,
8443 Left_Opnd =>
8444 Make_Op_Expon (Loc,
8445 Left_Opnd =>
8446 Make_Real_Literal (Loc, Ureal_2),
8447 Right_Opnd =>
8448 Make_Attribute_Reference (Loc,
8449 Prefix => P,
8450 Attribute_Name => Name_Mantissa)),
8451 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
8452
8453 Right_Opnd =>
8454 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
8455
8456 Analyze_And_Resolve (N, C_Type);
8457
8458 -- Floating-point (Ada 83 compatibility)
8459
8460 else
8461 -- Ada 83 attribute is defined as (RM83 3.5.8)
8462
8463 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
8464
8465 -- where
8466
8467 -- T'Emax = 4 * T'Mantissa
8468
af31bd57
AC
8469 Fold_Ureal
8470 (N,
8471 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
8472 True);
996ae0b0
RK
8473 end if;
8474
2a290fec
AC
8475 ---------------
8476 -- Lock_Free --
8477 ---------------
8478
0ebc109a
VP
8479 when Attribute_Lock_Free => Lock_Free : declare
8480 V : constant Entity_Id := Boolean_Literals (Uses_Lock_Free (P_Type));
2a290fec 8481
0ebc109a
VP
8482 begin
8483 Rewrite (N, New_Occurrence_Of (V, Loc));
8484
8485 -- Analyze and resolve as boolean. Note that this attribute is a
8486 -- static attribute in GNAT.
8487
8488 Analyze_And_Resolve (N, Standard_Boolean);
edab6088
RD
8489 Static := True;
8490 Set_Is_Static_Expression (N, True);
0ebc109a 8491 end Lock_Free;
2a290fec 8492
996ae0b0
RK
8493 ----------
8494 -- Last --
8495 ----------
8496
011f9d5d 8497 when Attribute_Last => Last_Attr :
996ae0b0
RK
8498 begin
8499 Set_Bounds;
8500
8501 if Compile_Time_Known_Value (Hi_Bound) then
8502 if Is_Real_Type (P_Type) then
fbf5a39b 8503 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
996ae0b0 8504 else
fbf5a39b 8505 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
996ae0b0 8506 end if;
6ca9ec9c 8507
5a153b27
AC
8508 else
8509 Check_Concurrent_Discriminant (Hi_Bound);
996ae0b0 8510 end if;
011f9d5d
AC
8511 end Last_Attr;
8512
8513 ----------------
8514 -- Last_Valid --
8515 ----------------
8516
8517 when Attribute_Last_Valid => Last_Valid :
8518 begin
8519 if Has_Predicates (P_Type)
60f908dd 8520 and then Has_Static_Predicate (P_Type)
011f9d5d
AC
8521 then
8522 declare
60f908dd
RD
8523 LastN : constant Node_Id :=
8524 Last (Static_Discrete_Predicate (P_Type));
011f9d5d
AC
8525 begin
8526 if Nkind (LastN) = N_Range then
8527 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
8528 else
8529 Fold_Uint (N, Expr_Value (LastN), Static);
8530 end if;
8531 end;
8532
8533 else
8534 Set_Bounds;
8535 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
8536 end if;
8537 end Last_Valid;
996ae0b0
RK
8538
8539 ------------------
8540 -- Leading_Part --
8541 ------------------
8542
8543 when Attribute_Leading_Part =>
af31bd57
AC
8544 Fold_Ureal
8545 (N,
8546 Eval_Fat.Leading_Part
8547 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
8548 Static);
996ae0b0
RK
8549
8550 ------------
8551 -- Length --
8552 ------------
8553
fbf5a39b
AC
8554 when Attribute_Length => Length : declare
8555 Ind : Node_Id;
8556
996ae0b0 8557 begin
b4d7b435
AC
8558 -- If any index type is a formal type, or derived from one, the
8559 -- bounds are not static. Treating them as static can produce
8560 -- spurious warnings or improper constant folding.
fbf5a39b
AC
8561
8562 Ind := First_Index (P_Type);
fbf5a39b 8563 while Present (Ind) loop
b4d7b435 8564 if Is_Generic_Type (Root_Type (Etype (Ind))) then
fbf5a39b
AC
8565 return;
8566 end if;
8567
8568 Next_Index (Ind);
8569 end loop;
8570
996ae0b0
RK
8571 Set_Bounds;
8572
af02a866
RD
8573 -- For two compile time values, we can compute length
8574
996ae0b0
RK
8575 if Compile_Time_Known_Value (Lo_Bound)
8576 and then Compile_Time_Known_Value (Hi_Bound)
8577 then
8578 Fold_Uint (N,
fbf5a39b 8579 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
edab6088 8580 Static);
996ae0b0 8581 end if;
af02a866
RD
8582
8583 -- One more case is where Hi_Bound and Lo_Bound are compile-time
8584 -- comparable, and we can figure out the difference between them.
8585
8586 declare
8587 Diff : aliased Uint;
8588
8589 begin
8590 case
8591 Compile_Time_Compare
8592 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
8593 is
8594 when EQ =>
edab6088 8595 Fold_Uint (N, Uint_1, Static);
af02a866
RD
8596
8597 when GT =>
edab6088 8598 Fold_Uint (N, Uint_0, Static);
af02a866
RD
8599
8600 when LT =>
8601 if Diff /= No_Uint then
edab6088 8602 Fold_Uint (N, Diff + 1, Static);
af02a866
RD
8603 end if;
8604
8605 when others =>
8606 null;
8607 end case;
8608 end;
996ae0b0
RK
8609 end Length;
8610
150ac76e
AC
8611 ----------------
8612 -- Loop_Entry --
8613 ----------------
8614
739e7bbf
AC
8615 -- Loop_Entry acts as an alias of a constant initialized to the prefix
8616 -- of the said attribute at the point of entry into the related loop. As
8617 -- such, the attribute reference does not need to be evaluated because
8618 -- the prefix is the one that is evaluted.
97ce3a14 8619
150ac76e
AC
8620 when Attribute_Loop_Entry =>
8621 null;
8622
996ae0b0
RK
8623 -------------
8624 -- Machine --
8625 -------------
8626
8627 when Attribute_Machine =>
af31bd57
AC
8628 Fold_Ureal
8629 (N,
8630 Eval_Fat.Machine
8631 (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
8632 Static);
996ae0b0
RK
8633
8634 ------------------
8635 -- Machine_Emax --
8636 ------------------
8637
8638 when Attribute_Machine_Emax =>
d32e3cee 8639 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
996ae0b0
RK
8640
8641 ------------------
8642 -- Machine_Emin --
8643 ------------------
8644
8645 when Attribute_Machine_Emin =>
d32e3cee 8646 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
996ae0b0
RK
8647
8648 ----------------------
8649 -- Machine_Mantissa --
8650 ----------------------
8651
8652 when Attribute_Machine_Mantissa =>
d32e3cee 8653 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
996ae0b0
RK
8654
8655 -----------------------
8656 -- Machine_Overflows --
8657 -----------------------
8658
8659 when Attribute_Machine_Overflows =>
8660
8661 -- Always true for fixed-point
8662
8663 if Is_Fixed_Point_Type (P_Type) then
edab6088 8664 Fold_Uint (N, True_Value, Static);
996ae0b0
RK
8665
8666 -- Floating point case
8667
8668 else
fbf5a39b
AC
8669 Fold_Uint (N,
8670 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
edab6088 8671 Static);
996ae0b0
RK
8672 end if;
8673
8674 -------------------
8675 -- Machine_Radix --
8676 -------------------
8677
8678 when Attribute_Machine_Radix =>
8679 if Is_Fixed_Point_Type (P_Type) then
8680 if Is_Decimal_Fixed_Point_Type (P_Type)
8681 and then Machine_Radix_10 (P_Type)
8682 then
edab6088 8683 Fold_Uint (N, Uint_10, Static);
996ae0b0 8684 else
edab6088 8685 Fold_Uint (N, Uint_2, Static);
996ae0b0
RK
8686 end if;
8687
8688 -- All floating-point type always have radix 2
8689
8690 else
edab6088 8691 Fold_Uint (N, Uint_2, Static);
996ae0b0
RK
8692 end if;
8693
65f01153
RD
8694 ----------------------
8695 -- Machine_Rounding --
8696 ----------------------
8697
8698 -- Note: for the folding case, it is fine to treat Machine_Rounding
8699 -- exactly the same way as Rounding, since this is one of the allowed
8700 -- behaviors, and performance is not an issue here. It might be a bit
e7c0dd39 8701 -- better to give the same result as it would give at run time, even
65f01153
RD
8702 -- though the non-determinism is certainly permitted.
8703
8704 when Attribute_Machine_Rounding =>
af31bd57
AC
8705 Fold_Ureal
8706 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
65f01153 8707
996ae0b0
RK
8708 --------------------
8709 -- Machine_Rounds --
8710 --------------------
8711
8712 when Attribute_Machine_Rounds =>
8713
8714 -- Always False for fixed-point
8715
8716 if Is_Fixed_Point_Type (P_Type) then
edab6088 8717 Fold_Uint (N, False_Value, Static);
996ae0b0
RK
8718
8719 -- Else yield proper floating-point result
8720
8721 else
8722 Fold_Uint
edab6088
RD
8723 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)),
8724 Static);
996ae0b0
RK
8725 end if;
8726
8727 ------------------
8728 -- Machine_Size --
8729 ------------------
8730
8731 -- Note: Machine_Size is identical to Object_Size
8732
8733 when Attribute_Machine_Size => Machine_Size : declare
8734 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8735
8736 begin
8737 if Known_Esize (P_TypeA) then
edab6088 8738 Fold_Uint (N, Esize (P_TypeA), Static);
996ae0b0
RK
8739 end if;
8740 end Machine_Size;
8741
8742 --------------
8743 -- Mantissa --
8744 --------------
8745
8746 when Attribute_Mantissa =>
8747
8748 -- Fixed-point mantissa
8749
8750 if Is_Fixed_Point_Type (P_Type) then
8751
8752 -- Compile time foldable case
8753
8754 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
8755 and then
8756 Compile_Time_Known_Value (Type_High_Bound (P_Type))
8757 then
8758 -- The calculation of the obsolete Ada 83 attribute Mantissa
8759 -- is annoying, because of AI00143, quoted here:
8760
8761 -- !question 84-01-10
8762
8763 -- Consider the model numbers for F:
8764
8765 -- type F is delta 1.0 range -7.0 .. 8.0;
8766
8767 -- The wording requires that F'MANTISSA be the SMALLEST
8768 -- integer number for which each bound of the specified
8769 -- range is either a model number or lies at most small
8770 -- distant from a model number. This means F'MANTISSA
8771 -- is required to be 3 since the range -7.0 .. 7.0 fits
8772 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
8773 -- number, namely, 7. Is this analysis correct? Note that
8774 -- this implies the upper bound of the range is not
8775 -- represented as a model number.
8776
8777 -- !response 84-03-17
8778
8779 -- The analysis is correct. The upper and lower bounds for
8780 -- a fixed point type can lie outside the range of model
8781 -- numbers.
8782
8783 declare
8784 Siz : Uint;
8785 LBound : Ureal;
8786 UBound : Ureal;
8787 Bound : Ureal;
8788 Max_Man : Uint;
8789
8790 begin
8791 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
8792 UBound := Expr_Value_R (Type_High_Bound (P_Type));
8793 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
8794 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
8795
8796 -- If the Bound is exactly a model number, i.e. a multiple
8797 -- of Small, then we back it off by one to get the integer
8798 -- value that must be representable.
8799
8800 if Small_Value (P_Type) * Max_Man = Bound then
8801 Max_Man := Max_Man - 1;
8802 end if;
8803
8804 -- Now find corresponding size = Mantissa value
8805
8806 Siz := Uint_0;
8807 while 2 ** Siz < Max_Man loop
8808 Siz := Siz + 1;
8809 end loop;
8810
edab6088 8811 Fold_Uint (N, Siz, Static);
996ae0b0
RK
8812 end;
8813
8814 else
8815 -- The case of dynamic bounds cannot be evaluated at compile
8816 -- time. Instead we use a runtime routine (see Exp_Attr).
8817
8818 null;
8819 end if;
8820
8821 -- Floating-point Mantissa
8822
8823 else
edab6088 8824 Fold_Uint (N, Mantissa, Static);
996ae0b0
RK
8825 end if;
8826
8827 ---------
8828 -- Max --
8829 ---------
8830
8831 when Attribute_Max => Max :
8832 begin
8833 if Is_Real_Type (P_Type) then
fbf5a39b
AC
8834 Fold_Ureal
8835 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
996ae0b0 8836 else
fbf5a39b 8837 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
996ae0b0
RK
8838 end if;
8839 end Max;
8840
4adf3c50
AC
8841 ----------------------------------
8842 -- Max_Alignment_For_Allocation --
8843 ----------------------------------
8844
8845 -- Max_Alignment_For_Allocation is usually the Alignment. However,
8846 -- arrays are allocated with dope, so we need to take into account both
8847 -- the alignment of the array, which comes from the component alignment,
8848 -- and the alignment of the dope. Also, if the alignment is unknown, we
8849 -- use the max (it's OK to be pessimistic).
8850
8851 when Attribute_Max_Alignment_For_Allocation =>
8852 declare
8853 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
8854 begin
8855 if Known_Alignment (P_Type) and then
8856 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
8857 then
8858 A := Alignment (P_Type);
8859 end if;
8860
8861 Fold_Uint (N, A, Static);
8862 end;
8863
996ae0b0
RK
8864 ----------------------------------
8865 -- Max_Size_In_Storage_Elements --
8866 ----------------------------------
8867
8868 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
8869 -- Storage_Unit boundary. We can fold any cases for which the size
8870 -- is known by the front end.
8871
8872 when Attribute_Max_Size_In_Storage_Elements =>
8873 if Known_Esize (P_Type) then
8874 Fold_Uint (N,
8875 (Esize (P_Type) + System_Storage_Unit - 1) /
fbf5a39b
AC
8876 System_Storage_Unit,
8877 Static);
996ae0b0
RK
8878 end if;
8879
8880 --------------------
8881 -- Mechanism_Code --
8882 --------------------
8883
8884 when Attribute_Mechanism_Code =>
8885 declare
8886 Val : Int;
8887 Formal : Entity_Id;
8888 Mech : Mechanism_Type;
8889
8890 begin
8891 if No (E1) then
8892 Mech := Mechanism (P_Entity);
8893
8894 else
8895 Val := UI_To_Int (Expr_Value (E1));
8896
8897 Formal := First_Formal (P_Entity);
8898 for J in 1 .. Val - 1 loop
8899 Next_Formal (Formal);
8900 end loop;
8901 Mech := Mechanism (Formal);
8902 end if;
8903
8904 if Mech < 0 then
edab6088 8905 Fold_Uint (N, UI_From_Int (Int (-Mech)), Static);
996ae0b0
RK
8906 end if;
8907 end;
8908
8909 ---------
8910 -- Min --
8911 ---------
8912
8913 when Attribute_Min => Min :
8914 begin
8915 if Is_Real_Type (P_Type) then
fbf5a39b
AC
8916 Fold_Ureal
8917 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
996ae0b0 8918 else
5f3ab6fb
AC
8919 Fold_Uint
8920 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
996ae0b0
RK
8921 end if;
8922 end Min;
8923
5f3ab6fb
AC
8924 ---------
8925 -- Mod --
8926 ---------
8927
8928 when Attribute_Mod =>
8929 Fold_Uint
8930 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
8931
996ae0b0
RK
8932 -----------
8933 -- Model --
8934 -----------
8935
8936 when Attribute_Model =>
af31bd57
AC
8937 Fold_Ureal
8938 (N, Eval_Fat.Model (P_Base_Type, Expr_Value_R (E1)), Static);
996ae0b0
RK
8939
8940 ----------------
8941 -- Model_Emin --
8942 ----------------
8943
8944 when Attribute_Model_Emin =>
d32e3cee 8945 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
996ae0b0
RK
8946
8947 -------------------
8948 -- Model_Epsilon --
8949 -------------------
8950
8951 when Attribute_Model_Epsilon =>
d32e3cee 8952 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
996ae0b0
RK
8953
8954 --------------------
8955 -- Model_Mantissa --
8956 --------------------
8957
8958 when Attribute_Model_Mantissa =>
d32e3cee 8959 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
996ae0b0
RK
8960
8961 -----------------
8962 -- Model_Small --
8963 -----------------
8964
8965 when Attribute_Model_Small =>
d32e3cee 8966 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
996ae0b0
RK
8967
8968 -------------
8969 -- Modulus --
8970 -------------
8971
8972 when Attribute_Modulus =>
edab6088 8973 Fold_Uint (N, Modulus (P_Type), Static);
996ae0b0
RK
8974
8975 --------------------
8976 -- Null_Parameter --
8977 --------------------
8978
8979 -- Cannot fold, we know the value sort of, but the whole point is
8980 -- that there is no way to talk about this imaginary value except
8981 -- by using the attribute, so we leave it the way it is.
8982
8983 when Attribute_Null_Parameter =>
8984 null;
8985
8986 -----------------
8987 -- Object_Size --
8988 -----------------
8989
8990 -- The Object_Size attribute for a type returns the Esize of the
8991 -- type and can be folded if this value is known.
8992
8993 when Attribute_Object_Size => Object_Size : declare
8994 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
8995
8996 begin
8997 if Known_Esize (P_TypeA) then
edab6088 8998 Fold_Uint (N, Esize (P_TypeA), Static);
996ae0b0
RK
8999 end if;
9000 end Object_Size;
9001
2d42e881
ES
9002 ----------------------
9003 -- Overlaps_Storage --
9004 ----------------------
9005
9006 when Attribute_Overlaps_Storage =>
9007 null;
9008
996ae0b0
RK
9009 -------------------------
9010 -- Passed_By_Reference --
9011 -------------------------
9012
9013 -- Scalar types are never passed by reference
9014
9015 when Attribute_Passed_By_Reference =>
edab6088 9016 Fold_Uint (N, False_Value, Static);
996ae0b0
RK
9017
9018 ---------
9019 -- Pos --
9020 ---------
9021
9022 when Attribute_Pos =>
edab6088 9023 Fold_Uint (N, Expr_Value (E1), Static);
996ae0b0
RK
9024
9025 ----------
9026 -- Pred --
9027 ----------
9028
9029 when Attribute_Pred => Pred :
9030 begin
fbf5a39b 9031 -- Floating-point case
996ae0b0 9032
fbf5a39b 9033 if Is_Floating_Point_Type (P_Type) then
af31bd57
AC
9034 Fold_Ureal
9035 (N, Eval_Fat.Pred (P_Base_Type, Expr_Value_R (E1)), Static);
996ae0b0 9036
fbf5a39b 9037 -- Fixed-point case
996ae0b0 9038
fbf5a39b 9039 elsif Is_Fixed_Point_Type (P_Type) then
af31bd57
AC
9040 Fold_Ureal
9041 (N, Expr_Value_R (E1) - Small_Value (P_Type), True);
996ae0b0 9042
fbf5a39b 9043 -- Modular integer case (wraps)
996ae0b0 9044
fbf5a39b
AC
9045 elsif Is_Modular_Integer_Type (P_Type) then
9046 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
996ae0b0 9047
fbf5a39b 9048 -- Other scalar cases
996ae0b0 9049
fbf5a39b
AC
9050 else
9051 pragma Assert (Is_Scalar_Type (P_Type));
996ae0b0 9052
fbf5a39b
AC
9053 if Is_Enumeration_Type (P_Type)
9054 and then Expr_Value (E1) =
9055 Expr_Value (Type_Low_Bound (P_Base_Type))
9056 then
9057 Apply_Compile_Time_Constraint_Error
9058 (N, "Pred of `&''First`",
9059 CE_Overflow_Check_Failed,
9060 Ent => P_Base_Type,
9061 Warn => not Static);
996ae0b0 9062
fbf5a39b
AC
9063 Check_Expressions;
9064 return;
996ae0b0 9065 end if;
fbf5a39b
AC
9066
9067 Fold_Uint (N, Expr_Value (E1) - 1, Static);
996ae0b0
RK
9068 end if;
9069 end Pred;
9070
9071 -----------
9072 -- Range --
9073 -----------
9074
9075 -- No processing required, because by this stage, Range has been
9076 -- replaced by First .. Last, so this branch can never be taken.
9077
9078 when Attribute_Range =>
9079 raise Program_Error;
9080
9081 ------------------
9082 -- Range_Length --
9083 ------------------
9084
9085 when Attribute_Range_Length =>
9086 Set_Bounds;
9087
af02a866
RD
9088 -- Can fold if both bounds are compile time known
9089
996ae0b0
RK
9090 if Compile_Time_Known_Value (Hi_Bound)
9091 and then Compile_Time_Known_Value (Lo_Bound)
9092 then
9093 Fold_Uint (N,
9094 UI_Max
fbf5a39b
AC
9095 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
9096 Static);
996ae0b0
RK
9097 end if;
9098
af02a866
RD
9099 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9100 -- comparable, and we can figure out the difference between them.
9101
9102 declare
9103 Diff : aliased Uint;
9104
9105 begin
9106 case
9107 Compile_Time_Compare
9108 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
9109 is
9110 when EQ =>
edab6088 9111 Fold_Uint (N, Uint_1, Static);
af02a866
RD
9112
9113 when GT =>
edab6088 9114 Fold_Uint (N, Uint_0, Static);
af02a866
RD
9115
9116 when LT =>
9117 if Diff /= No_Uint then
edab6088 9118 Fold_Uint (N, Diff + 1, Static);
af02a866
RD
9119 end if;
9120
9121 when others =>
9122 null;
9123 end case;
9124 end;
9125
46202729
AC
9126 ---------
9127 -- Ref --
9128 ---------
9129
9130 when Attribute_Ref =>
edab6088 9131 Fold_Uint (N, Expr_Value (E1), Static);
46202729 9132
996ae0b0
RK
9133 ---------------
9134 -- Remainder --
9135 ---------------
9136
82c80734
RD
9137 when Attribute_Remainder => Remainder : declare
9138 X : constant Ureal := Expr_Value_R (E1);
9139 Y : constant Ureal := Expr_Value_R (E2);
9140
9141 begin
9142 if UR_Is_Zero (Y) then
9143 Apply_Compile_Time_Constraint_Error
9144 (N, "division by zero in Remainder",
9145 CE_Overflow_Check_Failed,
9146 Warn => not Static);
9147
9148 Check_Expressions;
9149 return;
9150 end if;
9151
7b55fea6 9152 Fold_Ureal (N, Eval_Fat.Remainder (P_Base_Type, X, Y), Static);
82c80734 9153 end Remainder;
996ae0b0 9154
2cbac6c6
AC
9155 -----------------
9156 -- Restriction --
9157 -----------------
9158
9159 when Attribute_Restriction_Set => Restriction_Set : declare
9160 begin
9161 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
9162 Set_Is_Static_Expression (N);
9163 end Restriction_Set;
9164
996ae0b0
RK
9165 -----------
9166 -- Round --
9167 -----------
9168
9169 when Attribute_Round => Round :
9170 declare
9171 Sr : Ureal;
9172 Si : Uint;
9173
9174 begin
fbf5a39b 9175 -- First we get the (exact result) in units of small
996ae0b0 9176
fbf5a39b 9177 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
996ae0b0 9178
fbf5a39b 9179 -- Now round that exactly to an integer
996ae0b0 9180
fbf5a39b 9181 Si := UR_To_Uint (Sr);
996ae0b0 9182
fbf5a39b 9183 -- Finally the result is obtained by converting back to real
996ae0b0 9184
fbf5a39b 9185 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
996ae0b0
RK
9186 end Round;
9187
9188 --------------
9189 -- Rounding --
9190 --------------
9191
9192 when Attribute_Rounding =>
af31bd57
AC
9193 Fold_Ureal
9194 (N, Eval_Fat.Rounding (P_Base_Type, Expr_Value_R (E1)), Static);
996ae0b0
RK
9195
9196 ---------------
9197 -- Safe_Emax --
9198 ---------------
9199
9200 when Attribute_Safe_Emax =>
d32e3cee 9201 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
996ae0b0
RK
9202
9203 ----------------
9204 -- Safe_First --
9205 ----------------
9206
9207 when Attribute_Safe_First =>
d32e3cee 9208 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
996ae0b0
RK
9209
9210 ----------------
9211 -- Safe_Large --
9212 ----------------
9213
9214 when Attribute_Safe_Large =>
9215 if Is_Fixed_Point_Type (P_Type) then
fbf5a39b
AC
9216 Fold_Ureal
9217 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
996ae0b0 9218 else
d32e3cee 9219 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
996ae0b0
RK
9220 end if;
9221
9222 ---------------
9223 -- Safe_Last --
9224 ---------------
9225
9226 when Attribute_Safe_Last =>
d32e3cee 9227 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
996ae0b0
RK
9228
9229 ----------------
9230 -- Safe_Small --
9231 ----------------
9232
9233 when Attribute_Safe_Small =>
9234
9235 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9236 -- for fixed-point, since is the same as Small, but we implement
9237 -- it for backwards compatibility.
9238
9239 if Is_Fixed_Point_Type (P_Type) then
fbf5a39b 9240 Fold_Ureal (N, Small_Value (P_Type), Static);
996ae0b0
RK
9241
9242 -- Ada 83 Safe_Small for floating-point cases
9243
9244 else
d32e3cee 9245 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
996ae0b0
RK
9246 end if;
9247
9248 -----------
9249 -- Scale --
9250 -----------
9251
9252 when Attribute_Scale =>
edab6088 9253 Fold_Uint (N, Scale_Value (P_Type), Static);
996ae0b0
RK
9254
9255 -------------
9256 -- Scaling --
9257 -------------
9258
9259 when Attribute_Scaling =>
af31bd57
AC
9260 Fold_Ureal
9261 (N,
9262 Eval_Fat.Scaling
9263 (P_Base_Type, Expr_Value_R (E1), Expr_Value (E2)),
9264 Static);
996ae0b0
RK
9265
9266 ------------------
9267 -- Signed_Zeros --
9268 ------------------
9269
9270 when Attribute_Signed_Zeros =>
9271 Fold_Uint
b887f1a0 9272 (N, UI_From_Int (Boolean'Pos (Has_Signed_Zeros (P_Type))), Static);
996ae0b0
RK
9273
9274 ----------
9275 -- Size --
9276 ----------
9277
9278 -- Size attribute returns the RM size. All scalar types can be folded,
9279 -- as well as any types for which the size is known by the front end,
edab6088
RD
9280 -- including any type for which a size attribute is specified. This is
9281 -- one of the places where it is annoying that a size of zero means two
9282 -- things (zero size for scalars, unspecified size for non-scalars).
996ae0b0
RK
9283
9284 when Attribute_Size | Attribute_VADS_Size => Size : declare
9285 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
9286
9287 begin
edab6088 9288 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
996ae0b0
RK
9289
9290 -- VADS_Size case
9291
fbf5a39b 9292 if Id = Attribute_VADS_Size or else Use_VADS_Size then
996ae0b0
RK
9293 declare
9294 S : constant Node_Id := Size_Clause (P_TypeA);
9295
9296 begin
9297 -- If a size clause applies, then use the size from it.
9298 -- This is one of the rare cases where we can use the
9299 -- Size_Clause field for a subtype when Has_Size_Clause
9300 -- is False. Consider:
9301
82c80734 9302 -- type x is range 1 .. 64;
996ae0b0
RK
9303 -- for x'size use 12;
9304 -- subtype y is x range 0 .. 3;
9305
9306 -- Here y has a size clause inherited from x, but normally
9307 -- it does not apply, and y'size is 2. However, y'VADS_Size
9308 -- is indeed 12 and not 2.
9309
9310 if Present (S)
9311 and then Is_OK_Static_Expression (Expression (S))
9312 then
edab6088 9313 Fold_Uint (N, Expr_Value (Expression (S)), Static);
996ae0b0
RK
9314
9315 -- If no size is specified, then we simply use the object
9316 -- size in the VADS_Size case (e.g. Natural'Size is equal
9317 -- to Integer'Size, not one less).
9318
9319 else
edab6088 9320 Fold_Uint (N, Esize (P_TypeA), Static);
996ae0b0
RK
9321 end if;
9322 end;
9323
9324 -- Normal case (Size) in which case we want the RM_Size
9325
9326 else
edab6088 9327 Fold_Uint (N, RM_Size (P_TypeA), Static);
996ae0b0
RK
9328 end if;
9329 end if;
9330 end Size;
9331
9332 -----------
9333 -- Small --
9334 -----------
9335
9336 when Attribute_Small =>
9337
12a13f01 9338 -- The floating-point case is present only for Ada 83 compatibility.
996ae0b0
RK
9339 -- Note that strictly this is an illegal addition, since we are
9340 -- extending an Ada 95 defined attribute, but we anticipate an
9341 -- ARG ruling that will permit this.
9342
9343 if Is_Floating_Point_Type (P_Type) then
9344
9345 -- Ada 83 attribute is defined as (RM83 3.5.8)
9346
9347 -- T'Small = 2.0**(-T'Emax - 1)
9348
9349 -- where
9350
9351 -- T'Emax = 4 * T'Mantissa
9352
fbf5a39b 9353 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
996ae0b0
RK
9354
9355 -- Normal Ada 95 fixed-point case
9356
9357 else
fbf5a39b 9358 Fold_Ureal (N, Small_Value (P_Type), True);
996ae0b0
RK
9359 end if;
9360
82c80734
RD
9361 -----------------
9362 -- Stream_Size --
9363 -----------------
9364
9365 when Attribute_Stream_Size =>
9366 null;
9367
996ae0b0
RK
9368 ----------
9369 -- Succ --
9370 ----------
9371
9372 when Attribute_Succ => Succ :
9373 begin
fbf5a39b 9374 -- Floating-point case
996ae0b0 9375
fbf5a39b 9376 if Is_Floating_Point_Type (P_Type) then
af31bd57
AC
9377 Fold_Ureal
9378 (N, Eval_Fat.Succ (P_Base_Type, Expr_Value_R (E1)), Static);
996ae0b0 9379
fbf5a39b 9380 -- Fixed-point case
996ae0b0 9381
fbf5a39b 9382 elsif Is_Fixed_Point_Type (P_Type) then
af31bd57 9383 Fold_Ureal (N, Expr_Value_R (E1) + Small_Value (P_Type), Static);
996ae0b0 9384
fbf5a39b 9385 -- Modular integer case (wraps)
996ae0b0 9386
fbf5a39b
AC
9387 elsif Is_Modular_Integer_Type (P_Type) then
9388 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
996ae0b0 9389
fbf5a39b 9390 -- Other scalar cases
996ae0b0 9391
fbf5a39b
AC
9392 else
9393 pragma Assert (Is_Scalar_Type (P_Type));
996ae0b0 9394
fbf5a39b
AC
9395 if Is_Enumeration_Type (P_Type)
9396 and then Expr_Value (E1) =
9397 Expr_Value (Type_High_Bound (P_Base_Type))
9398 then
9399 Apply_Compile_Time_Constraint_Error
9400 (N, "Succ of `&''Last`",
9401 CE_Overflow_Check_Failed,
9402 Ent => P_Base_Type,
9403 Warn => not Static);
996ae0b0 9404
fbf5a39b
AC
9405 Check_Expressions;
9406 return;
9407 else
9408 Fold_Uint (N, Expr_Value (E1) + 1, Static);
996ae0b0
RK
9409 end if;
9410 end if;
9411 end Succ;
9412
9413 ----------------
9414 -- Truncation --
9415 ----------------
9416
9417 when Attribute_Truncation =>
af31bd57
AC
9418 Fold_Ureal
9419 (N,
9420 Eval_Fat.Truncation (P_Base_Type, Expr_Value_R (E1)),
9421 Static);
996ae0b0
RK
9422
9423 ----------------
9424 -- Type_Class --
9425 ----------------
9426
9427 when Attribute_Type_Class => Type_Class : declare
9428 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
9429 Id : RE_Id;
9430
9431 begin
d9d25d04 9432 if Is_Descendant_Of_Address (Typ) then
996ae0b0
RK
9433 Id := RE_Type_Class_Address;
9434
9435 elsif Is_Enumeration_Type (Typ) then
9436 Id := RE_Type_Class_Enumeration;
9437
9438 elsif Is_Integer_Type (Typ) then
9439 Id := RE_Type_Class_Integer;
9440
9441 elsif Is_Fixed_Point_Type (Typ) then
9442 Id := RE_Type_Class_Fixed_Point;
9443
9444 elsif Is_Floating_Point_Type (Typ) then
9445 Id := RE_Type_Class_Floating_Point;
9446
9447 elsif Is_Array_Type (Typ) then
9448 Id := RE_Type_Class_Array;
9449
9450 elsif Is_Record_Type (Typ) then
9451 Id := RE_Type_Class_Record;
9452
9453 elsif Is_Access_Type (Typ) then
9454 Id := RE_Type_Class_Access;
9455
9456 elsif Is_Enumeration_Type (Typ) then
9457 Id := RE_Type_Class_Enumeration;
9458
9459 elsif Is_Task_Type (Typ) then
9460 Id := RE_Type_Class_Task;
9461
9462 -- We treat protected types like task types. It would make more
9463 -- sense to have another enumeration value, but after all the
9464 -- whole point of this feature is to be exactly DEC compatible,
12a13f01 9465 -- and changing the type Type_Class would not meet this requirement.
996ae0b0
RK
9466
9467 elsif Is_Protected_Type (Typ) then
9468 Id := RE_Type_Class_Task;
9469
9470 -- Not clear if there are any other possibilities, but if there
9471 -- are, then we will treat them as the address case.
9472
9473 else
9474 Id := RE_Type_Class_Address;
9475 end if;
9476
9477 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
996ae0b0
RK
9478 end Type_Class;
9479
9480 -----------------------
9481 -- Unbiased_Rounding --
9482 -----------------------
9483
9484 when Attribute_Unbiased_Rounding =>
af31bd57
AC
9485 Fold_Ureal
9486 (N,
9487 Eval_Fat.Unbiased_Rounding (P_Base_Type, Expr_Value_R (E1)),
9488 Static);
fbf5a39b
AC
9489
9490 -------------------------
9491 -- Unconstrained_Array --
9492 -------------------------
9493
9494 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
9495 Typ : constant Entity_Id := Underlying_Type (P_Type);
9496
9497 begin
aa720a54
AC
9498 Rewrite (N, New_Occurrence_Of (
9499 Boolean_Literals (
9500 Is_Array_Type (P_Type)
9501 and then not Is_Constrained (Typ)), Loc));
996ae0b0 9502
fbf5a39b
AC
9503 -- Analyze and resolve as boolean, note that this attribute is
9504 -- a static attribute in GNAT.
9505
9506 Analyze_And_Resolve (N, Standard_Boolean);
9507 Static := True;
edab6088 9508 Set_Is_Static_Expression (N, True);
fbf5a39b
AC
9509 end Unconstrained_Array;
9510
18a2ad5d
AC
9511 -- Attribute Update is never static
9512
18a2ad5d 9513 when Attribute_Update =>
08cd7c2f 9514 return;
18a2ad5d 9515
996ae0b0
RK
9516 ---------------
9517 -- VADS_Size --
9518 ---------------
9519
9520 -- Processing is shared with Size
9521
9522 ---------
9523 -- Val --
9524 ---------
9525
9526 when Attribute_Val => Val :
9527 begin
fbf5a39b
AC
9528 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
9529 or else
9530 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
9531 then
9532 Apply_Compile_Time_Constraint_Error
9533 (N, "Val expression out of range",
9534 CE_Range_Check_Failed,
9535 Warn => not Static);
9536
9537 Check_Expressions;
9538 return;
9539
9540 else
9541 Fold_Uint (N, Expr_Value (E1), Static);
996ae0b0
RK
9542 end if;
9543 end Val;
9544
9545 ----------------
9546 -- Value_Size --
9547 ----------------
9548
edab6088
RD
9549 -- The Value_Size attribute for a type returns the RM size of the type.
9550 -- This an always be folded for scalar types, and can also be folded for
9551 -- non-scalar types if the size is set. This is one of the places where
9552 -- it is annoying that a size of zero means two things!
996ae0b0
RK
9553
9554 when Attribute_Value_Size => Value_Size : declare
9555 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
996ae0b0 9556 begin
edab6088
RD
9557 if Is_Scalar_Type (P_TypeA) or else RM_Size (P_TypeA) /= Uint_0 then
9558 Fold_Uint (N, RM_Size (P_TypeA), Static);
996ae0b0 9559 end if;
996ae0b0
RK
9560 end Value_Size;
9561
9562 -------------
9563 -- Version --
9564 -------------
9565
9566 -- Version can never be static
9567
9568 when Attribute_Version =>
9569 null;
9570
9571 ----------------
9572 -- Wide_Image --
9573 ----------------
9574
9575 -- Wide_Image is a scalar attribute, but is never static, because it
9576 -- is not a static function (having a non-scalar argument (RM 4.9(22))
9577
9578 when Attribute_Wide_Image =>
9579 null;
9580
82c80734
RD
9581 ---------------------
9582 -- Wide_Wide_Image --
9583 ---------------------
9584
9585 -- Wide_Wide_Image is a scalar attribute but is never static, because it
9586 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
9587
9588 when Attribute_Wide_Wide_Image =>
9589 null;
9590
9591 ---------------------
9592 -- Wide_Wide_Width --
9593 ---------------------
9594
9595 -- Processing for Wide_Wide_Width is combined with Width
9596
996ae0b0
RK
9597 ----------------
9598 -- Wide_Width --
9599 ----------------
9600
9601 -- Processing for Wide_Width is combined with Width
9602
9603 -----------
9604 -- Width --
9605 -----------
9606
82c80734 9607 -- This processing also handles the case of Wide_[Wide_]Width
996ae0b0 9608
82c80734
RD
9609 when Attribute_Width |
9610 Attribute_Wide_Width |
9611 Attribute_Wide_Wide_Width => Width :
996ae0b0 9612 begin
fbf5a39b 9613 if Compile_Time_Known_Bounds (P_Type) then
996ae0b0
RK
9614
9615 -- Floating-point types
9616
9617 if Is_Floating_Point_Type (P_Type) then
9618
9619 -- Width is zero for a null range (RM 3.5 (38))
9620
9621 if Expr_Value_R (Type_High_Bound (P_Type)) <
9622 Expr_Value_R (Type_Low_Bound (P_Type))
9623 then
edab6088 9624 Fold_Uint (N, Uint_0, Static);
996ae0b0
RK
9625
9626 else
9627 -- For floating-point, we have +N.dddE+nnn where length
9628 -- of ddd is determined by type'Digits - 1, but is one
9629 -- if Digits is one (RM 3.5 (33)).
9630
9631 -- nnn is set to 2 for Short_Float and Float (32 bit
9632 -- floats), and 3 for Long_Float and Long_Long_Float.
b8dc622e
JM
9633 -- For machines where Long_Long_Float is the IEEE
9634 -- extended precision type, the exponent takes 4 digits.
996ae0b0
RK
9635
9636 declare
9637 Len : Int :=
9638 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
9639
9640 begin
9641 if Esize (P_Type) <= 32 then
9642 Len := Len + 6;
b8dc622e 9643 elsif Esize (P_Type) = 64 then
996ae0b0 9644 Len := Len + 7;
b8dc622e
JM
9645 else
9646 Len := Len + 8;
996ae0b0
RK
9647 end if;
9648
edab6088 9649 Fold_Uint (N, UI_From_Int (Len), Static);
996ae0b0
RK
9650 end;
9651 end if;
9652
9653 -- Fixed-point types
9654
9655 elsif Is_Fixed_Point_Type (P_Type) then
9656
9657 -- Width is zero for a null range (RM 3.5 (38))
9658
9659 if Expr_Value (Type_High_Bound (P_Type)) <
9660 Expr_Value (Type_Low_Bound (P_Type))
9661 then
edab6088 9662 Fold_Uint (N, Uint_0, Static);
996ae0b0
RK
9663
9664 -- The non-null case depends on the specific real type
9665
9666 else
9667 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
9668
fbf5a39b 9669 Fold_Uint
5087048c 9670 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
edab6088 9671 Static);
996ae0b0
RK
9672 end if;
9673
9674 -- Discrete types
9675
9676 else
9677 declare
9678 R : constant Entity_Id := Root_Type (P_Type);
21d27997
RD
9679 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
9680 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
996ae0b0
RK
9681 W : Nat;
9682 Wt : Nat;
9683 T : Uint;
9684 L : Node_Id;
9685 C : Character;
9686
9687 begin
9688 -- Empty ranges
9689
9690 if Lo > Hi then
9691 W := 0;
9692
9693 -- Width for types derived from Standard.Character
82c80734 9694 -- and Standard.Wide_[Wide_]Character.
996ae0b0 9695
21d27997 9696 elsif Is_Standard_Character_Type (P_Type) then
996ae0b0
RK
9697 W := 0;
9698
9699 -- Set W larger if needed
9700
9701 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
9702
edd63e9b 9703 -- All wide characters look like Hex_hhhhhhhh
82c80734 9704
996ae0b0 9705 if J > 255 then
39486058 9706
a90bd866 9707 -- No need to compute this more than once
39486058 9708
39486058 9709 exit;
996ae0b0
RK
9710
9711 else
9712 C := Character'Val (J);
9713
9714 -- Test for all cases where Character'Image
9715 -- yields an image that is longer than three
9716 -- characters. First the cases of Reserved_xxx
9717 -- names (length = 12).
9718
9719 case C is
9720 when Reserved_128 | Reserved_129 |
9721 Reserved_132 | Reserved_153
996ae0b0
RK
9722 => Wt := 12;
9723
9724 when BS | HT | LF | VT | FF | CR |
9725 SO | SI | EM | FS | GS | RS |
9726 US | RI | MW | ST | PM
996ae0b0
RK
9727 => Wt := 2;
9728
9729 when NUL | SOH | STX | ETX | EOT |
9730 ENQ | ACK | BEL | DLE | DC1 |
9731 DC2 | DC3 | DC4 | NAK | SYN |
9732 ETB | CAN | SUB | ESC | DEL |
9733 BPH | NBH | NEL | SSA | ESA |
9734 HTS | HTJ | VTS | PLD | PLU |
9735 SS2 | SS3 | DCS | PU1 | PU2 |
9736 STS | CCH | SPA | EPA | SOS |
9737 SCI | CSI | OSC | APC
996ae0b0
RK
9738 => Wt := 3;
9739
9740 when Space .. Tilde |
9741 No_Break_Space .. LC_Y_Diaeresis
3786bbd1
RD
9742 =>
9743 -- Special case of soft hyphen in Ada 2005
9744
9745 if C = Character'Val (16#AD#)
0791fbe9 9746 and then Ada_Version >= Ada_2005
3786bbd1
RD
9747 then
9748 Wt := 11;
9749 else
9750 Wt := 3;
9751 end if;
996ae0b0
RK
9752 end case;
9753
9754 W := Int'Max (W, Wt);
9755 end if;
9756 end loop;
9757
9758 -- Width for types derived from Standard.Boolean
9759
9760 elsif R = Standard_Boolean then
9761 if Lo = 0 then
9762 W := 5; -- FALSE
9763 else
9764 W := 4; -- TRUE
9765 end if;
9766
9767 -- Width for integer types
9768
9769 elsif Is_Integer_Type (P_Type) then
9770 T := UI_Max (abs Lo, abs Hi);
9771
9772 W := 2;
9773 while T >= 10 loop
9774 W := W + 1;
9775 T := T / 10;
9776 end loop;
9777
8a06151a
RD
9778 -- User declared enum type with discard names
9779
9780 elsif Discard_Names (R) then
9781
9782 -- If range is null, result is zero, that has already
9783 -- been dealt with, so what we need is the power of ten
9784 -- that accomodates the Pos of the largest value, which
9785 -- is the high bound of the range + one for the space.
9786
9787 W := 1;
9788 T := Hi;
9789 while T /= 0 loop
9790 T := T / 10;
9791 W := W + 1;
9792 end loop;
9793
996ae0b0 9794 -- Only remaining possibility is user declared enum type
8a06151a 9795 -- with normal case of Discard_Names not active.
996ae0b0
RK
9796
9797 else
9798 pragma Assert (Is_Enumeration_Type (P_Type));
9799
9800 W := 0;
9801 L := First_Literal (P_Type);
996ae0b0
RK
9802 while Present (L) loop
9803
9804 -- Only pay attention to in range characters
9805
9806 if Lo <= Enumeration_Pos (L)
9807 and then Enumeration_Pos (L) <= Hi
9808 then
9809 -- For Width case, use decoded name
9810
9811 if Id = Attribute_Width then
9812 Get_Decoded_Name_String (Chars (L));
9813 Wt := Nat (Name_Len);
9814
82c80734
RD
9815 -- For Wide_[Wide_]Width, use encoded name, and
9816 -- then adjust for the encoding.
996ae0b0
RK
9817
9818 else
9819 Get_Name_String (Chars (L));
9820
9821 -- Character literals are always of length 3
9822
9823 if Name_Buffer (1) = 'Q' then
9824 Wt := 3;
9825
9826 -- Otherwise loop to adjust for upper/wide chars
9827
9828 else
9829 Wt := Nat (Name_Len);
9830
9831 for J in 1 .. Name_Len loop
9832 if Name_Buffer (J) = 'U' then
9833 Wt := Wt - 2;
9834 elsif Name_Buffer (J) = 'W' then
9835 Wt := Wt - 4;
9836 end if;
9837 end loop;
9838 end if;
9839 end if;
9840
9841 W := Int'Max (W, Wt);
9842 end if;
9843
9844 Next_Literal (L);
9845 end loop;
9846 end if;
9847
edab6088 9848 Fold_Uint (N, UI_From_Int (W), Static);
996ae0b0
RK
9849 end;
9850 end if;
9851 end if;
9852 end Width;
9853
4adf3c50 9854 -- The following attributes denote functions that cannot be folded
54838d1f
AC
9855
9856 when Attribute_From_Any |
9857 Attribute_To_Any |
9858 Attribute_TypeCode =>
9859 null;
9860
996ae0b0
RK
9861 -- The following attributes can never be folded, and furthermore we
9862 -- should not even have entered the case statement for any of these.
9863 -- Note that in some cases, the values have already been folded as
21db8699
RD
9864 -- a result of the processing in Analyze_Attribute or earlier in
9865 -- this procedure.
996ae0b0 9866
7ed57189
AC
9867 when Attribute_Abort_Signal |
9868 Attribute_Access |
9869 Attribute_Address |
9870 Attribute_Address_Size |
9871 Attribute_Asm_Input |
9872 Attribute_Asm_Output |
9873 Attribute_Base |
9874 Attribute_Bit_Order |
9875 Attribute_Bit_Position |
9876 Attribute_Callable |
9877 Attribute_Caller |
9878 Attribute_Class |
9879 Attribute_Code_Address |
9880 Attribute_Compiler_Version |
9881 Attribute_Count |
9882 Attribute_Default_Bit_Order |
9883 Attribute_Default_Scalar_Storage_Order |
eaed2a2c 9884 Attribute_Deref |
7ed57189
AC
9885 Attribute_Elaborated |
9886 Attribute_Elab_Body |
9887 Attribute_Elab_Spec |
9888 Attribute_Elab_Subp_Body |
9889 Attribute_Enabled |
9890 Attribute_External_Tag |
9891 Attribute_Fast_Math |
9892 Attribute_First_Bit |
21db8699 9893 Attribute_Img |
7ed57189
AC
9894 Attribute_Input |
9895 Attribute_Last_Bit |
9896 Attribute_Library_Level |
9897 Attribute_Maximum_Alignment |
9898 Attribute_Old |
9899 Attribute_Output |
9900 Attribute_Partition_ID |
9901 Attribute_Pool_Address |
9902 Attribute_Position |
9903 Attribute_Priority |
9904 Attribute_Read |
9905 Attribute_Result |
9906 Attribute_Scalar_Storage_Order |
9907 Attribute_Simple_Storage_Pool |
9908 Attribute_Storage_Pool |
9909 Attribute_Storage_Size |
9910 Attribute_Storage_Unit |
9911 Attribute_Stub_Type |
9912 Attribute_System_Allocator_Alignment |
9913 Attribute_Tag |
9914 Attribute_Target_Name |
9915 Attribute_Terminated |
9916 Attribute_To_Address |
9917 Attribute_Type_Key |
7ed57189
AC
9918 Attribute_Unchecked_Access |
9919 Attribute_Universal_Literal_String |
9920 Attribute_Unrestricted_Access |
9921 Attribute_Valid |
9922 Attribute_Valid_Scalars |
9923 Attribute_Value |
9924 Attribute_Wchar_T_Size |
9925 Attribute_Wide_Value |
9926 Attribute_Wide_Wide_Value |
9927 Attribute_Word_Size |
9928 Attribute_Write =>
996ae0b0
RK
9929
9930 raise Program_Error;
996ae0b0
RK
9931 end case;
9932
9933 -- At the end of the case, one more check. If we did a static evaluation
9934 -- so that the result is now a literal, then set Is_Static_Expression
9935 -- in the constant only if the prefix type is a static subtype. For
9936 -- non-static subtypes, the folding is still OK, but not static.
9937
fbf5a39b
AC
9938 -- An exception is the GNAT attribute Constrained_Array which is
9939 -- defined to be a static attribute in all cases.
9940
e10dab7f
JM
9941 if Nkind_In (N, N_Integer_Literal,
9942 N_Real_Literal,
9943 N_Character_Literal,
9944 N_String_Literal)
996ae0b0
RK
9945 or else (Is_Entity_Name (N)
9946 and then Ekind (Entity (N)) = E_Enumeration_Literal)
9947 then
9948 Set_Is_Static_Expression (N, Static);
9949
9950 -- If this is still an attribute reference, then it has not been folded
9951 -- and that means that its expressions are in a non-static context.
9952
9953 elsif Nkind (N) = N_Attribute_Reference then
9954 Check_Expressions;
9955
9956 -- Note: the else case not covered here are odd cases where the
9957 -- processing has transformed the attribute into something other
9958 -- than a constant. Nothing more to do in such cases.
9959
9960 else
9961 null;
9962 end if;
996ae0b0
RK
9963 end Eval_Attribute;
9964
9965 ------------------------------
9966 -- Is_Anonymous_Tagged_Base --
9967 ------------------------------
9968
9969 function Is_Anonymous_Tagged_Base
9970 (Anon : Entity_Id;
155562cb 9971 Typ : Entity_Id) return Boolean
996ae0b0
RK
9972 is
9973 begin
9974 return
9975 Anon = Current_Scope
9976 and then Is_Itype (Anon)
9977 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
9978 end Is_Anonymous_Tagged_Base;
9979
442ade9d
RD
9980 --------------------------------
9981 -- Name_Implies_Lvalue_Prefix --
9982 --------------------------------
822033eb 9983
442ade9d 9984 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
822033eb
HK
9985 pragma Assert (Is_Attribute_Name (Nam));
9986 begin
442ade9d
RD
9987 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
9988 end Name_Implies_Lvalue_Prefix;
822033eb 9989
996ae0b0
RK
9990 -----------------------
9991 -- Resolve_Attribute --
9992 -----------------------
9993
9994 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
9995 Loc : constant Source_Ptr := Sloc (N);
9996 P : constant Node_Id := Prefix (N);
9997 Aname : constant Name_Id := Attribute_Name (N);
9998 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
fbf5a39b 9999 Btyp : constant Entity_Id := Base_Type (Typ);
468c6c8a 10000 Des_Btyp : Entity_Id;
996ae0b0
RK
10001 Index : Interp_Index;
10002 It : Interp;
996ae0b0
RK
10003 Nom_Subt : Entity_Id;
10004
f529bac5
ES
10005 procedure Accessibility_Message;
10006 -- Error, or warning within an instance, if the static accessibility
10007 -- rules of 3.10.2 are violated.
10008
46413d9e
AC
10009 function Declared_Within_Generic_Unit
10010 (Entity : Entity_Id;
10011 Generic_Unit : Node_Id) return Boolean;
10012 -- Returns True if Declared_Entity is declared within the declarative
10013 -- region of Generic_Unit; otherwise returns False.
10014
f529bac5
ES
10015 ---------------------------
10016 -- Accessibility_Message --
10017 ---------------------------
10018
10019 procedure Accessibility_Message is
10020 Indic : Node_Id := Parent (Parent (N));
10021
10022 begin
10023 -- In an instance, this is a runtime check, but one we
10024 -- know will fail, so generate an appropriate warning.
10025
10026 if In_Instance_Body then
43417b90 10027 Error_Msg_Warn := SPARK_Mode /= On;
822033eb 10028 Error_Msg_F
4a28b181
AC
10029 ("non-local pointer cannot point to local object<<", P);
10030 Error_Msg_F ("\Program_Error [<<", P);
f529bac5
ES
10031 Rewrite (N,
10032 Make_Raise_Program_Error (Loc,
10033 Reason => PE_Accessibility_Check_Failed));
10034 Set_Etype (N, Typ);
10035 return;
10036
10037 else
ed2233dc 10038 Error_Msg_F ("non-local pointer cannot point to local object", P);
f529bac5
ES
10039
10040 -- Check for case where we have a missing access definition
10041
10042 if Is_Record_Type (Current_Scope)
10043 and then
e10dab7f
JM
10044 Nkind_In (Parent (N), N_Discriminant_Association,
10045 N_Index_Or_Discriminant_Constraint)
f529bac5
ES
10046 then
10047 Indic := Parent (Parent (N));
10048 while Present (Indic)
10049 and then Nkind (Indic) /= N_Subtype_Indication
10050 loop
10051 Indic := Parent (Indic);
10052 end loop;
10053
10054 if Present (Indic) then
10055 Error_Msg_NE
10056 ("\use an access definition for" &
822033eb
HK
10057 " the access discriminant of&",
10058 N, Entity (Subtype_Mark (Indic)));
f529bac5
ES
10059 end if;
10060 end if;
10061 end if;
10062 end Accessibility_Message;
10063
46413d9e
AC
10064 ----------------------------------
10065 -- Declared_Within_Generic_Unit --
10066 ----------------------------------
10067
10068 function Declared_Within_Generic_Unit
10069 (Entity : Entity_Id;
10070 Generic_Unit : Node_Id) return Boolean
10071 is
10072 Generic_Encloser : Node_Id := Enclosing_Generic_Unit (Entity);
10073
10074 begin
10075 while Present (Generic_Encloser) loop
10076 if Generic_Encloser = Generic_Unit then
10077 return True;
10078 end if;
10079
10080 -- We have to step to the scope of the generic's entity, because
10081 -- otherwise we'll just get back the same generic.
10082
10083 Generic_Encloser :=
10084 Enclosing_Generic_Unit
10085 (Scope (Defining_Entity (Generic_Encloser)));
10086 end loop;
10087
10088 return False;
10089 end Declared_Within_Generic_Unit;
10090
f529bac5
ES
10091 -- Start of processing for Resolve_Attribute
10092
996ae0b0 10093 begin
3b42c566
RD
10094 -- If error during analysis, no point in continuing, except for array
10095 -- types, where we get better recovery by using unconstrained indexes
10096 -- than nothing at all (see Check_Array_Type).
996ae0b0
RK
10097
10098 if Error_Posted (N)
10099 and then Attr_Id /= Attribute_First
10100 and then Attr_Id /= Attribute_Last
10101 and then Attr_Id /= Attribute_Length
10102 and then Attr_Id /= Attribute_Range
10103 then
10104 return;
10105 end if;
10106
10107 -- If attribute was universal type, reset to actual type
10108
10109 if Etype (N) = Universal_Integer
10110 or else Etype (N) = Universal_Real
10111 then
10112 Set_Etype (N, Typ);
10113 end if;
10114
10115 -- Remaining processing depends on attribute
10116
10117 case Attr_Id is
10118
10119 ------------
10120 -- Access --
10121 ------------
10122
10123 -- For access attributes, if the prefix denotes an entity, it is
10124 -- interpreted as a name, never as a call. It may be overloaded,
10125 -- in which case resolution uses the profile of the context type.
10126 -- Otherwise prefix must be resolved.
10127
10128 when Attribute_Access
10129 | Attribute_Unchecked_Access
10130 | Attribute_Unrestricted_Access =>
10131
e7ba564f 10132 Access_Attribute :
e10dab7f 10133 begin
91669e7e
AC
10134 -- Note possible modification if we have a variable
10135
996ae0b0 10136 if Is_Variable (P) then
91669e7e
AC
10137 declare
10138 PN : constant Node_Id := Parent (N);
10139 Nm : Node_Id;
10140
10141 Note : Boolean := True;
10142 -- Skip this for the case of Unrestricted_Access occuring in
10143 -- the context of a Valid check, since this otherwise leads
10144 -- to a missed warning (the Valid check does not really
10145 -- modify!) If this case, Note will be reset to False.
10146
81501d2b
AC
10147 -- Skip it as well if the type is an Acccess_To_Constant,
10148 -- given that no use of the value can modify the prefix.
10149
91669e7e
AC
10150 begin
10151 if Attr_Id = Attribute_Unrestricted_Access
10152 and then Nkind (PN) = N_Function_Call
10153 then
10154 Nm := Name (PN);
10155
10156 if Nkind (Nm) = N_Expanded_Name
10157 and then Chars (Nm) = Name_Valid
10158 and then Nkind (Prefix (Nm)) = N_Identifier
10159 and then Chars (Prefix (Nm)) = Name_Attr_Long_Float
10160 then
10161 Note := False;
10162 end if;
81501d2b
AC
10163
10164 elsif Is_Access_Constant (Typ) then
10165 Note := False;
91669e7e
AC
10166 end if;
10167
10168 if Note then
10169 Note_Possible_Modification (P, Sure => False);
10170 end if;
10171 end;
996ae0b0
RK
10172 end if;
10173
b07b7ace
AC
10174 -- The following comes from a query concerning improper use of
10175 -- universal_access in equality tests involving anonymous access
10176 -- types. Another good reason for 'Ref, but for now disable the
10177 -- test, which breaks several filed tests???
8d9509fd
ES
10178
10179 if Ekind (Typ) = E_Anonymous_Access_Type
10180 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
10181 and then False
10182 then
10183 Error_Msg_N ("need unique type to resolve 'Access", N);
10184 Error_Msg_N ("\qualify attribute with some access type", N);
10185 end if;
10186
b07b7ace
AC
10187 -- Case where prefix is an entity name
10188
996ae0b0 10189 if Is_Entity_Name (P) then
b07b7ace
AC
10190
10191 -- Deal with case where prefix itself is overloaded
10192
996ae0b0
RK
10193 if Is_Overloaded (P) then
10194 Get_First_Interp (P, Index, It);
996ae0b0 10195 while Present (It.Nam) loop
996ae0b0
RK
10196 if Type_Conformant (Designated_Type (Typ), It.Nam) then
10197 Set_Entity (P, It.Nam);
10198
fea9e956
ES
10199 -- The prefix is definitely NOT overloaded anymore at
10200 -- this point, so we reset the Is_Overloaded flag to
10201 -- avoid any confusion when reanalyzing the node.
996ae0b0
RK
10202
10203 Set_Is_Overloaded (P, False);
fea9e956 10204 Set_Is_Overloaded (N, False);
996ae0b0
RK
10205 Generate_Reference (Entity (P), P);
10206 exit;
10207 end if;
10208
10209 Get_Next_Interp (Index, It);
10210 end loop;
10211
16e764a7 10212 -- If Prefix is a subprogram name, this reference freezes,
6dc87f5f
AC
10213 -- but not if within spec expression mode. The profile of
10214 -- the subprogram is not frozen at this point.
16e764a7
AC
10215
10216 if not In_Spec_Expression then
b1d8d229 10217 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
16e764a7 10218 end if;
442ade9d 10219
6dc87f5f
AC
10220 -- If it is a type, there is nothing to resolve.
10221 -- If it is a subprogram, do not freeze its profile.
10222 -- If it is an object, complete its resolution.
996ae0b0 10223
fea9e956 10224 elsif Is_Overloadable (Entity (P)) then
21d27997 10225 if not In_Spec_Expression then
b1d8d229 10226 Freeze_Before (N, Entity (P), Do_Freeze_Profile => False);
fea9e956
ES
10227 end if;
10228
b07b7ace
AC
10229 -- Nothing to do if prefix is a type name
10230
fea9e956
ES
10231 elsif Is_Type (Entity (P)) then
10232 null;
b07b7ace
AC
10233
10234 -- Otherwise non-overloaded other case, resolve the prefix
10235
fea9e956 10236 else
fbf5a39b 10237 Resolve (P);
996ae0b0
RK
10238 end if;
10239
b07b7ace
AC
10240 -- Some further error checks
10241
12e0c41c
AC
10242 Error_Msg_Name_1 := Aname;
10243
996ae0b0
RK
10244 if not Is_Entity_Name (P) then
10245 null;
10246
fea9e956
ES
10247 elsif Is_Overloadable (Entity (P))
10248 and then Is_Abstract_Subprogram (Entity (P))
996ae0b0 10249 then
822033eb 10250 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
996ae0b0
RK
10251 Set_Etype (N, Any_Type);
10252
53f697ee
AC
10253 elsif Ekind (Entity (P)) = E_Enumeration_Literal then
10254 Error_Msg_F
d7cb47b4 10255 ("prefix of % attribute cannot be enumeration literal", P);
53f697ee 10256 Set_Etype (N, Any_Type);
996ae0b0 10257
53f697ee
AC
10258 -- An attempt to take 'Access of a function that renames an
10259 -- enumeration literal. Issue a specialized error message.
10260
10261 elsif Ekind (Entity (P)) = E_Function
10262 and then Present (Alias (Entity (P)))
10263 and then Ekind (Alias (Entity (P))) = E_Enumeration_Literal
10264 then
10265 Error_Msg_F
d7cb47b4
AC
10266 ("prefix of % attribute cannot be function renaming "
10267 & "an enumeration literal", P);
53f697ee
AC
10268 Set_Etype (N, Any_Type);
10269
10270 elsif Convention (Entity (P)) = Convention_Intrinsic then
d7cb47b4 10271 Error_Msg_F ("prefix of % attribute cannot be intrinsic", P);
996ae0b0
RK
10272 Set_Etype (N, Any_Type);
10273 end if;
10274
10275 -- Assignments, return statements, components of aggregates,
10276 -- generic instantiations will require convention checks if
10277 -- the type is an access to subprogram. Given that there will
10278 -- also be accessibility checks on those, this is where the
10279 -- checks can eventually be centralized ???
10280
e1b871e9
AC
10281 if Ekind_In (Btyp, E_Access_Subprogram_Type,
10282 E_Anonymous_Access_Subprogram_Type,
1a265e78 10283 E_Access_Protected_Subprogram_Type,
e1b871e9 10284 E_Anonymous_Access_Protected_Subprogram_Type)
af4b9434 10285 then
822033eb
HK
10286 -- Deal with convention mismatch
10287
1a265e78
AC
10288 if Convention (Designated_Type (Btyp)) /=
10289 Convention (Entity (P))
10290 then
822033eb
HK
10291 Error_Msg_FE
10292 ("subprogram & has wrong convention", P, Entity (P));
80c2c202
AC
10293 Error_Msg_Sloc := Sloc (Btyp);
10294 Error_Msg_FE ("\does not match & declared#", P, Btyp);
822033eb 10295
80c2c202
AC
10296 if not Is_Itype (Btyp)
10297 and then not Has_Convention_Pragma (Btyp)
10298 then
822033eb
HK
10299 Error_Msg_FE
10300 ("\probable missing pragma Convention for &",
10301 P, Btyp);
10302 end if;
996ae0b0
RK
10303
10304 else
10305 Check_Subtype_Conformant
10306 (New_Id => Entity (P),
10307 Old_Id => Designated_Type (Btyp),
10308 Err_Loc => P);
10309 end if;
10310
10311 if Attr_Id = Attribute_Unchecked_Access then
10312 Error_Msg_Name_1 := Aname;
822033eb 10313 Error_Msg_F
996ae0b0
RK
10314 ("attribute% cannot be applied to a subprogram", P);
10315
10316 elsif Aname = Name_Unrestricted_Access then
10317 null; -- Nothing to check
10318
b8dc622e
JM
10319 -- Check the static accessibility rule of 3.10.2(32).
10320 -- This rule also applies within the private part of an
10321 -- instantiation. This rule does not apply to anonymous
2995ebee 10322 -- access-to-subprogram types in access parameters.
996ae0b0
RK
10323
10324 elsif Attr_Id = Attribute_Access
b8dc622e 10325 and then not In_Instance_Body
2995ebee
AC
10326 and then
10327 (Ekind (Btyp) = E_Access_Subprogram_Type
10328 or else Is_Local_Anonymous_Access (Btyp))
af4b9434
AC
10329 and then Subprogram_Access_Level (Entity (P)) >
10330 Type_Access_Level (Btyp)
996ae0b0 10331 then
822033eb 10332 Error_Msg_F
b8dc622e
JM
10333 ("subprogram must not be deeper than access type", P);
10334
10335 -- Check the restriction of 3.10.2(32) that disallows the
10336 -- access attribute within a generic body when the ultimate
10337 -- ancestor of the type of the attribute is declared outside
10338 -- of the generic unit and the subprogram is declared within
10339 -- that generic unit. This includes any such attribute that
10340 -- occurs within the body of a generic unit that is a child
10341 -- of the generic unit where the subprogram is declared.
599a7411 10342
12a13f01 10343 -- The rule also prohibits applying the attribute when the
b8dc622e
JM
10344 -- access type is a generic formal access type (since the
10345 -- level of the actual type is not known). This restriction
10346 -- does not apply when the attribute type is an anonymous
10347 -- access-to-subprogram type. Note that this check was
46413d9e 10348 -- revised by AI-229, because the original Ada 95 rule
b8dc622e
JM
10349 -- was too lax. The original rule only applied when the
10350 -- subprogram was declared within the body of the generic,
10351 -- which allowed the possibility of dangling references).
46413d9e 10352 -- The rule was also too strict in some cases, in that it
b8dc622e
JM
10353 -- didn't permit the access to be declared in the generic
10354 -- spec, whereas the revised rule does (as long as it's not
10355 -- a formal type).
10356
10357 -- There are a couple of subtleties of the test for applying
10358 -- the check that are worth noting. First, we only apply it
10359 -- when the levels of the subprogram and access type are the
10360 -- same (the case where the subprogram is statically deeper
10361 -- was applied above, and the case where the type is deeper
10362 -- is always safe). Second, we want the check to apply
10363 -- within nested generic bodies and generic child unit
10364 -- bodies, but not to apply to an attribute that appears in
10365 -- the generic unit's specification. This is done by testing
10366 -- that the attribute's innermost enclosing generic body is
10367 -- not the same as the innermost generic body enclosing the
10368 -- generic unit where the subprogram is declared (we don't
10369 -- want the check to apply when the access attribute is in
10370 -- the spec and there's some other generic body enclosing
10371 -- generic). Finally, there's no point applying the check
822033eb
HK
10372 -- when within an instance, because any violations will have
10373 -- been caught by the compilation of the generic unit.
f529bac5 10374
303fbb20
AC
10375 -- We relax this check in Relaxed_RM_Semantics mode for
10376 -- compatibility with legacy code for use by Ada source
10377 -- code analyzers (e.g. CodePeer).
8cce3d75 10378
b8dc622e 10379 elsif Attr_Id = Attribute_Access
303fbb20 10380 and then not Relaxed_RM_Semantics
b8dc622e
JM
10381 and then not In_Instance
10382 and then Present (Enclosing_Generic_Unit (Entity (P)))
10383 and then Present (Enclosing_Generic_Body (N))
10384 and then Enclosing_Generic_Body (N) /=
10385 Enclosing_Generic_Body
10386 (Enclosing_Generic_Unit (Entity (P)))
10387 and then Subprogram_Access_Level (Entity (P)) =
10388 Type_Access_Level (Btyp)
10389 and then Ekind (Btyp) /=
10390 E_Anonymous_Access_Subprogram_Type
10391 and then Ekind (Btyp) /=
10392 E_Anonymous_Access_Protected_Subprogram_Type
996ae0b0 10393 then
b8dc622e
JM
10394 -- The attribute type's ultimate ancestor must be
10395 -- declared within the same generic unit as the
46413d9e
AC
10396 -- subprogram is declared (including within another
10397 -- nested generic unit). The error message is
8cce3d75
AC
10398 -- specialized to say "ancestor" for the case where the
10399 -- access type is not its own ancestor, since saying
10400 -- simply "access type" would be very confusing.
b8dc622e 10401
46413d9e
AC
10402 if not Declared_Within_Generic_Unit
10403 (Root_Type (Btyp),
10404 Enclosing_Generic_Unit (Entity (P)))
b8dc622e 10405 then
fea9e956
ES
10406 Error_Msg_N
10407 ("''Access attribute not allowed in generic body",
10408 N);
10409
b8dc622e 10410 if Root_Type (Btyp) = Btyp then
fea9e956
ES
10411 Error_Msg_NE
10412 ("\because " &
10413 "access type & is declared outside " &
442ade9d 10414 "generic unit (RM 3.10.2(32))", N, Btyp);
b8dc622e 10415 else
fea9e956
ES
10416 Error_Msg_NE
10417 ("\because ancestor of " &
10418 "access type & is declared outside " &
442ade9d 10419 "generic unit (RM 3.10.2(32))", N, Btyp);
b8dc622e
JM
10420 end if;
10421
fea9e956
ES
10422 Error_Msg_NE
10423 ("\move ''Access to private part, or " &
10424 "(Ada 2005) use anonymous access type instead of &",
10425 N, Btyp);
10426
b8dc622e
JM
10427 -- If the ultimate ancestor of the attribute's type is
10428 -- a formal type, then the attribute is illegal because
10429 -- the actual type might be declared at a higher level.
10430 -- The error message is specialized to say "ancestor"
10431 -- for the case where the access type is not its own
10432 -- ancestor, since saying simply "access type" would be
10433 -- very confusing.
10434
10435 elsif Is_Generic_Type (Root_Type (Btyp)) then
10436 if Root_Type (Btyp) = Btyp then
10437 Error_Msg_N
10438 ("access type must not be a generic formal type",
10439 N);
10440 else
10441 Error_Msg_N
10442 ("ancestor access type must not be a generic " &
10443 "formal type", N);
10444 end if;
10445 end if;
996ae0b0
RK
10446 end if;
10447 end if;
10448
5d09245e 10449 -- If this is a renaming, an inherited operation, or a
fea9e956
ES
10450 -- subprogram instance, use the original entity. This may make
10451 -- the node type-inconsistent, so this transformation can only
10452 -- be done if the node will not be reanalyzed. In particular,
10453 -- if it is within a default expression, the transformation
10454 -- must be delayed until the default subprogram is created for
10455 -- it, when the enclosing subprogram is frozen.
996ae0b0
RK
10456
10457 if Is_Entity_Name (P)
10458 and then Is_Overloadable (Entity (P))
10459 and then Present (Alias (Entity (P)))
fea9e956 10460 and then Expander_Active
996ae0b0
RK
10461 then
10462 Rewrite (P,
10463 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
10464 end if;
10465
10466 elsif Nkind (P) = N_Selected_Component
10467 and then Is_Overloadable (Entity (Selector_Name (P)))
10468 then
10469 -- Protected operation. If operation is overloaded, must
10470 -- disambiguate. Prefix that denotes protected object itself
10471 -- is resolved with its own type.
10472
10473 if Attr_Id = Attribute_Unchecked_Access then
10474 Error_Msg_Name_1 := Aname;
822033eb 10475 Error_Msg_F
996ae0b0
RK
10476 ("attribute% cannot be applied to protected operation", P);
10477 end if;
10478
fbf5a39b 10479 Resolve (Prefix (P));
07fc65c4 10480 Generate_Reference (Entity (Selector_Name (P)), P);
996ae0b0 10481
289a994b
AC
10482 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
10483 -- statically illegal if F is an anonymous access to subprogram.
10484
10485 elsif Nkind (P) = N_Explicit_Dereference
10486 and then Is_Entity_Name (Prefix (P))
10487 and then Ekind (Etype (Entity (Prefix (P)))) =
10488 E_Anonymous_Access_Subprogram_Type
10489 then
10490 Error_Msg_N ("anonymous access to subprogram "
10491 & "has deeper accessibility than any master", P);
10492
996ae0b0
RK
10493 elsif Is_Overloaded (P) then
10494
5f3ab6fb
AC
10495 -- Use the designated type of the context to disambiguate
10496 -- Note that this was not strictly conformant to Ada 95,
10497 -- but was the implementation adopted by most Ada 95 compilers.
10498 -- The use of the context type to resolve an Access attribute
10499 -- reference is now mandated in AI-235 for Ada 2005.
5d09245e 10500
996ae0b0
RK
10501 declare
10502 Index : Interp_Index;
10503 It : Interp;
5f3ab6fb 10504
996ae0b0
RK
10505 begin
10506 Get_First_Interp (P, Index, It);
996ae0b0
RK
10507 while Present (It.Typ) loop
10508 if Covers (Designated_Type (Typ), It.Typ) then
10509 Resolve (P, It.Typ);
10510 exit;
10511 end if;
10512
10513 Get_Next_Interp (Index, It);
10514 end loop;
10515 end;
10516 else
fbf5a39b 10517 Resolve (P);
996ae0b0
RK
10518 end if;
10519
822033eb
HK
10520 -- X'Access is illegal if X denotes a constant and the access type
10521 -- is access-to-variable. Same for 'Unchecked_Access. The rule
10522 -- does not apply to 'Unrestricted_Access. If the reference is a
10523 -- default-initialized aggregate component for a self-referential
10524 -- type the reference is legal.
996ae0b0
RK
10525
10526 if not (Ekind (Btyp) = E_Access_Subprogram_Type
af4b9434 10527 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
442ade9d
RD
10528 or else (Is_Record_Type (Btyp)
10529 and then
10530 Present (Corresponding_Remote_Type (Btyp)))
996ae0b0 10531 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
af4b9434
AC
10532 or else Ekind (Btyp)
10533 = E_Anonymous_Access_Protected_Subprogram_Type
996ae0b0
RK
10534 or else Is_Access_Constant (Btyp)
10535 or else Is_Variable (P)
10536 or else Attr_Id = Attribute_Unrestricted_Access)
10537 then
468c6c8a
ES
10538 if Is_Entity_Name (P)
10539 and then Is_Type (Entity (P))
10540 then
10541 -- Legality of a self-reference through an access
10542 -- attribute has been verified in Analyze_Access_Attribute.
10543
10544 null;
10545
10546 elsif Comes_From_Source (N) then
822033eb 10547 Error_Msg_F ("access-to-variable designates constant", P);
996ae0b0
RK
10548 end if;
10549 end if;
10550
7b76e805
RD
10551 Des_Btyp := Designated_Type (Btyp);
10552
0791fbe9 10553 if Ada_Version >= Ada_2005
7b76e805
RD
10554 and then Is_Incomplete_Type (Des_Btyp)
10555 then
10556 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
10557 -- imported entity, and the non-limited view is visible, make
10558 -- use of it. If it is an incomplete subtype, use the base type
10559 -- in any case.
10560
7b56a91b 10561 if From_Limited_With (Des_Btyp)
7b76e805
RD
10562 and then Present (Non_Limited_View (Des_Btyp))
10563 then
10564 Des_Btyp := Non_Limited_View (Des_Btyp);
10565
10566 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
10567 Des_Btyp := Etype (Des_Btyp);
10568 end if;
10569 end if;
10570
996ae0b0
RK
10571 if (Attr_Id = Attribute_Access
10572 or else
10573 Attr_Id = Attribute_Unchecked_Access)
10574 and then (Ekind (Btyp) = E_General_Access_Type
b07b7ace 10575 or else Ekind (Btyp) = E_Anonymous_Access_Type)
996ae0b0 10576 then
0ab80019 10577 -- Ada 2005 (AI-230): Check the accessibility of anonymous
822033eb
HK
10578 -- access types for stand-alone objects, record and array
10579 -- components, and return objects. For a component definition
10580 -- the level is the same of the enclosing composite type.
35b7fa6a 10581
0791fbe9 10582 if Ada_Version >= Ada_2005
d15f9422
AC
10583 and then (Is_Local_Anonymous_Access (Btyp)
10584
011f9d5d
AC
10585 -- Handle cases where Btyp is the anonymous access
10586 -- type of an Ada 2012 stand-alone object.
d15f9422 10587
996c8821
RD
10588 or else Nkind (Associated_Node_For_Itype (Btyp)) =
10589 N_Object_Declaration)
011f9d5d
AC
10590 and then
10591 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
468c6c8a 10592 and then Attr_Id = Attribute_Access
35b7fa6a 10593 then
ef1c0511
AC
10594 -- In an instance, this is a runtime check, but one we know
10595 -- will fail, so generate an appropriate warning. As usual,
10596 -- this kind of warning is an error in SPARK mode.
35b7fa6a
AC
10597
10598 if In_Instance_Body then
43417b90 10599 Error_Msg_Warn := SPARK_Mode /= On;
822033eb 10600 Error_Msg_F
4a28b181
AC
10601 ("non-local pointer cannot point to local object<<", P);
10602 Error_Msg_F ("\Program_Error [<<", P);
10603
35b7fa6a
AC
10604 Rewrite (N,
10605 Make_Raise_Program_Error (Loc,
10606 Reason => PE_Accessibility_Check_Failed));
10607 Set_Etype (N, Typ);
822033eb 10608
35b7fa6a 10609 else
822033eb 10610 Error_Msg_F
35b7fa6a
AC
10611 ("non-local pointer cannot point to local object", P);
10612 end if;
10613 end if;
10614
996ae0b0 10615 if Is_Dependent_Component_Of_Mutable_Object (P) then
822033eb 10616 Error_Msg_F
996ae0b0
RK
10617 ("illegal attribute for discriminant-dependent component",
10618 P);
10619 end if;
10620
442ade9d
RD
10621 -- Check static matching rule of 3.10.2(27). Nominal subtype
10622 -- of the prefix must statically match the designated type.
996ae0b0
RK
10623
10624 Nom_Subt := Etype (P);
10625
10626 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
822033eb 10627 Nom_Subt := Base_Type (Nom_Subt);
996ae0b0
RK
10628 end if;
10629
10630 if Is_Tagged_Type (Designated_Type (Typ)) then
32213142 10631
996ae0b0 10632 -- If the attribute is in the context of an access
3f1bc2cf
AC
10633 -- parameter, then the prefix is allowed to be of
10634 -- the class-wide type (by AI-127).
996ae0b0
RK
10635
10636 if Ekind (Typ) = E_Anonymous_Access_Type then
10637 if not Covers (Designated_Type (Typ), Nom_Subt)
10638 and then not Covers (Nom_Subt, Designated_Type (Typ))
10639 then
ee0a48c5
ES
10640 declare
10641 Desig : Entity_Id;
10642
10643 begin
10644 Desig := Designated_Type (Typ);
10645
10646 if Is_Class_Wide_Type (Desig) then
10647 Desig := Etype (Desig);
10648 end if;
10649
10650 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
10651 null;
10652
10653 else
822033eb 10654 Error_Msg_FE
ee0a48c5
ES
10655 ("type of prefix: & not compatible",
10656 P, Nom_Subt);
822033eb 10657 Error_Msg_FE
ee0a48c5
ES
10658 ("\with &, the expected designated type",
10659 P, Designated_Type (Typ));
10660 end if;
10661 end;
996ae0b0
RK
10662 end if;
10663
10664 elsif not Covers (Designated_Type (Typ), Nom_Subt)
10665 or else
10666 (not Is_Class_Wide_Type (Designated_Type (Typ))
10667 and then Is_Class_Wide_Type (Nom_Subt))
10668 then
822033eb 10669 Error_Msg_FE
996ae0b0 10670 ("type of prefix: & is not covered", P, Nom_Subt);
822033eb 10671 Error_Msg_FE
996ae0b0 10672 ("\by &, the expected designated type" &
442ade9d 10673 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
996ae0b0
RK
10674 end if;
10675
10676 if Is_Class_Wide_Type (Designated_Type (Typ))
10677 and then Has_Discriminants (Etype (Designated_Type (Typ)))
10678 and then Is_Constrained (Etype (Designated_Type (Typ)))
10679 and then Designated_Type (Typ) /= Nom_Subt
10680 then
10681 Apply_Discriminant_Check
10682 (N, Etype (Designated_Type (Typ)));
10683 end if;
10684
468c6c8a
ES
10685 -- Ada 2005 (AI-363): Require static matching when designated
10686 -- type has discriminants and a constrained partial view, since
10687 -- in general objects of such types are mutable, so we can't
10688 -- allow the access value to designate a constrained object
10689 -- (because access values must be assumed to designate mutable
10690 -- objects when designated type does not impose a constraint).
10691
7b76e805
RD
10692 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
10693 null;
10694
10695 elsif Has_Discriminants (Designated_Type (Typ))
10696 and then not Is_Constrained (Des_Btyp)
996ae0b0 10697 and then
0791fbe9 10698 (Ada_Version < Ada_2005
7b76e805 10699 or else
0fbcb11c 10700 not Object_Type_Has_Constrained_Partial_View
d600ef16
RD
10701 (Typ => Designated_Type (Base_Type (Typ)),
10702 Scop => Current_Scope))
996ae0b0 10703 then
7b76e805
RD
10704 null;
10705
10706 else
822033eb 10707 Error_Msg_F
996ae0b0
RK
10708 ("object subtype must statically match "
10709 & "designated subtype", P);
10710
10711 if Is_Entity_Name (P)
10712 and then Is_Array_Type (Designated_Type (Typ))
10713 then
996ae0b0
RK
10714 declare
10715 D : constant Node_Id := Declaration_Node (Entity (P));
996ae0b0 10716 begin
324ac540
AC
10717 Error_Msg_N
10718 ("aliased object has explicit bounds??", D);
10719 Error_Msg_N
10720 ("\declare without bounds (and with explicit "
10721 & "initialization)??", D);
10722 Error_Msg_N
10723 ("\for use with unconstrained access??", D);
996ae0b0
RK
10724 end;
10725 end if;
10726 end if;
10727
83e5da69
AC
10728 -- Check the static accessibility rule of 3.10.2(28). Note that
10729 -- this check is not performed for the case of an anonymous
10730 -- access type, since the access attribute is always legal
10731 -- in such a context.
996ae0b0
RK
10732
10733 if Attr_Id /= Attribute_Unchecked_Access
a7e68e7f 10734 and then Ekind (Btyp) = E_General_Access_Type
f460d8f3
SB
10735 and then
10736 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
996ae0b0 10737 then
f529bac5
ES
10738 Accessibility_Message;
10739 return;
996ae0b0
RK
10740 end if;
10741 end if;
10742
e1b871e9
AC
10743 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
10744 E_Anonymous_Access_Protected_Subprogram_Type)
996ae0b0 10745 then
f529bac5
ES
10746 if Is_Entity_Name (P)
10747 and then not Is_Protected_Type (Scope (Entity (P)))
10748 then
822033eb 10749 Error_Msg_F ("context requires a protected subprogram", P);
f529bac5 10750
442ade9d
RD
10751 -- Check accessibility of protected object against that of the
10752 -- access type, but only on user code, because the expander
10753 -- creates access references for handlers. If the context is an
10754 -- anonymous_access_to_protected, there are no accessibility
10755 -- checks either. Omit check entirely for Unrestricted_Access.
f529bac5 10756
f460d8f3 10757 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
f529bac5
ES
10758 and then Comes_From_Source (N)
10759 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
822033eb 10760 and then Attr_Id /= Attribute_Unrestricted_Access
f529bac5
ES
10761 then
10762 Accessibility_Message;
10763 return;
c92e8586
AC
10764
10765 -- AI05-0225: If the context is not an access to protected
10766 -- function, the prefix must be a variable, given that it may
10767 -- be used subsequently in a protected call.
10768
10769 elsif Nkind (P) = N_Selected_Component
10770 and then not Is_Variable (Prefix (P))
10771 and then Ekind (Entity (Selector_Name (P))) /= E_Function
10772 then
10773 Error_Msg_N
10774 ("target object of access to protected procedure "
10775 & "must be variable", N);
10776
10777 elsif Is_Entity_Name (P) then
10778 Check_Internal_Protected_Use (N, Entity (P));
f529bac5 10779 end if;
996ae0b0 10780
e1b871e9
AC
10781 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
10782 E_Anonymous_Access_Subprogram_Type)
996ae0b0
RK
10783 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
10784 then
822033eb 10785 Error_Msg_F ("context requires a non-protected subprogram", P);
996ae0b0
RK
10786 end if;
10787
10788 -- The context cannot be a pool-specific type, but this is a
10789 -- legality rule, not a resolution rule, so it must be checked
10790 -- separately, after possibly disambiguation (see AI-245).
10791
10792 if Ekind (Btyp) = E_Access_Type
10793 and then Attr_Id /= Attribute_Unrestricted_Access
10794 then
10795 Wrong_Type (N, Typ);
10796 end if;
10797
822033eb
HK
10798 -- The context may be a constrained access type (however ill-
10799 -- advised such subtypes might be) so in order to generate a
10800 -- constraint check when needed set the type of the attribute
10801 -- reference to the base type of the context.
10802
10803 Set_Etype (N, Btyp);
996ae0b0
RK
10804
10805 -- Check for incorrect atomic/volatile reference (RM C.6(12))
10806
10807 if Attr_Id /= Attribute_Unrestricted_Access then
10808 if Is_Atomic_Object (P)
10809 and then not Is_Atomic (Designated_Type (Typ))
10810 then
822033eb 10811 Error_Msg_F
996ae0b0
RK
10812 ("access to atomic object cannot yield access-to-" &
10813 "non-atomic type", P);
10814
10815 elsif Is_Volatile_Object (P)
10816 and then not Is_Volatile (Designated_Type (Typ))
10817 then
822033eb 10818 Error_Msg_F
996ae0b0
RK
10819 ("access to volatile object cannot yield access-to-" &
10820 "non-volatile type", P);
10821 end if;
10822 end if;
10823
b07b7ace
AC
10824 -- Check for unrestricted access where expected type is a thin
10825 -- pointer to an unconstrained array.
10826
10827 if Non_Aliased_Prefix (N)
10828 and then Has_Size_Clause (Typ)
10829 and then RM_Size (Typ) = System_Address_Size
10830 then
10831 declare
10832 DT : constant Entity_Id := Designated_Type (Typ);
10833 begin
10834 if Is_Array_Type (DT) and then not Is_Constrained (DT) then
10835 Error_Msg_N
10836 ("illegal use of Unrestricted_Access attribute", P);
10837 Error_Msg_N
10838 ("\attempt to generate thin pointer to unaliased "
10839 & "object", P);
10840 end if;
10841 end;
10842 end if;
10843
10844 -- Mark that address of entity is taken
10845
442ade9d
RD
10846 if Is_Entity_Name (P) then
10847 Set_Address_Taken (Entity (P));
10848 end if;
113a9fb6 10849
b8e6830b
AC
10850 -- Deal with possible elaboration check
10851
10852 if Is_Entity_Name (P) and then Is_Subprogram (Entity (P)) then
113a9fb6 10853 declare
b8e6830b
AC
10854 Subp_Id : constant Entity_Id := Entity (P);
10855 Scop : constant Entity_Id := Scope (Subp_Id);
10856 Subp_Decl : constant Node_Id :=
10857 Unit_Declaration_Node (Subp_Id);
8c691dc6
AC
10858 Flag_Id : Entity_Id;
10859 Subp_Body : Node_Id;
113a9fb6
AC
10860
10861 -- If the access has been taken and the body of the subprogram
10862 -- has not been see yet, indirect calls must be protected with
10863 -- elaboration checks. We have the proper elaboration machinery
10864 -- for subprograms declared in packages, but within a block or
10865 -- a subprogram the body will appear in the same declarative
10866 -- part, and we must insert a check in the eventual body itself
10867 -- using the elaboration flag that we generate now. The check
b8e6830b
AC
10868 -- is then inserted when the body is expanded. This processing
10869 -- is not needed for a stand alone expression function because
10870 -- the internally generated spec and body are always inserted
10871 -- as a pair in the same declarative list.
113a9fb6
AC
10872
10873 begin
b8e6830b
AC
10874 if Expander_Active
10875 and then Comes_From_Source (Subp_Id)
113a9fb6 10876 and then Comes_From_Source (N)
b8e6830b
AC
10877 and then In_Open_Scopes (Scop)
10878 and then Ekind_In (Scop, E_Block, E_Procedure, E_Function)
10879 and then not Has_Completion (Subp_Id)
10880 and then No (Elaboration_Entity (Subp_Id))
10881 and then Nkind (Subp_Decl) = N_Subprogram_Declaration
10882 and then Nkind (Original_Node (Subp_Decl)) /=
10883 N_Expression_Function
113a9fb6
AC
10884 then
10885 -- Create elaboration variable for it
10886
b8e6830b
AC
10887 Flag_Id := Make_Temporary (Loc, 'E');
10888 Set_Elaboration_Entity (Subp_Id, Flag_Id);
10889 Set_Is_Frozen (Flag_Id);
10890
10891 -- Insert declaration for flag after subprogram
10892 -- declaration. Note that attribute reference may
10893 -- appear within a nested scope.
10894
10895 Insert_After_And_Analyze (Subp_Decl,
113a9fb6 10896 Make_Object_Declaration (Loc,
b8e6830b 10897 Defining_Identifier => Flag_Id,
113a9fb6
AC
10898 Object_Definition =>
10899 New_Occurrence_Of (Standard_Short_Integer, Loc),
10900 Expression =>
b8e6830b
AC
10901 Make_Integer_Literal (Loc, Uint_0)));
10902 end if;
f9e333ab 10903
b8e6830b
AC
10904 -- Taking the 'Access of an expression function freezes its
10905 -- expression (RM 13.14 10.3/3). This does not apply to an
10906 -- expression function that acts as a completion because the
10907 -- generated body is immediately analyzed and the expression
10908 -- is automatically frozen.
10909
8c691dc6 10910 if Is_Expression_Function (Subp_Id)
b8e6830b 10911 and then Present (Corresponding_Body (Subp_Decl))
b8e6830b 10912 then
8c691dc6
AC
10913 Subp_Body :=
10914 Unit_Declaration_Node (Corresponding_Body (Subp_Decl));
b8e6830b 10915
8d1fe980
AC
10916 -- The body has already been analyzed when the expression
10917 -- function acts as a completion.
f9e333ab 10918
8d1fe980
AC
10919 if Analyzed (Subp_Body) then
10920 null;
10921
10922 -- Attribute 'Access may appear within the generated body
10923 -- of the expression function subject to the attribute:
10924
10925 -- function F is (... F'Access ...);
10926
10927 -- If the expression function is on the scope stack, then
10928 -- the body is currently being analyzed. Do not reanalyze
10929 -- it because this will lead to infinite recursion.
10930
10931 elsif In_Open_Scopes (Subp_Id) then
10932 null;
10933
72d5c70b
AC
10934 -- If reference to the expression function appears in an
10935 -- inner scope, for example as an actual in an instance,
10936 -- this is not a freeze point either.
10937
10938 elsif Scope (Subp_Id) /= Current_Scope then
10939 null;
10940
8d1fe980
AC
10941 -- Analyze the body of the expression function to freeze
10942 -- the expression. This takes care of the case where the
10943 -- 'Access is part of dispatch table initialization and
10944 -- the generated body of the expression function has not
10945 -- been analyzed yet.
10946
10947 else
8c691dc6 10948 Analyze (Subp_Body);
b8e6830b 10949 end if;
113a9fb6
AC
10950 end if;
10951 end;
10952 end if;
442ade9d
RD
10953 end Access_Attribute;
10954
996ae0b0
RK
10955 -------------
10956 -- Address --
10957 -------------
10958
10959 -- Deal with resolving the type for Address attribute, overloading
10960 -- is not permitted here, since there is no context to resolve it.
10961
10962 when Attribute_Address | Attribute_Code_Address =>
442ade9d 10963 Address_Attribute : begin
996ae0b0
RK
10964
10965 -- To be safe, assume that if the address of a variable is taken,
10966 -- it may be modified via this address, so note modification.
10967
10968 if Is_Variable (P) then
21d27997 10969 Note_Possible_Modification (P, Sure => False);
996ae0b0
RK
10970 end if;
10971
c4e5e10f 10972 if Nkind (P) in N_Subexpr
996ae0b0
RK
10973 and then Is_Overloaded (P)
10974 then
10975 Get_First_Interp (P, Index, It);
10976 Get_Next_Interp (Index, It);
10977
10978 if Present (It.Nam) then
10979 Error_Msg_Name_1 := Aname;
822033eb 10980 Error_Msg_F
c4e5e10f 10981 ("prefix of % attribute cannot be overloaded", P);
996ae0b0
RK
10982 end if;
10983 end if;
10984
996ae0b0 10985 if not Is_Entity_Name (P)
442ade9d 10986 or else not Is_Overloadable (Entity (P))
996ae0b0
RK
10987 then
10988 if not Is_Task_Type (Etype (P))
10989 or else Nkind (P) = N_Explicit_Dereference
10990 then
fbf5a39b 10991 Resolve (P);
996ae0b0
RK
10992 end if;
10993 end if;
10994
10995 -- If this is the name of a derived subprogram, or that of a
10996 -- generic actual, the address is that of the original entity.
10997
10998 if Is_Entity_Name (P)
10999 and then Is_Overloadable (Entity (P))
11000 and then Present (Alias (Entity (P)))
11001 then
11002 Rewrite (P,
11003 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
11004 end if;
11005
442ade9d
RD
11006 if Is_Entity_Name (P) then
11007 Set_Address_Taken (Entity (P));
11008 end if;
bfc157d5
ES
11009
11010 if Nkind (P) = N_Slice then
11011
11012 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11013 -- even if the array is packed and the slice itself is not
11014 -- addressable. Transform the prefix into an indexed component.
edde779b 11015
a66996b3 11016 -- Note that the transformation is safe only if we know that
edde779b
AC
11017 -- the slice is non-null. That is because a null slice can have
11018 -- an out of bounds index value.
11019
4c4e9ad2
ES
11020 -- Right now, gigi blows up if given 'Address on a slice as a
11021 -- result of some incorrect freeze nodes generated by the front
11022 -- end, and this covers up that bug in one case, but the bug is
11023 -- likely still there in the cases not handled by this code ???
edde779b
AC
11024
11025 -- It's not clear what 'Address *should* return for a null
11026 -- slice with out of bounds indexes, this might be worth an ARG
11027 -- discussion ???
11028
11029 -- One approach would be to do a length check unconditionally,
11030 -- and then do the transformation below unconditionally, but
11031 -- analyze with checks off, avoiding the problem of the out of
11032 -- bounds index. This approach would interpret the address of
11033 -- an out of bounds null slice as being the address where the
11034 -- array element would be if there was one, which is probably
11035 -- as reasonable an interpretation as any ???
bfc157d5
ES
11036
11037 declare
11038 Loc : constant Source_Ptr := Sloc (P);
11039 D : constant Node_Id := Discrete_Range (P);
11040 Lo : Node_Id;
11041
11042 begin
a66996b3
ES
11043 if Is_Entity_Name (D)
11044 and then
11045 Not_Null_Range
11046 (Type_Low_Bound (Entity (D)),
11047 Type_High_Bound (Entity (D)))
11048 then
bfc157d5
ES
11049 Lo :=
11050 Make_Attribute_Reference (Loc,
11051 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
11052 Attribute_Name => Name_First);
a66996b3 11053
4c4e9ad2
ES
11054 elsif Nkind (D) = N_Range
11055 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
11056 then
bfc157d5 11057 Lo := Low_Bound (D);
a66996b3
ES
11058
11059 else
11060 Lo := Empty;
bfc157d5
ES
11061 end if;
11062
a66996b3
ES
11063 if Present (Lo) then
11064 Rewrite (P,
11065 Make_Indexed_Component (Loc,
11066 Prefix => Relocate_Node (Prefix (P)),
11067 Expressions => New_List (Lo)));
bfc157d5 11068
a66996b3
ES
11069 Analyze_And_Resolve (P);
11070 end if;
bfc157d5
ES
11071 end;
11072 end if;
442ade9d
RD
11073 end Address_Attribute;
11074
996ae0b0
RK
11075 ------------------
11076 -- Body_Version --
11077 ------------------
11078
11079 -- Prefix of Body_Version attribute can be a subprogram name which
11080 -- must not be resolved, since this is not a call.
11081
11082 when Attribute_Body_Version =>
11083 null;
11084
11085 ------------
11086 -- Caller --
11087 ------------
11088
11089 -- Prefix of Caller attribute is an entry name which must not
11090 -- be resolved, since this is definitely not an entry call.
11091
11092 when Attribute_Caller =>
11093 null;
11094
11095 ------------------
11096 -- Code_Address --
11097 ------------------
11098
11099 -- Shares processing with Address attribute
11100
11101 -----------
11102 -- Count --
11103 -----------
11104
fbf5a39b
AC
11105 -- If the prefix of the Count attribute is an entry name it must not
11106 -- be resolved, since this is definitely not an entry call. However,
11107 -- if it is an element of an entry family, the index itself may
11108 -- have to be resolved because it can be a general expression.
996ae0b0
RK
11109
11110 when Attribute_Count =>
fbf5a39b
AC
11111 if Nkind (P) = N_Indexed_Component
11112 and then Is_Entity_Name (Prefix (P))
11113 then
11114 declare
11115 Indx : constant Node_Id := First (Expressions (P));
11116 Fam : constant Entity_Id := Entity (Prefix (P));
11117 begin
11118 Resolve (Indx, Entry_Index_Type (Fam));
11119 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
11120 end;
11121 end if;
996ae0b0
RK
11122
11123 ----------------
11124 -- Elaborated --
11125 ----------------
11126
11127 -- Prefix of the Elaborated attribute is a subprogram name which
11128 -- must not be resolved, since this is definitely not a call. Note
11129 -- that it is a library unit, so it cannot be overloaded here.
11130
11131 when Attribute_Elaborated =>
11132 null;
11133
442ade9d
RD
11134 -------------
11135 -- Enabled --
11136 -------------
11137
11138 -- Prefix of Enabled attribute is a check name, which must be treated
11139 -- specially and not touched by Resolve.
11140
11141 when Attribute_Enabled =>
11142 null;
11143
ba08ba84
AC
11144 ----------------
11145 -- Loop_Entry --
11146 ----------------
11147
11148 -- Do not resolve the prefix of Loop_Entry, instead wait until the
11149 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
11150 -- The delay ensures that any generated checks or temporaries are
11151 -- inserted before the relocated prefix.
11152
11153 when Attribute_Loop_Entry =>
11154 null;
11155
996ae0b0
RK
11156 --------------------
11157 -- Mechanism_Code --
11158 --------------------
11159
11160 -- Prefix of the Mechanism_Code attribute is a function name
11161 -- which must not be resolved. Should we check for overloaded ???
11162
11163 when Attribute_Mechanism_Code =>
11164 null;
11165
11166 ------------------
11167 -- Partition_ID --
11168 ------------------
11169
11170 -- Most processing is done in sem_dist, after determining the
11171 -- context type. Node is rewritten as a conversion to a runtime call.
11172
11173 when Attribute_Partition_ID =>
11174 Process_Partition_Id (N);
11175 return;
11176
1033834f
RD
11177 ------------------
11178 -- Pool_Address --
11179 ------------------
11180
fbf5a39b
AC
11181 when Attribute_Pool_Address =>
11182 Resolve (P);
11183
996ae0b0
RK
11184 -----------
11185 -- Range --
11186 -----------
11187
c159409f
AC
11188 -- We replace the Range attribute node with a range expression whose
11189 -- bounds are the 'First and 'Last attributes applied to the same
11190 -- prefix. The reason that we do this transformation here instead of
11191 -- in the expander is that it simplifies other parts of the semantic
11192 -- analysis which assume that the Range has been replaced; thus it
11193 -- must be done even when in semantic-only mode (note that the RM
11194 -- specifically mentions this equivalence, we take care that the
11195 -- prefix is only evaluated once).
996ae0b0
RK
11196
11197 when Attribute_Range => Range_Attribute :
11198 declare
11199 LB : Node_Id;
11200 HB : Node_Id;
ed32b82e 11201 Dims : List_Id;
996ae0b0 11202
996ae0b0
RK
11203 begin
11204 if not Is_Entity_Name (P)
11205 or else not Is_Type (Entity (P))
11206 then
fbf5a39b 11207 Resolve (P);
996ae0b0
RK
11208 end if;
11209
ed32b82e
ES
11210 Dims := Expressions (N);
11211
21d27997
RD
11212 HB :=
11213 Make_Attribute_Reference (Loc,
a1d3851b 11214 Prefix => Duplicate_Subexpr (P, Name_Req => True),
21d27997 11215 Attribute_Name => Name_Last,
ed32b82e 11216 Expressions => Dims);
996ae0b0 11217
21d27997
RD
11218 LB :=
11219 Make_Attribute_Reference (Loc,
ed32b82e 11220 Prefix => P,
2c17ca0a
AC
11221 Attribute_Name => Name_First,
11222 Expressions => (Dims));
ed32b82e
ES
11223
11224 -- Do not share the dimension indicator, if present. Even
11225 -- though it is a static constant, its source location
11226 -- may be modified when printing expanded code and node
11227 -- sharing will lead to chaos in Sprint.
11228
11229 if Present (Dims) then
11230 Set_Expressions (LB,
11231 New_List (New_Copy_Tree (First (Dims))));
11232 end if;
996ae0b0
RK
11233
11234 -- If the original was marked as Must_Not_Freeze (see code
11235 -- in Sem_Ch3.Make_Index), then make sure the rewriting
11236 -- does not freeze either.
11237
11238 if Must_Not_Freeze (N) then
11239 Set_Must_Not_Freeze (HB);
11240 Set_Must_Not_Freeze (LB);
11241 Set_Must_Not_Freeze (Prefix (HB));
11242 Set_Must_Not_Freeze (Prefix (LB));
11243 end if;
11244
11245 if Raises_Constraint_Error (Prefix (N)) then
11246
11247 -- Preserve Sloc of prefix in the new bounds, so that
11248 -- the posted warning can be removed if we are within
11249 -- unreachable code.
11250
11251 Set_Sloc (LB, Sloc (Prefix (N)));
11252 Set_Sloc (HB, Sloc (Prefix (N)));
11253 end if;
11254
11255 Rewrite (N, Make_Range (Loc, LB, HB));
11256 Analyze_And_Resolve (N, Typ);
11257
25e29378
AC
11258 -- Ensure that the expanded range does not have side effects
11259
11260 Force_Evaluation (LB);
11261 Force_Evaluation (HB);
11262
996ae0b0
RK
11263 -- Normally after resolving attribute nodes, Eval_Attribute
11264 -- is called to do any possible static evaluation of the node.
11265 -- However, here since the Range attribute has just been
11266 -- transformed into a range expression it is no longer an
11267 -- attribute node and therefore the call needs to be avoided
11268 -- and is accomplished by simply returning from the procedure.
11269
11270 return;
11271 end Range_Attribute;
11272
21d27997
RD
11273 ------------
11274 -- Result --
11275 ------------
11276
11277 -- We will only come here during the prescan of a spec expression
11278 -- containing a Result attribute. In that case the proper Etype has
11279 -- already been set, and nothing more needs to be done here.
11280
11281 when Attribute_Result =>
11282 null;
11283
996ae0b0
RK
11284 ----------------------
11285 -- Unchecked_Access --
11286 ----------------------
11287
11288 -- Processing is shared with Access
11289
11290 -------------------------
11291 -- Unrestricted_Access --
11292 -------------------------
11293
11294 -- Processing is shared with Access
11295
08cd7c2f
AC
11296 ------------
11297 -- Update --
11298 ------------
11299
11300 -- Resolve aggregate components in component associations
11301
11302 when Attribute_Update =>
11303 declare
11304 Aggr : constant Node_Id := First (Expressions (N));
11305 Typ : constant Entity_Id := Etype (Prefix (N));
11306 Assoc : Node_Id;
11307 Comp : Node_Id;
3f433bc0 11308 Expr : Node_Id;
08cd7c2f
AC
11309
11310 begin
11311 -- Set the Etype of the aggregate to that of the prefix, even
11312 -- though the aggregate may not be a proper representation of a
11313 -- value of the type (missing or duplicated associations, etc.)
f1bd0415
AC
11314 -- Complete resolution of the prefix. Note that in Ada 2012 it
11315 -- can be a qualified expression that is e.g. an aggregate.
08cd7c2f
AC
11316
11317 Set_Etype (Aggr, Typ);
f1bd0415 11318 Resolve (Prefix (N), Typ);
08cd7c2f
AC
11319
11320 -- For an array type, resolve expressions with the component
3f433bc0 11321 -- type of the array, and apply constraint checks when needed.
08cd7c2f
AC
11322
11323 if Is_Array_Type (Typ) then
11324 Assoc := First (Component_Associations (Aggr));
11325 while Present (Assoc) loop
cc6f5d75 11326 Expr := Expression (Assoc);
3f433bc0 11327 Resolve (Expr, Component_Type (Typ));
414c6563
AC
11328
11329 -- For scalar array components set Do_Range_Check when
11330 -- needed. Constraint checking on non-scalar components
11331 -- is done in Aggregate_Constraint_Checks, but only if
11332 -- full analysis is enabled. These flags are not set in
11333 -- the front-end in GnatProve mode.
11334
11335 if Is_Scalar_Type (Component_Type (Typ))
11336 and then not Is_OK_Static_Expression (Expr)
11337 then
11338 if Is_Entity_Name (Expr)
11339 and then Etype (Expr) = Component_Type (Typ)
11340 then
11341 null;
11342
11343 else
11344 Set_Do_Range_Check (Expr);
11345 end if;
11346 end if;
3f1bc2cf
AC
11347
11348 -- The choices in the association are static constants,
11349 -- or static aggregates each of whose components belongs
11350 -- to the proper index type. However, they must also
11351 -- belong to the index subtype (s) of the prefix, which
11352 -- may be a subtype (e.g. given by a slice).
11353
11354 -- Choices may also be identifiers with no staticness
b3b26ace
AC
11355 -- requirements, in which case they must resolve to the
11356 -- index type.
3f1bc2cf
AC
11357
11358 declare
11359 C : Node_Id;
11360 C_E : Node_Id;
11361 Indx : Node_Id;
11362
11363 begin
11364 C := First (Choices (Assoc));
11365 while Present (C) loop
11366 Indx := First_Index (Etype (Prefix (N)));
11367
11368 if Nkind (C) /= N_Aggregate then
b3b26ace
AC
11369 Analyze_And_Resolve (C, Etype (Indx));
11370 Apply_Constraint_Check (C, Etype (Indx));
3f1bc2cf
AC
11371 Check_Non_Static_Context (C);
11372
11373 else
11374 C_E := First (Expressions (C));
11375 while Present (C_E) loop
b3b26ace
AC
11376 Analyze_And_Resolve (C_E, Etype (Indx));
11377 Apply_Constraint_Check (C_E, Etype (Indx));
3f1bc2cf 11378 Check_Non_Static_Context (C_E);
b3b26ace 11379
3f1bc2cf
AC
11380 Next (C_E);
11381 Next_Index (Indx);
11382 end loop;
11383 end if;
11384
11385 Next (C);
11386 end loop;
11387 end;
11388
08cd7c2f
AC
11389 Next (Assoc);
11390 end loop;
11391
11392 -- For a record type, use type of each component, which is
11393 -- recorded during analysis.
11394
11395 else
11396 Assoc := First (Component_Associations (Aggr));
11397 while Present (Assoc) loop
11398 Comp := First (Choices (Assoc));
33b87152 11399 Expr := Expression (Assoc);
3f1bc2cf 11400
08cd7c2f
AC
11401 if Nkind (Comp) /= N_Others_Choice
11402 and then not Error_Posted (Comp)
11403 then
33b87152
AC
11404 Resolve (Expr, Etype (Entity (Comp)));
11405
11406 if Is_Scalar_Type (Etype (Entity (Comp)))
11407 and then not Is_OK_Static_Expression (Expr)
11408 then
11409 Set_Do_Range_Check (Expr);
11410 end if;
08cd7c2f 11411 end if;
3f1bc2cf 11412
08cd7c2f
AC
11413 Next (Assoc);
11414 end loop;
11415 end if;
11416 end;
11417
996ae0b0
RK
11418 ---------
11419 -- Val --
11420 ---------
11421
11422 -- Apply range check. Note that we did not do this during the
11423 -- analysis phase, since we wanted Eval_Attribute to have a
11424 -- chance at finding an illegal out of range value.
11425
11426 when Attribute_Val =>
11427
11428 -- Note that we do our own Eval_Attribute call here rather than
11429 -- use the common one, because we need to do processing after
11430 -- the call, as per above comment.
11431
11432 Eval_Attribute (N);
11433
11434 -- Eval_Attribute may replace the node with a raise CE, or
11435 -- fold it to a constant. Obviously we only apply a scalar
a90bd866 11436 -- range check if this did not happen.
996ae0b0
RK
11437
11438 if Nkind (N) = N_Attribute_Reference
11439 and then Attribute_Name (N) = Name_Val
11440 then
11441 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
11442 end if;
11443
11444 return;
11445
11446 -------------
11447 -- Version --
11448 -------------
11449
11450 -- Prefix of Version attribute can be a subprogram name which
11451 -- must not be resolved, since this is not a call.
11452
11453 when Attribute_Version =>
11454 null;
11455
11456 ----------------------
11457 -- Other Attributes --
11458 ----------------------
11459
11460 -- For other attributes, resolve prefix unless it is a type. If
11461 -- the attribute reference itself is a type name ('Base and 'Class)
11462 -- then this is only legal within a task or protected record.
11463
11464 when others =>
59fad002 11465 if not Is_Entity_Name (P) or else not Is_Type (Entity (P)) then
fbf5a39b 11466 Resolve (P);
996ae0b0
RK
11467 end if;
11468
11469 -- If the attribute reference itself is a type name ('Base,
11470 -- 'Class) then this is only legal within a task or protected
11471 -- record. What is this all about ???
11472
59fad002 11473 if Is_Entity_Name (N) and then Is_Type (Entity (N)) then
996ae0b0
RK
11474 if Is_Concurrent_Type (Entity (N))
11475 and then In_Open_Scopes (Entity (P))
11476 then
11477 null;
11478 else
11479 Error_Msg_N
11480 ("invalid use of subtype name in expression or call", N);
11481 end if;
11482 end if;
11483
11484 -- For attributes whose argument may be a string, complete
11485 -- resolution of argument now. This avoids premature expansion
11486 -- (and the creation of transient scopes) before the attribute
11487 -- reference is resolved.
11488
11489 case Attr_Id is
11490 when Attribute_Value =>
11491 Resolve (First (Expressions (N)), Standard_String);
11492
11493 when Attribute_Wide_Value =>
11494 Resolve (First (Expressions (N)), Standard_Wide_String);
11495
82c80734
RD
11496 when Attribute_Wide_Wide_Value =>
11497 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
11498
996ae0b0
RK
11499 when others => null;
11500 end case;
fea9e956
ES
11501
11502 -- If the prefix of the attribute is a class-wide type then it
11503 -- will be expanded into a dispatching call to a predefined
11504 -- primitive. Therefore we must check for potential violation
11505 -- of such restriction.
11506
11507 if Is_Class_Wide_Type (Etype (P)) then
11508 Check_Restriction (No_Dispatching_Calls, N);
11509 end if;
996ae0b0
RK
11510 end case;
11511
11512 -- Normally the Freezing is done by Resolve but sometimes the Prefix
442ade9d 11513 -- is not resolved, in which case the freezing must be done now.
996ae0b0 11514
13fa2acb
AC
11515 -- For an elaboration check on a subprogram, we do not freeze its type.
11516 -- It may be declared in an unrelated scope, in particular in the case
11517 -- of a generic function whose type may remain unelaborated.
11518
11519 if Attr_Id = Attribute_Elaborated then
11520 null;
11521
11522 else
11523 Freeze_Expression (P);
11524 end if;
996ae0b0
RK
11525
11526 -- Finally perform static evaluation on the attribute reference
11527
dec6faf1 11528 Analyze_Dimension (N);
996ae0b0 11529 Eval_Attribute (N);
996ae0b0
RK
11530 end Resolve_Attribute;
11531
f7ea2603
RD
11532 ------------------------
11533 -- Set_Boolean_Result --
11534 ------------------------
11535
11536 procedure Set_Boolean_Result (N : Node_Id; B : Boolean) is
11537 Loc : constant Source_Ptr := Sloc (N);
f7ea2603
RD
11538 begin
11539 if B then
11540 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
11541 else
11542 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
11543 end if;
f7ea2603
RD
11544 end Set_Boolean_Result;
11545
edd63e9b
ES
11546 --------------------------------
11547 -- Stream_Attribute_Available --
11548 --------------------------------
11549
11550 function Stream_Attribute_Available
11551 (Typ : Entity_Id;
11552 Nam : TSS_Name_Type;
11553 Partial_View : Node_Id := Empty) return Boolean
11554 is
11555 Etyp : Entity_Id := Typ;
11556
edd63e9b
ES
11557 -- Start of processing for Stream_Attribute_Available
11558
11559 begin
11560 -- We need some comments in this body ???
11561
468c6c8a 11562 if Has_Stream_Attribute_Definition (Typ, Nam) then
edd63e9b
ES
11563 return True;
11564 end if;
11565
11566 if Is_Class_Wide_Type (Typ) then
11567 return not Is_Limited_Type (Typ)
11568 or else Stream_Attribute_Available (Etype (Typ), Nam);
11569 end if;
11570
11571 if Nam = TSS_Stream_Input
fea9e956 11572 and then Is_Abstract_Type (Typ)
edd63e9b
ES
11573 and then not Is_Class_Wide_Type (Typ)
11574 then
11575 return False;
11576 end if;
11577
11578 if not (Is_Limited_Type (Typ)
11579 or else (Present (Partial_View)
11580 and then Is_Limited_Type (Partial_View)))
11581 then
11582 return True;
11583 end if;
11584
65f01153
RD
11585 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
11586
11587 if Nam = TSS_Stream_Input
0791fbe9 11588 and then Ada_Version >= Ada_2005
65f01153
RD
11589 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
11590 then
11591 return True;
11592
11593 elsif Nam = TSS_Stream_Output
0791fbe9 11594 and then Ada_Version >= Ada_2005
65f01153
RD
11595 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
11596 then
11597 return True;
edd63e9b
ES
11598 end if;
11599
11600 -- Case of Read and Write: check for attribute definition clause that
11601 -- applies to an ancestor type.
11602
11603 while Etype (Etyp) /= Etyp loop
11604 Etyp := Etype (Etyp);
11605
468c6c8a 11606 if Has_Stream_Attribute_Definition (Etyp, Nam) then
edd63e9b
ES
11607 return True;
11608 end if;
11609 end loop;
11610
0791fbe9 11611 if Ada_Version < Ada_2005 then
edd63e9b
ES
11612
11613 -- In Ada 95 mode, also consider a non-visible definition
11614
11615 declare
11616 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
11617 begin
11618 return Btyp /= Typ
11619 and then Stream_Attribute_Available
11620 (Btyp, Nam, Partial_View => Typ);
11621 end;
11622 end if;
11623
11624 return False;
11625 end Stream_Attribute_Available;
11626
996ae0b0 11627end Sem_Attr;