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