Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / ada / exp_attr.adb
blob5126e5a1730482757d53f60a6fb5e31ab8bfe78f
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-2010, 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. --
17 -- --
18 -- You should have received a copy of the GNU General Public License along --
19 -- with this program; see file COPYING3. If not see --
20 -- <http://www.gnu.org/licenses/>. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch2; use Exp_Ch2;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Imgv; use Exp_Imgv;
38 with Exp_Pakd; use Exp_Pakd;
39 with Exp_Strm; use Exp_Strm;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Exp_VFpt; use Exp_VFpt;
43 with Fname; use Fname;
44 with Freeze; use Freeze;
45 with Gnatvsn; use Gnatvsn;
46 with Itypes; use Itypes;
47 with Lib; use Lib;
48 with Namet; use Namet;
49 with Nmake; use Nmake;
50 with Nlists; use Nlists;
51 with Opt; use Opt;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
55 with Sem; use Sem;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Res; use Sem_Res;
62 with Sem_Util; use Sem_Util;
63 with Sinfo; use Sinfo;
64 with Snames; use Snames;
65 with Stand; use Stand;
66 with Stringt; use Stringt;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uintp; use Uintp;
71 with Uname; use Uname;
72 with Validsw; use Validsw;
74 package body Exp_Attr is
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 procedure Compile_Stream_Body_In_Scope
81 (N : Node_Id;
82 Decl : Node_Id;
83 Arr : Entity_Id;
84 Check : Boolean);
85 -- The body for a stream subprogram may be generated outside of the scope
86 -- of the type. If the type is fully private, it may depend on the full
87 -- view of other types (e.g. indices) that are currently private as well.
88 -- We install the declarations of the package in which the type is declared
89 -- before compiling the body in what is its proper environment. The Check
90 -- parameter indicates if checks are to be suppressed for the stream body.
91 -- We suppress checks for array/record reads, since the rule is that these
92 -- are like assignments, out of range values due to uninitialized storage,
93 -- or other invalid values do NOT cause a Constraint_Error to be raised.
95 procedure Expand_Access_To_Protected_Op
96 (N : Node_Id;
97 Pref : Node_Id;
98 Typ : Entity_Id);
100 -- An attribute reference to a protected subprogram is transformed into
101 -- a pair of pointers: one to the object, and one to the operations.
102 -- This expansion is performed for 'Access and for 'Unrestricted_Access.
104 procedure Expand_Fpt_Attribute
105 (N : Node_Id;
106 Pkg : RE_Id;
107 Nam : Name_Id;
108 Args : List_Id);
109 -- This procedure expands a call to a floating-point attribute function.
110 -- N is the attribute reference node, and Args is a list of arguments to
111 -- be passed to the function call. Pkg identifies the package containing
112 -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args
113 -- have already been converted to the floating-point type for which Pkg was
114 -- instantiated. The Nam argument is the relevant attribute processing
115 -- routine to be called. This is the same as the attribute name, except in
116 -- the Unaligned_Valid case.
118 procedure Expand_Fpt_Attribute_R (N : Node_Id);
119 -- This procedure expands a call to a floating-point attribute function
120 -- that takes a single floating-point argument. The function to be called
121 -- is always the same as the attribute name.
123 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
124 -- This procedure expands a call to a floating-point attribute function
125 -- that takes one floating-point argument and one integer argument. The
126 -- function to be called is always the same as the attribute name.
128 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
129 -- This procedure expands a call to a floating-point attribute function
130 -- that takes two floating-point arguments. The function to be called
131 -- is always the same as the attribute name.
133 procedure Expand_Pred_Succ (N : Node_Id);
134 -- Handles expansion of Pred or Succ attributes for case of non-real
135 -- operand with overflow checking required.
137 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
138 -- Used for Last, Last, and Length, when the prefix is an array type.
139 -- Obtains the corresponding index subtype.
141 procedure Find_Fat_Info
142 (T : Entity_Id;
143 Fat_Type : out Entity_Id;
144 Fat_Pkg : out RE_Id);
145 -- Given a floating-point type T, identifies the package containing the
146 -- attributes for this type (returned in Fat_Pkg), and the corresponding
147 -- type for which this package was instantiated from Fat_Gen. Error if T
148 -- is not a floating-point type.
150 function Find_Stream_Subprogram
151 (Typ : Entity_Id;
152 Nam : TSS_Name_Type) return Entity_Id;
153 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
154 -- types, the corresponding primitive operation is looked up, else the
155 -- appropriate TSS from the type itself, or from its closest ancestor
156 -- defining it, is returned. In both cases, inheritance of representation
157 -- aspects is thus taken into account.
159 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id;
160 -- Given a type, find a corresponding stream convert pragma that applies to
161 -- the implementation base type of this type (Typ). If found, return the
162 -- pragma node, otherwise return Empty if no pragma is found.
164 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
165 -- Utility for array attributes, returns true on packed constrained
166 -- arrays, and on access to same.
168 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean;
169 -- Returns true iff the given node refers to an attribute call that
170 -- can be expanded directly by the back end and does not need front end
171 -- expansion. Typically used for rounding and truncation attributes that
172 -- appear directly inside a conversion to integer.
174 ----------------------------------
175 -- Compile_Stream_Body_In_Scope --
176 ----------------------------------
178 procedure Compile_Stream_Body_In_Scope
179 (N : Node_Id;
180 Decl : Node_Id;
181 Arr : Entity_Id;
182 Check : Boolean)
184 Installed : Boolean := False;
185 Scop : constant Entity_Id := Scope (Arr);
186 Curr : constant Entity_Id := Current_Scope;
188 begin
189 if Is_Hidden (Arr)
190 and then not In_Open_Scopes (Scop)
191 and then Ekind (Scop) = E_Package
192 then
193 Push_Scope (Scop);
194 Install_Visible_Declarations (Scop);
195 Install_Private_Declarations (Scop);
196 Installed := True;
198 -- The entities in the package are now visible, but the generated
199 -- stream entity must appear in the current scope (usually an
200 -- enclosing stream function) so that itypes all have their proper
201 -- scopes.
203 Push_Scope (Curr);
204 end if;
206 if Check then
207 Insert_Action (N, Decl);
208 else
209 Insert_Action (N, Decl, Suppress => All_Checks);
210 end if;
212 if Installed then
214 -- Remove extra copy of current scope, and package itself
216 Pop_Scope;
217 End_Package_Scope (Scop);
218 end if;
219 end Compile_Stream_Body_In_Scope;
221 -----------------------------------
222 -- Expand_Access_To_Protected_Op --
223 -----------------------------------
225 procedure Expand_Access_To_Protected_Op
226 (N : Node_Id;
227 Pref : Node_Id;
228 Typ : Entity_Id)
230 -- The value of the attribute_reference is a record containing two
231 -- fields: an access to the protected object, and an access to the
232 -- subprogram itself. The prefix is a selected component.
234 Loc : constant Source_Ptr := Sloc (N);
235 Agg : Node_Id;
236 Btyp : constant Entity_Id := Base_Type (Typ);
237 Sub : Entity_Id;
238 Sub_Ref : Node_Id;
239 E_T : constant Entity_Id := Equivalent_Type (Btyp);
240 Acc : constant Entity_Id :=
241 Etype (Next_Component (First_Component (E_T)));
242 Obj_Ref : Node_Id;
243 Curr : Entity_Id;
245 function May_Be_External_Call return Boolean;
246 -- If the 'Access is to a local operation, but appears in a context
247 -- where it may lead to a call from outside the object, we must treat
248 -- this as an external call. Clearly we cannot tell without full
249 -- flow analysis, and a subsequent call that uses this 'Access may
250 -- lead to a bounded error (trying to seize locks twice, e.g.). For
251 -- now we treat 'Access as a potential external call if it is an actual
252 -- in a call to an outside subprogram.
254 --------------------------
255 -- May_Be_External_Call --
256 --------------------------
258 function May_Be_External_Call return Boolean is
259 Subp : Entity_Id;
260 Par : Node_Id := Parent (N);
262 begin
263 -- Account for the case where the Access attribute is part of a
264 -- named parameter association.
266 if Nkind (Par) = N_Parameter_Association then
267 Par := Parent (Par);
268 end if;
270 if Nkind_In (Par, N_Procedure_Call_Statement, N_Function_Call)
271 and then Is_Entity_Name (Name (Par))
272 then
273 Subp := Entity (Name (Par));
274 return not In_Open_Scopes (Scope (Subp));
275 else
276 return False;
277 end if;
278 end May_Be_External_Call;
280 -- Start of processing for Expand_Access_To_Protected_Op
282 begin
283 -- Within the body of the protected type, the prefix designates a local
284 -- operation, and the object is the first parameter of the corresponding
285 -- protected body of the current enclosing operation.
287 if Is_Entity_Name (Pref) then
288 if May_Be_External_Call then
289 Sub :=
290 New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc);
291 else
292 Sub :=
293 New_Occurrence_Of
294 (Protected_Body_Subprogram (Entity (Pref)), Loc);
295 end if;
297 -- Don't traverse the scopes when the attribute occurs within an init
298 -- proc, because we directly use the _init formal of the init proc in
299 -- that case.
301 Curr := Current_Scope;
302 if not Is_Init_Proc (Curr) then
303 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
305 while Scope (Curr) /= Scope (Entity (Pref)) loop
306 Curr := Scope (Curr);
307 end loop;
308 end if;
310 -- In case of protected entries the first formal of its Protected_
311 -- Body_Subprogram is the address of the object.
313 if Ekind (Curr) = E_Entry then
314 Obj_Ref :=
315 New_Occurrence_Of
316 (First_Formal
317 (Protected_Body_Subprogram (Curr)), Loc);
319 -- If the current scope is an init proc, then use the address of the
320 -- _init formal as the object reference.
322 elsif Is_Init_Proc (Curr) then
323 Obj_Ref :=
324 Make_Attribute_Reference (Loc,
325 Prefix => New_Occurrence_Of (First_Formal (Curr), Loc),
326 Attribute_Name => Name_Address);
328 -- In case of protected subprograms the first formal of its
329 -- Protected_Body_Subprogram is the object and we get its address.
331 else
332 Obj_Ref :=
333 Make_Attribute_Reference (Loc,
334 Prefix =>
335 New_Occurrence_Of
336 (First_Formal
337 (Protected_Body_Subprogram (Curr)), Loc),
338 Attribute_Name => Name_Address);
339 end if;
341 -- Case where the prefix is not an entity name. Find the
342 -- version of the protected operation to be called from
343 -- outside the protected object.
345 else
346 Sub :=
347 New_Occurrence_Of
348 (External_Subprogram
349 (Entity (Selector_Name (Pref))), Loc);
351 Obj_Ref :=
352 Make_Attribute_Reference (Loc,
353 Prefix => Relocate_Node (Prefix (Pref)),
354 Attribute_Name => Name_Address);
355 end if;
357 Sub_Ref :=
358 Make_Attribute_Reference (Loc,
359 Prefix => Sub,
360 Attribute_Name => Name_Access);
362 -- We set the type of the access reference to the already generated
363 -- access_to_subprogram type, and declare the reference analyzed, to
364 -- prevent further expansion when the enclosing aggregate is analyzed.
366 Set_Etype (Sub_Ref, Acc);
367 Set_Analyzed (Sub_Ref);
369 Agg :=
370 Make_Aggregate (Loc,
371 Expressions => New_List (Obj_Ref, Sub_Ref));
373 Freeze_Before (N, Entity (Sub));
374 Rewrite (N, Agg);
375 Analyze_And_Resolve (N, E_T);
377 -- For subsequent analysis, the node must retain its type. The backend
378 -- will replace it with the equivalent type where needed.
380 Set_Etype (N, Typ);
381 end Expand_Access_To_Protected_Op;
383 --------------------------
384 -- Expand_Fpt_Attribute --
385 --------------------------
387 procedure Expand_Fpt_Attribute
388 (N : Node_Id;
389 Pkg : RE_Id;
390 Nam : Name_Id;
391 Args : List_Id)
393 Loc : constant Source_Ptr := Sloc (N);
394 Typ : constant Entity_Id := Etype (N);
395 Fnm : Node_Id;
397 begin
398 -- The function name is the selected component Attr_xxx.yyy where
399 -- Attr_xxx is the package name, and yyy is the argument Nam.
401 -- Note: it would be more usual to have separate RE entries for each
402 -- of the entities in the Fat packages, but first they have identical
403 -- names (so we would have to have lots of renaming declarations to
404 -- meet the normal RE rule of separate names for all runtime entities),
405 -- and second there would be an awful lot of them!
407 Fnm :=
408 Make_Selected_Component (Loc,
409 Prefix => New_Reference_To (RTE (Pkg), Loc),
410 Selector_Name => Make_Identifier (Loc, Nam));
412 -- The generated call is given the provided set of parameters, and then
413 -- wrapped in a conversion which converts the result to the target type
414 -- We use the base type as the target because a range check may be
415 -- required.
417 Rewrite (N,
418 Unchecked_Convert_To (Base_Type (Etype (N)),
419 Make_Function_Call (Loc,
420 Name => Fnm,
421 Parameter_Associations => Args)));
423 Analyze_And_Resolve (N, Typ);
424 end Expand_Fpt_Attribute;
426 ----------------------------
427 -- Expand_Fpt_Attribute_R --
428 ----------------------------
430 -- The single argument is converted to its root type to call the
431 -- appropriate runtime function, with the actual call being built
432 -- by Expand_Fpt_Attribute
434 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
435 E1 : constant Node_Id := First (Expressions (N));
436 Ftp : Entity_Id;
437 Pkg : RE_Id;
438 begin
439 Find_Fat_Info (Etype (E1), Ftp, Pkg);
440 Expand_Fpt_Attribute
441 (N, Pkg, Attribute_Name (N),
442 New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1))));
443 end Expand_Fpt_Attribute_R;
445 -----------------------------
446 -- Expand_Fpt_Attribute_RI --
447 -----------------------------
449 -- The first argument is converted to its root type and the second
450 -- argument is converted to standard long long integer to call the
451 -- appropriate runtime function, with the actual call being built
452 -- by Expand_Fpt_Attribute
454 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
455 E1 : constant Node_Id := First (Expressions (N));
456 Ftp : Entity_Id;
457 Pkg : RE_Id;
458 E2 : constant Node_Id := Next (E1);
459 begin
460 Find_Fat_Info (Etype (E1), Ftp, Pkg);
461 Expand_Fpt_Attribute
462 (N, Pkg, Attribute_Name (N),
463 New_List (
464 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
465 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
466 end Expand_Fpt_Attribute_RI;
468 -----------------------------
469 -- Expand_Fpt_Attribute_RR --
470 -----------------------------
472 -- The two arguments are converted to their root types to call the
473 -- appropriate runtime function, with the actual call being built
474 -- by Expand_Fpt_Attribute
476 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
477 E1 : constant Node_Id := First (Expressions (N));
478 Ftp : Entity_Id;
479 Pkg : RE_Id;
480 E2 : constant Node_Id := Next (E1);
481 begin
482 Find_Fat_Info (Etype (E1), Ftp, Pkg);
483 Expand_Fpt_Attribute
484 (N, Pkg, Attribute_Name (N),
485 New_List (
486 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
487 Unchecked_Convert_To (Ftp, Relocate_Node (E2))));
488 end Expand_Fpt_Attribute_RR;
490 ----------------------------------
491 -- Expand_N_Attribute_Reference --
492 ----------------------------------
494 procedure Expand_N_Attribute_Reference (N : Node_Id) is
495 Loc : constant Source_Ptr := Sloc (N);
496 Typ : constant Entity_Id := Etype (N);
497 Btyp : constant Entity_Id := Base_Type (Typ);
498 Pref : constant Node_Id := Prefix (N);
499 Ptyp : constant Entity_Id := Etype (Pref);
500 Exprs : constant List_Id := Expressions (N);
501 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
503 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
504 -- Rewrites a stream attribute for Read, Write or Output with the
505 -- procedure call. Pname is the entity for the procedure to call.
507 ------------------------------
508 -- Rewrite_Stream_Proc_Call --
509 ------------------------------
511 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
512 Item : constant Node_Id := Next (First (Exprs));
513 Formal : constant Entity_Id := Next_Formal (First_Formal (Pname));
514 Formal_Typ : constant Entity_Id := Etype (Formal);
515 Is_Written : constant Boolean := (Ekind (Formal) /= E_In_Parameter);
517 begin
518 -- The expansion depends on Item, the second actual, which is
519 -- the object being streamed in or out.
521 -- If the item is a component of a packed array type, and
522 -- a conversion is needed on exit, we introduce a temporary to
523 -- hold the value, because otherwise the packed reference will
524 -- not be properly expanded.
526 if Nkind (Item) = N_Indexed_Component
527 and then Is_Packed (Base_Type (Etype (Prefix (Item))))
528 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
529 and then Is_Written
530 then
531 declare
532 Temp : constant Entity_Id := Make_Temporary (Loc, 'V');
533 Decl : Node_Id;
534 Assn : Node_Id;
536 begin
537 Decl :=
538 Make_Object_Declaration (Loc,
539 Defining_Identifier => Temp,
540 Object_Definition =>
541 New_Occurrence_Of (Formal_Typ, Loc));
542 Set_Etype (Temp, Formal_Typ);
544 Assn :=
545 Make_Assignment_Statement (Loc,
546 Name => New_Copy_Tree (Item),
547 Expression =>
548 Unchecked_Convert_To
549 (Etype (Item), New_Occurrence_Of (Temp, Loc)));
551 Rewrite (Item, New_Occurrence_Of (Temp, Loc));
552 Insert_Actions (N,
553 New_List (
554 Decl,
555 Make_Procedure_Call_Statement (Loc,
556 Name => New_Occurrence_Of (Pname, Loc),
557 Parameter_Associations => Exprs),
558 Assn));
560 Rewrite (N, Make_Null_Statement (Loc));
561 return;
562 end;
563 end if;
565 -- For the class-wide dispatching cases, and for cases in which
566 -- the base type of the second argument matches the base type of
567 -- the corresponding formal parameter (that is to say the stream
568 -- operation is not inherited), we are all set, and can use the
569 -- argument unchanged.
571 -- For all other cases we do an unchecked conversion of the second
572 -- parameter to the type of the formal of the procedure we are
573 -- calling. This deals with the private type cases, and with going
574 -- to the root type as required in elementary type case.
576 if not Is_Class_Wide_Type (Entity (Pref))
577 and then not Is_Class_Wide_Type (Etype (Item))
578 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
579 then
580 Rewrite (Item,
581 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
583 -- For untagged derived types set Assignment_OK, to prevent
584 -- copies from being created when the unchecked conversion
585 -- is expanded (which would happen in Remove_Side_Effects
586 -- if Expand_N_Unchecked_Conversion were allowed to call
587 -- Force_Evaluation). The copy could violate Ada semantics
588 -- in cases such as an actual that is an out parameter.
589 -- Note that this approach is also used in exp_ch7 for calls
590 -- to controlled type operations to prevent problems with
591 -- actuals wrapped in unchecked conversions.
593 if Is_Untagged_Derivation (Etype (Expression (Item))) then
594 Set_Assignment_OK (Item);
595 end if;
596 end if;
598 -- The stream operation to call maybe a renaming created by
599 -- an attribute definition clause, and may not be frozen yet.
600 -- Ensure that it has the necessary extra formals.
602 if not Is_Frozen (Pname) then
603 Create_Extra_Formals (Pname);
604 end if;
606 -- And now rewrite the call
608 Rewrite (N,
609 Make_Procedure_Call_Statement (Loc,
610 Name => New_Occurrence_Of (Pname, Loc),
611 Parameter_Associations => Exprs));
613 Analyze (N);
614 end Rewrite_Stream_Proc_Call;
616 -- Start of processing for Expand_N_Attribute_Reference
618 begin
619 -- Do required validity checking, if enabled. Do not apply check to
620 -- output parameters of an Asm instruction, since the value of this
621 -- is not set till after the attribute has been elaborated, and do
622 -- not apply the check to the arguments of a 'Read or 'Input attribute
623 -- reference since the scalar argument is an OUT scalar.
625 if Validity_Checks_On and then Validity_Check_Operands
626 and then Id /= Attribute_Asm_Output
627 and then Id /= Attribute_Read
628 and then Id /= Attribute_Input
629 then
630 declare
631 Expr : Node_Id;
632 begin
633 Expr := First (Expressions (N));
634 while Present (Expr) loop
635 Ensure_Valid (Expr);
636 Next (Expr);
637 end loop;
638 end;
639 end if;
641 -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in-
642 -- place function, then a temporary return object needs to be created
643 -- and access to it must be passed to the function. Currently we limit
644 -- such functions to those with inherently limited result subtypes, but
645 -- eventually we plan to expand the functions that are treated as
646 -- build-in-place to include other composite result types.
648 if Ada_Version >= Ada_05
649 and then Is_Build_In_Place_Function_Call (Pref)
650 then
651 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
652 end if;
654 -- If prefix is a protected type name, this is a reference to the
655 -- current instance of the type. For a component definition, nothing
656 -- to do (expansion will occur in the init proc). In other contexts,
657 -- rewrite into reference to current instance.
659 if Is_Protected_Self_Reference (Pref)
660 and then not
661 (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint,
662 N_Discriminant_Association)
663 and then Nkind (Parent (Parent (Parent (Parent (N))))) =
664 N_Component_Definition)
665 then
666 Rewrite (Pref, Concurrent_Ref (Pref));
667 Analyze (Pref);
668 end if;
670 -- Remaining processing depends on specific attribute
672 case Id is
674 ------------
675 -- Access --
676 ------------
678 when Attribute_Access |
679 Attribute_Unchecked_Access |
680 Attribute_Unrestricted_Access =>
682 Access_Cases : declare
683 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
684 Btyp_DDT : Entity_Id;
686 function Enclosing_Object (N : Node_Id) return Node_Id;
687 -- If N denotes a compound name (selected component, indexed
688 -- component, or slice), returns the name of the outermost such
689 -- enclosing object. Otherwise returns N. If the object is a
690 -- renaming, then the renamed object is returned.
692 ----------------------
693 -- Enclosing_Object --
694 ----------------------
696 function Enclosing_Object (N : Node_Id) return Node_Id is
697 Obj_Name : Node_Id;
699 begin
700 Obj_Name := N;
701 while Nkind_In (Obj_Name, N_Selected_Component,
702 N_Indexed_Component,
703 N_Slice)
704 loop
705 Obj_Name := Prefix (Obj_Name);
706 end loop;
708 return Get_Referenced_Object (Obj_Name);
709 end Enclosing_Object;
711 -- Local declarations
713 Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object);
715 -- Start of processing for Access_Cases
717 begin
718 Btyp_DDT := Designated_Type (Btyp);
720 -- Handle designated types that come from the limited view
722 if Ekind (Btyp_DDT) = E_Incomplete_Type
723 and then From_With_Type (Btyp_DDT)
724 and then Present (Non_Limited_View (Btyp_DDT))
725 then
726 Btyp_DDT := Non_Limited_View (Btyp_DDT);
728 elsif Is_Class_Wide_Type (Btyp_DDT)
729 and then Ekind (Etype (Btyp_DDT)) = E_Incomplete_Type
730 and then From_With_Type (Etype (Btyp_DDT))
731 and then Present (Non_Limited_View (Etype (Btyp_DDT)))
732 and then Present (Class_Wide_Type
733 (Non_Limited_View (Etype (Btyp_DDT))))
734 then
735 Btyp_DDT :=
736 Class_Wide_Type (Non_Limited_View (Etype (Btyp_DDT)));
737 end if;
739 -- In order to improve the text of error messages, the designated
740 -- type of access-to-subprogram itypes is set by the semantics as
741 -- the associated subprogram entity (see sem_attr). Now we replace
742 -- such node with the proper E_Subprogram_Type itype.
744 if Id = Attribute_Unrestricted_Access
745 and then Is_Subprogram (Directly_Designated_Type (Typ))
746 then
747 -- The following conditions ensure that this special management
748 -- is done only for "Address!(Prim'Unrestricted_Access)" nodes.
749 -- At this stage other cases in which the designated type is
750 -- still a subprogram (instead of an E_Subprogram_Type) are
751 -- wrong because the semantics must have overridden the type of
752 -- the node with the type imposed by the context.
754 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
755 and then Etype (Parent (N)) = RTE (RE_Prim_Ptr)
756 then
757 Set_Etype (N, RTE (RE_Prim_Ptr));
759 else
760 declare
761 Subp : constant Entity_Id :=
762 Directly_Designated_Type (Typ);
763 Etyp : Entity_Id;
764 Extra : Entity_Id := Empty;
765 New_Formal : Entity_Id;
766 Old_Formal : Entity_Id := First_Formal (Subp);
767 Subp_Typ : Entity_Id;
769 begin
770 Subp_Typ := Create_Itype (E_Subprogram_Type, N);
771 Set_Etype (Subp_Typ, Etype (Subp));
772 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
774 if Present (Old_Formal) then
775 New_Formal := New_Copy (Old_Formal);
776 Set_First_Entity (Subp_Typ, New_Formal);
778 loop
779 Set_Scope (New_Formal, Subp_Typ);
780 Etyp := Etype (New_Formal);
782 -- Handle itypes. There is no need to duplicate
783 -- here the itypes associated with record types
784 -- (i.e the implicit full view of private types).
786 if Is_Itype (Etyp)
787 and then Ekind (Base_Type (Etyp)) /= E_Record_Type
788 then
789 Extra := New_Copy (Etyp);
790 Set_Parent (Extra, New_Formal);
791 Set_Etype (New_Formal, Extra);
792 Set_Scope (Extra, Subp_Typ);
793 end if;
795 Extra := New_Formal;
796 Next_Formal (Old_Formal);
797 exit when No (Old_Formal);
799 Set_Next_Entity (New_Formal,
800 New_Copy (Old_Formal));
801 Next_Entity (New_Formal);
802 end loop;
804 Set_Next_Entity (New_Formal, Empty);
805 Set_Last_Entity (Subp_Typ, Extra);
806 end if;
808 -- Now that the explicit formals have been duplicated,
809 -- any extra formals needed by the subprogram must be
810 -- created.
812 if Present (Extra) then
813 Set_Extra_Formal (Extra, Empty);
814 end if;
816 Create_Extra_Formals (Subp_Typ);
817 Set_Directly_Designated_Type (Typ, Subp_Typ);
818 end;
819 end if;
820 end if;
822 if Is_Access_Protected_Subprogram_Type (Btyp) then
823 Expand_Access_To_Protected_Op (N, Pref, Typ);
825 -- If prefix is a type name, this is a reference to the current
826 -- instance of the type, within its initialization procedure.
828 elsif Is_Entity_Name (Pref)
829 and then Is_Type (Entity (Pref))
830 then
831 declare
832 Par : Node_Id;
833 Formal : Entity_Id;
835 begin
836 -- If the current instance name denotes a task type, then
837 -- the access attribute is rewritten to be the name of the
838 -- "_task" parameter associated with the task type's task
839 -- procedure. An unchecked conversion is applied to ensure
840 -- a type match in cases of expander-generated calls (e.g.
841 -- init procs).
843 if Is_Task_Type (Entity (Pref)) then
844 Formal :=
845 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
846 while Present (Formal) loop
847 exit when Chars (Formal) = Name_uTask;
848 Next_Entity (Formal);
849 end loop;
851 pragma Assert (Present (Formal));
853 Rewrite (N,
854 Unchecked_Convert_To (Typ,
855 New_Occurrence_Of (Formal, Loc)));
856 Set_Etype (N, Typ);
858 -- The expression must appear in a default expression,
859 -- (which in the initialization procedure is the
860 -- right-hand side of an assignment), and not in a
861 -- discriminant constraint.
863 else
864 Par := Parent (N);
865 while Present (Par) loop
866 exit when Nkind (Par) = N_Assignment_Statement;
868 if Nkind (Par) = N_Component_Declaration then
869 return;
870 end if;
872 Par := Parent (Par);
873 end loop;
875 if Present (Par) then
876 Rewrite (N,
877 Make_Attribute_Reference (Loc,
878 Prefix => Make_Identifier (Loc, Name_uInit),
879 Attribute_Name => Attribute_Name (N)));
881 Analyze_And_Resolve (N, Typ);
882 end if;
883 end if;
884 end;
886 -- If the prefix of an Access attribute is a dereference of an
887 -- access parameter (or a renaming of such a dereference, or a
888 -- subcomponent of such a dereference) and the context is a
889 -- general access type (including the type of an object or
890 -- component with an access_definition, but not the anonymous
891 -- type of an access parameter or access discriminant), then
892 -- apply an accessibility check to the access parameter. We used
893 -- to rewrite the access parameter as a type conversion, but that
894 -- could only be done if the immediate prefix of the Access
895 -- attribute was the dereference, and didn't handle cases where
896 -- the attribute is applied to a subcomponent of the dereference,
897 -- since there's generally no available, appropriate access type
898 -- to convert to in that case. The attribute is passed as the
899 -- point to insert the check, because the access parameter may
900 -- come from a renaming, possibly in a different scope, and the
901 -- check must be associated with the attribute itself.
903 elsif Id = Attribute_Access
904 and then Nkind (Enc_Object) = N_Explicit_Dereference
905 and then Is_Entity_Name (Prefix (Enc_Object))
906 and then (Ekind (Btyp) = E_General_Access_Type
907 or else Is_Local_Anonymous_Access (Btyp))
908 and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind
909 and then Ekind (Etype (Entity (Prefix (Enc_Object))))
910 = E_Anonymous_Access_Type
911 and then Present (Extra_Accessibility
912 (Entity (Prefix (Enc_Object))))
913 then
914 Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N);
916 -- Ada 2005 (AI-251): If the designated type is an interface we
917 -- add an implicit conversion to force the displacement of the
918 -- pointer to reference the secondary dispatch table.
920 elsif Is_Interface (Btyp_DDT)
921 and then (Comes_From_Source (N)
922 or else Comes_From_Source (Ref_Object)
923 or else (Nkind (Ref_Object) in N_Has_Chars
924 and then Chars (Ref_Object) = Name_uInit))
925 then
926 if Nkind (Ref_Object) /= N_Explicit_Dereference then
928 -- No implicit conversion required if types match, or if
929 -- the prefix is the class_wide_type of the interface. In
930 -- either case passing an object of the interface type has
931 -- already set the pointer correctly.
933 if Btyp_DDT = Etype (Ref_Object)
934 or else (Is_Class_Wide_Type (Etype (Ref_Object))
935 and then
936 Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object))
937 then
938 null;
940 else
941 Rewrite (Prefix (N),
942 Convert_To (Btyp_DDT,
943 New_Copy_Tree (Prefix (N))));
945 Analyze_And_Resolve (Prefix (N), Btyp_DDT);
946 end if;
948 -- When the object is an explicit dereference, convert the
949 -- dereference's prefix.
951 else
952 declare
953 Obj_DDT : constant Entity_Id :=
954 Base_Type
955 (Directly_Designated_Type
956 (Etype (Prefix (Ref_Object))));
957 begin
958 -- No implicit conversion required if designated types
959 -- match.
961 if Obj_DDT /= Btyp_DDT
962 and then not (Is_Class_Wide_Type (Obj_DDT)
963 and then Etype (Obj_DDT) = Btyp_DDT)
964 then
965 Rewrite (N,
966 Convert_To (Typ,
967 New_Copy_Tree (Prefix (Ref_Object))));
968 Analyze_And_Resolve (N, Typ);
969 end if;
970 end;
971 end if;
972 end if;
973 end Access_Cases;
975 --------------
976 -- Adjacent --
977 --------------
979 -- Transforms 'Adjacent into a call to the floating-point attribute
980 -- function Adjacent in Fat_xxx (where xxx is the root type)
982 when Attribute_Adjacent =>
983 Expand_Fpt_Attribute_RR (N);
985 -------------
986 -- Address --
987 -------------
989 when Attribute_Address => Address : declare
990 Task_Proc : Entity_Id;
992 begin
993 -- If the prefix is a task or a task type, the useful address is that
994 -- of the procedure for the task body, i.e. the actual program unit.
995 -- We replace the original entity with that of the procedure.
997 if Is_Entity_Name (Pref)
998 and then Is_Task_Type (Entity (Pref))
999 then
1000 Task_Proc := Next_Entity (Root_Type (Ptyp));
1002 while Present (Task_Proc) loop
1003 exit when Ekind (Task_Proc) = E_Procedure
1004 and then Etype (First_Formal (Task_Proc)) =
1005 Corresponding_Record_Type (Ptyp);
1006 Next_Entity (Task_Proc);
1007 end loop;
1009 if Present (Task_Proc) then
1010 Set_Entity (Pref, Task_Proc);
1011 Set_Etype (Pref, Etype (Task_Proc));
1012 end if;
1014 -- Similarly, the address of a protected operation is the address
1015 -- of the corresponding protected body, regardless of the protected
1016 -- object from which it is selected.
1018 elsif Nkind (Pref) = N_Selected_Component
1019 and then Is_Subprogram (Entity (Selector_Name (Pref)))
1020 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
1021 then
1022 Rewrite (Pref,
1023 New_Occurrence_Of (
1024 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
1026 elsif Nkind (Pref) = N_Explicit_Dereference
1027 and then Ekind (Ptyp) = E_Subprogram_Type
1028 and then Convention (Ptyp) = Convention_Protected
1029 then
1030 -- The prefix is be a dereference of an access_to_protected_
1031 -- subprogram. The desired address is the second component of
1032 -- the record that represents the access.
1034 declare
1035 Addr : constant Entity_Id := Etype (N);
1036 Ptr : constant Node_Id := Prefix (Pref);
1037 T : constant Entity_Id :=
1038 Equivalent_Type (Base_Type (Etype (Ptr)));
1040 begin
1041 Rewrite (N,
1042 Unchecked_Convert_To (Addr,
1043 Make_Selected_Component (Loc,
1044 Prefix => Unchecked_Convert_To (T, Ptr),
1045 Selector_Name => New_Occurrence_Of (
1046 Next_Entity (First_Entity (T)), Loc))));
1048 Analyze_And_Resolve (N, Addr);
1049 end;
1051 -- Ada 2005 (AI-251): Class-wide interface objects are always
1052 -- "displaced" to reference the tag associated with the interface
1053 -- type. In order to obtain the real address of such objects we
1054 -- generate a call to a run-time subprogram that returns the base
1055 -- address of the object.
1057 -- This processing is not needed in the VM case, where dispatching
1058 -- issues are taken care of by the virtual machine.
1060 elsif Is_Class_Wide_Type (Ptyp)
1061 and then Is_Interface (Ptyp)
1062 and then Tagged_Type_Expansion
1063 and then not (Nkind (Pref) in N_Has_Entity
1064 and then Is_Subprogram (Entity (Pref)))
1065 then
1066 Rewrite (N,
1067 Make_Function_Call (Loc,
1068 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
1069 Parameter_Associations => New_List (
1070 Relocate_Node (N))));
1071 Analyze (N);
1072 return;
1073 end if;
1075 -- Deal with packed array reference, other cases are handled by
1076 -- the back end.
1078 if Involves_Packed_Array_Reference (Pref) then
1079 Expand_Packed_Address_Reference (N);
1080 end if;
1081 end Address;
1083 ---------------
1084 -- Alignment --
1085 ---------------
1087 when Attribute_Alignment => Alignment : declare
1088 New_Node : Node_Id;
1090 begin
1091 -- For class-wide types, X'Class'Alignment is transformed into a
1092 -- direct reference to the Alignment of the class type, so that the
1093 -- back end does not have to deal with the X'Class'Alignment
1094 -- reference.
1096 if Is_Entity_Name (Pref)
1097 and then Is_Class_Wide_Type (Entity (Pref))
1098 then
1099 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
1100 return;
1102 -- For x'Alignment applied to an object of a class wide type,
1103 -- transform X'Alignment into a call to the predefined primitive
1104 -- operation _Alignment applied to X.
1106 elsif Is_Class_Wide_Type (Ptyp) then
1108 -- No need to do anything else compiling under restriction
1109 -- No_Dispatching_Calls. During the semantic analysis we
1110 -- already notified such violation.
1112 if Restriction_Active (No_Dispatching_Calls) then
1113 return;
1114 end if;
1116 New_Node :=
1117 Make_Function_Call (Loc,
1118 Name => New_Reference_To
1119 (Find_Prim_Op (Ptyp, Name_uAlignment), Loc),
1120 Parameter_Associations => New_List (Pref));
1122 if Typ /= Standard_Integer then
1124 -- The context is a specific integer type with which the
1125 -- original attribute was compatible. The function has a
1126 -- specific type as well, so to preserve the compatibility
1127 -- we must convert explicitly.
1129 New_Node := Convert_To (Typ, New_Node);
1130 end if;
1132 Rewrite (N, New_Node);
1133 Analyze_And_Resolve (N, Typ);
1134 return;
1136 -- For all other cases, we just have to deal with the case of
1137 -- the fact that the result can be universal.
1139 else
1140 Apply_Universal_Integer_Attribute_Checks (N);
1141 end if;
1142 end Alignment;
1144 ---------------
1145 -- AST_Entry --
1146 ---------------
1148 when Attribute_AST_Entry => AST_Entry : declare
1149 Ttyp : Entity_Id;
1150 T_Id : Node_Id;
1151 Eent : Entity_Id;
1153 Entry_Ref : Node_Id;
1154 -- The reference to the entry or entry family
1156 Index : Node_Id;
1157 -- The index expression for an entry family reference, or
1158 -- the Empty if Entry_Ref references a simple entry.
1160 begin
1161 if Nkind (Pref) = N_Indexed_Component then
1162 Entry_Ref := Prefix (Pref);
1163 Index := First (Expressions (Pref));
1164 else
1165 Entry_Ref := Pref;
1166 Index := Empty;
1167 end if;
1169 -- Get expression for Task_Id and the entry entity
1171 if Nkind (Entry_Ref) = N_Selected_Component then
1172 T_Id :=
1173 Make_Attribute_Reference (Loc,
1174 Attribute_Name => Name_Identity,
1175 Prefix => Prefix (Entry_Ref));
1177 Ttyp := Etype (Prefix (Entry_Ref));
1178 Eent := Entity (Selector_Name (Entry_Ref));
1180 else
1181 T_Id :=
1182 Make_Function_Call (Loc,
1183 Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc));
1185 Eent := Entity (Entry_Ref);
1187 -- We have to find the enclosing task to get the task type
1188 -- There must be one, since we already validated this earlier
1190 Ttyp := Current_Scope;
1191 while not Is_Task_Type (Ttyp) loop
1192 Ttyp := Scope (Ttyp);
1193 end loop;
1194 end if;
1196 -- Now rewrite the attribute with a call to Create_AST_Handler
1198 Rewrite (N,
1199 Make_Function_Call (Loc,
1200 Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc),
1201 Parameter_Associations => New_List (
1202 T_Id,
1203 Entry_Index_Expression (Loc, Eent, Index, Ttyp))));
1205 Analyze_And_Resolve (N, RTE (RE_AST_Handler));
1206 end AST_Entry;
1208 ---------
1209 -- Bit --
1210 ---------
1212 -- We compute this if a packed array reference was present, otherwise we
1213 -- leave the computation up to the back end.
1215 when Attribute_Bit =>
1216 if Involves_Packed_Array_Reference (Pref) then
1217 Expand_Packed_Bit_Reference (N);
1218 else
1219 Apply_Universal_Integer_Attribute_Checks (N);
1220 end if;
1222 ------------------
1223 -- Bit_Position --
1224 ------------------
1226 -- We compute this if a component clause was present, otherwise we leave
1227 -- the computation up to the back end, since we don't know what layout
1228 -- will be chosen.
1230 -- Note that the attribute can apply to a naked record component
1231 -- in generated code (i.e. the prefix is an identifier that
1232 -- references the component or discriminant entity).
1234 when Attribute_Bit_Position => Bit_Position : declare
1235 CE : Entity_Id;
1237 begin
1238 if Nkind (Pref) = N_Identifier then
1239 CE := Entity (Pref);
1240 else
1241 CE := Entity (Selector_Name (Pref));
1242 end if;
1244 if Known_Static_Component_Bit_Offset (CE) then
1245 Rewrite (N,
1246 Make_Integer_Literal (Loc,
1247 Intval => Component_Bit_Offset (CE)));
1248 Analyze_And_Resolve (N, Typ);
1250 else
1251 Apply_Universal_Integer_Attribute_Checks (N);
1252 end if;
1253 end Bit_Position;
1255 ------------------
1256 -- Body_Version --
1257 ------------------
1259 -- A reference to P'Body_Version or P'Version is expanded to
1261 -- Vnn : Unsigned;
1262 -- pragma Import (C, Vnn, "uuuuT");
1263 -- ...
1264 -- Get_Version_String (Vnn)
1266 -- where uuuu is the unit name (dots replaced by double underscore)
1267 -- and T is B for the cases of Body_Version, or Version applied to a
1268 -- subprogram acting as its own spec, and S for Version applied to a
1269 -- subprogram spec or package. This sequence of code references the
1270 -- the unsigned constant created in the main program by the binder.
1272 -- A special exception occurs for Standard, where the string returned
1273 -- is a copy of the library string in gnatvsn.ads.
1275 when Attribute_Body_Version | Attribute_Version => Version : declare
1276 E : constant Entity_Id := Make_Temporary (Loc, 'V');
1277 Pent : Entity_Id;
1278 S : String_Id;
1280 begin
1281 -- If not library unit, get to containing library unit
1283 Pent := Entity (Pref);
1284 while Pent /= Standard_Standard
1285 and then Scope (Pent) /= Standard_Standard
1286 and then not Is_Child_Unit (Pent)
1287 loop
1288 Pent := Scope (Pent);
1289 end loop;
1291 -- Special case Standard and Standard.ASCII
1293 if Pent = Standard_Standard or else Pent = Standard_ASCII then
1294 Rewrite (N,
1295 Make_String_Literal (Loc,
1296 Strval => Verbose_Library_Version));
1298 -- All other cases
1300 else
1301 -- Build required string constant
1303 Get_Name_String (Get_Unit_Name (Pent));
1305 Start_String;
1306 for J in 1 .. Name_Len - 2 loop
1307 if Name_Buffer (J) = '.' then
1308 Store_String_Chars ("__");
1309 else
1310 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
1311 end if;
1312 end loop;
1314 -- Case of subprogram acting as its own spec, always use body
1316 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
1317 and then Nkind (Parent (Declaration_Node (Pent))) =
1318 N_Subprogram_Body
1319 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
1320 then
1321 Store_String_Chars ("B");
1323 -- Case of no body present, always use spec
1325 elsif not Unit_Requires_Body (Pent) then
1326 Store_String_Chars ("S");
1328 -- Otherwise use B for Body_Version, S for spec
1330 elsif Id = Attribute_Body_Version then
1331 Store_String_Chars ("B");
1332 else
1333 Store_String_Chars ("S");
1334 end if;
1336 S := End_String;
1337 Lib.Version_Referenced (S);
1339 -- Insert the object declaration
1341 Insert_Actions (N, New_List (
1342 Make_Object_Declaration (Loc,
1343 Defining_Identifier => E,
1344 Object_Definition =>
1345 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
1347 -- Set entity as imported with correct external name
1349 Set_Is_Imported (E);
1350 Set_Interface_Name (E, Make_String_Literal (Loc, S));
1352 -- Set entity as internal to ensure proper Sprint output of its
1353 -- implicit importation.
1355 Set_Is_Internal (E);
1357 -- And now rewrite original reference
1359 Rewrite (N,
1360 Make_Function_Call (Loc,
1361 Name => New_Reference_To (RTE (RE_Get_Version_String), Loc),
1362 Parameter_Associations => New_List (
1363 New_Occurrence_Of (E, Loc))));
1364 end if;
1366 Analyze_And_Resolve (N, RTE (RE_Version_String));
1367 end Version;
1369 -------------
1370 -- Ceiling --
1371 -------------
1373 -- Transforms 'Ceiling into a call to the floating-point attribute
1374 -- function Ceiling in Fat_xxx (where xxx is the root type)
1376 when Attribute_Ceiling =>
1377 Expand_Fpt_Attribute_R (N);
1379 --------------
1380 -- Callable --
1381 --------------
1383 -- Transforms 'Callable attribute into a call to the Callable function
1385 when Attribute_Callable => Callable :
1386 begin
1387 -- We have an object of a task interface class-wide type as a prefix
1388 -- to Callable. Generate:
1389 -- callable (Task_Id (Pref._disp_get_task_id));
1391 if Ada_Version >= Ada_05
1392 and then Ekind (Ptyp) = E_Class_Wide_Type
1393 and then Is_Interface (Ptyp)
1394 and then Is_Task_Interface (Ptyp)
1395 then
1396 Rewrite (N,
1397 Make_Function_Call (Loc,
1398 Name =>
1399 New_Reference_To (RTE (RE_Callable), Loc),
1400 Parameter_Associations => New_List (
1401 Make_Unchecked_Type_Conversion (Loc,
1402 Subtype_Mark =>
1403 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
1404 Expression =>
1405 Make_Selected_Component (Loc,
1406 Prefix =>
1407 New_Copy_Tree (Pref),
1408 Selector_Name =>
1409 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
1411 else
1412 Rewrite (N,
1413 Build_Call_With_Task (Pref, RTE (RE_Callable)));
1414 end if;
1416 Analyze_And_Resolve (N, Standard_Boolean);
1417 end Callable;
1419 ------------
1420 -- Caller --
1421 ------------
1423 -- Transforms 'Caller attribute into a call to either the
1424 -- Task_Entry_Caller or the Protected_Entry_Caller function.
1426 when Attribute_Caller => Caller : declare
1427 Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id);
1428 Ent : constant Entity_Id := Entity (Pref);
1429 Conctype : constant Entity_Id := Scope (Ent);
1430 Nest_Depth : Integer := 0;
1431 Name : Node_Id;
1432 S : Entity_Id;
1434 begin
1435 -- Protected case
1437 if Is_Protected_Type (Conctype) then
1438 case Corresponding_Runtime_Package (Conctype) is
1439 when System_Tasking_Protected_Objects_Entries =>
1440 Name :=
1441 New_Reference_To
1442 (RTE (RE_Protected_Entry_Caller), Loc);
1444 when System_Tasking_Protected_Objects_Single_Entry =>
1445 Name :=
1446 New_Reference_To
1447 (RTE (RE_Protected_Single_Entry_Caller), Loc);
1449 when others =>
1450 raise Program_Error;
1451 end case;
1453 Rewrite (N,
1454 Unchecked_Convert_To (Id_Kind,
1455 Make_Function_Call (Loc,
1456 Name => Name,
1457 Parameter_Associations => New_List (
1458 New_Reference_To
1459 (Find_Protection_Object (Current_Scope), Loc)))));
1461 -- Task case
1463 else
1464 -- Determine the nesting depth of the E'Caller attribute, that
1465 -- is, how many accept statements are nested within the accept
1466 -- statement for E at the point of E'Caller. The runtime uses
1467 -- this depth to find the specified entry call.
1469 for J in reverse 0 .. Scope_Stack.Last loop
1470 S := Scope_Stack.Table (J).Entity;
1472 -- We should not reach the scope of the entry, as it should
1473 -- already have been checked in Sem_Attr that this attribute
1474 -- reference is within a matching accept statement.
1476 pragma Assert (S /= Conctype);
1478 if S = Ent then
1479 exit;
1481 elsif Is_Entry (S) then
1482 Nest_Depth := Nest_Depth + 1;
1483 end if;
1484 end loop;
1486 Rewrite (N,
1487 Unchecked_Convert_To (Id_Kind,
1488 Make_Function_Call (Loc,
1489 Name =>
1490 New_Reference_To (RTE (RE_Task_Entry_Caller), Loc),
1491 Parameter_Associations => New_List (
1492 Make_Integer_Literal (Loc,
1493 Intval => Int (Nest_Depth))))));
1494 end if;
1496 Analyze_And_Resolve (N, Id_Kind);
1497 end Caller;
1499 -------------
1500 -- Compose --
1501 -------------
1503 -- Transforms 'Compose into a call to the floating-point attribute
1504 -- function Compose in Fat_xxx (where xxx is the root type)
1506 -- Note: we strictly should have special code here to deal with the
1507 -- case of absurdly negative arguments (less than Integer'First)
1508 -- which will return a (signed) zero value, but it hardly seems
1509 -- worth the effort. Absurdly large positive arguments will raise
1510 -- constraint error which is fine.
1512 when Attribute_Compose =>
1513 Expand_Fpt_Attribute_RI (N);
1515 -----------------
1516 -- Constrained --
1517 -----------------
1519 when Attribute_Constrained => Constrained : declare
1520 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
1522 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
1523 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
1524 -- view of an aliased object whose subtype is constrained.
1526 ---------------------------------
1527 -- Is_Constrained_Aliased_View --
1528 ---------------------------------
1530 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
1531 E : Entity_Id;
1533 begin
1534 if Is_Entity_Name (Obj) then
1535 E := Entity (Obj);
1537 if Present (Renamed_Object (E)) then
1538 return Is_Constrained_Aliased_View (Renamed_Object (E));
1539 else
1540 return Is_Aliased (E) and then Is_Constrained (Etype (E));
1541 end if;
1543 else
1544 return Is_Aliased_View (Obj)
1545 and then
1546 (Is_Constrained (Etype (Obj))
1547 or else (Nkind (Obj) = N_Explicit_Dereference
1548 and then
1549 not Has_Constrained_Partial_View
1550 (Base_Type (Etype (Obj)))));
1551 end if;
1552 end Is_Constrained_Aliased_View;
1554 -- Start of processing for Constrained
1556 begin
1557 -- Reference to a parameter where the value is passed as an extra
1558 -- actual, corresponding to the extra formal referenced by the
1559 -- Extra_Constrained field of the corresponding formal. If this
1560 -- is an entry in-parameter, it is replaced by a constant renaming
1561 -- for which Extra_Constrained is never created.
1563 if Present (Formal_Ent)
1564 and then Ekind (Formal_Ent) /= E_Constant
1565 and then Present (Extra_Constrained (Formal_Ent))
1566 then
1567 Rewrite (N,
1568 New_Occurrence_Of
1569 (Extra_Constrained (Formal_Ent), Sloc (N)));
1571 -- For variables with a Extra_Constrained field, we use the
1572 -- corresponding entity.
1574 elsif Nkind (Pref) = N_Identifier
1575 and then Ekind (Entity (Pref)) = E_Variable
1576 and then Present (Extra_Constrained (Entity (Pref)))
1577 then
1578 Rewrite (N,
1579 New_Occurrence_Of
1580 (Extra_Constrained (Entity (Pref)), Sloc (N)));
1582 -- For all other entity names, we can tell at compile time
1584 elsif Is_Entity_Name (Pref) then
1585 declare
1586 Ent : constant Entity_Id := Entity (Pref);
1587 Res : Boolean;
1589 begin
1590 -- (RM J.4) obsolescent cases
1592 if Is_Type (Ent) then
1594 -- Private type
1596 if Is_Private_Type (Ent) then
1597 Res := not Has_Discriminants (Ent)
1598 or else Is_Constrained (Ent);
1600 -- It not a private type, must be a generic actual type
1601 -- that corresponded to a private type. We know that this
1602 -- correspondence holds, since otherwise the reference
1603 -- within the generic template would have been illegal.
1605 else
1606 if Is_Composite_Type (Underlying_Type (Ent)) then
1607 Res := Is_Constrained (Ent);
1608 else
1609 Res := True;
1610 end if;
1611 end if;
1613 -- If the prefix is not a variable or is aliased, then
1614 -- definitely true; if it's a formal parameter without an
1615 -- associated extra formal, then treat it as constrained.
1617 -- Ada 2005 (AI-363): An aliased prefix must be known to be
1618 -- constrained in order to set the attribute to True.
1620 elsif not Is_Variable (Pref)
1621 or else Present (Formal_Ent)
1622 or else (Ada_Version < Ada_05
1623 and then Is_Aliased_View (Pref))
1624 or else (Ada_Version >= Ada_05
1625 and then Is_Constrained_Aliased_View (Pref))
1626 then
1627 Res := True;
1629 -- Variable case, look at type to see if it is constrained.
1630 -- Note that the one case where this is not accurate (the
1631 -- procedure formal case), has been handled above.
1633 -- We use the Underlying_Type here (and below) in case the
1634 -- type is private without discriminants, but the full type
1635 -- has discriminants. This case is illegal, but we generate it
1636 -- internally for passing to the Extra_Constrained parameter.
1638 else
1639 Res := Is_Constrained (Underlying_Type (Etype (Ent)));
1640 end if;
1642 Rewrite (N,
1643 New_Reference_To (Boolean_Literals (Res), Loc));
1644 end;
1646 -- Prefix is not an entity name. These are also cases where we can
1647 -- always tell at compile time by looking at the form and type of the
1648 -- prefix. If an explicit dereference of an object with constrained
1649 -- partial view, this is unconstrained (Ada 2005 AI-363).
1651 else
1652 Rewrite (N,
1653 New_Reference_To (
1654 Boolean_Literals (
1655 not Is_Variable (Pref)
1656 or else
1657 (Nkind (Pref) = N_Explicit_Dereference
1658 and then
1659 not Has_Constrained_Partial_View (Base_Type (Ptyp)))
1660 or else Is_Constrained (Underlying_Type (Ptyp))),
1661 Loc));
1662 end if;
1664 Analyze_And_Resolve (N, Standard_Boolean);
1665 end Constrained;
1667 ---------------
1668 -- Copy_Sign --
1669 ---------------
1671 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1672 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1674 when Attribute_Copy_Sign =>
1675 Expand_Fpt_Attribute_RR (N);
1677 -----------
1678 -- Count --
1679 -----------
1681 -- Transforms 'Count attribute into a call to the Count function
1683 when Attribute_Count => Count : declare
1684 Call : Node_Id;
1685 Conctyp : Entity_Id;
1686 Entnam : Node_Id;
1687 Entry_Id : Entity_Id;
1688 Index : Node_Id;
1689 Name : Node_Id;
1691 begin
1692 -- If the prefix is a member of an entry family, retrieve both
1693 -- entry name and index. For a simple entry there is no index.
1695 if Nkind (Pref) = N_Indexed_Component then
1696 Entnam := Prefix (Pref);
1697 Index := First (Expressions (Pref));
1698 else
1699 Entnam := Pref;
1700 Index := Empty;
1701 end if;
1703 Entry_Id := Entity (Entnam);
1705 -- Find the concurrent type in which this attribute is referenced
1706 -- (there had better be one).
1708 Conctyp := Current_Scope;
1709 while not Is_Concurrent_Type (Conctyp) loop
1710 Conctyp := Scope (Conctyp);
1711 end loop;
1713 -- Protected case
1715 if Is_Protected_Type (Conctyp) then
1716 case Corresponding_Runtime_Package (Conctyp) is
1717 when System_Tasking_Protected_Objects_Entries =>
1718 Name := New_Reference_To (RTE (RE_Protected_Count), Loc);
1720 Call :=
1721 Make_Function_Call (Loc,
1722 Name => Name,
1723 Parameter_Associations => New_List (
1724 New_Reference_To
1725 (Find_Protection_Object (Current_Scope), Loc),
1726 Entry_Index_Expression
1727 (Loc, Entry_Id, Index, Scope (Entry_Id))));
1729 when System_Tasking_Protected_Objects_Single_Entry =>
1730 Name :=
1731 New_Reference_To (RTE (RE_Protected_Count_Entry), Loc);
1733 Call :=
1734 Make_Function_Call (Loc,
1735 Name => Name,
1736 Parameter_Associations => New_List (
1737 New_Reference_To
1738 (Find_Protection_Object (Current_Scope), Loc)));
1740 when others =>
1741 raise Program_Error;
1742 end case;
1744 -- Task case
1746 else
1747 Call :=
1748 Make_Function_Call (Loc,
1749 Name => New_Reference_To (RTE (RE_Task_Count), Loc),
1750 Parameter_Associations => New_List (
1751 Entry_Index_Expression (Loc,
1752 Entry_Id, Index, Scope (Entry_Id))));
1753 end if;
1755 -- The call returns type Natural but the context is universal integer
1756 -- so any integer type is allowed. The attribute was already resolved
1757 -- so its Etype is the required result type. If the base type of the
1758 -- context type is other than Standard.Integer we put in a conversion
1759 -- to the required type. This can be a normal typed conversion since
1760 -- both input and output types of the conversion are integer types
1762 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
1763 Rewrite (N, Convert_To (Typ, Call));
1764 else
1765 Rewrite (N, Call);
1766 end if;
1768 Analyze_And_Resolve (N, Typ);
1769 end Count;
1771 ---------------
1772 -- Elab_Body --
1773 ---------------
1775 -- This processing is shared by Elab_Spec
1777 -- What we do is to insert the following declarations
1779 -- procedure tnn;
1780 -- pragma Import (C, enn, "name___elabb/s");
1782 -- and then the Elab_Body/Spec attribute is replaced by a reference
1783 -- to this defining identifier.
1785 when Attribute_Elab_Body |
1786 Attribute_Elab_Spec =>
1788 Elab_Body : declare
1789 Ent : constant Entity_Id := Make_Temporary (Loc, 'E');
1790 Str : String_Id;
1791 Lang : Node_Id;
1793 procedure Make_Elab_String (Nod : Node_Id);
1794 -- Given Nod, an identifier, or a selected component, put the
1795 -- image into the current string literal, with double underline
1796 -- between components.
1798 ----------------------
1799 -- Make_Elab_String --
1800 ----------------------
1802 procedure Make_Elab_String (Nod : Node_Id) is
1803 begin
1804 if Nkind (Nod) = N_Selected_Component then
1805 Make_Elab_String (Prefix (Nod));
1807 case VM_Target is
1808 when JVM_Target =>
1809 Store_String_Char ('$');
1810 when CLI_Target =>
1811 Store_String_Char ('.');
1812 when No_VM =>
1813 Store_String_Char ('_');
1814 Store_String_Char ('_');
1815 end case;
1817 Get_Name_String (Chars (Selector_Name (Nod)));
1819 else
1820 pragma Assert (Nkind (Nod) = N_Identifier);
1821 Get_Name_String (Chars (Nod));
1822 end if;
1824 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1825 end Make_Elab_String;
1827 -- Start of processing for Elab_Body/Elab_Spec
1829 begin
1830 -- First we need to prepare the string literal for the name of
1831 -- the elaboration routine to be referenced.
1833 Start_String;
1834 Make_Elab_String (Pref);
1836 if VM_Target = No_VM then
1837 Store_String_Chars ("___elab");
1838 Lang := Make_Identifier (Loc, Name_C);
1839 else
1840 Store_String_Chars ("._elab");
1841 Lang := Make_Identifier (Loc, Name_Ada);
1842 end if;
1844 if Id = Attribute_Elab_Body then
1845 Store_String_Char ('b');
1846 else
1847 Store_String_Char ('s');
1848 end if;
1850 Str := End_String;
1852 Insert_Actions (N, New_List (
1853 Make_Subprogram_Declaration (Loc,
1854 Specification =>
1855 Make_Procedure_Specification (Loc,
1856 Defining_Unit_Name => Ent)),
1858 Make_Pragma (Loc,
1859 Chars => Name_Import,
1860 Pragma_Argument_Associations => New_List (
1861 Make_Pragma_Argument_Association (Loc,
1862 Expression => Lang),
1864 Make_Pragma_Argument_Association (Loc,
1865 Expression =>
1866 Make_Identifier (Loc, Chars (Ent))),
1868 Make_Pragma_Argument_Association (Loc,
1869 Expression =>
1870 Make_String_Literal (Loc, Str))))));
1872 Set_Entity (N, Ent);
1873 Rewrite (N, New_Occurrence_Of (Ent, Loc));
1874 end Elab_Body;
1876 ----------------
1877 -- Elaborated --
1878 ----------------
1880 -- Elaborated is always True for preelaborated units, predefined units,
1881 -- pure units and units which have Elaborate_Body pragmas. These units
1882 -- have no elaboration entity.
1884 -- Note: The Elaborated attribute is never passed to the back end
1886 when Attribute_Elaborated => Elaborated : declare
1887 Ent : constant Entity_Id := Entity (Pref);
1889 begin
1890 if Present (Elaboration_Entity (Ent)) then
1891 Rewrite (N,
1892 New_Occurrence_Of (Elaboration_Entity (Ent), Loc));
1893 else
1894 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
1895 end if;
1896 end Elaborated;
1898 --------------
1899 -- Enum_Rep --
1900 --------------
1902 when Attribute_Enum_Rep => Enum_Rep :
1903 begin
1904 -- X'Enum_Rep (Y) expands to
1906 -- target-type (Y)
1908 -- This is simply a direct conversion from the enumeration type to
1909 -- the target integer type, which is treated by the back end as a
1910 -- normal integer conversion, treating the enumeration type as an
1911 -- integer, which is exactly what we want! We set Conversion_OK to
1912 -- make sure that the analyzer does not complain about what otherwise
1913 -- might be an illegal conversion.
1915 if Is_Non_Empty_List (Exprs) then
1916 Rewrite (N,
1917 OK_Convert_To (Typ, Relocate_Node (First (Exprs))));
1919 -- X'Enum_Rep where X is an enumeration literal is replaced by
1920 -- the literal value.
1922 elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then
1923 Rewrite (N,
1924 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref))));
1926 -- If this is a renaming of a literal, recover the representation
1927 -- of the original.
1929 elsif Ekind (Entity (Pref)) = E_Constant
1930 and then Present (Renamed_Object (Entity (Pref)))
1931 and then
1932 Ekind (Entity (Renamed_Object (Entity (Pref))))
1933 = E_Enumeration_Literal
1934 then
1935 Rewrite (N,
1936 Make_Integer_Literal (Loc,
1937 Enumeration_Rep (Entity (Renamed_Object (Entity (Pref))))));
1939 -- X'Enum_Rep where X is an object does a direct unchecked conversion
1940 -- of the object value, as described for the type case above.
1942 else
1943 Rewrite (N,
1944 OK_Convert_To (Typ, Relocate_Node (Pref)));
1945 end if;
1947 Set_Etype (N, Typ);
1948 Analyze_And_Resolve (N, Typ);
1949 end Enum_Rep;
1951 --------------
1952 -- Enum_Val --
1953 --------------
1955 when Attribute_Enum_Val => Enum_Val : declare
1956 Expr : Node_Id;
1957 Btyp : constant Entity_Id := Base_Type (Ptyp);
1959 begin
1960 -- X'Enum_Val (Y) expands to
1962 -- [constraint_error when _rep_to_pos (Y, False) = -1, msg]
1963 -- X!(Y);
1965 Expr := Unchecked_Convert_To (Ptyp, First (Exprs));
1967 Insert_Action (N,
1968 Make_Raise_Constraint_Error (Loc,
1969 Condition =>
1970 Make_Op_Eq (Loc,
1971 Left_Opnd =>
1972 Make_Function_Call (Loc,
1973 Name =>
1974 New_Reference_To (TSS (Btyp, TSS_Rep_To_Pos), Loc),
1975 Parameter_Associations => New_List (
1976 Relocate_Node (Duplicate_Subexpr (Expr)),
1977 New_Occurrence_Of (Standard_False, Loc))),
1979 Right_Opnd => Make_Integer_Literal (Loc, -1)),
1980 Reason => CE_Range_Check_Failed));
1982 Rewrite (N, Expr);
1983 Analyze_And_Resolve (N, Ptyp);
1984 end Enum_Val;
1986 --------------
1987 -- Exponent --
1988 --------------
1990 -- Transforms 'Exponent into a call to the floating-point attribute
1991 -- function Exponent in Fat_xxx (where xxx is the root type)
1993 when Attribute_Exponent =>
1994 Expand_Fpt_Attribute_R (N);
1996 ------------------
1997 -- External_Tag --
1998 ------------------
2000 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
2002 when Attribute_External_Tag => External_Tag :
2003 begin
2004 Rewrite (N,
2005 Make_Function_Call (Loc,
2006 Name => New_Reference_To (RTE (RE_External_Tag), Loc),
2007 Parameter_Associations => New_List (
2008 Make_Attribute_Reference (Loc,
2009 Attribute_Name => Name_Tag,
2010 Prefix => Prefix (N)))));
2012 Analyze_And_Resolve (N, Standard_String);
2013 end External_Tag;
2015 -----------
2016 -- First --
2017 -----------
2019 when Attribute_First =>
2021 -- If the prefix type is a constrained packed array type which
2022 -- already has a Packed_Array_Type representation defined, then
2023 -- replace this attribute with a direct reference to 'First of the
2024 -- appropriate index subtype (since otherwise the back end will try
2025 -- to give us the value of 'First for this implementation type).
2027 if Is_Constrained_Packed_Array (Ptyp) then
2028 Rewrite (N,
2029 Make_Attribute_Reference (Loc,
2030 Attribute_Name => Name_First,
2031 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2032 Analyze_And_Resolve (N, Typ);
2034 elsif Is_Access_Type (Ptyp) then
2035 Apply_Access_Check (N);
2036 end if;
2038 ---------------
2039 -- First_Bit --
2040 ---------------
2042 -- Compute this if component clause was present, otherwise we leave the
2043 -- computation to be completed in the back-end, since we don't know what
2044 -- layout will be chosen.
2046 when Attribute_First_Bit => First_Bit : declare
2047 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2049 begin
2050 if Known_Static_Component_Bit_Offset (CE) then
2051 Rewrite (N,
2052 Make_Integer_Literal (Loc,
2053 Component_Bit_Offset (CE) mod System_Storage_Unit));
2055 Analyze_And_Resolve (N, Typ);
2057 else
2058 Apply_Universal_Integer_Attribute_Checks (N);
2059 end if;
2060 end First_Bit;
2062 -----------------
2063 -- Fixed_Value --
2064 -----------------
2066 -- We transform:
2068 -- fixtype'Fixed_Value (integer-value)
2070 -- into
2072 -- fixtype(integer-value)
2074 -- We do all the required analysis of the conversion here, because we do
2075 -- not want this to go through the fixed-point conversion circuits. Note
2076 -- that the back end always treats fixed-point as equivalent to the
2077 -- corresponding integer type anyway.
2079 when Attribute_Fixed_Value => Fixed_Value :
2080 begin
2081 Rewrite (N,
2082 Make_Type_Conversion (Loc,
2083 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2084 Expression => Relocate_Node (First (Exprs))));
2085 Set_Etype (N, Entity (Pref));
2086 Set_Analyzed (N);
2088 -- Note: it might appear that a properly analyzed unchecked conversion
2089 -- would be just fine here, but that's not the case, since the full
2090 -- range checks performed by the following call are critical!
2092 Apply_Type_Conversion_Checks (N);
2093 end Fixed_Value;
2095 -----------
2096 -- Floor --
2097 -----------
2099 -- Transforms 'Floor into a call to the floating-point attribute
2100 -- function Floor in Fat_xxx (where xxx is the root type)
2102 when Attribute_Floor =>
2103 Expand_Fpt_Attribute_R (N);
2105 ----------
2106 -- Fore --
2107 ----------
2109 -- For the fixed-point type Typ:
2111 -- Typ'Fore
2113 -- expands into
2115 -- Result_Type (System.Fore (Universal_Real (Type'First)),
2116 -- Universal_Real (Type'Last))
2118 -- Note that we know that the type is a non-static subtype, or Fore
2119 -- would have itself been computed dynamically in Eval_Attribute.
2121 when Attribute_Fore => Fore : begin
2122 Rewrite (N,
2123 Convert_To (Typ,
2124 Make_Function_Call (Loc,
2125 Name => New_Reference_To (RTE (RE_Fore), Loc),
2127 Parameter_Associations => New_List (
2128 Convert_To (Universal_Real,
2129 Make_Attribute_Reference (Loc,
2130 Prefix => New_Reference_To (Ptyp, Loc),
2131 Attribute_Name => Name_First)),
2133 Convert_To (Universal_Real,
2134 Make_Attribute_Reference (Loc,
2135 Prefix => New_Reference_To (Ptyp, Loc),
2136 Attribute_Name => Name_Last))))));
2138 Analyze_And_Resolve (N, Typ);
2139 end Fore;
2141 --------------
2142 -- Fraction --
2143 --------------
2145 -- Transforms 'Fraction into a call to the floating-point attribute
2146 -- function Fraction in Fat_xxx (where xxx is the root type)
2148 when Attribute_Fraction =>
2149 Expand_Fpt_Attribute_R (N);
2151 --------------
2152 -- From_Any --
2153 --------------
2155 when Attribute_From_Any => From_Any : declare
2156 P_Type : constant Entity_Id := Etype (Pref);
2157 Decls : constant List_Id := New_List;
2158 begin
2159 Rewrite (N,
2160 Build_From_Any_Call (P_Type,
2161 Relocate_Node (First (Exprs)),
2162 Decls));
2163 Insert_Actions (N, Decls);
2164 Analyze_And_Resolve (N, P_Type);
2165 end From_Any;
2167 --------------
2168 -- Identity --
2169 --------------
2171 -- For an exception returns a reference to the exception data:
2172 -- Exception_Id!(Prefix'Reference)
2174 -- For a task it returns a reference to the _task_id component of
2175 -- corresponding record:
2177 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
2179 -- in Ada.Task_Identification
2181 when Attribute_Identity => Identity : declare
2182 Id_Kind : Entity_Id;
2184 begin
2185 if Ptyp = Standard_Exception_Type then
2186 Id_Kind := RTE (RE_Exception_Id);
2188 if Present (Renamed_Object (Entity (Pref))) then
2189 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
2190 end if;
2192 Rewrite (N,
2193 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
2194 else
2195 Id_Kind := RTE (RO_AT_Task_Id);
2197 -- If the prefix is a task interface, the Task_Id is obtained
2198 -- dynamically through a dispatching call, as for other task
2199 -- attributes applied to interfaces.
2201 if Ada_Version >= Ada_05
2202 and then Ekind (Ptyp) = E_Class_Wide_Type
2203 and then Is_Interface (Ptyp)
2204 and then Is_Task_Interface (Ptyp)
2205 then
2206 Rewrite (N,
2207 Unchecked_Convert_To (Id_Kind,
2208 Make_Selected_Component (Loc,
2209 Prefix =>
2210 New_Copy_Tree (Pref),
2211 Selector_Name =>
2212 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))));
2214 else
2215 Rewrite (N,
2216 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
2217 end if;
2218 end if;
2220 Analyze_And_Resolve (N, Id_Kind);
2221 end Identity;
2223 -----------
2224 -- Image --
2225 -----------
2227 -- Image attribute is handled in separate unit Exp_Imgv
2229 when Attribute_Image =>
2230 Exp_Imgv.Expand_Image_Attribute (N);
2232 ---------
2233 -- Img --
2234 ---------
2236 -- X'Img is expanded to typ'Image (X), where typ is the type of X
2238 when Attribute_Img => Img :
2239 begin
2240 Rewrite (N,
2241 Make_Attribute_Reference (Loc,
2242 Prefix => New_Reference_To (Ptyp, Loc),
2243 Attribute_Name => Name_Image,
2244 Expressions => New_List (Relocate_Node (Pref))));
2246 Analyze_And_Resolve (N, Standard_String);
2247 end Img;
2249 -----------
2250 -- Input --
2251 -----------
2253 when Attribute_Input => Input : declare
2254 P_Type : constant Entity_Id := Entity (Pref);
2255 B_Type : constant Entity_Id := Base_Type (P_Type);
2256 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2257 Strm : constant Node_Id := First (Exprs);
2258 Fname : Entity_Id;
2259 Decl : Node_Id;
2260 Call : Node_Id;
2261 Prag : Node_Id;
2262 Arg2 : Node_Id;
2263 Rfunc : Node_Id;
2265 Cntrl : Node_Id := Empty;
2266 -- Value for controlling argument in call. Always Empty except in
2267 -- the dispatching (class-wide type) case, where it is a reference
2268 -- to the dummy object initialized to the right internal tag.
2270 procedure Freeze_Stream_Subprogram (F : Entity_Id);
2271 -- The expansion of the attribute reference may generate a call to
2272 -- a user-defined stream subprogram that is frozen by the call. This
2273 -- can lead to access-before-elaboration problem if the reference
2274 -- appears in an object declaration and the subprogram body has not
2275 -- been seen. The freezing of the subprogram requires special code
2276 -- because it appears in an expanded context where expressions do
2277 -- not freeze their constituents.
2279 ------------------------------
2280 -- Freeze_Stream_Subprogram --
2281 ------------------------------
2283 procedure Freeze_Stream_Subprogram (F : Entity_Id) is
2284 Decl : constant Node_Id := Unit_Declaration_Node (F);
2285 Bod : Node_Id;
2287 begin
2288 -- If this is user-defined subprogram, the corresponding
2289 -- stream function appears as a renaming-as-body, and the
2290 -- user subprogram must be retrieved by tree traversal.
2292 if Present (Decl)
2293 and then Nkind (Decl) = N_Subprogram_Declaration
2294 and then Present (Corresponding_Body (Decl))
2295 then
2296 Bod := Corresponding_Body (Decl);
2298 if Nkind (Unit_Declaration_Node (Bod)) =
2299 N_Subprogram_Renaming_Declaration
2300 then
2301 Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
2302 end if;
2303 end if;
2304 end Freeze_Stream_Subprogram;
2306 -- Start of processing for Input
2308 begin
2309 -- If no underlying type, we have an error that will be diagnosed
2310 -- elsewhere, so here we just completely ignore the expansion.
2312 if No (U_Type) then
2313 return;
2314 end if;
2316 -- If there is a TSS for Input, just call it
2318 Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
2320 if Present (Fname) then
2321 null;
2323 else
2324 -- If there is a Stream_Convert pragma, use it, we rewrite
2326 -- sourcetyp'Input (stream)
2328 -- as
2330 -- sourcetyp (streamread (strmtyp'Input (stream)));
2332 -- where streamread is the given Read function that converts an
2333 -- argument of type strmtyp to type sourcetyp or a type from which
2334 -- it is derived (extra conversion required for the derived case).
2336 Prag := Get_Stream_Convert_Pragma (P_Type);
2338 if Present (Prag) then
2339 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
2340 Rfunc := Entity (Expression (Arg2));
2342 Rewrite (N,
2343 Convert_To (B_Type,
2344 Make_Function_Call (Loc,
2345 Name => New_Occurrence_Of (Rfunc, Loc),
2346 Parameter_Associations => New_List (
2347 Make_Attribute_Reference (Loc,
2348 Prefix =>
2349 New_Occurrence_Of
2350 (Etype (First_Formal (Rfunc)), Loc),
2351 Attribute_Name => Name_Input,
2352 Expressions => Exprs)))));
2354 Analyze_And_Resolve (N, B_Type);
2355 return;
2357 -- Elementary types
2359 elsif Is_Elementary_Type (U_Type) then
2361 -- A special case arises if we have a defined _Read routine,
2362 -- since in this case we are required to call this routine.
2364 if Present (TSS (Base_Type (U_Type), TSS_Stream_Read)) then
2365 Build_Record_Or_Elementary_Input_Function
2366 (Loc, U_Type, Decl, Fname);
2367 Insert_Action (N, Decl);
2369 -- For normal cases, we call the I_xxx routine directly
2371 else
2372 Rewrite (N, Build_Elementary_Input_Call (N));
2373 Analyze_And_Resolve (N, P_Type);
2374 return;
2375 end if;
2377 -- Array type case
2379 elsif Is_Array_Type (U_Type) then
2380 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
2381 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2383 -- Dispatching case with class-wide type
2385 elsif Is_Class_Wide_Type (P_Type) then
2387 -- No need to do anything else compiling under restriction
2388 -- No_Dispatching_Calls. During the semantic analysis we
2389 -- already notified such violation.
2391 if Restriction_Active (No_Dispatching_Calls) then
2392 return;
2393 end if;
2395 declare
2396 Rtyp : constant Entity_Id := Root_Type (P_Type);
2397 Dnn : Entity_Id;
2398 Decl : Node_Id;
2399 Expr : Node_Id;
2401 begin
2402 -- Read the internal tag (RM 13.13.2(34)) and use it to
2403 -- initialize a dummy tag object:
2405 -- Dnn : Ada.Tags.Tag :=
2406 -- Descendant_Tag (String'Input (Strm), P_Type);
2408 -- This dummy object is used only to provide a controlling
2409 -- argument for the eventual _Input call. Descendant_Tag is
2410 -- called rather than Internal_Tag to ensure that we have a
2411 -- tag for a type that is descended from the prefix type and
2412 -- declared at the same accessibility level (the exception
2413 -- Tag_Error will be raised otherwise). The level check is
2414 -- required for Ada 2005 because tagged types can be
2415 -- extended in nested scopes (AI-344).
2417 Expr :=
2418 Make_Function_Call (Loc,
2419 Name =>
2420 New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc),
2421 Parameter_Associations => New_List (
2422 Make_Attribute_Reference (Loc,
2423 Prefix => New_Occurrence_Of (Standard_String, Loc),
2424 Attribute_Name => Name_Input,
2425 Expressions => New_List (
2426 Relocate_Node (Duplicate_Subexpr (Strm)))),
2427 Make_Attribute_Reference (Loc,
2428 Prefix => New_Reference_To (P_Type, Loc),
2429 Attribute_Name => Name_Tag)));
2431 Dnn := Make_Temporary (Loc, 'D', Expr);
2433 Decl :=
2434 Make_Object_Declaration (Loc,
2435 Defining_Identifier => Dnn,
2436 Object_Definition =>
2437 New_Occurrence_Of (RTE (RE_Tag), Loc),
2438 Expression => Expr);
2440 Insert_Action (N, Decl);
2442 -- Now we need to get the entity for the call, and construct
2443 -- a function call node, where we preset a reference to Dnn
2444 -- as the controlling argument (doing an unchecked convert
2445 -- to the class-wide tagged type to make it look like a real
2446 -- tagged object).
2448 Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
2449 Cntrl :=
2450 Unchecked_Convert_To (P_Type,
2451 New_Occurrence_Of (Dnn, Loc));
2452 Set_Etype (Cntrl, P_Type);
2453 Set_Parent (Cntrl, N);
2454 end;
2456 -- For tagged types, use the primitive Input function
2458 elsif Is_Tagged_Type (U_Type) then
2459 Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
2461 -- All other record type cases, including protected records. The
2462 -- latter only arise for expander generated code for handling
2463 -- shared passive partition access.
2465 else
2466 pragma Assert
2467 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2469 -- Ada 2005 (AI-216): Program_Error is raised executing default
2470 -- implementation of the Input attribute of an unchecked union
2471 -- type if the type lacks default discriminant values.
2473 if Is_Unchecked_Union (Base_Type (U_Type))
2474 and then No (Discriminant_Constraint (U_Type))
2475 then
2476 Insert_Action (N,
2477 Make_Raise_Program_Error (Loc,
2478 Reason => PE_Unchecked_Union_Restriction));
2480 return;
2481 end if;
2483 Build_Record_Or_Elementary_Input_Function
2484 (Loc, Base_Type (U_Type), Decl, Fname);
2485 Insert_Action (N, Decl);
2487 if Nkind (Parent (N)) = N_Object_Declaration
2488 and then Is_Record_Type (U_Type)
2489 then
2490 -- The stream function may contain calls to user-defined
2491 -- Read procedures for individual components.
2493 declare
2494 Comp : Entity_Id;
2495 Func : Entity_Id;
2497 begin
2498 Comp := First_Component (U_Type);
2499 while Present (Comp) loop
2500 Func :=
2501 Find_Stream_Subprogram
2502 (Etype (Comp), TSS_Stream_Read);
2504 if Present (Func) then
2505 Freeze_Stream_Subprogram (Func);
2506 end if;
2508 Next_Component (Comp);
2509 end loop;
2510 end;
2511 end if;
2512 end if;
2513 end if;
2515 -- If we fall through, Fname is the function to be called. The result
2516 -- is obtained by calling the appropriate function, then converting
2517 -- the result. The conversion does a subtype check.
2519 Call :=
2520 Make_Function_Call (Loc,
2521 Name => New_Occurrence_Of (Fname, Loc),
2522 Parameter_Associations => New_List (
2523 Relocate_Node (Strm)));
2525 Set_Controlling_Argument (Call, Cntrl);
2526 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
2527 Analyze_And_Resolve (N, P_Type);
2529 if Nkind (Parent (N)) = N_Object_Declaration then
2530 Freeze_Stream_Subprogram (Fname);
2531 end if;
2532 end Input;
2534 -------------------
2535 -- Integer_Value --
2536 -------------------
2538 -- We transform
2540 -- inttype'Fixed_Value (fixed-value)
2542 -- into
2544 -- inttype(integer-value))
2546 -- we do all the required analysis of the conversion here, because we do
2547 -- not want this to go through the fixed-point conversion circuits. Note
2548 -- that the back end always treats fixed-point as equivalent to the
2549 -- corresponding integer type anyway.
2551 when Attribute_Integer_Value => Integer_Value :
2552 begin
2553 Rewrite (N,
2554 Make_Type_Conversion (Loc,
2555 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2556 Expression => Relocate_Node (First (Exprs))));
2557 Set_Etype (N, Entity (Pref));
2558 Set_Analyzed (N);
2560 -- Note: it might appear that a properly analyzed unchecked conversion
2561 -- would be just fine here, but that's not the case, since the full
2562 -- range checks performed by the following call are critical!
2564 Apply_Type_Conversion_Checks (N);
2565 end Integer_Value;
2567 -------------------
2568 -- Invalid_Value --
2569 -------------------
2571 when Attribute_Invalid_Value =>
2572 Rewrite (N, Get_Simple_Init_Val (Ptyp, N));
2574 ----------
2575 -- Last --
2576 ----------
2578 when Attribute_Last =>
2580 -- If the prefix type is a constrained packed array type which
2581 -- already has a Packed_Array_Type representation defined, then
2582 -- replace this attribute with a direct reference to 'Last of the
2583 -- appropriate index subtype (since otherwise the back end will try
2584 -- to give us the value of 'Last for this implementation type).
2586 if Is_Constrained_Packed_Array (Ptyp) then
2587 Rewrite (N,
2588 Make_Attribute_Reference (Loc,
2589 Attribute_Name => Name_Last,
2590 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2591 Analyze_And_Resolve (N, Typ);
2593 elsif Is_Access_Type (Ptyp) then
2594 Apply_Access_Check (N);
2595 end if;
2597 --------------
2598 -- Last_Bit --
2599 --------------
2601 -- We compute this if a component clause was present, otherwise we leave
2602 -- the computation up to the back end, since we don't know what layout
2603 -- will be chosen.
2605 when Attribute_Last_Bit => Last_Bit : declare
2606 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2608 begin
2609 if Known_Static_Component_Bit_Offset (CE)
2610 and then Known_Static_Esize (CE)
2611 then
2612 Rewrite (N,
2613 Make_Integer_Literal (Loc,
2614 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
2615 + Esize (CE) - 1));
2617 Analyze_And_Resolve (N, Typ);
2619 else
2620 Apply_Universal_Integer_Attribute_Checks (N);
2621 end if;
2622 end Last_Bit;
2624 ------------------
2625 -- Leading_Part --
2626 ------------------
2628 -- Transforms 'Leading_Part into a call to the floating-point attribute
2629 -- function Leading_Part in Fat_xxx (where xxx is the root type)
2631 -- Note: strictly, we should generate special case code to deal with
2632 -- absurdly large positive arguments (greater than Integer'Last), which
2633 -- result in returning the first argument unchanged, but it hardly seems
2634 -- worth the effort. We raise constraint error for absurdly negative
2635 -- arguments which is fine.
2637 when Attribute_Leading_Part =>
2638 Expand_Fpt_Attribute_RI (N);
2640 ------------
2641 -- Length --
2642 ------------
2644 when Attribute_Length => declare
2645 Ityp : Entity_Id;
2646 Xnum : Uint;
2648 begin
2649 -- Processing for packed array types
2651 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
2652 Ityp := Get_Index_Subtype (N);
2654 -- If the index type, Ityp, is an enumeration type with holes,
2655 -- then we calculate X'Length explicitly using
2657 -- Typ'Max
2658 -- (0, Ityp'Pos (X'Last (N)) -
2659 -- Ityp'Pos (X'First (N)) + 1);
2661 -- Since the bounds in the template are the representation values
2662 -- and the back end would get the wrong value.
2664 if Is_Enumeration_Type (Ityp)
2665 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
2666 then
2667 if No (Exprs) then
2668 Xnum := Uint_1;
2669 else
2670 Xnum := Expr_Value (First (Expressions (N)));
2671 end if;
2673 Rewrite (N,
2674 Make_Attribute_Reference (Loc,
2675 Prefix => New_Occurrence_Of (Typ, Loc),
2676 Attribute_Name => Name_Max,
2677 Expressions => New_List
2678 (Make_Integer_Literal (Loc, 0),
2680 Make_Op_Add (Loc,
2681 Left_Opnd =>
2682 Make_Op_Subtract (Loc,
2683 Left_Opnd =>
2684 Make_Attribute_Reference (Loc,
2685 Prefix => New_Occurrence_Of (Ityp, Loc),
2686 Attribute_Name => Name_Pos,
2688 Expressions => New_List (
2689 Make_Attribute_Reference (Loc,
2690 Prefix => Duplicate_Subexpr (Pref),
2691 Attribute_Name => Name_Last,
2692 Expressions => New_List (
2693 Make_Integer_Literal (Loc, Xnum))))),
2695 Right_Opnd =>
2696 Make_Attribute_Reference (Loc,
2697 Prefix => New_Occurrence_Of (Ityp, Loc),
2698 Attribute_Name => Name_Pos,
2700 Expressions => New_List (
2701 Make_Attribute_Reference (Loc,
2702 Prefix =>
2703 Duplicate_Subexpr_No_Checks (Pref),
2704 Attribute_Name => Name_First,
2705 Expressions => New_List (
2706 Make_Integer_Literal (Loc, Xnum)))))),
2708 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
2710 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2711 return;
2713 -- If the prefix type is a constrained packed array type which
2714 -- already has a Packed_Array_Type representation defined, then
2715 -- replace this attribute with a direct reference to 'Range_Length
2716 -- of the appropriate index subtype (since otherwise the back end
2717 -- will try to give us the value of 'Length for this
2718 -- implementation type).
2720 elsif Is_Constrained (Ptyp) then
2721 Rewrite (N,
2722 Make_Attribute_Reference (Loc,
2723 Attribute_Name => Name_Range_Length,
2724 Prefix => New_Reference_To (Ityp, Loc)));
2725 Analyze_And_Resolve (N, Typ);
2726 end if;
2728 -- Access type case
2730 elsif Is_Access_Type (Ptyp) then
2731 Apply_Access_Check (N);
2733 -- If the designated type is a packed array type, then we convert
2734 -- the reference to:
2736 -- typ'Max (0, 1 +
2737 -- xtyp'Pos (Pref'Last (Expr)) -
2738 -- xtyp'Pos (Pref'First (Expr)));
2740 -- This is a bit complex, but it is the easiest thing to do that
2741 -- works in all cases including enum types with holes xtyp here
2742 -- is the appropriate index type.
2744 declare
2745 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
2746 Xtyp : Entity_Id;
2748 begin
2749 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
2750 Xtyp := Get_Index_Subtype (N);
2752 Rewrite (N,
2753 Make_Attribute_Reference (Loc,
2754 Prefix => New_Occurrence_Of (Typ, Loc),
2755 Attribute_Name => Name_Max,
2756 Expressions => New_List (
2757 Make_Integer_Literal (Loc, 0),
2759 Make_Op_Add (Loc,
2760 Make_Integer_Literal (Loc, 1),
2761 Make_Op_Subtract (Loc,
2762 Left_Opnd =>
2763 Make_Attribute_Reference (Loc,
2764 Prefix => New_Occurrence_Of (Xtyp, Loc),
2765 Attribute_Name => Name_Pos,
2766 Expressions => New_List (
2767 Make_Attribute_Reference (Loc,
2768 Prefix => Duplicate_Subexpr (Pref),
2769 Attribute_Name => Name_Last,
2770 Expressions =>
2771 New_Copy_List (Exprs)))),
2773 Right_Opnd =>
2774 Make_Attribute_Reference (Loc,
2775 Prefix => New_Occurrence_Of (Xtyp, Loc),
2776 Attribute_Name => Name_Pos,
2777 Expressions => New_List (
2778 Make_Attribute_Reference (Loc,
2779 Prefix =>
2780 Duplicate_Subexpr_No_Checks (Pref),
2781 Attribute_Name => Name_First,
2782 Expressions =>
2783 New_Copy_List (Exprs)))))))));
2785 Analyze_And_Resolve (N, Typ);
2786 end if;
2787 end;
2789 -- Otherwise leave it to the back end
2791 else
2792 Apply_Universal_Integer_Attribute_Checks (N);
2793 end if;
2794 end;
2796 -------------
2797 -- Machine --
2798 -------------
2800 -- Transforms 'Machine into a call to the floating-point attribute
2801 -- function Machine in Fat_xxx (where xxx is the root type)
2803 when Attribute_Machine =>
2804 Expand_Fpt_Attribute_R (N);
2806 ----------------------
2807 -- Machine_Rounding --
2808 ----------------------
2810 -- Transforms 'Machine_Rounding into a call to the floating-point
2811 -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root
2812 -- type). Expansion is avoided for cases the back end can handle
2813 -- directly.
2815 when Attribute_Machine_Rounding =>
2816 if not Is_Inline_Floating_Point_Attribute (N) then
2817 Expand_Fpt_Attribute_R (N);
2818 end if;
2820 ------------------
2821 -- Machine_Size --
2822 ------------------
2824 -- Machine_Size is equivalent to Object_Size, so transform it into
2825 -- Object_Size and that way the back end never sees Machine_Size.
2827 when Attribute_Machine_Size =>
2828 Rewrite (N,
2829 Make_Attribute_Reference (Loc,
2830 Prefix => Prefix (N),
2831 Attribute_Name => Name_Object_Size));
2833 Analyze_And_Resolve (N, Typ);
2835 --------------
2836 -- Mantissa --
2837 --------------
2839 -- The only case that can get this far is the dynamic case of the old
2840 -- Ada 83 Mantissa attribute for the fixed-point case. For this case,
2841 -- we expand:
2843 -- typ'Mantissa
2845 -- into
2847 -- ityp (System.Mantissa.Mantissa_Value
2848 -- (Integer'Integer_Value (typ'First),
2849 -- Integer'Integer_Value (typ'Last)));
2851 when Attribute_Mantissa => Mantissa : begin
2852 Rewrite (N,
2853 Convert_To (Typ,
2854 Make_Function_Call (Loc,
2855 Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
2857 Parameter_Associations => New_List (
2859 Make_Attribute_Reference (Loc,
2860 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2861 Attribute_Name => Name_Integer_Value,
2862 Expressions => New_List (
2864 Make_Attribute_Reference (Loc,
2865 Prefix => New_Occurrence_Of (Ptyp, Loc),
2866 Attribute_Name => Name_First))),
2868 Make_Attribute_Reference (Loc,
2869 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2870 Attribute_Name => Name_Integer_Value,
2871 Expressions => New_List (
2873 Make_Attribute_Reference (Loc,
2874 Prefix => New_Occurrence_Of (Ptyp, Loc),
2875 Attribute_Name => Name_Last)))))));
2877 Analyze_And_Resolve (N, Typ);
2878 end Mantissa;
2880 --------------------
2881 -- Mechanism_Code --
2882 --------------------
2884 when Attribute_Mechanism_Code =>
2886 -- We must replace the prefix in the renamed case
2888 if Is_Entity_Name (Pref)
2889 and then Present (Alias (Entity (Pref)))
2890 then
2891 Set_Renamed_Subprogram (Pref, Alias (Entity (Pref)));
2892 end if;
2894 ---------
2895 -- Mod --
2896 ---------
2898 when Attribute_Mod => Mod_Case : declare
2899 Arg : constant Node_Id := Relocate_Node (First (Exprs));
2900 Hi : constant Node_Id := Type_High_Bound (Etype (Arg));
2901 Modv : constant Uint := Modulus (Btyp);
2903 begin
2905 -- This is not so simple. The issue is what type to use for the
2906 -- computation of the modular value.
2908 -- The easy case is when the modulus value is within the bounds
2909 -- of the signed integer type of the argument. In this case we can
2910 -- just do the computation in that signed integer type, and then
2911 -- do an ordinary conversion to the target type.
2913 if Modv <= Expr_Value (Hi) then
2914 Rewrite (N,
2915 Convert_To (Btyp,
2916 Make_Op_Mod (Loc,
2917 Left_Opnd => Arg,
2918 Right_Opnd => Make_Integer_Literal (Loc, Modv))));
2920 -- Here we know that the modulus is larger than type'Last of the
2921 -- integer type. There are two cases to consider:
2923 -- a) The integer value is non-negative. In this case, it is
2924 -- returned as the result (since it is less than the modulus).
2926 -- b) The integer value is negative. In this case, we know that the
2927 -- result is modulus + value, where the value might be as small as
2928 -- -modulus. The trouble is what type do we use to do the subtract.
2929 -- No type will do, since modulus can be as big as 2**64, and no
2930 -- integer type accommodates this value. Let's do bit of algebra
2932 -- modulus + value
2933 -- = modulus - (-value)
2934 -- = (modulus - 1) - (-value - 1)
2936 -- Now modulus - 1 is certainly in range of the modular type.
2937 -- -value is in the range 1 .. modulus, so -value -1 is in the
2938 -- range 0 .. modulus-1 which is in range of the modular type.
2939 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
2940 -- which we can compute using the integer base type.
2942 -- Once this is done we analyze the conditional expression without
2943 -- range checks, because we know everything is in range, and we
2944 -- want to prevent spurious warnings on either branch.
2946 else
2947 Rewrite (N,
2948 Make_Conditional_Expression (Loc,
2949 Expressions => New_List (
2950 Make_Op_Ge (Loc,
2951 Left_Opnd => Duplicate_Subexpr (Arg),
2952 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2954 Convert_To (Btyp,
2955 Duplicate_Subexpr_No_Checks (Arg)),
2957 Make_Op_Subtract (Loc,
2958 Left_Opnd =>
2959 Make_Integer_Literal (Loc,
2960 Intval => Modv - 1),
2961 Right_Opnd =>
2962 Convert_To (Btyp,
2963 Make_Op_Minus (Loc,
2964 Right_Opnd =>
2965 Make_Op_Add (Loc,
2966 Left_Opnd => Duplicate_Subexpr_No_Checks (Arg),
2967 Right_Opnd =>
2968 Make_Integer_Literal (Loc,
2969 Intval => 1))))))));
2971 end if;
2973 Analyze_And_Resolve (N, Btyp, Suppress => All_Checks);
2974 end Mod_Case;
2976 -----------
2977 -- Model --
2978 -----------
2980 -- Transforms 'Model into a call to the floating-point attribute
2981 -- function Model in Fat_xxx (where xxx is the root type)
2983 when Attribute_Model =>
2984 Expand_Fpt_Attribute_R (N);
2986 -----------------
2987 -- Object_Size --
2988 -----------------
2990 -- The processing for Object_Size shares the processing for Size
2992 ---------
2993 -- Old --
2994 ---------
2996 when Attribute_Old => Old : declare
2997 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', Pref);
2998 Subp : Node_Id;
2999 Asn_Stm : Node_Id;
3001 begin
3002 -- Find the nearest subprogram body, ignoring _Preconditions
3004 Subp := N;
3005 loop
3006 Subp := Parent (Subp);
3007 exit when Nkind (Subp) = N_Subprogram_Body
3008 and then Chars (Defining_Entity (Subp)) /= Name_uPostconditions;
3009 end loop;
3011 -- Insert the assignment at the start of the declarations
3013 Asn_Stm :=
3014 Make_Object_Declaration (Loc,
3015 Defining_Identifier => Tnn,
3016 Constant_Present => True,
3017 Object_Definition => New_Occurrence_Of (Etype (N), Loc),
3018 Expression => Pref);
3020 if Is_Empty_List (Declarations (Subp)) then
3021 Set_Declarations (Subp, New_List (Asn_Stm));
3022 Analyze (Asn_Stm);
3023 else
3024 Insert_Action (First (Declarations (Subp)), Asn_Stm);
3025 end if;
3027 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
3028 end Old;
3030 ------------
3031 -- Output --
3032 ------------
3034 when Attribute_Output => Output : declare
3035 P_Type : constant Entity_Id := Entity (Pref);
3036 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3037 Pname : Entity_Id;
3038 Decl : Node_Id;
3039 Prag : Node_Id;
3040 Arg3 : Node_Id;
3041 Wfunc : Node_Id;
3043 begin
3044 -- If no underlying type, we have an error that will be diagnosed
3045 -- elsewhere, so here we just completely ignore the expansion.
3047 if No (U_Type) then
3048 return;
3049 end if;
3051 -- If TSS for Output is present, just call it
3053 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
3055 if Present (Pname) then
3056 null;
3058 else
3059 -- If there is a Stream_Convert pragma, use it, we rewrite
3061 -- sourcetyp'Output (stream, Item)
3063 -- as
3065 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
3067 -- where strmwrite is the given Write function that converts an
3068 -- argument of type sourcetyp or a type acctyp, from which it is
3069 -- derived to type strmtyp. The conversion to acttyp is required
3070 -- for the derived case.
3072 Prag := Get_Stream_Convert_Pragma (P_Type);
3074 if Present (Prag) then
3075 Arg3 :=
3076 Next (Next (First (Pragma_Argument_Associations (Prag))));
3077 Wfunc := Entity (Expression (Arg3));
3079 Rewrite (N,
3080 Make_Attribute_Reference (Loc,
3081 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
3082 Attribute_Name => Name_Output,
3083 Expressions => New_List (
3084 Relocate_Node (First (Exprs)),
3085 Make_Function_Call (Loc,
3086 Name => New_Occurrence_Of (Wfunc, Loc),
3087 Parameter_Associations => New_List (
3088 OK_Convert_To (Etype (First_Formal (Wfunc)),
3089 Relocate_Node (Next (First (Exprs)))))))));
3091 Analyze (N);
3092 return;
3094 -- For elementary types, we call the W_xxx routine directly.
3095 -- Note that the effect of Write and Output is identical for
3096 -- the case of an elementary type, since there are no
3097 -- discriminants or bounds.
3099 elsif Is_Elementary_Type (U_Type) then
3101 -- A special case arises if we have a defined _Write routine,
3102 -- since in this case we are required to call this routine.
3104 if Present (TSS (Base_Type (U_Type), TSS_Stream_Write)) then
3105 Build_Record_Or_Elementary_Output_Procedure
3106 (Loc, U_Type, Decl, Pname);
3107 Insert_Action (N, Decl);
3109 -- For normal cases, we call the W_xxx routine directly
3111 else
3112 Rewrite (N, Build_Elementary_Write_Call (N));
3113 Analyze (N);
3114 return;
3115 end if;
3117 -- Array type case
3119 elsif Is_Array_Type (U_Type) then
3120 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
3121 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3123 -- Class-wide case, first output external tag, then dispatch
3124 -- to the appropriate primitive Output function (RM 13.13.2(31)).
3126 elsif Is_Class_Wide_Type (P_Type) then
3128 -- No need to do anything else compiling under restriction
3129 -- No_Dispatching_Calls. During the semantic analysis we
3130 -- already notified such violation.
3132 if Restriction_Active (No_Dispatching_Calls) then
3133 return;
3134 end if;
3136 Tag_Write : declare
3137 Strm : constant Node_Id := First (Exprs);
3138 Item : constant Node_Id := Next (Strm);
3140 begin
3141 -- Ada 2005 (AI-344): Check that the accessibility level
3142 -- of the type of the output object is not deeper than
3143 -- that of the attribute's prefix type.
3145 -- if Get_Access_Level (Item'Tag)
3146 -- /= Get_Access_Level (P_Type'Tag)
3147 -- then
3148 -- raise Tag_Error;
3149 -- end if;
3151 -- String'Output (Strm, External_Tag (Item'Tag));
3153 -- We cannot figure out a practical way to implement this
3154 -- accessibility check on virtual machines, so we omit it.
3156 if Ada_Version >= Ada_05
3157 and then Tagged_Type_Expansion
3158 then
3159 Insert_Action (N,
3160 Make_Implicit_If_Statement (N,
3161 Condition =>
3162 Make_Op_Ne (Loc,
3163 Left_Opnd =>
3164 Build_Get_Access_Level (Loc,
3165 Make_Attribute_Reference (Loc,
3166 Prefix =>
3167 Relocate_Node (
3168 Duplicate_Subexpr (Item,
3169 Name_Req => True)),
3170 Attribute_Name => Name_Tag)),
3172 Right_Opnd =>
3173 Make_Integer_Literal (Loc,
3174 Type_Access_Level (P_Type))),
3176 Then_Statements =>
3177 New_List (Make_Raise_Statement (Loc,
3178 New_Occurrence_Of (
3179 RTE (RE_Tag_Error), Loc)))));
3180 end if;
3182 Insert_Action (N,
3183 Make_Attribute_Reference (Loc,
3184 Prefix => New_Occurrence_Of (Standard_String, Loc),
3185 Attribute_Name => Name_Output,
3186 Expressions => New_List (
3187 Relocate_Node (Duplicate_Subexpr (Strm)),
3188 Make_Function_Call (Loc,
3189 Name =>
3190 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
3191 Parameter_Associations => New_List (
3192 Make_Attribute_Reference (Loc,
3193 Prefix =>
3194 Relocate_Node
3195 (Duplicate_Subexpr (Item, Name_Req => True)),
3196 Attribute_Name => Name_Tag))))));
3197 end Tag_Write;
3199 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3201 -- Tagged type case, use the primitive Output function
3203 elsif Is_Tagged_Type (U_Type) then
3204 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3206 -- All other record type cases, including protected records.
3207 -- The latter only arise for expander generated code for
3208 -- handling shared passive partition access.
3210 else
3211 pragma Assert
3212 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3214 -- Ada 2005 (AI-216): Program_Error is raised when executing
3215 -- the default implementation of the Output attribute of an
3216 -- unchecked union type if the type lacks default discriminant
3217 -- values.
3219 if Is_Unchecked_Union (Base_Type (U_Type))
3220 and then No (Discriminant_Constraint (U_Type))
3221 then
3222 Insert_Action (N,
3223 Make_Raise_Program_Error (Loc,
3224 Reason => PE_Unchecked_Union_Restriction));
3226 return;
3227 end if;
3229 Build_Record_Or_Elementary_Output_Procedure
3230 (Loc, Base_Type (U_Type), Decl, Pname);
3231 Insert_Action (N, Decl);
3232 end if;
3233 end if;
3235 -- If we fall through, Pname is the name of the procedure to call
3237 Rewrite_Stream_Proc_Call (Pname);
3238 end Output;
3240 ---------
3241 -- Pos --
3242 ---------
3244 -- For enumeration types with a standard representation, Pos is
3245 -- handled by the back end.
3247 -- For enumeration types, with a non-standard representation we generate
3248 -- a call to the _Rep_To_Pos function created when the type was frozen.
3249 -- The call has the form
3251 -- _rep_to_pos (expr, flag)
3253 -- The parameter flag is True if range checks are enabled, causing
3254 -- Program_Error to be raised if the expression has an invalid
3255 -- representation, and False if range checks are suppressed.
3257 -- For integer types, Pos is equivalent to a simple integer
3258 -- conversion and we rewrite it as such
3260 when Attribute_Pos => Pos :
3261 declare
3262 Etyp : Entity_Id := Base_Type (Entity (Pref));
3264 begin
3265 -- Deal with zero/non-zero boolean values
3267 if Is_Boolean_Type (Etyp) then
3268 Adjust_Condition (First (Exprs));
3269 Etyp := Standard_Boolean;
3270 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
3271 end if;
3273 -- Case of enumeration type
3275 if Is_Enumeration_Type (Etyp) then
3277 -- Non-standard enumeration type (generate call)
3279 if Present (Enum_Pos_To_Rep (Etyp)) then
3280 Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
3281 Rewrite (N,
3282 Convert_To (Typ,
3283 Make_Function_Call (Loc,
3284 Name =>
3285 New_Reference_To (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3286 Parameter_Associations => Exprs)));
3288 Analyze_And_Resolve (N, Typ);
3290 -- Standard enumeration type (do universal integer check)
3292 else
3293 Apply_Universal_Integer_Attribute_Checks (N);
3294 end if;
3296 -- Deal with integer types (replace by conversion)
3298 elsif Is_Integer_Type (Etyp) then
3299 Rewrite (N, Convert_To (Typ, First (Exprs)));
3300 Analyze_And_Resolve (N, Typ);
3301 end if;
3303 end Pos;
3305 --------------
3306 -- Position --
3307 --------------
3309 -- We compute this if a component clause was present, otherwise we leave
3310 -- the computation up to the back end, since we don't know what layout
3311 -- will be chosen.
3313 when Attribute_Position => Position :
3314 declare
3315 CE : constant Entity_Id := Entity (Selector_Name (Pref));
3317 begin
3318 if Present (Component_Clause (CE)) then
3319 Rewrite (N,
3320 Make_Integer_Literal (Loc,
3321 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
3322 Analyze_And_Resolve (N, Typ);
3324 else
3325 Apply_Universal_Integer_Attribute_Checks (N);
3326 end if;
3327 end Position;
3329 ----------
3330 -- Pred --
3331 ----------
3333 -- 1. Deal with enumeration types with holes
3334 -- 2. For floating-point, generate call to attribute function
3335 -- 3. For other cases, deal with constraint checking
3337 when Attribute_Pred => Pred :
3338 declare
3339 Etyp : constant Entity_Id := Base_Type (Ptyp);
3341 begin
3343 -- For enumeration types with non-standard representations, we
3344 -- expand typ'Pred (x) into
3346 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
3348 -- If the representation is contiguous, we compute instead
3349 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
3350 -- The conversion function Enum_Pos_To_Rep is defined on the
3351 -- base type, not the subtype, so we have to use the base type
3352 -- explicitly for this and other enumeration attributes.
3354 if Is_Enumeration_Type (Ptyp)
3355 and then Present (Enum_Pos_To_Rep (Etyp))
3356 then
3357 if Has_Contiguous_Rep (Etyp) then
3358 Rewrite (N,
3359 Unchecked_Convert_To (Ptyp,
3360 Make_Op_Add (Loc,
3361 Left_Opnd =>
3362 Make_Integer_Literal (Loc,
3363 Enumeration_Rep (First_Literal (Ptyp))),
3364 Right_Opnd =>
3365 Make_Function_Call (Loc,
3366 Name =>
3367 New_Reference_To
3368 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3370 Parameter_Associations =>
3371 New_List (
3372 Unchecked_Convert_To (Ptyp,
3373 Make_Op_Subtract (Loc,
3374 Left_Opnd =>
3375 Unchecked_Convert_To (Standard_Integer,
3376 Relocate_Node (First (Exprs))),
3377 Right_Opnd =>
3378 Make_Integer_Literal (Loc, 1))),
3379 Rep_To_Pos_Flag (Ptyp, Loc))))));
3381 else
3382 -- Add Boolean parameter True, to request program errror if
3383 -- we have a bad representation on our hands. If checks are
3384 -- suppressed, then add False instead
3386 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
3387 Rewrite (N,
3388 Make_Indexed_Component (Loc,
3389 Prefix =>
3390 New_Reference_To
3391 (Enum_Pos_To_Rep (Etyp), Loc),
3392 Expressions => New_List (
3393 Make_Op_Subtract (Loc,
3394 Left_Opnd =>
3395 Make_Function_Call (Loc,
3396 Name =>
3397 New_Reference_To
3398 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3399 Parameter_Associations => Exprs),
3400 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3401 end if;
3403 Analyze_And_Resolve (N, Typ);
3405 -- For floating-point, we transform 'Pred into a call to the Pred
3406 -- floating-point attribute function in Fat_xxx (xxx is root type)
3408 elsif Is_Floating_Point_Type (Ptyp) then
3409 Expand_Fpt_Attribute_R (N);
3410 Analyze_And_Resolve (N, Typ);
3412 -- For modular types, nothing to do (no overflow, since wraps)
3414 elsif Is_Modular_Integer_Type (Ptyp) then
3415 null;
3417 -- For other types, if argument is marked as needing a range check or
3418 -- overflow checking is enabled, we must generate a check.
3420 elsif not Overflow_Checks_Suppressed (Ptyp)
3421 or else Do_Range_Check (First (Exprs))
3422 then
3423 Set_Do_Range_Check (First (Exprs), False);
3424 Expand_Pred_Succ (N);
3425 end if;
3426 end Pred;
3428 --------------
3429 -- Priority --
3430 --------------
3432 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3434 -- We rewrite X'Priority as the following run-time call:
3436 -- Get_Ceiling (X._Object)
3438 -- Note that although X'Priority is notionally an object, it is quite
3439 -- deliberately not defined as an aliased object in the RM. This means
3440 -- that it works fine to rewrite it as a call, without having to worry
3441 -- about complications that would other arise from X'Priority'Access,
3442 -- which is illegal, because of the lack of aliasing.
3444 when Attribute_Priority =>
3445 declare
3446 Call : Node_Id;
3447 Conctyp : Entity_Id;
3448 Object_Parm : Node_Id;
3449 Subprg : Entity_Id;
3450 RT_Subprg_Name : Node_Id;
3452 begin
3453 -- Look for the enclosing concurrent type
3455 Conctyp := Current_Scope;
3456 while not Is_Concurrent_Type (Conctyp) loop
3457 Conctyp := Scope (Conctyp);
3458 end loop;
3460 pragma Assert (Is_Protected_Type (Conctyp));
3462 -- Generate the actual of the call
3464 Subprg := Current_Scope;
3465 while not Present (Protected_Body_Subprogram (Subprg)) loop
3466 Subprg := Scope (Subprg);
3467 end loop;
3469 -- Use of 'Priority inside protected entries and barriers (in
3470 -- both cases the type of the first formal of their expanded
3471 -- subprogram is Address)
3473 if Etype (First_Entity (Protected_Body_Subprogram (Subprg)))
3474 = RTE (RE_Address)
3475 then
3476 declare
3477 New_Itype : Entity_Id;
3479 begin
3480 -- In the expansion of protected entries the type of the
3481 -- first formal of the Protected_Body_Subprogram is an
3482 -- Address. In order to reference the _object component
3483 -- we generate:
3485 -- type T is access p__ptTV;
3486 -- freeze T []
3488 New_Itype := Create_Itype (E_Access_Type, N);
3489 Set_Etype (New_Itype, New_Itype);
3490 Set_Directly_Designated_Type (New_Itype,
3491 Corresponding_Record_Type (Conctyp));
3492 Freeze_Itype (New_Itype, N);
3494 -- Generate:
3495 -- T!(O)._object'unchecked_access
3497 Object_Parm :=
3498 Make_Attribute_Reference (Loc,
3499 Prefix =>
3500 Make_Selected_Component (Loc,
3501 Prefix =>
3502 Unchecked_Convert_To (New_Itype,
3503 New_Reference_To
3504 (First_Entity
3505 (Protected_Body_Subprogram (Subprg)),
3506 Loc)),
3507 Selector_Name =>
3508 Make_Identifier (Loc, Name_uObject)),
3509 Attribute_Name => Name_Unchecked_Access);
3510 end;
3512 -- Use of 'Priority inside a protected subprogram
3514 else
3515 Object_Parm :=
3516 Make_Attribute_Reference (Loc,
3517 Prefix =>
3518 Make_Selected_Component (Loc,
3519 Prefix => New_Reference_To
3520 (First_Entity
3521 (Protected_Body_Subprogram (Subprg)),
3522 Loc),
3523 Selector_Name =>
3524 Make_Identifier (Loc, Name_uObject)),
3525 Attribute_Name => Name_Unchecked_Access);
3526 end if;
3528 -- Select the appropriate run-time subprogram
3530 if Number_Entries (Conctyp) = 0 then
3531 RT_Subprg_Name :=
3532 New_Reference_To (RTE (RE_Get_Ceiling), Loc);
3533 else
3534 RT_Subprg_Name :=
3535 New_Reference_To (RTE (RO_PE_Get_Ceiling), Loc);
3536 end if;
3538 Call :=
3539 Make_Function_Call (Loc,
3540 Name => RT_Subprg_Name,
3541 Parameter_Associations => New_List (Object_Parm));
3543 Rewrite (N, Call);
3545 -- Avoid the generation of extra checks on the pointer to the
3546 -- protected object.
3548 Analyze_And_Resolve (N, Typ, Suppress => Access_Check);
3549 end;
3551 ------------------
3552 -- Range_Length --
3553 ------------------
3555 when Attribute_Range_Length => Range_Length : begin
3557 -- The only special processing required is for the case where
3558 -- Range_Length is applied to an enumeration type with holes.
3559 -- In this case we transform
3561 -- X'Range_Length
3563 -- to
3565 -- X'Pos (X'Last) - X'Pos (X'First) + 1
3567 -- So that the result reflects the proper Pos values instead
3568 -- of the underlying representations.
3570 if Is_Enumeration_Type (Ptyp)
3571 and then Has_Non_Standard_Rep (Ptyp)
3572 then
3573 Rewrite (N,
3574 Make_Op_Add (Loc,
3575 Left_Opnd =>
3576 Make_Op_Subtract (Loc,
3577 Left_Opnd =>
3578 Make_Attribute_Reference (Loc,
3579 Attribute_Name => Name_Pos,
3580 Prefix => New_Occurrence_Of (Ptyp, Loc),
3581 Expressions => New_List (
3582 Make_Attribute_Reference (Loc,
3583 Attribute_Name => Name_Last,
3584 Prefix => New_Occurrence_Of (Ptyp, Loc)))),
3586 Right_Opnd =>
3587 Make_Attribute_Reference (Loc,
3588 Attribute_Name => Name_Pos,
3589 Prefix => New_Occurrence_Of (Ptyp, Loc),
3590 Expressions => New_List (
3591 Make_Attribute_Reference (Loc,
3592 Attribute_Name => Name_First,
3593 Prefix => New_Occurrence_Of (Ptyp, Loc))))),
3595 Right_Opnd => Make_Integer_Literal (Loc, 1)));
3597 Analyze_And_Resolve (N, Typ);
3599 -- For all other cases, the attribute is handled by the back end, but
3600 -- we need to deal with the case of the range check on a universal
3601 -- integer.
3603 else
3604 Apply_Universal_Integer_Attribute_Checks (N);
3605 end if;
3606 end Range_Length;
3608 ----------
3609 -- Read --
3610 ----------
3612 when Attribute_Read => Read : declare
3613 P_Type : constant Entity_Id := Entity (Pref);
3614 B_Type : constant Entity_Id := Base_Type (P_Type);
3615 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3616 Pname : Entity_Id;
3617 Decl : Node_Id;
3618 Prag : Node_Id;
3619 Arg2 : Node_Id;
3620 Rfunc : Node_Id;
3621 Lhs : Node_Id;
3622 Rhs : Node_Id;
3624 begin
3625 -- If no underlying type, we have an error that will be diagnosed
3626 -- elsewhere, so here we just completely ignore the expansion.
3628 if No (U_Type) then
3629 return;
3630 end if;
3632 -- The simple case, if there is a TSS for Read, just call it
3634 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
3636 if Present (Pname) then
3637 null;
3639 else
3640 -- If there is a Stream_Convert pragma, use it, we rewrite
3642 -- sourcetyp'Read (stream, Item)
3644 -- as
3646 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
3648 -- where strmread is the given Read function that converts an
3649 -- argument of type strmtyp to type sourcetyp or a type from which
3650 -- it is derived. The conversion to sourcetyp is required in the
3651 -- latter case.
3653 -- A special case arises if Item is a type conversion in which
3654 -- case, we have to expand to:
3656 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
3658 -- where Itemx is the expression of the type conversion (i.e.
3659 -- the actual object), and typex is the type of Itemx.
3661 Prag := Get_Stream_Convert_Pragma (P_Type);
3663 if Present (Prag) then
3664 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
3665 Rfunc := Entity (Expression (Arg2));
3666 Lhs := Relocate_Node (Next (First (Exprs)));
3667 Rhs :=
3668 OK_Convert_To (B_Type,
3669 Make_Function_Call (Loc,
3670 Name => New_Occurrence_Of (Rfunc, Loc),
3671 Parameter_Associations => New_List (
3672 Make_Attribute_Reference (Loc,
3673 Prefix =>
3674 New_Occurrence_Of
3675 (Etype (First_Formal (Rfunc)), Loc),
3676 Attribute_Name => Name_Input,
3677 Expressions => New_List (
3678 Relocate_Node (First (Exprs)))))));
3680 if Nkind (Lhs) = N_Type_Conversion then
3681 Lhs := Expression (Lhs);
3682 Rhs := Convert_To (Etype (Lhs), Rhs);
3683 end if;
3685 Rewrite (N,
3686 Make_Assignment_Statement (Loc,
3687 Name => Lhs,
3688 Expression => Rhs));
3689 Set_Assignment_OK (Lhs);
3690 Analyze (N);
3691 return;
3693 -- For elementary types, we call the I_xxx routine using the first
3694 -- parameter and then assign the result into the second parameter.
3695 -- We set Assignment_OK to deal with the conversion case.
3697 elsif Is_Elementary_Type (U_Type) then
3698 declare
3699 Lhs : Node_Id;
3700 Rhs : Node_Id;
3702 begin
3703 Lhs := Relocate_Node (Next (First (Exprs)));
3704 Rhs := Build_Elementary_Input_Call (N);
3706 if Nkind (Lhs) = N_Type_Conversion then
3707 Lhs := Expression (Lhs);
3708 Rhs := Convert_To (Etype (Lhs), Rhs);
3709 end if;
3711 Set_Assignment_OK (Lhs);
3713 Rewrite (N,
3714 Make_Assignment_Statement (Loc,
3715 Name => Lhs,
3716 Expression => Rhs));
3718 Analyze (N);
3719 return;
3720 end;
3722 -- Array type case
3724 elsif Is_Array_Type (U_Type) then
3725 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
3726 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3728 -- Tagged type case, use the primitive Read function. Note that
3729 -- this will dispatch in the class-wide case which is what we want
3731 elsif Is_Tagged_Type (U_Type) then
3732 Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
3734 -- All other record type cases, including protected records. The
3735 -- latter only arise for expander generated code for handling
3736 -- shared passive partition access.
3738 else
3739 pragma Assert
3740 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3742 -- Ada 2005 (AI-216): Program_Error is raised when executing
3743 -- the default implementation of the Read attribute of an
3744 -- Unchecked_Union type.
3746 if Is_Unchecked_Union (Base_Type (U_Type)) then
3747 Insert_Action (N,
3748 Make_Raise_Program_Error (Loc,
3749 Reason => PE_Unchecked_Union_Restriction));
3750 end if;
3752 if Has_Discriminants (U_Type)
3753 and then Present
3754 (Discriminant_Default_Value (First_Discriminant (U_Type)))
3755 then
3756 Build_Mutable_Record_Read_Procedure
3757 (Loc, Base_Type (U_Type), Decl, Pname);
3758 else
3759 Build_Record_Read_Procedure
3760 (Loc, Base_Type (U_Type), Decl, Pname);
3761 end if;
3763 -- Suppress checks, uninitialized or otherwise invalid
3764 -- data does not cause constraint errors to be raised for
3765 -- a complete record read.
3767 Insert_Action (N, Decl, All_Checks);
3768 end if;
3769 end if;
3771 Rewrite_Stream_Proc_Call (Pname);
3772 end Read;
3774 ---------------
3775 -- Remainder --
3776 ---------------
3778 -- Transforms 'Remainder into a call to the floating-point attribute
3779 -- function Remainder in Fat_xxx (where xxx is the root type)
3781 when Attribute_Remainder =>
3782 Expand_Fpt_Attribute_RR (N);
3784 ------------
3785 -- Result --
3786 ------------
3788 -- Transform 'Result into reference to _Result formal. At the point
3789 -- where a legal 'Result attribute is expanded, we know that we are in
3790 -- the context of a _Postcondition function with a _Result parameter.
3792 when Attribute_Result =>
3793 Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult));
3794 Analyze_And_Resolve (N, Typ);
3796 -----------
3797 -- Round --
3798 -----------
3800 -- The handling of the Round attribute is quite delicate. The processing
3801 -- in Sem_Attr introduced a conversion to universal real, reflecting the
3802 -- semantics of Round, but we do not want anything to do with universal
3803 -- real at runtime, since this corresponds to using floating-point
3804 -- arithmetic.
3806 -- What we have now is that the Etype of the Round attribute correctly
3807 -- indicates the final result type. The operand of the Round is the
3808 -- conversion to universal real, described above, and the operand of
3809 -- this conversion is the actual operand of Round, which may be the
3810 -- special case of a fixed point multiplication or division (Etype =
3811 -- universal fixed)
3813 -- The exapander will expand first the operand of the conversion, then
3814 -- the conversion, and finally the round attribute itself, since we
3815 -- always work inside out. But we cannot simply process naively in this
3816 -- order. In the semantic world where universal fixed and real really
3817 -- exist and have infinite precision, there is no problem, but in the
3818 -- implementation world, where universal real is a floating-point type,
3819 -- we would get the wrong result.
3821 -- So the approach is as follows. First, when expanding a multiply or
3822 -- divide whose type is universal fixed, we do nothing at all, instead
3823 -- deferring the operation till later.
3825 -- The actual processing is done in Expand_N_Type_Conversion which
3826 -- handles the special case of Round by looking at its parent to see if
3827 -- it is a Round attribute, and if it is, handling the conversion (or
3828 -- its fixed multiply/divide child) in an appropriate manner.
3830 -- This means that by the time we get to expanding the Round attribute
3831 -- itself, the Round is nothing more than a type conversion (and will
3832 -- often be a null type conversion), so we just replace it with the
3833 -- appropriate conversion operation.
3835 when Attribute_Round =>
3836 Rewrite (N,
3837 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
3838 Analyze_And_Resolve (N);
3840 --------------
3841 -- Rounding --
3842 --------------
3844 -- Transforms 'Rounding into a call to the floating-point attribute
3845 -- function Rounding in Fat_xxx (where xxx is the root type)
3847 when Attribute_Rounding =>
3848 Expand_Fpt_Attribute_R (N);
3850 -------------
3851 -- Scaling --
3852 -------------
3854 -- Transforms 'Scaling into a call to the floating-point attribute
3855 -- function Scaling in Fat_xxx (where xxx is the root type)
3857 when Attribute_Scaling =>
3858 Expand_Fpt_Attribute_RI (N);
3860 ----------
3861 -- Size --
3862 ----------
3864 when Attribute_Size |
3865 Attribute_Object_Size |
3866 Attribute_Value_Size |
3867 Attribute_VADS_Size => Size :
3869 declare
3870 Siz : Uint;
3871 New_Node : Node_Id;
3873 begin
3874 -- Processing for VADS_Size case. Note that this processing removes
3875 -- all traces of VADS_Size from the tree, and completes all required
3876 -- processing for VADS_Size by translating the attribute reference
3877 -- to an appropriate Size or Object_Size reference.
3879 if Id = Attribute_VADS_Size
3880 or else (Use_VADS_Size and then Id = Attribute_Size)
3881 then
3882 -- If the size is specified, then we simply use the specified
3883 -- size. This applies to both types and objects. The size of an
3884 -- object can be specified in the following ways:
3886 -- An explicit size object is given for an object
3887 -- A component size is specified for an indexed component
3888 -- A component clause is specified for a selected component
3889 -- The object is a component of a packed composite object
3891 -- If the size is specified, then VADS_Size of an object
3893 if (Is_Entity_Name (Pref)
3894 and then Present (Size_Clause (Entity (Pref))))
3895 or else
3896 (Nkind (Pref) = N_Component_Clause
3897 and then (Present (Component_Clause
3898 (Entity (Selector_Name (Pref))))
3899 or else Is_Packed (Etype (Prefix (Pref)))))
3900 or else
3901 (Nkind (Pref) = N_Indexed_Component
3902 and then (Component_Size (Etype (Prefix (Pref))) /= 0
3903 or else Is_Packed (Etype (Prefix (Pref)))))
3904 then
3905 Set_Attribute_Name (N, Name_Size);
3907 -- Otherwise if we have an object rather than a type, then the
3908 -- VADS_Size attribute applies to the type of the object, rather
3909 -- than the object itself. This is one of the respects in which
3910 -- VADS_Size differs from Size.
3912 else
3913 if (not Is_Entity_Name (Pref)
3914 or else not Is_Type (Entity (Pref)))
3915 and then (Is_Scalar_Type (Ptyp) or else Is_Constrained (Ptyp))
3916 then
3917 Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc));
3918 end if;
3920 -- For a scalar type for which no size was explicitly given,
3921 -- VADS_Size means Object_Size. This is the other respect in
3922 -- which VADS_Size differs from Size.
3924 if Is_Scalar_Type (Ptyp) and then No (Size_Clause (Ptyp)) then
3925 Set_Attribute_Name (N, Name_Object_Size);
3927 -- In all other cases, Size and VADS_Size are the sane
3929 else
3930 Set_Attribute_Name (N, Name_Size);
3931 end if;
3932 end if;
3933 end if;
3935 -- For class-wide types, X'Class'Size is transformed into a direct
3936 -- reference to the Size of the class type, so that the back end does
3937 -- not have to deal with the X'Class'Size reference.
3939 if Is_Entity_Name (Pref)
3940 and then Is_Class_Wide_Type (Entity (Pref))
3941 then
3942 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
3943 return;
3945 -- For X'Size applied to an object of a class-wide type, transform
3946 -- X'Size into a call to the primitive operation _Size applied to X.
3948 elsif Is_Class_Wide_Type (Ptyp)
3949 or else (Id = Attribute_Size
3950 and then Is_Tagged_Type (Ptyp)
3951 and then Has_Unknown_Discriminants (Ptyp))
3952 then
3953 -- No need to do anything else compiling under restriction
3954 -- No_Dispatching_Calls. During the semantic analysis we
3955 -- already notified such violation.
3957 if Restriction_Active (No_Dispatching_Calls) then
3958 return;
3959 end if;
3961 New_Node :=
3962 Make_Function_Call (Loc,
3963 Name => New_Reference_To
3964 (Find_Prim_Op (Ptyp, Name_uSize), Loc),
3965 Parameter_Associations => New_List (Pref));
3967 if Typ /= Standard_Long_Long_Integer then
3969 -- The context is a specific integer type with which the
3970 -- original attribute was compatible. The function has a
3971 -- specific type as well, so to preserve the compatibility
3972 -- we must convert explicitly.
3974 New_Node := Convert_To (Typ, New_Node);
3975 end if;
3977 Rewrite (N, New_Node);
3978 Analyze_And_Resolve (N, Typ);
3979 return;
3981 -- Case of known RM_Size of a type
3983 elsif (Id = Attribute_Size or else Id = Attribute_Value_Size)
3984 and then Is_Entity_Name (Pref)
3985 and then Is_Type (Entity (Pref))
3986 and then Known_Static_RM_Size (Entity (Pref))
3987 then
3988 Siz := RM_Size (Entity (Pref));
3990 -- Case of known Esize of a type
3992 elsif Id = Attribute_Object_Size
3993 and then Is_Entity_Name (Pref)
3994 and then Is_Type (Entity (Pref))
3995 and then Known_Static_Esize (Entity (Pref))
3996 then
3997 Siz := Esize (Entity (Pref));
3999 -- Case of known size of object
4001 elsif Id = Attribute_Size
4002 and then Is_Entity_Name (Pref)
4003 and then Is_Object (Entity (Pref))
4004 and then Known_Esize (Entity (Pref))
4005 and then Known_Static_Esize (Entity (Pref))
4006 then
4007 Siz := Esize (Entity (Pref));
4009 -- For an array component, we can do Size in the front end
4010 -- if the component_size of the array is set.
4012 elsif Nkind (Pref) = N_Indexed_Component then
4013 Siz := Component_Size (Etype (Prefix (Pref)));
4015 -- For a record component, we can do Size in the front end if there
4016 -- is a component clause, or if the record is packed and the
4017 -- component's size is known at compile time.
4019 elsif Nkind (Pref) = N_Selected_Component then
4020 declare
4021 Rec : constant Entity_Id := Etype (Prefix (Pref));
4022 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
4024 begin
4025 if Present (Component_Clause (Comp)) then
4026 Siz := Esize (Comp);
4028 elsif Is_Packed (Rec) then
4029 Siz := RM_Size (Ptyp);
4031 else
4032 Apply_Universal_Integer_Attribute_Checks (N);
4033 return;
4034 end if;
4035 end;
4037 -- All other cases are handled by the back end
4039 else
4040 Apply_Universal_Integer_Attribute_Checks (N);
4042 -- If Size is applied to a formal parameter that is of a packed
4043 -- array subtype, then apply Size to the actual subtype.
4045 if Is_Entity_Name (Pref)
4046 and then Is_Formal (Entity (Pref))
4047 and then Is_Array_Type (Ptyp)
4048 and then Is_Packed (Ptyp)
4049 then
4050 Rewrite (N,
4051 Make_Attribute_Reference (Loc,
4052 Prefix =>
4053 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
4054 Attribute_Name => Name_Size));
4055 Analyze_And_Resolve (N, Typ);
4056 end if;
4058 -- If Size applies to a dereference of an access to unconstrained
4059 -- packed array, the back end needs to see its unconstrained
4060 -- nominal type, but also a hint to the actual constrained type.
4062 if Nkind (Pref) = N_Explicit_Dereference
4063 and then Is_Array_Type (Ptyp)
4064 and then not Is_Constrained (Ptyp)
4065 and then Is_Packed (Ptyp)
4066 then
4067 Set_Actual_Designated_Subtype (Pref,
4068 Get_Actual_Subtype (Pref));
4069 end if;
4071 return;
4072 end if;
4074 -- Common processing for record and array component case
4076 if Siz /= No_Uint and then Siz /= 0 then
4077 declare
4078 CS : constant Boolean := Comes_From_Source (N);
4080 begin
4081 Rewrite (N, Make_Integer_Literal (Loc, Siz));
4083 -- This integer literal is not a static expression. We do not
4084 -- call Analyze_And_Resolve here, because this would activate
4085 -- the circuit for deciding that a static value was out of
4086 -- range, and we don't want that.
4088 -- So just manually set the type, mark the expression as non-
4089 -- static, and then ensure that the result is checked properly
4090 -- if the attribute comes from source (if it was internally
4091 -- generated, we never need a constraint check).
4093 Set_Etype (N, Typ);
4094 Set_Is_Static_Expression (N, False);
4096 if CS then
4097 Apply_Constraint_Check (N, Typ);
4098 end if;
4099 end;
4100 end if;
4101 end Size;
4103 ------------------
4104 -- Storage_Pool --
4105 ------------------
4107 when Attribute_Storage_Pool =>
4108 Rewrite (N,
4109 Make_Type_Conversion (Loc,
4110 Subtype_Mark => New_Reference_To (Etype (N), Loc),
4111 Expression => New_Reference_To (Entity (N), Loc)));
4112 Analyze_And_Resolve (N, Typ);
4114 ------------------
4115 -- Storage_Size --
4116 ------------------
4118 when Attribute_Storage_Size => Storage_Size : begin
4120 -- Access type case, always go to the root type
4122 -- The case of access types results in a value of zero for the case
4123 -- where no storage size attribute clause has been given. If a
4124 -- storage size has been given, then the attribute is converted
4125 -- to a reference to the variable used to hold this value.
4127 if Is_Access_Type (Ptyp) then
4128 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
4129 Rewrite (N,
4130 Make_Attribute_Reference (Loc,
4131 Prefix => New_Reference_To (Typ, Loc),
4132 Attribute_Name => Name_Max,
4133 Expressions => New_List (
4134 Make_Integer_Literal (Loc, 0),
4135 Convert_To (Typ,
4136 New_Reference_To
4137 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
4139 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
4140 Rewrite (N,
4141 OK_Convert_To (Typ,
4142 Make_Function_Call (Loc,
4143 Name =>
4144 New_Reference_To
4145 (Find_Prim_Op
4146 (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
4147 Attribute_Name (N)),
4148 Loc),
4150 Parameter_Associations => New_List (
4151 New_Reference_To
4152 (Associated_Storage_Pool (Root_Type (Ptyp)), Loc)))));
4154 else
4155 Rewrite (N, Make_Integer_Literal (Loc, 0));
4156 end if;
4158 Analyze_And_Resolve (N, Typ);
4160 -- For tasks, we retrieve the size directly from the TCB. The
4161 -- size may depend on a discriminant of the type, and therefore
4162 -- can be a per-object expression, so type-level information is
4163 -- not sufficient in general. There are four cases to consider:
4165 -- a) If the attribute appears within a task body, the designated
4166 -- TCB is obtained by a call to Self.
4168 -- b) If the prefix of the attribute is the name of a task object,
4169 -- the designated TCB is the one stored in the corresponding record.
4171 -- c) If the prefix is a task type, the size is obtained from the
4172 -- size variable created for each task type
4174 -- d) If no storage_size was specified for the type , there is no
4175 -- size variable, and the value is a system-specific default.
4177 else
4178 if In_Open_Scopes (Ptyp) then
4180 -- Storage_Size (Self)
4182 Rewrite (N,
4183 Convert_To (Typ,
4184 Make_Function_Call (Loc,
4185 Name =>
4186 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4187 Parameter_Associations =>
4188 New_List (
4189 Make_Function_Call (Loc,
4190 Name =>
4191 New_Reference_To (RTE (RE_Self), Loc))))));
4193 elsif not Is_Entity_Name (Pref)
4194 or else not Is_Type (Entity (Pref))
4195 then
4196 -- Storage_Size (Rec (Obj).Size)
4198 Rewrite (N,
4199 Convert_To (Typ,
4200 Make_Function_Call (Loc,
4201 Name =>
4202 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4203 Parameter_Associations =>
4204 New_List (
4205 Make_Selected_Component (Loc,
4206 Prefix =>
4207 Unchecked_Convert_To (
4208 Corresponding_Record_Type (Ptyp),
4209 New_Copy_Tree (Pref)),
4210 Selector_Name =>
4211 Make_Identifier (Loc, Name_uTask_Id))))));
4213 elsif Present (Storage_Size_Variable (Ptyp)) then
4215 -- Static storage size pragma given for type: retrieve value
4216 -- from its allocated storage variable.
4218 Rewrite (N,
4219 Convert_To (Typ,
4220 Make_Function_Call (Loc,
4221 Name => New_Occurrence_Of (
4222 RTE (RE_Adjust_Storage_Size), Loc),
4223 Parameter_Associations =>
4224 New_List (
4225 New_Reference_To (
4226 Storage_Size_Variable (Ptyp), Loc)))));
4227 else
4228 -- Get system default
4230 Rewrite (N,
4231 Convert_To (Typ,
4232 Make_Function_Call (Loc,
4233 Name =>
4234 New_Occurrence_Of (
4235 RTE (RE_Default_Stack_Size), Loc))));
4236 end if;
4238 Analyze_And_Resolve (N, Typ);
4239 end if;
4240 end Storage_Size;
4242 -----------------
4243 -- Stream_Size --
4244 -----------------
4246 when Attribute_Stream_Size => Stream_Size : declare
4247 Size : Int;
4249 begin
4250 -- If we have a Stream_Size clause for this type use it, otherwise
4251 -- the Stream_Size if the size of the type.
4253 if Has_Stream_Size_Clause (Ptyp) then
4254 Size :=
4255 UI_To_Int
4256 (Static_Integer (Expression (Stream_Size_Clause (Ptyp))));
4257 else
4258 Size := UI_To_Int (Esize (Ptyp));
4259 end if;
4261 Rewrite (N, Make_Integer_Literal (Loc, Intval => Size));
4262 Analyze_And_Resolve (N, Typ);
4263 end Stream_Size;
4265 ----------
4266 -- Succ --
4267 ----------
4269 -- 1. Deal with enumeration types with holes
4270 -- 2. For floating-point, generate call to attribute function
4271 -- 3. For other cases, deal with constraint checking
4273 when Attribute_Succ => Succ : declare
4274 Etyp : constant Entity_Id := Base_Type (Ptyp);
4276 begin
4278 -- For enumeration types with non-standard representations, we
4279 -- expand typ'Succ (x) into
4281 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
4283 -- If the representation is contiguous, we compute instead
4284 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
4286 if Is_Enumeration_Type (Ptyp)
4287 and then Present (Enum_Pos_To_Rep (Etyp))
4288 then
4289 if Has_Contiguous_Rep (Etyp) then
4290 Rewrite (N,
4291 Unchecked_Convert_To (Ptyp,
4292 Make_Op_Add (Loc,
4293 Left_Opnd =>
4294 Make_Integer_Literal (Loc,
4295 Enumeration_Rep (First_Literal (Ptyp))),
4296 Right_Opnd =>
4297 Make_Function_Call (Loc,
4298 Name =>
4299 New_Reference_To
4300 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4302 Parameter_Associations =>
4303 New_List (
4304 Unchecked_Convert_To (Ptyp,
4305 Make_Op_Add (Loc,
4306 Left_Opnd =>
4307 Unchecked_Convert_To (Standard_Integer,
4308 Relocate_Node (First (Exprs))),
4309 Right_Opnd =>
4310 Make_Integer_Literal (Loc, 1))),
4311 Rep_To_Pos_Flag (Ptyp, Loc))))));
4312 else
4313 -- Add Boolean parameter True, to request program errror if
4314 -- we have a bad representation on our hands. Add False if
4315 -- checks are suppressed.
4317 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
4318 Rewrite (N,
4319 Make_Indexed_Component (Loc,
4320 Prefix =>
4321 New_Reference_To
4322 (Enum_Pos_To_Rep (Etyp), Loc),
4323 Expressions => New_List (
4324 Make_Op_Add (Loc,
4325 Left_Opnd =>
4326 Make_Function_Call (Loc,
4327 Name =>
4328 New_Reference_To
4329 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4330 Parameter_Associations => Exprs),
4331 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
4332 end if;
4334 Analyze_And_Resolve (N, Typ);
4336 -- For floating-point, we transform 'Succ into a call to the Succ
4337 -- floating-point attribute function in Fat_xxx (xxx is root type)
4339 elsif Is_Floating_Point_Type (Ptyp) then
4340 Expand_Fpt_Attribute_R (N);
4341 Analyze_And_Resolve (N, Typ);
4343 -- For modular types, nothing to do (no overflow, since wraps)
4345 elsif Is_Modular_Integer_Type (Ptyp) then
4346 null;
4348 -- For other types, if argument is marked as needing a range check or
4349 -- overflow checking is enabled, we must generate a check.
4351 elsif not Overflow_Checks_Suppressed (Ptyp)
4352 or else Do_Range_Check (First (Exprs))
4353 then
4354 Set_Do_Range_Check (First (Exprs), False);
4355 Expand_Pred_Succ (N);
4356 end if;
4357 end Succ;
4359 ---------
4360 -- Tag --
4361 ---------
4363 -- Transforms X'Tag into a direct reference to the tag of X
4365 when Attribute_Tag => Tag : declare
4366 Ttyp : Entity_Id;
4367 Prefix_Is_Type : Boolean;
4369 begin
4370 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
4371 Ttyp := Entity (Pref);
4372 Prefix_Is_Type := True;
4373 else
4374 Ttyp := Ptyp;
4375 Prefix_Is_Type := False;
4376 end if;
4378 if Is_Class_Wide_Type (Ttyp) then
4379 Ttyp := Root_Type (Ttyp);
4380 end if;
4382 Ttyp := Underlying_Type (Ttyp);
4384 -- Ada 2005: The type may be a synchronized tagged type, in which
4385 -- case the tag information is stored in the corresponding record.
4387 if Is_Concurrent_Type (Ttyp) then
4388 Ttyp := Corresponding_Record_Type (Ttyp);
4389 end if;
4391 if Prefix_Is_Type then
4393 -- For VMs we leave the type attribute unexpanded because
4394 -- there's not a dispatching table to reference.
4396 if Tagged_Type_Expansion then
4397 Rewrite (N,
4398 Unchecked_Convert_To (RTE (RE_Tag),
4399 New_Reference_To
4400 (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc)));
4401 Analyze_And_Resolve (N, RTE (RE_Tag));
4402 end if;
4404 -- Ada 2005 (AI-251): The use of 'Tag in the sources always
4405 -- references the primary tag of the actual object. If 'Tag is
4406 -- applied to class-wide interface objects we generate code that
4407 -- displaces "this" to reference the base of the object.
4409 elsif Comes_From_Source (N)
4410 and then Is_Class_Wide_Type (Etype (Prefix (N)))
4411 and then Is_Interface (Etype (Prefix (N)))
4412 then
4413 -- Generate:
4414 -- (To_Tag_Ptr (Prefix'Address)).all
4416 -- Note that Prefix'Address is recursively expanded into a call
4417 -- to Base_Address (Obj.Tag)
4419 -- Not needed for VM targets, since all handled by the VM
4421 if Tagged_Type_Expansion then
4422 Rewrite (N,
4423 Make_Explicit_Dereference (Loc,
4424 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4425 Make_Attribute_Reference (Loc,
4426 Prefix => Relocate_Node (Pref),
4427 Attribute_Name => Name_Address))));
4428 Analyze_And_Resolve (N, RTE (RE_Tag));
4429 end if;
4431 else
4432 Rewrite (N,
4433 Make_Selected_Component (Loc,
4434 Prefix => Relocate_Node (Pref),
4435 Selector_Name =>
4436 New_Reference_To (First_Tag_Component (Ttyp), Loc)));
4437 Analyze_And_Resolve (N, RTE (RE_Tag));
4438 end if;
4439 end Tag;
4441 ----------------
4442 -- Terminated --
4443 ----------------
4445 -- Transforms 'Terminated attribute into a call to Terminated function
4447 when Attribute_Terminated => Terminated :
4448 begin
4449 -- The prefix of Terminated is of a task interface class-wide type.
4450 -- Generate:
4451 -- terminated (Task_Id (Pref._disp_get_task_id));
4453 if Ada_Version >= Ada_05
4454 and then Ekind (Ptyp) = E_Class_Wide_Type
4455 and then Is_Interface (Ptyp)
4456 and then Is_Task_Interface (Ptyp)
4457 then
4458 Rewrite (N,
4459 Make_Function_Call (Loc,
4460 Name =>
4461 New_Reference_To (RTE (RE_Terminated), Loc),
4462 Parameter_Associations => New_List (
4463 Make_Unchecked_Type_Conversion (Loc,
4464 Subtype_Mark =>
4465 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
4466 Expression =>
4467 Make_Selected_Component (Loc,
4468 Prefix =>
4469 New_Copy_Tree (Pref),
4470 Selector_Name =>
4471 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
4473 elsif Restricted_Profile then
4474 Rewrite (N,
4475 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
4477 else
4478 Rewrite (N,
4479 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
4480 end if;
4482 Analyze_And_Resolve (N, Standard_Boolean);
4483 end Terminated;
4485 ----------------
4486 -- To_Address --
4487 ----------------
4489 -- Transforms System'To_Address (X) into unchecked conversion
4490 -- from (integral) type of X to type address.
4492 when Attribute_To_Address =>
4493 Rewrite (N,
4494 Unchecked_Convert_To (RTE (RE_Address),
4495 Relocate_Node (First (Exprs))));
4496 Analyze_And_Resolve (N, RTE (RE_Address));
4498 ------------
4499 -- To_Any --
4500 ------------
4502 when Attribute_To_Any => To_Any : declare
4503 P_Type : constant Entity_Id := Etype (Pref);
4504 Decls : constant List_Id := New_List;
4505 begin
4506 Rewrite (N,
4507 Build_To_Any_Call
4508 (Convert_To (P_Type,
4509 Relocate_Node (First (Exprs))), Decls));
4510 Insert_Actions (N, Decls);
4511 Analyze_And_Resolve (N, RTE (RE_Any));
4512 end To_Any;
4514 ----------------
4515 -- Truncation --
4516 ----------------
4518 -- Transforms 'Truncation into a call to the floating-point attribute
4519 -- function Truncation in Fat_xxx (where xxx is the root type).
4520 -- Expansion is avoided for cases the back end can handle directly.
4522 when Attribute_Truncation =>
4523 if not Is_Inline_Floating_Point_Attribute (N) then
4524 Expand_Fpt_Attribute_R (N);
4525 end if;
4527 --------------
4528 -- TypeCode --
4529 --------------
4531 when Attribute_TypeCode => TypeCode : declare
4532 P_Type : constant Entity_Id := Etype (Pref);
4533 Decls : constant List_Id := New_List;
4534 begin
4535 Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls));
4536 Insert_Actions (N, Decls);
4537 Analyze_And_Resolve (N, RTE (RE_TypeCode));
4538 end TypeCode;
4540 -----------------------
4541 -- Unbiased_Rounding --
4542 -----------------------
4544 -- Transforms 'Unbiased_Rounding into a call to the floating-point
4545 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
4546 -- root type). Expansion is avoided for cases the back end can handle
4547 -- directly.
4549 when Attribute_Unbiased_Rounding =>
4550 if not Is_Inline_Floating_Point_Attribute (N) then
4551 Expand_Fpt_Attribute_R (N);
4552 end if;
4554 -----------------
4555 -- UET_Address --
4556 -----------------
4558 when Attribute_UET_Address => UET_Address : declare
4559 Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
4561 begin
4562 Insert_Action (N,
4563 Make_Object_Declaration (Loc,
4564 Defining_Identifier => Ent,
4565 Aliased_Present => True,
4566 Object_Definition =>
4567 New_Occurrence_Of (RTE (RE_Address), Loc)));
4569 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
4570 -- in normal external form.
4572 Get_External_Unit_Name_String (Get_Unit_Name (Pref));
4573 Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
4574 Name_Len := Name_Len + 7;
4575 Name_Buffer (1 .. 7) := "__gnat_";
4576 Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP";
4577 Name_Len := Name_Len + 5;
4579 Set_Is_Imported (Ent);
4580 Set_Interface_Name (Ent,
4581 Make_String_Literal (Loc,
4582 Strval => String_From_Name_Buffer));
4584 -- Set entity as internal to ensure proper Sprint output of its
4585 -- implicit importation.
4587 Set_Is_Internal (Ent);
4589 Rewrite (N,
4590 Make_Attribute_Reference (Loc,
4591 Prefix => New_Occurrence_Of (Ent, Loc),
4592 Attribute_Name => Name_Address));
4594 Analyze_And_Resolve (N, Typ);
4595 end UET_Address;
4597 ---------------
4598 -- VADS_Size --
4599 ---------------
4601 -- The processing for VADS_Size is shared with Size
4603 ---------
4604 -- Val --
4605 ---------
4607 -- For enumeration types with a standard representation, and for all
4608 -- other types, Val is handled by the back end. For enumeration types
4609 -- with a non-standard representation we use the _Pos_To_Rep array that
4610 -- was created when the type was frozen.
4612 when Attribute_Val => Val : declare
4613 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
4615 begin
4616 if Is_Enumeration_Type (Etyp)
4617 and then Present (Enum_Pos_To_Rep (Etyp))
4618 then
4619 if Has_Contiguous_Rep (Etyp) then
4620 declare
4621 Rep_Node : constant Node_Id :=
4622 Unchecked_Convert_To (Etyp,
4623 Make_Op_Add (Loc,
4624 Left_Opnd =>
4625 Make_Integer_Literal (Loc,
4626 Enumeration_Rep (First_Literal (Etyp))),
4627 Right_Opnd =>
4628 (Convert_To (Standard_Integer,
4629 Relocate_Node (First (Exprs))))));
4631 begin
4632 Rewrite (N,
4633 Unchecked_Convert_To (Etyp,
4634 Make_Op_Add (Loc,
4635 Left_Opnd =>
4636 Make_Integer_Literal (Loc,
4637 Enumeration_Rep (First_Literal (Etyp))),
4638 Right_Opnd =>
4639 Make_Function_Call (Loc,
4640 Name =>
4641 New_Reference_To
4642 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4643 Parameter_Associations => New_List (
4644 Rep_Node,
4645 Rep_To_Pos_Flag (Etyp, Loc))))));
4646 end;
4648 else
4649 Rewrite (N,
4650 Make_Indexed_Component (Loc,
4651 Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc),
4652 Expressions => New_List (
4653 Convert_To (Standard_Integer,
4654 Relocate_Node (First (Exprs))))));
4655 end if;
4657 Analyze_And_Resolve (N, Typ);
4659 -- If the argument is marked as requiring a range check then generate
4660 -- it here.
4662 elsif Do_Range_Check (First (Exprs)) then
4663 Set_Do_Range_Check (First (Exprs), False);
4664 Generate_Range_Check (First (Exprs), Etyp, CE_Range_Check_Failed);
4665 end if;
4666 end Val;
4668 -----------
4669 -- Valid --
4670 -----------
4672 -- The code for valid is dependent on the particular types involved.
4673 -- See separate sections below for the generated code in each case.
4675 when Attribute_Valid => Valid : declare
4676 Btyp : Entity_Id := Base_Type (Ptyp);
4677 Tst : Node_Id;
4679 Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
4680 -- Save the validity checking mode. We always turn off validity
4681 -- checking during process of 'Valid since this is one place
4682 -- where we do not want the implicit validity checks to intefere
4683 -- with the explicit validity check that the programmer is doing.
4685 function Make_Range_Test return Node_Id;
4686 -- Build the code for a range test of the form
4687 -- Btyp!(Pref) >= Btyp!(Ptyp'First)
4688 -- and then
4689 -- Btyp!(Pref) <= Btyp!(Ptyp'Last)
4691 ---------------------
4692 -- Make_Range_Test --
4693 ---------------------
4695 function Make_Range_Test return Node_Id is
4696 Temp : constant Node_Id := Duplicate_Subexpr (Pref);
4698 begin
4699 -- The value whose validity is being checked has been captured in
4700 -- an object declaration. We certainly don't want this object to
4701 -- appear valid because the declaration initializes it!
4703 if Is_Entity_Name (Temp) then
4704 Set_Is_Known_Valid (Entity (Temp), False);
4705 end if;
4707 return
4708 Make_And_Then (Loc,
4709 Left_Opnd =>
4710 Make_Op_Ge (Loc,
4711 Left_Opnd =>
4712 Unchecked_Convert_To (Btyp, Temp),
4714 Right_Opnd =>
4715 Unchecked_Convert_To (Btyp,
4716 Make_Attribute_Reference (Loc,
4717 Prefix => New_Occurrence_Of (Ptyp, Loc),
4718 Attribute_Name => Name_First))),
4720 Right_Opnd =>
4721 Make_Op_Le (Loc,
4722 Left_Opnd =>
4723 Unchecked_Convert_To (Btyp, Temp),
4725 Right_Opnd =>
4726 Unchecked_Convert_To (Btyp,
4727 Make_Attribute_Reference (Loc,
4728 Prefix => New_Occurrence_Of (Ptyp, Loc),
4729 Attribute_Name => Name_Last))));
4730 end Make_Range_Test;
4732 -- Start of processing for Attribute_Valid
4734 begin
4735 -- Do not expand sourced code 'Valid reference in CodePeer mode,
4736 -- will be handled by the back-end directly.
4738 if CodePeer_Mode and then Comes_From_Source (N) then
4739 return;
4740 end if;
4742 -- Turn off validity checks. We do not want any implicit validity
4743 -- checks to intefere with the explicit check from the attribute
4745 Validity_Checks_On := False;
4747 -- Floating-point case. This case is handled by the Valid attribute
4748 -- code in the floating-point attribute run-time library.
4750 if Is_Floating_Point_Type (Ptyp) then
4751 declare
4752 Pkg : RE_Id;
4753 Ftp : Entity_Id;
4755 begin
4756 -- For vax fpt types, call appropriate routine in special vax
4757 -- floating point unit. We do not have to worry about loads in
4758 -- this case, since these types have no signalling NaN's.
4760 if Vax_Float (Btyp) then
4761 Expand_Vax_Valid (N);
4763 -- The AAMP back end handles Valid for floating-point types
4765 elsif Is_AAMP_Float (Btyp) then
4766 Analyze_And_Resolve (Pref, Ptyp);
4767 Set_Etype (N, Standard_Boolean);
4768 Set_Analyzed (N);
4770 -- Non VAX float case
4772 else
4773 Find_Fat_Info (Ptyp, Ftp, Pkg);
4775 -- If the floating-point object might be unaligned, we need
4776 -- to call the special routine Unaligned_Valid, which makes
4777 -- the needed copy, being careful not to load the value into
4778 -- any floating-point register. The argument in this case is
4779 -- obj'Address (see Unaligned_Valid routine in Fat_Gen).
4781 if Is_Possibly_Unaligned_Object (Pref) then
4782 Expand_Fpt_Attribute
4783 (N, Pkg, Name_Unaligned_Valid,
4784 New_List (
4785 Make_Attribute_Reference (Loc,
4786 Prefix => Relocate_Node (Pref),
4787 Attribute_Name => Name_Address)));
4789 -- In the normal case where we are sure the object is
4790 -- aligned, we generate a call to Valid, and the argument in
4791 -- this case is obj'Unrestricted_Access (after converting
4792 -- obj to the right floating-point type).
4794 else
4795 Expand_Fpt_Attribute
4796 (N, Pkg, Name_Valid,
4797 New_List (
4798 Make_Attribute_Reference (Loc,
4799 Prefix => Unchecked_Convert_To (Ftp, Pref),
4800 Attribute_Name => Name_Unrestricted_Access)));
4801 end if;
4802 end if;
4804 -- One more task, we still need a range check. Required
4805 -- only if we have a constraint, since the Valid routine
4806 -- catches infinities properly (infinities are never valid).
4808 -- The way we do the range check is simply to create the
4809 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
4811 if not Subtypes_Statically_Match (Ptyp, Btyp) then
4812 Rewrite (N,
4813 Make_And_Then (Loc,
4814 Left_Opnd => Relocate_Node (N),
4815 Right_Opnd =>
4816 Make_In (Loc,
4817 Left_Opnd => Convert_To (Btyp, Pref),
4818 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
4819 end if;
4820 end;
4822 -- Enumeration type with holes
4824 -- For enumeration types with holes, the Pos value constructed by
4825 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
4826 -- second argument of False returns minus one for an invalid value,
4827 -- and the non-negative pos value for a valid value, so the
4828 -- expansion of X'Valid is simply:
4830 -- type(X)'Pos (X) >= 0
4832 -- We can't quite generate it that way because of the requirement
4833 -- for the non-standard second argument of False in the resulting
4834 -- rep_to_pos call, so we have to explicitly create:
4836 -- _rep_to_pos (X, False) >= 0
4838 -- If we have an enumeration subtype, we also check that the
4839 -- value is in range:
4841 -- _rep_to_pos (X, False) >= 0
4842 -- and then
4843 -- (X >= type(X)'First and then type(X)'Last <= X)
4845 elsif Is_Enumeration_Type (Ptyp)
4846 and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp)))
4847 then
4848 Tst :=
4849 Make_Op_Ge (Loc,
4850 Left_Opnd =>
4851 Make_Function_Call (Loc,
4852 Name =>
4853 New_Reference_To
4854 (TSS (Base_Type (Ptyp), TSS_Rep_To_Pos), Loc),
4855 Parameter_Associations => New_List (
4856 Pref,
4857 New_Occurrence_Of (Standard_False, Loc))),
4858 Right_Opnd => Make_Integer_Literal (Loc, 0));
4860 if Ptyp /= Btyp
4861 and then
4862 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
4863 or else
4864 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
4865 then
4866 -- The call to Make_Range_Test will create declarations
4867 -- that need a proper insertion point, but Pref is now
4868 -- attached to a node with no ancestor. Attach to tree
4869 -- even if it is to be rewritten below.
4871 Set_Parent (Tst, Parent (N));
4873 Tst :=
4874 Make_And_Then (Loc,
4875 Left_Opnd => Make_Range_Test,
4876 Right_Opnd => Tst);
4877 end if;
4879 Rewrite (N, Tst);
4881 -- Fortran convention booleans
4883 -- For the very special case of Fortran convention booleans, the
4884 -- value is always valid, since it is an integer with the semantics
4885 -- that non-zero is true, and any value is permissible.
4887 elsif Is_Boolean_Type (Ptyp)
4888 and then Convention (Ptyp) = Convention_Fortran
4889 then
4890 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4892 -- For biased representations, we will be doing an unchecked
4893 -- conversion without unbiasing the result. That means that the range
4894 -- test has to take this into account, and the proper form of the
4895 -- test is:
4897 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
4899 elsif Has_Biased_Representation (Ptyp) then
4900 Btyp := RTE (RE_Unsigned_32);
4901 Rewrite (N,
4902 Make_Op_Lt (Loc,
4903 Left_Opnd =>
4904 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
4905 Right_Opnd =>
4906 Unchecked_Convert_To (Btyp,
4907 Make_Attribute_Reference (Loc,
4908 Prefix => New_Occurrence_Of (Ptyp, Loc),
4909 Attribute_Name => Name_Range_Length))));
4911 -- For all other scalar types, what we want logically is a
4912 -- range test:
4914 -- X in type(X)'First .. type(X)'Last
4916 -- But that's precisely what won't work because of possible
4917 -- unwanted optimization (and indeed the basic motivation for
4918 -- the Valid attribute is exactly that this test does not work!)
4919 -- What will work is:
4921 -- Btyp!(X) >= Btyp!(type(X)'First)
4922 -- and then
4923 -- Btyp!(X) <= Btyp!(type(X)'Last)
4925 -- where Btyp is an integer type large enough to cover the full
4926 -- range of possible stored values (i.e. it is chosen on the basis
4927 -- of the size of the type, not the range of the values). We write
4928 -- this as two tests, rather than a range check, so that static
4929 -- evaluation will easily remove either or both of the checks if
4930 -- they can be -statically determined to be true (this happens
4931 -- when the type of X is static and the range extends to the full
4932 -- range of stored values).
4934 -- Unsigned types. Note: it is safe to consider only whether the
4935 -- subtype is unsigned, since we will in that case be doing all
4936 -- unsigned comparisons based on the subtype range. Since we use the
4937 -- actual subtype object size, this is appropriate.
4939 -- For example, if we have
4941 -- subtype x is integer range 1 .. 200;
4942 -- for x'Object_Size use 8;
4944 -- Now the base type is signed, but objects of this type are bits
4945 -- unsigned, and doing an unsigned test of the range 1 to 200 is
4946 -- correct, even though a value greater than 127 looks signed to a
4947 -- signed comparison.
4949 elsif Is_Unsigned_Type (Ptyp) then
4950 if Esize (Ptyp) <= 32 then
4951 Btyp := RTE (RE_Unsigned_32);
4952 else
4953 Btyp := RTE (RE_Unsigned_64);
4954 end if;
4956 Rewrite (N, Make_Range_Test);
4958 -- Signed types
4960 else
4961 if Esize (Ptyp) <= Esize (Standard_Integer) then
4962 Btyp := Standard_Integer;
4963 else
4964 Btyp := Universal_Integer;
4965 end if;
4967 Rewrite (N, Make_Range_Test);
4968 end if;
4970 Analyze_And_Resolve (N, Standard_Boolean);
4971 Validity_Checks_On := Save_Validity_Checks_On;
4972 end Valid;
4974 -----------
4975 -- Value --
4976 -----------
4978 -- Value attribute is handled in separate unti Exp_Imgv
4980 when Attribute_Value =>
4981 Exp_Imgv.Expand_Value_Attribute (N);
4983 -----------------
4984 -- Value_Size --
4985 -----------------
4987 -- The processing for Value_Size shares the processing for Size
4989 -------------
4990 -- Version --
4991 -------------
4993 -- The processing for Version shares the processing for Body_Version
4995 ----------------
4996 -- Wide_Image --
4997 ----------------
4999 -- Wide_Image attribute is handled in separate unit Exp_Imgv
5001 when Attribute_Wide_Image =>
5002 Exp_Imgv.Expand_Wide_Image_Attribute (N);
5004 ---------------------
5005 -- Wide_Wide_Image --
5006 ---------------------
5008 -- Wide_Wide_Image attribute is handled in separate unit Exp_Imgv
5010 when Attribute_Wide_Wide_Image =>
5011 Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N);
5013 ----------------
5014 -- Wide_Value --
5015 ----------------
5017 -- We expand typ'Wide_Value (X) into
5019 -- typ'Value
5020 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
5022 -- Wide_String_To_String is a runtime function that converts its wide
5023 -- string argument to String, converting any non-translatable characters
5024 -- into appropriate escape sequences. This preserves the required
5025 -- semantics of Wide_Value in all cases, and results in a very simple
5026 -- implementation approach.
5028 -- Note: for this approach to be fully standard compliant for the cases
5029 -- where typ is Wide_Character and Wide_Wide_Character, the encoding
5030 -- method must cover the entire character range (e.g. UTF-8). But that
5031 -- is a reasonable requirement when dealing with encoded character
5032 -- sequences. Presumably if one of the restrictive encoding mechanisms
5033 -- is in use such as Shift-JIS, then characters that cannot be
5034 -- represented using this encoding will not appear in any case.
5036 when Attribute_Wide_Value => Wide_Value :
5037 begin
5038 Rewrite (N,
5039 Make_Attribute_Reference (Loc,
5040 Prefix => Pref,
5041 Attribute_Name => Name_Value,
5043 Expressions => New_List (
5044 Make_Function_Call (Loc,
5045 Name =>
5046 New_Reference_To (RTE (RE_Wide_String_To_String), Loc),
5048 Parameter_Associations => New_List (
5049 Relocate_Node (First (Exprs)),
5050 Make_Integer_Literal (Loc,
5051 Intval => Int (Wide_Character_Encoding_Method)))))));
5053 Analyze_And_Resolve (N, Typ);
5054 end Wide_Value;
5056 ---------------------
5057 -- Wide_Wide_Value --
5058 ---------------------
5060 -- We expand typ'Wide_Value_Value (X) into
5062 -- typ'Value
5063 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
5065 -- Wide_Wide_String_To_String is a runtime function that converts its
5066 -- wide string argument to String, converting any non-translatable
5067 -- characters into appropriate escape sequences. This preserves the
5068 -- required semantics of Wide_Wide_Value in all cases, and results in a
5069 -- very simple implementation approach.
5071 -- It's not quite right where typ = Wide_Wide_Character, because the
5072 -- encoding method may not cover the whole character type ???
5074 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
5075 begin
5076 Rewrite (N,
5077 Make_Attribute_Reference (Loc,
5078 Prefix => Pref,
5079 Attribute_Name => Name_Value,
5081 Expressions => New_List (
5082 Make_Function_Call (Loc,
5083 Name =>
5084 New_Reference_To (RTE (RE_Wide_Wide_String_To_String), Loc),
5086 Parameter_Associations => New_List (
5087 Relocate_Node (First (Exprs)),
5088 Make_Integer_Literal (Loc,
5089 Intval => Int (Wide_Character_Encoding_Method)))))));
5091 Analyze_And_Resolve (N, Typ);
5092 end Wide_Wide_Value;
5094 ---------------------
5095 -- Wide_Wide_Width --
5096 ---------------------
5098 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
5100 when Attribute_Wide_Wide_Width =>
5101 Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide);
5103 ----------------
5104 -- Wide_Width --
5105 ----------------
5107 -- Wide_Width attribute is handled in separate unit Exp_Imgv
5109 when Attribute_Wide_Width =>
5110 Exp_Imgv.Expand_Width_Attribute (N, Wide);
5112 -----------
5113 -- Width --
5114 -----------
5116 -- Width attribute is handled in separate unit Exp_Imgv
5118 when Attribute_Width =>
5119 Exp_Imgv.Expand_Width_Attribute (N, Normal);
5121 -----------
5122 -- Write --
5123 -----------
5125 when Attribute_Write => Write : declare
5126 P_Type : constant Entity_Id := Entity (Pref);
5127 U_Type : constant Entity_Id := Underlying_Type (P_Type);
5128 Pname : Entity_Id;
5129 Decl : Node_Id;
5130 Prag : Node_Id;
5131 Arg3 : Node_Id;
5132 Wfunc : Node_Id;
5134 begin
5135 -- If no underlying type, we have an error that will be diagnosed
5136 -- elsewhere, so here we just completely ignore the expansion.
5138 if No (U_Type) then
5139 return;
5140 end if;
5142 -- The simple case, if there is a TSS for Write, just call it
5144 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
5146 if Present (Pname) then
5147 null;
5149 else
5150 -- If there is a Stream_Convert pragma, use it, we rewrite
5152 -- sourcetyp'Output (stream, Item)
5154 -- as
5156 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
5158 -- where strmwrite is the given Write function that converts an
5159 -- argument of type sourcetyp or a type acctyp, from which it is
5160 -- derived to type strmtyp. The conversion to acttyp is required
5161 -- for the derived case.
5163 Prag := Get_Stream_Convert_Pragma (P_Type);
5165 if Present (Prag) then
5166 Arg3 :=
5167 Next (Next (First (Pragma_Argument_Associations (Prag))));
5168 Wfunc := Entity (Expression (Arg3));
5170 Rewrite (N,
5171 Make_Attribute_Reference (Loc,
5172 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
5173 Attribute_Name => Name_Output,
5174 Expressions => New_List (
5175 Relocate_Node (First (Exprs)),
5176 Make_Function_Call (Loc,
5177 Name => New_Occurrence_Of (Wfunc, Loc),
5178 Parameter_Associations => New_List (
5179 OK_Convert_To (Etype (First_Formal (Wfunc)),
5180 Relocate_Node (Next (First (Exprs)))))))));
5182 Analyze (N);
5183 return;
5185 -- For elementary types, we call the W_xxx routine directly
5187 elsif Is_Elementary_Type (U_Type) then
5188 Rewrite (N, Build_Elementary_Write_Call (N));
5189 Analyze (N);
5190 return;
5192 -- Array type case
5194 elsif Is_Array_Type (U_Type) then
5195 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
5196 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5198 -- Tagged type case, use the primitive Write function. Note that
5199 -- this will dispatch in the class-wide case which is what we want
5201 elsif Is_Tagged_Type (U_Type) then
5202 Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
5204 -- All other record type cases, including protected records.
5205 -- The latter only arise for expander generated code for
5206 -- handling shared passive partition access.
5208 else
5209 pragma Assert
5210 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5212 -- Ada 2005 (AI-216): Program_Error is raised when executing
5213 -- the default implementation of the Write attribute of an
5214 -- Unchecked_Union type. However, if the 'Write reference is
5215 -- within the generated Output stream procedure, Write outputs
5216 -- the components, and the default values of the discriminant
5217 -- are streamed by the Output procedure itself.
5219 if Is_Unchecked_Union (Base_Type (U_Type))
5220 and not Is_TSS (Current_Scope, TSS_Stream_Output)
5221 then
5222 Insert_Action (N,
5223 Make_Raise_Program_Error (Loc,
5224 Reason => PE_Unchecked_Union_Restriction));
5225 end if;
5227 if Has_Discriminants (U_Type)
5228 and then Present
5229 (Discriminant_Default_Value (First_Discriminant (U_Type)))
5230 then
5231 Build_Mutable_Record_Write_Procedure
5232 (Loc, Base_Type (U_Type), Decl, Pname);
5233 else
5234 Build_Record_Write_Procedure
5235 (Loc, Base_Type (U_Type), Decl, Pname);
5236 end if;
5238 Insert_Action (N, Decl);
5239 end if;
5240 end if;
5242 -- If we fall through, Pname is the procedure to be called
5244 Rewrite_Stream_Proc_Call (Pname);
5245 end Write;
5247 -- Component_Size is handled by the back end, unless the component size
5248 -- is known at compile time, which is always true in the packed array
5249 -- case. It is important that the packed array case is handled in the
5250 -- front end (see Eval_Attribute) since the back end would otherwise get
5251 -- confused by the equivalent packed array type.
5253 when Attribute_Component_Size =>
5254 null;
5256 -- The following attributes are handled by the back end (except that
5257 -- static cases have already been evaluated during semantic processing,
5258 -- but in any case the back end should not count on this). The one bit
5259 -- of special processing required is that these attributes typically
5260 -- generate conditionals in the code, so we need to check the relevant
5261 -- restriction.
5263 when Attribute_Max |
5264 Attribute_Min =>
5265 Check_Restriction (No_Implicit_Conditionals, N);
5267 -- The following attributes are handled by the back end (except that
5268 -- static cases have already been evaluated during semantic processing,
5269 -- but in any case the back end should not count on this).
5271 -- The back end also handles the non-class-wide cases of Size
5273 when Attribute_Bit_Order |
5274 Attribute_Code_Address |
5275 Attribute_Definite |
5276 Attribute_Null_Parameter |
5277 Attribute_Passed_By_Reference |
5278 Attribute_Pool_Address =>
5279 null;
5281 -- The following attributes are also handled by the back end, but return
5282 -- a universal integer result, so may need a conversion for checking
5283 -- that the result is in range.
5285 when Attribute_Aft |
5286 Attribute_Max_Size_In_Storage_Elements
5288 Apply_Universal_Integer_Attribute_Checks (N);
5290 -- The following attributes should not appear at this stage, since they
5291 -- have already been handled by the analyzer (and properly rewritten
5292 -- with corresponding values or entities to represent the right values)
5294 when Attribute_Abort_Signal |
5295 Attribute_Address_Size |
5296 Attribute_Base |
5297 Attribute_Class |
5298 Attribute_Compiler_Version |
5299 Attribute_Default_Bit_Order |
5300 Attribute_Delta |
5301 Attribute_Denorm |
5302 Attribute_Digits |
5303 Attribute_Emax |
5304 Attribute_Enabled |
5305 Attribute_Epsilon |
5306 Attribute_Fast_Math |
5307 Attribute_Has_Access_Values |
5308 Attribute_Has_Discriminants |
5309 Attribute_Has_Tagged_Values |
5310 Attribute_Large |
5311 Attribute_Machine_Emax |
5312 Attribute_Machine_Emin |
5313 Attribute_Machine_Mantissa |
5314 Attribute_Machine_Overflows |
5315 Attribute_Machine_Radix |
5316 Attribute_Machine_Rounds |
5317 Attribute_Maximum_Alignment |
5318 Attribute_Model_Emin |
5319 Attribute_Model_Epsilon |
5320 Attribute_Model_Mantissa |
5321 Attribute_Model_Small |
5322 Attribute_Modulus |
5323 Attribute_Partition_ID |
5324 Attribute_Range |
5325 Attribute_Safe_Emax |
5326 Attribute_Safe_First |
5327 Attribute_Safe_Large |
5328 Attribute_Safe_Last |
5329 Attribute_Safe_Small |
5330 Attribute_Scale |
5331 Attribute_Signed_Zeros |
5332 Attribute_Small |
5333 Attribute_Storage_Unit |
5334 Attribute_Stub_Type |
5335 Attribute_Target_Name |
5336 Attribute_Type_Class |
5337 Attribute_Unconstrained_Array |
5338 Attribute_Universal_Literal_String |
5339 Attribute_Wchar_T_Size |
5340 Attribute_Word_Size =>
5342 raise Program_Error;
5344 -- The Asm_Input and Asm_Output attributes are not expanded at this
5345 -- stage, but will be eliminated in the expansion of the Asm call, see
5346 -- Exp_Intr for details. So the back end will never see these either.
5348 when Attribute_Asm_Input |
5349 Attribute_Asm_Output =>
5351 null;
5353 end case;
5355 exception
5356 when RE_Not_Available =>
5357 return;
5358 end Expand_N_Attribute_Reference;
5360 ----------------------
5361 -- Expand_Pred_Succ --
5362 ----------------------
5364 -- For typ'Pred (exp), we generate the check
5366 -- [constraint_error when exp = typ'Base'First]
5368 -- Similarly, for typ'Succ (exp), we generate the check
5370 -- [constraint_error when exp = typ'Base'Last]
5372 -- These checks are not generated for modular types, since the proper
5373 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
5375 procedure Expand_Pred_Succ (N : Node_Id) is
5376 Loc : constant Source_Ptr := Sloc (N);
5377 Cnam : Name_Id;
5379 begin
5380 if Attribute_Name (N) = Name_Pred then
5381 Cnam := Name_First;
5382 else
5383 Cnam := Name_Last;
5384 end if;
5386 Insert_Action (N,
5387 Make_Raise_Constraint_Error (Loc,
5388 Condition =>
5389 Make_Op_Eq (Loc,
5390 Left_Opnd =>
5391 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
5392 Right_Opnd =>
5393 Make_Attribute_Reference (Loc,
5394 Prefix =>
5395 New_Reference_To (Base_Type (Etype (Prefix (N))), Loc),
5396 Attribute_Name => Cnam)),
5397 Reason => CE_Overflow_Check_Failed));
5398 end Expand_Pred_Succ;
5400 -------------------
5401 -- Find_Fat_Info --
5402 -------------------
5404 procedure Find_Fat_Info
5405 (T : Entity_Id;
5406 Fat_Type : out Entity_Id;
5407 Fat_Pkg : out RE_Id)
5409 Btyp : constant Entity_Id := Base_Type (T);
5410 Rtyp : constant Entity_Id := Root_Type (T);
5411 Digs : constant Nat := UI_To_Int (Digits_Value (Btyp));
5413 begin
5414 -- If the base type is VAX float, then get appropriate VAX float type
5416 if Vax_Float (Btyp) then
5417 case Digs is
5418 when 6 =>
5419 Fat_Type := RTE (RE_Fat_VAX_F);
5420 Fat_Pkg := RE_Attr_VAX_F_Float;
5422 when 9 =>
5423 Fat_Type := RTE (RE_Fat_VAX_D);
5424 Fat_Pkg := RE_Attr_VAX_D_Float;
5426 when 15 =>
5427 Fat_Type := RTE (RE_Fat_VAX_G);
5428 Fat_Pkg := RE_Attr_VAX_G_Float;
5430 when others =>
5431 raise Program_Error;
5432 end case;
5434 -- If root type is VAX float, this is the case where the library has
5435 -- been recompiled in VAX float mode, and we have an IEEE float type.
5436 -- This is when we use the special IEEE Fat packages.
5438 elsif Vax_Float (Rtyp) then
5439 case Digs is
5440 when 6 =>
5441 Fat_Type := RTE (RE_Fat_IEEE_Short);
5442 Fat_Pkg := RE_Attr_IEEE_Short;
5444 when 15 =>
5445 Fat_Type := RTE (RE_Fat_IEEE_Long);
5446 Fat_Pkg := RE_Attr_IEEE_Long;
5448 when others =>
5449 raise Program_Error;
5450 end case;
5452 -- If neither the base type nor the root type is VAX_Float then VAX
5453 -- float is out of the picture, and we can just use the root type.
5455 else
5456 Fat_Type := Rtyp;
5458 if Fat_Type = Standard_Short_Float then
5459 Fat_Pkg := RE_Attr_Short_Float;
5461 elsif Fat_Type = Standard_Float then
5462 Fat_Pkg := RE_Attr_Float;
5464 elsif Fat_Type = Standard_Long_Float then
5465 Fat_Pkg := RE_Attr_Long_Float;
5467 elsif Fat_Type = Standard_Long_Long_Float then
5468 Fat_Pkg := RE_Attr_Long_Long_Float;
5470 -- Universal real (which is its own root type) is treated as being
5471 -- equivalent to Standard.Long_Long_Float, since it is defined to
5472 -- have the same precision as the longest Float type.
5474 elsif Fat_Type = Universal_Real then
5475 Fat_Type := Standard_Long_Long_Float;
5476 Fat_Pkg := RE_Attr_Long_Long_Float;
5478 else
5479 raise Program_Error;
5480 end if;
5481 end if;
5482 end Find_Fat_Info;
5484 ----------------------------
5485 -- Find_Stream_Subprogram --
5486 ----------------------------
5488 function Find_Stream_Subprogram
5489 (Typ : Entity_Id;
5490 Nam : TSS_Name_Type) return Entity_Id
5492 Base_Typ : constant Entity_Id := Base_Type (Typ);
5493 Ent : constant Entity_Id := TSS (Typ, Nam);
5495 begin
5496 if Present (Ent) then
5497 return Ent;
5498 end if;
5500 -- Stream attributes for strings are expanded into library calls. The
5501 -- following checks are disabled when the run-time is not available or
5502 -- when compiling predefined types due to bootstrap issues. As a result,
5503 -- the compiler will generate in-place stream routines for string types
5504 -- that appear in GNAT's library, but will generate calls via rtsfind
5505 -- to library routines for user code.
5506 -- ??? For now, disable this code for JVM, since this generates a
5507 -- VerifyError exception at run-time on e.g. c330001.
5508 -- This is disabled for AAMP, to avoid making dependences on files not
5509 -- supported in the AAMP library (such as s-fileio.adb).
5511 if VM_Target /= JVM_Target
5512 and then not AAMP_On_Target
5513 and then
5514 not Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
5515 then
5516 -- String as defined in package Ada
5518 if Base_Typ = Standard_String then
5519 if Restriction_Active (No_Stream_Optimizations) then
5520 if Nam = TSS_Stream_Input then
5521 return RTE (RE_String_Input);
5523 elsif Nam = TSS_Stream_Output then
5524 return RTE (RE_String_Output);
5526 elsif Nam = TSS_Stream_Read then
5527 return RTE (RE_String_Read);
5529 else pragma Assert (Nam = TSS_Stream_Write);
5530 return RTE (RE_String_Write);
5531 end if;
5533 else
5534 if Nam = TSS_Stream_Input then
5535 return RTE (RE_String_Input_Blk_IO);
5537 elsif Nam = TSS_Stream_Output then
5538 return RTE (RE_String_Output_Blk_IO);
5540 elsif Nam = TSS_Stream_Read then
5541 return RTE (RE_String_Read_Blk_IO);
5543 else pragma Assert (Nam = TSS_Stream_Write);
5544 return RTE (RE_String_Write_Blk_IO);
5545 end if;
5546 end if;
5548 -- Wide_String as defined in package Ada
5550 elsif Base_Typ = Standard_Wide_String then
5551 if Restriction_Active (No_Stream_Optimizations) then
5552 if Nam = TSS_Stream_Input then
5553 return RTE (RE_Wide_String_Input);
5555 elsif Nam = TSS_Stream_Output then
5556 return RTE (RE_Wide_String_Output);
5558 elsif Nam = TSS_Stream_Read then
5559 return RTE (RE_Wide_String_Read);
5561 else pragma Assert (Nam = TSS_Stream_Write);
5562 return RTE (RE_Wide_String_Write);
5563 end if;
5565 else
5566 if Nam = TSS_Stream_Input then
5567 return RTE (RE_Wide_String_Input_Blk_IO);
5569 elsif Nam = TSS_Stream_Output then
5570 return RTE (RE_Wide_String_Output_Blk_IO);
5572 elsif Nam = TSS_Stream_Read then
5573 return RTE (RE_Wide_String_Read_Blk_IO);
5575 else pragma Assert (Nam = TSS_Stream_Write);
5576 return RTE (RE_Wide_String_Write_Blk_IO);
5577 end if;
5578 end if;
5580 -- Wide_Wide_String as defined in package Ada
5582 elsif Base_Typ = Standard_Wide_Wide_String then
5583 if Restriction_Active (No_Stream_Optimizations) then
5584 if Nam = TSS_Stream_Input then
5585 return RTE (RE_Wide_Wide_String_Input);
5587 elsif Nam = TSS_Stream_Output then
5588 return RTE (RE_Wide_Wide_String_Output);
5590 elsif Nam = TSS_Stream_Read then
5591 return RTE (RE_Wide_Wide_String_Read);
5593 else pragma Assert (Nam = TSS_Stream_Write);
5594 return RTE (RE_Wide_Wide_String_Write);
5595 end if;
5597 else
5598 if Nam = TSS_Stream_Input then
5599 return RTE (RE_Wide_Wide_String_Input_Blk_IO);
5601 elsif Nam = TSS_Stream_Output then
5602 return RTE (RE_Wide_Wide_String_Output_Blk_IO);
5604 elsif Nam = TSS_Stream_Read then
5605 return RTE (RE_Wide_Wide_String_Read_Blk_IO);
5607 else pragma Assert (Nam = TSS_Stream_Write);
5608 return RTE (RE_Wide_Wide_String_Write_Blk_IO);
5609 end if;
5610 end if;
5611 end if;
5612 end if;
5614 if Is_Tagged_Type (Typ)
5615 and then Is_Derived_Type (Typ)
5616 then
5617 return Find_Prim_Op (Typ, Nam);
5618 else
5619 return Find_Inherited_TSS (Typ, Nam);
5620 end if;
5621 end Find_Stream_Subprogram;
5623 -----------------------
5624 -- Get_Index_Subtype --
5625 -----------------------
5627 function Get_Index_Subtype (N : Node_Id) return Node_Id is
5628 P_Type : Entity_Id := Etype (Prefix (N));
5629 Indx : Node_Id;
5630 J : Int;
5632 begin
5633 if Is_Access_Type (P_Type) then
5634 P_Type := Designated_Type (P_Type);
5635 end if;
5637 if No (Expressions (N)) then
5638 J := 1;
5639 else
5640 J := UI_To_Int (Expr_Value (First (Expressions (N))));
5641 end if;
5643 Indx := First_Index (P_Type);
5644 while J > 1 loop
5645 Next_Index (Indx);
5646 J := J - 1;
5647 end loop;
5649 return Etype (Indx);
5650 end Get_Index_Subtype;
5652 -------------------------------
5653 -- Get_Stream_Convert_Pragma --
5654 -------------------------------
5656 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is
5657 Typ : Entity_Id;
5658 N : Node_Id;
5660 begin
5661 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
5662 -- that a stream convert pragma for a tagged type is not inherited from
5663 -- its parent. Probably what is wrong here is that it is basically
5664 -- incorrect to consider a stream convert pragma to be a representation
5665 -- pragma at all ???
5667 N := First_Rep_Item (Implementation_Base_Type (T));
5668 while Present (N) loop
5669 if Nkind (N) = N_Pragma
5670 and then Pragma_Name (N) = Name_Stream_Convert
5671 then
5672 -- For tagged types this pragma is not inherited, so we
5673 -- must verify that it is defined for the given type and
5674 -- not an ancestor.
5676 Typ :=
5677 Entity (Expression (First (Pragma_Argument_Associations (N))));
5679 if not Is_Tagged_Type (T)
5680 or else T = Typ
5681 or else (Is_Private_Type (Typ) and then T = Full_View (Typ))
5682 then
5683 return N;
5684 end if;
5685 end if;
5687 Next_Rep_Item (N);
5688 end loop;
5690 return Empty;
5691 end Get_Stream_Convert_Pragma;
5693 ---------------------------------
5694 -- Is_Constrained_Packed_Array --
5695 ---------------------------------
5697 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
5698 Arr : Entity_Id := Typ;
5700 begin
5701 if Is_Access_Type (Arr) then
5702 Arr := Designated_Type (Arr);
5703 end if;
5705 return Is_Array_Type (Arr)
5706 and then Is_Constrained (Arr)
5707 and then Present (Packed_Array_Type (Arr));
5708 end Is_Constrained_Packed_Array;
5710 ----------------------------------------
5711 -- Is_Inline_Floating_Point_Attribute --
5712 ----------------------------------------
5714 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is
5715 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
5717 begin
5718 if Nkind (Parent (N)) /= N_Type_Conversion
5719 or else not Is_Integer_Type (Etype (Parent (N)))
5720 then
5721 return False;
5722 end if;
5724 -- Should also support 'Machine_Rounding and 'Unbiased_Rounding, but
5725 -- required back end support has not been implemented yet ???
5727 return Id = Attribute_Truncation;
5728 end Is_Inline_Floating_Point_Attribute;
5730 end Exp_Attr;