]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_attr.adb
345fdb55eebf439e1ef48001f60011bfa45212e3
[gcc.git] / gcc / ada / sem_attr.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A T T R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
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- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
27
28 with Atree; use Atree;
29 with Casing; use Casing;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat;
35 with Exp_Dist; use Exp_Dist;
36 with Exp_Util; use Exp_Util;
37 with Expander; use Expander;
38 with Freeze; use Freeze;
39 with Gnatvsn; use Gnatvsn;
40 with Itypes; use Itypes;
41 with Lib; use Lib;
42 with Lib.Xref; use Lib.Xref;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sdefault; use Sdefault;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Elim; use Sem_Elim;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Res; use Sem_Res;
62 with Sem_Type; use Sem_Type;
63 with Sem_Util; use Sem_Util;
64 with Stand; use Stand;
65 with Sinfo; use Sinfo;
66 with Sinput; use Sinput;
67 with Stringt; use Stringt;
68 with Style;
69 with Stylesw; use Stylesw;
70 with Targparm; use Targparm;
71 with Ttypes; use Ttypes;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
75
76 package body Sem_Attr is
77
78 True_Value : constant Uint := Uint_1;
79 False_Value : constant Uint := Uint_0;
80 -- Synonyms to be used when these constants are used as Boolean values
81
82 Bad_Attribute : exception;
83 -- Exception raised if an error is detected during attribute processing,
84 -- used so that we can abandon the processing so we don't run into
85 -- trouble with cascaded errors.
86
87 -- The following array is the list of attributes defined in the Ada 83 RM
88 -- that are not included in Ada 95, but still get recognized in GNAT.
89
90 Attribute_83 : constant Attribute_Class_Array := Attribute_Class_Array'(
91 Attribute_Address |
92 Attribute_Aft |
93 Attribute_Alignment |
94 Attribute_Base |
95 Attribute_Callable |
96 Attribute_Constrained |
97 Attribute_Count |
98 Attribute_Delta |
99 Attribute_Digits |
100 Attribute_Emax |
101 Attribute_Epsilon |
102 Attribute_First |
103 Attribute_First_Bit |
104 Attribute_Fore |
105 Attribute_Image |
106 Attribute_Large |
107 Attribute_Last |
108 Attribute_Last_Bit |
109 Attribute_Leading_Part |
110 Attribute_Length |
111 Attribute_Machine_Emax |
112 Attribute_Machine_Emin |
113 Attribute_Machine_Mantissa |
114 Attribute_Machine_Overflows |
115 Attribute_Machine_Radix |
116 Attribute_Machine_Rounds |
117 Attribute_Mantissa |
118 Attribute_Pos |
119 Attribute_Position |
120 Attribute_Pred |
121 Attribute_Range |
122 Attribute_Safe_Emax |
123 Attribute_Safe_Large |
124 Attribute_Safe_Small |
125 Attribute_Size |
126 Attribute_Small |
127 Attribute_Storage_Size |
128 Attribute_Succ |
129 Attribute_Terminated |
130 Attribute_Val |
131 Attribute_Value |
132 Attribute_Width => True,
133 others => False);
134
135 -- The following array is the list of attributes defined in the Ada 2005
136 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
137 -- but in Ada 95 they are considered to be implementation defined.
138
139 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
140 Attribute_Machine_Rounding |
141 Attribute_Mod |
142 Attribute_Priority |
143 Attribute_Stream_Size |
144 Attribute_Wide_Wide_Width => True,
145 others => False);
146
147 -- The following array contains all attributes that imply a modification
148 -- of their prefixes or result in an access value. Such prefixes can be
149 -- considered as lvalues.
150
151 Attribute_Name_Implies_Lvalue_Prefix : constant Attribute_Class_Array :=
152 Attribute_Class_Array'(
153 Attribute_Access |
154 Attribute_Address |
155 Attribute_Input |
156 Attribute_Read |
157 Attribute_Unchecked_Access |
158 Attribute_Unrestricted_Access => True,
159 others => False);
160
161 -----------------------
162 -- Local_Subprograms --
163 -----------------------
164
165 procedure Eval_Attribute (N : Node_Id);
166 -- Performs compile time evaluation of attributes where possible, leaving
167 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
168 -- set, and replacing the node with a literal node if the value can be
169 -- computed at compile time. All static attribute references are folded,
170 -- as well as a number of cases of non-static attributes that can always
171 -- be computed at compile time (e.g. floating-point model attributes that
172 -- are applied to non-static subtypes). Of course in such cases, the
173 -- Is_Static_Expression flag will not be set on the resulting literal.
174 -- Note that the only required action of this procedure is to catch the
175 -- static expression cases as described in the RM. Folding of other cases
176 -- is done where convenient, but some additional non-static folding is in
177 -- N_Expand_Attribute_Reference in cases where this is more convenient.
178
179 function Is_Anonymous_Tagged_Base
180 (Anon : Entity_Id;
181 Typ : Entity_Id)
182 return Boolean;
183 -- For derived tagged types that constrain parent discriminants we build
184 -- an anonymous unconstrained base type. We need to recognize the relation
185 -- between the two when analyzing an access attribute for a constrained
186 -- component, before the full declaration for Typ has been analyzed, and
187 -- where therefore the prefix of the attribute does not match the enclosing
188 -- scope.
189
190 -----------------------
191 -- Analyze_Attribute --
192 -----------------------
193
194 procedure Analyze_Attribute (N : Node_Id) is
195 Loc : constant Source_Ptr := Sloc (N);
196 Aname : constant Name_Id := Attribute_Name (N);
197 P : constant Node_Id := Prefix (N);
198 Exprs : constant List_Id := Expressions (N);
199 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
200 E1 : Node_Id;
201 E2 : Node_Id;
202
203 P_Type : Entity_Id;
204 -- Type of prefix after analysis
205
206 P_Base_Type : Entity_Id;
207 -- Base type of prefix after analysis
208
209 -----------------------
210 -- Local Subprograms --
211 -----------------------
212
213 procedure Analyze_Access_Attribute;
214 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
215 -- Internally, Id distinguishes which of the three cases is involved.
216
217 procedure Bad_Attribute_For_Predicate;
218 -- Output error message for use of a predicate (First, Last, Range) not
219 -- allowed with a type that has predicates. If the type is a generic
220 -- actual, then the message is a warning, and we generate code to raise
221 -- program error with an appropriate reason. No error message is given
222 -- for internally generated uses of the attributes. This legality rule
223 -- only applies to scalar types.
224
225 procedure Check_Ada_2012_Attribute;
226 -- Check that we are in Ada 2012 mode for an Ada 2012 attribute, and
227 -- issue appropriate messages if not (and return to caller even in
228 -- the error case).
229
230 procedure Check_Array_Or_Scalar_Type;
231 -- Common procedure used by First, Last, Range attribute to check
232 -- that the prefix is a constrained array or scalar type, or a name
233 -- of an array object, and that an argument appears only if appropriate
234 -- (i.e. only in the array case).
235
236 procedure Check_Array_Type;
237 -- Common semantic checks for all array attributes. Checks that the
238 -- prefix is a constrained array type or the name of an array object.
239 -- The error message for non-arrays is specialized appropriately.
240
241 procedure Check_Asm_Attribute;
242 -- Common semantic checks for Asm_Input and Asm_Output attributes
243
244 procedure Check_Component;
245 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
246 -- Position. Checks prefix is an appropriate selected component.
247
248 procedure Check_Decimal_Fixed_Point_Type;
249 -- Check that prefix of attribute N is a decimal fixed-point type
250
251 procedure Check_Dereference;
252 -- If the prefix of attribute is an object of an access type, then
253 -- introduce an explicit dereference, and adjust P_Type accordingly.
254
255 procedure Check_Discrete_Type;
256 -- Verify that prefix of attribute N is a discrete type
257
258 procedure Check_E0;
259 -- Check that no attribute arguments are present
260
261 procedure Check_Either_E0_Or_E1;
262 -- Check that there are zero or one attribute arguments present
263
264 procedure Check_E1;
265 -- Check that exactly one attribute argument is present
266
267 procedure Check_E2;
268 -- Check that two attribute arguments are present
269
270 procedure Check_Enum_Image;
271 -- If the prefix type is an enumeration type, set all its literals
272 -- as referenced, since the image function could possibly end up
273 -- referencing any of the literals indirectly. Same for Enum_Val.
274 -- Set the flag only if the reference is in the main code unit. Same
275 -- restriction when resolving 'Value; otherwise an improperly set
276 -- reference when analyzing an inlined body will lose a proper warning
277 -- on a useless with_clause.
278
279 procedure Check_First_Last_Valid;
280 -- Perform all checks for First_Valid and Last_Valid attributes
281
282 procedure Check_Fixed_Point_Type;
283 -- Verify that prefix of attribute N is a fixed type
284
285 procedure Check_Fixed_Point_Type_0;
286 -- Verify that prefix of attribute N is a fixed type and that
287 -- no attribute expressions are present
288
289 procedure Check_Floating_Point_Type;
290 -- Verify that prefix of attribute N is a float type
291
292 procedure Check_Floating_Point_Type_0;
293 -- Verify that prefix of attribute N is a float type and that
294 -- no attribute expressions are present
295
296 procedure Check_Floating_Point_Type_1;
297 -- Verify that prefix of attribute N is a float type and that
298 -- exactly one attribute expression is present
299
300 procedure Check_Floating_Point_Type_2;
301 -- Verify that prefix of attribute N is a float type and that
302 -- two attribute expressions are present
303
304 procedure Legal_Formal_Attribute;
305 -- Common processing for attributes Definite and Has_Discriminants.
306 -- Checks that prefix is generic indefinite formal type.
307
308 procedure Check_SPARK_Restriction_On_Attribute;
309 -- Issue an error in formal mode because attribute N is allowed
310
311 procedure Check_Integer_Type;
312 -- Verify that prefix of attribute N is an integer type
313
314 procedure Check_Modular_Integer_Type;
315 -- Verify that prefix of attribute N is a modular integer type
316
317 procedure Check_Not_CPP_Type;
318 -- Check that P (the prefix of the attribute) is not an CPP type
319 -- for which no Ada predefined primitive is available.
320
321 procedure Check_Not_Incomplete_Type;
322 -- Check that P (the prefix of the attribute) is not an incomplete
323 -- type or a private type for which no full view has been given.
324
325 procedure Check_Object_Reference (P : Node_Id);
326 -- Check that P is an object reference
327
328 procedure Check_Program_Unit;
329 -- Verify that prefix of attribute N is a program unit
330
331 procedure Check_Real_Type;
332 -- Verify that prefix of attribute N is fixed or float type
333
334 procedure Check_Scalar_Type;
335 -- Verify that prefix of attribute N is a scalar type
336
337 procedure Check_Standard_Prefix;
338 -- Verify that prefix of attribute N is package Standard
339
340 procedure Check_Stream_Attribute (Nam : TSS_Name_Type);
341 -- Validity checking for stream attribute. Nam is the TSS name of the
342 -- corresponding possible defined attribute function (e.g. for the
343 -- Read attribute, Nam will be TSS_Stream_Read).
344
345 procedure Check_PolyORB_Attribute;
346 -- Validity checking for PolyORB/DSA attribute
347
348 procedure Check_Task_Prefix;
349 -- Verify that prefix of attribute N is a task or task type
350
351 procedure Check_Type;
352 -- Verify that the prefix of attribute N is a type
353
354 procedure Check_Unit_Name (Nod : Node_Id);
355 -- Check that Nod is of the form of a library unit name, i.e that
356 -- it is an identifier, or a selected component whose prefix is
357 -- itself of the form of a library unit name. Note that this is
358 -- quite different from Check_Program_Unit, since it only checks
359 -- the syntactic form of the name, not the semantic identity. This
360 -- is because it is used with attributes (Elab_Body, Elab_Spec,
361 -- UET_Address and Elaborated) which can refer to non-visible unit.
362
363 procedure Error_Attr (Msg : String; Error_Node : Node_Id);
364 pragma No_Return (Error_Attr);
365 procedure Error_Attr;
366 pragma No_Return (Error_Attr);
367 -- Posts error using Error_Msg_N at given node, sets type of attribute
368 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
369 -- semantic processing. The message typically contains a % insertion
370 -- character which is replaced by the attribute name. The call with
371 -- no arguments is used when the caller has already generated the
372 -- required error messages.
373
374 procedure Error_Attr_P (Msg : String);
375 pragma No_Return (Error_Attr);
376 -- Like Error_Attr, but error is posted at the start of the prefix
377
378 procedure Standard_Attribute (Val : Int);
379 -- Used to process attributes whose prefix is package Standard which
380 -- yield values of type Universal_Integer. The attribute reference
381 -- node is rewritten with an integer literal of the given value.
382
383 procedure Unexpected_Argument (En : Node_Id);
384 -- Signal unexpected attribute argument (En is the argument)
385
386 procedure Validate_Non_Static_Attribute_Function_Call;
387 -- Called when processing an attribute that is a function call to a
388 -- non-static function, i.e. an attribute function that either takes
389 -- non-scalar arguments or returns a non-scalar result. Verifies that
390 -- such a call does not appear in a preelaborable context.
391
392 ------------------------------
393 -- Analyze_Access_Attribute --
394 ------------------------------
395
396 procedure Analyze_Access_Attribute is
397 Acc_Type : Entity_Id;
398
399 Scop : Entity_Id;
400 Typ : Entity_Id;
401
402 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id;
403 -- Build an access-to-object type whose designated type is DT,
404 -- and whose Ekind is appropriate to the attribute type. The
405 -- type that is constructed is returned as the result.
406
407 procedure Build_Access_Subprogram_Type (P : Node_Id);
408 -- Build an access to subprogram whose designated type is the type of
409 -- the prefix. If prefix is overloaded, so is the node itself. The
410 -- result is stored in Acc_Type.
411
412 function OK_Self_Reference return Boolean;
413 -- An access reference whose prefix is a type can legally appear
414 -- within an aggregate, where it is obtained by expansion of
415 -- a defaulted aggregate. The enclosing aggregate that contains
416 -- the self-referenced is flagged so that the self-reference can
417 -- be expanded into a reference to the target object (see exp_aggr).
418
419 ------------------------------
420 -- Build_Access_Object_Type --
421 ------------------------------
422
423 function Build_Access_Object_Type (DT : Entity_Id) return Entity_Id is
424 Typ : constant Entity_Id :=
425 New_Internal_Entity
426 (E_Access_Attribute_Type, Current_Scope, Loc, 'A');
427 begin
428 Set_Etype (Typ, Typ);
429 Set_Is_Itype (Typ);
430 Set_Associated_Node_For_Itype (Typ, N);
431 Set_Directly_Designated_Type (Typ, DT);
432 return Typ;
433 end Build_Access_Object_Type;
434
435 ----------------------------------
436 -- Build_Access_Subprogram_Type --
437 ----------------------------------
438
439 procedure Build_Access_Subprogram_Type (P : Node_Id) is
440 Index : Interp_Index;
441 It : Interp;
442
443 procedure Check_Local_Access (E : Entity_Id);
444 -- Deal with possible access to local subprogram. If we have such
445 -- an access, we set a flag to kill all tracked values on any call
446 -- because this access value may be passed around, and any called
447 -- code might use it to access a local procedure which clobbers a
448 -- tracked value. If the scope is a loop or block, indicate that
449 -- value tracking is disabled for the enclosing subprogram.
450
451 function Get_Kind (E : Entity_Id) return Entity_Kind;
452 -- Distinguish between access to regular/protected subprograms
453
454 ------------------------
455 -- Check_Local_Access --
456 ------------------------
457
458 procedure Check_Local_Access (E : Entity_Id) is
459 begin
460 if not Is_Library_Level_Entity (E) then
461 Set_Suppress_Value_Tracking_On_Call (Current_Scope);
462 Set_Suppress_Value_Tracking_On_Call
463 (Nearest_Dynamic_Scope (Current_Scope));
464 end if;
465 end Check_Local_Access;
466
467 --------------
468 -- Get_Kind --
469 --------------
470
471 function Get_Kind (E : Entity_Id) return Entity_Kind is
472 begin
473 if Convention (E) = Convention_Protected then
474 return E_Access_Protected_Subprogram_Type;
475 else
476 return E_Access_Subprogram_Type;
477 end if;
478 end Get_Kind;
479
480 -- Start of processing for Build_Access_Subprogram_Type
481
482 begin
483 -- In the case of an access to subprogram, use the name of the
484 -- subprogram itself as the designated type. Type-checking in
485 -- this case compares the signatures of the designated types.
486
487 -- Note: This fragment of the tree is temporarily malformed
488 -- because the correct tree requires an E_Subprogram_Type entity
489 -- as the designated type. In most cases this designated type is
490 -- later overridden by the semantics with the type imposed by the
491 -- context during the resolution phase. In the specific case of
492 -- the expression Address!(Prim'Unrestricted_Access), used to
493 -- initialize slots of dispatch tables, this work will be done by
494 -- the expander (see Exp_Aggr).
495
496 -- The reason to temporarily add this kind of node to the tree
497 -- instead of a proper E_Subprogram_Type itype, is the following:
498 -- in case of errors found in the source file we report better
499 -- error messages. For example, instead of generating the
500 -- following error:
501
502 -- "expected access to subprogram with profile
503 -- defined at line X"
504
505 -- we currently generate:
506
507 -- "expected access to function Z defined at line X"
508
509 Set_Etype (N, Any_Type);
510
511 if not Is_Overloaded (P) then
512 Check_Local_Access (Entity (P));
513
514 if not Is_Intrinsic_Subprogram (Entity (P)) then
515 Acc_Type := Create_Itype (Get_Kind (Entity (P)), N);
516 Set_Is_Public (Acc_Type, False);
517 Set_Etype (Acc_Type, Acc_Type);
518 Set_Convention (Acc_Type, Convention (Entity (P)));
519 Set_Directly_Designated_Type (Acc_Type, Entity (P));
520 Set_Etype (N, Acc_Type);
521 Freeze_Before (N, Acc_Type);
522 end if;
523
524 else
525 Get_First_Interp (P, Index, It);
526 while Present (It.Nam) loop
527 Check_Local_Access (It.Nam);
528
529 if not Is_Intrinsic_Subprogram (It.Nam) then
530 Acc_Type := Create_Itype (Get_Kind (It.Nam), N);
531 Set_Is_Public (Acc_Type, False);
532 Set_Etype (Acc_Type, Acc_Type);
533 Set_Convention (Acc_Type, Convention (It.Nam));
534 Set_Directly_Designated_Type (Acc_Type, It.Nam);
535 Add_One_Interp (N, Acc_Type, Acc_Type);
536 Freeze_Before (N, Acc_Type);
537 end if;
538
539 Get_Next_Interp (Index, It);
540 end loop;
541 end if;
542
543 -- Cannot be applied to intrinsic. Looking at the tests above,
544 -- the only way Etype (N) can still be set to Any_Type is if
545 -- Is_Intrinsic_Subprogram was True for some referenced entity.
546
547 if Etype (N) = Any_Type then
548 Error_Attr_P ("prefix of % attribute cannot be intrinsic");
549 end if;
550 end Build_Access_Subprogram_Type;
551
552 ----------------------
553 -- OK_Self_Reference --
554 ----------------------
555
556 function OK_Self_Reference return Boolean is
557 Par : Node_Id;
558
559 begin
560 Par := Parent (N);
561 while Present (Par)
562 and then
563 (Nkind (Par) = N_Component_Association
564 or else Nkind (Par) in N_Subexpr)
565 loop
566 if Nkind_In (Par, N_Aggregate, N_Extension_Aggregate) then
567 if Etype (Par) = Typ then
568 Set_Has_Self_Reference (Par);
569 return True;
570 end if;
571 end if;
572
573 Par := Parent (Par);
574 end loop;
575
576 -- No enclosing aggregate, or not a self-reference
577
578 return False;
579 end OK_Self_Reference;
580
581 -- Start of processing for Analyze_Access_Attribute
582
583 begin
584 Check_SPARK_Restriction_On_Attribute;
585 Check_E0;
586
587 if Nkind (P) = N_Character_Literal then
588 Error_Attr_P
589 ("prefix of % attribute cannot be enumeration literal");
590 end if;
591
592 -- Case of access to subprogram
593
594 if Is_Entity_Name (P)
595 and then Is_Overloadable (Entity (P))
596 then
597 if Has_Pragma_Inline_Always (Entity (P)) then
598 Error_Attr_P
599 ("prefix of % attribute cannot be Inline_Always subprogram");
600 end if;
601
602 if Aname = Name_Unchecked_Access then
603 Error_Attr ("attribute% cannot be applied to a subprogram", P);
604 end if;
605
606 -- Issue an error if the prefix denotes an eliminated subprogram
607
608 Check_For_Eliminated_Subprogram (P, Entity (P));
609
610 -- Check for obsolescent subprogram reference
611
612 Check_Obsolescent_2005_Entity (Entity (P), P);
613
614 -- Build the appropriate subprogram type
615
616 Build_Access_Subprogram_Type (P);
617
618 -- For P'Access or P'Unrestricted_Access, where P is a nested
619 -- subprogram, we might be passing P to another subprogram (but we
620 -- don't check that here), which might call P. P could modify
621 -- local variables, so we need to kill current values. It is
622 -- important not to do this for library-level subprograms, because
623 -- Kill_Current_Values is very inefficient in the case of library
624 -- level packages with lots of tagged types.
625
626 if Is_Library_Level_Entity (Entity (Prefix (N))) then
627 null;
628
629 -- Do not kill values on nodes initializing dispatch tables
630 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
631 -- is currently generated by the expander only for this
632 -- purpose. Done to keep the quality of warnings currently
633 -- generated by the compiler (otherwise any declaration of
634 -- a tagged type cleans constant indications from its scope).
635
636 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
637 and then (Etype (Parent (N)) = RTE (RE_Prim_Ptr)
638 or else
639 Etype (Parent (N)) = RTE (RE_Size_Ptr))
640 and then Is_Dispatching_Operation
641 (Directly_Designated_Type (Etype (N)))
642 then
643 null;
644
645 else
646 Kill_Current_Values;
647 end if;
648
649 -- Treat as call for elaboration purposes and we are all
650 -- done. Suppress this treatment under debug flag.
651
652 if not Debug_Flag_Dot_UU then
653 Check_Elab_Call (N);
654 end if;
655
656 return;
657
658 -- Component is an operation of a protected type
659
660 elsif Nkind (P) = N_Selected_Component
661 and then Is_Overloadable (Entity (Selector_Name (P)))
662 then
663 if Ekind (Entity (Selector_Name (P))) = E_Entry then
664 Error_Attr_P ("prefix of % attribute must be subprogram");
665 end if;
666
667 Build_Access_Subprogram_Type (Selector_Name (P));
668 return;
669 end if;
670
671 -- Deal with incorrect reference to a type, but note that some
672 -- accesses are allowed: references to the current type instance,
673 -- or in Ada 2005 self-referential pointer in a default-initialized
674 -- aggregate.
675
676 if Is_Entity_Name (P) then
677 Typ := Entity (P);
678
679 -- The reference may appear in an aggregate that has been expanded
680 -- into a loop. Locate scope of type definition, if any.
681
682 Scop := Current_Scope;
683 while Ekind (Scop) = E_Loop loop
684 Scop := Scope (Scop);
685 end loop;
686
687 if Is_Type (Typ) then
688
689 -- OK if we are within the scope of a limited type
690 -- let's mark the component as having per object constraint
691
692 if Is_Anonymous_Tagged_Base (Scop, Typ) then
693 Typ := Scop;
694 Set_Entity (P, Typ);
695 Set_Etype (P, Typ);
696 end if;
697
698 if Typ = Scop then
699 declare
700 Q : Node_Id := Parent (N);
701
702 begin
703 while Present (Q)
704 and then Nkind (Q) /= N_Component_Declaration
705 loop
706 Q := Parent (Q);
707 end loop;
708
709 if Present (Q) then
710 Set_Has_Per_Object_Constraint
711 (Defining_Identifier (Q), True);
712 end if;
713 end;
714
715 if Nkind (P) = N_Expanded_Name then
716 Error_Msg_F
717 ("current instance prefix must be a direct name", P);
718 end if;
719
720 -- If a current instance attribute appears in a component
721 -- constraint it must appear alone; other contexts (spec-
722 -- expressions, within a task body) are not subject to this
723 -- restriction.
724
725 if not In_Spec_Expression
726 and then not Has_Completion (Scop)
727 and then not
728 Nkind_In (Parent (N), N_Discriminant_Association,
729 N_Index_Or_Discriminant_Constraint)
730 then
731 Error_Msg_N
732 ("current instance attribute must appear alone", N);
733 end if;
734
735 if Is_CPP_Class (Root_Type (Typ)) then
736 Error_Msg_N
737 ("?current instance unsupported for derivations of "
738 & "'C'P'P types", N);
739 end if;
740
741 -- OK if we are in initialization procedure for the type
742 -- in question, in which case the reference to the type
743 -- is rewritten as a reference to the current object.
744
745 elsif Ekind (Scop) = E_Procedure
746 and then Is_Init_Proc (Scop)
747 and then Etype (First_Formal (Scop)) = Typ
748 then
749 Rewrite (N,
750 Make_Attribute_Reference (Loc,
751 Prefix => Make_Identifier (Loc, Name_uInit),
752 Attribute_Name => Name_Unrestricted_Access));
753 Analyze (N);
754 return;
755
756 -- OK if a task type, this test needs sharpening up ???
757
758 elsif Is_Task_Type (Typ) then
759 null;
760
761 -- OK if self-reference in an aggregate in Ada 2005, and
762 -- the reference comes from a copied default expression.
763
764 -- Note that we check legality of self-reference even if the
765 -- expression comes from source, e.g. when a single component
766 -- association in an aggregate has a box association.
767
768 elsif Ada_Version >= Ada_2005
769 and then OK_Self_Reference
770 then
771 null;
772
773 -- OK if reference to current instance of a protected object
774
775 elsif Is_Protected_Self_Reference (P) then
776 null;
777
778 -- Otherwise we have an error case
779
780 else
781 Error_Attr ("% attribute cannot be applied to type", P);
782 return;
783 end if;
784 end if;
785 end if;
786
787 -- If we fall through, we have a normal access to object case.
788 -- Unrestricted_Access is legal wherever an allocator would be
789 -- legal, so its Etype is set to E_Allocator. The expected type
790 -- of the other attributes is a general access type, and therefore
791 -- we label them with E_Access_Attribute_Type.
792
793 if not Is_Overloaded (P) then
794 Acc_Type := Build_Access_Object_Type (P_Type);
795 Set_Etype (N, Acc_Type);
796 else
797 declare
798 Index : Interp_Index;
799 It : Interp;
800 begin
801 Set_Etype (N, Any_Type);
802 Get_First_Interp (P, Index, It);
803 while Present (It.Typ) loop
804 Acc_Type := Build_Access_Object_Type (It.Typ);
805 Add_One_Interp (N, Acc_Type, Acc_Type);
806 Get_Next_Interp (Index, It);
807 end loop;
808 end;
809 end if;
810
811 -- Special cases when we can find a prefix that is an entity name
812
813 declare
814 PP : Node_Id;
815 Ent : Entity_Id;
816
817 begin
818 PP := P;
819 loop
820 if Is_Entity_Name (PP) then
821 Ent := Entity (PP);
822
823 -- If we have an access to an object, and the attribute
824 -- comes from source, then set the object as potentially
825 -- source modified. We do this because the resulting access
826 -- pointer can be used to modify the variable, and we might
827 -- not detect this, leading to some junk warnings.
828
829 Set_Never_Set_In_Source (Ent, False);
830
831 -- Mark entity as address taken, and kill current values
832
833 Set_Address_Taken (Ent);
834 Kill_Current_Values (Ent);
835 exit;
836
837 elsif Nkind_In (PP, N_Selected_Component,
838 N_Indexed_Component)
839 then
840 PP := Prefix (PP);
841
842 else
843 exit;
844 end if;
845 end loop;
846 end;
847
848 -- Check for aliased view unless unrestricted case. We allow a
849 -- nonaliased prefix when within an instance because the prefix may
850 -- have been a tagged formal object, which is defined to be aliased
851 -- even when the actual might not be (other instance cases will have
852 -- been caught in the generic). Similarly, within an inlined body we
853 -- know that the attribute is legal in the original subprogram, and
854 -- therefore legal in the expansion.
855
856 if Aname /= Name_Unrestricted_Access
857 and then not Is_Aliased_View (P)
858 and then not In_Instance
859 and then not In_Inlined_Body
860 then
861 Error_Attr_P ("prefix of % attribute must be aliased");
862 Check_No_Implicit_Aliasing (P);
863 end if;
864 end Analyze_Access_Attribute;
865
866 ---------------------------------
867 -- Bad_Attribute_For_Predicate --
868 ---------------------------------
869
870 procedure Bad_Attribute_For_Predicate is
871 begin
872 if Is_Scalar_Type (P_Type)
873 and then Comes_From_Source (N)
874 then
875 Error_Msg_Name_1 := Aname;
876 Bad_Predicated_Subtype_Use
877 ("type& has predicates, attribute % not allowed", N, P_Type);
878 end if;
879 end Bad_Attribute_For_Predicate;
880
881 ------------------------------
882 -- Check_Ada_2012_Attribute --
883 ------------------------------
884
885 procedure Check_Ada_2012_Attribute is
886 begin
887 if Ada_Version < Ada_2012 then
888 Error_Msg_Name_1 := Aname;
889 Error_Msg_N
890 ("attribute % is an Ada 2012 feature", N);
891 Error_Msg_N
892 ("\unit must be compiled with -gnat2012 switch", N);
893 end if;
894 end Check_Ada_2012_Attribute;
895
896 --------------------------------
897 -- Check_Array_Or_Scalar_Type --
898 --------------------------------
899
900 procedure Check_Array_Or_Scalar_Type is
901 Index : Entity_Id;
902
903 D : Int;
904 -- Dimension number for array attributes
905
906 begin
907 -- Case of string literal or string literal subtype. These cases
908 -- cannot arise from legal Ada code, but the expander is allowed
909 -- to generate them. They require special handling because string
910 -- literal subtypes do not have standard bounds (the whole idea
911 -- of these subtypes is to avoid having to generate the bounds)
912
913 if Ekind (P_Type) = E_String_Literal_Subtype then
914 Set_Etype (N, Etype (First_Index (P_Base_Type)));
915 return;
916
917 -- Scalar types
918
919 elsif Is_Scalar_Type (P_Type) then
920 Check_Type;
921
922 if Present (E1) then
923 Error_Attr ("invalid argument in % attribute", E1);
924 else
925 Set_Etype (N, P_Base_Type);
926 return;
927 end if;
928
929 -- The following is a special test to allow 'First to apply to
930 -- private scalar types if the attribute comes from generated
931 -- code. This occurs in the case of Normalize_Scalars code.
932
933 elsif Is_Private_Type (P_Type)
934 and then Present (Full_View (P_Type))
935 and then Is_Scalar_Type (Full_View (P_Type))
936 and then not Comes_From_Source (N)
937 then
938 Set_Etype (N, Implementation_Base_Type (P_Type));
939
940 -- Array types other than string literal subtypes handled above
941
942 else
943 Check_Array_Type;
944
945 -- We know prefix is an array type, or the name of an array
946 -- object, and that the expression, if present, is static
947 -- and within the range of the dimensions of the type.
948
949 pragma Assert (Is_Array_Type (P_Type));
950 Index := First_Index (P_Base_Type);
951
952 if No (E1) then
953
954 -- First dimension assumed
955
956 Set_Etype (N, Base_Type (Etype (Index)));
957
958 else
959 D := UI_To_Int (Intval (E1));
960
961 for J in 1 .. D - 1 loop
962 Next_Index (Index);
963 end loop;
964
965 Set_Etype (N, Base_Type (Etype (Index)));
966 Set_Etype (E1, Standard_Integer);
967 end if;
968 end if;
969 end Check_Array_Or_Scalar_Type;
970
971 ----------------------
972 -- Check_Array_Type --
973 ----------------------
974
975 procedure Check_Array_Type is
976 D : Int;
977 -- Dimension number for array attributes
978
979 begin
980 -- If the type is a string literal type, then this must be generated
981 -- internally, and no further check is required on its legality.
982
983 if Ekind (P_Type) = E_String_Literal_Subtype then
984 return;
985
986 -- If the type is a composite, it is an illegal aggregate, no point
987 -- in going on.
988
989 elsif P_Type = Any_Composite then
990 raise Bad_Attribute;
991 end if;
992
993 -- Normal case of array type or subtype
994
995 Check_Either_E0_Or_E1;
996 Check_Dereference;
997
998 if Is_Array_Type (P_Type) then
999 if not Is_Constrained (P_Type)
1000 and then Is_Entity_Name (P)
1001 and then Is_Type (Entity (P))
1002 then
1003 -- Note: we do not call Error_Attr here, since we prefer to
1004 -- continue, using the relevant index type of the array,
1005 -- even though it is unconstrained. This gives better error
1006 -- recovery behavior.
1007
1008 Error_Msg_Name_1 := Aname;
1009 Error_Msg_F
1010 ("prefix for % attribute must be constrained array", P);
1011 end if;
1012
1013 D := Number_Dimensions (P_Type);
1014
1015 else
1016 if Is_Private_Type (P_Type) then
1017 Error_Attr_P ("prefix for % attribute may not be private type");
1018
1019 elsif Is_Access_Type (P_Type)
1020 and then Is_Array_Type (Designated_Type (P_Type))
1021 and then Is_Entity_Name (P)
1022 and then Is_Type (Entity (P))
1023 then
1024 Error_Attr_P ("prefix of % attribute cannot be access type");
1025
1026 elsif Attr_Id = Attribute_First
1027 or else
1028 Attr_Id = Attribute_Last
1029 then
1030 Error_Attr ("invalid prefix for % attribute", P);
1031
1032 else
1033 Error_Attr_P ("prefix for % attribute must be array");
1034 end if;
1035 end if;
1036
1037 if Present (E1) then
1038 Resolve (E1, Any_Integer);
1039 Set_Etype (E1, Standard_Integer);
1040
1041 if not Is_Static_Expression (E1)
1042 or else Raises_Constraint_Error (E1)
1043 then
1044 Flag_Non_Static_Expr
1045 ("expression for dimension must be static!", E1);
1046 Error_Attr;
1047
1048 elsif UI_To_Int (Expr_Value (E1)) > D
1049 or else UI_To_Int (Expr_Value (E1)) < 1
1050 then
1051 Error_Attr ("invalid dimension number for array type", E1);
1052 end if;
1053 end if;
1054
1055 if (Style_Check and Style_Check_Array_Attribute_Index)
1056 and then Comes_From_Source (N)
1057 then
1058 Style.Check_Array_Attribute_Index (N, E1, D);
1059 end if;
1060 end Check_Array_Type;
1061
1062 -------------------------
1063 -- Check_Asm_Attribute --
1064 -------------------------
1065
1066 procedure Check_Asm_Attribute is
1067 begin
1068 Check_Type;
1069 Check_E2;
1070
1071 -- Check first argument is static string expression
1072
1073 Analyze_And_Resolve (E1, Standard_String);
1074
1075 if Etype (E1) = Any_Type then
1076 return;
1077
1078 elsif not Is_OK_Static_Expression (E1) then
1079 Flag_Non_Static_Expr
1080 ("constraint argument must be static string expression!", E1);
1081 Error_Attr;
1082 end if;
1083
1084 -- Check second argument is right type
1085
1086 Analyze_And_Resolve (E2, Entity (P));
1087
1088 -- Note: that is all we need to do, we don't need to check
1089 -- that it appears in a correct context. The Ada type system
1090 -- will do that for us.
1091
1092 end Check_Asm_Attribute;
1093
1094 ---------------------
1095 -- Check_Component --
1096 ---------------------
1097
1098 procedure Check_Component is
1099 begin
1100 Check_E0;
1101
1102 if Nkind (P) /= N_Selected_Component
1103 or else
1104 (Ekind (Entity (Selector_Name (P))) /= E_Component
1105 and then
1106 Ekind (Entity (Selector_Name (P))) /= E_Discriminant)
1107 then
1108 Error_Attr_P ("prefix for % attribute must be selected component");
1109 end if;
1110 end Check_Component;
1111
1112 ------------------------------------
1113 -- Check_Decimal_Fixed_Point_Type --
1114 ------------------------------------
1115
1116 procedure Check_Decimal_Fixed_Point_Type is
1117 begin
1118 Check_Type;
1119
1120 if not Is_Decimal_Fixed_Point_Type (P_Type) then
1121 Error_Attr_P ("prefix of % attribute must be decimal type");
1122 end if;
1123 end Check_Decimal_Fixed_Point_Type;
1124
1125 -----------------------
1126 -- Check_Dereference --
1127 -----------------------
1128
1129 procedure Check_Dereference is
1130 begin
1131
1132 -- Case of a subtype mark
1133
1134 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
1135 return;
1136 end if;
1137
1138 -- Case of an expression
1139
1140 Resolve (P);
1141
1142 if Is_Access_Type (P_Type) then
1143
1144 -- If there is an implicit dereference, then we must freeze the
1145 -- designated type of the access type, since the type of the
1146 -- referenced array is this type (see AI95-00106).
1147
1148 -- As done elsewhere, freezing must not happen when pre-analyzing
1149 -- a pre- or postcondition or a default value for an object or for
1150 -- a formal parameter.
1151
1152 if not In_Spec_Expression then
1153 Freeze_Before (N, Designated_Type (P_Type));
1154 end if;
1155
1156 Rewrite (P,
1157 Make_Explicit_Dereference (Sloc (P),
1158 Prefix => Relocate_Node (P)));
1159
1160 Analyze_And_Resolve (P);
1161 P_Type := Etype (P);
1162
1163 if P_Type = Any_Type then
1164 raise Bad_Attribute;
1165 end if;
1166
1167 P_Base_Type := Base_Type (P_Type);
1168 end if;
1169 end Check_Dereference;
1170
1171 -------------------------
1172 -- Check_Discrete_Type --
1173 -------------------------
1174
1175 procedure Check_Discrete_Type is
1176 begin
1177 Check_Type;
1178
1179 if not Is_Discrete_Type (P_Type) then
1180 Error_Attr_P ("prefix of % attribute must be discrete type");
1181 end if;
1182 end Check_Discrete_Type;
1183
1184 --------------
1185 -- Check_E0 --
1186 --------------
1187
1188 procedure Check_E0 is
1189 begin
1190 if Present (E1) then
1191 Unexpected_Argument (E1);
1192 end if;
1193 end Check_E0;
1194
1195 --------------
1196 -- Check_E1 --
1197 --------------
1198
1199 procedure Check_E1 is
1200 begin
1201 Check_Either_E0_Or_E1;
1202
1203 if No (E1) then
1204
1205 -- Special-case attributes that are functions and that appear as
1206 -- the prefix of another attribute. Error is posted on parent.
1207
1208 if Nkind (Parent (N)) = N_Attribute_Reference
1209 and then (Attribute_Name (Parent (N)) = Name_Address
1210 or else
1211 Attribute_Name (Parent (N)) = Name_Code_Address
1212 or else
1213 Attribute_Name (Parent (N)) = Name_Access)
1214 then
1215 Error_Msg_Name_1 := Attribute_Name (Parent (N));
1216 Error_Msg_N ("illegal prefix for % attribute", Parent (N));
1217 Set_Etype (Parent (N), Any_Type);
1218 Set_Entity (Parent (N), Any_Type);
1219 raise Bad_Attribute;
1220
1221 else
1222 Error_Attr ("missing argument for % attribute", N);
1223 end if;
1224 end if;
1225 end Check_E1;
1226
1227 --------------
1228 -- Check_E2 --
1229 --------------
1230
1231 procedure Check_E2 is
1232 begin
1233 if No (E1) then
1234 Error_Attr ("missing arguments for % attribute (2 required)", N);
1235 elsif No (E2) then
1236 Error_Attr ("missing argument for % attribute (2 required)", N);
1237 end if;
1238 end Check_E2;
1239
1240 ---------------------------
1241 -- Check_Either_E0_Or_E1 --
1242 ---------------------------
1243
1244 procedure Check_Either_E0_Or_E1 is
1245 begin
1246 if Present (E2) then
1247 Unexpected_Argument (E2);
1248 end if;
1249 end Check_Either_E0_Or_E1;
1250
1251 ----------------------
1252 -- Check_Enum_Image --
1253 ----------------------
1254
1255 procedure Check_Enum_Image is
1256 Lit : Entity_Id;
1257
1258 begin
1259 -- When an enumeration type appears in an attribute reference, all
1260 -- literals of the type are marked as referenced. This must only be
1261 -- done if the attribute reference appears in the current source.
1262 -- Otherwise the information on references may differ between a
1263 -- normal compilation and one that performs inlining.
1264
1265 if Is_Enumeration_Type (P_Base_Type)
1266 and then In_Extended_Main_Code_Unit (N)
1267 then
1268 Lit := First_Literal (P_Base_Type);
1269 while Present (Lit) loop
1270 Set_Referenced (Lit);
1271 Next_Literal (Lit);
1272 end loop;
1273 end if;
1274 end Check_Enum_Image;
1275
1276 ----------------------------
1277 -- Check_First_Last_Valid --
1278 ----------------------------
1279
1280 procedure Check_First_Last_Valid is
1281 begin
1282 Check_Ada_2012_Attribute;
1283 Check_Discrete_Type;
1284
1285 -- Freeze the subtype now, so that the following test for predicates
1286 -- works (we set the predicates stuff up at freeze time)
1287
1288 Insert_Actions (N, Freeze_Entity (P_Type, P));
1289
1290 -- Now test for dynamic predicate
1291
1292 if Has_Predicates (P_Type)
1293 and then No (Static_Predicate (P_Type))
1294 then
1295 Error_Attr_P
1296 ("prefix of % attribute may not have dynamic predicate");
1297 end if;
1298
1299 -- Check non-static subtype
1300
1301 if not Is_Static_Subtype (P_Type) then
1302 Error_Attr_P ("prefix of % attribute must be a static subtype");
1303 end if;
1304
1305 -- Test case for no values
1306
1307 if Expr_Value (Type_Low_Bound (P_Type)) >
1308 Expr_Value (Type_High_Bound (P_Type))
1309 or else (Has_Predicates (P_Type)
1310 and then Is_Empty_List (Static_Predicate (P_Type)))
1311 then
1312 Error_Attr_P
1313 ("prefix of % attribute must be subtype with "
1314 & "at least one value");
1315 end if;
1316 end Check_First_Last_Valid;
1317
1318 ----------------------------
1319 -- Check_Fixed_Point_Type --
1320 ----------------------------
1321
1322 procedure Check_Fixed_Point_Type is
1323 begin
1324 Check_Type;
1325
1326 if not Is_Fixed_Point_Type (P_Type) then
1327 Error_Attr_P ("prefix of % attribute must be fixed point type");
1328 end if;
1329 end Check_Fixed_Point_Type;
1330
1331 ------------------------------
1332 -- Check_Fixed_Point_Type_0 --
1333 ------------------------------
1334
1335 procedure Check_Fixed_Point_Type_0 is
1336 begin
1337 Check_Fixed_Point_Type;
1338 Check_E0;
1339 end Check_Fixed_Point_Type_0;
1340
1341 -------------------------------
1342 -- Check_Floating_Point_Type --
1343 -------------------------------
1344
1345 procedure Check_Floating_Point_Type is
1346 begin
1347 Check_Type;
1348
1349 if not Is_Floating_Point_Type (P_Type) then
1350 Error_Attr_P ("prefix of % attribute must be float type");
1351 end if;
1352 end Check_Floating_Point_Type;
1353
1354 ---------------------------------
1355 -- Check_Floating_Point_Type_0 --
1356 ---------------------------------
1357
1358 procedure Check_Floating_Point_Type_0 is
1359 begin
1360 Check_Floating_Point_Type;
1361 Check_E0;
1362 end Check_Floating_Point_Type_0;
1363
1364 ---------------------------------
1365 -- Check_Floating_Point_Type_1 --
1366 ---------------------------------
1367
1368 procedure Check_Floating_Point_Type_1 is
1369 begin
1370 Check_Floating_Point_Type;
1371 Check_E1;
1372 end Check_Floating_Point_Type_1;
1373
1374 ---------------------------------
1375 -- Check_Floating_Point_Type_2 --
1376 ---------------------------------
1377
1378 procedure Check_Floating_Point_Type_2 is
1379 begin
1380 Check_Floating_Point_Type;
1381 Check_E2;
1382 end Check_Floating_Point_Type_2;
1383
1384 ------------------------
1385 -- Check_Integer_Type --
1386 ------------------------
1387
1388 procedure Check_Integer_Type is
1389 begin
1390 Check_Type;
1391
1392 if not Is_Integer_Type (P_Type) then
1393 Error_Attr_P ("prefix of % attribute must be integer type");
1394 end if;
1395 end Check_Integer_Type;
1396
1397 --------------------------------
1398 -- Check_Modular_Integer_Type --
1399 --------------------------------
1400
1401 procedure Check_Modular_Integer_Type is
1402 begin
1403 Check_Type;
1404
1405 if not Is_Modular_Integer_Type (P_Type) then
1406 Error_Attr_P
1407 ("prefix of % attribute must be modular integer type");
1408 end if;
1409 end Check_Modular_Integer_Type;
1410
1411 ------------------------
1412 -- Check_Not_CPP_Type --
1413 ------------------------
1414
1415 procedure Check_Not_CPP_Type is
1416 begin
1417 if Is_Tagged_Type (Etype (P))
1418 and then Convention (Etype (P)) = Convention_CPP
1419 and then Is_CPP_Class (Root_Type (Etype (P)))
1420 then
1421 Error_Attr_P
1422 ("invalid use of % attribute with 'C'P'P tagged type");
1423 end if;
1424 end Check_Not_CPP_Type;
1425
1426 -------------------------------
1427 -- Check_Not_Incomplete_Type --
1428 -------------------------------
1429
1430 procedure Check_Not_Incomplete_Type is
1431 E : Entity_Id;
1432 Typ : Entity_Id;
1433
1434 begin
1435 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
1436 -- dereference we have to check wrong uses of incomplete types
1437 -- (other wrong uses are checked at their freezing point).
1438
1439 -- Example 1: Limited-with
1440
1441 -- limited with Pkg;
1442 -- package P is
1443 -- type Acc is access Pkg.T;
1444 -- X : Acc;
1445 -- S : Integer := X.all'Size; -- ERROR
1446 -- end P;
1447
1448 -- Example 2: Tagged incomplete
1449
1450 -- type T is tagged;
1451 -- type Acc is access all T;
1452 -- X : Acc;
1453 -- S : constant Integer := X.all'Size; -- ERROR
1454 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
1455
1456 if Ada_Version >= Ada_2005
1457 and then Nkind (P) = N_Explicit_Dereference
1458 then
1459 E := P;
1460 while Nkind (E) = N_Explicit_Dereference loop
1461 E := Prefix (E);
1462 end loop;
1463
1464 Typ := Etype (E);
1465
1466 if From_With_Type (Typ) then
1467 Error_Attr_P
1468 ("prefix of % attribute cannot be an incomplete type");
1469
1470 else
1471 if Is_Access_Type (Typ) then
1472 Typ := Directly_Designated_Type (Typ);
1473 end if;
1474
1475 if Is_Class_Wide_Type (Typ) then
1476 Typ := Root_Type (Typ);
1477 end if;
1478
1479 -- A legal use of a shadow entity occurs only when the unit
1480 -- where the non-limited view resides is imported via a regular
1481 -- with clause in the current body. Such references to shadow
1482 -- entities may occur in subprogram formals.
1483
1484 if Is_Incomplete_Type (Typ)
1485 and then From_With_Type (Typ)
1486 and then Present (Non_Limited_View (Typ))
1487 and then Is_Legal_Shadow_Entity_In_Body (Typ)
1488 then
1489 Typ := Non_Limited_View (Typ);
1490 end if;
1491
1492 if Ekind (Typ) = E_Incomplete_Type
1493 and then No (Full_View (Typ))
1494 then
1495 Error_Attr_P
1496 ("prefix of % attribute cannot be an incomplete type");
1497 end if;
1498 end if;
1499 end if;
1500
1501 if not Is_Entity_Name (P)
1502 or else not Is_Type (Entity (P))
1503 or else In_Spec_Expression
1504 then
1505 return;
1506 else
1507 Check_Fully_Declared (P_Type, P);
1508 end if;
1509 end Check_Not_Incomplete_Type;
1510
1511 ----------------------------
1512 -- Check_Object_Reference --
1513 ----------------------------
1514
1515 procedure Check_Object_Reference (P : Node_Id) is
1516 Rtyp : Entity_Id;
1517
1518 begin
1519 -- If we need an object, and we have a prefix that is the name of
1520 -- a function entity, convert it into a function call.
1521
1522 if Is_Entity_Name (P)
1523 and then Ekind (Entity (P)) = E_Function
1524 then
1525 Rtyp := Etype (Entity (P));
1526
1527 Rewrite (P,
1528 Make_Function_Call (Sloc (P),
1529 Name => Relocate_Node (P)));
1530
1531 Analyze_And_Resolve (P, Rtyp);
1532
1533 -- Otherwise we must have an object reference
1534
1535 elsif not Is_Object_Reference (P) then
1536 Error_Attr_P ("prefix of % attribute must be object");
1537 end if;
1538 end Check_Object_Reference;
1539
1540 ----------------------------
1541 -- Check_PolyORB_Attribute --
1542 ----------------------------
1543
1544 procedure Check_PolyORB_Attribute is
1545 begin
1546 Validate_Non_Static_Attribute_Function_Call;
1547
1548 Check_Type;
1549 Check_Not_CPP_Type;
1550
1551 if Get_PCS_Name /= Name_PolyORB_DSA then
1552 Error_Attr
1553 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N);
1554 end if;
1555 end Check_PolyORB_Attribute;
1556
1557 ------------------------
1558 -- Check_Program_Unit --
1559 ------------------------
1560
1561 procedure Check_Program_Unit is
1562 begin
1563 if Is_Entity_Name (P) then
1564 declare
1565 K : constant Entity_Kind := Ekind (Entity (P));
1566 T : constant Entity_Id := Etype (Entity (P));
1567
1568 begin
1569 if K in Subprogram_Kind
1570 or else K in Task_Kind
1571 or else K in Protected_Kind
1572 or else K = E_Package
1573 or else K in Generic_Unit_Kind
1574 or else (K = E_Variable
1575 and then
1576 (Is_Task_Type (T)
1577 or else
1578 Is_Protected_Type (T)))
1579 then
1580 return;
1581 end if;
1582 end;
1583 end if;
1584
1585 Error_Attr_P ("prefix of % attribute must be program unit");
1586 end Check_Program_Unit;
1587
1588 ---------------------
1589 -- Check_Real_Type --
1590 ---------------------
1591
1592 procedure Check_Real_Type is
1593 begin
1594 Check_Type;
1595
1596 if not Is_Real_Type (P_Type) then
1597 Error_Attr_P ("prefix of % attribute must be real type");
1598 end if;
1599 end Check_Real_Type;
1600
1601 -----------------------
1602 -- Check_Scalar_Type --
1603 -----------------------
1604
1605 procedure Check_Scalar_Type is
1606 begin
1607 Check_Type;
1608
1609 if not Is_Scalar_Type (P_Type) then
1610 Error_Attr_P ("prefix of % attribute must be scalar type");
1611 end if;
1612 end Check_Scalar_Type;
1613
1614 ------------------------------------------
1615 -- Check_SPARK_Restriction_On_Attribute --
1616 ------------------------------------------
1617
1618 procedure Check_SPARK_Restriction_On_Attribute is
1619 begin
1620 Error_Msg_Name_1 := Aname;
1621 Check_SPARK_Restriction ("attribute % is not allowed", P);
1622 end Check_SPARK_Restriction_On_Attribute;
1623
1624 ---------------------------
1625 -- Check_Standard_Prefix --
1626 ---------------------------
1627
1628 procedure Check_Standard_Prefix is
1629 begin
1630 Check_E0;
1631
1632 if Nkind (P) /= N_Identifier
1633 or else Chars (P) /= Name_Standard
1634 then
1635 Error_Attr ("only allowed prefix for % attribute is Standard", P);
1636 end if;
1637 end Check_Standard_Prefix;
1638
1639 ----------------------------
1640 -- Check_Stream_Attribute --
1641 ----------------------------
1642
1643 procedure Check_Stream_Attribute (Nam : TSS_Name_Type) is
1644 Etyp : Entity_Id;
1645 Btyp : Entity_Id;
1646
1647 In_Shared_Var_Procs : Boolean;
1648 -- True when compiling the body of System.Shared_Storage.
1649 -- Shared_Var_Procs. For this runtime package (always compiled in
1650 -- GNAT mode), we allow stream attributes references for limited
1651 -- types for the case where shared passive objects are implemented
1652 -- using stream attributes, which is the default in GNAT's persistent
1653 -- storage implementation.
1654
1655 begin
1656 Validate_Non_Static_Attribute_Function_Call;
1657
1658 -- With the exception of 'Input, Stream attributes are procedures,
1659 -- and can only appear at the position of procedure calls. We check
1660 -- for this here, before they are rewritten, to give a more precise
1661 -- diagnostic.
1662
1663 if Nam = TSS_Stream_Input then
1664 null;
1665
1666 elsif Is_List_Member (N)
1667 and then not Nkind_In (Parent (N), N_Procedure_Call_Statement,
1668 N_Aggregate)
1669 then
1670 null;
1671
1672 else
1673 Error_Attr
1674 ("invalid context for attribute%, which is a procedure", N);
1675 end if;
1676
1677 Check_Type;
1678 Btyp := Implementation_Base_Type (P_Type);
1679
1680 -- Stream attributes not allowed on limited types unless the
1681 -- attribute reference was generated by the expander (in which
1682 -- case the underlying type will be used, as described in Sinfo),
1683 -- or the attribute was specified explicitly for the type itself
1684 -- or one of its ancestors (taking visibility rules into account if
1685 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
1686 -- (with no visibility restriction).
1687
1688 declare
1689 Gen_Body : constant Node_Id := Enclosing_Generic_Body (N);
1690 begin
1691 if Present (Gen_Body) then
1692 In_Shared_Var_Procs :=
1693 Is_RTE (Corresponding_Spec (Gen_Body), RE_Shared_Var_Procs);
1694 else
1695 In_Shared_Var_Procs := False;
1696 end if;
1697 end;
1698
1699 if (Comes_From_Source (N)
1700 and then not (In_Shared_Var_Procs or In_Instance))
1701 and then not Stream_Attribute_Available (P_Type, Nam)
1702 and then not Has_Rep_Pragma (Btyp, Name_Stream_Convert)
1703 then
1704 Error_Msg_Name_1 := Aname;
1705
1706 if Is_Limited_Type (P_Type) then
1707 Error_Msg_NE
1708 ("limited type& has no% attribute", P, P_Type);
1709 Explain_Limited_Type (P_Type, P);
1710 else
1711 Error_Msg_NE
1712 ("attribute% for type& is not available", P, P_Type);
1713 end if;
1714 end if;
1715
1716 -- Check restriction violations
1717
1718 -- First check the No_Streams restriction, which prohibits the use
1719 -- of explicit stream attributes in the source program. We do not
1720 -- prevent the occurrence of stream attributes in generated code,
1721 -- for instance those generated implicitly for dispatching purposes.
1722
1723 if Comes_From_Source (N) then
1724 Check_Restriction (No_Streams, P);
1725 end if;
1726
1727 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
1728 -- it is illegal to use a predefined elementary type stream attribute
1729 -- either by itself, or more importantly as part of the attribute
1730 -- subprogram for a composite type.
1731
1732 if Restriction_Active (No_Default_Stream_Attributes) then
1733 declare
1734 T : Entity_Id;
1735
1736 begin
1737 if Nam = TSS_Stream_Input
1738 or else
1739 Nam = TSS_Stream_Read
1740 then
1741 T :=
1742 Type_Without_Stream_Operation (P_Type, TSS_Stream_Read);
1743 else
1744 T :=
1745 Type_Without_Stream_Operation (P_Type, TSS_Stream_Write);
1746 end if;
1747
1748 if Present (T) then
1749 Check_Restriction (No_Default_Stream_Attributes, N);
1750
1751 Error_Msg_NE
1752 ("missing user-defined Stream Read or Write for type&",
1753 N, T);
1754 if not Is_Elementary_Type (P_Type) then
1755 Error_Msg_NE
1756 ("\which is a component of type&", N, P_Type);
1757 end if;
1758 end if;
1759 end;
1760 end if;
1761
1762 -- Check special case of Exception_Id and Exception_Occurrence which
1763 -- are not allowed for restriction No_Exception_Registration.
1764
1765 if Restriction_Check_Required (No_Exception_Registration)
1766 and then (Is_RTE (P_Type, RE_Exception_Id)
1767 or else
1768 Is_RTE (P_Type, RE_Exception_Occurrence))
1769 then
1770 Check_Restriction (No_Exception_Registration, P);
1771 end if;
1772
1773 -- Here we must check that the first argument is an access type
1774 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
1775
1776 Analyze_And_Resolve (E1);
1777 Etyp := Etype (E1);
1778
1779 -- Note: the double call to Root_Type here is needed because the
1780 -- root type of a class-wide type is the corresponding type (e.g.
1781 -- X for X'Class, and we really want to go to the root.)
1782
1783 if not Is_Access_Type (Etyp)
1784 or else Root_Type (Root_Type (Designated_Type (Etyp))) /=
1785 RTE (RE_Root_Stream_Type)
1786 then
1787 Error_Attr
1788 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1);
1789 end if;
1790
1791 -- Check that the second argument is of the right type if there is
1792 -- one (the Input attribute has only one argument so this is skipped)
1793
1794 if Present (E2) then
1795 Analyze (E2);
1796
1797 if Nam = TSS_Stream_Read
1798 and then not Is_OK_Variable_For_Out_Formal (E2)
1799 then
1800 Error_Attr
1801 ("second argument of % attribute must be a variable", E2);
1802 end if;
1803
1804 Resolve (E2, P_Type);
1805 end if;
1806
1807 Check_Not_CPP_Type;
1808 end Check_Stream_Attribute;
1809
1810 -----------------------
1811 -- Check_Task_Prefix --
1812 -----------------------
1813
1814 procedure Check_Task_Prefix is
1815 begin
1816 Analyze (P);
1817
1818 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
1819 -- task interface class-wide types.
1820
1821 if Is_Task_Type (Etype (P))
1822 or else (Is_Access_Type (Etype (P))
1823 and then Is_Task_Type (Designated_Type (Etype (P))))
1824 or else (Ada_Version >= Ada_2005
1825 and then Ekind (Etype (P)) = E_Class_Wide_Type
1826 and then Is_Interface (Etype (P))
1827 and then Is_Task_Interface (Etype (P)))
1828 then
1829 Resolve (P);
1830
1831 else
1832 if Ada_Version >= Ada_2005 then
1833 Error_Attr_P
1834 ("prefix of % attribute must be a task or a task " &
1835 "interface class-wide object");
1836
1837 else
1838 Error_Attr_P ("prefix of % attribute must be a task");
1839 end if;
1840 end if;
1841 end Check_Task_Prefix;
1842
1843 ----------------
1844 -- Check_Type --
1845 ----------------
1846
1847 -- The possibilities are an entity name denoting a type, or an
1848 -- attribute reference that denotes a type (Base or Class). If
1849 -- the type is incomplete, replace it with its full view.
1850
1851 procedure Check_Type is
1852 begin
1853 if not Is_Entity_Name (P)
1854 or else not Is_Type (Entity (P))
1855 then
1856 Error_Attr_P ("prefix of % attribute must be a type");
1857
1858 elsif Is_Protected_Self_Reference (P) then
1859 Error_Attr_P
1860 ("prefix of % attribute denotes current instance "
1861 & "(RM 9.4(21/2))");
1862
1863 elsif Ekind (Entity (P)) = E_Incomplete_Type
1864 and then Present (Full_View (Entity (P)))
1865 then
1866 P_Type := Full_View (Entity (P));
1867 Set_Entity (P, P_Type);
1868 end if;
1869 end Check_Type;
1870
1871 ---------------------
1872 -- Check_Unit_Name --
1873 ---------------------
1874
1875 procedure Check_Unit_Name (Nod : Node_Id) is
1876 begin
1877 if Nkind (Nod) = N_Identifier then
1878 return;
1879
1880 elsif Nkind_In (Nod, N_Selected_Component, N_Expanded_Name) then
1881 Check_Unit_Name (Prefix (Nod));
1882
1883 if Nkind (Selector_Name (Nod)) = N_Identifier then
1884 return;
1885 end if;
1886 end if;
1887
1888 Error_Attr ("argument for % attribute must be unit name", P);
1889 end Check_Unit_Name;
1890
1891 ----------------
1892 -- Error_Attr --
1893 ----------------
1894
1895 procedure Error_Attr is
1896 begin
1897 Set_Etype (N, Any_Type);
1898 Set_Entity (N, Any_Type);
1899 raise Bad_Attribute;
1900 end Error_Attr;
1901
1902 procedure Error_Attr (Msg : String; Error_Node : Node_Id) is
1903 begin
1904 Error_Msg_Name_1 := Aname;
1905 Error_Msg_N (Msg, Error_Node);
1906 Error_Attr;
1907 end Error_Attr;
1908
1909 ------------------
1910 -- Error_Attr_P --
1911 ------------------
1912
1913 procedure Error_Attr_P (Msg : String) is
1914 begin
1915 Error_Msg_Name_1 := Aname;
1916 Error_Msg_F (Msg, P);
1917 Error_Attr;
1918 end Error_Attr_P;
1919
1920 ----------------------------
1921 -- Legal_Formal_Attribute --
1922 ----------------------------
1923
1924 procedure Legal_Formal_Attribute is
1925 begin
1926 Check_E0;
1927
1928 if not Is_Entity_Name (P)
1929 or else not Is_Type (Entity (P))
1930 then
1931 Error_Attr_P ("prefix of % attribute must be generic type");
1932
1933 elsif Is_Generic_Actual_Type (Entity (P))
1934 or else In_Instance
1935 or else In_Inlined_Body
1936 then
1937 null;
1938
1939 elsif Is_Generic_Type (Entity (P)) then
1940 if not Is_Indefinite_Subtype (Entity (P)) then
1941 Error_Attr_P
1942 ("prefix of % attribute must be indefinite generic type");
1943 end if;
1944
1945 else
1946 Error_Attr_P
1947 ("prefix of % attribute must be indefinite generic type");
1948 end if;
1949
1950 Set_Etype (N, Standard_Boolean);
1951 end Legal_Formal_Attribute;
1952
1953 ------------------------
1954 -- Standard_Attribute --
1955 ------------------------
1956
1957 procedure Standard_Attribute (Val : Int) is
1958 begin
1959 Check_Standard_Prefix;
1960 Rewrite (N, Make_Integer_Literal (Loc, Val));
1961 Analyze (N);
1962 end Standard_Attribute;
1963
1964 -------------------------
1965 -- Unexpected Argument --
1966 -------------------------
1967
1968 procedure Unexpected_Argument (En : Node_Id) is
1969 begin
1970 Error_Attr ("unexpected argument for % attribute", En);
1971 end Unexpected_Argument;
1972
1973 -------------------------------------------------
1974 -- Validate_Non_Static_Attribute_Function_Call --
1975 -------------------------------------------------
1976
1977 -- This function should be moved to Sem_Dist ???
1978
1979 procedure Validate_Non_Static_Attribute_Function_Call is
1980 begin
1981 if In_Preelaborated_Unit
1982 and then not In_Subprogram_Or_Concurrent_Unit
1983 then
1984 Flag_Non_Static_Expr
1985 ("non-static function call in preelaborated unit!", N);
1986 end if;
1987 end Validate_Non_Static_Attribute_Function_Call;
1988
1989 -- Start of processing for Analyze_Attribute
1990
1991 begin
1992 -- Immediate return if unrecognized attribute (already diagnosed
1993 -- by parser, so there is nothing more that we need to do)
1994
1995 if not Is_Attribute_Name (Aname) then
1996 raise Bad_Attribute;
1997 end if;
1998
1999 -- Deal with Ada 83 issues
2000
2001 if Comes_From_Source (N) then
2002 if not Attribute_83 (Attr_Id) then
2003 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2004 Error_Msg_Name_1 := Aname;
2005 Error_Msg_N ("(Ada 83) attribute% is not standard?", N);
2006 end if;
2007
2008 if Attribute_Impl_Def (Attr_Id) then
2009 Check_Restriction (No_Implementation_Attributes, N);
2010 end if;
2011 end if;
2012 end if;
2013
2014 -- Deal with Ada 2005 attributes that are
2015
2016 if Attribute_05 (Attr_Id) and then Ada_Version < Ada_2005 then
2017 Check_Restriction (No_Implementation_Attributes, N);
2018 end if;
2019
2020 -- Remote access to subprogram type access attribute reference needs
2021 -- unanalyzed copy for tree transformation. The analyzed copy is used
2022 -- for its semantic information (whether prefix is a remote subprogram
2023 -- name), the unanalyzed copy is used to construct new subtree rooted
2024 -- with N_Aggregate which represents a fat pointer aggregate.
2025
2026 if Aname = Name_Access then
2027 Discard_Node (Copy_Separate_Tree (N));
2028 end if;
2029
2030 -- Analyze prefix and exit if error in analysis. If the prefix is an
2031 -- incomplete type, use full view if available. Note that there are
2032 -- some attributes for which we do not analyze the prefix, since the
2033 -- prefix is not a normal name, or else needs special handling.
2034
2035 if Aname /= Name_Elab_Body
2036 and then
2037 Aname /= Name_Elab_Spec
2038 and then
2039 Aname /= Name_Elab_Subp_Body
2040 and then
2041 Aname /= Name_UET_Address
2042 and then
2043 Aname /= Name_Enabled
2044 and then
2045 Aname /= Name_Old
2046 then
2047 Analyze (P);
2048 P_Type := Etype (P);
2049
2050 if Is_Entity_Name (P)
2051 and then Present (Entity (P))
2052 and then Is_Type (Entity (P))
2053 then
2054 if Ekind (Entity (P)) = E_Incomplete_Type then
2055 P_Type := Get_Full_View (P_Type);
2056 Set_Entity (P, P_Type);
2057 Set_Etype (P, P_Type);
2058
2059 elsif Entity (P) = Current_Scope
2060 and then Is_Record_Type (Entity (P))
2061 then
2062 -- Use of current instance within the type. Verify that if the
2063 -- attribute appears within a constraint, it yields an access
2064 -- type, other uses are illegal.
2065
2066 declare
2067 Par : Node_Id;
2068
2069 begin
2070 Par := Parent (N);
2071 while Present (Par)
2072 and then Nkind (Parent (Par)) /= N_Component_Definition
2073 loop
2074 Par := Parent (Par);
2075 end loop;
2076
2077 if Present (Par)
2078 and then Nkind (Par) = N_Subtype_Indication
2079 then
2080 if Attr_Id /= Attribute_Access
2081 and then Attr_Id /= Attribute_Unchecked_Access
2082 and then Attr_Id /= Attribute_Unrestricted_Access
2083 then
2084 Error_Msg_N
2085 ("in a constraint the current instance can only"
2086 & " be used with an access attribute", N);
2087 end if;
2088 end if;
2089 end;
2090 end if;
2091 end if;
2092
2093 if P_Type = Any_Type then
2094 raise Bad_Attribute;
2095 end if;
2096
2097 P_Base_Type := Base_Type (P_Type);
2098 end if;
2099
2100 -- Analyze expressions that may be present, exiting if an error occurs
2101
2102 if No (Exprs) then
2103 E1 := Empty;
2104 E2 := Empty;
2105
2106 else
2107 E1 := First (Exprs);
2108 Analyze (E1);
2109
2110 -- Check for missing/bad expression (result of previous error)
2111
2112 if No (E1) or else Etype (E1) = Any_Type then
2113 raise Bad_Attribute;
2114 end if;
2115
2116 E2 := Next (E1);
2117
2118 if Present (E2) then
2119 Analyze (E2);
2120
2121 if Etype (E2) = Any_Type then
2122 raise Bad_Attribute;
2123 end if;
2124
2125 if Present (Next (E2)) then
2126 Unexpected_Argument (Next (E2));
2127 end if;
2128 end if;
2129 end if;
2130
2131 -- Ada 2005 (AI-345): Ensure that the compiler gives exactly the current
2132 -- output compiling in Ada 95 mode for the case of ambiguous prefixes.
2133
2134 if Ada_Version < Ada_2005
2135 and then Is_Overloaded (P)
2136 and then Aname /= Name_Access
2137 and then Aname /= Name_Address
2138 and then Aname /= Name_Code_Address
2139 and then Aname /= Name_Count
2140 and then Aname /= Name_Result
2141 and then Aname /= Name_Unchecked_Access
2142 then
2143 Error_Attr ("ambiguous prefix for % attribute", P);
2144
2145 elsif Ada_Version >= Ada_2005
2146 and then Is_Overloaded (P)
2147 and then Aname /= Name_Access
2148 and then Aname /= Name_Address
2149 and then Aname /= Name_Code_Address
2150 and then Aname /= Name_Result
2151 and then Aname /= Name_Unchecked_Access
2152 then
2153 -- Ada 2005 (AI-345): Since protected and task types have primitive
2154 -- entry wrappers, the attributes Count, Caller and AST_Entry require
2155 -- a context check
2156
2157 if Ada_Version >= Ada_2005
2158 and then (Aname = Name_Count
2159 or else Aname = Name_Caller
2160 or else Aname = Name_AST_Entry)
2161 then
2162 declare
2163 Count : Natural := 0;
2164 I : Interp_Index;
2165 It : Interp;
2166
2167 begin
2168 Get_First_Interp (P, I, It);
2169 while Present (It.Nam) loop
2170 if Comes_From_Source (It.Nam) then
2171 Count := Count + 1;
2172 else
2173 Remove_Interp (I);
2174 end if;
2175
2176 Get_Next_Interp (I, It);
2177 end loop;
2178
2179 if Count > 1 then
2180 Error_Attr ("ambiguous prefix for % attribute", P);
2181 else
2182 Set_Is_Overloaded (P, False);
2183 end if;
2184 end;
2185
2186 else
2187 Error_Attr ("ambiguous prefix for % attribute", P);
2188 end if;
2189 end if;
2190
2191 -- In SPARK, attributes of private types are only allowed if the full
2192 -- type declaration is visible.
2193
2194 if Is_Entity_Name (P)
2195 and then Present (Entity (P)) -- needed in some cases
2196 and then Is_Type (Entity (P))
2197 and then Is_Private_Type (P_Type)
2198 and then not In_Open_Scopes (Scope (P_Type))
2199 and then not In_Spec_Expression
2200 then
2201 Check_SPARK_Restriction ("invisible attribute of type", N);
2202 end if;
2203
2204 -- Remaining processing depends on attribute
2205
2206 case Attr_Id is
2207
2208 -- Attributes related to Ada 2012 iterators. Attribute specifications
2209 -- exist for these, but they cannot be queried.
2210
2211 when Attribute_Constant_Indexing |
2212 Attribute_Default_Iterator |
2213 Attribute_Implicit_Dereference |
2214 Attribute_Iterator_Element |
2215 Attribute_Variable_Indexing =>
2216 Error_Msg_N ("illegal attribute", N);
2217
2218 ------------------
2219 -- Abort_Signal --
2220 ------------------
2221
2222 when Attribute_Abort_Signal =>
2223 Check_Standard_Prefix;
2224 Rewrite (N, New_Reference_To (Stand.Abort_Signal, Loc));
2225 Analyze (N);
2226
2227 ------------
2228 -- Access --
2229 ------------
2230
2231 when Attribute_Access =>
2232 Analyze_Access_Attribute;
2233
2234 -------------
2235 -- Address --
2236 -------------
2237
2238 when Attribute_Address =>
2239 Check_E0;
2240
2241 -- Check for some junk cases, where we have to allow the address
2242 -- attribute but it does not make much sense, so at least for now
2243 -- just replace with Null_Address.
2244
2245 -- We also do this if the prefix is a reference to the AST_Entry
2246 -- attribute. If expansion is active, the attribute will be
2247 -- replaced by a function call, and address will work fine and
2248 -- get the proper value, but if expansion is not active, then
2249 -- the check here allows proper semantic analysis of the reference.
2250
2251 -- An Address attribute created by expansion is legal even when it
2252 -- applies to other entity-denoting expressions.
2253
2254 if Is_Protected_Self_Reference (P) then
2255
2256 -- Address attribute on a protected object self reference is legal
2257
2258 null;
2259
2260 elsif Is_Entity_Name (P) then
2261 declare
2262 Ent : constant Entity_Id := Entity (P);
2263
2264 begin
2265 if Is_Subprogram (Ent) then
2266 Set_Address_Taken (Ent);
2267 Kill_Current_Values (Ent);
2268
2269 -- An Address attribute is accepted when generated by the
2270 -- compiler for dispatching operation, and an error is
2271 -- issued once the subprogram is frozen (to avoid confusing
2272 -- errors about implicit uses of Address in the dispatch
2273 -- table initialization).
2274
2275 if Has_Pragma_Inline_Always (Entity (P))
2276 and then Comes_From_Source (P)
2277 then
2278 Error_Attr_P
2279 ("prefix of % attribute cannot be Inline_Always" &
2280 " subprogram");
2281
2282 -- It is illegal to apply 'Address to an intrinsic
2283 -- subprogram. This is now formalized in AI05-0095.
2284 -- In an instance, an attempt to obtain 'Address of an
2285 -- intrinsic subprogram (e.g the renaming of a predefined
2286 -- operator that is an actual) raises Program_Error.
2287
2288 elsif Convention (Ent) = Convention_Intrinsic then
2289 if In_Instance then
2290 Rewrite (N,
2291 Make_Raise_Program_Error (Loc,
2292 Reason => PE_Address_Of_Intrinsic));
2293
2294 else
2295 Error_Msg_N
2296 ("cannot take Address of intrinsic subprogram", N);
2297 end if;
2298
2299 -- Issue an error if prefix denotes an eliminated subprogram
2300
2301 else
2302 Check_For_Eliminated_Subprogram (P, Ent);
2303 end if;
2304
2305 elsif Is_Object (Ent)
2306 or else Ekind (Ent) = E_Label
2307 then
2308 Set_Address_Taken (Ent);
2309
2310 -- Deal with No_Implicit_Aliasing restriction
2311
2312 if Restriction_Check_Required (No_Implicit_Aliasing) then
2313 if not Is_Aliased_View (P) then
2314 Check_Restriction (No_Implicit_Aliasing, P);
2315 else
2316 Check_No_Implicit_Aliasing (P);
2317 end if;
2318 end if;
2319
2320 -- If we have an address of an object, and the attribute
2321 -- comes from source, then set the object as potentially
2322 -- source modified. We do this because the resulting address
2323 -- can potentially be used to modify the variable and we
2324 -- might not detect this, leading to some junk warnings.
2325
2326 Set_Never_Set_In_Source (Ent, False);
2327
2328 elsif (Is_Concurrent_Type (Etype (Ent))
2329 and then Etype (Ent) = Base_Type (Ent))
2330 or else Ekind (Ent) = E_Package
2331 or else Is_Generic_Unit (Ent)
2332 then
2333 Rewrite (N,
2334 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2335
2336 else
2337 Error_Attr ("invalid prefix for % attribute", P);
2338 end if;
2339 end;
2340
2341 elsif Nkind (P) = N_Attribute_Reference
2342 and then Attribute_Name (P) = Name_AST_Entry
2343 then
2344 Rewrite (N,
2345 New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
2346
2347 elsif Is_Object_Reference (P) then
2348 null;
2349
2350 elsif Nkind (P) = N_Selected_Component
2351 and then Is_Subprogram (Entity (Selector_Name (P)))
2352 then
2353 null;
2354
2355 -- What exactly are we allowing here ??? and is this properly
2356 -- documented in the sinfo documentation for this node ???
2357
2358 elsif not Comes_From_Source (N) then
2359 null;
2360
2361 else
2362 Error_Attr ("invalid prefix for % attribute", P);
2363 end if;
2364
2365 Set_Etype (N, RTE (RE_Address));
2366
2367 ------------------
2368 -- Address_Size --
2369 ------------------
2370
2371 when Attribute_Address_Size =>
2372 Standard_Attribute (System_Address_Size);
2373
2374 --------------
2375 -- Adjacent --
2376 --------------
2377
2378 when Attribute_Adjacent =>
2379 Check_Floating_Point_Type_2;
2380 Set_Etype (N, P_Base_Type);
2381 Resolve (E1, P_Base_Type);
2382 Resolve (E2, P_Base_Type);
2383
2384 ---------
2385 -- Aft --
2386 ---------
2387
2388 when Attribute_Aft =>
2389 Check_Fixed_Point_Type_0;
2390 Set_Etype (N, Universal_Integer);
2391
2392 ---------------
2393 -- Alignment --
2394 ---------------
2395
2396 when Attribute_Alignment =>
2397
2398 -- Don't we need more checking here, cf Size ???
2399
2400 Check_E0;
2401 Check_Not_Incomplete_Type;
2402 Check_Not_CPP_Type;
2403 Set_Etype (N, Universal_Integer);
2404
2405 ---------------
2406 -- Asm_Input --
2407 ---------------
2408
2409 when Attribute_Asm_Input =>
2410 Check_Asm_Attribute;
2411
2412 -- The back-end may need to take the address of E2
2413
2414 if Is_Entity_Name (E2) then
2415 Set_Address_Taken (Entity (E2));
2416 end if;
2417
2418 Set_Etype (N, RTE (RE_Asm_Input_Operand));
2419
2420 ----------------
2421 -- Asm_Output --
2422 ----------------
2423
2424 when Attribute_Asm_Output =>
2425 Check_Asm_Attribute;
2426
2427 if Etype (E2) = Any_Type then
2428 return;
2429
2430 elsif Aname = Name_Asm_Output then
2431 if not Is_Variable (E2) then
2432 Error_Attr
2433 ("second argument for Asm_Output is not variable", E2);
2434 end if;
2435 end if;
2436
2437 Note_Possible_Modification (E2, Sure => True);
2438
2439 -- The back-end may need to take the address of E2
2440
2441 if Is_Entity_Name (E2) then
2442 Set_Address_Taken (Entity (E2));
2443 end if;
2444
2445 Set_Etype (N, RTE (RE_Asm_Output_Operand));
2446
2447 ---------------
2448 -- AST_Entry --
2449 ---------------
2450
2451 when Attribute_AST_Entry => AST_Entry : declare
2452 Ent : Entity_Id;
2453 Pref : Node_Id;
2454 Ptyp : Entity_Id;
2455
2456 Indexed : Boolean;
2457 -- Indicates if entry family index is present. Note the coding
2458 -- here handles the entry family case, but in fact it cannot be
2459 -- executed currently, because pragma AST_Entry does not permit
2460 -- the specification of an entry family.
2461
2462 procedure Bad_AST_Entry;
2463 -- Signal a bad AST_Entry pragma
2464
2465 function OK_Entry (E : Entity_Id) return Boolean;
2466 -- Checks that E is of an appropriate entity kind for an entry
2467 -- (i.e. E_Entry if Index is False, or E_Entry_Family if Index
2468 -- is set True for the entry family case). In the True case,
2469 -- makes sure that Is_AST_Entry is set on the entry.
2470
2471 -------------------
2472 -- Bad_AST_Entry --
2473 -------------------
2474
2475 procedure Bad_AST_Entry is
2476 begin
2477 Error_Attr_P ("prefix for % attribute must be task entry");
2478 end Bad_AST_Entry;
2479
2480 --------------
2481 -- OK_Entry --
2482 --------------
2483
2484 function OK_Entry (E : Entity_Id) return Boolean is
2485 Result : Boolean;
2486
2487 begin
2488 if Indexed then
2489 Result := (Ekind (E) = E_Entry_Family);
2490 else
2491 Result := (Ekind (E) = E_Entry);
2492 end if;
2493
2494 if Result then
2495 if not Is_AST_Entry (E) then
2496 Error_Msg_Name_2 := Aname;
2497 Error_Attr ("% attribute requires previous % pragma", P);
2498 end if;
2499 end if;
2500
2501 return Result;
2502 end OK_Entry;
2503
2504 -- Start of processing for AST_Entry
2505
2506 begin
2507 Check_VMS (N);
2508 Check_E0;
2509
2510 -- Deal with entry family case
2511
2512 if Nkind (P) = N_Indexed_Component then
2513 Pref := Prefix (P);
2514 Indexed := True;
2515 else
2516 Pref := P;
2517 Indexed := False;
2518 end if;
2519
2520 Ptyp := Etype (Pref);
2521
2522 if Ptyp = Any_Type or else Error_Posted (Pref) then
2523 return;
2524 end if;
2525
2526 -- If the prefix is a selected component whose prefix is of an
2527 -- access type, then introduce an explicit dereference.
2528 -- ??? Could we reuse Check_Dereference here?
2529
2530 if Nkind (Pref) = N_Selected_Component
2531 and then Is_Access_Type (Ptyp)
2532 then
2533 Rewrite (Pref,
2534 Make_Explicit_Dereference (Sloc (Pref),
2535 Relocate_Node (Pref)));
2536 Analyze_And_Resolve (Pref, Designated_Type (Ptyp));
2537 end if;
2538
2539 -- Prefix can be of the form a.b, where a is a task object
2540 -- and b is one of the entries of the corresponding task type.
2541
2542 if Nkind (Pref) = N_Selected_Component
2543 and then OK_Entry (Entity (Selector_Name (Pref)))
2544 and then Is_Object_Reference (Prefix (Pref))
2545 and then Is_Task_Type (Etype (Prefix (Pref)))
2546 then
2547 null;
2548
2549 -- Otherwise the prefix must be an entry of a containing task,
2550 -- or of a variable of the enclosing task type.
2551
2552 else
2553 if Nkind_In (Pref, N_Identifier, N_Expanded_Name) then
2554 Ent := Entity (Pref);
2555
2556 if not OK_Entry (Ent)
2557 or else not In_Open_Scopes (Scope (Ent))
2558 then
2559 Bad_AST_Entry;
2560 end if;
2561
2562 else
2563 Bad_AST_Entry;
2564 end if;
2565 end if;
2566
2567 Set_Etype (N, RTE (RE_AST_Handler));
2568 end AST_Entry;
2569
2570 ----------
2571 -- Base --
2572 ----------
2573
2574 -- Note: when the base attribute appears in the context of a subtype
2575 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
2576 -- the following circuit.
2577
2578 when Attribute_Base => Base : declare
2579 Typ : Entity_Id;
2580
2581 begin
2582 Check_E0;
2583 Find_Type (P);
2584 Typ := Entity (P);
2585
2586 if Ada_Version >= Ada_95
2587 and then not Is_Scalar_Type (Typ)
2588 and then not Is_Generic_Type (Typ)
2589 then
2590 Error_Attr_P ("prefix of Base attribute must be scalar type");
2591
2592 elsif Sloc (Typ) = Standard_Location
2593 and then Base_Type (Typ) = Typ
2594 and then Warn_On_Redundant_Constructs
2595 then
2596 Error_Msg_NE -- CODEFIX
2597 ("?redundant attribute, & is its own base type", N, Typ);
2598 end if;
2599
2600 if Nkind (Parent (N)) /= N_Attribute_Reference then
2601 Error_Msg_Name_1 := Aname;
2602 Check_SPARK_Restriction
2603 ("attribute% is only allowed as prefix of another attribute", P);
2604 end if;
2605
2606 Set_Etype (N, Base_Type (Entity (P)));
2607 Set_Entity (N, Base_Type (Entity (P)));
2608 Rewrite (N, New_Reference_To (Entity (N), Loc));
2609 Analyze (N);
2610 end Base;
2611
2612 ---------
2613 -- Bit --
2614 ---------
2615
2616 when Attribute_Bit => Bit :
2617 begin
2618 Check_E0;
2619
2620 if not Is_Object_Reference (P) then
2621 Error_Attr_P ("prefix for % attribute must be object");
2622
2623 -- What about the access object cases ???
2624
2625 else
2626 null;
2627 end if;
2628
2629 Set_Etype (N, Universal_Integer);
2630 end Bit;
2631
2632 ---------------
2633 -- Bit_Order --
2634 ---------------
2635
2636 when Attribute_Bit_Order => Bit_Order :
2637 begin
2638 Check_E0;
2639 Check_Type;
2640
2641 if not Is_Record_Type (P_Type) then
2642 Error_Attr_P ("prefix of % attribute must be record type");
2643 end if;
2644
2645 if Bytes_Big_Endian xor Reverse_Bit_Order (P_Type) then
2646 Rewrite (N,
2647 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
2648 else
2649 Rewrite (N,
2650 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
2651 end if;
2652
2653 Set_Etype (N, RTE (RE_Bit_Order));
2654 Resolve (N);
2655
2656 -- Reset incorrect indication of staticness
2657
2658 Set_Is_Static_Expression (N, False);
2659 end Bit_Order;
2660
2661 ------------------
2662 -- Bit_Position --
2663 ------------------
2664
2665 -- Note: in generated code, we can have a Bit_Position attribute
2666 -- applied to a (naked) record component (i.e. the prefix is an
2667 -- identifier that references an E_Component or E_Discriminant
2668 -- entity directly, and this is interpreted as expected by Gigi.
2669 -- The following code will not tolerate such usage, but when the
2670 -- expander creates this special case, it marks it as analyzed
2671 -- immediately and sets an appropriate type.
2672
2673 when Attribute_Bit_Position =>
2674 if Comes_From_Source (N) then
2675 Check_Component;
2676 end if;
2677
2678 Set_Etype (N, Universal_Integer);
2679
2680 ------------------
2681 -- Body_Version --
2682 ------------------
2683
2684 when Attribute_Body_Version =>
2685 Check_E0;
2686 Check_Program_Unit;
2687 Set_Etype (N, RTE (RE_Version_String));
2688
2689 --------------
2690 -- Callable --
2691 --------------
2692
2693 when Attribute_Callable =>
2694 Check_E0;
2695 Set_Etype (N, Standard_Boolean);
2696 Check_Task_Prefix;
2697
2698 ------------
2699 -- Caller --
2700 ------------
2701
2702 when Attribute_Caller => Caller : declare
2703 Ent : Entity_Id;
2704 S : Entity_Id;
2705
2706 begin
2707 Check_E0;
2708
2709 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2710 Ent := Entity (P);
2711
2712 if not Is_Entry (Ent) then
2713 Error_Attr ("invalid entry name", N);
2714 end if;
2715
2716 else
2717 Error_Attr ("invalid entry name", N);
2718 return;
2719 end if;
2720
2721 for J in reverse 0 .. Scope_Stack.Last loop
2722 S := Scope_Stack.Table (J).Entity;
2723
2724 if S = Scope (Ent) then
2725 Error_Attr ("Caller must appear in matching accept or body", N);
2726 elsif S = Ent then
2727 exit;
2728 end if;
2729 end loop;
2730
2731 Set_Etype (N, RTE (RO_AT_Task_Id));
2732 end Caller;
2733
2734 -------------
2735 -- Ceiling --
2736 -------------
2737
2738 when Attribute_Ceiling =>
2739 Check_Floating_Point_Type_1;
2740 Set_Etype (N, P_Base_Type);
2741 Resolve (E1, P_Base_Type);
2742
2743 -----------
2744 -- Class --
2745 -----------
2746
2747 when Attribute_Class =>
2748 Check_Restriction (No_Dispatch, N);
2749 Check_E0;
2750 Find_Type (N);
2751
2752 -- Applying Class to untagged incomplete type is obsolescent in Ada
2753 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
2754 -- this flag gets set by Find_Type in this situation.
2755
2756 if Restriction_Check_Required (No_Obsolescent_Features)
2757 and then Ada_Version >= Ada_2005
2758 and then Ekind (P_Type) = E_Incomplete_Type
2759 then
2760 declare
2761 DN : constant Node_Id := Declaration_Node (P_Type);
2762 begin
2763 if Nkind (DN) = N_Incomplete_Type_Declaration
2764 and then not Tagged_Present (DN)
2765 then
2766 Check_Restriction (No_Obsolescent_Features, P);
2767 end if;
2768 end;
2769 end if;
2770
2771 ------------------
2772 -- Code_Address --
2773 ------------------
2774
2775 when Attribute_Code_Address =>
2776 Check_E0;
2777
2778 if Nkind (P) = N_Attribute_Reference
2779 and then (Attribute_Name (P) = Name_Elab_Body
2780 or else
2781 Attribute_Name (P) = Name_Elab_Spec)
2782 then
2783 null;
2784
2785 elsif not Is_Entity_Name (P)
2786 or else (Ekind (Entity (P)) /= E_Function
2787 and then
2788 Ekind (Entity (P)) /= E_Procedure)
2789 then
2790 Error_Attr ("invalid prefix for % attribute", P);
2791 Set_Address_Taken (Entity (P));
2792
2793 -- Issue an error if the prefix denotes an eliminated subprogram
2794
2795 else
2796 Check_For_Eliminated_Subprogram (P, Entity (P));
2797 end if;
2798
2799 Set_Etype (N, RTE (RE_Address));
2800
2801 ----------------------
2802 -- Compiler_Version --
2803 ----------------------
2804
2805 when Attribute_Compiler_Version =>
2806 Check_E0;
2807 Check_Standard_Prefix;
2808 Rewrite (N, Make_String_Literal (Loc, "GNAT " & Gnat_Version_String));
2809 Analyze_And_Resolve (N, Standard_String);
2810
2811 --------------------
2812 -- Component_Size --
2813 --------------------
2814
2815 when Attribute_Component_Size =>
2816 Check_E0;
2817 Set_Etype (N, Universal_Integer);
2818
2819 -- Note: unlike other array attributes, unconstrained arrays are OK
2820
2821 if Is_Array_Type (P_Type) and then not Is_Constrained (P_Type) then
2822 null;
2823 else
2824 Check_Array_Type;
2825 end if;
2826
2827 -------------
2828 -- Compose --
2829 -------------
2830
2831 when Attribute_Compose =>
2832 Check_Floating_Point_Type_2;
2833 Set_Etype (N, P_Base_Type);
2834 Resolve (E1, P_Base_Type);
2835 Resolve (E2, Any_Integer);
2836
2837 -----------------
2838 -- Constrained --
2839 -----------------
2840
2841 when Attribute_Constrained =>
2842 Check_E0;
2843 Set_Etype (N, Standard_Boolean);
2844
2845 -- Case from RM J.4(2) of constrained applied to private type
2846
2847 if Is_Entity_Name (P) and then Is_Type (Entity (P)) then
2848 Check_Restriction (No_Obsolescent_Features, P);
2849
2850 if Warn_On_Obsolescent_Feature then
2851 Error_Msg_N
2852 ("constrained for private type is an " &
2853 "obsolescent feature (RM J.4)?", N);
2854 end if;
2855
2856 -- If we are within an instance, the attribute must be legal
2857 -- because it was valid in the generic unit. Ditto if this is
2858 -- an inlining of a function declared in an instance.
2859
2860 if In_Instance
2861 or else In_Inlined_Body
2862 then
2863 return;
2864
2865 -- For sure OK if we have a real private type itself, but must
2866 -- be completed, cannot apply Constrained to incomplete type.
2867
2868 elsif Is_Private_Type (Entity (P)) then
2869
2870 -- Note: this is one of the Annex J features that does not
2871 -- generate a warning from -gnatwj, since in fact it seems
2872 -- very useful, and is used in the GNAT runtime.
2873
2874 Check_Not_Incomplete_Type;
2875 return;
2876 end if;
2877
2878 -- Normal (non-obsolescent case) of application to object of
2879 -- a discriminated type.
2880
2881 else
2882 Check_Object_Reference (P);
2883
2884 -- If N does not come from source, then we allow the
2885 -- the attribute prefix to be of a private type whose
2886 -- full type has discriminants. This occurs in cases
2887 -- involving expanded calls to stream attributes.
2888
2889 if not Comes_From_Source (N) then
2890 P_Type := Underlying_Type (P_Type);
2891 end if;
2892
2893 -- Must have discriminants or be an access type designating
2894 -- a type with discriminants. If it is a classwide type is ???
2895 -- has unknown discriminants.
2896
2897 if Has_Discriminants (P_Type)
2898 or else Has_Unknown_Discriminants (P_Type)
2899 or else
2900 (Is_Access_Type (P_Type)
2901 and then Has_Discriminants (Designated_Type (P_Type)))
2902 then
2903 return;
2904
2905 -- Also allow an object of a generic type if extensions allowed
2906 -- and allow this for any type at all.
2907
2908 elsif (Is_Generic_Type (P_Type)
2909 or else Is_Generic_Actual_Type (P_Type))
2910 and then Extensions_Allowed
2911 then
2912 return;
2913 end if;
2914 end if;
2915
2916 -- Fall through if bad prefix
2917
2918 Error_Attr_P
2919 ("prefix of % attribute must be object of discriminated type");
2920
2921 ---------------
2922 -- Copy_Sign --
2923 ---------------
2924
2925 when Attribute_Copy_Sign =>
2926 Check_Floating_Point_Type_2;
2927 Set_Etype (N, P_Base_Type);
2928 Resolve (E1, P_Base_Type);
2929 Resolve (E2, P_Base_Type);
2930
2931 -----------
2932 -- Count --
2933 -----------
2934
2935 when Attribute_Count => Count :
2936 declare
2937 Ent : Entity_Id;
2938 S : Entity_Id;
2939 Tsk : Entity_Id;
2940
2941 begin
2942 Check_E0;
2943
2944 if Nkind_In (P, N_Identifier, N_Expanded_Name) then
2945 Ent := Entity (P);
2946
2947 if Ekind (Ent) /= E_Entry then
2948 Error_Attr ("invalid entry name", N);
2949 end if;
2950
2951 elsif Nkind (P) = N_Indexed_Component then
2952 if not Is_Entity_Name (Prefix (P))
2953 or else No (Entity (Prefix (P)))
2954 or else Ekind (Entity (Prefix (P))) /= E_Entry_Family
2955 then
2956 if Nkind (Prefix (P)) = N_Selected_Component
2957 and then Present (Entity (Selector_Name (Prefix (P))))
2958 and then Ekind (Entity (Selector_Name (Prefix (P)))) =
2959 E_Entry_Family
2960 then
2961 Error_Attr
2962 ("attribute % must apply to entry of current task", P);
2963
2964 else
2965 Error_Attr ("invalid entry family name", P);
2966 end if;
2967 return;
2968
2969 else
2970 Ent := Entity (Prefix (P));
2971 end if;
2972
2973 elsif Nkind (P) = N_Selected_Component
2974 and then Present (Entity (Selector_Name (P)))
2975 and then Ekind (Entity (Selector_Name (P))) = E_Entry
2976 then
2977 Error_Attr
2978 ("attribute % must apply to entry of current task", P);
2979
2980 else
2981 Error_Attr ("invalid entry name", N);
2982 return;
2983 end if;
2984
2985 for J in reverse 0 .. Scope_Stack.Last loop
2986 S := Scope_Stack.Table (J).Entity;
2987
2988 if S = Scope (Ent) then
2989 if Nkind (P) = N_Expanded_Name then
2990 Tsk := Entity (Prefix (P));
2991
2992 -- The prefix denotes either the task type, or else a
2993 -- single task whose task type is being analyzed.
2994
2995 if (Is_Type (Tsk)
2996 and then Tsk = S)
2997
2998 or else (not Is_Type (Tsk)
2999 and then Etype (Tsk) = S
3000 and then not (Comes_From_Source (S)))
3001 then
3002 null;
3003 else
3004 Error_Attr
3005 ("Attribute % must apply to entry of current task", N);
3006 end if;
3007 end if;
3008
3009 exit;
3010
3011 elsif Ekind (Scope (Ent)) in Task_Kind
3012 and then
3013 not Ekind_In (S, E_Loop, E_Block, E_Entry, E_Entry_Family)
3014 then
3015 Error_Attr ("Attribute % cannot appear in inner unit", N);
3016
3017 elsif Ekind (Scope (Ent)) = E_Protected_Type
3018 and then not Has_Completion (Scope (Ent))
3019 then
3020 Error_Attr ("attribute % can only be used inside body", N);
3021 end if;
3022 end loop;
3023
3024 if Is_Overloaded (P) then
3025 declare
3026 Index : Interp_Index;
3027 It : Interp;
3028
3029 begin
3030 Get_First_Interp (P, Index, It);
3031
3032 while Present (It.Nam) loop
3033 if It.Nam = Ent then
3034 null;
3035
3036 -- Ada 2005 (AI-345): Do not consider primitive entry
3037 -- wrappers generated for task or protected types.
3038
3039 elsif Ada_Version >= Ada_2005
3040 and then not Comes_From_Source (It.Nam)
3041 then
3042 null;
3043
3044 else
3045 Error_Attr ("ambiguous entry name", N);
3046 end if;
3047
3048 Get_Next_Interp (Index, It);
3049 end loop;
3050 end;
3051 end if;
3052
3053 Set_Etype (N, Universal_Integer);
3054 end Count;
3055
3056 -----------------------
3057 -- Default_Bit_Order --
3058 -----------------------
3059
3060 when Attribute_Default_Bit_Order => Default_Bit_Order :
3061 begin
3062 Check_Standard_Prefix;
3063
3064 if Bytes_Big_Endian then
3065 Rewrite (N,
3066 Make_Integer_Literal (Loc, False_Value));
3067 else
3068 Rewrite (N,
3069 Make_Integer_Literal (Loc, True_Value));
3070 end if;
3071
3072 Set_Etype (N, Universal_Integer);
3073 Set_Is_Static_Expression (N);
3074 end Default_Bit_Order;
3075
3076 --------------
3077 -- Definite --
3078 --------------
3079
3080 when Attribute_Definite =>
3081 Legal_Formal_Attribute;
3082
3083 -----------
3084 -- Delta --
3085 -----------
3086
3087 when Attribute_Delta =>
3088 Check_Fixed_Point_Type_0;
3089 Set_Etype (N, Universal_Real);
3090
3091 ------------
3092 -- Denorm --
3093 ------------
3094
3095 when Attribute_Denorm =>
3096 Check_Floating_Point_Type_0;
3097 Set_Etype (N, Standard_Boolean);
3098
3099 ---------------------
3100 -- Descriptor_Size --
3101 ---------------------
3102
3103 when Attribute_Descriptor_Size =>
3104 Check_E0;
3105
3106 if not Is_Entity_Name (P)
3107 or else not Is_Type (Entity (P))
3108 then
3109 Error_Attr_P ("prefix of attribute % must denote a type");
3110 end if;
3111
3112 Set_Etype (N, Universal_Integer);
3113
3114 ------------
3115 -- Digits --
3116 ------------
3117
3118 when Attribute_Digits =>
3119 Check_E0;
3120 Check_Type;
3121
3122 if not Is_Floating_Point_Type (P_Type)
3123 and then not Is_Decimal_Fixed_Point_Type (P_Type)
3124 then
3125 Error_Attr_P
3126 ("prefix of % attribute must be float or decimal type");
3127 end if;
3128
3129 Set_Etype (N, Universal_Integer);
3130
3131 ---------------
3132 -- Elab_Body --
3133 ---------------
3134
3135 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3136
3137 when Attribute_Elab_Body |
3138 Attribute_Elab_Spec |
3139 Attribute_Elab_Subp_Body =>
3140
3141 Check_E0;
3142 Check_Unit_Name (P);
3143 Set_Etype (N, Standard_Void_Type);
3144
3145 -- We have to manually call the expander in this case to get
3146 -- the necessary expansion (normally attributes that return
3147 -- entities are not expanded).
3148
3149 Expand (N);
3150
3151 ---------------
3152 -- Elab_Spec --
3153 ---------------
3154
3155 -- Shares processing with Elab_Body
3156
3157 ----------------
3158 -- Elaborated --
3159 ----------------
3160
3161 when Attribute_Elaborated =>
3162 Check_E0;
3163 Check_Unit_Name (P);
3164 Set_Etype (N, Standard_Boolean);
3165
3166 ----------
3167 -- Emax --
3168 ----------
3169
3170 when Attribute_Emax =>
3171 Check_Floating_Point_Type_0;
3172 Set_Etype (N, Universal_Integer);
3173
3174 -------------
3175 -- Enabled --
3176 -------------
3177
3178 when Attribute_Enabled =>
3179 Check_Either_E0_Or_E1;
3180
3181 if Present (E1) then
3182 if not Is_Entity_Name (E1) or else No (Entity (E1)) then
3183 Error_Msg_N ("entity name expected for Enabled attribute", E1);
3184 E1 := Empty;
3185 end if;
3186 end if;
3187
3188 if Nkind (P) /= N_Identifier then
3189 Error_Msg_N ("identifier expected (check name)", P);
3190 elsif Get_Check_Id (Chars (P)) = No_Check_Id then
3191 Error_Msg_N ("& is not a recognized check name", P);
3192 end if;
3193
3194 Set_Etype (N, Standard_Boolean);
3195
3196 --------------
3197 -- Enum_Rep --
3198 --------------
3199
3200 when Attribute_Enum_Rep => Enum_Rep : declare
3201 begin
3202 if Present (E1) then
3203 Check_E1;
3204 Check_Discrete_Type;
3205 Resolve (E1, P_Base_Type);
3206
3207 else
3208 if not Is_Entity_Name (P)
3209 or else (not Is_Object (Entity (P))
3210 and then
3211 Ekind (Entity (P)) /= E_Enumeration_Literal)
3212 then
3213 Error_Attr_P
3214 ("prefix of % attribute must be " &
3215 "discrete type/object or enum literal");
3216 end if;
3217 end if;
3218
3219 Set_Etype (N, Universal_Integer);
3220 end Enum_Rep;
3221
3222 --------------
3223 -- Enum_Val --
3224 --------------
3225
3226 when Attribute_Enum_Val => Enum_Val : begin
3227 Check_E1;
3228 Check_Type;
3229
3230 if not Is_Enumeration_Type (P_Type) then
3231 Error_Attr_P ("prefix of % attribute must be enumeration type");
3232 end if;
3233
3234 -- If the enumeration type has a standard representation, the effect
3235 -- is the same as 'Val, so rewrite the attribute as a 'Val.
3236
3237 if not Has_Non_Standard_Rep (P_Base_Type) then
3238 Rewrite (N,
3239 Make_Attribute_Reference (Loc,
3240 Prefix => Relocate_Node (Prefix (N)),
3241 Attribute_Name => Name_Val,
3242 Expressions => New_List (Relocate_Node (E1))));
3243 Analyze_And_Resolve (N, P_Base_Type);
3244
3245 -- Non-standard representation case (enumeration with holes)
3246
3247 else
3248 Check_Enum_Image;
3249 Resolve (E1, Any_Integer);
3250 Set_Etype (N, P_Base_Type);
3251 end if;
3252 end Enum_Val;
3253
3254 -------------
3255 -- Epsilon --
3256 -------------
3257
3258 when Attribute_Epsilon =>
3259 Check_Floating_Point_Type_0;
3260 Set_Etype (N, Universal_Real);
3261
3262 --------------
3263 -- Exponent --
3264 --------------
3265
3266 when Attribute_Exponent =>
3267 Check_Floating_Point_Type_1;
3268 Set_Etype (N, Universal_Integer);
3269 Resolve (E1, P_Base_Type);
3270
3271 ------------------
3272 -- External_Tag --
3273 ------------------
3274
3275 when Attribute_External_Tag =>
3276 Check_E0;
3277 Check_Type;
3278
3279 Set_Etype (N, Standard_String);
3280
3281 if not Is_Tagged_Type (P_Type) then
3282 Error_Attr_P ("prefix of % attribute must be tagged");
3283 end if;
3284
3285 ---------------
3286 -- Fast_Math --
3287 ---------------
3288
3289 when Attribute_Fast_Math =>
3290 Check_Standard_Prefix;
3291
3292 if Opt.Fast_Math then
3293 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3294 else
3295 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3296 end if;
3297
3298 -----------
3299 -- First --
3300 -----------
3301
3302 when Attribute_First =>
3303 Check_Array_Or_Scalar_Type;
3304 Bad_Attribute_For_Predicate;
3305
3306 ---------------
3307 -- First_Bit --
3308 ---------------
3309
3310 when Attribute_First_Bit =>
3311 Check_Component;
3312 Set_Etype (N, Universal_Integer);
3313
3314 -----------------
3315 -- First_Valid --
3316 -----------------
3317
3318 when Attribute_First_Valid =>
3319 Check_First_Last_Valid;
3320 Set_Etype (N, P_Type);
3321
3322 -----------------
3323 -- Fixed_Value --
3324 -----------------
3325
3326 when Attribute_Fixed_Value =>
3327 Check_E1;
3328 Check_Fixed_Point_Type;
3329 Resolve (E1, Any_Integer);
3330 Set_Etype (N, P_Base_Type);
3331
3332 -----------
3333 -- Floor --
3334 -----------
3335
3336 when Attribute_Floor =>
3337 Check_Floating_Point_Type_1;
3338 Set_Etype (N, P_Base_Type);
3339 Resolve (E1, P_Base_Type);
3340
3341 ----------
3342 -- Fore --
3343 ----------
3344
3345 when Attribute_Fore =>
3346 Check_Fixed_Point_Type_0;
3347 Set_Etype (N, Universal_Integer);
3348
3349 --------------
3350 -- Fraction --
3351 --------------
3352
3353 when Attribute_Fraction =>
3354 Check_Floating_Point_Type_1;
3355 Set_Etype (N, P_Base_Type);
3356 Resolve (E1, P_Base_Type);
3357
3358 --------------
3359 -- From_Any --
3360 --------------
3361
3362 when Attribute_From_Any =>
3363 Check_E1;
3364 Check_PolyORB_Attribute;
3365 Set_Etype (N, P_Base_Type);
3366
3367 -----------------------
3368 -- Has_Access_Values --
3369 -----------------------
3370
3371 when Attribute_Has_Access_Values =>
3372 Check_Type;
3373 Check_E0;
3374 Set_Etype (N, Standard_Boolean);
3375
3376 -----------------------
3377 -- Has_Tagged_Values --
3378 -----------------------
3379
3380 when Attribute_Has_Tagged_Values =>
3381 Check_Type;
3382 Check_E0;
3383 Set_Etype (N, Standard_Boolean);
3384
3385 -----------------------
3386 -- Has_Discriminants --
3387 -----------------------
3388
3389 when Attribute_Has_Discriminants =>
3390 Legal_Formal_Attribute;
3391
3392 --------------
3393 -- Identity --
3394 --------------
3395
3396 when Attribute_Identity =>
3397 Check_E0;
3398 Analyze (P);
3399
3400 if Etype (P) = Standard_Exception_Type then
3401 Set_Etype (N, RTE (RE_Exception_Id));
3402
3403 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to
3404 -- task interface class-wide types.
3405
3406 elsif Is_Task_Type (Etype (P))
3407 or else (Is_Access_Type (Etype (P))
3408 and then Is_Task_Type (Designated_Type (Etype (P))))
3409 or else (Ada_Version >= Ada_2005
3410 and then Ekind (Etype (P)) = E_Class_Wide_Type
3411 and then Is_Interface (Etype (P))
3412 and then Is_Task_Interface (Etype (P)))
3413 then
3414 Resolve (P);
3415 Set_Etype (N, RTE (RO_AT_Task_Id));
3416
3417 else
3418 if Ada_Version >= Ada_2005 then
3419 Error_Attr_P
3420 ("prefix of % attribute must be an exception, a " &
3421 "task or a task interface class-wide object");
3422 else
3423 Error_Attr_P
3424 ("prefix of % attribute must be a task or an exception");
3425 end if;
3426 end if;
3427
3428 -----------
3429 -- Image --
3430 -----------
3431
3432 when Attribute_Image => Image :
3433 begin
3434 Check_SPARK_Restriction_On_Attribute;
3435 Check_Scalar_Type;
3436 Set_Etype (N, Standard_String);
3437
3438 if Is_Real_Type (P_Type) then
3439 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3440 Error_Msg_Name_1 := Aname;
3441 Error_Msg_N
3442 ("(Ada 83) % attribute not allowed for real types", N);
3443 end if;
3444 end if;
3445
3446 if Is_Enumeration_Type (P_Type) then
3447 Check_Restriction (No_Enumeration_Maps, N);
3448 end if;
3449
3450 Check_E1;
3451 Resolve (E1, P_Base_Type);
3452 Check_Enum_Image;
3453 Validate_Non_Static_Attribute_Function_Call;
3454 end Image;
3455
3456 ---------
3457 -- Img --
3458 ---------
3459
3460 when Attribute_Img => Img :
3461 begin
3462 Check_E0;
3463 Set_Etype (N, Standard_String);
3464
3465 if not Is_Scalar_Type (P_Type)
3466 or else (Is_Entity_Name (P) and then Is_Type (Entity (P)))
3467 then
3468 Error_Attr_P
3469 ("prefix of % attribute must be scalar object name");
3470 end if;
3471
3472 Check_Enum_Image;
3473 end Img;
3474
3475 -----------
3476 -- Input --
3477 -----------
3478
3479 when Attribute_Input =>
3480 Check_E1;
3481 Check_Stream_Attribute (TSS_Stream_Input);
3482 Set_Etype (N, P_Base_Type);
3483
3484 -------------------
3485 -- Integer_Value --
3486 -------------------
3487
3488 when Attribute_Integer_Value =>
3489 Check_E1;
3490 Check_Integer_Type;
3491 Resolve (E1, Any_Fixed);
3492
3493 -- Signal an error if argument type is not a specific fixed-point
3494 -- subtype. An error has been signalled already if the argument
3495 -- was not of a fixed-point type.
3496
3497 if Etype (E1) = Any_Fixed and then not Error_Posted (E1) then
3498 Error_Attr ("argument of % must be of a fixed-point type", E1);
3499 end if;
3500
3501 Set_Etype (N, P_Base_Type);
3502
3503 -------------------
3504 -- Invalid_Value --
3505 -------------------
3506
3507 when Attribute_Invalid_Value =>
3508 Check_E0;
3509 Check_Scalar_Type;
3510 Set_Etype (N, P_Base_Type);
3511 Invalid_Value_Used := True;
3512
3513 -----------
3514 -- Large --
3515 -----------
3516
3517 when Attribute_Large =>
3518 Check_E0;
3519 Check_Real_Type;
3520 Set_Etype (N, Universal_Real);
3521
3522 ----------
3523 -- Last --
3524 ----------
3525
3526 when Attribute_Last =>
3527 Check_Array_Or_Scalar_Type;
3528 Bad_Attribute_For_Predicate;
3529
3530 --------------
3531 -- Last_Bit --
3532 --------------
3533
3534 when Attribute_Last_Bit =>
3535 Check_Component;
3536 Set_Etype (N, Universal_Integer);
3537
3538 ----------------
3539 -- Last_Valid --
3540 ----------------
3541
3542 when Attribute_Last_Valid =>
3543 Check_First_Last_Valid;
3544 Set_Etype (N, P_Type);
3545
3546 ------------------
3547 -- Leading_Part --
3548 ------------------
3549
3550 when Attribute_Leading_Part =>
3551 Check_Floating_Point_Type_2;
3552 Set_Etype (N, P_Base_Type);
3553 Resolve (E1, P_Base_Type);
3554 Resolve (E2, Any_Integer);
3555
3556 ------------
3557 -- Length --
3558 ------------
3559
3560 when Attribute_Length =>
3561 Check_Array_Type;
3562 Set_Etype (N, Universal_Integer);
3563
3564 -------------
3565 -- Machine --
3566 -------------
3567
3568 when Attribute_Machine =>
3569 Check_Floating_Point_Type_1;
3570 Set_Etype (N, P_Base_Type);
3571 Resolve (E1, P_Base_Type);
3572
3573 ------------------
3574 -- Machine_Emax --
3575 ------------------
3576
3577 when Attribute_Machine_Emax =>
3578 Check_Floating_Point_Type_0;
3579 Set_Etype (N, Universal_Integer);
3580
3581 ------------------
3582 -- Machine_Emin --
3583 ------------------
3584
3585 when Attribute_Machine_Emin =>
3586 Check_Floating_Point_Type_0;
3587 Set_Etype (N, Universal_Integer);
3588
3589 ----------------------
3590 -- Machine_Mantissa --
3591 ----------------------
3592
3593 when Attribute_Machine_Mantissa =>
3594 Check_Floating_Point_Type_0;
3595 Set_Etype (N, Universal_Integer);
3596
3597 -----------------------
3598 -- Machine_Overflows --
3599 -----------------------
3600
3601 when Attribute_Machine_Overflows =>
3602 Check_Real_Type;
3603 Check_E0;
3604 Set_Etype (N, Standard_Boolean);
3605
3606 -------------------
3607 -- Machine_Radix --
3608 -------------------
3609
3610 when Attribute_Machine_Radix =>
3611 Check_Real_Type;
3612 Check_E0;
3613 Set_Etype (N, Universal_Integer);
3614
3615 ----------------------
3616 -- Machine_Rounding --
3617 ----------------------
3618
3619 when Attribute_Machine_Rounding =>
3620 Check_Floating_Point_Type_1;
3621 Set_Etype (N, P_Base_Type);
3622 Resolve (E1, P_Base_Type);
3623
3624 --------------------
3625 -- Machine_Rounds --
3626 --------------------
3627
3628 when Attribute_Machine_Rounds =>
3629 Check_Real_Type;
3630 Check_E0;
3631 Set_Etype (N, Standard_Boolean);
3632
3633 ------------------
3634 -- Machine_Size --
3635 ------------------
3636
3637 when Attribute_Machine_Size =>
3638 Check_E0;
3639 Check_Type;
3640 Check_Not_Incomplete_Type;
3641 Set_Etype (N, Universal_Integer);
3642
3643 --------------
3644 -- Mantissa --
3645 --------------
3646
3647 when Attribute_Mantissa =>
3648 Check_E0;
3649 Check_Real_Type;
3650 Set_Etype (N, Universal_Integer);
3651
3652 ---------
3653 -- Max --
3654 ---------
3655
3656 when Attribute_Max =>
3657 Check_E2;
3658 Check_Scalar_Type;
3659 Resolve (E1, P_Base_Type);
3660 Resolve (E2, P_Base_Type);
3661 Set_Etype (N, P_Base_Type);
3662
3663 ----------------------------------
3664 -- Max_Alignment_For_Allocation --
3665 -- Max_Size_In_Storage_Elements --
3666 ----------------------------------
3667
3668 when Attribute_Max_Alignment_For_Allocation |
3669 Attribute_Max_Size_In_Storage_Elements =>
3670 Check_E0;
3671 Check_Type;
3672 Check_Not_Incomplete_Type;
3673 Set_Etype (N, Universal_Integer);
3674
3675 -----------------------
3676 -- Maximum_Alignment --
3677 -----------------------
3678
3679 when Attribute_Maximum_Alignment =>
3680 Standard_Attribute (Ttypes.Maximum_Alignment);
3681
3682 --------------------
3683 -- Mechanism_Code --
3684 --------------------
3685
3686 when Attribute_Mechanism_Code =>
3687 if not Is_Entity_Name (P)
3688 or else not Is_Subprogram (Entity (P))
3689 then
3690 Error_Attr_P ("prefix of % attribute must be subprogram");
3691 end if;
3692
3693 Check_Either_E0_Or_E1;
3694
3695 if Present (E1) then
3696 Resolve (E1, Any_Integer);
3697 Set_Etype (E1, Standard_Integer);
3698
3699 if not Is_Static_Expression (E1) then
3700 Flag_Non_Static_Expr
3701 ("expression for parameter number must be static!", E1);
3702 Error_Attr;
3703
3704 elsif UI_To_Int (Intval (E1)) > Number_Formals (Entity (P))
3705 or else UI_To_Int (Intval (E1)) < 0
3706 then
3707 Error_Attr ("invalid parameter number for % attribute", E1);
3708 end if;
3709 end if;
3710
3711 Set_Etype (N, Universal_Integer);
3712
3713 ---------
3714 -- Min --
3715 ---------
3716
3717 when Attribute_Min =>
3718 Check_E2;
3719 Check_Scalar_Type;
3720 Resolve (E1, P_Base_Type);
3721 Resolve (E2, P_Base_Type);
3722 Set_Etype (N, P_Base_Type);
3723
3724 ---------
3725 -- Mod --
3726 ---------
3727
3728 when Attribute_Mod =>
3729
3730 -- Note: this attribute is only allowed in Ada 2005 mode, but
3731 -- we do not need to test that here, since Mod is only recognized
3732 -- as an attribute name in Ada 2005 mode during the parse.
3733
3734 Check_E1;
3735 Check_Modular_Integer_Type;
3736 Resolve (E1, Any_Integer);
3737 Set_Etype (N, P_Base_Type);
3738
3739 -----------
3740 -- Model --
3741 -----------
3742
3743 when Attribute_Model =>
3744 Check_Floating_Point_Type_1;
3745 Set_Etype (N, P_Base_Type);
3746 Resolve (E1, P_Base_Type);
3747
3748 ----------------
3749 -- Model_Emin --
3750 ----------------
3751
3752 when Attribute_Model_Emin =>
3753 Check_Floating_Point_Type_0;
3754 Set_Etype (N, Universal_Integer);
3755
3756 -------------------
3757 -- Model_Epsilon --
3758 -------------------
3759
3760 when Attribute_Model_Epsilon =>
3761 Check_Floating_Point_Type_0;
3762 Set_Etype (N, Universal_Real);
3763
3764 --------------------
3765 -- Model_Mantissa --
3766 --------------------
3767
3768 when Attribute_Model_Mantissa =>
3769 Check_Floating_Point_Type_0;
3770 Set_Etype (N, Universal_Integer);
3771
3772 -----------------
3773 -- Model_Small --
3774 -----------------
3775
3776 when Attribute_Model_Small =>
3777 Check_Floating_Point_Type_0;
3778 Set_Etype (N, Universal_Real);
3779
3780 -------------
3781 -- Modulus --
3782 -------------
3783
3784 when Attribute_Modulus =>
3785 Check_E0;
3786 Check_Modular_Integer_Type;
3787 Set_Etype (N, Universal_Integer);
3788
3789 --------------------
3790 -- Null_Parameter --
3791 --------------------
3792
3793 when Attribute_Null_Parameter => Null_Parameter : declare
3794 Parnt : constant Node_Id := Parent (N);
3795 GParnt : constant Node_Id := Parent (Parnt);
3796
3797 procedure Bad_Null_Parameter (Msg : String);
3798 -- Used if bad Null parameter attribute node is found. Issues
3799 -- given error message, and also sets the type to Any_Type to
3800 -- avoid blowups later on from dealing with a junk node.
3801
3802 procedure Must_Be_Imported (Proc_Ent : Entity_Id);
3803 -- Called to check that Proc_Ent is imported subprogram
3804
3805 ------------------------
3806 -- Bad_Null_Parameter --
3807 ------------------------
3808
3809 procedure Bad_Null_Parameter (Msg : String) is
3810 begin
3811 Error_Msg_N (Msg, N);
3812 Set_Etype (N, Any_Type);
3813 end Bad_Null_Parameter;
3814
3815 ----------------------
3816 -- Must_Be_Imported --
3817 ----------------------
3818
3819 procedure Must_Be_Imported (Proc_Ent : Entity_Id) is
3820 Pent : constant Entity_Id := Ultimate_Alias (Proc_Ent);
3821
3822 begin
3823 -- Ignore check if procedure not frozen yet (we will get
3824 -- another chance when the default parameter is reanalyzed)
3825
3826 if not Is_Frozen (Pent) then
3827 return;
3828
3829 elsif not Is_Imported (Pent) then
3830 Bad_Null_Parameter
3831 ("Null_Parameter can only be used with imported subprogram");
3832
3833 else
3834 return;
3835 end if;
3836 end Must_Be_Imported;
3837
3838 -- Start of processing for Null_Parameter
3839
3840 begin
3841 Check_Type;
3842 Check_E0;
3843 Set_Etype (N, P_Type);
3844
3845 -- Case of attribute used as default expression
3846
3847 if Nkind (Parnt) = N_Parameter_Specification then
3848 Must_Be_Imported (Defining_Entity (GParnt));
3849
3850 -- Case of attribute used as actual for subprogram (positional)
3851
3852 elsif Nkind (Parnt) in N_Subprogram_Call
3853 and then Is_Entity_Name (Name (Parnt))
3854 then
3855 Must_Be_Imported (Entity (Name (Parnt)));
3856
3857 -- Case of attribute used as actual for subprogram (named)
3858
3859 elsif Nkind (Parnt) = N_Parameter_Association
3860 and then Nkind (GParnt) in N_Subprogram_Call
3861 and then Is_Entity_Name (Name (GParnt))
3862 then
3863 Must_Be_Imported (Entity (Name (GParnt)));
3864
3865 -- Not an allowed case
3866
3867 else
3868 Bad_Null_Parameter
3869 ("Null_Parameter must be actual or default parameter");
3870 end if;
3871 end Null_Parameter;
3872
3873 -----------------
3874 -- Object_Size --
3875 -----------------
3876
3877 when Attribute_Object_Size =>
3878 Check_E0;
3879 Check_Type;
3880 Check_Not_Incomplete_Type;
3881 Set_Etype (N, Universal_Integer);
3882
3883 ---------
3884 -- Old --
3885 ---------
3886
3887 when Attribute_Old =>
3888
3889 -- The attribute reference is a primary. If expressions follow, the
3890 -- attribute reference is an indexable object, so rewrite the node
3891 -- accordingly.
3892
3893 if Present (E1) then
3894 Rewrite (N,
3895 Make_Indexed_Component (Loc,
3896 Prefix =>
3897 Make_Attribute_Reference (Loc,
3898 Prefix => Relocate_Node (Prefix (N)),
3899 Attribute_Name => Name_Old),
3900 Expressions => Expressions (N)));
3901
3902 Analyze (N);
3903 return;
3904 end if;
3905
3906 Check_E0;
3907
3908 -- Prefix has not been analyzed yet, and its full analysis will take
3909 -- place during expansion (see below).
3910
3911 Preanalyze_And_Resolve (P);
3912 P_Type := Etype (P);
3913 Set_Etype (N, P_Type);
3914
3915 if No (Current_Subprogram) then
3916 Error_Attr ("attribute % can only appear within subprogram", N);
3917 end if;
3918
3919 if Is_Limited_Type (P_Type) then
3920 Error_Attr ("attribute % cannot apply to limited objects", P);
3921 end if;
3922
3923 if Is_Entity_Name (P)
3924 and then Is_Constant_Object (Entity (P))
3925 then
3926 Error_Msg_N
3927 ("?attribute Old applied to constant has no effect", P);
3928 end if;
3929
3930 -- Check that the expression does not refer to local entities
3931
3932 Check_Local : declare
3933 Subp : Entity_Id := Current_Subprogram;
3934
3935 function Process (N : Node_Id) return Traverse_Result;
3936 -- Check that N does not contain references to local variables or
3937 -- other local entities of Subp.
3938
3939 -------------
3940 -- Process --
3941 -------------
3942
3943 function Process (N : Node_Id) return Traverse_Result is
3944 begin
3945 if Is_Entity_Name (N)
3946 and then Present (Entity (N))
3947 and then not Is_Formal (Entity (N))
3948 and then Enclosing_Subprogram (Entity (N)) = Subp
3949 then
3950 Error_Msg_Node_1 := Entity (N);
3951 Error_Attr
3952 ("attribute % cannot refer to local variable&", N);
3953 end if;
3954
3955 return OK;
3956 end Process;
3957
3958 procedure Check_No_Local is new Traverse_Proc;
3959
3960 -- Start of processing for Check_Local
3961
3962 begin
3963 Check_No_Local (P);
3964
3965 if In_Parameter_Specification (P) then
3966
3967 -- We have additional restrictions on using 'Old in parameter
3968 -- specifications.
3969
3970 if Present (Enclosing_Subprogram (Current_Subprogram)) then
3971
3972 -- Check that there is no reference to the enclosing
3973 -- subprogram local variables. Otherwise, we might end up
3974 -- being called from the enclosing subprogram and thus using
3975 -- 'Old on a local variable which is not defined at entry
3976 -- time.
3977
3978 Subp := Enclosing_Subprogram (Current_Subprogram);
3979 Check_No_Local (P);
3980
3981 else
3982 -- We must prevent default expression of library-level
3983 -- subprogram from using 'Old, as the subprogram may be
3984 -- used in elaboration code for which there is no enclosing
3985 -- subprogram.
3986
3987 Error_Attr
3988 ("attribute % can only appear within subprogram", N);
3989 end if;
3990 end if;
3991 end Check_Local;
3992
3993 -- The attribute appears within a pre/postcondition, but refers to
3994 -- an entity in the enclosing subprogram. If it is a component of a
3995 -- formal its expansion might generate actual subtypes that may be
3996 -- referenced in an inner context, and which must be elaborated
3997 -- within the subprogram itself. As a result we create a declaration
3998 -- for it and insert it at the start of the enclosing subprogram
3999 -- This is properly an expansion activity but it has to be performed
4000 -- now to prevent out-of-order issues.
4001
4002 if Nkind (P) = N_Selected_Component
4003 and then Has_Discriminants (Etype (Prefix (P)))
4004 then
4005 P_Type := Base_Type (P_Type);
4006 Set_Etype (N, P_Type);
4007 Set_Etype (P, P_Type);
4008 Expand (N);
4009 end if;
4010
4011 ----------------------
4012 -- Overlaps_Storage --
4013 ----------------------
4014
4015 when Attribute_Overlaps_Storage =>
4016 Check_Ada_2012_Attribute;
4017 Check_E1;
4018
4019 -- Both arguments must be objects of any type
4020
4021 Analyze_And_Resolve (P);
4022 Analyze_And_Resolve (E1);
4023 Check_Object_Reference (P);
4024 Check_Object_Reference (E1);
4025 Set_Etype (N, Standard_Boolean);
4026
4027 ------------
4028 -- Output --
4029 ------------
4030
4031 when Attribute_Output =>
4032 Check_E2;
4033 Check_Stream_Attribute (TSS_Stream_Output);
4034 Set_Etype (N, Standard_Void_Type);
4035 Resolve (N, Standard_Void_Type);
4036
4037 ------------------
4038 -- Partition_ID --
4039 ------------------
4040
4041 when Attribute_Partition_ID => Partition_Id :
4042 begin
4043 Check_E0;
4044
4045 if P_Type /= Any_Type then
4046 if not Is_Library_Level_Entity (Entity (P)) then
4047 Error_Attr_P
4048 ("prefix of % attribute must be library-level entity");
4049
4050 -- The defining entity of prefix should not be declared inside a
4051 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
4052
4053 elsif Is_Entity_Name (P)
4054 and then Is_Pure (Entity (P))
4055 then
4056 Error_Attr_P ("prefix of% attribute must not be declared pure");
4057 end if;
4058 end if;
4059
4060 Set_Etype (N, Universal_Integer);
4061 end Partition_Id;
4062
4063 -------------------------
4064 -- Passed_By_Reference --
4065 -------------------------
4066
4067 when Attribute_Passed_By_Reference =>
4068 Check_E0;
4069 Check_Type;
4070 Set_Etype (N, Standard_Boolean);
4071
4072 ------------------
4073 -- Pool_Address --
4074 ------------------
4075
4076 when Attribute_Pool_Address =>
4077 Check_E0;
4078 Set_Etype (N, RTE (RE_Address));
4079
4080 ---------
4081 -- Pos --
4082 ---------
4083
4084 when Attribute_Pos =>
4085 Check_Discrete_Type;
4086 Check_E1;
4087
4088 if Is_Boolean_Type (P_Type) then
4089 Error_Msg_Name_1 := Aname;
4090 Error_Msg_Name_2 := Chars (P_Type);
4091 Check_SPARK_Restriction
4092 ("attribute% is not allowed for type%", P);
4093 end if;
4094
4095 Resolve (E1, P_Base_Type);
4096 Set_Etype (N, Universal_Integer);
4097
4098 --------------
4099 -- Position --
4100 --------------
4101
4102 when Attribute_Position =>
4103 Check_Component;
4104 Set_Etype (N, Universal_Integer);
4105
4106 ----------
4107 -- Pred --
4108 ----------
4109
4110 when Attribute_Pred =>
4111 Check_Scalar_Type;
4112 Check_E1;
4113
4114 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4115 Error_Msg_Name_1 := Aname;
4116 Error_Msg_Name_2 := Chars (P_Type);
4117 Check_SPARK_Restriction
4118 ("attribute% is not allowed for type%", P);
4119 end if;
4120
4121 Resolve (E1, P_Base_Type);
4122 Set_Etype (N, P_Base_Type);
4123
4124 -- Nothing to do for real type case
4125
4126 if Is_Real_Type (P_Type) then
4127 null;
4128
4129 -- If not modular type, test for overflow check required
4130
4131 else
4132 if not Is_Modular_Integer_Type (P_Type)
4133 and then not Range_Checks_Suppressed (P_Base_Type)
4134 then
4135 Enable_Range_Check (E1);
4136 end if;
4137 end if;
4138
4139 --------------
4140 -- Priority --
4141 --------------
4142
4143 -- Ada 2005 (AI-327): Dynamic ceiling priorities
4144
4145 when Attribute_Priority =>
4146 if Ada_Version < Ada_2005 then
4147 Error_Attr ("% attribute is allowed only in Ada 2005 mode", P);
4148 end if;
4149
4150 Check_E0;
4151
4152 -- The prefix must be a protected object (AARM D.5.2 (2/2))
4153
4154 Analyze (P);
4155
4156 if Is_Protected_Type (Etype (P))
4157 or else (Is_Access_Type (Etype (P))
4158 and then Is_Protected_Type (Designated_Type (Etype (P))))
4159 then
4160 Resolve (P, Etype (P));
4161 else
4162 Error_Attr_P ("prefix of % attribute must be a protected object");
4163 end if;
4164
4165 Set_Etype (N, Standard_Integer);
4166
4167 -- Must be called from within a protected procedure or entry of the
4168 -- protected object.
4169
4170 declare
4171 S : Entity_Id;
4172
4173 begin
4174 S := Current_Scope;
4175 while S /= Etype (P)
4176 and then S /= Standard_Standard
4177 loop
4178 S := Scope (S);
4179 end loop;
4180
4181 if S = Standard_Standard then
4182 Error_Attr ("the attribute % is only allowed inside protected "
4183 & "operations", P);
4184 end if;
4185 end;
4186
4187 Validate_Non_Static_Attribute_Function_Call;
4188
4189 -----------
4190 -- Range --
4191 -----------
4192
4193 when Attribute_Range =>
4194 Check_Array_Or_Scalar_Type;
4195 Bad_Attribute_For_Predicate;
4196
4197 if Ada_Version = Ada_83
4198 and then Is_Scalar_Type (P_Type)
4199 and then Comes_From_Source (N)
4200 then
4201 Error_Attr
4202 ("(Ada 83) % attribute not allowed for scalar type", P);
4203 end if;
4204
4205 ------------
4206 -- Result --
4207 ------------
4208
4209 when Attribute_Result => Result : declare
4210 CS : Entity_Id;
4211 -- The enclosing scope, excluding loops for quantified expressions
4212
4213 PS : Entity_Id;
4214 -- During analysis, CS is the postcondition subprogram and PS the
4215 -- source subprogram to which the postcondition applies. During
4216 -- pre-analysis, CS is the scope of the subprogram declaration.
4217
4218 Prag : Node_Id;
4219 -- During pre-analysis, Prag is the enclosing pragma node if any
4220
4221 begin
4222 -- Find enclosing scopes, excluding loops
4223
4224 CS := Current_Scope;
4225 while Ekind (CS) = E_Loop loop
4226 CS := Scope (CS);
4227 end loop;
4228
4229 PS := Scope (CS);
4230
4231 -- If the enclosing subprogram is always inlined, the enclosing
4232 -- postcondition will not be propagated to the expanded call.
4233
4234 if not In_Spec_Expression
4235 and then Has_Pragma_Inline_Always (PS)
4236 and then Warn_On_Redundant_Constructs
4237 then
4238 Error_Msg_N
4239 ("postconditions on inlined functions not enforced?", N);
4240 end if;
4241
4242 -- If we are in the scope of a function and in Spec_Expression mode,
4243 -- this is likely the prescan of the postcondition pragma, and we
4244 -- just set the proper type. If there is an error it will be caught
4245 -- when the real Analyze call is done.
4246
4247 if Ekind (CS) = E_Function
4248 and then In_Spec_Expression
4249 then
4250 -- Check OK prefix
4251
4252 if Chars (CS) /= Chars (P) then
4253 Error_Msg_Name_1 := Name_Result;
4254
4255 Error_Msg_NE
4256 ("incorrect prefix for % attribute, expected &", P, CS);
4257 Error_Attr;
4258 end if;
4259
4260 -- Check in postcondition of function
4261
4262 Prag := N;
4263 while not Nkind_In (Prag, N_Pragma,
4264 N_Function_Specification,
4265 N_Subprogram_Body)
4266 loop
4267 Prag := Parent (Prag);
4268 end loop;
4269
4270 if Nkind (Prag) /= N_Pragma then
4271 Error_Attr
4272 ("% attribute can only appear in postcondition of function",
4273 P);
4274
4275 elsif Get_Pragma_Id (Prag) = Pragma_Contract_Case
4276 or else
4277 Get_Pragma_Id (Prag) = Pragma_Test_Case
4278 then
4279 declare
4280 Arg_Ens : constant Node_Id :=
4281 Get_Ensures_From_CTC_Pragma (Prag);
4282 Arg : Node_Id;
4283
4284 begin
4285 Arg := N;
4286 while Arg /= Prag and Arg /= Arg_Ens loop
4287 Arg := Parent (Arg);
4288 end loop;
4289
4290 if Arg /= Arg_Ens then
4291 if Get_Pragma_Id (Prag) = Pragma_Contract_Case then
4292 Error_Attr
4293 ("% attribute misplaced inside contract case", P);
4294 else
4295 Error_Attr
4296 ("% attribute misplaced inside test case", P);
4297 end if;
4298 end if;
4299 end;
4300
4301 elsif Get_Pragma_Id (Prag) /= Pragma_Postcondition then
4302 Error_Attr
4303 ("% attribute can only appear in postcondition of function",
4304 P);
4305 end if;
4306
4307 -- The attribute reference is a primary. If expressions follow,
4308 -- the attribute reference is really an indexable object, so
4309 -- rewrite and analyze as an indexed component.
4310
4311 if Present (E1) then
4312 Rewrite (N,
4313 Make_Indexed_Component (Loc,
4314 Prefix =>
4315 Make_Attribute_Reference (Loc,
4316 Prefix => Relocate_Node (Prefix (N)),
4317 Attribute_Name => Name_Result),
4318 Expressions => Expressions (N)));
4319 Analyze (N);
4320 return;
4321 end if;
4322
4323 Set_Etype (N, Etype (CS));
4324
4325 -- If several functions with that name are visible,
4326 -- the intended one is the current scope.
4327
4328 if Is_Overloaded (P) then
4329 Set_Entity (P, CS);
4330 Set_Is_Overloaded (P, False);
4331 end if;
4332
4333 -- Body case, where we must be inside a generated _Postcondition
4334 -- procedure, and the prefix must be on the scope stack, or else
4335 -- the attribute use is definitely misplaced. The condition itself
4336 -- may have generated transient scopes, and is not necessarily the
4337 -- current one.
4338
4339 else
4340 while Present (CS) and then CS /= Standard_Standard loop
4341 if Chars (CS) = Name_uPostconditions then
4342 exit;
4343 else
4344 CS := Scope (CS);
4345 end if;
4346 end loop;
4347
4348 PS := Scope (CS);
4349
4350 if Chars (CS) = Name_uPostconditions
4351 and then Ekind (PS) = E_Function
4352 then
4353 -- Check OK prefix
4354
4355 if Nkind_In (P, N_Identifier, N_Operator_Symbol)
4356 and then Chars (P) = Chars (PS)
4357 then
4358 null;
4359
4360 -- Within an instance, the prefix designates the local renaming
4361 -- of the original generic.
4362
4363 elsif Is_Entity_Name (P)
4364 and then Ekind (Entity (P)) = E_Function
4365 and then Present (Alias (Entity (P)))
4366 and then Chars (Alias (Entity (P))) = Chars (PS)
4367 then
4368 null;
4369
4370 else
4371 Error_Msg_NE
4372 ("incorrect prefix for % attribute, expected &", P, PS);
4373 Error_Attr;
4374 end if;
4375
4376 Rewrite (N, Make_Identifier (Sloc (N), Name_uResult));
4377 Analyze_And_Resolve (N, Etype (PS));
4378
4379 else
4380 Error_Attr
4381 ("% attribute can only appear in postcondition of function",
4382 P);
4383 end if;
4384 end if;
4385 end Result;
4386
4387 ------------------
4388 -- Range_Length --
4389 ------------------
4390
4391 when Attribute_Range_Length =>
4392 Check_E0;
4393 Check_Discrete_Type;
4394 Set_Etype (N, Universal_Integer);
4395
4396 ----------
4397 -- Read --
4398 ----------
4399
4400 when Attribute_Read =>
4401 Check_E2;
4402 Check_Stream_Attribute (TSS_Stream_Read);
4403 Set_Etype (N, Standard_Void_Type);
4404 Resolve (N, Standard_Void_Type);
4405 Note_Possible_Modification (E2, Sure => True);
4406
4407 ---------
4408 -- Ref --
4409 ---------
4410
4411 when Attribute_Ref =>
4412 Check_E1;
4413 Analyze (P);
4414
4415 if Nkind (P) /= N_Expanded_Name
4416 or else not Is_RTE (P_Type, RE_Address)
4417 then
4418 Error_Attr_P ("prefix of % attribute must be System.Address");
4419 end if;
4420
4421 Analyze_And_Resolve (E1, Any_Integer);
4422 Set_Etype (N, RTE (RE_Address));
4423
4424 ---------------
4425 -- Remainder --
4426 ---------------
4427
4428 when Attribute_Remainder =>
4429 Check_Floating_Point_Type_2;
4430 Set_Etype (N, P_Base_Type);
4431 Resolve (E1, P_Base_Type);
4432 Resolve (E2, P_Base_Type);
4433
4434 -----------
4435 -- Round --
4436 -----------
4437
4438 when Attribute_Round =>
4439 Check_E1;
4440 Check_Decimal_Fixed_Point_Type;
4441 Set_Etype (N, P_Base_Type);
4442
4443 -- Because the context is universal_real (3.5.10(12)) it is a legal
4444 -- context for a universal fixed expression. This is the only
4445 -- attribute whose functional description involves U_R.
4446
4447 if Etype (E1) = Universal_Fixed then
4448 declare
4449 Conv : constant Node_Id := Make_Type_Conversion (Loc,
4450 Subtype_Mark => New_Occurrence_Of (Universal_Real, Loc),
4451 Expression => Relocate_Node (E1));
4452
4453 begin
4454 Rewrite (E1, Conv);
4455 Analyze (E1);
4456 end;
4457 end if;
4458
4459 Resolve (E1, Any_Real);
4460
4461 --------------
4462 -- Rounding --
4463 --------------
4464
4465 when Attribute_Rounding =>
4466 Check_Floating_Point_Type_1;
4467 Set_Etype (N, P_Base_Type);
4468 Resolve (E1, P_Base_Type);
4469
4470 ---------------
4471 -- Safe_Emax --
4472 ---------------
4473
4474 when Attribute_Safe_Emax =>
4475 Check_Floating_Point_Type_0;
4476 Set_Etype (N, Universal_Integer);
4477
4478 ----------------
4479 -- Safe_First --
4480 ----------------
4481
4482 when Attribute_Safe_First =>
4483 Check_Floating_Point_Type_0;
4484 Set_Etype (N, Universal_Real);
4485
4486 ----------------
4487 -- Safe_Large --
4488 ----------------
4489
4490 when Attribute_Safe_Large =>
4491 Check_E0;
4492 Check_Real_Type;
4493 Set_Etype (N, Universal_Real);
4494
4495 ---------------
4496 -- Safe_Last --
4497 ---------------
4498
4499 when Attribute_Safe_Last =>
4500 Check_Floating_Point_Type_0;
4501 Set_Etype (N, Universal_Real);
4502
4503 ----------------
4504 -- Safe_Small --
4505 ----------------
4506
4507 when Attribute_Safe_Small =>
4508 Check_E0;
4509 Check_Real_Type;
4510 Set_Etype (N, Universal_Real);
4511
4512 ------------------
4513 -- Same_Storage --
4514 ------------------
4515
4516 when Attribute_Same_Storage =>
4517 Check_Ada_2012_Attribute;
4518 Check_E1;
4519
4520 -- The arguments must be objects of any type
4521
4522 Analyze_And_Resolve (P);
4523 Analyze_And_Resolve (E1);
4524 Check_Object_Reference (P);
4525 Check_Object_Reference (E1);
4526 Set_Etype (N, Standard_Boolean);
4527
4528 --------------------------
4529 -- Scalar_Storage_Order --
4530 --------------------------
4531
4532 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order :
4533 begin
4534 Check_E0;
4535 Check_Type;
4536
4537 if not Is_Record_Type (P_Type) then
4538 Error_Attr_P ("prefix of % attribute must be record type");
4539 end if;
4540
4541 if Bytes_Big_Endian xor Reverse_Storage_Order (P_Type) then
4542 Rewrite (N,
4543 New_Occurrence_Of (RTE (RE_High_Order_First), Loc));
4544 else
4545 Rewrite (N,
4546 New_Occurrence_Of (RTE (RE_Low_Order_First), Loc));
4547 end if;
4548
4549 Set_Etype (N, RTE (RE_Bit_Order));
4550 Resolve (N);
4551
4552 -- Reset incorrect indication of staticness
4553
4554 Set_Is_Static_Expression (N, False);
4555 end Scalar_Storage_Order;
4556
4557 -----------
4558 -- Scale --
4559 -----------
4560
4561 when Attribute_Scale =>
4562 Check_E0;
4563 Check_Decimal_Fixed_Point_Type;
4564 Set_Etype (N, Universal_Integer);
4565
4566 -------------
4567 -- Scaling --
4568 -------------
4569
4570 when Attribute_Scaling =>
4571 Check_Floating_Point_Type_2;
4572 Set_Etype (N, P_Base_Type);
4573 Resolve (E1, P_Base_Type);
4574
4575 ------------------
4576 -- Signed_Zeros --
4577 ------------------
4578
4579 when Attribute_Signed_Zeros =>
4580 Check_Floating_Point_Type_0;
4581 Set_Etype (N, Standard_Boolean);
4582
4583 ----------
4584 -- Size --
4585 ----------
4586
4587 when Attribute_Size | Attribute_VADS_Size => Size :
4588 begin
4589 Check_E0;
4590
4591 -- If prefix is parameterless function call, rewrite and resolve
4592 -- as such.
4593
4594 if Is_Entity_Name (P)
4595 and then Ekind (Entity (P)) = E_Function
4596 then
4597 Resolve (P);
4598
4599 -- Similar processing for a protected function call
4600
4601 elsif Nkind (P) = N_Selected_Component
4602 and then Ekind (Entity (Selector_Name (P))) = E_Function
4603 then
4604 Resolve (P);
4605 end if;
4606
4607 if Is_Object_Reference (P) then
4608 Check_Object_Reference (P);
4609
4610 elsif Is_Entity_Name (P)
4611 and then (Is_Type (Entity (P))
4612 or else Ekind (Entity (P)) = E_Enumeration_Literal)
4613 then
4614 null;
4615
4616 elsif Nkind (P) = N_Type_Conversion
4617 and then not Comes_From_Source (P)
4618 then
4619 null;
4620
4621 else
4622 Error_Attr_P ("invalid prefix for % attribute");
4623 end if;
4624
4625 Check_Not_Incomplete_Type;
4626 Check_Not_CPP_Type;
4627 Set_Etype (N, Universal_Integer);
4628 end Size;
4629
4630 -----------
4631 -- Small --
4632 -----------
4633
4634 when Attribute_Small =>
4635 Check_E0;
4636 Check_Real_Type;
4637 Set_Etype (N, Universal_Real);
4638
4639 ------------------
4640 -- Storage_Pool --
4641 ------------------
4642
4643 when Attribute_Storage_Pool |
4644 Attribute_Simple_Storage_Pool => Storage_Pool :
4645 begin
4646 Check_E0;
4647
4648 if Is_Access_Type (P_Type) then
4649 if Ekind (P_Type) = E_Access_Subprogram_Type then
4650 Error_Attr_P
4651 ("cannot use % attribute for access-to-subprogram type");
4652 end if;
4653
4654 -- Set appropriate entity
4655
4656 if Present (Associated_Storage_Pool (Root_Type (P_Type))) then
4657 Set_Entity (N, Associated_Storage_Pool (Root_Type (P_Type)));
4658 else
4659 Set_Entity (N, RTE (RE_Global_Pool_Object));
4660 end if;
4661
4662 if Attr_Id = Attribute_Storage_Pool then
4663 if Present (Get_Rep_Pragma (Etype (Entity (N)),
4664 Name_Simple_Storage_Pool_Type))
4665 then
4666 Error_Msg_Name_1 := Aname;
4667 Error_Msg_N ("cannot use % attribute for type with simple " &
4668 "storage pool?", N);
4669 Error_Msg_N
4670 ("\Program_Error will be raised at run time?", N);
4671
4672 Rewrite
4673 (N, Make_Raise_Program_Error
4674 (Sloc (N), Reason => PE_Explicit_Raise));
4675 end if;
4676
4677 Set_Etype (N, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4678
4679 -- In the Simple_Storage_Pool case, verify that the pool entity is
4680 -- actually of a simple storage pool type, and set the attribute's
4681 -- type to the pool object's type.
4682
4683 else
4684 if not Present (Get_Rep_Pragma (Etype (Entity (N)),
4685 Name_Simple_Storage_Pool_Type))
4686 then
4687 Error_Attr_P
4688 ("cannot use % attribute for type without simple " &
4689 "storage pool");
4690 end if;
4691
4692 Set_Etype (N, Etype (Entity (N)));
4693 end if;
4694
4695 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4696 -- Storage_Pool since this attribute is not defined for such
4697 -- types (RM E.2.3(22)).
4698
4699 Validate_Remote_Access_To_Class_Wide_Type (N);
4700
4701 else
4702 Error_Attr_P ("prefix of % attribute must be access type");
4703 end if;
4704 end Storage_Pool;
4705
4706 ------------------
4707 -- Storage_Size --
4708 ------------------
4709
4710 when Attribute_Storage_Size => Storage_Size :
4711 begin
4712 Check_E0;
4713
4714 if Is_Task_Type (P_Type) then
4715 Set_Etype (N, Universal_Integer);
4716
4717 -- Use with tasks is an obsolescent feature
4718
4719 Check_Restriction (No_Obsolescent_Features, P);
4720
4721 elsif Is_Access_Type (P_Type) then
4722 if Ekind (P_Type) = E_Access_Subprogram_Type then
4723 Error_Attr_P
4724 ("cannot use % attribute for access-to-subprogram type");
4725 end if;
4726
4727 if Is_Entity_Name (P)
4728 and then Is_Type (Entity (P))
4729 then
4730 Check_Type;
4731 Set_Etype (N, Universal_Integer);
4732
4733 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
4734 -- Storage_Size since this attribute is not defined for
4735 -- such types (RM E.2.3(22)).
4736
4737 Validate_Remote_Access_To_Class_Wide_Type (N);
4738
4739 -- The prefix is allowed to be an implicit dereference
4740 -- of an access value designating a task.
4741
4742 else
4743 Check_Task_Prefix;
4744 Set_Etype (N, Universal_Integer);
4745 end if;
4746
4747 else
4748 Error_Attr_P ("prefix of % attribute must be access or task type");
4749 end if;
4750 end Storage_Size;
4751
4752 ------------------
4753 -- Storage_Unit --
4754 ------------------
4755
4756 when Attribute_Storage_Unit =>
4757 Standard_Attribute (Ttypes.System_Storage_Unit);
4758
4759 -----------------
4760 -- Stream_Size --
4761 -----------------
4762
4763 when Attribute_Stream_Size =>
4764 Check_E0;
4765 Check_Type;
4766
4767 if Is_Entity_Name (P)
4768 and then Is_Elementary_Type (Entity (P))
4769 then
4770 Set_Etype (N, Universal_Integer);
4771 else
4772 Error_Attr_P ("invalid prefix for % attribute");
4773 end if;
4774
4775 ---------------
4776 -- Stub_Type --
4777 ---------------
4778
4779 when Attribute_Stub_Type =>
4780 Check_Type;
4781 Check_E0;
4782
4783 if Is_Remote_Access_To_Class_Wide_Type (Base_Type (P_Type)) then
4784
4785 -- For a real RACW [sub]type, use corresponding stub type
4786
4787 if not Is_Generic_Type (P_Type) then
4788 Rewrite (N,
4789 New_Occurrence_Of
4790 (Corresponding_Stub_Type (Base_Type (P_Type)), Loc));
4791
4792 -- For a generic type (that has been marked as an RACW using the
4793 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
4794 -- type. Note that if the actual is not a remote access type, the
4795 -- instantiation will fail.
4796
4797 else
4798 -- Note: we go to the underlying type here because the view
4799 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
4800
4801 Rewrite (N,
4802 New_Occurrence_Of
4803 (Underlying_Type (RTE (RE_RACW_Stub_Type)), Loc));
4804 end if;
4805
4806 else
4807 Error_Attr_P
4808 ("prefix of% attribute must be remote access to classwide");
4809 end if;
4810
4811 ----------
4812 -- Succ --
4813 ----------
4814
4815 when Attribute_Succ =>
4816 Check_Scalar_Type;
4817 Check_E1;
4818
4819 if Is_Real_Type (P_Type) or else Is_Boolean_Type (P_Type) then
4820 Error_Msg_Name_1 := Aname;
4821 Error_Msg_Name_2 := Chars (P_Type);
4822 Check_SPARK_Restriction
4823 ("attribute% is not allowed for type%", P);
4824 end if;
4825
4826 Resolve (E1, P_Base_Type);
4827 Set_Etype (N, P_Base_Type);
4828
4829 -- Nothing to do for real type case
4830
4831 if Is_Real_Type (P_Type) then
4832 null;
4833
4834 -- If not modular type, test for overflow check required
4835
4836 else
4837 if not Is_Modular_Integer_Type (P_Type)
4838 and then not Range_Checks_Suppressed (P_Base_Type)
4839 then
4840 Enable_Range_Check (E1);
4841 end if;
4842 end if;
4843
4844 --------------------------------
4845 -- System_Allocator_Alignment --
4846 --------------------------------
4847
4848 when Attribute_System_Allocator_Alignment =>
4849 Standard_Attribute (Ttypes.System_Allocator_Alignment);
4850
4851 ---------
4852 -- Tag --
4853 ---------
4854
4855 when Attribute_Tag => Tag :
4856 begin
4857 Check_E0;
4858 Check_Dereference;
4859
4860 if not Is_Tagged_Type (P_Type) then
4861 Error_Attr_P ("prefix of % attribute must be tagged");
4862
4863 -- Next test does not apply to generated code
4864 -- why not, and what does the illegal reference mean???
4865
4866 elsif Is_Object_Reference (P)
4867 and then not Is_Class_Wide_Type (P_Type)
4868 and then Comes_From_Source (N)
4869 then
4870 Error_Attr_P
4871 ("% attribute can only be applied to objects " &
4872 "of class - wide type");
4873 end if;
4874
4875 -- The prefix cannot be an incomplete type. However, references
4876 -- to 'Tag can be generated when expanding interface conversions,
4877 -- and this is legal.
4878
4879 if Comes_From_Source (N) then
4880 Check_Not_Incomplete_Type;
4881 end if;
4882
4883 -- Set appropriate type
4884
4885 Set_Etype (N, RTE (RE_Tag));
4886 end Tag;
4887
4888 -----------------
4889 -- Target_Name --
4890 -----------------
4891
4892 when Attribute_Target_Name => Target_Name : declare
4893 TN : constant String := Sdefault.Target_Name.all;
4894 TL : Natural;
4895
4896 begin
4897 Check_Standard_Prefix;
4898
4899 TL := TN'Last;
4900
4901 if TN (TL) = '/' or else TN (TL) = '\' then
4902 TL := TL - 1;
4903 end if;
4904
4905 Rewrite (N,
4906 Make_String_Literal (Loc,
4907 Strval => TN (TN'First .. TL)));
4908 Analyze_And_Resolve (N, Standard_String);
4909 end Target_Name;
4910
4911 ----------------
4912 -- Terminated --
4913 ----------------
4914
4915 when Attribute_Terminated =>
4916 Check_E0;
4917 Set_Etype (N, Standard_Boolean);
4918 Check_Task_Prefix;
4919
4920 ----------------
4921 -- To_Address --
4922 ----------------
4923
4924 when Attribute_To_Address =>
4925 Check_E1;
4926 Analyze (P);
4927
4928 if Nkind (P) /= N_Identifier
4929 or else Chars (P) /= Name_System
4930 then
4931 Error_Attr_P ("prefix of % attribute must be System");
4932 end if;
4933
4934 Generate_Reference (RTE (RE_Address), P);
4935 Analyze_And_Resolve (E1, Any_Integer);
4936 Set_Etype (N, RTE (RE_Address));
4937
4938 ------------
4939 -- To_Any --
4940 ------------
4941
4942 when Attribute_To_Any =>
4943 Check_E1;
4944 Check_PolyORB_Attribute;
4945 Set_Etype (N, RTE (RE_Any));
4946
4947 ----------------
4948 -- Truncation --
4949 ----------------
4950
4951 when Attribute_Truncation =>
4952 Check_Floating_Point_Type_1;
4953 Resolve (E1, P_Base_Type);
4954 Set_Etype (N, P_Base_Type);
4955
4956 ----------------
4957 -- Type_Class --
4958 ----------------
4959
4960 when Attribute_Type_Class =>
4961 Check_E0;
4962 Check_Type;
4963 Check_Not_Incomplete_Type;
4964 Set_Etype (N, RTE (RE_Type_Class));
4965
4966 --------------
4967 -- TypeCode --
4968 --------------
4969
4970 when Attribute_TypeCode =>
4971 Check_E0;
4972 Check_PolyORB_Attribute;
4973 Set_Etype (N, RTE (RE_TypeCode));
4974
4975 --------------
4976 -- Type_Key --
4977 --------------
4978
4979 when Attribute_Type_Key =>
4980 Check_E0;
4981 Check_Type;
4982
4983 -- This processing belongs in Eval_Attribute ???
4984
4985 declare
4986 function Type_Key return String_Id;
4987 -- A very preliminary implementation. For now, a signature
4988 -- consists of only the type name. This is clearly incomplete
4989 -- (e.g., adding a new field to a record type should change the
4990 -- type's Type_Key attribute).
4991
4992 --------------
4993 -- Type_Key --
4994 --------------
4995
4996 function Type_Key return String_Id is
4997 Full_Name : constant String_Id :=
4998 Fully_Qualified_Name_String (Entity (P));
4999
5000 begin
5001 -- Copy all characters in Full_Name but the trailing NUL
5002
5003 Start_String;
5004 for J in 1 .. String_Length (Full_Name) - 1 loop
5005 Store_String_Char (Get_String_Char (Full_Name, Int (J)));
5006 end loop;
5007
5008 Store_String_Chars ("'Type_Key");
5009 return End_String;
5010 end Type_Key;
5011
5012 begin
5013 Rewrite (N, Make_String_Literal (Loc, Type_Key));
5014 end;
5015
5016 Analyze_And_Resolve (N, Standard_String);
5017
5018 -----------------
5019 -- UET_Address --
5020 -----------------
5021
5022 when Attribute_UET_Address =>
5023 Check_E0;
5024 Check_Unit_Name (P);
5025 Set_Etype (N, RTE (RE_Address));
5026
5027 -----------------------
5028 -- Unbiased_Rounding --
5029 -----------------------
5030
5031 when Attribute_Unbiased_Rounding =>
5032 Check_Floating_Point_Type_1;
5033 Set_Etype (N, P_Base_Type);
5034 Resolve (E1, P_Base_Type);
5035
5036 ----------------------
5037 -- Unchecked_Access --
5038 ----------------------
5039
5040 when Attribute_Unchecked_Access =>
5041 if Comes_From_Source (N) then
5042 Check_Restriction (No_Unchecked_Access, N);
5043 end if;
5044
5045 Analyze_Access_Attribute;
5046
5047 -------------------------
5048 -- Unconstrained_Array --
5049 -------------------------
5050
5051 when Attribute_Unconstrained_Array =>
5052 Check_E0;
5053 Check_Type;
5054 Check_Not_Incomplete_Type;
5055 Set_Etype (N, Standard_Boolean);
5056
5057 ------------------------------
5058 -- Universal_Literal_String --
5059 ------------------------------
5060
5061 -- This is a GNAT specific attribute whose prefix must be a named
5062 -- number where the expression is either a single numeric literal,
5063 -- or a numeric literal immediately preceded by a minus sign. The
5064 -- result is equivalent to a string literal containing the text of
5065 -- the literal as it appeared in the source program with a possible
5066 -- leading minus sign.
5067
5068 when Attribute_Universal_Literal_String => Universal_Literal_String :
5069 begin
5070 Check_E0;
5071
5072 if not Is_Entity_Name (P)
5073 or else Ekind (Entity (P)) not in Named_Kind
5074 then
5075 Error_Attr_P ("prefix for % attribute must be named number");
5076
5077 else
5078 declare
5079 Expr : Node_Id;
5080 Negative : Boolean;
5081 S : Source_Ptr;
5082 Src : Source_Buffer_Ptr;
5083
5084 begin
5085 Expr := Original_Node (Expression (Parent (Entity (P))));
5086
5087 if Nkind (Expr) = N_Op_Minus then
5088 Negative := True;
5089 Expr := Original_Node (Right_Opnd (Expr));
5090 else
5091 Negative := False;
5092 end if;
5093
5094 if not Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
5095 Error_Attr
5096 ("named number for % attribute must be simple literal", N);
5097 end if;
5098
5099 -- Build string literal corresponding to source literal text
5100
5101 Start_String;
5102
5103 if Negative then
5104 Store_String_Char (Get_Char_Code ('-'));
5105 end if;
5106
5107 S := Sloc (Expr);
5108 Src := Source_Text (Get_Source_File_Index (S));
5109
5110 while Src (S) /= ';' and then Src (S) /= ' ' loop
5111 Store_String_Char (Get_Char_Code (Src (S)));
5112 S := S + 1;
5113 end loop;
5114
5115 -- Now we rewrite the attribute with the string literal
5116
5117 Rewrite (N,
5118 Make_String_Literal (Loc, End_String));
5119 Analyze (N);
5120 end;
5121 end if;
5122 end Universal_Literal_String;
5123
5124 -------------------------
5125 -- Unrestricted_Access --
5126 -------------------------
5127
5128 -- This is a GNAT specific attribute which is like Access except that
5129 -- all scope checks and checks for aliased views are omitted.
5130
5131 when Attribute_Unrestricted_Access =>
5132
5133 -- If from source, deal with relevant restrictions
5134
5135 if Comes_From_Source (N) then
5136 Check_Restriction (No_Unchecked_Access, N);
5137
5138 if Nkind (P) in N_Has_Entity
5139 and then Present (Entity (P))
5140 and then Is_Object (Entity (P))
5141 then
5142 Check_Restriction (No_Implicit_Aliasing, N);
5143 end if;
5144 end if;
5145
5146 if Is_Entity_Name (P) then
5147 Set_Address_Taken (Entity (P));
5148 end if;
5149
5150 Analyze_Access_Attribute;
5151
5152 ---------
5153 -- Val --
5154 ---------
5155
5156 when Attribute_Val => Val : declare
5157 begin
5158 Check_E1;
5159 Check_Discrete_Type;
5160
5161 if Is_Boolean_Type (P_Type) then
5162 Error_Msg_Name_1 := Aname;
5163 Error_Msg_Name_2 := Chars (P_Type);
5164 Check_SPARK_Restriction
5165 ("attribute% is not allowed for type%", P);
5166 end if;
5167
5168 Resolve (E1, Any_Integer);
5169 Set_Etype (N, P_Base_Type);
5170
5171 -- Note, we need a range check in general, but we wait for the
5172 -- Resolve call to do this, since we want to let Eval_Attribute
5173 -- have a chance to find an static illegality first!
5174 end Val;
5175
5176 -----------
5177 -- Valid --
5178 -----------
5179
5180 when Attribute_Valid =>
5181 Check_E0;
5182
5183 -- Ignore check for object if we have a 'Valid reference generated
5184 -- by the expanded code, since in some cases valid checks can occur
5185 -- on items that are names, but are not objects (e.g. attributes).
5186
5187 if Comes_From_Source (N) then
5188 Check_Object_Reference (P);
5189 end if;
5190
5191 if not Is_Scalar_Type (P_Type) then
5192 Error_Attr_P ("object for % attribute must be of scalar type");
5193 end if;
5194
5195 Set_Etype (N, Standard_Boolean);
5196
5197 -------------------
5198 -- Valid_Scalars --
5199 -------------------
5200
5201 when Attribute_Valid_Scalars =>
5202 Check_E0;
5203 Check_Object_Reference (P);
5204
5205 if No_Scalar_Parts (P_Type) then
5206 Error_Attr_P ("?attribute % always True, no scalars to check");
5207 end if;
5208
5209 Set_Etype (N, Standard_Boolean);
5210
5211 -----------
5212 -- Value --
5213 -----------
5214
5215 when Attribute_Value => Value :
5216 begin
5217 Check_SPARK_Restriction_On_Attribute;
5218 Check_E1;
5219 Check_Scalar_Type;
5220
5221 -- Case of enumeration type
5222
5223 -- When an enumeration type appears in an attribute reference, all
5224 -- literals of the type are marked as referenced. This must only be
5225 -- done if the attribute reference appears in the current source.
5226 -- Otherwise the information on references may differ between a
5227 -- normal compilation and one that performs inlining.
5228
5229 if Is_Enumeration_Type (P_Type)
5230 and then In_Extended_Main_Code_Unit (N)
5231 then
5232 Check_Restriction (No_Enumeration_Maps, N);
5233
5234 -- Mark all enumeration literals as referenced, since the use of
5235 -- the Value attribute can implicitly reference any of the
5236 -- literals of the enumeration base type.
5237
5238 declare
5239 Ent : Entity_Id := First_Literal (P_Base_Type);
5240 begin
5241 while Present (Ent) loop
5242 Set_Referenced (Ent);
5243 Next_Literal (Ent);
5244 end loop;
5245 end;
5246 end if;
5247
5248 -- Set Etype before resolving expression because expansion of
5249 -- expression may require enclosing type. Note that the type
5250 -- returned by 'Value is the base type of the prefix type.
5251
5252 Set_Etype (N, P_Base_Type);
5253 Validate_Non_Static_Attribute_Function_Call;
5254 end Value;
5255
5256 ----------------
5257 -- Value_Size --
5258 ----------------
5259
5260 when Attribute_Value_Size =>
5261 Check_E0;
5262 Check_Type;
5263 Check_Not_Incomplete_Type;
5264 Set_Etype (N, Universal_Integer);
5265
5266 -------------
5267 -- Version --
5268 -------------
5269
5270 when Attribute_Version =>
5271 Check_E0;
5272 Check_Program_Unit;
5273 Set_Etype (N, RTE (RE_Version_String));
5274
5275 ------------------
5276 -- Wchar_T_Size --
5277 ------------------
5278
5279 when Attribute_Wchar_T_Size =>
5280 Standard_Attribute (Interfaces_Wchar_T_Size);
5281
5282 ----------------
5283 -- Wide_Image --
5284 ----------------
5285
5286 when Attribute_Wide_Image => Wide_Image :
5287 begin
5288 Check_SPARK_Restriction_On_Attribute;
5289 Check_Scalar_Type;
5290 Set_Etype (N, Standard_Wide_String);
5291 Check_E1;
5292 Resolve (E1, P_Base_Type);
5293 Validate_Non_Static_Attribute_Function_Call;
5294 end Wide_Image;
5295
5296 ---------------------
5297 -- Wide_Wide_Image --
5298 ---------------------
5299
5300 when Attribute_Wide_Wide_Image => Wide_Wide_Image :
5301 begin
5302 Check_Scalar_Type;
5303 Set_Etype (N, Standard_Wide_Wide_String);
5304 Check_E1;
5305 Resolve (E1, P_Base_Type);
5306 Validate_Non_Static_Attribute_Function_Call;
5307 end Wide_Wide_Image;
5308
5309 ----------------
5310 -- Wide_Value --
5311 ----------------
5312
5313 when Attribute_Wide_Value => Wide_Value :
5314 begin
5315 Check_SPARK_Restriction_On_Attribute;
5316 Check_E1;
5317 Check_Scalar_Type;
5318
5319 -- Set Etype before resolving expression because expansion
5320 -- of expression may require enclosing type.
5321
5322 Set_Etype (N, P_Type);
5323 Validate_Non_Static_Attribute_Function_Call;
5324 end Wide_Value;
5325
5326 ---------------------
5327 -- Wide_Wide_Value --
5328 ---------------------
5329
5330 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
5331 begin
5332 Check_E1;
5333 Check_Scalar_Type;
5334
5335 -- Set Etype before resolving expression because expansion
5336 -- of expression may require enclosing type.
5337
5338 Set_Etype (N, P_Type);
5339 Validate_Non_Static_Attribute_Function_Call;
5340 end Wide_Wide_Value;
5341
5342 ---------------------
5343 -- Wide_Wide_Width --
5344 ---------------------
5345
5346 when Attribute_Wide_Wide_Width =>
5347 Check_E0;
5348 Check_Scalar_Type;
5349 Set_Etype (N, Universal_Integer);
5350
5351 ----------------
5352 -- Wide_Width --
5353 ----------------
5354
5355 when Attribute_Wide_Width =>
5356 Check_SPARK_Restriction_On_Attribute;
5357 Check_E0;
5358 Check_Scalar_Type;
5359 Set_Etype (N, Universal_Integer);
5360
5361 -----------
5362 -- Width --
5363 -----------
5364
5365 when Attribute_Width =>
5366 Check_SPARK_Restriction_On_Attribute;
5367 Check_E0;
5368 Check_Scalar_Type;
5369 Set_Etype (N, Universal_Integer);
5370
5371 ---------------
5372 -- Word_Size --
5373 ---------------
5374
5375 when Attribute_Word_Size =>
5376 Standard_Attribute (System_Word_Size);
5377
5378 -----------
5379 -- Write --
5380 -----------
5381
5382 when Attribute_Write =>
5383 Check_E2;
5384 Check_Stream_Attribute (TSS_Stream_Write);
5385 Set_Etype (N, Standard_Void_Type);
5386 Resolve (N, Standard_Void_Type);
5387
5388 end case;
5389
5390 -- All errors raise Bad_Attribute, so that we get out before any further
5391 -- damage occurs when an error is detected (for example, if we check for
5392 -- one attribute expression, and the check succeeds, we want to be able
5393 -- to proceed securely assuming that an expression is in fact present.
5394
5395 -- Note: we set the attribute analyzed in this case to prevent any
5396 -- attempt at reanalysis which could generate spurious error msgs.
5397
5398 exception
5399 when Bad_Attribute =>
5400 Set_Analyzed (N);
5401 Set_Etype (N, Any_Type);
5402 return;
5403 end Analyze_Attribute;
5404
5405 --------------------
5406 -- Eval_Attribute --
5407 --------------------
5408
5409 procedure Eval_Attribute (N : Node_Id) is
5410 Loc : constant Source_Ptr := Sloc (N);
5411 Aname : constant Name_Id := Attribute_Name (N);
5412 Id : constant Attribute_Id := Get_Attribute_Id (Aname);
5413 P : constant Node_Id := Prefix (N);
5414
5415 C_Type : constant Entity_Id := Etype (N);
5416 -- The type imposed by the context
5417
5418 E1 : Node_Id;
5419 -- First expression, or Empty if none
5420
5421 E2 : Node_Id;
5422 -- Second expression, or Empty if none
5423
5424 P_Entity : Entity_Id;
5425 -- Entity denoted by prefix
5426
5427 P_Type : Entity_Id;
5428 -- The type of the prefix
5429
5430 P_Base_Type : Entity_Id;
5431 -- The base type of the prefix type
5432
5433 P_Root_Type : Entity_Id;
5434 -- The root type of the prefix type
5435
5436 Static : Boolean;
5437 -- True if the result is Static. This is set by the general processing
5438 -- to true if the prefix is static, and all expressions are static. It
5439 -- can be reset as processing continues for particular attributes
5440
5441 Lo_Bound, Hi_Bound : Node_Id;
5442 -- Expressions for low and high bounds of type or array index referenced
5443 -- by First, Last, or Length attribute for array, set by Set_Bounds.
5444
5445 CE_Node : Node_Id;
5446 -- Constraint error node used if we have an attribute reference has
5447 -- an argument that raises a constraint error. In this case we replace
5448 -- the attribute with a raise constraint_error node. This is important
5449 -- processing, since otherwise gigi might see an attribute which it is
5450 -- unprepared to deal with.
5451
5452 procedure Check_Concurrent_Discriminant (Bound : Node_Id);
5453 -- If Bound is a reference to a discriminant of a task or protected type
5454 -- occurring within the object's body, rewrite attribute reference into
5455 -- a reference to the corresponding discriminal. Use for the expansion
5456 -- of checks against bounds of entry family index subtypes.
5457
5458 procedure Check_Expressions;
5459 -- In case where the attribute is not foldable, the expressions, if
5460 -- any, of the attribute, are in a non-static context. This procedure
5461 -- performs the required additional checks.
5462
5463 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean;
5464 -- Determines if the given type has compile time known bounds. Note
5465 -- that we enter the case statement even in cases where the prefix
5466 -- type does NOT have known bounds, so it is important to guard any
5467 -- attempt to evaluate both bounds with a call to this function.
5468
5469 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint);
5470 -- This procedure is called when the attribute N has a non-static
5471 -- but compile time known value given by Val. It includes the
5472 -- necessary checks for out of range values.
5473
5474 function Fore_Value return Nat;
5475 -- Computes the Fore value for the current attribute prefix, which is
5476 -- known to be a static fixed-point type. Used by Fore and Width.
5477
5478 function Is_VAX_Float (Typ : Entity_Id) return Boolean;
5479 -- Determine whether Typ denotes a VAX floating point type
5480
5481 function Mantissa return Uint;
5482 -- Returns the Mantissa value for the prefix type
5483
5484 procedure Set_Bounds;
5485 -- Used for First, Last and Length attributes applied to an array or
5486 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
5487 -- and high bound expressions for the index referenced by the attribute
5488 -- designator (i.e. the first index if no expression is present, and the
5489 -- N'th index if the value N is present as an expression). Also used for
5490 -- First and Last of scalar types and for First_Valid and Last_Valid.
5491 -- Static is reset to False if the type or index type is not statically
5492 -- constrained.
5493
5494 function Statically_Denotes_Entity (N : Node_Id) return Boolean;
5495 -- Verify that the prefix of a potentially static array attribute
5496 -- satisfies the conditions of 4.9 (14).
5497
5498 -----------------------------------
5499 -- Check_Concurrent_Discriminant --
5500 -----------------------------------
5501
5502 procedure Check_Concurrent_Discriminant (Bound : Node_Id) is
5503 Tsk : Entity_Id;
5504 -- The concurrent (task or protected) type
5505
5506 begin
5507 if Nkind (Bound) = N_Identifier
5508 and then Ekind (Entity (Bound)) = E_Discriminant
5509 and then Is_Concurrent_Record_Type (Scope (Entity (Bound)))
5510 then
5511 Tsk := Corresponding_Concurrent_Type (Scope (Entity (Bound)));
5512
5513 if In_Open_Scopes (Tsk) and then Has_Completion (Tsk) then
5514
5515 -- Find discriminant of original concurrent type, and use
5516 -- its current discriminal, which is the renaming within
5517 -- the task/protected body.
5518
5519 Rewrite (N,
5520 New_Occurrence_Of
5521 (Find_Body_Discriminal (Entity (Bound)), Loc));
5522 end if;
5523 end if;
5524 end Check_Concurrent_Discriminant;
5525
5526 -----------------------
5527 -- Check_Expressions --
5528 -----------------------
5529
5530 procedure Check_Expressions is
5531 E : Node_Id;
5532 begin
5533 E := E1;
5534 while Present (E) loop
5535 Check_Non_Static_Context (E);
5536 Next (E);
5537 end loop;
5538 end Check_Expressions;
5539
5540 ----------------------------------
5541 -- Compile_Time_Known_Attribute --
5542 ----------------------------------
5543
5544 procedure Compile_Time_Known_Attribute (N : Node_Id; Val : Uint) is
5545 T : constant Entity_Id := Etype (N);
5546
5547 begin
5548 Fold_Uint (N, Val, False);
5549
5550 -- Check that result is in bounds of the type if it is static
5551
5552 if Is_In_Range (N, T, Assume_Valid => False) then
5553 null;
5554
5555 elsif Is_Out_Of_Range (N, T) then
5556 Apply_Compile_Time_Constraint_Error
5557 (N, "value not in range of}?", CE_Range_Check_Failed);
5558
5559 elsif not Range_Checks_Suppressed (T) then
5560 Enable_Range_Check (N);
5561
5562 else
5563 Set_Do_Range_Check (N, False);
5564 end if;
5565 end Compile_Time_Known_Attribute;
5566
5567 -------------------------------
5568 -- Compile_Time_Known_Bounds --
5569 -------------------------------
5570
5571 function Compile_Time_Known_Bounds (Typ : Entity_Id) return Boolean is
5572 begin
5573 return
5574 Compile_Time_Known_Value (Type_Low_Bound (Typ))
5575 and then
5576 Compile_Time_Known_Value (Type_High_Bound (Typ));
5577 end Compile_Time_Known_Bounds;
5578
5579 ----------------
5580 -- Fore_Value --
5581 ----------------
5582
5583 -- Note that the Fore calculation is based on the actual values
5584 -- of the bounds, and does not take into account possible rounding.
5585
5586 function Fore_Value return Nat is
5587 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
5588 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
5589 Small : constant Ureal := Small_Value (P_Type);
5590 Lo_Real : constant Ureal := Lo * Small;
5591 Hi_Real : constant Ureal := Hi * Small;
5592 T : Ureal;
5593 R : Nat;
5594
5595 begin
5596 -- Bounds are given in terms of small units, so first compute
5597 -- proper values as reals.
5598
5599 T := UR_Max (abs Lo_Real, abs Hi_Real);
5600 R := 2;
5601
5602 -- Loop to compute proper value if more than one digit required
5603
5604 while T >= Ureal_10 loop
5605 R := R + 1;
5606 T := T / Ureal_10;
5607 end loop;
5608
5609 return R;
5610 end Fore_Value;
5611
5612 ------------------
5613 -- Is_VAX_Float --
5614 ------------------
5615
5616 function Is_VAX_Float (Typ : Entity_Id) return Boolean is
5617 begin
5618 return
5619 Is_Floating_Point_Type (Typ)
5620 and then
5621 (Float_Format = 'V'
5622 or else Float_Rep (Typ) = VAX_Native);
5623 end Is_VAX_Float;
5624
5625 --------------
5626 -- Mantissa --
5627 --------------
5628
5629 -- Table of mantissa values accessed by function Computed using
5630 -- the relation:
5631
5632 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
5633
5634 -- where D is T'Digits (RM83 3.5.7)
5635
5636 Mantissa_Value : constant array (Nat range 1 .. 40) of Nat := (
5637 1 => 5,
5638 2 => 8,
5639 3 => 11,
5640 4 => 15,
5641 5 => 18,
5642 6 => 21,
5643 7 => 25,
5644 8 => 28,
5645 9 => 31,
5646 10 => 35,
5647 11 => 38,
5648 12 => 41,
5649 13 => 45,
5650 14 => 48,
5651 15 => 51,
5652 16 => 55,
5653 17 => 58,
5654 18 => 61,
5655 19 => 65,
5656 20 => 68,
5657 21 => 71,
5658 22 => 75,
5659 23 => 78,
5660 24 => 81,
5661 25 => 85,
5662 26 => 88,
5663 27 => 91,
5664 28 => 95,
5665 29 => 98,
5666 30 => 101,
5667 31 => 104,
5668 32 => 108,
5669 33 => 111,
5670 34 => 114,
5671 35 => 118,
5672 36 => 121,
5673 37 => 124,
5674 38 => 128,
5675 39 => 131,
5676 40 => 134);
5677
5678 function Mantissa return Uint is
5679 begin
5680 return
5681 UI_From_Int (Mantissa_Value (UI_To_Int (Digits_Value (P_Type))));
5682 end Mantissa;
5683
5684 ----------------
5685 -- Set_Bounds --
5686 ----------------
5687
5688 procedure Set_Bounds is
5689 Ndim : Nat;
5690 Indx : Node_Id;
5691 Ityp : Entity_Id;
5692
5693 begin
5694 -- For a string literal subtype, we have to construct the bounds.
5695 -- Valid Ada code never applies attributes to string literals, but
5696 -- it is convenient to allow the expander to generate attribute
5697 -- references of this type (e.g. First and Last applied to a string
5698 -- literal).
5699
5700 -- Note that the whole point of the E_String_Literal_Subtype is to
5701 -- avoid this construction of bounds, but the cases in which we
5702 -- have to materialize them are rare enough that we don't worry!
5703
5704 -- The low bound is simply the low bound of the base type. The
5705 -- high bound is computed from the length of the string and this
5706 -- low bound.
5707
5708 if Ekind (P_Type) = E_String_Literal_Subtype then
5709 Ityp := Etype (First_Index (Base_Type (P_Type)));
5710 Lo_Bound := Type_Low_Bound (Ityp);
5711
5712 Hi_Bound :=
5713 Make_Integer_Literal (Sloc (P),
5714 Intval =>
5715 Expr_Value (Lo_Bound) + String_Literal_Length (P_Type) - 1);
5716
5717 Set_Parent (Hi_Bound, P);
5718 Analyze_And_Resolve (Hi_Bound, Etype (Lo_Bound));
5719 return;
5720
5721 -- For non-array case, just get bounds of scalar type
5722
5723 elsif Is_Scalar_Type (P_Type) then
5724 Ityp := P_Type;
5725
5726 -- For a fixed-point type, we must freeze to get the attributes
5727 -- of the fixed-point type set now so we can reference them.
5728
5729 if Is_Fixed_Point_Type (P_Type)
5730 and then not Is_Frozen (Base_Type (P_Type))
5731 and then Compile_Time_Known_Value (Type_Low_Bound (P_Type))
5732 and then Compile_Time_Known_Value (Type_High_Bound (P_Type))
5733 then
5734 Freeze_Fixed_Point_Type (Base_Type (P_Type));
5735 end if;
5736
5737 -- For array case, get type of proper index
5738
5739 else
5740 if No (E1) then
5741 Ndim := 1;
5742 else
5743 Ndim := UI_To_Int (Expr_Value (E1));
5744 end if;
5745
5746 Indx := First_Index (P_Type);
5747 for J in 1 .. Ndim - 1 loop
5748 Next_Index (Indx);
5749 end loop;
5750
5751 -- If no index type, get out (some other error occurred, and
5752 -- we don't have enough information to complete the job!)
5753
5754 if No (Indx) then
5755 Lo_Bound := Error;
5756 Hi_Bound := Error;
5757 return;
5758 end if;
5759
5760 Ityp := Etype (Indx);
5761 end if;
5762
5763 -- A discrete range in an index constraint is allowed to be a
5764 -- subtype indication. This is syntactically a pain, but should
5765 -- not propagate to the entity for the corresponding index subtype.
5766 -- After checking that the subtype indication is legal, the range
5767 -- of the subtype indication should be transfered to the entity.
5768 -- The attributes for the bounds should remain the simple retrievals
5769 -- that they are now.
5770
5771 Lo_Bound := Type_Low_Bound (Ityp);
5772 Hi_Bound := Type_High_Bound (Ityp);
5773
5774 if not Is_Static_Subtype (Ityp) then
5775 Static := False;
5776 end if;
5777 end Set_Bounds;
5778
5779 -------------------------------
5780 -- Statically_Denotes_Entity --
5781 -------------------------------
5782
5783 function Statically_Denotes_Entity (N : Node_Id) return Boolean is
5784 E : Entity_Id;
5785
5786 begin
5787 if not Is_Entity_Name (N) then
5788 return False;
5789 else
5790 E := Entity (N);
5791 end if;
5792
5793 return
5794 Nkind (Parent (E)) /= N_Object_Renaming_Declaration
5795 or else Statically_Denotes_Entity (Renamed_Object (E));
5796 end Statically_Denotes_Entity;
5797
5798 -- Start of processing for Eval_Attribute
5799
5800 begin
5801 -- Acquire first two expressions (at the moment, no attributes take more
5802 -- than two expressions in any case).
5803
5804 if Present (Expressions (N)) then
5805 E1 := First (Expressions (N));
5806 E2 := Next (E1);
5807 else
5808 E1 := Empty;
5809 E2 := Empty;
5810 end if;
5811
5812 -- Special processing for Enabled attribute. This attribute has a very
5813 -- special prefix, and the easiest way to avoid lots of special checks
5814 -- to protect this special prefix from causing trouble is to deal with
5815 -- this attribute immediately and be done with it.
5816
5817 if Id = Attribute_Enabled then
5818
5819 -- We skip evaluation if the expander is not active. This is not just
5820 -- an optimization. It is of key importance that we not rewrite the
5821 -- attribute in a generic template, since we want to pick up the
5822 -- setting of the check in the instance, and testing expander active
5823 -- is as easy way of doing this as any.
5824
5825 if Expander_Active then
5826 declare
5827 C : constant Check_Id := Get_Check_Id (Chars (P));
5828 R : Boolean;
5829
5830 begin
5831 if No (E1) then
5832 if C in Predefined_Check_Id then
5833 R := Scope_Suppress (C);
5834 else
5835 R := Is_Check_Suppressed (Empty, C);
5836 end if;
5837
5838 else
5839 R := Is_Check_Suppressed (Entity (E1), C);
5840 end if;
5841
5842 if R then
5843 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5844 else
5845 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5846 end if;
5847 end;
5848 end if;
5849
5850 return;
5851 end if;
5852
5853 -- Special processing for cases where the prefix is an object. For
5854 -- this purpose, a string literal counts as an object (attributes
5855 -- of string literals can only appear in generated code).
5856
5857 if Is_Object_Reference (P) or else Nkind (P) = N_String_Literal then
5858
5859 -- For Component_Size, the prefix is an array object, and we apply
5860 -- the attribute to the type of the object. This is allowed for
5861 -- both unconstrained and constrained arrays, since the bounds
5862 -- have no influence on the value of this attribute.
5863
5864 if Id = Attribute_Component_Size then
5865 P_Entity := Etype (P);
5866
5867 -- For First and Last, the prefix is an array object, and we apply
5868 -- the attribute to the type of the array, but we need a constrained
5869 -- type for this, so we use the actual subtype if available.
5870
5871 elsif Id = Attribute_First
5872 or else
5873 Id = Attribute_Last
5874 or else
5875 Id = Attribute_Length
5876 then
5877 declare
5878 AS : constant Entity_Id := Get_Actual_Subtype_If_Available (P);
5879
5880 begin
5881 if Present (AS) and then Is_Constrained (AS) then
5882 P_Entity := AS;
5883
5884 -- If we have an unconstrained type we cannot fold
5885
5886 else
5887 Check_Expressions;
5888 return;
5889 end if;
5890 end;
5891
5892 -- For Size, give size of object if available, otherwise we
5893 -- cannot fold Size.
5894
5895 elsif Id = Attribute_Size then
5896 if Is_Entity_Name (P)
5897 and then Known_Esize (Entity (P))
5898 then
5899 Compile_Time_Known_Attribute (N, Esize (Entity (P)));
5900 return;
5901
5902 else
5903 Check_Expressions;
5904 return;
5905 end if;
5906
5907 -- For Alignment, give size of object if available, otherwise we
5908 -- cannot fold Alignment.
5909
5910 elsif Id = Attribute_Alignment then
5911 if Is_Entity_Name (P)
5912 and then Known_Alignment (Entity (P))
5913 then
5914 Fold_Uint (N, Alignment (Entity (P)), False);
5915 return;
5916
5917 else
5918 Check_Expressions;
5919 return;
5920 end if;
5921
5922 -- No other attributes for objects are folded
5923
5924 else
5925 Check_Expressions;
5926 return;
5927 end if;
5928
5929 -- Cases where P is not an object. Cannot do anything if P is
5930 -- not the name of an entity.
5931
5932 elsif not Is_Entity_Name (P) then
5933 Check_Expressions;
5934 return;
5935
5936 -- Otherwise get prefix entity
5937
5938 else
5939 P_Entity := Entity (P);
5940 end if;
5941
5942 -- At this stage P_Entity is the entity to which the attribute
5943 -- is to be applied. This is usually simply the entity of the
5944 -- prefix, except in some cases of attributes for objects, where
5945 -- as described above, we apply the attribute to the object type.
5946
5947 -- First foldable possibility is a scalar or array type (RM 4.9(7))
5948 -- that is not generic (generic types are eliminated by RM 4.9(25)).
5949 -- Note we allow non-static non-generic types at this stage as further
5950 -- described below.
5951
5952 if Is_Type (P_Entity)
5953 and then (Is_Scalar_Type (P_Entity) or Is_Array_Type (P_Entity))
5954 and then (not Is_Generic_Type (P_Entity))
5955 then
5956 P_Type := P_Entity;
5957
5958 -- Second foldable possibility is an array object (RM 4.9(8))
5959
5960 elsif (Ekind (P_Entity) = E_Variable
5961 or else
5962 Ekind (P_Entity) = E_Constant)
5963 and then Is_Array_Type (Etype (P_Entity))
5964 and then (not Is_Generic_Type (Etype (P_Entity)))
5965 then
5966 P_Type := Etype (P_Entity);
5967
5968 -- If the entity is an array constant with an unconstrained nominal
5969 -- subtype then get the type from the initial value. If the value has
5970 -- been expanded into assignments, there is no expression and the
5971 -- attribute reference remains dynamic.
5972
5973 -- We could do better here and retrieve the type ???
5974
5975 if Ekind (P_Entity) = E_Constant
5976 and then not Is_Constrained (P_Type)
5977 then
5978 if No (Constant_Value (P_Entity)) then
5979 return;
5980 else
5981 P_Type := Etype (Constant_Value (P_Entity));
5982 end if;
5983 end if;
5984
5985 -- Definite must be folded if the prefix is not a generic type,
5986 -- that is to say if we are within an instantiation. Same processing
5987 -- applies to the GNAT attributes Has_Discriminants, Type_Class,
5988 -- Has_Tagged_Value, and Unconstrained_Array.
5989
5990 elsif (Id = Attribute_Definite
5991 or else
5992 Id = Attribute_Has_Access_Values
5993 or else
5994 Id = Attribute_Has_Discriminants
5995 or else
5996 Id = Attribute_Has_Tagged_Values
5997 or else
5998 Id = Attribute_Type_Class
5999 or else
6000 Id = Attribute_Unconstrained_Array
6001 or else
6002 Id = Attribute_Max_Alignment_For_Allocation)
6003 and then not Is_Generic_Type (P_Entity)
6004 then
6005 P_Type := P_Entity;
6006
6007 -- We can fold 'Size applied to a type if the size is known (as happens
6008 -- for a size from an attribute definition clause). At this stage, this
6009 -- can happen only for types (e.g. record types) for which the size is
6010 -- always non-static. We exclude generic types from consideration (since
6011 -- they have bogus sizes set within templates).
6012
6013 elsif Id = Attribute_Size
6014 and then Is_Type (P_Entity)
6015 and then (not Is_Generic_Type (P_Entity))
6016 and then Known_Static_RM_Size (P_Entity)
6017 then
6018 Compile_Time_Known_Attribute (N, RM_Size (P_Entity));
6019 return;
6020
6021 -- We can fold 'Alignment applied to a type if the alignment is known
6022 -- (as happens for an alignment from an attribute definition clause).
6023 -- At this stage, this can happen only for types (e.g. record
6024 -- types) for which the size is always non-static. We exclude
6025 -- generic types from consideration (since they have bogus
6026 -- sizes set within templates).
6027
6028 elsif Id = Attribute_Alignment
6029 and then Is_Type (P_Entity)
6030 and then (not Is_Generic_Type (P_Entity))
6031 and then Known_Alignment (P_Entity)
6032 then
6033 Compile_Time_Known_Attribute (N, Alignment (P_Entity));
6034 return;
6035
6036 -- If this is an access attribute that is known to fail accessibility
6037 -- check, rewrite accordingly.
6038
6039 elsif Attribute_Name (N) = Name_Access
6040 and then Raises_Constraint_Error (N)
6041 then
6042 Rewrite (N,
6043 Make_Raise_Program_Error (Loc,
6044 Reason => PE_Accessibility_Check_Failed));
6045 Set_Etype (N, C_Type);
6046 return;
6047
6048 -- No other cases are foldable (they certainly aren't static, and at
6049 -- the moment we don't try to fold any cases other than the ones above).
6050
6051 else
6052 Check_Expressions;
6053 return;
6054 end if;
6055
6056 -- If either attribute or the prefix is Any_Type, then propagate
6057 -- Any_Type to the result and don't do anything else at all.
6058
6059 if P_Type = Any_Type
6060 or else (Present (E1) and then Etype (E1) = Any_Type)
6061 or else (Present (E2) and then Etype (E2) = Any_Type)
6062 then
6063 Set_Etype (N, Any_Type);
6064 return;
6065 end if;
6066
6067 -- Scalar subtype case. We have not yet enforced the static requirement
6068 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
6069 -- of non-static attribute references (e.g. S'Digits for a non-static
6070 -- floating-point type, which we can compute at compile time).
6071
6072 -- Note: this folding of non-static attributes is not simply a case of
6073 -- optimization. For many of the attributes affected, Gigi cannot handle
6074 -- the attribute and depends on the front end having folded them away.
6075
6076 -- Note: although we don't require staticness at this stage, we do set
6077 -- the Static variable to record the staticness, for easy reference by
6078 -- those attributes where it matters (e.g. Succ and Pred), and also to
6079 -- be used to ensure that non-static folded things are not marked as
6080 -- being static (a check that is done right at the end).
6081
6082 P_Root_Type := Root_Type (P_Type);
6083 P_Base_Type := Base_Type (P_Type);
6084
6085 -- If the root type or base type is generic, then we cannot fold. This
6086 -- test is needed because subtypes of generic types are not always
6087 -- marked as being generic themselves (which seems odd???)
6088
6089 if Is_Generic_Type (P_Root_Type)
6090 or else Is_Generic_Type (P_Base_Type)
6091 then
6092 return;
6093 end if;
6094
6095 if Is_Scalar_Type (P_Type) then
6096 Static := Is_OK_Static_Subtype (P_Type);
6097
6098 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
6099 -- since we can't do anything with unconstrained arrays. In addition,
6100 -- only the First, Last and Length attributes are possibly static.
6101
6102 -- Definite, Has_Access_Values, Has_Discriminants, Has_Tagged_Values,
6103 -- Type_Class, and Unconstrained_Array are again exceptions, because
6104 -- they apply as well to unconstrained types.
6105
6106 -- In addition Component_Size is an exception since it is possibly
6107 -- foldable, even though it is never static, and it does apply to
6108 -- unconstrained arrays. Furthermore, it is essential to fold this
6109 -- in the packed case, since otherwise the value will be incorrect.
6110
6111 elsif Id = Attribute_Definite
6112 or else
6113 Id = Attribute_Has_Access_Values
6114 or else
6115 Id = Attribute_Has_Discriminants
6116 or else
6117 Id = Attribute_Has_Tagged_Values
6118 or else
6119 Id = Attribute_Type_Class
6120 or else
6121 Id = Attribute_Unconstrained_Array
6122 or else
6123 Id = Attribute_Component_Size
6124 then
6125 Static := False;
6126
6127 elsif Id /= Attribute_Max_Alignment_For_Allocation then
6128 if not Is_Constrained (P_Type)
6129 or else (Id /= Attribute_First and then
6130 Id /= Attribute_Last and then
6131 Id /= Attribute_Length)
6132 then
6133 Check_Expressions;
6134 return;
6135 end if;
6136
6137 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
6138 -- scalar case, we hold off on enforcing staticness, since there are
6139 -- cases which we can fold at compile time even though they are not
6140 -- static (e.g. 'Length applied to a static index, even though other
6141 -- non-static indexes make the array type non-static). This is only
6142 -- an optimization, but it falls out essentially free, so why not.
6143 -- Again we compute the variable Static for easy reference later
6144 -- (note that no array attributes are static in Ada 83).
6145
6146 -- We also need to set Static properly for subsequent legality checks
6147 -- which might otherwise accept non-static constants in contexts
6148 -- where they are not legal.
6149
6150 Static := Ada_Version >= Ada_95
6151 and then Statically_Denotes_Entity (P);
6152
6153 declare
6154 N : Node_Id;
6155
6156 begin
6157 N := First_Index (P_Type);
6158
6159 -- The expression is static if the array type is constrained
6160 -- by given bounds, and not by an initial expression. Constant
6161 -- strings are static in any case.
6162
6163 if Root_Type (P_Type) /= Standard_String then
6164 Static :=
6165 Static and then not Is_Constr_Subt_For_U_Nominal (P_Type);
6166 end if;
6167
6168 while Present (N) loop
6169 Static := Static and then Is_Static_Subtype (Etype (N));
6170
6171 -- If however the index type is generic, or derived from
6172 -- one, attributes cannot be folded.
6173
6174 if Is_Generic_Type (Root_Type (Etype (N)))
6175 and then Id /= Attribute_Component_Size
6176 then
6177 return;
6178 end if;
6179
6180 Next_Index (N);
6181 end loop;
6182 end;
6183 end if;
6184
6185 -- Check any expressions that are present. Note that these expressions,
6186 -- depending on the particular attribute type, are either part of the
6187 -- attribute designator, or they are arguments in a case where the
6188 -- attribute reference returns a function. In the latter case, the
6189 -- rule in (RM 4.9(22)) applies and in particular requires the type
6190 -- of the expressions to be scalar in order for the attribute to be
6191 -- considered to be static.
6192
6193 declare
6194 E : Node_Id;
6195
6196 begin
6197 E := E1;
6198 while Present (E) loop
6199
6200 -- If expression is not static, then the attribute reference
6201 -- result certainly cannot be static.
6202
6203 if not Is_Static_Expression (E) then
6204 Static := False;
6205 end if;
6206
6207 -- If the result is not known at compile time, or is not of
6208 -- a scalar type, then the result is definitely not static,
6209 -- so we can quit now.
6210
6211 if not Compile_Time_Known_Value (E)
6212 or else not Is_Scalar_Type (Etype (E))
6213 then
6214 -- An odd special case, if this is a Pos attribute, this
6215 -- is where we need to apply a range check since it does
6216 -- not get done anywhere else.
6217
6218 if Id = Attribute_Pos then
6219 if Is_Integer_Type (Etype (E)) then
6220 Apply_Range_Check (E, Etype (N));
6221 end if;
6222 end if;
6223
6224 Check_Expressions;
6225 return;
6226
6227 -- If the expression raises a constraint error, then so does
6228 -- the attribute reference. We keep going in this case because
6229 -- we are still interested in whether the attribute reference
6230 -- is static even if it is not static.
6231
6232 elsif Raises_Constraint_Error (E) then
6233 Set_Raises_Constraint_Error (N);
6234 end if;
6235
6236 Next (E);
6237 end loop;
6238
6239 if Raises_Constraint_Error (Prefix (N)) then
6240 return;
6241 end if;
6242 end;
6243
6244 -- Deal with the case of a static attribute reference that raises
6245 -- constraint error. The Raises_Constraint_Error flag will already
6246 -- have been set, and the Static flag shows whether the attribute
6247 -- reference is static. In any case we certainly can't fold such an
6248 -- attribute reference.
6249
6250 -- Note that the rewriting of the attribute node with the constraint
6251 -- error node is essential in this case, because otherwise Gigi might
6252 -- blow up on one of the attributes it never expects to see.
6253
6254 -- The constraint_error node must have the type imposed by the context,
6255 -- to avoid spurious errors in the enclosing expression.
6256
6257 if Raises_Constraint_Error (N) then
6258 CE_Node :=
6259 Make_Raise_Constraint_Error (Sloc (N),
6260 Reason => CE_Range_Check_Failed);
6261 Set_Etype (CE_Node, Etype (N));
6262 Set_Raises_Constraint_Error (CE_Node);
6263 Check_Expressions;
6264 Rewrite (N, Relocate_Node (CE_Node));
6265 Set_Is_Static_Expression (N, Static);
6266 return;
6267 end if;
6268
6269 -- At this point we have a potentially foldable attribute reference.
6270 -- If Static is set, then the attribute reference definitely obeys
6271 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
6272 -- folded. If Static is not set, then the attribute may or may not
6273 -- be foldable, and the individual attribute processing routines
6274 -- test Static as required in cases where it makes a difference.
6275
6276 -- In the case where Static is not set, we do know that all the
6277 -- expressions present are at least known at compile time (we assumed
6278 -- above that if this was not the case, then there was no hope of static
6279 -- evaluation). However, we did not require that the bounds of the
6280 -- prefix type be compile time known, let alone static). That's because
6281 -- there are many attributes that can be computed at compile time on
6282 -- non-static subtypes, even though such references are not static
6283 -- expressions.
6284
6285 case Id is
6286
6287 -- Attributes related to Ada 2012 iterators (placeholder ???)
6288
6289 when Attribute_Constant_Indexing => null;
6290 when Attribute_Default_Iterator => null;
6291 when Attribute_Implicit_Dereference => null;
6292 when Attribute_Iterator_Element => null;
6293 when Attribute_Variable_Indexing => null;
6294
6295 --------------
6296 -- Adjacent --
6297 --------------
6298
6299 when Attribute_Adjacent =>
6300 Fold_Ureal (N,
6301 Eval_Fat.Adjacent
6302 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6303
6304 ---------
6305 -- Aft --
6306 ---------
6307
6308 when Attribute_Aft =>
6309 Fold_Uint (N, Aft_Value (P_Type), True);
6310
6311 ---------------
6312 -- Alignment --
6313 ---------------
6314
6315 when Attribute_Alignment => Alignment_Block : declare
6316 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6317
6318 begin
6319 -- Fold if alignment is set and not otherwise
6320
6321 if Known_Alignment (P_TypeA) then
6322 Fold_Uint (N, Alignment (P_TypeA), Is_Discrete_Type (P_TypeA));
6323 end if;
6324 end Alignment_Block;
6325
6326 ---------------
6327 -- AST_Entry --
6328 ---------------
6329
6330 -- Can only be folded in No_Ast_Handler case
6331
6332 when Attribute_AST_Entry =>
6333 if not Is_AST_Entry (P_Entity) then
6334 Rewrite (N,
6335 New_Occurrence_Of (RTE (RE_No_AST_Handler), Loc));
6336 else
6337 null;
6338 end if;
6339
6340 ---------
6341 -- Bit --
6342 ---------
6343
6344 -- Bit can never be folded
6345
6346 when Attribute_Bit =>
6347 null;
6348
6349 ------------------
6350 -- Body_Version --
6351 ------------------
6352
6353 -- Body_version can never be static
6354
6355 when Attribute_Body_Version =>
6356 null;
6357
6358 -------------
6359 -- Ceiling --
6360 -------------
6361
6362 when Attribute_Ceiling =>
6363 Fold_Ureal (N,
6364 Eval_Fat.Ceiling (P_Root_Type, Expr_Value_R (E1)), Static);
6365
6366 --------------------
6367 -- Component_Size --
6368 --------------------
6369
6370 when Attribute_Component_Size =>
6371 if Known_Static_Component_Size (P_Type) then
6372 Fold_Uint (N, Component_Size (P_Type), False);
6373 end if;
6374
6375 -------------
6376 -- Compose --
6377 -------------
6378
6379 when Attribute_Compose =>
6380 Fold_Ureal (N,
6381 Eval_Fat.Compose
6382 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)),
6383 Static);
6384
6385 -----------------
6386 -- Constrained --
6387 -----------------
6388
6389 -- Constrained is never folded for now, there may be cases that
6390 -- could be handled at compile time. To be looked at later.
6391
6392 when Attribute_Constrained =>
6393 null;
6394
6395 ---------------
6396 -- Copy_Sign --
6397 ---------------
6398
6399 when Attribute_Copy_Sign =>
6400 Fold_Ureal (N,
6401 Eval_Fat.Copy_Sign
6402 (P_Root_Type, Expr_Value_R (E1), Expr_Value_R (E2)), Static);
6403
6404 --------------
6405 -- Definite --
6406 --------------
6407
6408 when Attribute_Definite =>
6409 Rewrite (N, New_Occurrence_Of (
6410 Boolean_Literals (not Is_Indefinite_Subtype (P_Entity)), Loc));
6411 Analyze_And_Resolve (N, Standard_Boolean);
6412
6413 -----------
6414 -- Delta --
6415 -----------
6416
6417 when Attribute_Delta =>
6418 Fold_Ureal (N, Delta_Value (P_Type), True);
6419
6420 ------------
6421 -- Denorm --
6422 ------------
6423
6424 when Attribute_Denorm =>
6425 Fold_Uint
6426 (N, UI_From_Int (Boolean'Pos (Denorm_On_Target)), True);
6427
6428 ---------------------
6429 -- Descriptor_Size --
6430 ---------------------
6431
6432 when Attribute_Descriptor_Size =>
6433 null;
6434
6435 ------------
6436 -- Digits --
6437 ------------
6438
6439 when Attribute_Digits =>
6440 Fold_Uint (N, Digits_Value (P_Type), True);
6441
6442 ----------
6443 -- Emax --
6444 ----------
6445
6446 when Attribute_Emax =>
6447
6448 -- Ada 83 attribute is defined as (RM83 3.5.8)
6449
6450 -- T'Emax = 4 * T'Mantissa
6451
6452 Fold_Uint (N, 4 * Mantissa, True);
6453
6454 --------------
6455 -- Enum_Rep --
6456 --------------
6457
6458 when Attribute_Enum_Rep =>
6459
6460 -- For an enumeration type with a non-standard representation use
6461 -- the Enumeration_Rep field of the proper constant. Note that this
6462 -- will not work for types Character/Wide_[Wide-]Character, since no
6463 -- real entities are created for the enumeration literals, but that
6464 -- does not matter since these two types do not have non-standard
6465 -- representations anyway.
6466
6467 if Is_Enumeration_Type (P_Type)
6468 and then Has_Non_Standard_Rep (P_Type)
6469 then
6470 Fold_Uint (N, Enumeration_Rep (Expr_Value_E (E1)), Static);
6471
6472 -- For enumeration types with standard representations and all
6473 -- other cases (i.e. all integer and modular types), Enum_Rep
6474 -- is equivalent to Pos.
6475
6476 else
6477 Fold_Uint (N, Expr_Value (E1), Static);
6478 end if;
6479
6480 --------------
6481 -- Enum_Val --
6482 --------------
6483
6484 when Attribute_Enum_Val => Enum_Val : declare
6485 Lit : Node_Id;
6486
6487 begin
6488 -- We have something like Enum_Type'Enum_Val (23), so search for a
6489 -- corresponding value in the list of Enum_Rep values for the type.
6490
6491 Lit := First_Literal (P_Base_Type);
6492 loop
6493 if Enumeration_Rep (Lit) = Expr_Value (E1) then
6494 Fold_Uint (N, Enumeration_Pos (Lit), Static);
6495 exit;
6496 end if;
6497
6498 Next_Literal (Lit);
6499
6500 if No (Lit) then
6501 Apply_Compile_Time_Constraint_Error
6502 (N, "no representation value matches",
6503 CE_Range_Check_Failed,
6504 Warn => not Static);
6505 exit;
6506 end if;
6507 end loop;
6508 end Enum_Val;
6509
6510 -------------
6511 -- Epsilon --
6512 -------------
6513
6514 when Attribute_Epsilon =>
6515
6516 -- Ada 83 attribute is defined as (RM83 3.5.8)
6517
6518 -- T'Epsilon = 2.0**(1 - T'Mantissa)
6519
6520 Fold_Ureal (N, Ureal_2 ** (1 - Mantissa), True);
6521
6522 --------------
6523 -- Exponent --
6524 --------------
6525
6526 when Attribute_Exponent =>
6527 Fold_Uint (N,
6528 Eval_Fat.Exponent (P_Root_Type, Expr_Value_R (E1)), Static);
6529
6530 -----------
6531 -- First --
6532 -----------
6533
6534 when Attribute_First => First_Attr :
6535 begin
6536 Set_Bounds;
6537
6538 if Compile_Time_Known_Value (Lo_Bound) then
6539 if Is_Real_Type (P_Type) then
6540 Fold_Ureal (N, Expr_Value_R (Lo_Bound), Static);
6541 else
6542 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
6543 end if;
6544
6545 -- Replace VAX Float_Type'First with a reference to the temporary
6546 -- which represents the low bound of the type. This transformation
6547 -- is needed since the back end cannot evaluate 'First on VAX.
6548
6549 elsif Is_VAX_Float (P_Type)
6550 and then Nkind (Lo_Bound) = N_Identifier
6551 then
6552 Rewrite (N, New_Reference_To (Entity (Lo_Bound), Sloc (N)));
6553 Analyze (N);
6554
6555 else
6556 Check_Concurrent_Discriminant (Lo_Bound);
6557 end if;
6558 end First_Attr;
6559
6560 -----------------
6561 -- First_Valid --
6562 -----------------
6563
6564 when Attribute_First_Valid => First_Valid :
6565 begin
6566 if Has_Predicates (P_Type)
6567 and then Present (Static_Predicate (P_Type))
6568 then
6569 declare
6570 FirstN : constant Node_Id := First (Static_Predicate (P_Type));
6571 begin
6572 if Nkind (FirstN) = N_Range then
6573 Fold_Uint (N, Expr_Value (Low_Bound (FirstN)), Static);
6574 else
6575 Fold_Uint (N, Expr_Value (FirstN), Static);
6576 end if;
6577 end;
6578
6579 else
6580 Set_Bounds;
6581 Fold_Uint (N, Expr_Value (Lo_Bound), Static);
6582 end if;
6583 end First_Valid;
6584
6585 -----------------
6586 -- Fixed_Value --
6587 -----------------
6588
6589 when Attribute_Fixed_Value =>
6590 null;
6591
6592 -----------
6593 -- Floor --
6594 -----------
6595
6596 when Attribute_Floor =>
6597 Fold_Ureal (N,
6598 Eval_Fat.Floor (P_Root_Type, Expr_Value_R (E1)), Static);
6599
6600 ----------
6601 -- Fore --
6602 ----------
6603
6604 when Attribute_Fore =>
6605 if Compile_Time_Known_Bounds (P_Type) then
6606 Fold_Uint (N, UI_From_Int (Fore_Value), Static);
6607 end if;
6608
6609 --------------
6610 -- Fraction --
6611 --------------
6612
6613 when Attribute_Fraction =>
6614 Fold_Ureal (N,
6615 Eval_Fat.Fraction (P_Root_Type, Expr_Value_R (E1)), Static);
6616
6617 -----------------------
6618 -- Has_Access_Values --
6619 -----------------------
6620
6621 when Attribute_Has_Access_Values =>
6622 Rewrite (N, New_Occurrence_Of
6623 (Boolean_Literals (Has_Access_Values (P_Root_Type)), Loc));
6624 Analyze_And_Resolve (N, Standard_Boolean);
6625
6626 -----------------------
6627 -- Has_Discriminants --
6628 -----------------------
6629
6630 when Attribute_Has_Discriminants =>
6631 Rewrite (N, New_Occurrence_Of (
6632 Boolean_Literals (Has_Discriminants (P_Entity)), Loc));
6633 Analyze_And_Resolve (N, Standard_Boolean);
6634
6635 -----------------------
6636 -- Has_Tagged_Values --
6637 -----------------------
6638
6639 when Attribute_Has_Tagged_Values =>
6640 Rewrite (N, New_Occurrence_Of
6641 (Boolean_Literals (Has_Tagged_Component (P_Root_Type)), Loc));
6642 Analyze_And_Resolve (N, Standard_Boolean);
6643
6644 --------------
6645 -- Identity --
6646 --------------
6647
6648 when Attribute_Identity =>
6649 null;
6650
6651 -----------
6652 -- Image --
6653 -----------
6654
6655 -- Image is a scalar attribute, but is never static, because it is
6656 -- not a static function (having a non-scalar argument (RM 4.9(22))
6657 -- However, we can constant-fold the image of an enumeration literal
6658 -- if names are available.
6659
6660 when Attribute_Image =>
6661 if Is_Entity_Name (E1)
6662 and then Ekind (Entity (E1)) = E_Enumeration_Literal
6663 and then not Discard_Names (First_Subtype (Etype (E1)))
6664 and then not Global_Discard_Names
6665 then
6666 declare
6667 Lit : constant Entity_Id := Entity (E1);
6668 Str : String_Id;
6669 begin
6670 Start_String;
6671 Get_Unqualified_Decoded_Name_String (Chars (Lit));
6672 Set_Casing (All_Upper_Case);
6673 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6674 Str := End_String;
6675 Rewrite (N, Make_String_Literal (Loc, Strval => Str));
6676 Analyze_And_Resolve (N, Standard_String);
6677 Set_Is_Static_Expression (N, False);
6678 end;
6679 end if;
6680
6681 ---------
6682 -- Img --
6683 ---------
6684
6685 -- Img is a scalar attribute, but is never static, because it is
6686 -- not a static function (having a non-scalar argument (RM 4.9(22))
6687
6688 when Attribute_Img =>
6689 null;
6690
6691 -------------------
6692 -- Integer_Value --
6693 -------------------
6694
6695 -- We never try to fold Integer_Value (though perhaps we could???)
6696
6697 when Attribute_Integer_Value =>
6698 null;
6699
6700 -------------------
6701 -- Invalid_Value --
6702 -------------------
6703
6704 -- Invalid_Value is a scalar attribute that is never static, because
6705 -- the value is by design out of range.
6706
6707 when Attribute_Invalid_Value =>
6708 null;
6709
6710 -----------
6711 -- Large --
6712 -----------
6713
6714 when Attribute_Large =>
6715
6716 -- For fixed-point, we use the identity:
6717
6718 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
6719
6720 if Is_Fixed_Point_Type (P_Type) then
6721 Rewrite (N,
6722 Make_Op_Multiply (Loc,
6723 Left_Opnd =>
6724 Make_Op_Subtract (Loc,
6725 Left_Opnd =>
6726 Make_Op_Expon (Loc,
6727 Left_Opnd =>
6728 Make_Real_Literal (Loc, Ureal_2),
6729 Right_Opnd =>
6730 Make_Attribute_Reference (Loc,
6731 Prefix => P,
6732 Attribute_Name => Name_Mantissa)),
6733 Right_Opnd => Make_Real_Literal (Loc, Ureal_1)),
6734
6735 Right_Opnd =>
6736 Make_Real_Literal (Loc, Small_Value (Entity (P)))));
6737
6738 Analyze_And_Resolve (N, C_Type);
6739
6740 -- Floating-point (Ada 83 compatibility)
6741
6742 else
6743 -- Ada 83 attribute is defined as (RM83 3.5.8)
6744
6745 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
6746
6747 -- where
6748
6749 -- T'Emax = 4 * T'Mantissa
6750
6751 Fold_Ureal (N,
6752 Ureal_2 ** (4 * Mantissa) * (Ureal_1 - Ureal_2 ** (-Mantissa)),
6753 True);
6754 end if;
6755
6756 ----------
6757 -- Last --
6758 ----------
6759
6760 when Attribute_Last => Last_Attr :
6761 begin
6762 Set_Bounds;
6763
6764 if Compile_Time_Known_Value (Hi_Bound) then
6765 if Is_Real_Type (P_Type) then
6766 Fold_Ureal (N, Expr_Value_R (Hi_Bound), Static);
6767 else
6768 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
6769 end if;
6770
6771 -- Replace VAX Float_Type'Last with a reference to the temporary
6772 -- which represents the high bound of the type. This transformation
6773 -- is needed since the back end cannot evaluate 'Last on VAX.
6774
6775 elsif Is_VAX_Float (P_Type)
6776 and then Nkind (Hi_Bound) = N_Identifier
6777 then
6778 Rewrite (N, New_Reference_To (Entity (Hi_Bound), Sloc (N)));
6779 Analyze (N);
6780
6781 else
6782 Check_Concurrent_Discriminant (Hi_Bound);
6783 end if;
6784 end Last_Attr;
6785
6786 ----------------
6787 -- Last_Valid --
6788 ----------------
6789
6790 when Attribute_Last_Valid => Last_Valid :
6791 begin
6792 if Has_Predicates (P_Type)
6793 and then Present (Static_Predicate (P_Type))
6794 then
6795 declare
6796 LastN : constant Node_Id := Last (Static_Predicate (P_Type));
6797 begin
6798 if Nkind (LastN) = N_Range then
6799 Fold_Uint (N, Expr_Value (High_Bound (LastN)), Static);
6800 else
6801 Fold_Uint (N, Expr_Value (LastN), Static);
6802 end if;
6803 end;
6804
6805 else
6806 Set_Bounds;
6807 Fold_Uint (N, Expr_Value (Hi_Bound), Static);
6808 end if;
6809 end Last_Valid;
6810
6811 ------------------
6812 -- Leading_Part --
6813 ------------------
6814
6815 when Attribute_Leading_Part =>
6816 Fold_Ureal (N,
6817 Eval_Fat.Leading_Part
6818 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
6819
6820 ------------
6821 -- Length --
6822 ------------
6823
6824 when Attribute_Length => Length : declare
6825 Ind : Node_Id;
6826
6827 begin
6828 -- If any index type is a formal type, or derived from one, the
6829 -- bounds are not static. Treating them as static can produce
6830 -- spurious warnings or improper constant folding.
6831
6832 Ind := First_Index (P_Type);
6833 while Present (Ind) loop
6834 if Is_Generic_Type (Root_Type (Etype (Ind))) then
6835 return;
6836 end if;
6837
6838 Next_Index (Ind);
6839 end loop;
6840
6841 Set_Bounds;
6842
6843 -- For two compile time values, we can compute length
6844
6845 if Compile_Time_Known_Value (Lo_Bound)
6846 and then Compile_Time_Known_Value (Hi_Bound)
6847 then
6848 Fold_Uint (N,
6849 UI_Max (0, 1 + (Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound))),
6850 True);
6851 end if;
6852
6853 -- One more case is where Hi_Bound and Lo_Bound are compile-time
6854 -- comparable, and we can figure out the difference between them.
6855
6856 declare
6857 Diff : aliased Uint;
6858
6859 begin
6860 case
6861 Compile_Time_Compare
6862 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
6863 is
6864 when EQ =>
6865 Fold_Uint (N, Uint_1, False);
6866
6867 when GT =>
6868 Fold_Uint (N, Uint_0, False);
6869
6870 when LT =>
6871 if Diff /= No_Uint then
6872 Fold_Uint (N, Diff + 1, False);
6873 end if;
6874
6875 when others =>
6876 null;
6877 end case;
6878 end;
6879 end Length;
6880
6881 -------------
6882 -- Machine --
6883 -------------
6884
6885 when Attribute_Machine =>
6886 Fold_Ureal (N,
6887 Eval_Fat.Machine
6888 (P_Root_Type, Expr_Value_R (E1), Eval_Fat.Round, N),
6889 Static);
6890
6891 ------------------
6892 -- Machine_Emax --
6893 ------------------
6894
6895 when Attribute_Machine_Emax =>
6896 Fold_Uint (N, Machine_Emax_Value (P_Type), Static);
6897
6898 ------------------
6899 -- Machine_Emin --
6900 ------------------
6901
6902 when Attribute_Machine_Emin =>
6903 Fold_Uint (N, Machine_Emin_Value (P_Type), Static);
6904
6905 ----------------------
6906 -- Machine_Mantissa --
6907 ----------------------
6908
6909 when Attribute_Machine_Mantissa =>
6910 Fold_Uint (N, Machine_Mantissa_Value (P_Type), Static);
6911
6912 -----------------------
6913 -- Machine_Overflows --
6914 -----------------------
6915
6916 when Attribute_Machine_Overflows =>
6917
6918 -- Always true for fixed-point
6919
6920 if Is_Fixed_Point_Type (P_Type) then
6921 Fold_Uint (N, True_Value, True);
6922
6923 -- Floating point case
6924
6925 else
6926 Fold_Uint (N,
6927 UI_From_Int (Boolean'Pos (Machine_Overflows_On_Target)),
6928 True);
6929 end if;
6930
6931 -------------------
6932 -- Machine_Radix --
6933 -------------------
6934
6935 when Attribute_Machine_Radix =>
6936 if Is_Fixed_Point_Type (P_Type) then
6937 if Is_Decimal_Fixed_Point_Type (P_Type)
6938 and then Machine_Radix_10 (P_Type)
6939 then
6940 Fold_Uint (N, Uint_10, True);
6941 else
6942 Fold_Uint (N, Uint_2, True);
6943 end if;
6944
6945 -- All floating-point type always have radix 2
6946
6947 else
6948 Fold_Uint (N, Uint_2, True);
6949 end if;
6950
6951 ----------------------
6952 -- Machine_Rounding --
6953 ----------------------
6954
6955 -- Note: for the folding case, it is fine to treat Machine_Rounding
6956 -- exactly the same way as Rounding, since this is one of the allowed
6957 -- behaviors, and performance is not an issue here. It might be a bit
6958 -- better to give the same result as it would give at run time, even
6959 -- though the non-determinism is certainly permitted.
6960
6961 when Attribute_Machine_Rounding =>
6962 Fold_Ureal (N,
6963 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
6964
6965 --------------------
6966 -- Machine_Rounds --
6967 --------------------
6968
6969 when Attribute_Machine_Rounds =>
6970
6971 -- Always False for fixed-point
6972
6973 if Is_Fixed_Point_Type (P_Type) then
6974 Fold_Uint (N, False_Value, True);
6975
6976 -- Else yield proper floating-point result
6977
6978 else
6979 Fold_Uint
6980 (N, UI_From_Int (Boolean'Pos (Machine_Rounds_On_Target)), True);
6981 end if;
6982
6983 ------------------
6984 -- Machine_Size --
6985 ------------------
6986
6987 -- Note: Machine_Size is identical to Object_Size
6988
6989 when Attribute_Machine_Size => Machine_Size : declare
6990 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
6991
6992 begin
6993 if Known_Esize (P_TypeA) then
6994 Fold_Uint (N, Esize (P_TypeA), True);
6995 end if;
6996 end Machine_Size;
6997
6998 --------------
6999 -- Mantissa --
7000 --------------
7001
7002 when Attribute_Mantissa =>
7003
7004 -- Fixed-point mantissa
7005
7006 if Is_Fixed_Point_Type (P_Type) then
7007
7008 -- Compile time foldable case
7009
7010 if Compile_Time_Known_Value (Type_Low_Bound (P_Type))
7011 and then
7012 Compile_Time_Known_Value (Type_High_Bound (P_Type))
7013 then
7014 -- The calculation of the obsolete Ada 83 attribute Mantissa
7015 -- is annoying, because of AI00143, quoted here:
7016
7017 -- !question 84-01-10
7018
7019 -- Consider the model numbers for F:
7020
7021 -- type F is delta 1.0 range -7.0 .. 8.0;
7022
7023 -- The wording requires that F'MANTISSA be the SMALLEST
7024 -- integer number for which each bound of the specified
7025 -- range is either a model number or lies at most small
7026 -- distant from a model number. This means F'MANTISSA
7027 -- is required to be 3 since the range -7.0 .. 7.0 fits
7028 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
7029 -- number, namely, 7. Is this analysis correct? Note that
7030 -- this implies the upper bound of the range is not
7031 -- represented as a model number.
7032
7033 -- !response 84-03-17
7034
7035 -- The analysis is correct. The upper and lower bounds for
7036 -- a fixed point type can lie outside the range of model
7037 -- numbers.
7038
7039 declare
7040 Siz : Uint;
7041 LBound : Ureal;
7042 UBound : Ureal;
7043 Bound : Ureal;
7044 Max_Man : Uint;
7045
7046 begin
7047 LBound := Expr_Value_R (Type_Low_Bound (P_Type));
7048 UBound := Expr_Value_R (Type_High_Bound (P_Type));
7049 Bound := UR_Max (UR_Abs (LBound), UR_Abs (UBound));
7050 Max_Man := UR_Trunc (Bound / Small_Value (P_Type));
7051
7052 -- If the Bound is exactly a model number, i.e. a multiple
7053 -- of Small, then we back it off by one to get the integer
7054 -- value that must be representable.
7055
7056 if Small_Value (P_Type) * Max_Man = Bound then
7057 Max_Man := Max_Man - 1;
7058 end if;
7059
7060 -- Now find corresponding size = Mantissa value
7061
7062 Siz := Uint_0;
7063 while 2 ** Siz < Max_Man loop
7064 Siz := Siz + 1;
7065 end loop;
7066
7067 Fold_Uint (N, Siz, True);
7068 end;
7069
7070 else
7071 -- The case of dynamic bounds cannot be evaluated at compile
7072 -- time. Instead we use a runtime routine (see Exp_Attr).
7073
7074 null;
7075 end if;
7076
7077 -- Floating-point Mantissa
7078
7079 else
7080 Fold_Uint (N, Mantissa, True);
7081 end if;
7082
7083 ---------
7084 -- Max --
7085 ---------
7086
7087 when Attribute_Max => Max :
7088 begin
7089 if Is_Real_Type (P_Type) then
7090 Fold_Ureal
7091 (N, UR_Max (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
7092 else
7093 Fold_Uint (N, UI_Max (Expr_Value (E1), Expr_Value (E2)), Static);
7094 end if;
7095 end Max;
7096
7097 ----------------------------------
7098 -- Max_Alignment_For_Allocation --
7099 ----------------------------------
7100
7101 -- Max_Alignment_For_Allocation is usually the Alignment. However,
7102 -- arrays are allocated with dope, so we need to take into account both
7103 -- the alignment of the array, which comes from the component alignment,
7104 -- and the alignment of the dope. Also, if the alignment is unknown, we
7105 -- use the max (it's OK to be pessimistic).
7106
7107 when Attribute_Max_Alignment_For_Allocation =>
7108 declare
7109 A : Uint := UI_From_Int (Ttypes.Maximum_Alignment);
7110 begin
7111 if Known_Alignment (P_Type) and then
7112 (not Is_Array_Type (P_Type) or else Alignment (P_Type) > A)
7113 then
7114 A := Alignment (P_Type);
7115 end if;
7116
7117 Fold_Uint (N, A, Static);
7118 end;
7119
7120 ----------------------------------
7121 -- Max_Size_In_Storage_Elements --
7122 ----------------------------------
7123
7124 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
7125 -- Storage_Unit boundary. We can fold any cases for which the size
7126 -- is known by the front end.
7127
7128 when Attribute_Max_Size_In_Storage_Elements =>
7129 if Known_Esize (P_Type) then
7130 Fold_Uint (N,
7131 (Esize (P_Type) + System_Storage_Unit - 1) /
7132 System_Storage_Unit,
7133 Static);
7134 end if;
7135
7136 --------------------
7137 -- Mechanism_Code --
7138 --------------------
7139
7140 when Attribute_Mechanism_Code =>
7141 declare
7142 Val : Int;
7143 Formal : Entity_Id;
7144 Mech : Mechanism_Type;
7145
7146 begin
7147 if No (E1) then
7148 Mech := Mechanism (P_Entity);
7149
7150 else
7151 Val := UI_To_Int (Expr_Value (E1));
7152
7153 Formal := First_Formal (P_Entity);
7154 for J in 1 .. Val - 1 loop
7155 Next_Formal (Formal);
7156 end loop;
7157 Mech := Mechanism (Formal);
7158 end if;
7159
7160 if Mech < 0 then
7161 Fold_Uint (N, UI_From_Int (Int (-Mech)), True);
7162 end if;
7163 end;
7164
7165 ---------
7166 -- Min --
7167 ---------
7168
7169 when Attribute_Min => Min :
7170 begin
7171 if Is_Real_Type (P_Type) then
7172 Fold_Ureal
7173 (N, UR_Min (Expr_Value_R (E1), Expr_Value_R (E2)), Static);
7174 else
7175 Fold_Uint
7176 (N, UI_Min (Expr_Value (E1), Expr_Value (E2)), Static);
7177 end if;
7178 end Min;
7179
7180 ---------
7181 -- Mod --
7182 ---------
7183
7184 when Attribute_Mod =>
7185 Fold_Uint
7186 (N, UI_Mod (Expr_Value (E1), Modulus (P_Base_Type)), Static);
7187
7188 -----------
7189 -- Model --
7190 -----------
7191
7192 when Attribute_Model =>
7193 Fold_Ureal (N,
7194 Eval_Fat.Model (P_Root_Type, Expr_Value_R (E1)), Static);
7195
7196 ----------------
7197 -- Model_Emin --
7198 ----------------
7199
7200 when Attribute_Model_Emin =>
7201 Fold_Uint (N, Model_Emin_Value (P_Base_Type), Static);
7202
7203 -------------------
7204 -- Model_Epsilon --
7205 -------------------
7206
7207 when Attribute_Model_Epsilon =>
7208 Fold_Ureal (N, Model_Epsilon_Value (P_Base_Type), Static);
7209
7210 --------------------
7211 -- Model_Mantissa --
7212 --------------------
7213
7214 when Attribute_Model_Mantissa =>
7215 Fold_Uint (N, Model_Mantissa_Value (P_Base_Type), Static);
7216
7217 -----------------
7218 -- Model_Small --
7219 -----------------
7220
7221 when Attribute_Model_Small =>
7222 Fold_Ureal (N, Model_Small_Value (P_Base_Type), Static);
7223
7224 -------------
7225 -- Modulus --
7226 -------------
7227
7228 when Attribute_Modulus =>
7229 Fold_Uint (N, Modulus (P_Type), True);
7230
7231 --------------------
7232 -- Null_Parameter --
7233 --------------------
7234
7235 -- Cannot fold, we know the value sort of, but the whole point is
7236 -- that there is no way to talk about this imaginary value except
7237 -- by using the attribute, so we leave it the way it is.
7238
7239 when Attribute_Null_Parameter =>
7240 null;
7241
7242 -----------------
7243 -- Object_Size --
7244 -----------------
7245
7246 -- The Object_Size attribute for a type returns the Esize of the
7247 -- type and can be folded if this value is known.
7248
7249 when Attribute_Object_Size => Object_Size : declare
7250 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7251
7252 begin
7253 if Known_Esize (P_TypeA) then
7254 Fold_Uint (N, Esize (P_TypeA), True);
7255 end if;
7256 end Object_Size;
7257
7258 ----------------------
7259 -- Overlaps_Storage --
7260 ----------------------
7261
7262 when Attribute_Overlaps_Storage =>
7263 null;
7264
7265 -------------------------
7266 -- Passed_By_Reference --
7267 -------------------------
7268
7269 -- Scalar types are never passed by reference
7270
7271 when Attribute_Passed_By_Reference =>
7272 Fold_Uint (N, False_Value, True);
7273
7274 ---------
7275 -- Pos --
7276 ---------
7277
7278 when Attribute_Pos =>
7279 Fold_Uint (N, Expr_Value (E1), True);
7280
7281 ----------
7282 -- Pred --
7283 ----------
7284
7285 when Attribute_Pred => Pred :
7286 begin
7287 -- Floating-point case
7288
7289 if Is_Floating_Point_Type (P_Type) then
7290 Fold_Ureal (N,
7291 Eval_Fat.Pred (P_Root_Type, Expr_Value_R (E1)), Static);
7292
7293 -- Fixed-point case
7294
7295 elsif Is_Fixed_Point_Type (P_Type) then
7296 Fold_Ureal (N,
7297 Expr_Value_R (E1) - Small_Value (P_Type), True);
7298
7299 -- Modular integer case (wraps)
7300
7301 elsif Is_Modular_Integer_Type (P_Type) then
7302 Fold_Uint (N, (Expr_Value (E1) - 1) mod Modulus (P_Type), Static);
7303
7304 -- Other scalar cases
7305
7306 else
7307 pragma Assert (Is_Scalar_Type (P_Type));
7308
7309 if Is_Enumeration_Type (P_Type)
7310 and then Expr_Value (E1) =
7311 Expr_Value (Type_Low_Bound (P_Base_Type))
7312 then
7313 Apply_Compile_Time_Constraint_Error
7314 (N, "Pred of `&''First`",
7315 CE_Overflow_Check_Failed,
7316 Ent => P_Base_Type,
7317 Warn => not Static);
7318
7319 Check_Expressions;
7320 return;
7321 end if;
7322
7323 Fold_Uint (N, Expr_Value (E1) - 1, Static);
7324 end if;
7325 end Pred;
7326
7327 -----------
7328 -- Range --
7329 -----------
7330
7331 -- No processing required, because by this stage, Range has been
7332 -- replaced by First .. Last, so this branch can never be taken.
7333
7334 when Attribute_Range =>
7335 raise Program_Error;
7336
7337 ------------------
7338 -- Range_Length --
7339 ------------------
7340
7341 when Attribute_Range_Length =>
7342 Set_Bounds;
7343
7344 -- Can fold if both bounds are compile time known
7345
7346 if Compile_Time_Known_Value (Hi_Bound)
7347 and then Compile_Time_Known_Value (Lo_Bound)
7348 then
7349 Fold_Uint (N,
7350 UI_Max
7351 (0, Expr_Value (Hi_Bound) - Expr_Value (Lo_Bound) + 1),
7352 Static);
7353 end if;
7354
7355 -- One more case is where Hi_Bound and Lo_Bound are compile-time
7356 -- comparable, and we can figure out the difference between them.
7357
7358 declare
7359 Diff : aliased Uint;
7360
7361 begin
7362 case
7363 Compile_Time_Compare
7364 (Lo_Bound, Hi_Bound, Diff'Access, Assume_Valid => False)
7365 is
7366 when EQ =>
7367 Fold_Uint (N, Uint_1, False);
7368
7369 when GT =>
7370 Fold_Uint (N, Uint_0, False);
7371
7372 when LT =>
7373 if Diff /= No_Uint then
7374 Fold_Uint (N, Diff + 1, False);
7375 end if;
7376
7377 when others =>
7378 null;
7379 end case;
7380 end;
7381
7382 ---------
7383 -- Ref --
7384 ---------
7385
7386 when Attribute_Ref =>
7387 Fold_Uint (N, Expr_Value (E1), True);
7388
7389 ---------------
7390 -- Remainder --
7391 ---------------
7392
7393 when Attribute_Remainder => Remainder : declare
7394 X : constant Ureal := Expr_Value_R (E1);
7395 Y : constant Ureal := Expr_Value_R (E2);
7396
7397 begin
7398 if UR_Is_Zero (Y) then
7399 Apply_Compile_Time_Constraint_Error
7400 (N, "division by zero in Remainder",
7401 CE_Overflow_Check_Failed,
7402 Warn => not Static);
7403
7404 Check_Expressions;
7405 return;
7406 end if;
7407
7408 Fold_Ureal (N, Eval_Fat.Remainder (P_Root_Type, X, Y), Static);
7409 end Remainder;
7410
7411 -----------
7412 -- Round --
7413 -----------
7414
7415 when Attribute_Round => Round :
7416 declare
7417 Sr : Ureal;
7418 Si : Uint;
7419
7420 begin
7421 -- First we get the (exact result) in units of small
7422
7423 Sr := Expr_Value_R (E1) / Small_Value (C_Type);
7424
7425 -- Now round that exactly to an integer
7426
7427 Si := UR_To_Uint (Sr);
7428
7429 -- Finally the result is obtained by converting back to real
7430
7431 Fold_Ureal (N, Si * Small_Value (C_Type), Static);
7432 end Round;
7433
7434 --------------
7435 -- Rounding --
7436 --------------
7437
7438 when Attribute_Rounding =>
7439 Fold_Ureal (N,
7440 Eval_Fat.Rounding (P_Root_Type, Expr_Value_R (E1)), Static);
7441
7442 ---------------
7443 -- Safe_Emax --
7444 ---------------
7445
7446 when Attribute_Safe_Emax =>
7447 Fold_Uint (N, Safe_Emax_Value (P_Type), Static);
7448
7449 ----------------
7450 -- Safe_First --
7451 ----------------
7452
7453 when Attribute_Safe_First =>
7454 Fold_Ureal (N, Safe_First_Value (P_Type), Static);
7455
7456 ----------------
7457 -- Safe_Large --
7458 ----------------
7459
7460 when Attribute_Safe_Large =>
7461 if Is_Fixed_Point_Type (P_Type) then
7462 Fold_Ureal
7463 (N, Expr_Value_R (Type_High_Bound (P_Base_Type)), Static);
7464 else
7465 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
7466 end if;
7467
7468 ---------------
7469 -- Safe_Last --
7470 ---------------
7471
7472 when Attribute_Safe_Last =>
7473 Fold_Ureal (N, Safe_Last_Value (P_Type), Static);
7474
7475 ----------------
7476 -- Safe_Small --
7477 ----------------
7478
7479 when Attribute_Safe_Small =>
7480
7481 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
7482 -- for fixed-point, since is the same as Small, but we implement
7483 -- it for backwards compatibility.
7484
7485 if Is_Fixed_Point_Type (P_Type) then
7486 Fold_Ureal (N, Small_Value (P_Type), Static);
7487
7488 -- Ada 83 Safe_Small for floating-point cases
7489
7490 else
7491 Fold_Ureal (N, Model_Small_Value (P_Type), Static);
7492 end if;
7493
7494 ------------------
7495 -- Same_Storage --
7496 ------------------
7497
7498 when Attribute_Same_Storage =>
7499 null;
7500
7501 -----------
7502 -- Scale --
7503 -----------
7504
7505 when Attribute_Scale =>
7506 Fold_Uint (N, Scale_Value (P_Type), True);
7507
7508 -------------
7509 -- Scaling --
7510 -------------
7511
7512 when Attribute_Scaling =>
7513 Fold_Ureal (N,
7514 Eval_Fat.Scaling
7515 (P_Root_Type, Expr_Value_R (E1), Expr_Value (E2)), Static);
7516
7517 ------------------
7518 -- Signed_Zeros --
7519 ------------------
7520
7521 when Attribute_Signed_Zeros =>
7522 Fold_Uint
7523 (N, UI_From_Int (Boolean'Pos (Signed_Zeros_On_Target)), Static);
7524
7525 ----------
7526 -- Size --
7527 ----------
7528
7529 -- Size attribute returns the RM size. All scalar types can be folded,
7530 -- as well as any types for which the size is known by the front end,
7531 -- including any type for which a size attribute is specified.
7532
7533 when Attribute_Size | Attribute_VADS_Size => Size : declare
7534 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7535
7536 begin
7537 if RM_Size (P_TypeA) /= Uint_0 then
7538
7539 -- VADS_Size case
7540
7541 if Id = Attribute_VADS_Size or else Use_VADS_Size then
7542 declare
7543 S : constant Node_Id := Size_Clause (P_TypeA);
7544
7545 begin
7546 -- If a size clause applies, then use the size from it.
7547 -- This is one of the rare cases where we can use the
7548 -- Size_Clause field for a subtype when Has_Size_Clause
7549 -- is False. Consider:
7550
7551 -- type x is range 1 .. 64;
7552 -- for x'size use 12;
7553 -- subtype y is x range 0 .. 3;
7554
7555 -- Here y has a size clause inherited from x, but normally
7556 -- it does not apply, and y'size is 2. However, y'VADS_Size
7557 -- is indeed 12 and not 2.
7558
7559 if Present (S)
7560 and then Is_OK_Static_Expression (Expression (S))
7561 then
7562 Fold_Uint (N, Expr_Value (Expression (S)), True);
7563
7564 -- If no size is specified, then we simply use the object
7565 -- size in the VADS_Size case (e.g. Natural'Size is equal
7566 -- to Integer'Size, not one less).
7567
7568 else
7569 Fold_Uint (N, Esize (P_TypeA), True);
7570 end if;
7571 end;
7572
7573 -- Normal case (Size) in which case we want the RM_Size
7574
7575 else
7576 Fold_Uint (N,
7577 RM_Size (P_TypeA),
7578 Static and then Is_Discrete_Type (P_TypeA));
7579 end if;
7580 end if;
7581 end Size;
7582
7583 -----------
7584 -- Small --
7585 -----------
7586
7587 when Attribute_Small =>
7588
7589 -- The floating-point case is present only for Ada 83 compatibility.
7590 -- Note that strictly this is an illegal addition, since we are
7591 -- extending an Ada 95 defined attribute, but we anticipate an
7592 -- ARG ruling that will permit this.
7593
7594 if Is_Floating_Point_Type (P_Type) then
7595
7596 -- Ada 83 attribute is defined as (RM83 3.5.8)
7597
7598 -- T'Small = 2.0**(-T'Emax - 1)
7599
7600 -- where
7601
7602 -- T'Emax = 4 * T'Mantissa
7603
7604 Fold_Ureal (N, Ureal_2 ** ((-(4 * Mantissa)) - 1), Static);
7605
7606 -- Normal Ada 95 fixed-point case
7607
7608 else
7609 Fold_Ureal (N, Small_Value (P_Type), True);
7610 end if;
7611
7612 -----------------
7613 -- Stream_Size --
7614 -----------------
7615
7616 when Attribute_Stream_Size =>
7617 null;
7618
7619 ----------
7620 -- Succ --
7621 ----------
7622
7623 when Attribute_Succ => Succ :
7624 begin
7625 -- Floating-point case
7626
7627 if Is_Floating_Point_Type (P_Type) then
7628 Fold_Ureal (N,
7629 Eval_Fat.Succ (P_Root_Type, Expr_Value_R (E1)), Static);
7630
7631 -- Fixed-point case
7632
7633 elsif Is_Fixed_Point_Type (P_Type) then
7634 Fold_Ureal (N,
7635 Expr_Value_R (E1) + Small_Value (P_Type), Static);
7636
7637 -- Modular integer case (wraps)
7638
7639 elsif Is_Modular_Integer_Type (P_Type) then
7640 Fold_Uint (N, (Expr_Value (E1) + 1) mod Modulus (P_Type), Static);
7641
7642 -- Other scalar cases
7643
7644 else
7645 pragma Assert (Is_Scalar_Type (P_Type));
7646
7647 if Is_Enumeration_Type (P_Type)
7648 and then Expr_Value (E1) =
7649 Expr_Value (Type_High_Bound (P_Base_Type))
7650 then
7651 Apply_Compile_Time_Constraint_Error
7652 (N, "Succ of `&''Last`",
7653 CE_Overflow_Check_Failed,
7654 Ent => P_Base_Type,
7655 Warn => not Static);
7656
7657 Check_Expressions;
7658 return;
7659 else
7660 Fold_Uint (N, Expr_Value (E1) + 1, Static);
7661 end if;
7662 end if;
7663 end Succ;
7664
7665 ----------------
7666 -- Truncation --
7667 ----------------
7668
7669 when Attribute_Truncation =>
7670 Fold_Ureal (N,
7671 Eval_Fat.Truncation (P_Root_Type, Expr_Value_R (E1)), Static);
7672
7673 ----------------
7674 -- Type_Class --
7675 ----------------
7676
7677 when Attribute_Type_Class => Type_Class : declare
7678 Typ : constant Entity_Id := Underlying_Type (P_Base_Type);
7679 Id : RE_Id;
7680
7681 begin
7682 if Is_Descendent_Of_Address (Typ) then
7683 Id := RE_Type_Class_Address;
7684
7685 elsif Is_Enumeration_Type (Typ) then
7686 Id := RE_Type_Class_Enumeration;
7687
7688 elsif Is_Integer_Type (Typ) then
7689 Id := RE_Type_Class_Integer;
7690
7691 elsif Is_Fixed_Point_Type (Typ) then
7692 Id := RE_Type_Class_Fixed_Point;
7693
7694 elsif Is_Floating_Point_Type (Typ) then
7695 Id := RE_Type_Class_Floating_Point;
7696
7697 elsif Is_Array_Type (Typ) then
7698 Id := RE_Type_Class_Array;
7699
7700 elsif Is_Record_Type (Typ) then
7701 Id := RE_Type_Class_Record;
7702
7703 elsif Is_Access_Type (Typ) then
7704 Id := RE_Type_Class_Access;
7705
7706 elsif Is_Enumeration_Type (Typ) then
7707 Id := RE_Type_Class_Enumeration;
7708
7709 elsif Is_Task_Type (Typ) then
7710 Id := RE_Type_Class_Task;
7711
7712 -- We treat protected types like task types. It would make more
7713 -- sense to have another enumeration value, but after all the
7714 -- whole point of this feature is to be exactly DEC compatible,
7715 -- and changing the type Type_Class would not meet this requirement.
7716
7717 elsif Is_Protected_Type (Typ) then
7718 Id := RE_Type_Class_Task;
7719
7720 -- Not clear if there are any other possibilities, but if there
7721 -- are, then we will treat them as the address case.
7722
7723 else
7724 Id := RE_Type_Class_Address;
7725 end if;
7726
7727 Rewrite (N, New_Occurrence_Of (RTE (Id), Loc));
7728 end Type_Class;
7729
7730 -----------------------
7731 -- Unbiased_Rounding --
7732 -----------------------
7733
7734 when Attribute_Unbiased_Rounding =>
7735 Fold_Ureal (N,
7736 Eval_Fat.Unbiased_Rounding (P_Root_Type, Expr_Value_R (E1)),
7737 Static);
7738
7739 -------------------------
7740 -- Unconstrained_Array --
7741 -------------------------
7742
7743 when Attribute_Unconstrained_Array => Unconstrained_Array : declare
7744 Typ : constant Entity_Id := Underlying_Type (P_Type);
7745
7746 begin
7747 Rewrite (N, New_Occurrence_Of (
7748 Boolean_Literals (
7749 Is_Array_Type (P_Type)
7750 and then not Is_Constrained (Typ)), Loc));
7751
7752 -- Analyze and resolve as boolean, note that this attribute is
7753 -- a static attribute in GNAT.
7754
7755 Analyze_And_Resolve (N, Standard_Boolean);
7756 Static := True;
7757 end Unconstrained_Array;
7758
7759 ---------------
7760 -- VADS_Size --
7761 ---------------
7762
7763 -- Processing is shared with Size
7764
7765 ---------
7766 -- Val --
7767 ---------
7768
7769 when Attribute_Val => Val :
7770 begin
7771 if Expr_Value (E1) < Expr_Value (Type_Low_Bound (P_Base_Type))
7772 or else
7773 Expr_Value (E1) > Expr_Value (Type_High_Bound (P_Base_Type))
7774 then
7775 Apply_Compile_Time_Constraint_Error
7776 (N, "Val expression out of range",
7777 CE_Range_Check_Failed,
7778 Warn => not Static);
7779
7780 Check_Expressions;
7781 return;
7782
7783 else
7784 Fold_Uint (N, Expr_Value (E1), Static);
7785 end if;
7786 end Val;
7787
7788 ----------------
7789 -- Value_Size --
7790 ----------------
7791
7792 -- The Value_Size attribute for a type returns the RM size of the
7793 -- type. This an always be folded for scalar types, and can also
7794 -- be folded for non-scalar types if the size is set.
7795
7796 when Attribute_Value_Size => Value_Size : declare
7797 P_TypeA : constant Entity_Id := Underlying_Type (P_Type);
7798 begin
7799 if RM_Size (P_TypeA) /= Uint_0 then
7800 Fold_Uint (N, RM_Size (P_TypeA), True);
7801 end if;
7802 end Value_Size;
7803
7804 -------------
7805 -- Version --
7806 -------------
7807
7808 -- Version can never be static
7809
7810 when Attribute_Version =>
7811 null;
7812
7813 ----------------
7814 -- Wide_Image --
7815 ----------------
7816
7817 -- Wide_Image is a scalar attribute, but is never static, because it
7818 -- is not a static function (having a non-scalar argument (RM 4.9(22))
7819
7820 when Attribute_Wide_Image =>
7821 null;
7822
7823 ---------------------
7824 -- Wide_Wide_Image --
7825 ---------------------
7826
7827 -- Wide_Wide_Image is a scalar attribute but is never static, because it
7828 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
7829
7830 when Attribute_Wide_Wide_Image =>
7831 null;
7832
7833 ---------------------
7834 -- Wide_Wide_Width --
7835 ---------------------
7836
7837 -- Processing for Wide_Wide_Width is combined with Width
7838
7839 ----------------
7840 -- Wide_Width --
7841 ----------------
7842
7843 -- Processing for Wide_Width is combined with Width
7844
7845 -----------
7846 -- Width --
7847 -----------
7848
7849 -- This processing also handles the case of Wide_[Wide_]Width
7850
7851 when Attribute_Width |
7852 Attribute_Wide_Width |
7853 Attribute_Wide_Wide_Width => Width :
7854 begin
7855 if Compile_Time_Known_Bounds (P_Type) then
7856
7857 -- Floating-point types
7858
7859 if Is_Floating_Point_Type (P_Type) then
7860
7861 -- Width is zero for a null range (RM 3.5 (38))
7862
7863 if Expr_Value_R (Type_High_Bound (P_Type)) <
7864 Expr_Value_R (Type_Low_Bound (P_Type))
7865 then
7866 Fold_Uint (N, Uint_0, True);
7867
7868 else
7869 -- For floating-point, we have +N.dddE+nnn where length
7870 -- of ddd is determined by type'Digits - 1, but is one
7871 -- if Digits is one (RM 3.5 (33)).
7872
7873 -- nnn is set to 2 for Short_Float and Float (32 bit
7874 -- floats), and 3 for Long_Float and Long_Long_Float.
7875 -- For machines where Long_Long_Float is the IEEE
7876 -- extended precision type, the exponent takes 4 digits.
7877
7878 declare
7879 Len : Int :=
7880 Int'Max (2, UI_To_Int (Digits_Value (P_Type)));
7881
7882 begin
7883 if Esize (P_Type) <= 32 then
7884 Len := Len + 6;
7885 elsif Esize (P_Type) = 64 then
7886 Len := Len + 7;
7887 else
7888 Len := Len + 8;
7889 end if;
7890
7891 Fold_Uint (N, UI_From_Int (Len), True);
7892 end;
7893 end if;
7894
7895 -- Fixed-point types
7896
7897 elsif Is_Fixed_Point_Type (P_Type) then
7898
7899 -- Width is zero for a null range (RM 3.5 (38))
7900
7901 if Expr_Value (Type_High_Bound (P_Type)) <
7902 Expr_Value (Type_Low_Bound (P_Type))
7903 then
7904 Fold_Uint (N, Uint_0, True);
7905
7906 -- The non-null case depends on the specific real type
7907
7908 else
7909 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
7910
7911 Fold_Uint
7912 (N, UI_From_Int (Fore_Value + 1) + Aft_Value (P_Type),
7913 True);
7914 end if;
7915
7916 -- Discrete types
7917
7918 else
7919 declare
7920 R : constant Entity_Id := Root_Type (P_Type);
7921 Lo : constant Uint := Expr_Value (Type_Low_Bound (P_Type));
7922 Hi : constant Uint := Expr_Value (Type_High_Bound (P_Type));
7923 W : Nat;
7924 Wt : Nat;
7925 T : Uint;
7926 L : Node_Id;
7927 C : Character;
7928
7929 begin
7930 -- Empty ranges
7931
7932 if Lo > Hi then
7933 W := 0;
7934
7935 -- Width for types derived from Standard.Character
7936 -- and Standard.Wide_[Wide_]Character.
7937
7938 elsif Is_Standard_Character_Type (P_Type) then
7939 W := 0;
7940
7941 -- Set W larger if needed
7942
7943 for J in UI_To_Int (Lo) .. UI_To_Int (Hi) loop
7944
7945 -- All wide characters look like Hex_hhhhhhhh
7946
7947 if J > 255 then
7948
7949 -- No need to compute this more than once!
7950
7951 exit;
7952
7953 else
7954 C := Character'Val (J);
7955
7956 -- Test for all cases where Character'Image
7957 -- yields an image that is longer than three
7958 -- characters. First the cases of Reserved_xxx
7959 -- names (length = 12).
7960
7961 case C is
7962 when Reserved_128 | Reserved_129 |
7963 Reserved_132 | Reserved_153
7964 => Wt := 12;
7965
7966 when BS | HT | LF | VT | FF | CR |
7967 SO | SI | EM | FS | GS | RS |
7968 US | RI | MW | ST | PM
7969 => Wt := 2;
7970
7971 when NUL | SOH | STX | ETX | EOT |
7972 ENQ | ACK | BEL | DLE | DC1 |
7973 DC2 | DC3 | DC4 | NAK | SYN |
7974 ETB | CAN | SUB | ESC | DEL |
7975 BPH | NBH | NEL | SSA | ESA |
7976 HTS | HTJ | VTS | PLD | PLU |
7977 SS2 | SS3 | DCS | PU1 | PU2 |
7978 STS | CCH | SPA | EPA | SOS |
7979 SCI | CSI | OSC | APC
7980 => Wt := 3;
7981
7982 when Space .. Tilde |
7983 No_Break_Space .. LC_Y_Diaeresis
7984 =>
7985 -- Special case of soft hyphen in Ada 2005
7986
7987 if C = Character'Val (16#AD#)
7988 and then Ada_Version >= Ada_2005
7989 then
7990 Wt := 11;
7991 else
7992 Wt := 3;
7993 end if;
7994 end case;
7995
7996 W := Int'Max (W, Wt);
7997 end if;
7998 end loop;
7999
8000 -- Width for types derived from Standard.Boolean
8001
8002 elsif R = Standard_Boolean then
8003 if Lo = 0 then
8004 W := 5; -- FALSE
8005 else
8006 W := 4; -- TRUE
8007 end if;
8008
8009 -- Width for integer types
8010
8011 elsif Is_Integer_Type (P_Type) then
8012 T := UI_Max (abs Lo, abs Hi);
8013
8014 W := 2;
8015 while T >= 10 loop
8016 W := W + 1;
8017 T := T / 10;
8018 end loop;
8019
8020 -- User declared enum type with discard names
8021
8022 elsif Discard_Names (R) then
8023
8024 -- If range is null, result is zero, that has already
8025 -- been dealt with, so what we need is the power of ten
8026 -- that accomodates the Pos of the largest value, which
8027 -- is the high bound of the range + one for the space.
8028
8029 W := 1;
8030 T := Hi;
8031 while T /= 0 loop
8032 T := T / 10;
8033 W := W + 1;
8034 end loop;
8035
8036 -- Only remaining possibility is user declared enum type
8037 -- with normal case of Discard_Names not active.
8038
8039 else
8040 pragma Assert (Is_Enumeration_Type (P_Type));
8041
8042 W := 0;
8043 L := First_Literal (P_Type);
8044 while Present (L) loop
8045
8046 -- Only pay attention to in range characters
8047
8048 if Lo <= Enumeration_Pos (L)
8049 and then Enumeration_Pos (L) <= Hi
8050 then
8051 -- For Width case, use decoded name
8052
8053 if Id = Attribute_Width then
8054 Get_Decoded_Name_String (Chars (L));
8055 Wt := Nat (Name_Len);
8056
8057 -- For Wide_[Wide_]Width, use encoded name, and
8058 -- then adjust for the encoding.
8059
8060 else
8061 Get_Name_String (Chars (L));
8062
8063 -- Character literals are always of length 3
8064
8065 if Name_Buffer (1) = 'Q' then
8066 Wt := 3;
8067
8068 -- Otherwise loop to adjust for upper/wide chars
8069
8070 else
8071 Wt := Nat (Name_Len);
8072
8073 for J in 1 .. Name_Len loop
8074 if Name_Buffer (J) = 'U' then
8075 Wt := Wt - 2;
8076 elsif Name_Buffer (J) = 'W' then
8077 Wt := Wt - 4;
8078 end if;
8079 end loop;
8080 end if;
8081 end if;
8082
8083 W := Int'Max (W, Wt);
8084 end if;
8085
8086 Next_Literal (L);
8087 end loop;
8088 end if;
8089
8090 Fold_Uint (N, UI_From_Int (W), True);
8091 end;
8092 end if;
8093 end if;
8094 end Width;
8095
8096 -- The following attributes denote functions that cannot be folded
8097
8098 when Attribute_From_Any |
8099 Attribute_To_Any |
8100 Attribute_TypeCode =>
8101 null;
8102
8103 -- The following attributes can never be folded, and furthermore we
8104 -- should not even have entered the case statement for any of these.
8105 -- Note that in some cases, the values have already been folded as
8106 -- a result of the processing in Analyze_Attribute.
8107
8108 when Attribute_Abort_Signal |
8109 Attribute_Access |
8110 Attribute_Address |
8111 Attribute_Address_Size |
8112 Attribute_Asm_Input |
8113 Attribute_Asm_Output |
8114 Attribute_Base |
8115 Attribute_Bit_Order |
8116 Attribute_Bit_Position |
8117 Attribute_Callable |
8118 Attribute_Caller |
8119 Attribute_Class |
8120 Attribute_Code_Address |
8121 Attribute_Compiler_Version |
8122 Attribute_Count |
8123 Attribute_Default_Bit_Order |
8124 Attribute_Elaborated |
8125 Attribute_Elab_Body |
8126 Attribute_Elab_Spec |
8127 Attribute_Elab_Subp_Body |
8128 Attribute_Enabled |
8129 Attribute_External_Tag |
8130 Attribute_Fast_Math |
8131 Attribute_First_Bit |
8132 Attribute_Input |
8133 Attribute_Last_Bit |
8134 Attribute_Maximum_Alignment |
8135 Attribute_Old |
8136 Attribute_Output |
8137 Attribute_Partition_ID |
8138 Attribute_Pool_Address |
8139 Attribute_Position |
8140 Attribute_Priority |
8141 Attribute_Read |
8142 Attribute_Result |
8143 Attribute_Scalar_Storage_Order |
8144 Attribute_Simple_Storage_Pool |
8145 Attribute_Storage_Pool |
8146 Attribute_Storage_Size |
8147 Attribute_Storage_Unit |
8148 Attribute_Stub_Type |
8149 Attribute_System_Allocator_Alignment |
8150 Attribute_Tag |
8151 Attribute_Target_Name |
8152 Attribute_Terminated |
8153 Attribute_To_Address |
8154 Attribute_Type_Key |
8155 Attribute_UET_Address |
8156 Attribute_Unchecked_Access |
8157 Attribute_Universal_Literal_String |
8158 Attribute_Unrestricted_Access |
8159 Attribute_Valid |
8160 Attribute_Valid_Scalars |
8161 Attribute_Value |
8162 Attribute_Wchar_T_Size |
8163 Attribute_Wide_Value |
8164 Attribute_Wide_Wide_Value |
8165 Attribute_Word_Size |
8166 Attribute_Write =>
8167
8168 raise Program_Error;
8169 end case;
8170
8171 -- At the end of the case, one more check. If we did a static evaluation
8172 -- so that the result is now a literal, then set Is_Static_Expression
8173 -- in the constant only if the prefix type is a static subtype. For
8174 -- non-static subtypes, the folding is still OK, but not static.
8175
8176 -- An exception is the GNAT attribute Constrained_Array which is
8177 -- defined to be a static attribute in all cases.
8178
8179 if Nkind_In (N, N_Integer_Literal,
8180 N_Real_Literal,
8181 N_Character_Literal,
8182 N_String_Literal)
8183 or else (Is_Entity_Name (N)
8184 and then Ekind (Entity (N)) = E_Enumeration_Literal)
8185 then
8186 Set_Is_Static_Expression (N, Static);
8187
8188 -- If this is still an attribute reference, then it has not been folded
8189 -- and that means that its expressions are in a non-static context.
8190
8191 elsif Nkind (N) = N_Attribute_Reference then
8192 Check_Expressions;
8193
8194 -- Note: the else case not covered here are odd cases where the
8195 -- processing has transformed the attribute into something other
8196 -- than a constant. Nothing more to do in such cases.
8197
8198 else
8199 null;
8200 end if;
8201 end Eval_Attribute;
8202
8203 ------------------------------
8204 -- Is_Anonymous_Tagged_Base --
8205 ------------------------------
8206
8207 function Is_Anonymous_Tagged_Base
8208 (Anon : Entity_Id;
8209 Typ : Entity_Id)
8210 return Boolean
8211 is
8212 begin
8213 return
8214 Anon = Current_Scope
8215 and then Is_Itype (Anon)
8216 and then Associated_Node_For_Itype (Anon) = Parent (Typ);
8217 end Is_Anonymous_Tagged_Base;
8218
8219 --------------------------------
8220 -- Name_Implies_Lvalue_Prefix --
8221 --------------------------------
8222
8223 function Name_Implies_Lvalue_Prefix (Nam : Name_Id) return Boolean is
8224 pragma Assert (Is_Attribute_Name (Nam));
8225 begin
8226 return Attribute_Name_Implies_Lvalue_Prefix (Get_Attribute_Id (Nam));
8227 end Name_Implies_Lvalue_Prefix;
8228
8229 -----------------------
8230 -- Resolve_Attribute --
8231 -----------------------
8232
8233 procedure Resolve_Attribute (N : Node_Id; Typ : Entity_Id) is
8234 Loc : constant Source_Ptr := Sloc (N);
8235 P : constant Node_Id := Prefix (N);
8236 Aname : constant Name_Id := Attribute_Name (N);
8237 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
8238 Btyp : constant Entity_Id := Base_Type (Typ);
8239 Des_Btyp : Entity_Id;
8240 Index : Interp_Index;
8241 It : Interp;
8242 Nom_Subt : Entity_Id;
8243
8244 procedure Accessibility_Message;
8245 -- Error, or warning within an instance, if the static accessibility
8246 -- rules of 3.10.2 are violated.
8247
8248 ---------------------------
8249 -- Accessibility_Message --
8250 ---------------------------
8251
8252 procedure Accessibility_Message is
8253 Indic : Node_Id := Parent (Parent (N));
8254
8255 begin
8256 -- In an instance, this is a runtime check, but one we
8257 -- know will fail, so generate an appropriate warning.
8258
8259 if In_Instance_Body then
8260 Error_Msg_F ("?non-local pointer cannot point to local object", P);
8261 Error_Msg_F
8262 ("\?Program_Error will be raised at run time", P);
8263 Rewrite (N,
8264 Make_Raise_Program_Error (Loc,
8265 Reason => PE_Accessibility_Check_Failed));
8266 Set_Etype (N, Typ);
8267 return;
8268
8269 else
8270 Error_Msg_F ("non-local pointer cannot point to local object", P);
8271
8272 -- Check for case where we have a missing access definition
8273
8274 if Is_Record_Type (Current_Scope)
8275 and then
8276 Nkind_In (Parent (N), N_Discriminant_Association,
8277 N_Index_Or_Discriminant_Constraint)
8278 then
8279 Indic := Parent (Parent (N));
8280 while Present (Indic)
8281 and then Nkind (Indic) /= N_Subtype_Indication
8282 loop
8283 Indic := Parent (Indic);
8284 end loop;
8285
8286 if Present (Indic) then
8287 Error_Msg_NE
8288 ("\use an access definition for" &
8289 " the access discriminant of&",
8290 N, Entity (Subtype_Mark (Indic)));
8291 end if;
8292 end if;
8293 end if;
8294 end Accessibility_Message;
8295
8296 -- Start of processing for Resolve_Attribute
8297
8298 begin
8299 -- If error during analysis, no point in continuing, except for array
8300 -- types, where we get better recovery by using unconstrained indexes
8301 -- than nothing at all (see Check_Array_Type).
8302
8303 if Error_Posted (N)
8304 and then Attr_Id /= Attribute_First
8305 and then Attr_Id /= Attribute_Last
8306 and then Attr_Id /= Attribute_Length
8307 and then Attr_Id /= Attribute_Range
8308 then
8309 return;
8310 end if;
8311
8312 -- If attribute was universal type, reset to actual type
8313
8314 if Etype (N) = Universal_Integer
8315 or else Etype (N) = Universal_Real
8316 then
8317 Set_Etype (N, Typ);
8318 end if;
8319
8320 -- Remaining processing depends on attribute
8321
8322 case Attr_Id is
8323
8324 ------------
8325 -- Access --
8326 ------------
8327
8328 -- For access attributes, if the prefix denotes an entity, it is
8329 -- interpreted as a name, never as a call. It may be overloaded,
8330 -- in which case resolution uses the profile of the context type.
8331 -- Otherwise prefix must be resolved.
8332
8333 when Attribute_Access
8334 | Attribute_Unchecked_Access
8335 | Attribute_Unrestricted_Access =>
8336
8337 Access_Attribute :
8338 begin
8339 if Is_Variable (P) then
8340 Note_Possible_Modification (P, Sure => False);
8341 end if;
8342
8343 -- The following comes from a query by Adam Beneschan, concerning
8344 -- improper use of universal_access in equality tests involving
8345 -- anonymous access types. Another good reason for 'Ref, but
8346 -- for now disable the test, which breaks several filed tests.
8347
8348 if Ekind (Typ) = E_Anonymous_Access_Type
8349 and then Nkind_In (Parent (N), N_Op_Eq, N_Op_Ne)
8350 and then False
8351 then
8352 Error_Msg_N ("need unique type to resolve 'Access", N);
8353 Error_Msg_N ("\qualify attribute with some access type", N);
8354 end if;
8355
8356 if Is_Entity_Name (P) then
8357 if Is_Overloaded (P) then
8358 Get_First_Interp (P, Index, It);
8359 while Present (It.Nam) loop
8360 if Type_Conformant (Designated_Type (Typ), It.Nam) then
8361 Set_Entity (P, It.Nam);
8362
8363 -- The prefix is definitely NOT overloaded anymore at
8364 -- this point, so we reset the Is_Overloaded flag to
8365 -- avoid any confusion when reanalyzing the node.
8366
8367 Set_Is_Overloaded (P, False);
8368 Set_Is_Overloaded (N, False);
8369 Generate_Reference (Entity (P), P);
8370 exit;
8371 end if;
8372
8373 Get_Next_Interp (Index, It);
8374 end loop;
8375
8376 -- If Prefix is a subprogram name, it is frozen by this
8377 -- reference:
8378
8379 -- If it is a type, there is nothing to resolve.
8380 -- If it is an object, complete its resolution.
8381
8382 elsif Is_Overloadable (Entity (P)) then
8383
8384 -- Avoid insertion of freeze actions in spec expression mode
8385
8386 if not In_Spec_Expression then
8387 Freeze_Before (N, Entity (P));
8388 end if;
8389
8390 elsif Is_Type (Entity (P)) then
8391 null;
8392 else
8393 Resolve (P);
8394 end if;
8395
8396 Error_Msg_Name_1 := Aname;
8397
8398 if not Is_Entity_Name (P) then
8399 null;
8400
8401 elsif Is_Overloadable (Entity (P))
8402 and then Is_Abstract_Subprogram (Entity (P))
8403 then
8404 Error_Msg_F ("prefix of % attribute cannot be abstract", P);
8405 Set_Etype (N, Any_Type);
8406
8407 elsif Convention (Entity (P)) = Convention_Intrinsic then
8408 if Ekind (Entity (P)) = E_Enumeration_Literal then
8409 Error_Msg_F
8410 ("prefix of % attribute cannot be enumeration literal",
8411 P);
8412 else
8413 Error_Msg_F
8414 ("prefix of % attribute cannot be intrinsic", P);
8415 end if;
8416
8417 Set_Etype (N, Any_Type);
8418 end if;
8419
8420 -- Assignments, return statements, components of aggregates,
8421 -- generic instantiations will require convention checks if
8422 -- the type is an access to subprogram. Given that there will
8423 -- also be accessibility checks on those, this is where the
8424 -- checks can eventually be centralized ???
8425
8426 if Ekind_In (Btyp, E_Access_Subprogram_Type,
8427 E_Anonymous_Access_Subprogram_Type,
8428 E_Access_Protected_Subprogram_Type,
8429 E_Anonymous_Access_Protected_Subprogram_Type)
8430 then
8431 -- Deal with convention mismatch
8432
8433 if Convention (Designated_Type (Btyp)) /=
8434 Convention (Entity (P))
8435 then
8436 Error_Msg_FE
8437 ("subprogram & has wrong convention", P, Entity (P));
8438 Error_Msg_FE
8439 ("\does not match convention of access type &",
8440 P, Btyp);
8441
8442 if not Has_Convention_Pragma (Btyp) then
8443 Error_Msg_FE
8444 ("\probable missing pragma Convention for &",
8445 P, Btyp);
8446 end if;
8447
8448 else
8449 Check_Subtype_Conformant
8450 (New_Id => Entity (P),
8451 Old_Id => Designated_Type (Btyp),
8452 Err_Loc => P);
8453 end if;
8454
8455 if Attr_Id = Attribute_Unchecked_Access then
8456 Error_Msg_Name_1 := Aname;
8457 Error_Msg_F
8458 ("attribute% cannot be applied to a subprogram", P);
8459
8460 elsif Aname = Name_Unrestricted_Access then
8461 null; -- Nothing to check
8462
8463 -- Check the static accessibility rule of 3.10.2(32).
8464 -- This rule also applies within the private part of an
8465 -- instantiation. This rule does not apply to anonymous
8466 -- access-to-subprogram types in access parameters.
8467
8468 elsif Attr_Id = Attribute_Access
8469 and then not In_Instance_Body
8470 and then
8471 (Ekind (Btyp) = E_Access_Subprogram_Type
8472 or else Is_Local_Anonymous_Access (Btyp))
8473
8474 and then Subprogram_Access_Level (Entity (P)) >
8475 Type_Access_Level (Btyp)
8476 then
8477 Error_Msg_F
8478 ("subprogram must not be deeper than access type", P);
8479
8480 -- Check the restriction of 3.10.2(32) that disallows the
8481 -- access attribute within a generic body when the ultimate
8482 -- ancestor of the type of the attribute is declared outside
8483 -- of the generic unit and the subprogram is declared within
8484 -- that generic unit. This includes any such attribute that
8485 -- occurs within the body of a generic unit that is a child
8486 -- of the generic unit where the subprogram is declared.
8487
8488 -- The rule also prohibits applying the attribute when the
8489 -- access type is a generic formal access type (since the
8490 -- level of the actual type is not known). This restriction
8491 -- does not apply when the attribute type is an anonymous
8492 -- access-to-subprogram type. Note that this check was
8493 -- revised by AI-229, because the originally Ada 95 rule
8494 -- was too lax. The original rule only applied when the
8495 -- subprogram was declared within the body of the generic,
8496 -- which allowed the possibility of dangling references).
8497 -- The rule was also too strict in some case, in that it
8498 -- didn't permit the access to be declared in the generic
8499 -- spec, whereas the revised rule does (as long as it's not
8500 -- a formal type).
8501
8502 -- There are a couple of subtleties of the test for applying
8503 -- the check that are worth noting. First, we only apply it
8504 -- when the levels of the subprogram and access type are the
8505 -- same (the case where the subprogram is statically deeper
8506 -- was applied above, and the case where the type is deeper
8507 -- is always safe). Second, we want the check to apply
8508 -- within nested generic bodies and generic child unit
8509 -- bodies, but not to apply to an attribute that appears in
8510 -- the generic unit's specification. This is done by testing
8511 -- that the attribute's innermost enclosing generic body is
8512 -- not the same as the innermost generic body enclosing the
8513 -- generic unit where the subprogram is declared (we don't
8514 -- want the check to apply when the access attribute is in
8515 -- the spec and there's some other generic body enclosing
8516 -- generic). Finally, there's no point applying the check
8517 -- when within an instance, because any violations will have
8518 -- been caught by the compilation of the generic unit.
8519
8520 -- Note that we relax this check in CodePeer mode for
8521 -- compatibility with legacy code, since CodePeer is an
8522 -- Ada source code analyzer, not a strict compiler.
8523 -- ??? Note that a better approach would be to have a
8524 -- separate switch to relax this rule, and enable this
8525 -- switch in CodePeer mode.
8526
8527 elsif Attr_Id = Attribute_Access
8528 and then not CodePeer_Mode
8529 and then not In_Instance
8530 and then Present (Enclosing_Generic_Unit (Entity (P)))
8531 and then Present (Enclosing_Generic_Body (N))
8532 and then Enclosing_Generic_Body (N) /=
8533 Enclosing_Generic_Body
8534 (Enclosing_Generic_Unit (Entity (P)))
8535 and then Subprogram_Access_Level (Entity (P)) =
8536 Type_Access_Level (Btyp)
8537 and then Ekind (Btyp) /=
8538 E_Anonymous_Access_Subprogram_Type
8539 and then Ekind (Btyp) /=
8540 E_Anonymous_Access_Protected_Subprogram_Type
8541 then
8542 -- The attribute type's ultimate ancestor must be
8543 -- declared within the same generic unit as the
8544 -- subprogram is declared. The error message is
8545 -- specialized to say "ancestor" for the case where the
8546 -- access type is not its own ancestor, since saying
8547 -- simply "access type" would be very confusing.
8548
8549 if Enclosing_Generic_Unit (Entity (P)) /=
8550 Enclosing_Generic_Unit (Root_Type (Btyp))
8551 then
8552 Error_Msg_N
8553 ("''Access attribute not allowed in generic body",
8554 N);
8555
8556 if Root_Type (Btyp) = Btyp then
8557 Error_Msg_NE
8558 ("\because " &
8559 "access type & is declared outside " &
8560 "generic unit (RM 3.10.2(32))", N, Btyp);
8561 else
8562 Error_Msg_NE
8563 ("\because ancestor of " &
8564 "access type & is declared outside " &
8565 "generic unit (RM 3.10.2(32))", N, Btyp);
8566 end if;
8567
8568 Error_Msg_NE
8569 ("\move ''Access to private part, or " &
8570 "(Ada 2005) use anonymous access type instead of &",
8571 N, Btyp);
8572
8573 -- If the ultimate ancestor of the attribute's type is
8574 -- a formal type, then the attribute is illegal because
8575 -- the actual type might be declared at a higher level.
8576 -- The error message is specialized to say "ancestor"
8577 -- for the case where the access type is not its own
8578 -- ancestor, since saying simply "access type" would be
8579 -- very confusing.
8580
8581 elsif Is_Generic_Type (Root_Type (Btyp)) then
8582 if Root_Type (Btyp) = Btyp then
8583 Error_Msg_N
8584 ("access type must not be a generic formal type",
8585 N);
8586 else
8587 Error_Msg_N
8588 ("ancestor access type must not be a generic " &
8589 "formal type", N);
8590 end if;
8591 end if;
8592 end if;
8593 end if;
8594
8595 -- If this is a renaming, an inherited operation, or a
8596 -- subprogram instance, use the original entity. This may make
8597 -- the node type-inconsistent, so this transformation can only
8598 -- be done if the node will not be reanalyzed. In particular,
8599 -- if it is within a default expression, the transformation
8600 -- must be delayed until the default subprogram is created for
8601 -- it, when the enclosing subprogram is frozen.
8602
8603 if Is_Entity_Name (P)
8604 and then Is_Overloadable (Entity (P))
8605 and then Present (Alias (Entity (P)))
8606 and then Expander_Active
8607 then
8608 Rewrite (P,
8609 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8610 end if;
8611
8612 elsif Nkind (P) = N_Selected_Component
8613 and then Is_Overloadable (Entity (Selector_Name (P)))
8614 then
8615 -- Protected operation. If operation is overloaded, must
8616 -- disambiguate. Prefix that denotes protected object itself
8617 -- is resolved with its own type.
8618
8619 if Attr_Id = Attribute_Unchecked_Access then
8620 Error_Msg_Name_1 := Aname;
8621 Error_Msg_F
8622 ("attribute% cannot be applied to protected operation", P);
8623 end if;
8624
8625 Resolve (Prefix (P));
8626 Generate_Reference (Entity (Selector_Name (P)), P);
8627
8628 elsif Is_Overloaded (P) then
8629
8630 -- Use the designated type of the context to disambiguate
8631 -- Note that this was not strictly conformant to Ada 95,
8632 -- but was the implementation adopted by most Ada 95 compilers.
8633 -- The use of the context type to resolve an Access attribute
8634 -- reference is now mandated in AI-235 for Ada 2005.
8635
8636 declare
8637 Index : Interp_Index;
8638 It : Interp;
8639
8640 begin
8641 Get_First_Interp (P, Index, It);
8642 while Present (It.Typ) loop
8643 if Covers (Designated_Type (Typ), It.Typ) then
8644 Resolve (P, It.Typ);
8645 exit;
8646 end if;
8647
8648 Get_Next_Interp (Index, It);
8649 end loop;
8650 end;
8651 else
8652 Resolve (P);
8653 end if;
8654
8655 -- X'Access is illegal if X denotes a constant and the access type
8656 -- is access-to-variable. Same for 'Unchecked_Access. The rule
8657 -- does not apply to 'Unrestricted_Access. If the reference is a
8658 -- default-initialized aggregate component for a self-referential
8659 -- type the reference is legal.
8660
8661 if not (Ekind (Btyp) = E_Access_Subprogram_Type
8662 or else Ekind (Btyp) = E_Anonymous_Access_Subprogram_Type
8663 or else (Is_Record_Type (Btyp)
8664 and then
8665 Present (Corresponding_Remote_Type (Btyp)))
8666 or else Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8667 or else Ekind (Btyp)
8668 = E_Anonymous_Access_Protected_Subprogram_Type
8669 or else Is_Access_Constant (Btyp)
8670 or else Is_Variable (P)
8671 or else Attr_Id = Attribute_Unrestricted_Access)
8672 then
8673 if Is_Entity_Name (P)
8674 and then Is_Type (Entity (P))
8675 then
8676 -- Legality of a self-reference through an access
8677 -- attribute has been verified in Analyze_Access_Attribute.
8678
8679 null;
8680
8681 elsif Comes_From_Source (N) then
8682 Error_Msg_F ("access-to-variable designates constant", P);
8683 end if;
8684 end if;
8685
8686 Des_Btyp := Designated_Type (Btyp);
8687
8688 if Ada_Version >= Ada_2005
8689 and then Is_Incomplete_Type (Des_Btyp)
8690 then
8691 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
8692 -- imported entity, and the non-limited view is visible, make
8693 -- use of it. If it is an incomplete subtype, use the base type
8694 -- in any case.
8695
8696 if From_With_Type (Des_Btyp)
8697 and then Present (Non_Limited_View (Des_Btyp))
8698 then
8699 Des_Btyp := Non_Limited_View (Des_Btyp);
8700
8701 elsif Ekind (Des_Btyp) = E_Incomplete_Subtype then
8702 Des_Btyp := Etype (Des_Btyp);
8703 end if;
8704 end if;
8705
8706 if (Attr_Id = Attribute_Access
8707 or else
8708 Attr_Id = Attribute_Unchecked_Access)
8709 and then (Ekind (Btyp) = E_General_Access_Type
8710 or else Ekind (Btyp) = E_Anonymous_Access_Type)
8711 then
8712 -- Ada 2005 (AI-230): Check the accessibility of anonymous
8713 -- access types for stand-alone objects, record and array
8714 -- components, and return objects. For a component definition
8715 -- the level is the same of the enclosing composite type.
8716
8717 if Ada_Version >= Ada_2005
8718 and then (Is_Local_Anonymous_Access (Btyp)
8719
8720 -- Handle cases where Btyp is the anonymous access
8721 -- type of an Ada 2012 stand-alone object.
8722
8723 or else Nkind (Associated_Node_For_Itype (Btyp)) =
8724 N_Object_Declaration)
8725 and then
8726 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
8727 and then Attr_Id = Attribute_Access
8728 then
8729 -- In an instance, this is a runtime check, but one we
8730 -- know will fail, so generate an appropriate warning.
8731
8732 if In_Instance_Body then
8733 Error_Msg_F
8734 ("?non-local pointer cannot point to local object", P);
8735 Error_Msg_F
8736 ("\?Program_Error will be raised at run time", P);
8737 Rewrite (N,
8738 Make_Raise_Program_Error (Loc,
8739 Reason => PE_Accessibility_Check_Failed));
8740 Set_Etype (N, Typ);
8741
8742 else
8743 Error_Msg_F
8744 ("non-local pointer cannot point to local object", P);
8745 end if;
8746 end if;
8747
8748 if Is_Dependent_Component_Of_Mutable_Object (P) then
8749 Error_Msg_F
8750 ("illegal attribute for discriminant-dependent component",
8751 P);
8752 end if;
8753
8754 -- Check static matching rule of 3.10.2(27). Nominal subtype
8755 -- of the prefix must statically match the designated type.
8756
8757 Nom_Subt := Etype (P);
8758
8759 if Is_Constr_Subt_For_U_Nominal (Nom_Subt) then
8760 Nom_Subt := Base_Type (Nom_Subt);
8761 end if;
8762
8763 if Is_Tagged_Type (Designated_Type (Typ)) then
8764
8765 -- If the attribute is in the context of an access
8766 -- parameter, then the prefix is allowed to be of the
8767 -- class-wide type (by AI-127).
8768
8769 if Ekind (Typ) = E_Anonymous_Access_Type then
8770 if not Covers (Designated_Type (Typ), Nom_Subt)
8771 and then not Covers (Nom_Subt, Designated_Type (Typ))
8772 then
8773 declare
8774 Desig : Entity_Id;
8775
8776 begin
8777 Desig := Designated_Type (Typ);
8778
8779 if Is_Class_Wide_Type (Desig) then
8780 Desig := Etype (Desig);
8781 end if;
8782
8783 if Is_Anonymous_Tagged_Base (Nom_Subt, Desig) then
8784 null;
8785
8786 else
8787 Error_Msg_FE
8788 ("type of prefix: & not compatible",
8789 P, Nom_Subt);
8790 Error_Msg_FE
8791 ("\with &, the expected designated type",
8792 P, Designated_Type (Typ));
8793 end if;
8794 end;
8795 end if;
8796
8797 elsif not Covers (Designated_Type (Typ), Nom_Subt)
8798 or else
8799 (not Is_Class_Wide_Type (Designated_Type (Typ))
8800 and then Is_Class_Wide_Type (Nom_Subt))
8801 then
8802 Error_Msg_FE
8803 ("type of prefix: & is not covered", P, Nom_Subt);
8804 Error_Msg_FE
8805 ("\by &, the expected designated type" &
8806 " (RM 3.10.2 (27))", P, Designated_Type (Typ));
8807 end if;
8808
8809 if Is_Class_Wide_Type (Designated_Type (Typ))
8810 and then Has_Discriminants (Etype (Designated_Type (Typ)))
8811 and then Is_Constrained (Etype (Designated_Type (Typ)))
8812 and then Designated_Type (Typ) /= Nom_Subt
8813 then
8814 Apply_Discriminant_Check
8815 (N, Etype (Designated_Type (Typ)));
8816 end if;
8817
8818 -- Ada 2005 (AI-363): Require static matching when designated
8819 -- type has discriminants and a constrained partial view, since
8820 -- in general objects of such types are mutable, so we can't
8821 -- allow the access value to designate a constrained object
8822 -- (because access values must be assumed to designate mutable
8823 -- objects when designated type does not impose a constraint).
8824
8825 elsif Subtypes_Statically_Match (Des_Btyp, Nom_Subt) then
8826 null;
8827
8828 elsif Has_Discriminants (Designated_Type (Typ))
8829 and then not Is_Constrained (Des_Btyp)
8830 and then
8831 (Ada_Version < Ada_2005
8832 or else
8833 not Effectively_Has_Constrained_Partial_View
8834 (Typ => Designated_Type (Base_Type (Typ)),
8835 Scop => Current_Scope))
8836 then
8837 null;
8838
8839 else
8840 Error_Msg_F
8841 ("object subtype must statically match "
8842 & "designated subtype", P);
8843
8844 if Is_Entity_Name (P)
8845 and then Is_Array_Type (Designated_Type (Typ))
8846 then
8847 declare
8848 D : constant Node_Id := Declaration_Node (Entity (P));
8849 begin
8850 Error_Msg_N ("aliased object has explicit bounds?",
8851 D);
8852 Error_Msg_N ("\declare without bounds"
8853 & " (and with explicit initialization)?", D);
8854 Error_Msg_N ("\for use with unconstrained access?", D);
8855 end;
8856 end if;
8857 end if;
8858
8859 -- Check the static accessibility rule of 3.10.2(28). Note that
8860 -- this check is not performed for the case of an anonymous
8861 -- access type, since the access attribute is always legal
8862 -- in such a context.
8863
8864 if Attr_Id /= Attribute_Unchecked_Access
8865 and then
8866 Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
8867 and then Ekind (Btyp) = E_General_Access_Type
8868 then
8869 Accessibility_Message;
8870 return;
8871 end if;
8872 end if;
8873
8874 if Ekind_In (Btyp, E_Access_Protected_Subprogram_Type,
8875 E_Anonymous_Access_Protected_Subprogram_Type)
8876 then
8877 if Is_Entity_Name (P)
8878 and then not Is_Protected_Type (Scope (Entity (P)))
8879 then
8880 Error_Msg_F ("context requires a protected subprogram", P);
8881
8882 -- Check accessibility of protected object against that of the
8883 -- access type, but only on user code, because the expander
8884 -- creates access references for handlers. If the context is an
8885 -- anonymous_access_to_protected, there are no accessibility
8886 -- checks either. Omit check entirely for Unrestricted_Access.
8887
8888 elsif Object_Access_Level (P) > Deepest_Type_Access_Level (Btyp)
8889 and then Comes_From_Source (N)
8890 and then Ekind (Btyp) = E_Access_Protected_Subprogram_Type
8891 and then Attr_Id /= Attribute_Unrestricted_Access
8892 then
8893 Accessibility_Message;
8894 return;
8895 end if;
8896
8897 elsif Ekind_In (Btyp, E_Access_Subprogram_Type,
8898 E_Anonymous_Access_Subprogram_Type)
8899 and then Ekind (Etype (N)) = E_Access_Protected_Subprogram_Type
8900 then
8901 Error_Msg_F ("context requires a non-protected subprogram", P);
8902 end if;
8903
8904 -- The context cannot be a pool-specific type, but this is a
8905 -- legality rule, not a resolution rule, so it must be checked
8906 -- separately, after possibly disambiguation (see AI-245).
8907
8908 if Ekind (Btyp) = E_Access_Type
8909 and then Attr_Id /= Attribute_Unrestricted_Access
8910 then
8911 Wrong_Type (N, Typ);
8912 end if;
8913
8914 -- The context may be a constrained access type (however ill-
8915 -- advised such subtypes might be) so in order to generate a
8916 -- constraint check when needed set the type of the attribute
8917 -- reference to the base type of the context.
8918
8919 Set_Etype (N, Btyp);
8920
8921 -- Check for incorrect atomic/volatile reference (RM C.6(12))
8922
8923 if Attr_Id /= Attribute_Unrestricted_Access then
8924 if Is_Atomic_Object (P)
8925 and then not Is_Atomic (Designated_Type (Typ))
8926 then
8927 Error_Msg_F
8928 ("access to atomic object cannot yield access-to-" &
8929 "non-atomic type", P);
8930
8931 elsif Is_Volatile_Object (P)
8932 and then not Is_Volatile (Designated_Type (Typ))
8933 then
8934 Error_Msg_F
8935 ("access to volatile object cannot yield access-to-" &
8936 "non-volatile type", P);
8937 end if;
8938 end if;
8939
8940 if Is_Entity_Name (P) then
8941 Set_Address_Taken (Entity (P));
8942 end if;
8943 end Access_Attribute;
8944
8945 -------------
8946 -- Address --
8947 -------------
8948
8949 -- Deal with resolving the type for Address attribute, overloading
8950 -- is not permitted here, since there is no context to resolve it.
8951
8952 when Attribute_Address | Attribute_Code_Address =>
8953 Address_Attribute : begin
8954
8955 -- To be safe, assume that if the address of a variable is taken,
8956 -- it may be modified via this address, so note modification.
8957
8958 if Is_Variable (P) then
8959 Note_Possible_Modification (P, Sure => False);
8960 end if;
8961
8962 if Nkind (P) in N_Subexpr
8963 and then Is_Overloaded (P)
8964 then
8965 Get_First_Interp (P, Index, It);
8966 Get_Next_Interp (Index, It);
8967
8968 if Present (It.Nam) then
8969 Error_Msg_Name_1 := Aname;
8970 Error_Msg_F
8971 ("prefix of % attribute cannot be overloaded", P);
8972 end if;
8973 end if;
8974
8975 if not Is_Entity_Name (P)
8976 or else not Is_Overloadable (Entity (P))
8977 then
8978 if not Is_Task_Type (Etype (P))
8979 or else Nkind (P) = N_Explicit_Dereference
8980 then
8981 Resolve (P);
8982 end if;
8983 end if;
8984
8985 -- If this is the name of a derived subprogram, or that of a
8986 -- generic actual, the address is that of the original entity.
8987
8988 if Is_Entity_Name (P)
8989 and then Is_Overloadable (Entity (P))
8990 and then Present (Alias (Entity (P)))
8991 then
8992 Rewrite (P,
8993 New_Occurrence_Of (Alias (Entity (P)), Sloc (P)));
8994 end if;
8995
8996 if Is_Entity_Name (P) then
8997 Set_Address_Taken (Entity (P));
8998 end if;
8999
9000 if Nkind (P) = N_Slice then
9001
9002 -- Arr (X .. Y)'address is identical to Arr (X)'address,
9003 -- even if the array is packed and the slice itself is not
9004 -- addressable. Transform the prefix into an indexed component.
9005
9006 -- Note that the transformation is safe only if we know that
9007 -- the slice is non-null. That is because a null slice can have
9008 -- an out of bounds index value.
9009
9010 -- Right now, gigi blows up if given 'Address on a slice as a
9011 -- result of some incorrect freeze nodes generated by the front
9012 -- end, and this covers up that bug in one case, but the bug is
9013 -- likely still there in the cases not handled by this code ???
9014
9015 -- It's not clear what 'Address *should* return for a null
9016 -- slice with out of bounds indexes, this might be worth an ARG
9017 -- discussion ???
9018
9019 -- One approach would be to do a length check unconditionally,
9020 -- and then do the transformation below unconditionally, but
9021 -- analyze with checks off, avoiding the problem of the out of
9022 -- bounds index. This approach would interpret the address of
9023 -- an out of bounds null slice as being the address where the
9024 -- array element would be if there was one, which is probably
9025 -- as reasonable an interpretation as any ???
9026
9027 declare
9028 Loc : constant Source_Ptr := Sloc (P);
9029 D : constant Node_Id := Discrete_Range (P);
9030 Lo : Node_Id;
9031
9032 begin
9033 if Is_Entity_Name (D)
9034 and then
9035 Not_Null_Range
9036 (Type_Low_Bound (Entity (D)),
9037 Type_High_Bound (Entity (D)))
9038 then
9039 Lo :=
9040 Make_Attribute_Reference (Loc,
9041 Prefix => (New_Occurrence_Of (Entity (D), Loc)),
9042 Attribute_Name => Name_First);
9043
9044 elsif Nkind (D) = N_Range
9045 and then Not_Null_Range (Low_Bound (D), High_Bound (D))
9046 then
9047 Lo := Low_Bound (D);
9048
9049 else
9050 Lo := Empty;
9051 end if;
9052
9053 if Present (Lo) then
9054 Rewrite (P,
9055 Make_Indexed_Component (Loc,
9056 Prefix => Relocate_Node (Prefix (P)),
9057 Expressions => New_List (Lo)));
9058
9059 Analyze_And_Resolve (P);
9060 end if;
9061 end;
9062 end if;
9063 end Address_Attribute;
9064
9065 ---------------
9066 -- AST_Entry --
9067 ---------------
9068
9069 -- Prefix of the AST_Entry attribute is an entry name which must
9070 -- not be resolved, since this is definitely not an entry call.
9071
9072 when Attribute_AST_Entry =>
9073 null;
9074
9075 ------------------
9076 -- Body_Version --
9077 ------------------
9078
9079 -- Prefix of Body_Version attribute can be a subprogram name which
9080 -- must not be resolved, since this is not a call.
9081
9082 when Attribute_Body_Version =>
9083 null;
9084
9085 ------------
9086 -- Caller --
9087 ------------
9088
9089 -- Prefix of Caller attribute is an entry name which must not
9090 -- be resolved, since this is definitely not an entry call.
9091
9092 when Attribute_Caller =>
9093 null;
9094
9095 ------------------
9096 -- Code_Address --
9097 ------------------
9098
9099 -- Shares processing with Address attribute
9100
9101 -----------
9102 -- Count --
9103 -----------
9104
9105 -- If the prefix of the Count attribute is an entry name it must not
9106 -- be resolved, since this is definitely not an entry call. However,
9107 -- if it is an element of an entry family, the index itself may
9108 -- have to be resolved because it can be a general expression.
9109
9110 when Attribute_Count =>
9111 if Nkind (P) = N_Indexed_Component
9112 and then Is_Entity_Name (Prefix (P))
9113 then
9114 declare
9115 Indx : constant Node_Id := First (Expressions (P));
9116 Fam : constant Entity_Id := Entity (Prefix (P));
9117 begin
9118 Resolve (Indx, Entry_Index_Type (Fam));
9119 Apply_Range_Check (Indx, Entry_Index_Type (Fam));
9120 end;
9121 end if;
9122
9123 ----------------
9124 -- Elaborated --
9125 ----------------
9126
9127 -- Prefix of the Elaborated attribute is a subprogram name which
9128 -- must not be resolved, since this is definitely not a call. Note
9129 -- that it is a library unit, so it cannot be overloaded here.
9130
9131 when Attribute_Elaborated =>
9132 null;
9133
9134 -------------
9135 -- Enabled --
9136 -------------
9137
9138 -- Prefix of Enabled attribute is a check name, which must be treated
9139 -- specially and not touched by Resolve.
9140
9141 when Attribute_Enabled =>
9142 null;
9143
9144 --------------------
9145 -- Mechanism_Code --
9146 --------------------
9147
9148 -- Prefix of the Mechanism_Code attribute is a function name
9149 -- which must not be resolved. Should we check for overloaded ???
9150
9151 when Attribute_Mechanism_Code =>
9152 null;
9153
9154 ------------------
9155 -- Partition_ID --
9156 ------------------
9157
9158 -- Most processing is done in sem_dist, after determining the
9159 -- context type. Node is rewritten as a conversion to a runtime call.
9160
9161 when Attribute_Partition_ID =>
9162 Process_Partition_Id (N);
9163 return;
9164
9165 ------------------
9166 -- Pool_Address --
9167 ------------------
9168
9169 when Attribute_Pool_Address =>
9170 Resolve (P);
9171
9172 -----------
9173 -- Range --
9174 -----------
9175
9176 -- We replace the Range attribute node with a range expression whose
9177 -- bounds are the 'First and 'Last attributes applied to the same
9178 -- prefix. The reason that we do this transformation here instead of
9179 -- in the expander is that it simplifies other parts of the semantic
9180 -- analysis which assume that the Range has been replaced; thus it
9181 -- must be done even when in semantic-only mode (note that the RM
9182 -- specifically mentions this equivalence, we take care that the
9183 -- prefix is only evaluated once).
9184
9185 when Attribute_Range => Range_Attribute :
9186 declare
9187 LB : Node_Id;
9188 HB : Node_Id;
9189 Dims : List_Id;
9190
9191 begin
9192 if not Is_Entity_Name (P)
9193 or else not Is_Type (Entity (P))
9194 then
9195 Resolve (P);
9196 end if;
9197
9198 Dims := Expressions (N);
9199
9200 HB :=
9201 Make_Attribute_Reference (Loc,
9202 Prefix =>
9203 Duplicate_Subexpr (P, Name_Req => True),
9204 Attribute_Name => Name_Last,
9205 Expressions => Dims);
9206
9207 LB :=
9208 Make_Attribute_Reference (Loc,
9209 Prefix => P,
9210 Attribute_Name => Name_First,
9211 Expressions => (Dims));
9212
9213 -- Do not share the dimension indicator, if present. Even
9214 -- though it is a static constant, its source location
9215 -- may be modified when printing expanded code and node
9216 -- sharing will lead to chaos in Sprint.
9217
9218 if Present (Dims) then
9219 Set_Expressions (LB,
9220 New_List (New_Copy_Tree (First (Dims))));
9221 end if;
9222
9223 -- If the original was marked as Must_Not_Freeze (see code
9224 -- in Sem_Ch3.Make_Index), then make sure the rewriting
9225 -- does not freeze either.
9226
9227 if Must_Not_Freeze (N) then
9228 Set_Must_Not_Freeze (HB);
9229 Set_Must_Not_Freeze (LB);
9230 Set_Must_Not_Freeze (Prefix (HB));
9231 Set_Must_Not_Freeze (Prefix (LB));
9232 end if;
9233
9234 if Raises_Constraint_Error (Prefix (N)) then
9235
9236 -- Preserve Sloc of prefix in the new bounds, so that
9237 -- the posted warning can be removed if we are within
9238 -- unreachable code.
9239
9240 Set_Sloc (LB, Sloc (Prefix (N)));
9241 Set_Sloc (HB, Sloc (Prefix (N)));
9242 end if;
9243
9244 Rewrite (N, Make_Range (Loc, LB, HB));
9245 Analyze_And_Resolve (N, Typ);
9246
9247 -- Ensure that the expanded range does not have side effects
9248
9249 Force_Evaluation (LB);
9250 Force_Evaluation (HB);
9251
9252 -- Normally after resolving attribute nodes, Eval_Attribute
9253 -- is called to do any possible static evaluation of the node.
9254 -- However, here since the Range attribute has just been
9255 -- transformed into a range expression it is no longer an
9256 -- attribute node and therefore the call needs to be avoided
9257 -- and is accomplished by simply returning from the procedure.
9258
9259 return;
9260 end Range_Attribute;
9261
9262 ------------
9263 -- Result --
9264 ------------
9265
9266 -- We will only come here during the prescan of a spec expression
9267 -- containing a Result attribute. In that case the proper Etype has
9268 -- already been set, and nothing more needs to be done here.
9269
9270 when Attribute_Result =>
9271 null;
9272
9273 -----------------
9274 -- UET_Address --
9275 -----------------
9276
9277 -- Prefix must not be resolved in this case, since it is not a
9278 -- real entity reference. No action of any kind is require!
9279
9280 when Attribute_UET_Address =>
9281 return;
9282
9283 ----------------------
9284 -- Unchecked_Access --
9285 ----------------------
9286
9287 -- Processing is shared with Access
9288
9289 -------------------------
9290 -- Unrestricted_Access --
9291 -------------------------
9292
9293 -- Processing is shared with Access
9294
9295 ---------
9296 -- Val --
9297 ---------
9298
9299 -- Apply range check. Note that we did not do this during the
9300 -- analysis phase, since we wanted Eval_Attribute to have a
9301 -- chance at finding an illegal out of range value.
9302
9303 when Attribute_Val =>
9304
9305 -- Note that we do our own Eval_Attribute call here rather than
9306 -- use the common one, because we need to do processing after
9307 -- the call, as per above comment.
9308
9309 Eval_Attribute (N);
9310
9311 -- Eval_Attribute may replace the node with a raise CE, or
9312 -- fold it to a constant. Obviously we only apply a scalar
9313 -- range check if this did not happen!
9314
9315 if Nkind (N) = N_Attribute_Reference
9316 and then Attribute_Name (N) = Name_Val
9317 then
9318 Apply_Scalar_Range_Check (First (Expressions (N)), Btyp);
9319 end if;
9320
9321 return;
9322
9323 -------------
9324 -- Version --
9325 -------------
9326
9327 -- Prefix of Version attribute can be a subprogram name which
9328 -- must not be resolved, since this is not a call.
9329
9330 when Attribute_Version =>
9331 null;
9332
9333 ----------------------
9334 -- Other Attributes --
9335 ----------------------
9336
9337 -- For other attributes, resolve prefix unless it is a type. If
9338 -- the attribute reference itself is a type name ('Base and 'Class)
9339 -- then this is only legal within a task or protected record.
9340
9341 when others =>
9342 if not Is_Entity_Name (P)
9343 or else not Is_Type (Entity (P))
9344 then
9345 Resolve (P);
9346 end if;
9347
9348 -- If the attribute reference itself is a type name ('Base,
9349 -- 'Class) then this is only legal within a task or protected
9350 -- record. What is this all about ???
9351
9352 if Is_Entity_Name (N)
9353 and then Is_Type (Entity (N))
9354 then
9355 if Is_Concurrent_Type (Entity (N))
9356 and then In_Open_Scopes (Entity (P))
9357 then
9358 null;
9359 else
9360 Error_Msg_N
9361 ("invalid use of subtype name in expression or call", N);
9362 end if;
9363 end if;
9364
9365 -- For attributes whose argument may be a string, complete
9366 -- resolution of argument now. This avoids premature expansion
9367 -- (and the creation of transient scopes) before the attribute
9368 -- reference is resolved.
9369
9370 case Attr_Id is
9371 when Attribute_Value =>
9372 Resolve (First (Expressions (N)), Standard_String);
9373
9374 when Attribute_Wide_Value =>
9375 Resolve (First (Expressions (N)), Standard_Wide_String);
9376
9377 when Attribute_Wide_Wide_Value =>
9378 Resolve (First (Expressions (N)), Standard_Wide_Wide_String);
9379
9380 when others => null;
9381 end case;
9382
9383 -- If the prefix of the attribute is a class-wide type then it
9384 -- will be expanded into a dispatching call to a predefined
9385 -- primitive. Therefore we must check for potential violation
9386 -- of such restriction.
9387
9388 if Is_Class_Wide_Type (Etype (P)) then
9389 Check_Restriction (No_Dispatching_Calls, N);
9390 end if;
9391 end case;
9392
9393 -- Normally the Freezing is done by Resolve but sometimes the Prefix
9394 -- is not resolved, in which case the freezing must be done now.
9395
9396 Freeze_Expression (P);
9397
9398 -- Finally perform static evaluation on the attribute reference
9399
9400 Analyze_Dimension (N);
9401 Eval_Attribute (N);
9402 end Resolve_Attribute;
9403
9404 --------------------------------
9405 -- Stream_Attribute_Available --
9406 --------------------------------
9407
9408 function Stream_Attribute_Available
9409 (Typ : Entity_Id;
9410 Nam : TSS_Name_Type;
9411 Partial_View : Node_Id := Empty) return Boolean
9412 is
9413 Etyp : Entity_Id := Typ;
9414
9415 -- Start of processing for Stream_Attribute_Available
9416
9417 begin
9418 -- We need some comments in this body ???
9419
9420 if Has_Stream_Attribute_Definition (Typ, Nam) then
9421 return True;
9422 end if;
9423
9424 if Is_Class_Wide_Type (Typ) then
9425 return not Is_Limited_Type (Typ)
9426 or else Stream_Attribute_Available (Etype (Typ), Nam);
9427 end if;
9428
9429 if Nam = TSS_Stream_Input
9430 and then Is_Abstract_Type (Typ)
9431 and then not Is_Class_Wide_Type (Typ)
9432 then
9433 return False;
9434 end if;
9435
9436 if not (Is_Limited_Type (Typ)
9437 or else (Present (Partial_View)
9438 and then Is_Limited_Type (Partial_View)))
9439 then
9440 return True;
9441 end if;
9442
9443 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
9444
9445 if Nam = TSS_Stream_Input
9446 and then Ada_Version >= Ada_2005
9447 and then Stream_Attribute_Available (Etyp, TSS_Stream_Read)
9448 then
9449 return True;
9450
9451 elsif Nam = TSS_Stream_Output
9452 and then Ada_Version >= Ada_2005
9453 and then Stream_Attribute_Available (Etyp, TSS_Stream_Write)
9454 then
9455 return True;
9456 end if;
9457
9458 -- Case of Read and Write: check for attribute definition clause that
9459 -- applies to an ancestor type.
9460
9461 while Etype (Etyp) /= Etyp loop
9462 Etyp := Etype (Etyp);
9463
9464 if Has_Stream_Attribute_Definition (Etyp, Nam) then
9465 return True;
9466 end if;
9467 end loop;
9468
9469 if Ada_Version < Ada_2005 then
9470
9471 -- In Ada 95 mode, also consider a non-visible definition
9472
9473 declare
9474 Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
9475 begin
9476 return Btyp /= Typ
9477 and then Stream_Attribute_Available
9478 (Btyp, Nam, Partial_View => Typ);
9479 end;
9480 end if;
9481
9482 return False;
9483 end Stream_Attribute_Available;
9484
9485 end Sem_Attr;
This page took 0.483883 seconds and 5 git commands to generate.