cfgloopmanip.c (copy_loop_info): New function.
[official-gcc.git] / gcc / ada / exp_attr.adb
blob04930a9bd7cb5fb372580e93156d7ea65e815dd3
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-2012, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Exp_Atag; use Exp_Atag;
31 with Exp_Ch2; use Exp_Ch2;
32 with Exp_Ch3; use Exp_Ch3;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch9; use Exp_Ch9;
35 with Exp_Dist; use Exp_Dist;
36 with Exp_Imgv; use Exp_Imgv;
37 with Exp_Pakd; use Exp_Pakd;
38 with Exp_Strm; use Exp_Strm;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Exp_VFpt; use Exp_VFpt;
42 with Fname; use Fname;
43 with Freeze; use Freeze;
44 with Gnatvsn; use Gnatvsn;
45 with Itypes; use Itypes;
46 with Lib; use Lib;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Nlists; use Nlists;
50 with Opt; use Opt;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sinfo; use Sinfo;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Stringt; use Stringt;
66 with Targparm; use Targparm;
67 with Tbuild; use Tbuild;
68 with Ttypes; use Ttypes;
69 with Uintp; use Uintp;
70 with Uname; use Uname;
71 with Validsw; use Validsw;
73 package body Exp_Attr is
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 function Build_Array_VS_Func
80 (A_Type : Entity_Id;
81 Nod : Node_Id) return Entity_Id;
82 -- Build function to test Valid_Scalars for array type A_Type. Nod is the
83 -- Valid_Scalars attribute node, used to insert the function body, and the
84 -- value returned is the entity of the constructed function body. We do not
85 -- bother to generate a separate spec for this subprogram.
87 procedure Compile_Stream_Body_In_Scope
88 (N : Node_Id;
89 Decl : Node_Id;
90 Arr : Entity_Id;
91 Check : Boolean);
92 -- The body for a stream subprogram may be generated outside of the scope
93 -- of the type. If the type is fully private, it may depend on the full
94 -- view of other types (e.g. indexes) that are currently private as well.
95 -- We install the declarations of the package in which the type is declared
96 -- before compiling the body in what is its proper environment. The Check
97 -- parameter indicates if checks are to be suppressed for the stream body.
98 -- We suppress checks for array/record reads, since the rule is that these
99 -- are like assignments, out of range values due to uninitialized storage,
100 -- or other invalid values do NOT cause a Constraint_Error to be raised.
102 procedure Expand_Access_To_Protected_Op
103 (N : Node_Id;
104 Pref : Node_Id;
105 Typ : Entity_Id);
106 -- An attribute reference to a protected subprogram is transformed into
107 -- a pair of pointers: one to the object, and one to the operations.
108 -- This expansion is performed for 'Access and for 'Unrestricted_Access.
110 procedure Expand_Fpt_Attribute
111 (N : Node_Id;
112 Pkg : RE_Id;
113 Nam : Name_Id;
114 Args : List_Id);
115 -- This procedure expands a call to a floating-point attribute function.
116 -- N is the attribute reference node, and Args is a list of arguments to
117 -- be passed to the function call. Pkg identifies the package containing
118 -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args
119 -- have already been converted to the floating-point type for which Pkg was
120 -- instantiated. The Nam argument is the relevant attribute processing
121 -- routine to be called. This is the same as the attribute name, except in
122 -- the Unaligned_Valid case.
124 procedure Expand_Fpt_Attribute_R (N : Node_Id);
125 -- This procedure expands a call to a floating-point attribute function
126 -- that takes a single floating-point argument. The function to be called
127 -- is always the same as the attribute name.
129 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
130 -- This procedure expands a call to a floating-point attribute function
131 -- that takes one floating-point argument and one integer argument. The
132 -- function to be called is always the same as the attribute name.
134 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
135 -- This procedure expands a call to a floating-point attribute function
136 -- that takes two floating-point arguments. The function to be called
137 -- is always the same as the attribute name.
139 procedure Expand_Pred_Succ (N : Node_Id);
140 -- Handles expansion of Pred or Succ attributes for case of non-real
141 -- operand with overflow checking required.
143 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
144 -- Used for Last, Last, and Length, when the prefix is an array type.
145 -- Obtains the corresponding index subtype.
147 procedure Find_Fat_Info
148 (T : Entity_Id;
149 Fat_Type : out Entity_Id;
150 Fat_Pkg : out RE_Id);
151 -- Given a floating-point type T, identifies the package containing the
152 -- attributes for this type (returned in Fat_Pkg), and the corresponding
153 -- type for which this package was instantiated from Fat_Gen. Error if T
154 -- is not a floating-point type.
156 function Find_Stream_Subprogram
157 (Typ : Entity_Id;
158 Nam : TSS_Name_Type) return Entity_Id;
159 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
160 -- types, the corresponding primitive operation is looked up, else the
161 -- appropriate TSS from the type itself, or from its closest ancestor
162 -- defining it, is returned. In both cases, inheritance of representation
163 -- aspects is thus taken into account.
165 function Full_Base (T : Entity_Id) return Entity_Id;
166 -- The stream functions need to examine the underlying representation of
167 -- composite types. In some cases T may be non-private but its base type
168 -- is, in which case the function returns the corresponding full view.
170 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id;
171 -- Given a type, find a corresponding stream convert pragma that applies to
172 -- the implementation base type of this type (Typ). If found, return the
173 -- pragma node, otherwise return Empty if no pragma is found.
175 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
176 -- Utility for array attributes, returns true on packed constrained
177 -- arrays, and on access to same.
179 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean;
180 -- Returns true iff the given node refers to an attribute call that
181 -- can be expanded directly by the back end and does not need front end
182 -- expansion. Typically used for rounding and truncation attributes that
183 -- appear directly inside a conversion to integer.
185 -------------------------
186 -- Build_Array_VS_Func --
187 -------------------------
189 function Build_Array_VS_Func
190 (A_Type : Entity_Id;
191 Nod : Node_Id) return Entity_Id
193 Loc : constant Source_Ptr := Sloc (Nod);
194 Comp_Type : constant Entity_Id := Component_Type (A_Type);
195 Body_Stmts : List_Id;
196 Index_List : List_Id;
197 Func_Id : Entity_Id;
198 Formals : List_Id;
200 function Test_Component return List_Id;
201 -- Create one statement to test validity of one component designated by
202 -- a full set of indexes. Returns statement list containing test.
204 function Test_One_Dimension (N : Int) return List_Id;
205 -- Create loop to test one dimension of the array. The single statement
206 -- in the loop body tests the inner dimensions if any, or else the
207 -- single component. Note that this procedure is called recursively,
208 -- with N being the dimension to be initialized. A call with N greater
209 -- than the number of dimensions simply generates the component test,
210 -- terminating the recursion. Returns statement list containing tests.
212 --------------------
213 -- Test_Component --
214 --------------------
216 function Test_Component return List_Id is
217 Comp : Node_Id;
218 Anam : Name_Id;
220 begin
221 Comp :=
222 Make_Indexed_Component (Loc,
223 Prefix => Make_Identifier (Loc, Name_uA),
224 Expressions => Index_List);
226 if Is_Scalar_Type (Comp_Type) then
227 Anam := Name_Valid;
228 else
229 Anam := Name_Valid_Scalars;
230 end if;
232 return New_List (
233 Make_If_Statement (Loc,
234 Condition =>
235 Make_Op_Not (Loc,
236 Right_Opnd =>
237 Make_Attribute_Reference (Loc,
238 Attribute_Name => Anam,
239 Prefix => Comp)),
240 Then_Statements => New_List (
241 Make_Simple_Return_Statement (Loc,
242 Expression => New_Occurrence_Of (Standard_False, Loc)))));
243 end Test_Component;
245 ------------------------
246 -- Test_One_Dimension --
247 ------------------------
249 function Test_One_Dimension (N : Int) return List_Id is
250 Index : Entity_Id;
252 begin
253 -- If all dimensions dealt with, we simply test the component
255 if N > Number_Dimensions (A_Type) then
256 return Test_Component;
258 -- Here we generate the required loop
260 else
261 Index :=
262 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
264 Append (New_Reference_To (Index, Loc), Index_List);
266 return New_List (
267 Make_Implicit_Loop_Statement (Nod,
268 Identifier => Empty,
269 Iteration_Scheme =>
270 Make_Iteration_Scheme (Loc,
271 Loop_Parameter_Specification =>
272 Make_Loop_Parameter_Specification (Loc,
273 Defining_Identifier => Index,
274 Discrete_Subtype_Definition =>
275 Make_Attribute_Reference (Loc,
276 Prefix => Make_Identifier (Loc, Name_uA),
277 Attribute_Name => Name_Range,
278 Expressions => New_List (
279 Make_Integer_Literal (Loc, N))))),
280 Statements => Test_One_Dimension (N + 1)),
281 Make_Simple_Return_Statement (Loc,
282 Expression => New_Occurrence_Of (Standard_True, Loc)));
283 end if;
284 end Test_One_Dimension;
286 -- Start of processing for Build_Array_VS_Func
288 begin
289 Index_List := New_List;
290 Func_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
292 Body_Stmts := Test_One_Dimension (1);
294 -- Parameter is always (A : A_Typ)
296 Formals := New_List (
297 Make_Parameter_Specification (Loc,
298 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uA),
299 In_Present => True,
300 Out_Present => False,
301 Parameter_Type => New_Reference_To (A_Type, Loc)));
303 -- Build body
305 Set_Ekind (Func_Id, E_Function);
306 Set_Is_Internal (Func_Id);
308 Insert_Action (Nod,
309 Make_Subprogram_Body (Loc,
310 Specification =>
311 Make_Function_Specification (Loc,
312 Defining_Unit_Name => Func_Id,
313 Parameter_Specifications => Formals,
314 Result_Definition =>
315 New_Occurrence_Of (Standard_Boolean, Loc)),
316 Declarations => New_List,
317 Handled_Statement_Sequence =>
318 Make_Handled_Sequence_Of_Statements (Loc,
319 Statements => Body_Stmts)));
321 if not Debug_Generated_Code then
322 Set_Debug_Info_Off (Func_Id);
323 end if;
325 return Func_Id;
326 end Build_Array_VS_Func;
328 ----------------------------------
329 -- Compile_Stream_Body_In_Scope --
330 ----------------------------------
332 procedure Compile_Stream_Body_In_Scope
333 (N : Node_Id;
334 Decl : Node_Id;
335 Arr : Entity_Id;
336 Check : Boolean)
338 Installed : Boolean := False;
339 Scop : constant Entity_Id := Scope (Arr);
340 Curr : constant Entity_Id := Current_Scope;
342 begin
343 if Is_Hidden (Arr)
344 and then not In_Open_Scopes (Scop)
345 and then Ekind (Scop) = E_Package
346 then
347 Push_Scope (Scop);
348 Install_Visible_Declarations (Scop);
349 Install_Private_Declarations (Scop);
350 Installed := True;
352 -- The entities in the package are now visible, but the generated
353 -- stream entity must appear in the current scope (usually an
354 -- enclosing stream function) so that itypes all have their proper
355 -- scopes.
357 Push_Scope (Curr);
358 end if;
360 if Check then
361 Insert_Action (N, Decl);
362 else
363 Insert_Action (N, Decl, Suppress => All_Checks);
364 end if;
366 if Installed then
368 -- Remove extra copy of current scope, and package itself
370 Pop_Scope;
371 End_Package_Scope (Scop);
372 end if;
373 end Compile_Stream_Body_In_Scope;
375 -----------------------------------
376 -- Expand_Access_To_Protected_Op --
377 -----------------------------------
379 procedure Expand_Access_To_Protected_Op
380 (N : Node_Id;
381 Pref : Node_Id;
382 Typ : Entity_Id)
384 -- The value of the attribute_reference is a record containing two
385 -- fields: an access to the protected object, and an access to the
386 -- subprogram itself. The prefix is a selected component.
388 Loc : constant Source_Ptr := Sloc (N);
389 Agg : Node_Id;
390 Btyp : constant Entity_Id := Base_Type (Typ);
391 Sub : Entity_Id;
392 Sub_Ref : Node_Id;
393 E_T : constant Entity_Id := Equivalent_Type (Btyp);
394 Acc : constant Entity_Id :=
395 Etype (Next_Component (First_Component (E_T)));
396 Obj_Ref : Node_Id;
397 Curr : Entity_Id;
399 function May_Be_External_Call return Boolean;
400 -- If the 'Access is to a local operation, but appears in a context
401 -- where it may lead to a call from outside the object, we must treat
402 -- this as an external call. Clearly we cannot tell without full
403 -- flow analysis, and a subsequent call that uses this 'Access may
404 -- lead to a bounded error (trying to seize locks twice, e.g.). For
405 -- now we treat 'Access as a potential external call if it is an actual
406 -- in a call to an outside subprogram.
408 --------------------------
409 -- May_Be_External_Call --
410 --------------------------
412 function May_Be_External_Call return Boolean is
413 Subp : Entity_Id;
414 Par : Node_Id := Parent (N);
416 begin
417 -- Account for the case where the Access attribute is part of a
418 -- named parameter association.
420 if Nkind (Par) = N_Parameter_Association then
421 Par := Parent (Par);
422 end if;
424 if Nkind (Par) in N_Subprogram_Call
425 and then Is_Entity_Name (Name (Par))
426 then
427 Subp := Entity (Name (Par));
428 return not In_Open_Scopes (Scope (Subp));
429 else
430 return False;
431 end if;
432 end May_Be_External_Call;
434 -- Start of processing for Expand_Access_To_Protected_Op
436 begin
437 -- Within the body of the protected type, the prefix designates a local
438 -- operation, and the object is the first parameter of the corresponding
439 -- protected body of the current enclosing operation.
441 if Is_Entity_Name (Pref) then
442 if May_Be_External_Call then
443 Sub :=
444 New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc);
445 else
446 Sub :=
447 New_Occurrence_Of
448 (Protected_Body_Subprogram (Entity (Pref)), Loc);
449 end if;
451 -- Don't traverse the scopes when the attribute occurs within an init
452 -- proc, because we directly use the _init formal of the init proc in
453 -- that case.
455 Curr := Current_Scope;
456 if not Is_Init_Proc (Curr) then
457 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
459 while Scope (Curr) /= Scope (Entity (Pref)) loop
460 Curr := Scope (Curr);
461 end loop;
462 end if;
464 -- In case of protected entries the first formal of its Protected_
465 -- Body_Subprogram is the address of the object.
467 if Ekind (Curr) = E_Entry then
468 Obj_Ref :=
469 New_Occurrence_Of
470 (First_Formal
471 (Protected_Body_Subprogram (Curr)), Loc);
473 -- If the current scope is an init proc, then use the address of the
474 -- _init formal as the object reference.
476 elsif Is_Init_Proc (Curr) then
477 Obj_Ref :=
478 Make_Attribute_Reference (Loc,
479 Prefix => New_Occurrence_Of (First_Formal (Curr), Loc),
480 Attribute_Name => Name_Address);
482 -- In case of protected subprograms the first formal of its
483 -- Protected_Body_Subprogram is the object and we get its address.
485 else
486 Obj_Ref :=
487 Make_Attribute_Reference (Loc,
488 Prefix =>
489 New_Occurrence_Of
490 (First_Formal
491 (Protected_Body_Subprogram (Curr)), Loc),
492 Attribute_Name => Name_Address);
493 end if;
495 -- Case where the prefix is not an entity name. Find the
496 -- version of the protected operation to be called from
497 -- outside the protected object.
499 else
500 Sub :=
501 New_Occurrence_Of
502 (External_Subprogram
503 (Entity (Selector_Name (Pref))), Loc);
505 Obj_Ref :=
506 Make_Attribute_Reference (Loc,
507 Prefix => Relocate_Node (Prefix (Pref)),
508 Attribute_Name => Name_Address);
509 end if;
511 Sub_Ref :=
512 Make_Attribute_Reference (Loc,
513 Prefix => Sub,
514 Attribute_Name => Name_Access);
516 -- We set the type of the access reference to the already generated
517 -- access_to_subprogram type, and declare the reference analyzed, to
518 -- prevent further expansion when the enclosing aggregate is analyzed.
520 Set_Etype (Sub_Ref, Acc);
521 Set_Analyzed (Sub_Ref);
523 Agg :=
524 Make_Aggregate (Loc,
525 Expressions => New_List (Obj_Ref, Sub_Ref));
527 -- Sub_Ref has been marked as analyzed, but we still need to make sure
528 -- Sub is correctly frozen.
530 Freeze_Before (N, Entity (Sub));
532 Rewrite (N, Agg);
533 Analyze_And_Resolve (N, E_T);
535 -- For subsequent analysis, the node must retain its type. The backend
536 -- will replace it with the equivalent type where needed.
538 Set_Etype (N, Typ);
539 end Expand_Access_To_Protected_Op;
541 --------------------------
542 -- Expand_Fpt_Attribute --
543 --------------------------
545 procedure Expand_Fpt_Attribute
546 (N : Node_Id;
547 Pkg : RE_Id;
548 Nam : Name_Id;
549 Args : List_Id)
551 Loc : constant Source_Ptr := Sloc (N);
552 Typ : constant Entity_Id := Etype (N);
553 Fnm : Node_Id;
555 begin
556 -- The function name is the selected component Attr_xxx.yyy where
557 -- Attr_xxx is the package name, and yyy is the argument Nam.
559 -- Note: it would be more usual to have separate RE entries for each
560 -- of the entities in the Fat packages, but first they have identical
561 -- names (so we would have to have lots of renaming declarations to
562 -- meet the normal RE rule of separate names for all runtime entities),
563 -- and second there would be an awful lot of them!
565 Fnm :=
566 Make_Selected_Component (Loc,
567 Prefix => New_Reference_To (RTE (Pkg), Loc),
568 Selector_Name => Make_Identifier (Loc, Nam));
570 -- The generated call is given the provided set of parameters, and then
571 -- wrapped in a conversion which converts the result to the target type
572 -- We use the base type as the target because a range check may be
573 -- required.
575 Rewrite (N,
576 Unchecked_Convert_To (Base_Type (Etype (N)),
577 Make_Function_Call (Loc,
578 Name => Fnm,
579 Parameter_Associations => Args)));
581 Analyze_And_Resolve (N, Typ);
582 end Expand_Fpt_Attribute;
584 ----------------------------
585 -- Expand_Fpt_Attribute_R --
586 ----------------------------
588 -- The single argument is converted to its root type to call the
589 -- appropriate runtime function, with the actual call being built
590 -- by Expand_Fpt_Attribute
592 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
593 E1 : constant Node_Id := First (Expressions (N));
594 Ftp : Entity_Id;
595 Pkg : RE_Id;
596 begin
597 Find_Fat_Info (Etype (E1), Ftp, Pkg);
598 Expand_Fpt_Attribute
599 (N, Pkg, Attribute_Name (N),
600 New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1))));
601 end Expand_Fpt_Attribute_R;
603 -----------------------------
604 -- Expand_Fpt_Attribute_RI --
605 -----------------------------
607 -- The first argument is converted to its root type and the second
608 -- argument is converted to standard long long integer to call the
609 -- appropriate runtime function, with the actual call being built
610 -- by Expand_Fpt_Attribute
612 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
613 E1 : constant Node_Id := First (Expressions (N));
614 Ftp : Entity_Id;
615 Pkg : RE_Id;
616 E2 : constant Node_Id := Next (E1);
617 begin
618 Find_Fat_Info (Etype (E1), Ftp, Pkg);
619 Expand_Fpt_Attribute
620 (N, Pkg, Attribute_Name (N),
621 New_List (
622 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
623 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
624 end Expand_Fpt_Attribute_RI;
626 -----------------------------
627 -- Expand_Fpt_Attribute_RR --
628 -----------------------------
630 -- The two arguments are converted to their root types to call the
631 -- appropriate runtime function, with the actual call being built
632 -- by Expand_Fpt_Attribute
634 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
635 E1 : constant Node_Id := First (Expressions (N));
636 Ftp : Entity_Id;
637 Pkg : RE_Id;
638 E2 : constant Node_Id := Next (E1);
639 begin
640 Find_Fat_Info (Etype (E1), Ftp, Pkg);
641 Expand_Fpt_Attribute
642 (N, Pkg, Attribute_Name (N),
643 New_List (
644 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
645 Unchecked_Convert_To (Ftp, Relocate_Node (E2))));
646 end Expand_Fpt_Attribute_RR;
648 ----------------------------------
649 -- Expand_N_Attribute_Reference --
650 ----------------------------------
652 procedure Expand_N_Attribute_Reference (N : Node_Id) is
653 Loc : constant Source_Ptr := Sloc (N);
654 Typ : constant Entity_Id := Etype (N);
655 Btyp : constant Entity_Id := Base_Type (Typ);
656 Pref : constant Node_Id := Prefix (N);
657 Ptyp : constant Entity_Id := Etype (Pref);
658 Exprs : constant List_Id := Expressions (N);
659 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
661 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
662 -- Rewrites a stream attribute for Read, Write or Output with the
663 -- procedure call. Pname is the entity for the procedure to call.
665 ------------------------------
666 -- Rewrite_Stream_Proc_Call --
667 ------------------------------
669 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
670 Item : constant Node_Id := Next (First (Exprs));
671 Formal : constant Entity_Id := Next_Formal (First_Formal (Pname));
672 Formal_Typ : constant Entity_Id := Etype (Formal);
673 Is_Written : constant Boolean := (Ekind (Formal) /= E_In_Parameter);
675 begin
676 -- The expansion depends on Item, the second actual, which is
677 -- the object being streamed in or out.
679 -- If the item is a component of a packed array type, and
680 -- a conversion is needed on exit, we introduce a temporary to
681 -- hold the value, because otherwise the packed reference will
682 -- not be properly expanded.
684 if Nkind (Item) = N_Indexed_Component
685 and then Is_Packed (Base_Type (Etype (Prefix (Item))))
686 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
687 and then Is_Written
688 then
689 declare
690 Temp : constant Entity_Id := Make_Temporary (Loc, 'V');
691 Decl : Node_Id;
692 Assn : Node_Id;
694 begin
695 Decl :=
696 Make_Object_Declaration (Loc,
697 Defining_Identifier => Temp,
698 Object_Definition =>
699 New_Occurrence_Of (Formal_Typ, Loc));
700 Set_Etype (Temp, Formal_Typ);
702 Assn :=
703 Make_Assignment_Statement (Loc,
704 Name => New_Copy_Tree (Item),
705 Expression =>
706 Unchecked_Convert_To
707 (Etype (Item), New_Occurrence_Of (Temp, Loc)));
709 Rewrite (Item, New_Occurrence_Of (Temp, Loc));
710 Insert_Actions (N,
711 New_List (
712 Decl,
713 Make_Procedure_Call_Statement (Loc,
714 Name => New_Occurrence_Of (Pname, Loc),
715 Parameter_Associations => Exprs),
716 Assn));
718 Rewrite (N, Make_Null_Statement (Loc));
719 return;
720 end;
721 end if;
723 -- For the class-wide dispatching cases, and for cases in which
724 -- the base type of the second argument matches the base type of
725 -- the corresponding formal parameter (that is to say the stream
726 -- operation is not inherited), we are all set, and can use the
727 -- argument unchanged.
729 -- For all other cases we do an unchecked conversion of the second
730 -- parameter to the type of the formal of the procedure we are
731 -- calling. This deals with the private type cases, and with going
732 -- to the root type as required in elementary type case.
734 if not Is_Class_Wide_Type (Entity (Pref))
735 and then not Is_Class_Wide_Type (Etype (Item))
736 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
737 then
738 Rewrite (Item,
739 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
741 -- For untagged derived types set Assignment_OK, to prevent
742 -- copies from being created when the unchecked conversion
743 -- is expanded (which would happen in Remove_Side_Effects
744 -- if Expand_N_Unchecked_Conversion were allowed to call
745 -- Force_Evaluation). The copy could violate Ada semantics
746 -- in cases such as an actual that is an out parameter.
747 -- Note that this approach is also used in exp_ch7 for calls
748 -- to controlled type operations to prevent problems with
749 -- actuals wrapped in unchecked conversions.
751 if Is_Untagged_Derivation (Etype (Expression (Item))) then
752 Set_Assignment_OK (Item);
753 end if;
754 end if;
756 -- The stream operation to call maybe a renaming created by
757 -- an attribute definition clause, and may not be frozen yet.
758 -- Ensure that it has the necessary extra formals.
760 if not Is_Frozen (Pname) then
761 Create_Extra_Formals (Pname);
762 end if;
764 -- And now rewrite the call
766 Rewrite (N,
767 Make_Procedure_Call_Statement (Loc,
768 Name => New_Occurrence_Of (Pname, Loc),
769 Parameter_Associations => Exprs));
771 Analyze (N);
772 end Rewrite_Stream_Proc_Call;
774 -- Start of processing for Expand_N_Attribute_Reference
776 begin
777 -- Do required validity checking, if enabled. Do not apply check to
778 -- output parameters of an Asm instruction, since the value of this
779 -- is not set till after the attribute has been elaborated, and do
780 -- not apply the check to the arguments of a 'Read or 'Input attribute
781 -- reference since the scalar argument is an OUT scalar.
783 if Validity_Checks_On and then Validity_Check_Operands
784 and then Id /= Attribute_Asm_Output
785 and then Id /= Attribute_Read
786 and then Id /= Attribute_Input
787 then
788 declare
789 Expr : Node_Id;
790 begin
791 Expr := First (Expressions (N));
792 while Present (Expr) loop
793 Ensure_Valid (Expr);
794 Next (Expr);
795 end loop;
796 end;
797 end if;
799 -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in-
800 -- place function, then a temporary return object needs to be created
801 -- and access to it must be passed to the function. Currently we limit
802 -- such functions to those with inherently limited result subtypes, but
803 -- eventually we plan to expand the functions that are treated as
804 -- build-in-place to include other composite result types.
806 if Ada_Version >= Ada_2005
807 and then Is_Build_In_Place_Function_Call (Pref)
808 then
809 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
810 end if;
812 -- If prefix is a protected type name, this is a reference to the
813 -- current instance of the type. For a component definition, nothing
814 -- to do (expansion will occur in the init proc). In other contexts,
815 -- rewrite into reference to current instance.
817 if Is_Protected_Self_Reference (Pref)
818 and then not
819 (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint,
820 N_Discriminant_Association)
821 and then Nkind (Parent (Parent (Parent (Parent (N))))) =
822 N_Component_Definition)
824 -- No action needed for these attributes since the current instance
825 -- will be rewritten to be the name of the _object parameter
826 -- associated with the enclosing protected subprogram (see below).
828 and then Id /= Attribute_Access
829 and then Id /= Attribute_Unchecked_Access
830 and then Id /= Attribute_Unrestricted_Access
831 then
832 Rewrite (Pref, Concurrent_Ref (Pref));
833 Analyze (Pref);
834 end if;
836 -- Remaining processing depends on specific attribute
838 -- Note: individual sections of the following case statement are
839 -- allowed to assume there is no code after the case statement, and
840 -- are legitimately allowed to execute return statements if they have
841 -- nothing more to do.
843 case Id is
845 -- Attributes related to Ada 2012 iterators (placeholder ???)
847 when Attribute_Constant_Indexing |
848 Attribute_Default_Iterator |
849 Attribute_Implicit_Dereference |
850 Attribute_Iterator_Element |
851 Attribute_Variable_Indexing =>
852 null;
854 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
855 -- were already rejected by the parser. Thus they shouldn't appear here.
857 when Internal_Attribute_Id =>
858 raise Program_Error;
860 ------------
861 -- Access --
862 ------------
864 when Attribute_Access |
865 Attribute_Unchecked_Access |
866 Attribute_Unrestricted_Access =>
868 Access_Cases : declare
869 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
870 Btyp_DDT : Entity_Id;
872 function Enclosing_Object (N : Node_Id) return Node_Id;
873 -- If N denotes a compound name (selected component, indexed
874 -- component, or slice), returns the name of the outermost such
875 -- enclosing object. Otherwise returns N. If the object is a
876 -- renaming, then the renamed object is returned.
878 ----------------------
879 -- Enclosing_Object --
880 ----------------------
882 function Enclosing_Object (N : Node_Id) return Node_Id is
883 Obj_Name : Node_Id;
885 begin
886 Obj_Name := N;
887 while Nkind_In (Obj_Name, N_Selected_Component,
888 N_Indexed_Component,
889 N_Slice)
890 loop
891 Obj_Name := Prefix (Obj_Name);
892 end loop;
894 return Get_Referenced_Object (Obj_Name);
895 end Enclosing_Object;
897 -- Local declarations
899 Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object);
901 -- Start of processing for Access_Cases
903 begin
904 Btyp_DDT := Designated_Type (Btyp);
906 -- Handle designated types that come from the limited view
908 if Ekind (Btyp_DDT) = E_Incomplete_Type
909 and then From_With_Type (Btyp_DDT)
910 and then Present (Non_Limited_View (Btyp_DDT))
911 then
912 Btyp_DDT := Non_Limited_View (Btyp_DDT);
914 elsif Is_Class_Wide_Type (Btyp_DDT)
915 and then Ekind (Etype (Btyp_DDT)) = E_Incomplete_Type
916 and then From_With_Type (Etype (Btyp_DDT))
917 and then Present (Non_Limited_View (Etype (Btyp_DDT)))
918 and then Present (Class_Wide_Type
919 (Non_Limited_View (Etype (Btyp_DDT))))
920 then
921 Btyp_DDT :=
922 Class_Wide_Type (Non_Limited_View (Etype (Btyp_DDT)));
923 end if;
925 -- In order to improve the text of error messages, the designated
926 -- type of access-to-subprogram itypes is set by the semantics as
927 -- the associated subprogram entity (see sem_attr). Now we replace
928 -- such node with the proper E_Subprogram_Type itype.
930 if Id = Attribute_Unrestricted_Access
931 and then Is_Subprogram (Directly_Designated_Type (Typ))
932 then
933 -- The following conditions ensure that this special management
934 -- is done only for "Address!(Prim'Unrestricted_Access)" nodes.
935 -- At this stage other cases in which the designated type is
936 -- still a subprogram (instead of an E_Subprogram_Type) are
937 -- wrong because the semantics must have overridden the type of
938 -- the node with the type imposed by the context.
940 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
941 and then Etype (Parent (N)) = RTE (RE_Prim_Ptr)
942 then
943 Set_Etype (N, RTE (RE_Prim_Ptr));
945 else
946 declare
947 Subp : constant Entity_Id :=
948 Directly_Designated_Type (Typ);
949 Etyp : Entity_Id;
950 Extra : Entity_Id := Empty;
951 New_Formal : Entity_Id;
952 Old_Formal : Entity_Id := First_Formal (Subp);
953 Subp_Typ : Entity_Id;
955 begin
956 Subp_Typ := Create_Itype (E_Subprogram_Type, N);
957 Set_Etype (Subp_Typ, Etype (Subp));
958 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
960 if Present (Old_Formal) then
961 New_Formal := New_Copy (Old_Formal);
962 Set_First_Entity (Subp_Typ, New_Formal);
964 loop
965 Set_Scope (New_Formal, Subp_Typ);
966 Etyp := Etype (New_Formal);
968 -- Handle itypes. There is no need to duplicate
969 -- here the itypes associated with record types
970 -- (i.e the implicit full view of private types).
972 if Is_Itype (Etyp)
973 and then Ekind (Base_Type (Etyp)) /= E_Record_Type
974 then
975 Extra := New_Copy (Etyp);
976 Set_Parent (Extra, New_Formal);
977 Set_Etype (New_Formal, Extra);
978 Set_Scope (Extra, Subp_Typ);
979 end if;
981 Extra := New_Formal;
982 Next_Formal (Old_Formal);
983 exit when No (Old_Formal);
985 Set_Next_Entity (New_Formal,
986 New_Copy (Old_Formal));
987 Next_Entity (New_Formal);
988 end loop;
990 Set_Next_Entity (New_Formal, Empty);
991 Set_Last_Entity (Subp_Typ, Extra);
992 end if;
994 -- Now that the explicit formals have been duplicated,
995 -- any extra formals needed by the subprogram must be
996 -- created.
998 if Present (Extra) then
999 Set_Extra_Formal (Extra, Empty);
1000 end if;
1002 Create_Extra_Formals (Subp_Typ);
1003 Set_Directly_Designated_Type (Typ, Subp_Typ);
1004 end;
1005 end if;
1006 end if;
1008 if Is_Access_Protected_Subprogram_Type (Btyp) then
1009 Expand_Access_To_Protected_Op (N, Pref, Typ);
1011 -- If prefix is a type name, this is a reference to the current
1012 -- instance of the type, within its initialization procedure.
1014 elsif Is_Entity_Name (Pref)
1015 and then Is_Type (Entity (Pref))
1016 then
1017 declare
1018 Par : Node_Id;
1019 Formal : Entity_Id;
1021 begin
1022 -- If the current instance name denotes a task type, then
1023 -- the access attribute is rewritten to be the name of the
1024 -- "_task" parameter associated with the task type's task
1025 -- procedure. An unchecked conversion is applied to ensure
1026 -- a type match in cases of expander-generated calls (e.g.
1027 -- init procs).
1029 if Is_Task_Type (Entity (Pref)) then
1030 Formal :=
1031 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
1032 while Present (Formal) loop
1033 exit when Chars (Formal) = Name_uTask;
1034 Next_Entity (Formal);
1035 end loop;
1037 pragma Assert (Present (Formal));
1039 Rewrite (N,
1040 Unchecked_Convert_To (Typ,
1041 New_Occurrence_Of (Formal, Loc)));
1042 Set_Etype (N, Typ);
1044 elsif Is_Protected_Type (Entity (Pref)) then
1046 -- No action needed for current instance located in a
1047 -- component definition (expansion will occur in the
1048 -- init proc)
1050 if Is_Protected_Type (Current_Scope) then
1051 null;
1053 -- If the current instance reference is located in a
1054 -- protected subprogram or entry then rewrite the access
1055 -- attribute to be the name of the "_object" parameter.
1056 -- An unchecked conversion is applied to ensure a type
1057 -- match in cases of expander-generated calls (e.g. init
1058 -- procs).
1060 else
1061 Formal :=
1062 First_Entity
1063 (Protected_Body_Subprogram (Current_Scope));
1064 Rewrite (N,
1065 Unchecked_Convert_To (Typ,
1066 New_Occurrence_Of (Formal, Loc)));
1067 Set_Etype (N, Typ);
1068 end if;
1070 -- The expression must appear in a default expression,
1071 -- (which in the initialization procedure is the right-hand
1072 -- side of an assignment), and not in a discriminant
1073 -- constraint.
1075 else
1076 Par := Parent (N);
1077 while Present (Par) loop
1078 exit when Nkind (Par) = N_Assignment_Statement;
1080 if Nkind (Par) = N_Component_Declaration then
1081 return;
1082 end if;
1084 Par := Parent (Par);
1085 end loop;
1087 if Present (Par) then
1088 Rewrite (N,
1089 Make_Attribute_Reference (Loc,
1090 Prefix => Make_Identifier (Loc, Name_uInit),
1091 Attribute_Name => Attribute_Name (N)));
1093 Analyze_And_Resolve (N, Typ);
1094 end if;
1095 end if;
1096 end;
1098 -- If the prefix of an Access attribute is a dereference of an
1099 -- access parameter (or a renaming of such a dereference, or a
1100 -- subcomponent of such a dereference) and the context is a
1101 -- general access type (including the type of an object or
1102 -- component with an access_definition, but not the anonymous
1103 -- type of an access parameter or access discriminant), then
1104 -- apply an accessibility check to the access parameter. We used
1105 -- to rewrite the access parameter as a type conversion, but that
1106 -- could only be done if the immediate prefix of the Access
1107 -- attribute was the dereference, and didn't handle cases where
1108 -- the attribute is applied to a subcomponent of the dereference,
1109 -- since there's generally no available, appropriate access type
1110 -- to convert to in that case. The attribute is passed as the
1111 -- point to insert the check, because the access parameter may
1112 -- come from a renaming, possibly in a different scope, and the
1113 -- check must be associated with the attribute itself.
1115 elsif Id = Attribute_Access
1116 and then Nkind (Enc_Object) = N_Explicit_Dereference
1117 and then Is_Entity_Name (Prefix (Enc_Object))
1118 and then (Ekind (Btyp) = E_General_Access_Type
1119 or else Is_Local_Anonymous_Access (Btyp))
1120 and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind
1121 and then Ekind (Etype (Entity (Prefix (Enc_Object))))
1122 = E_Anonymous_Access_Type
1123 and then Present (Extra_Accessibility
1124 (Entity (Prefix (Enc_Object))))
1125 then
1126 Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N);
1128 -- Ada 2005 (AI-251): If the designated type is an interface we
1129 -- add an implicit conversion to force the displacement of the
1130 -- pointer to reference the secondary dispatch table.
1132 elsif Is_Interface (Btyp_DDT)
1133 and then (Comes_From_Source (N)
1134 or else Comes_From_Source (Ref_Object)
1135 or else (Nkind (Ref_Object) in N_Has_Chars
1136 and then Chars (Ref_Object) = Name_uInit))
1137 then
1138 if Nkind (Ref_Object) /= N_Explicit_Dereference then
1140 -- No implicit conversion required if types match, or if
1141 -- the prefix is the class_wide_type of the interface. In
1142 -- either case passing an object of the interface type has
1143 -- already set the pointer correctly.
1145 if Btyp_DDT = Etype (Ref_Object)
1146 or else (Is_Class_Wide_Type (Etype (Ref_Object))
1147 and then
1148 Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object))
1149 then
1150 null;
1152 else
1153 Rewrite (Prefix (N),
1154 Convert_To (Btyp_DDT,
1155 New_Copy_Tree (Prefix (N))));
1157 Analyze_And_Resolve (Prefix (N), Btyp_DDT);
1158 end if;
1160 -- When the object is an explicit dereference, convert the
1161 -- dereference's prefix.
1163 else
1164 declare
1165 Obj_DDT : constant Entity_Id :=
1166 Base_Type
1167 (Directly_Designated_Type
1168 (Etype (Prefix (Ref_Object))));
1169 begin
1170 -- No implicit conversion required if designated types
1171 -- match, or if we have an unrestricted access.
1173 if Obj_DDT /= Btyp_DDT
1174 and then Id /= Attribute_Unrestricted_Access
1175 and then not (Is_Class_Wide_Type (Obj_DDT)
1176 and then Etype (Obj_DDT) = Btyp_DDT)
1177 then
1178 Rewrite (N,
1179 Convert_To (Typ,
1180 New_Copy_Tree (Prefix (Ref_Object))));
1181 Analyze_And_Resolve (N, Typ);
1182 end if;
1183 end;
1184 end if;
1185 end if;
1186 end Access_Cases;
1188 --------------
1189 -- Adjacent --
1190 --------------
1192 -- Transforms 'Adjacent into a call to the floating-point attribute
1193 -- function Adjacent in Fat_xxx (where xxx is the root type)
1195 when Attribute_Adjacent =>
1196 Expand_Fpt_Attribute_RR (N);
1198 -------------
1199 -- Address --
1200 -------------
1202 when Attribute_Address => Address : declare
1203 Task_Proc : Entity_Id;
1205 begin
1206 -- If the prefix is a task or a task type, the useful address is that
1207 -- of the procedure for the task body, i.e. the actual program unit.
1208 -- We replace the original entity with that of the procedure.
1210 if Is_Entity_Name (Pref)
1211 and then Is_Task_Type (Entity (Pref))
1212 then
1213 Task_Proc := Next_Entity (Root_Type (Ptyp));
1215 while Present (Task_Proc) loop
1216 exit when Ekind (Task_Proc) = E_Procedure
1217 and then Etype (First_Formal (Task_Proc)) =
1218 Corresponding_Record_Type (Ptyp);
1219 Next_Entity (Task_Proc);
1220 end loop;
1222 if Present (Task_Proc) then
1223 Set_Entity (Pref, Task_Proc);
1224 Set_Etype (Pref, Etype (Task_Proc));
1225 end if;
1227 -- Similarly, the address of a protected operation is the address
1228 -- of the corresponding protected body, regardless of the protected
1229 -- object from which it is selected.
1231 elsif Nkind (Pref) = N_Selected_Component
1232 and then Is_Subprogram (Entity (Selector_Name (Pref)))
1233 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
1234 then
1235 Rewrite (Pref,
1236 New_Occurrence_Of (
1237 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
1239 elsif Nkind (Pref) = N_Explicit_Dereference
1240 and then Ekind (Ptyp) = E_Subprogram_Type
1241 and then Convention (Ptyp) = Convention_Protected
1242 then
1243 -- The prefix is be a dereference of an access_to_protected_
1244 -- subprogram. The desired address is the second component of
1245 -- the record that represents the access.
1247 declare
1248 Addr : constant Entity_Id := Etype (N);
1249 Ptr : constant Node_Id := Prefix (Pref);
1250 T : constant Entity_Id :=
1251 Equivalent_Type (Base_Type (Etype (Ptr)));
1253 begin
1254 Rewrite (N,
1255 Unchecked_Convert_To (Addr,
1256 Make_Selected_Component (Loc,
1257 Prefix => Unchecked_Convert_To (T, Ptr),
1258 Selector_Name => New_Occurrence_Of (
1259 Next_Entity (First_Entity (T)), Loc))));
1261 Analyze_And_Resolve (N, Addr);
1262 end;
1264 -- Ada 2005 (AI-251): Class-wide interface objects are always
1265 -- "displaced" to reference the tag associated with the interface
1266 -- type. In order to obtain the real address of such objects we
1267 -- generate a call to a run-time subprogram that returns the base
1268 -- address of the object.
1270 -- This processing is not needed in the VM case, where dispatching
1271 -- issues are taken care of by the virtual machine.
1273 elsif Is_Class_Wide_Type (Ptyp)
1274 and then Is_Interface (Ptyp)
1275 and then Tagged_Type_Expansion
1276 and then not (Nkind (Pref) in N_Has_Entity
1277 and then Is_Subprogram (Entity (Pref)))
1278 then
1279 Rewrite (N,
1280 Make_Function_Call (Loc,
1281 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
1282 Parameter_Associations => New_List (
1283 Relocate_Node (N))));
1284 Analyze (N);
1285 return;
1286 end if;
1288 -- Deal with packed array reference, other cases are handled by
1289 -- the back end.
1291 if Involves_Packed_Array_Reference (Pref) then
1292 Expand_Packed_Address_Reference (N);
1293 end if;
1294 end Address;
1296 ---------------
1297 -- Alignment --
1298 ---------------
1300 when Attribute_Alignment => Alignment : declare
1301 New_Node : Node_Id;
1303 begin
1304 -- For class-wide types, X'Class'Alignment is transformed into a
1305 -- direct reference to the Alignment of the class type, so that the
1306 -- back end does not have to deal with the X'Class'Alignment
1307 -- reference.
1309 if Is_Entity_Name (Pref)
1310 and then Is_Class_Wide_Type (Entity (Pref))
1311 then
1312 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
1313 return;
1315 -- For x'Alignment applied to an object of a class wide type,
1316 -- transform X'Alignment into a call to the predefined primitive
1317 -- operation _Alignment applied to X.
1319 elsif Is_Class_Wide_Type (Ptyp) then
1320 New_Node :=
1321 Make_Attribute_Reference (Loc,
1322 Prefix => Pref,
1323 Attribute_Name => Name_Tag);
1325 if VM_Target = No_VM then
1326 New_Node := Build_Get_Alignment (Loc, New_Node);
1327 else
1328 New_Node :=
1329 Make_Function_Call (Loc,
1330 Name => New_Reference_To (RTE (RE_Get_Alignment), Loc),
1331 Parameter_Associations => New_List (New_Node));
1332 end if;
1334 -- Case where the context is a specific integer type with which
1335 -- the original attribute was compatible. The function has a
1336 -- specific type as well, so to preserve the compatibility we
1337 -- must convert explicitly.
1339 if Typ /= Standard_Integer then
1340 New_Node := Convert_To (Typ, New_Node);
1341 end if;
1343 Rewrite (N, New_Node);
1344 Analyze_And_Resolve (N, Typ);
1345 return;
1347 -- For all other cases, we just have to deal with the case of
1348 -- the fact that the result can be universal.
1350 else
1351 Apply_Universal_Integer_Attribute_Checks (N);
1352 end if;
1353 end Alignment;
1355 ---------------
1356 -- AST_Entry --
1357 ---------------
1359 when Attribute_AST_Entry => AST_Entry : declare
1360 Ttyp : Entity_Id;
1361 T_Id : Node_Id;
1362 Eent : Entity_Id;
1364 Entry_Ref : Node_Id;
1365 -- The reference to the entry or entry family
1367 Index : Node_Id;
1368 -- The index expression for an entry family reference, or
1369 -- the Empty if Entry_Ref references a simple entry.
1371 begin
1372 if Nkind (Pref) = N_Indexed_Component then
1373 Entry_Ref := Prefix (Pref);
1374 Index := First (Expressions (Pref));
1375 else
1376 Entry_Ref := Pref;
1377 Index := Empty;
1378 end if;
1380 -- Get expression for Task_Id and the entry entity
1382 if Nkind (Entry_Ref) = N_Selected_Component then
1383 T_Id :=
1384 Make_Attribute_Reference (Loc,
1385 Attribute_Name => Name_Identity,
1386 Prefix => Prefix (Entry_Ref));
1388 Ttyp := Etype (Prefix (Entry_Ref));
1389 Eent := Entity (Selector_Name (Entry_Ref));
1391 else
1392 T_Id :=
1393 Make_Function_Call (Loc,
1394 Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc));
1396 Eent := Entity (Entry_Ref);
1398 -- We have to find the enclosing task to get the task type
1399 -- There must be one, since we already validated this earlier
1401 Ttyp := Current_Scope;
1402 while not Is_Task_Type (Ttyp) loop
1403 Ttyp := Scope (Ttyp);
1404 end loop;
1405 end if;
1407 -- Now rewrite the attribute with a call to Create_AST_Handler
1409 Rewrite (N,
1410 Make_Function_Call (Loc,
1411 Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc),
1412 Parameter_Associations => New_List (
1413 T_Id,
1414 Entry_Index_Expression (Loc, Eent, Index, Ttyp))));
1416 Analyze_And_Resolve (N, RTE (RE_AST_Handler));
1417 end AST_Entry;
1419 ---------
1420 -- Bit --
1421 ---------
1423 -- We compute this if a packed array reference was present, otherwise we
1424 -- leave the computation up to the back end.
1426 when Attribute_Bit =>
1427 if Involves_Packed_Array_Reference (Pref) then
1428 Expand_Packed_Bit_Reference (N);
1429 else
1430 Apply_Universal_Integer_Attribute_Checks (N);
1431 end if;
1433 ------------------
1434 -- Bit_Position --
1435 ------------------
1437 -- We compute this if a component clause was present, otherwise we leave
1438 -- the computation up to the back end, since we don't know what layout
1439 -- will be chosen.
1441 -- Note that the attribute can apply to a naked record component
1442 -- in generated code (i.e. the prefix is an identifier that
1443 -- references the component or discriminant entity).
1445 when Attribute_Bit_Position => Bit_Position : declare
1446 CE : Entity_Id;
1448 begin
1449 if Nkind (Pref) = N_Identifier then
1450 CE := Entity (Pref);
1451 else
1452 CE := Entity (Selector_Name (Pref));
1453 end if;
1455 if Known_Static_Component_Bit_Offset (CE) then
1456 Rewrite (N,
1457 Make_Integer_Literal (Loc,
1458 Intval => Component_Bit_Offset (CE)));
1459 Analyze_And_Resolve (N, Typ);
1461 else
1462 Apply_Universal_Integer_Attribute_Checks (N);
1463 end if;
1464 end Bit_Position;
1466 ------------------
1467 -- Body_Version --
1468 ------------------
1470 -- A reference to P'Body_Version or P'Version is expanded to
1472 -- Vnn : Unsigned;
1473 -- pragma Import (C, Vnn, "uuuuT");
1474 -- ...
1475 -- Get_Version_String (Vnn)
1477 -- where uuuu is the unit name (dots replaced by double underscore)
1478 -- and T is B for the cases of Body_Version, or Version applied to a
1479 -- subprogram acting as its own spec, and S for Version applied to a
1480 -- subprogram spec or package. This sequence of code references the
1481 -- unsigned constant created in the main program by the binder.
1483 -- A special exception occurs for Standard, where the string returned
1484 -- is a copy of the library string in gnatvsn.ads.
1486 when Attribute_Body_Version | Attribute_Version => Version : declare
1487 E : constant Entity_Id := Make_Temporary (Loc, 'V');
1488 Pent : Entity_Id;
1489 S : String_Id;
1491 begin
1492 -- If not library unit, get to containing library unit
1494 Pent := Entity (Pref);
1495 while Pent /= Standard_Standard
1496 and then Scope (Pent) /= Standard_Standard
1497 and then not Is_Child_Unit (Pent)
1498 loop
1499 Pent := Scope (Pent);
1500 end loop;
1502 -- Special case Standard and Standard.ASCII
1504 if Pent = Standard_Standard or else Pent = Standard_ASCII then
1505 Rewrite (N,
1506 Make_String_Literal (Loc,
1507 Strval => Verbose_Library_Version));
1509 -- All other cases
1511 else
1512 -- Build required string constant
1514 Get_Name_String (Get_Unit_Name (Pent));
1516 Start_String;
1517 for J in 1 .. Name_Len - 2 loop
1518 if Name_Buffer (J) = '.' then
1519 Store_String_Chars ("__");
1520 else
1521 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
1522 end if;
1523 end loop;
1525 -- Case of subprogram acting as its own spec, always use body
1527 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
1528 and then Nkind (Parent (Declaration_Node (Pent))) =
1529 N_Subprogram_Body
1530 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
1531 then
1532 Store_String_Chars ("B");
1534 -- Case of no body present, always use spec
1536 elsif not Unit_Requires_Body (Pent) then
1537 Store_String_Chars ("S");
1539 -- Otherwise use B for Body_Version, S for spec
1541 elsif Id = Attribute_Body_Version then
1542 Store_String_Chars ("B");
1543 else
1544 Store_String_Chars ("S");
1545 end if;
1547 S := End_String;
1548 Lib.Version_Referenced (S);
1550 -- Insert the object declaration
1552 Insert_Actions (N, New_List (
1553 Make_Object_Declaration (Loc,
1554 Defining_Identifier => E,
1555 Object_Definition =>
1556 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
1558 -- Set entity as imported with correct external name
1560 Set_Is_Imported (E);
1561 Set_Interface_Name (E, Make_String_Literal (Loc, S));
1563 -- Set entity as internal to ensure proper Sprint output of its
1564 -- implicit importation.
1566 Set_Is_Internal (E);
1568 -- And now rewrite original reference
1570 Rewrite (N,
1571 Make_Function_Call (Loc,
1572 Name => New_Reference_To (RTE (RE_Get_Version_String), Loc),
1573 Parameter_Associations => New_List (
1574 New_Occurrence_Of (E, Loc))));
1575 end if;
1577 Analyze_And_Resolve (N, RTE (RE_Version_String));
1578 end Version;
1580 -------------
1581 -- Ceiling --
1582 -------------
1584 -- Transforms 'Ceiling into a call to the floating-point attribute
1585 -- function Ceiling in Fat_xxx (where xxx is the root type)
1587 when Attribute_Ceiling =>
1588 Expand_Fpt_Attribute_R (N);
1590 --------------
1591 -- Callable --
1592 --------------
1594 -- Transforms 'Callable attribute into a call to the Callable function
1596 when Attribute_Callable => Callable :
1597 begin
1598 -- We have an object of a task interface class-wide type as a prefix
1599 -- to Callable. Generate:
1600 -- callable (Task_Id (Pref._disp_get_task_id));
1602 if Ada_Version >= Ada_2005
1603 and then Ekind (Ptyp) = E_Class_Wide_Type
1604 and then Is_Interface (Ptyp)
1605 and then Is_Task_Interface (Ptyp)
1606 then
1607 Rewrite (N,
1608 Make_Function_Call (Loc,
1609 Name =>
1610 New_Reference_To (RTE (RE_Callable), Loc),
1611 Parameter_Associations => New_List (
1612 Make_Unchecked_Type_Conversion (Loc,
1613 Subtype_Mark =>
1614 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
1615 Expression =>
1616 Make_Selected_Component (Loc,
1617 Prefix =>
1618 New_Copy_Tree (Pref),
1619 Selector_Name =>
1620 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
1622 else
1623 Rewrite (N,
1624 Build_Call_With_Task (Pref, RTE (RE_Callable)));
1625 end if;
1627 Analyze_And_Resolve (N, Standard_Boolean);
1628 end Callable;
1630 ------------
1631 -- Caller --
1632 ------------
1634 -- Transforms 'Caller attribute into a call to either the
1635 -- Task_Entry_Caller or the Protected_Entry_Caller function.
1637 when Attribute_Caller => Caller : declare
1638 Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id);
1639 Ent : constant Entity_Id := Entity (Pref);
1640 Conctype : constant Entity_Id := Scope (Ent);
1641 Nest_Depth : Integer := 0;
1642 Name : Node_Id;
1643 S : Entity_Id;
1645 begin
1646 -- Protected case
1648 if Is_Protected_Type (Conctype) then
1649 case Corresponding_Runtime_Package (Conctype) is
1650 when System_Tasking_Protected_Objects_Entries =>
1651 Name :=
1652 New_Reference_To
1653 (RTE (RE_Protected_Entry_Caller), Loc);
1655 when System_Tasking_Protected_Objects_Single_Entry =>
1656 Name :=
1657 New_Reference_To
1658 (RTE (RE_Protected_Single_Entry_Caller), Loc);
1660 when others =>
1661 raise Program_Error;
1662 end case;
1664 Rewrite (N,
1665 Unchecked_Convert_To (Id_Kind,
1666 Make_Function_Call (Loc,
1667 Name => Name,
1668 Parameter_Associations => New_List (
1669 New_Reference_To
1670 (Find_Protection_Object (Current_Scope), Loc)))));
1672 -- Task case
1674 else
1675 -- Determine the nesting depth of the E'Caller attribute, that
1676 -- is, how many accept statements are nested within the accept
1677 -- statement for E at the point of E'Caller. The runtime uses
1678 -- this depth to find the specified entry call.
1680 for J in reverse 0 .. Scope_Stack.Last loop
1681 S := Scope_Stack.Table (J).Entity;
1683 -- We should not reach the scope of the entry, as it should
1684 -- already have been checked in Sem_Attr that this attribute
1685 -- reference is within a matching accept statement.
1687 pragma Assert (S /= Conctype);
1689 if S = Ent then
1690 exit;
1692 elsif Is_Entry (S) then
1693 Nest_Depth := Nest_Depth + 1;
1694 end if;
1695 end loop;
1697 Rewrite (N,
1698 Unchecked_Convert_To (Id_Kind,
1699 Make_Function_Call (Loc,
1700 Name =>
1701 New_Reference_To (RTE (RE_Task_Entry_Caller), Loc),
1702 Parameter_Associations => New_List (
1703 Make_Integer_Literal (Loc,
1704 Intval => Int (Nest_Depth))))));
1705 end if;
1707 Analyze_And_Resolve (N, Id_Kind);
1708 end Caller;
1710 -------------
1711 -- Compose --
1712 -------------
1714 -- Transforms 'Compose into a call to the floating-point attribute
1715 -- function Compose in Fat_xxx (where xxx is the root type)
1717 -- Note: we strictly should have special code here to deal with the
1718 -- case of absurdly negative arguments (less than Integer'First)
1719 -- which will return a (signed) zero value, but it hardly seems
1720 -- worth the effort. Absurdly large positive arguments will raise
1721 -- constraint error which is fine.
1723 when Attribute_Compose =>
1724 Expand_Fpt_Attribute_RI (N);
1726 -----------------
1727 -- Constrained --
1728 -----------------
1730 when Attribute_Constrained => Constrained : declare
1731 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
1733 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
1734 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
1735 -- view of an aliased object whose subtype is constrained.
1737 ---------------------------------
1738 -- Is_Constrained_Aliased_View --
1739 ---------------------------------
1741 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
1742 E : Entity_Id;
1744 begin
1745 if Is_Entity_Name (Obj) then
1746 E := Entity (Obj);
1748 if Present (Renamed_Object (E)) then
1749 return Is_Constrained_Aliased_View (Renamed_Object (E));
1750 else
1751 return Is_Aliased (E) and then Is_Constrained (Etype (E));
1752 end if;
1754 else
1755 return Is_Aliased_View (Obj)
1756 and then
1757 (Is_Constrained (Etype (Obj))
1758 or else
1759 (Nkind (Obj) = N_Explicit_Dereference
1760 and then
1761 not Effectively_Has_Constrained_Partial_View
1762 (Typ => Base_Type (Etype (Obj)),
1763 Scop => Current_Scope)));
1764 end if;
1765 end Is_Constrained_Aliased_View;
1767 -- Start of processing for Constrained
1769 begin
1770 -- Reference to a parameter where the value is passed as an extra
1771 -- actual, corresponding to the extra formal referenced by the
1772 -- Extra_Constrained field of the corresponding formal. If this
1773 -- is an entry in-parameter, it is replaced by a constant renaming
1774 -- for which Extra_Constrained is never created.
1776 if Present (Formal_Ent)
1777 and then Ekind (Formal_Ent) /= E_Constant
1778 and then Present (Extra_Constrained (Formal_Ent))
1779 then
1780 Rewrite (N,
1781 New_Occurrence_Of
1782 (Extra_Constrained (Formal_Ent), Sloc (N)));
1784 -- For variables with a Extra_Constrained field, we use the
1785 -- corresponding entity.
1787 elsif Nkind (Pref) = N_Identifier
1788 and then Ekind (Entity (Pref)) = E_Variable
1789 and then Present (Extra_Constrained (Entity (Pref)))
1790 then
1791 Rewrite (N,
1792 New_Occurrence_Of
1793 (Extra_Constrained (Entity (Pref)), Sloc (N)));
1795 -- For all other entity names, we can tell at compile time
1797 elsif Is_Entity_Name (Pref) then
1798 declare
1799 Ent : constant Entity_Id := Entity (Pref);
1800 Res : Boolean;
1802 begin
1803 -- (RM J.4) obsolescent cases
1805 if Is_Type (Ent) then
1807 -- Private type
1809 if Is_Private_Type (Ent) then
1810 Res := not Has_Discriminants (Ent)
1811 or else Is_Constrained (Ent);
1813 -- It not a private type, must be a generic actual type
1814 -- that corresponded to a private type. We know that this
1815 -- correspondence holds, since otherwise the reference
1816 -- within the generic template would have been illegal.
1818 else
1819 if Is_Composite_Type (Underlying_Type (Ent)) then
1820 Res := Is_Constrained (Ent);
1821 else
1822 Res := True;
1823 end if;
1824 end if;
1826 -- If the prefix is not a variable or is aliased, then
1827 -- definitely true; if it's a formal parameter without an
1828 -- associated extra formal, then treat it as constrained.
1830 -- Ada 2005 (AI-363): An aliased prefix must be known to be
1831 -- constrained in order to set the attribute to True.
1833 elsif not Is_Variable (Pref)
1834 or else Present (Formal_Ent)
1835 or else (Ada_Version < Ada_2005
1836 and then Is_Aliased_View (Pref))
1837 or else (Ada_Version >= Ada_2005
1838 and then Is_Constrained_Aliased_View (Pref))
1839 then
1840 Res := True;
1842 -- Variable case, look at type to see if it is constrained.
1843 -- Note that the one case where this is not accurate (the
1844 -- procedure formal case), has been handled above.
1846 -- We use the Underlying_Type here (and below) in case the
1847 -- type is private without discriminants, but the full type
1848 -- has discriminants. This case is illegal, but we generate it
1849 -- internally for passing to the Extra_Constrained parameter.
1851 else
1852 -- In Ada 2012, test for case of a limited tagged type, in
1853 -- which case the attribute is always required to return
1854 -- True. The underlying type is tested, to make sure we also
1855 -- return True for cases where there is an unconstrained
1856 -- object with an untagged limited partial view which has
1857 -- defaulted discriminants (such objects always produce a
1858 -- False in earlier versions of Ada). (Ada 2012: AI05-0214)
1860 Res := Is_Constrained (Underlying_Type (Etype (Ent)))
1861 or else
1862 (Ada_Version >= Ada_2012
1863 and then Is_Tagged_Type (Underlying_Type (Ptyp))
1864 and then Is_Limited_Type (Ptyp));
1865 end if;
1867 Rewrite (N, New_Reference_To (Boolean_Literals (Res), Loc));
1868 end;
1870 -- Prefix is not an entity name. These are also cases where we can
1871 -- always tell at compile time by looking at the form and type of the
1872 -- prefix. If an explicit dereference of an object with constrained
1873 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
1874 -- underlying type is a limited tagged type, then Constrained is
1875 -- required to always return True (Ada 2012: AI05-0214).
1877 else
1878 Rewrite (N,
1879 New_Reference_To (
1880 Boolean_Literals (
1881 not Is_Variable (Pref)
1882 or else
1883 (Nkind (Pref) = N_Explicit_Dereference
1884 and then
1885 not Effectively_Has_Constrained_Partial_View
1886 (Typ => Base_Type (Ptyp),
1887 Scop => Current_Scope))
1888 or else Is_Constrained (Underlying_Type (Ptyp))
1889 or else (Ada_Version >= Ada_2012
1890 and then Is_Tagged_Type (Underlying_Type (Ptyp))
1891 and then Is_Limited_Type (Ptyp))),
1892 Loc));
1893 end if;
1895 Analyze_And_Resolve (N, Standard_Boolean);
1896 end Constrained;
1898 ---------------
1899 -- Copy_Sign --
1900 ---------------
1902 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1903 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1905 when Attribute_Copy_Sign =>
1906 Expand_Fpt_Attribute_RR (N);
1908 -----------
1909 -- Count --
1910 -----------
1912 -- Transforms 'Count attribute into a call to the Count function
1914 when Attribute_Count => Count : declare
1915 Call : Node_Id;
1916 Conctyp : Entity_Id;
1917 Entnam : Node_Id;
1918 Entry_Id : Entity_Id;
1919 Index : Node_Id;
1920 Name : Node_Id;
1922 begin
1923 -- If the prefix is a member of an entry family, retrieve both
1924 -- entry name and index. For a simple entry there is no index.
1926 if Nkind (Pref) = N_Indexed_Component then
1927 Entnam := Prefix (Pref);
1928 Index := First (Expressions (Pref));
1929 else
1930 Entnam := Pref;
1931 Index := Empty;
1932 end if;
1934 Entry_Id := Entity (Entnam);
1936 -- Find the concurrent type in which this attribute is referenced
1937 -- (there had better be one).
1939 Conctyp := Current_Scope;
1940 while not Is_Concurrent_Type (Conctyp) loop
1941 Conctyp := Scope (Conctyp);
1942 end loop;
1944 -- Protected case
1946 if Is_Protected_Type (Conctyp) then
1947 case Corresponding_Runtime_Package (Conctyp) is
1948 when System_Tasking_Protected_Objects_Entries =>
1949 Name := New_Reference_To (RTE (RE_Protected_Count), Loc);
1951 Call :=
1952 Make_Function_Call (Loc,
1953 Name => Name,
1954 Parameter_Associations => New_List (
1955 New_Reference_To
1956 (Find_Protection_Object (Current_Scope), Loc),
1957 Entry_Index_Expression
1958 (Loc, Entry_Id, Index, Scope (Entry_Id))));
1960 when System_Tasking_Protected_Objects_Single_Entry =>
1961 Name :=
1962 New_Reference_To (RTE (RE_Protected_Count_Entry), Loc);
1964 Call :=
1965 Make_Function_Call (Loc,
1966 Name => Name,
1967 Parameter_Associations => New_List (
1968 New_Reference_To
1969 (Find_Protection_Object (Current_Scope), Loc)));
1971 when others =>
1972 raise Program_Error;
1973 end case;
1975 -- Task case
1977 else
1978 Call :=
1979 Make_Function_Call (Loc,
1980 Name => New_Reference_To (RTE (RE_Task_Count), Loc),
1981 Parameter_Associations => New_List (
1982 Entry_Index_Expression (Loc,
1983 Entry_Id, Index, Scope (Entry_Id))));
1984 end if;
1986 -- The call returns type Natural but the context is universal integer
1987 -- so any integer type is allowed. The attribute was already resolved
1988 -- so its Etype is the required result type. If the base type of the
1989 -- context type is other than Standard.Integer we put in a conversion
1990 -- to the required type. This can be a normal typed conversion since
1991 -- both input and output types of the conversion are integer types
1993 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
1994 Rewrite (N, Convert_To (Typ, Call));
1995 else
1996 Rewrite (N, Call);
1997 end if;
1999 Analyze_And_Resolve (N, Typ);
2000 end Count;
2002 ---------------------
2003 -- Descriptor_Size --
2004 ---------------------
2006 when Attribute_Descriptor_Size =>
2008 -- Attribute Descriptor_Size is handled by the back end when applied
2009 -- to an unconstrained array type.
2011 if Is_Array_Type (Ptyp)
2012 and then not Is_Constrained (Ptyp)
2013 then
2014 Apply_Universal_Integer_Attribute_Checks (N);
2016 -- For any other type, the descriptor size is 0 because there is no
2017 -- actual descriptor, but the result is not formally static.
2019 else
2020 Rewrite (N, Make_Integer_Literal (Loc, 0));
2021 Analyze (N);
2022 Set_Is_Static_Expression (N, False);
2023 end if;
2025 ---------------
2026 -- Elab_Body --
2027 ---------------
2029 -- This processing is shared by Elab_Spec
2031 -- What we do is to insert the following declarations
2033 -- procedure tnn;
2034 -- pragma Import (C, enn, "name___elabb/s");
2036 -- and then the Elab_Body/Spec attribute is replaced by a reference
2037 -- to this defining identifier.
2039 when Attribute_Elab_Body |
2040 Attribute_Elab_Spec =>
2042 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
2043 -- back-end knows how to handle these attributes directly.
2045 if CodePeer_Mode then
2046 return;
2047 end if;
2049 Elab_Body : declare
2050 Ent : constant Entity_Id := Make_Temporary (Loc, 'E');
2051 Str : String_Id;
2052 Lang : Node_Id;
2054 procedure Make_Elab_String (Nod : Node_Id);
2055 -- Given Nod, an identifier, or a selected component, put the
2056 -- image into the current string literal, with double underline
2057 -- between components.
2059 ----------------------
2060 -- Make_Elab_String --
2061 ----------------------
2063 procedure Make_Elab_String (Nod : Node_Id) is
2064 begin
2065 if Nkind (Nod) = N_Selected_Component then
2066 Make_Elab_String (Prefix (Nod));
2068 case VM_Target is
2069 when JVM_Target =>
2070 Store_String_Char ('$');
2071 when CLI_Target =>
2072 Store_String_Char ('.');
2073 when No_VM =>
2074 Store_String_Char ('_');
2075 Store_String_Char ('_');
2076 end case;
2078 Get_Name_String (Chars (Selector_Name (Nod)));
2080 else
2081 pragma Assert (Nkind (Nod) = N_Identifier);
2082 Get_Name_String (Chars (Nod));
2083 end if;
2085 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2086 end Make_Elab_String;
2088 -- Start of processing for Elab_Body/Elab_Spec
2090 begin
2091 -- First we need to prepare the string literal for the name of
2092 -- the elaboration routine to be referenced.
2094 Start_String;
2095 Make_Elab_String (Pref);
2097 if VM_Target = No_VM then
2098 Store_String_Chars ("___elab");
2099 Lang := Make_Identifier (Loc, Name_C);
2100 else
2101 Store_String_Chars ("._elab");
2102 Lang := Make_Identifier (Loc, Name_Ada);
2103 end if;
2105 if Id = Attribute_Elab_Body then
2106 Store_String_Char ('b');
2107 else
2108 Store_String_Char ('s');
2109 end if;
2111 Str := End_String;
2113 Insert_Actions (N, New_List (
2114 Make_Subprogram_Declaration (Loc,
2115 Specification =>
2116 Make_Procedure_Specification (Loc,
2117 Defining_Unit_Name => Ent)),
2119 Make_Pragma (Loc,
2120 Chars => Name_Import,
2121 Pragma_Argument_Associations => New_List (
2122 Make_Pragma_Argument_Association (Loc, Expression => Lang),
2124 Make_Pragma_Argument_Association (Loc,
2125 Expression => Make_Identifier (Loc, Chars (Ent))),
2127 Make_Pragma_Argument_Association (Loc,
2128 Expression => Make_String_Literal (Loc, Str))))));
2130 Set_Entity (N, Ent);
2131 Rewrite (N, New_Occurrence_Of (Ent, Loc));
2132 end Elab_Body;
2134 --------------------
2135 -- Elab_Subp_Body --
2136 --------------------
2138 -- Always ignored. In CodePeer mode, gnat2scil knows how to handle
2139 -- this attribute directly, and if we are not in CodePeer mode it is
2140 -- entirely ignored ???
2142 when Attribute_Elab_Subp_Body =>
2143 return;
2145 ----------------
2146 -- Elaborated --
2147 ----------------
2149 -- Elaborated is always True for preelaborated units, predefined units,
2150 -- pure units and units which have Elaborate_Body pragmas. These units
2151 -- have no elaboration entity.
2153 -- Note: The Elaborated attribute is never passed to the back end
2155 when Attribute_Elaborated => Elaborated : declare
2156 Ent : constant Entity_Id := Entity (Pref);
2158 begin
2159 if Present (Elaboration_Entity (Ent)) then
2160 Rewrite (N,
2161 Make_Op_Ne (Loc,
2162 Left_Opnd =>
2163 New_Occurrence_Of (Elaboration_Entity (Ent), Loc),
2164 Right_Opnd =>
2165 Make_Integer_Literal (Loc, Uint_0)));
2166 Analyze_And_Resolve (N, Typ);
2167 else
2168 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2169 end if;
2170 end Elaborated;
2172 --------------
2173 -- Enum_Rep --
2174 --------------
2176 when Attribute_Enum_Rep => Enum_Rep :
2177 begin
2178 -- X'Enum_Rep (Y) expands to
2180 -- target-type (Y)
2182 -- This is simply a direct conversion from the enumeration type to
2183 -- the target integer type, which is treated by the back end as a
2184 -- normal integer conversion, treating the enumeration type as an
2185 -- integer, which is exactly what we want! We set Conversion_OK to
2186 -- make sure that the analyzer does not complain about what otherwise
2187 -- might be an illegal conversion.
2189 if Is_Non_Empty_List (Exprs) then
2190 Rewrite (N,
2191 OK_Convert_To (Typ, Relocate_Node (First (Exprs))));
2193 -- X'Enum_Rep where X is an enumeration literal is replaced by
2194 -- the literal value.
2196 elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then
2197 Rewrite (N,
2198 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref))));
2200 -- If this is a renaming of a literal, recover the representation
2201 -- of the original.
2203 elsif Ekind (Entity (Pref)) = E_Constant
2204 and then Present (Renamed_Object (Entity (Pref)))
2205 and then
2206 Ekind (Entity (Renamed_Object (Entity (Pref))))
2207 = E_Enumeration_Literal
2208 then
2209 Rewrite (N,
2210 Make_Integer_Literal (Loc,
2211 Enumeration_Rep (Entity (Renamed_Object (Entity (Pref))))));
2213 -- X'Enum_Rep where X is an object does a direct unchecked conversion
2214 -- of the object value, as described for the type case above.
2216 else
2217 Rewrite (N,
2218 OK_Convert_To (Typ, Relocate_Node (Pref)));
2219 end if;
2221 Set_Etype (N, Typ);
2222 Analyze_And_Resolve (N, Typ);
2223 end Enum_Rep;
2225 --------------
2226 -- Enum_Val --
2227 --------------
2229 when Attribute_Enum_Val => Enum_Val : declare
2230 Expr : Node_Id;
2231 Btyp : constant Entity_Id := Base_Type (Ptyp);
2233 begin
2234 -- X'Enum_Val (Y) expands to
2236 -- [constraint_error when _rep_to_pos (Y, False) = -1, msg]
2237 -- X!(Y);
2239 Expr := Unchecked_Convert_To (Ptyp, First (Exprs));
2241 Insert_Action (N,
2242 Make_Raise_Constraint_Error (Loc,
2243 Condition =>
2244 Make_Op_Eq (Loc,
2245 Left_Opnd =>
2246 Make_Function_Call (Loc,
2247 Name =>
2248 New_Reference_To (TSS (Btyp, TSS_Rep_To_Pos), Loc),
2249 Parameter_Associations => New_List (
2250 Relocate_Node (Duplicate_Subexpr (Expr)),
2251 New_Occurrence_Of (Standard_False, Loc))),
2253 Right_Opnd => Make_Integer_Literal (Loc, -1)),
2254 Reason => CE_Range_Check_Failed));
2256 Rewrite (N, Expr);
2257 Analyze_And_Resolve (N, Ptyp);
2258 end Enum_Val;
2260 --------------
2261 -- Exponent --
2262 --------------
2264 -- Transforms 'Exponent into a call to the floating-point attribute
2265 -- function Exponent in Fat_xxx (where xxx is the root type)
2267 when Attribute_Exponent =>
2268 Expand_Fpt_Attribute_R (N);
2270 ------------------
2271 -- External_Tag --
2272 ------------------
2274 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
2276 when Attribute_External_Tag => External_Tag :
2277 begin
2278 Rewrite (N,
2279 Make_Function_Call (Loc,
2280 Name => New_Reference_To (RTE (RE_External_Tag), Loc),
2281 Parameter_Associations => New_List (
2282 Make_Attribute_Reference (Loc,
2283 Attribute_Name => Name_Tag,
2284 Prefix => Prefix (N)))));
2286 Analyze_And_Resolve (N, Standard_String);
2287 end External_Tag;
2289 -----------
2290 -- First --
2291 -----------
2293 when Attribute_First =>
2295 -- If the prefix type is a constrained packed array type which
2296 -- already has a Packed_Array_Type representation defined, then
2297 -- replace this attribute with a direct reference to 'First of the
2298 -- appropriate index subtype (since otherwise the back end will try
2299 -- to give us the value of 'First for this implementation type).
2301 if Is_Constrained_Packed_Array (Ptyp) then
2302 Rewrite (N,
2303 Make_Attribute_Reference (Loc,
2304 Attribute_Name => Name_First,
2305 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2306 Analyze_And_Resolve (N, Typ);
2308 elsif Is_Access_Type (Ptyp) then
2309 Apply_Access_Check (N);
2310 end if;
2312 ---------------
2313 -- First_Bit --
2314 ---------------
2316 -- Compute this if component clause was present, otherwise we leave the
2317 -- computation to be completed in the back-end, since we don't know what
2318 -- layout will be chosen.
2320 when Attribute_First_Bit => First_Bit_Attr : declare
2321 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2323 begin
2324 -- In Ada 2005 (or later) if we have the standard nondefault
2325 -- bit order, then we return the original value as given in
2326 -- the component clause (RM 2005 13.5.2(3/2)).
2328 if Present (Component_Clause (CE))
2329 and then Ada_Version >= Ada_2005
2330 and then not Reverse_Bit_Order (Scope (CE))
2331 then
2332 Rewrite (N,
2333 Make_Integer_Literal (Loc,
2334 Intval => Expr_Value (First_Bit (Component_Clause (CE)))));
2335 Analyze_And_Resolve (N, Typ);
2337 -- Otherwise (Ada 83/95 or Ada 2005 or later with reverse bit order),
2338 -- rewrite with normalized value if we know it statically.
2340 elsif Known_Static_Component_Bit_Offset (CE) then
2341 Rewrite (N,
2342 Make_Integer_Literal (Loc,
2343 Component_Bit_Offset (CE) mod System_Storage_Unit));
2344 Analyze_And_Resolve (N, Typ);
2346 -- Otherwise left to back end, just do universal integer checks
2348 else
2349 Apply_Universal_Integer_Attribute_Checks (N);
2350 end if;
2351 end First_Bit_Attr;
2353 -----------------
2354 -- Fixed_Value --
2355 -----------------
2357 -- We transform:
2359 -- fixtype'Fixed_Value (integer-value)
2361 -- into
2363 -- fixtype(integer-value)
2365 -- We do all the required analysis of the conversion here, because we do
2366 -- not want this to go through the fixed-point conversion circuits. Note
2367 -- that the back end always treats fixed-point as equivalent to the
2368 -- corresponding integer type anyway.
2370 when Attribute_Fixed_Value => Fixed_Value :
2371 begin
2372 Rewrite (N,
2373 Make_Type_Conversion (Loc,
2374 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2375 Expression => Relocate_Node (First (Exprs))));
2376 Set_Etype (N, Entity (Pref));
2377 Set_Analyzed (N);
2379 -- Note: it might appear that a properly analyzed unchecked conversion
2380 -- would be just fine here, but that's not the case, since the full
2381 -- range checks performed by the following call are critical!
2383 Apply_Type_Conversion_Checks (N);
2384 end Fixed_Value;
2386 -----------
2387 -- Floor --
2388 -----------
2390 -- Transforms 'Floor into a call to the floating-point attribute
2391 -- function Floor in Fat_xxx (where xxx is the root type)
2393 when Attribute_Floor =>
2394 Expand_Fpt_Attribute_R (N);
2396 ----------
2397 -- Fore --
2398 ----------
2400 -- For the fixed-point type Typ:
2402 -- Typ'Fore
2404 -- expands into
2406 -- Result_Type (System.Fore (Universal_Real (Type'First)),
2407 -- Universal_Real (Type'Last))
2409 -- Note that we know that the type is a non-static subtype, or Fore
2410 -- would have itself been computed dynamically in Eval_Attribute.
2412 when Attribute_Fore => Fore : begin
2413 Rewrite (N,
2414 Convert_To (Typ,
2415 Make_Function_Call (Loc,
2416 Name => New_Reference_To (RTE (RE_Fore), Loc),
2418 Parameter_Associations => New_List (
2419 Convert_To (Universal_Real,
2420 Make_Attribute_Reference (Loc,
2421 Prefix => New_Reference_To (Ptyp, Loc),
2422 Attribute_Name => Name_First)),
2424 Convert_To (Universal_Real,
2425 Make_Attribute_Reference (Loc,
2426 Prefix => New_Reference_To (Ptyp, Loc),
2427 Attribute_Name => Name_Last))))));
2429 Analyze_And_Resolve (N, Typ);
2430 end Fore;
2432 --------------
2433 -- Fraction --
2434 --------------
2436 -- Transforms 'Fraction into a call to the floating-point attribute
2437 -- function Fraction in Fat_xxx (where xxx is the root type)
2439 when Attribute_Fraction =>
2440 Expand_Fpt_Attribute_R (N);
2442 --------------
2443 -- From_Any --
2444 --------------
2446 when Attribute_From_Any => From_Any : declare
2447 P_Type : constant Entity_Id := Etype (Pref);
2448 Decls : constant List_Id := New_List;
2449 begin
2450 Rewrite (N,
2451 Build_From_Any_Call (P_Type,
2452 Relocate_Node (First (Exprs)),
2453 Decls));
2454 Insert_Actions (N, Decls);
2455 Analyze_And_Resolve (N, P_Type);
2456 end From_Any;
2458 --------------
2459 -- Identity --
2460 --------------
2462 -- For an exception returns a reference to the exception data:
2463 -- Exception_Id!(Prefix'Reference)
2465 -- For a task it returns a reference to the _task_id component of
2466 -- corresponding record:
2468 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
2470 -- in Ada.Task_Identification
2472 when Attribute_Identity => Identity : declare
2473 Id_Kind : Entity_Id;
2475 begin
2476 if Ptyp = Standard_Exception_Type then
2477 Id_Kind := RTE (RE_Exception_Id);
2479 if Present (Renamed_Object (Entity (Pref))) then
2480 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
2481 end if;
2483 Rewrite (N,
2484 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
2485 else
2486 Id_Kind := RTE (RO_AT_Task_Id);
2488 -- If the prefix is a task interface, the Task_Id is obtained
2489 -- dynamically through a dispatching call, as for other task
2490 -- attributes applied to interfaces.
2492 if Ada_Version >= Ada_2005
2493 and then Ekind (Ptyp) = E_Class_Wide_Type
2494 and then Is_Interface (Ptyp)
2495 and then Is_Task_Interface (Ptyp)
2496 then
2497 Rewrite (N,
2498 Unchecked_Convert_To (Id_Kind,
2499 Make_Selected_Component (Loc,
2500 Prefix =>
2501 New_Copy_Tree (Pref),
2502 Selector_Name =>
2503 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))));
2505 else
2506 Rewrite (N,
2507 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
2508 end if;
2509 end if;
2511 Analyze_And_Resolve (N, Id_Kind);
2512 end Identity;
2514 -----------
2515 -- Image --
2516 -----------
2518 -- Image attribute is handled in separate unit Exp_Imgv
2520 when Attribute_Image =>
2521 Exp_Imgv.Expand_Image_Attribute (N);
2523 ---------
2524 -- Img --
2525 ---------
2527 -- X'Img is expanded to typ'Image (X), where typ is the type of X
2529 when Attribute_Img => Img :
2530 begin
2531 Rewrite (N,
2532 Make_Attribute_Reference (Loc,
2533 Prefix => New_Reference_To (Ptyp, Loc),
2534 Attribute_Name => Name_Image,
2535 Expressions => New_List (Relocate_Node (Pref))));
2537 Analyze_And_Resolve (N, Standard_String);
2538 end Img;
2540 -----------
2541 -- Input --
2542 -----------
2544 when Attribute_Input => Input : declare
2545 P_Type : constant Entity_Id := Entity (Pref);
2546 B_Type : constant Entity_Id := Base_Type (P_Type);
2547 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2548 Strm : constant Node_Id := First (Exprs);
2549 Fname : Entity_Id;
2550 Decl : Node_Id;
2551 Call : Node_Id;
2552 Prag : Node_Id;
2553 Arg2 : Node_Id;
2554 Rfunc : Node_Id;
2556 Cntrl : Node_Id := Empty;
2557 -- Value for controlling argument in call. Always Empty except in
2558 -- the dispatching (class-wide type) case, where it is a reference
2559 -- to the dummy object initialized to the right internal tag.
2561 procedure Freeze_Stream_Subprogram (F : Entity_Id);
2562 -- The expansion of the attribute reference may generate a call to
2563 -- a user-defined stream subprogram that is frozen by the call. This
2564 -- can lead to access-before-elaboration problem if the reference
2565 -- appears in an object declaration and the subprogram body has not
2566 -- been seen. The freezing of the subprogram requires special code
2567 -- because it appears in an expanded context where expressions do
2568 -- not freeze their constituents.
2570 ------------------------------
2571 -- Freeze_Stream_Subprogram --
2572 ------------------------------
2574 procedure Freeze_Stream_Subprogram (F : Entity_Id) is
2575 Decl : constant Node_Id := Unit_Declaration_Node (F);
2576 Bod : Node_Id;
2578 begin
2579 -- If this is user-defined subprogram, the corresponding
2580 -- stream function appears as a renaming-as-body, and the
2581 -- user subprogram must be retrieved by tree traversal.
2583 if Present (Decl)
2584 and then Nkind (Decl) = N_Subprogram_Declaration
2585 and then Present (Corresponding_Body (Decl))
2586 then
2587 Bod := Corresponding_Body (Decl);
2589 if Nkind (Unit_Declaration_Node (Bod)) =
2590 N_Subprogram_Renaming_Declaration
2591 then
2592 Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
2593 end if;
2594 end if;
2595 end Freeze_Stream_Subprogram;
2597 -- Start of processing for Input
2599 begin
2600 -- If no underlying type, we have an error that will be diagnosed
2601 -- elsewhere, so here we just completely ignore the expansion.
2603 if No (U_Type) then
2604 return;
2605 end if;
2607 -- If there is a TSS for Input, just call it
2609 Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
2611 if Present (Fname) then
2612 null;
2614 else
2615 -- If there is a Stream_Convert pragma, use it, we rewrite
2617 -- sourcetyp'Input (stream)
2619 -- as
2621 -- sourcetyp (streamread (strmtyp'Input (stream)));
2623 -- where streamread is the given Read function that converts an
2624 -- argument of type strmtyp to type sourcetyp or a type from which
2625 -- it is derived (extra conversion required for the derived case).
2627 Prag := Get_Stream_Convert_Pragma (P_Type);
2629 if Present (Prag) then
2630 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
2631 Rfunc := Entity (Expression (Arg2));
2633 Rewrite (N,
2634 Convert_To (B_Type,
2635 Make_Function_Call (Loc,
2636 Name => New_Occurrence_Of (Rfunc, Loc),
2637 Parameter_Associations => New_List (
2638 Make_Attribute_Reference (Loc,
2639 Prefix =>
2640 New_Occurrence_Of
2641 (Etype (First_Formal (Rfunc)), Loc),
2642 Attribute_Name => Name_Input,
2643 Expressions => Exprs)))));
2645 Analyze_And_Resolve (N, B_Type);
2646 return;
2648 -- Elementary types
2650 elsif Is_Elementary_Type (U_Type) then
2652 -- A special case arises if we have a defined _Read routine,
2653 -- since in this case we are required to call this routine.
2655 if Present (TSS (Base_Type (U_Type), TSS_Stream_Read)) then
2656 Build_Record_Or_Elementary_Input_Function
2657 (Loc, U_Type, Decl, Fname);
2658 Insert_Action (N, Decl);
2660 -- For normal cases, we call the I_xxx routine directly
2662 else
2663 Rewrite (N, Build_Elementary_Input_Call (N));
2664 Analyze_And_Resolve (N, P_Type);
2665 return;
2666 end if;
2668 -- Array type case
2670 elsif Is_Array_Type (U_Type) then
2671 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
2672 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2674 -- Dispatching case with class-wide type
2676 elsif Is_Class_Wide_Type (P_Type) then
2678 -- No need to do anything else compiling under restriction
2679 -- No_Dispatching_Calls. During the semantic analysis we
2680 -- already notified such violation.
2682 if Restriction_Active (No_Dispatching_Calls) then
2683 return;
2684 end if;
2686 declare
2687 Rtyp : constant Entity_Id := Root_Type (P_Type);
2688 Dnn : Entity_Id;
2689 Decl : Node_Id;
2690 Expr : Node_Id;
2692 begin
2693 -- Read the internal tag (RM 13.13.2(34)) and use it to
2694 -- initialize a dummy tag object:
2696 -- Dnn : Ada.Tags.Tag :=
2697 -- Descendant_Tag (String'Input (Strm), P_Type);
2699 -- This dummy object is used only to provide a controlling
2700 -- argument for the eventual _Input call. Descendant_Tag is
2701 -- called rather than Internal_Tag to ensure that we have a
2702 -- tag for a type that is descended from the prefix type and
2703 -- declared at the same accessibility level (the exception
2704 -- Tag_Error will be raised otherwise). The level check is
2705 -- required for Ada 2005 because tagged types can be
2706 -- extended in nested scopes (AI-344).
2708 Expr :=
2709 Make_Function_Call (Loc,
2710 Name =>
2711 New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc),
2712 Parameter_Associations => New_List (
2713 Make_Attribute_Reference (Loc,
2714 Prefix => New_Occurrence_Of (Standard_String, Loc),
2715 Attribute_Name => Name_Input,
2716 Expressions => New_List (
2717 Relocate_Node (Duplicate_Subexpr (Strm)))),
2718 Make_Attribute_Reference (Loc,
2719 Prefix => New_Reference_To (P_Type, Loc),
2720 Attribute_Name => Name_Tag)));
2722 Dnn := Make_Temporary (Loc, 'D', Expr);
2724 Decl :=
2725 Make_Object_Declaration (Loc,
2726 Defining_Identifier => Dnn,
2727 Object_Definition =>
2728 New_Occurrence_Of (RTE (RE_Tag), Loc),
2729 Expression => Expr);
2731 Insert_Action (N, Decl);
2733 -- Now we need to get the entity for the call, and construct
2734 -- a function call node, where we preset a reference to Dnn
2735 -- as the controlling argument (doing an unchecked convert
2736 -- to the class-wide tagged type to make it look like a real
2737 -- tagged object).
2739 Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
2740 Cntrl :=
2741 Unchecked_Convert_To (P_Type,
2742 New_Occurrence_Of (Dnn, Loc));
2743 Set_Etype (Cntrl, P_Type);
2744 Set_Parent (Cntrl, N);
2745 end;
2747 -- For tagged types, use the primitive Input function
2749 elsif Is_Tagged_Type (U_Type) then
2750 Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
2752 -- All other record type cases, including protected records. The
2753 -- latter only arise for expander generated code for handling
2754 -- shared passive partition access.
2756 else
2757 pragma Assert
2758 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2760 -- Ada 2005 (AI-216): Program_Error is raised executing default
2761 -- implementation of the Input attribute of an unchecked union
2762 -- type if the type lacks default discriminant values.
2764 if Is_Unchecked_Union (Base_Type (U_Type))
2765 and then No (Discriminant_Constraint (U_Type))
2766 then
2767 Insert_Action (N,
2768 Make_Raise_Program_Error (Loc,
2769 Reason => PE_Unchecked_Union_Restriction));
2771 return;
2772 end if;
2774 -- Build the type's Input function, passing the subtype rather
2775 -- than its base type, because checks are needed in the case of
2776 -- constrained discriminants (see Ada 2012 AI05-0192).
2778 Build_Record_Or_Elementary_Input_Function
2779 (Loc, U_Type, Decl, Fname);
2780 Insert_Action (N, Decl);
2782 if Nkind (Parent (N)) = N_Object_Declaration
2783 and then Is_Record_Type (U_Type)
2784 then
2785 -- The stream function may contain calls to user-defined
2786 -- Read procedures for individual components.
2788 declare
2789 Comp : Entity_Id;
2790 Func : Entity_Id;
2792 begin
2793 Comp := First_Component (U_Type);
2794 while Present (Comp) loop
2795 Func :=
2796 Find_Stream_Subprogram
2797 (Etype (Comp), TSS_Stream_Read);
2799 if Present (Func) then
2800 Freeze_Stream_Subprogram (Func);
2801 end if;
2803 Next_Component (Comp);
2804 end loop;
2805 end;
2806 end if;
2807 end if;
2808 end if;
2810 -- If we fall through, Fname is the function to be called. The result
2811 -- is obtained by calling the appropriate function, then converting
2812 -- the result. The conversion does a subtype check.
2814 Call :=
2815 Make_Function_Call (Loc,
2816 Name => New_Occurrence_Of (Fname, Loc),
2817 Parameter_Associations => New_List (
2818 Relocate_Node (Strm)));
2820 Set_Controlling_Argument (Call, Cntrl);
2821 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
2822 Analyze_And_Resolve (N, P_Type);
2824 if Nkind (Parent (N)) = N_Object_Declaration then
2825 Freeze_Stream_Subprogram (Fname);
2826 end if;
2827 end Input;
2829 -------------------
2830 -- Integer_Value --
2831 -------------------
2833 -- We transform
2835 -- inttype'Fixed_Value (fixed-value)
2837 -- into
2839 -- inttype(integer-value))
2841 -- we do all the required analysis of the conversion here, because we do
2842 -- not want this to go through the fixed-point conversion circuits. Note
2843 -- that the back end always treats fixed-point as equivalent to the
2844 -- corresponding integer type anyway.
2846 when Attribute_Integer_Value => Integer_Value :
2847 begin
2848 Rewrite (N,
2849 Make_Type_Conversion (Loc,
2850 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2851 Expression => Relocate_Node (First (Exprs))));
2852 Set_Etype (N, Entity (Pref));
2853 Set_Analyzed (N);
2855 -- Note: it might appear that a properly analyzed unchecked conversion
2856 -- would be just fine here, but that's not the case, since the full
2857 -- range checks performed by the following call are critical!
2859 Apply_Type_Conversion_Checks (N);
2860 end Integer_Value;
2862 -------------------
2863 -- Invalid_Value --
2864 -------------------
2866 when Attribute_Invalid_Value =>
2867 Rewrite (N, Get_Simple_Init_Val (Ptyp, N));
2869 ----------
2870 -- Last --
2871 ----------
2873 when Attribute_Last =>
2875 -- If the prefix type is a constrained packed array type which
2876 -- already has a Packed_Array_Type representation defined, then
2877 -- replace this attribute with a direct reference to 'Last of the
2878 -- appropriate index subtype (since otherwise the back end will try
2879 -- to give us the value of 'Last for this implementation type).
2881 if Is_Constrained_Packed_Array (Ptyp) then
2882 Rewrite (N,
2883 Make_Attribute_Reference (Loc,
2884 Attribute_Name => Name_Last,
2885 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2886 Analyze_And_Resolve (N, Typ);
2888 elsif Is_Access_Type (Ptyp) then
2889 Apply_Access_Check (N);
2890 end if;
2892 --------------
2893 -- Last_Bit --
2894 --------------
2896 -- We compute this if a component clause was present, otherwise we leave
2897 -- the computation up to the back end, since we don't know what layout
2898 -- will be chosen.
2900 when Attribute_Last_Bit => Last_Bit_Attr : declare
2901 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2903 begin
2904 -- In Ada 2005 (or later) if we have the standard nondefault
2905 -- bit order, then we return the original value as given in
2906 -- the component clause (RM 2005 13.5.2(4/2)).
2908 if Present (Component_Clause (CE))
2909 and then Ada_Version >= Ada_2005
2910 and then not Reverse_Bit_Order (Scope (CE))
2911 then
2912 Rewrite (N,
2913 Make_Integer_Literal (Loc,
2914 Intval => Expr_Value (Last_Bit (Component_Clause (CE)))));
2915 Analyze_And_Resolve (N, Typ);
2917 -- Otherwise (Ada 83/95 or Ada 2005 or later with reverse bit order),
2918 -- rewrite with normalized value if we know it statically.
2920 elsif Known_Static_Component_Bit_Offset (CE)
2921 and then Known_Static_Esize (CE)
2922 then
2923 Rewrite (N,
2924 Make_Integer_Literal (Loc,
2925 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
2926 + Esize (CE) - 1));
2927 Analyze_And_Resolve (N, Typ);
2929 -- Otherwise leave to back end, just apply universal integer checks
2931 else
2932 Apply_Universal_Integer_Attribute_Checks (N);
2933 end if;
2934 end Last_Bit_Attr;
2936 ------------------
2937 -- Leading_Part --
2938 ------------------
2940 -- Transforms 'Leading_Part into a call to the floating-point attribute
2941 -- function Leading_Part in Fat_xxx (where xxx is the root type)
2943 -- Note: strictly, we should generate special case code to deal with
2944 -- absurdly large positive arguments (greater than Integer'Last), which
2945 -- result in returning the first argument unchanged, but it hardly seems
2946 -- worth the effort. We raise constraint error for absurdly negative
2947 -- arguments which is fine.
2949 when Attribute_Leading_Part =>
2950 Expand_Fpt_Attribute_RI (N);
2952 ------------
2953 -- Length --
2954 ------------
2956 when Attribute_Length => declare
2957 Ityp : Entity_Id;
2958 Xnum : Uint;
2960 begin
2961 -- Processing for packed array types
2963 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
2964 Ityp := Get_Index_Subtype (N);
2966 -- If the index type, Ityp, is an enumeration type with holes,
2967 -- then we calculate X'Length explicitly using
2969 -- Typ'Max
2970 -- (0, Ityp'Pos (X'Last (N)) -
2971 -- Ityp'Pos (X'First (N)) + 1);
2973 -- Since the bounds in the template are the representation values
2974 -- and the back end would get the wrong value.
2976 if Is_Enumeration_Type (Ityp)
2977 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
2978 then
2979 if No (Exprs) then
2980 Xnum := Uint_1;
2981 else
2982 Xnum := Expr_Value (First (Expressions (N)));
2983 end if;
2985 Rewrite (N,
2986 Make_Attribute_Reference (Loc,
2987 Prefix => New_Occurrence_Of (Typ, Loc),
2988 Attribute_Name => Name_Max,
2989 Expressions => New_List
2990 (Make_Integer_Literal (Loc, 0),
2992 Make_Op_Add (Loc,
2993 Left_Opnd =>
2994 Make_Op_Subtract (Loc,
2995 Left_Opnd =>
2996 Make_Attribute_Reference (Loc,
2997 Prefix => New_Occurrence_Of (Ityp, Loc),
2998 Attribute_Name => Name_Pos,
3000 Expressions => New_List (
3001 Make_Attribute_Reference (Loc,
3002 Prefix => Duplicate_Subexpr (Pref),
3003 Attribute_Name => Name_Last,
3004 Expressions => New_List (
3005 Make_Integer_Literal (Loc, Xnum))))),
3007 Right_Opnd =>
3008 Make_Attribute_Reference (Loc,
3009 Prefix => New_Occurrence_Of (Ityp, Loc),
3010 Attribute_Name => Name_Pos,
3012 Expressions => New_List (
3013 Make_Attribute_Reference (Loc,
3014 Prefix =>
3015 Duplicate_Subexpr_No_Checks (Pref),
3016 Attribute_Name => Name_First,
3017 Expressions => New_List (
3018 Make_Integer_Literal (Loc, Xnum)))))),
3020 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3022 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
3023 return;
3025 -- If the prefix type is a constrained packed array type which
3026 -- already has a Packed_Array_Type representation defined, then
3027 -- replace this attribute with a direct reference to 'Range_Length
3028 -- of the appropriate index subtype (since otherwise the back end
3029 -- will try to give us the value of 'Length for this
3030 -- implementation type).
3032 elsif Is_Constrained (Ptyp) then
3033 Rewrite (N,
3034 Make_Attribute_Reference (Loc,
3035 Attribute_Name => Name_Range_Length,
3036 Prefix => New_Reference_To (Ityp, Loc)));
3037 Analyze_And_Resolve (N, Typ);
3038 end if;
3040 -- Access type case
3042 elsif Is_Access_Type (Ptyp) then
3043 Apply_Access_Check (N);
3045 -- If the designated type is a packed array type, then we convert
3046 -- the reference to:
3048 -- typ'Max (0, 1 +
3049 -- xtyp'Pos (Pref'Last (Expr)) -
3050 -- xtyp'Pos (Pref'First (Expr)));
3052 -- This is a bit complex, but it is the easiest thing to do that
3053 -- works in all cases including enum types with holes xtyp here
3054 -- is the appropriate index type.
3056 declare
3057 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
3058 Xtyp : Entity_Id;
3060 begin
3061 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
3062 Xtyp := Get_Index_Subtype (N);
3064 Rewrite (N,
3065 Make_Attribute_Reference (Loc,
3066 Prefix => New_Occurrence_Of (Typ, Loc),
3067 Attribute_Name => Name_Max,
3068 Expressions => New_List (
3069 Make_Integer_Literal (Loc, 0),
3071 Make_Op_Add (Loc,
3072 Make_Integer_Literal (Loc, 1),
3073 Make_Op_Subtract (Loc,
3074 Left_Opnd =>
3075 Make_Attribute_Reference (Loc,
3076 Prefix => New_Occurrence_Of (Xtyp, Loc),
3077 Attribute_Name => Name_Pos,
3078 Expressions => New_List (
3079 Make_Attribute_Reference (Loc,
3080 Prefix => Duplicate_Subexpr (Pref),
3081 Attribute_Name => Name_Last,
3082 Expressions =>
3083 New_Copy_List (Exprs)))),
3085 Right_Opnd =>
3086 Make_Attribute_Reference (Loc,
3087 Prefix => New_Occurrence_Of (Xtyp, Loc),
3088 Attribute_Name => Name_Pos,
3089 Expressions => New_List (
3090 Make_Attribute_Reference (Loc,
3091 Prefix =>
3092 Duplicate_Subexpr_No_Checks (Pref),
3093 Attribute_Name => Name_First,
3094 Expressions =>
3095 New_Copy_List (Exprs)))))))));
3097 Analyze_And_Resolve (N, Typ);
3098 end if;
3099 end;
3101 -- Otherwise leave it to the back end
3103 else
3104 Apply_Universal_Integer_Attribute_Checks (N);
3105 end if;
3106 end;
3108 -------------
3109 -- Machine --
3110 -------------
3112 -- Transforms 'Machine into a call to the floating-point attribute
3113 -- function Machine in Fat_xxx (where xxx is the root type)
3115 when Attribute_Machine =>
3116 Expand_Fpt_Attribute_R (N);
3118 ----------------------
3119 -- Machine_Rounding --
3120 ----------------------
3122 -- Transforms 'Machine_Rounding into a call to the floating-point
3123 -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root
3124 -- type). Expansion is avoided for cases the back end can handle
3125 -- directly.
3127 when Attribute_Machine_Rounding =>
3128 if not Is_Inline_Floating_Point_Attribute (N) then
3129 Expand_Fpt_Attribute_R (N);
3130 end if;
3132 ------------------
3133 -- Machine_Size --
3134 ------------------
3136 -- Machine_Size is equivalent to Object_Size, so transform it into
3137 -- Object_Size and that way the back end never sees Machine_Size.
3139 when Attribute_Machine_Size =>
3140 Rewrite (N,
3141 Make_Attribute_Reference (Loc,
3142 Prefix => Prefix (N),
3143 Attribute_Name => Name_Object_Size));
3145 Analyze_And_Resolve (N, Typ);
3147 --------------
3148 -- Mantissa --
3149 --------------
3151 -- The only case that can get this far is the dynamic case of the old
3152 -- Ada 83 Mantissa attribute for the fixed-point case. For this case,
3153 -- we expand:
3155 -- typ'Mantissa
3157 -- into
3159 -- ityp (System.Mantissa.Mantissa_Value
3160 -- (Integer'Integer_Value (typ'First),
3161 -- Integer'Integer_Value (typ'Last)));
3163 when Attribute_Mantissa => Mantissa : begin
3164 Rewrite (N,
3165 Convert_To (Typ,
3166 Make_Function_Call (Loc,
3167 Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
3169 Parameter_Associations => New_List (
3171 Make_Attribute_Reference (Loc,
3172 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
3173 Attribute_Name => Name_Integer_Value,
3174 Expressions => New_List (
3176 Make_Attribute_Reference (Loc,
3177 Prefix => New_Occurrence_Of (Ptyp, Loc),
3178 Attribute_Name => Name_First))),
3180 Make_Attribute_Reference (Loc,
3181 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
3182 Attribute_Name => Name_Integer_Value,
3183 Expressions => New_List (
3185 Make_Attribute_Reference (Loc,
3186 Prefix => New_Occurrence_Of (Ptyp, Loc),
3187 Attribute_Name => Name_Last)))))));
3189 Analyze_And_Resolve (N, Typ);
3190 end Mantissa;
3192 ----------------------------------
3193 -- Max_Size_In_Storage_Elements --
3194 ----------------------------------
3196 when Attribute_Max_Size_In_Storage_Elements => declare
3197 Typ : constant Entity_Id := Etype (N);
3198 Attr : Node_Id;
3200 Conversion_Added : Boolean := False;
3201 -- A flag which tracks whether the original attribute has been
3202 -- wrapped inside a type conversion.
3204 begin
3205 Apply_Universal_Integer_Attribute_Checks (N);
3207 -- The universal integer check may sometimes add a type conversion,
3208 -- retrieve the original attribute reference from the expression.
3210 Attr := N;
3211 if Nkind (Attr) = N_Type_Conversion then
3212 Attr := Expression (Attr);
3213 Conversion_Added := True;
3214 end if;
3216 -- Heap-allocated controlled objects contain two extra pointers which
3217 -- are not part of the actual type. Transform the attribute reference
3218 -- into a runtime expression to add the size of the hidden header.
3220 -- Do not perform this expansion on .NET/JVM targets because the
3221 -- two pointers are already present in the type.
3223 if VM_Target = No_VM
3224 and then Nkind (Attr) = N_Attribute_Reference
3225 and then Needs_Finalization (Ptyp)
3226 and then not Header_Size_Added (Attr)
3227 then
3228 Set_Header_Size_Added (Attr);
3230 -- Generate:
3231 -- P'Max_Size_In_Storage_Elements +
3232 -- Universal_Integer
3233 -- (Header_Size_With_Padding (Ptyp'Alignment))
3235 Rewrite (Attr,
3236 Make_Op_Add (Loc,
3237 Left_Opnd => Relocate_Node (Attr),
3238 Right_Opnd =>
3239 Convert_To (Universal_Integer,
3240 Make_Function_Call (Loc,
3241 Name =>
3242 New_Reference_To
3243 (RTE (RE_Header_Size_With_Padding), Loc),
3245 Parameter_Associations => New_List (
3246 Make_Attribute_Reference (Loc,
3247 Prefix =>
3248 New_Reference_To (Ptyp, Loc),
3249 Attribute_Name => Name_Alignment))))));
3251 -- Add a conversion to the target type
3253 if not Conversion_Added then
3254 Rewrite (Attr,
3255 Make_Type_Conversion (Loc,
3256 Subtype_Mark => New_Reference_To (Typ, Loc),
3257 Expression => Relocate_Node (Attr)));
3258 end if;
3260 Analyze (Attr);
3261 return;
3262 end if;
3263 end;
3265 --------------------
3266 -- Mechanism_Code --
3267 --------------------
3269 when Attribute_Mechanism_Code =>
3271 -- We must replace the prefix in the renamed case
3273 if Is_Entity_Name (Pref)
3274 and then Present (Alias (Entity (Pref)))
3275 then
3276 Set_Renamed_Subprogram (Pref, Alias (Entity (Pref)));
3277 end if;
3279 ---------
3280 -- Mod --
3281 ---------
3283 when Attribute_Mod => Mod_Case : declare
3284 Arg : constant Node_Id := Relocate_Node (First (Exprs));
3285 Hi : constant Node_Id := Type_High_Bound (Etype (Arg));
3286 Modv : constant Uint := Modulus (Btyp);
3288 begin
3290 -- This is not so simple. The issue is what type to use for the
3291 -- computation of the modular value.
3293 -- The easy case is when the modulus value is within the bounds
3294 -- of the signed integer type of the argument. In this case we can
3295 -- just do the computation in that signed integer type, and then
3296 -- do an ordinary conversion to the target type.
3298 if Modv <= Expr_Value (Hi) then
3299 Rewrite (N,
3300 Convert_To (Btyp,
3301 Make_Op_Mod (Loc,
3302 Left_Opnd => Arg,
3303 Right_Opnd => Make_Integer_Literal (Loc, Modv))));
3305 -- Here we know that the modulus is larger than type'Last of the
3306 -- integer type. There are two cases to consider:
3308 -- a) The integer value is non-negative. In this case, it is
3309 -- returned as the result (since it is less than the modulus).
3311 -- b) The integer value is negative. In this case, we know that the
3312 -- result is modulus + value, where the value might be as small as
3313 -- -modulus. The trouble is what type do we use to do the subtract.
3314 -- No type will do, since modulus can be as big as 2**64, and no
3315 -- integer type accommodates this value. Let's do bit of algebra
3317 -- modulus + value
3318 -- = modulus - (-value)
3319 -- = (modulus - 1) - (-value - 1)
3321 -- Now modulus - 1 is certainly in range of the modular type.
3322 -- -value is in the range 1 .. modulus, so -value -1 is in the
3323 -- range 0 .. modulus-1 which is in range of the modular type.
3324 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
3325 -- which we can compute using the integer base type.
3327 -- Once this is done we analyze the if expression without range
3328 -- checks, because we know everything is in range, and we want
3329 -- to prevent spurious warnings on either branch.
3331 else
3332 Rewrite (N,
3333 Make_If_Expression (Loc,
3334 Expressions => New_List (
3335 Make_Op_Ge (Loc,
3336 Left_Opnd => Duplicate_Subexpr (Arg),
3337 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3339 Convert_To (Btyp,
3340 Duplicate_Subexpr_No_Checks (Arg)),
3342 Make_Op_Subtract (Loc,
3343 Left_Opnd =>
3344 Make_Integer_Literal (Loc,
3345 Intval => Modv - 1),
3346 Right_Opnd =>
3347 Convert_To (Btyp,
3348 Make_Op_Minus (Loc,
3349 Right_Opnd =>
3350 Make_Op_Add (Loc,
3351 Left_Opnd => Duplicate_Subexpr_No_Checks (Arg),
3352 Right_Opnd =>
3353 Make_Integer_Literal (Loc,
3354 Intval => 1))))))));
3356 end if;
3358 Analyze_And_Resolve (N, Btyp, Suppress => All_Checks);
3359 end Mod_Case;
3361 -----------
3362 -- Model --
3363 -----------
3365 -- Transforms 'Model into a call to the floating-point attribute
3366 -- function Model in Fat_xxx (where xxx is the root type)
3368 when Attribute_Model =>
3369 Expand_Fpt_Attribute_R (N);
3371 -----------------
3372 -- Object_Size --
3373 -----------------
3375 -- The processing for Object_Size shares the processing for Size
3377 ---------
3378 -- Old --
3379 ---------
3381 when Attribute_Old => Old : declare
3382 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', Pref);
3383 Subp : Node_Id;
3384 Asn_Stm : Node_Id;
3386 begin
3387 -- If assertions are disabled, no need to create the declaration
3388 -- that preserves the value.
3390 if not Assertions_Enabled then
3391 return;
3392 end if;
3394 -- Find the nearest subprogram body, ignoring _Preconditions
3396 Subp := N;
3397 loop
3398 Subp := Parent (Subp);
3399 exit when Nkind (Subp) = N_Subprogram_Body
3400 and then Chars (Defining_Entity (Subp)) /= Name_uPostconditions;
3401 end loop;
3403 -- Insert the initialized object declaration at the start of the
3404 -- subprogram's declarations.
3406 Asn_Stm :=
3407 Make_Object_Declaration (Loc,
3408 Defining_Identifier => Tnn,
3409 Constant_Present => True,
3410 Object_Definition => New_Occurrence_Of (Etype (N), Loc),
3411 Expression => Pref);
3413 -- Push the subprogram's scope, so that the object will be analyzed
3414 -- in that context (rather than the context of the Precondition
3415 -- subprogram) and will have its Scope set properly.
3417 if Present (Corresponding_Spec (Subp)) then
3418 Push_Scope (Corresponding_Spec (Subp));
3419 else
3420 Push_Scope (Defining_Entity (Subp));
3421 end if;
3423 if Is_Empty_List (Declarations (Subp)) then
3424 Set_Declarations (Subp, New_List (Asn_Stm));
3425 Analyze (Asn_Stm);
3426 else
3427 Insert_Action (First (Declarations (Subp)), Asn_Stm);
3428 end if;
3430 Pop_Scope;
3432 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
3433 end Old;
3435 ----------------------
3436 -- Overlaps_Storage --
3437 ----------------------
3439 when Attribute_Overlaps_Storage => Overlaps_Storage : declare
3440 Loc : constant Source_Ptr := Sloc (N);
3442 X : constant Node_Id := Prefix (N);
3443 Y : constant Node_Id := First (Expressions (N));
3444 -- The argumens
3446 X_Addr, Y_Addr : Node_Id;
3447 -- the expressions for their integer addresses
3449 X_Size, Y_Size : Node_Id;
3450 -- the expressions for their sizes
3452 Cond : Node_Id;
3454 begin
3455 -- Attribute expands into:
3457 -- if X'Address < Y'address then
3458 -- (X'address + X'Size - 1) >= Y'address
3459 -- else
3460 -- (Y'address + Y'size - 1) >= X'Address
3461 -- end if;
3463 -- with the proper address operations. We convert addresses to
3464 -- integer addresses to use predefined arithmetic. The size is
3465 -- expressed in storage units.
3467 X_Addr :=
3468 Unchecked_Convert_To (RTE (RE_Integer_Address),
3469 Make_Attribute_Reference (Loc,
3470 Attribute_Name => Name_Address,
3471 Prefix => New_Copy_Tree (X)));
3473 Y_Addr :=
3474 Unchecked_Convert_To (RTE (RE_Integer_Address),
3475 Make_Attribute_Reference (Loc,
3476 Attribute_Name => Name_Address,
3477 Prefix => New_Copy_Tree (Y)));
3479 X_Size :=
3480 Make_Op_Divide (Loc,
3481 Left_Opnd =>
3482 Make_Attribute_Reference (Loc,
3483 Attribute_Name => Name_Size,
3484 Prefix => New_Copy_Tree (X)),
3485 Right_Opnd =>
3486 Make_Integer_Literal (Loc, System_Storage_Unit));
3488 Y_Size :=
3489 Make_Op_Divide (Loc,
3490 Left_Opnd =>
3491 Make_Attribute_Reference (Loc,
3492 Attribute_Name => Name_Size,
3493 Prefix => New_Copy_Tree (Y)),
3494 Right_Opnd =>
3495 Make_Integer_Literal (Loc, System_Storage_Unit));
3497 Cond :=
3498 Make_Op_Le (Loc,
3499 Left_Opnd => X_Addr,
3500 Right_Opnd => Y_Addr);
3502 Rewrite (N,
3503 Make_If_Expression (Loc,
3504 New_List (
3505 Cond,
3507 Make_Op_Ge (Loc,
3508 Left_Opnd =>
3509 Make_Op_Add (Loc,
3510 Left_Opnd => X_Addr,
3511 Right_Opnd =>
3512 Make_Op_Subtract (Loc,
3513 Left_Opnd => X_Size,
3514 Right_Opnd => Make_Integer_Literal (Loc, 1))),
3515 Right_Opnd => Y_Addr),
3517 Make_Op_Ge (Loc,
3518 Make_Op_Add (Loc,
3519 Left_Opnd => Y_Addr,
3520 Right_Opnd =>
3521 Make_Op_Subtract (Loc,
3522 Left_Opnd => Y_Size,
3523 Right_Opnd => Make_Integer_Literal (Loc, 1))),
3524 Right_Opnd => X_Addr))));
3526 Analyze_And_Resolve (N, Standard_Boolean);
3527 end Overlaps_Storage;
3529 ------------
3530 -- Output --
3531 ------------
3533 when Attribute_Output => Output : declare
3534 P_Type : constant Entity_Id := Entity (Pref);
3535 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3536 Pname : Entity_Id;
3537 Decl : Node_Id;
3538 Prag : Node_Id;
3539 Arg3 : Node_Id;
3540 Wfunc : Node_Id;
3542 begin
3543 -- If no underlying type, we have an error that will be diagnosed
3544 -- elsewhere, so here we just completely ignore the expansion.
3546 if No (U_Type) then
3547 return;
3548 end if;
3550 -- If TSS for Output is present, just call it
3552 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
3554 if Present (Pname) then
3555 null;
3557 else
3558 -- If there is a Stream_Convert pragma, use it, we rewrite
3560 -- sourcetyp'Output (stream, Item)
3562 -- as
3564 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
3566 -- where strmwrite is the given Write function that converts an
3567 -- argument of type sourcetyp or a type acctyp, from which it is
3568 -- derived to type strmtyp. The conversion to acttyp is required
3569 -- for the derived case.
3571 Prag := Get_Stream_Convert_Pragma (P_Type);
3573 if Present (Prag) then
3574 Arg3 :=
3575 Next (Next (First (Pragma_Argument_Associations (Prag))));
3576 Wfunc := Entity (Expression (Arg3));
3578 Rewrite (N,
3579 Make_Attribute_Reference (Loc,
3580 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
3581 Attribute_Name => Name_Output,
3582 Expressions => New_List (
3583 Relocate_Node (First (Exprs)),
3584 Make_Function_Call (Loc,
3585 Name => New_Occurrence_Of (Wfunc, Loc),
3586 Parameter_Associations => New_List (
3587 OK_Convert_To (Etype (First_Formal (Wfunc)),
3588 Relocate_Node (Next (First (Exprs)))))))));
3590 Analyze (N);
3591 return;
3593 -- For elementary types, we call the W_xxx routine directly.
3594 -- Note that the effect of Write and Output is identical for
3595 -- the case of an elementary type, since there are no
3596 -- discriminants or bounds.
3598 elsif Is_Elementary_Type (U_Type) then
3600 -- A special case arises if we have a defined _Write routine,
3601 -- since in this case we are required to call this routine.
3603 if Present (TSS (Base_Type (U_Type), TSS_Stream_Write)) then
3604 Build_Record_Or_Elementary_Output_Procedure
3605 (Loc, U_Type, Decl, Pname);
3606 Insert_Action (N, Decl);
3608 -- For normal cases, we call the W_xxx routine directly
3610 else
3611 Rewrite (N, Build_Elementary_Write_Call (N));
3612 Analyze (N);
3613 return;
3614 end if;
3616 -- Array type case
3618 elsif Is_Array_Type (U_Type) then
3619 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
3620 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3622 -- Class-wide case, first output external tag, then dispatch
3623 -- to the appropriate primitive Output function (RM 13.13.2(31)).
3625 elsif Is_Class_Wide_Type (P_Type) then
3627 -- No need to do anything else compiling under restriction
3628 -- No_Dispatching_Calls. During the semantic analysis we
3629 -- already notified such violation.
3631 if Restriction_Active (No_Dispatching_Calls) then
3632 return;
3633 end if;
3635 Tag_Write : declare
3636 Strm : constant Node_Id := First (Exprs);
3637 Item : constant Node_Id := Next (Strm);
3639 begin
3640 -- Ada 2005 (AI-344): Check that the accessibility level
3641 -- of the type of the output object is not deeper than
3642 -- that of the attribute's prefix type.
3644 -- if Get_Access_Level (Item'Tag)
3645 -- /= Get_Access_Level (P_Type'Tag)
3646 -- then
3647 -- raise Tag_Error;
3648 -- end if;
3650 -- String'Output (Strm, External_Tag (Item'Tag));
3652 -- We cannot figure out a practical way to implement this
3653 -- accessibility check on virtual machines, so we omit it.
3655 if Ada_Version >= Ada_2005
3656 and then Tagged_Type_Expansion
3657 then
3658 Insert_Action (N,
3659 Make_Implicit_If_Statement (N,
3660 Condition =>
3661 Make_Op_Ne (Loc,
3662 Left_Opnd =>
3663 Build_Get_Access_Level (Loc,
3664 Make_Attribute_Reference (Loc,
3665 Prefix =>
3666 Relocate_Node (
3667 Duplicate_Subexpr (Item,
3668 Name_Req => True)),
3669 Attribute_Name => Name_Tag)),
3671 Right_Opnd =>
3672 Make_Integer_Literal (Loc,
3673 Type_Access_Level (P_Type))),
3675 Then_Statements =>
3676 New_List (Make_Raise_Statement (Loc,
3677 New_Occurrence_Of (
3678 RTE (RE_Tag_Error), Loc)))));
3679 end if;
3681 Insert_Action (N,
3682 Make_Attribute_Reference (Loc,
3683 Prefix => New_Occurrence_Of (Standard_String, Loc),
3684 Attribute_Name => Name_Output,
3685 Expressions => New_List (
3686 Relocate_Node (Duplicate_Subexpr (Strm)),
3687 Make_Function_Call (Loc,
3688 Name =>
3689 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
3690 Parameter_Associations => New_List (
3691 Make_Attribute_Reference (Loc,
3692 Prefix =>
3693 Relocate_Node
3694 (Duplicate_Subexpr (Item, Name_Req => True)),
3695 Attribute_Name => Name_Tag))))));
3696 end Tag_Write;
3698 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3700 -- Tagged type case, use the primitive Output function
3702 elsif Is_Tagged_Type (U_Type) then
3703 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3705 -- All other record type cases, including protected records.
3706 -- The latter only arise for expander generated code for
3707 -- handling shared passive partition access.
3709 else
3710 pragma Assert
3711 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3713 -- Ada 2005 (AI-216): Program_Error is raised when executing
3714 -- the default implementation of the Output attribute of an
3715 -- unchecked union type if the type lacks default discriminant
3716 -- values.
3718 if Is_Unchecked_Union (Base_Type (U_Type))
3719 and then No (Discriminant_Constraint (U_Type))
3720 then
3721 Insert_Action (N,
3722 Make_Raise_Program_Error (Loc,
3723 Reason => PE_Unchecked_Union_Restriction));
3725 return;
3726 end if;
3728 Build_Record_Or_Elementary_Output_Procedure
3729 (Loc, Base_Type (U_Type), Decl, Pname);
3730 Insert_Action (N, Decl);
3731 end if;
3732 end if;
3734 -- If we fall through, Pname is the name of the procedure to call
3736 Rewrite_Stream_Proc_Call (Pname);
3737 end Output;
3739 ---------
3740 -- Pos --
3741 ---------
3743 -- For enumeration types with a standard representation, Pos is
3744 -- handled by the back end.
3746 -- For enumeration types, with a non-standard representation we generate
3747 -- a call to the _Rep_To_Pos function created when the type was frozen.
3748 -- The call has the form
3750 -- _rep_to_pos (expr, flag)
3752 -- The parameter flag is True if range checks are enabled, causing
3753 -- Program_Error to be raised if the expression has an invalid
3754 -- representation, and False if range checks are suppressed.
3756 -- For integer types, Pos is equivalent to a simple integer
3757 -- conversion and we rewrite it as such
3759 when Attribute_Pos => Pos :
3760 declare
3761 Etyp : Entity_Id := Base_Type (Entity (Pref));
3763 begin
3764 -- Deal with zero/non-zero boolean values
3766 if Is_Boolean_Type (Etyp) then
3767 Adjust_Condition (First (Exprs));
3768 Etyp := Standard_Boolean;
3769 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
3770 end if;
3772 -- Case of enumeration type
3774 if Is_Enumeration_Type (Etyp) then
3776 -- Non-standard enumeration type (generate call)
3778 if Present (Enum_Pos_To_Rep (Etyp)) then
3779 Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
3780 Rewrite (N,
3781 Convert_To (Typ,
3782 Make_Function_Call (Loc,
3783 Name =>
3784 New_Reference_To (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3785 Parameter_Associations => Exprs)));
3787 Analyze_And_Resolve (N, Typ);
3789 -- Standard enumeration type (do universal integer check)
3791 else
3792 Apply_Universal_Integer_Attribute_Checks (N);
3793 end if;
3795 -- Deal with integer types (replace by conversion)
3797 elsif Is_Integer_Type (Etyp) then
3798 Rewrite (N, Convert_To (Typ, First (Exprs)));
3799 Analyze_And_Resolve (N, Typ);
3800 end if;
3802 end Pos;
3804 --------------
3805 -- Position --
3806 --------------
3808 -- We compute this if a component clause was present, otherwise we leave
3809 -- the computation up to the back end, since we don't know what layout
3810 -- will be chosen.
3812 when Attribute_Position => Position_Attr :
3813 declare
3814 CE : constant Entity_Id := Entity (Selector_Name (Pref));
3816 begin
3817 if Present (Component_Clause (CE)) then
3819 -- In Ada 2005 (or later) if we have the standard nondefault
3820 -- bit order, then we return the original value as given in
3821 -- the component clause (RM 2005 13.5.2(2/2)).
3823 if Ada_Version >= Ada_2005
3824 and then not Reverse_Bit_Order (Scope (CE))
3825 then
3826 Rewrite (N,
3827 Make_Integer_Literal (Loc,
3828 Intval => Expr_Value (Position (Component_Clause (CE)))));
3830 -- Otherwise (Ada 83 or 95, or reverse bit order specified in
3831 -- later Ada version), return the normalized value.
3833 else
3834 Rewrite (N,
3835 Make_Integer_Literal (Loc,
3836 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
3837 end if;
3839 Analyze_And_Resolve (N, Typ);
3841 -- If back end is doing things, just apply universal integer checks
3843 else
3844 Apply_Universal_Integer_Attribute_Checks (N);
3845 end if;
3846 end Position_Attr;
3848 ----------
3849 -- Pred --
3850 ----------
3852 -- 1. Deal with enumeration types with holes
3853 -- 2. For floating-point, generate call to attribute function
3854 -- 3. For other cases, deal with constraint checking
3856 when Attribute_Pred => Pred :
3857 declare
3858 Etyp : constant Entity_Id := Base_Type (Ptyp);
3860 begin
3862 -- For enumeration types with non-standard representations, we
3863 -- expand typ'Pred (x) into
3865 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
3867 -- If the representation is contiguous, we compute instead
3868 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
3869 -- The conversion function Enum_Pos_To_Rep is defined on the
3870 -- base type, not the subtype, so we have to use the base type
3871 -- explicitly for this and other enumeration attributes.
3873 if Is_Enumeration_Type (Ptyp)
3874 and then Present (Enum_Pos_To_Rep (Etyp))
3875 then
3876 if Has_Contiguous_Rep (Etyp) then
3877 Rewrite (N,
3878 Unchecked_Convert_To (Ptyp,
3879 Make_Op_Add (Loc,
3880 Left_Opnd =>
3881 Make_Integer_Literal (Loc,
3882 Enumeration_Rep (First_Literal (Ptyp))),
3883 Right_Opnd =>
3884 Make_Function_Call (Loc,
3885 Name =>
3886 New_Reference_To
3887 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3889 Parameter_Associations =>
3890 New_List (
3891 Unchecked_Convert_To (Ptyp,
3892 Make_Op_Subtract (Loc,
3893 Left_Opnd =>
3894 Unchecked_Convert_To (Standard_Integer,
3895 Relocate_Node (First (Exprs))),
3896 Right_Opnd =>
3897 Make_Integer_Literal (Loc, 1))),
3898 Rep_To_Pos_Flag (Ptyp, Loc))))));
3900 else
3901 -- Add Boolean parameter True, to request program errror if
3902 -- we have a bad representation on our hands. If checks are
3903 -- suppressed, then add False instead
3905 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
3906 Rewrite (N,
3907 Make_Indexed_Component (Loc,
3908 Prefix =>
3909 New_Reference_To
3910 (Enum_Pos_To_Rep (Etyp), Loc),
3911 Expressions => New_List (
3912 Make_Op_Subtract (Loc,
3913 Left_Opnd =>
3914 Make_Function_Call (Loc,
3915 Name =>
3916 New_Reference_To
3917 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3918 Parameter_Associations => Exprs),
3919 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3920 end if;
3922 Analyze_And_Resolve (N, Typ);
3924 -- For floating-point, we transform 'Pred into a call to the Pred
3925 -- floating-point attribute function in Fat_xxx (xxx is root type)
3927 elsif Is_Floating_Point_Type (Ptyp) then
3928 Expand_Fpt_Attribute_R (N);
3929 Analyze_And_Resolve (N, Typ);
3931 -- For modular types, nothing to do (no overflow, since wraps)
3933 elsif Is_Modular_Integer_Type (Ptyp) then
3934 null;
3936 -- For other types, if argument is marked as needing a range check or
3937 -- overflow checking is enabled, we must generate a check.
3939 elsif not Overflow_Checks_Suppressed (Ptyp)
3940 or else Do_Range_Check (First (Exprs))
3941 then
3942 Set_Do_Range_Check (First (Exprs), False);
3943 Expand_Pred_Succ (N);
3944 end if;
3945 end Pred;
3947 --------------
3948 -- Priority --
3949 --------------
3951 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3953 -- We rewrite X'Priority as the following run-time call:
3955 -- Get_Ceiling (X._Object)
3957 -- Note that although X'Priority is notionally an object, it is quite
3958 -- deliberately not defined as an aliased object in the RM. This means
3959 -- that it works fine to rewrite it as a call, without having to worry
3960 -- about complications that would other arise from X'Priority'Access,
3961 -- which is illegal, because of the lack of aliasing.
3963 when Attribute_Priority =>
3964 declare
3965 Call : Node_Id;
3966 Conctyp : Entity_Id;
3967 Object_Parm : Node_Id;
3968 Subprg : Entity_Id;
3969 RT_Subprg_Name : Node_Id;
3971 begin
3972 -- Look for the enclosing concurrent type
3974 Conctyp := Current_Scope;
3975 while not Is_Concurrent_Type (Conctyp) loop
3976 Conctyp := Scope (Conctyp);
3977 end loop;
3979 pragma Assert (Is_Protected_Type (Conctyp));
3981 -- Generate the actual of the call
3983 Subprg := Current_Scope;
3984 while not Present (Protected_Body_Subprogram (Subprg)) loop
3985 Subprg := Scope (Subprg);
3986 end loop;
3988 -- Use of 'Priority inside protected entries and barriers (in
3989 -- both cases the type of the first formal of their expanded
3990 -- subprogram is Address)
3992 if Etype (First_Entity (Protected_Body_Subprogram (Subprg)))
3993 = RTE (RE_Address)
3994 then
3995 declare
3996 New_Itype : Entity_Id;
3998 begin
3999 -- In the expansion of protected entries the type of the
4000 -- first formal of the Protected_Body_Subprogram is an
4001 -- Address. In order to reference the _object component
4002 -- we generate:
4004 -- type T is access p__ptTV;
4005 -- freeze T []
4007 New_Itype := Create_Itype (E_Access_Type, N);
4008 Set_Etype (New_Itype, New_Itype);
4009 Set_Directly_Designated_Type (New_Itype,
4010 Corresponding_Record_Type (Conctyp));
4011 Freeze_Itype (New_Itype, N);
4013 -- Generate:
4014 -- T!(O)._object'unchecked_access
4016 Object_Parm :=
4017 Make_Attribute_Reference (Loc,
4018 Prefix =>
4019 Make_Selected_Component (Loc,
4020 Prefix =>
4021 Unchecked_Convert_To (New_Itype,
4022 New_Reference_To
4023 (First_Entity
4024 (Protected_Body_Subprogram (Subprg)),
4025 Loc)),
4026 Selector_Name =>
4027 Make_Identifier (Loc, Name_uObject)),
4028 Attribute_Name => Name_Unchecked_Access);
4029 end;
4031 -- Use of 'Priority inside a protected subprogram
4033 else
4034 Object_Parm :=
4035 Make_Attribute_Reference (Loc,
4036 Prefix =>
4037 Make_Selected_Component (Loc,
4038 Prefix => New_Reference_To
4039 (First_Entity
4040 (Protected_Body_Subprogram (Subprg)),
4041 Loc),
4042 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4043 Attribute_Name => Name_Unchecked_Access);
4044 end if;
4046 -- Select the appropriate run-time subprogram
4048 if Number_Entries (Conctyp) = 0 then
4049 RT_Subprg_Name :=
4050 New_Reference_To (RTE (RE_Get_Ceiling), Loc);
4051 else
4052 RT_Subprg_Name :=
4053 New_Reference_To (RTE (RO_PE_Get_Ceiling), Loc);
4054 end if;
4056 Call :=
4057 Make_Function_Call (Loc,
4058 Name => RT_Subprg_Name,
4059 Parameter_Associations => New_List (Object_Parm));
4061 Rewrite (N, Call);
4063 -- Avoid the generation of extra checks on the pointer to the
4064 -- protected object.
4066 Analyze_And_Resolve (N, Typ, Suppress => Access_Check);
4067 end;
4069 ------------------
4070 -- Range_Length --
4071 ------------------
4073 when Attribute_Range_Length => Range_Length : begin
4075 -- The only special processing required is for the case where
4076 -- Range_Length is applied to an enumeration type with holes.
4077 -- In this case we transform
4079 -- X'Range_Length
4081 -- to
4083 -- X'Pos (X'Last) - X'Pos (X'First) + 1
4085 -- So that the result reflects the proper Pos values instead
4086 -- of the underlying representations.
4088 if Is_Enumeration_Type (Ptyp)
4089 and then Has_Non_Standard_Rep (Ptyp)
4090 then
4091 Rewrite (N,
4092 Make_Op_Add (Loc,
4093 Left_Opnd =>
4094 Make_Op_Subtract (Loc,
4095 Left_Opnd =>
4096 Make_Attribute_Reference (Loc,
4097 Attribute_Name => Name_Pos,
4098 Prefix => New_Occurrence_Of (Ptyp, Loc),
4099 Expressions => New_List (
4100 Make_Attribute_Reference (Loc,
4101 Attribute_Name => Name_Last,
4102 Prefix => New_Occurrence_Of (Ptyp, Loc)))),
4104 Right_Opnd =>
4105 Make_Attribute_Reference (Loc,
4106 Attribute_Name => Name_Pos,
4107 Prefix => New_Occurrence_Of (Ptyp, Loc),
4108 Expressions => New_List (
4109 Make_Attribute_Reference (Loc,
4110 Attribute_Name => Name_First,
4111 Prefix => New_Occurrence_Of (Ptyp, Loc))))),
4113 Right_Opnd => Make_Integer_Literal (Loc, 1)));
4115 Analyze_And_Resolve (N, Typ);
4117 -- For all other cases, the attribute is handled by the back end, but
4118 -- we need to deal with the case of the range check on a universal
4119 -- integer.
4121 else
4122 Apply_Universal_Integer_Attribute_Checks (N);
4123 end if;
4124 end Range_Length;
4126 ----------
4127 -- Read --
4128 ----------
4130 when Attribute_Read => Read : declare
4131 P_Type : constant Entity_Id := Entity (Pref);
4132 B_Type : constant Entity_Id := Base_Type (P_Type);
4133 U_Type : constant Entity_Id := Underlying_Type (P_Type);
4134 Pname : Entity_Id;
4135 Decl : Node_Id;
4136 Prag : Node_Id;
4137 Arg2 : Node_Id;
4138 Rfunc : Node_Id;
4139 Lhs : Node_Id;
4140 Rhs : Node_Id;
4142 begin
4143 -- If no underlying type, we have an error that will be diagnosed
4144 -- elsewhere, so here we just completely ignore the expansion.
4146 if No (U_Type) then
4147 return;
4148 end if;
4150 -- The simple case, if there is a TSS for Read, just call it
4152 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
4154 if Present (Pname) then
4155 null;
4157 else
4158 -- If there is a Stream_Convert pragma, use it, we rewrite
4160 -- sourcetyp'Read (stream, Item)
4162 -- as
4164 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
4166 -- where strmread is the given Read function that converts an
4167 -- argument of type strmtyp to type sourcetyp or a type from which
4168 -- it is derived. The conversion to sourcetyp is required in the
4169 -- latter case.
4171 -- A special case arises if Item is a type conversion in which
4172 -- case, we have to expand to:
4174 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
4176 -- where Itemx is the expression of the type conversion (i.e.
4177 -- the actual object), and typex is the type of Itemx.
4179 Prag := Get_Stream_Convert_Pragma (P_Type);
4181 if Present (Prag) then
4182 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
4183 Rfunc := Entity (Expression (Arg2));
4184 Lhs := Relocate_Node (Next (First (Exprs)));
4185 Rhs :=
4186 OK_Convert_To (B_Type,
4187 Make_Function_Call (Loc,
4188 Name => New_Occurrence_Of (Rfunc, Loc),
4189 Parameter_Associations => New_List (
4190 Make_Attribute_Reference (Loc,
4191 Prefix =>
4192 New_Occurrence_Of
4193 (Etype (First_Formal (Rfunc)), Loc),
4194 Attribute_Name => Name_Input,
4195 Expressions => New_List (
4196 Relocate_Node (First (Exprs)))))));
4198 if Nkind (Lhs) = N_Type_Conversion then
4199 Lhs := Expression (Lhs);
4200 Rhs := Convert_To (Etype (Lhs), Rhs);
4201 end if;
4203 Rewrite (N,
4204 Make_Assignment_Statement (Loc,
4205 Name => Lhs,
4206 Expression => Rhs));
4207 Set_Assignment_OK (Lhs);
4208 Analyze (N);
4209 return;
4211 -- For elementary types, we call the I_xxx routine using the first
4212 -- parameter and then assign the result into the second parameter.
4213 -- We set Assignment_OK to deal with the conversion case.
4215 elsif Is_Elementary_Type (U_Type) then
4216 declare
4217 Lhs : Node_Id;
4218 Rhs : Node_Id;
4220 begin
4221 Lhs := Relocate_Node (Next (First (Exprs)));
4222 Rhs := Build_Elementary_Input_Call (N);
4224 if Nkind (Lhs) = N_Type_Conversion then
4225 Lhs := Expression (Lhs);
4226 Rhs := Convert_To (Etype (Lhs), Rhs);
4227 end if;
4229 Set_Assignment_OK (Lhs);
4231 Rewrite (N,
4232 Make_Assignment_Statement (Loc,
4233 Name => Lhs,
4234 Expression => Rhs));
4236 Analyze (N);
4237 return;
4238 end;
4240 -- Array type case
4242 elsif Is_Array_Type (U_Type) then
4243 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
4244 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
4246 -- Tagged type case, use the primitive Read function. Note that
4247 -- this will dispatch in the class-wide case which is what we want
4249 elsif Is_Tagged_Type (U_Type) then
4250 Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
4252 -- All other record type cases, including protected records. The
4253 -- latter only arise for expander generated code for handling
4254 -- shared passive partition access.
4256 else
4257 pragma Assert
4258 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
4260 -- Ada 2005 (AI-216): Program_Error is raised when executing
4261 -- the default implementation of the Read attribute of an
4262 -- Unchecked_Union type.
4264 if Is_Unchecked_Union (Base_Type (U_Type)) then
4265 Insert_Action (N,
4266 Make_Raise_Program_Error (Loc,
4267 Reason => PE_Unchecked_Union_Restriction));
4268 end if;
4270 if Has_Discriminants (U_Type)
4271 and then Present
4272 (Discriminant_Default_Value (First_Discriminant (U_Type)))
4273 then
4274 Build_Mutable_Record_Read_Procedure
4275 (Loc, Full_Base (U_Type), Decl, Pname);
4276 else
4277 Build_Record_Read_Procedure
4278 (Loc, Full_Base (U_Type), Decl, Pname);
4279 end if;
4281 -- Suppress checks, uninitialized or otherwise invalid
4282 -- data does not cause constraint errors to be raised for
4283 -- a complete record read.
4285 Insert_Action (N, Decl, All_Checks);
4286 end if;
4287 end if;
4289 Rewrite_Stream_Proc_Call (Pname);
4290 end Read;
4292 ---------
4293 -- Ref --
4294 ---------
4296 -- Ref is identical to To_Address, see To_Address for processing
4298 ---------------
4299 -- Remainder --
4300 ---------------
4302 -- Transforms 'Remainder into a call to the floating-point attribute
4303 -- function Remainder in Fat_xxx (where xxx is the root type)
4305 when Attribute_Remainder =>
4306 Expand_Fpt_Attribute_RR (N);
4308 ------------
4309 -- Result --
4310 ------------
4312 -- Transform 'Result into reference to _Result formal. At the point
4313 -- where a legal 'Result attribute is expanded, we know that we are in
4314 -- the context of a _Postcondition function with a _Result parameter.
4316 when Attribute_Result =>
4317 Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult));
4318 Analyze_And_Resolve (N, Typ);
4320 -----------
4321 -- Round --
4322 -----------
4324 -- The handling of the Round attribute is quite delicate. The processing
4325 -- in Sem_Attr introduced a conversion to universal real, reflecting the
4326 -- semantics of Round, but we do not want anything to do with universal
4327 -- real at runtime, since this corresponds to using floating-point
4328 -- arithmetic.
4330 -- What we have now is that the Etype of the Round attribute correctly
4331 -- indicates the final result type. The operand of the Round is the
4332 -- conversion to universal real, described above, and the operand of
4333 -- this conversion is the actual operand of Round, which may be the
4334 -- special case of a fixed point multiplication or division (Etype =
4335 -- universal fixed)
4337 -- The exapander will expand first the operand of the conversion, then
4338 -- the conversion, and finally the round attribute itself, since we
4339 -- always work inside out. But we cannot simply process naively in this
4340 -- order. In the semantic world where universal fixed and real really
4341 -- exist and have infinite precision, there is no problem, but in the
4342 -- implementation world, where universal real is a floating-point type,
4343 -- we would get the wrong result.
4345 -- So the approach is as follows. First, when expanding a multiply or
4346 -- divide whose type is universal fixed, we do nothing at all, instead
4347 -- deferring the operation till later.
4349 -- The actual processing is done in Expand_N_Type_Conversion which
4350 -- handles the special case of Round by looking at its parent to see if
4351 -- it is a Round attribute, and if it is, handling the conversion (or
4352 -- its fixed multiply/divide child) in an appropriate manner.
4354 -- This means that by the time we get to expanding the Round attribute
4355 -- itself, the Round is nothing more than a type conversion (and will
4356 -- often be a null type conversion), so we just replace it with the
4357 -- appropriate conversion operation.
4359 when Attribute_Round =>
4360 Rewrite (N,
4361 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
4362 Analyze_And_Resolve (N);
4364 --------------
4365 -- Rounding --
4366 --------------
4368 -- Transforms 'Rounding into a call to the floating-point attribute
4369 -- function Rounding in Fat_xxx (where xxx is the root type)
4371 when Attribute_Rounding =>
4372 Expand_Fpt_Attribute_R (N);
4374 ------------------
4375 -- Same_Storage --
4376 ------------------
4378 when Attribute_Same_Storage => Same_Storage : declare
4379 Loc : constant Source_Ptr := Sloc (N);
4381 X : constant Node_Id := Prefix (N);
4382 Y : constant Node_Id := First (Expressions (N));
4383 -- The arguments
4385 X_Addr, Y_Addr : Node_Id;
4386 -- Rhe expressions for their addresses
4388 X_Size, Y_Size : Node_Id;
4389 -- Rhe expressions for their sizes
4391 begin
4392 -- The attribute is expanded as:
4394 -- (X'address = Y'address)
4395 -- and then (X'Size = Y'Size)
4397 -- If both arguments have the same Etype the second conjunct can be
4398 -- omitted.
4400 X_Addr :=
4401 Make_Attribute_Reference (Loc,
4402 Attribute_Name => Name_Address,
4403 Prefix => New_Copy_Tree (X));
4405 Y_Addr :=
4406 Make_Attribute_Reference (Loc,
4407 Attribute_Name => Name_Address,
4408 Prefix => New_Copy_Tree (Y));
4410 X_Size :=
4411 Make_Attribute_Reference (Loc,
4412 Attribute_Name => Name_Size,
4413 Prefix => New_Copy_Tree (X));
4415 Y_Size :=
4416 Make_Attribute_Reference (Loc,
4417 Attribute_Name => Name_Size,
4418 Prefix => New_Copy_Tree (Y));
4420 if Etype (X) = Etype (Y) then
4421 Rewrite (N,
4422 (Make_Op_Eq (Loc,
4423 Left_Opnd => X_Addr,
4424 Right_Opnd => Y_Addr)));
4425 else
4426 Rewrite (N,
4427 Make_Op_And (Loc,
4428 Left_Opnd =>
4429 Make_Op_Eq (Loc,
4430 Left_Opnd => X_Addr,
4431 Right_Opnd => Y_Addr),
4432 Right_Opnd =>
4433 Make_Op_Eq (Loc,
4434 Left_Opnd => X_Size,
4435 Right_Opnd => Y_Size)));
4436 end if;
4438 Analyze_And_Resolve (N, Standard_Boolean);
4439 end Same_Storage;
4441 -------------
4442 -- Scaling --
4443 -------------
4445 -- Transforms 'Scaling into a call to the floating-point attribute
4446 -- function Scaling in Fat_xxx (where xxx is the root type)
4448 when Attribute_Scaling =>
4449 Expand_Fpt_Attribute_RI (N);
4451 -------------------------
4452 -- Simple_Storage_Pool --
4453 -------------------------
4455 when Attribute_Simple_Storage_Pool =>
4456 Rewrite (N,
4457 Make_Type_Conversion (Loc,
4458 Subtype_Mark => New_Reference_To (Etype (N), Loc),
4459 Expression => New_Reference_To (Entity (N), Loc)));
4460 Analyze_And_Resolve (N, Typ);
4462 ----------
4463 -- Size --
4464 ----------
4466 when Attribute_Size |
4467 Attribute_Object_Size |
4468 Attribute_Value_Size |
4469 Attribute_VADS_Size => Size :
4471 declare
4472 Siz : Uint;
4473 New_Node : Node_Id;
4475 begin
4476 -- Processing for VADS_Size case. Note that this processing removes
4477 -- all traces of VADS_Size from the tree, and completes all required
4478 -- processing for VADS_Size by translating the attribute reference
4479 -- to an appropriate Size or Object_Size reference.
4481 if Id = Attribute_VADS_Size
4482 or else (Use_VADS_Size and then Id = Attribute_Size)
4483 then
4484 -- If the size is specified, then we simply use the specified
4485 -- size. This applies to both types and objects. The size of an
4486 -- object can be specified in the following ways:
4488 -- An explicit size object is given for an object
4489 -- A component size is specified for an indexed component
4490 -- A component clause is specified for a selected component
4491 -- The object is a component of a packed composite object
4493 -- If the size is specified, then VADS_Size of an object
4495 if (Is_Entity_Name (Pref)
4496 and then Present (Size_Clause (Entity (Pref))))
4497 or else
4498 (Nkind (Pref) = N_Component_Clause
4499 and then (Present (Component_Clause
4500 (Entity (Selector_Name (Pref))))
4501 or else Is_Packed (Etype (Prefix (Pref)))))
4502 or else
4503 (Nkind (Pref) = N_Indexed_Component
4504 and then (Component_Size (Etype (Prefix (Pref))) /= 0
4505 or else Is_Packed (Etype (Prefix (Pref)))))
4506 then
4507 Set_Attribute_Name (N, Name_Size);
4509 -- Otherwise if we have an object rather than a type, then the
4510 -- VADS_Size attribute applies to the type of the object, rather
4511 -- than the object itself. This is one of the respects in which
4512 -- VADS_Size differs from Size.
4514 else
4515 if (not Is_Entity_Name (Pref)
4516 or else not Is_Type (Entity (Pref)))
4517 and then (Is_Scalar_Type (Ptyp) or else Is_Constrained (Ptyp))
4518 then
4519 Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc));
4520 end if;
4522 -- For a scalar type for which no size was explicitly given,
4523 -- VADS_Size means Object_Size. This is the other respect in
4524 -- which VADS_Size differs from Size.
4526 if Is_Scalar_Type (Ptyp) and then No (Size_Clause (Ptyp)) then
4527 Set_Attribute_Name (N, Name_Object_Size);
4529 -- In all other cases, Size and VADS_Size are the sane
4531 else
4532 Set_Attribute_Name (N, Name_Size);
4533 end if;
4534 end if;
4535 end if;
4537 -- For class-wide types, X'Class'Size is transformed into a direct
4538 -- reference to the Size of the class type, so that the back end does
4539 -- not have to deal with the X'Class'Size reference.
4541 if Is_Entity_Name (Pref)
4542 and then Is_Class_Wide_Type (Entity (Pref))
4543 then
4544 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
4545 return;
4547 -- For X'Size applied to an object of a class-wide type, transform
4548 -- X'Size into a call to the primitive operation _Size applied to X.
4550 elsif Is_Class_Wide_Type (Ptyp)
4551 or else (Id = Attribute_Size
4552 and then Is_Tagged_Type (Ptyp)
4553 and then Has_Unknown_Discriminants (Ptyp))
4554 then
4555 -- No need to do anything else compiling under restriction
4556 -- No_Dispatching_Calls. During the semantic analysis we
4557 -- already notified such violation.
4559 if Restriction_Active (No_Dispatching_Calls) then
4560 return;
4561 end if;
4563 New_Node :=
4564 Make_Function_Call (Loc,
4565 Name => New_Reference_To
4566 (Find_Prim_Op (Ptyp, Name_uSize), Loc),
4567 Parameter_Associations => New_List (Pref));
4569 if Typ /= Standard_Long_Long_Integer then
4571 -- The context is a specific integer type with which the
4572 -- original attribute was compatible. The function has a
4573 -- specific type as well, so to preserve the compatibility
4574 -- we must convert explicitly.
4576 New_Node := Convert_To (Typ, New_Node);
4577 end if;
4579 Rewrite (N, New_Node);
4580 Analyze_And_Resolve (N, Typ);
4581 return;
4583 -- Case of known RM_Size of a type
4585 elsif (Id = Attribute_Size or else Id = Attribute_Value_Size)
4586 and then Is_Entity_Name (Pref)
4587 and then Is_Type (Entity (Pref))
4588 and then Known_Static_RM_Size (Entity (Pref))
4589 then
4590 Siz := RM_Size (Entity (Pref));
4592 -- Case of known Esize of a type
4594 elsif Id = Attribute_Object_Size
4595 and then Is_Entity_Name (Pref)
4596 and then Is_Type (Entity (Pref))
4597 and then Known_Static_Esize (Entity (Pref))
4598 then
4599 Siz := Esize (Entity (Pref));
4601 -- Case of known size of object
4603 elsif Id = Attribute_Size
4604 and then Is_Entity_Name (Pref)
4605 and then Is_Object (Entity (Pref))
4606 and then Known_Esize (Entity (Pref))
4607 and then Known_Static_Esize (Entity (Pref))
4608 then
4609 Siz := Esize (Entity (Pref));
4611 -- For an array component, we can do Size in the front end
4612 -- if the component_size of the array is set.
4614 elsif Nkind (Pref) = N_Indexed_Component then
4615 Siz := Component_Size (Etype (Prefix (Pref)));
4617 -- For a record component, we can do Size in the front end if there
4618 -- is a component clause, or if the record is packed and the
4619 -- component's size is known at compile time.
4621 elsif Nkind (Pref) = N_Selected_Component then
4622 declare
4623 Rec : constant Entity_Id := Etype (Prefix (Pref));
4624 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
4626 begin
4627 if Present (Component_Clause (Comp)) then
4628 Siz := Esize (Comp);
4630 elsif Is_Packed (Rec) then
4631 Siz := RM_Size (Ptyp);
4633 else
4634 Apply_Universal_Integer_Attribute_Checks (N);
4635 return;
4636 end if;
4637 end;
4639 -- All other cases are handled by the back end
4641 else
4642 Apply_Universal_Integer_Attribute_Checks (N);
4644 -- If Size is applied to a formal parameter that is of a packed
4645 -- array subtype, then apply Size to the actual subtype.
4647 if Is_Entity_Name (Pref)
4648 and then Is_Formal (Entity (Pref))
4649 and then Is_Array_Type (Ptyp)
4650 and then Is_Packed (Ptyp)
4651 then
4652 Rewrite (N,
4653 Make_Attribute_Reference (Loc,
4654 Prefix =>
4655 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
4656 Attribute_Name => Name_Size));
4657 Analyze_And_Resolve (N, Typ);
4658 end if;
4660 -- If Size applies to a dereference of an access to unconstrained
4661 -- packed array, the back end needs to see its unconstrained
4662 -- nominal type, but also a hint to the actual constrained type.
4664 if Nkind (Pref) = N_Explicit_Dereference
4665 and then Is_Array_Type (Ptyp)
4666 and then not Is_Constrained (Ptyp)
4667 and then Is_Packed (Ptyp)
4668 then
4669 Set_Actual_Designated_Subtype (Pref,
4670 Get_Actual_Subtype (Pref));
4671 end if;
4673 return;
4674 end if;
4676 -- Common processing for record and array component case
4678 if Siz /= No_Uint and then Siz /= 0 then
4679 declare
4680 CS : constant Boolean := Comes_From_Source (N);
4682 begin
4683 Rewrite (N, Make_Integer_Literal (Loc, Siz));
4685 -- This integer literal is not a static expression. We do not
4686 -- call Analyze_And_Resolve here, because this would activate
4687 -- the circuit for deciding that a static value was out of
4688 -- range, and we don't want that.
4690 -- So just manually set the type, mark the expression as non-
4691 -- static, and then ensure that the result is checked properly
4692 -- if the attribute comes from source (if it was internally
4693 -- generated, we never need a constraint check).
4695 Set_Etype (N, Typ);
4696 Set_Is_Static_Expression (N, False);
4698 if CS then
4699 Apply_Constraint_Check (N, Typ);
4700 end if;
4701 end;
4702 end if;
4703 end Size;
4705 ------------------
4706 -- Storage_Pool --
4707 ------------------
4709 when Attribute_Storage_Pool =>
4710 Rewrite (N,
4711 Make_Type_Conversion (Loc,
4712 Subtype_Mark => New_Reference_To (Etype (N), Loc),
4713 Expression => New_Reference_To (Entity (N), Loc)));
4714 Analyze_And_Resolve (N, Typ);
4716 ------------------
4717 -- Storage_Size --
4718 ------------------
4720 when Attribute_Storage_Size => Storage_Size : declare
4721 Alloc_Op : Entity_Id := Empty;
4723 begin
4725 -- Access type case, always go to the root type
4727 -- The case of access types results in a value of zero for the case
4728 -- where no storage size attribute clause has been given. If a
4729 -- storage size has been given, then the attribute is converted
4730 -- to a reference to the variable used to hold this value.
4732 if Is_Access_Type (Ptyp) then
4733 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
4734 Rewrite (N,
4735 Make_Attribute_Reference (Loc,
4736 Prefix => New_Reference_To (Typ, Loc),
4737 Attribute_Name => Name_Max,
4738 Expressions => New_List (
4739 Make_Integer_Literal (Loc, 0),
4740 Convert_To (Typ,
4741 New_Reference_To
4742 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
4744 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
4746 -- If the access type is associated with a simple storage pool
4747 -- object, then attempt to locate the optional Storage_Size
4748 -- function of the simple storage pool type. If not found,
4749 -- then the result will default to zero.
4751 if Present (Get_Rep_Pragma (Root_Type (Ptyp),
4752 Name_Simple_Storage_Pool_Type))
4753 then
4754 declare
4755 Pool_Type : constant Entity_Id :=
4756 Base_Type (Etype (Entity (N)));
4758 begin
4759 Alloc_Op := Get_Name_Entity_Id (Name_Storage_Size);
4760 while Present (Alloc_Op) loop
4761 if Scope (Alloc_Op) = Scope (Pool_Type)
4762 and then Present (First_Formal (Alloc_Op))
4763 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4764 then
4765 exit;
4766 end if;
4768 Alloc_Op := Homonym (Alloc_Op);
4769 end loop;
4770 end;
4772 -- In the normal Storage_Pool case, retrieve the primitive
4773 -- function associated with the pool type.
4775 else
4776 Alloc_Op :=
4777 Find_Prim_Op
4778 (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
4779 Attribute_Name (N));
4780 end if;
4782 -- If Storage_Size wasn't found (can only occur in the simple
4783 -- storage pool case), then simply use zero for the result.
4785 if not Present (Alloc_Op) then
4786 Rewrite (N, Make_Integer_Literal (Loc, 0));
4788 -- Otherwise, rewrite the allocator as a call to pool type's
4789 -- Storage_Size function.
4791 else
4792 Rewrite (N,
4793 OK_Convert_To (Typ,
4794 Make_Function_Call (Loc,
4795 Name =>
4796 New_Reference_To (Alloc_Op, Loc),
4798 Parameter_Associations => New_List (
4799 New_Reference_To
4800 (Associated_Storage_Pool
4801 (Root_Type (Ptyp)), Loc)))));
4802 end if;
4804 else
4805 Rewrite (N, Make_Integer_Literal (Loc, 0));
4806 end if;
4808 Analyze_And_Resolve (N, Typ);
4810 -- For tasks, we retrieve the size directly from the TCB. The
4811 -- size may depend on a discriminant of the type, and therefore
4812 -- can be a per-object expression, so type-level information is
4813 -- not sufficient in general. There are four cases to consider:
4815 -- a) If the attribute appears within a task body, the designated
4816 -- TCB is obtained by a call to Self.
4818 -- b) If the prefix of the attribute is the name of a task object,
4819 -- the designated TCB is the one stored in the corresponding record.
4821 -- c) If the prefix is a task type, the size is obtained from the
4822 -- size variable created for each task type
4824 -- d) If no storage_size was specified for the type , there is no
4825 -- size variable, and the value is a system-specific default.
4827 else
4828 if In_Open_Scopes (Ptyp) then
4830 -- Storage_Size (Self)
4832 Rewrite (N,
4833 Convert_To (Typ,
4834 Make_Function_Call (Loc,
4835 Name =>
4836 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4837 Parameter_Associations =>
4838 New_List (
4839 Make_Function_Call (Loc,
4840 Name =>
4841 New_Reference_To (RTE (RE_Self), Loc))))));
4843 elsif not Is_Entity_Name (Pref)
4844 or else not Is_Type (Entity (Pref))
4845 then
4846 -- Storage_Size (Rec (Obj).Size)
4848 Rewrite (N,
4849 Convert_To (Typ,
4850 Make_Function_Call (Loc,
4851 Name =>
4852 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4853 Parameter_Associations =>
4854 New_List (
4855 Make_Selected_Component (Loc,
4856 Prefix =>
4857 Unchecked_Convert_To (
4858 Corresponding_Record_Type (Ptyp),
4859 New_Copy_Tree (Pref)),
4860 Selector_Name =>
4861 Make_Identifier (Loc, Name_uTask_Id))))));
4863 elsif Present (Storage_Size_Variable (Ptyp)) then
4865 -- Static storage size pragma given for type: retrieve value
4866 -- from its allocated storage variable.
4868 Rewrite (N,
4869 Convert_To (Typ,
4870 Make_Function_Call (Loc,
4871 Name => New_Occurrence_Of (
4872 RTE (RE_Adjust_Storage_Size), Loc),
4873 Parameter_Associations =>
4874 New_List (
4875 New_Reference_To (
4876 Storage_Size_Variable (Ptyp), Loc)))));
4877 else
4878 -- Get system default
4880 Rewrite (N,
4881 Convert_To (Typ,
4882 Make_Function_Call (Loc,
4883 Name =>
4884 New_Occurrence_Of (
4885 RTE (RE_Default_Stack_Size), Loc))));
4886 end if;
4888 Analyze_And_Resolve (N, Typ);
4889 end if;
4890 end Storage_Size;
4892 -----------------
4893 -- Stream_Size --
4894 -----------------
4896 when Attribute_Stream_Size =>
4897 Rewrite (N,
4898 Make_Integer_Literal (Loc, Intval => Get_Stream_Size (Ptyp)));
4899 Analyze_And_Resolve (N, Typ);
4901 ----------
4902 -- Succ --
4903 ----------
4905 -- 1. Deal with enumeration types with holes
4906 -- 2. For floating-point, generate call to attribute function
4907 -- 3. For other cases, deal with constraint checking
4909 when Attribute_Succ => Succ : declare
4910 Etyp : constant Entity_Id := Base_Type (Ptyp);
4912 begin
4914 -- For enumeration types with non-standard representations, we
4915 -- expand typ'Succ (x) into
4917 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
4919 -- If the representation is contiguous, we compute instead
4920 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
4922 if Is_Enumeration_Type (Ptyp)
4923 and then Present (Enum_Pos_To_Rep (Etyp))
4924 then
4925 if Has_Contiguous_Rep (Etyp) then
4926 Rewrite (N,
4927 Unchecked_Convert_To (Ptyp,
4928 Make_Op_Add (Loc,
4929 Left_Opnd =>
4930 Make_Integer_Literal (Loc,
4931 Enumeration_Rep (First_Literal (Ptyp))),
4932 Right_Opnd =>
4933 Make_Function_Call (Loc,
4934 Name =>
4935 New_Reference_To
4936 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4938 Parameter_Associations =>
4939 New_List (
4940 Unchecked_Convert_To (Ptyp,
4941 Make_Op_Add (Loc,
4942 Left_Opnd =>
4943 Unchecked_Convert_To (Standard_Integer,
4944 Relocate_Node (First (Exprs))),
4945 Right_Opnd =>
4946 Make_Integer_Literal (Loc, 1))),
4947 Rep_To_Pos_Flag (Ptyp, Loc))))));
4948 else
4949 -- Add Boolean parameter True, to request program errror if
4950 -- we have a bad representation on our hands. Add False if
4951 -- checks are suppressed.
4953 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
4954 Rewrite (N,
4955 Make_Indexed_Component (Loc,
4956 Prefix =>
4957 New_Reference_To
4958 (Enum_Pos_To_Rep (Etyp), Loc),
4959 Expressions => New_List (
4960 Make_Op_Add (Loc,
4961 Left_Opnd =>
4962 Make_Function_Call (Loc,
4963 Name =>
4964 New_Reference_To
4965 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4966 Parameter_Associations => Exprs),
4967 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
4968 end if;
4970 Analyze_And_Resolve (N, Typ);
4972 -- For floating-point, we transform 'Succ into a call to the Succ
4973 -- floating-point attribute function in Fat_xxx (xxx is root type)
4975 elsif Is_Floating_Point_Type (Ptyp) then
4976 Expand_Fpt_Attribute_R (N);
4977 Analyze_And_Resolve (N, Typ);
4979 -- For modular types, nothing to do (no overflow, since wraps)
4981 elsif Is_Modular_Integer_Type (Ptyp) then
4982 null;
4984 -- For other types, if argument is marked as needing a range check or
4985 -- overflow checking is enabled, we must generate a check.
4987 elsif not Overflow_Checks_Suppressed (Ptyp)
4988 or else Do_Range_Check (First (Exprs))
4989 then
4990 Set_Do_Range_Check (First (Exprs), False);
4991 Expand_Pred_Succ (N);
4992 end if;
4993 end Succ;
4995 ---------
4996 -- Tag --
4997 ---------
4999 -- Transforms X'Tag into a direct reference to the tag of X
5001 when Attribute_Tag => Tag : declare
5002 Ttyp : Entity_Id;
5003 Prefix_Is_Type : Boolean;
5005 begin
5006 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
5007 Ttyp := Entity (Pref);
5008 Prefix_Is_Type := True;
5009 else
5010 Ttyp := Ptyp;
5011 Prefix_Is_Type := False;
5012 end if;
5014 if Is_Class_Wide_Type (Ttyp) then
5015 Ttyp := Root_Type (Ttyp);
5016 end if;
5018 Ttyp := Underlying_Type (Ttyp);
5020 -- Ada 2005: The type may be a synchronized tagged type, in which
5021 -- case the tag information is stored in the corresponding record.
5023 if Is_Concurrent_Type (Ttyp) then
5024 Ttyp := Corresponding_Record_Type (Ttyp);
5025 end if;
5027 if Prefix_Is_Type then
5029 -- For VMs we leave the type attribute unexpanded because
5030 -- there's not a dispatching table to reference.
5032 if Tagged_Type_Expansion then
5033 Rewrite (N,
5034 Unchecked_Convert_To (RTE (RE_Tag),
5035 New_Reference_To
5036 (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc)));
5037 Analyze_And_Resolve (N, RTE (RE_Tag));
5038 end if;
5040 -- Ada 2005 (AI-251): The use of 'Tag in the sources always
5041 -- references the primary tag of the actual object. If 'Tag is
5042 -- applied to class-wide interface objects we generate code that
5043 -- displaces "this" to reference the base of the object.
5045 elsif Comes_From_Source (N)
5046 and then Is_Class_Wide_Type (Etype (Prefix (N)))
5047 and then Is_Interface (Etype (Prefix (N)))
5048 then
5049 -- Generate:
5050 -- (To_Tag_Ptr (Prefix'Address)).all
5052 -- Note that Prefix'Address is recursively expanded into a call
5053 -- to Base_Address (Obj.Tag)
5055 -- Not needed for VM targets, since all handled by the VM
5057 if Tagged_Type_Expansion then
5058 Rewrite (N,
5059 Make_Explicit_Dereference (Loc,
5060 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5061 Make_Attribute_Reference (Loc,
5062 Prefix => Relocate_Node (Pref),
5063 Attribute_Name => Name_Address))));
5064 Analyze_And_Resolve (N, RTE (RE_Tag));
5065 end if;
5067 else
5068 Rewrite (N,
5069 Make_Selected_Component (Loc,
5070 Prefix => Relocate_Node (Pref),
5071 Selector_Name =>
5072 New_Reference_To (First_Tag_Component (Ttyp), Loc)));
5073 Analyze_And_Resolve (N, RTE (RE_Tag));
5074 end if;
5075 end Tag;
5077 ----------------
5078 -- Terminated --
5079 ----------------
5081 -- Transforms 'Terminated attribute into a call to Terminated function
5083 when Attribute_Terminated => Terminated :
5084 begin
5085 -- The prefix of Terminated is of a task interface class-wide type.
5086 -- Generate:
5087 -- terminated (Task_Id (Pref._disp_get_task_id));
5089 if Ada_Version >= Ada_2005
5090 and then Ekind (Ptyp) = E_Class_Wide_Type
5091 and then Is_Interface (Ptyp)
5092 and then Is_Task_Interface (Ptyp)
5093 then
5094 Rewrite (N,
5095 Make_Function_Call (Loc,
5096 Name =>
5097 New_Reference_To (RTE (RE_Terminated), Loc),
5098 Parameter_Associations => New_List (
5099 Make_Unchecked_Type_Conversion (Loc,
5100 Subtype_Mark =>
5101 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
5102 Expression =>
5103 Make_Selected_Component (Loc,
5104 Prefix =>
5105 New_Copy_Tree (Pref),
5106 Selector_Name =>
5107 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
5109 elsif Restricted_Profile then
5110 Rewrite (N,
5111 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
5113 else
5114 Rewrite (N,
5115 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
5116 end if;
5118 Analyze_And_Resolve (N, Standard_Boolean);
5119 end Terminated;
5121 ----------------
5122 -- To_Address --
5123 ----------------
5125 -- Transforms System'To_Address (X) and System.Address'Ref (X) into
5126 -- unchecked conversion from (integral) type of X to type address.
5128 when Attribute_To_Address | Attribute_Ref =>
5129 Rewrite (N,
5130 Unchecked_Convert_To (RTE (RE_Address),
5131 Relocate_Node (First (Exprs))));
5132 Analyze_And_Resolve (N, RTE (RE_Address));
5134 ------------
5135 -- To_Any --
5136 ------------
5138 when Attribute_To_Any => To_Any : declare
5139 P_Type : constant Entity_Id := Etype (Pref);
5140 Decls : constant List_Id := New_List;
5141 begin
5142 Rewrite (N,
5143 Build_To_Any_Call
5144 (Convert_To (P_Type,
5145 Relocate_Node (First (Exprs))), Decls));
5146 Insert_Actions (N, Decls);
5147 Analyze_And_Resolve (N, RTE (RE_Any));
5148 end To_Any;
5150 ----------------
5151 -- Truncation --
5152 ----------------
5154 -- Transforms 'Truncation into a call to the floating-point attribute
5155 -- function Truncation in Fat_xxx (where xxx is the root type).
5156 -- Expansion is avoided for cases the back end can handle directly.
5158 when Attribute_Truncation =>
5159 if not Is_Inline_Floating_Point_Attribute (N) then
5160 Expand_Fpt_Attribute_R (N);
5161 end if;
5163 --------------
5164 -- TypeCode --
5165 --------------
5167 when Attribute_TypeCode => TypeCode : declare
5168 P_Type : constant Entity_Id := Etype (Pref);
5169 Decls : constant List_Id := New_List;
5170 begin
5171 Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls));
5172 Insert_Actions (N, Decls);
5173 Analyze_And_Resolve (N, RTE (RE_TypeCode));
5174 end TypeCode;
5176 -----------------------
5177 -- Unbiased_Rounding --
5178 -----------------------
5180 -- Transforms 'Unbiased_Rounding into a call to the floating-point
5181 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
5182 -- root type). Expansion is avoided for cases the back end can handle
5183 -- directly.
5185 when Attribute_Unbiased_Rounding =>
5186 if not Is_Inline_Floating_Point_Attribute (N) then
5187 Expand_Fpt_Attribute_R (N);
5188 end if;
5190 -----------------
5191 -- UET_Address --
5192 -----------------
5194 when Attribute_UET_Address => UET_Address : declare
5195 Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
5197 begin
5198 Insert_Action (N,
5199 Make_Object_Declaration (Loc,
5200 Defining_Identifier => Ent,
5201 Aliased_Present => True,
5202 Object_Definition =>
5203 New_Occurrence_Of (RTE (RE_Address), Loc)));
5205 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
5206 -- in normal external form.
5208 Get_External_Unit_Name_String (Get_Unit_Name (Pref));
5209 Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
5210 Name_Len := Name_Len + 7;
5211 Name_Buffer (1 .. 7) := "__gnat_";
5212 Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP";
5213 Name_Len := Name_Len + 5;
5215 Set_Is_Imported (Ent);
5216 Set_Interface_Name (Ent,
5217 Make_String_Literal (Loc,
5218 Strval => String_From_Name_Buffer));
5220 -- Set entity as internal to ensure proper Sprint output of its
5221 -- implicit importation.
5223 Set_Is_Internal (Ent);
5225 Rewrite (N,
5226 Make_Attribute_Reference (Loc,
5227 Prefix => New_Occurrence_Of (Ent, Loc),
5228 Attribute_Name => Name_Address));
5230 Analyze_And_Resolve (N, Typ);
5231 end UET_Address;
5233 ---------------
5234 -- VADS_Size --
5235 ---------------
5237 -- The processing for VADS_Size is shared with Size
5239 ---------
5240 -- Val --
5241 ---------
5243 -- For enumeration types with a standard representation, and for all
5244 -- other types, Val is handled by the back end. For enumeration types
5245 -- with a non-standard representation we use the _Pos_To_Rep array that
5246 -- was created when the type was frozen.
5248 when Attribute_Val => Val : declare
5249 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
5251 begin
5252 if Is_Enumeration_Type (Etyp)
5253 and then Present (Enum_Pos_To_Rep (Etyp))
5254 then
5255 if Has_Contiguous_Rep (Etyp) then
5256 declare
5257 Rep_Node : constant Node_Id :=
5258 Unchecked_Convert_To (Etyp,
5259 Make_Op_Add (Loc,
5260 Left_Opnd =>
5261 Make_Integer_Literal (Loc,
5262 Enumeration_Rep (First_Literal (Etyp))),
5263 Right_Opnd =>
5264 (Convert_To (Standard_Integer,
5265 Relocate_Node (First (Exprs))))));
5267 begin
5268 Rewrite (N,
5269 Unchecked_Convert_To (Etyp,
5270 Make_Op_Add (Loc,
5271 Left_Opnd =>
5272 Make_Integer_Literal (Loc,
5273 Enumeration_Rep (First_Literal (Etyp))),
5274 Right_Opnd =>
5275 Make_Function_Call (Loc,
5276 Name =>
5277 New_Reference_To
5278 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5279 Parameter_Associations => New_List (
5280 Rep_Node,
5281 Rep_To_Pos_Flag (Etyp, Loc))))));
5282 end;
5284 else
5285 Rewrite (N,
5286 Make_Indexed_Component (Loc,
5287 Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc),
5288 Expressions => New_List (
5289 Convert_To (Standard_Integer,
5290 Relocate_Node (First (Exprs))))));
5291 end if;
5293 Analyze_And_Resolve (N, Typ);
5295 -- If the argument is marked as requiring a range check then generate
5296 -- it here.
5298 elsif Do_Range_Check (First (Exprs)) then
5299 Set_Do_Range_Check (First (Exprs), False);
5300 Generate_Range_Check (First (Exprs), Etyp, CE_Range_Check_Failed);
5301 end if;
5302 end Val;
5304 -----------
5305 -- Valid --
5306 -----------
5308 -- The code for valid is dependent on the particular types involved.
5309 -- See separate sections below for the generated code in each case.
5311 when Attribute_Valid => Valid : declare
5312 Btyp : Entity_Id := Base_Type (Ptyp);
5313 Tst : Node_Id;
5315 Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
5316 -- Save the validity checking mode. We always turn off validity
5317 -- checking during process of 'Valid since this is one place
5318 -- where we do not want the implicit validity checks to intefere
5319 -- with the explicit validity check that the programmer is doing.
5321 function Make_Range_Test return Node_Id;
5322 -- Build the code for a range test of the form
5323 -- Btyp!(Pref) in Btyp!(Ptyp'First) .. Btyp!(Ptyp'Last)
5325 ---------------------
5326 -- Make_Range_Test --
5327 ---------------------
5329 function Make_Range_Test return Node_Id is
5330 Temp : constant Node_Id := Duplicate_Subexpr (Pref);
5332 begin
5333 -- The value whose validity is being checked has been captured in
5334 -- an object declaration. We certainly don't want this object to
5335 -- appear valid because the declaration initializes it!
5337 if Is_Entity_Name (Temp) then
5338 Set_Is_Known_Valid (Entity (Temp), False);
5339 end if;
5341 return
5342 Make_In (Loc,
5343 Left_Opnd =>
5344 Unchecked_Convert_To (Btyp, Temp),
5345 Right_Opnd =>
5346 Make_Range (Loc,
5347 Low_Bound =>
5348 Unchecked_Convert_To (Btyp,
5349 Make_Attribute_Reference (Loc,
5350 Prefix => New_Occurrence_Of (Ptyp, Loc),
5351 Attribute_Name => Name_First)),
5352 High_Bound =>
5353 Unchecked_Convert_To (Btyp,
5354 Make_Attribute_Reference (Loc,
5355 Prefix => New_Occurrence_Of (Ptyp, Loc),
5356 Attribute_Name => Name_Last))));
5357 end Make_Range_Test;
5359 -- Start of processing for Attribute_Valid
5361 begin
5362 -- Do not expand sourced code 'Valid reference in CodePeer mode,
5363 -- will be handled by the back-end directly.
5365 if CodePeer_Mode and then Comes_From_Source (N) then
5366 return;
5367 end if;
5369 -- Turn off validity checks. We do not want any implicit validity
5370 -- checks to intefere with the explicit check from the attribute
5372 Validity_Checks_On := False;
5374 -- Retrieve the base type. Handle the case where the base type is a
5375 -- private enumeration type.
5377 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
5378 Btyp := Full_View (Btyp);
5379 end if;
5381 -- Floating-point case. This case is handled by the Valid attribute
5382 -- code in the floating-point attribute run-time library.
5384 if Is_Floating_Point_Type (Ptyp) then
5385 declare
5386 Pkg : RE_Id;
5387 Ftp : Entity_Id;
5389 begin
5391 case Float_Rep (Btyp) is
5393 -- For vax fpt types, call appropriate routine in special
5394 -- vax floating point unit. No need to worry about loads in
5395 -- this case, since these types have no signalling NaN's.
5397 when VAX_Native => Expand_Vax_Valid (N);
5399 -- The AAMP back end handles Valid for floating-point types
5401 when AAMP =>
5402 Analyze_And_Resolve (Pref, Ptyp);
5403 Set_Etype (N, Standard_Boolean);
5404 Set_Analyzed (N);
5406 when IEEE_Binary =>
5407 Find_Fat_Info (Ptyp, Ftp, Pkg);
5409 -- If the floating-point object might be unaligned, we
5410 -- need to call the special routine Unaligned_Valid,
5411 -- which makes the needed copy, being careful not to
5412 -- load the value into any floating-point register.
5413 -- The argument in this case is obj'Address (see
5414 -- Unaligned_Valid routine in Fat_Gen).
5416 if Is_Possibly_Unaligned_Object (Pref) then
5417 Expand_Fpt_Attribute
5418 (N, Pkg, Name_Unaligned_Valid,
5419 New_List (
5420 Make_Attribute_Reference (Loc,
5421 Prefix => Relocate_Node (Pref),
5422 Attribute_Name => Name_Address)));
5424 -- In the normal case where we are sure the object is
5425 -- aligned, we generate a call to Valid, and the argument
5426 -- in this case is obj'Unrestricted_Access (after
5427 -- converting obj to the right floating-point type).
5429 else
5430 Expand_Fpt_Attribute
5431 (N, Pkg, Name_Valid,
5432 New_List (
5433 Make_Attribute_Reference (Loc,
5434 Prefix => Unchecked_Convert_To (Ftp, Pref),
5435 Attribute_Name => Name_Unrestricted_Access)));
5436 end if;
5437 end case;
5439 -- One more task, we still need a range check. Required
5440 -- only if we have a constraint, since the Valid routine
5441 -- catches infinities properly (infinities are never valid).
5443 -- The way we do the range check is simply to create the
5444 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
5446 if not Subtypes_Statically_Match (Ptyp, Btyp) then
5447 Rewrite (N,
5448 Make_And_Then (Loc,
5449 Left_Opnd => Relocate_Node (N),
5450 Right_Opnd =>
5451 Make_In (Loc,
5452 Left_Opnd => Convert_To (Btyp, Pref),
5453 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
5454 end if;
5455 end;
5457 -- Enumeration type with holes
5459 -- For enumeration types with holes, the Pos value constructed by
5460 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
5461 -- second argument of False returns minus one for an invalid value,
5462 -- and the non-negative pos value for a valid value, so the
5463 -- expansion of X'Valid is simply:
5465 -- type(X)'Pos (X) >= 0
5467 -- We can't quite generate it that way because of the requirement
5468 -- for the non-standard second argument of False in the resulting
5469 -- rep_to_pos call, so we have to explicitly create:
5471 -- _rep_to_pos (X, False) >= 0
5473 -- If we have an enumeration subtype, we also check that the
5474 -- value is in range:
5476 -- _rep_to_pos (X, False) >= 0
5477 -- and then
5478 -- (X >= type(X)'First and then type(X)'Last <= X)
5480 elsif Is_Enumeration_Type (Ptyp)
5481 and then Present (Enum_Pos_To_Rep (Btyp))
5482 then
5483 Tst :=
5484 Make_Op_Ge (Loc,
5485 Left_Opnd =>
5486 Make_Function_Call (Loc,
5487 Name =>
5488 New_Reference_To (TSS (Btyp, TSS_Rep_To_Pos), Loc),
5489 Parameter_Associations => New_List (
5490 Pref,
5491 New_Occurrence_Of (Standard_False, Loc))),
5492 Right_Opnd => Make_Integer_Literal (Loc, 0));
5494 if Ptyp /= Btyp
5495 and then
5496 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
5497 or else
5498 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
5499 then
5500 -- The call to Make_Range_Test will create declarations
5501 -- that need a proper insertion point, but Pref is now
5502 -- attached to a node with no ancestor. Attach to tree
5503 -- even if it is to be rewritten below.
5505 Set_Parent (Tst, Parent (N));
5507 Tst :=
5508 Make_And_Then (Loc,
5509 Left_Opnd => Make_Range_Test,
5510 Right_Opnd => Tst);
5511 end if;
5513 Rewrite (N, Tst);
5515 -- Fortran convention booleans
5517 -- For the very special case of Fortran convention booleans, the
5518 -- value is always valid, since it is an integer with the semantics
5519 -- that non-zero is true, and any value is permissible.
5521 elsif Is_Boolean_Type (Ptyp)
5522 and then Convention (Ptyp) = Convention_Fortran
5523 then
5524 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5526 -- For biased representations, we will be doing an unchecked
5527 -- conversion without unbiasing the result. That means that the range
5528 -- test has to take this into account, and the proper form of the
5529 -- test is:
5531 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
5533 elsif Has_Biased_Representation (Ptyp) then
5534 Btyp := RTE (RE_Unsigned_32);
5535 Rewrite (N,
5536 Make_Op_Lt (Loc,
5537 Left_Opnd =>
5538 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
5539 Right_Opnd =>
5540 Unchecked_Convert_To (Btyp,
5541 Make_Attribute_Reference (Loc,
5542 Prefix => New_Occurrence_Of (Ptyp, Loc),
5543 Attribute_Name => Name_Range_Length))));
5545 -- For all other scalar types, what we want logically is a
5546 -- range test:
5548 -- X in type(X)'First .. type(X)'Last
5550 -- But that's precisely what won't work because of possible
5551 -- unwanted optimization (and indeed the basic motivation for
5552 -- the Valid attribute is exactly that this test does not work!)
5553 -- What will work is:
5555 -- Btyp!(X) >= Btyp!(type(X)'First)
5556 -- and then
5557 -- Btyp!(X) <= Btyp!(type(X)'Last)
5559 -- where Btyp is an integer type large enough to cover the full
5560 -- range of possible stored values (i.e. it is chosen on the basis
5561 -- of the size of the type, not the range of the values). We write
5562 -- this as two tests, rather than a range check, so that static
5563 -- evaluation will easily remove either or both of the checks if
5564 -- they can be -statically determined to be true (this happens
5565 -- when the type of X is static and the range extends to the full
5566 -- range of stored values).
5568 -- Unsigned types. Note: it is safe to consider only whether the
5569 -- subtype is unsigned, since we will in that case be doing all
5570 -- unsigned comparisons based on the subtype range. Since we use the
5571 -- actual subtype object size, this is appropriate.
5573 -- For example, if we have
5575 -- subtype x is integer range 1 .. 200;
5576 -- for x'Object_Size use 8;
5578 -- Now the base type is signed, but objects of this type are bits
5579 -- unsigned, and doing an unsigned test of the range 1 to 200 is
5580 -- correct, even though a value greater than 127 looks signed to a
5581 -- signed comparison.
5583 elsif Is_Unsigned_Type (Ptyp) then
5584 if Esize (Ptyp) <= 32 then
5585 Btyp := RTE (RE_Unsigned_32);
5586 else
5587 Btyp := RTE (RE_Unsigned_64);
5588 end if;
5590 Rewrite (N, Make_Range_Test);
5592 -- Signed types
5594 else
5595 if Esize (Ptyp) <= Esize (Standard_Integer) then
5596 Btyp := Standard_Integer;
5597 else
5598 Btyp := Universal_Integer;
5599 end if;
5601 Rewrite (N, Make_Range_Test);
5602 end if;
5604 Analyze_And_Resolve (N, Standard_Boolean);
5605 Validity_Checks_On := Save_Validity_Checks_On;
5606 end Valid;
5608 -------------------
5609 -- Valid_Scalars --
5610 -------------------
5612 when Attribute_Valid_Scalars => Valid_Scalars : declare
5613 Ftyp : Entity_Id;
5615 begin
5616 if Present (Underlying_Type (Ptyp)) then
5617 Ftyp := Underlying_Type (Ptyp);
5618 else
5619 Ftyp := Ptyp;
5620 end if;
5622 -- For scalar types, Valid_Scalars is the same as Valid
5624 if Is_Scalar_Type (Ftyp) then
5625 Rewrite (N,
5626 Make_Attribute_Reference (Loc,
5627 Attribute_Name => Name_Valid,
5628 Prefix => Pref));
5629 Analyze_And_Resolve (N, Standard_Boolean);
5631 -- For array types, we construct a function that determines if there
5632 -- are any non-valid scalar subcomponents, and call the function.
5633 -- We only do this for arrays whose component type needs checking
5635 elsif Is_Array_Type (Ftyp)
5636 and then not No_Scalar_Parts (Component_Type (Ftyp))
5637 then
5638 Rewrite (N,
5639 Make_Function_Call (Loc,
5640 Name =>
5641 New_Occurrence_Of (Build_Array_VS_Func (Ftyp, N), Loc),
5642 Parameter_Associations => New_List (Pref)));
5644 Analyze_And_Resolve (N, Standard_Boolean);
5646 -- For record types, we build a big if expression, applying Valid or
5647 -- Valid_Scalars as appropriate to all relevant components.
5649 elsif (Is_Record_Type (Ptyp) or else Has_Discriminants (Ptyp))
5650 and then not No_Scalar_Parts (Ptyp)
5651 then
5652 declare
5653 C : Entity_Id;
5654 X : Node_Id;
5655 A : Name_Id;
5657 begin
5658 X := New_Occurrence_Of (Standard_True, Loc);
5659 C := First_Component_Or_Discriminant (Ptyp);
5660 while Present (C) loop
5661 if No_Scalar_Parts (Etype (C)) then
5662 goto Continue;
5663 elsif Is_Scalar_Type (Etype (C)) then
5664 A := Name_Valid;
5665 else
5666 A := Name_Valid_Scalars;
5667 end if;
5669 X :=
5670 Make_And_Then (Loc,
5671 Left_Opnd => X,
5672 Right_Opnd =>
5673 Make_Attribute_Reference (Loc,
5674 Attribute_Name => A,
5675 Prefix =>
5676 Make_Selected_Component (Loc,
5677 Prefix =>
5678 Duplicate_Subexpr (Pref, Name_Req => True),
5679 Selector_Name =>
5680 New_Occurrence_Of (C, Loc))));
5681 <<Continue>>
5682 Next_Component_Or_Discriminant (C);
5683 end loop;
5685 Rewrite (N, X);
5686 Analyze_And_Resolve (N, Standard_Boolean);
5687 end;
5689 -- For all other types, result is True (but not static)
5691 else
5692 Rewrite (N, New_Occurrence_Of (Standard_Boolean, Loc));
5693 Analyze_And_Resolve (N, Standard_Boolean);
5694 Set_Is_Static_Expression (N, False);
5695 end if;
5696 end Valid_Scalars;
5698 -----------
5699 -- Value --
5700 -----------
5702 -- Value attribute is handled in separate unit Exp_Imgv
5704 when Attribute_Value =>
5705 Exp_Imgv.Expand_Value_Attribute (N);
5707 -----------------
5708 -- Value_Size --
5709 -----------------
5711 -- The processing for Value_Size shares the processing for Size
5713 -------------
5714 -- Version --
5715 -------------
5717 -- The processing for Version shares the processing for Body_Version
5719 ----------------
5720 -- Wide_Image --
5721 ----------------
5723 -- Wide_Image attribute is handled in separate unit Exp_Imgv
5725 when Attribute_Wide_Image =>
5726 Exp_Imgv.Expand_Wide_Image_Attribute (N);
5728 ---------------------
5729 -- Wide_Wide_Image --
5730 ---------------------
5732 -- Wide_Wide_Image attribute is handled in separate unit Exp_Imgv
5734 when Attribute_Wide_Wide_Image =>
5735 Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N);
5737 ----------------
5738 -- Wide_Value --
5739 ----------------
5741 -- We expand typ'Wide_Value (X) into
5743 -- typ'Value
5744 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
5746 -- Wide_String_To_String is a runtime function that converts its wide
5747 -- string argument to String, converting any non-translatable characters
5748 -- into appropriate escape sequences. This preserves the required
5749 -- semantics of Wide_Value in all cases, and results in a very simple
5750 -- implementation approach.
5752 -- Note: for this approach to be fully standard compliant for the cases
5753 -- where typ is Wide_Character and Wide_Wide_Character, the encoding
5754 -- method must cover the entire character range (e.g. UTF-8). But that
5755 -- is a reasonable requirement when dealing with encoded character
5756 -- sequences. Presumably if one of the restrictive encoding mechanisms
5757 -- is in use such as Shift-JIS, then characters that cannot be
5758 -- represented using this encoding will not appear in any case.
5760 when Attribute_Wide_Value => Wide_Value :
5761 begin
5762 Rewrite (N,
5763 Make_Attribute_Reference (Loc,
5764 Prefix => Pref,
5765 Attribute_Name => Name_Value,
5767 Expressions => New_List (
5768 Make_Function_Call (Loc,
5769 Name =>
5770 New_Reference_To (RTE (RE_Wide_String_To_String), Loc),
5772 Parameter_Associations => New_List (
5773 Relocate_Node (First (Exprs)),
5774 Make_Integer_Literal (Loc,
5775 Intval => Int (Wide_Character_Encoding_Method)))))));
5777 Analyze_And_Resolve (N, Typ);
5778 end Wide_Value;
5780 ---------------------
5781 -- Wide_Wide_Value --
5782 ---------------------
5784 -- We expand typ'Wide_Value_Value (X) into
5786 -- typ'Value
5787 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
5789 -- Wide_Wide_String_To_String is a runtime function that converts its
5790 -- wide string argument to String, converting any non-translatable
5791 -- characters into appropriate escape sequences. This preserves the
5792 -- required semantics of Wide_Wide_Value in all cases, and results in a
5793 -- very simple implementation approach.
5795 -- It's not quite right where typ = Wide_Wide_Character, because the
5796 -- encoding method may not cover the whole character type ???
5798 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
5799 begin
5800 Rewrite (N,
5801 Make_Attribute_Reference (Loc,
5802 Prefix => Pref,
5803 Attribute_Name => Name_Value,
5805 Expressions => New_List (
5806 Make_Function_Call (Loc,
5807 Name =>
5808 New_Reference_To (RTE (RE_Wide_Wide_String_To_String), Loc),
5810 Parameter_Associations => New_List (
5811 Relocate_Node (First (Exprs)),
5812 Make_Integer_Literal (Loc,
5813 Intval => Int (Wide_Character_Encoding_Method)))))));
5815 Analyze_And_Resolve (N, Typ);
5816 end Wide_Wide_Value;
5818 ---------------------
5819 -- Wide_Wide_Width --
5820 ---------------------
5822 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
5824 when Attribute_Wide_Wide_Width =>
5825 Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide);
5827 ----------------
5828 -- Wide_Width --
5829 ----------------
5831 -- Wide_Width attribute is handled in separate unit Exp_Imgv
5833 when Attribute_Wide_Width =>
5834 Exp_Imgv.Expand_Width_Attribute (N, Wide);
5836 -----------
5837 -- Width --
5838 -----------
5840 -- Width attribute is handled in separate unit Exp_Imgv
5842 when Attribute_Width =>
5843 Exp_Imgv.Expand_Width_Attribute (N, Normal);
5845 -----------
5846 -- Write --
5847 -----------
5849 when Attribute_Write => Write : declare
5850 P_Type : constant Entity_Id := Entity (Pref);
5851 U_Type : constant Entity_Id := Underlying_Type (P_Type);
5852 Pname : Entity_Id;
5853 Decl : Node_Id;
5854 Prag : Node_Id;
5855 Arg3 : Node_Id;
5856 Wfunc : Node_Id;
5858 begin
5859 -- If no underlying type, we have an error that will be diagnosed
5860 -- elsewhere, so here we just completely ignore the expansion.
5862 if No (U_Type) then
5863 return;
5864 end if;
5866 -- The simple case, if there is a TSS for Write, just call it
5868 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
5870 if Present (Pname) then
5871 null;
5873 else
5874 -- If there is a Stream_Convert pragma, use it, we rewrite
5876 -- sourcetyp'Output (stream, Item)
5878 -- as
5880 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
5882 -- where strmwrite is the given Write function that converts an
5883 -- argument of type sourcetyp or a type acctyp, from which it is
5884 -- derived to type strmtyp. The conversion to acttyp is required
5885 -- for the derived case.
5887 Prag := Get_Stream_Convert_Pragma (P_Type);
5889 if Present (Prag) then
5890 Arg3 :=
5891 Next (Next (First (Pragma_Argument_Associations (Prag))));
5892 Wfunc := Entity (Expression (Arg3));
5894 Rewrite (N,
5895 Make_Attribute_Reference (Loc,
5896 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
5897 Attribute_Name => Name_Output,
5898 Expressions => New_List (
5899 Relocate_Node (First (Exprs)),
5900 Make_Function_Call (Loc,
5901 Name => New_Occurrence_Of (Wfunc, Loc),
5902 Parameter_Associations => New_List (
5903 OK_Convert_To (Etype (First_Formal (Wfunc)),
5904 Relocate_Node (Next (First (Exprs)))))))));
5906 Analyze (N);
5907 return;
5909 -- For elementary types, we call the W_xxx routine directly
5911 elsif Is_Elementary_Type (U_Type) then
5912 Rewrite (N, Build_Elementary_Write_Call (N));
5913 Analyze (N);
5914 return;
5916 -- Array type case
5918 elsif Is_Array_Type (U_Type) then
5919 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
5920 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5922 -- Tagged type case, use the primitive Write function. Note that
5923 -- this will dispatch in the class-wide case which is what we want
5925 elsif Is_Tagged_Type (U_Type) then
5926 Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
5928 -- All other record type cases, including protected records.
5929 -- The latter only arise for expander generated code for
5930 -- handling shared passive partition access.
5932 else
5933 pragma Assert
5934 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5936 -- Ada 2005 (AI-216): Program_Error is raised when executing
5937 -- the default implementation of the Write attribute of an
5938 -- Unchecked_Union type. However, if the 'Write reference is
5939 -- within the generated Output stream procedure, Write outputs
5940 -- the components, and the default values of the discriminant
5941 -- are streamed by the Output procedure itself.
5943 if Is_Unchecked_Union (Base_Type (U_Type))
5944 and not Is_TSS (Current_Scope, TSS_Stream_Output)
5945 then
5946 Insert_Action (N,
5947 Make_Raise_Program_Error (Loc,
5948 Reason => PE_Unchecked_Union_Restriction));
5949 end if;
5951 if Has_Discriminants (U_Type)
5952 and then Present
5953 (Discriminant_Default_Value (First_Discriminant (U_Type)))
5954 then
5955 Build_Mutable_Record_Write_Procedure
5956 (Loc, Full_Base (U_Type), Decl, Pname);
5957 else
5958 Build_Record_Write_Procedure
5959 (Loc, Full_Base (U_Type), Decl, Pname);
5960 end if;
5962 Insert_Action (N, Decl);
5963 end if;
5964 end if;
5966 -- If we fall through, Pname is the procedure to be called
5968 Rewrite_Stream_Proc_Call (Pname);
5969 end Write;
5971 -- Component_Size is handled by the back end, unless the component size
5972 -- is known at compile time, which is always true in the packed array
5973 -- case. It is important that the packed array case is handled in the
5974 -- front end (see Eval_Attribute) since the back end would otherwise get
5975 -- confused by the equivalent packed array type.
5977 when Attribute_Component_Size =>
5978 null;
5980 -- The following attributes are handled by the back end (except that
5981 -- static cases have already been evaluated during semantic processing,
5982 -- but in any case the back end should not count on this). The one bit
5983 -- of special processing required is that these attributes typically
5984 -- generate conditionals in the code, so we need to check the relevant
5985 -- restriction.
5987 when Attribute_Max |
5988 Attribute_Min =>
5989 Check_Restriction (No_Implicit_Conditionals, N);
5991 -- The following attributes are handled by the back end (except that
5992 -- static cases have already been evaluated during semantic processing,
5993 -- but in any case the back end should not count on this).
5995 -- The back end also handles the non-class-wide cases of Size
5997 when Attribute_Bit_Order |
5998 Attribute_Code_Address |
5999 Attribute_Definite |
6000 Attribute_Null_Parameter |
6001 Attribute_Passed_By_Reference |
6002 Attribute_Pool_Address |
6003 Attribute_Scalar_Storage_Order =>
6004 null;
6006 -- The following attributes are also handled by the back end, but return
6007 -- a universal integer result, so may need a conversion for checking
6008 -- that the result is in range.
6010 when Attribute_Aft |
6011 Attribute_Max_Alignment_For_Allocation =>
6012 Apply_Universal_Integer_Attribute_Checks (N);
6014 -- The following attributes should not appear at this stage, since they
6015 -- have already been handled by the analyzer (and properly rewritten
6016 -- with corresponding values or entities to represent the right values)
6018 when Attribute_Abort_Signal |
6019 Attribute_Address_Size |
6020 Attribute_Atomic_Always_Lock_Free |
6021 Attribute_Base |
6022 Attribute_Class |
6023 Attribute_Compiler_Version |
6024 Attribute_Default_Bit_Order |
6025 Attribute_Delta |
6026 Attribute_Denorm |
6027 Attribute_Digits |
6028 Attribute_Emax |
6029 Attribute_Enabled |
6030 Attribute_Epsilon |
6031 Attribute_Fast_Math |
6032 Attribute_First_Valid |
6033 Attribute_Has_Access_Values |
6034 Attribute_Has_Discriminants |
6035 Attribute_Has_Tagged_Values |
6036 Attribute_Large |
6037 Attribute_Last_Valid |
6038 Attribute_Lock_Free |
6039 Attribute_Machine_Emax |
6040 Attribute_Machine_Emin |
6041 Attribute_Machine_Mantissa |
6042 Attribute_Machine_Overflows |
6043 Attribute_Machine_Radix |
6044 Attribute_Machine_Rounds |
6045 Attribute_Maximum_Alignment |
6046 Attribute_Model_Emin |
6047 Attribute_Model_Epsilon |
6048 Attribute_Model_Mantissa |
6049 Attribute_Model_Small |
6050 Attribute_Modulus |
6051 Attribute_Partition_ID |
6052 Attribute_Range |
6053 Attribute_Safe_Emax |
6054 Attribute_Safe_First |
6055 Attribute_Safe_Large |
6056 Attribute_Safe_Last |
6057 Attribute_Safe_Small |
6058 Attribute_Scale |
6059 Attribute_Signed_Zeros |
6060 Attribute_Small |
6061 Attribute_Storage_Unit |
6062 Attribute_Stub_Type |
6063 Attribute_System_Allocator_Alignment |
6064 Attribute_Target_Name |
6065 Attribute_Type_Class |
6066 Attribute_Type_Key |
6067 Attribute_Unconstrained_Array |
6068 Attribute_Universal_Literal_String |
6069 Attribute_Wchar_T_Size |
6070 Attribute_Word_Size =>
6071 raise Program_Error;
6073 -- The Asm_Input and Asm_Output attributes are not expanded at this
6074 -- stage, but will be eliminated in the expansion of the Asm call, see
6075 -- Exp_Intr for details. So the back end will never see these either.
6077 when Attribute_Asm_Input |
6078 Attribute_Asm_Output =>
6079 null;
6080 end case;
6082 -- Note: as mentioned earlier, individual sections of the above case
6083 -- statement assume there is no code after the case statement, and are
6084 -- legitimately allowed to execute return statements if they have nothing
6085 -- more to do, so DO NOT add code at this point.
6087 exception
6088 when RE_Not_Available =>
6089 return;
6090 end Expand_N_Attribute_Reference;
6092 ----------------------
6093 -- Expand_Pred_Succ --
6094 ----------------------
6096 -- For typ'Pred (exp), we generate the check
6098 -- [constraint_error when exp = typ'Base'First]
6100 -- Similarly, for typ'Succ (exp), we generate the check
6102 -- [constraint_error when exp = typ'Base'Last]
6104 -- These checks are not generated for modular types, since the proper
6105 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
6106 -- We also suppress these checks if we are the right side of an assignment
6107 -- statement or the expression of an object declaration, where the flag
6108 -- Suppress_Assignment_Checks is set for the assignment/declaration.
6110 procedure Expand_Pred_Succ (N : Node_Id) is
6111 Loc : constant Source_Ptr := Sloc (N);
6112 P : constant Node_Id := Parent (N);
6113 Cnam : Name_Id;
6115 begin
6116 if Attribute_Name (N) = Name_Pred then
6117 Cnam := Name_First;
6118 else
6119 Cnam := Name_Last;
6120 end if;
6122 if not Nkind_In (P, N_Assignment_Statement, N_Object_Declaration)
6123 or else not Suppress_Assignment_Checks (P)
6124 then
6125 Insert_Action (N,
6126 Make_Raise_Constraint_Error (Loc,
6127 Condition =>
6128 Make_Op_Eq (Loc,
6129 Left_Opnd =>
6130 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
6131 Right_Opnd =>
6132 Make_Attribute_Reference (Loc,
6133 Prefix =>
6134 New_Reference_To (Base_Type (Etype (Prefix (N))), Loc),
6135 Attribute_Name => Cnam)),
6136 Reason => CE_Overflow_Check_Failed));
6137 end if;
6138 end Expand_Pred_Succ;
6140 -------------------
6141 -- Find_Fat_Info --
6142 -------------------
6144 procedure Find_Fat_Info
6145 (T : Entity_Id;
6146 Fat_Type : out Entity_Id;
6147 Fat_Pkg : out RE_Id)
6149 Btyp : constant Entity_Id := Base_Type (T);
6150 Rtyp : constant Entity_Id := Root_Type (T);
6151 Digs : constant Nat := UI_To_Int (Digits_Value (Btyp));
6153 begin
6154 -- If the base type is VAX float, then get appropriate VAX float type
6156 if Vax_Float (Btyp) then
6157 case Digs is
6158 when 6 =>
6159 Fat_Type := RTE (RE_Fat_VAX_F);
6160 Fat_Pkg := RE_Attr_VAX_F_Float;
6162 when 9 =>
6163 Fat_Type := RTE (RE_Fat_VAX_D);
6164 Fat_Pkg := RE_Attr_VAX_D_Float;
6166 when 15 =>
6167 Fat_Type := RTE (RE_Fat_VAX_G);
6168 Fat_Pkg := RE_Attr_VAX_G_Float;
6170 when others =>
6171 raise Program_Error;
6172 end case;
6174 -- If root type is VAX float, this is the case where the library has
6175 -- been recompiled in VAX float mode, and we have an IEEE float type.
6176 -- This is when we use the special IEEE Fat packages.
6178 elsif Vax_Float (Rtyp) then
6179 case Digs is
6180 when 6 =>
6181 Fat_Type := RTE (RE_Fat_IEEE_Short);
6182 Fat_Pkg := RE_Attr_IEEE_Short;
6184 when 15 =>
6185 Fat_Type := RTE (RE_Fat_IEEE_Long);
6186 Fat_Pkg := RE_Attr_IEEE_Long;
6188 when others =>
6189 raise Program_Error;
6190 end case;
6192 -- If neither the base type nor the root type is VAX_Native then VAX
6193 -- float is out of the picture, and we can just use the root type.
6195 else
6196 Fat_Type := Rtyp;
6198 if Fat_Type = Standard_Short_Float then
6199 Fat_Pkg := RE_Attr_Short_Float;
6201 elsif Fat_Type = Standard_Float then
6202 Fat_Pkg := RE_Attr_Float;
6204 elsif Fat_Type = Standard_Long_Float then
6205 Fat_Pkg := RE_Attr_Long_Float;
6207 elsif Fat_Type = Standard_Long_Long_Float then
6208 Fat_Pkg := RE_Attr_Long_Long_Float;
6210 -- Universal real (which is its own root type) is treated as being
6211 -- equivalent to Standard.Long_Long_Float, since it is defined to
6212 -- have the same precision as the longest Float type.
6214 elsif Fat_Type = Universal_Real then
6215 Fat_Type := Standard_Long_Long_Float;
6216 Fat_Pkg := RE_Attr_Long_Long_Float;
6218 else
6219 raise Program_Error;
6220 end if;
6221 end if;
6222 end Find_Fat_Info;
6224 ----------------------------
6225 -- Find_Stream_Subprogram --
6226 ----------------------------
6228 function Find_Stream_Subprogram
6229 (Typ : Entity_Id;
6230 Nam : TSS_Name_Type) return Entity_Id
6232 Base_Typ : constant Entity_Id := Base_Type (Typ);
6233 Ent : constant Entity_Id := TSS (Typ, Nam);
6235 function Is_Available (Entity : RE_Id) return Boolean;
6236 pragma Inline (Is_Available);
6237 -- Function to check whether the specified run-time call is available
6238 -- in the run time used. In the case of a configurable run time, it
6239 -- is normal that some subprograms are not there.
6241 -- I don't understand this routine at all, why is this not just a
6242 -- call to RTE_Available? And if for some reason we need a different
6243 -- routine with different semantics, why is not in Rtsfind ???
6245 ------------------
6246 -- Is_Available --
6247 ------------------
6249 function Is_Available (Entity : RE_Id) return Boolean is
6250 begin
6251 -- Assume that the unit will always be available when using a
6252 -- "normal" (not configurable) run time.
6254 return not Configurable_Run_Time_Mode
6255 or else RTE_Available (Entity);
6256 end Is_Available;
6258 -- Start of processing for Find_Stream_Subprogram
6260 begin
6261 if Present (Ent) then
6262 return Ent;
6263 end if;
6265 -- Stream attributes for strings are expanded into library calls. The
6266 -- following checks are disabled when the run-time is not available or
6267 -- when compiling predefined types due to bootstrap issues. As a result,
6268 -- the compiler will generate in-place stream routines for string types
6269 -- that appear in GNAT's library, but will generate calls via rtsfind
6270 -- to library routines for user code.
6272 -- ??? For now, disable this code for JVM, since this generates a
6273 -- VerifyError exception at run time on e.g. c330001.
6275 -- This is disabled for AAMP, to avoid creating dependences on files not
6276 -- supported in the AAMP library (such as s-fileio.adb).
6278 -- Note: In the case of using a configurable run time, it is very likely
6279 -- that stream routines for string types are not present (they require
6280 -- file system support). In this case, the specific stream routines for
6281 -- strings are not used, relying on the regular stream mechanism
6282 -- instead. That is why we include the test Is_Available when dealing
6283 -- with these cases.
6285 if VM_Target /= JVM_Target
6286 and then not AAMP_On_Target
6287 and then
6288 not Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
6289 then
6290 -- String as defined in package Ada
6292 if Base_Typ = Standard_String then
6293 if Restriction_Active (No_Stream_Optimizations) then
6294 if Nam = TSS_Stream_Input
6295 and then Is_Available (RE_String_Input)
6296 then
6297 return RTE (RE_String_Input);
6299 elsif Nam = TSS_Stream_Output
6300 and then Is_Available (RE_String_Output)
6301 then
6302 return RTE (RE_String_Output);
6304 elsif Nam = TSS_Stream_Read
6305 and then Is_Available (RE_String_Read)
6306 then
6307 return RTE (RE_String_Read);
6309 elsif Nam = TSS_Stream_Write
6310 and then Is_Available (RE_String_Write)
6311 then
6312 return RTE (RE_String_Write);
6314 elsif Nam /= TSS_Stream_Input and then
6315 Nam /= TSS_Stream_Output and then
6316 Nam /= TSS_Stream_Read and then
6317 Nam /= TSS_Stream_Write
6318 then
6319 raise Program_Error;
6320 end if;
6322 else
6323 if Nam = TSS_Stream_Input
6324 and then Is_Available (RE_String_Input_Blk_IO)
6325 then
6326 return RTE (RE_String_Input_Blk_IO);
6328 elsif Nam = TSS_Stream_Output
6329 and then Is_Available (RE_String_Output_Blk_IO)
6330 then
6331 return RTE (RE_String_Output_Blk_IO);
6333 elsif Nam = TSS_Stream_Read
6334 and then Is_Available (RE_String_Read_Blk_IO)
6335 then
6336 return RTE (RE_String_Read_Blk_IO);
6338 elsif Nam = TSS_Stream_Write
6339 and then Is_Available (RE_String_Write_Blk_IO)
6340 then
6341 return RTE (RE_String_Write_Blk_IO);
6343 elsif Nam /= TSS_Stream_Input and then
6344 Nam /= TSS_Stream_Output and then
6345 Nam /= TSS_Stream_Read and then
6346 Nam /= TSS_Stream_Write
6347 then
6348 raise Program_Error;
6349 end if;
6350 end if;
6352 -- Wide_String as defined in package Ada
6354 elsif Base_Typ = Standard_Wide_String then
6355 if Restriction_Active (No_Stream_Optimizations) then
6356 if Nam = TSS_Stream_Input
6357 and then Is_Available (RE_Wide_String_Input)
6358 then
6359 return RTE (RE_Wide_String_Input);
6361 elsif Nam = TSS_Stream_Output
6362 and then Is_Available (RE_Wide_String_Output)
6363 then
6364 return RTE (RE_Wide_String_Output);
6366 elsif Nam = TSS_Stream_Read
6367 and then Is_Available (RE_Wide_String_Read)
6368 then
6369 return RTE (RE_Wide_String_Read);
6371 elsif Nam = TSS_Stream_Write
6372 and then Is_Available (RE_Wide_String_Write)
6373 then
6374 return RTE (RE_Wide_String_Write);
6376 elsif Nam /= TSS_Stream_Input and then
6377 Nam /= TSS_Stream_Output and then
6378 Nam /= TSS_Stream_Read and then
6379 Nam /= TSS_Stream_Write
6380 then
6381 raise Program_Error;
6382 end if;
6384 else
6385 if Nam = TSS_Stream_Input
6386 and then Is_Available (RE_Wide_String_Input_Blk_IO)
6387 then
6388 return RTE (RE_Wide_String_Input_Blk_IO);
6390 elsif Nam = TSS_Stream_Output
6391 and then Is_Available (RE_Wide_String_Output_Blk_IO)
6392 then
6393 return RTE (RE_Wide_String_Output_Blk_IO);
6395 elsif Nam = TSS_Stream_Read
6396 and then Is_Available (RE_Wide_String_Read_Blk_IO)
6397 then
6398 return RTE (RE_Wide_String_Read_Blk_IO);
6400 elsif Nam = TSS_Stream_Write
6401 and then Is_Available (RE_Wide_String_Write_Blk_IO)
6402 then
6403 return RTE (RE_Wide_String_Write_Blk_IO);
6405 elsif Nam /= TSS_Stream_Input and then
6406 Nam /= TSS_Stream_Output and then
6407 Nam /= TSS_Stream_Read and then
6408 Nam /= TSS_Stream_Write
6409 then
6410 raise Program_Error;
6411 end if;
6412 end if;
6414 -- Wide_Wide_String as defined in package Ada
6416 elsif Base_Typ = Standard_Wide_Wide_String then
6417 if Restriction_Active (No_Stream_Optimizations) then
6418 if Nam = TSS_Stream_Input
6419 and then Is_Available (RE_Wide_Wide_String_Input)
6420 then
6421 return RTE (RE_Wide_Wide_String_Input);
6423 elsif Nam = TSS_Stream_Output
6424 and then Is_Available (RE_Wide_Wide_String_Output)
6425 then
6426 return RTE (RE_Wide_Wide_String_Output);
6428 elsif Nam = TSS_Stream_Read
6429 and then Is_Available (RE_Wide_Wide_String_Read)
6430 then
6431 return RTE (RE_Wide_Wide_String_Read);
6433 elsif Nam = TSS_Stream_Write
6434 and then Is_Available (RE_Wide_Wide_String_Write)
6435 then
6436 return RTE (RE_Wide_Wide_String_Write);
6438 elsif Nam /= TSS_Stream_Input and then
6439 Nam /= TSS_Stream_Output and then
6440 Nam /= TSS_Stream_Read and then
6441 Nam /= TSS_Stream_Write
6442 then
6443 raise Program_Error;
6444 end if;
6446 else
6447 if Nam = TSS_Stream_Input
6448 and then Is_Available (RE_Wide_Wide_String_Input_Blk_IO)
6449 then
6450 return RTE (RE_Wide_Wide_String_Input_Blk_IO);
6452 elsif Nam = TSS_Stream_Output
6453 and then Is_Available (RE_Wide_Wide_String_Output_Blk_IO)
6454 then
6455 return RTE (RE_Wide_Wide_String_Output_Blk_IO);
6457 elsif Nam = TSS_Stream_Read
6458 and then Is_Available (RE_Wide_Wide_String_Read_Blk_IO)
6459 then
6460 return RTE (RE_Wide_Wide_String_Read_Blk_IO);
6462 elsif Nam = TSS_Stream_Write
6463 and then Is_Available (RE_Wide_Wide_String_Write_Blk_IO)
6464 then
6465 return RTE (RE_Wide_Wide_String_Write_Blk_IO);
6467 elsif Nam /= TSS_Stream_Input and then
6468 Nam /= TSS_Stream_Output and then
6469 Nam /= TSS_Stream_Read and then
6470 Nam /= TSS_Stream_Write
6471 then
6472 raise Program_Error;
6473 end if;
6474 end if;
6475 end if;
6476 end if;
6478 if Is_Tagged_Type (Typ)
6479 and then Is_Derived_Type (Typ)
6480 then
6481 return Find_Prim_Op (Typ, Nam);
6482 else
6483 return Find_Inherited_TSS (Typ, Nam);
6484 end if;
6485 end Find_Stream_Subprogram;
6487 ---------------
6488 -- Full_Base --
6489 ---------------
6491 function Full_Base (T : Entity_Id) return Entity_Id is
6492 BT : Entity_Id;
6494 begin
6495 BT := Base_Type (T);
6497 if Is_Private_Type (BT)
6498 and then Present (Full_View (BT))
6499 then
6500 BT := Full_View (BT);
6501 end if;
6503 return BT;
6504 end Full_Base;
6506 -----------------------
6507 -- Get_Index_Subtype --
6508 -----------------------
6510 function Get_Index_Subtype (N : Node_Id) return Node_Id is
6511 P_Type : Entity_Id := Etype (Prefix (N));
6512 Indx : Node_Id;
6513 J : Int;
6515 begin
6516 if Is_Access_Type (P_Type) then
6517 P_Type := Designated_Type (P_Type);
6518 end if;
6520 if No (Expressions (N)) then
6521 J := 1;
6522 else
6523 J := UI_To_Int (Expr_Value (First (Expressions (N))));
6524 end if;
6526 Indx := First_Index (P_Type);
6527 while J > 1 loop
6528 Next_Index (Indx);
6529 J := J - 1;
6530 end loop;
6532 return Etype (Indx);
6533 end Get_Index_Subtype;
6535 -------------------------------
6536 -- Get_Stream_Convert_Pragma --
6537 -------------------------------
6539 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is
6540 Typ : Entity_Id;
6541 N : Node_Id;
6543 begin
6544 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
6545 -- that a stream convert pragma for a tagged type is not inherited from
6546 -- its parent. Probably what is wrong here is that it is basically
6547 -- incorrect to consider a stream convert pragma to be a representation
6548 -- pragma at all ???
6550 N := First_Rep_Item (Implementation_Base_Type (T));
6551 while Present (N) loop
6552 if Nkind (N) = N_Pragma
6553 and then Pragma_Name (N) = Name_Stream_Convert
6554 then
6555 -- For tagged types this pragma is not inherited, so we
6556 -- must verify that it is defined for the given type and
6557 -- not an ancestor.
6559 Typ :=
6560 Entity (Expression (First (Pragma_Argument_Associations (N))));
6562 if not Is_Tagged_Type (T)
6563 or else T = Typ
6564 or else (Is_Private_Type (Typ) and then T = Full_View (Typ))
6565 then
6566 return N;
6567 end if;
6568 end if;
6570 Next_Rep_Item (N);
6571 end loop;
6573 return Empty;
6574 end Get_Stream_Convert_Pragma;
6576 ---------------------------------
6577 -- Is_Constrained_Packed_Array --
6578 ---------------------------------
6580 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
6581 Arr : Entity_Id := Typ;
6583 begin
6584 if Is_Access_Type (Arr) then
6585 Arr := Designated_Type (Arr);
6586 end if;
6588 return Is_Array_Type (Arr)
6589 and then Is_Constrained (Arr)
6590 and then Present (Packed_Array_Type (Arr));
6591 end Is_Constrained_Packed_Array;
6593 ----------------------------------------
6594 -- Is_Inline_Floating_Point_Attribute --
6595 ----------------------------------------
6597 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is
6598 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
6600 begin
6601 if Nkind (Parent (N)) /= N_Type_Conversion
6602 or else not Is_Integer_Type (Etype (Parent (N)))
6603 then
6604 return False;
6605 end if;
6607 -- Should also support 'Machine_Rounding and 'Unbiased_Rounding, but
6608 -- required back end support has not been implemented yet ???
6610 return Id = Attribute_Truncation;
6611 end Is_Inline_Floating_Point_Attribute;
6613 end Exp_Attr;