PR middle-end/77674
[official-gcc.git] / gcc / ada / exp_attr.adb
blob04929b5aa575a2f6191c2bd7ffd12c52ae86a9c4
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-2016, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch2; use Exp_Ch2;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Imgv; use Exp_Imgv;
38 with Exp_Pakd; use Exp_Pakd;
39 with Exp_Strm; use Exp_Strm;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with 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 function Build_Record_VS_Func
88 (R_Type : Entity_Id;
89 Nod : Node_Id) return Entity_Id;
90 -- Build function to test Valid_Scalars for record type A_Type. Nod is the
91 -- Valid_Scalars attribute node, used to insert the function body, and the
92 -- value returned is the entity of the constructed function body. We do not
93 -- bother to generate a separate spec for this subprogram.
95 procedure Compile_Stream_Body_In_Scope
96 (N : Node_Id;
97 Decl : Node_Id;
98 Arr : Entity_Id;
99 Check : Boolean);
100 -- The body for a stream subprogram may be generated outside of the scope
101 -- of the type. If the type is fully private, it may depend on the full
102 -- view of other types (e.g. indexes) that are currently private as well.
103 -- We install the declarations of the package in which the type is declared
104 -- before compiling the body in what is its proper environment. The Check
105 -- parameter indicates if checks are to be suppressed for the stream body.
106 -- We suppress checks for array/record reads, since the rule is that these
107 -- are like assignments, out of range values due to uninitialized storage,
108 -- or other invalid values do NOT cause a Constraint_Error to be raised.
109 -- If we are within an instance body all visibility has been established
110 -- already and there is no need to install the package.
112 -- This mechanism is now extended to the component types of the array type,
113 -- when the component type is not in scope and is private, to handle
114 -- properly the case when the full view has defaulted discriminants.
116 -- This special processing is ultimately caused by the fact that the
117 -- compiler lacks a well-defined phase when full views are visible
118 -- everywhere. Having such a separate pass would remove much of the
119 -- special-case code that shuffles partial and full views in the middle
120 -- of semantic analysis and expansion.
122 procedure Expand_Access_To_Protected_Op
123 (N : Node_Id;
124 Pref : Node_Id;
125 Typ : Entity_Id);
126 -- An attribute reference to a protected subprogram is transformed into
127 -- a pair of pointers: one to the object, and one to the operations.
128 -- This expansion is performed for 'Access and for 'Unrestricted_Access.
130 procedure Expand_Fpt_Attribute
131 (N : Node_Id;
132 Pkg : RE_Id;
133 Nam : Name_Id;
134 Args : List_Id);
135 -- This procedure expands a call to a floating-point attribute function.
136 -- N is the attribute reference node, and Args is a list of arguments to
137 -- be passed to the function call. Pkg identifies the package containing
138 -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args
139 -- have already been converted to the floating-point type for which Pkg was
140 -- instantiated. The Nam argument is the relevant attribute processing
141 -- routine to be called. This is the same as the attribute name, except in
142 -- the Unaligned_Valid case.
144 procedure Expand_Fpt_Attribute_R (N : Node_Id);
145 -- This procedure expands a call to a floating-point attribute function
146 -- that takes a single floating-point argument. The function to be called
147 -- is always the same as the attribute name.
149 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
150 -- This procedure expands a call to a floating-point attribute function
151 -- that takes one floating-point argument and one integer argument. The
152 -- function to be called is always the same as the attribute name.
154 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
155 -- This procedure expands a call to a floating-point attribute function
156 -- that takes two floating-point arguments. The function to be called
157 -- is always the same as the attribute name.
159 procedure Expand_Loop_Entry_Attribute (N : Node_Id);
160 -- Handle the expansion of attribute 'Loop_Entry. As a result, the related
161 -- loop may be converted into a conditional block. See body for details.
163 procedure Expand_Min_Max_Attribute (N : Node_Id);
164 -- Handle the expansion of attributes 'Max and 'Min, including expanding
165 -- then out if we are in Modify_Tree_For_C mode.
167 procedure Expand_Pred_Succ_Attribute (N : Node_Id);
168 -- Handles expansion of Pred or Succ attributes for case of non-real
169 -- operand with overflow checking required.
171 procedure Expand_Update_Attribute (N : Node_Id);
172 -- Handle the expansion of attribute Update
174 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
175 -- Used for Last, Last, and Length, when the prefix is an array type.
176 -- Obtains the corresponding index subtype.
178 procedure Find_Fat_Info
179 (T : Entity_Id;
180 Fat_Type : out Entity_Id;
181 Fat_Pkg : out RE_Id);
182 -- Given a floating-point type T, identifies the package containing the
183 -- attributes for this type (returned in Fat_Pkg), and the corresponding
184 -- type for which this package was instantiated from Fat_Gen. Error if T
185 -- is not a floating-point type.
187 function Find_Stream_Subprogram
188 (Typ : Entity_Id;
189 Nam : TSS_Name_Type) return Entity_Id;
190 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
191 -- types, the corresponding primitive operation is looked up, else the
192 -- appropriate TSS from the type itself, or from its closest ancestor
193 -- defining it, is returned. In both cases, inheritance of representation
194 -- aspects is thus taken into account.
196 function Full_Base (T : Entity_Id) return Entity_Id;
197 -- The stream functions need to examine the underlying representation of
198 -- composite types. In some cases T may be non-private but its base type
199 -- is, in which case the function returns the corresponding full view.
201 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id;
202 -- Given a type, find a corresponding stream convert pragma that applies to
203 -- the implementation base type of this type (Typ). If found, return the
204 -- pragma node, otherwise return Empty if no pragma is found.
206 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
207 -- Utility for array attributes, returns true on packed constrained
208 -- arrays, and on access to same.
210 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean;
211 -- Returns true iff the given node refers to an attribute call that
212 -- can be expanded directly by the back end and does not need front end
213 -- expansion. Typically used for rounding and truncation attributes that
214 -- appear directly inside a conversion to integer.
216 -------------------------
217 -- Build_Array_VS_Func --
218 -------------------------
220 function Build_Array_VS_Func
221 (A_Type : Entity_Id;
222 Nod : Node_Id) return Entity_Id
224 Loc : constant Source_Ptr := Sloc (Nod);
225 Func_Id : constant Entity_Id := Make_Temporary (Loc, 'V');
226 Comp_Type : constant Entity_Id := Component_Type (A_Type);
227 Body_Stmts : List_Id;
228 Index_List : List_Id;
229 Formals : List_Id;
231 function Test_Component return List_Id;
232 -- Create one statement to test validity of one component designated by
233 -- a full set of indexes. Returns statement list containing test.
235 function Test_One_Dimension (N : Int) return List_Id;
236 -- Create loop to test one dimension of the array. The single statement
237 -- in the loop body tests the inner dimensions if any, or else the
238 -- single component. Note that this procedure is called recursively,
239 -- with N being the dimension to be initialized. A call with N greater
240 -- than the number of dimensions simply generates the component test,
241 -- terminating the recursion. Returns statement list containing tests.
243 --------------------
244 -- Test_Component --
245 --------------------
247 function Test_Component return List_Id is
248 Comp : Node_Id;
249 Anam : Name_Id;
251 begin
252 Comp :=
253 Make_Indexed_Component (Loc,
254 Prefix => Make_Identifier (Loc, Name_uA),
255 Expressions => Index_List);
257 if Is_Scalar_Type (Comp_Type) then
258 Anam := Name_Valid;
259 else
260 Anam := Name_Valid_Scalars;
261 end if;
263 return New_List (
264 Make_If_Statement (Loc,
265 Condition =>
266 Make_Op_Not (Loc,
267 Right_Opnd =>
268 Make_Attribute_Reference (Loc,
269 Attribute_Name => Anam,
270 Prefix => Comp)),
271 Then_Statements => New_List (
272 Make_Simple_Return_Statement (Loc,
273 Expression => New_Occurrence_Of (Standard_False, Loc)))));
274 end Test_Component;
276 ------------------------
277 -- Test_One_Dimension --
278 ------------------------
280 function Test_One_Dimension (N : Int) return List_Id is
281 Index : Entity_Id;
283 begin
284 -- If all dimensions dealt with, we simply test the component
286 if N > Number_Dimensions (A_Type) then
287 return Test_Component;
289 -- Here we generate the required loop
291 else
292 Index :=
293 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
295 Append (New_Occurrence_Of (Index, Loc), Index_List);
297 return New_List (
298 Make_Implicit_Loop_Statement (Nod,
299 Identifier => Empty,
300 Iteration_Scheme =>
301 Make_Iteration_Scheme (Loc,
302 Loop_Parameter_Specification =>
303 Make_Loop_Parameter_Specification (Loc,
304 Defining_Identifier => Index,
305 Discrete_Subtype_Definition =>
306 Make_Attribute_Reference (Loc,
307 Prefix => Make_Identifier (Loc, Name_uA),
308 Attribute_Name => Name_Range,
309 Expressions => New_List (
310 Make_Integer_Literal (Loc, N))))),
311 Statements => Test_One_Dimension (N + 1)),
312 Make_Simple_Return_Statement (Loc,
313 Expression => New_Occurrence_Of (Standard_True, Loc)));
314 end if;
315 end Test_One_Dimension;
317 -- Start of processing for Build_Array_VS_Func
319 begin
320 Index_List := New_List;
321 Body_Stmts := Test_One_Dimension (1);
323 -- Parameter is always (A : A_Typ)
325 Formals := New_List (
326 Make_Parameter_Specification (Loc,
327 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uA),
328 In_Present => True,
329 Out_Present => False,
330 Parameter_Type => New_Occurrence_Of (A_Type, Loc)));
332 -- Build body
334 Set_Ekind (Func_Id, E_Function);
335 Set_Is_Internal (Func_Id);
337 Insert_Action (Nod,
338 Make_Subprogram_Body (Loc,
339 Specification =>
340 Make_Function_Specification (Loc,
341 Defining_Unit_Name => Func_Id,
342 Parameter_Specifications => Formals,
343 Result_Definition =>
344 New_Occurrence_Of (Standard_Boolean, Loc)),
345 Declarations => New_List,
346 Handled_Statement_Sequence =>
347 Make_Handled_Sequence_Of_Statements (Loc,
348 Statements => Body_Stmts)));
350 if not Debug_Generated_Code then
351 Set_Debug_Info_Off (Func_Id);
352 end if;
354 Set_Is_Pure (Func_Id);
355 return Func_Id;
356 end Build_Array_VS_Func;
358 --------------------------
359 -- Build_Record_VS_Func --
360 --------------------------
362 -- Generates:
364 -- function _Valid_Scalars (X : T) return Boolean is
365 -- begin
366 -- -- Check discriminants
368 -- if not X.D1'Valid_Scalars or else
369 -- not X.D2'Valid_Scalars or else
370 -- ...
371 -- then
372 -- return False;
373 -- end if;
375 -- -- Check components
377 -- if not X.C1'Valid_Scalars or else
378 -- not X.C2'Valid_Scalars or else
379 -- ...
380 -- then
381 -- return False;
382 -- end if;
384 -- -- Check variant part
386 -- case X.D1 is
387 -- when V1 =>
388 -- if not X.C2'Valid_Scalars or else
389 -- not X.C3'Valid_Scalars or else
390 -- ...
391 -- then
392 -- return False;
393 -- end if;
394 -- ...
395 -- when Vn =>
396 -- if not X.Cn'Valid_Scalars or else
397 -- ...
398 -- then
399 -- return False;
400 -- end if;
401 -- end case;
403 -- return True;
404 -- end _Valid_Scalars;
406 function Build_Record_VS_Func
407 (R_Type : Entity_Id;
408 Nod : Node_Id) return Entity_Id
410 Loc : constant Source_Ptr := Sloc (R_Type);
411 Func_Id : constant Entity_Id := Make_Temporary (Loc, 'V');
412 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_X);
414 function Make_VS_Case
415 (E : Entity_Id;
416 CL : Node_Id;
417 Discrs : Elist_Id := New_Elmt_List) return List_Id;
418 -- Building block for variant valid scalars. Given a Component_List node
419 -- CL, it generates an 'if' followed by a 'case' statement that compares
420 -- all components of local temporaries named X and Y (that are declared
421 -- as formals at some upper level). E provides the Sloc to be used for
422 -- the generated code.
424 function Make_VS_If
425 (E : Entity_Id;
426 L : List_Id) return Node_Id;
427 -- Building block for variant validate scalars. Given the list, L, of
428 -- components (or discriminants) L, it generates a return statement that
429 -- compares all components of local temporaries named X and Y (that are
430 -- declared as formals at some upper level). E provides the Sloc to be
431 -- used for the generated code.
433 ------------------
434 -- Make_VS_Case --
435 ------------------
437 -- <Make_VS_If on shared components>
439 -- case X.D1 is
440 -- when V1 => <Make_VS_Case> on subcomponents
441 -- ...
442 -- when Vn => <Make_VS_Case> on subcomponents
443 -- end case;
445 function Make_VS_Case
446 (E : Entity_Id;
447 CL : Node_Id;
448 Discrs : Elist_Id := New_Elmt_List) return List_Id
450 Loc : constant Source_Ptr := Sloc (E);
451 Result : constant List_Id := New_List;
452 Variant : Node_Id;
453 Alt_List : List_Id;
455 begin
456 Append_To (Result, Make_VS_If (E, Component_Items (CL)));
458 if No (Variant_Part (CL)) then
459 return Result;
460 end if;
462 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
464 if No (Variant) then
465 return Result;
466 end if;
468 Alt_List := New_List;
469 while Present (Variant) loop
470 Append_To (Alt_List,
471 Make_Case_Statement_Alternative (Loc,
472 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
473 Statements =>
474 Make_VS_Case (E, Component_List (Variant), Discrs)));
475 Next_Non_Pragma (Variant);
476 end loop;
478 Append_To (Result,
479 Make_Case_Statement (Loc,
480 Expression =>
481 Make_Selected_Component (Loc,
482 Prefix => Make_Identifier (Loc, Name_X),
483 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
484 Alternatives => Alt_List));
486 return Result;
487 end Make_VS_Case;
489 ----------------
490 -- Make_VS_If --
491 ----------------
493 -- Generates:
495 -- if
496 -- not X.C1'Valid_Scalars
497 -- or else
498 -- not X.C2'Valid_Scalars
499 -- ...
500 -- then
501 -- return False;
502 -- end if;
504 -- or a null statement if the list L is empty
506 function Make_VS_If
507 (E : Entity_Id;
508 L : List_Id) return Node_Id
510 Loc : constant Source_Ptr := Sloc (E);
511 C : Node_Id;
512 Def_Id : Entity_Id;
513 Field_Name : Name_Id;
514 Cond : Node_Id;
516 begin
517 if No (L) then
518 return Make_Null_Statement (Loc);
520 else
521 Cond := Empty;
523 C := First_Non_Pragma (L);
524 while Present (C) loop
525 Def_Id := Defining_Identifier (C);
526 Field_Name := Chars (Def_Id);
528 -- The tags need not be checked since they will always be valid
530 -- Note also that in the following, we use Make_Identifier for
531 -- the component names. Use of New_Occurrence_Of to identify
532 -- the components would be incorrect because wrong entities for
533 -- discriminants could be picked up in the private type case.
535 -- Don't bother with abstract parent in interface case
537 if Field_Name = Name_uParent
538 and then Is_Interface (Etype (Def_Id))
539 then
540 null;
542 -- Don't bother with tag, always valid, and not scalar anyway
544 elsif Field_Name = Name_uTag then
545 null;
547 -- Don't bother with component with no scalar components
549 elsif not Scalar_Part_Present (Etype (Def_Id)) then
550 null;
552 -- Normal case, generate Valid_Scalars attribute reference
554 else
555 Evolve_Or_Else (Cond,
556 Make_Op_Not (Loc,
557 Right_Opnd =>
558 Make_Attribute_Reference (Loc,
559 Prefix =>
560 Make_Selected_Component (Loc,
561 Prefix =>
562 Make_Identifier (Loc, Name_X),
563 Selector_Name =>
564 Make_Identifier (Loc, Field_Name)),
565 Attribute_Name => Name_Valid_Scalars)));
566 end if;
568 Next_Non_Pragma (C);
569 end loop;
571 if No (Cond) then
572 return Make_Null_Statement (Loc);
574 else
575 return
576 Make_Implicit_If_Statement (E,
577 Condition => Cond,
578 Then_Statements => New_List (
579 Make_Simple_Return_Statement (Loc,
580 Expression =>
581 New_Occurrence_Of (Standard_False, Loc))));
582 end if;
583 end if;
584 end Make_VS_If;
586 -- Local variables
588 Def : constant Node_Id := Parent (R_Type);
589 Comps : constant Node_Id := Component_List (Type_Definition (Def));
590 Stmts : constant List_Id := New_List;
591 Pspecs : constant List_Id := New_List;
593 -- Start of processing for Build_Record_VS_Func
595 begin
596 Append_To (Pspecs,
597 Make_Parameter_Specification (Loc,
598 Defining_Identifier => X,
599 Parameter_Type => New_Occurrence_Of (R_Type, Loc)));
601 Append_To (Stmts,
602 Make_VS_If (R_Type, Discriminant_Specifications (Def)));
603 Append_List_To (Stmts, Make_VS_Case (R_Type, Comps));
605 Append_To (Stmts,
606 Make_Simple_Return_Statement (Loc,
607 Expression => New_Occurrence_Of (Standard_True, Loc)));
609 Insert_Action (Nod,
610 Make_Subprogram_Body (Loc,
611 Specification =>
612 Make_Function_Specification (Loc,
613 Defining_Unit_Name => Func_Id,
614 Parameter_Specifications => Pspecs,
615 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
616 Declarations => New_List,
617 Handled_Statement_Sequence =>
618 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)),
619 Suppress => Discriminant_Check);
621 if not Debug_Generated_Code then
622 Set_Debug_Info_Off (Func_Id);
623 end if;
625 Set_Is_Pure (Func_Id);
626 return Func_Id;
627 end Build_Record_VS_Func;
629 ----------------------------------
630 -- Compile_Stream_Body_In_Scope --
631 ----------------------------------
633 procedure Compile_Stream_Body_In_Scope
634 (N : Node_Id;
635 Decl : Node_Id;
636 Arr : Entity_Id;
637 Check : Boolean)
639 C_Type : constant Entity_Id := Base_Type (Component_Type (Arr));
640 Curr : constant Entity_Id := Current_Scope;
641 Install : Boolean := False;
642 Scop : Entity_Id := Scope (Arr);
644 begin
645 if Is_Hidden (Arr)
646 and then not In_Open_Scopes (Scop)
647 and then Ekind (Scop) = E_Package
648 then
649 Install := True;
651 else
652 -- The component type may be private, in which case we install its
653 -- full view to compile the subprogram.
655 -- The component type may be private, in which case we install its
656 -- full view to compile the subprogram. We do not do this if the
657 -- type has a Stream_Convert pragma, which indicates that there are
658 -- special stream-processing operations for that type (for example
659 -- Unbounded_String and its wide varieties).
661 Scop := Scope (C_Type);
663 if Is_Private_Type (C_Type)
664 and then Present (Full_View (C_Type))
665 and then not In_Open_Scopes (Scop)
666 and then Ekind (Scop) = E_Package
667 and then No (Get_Stream_Convert_Pragma (C_Type))
668 then
669 Install := True;
670 end if;
671 end if;
673 -- If we are within an instance body, then all visibility has been
674 -- established already and there is no need to install the package.
676 if Install and then not In_Instance_Body then
677 Push_Scope (Scop);
678 Install_Visible_Declarations (Scop);
679 Install_Private_Declarations (Scop);
681 -- The entities in the package are now visible, but the generated
682 -- stream entity must appear in the current scope (usually an
683 -- enclosing stream function) so that itypes all have their proper
684 -- scopes.
686 Push_Scope (Curr);
687 else
688 Install := False;
689 end if;
691 if Check then
692 Insert_Action (N, Decl);
693 else
694 Insert_Action (N, Decl, Suppress => All_Checks);
695 end if;
697 if Install then
699 -- Remove extra copy of current scope, and package itself
701 Pop_Scope;
702 End_Package_Scope (Scop);
703 end if;
704 end Compile_Stream_Body_In_Scope;
706 -----------------------------------
707 -- Expand_Access_To_Protected_Op --
708 -----------------------------------
710 procedure Expand_Access_To_Protected_Op
711 (N : Node_Id;
712 Pref : Node_Id;
713 Typ : Entity_Id)
715 -- The value of the attribute_reference is a record containing two
716 -- fields: an access to the protected object, and an access to the
717 -- subprogram itself. The prefix is a selected component.
719 Loc : constant Source_Ptr := Sloc (N);
720 Agg : Node_Id;
721 Btyp : constant Entity_Id := Base_Type (Typ);
722 Sub : Entity_Id;
723 Sub_Ref : Node_Id;
724 E_T : constant Entity_Id := Equivalent_Type (Btyp);
725 Acc : constant Entity_Id :=
726 Etype (Next_Component (First_Component (E_T)));
727 Obj_Ref : Node_Id;
728 Curr : Entity_Id;
730 -- Start of processing for Expand_Access_To_Protected_Op
732 begin
733 -- Within the body of the protected type, the prefix designates a local
734 -- operation, and the object is the first parameter of the corresponding
735 -- protected body of the current enclosing operation.
737 if Is_Entity_Name (Pref) then
738 -- All indirect calls are external calls, so must do locking and
739 -- barrier reevaluation, even if the 'Access occurs within the
740 -- protected body. Hence the call to External_Subprogram, as opposed
741 -- to Protected_Body_Subprogram, below. See RM-9.5(5). This means
742 -- that indirect calls from within the same protected body will
743 -- deadlock, as allowed by RM-9.5.1(8,15,17).
745 Sub := New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc);
747 -- Don't traverse the scopes when the attribute occurs within an init
748 -- proc, because we directly use the _init formal of the init proc in
749 -- that case.
751 Curr := Current_Scope;
752 if not Is_Init_Proc (Curr) then
753 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
755 while Scope (Curr) /= Scope (Entity (Pref)) loop
756 Curr := Scope (Curr);
757 end loop;
758 end if;
760 -- In case of protected entries the first formal of its Protected_
761 -- Body_Subprogram is the address of the object.
763 if Ekind (Curr) = E_Entry then
764 Obj_Ref :=
765 New_Occurrence_Of
766 (First_Formal
767 (Protected_Body_Subprogram (Curr)), Loc);
769 -- If the current scope is an init proc, then use the address of the
770 -- _init formal as the object reference.
772 elsif Is_Init_Proc (Curr) then
773 Obj_Ref :=
774 Make_Attribute_Reference (Loc,
775 Prefix => New_Occurrence_Of (First_Formal (Curr), Loc),
776 Attribute_Name => Name_Address);
778 -- In case of protected subprograms the first formal of its
779 -- Protected_Body_Subprogram is the object and we get its address.
781 else
782 Obj_Ref :=
783 Make_Attribute_Reference (Loc,
784 Prefix =>
785 New_Occurrence_Of
786 (First_Formal
787 (Protected_Body_Subprogram (Curr)), Loc),
788 Attribute_Name => Name_Address);
789 end if;
791 -- Case where the prefix is not an entity name. Find the
792 -- version of the protected operation to be called from
793 -- outside the protected object.
795 else
796 Sub :=
797 New_Occurrence_Of
798 (External_Subprogram
799 (Entity (Selector_Name (Pref))), Loc);
801 Obj_Ref :=
802 Make_Attribute_Reference (Loc,
803 Prefix => Relocate_Node (Prefix (Pref)),
804 Attribute_Name => Name_Address);
805 end if;
807 Sub_Ref :=
808 Make_Attribute_Reference (Loc,
809 Prefix => Sub,
810 Attribute_Name => Name_Access);
812 -- We set the type of the access reference to the already generated
813 -- access_to_subprogram type, and declare the reference analyzed, to
814 -- prevent further expansion when the enclosing aggregate is analyzed.
816 Set_Etype (Sub_Ref, Acc);
817 Set_Analyzed (Sub_Ref);
819 Agg :=
820 Make_Aggregate (Loc,
821 Expressions => New_List (Obj_Ref, Sub_Ref));
823 -- Sub_Ref has been marked as analyzed, but we still need to make sure
824 -- Sub is correctly frozen.
826 Freeze_Before (N, Entity (Sub));
828 Rewrite (N, Agg);
829 Analyze_And_Resolve (N, E_T);
831 -- For subsequent analysis, the node must retain its type. The backend
832 -- will replace it with the equivalent type where needed.
834 Set_Etype (N, Typ);
835 end Expand_Access_To_Protected_Op;
837 --------------------------
838 -- Expand_Fpt_Attribute --
839 --------------------------
841 procedure Expand_Fpt_Attribute
842 (N : Node_Id;
843 Pkg : RE_Id;
844 Nam : Name_Id;
845 Args : List_Id)
847 Loc : constant Source_Ptr := Sloc (N);
848 Typ : constant Entity_Id := Etype (N);
849 Fnm : Node_Id;
851 begin
852 -- The function name is the selected component Attr_xxx.yyy where
853 -- Attr_xxx is the package name, and yyy is the argument Nam.
855 -- Note: it would be more usual to have separate RE entries for each
856 -- of the entities in the Fat packages, but first they have identical
857 -- names (so we would have to have lots of renaming declarations to
858 -- meet the normal RE rule of separate names for all runtime entities),
859 -- and second there would be an awful lot of them.
861 Fnm :=
862 Make_Selected_Component (Loc,
863 Prefix => New_Occurrence_Of (RTE (Pkg), Loc),
864 Selector_Name => Make_Identifier (Loc, Nam));
866 -- The generated call is given the provided set of parameters, and then
867 -- wrapped in a conversion which converts the result to the target type
868 -- We use the base type as the target because a range check may be
869 -- required.
871 Rewrite (N,
872 Unchecked_Convert_To (Base_Type (Etype (N)),
873 Make_Function_Call (Loc,
874 Name => Fnm,
875 Parameter_Associations => Args)));
877 Analyze_And_Resolve (N, Typ);
878 end Expand_Fpt_Attribute;
880 ----------------------------
881 -- Expand_Fpt_Attribute_R --
882 ----------------------------
884 -- The single argument is converted to its root type to call the
885 -- appropriate runtime function, with the actual call being built
886 -- by Expand_Fpt_Attribute
888 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
889 E1 : constant Node_Id := First (Expressions (N));
890 Ftp : Entity_Id;
891 Pkg : RE_Id;
892 begin
893 Find_Fat_Info (Etype (E1), Ftp, Pkg);
894 Expand_Fpt_Attribute
895 (N, Pkg, Attribute_Name (N),
896 New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1))));
897 end Expand_Fpt_Attribute_R;
899 -----------------------------
900 -- Expand_Fpt_Attribute_RI --
901 -----------------------------
903 -- The first argument is converted to its root type and the second
904 -- argument is converted to standard long long integer to call the
905 -- appropriate runtime function, with the actual call being built
906 -- by Expand_Fpt_Attribute
908 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
909 E1 : constant Node_Id := First (Expressions (N));
910 Ftp : Entity_Id;
911 Pkg : RE_Id;
912 E2 : constant Node_Id := Next (E1);
913 begin
914 Find_Fat_Info (Etype (E1), Ftp, Pkg);
915 Expand_Fpt_Attribute
916 (N, Pkg, Attribute_Name (N),
917 New_List (
918 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
919 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
920 end Expand_Fpt_Attribute_RI;
922 -----------------------------
923 -- Expand_Fpt_Attribute_RR --
924 -----------------------------
926 -- The two arguments are converted to their root types to call the
927 -- appropriate runtime function, with the actual call being built
928 -- by Expand_Fpt_Attribute
930 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
931 E1 : constant Node_Id := First (Expressions (N));
932 E2 : constant Node_Id := Next (E1);
933 Ftp : Entity_Id;
934 Pkg : RE_Id;
936 begin
937 Find_Fat_Info (Etype (E1), Ftp, Pkg);
938 Expand_Fpt_Attribute
939 (N, Pkg, Attribute_Name (N),
940 New_List (
941 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
942 Unchecked_Convert_To (Ftp, Relocate_Node (E2))));
943 end Expand_Fpt_Attribute_RR;
945 ---------------------------------
946 -- Expand_Loop_Entry_Attribute --
947 ---------------------------------
949 procedure Expand_Loop_Entry_Attribute (N : Node_Id) is
950 procedure Build_Conditional_Block
951 (Loc : Source_Ptr;
952 Cond : Node_Id;
953 Loop_Stmt : Node_Id;
954 If_Stmt : out Node_Id;
955 Blk_Stmt : out Node_Id);
956 -- Create a block Blk_Stmt with an empty declarative list and a single
957 -- loop Loop_Stmt. The block is encased in an if statement If_Stmt with
958 -- condition Cond. If_Stmt is Empty when there is no condition provided.
960 function Is_Array_Iteration (N : Node_Id) return Boolean;
961 -- Determine whether loop statement N denotes an Ada 2012 iteration over
962 -- an array object.
964 -----------------------------
965 -- Build_Conditional_Block --
966 -----------------------------
968 procedure Build_Conditional_Block
969 (Loc : Source_Ptr;
970 Cond : Node_Id;
971 Loop_Stmt : Node_Id;
972 If_Stmt : out Node_Id;
973 Blk_Stmt : out Node_Id)
975 begin
976 -- Do not reanalyze the original loop statement because it is simply
977 -- being relocated.
979 Set_Analyzed (Loop_Stmt);
981 Blk_Stmt :=
982 Make_Block_Statement (Loc,
983 Declarations => New_List,
984 Handled_Statement_Sequence =>
985 Make_Handled_Sequence_Of_Statements (Loc,
986 Statements => New_List (Loop_Stmt)));
988 if Present (Cond) then
989 If_Stmt :=
990 Make_If_Statement (Loc,
991 Condition => Cond,
992 Then_Statements => New_List (Blk_Stmt));
993 else
994 If_Stmt := Empty;
995 end if;
996 end Build_Conditional_Block;
998 ------------------------
999 -- Is_Array_Iteration --
1000 ------------------------
1002 function Is_Array_Iteration (N : Node_Id) return Boolean is
1003 Stmt : constant Node_Id := Original_Node (N);
1004 Iter : Node_Id;
1006 begin
1007 if Nkind (Stmt) = N_Loop_Statement
1008 and then Present (Iteration_Scheme (Stmt))
1009 and then Present (Iterator_Specification (Iteration_Scheme (Stmt)))
1010 then
1011 Iter := Iterator_Specification (Iteration_Scheme (Stmt));
1013 return
1014 Of_Present (Iter) and then Is_Array_Type (Etype (Name (Iter)));
1015 end if;
1017 return False;
1018 end Is_Array_Iteration;
1020 -- Local variables
1022 Exprs : constant List_Id := Expressions (N);
1023 Pref : constant Node_Id := Prefix (N);
1024 Typ : constant Entity_Id := Etype (Pref);
1025 Blk : Node_Id;
1026 CW_Decl : Node_Id;
1027 CW_Temp : Entity_Id;
1028 CW_Typ : Entity_Id;
1029 Decls : List_Id;
1030 Installed : Boolean;
1031 Loc : Source_Ptr;
1032 Loop_Id : Entity_Id;
1033 Loop_Stmt : Node_Id;
1034 Result : Node_Id;
1035 Scheme : Node_Id;
1036 Temp_Decl : Node_Id;
1037 Temp_Id : Entity_Id;
1039 -- Start of processing for Expand_Loop_Entry_Attribute
1041 begin
1042 -- Step 1: Find the related loop
1044 -- The loop label variant of attribute 'Loop_Entry already has all the
1045 -- information in its expression.
1047 if Present (Exprs) then
1048 Loop_Id := Entity (First (Exprs));
1049 Loop_Stmt := Label_Construct (Parent (Loop_Id));
1051 -- Climb the parent chain to find the nearest enclosing loop. Skip all
1052 -- internally generated loops for quantified expressions and for
1053 -- element iterators over multidimensional arrays: pragma applies to
1054 -- source loop.
1056 else
1057 Loop_Stmt := N;
1058 while Present (Loop_Stmt) loop
1059 if Nkind (Loop_Stmt) = N_Loop_Statement
1060 and then Comes_From_Source (Loop_Stmt)
1061 then
1062 exit;
1063 end if;
1065 Loop_Stmt := Parent (Loop_Stmt);
1066 end loop;
1068 Loop_Id := Entity (Identifier (Loop_Stmt));
1069 end if;
1071 Loc := Sloc (Loop_Stmt);
1073 -- Step 2: Transform the loop
1075 -- The loop has already been transformed during the expansion of a prior
1076 -- 'Loop_Entry attribute. Retrieve the declarative list of the block.
1078 if Has_Loop_Entry_Attributes (Loop_Id) then
1080 -- When the related loop name appears as the argument of attribute
1081 -- Loop_Entry, the corresponding label construct is the generated
1082 -- block statement. This is because the expander reuses the label.
1084 if Nkind (Loop_Stmt) = N_Block_Statement then
1085 Decls := Declarations (Loop_Stmt);
1087 -- In all other cases, the loop must appear in the handled sequence
1088 -- of statements of the generated block.
1090 else
1091 pragma Assert
1092 (Nkind (Parent (Loop_Stmt)) = N_Handled_Sequence_Of_Statements
1093 and then
1094 Nkind (Parent (Parent (Loop_Stmt))) = N_Block_Statement);
1096 Decls := Declarations (Parent (Parent (Loop_Stmt)));
1097 end if;
1099 Result := Empty;
1101 -- Transform the loop into a conditional block
1103 else
1104 Set_Has_Loop_Entry_Attributes (Loop_Id);
1105 Scheme := Iteration_Scheme (Loop_Stmt);
1107 -- Infinite loops are transformed into:
1109 -- declare
1110 -- Temp1 : constant <type of Pref1> := <Pref1>;
1111 -- . . .
1112 -- TempN : constant <type of PrefN> := <PrefN>;
1113 -- begin
1114 -- loop
1115 -- <original source statements with attribute rewrites>
1116 -- end loop;
1117 -- end;
1119 if No (Scheme) then
1120 Build_Conditional_Block (Loc,
1121 Cond => Empty,
1122 Loop_Stmt => Relocate_Node (Loop_Stmt),
1123 If_Stmt => Result,
1124 Blk_Stmt => Blk);
1126 Result := Blk;
1128 -- While loops are transformed into:
1130 -- function Fnn return Boolean is
1131 -- begin
1132 -- <condition actions>
1133 -- return <condition>;
1134 -- end Fnn;
1136 -- if Fnn then
1137 -- declare
1138 -- Temp1 : constant <type of Pref1> := <Pref1>;
1139 -- . . .
1140 -- TempN : constant <type of PrefN> := <PrefN>;
1141 -- begin
1142 -- loop
1143 -- <original source statements with attribute rewrites>
1144 -- exit when not Fnn;
1145 -- end loop;
1146 -- end;
1147 -- end if;
1149 -- Note that loops over iterators and containers are already
1150 -- converted into while loops.
1152 elsif Present (Condition (Scheme)) then
1153 declare
1154 Func_Decl : Node_Id;
1155 Func_Id : Entity_Id;
1156 Stmts : List_Id;
1158 begin
1159 -- Wrap the condition of the while loop in a Boolean function.
1160 -- This avoids the duplication of the same code which may lead
1161 -- to gigi issues with respect to multiple declaration of the
1162 -- same entity in the presence of side effects or checks. Note
1163 -- that the condition actions must also be relocated to the
1164 -- wrapping function.
1166 -- Generate:
1167 -- <condition actions>
1168 -- return <condition>;
1170 if Present (Condition_Actions (Scheme)) then
1171 Stmts := Condition_Actions (Scheme);
1172 else
1173 Stmts := New_List;
1174 end if;
1176 Append_To (Stmts,
1177 Make_Simple_Return_Statement (Loc,
1178 Expression => Relocate_Node (Condition (Scheme))));
1180 -- Generate:
1181 -- function Fnn return Boolean is
1182 -- begin
1183 -- <Stmts>
1184 -- end Fnn;
1186 Func_Id := Make_Temporary (Loc, 'F');
1187 Func_Decl :=
1188 Make_Subprogram_Body (Loc,
1189 Specification =>
1190 Make_Function_Specification (Loc,
1191 Defining_Unit_Name => Func_Id,
1192 Result_Definition =>
1193 New_Occurrence_Of (Standard_Boolean, Loc)),
1194 Declarations => Empty_List,
1195 Handled_Statement_Sequence =>
1196 Make_Handled_Sequence_Of_Statements (Loc,
1197 Statements => Stmts));
1199 -- The function is inserted before the related loop. Make sure
1200 -- to analyze it in the context of the loop's enclosing scope.
1202 Push_Scope (Scope (Loop_Id));
1203 Insert_Action (Loop_Stmt, Func_Decl);
1204 Pop_Scope;
1206 -- Transform the original while loop into an infinite loop
1207 -- where the last statement checks the negated condition. This
1208 -- placement ensures that the condition will not be evaluated
1209 -- twice on the first iteration.
1211 Set_Iteration_Scheme (Loop_Stmt, Empty);
1212 Scheme := Empty;
1214 -- Generate:
1215 -- exit when not Fnn;
1217 Append_To (Statements (Loop_Stmt),
1218 Make_Exit_Statement (Loc,
1219 Condition =>
1220 Make_Op_Not (Loc,
1221 Right_Opnd =>
1222 Make_Function_Call (Loc,
1223 Name => New_Occurrence_Of (Func_Id, Loc)))));
1225 Build_Conditional_Block (Loc,
1226 Cond =>
1227 Make_Function_Call (Loc,
1228 Name => New_Occurrence_Of (Func_Id, Loc)),
1229 Loop_Stmt => Relocate_Node (Loop_Stmt),
1230 If_Stmt => Result,
1231 Blk_Stmt => Blk);
1232 end;
1234 -- Ada 2012 iteration over an array is transformed into:
1236 -- if <Array_Nam>'Length (1) > 0
1237 -- and then <Array_Nam>'Length (N) > 0
1238 -- then
1239 -- declare
1240 -- Temp1 : constant <type of Pref1> := <Pref1>;
1241 -- . . .
1242 -- TempN : constant <type of PrefN> := <PrefN>;
1243 -- begin
1244 -- for X in ... loop -- multiple loops depending on dims
1245 -- <original source statements with attribute rewrites>
1246 -- end loop;
1247 -- end;
1248 -- end if;
1250 elsif Is_Array_Iteration (Loop_Stmt) then
1251 declare
1252 Array_Nam : constant Entity_Id :=
1253 Entity (Name (Iterator_Specification
1254 (Iteration_Scheme (Original_Node (Loop_Stmt)))));
1255 Num_Dims : constant Pos :=
1256 Number_Dimensions (Etype (Array_Nam));
1257 Cond : Node_Id := Empty;
1258 Check : Node_Id;
1260 begin
1261 -- Generate a check which determines whether all dimensions of
1262 -- the array are non-null.
1264 for Dim in 1 .. Num_Dims loop
1265 Check :=
1266 Make_Op_Gt (Loc,
1267 Left_Opnd =>
1268 Make_Attribute_Reference (Loc,
1269 Prefix => New_Occurrence_Of (Array_Nam, Loc),
1270 Attribute_Name => Name_Length,
1271 Expressions => New_List (
1272 Make_Integer_Literal (Loc, Dim))),
1273 Right_Opnd =>
1274 Make_Integer_Literal (Loc, 0));
1276 if No (Cond) then
1277 Cond := Check;
1278 else
1279 Cond :=
1280 Make_And_Then (Loc,
1281 Left_Opnd => Cond,
1282 Right_Opnd => Check);
1283 end if;
1284 end loop;
1286 Build_Conditional_Block (Loc,
1287 Cond => Cond,
1288 Loop_Stmt => Relocate_Node (Loop_Stmt),
1289 If_Stmt => Result,
1290 Blk_Stmt => Blk);
1291 end;
1293 -- For loops are transformed into:
1295 -- if <Low> <= <High> then
1296 -- declare
1297 -- Temp1 : constant <type of Pref1> := <Pref1>;
1298 -- . . .
1299 -- TempN : constant <type of PrefN> := <PrefN>;
1300 -- begin
1301 -- for <Def_Id> in <Low> .. <High> loop
1302 -- <original source statements with attribute rewrites>
1303 -- end loop;
1304 -- end;
1305 -- end if;
1307 elsif Present (Loop_Parameter_Specification (Scheme)) then
1308 declare
1309 Loop_Spec : constant Node_Id :=
1310 Loop_Parameter_Specification (Scheme);
1311 Cond : Node_Id;
1312 Subt_Def : Node_Id;
1314 begin
1315 Subt_Def := Discrete_Subtype_Definition (Loop_Spec);
1317 -- When the loop iterates over a subtype indication with a
1318 -- range, use the low and high bounds of the subtype itself.
1320 if Nkind (Subt_Def) = N_Subtype_Indication then
1321 Subt_Def := Scalar_Range (Etype (Subt_Def));
1322 end if;
1324 pragma Assert (Nkind (Subt_Def) = N_Range);
1326 -- Generate
1327 -- Low <= High
1329 Cond :=
1330 Make_Op_Le (Loc,
1331 Left_Opnd => New_Copy_Tree (Low_Bound (Subt_Def)),
1332 Right_Opnd => New_Copy_Tree (High_Bound (Subt_Def)));
1334 Build_Conditional_Block (Loc,
1335 Cond => Cond,
1336 Loop_Stmt => Relocate_Node (Loop_Stmt),
1337 If_Stmt => Result,
1338 Blk_Stmt => Blk);
1339 end;
1340 end if;
1342 Decls := Declarations (Blk);
1343 end if;
1345 -- Step 3: Create a constant to capture the value of the prefix at the
1346 -- entry point into the loop.
1348 Temp_Id := Make_Temporary (Loc, 'P');
1350 -- Preserve the tag of the prefix by offering a specific view of the
1351 -- class-wide version of the prefix.
1353 if Is_Tagged_Type (Typ) then
1355 -- Generate:
1356 -- CW_Temp : constant Typ'Class := Typ'Class (Pref);
1358 CW_Temp := Make_Temporary (Loc, 'T');
1359 CW_Typ := Class_Wide_Type (Typ);
1361 CW_Decl :=
1362 Make_Object_Declaration (Loc,
1363 Defining_Identifier => CW_Temp,
1364 Constant_Present => True,
1365 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
1366 Expression =>
1367 Convert_To (CW_Typ, Relocate_Node (Pref)));
1368 Append_To (Decls, CW_Decl);
1370 -- Generate:
1371 -- Temp : Typ renames Typ (CW_Temp);
1373 Temp_Decl :=
1374 Make_Object_Renaming_Declaration (Loc,
1375 Defining_Identifier => Temp_Id,
1376 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
1377 Name =>
1378 Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc)));
1379 Append_To (Decls, Temp_Decl);
1381 -- Non-tagged case
1383 else
1384 CW_Decl := Empty;
1386 -- Generate:
1387 -- Temp : constant Typ := Pref;
1389 Temp_Decl :=
1390 Make_Object_Declaration (Loc,
1391 Defining_Identifier => Temp_Id,
1392 Constant_Present => True,
1393 Object_Definition => New_Occurrence_Of (Typ, Loc),
1394 Expression => Relocate_Node (Pref));
1395 Append_To (Decls, Temp_Decl);
1396 end if;
1398 -- Step 4: Analyze all bits
1400 Installed := Current_Scope = Scope (Loop_Id);
1402 -- Depending on the pracement of attribute 'Loop_Entry relative to the
1403 -- associated loop, ensure the proper visibility for analysis.
1405 if not Installed then
1406 Push_Scope (Scope (Loop_Id));
1407 end if;
1409 -- The analysis of the conditional block takes care of the constant
1410 -- declaration.
1412 if Present (Result) then
1413 Rewrite (Loop_Stmt, Result);
1414 Analyze (Loop_Stmt);
1416 -- The conditional block was analyzed when a previous 'Loop_Entry was
1417 -- expanded. There is no point in reanalyzing the block, simply analyze
1418 -- the declaration of the constant.
1420 else
1421 if Present (CW_Decl) then
1422 Analyze (CW_Decl);
1423 end if;
1425 Analyze (Temp_Decl);
1426 end if;
1428 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
1429 Analyze (N);
1431 if not Installed then
1432 Pop_Scope;
1433 end if;
1434 end Expand_Loop_Entry_Attribute;
1436 ------------------------------
1437 -- Expand_Min_Max_Attribute --
1438 ------------------------------
1440 procedure Expand_Min_Max_Attribute (N : Node_Id) is
1441 begin
1442 -- Min and Max are handled by the back end (except that static cases
1443 -- have already been evaluated during semantic processing, although the
1444 -- back end should not count on this). The one bit of special processing
1445 -- required in the normal case is that these two attributes typically
1446 -- generate conditionals in the code, so check the relevant restriction.
1448 Check_Restriction (No_Implicit_Conditionals, N);
1450 -- In Modify_Tree_For_C mode, we rewrite as an if expression
1452 if Modify_Tree_For_C then
1453 declare
1454 Loc : constant Source_Ptr := Sloc (N);
1455 Typ : constant Entity_Id := Etype (N);
1456 Expr : constant Node_Id := First (Expressions (N));
1457 Left : constant Node_Id := Relocate_Node (Expr);
1458 Right : constant Node_Id := Relocate_Node (Next (Expr));
1460 function Make_Compare (Left, Right : Node_Id) return Node_Id;
1461 -- Returns Left >= Right for Max, Left <= Right for Min
1463 ------------------
1464 -- Make_Compare --
1465 ------------------
1467 function Make_Compare (Left, Right : Node_Id) return Node_Id is
1468 begin
1469 if Attribute_Name (N) = Name_Max then
1470 return
1471 Make_Op_Ge (Loc,
1472 Left_Opnd => Left,
1473 Right_Opnd => Right);
1474 else
1475 return
1476 Make_Op_Le (Loc,
1477 Left_Opnd => Left,
1478 Right_Opnd => Right);
1479 end if;
1480 end Make_Compare;
1482 -- Start of processing for Min_Max
1484 begin
1485 -- If both Left and Right are side effect free, then we can just
1486 -- use Duplicate_Expr to duplicate the references and return
1488 -- (if Left >=|<= Right then Left else Right)
1490 if Side_Effect_Free (Left) and then Side_Effect_Free (Right) then
1491 Rewrite (N,
1492 Make_If_Expression (Loc,
1493 Expressions => New_List (
1494 Make_Compare (Left, Right),
1495 Duplicate_Subexpr_No_Checks (Left),
1496 Duplicate_Subexpr_No_Checks (Right))));
1498 -- Otherwise we generate declarations to capture the values.
1500 -- The translation is
1502 -- do
1503 -- T1 : constant typ := Left;
1504 -- T2 : constant typ := Right;
1505 -- in
1506 -- (if T1 >=|<= T2 then T1 else T2)
1507 -- end;
1509 else
1510 declare
1511 T1 : constant Entity_Id := Make_Temporary (Loc, 'T', Left);
1512 T2 : constant Entity_Id := Make_Temporary (Loc, 'T', Right);
1514 begin
1515 Rewrite (N,
1516 Make_Expression_With_Actions (Loc,
1517 Actions => New_List (
1518 Make_Object_Declaration (Loc,
1519 Defining_Identifier => T1,
1520 Constant_Present => True,
1521 Object_Definition =>
1522 New_Occurrence_Of (Etype (Left), Loc),
1523 Expression => Relocate_Node (Left)),
1525 Make_Object_Declaration (Loc,
1526 Defining_Identifier => T2,
1527 Constant_Present => True,
1528 Object_Definition =>
1529 New_Occurrence_Of (Etype (Right), Loc),
1530 Expression => Relocate_Node (Right))),
1532 Expression =>
1533 Make_If_Expression (Loc,
1534 Expressions => New_List (
1535 Make_Compare
1536 (New_Occurrence_Of (T1, Loc),
1537 New_Occurrence_Of (T2, Loc)),
1538 New_Occurrence_Of (T1, Loc),
1539 New_Occurrence_Of (T2, Loc)))));
1540 end;
1541 end if;
1543 Analyze_And_Resolve (N, Typ);
1544 end;
1545 end if;
1546 end Expand_Min_Max_Attribute;
1548 ----------------------------------
1549 -- Expand_N_Attribute_Reference --
1550 ----------------------------------
1552 procedure Expand_N_Attribute_Reference (N : Node_Id) is
1553 Loc : constant Source_Ptr := Sloc (N);
1554 Typ : constant Entity_Id := Etype (N);
1555 Btyp : constant Entity_Id := Base_Type (Typ);
1556 Pref : constant Node_Id := Prefix (N);
1557 Ptyp : constant Entity_Id := Etype (Pref);
1558 Exprs : constant List_Id := Expressions (N);
1559 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
1561 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
1562 -- Rewrites a stream attribute for Read, Write or Output with the
1563 -- procedure call. Pname is the entity for the procedure to call.
1565 ------------------------------
1566 -- Rewrite_Stream_Proc_Call --
1567 ------------------------------
1569 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
1570 Item : constant Node_Id := Next (First (Exprs));
1571 Formal : constant Entity_Id := Next_Formal (First_Formal (Pname));
1572 Formal_Typ : constant Entity_Id := Etype (Formal);
1573 Is_Written : constant Boolean := (Ekind (Formal) /= E_In_Parameter);
1575 begin
1576 -- The expansion depends on Item, the second actual, which is
1577 -- the object being streamed in or out.
1579 -- If the item is a component of a packed array type, and
1580 -- a conversion is needed on exit, we introduce a temporary to
1581 -- hold the value, because otherwise the packed reference will
1582 -- not be properly expanded.
1584 if Nkind (Item) = N_Indexed_Component
1585 and then Is_Packed (Base_Type (Etype (Prefix (Item))))
1586 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
1587 and then Is_Written
1588 then
1589 declare
1590 Temp : constant Entity_Id := Make_Temporary (Loc, 'V');
1591 Decl : Node_Id;
1592 Assn : Node_Id;
1594 begin
1595 Decl :=
1596 Make_Object_Declaration (Loc,
1597 Defining_Identifier => Temp,
1598 Object_Definition =>
1599 New_Occurrence_Of (Formal_Typ, Loc));
1600 Set_Etype (Temp, Formal_Typ);
1602 Assn :=
1603 Make_Assignment_Statement (Loc,
1604 Name => New_Copy_Tree (Item),
1605 Expression =>
1606 Unchecked_Convert_To
1607 (Etype (Item), New_Occurrence_Of (Temp, Loc)));
1609 Rewrite (Item, New_Occurrence_Of (Temp, Loc));
1610 Insert_Actions (N,
1611 New_List (
1612 Decl,
1613 Make_Procedure_Call_Statement (Loc,
1614 Name => New_Occurrence_Of (Pname, Loc),
1615 Parameter_Associations => Exprs),
1616 Assn));
1618 Rewrite (N, Make_Null_Statement (Loc));
1619 return;
1620 end;
1621 end if;
1623 -- For the class-wide dispatching cases, and for cases in which
1624 -- the base type of the second argument matches the base type of
1625 -- the corresponding formal parameter (that is to say the stream
1626 -- operation is not inherited), we are all set, and can use the
1627 -- argument unchanged.
1629 -- For all other cases we do an unchecked conversion of the second
1630 -- parameter to the type of the formal of the procedure we are
1631 -- calling. This deals with the private type cases, and with going
1632 -- to the root type as required in elementary type case.
1634 if not Is_Class_Wide_Type (Entity (Pref))
1635 and then not Is_Class_Wide_Type (Etype (Item))
1636 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
1637 then
1638 Rewrite (Item,
1639 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
1641 -- For untagged derived types set Assignment_OK, to prevent
1642 -- copies from being created when the unchecked conversion
1643 -- is expanded (which would happen in Remove_Side_Effects
1644 -- if Expand_N_Unchecked_Conversion were allowed to call
1645 -- Force_Evaluation). The copy could violate Ada semantics in
1646 -- cases such as an actual that is an out parameter. Note that
1647 -- this approach is also used in exp_ch7 for calls to controlled
1648 -- type operations to prevent problems with actuals wrapped in
1649 -- unchecked conversions.
1651 if Is_Untagged_Derivation (Etype (Expression (Item))) then
1652 Set_Assignment_OK (Item);
1653 end if;
1654 end if;
1656 -- The stream operation to call may be a renaming created by an
1657 -- attribute definition clause, and may not be frozen yet. Ensure
1658 -- that it has the necessary extra formals.
1660 if not Is_Frozen (Pname) then
1661 Create_Extra_Formals (Pname);
1662 end if;
1664 -- And now rewrite the call
1666 Rewrite (N,
1667 Make_Procedure_Call_Statement (Loc,
1668 Name => New_Occurrence_Of (Pname, Loc),
1669 Parameter_Associations => Exprs));
1671 Analyze (N);
1672 end Rewrite_Stream_Proc_Call;
1674 -- Start of processing for Expand_N_Attribute_Reference
1676 begin
1677 -- Do required validity checking, if enabled. Do not apply check to
1678 -- output parameters of an Asm instruction, since the value of this
1679 -- is not set till after the attribute has been elaborated, and do
1680 -- not apply the check to the arguments of a 'Read or 'Input attribute
1681 -- reference since the scalar argument is an OUT scalar.
1683 if Validity_Checks_On and then Validity_Check_Operands
1684 and then Id /= Attribute_Asm_Output
1685 and then Id /= Attribute_Read
1686 and then Id /= Attribute_Input
1687 then
1688 declare
1689 Expr : Node_Id;
1690 begin
1691 Expr := First (Expressions (N));
1692 while Present (Expr) loop
1693 Ensure_Valid (Expr);
1694 Next (Expr);
1695 end loop;
1696 end;
1697 end if;
1699 -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in-
1700 -- place function, then a temporary return object needs to be created
1701 -- and access to it must be passed to the function. Currently we limit
1702 -- such functions to those with inherently limited result subtypes, but
1703 -- eventually we plan to expand the functions that are treated as
1704 -- build-in-place to include other composite result types.
1706 if Ada_Version >= Ada_2005
1707 and then Is_Build_In_Place_Function_Call (Pref)
1708 then
1709 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
1710 end if;
1712 -- If prefix is a protected type name, this is a reference to the
1713 -- current instance of the type. For a component definition, nothing
1714 -- to do (expansion will occur in the init proc). In other contexts,
1715 -- rewrite into reference to current instance.
1717 if Is_Protected_Self_Reference (Pref)
1718 and then not
1719 (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint,
1720 N_Discriminant_Association)
1721 and then Nkind (Parent (Parent (Parent (Parent (N))))) =
1722 N_Component_Definition)
1724 -- No action needed for these attributes since the current instance
1725 -- will be rewritten to be the name of the _object parameter
1726 -- associated with the enclosing protected subprogram (see below).
1728 and then Id /= Attribute_Access
1729 and then Id /= Attribute_Unchecked_Access
1730 and then Id /= Attribute_Unrestricted_Access
1731 then
1732 Rewrite (Pref, Concurrent_Ref (Pref));
1733 Analyze (Pref);
1734 end if;
1736 -- Remaining processing depends on specific attribute
1738 -- Note: individual sections of the following case statement are
1739 -- allowed to assume there is no code after the case statement, and
1740 -- are legitimately allowed to execute return statements if they have
1741 -- nothing more to do.
1743 case Id is
1745 -- Attributes related to Ada 2012 iterators
1747 when Attribute_Constant_Indexing |
1748 Attribute_Default_Iterator |
1749 Attribute_Implicit_Dereference |
1750 Attribute_Iterable |
1751 Attribute_Iterator_Element |
1752 Attribute_Variable_Indexing =>
1753 null;
1755 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
1756 -- were already rejected by the parser. Thus they shouldn't appear here.
1758 when Internal_Attribute_Id =>
1759 raise Program_Error;
1761 ------------
1762 -- Access --
1763 ------------
1765 when Attribute_Access |
1766 Attribute_Unchecked_Access |
1767 Attribute_Unrestricted_Access =>
1769 Access_Cases : declare
1770 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
1771 Btyp_DDT : Entity_Id;
1773 function Enclosing_Object (N : Node_Id) return Node_Id;
1774 -- If N denotes a compound name (selected component, indexed
1775 -- component, or slice), returns the name of the outermost such
1776 -- enclosing object. Otherwise returns N. If the object is a
1777 -- renaming, then the renamed object is returned.
1779 ----------------------
1780 -- Enclosing_Object --
1781 ----------------------
1783 function Enclosing_Object (N : Node_Id) return Node_Id is
1784 Obj_Name : Node_Id;
1786 begin
1787 Obj_Name := N;
1788 while Nkind_In (Obj_Name, N_Selected_Component,
1789 N_Indexed_Component,
1790 N_Slice)
1791 loop
1792 Obj_Name := Prefix (Obj_Name);
1793 end loop;
1795 return Get_Referenced_Object (Obj_Name);
1796 end Enclosing_Object;
1798 -- Local declarations
1800 Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object);
1802 -- Start of processing for Access_Cases
1804 begin
1805 Btyp_DDT := Designated_Type (Btyp);
1807 -- Handle designated types that come from the limited view
1809 if From_Limited_With (Btyp_DDT)
1810 and then Has_Non_Limited_View (Btyp_DDT)
1811 then
1812 Btyp_DDT := Non_Limited_View (Btyp_DDT);
1813 end if;
1815 -- In order to improve the text of error messages, the designated
1816 -- type of access-to-subprogram itypes is set by the semantics as
1817 -- the associated subprogram entity (see sem_attr). Now we replace
1818 -- such node with the proper E_Subprogram_Type itype.
1820 if Id = Attribute_Unrestricted_Access
1821 and then Is_Subprogram (Directly_Designated_Type (Typ))
1822 then
1823 -- The following conditions ensure that this special management
1824 -- is done only for "Address!(Prim'Unrestricted_Access)" nodes.
1825 -- At this stage other cases in which the designated type is
1826 -- still a subprogram (instead of an E_Subprogram_Type) are
1827 -- wrong because the semantics must have overridden the type of
1828 -- the node with the type imposed by the context.
1830 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
1831 and then Etype (Parent (N)) = RTE (RE_Prim_Ptr)
1832 then
1833 Set_Etype (N, RTE (RE_Prim_Ptr));
1835 else
1836 declare
1837 Subp : constant Entity_Id :=
1838 Directly_Designated_Type (Typ);
1839 Etyp : Entity_Id;
1840 Extra : Entity_Id := Empty;
1841 New_Formal : Entity_Id;
1842 Old_Formal : Entity_Id := First_Formal (Subp);
1843 Subp_Typ : Entity_Id;
1845 begin
1846 Subp_Typ := Create_Itype (E_Subprogram_Type, N);
1847 Set_Etype (Subp_Typ, Etype (Subp));
1848 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
1850 if Present (Old_Formal) then
1851 New_Formal := New_Copy (Old_Formal);
1852 Set_First_Entity (Subp_Typ, New_Formal);
1854 loop
1855 Set_Scope (New_Formal, Subp_Typ);
1856 Etyp := Etype (New_Formal);
1858 -- Handle itypes. There is no need to duplicate
1859 -- here the itypes associated with record types
1860 -- (i.e the implicit full view of private types).
1862 if Is_Itype (Etyp)
1863 and then Ekind (Base_Type (Etyp)) /= E_Record_Type
1864 then
1865 Extra := New_Copy (Etyp);
1866 Set_Parent (Extra, New_Formal);
1867 Set_Etype (New_Formal, Extra);
1868 Set_Scope (Extra, Subp_Typ);
1869 end if;
1871 Extra := New_Formal;
1872 Next_Formal (Old_Formal);
1873 exit when No (Old_Formal);
1875 Set_Next_Entity (New_Formal,
1876 New_Copy (Old_Formal));
1877 Next_Entity (New_Formal);
1878 end loop;
1880 Set_Next_Entity (New_Formal, Empty);
1881 Set_Last_Entity (Subp_Typ, Extra);
1882 end if;
1884 -- Now that the explicit formals have been duplicated,
1885 -- any extra formals needed by the subprogram must be
1886 -- created.
1888 if Present (Extra) then
1889 Set_Extra_Formal (Extra, Empty);
1890 end if;
1892 Create_Extra_Formals (Subp_Typ);
1893 Set_Directly_Designated_Type (Typ, Subp_Typ);
1894 end;
1895 end if;
1896 end if;
1898 if Is_Access_Protected_Subprogram_Type (Btyp) then
1899 Expand_Access_To_Protected_Op (N, Pref, Typ);
1901 -- If prefix is a type name, this is a reference to the current
1902 -- instance of the type, within its initialization procedure.
1904 elsif Is_Entity_Name (Pref)
1905 and then Is_Type (Entity (Pref))
1906 then
1907 declare
1908 Par : Node_Id;
1909 Formal : Entity_Id;
1911 begin
1912 -- If the current instance name denotes a task type, then
1913 -- the access attribute is rewritten to be the name of the
1914 -- "_task" parameter associated with the task type's task
1915 -- procedure. An unchecked conversion is applied to ensure
1916 -- a type match in cases of expander-generated calls (e.g.
1917 -- init procs).
1919 if Is_Task_Type (Entity (Pref)) then
1920 Formal :=
1921 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
1922 while Present (Formal) loop
1923 exit when Chars (Formal) = Name_uTask;
1924 Next_Entity (Formal);
1925 end loop;
1927 pragma Assert (Present (Formal));
1929 Rewrite (N,
1930 Unchecked_Convert_To (Typ,
1931 New_Occurrence_Of (Formal, Loc)));
1932 Set_Etype (N, Typ);
1934 elsif Is_Protected_Type (Entity (Pref)) then
1936 -- No action needed for current instance located in a
1937 -- component definition (expansion will occur in the
1938 -- init proc)
1940 if Is_Protected_Type (Current_Scope) then
1941 null;
1943 -- If the current instance reference is located in a
1944 -- protected subprogram or entry then rewrite the access
1945 -- attribute to be the name of the "_object" parameter.
1946 -- An unchecked conversion is applied to ensure a type
1947 -- match in cases of expander-generated calls (e.g. init
1948 -- procs).
1950 -- The code may be nested in a block, so find enclosing
1951 -- scope that is a protected operation.
1953 else
1954 declare
1955 Subp : Entity_Id;
1957 begin
1958 Subp := Current_Scope;
1959 while Ekind_In (Subp, E_Loop, E_Block) loop
1960 Subp := Scope (Subp);
1961 end loop;
1963 Formal :=
1964 First_Entity
1965 (Protected_Body_Subprogram (Subp));
1967 -- For a protected subprogram the _Object parameter
1968 -- is the protected record, so we create an access
1969 -- to it. The _Object parameter of an entry is an
1970 -- address.
1972 if Ekind (Subp) = E_Entry then
1973 Rewrite (N,
1974 Unchecked_Convert_To (Typ,
1975 New_Occurrence_Of (Formal, Loc)));
1976 Set_Etype (N, Typ);
1978 else
1979 Rewrite (N,
1980 Unchecked_Convert_To (Typ,
1981 Make_Attribute_Reference (Loc,
1982 Attribute_Name => Name_Unrestricted_Access,
1983 Prefix =>
1984 New_Occurrence_Of (Formal, Loc))));
1985 Analyze_And_Resolve (N);
1986 end if;
1987 end;
1988 end if;
1990 -- The expression must appear in a default expression,
1991 -- (which in the initialization procedure is the right-hand
1992 -- side of an assignment), and not in a discriminant
1993 -- constraint.
1995 else
1996 Par := Parent (N);
1997 while Present (Par) loop
1998 exit when Nkind (Par) = N_Assignment_Statement;
2000 if Nkind (Par) = N_Component_Declaration then
2001 return;
2002 end if;
2004 Par := Parent (Par);
2005 end loop;
2007 if Present (Par) then
2008 Rewrite (N,
2009 Make_Attribute_Reference (Loc,
2010 Prefix => Make_Identifier (Loc, Name_uInit),
2011 Attribute_Name => Attribute_Name (N)));
2013 Analyze_And_Resolve (N, Typ);
2014 end if;
2015 end if;
2016 end;
2018 -- If the prefix of an Access attribute is a dereference of an
2019 -- access parameter (or a renaming of such a dereference, or a
2020 -- subcomponent of such a dereference) and the context is a
2021 -- general access type (including the type of an object or
2022 -- component with an access_definition, but not the anonymous
2023 -- type of an access parameter or access discriminant), then
2024 -- apply an accessibility check to the access parameter. We used
2025 -- to rewrite the access parameter as a type conversion, but that
2026 -- could only be done if the immediate prefix of the Access
2027 -- attribute was the dereference, and didn't handle cases where
2028 -- the attribute is applied to a subcomponent of the dereference,
2029 -- since there's generally no available, appropriate access type
2030 -- to convert to in that case. The attribute is passed as the
2031 -- point to insert the check, because the access parameter may
2032 -- come from a renaming, possibly in a different scope, and the
2033 -- check must be associated with the attribute itself.
2035 elsif Id = Attribute_Access
2036 and then Nkind (Enc_Object) = N_Explicit_Dereference
2037 and then Is_Entity_Name (Prefix (Enc_Object))
2038 and then (Ekind (Btyp) = E_General_Access_Type
2039 or else Is_Local_Anonymous_Access (Btyp))
2040 and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind
2041 and then Ekind (Etype (Entity (Prefix (Enc_Object))))
2042 = E_Anonymous_Access_Type
2043 and then Present (Extra_Accessibility
2044 (Entity (Prefix (Enc_Object))))
2045 then
2046 Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N);
2048 -- Ada 2005 (AI-251): If the designated type is an interface we
2049 -- add an implicit conversion to force the displacement of the
2050 -- pointer to reference the secondary dispatch table.
2052 elsif Is_Interface (Btyp_DDT)
2053 and then (Comes_From_Source (N)
2054 or else Comes_From_Source (Ref_Object)
2055 or else (Nkind (Ref_Object) in N_Has_Chars
2056 and then Chars (Ref_Object) = Name_uInit))
2057 then
2058 if Nkind (Ref_Object) /= N_Explicit_Dereference then
2060 -- No implicit conversion required if types match, or if
2061 -- the prefix is the class_wide_type of the interface. In
2062 -- either case passing an object of the interface type has
2063 -- already set the pointer correctly.
2065 if Btyp_DDT = Etype (Ref_Object)
2066 or else (Is_Class_Wide_Type (Etype (Ref_Object))
2067 and then
2068 Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object))
2069 then
2070 null;
2072 else
2073 Rewrite (Prefix (N),
2074 Convert_To (Btyp_DDT,
2075 New_Copy_Tree (Prefix (N))));
2077 Analyze_And_Resolve (Prefix (N), Btyp_DDT);
2078 end if;
2080 -- When the object is an explicit dereference, convert the
2081 -- dereference's prefix.
2083 else
2084 declare
2085 Obj_DDT : constant Entity_Id :=
2086 Base_Type
2087 (Directly_Designated_Type
2088 (Etype (Prefix (Ref_Object))));
2089 begin
2090 -- No implicit conversion required if designated types
2091 -- match, or if we have an unrestricted access.
2093 if Obj_DDT /= Btyp_DDT
2094 and then Id /= Attribute_Unrestricted_Access
2095 and then not (Is_Class_Wide_Type (Obj_DDT)
2096 and then Etype (Obj_DDT) = Btyp_DDT)
2097 then
2098 Rewrite (N,
2099 Convert_To (Typ,
2100 New_Copy_Tree (Prefix (Ref_Object))));
2101 Analyze_And_Resolve (N, Typ);
2102 end if;
2103 end;
2104 end if;
2105 end if;
2106 end Access_Cases;
2108 --------------
2109 -- Adjacent --
2110 --------------
2112 -- Transforms 'Adjacent into a call to the floating-point attribute
2113 -- function Adjacent in Fat_xxx (where xxx is the root type)
2115 when Attribute_Adjacent =>
2116 Expand_Fpt_Attribute_RR (N);
2118 -------------
2119 -- Address --
2120 -------------
2122 when Attribute_Address => Address : declare
2123 Task_Proc : Entity_Id;
2125 begin
2126 -- If the prefix is a task or a task type, the useful address is that
2127 -- of the procedure for the task body, i.e. the actual program unit.
2128 -- We replace the original entity with that of the procedure.
2130 if Is_Entity_Name (Pref)
2131 and then Is_Task_Type (Entity (Pref))
2132 then
2133 Task_Proc := Next_Entity (Root_Type (Ptyp));
2135 while Present (Task_Proc) loop
2136 exit when Ekind (Task_Proc) = E_Procedure
2137 and then Etype (First_Formal (Task_Proc)) =
2138 Corresponding_Record_Type (Ptyp);
2139 Next_Entity (Task_Proc);
2140 end loop;
2142 if Present (Task_Proc) then
2143 Set_Entity (Pref, Task_Proc);
2144 Set_Etype (Pref, Etype (Task_Proc));
2145 end if;
2147 -- Similarly, the address of a protected operation is the address
2148 -- of the corresponding protected body, regardless of the protected
2149 -- object from which it is selected.
2151 elsif Nkind (Pref) = N_Selected_Component
2152 and then Is_Subprogram (Entity (Selector_Name (Pref)))
2153 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
2154 then
2155 Rewrite (Pref,
2156 New_Occurrence_Of (
2157 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
2159 elsif Nkind (Pref) = N_Explicit_Dereference
2160 and then Ekind (Ptyp) = E_Subprogram_Type
2161 and then Convention (Ptyp) = Convention_Protected
2162 then
2163 -- The prefix is be a dereference of an access_to_protected_
2164 -- subprogram. The desired address is the second component of
2165 -- the record that represents the access.
2167 declare
2168 Addr : constant Entity_Id := Etype (N);
2169 Ptr : constant Node_Id := Prefix (Pref);
2170 T : constant Entity_Id :=
2171 Equivalent_Type (Base_Type (Etype (Ptr)));
2173 begin
2174 Rewrite (N,
2175 Unchecked_Convert_To (Addr,
2176 Make_Selected_Component (Loc,
2177 Prefix => Unchecked_Convert_To (T, Ptr),
2178 Selector_Name => New_Occurrence_Of (
2179 Next_Entity (First_Entity (T)), Loc))));
2181 Analyze_And_Resolve (N, Addr);
2182 end;
2184 -- Ada 2005 (AI-251): Class-wide interface objects are always
2185 -- "displaced" to reference the tag associated with the interface
2186 -- type. In order to obtain the real address of such objects we
2187 -- generate a call to a run-time subprogram that returns the base
2188 -- address of the object.
2190 -- This processing is not needed in the VM case, where dispatching
2191 -- issues are taken care of by the virtual machine.
2193 elsif Is_Class_Wide_Type (Ptyp)
2194 and then Is_Interface (Ptyp)
2195 and then Tagged_Type_Expansion
2196 and then not (Nkind (Pref) in N_Has_Entity
2197 and then Is_Subprogram (Entity (Pref)))
2198 then
2199 Rewrite (N,
2200 Make_Function_Call (Loc,
2201 Name => New_Occurrence_Of (RTE (RE_Base_Address), Loc),
2202 Parameter_Associations => New_List (
2203 Relocate_Node (N))));
2204 Analyze (N);
2205 return;
2206 end if;
2208 -- Deal with packed array reference, other cases are handled by
2209 -- the back end.
2211 if Involves_Packed_Array_Reference (Pref) then
2212 Expand_Packed_Address_Reference (N);
2213 end if;
2214 end Address;
2216 ---------------
2217 -- Alignment --
2218 ---------------
2220 when Attribute_Alignment => Alignment : declare
2221 New_Node : Node_Id;
2223 begin
2224 -- For class-wide types, X'Class'Alignment is transformed into a
2225 -- direct reference to the Alignment of the class type, so that the
2226 -- back end does not have to deal with the X'Class'Alignment
2227 -- reference.
2229 if Is_Entity_Name (Pref)
2230 and then Is_Class_Wide_Type (Entity (Pref))
2231 then
2232 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
2233 return;
2235 -- For x'Alignment applied to an object of a class wide type,
2236 -- transform X'Alignment into a call to the predefined primitive
2237 -- operation _Alignment applied to X.
2239 elsif Is_Class_Wide_Type (Ptyp) then
2240 New_Node :=
2241 Make_Attribute_Reference (Loc,
2242 Prefix => Pref,
2243 Attribute_Name => Name_Tag);
2245 New_Node := Build_Get_Alignment (Loc, New_Node);
2247 -- Case where the context is a specific integer type with which
2248 -- the original attribute was compatible. The function has a
2249 -- specific type as well, so to preserve the compatibility we
2250 -- must convert explicitly.
2252 if Typ /= Standard_Integer then
2253 New_Node := Convert_To (Typ, New_Node);
2254 end if;
2256 Rewrite (N, New_Node);
2257 Analyze_And_Resolve (N, Typ);
2258 return;
2260 -- For all other cases, we just have to deal with the case of
2261 -- the fact that the result can be universal.
2263 else
2264 Apply_Universal_Integer_Attribute_Checks (N);
2265 end if;
2266 end Alignment;
2268 ---------
2269 -- Bit --
2270 ---------
2272 -- We compute this if a packed array reference was present, otherwise we
2273 -- leave the computation up to the back end.
2275 when Attribute_Bit =>
2276 if Involves_Packed_Array_Reference (Pref) then
2277 Expand_Packed_Bit_Reference (N);
2278 else
2279 Apply_Universal_Integer_Attribute_Checks (N);
2280 end if;
2282 ------------------
2283 -- Bit_Position --
2284 ------------------
2286 -- We compute this if a component clause was present, otherwise we leave
2287 -- the computation up to the back end, since we don't know what layout
2288 -- will be chosen.
2290 -- Note that the attribute can apply to a naked record component
2291 -- in generated code (i.e. the prefix is an identifier that
2292 -- references the component or discriminant entity).
2294 when Attribute_Bit_Position => Bit_Position : declare
2295 CE : Entity_Id;
2297 begin
2298 if Nkind (Pref) = N_Identifier then
2299 CE := Entity (Pref);
2300 else
2301 CE := Entity (Selector_Name (Pref));
2302 end if;
2304 if Known_Static_Component_Bit_Offset (CE) then
2305 Rewrite (N,
2306 Make_Integer_Literal (Loc,
2307 Intval => Component_Bit_Offset (CE)));
2308 Analyze_And_Resolve (N, Typ);
2310 else
2311 Apply_Universal_Integer_Attribute_Checks (N);
2312 end if;
2313 end Bit_Position;
2315 ------------------
2316 -- Body_Version --
2317 ------------------
2319 -- A reference to P'Body_Version or P'Version is expanded to
2321 -- Vnn : Unsigned;
2322 -- pragma Import (C, Vnn, "uuuuT");
2323 -- ...
2324 -- Get_Version_String (Vnn)
2326 -- where uuuu is the unit name (dots replaced by double underscore)
2327 -- and T is B for the cases of Body_Version, or Version applied to a
2328 -- subprogram acting as its own spec, and S for Version applied to a
2329 -- subprogram spec or package. This sequence of code references the
2330 -- unsigned constant created in the main program by the binder.
2332 -- A special exception occurs for Standard, where the string returned
2333 -- is a copy of the library string in gnatvsn.ads.
2335 when Attribute_Body_Version | Attribute_Version => Version : declare
2336 E : constant Entity_Id := Make_Temporary (Loc, 'V');
2337 Pent : Entity_Id;
2338 S : String_Id;
2340 begin
2341 -- If not library unit, get to containing library unit
2343 Pent := Entity (Pref);
2344 while Pent /= Standard_Standard
2345 and then Scope (Pent) /= Standard_Standard
2346 and then not Is_Child_Unit (Pent)
2347 loop
2348 Pent := Scope (Pent);
2349 end loop;
2351 -- Special case Standard and Standard.ASCII
2353 if Pent = Standard_Standard or else Pent = Standard_ASCII then
2354 Rewrite (N,
2355 Make_String_Literal (Loc,
2356 Strval => Verbose_Library_Version));
2358 -- All other cases
2360 else
2361 -- Build required string constant
2363 Get_Name_String (Get_Unit_Name (Pent));
2365 Start_String;
2366 for J in 1 .. Name_Len - 2 loop
2367 if Name_Buffer (J) = '.' then
2368 Store_String_Chars ("__");
2369 else
2370 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
2371 end if;
2372 end loop;
2374 -- Case of subprogram acting as its own spec, always use body
2376 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
2377 and then Nkind (Parent (Declaration_Node (Pent))) =
2378 N_Subprogram_Body
2379 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
2380 then
2381 Store_String_Chars ("B");
2383 -- Case of no body present, always use spec
2385 elsif not Unit_Requires_Body (Pent) then
2386 Store_String_Chars ("S");
2388 -- Otherwise use B for Body_Version, S for spec
2390 elsif Id = Attribute_Body_Version then
2391 Store_String_Chars ("B");
2392 else
2393 Store_String_Chars ("S");
2394 end if;
2396 S := End_String;
2397 Lib.Version_Referenced (S);
2399 -- Insert the object declaration
2401 Insert_Actions (N, New_List (
2402 Make_Object_Declaration (Loc,
2403 Defining_Identifier => E,
2404 Object_Definition =>
2405 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
2407 -- Set entity as imported with correct external name
2409 Set_Is_Imported (E);
2410 Set_Interface_Name (E, Make_String_Literal (Loc, S));
2412 -- Set entity as internal to ensure proper Sprint output of its
2413 -- implicit importation.
2415 Set_Is_Internal (E);
2417 -- And now rewrite original reference
2419 Rewrite (N,
2420 Make_Function_Call (Loc,
2421 Name => New_Occurrence_Of (RTE (RE_Get_Version_String), Loc),
2422 Parameter_Associations => New_List (
2423 New_Occurrence_Of (E, Loc))));
2424 end if;
2426 Analyze_And_Resolve (N, RTE (RE_Version_String));
2427 end Version;
2429 -------------
2430 -- Ceiling --
2431 -------------
2433 -- Transforms 'Ceiling into a call to the floating-point attribute
2434 -- function Ceiling in Fat_xxx (where xxx is the root type)
2436 when Attribute_Ceiling =>
2437 Expand_Fpt_Attribute_R (N);
2439 --------------
2440 -- Callable --
2441 --------------
2443 -- Transforms 'Callable attribute into a call to the Callable function
2445 when Attribute_Callable => Callable :
2446 begin
2447 -- We have an object of a task interface class-wide type as a prefix
2448 -- to Callable. Generate:
2449 -- callable (Task_Id (Pref._disp_get_task_id));
2451 if Ada_Version >= Ada_2005
2452 and then Ekind (Ptyp) = E_Class_Wide_Type
2453 and then Is_Interface (Ptyp)
2454 and then Is_Task_Interface (Ptyp)
2455 then
2456 Rewrite (N,
2457 Make_Function_Call (Loc,
2458 Name =>
2459 New_Occurrence_Of (RTE (RE_Callable), Loc),
2460 Parameter_Associations => New_List (
2461 Make_Unchecked_Type_Conversion (Loc,
2462 Subtype_Mark =>
2463 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
2464 Expression =>
2465 Make_Selected_Component (Loc,
2466 Prefix =>
2467 New_Copy_Tree (Pref),
2468 Selector_Name =>
2469 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
2471 else
2472 Rewrite (N,
2473 Build_Call_With_Task (Pref, RTE (RE_Callable)));
2474 end if;
2476 Analyze_And_Resolve (N, Standard_Boolean);
2477 end Callable;
2479 ------------
2480 -- Caller --
2481 ------------
2483 -- Transforms 'Caller attribute into a call to either the
2484 -- Task_Entry_Caller or the Protected_Entry_Caller function.
2486 when Attribute_Caller => Caller : declare
2487 Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id);
2488 Ent : constant Entity_Id := Entity (Pref);
2489 Conctype : constant Entity_Id := Scope (Ent);
2490 Nest_Depth : Integer := 0;
2491 Name : Node_Id;
2492 S : Entity_Id;
2494 begin
2495 -- Protected case
2497 if Is_Protected_Type (Conctype) then
2498 case Corresponding_Runtime_Package (Conctype) is
2499 when System_Tasking_Protected_Objects_Entries =>
2500 Name :=
2501 New_Occurrence_Of
2502 (RTE (RE_Protected_Entry_Caller), Loc);
2504 when System_Tasking_Protected_Objects_Single_Entry =>
2505 Name :=
2506 New_Occurrence_Of
2507 (RTE (RE_Protected_Single_Entry_Caller), Loc);
2509 when others =>
2510 raise Program_Error;
2511 end case;
2513 Rewrite (N,
2514 Unchecked_Convert_To (Id_Kind,
2515 Make_Function_Call (Loc,
2516 Name => Name,
2517 Parameter_Associations => New_List (
2518 New_Occurrence_Of
2519 (Find_Protection_Object (Current_Scope), Loc)))));
2521 -- Task case
2523 else
2524 -- Determine the nesting depth of the E'Caller attribute, that
2525 -- is, how many accept statements are nested within the accept
2526 -- statement for E at the point of E'Caller. The runtime uses
2527 -- this depth to find the specified entry call.
2529 for J in reverse 0 .. Scope_Stack.Last loop
2530 S := Scope_Stack.Table (J).Entity;
2532 -- We should not reach the scope of the entry, as it should
2533 -- already have been checked in Sem_Attr that this attribute
2534 -- reference is within a matching accept statement.
2536 pragma Assert (S /= Conctype);
2538 if S = Ent then
2539 exit;
2541 elsif Is_Entry (S) then
2542 Nest_Depth := Nest_Depth + 1;
2543 end if;
2544 end loop;
2546 Rewrite (N,
2547 Unchecked_Convert_To (Id_Kind,
2548 Make_Function_Call (Loc,
2549 Name =>
2550 New_Occurrence_Of (RTE (RE_Task_Entry_Caller), Loc),
2551 Parameter_Associations => New_List (
2552 Make_Integer_Literal (Loc,
2553 Intval => Int (Nest_Depth))))));
2554 end if;
2556 Analyze_And_Resolve (N, Id_Kind);
2557 end Caller;
2559 -------------
2560 -- Compose --
2561 -------------
2563 -- Transforms 'Compose into a call to the floating-point attribute
2564 -- function Compose in Fat_xxx (where xxx is the root type)
2566 -- Note: we strictly should have special code here to deal with the
2567 -- case of absurdly negative arguments (less than Integer'First)
2568 -- which will return a (signed) zero value, but it hardly seems
2569 -- worth the effort. Absurdly large positive arguments will raise
2570 -- constraint error which is fine.
2572 when Attribute_Compose =>
2573 Expand_Fpt_Attribute_RI (N);
2575 -----------------
2576 -- Constrained --
2577 -----------------
2579 when Attribute_Constrained => Constrained : declare
2580 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
2582 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
2583 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
2584 -- view of an aliased object whose subtype is constrained.
2586 ---------------------------------
2587 -- Is_Constrained_Aliased_View --
2588 ---------------------------------
2590 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
2591 E : Entity_Id;
2593 begin
2594 if Is_Entity_Name (Obj) then
2595 E := Entity (Obj);
2597 if Present (Renamed_Object (E)) then
2598 return Is_Constrained_Aliased_View (Renamed_Object (E));
2599 else
2600 return Is_Aliased (E) and then Is_Constrained (Etype (E));
2601 end if;
2603 else
2604 return Is_Aliased_View (Obj)
2605 and then
2606 (Is_Constrained (Etype (Obj))
2607 or else
2608 (Nkind (Obj) = N_Explicit_Dereference
2609 and then
2610 not Object_Type_Has_Constrained_Partial_View
2611 (Typ => Base_Type (Etype (Obj)),
2612 Scop => Current_Scope)));
2613 end if;
2614 end Is_Constrained_Aliased_View;
2616 -- Start of processing for Constrained
2618 begin
2619 -- Reference to a parameter where the value is passed as an extra
2620 -- actual, corresponding to the extra formal referenced by the
2621 -- Extra_Constrained field of the corresponding formal. If this
2622 -- is an entry in-parameter, it is replaced by a constant renaming
2623 -- for which Extra_Constrained is never created.
2625 if Present (Formal_Ent)
2626 and then Ekind (Formal_Ent) /= E_Constant
2627 and then Present (Extra_Constrained (Formal_Ent))
2628 then
2629 Rewrite (N,
2630 New_Occurrence_Of
2631 (Extra_Constrained (Formal_Ent), Sloc (N)));
2633 -- For variables with a Extra_Constrained field, we use the
2634 -- corresponding entity.
2636 elsif Nkind (Pref) = N_Identifier
2637 and then Ekind (Entity (Pref)) = E_Variable
2638 and then Present (Extra_Constrained (Entity (Pref)))
2639 then
2640 Rewrite (N,
2641 New_Occurrence_Of
2642 (Extra_Constrained (Entity (Pref)), Sloc (N)));
2644 -- For all other entity names, we can tell at compile time
2646 elsif Is_Entity_Name (Pref) then
2647 declare
2648 Ent : constant Entity_Id := Entity (Pref);
2649 Res : Boolean;
2651 begin
2652 -- (RM J.4) obsolescent cases
2654 if Is_Type (Ent) then
2656 -- Private type
2658 if Is_Private_Type (Ent) then
2659 Res := not Has_Discriminants (Ent)
2660 or else Is_Constrained (Ent);
2662 -- It not a private type, must be a generic actual type
2663 -- that corresponded to a private type. We know that this
2664 -- correspondence holds, since otherwise the reference
2665 -- within the generic template would have been illegal.
2667 else
2668 if Is_Composite_Type (Underlying_Type (Ent)) then
2669 Res := Is_Constrained (Ent);
2670 else
2671 Res := True;
2672 end if;
2673 end if;
2675 -- If the prefix is not a variable or is aliased, then
2676 -- definitely true; if it's a formal parameter without an
2677 -- associated extra formal, then treat it as constrained.
2679 -- Ada 2005 (AI-363): An aliased prefix must be known to be
2680 -- constrained in order to set the attribute to True.
2682 elsif not Is_Variable (Pref)
2683 or else Present (Formal_Ent)
2684 or else (Ada_Version < Ada_2005
2685 and then Is_Aliased_View (Pref))
2686 or else (Ada_Version >= Ada_2005
2687 and then Is_Constrained_Aliased_View (Pref))
2688 then
2689 Res := True;
2691 -- Variable case, look at type to see if it is constrained.
2692 -- Note that the one case where this is not accurate (the
2693 -- procedure formal case), has been handled above.
2695 -- We use the Underlying_Type here (and below) in case the
2696 -- type is private without discriminants, but the full type
2697 -- has discriminants. This case is illegal, but we generate it
2698 -- internally for passing to the Extra_Constrained parameter.
2700 else
2701 -- In Ada 2012, test for case of a limited tagged type, in
2702 -- which case the attribute is always required to return
2703 -- True. The underlying type is tested, to make sure we also
2704 -- return True for cases where there is an unconstrained
2705 -- object with an untagged limited partial view which has
2706 -- defaulted discriminants (such objects always produce a
2707 -- False in earlier versions of Ada). (Ada 2012: AI05-0214)
2709 Res := Is_Constrained (Underlying_Type (Etype (Ent)))
2710 or else
2711 (Ada_Version >= Ada_2012
2712 and then Is_Tagged_Type (Underlying_Type (Ptyp))
2713 and then Is_Limited_Type (Ptyp));
2714 end if;
2716 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Res), Loc));
2717 end;
2719 -- Prefix is not an entity name. These are also cases where we can
2720 -- always tell at compile time by looking at the form and type of the
2721 -- prefix. If an explicit dereference of an object with constrained
2722 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
2723 -- underlying type is a limited tagged type, then Constrained is
2724 -- required to always return True (Ada 2012: AI05-0214).
2726 else
2727 Rewrite (N,
2728 New_Occurrence_Of (
2729 Boolean_Literals (
2730 not Is_Variable (Pref)
2731 or else
2732 (Nkind (Pref) = N_Explicit_Dereference
2733 and then
2734 not Object_Type_Has_Constrained_Partial_View
2735 (Typ => Base_Type (Ptyp),
2736 Scop => Current_Scope))
2737 or else Is_Constrained (Underlying_Type (Ptyp))
2738 or else (Ada_Version >= Ada_2012
2739 and then Is_Tagged_Type (Underlying_Type (Ptyp))
2740 and then Is_Limited_Type (Ptyp))),
2741 Loc));
2742 end if;
2744 Analyze_And_Resolve (N, Standard_Boolean);
2745 end Constrained;
2747 ---------------
2748 -- Copy_Sign --
2749 ---------------
2751 -- Transforms 'Copy_Sign into a call to the floating-point attribute
2752 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
2754 when Attribute_Copy_Sign =>
2755 Expand_Fpt_Attribute_RR (N);
2757 -----------
2758 -- Count --
2759 -----------
2761 -- Transforms 'Count attribute into a call to the Count function
2763 when Attribute_Count => Count : declare
2764 Call : Node_Id;
2765 Conctyp : Entity_Id;
2766 Entnam : Node_Id;
2767 Entry_Id : Entity_Id;
2768 Index : Node_Id;
2769 Name : Node_Id;
2771 begin
2772 -- If the prefix is a member of an entry family, retrieve both
2773 -- entry name and index. For a simple entry there is no index.
2775 if Nkind (Pref) = N_Indexed_Component then
2776 Entnam := Prefix (Pref);
2777 Index := First (Expressions (Pref));
2778 else
2779 Entnam := Pref;
2780 Index := Empty;
2781 end if;
2783 Entry_Id := Entity (Entnam);
2785 -- Find the concurrent type in which this attribute is referenced
2786 -- (there had better be one).
2788 Conctyp := Current_Scope;
2789 while not Is_Concurrent_Type (Conctyp) loop
2790 Conctyp := Scope (Conctyp);
2791 end loop;
2793 -- Protected case
2795 if Is_Protected_Type (Conctyp) then
2796 case Corresponding_Runtime_Package (Conctyp) is
2797 when System_Tasking_Protected_Objects_Entries =>
2798 Name := New_Occurrence_Of (RTE (RE_Protected_Count), Loc);
2800 Call :=
2801 Make_Function_Call (Loc,
2802 Name => Name,
2803 Parameter_Associations => New_List (
2804 New_Occurrence_Of
2805 (Find_Protection_Object (Current_Scope), Loc),
2806 Entry_Index_Expression
2807 (Loc, Entry_Id, Index, Scope (Entry_Id))));
2809 when System_Tasking_Protected_Objects_Single_Entry =>
2810 Name :=
2811 New_Occurrence_Of (RTE (RE_Protected_Count_Entry), Loc);
2813 Call :=
2814 Make_Function_Call (Loc,
2815 Name => Name,
2816 Parameter_Associations => New_List (
2817 New_Occurrence_Of
2818 (Find_Protection_Object (Current_Scope), Loc)));
2820 when others =>
2821 raise Program_Error;
2822 end case;
2824 -- Task case
2826 else
2827 Call :=
2828 Make_Function_Call (Loc,
2829 Name => New_Occurrence_Of (RTE (RE_Task_Count), Loc),
2830 Parameter_Associations => New_List (
2831 Entry_Index_Expression (Loc,
2832 Entry_Id, Index, Scope (Entry_Id))));
2833 end if;
2835 -- The call returns type Natural but the context is universal integer
2836 -- so any integer type is allowed. The attribute was already resolved
2837 -- so its Etype is the required result type. If the base type of the
2838 -- context type is other than Standard.Integer we put in a conversion
2839 -- to the required type. This can be a normal typed conversion since
2840 -- both input and output types of the conversion are integer types
2842 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
2843 Rewrite (N, Convert_To (Typ, Call));
2844 else
2845 Rewrite (N, Call);
2846 end if;
2848 Analyze_And_Resolve (N, Typ);
2849 end Count;
2851 ---------------------
2852 -- Descriptor_Size --
2853 ---------------------
2855 when Attribute_Descriptor_Size =>
2857 -- Attribute Descriptor_Size is handled by the back end when applied
2858 -- to an unconstrained array type.
2860 if Is_Array_Type (Ptyp)
2861 and then not Is_Constrained (Ptyp)
2862 then
2863 Apply_Universal_Integer_Attribute_Checks (N);
2865 -- For any other type, the descriptor size is 0 because there is no
2866 -- actual descriptor, but the result is not formally static.
2868 else
2869 Rewrite (N, Make_Integer_Literal (Loc, 0));
2870 Analyze (N);
2871 Set_Is_Static_Expression (N, False);
2872 end if;
2874 ---------------
2875 -- Elab_Body --
2876 ---------------
2878 -- This processing is shared by Elab_Spec
2880 -- What we do is to insert the following declarations
2882 -- procedure tnn;
2883 -- pragma Import (C, enn, "name___elabb/s");
2885 -- and then the Elab_Body/Spec attribute is replaced by a reference
2886 -- to this defining identifier.
2888 when Attribute_Elab_Body |
2889 Attribute_Elab_Spec =>
2891 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
2892 -- back-end knows how to handle these attributes directly.
2894 if CodePeer_Mode then
2895 return;
2896 end if;
2898 Elab_Body : declare
2899 Ent : constant Entity_Id := Make_Temporary (Loc, 'E');
2900 Str : String_Id;
2901 Lang : Node_Id;
2903 procedure Make_Elab_String (Nod : Node_Id);
2904 -- Given Nod, an identifier, or a selected component, put the
2905 -- image into the current string literal, with double underline
2906 -- between components.
2908 ----------------------
2909 -- Make_Elab_String --
2910 ----------------------
2912 procedure Make_Elab_String (Nod : Node_Id) is
2913 begin
2914 if Nkind (Nod) = N_Selected_Component then
2915 Make_Elab_String (Prefix (Nod));
2916 Store_String_Char ('_');
2917 Store_String_Char ('_');
2918 Get_Name_String (Chars (Selector_Name (Nod)));
2920 else
2921 pragma Assert (Nkind (Nod) = N_Identifier);
2922 Get_Name_String (Chars (Nod));
2923 end if;
2925 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2926 end Make_Elab_String;
2928 -- Start of processing for Elab_Body/Elab_Spec
2930 begin
2931 -- First we need to prepare the string literal for the name of
2932 -- the elaboration routine to be referenced.
2934 Start_String;
2935 Make_Elab_String (Pref);
2936 Store_String_Chars ("___elab");
2937 Lang := Make_Identifier (Loc, Name_C);
2939 if Id = Attribute_Elab_Body then
2940 Store_String_Char ('b');
2941 else
2942 Store_String_Char ('s');
2943 end if;
2945 Str := End_String;
2947 Insert_Actions (N, New_List (
2948 Make_Subprogram_Declaration (Loc,
2949 Specification =>
2950 Make_Procedure_Specification (Loc,
2951 Defining_Unit_Name => Ent)),
2953 Make_Pragma (Loc,
2954 Chars => Name_Import,
2955 Pragma_Argument_Associations => New_List (
2956 Make_Pragma_Argument_Association (Loc, Expression => Lang),
2958 Make_Pragma_Argument_Association (Loc,
2959 Expression => Make_Identifier (Loc, Chars (Ent))),
2961 Make_Pragma_Argument_Association (Loc,
2962 Expression => Make_String_Literal (Loc, Str))))));
2964 Set_Entity (N, Ent);
2965 Rewrite (N, New_Occurrence_Of (Ent, Loc));
2966 end Elab_Body;
2968 --------------------
2969 -- Elab_Subp_Body --
2970 --------------------
2972 -- Always ignored. In CodePeer mode, gnat2scil knows how to handle
2973 -- this attribute directly, and if we are not in CodePeer mode it is
2974 -- entirely ignored ???
2976 when Attribute_Elab_Subp_Body =>
2977 return;
2979 ----------------
2980 -- Elaborated --
2981 ----------------
2983 -- Elaborated is always True for preelaborated units, predefined units,
2984 -- pure units and units which have Elaborate_Body pragmas. These units
2985 -- have no elaboration entity.
2987 -- Note: The Elaborated attribute is never passed to the back end
2989 when Attribute_Elaborated => Elaborated : declare
2990 Ent : constant Entity_Id := Entity (Pref);
2992 begin
2993 if Present (Elaboration_Entity (Ent)) then
2994 Rewrite (N,
2995 Make_Op_Ne (Loc,
2996 Left_Opnd =>
2997 New_Occurrence_Of (Elaboration_Entity (Ent), Loc),
2998 Right_Opnd =>
2999 Make_Integer_Literal (Loc, Uint_0)));
3000 Analyze_And_Resolve (N, Typ);
3001 else
3002 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3003 end if;
3004 end Elaborated;
3006 --------------
3007 -- Enum_Rep --
3008 --------------
3010 when Attribute_Enum_Rep => Enum_Rep : declare
3011 Expr : Node_Id;
3013 begin
3014 -- Get the expression, which is X for Enum_Type'Enum_Rep (X) or
3015 -- X'Enum_Rep.
3017 if Is_Non_Empty_List (Exprs) then
3018 Expr := First (Exprs);
3019 else
3020 Expr := Pref;
3021 end if;
3023 -- If the expression is an enumeration literal, it is replaced by the
3024 -- literal value.
3026 if Nkind (Expr) in N_Has_Entity
3027 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
3028 then
3029 Rewrite (N,
3030 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Expr))));
3032 -- If this is a renaming of a literal, recover the representation
3033 -- of the original. If it renames an expression there is nothing to
3034 -- fold.
3036 elsif Nkind (Expr) in N_Has_Entity
3037 and then Ekind (Entity (Expr)) = E_Constant
3038 and then Present (Renamed_Object (Entity (Expr)))
3039 and then Is_Entity_Name (Renamed_Object (Entity (Expr)))
3040 and then Ekind (Entity (Renamed_Object (Entity (Expr)))) =
3041 E_Enumeration_Literal
3042 then
3043 Rewrite (N,
3044 Make_Integer_Literal (Loc,
3045 Enumeration_Rep (Entity (Renamed_Object (Entity (Expr))))));
3047 -- If not constant-folded above, Enum_Type'Enum_Rep (X) or
3048 -- X'Enum_Rep expands to
3050 -- target-type (X)
3052 -- This is simply a direct conversion from the enumeration type to
3053 -- the target integer type, which is treated by the back end as a
3054 -- normal integer conversion, treating the enumeration type as an
3055 -- integer, which is exactly what we want. We set Conversion_OK to
3056 -- make sure that the analyzer does not complain about what otherwise
3057 -- might be an illegal conversion.
3059 else
3060 Rewrite (N, OK_Convert_To (Typ, Relocate_Node (Expr)));
3061 end if;
3063 Set_Etype (N, Typ);
3064 Analyze_And_Resolve (N, Typ);
3065 end Enum_Rep;
3067 --------------
3068 -- Enum_Val --
3069 --------------
3071 when Attribute_Enum_Val => Enum_Val : declare
3072 Expr : Node_Id;
3073 Btyp : constant Entity_Id := Base_Type (Ptyp);
3075 begin
3076 -- X'Enum_Val (Y) expands to
3078 -- [constraint_error when _rep_to_pos (Y, False) = -1, msg]
3079 -- X!(Y);
3081 Expr := Unchecked_Convert_To (Ptyp, First (Exprs));
3083 Insert_Action (N,
3084 Make_Raise_Constraint_Error (Loc,
3085 Condition =>
3086 Make_Op_Eq (Loc,
3087 Left_Opnd =>
3088 Make_Function_Call (Loc,
3089 Name =>
3090 New_Occurrence_Of (TSS (Btyp, TSS_Rep_To_Pos), Loc),
3091 Parameter_Associations => New_List (
3092 Relocate_Node (Duplicate_Subexpr (Expr)),
3093 New_Occurrence_Of (Standard_False, Loc))),
3095 Right_Opnd => Make_Integer_Literal (Loc, -1)),
3096 Reason => CE_Range_Check_Failed));
3098 Rewrite (N, Expr);
3099 Analyze_And_Resolve (N, Ptyp);
3100 end Enum_Val;
3102 --------------
3103 -- Exponent --
3104 --------------
3106 -- Transforms 'Exponent into a call to the floating-point attribute
3107 -- function Exponent in Fat_xxx (where xxx is the root type)
3109 when Attribute_Exponent =>
3110 Expand_Fpt_Attribute_R (N);
3112 ------------------
3113 -- External_Tag --
3114 ------------------
3116 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
3118 when Attribute_External_Tag => External_Tag :
3119 begin
3120 Rewrite (N,
3121 Make_Function_Call (Loc,
3122 Name => New_Occurrence_Of (RTE (RE_External_Tag), Loc),
3123 Parameter_Associations => New_List (
3124 Make_Attribute_Reference (Loc,
3125 Attribute_Name => Name_Tag,
3126 Prefix => Prefix (N)))));
3128 Analyze_And_Resolve (N, Standard_String);
3129 end External_Tag;
3131 -----------
3132 -- First --
3133 -----------
3135 when Attribute_First =>
3137 -- If the prefix type is a constrained packed array type which
3138 -- already has a Packed_Array_Impl_Type representation defined, then
3139 -- replace this attribute with a direct reference to 'First of the
3140 -- appropriate index subtype (since otherwise the back end will try
3141 -- to give us the value of 'First for this implementation type).
3143 if Is_Constrained_Packed_Array (Ptyp) then
3144 Rewrite (N,
3145 Make_Attribute_Reference (Loc,
3146 Attribute_Name => Name_First,
3147 Prefix =>
3148 New_Occurrence_Of (Get_Index_Subtype (N), Loc)));
3149 Analyze_And_Resolve (N, Typ);
3151 -- For access type, apply access check as needed
3153 elsif Is_Access_Type (Ptyp) then
3154 Apply_Access_Check (N);
3156 -- For scalar type, if low bound is a reference to an entity, just
3157 -- replace with a direct reference. Note that we can only have a
3158 -- reference to a constant entity at this stage, anything else would
3159 -- have already been rewritten.
3161 elsif Is_Scalar_Type (Ptyp) then
3162 declare
3163 Lo : constant Node_Id := Type_Low_Bound (Ptyp);
3164 begin
3165 if Is_Entity_Name (Lo) then
3166 Rewrite (N, New_Occurrence_Of (Entity (Lo), Loc));
3167 end if;
3168 end;
3169 end if;
3171 ---------------
3172 -- First_Bit --
3173 ---------------
3175 -- Compute this if component clause was present, otherwise we leave the
3176 -- computation to be completed in the back-end, since we don't know what
3177 -- layout will be chosen.
3179 when Attribute_First_Bit => First_Bit_Attr : declare
3180 CE : constant Entity_Id := Entity (Selector_Name (Pref));
3182 begin
3183 -- In Ada 2005 (or later) if we have the non-default bit order, then
3184 -- we return the original value as given in the component clause
3185 -- (RM 2005 13.5.2(3/2)).
3187 if Present (Component_Clause (CE))
3188 and then Ada_Version >= Ada_2005
3189 and then Reverse_Bit_Order (Scope (CE))
3190 then
3191 Rewrite (N,
3192 Make_Integer_Literal (Loc,
3193 Intval => Expr_Value (First_Bit (Component_Clause (CE)))));
3194 Analyze_And_Resolve (N, Typ);
3196 -- Otherwise (Ada 83/95 or Ada 2005 or later with default bit order),
3197 -- rewrite with normalized value if we know it statically.
3199 elsif Known_Static_Component_Bit_Offset (CE) then
3200 Rewrite (N,
3201 Make_Integer_Literal (Loc,
3202 Component_Bit_Offset (CE) mod System_Storage_Unit));
3203 Analyze_And_Resolve (N, Typ);
3205 -- Otherwise left to back end, just do universal integer checks
3207 else
3208 Apply_Universal_Integer_Attribute_Checks (N);
3209 end if;
3210 end First_Bit_Attr;
3212 -----------------
3213 -- Fixed_Value --
3214 -----------------
3216 -- We transform:
3218 -- fixtype'Fixed_Value (integer-value)
3220 -- into
3222 -- fixtype(integer-value)
3224 -- We do all the required analysis of the conversion here, because we do
3225 -- not want this to go through the fixed-point conversion circuits. Note
3226 -- that the back end always treats fixed-point as equivalent to the
3227 -- corresponding integer type anyway.
3229 when Attribute_Fixed_Value => Fixed_Value :
3230 begin
3231 Rewrite (N,
3232 Make_Type_Conversion (Loc,
3233 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
3234 Expression => Relocate_Node (First (Exprs))));
3235 Set_Etype (N, Entity (Pref));
3236 Set_Analyzed (N);
3238 -- Note: it might appear that a properly analyzed unchecked conversion
3239 -- would be just fine here, but that's not the case, since the full
3240 -- range checks performed by the following call are critical.
3242 Apply_Type_Conversion_Checks (N);
3243 end Fixed_Value;
3245 -----------
3246 -- Floor --
3247 -----------
3249 -- Transforms 'Floor into a call to the floating-point attribute
3250 -- function Floor in Fat_xxx (where xxx is the root type)
3252 when Attribute_Floor =>
3253 Expand_Fpt_Attribute_R (N);
3255 ----------
3256 -- Fore --
3257 ----------
3259 -- For the fixed-point type Typ:
3261 -- Typ'Fore
3263 -- expands into
3265 -- Result_Type (System.Fore (Universal_Real (Type'First)),
3266 -- Universal_Real (Type'Last))
3268 -- Note that we know that the type is a non-static subtype, or Fore
3269 -- would have itself been computed dynamically in Eval_Attribute.
3271 when Attribute_Fore => Fore : begin
3272 Rewrite (N,
3273 Convert_To (Typ,
3274 Make_Function_Call (Loc,
3275 Name => New_Occurrence_Of (RTE (RE_Fore), Loc),
3277 Parameter_Associations => New_List (
3278 Convert_To (Universal_Real,
3279 Make_Attribute_Reference (Loc,
3280 Prefix => New_Occurrence_Of (Ptyp, Loc),
3281 Attribute_Name => Name_First)),
3283 Convert_To (Universal_Real,
3284 Make_Attribute_Reference (Loc,
3285 Prefix => New_Occurrence_Of (Ptyp, Loc),
3286 Attribute_Name => Name_Last))))));
3288 Analyze_And_Resolve (N, Typ);
3289 end Fore;
3291 --------------
3292 -- Fraction --
3293 --------------
3295 -- Transforms 'Fraction into a call to the floating-point attribute
3296 -- function Fraction in Fat_xxx (where xxx is the root type)
3298 when Attribute_Fraction =>
3299 Expand_Fpt_Attribute_R (N);
3301 --------------
3302 -- From_Any --
3303 --------------
3305 when Attribute_From_Any => From_Any : declare
3306 P_Type : constant Entity_Id := Etype (Pref);
3307 Decls : constant List_Id := New_List;
3308 begin
3309 Rewrite (N,
3310 Build_From_Any_Call (P_Type,
3311 Relocate_Node (First (Exprs)),
3312 Decls));
3313 Insert_Actions (N, Decls);
3314 Analyze_And_Resolve (N, P_Type);
3315 end From_Any;
3317 ----------------------
3318 -- Has_Same_Storage --
3319 ----------------------
3321 when Attribute_Has_Same_Storage => Has_Same_Storage : declare
3322 Loc : constant Source_Ptr := Sloc (N);
3324 X : constant Node_Id := Prefix (N);
3325 Y : constant Node_Id := First (Expressions (N));
3326 -- The arguments
3328 X_Addr, Y_Addr : Node_Id;
3329 -- Rhe expressions for their addresses
3331 X_Size, Y_Size : Node_Id;
3332 -- Rhe expressions for their sizes
3334 begin
3335 -- The attribute is expanded as:
3337 -- (X'address = Y'address)
3338 -- and then (X'Size = Y'Size)
3340 -- If both arguments have the same Etype the second conjunct can be
3341 -- omitted.
3343 X_Addr :=
3344 Make_Attribute_Reference (Loc,
3345 Attribute_Name => Name_Address,
3346 Prefix => New_Copy_Tree (X));
3348 Y_Addr :=
3349 Make_Attribute_Reference (Loc,
3350 Attribute_Name => Name_Address,
3351 Prefix => New_Copy_Tree (Y));
3353 X_Size :=
3354 Make_Attribute_Reference (Loc,
3355 Attribute_Name => Name_Size,
3356 Prefix => New_Copy_Tree (X));
3358 Y_Size :=
3359 Make_Attribute_Reference (Loc,
3360 Attribute_Name => Name_Size,
3361 Prefix => New_Copy_Tree (Y));
3363 if Etype (X) = Etype (Y) then
3364 Rewrite (N,
3365 (Make_Op_Eq (Loc,
3366 Left_Opnd => X_Addr,
3367 Right_Opnd => Y_Addr)));
3368 else
3369 Rewrite (N,
3370 Make_Op_And (Loc,
3371 Left_Opnd =>
3372 Make_Op_Eq (Loc,
3373 Left_Opnd => X_Addr,
3374 Right_Opnd => Y_Addr),
3375 Right_Opnd =>
3376 Make_Op_Eq (Loc,
3377 Left_Opnd => X_Size,
3378 Right_Opnd => Y_Size)));
3379 end if;
3381 Analyze_And_Resolve (N, Standard_Boolean);
3382 end Has_Same_Storage;
3384 --------------
3385 -- Identity --
3386 --------------
3388 -- For an exception returns a reference to the exception data:
3389 -- Exception_Id!(Prefix'Reference)
3391 -- For a task it returns a reference to the _task_id component of
3392 -- corresponding record:
3394 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
3396 -- in Ada.Task_Identification
3398 when Attribute_Identity => Identity : declare
3399 Id_Kind : Entity_Id;
3401 begin
3402 if Ptyp = Standard_Exception_Type then
3403 Id_Kind := RTE (RE_Exception_Id);
3405 if Present (Renamed_Object (Entity (Pref))) then
3406 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
3407 end if;
3409 Rewrite (N,
3410 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
3411 else
3412 Id_Kind := RTE (RO_AT_Task_Id);
3414 -- If the prefix is a task interface, the Task_Id is obtained
3415 -- dynamically through a dispatching call, as for other task
3416 -- attributes applied to interfaces.
3418 if Ada_Version >= Ada_2005
3419 and then Ekind (Ptyp) = E_Class_Wide_Type
3420 and then Is_Interface (Ptyp)
3421 and then Is_Task_Interface (Ptyp)
3422 then
3423 Rewrite (N,
3424 Unchecked_Convert_To (Id_Kind,
3425 Make_Selected_Component (Loc,
3426 Prefix =>
3427 New_Copy_Tree (Pref),
3428 Selector_Name =>
3429 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))));
3431 else
3432 Rewrite (N,
3433 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
3434 end if;
3435 end if;
3437 Analyze_And_Resolve (N, Id_Kind);
3438 end Identity;
3440 -----------
3441 -- Image --
3442 -----------
3444 -- Image attribute is handled in separate unit Exp_Imgv
3446 when Attribute_Image =>
3447 Exp_Imgv.Expand_Image_Attribute (N);
3449 ---------
3450 -- Img --
3451 ---------
3453 -- X'Img is expanded to typ'Image (X), where typ is the type of X
3455 when Attribute_Img => Img :
3456 begin
3457 Rewrite (N,
3458 Make_Attribute_Reference (Loc,
3459 Prefix => New_Occurrence_Of (Ptyp, Loc),
3460 Attribute_Name => Name_Image,
3461 Expressions => New_List (Relocate_Node (Pref))));
3463 Analyze_And_Resolve (N, Standard_String);
3464 end Img;
3466 -----------
3467 -- Input --
3468 -----------
3470 when Attribute_Input => Input : declare
3471 P_Type : constant Entity_Id := Entity (Pref);
3472 B_Type : constant Entity_Id := Base_Type (P_Type);
3473 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3474 Strm : constant Node_Id := First (Exprs);
3475 Fname : Entity_Id;
3476 Decl : Node_Id;
3477 Call : Node_Id;
3478 Prag : Node_Id;
3479 Arg2 : Node_Id;
3480 Rfunc : Node_Id;
3482 Cntrl : Node_Id := Empty;
3483 -- Value for controlling argument in call. Always Empty except in
3484 -- the dispatching (class-wide type) case, where it is a reference
3485 -- to the dummy object initialized to the right internal tag.
3487 procedure Freeze_Stream_Subprogram (F : Entity_Id);
3488 -- The expansion of the attribute reference may generate a call to
3489 -- a user-defined stream subprogram that is frozen by the call. This
3490 -- can lead to access-before-elaboration problem if the reference
3491 -- appears in an object declaration and the subprogram body has not
3492 -- been seen. The freezing of the subprogram requires special code
3493 -- because it appears in an expanded context where expressions do
3494 -- not freeze their constituents.
3496 ------------------------------
3497 -- Freeze_Stream_Subprogram --
3498 ------------------------------
3500 procedure Freeze_Stream_Subprogram (F : Entity_Id) is
3501 Decl : constant Node_Id := Unit_Declaration_Node (F);
3502 Bod : Node_Id;
3504 begin
3505 -- If this is user-defined subprogram, the corresponding
3506 -- stream function appears as a renaming-as-body, and the
3507 -- user subprogram must be retrieved by tree traversal.
3509 if Present (Decl)
3510 and then Nkind (Decl) = N_Subprogram_Declaration
3511 and then Present (Corresponding_Body (Decl))
3512 then
3513 Bod := Corresponding_Body (Decl);
3515 if Nkind (Unit_Declaration_Node (Bod)) =
3516 N_Subprogram_Renaming_Declaration
3517 then
3518 Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
3519 end if;
3520 end if;
3521 end Freeze_Stream_Subprogram;
3523 -- Start of processing for Input
3525 begin
3526 -- If no underlying type, we have an error that will be diagnosed
3527 -- elsewhere, so here we just completely ignore the expansion.
3529 if No (U_Type) then
3530 return;
3531 end if;
3533 -- Stream operations can appear in user code even if the restriction
3534 -- No_Streams is active (for example, when instantiating a predefined
3535 -- container). In that case rewrite the attribute as a Raise to
3536 -- prevent any run-time use.
3538 if Restriction_Active (No_Streams) then
3539 Rewrite (N,
3540 Make_Raise_Program_Error (Sloc (N),
3541 Reason => PE_Stream_Operation_Not_Allowed));
3542 Set_Etype (N, B_Type);
3543 return;
3544 end if;
3546 -- If there is a TSS for Input, just call it
3548 Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
3550 if Present (Fname) then
3551 null;
3553 else
3554 -- If there is a Stream_Convert pragma, use it, we rewrite
3556 -- sourcetyp'Input (stream)
3558 -- as
3560 -- sourcetyp (streamread (strmtyp'Input (stream)));
3562 -- where streamread is the given Read function that converts an
3563 -- argument of type strmtyp to type sourcetyp or a type from which
3564 -- it is derived (extra conversion required for the derived case).
3566 Prag := Get_Stream_Convert_Pragma (P_Type);
3568 if Present (Prag) then
3569 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
3570 Rfunc := Entity (Expression (Arg2));
3572 Rewrite (N,
3573 Convert_To (B_Type,
3574 Make_Function_Call (Loc,
3575 Name => New_Occurrence_Of (Rfunc, Loc),
3576 Parameter_Associations => New_List (
3577 Make_Attribute_Reference (Loc,
3578 Prefix =>
3579 New_Occurrence_Of
3580 (Etype (First_Formal (Rfunc)), Loc),
3581 Attribute_Name => Name_Input,
3582 Expressions => Exprs)))));
3584 Analyze_And_Resolve (N, B_Type);
3585 return;
3587 -- Elementary types
3589 elsif Is_Elementary_Type (U_Type) then
3591 -- A special case arises if we have a defined _Read routine,
3592 -- since in this case we are required to call this routine.
3594 if Present (TSS (Base_Type (U_Type), TSS_Stream_Read)) then
3595 Build_Record_Or_Elementary_Input_Function
3596 (Loc, U_Type, Decl, Fname);
3597 Insert_Action (N, Decl);
3599 -- For normal cases, we call the I_xxx routine directly
3601 else
3602 Rewrite (N, Build_Elementary_Input_Call (N));
3603 Analyze_And_Resolve (N, P_Type);
3604 return;
3605 end if;
3607 -- Array type case
3609 elsif Is_Array_Type (U_Type) then
3610 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
3611 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3613 -- Dispatching case with class-wide type
3615 elsif Is_Class_Wide_Type (P_Type) then
3617 -- No need to do anything else compiling under restriction
3618 -- No_Dispatching_Calls. During the semantic analysis we
3619 -- already notified such violation.
3621 if Restriction_Active (No_Dispatching_Calls) then
3622 return;
3623 end if;
3625 declare
3626 Rtyp : constant Entity_Id := Root_Type (P_Type);
3627 Expr : Node_Id;
3629 begin
3630 -- Read the internal tag (RM 13.13.2(34)) and use it to
3631 -- initialize a dummy tag value:
3633 -- Descendant_Tag (String'Input (Strm), P_Type);
3635 -- This value is used only to provide a controlling
3636 -- argument for the eventual _Input call. Descendant_Tag is
3637 -- called rather than Internal_Tag to ensure that we have a
3638 -- tag for a type that is descended from the prefix type and
3639 -- declared at the same accessibility level (the exception
3640 -- Tag_Error will be raised otherwise). The level check is
3641 -- required for Ada 2005 because tagged types can be
3642 -- extended in nested scopes (AI-344).
3644 -- Note: we used to generate an explicit declaration of a
3645 -- constant Ada.Tags.Tag object, and use an occurrence of
3646 -- this constant in Cntrl, but this caused a secondary stack
3647 -- leak.
3649 Expr :=
3650 Make_Function_Call (Loc,
3651 Name =>
3652 New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc),
3653 Parameter_Associations => New_List (
3654 Make_Attribute_Reference (Loc,
3655 Prefix =>
3656 New_Occurrence_Of (Standard_String, Loc),
3657 Attribute_Name => Name_Input,
3658 Expressions => New_List (
3659 Relocate_Node (Duplicate_Subexpr (Strm)))),
3660 Make_Attribute_Reference (Loc,
3661 Prefix => New_Occurrence_Of (P_Type, Loc),
3662 Attribute_Name => Name_Tag)));
3663 Set_Etype (Expr, RTE (RE_Tag));
3665 -- Now we need to get the entity for the call, and construct
3666 -- a function call node, where we preset a reference to Dnn
3667 -- as the controlling argument (doing an unchecked convert
3668 -- to the class-wide tagged type to make it look like a real
3669 -- tagged object).
3671 Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
3672 Cntrl := Unchecked_Convert_To (P_Type, Expr);
3673 Set_Etype (Cntrl, P_Type);
3674 Set_Parent (Cntrl, N);
3675 end;
3677 -- For tagged types, use the primitive Input function
3679 elsif Is_Tagged_Type (U_Type) then
3680 Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
3682 -- All other record type cases, including protected records. The
3683 -- latter only arise for expander generated code for handling
3684 -- shared passive partition access.
3686 else
3687 pragma Assert
3688 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3690 -- Ada 2005 (AI-216): Program_Error is raised executing default
3691 -- implementation of the Input attribute of an unchecked union
3692 -- type if the type lacks default discriminant values.
3694 if Is_Unchecked_Union (Base_Type (U_Type))
3695 and then No (Discriminant_Constraint (U_Type))
3696 then
3697 Insert_Action (N,
3698 Make_Raise_Program_Error (Loc,
3699 Reason => PE_Unchecked_Union_Restriction));
3701 return;
3702 end if;
3704 -- Build the type's Input function, passing the subtype rather
3705 -- than its base type, because checks are needed in the case of
3706 -- constrained discriminants (see Ada 2012 AI05-0192).
3708 Build_Record_Or_Elementary_Input_Function
3709 (Loc, U_Type, Decl, Fname);
3710 Insert_Action (N, Decl);
3712 if Nkind (Parent (N)) = N_Object_Declaration
3713 and then Is_Record_Type (U_Type)
3714 then
3715 -- The stream function may contain calls to user-defined
3716 -- Read procedures for individual components.
3718 declare
3719 Comp : Entity_Id;
3720 Func : Entity_Id;
3722 begin
3723 Comp := First_Component (U_Type);
3724 while Present (Comp) loop
3725 Func :=
3726 Find_Stream_Subprogram
3727 (Etype (Comp), TSS_Stream_Read);
3729 if Present (Func) then
3730 Freeze_Stream_Subprogram (Func);
3731 end if;
3733 Next_Component (Comp);
3734 end loop;
3735 end;
3736 end if;
3737 end if;
3738 end if;
3740 -- If we fall through, Fname is the function to be called. The result
3741 -- is obtained by calling the appropriate function, then converting
3742 -- the result. The conversion does a subtype check.
3744 Call :=
3745 Make_Function_Call (Loc,
3746 Name => New_Occurrence_Of (Fname, Loc),
3747 Parameter_Associations => New_List (
3748 Relocate_Node (Strm)));
3750 Set_Controlling_Argument (Call, Cntrl);
3751 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
3752 Analyze_And_Resolve (N, P_Type);
3754 if Nkind (Parent (N)) = N_Object_Declaration then
3755 Freeze_Stream_Subprogram (Fname);
3756 end if;
3757 end Input;
3759 -------------------
3760 -- Integer_Value --
3761 -------------------
3763 -- We transform
3765 -- inttype'Fixed_Value (fixed-value)
3767 -- into
3769 -- inttype(integer-value))
3771 -- we do all the required analysis of the conversion here, because we do
3772 -- not want this to go through the fixed-point conversion circuits. Note
3773 -- that the back end always treats fixed-point as equivalent to the
3774 -- corresponding integer type anyway.
3776 when Attribute_Integer_Value => Integer_Value :
3777 begin
3778 Rewrite (N,
3779 Make_Type_Conversion (Loc,
3780 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
3781 Expression => Relocate_Node (First (Exprs))));
3782 Set_Etype (N, Entity (Pref));
3783 Set_Analyzed (N);
3785 -- Note: it might appear that a properly analyzed unchecked conversion
3786 -- would be just fine here, but that's not the case, since the full
3787 -- range checks performed by the following call are critical.
3789 Apply_Type_Conversion_Checks (N);
3790 end Integer_Value;
3792 -------------------
3793 -- Invalid_Value --
3794 -------------------
3796 when Attribute_Invalid_Value =>
3797 Rewrite (N, Get_Simple_Init_Val (Ptyp, N));
3799 ----------
3800 -- Last --
3801 ----------
3803 when Attribute_Last =>
3805 -- If the prefix type is a constrained packed array type which
3806 -- already has a Packed_Array_Impl_Type representation defined, then
3807 -- replace this attribute with a direct reference to 'Last of the
3808 -- appropriate index subtype (since otherwise the back end will try
3809 -- to give us the value of 'Last for this implementation type).
3811 if Is_Constrained_Packed_Array (Ptyp) then
3812 Rewrite (N,
3813 Make_Attribute_Reference (Loc,
3814 Attribute_Name => Name_Last,
3815 Prefix => New_Occurrence_Of (Get_Index_Subtype (N), Loc)));
3816 Analyze_And_Resolve (N, Typ);
3818 -- For access type, apply access check as needed
3820 elsif Is_Access_Type (Ptyp) then
3821 Apply_Access_Check (N);
3823 -- For scalar type, if low bound is a reference to an entity, just
3824 -- replace with a direct reference. Note that we can only have a
3825 -- reference to a constant entity at this stage, anything else would
3826 -- have already been rewritten.
3828 elsif Is_Scalar_Type (Ptyp) then
3829 declare
3830 Hi : constant Node_Id := Type_High_Bound (Ptyp);
3831 begin
3832 if Is_Entity_Name (Hi) then
3833 Rewrite (N, New_Occurrence_Of (Entity (Hi), Loc));
3834 end if;
3835 end;
3836 end if;
3838 --------------
3839 -- Last_Bit --
3840 --------------
3842 -- We compute this if a component clause was present, otherwise we leave
3843 -- the computation up to the back end, since we don't know what layout
3844 -- will be chosen.
3846 when Attribute_Last_Bit => Last_Bit_Attr : declare
3847 CE : constant Entity_Id := Entity (Selector_Name (Pref));
3849 begin
3850 -- In Ada 2005 (or later) if we have the non-default bit order, then
3851 -- we return the original value as given in the component clause
3852 -- (RM 2005 13.5.2(3/2)).
3854 if Present (Component_Clause (CE))
3855 and then Ada_Version >= Ada_2005
3856 and then Reverse_Bit_Order (Scope (CE))
3857 then
3858 Rewrite (N,
3859 Make_Integer_Literal (Loc,
3860 Intval => Expr_Value (Last_Bit (Component_Clause (CE)))));
3861 Analyze_And_Resolve (N, Typ);
3863 -- Otherwise (Ada 83/95 or Ada 2005 or later with default bit order),
3864 -- rewrite with normalized value if we know it statically.
3866 elsif Known_Static_Component_Bit_Offset (CE)
3867 and then Known_Static_Esize (CE)
3868 then
3869 Rewrite (N,
3870 Make_Integer_Literal (Loc,
3871 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
3872 + Esize (CE) - 1));
3873 Analyze_And_Resolve (N, Typ);
3875 -- Otherwise leave to back end, just apply universal integer checks
3877 else
3878 Apply_Universal_Integer_Attribute_Checks (N);
3879 end if;
3880 end Last_Bit_Attr;
3882 ------------------
3883 -- Leading_Part --
3884 ------------------
3886 -- Transforms 'Leading_Part into a call to the floating-point attribute
3887 -- function Leading_Part in Fat_xxx (where xxx is the root type)
3889 -- Note: strictly, we should generate special case code to deal with
3890 -- absurdly large positive arguments (greater than Integer'Last), which
3891 -- result in returning the first argument unchanged, but it hardly seems
3892 -- worth the effort. We raise constraint error for absurdly negative
3893 -- arguments which is fine.
3895 when Attribute_Leading_Part =>
3896 Expand_Fpt_Attribute_RI (N);
3898 ------------
3899 -- Length --
3900 ------------
3902 when Attribute_Length => Length : declare
3903 Ityp : Entity_Id;
3904 Xnum : Uint;
3906 begin
3907 -- Processing for packed array types
3909 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
3910 Ityp := Get_Index_Subtype (N);
3912 -- If the index type, Ityp, is an enumeration type with holes,
3913 -- then we calculate X'Length explicitly using
3915 -- Typ'Max
3916 -- (0, Ityp'Pos (X'Last (N)) -
3917 -- Ityp'Pos (X'First (N)) + 1);
3919 -- Since the bounds in the template are the representation values
3920 -- and the back end would get the wrong value.
3922 if Is_Enumeration_Type (Ityp)
3923 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
3924 then
3925 if No (Exprs) then
3926 Xnum := Uint_1;
3927 else
3928 Xnum := Expr_Value (First (Expressions (N)));
3929 end if;
3931 Rewrite (N,
3932 Make_Attribute_Reference (Loc,
3933 Prefix => New_Occurrence_Of (Typ, Loc),
3934 Attribute_Name => Name_Max,
3935 Expressions => New_List
3936 (Make_Integer_Literal (Loc, 0),
3938 Make_Op_Add (Loc,
3939 Left_Opnd =>
3940 Make_Op_Subtract (Loc,
3941 Left_Opnd =>
3942 Make_Attribute_Reference (Loc,
3943 Prefix => New_Occurrence_Of (Ityp, Loc),
3944 Attribute_Name => Name_Pos,
3946 Expressions => New_List (
3947 Make_Attribute_Reference (Loc,
3948 Prefix => Duplicate_Subexpr (Pref),
3949 Attribute_Name => Name_Last,
3950 Expressions => New_List (
3951 Make_Integer_Literal (Loc, Xnum))))),
3953 Right_Opnd =>
3954 Make_Attribute_Reference (Loc,
3955 Prefix => New_Occurrence_Of (Ityp, Loc),
3956 Attribute_Name => Name_Pos,
3958 Expressions => New_List (
3959 Make_Attribute_Reference (Loc,
3960 Prefix =>
3961 Duplicate_Subexpr_No_Checks (Pref),
3962 Attribute_Name => Name_First,
3963 Expressions => New_List (
3964 Make_Integer_Literal (Loc, Xnum)))))),
3966 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3968 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
3969 return;
3971 -- If the prefix type is a constrained packed array type which
3972 -- already has a Packed_Array_Impl_Type representation defined,
3973 -- then replace this attribute with a reference to 'Range_Length
3974 -- of the appropriate index subtype (since otherwise the
3975 -- back end will try to give us the value of 'Length for
3976 -- this implementation type).s
3978 elsif Is_Constrained (Ptyp) then
3979 Rewrite (N,
3980 Make_Attribute_Reference (Loc,
3981 Attribute_Name => Name_Range_Length,
3982 Prefix => New_Occurrence_Of (Ityp, Loc)));
3983 Analyze_And_Resolve (N, Typ);
3984 end if;
3986 -- Access type case
3988 elsif Is_Access_Type (Ptyp) then
3989 Apply_Access_Check (N);
3991 -- If the designated type is a packed array type, then we convert
3992 -- the reference to:
3994 -- typ'Max (0, 1 +
3995 -- xtyp'Pos (Pref'Last (Expr)) -
3996 -- xtyp'Pos (Pref'First (Expr)));
3998 -- This is a bit complex, but it is the easiest thing to do that
3999 -- works in all cases including enum types with holes xtyp here
4000 -- is the appropriate index type.
4002 declare
4003 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
4004 Xtyp : Entity_Id;
4006 begin
4007 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
4008 Xtyp := Get_Index_Subtype (N);
4010 Rewrite (N,
4011 Make_Attribute_Reference (Loc,
4012 Prefix => New_Occurrence_Of (Typ, Loc),
4013 Attribute_Name => Name_Max,
4014 Expressions => New_List (
4015 Make_Integer_Literal (Loc, 0),
4017 Make_Op_Add (Loc,
4018 Make_Integer_Literal (Loc, 1),
4019 Make_Op_Subtract (Loc,
4020 Left_Opnd =>
4021 Make_Attribute_Reference (Loc,
4022 Prefix => New_Occurrence_Of (Xtyp, Loc),
4023 Attribute_Name => Name_Pos,
4024 Expressions => New_List (
4025 Make_Attribute_Reference (Loc,
4026 Prefix => Duplicate_Subexpr (Pref),
4027 Attribute_Name => Name_Last,
4028 Expressions =>
4029 New_Copy_List (Exprs)))),
4031 Right_Opnd =>
4032 Make_Attribute_Reference (Loc,
4033 Prefix => New_Occurrence_Of (Xtyp, Loc),
4034 Attribute_Name => Name_Pos,
4035 Expressions => New_List (
4036 Make_Attribute_Reference (Loc,
4037 Prefix =>
4038 Duplicate_Subexpr_No_Checks (Pref),
4039 Attribute_Name => Name_First,
4040 Expressions =>
4041 New_Copy_List (Exprs)))))))));
4043 Analyze_And_Resolve (N, Typ);
4044 end if;
4045 end;
4047 -- Otherwise leave it to the back end
4049 else
4050 Apply_Universal_Integer_Attribute_Checks (N);
4051 end if;
4052 end Length;
4054 -- Attribute Loop_Entry is replaced with a reference to a constant value
4055 -- which captures the prefix at the entry point of the related loop. The
4056 -- loop itself may be transformed into a conditional block.
4058 when Attribute_Loop_Entry =>
4059 Expand_Loop_Entry_Attribute (N);
4061 -------------
4062 -- Machine --
4063 -------------
4065 -- Transforms 'Machine into a call to the floating-point attribute
4066 -- function Machine in Fat_xxx (where xxx is the root type).
4067 -- Expansion is avoided for cases the back end can handle directly.
4069 when Attribute_Machine =>
4070 if not Is_Inline_Floating_Point_Attribute (N) then
4071 Expand_Fpt_Attribute_R (N);
4072 end if;
4074 ----------------------
4075 -- Machine_Rounding --
4076 ----------------------
4078 -- Transforms 'Machine_Rounding into a call to the floating-point
4079 -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root
4080 -- type). Expansion is avoided for cases the back end can handle
4081 -- directly.
4083 when Attribute_Machine_Rounding =>
4084 if not Is_Inline_Floating_Point_Attribute (N) then
4085 Expand_Fpt_Attribute_R (N);
4086 end if;
4088 ------------------
4089 -- Machine_Size --
4090 ------------------
4092 -- Machine_Size is equivalent to Object_Size, so transform it into
4093 -- Object_Size and that way the back end never sees Machine_Size.
4095 when Attribute_Machine_Size =>
4096 Rewrite (N,
4097 Make_Attribute_Reference (Loc,
4098 Prefix => Prefix (N),
4099 Attribute_Name => Name_Object_Size));
4101 Analyze_And_Resolve (N, Typ);
4103 --------------
4104 -- Mantissa --
4105 --------------
4107 -- The only case that can get this far is the dynamic case of the old
4108 -- Ada 83 Mantissa attribute for the fixed-point case. For this case,
4109 -- we expand:
4111 -- typ'Mantissa
4113 -- into
4115 -- ityp (System.Mantissa.Mantissa_Value
4116 -- (Integer'Integer_Value (typ'First),
4117 -- Integer'Integer_Value (typ'Last)));
4119 when Attribute_Mantissa => Mantissa : begin
4120 Rewrite (N,
4121 Convert_To (Typ,
4122 Make_Function_Call (Loc,
4123 Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
4125 Parameter_Associations => New_List (
4127 Make_Attribute_Reference (Loc,
4128 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
4129 Attribute_Name => Name_Integer_Value,
4130 Expressions => New_List (
4132 Make_Attribute_Reference (Loc,
4133 Prefix => New_Occurrence_Of (Ptyp, Loc),
4134 Attribute_Name => Name_First))),
4136 Make_Attribute_Reference (Loc,
4137 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
4138 Attribute_Name => Name_Integer_Value,
4139 Expressions => New_List (
4141 Make_Attribute_Reference (Loc,
4142 Prefix => New_Occurrence_Of (Ptyp, Loc),
4143 Attribute_Name => Name_Last)))))));
4145 Analyze_And_Resolve (N, Typ);
4146 end Mantissa;
4148 ---------
4149 -- Max --
4150 ---------
4152 when Attribute_Max =>
4153 Expand_Min_Max_Attribute (N);
4155 ----------------------------------
4156 -- Max_Size_In_Storage_Elements --
4157 ----------------------------------
4159 when Attribute_Max_Size_In_Storage_Elements => declare
4160 Typ : constant Entity_Id := Etype (N);
4161 Attr : Node_Id;
4163 Conversion_Added : Boolean := False;
4164 -- A flag which tracks whether the original attribute has been
4165 -- wrapped inside a type conversion.
4167 begin
4168 -- If the prefix is X'Class, we transform it into a direct reference
4169 -- to the class-wide type, because the back end must not see a 'Class
4170 -- reference. See also 'Size.
4172 if Is_Entity_Name (Pref)
4173 and then Is_Class_Wide_Type (Entity (Pref))
4174 then
4175 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
4176 return;
4177 end if;
4179 Apply_Universal_Integer_Attribute_Checks (N);
4181 -- The universal integer check may sometimes add a type conversion,
4182 -- retrieve the original attribute reference from the expression.
4184 Attr := N;
4186 if Nkind (Attr) = N_Type_Conversion then
4187 Attr := Expression (Attr);
4188 Conversion_Added := True;
4189 end if;
4191 pragma Assert (Nkind (Attr) = N_Attribute_Reference);
4193 -- Heap-allocated controlled objects contain two extra pointers which
4194 -- are not part of the actual type. Transform the attribute reference
4195 -- into a runtime expression to add the size of the hidden header.
4197 if Needs_Finalization (Ptyp)
4198 and then not Header_Size_Added (Attr)
4199 then
4200 Set_Header_Size_Added (Attr);
4202 -- Generate:
4203 -- P'Max_Size_In_Storage_Elements +
4204 -- Universal_Integer
4205 -- (Header_Size_With_Padding (Ptyp'Alignment))
4207 Rewrite (Attr,
4208 Make_Op_Add (Loc,
4209 Left_Opnd => Relocate_Node (Attr),
4210 Right_Opnd =>
4211 Convert_To (Universal_Integer,
4212 Make_Function_Call (Loc,
4213 Name =>
4214 New_Occurrence_Of
4215 (RTE (RE_Header_Size_With_Padding), Loc),
4217 Parameter_Associations => New_List (
4218 Make_Attribute_Reference (Loc,
4219 Prefix =>
4220 New_Occurrence_Of (Ptyp, Loc),
4221 Attribute_Name => Name_Alignment))))));
4223 -- Add a conversion to the target type
4225 if not Conversion_Added then
4226 Rewrite (Attr,
4227 Make_Type_Conversion (Loc,
4228 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4229 Expression => Relocate_Node (Attr)));
4230 end if;
4232 Analyze (Attr);
4233 return;
4234 end if;
4235 end;
4237 --------------------
4238 -- Mechanism_Code --
4239 --------------------
4241 when Attribute_Mechanism_Code =>
4243 -- We must replace the prefix i the renamed case
4245 if Is_Entity_Name (Pref)
4246 and then Present (Alias (Entity (Pref)))
4247 then
4248 Set_Renamed_Subprogram (Pref, Alias (Entity (Pref)));
4249 end if;
4251 ---------
4252 -- Min --
4253 ---------
4255 when Attribute_Min =>
4256 Expand_Min_Max_Attribute (N);
4258 ---------
4259 -- Mod --
4260 ---------
4262 when Attribute_Mod => Mod_Case : declare
4263 Arg : constant Node_Id := Relocate_Node (First (Exprs));
4264 Hi : constant Node_Id := Type_High_Bound (Etype (Arg));
4265 Modv : constant Uint := Modulus (Btyp);
4267 begin
4269 -- This is not so simple. The issue is what type to use for the
4270 -- computation of the modular value.
4272 -- The easy case is when the modulus value is within the bounds
4273 -- of the signed integer type of the argument. In this case we can
4274 -- just do the computation in that signed integer type, and then
4275 -- do an ordinary conversion to the target type.
4277 if Modv <= Expr_Value (Hi) then
4278 Rewrite (N,
4279 Convert_To (Btyp,
4280 Make_Op_Mod (Loc,
4281 Left_Opnd => Arg,
4282 Right_Opnd => Make_Integer_Literal (Loc, Modv))));
4284 -- Here we know that the modulus is larger than type'Last of the
4285 -- integer type. There are two cases to consider:
4287 -- a) The integer value is non-negative. In this case, it is
4288 -- returned as the result (since it is less than the modulus).
4290 -- b) The integer value is negative. In this case, we know that the
4291 -- result is modulus + value, where the value might be as small as
4292 -- -modulus. The trouble is what type do we use to do the subtract.
4293 -- No type will do, since modulus can be as big as 2**64, and no
4294 -- integer type accommodates this value. Let's do bit of algebra
4296 -- modulus + value
4297 -- = modulus - (-value)
4298 -- = (modulus - 1) - (-value - 1)
4300 -- Now modulus - 1 is certainly in range of the modular type.
4301 -- -value is in the range 1 .. modulus, so -value -1 is in the
4302 -- range 0 .. modulus-1 which is in range of the modular type.
4303 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
4304 -- which we can compute using the integer base type.
4306 -- Once this is done we analyze the if expression without range
4307 -- checks, because we know everything is in range, and we want
4308 -- to prevent spurious warnings on either branch.
4310 else
4311 Rewrite (N,
4312 Make_If_Expression (Loc,
4313 Expressions => New_List (
4314 Make_Op_Ge (Loc,
4315 Left_Opnd => Duplicate_Subexpr (Arg),
4316 Right_Opnd => Make_Integer_Literal (Loc, 0)),
4318 Convert_To (Btyp,
4319 Duplicate_Subexpr_No_Checks (Arg)),
4321 Make_Op_Subtract (Loc,
4322 Left_Opnd =>
4323 Make_Integer_Literal (Loc,
4324 Intval => Modv - 1),
4325 Right_Opnd =>
4326 Convert_To (Btyp,
4327 Make_Op_Minus (Loc,
4328 Right_Opnd =>
4329 Make_Op_Add (Loc,
4330 Left_Opnd => Duplicate_Subexpr_No_Checks (Arg),
4331 Right_Opnd =>
4332 Make_Integer_Literal (Loc,
4333 Intval => 1))))))));
4335 end if;
4337 Analyze_And_Resolve (N, Btyp, Suppress => All_Checks);
4338 end Mod_Case;
4340 -----------
4341 -- Model --
4342 -----------
4344 -- Transforms 'Model into a call to the floating-point attribute
4345 -- function Model in Fat_xxx (where xxx is the root type).
4346 -- Expansion is avoided for cases the back end can handle directly.
4348 when Attribute_Model =>
4349 if not Is_Inline_Floating_Point_Attribute (N) then
4350 Expand_Fpt_Attribute_R (N);
4351 end if;
4353 -----------------
4354 -- Object_Size --
4355 -----------------
4357 -- The processing for Object_Size shares the processing for Size
4359 ---------
4360 -- Old --
4361 ---------
4363 when Attribute_Old => Old : declare
4364 Typ : constant Entity_Id := Etype (N);
4365 CW_Temp : Entity_Id;
4366 CW_Typ : Entity_Id;
4367 Ins_Nod : Node_Id;
4368 Subp : Node_Id;
4369 Temp : Entity_Id;
4371 begin
4372 -- Generating C code we don't need to expand this attribute when
4373 -- we are analyzing the internally built nested postconditions
4374 -- procedure since it will be expanded inline (and later it will
4375 -- be removed by Expand_N_Subprogram_Body). It this expansion is
4376 -- performed in such case then the compiler generates unreferenced
4377 -- extra temporaries.
4379 if Modify_Tree_For_C
4380 and then Chars (Current_Scope) = Name_uPostconditions
4381 then
4382 return;
4383 end if;
4385 -- Climb the parent chain looking for subprogram _Postconditions
4387 Subp := N;
4388 while Present (Subp) loop
4389 exit when Nkind (Subp) = N_Subprogram_Body
4390 and then Chars (Defining_Entity (Subp)) = Name_uPostconditions;
4392 -- If assertions are disabled, no need to create the declaration
4393 -- that preserves the value. The postcondition pragma in which
4394 -- 'Old appears will be checked or disabled according to the
4395 -- current policy in effect.
4397 if Nkind (Subp) = N_Pragma and then not Is_Checked (Subp) then
4398 return;
4399 end if;
4401 Subp := Parent (Subp);
4402 end loop;
4404 -- 'Old can only appear in a postcondition, the generated body of
4405 -- _Postconditions must be in the tree (or inlined if we are
4406 -- generating C code).
4408 pragma Assert
4409 (Present (Subp)
4410 or else (Modify_Tree_For_C and then In_Inlined_Body));
4412 Temp := Make_Temporary (Loc, 'T', Pref);
4414 -- Set the entity kind now in order to mark the temporary as a
4415 -- handler of attribute 'Old's prefix.
4417 Set_Ekind (Temp, E_Constant);
4418 Set_Stores_Attribute_Old_Prefix (Temp);
4420 -- Push the scope of the related subprogram where _Postcondition
4421 -- resides as this ensures that the object will be analyzed in the
4422 -- proper context.
4424 if Present (Subp) then
4425 Push_Scope (Scope (Defining_Entity (Subp)));
4427 -- No need to push the scope when generating C code since the
4428 -- _Postcondition procedure has been inlined.
4430 else pragma Assert (Modify_Tree_For_C);
4431 pragma Assert (In_Inlined_Body);
4432 null;
4433 end if;
4435 -- Locate the insertion place of the internal temporary that saves
4436 -- the 'Old value.
4438 if Present (Subp) then
4439 Ins_Nod := Subp;
4441 -- Generating C, the postcondition procedure has been inlined and the
4442 -- temporary is added before the first declaration of the enclosing
4443 -- subprogram.
4445 else pragma Assert (Modify_Tree_For_C);
4446 Ins_Nod := N;
4447 while Nkind (Ins_Nod) /= N_Subprogram_Body loop
4448 Ins_Nod := Parent (Ins_Nod);
4449 end loop;
4451 Ins_Nod := First (Declarations (Ins_Nod));
4452 end if;
4454 -- Preserve the tag of the prefix by offering a specific view of the
4455 -- class-wide version of the prefix.
4457 if Is_Tagged_Type (Typ) then
4459 -- Generate:
4460 -- CW_Temp : constant Typ'Class := Typ'Class (Pref);
4462 CW_Temp := Make_Temporary (Loc, 'T');
4463 CW_Typ := Class_Wide_Type (Typ);
4465 Insert_Before_And_Analyze (Ins_Nod,
4466 Make_Object_Declaration (Loc,
4467 Defining_Identifier => CW_Temp,
4468 Constant_Present => True,
4469 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
4470 Expression =>
4471 Convert_To (CW_Typ, Relocate_Node (Pref))));
4473 -- Generate:
4474 -- Temp : Typ renames Typ (CW_Temp);
4476 Insert_Before_And_Analyze (Ins_Nod,
4477 Make_Object_Renaming_Declaration (Loc,
4478 Defining_Identifier => Temp,
4479 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4480 Name =>
4481 Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc))));
4483 -- Non-tagged case
4485 else
4486 -- Generate:
4487 -- Temp : constant Typ := Pref;
4489 Insert_Before_And_Analyze (Ins_Nod,
4490 Make_Object_Declaration (Loc,
4491 Defining_Identifier => Temp,
4492 Constant_Present => True,
4493 Object_Definition => New_Occurrence_Of (Typ, Loc),
4494 Expression => Relocate_Node (Pref)));
4495 end if;
4497 if Present (Subp) then
4498 Pop_Scope;
4499 end if;
4501 -- Ensure that the prefix of attribute 'Old is valid. The check must
4502 -- be inserted after the expansion of the attribute has taken place
4503 -- to reflect the new placement of the prefix.
4505 if Validity_Checks_On and then Validity_Check_Operands then
4506 Ensure_Valid (Pref);
4507 end if;
4509 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4510 end Old;
4512 ----------------------
4513 -- Overlaps_Storage --
4514 ----------------------
4516 when Attribute_Overlaps_Storage => Overlaps_Storage : declare
4517 Loc : constant Source_Ptr := Sloc (N);
4519 X : constant Node_Id := Prefix (N);
4520 Y : constant Node_Id := First (Expressions (N));
4521 -- The arguments
4523 X_Addr, Y_Addr : Node_Id;
4524 -- the expressions for their integer addresses
4526 X_Size, Y_Size : Node_Id;
4527 -- the expressions for their sizes
4529 Cond : Node_Id;
4531 begin
4532 -- Attribute expands into:
4534 -- if X'Address < Y'address then
4535 -- (X'address + X'Size - 1) >= Y'address
4536 -- else
4537 -- (Y'address + Y'size - 1) >= X'Address
4538 -- end if;
4540 -- with the proper address operations. We convert addresses to
4541 -- integer addresses to use predefined arithmetic. The size is
4542 -- expressed in storage units. We add copies of X_Addr and Y_Addr
4543 -- to prevent the appearance of the same node in two places in
4544 -- the tree.
4546 X_Addr :=
4547 Unchecked_Convert_To (RTE (RE_Integer_Address),
4548 Make_Attribute_Reference (Loc,
4549 Attribute_Name => Name_Address,
4550 Prefix => New_Copy_Tree (X)));
4552 Y_Addr :=
4553 Unchecked_Convert_To (RTE (RE_Integer_Address),
4554 Make_Attribute_Reference (Loc,
4555 Attribute_Name => Name_Address,
4556 Prefix => New_Copy_Tree (Y)));
4558 X_Size :=
4559 Make_Op_Divide (Loc,
4560 Left_Opnd =>
4561 Make_Attribute_Reference (Loc,
4562 Attribute_Name => Name_Size,
4563 Prefix => New_Copy_Tree (X)),
4564 Right_Opnd =>
4565 Make_Integer_Literal (Loc, System_Storage_Unit));
4567 Y_Size :=
4568 Make_Op_Divide (Loc,
4569 Left_Opnd =>
4570 Make_Attribute_Reference (Loc,
4571 Attribute_Name => Name_Size,
4572 Prefix => New_Copy_Tree (Y)),
4573 Right_Opnd =>
4574 Make_Integer_Literal (Loc, System_Storage_Unit));
4576 Cond :=
4577 Make_Op_Le (Loc,
4578 Left_Opnd => X_Addr,
4579 Right_Opnd => Y_Addr);
4581 Rewrite (N,
4582 Make_If_Expression (Loc, New_List (
4583 Cond,
4585 Make_Op_Ge (Loc,
4586 Left_Opnd =>
4587 Make_Op_Add (Loc,
4588 Left_Opnd => New_Copy_Tree (X_Addr),
4589 Right_Opnd =>
4590 Make_Op_Subtract (Loc,
4591 Left_Opnd => X_Size,
4592 Right_Opnd => Make_Integer_Literal (Loc, 1))),
4593 Right_Opnd => Y_Addr),
4595 Make_Op_Ge (Loc,
4596 Left_Opnd =>
4597 Make_Op_Add (Loc,
4598 Left_Opnd => New_Copy_Tree (Y_Addr),
4599 Right_Opnd =>
4600 Make_Op_Subtract (Loc,
4601 Left_Opnd => Y_Size,
4602 Right_Opnd => Make_Integer_Literal (Loc, 1))),
4603 Right_Opnd => X_Addr))));
4605 Analyze_And_Resolve (N, Standard_Boolean);
4606 end Overlaps_Storage;
4608 ------------
4609 -- Output --
4610 ------------
4612 when Attribute_Output => Output : declare
4613 P_Type : constant Entity_Id := Entity (Pref);
4614 U_Type : constant Entity_Id := Underlying_Type (P_Type);
4615 Pname : Entity_Id;
4616 Decl : Node_Id;
4617 Prag : Node_Id;
4618 Arg3 : Node_Id;
4619 Wfunc : Node_Id;
4621 begin
4622 -- If no underlying type, we have an error that will be diagnosed
4623 -- elsewhere, so here we just completely ignore the expansion.
4625 if No (U_Type) then
4626 return;
4627 end if;
4629 -- Stream operations can appear in user code even if the restriction
4630 -- No_Streams is active (for example, when instantiating a predefined
4631 -- container). In that case rewrite the attribute as a Raise to
4632 -- prevent any run-time use.
4634 if Restriction_Active (No_Streams) then
4635 Rewrite (N,
4636 Make_Raise_Program_Error (Sloc (N),
4637 Reason => PE_Stream_Operation_Not_Allowed));
4638 Set_Etype (N, Standard_Void_Type);
4639 return;
4640 end if;
4642 -- If TSS for Output is present, just call it
4644 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
4646 if Present (Pname) then
4647 null;
4649 else
4650 -- If there is a Stream_Convert pragma, use it, we rewrite
4652 -- sourcetyp'Output (stream, Item)
4654 -- as
4656 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
4658 -- where strmwrite is the given Write function that converts an
4659 -- argument of type sourcetyp or a type acctyp, from which it is
4660 -- derived to type strmtyp. The conversion to acttyp is required
4661 -- for the derived case.
4663 Prag := Get_Stream_Convert_Pragma (P_Type);
4665 if Present (Prag) then
4666 Arg3 :=
4667 Next (Next (First (Pragma_Argument_Associations (Prag))));
4668 Wfunc := Entity (Expression (Arg3));
4670 Rewrite (N,
4671 Make_Attribute_Reference (Loc,
4672 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
4673 Attribute_Name => Name_Output,
4674 Expressions => New_List (
4675 Relocate_Node (First (Exprs)),
4676 Make_Function_Call (Loc,
4677 Name => New_Occurrence_Of (Wfunc, Loc),
4678 Parameter_Associations => New_List (
4679 OK_Convert_To (Etype (First_Formal (Wfunc)),
4680 Relocate_Node (Next (First (Exprs)))))))));
4682 Analyze (N);
4683 return;
4685 -- For elementary types, we call the W_xxx routine directly. Note
4686 -- that the effect of Write and Output is identical for the case
4687 -- of an elementary type (there are no discriminants or bounds).
4689 elsif Is_Elementary_Type (U_Type) then
4691 -- A special case arises if we have a defined _Write routine,
4692 -- since in this case we are required to call this routine.
4694 if Present (TSS (Base_Type (U_Type), TSS_Stream_Write)) then
4695 Build_Record_Or_Elementary_Output_Procedure
4696 (Loc, U_Type, Decl, Pname);
4697 Insert_Action (N, Decl);
4699 -- For normal cases, we call the W_xxx routine directly
4701 else
4702 Rewrite (N, Build_Elementary_Write_Call (N));
4703 Analyze (N);
4704 return;
4705 end if;
4707 -- Array type case
4709 elsif Is_Array_Type (U_Type) then
4710 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
4711 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
4713 -- Class-wide case, first output external tag, then dispatch
4714 -- to the appropriate primitive Output function (RM 13.13.2(31)).
4716 elsif Is_Class_Wide_Type (P_Type) then
4718 -- No need to do anything else compiling under restriction
4719 -- No_Dispatching_Calls. During the semantic analysis we
4720 -- already notified such violation.
4722 if Restriction_Active (No_Dispatching_Calls) then
4723 return;
4724 end if;
4726 Tag_Write : declare
4727 Strm : constant Node_Id := First (Exprs);
4728 Item : constant Node_Id := Next (Strm);
4730 begin
4731 -- Ada 2005 (AI-344): Check that the accessibility level
4732 -- of the type of the output object is not deeper than
4733 -- that of the attribute's prefix type.
4735 -- if Get_Access_Level (Item'Tag)
4736 -- /= Get_Access_Level (P_Type'Tag)
4737 -- then
4738 -- raise Tag_Error;
4739 -- end if;
4741 -- String'Output (Strm, External_Tag (Item'Tag));
4743 -- We cannot figure out a practical way to implement this
4744 -- accessibility check on virtual machines, so we omit it.
4746 if Ada_Version >= Ada_2005
4747 and then Tagged_Type_Expansion
4748 then
4749 Insert_Action (N,
4750 Make_Implicit_If_Statement (N,
4751 Condition =>
4752 Make_Op_Ne (Loc,
4753 Left_Opnd =>
4754 Build_Get_Access_Level (Loc,
4755 Make_Attribute_Reference (Loc,
4756 Prefix =>
4757 Relocate_Node (
4758 Duplicate_Subexpr (Item,
4759 Name_Req => True)),
4760 Attribute_Name => Name_Tag)),
4762 Right_Opnd =>
4763 Make_Integer_Literal (Loc,
4764 Type_Access_Level (P_Type))),
4766 Then_Statements =>
4767 New_List (Make_Raise_Statement (Loc,
4768 New_Occurrence_Of (
4769 RTE (RE_Tag_Error), Loc)))));
4770 end if;
4772 Insert_Action (N,
4773 Make_Attribute_Reference (Loc,
4774 Prefix => New_Occurrence_Of (Standard_String, Loc),
4775 Attribute_Name => Name_Output,
4776 Expressions => New_List (
4777 Relocate_Node (Duplicate_Subexpr (Strm)),
4778 Make_Function_Call (Loc,
4779 Name =>
4780 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
4781 Parameter_Associations => New_List (
4782 Make_Attribute_Reference (Loc,
4783 Prefix =>
4784 Relocate_Node
4785 (Duplicate_Subexpr (Item, Name_Req => True)),
4786 Attribute_Name => Name_Tag))))));
4787 end Tag_Write;
4789 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
4791 -- Tagged type case, use the primitive Output function
4793 elsif Is_Tagged_Type (U_Type) then
4794 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
4796 -- All other record type cases, including protected records.
4797 -- The latter only arise for expander generated code for
4798 -- handling shared passive partition access.
4800 else
4801 pragma Assert
4802 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
4804 -- Ada 2005 (AI-216): Program_Error is raised when executing
4805 -- the default implementation of the Output attribute of an
4806 -- unchecked union type if the type lacks default discriminant
4807 -- values.
4809 if Is_Unchecked_Union (Base_Type (U_Type))
4810 and then No (Discriminant_Constraint (U_Type))
4811 then
4812 Insert_Action (N,
4813 Make_Raise_Program_Error (Loc,
4814 Reason => PE_Unchecked_Union_Restriction));
4816 return;
4817 end if;
4819 Build_Record_Or_Elementary_Output_Procedure
4820 (Loc, Base_Type (U_Type), Decl, Pname);
4821 Insert_Action (N, Decl);
4822 end if;
4823 end if;
4825 -- If we fall through, Pname is the name of the procedure to call
4827 Rewrite_Stream_Proc_Call (Pname);
4828 end Output;
4830 ---------
4831 -- Pos --
4832 ---------
4834 -- For enumeration types with a standard representation, Pos is
4835 -- handled by the back end.
4837 -- For enumeration types, with a non-standard representation we generate
4838 -- a call to the _Rep_To_Pos function created when the type was frozen.
4839 -- The call has the form
4841 -- _rep_to_pos (expr, flag)
4843 -- The parameter flag is True if range checks are enabled, causing
4844 -- Program_Error to be raised if the expression has an invalid
4845 -- representation, and False if range checks are suppressed.
4847 -- For integer types, Pos is equivalent to a simple integer
4848 -- conversion and we rewrite it as such
4850 when Attribute_Pos => Pos :
4851 declare
4852 Etyp : Entity_Id := Base_Type (Entity (Pref));
4854 begin
4855 -- Deal with zero/non-zero boolean values
4857 if Is_Boolean_Type (Etyp) then
4858 Adjust_Condition (First (Exprs));
4859 Etyp := Standard_Boolean;
4860 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
4861 end if;
4863 -- Case of enumeration type
4865 if Is_Enumeration_Type (Etyp) then
4867 -- Non-standard enumeration type (generate call)
4869 if Present (Enum_Pos_To_Rep (Etyp)) then
4870 Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
4871 Rewrite (N,
4872 Convert_To (Typ,
4873 Make_Function_Call (Loc,
4874 Name =>
4875 New_Occurrence_Of (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4876 Parameter_Associations => Exprs)));
4878 Analyze_And_Resolve (N, Typ);
4880 -- Standard enumeration type (do universal integer check)
4882 else
4883 Apply_Universal_Integer_Attribute_Checks (N);
4884 end if;
4886 -- Deal with integer types (replace by conversion)
4888 elsif Is_Integer_Type (Etyp) then
4889 Rewrite (N, Convert_To (Typ, First (Exprs)));
4890 Analyze_And_Resolve (N, Typ);
4891 end if;
4893 end Pos;
4895 --------------
4896 -- Position --
4897 --------------
4899 -- We compute this if a component clause was present, otherwise we leave
4900 -- the computation up to the back end, since we don't know what layout
4901 -- will be chosen.
4903 when Attribute_Position => Position_Attr :
4904 declare
4905 CE : constant Entity_Id := Entity (Selector_Name (Pref));
4907 begin
4908 if Present (Component_Clause (CE)) then
4910 -- In Ada 2005 (or later) if we have the non-default bit order,
4911 -- then we return the original value as given in the component
4912 -- clause (RM 2005 13.5.2(2/2)).
4914 if Ada_Version >= Ada_2005
4915 and then Reverse_Bit_Order (Scope (CE))
4916 then
4917 Rewrite (N,
4918 Make_Integer_Literal (Loc,
4919 Intval => Expr_Value (Position (Component_Clause (CE)))));
4921 -- Otherwise (Ada 83 or 95, or default bit order specified in
4922 -- later Ada version), return the normalized value.
4924 else
4925 Rewrite (N,
4926 Make_Integer_Literal (Loc,
4927 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
4928 end if;
4930 Analyze_And_Resolve (N, Typ);
4932 -- If back end is doing things, just apply universal integer checks
4934 else
4935 Apply_Universal_Integer_Attribute_Checks (N);
4936 end if;
4937 end Position_Attr;
4939 ----------
4940 -- Pred --
4941 ----------
4943 -- 1. Deal with enumeration types with holes.
4944 -- 2. For floating-point, generate call to attribute function.
4945 -- 3. For other cases, deal with constraint checking.
4947 when Attribute_Pred => Pred :
4948 declare
4949 Etyp : constant Entity_Id := Base_Type (Ptyp);
4951 begin
4953 -- For enumeration types with non-standard representations, we
4954 -- expand typ'Pred (x) into
4956 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
4958 -- If the representation is contiguous, we compute instead
4959 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
4960 -- The conversion function Enum_Pos_To_Rep is defined on the
4961 -- base type, not the subtype, so we have to use the base type
4962 -- explicitly for this and other enumeration attributes.
4964 if Is_Enumeration_Type (Ptyp)
4965 and then Present (Enum_Pos_To_Rep (Etyp))
4966 then
4967 if Has_Contiguous_Rep (Etyp) then
4968 Rewrite (N,
4969 Unchecked_Convert_To (Ptyp,
4970 Make_Op_Add (Loc,
4971 Left_Opnd =>
4972 Make_Integer_Literal (Loc,
4973 Enumeration_Rep (First_Literal (Ptyp))),
4974 Right_Opnd =>
4975 Make_Function_Call (Loc,
4976 Name =>
4977 New_Occurrence_Of
4978 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4980 Parameter_Associations =>
4981 New_List (
4982 Unchecked_Convert_To (Ptyp,
4983 Make_Op_Subtract (Loc,
4984 Left_Opnd =>
4985 Unchecked_Convert_To (Standard_Integer,
4986 Relocate_Node (First (Exprs))),
4987 Right_Opnd =>
4988 Make_Integer_Literal (Loc, 1))),
4989 Rep_To_Pos_Flag (Ptyp, Loc))))));
4991 else
4992 -- Add Boolean parameter True, to request program errror if
4993 -- we have a bad representation on our hands. If checks are
4994 -- suppressed, then add False instead
4996 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
4997 Rewrite (N,
4998 Make_Indexed_Component (Loc,
4999 Prefix =>
5000 New_Occurrence_Of
5001 (Enum_Pos_To_Rep (Etyp), Loc),
5002 Expressions => New_List (
5003 Make_Op_Subtract (Loc,
5004 Left_Opnd =>
5005 Make_Function_Call (Loc,
5006 Name =>
5007 New_Occurrence_Of
5008 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5009 Parameter_Associations => Exprs),
5010 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
5011 end if;
5013 Analyze_And_Resolve (N, Typ);
5015 -- For floating-point, we transform 'Pred into a call to the Pred
5016 -- floating-point attribute function in Fat_xxx (xxx is root type).
5017 -- Note that this function takes care of the overflow case.
5019 elsif Is_Floating_Point_Type (Ptyp) then
5020 Expand_Fpt_Attribute_R (N);
5021 Analyze_And_Resolve (N, Typ);
5023 -- For modular types, nothing to do (no overflow, since wraps)
5025 elsif Is_Modular_Integer_Type (Ptyp) then
5026 null;
5028 -- For other types, if argument is marked as needing a range check or
5029 -- overflow checking is enabled, we must generate a check.
5031 elsif not Overflow_Checks_Suppressed (Ptyp)
5032 or else Do_Range_Check (First (Exprs))
5033 then
5034 Set_Do_Range_Check (First (Exprs), False);
5035 Expand_Pred_Succ_Attribute (N);
5036 end if;
5037 end Pred;
5039 --------------
5040 -- Priority --
5041 --------------
5043 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5045 -- We rewrite X'Priority as the following run-time call:
5047 -- Get_Ceiling (X._Object)
5049 -- Note that although X'Priority is notionally an object, it is quite
5050 -- deliberately not defined as an aliased object in the RM. This means
5051 -- that it works fine to rewrite it as a call, without having to worry
5052 -- about complications that would other arise from X'Priority'Access,
5053 -- which is illegal, because of the lack of aliasing.
5055 when Attribute_Priority =>
5056 declare
5057 Call : Node_Id;
5058 Conctyp : Entity_Id;
5059 Object_Parm : Node_Id;
5060 Subprg : Entity_Id;
5061 RT_Subprg_Name : Node_Id;
5063 begin
5064 -- Look for the enclosing concurrent type
5066 Conctyp := Current_Scope;
5067 while not Is_Concurrent_Type (Conctyp) loop
5068 Conctyp := Scope (Conctyp);
5069 end loop;
5071 pragma Assert (Is_Protected_Type (Conctyp));
5073 -- Generate the actual of the call
5075 Subprg := Current_Scope;
5076 while not Present (Protected_Body_Subprogram (Subprg)) loop
5077 Subprg := Scope (Subprg);
5078 end loop;
5080 -- Use of 'Priority inside protected entries and barriers (in
5081 -- both cases the type of the first formal of their expanded
5082 -- subprogram is Address)
5084 if Etype (First_Entity (Protected_Body_Subprogram (Subprg))) =
5085 RTE (RE_Address)
5086 then
5087 declare
5088 New_Itype : Entity_Id;
5090 begin
5091 -- In the expansion of protected entries the type of the
5092 -- first formal of the Protected_Body_Subprogram is an
5093 -- Address. In order to reference the _object component
5094 -- we generate:
5096 -- type T is access p__ptTV;
5097 -- freeze T []
5099 New_Itype := Create_Itype (E_Access_Type, N);
5100 Set_Etype (New_Itype, New_Itype);
5101 Set_Directly_Designated_Type (New_Itype,
5102 Corresponding_Record_Type (Conctyp));
5103 Freeze_Itype (New_Itype, N);
5105 -- Generate:
5106 -- T!(O)._object'unchecked_access
5108 Object_Parm :=
5109 Make_Attribute_Reference (Loc,
5110 Prefix =>
5111 Make_Selected_Component (Loc,
5112 Prefix =>
5113 Unchecked_Convert_To (New_Itype,
5114 New_Occurrence_Of
5115 (First_Entity
5116 (Protected_Body_Subprogram (Subprg)),
5117 Loc)),
5118 Selector_Name =>
5119 Make_Identifier (Loc, Name_uObject)),
5120 Attribute_Name => Name_Unchecked_Access);
5121 end;
5123 -- Use of 'Priority inside a protected subprogram
5125 else
5126 Object_Parm :=
5127 Make_Attribute_Reference (Loc,
5128 Prefix =>
5129 Make_Selected_Component (Loc,
5130 Prefix => New_Occurrence_Of
5131 (First_Entity
5132 (Protected_Body_Subprogram (Subprg)),
5133 Loc),
5134 Selector_Name => Make_Identifier (Loc, Name_uObject)),
5135 Attribute_Name => Name_Unchecked_Access);
5136 end if;
5138 -- Select the appropriate run-time subprogram
5140 if Number_Entries (Conctyp) = 0 then
5141 RT_Subprg_Name :=
5142 New_Occurrence_Of (RTE (RE_Get_Ceiling), Loc);
5143 else
5144 RT_Subprg_Name :=
5145 New_Occurrence_Of (RTE (RO_PE_Get_Ceiling), Loc);
5146 end if;
5148 Call :=
5149 Make_Function_Call (Loc,
5150 Name => RT_Subprg_Name,
5151 Parameter_Associations => New_List (Object_Parm));
5153 Rewrite (N, Call);
5155 -- Avoid the generation of extra checks on the pointer to the
5156 -- protected object.
5158 Analyze_And_Resolve (N, Typ, Suppress => Access_Check);
5159 end;
5161 ------------------
5162 -- Range_Length --
5163 ------------------
5165 when Attribute_Range_Length => Range_Length : begin
5167 -- The only special processing required is for the case where
5168 -- Range_Length is applied to an enumeration type with holes.
5169 -- In this case we transform
5171 -- X'Range_Length
5173 -- to
5175 -- X'Pos (X'Last) - X'Pos (X'First) + 1
5177 -- So that the result reflects the proper Pos values instead
5178 -- of the underlying representations.
5180 if Is_Enumeration_Type (Ptyp)
5181 and then Has_Non_Standard_Rep (Ptyp)
5182 then
5183 Rewrite (N,
5184 Make_Op_Add (Loc,
5185 Left_Opnd =>
5186 Make_Op_Subtract (Loc,
5187 Left_Opnd =>
5188 Make_Attribute_Reference (Loc,
5189 Attribute_Name => Name_Pos,
5190 Prefix => New_Occurrence_Of (Ptyp, Loc),
5191 Expressions => New_List (
5192 Make_Attribute_Reference (Loc,
5193 Attribute_Name => Name_Last,
5194 Prefix => New_Occurrence_Of (Ptyp, Loc)))),
5196 Right_Opnd =>
5197 Make_Attribute_Reference (Loc,
5198 Attribute_Name => Name_Pos,
5199 Prefix => New_Occurrence_Of (Ptyp, Loc),
5200 Expressions => New_List (
5201 Make_Attribute_Reference (Loc,
5202 Attribute_Name => Name_First,
5203 Prefix => New_Occurrence_Of (Ptyp, Loc))))),
5205 Right_Opnd => Make_Integer_Literal (Loc, 1)));
5207 Analyze_And_Resolve (N, Typ);
5209 -- For all other cases, the attribute is handled by the back end, but
5210 -- we need to deal with the case of the range check on a universal
5211 -- integer.
5213 else
5214 Apply_Universal_Integer_Attribute_Checks (N);
5215 end if;
5216 end Range_Length;
5218 ----------
5219 -- Read --
5220 ----------
5222 when Attribute_Read => Read : declare
5223 P_Type : constant Entity_Id := Entity (Pref);
5224 B_Type : constant Entity_Id := Base_Type (P_Type);
5225 U_Type : constant Entity_Id := Underlying_Type (P_Type);
5226 Pname : Entity_Id;
5227 Decl : Node_Id;
5228 Prag : Node_Id;
5229 Arg2 : Node_Id;
5230 Rfunc : Node_Id;
5231 Lhs : Node_Id;
5232 Rhs : Node_Id;
5234 begin
5235 -- If no underlying type, we have an error that will be diagnosed
5236 -- elsewhere, so here we just completely ignore the expansion.
5238 if No (U_Type) then
5239 return;
5240 end if;
5242 -- Stream operations can appear in user code even if the restriction
5243 -- No_Streams is active (for example, when instantiating a predefined
5244 -- container). In that case rewrite the attribute as a Raise to
5245 -- prevent any run-time use.
5247 if Restriction_Active (No_Streams) then
5248 Rewrite (N,
5249 Make_Raise_Program_Error (Sloc (N),
5250 Reason => PE_Stream_Operation_Not_Allowed));
5251 Set_Etype (N, B_Type);
5252 return;
5253 end if;
5255 -- The simple case, if there is a TSS for Read, just call it
5257 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
5259 if Present (Pname) then
5260 null;
5262 else
5263 -- If there is a Stream_Convert pragma, use it, we rewrite
5265 -- sourcetyp'Read (stream, Item)
5267 -- as
5269 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
5271 -- where strmread is the given Read function that converts an
5272 -- argument of type strmtyp to type sourcetyp or a type from which
5273 -- it is derived. The conversion to sourcetyp is required in the
5274 -- latter case.
5276 -- A special case arises if Item is a type conversion in which
5277 -- case, we have to expand to:
5279 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
5281 -- where Itemx is the expression of the type conversion (i.e.
5282 -- the actual object), and typex is the type of Itemx.
5284 Prag := Get_Stream_Convert_Pragma (P_Type);
5286 if Present (Prag) then
5287 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
5288 Rfunc := Entity (Expression (Arg2));
5289 Lhs := Relocate_Node (Next (First (Exprs)));
5290 Rhs :=
5291 OK_Convert_To (B_Type,
5292 Make_Function_Call (Loc,
5293 Name => New_Occurrence_Of (Rfunc, Loc),
5294 Parameter_Associations => New_List (
5295 Make_Attribute_Reference (Loc,
5296 Prefix =>
5297 New_Occurrence_Of
5298 (Etype (First_Formal (Rfunc)), Loc),
5299 Attribute_Name => Name_Input,
5300 Expressions => New_List (
5301 Relocate_Node (First (Exprs)))))));
5303 if Nkind (Lhs) = N_Type_Conversion then
5304 Lhs := Expression (Lhs);
5305 Rhs := Convert_To (Etype (Lhs), Rhs);
5306 end if;
5308 Rewrite (N,
5309 Make_Assignment_Statement (Loc,
5310 Name => Lhs,
5311 Expression => Rhs));
5312 Set_Assignment_OK (Lhs);
5313 Analyze (N);
5314 return;
5316 -- For elementary types, we call the I_xxx routine using the first
5317 -- parameter and then assign the result into the second parameter.
5318 -- We set Assignment_OK to deal with the conversion case.
5320 elsif Is_Elementary_Type (U_Type) then
5321 declare
5322 Lhs : Node_Id;
5323 Rhs : Node_Id;
5325 begin
5326 Lhs := Relocate_Node (Next (First (Exprs)));
5327 Rhs := Build_Elementary_Input_Call (N);
5329 if Nkind (Lhs) = N_Type_Conversion then
5330 Lhs := Expression (Lhs);
5331 Rhs := Convert_To (Etype (Lhs), Rhs);
5332 end if;
5334 Set_Assignment_OK (Lhs);
5336 Rewrite (N,
5337 Make_Assignment_Statement (Loc,
5338 Name => Lhs,
5339 Expression => Rhs));
5341 Analyze (N);
5342 return;
5343 end;
5345 -- Array type case
5347 elsif Is_Array_Type (U_Type) then
5348 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
5349 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5351 -- Tagged type case, use the primitive Read function. Note that
5352 -- this will dispatch in the class-wide case which is what we want
5354 elsif Is_Tagged_Type (U_Type) then
5355 Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
5357 -- All other record type cases, including protected records. The
5358 -- latter only arise for expander generated code for handling
5359 -- shared passive partition access.
5361 else
5362 pragma Assert
5363 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5365 -- Ada 2005 (AI-216): Program_Error is raised when executing
5366 -- the default implementation of the Read attribute of an
5367 -- Unchecked_Union type.
5369 if Is_Unchecked_Union (Base_Type (U_Type)) then
5370 Insert_Action (N,
5371 Make_Raise_Program_Error (Loc,
5372 Reason => PE_Unchecked_Union_Restriction));
5373 end if;
5375 if Has_Discriminants (U_Type)
5376 and then Present
5377 (Discriminant_Default_Value (First_Discriminant (U_Type)))
5378 then
5379 Build_Mutable_Record_Read_Procedure
5380 (Loc, Full_Base (U_Type), Decl, Pname);
5381 else
5382 Build_Record_Read_Procedure
5383 (Loc, Full_Base (U_Type), Decl, Pname);
5384 end if;
5386 -- Suppress checks, uninitialized or otherwise invalid
5387 -- data does not cause constraint errors to be raised for
5388 -- a complete record read.
5390 Insert_Action (N, Decl, All_Checks);
5391 end if;
5392 end if;
5394 Rewrite_Stream_Proc_Call (Pname);
5395 end Read;
5397 ---------
5398 -- Ref --
5399 ---------
5401 -- Ref is identical to To_Address, see To_Address for processing
5403 ---------------
5404 -- Remainder --
5405 ---------------
5407 -- Transforms 'Remainder into a call to the floating-point attribute
5408 -- function Remainder in Fat_xxx (where xxx is the root type)
5410 when Attribute_Remainder =>
5411 Expand_Fpt_Attribute_RR (N);
5413 ------------
5414 -- Result --
5415 ------------
5417 -- Transform 'Result into reference to _Result formal. At the point
5418 -- where a legal 'Result attribute is expanded, we know that we are in
5419 -- the context of a _Postcondition function with a _Result parameter.
5421 when Attribute_Result =>
5422 Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult));
5423 Analyze_And_Resolve (N, Typ);
5425 -----------
5426 -- Round --
5427 -----------
5429 -- The handling of the Round attribute is quite delicate. The processing
5430 -- in Sem_Attr introduced a conversion to universal real, reflecting the
5431 -- semantics of Round, but we do not want anything to do with universal
5432 -- real at runtime, since this corresponds to using floating-point
5433 -- arithmetic.
5435 -- What we have now is that the Etype of the Round attribute correctly
5436 -- indicates the final result type. The operand of the Round is the
5437 -- conversion to universal real, described above, and the operand of
5438 -- this conversion is the actual operand of Round, which may be the
5439 -- special case of a fixed point multiplication or division (Etype =
5440 -- universal fixed)
5442 -- The exapander will expand first the operand of the conversion, then
5443 -- the conversion, and finally the round attribute itself, since we
5444 -- always work inside out. But we cannot simply process naively in this
5445 -- order. In the semantic world where universal fixed and real really
5446 -- exist and have infinite precision, there is no problem, but in the
5447 -- implementation world, where universal real is a floating-point type,
5448 -- we would get the wrong result.
5450 -- So the approach is as follows. First, when expanding a multiply or
5451 -- divide whose type is universal fixed, we do nothing at all, instead
5452 -- deferring the operation till later.
5454 -- The actual processing is done in Expand_N_Type_Conversion which
5455 -- handles the special case of Round by looking at its parent to see if
5456 -- it is a Round attribute, and if it is, handling the conversion (or
5457 -- its fixed multiply/divide child) in an appropriate manner.
5459 -- This means that by the time we get to expanding the Round attribute
5460 -- itself, the Round is nothing more than a type conversion (and will
5461 -- often be a null type conversion), so we just replace it with the
5462 -- appropriate conversion operation.
5464 when Attribute_Round =>
5465 Rewrite (N,
5466 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
5467 Analyze_And_Resolve (N);
5469 --------------
5470 -- Rounding --
5471 --------------
5473 -- Transforms 'Rounding into a call to the floating-point attribute
5474 -- function Rounding in Fat_xxx (where xxx is the root type)
5475 -- Expansion is avoided for cases the back end can handle directly.
5477 when Attribute_Rounding =>
5478 if not Is_Inline_Floating_Point_Attribute (N) then
5479 Expand_Fpt_Attribute_R (N);
5480 end if;
5482 -------------
5483 -- Scaling --
5484 -------------
5486 -- Transforms 'Scaling into a call to the floating-point attribute
5487 -- function Scaling in Fat_xxx (where xxx is the root type)
5489 when Attribute_Scaling =>
5490 Expand_Fpt_Attribute_RI (N);
5492 -------------------------
5493 -- Simple_Storage_Pool --
5494 -------------------------
5496 when Attribute_Simple_Storage_Pool =>
5497 Rewrite (N,
5498 Make_Type_Conversion (Loc,
5499 Subtype_Mark => New_Occurrence_Of (Etype (N), Loc),
5500 Expression => New_Occurrence_Of (Entity (N), Loc)));
5501 Analyze_And_Resolve (N, Typ);
5503 ----------
5504 -- Size --
5505 ----------
5507 when Attribute_Size |
5508 Attribute_Object_Size |
5509 Attribute_Value_Size |
5510 Attribute_VADS_Size => Size :
5512 declare
5513 Siz : Uint;
5514 New_Node : Node_Id;
5516 begin
5517 -- Processing for VADS_Size case. Note that this processing removes
5518 -- all traces of VADS_Size from the tree, and completes all required
5519 -- processing for VADS_Size by translating the attribute reference
5520 -- to an appropriate Size or Object_Size reference.
5522 if Id = Attribute_VADS_Size
5523 or else (Use_VADS_Size and then Id = Attribute_Size)
5524 then
5525 -- If the size is specified, then we simply use the specified
5526 -- size. This applies to both types and objects. The size of an
5527 -- object can be specified in the following ways:
5529 -- An explicit size object is given for an object
5530 -- A component size is specified for an indexed component
5531 -- A component clause is specified for a selected component
5532 -- The object is a component of a packed composite object
5534 -- If the size is specified, then VADS_Size of an object
5536 if (Is_Entity_Name (Pref)
5537 and then Present (Size_Clause (Entity (Pref))))
5538 or else
5539 (Nkind (Pref) = N_Component_Clause
5540 and then (Present (Component_Clause
5541 (Entity (Selector_Name (Pref))))
5542 or else Is_Packed (Etype (Prefix (Pref)))))
5543 or else
5544 (Nkind (Pref) = N_Indexed_Component
5545 and then (Component_Size (Etype (Prefix (Pref))) /= 0
5546 or else Is_Packed (Etype (Prefix (Pref)))))
5547 then
5548 Set_Attribute_Name (N, Name_Size);
5550 -- Otherwise if we have an object rather than a type, then the
5551 -- VADS_Size attribute applies to the type of the object, rather
5552 -- than the object itself. This is one of the respects in which
5553 -- VADS_Size differs from Size.
5555 else
5556 if (not Is_Entity_Name (Pref)
5557 or else not Is_Type (Entity (Pref)))
5558 and then (Is_Scalar_Type (Ptyp) or else Is_Constrained (Ptyp))
5559 then
5560 Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc));
5561 end if;
5563 -- For a scalar type for which no size was explicitly given,
5564 -- VADS_Size means Object_Size. This is the other respect in
5565 -- which VADS_Size differs from Size.
5567 if Is_Scalar_Type (Ptyp) and then No (Size_Clause (Ptyp)) then
5568 Set_Attribute_Name (N, Name_Object_Size);
5570 -- In all other cases, Size and VADS_Size are the sane
5572 else
5573 Set_Attribute_Name (N, Name_Size);
5574 end if;
5575 end if;
5576 end if;
5578 -- If the prefix is X'Class, we transform it into a direct reference
5579 -- to the class-wide type, because the back end must not see a 'Class
5580 -- reference.
5582 if Is_Entity_Name (Pref)
5583 and then Is_Class_Wide_Type (Entity (Pref))
5584 then
5585 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
5586 return;
5588 -- For X'Size applied to an object of a class-wide type, transform
5589 -- X'Size into a call to the primitive operation _Size applied to X.
5591 elsif Is_Class_Wide_Type (Ptyp) then
5593 -- No need to do anything else compiling under restriction
5594 -- No_Dispatching_Calls. During the semantic analysis we
5595 -- already noted this restriction violation.
5597 if Restriction_Active (No_Dispatching_Calls) then
5598 return;
5599 end if;
5601 New_Node :=
5602 Make_Function_Call (Loc,
5603 Name => New_Occurrence_Of
5604 (Find_Prim_Op (Ptyp, Name_uSize), Loc),
5605 Parameter_Associations => New_List (Pref));
5607 if Typ /= Standard_Long_Long_Integer then
5609 -- The context is a specific integer type with which the
5610 -- original attribute was compatible. The function has a
5611 -- specific type as well, so to preserve the compatibility
5612 -- we must convert explicitly.
5614 New_Node := Convert_To (Typ, New_Node);
5615 end if;
5617 Rewrite (N, New_Node);
5618 Analyze_And_Resolve (N, Typ);
5619 return;
5621 -- Case of known RM_Size of a type
5623 elsif (Id = Attribute_Size or else Id = Attribute_Value_Size)
5624 and then Is_Entity_Name (Pref)
5625 and then Is_Type (Entity (Pref))
5626 and then Known_Static_RM_Size (Entity (Pref))
5627 then
5628 Siz := RM_Size (Entity (Pref));
5630 -- Case of known Esize of a type
5632 elsif Id = Attribute_Object_Size
5633 and then Is_Entity_Name (Pref)
5634 and then Is_Type (Entity (Pref))
5635 and then Known_Static_Esize (Entity (Pref))
5636 then
5637 Siz := Esize (Entity (Pref));
5639 -- Case of known size of object
5641 elsif Id = Attribute_Size
5642 and then Is_Entity_Name (Pref)
5643 and then Is_Object (Entity (Pref))
5644 and then Known_Esize (Entity (Pref))
5645 and then Known_Static_Esize (Entity (Pref))
5646 then
5647 Siz := Esize (Entity (Pref));
5649 -- For an array component, we can do Size in the front end
5650 -- if the component_size of the array is set.
5652 elsif Nkind (Pref) = N_Indexed_Component then
5653 Siz := Component_Size (Etype (Prefix (Pref)));
5655 -- For a record component, we can do Size in the front end if there
5656 -- is a component clause, or if the record is packed and the
5657 -- component's size is known at compile time.
5659 elsif Nkind (Pref) = N_Selected_Component then
5660 declare
5661 Rec : constant Entity_Id := Etype (Prefix (Pref));
5662 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
5664 begin
5665 if Present (Component_Clause (Comp)) then
5666 Siz := Esize (Comp);
5668 elsif Is_Packed (Rec) then
5669 Siz := RM_Size (Ptyp);
5671 else
5672 Apply_Universal_Integer_Attribute_Checks (N);
5673 return;
5674 end if;
5675 end;
5677 -- All other cases are handled by the back end
5679 else
5680 Apply_Universal_Integer_Attribute_Checks (N);
5682 -- If Size is applied to a formal parameter that is of a packed
5683 -- array subtype, then apply Size to the actual subtype.
5685 if Is_Entity_Name (Pref)
5686 and then Is_Formal (Entity (Pref))
5687 and then Is_Array_Type (Ptyp)
5688 and then Is_Packed (Ptyp)
5689 then
5690 Rewrite (N,
5691 Make_Attribute_Reference (Loc,
5692 Prefix =>
5693 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
5694 Attribute_Name => Name_Size));
5695 Analyze_And_Resolve (N, Typ);
5696 end if;
5698 -- If Size applies to a dereference of an access to unconstrained
5699 -- packed array, the back end needs to see its unconstrained
5700 -- nominal type, but also a hint to the actual constrained type.
5702 if Nkind (Pref) = N_Explicit_Dereference
5703 and then Is_Array_Type (Ptyp)
5704 and then not Is_Constrained (Ptyp)
5705 and then Is_Packed (Ptyp)
5706 then
5707 Set_Actual_Designated_Subtype (Pref,
5708 Get_Actual_Subtype (Pref));
5709 end if;
5711 return;
5712 end if;
5714 -- Common processing for record and array component case
5716 if Siz /= No_Uint and then Siz /= 0 then
5717 declare
5718 CS : constant Boolean := Comes_From_Source (N);
5720 begin
5721 Rewrite (N, Make_Integer_Literal (Loc, Siz));
5723 -- This integer literal is not a static expression. We do not
5724 -- call Analyze_And_Resolve here, because this would activate
5725 -- the circuit for deciding that a static value was out of
5726 -- range, and we don't want that.
5728 -- So just manually set the type, mark the expression as non-
5729 -- static, and then ensure that the result is checked properly
5730 -- if the attribute comes from source (if it was internally
5731 -- generated, we never need a constraint check).
5733 Set_Etype (N, Typ);
5734 Set_Is_Static_Expression (N, False);
5736 if CS then
5737 Apply_Constraint_Check (N, Typ);
5738 end if;
5739 end;
5740 end if;
5741 end Size;
5743 ------------------
5744 -- Storage_Pool --
5745 ------------------
5747 when Attribute_Storage_Pool =>
5748 Rewrite (N,
5749 Make_Type_Conversion (Loc,
5750 Subtype_Mark => New_Occurrence_Of (Etype (N), Loc),
5751 Expression => New_Occurrence_Of (Entity (N), Loc)));
5752 Analyze_And_Resolve (N, Typ);
5754 ------------------
5755 -- Storage_Size --
5756 ------------------
5758 when Attribute_Storage_Size => Storage_Size : declare
5759 Alloc_Op : Entity_Id := Empty;
5761 begin
5763 -- Access type case, always go to the root type
5765 -- The case of access types results in a value of zero for the case
5766 -- where no storage size attribute clause has been given. If a
5767 -- storage size has been given, then the attribute is converted
5768 -- to a reference to the variable used to hold this value.
5770 if Is_Access_Type (Ptyp) then
5771 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
5772 Rewrite (N,
5773 Make_Attribute_Reference (Loc,
5774 Prefix => New_Occurrence_Of (Typ, Loc),
5775 Attribute_Name => Name_Max,
5776 Expressions => New_List (
5777 Make_Integer_Literal (Loc, 0),
5778 Convert_To (Typ,
5779 New_Occurrence_Of
5780 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
5782 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
5784 -- If the access type is associated with a simple storage pool
5785 -- object, then attempt to locate the optional Storage_Size
5786 -- function of the simple storage pool type. If not found,
5787 -- then the result will default to zero.
5789 if Present (Get_Rep_Pragma (Root_Type (Ptyp),
5790 Name_Simple_Storage_Pool_Type))
5791 then
5792 declare
5793 Pool_Type : constant Entity_Id :=
5794 Base_Type (Etype (Entity (N)));
5796 begin
5797 Alloc_Op := Get_Name_Entity_Id (Name_Storage_Size);
5798 while Present (Alloc_Op) loop
5799 if Scope (Alloc_Op) = Scope (Pool_Type)
5800 and then Present (First_Formal (Alloc_Op))
5801 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
5802 then
5803 exit;
5804 end if;
5806 Alloc_Op := Homonym (Alloc_Op);
5807 end loop;
5808 end;
5810 -- In the normal Storage_Pool case, retrieve the primitive
5811 -- function associated with the pool type.
5813 else
5814 Alloc_Op :=
5815 Find_Prim_Op
5816 (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
5817 Attribute_Name (N));
5818 end if;
5820 -- If Storage_Size wasn't found (can only occur in the simple
5821 -- storage pool case), then simply use zero for the result.
5823 if not Present (Alloc_Op) then
5824 Rewrite (N, Make_Integer_Literal (Loc, 0));
5826 -- Otherwise, rewrite the allocator as a call to pool type's
5827 -- Storage_Size function.
5829 else
5830 Rewrite (N,
5831 OK_Convert_To (Typ,
5832 Make_Function_Call (Loc,
5833 Name =>
5834 New_Occurrence_Of (Alloc_Op, Loc),
5836 Parameter_Associations => New_List (
5837 New_Occurrence_Of
5838 (Associated_Storage_Pool
5839 (Root_Type (Ptyp)), Loc)))));
5840 end if;
5842 else
5843 Rewrite (N, Make_Integer_Literal (Loc, 0));
5844 end if;
5846 Analyze_And_Resolve (N, Typ);
5848 -- For tasks, we retrieve the size directly from the TCB. The
5849 -- size may depend on a discriminant of the type, and therefore
5850 -- can be a per-object expression, so type-level information is
5851 -- not sufficient in general. There are four cases to consider:
5853 -- a) If the attribute appears within a task body, the designated
5854 -- TCB is obtained by a call to Self.
5856 -- b) If the prefix of the attribute is the name of a task object,
5857 -- the designated TCB is the one stored in the corresponding record.
5859 -- c) If the prefix is a task type, the size is obtained from the
5860 -- size variable created for each task type
5862 -- d) If no Storage_Size was specified for the type, there is no
5863 -- size variable, and the value is a system-specific default.
5865 else
5866 if In_Open_Scopes (Ptyp) then
5868 -- Storage_Size (Self)
5870 Rewrite (N,
5871 Convert_To (Typ,
5872 Make_Function_Call (Loc,
5873 Name =>
5874 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
5875 Parameter_Associations =>
5876 New_List (
5877 Make_Function_Call (Loc,
5878 Name =>
5879 New_Occurrence_Of (RTE (RE_Self), Loc))))));
5881 elsif not Is_Entity_Name (Pref)
5882 or else not Is_Type (Entity (Pref))
5883 then
5884 -- Storage_Size (Rec (Obj).Size)
5886 Rewrite (N,
5887 Convert_To (Typ,
5888 Make_Function_Call (Loc,
5889 Name =>
5890 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
5891 Parameter_Associations =>
5892 New_List (
5893 Make_Selected_Component (Loc,
5894 Prefix =>
5895 Unchecked_Convert_To (
5896 Corresponding_Record_Type (Ptyp),
5897 New_Copy_Tree (Pref)),
5898 Selector_Name =>
5899 Make_Identifier (Loc, Name_uTask_Id))))));
5901 elsif Present (Storage_Size_Variable (Ptyp)) then
5903 -- Static Storage_Size pragma given for type: retrieve value
5904 -- from its allocated storage variable.
5906 Rewrite (N,
5907 Convert_To (Typ,
5908 Make_Function_Call (Loc,
5909 Name => New_Occurrence_Of (
5910 RTE (RE_Adjust_Storage_Size), Loc),
5911 Parameter_Associations =>
5912 New_List (
5913 New_Occurrence_Of (
5914 Storage_Size_Variable (Ptyp), Loc)))));
5915 else
5916 -- Get system default
5918 Rewrite (N,
5919 Convert_To (Typ,
5920 Make_Function_Call (Loc,
5921 Name =>
5922 New_Occurrence_Of (
5923 RTE (RE_Default_Stack_Size), Loc))));
5924 end if;
5926 Analyze_And_Resolve (N, Typ);
5927 end if;
5928 end Storage_Size;
5930 -----------------
5931 -- Stream_Size --
5932 -----------------
5934 when Attribute_Stream_Size =>
5935 Rewrite (N,
5936 Make_Integer_Literal (Loc, Intval => Get_Stream_Size (Ptyp)));
5937 Analyze_And_Resolve (N, Typ);
5939 ----------
5940 -- Succ --
5941 ----------
5943 -- 1. Deal with enumeration types with holes.
5944 -- 2. For floating-point, generate call to attribute function.
5945 -- 3. For other cases, deal with constraint checking.
5947 when Attribute_Succ => Succ : declare
5948 Etyp : constant Entity_Id := Base_Type (Ptyp);
5950 begin
5952 -- For enumeration types with non-standard representations, we
5953 -- expand typ'Succ (x) into
5955 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
5957 -- If the representation is contiguous, we compute instead
5958 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
5960 if Is_Enumeration_Type (Ptyp)
5961 and then Present (Enum_Pos_To_Rep (Etyp))
5962 then
5963 if Has_Contiguous_Rep (Etyp) then
5964 Rewrite (N,
5965 Unchecked_Convert_To (Ptyp,
5966 Make_Op_Add (Loc,
5967 Left_Opnd =>
5968 Make_Integer_Literal (Loc,
5969 Enumeration_Rep (First_Literal (Ptyp))),
5970 Right_Opnd =>
5971 Make_Function_Call (Loc,
5972 Name =>
5973 New_Occurrence_Of
5974 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5976 Parameter_Associations =>
5977 New_List (
5978 Unchecked_Convert_To (Ptyp,
5979 Make_Op_Add (Loc,
5980 Left_Opnd =>
5981 Unchecked_Convert_To (Standard_Integer,
5982 Relocate_Node (First (Exprs))),
5983 Right_Opnd =>
5984 Make_Integer_Literal (Loc, 1))),
5985 Rep_To_Pos_Flag (Ptyp, Loc))))));
5986 else
5987 -- Add Boolean parameter True, to request program errror if
5988 -- we have a bad representation on our hands. Add False if
5989 -- checks are suppressed.
5991 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
5992 Rewrite (N,
5993 Make_Indexed_Component (Loc,
5994 Prefix =>
5995 New_Occurrence_Of
5996 (Enum_Pos_To_Rep (Etyp), Loc),
5997 Expressions => New_List (
5998 Make_Op_Add (Loc,
5999 Left_Opnd =>
6000 Make_Function_Call (Loc,
6001 Name =>
6002 New_Occurrence_Of
6003 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6004 Parameter_Associations => Exprs),
6005 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
6006 end if;
6008 Analyze_And_Resolve (N, Typ);
6010 -- For floating-point, we transform 'Succ into a call to the Succ
6011 -- floating-point attribute function in Fat_xxx (xxx is root type)
6013 elsif Is_Floating_Point_Type (Ptyp) then
6014 Expand_Fpt_Attribute_R (N);
6015 Analyze_And_Resolve (N, Typ);
6017 -- For modular types, nothing to do (no overflow, since wraps)
6019 elsif Is_Modular_Integer_Type (Ptyp) then
6020 null;
6022 -- For other types, if argument is marked as needing a range check or
6023 -- overflow checking is enabled, we must generate a check.
6025 elsif not Overflow_Checks_Suppressed (Ptyp)
6026 or else Do_Range_Check (First (Exprs))
6027 then
6028 Set_Do_Range_Check (First (Exprs), False);
6029 Expand_Pred_Succ_Attribute (N);
6030 end if;
6031 end Succ;
6033 ---------
6034 -- Tag --
6035 ---------
6037 -- Transforms X'Tag into a direct reference to the tag of X
6039 when Attribute_Tag => Tag : declare
6040 Ttyp : Entity_Id;
6041 Prefix_Is_Type : Boolean;
6043 begin
6044 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
6045 Ttyp := Entity (Pref);
6046 Prefix_Is_Type := True;
6047 else
6048 Ttyp := Ptyp;
6049 Prefix_Is_Type := False;
6050 end if;
6052 if Is_Class_Wide_Type (Ttyp) then
6053 Ttyp := Root_Type (Ttyp);
6054 end if;
6056 Ttyp := Underlying_Type (Ttyp);
6058 -- Ada 2005: The type may be a synchronized tagged type, in which
6059 -- case the tag information is stored in the corresponding record.
6061 if Is_Concurrent_Type (Ttyp) then
6062 Ttyp := Corresponding_Record_Type (Ttyp);
6063 end if;
6065 if Prefix_Is_Type then
6067 -- For VMs we leave the type attribute unexpanded because
6068 -- there's not a dispatching table to reference.
6070 if Tagged_Type_Expansion then
6071 Rewrite (N,
6072 Unchecked_Convert_To (RTE (RE_Tag),
6073 New_Occurrence_Of
6074 (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc)));
6075 Analyze_And_Resolve (N, RTE (RE_Tag));
6076 end if;
6078 -- Ada 2005 (AI-251): The use of 'Tag in the sources always
6079 -- references the primary tag of the actual object. If 'Tag is
6080 -- applied to class-wide interface objects we generate code that
6081 -- displaces "this" to reference the base of the object.
6083 elsif Comes_From_Source (N)
6084 and then Is_Class_Wide_Type (Etype (Prefix (N)))
6085 and then Is_Interface (Etype (Prefix (N)))
6086 then
6087 -- Generate:
6088 -- (To_Tag_Ptr (Prefix'Address)).all
6090 -- Note that Prefix'Address is recursively expanded into a call
6091 -- to Base_Address (Obj.Tag)
6093 -- Not needed for VM targets, since all handled by the VM
6095 if Tagged_Type_Expansion then
6096 Rewrite (N,
6097 Make_Explicit_Dereference (Loc,
6098 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6099 Make_Attribute_Reference (Loc,
6100 Prefix => Relocate_Node (Pref),
6101 Attribute_Name => Name_Address))));
6102 Analyze_And_Resolve (N, RTE (RE_Tag));
6103 end if;
6105 else
6106 Rewrite (N,
6107 Make_Selected_Component (Loc,
6108 Prefix => Relocate_Node (Pref),
6109 Selector_Name =>
6110 New_Occurrence_Of (First_Tag_Component (Ttyp), Loc)));
6111 Analyze_And_Resolve (N, RTE (RE_Tag));
6112 end if;
6113 end Tag;
6115 ----------------
6116 -- Terminated --
6117 ----------------
6119 -- Transforms 'Terminated attribute into a call to Terminated function
6121 when Attribute_Terminated => Terminated :
6122 begin
6123 -- The prefix of Terminated is of a task interface class-wide type.
6124 -- Generate:
6125 -- terminated (Task_Id (Pref._disp_get_task_id));
6127 if Ada_Version >= Ada_2005
6128 and then Ekind (Ptyp) = E_Class_Wide_Type
6129 and then Is_Interface (Ptyp)
6130 and then Is_Task_Interface (Ptyp)
6131 then
6132 Rewrite (N,
6133 Make_Function_Call (Loc,
6134 Name =>
6135 New_Occurrence_Of (RTE (RE_Terminated), Loc),
6136 Parameter_Associations => New_List (
6137 Make_Unchecked_Type_Conversion (Loc,
6138 Subtype_Mark =>
6139 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6140 Expression =>
6141 Make_Selected_Component (Loc,
6142 Prefix =>
6143 New_Copy_Tree (Pref),
6144 Selector_Name =>
6145 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
6147 elsif Restricted_Profile then
6148 Rewrite (N,
6149 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
6151 else
6152 Rewrite (N,
6153 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
6154 end if;
6156 Analyze_And_Resolve (N, Standard_Boolean);
6157 end Terminated;
6159 ----------------
6160 -- To_Address --
6161 ----------------
6163 -- Transforms System'To_Address (X) and System.Address'Ref (X) into
6164 -- unchecked conversion from (integral) type of X to type address.
6166 when Attribute_To_Address | Attribute_Ref =>
6167 Rewrite (N,
6168 Unchecked_Convert_To (RTE (RE_Address),
6169 Relocate_Node (First (Exprs))));
6170 Analyze_And_Resolve (N, RTE (RE_Address));
6172 ------------
6173 -- To_Any --
6174 ------------
6176 when Attribute_To_Any => To_Any : declare
6177 P_Type : constant Entity_Id := Etype (Pref);
6178 Decls : constant List_Id := New_List;
6179 begin
6180 Rewrite (N,
6181 Build_To_Any_Call
6182 (Loc,
6183 Convert_To (P_Type,
6184 Relocate_Node (First (Exprs))), Decls));
6185 Insert_Actions (N, Decls);
6186 Analyze_And_Resolve (N, RTE (RE_Any));
6187 end To_Any;
6189 ----------------
6190 -- Truncation --
6191 ----------------
6193 -- Transforms 'Truncation into a call to the floating-point attribute
6194 -- function Truncation in Fat_xxx (where xxx is the root type).
6195 -- Expansion is avoided for cases the back end can handle directly.
6197 when Attribute_Truncation =>
6198 if not Is_Inline_Floating_Point_Attribute (N) then
6199 Expand_Fpt_Attribute_R (N);
6200 end if;
6202 --------------
6203 -- TypeCode --
6204 --------------
6206 when Attribute_TypeCode => TypeCode : declare
6207 P_Type : constant Entity_Id := Etype (Pref);
6208 Decls : constant List_Id := New_List;
6209 begin
6210 Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls));
6211 Insert_Actions (N, Decls);
6212 Analyze_And_Resolve (N, RTE (RE_TypeCode));
6213 end TypeCode;
6215 -----------------------
6216 -- Unbiased_Rounding --
6217 -----------------------
6219 -- Transforms 'Unbiased_Rounding into a call to the floating-point
6220 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
6221 -- root type). Expansion is avoided for cases the back end can handle
6222 -- directly.
6224 when Attribute_Unbiased_Rounding =>
6225 if not Is_Inline_Floating_Point_Attribute (N) then
6226 Expand_Fpt_Attribute_R (N);
6227 end if;
6229 ------------
6230 -- Update --
6231 ------------
6233 when Attribute_Update =>
6234 Expand_Update_Attribute (N);
6236 ---------------
6237 -- VADS_Size --
6238 ---------------
6240 -- The processing for VADS_Size is shared with Size
6242 ---------
6243 -- Val --
6244 ---------
6246 -- For enumeration types with a standard representation, and for all
6247 -- other types, Val is handled by the back end. For enumeration types
6248 -- with a non-standard representation we use the _Pos_To_Rep array that
6249 -- was created when the type was frozen.
6251 when Attribute_Val => Val : declare
6252 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
6254 begin
6255 if Is_Enumeration_Type (Etyp)
6256 and then Present (Enum_Pos_To_Rep (Etyp))
6257 then
6258 if Has_Contiguous_Rep (Etyp) then
6259 declare
6260 Rep_Node : constant Node_Id :=
6261 Unchecked_Convert_To (Etyp,
6262 Make_Op_Add (Loc,
6263 Left_Opnd =>
6264 Make_Integer_Literal (Loc,
6265 Enumeration_Rep (First_Literal (Etyp))),
6266 Right_Opnd =>
6267 (Convert_To (Standard_Integer,
6268 Relocate_Node (First (Exprs))))));
6270 begin
6271 Rewrite (N,
6272 Unchecked_Convert_To (Etyp,
6273 Make_Op_Add (Loc,
6274 Left_Opnd =>
6275 Make_Integer_Literal (Loc,
6276 Enumeration_Rep (First_Literal (Etyp))),
6277 Right_Opnd =>
6278 Make_Function_Call (Loc,
6279 Name =>
6280 New_Occurrence_Of
6281 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6282 Parameter_Associations => New_List (
6283 Rep_Node,
6284 Rep_To_Pos_Flag (Etyp, Loc))))));
6285 end;
6287 else
6288 Rewrite (N,
6289 Make_Indexed_Component (Loc,
6290 Prefix => New_Occurrence_Of (Enum_Pos_To_Rep (Etyp), Loc),
6291 Expressions => New_List (
6292 Convert_To (Standard_Integer,
6293 Relocate_Node (First (Exprs))))));
6294 end if;
6296 Analyze_And_Resolve (N, Typ);
6298 -- If the argument is marked as requiring a range check then generate
6299 -- it here.
6301 elsif Do_Range_Check (First (Exprs)) then
6302 Generate_Range_Check (First (Exprs), Etyp, CE_Range_Check_Failed);
6303 end if;
6304 end Val;
6306 -----------
6307 -- Valid --
6308 -----------
6310 -- The code for valid is dependent on the particular types involved.
6311 -- See separate sections below for the generated code in each case.
6313 when Attribute_Valid => Valid : declare
6314 Btyp : Entity_Id := Base_Type (Ptyp);
6315 Tst : Node_Id;
6317 Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
6318 -- Save the validity checking mode. We always turn off validity
6319 -- checking during process of 'Valid since this is one place
6320 -- where we do not want the implicit validity checks to intefere
6321 -- with the explicit validity check that the programmer is doing.
6323 function Make_Range_Test return Node_Id;
6324 -- Build the code for a range test of the form
6325 -- Btyp!(Pref) in Btyp!(Ptyp'First) .. Btyp!(Ptyp'Last)
6327 ---------------------
6328 -- Make_Range_Test --
6329 ---------------------
6331 function Make_Range_Test return Node_Id is
6332 Temp : constant Node_Id := Duplicate_Subexpr (Pref);
6334 begin
6335 -- The value whose validity is being checked has been captured in
6336 -- an object declaration. We certainly don't want this object to
6337 -- appear valid because the declaration initializes it.
6339 if Is_Entity_Name (Temp) then
6340 Set_Is_Known_Valid (Entity (Temp), False);
6341 end if;
6343 return
6344 Make_In (Loc,
6345 Left_Opnd =>
6346 Unchecked_Convert_To (Btyp, Temp),
6347 Right_Opnd =>
6348 Make_Range (Loc,
6349 Low_Bound =>
6350 Unchecked_Convert_To (Btyp,
6351 Make_Attribute_Reference (Loc,
6352 Prefix => New_Occurrence_Of (Ptyp, Loc),
6353 Attribute_Name => Name_First)),
6354 High_Bound =>
6355 Unchecked_Convert_To (Btyp,
6356 Make_Attribute_Reference (Loc,
6357 Prefix => New_Occurrence_Of (Ptyp, Loc),
6358 Attribute_Name => Name_Last))));
6359 end Make_Range_Test;
6361 -- Start of processing for Attribute_Valid
6363 begin
6364 -- Do not expand sourced code 'Valid reference in CodePeer mode,
6365 -- will be handled by the back-end directly.
6367 if CodePeer_Mode and then Comes_From_Source (N) then
6368 return;
6369 end if;
6371 -- Turn off validity checks. We do not want any implicit validity
6372 -- checks to intefere with the explicit check from the attribute
6374 Validity_Checks_On := False;
6376 -- Retrieve the base type. Handle the case where the base type is a
6377 -- private enumeration type.
6379 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
6380 Btyp := Full_View (Btyp);
6381 end if;
6383 -- Floating-point case. This case is handled by the Valid attribute
6384 -- code in the floating-point attribute run-time library.
6386 if Is_Floating_Point_Type (Ptyp) then
6387 Float_Valid : declare
6388 Pkg : RE_Id;
6389 Ftp : Entity_Id;
6391 function Get_Fat_Entity (Nam : Name_Id) return Entity_Id;
6392 -- Return entity for Pkg.Nam
6394 --------------------
6395 -- Get_Fat_Entity --
6396 --------------------
6398 function Get_Fat_Entity (Nam : Name_Id) return Entity_Id is
6399 Exp_Name : constant Node_Id :=
6400 Make_Selected_Component (Loc,
6401 Prefix => New_Occurrence_Of (RTE (Pkg), Loc),
6402 Selector_Name => Make_Identifier (Loc, Nam));
6403 begin
6404 Find_Selected_Component (Exp_Name);
6405 return Entity (Exp_Name);
6406 end Get_Fat_Entity;
6408 -- Start of processing for Float_Valid
6410 begin
6411 -- The C and AAMP back-ends handle Valid for fpt types
6413 if Generate_C_Code or else Float_Rep (Btyp) = AAMP then
6414 Analyze_And_Resolve (Pref, Ptyp);
6415 Set_Etype (N, Standard_Boolean);
6416 Set_Analyzed (N);
6418 else
6419 Find_Fat_Info (Ptyp, Ftp, Pkg);
6421 -- If the prefix is a reverse SSO component, or is possibly
6422 -- unaligned, first create a temporary copy that is in
6423 -- native SSO, and properly aligned. Make it Volatile to
6424 -- prevent folding in the back-end. Note that we use an
6425 -- intermediate constrained string type to initialize the
6426 -- temporary, as the value at hand might be invalid, and in
6427 -- that case it cannot be copied using a floating point
6428 -- register.
6430 if In_Reverse_Storage_Order_Object (Pref)
6431 or else Is_Possibly_Unaligned_Object (Pref)
6432 then
6433 declare
6434 Temp : constant Entity_Id :=
6435 Make_Temporary (Loc, 'F');
6437 Fat_S : constant Entity_Id :=
6438 Get_Fat_Entity (Name_S);
6439 -- Constrained string subtype of appropriate size
6441 Fat_P : constant Entity_Id :=
6442 Get_Fat_Entity (Name_P);
6443 -- Access to Fat_S
6445 Decl : constant Node_Id :=
6446 Make_Object_Declaration (Loc,
6447 Defining_Identifier => Temp,
6448 Aliased_Present => True,
6449 Object_Definition =>
6450 New_Occurrence_Of (Ptyp, Loc));
6452 begin
6453 Set_Aspect_Specifications (Decl, New_List (
6454 Make_Aspect_Specification (Loc,
6455 Identifier =>
6456 Make_Identifier (Loc, Name_Volatile))));
6458 Insert_Actions (N,
6459 New_List (
6460 Decl,
6462 Make_Assignment_Statement (Loc,
6463 Name =>
6464 Make_Explicit_Dereference (Loc,
6465 Prefix =>
6466 Unchecked_Convert_To (Fat_P,
6467 Make_Attribute_Reference (Loc,
6468 Prefix =>
6469 New_Occurrence_Of (Temp, Loc),
6470 Attribute_Name =>
6471 Name_Unrestricted_Access))),
6472 Expression =>
6473 Unchecked_Convert_To (Fat_S,
6474 Relocate_Node (Pref)))),
6476 Suppress => All_Checks);
6478 Rewrite (Pref, New_Occurrence_Of (Temp, Loc));
6479 end;
6480 end if;
6482 -- We now have an object of the proper endianness and
6483 -- alignment, and can construct a Valid attribute.
6485 -- We make sure the prefix of this valid attribute is
6486 -- marked as not coming from source, to avoid losing
6487 -- warnings from 'Valid looking like a possible update.
6489 Set_Comes_From_Source (Pref, False);
6491 Expand_Fpt_Attribute
6492 (N, Pkg, Name_Valid,
6493 New_List (
6494 Make_Attribute_Reference (Loc,
6495 Prefix => Unchecked_Convert_To (Ftp, Pref),
6496 Attribute_Name => Name_Unrestricted_Access)));
6497 end if;
6499 -- One more task, we still need a range check. Required
6500 -- only if we have a constraint, since the Valid routine
6501 -- catches infinities properly (infinities are never valid).
6503 -- The way we do the range check is simply to create the
6504 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
6506 if not Subtypes_Statically_Match (Ptyp, Btyp) then
6507 Rewrite (N,
6508 Make_And_Then (Loc,
6509 Left_Opnd => Relocate_Node (N),
6510 Right_Opnd =>
6511 Make_In (Loc,
6512 Left_Opnd => Convert_To (Btyp, Pref),
6513 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
6514 end if;
6515 end Float_Valid;
6517 -- Enumeration type with holes
6519 -- For enumeration types with holes, the Pos value constructed by
6520 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
6521 -- second argument of False returns minus one for an invalid value,
6522 -- and the non-negative pos value for a valid value, so the
6523 -- expansion of X'Valid is simply:
6525 -- type(X)'Pos (X) >= 0
6527 -- We can't quite generate it that way because of the requirement
6528 -- for the non-standard second argument of False in the resulting
6529 -- rep_to_pos call, so we have to explicitly create:
6531 -- _rep_to_pos (X, False) >= 0
6533 -- If we have an enumeration subtype, we also check that the
6534 -- value is in range:
6536 -- _rep_to_pos (X, False) >= 0
6537 -- and then
6538 -- (X >= type(X)'First and then type(X)'Last <= X)
6540 elsif Is_Enumeration_Type (Ptyp)
6541 and then Present (Enum_Pos_To_Rep (Btyp))
6542 then
6543 Tst :=
6544 Make_Op_Ge (Loc,
6545 Left_Opnd =>
6546 Make_Function_Call (Loc,
6547 Name =>
6548 New_Occurrence_Of (TSS (Btyp, TSS_Rep_To_Pos), Loc),
6549 Parameter_Associations => New_List (
6550 Pref,
6551 New_Occurrence_Of (Standard_False, Loc))),
6552 Right_Opnd => Make_Integer_Literal (Loc, 0));
6554 if Ptyp /= Btyp
6555 and then
6556 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
6557 or else
6558 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
6559 then
6560 -- The call to Make_Range_Test will create declarations
6561 -- that need a proper insertion point, but Pref is now
6562 -- attached to a node with no ancestor. Attach to tree
6563 -- even if it is to be rewritten below.
6565 Set_Parent (Tst, Parent (N));
6567 Tst :=
6568 Make_And_Then (Loc,
6569 Left_Opnd => Make_Range_Test,
6570 Right_Opnd => Tst);
6571 end if;
6573 Rewrite (N, Tst);
6575 -- Fortran convention booleans
6577 -- For the very special case of Fortran convention booleans, the
6578 -- value is always valid, since it is an integer with the semantics
6579 -- that non-zero is true, and any value is permissible.
6581 elsif Is_Boolean_Type (Ptyp)
6582 and then Convention (Ptyp) = Convention_Fortran
6583 then
6584 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6586 -- For biased representations, we will be doing an unchecked
6587 -- conversion without unbiasing the result. That means that the range
6588 -- test has to take this into account, and the proper form of the
6589 -- test is:
6591 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
6593 elsif Has_Biased_Representation (Ptyp) then
6594 Btyp := RTE (RE_Unsigned_32);
6595 Rewrite (N,
6596 Make_Op_Lt (Loc,
6597 Left_Opnd =>
6598 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
6599 Right_Opnd =>
6600 Unchecked_Convert_To (Btyp,
6601 Make_Attribute_Reference (Loc,
6602 Prefix => New_Occurrence_Of (Ptyp, Loc),
6603 Attribute_Name => Name_Range_Length))));
6605 -- For all other scalar types, what we want logically is a
6606 -- range test:
6608 -- X in type(X)'First .. type(X)'Last
6610 -- But that's precisely what won't work because of possible
6611 -- unwanted optimization (and indeed the basic motivation for
6612 -- the Valid attribute is exactly that this test does not work).
6613 -- What will work is:
6615 -- Btyp!(X) >= Btyp!(type(X)'First)
6616 -- and then
6617 -- Btyp!(X) <= Btyp!(type(X)'Last)
6619 -- where Btyp is an integer type large enough to cover the full
6620 -- range of possible stored values (i.e. it is chosen on the basis
6621 -- of the size of the type, not the range of the values). We write
6622 -- this as two tests, rather than a range check, so that static
6623 -- evaluation will easily remove either or both of the checks if
6624 -- they can be -statically determined to be true (this happens
6625 -- when the type of X is static and the range extends to the full
6626 -- range of stored values).
6628 -- Unsigned types. Note: it is safe to consider only whether the
6629 -- subtype is unsigned, since we will in that case be doing all
6630 -- unsigned comparisons based on the subtype range. Since we use the
6631 -- actual subtype object size, this is appropriate.
6633 -- For example, if we have
6635 -- subtype x is integer range 1 .. 200;
6636 -- for x'Object_Size use 8;
6638 -- Now the base type is signed, but objects of this type are bits
6639 -- unsigned, and doing an unsigned test of the range 1 to 200 is
6640 -- correct, even though a value greater than 127 looks signed to a
6641 -- signed comparison.
6643 elsif Is_Unsigned_Type (Ptyp) then
6644 if Esize (Ptyp) <= 32 then
6645 Btyp := RTE (RE_Unsigned_32);
6646 else
6647 Btyp := RTE (RE_Unsigned_64);
6648 end if;
6650 Rewrite (N, Make_Range_Test);
6652 -- Signed types
6654 else
6655 if Esize (Ptyp) <= Esize (Standard_Integer) then
6656 Btyp := Standard_Integer;
6657 else
6658 Btyp := Universal_Integer;
6659 end if;
6661 Rewrite (N, Make_Range_Test);
6662 end if;
6664 -- If a predicate is present, then we do the predicate test, even if
6665 -- within the predicate function (infinite recursion is warned about
6666 -- in Sem_Attr in that case).
6668 declare
6669 Pred_Func : constant Entity_Id := Predicate_Function (Ptyp);
6671 begin
6672 if Present (Pred_Func) then
6673 Rewrite (N,
6674 Make_And_Then (Loc,
6675 Left_Opnd => Relocate_Node (N),
6676 Right_Opnd => Make_Predicate_Call (Ptyp, Pref)));
6677 end if;
6678 end;
6680 Analyze_And_Resolve (N, Standard_Boolean);
6681 Validity_Checks_On := Save_Validity_Checks_On;
6682 end Valid;
6684 -------------------
6685 -- Valid_Scalars --
6686 -------------------
6688 when Attribute_Valid_Scalars => Valid_Scalars : declare
6689 Ftyp : Entity_Id;
6691 begin
6692 if Present (Underlying_Type (Ptyp)) then
6693 Ftyp := Underlying_Type (Ptyp);
6694 else
6695 Ftyp := Ptyp;
6696 end if;
6698 -- Replace by True if no scalar parts
6700 if not Scalar_Part_Present (Ftyp) then
6701 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6703 -- For scalar types, Valid_Scalars is the same as Valid
6705 elsif Is_Scalar_Type (Ftyp) then
6706 Rewrite (N,
6707 Make_Attribute_Reference (Loc,
6708 Attribute_Name => Name_Valid,
6709 Prefix => Pref));
6711 -- For array types, we construct a function that determines if there
6712 -- are any non-valid scalar subcomponents, and call the function.
6713 -- We only do this for arrays whose component type needs checking
6715 elsif Is_Array_Type (Ftyp)
6716 and then Scalar_Part_Present (Component_Type (Ftyp))
6717 then
6718 Rewrite (N,
6719 Make_Function_Call (Loc,
6720 Name =>
6721 New_Occurrence_Of (Build_Array_VS_Func (Ftyp, N), Loc),
6722 Parameter_Associations => New_List (Pref)));
6724 -- For record types, we construct a function that determines if there
6725 -- are any non-valid scalar subcomponents, and call the function.
6727 elsif Is_Record_Type (Ftyp)
6728 and then Nkind (Type_Definition (Declaration_Node (Ftyp))) =
6729 N_Record_Definition
6730 then
6731 Rewrite (N,
6732 Make_Function_Call (Loc,
6733 Name =>
6734 New_Occurrence_Of (Build_Record_VS_Func (Ftyp, N), Loc),
6735 Parameter_Associations => New_List (Pref)));
6737 -- Other record types or types with discriminants
6739 elsif Is_Record_Type (Ftyp) or else Has_Discriminants (Ptyp) then
6741 -- Build expression with list of equality tests
6743 declare
6744 C : Entity_Id;
6745 X : Node_Id;
6746 A : Name_Id;
6748 begin
6749 X := New_Occurrence_Of (Standard_True, Loc);
6750 C := First_Component_Or_Discriminant (Ptyp);
6751 while Present (C) loop
6752 if not Scalar_Part_Present (Etype (C)) then
6753 goto Continue;
6754 elsif Is_Scalar_Type (Etype (C)) then
6755 A := Name_Valid;
6756 else
6757 A := Name_Valid_Scalars;
6758 end if;
6760 X :=
6761 Make_And_Then (Loc,
6762 Left_Opnd => X,
6763 Right_Opnd =>
6764 Make_Attribute_Reference (Loc,
6765 Attribute_Name => A,
6766 Prefix =>
6767 Make_Selected_Component (Loc,
6768 Prefix =>
6769 Duplicate_Subexpr (Pref, Name_Req => True),
6770 Selector_Name =>
6771 New_Occurrence_Of (C, Loc))));
6772 <<Continue>>
6773 Next_Component_Or_Discriminant (C);
6774 end loop;
6776 Rewrite (N, X);
6777 end;
6779 -- For all other types, result is True
6781 else
6782 Rewrite (N, New_Occurrence_Of (Standard_Boolean, Loc));
6783 end if;
6785 -- Result is always boolean, but never static
6787 Analyze_And_Resolve (N, Standard_Boolean);
6788 Set_Is_Static_Expression (N, False);
6789 end Valid_Scalars;
6791 -----------
6792 -- Value --
6793 -----------
6795 -- Value attribute is handled in separate unit Exp_Imgv
6797 when Attribute_Value =>
6798 Exp_Imgv.Expand_Value_Attribute (N);
6800 -----------------
6801 -- Value_Size --
6802 -----------------
6804 -- The processing for Value_Size shares the processing for Size
6806 -------------
6807 -- Version --
6808 -------------
6810 -- The processing for Version shares the processing for Body_Version
6812 ----------------
6813 -- Wide_Image --
6814 ----------------
6816 -- Wide_Image attribute is handled in separate unit Exp_Imgv
6818 when Attribute_Wide_Image =>
6819 Exp_Imgv.Expand_Wide_Image_Attribute (N);
6821 ---------------------
6822 -- Wide_Wide_Image --
6823 ---------------------
6825 -- Wide_Wide_Image attribute is handled in separate unit Exp_Imgv
6827 when Attribute_Wide_Wide_Image =>
6828 Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N);
6830 ----------------
6831 -- Wide_Value --
6832 ----------------
6834 -- We expand typ'Wide_Value (X) into
6836 -- typ'Value
6837 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
6839 -- Wide_String_To_String is a runtime function that converts its wide
6840 -- string argument to String, converting any non-translatable characters
6841 -- into appropriate escape sequences. This preserves the required
6842 -- semantics of Wide_Value in all cases, and results in a very simple
6843 -- implementation approach.
6845 -- Note: for this approach to be fully standard compliant for the cases
6846 -- where typ is Wide_Character and Wide_Wide_Character, the encoding
6847 -- method must cover the entire character range (e.g. UTF-8). But that
6848 -- is a reasonable requirement when dealing with encoded character
6849 -- sequences. Presumably if one of the restrictive encoding mechanisms
6850 -- is in use such as Shift-JIS, then characters that cannot be
6851 -- represented using this encoding will not appear in any case.
6853 when Attribute_Wide_Value => Wide_Value :
6854 begin
6855 Rewrite (N,
6856 Make_Attribute_Reference (Loc,
6857 Prefix => Pref,
6858 Attribute_Name => Name_Value,
6860 Expressions => New_List (
6861 Make_Function_Call (Loc,
6862 Name =>
6863 New_Occurrence_Of (RTE (RE_Wide_String_To_String), Loc),
6865 Parameter_Associations => New_List (
6866 Relocate_Node (First (Exprs)),
6867 Make_Integer_Literal (Loc,
6868 Intval => Int (Wide_Character_Encoding_Method)))))));
6870 Analyze_And_Resolve (N, Typ);
6871 end Wide_Value;
6873 ---------------------
6874 -- Wide_Wide_Value --
6875 ---------------------
6877 -- We expand typ'Wide_Value_Value (X) into
6879 -- typ'Value
6880 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
6882 -- Wide_Wide_String_To_String is a runtime function that converts its
6883 -- wide string argument to String, converting any non-translatable
6884 -- characters into appropriate escape sequences. This preserves the
6885 -- required semantics of Wide_Wide_Value in all cases, and results in a
6886 -- very simple implementation approach.
6888 -- It's not quite right where typ = Wide_Wide_Character, because the
6889 -- encoding method may not cover the whole character type ???
6891 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
6892 begin
6893 Rewrite (N,
6894 Make_Attribute_Reference (Loc,
6895 Prefix => Pref,
6896 Attribute_Name => Name_Value,
6898 Expressions => New_List (
6899 Make_Function_Call (Loc,
6900 Name =>
6901 New_Occurrence_Of
6902 (RTE (RE_Wide_Wide_String_To_String), Loc),
6904 Parameter_Associations => New_List (
6905 Relocate_Node (First (Exprs)),
6906 Make_Integer_Literal (Loc,
6907 Intval => Int (Wide_Character_Encoding_Method)))))));
6909 Analyze_And_Resolve (N, Typ);
6910 end Wide_Wide_Value;
6912 ---------------------
6913 -- Wide_Wide_Width --
6914 ---------------------
6916 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
6918 when Attribute_Wide_Wide_Width =>
6919 Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide);
6921 ----------------
6922 -- Wide_Width --
6923 ----------------
6925 -- Wide_Width attribute is handled in separate unit Exp_Imgv
6927 when Attribute_Wide_Width =>
6928 Exp_Imgv.Expand_Width_Attribute (N, Wide);
6930 -----------
6931 -- Width --
6932 -----------
6934 -- Width attribute is handled in separate unit Exp_Imgv
6936 when Attribute_Width =>
6937 Exp_Imgv.Expand_Width_Attribute (N, Normal);
6939 -----------
6940 -- Write --
6941 -----------
6943 when Attribute_Write => Write : declare
6944 P_Type : constant Entity_Id := Entity (Pref);
6945 U_Type : constant Entity_Id := Underlying_Type (P_Type);
6946 Pname : Entity_Id;
6947 Decl : Node_Id;
6948 Prag : Node_Id;
6949 Arg3 : Node_Id;
6950 Wfunc : Node_Id;
6952 begin
6953 -- If no underlying type, we have an error that will be diagnosed
6954 -- elsewhere, so here we just completely ignore the expansion.
6956 if No (U_Type) then
6957 return;
6958 end if;
6960 -- Stream operations can appear in user code even if the restriction
6961 -- No_Streams is active (for example, when instantiating a predefined
6962 -- container). In that case rewrite the attribute as a Raise to
6963 -- prevent any run-time use.
6965 if Restriction_Active (No_Streams) then
6966 Rewrite (N,
6967 Make_Raise_Program_Error (Sloc (N),
6968 Reason => PE_Stream_Operation_Not_Allowed));
6969 Set_Etype (N, U_Type);
6970 return;
6971 end if;
6973 -- The simple case, if there is a TSS for Write, just call it
6975 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
6977 if Present (Pname) then
6978 null;
6980 else
6981 -- If there is a Stream_Convert pragma, use it, we rewrite
6983 -- sourcetyp'Output (stream, Item)
6985 -- as
6987 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
6989 -- where strmwrite is the given Write function that converts an
6990 -- argument of type sourcetyp or a type acctyp, from which it is
6991 -- derived to type strmtyp. The conversion to acttyp is required
6992 -- for the derived case.
6994 Prag := Get_Stream_Convert_Pragma (P_Type);
6996 if Present (Prag) then
6997 Arg3 :=
6998 Next (Next (First (Pragma_Argument_Associations (Prag))));
6999 Wfunc := Entity (Expression (Arg3));
7001 Rewrite (N,
7002 Make_Attribute_Reference (Loc,
7003 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
7004 Attribute_Name => Name_Output,
7005 Expressions => New_List (
7006 Relocate_Node (First (Exprs)),
7007 Make_Function_Call (Loc,
7008 Name => New_Occurrence_Of (Wfunc, Loc),
7009 Parameter_Associations => New_List (
7010 OK_Convert_To (Etype (First_Formal (Wfunc)),
7011 Relocate_Node (Next (First (Exprs)))))))));
7013 Analyze (N);
7014 return;
7016 -- For elementary types, we call the W_xxx routine directly
7018 elsif Is_Elementary_Type (U_Type) then
7019 Rewrite (N, Build_Elementary_Write_Call (N));
7020 Analyze (N);
7021 return;
7023 -- Array type case
7025 elsif Is_Array_Type (U_Type) then
7026 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
7027 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
7029 -- Tagged type case, use the primitive Write function. Note that
7030 -- this will dispatch in the class-wide case which is what we want
7032 elsif Is_Tagged_Type (U_Type) then
7033 Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
7035 -- All other record type cases, including protected records.
7036 -- The latter only arise for expander generated code for
7037 -- handling shared passive partition access.
7039 else
7040 pragma Assert
7041 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
7043 -- Ada 2005 (AI-216): Program_Error is raised when executing
7044 -- the default implementation of the Write attribute of an
7045 -- Unchecked_Union type. However, if the 'Write reference is
7046 -- within the generated Output stream procedure, Write outputs
7047 -- the components, and the default values of the discriminant
7048 -- are streamed by the Output procedure itself.
7050 if Is_Unchecked_Union (Base_Type (U_Type))
7051 and not Is_TSS (Current_Scope, TSS_Stream_Output)
7052 then
7053 Insert_Action (N,
7054 Make_Raise_Program_Error (Loc,
7055 Reason => PE_Unchecked_Union_Restriction));
7056 end if;
7058 if Has_Discriminants (U_Type)
7059 and then Present
7060 (Discriminant_Default_Value (First_Discriminant (U_Type)))
7061 then
7062 Build_Mutable_Record_Write_Procedure
7063 (Loc, Full_Base (U_Type), Decl, Pname);
7064 else
7065 Build_Record_Write_Procedure
7066 (Loc, Full_Base (U_Type), Decl, Pname);
7067 end if;
7069 Insert_Action (N, Decl);
7070 end if;
7071 end if;
7073 -- If we fall through, Pname is the procedure to be called
7075 Rewrite_Stream_Proc_Call (Pname);
7076 end Write;
7078 -- Component_Size is handled by the back end, unless the component size
7079 -- is known at compile time, which is always true in the packed array
7080 -- case. It is important that the packed array case is handled in the
7081 -- front end (see Eval_Attribute) since the back end would otherwise get
7082 -- confused by the equivalent packed array type.
7084 when Attribute_Component_Size =>
7085 null;
7087 -- The following attributes are handled by the back end (except that
7088 -- static cases have already been evaluated during semantic processing,
7089 -- but in any case the back end should not count on this).
7091 -- The back end also handles the non-class-wide cases of Size
7093 when Attribute_Bit_Order |
7094 Attribute_Code_Address |
7095 Attribute_Definite |
7096 Attribute_Deref |
7097 Attribute_Null_Parameter |
7098 Attribute_Passed_By_Reference |
7099 Attribute_Pool_Address |
7100 Attribute_Scalar_Storage_Order =>
7101 null;
7103 -- The following attributes are also handled by the back end, but return
7104 -- a universal integer result, so may need a conversion for checking
7105 -- that the result is in range.
7107 when Attribute_Aft |
7108 Attribute_Max_Alignment_For_Allocation =>
7109 Apply_Universal_Integer_Attribute_Checks (N);
7111 -- The following attributes should not appear at this stage, since they
7112 -- have already been handled by the analyzer (and properly rewritten
7113 -- with corresponding values or entities to represent the right values)
7115 when Attribute_Abort_Signal |
7116 Attribute_Address_Size |
7117 Attribute_Atomic_Always_Lock_Free |
7118 Attribute_Base |
7119 Attribute_Class |
7120 Attribute_Compiler_Version |
7121 Attribute_Default_Bit_Order |
7122 Attribute_Default_Scalar_Storage_Order |
7123 Attribute_Delta |
7124 Attribute_Denorm |
7125 Attribute_Digits |
7126 Attribute_Emax |
7127 Attribute_Enabled |
7128 Attribute_Epsilon |
7129 Attribute_Fast_Math |
7130 Attribute_First_Valid |
7131 Attribute_Has_Access_Values |
7132 Attribute_Has_Discriminants |
7133 Attribute_Has_Tagged_Values |
7134 Attribute_Large |
7135 Attribute_Last_Valid |
7136 Attribute_Library_Level |
7137 Attribute_Lock_Free |
7138 Attribute_Machine_Emax |
7139 Attribute_Machine_Emin |
7140 Attribute_Machine_Mantissa |
7141 Attribute_Machine_Overflows |
7142 Attribute_Machine_Radix |
7143 Attribute_Machine_Rounds |
7144 Attribute_Maximum_Alignment |
7145 Attribute_Model_Emin |
7146 Attribute_Model_Epsilon |
7147 Attribute_Model_Mantissa |
7148 Attribute_Model_Small |
7149 Attribute_Modulus |
7150 Attribute_Partition_ID |
7151 Attribute_Range |
7152 Attribute_Restriction_Set |
7153 Attribute_Safe_Emax |
7154 Attribute_Safe_First |
7155 Attribute_Safe_Large |
7156 Attribute_Safe_Last |
7157 Attribute_Safe_Small |
7158 Attribute_Scale |
7159 Attribute_Signed_Zeros |
7160 Attribute_Small |
7161 Attribute_Storage_Unit |
7162 Attribute_Stub_Type |
7163 Attribute_System_Allocator_Alignment |
7164 Attribute_Target_Name |
7165 Attribute_Type_Class |
7166 Attribute_Type_Key |
7167 Attribute_Unconstrained_Array |
7168 Attribute_Universal_Literal_String |
7169 Attribute_Wchar_T_Size |
7170 Attribute_Word_Size =>
7171 raise Program_Error;
7173 -- The Asm_Input and Asm_Output attributes are not expanded at this
7174 -- stage, but will be eliminated in the expansion of the Asm call, see
7175 -- Exp_Intr for details. So the back end will never see these either.
7177 when Attribute_Asm_Input |
7178 Attribute_Asm_Output =>
7179 null;
7180 end case;
7182 -- Note: as mentioned earlier, individual sections of the above case
7183 -- statement assume there is no code after the case statement, and are
7184 -- legitimately allowed to execute return statements if they have nothing
7185 -- more to do, so DO NOT add code at this point.
7187 exception
7188 when RE_Not_Available =>
7189 return;
7190 end Expand_N_Attribute_Reference;
7192 --------------------------------
7193 -- Expand_Pred_Succ_Attribute --
7194 --------------------------------
7196 -- For typ'Pred (exp), we generate the check
7198 -- [constraint_error when exp = typ'Base'First]
7200 -- Similarly, for typ'Succ (exp), we generate the check
7202 -- [constraint_error when exp = typ'Base'Last]
7204 -- These checks are not generated for modular types, since the proper
7205 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
7206 -- We also suppress these checks if we are the right side of an assignment
7207 -- statement or the expression of an object declaration, where the flag
7208 -- Suppress_Assignment_Checks is set for the assignment/declaration.
7210 procedure Expand_Pred_Succ_Attribute (N : Node_Id) is
7211 Loc : constant Source_Ptr := Sloc (N);
7212 P : constant Node_Id := Parent (N);
7213 Cnam : Name_Id;
7215 begin
7216 if Attribute_Name (N) = Name_Pred then
7217 Cnam := Name_First;
7218 else
7219 Cnam := Name_Last;
7220 end if;
7222 if not Nkind_In (P, N_Assignment_Statement, N_Object_Declaration)
7223 or else not Suppress_Assignment_Checks (P)
7224 then
7225 Insert_Action (N,
7226 Make_Raise_Constraint_Error (Loc,
7227 Condition =>
7228 Make_Op_Eq (Loc,
7229 Left_Opnd =>
7230 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
7231 Right_Opnd =>
7232 Make_Attribute_Reference (Loc,
7233 Prefix =>
7234 New_Occurrence_Of (Base_Type (Etype (Prefix (N))), Loc),
7235 Attribute_Name => Cnam)),
7236 Reason => CE_Overflow_Check_Failed));
7237 end if;
7238 end Expand_Pred_Succ_Attribute;
7240 -----------------------------
7241 -- Expand_Update_Attribute --
7242 -----------------------------
7244 procedure Expand_Update_Attribute (N : Node_Id) is
7245 procedure Process_Component_Or_Element_Update
7246 (Temp : Entity_Id;
7247 Comp : Node_Id;
7248 Expr : Node_Id;
7249 Typ : Entity_Id);
7250 -- Generate the statements necessary to update a single component or an
7251 -- element of the prefix. The code is inserted before the attribute N.
7252 -- Temp denotes the entity of the anonymous object created to reflect
7253 -- the changes in values. Comp is the component/index expression to be
7254 -- updated. Expr is an expression yielding the new value of Comp. Typ
7255 -- is the type of the prefix of attribute Update.
7257 procedure Process_Range_Update
7258 (Temp : Entity_Id;
7259 Comp : Node_Id;
7260 Expr : Node_Id;
7261 Typ : Entity_Id);
7262 -- Generate the statements necessary to update a slice of the prefix.
7263 -- The code is inserted before the attribute N. Temp denotes the entity
7264 -- of the anonymous object created to reflect the changes in values.
7265 -- Comp is range of the slice to be updated. Expr is an expression
7266 -- yielding the new value of Comp. Typ is the type of the prefix of
7267 -- attribute Update.
7269 -----------------------------------------
7270 -- Process_Component_Or_Element_Update --
7271 -----------------------------------------
7273 procedure Process_Component_Or_Element_Update
7274 (Temp : Entity_Id;
7275 Comp : Node_Id;
7276 Expr : Node_Id;
7277 Typ : Entity_Id)
7279 Loc : constant Source_Ptr := Sloc (Comp);
7280 Exprs : List_Id;
7281 LHS : Node_Id;
7283 begin
7284 -- An array element may be modified by the following relations
7285 -- depending on the number of dimensions:
7287 -- 1 => Expr -- one dimensional update
7288 -- (1, ..., N) => Expr -- multi dimensional update
7290 -- The above forms are converted in assignment statements where the
7291 -- left hand side is an indexed component:
7293 -- Temp (1) := Expr; -- one dimensional update
7294 -- Temp (1, ..., N) := Expr; -- multi dimensional update
7296 if Is_Array_Type (Typ) then
7298 -- The index expressions of a multi dimensional array update
7299 -- appear as an aggregate.
7301 if Nkind (Comp) = N_Aggregate then
7302 Exprs := New_Copy_List_Tree (Expressions (Comp));
7303 else
7304 Exprs := New_List (Relocate_Node (Comp));
7305 end if;
7307 LHS :=
7308 Make_Indexed_Component (Loc,
7309 Prefix => New_Occurrence_Of (Temp, Loc),
7310 Expressions => Exprs);
7312 -- A record component update appears in the following form:
7314 -- Comp => Expr
7316 -- The above relation is transformed into an assignment statement
7317 -- where the left hand side is a selected component:
7319 -- Temp.Comp := Expr;
7321 else pragma Assert (Is_Record_Type (Typ));
7322 LHS :=
7323 Make_Selected_Component (Loc,
7324 Prefix => New_Occurrence_Of (Temp, Loc),
7325 Selector_Name => Relocate_Node (Comp));
7326 end if;
7328 Insert_Action (N,
7329 Make_Assignment_Statement (Loc,
7330 Name => LHS,
7331 Expression => Relocate_Node (Expr)));
7332 end Process_Component_Or_Element_Update;
7334 --------------------------
7335 -- Process_Range_Update --
7336 --------------------------
7338 procedure Process_Range_Update
7339 (Temp : Entity_Id;
7340 Comp : Node_Id;
7341 Expr : Node_Id;
7342 Typ : Entity_Id)
7344 Index_Typ : constant Entity_Id := Etype (First_Index (Typ));
7345 Loc : constant Source_Ptr := Sloc (Comp);
7346 Index : Entity_Id;
7348 begin
7349 -- A range update appears as
7351 -- (Low .. High => Expr)
7353 -- The above construct is transformed into a loop that iterates over
7354 -- the given range and modifies the corresponding array values to the
7355 -- value of Expr:
7357 -- for Index in Low .. High loop
7358 -- Temp (<Index_Typ> (Index)) := Expr;
7359 -- end loop;
7361 Index := Make_Temporary (Loc, 'I');
7363 Insert_Action (N,
7364 Make_Loop_Statement (Loc,
7365 Iteration_Scheme =>
7366 Make_Iteration_Scheme (Loc,
7367 Loop_Parameter_Specification =>
7368 Make_Loop_Parameter_Specification (Loc,
7369 Defining_Identifier => Index,
7370 Discrete_Subtype_Definition => Relocate_Node (Comp))),
7372 Statements => New_List (
7373 Make_Assignment_Statement (Loc,
7374 Name =>
7375 Make_Indexed_Component (Loc,
7376 Prefix => New_Occurrence_Of (Temp, Loc),
7377 Expressions => New_List (
7378 Convert_To (Index_Typ,
7379 New_Occurrence_Of (Index, Loc)))),
7380 Expression => Relocate_Node (Expr))),
7382 End_Label => Empty));
7383 end Process_Range_Update;
7385 -- Local variables
7387 Aggr : constant Node_Id := First (Expressions (N));
7388 Loc : constant Source_Ptr := Sloc (N);
7389 Pref : constant Node_Id := Prefix (N);
7390 Typ : constant Entity_Id := Etype (Pref);
7391 Assoc : Node_Id;
7392 Comp : Node_Id;
7393 CW_Temp : Entity_Id;
7394 CW_Typ : Entity_Id;
7395 Expr : Node_Id;
7396 Temp : Entity_Id;
7398 -- Start of processing for Expand_Update_Attribute
7400 begin
7401 -- Create the anonymous object to store the value of the prefix and
7402 -- capture subsequent changes in value.
7404 Temp := Make_Temporary (Loc, 'T', Pref);
7406 -- Preserve the tag of the prefix by offering a specific view of the
7407 -- class-wide version of the prefix.
7409 if Is_Tagged_Type (Typ) then
7411 -- Generate:
7412 -- CW_Temp : Typ'Class := Typ'Class (Pref);
7414 CW_Temp := Make_Temporary (Loc, 'T');
7415 CW_Typ := Class_Wide_Type (Typ);
7417 Insert_Action (N,
7418 Make_Object_Declaration (Loc,
7419 Defining_Identifier => CW_Temp,
7420 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
7421 Expression =>
7422 Convert_To (CW_Typ, Relocate_Node (Pref))));
7424 -- Generate:
7425 -- Temp : Typ renames Typ (CW_Temp);
7427 Insert_Action (N,
7428 Make_Object_Renaming_Declaration (Loc,
7429 Defining_Identifier => Temp,
7430 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
7431 Name =>
7432 Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc))));
7434 -- Non-tagged case
7436 else
7437 -- Generate:
7438 -- Temp : Typ := Pref;
7440 Insert_Action (N,
7441 Make_Object_Declaration (Loc,
7442 Defining_Identifier => Temp,
7443 Object_Definition => New_Occurrence_Of (Typ, Loc),
7444 Expression => Relocate_Node (Pref)));
7445 end if;
7447 -- Process the update aggregate
7449 Assoc := First (Component_Associations (Aggr));
7450 while Present (Assoc) loop
7451 Comp := First (Choices (Assoc));
7452 Expr := Expression (Assoc);
7453 while Present (Comp) loop
7454 if Nkind (Comp) = N_Range then
7455 Process_Range_Update (Temp, Comp, Expr, Typ);
7456 else
7457 Process_Component_Or_Element_Update (Temp, Comp, Expr, Typ);
7458 end if;
7460 Next (Comp);
7461 end loop;
7463 Next (Assoc);
7464 end loop;
7466 -- The attribute is replaced by a reference to the anonymous object
7468 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7469 Analyze (N);
7470 end Expand_Update_Attribute;
7472 -------------------
7473 -- Find_Fat_Info --
7474 -------------------
7476 procedure Find_Fat_Info
7477 (T : Entity_Id;
7478 Fat_Type : out Entity_Id;
7479 Fat_Pkg : out RE_Id)
7481 Rtyp : constant Entity_Id := Root_Type (T);
7483 begin
7484 -- All we do is use the root type (historically this dealt with
7485 -- VAX-float .. to be cleaned up further later ???)
7487 Fat_Type := Rtyp;
7489 if Fat_Type = Standard_Short_Float then
7490 Fat_Pkg := RE_Attr_Short_Float;
7492 elsif Fat_Type = Standard_Float then
7493 Fat_Pkg := RE_Attr_Float;
7495 elsif Fat_Type = Standard_Long_Float then
7496 Fat_Pkg := RE_Attr_Long_Float;
7498 elsif Fat_Type = Standard_Long_Long_Float then
7499 Fat_Pkg := RE_Attr_Long_Long_Float;
7501 -- Universal real (which is its own root type) is treated as being
7502 -- equivalent to Standard.Long_Long_Float, since it is defined to
7503 -- have the same precision as the longest Float type.
7505 elsif Fat_Type = Universal_Real then
7506 Fat_Type := Standard_Long_Long_Float;
7507 Fat_Pkg := RE_Attr_Long_Long_Float;
7509 else
7510 raise Program_Error;
7511 end if;
7512 end Find_Fat_Info;
7514 ----------------------------
7515 -- Find_Stream_Subprogram --
7516 ----------------------------
7518 function Find_Stream_Subprogram
7519 (Typ : Entity_Id;
7520 Nam : TSS_Name_Type) return Entity_Id
7522 Base_Typ : constant Entity_Id := Base_Type (Typ);
7523 Ent : constant Entity_Id := TSS (Typ, Nam);
7525 function Is_Available (Entity : RE_Id) return Boolean;
7526 pragma Inline (Is_Available);
7527 -- Function to check whether the specified run-time call is available
7528 -- in the run time used. In the case of a configurable run time, it
7529 -- is normal that some subprograms are not there.
7531 -- I don't understand this routine at all, why is this not just a
7532 -- call to RTE_Available? And if for some reason we need a different
7533 -- routine with different semantics, why is not in Rtsfind ???
7535 ------------------
7536 -- Is_Available --
7537 ------------------
7539 function Is_Available (Entity : RE_Id) return Boolean is
7540 begin
7541 -- Assume that the unit will always be available when using a
7542 -- "normal" (not configurable) run time.
7544 return not Configurable_Run_Time_Mode or else RTE_Available (Entity);
7545 end Is_Available;
7547 -- Start of processing for Find_Stream_Subprogram
7549 begin
7550 if Present (Ent) then
7551 return Ent;
7552 end if;
7554 -- Stream attributes for strings are expanded into library calls. The
7555 -- following checks are disabled when the run-time is not available or
7556 -- when compiling predefined types due to bootstrap issues. As a result,
7557 -- the compiler will generate in-place stream routines for string types
7558 -- that appear in GNAT's library, but will generate calls via rtsfind
7559 -- to library routines for user code.
7561 -- Note: In the case of using a configurable run time, it is very likely
7562 -- that stream routines for string types are not present (they require
7563 -- file system support). In this case, the specific stream routines for
7564 -- strings are not used, relying on the regular stream mechanism
7565 -- instead. That is why we include the test Is_Available when dealing
7566 -- with these cases.
7568 if not Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit)) then
7569 -- Storage_Array as defined in package System.Storage_Elements
7571 if Is_RTE (Base_Typ, RE_Storage_Array) then
7573 -- Case of No_Stream_Optimizations restriction active
7575 if Restriction_Active (No_Stream_Optimizations) then
7576 if Nam = TSS_Stream_Input
7577 and then Is_Available (RE_Storage_Array_Input)
7578 then
7579 return RTE (RE_Storage_Array_Input);
7581 elsif Nam = TSS_Stream_Output
7582 and then Is_Available (RE_Storage_Array_Output)
7583 then
7584 return RTE (RE_Storage_Array_Output);
7586 elsif Nam = TSS_Stream_Read
7587 and then Is_Available (RE_Storage_Array_Read)
7588 then
7589 return RTE (RE_Storage_Array_Read);
7591 elsif Nam = TSS_Stream_Write
7592 and then Is_Available (RE_Storage_Array_Write)
7593 then
7594 return RTE (RE_Storage_Array_Write);
7596 elsif Nam /= TSS_Stream_Input and then
7597 Nam /= TSS_Stream_Output and then
7598 Nam /= TSS_Stream_Read and then
7599 Nam /= TSS_Stream_Write
7600 then
7601 raise Program_Error;
7602 end if;
7604 -- Restriction No_Stream_Optimizations is not set, so we can go
7605 -- ahead and optimize using the block IO forms of the routines.
7607 else
7608 if Nam = TSS_Stream_Input
7609 and then Is_Available (RE_Storage_Array_Input_Blk_IO)
7610 then
7611 return RTE (RE_Storage_Array_Input_Blk_IO);
7613 elsif Nam = TSS_Stream_Output
7614 and then Is_Available (RE_Storage_Array_Output_Blk_IO)
7615 then
7616 return RTE (RE_Storage_Array_Output_Blk_IO);
7618 elsif Nam = TSS_Stream_Read
7619 and then Is_Available (RE_Storage_Array_Read_Blk_IO)
7620 then
7621 return RTE (RE_Storage_Array_Read_Blk_IO);
7623 elsif Nam = TSS_Stream_Write
7624 and then Is_Available (RE_Storage_Array_Write_Blk_IO)
7625 then
7626 return RTE (RE_Storage_Array_Write_Blk_IO);
7628 elsif Nam /= TSS_Stream_Input and then
7629 Nam /= TSS_Stream_Output and then
7630 Nam /= TSS_Stream_Read and then
7631 Nam /= TSS_Stream_Write
7632 then
7633 raise Program_Error;
7634 end if;
7635 end if;
7637 -- Stream_Element_Array as defined in package Ada.Streams
7639 elsif Is_RTE (Base_Typ, RE_Stream_Element_Array) then
7641 -- Case of No_Stream_Optimizations restriction active
7643 if Restriction_Active (No_Stream_Optimizations) then
7644 if Nam = TSS_Stream_Input
7645 and then Is_Available (RE_Stream_Element_Array_Input)
7646 then
7647 return RTE (RE_Stream_Element_Array_Input);
7649 elsif Nam = TSS_Stream_Output
7650 and then Is_Available (RE_Stream_Element_Array_Output)
7651 then
7652 return RTE (RE_Stream_Element_Array_Output);
7654 elsif Nam = TSS_Stream_Read
7655 and then Is_Available (RE_Stream_Element_Array_Read)
7656 then
7657 return RTE (RE_Stream_Element_Array_Read);
7659 elsif Nam = TSS_Stream_Write
7660 and then Is_Available (RE_Stream_Element_Array_Write)
7661 then
7662 return RTE (RE_Stream_Element_Array_Write);
7664 elsif Nam /= TSS_Stream_Input and then
7665 Nam /= TSS_Stream_Output and then
7666 Nam /= TSS_Stream_Read and then
7667 Nam /= TSS_Stream_Write
7668 then
7669 raise Program_Error;
7670 end if;
7672 -- Restriction No_Stream_Optimizations is not set, so we can go
7673 -- ahead and optimize using the block IO forms of the routines.
7675 else
7676 if Nam = TSS_Stream_Input
7677 and then Is_Available (RE_Stream_Element_Array_Input_Blk_IO)
7678 then
7679 return RTE (RE_Stream_Element_Array_Input_Blk_IO);
7681 elsif Nam = TSS_Stream_Output
7682 and then Is_Available (RE_Stream_Element_Array_Output_Blk_IO)
7683 then
7684 return RTE (RE_Stream_Element_Array_Output_Blk_IO);
7686 elsif Nam = TSS_Stream_Read
7687 and then Is_Available (RE_Stream_Element_Array_Read_Blk_IO)
7688 then
7689 return RTE (RE_Stream_Element_Array_Read_Blk_IO);
7691 elsif Nam = TSS_Stream_Write
7692 and then Is_Available (RE_Stream_Element_Array_Write_Blk_IO)
7693 then
7694 return RTE (RE_Stream_Element_Array_Write_Blk_IO);
7696 elsif Nam /= TSS_Stream_Input and then
7697 Nam /= TSS_Stream_Output and then
7698 Nam /= TSS_Stream_Read and then
7699 Nam /= TSS_Stream_Write
7700 then
7701 raise Program_Error;
7702 end if;
7703 end if;
7705 -- String as defined in package Ada
7707 elsif Base_Typ = Standard_String then
7709 -- Case of No_Stream_Optimizations restriction active
7711 if Restriction_Active (No_Stream_Optimizations) then
7712 if Nam = TSS_Stream_Input
7713 and then Is_Available (RE_String_Input)
7714 then
7715 return RTE (RE_String_Input);
7717 elsif Nam = TSS_Stream_Output
7718 and then Is_Available (RE_String_Output)
7719 then
7720 return RTE (RE_String_Output);
7722 elsif Nam = TSS_Stream_Read
7723 and then Is_Available (RE_String_Read)
7724 then
7725 return RTE (RE_String_Read);
7727 elsif Nam = TSS_Stream_Write
7728 and then Is_Available (RE_String_Write)
7729 then
7730 return RTE (RE_String_Write);
7732 elsif Nam /= TSS_Stream_Input and then
7733 Nam /= TSS_Stream_Output and then
7734 Nam /= TSS_Stream_Read and then
7735 Nam /= TSS_Stream_Write
7736 then
7737 raise Program_Error;
7738 end if;
7740 -- Restriction No_Stream_Optimizations is not set, so we can go
7741 -- ahead and optimize using the block IO forms of the routines.
7743 else
7744 if Nam = TSS_Stream_Input
7745 and then Is_Available (RE_String_Input_Blk_IO)
7746 then
7747 return RTE (RE_String_Input_Blk_IO);
7749 elsif Nam = TSS_Stream_Output
7750 and then Is_Available (RE_String_Output_Blk_IO)
7751 then
7752 return RTE (RE_String_Output_Blk_IO);
7754 elsif Nam = TSS_Stream_Read
7755 and then Is_Available (RE_String_Read_Blk_IO)
7756 then
7757 return RTE (RE_String_Read_Blk_IO);
7759 elsif Nam = TSS_Stream_Write
7760 and then Is_Available (RE_String_Write_Blk_IO)
7761 then
7762 return RTE (RE_String_Write_Blk_IO);
7764 elsif Nam /= TSS_Stream_Input and then
7765 Nam /= TSS_Stream_Output and then
7766 Nam /= TSS_Stream_Read and then
7767 Nam /= TSS_Stream_Write
7768 then
7769 raise Program_Error;
7770 end if;
7771 end if;
7773 -- Wide_String as defined in package Ada
7775 elsif Base_Typ = Standard_Wide_String then
7777 -- Case of No_Stream_Optimizations restriction active
7779 if Restriction_Active (No_Stream_Optimizations) then
7780 if Nam = TSS_Stream_Input
7781 and then Is_Available (RE_Wide_String_Input)
7782 then
7783 return RTE (RE_Wide_String_Input);
7785 elsif Nam = TSS_Stream_Output
7786 and then Is_Available (RE_Wide_String_Output)
7787 then
7788 return RTE (RE_Wide_String_Output);
7790 elsif Nam = TSS_Stream_Read
7791 and then Is_Available (RE_Wide_String_Read)
7792 then
7793 return RTE (RE_Wide_String_Read);
7795 elsif Nam = TSS_Stream_Write
7796 and then Is_Available (RE_Wide_String_Write)
7797 then
7798 return RTE (RE_Wide_String_Write);
7800 elsif Nam /= TSS_Stream_Input and then
7801 Nam /= TSS_Stream_Output and then
7802 Nam /= TSS_Stream_Read and then
7803 Nam /= TSS_Stream_Write
7804 then
7805 raise Program_Error;
7806 end if;
7808 -- Restriction No_Stream_Optimizations is not set, so we can go
7809 -- ahead and optimize using the block IO forms of the routines.
7811 else
7812 if Nam = TSS_Stream_Input
7813 and then Is_Available (RE_Wide_String_Input_Blk_IO)
7814 then
7815 return RTE (RE_Wide_String_Input_Blk_IO);
7817 elsif Nam = TSS_Stream_Output
7818 and then Is_Available (RE_Wide_String_Output_Blk_IO)
7819 then
7820 return RTE (RE_Wide_String_Output_Blk_IO);
7822 elsif Nam = TSS_Stream_Read
7823 and then Is_Available (RE_Wide_String_Read_Blk_IO)
7824 then
7825 return RTE (RE_Wide_String_Read_Blk_IO);
7827 elsif Nam = TSS_Stream_Write
7828 and then Is_Available (RE_Wide_String_Write_Blk_IO)
7829 then
7830 return RTE (RE_Wide_String_Write_Blk_IO);
7832 elsif Nam /= TSS_Stream_Input and then
7833 Nam /= TSS_Stream_Output and then
7834 Nam /= TSS_Stream_Read and then
7835 Nam /= TSS_Stream_Write
7836 then
7837 raise Program_Error;
7838 end if;
7839 end if;
7841 -- Wide_Wide_String as defined in package Ada
7843 elsif Base_Typ = Standard_Wide_Wide_String then
7845 -- Case of No_Stream_Optimizations restriction active
7847 if Restriction_Active (No_Stream_Optimizations) then
7848 if Nam = TSS_Stream_Input
7849 and then Is_Available (RE_Wide_Wide_String_Input)
7850 then
7851 return RTE (RE_Wide_Wide_String_Input);
7853 elsif Nam = TSS_Stream_Output
7854 and then Is_Available (RE_Wide_Wide_String_Output)
7855 then
7856 return RTE (RE_Wide_Wide_String_Output);
7858 elsif Nam = TSS_Stream_Read
7859 and then Is_Available (RE_Wide_Wide_String_Read)
7860 then
7861 return RTE (RE_Wide_Wide_String_Read);
7863 elsif Nam = TSS_Stream_Write
7864 and then Is_Available (RE_Wide_Wide_String_Write)
7865 then
7866 return RTE (RE_Wide_Wide_String_Write);
7868 elsif Nam /= TSS_Stream_Input and then
7869 Nam /= TSS_Stream_Output and then
7870 Nam /= TSS_Stream_Read and then
7871 Nam /= TSS_Stream_Write
7872 then
7873 raise Program_Error;
7874 end if;
7876 -- Restriction No_Stream_Optimizations is not set, so we can go
7877 -- ahead and optimize using the block IO forms of the routines.
7879 else
7880 if Nam = TSS_Stream_Input
7881 and then Is_Available (RE_Wide_Wide_String_Input_Blk_IO)
7882 then
7883 return RTE (RE_Wide_Wide_String_Input_Blk_IO);
7885 elsif Nam = TSS_Stream_Output
7886 and then Is_Available (RE_Wide_Wide_String_Output_Blk_IO)
7887 then
7888 return RTE (RE_Wide_Wide_String_Output_Blk_IO);
7890 elsif Nam = TSS_Stream_Read
7891 and then Is_Available (RE_Wide_Wide_String_Read_Blk_IO)
7892 then
7893 return RTE (RE_Wide_Wide_String_Read_Blk_IO);
7895 elsif Nam = TSS_Stream_Write
7896 and then Is_Available (RE_Wide_Wide_String_Write_Blk_IO)
7897 then
7898 return RTE (RE_Wide_Wide_String_Write_Blk_IO);
7900 elsif Nam /= TSS_Stream_Input and then
7901 Nam /= TSS_Stream_Output and then
7902 Nam /= TSS_Stream_Read and then
7903 Nam /= TSS_Stream_Write
7904 then
7905 raise Program_Error;
7906 end if;
7907 end if;
7908 end if;
7909 end if;
7911 if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then
7912 return Find_Prim_Op (Typ, Nam);
7913 else
7914 return Find_Inherited_TSS (Typ, Nam);
7915 end if;
7916 end Find_Stream_Subprogram;
7918 ---------------
7919 -- Full_Base --
7920 ---------------
7922 function Full_Base (T : Entity_Id) return Entity_Id is
7923 BT : Entity_Id;
7925 begin
7926 BT := Base_Type (T);
7928 if Is_Private_Type (BT)
7929 and then Present (Full_View (BT))
7930 then
7931 BT := Full_View (BT);
7932 end if;
7934 return BT;
7935 end Full_Base;
7937 -----------------------
7938 -- Get_Index_Subtype --
7939 -----------------------
7941 function Get_Index_Subtype (N : Node_Id) return Node_Id is
7942 P_Type : Entity_Id := Etype (Prefix (N));
7943 Indx : Node_Id;
7944 J : Int;
7946 begin
7947 if Is_Access_Type (P_Type) then
7948 P_Type := Designated_Type (P_Type);
7949 end if;
7951 if No (Expressions (N)) then
7952 J := 1;
7953 else
7954 J := UI_To_Int (Expr_Value (First (Expressions (N))));
7955 end if;
7957 Indx := First_Index (P_Type);
7958 while J > 1 loop
7959 Next_Index (Indx);
7960 J := J - 1;
7961 end loop;
7963 return Etype (Indx);
7964 end Get_Index_Subtype;
7966 -------------------------------
7967 -- Get_Stream_Convert_Pragma --
7968 -------------------------------
7970 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is
7971 Typ : Entity_Id;
7972 N : Node_Id;
7974 begin
7975 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
7976 -- that a stream convert pragma for a tagged type is not inherited from
7977 -- its parent. Probably what is wrong here is that it is basically
7978 -- incorrect to consider a stream convert pragma to be a representation
7979 -- pragma at all ???
7981 N := First_Rep_Item (Implementation_Base_Type (T));
7982 while Present (N) loop
7983 if Nkind (N) = N_Pragma
7984 and then Pragma_Name (N) = Name_Stream_Convert
7985 then
7986 -- For tagged types this pragma is not inherited, so we
7987 -- must verify that it is defined for the given type and
7988 -- not an ancestor.
7990 Typ :=
7991 Entity (Expression (First (Pragma_Argument_Associations (N))));
7993 if not Is_Tagged_Type (T)
7994 or else T = Typ
7995 or else (Is_Private_Type (Typ) and then T = Full_View (Typ))
7996 then
7997 return N;
7998 end if;
7999 end if;
8001 Next_Rep_Item (N);
8002 end loop;
8004 return Empty;
8005 end Get_Stream_Convert_Pragma;
8007 ---------------------------------
8008 -- Is_Constrained_Packed_Array --
8009 ---------------------------------
8011 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
8012 Arr : Entity_Id := Typ;
8014 begin
8015 if Is_Access_Type (Arr) then
8016 Arr := Designated_Type (Arr);
8017 end if;
8019 return Is_Array_Type (Arr)
8020 and then Is_Constrained (Arr)
8021 and then Present (Packed_Array_Impl_Type (Arr));
8022 end Is_Constrained_Packed_Array;
8024 ----------------------------------------
8025 -- Is_Inline_Floating_Point_Attribute --
8026 ----------------------------------------
8028 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is
8029 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
8031 function Is_GCC_Target return Boolean;
8032 -- Return True if we are using a GCC target/back-end
8033 -- ??? Note: the implementation is kludgy/fragile
8035 -------------------
8036 -- Is_GCC_Target --
8037 -------------------
8039 function Is_GCC_Target return Boolean is
8040 begin
8041 return not CodePeer_Mode
8042 and then not AAMP_On_Target
8043 and then not Generate_C_Code;
8044 end Is_GCC_Target;
8046 -- Start of processing for Is_Inline_Floating_Point_Attribute
8048 begin
8049 -- Machine and Model can be expanded by the GCC and AAMP back ends only
8051 if Id = Attribute_Machine or else Id = Attribute_Model then
8052 return Is_GCC_Target or else AAMP_On_Target;
8054 -- Remaining cases handled by all back ends are Rounding and Truncation
8055 -- when appearing as the operand of a conversion to some integer type.
8057 elsif Nkind (Parent (N)) /= N_Type_Conversion
8058 or else not Is_Integer_Type (Etype (Parent (N)))
8059 then
8060 return False;
8061 end if;
8063 -- Here we are in the integer conversion context
8065 -- Very probably we should also recognize the cases of Machine_Rounding
8066 -- and unbiased rounding in this conversion context, but the back end is
8067 -- not yet prepared to handle these cases ???
8069 return Id = Attribute_Rounding or else Id = Attribute_Truncation;
8070 end Is_Inline_Floating_Point_Attribute;
8072 end Exp_Attr;