Debian Codegraph @

gcc/ada/sem_attr.adb

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