a-cohase.ads, [...]: Update to latest RM version.
[official-gcc.git] / gcc / ada / exp_attr.adb
blob14d9da1609a7cadc21ed50348d221f7c91ec7579
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ A T T R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2011, 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 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Exp_Atag; use Exp_Atag;
31 with Exp_Ch2; use Exp_Ch2;
32 with Exp_Ch3; use Exp_Ch3;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch9; use Exp_Ch9;
35 with Exp_Dist; use Exp_Dist;
36 with Exp_Imgv; use Exp_Imgv;
37 with Exp_Pakd; use Exp_Pakd;
38 with Exp_Strm; use Exp_Strm;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Exp_VFpt; use Exp_VFpt;
42 with Fname; use Fname;
43 with Freeze; use Freeze;
44 with Gnatvsn; use Gnatvsn;
45 with Itypes; use Itypes;
46 with Lib; use Lib;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Nlists; use Nlists;
50 with Opt; use Opt;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sinfo; use Sinfo;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Stringt; use Stringt;
66 with Targparm; use Targparm;
67 with Tbuild; use Tbuild;
68 with Ttypes; use Ttypes;
69 with Uintp; use Uintp;
70 with Uname; use Uname;
71 with Validsw; use Validsw;
73 package body Exp_Attr is
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Compile_Stream_Body_In_Scope
80 (N : Node_Id;
81 Decl : Node_Id;
82 Arr : Entity_Id;
83 Check : Boolean);
84 -- The body for a stream subprogram may be generated outside of the scope
85 -- of the type. If the type is fully private, it may depend on the full
86 -- view of other types (e.g. indexes) that are currently private as well.
87 -- We install the declarations of the package in which the type is declared
88 -- before compiling the body in what is its proper environment. The Check
89 -- parameter indicates if checks are to be suppressed for the stream body.
90 -- We suppress checks for array/record reads, since the rule is that these
91 -- are like assignments, out of range values due to uninitialized storage,
92 -- or other invalid values do NOT cause a Constraint_Error to be raised.
94 procedure Expand_Access_To_Protected_Op
95 (N : Node_Id;
96 Pref : Node_Id;
97 Typ : Entity_Id);
98 -- An attribute reference to a protected subprogram is transformed into
99 -- a pair of pointers: one to the object, and one to the operations.
100 -- This expansion is performed for 'Access and for 'Unrestricted_Access.
102 procedure Expand_Fpt_Attribute
103 (N : Node_Id;
104 Pkg : RE_Id;
105 Nam : Name_Id;
106 Args : List_Id);
107 -- This procedure expands a call to a floating-point attribute function.
108 -- N is the attribute reference node, and Args is a list of arguments to
109 -- be passed to the function call. Pkg identifies the package containing
110 -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args
111 -- have already been converted to the floating-point type for which Pkg was
112 -- instantiated. The Nam argument is the relevant attribute processing
113 -- routine to be called. This is the same as the attribute name, except in
114 -- the Unaligned_Valid case.
116 procedure Expand_Fpt_Attribute_R (N : Node_Id);
117 -- This procedure expands a call to a floating-point attribute function
118 -- that takes a single floating-point argument. The function to be called
119 -- is always the same as the attribute name.
121 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
122 -- This procedure expands a call to a floating-point attribute function
123 -- that takes one floating-point argument and one integer argument. The
124 -- function to be called is always the same as the attribute name.
126 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
127 -- This procedure expands a call to a floating-point attribute function
128 -- that takes two floating-point arguments. The function to be called
129 -- is always the same as the attribute name.
131 procedure Expand_Pred_Succ (N : Node_Id);
132 -- Handles expansion of Pred or Succ attributes for case of non-real
133 -- operand with overflow checking required.
135 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
136 -- Used for Last, Last, and Length, when the prefix is an array type.
137 -- Obtains the corresponding index subtype.
139 procedure Find_Fat_Info
140 (T : Entity_Id;
141 Fat_Type : out Entity_Id;
142 Fat_Pkg : out RE_Id);
143 -- Given a floating-point type T, identifies the package containing the
144 -- attributes for this type (returned in Fat_Pkg), and the corresponding
145 -- type for which this package was instantiated from Fat_Gen. Error if T
146 -- is not a floating-point type.
148 function Find_Stream_Subprogram
149 (Typ : Entity_Id;
150 Nam : TSS_Name_Type) return Entity_Id;
151 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
152 -- types, the corresponding primitive operation is looked up, else the
153 -- appropriate TSS from the type itself, or from its closest ancestor
154 -- defining it, is returned. In both cases, inheritance of representation
155 -- aspects is thus taken into account.
157 function Full_Base (T : Entity_Id) return Entity_Id;
158 -- The stream functions need to examine the underlying representation of
159 -- composite types. In some cases T may be non-private but its base type
160 -- is, in which case the function returns the corresponding full view.
162 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id;
163 -- Given a type, find a corresponding stream convert pragma that applies to
164 -- the implementation base type of this type (Typ). If found, return the
165 -- pragma node, otherwise return Empty if no pragma is found.
167 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
168 -- Utility for array attributes, returns true on packed constrained
169 -- arrays, and on access to same.
171 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean;
172 -- Returns true iff the given node refers to an attribute call that
173 -- can be expanded directly by the back end and does not need front end
174 -- expansion. Typically used for rounding and truncation attributes that
175 -- appear directly inside a conversion to integer.
177 ----------------------------------
178 -- Compile_Stream_Body_In_Scope --
179 ----------------------------------
181 procedure Compile_Stream_Body_In_Scope
182 (N : Node_Id;
183 Decl : Node_Id;
184 Arr : Entity_Id;
185 Check : Boolean)
187 Installed : Boolean := False;
188 Scop : constant Entity_Id := Scope (Arr);
189 Curr : constant Entity_Id := Current_Scope;
191 begin
192 if Is_Hidden (Arr)
193 and then not In_Open_Scopes (Scop)
194 and then Ekind (Scop) = E_Package
195 then
196 Push_Scope (Scop);
197 Install_Visible_Declarations (Scop);
198 Install_Private_Declarations (Scop);
199 Installed := True;
201 -- The entities in the package are now visible, but the generated
202 -- stream entity must appear in the current scope (usually an
203 -- enclosing stream function) so that itypes all have their proper
204 -- scopes.
206 Push_Scope (Curr);
207 end if;
209 if Check then
210 Insert_Action (N, Decl);
211 else
212 Insert_Action (N, Decl, Suppress => All_Checks);
213 end if;
215 if Installed then
217 -- Remove extra copy of current scope, and package itself
219 Pop_Scope;
220 End_Package_Scope (Scop);
221 end if;
222 end Compile_Stream_Body_In_Scope;
224 -----------------------------------
225 -- Expand_Access_To_Protected_Op --
226 -----------------------------------
228 procedure Expand_Access_To_Protected_Op
229 (N : Node_Id;
230 Pref : Node_Id;
231 Typ : Entity_Id)
233 -- The value of the attribute_reference is a record containing two
234 -- fields: an access to the protected object, and an access to the
235 -- subprogram itself. The prefix is a selected component.
237 Loc : constant Source_Ptr := Sloc (N);
238 Agg : Node_Id;
239 Btyp : constant Entity_Id := Base_Type (Typ);
240 Sub : Entity_Id;
241 Sub_Ref : Node_Id;
242 E_T : constant Entity_Id := Equivalent_Type (Btyp);
243 Acc : constant Entity_Id :=
244 Etype (Next_Component (First_Component (E_T)));
245 Obj_Ref : Node_Id;
246 Curr : Entity_Id;
248 function May_Be_External_Call return Boolean;
249 -- If the 'Access is to a local operation, but appears in a context
250 -- where it may lead to a call from outside the object, we must treat
251 -- this as an external call. Clearly we cannot tell without full
252 -- flow analysis, and a subsequent call that uses this 'Access may
253 -- lead to a bounded error (trying to seize locks twice, e.g.). For
254 -- now we treat 'Access as a potential external call if it is an actual
255 -- in a call to an outside subprogram.
257 --------------------------
258 -- May_Be_External_Call --
259 --------------------------
261 function May_Be_External_Call return Boolean is
262 Subp : Entity_Id;
263 Par : Node_Id := Parent (N);
265 begin
266 -- Account for the case where the Access attribute is part of a
267 -- named parameter association.
269 if Nkind (Par) = N_Parameter_Association then
270 Par := Parent (Par);
271 end if;
273 if Nkind_In (Par, N_Procedure_Call_Statement, N_Function_Call)
274 and then Is_Entity_Name (Name (Par))
275 then
276 Subp := Entity (Name (Par));
277 return not In_Open_Scopes (Scope (Subp));
278 else
279 return False;
280 end if;
281 end May_Be_External_Call;
283 -- Start of processing for Expand_Access_To_Protected_Op
285 begin
286 -- Within the body of the protected type, the prefix designates a local
287 -- operation, and the object is the first parameter of the corresponding
288 -- protected body of the current enclosing operation.
290 if Is_Entity_Name (Pref) then
291 if May_Be_External_Call then
292 Sub :=
293 New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc);
294 else
295 Sub :=
296 New_Occurrence_Of
297 (Protected_Body_Subprogram (Entity (Pref)), Loc);
298 end if;
300 -- Don't traverse the scopes when the attribute occurs within an init
301 -- proc, because we directly use the _init formal of the init proc in
302 -- that case.
304 Curr := Current_Scope;
305 if not Is_Init_Proc (Curr) then
306 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
308 while Scope (Curr) /= Scope (Entity (Pref)) loop
309 Curr := Scope (Curr);
310 end loop;
311 end if;
313 -- In case of protected entries the first formal of its Protected_
314 -- Body_Subprogram is the address of the object.
316 if Ekind (Curr) = E_Entry then
317 Obj_Ref :=
318 New_Occurrence_Of
319 (First_Formal
320 (Protected_Body_Subprogram (Curr)), Loc);
322 -- If the current scope is an init proc, then use the address of the
323 -- _init formal as the object reference.
325 elsif Is_Init_Proc (Curr) then
326 Obj_Ref :=
327 Make_Attribute_Reference (Loc,
328 Prefix => New_Occurrence_Of (First_Formal (Curr), Loc),
329 Attribute_Name => Name_Address);
331 -- In case of protected subprograms the first formal of its
332 -- Protected_Body_Subprogram is the object and we get its address.
334 else
335 Obj_Ref :=
336 Make_Attribute_Reference (Loc,
337 Prefix =>
338 New_Occurrence_Of
339 (First_Formal
340 (Protected_Body_Subprogram (Curr)), Loc),
341 Attribute_Name => Name_Address);
342 end if;
344 -- Case where the prefix is not an entity name. Find the
345 -- version of the protected operation to be called from
346 -- outside the protected object.
348 else
349 Sub :=
350 New_Occurrence_Of
351 (External_Subprogram
352 (Entity (Selector_Name (Pref))), Loc);
354 Obj_Ref :=
355 Make_Attribute_Reference (Loc,
356 Prefix => Relocate_Node (Prefix (Pref)),
357 Attribute_Name => Name_Address);
358 end if;
360 Sub_Ref :=
361 Make_Attribute_Reference (Loc,
362 Prefix => Sub,
363 Attribute_Name => Name_Access);
365 -- We set the type of the access reference to the already generated
366 -- access_to_subprogram type, and declare the reference analyzed, to
367 -- prevent further expansion when the enclosing aggregate is analyzed.
369 Set_Etype (Sub_Ref, Acc);
370 Set_Analyzed (Sub_Ref);
372 Agg :=
373 Make_Aggregate (Loc,
374 Expressions => New_List (Obj_Ref, Sub_Ref));
376 -- Sub_Ref has been marked as analyzed, but we still need to make sure
377 -- Sub is correctly frozen.
379 Freeze_Before (N, Entity (Sub));
381 Rewrite (N, Agg);
382 Analyze_And_Resolve (N, E_T);
384 -- For subsequent analysis, the node must retain its type. The backend
385 -- will replace it with the equivalent type where needed.
387 Set_Etype (N, Typ);
388 end Expand_Access_To_Protected_Op;
390 --------------------------
391 -- Expand_Fpt_Attribute --
392 --------------------------
394 procedure Expand_Fpt_Attribute
395 (N : Node_Id;
396 Pkg : RE_Id;
397 Nam : Name_Id;
398 Args : List_Id)
400 Loc : constant Source_Ptr := Sloc (N);
401 Typ : constant Entity_Id := Etype (N);
402 Fnm : Node_Id;
404 begin
405 -- The function name is the selected component Attr_xxx.yyy where
406 -- Attr_xxx is the package name, and yyy is the argument Nam.
408 -- Note: it would be more usual to have separate RE entries for each
409 -- of the entities in the Fat packages, but first they have identical
410 -- names (so we would have to have lots of renaming declarations to
411 -- meet the normal RE rule of separate names for all runtime entities),
412 -- and second there would be an awful lot of them!
414 Fnm :=
415 Make_Selected_Component (Loc,
416 Prefix => New_Reference_To (RTE (Pkg), Loc),
417 Selector_Name => Make_Identifier (Loc, Nam));
419 -- The generated call is given the provided set of parameters, and then
420 -- wrapped in a conversion which converts the result to the target type
421 -- We use the base type as the target because a range check may be
422 -- required.
424 Rewrite (N,
425 Unchecked_Convert_To (Base_Type (Etype (N)),
426 Make_Function_Call (Loc,
427 Name => Fnm,
428 Parameter_Associations => Args)));
430 Analyze_And_Resolve (N, Typ);
431 end Expand_Fpt_Attribute;
433 ----------------------------
434 -- Expand_Fpt_Attribute_R --
435 ----------------------------
437 -- The single argument is converted to its root type to call the
438 -- appropriate runtime function, with the actual call being built
439 -- by Expand_Fpt_Attribute
441 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
442 E1 : constant Node_Id := First (Expressions (N));
443 Ftp : Entity_Id;
444 Pkg : RE_Id;
445 begin
446 Find_Fat_Info (Etype (E1), Ftp, Pkg);
447 Expand_Fpt_Attribute
448 (N, Pkg, Attribute_Name (N),
449 New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1))));
450 end Expand_Fpt_Attribute_R;
452 -----------------------------
453 -- Expand_Fpt_Attribute_RI --
454 -----------------------------
456 -- The first argument is converted to its root type and the second
457 -- argument is converted to standard long long integer to call the
458 -- appropriate runtime function, with the actual call being built
459 -- by Expand_Fpt_Attribute
461 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
462 E1 : constant Node_Id := First (Expressions (N));
463 Ftp : Entity_Id;
464 Pkg : RE_Id;
465 E2 : constant Node_Id := Next (E1);
466 begin
467 Find_Fat_Info (Etype (E1), Ftp, Pkg);
468 Expand_Fpt_Attribute
469 (N, Pkg, Attribute_Name (N),
470 New_List (
471 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
472 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
473 end Expand_Fpt_Attribute_RI;
475 -----------------------------
476 -- Expand_Fpt_Attribute_RR --
477 -----------------------------
479 -- The two arguments are converted to their root types to call the
480 -- appropriate runtime function, with the actual call being built
481 -- by Expand_Fpt_Attribute
483 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
484 E1 : constant Node_Id := First (Expressions (N));
485 Ftp : Entity_Id;
486 Pkg : RE_Id;
487 E2 : constant Node_Id := Next (E1);
488 begin
489 Find_Fat_Info (Etype (E1), Ftp, Pkg);
490 Expand_Fpt_Attribute
491 (N, Pkg, Attribute_Name (N),
492 New_List (
493 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
494 Unchecked_Convert_To (Ftp, Relocate_Node (E2))));
495 end Expand_Fpt_Attribute_RR;
497 ----------------------------------
498 -- Expand_N_Attribute_Reference --
499 ----------------------------------
501 procedure Expand_N_Attribute_Reference (N : Node_Id) is
502 Loc : constant Source_Ptr := Sloc (N);
503 Typ : constant Entity_Id := Etype (N);
504 Btyp : constant Entity_Id := Base_Type (Typ);
505 Pref : constant Node_Id := Prefix (N);
506 Ptyp : constant Entity_Id := Etype (Pref);
507 Exprs : constant List_Id := Expressions (N);
508 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
510 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
511 -- Rewrites a stream attribute for Read, Write or Output with the
512 -- procedure call. Pname is the entity for the procedure to call.
514 ------------------------------
515 -- Rewrite_Stream_Proc_Call --
516 ------------------------------
518 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
519 Item : constant Node_Id := Next (First (Exprs));
520 Formal : constant Entity_Id := Next_Formal (First_Formal (Pname));
521 Formal_Typ : constant Entity_Id := Etype (Formal);
522 Is_Written : constant Boolean := (Ekind (Formal) /= E_In_Parameter);
524 begin
525 -- The expansion depends on Item, the second actual, which is
526 -- the object being streamed in or out.
528 -- If the item is a component of a packed array type, and
529 -- a conversion is needed on exit, we introduce a temporary to
530 -- hold the value, because otherwise the packed reference will
531 -- not be properly expanded.
533 if Nkind (Item) = N_Indexed_Component
534 and then Is_Packed (Base_Type (Etype (Prefix (Item))))
535 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
536 and then Is_Written
537 then
538 declare
539 Temp : constant Entity_Id := Make_Temporary (Loc, 'V');
540 Decl : Node_Id;
541 Assn : Node_Id;
543 begin
544 Decl :=
545 Make_Object_Declaration (Loc,
546 Defining_Identifier => Temp,
547 Object_Definition =>
548 New_Occurrence_Of (Formal_Typ, Loc));
549 Set_Etype (Temp, Formal_Typ);
551 Assn :=
552 Make_Assignment_Statement (Loc,
553 Name => New_Copy_Tree (Item),
554 Expression =>
555 Unchecked_Convert_To
556 (Etype (Item), New_Occurrence_Of (Temp, Loc)));
558 Rewrite (Item, New_Occurrence_Of (Temp, Loc));
559 Insert_Actions (N,
560 New_List (
561 Decl,
562 Make_Procedure_Call_Statement (Loc,
563 Name => New_Occurrence_Of (Pname, Loc),
564 Parameter_Associations => Exprs),
565 Assn));
567 Rewrite (N, Make_Null_Statement (Loc));
568 return;
569 end;
570 end if;
572 -- For the class-wide dispatching cases, and for cases in which
573 -- the base type of the second argument matches the base type of
574 -- the corresponding formal parameter (that is to say the stream
575 -- operation is not inherited), we are all set, and can use the
576 -- argument unchanged.
578 -- For all other cases we do an unchecked conversion of the second
579 -- parameter to the type of the formal of the procedure we are
580 -- calling. This deals with the private type cases, and with going
581 -- to the root type as required in elementary type case.
583 if not Is_Class_Wide_Type (Entity (Pref))
584 and then not Is_Class_Wide_Type (Etype (Item))
585 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
586 then
587 Rewrite (Item,
588 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
590 -- For untagged derived types set Assignment_OK, to prevent
591 -- copies from being created when the unchecked conversion
592 -- is expanded (which would happen in Remove_Side_Effects
593 -- if Expand_N_Unchecked_Conversion were allowed to call
594 -- Force_Evaluation). The copy could violate Ada semantics
595 -- in cases such as an actual that is an out parameter.
596 -- Note that this approach is also used in exp_ch7 for calls
597 -- to controlled type operations to prevent problems with
598 -- actuals wrapped in unchecked conversions.
600 if Is_Untagged_Derivation (Etype (Expression (Item))) then
601 Set_Assignment_OK (Item);
602 end if;
603 end if;
605 -- The stream operation to call maybe a renaming created by
606 -- an attribute definition clause, and may not be frozen yet.
607 -- Ensure that it has the necessary extra formals.
609 if not Is_Frozen (Pname) then
610 Create_Extra_Formals (Pname);
611 end if;
613 -- And now rewrite the call
615 Rewrite (N,
616 Make_Procedure_Call_Statement (Loc,
617 Name => New_Occurrence_Of (Pname, Loc),
618 Parameter_Associations => Exprs));
620 Analyze (N);
621 end Rewrite_Stream_Proc_Call;
623 -- Start of processing for Expand_N_Attribute_Reference
625 begin
626 -- Do required validity checking, if enabled. Do not apply check to
627 -- output parameters of an Asm instruction, since the value of this
628 -- is not set till after the attribute has been elaborated, and do
629 -- not apply the check to the arguments of a 'Read or 'Input attribute
630 -- reference since the scalar argument is an OUT scalar.
632 if Validity_Checks_On and then Validity_Check_Operands
633 and then Id /= Attribute_Asm_Output
634 and then Id /= Attribute_Read
635 and then Id /= Attribute_Input
636 then
637 declare
638 Expr : Node_Id;
639 begin
640 Expr := First (Expressions (N));
641 while Present (Expr) loop
642 Ensure_Valid (Expr);
643 Next (Expr);
644 end loop;
645 end;
646 end if;
648 -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in-
649 -- place function, then a temporary return object needs to be created
650 -- and access to it must be passed to the function. Currently we limit
651 -- such functions to those with inherently limited result subtypes, but
652 -- eventually we plan to expand the functions that are treated as
653 -- build-in-place to include other composite result types.
655 if Ada_Version >= Ada_2005
656 and then Is_Build_In_Place_Function_Call (Pref)
657 then
658 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
659 end if;
661 -- If prefix is a protected type name, this is a reference to the
662 -- current instance of the type. For a component definition, nothing
663 -- to do (expansion will occur in the init proc). In other contexts,
664 -- rewrite into reference to current instance.
666 if Is_Protected_Self_Reference (Pref)
667 and then not
668 (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint,
669 N_Discriminant_Association)
670 and then Nkind (Parent (Parent (Parent (Parent (N))))) =
671 N_Component_Definition)
672 then
673 Rewrite (Pref, Concurrent_Ref (Pref));
674 Analyze (Pref);
675 end if;
677 -- Remaining processing depends on specific attribute
679 case Id is
681 -- Attributes related to Ada 2012 iterators (placeholder ???)
683 when Attribute_Constant_Indexing => null;
684 when Attribute_Default_Iterator => null;
685 when Attribute_Implicit_Dereference => null;
686 when Attribute_Iterator_Element => null;
687 when Attribute_Variable_Indexing => null;
689 ------------
690 -- Access --
691 ------------
693 when Attribute_Access |
694 Attribute_Unchecked_Access |
695 Attribute_Unrestricted_Access =>
697 Access_Cases : declare
698 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
699 Btyp_DDT : Entity_Id;
701 function Enclosing_Object (N : Node_Id) return Node_Id;
702 -- If N denotes a compound name (selected component, indexed
703 -- component, or slice), returns the name of the outermost such
704 -- enclosing object. Otherwise returns N. If the object is a
705 -- renaming, then the renamed object is returned.
707 ----------------------
708 -- Enclosing_Object --
709 ----------------------
711 function Enclosing_Object (N : Node_Id) return Node_Id is
712 Obj_Name : Node_Id;
714 begin
715 Obj_Name := N;
716 while Nkind_In (Obj_Name, N_Selected_Component,
717 N_Indexed_Component,
718 N_Slice)
719 loop
720 Obj_Name := Prefix (Obj_Name);
721 end loop;
723 return Get_Referenced_Object (Obj_Name);
724 end Enclosing_Object;
726 -- Local declarations
728 Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object);
730 -- Start of processing for Access_Cases
732 begin
733 Btyp_DDT := Designated_Type (Btyp);
735 -- Handle designated types that come from the limited view
737 if Ekind (Btyp_DDT) = E_Incomplete_Type
738 and then From_With_Type (Btyp_DDT)
739 and then Present (Non_Limited_View (Btyp_DDT))
740 then
741 Btyp_DDT := Non_Limited_View (Btyp_DDT);
743 elsif Is_Class_Wide_Type (Btyp_DDT)
744 and then Ekind (Etype (Btyp_DDT)) = E_Incomplete_Type
745 and then From_With_Type (Etype (Btyp_DDT))
746 and then Present (Non_Limited_View (Etype (Btyp_DDT)))
747 and then Present (Class_Wide_Type
748 (Non_Limited_View (Etype (Btyp_DDT))))
749 then
750 Btyp_DDT :=
751 Class_Wide_Type (Non_Limited_View (Etype (Btyp_DDT)));
752 end if;
754 -- In order to improve the text of error messages, the designated
755 -- type of access-to-subprogram itypes is set by the semantics as
756 -- the associated subprogram entity (see sem_attr). Now we replace
757 -- such node with the proper E_Subprogram_Type itype.
759 if Id = Attribute_Unrestricted_Access
760 and then Is_Subprogram (Directly_Designated_Type (Typ))
761 then
762 -- The following conditions ensure that this special management
763 -- is done only for "Address!(Prim'Unrestricted_Access)" nodes.
764 -- At this stage other cases in which the designated type is
765 -- still a subprogram (instead of an E_Subprogram_Type) are
766 -- wrong because the semantics must have overridden the type of
767 -- the node with the type imposed by the context.
769 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
770 and then Etype (Parent (N)) = RTE (RE_Prim_Ptr)
771 then
772 Set_Etype (N, RTE (RE_Prim_Ptr));
774 else
775 declare
776 Subp : constant Entity_Id :=
777 Directly_Designated_Type (Typ);
778 Etyp : Entity_Id;
779 Extra : Entity_Id := Empty;
780 New_Formal : Entity_Id;
781 Old_Formal : Entity_Id := First_Formal (Subp);
782 Subp_Typ : Entity_Id;
784 begin
785 Subp_Typ := Create_Itype (E_Subprogram_Type, N);
786 Set_Etype (Subp_Typ, Etype (Subp));
787 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
789 if Present (Old_Formal) then
790 New_Formal := New_Copy (Old_Formal);
791 Set_First_Entity (Subp_Typ, New_Formal);
793 loop
794 Set_Scope (New_Formal, Subp_Typ);
795 Etyp := Etype (New_Formal);
797 -- Handle itypes. There is no need to duplicate
798 -- here the itypes associated with record types
799 -- (i.e the implicit full view of private types).
801 if Is_Itype (Etyp)
802 and then Ekind (Base_Type (Etyp)) /= E_Record_Type
803 then
804 Extra := New_Copy (Etyp);
805 Set_Parent (Extra, New_Formal);
806 Set_Etype (New_Formal, Extra);
807 Set_Scope (Extra, Subp_Typ);
808 end if;
810 Extra := New_Formal;
811 Next_Formal (Old_Formal);
812 exit when No (Old_Formal);
814 Set_Next_Entity (New_Formal,
815 New_Copy (Old_Formal));
816 Next_Entity (New_Formal);
817 end loop;
819 Set_Next_Entity (New_Formal, Empty);
820 Set_Last_Entity (Subp_Typ, Extra);
821 end if;
823 -- Now that the explicit formals have been duplicated,
824 -- any extra formals needed by the subprogram must be
825 -- created.
827 if Present (Extra) then
828 Set_Extra_Formal (Extra, Empty);
829 end if;
831 Create_Extra_Formals (Subp_Typ);
832 Set_Directly_Designated_Type (Typ, Subp_Typ);
833 end;
834 end if;
835 end if;
837 if Is_Access_Protected_Subprogram_Type (Btyp) then
838 Expand_Access_To_Protected_Op (N, Pref, Typ);
840 -- If prefix is a type name, this is a reference to the current
841 -- instance of the type, within its initialization procedure.
843 elsif Is_Entity_Name (Pref)
844 and then Is_Type (Entity (Pref))
845 then
846 declare
847 Par : Node_Id;
848 Formal : Entity_Id;
850 begin
851 -- If the current instance name denotes a task type, then
852 -- the access attribute is rewritten to be the name of the
853 -- "_task" parameter associated with the task type's task
854 -- procedure. An unchecked conversion is applied to ensure
855 -- a type match in cases of expander-generated calls (e.g.
856 -- init procs).
858 if Is_Task_Type (Entity (Pref)) then
859 Formal :=
860 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
861 while Present (Formal) loop
862 exit when Chars (Formal) = Name_uTask;
863 Next_Entity (Formal);
864 end loop;
866 pragma Assert (Present (Formal));
868 Rewrite (N,
869 Unchecked_Convert_To (Typ,
870 New_Occurrence_Of (Formal, Loc)));
871 Set_Etype (N, Typ);
873 -- The expression must appear in a default expression,
874 -- (which in the initialization procedure is the
875 -- right-hand side of an assignment), and not in a
876 -- discriminant constraint.
878 else
879 Par := Parent (N);
880 while Present (Par) loop
881 exit when Nkind (Par) = N_Assignment_Statement;
883 if Nkind (Par) = N_Component_Declaration then
884 return;
885 end if;
887 Par := Parent (Par);
888 end loop;
890 if Present (Par) then
891 Rewrite (N,
892 Make_Attribute_Reference (Loc,
893 Prefix => Make_Identifier (Loc, Name_uInit),
894 Attribute_Name => Attribute_Name (N)));
896 Analyze_And_Resolve (N, Typ);
897 end if;
898 end if;
899 end;
901 -- If the prefix of an Access attribute is a dereference of an
902 -- access parameter (or a renaming of such a dereference, or a
903 -- subcomponent of such a dereference) and the context is a
904 -- general access type (including the type of an object or
905 -- component with an access_definition, but not the anonymous
906 -- type of an access parameter or access discriminant), then
907 -- apply an accessibility check to the access parameter. We used
908 -- to rewrite the access parameter as a type conversion, but that
909 -- could only be done if the immediate prefix of the Access
910 -- attribute was the dereference, and didn't handle cases where
911 -- the attribute is applied to a subcomponent of the dereference,
912 -- since there's generally no available, appropriate access type
913 -- to convert to in that case. The attribute is passed as the
914 -- point to insert the check, because the access parameter may
915 -- come from a renaming, possibly in a different scope, and the
916 -- check must be associated with the attribute itself.
918 elsif Id = Attribute_Access
919 and then Nkind (Enc_Object) = N_Explicit_Dereference
920 and then Is_Entity_Name (Prefix (Enc_Object))
921 and then (Ekind (Btyp) = E_General_Access_Type
922 or else Is_Local_Anonymous_Access (Btyp))
923 and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind
924 and then Ekind (Etype (Entity (Prefix (Enc_Object))))
925 = E_Anonymous_Access_Type
926 and then Present (Extra_Accessibility
927 (Entity (Prefix (Enc_Object))))
928 then
929 Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N);
931 -- Ada 2005 (AI-251): If the designated type is an interface we
932 -- add an implicit conversion to force the displacement of the
933 -- pointer to reference the secondary dispatch table.
935 elsif Is_Interface (Btyp_DDT)
936 and then (Comes_From_Source (N)
937 or else Comes_From_Source (Ref_Object)
938 or else (Nkind (Ref_Object) in N_Has_Chars
939 and then Chars (Ref_Object) = Name_uInit))
940 then
941 if Nkind (Ref_Object) /= N_Explicit_Dereference then
943 -- No implicit conversion required if types match, or if
944 -- the prefix is the class_wide_type of the interface. In
945 -- either case passing an object of the interface type has
946 -- already set the pointer correctly.
948 if Btyp_DDT = Etype (Ref_Object)
949 or else (Is_Class_Wide_Type (Etype (Ref_Object))
950 and then
951 Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object))
952 then
953 null;
955 else
956 Rewrite (Prefix (N),
957 Convert_To (Btyp_DDT,
958 New_Copy_Tree (Prefix (N))));
960 Analyze_And_Resolve (Prefix (N), Btyp_DDT);
961 end if;
963 -- When the object is an explicit dereference, convert the
964 -- dereference's prefix.
966 else
967 declare
968 Obj_DDT : constant Entity_Id :=
969 Base_Type
970 (Directly_Designated_Type
971 (Etype (Prefix (Ref_Object))));
972 begin
973 -- No implicit conversion required if designated types
974 -- match, or if we have an unrestricted access.
976 if Obj_DDT /= Btyp_DDT
977 and then Id /= Attribute_Unrestricted_Access
978 and then not (Is_Class_Wide_Type (Obj_DDT)
979 and then Etype (Obj_DDT) = Btyp_DDT)
980 then
981 Rewrite (N,
982 Convert_To (Typ,
983 New_Copy_Tree (Prefix (Ref_Object))));
984 Analyze_And_Resolve (N, Typ);
985 end if;
986 end;
987 end if;
988 end if;
989 end Access_Cases;
991 --------------
992 -- Adjacent --
993 --------------
995 -- Transforms 'Adjacent into a call to the floating-point attribute
996 -- function Adjacent in Fat_xxx (where xxx is the root type)
998 when Attribute_Adjacent =>
999 Expand_Fpt_Attribute_RR (N);
1001 -------------
1002 -- Address --
1003 -------------
1005 when Attribute_Address => Address : declare
1006 Task_Proc : Entity_Id;
1008 begin
1009 -- If the prefix is a task or a task type, the useful address is that
1010 -- of the procedure for the task body, i.e. the actual program unit.
1011 -- We replace the original entity with that of the procedure.
1013 if Is_Entity_Name (Pref)
1014 and then Is_Task_Type (Entity (Pref))
1015 then
1016 Task_Proc := Next_Entity (Root_Type (Ptyp));
1018 while Present (Task_Proc) loop
1019 exit when Ekind (Task_Proc) = E_Procedure
1020 and then Etype (First_Formal (Task_Proc)) =
1021 Corresponding_Record_Type (Ptyp);
1022 Next_Entity (Task_Proc);
1023 end loop;
1025 if Present (Task_Proc) then
1026 Set_Entity (Pref, Task_Proc);
1027 Set_Etype (Pref, Etype (Task_Proc));
1028 end if;
1030 -- Similarly, the address of a protected operation is the address
1031 -- of the corresponding protected body, regardless of the protected
1032 -- object from which it is selected.
1034 elsif Nkind (Pref) = N_Selected_Component
1035 and then Is_Subprogram (Entity (Selector_Name (Pref)))
1036 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
1037 then
1038 Rewrite (Pref,
1039 New_Occurrence_Of (
1040 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
1042 elsif Nkind (Pref) = N_Explicit_Dereference
1043 and then Ekind (Ptyp) = E_Subprogram_Type
1044 and then Convention (Ptyp) = Convention_Protected
1045 then
1046 -- The prefix is be a dereference of an access_to_protected_
1047 -- subprogram. The desired address is the second component of
1048 -- the record that represents the access.
1050 declare
1051 Addr : constant Entity_Id := Etype (N);
1052 Ptr : constant Node_Id := Prefix (Pref);
1053 T : constant Entity_Id :=
1054 Equivalent_Type (Base_Type (Etype (Ptr)));
1056 begin
1057 Rewrite (N,
1058 Unchecked_Convert_To (Addr,
1059 Make_Selected_Component (Loc,
1060 Prefix => Unchecked_Convert_To (T, Ptr),
1061 Selector_Name => New_Occurrence_Of (
1062 Next_Entity (First_Entity (T)), Loc))));
1064 Analyze_And_Resolve (N, Addr);
1065 end;
1067 -- Ada 2005 (AI-251): Class-wide interface objects are always
1068 -- "displaced" to reference the tag associated with the interface
1069 -- type. In order to obtain the real address of such objects we
1070 -- generate a call to a run-time subprogram that returns the base
1071 -- address of the object.
1073 -- This processing is not needed in the VM case, where dispatching
1074 -- issues are taken care of by the virtual machine.
1076 elsif Is_Class_Wide_Type (Ptyp)
1077 and then Is_Interface (Ptyp)
1078 and then Tagged_Type_Expansion
1079 and then not (Nkind (Pref) in N_Has_Entity
1080 and then Is_Subprogram (Entity (Pref)))
1081 then
1082 Rewrite (N,
1083 Make_Function_Call (Loc,
1084 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
1085 Parameter_Associations => New_List (
1086 Relocate_Node (N))));
1087 Analyze (N);
1088 return;
1089 end if;
1091 -- Deal with packed array reference, other cases are handled by
1092 -- the back end.
1094 if Involves_Packed_Array_Reference (Pref) then
1095 Expand_Packed_Address_Reference (N);
1096 end if;
1097 end Address;
1099 ---------------
1100 -- Alignment --
1101 ---------------
1103 when Attribute_Alignment => Alignment : declare
1104 New_Node : Node_Id;
1106 begin
1107 -- For class-wide types, X'Class'Alignment is transformed into a
1108 -- direct reference to the Alignment of the class type, so that the
1109 -- back end does not have to deal with the X'Class'Alignment
1110 -- reference.
1112 if Is_Entity_Name (Pref)
1113 and then Is_Class_Wide_Type (Entity (Pref))
1114 then
1115 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
1116 return;
1118 -- For x'Alignment applied to an object of a class wide type,
1119 -- transform X'Alignment into a call to the predefined primitive
1120 -- operation _Alignment applied to X.
1122 elsif Is_Class_Wide_Type (Ptyp) then
1123 New_Node :=
1124 Make_Attribute_Reference (Loc,
1125 Prefix => Pref,
1126 Attribute_Name => Name_Tag);
1128 if VM_Target = No_VM then
1129 New_Node := Build_Get_Alignment (Loc, New_Node);
1130 else
1131 New_Node :=
1132 Make_Function_Call (Loc,
1133 Name => New_Reference_To (RTE (RE_Get_Alignment), Loc),
1134 Parameter_Associations => New_List (New_Node));
1135 end if;
1137 -- Case where the context is a specific integer type with which
1138 -- the original attribute was compatible. The function has a
1139 -- specific type as well, so to preserve the compatibility we
1140 -- must convert explicitly.
1142 if Typ /= Standard_Integer then
1143 New_Node := Convert_To (Typ, New_Node);
1144 end if;
1146 Rewrite (N, New_Node);
1147 Analyze_And_Resolve (N, Typ);
1148 return;
1150 -- For all other cases, we just have to deal with the case of
1151 -- the fact that the result can be universal.
1153 else
1154 Apply_Universal_Integer_Attribute_Checks (N);
1155 end if;
1156 end Alignment;
1158 ---------------
1159 -- AST_Entry --
1160 ---------------
1162 when Attribute_AST_Entry => AST_Entry : declare
1163 Ttyp : Entity_Id;
1164 T_Id : Node_Id;
1165 Eent : Entity_Id;
1167 Entry_Ref : Node_Id;
1168 -- The reference to the entry or entry family
1170 Index : Node_Id;
1171 -- The index expression for an entry family reference, or
1172 -- the Empty if Entry_Ref references a simple entry.
1174 begin
1175 if Nkind (Pref) = N_Indexed_Component then
1176 Entry_Ref := Prefix (Pref);
1177 Index := First (Expressions (Pref));
1178 else
1179 Entry_Ref := Pref;
1180 Index := Empty;
1181 end if;
1183 -- Get expression for Task_Id and the entry entity
1185 if Nkind (Entry_Ref) = N_Selected_Component then
1186 T_Id :=
1187 Make_Attribute_Reference (Loc,
1188 Attribute_Name => Name_Identity,
1189 Prefix => Prefix (Entry_Ref));
1191 Ttyp := Etype (Prefix (Entry_Ref));
1192 Eent := Entity (Selector_Name (Entry_Ref));
1194 else
1195 T_Id :=
1196 Make_Function_Call (Loc,
1197 Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc));
1199 Eent := Entity (Entry_Ref);
1201 -- We have to find the enclosing task to get the task type
1202 -- There must be one, since we already validated this earlier
1204 Ttyp := Current_Scope;
1205 while not Is_Task_Type (Ttyp) loop
1206 Ttyp := Scope (Ttyp);
1207 end loop;
1208 end if;
1210 -- Now rewrite the attribute with a call to Create_AST_Handler
1212 Rewrite (N,
1213 Make_Function_Call (Loc,
1214 Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc),
1215 Parameter_Associations => New_List (
1216 T_Id,
1217 Entry_Index_Expression (Loc, Eent, Index, Ttyp))));
1219 Analyze_And_Resolve (N, RTE (RE_AST_Handler));
1220 end AST_Entry;
1222 ---------
1223 -- Bit --
1224 ---------
1226 -- We compute this if a packed array reference was present, otherwise we
1227 -- leave the computation up to the back end.
1229 when Attribute_Bit =>
1230 if Involves_Packed_Array_Reference (Pref) then
1231 Expand_Packed_Bit_Reference (N);
1232 else
1233 Apply_Universal_Integer_Attribute_Checks (N);
1234 end if;
1236 ------------------
1237 -- Bit_Position --
1238 ------------------
1240 -- We compute this if a component clause was present, otherwise we leave
1241 -- the computation up to the back end, since we don't know what layout
1242 -- will be chosen.
1244 -- Note that the attribute can apply to a naked record component
1245 -- in generated code (i.e. the prefix is an identifier that
1246 -- references the component or discriminant entity).
1248 when Attribute_Bit_Position => Bit_Position : declare
1249 CE : Entity_Id;
1251 begin
1252 if Nkind (Pref) = N_Identifier then
1253 CE := Entity (Pref);
1254 else
1255 CE := Entity (Selector_Name (Pref));
1256 end if;
1258 if Known_Static_Component_Bit_Offset (CE) then
1259 Rewrite (N,
1260 Make_Integer_Literal (Loc,
1261 Intval => Component_Bit_Offset (CE)));
1262 Analyze_And_Resolve (N, Typ);
1264 else
1265 Apply_Universal_Integer_Attribute_Checks (N);
1266 end if;
1267 end Bit_Position;
1269 ------------------
1270 -- Body_Version --
1271 ------------------
1273 -- A reference to P'Body_Version or P'Version is expanded to
1275 -- Vnn : Unsigned;
1276 -- pragma Import (C, Vnn, "uuuuT");
1277 -- ...
1278 -- Get_Version_String (Vnn)
1280 -- where uuuu is the unit name (dots replaced by double underscore)
1281 -- and T is B for the cases of Body_Version, or Version applied to a
1282 -- subprogram acting as its own spec, and S for Version applied to a
1283 -- subprogram spec or package. This sequence of code references the
1284 -- unsigned constant created in the main program by the binder.
1286 -- A special exception occurs for Standard, where the string returned
1287 -- is a copy of the library string in gnatvsn.ads.
1289 when Attribute_Body_Version | Attribute_Version => Version : declare
1290 E : constant Entity_Id := Make_Temporary (Loc, 'V');
1291 Pent : Entity_Id;
1292 S : String_Id;
1294 begin
1295 -- If not library unit, get to containing library unit
1297 Pent := Entity (Pref);
1298 while Pent /= Standard_Standard
1299 and then Scope (Pent) /= Standard_Standard
1300 and then not Is_Child_Unit (Pent)
1301 loop
1302 Pent := Scope (Pent);
1303 end loop;
1305 -- Special case Standard and Standard.ASCII
1307 if Pent = Standard_Standard or else Pent = Standard_ASCII then
1308 Rewrite (N,
1309 Make_String_Literal (Loc,
1310 Strval => Verbose_Library_Version));
1312 -- All other cases
1314 else
1315 -- Build required string constant
1317 Get_Name_String (Get_Unit_Name (Pent));
1319 Start_String;
1320 for J in 1 .. Name_Len - 2 loop
1321 if Name_Buffer (J) = '.' then
1322 Store_String_Chars ("__");
1323 else
1324 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
1325 end if;
1326 end loop;
1328 -- Case of subprogram acting as its own spec, always use body
1330 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
1331 and then Nkind (Parent (Declaration_Node (Pent))) =
1332 N_Subprogram_Body
1333 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
1334 then
1335 Store_String_Chars ("B");
1337 -- Case of no body present, always use spec
1339 elsif not Unit_Requires_Body (Pent) then
1340 Store_String_Chars ("S");
1342 -- Otherwise use B for Body_Version, S for spec
1344 elsif Id = Attribute_Body_Version then
1345 Store_String_Chars ("B");
1346 else
1347 Store_String_Chars ("S");
1348 end if;
1350 S := End_String;
1351 Lib.Version_Referenced (S);
1353 -- Insert the object declaration
1355 Insert_Actions (N, New_List (
1356 Make_Object_Declaration (Loc,
1357 Defining_Identifier => E,
1358 Object_Definition =>
1359 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
1361 -- Set entity as imported with correct external name
1363 Set_Is_Imported (E);
1364 Set_Interface_Name (E, Make_String_Literal (Loc, S));
1366 -- Set entity as internal to ensure proper Sprint output of its
1367 -- implicit importation.
1369 Set_Is_Internal (E);
1371 -- And now rewrite original reference
1373 Rewrite (N,
1374 Make_Function_Call (Loc,
1375 Name => New_Reference_To (RTE (RE_Get_Version_String), Loc),
1376 Parameter_Associations => New_List (
1377 New_Occurrence_Of (E, Loc))));
1378 end if;
1380 Analyze_And_Resolve (N, RTE (RE_Version_String));
1381 end Version;
1383 -------------
1384 -- Ceiling --
1385 -------------
1387 -- Transforms 'Ceiling into a call to the floating-point attribute
1388 -- function Ceiling in Fat_xxx (where xxx is the root type)
1390 when Attribute_Ceiling =>
1391 Expand_Fpt_Attribute_R (N);
1393 --------------
1394 -- Callable --
1395 --------------
1397 -- Transforms 'Callable attribute into a call to the Callable function
1399 when Attribute_Callable => Callable :
1400 begin
1401 -- We have an object of a task interface class-wide type as a prefix
1402 -- to Callable. Generate:
1403 -- callable (Task_Id (Pref._disp_get_task_id));
1405 if Ada_Version >= Ada_2005
1406 and then Ekind (Ptyp) = E_Class_Wide_Type
1407 and then Is_Interface (Ptyp)
1408 and then Is_Task_Interface (Ptyp)
1409 then
1410 Rewrite (N,
1411 Make_Function_Call (Loc,
1412 Name =>
1413 New_Reference_To (RTE (RE_Callable), Loc),
1414 Parameter_Associations => New_List (
1415 Make_Unchecked_Type_Conversion (Loc,
1416 Subtype_Mark =>
1417 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
1418 Expression =>
1419 Make_Selected_Component (Loc,
1420 Prefix =>
1421 New_Copy_Tree (Pref),
1422 Selector_Name =>
1423 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
1425 else
1426 Rewrite (N,
1427 Build_Call_With_Task (Pref, RTE (RE_Callable)));
1428 end if;
1430 Analyze_And_Resolve (N, Standard_Boolean);
1431 end Callable;
1433 ------------
1434 -- Caller --
1435 ------------
1437 -- Transforms 'Caller attribute into a call to either the
1438 -- Task_Entry_Caller or the Protected_Entry_Caller function.
1440 when Attribute_Caller => Caller : declare
1441 Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id);
1442 Ent : constant Entity_Id := Entity (Pref);
1443 Conctype : constant Entity_Id := Scope (Ent);
1444 Nest_Depth : Integer := 0;
1445 Name : Node_Id;
1446 S : Entity_Id;
1448 begin
1449 -- Protected case
1451 if Is_Protected_Type (Conctype) then
1452 case Corresponding_Runtime_Package (Conctype) is
1453 when System_Tasking_Protected_Objects_Entries =>
1454 Name :=
1455 New_Reference_To
1456 (RTE (RE_Protected_Entry_Caller), Loc);
1458 when System_Tasking_Protected_Objects_Single_Entry =>
1459 Name :=
1460 New_Reference_To
1461 (RTE (RE_Protected_Single_Entry_Caller), Loc);
1463 when others =>
1464 raise Program_Error;
1465 end case;
1467 Rewrite (N,
1468 Unchecked_Convert_To (Id_Kind,
1469 Make_Function_Call (Loc,
1470 Name => Name,
1471 Parameter_Associations => New_List (
1472 New_Reference_To
1473 (Find_Protection_Object (Current_Scope), Loc)))));
1475 -- Task case
1477 else
1478 -- Determine the nesting depth of the E'Caller attribute, that
1479 -- is, how many accept statements are nested within the accept
1480 -- statement for E at the point of E'Caller. The runtime uses
1481 -- this depth to find the specified entry call.
1483 for J in reverse 0 .. Scope_Stack.Last loop
1484 S := Scope_Stack.Table (J).Entity;
1486 -- We should not reach the scope of the entry, as it should
1487 -- already have been checked in Sem_Attr that this attribute
1488 -- reference is within a matching accept statement.
1490 pragma Assert (S /= Conctype);
1492 if S = Ent then
1493 exit;
1495 elsif Is_Entry (S) then
1496 Nest_Depth := Nest_Depth + 1;
1497 end if;
1498 end loop;
1500 Rewrite (N,
1501 Unchecked_Convert_To (Id_Kind,
1502 Make_Function_Call (Loc,
1503 Name =>
1504 New_Reference_To (RTE (RE_Task_Entry_Caller), Loc),
1505 Parameter_Associations => New_List (
1506 Make_Integer_Literal (Loc,
1507 Intval => Int (Nest_Depth))))));
1508 end if;
1510 Analyze_And_Resolve (N, Id_Kind);
1511 end Caller;
1513 -------------
1514 -- Compose --
1515 -------------
1517 -- Transforms 'Compose into a call to the floating-point attribute
1518 -- function Compose in Fat_xxx (where xxx is the root type)
1520 -- Note: we strictly should have special code here to deal with the
1521 -- case of absurdly negative arguments (less than Integer'First)
1522 -- which will return a (signed) zero value, but it hardly seems
1523 -- worth the effort. Absurdly large positive arguments will raise
1524 -- constraint error which is fine.
1526 when Attribute_Compose =>
1527 Expand_Fpt_Attribute_RI (N);
1529 -----------------
1530 -- Constrained --
1531 -----------------
1533 when Attribute_Constrained => Constrained : declare
1534 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
1536 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
1537 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
1538 -- view of an aliased object whose subtype is constrained.
1540 ---------------------------------
1541 -- Is_Constrained_Aliased_View --
1542 ---------------------------------
1544 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
1545 E : Entity_Id;
1547 begin
1548 if Is_Entity_Name (Obj) then
1549 E := Entity (Obj);
1551 if Present (Renamed_Object (E)) then
1552 return Is_Constrained_Aliased_View (Renamed_Object (E));
1553 else
1554 return Is_Aliased (E) and then Is_Constrained (Etype (E));
1555 end if;
1557 else
1558 return Is_Aliased_View (Obj)
1559 and then
1560 (Is_Constrained (Etype (Obj))
1561 or else
1562 (Nkind (Obj) = N_Explicit_Dereference
1563 and then
1564 not Effectively_Has_Constrained_Partial_View
1565 (Typ => Base_Type (Etype (Obj)),
1566 Scop => Current_Scope)));
1567 end if;
1568 end Is_Constrained_Aliased_View;
1570 -- Start of processing for Constrained
1572 begin
1573 -- Reference to a parameter where the value is passed as an extra
1574 -- actual, corresponding to the extra formal referenced by the
1575 -- Extra_Constrained field of the corresponding formal. If this
1576 -- is an entry in-parameter, it is replaced by a constant renaming
1577 -- for which Extra_Constrained is never created.
1579 if Present (Formal_Ent)
1580 and then Ekind (Formal_Ent) /= E_Constant
1581 and then Present (Extra_Constrained (Formal_Ent))
1582 then
1583 Rewrite (N,
1584 New_Occurrence_Of
1585 (Extra_Constrained (Formal_Ent), Sloc (N)));
1587 -- For variables with a Extra_Constrained field, we use the
1588 -- corresponding entity.
1590 elsif Nkind (Pref) = N_Identifier
1591 and then Ekind (Entity (Pref)) = E_Variable
1592 and then Present (Extra_Constrained (Entity (Pref)))
1593 then
1594 Rewrite (N,
1595 New_Occurrence_Of
1596 (Extra_Constrained (Entity (Pref)), Sloc (N)));
1598 -- For all other entity names, we can tell at compile time
1600 elsif Is_Entity_Name (Pref) then
1601 declare
1602 Ent : constant Entity_Id := Entity (Pref);
1603 Res : Boolean;
1605 begin
1606 -- (RM J.4) obsolescent cases
1608 if Is_Type (Ent) then
1610 -- Private type
1612 if Is_Private_Type (Ent) then
1613 Res := not Has_Discriminants (Ent)
1614 or else Is_Constrained (Ent);
1616 -- It not a private type, must be a generic actual type
1617 -- that corresponded to a private type. We know that this
1618 -- correspondence holds, since otherwise the reference
1619 -- within the generic template would have been illegal.
1621 else
1622 if Is_Composite_Type (Underlying_Type (Ent)) then
1623 Res := Is_Constrained (Ent);
1624 else
1625 Res := True;
1626 end if;
1627 end if;
1629 -- If the prefix is not a variable or is aliased, then
1630 -- definitely true; if it's a formal parameter without an
1631 -- associated extra formal, then treat it as constrained.
1633 -- Ada 2005 (AI-363): An aliased prefix must be known to be
1634 -- constrained in order to set the attribute to True.
1636 elsif not Is_Variable (Pref)
1637 or else Present (Formal_Ent)
1638 or else (Ada_Version < Ada_2005
1639 and then Is_Aliased_View (Pref))
1640 or else (Ada_Version >= Ada_2005
1641 and then Is_Constrained_Aliased_View (Pref))
1642 then
1643 Res := True;
1645 -- Variable case, look at type to see if it is constrained.
1646 -- Note that the one case where this is not accurate (the
1647 -- procedure formal case), has been handled above.
1649 -- We use the Underlying_Type here (and below) in case the
1650 -- type is private without discriminants, but the full type
1651 -- has discriminants. This case is illegal, but we generate it
1652 -- internally for passing to the Extra_Constrained parameter.
1654 else
1655 -- In Ada 2012, test for case of a limited tagged type, in
1656 -- which case the attribute is always required to return
1657 -- True. The underlying type is tested, to make sure we also
1658 -- return True for cases where there is an unconstrained
1659 -- object with an untagged limited partial view which has
1660 -- defaulted discriminants (such objects always produce a
1661 -- False in earlier versions of Ada). (Ada 2012: AI05-0214)
1663 Res := Is_Constrained (Underlying_Type (Etype (Ent)))
1664 or else
1665 (Ada_Version >= Ada_2012
1666 and then Is_Tagged_Type (Underlying_Type (Ptyp))
1667 and then Is_Limited_Type (Ptyp));
1668 end if;
1670 Rewrite (N, New_Reference_To (Boolean_Literals (Res), Loc));
1671 end;
1673 -- Prefix is not an entity name. These are also cases where we can
1674 -- always tell at compile time by looking at the form and type of the
1675 -- prefix. If an explicit dereference of an object with constrained
1676 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
1677 -- underlying type is a limited tagged type, then Constrained is
1678 -- required to always return True (Ada 2012: AI05-0214).
1680 else
1681 Rewrite (N,
1682 New_Reference_To (
1683 Boolean_Literals (
1684 not Is_Variable (Pref)
1685 or else
1686 (Nkind (Pref) = N_Explicit_Dereference
1687 and then
1688 not Effectively_Has_Constrained_Partial_View
1689 (Typ => Base_Type (Ptyp),
1690 Scop => Current_Scope))
1691 or else Is_Constrained (Underlying_Type (Ptyp))
1692 or else (Ada_Version >= Ada_2012
1693 and then Is_Tagged_Type (Underlying_Type (Ptyp))
1694 and then Is_Limited_Type (Ptyp))),
1695 Loc));
1696 end if;
1698 Analyze_And_Resolve (N, Standard_Boolean);
1699 end Constrained;
1701 ---------------
1702 -- Copy_Sign --
1703 ---------------
1705 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1706 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1708 when Attribute_Copy_Sign =>
1709 Expand_Fpt_Attribute_RR (N);
1711 -----------
1712 -- Count --
1713 -----------
1715 -- Transforms 'Count attribute into a call to the Count function
1717 when Attribute_Count => Count : declare
1718 Call : Node_Id;
1719 Conctyp : Entity_Id;
1720 Entnam : Node_Id;
1721 Entry_Id : Entity_Id;
1722 Index : Node_Id;
1723 Name : Node_Id;
1725 begin
1726 -- If the prefix is a member of an entry family, retrieve both
1727 -- entry name and index. For a simple entry there is no index.
1729 if Nkind (Pref) = N_Indexed_Component then
1730 Entnam := Prefix (Pref);
1731 Index := First (Expressions (Pref));
1732 else
1733 Entnam := Pref;
1734 Index := Empty;
1735 end if;
1737 Entry_Id := Entity (Entnam);
1739 -- Find the concurrent type in which this attribute is referenced
1740 -- (there had better be one).
1742 Conctyp := Current_Scope;
1743 while not Is_Concurrent_Type (Conctyp) loop
1744 Conctyp := Scope (Conctyp);
1745 end loop;
1747 -- Protected case
1749 if Is_Protected_Type (Conctyp) then
1750 case Corresponding_Runtime_Package (Conctyp) is
1751 when System_Tasking_Protected_Objects_Entries =>
1752 Name := New_Reference_To (RTE (RE_Protected_Count), Loc);
1754 Call :=
1755 Make_Function_Call (Loc,
1756 Name => Name,
1757 Parameter_Associations => New_List (
1758 New_Reference_To
1759 (Find_Protection_Object (Current_Scope), Loc),
1760 Entry_Index_Expression
1761 (Loc, Entry_Id, Index, Scope (Entry_Id))));
1763 when System_Tasking_Protected_Objects_Single_Entry =>
1764 Name :=
1765 New_Reference_To (RTE (RE_Protected_Count_Entry), Loc);
1767 Call :=
1768 Make_Function_Call (Loc,
1769 Name => Name,
1770 Parameter_Associations => New_List (
1771 New_Reference_To
1772 (Find_Protection_Object (Current_Scope), Loc)));
1774 when others =>
1775 raise Program_Error;
1776 end case;
1778 -- Task case
1780 else
1781 Call :=
1782 Make_Function_Call (Loc,
1783 Name => New_Reference_To (RTE (RE_Task_Count), Loc),
1784 Parameter_Associations => New_List (
1785 Entry_Index_Expression (Loc,
1786 Entry_Id, Index, Scope (Entry_Id))));
1787 end if;
1789 -- The call returns type Natural but the context is universal integer
1790 -- so any integer type is allowed. The attribute was already resolved
1791 -- so its Etype is the required result type. If the base type of the
1792 -- context type is other than Standard.Integer we put in a conversion
1793 -- to the required type. This can be a normal typed conversion since
1794 -- both input and output types of the conversion are integer types
1796 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
1797 Rewrite (N, Convert_To (Typ, Call));
1798 else
1799 Rewrite (N, Call);
1800 end if;
1802 Analyze_And_Resolve (N, Typ);
1803 end Count;
1805 ---------------------
1806 -- Descriptor_Size --
1807 ---------------------
1809 when Attribute_Descriptor_Size =>
1811 -- Attribute Descriptor_Size is handled by the back end when applied
1812 -- to an unconstrained array type.
1814 if Is_Array_Type (Ptyp)
1815 and then not Is_Constrained (Ptyp)
1816 then
1817 Apply_Universal_Integer_Attribute_Checks (N);
1819 -- For any other type, the descriptor size is 0 because there is no
1820 -- actual descriptor, but the result is not formally static.
1822 else
1823 Rewrite (N, Make_Integer_Literal (Loc, 0));
1824 Analyze (N);
1825 Set_Is_Static_Expression (N, False);
1826 end if;
1828 ---------------
1829 -- Elab_Body --
1830 ---------------
1832 -- This processing is shared by Elab_Spec
1834 -- What we do is to insert the following declarations
1836 -- procedure tnn;
1837 -- pragma Import (C, enn, "name___elabb/s");
1839 -- and then the Elab_Body/Spec attribute is replaced by a reference
1840 -- to this defining identifier.
1842 when Attribute_Elab_Body |
1843 Attribute_Elab_Spec =>
1845 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
1846 -- back-end knows how to handle these attributes directly.
1848 if CodePeer_Mode then
1849 return;
1850 end if;
1852 Elab_Body : declare
1853 Ent : constant Entity_Id := Make_Temporary (Loc, 'E');
1854 Str : String_Id;
1855 Lang : Node_Id;
1857 procedure Make_Elab_String (Nod : Node_Id);
1858 -- Given Nod, an identifier, or a selected component, put the
1859 -- image into the current string literal, with double underline
1860 -- between components.
1862 ----------------------
1863 -- Make_Elab_String --
1864 ----------------------
1866 procedure Make_Elab_String (Nod : Node_Id) is
1867 begin
1868 if Nkind (Nod) = N_Selected_Component then
1869 Make_Elab_String (Prefix (Nod));
1871 case VM_Target is
1872 when JVM_Target =>
1873 Store_String_Char ('$');
1874 when CLI_Target =>
1875 Store_String_Char ('.');
1876 when No_VM =>
1877 Store_String_Char ('_');
1878 Store_String_Char ('_');
1879 end case;
1881 Get_Name_String (Chars (Selector_Name (Nod)));
1883 else
1884 pragma Assert (Nkind (Nod) = N_Identifier);
1885 Get_Name_String (Chars (Nod));
1886 end if;
1888 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1889 end Make_Elab_String;
1891 -- Start of processing for Elab_Body/Elab_Spec
1893 begin
1894 -- First we need to prepare the string literal for the name of
1895 -- the elaboration routine to be referenced.
1897 Start_String;
1898 Make_Elab_String (Pref);
1900 if VM_Target = No_VM then
1901 Store_String_Chars ("___elab");
1902 Lang := Make_Identifier (Loc, Name_C);
1903 else
1904 Store_String_Chars ("._elab");
1905 Lang := Make_Identifier (Loc, Name_Ada);
1906 end if;
1908 if Id = Attribute_Elab_Body then
1909 Store_String_Char ('b');
1910 else
1911 Store_String_Char ('s');
1912 end if;
1914 Str := End_String;
1916 Insert_Actions (N, New_List (
1917 Make_Subprogram_Declaration (Loc,
1918 Specification =>
1919 Make_Procedure_Specification (Loc,
1920 Defining_Unit_Name => Ent)),
1922 Make_Pragma (Loc,
1923 Chars => Name_Import,
1924 Pragma_Argument_Associations => New_List (
1925 Make_Pragma_Argument_Association (Loc, Expression => Lang),
1927 Make_Pragma_Argument_Association (Loc,
1928 Expression => Make_Identifier (Loc, Chars (Ent))),
1930 Make_Pragma_Argument_Association (Loc,
1931 Expression => Make_String_Literal (Loc, Str))))));
1933 Set_Entity (N, Ent);
1934 Rewrite (N, New_Occurrence_Of (Ent, Loc));
1935 end Elab_Body;
1937 --------------------
1938 -- Elab_Subp_Body --
1939 --------------------
1941 -- Always ignored. In CodePeer mode, gnat2scil knows how to handle
1942 -- this attribute directly, and if we are not in CodePeer mode it is
1943 -- entirely ignored ???
1945 when Attribute_Elab_Subp_Body =>
1946 return;
1948 ----------------
1949 -- Elaborated --
1950 ----------------
1952 -- Elaborated is always True for preelaborated units, predefined units,
1953 -- pure units and units which have Elaborate_Body pragmas. These units
1954 -- have no elaboration entity.
1956 -- Note: The Elaborated attribute is never passed to the back end
1958 when Attribute_Elaborated => Elaborated : declare
1959 Ent : constant Entity_Id := Entity (Pref);
1961 begin
1962 if Present (Elaboration_Entity (Ent)) then
1963 Rewrite (N,
1964 Make_Op_Ne (Loc,
1965 Left_Opnd =>
1966 New_Occurrence_Of (Elaboration_Entity (Ent), Loc),
1967 Right_Opnd =>
1968 Make_Integer_Literal (Loc, Uint_0)));
1969 Analyze_And_Resolve (N, Typ);
1970 else
1971 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
1972 end if;
1973 end Elaborated;
1975 --------------
1976 -- Enum_Rep --
1977 --------------
1979 when Attribute_Enum_Rep => Enum_Rep :
1980 begin
1981 -- X'Enum_Rep (Y) expands to
1983 -- target-type (Y)
1985 -- This is simply a direct conversion from the enumeration type to
1986 -- the target integer type, which is treated by the back end as a
1987 -- normal integer conversion, treating the enumeration type as an
1988 -- integer, which is exactly what we want! We set Conversion_OK to
1989 -- make sure that the analyzer does not complain about what otherwise
1990 -- might be an illegal conversion.
1992 if Is_Non_Empty_List (Exprs) then
1993 Rewrite (N,
1994 OK_Convert_To (Typ, Relocate_Node (First (Exprs))));
1996 -- X'Enum_Rep where X is an enumeration literal is replaced by
1997 -- the literal value.
1999 elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then
2000 Rewrite (N,
2001 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref))));
2003 -- If this is a renaming of a literal, recover the representation
2004 -- of the original.
2006 elsif Ekind (Entity (Pref)) = E_Constant
2007 and then Present (Renamed_Object (Entity (Pref)))
2008 and then
2009 Ekind (Entity (Renamed_Object (Entity (Pref))))
2010 = E_Enumeration_Literal
2011 then
2012 Rewrite (N,
2013 Make_Integer_Literal (Loc,
2014 Enumeration_Rep (Entity (Renamed_Object (Entity (Pref))))));
2016 -- X'Enum_Rep where X is an object does a direct unchecked conversion
2017 -- of the object value, as described for the type case above.
2019 else
2020 Rewrite (N,
2021 OK_Convert_To (Typ, Relocate_Node (Pref)));
2022 end if;
2024 Set_Etype (N, Typ);
2025 Analyze_And_Resolve (N, Typ);
2026 end Enum_Rep;
2028 --------------
2029 -- Enum_Val --
2030 --------------
2032 when Attribute_Enum_Val => Enum_Val : declare
2033 Expr : Node_Id;
2034 Btyp : constant Entity_Id := Base_Type (Ptyp);
2036 begin
2037 -- X'Enum_Val (Y) expands to
2039 -- [constraint_error when _rep_to_pos (Y, False) = -1, msg]
2040 -- X!(Y);
2042 Expr := Unchecked_Convert_To (Ptyp, First (Exprs));
2044 Insert_Action (N,
2045 Make_Raise_Constraint_Error (Loc,
2046 Condition =>
2047 Make_Op_Eq (Loc,
2048 Left_Opnd =>
2049 Make_Function_Call (Loc,
2050 Name =>
2051 New_Reference_To (TSS (Btyp, TSS_Rep_To_Pos), Loc),
2052 Parameter_Associations => New_List (
2053 Relocate_Node (Duplicate_Subexpr (Expr)),
2054 New_Occurrence_Of (Standard_False, Loc))),
2056 Right_Opnd => Make_Integer_Literal (Loc, -1)),
2057 Reason => CE_Range_Check_Failed));
2059 Rewrite (N, Expr);
2060 Analyze_And_Resolve (N, Ptyp);
2061 end Enum_Val;
2063 --------------
2064 -- Exponent --
2065 --------------
2067 -- Transforms 'Exponent into a call to the floating-point attribute
2068 -- function Exponent in Fat_xxx (where xxx is the root type)
2070 when Attribute_Exponent =>
2071 Expand_Fpt_Attribute_R (N);
2073 ------------------
2074 -- External_Tag --
2075 ------------------
2077 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
2079 when Attribute_External_Tag => External_Tag :
2080 begin
2081 Rewrite (N,
2082 Make_Function_Call (Loc,
2083 Name => New_Reference_To (RTE (RE_External_Tag), Loc),
2084 Parameter_Associations => New_List (
2085 Make_Attribute_Reference (Loc,
2086 Attribute_Name => Name_Tag,
2087 Prefix => Prefix (N)))));
2089 Analyze_And_Resolve (N, Standard_String);
2090 end External_Tag;
2092 -----------
2093 -- First --
2094 -----------
2096 when Attribute_First =>
2098 -- If the prefix type is a constrained packed array type which
2099 -- already has a Packed_Array_Type representation defined, then
2100 -- replace this attribute with a direct reference to 'First of the
2101 -- appropriate index subtype (since otherwise the back end will try
2102 -- to give us the value of 'First for this implementation type).
2104 if Is_Constrained_Packed_Array (Ptyp) then
2105 Rewrite (N,
2106 Make_Attribute_Reference (Loc,
2107 Attribute_Name => Name_First,
2108 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2109 Analyze_And_Resolve (N, Typ);
2111 elsif Is_Access_Type (Ptyp) then
2112 Apply_Access_Check (N);
2113 end if;
2115 ---------------
2116 -- First_Bit --
2117 ---------------
2119 -- Compute this if component clause was present, otherwise we leave the
2120 -- computation to be completed in the back-end, since we don't know what
2121 -- layout will be chosen.
2123 when Attribute_First_Bit => First_Bit_Attr : declare
2124 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2126 begin
2127 -- In Ada 2005 (or later) if we have the standard nondefault
2128 -- bit order, then we return the original value as given in
2129 -- the component clause (RM 2005 13.5.2(3/2)).
2131 if Present (Component_Clause (CE))
2132 and then Ada_Version >= Ada_2005
2133 and then not Reverse_Bit_Order (Scope (CE))
2134 then
2135 Rewrite (N,
2136 Make_Integer_Literal (Loc,
2137 Intval => Expr_Value (First_Bit (Component_Clause (CE)))));
2138 Analyze_And_Resolve (N, Typ);
2140 -- Otherwise (Ada 83/95 or Ada 2005 or later with reverse bit order),
2141 -- rewrite with normalized value if we know it statically.
2143 elsif Known_Static_Component_Bit_Offset (CE) then
2144 Rewrite (N,
2145 Make_Integer_Literal (Loc,
2146 Component_Bit_Offset (CE) mod System_Storage_Unit));
2147 Analyze_And_Resolve (N, Typ);
2149 -- Otherwise left to back end, just do universal integer checks
2151 else
2152 Apply_Universal_Integer_Attribute_Checks (N);
2153 end if;
2154 end First_Bit_Attr;
2156 -----------------
2157 -- Fixed_Value --
2158 -----------------
2160 -- We transform:
2162 -- fixtype'Fixed_Value (integer-value)
2164 -- into
2166 -- fixtype(integer-value)
2168 -- We do all the required analysis of the conversion here, because we do
2169 -- not want this to go through the fixed-point conversion circuits. Note
2170 -- that the back end always treats fixed-point as equivalent to the
2171 -- corresponding integer type anyway.
2173 when Attribute_Fixed_Value => Fixed_Value :
2174 begin
2175 Rewrite (N,
2176 Make_Type_Conversion (Loc,
2177 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2178 Expression => Relocate_Node (First (Exprs))));
2179 Set_Etype (N, Entity (Pref));
2180 Set_Analyzed (N);
2182 -- Note: it might appear that a properly analyzed unchecked conversion
2183 -- would be just fine here, but that's not the case, since the full
2184 -- range checks performed by the following call are critical!
2186 Apply_Type_Conversion_Checks (N);
2187 end Fixed_Value;
2189 -----------
2190 -- Floor --
2191 -----------
2193 -- Transforms 'Floor into a call to the floating-point attribute
2194 -- function Floor in Fat_xxx (where xxx is the root type)
2196 when Attribute_Floor =>
2197 Expand_Fpt_Attribute_R (N);
2199 ----------
2200 -- Fore --
2201 ----------
2203 -- For the fixed-point type Typ:
2205 -- Typ'Fore
2207 -- expands into
2209 -- Result_Type (System.Fore (Universal_Real (Type'First)),
2210 -- Universal_Real (Type'Last))
2212 -- Note that we know that the type is a non-static subtype, or Fore
2213 -- would have itself been computed dynamically in Eval_Attribute.
2215 when Attribute_Fore => Fore : begin
2216 Rewrite (N,
2217 Convert_To (Typ,
2218 Make_Function_Call (Loc,
2219 Name => New_Reference_To (RTE (RE_Fore), Loc),
2221 Parameter_Associations => New_List (
2222 Convert_To (Universal_Real,
2223 Make_Attribute_Reference (Loc,
2224 Prefix => New_Reference_To (Ptyp, Loc),
2225 Attribute_Name => Name_First)),
2227 Convert_To (Universal_Real,
2228 Make_Attribute_Reference (Loc,
2229 Prefix => New_Reference_To (Ptyp, Loc),
2230 Attribute_Name => Name_Last))))));
2232 Analyze_And_Resolve (N, Typ);
2233 end Fore;
2235 --------------
2236 -- Fraction --
2237 --------------
2239 -- Transforms 'Fraction into a call to the floating-point attribute
2240 -- function Fraction in Fat_xxx (where xxx is the root type)
2242 when Attribute_Fraction =>
2243 Expand_Fpt_Attribute_R (N);
2245 --------------
2246 -- From_Any --
2247 --------------
2249 when Attribute_From_Any => From_Any : declare
2250 P_Type : constant Entity_Id := Etype (Pref);
2251 Decls : constant List_Id := New_List;
2252 begin
2253 Rewrite (N,
2254 Build_From_Any_Call (P_Type,
2255 Relocate_Node (First (Exprs)),
2256 Decls));
2257 Insert_Actions (N, Decls);
2258 Analyze_And_Resolve (N, P_Type);
2259 end From_Any;
2261 --------------
2262 -- Identity --
2263 --------------
2265 -- For an exception returns a reference to the exception data:
2266 -- Exception_Id!(Prefix'Reference)
2268 -- For a task it returns a reference to the _task_id component of
2269 -- corresponding record:
2271 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
2273 -- in Ada.Task_Identification
2275 when Attribute_Identity => Identity : declare
2276 Id_Kind : Entity_Id;
2278 begin
2279 if Ptyp = Standard_Exception_Type then
2280 Id_Kind := RTE (RE_Exception_Id);
2282 if Present (Renamed_Object (Entity (Pref))) then
2283 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
2284 end if;
2286 Rewrite (N,
2287 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
2288 else
2289 Id_Kind := RTE (RO_AT_Task_Id);
2291 -- If the prefix is a task interface, the Task_Id is obtained
2292 -- dynamically through a dispatching call, as for other task
2293 -- attributes applied to interfaces.
2295 if Ada_Version >= Ada_2005
2296 and then Ekind (Ptyp) = E_Class_Wide_Type
2297 and then Is_Interface (Ptyp)
2298 and then Is_Task_Interface (Ptyp)
2299 then
2300 Rewrite (N,
2301 Unchecked_Convert_To (Id_Kind,
2302 Make_Selected_Component (Loc,
2303 Prefix =>
2304 New_Copy_Tree (Pref),
2305 Selector_Name =>
2306 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))));
2308 else
2309 Rewrite (N,
2310 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
2311 end if;
2312 end if;
2314 Analyze_And_Resolve (N, Id_Kind);
2315 end Identity;
2317 -----------
2318 -- Image --
2319 -----------
2321 -- Image attribute is handled in separate unit Exp_Imgv
2323 when Attribute_Image =>
2324 Exp_Imgv.Expand_Image_Attribute (N);
2326 ---------
2327 -- Img --
2328 ---------
2330 -- X'Img is expanded to typ'Image (X), where typ is the type of X
2332 when Attribute_Img => Img :
2333 begin
2334 Rewrite (N,
2335 Make_Attribute_Reference (Loc,
2336 Prefix => New_Reference_To (Ptyp, Loc),
2337 Attribute_Name => Name_Image,
2338 Expressions => New_List (Relocate_Node (Pref))));
2340 Analyze_And_Resolve (N, Standard_String);
2341 end Img;
2343 -----------
2344 -- Input --
2345 -----------
2347 when Attribute_Input => Input : declare
2348 P_Type : constant Entity_Id := Entity (Pref);
2349 B_Type : constant Entity_Id := Base_Type (P_Type);
2350 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2351 Strm : constant Node_Id := First (Exprs);
2352 Fname : Entity_Id;
2353 Decl : Node_Id;
2354 Call : Node_Id;
2355 Prag : Node_Id;
2356 Arg2 : Node_Id;
2357 Rfunc : Node_Id;
2359 Cntrl : Node_Id := Empty;
2360 -- Value for controlling argument in call. Always Empty except in
2361 -- the dispatching (class-wide type) case, where it is a reference
2362 -- to the dummy object initialized to the right internal tag.
2364 procedure Freeze_Stream_Subprogram (F : Entity_Id);
2365 -- The expansion of the attribute reference may generate a call to
2366 -- a user-defined stream subprogram that is frozen by the call. This
2367 -- can lead to access-before-elaboration problem if the reference
2368 -- appears in an object declaration and the subprogram body has not
2369 -- been seen. The freezing of the subprogram requires special code
2370 -- because it appears in an expanded context where expressions do
2371 -- not freeze their constituents.
2373 ------------------------------
2374 -- Freeze_Stream_Subprogram --
2375 ------------------------------
2377 procedure Freeze_Stream_Subprogram (F : Entity_Id) is
2378 Decl : constant Node_Id := Unit_Declaration_Node (F);
2379 Bod : Node_Id;
2381 begin
2382 -- If this is user-defined subprogram, the corresponding
2383 -- stream function appears as a renaming-as-body, and the
2384 -- user subprogram must be retrieved by tree traversal.
2386 if Present (Decl)
2387 and then Nkind (Decl) = N_Subprogram_Declaration
2388 and then Present (Corresponding_Body (Decl))
2389 then
2390 Bod := Corresponding_Body (Decl);
2392 if Nkind (Unit_Declaration_Node (Bod)) =
2393 N_Subprogram_Renaming_Declaration
2394 then
2395 Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
2396 end if;
2397 end if;
2398 end Freeze_Stream_Subprogram;
2400 -- Start of processing for Input
2402 begin
2403 -- If no underlying type, we have an error that will be diagnosed
2404 -- elsewhere, so here we just completely ignore the expansion.
2406 if No (U_Type) then
2407 return;
2408 end if;
2410 -- If there is a TSS for Input, just call it
2412 Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
2414 if Present (Fname) then
2415 null;
2417 else
2418 -- If there is a Stream_Convert pragma, use it, we rewrite
2420 -- sourcetyp'Input (stream)
2422 -- as
2424 -- sourcetyp (streamread (strmtyp'Input (stream)));
2426 -- where streamread is the given Read function that converts an
2427 -- argument of type strmtyp to type sourcetyp or a type from which
2428 -- it is derived (extra conversion required for the derived case).
2430 Prag := Get_Stream_Convert_Pragma (P_Type);
2432 if Present (Prag) then
2433 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
2434 Rfunc := Entity (Expression (Arg2));
2436 Rewrite (N,
2437 Convert_To (B_Type,
2438 Make_Function_Call (Loc,
2439 Name => New_Occurrence_Of (Rfunc, Loc),
2440 Parameter_Associations => New_List (
2441 Make_Attribute_Reference (Loc,
2442 Prefix =>
2443 New_Occurrence_Of
2444 (Etype (First_Formal (Rfunc)), Loc),
2445 Attribute_Name => Name_Input,
2446 Expressions => Exprs)))));
2448 Analyze_And_Resolve (N, B_Type);
2449 return;
2451 -- Elementary types
2453 elsif Is_Elementary_Type (U_Type) then
2455 -- A special case arises if we have a defined _Read routine,
2456 -- since in this case we are required to call this routine.
2458 if Present (TSS (Base_Type (U_Type), TSS_Stream_Read)) then
2459 Build_Record_Or_Elementary_Input_Function
2460 (Loc, U_Type, Decl, Fname);
2461 Insert_Action (N, Decl);
2463 -- For normal cases, we call the I_xxx routine directly
2465 else
2466 Rewrite (N, Build_Elementary_Input_Call (N));
2467 Analyze_And_Resolve (N, P_Type);
2468 return;
2469 end if;
2471 -- Array type case
2473 elsif Is_Array_Type (U_Type) then
2474 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
2475 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2477 -- Dispatching case with class-wide type
2479 elsif Is_Class_Wide_Type (P_Type) then
2481 -- No need to do anything else compiling under restriction
2482 -- No_Dispatching_Calls. During the semantic analysis we
2483 -- already notified such violation.
2485 if Restriction_Active (No_Dispatching_Calls) then
2486 return;
2487 end if;
2489 declare
2490 Rtyp : constant Entity_Id := Root_Type (P_Type);
2491 Dnn : Entity_Id;
2492 Decl : Node_Id;
2493 Expr : Node_Id;
2495 begin
2496 -- Read the internal tag (RM 13.13.2(34)) and use it to
2497 -- initialize a dummy tag object:
2499 -- Dnn : Ada.Tags.Tag :=
2500 -- Descendant_Tag (String'Input (Strm), P_Type);
2502 -- This dummy object is used only to provide a controlling
2503 -- argument for the eventual _Input call. Descendant_Tag is
2504 -- called rather than Internal_Tag to ensure that we have a
2505 -- tag for a type that is descended from the prefix type and
2506 -- declared at the same accessibility level (the exception
2507 -- Tag_Error will be raised otherwise). The level check is
2508 -- required for Ada 2005 because tagged types can be
2509 -- extended in nested scopes (AI-344).
2511 Expr :=
2512 Make_Function_Call (Loc,
2513 Name =>
2514 New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc),
2515 Parameter_Associations => New_List (
2516 Make_Attribute_Reference (Loc,
2517 Prefix => New_Occurrence_Of (Standard_String, Loc),
2518 Attribute_Name => Name_Input,
2519 Expressions => New_List (
2520 Relocate_Node (Duplicate_Subexpr (Strm)))),
2521 Make_Attribute_Reference (Loc,
2522 Prefix => New_Reference_To (P_Type, Loc),
2523 Attribute_Name => Name_Tag)));
2525 Dnn := Make_Temporary (Loc, 'D', Expr);
2527 Decl :=
2528 Make_Object_Declaration (Loc,
2529 Defining_Identifier => Dnn,
2530 Object_Definition =>
2531 New_Occurrence_Of (RTE (RE_Tag), Loc),
2532 Expression => Expr);
2534 Insert_Action (N, Decl);
2536 -- Now we need to get the entity for the call, and construct
2537 -- a function call node, where we preset a reference to Dnn
2538 -- as the controlling argument (doing an unchecked convert
2539 -- to the class-wide tagged type to make it look like a real
2540 -- tagged object).
2542 Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
2543 Cntrl :=
2544 Unchecked_Convert_To (P_Type,
2545 New_Occurrence_Of (Dnn, Loc));
2546 Set_Etype (Cntrl, P_Type);
2547 Set_Parent (Cntrl, N);
2548 end;
2550 -- For tagged types, use the primitive Input function
2552 elsif Is_Tagged_Type (U_Type) then
2553 Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
2555 -- All other record type cases, including protected records. The
2556 -- latter only arise for expander generated code for handling
2557 -- shared passive partition access.
2559 else
2560 pragma Assert
2561 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2563 -- Ada 2005 (AI-216): Program_Error is raised executing default
2564 -- implementation of the Input attribute of an unchecked union
2565 -- type if the type lacks default discriminant values.
2567 if Is_Unchecked_Union (Base_Type (U_Type))
2568 and then No (Discriminant_Constraint (U_Type))
2569 then
2570 Insert_Action (N,
2571 Make_Raise_Program_Error (Loc,
2572 Reason => PE_Unchecked_Union_Restriction));
2574 return;
2575 end if;
2577 -- Build the type's Input function, passing the subtype rather
2578 -- than its base type, because checks are needed in the case of
2579 -- constrained discriminants (see Ada 2012 AI05-0192).
2581 Build_Record_Or_Elementary_Input_Function
2582 (Loc, U_Type, Decl, Fname);
2583 Insert_Action (N, Decl);
2585 if Nkind (Parent (N)) = N_Object_Declaration
2586 and then Is_Record_Type (U_Type)
2587 then
2588 -- The stream function may contain calls to user-defined
2589 -- Read procedures for individual components.
2591 declare
2592 Comp : Entity_Id;
2593 Func : Entity_Id;
2595 begin
2596 Comp := First_Component (U_Type);
2597 while Present (Comp) loop
2598 Func :=
2599 Find_Stream_Subprogram
2600 (Etype (Comp), TSS_Stream_Read);
2602 if Present (Func) then
2603 Freeze_Stream_Subprogram (Func);
2604 end if;
2606 Next_Component (Comp);
2607 end loop;
2608 end;
2609 end if;
2610 end if;
2611 end if;
2613 -- If we fall through, Fname is the function to be called. The result
2614 -- is obtained by calling the appropriate function, then converting
2615 -- the result. The conversion does a subtype check.
2617 Call :=
2618 Make_Function_Call (Loc,
2619 Name => New_Occurrence_Of (Fname, Loc),
2620 Parameter_Associations => New_List (
2621 Relocate_Node (Strm)));
2623 Set_Controlling_Argument (Call, Cntrl);
2624 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
2625 Analyze_And_Resolve (N, P_Type);
2627 if Nkind (Parent (N)) = N_Object_Declaration then
2628 Freeze_Stream_Subprogram (Fname);
2629 end if;
2630 end Input;
2632 -------------------
2633 -- Integer_Value --
2634 -------------------
2636 -- We transform
2638 -- inttype'Fixed_Value (fixed-value)
2640 -- into
2642 -- inttype(integer-value))
2644 -- we do all the required analysis of the conversion here, because we do
2645 -- not want this to go through the fixed-point conversion circuits. Note
2646 -- that the back end always treats fixed-point as equivalent to the
2647 -- corresponding integer type anyway.
2649 when Attribute_Integer_Value => Integer_Value :
2650 begin
2651 Rewrite (N,
2652 Make_Type_Conversion (Loc,
2653 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2654 Expression => Relocate_Node (First (Exprs))));
2655 Set_Etype (N, Entity (Pref));
2656 Set_Analyzed (N);
2658 -- Note: it might appear that a properly analyzed unchecked conversion
2659 -- would be just fine here, but that's not the case, since the full
2660 -- range checks performed by the following call are critical!
2662 Apply_Type_Conversion_Checks (N);
2663 end Integer_Value;
2665 -------------------
2666 -- Invalid_Value --
2667 -------------------
2669 when Attribute_Invalid_Value =>
2670 Rewrite (N, Get_Simple_Init_Val (Ptyp, N));
2672 ----------
2673 -- Last --
2674 ----------
2676 when Attribute_Last =>
2678 -- If the prefix type is a constrained packed array type which
2679 -- already has a Packed_Array_Type representation defined, then
2680 -- replace this attribute with a direct reference to 'Last of the
2681 -- appropriate index subtype (since otherwise the back end will try
2682 -- to give us the value of 'Last for this implementation type).
2684 if Is_Constrained_Packed_Array (Ptyp) then
2685 Rewrite (N,
2686 Make_Attribute_Reference (Loc,
2687 Attribute_Name => Name_Last,
2688 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2689 Analyze_And_Resolve (N, Typ);
2691 elsif Is_Access_Type (Ptyp) then
2692 Apply_Access_Check (N);
2693 end if;
2695 --------------
2696 -- Last_Bit --
2697 --------------
2699 -- We compute this if a component clause was present, otherwise we leave
2700 -- the computation up to the back end, since we don't know what layout
2701 -- will be chosen.
2703 when Attribute_Last_Bit => Last_Bit_Attr : declare
2704 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2706 begin
2707 -- In Ada 2005 (or later) if we have the standard nondefault
2708 -- bit order, then we return the original value as given in
2709 -- the component clause (RM 2005 13.5.2(4/2)).
2711 if Present (Component_Clause (CE))
2712 and then Ada_Version >= Ada_2005
2713 and then not Reverse_Bit_Order (Scope (CE))
2714 then
2715 Rewrite (N,
2716 Make_Integer_Literal (Loc,
2717 Intval => Expr_Value (Last_Bit (Component_Clause (CE)))));
2718 Analyze_And_Resolve (N, Typ);
2720 -- Otherwise (Ada 83/95 or Ada 2005 or later with reverse bit order),
2721 -- rewrite with normalized value if we know it statically.
2723 elsif Known_Static_Component_Bit_Offset (CE)
2724 and then Known_Static_Esize (CE)
2725 then
2726 Rewrite (N,
2727 Make_Integer_Literal (Loc,
2728 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
2729 + Esize (CE) - 1));
2730 Analyze_And_Resolve (N, Typ);
2732 -- Otherwise leave to back end, just apply universal integer checks
2734 else
2735 Apply_Universal_Integer_Attribute_Checks (N);
2736 end if;
2737 end Last_Bit_Attr;
2739 ------------------
2740 -- Leading_Part --
2741 ------------------
2743 -- Transforms 'Leading_Part into a call to the floating-point attribute
2744 -- function Leading_Part in Fat_xxx (where xxx is the root type)
2746 -- Note: strictly, we should generate special case code to deal with
2747 -- absurdly large positive arguments (greater than Integer'Last), which
2748 -- result in returning the first argument unchanged, but it hardly seems
2749 -- worth the effort. We raise constraint error for absurdly negative
2750 -- arguments which is fine.
2752 when Attribute_Leading_Part =>
2753 Expand_Fpt_Attribute_RI (N);
2755 ------------
2756 -- Length --
2757 ------------
2759 when Attribute_Length => declare
2760 Ityp : Entity_Id;
2761 Xnum : Uint;
2763 begin
2764 -- Processing for packed array types
2766 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
2767 Ityp := Get_Index_Subtype (N);
2769 -- If the index type, Ityp, is an enumeration type with holes,
2770 -- then we calculate X'Length explicitly using
2772 -- Typ'Max
2773 -- (0, Ityp'Pos (X'Last (N)) -
2774 -- Ityp'Pos (X'First (N)) + 1);
2776 -- Since the bounds in the template are the representation values
2777 -- and the back end would get the wrong value.
2779 if Is_Enumeration_Type (Ityp)
2780 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
2781 then
2782 if No (Exprs) then
2783 Xnum := Uint_1;
2784 else
2785 Xnum := Expr_Value (First (Expressions (N)));
2786 end if;
2788 Rewrite (N,
2789 Make_Attribute_Reference (Loc,
2790 Prefix => New_Occurrence_Of (Typ, Loc),
2791 Attribute_Name => Name_Max,
2792 Expressions => New_List
2793 (Make_Integer_Literal (Loc, 0),
2795 Make_Op_Add (Loc,
2796 Left_Opnd =>
2797 Make_Op_Subtract (Loc,
2798 Left_Opnd =>
2799 Make_Attribute_Reference (Loc,
2800 Prefix => New_Occurrence_Of (Ityp, Loc),
2801 Attribute_Name => Name_Pos,
2803 Expressions => New_List (
2804 Make_Attribute_Reference (Loc,
2805 Prefix => Duplicate_Subexpr (Pref),
2806 Attribute_Name => Name_Last,
2807 Expressions => New_List (
2808 Make_Integer_Literal (Loc, Xnum))))),
2810 Right_Opnd =>
2811 Make_Attribute_Reference (Loc,
2812 Prefix => New_Occurrence_Of (Ityp, Loc),
2813 Attribute_Name => Name_Pos,
2815 Expressions => New_List (
2816 Make_Attribute_Reference (Loc,
2817 Prefix =>
2818 Duplicate_Subexpr_No_Checks (Pref),
2819 Attribute_Name => Name_First,
2820 Expressions => New_List (
2821 Make_Integer_Literal (Loc, Xnum)))))),
2823 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
2825 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2826 return;
2828 -- If the prefix type is a constrained packed array type which
2829 -- already has a Packed_Array_Type representation defined, then
2830 -- replace this attribute with a direct reference to 'Range_Length
2831 -- of the appropriate index subtype (since otherwise the back end
2832 -- will try to give us the value of 'Length for this
2833 -- implementation type).
2835 elsif Is_Constrained (Ptyp) then
2836 Rewrite (N,
2837 Make_Attribute_Reference (Loc,
2838 Attribute_Name => Name_Range_Length,
2839 Prefix => New_Reference_To (Ityp, Loc)));
2840 Analyze_And_Resolve (N, Typ);
2841 end if;
2843 -- Access type case
2845 elsif Is_Access_Type (Ptyp) then
2846 Apply_Access_Check (N);
2848 -- If the designated type is a packed array type, then we convert
2849 -- the reference to:
2851 -- typ'Max (0, 1 +
2852 -- xtyp'Pos (Pref'Last (Expr)) -
2853 -- xtyp'Pos (Pref'First (Expr)));
2855 -- This is a bit complex, but it is the easiest thing to do that
2856 -- works in all cases including enum types with holes xtyp here
2857 -- is the appropriate index type.
2859 declare
2860 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
2861 Xtyp : Entity_Id;
2863 begin
2864 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
2865 Xtyp := Get_Index_Subtype (N);
2867 Rewrite (N,
2868 Make_Attribute_Reference (Loc,
2869 Prefix => New_Occurrence_Of (Typ, Loc),
2870 Attribute_Name => Name_Max,
2871 Expressions => New_List (
2872 Make_Integer_Literal (Loc, 0),
2874 Make_Op_Add (Loc,
2875 Make_Integer_Literal (Loc, 1),
2876 Make_Op_Subtract (Loc,
2877 Left_Opnd =>
2878 Make_Attribute_Reference (Loc,
2879 Prefix => New_Occurrence_Of (Xtyp, Loc),
2880 Attribute_Name => Name_Pos,
2881 Expressions => New_List (
2882 Make_Attribute_Reference (Loc,
2883 Prefix => Duplicate_Subexpr (Pref),
2884 Attribute_Name => Name_Last,
2885 Expressions =>
2886 New_Copy_List (Exprs)))),
2888 Right_Opnd =>
2889 Make_Attribute_Reference (Loc,
2890 Prefix => New_Occurrence_Of (Xtyp, Loc),
2891 Attribute_Name => Name_Pos,
2892 Expressions => New_List (
2893 Make_Attribute_Reference (Loc,
2894 Prefix =>
2895 Duplicate_Subexpr_No_Checks (Pref),
2896 Attribute_Name => Name_First,
2897 Expressions =>
2898 New_Copy_List (Exprs)))))))));
2900 Analyze_And_Resolve (N, Typ);
2901 end if;
2902 end;
2904 -- Otherwise leave it to the back end
2906 else
2907 Apply_Universal_Integer_Attribute_Checks (N);
2908 end if;
2909 end;
2911 -------------
2912 -- Machine --
2913 -------------
2915 -- Transforms 'Machine into a call to the floating-point attribute
2916 -- function Machine in Fat_xxx (where xxx is the root type)
2918 when Attribute_Machine =>
2919 Expand_Fpt_Attribute_R (N);
2921 ----------------------
2922 -- Machine_Rounding --
2923 ----------------------
2925 -- Transforms 'Machine_Rounding into a call to the floating-point
2926 -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root
2927 -- type). Expansion is avoided for cases the back end can handle
2928 -- directly.
2930 when Attribute_Machine_Rounding =>
2931 if not Is_Inline_Floating_Point_Attribute (N) then
2932 Expand_Fpt_Attribute_R (N);
2933 end if;
2935 ------------------
2936 -- Machine_Size --
2937 ------------------
2939 -- Machine_Size is equivalent to Object_Size, so transform it into
2940 -- Object_Size and that way the back end never sees Machine_Size.
2942 when Attribute_Machine_Size =>
2943 Rewrite (N,
2944 Make_Attribute_Reference (Loc,
2945 Prefix => Prefix (N),
2946 Attribute_Name => Name_Object_Size));
2948 Analyze_And_Resolve (N, Typ);
2950 --------------
2951 -- Mantissa --
2952 --------------
2954 -- The only case that can get this far is the dynamic case of the old
2955 -- Ada 83 Mantissa attribute for the fixed-point case. For this case,
2956 -- we expand:
2958 -- typ'Mantissa
2960 -- into
2962 -- ityp (System.Mantissa.Mantissa_Value
2963 -- (Integer'Integer_Value (typ'First),
2964 -- Integer'Integer_Value (typ'Last)));
2966 when Attribute_Mantissa => Mantissa : begin
2967 Rewrite (N,
2968 Convert_To (Typ,
2969 Make_Function_Call (Loc,
2970 Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
2972 Parameter_Associations => New_List (
2974 Make_Attribute_Reference (Loc,
2975 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2976 Attribute_Name => Name_Integer_Value,
2977 Expressions => New_List (
2979 Make_Attribute_Reference (Loc,
2980 Prefix => New_Occurrence_Of (Ptyp, Loc),
2981 Attribute_Name => Name_First))),
2983 Make_Attribute_Reference (Loc,
2984 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2985 Attribute_Name => Name_Integer_Value,
2986 Expressions => New_List (
2988 Make_Attribute_Reference (Loc,
2989 Prefix => New_Occurrence_Of (Ptyp, Loc),
2990 Attribute_Name => Name_Last)))))));
2992 Analyze_And_Resolve (N, Typ);
2993 end Mantissa;
2995 ----------------------------------
2996 -- Max_Size_In_Storage_Elements --
2997 ----------------------------------
2999 when Attribute_Max_Size_In_Storage_Elements =>
3000 Apply_Universal_Integer_Attribute_Checks (N);
3002 -- Heap-allocated controlled objects contain two extra pointers which
3003 -- are not part of the actual type. Transform the attribute reference
3004 -- into a runtime expression to add the size of the hidden header.
3006 -- Do not perform this expansion on .NET/JVM targets because the
3007 -- two pointers are already present in the type.
3009 if VM_Target = No_VM
3010 and then Nkind (N) = N_Attribute_Reference
3011 and then Needs_Finalization (Ptyp)
3012 and then not Header_Size_Added (N)
3013 then
3014 Set_Header_Size_Added (N);
3016 -- Generate:
3017 -- P'Max_Size_In_Storage_Elements +
3018 -- Universal_Integer
3019 -- (Header_Size_With_Padding (Ptyp'Alignment))
3021 Rewrite (N,
3022 Make_Op_Add (Loc,
3023 Left_Opnd => Relocate_Node (N),
3024 Right_Opnd =>
3025 Convert_To (Universal_Integer,
3026 Make_Function_Call (Loc,
3027 Name =>
3028 New_Reference_To
3029 (RTE (RE_Header_Size_With_Padding), Loc),
3031 Parameter_Associations => New_List (
3032 Make_Attribute_Reference (Loc,
3033 Prefix =>
3034 New_Reference_To (Ptyp, Loc),
3035 Attribute_Name => Name_Alignment))))));
3037 Analyze (N);
3038 return;
3039 end if;
3041 --------------------
3042 -- Mechanism_Code --
3043 --------------------
3045 when Attribute_Mechanism_Code =>
3047 -- We must replace the prefix in the renamed case
3049 if Is_Entity_Name (Pref)
3050 and then Present (Alias (Entity (Pref)))
3051 then
3052 Set_Renamed_Subprogram (Pref, Alias (Entity (Pref)));
3053 end if;
3055 ---------
3056 -- Mod --
3057 ---------
3059 when Attribute_Mod => Mod_Case : declare
3060 Arg : constant Node_Id := Relocate_Node (First (Exprs));
3061 Hi : constant Node_Id := Type_High_Bound (Etype (Arg));
3062 Modv : constant Uint := Modulus (Btyp);
3064 begin
3066 -- This is not so simple. The issue is what type to use for the
3067 -- computation of the modular value.
3069 -- The easy case is when the modulus value is within the bounds
3070 -- of the signed integer type of the argument. In this case we can
3071 -- just do the computation in that signed integer type, and then
3072 -- do an ordinary conversion to the target type.
3074 if Modv <= Expr_Value (Hi) then
3075 Rewrite (N,
3076 Convert_To (Btyp,
3077 Make_Op_Mod (Loc,
3078 Left_Opnd => Arg,
3079 Right_Opnd => Make_Integer_Literal (Loc, Modv))));
3081 -- Here we know that the modulus is larger than type'Last of the
3082 -- integer type. There are two cases to consider:
3084 -- a) The integer value is non-negative. In this case, it is
3085 -- returned as the result (since it is less than the modulus).
3087 -- b) The integer value is negative. In this case, we know that the
3088 -- result is modulus + value, where the value might be as small as
3089 -- -modulus. The trouble is what type do we use to do the subtract.
3090 -- No type will do, since modulus can be as big as 2**64, and no
3091 -- integer type accommodates this value. Let's do bit of algebra
3093 -- modulus + value
3094 -- = modulus - (-value)
3095 -- = (modulus - 1) - (-value - 1)
3097 -- Now modulus - 1 is certainly in range of the modular type.
3098 -- -value is in the range 1 .. modulus, so -value -1 is in the
3099 -- range 0 .. modulus-1 which is in range of the modular type.
3100 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
3101 -- which we can compute using the integer base type.
3103 -- Once this is done we analyze the conditional expression without
3104 -- range checks, because we know everything is in range, and we
3105 -- want to prevent spurious warnings on either branch.
3107 else
3108 Rewrite (N,
3109 Make_Conditional_Expression (Loc,
3110 Expressions => New_List (
3111 Make_Op_Ge (Loc,
3112 Left_Opnd => Duplicate_Subexpr (Arg),
3113 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3115 Convert_To (Btyp,
3116 Duplicate_Subexpr_No_Checks (Arg)),
3118 Make_Op_Subtract (Loc,
3119 Left_Opnd =>
3120 Make_Integer_Literal (Loc,
3121 Intval => Modv - 1),
3122 Right_Opnd =>
3123 Convert_To (Btyp,
3124 Make_Op_Minus (Loc,
3125 Right_Opnd =>
3126 Make_Op_Add (Loc,
3127 Left_Opnd => Duplicate_Subexpr_No_Checks (Arg),
3128 Right_Opnd =>
3129 Make_Integer_Literal (Loc,
3130 Intval => 1))))))));
3132 end if;
3134 Analyze_And_Resolve (N, Btyp, Suppress => All_Checks);
3135 end Mod_Case;
3137 -----------
3138 -- Model --
3139 -----------
3141 -- Transforms 'Model into a call to the floating-point attribute
3142 -- function Model in Fat_xxx (where xxx is the root type)
3144 when Attribute_Model =>
3145 Expand_Fpt_Attribute_R (N);
3147 -----------------
3148 -- Object_Size --
3149 -----------------
3151 -- The processing for Object_Size shares the processing for Size
3153 ---------
3154 -- Old --
3155 ---------
3157 when Attribute_Old => Old : declare
3158 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', Pref);
3159 Subp : Node_Id;
3160 Asn_Stm : Node_Id;
3162 begin
3163 -- Find the nearest subprogram body, ignoring _Preconditions
3165 Subp := N;
3166 loop
3167 Subp := Parent (Subp);
3168 exit when Nkind (Subp) = N_Subprogram_Body
3169 and then Chars (Defining_Entity (Subp)) /= Name_uPostconditions;
3170 end loop;
3172 -- Insert the initialized object declaration at the start of the
3173 -- subprogram's declarations.
3175 Asn_Stm :=
3176 Make_Object_Declaration (Loc,
3177 Defining_Identifier => Tnn,
3178 Constant_Present => True,
3179 Object_Definition => New_Occurrence_Of (Etype (N), Loc),
3180 Expression => Pref);
3182 -- Push the subprogram's scope, so that the object will be analyzed
3183 -- in that context (rather than the context of the Precondition
3184 -- subprogram) and will have its Scope set properly.
3186 if Present (Corresponding_Spec (Subp)) then
3187 Push_Scope (Corresponding_Spec (Subp));
3188 else
3189 Push_Scope (Defining_Entity (Subp));
3190 end if;
3192 if Is_Empty_List (Declarations (Subp)) then
3193 Set_Declarations (Subp, New_List (Asn_Stm));
3194 Analyze (Asn_Stm);
3195 else
3196 Insert_Action (First (Declarations (Subp)), Asn_Stm);
3197 end if;
3199 Pop_Scope;
3201 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
3202 end Old;
3204 ----------------------
3205 -- Overlaps_Storage --
3206 ----------------------
3208 when Attribute_Overlaps_Storage => Overlaps_Storage : declare
3209 Loc : constant Source_Ptr := Sloc (N);
3211 X : constant Node_Id := Prefix (N);
3212 Y : constant Node_Id := First (Expressions (N));
3213 -- The argumens
3215 X_Addr, Y_Addr : Node_Id;
3216 -- the expressions for their integer addresses
3218 X_Size, Y_Size : Node_Id;
3219 -- the expressions for their sizes
3221 Cond : Node_Id;
3223 begin
3224 -- Attribute expands into:
3226 -- if X'Address < Y'address then
3227 -- (X'address + X'Size - 1) >= Y'address
3228 -- else
3229 -- (Y'address + Y'size - 1) >= X'Address
3230 -- end if;
3232 -- with the proper address operations. We convert addresses to
3233 -- integer addresses to use predefined arithmetic. The size is
3234 -- expressed in storage units.
3236 X_Addr :=
3237 Unchecked_Convert_To (RTE (RE_Integer_Address),
3238 Make_Attribute_Reference (Loc,
3239 Attribute_Name => Name_Address,
3240 Prefix => New_Copy_Tree (X)));
3242 Y_Addr :=
3243 Unchecked_Convert_To (RTE (RE_Integer_Address),
3244 Make_Attribute_Reference (Loc,
3245 Attribute_Name => Name_Address,
3246 Prefix => New_Copy_Tree (Y)));
3248 X_Size :=
3249 Make_Op_Divide (Loc,
3250 Left_Opnd =>
3251 Make_Attribute_Reference (Loc,
3252 Attribute_Name => Name_Size,
3253 Prefix => New_Copy_Tree (X)),
3254 Right_Opnd =>
3255 Make_Integer_Literal (Loc, System_Storage_Unit));
3257 Y_Size :=
3258 Make_Op_Divide (Loc,
3259 Left_Opnd =>
3260 Make_Attribute_Reference (Loc,
3261 Attribute_Name => Name_Size,
3262 Prefix => New_Copy_Tree (Y)),
3263 Right_Opnd =>
3264 Make_Integer_Literal (Loc, System_Storage_Unit));
3266 Cond :=
3267 Make_Op_Le (Loc,
3268 Left_Opnd => X_Addr,
3269 Right_Opnd => Y_Addr);
3271 Rewrite (N,
3272 Make_Conditional_Expression (Loc,
3273 New_List (
3274 Cond,
3276 Make_Op_Ge (Loc,
3277 Left_Opnd =>
3278 Make_Op_Add (Loc,
3279 Left_Opnd => X_Addr,
3280 Right_Opnd =>
3281 Make_Op_Subtract (Loc,
3282 Left_Opnd => X_Size,
3283 Right_Opnd => Make_Integer_Literal (Loc, 1))),
3284 Right_Opnd => Y_Addr),
3286 Make_Op_Ge (Loc,
3287 Make_Op_Add (Loc,
3288 Left_Opnd => Y_Addr,
3289 Right_Opnd =>
3290 Make_Op_Subtract (Loc,
3291 Left_Opnd => Y_Size,
3292 Right_Opnd => Make_Integer_Literal (Loc, 1))),
3293 Right_Opnd => X_Addr))));
3295 Analyze_And_Resolve (N, Standard_Boolean);
3296 end Overlaps_Storage;
3298 ------------
3299 -- Output --
3300 ------------
3302 when Attribute_Output => Output : declare
3303 P_Type : constant Entity_Id := Entity (Pref);
3304 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3305 Pname : Entity_Id;
3306 Decl : Node_Id;
3307 Prag : Node_Id;
3308 Arg3 : Node_Id;
3309 Wfunc : Node_Id;
3311 begin
3312 -- If no underlying type, we have an error that will be diagnosed
3313 -- elsewhere, so here we just completely ignore the expansion.
3315 if No (U_Type) then
3316 return;
3317 end if;
3319 -- If TSS for Output is present, just call it
3321 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
3323 if Present (Pname) then
3324 null;
3326 else
3327 -- If there is a Stream_Convert pragma, use it, we rewrite
3329 -- sourcetyp'Output (stream, Item)
3331 -- as
3333 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
3335 -- where strmwrite is the given Write function that converts an
3336 -- argument of type sourcetyp or a type acctyp, from which it is
3337 -- derived to type strmtyp. The conversion to acttyp is required
3338 -- for the derived case.
3340 Prag := Get_Stream_Convert_Pragma (P_Type);
3342 if Present (Prag) then
3343 Arg3 :=
3344 Next (Next (First (Pragma_Argument_Associations (Prag))));
3345 Wfunc := Entity (Expression (Arg3));
3347 Rewrite (N,
3348 Make_Attribute_Reference (Loc,
3349 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
3350 Attribute_Name => Name_Output,
3351 Expressions => New_List (
3352 Relocate_Node (First (Exprs)),
3353 Make_Function_Call (Loc,
3354 Name => New_Occurrence_Of (Wfunc, Loc),
3355 Parameter_Associations => New_List (
3356 OK_Convert_To (Etype (First_Formal (Wfunc)),
3357 Relocate_Node (Next (First (Exprs)))))))));
3359 Analyze (N);
3360 return;
3362 -- For elementary types, we call the W_xxx routine directly.
3363 -- Note that the effect of Write and Output is identical for
3364 -- the case of an elementary type, since there are no
3365 -- discriminants or bounds.
3367 elsif Is_Elementary_Type (U_Type) then
3369 -- A special case arises if we have a defined _Write routine,
3370 -- since in this case we are required to call this routine.
3372 if Present (TSS (Base_Type (U_Type), TSS_Stream_Write)) then
3373 Build_Record_Or_Elementary_Output_Procedure
3374 (Loc, U_Type, Decl, Pname);
3375 Insert_Action (N, Decl);
3377 -- For normal cases, we call the W_xxx routine directly
3379 else
3380 Rewrite (N, Build_Elementary_Write_Call (N));
3381 Analyze (N);
3382 return;
3383 end if;
3385 -- Array type case
3387 elsif Is_Array_Type (U_Type) then
3388 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
3389 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3391 -- Class-wide case, first output external tag, then dispatch
3392 -- to the appropriate primitive Output function (RM 13.13.2(31)).
3394 elsif Is_Class_Wide_Type (P_Type) then
3396 -- No need to do anything else compiling under restriction
3397 -- No_Dispatching_Calls. During the semantic analysis we
3398 -- already notified such violation.
3400 if Restriction_Active (No_Dispatching_Calls) then
3401 return;
3402 end if;
3404 Tag_Write : declare
3405 Strm : constant Node_Id := First (Exprs);
3406 Item : constant Node_Id := Next (Strm);
3408 begin
3409 -- Ada 2005 (AI-344): Check that the accessibility level
3410 -- of the type of the output object is not deeper than
3411 -- that of the attribute's prefix type.
3413 -- if Get_Access_Level (Item'Tag)
3414 -- /= Get_Access_Level (P_Type'Tag)
3415 -- then
3416 -- raise Tag_Error;
3417 -- end if;
3419 -- String'Output (Strm, External_Tag (Item'Tag));
3421 -- We cannot figure out a practical way to implement this
3422 -- accessibility check on virtual machines, so we omit it.
3424 if Ada_Version >= Ada_2005
3425 and then Tagged_Type_Expansion
3426 then
3427 Insert_Action (N,
3428 Make_Implicit_If_Statement (N,
3429 Condition =>
3430 Make_Op_Ne (Loc,
3431 Left_Opnd =>
3432 Build_Get_Access_Level (Loc,
3433 Make_Attribute_Reference (Loc,
3434 Prefix =>
3435 Relocate_Node (
3436 Duplicate_Subexpr (Item,
3437 Name_Req => True)),
3438 Attribute_Name => Name_Tag)),
3440 Right_Opnd =>
3441 Make_Integer_Literal (Loc,
3442 Type_Access_Level (P_Type))),
3444 Then_Statements =>
3445 New_List (Make_Raise_Statement (Loc,
3446 New_Occurrence_Of (
3447 RTE (RE_Tag_Error), Loc)))));
3448 end if;
3450 Insert_Action (N,
3451 Make_Attribute_Reference (Loc,
3452 Prefix => New_Occurrence_Of (Standard_String, Loc),
3453 Attribute_Name => Name_Output,
3454 Expressions => New_List (
3455 Relocate_Node (Duplicate_Subexpr (Strm)),
3456 Make_Function_Call (Loc,
3457 Name =>
3458 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
3459 Parameter_Associations => New_List (
3460 Make_Attribute_Reference (Loc,
3461 Prefix =>
3462 Relocate_Node
3463 (Duplicate_Subexpr (Item, Name_Req => True)),
3464 Attribute_Name => Name_Tag))))));
3465 end Tag_Write;
3467 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3469 -- Tagged type case, use the primitive Output function
3471 elsif Is_Tagged_Type (U_Type) then
3472 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3474 -- All other record type cases, including protected records.
3475 -- The latter only arise for expander generated code for
3476 -- handling shared passive partition access.
3478 else
3479 pragma Assert
3480 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3482 -- Ada 2005 (AI-216): Program_Error is raised when executing
3483 -- the default implementation of the Output attribute of an
3484 -- unchecked union type if the type lacks default discriminant
3485 -- values.
3487 if Is_Unchecked_Union (Base_Type (U_Type))
3488 and then No (Discriminant_Constraint (U_Type))
3489 then
3490 Insert_Action (N,
3491 Make_Raise_Program_Error (Loc,
3492 Reason => PE_Unchecked_Union_Restriction));
3494 return;
3495 end if;
3497 Build_Record_Or_Elementary_Output_Procedure
3498 (Loc, Base_Type (U_Type), Decl, Pname);
3499 Insert_Action (N, Decl);
3500 end if;
3501 end if;
3503 -- If we fall through, Pname is the name of the procedure to call
3505 Rewrite_Stream_Proc_Call (Pname);
3506 end Output;
3508 ---------
3509 -- Pos --
3510 ---------
3512 -- For enumeration types with a standard representation, Pos is
3513 -- handled by the back end.
3515 -- For enumeration types, with a non-standard representation we generate
3516 -- a call to the _Rep_To_Pos function created when the type was frozen.
3517 -- The call has the form
3519 -- _rep_to_pos (expr, flag)
3521 -- The parameter flag is True if range checks are enabled, causing
3522 -- Program_Error to be raised if the expression has an invalid
3523 -- representation, and False if range checks are suppressed.
3525 -- For integer types, Pos is equivalent to a simple integer
3526 -- conversion and we rewrite it as such
3528 when Attribute_Pos => Pos :
3529 declare
3530 Etyp : Entity_Id := Base_Type (Entity (Pref));
3532 begin
3533 -- Deal with zero/non-zero boolean values
3535 if Is_Boolean_Type (Etyp) then
3536 Adjust_Condition (First (Exprs));
3537 Etyp := Standard_Boolean;
3538 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
3539 end if;
3541 -- Case of enumeration type
3543 if Is_Enumeration_Type (Etyp) then
3545 -- Non-standard enumeration type (generate call)
3547 if Present (Enum_Pos_To_Rep (Etyp)) then
3548 Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
3549 Rewrite (N,
3550 Convert_To (Typ,
3551 Make_Function_Call (Loc,
3552 Name =>
3553 New_Reference_To (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3554 Parameter_Associations => Exprs)));
3556 Analyze_And_Resolve (N, Typ);
3558 -- Standard enumeration type (do universal integer check)
3560 else
3561 Apply_Universal_Integer_Attribute_Checks (N);
3562 end if;
3564 -- Deal with integer types (replace by conversion)
3566 elsif Is_Integer_Type (Etyp) then
3567 Rewrite (N, Convert_To (Typ, First (Exprs)));
3568 Analyze_And_Resolve (N, Typ);
3569 end if;
3571 end Pos;
3573 --------------
3574 -- Position --
3575 --------------
3577 -- We compute this if a component clause was present, otherwise we leave
3578 -- the computation up to the back end, since we don't know what layout
3579 -- will be chosen.
3581 when Attribute_Position => Position_Attr :
3582 declare
3583 CE : constant Entity_Id := Entity (Selector_Name (Pref));
3585 begin
3586 if Present (Component_Clause (CE)) then
3588 -- In Ada 2005 (or later) if we have the standard nondefault
3589 -- bit order, then we return the original value as given in
3590 -- the component clause (RM 2005 13.5.2(2/2)).
3592 if Ada_Version >= Ada_2005
3593 and then not Reverse_Bit_Order (Scope (CE))
3594 then
3595 Rewrite (N,
3596 Make_Integer_Literal (Loc,
3597 Intval => Expr_Value (Position (Component_Clause (CE)))));
3599 -- Otherwise (Ada 83 or 95, or reverse bit order specified in
3600 -- later Ada version), return the normalized value.
3602 else
3603 Rewrite (N,
3604 Make_Integer_Literal (Loc,
3605 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
3606 end if;
3608 Analyze_And_Resolve (N, Typ);
3610 -- If back end is doing things, just apply universal integer checks
3612 else
3613 Apply_Universal_Integer_Attribute_Checks (N);
3614 end if;
3615 end Position_Attr;
3617 ----------
3618 -- Pred --
3619 ----------
3621 -- 1. Deal with enumeration types with holes
3622 -- 2. For floating-point, generate call to attribute function
3623 -- 3. For other cases, deal with constraint checking
3625 when Attribute_Pred => Pred :
3626 declare
3627 Etyp : constant Entity_Id := Base_Type (Ptyp);
3629 begin
3631 -- For enumeration types with non-standard representations, we
3632 -- expand typ'Pred (x) into
3634 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
3636 -- If the representation is contiguous, we compute instead
3637 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
3638 -- The conversion function Enum_Pos_To_Rep is defined on the
3639 -- base type, not the subtype, so we have to use the base type
3640 -- explicitly for this and other enumeration attributes.
3642 if Is_Enumeration_Type (Ptyp)
3643 and then Present (Enum_Pos_To_Rep (Etyp))
3644 then
3645 if Has_Contiguous_Rep (Etyp) then
3646 Rewrite (N,
3647 Unchecked_Convert_To (Ptyp,
3648 Make_Op_Add (Loc,
3649 Left_Opnd =>
3650 Make_Integer_Literal (Loc,
3651 Enumeration_Rep (First_Literal (Ptyp))),
3652 Right_Opnd =>
3653 Make_Function_Call (Loc,
3654 Name =>
3655 New_Reference_To
3656 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3658 Parameter_Associations =>
3659 New_List (
3660 Unchecked_Convert_To (Ptyp,
3661 Make_Op_Subtract (Loc,
3662 Left_Opnd =>
3663 Unchecked_Convert_To (Standard_Integer,
3664 Relocate_Node (First (Exprs))),
3665 Right_Opnd =>
3666 Make_Integer_Literal (Loc, 1))),
3667 Rep_To_Pos_Flag (Ptyp, Loc))))));
3669 else
3670 -- Add Boolean parameter True, to request program errror if
3671 -- we have a bad representation on our hands. If checks are
3672 -- suppressed, then add False instead
3674 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
3675 Rewrite (N,
3676 Make_Indexed_Component (Loc,
3677 Prefix =>
3678 New_Reference_To
3679 (Enum_Pos_To_Rep (Etyp), Loc),
3680 Expressions => New_List (
3681 Make_Op_Subtract (Loc,
3682 Left_Opnd =>
3683 Make_Function_Call (Loc,
3684 Name =>
3685 New_Reference_To
3686 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3687 Parameter_Associations => Exprs),
3688 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3689 end if;
3691 Analyze_And_Resolve (N, Typ);
3693 -- For floating-point, we transform 'Pred into a call to the Pred
3694 -- floating-point attribute function in Fat_xxx (xxx is root type)
3696 elsif Is_Floating_Point_Type (Ptyp) then
3697 Expand_Fpt_Attribute_R (N);
3698 Analyze_And_Resolve (N, Typ);
3700 -- For modular types, nothing to do (no overflow, since wraps)
3702 elsif Is_Modular_Integer_Type (Ptyp) then
3703 null;
3705 -- For other types, if argument is marked as needing a range check or
3706 -- overflow checking is enabled, we must generate a check.
3708 elsif not Overflow_Checks_Suppressed (Ptyp)
3709 or else Do_Range_Check (First (Exprs))
3710 then
3711 Set_Do_Range_Check (First (Exprs), False);
3712 Expand_Pred_Succ (N);
3713 end if;
3714 end Pred;
3716 --------------
3717 -- Priority --
3718 --------------
3720 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3722 -- We rewrite X'Priority as the following run-time call:
3724 -- Get_Ceiling (X._Object)
3726 -- Note that although X'Priority is notionally an object, it is quite
3727 -- deliberately not defined as an aliased object in the RM. This means
3728 -- that it works fine to rewrite it as a call, without having to worry
3729 -- about complications that would other arise from X'Priority'Access,
3730 -- which is illegal, because of the lack of aliasing.
3732 when Attribute_Priority =>
3733 declare
3734 Call : Node_Id;
3735 Conctyp : Entity_Id;
3736 Object_Parm : Node_Id;
3737 Subprg : Entity_Id;
3738 RT_Subprg_Name : Node_Id;
3740 begin
3741 -- Look for the enclosing concurrent type
3743 Conctyp := Current_Scope;
3744 while not Is_Concurrent_Type (Conctyp) loop
3745 Conctyp := Scope (Conctyp);
3746 end loop;
3748 pragma Assert (Is_Protected_Type (Conctyp));
3750 -- Generate the actual of the call
3752 Subprg := Current_Scope;
3753 while not Present (Protected_Body_Subprogram (Subprg)) loop
3754 Subprg := Scope (Subprg);
3755 end loop;
3757 -- Use of 'Priority inside protected entries and barriers (in
3758 -- both cases the type of the first formal of their expanded
3759 -- subprogram is Address)
3761 if Etype (First_Entity (Protected_Body_Subprogram (Subprg)))
3762 = RTE (RE_Address)
3763 then
3764 declare
3765 New_Itype : Entity_Id;
3767 begin
3768 -- In the expansion of protected entries the type of the
3769 -- first formal of the Protected_Body_Subprogram is an
3770 -- Address. In order to reference the _object component
3771 -- we generate:
3773 -- type T is access p__ptTV;
3774 -- freeze T []
3776 New_Itype := Create_Itype (E_Access_Type, N);
3777 Set_Etype (New_Itype, New_Itype);
3778 Set_Directly_Designated_Type (New_Itype,
3779 Corresponding_Record_Type (Conctyp));
3780 Freeze_Itype (New_Itype, N);
3782 -- Generate:
3783 -- T!(O)._object'unchecked_access
3785 Object_Parm :=
3786 Make_Attribute_Reference (Loc,
3787 Prefix =>
3788 Make_Selected_Component (Loc,
3789 Prefix =>
3790 Unchecked_Convert_To (New_Itype,
3791 New_Reference_To
3792 (First_Entity
3793 (Protected_Body_Subprogram (Subprg)),
3794 Loc)),
3795 Selector_Name =>
3796 Make_Identifier (Loc, Name_uObject)),
3797 Attribute_Name => Name_Unchecked_Access);
3798 end;
3800 -- Use of 'Priority inside a protected subprogram
3802 else
3803 Object_Parm :=
3804 Make_Attribute_Reference (Loc,
3805 Prefix =>
3806 Make_Selected_Component (Loc,
3807 Prefix => New_Reference_To
3808 (First_Entity
3809 (Protected_Body_Subprogram (Subprg)),
3810 Loc),
3811 Selector_Name => Make_Identifier (Loc, Name_uObject)),
3812 Attribute_Name => Name_Unchecked_Access);
3813 end if;
3815 -- Select the appropriate run-time subprogram
3817 if Number_Entries (Conctyp) = 0 then
3818 RT_Subprg_Name :=
3819 New_Reference_To (RTE (RE_Get_Ceiling), Loc);
3820 else
3821 RT_Subprg_Name :=
3822 New_Reference_To (RTE (RO_PE_Get_Ceiling), Loc);
3823 end if;
3825 Call :=
3826 Make_Function_Call (Loc,
3827 Name => RT_Subprg_Name,
3828 Parameter_Associations => New_List (Object_Parm));
3830 Rewrite (N, Call);
3832 -- Avoid the generation of extra checks on the pointer to the
3833 -- protected object.
3835 Analyze_And_Resolve (N, Typ, Suppress => Access_Check);
3836 end;
3838 ------------------
3839 -- Range_Length --
3840 ------------------
3842 when Attribute_Range_Length => Range_Length : begin
3844 -- The only special processing required is for the case where
3845 -- Range_Length is applied to an enumeration type with holes.
3846 -- In this case we transform
3848 -- X'Range_Length
3850 -- to
3852 -- X'Pos (X'Last) - X'Pos (X'First) + 1
3854 -- So that the result reflects the proper Pos values instead
3855 -- of the underlying representations.
3857 if Is_Enumeration_Type (Ptyp)
3858 and then Has_Non_Standard_Rep (Ptyp)
3859 then
3860 Rewrite (N,
3861 Make_Op_Add (Loc,
3862 Left_Opnd =>
3863 Make_Op_Subtract (Loc,
3864 Left_Opnd =>
3865 Make_Attribute_Reference (Loc,
3866 Attribute_Name => Name_Pos,
3867 Prefix => New_Occurrence_Of (Ptyp, Loc),
3868 Expressions => New_List (
3869 Make_Attribute_Reference (Loc,
3870 Attribute_Name => Name_Last,
3871 Prefix => New_Occurrence_Of (Ptyp, Loc)))),
3873 Right_Opnd =>
3874 Make_Attribute_Reference (Loc,
3875 Attribute_Name => Name_Pos,
3876 Prefix => New_Occurrence_Of (Ptyp, Loc),
3877 Expressions => New_List (
3878 Make_Attribute_Reference (Loc,
3879 Attribute_Name => Name_First,
3880 Prefix => New_Occurrence_Of (Ptyp, Loc))))),
3882 Right_Opnd => Make_Integer_Literal (Loc, 1)));
3884 Analyze_And_Resolve (N, Typ);
3886 -- For all other cases, the attribute is handled by the back end, but
3887 -- we need to deal with the case of the range check on a universal
3888 -- integer.
3890 else
3891 Apply_Universal_Integer_Attribute_Checks (N);
3892 end if;
3893 end Range_Length;
3895 ----------
3896 -- Read --
3897 ----------
3899 when Attribute_Read => Read : declare
3900 P_Type : constant Entity_Id := Entity (Pref);
3901 B_Type : constant Entity_Id := Base_Type (P_Type);
3902 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3903 Pname : Entity_Id;
3904 Decl : Node_Id;
3905 Prag : Node_Id;
3906 Arg2 : Node_Id;
3907 Rfunc : Node_Id;
3908 Lhs : Node_Id;
3909 Rhs : Node_Id;
3911 begin
3912 -- If no underlying type, we have an error that will be diagnosed
3913 -- elsewhere, so here we just completely ignore the expansion.
3915 if No (U_Type) then
3916 return;
3917 end if;
3919 -- The simple case, if there is a TSS for Read, just call it
3921 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
3923 if Present (Pname) then
3924 null;
3926 else
3927 -- If there is a Stream_Convert pragma, use it, we rewrite
3929 -- sourcetyp'Read (stream, Item)
3931 -- as
3933 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
3935 -- where strmread is the given Read function that converts an
3936 -- argument of type strmtyp to type sourcetyp or a type from which
3937 -- it is derived. The conversion to sourcetyp is required in the
3938 -- latter case.
3940 -- A special case arises if Item is a type conversion in which
3941 -- case, we have to expand to:
3943 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
3945 -- where Itemx is the expression of the type conversion (i.e.
3946 -- the actual object), and typex is the type of Itemx.
3948 Prag := Get_Stream_Convert_Pragma (P_Type);
3950 if Present (Prag) then
3951 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
3952 Rfunc := Entity (Expression (Arg2));
3953 Lhs := Relocate_Node (Next (First (Exprs)));
3954 Rhs :=
3955 OK_Convert_To (B_Type,
3956 Make_Function_Call (Loc,
3957 Name => New_Occurrence_Of (Rfunc, Loc),
3958 Parameter_Associations => New_List (
3959 Make_Attribute_Reference (Loc,
3960 Prefix =>
3961 New_Occurrence_Of
3962 (Etype (First_Formal (Rfunc)), Loc),
3963 Attribute_Name => Name_Input,
3964 Expressions => New_List (
3965 Relocate_Node (First (Exprs)))))));
3967 if Nkind (Lhs) = N_Type_Conversion then
3968 Lhs := Expression (Lhs);
3969 Rhs := Convert_To (Etype (Lhs), Rhs);
3970 end if;
3972 Rewrite (N,
3973 Make_Assignment_Statement (Loc,
3974 Name => Lhs,
3975 Expression => Rhs));
3976 Set_Assignment_OK (Lhs);
3977 Analyze (N);
3978 return;
3980 -- For elementary types, we call the I_xxx routine using the first
3981 -- parameter and then assign the result into the second parameter.
3982 -- We set Assignment_OK to deal with the conversion case.
3984 elsif Is_Elementary_Type (U_Type) then
3985 declare
3986 Lhs : Node_Id;
3987 Rhs : Node_Id;
3989 begin
3990 Lhs := Relocate_Node (Next (First (Exprs)));
3991 Rhs := Build_Elementary_Input_Call (N);
3993 if Nkind (Lhs) = N_Type_Conversion then
3994 Lhs := Expression (Lhs);
3995 Rhs := Convert_To (Etype (Lhs), Rhs);
3996 end if;
3998 Set_Assignment_OK (Lhs);
4000 Rewrite (N,
4001 Make_Assignment_Statement (Loc,
4002 Name => Lhs,
4003 Expression => Rhs));
4005 Analyze (N);
4006 return;
4007 end;
4009 -- Array type case
4011 elsif Is_Array_Type (U_Type) then
4012 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
4013 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
4015 -- Tagged type case, use the primitive Read function. Note that
4016 -- this will dispatch in the class-wide case which is what we want
4018 elsif Is_Tagged_Type (U_Type) then
4019 Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
4021 -- All other record type cases, including protected records. The
4022 -- latter only arise for expander generated code for handling
4023 -- shared passive partition access.
4025 else
4026 pragma Assert
4027 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
4029 -- Ada 2005 (AI-216): Program_Error is raised when executing
4030 -- the default implementation of the Read attribute of an
4031 -- Unchecked_Union type.
4033 if Is_Unchecked_Union (Base_Type (U_Type)) then
4034 Insert_Action (N,
4035 Make_Raise_Program_Error (Loc,
4036 Reason => PE_Unchecked_Union_Restriction));
4037 end if;
4039 if Has_Discriminants (U_Type)
4040 and then Present
4041 (Discriminant_Default_Value (First_Discriminant (U_Type)))
4042 then
4043 Build_Mutable_Record_Read_Procedure
4044 (Loc, Full_Base (U_Type), Decl, Pname);
4045 else
4046 Build_Record_Read_Procedure
4047 (Loc, Full_Base (U_Type), Decl, Pname);
4048 end if;
4050 -- Suppress checks, uninitialized or otherwise invalid
4051 -- data does not cause constraint errors to be raised for
4052 -- a complete record read.
4054 Insert_Action (N, Decl, All_Checks);
4055 end if;
4056 end if;
4058 Rewrite_Stream_Proc_Call (Pname);
4059 end Read;
4061 ---------
4062 -- Ref --
4063 ---------
4065 -- Ref is identical to To_Address, see To_Address for processing
4067 ---------------
4068 -- Remainder --
4069 ---------------
4071 -- Transforms 'Remainder into a call to the floating-point attribute
4072 -- function Remainder in Fat_xxx (where xxx is the root type)
4074 when Attribute_Remainder =>
4075 Expand_Fpt_Attribute_RR (N);
4077 ------------
4078 -- Result --
4079 ------------
4081 -- Transform 'Result into reference to _Result formal. At the point
4082 -- where a legal 'Result attribute is expanded, we know that we are in
4083 -- the context of a _Postcondition function with a _Result parameter.
4085 when Attribute_Result =>
4086 Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult));
4087 Analyze_And_Resolve (N, Typ);
4089 -----------
4090 -- Round --
4091 -----------
4093 -- The handling of the Round attribute is quite delicate. The processing
4094 -- in Sem_Attr introduced a conversion to universal real, reflecting the
4095 -- semantics of Round, but we do not want anything to do with universal
4096 -- real at runtime, since this corresponds to using floating-point
4097 -- arithmetic.
4099 -- What we have now is that the Etype of the Round attribute correctly
4100 -- indicates the final result type. The operand of the Round is the
4101 -- conversion to universal real, described above, and the operand of
4102 -- this conversion is the actual operand of Round, which may be the
4103 -- special case of a fixed point multiplication or division (Etype =
4104 -- universal fixed)
4106 -- The exapander will expand first the operand of the conversion, then
4107 -- the conversion, and finally the round attribute itself, since we
4108 -- always work inside out. But we cannot simply process naively in this
4109 -- order. In the semantic world where universal fixed and real really
4110 -- exist and have infinite precision, there is no problem, but in the
4111 -- implementation world, where universal real is a floating-point type,
4112 -- we would get the wrong result.
4114 -- So the approach is as follows. First, when expanding a multiply or
4115 -- divide whose type is universal fixed, we do nothing at all, instead
4116 -- deferring the operation till later.
4118 -- The actual processing is done in Expand_N_Type_Conversion which
4119 -- handles the special case of Round by looking at its parent to see if
4120 -- it is a Round attribute, and if it is, handling the conversion (or
4121 -- its fixed multiply/divide child) in an appropriate manner.
4123 -- This means that by the time we get to expanding the Round attribute
4124 -- itself, the Round is nothing more than a type conversion (and will
4125 -- often be a null type conversion), so we just replace it with the
4126 -- appropriate conversion operation.
4128 when Attribute_Round =>
4129 Rewrite (N,
4130 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
4131 Analyze_And_Resolve (N);
4133 --------------
4134 -- Rounding --
4135 --------------
4137 -- Transforms 'Rounding into a call to the floating-point attribute
4138 -- function Rounding in Fat_xxx (where xxx is the root type)
4140 when Attribute_Rounding =>
4141 Expand_Fpt_Attribute_R (N);
4143 ------------------
4144 -- Same_Storage --
4145 ------------------
4147 when Attribute_Same_Storage => Same_Storage : declare
4148 Loc : constant Source_Ptr := Sloc (N);
4150 X : constant Node_Id := Prefix (N);
4151 Y : constant Node_Id := First (Expressions (N));
4152 -- The arguments
4154 X_Addr, Y_Addr : Node_Id;
4155 -- Rhe expressions for their addresses
4157 X_Size, Y_Size : Node_Id;
4158 -- Rhe expressions for their sizes
4160 begin
4161 -- The attribute is expanded as:
4163 -- (X'address = Y'address)
4164 -- and then (X'Size = Y'Size)
4166 -- If both arguments have the same Etype the second conjunct can be
4167 -- omitted.
4169 X_Addr :=
4170 Make_Attribute_Reference (Loc,
4171 Attribute_Name => Name_Address,
4172 Prefix => New_Copy_Tree (X));
4174 Y_Addr :=
4175 Make_Attribute_Reference (Loc,
4176 Attribute_Name => Name_Address,
4177 Prefix => New_Copy_Tree (Y));
4179 X_Size :=
4180 Make_Attribute_Reference (Loc,
4181 Attribute_Name => Name_Size,
4182 Prefix => New_Copy_Tree (X));
4184 Y_Size :=
4185 Make_Attribute_Reference (Loc,
4186 Attribute_Name => Name_Size,
4187 Prefix => New_Copy_Tree (Y));
4189 if Etype (X) = Etype (Y) then
4190 Rewrite (N,
4191 (Make_Op_Eq (Loc,
4192 Left_Opnd => X_Addr,
4193 Right_Opnd => Y_Addr)));
4194 else
4195 Rewrite (N,
4196 Make_Op_And (Loc,
4197 Left_Opnd =>
4198 Make_Op_Eq (Loc,
4199 Left_Opnd => X_Addr,
4200 Right_Opnd => Y_Addr),
4201 Right_Opnd =>
4202 Make_Op_Eq (Loc,
4203 Left_Opnd => X_Size,
4204 Right_Opnd => Y_Size)));
4205 end if;
4207 Analyze_And_Resolve (N, Standard_Boolean);
4208 end Same_Storage;
4210 -------------
4211 -- Scaling --
4212 -------------
4214 -- Transforms 'Scaling into a call to the floating-point attribute
4215 -- function Scaling in Fat_xxx (where xxx is the root type)
4217 when Attribute_Scaling =>
4218 Expand_Fpt_Attribute_RI (N);
4220 ----------
4221 -- Size --
4222 ----------
4224 when Attribute_Size |
4225 Attribute_Object_Size |
4226 Attribute_Value_Size |
4227 Attribute_VADS_Size => Size :
4229 declare
4230 Siz : Uint;
4231 New_Node : Node_Id;
4233 begin
4234 -- Processing for VADS_Size case. Note that this processing removes
4235 -- all traces of VADS_Size from the tree, and completes all required
4236 -- processing for VADS_Size by translating the attribute reference
4237 -- to an appropriate Size or Object_Size reference.
4239 if Id = Attribute_VADS_Size
4240 or else (Use_VADS_Size and then Id = Attribute_Size)
4241 then
4242 -- If the size is specified, then we simply use the specified
4243 -- size. This applies to both types and objects. The size of an
4244 -- object can be specified in the following ways:
4246 -- An explicit size object is given for an object
4247 -- A component size is specified for an indexed component
4248 -- A component clause is specified for a selected component
4249 -- The object is a component of a packed composite object
4251 -- If the size is specified, then VADS_Size of an object
4253 if (Is_Entity_Name (Pref)
4254 and then Present (Size_Clause (Entity (Pref))))
4255 or else
4256 (Nkind (Pref) = N_Component_Clause
4257 and then (Present (Component_Clause
4258 (Entity (Selector_Name (Pref))))
4259 or else Is_Packed (Etype (Prefix (Pref)))))
4260 or else
4261 (Nkind (Pref) = N_Indexed_Component
4262 and then (Component_Size (Etype (Prefix (Pref))) /= 0
4263 or else Is_Packed (Etype (Prefix (Pref)))))
4264 then
4265 Set_Attribute_Name (N, Name_Size);
4267 -- Otherwise if we have an object rather than a type, then the
4268 -- VADS_Size attribute applies to the type of the object, rather
4269 -- than the object itself. This is one of the respects in which
4270 -- VADS_Size differs from Size.
4272 else
4273 if (not Is_Entity_Name (Pref)
4274 or else not Is_Type (Entity (Pref)))
4275 and then (Is_Scalar_Type (Ptyp) or else Is_Constrained (Ptyp))
4276 then
4277 Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc));
4278 end if;
4280 -- For a scalar type for which no size was explicitly given,
4281 -- VADS_Size means Object_Size. This is the other respect in
4282 -- which VADS_Size differs from Size.
4284 if Is_Scalar_Type (Ptyp) and then No (Size_Clause (Ptyp)) then
4285 Set_Attribute_Name (N, Name_Object_Size);
4287 -- In all other cases, Size and VADS_Size are the sane
4289 else
4290 Set_Attribute_Name (N, Name_Size);
4291 end if;
4292 end if;
4293 end if;
4295 -- For class-wide types, X'Class'Size is transformed into a direct
4296 -- reference to the Size of the class type, so that the back end does
4297 -- not have to deal with the X'Class'Size reference.
4299 if Is_Entity_Name (Pref)
4300 and then Is_Class_Wide_Type (Entity (Pref))
4301 then
4302 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
4303 return;
4305 -- For X'Size applied to an object of a class-wide type, transform
4306 -- X'Size into a call to the primitive operation _Size applied to X.
4308 elsif Is_Class_Wide_Type (Ptyp)
4309 or else (Id = Attribute_Size
4310 and then Is_Tagged_Type (Ptyp)
4311 and then Has_Unknown_Discriminants (Ptyp))
4312 then
4313 -- No need to do anything else compiling under restriction
4314 -- No_Dispatching_Calls. During the semantic analysis we
4315 -- already notified such violation.
4317 if Restriction_Active (No_Dispatching_Calls) then
4318 return;
4319 end if;
4321 New_Node :=
4322 Make_Function_Call (Loc,
4323 Name => New_Reference_To
4324 (Find_Prim_Op (Ptyp, Name_uSize), Loc),
4325 Parameter_Associations => New_List (Pref));
4327 if Typ /= Standard_Long_Long_Integer then
4329 -- The context is a specific integer type with which the
4330 -- original attribute was compatible. The function has a
4331 -- specific type as well, so to preserve the compatibility
4332 -- we must convert explicitly.
4334 New_Node := Convert_To (Typ, New_Node);
4335 end if;
4337 Rewrite (N, New_Node);
4338 Analyze_And_Resolve (N, Typ);
4339 return;
4341 -- Case of known RM_Size of a type
4343 elsif (Id = Attribute_Size or else Id = Attribute_Value_Size)
4344 and then Is_Entity_Name (Pref)
4345 and then Is_Type (Entity (Pref))
4346 and then Known_Static_RM_Size (Entity (Pref))
4347 then
4348 Siz := RM_Size (Entity (Pref));
4350 -- Case of known Esize of a type
4352 elsif Id = Attribute_Object_Size
4353 and then Is_Entity_Name (Pref)
4354 and then Is_Type (Entity (Pref))
4355 and then Known_Static_Esize (Entity (Pref))
4356 then
4357 Siz := Esize (Entity (Pref));
4359 -- Case of known size of object
4361 elsif Id = Attribute_Size
4362 and then Is_Entity_Name (Pref)
4363 and then Is_Object (Entity (Pref))
4364 and then Known_Esize (Entity (Pref))
4365 and then Known_Static_Esize (Entity (Pref))
4366 then
4367 Siz := Esize (Entity (Pref));
4369 -- For an array component, we can do Size in the front end
4370 -- if the component_size of the array is set.
4372 elsif Nkind (Pref) = N_Indexed_Component then
4373 Siz := Component_Size (Etype (Prefix (Pref)));
4375 -- For a record component, we can do Size in the front end if there
4376 -- is a component clause, or if the record is packed and the
4377 -- component's size is known at compile time.
4379 elsif Nkind (Pref) = N_Selected_Component then
4380 declare
4381 Rec : constant Entity_Id := Etype (Prefix (Pref));
4382 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
4384 begin
4385 if Present (Component_Clause (Comp)) then
4386 Siz := Esize (Comp);
4388 elsif Is_Packed (Rec) then
4389 Siz := RM_Size (Ptyp);
4391 else
4392 Apply_Universal_Integer_Attribute_Checks (N);
4393 return;
4394 end if;
4395 end;
4397 -- All other cases are handled by the back end
4399 else
4400 Apply_Universal_Integer_Attribute_Checks (N);
4402 -- If Size is applied to a formal parameter that is of a packed
4403 -- array subtype, then apply Size to the actual subtype.
4405 if Is_Entity_Name (Pref)
4406 and then Is_Formal (Entity (Pref))
4407 and then Is_Array_Type (Ptyp)
4408 and then Is_Packed (Ptyp)
4409 then
4410 Rewrite (N,
4411 Make_Attribute_Reference (Loc,
4412 Prefix =>
4413 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
4414 Attribute_Name => Name_Size));
4415 Analyze_And_Resolve (N, Typ);
4416 end if;
4418 -- If Size applies to a dereference of an access to unconstrained
4419 -- packed array, the back end needs to see its unconstrained
4420 -- nominal type, but also a hint to the actual constrained type.
4422 if Nkind (Pref) = N_Explicit_Dereference
4423 and then Is_Array_Type (Ptyp)
4424 and then not Is_Constrained (Ptyp)
4425 and then Is_Packed (Ptyp)
4426 then
4427 Set_Actual_Designated_Subtype (Pref,
4428 Get_Actual_Subtype (Pref));
4429 end if;
4431 return;
4432 end if;
4434 -- Common processing for record and array component case
4436 if Siz /= No_Uint and then Siz /= 0 then
4437 declare
4438 CS : constant Boolean := Comes_From_Source (N);
4440 begin
4441 Rewrite (N, Make_Integer_Literal (Loc, Siz));
4443 -- This integer literal is not a static expression. We do not
4444 -- call Analyze_And_Resolve here, because this would activate
4445 -- the circuit for deciding that a static value was out of
4446 -- range, and we don't want that.
4448 -- So just manually set the type, mark the expression as non-
4449 -- static, and then ensure that the result is checked properly
4450 -- if the attribute comes from source (if it was internally
4451 -- generated, we never need a constraint check).
4453 Set_Etype (N, Typ);
4454 Set_Is_Static_Expression (N, False);
4456 if CS then
4457 Apply_Constraint_Check (N, Typ);
4458 end if;
4459 end;
4460 end if;
4461 end Size;
4463 ------------------
4464 -- Storage_Pool --
4465 ------------------
4467 when Attribute_Storage_Pool =>
4468 Rewrite (N,
4469 Make_Type_Conversion (Loc,
4470 Subtype_Mark => New_Reference_To (Etype (N), Loc),
4471 Expression => New_Reference_To (Entity (N), Loc)));
4472 Analyze_And_Resolve (N, Typ);
4474 ------------------
4475 -- Storage_Size --
4476 ------------------
4478 when Attribute_Storage_Size => Storage_Size : begin
4480 -- Access type case, always go to the root type
4482 -- The case of access types results in a value of zero for the case
4483 -- where no storage size attribute clause has been given. If a
4484 -- storage size has been given, then the attribute is converted
4485 -- to a reference to the variable used to hold this value.
4487 if Is_Access_Type (Ptyp) then
4488 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
4489 Rewrite (N,
4490 Make_Attribute_Reference (Loc,
4491 Prefix => New_Reference_To (Typ, Loc),
4492 Attribute_Name => Name_Max,
4493 Expressions => New_List (
4494 Make_Integer_Literal (Loc, 0),
4495 Convert_To (Typ,
4496 New_Reference_To
4497 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
4499 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
4500 Rewrite (N,
4501 OK_Convert_To (Typ,
4502 Make_Function_Call (Loc,
4503 Name =>
4504 New_Reference_To
4505 (Find_Prim_Op
4506 (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
4507 Attribute_Name (N)),
4508 Loc),
4510 Parameter_Associations => New_List (
4511 New_Reference_To
4512 (Associated_Storage_Pool (Root_Type (Ptyp)), Loc)))));
4514 else
4515 Rewrite (N, Make_Integer_Literal (Loc, 0));
4516 end if;
4518 Analyze_And_Resolve (N, Typ);
4520 -- For tasks, we retrieve the size directly from the TCB. The
4521 -- size may depend on a discriminant of the type, and therefore
4522 -- can be a per-object expression, so type-level information is
4523 -- not sufficient in general. There are four cases to consider:
4525 -- a) If the attribute appears within a task body, the designated
4526 -- TCB is obtained by a call to Self.
4528 -- b) If the prefix of the attribute is the name of a task object,
4529 -- the designated TCB is the one stored in the corresponding record.
4531 -- c) If the prefix is a task type, the size is obtained from the
4532 -- size variable created for each task type
4534 -- d) If no storage_size was specified for the type , there is no
4535 -- size variable, and the value is a system-specific default.
4537 else
4538 if In_Open_Scopes (Ptyp) then
4540 -- Storage_Size (Self)
4542 Rewrite (N,
4543 Convert_To (Typ,
4544 Make_Function_Call (Loc,
4545 Name =>
4546 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4547 Parameter_Associations =>
4548 New_List (
4549 Make_Function_Call (Loc,
4550 Name =>
4551 New_Reference_To (RTE (RE_Self), Loc))))));
4553 elsif not Is_Entity_Name (Pref)
4554 or else not Is_Type (Entity (Pref))
4555 then
4556 -- Storage_Size (Rec (Obj).Size)
4558 Rewrite (N,
4559 Convert_To (Typ,
4560 Make_Function_Call (Loc,
4561 Name =>
4562 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4563 Parameter_Associations =>
4564 New_List (
4565 Make_Selected_Component (Loc,
4566 Prefix =>
4567 Unchecked_Convert_To (
4568 Corresponding_Record_Type (Ptyp),
4569 New_Copy_Tree (Pref)),
4570 Selector_Name =>
4571 Make_Identifier (Loc, Name_uTask_Id))))));
4573 elsif Present (Storage_Size_Variable (Ptyp)) then
4575 -- Static storage size pragma given for type: retrieve value
4576 -- from its allocated storage variable.
4578 Rewrite (N,
4579 Convert_To (Typ,
4580 Make_Function_Call (Loc,
4581 Name => New_Occurrence_Of (
4582 RTE (RE_Adjust_Storage_Size), Loc),
4583 Parameter_Associations =>
4584 New_List (
4585 New_Reference_To (
4586 Storage_Size_Variable (Ptyp), Loc)))));
4587 else
4588 -- Get system default
4590 Rewrite (N,
4591 Convert_To (Typ,
4592 Make_Function_Call (Loc,
4593 Name =>
4594 New_Occurrence_Of (
4595 RTE (RE_Default_Stack_Size), Loc))));
4596 end if;
4598 Analyze_And_Resolve (N, Typ);
4599 end if;
4600 end Storage_Size;
4602 -----------------
4603 -- Stream_Size --
4604 -----------------
4606 when Attribute_Stream_Size =>
4607 Rewrite (N,
4608 Make_Integer_Literal (Loc, Intval => Get_Stream_Size (Ptyp)));
4609 Analyze_And_Resolve (N, Typ);
4611 ----------
4612 -- Succ --
4613 ----------
4615 -- 1. Deal with enumeration types with holes
4616 -- 2. For floating-point, generate call to attribute function
4617 -- 3. For other cases, deal with constraint checking
4619 when Attribute_Succ => Succ : declare
4620 Etyp : constant Entity_Id := Base_Type (Ptyp);
4622 begin
4624 -- For enumeration types with non-standard representations, we
4625 -- expand typ'Succ (x) into
4627 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
4629 -- If the representation is contiguous, we compute instead
4630 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
4632 if Is_Enumeration_Type (Ptyp)
4633 and then Present (Enum_Pos_To_Rep (Etyp))
4634 then
4635 if Has_Contiguous_Rep (Etyp) then
4636 Rewrite (N,
4637 Unchecked_Convert_To (Ptyp,
4638 Make_Op_Add (Loc,
4639 Left_Opnd =>
4640 Make_Integer_Literal (Loc,
4641 Enumeration_Rep (First_Literal (Ptyp))),
4642 Right_Opnd =>
4643 Make_Function_Call (Loc,
4644 Name =>
4645 New_Reference_To
4646 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4648 Parameter_Associations =>
4649 New_List (
4650 Unchecked_Convert_To (Ptyp,
4651 Make_Op_Add (Loc,
4652 Left_Opnd =>
4653 Unchecked_Convert_To (Standard_Integer,
4654 Relocate_Node (First (Exprs))),
4655 Right_Opnd =>
4656 Make_Integer_Literal (Loc, 1))),
4657 Rep_To_Pos_Flag (Ptyp, Loc))))));
4658 else
4659 -- Add Boolean parameter True, to request program errror if
4660 -- we have a bad representation on our hands. Add False if
4661 -- checks are suppressed.
4663 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
4664 Rewrite (N,
4665 Make_Indexed_Component (Loc,
4666 Prefix =>
4667 New_Reference_To
4668 (Enum_Pos_To_Rep (Etyp), Loc),
4669 Expressions => New_List (
4670 Make_Op_Add (Loc,
4671 Left_Opnd =>
4672 Make_Function_Call (Loc,
4673 Name =>
4674 New_Reference_To
4675 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4676 Parameter_Associations => Exprs),
4677 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
4678 end if;
4680 Analyze_And_Resolve (N, Typ);
4682 -- For floating-point, we transform 'Succ into a call to the Succ
4683 -- floating-point attribute function in Fat_xxx (xxx is root type)
4685 elsif Is_Floating_Point_Type (Ptyp) then
4686 Expand_Fpt_Attribute_R (N);
4687 Analyze_And_Resolve (N, Typ);
4689 -- For modular types, nothing to do (no overflow, since wraps)
4691 elsif Is_Modular_Integer_Type (Ptyp) then
4692 null;
4694 -- For other types, if argument is marked as needing a range check or
4695 -- overflow checking is enabled, we must generate a check.
4697 elsif not Overflow_Checks_Suppressed (Ptyp)
4698 or else Do_Range_Check (First (Exprs))
4699 then
4700 Set_Do_Range_Check (First (Exprs), False);
4701 Expand_Pred_Succ (N);
4702 end if;
4703 end Succ;
4705 ---------
4706 -- Tag --
4707 ---------
4709 -- Transforms X'Tag into a direct reference to the tag of X
4711 when Attribute_Tag => Tag : declare
4712 Ttyp : Entity_Id;
4713 Prefix_Is_Type : Boolean;
4715 begin
4716 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
4717 Ttyp := Entity (Pref);
4718 Prefix_Is_Type := True;
4719 else
4720 Ttyp := Ptyp;
4721 Prefix_Is_Type := False;
4722 end if;
4724 if Is_Class_Wide_Type (Ttyp) then
4725 Ttyp := Root_Type (Ttyp);
4726 end if;
4728 Ttyp := Underlying_Type (Ttyp);
4730 -- Ada 2005: The type may be a synchronized tagged type, in which
4731 -- case the tag information is stored in the corresponding record.
4733 if Is_Concurrent_Type (Ttyp) then
4734 Ttyp := Corresponding_Record_Type (Ttyp);
4735 end if;
4737 if Prefix_Is_Type then
4739 -- For VMs we leave the type attribute unexpanded because
4740 -- there's not a dispatching table to reference.
4742 if Tagged_Type_Expansion then
4743 Rewrite (N,
4744 Unchecked_Convert_To (RTE (RE_Tag),
4745 New_Reference_To
4746 (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc)));
4747 Analyze_And_Resolve (N, RTE (RE_Tag));
4748 end if;
4750 -- Ada 2005 (AI-251): The use of 'Tag in the sources always
4751 -- references the primary tag of the actual object. If 'Tag is
4752 -- applied to class-wide interface objects we generate code that
4753 -- displaces "this" to reference the base of the object.
4755 elsif Comes_From_Source (N)
4756 and then Is_Class_Wide_Type (Etype (Prefix (N)))
4757 and then Is_Interface (Etype (Prefix (N)))
4758 then
4759 -- Generate:
4760 -- (To_Tag_Ptr (Prefix'Address)).all
4762 -- Note that Prefix'Address is recursively expanded into a call
4763 -- to Base_Address (Obj.Tag)
4765 -- Not needed for VM targets, since all handled by the VM
4767 if Tagged_Type_Expansion then
4768 Rewrite (N,
4769 Make_Explicit_Dereference (Loc,
4770 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4771 Make_Attribute_Reference (Loc,
4772 Prefix => Relocate_Node (Pref),
4773 Attribute_Name => Name_Address))));
4774 Analyze_And_Resolve (N, RTE (RE_Tag));
4775 end if;
4777 else
4778 Rewrite (N,
4779 Make_Selected_Component (Loc,
4780 Prefix => Relocate_Node (Pref),
4781 Selector_Name =>
4782 New_Reference_To (First_Tag_Component (Ttyp), Loc)));
4783 Analyze_And_Resolve (N, RTE (RE_Tag));
4784 end if;
4785 end Tag;
4787 ----------------
4788 -- Terminated --
4789 ----------------
4791 -- Transforms 'Terminated attribute into a call to Terminated function
4793 when Attribute_Terminated => Terminated :
4794 begin
4795 -- The prefix of Terminated is of a task interface class-wide type.
4796 -- Generate:
4797 -- terminated (Task_Id (Pref._disp_get_task_id));
4799 if Ada_Version >= Ada_2005
4800 and then Ekind (Ptyp) = E_Class_Wide_Type
4801 and then Is_Interface (Ptyp)
4802 and then Is_Task_Interface (Ptyp)
4803 then
4804 Rewrite (N,
4805 Make_Function_Call (Loc,
4806 Name =>
4807 New_Reference_To (RTE (RE_Terminated), Loc),
4808 Parameter_Associations => New_List (
4809 Make_Unchecked_Type_Conversion (Loc,
4810 Subtype_Mark =>
4811 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
4812 Expression =>
4813 Make_Selected_Component (Loc,
4814 Prefix =>
4815 New_Copy_Tree (Pref),
4816 Selector_Name =>
4817 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
4819 elsif Restricted_Profile then
4820 Rewrite (N,
4821 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
4823 else
4824 Rewrite (N,
4825 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
4826 end if;
4828 Analyze_And_Resolve (N, Standard_Boolean);
4829 end Terminated;
4831 ----------------
4832 -- To_Address --
4833 ----------------
4835 -- Transforms System'To_Address (X) and System.Address'Ref (X) into
4836 -- unchecked conversion from (integral) type of X to type address.
4838 when Attribute_To_Address | Attribute_Ref =>
4839 Rewrite (N,
4840 Unchecked_Convert_To (RTE (RE_Address),
4841 Relocate_Node (First (Exprs))));
4842 Analyze_And_Resolve (N, RTE (RE_Address));
4844 ------------
4845 -- To_Any --
4846 ------------
4848 when Attribute_To_Any => To_Any : declare
4849 P_Type : constant Entity_Id := Etype (Pref);
4850 Decls : constant List_Id := New_List;
4851 begin
4852 Rewrite (N,
4853 Build_To_Any_Call
4854 (Convert_To (P_Type,
4855 Relocate_Node (First (Exprs))), Decls));
4856 Insert_Actions (N, Decls);
4857 Analyze_And_Resolve (N, RTE (RE_Any));
4858 end To_Any;
4860 ----------------
4861 -- Truncation --
4862 ----------------
4864 -- Transforms 'Truncation into a call to the floating-point attribute
4865 -- function Truncation in Fat_xxx (where xxx is the root type).
4866 -- Expansion is avoided for cases the back end can handle directly.
4868 when Attribute_Truncation =>
4869 if not Is_Inline_Floating_Point_Attribute (N) then
4870 Expand_Fpt_Attribute_R (N);
4871 end if;
4873 --------------
4874 -- TypeCode --
4875 --------------
4877 when Attribute_TypeCode => TypeCode : declare
4878 P_Type : constant Entity_Id := Etype (Pref);
4879 Decls : constant List_Id := New_List;
4880 begin
4881 Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls));
4882 Insert_Actions (N, Decls);
4883 Analyze_And_Resolve (N, RTE (RE_TypeCode));
4884 end TypeCode;
4886 -----------------------
4887 -- Unbiased_Rounding --
4888 -----------------------
4890 -- Transforms 'Unbiased_Rounding into a call to the floating-point
4891 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
4892 -- root type). Expansion is avoided for cases the back end can handle
4893 -- directly.
4895 when Attribute_Unbiased_Rounding =>
4896 if not Is_Inline_Floating_Point_Attribute (N) then
4897 Expand_Fpt_Attribute_R (N);
4898 end if;
4900 -----------------
4901 -- UET_Address --
4902 -----------------
4904 when Attribute_UET_Address => UET_Address : declare
4905 Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
4907 begin
4908 Insert_Action (N,
4909 Make_Object_Declaration (Loc,
4910 Defining_Identifier => Ent,
4911 Aliased_Present => True,
4912 Object_Definition =>
4913 New_Occurrence_Of (RTE (RE_Address), Loc)));
4915 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
4916 -- in normal external form.
4918 Get_External_Unit_Name_String (Get_Unit_Name (Pref));
4919 Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
4920 Name_Len := Name_Len + 7;
4921 Name_Buffer (1 .. 7) := "__gnat_";
4922 Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP";
4923 Name_Len := Name_Len + 5;
4925 Set_Is_Imported (Ent);
4926 Set_Interface_Name (Ent,
4927 Make_String_Literal (Loc,
4928 Strval => String_From_Name_Buffer));
4930 -- Set entity as internal to ensure proper Sprint output of its
4931 -- implicit importation.
4933 Set_Is_Internal (Ent);
4935 Rewrite (N,
4936 Make_Attribute_Reference (Loc,
4937 Prefix => New_Occurrence_Of (Ent, Loc),
4938 Attribute_Name => Name_Address));
4940 Analyze_And_Resolve (N, Typ);
4941 end UET_Address;
4943 ---------------
4944 -- VADS_Size --
4945 ---------------
4947 -- The processing for VADS_Size is shared with Size
4949 ---------
4950 -- Val --
4951 ---------
4953 -- For enumeration types with a standard representation, and for all
4954 -- other types, Val is handled by the back end. For enumeration types
4955 -- with a non-standard representation we use the _Pos_To_Rep array that
4956 -- was created when the type was frozen.
4958 when Attribute_Val => Val : declare
4959 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
4961 begin
4962 if Is_Enumeration_Type (Etyp)
4963 and then Present (Enum_Pos_To_Rep (Etyp))
4964 then
4965 if Has_Contiguous_Rep (Etyp) then
4966 declare
4967 Rep_Node : constant Node_Id :=
4968 Unchecked_Convert_To (Etyp,
4969 Make_Op_Add (Loc,
4970 Left_Opnd =>
4971 Make_Integer_Literal (Loc,
4972 Enumeration_Rep (First_Literal (Etyp))),
4973 Right_Opnd =>
4974 (Convert_To (Standard_Integer,
4975 Relocate_Node (First (Exprs))))));
4977 begin
4978 Rewrite (N,
4979 Unchecked_Convert_To (Etyp,
4980 Make_Op_Add (Loc,
4981 Left_Opnd =>
4982 Make_Integer_Literal (Loc,
4983 Enumeration_Rep (First_Literal (Etyp))),
4984 Right_Opnd =>
4985 Make_Function_Call (Loc,
4986 Name =>
4987 New_Reference_To
4988 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4989 Parameter_Associations => New_List (
4990 Rep_Node,
4991 Rep_To_Pos_Flag (Etyp, Loc))))));
4992 end;
4994 else
4995 Rewrite (N,
4996 Make_Indexed_Component (Loc,
4997 Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc),
4998 Expressions => New_List (
4999 Convert_To (Standard_Integer,
5000 Relocate_Node (First (Exprs))))));
5001 end if;
5003 Analyze_And_Resolve (N, Typ);
5005 -- If the argument is marked as requiring a range check then generate
5006 -- it here.
5008 elsif Do_Range_Check (First (Exprs)) then
5009 Set_Do_Range_Check (First (Exprs), False);
5010 Generate_Range_Check (First (Exprs), Etyp, CE_Range_Check_Failed);
5011 end if;
5012 end Val;
5014 -----------
5015 -- Valid --
5016 -----------
5018 -- The code for valid is dependent on the particular types involved.
5019 -- See separate sections below for the generated code in each case.
5021 when Attribute_Valid => Valid : declare
5022 Btyp : Entity_Id := Base_Type (Ptyp);
5023 Tst : Node_Id;
5025 Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
5026 -- Save the validity checking mode. We always turn off validity
5027 -- checking during process of 'Valid since this is one place
5028 -- where we do not want the implicit validity checks to intefere
5029 -- with the explicit validity check that the programmer is doing.
5031 function Make_Range_Test return Node_Id;
5032 -- Build the code for a range test of the form
5033 -- Btyp!(Pref) in Btyp!(Ptyp'First) .. Btyp!(Ptyp'Last)
5035 ---------------------
5036 -- Make_Range_Test --
5037 ---------------------
5039 function Make_Range_Test return Node_Id is
5040 Temp : constant Node_Id := Duplicate_Subexpr (Pref);
5042 begin
5043 -- The value whose validity is being checked has been captured in
5044 -- an object declaration. We certainly don't want this object to
5045 -- appear valid because the declaration initializes it!
5047 if Is_Entity_Name (Temp) then
5048 Set_Is_Known_Valid (Entity (Temp), False);
5049 end if;
5051 return
5052 Make_In (Loc,
5053 Left_Opnd =>
5054 Unchecked_Convert_To (Btyp, Temp),
5055 Right_Opnd =>
5056 Make_Range (Loc,
5057 Low_Bound =>
5058 Unchecked_Convert_To (Btyp,
5059 Make_Attribute_Reference (Loc,
5060 Prefix => New_Occurrence_Of (Ptyp, Loc),
5061 Attribute_Name => Name_First)),
5062 High_Bound =>
5063 Unchecked_Convert_To (Btyp,
5064 Make_Attribute_Reference (Loc,
5065 Prefix => New_Occurrence_Of (Ptyp, Loc),
5066 Attribute_Name => Name_Last))));
5067 end Make_Range_Test;
5069 -- Start of processing for Attribute_Valid
5071 begin
5072 -- Do not expand sourced code 'Valid reference in CodePeer mode,
5073 -- will be handled by the back-end directly.
5075 if CodePeer_Mode and then Comes_From_Source (N) then
5076 return;
5077 end if;
5079 -- Turn off validity checks. We do not want any implicit validity
5080 -- checks to intefere with the explicit check from the attribute
5082 Validity_Checks_On := False;
5084 -- Floating-point case. This case is handled by the Valid attribute
5085 -- code in the floating-point attribute run-time library.
5087 if Is_Floating_Point_Type (Ptyp) then
5088 declare
5089 Pkg : RE_Id;
5090 Ftp : Entity_Id;
5092 begin
5094 case Float_Rep (Btyp) is
5096 -- For vax fpt types, call appropriate routine in special
5097 -- vax floating point unit. No need to worry about loads in
5098 -- this case, since these types have no signalling NaN's.
5100 when VAX_Native => Expand_Vax_Valid (N);
5102 -- The AAMP back end handles Valid for floating-point types
5104 when AAMP =>
5105 Analyze_And_Resolve (Pref, Ptyp);
5106 Set_Etype (N, Standard_Boolean);
5107 Set_Analyzed (N);
5109 when IEEE_Binary =>
5110 Find_Fat_Info (Ptyp, Ftp, Pkg);
5112 -- If the floating-point object might be unaligned, we
5113 -- need to call the special routine Unaligned_Valid,
5114 -- which makes the needed copy, being careful not to
5115 -- load the value into any floating-point register.
5116 -- The argument in this case is obj'Address (see
5117 -- Unaligned_Valid routine in Fat_Gen).
5119 if Is_Possibly_Unaligned_Object (Pref) then
5120 Expand_Fpt_Attribute
5121 (N, Pkg, Name_Unaligned_Valid,
5122 New_List (
5123 Make_Attribute_Reference (Loc,
5124 Prefix => Relocate_Node (Pref),
5125 Attribute_Name => Name_Address)));
5127 -- In the normal case where we are sure the object is
5128 -- aligned, we generate a call to Valid, and the argument
5129 -- in this case is obj'Unrestricted_Access (after
5130 -- converting obj to the right floating-point type).
5132 else
5133 Expand_Fpt_Attribute
5134 (N, Pkg, Name_Valid,
5135 New_List (
5136 Make_Attribute_Reference (Loc,
5137 Prefix => Unchecked_Convert_To (Ftp, Pref),
5138 Attribute_Name => Name_Unrestricted_Access)));
5139 end if;
5140 end case;
5142 -- One more task, we still need a range check. Required
5143 -- only if we have a constraint, since the Valid routine
5144 -- catches infinities properly (infinities are never valid).
5146 -- The way we do the range check is simply to create the
5147 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
5149 if not Subtypes_Statically_Match (Ptyp, Btyp) then
5150 Rewrite (N,
5151 Make_And_Then (Loc,
5152 Left_Opnd => Relocate_Node (N),
5153 Right_Opnd =>
5154 Make_In (Loc,
5155 Left_Opnd => Convert_To (Btyp, Pref),
5156 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
5157 end if;
5158 end;
5160 -- Enumeration type with holes
5162 -- For enumeration types with holes, the Pos value constructed by
5163 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
5164 -- second argument of False returns minus one for an invalid value,
5165 -- and the non-negative pos value for a valid value, so the
5166 -- expansion of X'Valid is simply:
5168 -- type(X)'Pos (X) >= 0
5170 -- We can't quite generate it that way because of the requirement
5171 -- for the non-standard second argument of False in the resulting
5172 -- rep_to_pos call, so we have to explicitly create:
5174 -- _rep_to_pos (X, False) >= 0
5176 -- If we have an enumeration subtype, we also check that the
5177 -- value is in range:
5179 -- _rep_to_pos (X, False) >= 0
5180 -- and then
5181 -- (X >= type(X)'First and then type(X)'Last <= X)
5183 elsif Is_Enumeration_Type (Ptyp)
5184 and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp)))
5185 then
5186 Tst :=
5187 Make_Op_Ge (Loc,
5188 Left_Opnd =>
5189 Make_Function_Call (Loc,
5190 Name =>
5191 New_Reference_To
5192 (TSS (Base_Type (Ptyp), TSS_Rep_To_Pos), Loc),
5193 Parameter_Associations => New_List (
5194 Pref,
5195 New_Occurrence_Of (Standard_False, Loc))),
5196 Right_Opnd => Make_Integer_Literal (Loc, 0));
5198 if Ptyp /= Btyp
5199 and then
5200 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
5201 or else
5202 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
5203 then
5204 -- The call to Make_Range_Test will create declarations
5205 -- that need a proper insertion point, but Pref is now
5206 -- attached to a node with no ancestor. Attach to tree
5207 -- even if it is to be rewritten below.
5209 Set_Parent (Tst, Parent (N));
5211 Tst :=
5212 Make_And_Then (Loc,
5213 Left_Opnd => Make_Range_Test,
5214 Right_Opnd => Tst);
5215 end if;
5217 Rewrite (N, Tst);
5219 -- Fortran convention booleans
5221 -- For the very special case of Fortran convention booleans, the
5222 -- value is always valid, since it is an integer with the semantics
5223 -- that non-zero is true, and any value is permissible.
5225 elsif Is_Boolean_Type (Ptyp)
5226 and then Convention (Ptyp) = Convention_Fortran
5227 then
5228 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5230 -- For biased representations, we will be doing an unchecked
5231 -- conversion without unbiasing the result. That means that the range
5232 -- test has to take this into account, and the proper form of the
5233 -- test is:
5235 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
5237 elsif Has_Biased_Representation (Ptyp) then
5238 Btyp := RTE (RE_Unsigned_32);
5239 Rewrite (N,
5240 Make_Op_Lt (Loc,
5241 Left_Opnd =>
5242 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
5243 Right_Opnd =>
5244 Unchecked_Convert_To (Btyp,
5245 Make_Attribute_Reference (Loc,
5246 Prefix => New_Occurrence_Of (Ptyp, Loc),
5247 Attribute_Name => Name_Range_Length))));
5249 -- For all other scalar types, what we want logically is a
5250 -- range test:
5252 -- X in type(X)'First .. type(X)'Last
5254 -- But that's precisely what won't work because of possible
5255 -- unwanted optimization (and indeed the basic motivation for
5256 -- the Valid attribute is exactly that this test does not work!)
5257 -- What will work is:
5259 -- Btyp!(X) >= Btyp!(type(X)'First)
5260 -- and then
5261 -- Btyp!(X) <= Btyp!(type(X)'Last)
5263 -- where Btyp is an integer type large enough to cover the full
5264 -- range of possible stored values (i.e. it is chosen on the basis
5265 -- of the size of the type, not the range of the values). We write
5266 -- this as two tests, rather than a range check, so that static
5267 -- evaluation will easily remove either or both of the checks if
5268 -- they can be -statically determined to be true (this happens
5269 -- when the type of X is static and the range extends to the full
5270 -- range of stored values).
5272 -- Unsigned types. Note: it is safe to consider only whether the
5273 -- subtype is unsigned, since we will in that case be doing all
5274 -- unsigned comparisons based on the subtype range. Since we use the
5275 -- actual subtype object size, this is appropriate.
5277 -- For example, if we have
5279 -- subtype x is integer range 1 .. 200;
5280 -- for x'Object_Size use 8;
5282 -- Now the base type is signed, but objects of this type are bits
5283 -- unsigned, and doing an unsigned test of the range 1 to 200 is
5284 -- correct, even though a value greater than 127 looks signed to a
5285 -- signed comparison.
5287 elsif Is_Unsigned_Type (Ptyp) then
5288 if Esize (Ptyp) <= 32 then
5289 Btyp := RTE (RE_Unsigned_32);
5290 else
5291 Btyp := RTE (RE_Unsigned_64);
5292 end if;
5294 Rewrite (N, Make_Range_Test);
5296 -- Signed types
5298 else
5299 if Esize (Ptyp) <= Esize (Standard_Integer) then
5300 Btyp := Standard_Integer;
5301 else
5302 Btyp := Universal_Integer;
5303 end if;
5305 Rewrite (N, Make_Range_Test);
5306 end if;
5308 Analyze_And_Resolve (N, Standard_Boolean);
5309 Validity_Checks_On := Save_Validity_Checks_On;
5310 end Valid;
5312 -----------
5313 -- Value --
5314 -----------
5316 -- Value attribute is handled in separate unit Exp_Imgv
5318 when Attribute_Value =>
5319 Exp_Imgv.Expand_Value_Attribute (N);
5321 -----------------
5322 -- Value_Size --
5323 -----------------
5325 -- The processing for Value_Size shares the processing for Size
5327 -------------
5328 -- Version --
5329 -------------
5331 -- The processing for Version shares the processing for Body_Version
5333 ----------------
5334 -- Wide_Image --
5335 ----------------
5337 -- Wide_Image attribute is handled in separate unit Exp_Imgv
5339 when Attribute_Wide_Image =>
5340 Exp_Imgv.Expand_Wide_Image_Attribute (N);
5342 ---------------------
5343 -- Wide_Wide_Image --
5344 ---------------------
5346 -- Wide_Wide_Image attribute is handled in separate unit Exp_Imgv
5348 when Attribute_Wide_Wide_Image =>
5349 Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N);
5351 ----------------
5352 -- Wide_Value --
5353 ----------------
5355 -- We expand typ'Wide_Value (X) into
5357 -- typ'Value
5358 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
5360 -- Wide_String_To_String is a runtime function that converts its wide
5361 -- string argument to String, converting any non-translatable characters
5362 -- into appropriate escape sequences. This preserves the required
5363 -- semantics of Wide_Value in all cases, and results in a very simple
5364 -- implementation approach.
5366 -- Note: for this approach to be fully standard compliant for the cases
5367 -- where typ is Wide_Character and Wide_Wide_Character, the encoding
5368 -- method must cover the entire character range (e.g. UTF-8). But that
5369 -- is a reasonable requirement when dealing with encoded character
5370 -- sequences. Presumably if one of the restrictive encoding mechanisms
5371 -- is in use such as Shift-JIS, then characters that cannot be
5372 -- represented using this encoding will not appear in any case.
5374 when Attribute_Wide_Value => Wide_Value :
5375 begin
5376 Rewrite (N,
5377 Make_Attribute_Reference (Loc,
5378 Prefix => Pref,
5379 Attribute_Name => Name_Value,
5381 Expressions => New_List (
5382 Make_Function_Call (Loc,
5383 Name =>
5384 New_Reference_To (RTE (RE_Wide_String_To_String), Loc),
5386 Parameter_Associations => New_List (
5387 Relocate_Node (First (Exprs)),
5388 Make_Integer_Literal (Loc,
5389 Intval => Int (Wide_Character_Encoding_Method)))))));
5391 Analyze_And_Resolve (N, Typ);
5392 end Wide_Value;
5394 ---------------------
5395 -- Wide_Wide_Value --
5396 ---------------------
5398 -- We expand typ'Wide_Value_Value (X) into
5400 -- typ'Value
5401 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
5403 -- Wide_Wide_String_To_String is a runtime function that converts its
5404 -- wide string argument to String, converting any non-translatable
5405 -- characters into appropriate escape sequences. This preserves the
5406 -- required semantics of Wide_Wide_Value in all cases, and results in a
5407 -- very simple implementation approach.
5409 -- It's not quite right where typ = Wide_Wide_Character, because the
5410 -- encoding method may not cover the whole character type ???
5412 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
5413 begin
5414 Rewrite (N,
5415 Make_Attribute_Reference (Loc,
5416 Prefix => Pref,
5417 Attribute_Name => Name_Value,
5419 Expressions => New_List (
5420 Make_Function_Call (Loc,
5421 Name =>
5422 New_Reference_To (RTE (RE_Wide_Wide_String_To_String), Loc),
5424 Parameter_Associations => New_List (
5425 Relocate_Node (First (Exprs)),
5426 Make_Integer_Literal (Loc,
5427 Intval => Int (Wide_Character_Encoding_Method)))))));
5429 Analyze_And_Resolve (N, Typ);
5430 end Wide_Wide_Value;
5432 ---------------------
5433 -- Wide_Wide_Width --
5434 ---------------------
5436 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
5438 when Attribute_Wide_Wide_Width =>
5439 Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide);
5441 ----------------
5442 -- Wide_Width --
5443 ----------------
5445 -- Wide_Width attribute is handled in separate unit Exp_Imgv
5447 when Attribute_Wide_Width =>
5448 Exp_Imgv.Expand_Width_Attribute (N, Wide);
5450 -----------
5451 -- Width --
5452 -----------
5454 -- Width attribute is handled in separate unit Exp_Imgv
5456 when Attribute_Width =>
5457 Exp_Imgv.Expand_Width_Attribute (N, Normal);
5459 -----------
5460 -- Write --
5461 -----------
5463 when Attribute_Write => Write : declare
5464 P_Type : constant Entity_Id := Entity (Pref);
5465 U_Type : constant Entity_Id := Underlying_Type (P_Type);
5466 Pname : Entity_Id;
5467 Decl : Node_Id;
5468 Prag : Node_Id;
5469 Arg3 : Node_Id;
5470 Wfunc : Node_Id;
5472 begin
5473 -- If no underlying type, we have an error that will be diagnosed
5474 -- elsewhere, so here we just completely ignore the expansion.
5476 if No (U_Type) then
5477 return;
5478 end if;
5480 -- The simple case, if there is a TSS for Write, just call it
5482 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
5484 if Present (Pname) then
5485 null;
5487 else
5488 -- If there is a Stream_Convert pragma, use it, we rewrite
5490 -- sourcetyp'Output (stream, Item)
5492 -- as
5494 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
5496 -- where strmwrite is the given Write function that converts an
5497 -- argument of type sourcetyp or a type acctyp, from which it is
5498 -- derived to type strmtyp. The conversion to acttyp is required
5499 -- for the derived case.
5501 Prag := Get_Stream_Convert_Pragma (P_Type);
5503 if Present (Prag) then
5504 Arg3 :=
5505 Next (Next (First (Pragma_Argument_Associations (Prag))));
5506 Wfunc := Entity (Expression (Arg3));
5508 Rewrite (N,
5509 Make_Attribute_Reference (Loc,
5510 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
5511 Attribute_Name => Name_Output,
5512 Expressions => New_List (
5513 Relocate_Node (First (Exprs)),
5514 Make_Function_Call (Loc,
5515 Name => New_Occurrence_Of (Wfunc, Loc),
5516 Parameter_Associations => New_List (
5517 OK_Convert_To (Etype (First_Formal (Wfunc)),
5518 Relocate_Node (Next (First (Exprs)))))))));
5520 Analyze (N);
5521 return;
5523 -- For elementary types, we call the W_xxx routine directly
5525 elsif Is_Elementary_Type (U_Type) then
5526 Rewrite (N, Build_Elementary_Write_Call (N));
5527 Analyze (N);
5528 return;
5530 -- Array type case
5532 elsif Is_Array_Type (U_Type) then
5533 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
5534 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5536 -- Tagged type case, use the primitive Write function. Note that
5537 -- this will dispatch in the class-wide case which is what we want
5539 elsif Is_Tagged_Type (U_Type) then
5540 Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
5542 -- All other record type cases, including protected records.
5543 -- The latter only arise for expander generated code for
5544 -- handling shared passive partition access.
5546 else
5547 pragma Assert
5548 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5550 -- Ada 2005 (AI-216): Program_Error is raised when executing
5551 -- the default implementation of the Write attribute of an
5552 -- Unchecked_Union type. However, if the 'Write reference is
5553 -- within the generated Output stream procedure, Write outputs
5554 -- the components, and the default values of the discriminant
5555 -- are streamed by the Output procedure itself.
5557 if Is_Unchecked_Union (Base_Type (U_Type))
5558 and not Is_TSS (Current_Scope, TSS_Stream_Output)
5559 then
5560 Insert_Action (N,
5561 Make_Raise_Program_Error (Loc,
5562 Reason => PE_Unchecked_Union_Restriction));
5563 end if;
5565 if Has_Discriminants (U_Type)
5566 and then Present
5567 (Discriminant_Default_Value (First_Discriminant (U_Type)))
5568 then
5569 Build_Mutable_Record_Write_Procedure
5570 (Loc, Full_Base (U_Type), Decl, Pname);
5571 else
5572 Build_Record_Write_Procedure
5573 (Loc, Full_Base (U_Type), Decl, Pname);
5574 end if;
5576 Insert_Action (N, Decl);
5577 end if;
5578 end if;
5580 -- If we fall through, Pname is the procedure to be called
5582 Rewrite_Stream_Proc_Call (Pname);
5583 end Write;
5585 -- Component_Size is handled by the back end, unless the component size
5586 -- is known at compile time, which is always true in the packed array
5587 -- case. It is important that the packed array case is handled in the
5588 -- front end (see Eval_Attribute) since the back end would otherwise get
5589 -- confused by the equivalent packed array type.
5591 when Attribute_Component_Size =>
5592 null;
5594 -- The following attributes are handled by the back end (except that
5595 -- static cases have already been evaluated during semantic processing,
5596 -- but in any case the back end should not count on this). The one bit
5597 -- of special processing required is that these attributes typically
5598 -- generate conditionals in the code, so we need to check the relevant
5599 -- restriction.
5601 when Attribute_Max |
5602 Attribute_Min =>
5603 Check_Restriction (No_Implicit_Conditionals, N);
5605 -- The following attributes are handled by the back end (except that
5606 -- static cases have already been evaluated during semantic processing,
5607 -- but in any case the back end should not count on this).
5609 -- The back end also handles the non-class-wide cases of Size
5611 when Attribute_Bit_Order |
5612 Attribute_Code_Address |
5613 Attribute_Definite |
5614 Attribute_Null_Parameter |
5615 Attribute_Passed_By_Reference |
5616 Attribute_Pool_Address =>
5617 null;
5619 -- The following attributes are also handled by the back end, but return
5620 -- a universal integer result, so may need a conversion for checking
5621 -- that the result is in range.
5623 when Attribute_Aft |
5624 Attribute_Max_Alignment_For_Allocation =>
5625 Apply_Universal_Integer_Attribute_Checks (N);
5627 -- The following attributes should not appear at this stage, since they
5628 -- have already been handled by the analyzer (and properly rewritten
5629 -- with corresponding values or entities to represent the right values)
5631 when Attribute_Abort_Signal |
5632 Attribute_Address_Size |
5633 Attribute_Base |
5634 Attribute_Class |
5635 Attribute_Compiler_Version |
5636 Attribute_Default_Bit_Order |
5637 Attribute_Delta |
5638 Attribute_Denorm |
5639 Attribute_Digits |
5640 Attribute_Emax |
5641 Attribute_Enabled |
5642 Attribute_Epsilon |
5643 Attribute_Fast_Math |
5644 Attribute_Has_Access_Values |
5645 Attribute_Has_Discriminants |
5646 Attribute_Has_Tagged_Values |
5647 Attribute_Large |
5648 Attribute_Machine_Emax |
5649 Attribute_Machine_Emin |
5650 Attribute_Machine_Mantissa |
5651 Attribute_Machine_Overflows |
5652 Attribute_Machine_Radix |
5653 Attribute_Machine_Rounds |
5654 Attribute_Maximum_Alignment |
5655 Attribute_Model_Emin |
5656 Attribute_Model_Epsilon |
5657 Attribute_Model_Mantissa |
5658 Attribute_Model_Small |
5659 Attribute_Modulus |
5660 Attribute_Partition_ID |
5661 Attribute_Range |
5662 Attribute_Safe_Emax |
5663 Attribute_Safe_First |
5664 Attribute_Safe_Large |
5665 Attribute_Safe_Last |
5666 Attribute_Safe_Small |
5667 Attribute_Scale |
5668 Attribute_Signed_Zeros |
5669 Attribute_Small |
5670 Attribute_Storage_Unit |
5671 Attribute_Stub_Type |
5672 Attribute_System_Allocator_Alignment |
5673 Attribute_Target_Name |
5674 Attribute_Type_Class |
5675 Attribute_Type_Key |
5676 Attribute_Unconstrained_Array |
5677 Attribute_Universal_Literal_String |
5678 Attribute_Wchar_T_Size |
5679 Attribute_Word_Size =>
5680 raise Program_Error;
5682 -- The Asm_Input and Asm_Output attributes are not expanded at this
5683 -- stage, but will be eliminated in the expansion of the Asm call, see
5684 -- Exp_Intr for details. So the back end will never see these either.
5686 when Attribute_Asm_Input |
5687 Attribute_Asm_Output =>
5688 null;
5689 end case;
5691 exception
5692 when RE_Not_Available =>
5693 return;
5694 end Expand_N_Attribute_Reference;
5696 ----------------------
5697 -- Expand_Pred_Succ --
5698 ----------------------
5700 -- For typ'Pred (exp), we generate the check
5702 -- [constraint_error when exp = typ'Base'First]
5704 -- Similarly, for typ'Succ (exp), we generate the check
5706 -- [constraint_error when exp = typ'Base'Last]
5708 -- These checks are not generated for modular types, since the proper
5709 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
5710 -- We also suppress these checks if we are the right side of an assignment
5711 -- statement or the expression of an object declaration, where the flag
5712 -- Suppress_Assignment_Checks is set for the assignment/declaration.
5714 procedure Expand_Pred_Succ (N : Node_Id) is
5715 Loc : constant Source_Ptr := Sloc (N);
5716 P : constant Node_Id := Parent (N);
5717 Cnam : Name_Id;
5719 begin
5720 if Attribute_Name (N) = Name_Pred then
5721 Cnam := Name_First;
5722 else
5723 Cnam := Name_Last;
5724 end if;
5726 if not Nkind_In (P, N_Assignment_Statement, N_Object_Declaration)
5727 or else not Suppress_Assignment_Checks (P)
5728 then
5729 Insert_Action (N,
5730 Make_Raise_Constraint_Error (Loc,
5731 Condition =>
5732 Make_Op_Eq (Loc,
5733 Left_Opnd =>
5734 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
5735 Right_Opnd =>
5736 Make_Attribute_Reference (Loc,
5737 Prefix =>
5738 New_Reference_To (Base_Type (Etype (Prefix (N))), Loc),
5739 Attribute_Name => Cnam)),
5740 Reason => CE_Overflow_Check_Failed));
5741 end if;
5742 end Expand_Pred_Succ;
5744 -------------------
5745 -- Find_Fat_Info --
5746 -------------------
5748 procedure Find_Fat_Info
5749 (T : Entity_Id;
5750 Fat_Type : out Entity_Id;
5751 Fat_Pkg : out RE_Id)
5753 Btyp : constant Entity_Id := Base_Type (T);
5754 Rtyp : constant Entity_Id := Root_Type (T);
5755 Digs : constant Nat := UI_To_Int (Digits_Value (Btyp));
5757 begin
5758 -- If the base type is VAX float, then get appropriate VAX float type
5760 if Vax_Float (Btyp) then
5761 case Digs is
5762 when 6 =>
5763 Fat_Type := RTE (RE_Fat_VAX_F);
5764 Fat_Pkg := RE_Attr_VAX_F_Float;
5766 when 9 =>
5767 Fat_Type := RTE (RE_Fat_VAX_D);
5768 Fat_Pkg := RE_Attr_VAX_D_Float;
5770 when 15 =>
5771 Fat_Type := RTE (RE_Fat_VAX_G);
5772 Fat_Pkg := RE_Attr_VAX_G_Float;
5774 when others =>
5775 raise Program_Error;
5776 end case;
5778 -- If root type is VAX float, this is the case where the library has
5779 -- been recompiled in VAX float mode, and we have an IEEE float type.
5780 -- This is when we use the special IEEE Fat packages.
5782 elsif Vax_Float (Rtyp) then
5783 case Digs is
5784 when 6 =>
5785 Fat_Type := RTE (RE_Fat_IEEE_Short);
5786 Fat_Pkg := RE_Attr_IEEE_Short;
5788 when 15 =>
5789 Fat_Type := RTE (RE_Fat_IEEE_Long);
5790 Fat_Pkg := RE_Attr_IEEE_Long;
5792 when others =>
5793 raise Program_Error;
5794 end case;
5796 -- If neither the base type nor the root type is VAX_Native then VAX
5797 -- float is out of the picture, and we can just use the root type.
5799 else
5800 Fat_Type := Rtyp;
5802 if Fat_Type = Standard_Short_Float then
5803 Fat_Pkg := RE_Attr_Short_Float;
5805 elsif Fat_Type = Standard_Float then
5806 Fat_Pkg := RE_Attr_Float;
5808 elsif Fat_Type = Standard_Long_Float then
5809 Fat_Pkg := RE_Attr_Long_Float;
5811 elsif Fat_Type = Standard_Long_Long_Float then
5812 Fat_Pkg := RE_Attr_Long_Long_Float;
5814 -- Universal real (which is its own root type) is treated as being
5815 -- equivalent to Standard.Long_Long_Float, since it is defined to
5816 -- have the same precision as the longest Float type.
5818 elsif Fat_Type = Universal_Real then
5819 Fat_Type := Standard_Long_Long_Float;
5820 Fat_Pkg := RE_Attr_Long_Long_Float;
5822 else
5823 raise Program_Error;
5824 end if;
5825 end if;
5826 end Find_Fat_Info;
5828 ----------------------------
5829 -- Find_Stream_Subprogram --
5830 ----------------------------
5832 function Find_Stream_Subprogram
5833 (Typ : Entity_Id;
5834 Nam : TSS_Name_Type) return Entity_Id
5836 Base_Typ : constant Entity_Id := Base_Type (Typ);
5837 Ent : constant Entity_Id := TSS (Typ, Nam);
5839 function Is_Available (Entity : RE_Id) return Boolean;
5840 pragma Inline (Is_Available);
5841 -- Function to check whether the specified run-time call is available
5842 -- in the run time used. In the case of a configurable run time, it
5843 -- is normal that some subprograms are not there.
5845 -- I don't understand this routine at all, why is this not just a
5846 -- call to RTE_Available? And if for some reason we need a different
5847 -- routine with different semantics, why is not in Rtsfind ???
5849 ------------------
5850 -- Is_Available --
5851 ------------------
5853 function Is_Available (Entity : RE_Id) return Boolean is
5854 begin
5855 -- Assume that the unit will always be available when using a
5856 -- "normal" (not configurable) run time.
5858 return not Configurable_Run_Time_Mode
5859 or else RTE_Available (Entity);
5860 end Is_Available;
5862 -- Start of processing for Find_Stream_Subprogram
5864 begin
5865 if Present (Ent) then
5866 return Ent;
5867 end if;
5869 -- Stream attributes for strings are expanded into library calls. The
5870 -- following checks are disabled when the run-time is not available or
5871 -- when compiling predefined types due to bootstrap issues. As a result,
5872 -- the compiler will generate in-place stream routines for string types
5873 -- that appear in GNAT's library, but will generate calls via rtsfind
5874 -- to library routines for user code.
5876 -- ??? For now, disable this code for JVM, since this generates a
5877 -- VerifyError exception at run time on e.g. c330001.
5879 -- This is disabled for AAMP, to avoid creating dependences on files not
5880 -- supported in the AAMP library (such as s-fileio.adb).
5882 -- Note: In the case of using a configurable run time, it is very likely
5883 -- that stream routines for string types are not present (they require
5884 -- file system support). In this case, the specific stream routines for
5885 -- strings are not used, relying on the regular stream mechanism
5886 -- instead. That is why we include the test Is_Available when dealing
5887 -- with these cases.
5889 if VM_Target /= JVM_Target
5890 and then not AAMP_On_Target
5891 and then
5892 not Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
5893 then
5894 -- String as defined in package Ada
5896 if Base_Typ = Standard_String then
5897 if Restriction_Active (No_Stream_Optimizations) then
5898 if Nam = TSS_Stream_Input
5899 and then Is_Available (RE_String_Input)
5900 then
5901 return RTE (RE_String_Input);
5903 elsif Nam = TSS_Stream_Output
5904 and then Is_Available (RE_String_Output)
5905 then
5906 return RTE (RE_String_Output);
5908 elsif Nam = TSS_Stream_Read
5909 and then Is_Available (RE_String_Read)
5910 then
5911 return RTE (RE_String_Read);
5913 elsif Nam = TSS_Stream_Write
5914 and then Is_Available (RE_String_Write)
5915 then
5916 return RTE (RE_String_Write);
5918 elsif Nam /= TSS_Stream_Input and then
5919 Nam /= TSS_Stream_Output and then
5920 Nam /= TSS_Stream_Read and then
5921 Nam /= TSS_Stream_Write
5922 then
5923 raise Program_Error;
5924 end if;
5926 else
5927 if Nam = TSS_Stream_Input
5928 and then Is_Available (RE_String_Input_Blk_IO)
5929 then
5930 return RTE (RE_String_Input_Blk_IO);
5932 elsif Nam = TSS_Stream_Output
5933 and then Is_Available (RE_String_Output_Blk_IO)
5934 then
5935 return RTE (RE_String_Output_Blk_IO);
5937 elsif Nam = TSS_Stream_Read
5938 and then Is_Available (RE_String_Read_Blk_IO)
5939 then
5940 return RTE (RE_String_Read_Blk_IO);
5942 elsif Nam = TSS_Stream_Write
5943 and then Is_Available (RE_String_Write_Blk_IO)
5944 then
5945 return RTE (RE_String_Write_Blk_IO);
5947 elsif Nam /= TSS_Stream_Input and then
5948 Nam /= TSS_Stream_Output and then
5949 Nam /= TSS_Stream_Read and then
5950 Nam /= TSS_Stream_Write
5951 then
5952 raise Program_Error;
5953 end if;
5954 end if;
5956 -- Wide_String as defined in package Ada
5958 elsif Base_Typ = Standard_Wide_String then
5959 if Restriction_Active (No_Stream_Optimizations) then
5960 if Nam = TSS_Stream_Input
5961 and then Is_Available (RE_Wide_String_Input)
5962 then
5963 return RTE (RE_Wide_String_Input);
5965 elsif Nam = TSS_Stream_Output
5966 and then Is_Available (RE_Wide_String_Output)
5967 then
5968 return RTE (RE_Wide_String_Output);
5970 elsif Nam = TSS_Stream_Read
5971 and then Is_Available (RE_Wide_String_Read)
5972 then
5973 return RTE (RE_Wide_String_Read);
5975 elsif Nam = TSS_Stream_Write
5976 and then Is_Available (RE_Wide_String_Write)
5977 then
5978 return RTE (RE_Wide_String_Write);
5980 elsif Nam /= TSS_Stream_Input and then
5981 Nam /= TSS_Stream_Output and then
5982 Nam /= TSS_Stream_Read and then
5983 Nam /= TSS_Stream_Write
5984 then
5985 raise Program_Error;
5986 end if;
5988 else
5989 if Nam = TSS_Stream_Input
5990 and then Is_Available (RE_Wide_String_Input_Blk_IO)
5991 then
5992 return RTE (RE_Wide_String_Input_Blk_IO);
5994 elsif Nam = TSS_Stream_Output
5995 and then Is_Available (RE_Wide_String_Output_Blk_IO)
5996 then
5997 return RTE (RE_Wide_String_Output_Blk_IO);
5999 elsif Nam = TSS_Stream_Read
6000 and then Is_Available (RE_Wide_String_Read_Blk_IO)
6001 then
6002 return RTE (RE_Wide_String_Read_Blk_IO);
6004 elsif Nam = TSS_Stream_Write
6005 and then Is_Available (RE_Wide_String_Write_Blk_IO)
6006 then
6007 return RTE (RE_Wide_String_Write_Blk_IO);
6009 elsif Nam /= TSS_Stream_Input and then
6010 Nam /= TSS_Stream_Output and then
6011 Nam /= TSS_Stream_Read and then
6012 Nam /= TSS_Stream_Write
6013 then
6014 raise Program_Error;
6015 end if;
6016 end if;
6018 -- Wide_Wide_String as defined in package Ada
6020 elsif Base_Typ = Standard_Wide_Wide_String then
6021 if Restriction_Active (No_Stream_Optimizations) then
6022 if Nam = TSS_Stream_Input
6023 and then Is_Available (RE_Wide_Wide_String_Input)
6024 then
6025 return RTE (RE_Wide_Wide_String_Input);
6027 elsif Nam = TSS_Stream_Output
6028 and then Is_Available (RE_Wide_Wide_String_Output)
6029 then
6030 return RTE (RE_Wide_Wide_String_Output);
6032 elsif Nam = TSS_Stream_Read
6033 and then Is_Available (RE_Wide_Wide_String_Read)
6034 then
6035 return RTE (RE_Wide_Wide_String_Read);
6037 elsif Nam = TSS_Stream_Write
6038 and then Is_Available (RE_Wide_Wide_String_Write)
6039 then
6040 return RTE (RE_Wide_Wide_String_Write);
6042 elsif Nam /= TSS_Stream_Input and then
6043 Nam /= TSS_Stream_Output and then
6044 Nam /= TSS_Stream_Read and then
6045 Nam /= TSS_Stream_Write
6046 then
6047 raise Program_Error;
6048 end if;
6050 else
6051 if Nam = TSS_Stream_Input
6052 and then Is_Available (RE_Wide_Wide_String_Input_Blk_IO)
6053 then
6054 return RTE (RE_Wide_Wide_String_Input_Blk_IO);
6056 elsif Nam = TSS_Stream_Output
6057 and then Is_Available (RE_Wide_Wide_String_Output_Blk_IO)
6058 then
6059 return RTE (RE_Wide_Wide_String_Output_Blk_IO);
6061 elsif Nam = TSS_Stream_Read
6062 and then Is_Available (RE_Wide_Wide_String_Read_Blk_IO)
6063 then
6064 return RTE (RE_Wide_Wide_String_Read_Blk_IO);
6066 elsif Nam = TSS_Stream_Write
6067 and then Is_Available (RE_Wide_Wide_String_Write_Blk_IO)
6068 then
6069 return RTE (RE_Wide_Wide_String_Write_Blk_IO);
6071 elsif Nam /= TSS_Stream_Input and then
6072 Nam /= TSS_Stream_Output and then
6073 Nam /= TSS_Stream_Read and then
6074 Nam /= TSS_Stream_Write
6075 then
6076 raise Program_Error;
6077 end if;
6078 end if;
6079 end if;
6080 end if;
6082 if Is_Tagged_Type (Typ)
6083 and then Is_Derived_Type (Typ)
6084 then
6085 return Find_Prim_Op (Typ, Nam);
6086 else
6087 return Find_Inherited_TSS (Typ, Nam);
6088 end if;
6089 end Find_Stream_Subprogram;
6091 ---------------
6092 -- Full_Base --
6093 ---------------
6095 function Full_Base (T : Entity_Id) return Entity_Id is
6096 BT : Entity_Id;
6098 begin
6099 BT := Base_Type (T);
6101 if Is_Private_Type (BT)
6102 and then Present (Full_View (BT))
6103 then
6104 BT := Full_View (BT);
6105 end if;
6107 return BT;
6108 end Full_Base;
6110 -----------------------
6111 -- Get_Index_Subtype --
6112 -----------------------
6114 function Get_Index_Subtype (N : Node_Id) return Node_Id is
6115 P_Type : Entity_Id := Etype (Prefix (N));
6116 Indx : Node_Id;
6117 J : Int;
6119 begin
6120 if Is_Access_Type (P_Type) then
6121 P_Type := Designated_Type (P_Type);
6122 end if;
6124 if No (Expressions (N)) then
6125 J := 1;
6126 else
6127 J := UI_To_Int (Expr_Value (First (Expressions (N))));
6128 end if;
6130 Indx := First_Index (P_Type);
6131 while J > 1 loop
6132 Next_Index (Indx);
6133 J := J - 1;
6134 end loop;
6136 return Etype (Indx);
6137 end Get_Index_Subtype;
6139 -------------------------------
6140 -- Get_Stream_Convert_Pragma --
6141 -------------------------------
6143 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is
6144 Typ : Entity_Id;
6145 N : Node_Id;
6147 begin
6148 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
6149 -- that a stream convert pragma for a tagged type is not inherited from
6150 -- its parent. Probably what is wrong here is that it is basically
6151 -- incorrect to consider a stream convert pragma to be a representation
6152 -- pragma at all ???
6154 N := First_Rep_Item (Implementation_Base_Type (T));
6155 while Present (N) loop
6156 if Nkind (N) = N_Pragma
6157 and then Pragma_Name (N) = Name_Stream_Convert
6158 then
6159 -- For tagged types this pragma is not inherited, so we
6160 -- must verify that it is defined for the given type and
6161 -- not an ancestor.
6163 Typ :=
6164 Entity (Expression (First (Pragma_Argument_Associations (N))));
6166 if not Is_Tagged_Type (T)
6167 or else T = Typ
6168 or else (Is_Private_Type (Typ) and then T = Full_View (Typ))
6169 then
6170 return N;
6171 end if;
6172 end if;
6174 Next_Rep_Item (N);
6175 end loop;
6177 return Empty;
6178 end Get_Stream_Convert_Pragma;
6180 ---------------------------------
6181 -- Is_Constrained_Packed_Array --
6182 ---------------------------------
6184 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
6185 Arr : Entity_Id := Typ;
6187 begin
6188 if Is_Access_Type (Arr) then
6189 Arr := Designated_Type (Arr);
6190 end if;
6192 return Is_Array_Type (Arr)
6193 and then Is_Constrained (Arr)
6194 and then Present (Packed_Array_Type (Arr));
6195 end Is_Constrained_Packed_Array;
6197 ----------------------------------------
6198 -- Is_Inline_Floating_Point_Attribute --
6199 ----------------------------------------
6201 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is
6202 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
6204 begin
6205 if Nkind (Parent (N)) /= N_Type_Conversion
6206 or else not Is_Integer_Type (Etype (Parent (N)))
6207 then
6208 return False;
6209 end if;
6211 -- Should also support 'Machine_Rounding and 'Unbiased_Rounding, but
6212 -- required back end support has not been implemented yet ???
6214 return Id = Attribute_Truncation;
6215 end Is_Inline_Floating_Point_Attribute;
6217 end Exp_Attr;