* libgnarl/a-intnam__rtems.ads: Update copyright date.
[official-gcc.git] / gcc / ada / exp_attr.adb
blob9afb23be02eb31c7789b941b0a8ac0ddc7181189
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-2017, 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 Freeze; use Freeze;
43 with Gnatvsn; use Gnatvsn;
44 with Itypes; use Itypes;
45 with Lib; use Lib;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
48 with Nlists; use Nlists;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch7; use Sem_Ch7;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sinfo; use Sinfo;
62 with Snames; use Snames;
63 with Stand; use Stand;
64 with Stringt; use Stringt;
65 with Tbuild; use Tbuild;
66 with Ttypes; use Ttypes;
67 with Uintp; use Uintp;
68 with Uname; use Uname;
69 with Validsw; use Validsw;
71 package body Exp_Attr is
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 function Build_Array_VS_Func
78 (A_Type : Entity_Id;
79 Nod : Node_Id) return Entity_Id;
80 -- Build function to test Valid_Scalars for array type A_Type. Nod is the
81 -- Valid_Scalars attribute node, used to insert the function body, and the
82 -- value returned is the entity of the constructed function body. We do not
83 -- bother to generate a separate spec for this subprogram.
85 function Build_Disp_Get_Task_Id_Call (Actual : Node_Id) return Node_Id;
86 -- Build a call to Disp_Get_Task_Id, passing Actual as actual parameter
88 function Build_Record_VS_Func
89 (R_Type : Entity_Id;
90 Nod : Node_Id) return Entity_Id;
91 -- Build function to test Valid_Scalars for record type A_Type. Nod is the
92 -- Valid_Scalars attribute node, used to insert the function body, and the
93 -- value returned is the entity of the constructed function body. We do not
94 -- bother to generate a separate spec for this subprogram.
96 procedure Compile_Stream_Body_In_Scope
97 (N : Node_Id;
98 Decl : Node_Id;
99 Arr : Entity_Id;
100 Check : Boolean);
101 -- The body for a stream subprogram may be generated outside of the scope
102 -- of the type. If the type is fully private, it may depend on the full
103 -- view of other types (e.g. indexes) that are currently private as well.
104 -- We install the declarations of the package in which the type is declared
105 -- before compiling the body in what is its proper environment. The Check
106 -- parameter indicates if checks are to be suppressed for the stream body.
107 -- We suppress checks for array/record reads, since the rule is that these
108 -- are like assignments, out of range values due to uninitialized storage,
109 -- or other invalid values do NOT cause a Constraint_Error to be raised.
110 -- If we are within an instance body all visibility has been established
111 -- already and there is no need to install the package.
113 -- This mechanism is now extended to the component types of the array type,
114 -- when the component type is not in scope and is private, to handle
115 -- properly the case when the full view has defaulted discriminants.
117 -- This special processing is ultimately caused by the fact that the
118 -- compiler lacks a well-defined phase when full views are visible
119 -- everywhere. Having such a separate pass would remove much of the
120 -- special-case code that shuffles partial and full views in the middle
121 -- of semantic analysis and expansion.
123 procedure Expand_Access_To_Protected_Op
124 (N : Node_Id;
125 Pref : Node_Id;
126 Typ : Entity_Id);
127 -- An attribute reference to a protected subprogram is transformed into
128 -- a pair of pointers: one to the object, and one to the operations.
129 -- This expansion is performed for 'Access and for 'Unrestricted_Access.
131 procedure Expand_Fpt_Attribute
132 (N : Node_Id;
133 Pkg : RE_Id;
134 Nam : Name_Id;
135 Args : List_Id);
136 -- This procedure expands a call to a floating-point attribute function.
137 -- N is the attribute reference node, and Args is a list of arguments to
138 -- be passed to the function call. Pkg identifies the package containing
139 -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args
140 -- have already been converted to the floating-point type for which Pkg was
141 -- instantiated. The Nam argument is the relevant attribute processing
142 -- routine to be called. This is the same as the attribute name, except in
143 -- the Unaligned_Valid case.
145 procedure Expand_Fpt_Attribute_R (N : Node_Id);
146 -- This procedure expands a call to a floating-point attribute function
147 -- that takes a single floating-point argument. The function to be called
148 -- is always the same as the attribute name.
150 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
151 -- This procedure expands a call to a floating-point attribute function
152 -- that takes one floating-point argument and one integer argument. The
153 -- function to be called is always the same as the attribute name.
155 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
156 -- This procedure expands a call to a floating-point attribute function
157 -- that takes two floating-point arguments. The function to be called
158 -- is always the same as the attribute name.
160 procedure Expand_Loop_Entry_Attribute (N : Node_Id);
161 -- Handle the expansion of attribute 'Loop_Entry. As a result, the related
162 -- loop may be converted into a conditional block. See body for details.
164 procedure Expand_Min_Max_Attribute (N : Node_Id);
165 -- Handle the expansion of attributes 'Max and 'Min, including expanding
166 -- then out if we are in Modify_Tree_For_C mode.
168 procedure Expand_Pred_Succ_Attribute (N : Node_Id);
169 -- Handles expansion of Pred or Succ attributes for case of non-real
170 -- operand with overflow checking required.
172 procedure Expand_Update_Attribute (N : Node_Id);
173 -- Handle the expansion of attribute Update
175 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
176 -- Used for Last, Last, and Length, when the prefix is an array type.
177 -- Obtains the corresponding index subtype.
179 procedure Find_Fat_Info
180 (T : Entity_Id;
181 Fat_Type : out Entity_Id;
182 Fat_Pkg : out RE_Id);
183 -- Given a floating-point type T, identifies the package containing the
184 -- attributes for this type (returned in Fat_Pkg), and the corresponding
185 -- type for which this package was instantiated from Fat_Gen. Error if T
186 -- is not a floating-point type.
188 function Find_Stream_Subprogram
189 (Typ : Entity_Id;
190 Nam : TSS_Name_Type) return Entity_Id;
191 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
192 -- types, the corresponding primitive operation is looked up, else the
193 -- appropriate TSS from the type itself, or from its closest ancestor
194 -- defining it, is returned. In both cases, inheritance of representation
195 -- aspects is thus taken into account.
197 function Full_Base (T : Entity_Id) return Entity_Id;
198 -- The stream functions need to examine the underlying representation of
199 -- composite types. In some cases T may be non-private but its base type
200 -- is, in which case the function returns the corresponding full view.
202 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id;
203 -- Given a type, find a corresponding stream convert pragma that applies to
204 -- the implementation base type of this type (Typ). If found, return the
205 -- pragma node, otherwise return Empty if no pragma is found.
207 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
208 -- Utility for array attributes, returns true on packed constrained
209 -- arrays, and on access to same.
211 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean;
212 -- Returns true iff the given node refers to an attribute call that
213 -- can be expanded directly by the back end and does not need front end
214 -- expansion. Typically used for rounding and truncation attributes that
215 -- appear directly inside a conversion to integer.
217 -------------------------
218 -- Build_Array_VS_Func --
219 -------------------------
221 function Build_Array_VS_Func
222 (A_Type : Entity_Id;
223 Nod : Node_Id) return Entity_Id
225 Loc : constant Source_Ptr := Sloc (Nod);
226 Func_Id : constant Entity_Id := Make_Temporary (Loc, 'V');
227 Comp_Type : constant Entity_Id := Component_Type (A_Type);
228 Body_Stmts : List_Id;
229 Index_List : List_Id;
230 Formals : List_Id;
232 function Test_Component return List_Id;
233 -- Create one statement to test validity of one component designated by
234 -- a full set of indexes. Returns statement list containing test.
236 function Test_One_Dimension (N : Int) return List_Id;
237 -- Create loop to test one dimension of the array. The single statement
238 -- in the loop body tests the inner dimensions if any, or else the
239 -- single component. Note that this procedure is called recursively,
240 -- with N being the dimension to be initialized. A call with N greater
241 -- than the number of dimensions simply generates the component test,
242 -- terminating the recursion. Returns statement list containing tests.
244 --------------------
245 -- Test_Component --
246 --------------------
248 function Test_Component return List_Id is
249 Comp : Node_Id;
250 Anam : Name_Id;
252 begin
253 Comp :=
254 Make_Indexed_Component (Loc,
255 Prefix => Make_Identifier (Loc, Name_uA),
256 Expressions => Index_List);
258 if Is_Scalar_Type (Comp_Type) then
259 Anam := Name_Valid;
260 else
261 Anam := Name_Valid_Scalars;
262 end if;
264 return New_List (
265 Make_If_Statement (Loc,
266 Condition =>
267 Make_Op_Not (Loc,
268 Right_Opnd =>
269 Make_Attribute_Reference (Loc,
270 Attribute_Name => Anam,
271 Prefix => Comp)),
272 Then_Statements => New_List (
273 Make_Simple_Return_Statement (Loc,
274 Expression => New_Occurrence_Of (Standard_False, Loc)))));
275 end Test_Component;
277 ------------------------
278 -- Test_One_Dimension --
279 ------------------------
281 function Test_One_Dimension (N : Int) return List_Id is
282 Index : Entity_Id;
284 begin
285 -- If all dimensions dealt with, we simply test the component
287 if N > Number_Dimensions (A_Type) then
288 return Test_Component;
290 -- Here we generate the required loop
292 else
293 Index :=
294 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
296 Append (New_Occurrence_Of (Index, Loc), Index_List);
298 return New_List (
299 Make_Implicit_Loop_Statement (Nod,
300 Identifier => Empty,
301 Iteration_Scheme =>
302 Make_Iteration_Scheme (Loc,
303 Loop_Parameter_Specification =>
304 Make_Loop_Parameter_Specification (Loc,
305 Defining_Identifier => Index,
306 Discrete_Subtype_Definition =>
307 Make_Attribute_Reference (Loc,
308 Prefix => Make_Identifier (Loc, Name_uA),
309 Attribute_Name => Name_Range,
310 Expressions => New_List (
311 Make_Integer_Literal (Loc, N))))),
312 Statements => Test_One_Dimension (N + 1)),
313 Make_Simple_Return_Statement (Loc,
314 Expression => New_Occurrence_Of (Standard_True, Loc)));
315 end if;
316 end Test_One_Dimension;
318 -- Start of processing for Build_Array_VS_Func
320 begin
321 Index_List := New_List;
322 Body_Stmts := Test_One_Dimension (1);
324 -- Parameter is always (A : A_Typ)
326 Formals := New_List (
327 Make_Parameter_Specification (Loc,
328 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uA),
329 In_Present => True,
330 Out_Present => False,
331 Parameter_Type => New_Occurrence_Of (A_Type, Loc)));
333 -- Build body
335 Set_Ekind (Func_Id, E_Function);
336 Set_Is_Internal (Func_Id);
338 Insert_Action (Nod,
339 Make_Subprogram_Body (Loc,
340 Specification =>
341 Make_Function_Specification (Loc,
342 Defining_Unit_Name => Func_Id,
343 Parameter_Specifications => Formals,
344 Result_Definition =>
345 New_Occurrence_Of (Standard_Boolean, Loc)),
346 Declarations => New_List,
347 Handled_Statement_Sequence =>
348 Make_Handled_Sequence_Of_Statements (Loc,
349 Statements => Body_Stmts)));
351 if not Debug_Generated_Code then
352 Set_Debug_Info_Off (Func_Id);
353 end if;
355 Set_Is_Pure (Func_Id);
356 return Func_Id;
357 end Build_Array_VS_Func;
359 ---------------------------------
360 -- Build_Disp_Get_Task_Id_Call --
361 ---------------------------------
363 function Build_Disp_Get_Task_Id_Call (Actual : Node_Id) return Node_Id is
364 Loc : constant Source_Ptr := Sloc (Actual);
365 Typ : constant Entity_Id := Etype (Actual);
366 Subp : constant Entity_Id := Find_Prim_Op (Typ, Name_uDisp_Get_Task_Id);
368 begin
369 -- Generate:
370 -- _Disp_Get_Task_Id (Actual)
372 return
373 Make_Function_Call (Loc,
374 Name => New_Occurrence_Of (Subp, Loc),
375 Parameter_Associations => New_List (Actual));
376 end Build_Disp_Get_Task_Id_Call;
378 --------------------------
379 -- Build_Record_VS_Func --
380 --------------------------
382 -- Generates:
384 -- function _Valid_Scalars (X : T) return Boolean is
385 -- begin
386 -- -- Check discriminants
388 -- if not X.D1'Valid_Scalars or else
389 -- not X.D2'Valid_Scalars or else
390 -- ...
391 -- then
392 -- return False;
393 -- end if;
395 -- -- Check components
397 -- if not X.C1'Valid_Scalars or else
398 -- not X.C2'Valid_Scalars or else
399 -- ...
400 -- then
401 -- return False;
402 -- end if;
404 -- -- Check variant part
406 -- case X.D1 is
407 -- when V1 =>
408 -- if not X.C2'Valid_Scalars or else
409 -- not X.C3'Valid_Scalars or else
410 -- ...
411 -- then
412 -- return False;
413 -- end if;
414 -- ...
415 -- when Vn =>
416 -- if not X.Cn'Valid_Scalars or else
417 -- ...
418 -- then
419 -- return False;
420 -- end if;
421 -- end case;
423 -- return True;
424 -- end _Valid_Scalars;
426 -- If the record type is an unchecked union, we can only check components
427 -- in the invariant part, given that there are no discriminant values to
428 -- select a variant.
430 function Build_Record_VS_Func
431 (R_Type : Entity_Id;
432 Nod : Node_Id) return Entity_Id
434 Loc : constant Source_Ptr := Sloc (R_Type);
435 Func_Id : constant Entity_Id := Make_Temporary (Loc, 'V');
436 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_X);
438 function Make_VS_Case
439 (E : Entity_Id;
440 CL : Node_Id;
441 Discrs : Elist_Id := New_Elmt_List) return List_Id;
442 -- Building block for variant valid scalars. Given a Component_List node
443 -- CL, it generates an 'if' followed by a 'case' statement that compares
444 -- all components of local temporaries named X and Y (that are declared
445 -- as formals at some upper level). E provides the Sloc to be used for
446 -- the generated code.
448 function Make_VS_If
449 (E : Entity_Id;
450 L : List_Id) return Node_Id;
451 -- Building block for variant validate scalars. Given the list, L, of
452 -- components (or discriminants) L, it generates a return statement that
453 -- compares all components of local temporaries named X and Y (that are
454 -- declared as formals at some upper level). E provides the Sloc to be
455 -- used for the generated code.
457 ------------------
458 -- Make_VS_Case --
459 ------------------
461 -- <Make_VS_If on shared components>
463 -- case X.D1 is
464 -- when V1 => <Make_VS_Case> on subcomponents
465 -- ...
466 -- when Vn => <Make_VS_Case> on subcomponents
467 -- end case;
469 function Make_VS_Case
470 (E : Entity_Id;
471 CL : Node_Id;
472 Discrs : Elist_Id := New_Elmt_List) return List_Id
474 Loc : constant Source_Ptr := Sloc (E);
475 Result : constant List_Id := New_List;
476 Variant : Node_Id;
477 Alt_List : List_Id;
479 begin
480 Append_To (Result, Make_VS_If (E, Component_Items (CL)));
482 if No (Variant_Part (CL))
483 or else Is_Unchecked_Union (R_Type)
484 then
485 return Result;
486 end if;
488 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
490 if No (Variant) then
491 return Result;
492 end if;
494 Alt_List := New_List;
495 while Present (Variant) loop
496 Append_To (Alt_List,
497 Make_Case_Statement_Alternative (Loc,
498 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
499 Statements =>
500 Make_VS_Case (E, Component_List (Variant), Discrs)));
501 Next_Non_Pragma (Variant);
502 end loop;
504 Append_To (Result,
505 Make_Case_Statement (Loc,
506 Expression =>
507 Make_Selected_Component (Loc,
508 Prefix => Make_Identifier (Loc, Name_X),
509 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
510 Alternatives => Alt_List));
512 return Result;
513 end Make_VS_Case;
515 ----------------
516 -- Make_VS_If --
517 ----------------
519 -- Generates:
521 -- if
522 -- not X.C1'Valid_Scalars
523 -- or else
524 -- not X.C2'Valid_Scalars
525 -- ...
526 -- then
527 -- return False;
528 -- end if;
530 -- or a null statement if the list L is empty
532 function Make_VS_If
533 (E : Entity_Id;
534 L : List_Id) return Node_Id
536 Loc : constant Source_Ptr := Sloc (E);
537 C : Node_Id;
538 Def_Id : Entity_Id;
539 Field_Name : Name_Id;
540 Cond : Node_Id;
542 begin
543 if No (L) then
544 return Make_Null_Statement (Loc);
546 else
547 Cond := Empty;
549 C := First_Non_Pragma (L);
550 while Present (C) loop
551 Def_Id := Defining_Identifier (C);
552 Field_Name := Chars (Def_Id);
554 -- The tags need not be checked since they will always be valid
556 -- Note also that in the following, we use Make_Identifier for
557 -- the component names. Use of New_Occurrence_Of to identify
558 -- the components would be incorrect because wrong entities for
559 -- discriminants could be picked up in the private type case.
561 -- Don't bother with abstract parent in interface case
563 if Field_Name = Name_uParent
564 and then Is_Interface (Etype (Def_Id))
565 then
566 null;
568 -- Don't bother with tag, always valid, and not scalar anyway
570 elsif Field_Name = Name_uTag then
571 null;
573 elsif Ekind (Def_Id) = E_Discriminant
574 and then Is_Unchecked_Union (R_Type)
575 then
576 null;
578 -- Don't bother with component with no scalar components
580 elsif not Scalar_Part_Present (Etype (Def_Id)) then
581 null;
583 -- Normal case, generate Valid_Scalars attribute reference
585 else
586 Evolve_Or_Else (Cond,
587 Make_Op_Not (Loc,
588 Right_Opnd =>
589 Make_Attribute_Reference (Loc,
590 Prefix =>
591 Make_Selected_Component (Loc,
592 Prefix =>
593 Make_Identifier (Loc, Name_X),
594 Selector_Name =>
595 Make_Identifier (Loc, Field_Name)),
596 Attribute_Name => Name_Valid_Scalars)));
597 end if;
599 Next_Non_Pragma (C);
600 end loop;
602 if No (Cond) then
603 return Make_Null_Statement (Loc);
605 else
606 return
607 Make_Implicit_If_Statement (E,
608 Condition => Cond,
609 Then_Statements => New_List (
610 Make_Simple_Return_Statement (Loc,
611 Expression =>
612 New_Occurrence_Of (Standard_False, Loc))));
613 end if;
614 end if;
615 end Make_VS_If;
617 -- Local variables
619 Def : constant Node_Id := Parent (R_Type);
620 Comps : constant Node_Id := Component_List (Type_Definition (Def));
621 Stmts : constant List_Id := New_List;
622 Pspecs : constant List_Id := New_List;
624 -- Start of processing for Build_Record_VS_Func
626 begin
627 Append_To (Pspecs,
628 Make_Parameter_Specification (Loc,
629 Defining_Identifier => X,
630 Parameter_Type => New_Occurrence_Of (R_Type, Loc)));
632 Append_To (Stmts,
633 Make_VS_If (R_Type, Discriminant_Specifications (Def)));
634 Append_List_To (Stmts, Make_VS_Case (R_Type, Comps));
636 Append_To (Stmts,
637 Make_Simple_Return_Statement (Loc,
638 Expression => New_Occurrence_Of (Standard_True, Loc)));
640 Insert_Action (Nod,
641 Make_Subprogram_Body (Loc,
642 Specification =>
643 Make_Function_Specification (Loc,
644 Defining_Unit_Name => Func_Id,
645 Parameter_Specifications => Pspecs,
646 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
647 Declarations => New_List,
648 Handled_Statement_Sequence =>
649 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts)),
650 Suppress => Discriminant_Check);
652 if not Debug_Generated_Code then
653 Set_Debug_Info_Off (Func_Id);
654 end if;
656 Set_Is_Pure (Func_Id);
657 return Func_Id;
658 end Build_Record_VS_Func;
660 ----------------------------------
661 -- Compile_Stream_Body_In_Scope --
662 ----------------------------------
664 procedure Compile_Stream_Body_In_Scope
665 (N : Node_Id;
666 Decl : Node_Id;
667 Arr : Entity_Id;
668 Check : Boolean)
670 C_Type : constant Entity_Id := Base_Type (Component_Type (Arr));
671 Curr : constant Entity_Id := Current_Scope;
672 Install : Boolean := False;
673 Scop : Entity_Id := Scope (Arr);
675 begin
676 if Is_Hidden (Arr)
677 and then not In_Open_Scopes (Scop)
678 and then Ekind (Scop) = E_Package
679 then
680 Install := True;
682 else
683 -- The component type may be private, in which case we install its
684 -- full view to compile the subprogram.
686 -- The component type may be private, in which case we install its
687 -- full view to compile the subprogram. We do not do this if the
688 -- type has a Stream_Convert pragma, which indicates that there are
689 -- special stream-processing operations for that type (for example
690 -- Unbounded_String and its wide varieties).
692 Scop := Scope (C_Type);
694 if Is_Private_Type (C_Type)
695 and then Present (Full_View (C_Type))
696 and then not In_Open_Scopes (Scop)
697 and then Ekind (Scop) = E_Package
698 and then No (Get_Stream_Convert_Pragma (C_Type))
699 then
700 Install := True;
701 end if;
702 end if;
704 -- If we are within an instance body, then all visibility has been
705 -- established already and there is no need to install the package.
707 if Install and then not In_Instance_Body then
708 Push_Scope (Scop);
709 Install_Visible_Declarations (Scop);
710 Install_Private_Declarations (Scop);
712 -- The entities in the package are now visible, but the generated
713 -- stream entity must appear in the current scope (usually an
714 -- enclosing stream function) so that itypes all have their proper
715 -- scopes.
717 Push_Scope (Curr);
718 else
719 Install := False;
720 end if;
722 if Check then
723 Insert_Action (N, Decl);
724 else
725 Insert_Action (N, Decl, Suppress => All_Checks);
726 end if;
728 if Install then
730 -- Remove extra copy of current scope, and package itself
732 Pop_Scope;
733 End_Package_Scope (Scop);
734 end if;
735 end Compile_Stream_Body_In_Scope;
737 -----------------------------------
738 -- Expand_Access_To_Protected_Op --
739 -----------------------------------
741 procedure Expand_Access_To_Protected_Op
742 (N : Node_Id;
743 Pref : Node_Id;
744 Typ : Entity_Id)
746 -- The value of the attribute_reference is a record containing two
747 -- fields: an access to the protected object, and an access to the
748 -- subprogram itself. The prefix is a selected component.
750 Loc : constant Source_Ptr := Sloc (N);
751 Agg : Node_Id;
752 Btyp : constant Entity_Id := Base_Type (Typ);
753 Sub : Entity_Id;
754 Sub_Ref : Node_Id;
755 E_T : constant Entity_Id := Equivalent_Type (Btyp);
756 Acc : constant Entity_Id :=
757 Etype (Next_Component (First_Component (E_T)));
758 Obj_Ref : Node_Id;
759 Curr : Entity_Id;
761 -- Start of processing for Expand_Access_To_Protected_Op
763 begin
764 -- Within the body of the protected type, the prefix designates a local
765 -- operation, and the object is the first parameter of the corresponding
766 -- protected body of the current enclosing operation.
768 if Is_Entity_Name (Pref) then
769 -- All indirect calls are external calls, so must do locking and
770 -- barrier reevaluation, even if the 'Access occurs within the
771 -- protected body. Hence the call to External_Subprogram, as opposed
772 -- to Protected_Body_Subprogram, below. See RM-9.5(5). This means
773 -- that indirect calls from within the same protected body will
774 -- deadlock, as allowed by RM-9.5.1(8,15,17).
776 Sub := New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc);
778 -- Don't traverse the scopes when the attribute occurs within an init
779 -- proc, because we directly use the _init formal of the init proc in
780 -- that case.
782 Curr := Current_Scope;
783 if not Is_Init_Proc (Curr) then
784 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
786 while Scope (Curr) /= Scope (Entity (Pref)) loop
787 Curr := Scope (Curr);
788 end loop;
789 end if;
791 -- In case of protected entries the first formal of its Protected_
792 -- Body_Subprogram is the address of the object.
794 if Ekind (Curr) = E_Entry then
795 Obj_Ref :=
796 New_Occurrence_Of
797 (First_Formal
798 (Protected_Body_Subprogram (Curr)), Loc);
800 -- If the current scope is an init proc, then use the address of the
801 -- _init formal as the object reference.
803 elsif Is_Init_Proc (Curr) then
804 Obj_Ref :=
805 Make_Attribute_Reference (Loc,
806 Prefix => New_Occurrence_Of (First_Formal (Curr), Loc),
807 Attribute_Name => Name_Address);
809 -- In case of protected subprograms the first formal of its
810 -- Protected_Body_Subprogram is the object and we get its address.
812 else
813 Obj_Ref :=
814 Make_Attribute_Reference (Loc,
815 Prefix =>
816 New_Occurrence_Of
817 (First_Formal
818 (Protected_Body_Subprogram (Curr)), Loc),
819 Attribute_Name => Name_Address);
820 end if;
822 -- Case where the prefix is not an entity name. Find the
823 -- version of the protected operation to be called from
824 -- outside the protected object.
826 else
827 Sub :=
828 New_Occurrence_Of
829 (External_Subprogram
830 (Entity (Selector_Name (Pref))), Loc);
832 Obj_Ref :=
833 Make_Attribute_Reference (Loc,
834 Prefix => Relocate_Node (Prefix (Pref)),
835 Attribute_Name => Name_Address);
836 end if;
838 Sub_Ref :=
839 Make_Attribute_Reference (Loc,
840 Prefix => Sub,
841 Attribute_Name => Name_Access);
843 -- We set the type of the access reference to the already generated
844 -- access_to_subprogram type, and declare the reference analyzed, to
845 -- prevent further expansion when the enclosing aggregate is analyzed.
847 Set_Etype (Sub_Ref, Acc);
848 Set_Analyzed (Sub_Ref);
850 Agg :=
851 Make_Aggregate (Loc,
852 Expressions => New_List (Obj_Ref, Sub_Ref));
854 -- Sub_Ref has been marked as analyzed, but we still need to make sure
855 -- Sub is correctly frozen.
857 Freeze_Before (N, Entity (Sub));
859 Rewrite (N, Agg);
860 Analyze_And_Resolve (N, E_T);
862 -- For subsequent analysis, the node must retain its type. The backend
863 -- will replace it with the equivalent type where needed.
865 Set_Etype (N, Typ);
866 end Expand_Access_To_Protected_Op;
868 --------------------------
869 -- Expand_Fpt_Attribute --
870 --------------------------
872 procedure Expand_Fpt_Attribute
873 (N : Node_Id;
874 Pkg : RE_Id;
875 Nam : Name_Id;
876 Args : List_Id)
878 Loc : constant Source_Ptr := Sloc (N);
879 Typ : constant Entity_Id := Etype (N);
880 Fnm : Node_Id;
882 begin
883 -- The function name is the selected component Attr_xxx.yyy where
884 -- Attr_xxx is the package name, and yyy is the argument Nam.
886 -- Note: it would be more usual to have separate RE entries for each
887 -- of the entities in the Fat packages, but first they have identical
888 -- names (so we would have to have lots of renaming declarations to
889 -- meet the normal RE rule of separate names for all runtime entities),
890 -- and second there would be an awful lot of them.
892 Fnm :=
893 Make_Selected_Component (Loc,
894 Prefix => New_Occurrence_Of (RTE (Pkg), Loc),
895 Selector_Name => Make_Identifier (Loc, Nam));
897 -- The generated call is given the provided set of parameters, and then
898 -- wrapped in a conversion which converts the result to the target type
899 -- We use the base type as the target because a range check may be
900 -- required.
902 Rewrite (N,
903 Unchecked_Convert_To (Base_Type (Etype (N)),
904 Make_Function_Call (Loc,
905 Name => Fnm,
906 Parameter_Associations => Args)));
908 Analyze_And_Resolve (N, Typ);
909 end Expand_Fpt_Attribute;
911 ----------------------------
912 -- Expand_Fpt_Attribute_R --
913 ----------------------------
915 -- The single argument is converted to its root type to call the
916 -- appropriate runtime function, with the actual call being built
917 -- by Expand_Fpt_Attribute
919 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
920 E1 : constant Node_Id := First (Expressions (N));
921 Ftp : Entity_Id;
922 Pkg : RE_Id;
923 begin
924 Find_Fat_Info (Etype (E1), Ftp, Pkg);
925 Expand_Fpt_Attribute
926 (N, Pkg, Attribute_Name (N),
927 New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1))));
928 end Expand_Fpt_Attribute_R;
930 -----------------------------
931 -- Expand_Fpt_Attribute_RI --
932 -----------------------------
934 -- The first argument is converted to its root type and the second
935 -- argument is converted to standard long long integer to call the
936 -- appropriate runtime function, with the actual call being built
937 -- by Expand_Fpt_Attribute
939 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
940 E1 : constant Node_Id := First (Expressions (N));
941 Ftp : Entity_Id;
942 Pkg : RE_Id;
943 E2 : constant Node_Id := Next (E1);
944 begin
945 Find_Fat_Info (Etype (E1), Ftp, Pkg);
946 Expand_Fpt_Attribute
947 (N, Pkg, Attribute_Name (N),
948 New_List (
949 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
950 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
951 end Expand_Fpt_Attribute_RI;
953 -----------------------------
954 -- Expand_Fpt_Attribute_RR --
955 -----------------------------
957 -- The two arguments are converted to their root types to call the
958 -- appropriate runtime function, with the actual call being built
959 -- by Expand_Fpt_Attribute
961 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
962 E1 : constant Node_Id := First (Expressions (N));
963 E2 : constant Node_Id := Next (E1);
964 Ftp : Entity_Id;
965 Pkg : RE_Id;
967 begin
968 Find_Fat_Info (Etype (E1), Ftp, Pkg);
969 Expand_Fpt_Attribute
970 (N, Pkg, Attribute_Name (N),
971 New_List (
972 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
973 Unchecked_Convert_To (Ftp, Relocate_Node (E2))));
974 end Expand_Fpt_Attribute_RR;
976 ---------------------------------
977 -- Expand_Loop_Entry_Attribute --
978 ---------------------------------
980 procedure Expand_Loop_Entry_Attribute (N : Node_Id) is
981 procedure Build_Conditional_Block
982 (Loc : Source_Ptr;
983 Cond : Node_Id;
984 Loop_Stmt : Node_Id;
985 If_Stmt : out Node_Id;
986 Blk_Stmt : out Node_Id);
987 -- Create a block Blk_Stmt with an empty declarative list and a single
988 -- loop Loop_Stmt. The block is encased in an if statement If_Stmt with
989 -- condition Cond. If_Stmt is Empty when there is no condition provided.
991 function Is_Array_Iteration (N : Node_Id) return Boolean;
992 -- Determine whether loop statement N denotes an Ada 2012 iteration over
993 -- an array object.
995 -----------------------------
996 -- Build_Conditional_Block --
997 -----------------------------
999 procedure Build_Conditional_Block
1000 (Loc : Source_Ptr;
1001 Cond : Node_Id;
1002 Loop_Stmt : Node_Id;
1003 If_Stmt : out Node_Id;
1004 Blk_Stmt : out Node_Id)
1006 begin
1007 -- Do not reanalyze the original loop statement because it is simply
1008 -- being relocated.
1010 Set_Analyzed (Loop_Stmt);
1012 Blk_Stmt :=
1013 Make_Block_Statement (Loc,
1014 Declarations => New_List,
1015 Handled_Statement_Sequence =>
1016 Make_Handled_Sequence_Of_Statements (Loc,
1017 Statements => New_List (Loop_Stmt)));
1019 if Present (Cond) then
1020 If_Stmt :=
1021 Make_If_Statement (Loc,
1022 Condition => Cond,
1023 Then_Statements => New_List (Blk_Stmt));
1024 else
1025 If_Stmt := Empty;
1026 end if;
1027 end Build_Conditional_Block;
1029 ------------------------
1030 -- Is_Array_Iteration --
1031 ------------------------
1033 function Is_Array_Iteration (N : Node_Id) return Boolean is
1034 Stmt : constant Node_Id := Original_Node (N);
1035 Iter : Node_Id;
1037 begin
1038 if Nkind (Stmt) = N_Loop_Statement
1039 and then Present (Iteration_Scheme (Stmt))
1040 and then Present (Iterator_Specification (Iteration_Scheme (Stmt)))
1041 then
1042 Iter := Iterator_Specification (Iteration_Scheme (Stmt));
1044 return
1045 Of_Present (Iter) and then Is_Array_Type (Etype (Name (Iter)));
1046 end if;
1048 return False;
1049 end Is_Array_Iteration;
1051 -- Local variables
1053 Pref : constant Node_Id := Prefix (N);
1054 Base_Typ : constant Entity_Id := Base_Type (Etype (Pref));
1055 Exprs : constant List_Id := Expressions (N);
1056 Aux_Decl : Node_Id;
1057 Blk : Node_Id;
1058 Decls : List_Id;
1059 Installed : Boolean;
1060 Loc : Source_Ptr;
1061 Loop_Id : Entity_Id;
1062 Loop_Stmt : Node_Id;
1063 Result : Node_Id := Empty;
1064 Scheme : Node_Id;
1065 Temp_Decl : Node_Id;
1066 Temp_Id : Entity_Id;
1068 -- Start of processing for Expand_Loop_Entry_Attribute
1070 begin
1071 -- Step 1: Find the related loop
1073 -- The loop label variant of attribute 'Loop_Entry already has all the
1074 -- information in its expression.
1076 if Present (Exprs) then
1077 Loop_Id := Entity (First (Exprs));
1078 Loop_Stmt := Label_Construct (Parent (Loop_Id));
1080 -- Climb the parent chain to find the nearest enclosing loop. Skip
1081 -- all internally generated loops for quantified expressions and for
1082 -- element iterators over multidimensional arrays because the pragma
1083 -- applies to source loop.
1085 else
1086 Loop_Stmt := N;
1087 while Present (Loop_Stmt) loop
1088 if Nkind (Loop_Stmt) = N_Loop_Statement
1089 and then Nkind (Original_Node (Loop_Stmt)) = N_Loop_Statement
1090 and then Comes_From_Source (Original_Node (Loop_Stmt))
1091 then
1092 exit;
1093 end if;
1095 Loop_Stmt := Parent (Loop_Stmt);
1096 end loop;
1098 Loop_Id := Entity (Identifier (Loop_Stmt));
1099 end if;
1101 Loc := Sloc (Loop_Stmt);
1103 -- Step 2: Transform the loop
1105 -- The loop has already been transformed during the expansion of a prior
1106 -- 'Loop_Entry attribute. Retrieve the declarative list of the block.
1108 if Has_Loop_Entry_Attributes (Loop_Id) then
1110 -- When the related loop name appears as the argument of attribute
1111 -- Loop_Entry, the corresponding label construct is the generated
1112 -- block statement. This is because the expander reuses the label.
1114 if Nkind (Loop_Stmt) = N_Block_Statement then
1115 Decls := Declarations (Loop_Stmt);
1117 -- In all other cases, the loop must appear in the handled sequence
1118 -- of statements of the generated block.
1120 else
1121 pragma Assert
1122 (Nkind (Parent (Loop_Stmt)) = N_Handled_Sequence_Of_Statements
1123 and then
1124 Nkind (Parent (Parent (Loop_Stmt))) = N_Block_Statement);
1126 Decls := Declarations (Parent (Parent (Loop_Stmt)));
1127 end if;
1129 -- Transform the loop into a conditional block
1131 else
1132 Set_Has_Loop_Entry_Attributes (Loop_Id);
1133 Scheme := Iteration_Scheme (Loop_Stmt);
1135 -- Infinite loops are transformed into:
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 -- end loop;
1145 -- end;
1147 if No (Scheme) then
1148 Build_Conditional_Block (Loc,
1149 Cond => Empty,
1150 Loop_Stmt => Relocate_Node (Loop_Stmt),
1151 If_Stmt => Result,
1152 Blk_Stmt => Blk);
1154 Result := Blk;
1156 -- While loops are transformed into:
1158 -- function Fnn return Boolean is
1159 -- begin
1160 -- <condition actions>
1161 -- return <condition>;
1162 -- end Fnn;
1164 -- if Fnn then
1165 -- declare
1166 -- Temp1 : constant <type of Pref1> := <Pref1>;
1167 -- . . .
1168 -- TempN : constant <type of PrefN> := <PrefN>;
1169 -- begin
1170 -- loop
1171 -- <original source statements with attribute rewrites>
1172 -- exit when not Fnn;
1173 -- end loop;
1174 -- end;
1175 -- end if;
1177 -- Note that loops over iterators and containers are already
1178 -- converted into while loops.
1180 elsif Present (Condition (Scheme)) then
1181 declare
1182 Func_Decl : Node_Id;
1183 Func_Id : Entity_Id;
1184 Stmts : List_Id;
1186 begin
1187 -- Wrap the condition of the while loop in a Boolean function.
1188 -- This avoids the duplication of the same code which may lead
1189 -- to gigi issues with respect to multiple declaration of the
1190 -- same entity in the presence of side effects or checks. Note
1191 -- that the condition actions must also be relocated to the
1192 -- wrapping function.
1194 -- Generate:
1195 -- <condition actions>
1196 -- return <condition>;
1198 if Present (Condition_Actions (Scheme)) then
1199 Stmts := Condition_Actions (Scheme);
1200 else
1201 Stmts := New_List;
1202 end if;
1204 Append_To (Stmts,
1205 Make_Simple_Return_Statement (Loc,
1206 Expression => Relocate_Node (Condition (Scheme))));
1208 -- Generate:
1209 -- function Fnn return Boolean is
1210 -- begin
1211 -- <Stmts>
1212 -- end Fnn;
1214 Func_Id := Make_Temporary (Loc, 'F');
1215 Func_Decl :=
1216 Make_Subprogram_Body (Loc,
1217 Specification =>
1218 Make_Function_Specification (Loc,
1219 Defining_Unit_Name => Func_Id,
1220 Result_Definition =>
1221 New_Occurrence_Of (Standard_Boolean, Loc)),
1222 Declarations => Empty_List,
1223 Handled_Statement_Sequence =>
1224 Make_Handled_Sequence_Of_Statements (Loc,
1225 Statements => Stmts));
1227 -- The function is inserted before the related loop. Make sure
1228 -- to analyze it in the context of the loop's enclosing scope.
1230 Push_Scope (Scope (Loop_Id));
1231 Insert_Action (Loop_Stmt, Func_Decl);
1232 Pop_Scope;
1234 -- Transform the original while loop into an infinite loop
1235 -- where the last statement checks the negated condition. This
1236 -- placement ensures that the condition will not be evaluated
1237 -- twice on the first iteration.
1239 Set_Iteration_Scheme (Loop_Stmt, Empty);
1240 Scheme := Empty;
1242 -- Generate:
1243 -- exit when not Fnn;
1245 Append_To (Statements (Loop_Stmt),
1246 Make_Exit_Statement (Loc,
1247 Condition =>
1248 Make_Op_Not (Loc,
1249 Right_Opnd =>
1250 Make_Function_Call (Loc,
1251 Name => New_Occurrence_Of (Func_Id, Loc)))));
1253 Build_Conditional_Block (Loc,
1254 Cond =>
1255 Make_Function_Call (Loc,
1256 Name => New_Occurrence_Of (Func_Id, Loc)),
1257 Loop_Stmt => Relocate_Node (Loop_Stmt),
1258 If_Stmt => Result,
1259 Blk_Stmt => Blk);
1260 end;
1262 -- Ada 2012 iteration over an array is transformed into:
1264 -- if <Array_Nam>'Length (1) > 0
1265 -- and then <Array_Nam>'Length (N) > 0
1266 -- then
1267 -- declare
1268 -- Temp1 : constant <type of Pref1> := <Pref1>;
1269 -- . . .
1270 -- TempN : constant <type of PrefN> := <PrefN>;
1271 -- begin
1272 -- for X in ... loop -- multiple loops depending on dims
1273 -- <original source statements with attribute rewrites>
1274 -- end loop;
1275 -- end;
1276 -- end if;
1278 elsif Is_Array_Iteration (Loop_Stmt) then
1279 declare
1280 Array_Nam : constant Entity_Id :=
1281 Entity (Name (Iterator_Specification
1282 (Iteration_Scheme (Original_Node (Loop_Stmt)))));
1283 Num_Dims : constant Pos :=
1284 Number_Dimensions (Etype (Array_Nam));
1285 Cond : Node_Id := Empty;
1286 Check : Node_Id;
1288 begin
1289 -- Generate a check which determines whether all dimensions of
1290 -- the array are non-null.
1292 for Dim in 1 .. Num_Dims loop
1293 Check :=
1294 Make_Op_Gt (Loc,
1295 Left_Opnd =>
1296 Make_Attribute_Reference (Loc,
1297 Prefix => New_Occurrence_Of (Array_Nam, Loc),
1298 Attribute_Name => Name_Length,
1299 Expressions => New_List (
1300 Make_Integer_Literal (Loc, Dim))),
1301 Right_Opnd =>
1302 Make_Integer_Literal (Loc, 0));
1304 if No (Cond) then
1305 Cond := Check;
1306 else
1307 Cond :=
1308 Make_And_Then (Loc,
1309 Left_Opnd => Cond,
1310 Right_Opnd => Check);
1311 end if;
1312 end loop;
1314 Build_Conditional_Block (Loc,
1315 Cond => Cond,
1316 Loop_Stmt => Relocate_Node (Loop_Stmt),
1317 If_Stmt => Result,
1318 Blk_Stmt => Blk);
1319 end;
1321 -- For loops are transformed into:
1323 -- if <Low> <= <High> then
1324 -- declare
1325 -- Temp1 : constant <type of Pref1> := <Pref1>;
1326 -- . . .
1327 -- TempN : constant <type of PrefN> := <PrefN>;
1328 -- begin
1329 -- for <Def_Id> in <Low> .. <High> loop
1330 -- <original source statements with attribute rewrites>
1331 -- end loop;
1332 -- end;
1333 -- end if;
1335 elsif Present (Loop_Parameter_Specification (Scheme)) then
1336 declare
1337 Loop_Spec : constant Node_Id :=
1338 Loop_Parameter_Specification (Scheme);
1339 Cond : Node_Id;
1340 Subt_Def : Node_Id;
1342 begin
1343 Subt_Def := Discrete_Subtype_Definition (Loop_Spec);
1345 -- When the loop iterates over a subtype indication with a
1346 -- range, use the low and high bounds of the subtype itself.
1348 if Nkind (Subt_Def) = N_Subtype_Indication then
1349 Subt_Def := Scalar_Range (Etype (Subt_Def));
1350 end if;
1352 pragma Assert (Nkind (Subt_Def) = N_Range);
1354 -- Generate
1355 -- Low <= High
1357 Cond :=
1358 Make_Op_Le (Loc,
1359 Left_Opnd => New_Copy_Tree (Low_Bound (Subt_Def)),
1360 Right_Opnd => New_Copy_Tree (High_Bound (Subt_Def)));
1362 Build_Conditional_Block (Loc,
1363 Cond => Cond,
1364 Loop_Stmt => Relocate_Node (Loop_Stmt),
1365 If_Stmt => Result,
1366 Blk_Stmt => Blk);
1367 end;
1368 end if;
1370 Decls := Declarations (Blk);
1371 end if;
1373 -- Step 3: Create a constant to capture the value of the prefix at the
1374 -- entry point into the loop.
1376 Temp_Id := Make_Temporary (Loc, 'P');
1378 -- Preserve the tag of the prefix by offering a specific view of the
1379 -- class-wide version of the prefix.
1381 if Is_Tagged_Type (Base_Typ) then
1382 Tagged_Case : declare
1383 CW_Temp : Entity_Id;
1384 CW_Typ : Entity_Id;
1386 begin
1387 -- Generate:
1388 -- CW_Temp : constant Base_Typ'Class := Base_Typ'Class (Pref);
1390 CW_Temp := Make_Temporary (Loc, 'T');
1391 CW_Typ := Class_Wide_Type (Base_Typ);
1393 Aux_Decl :=
1394 Make_Object_Declaration (Loc,
1395 Defining_Identifier => CW_Temp,
1396 Constant_Present => True,
1397 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
1398 Expression =>
1399 Convert_To (CW_Typ, Relocate_Node (Pref)));
1400 Append_To (Decls, Aux_Decl);
1402 -- Generate:
1403 -- Temp : Base_Typ renames Base_Typ (CW_Temp);
1405 Temp_Decl :=
1406 Make_Object_Renaming_Declaration (Loc,
1407 Defining_Identifier => Temp_Id,
1408 Subtype_Mark => New_Occurrence_Of (Base_Typ, Loc),
1409 Name =>
1410 Convert_To (Base_Typ, New_Occurrence_Of (CW_Temp, Loc)));
1411 Append_To (Decls, Temp_Decl);
1412 end Tagged_Case;
1414 -- Untagged case
1416 else
1417 Untagged_Case : declare
1418 Temp_Expr : Node_Id;
1420 begin
1421 Aux_Decl := Empty;
1423 -- Generate a nominal type for the constant when the prefix is of
1424 -- a constrained type. This is achieved by setting the Etype of
1425 -- the relocated prefix to its base type. Since the prefix is now
1426 -- the initialization expression of the constant, its freezing
1427 -- will produce a proper nominal type.
1429 Temp_Expr := Relocate_Node (Pref);
1430 Set_Etype (Temp_Expr, Base_Typ);
1432 -- Generate:
1433 -- Temp : constant Base_Typ := Pref;
1435 Temp_Decl :=
1436 Make_Object_Declaration (Loc,
1437 Defining_Identifier => Temp_Id,
1438 Constant_Present => True,
1439 Object_Definition => New_Occurrence_Of (Base_Typ, Loc),
1440 Expression => Temp_Expr);
1441 Append_To (Decls, Temp_Decl);
1442 end Untagged_Case;
1443 end if;
1445 -- Step 4: Analyze all bits
1447 Installed := Current_Scope = Scope (Loop_Id);
1449 -- Depending on the pracement of attribute 'Loop_Entry relative to the
1450 -- associated loop, ensure the proper visibility for analysis.
1452 if not Installed then
1453 Push_Scope (Scope (Loop_Id));
1454 end if;
1456 -- The analysis of the conditional block takes care of the constant
1457 -- declaration.
1459 if Present (Result) then
1460 Rewrite (Loop_Stmt, Result);
1461 Analyze (Loop_Stmt);
1463 -- The conditional block was analyzed when a previous 'Loop_Entry was
1464 -- expanded. There is no point in reanalyzing the block, simply analyze
1465 -- the declaration of the constant.
1467 else
1468 if Present (Aux_Decl) then
1469 Analyze (Aux_Decl);
1470 end if;
1472 Analyze (Temp_Decl);
1473 end if;
1475 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
1476 Analyze (N);
1478 if not Installed then
1479 Pop_Scope;
1480 end if;
1481 end Expand_Loop_Entry_Attribute;
1483 ------------------------------
1484 -- Expand_Min_Max_Attribute --
1485 ------------------------------
1487 procedure Expand_Min_Max_Attribute (N : Node_Id) is
1488 begin
1489 -- Min and Max are handled by the back end (except that static cases
1490 -- have already been evaluated during semantic processing, although the
1491 -- back end should not count on this). The one bit of special processing
1492 -- required in the normal case is that these two attributes typically
1493 -- generate conditionals in the code, so check the relevant restriction.
1495 Check_Restriction (No_Implicit_Conditionals, N);
1497 -- In Modify_Tree_For_C mode, we rewrite as an if expression
1499 if Modify_Tree_For_C then
1500 declare
1501 Loc : constant Source_Ptr := Sloc (N);
1502 Typ : constant Entity_Id := Etype (N);
1503 Expr : constant Node_Id := First (Expressions (N));
1504 Left : constant Node_Id := Relocate_Node (Expr);
1505 Right : constant Node_Id := Relocate_Node (Next (Expr));
1507 function Make_Compare (Left, Right : Node_Id) return Node_Id;
1508 -- Returns Left >= Right for Max, Left <= Right for Min
1510 ------------------
1511 -- Make_Compare --
1512 ------------------
1514 function Make_Compare (Left, Right : Node_Id) return Node_Id is
1515 begin
1516 if Attribute_Name (N) = Name_Max then
1517 return
1518 Make_Op_Ge (Loc,
1519 Left_Opnd => Left,
1520 Right_Opnd => Right);
1521 else
1522 return
1523 Make_Op_Le (Loc,
1524 Left_Opnd => Left,
1525 Right_Opnd => Right);
1526 end if;
1527 end Make_Compare;
1529 -- Start of processing for Min_Max
1531 begin
1532 -- If both Left and Right are side effect free, then we can just
1533 -- use Duplicate_Expr to duplicate the references and return
1535 -- (if Left >=|<= Right then Left else Right)
1537 if Side_Effect_Free (Left) and then Side_Effect_Free (Right) then
1538 Rewrite (N,
1539 Make_If_Expression (Loc,
1540 Expressions => New_List (
1541 Make_Compare (Left, Right),
1542 Duplicate_Subexpr_No_Checks (Left),
1543 Duplicate_Subexpr_No_Checks (Right))));
1545 -- Otherwise we generate declarations to capture the values.
1547 -- The translation is
1549 -- do
1550 -- T1 : constant typ := Left;
1551 -- T2 : constant typ := Right;
1552 -- in
1553 -- (if T1 >=|<= T2 then T1 else T2)
1554 -- end;
1556 else
1557 declare
1558 T1 : constant Entity_Id := Make_Temporary (Loc, 'T', Left);
1559 T2 : constant Entity_Id := Make_Temporary (Loc, 'T', Right);
1561 begin
1562 Rewrite (N,
1563 Make_Expression_With_Actions (Loc,
1564 Actions => New_List (
1565 Make_Object_Declaration (Loc,
1566 Defining_Identifier => T1,
1567 Constant_Present => True,
1568 Object_Definition =>
1569 New_Occurrence_Of (Etype (Left), Loc),
1570 Expression => Relocate_Node (Left)),
1572 Make_Object_Declaration (Loc,
1573 Defining_Identifier => T2,
1574 Constant_Present => True,
1575 Object_Definition =>
1576 New_Occurrence_Of (Etype (Right), Loc),
1577 Expression => Relocate_Node (Right))),
1579 Expression =>
1580 Make_If_Expression (Loc,
1581 Expressions => New_List (
1582 Make_Compare
1583 (New_Occurrence_Of (T1, Loc),
1584 New_Occurrence_Of (T2, Loc)),
1585 New_Occurrence_Of (T1, Loc),
1586 New_Occurrence_Of (T2, Loc)))));
1587 end;
1588 end if;
1590 Analyze_And_Resolve (N, Typ);
1591 end;
1592 end if;
1593 end Expand_Min_Max_Attribute;
1595 ----------------------------------
1596 -- Expand_N_Attribute_Reference --
1597 ----------------------------------
1599 procedure Expand_N_Attribute_Reference (N : Node_Id) is
1600 Loc : constant Source_Ptr := Sloc (N);
1601 Typ : constant Entity_Id := Etype (N);
1602 Btyp : constant Entity_Id := Base_Type (Typ);
1603 Pref : constant Node_Id := Prefix (N);
1604 Ptyp : constant Entity_Id := Etype (Pref);
1605 Exprs : constant List_Id := Expressions (N);
1606 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
1608 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
1609 -- Rewrites a stream attribute for Read, Write or Output with the
1610 -- procedure call. Pname is the entity for the procedure to call.
1612 ------------------------------
1613 -- Rewrite_Stream_Proc_Call --
1614 ------------------------------
1616 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
1617 Item : constant Node_Id := Next (First (Exprs));
1618 Item_Typ : constant Entity_Id := Etype (Item);
1619 Formal : constant Entity_Id := Next_Formal (First_Formal (Pname));
1620 Formal_Typ : constant Entity_Id := Etype (Formal);
1621 Is_Written : constant Boolean := Ekind (Formal) /= E_In_Parameter;
1623 begin
1624 -- The expansion depends on Item, the second actual, which is
1625 -- the object being streamed in or out.
1627 -- If the item is a component of a packed array type, and
1628 -- a conversion is needed on exit, we introduce a temporary to
1629 -- hold the value, because otherwise the packed reference will
1630 -- not be properly expanded.
1632 if Nkind (Item) = N_Indexed_Component
1633 and then Is_Packed (Base_Type (Etype (Prefix (Item))))
1634 and then Base_Type (Item_Typ) /= Base_Type (Formal_Typ)
1635 and then Is_Written
1636 then
1637 declare
1638 Temp : constant Entity_Id := Make_Temporary (Loc, 'V');
1639 Decl : Node_Id;
1640 Assn : Node_Id;
1642 begin
1643 Decl :=
1644 Make_Object_Declaration (Loc,
1645 Defining_Identifier => Temp,
1646 Object_Definition => New_Occurrence_Of (Formal_Typ, Loc));
1647 Set_Etype (Temp, Formal_Typ);
1649 Assn :=
1650 Make_Assignment_Statement (Loc,
1651 Name => New_Copy_Tree (Item),
1652 Expression =>
1653 Unchecked_Convert_To
1654 (Item_Typ, New_Occurrence_Of (Temp, Loc)));
1656 Rewrite (Item, New_Occurrence_Of (Temp, Loc));
1657 Insert_Actions (N,
1658 New_List (
1659 Decl,
1660 Make_Procedure_Call_Statement (Loc,
1661 Name => New_Occurrence_Of (Pname, Loc),
1662 Parameter_Associations => Exprs),
1663 Assn));
1665 Rewrite (N, Make_Null_Statement (Loc));
1666 return;
1667 end;
1668 end if;
1670 -- For the class-wide dispatching cases, and for cases in which
1671 -- the base type of the second argument matches the base type of
1672 -- the corresponding formal parameter (that is to say the stream
1673 -- operation is not inherited), we are all set, and can use the
1674 -- argument unchanged.
1676 if not Is_Class_Wide_Type (Entity (Pref))
1677 and then not Is_Class_Wide_Type (Etype (Item))
1678 and then Base_Type (Item_Typ) /= Base_Type (Formal_Typ)
1679 then
1680 -- Perform a view conversion when either the argument or the
1681 -- formal parameter are of a private type.
1683 if Is_Private_Type (Base_Type (Formal_Typ))
1684 or else Is_Private_Type (Base_Type (Item_Typ))
1685 then
1686 Rewrite (Item,
1687 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
1689 -- Otherwise perform a regular type conversion to ensure that all
1690 -- relevant checks are installed.
1692 else
1693 Rewrite (Item, Convert_To (Formal_Typ, Relocate_Node (Item)));
1694 end if;
1696 -- For untagged derived types set Assignment_OK, to prevent
1697 -- copies from being created when the unchecked conversion
1698 -- is expanded (which would happen in Remove_Side_Effects
1699 -- if Expand_N_Unchecked_Conversion were allowed to call
1700 -- Force_Evaluation). The copy could violate Ada semantics in
1701 -- cases such as an actual that is an out parameter. Note that
1702 -- this approach is also used in exp_ch7 for calls to controlled
1703 -- type operations to prevent problems with actuals wrapped in
1704 -- unchecked conversions.
1706 if Is_Untagged_Derivation (Etype (Expression (Item))) then
1707 Set_Assignment_OK (Item);
1708 end if;
1709 end if;
1711 -- The stream operation to call may be a renaming created by an
1712 -- attribute definition clause, and may not be frozen yet. Ensure
1713 -- that it has the necessary extra formals.
1715 if not Is_Frozen (Pname) then
1716 Create_Extra_Formals (Pname);
1717 end if;
1719 -- And now rewrite the call
1721 Rewrite (N,
1722 Make_Procedure_Call_Statement (Loc,
1723 Name => New_Occurrence_Of (Pname, Loc),
1724 Parameter_Associations => Exprs));
1726 Analyze (N);
1727 end Rewrite_Stream_Proc_Call;
1729 -- Start of processing for Expand_N_Attribute_Reference
1731 begin
1732 -- Do required validity checking, if enabled. Do not apply check to
1733 -- output parameters of an Asm instruction, since the value of this
1734 -- is not set till after the attribute has been elaborated, and do
1735 -- not apply the check to the arguments of a 'Read or 'Input attribute
1736 -- reference since the scalar argument is an OUT scalar.
1738 if Validity_Checks_On and then Validity_Check_Operands
1739 and then Id /= Attribute_Asm_Output
1740 and then Id /= Attribute_Read
1741 and then Id /= Attribute_Input
1742 then
1743 declare
1744 Expr : Node_Id;
1745 begin
1746 Expr := First (Expressions (N));
1747 while Present (Expr) loop
1748 Ensure_Valid (Expr);
1749 Next (Expr);
1750 end loop;
1751 end;
1752 end if;
1754 -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in-
1755 -- place function, then a temporary return object needs to be created
1756 -- and access to it must be passed to the function. Currently we limit
1757 -- such functions to those with inherently limited result subtypes, but
1758 -- eventually we plan to expand the functions that are treated as
1759 -- build-in-place to include other composite result types.
1761 if Ada_Version >= Ada_2005
1762 and then Is_Build_In_Place_Function_Call (Pref)
1763 then
1764 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
1766 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
1767 -- containing build-in-place function calls whose returned object covers
1768 -- interface types.
1770 elsif Ada_Version >= Ada_2005
1771 and then Present (Unqual_BIP_Iface_Function_Call (Pref))
1772 then
1773 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Pref);
1774 end if;
1776 -- If prefix is a protected type name, this is a reference to the
1777 -- current instance of the type. For a component definition, nothing
1778 -- to do (expansion will occur in the init proc). In other contexts,
1779 -- rewrite into reference to current instance.
1781 if Is_Protected_Self_Reference (Pref)
1782 and then not
1783 (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint,
1784 N_Discriminant_Association)
1785 and then Nkind (Parent (Parent (Parent (Parent (N))))) =
1786 N_Component_Definition)
1788 -- No action needed for these attributes since the current instance
1789 -- will be rewritten to be the name of the _object parameter
1790 -- associated with the enclosing protected subprogram (see below).
1792 and then Id /= Attribute_Access
1793 and then Id /= Attribute_Unchecked_Access
1794 and then Id /= Attribute_Unrestricted_Access
1795 then
1796 Rewrite (Pref, Concurrent_Ref (Pref));
1797 Analyze (Pref);
1798 end if;
1800 -- Remaining processing depends on specific attribute
1802 -- Note: individual sections of the following case statement are
1803 -- allowed to assume there is no code after the case statement, and
1804 -- are legitimately allowed to execute return statements if they have
1805 -- nothing more to do.
1807 case Id is
1809 -- Attributes related to Ada 2012 iterators
1811 when Attribute_Constant_Indexing
1812 | Attribute_Default_Iterator
1813 | Attribute_Implicit_Dereference
1814 | Attribute_Iterable
1815 | Attribute_Iterator_Element
1816 | Attribute_Variable_Indexing
1818 null;
1820 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
1821 -- were already rejected by the parser. Thus they shouldn't appear here.
1823 when Internal_Attribute_Id =>
1824 raise Program_Error;
1826 ------------
1827 -- Access --
1828 ------------
1830 when Attribute_Access
1831 | Attribute_Unchecked_Access
1832 | Attribute_Unrestricted_Access
1834 Access_Cases : declare
1835 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
1836 Btyp_DDT : Entity_Id;
1838 function Enclosing_Object (N : Node_Id) return Node_Id;
1839 -- If N denotes a compound name (selected component, indexed
1840 -- component, or slice), returns the name of the outermost such
1841 -- enclosing object. Otherwise returns N. If the object is a
1842 -- renaming, then the renamed object is returned.
1844 ----------------------
1845 -- Enclosing_Object --
1846 ----------------------
1848 function Enclosing_Object (N : Node_Id) return Node_Id is
1849 Obj_Name : Node_Id;
1851 begin
1852 Obj_Name := N;
1853 while Nkind_In (Obj_Name, N_Selected_Component,
1854 N_Indexed_Component,
1855 N_Slice)
1856 loop
1857 Obj_Name := Prefix (Obj_Name);
1858 end loop;
1860 return Get_Referenced_Object (Obj_Name);
1861 end Enclosing_Object;
1863 -- Local declarations
1865 Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object);
1867 -- Start of processing for Access_Cases
1869 begin
1870 Btyp_DDT := Designated_Type (Btyp);
1872 -- Handle designated types that come from the limited view
1874 if From_Limited_With (Btyp_DDT)
1875 and then Has_Non_Limited_View (Btyp_DDT)
1876 then
1877 Btyp_DDT := Non_Limited_View (Btyp_DDT);
1878 end if;
1880 -- In order to improve the text of error messages, the designated
1881 -- type of access-to-subprogram itypes is set by the semantics as
1882 -- the associated subprogram entity (see sem_attr). Now we replace
1883 -- such node with the proper E_Subprogram_Type itype.
1885 if Id = Attribute_Unrestricted_Access
1886 and then Is_Subprogram (Directly_Designated_Type (Typ))
1887 then
1888 -- The following conditions ensure that this special management
1889 -- is done only for "Address!(Prim'Unrestricted_Access)" nodes.
1890 -- At this stage other cases in which the designated type is
1891 -- still a subprogram (instead of an E_Subprogram_Type) are
1892 -- wrong because the semantics must have overridden the type of
1893 -- the node with the type imposed by the context.
1895 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
1896 and then Etype (Parent (N)) = RTE (RE_Prim_Ptr)
1897 then
1898 Set_Etype (N, RTE (RE_Prim_Ptr));
1900 else
1901 declare
1902 Subp : constant Entity_Id :=
1903 Directly_Designated_Type (Typ);
1904 Etyp : Entity_Id;
1905 Extra : Entity_Id := Empty;
1906 New_Formal : Entity_Id;
1907 Old_Formal : Entity_Id := First_Formal (Subp);
1908 Subp_Typ : Entity_Id;
1910 begin
1911 Subp_Typ := Create_Itype (E_Subprogram_Type, N);
1912 Set_Etype (Subp_Typ, Etype (Subp));
1913 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
1915 if Present (Old_Formal) then
1916 New_Formal := New_Copy (Old_Formal);
1917 Set_First_Entity (Subp_Typ, New_Formal);
1919 loop
1920 Set_Scope (New_Formal, Subp_Typ);
1921 Etyp := Etype (New_Formal);
1923 -- Handle itypes. There is no need to duplicate
1924 -- here the itypes associated with record types
1925 -- (i.e the implicit full view of private types).
1927 if Is_Itype (Etyp)
1928 and then Ekind (Base_Type (Etyp)) /= E_Record_Type
1929 then
1930 Extra := New_Copy (Etyp);
1931 Set_Parent (Extra, New_Formal);
1932 Set_Etype (New_Formal, Extra);
1933 Set_Scope (Extra, Subp_Typ);
1934 end if;
1936 Extra := New_Formal;
1937 Next_Formal (Old_Formal);
1938 exit when No (Old_Formal);
1940 Set_Next_Entity (New_Formal,
1941 New_Copy (Old_Formal));
1942 Next_Entity (New_Formal);
1943 end loop;
1945 Set_Next_Entity (New_Formal, Empty);
1946 Set_Last_Entity (Subp_Typ, Extra);
1947 end if;
1949 -- Now that the explicit formals have been duplicated,
1950 -- any extra formals needed by the subprogram must be
1951 -- created.
1953 if Present (Extra) then
1954 Set_Extra_Formal (Extra, Empty);
1955 end if;
1957 Create_Extra_Formals (Subp_Typ);
1958 Set_Directly_Designated_Type (Typ, Subp_Typ);
1959 end;
1960 end if;
1961 end if;
1963 if Is_Access_Protected_Subprogram_Type (Btyp) then
1964 Expand_Access_To_Protected_Op (N, Pref, Typ);
1966 -- If prefix is a type name, this is a reference to the current
1967 -- instance of the type, within its initialization procedure.
1969 elsif Is_Entity_Name (Pref)
1970 and then Is_Type (Entity (Pref))
1971 then
1972 declare
1973 Par : Node_Id;
1974 Formal : Entity_Id;
1976 begin
1977 -- If the current instance name denotes a task type, then
1978 -- the access attribute is rewritten to be the name of the
1979 -- "_task" parameter associated with the task type's task
1980 -- procedure. An unchecked conversion is applied to ensure
1981 -- a type match in cases of expander-generated calls (e.g.
1982 -- init procs).
1984 if Is_Task_Type (Entity (Pref)) then
1985 Formal :=
1986 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
1987 while Present (Formal) loop
1988 exit when Chars (Formal) = Name_uTask;
1989 Next_Entity (Formal);
1990 end loop;
1992 pragma Assert (Present (Formal));
1994 Rewrite (N,
1995 Unchecked_Convert_To (Typ,
1996 New_Occurrence_Of (Formal, Loc)));
1997 Set_Etype (N, Typ);
1999 elsif Is_Protected_Type (Entity (Pref)) then
2001 -- No action needed for current instance located in a
2002 -- component definition (expansion will occur in the
2003 -- init proc)
2005 if Is_Protected_Type (Current_Scope) then
2006 null;
2008 -- If the current instance reference is located in a
2009 -- protected subprogram or entry then rewrite the access
2010 -- attribute to be the name of the "_object" parameter.
2011 -- An unchecked conversion is applied to ensure a type
2012 -- match in cases of expander-generated calls (e.g. init
2013 -- procs).
2015 -- The code may be nested in a block, so find enclosing
2016 -- scope that is a protected operation.
2018 else
2019 declare
2020 Subp : Entity_Id;
2022 begin
2023 Subp := Current_Scope;
2024 while Ekind_In (Subp, E_Loop, E_Block) loop
2025 Subp := Scope (Subp);
2026 end loop;
2028 Formal :=
2029 First_Entity
2030 (Protected_Body_Subprogram (Subp));
2032 -- For a protected subprogram the _Object parameter
2033 -- is the protected record, so we create an access
2034 -- to it. The _Object parameter of an entry is an
2035 -- address.
2037 if Ekind (Subp) = E_Entry then
2038 Rewrite (N,
2039 Unchecked_Convert_To (Typ,
2040 New_Occurrence_Of (Formal, Loc)));
2041 Set_Etype (N, Typ);
2043 else
2044 Rewrite (N,
2045 Unchecked_Convert_To (Typ,
2046 Make_Attribute_Reference (Loc,
2047 Attribute_Name => Name_Unrestricted_Access,
2048 Prefix =>
2049 New_Occurrence_Of (Formal, Loc))));
2050 Analyze_And_Resolve (N);
2051 end if;
2052 end;
2053 end if;
2055 -- The expression must appear in a default expression,
2056 -- (which in the initialization procedure is the right-hand
2057 -- side of an assignment), and not in a discriminant
2058 -- constraint.
2060 else
2061 Par := Parent (N);
2062 while Present (Par) loop
2063 exit when Nkind (Par) = N_Assignment_Statement;
2065 if Nkind (Par) = N_Component_Declaration then
2066 return;
2067 end if;
2069 Par := Parent (Par);
2070 end loop;
2072 if Present (Par) then
2073 Rewrite (N,
2074 Make_Attribute_Reference (Loc,
2075 Prefix => Make_Identifier (Loc, Name_uInit),
2076 Attribute_Name => Attribute_Name (N)));
2078 Analyze_And_Resolve (N, Typ);
2079 end if;
2080 end if;
2081 end;
2083 -- If the prefix of an Access attribute is a dereference of an
2084 -- access parameter (or a renaming of such a dereference, or a
2085 -- subcomponent of such a dereference) and the context is a
2086 -- general access type (including the type of an object or
2087 -- component with an access_definition, but not the anonymous
2088 -- type of an access parameter or access discriminant), then
2089 -- apply an accessibility check to the access parameter. We used
2090 -- to rewrite the access parameter as a type conversion, but that
2091 -- could only be done if the immediate prefix of the Access
2092 -- attribute was the dereference, and didn't handle cases where
2093 -- the attribute is applied to a subcomponent of the dereference,
2094 -- since there's generally no available, appropriate access type
2095 -- to convert to in that case. The attribute is passed as the
2096 -- point to insert the check, because the access parameter may
2097 -- come from a renaming, possibly in a different scope, and the
2098 -- check must be associated with the attribute itself.
2100 elsif Id = Attribute_Access
2101 and then Nkind (Enc_Object) = N_Explicit_Dereference
2102 and then Is_Entity_Name (Prefix (Enc_Object))
2103 and then (Ekind (Btyp) = E_General_Access_Type
2104 or else Is_Local_Anonymous_Access (Btyp))
2105 and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind
2106 and then Ekind (Etype (Entity (Prefix (Enc_Object))))
2107 = E_Anonymous_Access_Type
2108 and then Present (Extra_Accessibility
2109 (Entity (Prefix (Enc_Object))))
2110 then
2111 Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N);
2113 -- Ada 2005 (AI-251): If the designated type is an interface we
2114 -- add an implicit conversion to force the displacement of the
2115 -- pointer to reference the secondary dispatch table.
2117 elsif Is_Interface (Btyp_DDT)
2118 and then (Comes_From_Source (N)
2119 or else Comes_From_Source (Ref_Object)
2120 or else (Nkind (Ref_Object) in N_Has_Chars
2121 and then Chars (Ref_Object) = Name_uInit))
2122 then
2123 if Nkind (Ref_Object) /= N_Explicit_Dereference then
2125 -- No implicit conversion required if types match, or if
2126 -- the prefix is the class_wide_type of the interface. In
2127 -- either case passing an object of the interface type has
2128 -- already set the pointer correctly.
2130 if Btyp_DDT = Etype (Ref_Object)
2131 or else (Is_Class_Wide_Type (Etype (Ref_Object))
2132 and then
2133 Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object))
2134 then
2135 null;
2137 else
2138 Rewrite (Prefix (N),
2139 Convert_To (Btyp_DDT,
2140 New_Copy_Tree (Prefix (N))));
2142 Analyze_And_Resolve (Prefix (N), Btyp_DDT);
2143 end if;
2145 -- When the object is an explicit dereference, convert the
2146 -- dereference's prefix.
2148 else
2149 declare
2150 Obj_DDT : constant Entity_Id :=
2151 Base_Type
2152 (Directly_Designated_Type
2153 (Etype (Prefix (Ref_Object))));
2154 begin
2155 -- No implicit conversion required if designated types
2156 -- match.
2158 if Obj_DDT /= Btyp_DDT
2159 and then not (Is_Class_Wide_Type (Obj_DDT)
2160 and then Etype (Obj_DDT) = Btyp_DDT)
2161 then
2162 Rewrite (N,
2163 Convert_To (Typ,
2164 New_Copy_Tree (Prefix (Ref_Object))));
2165 Analyze_And_Resolve (N, Typ);
2166 end if;
2167 end;
2168 end if;
2169 end if;
2170 end Access_Cases;
2172 --------------
2173 -- Adjacent --
2174 --------------
2176 -- Transforms 'Adjacent into a call to the floating-point attribute
2177 -- function Adjacent in Fat_xxx (where xxx is the root type)
2179 when Attribute_Adjacent =>
2180 Expand_Fpt_Attribute_RR (N);
2182 -------------
2183 -- Address --
2184 -------------
2186 when Attribute_Address => Address : declare
2187 Task_Proc : Entity_Id;
2189 begin
2190 -- If the prefix is a task or a task type, the useful address is that
2191 -- of the procedure for the task body, i.e. the actual program unit.
2192 -- We replace the original entity with that of the procedure.
2194 if Is_Entity_Name (Pref)
2195 and then Is_Task_Type (Entity (Pref))
2196 then
2197 Task_Proc := Next_Entity (Root_Type (Ptyp));
2199 while Present (Task_Proc) loop
2200 exit when Ekind (Task_Proc) = E_Procedure
2201 and then Etype (First_Formal (Task_Proc)) =
2202 Corresponding_Record_Type (Ptyp);
2203 Next_Entity (Task_Proc);
2204 end loop;
2206 if Present (Task_Proc) then
2207 Set_Entity (Pref, Task_Proc);
2208 Set_Etype (Pref, Etype (Task_Proc));
2209 end if;
2211 -- Similarly, the address of a protected operation is the address
2212 -- of the corresponding protected body, regardless of the protected
2213 -- object from which it is selected.
2215 elsif Nkind (Pref) = N_Selected_Component
2216 and then Is_Subprogram (Entity (Selector_Name (Pref)))
2217 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
2218 then
2219 Rewrite (Pref,
2220 New_Occurrence_Of (
2221 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
2223 elsif Nkind (Pref) = N_Explicit_Dereference
2224 and then Ekind (Ptyp) = E_Subprogram_Type
2225 and then Convention (Ptyp) = Convention_Protected
2226 then
2227 -- The prefix is be a dereference of an access_to_protected_
2228 -- subprogram. The desired address is the second component of
2229 -- the record that represents the access.
2231 declare
2232 Addr : constant Entity_Id := Etype (N);
2233 Ptr : constant Node_Id := Prefix (Pref);
2234 T : constant Entity_Id :=
2235 Equivalent_Type (Base_Type (Etype (Ptr)));
2237 begin
2238 Rewrite (N,
2239 Unchecked_Convert_To (Addr,
2240 Make_Selected_Component (Loc,
2241 Prefix => Unchecked_Convert_To (T, Ptr),
2242 Selector_Name => New_Occurrence_Of (
2243 Next_Entity (First_Entity (T)), Loc))));
2245 Analyze_And_Resolve (N, Addr);
2246 end;
2248 -- Ada 2005 (AI-251): Class-wide interface objects are always
2249 -- "displaced" to reference the tag associated with the interface
2250 -- type. In order to obtain the real address of such objects we
2251 -- generate a call to a run-time subprogram that returns the base
2252 -- address of the object.
2254 -- This processing is not needed in the VM case, where dispatching
2255 -- issues are taken care of by the virtual machine.
2257 elsif Is_Class_Wide_Type (Ptyp)
2258 and then Is_Interface (Underlying_Type (Ptyp))
2259 and then Tagged_Type_Expansion
2260 and then not (Nkind (Pref) in N_Has_Entity
2261 and then Is_Subprogram (Entity (Pref)))
2262 then
2263 Rewrite (N,
2264 Make_Function_Call (Loc,
2265 Name => New_Occurrence_Of (RTE (RE_Base_Address), Loc),
2266 Parameter_Associations => New_List (
2267 Relocate_Node (N))));
2268 Analyze (N);
2269 return;
2270 end if;
2272 -- Deal with packed array reference, other cases are handled by
2273 -- the back end.
2275 if Involves_Packed_Array_Reference (Pref) then
2276 Expand_Packed_Address_Reference (N);
2277 end if;
2278 end Address;
2280 ---------------
2281 -- Alignment --
2282 ---------------
2284 when Attribute_Alignment => Alignment : declare
2285 New_Node : Node_Id;
2287 begin
2288 -- For class-wide types, X'Class'Alignment is transformed into a
2289 -- direct reference to the Alignment of the class type, so that the
2290 -- back end does not have to deal with the X'Class'Alignment
2291 -- reference.
2293 if Is_Entity_Name (Pref)
2294 and then Is_Class_Wide_Type (Entity (Pref))
2295 then
2296 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
2297 return;
2299 -- For x'Alignment applied to an object of a class wide type,
2300 -- transform X'Alignment into a call to the predefined primitive
2301 -- operation _Alignment applied to X.
2303 elsif Is_Class_Wide_Type (Ptyp) then
2304 New_Node :=
2305 Make_Attribute_Reference (Loc,
2306 Prefix => Pref,
2307 Attribute_Name => Name_Tag);
2309 New_Node := Build_Get_Alignment (Loc, New_Node);
2311 -- Case where the context is a specific integer type with which
2312 -- the original attribute was compatible. The function has a
2313 -- specific type as well, so to preserve the compatibility we
2314 -- must convert explicitly.
2316 if Typ /= Standard_Integer then
2317 New_Node := Convert_To (Typ, New_Node);
2318 end if;
2320 Rewrite (N, New_Node);
2321 Analyze_And_Resolve (N, Typ);
2322 return;
2324 -- For all other cases, we just have to deal with the case of
2325 -- the fact that the result can be universal.
2327 else
2328 Apply_Universal_Integer_Attribute_Checks (N);
2329 end if;
2330 end Alignment;
2332 ---------
2333 -- Bit --
2334 ---------
2336 -- We compute this if a packed array reference was present, otherwise we
2337 -- leave the computation up to the back end.
2339 when Attribute_Bit =>
2340 if Involves_Packed_Array_Reference (Pref) then
2341 Expand_Packed_Bit_Reference (N);
2342 else
2343 Apply_Universal_Integer_Attribute_Checks (N);
2344 end if;
2346 ------------------
2347 -- Bit_Position --
2348 ------------------
2350 -- We compute this if a component clause was present, otherwise we leave
2351 -- the computation up to the back end, since we don't know what layout
2352 -- will be chosen.
2354 -- Note that the attribute can apply to a naked record component
2355 -- in generated code (i.e. the prefix is an identifier that
2356 -- references the component or discriminant entity).
2358 when Attribute_Bit_Position => Bit_Position : declare
2359 CE : Entity_Id;
2361 begin
2362 if Nkind (Pref) = N_Identifier then
2363 CE := Entity (Pref);
2364 else
2365 CE := Entity (Selector_Name (Pref));
2366 end if;
2368 if Known_Static_Component_Bit_Offset (CE) then
2369 Rewrite (N,
2370 Make_Integer_Literal (Loc,
2371 Intval => Component_Bit_Offset (CE)));
2372 Analyze_And_Resolve (N, Typ);
2374 else
2375 Apply_Universal_Integer_Attribute_Checks (N);
2376 end if;
2377 end Bit_Position;
2379 ------------------
2380 -- Body_Version --
2381 ------------------
2383 -- A reference to P'Body_Version or P'Version is expanded to
2385 -- Vnn : Unsigned;
2386 -- pragma Import (C, Vnn, "uuuuT");
2387 -- ...
2388 -- Get_Version_String (Vnn)
2390 -- where uuuu is the unit name (dots replaced by double underscore)
2391 -- and T is B for the cases of Body_Version, or Version applied to a
2392 -- subprogram acting as its own spec, and S for Version applied to a
2393 -- subprogram spec or package. This sequence of code references the
2394 -- unsigned constant created in the main program by the binder.
2396 -- A special exception occurs for Standard, where the string returned
2397 -- is a copy of the library string in gnatvsn.ads.
2399 when Attribute_Body_Version
2400 | Attribute_Version
2402 Version : declare
2403 E : constant Entity_Id := Make_Temporary (Loc, 'V');
2404 Pent : Entity_Id;
2405 S : String_Id;
2407 begin
2408 -- If not library unit, get to containing library unit
2410 Pent := Entity (Pref);
2411 while Pent /= Standard_Standard
2412 and then Scope (Pent) /= Standard_Standard
2413 and then not Is_Child_Unit (Pent)
2414 loop
2415 Pent := Scope (Pent);
2416 end loop;
2418 -- Special case Standard and Standard.ASCII
2420 if Pent = Standard_Standard or else Pent = Standard_ASCII then
2421 Rewrite (N,
2422 Make_String_Literal (Loc,
2423 Strval => Verbose_Library_Version));
2425 -- All other cases
2427 else
2428 -- Build required string constant
2430 Get_Name_String (Get_Unit_Name (Pent));
2432 Start_String;
2433 for J in 1 .. Name_Len - 2 loop
2434 if Name_Buffer (J) = '.' then
2435 Store_String_Chars ("__");
2436 else
2437 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
2438 end if;
2439 end loop;
2441 -- Case of subprogram acting as its own spec, always use body
2443 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
2444 and then Nkind (Parent (Declaration_Node (Pent))) =
2445 N_Subprogram_Body
2446 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
2447 then
2448 Store_String_Chars ("B");
2450 -- Case of no body present, always use spec
2452 elsif not Unit_Requires_Body (Pent) then
2453 Store_String_Chars ("S");
2455 -- Otherwise use B for Body_Version, S for spec
2457 elsif Id = Attribute_Body_Version then
2458 Store_String_Chars ("B");
2459 else
2460 Store_String_Chars ("S");
2461 end if;
2463 S := End_String;
2464 Lib.Version_Referenced (S);
2466 -- Insert the object declaration
2468 Insert_Actions (N, New_List (
2469 Make_Object_Declaration (Loc,
2470 Defining_Identifier => E,
2471 Object_Definition =>
2472 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
2474 -- Set entity as imported with correct external name
2476 Set_Is_Imported (E);
2477 Set_Interface_Name (E, Make_String_Literal (Loc, S));
2479 -- Set entity as internal to ensure proper Sprint output of its
2480 -- implicit importation.
2482 Set_Is_Internal (E);
2484 -- And now rewrite original reference
2486 Rewrite (N,
2487 Make_Function_Call (Loc,
2488 Name =>
2489 New_Occurrence_Of (RTE (RE_Get_Version_String), Loc),
2490 Parameter_Associations => New_List (
2491 New_Occurrence_Of (E, Loc))));
2492 end if;
2494 Analyze_And_Resolve (N, RTE (RE_Version_String));
2495 end Version;
2497 -------------
2498 -- Ceiling --
2499 -------------
2501 -- Transforms 'Ceiling into a call to the floating-point attribute
2502 -- function Ceiling in Fat_xxx (where xxx is the root type)
2504 when Attribute_Ceiling =>
2505 Expand_Fpt_Attribute_R (N);
2507 --------------
2508 -- Callable --
2509 --------------
2511 -- Transforms 'Callable attribute into a call to the Callable function
2513 when Attribute_Callable =>
2515 -- We have an object of a task interface class-wide type as a prefix
2516 -- to Callable. Generate:
2517 -- callable (Task_Id (Pref._disp_get_task_id));
2519 if Ada_Version >= Ada_2005
2520 and then Ekind (Ptyp) = E_Class_Wide_Type
2521 and then Is_Interface (Ptyp)
2522 and then Is_Task_Interface (Ptyp)
2523 then
2524 Rewrite (N,
2525 Make_Function_Call (Loc,
2526 Name =>
2527 New_Occurrence_Of (RTE (RE_Callable), Loc),
2528 Parameter_Associations => New_List (
2529 Make_Unchecked_Type_Conversion (Loc,
2530 Subtype_Mark =>
2531 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
2532 Expression => Build_Disp_Get_Task_Id_Call (Pref)))));
2534 else
2535 Rewrite (N, Build_Call_With_Task (Pref, RTE (RE_Callable)));
2536 end if;
2538 Analyze_And_Resolve (N, Standard_Boolean);
2540 ------------
2541 -- Caller --
2542 ------------
2544 -- Transforms 'Caller attribute into a call to either the
2545 -- Task_Entry_Caller or the Protected_Entry_Caller function.
2547 when Attribute_Caller => Caller : declare
2548 Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id);
2549 Ent : constant Entity_Id := Entity (Pref);
2550 Conctype : constant Entity_Id := Scope (Ent);
2551 Nest_Depth : Integer := 0;
2552 Name : Node_Id;
2553 S : Entity_Id;
2555 begin
2556 -- Protected case
2558 if Is_Protected_Type (Conctype) then
2559 case Corresponding_Runtime_Package (Conctype) is
2560 when System_Tasking_Protected_Objects_Entries =>
2561 Name :=
2562 New_Occurrence_Of
2563 (RTE (RE_Protected_Entry_Caller), Loc);
2565 when System_Tasking_Protected_Objects_Single_Entry =>
2566 Name :=
2567 New_Occurrence_Of
2568 (RTE (RE_Protected_Single_Entry_Caller), Loc);
2570 when others =>
2571 raise Program_Error;
2572 end case;
2574 Rewrite (N,
2575 Unchecked_Convert_To (Id_Kind,
2576 Make_Function_Call (Loc,
2577 Name => Name,
2578 Parameter_Associations => New_List (
2579 New_Occurrence_Of
2580 (Find_Protection_Object (Current_Scope), Loc)))));
2582 -- Task case
2584 else
2585 -- Determine the nesting depth of the E'Caller attribute, that
2586 -- is, how many accept statements are nested within the accept
2587 -- statement for E at the point of E'Caller. The runtime uses
2588 -- this depth to find the specified entry call.
2590 for J in reverse 0 .. Scope_Stack.Last loop
2591 S := Scope_Stack.Table (J).Entity;
2593 -- We should not reach the scope of the entry, as it should
2594 -- already have been checked in Sem_Attr that this attribute
2595 -- reference is within a matching accept statement.
2597 pragma Assert (S /= Conctype);
2599 if S = Ent then
2600 exit;
2602 elsif Is_Entry (S) then
2603 Nest_Depth := Nest_Depth + 1;
2604 end if;
2605 end loop;
2607 Rewrite (N,
2608 Unchecked_Convert_To (Id_Kind,
2609 Make_Function_Call (Loc,
2610 Name =>
2611 New_Occurrence_Of (RTE (RE_Task_Entry_Caller), Loc),
2612 Parameter_Associations => New_List (
2613 Make_Integer_Literal (Loc,
2614 Intval => Int (Nest_Depth))))));
2615 end if;
2617 Analyze_And_Resolve (N, Id_Kind);
2618 end Caller;
2620 -------------
2621 -- Compose --
2622 -------------
2624 -- Transforms 'Compose into a call to the floating-point attribute
2625 -- function Compose in Fat_xxx (where xxx is the root type)
2627 -- Note: we strictly should have special code here to deal with the
2628 -- case of absurdly negative arguments (less than Integer'First)
2629 -- which will return a (signed) zero value, but it hardly seems
2630 -- worth the effort. Absurdly large positive arguments will raise
2631 -- constraint error which is fine.
2633 when Attribute_Compose =>
2634 Expand_Fpt_Attribute_RI (N);
2636 -----------------
2637 -- Constrained --
2638 -----------------
2640 when Attribute_Constrained => Constrained : declare
2641 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
2643 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
2644 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
2645 -- view of an aliased object whose subtype is constrained.
2647 ---------------------------------
2648 -- Is_Constrained_Aliased_View --
2649 ---------------------------------
2651 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
2652 E : Entity_Id;
2654 begin
2655 if Is_Entity_Name (Obj) then
2656 E := Entity (Obj);
2658 if Present (Renamed_Object (E)) then
2659 return Is_Constrained_Aliased_View (Renamed_Object (E));
2660 else
2661 return Is_Aliased (E) and then Is_Constrained (Etype (E));
2662 end if;
2664 else
2665 return Is_Aliased_View (Obj)
2666 and then
2667 (Is_Constrained (Etype (Obj))
2668 or else
2669 (Nkind (Obj) = N_Explicit_Dereference
2670 and then
2671 not Object_Type_Has_Constrained_Partial_View
2672 (Typ => Base_Type (Etype (Obj)),
2673 Scop => Current_Scope)));
2674 end if;
2675 end Is_Constrained_Aliased_View;
2677 -- Start of processing for Constrained
2679 begin
2680 -- Reference to a parameter where the value is passed as an extra
2681 -- actual, corresponding to the extra formal referenced by the
2682 -- Extra_Constrained field of the corresponding formal. If this
2683 -- is an entry in-parameter, it is replaced by a constant renaming
2684 -- for which Extra_Constrained is never created.
2686 if Present (Formal_Ent)
2687 and then Ekind (Formal_Ent) /= E_Constant
2688 and then Present (Extra_Constrained (Formal_Ent))
2689 then
2690 Rewrite (N,
2691 New_Occurrence_Of
2692 (Extra_Constrained (Formal_Ent), Sloc (N)));
2694 -- If the prefix is an access to object, the attribute applies to
2695 -- the designated object, so rewrite with an explicit dereference.
2697 elsif Is_Access_Type (Etype (Pref))
2698 and then
2699 (not Is_Entity_Name (Pref) or else Is_Object (Entity (Pref)))
2700 then
2701 Rewrite (Pref,
2702 Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
2703 Analyze_And_Resolve (N, Standard_Boolean);
2704 return;
2706 -- For variables with a Extra_Constrained field, we use the
2707 -- corresponding entity.
2709 elsif Nkind (Pref) = N_Identifier
2710 and then Ekind (Entity (Pref)) = E_Variable
2711 and then Present (Extra_Constrained (Entity (Pref)))
2712 then
2713 Rewrite (N,
2714 New_Occurrence_Of
2715 (Extra_Constrained (Entity (Pref)), Sloc (N)));
2717 -- For all other entity names, we can tell at compile time
2719 elsif Is_Entity_Name (Pref) then
2720 declare
2721 Ent : constant Entity_Id := Entity (Pref);
2722 Res : Boolean;
2724 begin
2725 -- (RM J.4) obsolescent cases
2727 if Is_Type (Ent) then
2729 -- Private type
2731 if Is_Private_Type (Ent) then
2732 Res := not Has_Discriminants (Ent)
2733 or else Is_Constrained (Ent);
2735 -- It not a private type, must be a generic actual type
2736 -- that corresponded to a private type. We know that this
2737 -- correspondence holds, since otherwise the reference
2738 -- within the generic template would have been illegal.
2740 else
2741 if Is_Composite_Type (Underlying_Type (Ent)) then
2742 Res := Is_Constrained (Ent);
2743 else
2744 Res := True;
2745 end if;
2746 end if;
2748 else
2749 -- For access type, apply access check as needed
2751 if Is_Access_Type (Ptyp) then
2752 Apply_Access_Check (N);
2753 end if;
2755 -- If the prefix is not a variable or is aliased, then
2756 -- definitely true; if it's a formal parameter without an
2757 -- associated extra formal, then treat it as constrained.
2759 -- Ada 2005 (AI-363): An aliased prefix must be known to be
2760 -- constrained in order to set the attribute to True.
2762 if not Is_Variable (Pref)
2763 or else Present (Formal_Ent)
2764 or else (Ada_Version < Ada_2005
2765 and then Is_Aliased_View (Pref))
2766 or else (Ada_Version >= Ada_2005
2767 and then Is_Constrained_Aliased_View (Pref))
2768 then
2769 Res := True;
2771 -- Variable case, look at type to see if it is constrained.
2772 -- Note that the one case where this is not accurate (the
2773 -- procedure formal case), has been handled above.
2775 -- We use the Underlying_Type here (and below) in case the
2776 -- type is private without discriminants, but the full type
2777 -- has discriminants. This case is illegal, but we generate
2778 -- it internally for passing to the Extra_Constrained
2779 -- parameter.
2781 else
2782 -- In Ada 2012, test for case of a limited tagged type,
2783 -- in which case the attribute is always required to
2784 -- return True. The underlying type is tested, to make
2785 -- sure we also return True for cases where there is an
2786 -- unconstrained object with an untagged limited partial
2787 -- view which has defaulted discriminants (such objects
2788 -- always produce a False in earlier versions of
2789 -- Ada). (Ada 2012: AI05-0214)
2791 Res :=
2792 Is_Constrained (Underlying_Type (Etype (Ent)))
2793 or else
2794 (Ada_Version >= Ada_2012
2795 and then Is_Tagged_Type (Underlying_Type (Ptyp))
2796 and then Is_Limited_Type (Ptyp));
2797 end if;
2798 end if;
2800 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Res), Loc));
2801 end;
2803 -- Prefix is not an entity name. These are also cases where we can
2804 -- always tell at compile time by looking at the form and type of the
2805 -- prefix. If an explicit dereference of an object with constrained
2806 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
2807 -- underlying type is a limited tagged type, then Constrained is
2808 -- required to always return True (Ada 2012: AI05-0214).
2810 else
2811 Rewrite (N,
2812 New_Occurrence_Of (
2813 Boolean_Literals (
2814 not Is_Variable (Pref)
2815 or else
2816 (Nkind (Pref) = N_Explicit_Dereference
2817 and then
2818 not Object_Type_Has_Constrained_Partial_View
2819 (Typ => Base_Type (Ptyp),
2820 Scop => Current_Scope))
2821 or else Is_Constrained (Underlying_Type (Ptyp))
2822 or else (Ada_Version >= Ada_2012
2823 and then Is_Tagged_Type (Underlying_Type (Ptyp))
2824 and then Is_Limited_Type (Ptyp))),
2825 Loc));
2826 end if;
2828 Analyze_And_Resolve (N, Standard_Boolean);
2829 end Constrained;
2831 ---------------
2832 -- Copy_Sign --
2833 ---------------
2835 -- Transforms 'Copy_Sign into a call to the floating-point attribute
2836 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
2838 when Attribute_Copy_Sign =>
2839 Expand_Fpt_Attribute_RR (N);
2841 -----------
2842 -- Count --
2843 -----------
2845 -- Transforms 'Count attribute into a call to the Count function
2847 when Attribute_Count => Count : declare
2848 Call : Node_Id;
2849 Conctyp : Entity_Id;
2850 Entnam : Node_Id;
2851 Entry_Id : Entity_Id;
2852 Index : Node_Id;
2853 Name : Node_Id;
2855 begin
2856 -- If the prefix is a member of an entry family, retrieve both
2857 -- entry name and index. For a simple entry there is no index.
2859 if Nkind (Pref) = N_Indexed_Component then
2860 Entnam := Prefix (Pref);
2861 Index := First (Expressions (Pref));
2862 else
2863 Entnam := Pref;
2864 Index := Empty;
2865 end if;
2867 Entry_Id := Entity (Entnam);
2869 -- Find the concurrent type in which this attribute is referenced
2870 -- (there had better be one).
2872 Conctyp := Current_Scope;
2873 while not Is_Concurrent_Type (Conctyp) loop
2874 Conctyp := Scope (Conctyp);
2875 end loop;
2877 -- Protected case
2879 if Is_Protected_Type (Conctyp) then
2880 case Corresponding_Runtime_Package (Conctyp) is
2881 when System_Tasking_Protected_Objects_Entries =>
2882 Name := New_Occurrence_Of (RTE (RE_Protected_Count), Loc);
2884 Call :=
2885 Make_Function_Call (Loc,
2886 Name => Name,
2887 Parameter_Associations => New_List (
2888 New_Occurrence_Of
2889 (Find_Protection_Object (Current_Scope), Loc),
2890 Entry_Index_Expression
2891 (Loc, Entry_Id, Index, Scope (Entry_Id))));
2893 when System_Tasking_Protected_Objects_Single_Entry =>
2894 Name :=
2895 New_Occurrence_Of (RTE (RE_Protected_Count_Entry), Loc);
2897 Call :=
2898 Make_Function_Call (Loc,
2899 Name => Name,
2900 Parameter_Associations => New_List (
2901 New_Occurrence_Of
2902 (Find_Protection_Object (Current_Scope), Loc)));
2904 when others =>
2905 raise Program_Error;
2906 end case;
2908 -- Task case
2910 else
2911 Call :=
2912 Make_Function_Call (Loc,
2913 Name => New_Occurrence_Of (RTE (RE_Task_Count), Loc),
2914 Parameter_Associations => New_List (
2915 Entry_Index_Expression (Loc,
2916 Entry_Id, Index, Scope (Entry_Id))));
2917 end if;
2919 -- The call returns type Natural but the context is universal integer
2920 -- so any integer type is allowed. The attribute was already resolved
2921 -- so its Etype is the required result type. If the base type of the
2922 -- context type is other than Standard.Integer we put in a conversion
2923 -- to the required type. This can be a normal typed conversion since
2924 -- both input and output types of the conversion are integer types
2926 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
2927 Rewrite (N, Convert_To (Typ, Call));
2928 else
2929 Rewrite (N, Call);
2930 end if;
2932 Analyze_And_Resolve (N, Typ);
2933 end Count;
2935 ---------------------
2936 -- Descriptor_Size --
2937 ---------------------
2939 when Attribute_Descriptor_Size =>
2941 -- Attribute Descriptor_Size is handled by the back end when applied
2942 -- to an unconstrained array type.
2944 if Is_Array_Type (Ptyp)
2945 and then not Is_Constrained (Ptyp)
2946 then
2947 Apply_Universal_Integer_Attribute_Checks (N);
2949 -- For any other type, the descriptor size is 0 because there is no
2950 -- actual descriptor, but the result is not formally static.
2952 else
2953 Rewrite (N, Make_Integer_Literal (Loc, 0));
2954 Analyze (N);
2955 Set_Is_Static_Expression (N, False);
2956 end if;
2958 ---------------
2959 -- Elab_Body --
2960 ---------------
2962 -- This processing is shared by Elab_Spec
2964 -- What we do is to insert the following declarations
2966 -- procedure tnn;
2967 -- pragma Import (C, enn, "name___elabb/s");
2969 -- and then the Elab_Body/Spec attribute is replaced by a reference
2970 -- to this defining identifier.
2972 when Attribute_Elab_Body
2973 | Attribute_Elab_Spec
2975 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
2976 -- back-end knows how to handle these attributes directly.
2978 if CodePeer_Mode then
2979 return;
2980 end if;
2982 Elab_Body : declare
2983 Ent : constant Entity_Id := Make_Temporary (Loc, 'E');
2984 Str : String_Id;
2985 Lang : Node_Id;
2987 procedure Make_Elab_String (Nod : Node_Id);
2988 -- Given Nod, an identifier, or a selected component, put the
2989 -- image into the current string literal, with double underline
2990 -- between components.
2992 ----------------------
2993 -- Make_Elab_String --
2994 ----------------------
2996 procedure Make_Elab_String (Nod : Node_Id) is
2997 begin
2998 if Nkind (Nod) = N_Selected_Component then
2999 Make_Elab_String (Prefix (Nod));
3000 Store_String_Char ('_');
3001 Store_String_Char ('_');
3002 Get_Name_String (Chars (Selector_Name (Nod)));
3004 else
3005 pragma Assert (Nkind (Nod) = N_Identifier);
3006 Get_Name_String (Chars (Nod));
3007 end if;
3009 Store_String_Chars (Name_Buffer (1 .. Name_Len));
3010 end Make_Elab_String;
3012 -- Start of processing for Elab_Body/Elab_Spec
3014 begin
3015 -- First we need to prepare the string literal for the name of
3016 -- the elaboration routine to be referenced.
3018 Start_String;
3019 Make_Elab_String (Pref);
3020 Store_String_Chars ("___elab");
3021 Lang := Make_Identifier (Loc, Name_C);
3023 if Id = Attribute_Elab_Body then
3024 Store_String_Char ('b');
3025 else
3026 Store_String_Char ('s');
3027 end if;
3029 Str := End_String;
3031 Insert_Actions (N, New_List (
3032 Make_Subprogram_Declaration (Loc,
3033 Specification =>
3034 Make_Procedure_Specification (Loc,
3035 Defining_Unit_Name => Ent)),
3037 Make_Pragma (Loc,
3038 Chars => Name_Import,
3039 Pragma_Argument_Associations => New_List (
3040 Make_Pragma_Argument_Association (Loc, Expression => Lang),
3042 Make_Pragma_Argument_Association (Loc,
3043 Expression => Make_Identifier (Loc, Chars (Ent))),
3045 Make_Pragma_Argument_Association (Loc,
3046 Expression => Make_String_Literal (Loc, Str))))));
3048 Set_Entity (N, Ent);
3049 Rewrite (N, New_Occurrence_Of (Ent, Loc));
3050 end Elab_Body;
3052 --------------------
3053 -- Elab_Subp_Body --
3054 --------------------
3056 -- Always ignored. In CodePeer mode, gnat2scil knows how to handle
3057 -- this attribute directly, and if we are not in CodePeer mode it is
3058 -- entirely ignored ???
3060 when Attribute_Elab_Subp_Body =>
3061 return;
3063 ----------------
3064 -- Elaborated --
3065 ----------------
3067 -- Elaborated is always True for preelaborated units, predefined units,
3068 -- pure units and units which have Elaborate_Body pragmas. These units
3069 -- have no elaboration entity.
3071 -- Note: The Elaborated attribute is never passed to the back end
3073 when Attribute_Elaborated => Elaborated : declare
3074 Elab_Id : constant Entity_Id := Elaboration_Entity (Entity (Pref));
3076 begin
3077 if Present (Elab_Id) then
3078 Rewrite (N,
3079 Make_Op_Ne (Loc,
3080 Left_Opnd => New_Occurrence_Of (Elab_Id, Loc),
3081 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)));
3083 Analyze_And_Resolve (N, Typ);
3084 else
3085 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3086 end if;
3087 end Elaborated;
3089 --------------
3090 -- Enum_Rep --
3091 --------------
3093 when Attribute_Enum_Rep => Enum_Rep : declare
3094 Expr : Node_Id;
3096 begin
3097 -- Get the expression, which is X for Enum_Type'Enum_Rep (X) or
3098 -- X'Enum_Rep.
3100 if Is_Non_Empty_List (Exprs) then
3101 Expr := First (Exprs);
3102 else
3103 Expr := Pref;
3104 end if;
3106 -- If the expression is an enumeration literal, it is replaced by the
3107 -- literal value.
3109 if Nkind (Expr) in N_Has_Entity
3110 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
3111 then
3112 Rewrite (N,
3113 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Expr))));
3115 -- If this is a renaming of a literal, recover the representation
3116 -- of the original. If it renames an expression there is nothing to
3117 -- fold.
3119 elsif Nkind (Expr) in N_Has_Entity
3120 and then Ekind (Entity (Expr)) = E_Constant
3121 and then Present (Renamed_Object (Entity (Expr)))
3122 and then Is_Entity_Name (Renamed_Object (Entity (Expr)))
3123 and then Ekind (Entity (Renamed_Object (Entity (Expr)))) =
3124 E_Enumeration_Literal
3125 then
3126 Rewrite (N,
3127 Make_Integer_Literal (Loc,
3128 Enumeration_Rep (Entity (Renamed_Object (Entity (Expr))))));
3130 -- If not constant-folded above, Enum_Type'Enum_Rep (X) or
3131 -- X'Enum_Rep expands to
3133 -- target-type (X)
3135 -- This is simply a direct conversion from the enumeration type to
3136 -- the target integer type, which is treated by the back end as a
3137 -- normal integer conversion, treating the enumeration type as an
3138 -- integer, which is exactly what we want. We set Conversion_OK to
3139 -- make sure that the analyzer does not complain about what otherwise
3140 -- might be an illegal conversion.
3142 else
3143 Rewrite (N, OK_Convert_To (Typ, Relocate_Node (Expr)));
3144 end if;
3146 Set_Etype (N, Typ);
3147 Analyze_And_Resolve (N, Typ);
3148 end Enum_Rep;
3150 --------------
3151 -- Enum_Val --
3152 --------------
3154 when Attribute_Enum_Val => Enum_Val : declare
3155 Expr : Node_Id;
3156 Btyp : constant Entity_Id := Base_Type (Ptyp);
3158 begin
3159 -- X'Enum_Val (Y) expands to
3161 -- [constraint_error when _rep_to_pos (Y, False) = -1, msg]
3162 -- X!(Y);
3164 Expr := Unchecked_Convert_To (Ptyp, First (Exprs));
3166 Insert_Action (N,
3167 Make_Raise_Constraint_Error (Loc,
3168 Condition =>
3169 Make_Op_Eq (Loc,
3170 Left_Opnd =>
3171 Make_Function_Call (Loc,
3172 Name =>
3173 New_Occurrence_Of (TSS (Btyp, TSS_Rep_To_Pos), Loc),
3174 Parameter_Associations => New_List (
3175 Relocate_Node (Duplicate_Subexpr (Expr)),
3176 New_Occurrence_Of (Standard_False, Loc))),
3178 Right_Opnd => Make_Integer_Literal (Loc, -1)),
3179 Reason => CE_Range_Check_Failed));
3181 Rewrite (N, Expr);
3182 Analyze_And_Resolve (N, Ptyp);
3183 end Enum_Val;
3185 --------------
3186 -- Exponent --
3187 --------------
3189 -- Transforms 'Exponent into a call to the floating-point attribute
3190 -- function Exponent in Fat_xxx (where xxx is the root type)
3192 when Attribute_Exponent =>
3193 Expand_Fpt_Attribute_R (N);
3195 ------------------
3196 -- External_Tag --
3197 ------------------
3199 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
3201 when Attribute_External_Tag =>
3202 Rewrite (N,
3203 Make_Function_Call (Loc,
3204 Name =>
3205 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
3206 Parameter_Associations => New_List (
3207 Make_Attribute_Reference (Loc,
3208 Attribute_Name => Name_Tag,
3209 Prefix => Prefix (N)))));
3211 Analyze_And_Resolve (N, Standard_String);
3213 -----------------------
3214 -- Finalization_Size --
3215 -----------------------
3217 when Attribute_Finalization_Size => Finalization_Size : declare
3218 function Calculate_Header_Size return Node_Id;
3219 -- Generate a runtime call to calculate the size of the hidden header
3220 -- along with any added padding which would precede a heap-allocated
3221 -- object of the prefix type.
3223 ---------------------------
3224 -- Calculate_Header_Size --
3225 ---------------------------
3227 function Calculate_Header_Size return Node_Id is
3228 begin
3229 -- Generate:
3230 -- Universal_Integer
3231 -- (Header_Size_With_Padding (Pref'Alignment))
3233 return
3234 Convert_To (Universal_Integer,
3235 Make_Function_Call (Loc,
3236 Name =>
3237 New_Occurrence_Of (RTE (RE_Header_Size_With_Padding), Loc),
3239 Parameter_Associations => New_List (
3240 Make_Attribute_Reference (Loc,
3241 Prefix => New_Copy_Tree (Pref),
3242 Attribute_Name => Name_Alignment))));
3243 end Calculate_Header_Size;
3245 -- Local variables
3247 Size : Entity_Id;
3249 -- Start of Finalization_Size
3251 begin
3252 -- An object of a class-wide type first requires a runtime check to
3253 -- determine whether it is actually controlled or not. Depending on
3254 -- the outcome of this check, the Finalization_Size of the object
3255 -- may be zero or some positive value.
3257 -- In this scenario, Pref'Finalization_Size is expanded into
3259 -- Size : Integer := 0;
3261 -- if Needs_Finalization (Pref'Tag) then
3262 -- Size :=
3263 -- Universal_Integer
3264 -- (Header_Size_With_Padding (Pref'Alignment));
3265 -- end if;
3267 -- and the attribute reference is replaced with a reference to Size.
3269 if Is_Class_Wide_Type (Ptyp) then
3270 Size := Make_Temporary (Loc, 'S');
3272 Insert_Actions (N, New_List (
3274 -- Generate:
3275 -- Size : Integer := 0;
3277 Make_Object_Declaration (Loc,
3278 Defining_Identifier => Size,
3279 Object_Definition =>
3280 New_Occurrence_Of (Standard_Integer, Loc),
3281 Expression => Make_Integer_Literal (Loc, 0)),
3283 -- Generate:
3284 -- if Needs_Finalization (Pref'Tag) then
3285 -- Size :=
3286 -- Universal_Integer
3287 -- (Header_Size_With_Padding (Pref'Alignment));
3288 -- end if;
3290 Make_If_Statement (Loc,
3291 Condition =>
3292 Make_Function_Call (Loc,
3293 Name =>
3294 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
3296 Parameter_Associations => New_List (
3297 Make_Attribute_Reference (Loc,
3298 Prefix => New_Copy_Tree (Pref),
3299 Attribute_Name => Name_Tag))),
3301 Then_Statements => New_List (
3302 Make_Assignment_Statement (Loc,
3303 Name => New_Occurrence_Of (Size, Loc),
3304 Expression => Calculate_Header_Size)))));
3306 Rewrite (N, New_Occurrence_Of (Size, Loc));
3308 -- The prefix is known to be controlled at compile time. Calculate
3309 -- Finalization_Size by calling function Header_Size_With_Padding.
3311 elsif Needs_Finalization (Ptyp) then
3312 Rewrite (N, Calculate_Header_Size);
3314 -- The prefix is not an object with controlled parts, so its
3315 -- Finalization_Size is zero.
3317 else
3318 Rewrite (N, Make_Integer_Literal (Loc, 0));
3319 end if;
3321 -- Due to cases where the entity type of the attribute is already
3322 -- resolved the rewritten N must get re-resolved to its appropriate
3323 -- type.
3325 Analyze_And_Resolve (N, Typ);
3326 end Finalization_Size;
3328 -----------
3329 -- First --
3330 -----------
3332 when Attribute_First =>
3334 -- If the prefix type is a constrained packed array type which
3335 -- already has a Packed_Array_Impl_Type representation defined, then
3336 -- replace this attribute with a direct reference to 'First of the
3337 -- appropriate index subtype (since otherwise the back end will try
3338 -- to give us the value of 'First for this implementation type).
3340 if Is_Constrained_Packed_Array (Ptyp) then
3341 Rewrite (N,
3342 Make_Attribute_Reference (Loc,
3343 Attribute_Name => Name_First,
3344 Prefix =>
3345 New_Occurrence_Of (Get_Index_Subtype (N), Loc)));
3346 Analyze_And_Resolve (N, Typ);
3348 -- For access type, apply access check as needed
3350 elsif Is_Access_Type (Ptyp) then
3351 Apply_Access_Check (N);
3353 -- For scalar type, if low bound is a reference to an entity, just
3354 -- replace with a direct reference. Note that we can only have a
3355 -- reference to a constant entity at this stage, anything else would
3356 -- have already been rewritten.
3358 elsif Is_Scalar_Type (Ptyp) then
3359 declare
3360 Lo : constant Node_Id := Type_Low_Bound (Ptyp);
3361 begin
3362 if Is_Entity_Name (Lo) then
3363 Rewrite (N, New_Occurrence_Of (Entity (Lo), Loc));
3364 end if;
3365 end;
3366 end if;
3368 ---------------
3369 -- First_Bit --
3370 ---------------
3372 -- Compute this if component clause was present, otherwise we leave the
3373 -- computation to be completed in the back-end, since we don't know what
3374 -- layout will be chosen.
3376 when Attribute_First_Bit => First_Bit_Attr : declare
3377 CE : constant Entity_Id := Entity (Selector_Name (Pref));
3379 begin
3380 -- In Ada 2005 (or later) if we have the non-default bit order, then
3381 -- we return the original value as given in the component clause
3382 -- (RM 2005 13.5.2(3/2)).
3384 if Present (Component_Clause (CE))
3385 and then Ada_Version >= Ada_2005
3386 and then Reverse_Bit_Order (Scope (CE))
3387 then
3388 Rewrite (N,
3389 Make_Integer_Literal (Loc,
3390 Intval => Expr_Value (First_Bit (Component_Clause (CE)))));
3391 Analyze_And_Resolve (N, Typ);
3393 -- Otherwise (Ada 83/95 or Ada 2005 or later with default bit order),
3394 -- rewrite with normalized value if we know it statically.
3396 elsif Known_Static_Component_Bit_Offset (CE) then
3397 Rewrite (N,
3398 Make_Integer_Literal (Loc,
3399 Component_Bit_Offset (CE) mod System_Storage_Unit));
3400 Analyze_And_Resolve (N, Typ);
3402 -- Otherwise left to back end, just do universal integer checks
3404 else
3405 Apply_Universal_Integer_Attribute_Checks (N);
3406 end if;
3407 end First_Bit_Attr;
3409 --------------------------------
3410 -- Fixed_Value, Integer_Value --
3411 --------------------------------
3413 -- We transform
3415 -- fixtype'Fixed_Value (integer-value)
3416 -- inttype'Fixed_Value (fixed-value)
3418 -- into
3420 -- fixtype (integer-value)
3421 -- inttype (fixed-value)
3423 -- respectively.
3425 -- We do all the required analysis of the conversion here, because we do
3426 -- not want this to go through the fixed-point conversion circuits. Note
3427 -- that the back end always treats fixed-point as equivalent to the
3428 -- corresponding integer type anyway.
3430 when Attribute_Fixed_Value
3431 | Attribute_Integer_Value
3433 Rewrite (N,
3434 Make_Type_Conversion (Loc,
3435 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
3436 Expression => Relocate_Node (First (Exprs))));
3437 Set_Etype (N, Entity (Pref));
3438 Set_Analyzed (N);
3440 -- Note: it might appear that a properly analyzed unchecked
3441 -- conversion would be just fine here, but that's not the case,
3442 -- since the full range checks performed by the following call
3443 -- are critical.
3445 Apply_Type_Conversion_Checks (N);
3447 -----------
3448 -- Floor --
3449 -----------
3451 -- Transforms 'Floor into a call to the floating-point attribute
3452 -- function Floor in Fat_xxx (where xxx is the root type)
3454 when Attribute_Floor =>
3455 Expand_Fpt_Attribute_R (N);
3457 ----------
3458 -- Fore --
3459 ----------
3461 -- For the fixed-point type Typ:
3463 -- Typ'Fore
3465 -- expands into
3467 -- Result_Type (System.Fore (Universal_Real (Type'First)),
3468 -- Universal_Real (Type'Last))
3470 -- Note that we know that the type is a non-static subtype, or Fore
3471 -- would have itself been computed dynamically in Eval_Attribute.
3473 when Attribute_Fore =>
3474 Rewrite (N,
3475 Convert_To (Typ,
3476 Make_Function_Call (Loc,
3477 Name =>
3478 New_Occurrence_Of (RTE (RE_Fore), Loc),
3480 Parameter_Associations => New_List (
3481 Convert_To (Universal_Real,
3482 Make_Attribute_Reference (Loc,
3483 Prefix => New_Occurrence_Of (Ptyp, Loc),
3484 Attribute_Name => Name_First)),
3486 Convert_To (Universal_Real,
3487 Make_Attribute_Reference (Loc,
3488 Prefix => New_Occurrence_Of (Ptyp, Loc),
3489 Attribute_Name => Name_Last))))));
3491 Analyze_And_Resolve (N, Typ);
3493 --------------
3494 -- Fraction --
3495 --------------
3497 -- Transforms 'Fraction into a call to the floating-point attribute
3498 -- function Fraction in Fat_xxx (where xxx is the root type)
3500 when Attribute_Fraction =>
3501 Expand_Fpt_Attribute_R (N);
3503 --------------
3504 -- From_Any --
3505 --------------
3507 when Attribute_From_Any => From_Any : declare
3508 P_Type : constant Entity_Id := Etype (Pref);
3509 Decls : constant List_Id := New_List;
3511 begin
3512 Rewrite (N,
3513 Build_From_Any_Call (P_Type,
3514 Relocate_Node (First (Exprs)),
3515 Decls));
3516 Insert_Actions (N, Decls);
3517 Analyze_And_Resolve (N, P_Type);
3518 end From_Any;
3520 ----------------------
3521 -- Has_Same_Storage --
3522 ----------------------
3524 when Attribute_Has_Same_Storage => Has_Same_Storage : declare
3525 Loc : constant Source_Ptr := Sloc (N);
3527 X : constant Node_Id := Prefix (N);
3528 Y : constant Node_Id := First (Expressions (N));
3529 -- The arguments
3531 X_Addr : Node_Id;
3532 Y_Addr : Node_Id;
3533 -- Rhe expressions for their addresses
3535 X_Size : Node_Id;
3536 Y_Size : Node_Id;
3537 -- Rhe expressions for their sizes
3539 begin
3540 -- The attribute is expanded as:
3542 -- (X'address = Y'address)
3543 -- and then (X'Size = Y'Size)
3545 -- If both arguments have the same Etype the second conjunct can be
3546 -- omitted.
3548 X_Addr :=
3549 Make_Attribute_Reference (Loc,
3550 Attribute_Name => Name_Address,
3551 Prefix => New_Copy_Tree (X));
3553 Y_Addr :=
3554 Make_Attribute_Reference (Loc,
3555 Attribute_Name => Name_Address,
3556 Prefix => New_Copy_Tree (Y));
3558 X_Size :=
3559 Make_Attribute_Reference (Loc,
3560 Attribute_Name => Name_Size,
3561 Prefix => New_Copy_Tree (X));
3563 Y_Size :=
3564 Make_Attribute_Reference (Loc,
3565 Attribute_Name => Name_Size,
3566 Prefix => New_Copy_Tree (Y));
3568 if Etype (X) = Etype (Y) then
3569 Rewrite (N,
3570 Make_Op_Eq (Loc,
3571 Left_Opnd => X_Addr,
3572 Right_Opnd => Y_Addr));
3573 else
3574 Rewrite (N,
3575 Make_Op_And (Loc,
3576 Left_Opnd =>
3577 Make_Op_Eq (Loc,
3578 Left_Opnd => X_Addr,
3579 Right_Opnd => Y_Addr),
3580 Right_Opnd =>
3581 Make_Op_Eq (Loc,
3582 Left_Opnd => X_Size,
3583 Right_Opnd => Y_Size)));
3584 end if;
3586 Analyze_And_Resolve (N, Standard_Boolean);
3587 end Has_Same_Storage;
3589 --------------
3590 -- Identity --
3591 --------------
3593 -- For an exception returns a reference to the exception data:
3594 -- Exception_Id!(Prefix'Reference)
3596 -- For a task it returns a reference to the _task_id component of
3597 -- corresponding record:
3599 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
3601 -- in Ada.Task_Identification
3603 when Attribute_Identity => Identity : declare
3604 Id_Kind : Entity_Id;
3606 begin
3607 if Ptyp = Standard_Exception_Type then
3608 Id_Kind := RTE (RE_Exception_Id);
3610 if Present (Renamed_Object (Entity (Pref))) then
3611 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
3612 end if;
3614 Rewrite (N,
3615 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
3616 else
3617 Id_Kind := RTE (RO_AT_Task_Id);
3619 -- If the prefix is a task interface, the Task_Id is obtained
3620 -- dynamically through a dispatching call, as for other task
3621 -- attributes applied to interfaces.
3623 if Ada_Version >= Ada_2005
3624 and then Ekind (Ptyp) = E_Class_Wide_Type
3625 and then Is_Interface (Ptyp)
3626 and then Is_Task_Interface (Ptyp)
3627 then
3628 Rewrite (N,
3629 Unchecked_Convert_To
3630 (Id_Kind, Build_Disp_Get_Task_Id_Call (Pref)));
3632 else
3633 Rewrite (N,
3634 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
3635 end if;
3636 end if;
3638 Analyze_And_Resolve (N, Id_Kind);
3639 end Identity;
3641 -----------
3642 -- Image --
3643 -----------
3645 -- Image attribute is handled in separate unit Exp_Imgv
3647 when Attribute_Image =>
3649 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
3650 -- back-end knows how to handle this attribute directly.
3652 if CodePeer_Mode then
3653 return;
3654 end if;
3656 Expand_Image_Attribute (N);
3658 ---------
3659 -- Img --
3660 ---------
3662 -- X'Img is expanded to typ'Image (X), where typ is the type of X
3664 when Attribute_Img =>
3665 Expand_Image_Attribute (N);
3667 -----------
3668 -- Input --
3669 -----------
3671 when Attribute_Input => Input : declare
3672 P_Type : constant Entity_Id := Entity (Pref);
3673 B_Type : constant Entity_Id := Base_Type (P_Type);
3674 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3675 Strm : constant Node_Id := First (Exprs);
3676 Fname : Entity_Id;
3677 Decl : Node_Id;
3678 Call : Node_Id;
3679 Prag : Node_Id;
3680 Arg2 : Node_Id;
3681 Rfunc : Node_Id;
3683 Cntrl : Node_Id := Empty;
3684 -- Value for controlling argument in call. Always Empty except in
3685 -- the dispatching (class-wide type) case, where it is a reference
3686 -- to the dummy object initialized to the right internal tag.
3688 procedure Freeze_Stream_Subprogram (F : Entity_Id);
3689 -- The expansion of the attribute reference may generate a call to
3690 -- a user-defined stream subprogram that is frozen by the call. This
3691 -- can lead to access-before-elaboration problem if the reference
3692 -- appears in an object declaration and the subprogram body has not
3693 -- been seen. The freezing of the subprogram requires special code
3694 -- because it appears in an expanded context where expressions do
3695 -- not freeze their constituents.
3697 ------------------------------
3698 -- Freeze_Stream_Subprogram --
3699 ------------------------------
3701 procedure Freeze_Stream_Subprogram (F : Entity_Id) is
3702 Decl : constant Node_Id := Unit_Declaration_Node (F);
3703 Bod : Node_Id;
3705 begin
3706 -- If this is user-defined subprogram, the corresponding
3707 -- stream function appears as a renaming-as-body, and the
3708 -- user subprogram must be retrieved by tree traversal.
3710 if Present (Decl)
3711 and then Nkind (Decl) = N_Subprogram_Declaration
3712 and then Present (Corresponding_Body (Decl))
3713 then
3714 Bod := Corresponding_Body (Decl);
3716 if Nkind (Unit_Declaration_Node (Bod)) =
3717 N_Subprogram_Renaming_Declaration
3718 then
3719 Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
3720 end if;
3721 end if;
3722 end Freeze_Stream_Subprogram;
3724 -- Start of processing for Input
3726 begin
3727 -- If no underlying type, we have an error that will be diagnosed
3728 -- elsewhere, so here we just completely ignore the expansion.
3730 if No (U_Type) then
3731 return;
3732 end if;
3734 -- Stream operations can appear in user code even if the restriction
3735 -- No_Streams is active (for example, when instantiating a predefined
3736 -- container). In that case rewrite the attribute as a Raise to
3737 -- prevent any run-time use.
3739 if Restriction_Active (No_Streams) then
3740 Rewrite (N,
3741 Make_Raise_Program_Error (Sloc (N),
3742 Reason => PE_Stream_Operation_Not_Allowed));
3743 Set_Etype (N, B_Type);
3744 return;
3745 end if;
3747 -- If there is a TSS for Input, just call it
3749 Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
3751 if Present (Fname) then
3752 null;
3754 else
3755 -- If there is a Stream_Convert pragma, use it, we rewrite
3757 -- sourcetyp'Input (stream)
3759 -- as
3761 -- sourcetyp (streamread (strmtyp'Input (stream)));
3763 -- where streamread is the given Read function that converts an
3764 -- argument of type strmtyp to type sourcetyp or a type from which
3765 -- it is derived (extra conversion required for the derived case).
3767 Prag := Get_Stream_Convert_Pragma (P_Type);
3769 if Present (Prag) then
3770 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
3771 Rfunc := Entity (Expression (Arg2));
3773 Rewrite (N,
3774 Convert_To (B_Type,
3775 Make_Function_Call (Loc,
3776 Name => New_Occurrence_Of (Rfunc, Loc),
3777 Parameter_Associations => New_List (
3778 Make_Attribute_Reference (Loc,
3779 Prefix =>
3780 New_Occurrence_Of
3781 (Etype (First_Formal (Rfunc)), Loc),
3782 Attribute_Name => Name_Input,
3783 Expressions => Exprs)))));
3785 Analyze_And_Resolve (N, B_Type);
3786 return;
3788 -- Elementary types
3790 elsif Is_Elementary_Type (U_Type) then
3792 -- A special case arises if we have a defined _Read routine,
3793 -- since in this case we are required to call this routine.
3795 declare
3796 Typ : Entity_Id := P_Type;
3797 begin
3798 if Present (Full_View (Typ)) then
3799 Typ := Full_View (Typ);
3800 end if;
3802 if Present (TSS (Base_Type (Typ), TSS_Stream_Read)) then
3803 Build_Record_Or_Elementary_Input_Function
3804 (Loc, Typ, Decl, Fname, Use_Underlying => False);
3805 Insert_Action (N, Decl);
3807 -- For normal cases, we call the I_xxx routine directly
3809 else
3810 Rewrite (N, Build_Elementary_Input_Call (N));
3811 Analyze_And_Resolve (N, P_Type);
3812 return;
3813 end if;
3814 end;
3816 -- Array type case
3818 elsif Is_Array_Type (U_Type) then
3819 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
3820 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3822 -- Dispatching case with class-wide type
3824 elsif Is_Class_Wide_Type (P_Type) then
3826 -- No need to do anything else compiling under restriction
3827 -- No_Dispatching_Calls. During the semantic analysis we
3828 -- already notified such violation.
3830 if Restriction_Active (No_Dispatching_Calls) then
3831 return;
3832 end if;
3834 declare
3835 Rtyp : constant Entity_Id := Root_Type (P_Type);
3836 Expr : Node_Id;
3838 begin
3839 -- Read the internal tag (RM 13.13.2(34)) and use it to
3840 -- initialize a dummy tag value. We used to generate:
3842 -- Descendant_Tag (String'Input (Strm), P_Type);
3844 -- which turns into a call to String_Input_Blk_IO. However,
3845 -- if the input is malformed, that could try to read an
3846 -- enormous String, causing chaos. So instead we call
3847 -- String_Input_Tag, which does the same thing as
3848 -- String_Input_Blk_IO, except that if the String is
3849 -- absurdly long, it raises an exception.
3851 -- This value is used only to provide a controlling
3852 -- argument for the eventual _Input call. Descendant_Tag is
3853 -- called rather than Internal_Tag to ensure that we have a
3854 -- tag for a type that is descended from the prefix type and
3855 -- declared at the same accessibility level (the exception
3856 -- Tag_Error will be raised otherwise). The level check is
3857 -- required for Ada 2005 because tagged types can be
3858 -- extended in nested scopes (AI-344).
3860 -- Note: we used to generate an explicit declaration of a
3861 -- constant Ada.Tags.Tag object, and use an occurrence of
3862 -- this constant in Cntrl, but this caused a secondary stack
3863 -- leak.
3865 Expr :=
3866 Make_Function_Call (Loc,
3867 Name =>
3868 New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc),
3869 Parameter_Associations => New_List (
3870 Make_Function_Call (Loc,
3871 Name =>
3872 New_Occurrence_Of
3873 (RTE (RE_String_Input_Tag), Loc),
3874 Parameter_Associations => New_List (
3875 Relocate_Node (Duplicate_Subexpr (Strm)))),
3877 Make_Attribute_Reference (Loc,
3878 Prefix => New_Occurrence_Of (P_Type, Loc),
3879 Attribute_Name => Name_Tag)));
3881 Set_Etype (Expr, RTE (RE_Tag));
3883 -- Now we need to get the entity for the call, and construct
3884 -- a function call node, where we preset a reference to Dnn
3885 -- as the controlling argument (doing an unchecked convert
3886 -- to the class-wide tagged type to make it look like a real
3887 -- tagged object).
3889 Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
3890 Cntrl := Unchecked_Convert_To (P_Type, Expr);
3891 Set_Etype (Cntrl, P_Type);
3892 Set_Parent (Cntrl, N);
3893 end;
3895 -- For tagged types, use the primitive Input function
3897 elsif Is_Tagged_Type (U_Type) then
3898 Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
3900 -- All other record type cases, including protected records. The
3901 -- latter only arise for expander generated code for handling
3902 -- shared passive partition access.
3904 else
3905 pragma Assert
3906 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3908 -- Ada 2005 (AI-216): Program_Error is raised executing default
3909 -- implementation of the Input attribute of an unchecked union
3910 -- type if the type lacks default discriminant values.
3912 if Is_Unchecked_Union (Base_Type (U_Type))
3913 and then No (Discriminant_Constraint (U_Type))
3914 then
3915 Insert_Action (N,
3916 Make_Raise_Program_Error (Loc,
3917 Reason => PE_Unchecked_Union_Restriction));
3919 return;
3920 end if;
3922 -- Build the type's Input function, passing the subtype rather
3923 -- than its base type, because checks are needed in the case of
3924 -- constrained discriminants (see Ada 2012 AI05-0192).
3926 Build_Record_Or_Elementary_Input_Function
3927 (Loc, U_Type, Decl, Fname);
3928 Insert_Action (N, Decl);
3930 if Nkind (Parent (N)) = N_Object_Declaration
3931 and then Is_Record_Type (U_Type)
3932 then
3933 -- The stream function may contain calls to user-defined
3934 -- Read procedures for individual components.
3936 declare
3937 Comp : Entity_Id;
3938 Func : Entity_Id;
3940 begin
3941 Comp := First_Component (U_Type);
3942 while Present (Comp) loop
3943 Func :=
3944 Find_Stream_Subprogram
3945 (Etype (Comp), TSS_Stream_Read);
3947 if Present (Func) then
3948 Freeze_Stream_Subprogram (Func);
3949 end if;
3951 Next_Component (Comp);
3952 end loop;
3953 end;
3954 end if;
3955 end if;
3956 end if;
3958 -- If we fall through, Fname is the function to be called. The result
3959 -- is obtained by calling the appropriate function, then converting
3960 -- the result. The conversion does a subtype check.
3962 Call :=
3963 Make_Function_Call (Loc,
3964 Name => New_Occurrence_Of (Fname, Loc),
3965 Parameter_Associations => New_List (
3966 Relocate_Node (Strm)));
3968 Set_Controlling_Argument (Call, Cntrl);
3969 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
3970 Analyze_And_Resolve (N, P_Type);
3972 if Nkind (Parent (N)) = N_Object_Declaration then
3973 Freeze_Stream_Subprogram (Fname);
3974 end if;
3975 end Input;
3977 -------------------
3978 -- Invalid_Value --
3979 -------------------
3981 when Attribute_Invalid_Value =>
3982 Rewrite (N, Get_Simple_Init_Val (Ptyp, N));
3984 ----------
3985 -- Last --
3986 ----------
3988 when Attribute_Last =>
3990 -- If the prefix type is a constrained packed array type which
3991 -- already has a Packed_Array_Impl_Type representation defined, then
3992 -- replace this attribute with a direct reference to 'Last of the
3993 -- appropriate index subtype (since otherwise the back end will try
3994 -- to give us the value of 'Last for this implementation type).
3996 if Is_Constrained_Packed_Array (Ptyp) then
3997 Rewrite (N,
3998 Make_Attribute_Reference (Loc,
3999 Attribute_Name => Name_Last,
4000 Prefix => New_Occurrence_Of (Get_Index_Subtype (N), Loc)));
4001 Analyze_And_Resolve (N, Typ);
4003 -- For access type, apply access check as needed
4005 elsif Is_Access_Type (Ptyp) then
4006 Apply_Access_Check (N);
4008 -- For scalar type, if low bound is a reference to an entity, just
4009 -- replace with a direct reference. Note that we can only have a
4010 -- reference to a constant entity at this stage, anything else would
4011 -- have already been rewritten.
4013 elsif Is_Scalar_Type (Ptyp) then
4014 declare
4015 Hi : constant Node_Id := Type_High_Bound (Ptyp);
4016 begin
4017 if Is_Entity_Name (Hi) then
4018 Rewrite (N, New_Occurrence_Of (Entity (Hi), Loc));
4019 end if;
4020 end;
4021 end if;
4023 --------------
4024 -- Last_Bit --
4025 --------------
4027 -- We compute this if a component clause was present, otherwise we leave
4028 -- the computation up to the back end, since we don't know what layout
4029 -- will be chosen.
4031 when Attribute_Last_Bit => Last_Bit_Attr : declare
4032 CE : constant Entity_Id := Entity (Selector_Name (Pref));
4034 begin
4035 -- In Ada 2005 (or later) if we have the non-default bit order, then
4036 -- we return the original value as given in the component clause
4037 -- (RM 2005 13.5.2(3/2)).
4039 if Present (Component_Clause (CE))
4040 and then Ada_Version >= Ada_2005
4041 and then Reverse_Bit_Order (Scope (CE))
4042 then
4043 Rewrite (N,
4044 Make_Integer_Literal (Loc,
4045 Intval => Expr_Value (Last_Bit (Component_Clause (CE)))));
4046 Analyze_And_Resolve (N, Typ);
4048 -- Otherwise (Ada 83/95 or Ada 2005 or later with default bit order),
4049 -- rewrite with normalized value if we know it statically.
4051 elsif Known_Static_Component_Bit_Offset (CE)
4052 and then Known_Static_Esize (CE)
4053 then
4054 Rewrite (N,
4055 Make_Integer_Literal (Loc,
4056 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
4057 + Esize (CE) - 1));
4058 Analyze_And_Resolve (N, Typ);
4060 -- Otherwise leave to back end, just apply universal integer checks
4062 else
4063 Apply_Universal_Integer_Attribute_Checks (N);
4064 end if;
4065 end Last_Bit_Attr;
4067 ------------------
4068 -- Leading_Part --
4069 ------------------
4071 -- Transforms 'Leading_Part into a call to the floating-point attribute
4072 -- function Leading_Part in Fat_xxx (where xxx is the root type)
4074 -- Note: strictly, we should generate special case code to deal with
4075 -- absurdly large positive arguments (greater than Integer'Last), which
4076 -- result in returning the first argument unchanged, but it hardly seems
4077 -- worth the effort. We raise constraint error for absurdly negative
4078 -- arguments which is fine.
4080 when Attribute_Leading_Part =>
4081 Expand_Fpt_Attribute_RI (N);
4083 ------------
4084 -- Length --
4085 ------------
4087 when Attribute_Length => Length : declare
4088 Ityp : Entity_Id;
4089 Xnum : Uint;
4091 begin
4092 -- Processing for packed array types
4094 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
4095 Ityp := Get_Index_Subtype (N);
4097 -- If the index type, Ityp, is an enumeration type with holes,
4098 -- then we calculate X'Length explicitly using
4100 -- Typ'Max
4101 -- (0, Ityp'Pos (X'Last (N)) -
4102 -- Ityp'Pos (X'First (N)) + 1);
4104 -- Since the bounds in the template are the representation values
4105 -- and the back end would get the wrong value.
4107 if Is_Enumeration_Type (Ityp)
4108 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
4109 then
4110 if No (Exprs) then
4111 Xnum := Uint_1;
4112 else
4113 Xnum := Expr_Value (First (Expressions (N)));
4114 end if;
4116 Rewrite (N,
4117 Make_Attribute_Reference (Loc,
4118 Prefix => New_Occurrence_Of (Typ, Loc),
4119 Attribute_Name => Name_Max,
4120 Expressions => New_List
4121 (Make_Integer_Literal (Loc, 0),
4123 Make_Op_Add (Loc,
4124 Left_Opnd =>
4125 Make_Op_Subtract (Loc,
4126 Left_Opnd =>
4127 Make_Attribute_Reference (Loc,
4128 Prefix => New_Occurrence_Of (Ityp, Loc),
4129 Attribute_Name => Name_Pos,
4131 Expressions => New_List (
4132 Make_Attribute_Reference (Loc,
4133 Prefix => Duplicate_Subexpr (Pref),
4134 Attribute_Name => Name_Last,
4135 Expressions => New_List (
4136 Make_Integer_Literal (Loc, Xnum))))),
4138 Right_Opnd =>
4139 Make_Attribute_Reference (Loc,
4140 Prefix => New_Occurrence_Of (Ityp, Loc),
4141 Attribute_Name => Name_Pos,
4143 Expressions => New_List (
4144 Make_Attribute_Reference (Loc,
4145 Prefix =>
4146 Duplicate_Subexpr_No_Checks (Pref),
4147 Attribute_Name => Name_First,
4148 Expressions => New_List (
4149 Make_Integer_Literal (Loc, Xnum)))))),
4151 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
4153 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
4154 return;
4156 -- If the prefix type is a constrained packed array type which
4157 -- already has a Packed_Array_Impl_Type representation defined,
4158 -- then replace this attribute with a reference to 'Range_Length
4159 -- of the appropriate index subtype (since otherwise the
4160 -- back end will try to give us the value of 'Length for
4161 -- this implementation type).s
4163 elsif Is_Constrained (Ptyp) then
4164 Rewrite (N,
4165 Make_Attribute_Reference (Loc,
4166 Attribute_Name => Name_Range_Length,
4167 Prefix => New_Occurrence_Of (Ityp, Loc)));
4168 Analyze_And_Resolve (N, Typ);
4169 end if;
4171 -- Access type case
4173 elsif Is_Access_Type (Ptyp) then
4174 Apply_Access_Check (N);
4176 -- If the designated type is a packed array type, then we convert
4177 -- the reference to:
4179 -- typ'Max (0, 1 +
4180 -- xtyp'Pos (Pref'Last (Expr)) -
4181 -- xtyp'Pos (Pref'First (Expr)));
4183 -- This is a bit complex, but it is the easiest thing to do that
4184 -- works in all cases including enum types with holes xtyp here
4185 -- is the appropriate index type.
4187 declare
4188 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
4189 Xtyp : Entity_Id;
4191 begin
4192 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
4193 Xtyp := Get_Index_Subtype (N);
4195 Rewrite (N,
4196 Make_Attribute_Reference (Loc,
4197 Prefix => New_Occurrence_Of (Typ, Loc),
4198 Attribute_Name => Name_Max,
4199 Expressions => New_List (
4200 Make_Integer_Literal (Loc, 0),
4202 Make_Op_Add (Loc,
4203 Make_Integer_Literal (Loc, 1),
4204 Make_Op_Subtract (Loc,
4205 Left_Opnd =>
4206 Make_Attribute_Reference (Loc,
4207 Prefix => New_Occurrence_Of (Xtyp, Loc),
4208 Attribute_Name => Name_Pos,
4209 Expressions => New_List (
4210 Make_Attribute_Reference (Loc,
4211 Prefix => Duplicate_Subexpr (Pref),
4212 Attribute_Name => Name_Last,
4213 Expressions =>
4214 New_Copy_List (Exprs)))),
4216 Right_Opnd =>
4217 Make_Attribute_Reference (Loc,
4218 Prefix => New_Occurrence_Of (Xtyp, Loc),
4219 Attribute_Name => Name_Pos,
4220 Expressions => New_List (
4221 Make_Attribute_Reference (Loc,
4222 Prefix =>
4223 Duplicate_Subexpr_No_Checks (Pref),
4224 Attribute_Name => Name_First,
4225 Expressions =>
4226 New_Copy_List (Exprs)))))))));
4228 Analyze_And_Resolve (N, Typ);
4229 end if;
4230 end;
4232 -- Otherwise leave it to the back end
4234 else
4235 Apply_Universal_Integer_Attribute_Checks (N);
4236 end if;
4237 end Length;
4239 -- Attribute Loop_Entry is replaced with a reference to a constant value
4240 -- which captures the prefix at the entry point of the related loop. The
4241 -- loop itself may be transformed into a conditional block.
4243 when Attribute_Loop_Entry =>
4244 Expand_Loop_Entry_Attribute (N);
4246 -------------
4247 -- Machine --
4248 -------------
4250 -- Transforms 'Machine into a call to the floating-point attribute
4251 -- function Machine in Fat_xxx (where xxx is the root type).
4252 -- Expansion is avoided for cases the back end can handle directly.
4254 when Attribute_Machine =>
4255 if not Is_Inline_Floating_Point_Attribute (N) then
4256 Expand_Fpt_Attribute_R (N);
4257 end if;
4259 ----------------------
4260 -- Machine_Rounding --
4261 ----------------------
4263 -- Transforms 'Machine_Rounding into a call to the floating-point
4264 -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root
4265 -- type). Expansion is avoided for cases the back end can handle
4266 -- directly.
4268 when Attribute_Machine_Rounding =>
4269 if not Is_Inline_Floating_Point_Attribute (N) then
4270 Expand_Fpt_Attribute_R (N);
4271 end if;
4273 ------------------
4274 -- Machine_Size --
4275 ------------------
4277 -- Machine_Size is equivalent to Object_Size, so transform it into
4278 -- Object_Size and that way the back end never sees Machine_Size.
4280 when Attribute_Machine_Size =>
4281 Rewrite (N,
4282 Make_Attribute_Reference (Loc,
4283 Prefix => Prefix (N),
4284 Attribute_Name => Name_Object_Size));
4286 Analyze_And_Resolve (N, Typ);
4288 --------------
4289 -- Mantissa --
4290 --------------
4292 -- The only case that can get this far is the dynamic case of the old
4293 -- Ada 83 Mantissa attribute for the fixed-point case. For this case,
4294 -- we expand:
4296 -- typ'Mantissa
4298 -- into
4300 -- ityp (System.Mantissa.Mantissa_Value
4301 -- (Integer'Integer_Value (typ'First),
4302 -- Integer'Integer_Value (typ'Last)));
4304 when Attribute_Mantissa =>
4305 Rewrite (N,
4306 Convert_To (Typ,
4307 Make_Function_Call (Loc,
4308 Name =>
4309 New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
4311 Parameter_Associations => New_List (
4312 Make_Attribute_Reference (Loc,
4313 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
4314 Attribute_Name => Name_Integer_Value,
4315 Expressions => New_List (
4316 Make_Attribute_Reference (Loc,
4317 Prefix => New_Occurrence_Of (Ptyp, Loc),
4318 Attribute_Name => Name_First))),
4320 Make_Attribute_Reference (Loc,
4321 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
4322 Attribute_Name => Name_Integer_Value,
4323 Expressions => New_List (
4324 Make_Attribute_Reference (Loc,
4325 Prefix => New_Occurrence_Of (Ptyp, Loc),
4326 Attribute_Name => Name_Last)))))));
4328 Analyze_And_Resolve (N, Typ);
4330 ---------
4331 -- Max --
4332 ---------
4334 when Attribute_Max =>
4335 Expand_Min_Max_Attribute (N);
4337 ----------------------------------
4338 -- Max_Size_In_Storage_Elements --
4339 ----------------------------------
4341 when Attribute_Max_Size_In_Storage_Elements => declare
4342 Typ : constant Entity_Id := Etype (N);
4343 Attr : Node_Id;
4345 Conversion_Added : Boolean := False;
4346 -- A flag which tracks whether the original attribute has been
4347 -- wrapped inside a type conversion.
4349 begin
4350 -- If the prefix is X'Class, we transform it into a direct reference
4351 -- to the class-wide type, because the back end must not see a 'Class
4352 -- reference. See also 'Size.
4354 if Is_Entity_Name (Pref)
4355 and then Is_Class_Wide_Type (Entity (Pref))
4356 then
4357 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
4358 return;
4359 end if;
4361 Apply_Universal_Integer_Attribute_Checks (N);
4363 -- The universal integer check may sometimes add a type conversion,
4364 -- retrieve the original attribute reference from the expression.
4366 Attr := N;
4368 if Nkind (Attr) = N_Type_Conversion then
4369 Attr := Expression (Attr);
4370 Conversion_Added := True;
4371 end if;
4373 pragma Assert (Nkind (Attr) = N_Attribute_Reference);
4375 -- Heap-allocated controlled objects contain two extra pointers which
4376 -- are not part of the actual type. Transform the attribute reference
4377 -- into a runtime expression to add the size of the hidden header.
4379 if Needs_Finalization (Ptyp)
4380 and then not Header_Size_Added (Attr)
4381 then
4382 Set_Header_Size_Added (Attr);
4384 -- Generate:
4385 -- P'Max_Size_In_Storage_Elements +
4386 -- Universal_Integer
4387 -- (Header_Size_With_Padding (Ptyp'Alignment))
4389 Rewrite (Attr,
4390 Make_Op_Add (Loc,
4391 Left_Opnd => Relocate_Node (Attr),
4392 Right_Opnd =>
4393 Convert_To (Universal_Integer,
4394 Make_Function_Call (Loc,
4395 Name =>
4396 New_Occurrence_Of
4397 (RTE (RE_Header_Size_With_Padding), Loc),
4399 Parameter_Associations => New_List (
4400 Make_Attribute_Reference (Loc,
4401 Prefix =>
4402 New_Occurrence_Of (Ptyp, Loc),
4403 Attribute_Name => Name_Alignment))))));
4405 -- Add a conversion to the target type
4407 if not Conversion_Added then
4408 Rewrite (Attr,
4409 Make_Type_Conversion (Loc,
4410 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4411 Expression => Relocate_Node (Attr)));
4412 end if;
4414 Analyze (Attr);
4415 return;
4416 end if;
4417 end;
4419 --------------------
4420 -- Mechanism_Code --
4421 --------------------
4423 when Attribute_Mechanism_Code =>
4425 -- We must replace the prefix in the renamed case
4427 if Is_Entity_Name (Pref)
4428 and then Present (Alias (Entity (Pref)))
4429 then
4430 Set_Renamed_Subprogram (Pref, Alias (Entity (Pref)));
4431 end if;
4433 ---------
4434 -- Min --
4435 ---------
4437 when Attribute_Min =>
4438 Expand_Min_Max_Attribute (N);
4440 ---------
4441 -- Mod --
4442 ---------
4444 when Attribute_Mod => Mod_Case : declare
4445 Arg : constant Node_Id := Relocate_Node (First (Exprs));
4446 Hi : constant Node_Id := Type_High_Bound (Etype (Arg));
4447 Modv : constant Uint := Modulus (Btyp);
4449 begin
4451 -- This is not so simple. The issue is what type to use for the
4452 -- computation of the modular value.
4454 -- The easy case is when the modulus value is within the bounds
4455 -- of the signed integer type of the argument. In this case we can
4456 -- just do the computation in that signed integer type, and then
4457 -- do an ordinary conversion to the target type.
4459 if Modv <= Expr_Value (Hi) then
4460 Rewrite (N,
4461 Convert_To (Btyp,
4462 Make_Op_Mod (Loc,
4463 Left_Opnd => Arg,
4464 Right_Opnd => Make_Integer_Literal (Loc, Modv))));
4466 -- Here we know that the modulus is larger than type'Last of the
4467 -- integer type. There are two cases to consider:
4469 -- a) The integer value is non-negative. In this case, it is
4470 -- returned as the result (since it is less than the modulus).
4472 -- b) The integer value is negative. In this case, we know that the
4473 -- result is modulus + value, where the value might be as small as
4474 -- -modulus. The trouble is what type do we use to do the subtract.
4475 -- No type will do, since modulus can be as big as 2**64, and no
4476 -- integer type accommodates this value. Let's do bit of algebra
4478 -- modulus + value
4479 -- = modulus - (-value)
4480 -- = (modulus - 1) - (-value - 1)
4482 -- Now modulus - 1 is certainly in range of the modular type.
4483 -- -value is in the range 1 .. modulus, so -value -1 is in the
4484 -- range 0 .. modulus-1 which is in range of the modular type.
4485 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
4486 -- which we can compute using the integer base type.
4488 -- Once this is done we analyze the if expression without range
4489 -- checks, because we know everything is in range, and we want
4490 -- to prevent spurious warnings on either branch.
4492 else
4493 Rewrite (N,
4494 Make_If_Expression (Loc,
4495 Expressions => New_List (
4496 Make_Op_Ge (Loc,
4497 Left_Opnd => Duplicate_Subexpr (Arg),
4498 Right_Opnd => Make_Integer_Literal (Loc, 0)),
4500 Convert_To (Btyp,
4501 Duplicate_Subexpr_No_Checks (Arg)),
4503 Make_Op_Subtract (Loc,
4504 Left_Opnd =>
4505 Make_Integer_Literal (Loc,
4506 Intval => Modv - 1),
4507 Right_Opnd =>
4508 Convert_To (Btyp,
4509 Make_Op_Minus (Loc,
4510 Right_Opnd =>
4511 Make_Op_Add (Loc,
4512 Left_Opnd => Duplicate_Subexpr_No_Checks (Arg),
4513 Right_Opnd =>
4514 Make_Integer_Literal (Loc,
4515 Intval => 1))))))));
4517 end if;
4519 Analyze_And_Resolve (N, Btyp, Suppress => All_Checks);
4520 end Mod_Case;
4522 -----------
4523 -- Model --
4524 -----------
4526 -- Transforms 'Model into a call to the floating-point attribute
4527 -- function Model in Fat_xxx (where xxx is the root type).
4528 -- Expansion is avoided for cases the back end can handle directly.
4530 when Attribute_Model =>
4531 if not Is_Inline_Floating_Point_Attribute (N) then
4532 Expand_Fpt_Attribute_R (N);
4533 end if;
4535 -----------------
4536 -- Object_Size --
4537 -----------------
4539 -- The processing for Object_Size shares the processing for Size
4541 ---------
4542 -- Old --
4543 ---------
4545 when Attribute_Old => Old : declare
4546 Typ : constant Entity_Id := Etype (N);
4547 CW_Temp : Entity_Id;
4548 CW_Typ : Entity_Id;
4549 Ins_Nod : Node_Id;
4550 Subp : Node_Id;
4551 Temp : Entity_Id;
4553 begin
4554 -- Generating C code we don't need to expand this attribute when
4555 -- we are analyzing the internally built nested postconditions
4556 -- procedure since it will be expanded inline (and later it will
4557 -- be removed by Expand_N_Subprogram_Body). It this expansion is
4558 -- performed in such case then the compiler generates unreferenced
4559 -- extra temporaries.
4561 if Modify_Tree_For_C
4562 and then Chars (Current_Scope) = Name_uPostconditions
4563 then
4564 return;
4565 end if;
4567 -- Climb the parent chain looking for subprogram _Postconditions
4569 Subp := N;
4570 while Present (Subp) loop
4571 exit when Nkind (Subp) = N_Subprogram_Body
4572 and then Chars (Defining_Entity (Subp)) = Name_uPostconditions;
4574 -- If assertions are disabled, no need to create the declaration
4575 -- that preserves the value. The postcondition pragma in which
4576 -- 'Old appears will be checked or disabled according to the
4577 -- current policy in effect.
4579 if Nkind (Subp) = N_Pragma and then not Is_Checked (Subp) then
4580 return;
4581 end if;
4583 Subp := Parent (Subp);
4584 end loop;
4586 -- 'Old can only appear in a postcondition, the generated body of
4587 -- _Postconditions must be in the tree (or inlined if we are
4588 -- generating C code).
4590 pragma Assert
4591 (Present (Subp)
4592 or else (Modify_Tree_For_C and then In_Inlined_Body));
4594 Temp := Make_Temporary (Loc, 'T', Pref);
4596 -- Set the entity kind now in order to mark the temporary as a
4597 -- handler of attribute 'Old's prefix.
4599 Set_Ekind (Temp, E_Constant);
4600 Set_Stores_Attribute_Old_Prefix (Temp);
4602 -- Push the scope of the related subprogram where _Postcondition
4603 -- resides as this ensures that the object will be analyzed in the
4604 -- proper context.
4606 if Present (Subp) then
4607 Push_Scope (Scope (Defining_Entity (Subp)));
4609 -- No need to push the scope when generating C code since the
4610 -- _Postcondition procedure has been inlined.
4612 else pragma Assert (Modify_Tree_For_C);
4613 pragma Assert (In_Inlined_Body);
4614 null;
4615 end if;
4617 -- Locate the insertion place of the internal temporary that saves
4618 -- the 'Old value.
4620 if Present (Subp) then
4621 Ins_Nod := Subp;
4623 -- Generating C, the postcondition procedure has been inlined and the
4624 -- temporary is added before the first declaration of the enclosing
4625 -- subprogram.
4627 else pragma Assert (Modify_Tree_For_C);
4628 Ins_Nod := N;
4629 while Nkind (Ins_Nod) /= N_Subprogram_Body loop
4630 Ins_Nod := Parent (Ins_Nod);
4631 end loop;
4633 Ins_Nod := First (Declarations (Ins_Nod));
4634 end if;
4636 -- Preserve the tag of the prefix by offering a specific view of the
4637 -- class-wide version of the prefix.
4639 if Is_Tagged_Type (Typ) then
4641 -- Generate:
4642 -- CW_Temp : constant Typ'Class := Typ'Class (Pref);
4644 CW_Temp := Make_Temporary (Loc, 'T');
4645 CW_Typ := Class_Wide_Type (Typ);
4647 Insert_Before_And_Analyze (Ins_Nod,
4648 Make_Object_Declaration (Loc,
4649 Defining_Identifier => CW_Temp,
4650 Constant_Present => True,
4651 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
4652 Expression =>
4653 Convert_To (CW_Typ, Relocate_Node (Pref))));
4655 -- Generate:
4656 -- Temp : Typ renames Typ (CW_Temp);
4658 Insert_Before_And_Analyze (Ins_Nod,
4659 Make_Object_Renaming_Declaration (Loc,
4660 Defining_Identifier => Temp,
4661 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4662 Name =>
4663 Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc))));
4665 -- Non-tagged case
4667 else
4668 -- Generate:
4669 -- Temp : constant Typ := Pref;
4671 Insert_Before_And_Analyze (Ins_Nod,
4672 Make_Object_Declaration (Loc,
4673 Defining_Identifier => Temp,
4674 Constant_Present => True,
4675 Object_Definition => New_Occurrence_Of (Typ, Loc),
4676 Expression => Relocate_Node (Pref)));
4677 end if;
4679 if Present (Subp) then
4680 Pop_Scope;
4681 end if;
4683 -- Ensure that the prefix of attribute 'Old is valid. The check must
4684 -- be inserted after the expansion of the attribute has taken place
4685 -- to reflect the new placement of the prefix.
4687 if Validity_Checks_On and then Validity_Check_Operands then
4688 Ensure_Valid (Pref);
4689 end if;
4691 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4692 end Old;
4694 ----------------------
4695 -- Overlaps_Storage --
4696 ----------------------
4698 when Attribute_Overlaps_Storage => Overlaps_Storage : declare
4699 Loc : constant Source_Ptr := Sloc (N);
4701 X : constant Node_Id := Prefix (N);
4702 Y : constant Node_Id := First (Expressions (N));
4703 -- The arguments
4705 X_Addr, Y_Addr : Node_Id;
4706 -- the expressions for their integer addresses
4708 X_Size, Y_Size : Node_Id;
4709 -- the expressions for their sizes
4711 Cond : Node_Id;
4713 begin
4714 -- Attribute expands into:
4716 -- if X'Address < Y'address then
4717 -- (X'address + X'Size - 1) >= Y'address
4718 -- else
4719 -- (Y'address + Y'size - 1) >= X'Address
4720 -- end if;
4722 -- with the proper address operations. We convert addresses to
4723 -- integer addresses to use predefined arithmetic. The size is
4724 -- expressed in storage units. We add copies of X_Addr and Y_Addr
4725 -- to prevent the appearance of the same node in two places in
4726 -- the tree.
4728 X_Addr :=
4729 Unchecked_Convert_To (RTE (RE_Integer_Address),
4730 Make_Attribute_Reference (Loc,
4731 Attribute_Name => Name_Address,
4732 Prefix => New_Copy_Tree (X)));
4734 Y_Addr :=
4735 Unchecked_Convert_To (RTE (RE_Integer_Address),
4736 Make_Attribute_Reference (Loc,
4737 Attribute_Name => Name_Address,
4738 Prefix => New_Copy_Tree (Y)));
4740 X_Size :=
4741 Make_Op_Divide (Loc,
4742 Left_Opnd =>
4743 Make_Attribute_Reference (Loc,
4744 Attribute_Name => Name_Size,
4745 Prefix => New_Copy_Tree (X)),
4746 Right_Opnd =>
4747 Make_Integer_Literal (Loc, System_Storage_Unit));
4749 Y_Size :=
4750 Make_Op_Divide (Loc,
4751 Left_Opnd =>
4752 Make_Attribute_Reference (Loc,
4753 Attribute_Name => Name_Size,
4754 Prefix => New_Copy_Tree (Y)),
4755 Right_Opnd =>
4756 Make_Integer_Literal (Loc, System_Storage_Unit));
4758 Cond :=
4759 Make_Op_Le (Loc,
4760 Left_Opnd => X_Addr,
4761 Right_Opnd => Y_Addr);
4763 Rewrite (N,
4764 Make_If_Expression (Loc, New_List (
4765 Cond,
4767 Make_Op_Ge (Loc,
4768 Left_Opnd =>
4769 Make_Op_Add (Loc,
4770 Left_Opnd => New_Copy_Tree (X_Addr),
4771 Right_Opnd =>
4772 Make_Op_Subtract (Loc,
4773 Left_Opnd => X_Size,
4774 Right_Opnd => Make_Integer_Literal (Loc, 1))),
4775 Right_Opnd => Y_Addr),
4777 Make_Op_Ge (Loc,
4778 Left_Opnd =>
4779 Make_Op_Add (Loc,
4780 Left_Opnd => New_Copy_Tree (Y_Addr),
4781 Right_Opnd =>
4782 Make_Op_Subtract (Loc,
4783 Left_Opnd => Y_Size,
4784 Right_Opnd => Make_Integer_Literal (Loc, 1))),
4785 Right_Opnd => X_Addr))));
4787 Analyze_And_Resolve (N, Standard_Boolean);
4788 end Overlaps_Storage;
4790 ------------
4791 -- Output --
4792 ------------
4794 when Attribute_Output => Output : declare
4795 P_Type : constant Entity_Id := Entity (Pref);
4796 U_Type : constant Entity_Id := Underlying_Type (P_Type);
4797 Pname : Entity_Id;
4798 Decl : Node_Id;
4799 Prag : Node_Id;
4800 Arg3 : Node_Id;
4801 Wfunc : Node_Id;
4803 begin
4804 -- If no underlying type, we have an error that will be diagnosed
4805 -- elsewhere, so here we just completely ignore the expansion.
4807 if No (U_Type) then
4808 return;
4809 end if;
4811 -- Stream operations can appear in user code even if the restriction
4812 -- No_Streams is active (for example, when instantiating a predefined
4813 -- container). In that case rewrite the attribute as a Raise to
4814 -- prevent any run-time use.
4816 if Restriction_Active (No_Streams) then
4817 Rewrite (N,
4818 Make_Raise_Program_Error (Sloc (N),
4819 Reason => PE_Stream_Operation_Not_Allowed));
4820 Set_Etype (N, Standard_Void_Type);
4821 return;
4822 end if;
4824 -- If TSS for Output is present, just call it
4826 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
4828 if Present (Pname) then
4829 null;
4831 else
4832 -- If there is a Stream_Convert pragma, use it, we rewrite
4834 -- sourcetyp'Output (stream, Item)
4836 -- as
4838 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
4840 -- where strmwrite is the given Write function that converts an
4841 -- argument of type sourcetyp or a type acctyp, from which it is
4842 -- derived to type strmtyp. The conversion to acttyp is required
4843 -- for the derived case.
4845 Prag := Get_Stream_Convert_Pragma (P_Type);
4847 if Present (Prag) then
4848 Arg3 :=
4849 Next (Next (First (Pragma_Argument_Associations (Prag))));
4850 Wfunc := Entity (Expression (Arg3));
4852 Rewrite (N,
4853 Make_Attribute_Reference (Loc,
4854 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
4855 Attribute_Name => Name_Output,
4856 Expressions => New_List (
4857 Relocate_Node (First (Exprs)),
4858 Make_Function_Call (Loc,
4859 Name => New_Occurrence_Of (Wfunc, Loc),
4860 Parameter_Associations => New_List (
4861 OK_Convert_To (Etype (First_Formal (Wfunc)),
4862 Relocate_Node (Next (First (Exprs)))))))));
4864 Analyze (N);
4865 return;
4867 -- For elementary types, we call the W_xxx routine directly. Note
4868 -- that the effect of Write and Output is identical for the case
4869 -- of an elementary type (there are no discriminants or bounds).
4871 elsif Is_Elementary_Type (U_Type) then
4873 -- A special case arises if we have a defined _Write routine,
4874 -- since in this case we are required to call this routine.
4876 declare
4877 Typ : Entity_Id := P_Type;
4878 begin
4879 if Present (Full_View (Typ)) then
4880 Typ := Full_View (Typ);
4881 end if;
4883 if Present (TSS (Base_Type (Typ), TSS_Stream_Write)) then
4884 Build_Record_Or_Elementary_Output_Procedure
4885 (Loc, Typ, Decl, Pname);
4886 Insert_Action (N, Decl);
4888 -- For normal cases, we call the W_xxx routine directly
4890 else
4891 Rewrite (N, Build_Elementary_Write_Call (N));
4892 Analyze (N);
4893 return;
4894 end if;
4895 end;
4897 -- Array type case
4899 elsif Is_Array_Type (U_Type) then
4900 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
4901 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
4903 -- Class-wide case, first output external tag, then dispatch
4904 -- to the appropriate primitive Output function (RM 13.13.2(31)).
4906 elsif Is_Class_Wide_Type (P_Type) then
4908 -- No need to do anything else compiling under restriction
4909 -- No_Dispatching_Calls. During the semantic analysis we
4910 -- already notified such violation.
4912 if Restriction_Active (No_Dispatching_Calls) then
4913 return;
4914 end if;
4916 Tag_Write : declare
4917 Strm : constant Node_Id := First (Exprs);
4918 Item : constant Node_Id := Next (Strm);
4920 begin
4921 -- Ada 2005 (AI-344): Check that the accessibility level
4922 -- of the type of the output object is not deeper than
4923 -- that of the attribute's prefix type.
4925 -- if Get_Access_Level (Item'Tag)
4926 -- /= Get_Access_Level (P_Type'Tag)
4927 -- then
4928 -- raise Tag_Error;
4929 -- end if;
4931 -- String'Output (Strm, External_Tag (Item'Tag));
4933 -- We cannot figure out a practical way to implement this
4934 -- accessibility check on virtual machines, so we omit it.
4936 if Ada_Version >= Ada_2005
4937 and then Tagged_Type_Expansion
4938 then
4939 Insert_Action (N,
4940 Make_Implicit_If_Statement (N,
4941 Condition =>
4942 Make_Op_Ne (Loc,
4943 Left_Opnd =>
4944 Build_Get_Access_Level (Loc,
4945 Make_Attribute_Reference (Loc,
4946 Prefix =>
4947 Relocate_Node (
4948 Duplicate_Subexpr (Item,
4949 Name_Req => True)),
4950 Attribute_Name => Name_Tag)),
4952 Right_Opnd =>
4953 Make_Integer_Literal (Loc,
4954 Type_Access_Level (P_Type))),
4956 Then_Statements =>
4957 New_List (Make_Raise_Statement (Loc,
4958 New_Occurrence_Of (
4959 RTE (RE_Tag_Error), Loc)))));
4960 end if;
4962 Insert_Action (N,
4963 Make_Attribute_Reference (Loc,
4964 Prefix => New_Occurrence_Of (Standard_String, Loc),
4965 Attribute_Name => Name_Output,
4966 Expressions => New_List (
4967 Relocate_Node (Duplicate_Subexpr (Strm)),
4968 Make_Function_Call (Loc,
4969 Name =>
4970 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
4971 Parameter_Associations => New_List (
4972 Make_Attribute_Reference (Loc,
4973 Prefix =>
4974 Relocate_Node
4975 (Duplicate_Subexpr (Item, Name_Req => True)),
4976 Attribute_Name => Name_Tag))))));
4977 end Tag_Write;
4979 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
4981 -- Tagged type case, use the primitive Output function
4983 elsif Is_Tagged_Type (U_Type) then
4984 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
4986 -- All other record type cases, including protected records.
4987 -- The latter only arise for expander generated code for
4988 -- handling shared passive partition access.
4990 else
4991 pragma Assert
4992 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
4994 -- Ada 2005 (AI-216): Program_Error is raised when executing
4995 -- the default implementation of the Output attribute of an
4996 -- unchecked union type if the type lacks default discriminant
4997 -- values.
4999 if Is_Unchecked_Union (Base_Type (U_Type))
5000 and then No (Discriminant_Constraint (U_Type))
5001 then
5002 Insert_Action (N,
5003 Make_Raise_Program_Error (Loc,
5004 Reason => PE_Unchecked_Union_Restriction));
5006 return;
5007 end if;
5009 Build_Record_Or_Elementary_Output_Procedure
5010 (Loc, Base_Type (U_Type), Decl, Pname);
5011 Insert_Action (N, Decl);
5012 end if;
5013 end if;
5015 -- If we fall through, Pname is the name of the procedure to call
5017 Rewrite_Stream_Proc_Call (Pname);
5018 end Output;
5020 ---------
5021 -- Pos --
5022 ---------
5024 -- For enumeration types with a standard representation, Pos is
5025 -- handled by the back end.
5027 -- For enumeration types, with a non-standard representation we generate
5028 -- a call to the _Rep_To_Pos function created when the type was frozen.
5029 -- The call has the form
5031 -- _rep_to_pos (expr, flag)
5033 -- The parameter flag is True if range checks are enabled, causing
5034 -- Program_Error to be raised if the expression has an invalid
5035 -- representation, and False if range checks are suppressed.
5037 -- For integer types, Pos is equivalent to a simple integer
5038 -- conversion and we rewrite it as such
5040 when Attribute_Pos => Pos : declare
5041 Etyp : Entity_Id := Base_Type (Entity (Pref));
5043 begin
5044 -- Deal with zero/non-zero boolean values
5046 if Is_Boolean_Type (Etyp) then
5047 Adjust_Condition (First (Exprs));
5048 Etyp := Standard_Boolean;
5049 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
5050 end if;
5052 -- Case of enumeration type
5054 if Is_Enumeration_Type (Etyp) then
5056 -- Non-standard enumeration type (generate call)
5058 if Present (Enum_Pos_To_Rep (Etyp)) then
5059 Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
5060 Rewrite (N,
5061 Convert_To (Typ,
5062 Make_Function_Call (Loc,
5063 Name =>
5064 New_Occurrence_Of (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5065 Parameter_Associations => Exprs)));
5067 Analyze_And_Resolve (N, Typ);
5069 -- Standard enumeration type (do universal integer check)
5071 else
5072 Apply_Universal_Integer_Attribute_Checks (N);
5073 end if;
5075 -- Deal with integer types (replace by conversion)
5077 elsif Is_Integer_Type (Etyp) then
5078 Rewrite (N, Convert_To (Typ, First (Exprs)));
5079 Analyze_And_Resolve (N, Typ);
5080 end if;
5082 end Pos;
5084 --------------
5085 -- Position --
5086 --------------
5088 -- We compute this if a component clause was present, otherwise we leave
5089 -- the computation up to the back end, since we don't know what layout
5090 -- will be chosen.
5092 when Attribute_Position => Position_Attr : declare
5093 CE : constant Entity_Id := Entity (Selector_Name (Pref));
5095 begin
5096 if Present (Component_Clause (CE)) then
5098 -- In Ada 2005 (or later) if we have the non-default bit order,
5099 -- then we return the original value as given in the component
5100 -- clause (RM 2005 13.5.2(2/2)).
5102 if Ada_Version >= Ada_2005
5103 and then Reverse_Bit_Order (Scope (CE))
5104 then
5105 Rewrite (N,
5106 Make_Integer_Literal (Loc,
5107 Intval => Expr_Value (Position (Component_Clause (CE)))));
5109 -- Otherwise (Ada 83 or 95, or default bit order specified in
5110 -- later Ada version), return the normalized value.
5112 else
5113 Rewrite (N,
5114 Make_Integer_Literal (Loc,
5115 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
5116 end if;
5118 Analyze_And_Resolve (N, Typ);
5120 -- If back end is doing things, just apply universal integer checks
5122 else
5123 Apply_Universal_Integer_Attribute_Checks (N);
5124 end if;
5125 end Position_Attr;
5127 ----------
5128 -- Pred --
5129 ----------
5131 -- 1. Deal with enumeration types with holes.
5132 -- 2. For floating-point, generate call to attribute function.
5133 -- 3. For other cases, deal with constraint checking.
5135 when Attribute_Pred => Pred : declare
5136 Etyp : constant Entity_Id := Base_Type (Ptyp);
5138 begin
5140 -- For enumeration types with non-standard representations, we
5141 -- expand typ'Pred (x) into
5143 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
5145 -- If the representation is contiguous, we compute instead
5146 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
5147 -- The conversion function Enum_Pos_To_Rep is defined on the
5148 -- base type, not the subtype, so we have to use the base type
5149 -- explicitly for this and other enumeration attributes.
5151 if Is_Enumeration_Type (Ptyp)
5152 and then Present (Enum_Pos_To_Rep (Etyp))
5153 then
5154 if Has_Contiguous_Rep (Etyp) then
5155 Rewrite (N,
5156 Unchecked_Convert_To (Ptyp,
5157 Make_Op_Add (Loc,
5158 Left_Opnd =>
5159 Make_Integer_Literal (Loc,
5160 Enumeration_Rep (First_Literal (Ptyp))),
5161 Right_Opnd =>
5162 Make_Function_Call (Loc,
5163 Name =>
5164 New_Occurrence_Of
5165 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5167 Parameter_Associations =>
5168 New_List (
5169 Unchecked_Convert_To (Ptyp,
5170 Make_Op_Subtract (Loc,
5171 Left_Opnd =>
5172 Unchecked_Convert_To (Standard_Integer,
5173 Relocate_Node (First (Exprs))),
5174 Right_Opnd =>
5175 Make_Integer_Literal (Loc, 1))),
5176 Rep_To_Pos_Flag (Ptyp, Loc))))));
5178 else
5179 -- Add Boolean parameter True, to request program errror if
5180 -- we have a bad representation on our hands. If checks are
5181 -- suppressed, then add False instead
5183 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
5184 Rewrite (N,
5185 Make_Indexed_Component (Loc,
5186 Prefix =>
5187 New_Occurrence_Of
5188 (Enum_Pos_To_Rep (Etyp), Loc),
5189 Expressions => New_List (
5190 Make_Op_Subtract (Loc,
5191 Left_Opnd =>
5192 Make_Function_Call (Loc,
5193 Name =>
5194 New_Occurrence_Of
5195 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5196 Parameter_Associations => Exprs),
5197 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
5198 end if;
5200 Analyze_And_Resolve (N, Typ);
5202 -- For floating-point, we transform 'Pred into a call to the Pred
5203 -- floating-point attribute function in Fat_xxx (xxx is root type).
5204 -- Note that this function takes care of the overflow case.
5206 elsif Is_Floating_Point_Type (Ptyp) then
5207 Expand_Fpt_Attribute_R (N);
5208 Analyze_And_Resolve (N, Typ);
5210 -- For modular types, nothing to do (no overflow, since wraps)
5212 elsif Is_Modular_Integer_Type (Ptyp) then
5213 null;
5215 -- For other types, if argument is marked as needing a range check or
5216 -- overflow checking is enabled, we must generate a check.
5218 elsif not Overflow_Checks_Suppressed (Ptyp)
5219 or else Do_Range_Check (First (Exprs))
5220 then
5221 Set_Do_Range_Check (First (Exprs), False);
5222 Expand_Pred_Succ_Attribute (N);
5223 end if;
5224 end Pred;
5226 --------------
5227 -- Priority --
5228 --------------
5230 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5232 -- We rewrite X'Priority as the following run-time call:
5234 -- Get_Ceiling (X._Object)
5236 -- Note that although X'Priority is notionally an object, it is quite
5237 -- deliberately not defined as an aliased object in the RM. This means
5238 -- that it works fine to rewrite it as a call, without having to worry
5239 -- about complications that would other arise from X'Priority'Access,
5240 -- which is illegal, because of the lack of aliasing.
5242 when Attribute_Priority => Priority : declare
5243 Call : Node_Id;
5244 Conctyp : Entity_Id;
5245 New_Itype : Entity_Id;
5246 Object_Parm : Node_Id;
5247 Subprg : Entity_Id;
5248 RT_Subprg_Name : Node_Id;
5250 begin
5251 -- Look for the enclosing concurrent type
5253 Conctyp := Current_Scope;
5254 while not Is_Concurrent_Type (Conctyp) loop
5255 Conctyp := Scope (Conctyp);
5256 end loop;
5258 pragma Assert (Is_Protected_Type (Conctyp));
5260 -- Generate the actual of the call
5262 Subprg := Current_Scope;
5263 while not Present (Protected_Body_Subprogram (Subprg)) loop
5264 Subprg := Scope (Subprg);
5265 end loop;
5267 -- Use of 'Priority inside protected entries and barriers (in both
5268 -- cases the type of the first formal of their expanded subprogram
5269 -- is Address)
5271 if Etype (First_Entity (Protected_Body_Subprogram (Subprg))) =
5272 RTE (RE_Address)
5273 then
5274 -- In the expansion of protected entries the type of the first
5275 -- formal of the Protected_Body_Subprogram is an Address. In order
5276 -- to reference the _object component we generate:
5278 -- type T is access p__ptTV;
5279 -- freeze T []
5281 New_Itype := Create_Itype (E_Access_Type, N);
5282 Set_Etype (New_Itype, New_Itype);
5283 Set_Directly_Designated_Type (New_Itype,
5284 Corresponding_Record_Type (Conctyp));
5285 Freeze_Itype (New_Itype, N);
5287 -- Generate:
5288 -- T!(O)._object'unchecked_access
5290 Object_Parm :=
5291 Make_Attribute_Reference (Loc,
5292 Prefix =>
5293 Make_Selected_Component (Loc,
5294 Prefix =>
5295 Unchecked_Convert_To (New_Itype,
5296 New_Occurrence_Of
5297 (First_Entity (Protected_Body_Subprogram (Subprg)),
5298 Loc)),
5299 Selector_Name => Make_Identifier (Loc, Name_uObject)),
5300 Attribute_Name => Name_Unchecked_Access);
5302 -- Use of 'Priority inside a protected subprogram
5304 else
5305 Object_Parm :=
5306 Make_Attribute_Reference (Loc,
5307 Prefix =>
5308 Make_Selected_Component (Loc,
5309 Prefix =>
5310 New_Occurrence_Of
5311 (First_Entity (Protected_Body_Subprogram (Subprg)),
5312 Loc),
5313 Selector_Name => Make_Identifier (Loc, Name_uObject)),
5314 Attribute_Name => Name_Unchecked_Access);
5315 end if;
5317 -- Select the appropriate run-time subprogram
5319 if Number_Entries (Conctyp) = 0 then
5320 RT_Subprg_Name := New_Occurrence_Of (RTE (RE_Get_Ceiling), Loc);
5321 else
5322 RT_Subprg_Name := New_Occurrence_Of (RTE (RO_PE_Get_Ceiling), Loc);
5323 end if;
5325 Call :=
5326 Make_Function_Call (Loc,
5327 Name => RT_Subprg_Name,
5328 Parameter_Associations => New_List (Object_Parm));
5330 Rewrite (N, Call);
5332 -- Avoid the generation of extra checks on the pointer to the
5333 -- protected object.
5335 Analyze_And_Resolve (N, Typ, Suppress => Access_Check);
5336 end Priority;
5338 ------------------
5339 -- Range_Length --
5340 ------------------
5342 when Attribute_Range_Length =>
5344 -- The only special processing required is for the case where
5345 -- Range_Length is applied to an enumeration type with holes.
5346 -- In this case we transform
5348 -- X'Range_Length
5350 -- to
5352 -- X'Pos (X'Last) - X'Pos (X'First) + 1
5354 -- So that the result reflects the proper Pos values instead
5355 -- of the underlying representations.
5357 if Is_Enumeration_Type (Ptyp)
5358 and then Has_Non_Standard_Rep (Ptyp)
5359 then
5360 Rewrite (N,
5361 Make_Op_Add (Loc,
5362 Left_Opnd =>
5363 Make_Op_Subtract (Loc,
5364 Left_Opnd =>
5365 Make_Attribute_Reference (Loc,
5366 Attribute_Name => Name_Pos,
5367 Prefix => New_Occurrence_Of (Ptyp, Loc),
5368 Expressions => New_List (
5369 Make_Attribute_Reference (Loc,
5370 Attribute_Name => Name_Last,
5371 Prefix =>
5372 New_Occurrence_Of (Ptyp, Loc)))),
5374 Right_Opnd =>
5375 Make_Attribute_Reference (Loc,
5376 Attribute_Name => Name_Pos,
5377 Prefix => New_Occurrence_Of (Ptyp, Loc),
5378 Expressions => New_List (
5379 Make_Attribute_Reference (Loc,
5380 Attribute_Name => Name_First,
5381 Prefix =>
5382 New_Occurrence_Of (Ptyp, Loc))))),
5384 Right_Opnd => Make_Integer_Literal (Loc, 1)));
5386 Analyze_And_Resolve (N, Typ);
5388 -- For all other cases, the attribute is handled by the back end, but
5389 -- we need to deal with the case of the range check on a universal
5390 -- integer.
5392 else
5393 Apply_Universal_Integer_Attribute_Checks (N);
5394 end if;
5396 ----------
5397 -- Read --
5398 ----------
5400 when Attribute_Read => Read : declare
5401 P_Type : constant Entity_Id := Entity (Pref);
5402 B_Type : constant Entity_Id := Base_Type (P_Type);
5403 U_Type : constant Entity_Id := Underlying_Type (P_Type);
5404 Pname : Entity_Id;
5405 Decl : Node_Id;
5406 Prag : Node_Id;
5407 Arg2 : Node_Id;
5408 Rfunc : Node_Id;
5409 Lhs : Node_Id;
5410 Rhs : Node_Id;
5412 begin
5413 -- If no underlying type, we have an error that will be diagnosed
5414 -- elsewhere, so here we just completely ignore the expansion.
5416 if No (U_Type) then
5417 return;
5418 end if;
5420 -- Stream operations can appear in user code even if the restriction
5421 -- No_Streams is active (for example, when instantiating a predefined
5422 -- container). In that case rewrite the attribute as a Raise to
5423 -- prevent any run-time use.
5425 if Restriction_Active (No_Streams) then
5426 Rewrite (N,
5427 Make_Raise_Program_Error (Sloc (N),
5428 Reason => PE_Stream_Operation_Not_Allowed));
5429 Set_Etype (N, B_Type);
5430 return;
5431 end if;
5433 -- The simple case, if there is a TSS for Read, just call it
5435 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
5437 if Present (Pname) then
5438 null;
5440 else
5441 -- If there is a Stream_Convert pragma, use it, we rewrite
5443 -- sourcetyp'Read (stream, Item)
5445 -- as
5447 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
5449 -- where strmread is the given Read function that converts an
5450 -- argument of type strmtyp to type sourcetyp or a type from which
5451 -- it is derived. The conversion to sourcetyp is required in the
5452 -- latter case.
5454 -- A special case arises if Item is a type conversion in which
5455 -- case, we have to expand to:
5457 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
5459 -- where Itemx is the expression of the type conversion (i.e.
5460 -- the actual object), and typex is the type of Itemx.
5462 Prag := Get_Stream_Convert_Pragma (P_Type);
5464 if Present (Prag) then
5465 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
5466 Rfunc := Entity (Expression (Arg2));
5467 Lhs := Relocate_Node (Next (First (Exprs)));
5468 Rhs :=
5469 OK_Convert_To (B_Type,
5470 Make_Function_Call (Loc,
5471 Name => New_Occurrence_Of (Rfunc, Loc),
5472 Parameter_Associations => New_List (
5473 Make_Attribute_Reference (Loc,
5474 Prefix =>
5475 New_Occurrence_Of
5476 (Etype (First_Formal (Rfunc)), Loc),
5477 Attribute_Name => Name_Input,
5478 Expressions => New_List (
5479 Relocate_Node (First (Exprs)))))));
5481 if Nkind (Lhs) = N_Type_Conversion then
5482 Lhs := Expression (Lhs);
5483 Rhs := Convert_To (Etype (Lhs), Rhs);
5484 end if;
5486 Rewrite (N,
5487 Make_Assignment_Statement (Loc,
5488 Name => Lhs,
5489 Expression => Rhs));
5490 Set_Assignment_OK (Lhs);
5491 Analyze (N);
5492 return;
5494 -- For elementary types, we call the I_xxx routine using the first
5495 -- parameter and then assign the result into the second parameter.
5496 -- We set Assignment_OK to deal with the conversion case.
5498 elsif Is_Elementary_Type (U_Type) then
5499 declare
5500 Lhs : Node_Id;
5501 Rhs : Node_Id;
5503 begin
5504 Lhs := Relocate_Node (Next (First (Exprs)));
5505 Rhs := Build_Elementary_Input_Call (N);
5507 if Nkind (Lhs) = N_Type_Conversion then
5508 Lhs := Expression (Lhs);
5509 Rhs := Convert_To (Etype (Lhs), Rhs);
5510 end if;
5512 Set_Assignment_OK (Lhs);
5514 Rewrite (N,
5515 Make_Assignment_Statement (Loc,
5516 Name => Lhs,
5517 Expression => Rhs));
5519 Analyze (N);
5520 return;
5521 end;
5523 -- Array type case
5525 elsif Is_Array_Type (U_Type) then
5526 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
5527 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5529 -- Tagged type case, use the primitive Read function. Note that
5530 -- this will dispatch in the class-wide case which is what we want
5532 elsif Is_Tagged_Type (U_Type) then
5533 Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
5535 -- All other record type cases, including protected records. The
5536 -- latter only arise for expander generated code for handling
5537 -- shared passive partition access.
5539 else
5540 pragma Assert
5541 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5543 -- Ada 2005 (AI-216): Program_Error is raised when executing
5544 -- the default implementation of the Read attribute of an
5545 -- Unchecked_Union type. We replace the attribute with a
5546 -- raise statement (rather than inserting it before) to handle
5547 -- properly the case of an unchecked union that is a record
5548 -- component.
5550 if Is_Unchecked_Union (Base_Type (U_Type)) then
5551 Rewrite (N,
5552 Make_Raise_Program_Error (Loc,
5553 Reason => PE_Unchecked_Union_Restriction));
5554 Set_Etype (N, B_Type);
5555 return;
5556 end if;
5558 if Has_Discriminants (U_Type)
5559 and then Present
5560 (Discriminant_Default_Value (First_Discriminant (U_Type)))
5561 then
5562 Build_Mutable_Record_Read_Procedure
5563 (Loc, Full_Base (U_Type), Decl, Pname);
5564 else
5565 Build_Record_Read_Procedure
5566 (Loc, Full_Base (U_Type), Decl, Pname);
5567 end if;
5569 -- Suppress checks, uninitialized or otherwise invalid
5570 -- data does not cause constraint errors to be raised for
5571 -- a complete record read.
5573 Insert_Action (N, Decl, All_Checks);
5574 end if;
5575 end if;
5577 Rewrite_Stream_Proc_Call (Pname);
5578 end Read;
5580 ---------
5581 -- Ref --
5582 ---------
5584 -- Ref is identical to To_Address, see To_Address for processing
5586 ---------------
5587 -- Remainder --
5588 ---------------
5590 -- Transforms 'Remainder into a call to the floating-point attribute
5591 -- function Remainder in Fat_xxx (where xxx is the root type)
5593 when Attribute_Remainder =>
5594 Expand_Fpt_Attribute_RR (N);
5596 ------------
5597 -- Result --
5598 ------------
5600 -- Transform 'Result into reference to _Result formal. At the point
5601 -- where a legal 'Result attribute is expanded, we know that we are in
5602 -- the context of a _Postcondition function with a _Result parameter.
5604 when Attribute_Result =>
5605 Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult));
5606 Analyze_And_Resolve (N, Typ);
5608 -----------
5609 -- Round --
5610 -----------
5612 -- The handling of the Round attribute is quite delicate. The processing
5613 -- in Sem_Attr introduced a conversion to universal real, reflecting the
5614 -- semantics of Round, but we do not want anything to do with universal
5615 -- real at runtime, since this corresponds to using floating-point
5616 -- arithmetic.
5618 -- What we have now is that the Etype of the Round attribute correctly
5619 -- indicates the final result type. The operand of the Round is the
5620 -- conversion to universal real, described above, and the operand of
5621 -- this conversion is the actual operand of Round, which may be the
5622 -- special case of a fixed point multiplication or division (Etype =
5623 -- universal fixed)
5625 -- The exapander will expand first the operand of the conversion, then
5626 -- the conversion, and finally the round attribute itself, since we
5627 -- always work inside out. But we cannot simply process naively in this
5628 -- order. In the semantic world where universal fixed and real really
5629 -- exist and have infinite precision, there is no problem, but in the
5630 -- implementation world, where universal real is a floating-point type,
5631 -- we would get the wrong result.
5633 -- So the approach is as follows. First, when expanding a multiply or
5634 -- divide whose type is universal fixed, we do nothing at all, instead
5635 -- deferring the operation till later.
5637 -- The actual processing is done in Expand_N_Type_Conversion which
5638 -- handles the special case of Round by looking at its parent to see if
5639 -- it is a Round attribute, and if it is, handling the conversion (or
5640 -- its fixed multiply/divide child) in an appropriate manner.
5642 -- This means that by the time we get to expanding the Round attribute
5643 -- itself, the Round is nothing more than a type conversion (and will
5644 -- often be a null type conversion), so we just replace it with the
5645 -- appropriate conversion operation.
5647 when Attribute_Round =>
5648 Rewrite (N,
5649 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
5650 Analyze_And_Resolve (N);
5652 --------------
5653 -- Rounding --
5654 --------------
5656 -- Transforms 'Rounding into a call to the floating-point attribute
5657 -- function Rounding in Fat_xxx (where xxx is the root type)
5658 -- Expansion is avoided for cases the back end can handle directly.
5660 when Attribute_Rounding =>
5661 if not Is_Inline_Floating_Point_Attribute (N) then
5662 Expand_Fpt_Attribute_R (N);
5663 end if;
5665 -------------
5666 -- Scaling --
5667 -------------
5669 -- Transforms 'Scaling into a call to the floating-point attribute
5670 -- function Scaling in Fat_xxx (where xxx is the root type)
5672 when Attribute_Scaling =>
5673 Expand_Fpt_Attribute_RI (N);
5675 -------------------------
5676 -- Simple_Storage_Pool --
5677 -------------------------
5679 when Attribute_Simple_Storage_Pool =>
5680 Rewrite (N,
5681 Make_Type_Conversion (Loc,
5682 Subtype_Mark => New_Occurrence_Of (Etype (N), Loc),
5683 Expression => New_Occurrence_Of (Entity (N), Loc)));
5684 Analyze_And_Resolve (N, Typ);
5686 ----------
5687 -- Size --
5688 ----------
5690 when Attribute_Object_Size
5691 | Attribute_Size
5692 | Attribute_Value_Size
5693 | Attribute_VADS_Size
5695 Size : declare
5696 Siz : Uint;
5697 New_Node : Node_Id;
5699 begin
5700 -- Processing for VADS_Size case. Note that this processing
5701 -- removes all traces of VADS_Size from the tree, and completes
5702 -- all required processing for VADS_Size by translating the
5703 -- attribute reference to an appropriate Size or Object_Size
5704 -- reference.
5706 if Id = Attribute_VADS_Size
5707 or else (Use_VADS_Size and then Id = Attribute_Size)
5708 then
5709 -- If the size is specified, then we simply use the specified
5710 -- size. This applies to both types and objects. The size of an
5711 -- object can be specified in the following ways:
5713 -- An explicit size object is given for an object
5714 -- A component size is specified for an indexed component
5715 -- A component clause is specified for a selected component
5716 -- The object is a component of a packed composite object
5718 -- If the size is specified, then VADS_Size of an object
5720 if (Is_Entity_Name (Pref)
5721 and then Present (Size_Clause (Entity (Pref))))
5722 or else
5723 (Nkind (Pref) = N_Component_Clause
5724 and then (Present (Component_Clause
5725 (Entity (Selector_Name (Pref))))
5726 or else Is_Packed (Etype (Prefix (Pref)))))
5727 or else
5728 (Nkind (Pref) = N_Indexed_Component
5729 and then (Component_Size (Etype (Prefix (Pref))) /= 0
5730 or else Is_Packed (Etype (Prefix (Pref)))))
5731 then
5732 Set_Attribute_Name (N, Name_Size);
5734 -- Otherwise if we have an object rather than a type, then
5735 -- the VADS_Size attribute applies to the type of the object,
5736 -- rather than the object itself. This is one of the respects
5737 -- in which VADS_Size differs from Size.
5739 else
5740 if (not Is_Entity_Name (Pref)
5741 or else not Is_Type (Entity (Pref)))
5742 and then (Is_Scalar_Type (Ptyp)
5743 or else Is_Constrained (Ptyp))
5744 then
5745 Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc));
5746 end if;
5748 -- For a scalar type for which no size was explicitly given,
5749 -- VADS_Size means Object_Size. This is the other respect in
5750 -- which VADS_Size differs from Size.
5752 if Is_Scalar_Type (Ptyp)
5753 and then No (Size_Clause (Ptyp))
5754 then
5755 Set_Attribute_Name (N, Name_Object_Size);
5757 -- In all other cases, Size and VADS_Size are the sane
5759 else
5760 Set_Attribute_Name (N, Name_Size);
5761 end if;
5762 end if;
5763 end if;
5765 -- If the prefix is X'Class, transform it into a direct reference
5766 -- to the class-wide type, because the back end must not see a
5767 -- 'Class reference.
5769 if Is_Entity_Name (Pref)
5770 and then Is_Class_Wide_Type (Entity (Pref))
5771 then
5772 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
5773 return;
5775 -- For X'Size applied to an object of a class-wide type, transform
5776 -- X'Size into a call to the primitive operation _Size applied to
5777 -- X.
5779 elsif Is_Class_Wide_Type (Ptyp) then
5781 -- No need to do anything else compiling under restriction
5782 -- No_Dispatching_Calls. During the semantic analysis we
5783 -- already noted this restriction violation.
5785 if Restriction_Active (No_Dispatching_Calls) then
5786 return;
5787 end if;
5789 New_Node :=
5790 Make_Function_Call (Loc,
5791 Name =>
5792 New_Occurrence_Of (Find_Prim_Op (Ptyp, Name_uSize), Loc),
5793 Parameter_Associations => New_List (Pref));
5795 if Typ /= Standard_Long_Long_Integer then
5797 -- The context is a specific integer type with which the
5798 -- original attribute was compatible. The function has a
5799 -- specific type as well, so to preserve the compatibility
5800 -- we must convert explicitly.
5802 New_Node := Convert_To (Typ, New_Node);
5803 end if;
5805 Rewrite (N, New_Node);
5806 Analyze_And_Resolve (N, Typ);
5807 return;
5809 -- Case of known RM_Size of a type
5811 elsif (Id = Attribute_Size or else Id = Attribute_Value_Size)
5812 and then Is_Entity_Name (Pref)
5813 and then Is_Type (Entity (Pref))
5814 and then Known_Static_RM_Size (Entity (Pref))
5815 then
5816 Siz := RM_Size (Entity (Pref));
5818 -- Case of known Esize of a type
5820 elsif Id = Attribute_Object_Size
5821 and then Is_Entity_Name (Pref)
5822 and then Is_Type (Entity (Pref))
5823 and then Known_Static_Esize (Entity (Pref))
5824 then
5825 Siz := Esize (Entity (Pref));
5827 -- Case of known size of object
5829 elsif Id = Attribute_Size
5830 and then Is_Entity_Name (Pref)
5831 and then Is_Object (Entity (Pref))
5832 and then Known_Esize (Entity (Pref))
5833 and then Known_Static_Esize (Entity (Pref))
5834 then
5835 Siz := Esize (Entity (Pref));
5837 -- For an array component, we can do Size in the front end if the
5838 -- component_size of the array is set.
5840 elsif Nkind (Pref) = N_Indexed_Component then
5841 Siz := Component_Size (Etype (Prefix (Pref)));
5843 -- For a record component, we can do Size in the front end if
5844 -- there is a component clause, or if the record is packed and the
5845 -- component's size is known at compile time.
5847 elsif Nkind (Pref) = N_Selected_Component then
5848 declare
5849 Rec : constant Entity_Id := Etype (Prefix (Pref));
5850 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
5852 begin
5853 if Present (Component_Clause (Comp)) then
5854 Siz := Esize (Comp);
5856 elsif Is_Packed (Rec) then
5857 Siz := RM_Size (Ptyp);
5859 else
5860 Apply_Universal_Integer_Attribute_Checks (N);
5861 return;
5862 end if;
5863 end;
5865 -- All other cases are handled by the back end
5867 else
5868 Apply_Universal_Integer_Attribute_Checks (N);
5870 -- If Size is applied to a formal parameter that is of a packed
5871 -- array subtype, then apply Size to the actual subtype.
5873 if Is_Entity_Name (Pref)
5874 and then Is_Formal (Entity (Pref))
5875 and then Is_Array_Type (Ptyp)
5876 and then Is_Packed (Ptyp)
5877 then
5878 Rewrite (N,
5879 Make_Attribute_Reference (Loc,
5880 Prefix =>
5881 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
5882 Attribute_Name => Name_Size));
5883 Analyze_And_Resolve (N, Typ);
5884 end if;
5886 -- If Size applies to a dereference of an access to
5887 -- unconstrained packed array, the back end needs to see its
5888 -- unconstrained nominal type, but also a hint to the actual
5889 -- constrained type.
5891 if Nkind (Pref) = N_Explicit_Dereference
5892 and then Is_Array_Type (Ptyp)
5893 and then not Is_Constrained (Ptyp)
5894 and then Is_Packed (Ptyp)
5895 then
5896 Set_Actual_Designated_Subtype (Pref,
5897 Get_Actual_Subtype (Pref));
5898 end if;
5900 return;
5901 end if;
5903 -- Common processing for record and array component case
5905 if Siz /= No_Uint and then Siz /= 0 then
5906 declare
5907 CS : constant Boolean := Comes_From_Source (N);
5909 begin
5910 Rewrite (N, Make_Integer_Literal (Loc, Siz));
5912 -- This integer literal is not a static expression. We do
5913 -- not call Analyze_And_Resolve here, because this would
5914 -- activate the circuit for deciding that a static value
5915 -- was out of range, and we don't want that.
5917 -- So just manually set the type, mark the expression as
5918 -- non-static, and then ensure that the result is checked
5919 -- properly if the attribute comes from source (if it was
5920 -- internally generated, we never need a constraint check).
5922 Set_Etype (N, Typ);
5923 Set_Is_Static_Expression (N, False);
5925 if CS then
5926 Apply_Constraint_Check (N, Typ);
5927 end if;
5928 end;
5929 end if;
5930 end Size;
5932 ------------------
5933 -- Storage_Pool --
5934 ------------------
5936 when Attribute_Storage_Pool =>
5937 Rewrite (N,
5938 Make_Type_Conversion (Loc,
5939 Subtype_Mark => New_Occurrence_Of (Etype (N), Loc),
5940 Expression => New_Occurrence_Of (Entity (N), Loc)));
5941 Analyze_And_Resolve (N, Typ);
5943 ------------------
5944 -- Storage_Size --
5945 ------------------
5947 when Attribute_Storage_Size => Storage_Size : declare
5948 Alloc_Op : Entity_Id := Empty;
5950 begin
5952 -- Access type case, always go to the root type
5954 -- The case of access types results in a value of zero for the case
5955 -- where no storage size attribute clause has been given. If a
5956 -- storage size has been given, then the attribute is converted
5957 -- to a reference to the variable used to hold this value.
5959 if Is_Access_Type (Ptyp) then
5960 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
5961 Rewrite (N,
5962 Make_Attribute_Reference (Loc,
5963 Prefix => New_Occurrence_Of (Typ, Loc),
5964 Attribute_Name => Name_Max,
5965 Expressions => New_List (
5966 Make_Integer_Literal (Loc, 0),
5967 Convert_To (Typ,
5968 New_Occurrence_Of
5969 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
5971 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
5973 -- If the access type is associated with a simple storage pool
5974 -- object, then attempt to locate the optional Storage_Size
5975 -- function of the simple storage pool type. If not found,
5976 -- then the result will default to zero.
5978 if Present (Get_Rep_Pragma (Root_Type (Ptyp),
5979 Name_Simple_Storage_Pool_Type))
5980 then
5981 declare
5982 Pool_Type : constant Entity_Id :=
5983 Base_Type (Etype (Entity (N)));
5985 begin
5986 Alloc_Op := Get_Name_Entity_Id (Name_Storage_Size);
5987 while Present (Alloc_Op) loop
5988 if Scope (Alloc_Op) = Scope (Pool_Type)
5989 and then Present (First_Formal (Alloc_Op))
5990 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
5991 then
5992 exit;
5993 end if;
5995 Alloc_Op := Homonym (Alloc_Op);
5996 end loop;
5997 end;
5999 -- In the normal Storage_Pool case, retrieve the primitive
6000 -- function associated with the pool type.
6002 else
6003 Alloc_Op :=
6004 Find_Prim_Op
6005 (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
6006 Attribute_Name (N));
6007 end if;
6009 -- If Storage_Size wasn't found (can only occur in the simple
6010 -- storage pool case), then simply use zero for the result.
6012 if not Present (Alloc_Op) then
6013 Rewrite (N, Make_Integer_Literal (Loc, 0));
6015 -- Otherwise, rewrite the allocator as a call to pool type's
6016 -- Storage_Size function.
6018 else
6019 Rewrite (N,
6020 OK_Convert_To (Typ,
6021 Make_Function_Call (Loc,
6022 Name =>
6023 New_Occurrence_Of (Alloc_Op, Loc),
6025 Parameter_Associations => New_List (
6026 New_Occurrence_Of
6027 (Associated_Storage_Pool
6028 (Root_Type (Ptyp)), Loc)))));
6029 end if;
6031 else
6032 Rewrite (N, Make_Integer_Literal (Loc, 0));
6033 end if;
6035 Analyze_And_Resolve (N, Typ);
6037 -- For tasks, we retrieve the size directly from the TCB. The
6038 -- size may depend on a discriminant of the type, and therefore
6039 -- can be a per-object expression, so type-level information is
6040 -- not sufficient in general. There are four cases to consider:
6042 -- a) If the attribute appears within a task body, the designated
6043 -- TCB is obtained by a call to Self.
6045 -- b) If the prefix of the attribute is the name of a task object,
6046 -- the designated TCB is the one stored in the corresponding record.
6048 -- c) If the prefix is a task type, the size is obtained from the
6049 -- size variable created for each task type
6051 -- d) If no Storage_Size was specified for the type, there is no
6052 -- size variable, and the value is a system-specific default.
6054 else
6055 if In_Open_Scopes (Ptyp) then
6057 -- Storage_Size (Self)
6059 Rewrite (N,
6060 Convert_To (Typ,
6061 Make_Function_Call (Loc,
6062 Name =>
6063 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
6064 Parameter_Associations =>
6065 New_List (
6066 Make_Function_Call (Loc,
6067 Name =>
6068 New_Occurrence_Of (RTE (RE_Self), Loc))))));
6070 elsif not Is_Entity_Name (Pref)
6071 or else not Is_Type (Entity (Pref))
6072 then
6073 -- Storage_Size (Rec (Obj).Size)
6075 Rewrite (N,
6076 Convert_To (Typ,
6077 Make_Function_Call (Loc,
6078 Name =>
6079 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
6080 Parameter_Associations =>
6081 New_List (
6082 Make_Selected_Component (Loc,
6083 Prefix =>
6084 Unchecked_Convert_To (
6085 Corresponding_Record_Type (Ptyp),
6086 New_Copy_Tree (Pref)),
6087 Selector_Name =>
6088 Make_Identifier (Loc, Name_uTask_Id))))));
6090 elsif Present (Storage_Size_Variable (Ptyp)) then
6092 -- Static Storage_Size pragma given for type: retrieve value
6093 -- from its allocated storage variable.
6095 Rewrite (N,
6096 Convert_To (Typ,
6097 Make_Function_Call (Loc,
6098 Name => New_Occurrence_Of (
6099 RTE (RE_Adjust_Storage_Size), Loc),
6100 Parameter_Associations =>
6101 New_List (
6102 New_Occurrence_Of (
6103 Storage_Size_Variable (Ptyp), Loc)))));
6104 else
6105 -- Get system default
6107 Rewrite (N,
6108 Convert_To (Typ,
6109 Make_Function_Call (Loc,
6110 Name =>
6111 New_Occurrence_Of (
6112 RTE (RE_Default_Stack_Size), Loc))));
6113 end if;
6115 Analyze_And_Resolve (N, Typ);
6116 end if;
6117 end Storage_Size;
6119 -----------------
6120 -- Stream_Size --
6121 -----------------
6123 when Attribute_Stream_Size =>
6124 Rewrite (N,
6125 Make_Integer_Literal (Loc, Intval => Get_Stream_Size (Ptyp)));
6126 Analyze_And_Resolve (N, Typ);
6128 ----------
6129 -- Succ --
6130 ----------
6132 -- 1. Deal with enumeration types with holes.
6133 -- 2. For floating-point, generate call to attribute function.
6134 -- 3. For other cases, deal with constraint checking.
6136 when Attribute_Succ => Succ : declare
6137 Etyp : constant Entity_Id := Base_Type (Ptyp);
6139 begin
6140 -- For enumeration types with non-standard representations, we
6141 -- expand typ'Succ (x) into
6143 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
6145 -- If the representation is contiguous, we compute instead
6146 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
6148 if Is_Enumeration_Type (Ptyp)
6149 and then Present (Enum_Pos_To_Rep (Etyp))
6150 then
6151 if Has_Contiguous_Rep (Etyp) then
6152 Rewrite (N,
6153 Unchecked_Convert_To (Ptyp,
6154 Make_Op_Add (Loc,
6155 Left_Opnd =>
6156 Make_Integer_Literal (Loc,
6157 Enumeration_Rep (First_Literal (Ptyp))),
6158 Right_Opnd =>
6159 Make_Function_Call (Loc,
6160 Name =>
6161 New_Occurrence_Of
6162 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6164 Parameter_Associations =>
6165 New_List (
6166 Unchecked_Convert_To (Ptyp,
6167 Make_Op_Add (Loc,
6168 Left_Opnd =>
6169 Unchecked_Convert_To (Standard_Integer,
6170 Relocate_Node (First (Exprs))),
6171 Right_Opnd =>
6172 Make_Integer_Literal (Loc, 1))),
6173 Rep_To_Pos_Flag (Ptyp, Loc))))));
6174 else
6175 -- Add Boolean parameter True, to request program errror if
6176 -- we have a bad representation on our hands. Add False if
6177 -- checks are suppressed.
6179 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
6180 Rewrite (N,
6181 Make_Indexed_Component (Loc,
6182 Prefix =>
6183 New_Occurrence_Of
6184 (Enum_Pos_To_Rep (Etyp), Loc),
6185 Expressions => New_List (
6186 Make_Op_Add (Loc,
6187 Left_Opnd =>
6188 Make_Function_Call (Loc,
6189 Name =>
6190 New_Occurrence_Of
6191 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6192 Parameter_Associations => Exprs),
6193 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
6194 end if;
6196 Analyze_And_Resolve (N, Typ);
6198 -- For floating-point, we transform 'Succ into a call to the Succ
6199 -- floating-point attribute function in Fat_xxx (xxx is root type)
6201 elsif Is_Floating_Point_Type (Ptyp) then
6202 Expand_Fpt_Attribute_R (N);
6203 Analyze_And_Resolve (N, Typ);
6205 -- For modular types, nothing to do (no overflow, since wraps)
6207 elsif Is_Modular_Integer_Type (Ptyp) then
6208 null;
6210 -- For other types, if argument is marked as needing a range check or
6211 -- overflow checking is enabled, we must generate a check.
6213 elsif not Overflow_Checks_Suppressed (Ptyp)
6214 or else Do_Range_Check (First (Exprs))
6215 then
6216 Set_Do_Range_Check (First (Exprs), False);
6217 Expand_Pred_Succ_Attribute (N);
6218 end if;
6219 end Succ;
6221 ---------
6222 -- Tag --
6223 ---------
6225 -- Transforms X'Tag into a direct reference to the tag of X
6227 when Attribute_Tag => Tag : declare
6228 Ttyp : Entity_Id;
6229 Prefix_Is_Type : Boolean;
6231 begin
6232 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
6233 Ttyp := Entity (Pref);
6234 Prefix_Is_Type := True;
6235 else
6236 Ttyp := Ptyp;
6237 Prefix_Is_Type := False;
6238 end if;
6240 if Is_Class_Wide_Type (Ttyp) then
6241 Ttyp := Root_Type (Ttyp);
6242 end if;
6244 Ttyp := Underlying_Type (Ttyp);
6246 -- Ada 2005: The type may be a synchronized tagged type, in which
6247 -- case the tag information is stored in the corresponding record.
6249 if Is_Concurrent_Type (Ttyp) then
6250 Ttyp := Corresponding_Record_Type (Ttyp);
6251 end if;
6253 if Prefix_Is_Type then
6255 -- For VMs we leave the type attribute unexpanded because
6256 -- there's not a dispatching table to reference.
6258 if Tagged_Type_Expansion then
6259 Rewrite (N,
6260 Unchecked_Convert_To (RTE (RE_Tag),
6261 New_Occurrence_Of
6262 (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc)));
6263 Analyze_And_Resolve (N, RTE (RE_Tag));
6264 end if;
6266 -- Ada 2005 (AI-251): The use of 'Tag in the sources always
6267 -- references the primary tag of the actual object. If 'Tag is
6268 -- applied to class-wide interface objects we generate code that
6269 -- displaces "this" to reference the base of the object.
6271 elsif Comes_From_Source (N)
6272 and then Is_Class_Wide_Type (Etype (Prefix (N)))
6273 and then Is_Interface (Underlying_Type (Etype (Prefix (N))))
6274 then
6275 -- Generate:
6276 -- (To_Tag_Ptr (Prefix'Address)).all
6278 -- Note that Prefix'Address is recursively expanded into a call
6279 -- to Base_Address (Obj.Tag)
6281 -- Not needed for VM targets, since all handled by the VM
6283 if Tagged_Type_Expansion then
6284 Rewrite (N,
6285 Make_Explicit_Dereference (Loc,
6286 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6287 Make_Attribute_Reference (Loc,
6288 Prefix => Relocate_Node (Pref),
6289 Attribute_Name => Name_Address))));
6290 Analyze_And_Resolve (N, RTE (RE_Tag));
6291 end if;
6293 else
6294 Rewrite (N,
6295 Make_Selected_Component (Loc,
6296 Prefix => Relocate_Node (Pref),
6297 Selector_Name =>
6298 New_Occurrence_Of (First_Tag_Component (Ttyp), Loc)));
6299 Analyze_And_Resolve (N, RTE (RE_Tag));
6300 end if;
6301 end Tag;
6303 ----------------
6304 -- Terminated --
6305 ----------------
6307 -- Transforms 'Terminated attribute into a call to Terminated function
6309 when Attribute_Terminated => Terminated : begin
6311 -- The prefix of Terminated is of a task interface class-wide type.
6312 -- Generate:
6313 -- terminated (Task_Id (_disp_get_task_id (Pref)));
6315 if Ada_Version >= Ada_2005
6316 and then Ekind (Ptyp) = E_Class_Wide_Type
6317 and then Is_Interface (Ptyp)
6318 and then Is_Task_Interface (Ptyp)
6319 then
6320 Rewrite (N,
6321 Make_Function_Call (Loc,
6322 Name =>
6323 New_Occurrence_Of (RTE (RE_Terminated), Loc),
6324 Parameter_Associations => New_List (
6325 Make_Unchecked_Type_Conversion (Loc,
6326 Subtype_Mark =>
6327 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6328 Expression => Build_Disp_Get_Task_Id_Call (Pref)))));
6330 elsif Restricted_Profile then
6331 Rewrite (N,
6332 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
6334 else
6335 Rewrite (N,
6336 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
6337 end if;
6339 Analyze_And_Resolve (N, Standard_Boolean);
6340 end Terminated;
6342 ----------------
6343 -- To_Address --
6344 ----------------
6346 -- Transforms System'To_Address (X) and System.Address'Ref (X) into
6347 -- unchecked conversion from (integral) type of X to type address.
6349 when Attribute_Ref
6350 | Attribute_To_Address
6352 Rewrite (N,
6353 Unchecked_Convert_To (RTE (RE_Address),
6354 Relocate_Node (First (Exprs))));
6355 Analyze_And_Resolve (N, RTE (RE_Address));
6357 ------------
6358 -- To_Any --
6359 ------------
6361 when Attribute_To_Any => To_Any : declare
6362 P_Type : constant Entity_Id := Etype (Pref);
6363 Decls : constant List_Id := New_List;
6364 begin
6365 Rewrite (N,
6366 Build_To_Any_Call
6367 (Loc,
6368 Convert_To (P_Type,
6369 Relocate_Node (First (Exprs))), Decls));
6370 Insert_Actions (N, Decls);
6371 Analyze_And_Resolve (N, RTE (RE_Any));
6372 end To_Any;
6374 ----------------
6375 -- Truncation --
6376 ----------------
6378 -- Transforms 'Truncation into a call to the floating-point attribute
6379 -- function Truncation in Fat_xxx (where xxx is the root type).
6380 -- Expansion is avoided for cases the back end can handle directly.
6382 when Attribute_Truncation =>
6383 if not Is_Inline_Floating_Point_Attribute (N) then
6384 Expand_Fpt_Attribute_R (N);
6385 end if;
6387 --------------
6388 -- TypeCode --
6389 --------------
6391 when Attribute_TypeCode => TypeCode : declare
6392 P_Type : constant Entity_Id := Etype (Pref);
6393 Decls : constant List_Id := New_List;
6394 begin
6395 Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls));
6396 Insert_Actions (N, Decls);
6397 Analyze_And_Resolve (N, RTE (RE_TypeCode));
6398 end TypeCode;
6400 -----------------------
6401 -- Unbiased_Rounding --
6402 -----------------------
6404 -- Transforms 'Unbiased_Rounding into a call to the floating-point
6405 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
6406 -- root type). Expansion is avoided for cases the back end can handle
6407 -- directly.
6409 when Attribute_Unbiased_Rounding =>
6410 if not Is_Inline_Floating_Point_Attribute (N) then
6411 Expand_Fpt_Attribute_R (N);
6412 end if;
6414 ------------
6415 -- Update --
6416 ------------
6418 when Attribute_Update =>
6419 Expand_Update_Attribute (N);
6421 ---------------
6422 -- VADS_Size --
6423 ---------------
6425 -- The processing for VADS_Size is shared with Size
6427 ---------
6428 -- Val --
6429 ---------
6431 -- For enumeration types with a standard representation, and for all
6432 -- other types, Val is handled by the back end. For enumeration types
6433 -- with a non-standard representation we use the _Pos_To_Rep array that
6434 -- was created when the type was frozen.
6436 when Attribute_Val => Val : declare
6437 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
6439 begin
6440 if Is_Enumeration_Type (Etyp)
6441 and then Present (Enum_Pos_To_Rep (Etyp))
6442 then
6443 if Has_Contiguous_Rep (Etyp) then
6444 declare
6445 Rep_Node : constant Node_Id :=
6446 Unchecked_Convert_To (Etyp,
6447 Make_Op_Add (Loc,
6448 Left_Opnd =>
6449 Make_Integer_Literal (Loc,
6450 Enumeration_Rep (First_Literal (Etyp))),
6451 Right_Opnd =>
6452 (Convert_To (Standard_Integer,
6453 Relocate_Node (First (Exprs))))));
6455 begin
6456 Rewrite (N,
6457 Unchecked_Convert_To (Etyp,
6458 Make_Op_Add (Loc,
6459 Left_Opnd =>
6460 Make_Integer_Literal (Loc,
6461 Enumeration_Rep (First_Literal (Etyp))),
6462 Right_Opnd =>
6463 Make_Function_Call (Loc,
6464 Name =>
6465 New_Occurrence_Of
6466 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6467 Parameter_Associations => New_List (
6468 Rep_Node,
6469 Rep_To_Pos_Flag (Etyp, Loc))))));
6470 end;
6472 else
6473 Rewrite (N,
6474 Make_Indexed_Component (Loc,
6475 Prefix => New_Occurrence_Of (Enum_Pos_To_Rep (Etyp), Loc),
6476 Expressions => New_List (
6477 Convert_To (Standard_Integer,
6478 Relocate_Node (First (Exprs))))));
6479 end if;
6481 Analyze_And_Resolve (N, Typ);
6483 -- If the argument is marked as requiring a range check then generate
6484 -- it here.
6486 elsif Do_Range_Check (First (Exprs)) then
6487 Generate_Range_Check (First (Exprs), Etyp, CE_Range_Check_Failed);
6488 end if;
6489 end Val;
6491 -----------
6492 -- Valid --
6493 -----------
6495 -- The code for valid is dependent on the particular types involved.
6496 -- See separate sections below for the generated code in each case.
6498 when Attribute_Valid => Valid : declare
6499 Btyp : Entity_Id := Base_Type (Ptyp);
6500 Tst : Node_Id;
6502 Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
6503 -- Save the validity checking mode. We always turn off validity
6504 -- checking during process of 'Valid since this is one place
6505 -- where we do not want the implicit validity checks to intefere
6506 -- with the explicit validity check that the programmer is doing.
6508 function Make_Range_Test return Node_Id;
6509 -- Build the code for a range test of the form
6510 -- Btyp!(Pref) in Btyp!(Ptyp'First) .. Btyp!(Ptyp'Last)
6512 ---------------------
6513 -- Make_Range_Test --
6514 ---------------------
6516 function Make_Range_Test return Node_Id is
6517 Temp : Node_Id;
6519 begin
6520 -- The prefix of attribute 'Valid should always denote an object
6521 -- reference. The reference is either coming directly from source
6522 -- or is produced by validity check expansion.
6524 -- If the prefix denotes a variable which captures the value of
6525 -- an object for validation purposes, use the variable in the
6526 -- range test. This ensures that no extra copies or extra reads
6527 -- are produced as part of the test. Generate:
6529 -- Temp : ... := Object;
6530 -- if not Temp in ... then
6532 if Is_Validation_Variable_Reference (Pref) then
6533 Temp := New_Occurrence_Of (Entity (Pref), Loc);
6535 -- Otherwise the prefix is either a source object or a constant
6536 -- produced by validity check expansion. Generate:
6538 -- Temp : constant ... := Pref;
6539 -- if not Temp in ... then
6541 else
6542 Temp := Duplicate_Subexpr (Pref);
6543 end if;
6545 return
6546 Make_In (Loc,
6547 Left_Opnd => Unchecked_Convert_To (Btyp, Temp),
6548 Right_Opnd =>
6549 Make_Range (Loc,
6550 Low_Bound =>
6551 Unchecked_Convert_To (Btyp,
6552 Make_Attribute_Reference (Loc,
6553 Prefix => New_Occurrence_Of (Ptyp, Loc),
6554 Attribute_Name => Name_First)),
6555 High_Bound =>
6556 Unchecked_Convert_To (Btyp,
6557 Make_Attribute_Reference (Loc,
6558 Prefix => New_Occurrence_Of (Ptyp, Loc),
6559 Attribute_Name => Name_Last))));
6560 end Make_Range_Test;
6562 -- Start of processing for Attribute_Valid
6564 begin
6565 -- Do not expand sourced code 'Valid reference in CodePeer mode,
6566 -- will be handled by the back-end directly.
6568 if CodePeer_Mode and then Comes_From_Source (N) then
6569 return;
6570 end if;
6572 -- Turn off validity checks. We do not want any implicit validity
6573 -- checks to intefere with the explicit check from the attribute
6575 Validity_Checks_On := False;
6577 -- Retrieve the base type. Handle the case where the base type is a
6578 -- private enumeration type.
6580 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
6581 Btyp := Full_View (Btyp);
6582 end if;
6584 -- Floating-point case. This case is handled by the Valid attribute
6585 -- code in the floating-point attribute run-time library.
6587 if Is_Floating_Point_Type (Ptyp) then
6588 Float_Valid : declare
6589 Pkg : RE_Id;
6590 Ftp : Entity_Id;
6592 function Get_Fat_Entity (Nam : Name_Id) return Entity_Id;
6593 -- Return entity for Pkg.Nam
6595 --------------------
6596 -- Get_Fat_Entity --
6597 --------------------
6599 function Get_Fat_Entity (Nam : Name_Id) return Entity_Id is
6600 Exp_Name : constant Node_Id :=
6601 Make_Selected_Component (Loc,
6602 Prefix => New_Occurrence_Of (RTE (Pkg), Loc),
6603 Selector_Name => Make_Identifier (Loc, Nam));
6604 begin
6605 Find_Selected_Component (Exp_Name);
6606 return Entity (Exp_Name);
6607 end Get_Fat_Entity;
6609 -- Start of processing for Float_Valid
6611 begin
6612 -- The C and AAMP back-ends handle Valid for fpt types
6614 if Modify_Tree_For_C or else Float_Rep (Btyp) = AAMP then
6615 Analyze_And_Resolve (Pref, Ptyp);
6616 Set_Etype (N, Standard_Boolean);
6617 Set_Analyzed (N);
6619 else
6620 Find_Fat_Info (Ptyp, Ftp, Pkg);
6622 -- If the prefix is a reverse SSO component, or is possibly
6623 -- unaligned, first create a temporary copy that is in
6624 -- native SSO, and properly aligned. Make it Volatile to
6625 -- prevent folding in the back-end. Note that we use an
6626 -- intermediate constrained string type to initialize the
6627 -- temporary, as the value at hand might be invalid, and in
6628 -- that case it cannot be copied using a floating point
6629 -- register.
6631 if In_Reverse_Storage_Order_Object (Pref)
6632 or else Is_Possibly_Unaligned_Object (Pref)
6633 then
6634 declare
6635 Temp : constant Entity_Id :=
6636 Make_Temporary (Loc, 'F');
6638 Fat_S : constant Entity_Id :=
6639 Get_Fat_Entity (Name_S);
6640 -- Constrained string subtype of appropriate size
6642 Fat_P : constant Entity_Id :=
6643 Get_Fat_Entity (Name_P);
6644 -- Access to Fat_S
6646 Decl : constant Node_Id :=
6647 Make_Object_Declaration (Loc,
6648 Defining_Identifier => Temp,
6649 Aliased_Present => True,
6650 Object_Definition =>
6651 New_Occurrence_Of (Ptyp, Loc));
6653 begin
6654 Set_Aspect_Specifications (Decl, New_List (
6655 Make_Aspect_Specification (Loc,
6656 Identifier =>
6657 Make_Identifier (Loc, Name_Volatile))));
6659 Insert_Actions (N,
6660 New_List (
6661 Decl,
6663 Make_Assignment_Statement (Loc,
6664 Name =>
6665 Make_Explicit_Dereference (Loc,
6666 Prefix =>
6667 Unchecked_Convert_To (Fat_P,
6668 Make_Attribute_Reference (Loc,
6669 Prefix =>
6670 New_Occurrence_Of (Temp, Loc),
6671 Attribute_Name =>
6672 Name_Unrestricted_Access))),
6673 Expression =>
6674 Unchecked_Convert_To (Fat_S,
6675 Relocate_Node (Pref)))),
6677 Suppress => All_Checks);
6679 Rewrite (Pref, New_Occurrence_Of (Temp, Loc));
6680 end;
6681 end if;
6683 -- We now have an object of the proper endianness and
6684 -- alignment, and can construct a Valid attribute.
6686 -- We make sure the prefix of this valid attribute is
6687 -- marked as not coming from source, to avoid losing
6688 -- warnings from 'Valid looking like a possible update.
6690 Set_Comes_From_Source (Pref, False);
6692 Expand_Fpt_Attribute
6693 (N, Pkg, Name_Valid,
6694 New_List (
6695 Make_Attribute_Reference (Loc,
6696 Prefix => Unchecked_Convert_To (Ftp, Pref),
6697 Attribute_Name => Name_Unrestricted_Access)));
6698 end if;
6700 -- One more task, we still need a range check. Required
6701 -- only if we have a constraint, since the Valid routine
6702 -- catches infinities properly (infinities are never valid).
6704 -- The way we do the range check is simply to create the
6705 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
6707 if not Subtypes_Statically_Match (Ptyp, Btyp) then
6708 Rewrite (N,
6709 Make_And_Then (Loc,
6710 Left_Opnd => Relocate_Node (N),
6711 Right_Opnd =>
6712 Make_In (Loc,
6713 Left_Opnd => Convert_To (Btyp, Pref),
6714 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
6715 end if;
6716 end Float_Valid;
6718 -- Enumeration type with holes
6720 -- For enumeration types with holes, the Pos value constructed by
6721 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
6722 -- second argument of False returns minus one for an invalid value,
6723 -- and the non-negative pos value for a valid value, so the
6724 -- expansion of X'Valid is simply:
6726 -- type(X)'Pos (X) >= 0
6728 -- We can't quite generate it that way because of the requirement
6729 -- for the non-standard second argument of False in the resulting
6730 -- rep_to_pos call, so we have to explicitly create:
6732 -- _rep_to_pos (X, False) >= 0
6734 -- If we have an enumeration subtype, we also check that the
6735 -- value is in range:
6737 -- _rep_to_pos (X, False) >= 0
6738 -- and then
6739 -- (X >= type(X)'First and then type(X)'Last <= X)
6741 elsif Is_Enumeration_Type (Ptyp)
6742 and then Present (Enum_Pos_To_Rep (Btyp))
6743 then
6744 Tst :=
6745 Make_Op_Ge (Loc,
6746 Left_Opnd =>
6747 Make_Function_Call (Loc,
6748 Name =>
6749 New_Occurrence_Of (TSS (Btyp, TSS_Rep_To_Pos), Loc),
6750 Parameter_Associations => New_List (
6751 Pref,
6752 New_Occurrence_Of (Standard_False, Loc))),
6753 Right_Opnd => Make_Integer_Literal (Loc, 0));
6755 if Ptyp /= Btyp
6756 and then
6757 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
6758 or else
6759 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
6760 then
6761 -- The call to Make_Range_Test will create declarations
6762 -- that need a proper insertion point, but Pref is now
6763 -- attached to a node with no ancestor. Attach to tree
6764 -- even if it is to be rewritten below.
6766 Set_Parent (Tst, Parent (N));
6768 Tst :=
6769 Make_And_Then (Loc,
6770 Left_Opnd => Make_Range_Test,
6771 Right_Opnd => Tst);
6772 end if;
6774 Rewrite (N, Tst);
6776 -- Fortran convention booleans
6778 -- For the very special case of Fortran convention booleans, the
6779 -- value is always valid, since it is an integer with the semantics
6780 -- that non-zero is true, and any value is permissible.
6782 elsif Is_Boolean_Type (Ptyp)
6783 and then Convention (Ptyp) = Convention_Fortran
6784 then
6785 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6787 -- For biased representations, we will be doing an unchecked
6788 -- conversion without unbiasing the result. That means that the range
6789 -- test has to take this into account, and the proper form of the
6790 -- test is:
6792 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
6794 elsif Has_Biased_Representation (Ptyp) then
6795 Btyp := RTE (RE_Unsigned_32);
6796 Rewrite (N,
6797 Make_Op_Lt (Loc,
6798 Left_Opnd =>
6799 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
6800 Right_Opnd =>
6801 Unchecked_Convert_To (Btyp,
6802 Make_Attribute_Reference (Loc,
6803 Prefix => New_Occurrence_Of (Ptyp, Loc),
6804 Attribute_Name => Name_Range_Length))));
6806 -- For all other scalar types, what we want logically is a
6807 -- range test:
6809 -- X in type(X)'First .. type(X)'Last
6811 -- But that's precisely what won't work because of possible
6812 -- unwanted optimization (and indeed the basic motivation for
6813 -- the Valid attribute is exactly that this test does not work).
6814 -- What will work is:
6816 -- Btyp!(X) >= Btyp!(type(X)'First)
6817 -- and then
6818 -- Btyp!(X) <= Btyp!(type(X)'Last)
6820 -- where Btyp is an integer type large enough to cover the full
6821 -- range of possible stored values (i.e. it is chosen on the basis
6822 -- of the size of the type, not the range of the values). We write
6823 -- this as two tests, rather than a range check, so that static
6824 -- evaluation will easily remove either or both of the checks if
6825 -- they can be -statically determined to be true (this happens
6826 -- when the type of X is static and the range extends to the full
6827 -- range of stored values).
6829 -- Unsigned types. Note: it is safe to consider only whether the
6830 -- subtype is unsigned, since we will in that case be doing all
6831 -- unsigned comparisons based on the subtype range. Since we use the
6832 -- actual subtype object size, this is appropriate.
6834 -- For example, if we have
6836 -- subtype x is integer range 1 .. 200;
6837 -- for x'Object_Size use 8;
6839 -- Now the base type is signed, but objects of this type are bits
6840 -- unsigned, and doing an unsigned test of the range 1 to 200 is
6841 -- correct, even though a value greater than 127 looks signed to a
6842 -- signed comparison.
6844 elsif Is_Unsigned_Type (Ptyp) then
6845 if Esize (Ptyp) <= 32 then
6846 Btyp := RTE (RE_Unsigned_32);
6847 else
6848 Btyp := RTE (RE_Unsigned_64);
6849 end if;
6851 Rewrite (N, Make_Range_Test);
6853 -- Signed types
6855 else
6856 if Esize (Ptyp) <= Esize (Standard_Integer) then
6857 Btyp := Standard_Integer;
6858 else
6859 Btyp := Universal_Integer;
6860 end if;
6862 Rewrite (N, Make_Range_Test);
6863 end if;
6865 -- If a predicate is present, then we do the predicate test, even if
6866 -- within the predicate function (infinite recursion is warned about
6867 -- in Sem_Attr in that case).
6869 declare
6870 Pred_Func : constant Entity_Id := Predicate_Function (Ptyp);
6872 begin
6873 if Present (Pred_Func) then
6874 Rewrite (N,
6875 Make_And_Then (Loc,
6876 Left_Opnd => Relocate_Node (N),
6877 Right_Opnd => Make_Predicate_Call (Ptyp, Pref)));
6878 end if;
6879 end;
6881 Analyze_And_Resolve (N, Standard_Boolean);
6882 Validity_Checks_On := Save_Validity_Checks_On;
6883 end Valid;
6885 -------------------
6886 -- Valid_Scalars --
6887 -------------------
6889 when Attribute_Valid_Scalars => Valid_Scalars : declare
6890 Ftyp : Entity_Id;
6892 begin
6893 if Present (Underlying_Type (Ptyp)) then
6894 Ftyp := Underlying_Type (Ptyp);
6895 else
6896 Ftyp := Ptyp;
6897 end if;
6899 -- Replace by True if no scalar parts
6901 if not Scalar_Part_Present (Ftyp) then
6902 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6904 -- For scalar types, Valid_Scalars is the same as Valid
6906 elsif Is_Scalar_Type (Ftyp) then
6907 Rewrite (N,
6908 Make_Attribute_Reference (Loc,
6909 Attribute_Name => Name_Valid,
6910 Prefix => Pref));
6912 -- For array types, we construct a function that determines if there
6913 -- are any non-valid scalar subcomponents, and call the function.
6914 -- We only do this for arrays whose component type needs checking
6916 elsif Is_Array_Type (Ftyp)
6917 and then Scalar_Part_Present (Component_Type (Ftyp))
6918 then
6919 Rewrite (N,
6920 Make_Function_Call (Loc,
6921 Name =>
6922 New_Occurrence_Of (Build_Array_VS_Func (Ftyp, N), Loc),
6923 Parameter_Associations => New_List (Pref)));
6925 -- For record types, we construct a function that determines if there
6926 -- are any non-valid scalar subcomponents, and call the function.
6928 elsif Is_Record_Type (Ftyp)
6929 and then Present (Declaration_Node (Ftyp))
6930 and then Nkind (Type_Definition (Declaration_Node (Ftyp))) =
6931 N_Record_Definition
6932 then
6933 Rewrite (N,
6934 Make_Function_Call (Loc,
6935 Name =>
6936 New_Occurrence_Of (Build_Record_VS_Func (Ftyp, N), Loc),
6937 Parameter_Associations => New_List (Pref)));
6939 -- Other record types or types with discriminants
6941 elsif Is_Record_Type (Ftyp) or else Has_Discriminants (Ptyp) then
6943 -- Build expression with list of equality tests
6945 declare
6946 C : Entity_Id;
6947 X : Node_Id;
6948 A : Name_Id;
6950 begin
6951 X := New_Occurrence_Of (Standard_True, Loc);
6952 C := First_Component_Or_Discriminant (Ptyp);
6953 while Present (C) loop
6954 if not Scalar_Part_Present (Etype (C)) then
6955 goto Continue;
6956 elsif Is_Scalar_Type (Etype (C)) then
6957 A := Name_Valid;
6958 else
6959 A := Name_Valid_Scalars;
6960 end if;
6962 X :=
6963 Make_And_Then (Loc,
6964 Left_Opnd => X,
6965 Right_Opnd =>
6966 Make_Attribute_Reference (Loc,
6967 Attribute_Name => A,
6968 Prefix =>
6969 Make_Selected_Component (Loc,
6970 Prefix =>
6971 Duplicate_Subexpr (Pref, Name_Req => True),
6972 Selector_Name =>
6973 New_Occurrence_Of (C, Loc))));
6974 <<Continue>>
6975 Next_Component_Or_Discriminant (C);
6976 end loop;
6978 Rewrite (N, X);
6979 end;
6981 -- For all other types, result is True
6983 else
6984 Rewrite (N, New_Occurrence_Of (Standard_Boolean, Loc));
6985 end if;
6987 -- Result is always boolean, but never static
6989 Analyze_And_Resolve (N, Standard_Boolean);
6990 Set_Is_Static_Expression (N, False);
6991 end Valid_Scalars;
6993 -----------
6994 -- Value --
6995 -----------
6997 -- Value attribute is handled in separate unit Exp_Imgv
6999 when Attribute_Value =>
7000 Exp_Imgv.Expand_Value_Attribute (N);
7002 -----------------
7003 -- Value_Size --
7004 -----------------
7006 -- The processing for Value_Size shares the processing for Size
7008 -------------
7009 -- Version --
7010 -------------
7012 -- The processing for Version shares the processing for Body_Version
7014 ----------------
7015 -- Wide_Image --
7016 ----------------
7018 -- Wide_Image attribute is handled in separate unit Exp_Imgv
7020 when Attribute_Wide_Image =>
7021 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
7022 -- back-end knows how to handle this attribute directly.
7024 if CodePeer_Mode then
7025 return;
7026 end if;
7028 Exp_Imgv.Expand_Wide_Image_Attribute (N);
7030 ---------------------
7031 -- Wide_Wide_Image --
7032 ---------------------
7034 -- Wide_Wide_Image attribute is handled in separate unit Exp_Imgv
7036 when Attribute_Wide_Wide_Image =>
7037 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
7038 -- back-end knows how to handle this attribute directly.
7040 if CodePeer_Mode then
7041 return;
7042 end if;
7044 Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N);
7046 ----------------
7047 -- Wide_Value --
7048 ----------------
7050 -- We expand typ'Wide_Value (X) into
7052 -- typ'Value
7053 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
7055 -- Wide_String_To_String is a runtime function that converts its wide
7056 -- string argument to String, converting any non-translatable characters
7057 -- into appropriate escape sequences. This preserves the required
7058 -- semantics of Wide_Value in all cases, and results in a very simple
7059 -- implementation approach.
7061 -- Note: for this approach to be fully standard compliant for the cases
7062 -- where typ is Wide_Character and Wide_Wide_Character, the encoding
7063 -- method must cover the entire character range (e.g. UTF-8). But that
7064 -- is a reasonable requirement when dealing with encoded character
7065 -- sequences. Presumably if one of the restrictive encoding mechanisms
7066 -- is in use such as Shift-JIS, then characters that cannot be
7067 -- represented using this encoding will not appear in any case.
7069 when Attribute_Wide_Value =>
7070 Rewrite (N,
7071 Make_Attribute_Reference (Loc,
7072 Prefix => Pref,
7073 Attribute_Name => Name_Value,
7075 Expressions => New_List (
7076 Make_Function_Call (Loc,
7077 Name =>
7078 New_Occurrence_Of (RTE (RE_Wide_String_To_String), Loc),
7080 Parameter_Associations => New_List (
7081 Relocate_Node (First (Exprs)),
7082 Make_Integer_Literal (Loc,
7083 Intval => Int (Wide_Character_Encoding_Method)))))));
7085 Analyze_And_Resolve (N, Typ);
7087 ---------------------
7088 -- Wide_Wide_Value --
7089 ---------------------
7091 -- We expand typ'Wide_Value_Value (X) into
7093 -- typ'Value
7094 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
7096 -- Wide_Wide_String_To_String is a runtime function that converts its
7097 -- wide string argument to String, converting any non-translatable
7098 -- characters into appropriate escape sequences. This preserves the
7099 -- required semantics of Wide_Wide_Value in all cases, and results in a
7100 -- very simple implementation approach.
7102 -- It's not quite right where typ = Wide_Wide_Character, because the
7103 -- encoding method may not cover the whole character type ???
7105 when Attribute_Wide_Wide_Value =>
7106 Rewrite (N,
7107 Make_Attribute_Reference (Loc,
7108 Prefix => Pref,
7109 Attribute_Name => Name_Value,
7111 Expressions => New_List (
7112 Make_Function_Call (Loc,
7113 Name =>
7114 New_Occurrence_Of
7115 (RTE (RE_Wide_Wide_String_To_String), Loc),
7117 Parameter_Associations => New_List (
7118 Relocate_Node (First (Exprs)),
7119 Make_Integer_Literal (Loc,
7120 Intval => Int (Wide_Character_Encoding_Method)))))));
7122 Analyze_And_Resolve (N, Typ);
7124 ---------------------
7125 -- Wide_Wide_Width --
7126 ---------------------
7128 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
7130 when Attribute_Wide_Wide_Width =>
7131 Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide);
7133 ----------------
7134 -- Wide_Width --
7135 ----------------
7137 -- Wide_Width attribute is handled in separate unit Exp_Imgv
7139 when Attribute_Wide_Width =>
7140 Exp_Imgv.Expand_Width_Attribute (N, Wide);
7142 -----------
7143 -- Width --
7144 -----------
7146 -- Width attribute is handled in separate unit Exp_Imgv
7148 when Attribute_Width =>
7149 Exp_Imgv.Expand_Width_Attribute (N, Normal);
7151 -----------
7152 -- Write --
7153 -----------
7155 when Attribute_Write => Write : declare
7156 P_Type : constant Entity_Id := Entity (Pref);
7157 U_Type : constant Entity_Id := Underlying_Type (P_Type);
7158 Pname : Entity_Id;
7159 Decl : Node_Id;
7160 Prag : Node_Id;
7161 Arg3 : Node_Id;
7162 Wfunc : Node_Id;
7164 begin
7165 -- If no underlying type, we have an error that will be diagnosed
7166 -- elsewhere, so here we just completely ignore the expansion.
7168 if No (U_Type) then
7169 return;
7170 end if;
7172 -- Stream operations can appear in user code even if the restriction
7173 -- No_Streams is active (for example, when instantiating a predefined
7174 -- container). In that case rewrite the attribute as a Raise to
7175 -- prevent any run-time use.
7177 if Restriction_Active (No_Streams) then
7178 Rewrite (N,
7179 Make_Raise_Program_Error (Sloc (N),
7180 Reason => PE_Stream_Operation_Not_Allowed));
7181 Set_Etype (N, U_Type);
7182 return;
7183 end if;
7185 -- The simple case, if there is a TSS for Write, just call it
7187 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
7189 if Present (Pname) then
7190 null;
7192 else
7193 -- If there is a Stream_Convert pragma, use it, we rewrite
7195 -- sourcetyp'Output (stream, Item)
7197 -- as
7199 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
7201 -- where strmwrite is the given Write function that converts an
7202 -- argument of type sourcetyp or a type acctyp, from which it is
7203 -- derived to type strmtyp. The conversion to acttyp is required
7204 -- for the derived case.
7206 Prag := Get_Stream_Convert_Pragma (P_Type);
7208 if Present (Prag) then
7209 Arg3 :=
7210 Next (Next (First (Pragma_Argument_Associations (Prag))));
7211 Wfunc := Entity (Expression (Arg3));
7213 Rewrite (N,
7214 Make_Attribute_Reference (Loc,
7215 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
7216 Attribute_Name => Name_Output,
7217 Expressions => New_List (
7218 Relocate_Node (First (Exprs)),
7219 Make_Function_Call (Loc,
7220 Name => New_Occurrence_Of (Wfunc, Loc),
7221 Parameter_Associations => New_List (
7222 OK_Convert_To (Etype (First_Formal (Wfunc)),
7223 Relocate_Node (Next (First (Exprs)))))))));
7225 Analyze (N);
7226 return;
7228 -- For elementary types, we call the W_xxx routine directly
7230 elsif Is_Elementary_Type (U_Type) then
7231 Rewrite (N, Build_Elementary_Write_Call (N));
7232 Analyze (N);
7233 return;
7235 -- Array type case
7237 elsif Is_Array_Type (U_Type) then
7238 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
7239 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
7241 -- Tagged type case, use the primitive Write function. Note that
7242 -- this will dispatch in the class-wide case which is what we want
7244 elsif Is_Tagged_Type (U_Type) then
7245 Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
7247 -- All other record type cases, including protected records.
7248 -- The latter only arise for expander generated code for
7249 -- handling shared passive partition access.
7251 else
7252 pragma Assert
7253 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
7255 -- Ada 2005 (AI-216): Program_Error is raised when executing
7256 -- the default implementation of the Write attribute of an
7257 -- Unchecked_Union type. However, if the 'Write reference is
7258 -- within the generated Output stream procedure, Write outputs
7259 -- the components, and the default values of the discriminant
7260 -- are streamed by the Output procedure itself. If there are
7261 -- no default values this is also erroneous.
7263 if Is_Unchecked_Union (Base_Type (U_Type)) then
7264 if (not Is_TSS (Current_Scope, TSS_Stream_Output)
7265 and not Is_TSS (Current_Scope, TSS_Stream_Write))
7266 or else No (Discriminant_Default_Value
7267 (First_Discriminant (U_Type)))
7268 then
7269 Rewrite (N,
7270 Make_Raise_Program_Error (Loc,
7271 Reason => PE_Unchecked_Union_Restriction));
7272 Set_Etype (N, U_Type);
7273 return;
7274 end if;
7275 end if;
7277 if Has_Discriminants (U_Type)
7278 and then Present
7279 (Discriminant_Default_Value (First_Discriminant (U_Type)))
7280 then
7281 Build_Mutable_Record_Write_Procedure
7282 (Loc, Full_Base (U_Type), Decl, Pname);
7283 else
7284 Build_Record_Write_Procedure
7285 (Loc, Full_Base (U_Type), Decl, Pname);
7286 end if;
7288 Insert_Action (N, Decl);
7289 end if;
7290 end if;
7292 -- If we fall through, Pname is the procedure to be called
7294 Rewrite_Stream_Proc_Call (Pname);
7295 end Write;
7297 -- Component_Size is handled by the back end, unless the component size
7298 -- is known at compile time, which is always true in the packed array
7299 -- case. It is important that the packed array case is handled in the
7300 -- front end (see Eval_Attribute) since the back end would otherwise get
7301 -- confused by the equivalent packed array type.
7303 when Attribute_Component_Size =>
7304 null;
7306 -- The following attributes are handled by the back end (except that
7307 -- static cases have already been evaluated during semantic processing,
7308 -- but in any case the back end should not count on this).
7310 -- The back end also handles the non-class-wide cases of Size
7312 when Attribute_Bit_Order
7313 | Attribute_Code_Address
7314 | Attribute_Definite
7315 | Attribute_Deref
7316 | Attribute_Null_Parameter
7317 | Attribute_Passed_By_Reference
7318 | Attribute_Pool_Address
7319 | Attribute_Scalar_Storage_Order
7321 null;
7323 -- The following attributes are also handled by the back end, but return
7324 -- a universal integer result, so may need a conversion for checking
7325 -- that the result is in range.
7327 when Attribute_Aft
7328 | Attribute_Max_Alignment_For_Allocation
7330 Apply_Universal_Integer_Attribute_Checks (N);
7332 -- The following attributes should not appear at this stage, since they
7333 -- have already been handled by the analyzer (and properly rewritten
7334 -- with corresponding values or entities to represent the right values)
7336 when Attribute_Abort_Signal
7337 | Attribute_Address_Size
7338 | Attribute_Atomic_Always_Lock_Free
7339 | Attribute_Base
7340 | Attribute_Class
7341 | Attribute_Compiler_Version
7342 | Attribute_Default_Bit_Order
7343 | Attribute_Default_Scalar_Storage_Order
7344 | Attribute_Delta
7345 | Attribute_Denorm
7346 | Attribute_Digits
7347 | Attribute_Emax
7348 | Attribute_Enabled
7349 | Attribute_Epsilon
7350 | Attribute_Fast_Math
7351 | Attribute_First_Valid
7352 | Attribute_Has_Access_Values
7353 | Attribute_Has_Discriminants
7354 | Attribute_Has_Tagged_Values
7355 | Attribute_Large
7356 | Attribute_Last_Valid
7357 | Attribute_Library_Level
7358 | Attribute_Lock_Free
7359 | Attribute_Machine_Emax
7360 | Attribute_Machine_Emin
7361 | Attribute_Machine_Mantissa
7362 | Attribute_Machine_Overflows
7363 | Attribute_Machine_Radix
7364 | Attribute_Machine_Rounds
7365 | Attribute_Maximum_Alignment
7366 | Attribute_Model_Emin
7367 | Attribute_Model_Epsilon
7368 | Attribute_Model_Mantissa
7369 | Attribute_Model_Small
7370 | Attribute_Modulus
7371 | Attribute_Partition_ID
7372 | Attribute_Range
7373 | Attribute_Restriction_Set
7374 | Attribute_Safe_Emax
7375 | Attribute_Safe_First
7376 | Attribute_Safe_Large
7377 | Attribute_Safe_Last
7378 | Attribute_Safe_Small
7379 | Attribute_Scale
7380 | Attribute_Signed_Zeros
7381 | Attribute_Small
7382 | Attribute_Storage_Unit
7383 | Attribute_Stub_Type
7384 | Attribute_System_Allocator_Alignment
7385 | Attribute_Target_Name
7386 | Attribute_Type_Class
7387 | Attribute_Type_Key
7388 | Attribute_Unconstrained_Array
7389 | Attribute_Universal_Literal_String
7390 | Attribute_Wchar_T_Size
7391 | Attribute_Word_Size
7393 raise Program_Error;
7395 -- The Asm_Input and Asm_Output attributes are not expanded at this
7396 -- stage, but will be eliminated in the expansion of the Asm call, see
7397 -- Exp_Intr for details. So the back end will never see these either.
7399 when Attribute_Asm_Input
7400 | Attribute_Asm_Output
7402 null;
7403 end case;
7405 -- Note: as mentioned earlier, individual sections of the above case
7406 -- statement assume there is no code after the case statement, and are
7407 -- legitimately allowed to execute return statements if they have nothing
7408 -- more to do, so DO NOT add code at this point.
7410 exception
7411 when RE_Not_Available =>
7412 return;
7413 end Expand_N_Attribute_Reference;
7415 --------------------------------
7416 -- Expand_Pred_Succ_Attribute --
7417 --------------------------------
7419 -- For typ'Pred (exp), we generate the check
7421 -- [constraint_error when exp = typ'Base'First]
7423 -- Similarly, for typ'Succ (exp), we generate the check
7425 -- [constraint_error when exp = typ'Base'Last]
7427 -- These checks are not generated for modular types, since the proper
7428 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
7429 -- We also suppress these checks if we are the right side of an assignment
7430 -- statement or the expression of an object declaration, where the flag
7431 -- Suppress_Assignment_Checks is set for the assignment/declaration.
7433 procedure Expand_Pred_Succ_Attribute (N : Node_Id) is
7434 Loc : constant Source_Ptr := Sloc (N);
7435 P : constant Node_Id := Parent (N);
7436 Cnam : Name_Id;
7438 begin
7439 if Attribute_Name (N) = Name_Pred then
7440 Cnam := Name_First;
7441 else
7442 Cnam := Name_Last;
7443 end if;
7445 if not Nkind_In (P, N_Assignment_Statement, N_Object_Declaration)
7446 or else not Suppress_Assignment_Checks (P)
7447 then
7448 Insert_Action (N,
7449 Make_Raise_Constraint_Error (Loc,
7450 Condition =>
7451 Make_Op_Eq (Loc,
7452 Left_Opnd =>
7453 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
7454 Right_Opnd =>
7455 Make_Attribute_Reference (Loc,
7456 Prefix =>
7457 New_Occurrence_Of (Base_Type (Etype (Prefix (N))), Loc),
7458 Attribute_Name => Cnam)),
7459 Reason => CE_Overflow_Check_Failed));
7460 end if;
7461 end Expand_Pred_Succ_Attribute;
7463 -----------------------------
7464 -- Expand_Update_Attribute --
7465 -----------------------------
7467 procedure Expand_Update_Attribute (N : Node_Id) is
7468 procedure Process_Component_Or_Element_Update
7469 (Temp : Entity_Id;
7470 Comp : Node_Id;
7471 Expr : Node_Id;
7472 Typ : Entity_Id);
7473 -- Generate the statements necessary to update a single component or an
7474 -- element of the prefix. The code is inserted before the attribute N.
7475 -- Temp denotes the entity of the anonymous object created to reflect
7476 -- the changes in values. Comp is the component/index expression to be
7477 -- updated. Expr is an expression yielding the new value of Comp. Typ
7478 -- is the type of the prefix of attribute Update.
7480 procedure Process_Range_Update
7481 (Temp : Entity_Id;
7482 Comp : Node_Id;
7483 Expr : Node_Id;
7484 Typ : Entity_Id);
7485 -- Generate the statements necessary to update a slice of the prefix.
7486 -- The code is inserted before the attribute N. Temp denotes the entity
7487 -- of the anonymous object created to reflect the changes in values.
7488 -- Comp is range of the slice to be updated. Expr is an expression
7489 -- yielding the new value of Comp. Typ is the type of the prefix of
7490 -- attribute Update.
7492 -----------------------------------------
7493 -- Process_Component_Or_Element_Update --
7494 -----------------------------------------
7496 procedure Process_Component_Or_Element_Update
7497 (Temp : Entity_Id;
7498 Comp : Node_Id;
7499 Expr : Node_Id;
7500 Typ : Entity_Id)
7502 Loc : constant Source_Ptr := Sloc (Comp);
7503 Exprs : List_Id;
7504 LHS : Node_Id;
7506 begin
7507 -- An array element may be modified by the following relations
7508 -- depending on the number of dimensions:
7510 -- 1 => Expr -- one dimensional update
7511 -- (1, ..., N) => Expr -- multi dimensional update
7513 -- The above forms are converted in assignment statements where the
7514 -- left hand side is an indexed component:
7516 -- Temp (1) := Expr; -- one dimensional update
7517 -- Temp (1, ..., N) := Expr; -- multi dimensional update
7519 if Is_Array_Type (Typ) then
7521 -- The index expressions of a multi dimensional array update
7522 -- appear as an aggregate.
7524 if Nkind (Comp) = N_Aggregate then
7525 Exprs := New_Copy_List_Tree (Expressions (Comp));
7526 else
7527 Exprs := New_List (Relocate_Node (Comp));
7528 end if;
7530 LHS :=
7531 Make_Indexed_Component (Loc,
7532 Prefix => New_Occurrence_Of (Temp, Loc),
7533 Expressions => Exprs);
7535 -- A record component update appears in the following form:
7537 -- Comp => Expr
7539 -- The above relation is transformed into an assignment statement
7540 -- where the left hand side is a selected component:
7542 -- Temp.Comp := Expr;
7544 else pragma Assert (Is_Record_Type (Typ));
7545 LHS :=
7546 Make_Selected_Component (Loc,
7547 Prefix => New_Occurrence_Of (Temp, Loc),
7548 Selector_Name => Relocate_Node (Comp));
7549 end if;
7551 Insert_Action (N,
7552 Make_Assignment_Statement (Loc,
7553 Name => LHS,
7554 Expression => Relocate_Node (Expr)));
7555 end Process_Component_Or_Element_Update;
7557 --------------------------
7558 -- Process_Range_Update --
7559 --------------------------
7561 procedure Process_Range_Update
7562 (Temp : Entity_Id;
7563 Comp : Node_Id;
7564 Expr : Node_Id;
7565 Typ : Entity_Id)
7567 Index_Typ : constant Entity_Id := Etype (First_Index (Typ));
7568 Loc : constant Source_Ptr := Sloc (Comp);
7569 Index : Entity_Id;
7571 begin
7572 -- A range update appears as
7574 -- (Low .. High => Expr)
7576 -- The above construct is transformed into a loop that iterates over
7577 -- the given range and modifies the corresponding array values to the
7578 -- value of Expr:
7580 -- for Index in Low .. High loop
7581 -- Temp (<Index_Typ> (Index)) := Expr;
7582 -- end loop;
7584 Index := Make_Temporary (Loc, 'I');
7586 Insert_Action (N,
7587 Make_Loop_Statement (Loc,
7588 Iteration_Scheme =>
7589 Make_Iteration_Scheme (Loc,
7590 Loop_Parameter_Specification =>
7591 Make_Loop_Parameter_Specification (Loc,
7592 Defining_Identifier => Index,
7593 Discrete_Subtype_Definition => Relocate_Node (Comp))),
7595 Statements => New_List (
7596 Make_Assignment_Statement (Loc,
7597 Name =>
7598 Make_Indexed_Component (Loc,
7599 Prefix => New_Occurrence_Of (Temp, Loc),
7600 Expressions => New_List (
7601 Convert_To (Index_Typ,
7602 New_Occurrence_Of (Index, Loc)))),
7603 Expression => Relocate_Node (Expr))),
7605 End_Label => Empty));
7606 end Process_Range_Update;
7608 -- Local variables
7610 Aggr : constant Node_Id := First (Expressions (N));
7611 Loc : constant Source_Ptr := Sloc (N);
7612 Pref : constant Node_Id := Prefix (N);
7613 Typ : constant Entity_Id := Etype (Pref);
7614 Assoc : Node_Id;
7615 Comp : Node_Id;
7616 CW_Temp : Entity_Id;
7617 CW_Typ : Entity_Id;
7618 Expr : Node_Id;
7619 Temp : Entity_Id;
7621 -- Start of processing for Expand_Update_Attribute
7623 begin
7624 -- Create the anonymous object to store the value of the prefix and
7625 -- capture subsequent changes in value.
7627 Temp := Make_Temporary (Loc, 'T', Pref);
7629 -- Preserve the tag of the prefix by offering a specific view of the
7630 -- class-wide version of the prefix.
7632 if Is_Tagged_Type (Typ) then
7634 -- Generate:
7635 -- CW_Temp : Typ'Class := Typ'Class (Pref);
7637 CW_Temp := Make_Temporary (Loc, 'T');
7638 CW_Typ := Class_Wide_Type (Typ);
7640 Insert_Action (N,
7641 Make_Object_Declaration (Loc,
7642 Defining_Identifier => CW_Temp,
7643 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
7644 Expression =>
7645 Convert_To (CW_Typ, Relocate_Node (Pref))));
7647 -- Generate:
7648 -- Temp : Typ renames Typ (CW_Temp);
7650 Insert_Action (N,
7651 Make_Object_Renaming_Declaration (Loc,
7652 Defining_Identifier => Temp,
7653 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
7654 Name =>
7655 Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc))));
7657 -- Non-tagged case
7659 else
7660 -- Generate:
7661 -- Temp : Typ := Pref;
7663 Insert_Action (N,
7664 Make_Object_Declaration (Loc,
7665 Defining_Identifier => Temp,
7666 Object_Definition => New_Occurrence_Of (Typ, Loc),
7667 Expression => Relocate_Node (Pref)));
7668 end if;
7670 -- Process the update aggregate
7672 Assoc := First (Component_Associations (Aggr));
7673 while Present (Assoc) loop
7674 Comp := First (Choices (Assoc));
7675 Expr := Expression (Assoc);
7676 while Present (Comp) loop
7677 if Nkind (Comp) = N_Range then
7678 Process_Range_Update (Temp, Comp, Expr, Typ);
7679 else
7680 Process_Component_Or_Element_Update (Temp, Comp, Expr, Typ);
7681 end if;
7683 Next (Comp);
7684 end loop;
7686 Next (Assoc);
7687 end loop;
7689 -- The attribute is replaced by a reference to the anonymous object
7691 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7692 Analyze (N);
7693 end Expand_Update_Attribute;
7695 -------------------
7696 -- Find_Fat_Info --
7697 -------------------
7699 procedure Find_Fat_Info
7700 (T : Entity_Id;
7701 Fat_Type : out Entity_Id;
7702 Fat_Pkg : out RE_Id)
7704 Rtyp : constant Entity_Id := Root_Type (T);
7706 begin
7707 -- All we do is use the root type (historically this dealt with
7708 -- VAX-float .. to be cleaned up further later ???)
7710 Fat_Type := Rtyp;
7712 if Fat_Type = Standard_Short_Float then
7713 Fat_Pkg := RE_Attr_Short_Float;
7715 elsif Fat_Type = Standard_Float then
7716 Fat_Pkg := RE_Attr_Float;
7718 elsif Fat_Type = Standard_Long_Float then
7719 Fat_Pkg := RE_Attr_Long_Float;
7721 elsif Fat_Type = Standard_Long_Long_Float then
7722 Fat_Pkg := RE_Attr_Long_Long_Float;
7724 -- Universal real (which is its own root type) is treated as being
7725 -- equivalent to Standard.Long_Long_Float, since it is defined to
7726 -- have the same precision as the longest Float type.
7728 elsif Fat_Type = Universal_Real then
7729 Fat_Type := Standard_Long_Long_Float;
7730 Fat_Pkg := RE_Attr_Long_Long_Float;
7732 else
7733 raise Program_Error;
7734 end if;
7735 end Find_Fat_Info;
7737 ----------------------------
7738 -- Find_Stream_Subprogram --
7739 ----------------------------
7741 function Find_Stream_Subprogram
7742 (Typ : Entity_Id;
7743 Nam : TSS_Name_Type) return Entity_Id
7745 Base_Typ : constant Entity_Id := Base_Type (Typ);
7746 Ent : constant Entity_Id := TSS (Typ, Nam);
7748 function Is_Available (Entity : RE_Id) return Boolean;
7749 pragma Inline (Is_Available);
7750 -- Function to check whether the specified run-time call is available
7751 -- in the run time used. In the case of a configurable run time, it
7752 -- is normal that some subprograms are not there.
7754 -- I don't understand this routine at all, why is this not just a
7755 -- call to RTE_Available? And if for some reason we need a different
7756 -- routine with different semantics, why is not in Rtsfind ???
7758 ------------------
7759 -- Is_Available --
7760 ------------------
7762 function Is_Available (Entity : RE_Id) return Boolean is
7763 begin
7764 -- Assume that the unit will always be available when using a
7765 -- "normal" (not configurable) run time.
7767 return not Configurable_Run_Time_Mode or else RTE_Available (Entity);
7768 end Is_Available;
7770 -- Start of processing for Find_Stream_Subprogram
7772 begin
7773 if Present (Ent) then
7774 return Ent;
7775 end if;
7777 -- Stream attributes for strings are expanded into library calls. The
7778 -- following checks are disabled when the run-time is not available or
7779 -- when compiling predefined types due to bootstrap issues. As a result,
7780 -- the compiler will generate in-place stream routines for string types
7781 -- that appear in GNAT's library, but will generate calls via rtsfind
7782 -- to library routines for user code.
7784 -- Note: In the case of using a configurable run time, it is very likely
7785 -- that stream routines for string types are not present (they require
7786 -- file system support). In this case, the specific stream routines for
7787 -- strings are not used, relying on the regular stream mechanism
7788 -- instead. That is why we include the test Is_Available when dealing
7789 -- with these cases.
7791 if not Is_Predefined_Unit (Current_Sem_Unit) then
7792 -- Storage_Array as defined in package System.Storage_Elements
7794 if Is_RTE (Base_Typ, RE_Storage_Array) then
7796 -- Case of No_Stream_Optimizations restriction active
7798 if Restriction_Active (No_Stream_Optimizations) then
7799 if Nam = TSS_Stream_Input
7800 and then Is_Available (RE_Storage_Array_Input)
7801 then
7802 return RTE (RE_Storage_Array_Input);
7804 elsif Nam = TSS_Stream_Output
7805 and then Is_Available (RE_Storage_Array_Output)
7806 then
7807 return RTE (RE_Storage_Array_Output);
7809 elsif Nam = TSS_Stream_Read
7810 and then Is_Available (RE_Storage_Array_Read)
7811 then
7812 return RTE (RE_Storage_Array_Read);
7814 elsif Nam = TSS_Stream_Write
7815 and then Is_Available (RE_Storage_Array_Write)
7816 then
7817 return RTE (RE_Storage_Array_Write);
7819 elsif Nam /= TSS_Stream_Input and then
7820 Nam /= TSS_Stream_Output and then
7821 Nam /= TSS_Stream_Read and then
7822 Nam /= TSS_Stream_Write
7823 then
7824 raise Program_Error;
7825 end if;
7827 -- Restriction No_Stream_Optimizations is not set, so we can go
7828 -- ahead and optimize using the block IO forms of the routines.
7830 else
7831 if Nam = TSS_Stream_Input
7832 and then Is_Available (RE_Storage_Array_Input_Blk_IO)
7833 then
7834 return RTE (RE_Storage_Array_Input_Blk_IO);
7836 elsif Nam = TSS_Stream_Output
7837 and then Is_Available (RE_Storage_Array_Output_Blk_IO)
7838 then
7839 return RTE (RE_Storage_Array_Output_Blk_IO);
7841 elsif Nam = TSS_Stream_Read
7842 and then Is_Available (RE_Storage_Array_Read_Blk_IO)
7843 then
7844 return RTE (RE_Storage_Array_Read_Blk_IO);
7846 elsif Nam = TSS_Stream_Write
7847 and then Is_Available (RE_Storage_Array_Write_Blk_IO)
7848 then
7849 return RTE (RE_Storage_Array_Write_Blk_IO);
7851 elsif Nam /= TSS_Stream_Input and then
7852 Nam /= TSS_Stream_Output and then
7853 Nam /= TSS_Stream_Read and then
7854 Nam /= TSS_Stream_Write
7855 then
7856 raise Program_Error;
7857 end if;
7858 end if;
7860 -- Stream_Element_Array as defined in package Ada.Streams
7862 elsif Is_RTE (Base_Typ, RE_Stream_Element_Array) then
7864 -- Case of No_Stream_Optimizations restriction active
7866 if Restriction_Active (No_Stream_Optimizations) then
7867 if Nam = TSS_Stream_Input
7868 and then Is_Available (RE_Stream_Element_Array_Input)
7869 then
7870 return RTE (RE_Stream_Element_Array_Input);
7872 elsif Nam = TSS_Stream_Output
7873 and then Is_Available (RE_Stream_Element_Array_Output)
7874 then
7875 return RTE (RE_Stream_Element_Array_Output);
7877 elsif Nam = TSS_Stream_Read
7878 and then Is_Available (RE_Stream_Element_Array_Read)
7879 then
7880 return RTE (RE_Stream_Element_Array_Read);
7882 elsif Nam = TSS_Stream_Write
7883 and then Is_Available (RE_Stream_Element_Array_Write)
7884 then
7885 return RTE (RE_Stream_Element_Array_Write);
7887 elsif Nam /= TSS_Stream_Input and then
7888 Nam /= TSS_Stream_Output and then
7889 Nam /= TSS_Stream_Read and then
7890 Nam /= TSS_Stream_Write
7891 then
7892 raise Program_Error;
7893 end if;
7895 -- Restriction No_Stream_Optimizations is not set, so we can go
7896 -- ahead and optimize using the block IO forms of the routines.
7898 else
7899 if Nam = TSS_Stream_Input
7900 and then Is_Available (RE_Stream_Element_Array_Input_Blk_IO)
7901 then
7902 return RTE (RE_Stream_Element_Array_Input_Blk_IO);
7904 elsif Nam = TSS_Stream_Output
7905 and then Is_Available (RE_Stream_Element_Array_Output_Blk_IO)
7906 then
7907 return RTE (RE_Stream_Element_Array_Output_Blk_IO);
7909 elsif Nam = TSS_Stream_Read
7910 and then Is_Available (RE_Stream_Element_Array_Read_Blk_IO)
7911 then
7912 return RTE (RE_Stream_Element_Array_Read_Blk_IO);
7914 elsif Nam = TSS_Stream_Write
7915 and then Is_Available (RE_Stream_Element_Array_Write_Blk_IO)
7916 then
7917 return RTE (RE_Stream_Element_Array_Write_Blk_IO);
7919 elsif Nam /= TSS_Stream_Input and then
7920 Nam /= TSS_Stream_Output and then
7921 Nam /= TSS_Stream_Read and then
7922 Nam /= TSS_Stream_Write
7923 then
7924 raise Program_Error;
7925 end if;
7926 end if;
7928 -- String as defined in package Ada
7930 elsif Base_Typ = Standard_String then
7932 -- Case of No_Stream_Optimizations restriction active
7934 if Restriction_Active (No_Stream_Optimizations) then
7935 if Nam = TSS_Stream_Input
7936 and then Is_Available (RE_String_Input)
7937 then
7938 return RTE (RE_String_Input);
7940 elsif Nam = TSS_Stream_Output
7941 and then Is_Available (RE_String_Output)
7942 then
7943 return RTE (RE_String_Output);
7945 elsif Nam = TSS_Stream_Read
7946 and then Is_Available (RE_String_Read)
7947 then
7948 return RTE (RE_String_Read);
7950 elsif Nam = TSS_Stream_Write
7951 and then Is_Available (RE_String_Write)
7952 then
7953 return RTE (RE_String_Write);
7955 elsif Nam /= TSS_Stream_Input and then
7956 Nam /= TSS_Stream_Output and then
7957 Nam /= TSS_Stream_Read and then
7958 Nam /= TSS_Stream_Write
7959 then
7960 raise Program_Error;
7961 end if;
7963 -- Restriction No_Stream_Optimizations is not set, so we can go
7964 -- ahead and optimize using the block IO forms of the routines.
7966 else
7967 if Nam = TSS_Stream_Input
7968 and then Is_Available (RE_String_Input_Blk_IO)
7969 then
7970 return RTE (RE_String_Input_Blk_IO);
7972 elsif Nam = TSS_Stream_Output
7973 and then Is_Available (RE_String_Output_Blk_IO)
7974 then
7975 return RTE (RE_String_Output_Blk_IO);
7977 elsif Nam = TSS_Stream_Read
7978 and then Is_Available (RE_String_Read_Blk_IO)
7979 then
7980 return RTE (RE_String_Read_Blk_IO);
7982 elsif Nam = TSS_Stream_Write
7983 and then Is_Available (RE_String_Write_Blk_IO)
7984 then
7985 return RTE (RE_String_Write_Blk_IO);
7987 elsif Nam /= TSS_Stream_Input and then
7988 Nam /= TSS_Stream_Output and then
7989 Nam /= TSS_Stream_Read and then
7990 Nam /= TSS_Stream_Write
7991 then
7992 raise Program_Error;
7993 end if;
7994 end if;
7996 -- Wide_String as defined in package Ada
7998 elsif Base_Typ = Standard_Wide_String then
8000 -- Case of No_Stream_Optimizations restriction active
8002 if Restriction_Active (No_Stream_Optimizations) then
8003 if Nam = TSS_Stream_Input
8004 and then Is_Available (RE_Wide_String_Input)
8005 then
8006 return RTE (RE_Wide_String_Input);
8008 elsif Nam = TSS_Stream_Output
8009 and then Is_Available (RE_Wide_String_Output)
8010 then
8011 return RTE (RE_Wide_String_Output);
8013 elsif Nam = TSS_Stream_Read
8014 and then Is_Available (RE_Wide_String_Read)
8015 then
8016 return RTE (RE_Wide_String_Read);
8018 elsif Nam = TSS_Stream_Write
8019 and then Is_Available (RE_Wide_String_Write)
8020 then
8021 return RTE (RE_Wide_String_Write);
8023 elsif Nam /= TSS_Stream_Input and then
8024 Nam /= TSS_Stream_Output and then
8025 Nam /= TSS_Stream_Read and then
8026 Nam /= TSS_Stream_Write
8027 then
8028 raise Program_Error;
8029 end if;
8031 -- Restriction No_Stream_Optimizations is not set, so we can go
8032 -- ahead and optimize using the block IO forms of the routines.
8034 else
8035 if Nam = TSS_Stream_Input
8036 and then Is_Available (RE_Wide_String_Input_Blk_IO)
8037 then
8038 return RTE (RE_Wide_String_Input_Blk_IO);
8040 elsif Nam = TSS_Stream_Output
8041 and then Is_Available (RE_Wide_String_Output_Blk_IO)
8042 then
8043 return RTE (RE_Wide_String_Output_Blk_IO);
8045 elsif Nam = TSS_Stream_Read
8046 and then Is_Available (RE_Wide_String_Read_Blk_IO)
8047 then
8048 return RTE (RE_Wide_String_Read_Blk_IO);
8050 elsif Nam = TSS_Stream_Write
8051 and then Is_Available (RE_Wide_String_Write_Blk_IO)
8052 then
8053 return RTE (RE_Wide_String_Write_Blk_IO);
8055 elsif Nam /= TSS_Stream_Input and then
8056 Nam /= TSS_Stream_Output and then
8057 Nam /= TSS_Stream_Read and then
8058 Nam /= TSS_Stream_Write
8059 then
8060 raise Program_Error;
8061 end if;
8062 end if;
8064 -- Wide_Wide_String as defined in package Ada
8066 elsif Base_Typ = Standard_Wide_Wide_String then
8068 -- Case of No_Stream_Optimizations restriction active
8070 if Restriction_Active (No_Stream_Optimizations) then
8071 if Nam = TSS_Stream_Input
8072 and then Is_Available (RE_Wide_Wide_String_Input)
8073 then
8074 return RTE (RE_Wide_Wide_String_Input);
8076 elsif Nam = TSS_Stream_Output
8077 and then Is_Available (RE_Wide_Wide_String_Output)
8078 then
8079 return RTE (RE_Wide_Wide_String_Output);
8081 elsif Nam = TSS_Stream_Read
8082 and then Is_Available (RE_Wide_Wide_String_Read)
8083 then
8084 return RTE (RE_Wide_Wide_String_Read);
8086 elsif Nam = TSS_Stream_Write
8087 and then Is_Available (RE_Wide_Wide_String_Write)
8088 then
8089 return RTE (RE_Wide_Wide_String_Write);
8091 elsif Nam /= TSS_Stream_Input and then
8092 Nam /= TSS_Stream_Output and then
8093 Nam /= TSS_Stream_Read and then
8094 Nam /= TSS_Stream_Write
8095 then
8096 raise Program_Error;
8097 end if;
8099 -- Restriction No_Stream_Optimizations is not set, so we can go
8100 -- ahead and optimize using the block IO forms of the routines.
8102 else
8103 if Nam = TSS_Stream_Input
8104 and then Is_Available (RE_Wide_Wide_String_Input_Blk_IO)
8105 then
8106 return RTE (RE_Wide_Wide_String_Input_Blk_IO);
8108 elsif Nam = TSS_Stream_Output
8109 and then Is_Available (RE_Wide_Wide_String_Output_Blk_IO)
8110 then
8111 return RTE (RE_Wide_Wide_String_Output_Blk_IO);
8113 elsif Nam = TSS_Stream_Read
8114 and then Is_Available (RE_Wide_Wide_String_Read_Blk_IO)
8115 then
8116 return RTE (RE_Wide_Wide_String_Read_Blk_IO);
8118 elsif Nam = TSS_Stream_Write
8119 and then Is_Available (RE_Wide_Wide_String_Write_Blk_IO)
8120 then
8121 return RTE (RE_Wide_Wide_String_Write_Blk_IO);
8123 elsif Nam /= TSS_Stream_Input and then
8124 Nam /= TSS_Stream_Output and then
8125 Nam /= TSS_Stream_Read and then
8126 Nam /= TSS_Stream_Write
8127 then
8128 raise Program_Error;
8129 end if;
8130 end if;
8131 end if;
8132 end if;
8134 if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then
8135 return Find_Prim_Op (Typ, Nam);
8136 else
8137 return Find_Inherited_TSS (Typ, Nam);
8138 end if;
8139 end Find_Stream_Subprogram;
8141 ---------------
8142 -- Full_Base --
8143 ---------------
8145 function Full_Base (T : Entity_Id) return Entity_Id is
8146 BT : Entity_Id;
8148 begin
8149 BT := Base_Type (T);
8151 if Is_Private_Type (BT)
8152 and then Present (Full_View (BT))
8153 then
8154 BT := Full_View (BT);
8155 end if;
8157 return BT;
8158 end Full_Base;
8160 -----------------------
8161 -- Get_Index_Subtype --
8162 -----------------------
8164 function Get_Index_Subtype (N : Node_Id) return Node_Id is
8165 P_Type : Entity_Id := Etype (Prefix (N));
8166 Indx : Node_Id;
8167 J : Int;
8169 begin
8170 if Is_Access_Type (P_Type) then
8171 P_Type := Designated_Type (P_Type);
8172 end if;
8174 if No (Expressions (N)) then
8175 J := 1;
8176 else
8177 J := UI_To_Int (Expr_Value (First (Expressions (N))));
8178 end if;
8180 Indx := First_Index (P_Type);
8181 while J > 1 loop
8182 Next_Index (Indx);
8183 J := J - 1;
8184 end loop;
8186 return Etype (Indx);
8187 end Get_Index_Subtype;
8189 -------------------------------
8190 -- Get_Stream_Convert_Pragma --
8191 -------------------------------
8193 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is
8194 Typ : Entity_Id;
8195 N : Node_Id;
8197 begin
8198 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
8199 -- that a stream convert pragma for a tagged type is not inherited from
8200 -- its parent. Probably what is wrong here is that it is basically
8201 -- incorrect to consider a stream convert pragma to be a representation
8202 -- pragma at all ???
8204 N := First_Rep_Item (Implementation_Base_Type (T));
8205 while Present (N) loop
8206 if Nkind (N) = N_Pragma
8207 and then Pragma_Name (N) = Name_Stream_Convert
8208 then
8209 -- For tagged types this pragma is not inherited, so we
8210 -- must verify that it is defined for the given type and
8211 -- not an ancestor.
8213 Typ :=
8214 Entity (Expression (First (Pragma_Argument_Associations (N))));
8216 if not Is_Tagged_Type (T)
8217 or else T = Typ
8218 or else (Is_Private_Type (Typ) and then T = Full_View (Typ))
8219 then
8220 return N;
8221 end if;
8222 end if;
8224 Next_Rep_Item (N);
8225 end loop;
8227 return Empty;
8228 end Get_Stream_Convert_Pragma;
8230 ---------------------------------
8231 -- Is_Constrained_Packed_Array --
8232 ---------------------------------
8234 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
8235 Arr : Entity_Id := Typ;
8237 begin
8238 if Is_Access_Type (Arr) then
8239 Arr := Designated_Type (Arr);
8240 end if;
8242 return Is_Array_Type (Arr)
8243 and then Is_Constrained (Arr)
8244 and then Present (Packed_Array_Impl_Type (Arr));
8245 end Is_Constrained_Packed_Array;
8247 ----------------------------------------
8248 -- Is_Inline_Floating_Point_Attribute --
8249 ----------------------------------------
8251 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is
8252 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
8254 function Is_GCC_Target return Boolean;
8255 -- Return True if we are using a GCC target/back-end
8256 -- ??? Note: the implementation is kludgy/fragile
8258 -------------------
8259 -- Is_GCC_Target --
8260 -------------------
8262 function Is_GCC_Target return Boolean is
8263 begin
8264 return not CodePeer_Mode
8265 and then not Modify_Tree_For_C;
8266 end Is_GCC_Target;
8268 -- Start of processing for Is_Inline_Floating_Point_Attribute
8270 begin
8271 -- Machine and Model can be expanded by the GCC and AAMP back ends only
8273 if Id = Attribute_Machine or else Id = Attribute_Model then
8274 return Is_GCC_Target;
8276 -- Remaining cases handled by all back ends are Rounding and Truncation
8277 -- when appearing as the operand of a conversion to some integer type.
8279 elsif Nkind (Parent (N)) /= N_Type_Conversion
8280 or else not Is_Integer_Type (Etype (Parent (N)))
8281 then
8282 return False;
8283 end if;
8285 -- Here we are in the integer conversion context
8287 -- Very probably we should also recognize the cases of Machine_Rounding
8288 -- and unbiased rounding in this conversion context, but the back end is
8289 -- not yet prepared to handle these cases ???
8291 return Id = Attribute_Rounding or else Id = Attribute_Truncation;
8292 end Is_Inline_Floating_Point_Attribute;
8294 end Exp_Attr;