gcc/testsuite/ChangeLog:
[official-gcc.git] / gcc / ada / exp_attr.adb
blob77e706a97c970cadf832784beca1d075cbc377e6
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-2018, 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 (Attr : Node_Id;
79 Formal_Typ : Entity_Id;
80 Array_Typ : Entity_Id;
81 Comp_Typ : Entity_Id) return Entity_Id;
82 -- Validate the components of an array type by means of a function. Return
83 -- the entity of the validation function. The parameters are as follows:
85 -- * Attr - the 'Valid_Scalars attribute for which the function is
86 -- generated.
88 -- * Formal_Typ - the type of the generated function's only formal
89 -- parameter.
91 -- * Array_Typ - the array type whose components are to be validated
93 -- * Comp_Typ - the component type of the array
95 function Build_Disp_Get_Task_Id_Call (Actual : Node_Id) return Node_Id;
96 -- Build a call to Disp_Get_Task_Id, passing Actual as actual parameter
98 function Build_Record_VS_Func
99 (Attr : Node_Id;
100 Formal_Typ : Entity_Id;
101 Rec_Typ : Entity_Id) return Entity_Id;
102 -- Validate the components, discriminants, and variants of a record type by
103 -- means of a function. Return the entity of the validation function. The
104 -- parameters are as follows:
106 -- * Attr - the 'Valid_Scalars attribute for which the function is
107 -- generated.
109 -- * Formal_Typ - the type of the generated function's only formal
110 -- parameter.
112 -- * Rec_Typ - the record type whose internals are to be validated
114 procedure Compile_Stream_Body_In_Scope
115 (N : Node_Id;
116 Decl : Node_Id;
117 Arr : Entity_Id;
118 Check : Boolean);
119 -- The body for a stream subprogram may be generated outside of the scope
120 -- of the type. If the type is fully private, it may depend on the full
121 -- view of other types (e.g. indexes) that are currently private as well.
122 -- We install the declarations of the package in which the type is declared
123 -- before compiling the body in what is its proper environment. The Check
124 -- parameter indicates if checks are to be suppressed for the stream body.
125 -- We suppress checks for array/record reads, since the rule is that these
126 -- are like assignments, out of range values due to uninitialized storage,
127 -- or other invalid values do NOT cause a Constraint_Error to be raised.
128 -- If we are within an instance body all visibility has been established
129 -- already and there is no need to install the package.
131 -- This mechanism is now extended to the component types of the array type,
132 -- when the component type is not in scope and is private, to handle
133 -- properly the case when the full view has defaulted discriminants.
135 -- This special processing is ultimately caused by the fact that the
136 -- compiler lacks a well-defined phase when full views are visible
137 -- everywhere. Having such a separate pass would remove much of the
138 -- special-case code that shuffles partial and full views in the middle
139 -- of semantic analysis and expansion.
141 procedure Expand_Access_To_Protected_Op
142 (N : Node_Id;
143 Pref : Node_Id;
144 Typ : Entity_Id);
145 -- An attribute reference to a protected subprogram is transformed into
146 -- a pair of pointers: one to the object, and one to the operations.
147 -- This expansion is performed for 'Access and for 'Unrestricted_Access.
149 procedure Expand_Fpt_Attribute
150 (N : Node_Id;
151 Pkg : RE_Id;
152 Nam : Name_Id;
153 Args : List_Id);
154 -- This procedure expands a call to a floating-point attribute function.
155 -- N is the attribute reference node, and Args is a list of arguments to
156 -- be passed to the function call. Pkg identifies the package containing
157 -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args
158 -- have already been converted to the floating-point type for which Pkg was
159 -- instantiated. The Nam argument is the relevant attribute processing
160 -- routine to be called. This is the same as the attribute name, except in
161 -- the Unaligned_Valid case.
163 procedure Expand_Fpt_Attribute_R (N : Node_Id);
164 -- This procedure expands a call to a floating-point attribute function
165 -- that takes a single floating-point argument. The function to be called
166 -- is always the same as the attribute name.
168 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
169 -- This procedure expands a call to a floating-point attribute function
170 -- that takes one floating-point argument and one integer argument. The
171 -- function to be called is always the same as the attribute name.
173 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
174 -- This procedure expands a call to a floating-point attribute function
175 -- that takes two floating-point arguments. The function to be called
176 -- is always the same as the attribute name.
178 procedure Expand_Loop_Entry_Attribute (N : Node_Id);
179 -- Handle the expansion of attribute 'Loop_Entry. As a result, the related
180 -- loop may be converted into a conditional block. See body for details.
182 procedure Expand_Min_Max_Attribute (N : Node_Id);
183 -- Handle the expansion of attributes 'Max and 'Min, including expanding
184 -- then out if we are in Modify_Tree_For_C mode.
186 procedure Expand_Pred_Succ_Attribute (N : Node_Id);
187 -- Handles expansion of Pred or Succ attributes for case of non-real
188 -- operand with overflow checking required.
190 procedure Expand_Update_Attribute (N : Node_Id);
191 -- Handle the expansion of attribute Update
193 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
194 -- Used for Last, Last, and Length, when the prefix is an array type.
195 -- Obtains the corresponding index subtype.
197 procedure Find_Fat_Info
198 (T : Entity_Id;
199 Fat_Type : out Entity_Id;
200 Fat_Pkg : out RE_Id);
201 -- Given a floating-point type T, identifies the package containing the
202 -- attributes for this type (returned in Fat_Pkg), and the corresponding
203 -- type for which this package was instantiated from Fat_Gen. Error if T
204 -- is not a floating-point type.
206 function Find_Stream_Subprogram
207 (Typ : Entity_Id;
208 Nam : TSS_Name_Type) return Entity_Id;
209 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
210 -- types, the corresponding primitive operation is looked up, else the
211 -- appropriate TSS from the type itself, or from its closest ancestor
212 -- defining it, is returned. In both cases, inheritance of representation
213 -- aspects is thus taken into account.
215 function Full_Base (T : Entity_Id) return Entity_Id;
216 -- The stream functions need to examine the underlying representation of
217 -- composite types. In some cases T may be non-private but its base type
218 -- is, in which case the function returns the corresponding full view.
220 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id;
221 -- Given a type, find a corresponding stream convert pragma that applies to
222 -- the implementation base type of this type (Typ). If found, return the
223 -- pragma node, otherwise return Empty if no pragma is found.
225 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
226 -- Utility for array attributes, returns true on packed constrained
227 -- arrays, and on access to same.
229 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean;
230 -- Returns true iff the given node refers to an attribute call that
231 -- can be expanded directly by the back end and does not need front end
232 -- expansion. Typically used for rounding and truncation attributes that
233 -- appear directly inside a conversion to integer.
235 -------------------------
236 -- Build_Array_VS_Func --
237 -------------------------
239 function Build_Array_VS_Func
240 (Attr : Node_Id;
241 Formal_Typ : Entity_Id;
242 Array_Typ : Entity_Id;
243 Comp_Typ : Entity_Id) return Entity_Id
245 Loc : constant Source_Ptr := Sloc (Attr);
247 function Validate_Component
248 (Obj_Id : Entity_Id;
249 Indexes : List_Id) return Node_Id;
250 -- Process a single component denoted by indexes Indexes. Obj_Id denotes
251 -- the entity of the validation parameter. Return the check associated
252 -- with the component.
254 function Validate_Dimension
255 (Obj_Id : Entity_Id;
256 Dim : Int;
257 Indexes : List_Id) return Node_Id;
258 -- Process dimension Dim of the array type. Obj_Id denotes the entity
259 -- of the validation parameter. Indexes is a list where each dimension
260 -- deposits its loop variable, which will later identify a component.
261 -- Return the loop associated with the current dimension.
263 ------------------------
264 -- Validate_Component --
265 ------------------------
267 function Validate_Component
268 (Obj_Id : Entity_Id;
269 Indexes : List_Id) return Node_Id
271 Attr_Nam : Name_Id;
273 begin
274 if Is_Scalar_Type (Comp_Typ) then
275 Attr_Nam := Name_Valid;
276 else
277 Attr_Nam := Name_Valid_Scalars;
278 end if;
280 -- Generate:
281 -- if not Array_Typ (Obj_Id) (Indexes)'Valid[_Scalars] then
282 -- return False;
283 -- end if;
285 return
286 Make_If_Statement (Loc,
287 Condition =>
288 Make_Op_Not (Loc,
289 Right_Opnd =>
290 Make_Attribute_Reference (Loc,
291 Prefix =>
292 Make_Indexed_Component (Loc,
293 Prefix =>
294 Unchecked_Convert_To (Array_Typ,
295 New_Occurrence_Of (Obj_Id, Loc)),
296 Expressions => Indexes),
297 Attribute_Name => Attr_Nam)),
299 Then_Statements => New_List (
300 Make_Simple_Return_Statement (Loc,
301 Expression => New_Occurrence_Of (Standard_False, Loc))));
302 end Validate_Component;
304 ------------------------
305 -- Validate_Dimension --
306 ------------------------
308 function Validate_Dimension
309 (Obj_Id : Entity_Id;
310 Dim : Int;
311 Indexes : List_Id) return Node_Id
313 Index : Entity_Id;
315 begin
316 -- Validate the component once all dimensions have produced their
317 -- individual loops.
319 if Dim > Number_Dimensions (Array_Typ) then
320 return Validate_Component (Obj_Id, Indexes);
322 -- Process the current dimension
324 else
325 Index :=
326 Make_Defining_Identifier (Loc, New_External_Name ('J', Dim));
328 Append_To (Indexes, New_Occurrence_Of (Index, Loc));
330 -- Generate:
331 -- for J1 in Array_Typ (Obj_Id)'Range (1) loop
332 -- for JN in Array_Typ (Obj_Id)'Range (N) loop
333 -- if not Array_Typ (Obj_Id) (Indexes)'Valid[_Scalars]
334 -- then
335 -- return False;
336 -- end if;
337 -- end loop;
338 -- end loop;
340 return
341 Make_Implicit_Loop_Statement (Attr,
342 Identifier => Empty,
343 Iteration_Scheme =>
344 Make_Iteration_Scheme (Loc,
345 Loop_Parameter_Specification =>
346 Make_Loop_Parameter_Specification (Loc,
347 Defining_Identifier => Index,
348 Discrete_Subtype_Definition =>
349 Make_Attribute_Reference (Loc,
350 Prefix =>
351 Unchecked_Convert_To (Array_Typ,
352 New_Occurrence_Of (Obj_Id, Loc)),
353 Attribute_Name => Name_Range,
354 Expressions => New_List (
355 Make_Integer_Literal (Loc, Dim))))),
356 Statements => New_List (
357 Validate_Dimension (Obj_Id, Dim + 1, Indexes)));
358 end if;
359 end Validate_Dimension;
361 -- Local variables
363 Func_Id : constant Entity_Id := Make_Temporary (Loc, 'V');
364 Indexes : constant List_Id := New_List;
365 Obj_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
366 Stmts : List_Id;
368 -- Start of processing for Build_Array_VS_Func
370 begin
371 Stmts := New_List (Validate_Dimension (Obj_Id, 1, Indexes));
373 -- Generate:
374 -- return True;
376 Append_To (Stmts,
377 Make_Simple_Return_Statement (Loc,
378 Expression => New_Occurrence_Of (Standard_True, Loc)));
380 -- Generate:
381 -- function Func_Id (Obj_Id : Formal_Typ) return Boolean is
382 -- begin
383 -- Stmts
384 -- end Func_Id;
386 Set_Ekind (Func_Id, E_Function);
387 Set_Is_Internal (Func_Id);
388 Set_Is_Pure (Func_Id);
390 if not Debug_Generated_Code then
391 Set_Debug_Info_Off (Func_Id);
392 end if;
394 Insert_Action (Attr,
395 Make_Subprogram_Body (Loc,
396 Specification =>
397 Make_Function_Specification (Loc,
398 Defining_Unit_Name => Func_Id,
399 Parameter_Specifications => New_List (
400 Make_Parameter_Specification (Loc,
401 Defining_Identifier => Obj_Id,
402 In_Present => True,
403 Out_Present => False,
404 Parameter_Type => New_Occurrence_Of (Formal_Typ, Loc))),
405 Result_Definition =>
406 New_Occurrence_Of (Standard_Boolean, Loc)),
407 Declarations => New_List,
408 Handled_Statement_Sequence =>
409 Make_Handled_Sequence_Of_Statements (Loc,
410 Statements => Stmts)));
412 return Func_Id;
413 end Build_Array_VS_Func;
415 ---------------------------------
416 -- Build_Disp_Get_Task_Id_Call --
417 ---------------------------------
419 function Build_Disp_Get_Task_Id_Call (Actual : Node_Id) return Node_Id is
420 Loc : constant Source_Ptr := Sloc (Actual);
421 Typ : constant Entity_Id := Etype (Actual);
422 Subp : constant Entity_Id := Find_Prim_Op (Typ, Name_uDisp_Get_Task_Id);
424 begin
425 -- Generate:
426 -- _Disp_Get_Task_Id (Actual)
428 return
429 Make_Function_Call (Loc,
430 Name => New_Occurrence_Of (Subp, Loc),
431 Parameter_Associations => New_List (Actual));
432 end Build_Disp_Get_Task_Id_Call;
434 --------------------------
435 -- Build_Record_VS_Func --
436 --------------------------
438 function Build_Record_VS_Func
439 (Attr : Node_Id;
440 Formal_Typ : Entity_Id;
441 Rec_Typ : Entity_Id) return Entity_Id
443 -- NOTE: The logic of Build_Record_VS_Func is intentionally passive.
444 -- It generates code only when there are components, discriminants,
445 -- or variant parts to validate.
447 -- NOTE: The routines within Build_Record_VS_Func are intentionally
448 -- unnested to avoid deep indentation of code.
450 Loc : constant Source_Ptr := Sloc (Attr);
452 procedure Validate_Component_List
453 (Obj_Id : Entity_Id;
454 Comp_List : Node_Id;
455 Stmts : in out List_Id);
456 -- Process all components and variant parts of component list Comp_List.
457 -- Obj_Id denotes the entity of the validation parameter. All new code
458 -- is added to list Stmts.
460 procedure Validate_Field
461 (Obj_Id : Entity_Id;
462 Field : Node_Id;
463 Cond : in out Node_Id);
464 -- Process component declaration or discriminant specification Field.
465 -- Obj_Id denotes the entity of the validation parameter. Cond denotes
466 -- an "or else" conditional expression which contains the new code (if
467 -- any).
469 procedure Validate_Fields
470 (Obj_Id : Entity_Id;
471 Fields : List_Id;
472 Stmts : in out List_Id);
473 -- Process component declarations or discriminant specifications in list
474 -- Fields. Obj_Id denotes the entity of the validation parameter. All
475 -- new code is added to list Stmts.
477 procedure Validate_Variant
478 (Obj_Id : Entity_Id;
479 Var : Node_Id;
480 Alts : in out List_Id);
481 -- Process variant Var. Obj_Id denotes the entity of the validation
482 -- parameter. Alts denotes a list of case statement alternatives which
483 -- contains the new code (if any).
485 procedure Validate_Variant_Part
486 (Obj_Id : Entity_Id;
487 Var_Part : Node_Id;
488 Stmts : in out List_Id);
489 -- Process variant part Var_Part. Obj_Id denotes the entity of the
490 -- validation parameter. All new code is added to list Stmts.
492 -----------------------------
493 -- Validate_Component_List --
494 -----------------------------
496 procedure Validate_Component_List
497 (Obj_Id : Entity_Id;
498 Comp_List : Node_Id;
499 Stmts : in out List_Id)
501 Var_Part : constant Node_Id := Variant_Part (Comp_List);
503 begin
504 -- Validate all components
506 Validate_Fields
507 (Obj_Id => Obj_Id,
508 Fields => Component_Items (Comp_List),
509 Stmts => Stmts);
511 -- Validate the variant part
513 if Present (Var_Part) then
514 Validate_Variant_Part
515 (Obj_Id => Obj_Id,
516 Var_Part => Var_Part,
517 Stmts => Stmts);
518 end if;
519 end Validate_Component_List;
521 --------------------
522 -- Validate_Field --
523 --------------------
525 procedure Validate_Field
526 (Obj_Id : Entity_Id;
527 Field : Node_Id;
528 Cond : in out Node_Id)
530 Field_Id : constant Entity_Id := Defining_Entity (Field);
531 Field_Nam : constant Name_Id := Chars (Field_Id);
532 Field_Typ : constant Entity_Id := Validated_View (Etype (Field_Id));
533 Attr_Nam : Name_Id;
535 begin
536 -- Do not process internally-generated fields. Note that checking for
537 -- Comes_From_Source is not correct because this will eliminate the
538 -- components within the corresponding record of a protected type.
540 if Nam_In (Field_Nam, Name_uObject,
541 Name_uParent,
542 Name_uTag)
543 then
544 null;
546 -- Do not process fields without any scalar components
548 elsif not Scalar_Part_Present (Field_Typ) then
549 null;
551 -- Otherwise the field needs to be validated. Use Make_Identifier
552 -- rather than New_Occurrence_Of to identify the field because the
553 -- wrong entity may be picked up when private types are involved.
555 -- Generate:
556 -- [or else] not Rec_Typ (Obj_Id).Item_Nam'Valid[_Scalars]
558 else
559 if Is_Scalar_Type (Field_Typ) then
560 Attr_Nam := Name_Valid;
561 else
562 Attr_Nam := Name_Valid_Scalars;
563 end if;
565 Evolve_Or_Else (Cond,
566 Make_Op_Not (Loc,
567 Right_Opnd =>
568 Make_Attribute_Reference (Loc,
569 Prefix =>
570 Make_Selected_Component (Loc,
571 Prefix =>
572 Unchecked_Convert_To (Rec_Typ,
573 New_Occurrence_Of (Obj_Id, Loc)),
574 Selector_Name => Make_Identifier (Loc, Field_Nam)),
575 Attribute_Name => Attr_Nam)));
576 end if;
577 end Validate_Field;
579 ---------------------
580 -- Validate_Fields --
581 ---------------------
583 procedure Validate_Fields
584 (Obj_Id : Entity_Id;
585 Fields : List_Id;
586 Stmts : in out List_Id)
588 Cond : Node_Id;
589 Field : Node_Id;
591 begin
592 -- Assume that none of the fields are eligible for verification
594 Cond := Empty;
596 -- Validate all fields
598 Field := First_Non_Pragma (Fields);
599 while Present (Field) loop
600 Validate_Field
601 (Obj_Id => Obj_Id,
602 Field => Field,
603 Cond => Cond);
605 Next_Non_Pragma (Field);
606 end loop;
608 -- Generate:
609 -- if not Rec_Typ (Obj_Id).Item_Nam_1'Valid[_Scalars]
610 -- or else not Rec_Typ (Obj_Id).Item_Nam_N'Valid[_Scalars]
611 -- then
612 -- return False;
613 -- end if;
615 if Present (Cond) then
616 Append_New_To (Stmts,
617 Make_Implicit_If_Statement (Attr,
618 Condition => Cond,
619 Then_Statements => New_List (
620 Make_Simple_Return_Statement (Loc,
621 Expression => New_Occurrence_Of (Standard_False, Loc)))));
622 end if;
623 end Validate_Fields;
625 ----------------------
626 -- Validate_Variant --
627 ----------------------
629 procedure Validate_Variant
630 (Obj_Id : Entity_Id;
631 Var : Node_Id;
632 Alts : in out List_Id)
634 Stmts : List_Id;
636 begin
637 -- Assume that none of the components and variants are eligible for
638 -- verification.
640 Stmts := No_List;
642 -- Validate componants
644 Validate_Component_List
645 (Obj_Id => Obj_Id,
646 Comp_List => Component_List (Var),
647 Stmts => Stmts);
649 -- Generate a null statement in case none of the components were
650 -- verified because this will otherwise eliminate an alternative
651 -- from the variant case statement and render the generated code
652 -- illegal.
654 if No (Stmts) then
655 Append_New_To (Stmts, Make_Null_Statement (Loc));
656 end if;
658 -- Generate:
659 -- when Discrete_Choices =>
660 -- Stmts
662 Append_New_To (Alts,
663 Make_Case_Statement_Alternative (Loc,
664 Discrete_Choices =>
665 New_Copy_List_Tree (Discrete_Choices (Var)),
666 Statements => Stmts));
667 end Validate_Variant;
669 ---------------------------
670 -- Validate_Variant_Part --
671 ---------------------------
673 procedure Validate_Variant_Part
674 (Obj_Id : Entity_Id;
675 Var_Part : Node_Id;
676 Stmts : in out List_Id)
678 Vars : constant List_Id := Variants (Var_Part);
679 Alts : List_Id;
680 Var : Node_Id;
682 begin
683 -- Assume that none of the variants are eligible for verification
685 Alts := No_List;
687 -- Validate variants
689 Var := First_Non_Pragma (Vars);
690 while Present (Var) loop
691 Validate_Variant
692 (Obj_Id => Obj_Id,
693 Var => Var,
694 Alts => Alts);
696 Next_Non_Pragma (Var);
697 end loop;
699 -- Even though individual variants may lack eligible components, the
700 -- alternatives must still be generated.
702 pragma Assert (Present (Alts));
704 -- Generate:
705 -- case Rec_Typ (Obj_Id).Discriminant is
706 -- when Discrete_Choices_1 =>
707 -- Stmts_1
708 -- when Discrete_Choices_N =>
709 -- Stmts_N
710 -- end case;
712 Append_New_To (Stmts,
713 Make_Case_Statement (Loc,
714 Expression =>
715 Make_Selected_Component (Loc,
716 Prefix =>
717 Unchecked_Convert_To (Rec_Typ,
718 New_Occurrence_Of (Obj_Id, Loc)),
719 Selector_Name => New_Copy_Tree (Name (Var_Part))),
720 Alternatives => Alts));
721 end Validate_Variant_Part;
723 -- Local variables
725 Func_Id : constant Entity_Id := Make_Temporary (Loc, 'V');
726 Obj_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
727 Comps : Node_Id;
728 Stmts : List_Id;
729 Typ : Entity_Id;
730 Typ_Decl : Node_Id;
731 Typ_Def : Node_Id;
732 Typ_Ext : Node_Id;
734 -- Start of processing for Build_Record_VS_Func
736 begin
737 Typ := Rec_Typ;
739 -- Use the root type when dealing with a class-wide type
741 if Is_Class_Wide_Type (Typ) then
742 Typ := Root_Type (Typ);
743 end if;
745 Typ_Decl := Declaration_Node (Typ);
746 Typ_Def := Type_Definition (Typ_Decl);
748 -- The components of a derived type are located in the extension part
750 if Nkind (Typ_Def) = N_Derived_Type_Definition then
751 Typ_Ext := Record_Extension_Part (Typ_Def);
753 if Present (Typ_Ext) then
754 Comps := Component_List (Typ_Ext);
755 else
756 Comps := Empty;
757 end if;
759 -- Otherwise the components are available in the definition
761 else
762 Comps := Component_List (Typ_Def);
763 end if;
765 -- The code generated by this routine is as follows:
767 -- function Func_Id (Obj_Id : Formal_Typ) return Boolean is
768 -- begin
769 -- if not Rec_Typ (Obj_Id).Discriminant_1'Valid[_Scalars]
770 -- or else not Rec_Typ (Obj_Id).Discriminant_N'Valid[_Scalars]
771 -- then
772 -- return False;
773 -- end if;
775 -- if not Rec_Typ (Obj_Id).Component_1'Valid[_Scalars]
776 -- or else not Rec_Typ (Obj_Id).Component_N'Valid[_Scalars]
777 -- then
778 -- return False;
779 -- end if;
781 -- case Discriminant_1 is
782 -- when Choice_1 =>
783 -- if not Rec_Typ (Obj_Id).Component_1'Valid[_Scalars]
784 -- or else not Rec_Typ (Obj_Id).Component_N'Valid[_Scalars]
785 -- then
786 -- return False;
787 -- end if;
789 -- case Discriminant_N is
790 -- ...
791 -- when Choice_N =>
792 -- ...
793 -- end case;
795 -- return True;
796 -- end Func_Id;
798 -- Assume that the record type lacks eligible components, discriminants,
799 -- and variant parts.
801 Stmts := No_List;
803 -- Validate the discriminants
805 if not Is_Unchecked_Union (Rec_Typ) then
806 Validate_Fields
807 (Obj_Id => Obj_Id,
808 Fields => Discriminant_Specifications (Typ_Decl),
809 Stmts => Stmts);
810 end if;
812 -- Validate the components and variant parts
814 Validate_Component_List
815 (Obj_Id => Obj_Id,
816 Comp_List => Comps,
817 Stmts => Stmts);
819 -- Generate:
820 -- return True;
822 Append_New_To (Stmts,
823 Make_Simple_Return_Statement (Loc,
824 Expression => New_Occurrence_Of (Standard_True, Loc)));
826 -- Generate:
827 -- function Func_Id (Obj_Id : Formal_Typ) return Boolean is
828 -- begin
829 -- Stmts
830 -- end Func_Id;
832 Set_Ekind (Func_Id, E_Function);
833 Set_Is_Internal (Func_Id);
834 Set_Is_Pure (Func_Id);
836 if not Debug_Generated_Code then
837 Set_Debug_Info_Off (Func_Id);
838 end if;
840 Insert_Action (Attr,
841 Make_Subprogram_Body (Loc,
842 Specification =>
843 Make_Function_Specification (Loc,
844 Defining_Unit_Name => Func_Id,
845 Parameter_Specifications => New_List (
846 Make_Parameter_Specification (Loc,
847 Defining_Identifier => Obj_Id,
848 Parameter_Type => New_Occurrence_Of (Formal_Typ, Loc))),
849 Result_Definition =>
850 New_Occurrence_Of (Standard_Boolean, Loc)),
851 Declarations => New_List,
852 Handled_Statement_Sequence =>
853 Make_Handled_Sequence_Of_Statements (Loc,
854 Statements => Stmts)),
855 Suppress => Discriminant_Check);
857 return Func_Id;
858 end Build_Record_VS_Func;
860 ----------------------------------
861 -- Compile_Stream_Body_In_Scope --
862 ----------------------------------
864 procedure Compile_Stream_Body_In_Scope
865 (N : Node_Id;
866 Decl : Node_Id;
867 Arr : Entity_Id;
868 Check : Boolean)
870 C_Type : constant Entity_Id := Base_Type (Component_Type (Arr));
871 Curr : constant Entity_Id := Current_Scope;
872 Install : Boolean := False;
873 Scop : Entity_Id := Scope (Arr);
875 begin
876 if Is_Hidden (Arr)
877 and then not In_Open_Scopes (Scop)
878 and then Ekind (Scop) = E_Package
879 then
880 Install := True;
882 else
883 -- The component type may be private, in which case we install its
884 -- full view to compile the subprogram.
886 -- The component type may be private, in which case we install its
887 -- full view to compile the subprogram. We do not do this if the
888 -- type has a Stream_Convert pragma, which indicates that there are
889 -- special stream-processing operations for that type (for example
890 -- Unbounded_String and its wide varieties).
892 Scop := Scope (C_Type);
894 if Is_Private_Type (C_Type)
895 and then Present (Full_View (C_Type))
896 and then not In_Open_Scopes (Scop)
897 and then Ekind (Scop) = E_Package
898 and then No (Get_Stream_Convert_Pragma (C_Type))
899 then
900 Install := True;
901 end if;
902 end if;
904 -- If we are within an instance body, then all visibility has been
905 -- established already and there is no need to install the package.
907 if Install and then not In_Instance_Body then
908 Push_Scope (Scop);
909 Install_Visible_Declarations (Scop);
910 Install_Private_Declarations (Scop);
912 -- The entities in the package are now visible, but the generated
913 -- stream entity must appear in the current scope (usually an
914 -- enclosing stream function) so that itypes all have their proper
915 -- scopes.
917 Push_Scope (Curr);
918 else
919 Install := False;
920 end if;
922 if Check then
923 Insert_Action (N, Decl);
924 else
925 Insert_Action (N, Decl, Suppress => All_Checks);
926 end if;
928 if Install then
930 -- Remove extra copy of current scope, and package itself
932 Pop_Scope;
933 End_Package_Scope (Scop);
934 end if;
935 end Compile_Stream_Body_In_Scope;
937 -----------------------------------
938 -- Expand_Access_To_Protected_Op --
939 -----------------------------------
941 procedure Expand_Access_To_Protected_Op
942 (N : Node_Id;
943 Pref : Node_Id;
944 Typ : Entity_Id)
946 -- The value of the attribute_reference is a record containing two
947 -- fields: an access to the protected object, and an access to the
948 -- subprogram itself. The prefix is a selected component.
950 Loc : constant Source_Ptr := Sloc (N);
951 Agg : Node_Id;
952 Btyp : constant Entity_Id := Base_Type (Typ);
953 Sub : Entity_Id;
954 Sub_Ref : Node_Id;
955 E_T : constant Entity_Id := Equivalent_Type (Btyp);
956 Acc : constant Entity_Id :=
957 Etype (Next_Component (First_Component (E_T)));
958 Obj_Ref : Node_Id;
959 Curr : Entity_Id;
961 -- Start of processing for Expand_Access_To_Protected_Op
963 begin
964 -- Within the body of the protected type, the prefix designates a local
965 -- operation, and the object is the first parameter of the corresponding
966 -- protected body of the current enclosing operation.
968 if Is_Entity_Name (Pref) then
969 -- All indirect calls are external calls, so must do locking and
970 -- barrier reevaluation, even if the 'Access occurs within the
971 -- protected body. Hence the call to External_Subprogram, as opposed
972 -- to Protected_Body_Subprogram, below. See RM-9.5(5). This means
973 -- that indirect calls from within the same protected body will
974 -- deadlock, as allowed by RM-9.5.1(8,15,17).
976 Sub := New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc);
978 -- Don't traverse the scopes when the attribute occurs within an init
979 -- proc, because we directly use the _init formal of the init proc in
980 -- that case.
982 Curr := Current_Scope;
983 if not Is_Init_Proc (Curr) then
984 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
986 while Scope (Curr) /= Scope (Entity (Pref)) loop
987 Curr := Scope (Curr);
988 end loop;
989 end if;
991 -- In case of protected entries the first formal of its Protected_
992 -- Body_Subprogram is the address of the object.
994 if Ekind (Curr) = E_Entry then
995 Obj_Ref :=
996 New_Occurrence_Of
997 (First_Formal
998 (Protected_Body_Subprogram (Curr)), Loc);
1000 -- If the current scope is an init proc, then use the address of the
1001 -- _init formal as the object reference.
1003 elsif Is_Init_Proc (Curr) then
1004 Obj_Ref :=
1005 Make_Attribute_Reference (Loc,
1006 Prefix => New_Occurrence_Of (First_Formal (Curr), Loc),
1007 Attribute_Name => Name_Address);
1009 -- In case of protected subprograms the first formal of its
1010 -- Protected_Body_Subprogram is the object and we get its address.
1012 else
1013 Obj_Ref :=
1014 Make_Attribute_Reference (Loc,
1015 Prefix =>
1016 New_Occurrence_Of
1017 (First_Formal
1018 (Protected_Body_Subprogram (Curr)), Loc),
1019 Attribute_Name => Name_Address);
1020 end if;
1022 -- Case where the prefix is not an entity name. Find the
1023 -- version of the protected operation to be called from
1024 -- outside the protected object.
1026 else
1027 Sub :=
1028 New_Occurrence_Of
1029 (External_Subprogram
1030 (Entity (Selector_Name (Pref))), Loc);
1032 Obj_Ref :=
1033 Make_Attribute_Reference (Loc,
1034 Prefix => Relocate_Node (Prefix (Pref)),
1035 Attribute_Name => Name_Address);
1036 end if;
1038 Sub_Ref :=
1039 Make_Attribute_Reference (Loc,
1040 Prefix => Sub,
1041 Attribute_Name => Name_Access);
1043 -- We set the type of the access reference to the already generated
1044 -- access_to_subprogram type, and declare the reference analyzed, to
1045 -- prevent further expansion when the enclosing aggregate is analyzed.
1047 Set_Etype (Sub_Ref, Acc);
1048 Set_Analyzed (Sub_Ref);
1050 Agg :=
1051 Make_Aggregate (Loc,
1052 Expressions => New_List (Obj_Ref, Sub_Ref));
1054 -- Sub_Ref has been marked as analyzed, but we still need to make sure
1055 -- Sub is correctly frozen.
1057 Freeze_Before (N, Entity (Sub));
1059 Rewrite (N, Agg);
1060 Analyze_And_Resolve (N, E_T);
1062 -- For subsequent analysis, the node must retain its type. The backend
1063 -- will replace it with the equivalent type where needed.
1065 Set_Etype (N, Typ);
1066 end Expand_Access_To_Protected_Op;
1068 --------------------------
1069 -- Expand_Fpt_Attribute --
1070 --------------------------
1072 procedure Expand_Fpt_Attribute
1073 (N : Node_Id;
1074 Pkg : RE_Id;
1075 Nam : Name_Id;
1076 Args : List_Id)
1078 Loc : constant Source_Ptr := Sloc (N);
1079 Typ : constant Entity_Id := Etype (N);
1080 Fnm : Node_Id;
1082 begin
1083 -- The function name is the selected component Attr_xxx.yyy where
1084 -- Attr_xxx is the package name, and yyy is the argument Nam.
1086 -- Note: it would be more usual to have separate RE entries for each
1087 -- of the entities in the Fat packages, but first they have identical
1088 -- names (so we would have to have lots of renaming declarations to
1089 -- meet the normal RE rule of separate names for all runtime entities),
1090 -- and second there would be an awful lot of them.
1092 Fnm :=
1093 Make_Selected_Component (Loc,
1094 Prefix => New_Occurrence_Of (RTE (Pkg), Loc),
1095 Selector_Name => Make_Identifier (Loc, Nam));
1097 -- The generated call is given the provided set of parameters, and then
1098 -- wrapped in a conversion which converts the result to the target type
1099 -- We use the base type as the target because a range check may be
1100 -- required.
1102 Rewrite (N,
1103 Unchecked_Convert_To (Base_Type (Etype (N)),
1104 Make_Function_Call (Loc,
1105 Name => Fnm,
1106 Parameter_Associations => Args)));
1108 Analyze_And_Resolve (N, Typ);
1109 end Expand_Fpt_Attribute;
1111 ----------------------------
1112 -- Expand_Fpt_Attribute_R --
1113 ----------------------------
1115 -- The single argument is converted to its root type to call the
1116 -- appropriate runtime function, with the actual call being built
1117 -- by Expand_Fpt_Attribute
1119 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
1120 E1 : constant Node_Id := First (Expressions (N));
1121 Ftp : Entity_Id;
1122 Pkg : RE_Id;
1123 begin
1124 Find_Fat_Info (Etype (E1), Ftp, Pkg);
1125 Expand_Fpt_Attribute
1126 (N, Pkg, Attribute_Name (N),
1127 New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1))));
1128 end Expand_Fpt_Attribute_R;
1130 -----------------------------
1131 -- Expand_Fpt_Attribute_RI --
1132 -----------------------------
1134 -- The first argument is converted to its root type and the second
1135 -- argument is converted to standard long long integer to call the
1136 -- appropriate runtime function, with the actual call being built
1137 -- by Expand_Fpt_Attribute
1139 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
1140 E1 : constant Node_Id := First (Expressions (N));
1141 Ftp : Entity_Id;
1142 Pkg : RE_Id;
1143 E2 : constant Node_Id := Next (E1);
1144 begin
1145 Find_Fat_Info (Etype (E1), Ftp, Pkg);
1146 Expand_Fpt_Attribute
1147 (N, Pkg, Attribute_Name (N),
1148 New_List (
1149 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
1150 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
1151 end Expand_Fpt_Attribute_RI;
1153 -----------------------------
1154 -- Expand_Fpt_Attribute_RR --
1155 -----------------------------
1157 -- The two arguments are converted to their root types to call the
1158 -- appropriate runtime function, with the actual call being built
1159 -- by Expand_Fpt_Attribute
1161 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
1162 E1 : constant Node_Id := First (Expressions (N));
1163 E2 : constant Node_Id := Next (E1);
1164 Ftp : Entity_Id;
1165 Pkg : RE_Id;
1167 begin
1168 Find_Fat_Info (Etype (E1), Ftp, Pkg);
1169 Expand_Fpt_Attribute
1170 (N, Pkg, Attribute_Name (N),
1171 New_List (
1172 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
1173 Unchecked_Convert_To (Ftp, Relocate_Node (E2))));
1174 end Expand_Fpt_Attribute_RR;
1176 ---------------------------------
1177 -- Expand_Loop_Entry_Attribute --
1178 ---------------------------------
1180 procedure Expand_Loop_Entry_Attribute (N : Node_Id) is
1181 procedure Build_Conditional_Block
1182 (Loc : Source_Ptr;
1183 Cond : Node_Id;
1184 Loop_Stmt : Node_Id;
1185 If_Stmt : out Node_Id;
1186 Blk_Stmt : out Node_Id);
1187 -- Create a block Blk_Stmt with an empty declarative list and a single
1188 -- loop Loop_Stmt. The block is encased in an if statement If_Stmt with
1189 -- condition Cond. If_Stmt is Empty when there is no condition provided.
1191 function Is_Array_Iteration (N : Node_Id) return Boolean;
1192 -- Determine whether loop statement N denotes an Ada 2012 iteration over
1193 -- an array object.
1195 -----------------------------
1196 -- Build_Conditional_Block --
1197 -----------------------------
1199 procedure Build_Conditional_Block
1200 (Loc : Source_Ptr;
1201 Cond : Node_Id;
1202 Loop_Stmt : Node_Id;
1203 If_Stmt : out Node_Id;
1204 Blk_Stmt : out Node_Id)
1206 begin
1207 -- Do not reanalyze the original loop statement because it is simply
1208 -- being relocated.
1210 Set_Analyzed (Loop_Stmt);
1212 Blk_Stmt :=
1213 Make_Block_Statement (Loc,
1214 Declarations => New_List,
1215 Handled_Statement_Sequence =>
1216 Make_Handled_Sequence_Of_Statements (Loc,
1217 Statements => New_List (Loop_Stmt)));
1219 if Present (Cond) then
1220 If_Stmt :=
1221 Make_If_Statement (Loc,
1222 Condition => Cond,
1223 Then_Statements => New_List (Blk_Stmt));
1224 else
1225 If_Stmt := Empty;
1226 end if;
1227 end Build_Conditional_Block;
1229 ------------------------
1230 -- Is_Array_Iteration --
1231 ------------------------
1233 function Is_Array_Iteration (N : Node_Id) return Boolean is
1234 Stmt : constant Node_Id := Original_Node (N);
1235 Iter : Node_Id;
1237 begin
1238 if Nkind (Stmt) = N_Loop_Statement
1239 and then Present (Iteration_Scheme (Stmt))
1240 and then Present (Iterator_Specification (Iteration_Scheme (Stmt)))
1241 then
1242 Iter := Iterator_Specification (Iteration_Scheme (Stmt));
1244 return
1245 Of_Present (Iter) and then Is_Array_Type (Etype (Name (Iter)));
1246 end if;
1248 return False;
1249 end Is_Array_Iteration;
1251 -- Local variables
1253 Pref : constant Node_Id := Prefix (N);
1254 Base_Typ : constant Entity_Id := Base_Type (Etype (Pref));
1255 Exprs : constant List_Id := Expressions (N);
1256 Aux_Decl : Node_Id;
1257 Blk : Node_Id := Empty;
1258 Decls : List_Id;
1259 Installed : Boolean;
1260 Loc : Source_Ptr;
1261 Loop_Id : Entity_Id;
1262 Loop_Stmt : Node_Id;
1263 Result : Node_Id := Empty;
1264 Scheme : Node_Id;
1265 Temp_Decl : Node_Id;
1266 Temp_Id : Entity_Id;
1268 -- Start of processing for Expand_Loop_Entry_Attribute
1270 begin
1271 -- Step 1: Find the related loop
1273 -- The loop label variant of attribute 'Loop_Entry already has all the
1274 -- information in its expression.
1276 if Present (Exprs) then
1277 Loop_Id := Entity (First (Exprs));
1278 Loop_Stmt := Label_Construct (Parent (Loop_Id));
1280 -- Climb the parent chain to find the nearest enclosing loop. Skip
1281 -- all internally generated loops for quantified expressions and for
1282 -- element iterators over multidimensional arrays because the pragma
1283 -- applies to source loop.
1285 else
1286 Loop_Stmt := N;
1287 while Present (Loop_Stmt) loop
1288 if Nkind (Loop_Stmt) = N_Loop_Statement
1289 and then Nkind (Original_Node (Loop_Stmt)) = N_Loop_Statement
1290 and then Comes_From_Source (Original_Node (Loop_Stmt))
1291 then
1292 exit;
1293 end if;
1295 Loop_Stmt := Parent (Loop_Stmt);
1296 end loop;
1298 Loop_Id := Entity (Identifier (Loop_Stmt));
1299 end if;
1301 Loc := Sloc (Loop_Stmt);
1303 -- Step 2: Transform the loop
1305 -- The loop has already been transformed during the expansion of a prior
1306 -- 'Loop_Entry attribute. Retrieve the declarative list of the block.
1308 if Has_Loop_Entry_Attributes (Loop_Id) then
1310 -- When the related loop name appears as the argument of attribute
1311 -- Loop_Entry, the corresponding label construct is the generated
1312 -- block statement. This is because the expander reuses the label.
1314 if Nkind (Loop_Stmt) = N_Block_Statement then
1315 Decls := Declarations (Loop_Stmt);
1317 -- In all other cases, the loop must appear in the handled sequence
1318 -- of statements of the generated block.
1320 else
1321 pragma Assert
1322 (Nkind (Parent (Loop_Stmt)) = N_Handled_Sequence_Of_Statements
1323 and then
1324 Nkind (Parent (Parent (Loop_Stmt))) = N_Block_Statement);
1326 Decls := Declarations (Parent (Parent (Loop_Stmt)));
1327 end if;
1329 -- Transform the loop into a conditional block
1331 else
1332 Set_Has_Loop_Entry_Attributes (Loop_Id);
1333 Scheme := Iteration_Scheme (Loop_Stmt);
1335 -- Infinite loops are transformed into:
1337 -- declare
1338 -- Temp1 : constant <type of Pref1> := <Pref1>;
1339 -- . . .
1340 -- TempN : constant <type of PrefN> := <PrefN>;
1341 -- begin
1342 -- loop
1343 -- <original source statements with attribute rewrites>
1344 -- end loop;
1345 -- end;
1347 if No (Scheme) then
1348 Build_Conditional_Block (Loc,
1349 Cond => Empty,
1350 Loop_Stmt => Relocate_Node (Loop_Stmt),
1351 If_Stmt => Result,
1352 Blk_Stmt => Blk);
1354 Result := Blk;
1356 -- While loops are transformed into:
1358 -- function Fnn return Boolean is
1359 -- begin
1360 -- <condition actions>
1361 -- return <condition>;
1362 -- end Fnn;
1364 -- if Fnn then
1365 -- declare
1366 -- Temp1 : constant <type of Pref1> := <Pref1>;
1367 -- . . .
1368 -- TempN : constant <type of PrefN> := <PrefN>;
1369 -- begin
1370 -- loop
1371 -- <original source statements with attribute rewrites>
1372 -- exit when not Fnn;
1373 -- end loop;
1374 -- end;
1375 -- end if;
1377 -- Note that loops over iterators and containers are already
1378 -- converted into while loops.
1380 elsif Present (Condition (Scheme)) then
1381 declare
1382 Func_Decl : Node_Id;
1383 Func_Id : Entity_Id;
1384 Stmts : List_Id;
1386 begin
1387 -- Wrap the condition of the while loop in a Boolean function.
1388 -- This avoids the duplication of the same code which may lead
1389 -- to gigi issues with respect to multiple declaration of the
1390 -- same entity in the presence of side effects or checks. Note
1391 -- that the condition actions must also be relocated to the
1392 -- wrapping function.
1394 -- Generate:
1395 -- <condition actions>
1396 -- return <condition>;
1398 if Present (Condition_Actions (Scheme)) then
1399 Stmts := Condition_Actions (Scheme);
1400 else
1401 Stmts := New_List;
1402 end if;
1404 Append_To (Stmts,
1405 Make_Simple_Return_Statement (Loc,
1406 Expression => Relocate_Node (Condition (Scheme))));
1408 -- Generate:
1409 -- function Fnn return Boolean is
1410 -- begin
1411 -- <Stmts>
1412 -- end Fnn;
1414 Func_Id := Make_Temporary (Loc, 'F');
1415 Func_Decl :=
1416 Make_Subprogram_Body (Loc,
1417 Specification =>
1418 Make_Function_Specification (Loc,
1419 Defining_Unit_Name => Func_Id,
1420 Result_Definition =>
1421 New_Occurrence_Of (Standard_Boolean, Loc)),
1422 Declarations => Empty_List,
1423 Handled_Statement_Sequence =>
1424 Make_Handled_Sequence_Of_Statements (Loc,
1425 Statements => Stmts));
1427 -- The function is inserted before the related loop. Make sure
1428 -- to analyze it in the context of the loop's enclosing scope.
1430 Push_Scope (Scope (Loop_Id));
1431 Insert_Action (Loop_Stmt, Func_Decl);
1432 Pop_Scope;
1434 -- Transform the original while loop into an infinite loop
1435 -- where the last statement checks the negated condition. This
1436 -- placement ensures that the condition will not be evaluated
1437 -- twice on the first iteration.
1439 Set_Iteration_Scheme (Loop_Stmt, Empty);
1440 Scheme := Empty;
1442 -- Generate:
1443 -- exit when not Fnn;
1445 Append_To (Statements (Loop_Stmt),
1446 Make_Exit_Statement (Loc,
1447 Condition =>
1448 Make_Op_Not (Loc,
1449 Right_Opnd =>
1450 Make_Function_Call (Loc,
1451 Name => New_Occurrence_Of (Func_Id, Loc)))));
1453 Build_Conditional_Block (Loc,
1454 Cond =>
1455 Make_Function_Call (Loc,
1456 Name => New_Occurrence_Of (Func_Id, Loc)),
1457 Loop_Stmt => Relocate_Node (Loop_Stmt),
1458 If_Stmt => Result,
1459 Blk_Stmt => Blk);
1460 end;
1462 -- Ada 2012 iteration over an array is transformed into:
1464 -- if <Array_Nam>'Length (1) > 0
1465 -- and then <Array_Nam>'Length (N) > 0
1466 -- then
1467 -- declare
1468 -- Temp1 : constant <type of Pref1> := <Pref1>;
1469 -- . . .
1470 -- TempN : constant <type of PrefN> := <PrefN>;
1471 -- begin
1472 -- for X in ... loop -- multiple loops depending on dims
1473 -- <original source statements with attribute rewrites>
1474 -- end loop;
1475 -- end;
1476 -- end if;
1478 elsif Is_Array_Iteration (Loop_Stmt) then
1479 declare
1480 Array_Nam : constant Entity_Id :=
1481 Entity (Name (Iterator_Specification
1482 (Iteration_Scheme (Original_Node (Loop_Stmt)))));
1483 Num_Dims : constant Pos :=
1484 Number_Dimensions (Etype (Array_Nam));
1485 Cond : Node_Id := Empty;
1486 Check : Node_Id;
1488 begin
1489 -- Generate a check which determines whether all dimensions of
1490 -- the array are non-null.
1492 for Dim in 1 .. Num_Dims loop
1493 Check :=
1494 Make_Op_Gt (Loc,
1495 Left_Opnd =>
1496 Make_Attribute_Reference (Loc,
1497 Prefix => New_Occurrence_Of (Array_Nam, Loc),
1498 Attribute_Name => Name_Length,
1499 Expressions => New_List (
1500 Make_Integer_Literal (Loc, Dim))),
1501 Right_Opnd =>
1502 Make_Integer_Literal (Loc, 0));
1504 if No (Cond) then
1505 Cond := Check;
1506 else
1507 Cond :=
1508 Make_And_Then (Loc,
1509 Left_Opnd => Cond,
1510 Right_Opnd => Check);
1511 end if;
1512 end loop;
1514 Build_Conditional_Block (Loc,
1515 Cond => Cond,
1516 Loop_Stmt => Relocate_Node (Loop_Stmt),
1517 If_Stmt => Result,
1518 Blk_Stmt => Blk);
1519 end;
1521 -- For loops are transformed into:
1523 -- if <Low> <= <High> then
1524 -- declare
1525 -- Temp1 : constant <type of Pref1> := <Pref1>;
1526 -- . . .
1527 -- TempN : constant <type of PrefN> := <PrefN>;
1528 -- begin
1529 -- for <Def_Id> in <Low> .. <High> loop
1530 -- <original source statements with attribute rewrites>
1531 -- end loop;
1532 -- end;
1533 -- end if;
1535 elsif Present (Loop_Parameter_Specification (Scheme)) then
1536 declare
1537 Loop_Spec : constant Node_Id :=
1538 Loop_Parameter_Specification (Scheme);
1539 Cond : Node_Id;
1540 Subt_Def : Node_Id;
1542 begin
1543 Subt_Def := Discrete_Subtype_Definition (Loop_Spec);
1545 -- When the loop iterates over a subtype indication with a
1546 -- range, use the low and high bounds of the subtype itself.
1548 if Nkind (Subt_Def) = N_Subtype_Indication then
1549 Subt_Def := Scalar_Range (Etype (Subt_Def));
1550 end if;
1552 pragma Assert (Nkind (Subt_Def) = N_Range);
1554 -- Generate
1555 -- Low <= High
1557 Cond :=
1558 Make_Op_Le (Loc,
1559 Left_Opnd => New_Copy_Tree (Low_Bound (Subt_Def)),
1560 Right_Opnd => New_Copy_Tree (High_Bound (Subt_Def)));
1562 Build_Conditional_Block (Loc,
1563 Cond => Cond,
1564 Loop_Stmt => Relocate_Node (Loop_Stmt),
1565 If_Stmt => Result,
1566 Blk_Stmt => Blk);
1567 end;
1568 end if;
1570 Decls := Declarations (Blk);
1571 end if;
1573 -- Step 3: Create a constant to capture the value of the prefix at the
1574 -- entry point into the loop.
1576 Temp_Id := Make_Temporary (Loc, 'P');
1578 -- Preserve the tag of the prefix by offering a specific view of the
1579 -- class-wide version of the prefix.
1581 if Is_Tagged_Type (Base_Typ) then
1582 Tagged_Case : declare
1583 CW_Temp : Entity_Id;
1584 CW_Typ : Entity_Id;
1586 begin
1587 -- Generate:
1588 -- CW_Temp : constant Base_Typ'Class := Base_Typ'Class (Pref);
1590 CW_Temp := Make_Temporary (Loc, 'T');
1591 CW_Typ := Class_Wide_Type (Base_Typ);
1593 Aux_Decl :=
1594 Make_Object_Declaration (Loc,
1595 Defining_Identifier => CW_Temp,
1596 Constant_Present => True,
1597 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
1598 Expression =>
1599 Convert_To (CW_Typ, Relocate_Node (Pref)));
1600 Append_To (Decls, Aux_Decl);
1602 -- Generate:
1603 -- Temp : Base_Typ renames Base_Typ (CW_Temp);
1605 Temp_Decl :=
1606 Make_Object_Renaming_Declaration (Loc,
1607 Defining_Identifier => Temp_Id,
1608 Subtype_Mark => New_Occurrence_Of (Base_Typ, Loc),
1609 Name =>
1610 Convert_To (Base_Typ, New_Occurrence_Of (CW_Temp, Loc)));
1611 Append_To (Decls, Temp_Decl);
1612 end Tagged_Case;
1614 -- Untagged case
1616 else
1617 Untagged_Case : declare
1618 Temp_Expr : Node_Id;
1620 begin
1621 Aux_Decl := Empty;
1623 -- Generate a nominal type for the constant when the prefix is of
1624 -- a constrained type. This is achieved by setting the Etype of
1625 -- the relocated prefix to its base type. Since the prefix is now
1626 -- the initialization expression of the constant, its freezing
1627 -- will produce a proper nominal type.
1629 Temp_Expr := Relocate_Node (Pref);
1630 Set_Etype (Temp_Expr, Base_Typ);
1632 -- Generate:
1633 -- Temp : constant Base_Typ := Pref;
1635 Temp_Decl :=
1636 Make_Object_Declaration (Loc,
1637 Defining_Identifier => Temp_Id,
1638 Constant_Present => True,
1639 Object_Definition => New_Occurrence_Of (Base_Typ, Loc),
1640 Expression => Temp_Expr);
1641 Append_To (Decls, Temp_Decl);
1642 end Untagged_Case;
1643 end if;
1645 -- Step 4: Analyze all bits
1647 Installed := Current_Scope = Scope (Loop_Id);
1649 -- Depending on the pracement of attribute 'Loop_Entry relative to the
1650 -- associated loop, ensure the proper visibility for analysis.
1652 if not Installed then
1653 Push_Scope (Scope (Loop_Id));
1654 end if;
1656 -- The analysis of the conditional block takes care of the constant
1657 -- declaration.
1659 if Present (Result) then
1660 Rewrite (Loop_Stmt, Result);
1661 Analyze (Loop_Stmt);
1663 -- The conditional block was analyzed when a previous 'Loop_Entry was
1664 -- expanded. There is no point in reanalyzing the block, simply analyze
1665 -- the declaration of the constant.
1667 else
1668 if Present (Aux_Decl) then
1669 Analyze (Aux_Decl);
1670 end if;
1672 Analyze (Temp_Decl);
1673 end if;
1675 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
1676 Analyze (N);
1678 if not Installed then
1679 Pop_Scope;
1680 end if;
1681 end Expand_Loop_Entry_Attribute;
1683 ------------------------------
1684 -- Expand_Min_Max_Attribute --
1685 ------------------------------
1687 procedure Expand_Min_Max_Attribute (N : Node_Id) is
1688 begin
1689 -- Min and Max are handled by the back end (except that static cases
1690 -- have already been evaluated during semantic processing, although the
1691 -- back end should not count on this). The one bit of special processing
1692 -- required in the normal case is that these two attributes typically
1693 -- generate conditionals in the code, so check the relevant restriction.
1695 Check_Restriction (No_Implicit_Conditionals, N);
1697 -- In Modify_Tree_For_C mode, we rewrite as an if expression
1699 if Modify_Tree_For_C then
1700 declare
1701 Loc : constant Source_Ptr := Sloc (N);
1702 Typ : constant Entity_Id := Etype (N);
1703 Expr : constant Node_Id := First (Expressions (N));
1704 Left : constant Node_Id := Relocate_Node (Expr);
1705 Right : constant Node_Id := Relocate_Node (Next (Expr));
1707 function Make_Compare (Left, Right : Node_Id) return Node_Id;
1708 -- Returns Left >= Right for Max, Left <= Right for Min
1710 ------------------
1711 -- Make_Compare --
1712 ------------------
1714 function Make_Compare (Left, Right : Node_Id) return Node_Id is
1715 begin
1716 if Attribute_Name (N) = Name_Max then
1717 return
1718 Make_Op_Ge (Loc,
1719 Left_Opnd => Left,
1720 Right_Opnd => Right);
1721 else
1722 return
1723 Make_Op_Le (Loc,
1724 Left_Opnd => Left,
1725 Right_Opnd => Right);
1726 end if;
1727 end Make_Compare;
1729 -- Start of processing for Min_Max
1731 begin
1732 -- If both Left and Right are side effect free, then we can just
1733 -- use Duplicate_Expr to duplicate the references and return
1735 -- (if Left >=|<= Right then Left else Right)
1737 if Side_Effect_Free (Left) and then Side_Effect_Free (Right) then
1738 Rewrite (N,
1739 Make_If_Expression (Loc,
1740 Expressions => New_List (
1741 Make_Compare (Left, Right),
1742 Duplicate_Subexpr_No_Checks (Left),
1743 Duplicate_Subexpr_No_Checks (Right))));
1745 -- Otherwise we generate declarations to capture the values.
1747 -- The translation is
1749 -- do
1750 -- T1 : constant typ := Left;
1751 -- T2 : constant typ := Right;
1752 -- in
1753 -- (if T1 >=|<= T2 then T1 else T2)
1754 -- end;
1756 else
1757 declare
1758 T1 : constant Entity_Id := Make_Temporary (Loc, 'T', Left);
1759 T2 : constant Entity_Id := Make_Temporary (Loc, 'T', Right);
1761 begin
1762 Rewrite (N,
1763 Make_Expression_With_Actions (Loc,
1764 Actions => New_List (
1765 Make_Object_Declaration (Loc,
1766 Defining_Identifier => T1,
1767 Constant_Present => True,
1768 Object_Definition =>
1769 New_Occurrence_Of (Etype (Left), Loc),
1770 Expression => Relocate_Node (Left)),
1772 Make_Object_Declaration (Loc,
1773 Defining_Identifier => T2,
1774 Constant_Present => True,
1775 Object_Definition =>
1776 New_Occurrence_Of (Etype (Right), Loc),
1777 Expression => Relocate_Node (Right))),
1779 Expression =>
1780 Make_If_Expression (Loc,
1781 Expressions => New_List (
1782 Make_Compare
1783 (New_Occurrence_Of (T1, Loc),
1784 New_Occurrence_Of (T2, Loc)),
1785 New_Occurrence_Of (T1, Loc),
1786 New_Occurrence_Of (T2, Loc)))));
1787 end;
1788 end if;
1790 Analyze_And_Resolve (N, Typ);
1791 end;
1792 end if;
1793 end Expand_Min_Max_Attribute;
1795 ----------------------------------
1796 -- Expand_N_Attribute_Reference --
1797 ----------------------------------
1799 procedure Expand_N_Attribute_Reference (N : Node_Id) is
1800 Loc : constant Source_Ptr := Sloc (N);
1801 Typ : constant Entity_Id := Etype (N);
1802 Btyp : constant Entity_Id := Base_Type (Typ);
1803 Pref : constant Node_Id := Prefix (N);
1804 Ptyp : constant Entity_Id := Etype (Pref);
1805 Exprs : constant List_Id := Expressions (N);
1806 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
1808 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
1809 -- Rewrites a stream attribute for Read, Write or Output with the
1810 -- procedure call. Pname is the entity for the procedure to call.
1812 ------------------------------
1813 -- Rewrite_Stream_Proc_Call --
1814 ------------------------------
1816 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
1817 Item : constant Node_Id := Next (First (Exprs));
1818 Item_Typ : constant Entity_Id := Etype (Item);
1819 Formal : constant Entity_Id := Next_Formal (First_Formal (Pname));
1820 Formal_Typ : constant Entity_Id := Etype (Formal);
1821 Is_Written : constant Boolean := Ekind (Formal) /= E_In_Parameter;
1823 begin
1824 -- The expansion depends on Item, the second actual, which is
1825 -- the object being streamed in or out.
1827 -- If the item is a component of a packed array type, and
1828 -- a conversion is needed on exit, we introduce a temporary to
1829 -- hold the value, because otherwise the packed reference will
1830 -- not be properly expanded.
1832 if Nkind (Item) = N_Indexed_Component
1833 and then Is_Packed (Base_Type (Etype (Prefix (Item))))
1834 and then Base_Type (Item_Typ) /= Base_Type (Formal_Typ)
1835 and then Is_Written
1836 then
1837 declare
1838 Temp : constant Entity_Id := Make_Temporary (Loc, 'V');
1839 Decl : Node_Id;
1840 Assn : Node_Id;
1842 begin
1843 Decl :=
1844 Make_Object_Declaration (Loc,
1845 Defining_Identifier => Temp,
1846 Object_Definition => New_Occurrence_Of (Formal_Typ, Loc));
1847 Set_Etype (Temp, Formal_Typ);
1849 Assn :=
1850 Make_Assignment_Statement (Loc,
1851 Name => New_Copy_Tree (Item),
1852 Expression =>
1853 Unchecked_Convert_To
1854 (Item_Typ, New_Occurrence_Of (Temp, Loc)));
1856 Rewrite (Item, New_Occurrence_Of (Temp, Loc));
1857 Insert_Actions (N,
1858 New_List (
1859 Decl,
1860 Make_Procedure_Call_Statement (Loc,
1861 Name => New_Occurrence_Of (Pname, Loc),
1862 Parameter_Associations => Exprs),
1863 Assn));
1865 Rewrite (N, Make_Null_Statement (Loc));
1866 return;
1867 end;
1868 end if;
1870 -- For the class-wide dispatching cases, and for cases in which
1871 -- the base type of the second argument matches the base type of
1872 -- the corresponding formal parameter (that is to say the stream
1873 -- operation is not inherited), we are all set, and can use the
1874 -- argument unchanged.
1876 if not Is_Class_Wide_Type (Entity (Pref))
1877 and then not Is_Class_Wide_Type (Etype (Item))
1878 and then Base_Type (Item_Typ) /= Base_Type (Formal_Typ)
1879 then
1880 -- Perform a view conversion when either the argument or the
1881 -- formal parameter are of a private type.
1883 if Is_Private_Type (Base_Type (Formal_Typ))
1884 or else Is_Private_Type (Base_Type (Item_Typ))
1885 then
1886 Rewrite (Item,
1887 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
1889 -- Otherwise perform a regular type conversion to ensure that all
1890 -- relevant checks are installed.
1892 else
1893 Rewrite (Item, Convert_To (Formal_Typ, Relocate_Node (Item)));
1894 end if;
1896 -- For untagged derived types set Assignment_OK, to prevent
1897 -- copies from being created when the unchecked conversion
1898 -- is expanded (which would happen in Remove_Side_Effects
1899 -- if Expand_N_Unchecked_Conversion were allowed to call
1900 -- Force_Evaluation). The copy could violate Ada semantics in
1901 -- cases such as an actual that is an out parameter. Note that
1902 -- this approach is also used in exp_ch7 for calls to controlled
1903 -- type operations to prevent problems with actuals wrapped in
1904 -- unchecked conversions.
1906 if Is_Untagged_Derivation (Etype (Expression (Item))) then
1907 Set_Assignment_OK (Item);
1908 end if;
1909 end if;
1911 -- The stream operation to call may be a renaming created by an
1912 -- attribute definition clause, and may not be frozen yet. Ensure
1913 -- that it has the necessary extra formals.
1915 if not Is_Frozen (Pname) then
1916 Create_Extra_Formals (Pname);
1917 end if;
1919 -- And now rewrite the call
1921 Rewrite (N,
1922 Make_Procedure_Call_Statement (Loc,
1923 Name => New_Occurrence_Of (Pname, Loc),
1924 Parameter_Associations => Exprs));
1926 Analyze (N);
1927 end Rewrite_Stream_Proc_Call;
1929 -- Start of processing for Expand_N_Attribute_Reference
1931 begin
1932 -- Do required validity checking, if enabled. Do not apply check to
1933 -- output parameters of an Asm instruction, since the value of this
1934 -- is not set till after the attribute has been elaborated, and do
1935 -- not apply the check to the arguments of a 'Read or 'Input attribute
1936 -- reference since the scalar argument is an OUT scalar.
1938 if Validity_Checks_On and then Validity_Check_Operands
1939 and then Id /= Attribute_Asm_Output
1940 and then Id /= Attribute_Read
1941 and then Id /= Attribute_Input
1942 then
1943 declare
1944 Expr : Node_Id;
1945 begin
1946 Expr := First (Expressions (N));
1947 while Present (Expr) loop
1948 Ensure_Valid (Expr);
1949 Next (Expr);
1950 end loop;
1951 end;
1952 end if;
1954 -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in-
1955 -- place function, then a temporary return object needs to be created
1956 -- and access to it must be passed to the function.
1958 if Is_Build_In_Place_Function_Call (Pref) then
1960 -- If attribute is 'Old, the context is a postcondition, and
1961 -- the temporary must go in the corresponding subprogram, not
1962 -- the postcondition function or any created blocks, as when
1963 -- the attribute appears in a quantified expression. This is
1964 -- handled below in the expansion of the attribute.
1966 if Attribute_Name (Parent (Pref)) = Name_Old then
1967 null;
1968 else
1969 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
1970 end if;
1972 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
1973 -- containing build-in-place function calls whose returned object covers
1974 -- interface types.
1976 elsif Present (Unqual_BIP_Iface_Function_Call (Pref)) then
1977 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Pref);
1978 end if;
1980 -- If prefix is a protected type name, this is a reference to the
1981 -- current instance of the type. For a component definition, nothing
1982 -- to do (expansion will occur in the init proc). In other contexts,
1983 -- rewrite into reference to current instance.
1985 if Is_Protected_Self_Reference (Pref)
1986 and then not
1987 (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint,
1988 N_Discriminant_Association)
1989 and then Nkind (Parent (Parent (Parent (Parent (N))))) =
1990 N_Component_Definition)
1992 -- No action needed for these attributes since the current instance
1993 -- will be rewritten to be the name of the _object parameter
1994 -- associated with the enclosing protected subprogram (see below).
1996 and then Id /= Attribute_Access
1997 and then Id /= Attribute_Unchecked_Access
1998 and then Id /= Attribute_Unrestricted_Access
1999 then
2000 Rewrite (Pref, Concurrent_Ref (Pref));
2001 Analyze (Pref);
2002 end if;
2004 -- Remaining processing depends on specific attribute
2006 -- Note: individual sections of the following case statement are
2007 -- allowed to assume there is no code after the case statement, and
2008 -- are legitimately allowed to execute return statements if they have
2009 -- nothing more to do.
2011 case Id is
2013 -- Attributes related to Ada 2012 iterators
2015 when Attribute_Constant_Indexing
2016 | Attribute_Default_Iterator
2017 | Attribute_Implicit_Dereference
2018 | Attribute_Iterable
2019 | Attribute_Iterator_Element
2020 | Attribute_Variable_Indexing
2022 null;
2024 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
2025 -- were already rejected by the parser. Thus they shouldn't appear here.
2027 when Internal_Attribute_Id =>
2028 raise Program_Error;
2030 ------------
2031 -- Access --
2032 ------------
2034 when Attribute_Access
2035 | Attribute_Unchecked_Access
2036 | Attribute_Unrestricted_Access
2038 Access_Cases : declare
2039 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
2040 Btyp_DDT : Entity_Id;
2042 function Enclosing_Object (N : Node_Id) return Node_Id;
2043 -- If N denotes a compound name (selected component, indexed
2044 -- component, or slice), returns the name of the outermost such
2045 -- enclosing object. Otherwise returns N. If the object is a
2046 -- renaming, then the renamed object is returned.
2048 ----------------------
2049 -- Enclosing_Object --
2050 ----------------------
2052 function Enclosing_Object (N : Node_Id) return Node_Id is
2053 Obj_Name : Node_Id;
2055 begin
2056 Obj_Name := N;
2057 while Nkind_In (Obj_Name, N_Selected_Component,
2058 N_Indexed_Component,
2059 N_Slice)
2060 loop
2061 Obj_Name := Prefix (Obj_Name);
2062 end loop;
2064 return Get_Referenced_Object (Obj_Name);
2065 end Enclosing_Object;
2067 -- Local declarations
2069 Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object);
2071 -- Start of processing for Access_Cases
2073 begin
2074 Btyp_DDT := Designated_Type (Btyp);
2076 -- Handle designated types that come from the limited view
2078 if From_Limited_With (Btyp_DDT)
2079 and then Has_Non_Limited_View (Btyp_DDT)
2080 then
2081 Btyp_DDT := Non_Limited_View (Btyp_DDT);
2082 end if;
2084 -- In order to improve the text of error messages, the designated
2085 -- type of access-to-subprogram itypes is set by the semantics as
2086 -- the associated subprogram entity (see sem_attr). Now we replace
2087 -- such node with the proper E_Subprogram_Type itype.
2089 if Id = Attribute_Unrestricted_Access
2090 and then Is_Subprogram (Directly_Designated_Type (Typ))
2091 then
2092 -- The following conditions ensure that this special management
2093 -- is done only for "Address!(Prim'Unrestricted_Access)" nodes.
2094 -- At this stage other cases in which the designated type is
2095 -- still a subprogram (instead of an E_Subprogram_Type) are
2096 -- wrong because the semantics must have overridden the type of
2097 -- the node with the type imposed by the context.
2099 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
2100 and then Etype (Parent (N)) = RTE (RE_Prim_Ptr)
2101 then
2102 Set_Etype (N, RTE (RE_Prim_Ptr));
2104 else
2105 declare
2106 Subp : constant Entity_Id :=
2107 Directly_Designated_Type (Typ);
2108 Etyp : Entity_Id;
2109 Extra : Entity_Id := Empty;
2110 New_Formal : Entity_Id;
2111 Old_Formal : Entity_Id := First_Formal (Subp);
2112 Subp_Typ : Entity_Id;
2114 begin
2115 Subp_Typ := Create_Itype (E_Subprogram_Type, N);
2116 Set_Etype (Subp_Typ, Etype (Subp));
2117 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
2119 if Present (Old_Formal) then
2120 New_Formal := New_Copy (Old_Formal);
2121 Set_First_Entity (Subp_Typ, New_Formal);
2123 loop
2124 Set_Scope (New_Formal, Subp_Typ);
2125 Etyp := Etype (New_Formal);
2127 -- Handle itypes. There is no need to duplicate
2128 -- here the itypes associated with record types
2129 -- (i.e the implicit full view of private types).
2131 if Is_Itype (Etyp)
2132 and then Ekind (Base_Type (Etyp)) /= E_Record_Type
2133 then
2134 Extra := New_Copy (Etyp);
2135 Set_Parent (Extra, New_Formal);
2136 Set_Etype (New_Formal, Extra);
2137 Set_Scope (Extra, Subp_Typ);
2138 end if;
2140 Extra := New_Formal;
2141 Next_Formal (Old_Formal);
2142 exit when No (Old_Formal);
2144 Link_Entities (New_Formal, New_Copy (Old_Formal));
2145 Next_Entity (New_Formal);
2146 end loop;
2148 Unlink_Next_Entity (New_Formal);
2149 Set_Last_Entity (Subp_Typ, Extra);
2150 end if;
2152 -- Now that the explicit formals have been duplicated,
2153 -- any extra formals needed by the subprogram must be
2154 -- created.
2156 if Present (Extra) then
2157 Set_Extra_Formal (Extra, Empty);
2158 end if;
2160 Create_Extra_Formals (Subp_Typ);
2161 Set_Directly_Designated_Type (Typ, Subp_Typ);
2162 end;
2163 end if;
2164 end if;
2166 if Is_Access_Protected_Subprogram_Type (Btyp) then
2167 Expand_Access_To_Protected_Op (N, Pref, Typ);
2169 -- If prefix is a type name, this is a reference to the current
2170 -- instance of the type, within its initialization procedure.
2172 elsif Is_Entity_Name (Pref)
2173 and then Is_Type (Entity (Pref))
2174 then
2175 declare
2176 Par : Node_Id;
2177 Formal : Entity_Id;
2179 begin
2180 -- If the current instance name denotes a task type, then
2181 -- the access attribute is rewritten to be the name of the
2182 -- "_task" parameter associated with the task type's task
2183 -- procedure. An unchecked conversion is applied to ensure
2184 -- a type match in cases of expander-generated calls (e.g.
2185 -- init procs).
2187 if Is_Task_Type (Entity (Pref)) then
2188 Formal :=
2189 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
2190 while Present (Formal) loop
2191 exit when Chars (Formal) = Name_uTask;
2192 Next_Entity (Formal);
2193 end loop;
2195 pragma Assert (Present (Formal));
2197 Rewrite (N,
2198 Unchecked_Convert_To (Typ,
2199 New_Occurrence_Of (Formal, Loc)));
2200 Set_Etype (N, Typ);
2202 elsif Is_Protected_Type (Entity (Pref)) then
2204 -- No action needed for current instance located in a
2205 -- component definition (expansion will occur in the
2206 -- init proc)
2208 if Is_Protected_Type (Current_Scope) then
2209 null;
2211 -- If the current instance reference is located in a
2212 -- protected subprogram or entry then rewrite the access
2213 -- attribute to be the name of the "_object" parameter.
2214 -- An unchecked conversion is applied to ensure a type
2215 -- match in cases of expander-generated calls (e.g. init
2216 -- procs).
2218 -- The code may be nested in a block, so find enclosing
2219 -- scope that is a protected operation.
2221 else
2222 declare
2223 Subp : Entity_Id;
2225 begin
2226 Subp := Current_Scope;
2227 while Ekind_In (Subp, E_Loop, E_Block) loop
2228 Subp := Scope (Subp);
2229 end loop;
2231 Formal :=
2232 First_Entity
2233 (Protected_Body_Subprogram (Subp));
2235 -- For a protected subprogram the _Object parameter
2236 -- is the protected record, so we create an access
2237 -- to it. The _Object parameter of an entry is an
2238 -- address.
2240 if Ekind (Subp) = E_Entry then
2241 Rewrite (N,
2242 Unchecked_Convert_To (Typ,
2243 New_Occurrence_Of (Formal, Loc)));
2244 Set_Etype (N, Typ);
2246 else
2247 Rewrite (N,
2248 Unchecked_Convert_To (Typ,
2249 Make_Attribute_Reference (Loc,
2250 Attribute_Name => Name_Unrestricted_Access,
2251 Prefix =>
2252 New_Occurrence_Of (Formal, Loc))));
2253 Analyze_And_Resolve (N);
2254 end if;
2255 end;
2256 end if;
2258 -- The expression must appear in a default expression,
2259 -- (which in the initialization procedure is the right-hand
2260 -- side of an assignment), and not in a discriminant
2261 -- constraint.
2263 else
2264 Par := Parent (N);
2265 while Present (Par) loop
2266 exit when Nkind (Par) = N_Assignment_Statement;
2268 if Nkind (Par) = N_Component_Declaration then
2269 return;
2270 end if;
2272 Par := Parent (Par);
2273 end loop;
2275 if Present (Par) then
2276 Rewrite (N,
2277 Make_Attribute_Reference (Loc,
2278 Prefix => Make_Identifier (Loc, Name_uInit),
2279 Attribute_Name => Attribute_Name (N)));
2281 Analyze_And_Resolve (N, Typ);
2282 end if;
2283 end if;
2284 end;
2286 -- If the prefix of an Access attribute is a dereference of an
2287 -- access parameter (or a renaming of such a dereference, or a
2288 -- subcomponent of such a dereference) and the context is a
2289 -- general access type (including the type of an object or
2290 -- component with an access_definition, but not the anonymous
2291 -- type of an access parameter or access discriminant), then
2292 -- apply an accessibility check to the access parameter. We used
2293 -- to rewrite the access parameter as a type conversion, but that
2294 -- could only be done if the immediate prefix of the Access
2295 -- attribute was the dereference, and didn't handle cases where
2296 -- the attribute is applied to a subcomponent of the dereference,
2297 -- since there's generally no available, appropriate access type
2298 -- to convert to in that case. The attribute is passed as the
2299 -- point to insert the check, because the access parameter may
2300 -- come from a renaming, possibly in a different scope, and the
2301 -- check must be associated with the attribute itself.
2303 elsif Id = Attribute_Access
2304 and then Nkind (Enc_Object) = N_Explicit_Dereference
2305 and then Is_Entity_Name (Prefix (Enc_Object))
2306 and then (Ekind (Btyp) = E_General_Access_Type
2307 or else Is_Local_Anonymous_Access (Btyp))
2308 and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind
2309 and then Ekind (Etype (Entity (Prefix (Enc_Object))))
2310 = E_Anonymous_Access_Type
2311 and then Present (Extra_Accessibility
2312 (Entity (Prefix (Enc_Object))))
2313 then
2314 Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N);
2316 -- Ada 2005 (AI-251): If the designated type is an interface we
2317 -- add an implicit conversion to force the displacement of the
2318 -- pointer to reference the secondary dispatch table.
2320 elsif Is_Interface (Btyp_DDT)
2321 and then (Comes_From_Source (N)
2322 or else Comes_From_Source (Ref_Object)
2323 or else (Nkind (Ref_Object) in N_Has_Chars
2324 and then Chars (Ref_Object) = Name_uInit))
2325 then
2326 if Nkind (Ref_Object) /= N_Explicit_Dereference then
2328 -- No implicit conversion required if types match, or if
2329 -- the prefix is the class_wide_type of the interface. In
2330 -- either case passing an object of the interface type has
2331 -- already set the pointer correctly.
2333 if Btyp_DDT = Etype (Ref_Object)
2334 or else (Is_Class_Wide_Type (Etype (Ref_Object))
2335 and then
2336 Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object))
2337 then
2338 null;
2340 else
2341 Rewrite (Prefix (N),
2342 Convert_To (Btyp_DDT,
2343 New_Copy_Tree (Prefix (N))));
2345 Analyze_And_Resolve (Prefix (N), Btyp_DDT);
2346 end if;
2348 -- When the object is an explicit dereference, convert the
2349 -- dereference's prefix.
2351 else
2352 declare
2353 Obj_DDT : constant Entity_Id :=
2354 Base_Type
2355 (Directly_Designated_Type
2356 (Etype (Prefix (Ref_Object))));
2357 begin
2358 -- No implicit conversion required if designated types
2359 -- match.
2361 if Obj_DDT /= Btyp_DDT
2362 and then not (Is_Class_Wide_Type (Obj_DDT)
2363 and then Etype (Obj_DDT) = Btyp_DDT)
2364 then
2365 Rewrite (N,
2366 Convert_To (Typ,
2367 New_Copy_Tree (Prefix (Ref_Object))));
2368 Analyze_And_Resolve (N, Typ);
2369 end if;
2370 end;
2371 end if;
2372 end if;
2373 end Access_Cases;
2375 --------------
2376 -- Adjacent --
2377 --------------
2379 -- Transforms 'Adjacent into a call to the floating-point attribute
2380 -- function Adjacent in Fat_xxx (where xxx is the root type)
2382 when Attribute_Adjacent =>
2383 Expand_Fpt_Attribute_RR (N);
2385 -------------
2386 -- Address --
2387 -------------
2389 when Attribute_Address => Address : declare
2390 Task_Proc : Entity_Id;
2392 begin
2393 -- If the prefix is a task or a task type, the useful address is that
2394 -- of the procedure for the task body, i.e. the actual program unit.
2395 -- We replace the original entity with that of the procedure.
2397 if Is_Entity_Name (Pref)
2398 and then Is_Task_Type (Entity (Pref))
2399 then
2400 Task_Proc := Next_Entity (Root_Type (Ptyp));
2402 while Present (Task_Proc) loop
2403 exit when Ekind (Task_Proc) = E_Procedure
2404 and then Etype (First_Formal (Task_Proc)) =
2405 Corresponding_Record_Type (Ptyp);
2406 Next_Entity (Task_Proc);
2407 end loop;
2409 if Present (Task_Proc) then
2410 Set_Entity (Pref, Task_Proc);
2411 Set_Etype (Pref, Etype (Task_Proc));
2412 end if;
2414 -- Similarly, the address of a protected operation is the address
2415 -- of the corresponding protected body, regardless of the protected
2416 -- object from which it is selected.
2418 elsif Nkind (Pref) = N_Selected_Component
2419 and then Is_Subprogram (Entity (Selector_Name (Pref)))
2420 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
2421 then
2422 Rewrite (Pref,
2423 New_Occurrence_Of (
2424 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
2426 elsif Nkind (Pref) = N_Explicit_Dereference
2427 and then Ekind (Ptyp) = E_Subprogram_Type
2428 and then Convention (Ptyp) = Convention_Protected
2429 then
2430 -- The prefix is be a dereference of an access_to_protected_
2431 -- subprogram. The desired address is the second component of
2432 -- the record that represents the access.
2434 declare
2435 Addr : constant Entity_Id := Etype (N);
2436 Ptr : constant Node_Id := Prefix (Pref);
2437 T : constant Entity_Id :=
2438 Equivalent_Type (Base_Type (Etype (Ptr)));
2440 begin
2441 Rewrite (N,
2442 Unchecked_Convert_To (Addr,
2443 Make_Selected_Component (Loc,
2444 Prefix => Unchecked_Convert_To (T, Ptr),
2445 Selector_Name => New_Occurrence_Of (
2446 Next_Entity (First_Entity (T)), Loc))));
2448 Analyze_And_Resolve (N, Addr);
2449 end;
2451 -- Ada 2005 (AI-251): Class-wide interface objects are always
2452 -- "displaced" to reference the tag associated with the interface
2453 -- type. In order to obtain the real address of such objects we
2454 -- generate a call to a run-time subprogram that returns the base
2455 -- address of the object.
2457 -- This processing is not needed in the VM case, where dispatching
2458 -- issues are taken care of by the virtual machine.
2460 elsif Is_Class_Wide_Type (Ptyp)
2461 and then Is_Interface (Underlying_Type (Ptyp))
2462 and then Tagged_Type_Expansion
2463 and then not (Nkind (Pref) in N_Has_Entity
2464 and then Is_Subprogram (Entity (Pref)))
2465 then
2466 Rewrite (N,
2467 Make_Function_Call (Loc,
2468 Name => New_Occurrence_Of (RTE (RE_Base_Address), Loc),
2469 Parameter_Associations => New_List (
2470 Relocate_Node (N))));
2471 Analyze (N);
2472 return;
2473 end if;
2475 -- Deal with packed array reference, other cases are handled by
2476 -- the back end.
2478 if Involves_Packed_Array_Reference (Pref) then
2479 Expand_Packed_Address_Reference (N);
2480 end if;
2481 end Address;
2483 ---------------
2484 -- Alignment --
2485 ---------------
2487 when Attribute_Alignment => Alignment : declare
2488 New_Node : Node_Id;
2490 begin
2491 -- For class-wide types, X'Class'Alignment is transformed into a
2492 -- direct reference to the Alignment of the class type, so that the
2493 -- back end does not have to deal with the X'Class'Alignment
2494 -- reference.
2496 if Is_Entity_Name (Pref)
2497 and then Is_Class_Wide_Type (Entity (Pref))
2498 then
2499 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
2500 return;
2502 -- For x'Alignment applied to an object of a class wide type,
2503 -- transform X'Alignment into a call to the predefined primitive
2504 -- operation _Alignment applied to X.
2506 elsif Is_Class_Wide_Type (Ptyp) then
2507 New_Node :=
2508 Make_Attribute_Reference (Loc,
2509 Prefix => Pref,
2510 Attribute_Name => Name_Tag);
2512 New_Node := Build_Get_Alignment (Loc, New_Node);
2514 -- Case where the context is a specific integer type with which
2515 -- the original attribute was compatible. The function has a
2516 -- specific type as well, so to preserve the compatibility we
2517 -- must convert explicitly.
2519 if Typ /= Standard_Integer then
2520 New_Node := Convert_To (Typ, New_Node);
2521 end if;
2523 Rewrite (N, New_Node);
2524 Analyze_And_Resolve (N, Typ);
2525 return;
2527 -- For all other cases, we just have to deal with the case of
2528 -- the fact that the result can be universal.
2530 else
2531 Apply_Universal_Integer_Attribute_Checks (N);
2532 end if;
2533 end Alignment;
2535 ---------
2536 -- Bit --
2537 ---------
2539 -- We compute this if a packed array reference was present, otherwise we
2540 -- leave the computation up to the back end.
2542 when Attribute_Bit =>
2543 if Involves_Packed_Array_Reference (Pref) then
2544 Expand_Packed_Bit_Reference (N);
2545 else
2546 Apply_Universal_Integer_Attribute_Checks (N);
2547 end if;
2549 ------------------
2550 -- Bit_Position --
2551 ------------------
2553 -- We compute this if a component clause was present, otherwise we leave
2554 -- the computation up to the back end, since we don't know what layout
2555 -- will be chosen.
2557 -- Note that the attribute can apply to a naked record component
2558 -- in generated code (i.e. the prefix is an identifier that
2559 -- references the component or discriminant entity).
2561 when Attribute_Bit_Position => Bit_Position : declare
2562 CE : Entity_Id;
2564 begin
2565 if Nkind (Pref) = N_Identifier then
2566 CE := Entity (Pref);
2567 else
2568 CE := Entity (Selector_Name (Pref));
2569 end if;
2571 if Known_Static_Component_Bit_Offset (CE) then
2572 Rewrite (N,
2573 Make_Integer_Literal (Loc,
2574 Intval => Component_Bit_Offset (CE)));
2575 Analyze_And_Resolve (N, Typ);
2577 else
2578 Apply_Universal_Integer_Attribute_Checks (N);
2579 end if;
2580 end Bit_Position;
2582 ------------------
2583 -- Body_Version --
2584 ------------------
2586 -- A reference to P'Body_Version or P'Version is expanded to
2588 -- Vnn : Unsigned;
2589 -- pragma Import (C, Vnn, "uuuuT");
2590 -- ...
2591 -- Get_Version_String (Vnn)
2593 -- where uuuu is the unit name (dots replaced by double underscore)
2594 -- and T is B for the cases of Body_Version, or Version applied to a
2595 -- subprogram acting as its own spec, and S for Version applied to a
2596 -- subprogram spec or package. This sequence of code references the
2597 -- unsigned constant created in the main program by the binder.
2599 -- A special exception occurs for Standard, where the string returned
2600 -- is a copy of the library string in gnatvsn.ads.
2602 when Attribute_Body_Version
2603 | Attribute_Version
2605 Version : declare
2606 E : constant Entity_Id := Make_Temporary (Loc, 'V');
2607 Pent : Entity_Id;
2608 S : String_Id;
2610 begin
2611 -- If not library unit, get to containing library unit
2613 Pent := Entity (Pref);
2614 while Pent /= Standard_Standard
2615 and then Scope (Pent) /= Standard_Standard
2616 and then not Is_Child_Unit (Pent)
2617 loop
2618 Pent := Scope (Pent);
2619 end loop;
2621 -- Special case Standard and Standard.ASCII
2623 if Pent = Standard_Standard or else Pent = Standard_ASCII then
2624 Rewrite (N,
2625 Make_String_Literal (Loc,
2626 Strval => Verbose_Library_Version));
2628 -- All other cases
2630 else
2631 -- Build required string constant
2633 Get_Name_String (Get_Unit_Name (Pent));
2635 Start_String;
2636 for J in 1 .. Name_Len - 2 loop
2637 if Name_Buffer (J) = '.' then
2638 Store_String_Chars ("__");
2639 else
2640 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
2641 end if;
2642 end loop;
2644 -- Case of subprogram acting as its own spec, always use body
2646 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
2647 and then Nkind (Parent (Declaration_Node (Pent))) =
2648 N_Subprogram_Body
2649 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
2650 then
2651 Store_String_Chars ("B");
2653 -- Case of no body present, always use spec
2655 elsif not Unit_Requires_Body (Pent) then
2656 Store_String_Chars ("S");
2658 -- Otherwise use B for Body_Version, S for spec
2660 elsif Id = Attribute_Body_Version then
2661 Store_String_Chars ("B");
2662 else
2663 Store_String_Chars ("S");
2664 end if;
2666 S := End_String;
2667 Lib.Version_Referenced (S);
2669 -- Insert the object declaration
2671 Insert_Actions (N, New_List (
2672 Make_Object_Declaration (Loc,
2673 Defining_Identifier => E,
2674 Object_Definition =>
2675 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
2677 -- Set entity as imported with correct external name
2679 Set_Is_Imported (E);
2680 Set_Interface_Name (E, Make_String_Literal (Loc, S));
2682 -- Set entity as internal to ensure proper Sprint output of its
2683 -- implicit importation.
2685 Set_Is_Internal (E);
2687 -- And now rewrite original reference
2689 Rewrite (N,
2690 Make_Function_Call (Loc,
2691 Name =>
2692 New_Occurrence_Of (RTE (RE_Get_Version_String), Loc),
2693 Parameter_Associations => New_List (
2694 New_Occurrence_Of (E, Loc))));
2695 end if;
2697 Analyze_And_Resolve (N, RTE (RE_Version_String));
2698 end Version;
2700 -------------
2701 -- Ceiling --
2702 -------------
2704 -- Transforms 'Ceiling into a call to the floating-point attribute
2705 -- function Ceiling in Fat_xxx (where xxx is the root type)
2707 when Attribute_Ceiling =>
2708 Expand_Fpt_Attribute_R (N);
2710 --------------
2711 -- Callable --
2712 --------------
2714 -- Transforms 'Callable attribute into a call to the Callable function
2716 when Attribute_Callable =>
2718 -- We have an object of a task interface class-wide type as a prefix
2719 -- to Callable. Generate:
2720 -- callable (Task_Id (Pref._disp_get_task_id));
2722 if Ada_Version >= Ada_2005
2723 and then Ekind (Ptyp) = E_Class_Wide_Type
2724 and then Is_Interface (Ptyp)
2725 and then Is_Task_Interface (Ptyp)
2726 then
2727 Rewrite (N,
2728 Make_Function_Call (Loc,
2729 Name =>
2730 New_Occurrence_Of (RTE (RE_Callable), Loc),
2731 Parameter_Associations => New_List (
2732 Make_Unchecked_Type_Conversion (Loc,
2733 Subtype_Mark =>
2734 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
2735 Expression => Build_Disp_Get_Task_Id_Call (Pref)))));
2737 else
2738 Rewrite (N, Build_Call_With_Task (Pref, RTE (RE_Callable)));
2739 end if;
2741 Analyze_And_Resolve (N, Standard_Boolean);
2743 ------------
2744 -- Caller --
2745 ------------
2747 -- Transforms 'Caller attribute into a call to either the
2748 -- Task_Entry_Caller or the Protected_Entry_Caller function.
2750 when Attribute_Caller => Caller : declare
2751 Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id);
2752 Ent : constant Entity_Id := Entity (Pref);
2753 Conctype : constant Entity_Id := Scope (Ent);
2754 Nest_Depth : Integer := 0;
2755 Name : Node_Id;
2756 S : Entity_Id;
2758 begin
2759 -- Protected case
2761 if Is_Protected_Type (Conctype) then
2762 case Corresponding_Runtime_Package (Conctype) is
2763 when System_Tasking_Protected_Objects_Entries =>
2764 Name :=
2765 New_Occurrence_Of
2766 (RTE (RE_Protected_Entry_Caller), Loc);
2768 when System_Tasking_Protected_Objects_Single_Entry =>
2769 Name :=
2770 New_Occurrence_Of
2771 (RTE (RE_Protected_Single_Entry_Caller), Loc);
2773 when others =>
2774 raise Program_Error;
2775 end case;
2777 Rewrite (N,
2778 Unchecked_Convert_To (Id_Kind,
2779 Make_Function_Call (Loc,
2780 Name => Name,
2781 Parameter_Associations => New_List (
2782 New_Occurrence_Of
2783 (Find_Protection_Object (Current_Scope), Loc)))));
2785 -- Task case
2787 else
2788 -- Determine the nesting depth of the E'Caller attribute, that
2789 -- is, how many accept statements are nested within the accept
2790 -- statement for E at the point of E'Caller. The runtime uses
2791 -- this depth to find the specified entry call.
2793 for J in reverse 0 .. Scope_Stack.Last loop
2794 S := Scope_Stack.Table (J).Entity;
2796 -- We should not reach the scope of the entry, as it should
2797 -- already have been checked in Sem_Attr that this attribute
2798 -- reference is within a matching accept statement.
2800 pragma Assert (S /= Conctype);
2802 if S = Ent then
2803 exit;
2805 elsif Is_Entry (S) then
2806 Nest_Depth := Nest_Depth + 1;
2807 end if;
2808 end loop;
2810 Rewrite (N,
2811 Unchecked_Convert_To (Id_Kind,
2812 Make_Function_Call (Loc,
2813 Name =>
2814 New_Occurrence_Of (RTE (RE_Task_Entry_Caller), Loc),
2815 Parameter_Associations => New_List (
2816 Make_Integer_Literal (Loc,
2817 Intval => Int (Nest_Depth))))));
2818 end if;
2820 Analyze_And_Resolve (N, Id_Kind);
2821 end Caller;
2823 -------------
2824 -- Compose --
2825 -------------
2827 -- Transforms 'Compose into a call to the floating-point attribute
2828 -- function Compose in Fat_xxx (where xxx is the root type)
2830 -- Note: we strictly should have special code here to deal with the
2831 -- case of absurdly negative arguments (less than Integer'First)
2832 -- which will return a (signed) zero value, but it hardly seems
2833 -- worth the effort. Absurdly large positive arguments will raise
2834 -- constraint error which is fine.
2836 when Attribute_Compose =>
2837 Expand_Fpt_Attribute_RI (N);
2839 -----------------
2840 -- Constrained --
2841 -----------------
2843 when Attribute_Constrained => Constrained : declare
2844 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
2846 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
2847 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
2848 -- view of an aliased object whose subtype is constrained.
2850 ---------------------------------
2851 -- Is_Constrained_Aliased_View --
2852 ---------------------------------
2854 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
2855 E : Entity_Id;
2857 begin
2858 if Is_Entity_Name (Obj) then
2859 E := Entity (Obj);
2861 if Present (Renamed_Object (E)) then
2862 return Is_Constrained_Aliased_View (Renamed_Object (E));
2863 else
2864 return Is_Aliased (E) and then Is_Constrained (Etype (E));
2865 end if;
2867 else
2868 return Is_Aliased_View (Obj)
2869 and then
2870 (Is_Constrained (Etype (Obj))
2871 or else
2872 (Nkind (Obj) = N_Explicit_Dereference
2873 and then
2874 not Object_Type_Has_Constrained_Partial_View
2875 (Typ => Base_Type (Etype (Obj)),
2876 Scop => Current_Scope)));
2877 end if;
2878 end Is_Constrained_Aliased_View;
2880 -- Start of processing for Constrained
2882 begin
2883 -- Reference to a parameter where the value is passed as an extra
2884 -- actual, corresponding to the extra formal referenced by the
2885 -- Extra_Constrained field of the corresponding formal. If this
2886 -- is an entry in-parameter, it is replaced by a constant renaming
2887 -- for which Extra_Constrained is never created.
2889 if Present (Formal_Ent)
2890 and then Ekind (Formal_Ent) /= E_Constant
2891 and then Present (Extra_Constrained (Formal_Ent))
2892 then
2893 Rewrite (N,
2894 New_Occurrence_Of
2895 (Extra_Constrained (Formal_Ent), Sloc (N)));
2897 -- If the prefix is an access to object, the attribute applies to
2898 -- the designated object, so rewrite with an explicit dereference.
2900 elsif Is_Access_Type (Etype (Pref))
2901 and then
2902 (not Is_Entity_Name (Pref) or else Is_Object (Entity (Pref)))
2903 then
2904 Rewrite (Pref,
2905 Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
2906 Analyze_And_Resolve (N, Standard_Boolean);
2907 return;
2909 -- For variables with a Extra_Constrained field, we use the
2910 -- corresponding entity.
2912 elsif Nkind (Pref) = N_Identifier
2913 and then Ekind (Entity (Pref)) = E_Variable
2914 and then Present (Extra_Constrained (Entity (Pref)))
2915 then
2916 Rewrite (N,
2917 New_Occurrence_Of
2918 (Extra_Constrained (Entity (Pref)), Sloc (N)));
2920 -- For all other entity names, we can tell at compile time
2922 elsif Is_Entity_Name (Pref) then
2923 declare
2924 Ent : constant Entity_Id := Entity (Pref);
2925 Res : Boolean;
2927 begin
2928 -- (RM J.4) obsolescent cases
2930 if Is_Type (Ent) then
2932 -- Private type
2934 if Is_Private_Type (Ent) then
2935 Res := not Has_Discriminants (Ent)
2936 or else Is_Constrained (Ent);
2938 -- It not a private type, must be a generic actual type
2939 -- that corresponded to a private type. We know that this
2940 -- correspondence holds, since otherwise the reference
2941 -- within the generic template would have been illegal.
2943 else
2944 if Is_Composite_Type (Underlying_Type (Ent)) then
2945 Res := Is_Constrained (Ent);
2946 else
2947 Res := True;
2948 end if;
2949 end if;
2951 else
2952 -- For access type, apply access check as needed
2954 if Is_Access_Type (Ptyp) then
2955 Apply_Access_Check (N);
2956 end if;
2958 -- If the prefix is not a variable or is aliased, then
2959 -- definitely true; if it's a formal parameter without an
2960 -- associated extra formal, then treat it as constrained.
2962 -- Ada 2005 (AI-363): An aliased prefix must be known to be
2963 -- constrained in order to set the attribute to True.
2965 if not Is_Variable (Pref)
2966 or else Present (Formal_Ent)
2967 or else (Ada_Version < Ada_2005
2968 and then Is_Aliased_View (Pref))
2969 or else (Ada_Version >= Ada_2005
2970 and then Is_Constrained_Aliased_View (Pref))
2971 then
2972 Res := True;
2974 -- Variable case, look at type to see if it is constrained.
2975 -- Note that the one case where this is not accurate (the
2976 -- procedure formal case), has been handled above.
2978 -- We use the Underlying_Type here (and below) in case the
2979 -- type is private without discriminants, but the full type
2980 -- has discriminants. This case is illegal, but we generate
2981 -- it internally for passing to the Extra_Constrained
2982 -- parameter.
2984 else
2985 -- In Ada 2012, test for case of a limited tagged type,
2986 -- in which case the attribute is always required to
2987 -- return True. The underlying type is tested, to make
2988 -- sure we also return True for cases where there is an
2989 -- unconstrained object with an untagged limited partial
2990 -- view which has defaulted discriminants (such objects
2991 -- always produce a False in earlier versions of
2992 -- Ada). (Ada 2012: AI05-0214)
2994 Res :=
2995 Is_Constrained (Underlying_Type (Etype (Ent)))
2996 or else
2997 (Ada_Version >= Ada_2012
2998 and then Is_Tagged_Type (Underlying_Type (Ptyp))
2999 and then Is_Limited_Type (Ptyp));
3000 end if;
3001 end if;
3003 Rewrite (N, New_Occurrence_Of (Boolean_Literals (Res), Loc));
3004 end;
3006 -- Prefix is not an entity name. These are also cases where we can
3007 -- always tell at compile time by looking at the form and type of the
3008 -- prefix. If an explicit dereference of an object with constrained
3009 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
3010 -- underlying type is a limited tagged type, then Constrained is
3011 -- required to always return True (Ada 2012: AI05-0214).
3013 else
3014 Rewrite (N,
3015 New_Occurrence_Of (
3016 Boolean_Literals (
3017 not Is_Variable (Pref)
3018 or else
3019 (Nkind (Pref) = N_Explicit_Dereference
3020 and then
3021 not Object_Type_Has_Constrained_Partial_View
3022 (Typ => Base_Type (Ptyp),
3023 Scop => Current_Scope))
3024 or else Is_Constrained (Underlying_Type (Ptyp))
3025 or else (Ada_Version >= Ada_2012
3026 and then Is_Tagged_Type (Underlying_Type (Ptyp))
3027 and then Is_Limited_Type (Ptyp))),
3028 Loc));
3029 end if;
3031 Analyze_And_Resolve (N, Standard_Boolean);
3032 end Constrained;
3034 ---------------
3035 -- Copy_Sign --
3036 ---------------
3038 -- Transforms 'Copy_Sign into a call to the floating-point attribute
3039 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
3041 when Attribute_Copy_Sign =>
3042 Expand_Fpt_Attribute_RR (N);
3044 -----------
3045 -- Count --
3046 -----------
3048 -- Transforms 'Count attribute into a call to the Count function
3050 when Attribute_Count => Count : declare
3051 Call : Node_Id;
3052 Conctyp : Entity_Id;
3053 Entnam : Node_Id;
3054 Entry_Id : Entity_Id;
3055 Index : Node_Id;
3056 Name : Node_Id;
3058 begin
3059 -- If the prefix is a member of an entry family, retrieve both
3060 -- entry name and index. For a simple entry there is no index.
3062 if Nkind (Pref) = N_Indexed_Component then
3063 Entnam := Prefix (Pref);
3064 Index := First (Expressions (Pref));
3065 else
3066 Entnam := Pref;
3067 Index := Empty;
3068 end if;
3070 Entry_Id := Entity (Entnam);
3072 -- Find the concurrent type in which this attribute is referenced
3073 -- (there had better be one).
3075 Conctyp := Current_Scope;
3076 while not Is_Concurrent_Type (Conctyp) loop
3077 Conctyp := Scope (Conctyp);
3078 end loop;
3080 -- Protected case
3082 if Is_Protected_Type (Conctyp) then
3084 -- No need to transform 'Count into a function call if the current
3085 -- scope has been eliminated. In this case such transformation is
3086 -- also not viable because the enclosing protected object is not
3087 -- available.
3089 if Is_Eliminated (Current_Scope) then
3090 return;
3091 end if;
3093 case Corresponding_Runtime_Package (Conctyp) is
3094 when System_Tasking_Protected_Objects_Entries =>
3095 Name := New_Occurrence_Of (RTE (RE_Protected_Count), Loc);
3097 Call :=
3098 Make_Function_Call (Loc,
3099 Name => Name,
3100 Parameter_Associations => New_List (
3101 New_Occurrence_Of
3102 (Find_Protection_Object (Current_Scope), Loc),
3103 Entry_Index_Expression
3104 (Loc, Entry_Id, Index, Scope (Entry_Id))));
3106 when System_Tasking_Protected_Objects_Single_Entry =>
3107 Name :=
3108 New_Occurrence_Of (RTE (RE_Protected_Count_Entry), Loc);
3110 Call :=
3111 Make_Function_Call (Loc,
3112 Name => Name,
3113 Parameter_Associations => New_List (
3114 New_Occurrence_Of
3115 (Find_Protection_Object (Current_Scope), Loc)));
3117 when others =>
3118 raise Program_Error;
3119 end case;
3121 -- Task case
3123 else
3124 Call :=
3125 Make_Function_Call (Loc,
3126 Name => New_Occurrence_Of (RTE (RE_Task_Count), Loc),
3127 Parameter_Associations => New_List (
3128 Entry_Index_Expression (Loc,
3129 Entry_Id, Index, Scope (Entry_Id))));
3130 end if;
3132 -- The call returns type Natural but the context is universal integer
3133 -- so any integer type is allowed. The attribute was already resolved
3134 -- so its Etype is the required result type. If the base type of the
3135 -- context type is other than Standard.Integer we put in a conversion
3136 -- to the required type. This can be a normal typed conversion since
3137 -- both input and output types of the conversion are integer types
3139 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
3140 Rewrite (N, Convert_To (Typ, Call));
3141 else
3142 Rewrite (N, Call);
3143 end if;
3145 Analyze_And_Resolve (N, Typ);
3146 end Count;
3148 ---------------------
3149 -- Descriptor_Size --
3150 ---------------------
3152 when Attribute_Descriptor_Size =>
3154 -- Attribute Descriptor_Size is handled by the back end when applied
3155 -- to an unconstrained array type.
3157 if Is_Array_Type (Ptyp)
3158 and then not Is_Constrained (Ptyp)
3159 then
3160 Apply_Universal_Integer_Attribute_Checks (N);
3162 -- For any other type, the descriptor size is 0 because there is no
3163 -- actual descriptor, but the result is not formally static.
3165 else
3166 Rewrite (N, Make_Integer_Literal (Loc, 0));
3167 Analyze (N);
3168 Set_Is_Static_Expression (N, False);
3169 end if;
3171 ---------------
3172 -- Elab_Body --
3173 ---------------
3175 -- This processing is shared by Elab_Spec
3177 -- What we do is to insert the following declarations
3179 -- procedure tnn;
3180 -- pragma Import (C, enn, "name___elabb/s");
3182 -- and then the Elab_Body/Spec attribute is replaced by a reference
3183 -- to this defining identifier.
3185 when Attribute_Elab_Body
3186 | Attribute_Elab_Spec
3188 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
3189 -- back-end knows how to handle these attributes directly.
3191 if CodePeer_Mode then
3192 return;
3193 end if;
3195 Elab_Body : declare
3196 Ent : constant Entity_Id := Make_Temporary (Loc, 'E');
3197 Str : String_Id;
3198 Lang : Node_Id;
3200 procedure Make_Elab_String (Nod : Node_Id);
3201 -- Given Nod, an identifier, or a selected component, put the
3202 -- image into the current string literal, with double underline
3203 -- between components.
3205 ----------------------
3206 -- Make_Elab_String --
3207 ----------------------
3209 procedure Make_Elab_String (Nod : Node_Id) is
3210 begin
3211 if Nkind (Nod) = N_Selected_Component then
3212 Make_Elab_String (Prefix (Nod));
3213 Store_String_Char ('_');
3214 Store_String_Char ('_');
3215 Get_Name_String (Chars (Selector_Name (Nod)));
3217 else
3218 pragma Assert (Nkind (Nod) = N_Identifier);
3219 Get_Name_String (Chars (Nod));
3220 end if;
3222 Store_String_Chars (Name_Buffer (1 .. Name_Len));
3223 end Make_Elab_String;
3225 -- Start of processing for Elab_Body/Elab_Spec
3227 begin
3228 -- First we need to prepare the string literal for the name of
3229 -- the elaboration routine to be referenced.
3231 Start_String;
3232 Make_Elab_String (Pref);
3233 Store_String_Chars ("___elab");
3234 Lang := Make_Identifier (Loc, Name_C);
3236 if Id = Attribute_Elab_Body then
3237 Store_String_Char ('b');
3238 else
3239 Store_String_Char ('s');
3240 end if;
3242 Str := End_String;
3244 Insert_Actions (N, New_List (
3245 Make_Subprogram_Declaration (Loc,
3246 Specification =>
3247 Make_Procedure_Specification (Loc,
3248 Defining_Unit_Name => Ent)),
3250 Make_Pragma (Loc,
3251 Chars => Name_Import,
3252 Pragma_Argument_Associations => New_List (
3253 Make_Pragma_Argument_Association (Loc, Expression => Lang),
3255 Make_Pragma_Argument_Association (Loc,
3256 Expression => Make_Identifier (Loc, Chars (Ent))),
3258 Make_Pragma_Argument_Association (Loc,
3259 Expression => Make_String_Literal (Loc, Str))))));
3261 Set_Entity (N, Ent);
3262 Rewrite (N, New_Occurrence_Of (Ent, Loc));
3263 end Elab_Body;
3265 --------------------
3266 -- Elab_Subp_Body --
3267 --------------------
3269 -- Always ignored. In CodePeer mode, gnat2scil knows how to handle
3270 -- this attribute directly, and if we are not in CodePeer mode it is
3271 -- entirely ignored ???
3273 when Attribute_Elab_Subp_Body =>
3274 return;
3276 ----------------
3277 -- Elaborated --
3278 ----------------
3280 -- Elaborated is always True for preelaborated units, predefined units,
3281 -- pure units and units which have Elaborate_Body pragmas. These units
3282 -- have no elaboration entity.
3284 -- Note: The Elaborated attribute is never passed to the back end
3286 when Attribute_Elaborated => Elaborated : declare
3287 Elab_Id : constant Entity_Id := Elaboration_Entity (Entity (Pref));
3289 begin
3290 if Present (Elab_Id) then
3291 Rewrite (N,
3292 Make_Op_Ne (Loc,
3293 Left_Opnd => New_Occurrence_Of (Elab_Id, Loc),
3294 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)));
3296 Analyze_And_Resolve (N, Typ);
3297 else
3298 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3299 end if;
3300 end Elaborated;
3302 --------------
3303 -- Enum_Rep --
3304 --------------
3306 when Attribute_Enum_Rep => Enum_Rep : declare
3307 Expr : Node_Id;
3309 begin
3310 -- Get the expression, which is X for Enum_Type'Enum_Rep (X) or
3311 -- X'Enum_Rep.
3313 if Is_Non_Empty_List (Exprs) then
3314 Expr := First (Exprs);
3315 else
3316 Expr := Pref;
3317 end if;
3319 -- If the expression is an enumeration literal, it is replaced by the
3320 -- literal value.
3322 if Nkind (Expr) in N_Has_Entity
3323 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
3324 then
3325 Rewrite (N,
3326 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Expr))));
3328 -- If this is a renaming of a literal, recover the representation
3329 -- of the original. If it renames an expression there is nothing to
3330 -- fold.
3332 elsif Nkind (Expr) in N_Has_Entity
3333 and then Ekind (Entity (Expr)) = E_Constant
3334 and then Present (Renamed_Object (Entity (Expr)))
3335 and then Is_Entity_Name (Renamed_Object (Entity (Expr)))
3336 and then Ekind (Entity (Renamed_Object (Entity (Expr)))) =
3337 E_Enumeration_Literal
3338 then
3339 Rewrite (N,
3340 Make_Integer_Literal (Loc,
3341 Enumeration_Rep (Entity (Renamed_Object (Entity (Expr))))));
3343 -- If not constant-folded above, Enum_Type'Enum_Rep (X) or
3344 -- X'Enum_Rep expands to
3346 -- target-type (X)
3348 -- This is simply a direct conversion from the enumeration type to
3349 -- the target integer type, which is treated by the back end as a
3350 -- normal integer conversion, treating the enumeration type as an
3351 -- integer, which is exactly what we want. We set Conversion_OK to
3352 -- make sure that the analyzer does not complain about what otherwise
3353 -- might be an illegal conversion.
3355 else
3356 Rewrite (N, OK_Convert_To (Typ, Relocate_Node (Expr)));
3357 end if;
3359 Set_Etype (N, Typ);
3360 Analyze_And_Resolve (N, Typ);
3361 end Enum_Rep;
3363 --------------
3364 -- Enum_Val --
3365 --------------
3367 when Attribute_Enum_Val => Enum_Val : declare
3368 Expr : Node_Id;
3369 Btyp : constant Entity_Id := Base_Type (Ptyp);
3371 begin
3372 -- X'Enum_Val (Y) expands to
3374 -- [constraint_error when _rep_to_pos (Y, False) = -1, msg]
3375 -- X!(Y);
3377 Expr := Unchecked_Convert_To (Ptyp, First (Exprs));
3379 Insert_Action (N,
3380 Make_Raise_Constraint_Error (Loc,
3381 Condition =>
3382 Make_Op_Eq (Loc,
3383 Left_Opnd =>
3384 Make_Function_Call (Loc,
3385 Name =>
3386 New_Occurrence_Of (TSS (Btyp, TSS_Rep_To_Pos), Loc),
3387 Parameter_Associations => New_List (
3388 Relocate_Node (Duplicate_Subexpr (Expr)),
3389 New_Occurrence_Of (Standard_False, Loc))),
3391 Right_Opnd => Make_Integer_Literal (Loc, -1)),
3392 Reason => CE_Range_Check_Failed));
3394 Rewrite (N, Expr);
3395 Analyze_And_Resolve (N, Ptyp);
3396 end Enum_Val;
3398 --------------
3399 -- Exponent --
3400 --------------
3402 -- Transforms 'Exponent into a call to the floating-point attribute
3403 -- function Exponent in Fat_xxx (where xxx is the root type)
3405 when Attribute_Exponent =>
3406 Expand_Fpt_Attribute_R (N);
3408 ------------------
3409 -- External_Tag --
3410 ------------------
3412 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
3414 when Attribute_External_Tag =>
3415 Rewrite (N,
3416 Make_Function_Call (Loc,
3417 Name =>
3418 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
3419 Parameter_Associations => New_List (
3420 Make_Attribute_Reference (Loc,
3421 Attribute_Name => Name_Tag,
3422 Prefix => Prefix (N)))));
3424 Analyze_And_Resolve (N, Standard_String);
3426 -----------------------
3427 -- Finalization_Size --
3428 -----------------------
3430 when Attribute_Finalization_Size => Finalization_Size : declare
3431 function Calculate_Header_Size return Node_Id;
3432 -- Generate a runtime call to calculate the size of the hidden header
3433 -- along with any added padding which would precede a heap-allocated
3434 -- object of the prefix type.
3436 ---------------------------
3437 -- Calculate_Header_Size --
3438 ---------------------------
3440 function Calculate_Header_Size return Node_Id is
3441 begin
3442 -- Generate:
3443 -- Universal_Integer
3444 -- (Header_Size_With_Padding (Pref'Alignment))
3446 return
3447 Convert_To (Universal_Integer,
3448 Make_Function_Call (Loc,
3449 Name =>
3450 New_Occurrence_Of (RTE (RE_Header_Size_With_Padding), Loc),
3452 Parameter_Associations => New_List (
3453 Make_Attribute_Reference (Loc,
3454 Prefix => New_Copy_Tree (Pref),
3455 Attribute_Name => Name_Alignment))));
3456 end Calculate_Header_Size;
3458 -- Local variables
3460 Size : Entity_Id;
3462 -- Start of Finalization_Size
3464 begin
3465 -- An object of a class-wide type first requires a runtime check to
3466 -- determine whether it is actually controlled or not. Depending on
3467 -- the outcome of this check, the Finalization_Size of the object
3468 -- may be zero or some positive value.
3470 -- In this scenario, Pref'Finalization_Size is expanded into
3472 -- Size : Integer := 0;
3474 -- if Needs_Finalization (Pref'Tag) then
3475 -- Size :=
3476 -- Universal_Integer
3477 -- (Header_Size_With_Padding (Pref'Alignment));
3478 -- end if;
3480 -- and the attribute reference is replaced with a reference to Size.
3482 if Is_Class_Wide_Type (Ptyp) then
3483 Size := Make_Temporary (Loc, 'S');
3485 Insert_Actions (N, New_List (
3487 -- Generate:
3488 -- Size : Integer := 0;
3490 Make_Object_Declaration (Loc,
3491 Defining_Identifier => Size,
3492 Object_Definition =>
3493 New_Occurrence_Of (Standard_Integer, Loc),
3494 Expression => Make_Integer_Literal (Loc, 0)),
3496 -- Generate:
3497 -- if Needs_Finalization (Pref'Tag) then
3498 -- Size :=
3499 -- Universal_Integer
3500 -- (Header_Size_With_Padding (Pref'Alignment));
3501 -- end if;
3503 Make_If_Statement (Loc,
3504 Condition =>
3505 Make_Function_Call (Loc,
3506 Name =>
3507 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
3509 Parameter_Associations => New_List (
3510 Make_Attribute_Reference (Loc,
3511 Prefix => New_Copy_Tree (Pref),
3512 Attribute_Name => Name_Tag))),
3514 Then_Statements => New_List (
3515 Make_Assignment_Statement (Loc,
3516 Name => New_Occurrence_Of (Size, Loc),
3517 Expression => Calculate_Header_Size)))));
3519 Rewrite (N, New_Occurrence_Of (Size, Loc));
3521 -- The prefix is known to be controlled at compile time. Calculate
3522 -- Finalization_Size by calling function Header_Size_With_Padding.
3524 elsif Needs_Finalization (Ptyp) then
3525 Rewrite (N, Calculate_Header_Size);
3527 -- The prefix is not an object with controlled parts, so its
3528 -- Finalization_Size is zero.
3530 else
3531 Rewrite (N, Make_Integer_Literal (Loc, 0));
3532 end if;
3534 -- Due to cases where the entity type of the attribute is already
3535 -- resolved the rewritten N must get re-resolved to its appropriate
3536 -- type.
3538 Analyze_And_Resolve (N, Typ);
3539 end Finalization_Size;
3541 -----------
3542 -- First --
3543 -----------
3545 when Attribute_First =>
3547 -- If the prefix type is a constrained packed array type which
3548 -- already has a Packed_Array_Impl_Type representation defined, then
3549 -- replace this attribute with a direct reference to 'First of the
3550 -- appropriate index subtype (since otherwise the back end will try
3551 -- to give us the value of 'First for this implementation type).
3553 if Is_Constrained_Packed_Array (Ptyp) then
3554 Rewrite (N,
3555 Make_Attribute_Reference (Loc,
3556 Attribute_Name => Name_First,
3557 Prefix =>
3558 New_Occurrence_Of (Get_Index_Subtype (N), Loc)));
3559 Analyze_And_Resolve (N, Typ);
3561 -- For access type, apply access check as needed
3563 elsif Is_Access_Type (Ptyp) then
3564 Apply_Access_Check (N);
3566 -- For scalar type, if low bound is a reference to an entity, just
3567 -- replace with a direct reference. Note that we can only have a
3568 -- reference to a constant entity at this stage, anything else would
3569 -- have already been rewritten.
3571 elsif Is_Scalar_Type (Ptyp) then
3572 declare
3573 Lo : constant Node_Id := Type_Low_Bound (Ptyp);
3574 begin
3575 if Is_Entity_Name (Lo) then
3576 Rewrite (N, New_Occurrence_Of (Entity (Lo), Loc));
3577 end if;
3578 end;
3579 end if;
3581 ---------------
3582 -- First_Bit --
3583 ---------------
3585 -- Compute this if component clause was present, otherwise we leave the
3586 -- computation to be completed in the back-end, since we don't know what
3587 -- layout will be chosen.
3589 when Attribute_First_Bit => First_Bit_Attr : declare
3590 CE : constant Entity_Id := Entity (Selector_Name (Pref));
3592 begin
3593 -- In Ada 2005 (or later) if we have the non-default bit order, then
3594 -- we return the original value as given in the component clause
3595 -- (RM 2005 13.5.2(3/2)).
3597 if Present (Component_Clause (CE))
3598 and then Ada_Version >= Ada_2005
3599 and then Reverse_Bit_Order (Scope (CE))
3600 then
3601 Rewrite (N,
3602 Make_Integer_Literal (Loc,
3603 Intval => Expr_Value (First_Bit (Component_Clause (CE)))));
3604 Analyze_And_Resolve (N, Typ);
3606 -- Otherwise (Ada 83/95 or Ada 2005 or later with default bit order),
3607 -- rewrite with normalized value if we know it statically.
3609 elsif Known_Static_Component_Bit_Offset (CE) then
3610 Rewrite (N,
3611 Make_Integer_Literal (Loc,
3612 Component_Bit_Offset (CE) mod System_Storage_Unit));
3613 Analyze_And_Resolve (N, Typ);
3615 -- Otherwise left to back end, just do universal integer checks
3617 else
3618 Apply_Universal_Integer_Attribute_Checks (N);
3619 end if;
3620 end First_Bit_Attr;
3622 --------------------------------
3623 -- Fixed_Value, Integer_Value --
3624 --------------------------------
3626 -- We transform
3628 -- fixtype'Fixed_Value (integer-value)
3629 -- inttype'Fixed_Value (fixed-value)
3631 -- into
3633 -- fixtype (integer-value)
3634 -- inttype (fixed-value)
3636 -- respectively.
3638 -- We do all the required analysis of the conversion here, because we do
3639 -- not want this to go through the fixed-point conversion circuits. Note
3640 -- that the back end always treats fixed-point as equivalent to the
3641 -- corresponding integer type anyway.
3643 when Attribute_Fixed_Value
3644 | Attribute_Integer_Value
3646 Rewrite (N,
3647 Make_Type_Conversion (Loc,
3648 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
3649 Expression => Relocate_Node (First (Exprs))));
3650 Set_Etype (N, Entity (Pref));
3651 Set_Analyzed (N);
3653 -- Note: it might appear that a properly analyzed unchecked
3654 -- conversion would be just fine here, but that's not the case,
3655 -- since the full range checks performed by the following call
3656 -- are critical.
3658 Apply_Type_Conversion_Checks (N);
3660 -----------
3661 -- Floor --
3662 -----------
3664 -- Transforms 'Floor into a call to the floating-point attribute
3665 -- function Floor in Fat_xxx (where xxx is the root type)
3667 when Attribute_Floor =>
3668 Expand_Fpt_Attribute_R (N);
3670 ----------
3671 -- Fore --
3672 ----------
3674 -- For the fixed-point type Typ:
3676 -- Typ'Fore
3678 -- expands into
3680 -- Result_Type (System.Fore (Universal_Real (Type'First)),
3681 -- Universal_Real (Type'Last))
3683 -- Note that we know that the type is a non-static subtype, or Fore
3684 -- would have itself been computed dynamically in Eval_Attribute.
3686 when Attribute_Fore =>
3687 Rewrite (N,
3688 Convert_To (Typ,
3689 Make_Function_Call (Loc,
3690 Name =>
3691 New_Occurrence_Of (RTE (RE_Fore), Loc),
3693 Parameter_Associations => New_List (
3694 Convert_To (Universal_Real,
3695 Make_Attribute_Reference (Loc,
3696 Prefix => New_Occurrence_Of (Ptyp, Loc),
3697 Attribute_Name => Name_First)),
3699 Convert_To (Universal_Real,
3700 Make_Attribute_Reference (Loc,
3701 Prefix => New_Occurrence_Of (Ptyp, Loc),
3702 Attribute_Name => Name_Last))))));
3704 Analyze_And_Resolve (N, Typ);
3706 --------------
3707 -- Fraction --
3708 --------------
3710 -- Transforms 'Fraction into a call to the floating-point attribute
3711 -- function Fraction in Fat_xxx (where xxx is the root type)
3713 when Attribute_Fraction =>
3714 Expand_Fpt_Attribute_R (N);
3716 --------------
3717 -- From_Any --
3718 --------------
3720 when Attribute_From_Any => From_Any : declare
3721 P_Type : constant Entity_Id := Etype (Pref);
3722 Decls : constant List_Id := New_List;
3724 begin
3725 Rewrite (N,
3726 Build_From_Any_Call (P_Type,
3727 Relocate_Node (First (Exprs)),
3728 Decls));
3729 Insert_Actions (N, Decls);
3730 Analyze_And_Resolve (N, P_Type);
3731 end From_Any;
3733 ----------------------
3734 -- Has_Same_Storage --
3735 ----------------------
3737 when Attribute_Has_Same_Storage => Has_Same_Storage : declare
3738 Loc : constant Source_Ptr := Sloc (N);
3740 X : constant Node_Id := Prefix (N);
3741 Y : constant Node_Id := First (Expressions (N));
3742 -- The arguments
3744 X_Addr : Node_Id;
3745 Y_Addr : Node_Id;
3746 -- Rhe expressions for their addresses
3748 X_Size : Node_Id;
3749 Y_Size : Node_Id;
3750 -- Rhe expressions for their sizes
3752 begin
3753 -- The attribute is expanded as:
3755 -- (X'address = Y'address)
3756 -- and then (X'Size = Y'Size)
3758 -- If both arguments have the same Etype the second conjunct can be
3759 -- omitted.
3761 X_Addr :=
3762 Make_Attribute_Reference (Loc,
3763 Attribute_Name => Name_Address,
3764 Prefix => New_Copy_Tree (X));
3766 Y_Addr :=
3767 Make_Attribute_Reference (Loc,
3768 Attribute_Name => Name_Address,
3769 Prefix => New_Copy_Tree (Y));
3771 X_Size :=
3772 Make_Attribute_Reference (Loc,
3773 Attribute_Name => Name_Size,
3774 Prefix => New_Copy_Tree (X));
3776 Y_Size :=
3777 Make_Attribute_Reference (Loc,
3778 Attribute_Name => Name_Size,
3779 Prefix => New_Copy_Tree (Y));
3781 if Etype (X) = Etype (Y) then
3782 Rewrite (N,
3783 Make_Op_Eq (Loc,
3784 Left_Opnd => X_Addr,
3785 Right_Opnd => Y_Addr));
3786 else
3787 Rewrite (N,
3788 Make_Op_And (Loc,
3789 Left_Opnd =>
3790 Make_Op_Eq (Loc,
3791 Left_Opnd => X_Addr,
3792 Right_Opnd => Y_Addr),
3793 Right_Opnd =>
3794 Make_Op_Eq (Loc,
3795 Left_Opnd => X_Size,
3796 Right_Opnd => Y_Size)));
3797 end if;
3799 Analyze_And_Resolve (N, Standard_Boolean);
3800 end Has_Same_Storage;
3802 --------------
3803 -- Identity --
3804 --------------
3806 -- For an exception returns a reference to the exception data:
3807 -- Exception_Id!(Prefix'Reference)
3809 -- For a task it returns a reference to the _task_id component of
3810 -- corresponding record:
3812 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
3814 -- in Ada.Task_Identification
3816 when Attribute_Identity => Identity : declare
3817 Id_Kind : Entity_Id;
3819 begin
3820 if Ptyp = Standard_Exception_Type then
3821 Id_Kind := RTE (RE_Exception_Id);
3823 if Present (Renamed_Object (Entity (Pref))) then
3824 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
3825 end if;
3827 Rewrite (N,
3828 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
3829 else
3830 Id_Kind := RTE (RO_AT_Task_Id);
3832 -- If the prefix is a task interface, the Task_Id is obtained
3833 -- dynamically through a dispatching call, as for other task
3834 -- attributes applied to interfaces.
3836 if Ada_Version >= Ada_2005
3837 and then Ekind (Ptyp) = E_Class_Wide_Type
3838 and then Is_Interface (Ptyp)
3839 and then Is_Task_Interface (Ptyp)
3840 then
3841 Rewrite (N,
3842 Unchecked_Convert_To
3843 (Id_Kind, Build_Disp_Get_Task_Id_Call (Pref)));
3845 else
3846 Rewrite (N,
3847 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
3848 end if;
3849 end if;
3851 Analyze_And_Resolve (N, Id_Kind);
3852 end Identity;
3854 -----------
3855 -- Image --
3856 -----------
3858 -- Image attribute is handled in separate unit Exp_Imgv
3860 when Attribute_Image =>
3862 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
3863 -- back-end knows how to handle this attribute directly.
3865 if CodePeer_Mode then
3866 return;
3867 end if;
3869 Expand_Image_Attribute (N);
3871 ---------
3872 -- Img --
3873 ---------
3875 -- X'Img is expanded to typ'Image (X), where typ is the type of X
3877 when Attribute_Img =>
3878 Expand_Image_Attribute (N);
3880 -----------
3881 -- Input --
3882 -----------
3884 when Attribute_Input => Input : declare
3885 P_Type : constant Entity_Id := Entity (Pref);
3886 B_Type : constant Entity_Id := Base_Type (P_Type);
3887 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3888 Strm : constant Node_Id := First (Exprs);
3889 Fname : Entity_Id;
3890 Decl : Node_Id;
3891 Call : Node_Id;
3892 Prag : Node_Id;
3893 Arg2 : Node_Id;
3894 Rfunc : Node_Id;
3896 Cntrl : Node_Id := Empty;
3897 -- Value for controlling argument in call. Always Empty except in
3898 -- the dispatching (class-wide type) case, where it is a reference
3899 -- to the dummy object initialized to the right internal tag.
3901 procedure Freeze_Stream_Subprogram (F : Entity_Id);
3902 -- The expansion of the attribute reference may generate a call to
3903 -- a user-defined stream subprogram that is frozen by the call. This
3904 -- can lead to access-before-elaboration problem if the reference
3905 -- appears in an object declaration and the subprogram body has not
3906 -- been seen. The freezing of the subprogram requires special code
3907 -- because it appears in an expanded context where expressions do
3908 -- not freeze their constituents.
3910 ------------------------------
3911 -- Freeze_Stream_Subprogram --
3912 ------------------------------
3914 procedure Freeze_Stream_Subprogram (F : Entity_Id) is
3915 Decl : constant Node_Id := Unit_Declaration_Node (F);
3916 Bod : Node_Id;
3918 begin
3919 -- If this is user-defined subprogram, the corresponding
3920 -- stream function appears as a renaming-as-body, and the
3921 -- user subprogram must be retrieved by tree traversal.
3923 if Present (Decl)
3924 and then Nkind (Decl) = N_Subprogram_Declaration
3925 and then Present (Corresponding_Body (Decl))
3926 then
3927 Bod := Corresponding_Body (Decl);
3929 if Nkind (Unit_Declaration_Node (Bod)) =
3930 N_Subprogram_Renaming_Declaration
3931 then
3932 Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
3933 end if;
3934 end if;
3935 end Freeze_Stream_Subprogram;
3937 -- Start of processing for Input
3939 begin
3940 -- If no underlying type, we have an error that will be diagnosed
3941 -- elsewhere, so here we just completely ignore the expansion.
3943 if No (U_Type) then
3944 return;
3945 end if;
3947 -- Stream operations can appear in user code even if the restriction
3948 -- No_Streams is active (for example, when instantiating a predefined
3949 -- container). In that case rewrite the attribute as a Raise to
3950 -- prevent any run-time use.
3952 if Restriction_Active (No_Streams) then
3953 Rewrite (N,
3954 Make_Raise_Program_Error (Sloc (N),
3955 Reason => PE_Stream_Operation_Not_Allowed));
3956 Set_Etype (N, B_Type);
3957 return;
3958 end if;
3960 -- If there is a TSS for Input, just call it
3962 Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
3964 if Present (Fname) then
3965 null;
3967 else
3968 -- If there is a Stream_Convert pragma, use it, we rewrite
3970 -- sourcetyp'Input (stream)
3972 -- as
3974 -- sourcetyp (streamread (strmtyp'Input (stream)));
3976 -- where streamread is the given Read function that converts an
3977 -- argument of type strmtyp to type sourcetyp or a type from which
3978 -- it is derived (extra conversion required for the derived case).
3980 Prag := Get_Stream_Convert_Pragma (P_Type);
3982 if Present (Prag) then
3983 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
3984 Rfunc := Entity (Expression (Arg2));
3986 Rewrite (N,
3987 Convert_To (B_Type,
3988 Make_Function_Call (Loc,
3989 Name => New_Occurrence_Of (Rfunc, Loc),
3990 Parameter_Associations => New_List (
3991 Make_Attribute_Reference (Loc,
3992 Prefix =>
3993 New_Occurrence_Of
3994 (Etype (First_Formal (Rfunc)), Loc),
3995 Attribute_Name => Name_Input,
3996 Expressions => Exprs)))));
3998 Analyze_And_Resolve (N, B_Type);
3999 return;
4001 -- Elementary types
4003 elsif Is_Elementary_Type (U_Type) then
4005 -- A special case arises if we have a defined _Read routine,
4006 -- since in this case we are required to call this routine.
4008 declare
4009 Typ : Entity_Id := P_Type;
4010 begin
4011 if Present (Full_View (Typ)) then
4012 Typ := Full_View (Typ);
4013 end if;
4015 if Present (TSS (Base_Type (Typ), TSS_Stream_Read)) then
4016 Build_Record_Or_Elementary_Input_Function
4017 (Loc, Typ, Decl, Fname, Use_Underlying => False);
4018 Insert_Action (N, Decl);
4020 -- For normal cases, we call the I_xxx routine directly
4022 else
4023 Rewrite (N, Build_Elementary_Input_Call (N));
4024 Analyze_And_Resolve (N, P_Type);
4025 return;
4026 end if;
4027 end;
4029 -- Array type case
4031 elsif Is_Array_Type (U_Type) then
4032 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
4033 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
4035 -- Dispatching case with class-wide type
4037 elsif Is_Class_Wide_Type (P_Type) then
4039 -- No need to do anything else compiling under restriction
4040 -- No_Dispatching_Calls. During the semantic analysis we
4041 -- already notified such violation.
4043 if Restriction_Active (No_Dispatching_Calls) then
4044 return;
4045 end if;
4047 declare
4048 Rtyp : constant Entity_Id := Root_Type (P_Type);
4049 Expr : Node_Id;
4051 begin
4052 -- Read the internal tag (RM 13.13.2(34)) and use it to
4053 -- initialize a dummy tag value. We used to generate:
4055 -- Descendant_Tag (String'Input (Strm), P_Type);
4057 -- which turns into a call to String_Input_Blk_IO. However,
4058 -- if the input is malformed, that could try to read an
4059 -- enormous String, causing chaos. So instead we call
4060 -- String_Input_Tag, which does the same thing as
4061 -- String_Input_Blk_IO, except that if the String is
4062 -- absurdly long, it raises an exception.
4064 -- This value is used only to provide a controlling
4065 -- argument for the eventual _Input call. Descendant_Tag is
4066 -- called rather than Internal_Tag to ensure that we have a
4067 -- tag for a type that is descended from the prefix type and
4068 -- declared at the same accessibility level (the exception
4069 -- Tag_Error will be raised otherwise). The level check is
4070 -- required for Ada 2005 because tagged types can be
4071 -- extended in nested scopes (AI-344).
4073 -- Note: we used to generate an explicit declaration of a
4074 -- constant Ada.Tags.Tag object, and use an occurrence of
4075 -- this constant in Cntrl, but this caused a secondary stack
4076 -- leak.
4078 Expr :=
4079 Make_Function_Call (Loc,
4080 Name =>
4081 New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc),
4082 Parameter_Associations => New_List (
4083 Make_Function_Call (Loc,
4084 Name =>
4085 New_Occurrence_Of
4086 (RTE (RE_String_Input_Tag), Loc),
4087 Parameter_Associations => New_List (
4088 Relocate_Node (Duplicate_Subexpr (Strm)))),
4090 Make_Attribute_Reference (Loc,
4091 Prefix => New_Occurrence_Of (P_Type, Loc),
4092 Attribute_Name => Name_Tag)));
4094 Set_Etype (Expr, RTE (RE_Tag));
4096 -- Now we need to get the entity for the call, and construct
4097 -- a function call node, where we preset a reference to Dnn
4098 -- as the controlling argument (doing an unchecked convert
4099 -- to the class-wide tagged type to make it look like a real
4100 -- tagged object).
4102 Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
4103 Cntrl := Unchecked_Convert_To (P_Type, Expr);
4104 Set_Etype (Cntrl, P_Type);
4105 Set_Parent (Cntrl, N);
4106 end;
4108 -- For tagged types, use the primitive Input function
4110 elsif Is_Tagged_Type (U_Type) then
4111 Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
4113 -- All other record type cases, including protected records. The
4114 -- latter only arise for expander generated code for handling
4115 -- shared passive partition access.
4117 else
4118 pragma Assert
4119 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
4121 -- Ada 2005 (AI-216): Program_Error is raised executing default
4122 -- implementation of the Input attribute of an unchecked union
4123 -- type if the type lacks default discriminant values.
4125 if Is_Unchecked_Union (Base_Type (U_Type))
4126 and then No (Discriminant_Constraint (U_Type))
4127 then
4128 Insert_Action (N,
4129 Make_Raise_Program_Error (Loc,
4130 Reason => PE_Unchecked_Union_Restriction));
4132 return;
4133 end if;
4135 -- Build the type's Input function, passing the subtype rather
4136 -- than its base type, because checks are needed in the case of
4137 -- constrained discriminants (see Ada 2012 AI05-0192).
4139 Build_Record_Or_Elementary_Input_Function
4140 (Loc, U_Type, Decl, Fname);
4141 Insert_Action (N, Decl);
4143 if Nkind (Parent (N)) = N_Object_Declaration
4144 and then Is_Record_Type (U_Type)
4145 then
4146 -- The stream function may contain calls to user-defined
4147 -- Read procedures for individual components.
4149 declare
4150 Comp : Entity_Id;
4151 Func : Entity_Id;
4153 begin
4154 Comp := First_Component (U_Type);
4155 while Present (Comp) loop
4156 Func :=
4157 Find_Stream_Subprogram
4158 (Etype (Comp), TSS_Stream_Read);
4160 if Present (Func) then
4161 Freeze_Stream_Subprogram (Func);
4162 end if;
4164 Next_Component (Comp);
4165 end loop;
4166 end;
4167 end if;
4168 end if;
4169 end if;
4171 -- If we fall through, Fname is the function to be called. The result
4172 -- is obtained by calling the appropriate function, then converting
4173 -- the result. The conversion does a subtype check.
4175 Call :=
4176 Make_Function_Call (Loc,
4177 Name => New_Occurrence_Of (Fname, Loc),
4178 Parameter_Associations => New_List (
4179 Relocate_Node (Strm)));
4181 Set_Controlling_Argument (Call, Cntrl);
4182 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
4183 Analyze_And_Resolve (N, P_Type);
4185 if Nkind (Parent (N)) = N_Object_Declaration then
4186 Freeze_Stream_Subprogram (Fname);
4187 end if;
4188 end Input;
4190 -------------------
4191 -- Invalid_Value --
4192 -------------------
4194 when Attribute_Invalid_Value =>
4195 Rewrite (N, Get_Simple_Init_Val (Ptyp, N));
4197 ----------
4198 -- Last --
4199 ----------
4201 when Attribute_Last =>
4203 -- If the prefix type is a constrained packed array type which
4204 -- already has a Packed_Array_Impl_Type representation defined, then
4205 -- replace this attribute with a direct reference to 'Last of the
4206 -- appropriate index subtype (since otherwise the back end will try
4207 -- to give us the value of 'Last for this implementation type).
4209 if Is_Constrained_Packed_Array (Ptyp) then
4210 Rewrite (N,
4211 Make_Attribute_Reference (Loc,
4212 Attribute_Name => Name_Last,
4213 Prefix => New_Occurrence_Of (Get_Index_Subtype (N), Loc)));
4214 Analyze_And_Resolve (N, Typ);
4216 -- For access type, apply access check as needed
4218 elsif Is_Access_Type (Ptyp) then
4219 Apply_Access_Check (N);
4221 -- For scalar type, if low bound is a reference to an entity, just
4222 -- replace with a direct reference. Note that we can only have a
4223 -- reference to a constant entity at this stage, anything else would
4224 -- have already been rewritten.
4226 elsif Is_Scalar_Type (Ptyp) then
4227 declare
4228 Hi : constant Node_Id := Type_High_Bound (Ptyp);
4229 begin
4230 if Is_Entity_Name (Hi) then
4231 Rewrite (N, New_Occurrence_Of (Entity (Hi), Loc));
4232 end if;
4233 end;
4234 end if;
4236 --------------
4237 -- Last_Bit --
4238 --------------
4240 -- We compute this if a component clause was present, otherwise we leave
4241 -- the computation up to the back end, since we don't know what layout
4242 -- will be chosen.
4244 when Attribute_Last_Bit => Last_Bit_Attr : declare
4245 CE : constant Entity_Id := Entity (Selector_Name (Pref));
4247 begin
4248 -- In Ada 2005 (or later) if we have the non-default bit order, then
4249 -- we return the original value as given in the component clause
4250 -- (RM 2005 13.5.2(3/2)).
4252 if Present (Component_Clause (CE))
4253 and then Ada_Version >= Ada_2005
4254 and then Reverse_Bit_Order (Scope (CE))
4255 then
4256 Rewrite (N,
4257 Make_Integer_Literal (Loc,
4258 Intval => Expr_Value (Last_Bit (Component_Clause (CE)))));
4259 Analyze_And_Resolve (N, Typ);
4261 -- Otherwise (Ada 83/95 or Ada 2005 or later with default bit order),
4262 -- rewrite with normalized value if we know it statically.
4264 elsif Known_Static_Component_Bit_Offset (CE)
4265 and then Known_Static_Esize (CE)
4266 then
4267 Rewrite (N,
4268 Make_Integer_Literal (Loc,
4269 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
4270 + Esize (CE) - 1));
4271 Analyze_And_Resolve (N, Typ);
4273 -- Otherwise leave to back end, just apply universal integer checks
4275 else
4276 Apply_Universal_Integer_Attribute_Checks (N);
4277 end if;
4278 end Last_Bit_Attr;
4280 ------------------
4281 -- Leading_Part --
4282 ------------------
4284 -- Transforms 'Leading_Part into a call to the floating-point attribute
4285 -- function Leading_Part in Fat_xxx (where xxx is the root type)
4287 -- Note: strictly, we should generate special case code to deal with
4288 -- absurdly large positive arguments (greater than Integer'Last), which
4289 -- result in returning the first argument unchanged, but it hardly seems
4290 -- worth the effort. We raise constraint error for absurdly negative
4291 -- arguments which is fine.
4293 when Attribute_Leading_Part =>
4294 Expand_Fpt_Attribute_RI (N);
4296 ------------
4297 -- Length --
4298 ------------
4300 when Attribute_Length => Length : declare
4301 Ityp : Entity_Id;
4302 Xnum : Uint;
4304 begin
4305 -- Processing for packed array types
4307 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
4308 Ityp := Get_Index_Subtype (N);
4310 -- If the index type, Ityp, is an enumeration type with holes,
4311 -- then we calculate X'Length explicitly using
4313 -- Typ'Max
4314 -- (0, Ityp'Pos (X'Last (N)) -
4315 -- Ityp'Pos (X'First (N)) + 1);
4317 -- Since the bounds in the template are the representation values
4318 -- and the back end would get the wrong value.
4320 if Is_Enumeration_Type (Ityp)
4321 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
4322 then
4323 if No (Exprs) then
4324 Xnum := Uint_1;
4325 else
4326 Xnum := Expr_Value (First (Expressions (N)));
4327 end if;
4329 Rewrite (N,
4330 Make_Attribute_Reference (Loc,
4331 Prefix => New_Occurrence_Of (Typ, Loc),
4332 Attribute_Name => Name_Max,
4333 Expressions => New_List
4334 (Make_Integer_Literal (Loc, 0),
4336 Make_Op_Add (Loc,
4337 Left_Opnd =>
4338 Make_Op_Subtract (Loc,
4339 Left_Opnd =>
4340 Make_Attribute_Reference (Loc,
4341 Prefix => New_Occurrence_Of (Ityp, Loc),
4342 Attribute_Name => Name_Pos,
4344 Expressions => New_List (
4345 Make_Attribute_Reference (Loc,
4346 Prefix => Duplicate_Subexpr (Pref),
4347 Attribute_Name => Name_Last,
4348 Expressions => New_List (
4349 Make_Integer_Literal (Loc, Xnum))))),
4351 Right_Opnd =>
4352 Make_Attribute_Reference (Loc,
4353 Prefix => New_Occurrence_Of (Ityp, Loc),
4354 Attribute_Name => Name_Pos,
4356 Expressions => New_List (
4357 Make_Attribute_Reference (Loc,
4358 Prefix =>
4359 Duplicate_Subexpr_No_Checks (Pref),
4360 Attribute_Name => Name_First,
4361 Expressions => New_List (
4362 Make_Integer_Literal (Loc, Xnum)))))),
4364 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
4366 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
4367 return;
4369 -- If the prefix type is a constrained packed array type which
4370 -- already has a Packed_Array_Impl_Type representation defined,
4371 -- then replace this attribute with a reference to 'Range_Length
4372 -- of the appropriate index subtype (since otherwise the
4373 -- back end will try to give us the value of 'Length for
4374 -- this implementation type).s
4376 elsif Is_Constrained (Ptyp) then
4377 Rewrite (N,
4378 Make_Attribute_Reference (Loc,
4379 Attribute_Name => Name_Range_Length,
4380 Prefix => New_Occurrence_Of (Ityp, Loc)));
4381 Analyze_And_Resolve (N, Typ);
4382 end if;
4384 -- Access type case
4386 elsif Is_Access_Type (Ptyp) then
4387 Apply_Access_Check (N);
4389 -- If the designated type is a packed array type, then we convert
4390 -- the reference to:
4392 -- typ'Max (0, 1 +
4393 -- xtyp'Pos (Pref'Last (Expr)) -
4394 -- xtyp'Pos (Pref'First (Expr)));
4396 -- This is a bit complex, but it is the easiest thing to do that
4397 -- works in all cases including enum types with holes xtyp here
4398 -- is the appropriate index type.
4400 declare
4401 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
4402 Xtyp : Entity_Id;
4404 begin
4405 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
4406 Xtyp := Get_Index_Subtype (N);
4408 Rewrite (N,
4409 Make_Attribute_Reference (Loc,
4410 Prefix => New_Occurrence_Of (Typ, Loc),
4411 Attribute_Name => Name_Max,
4412 Expressions => New_List (
4413 Make_Integer_Literal (Loc, 0),
4415 Make_Op_Add (Loc,
4416 Make_Integer_Literal (Loc, 1),
4417 Make_Op_Subtract (Loc,
4418 Left_Opnd =>
4419 Make_Attribute_Reference (Loc,
4420 Prefix => New_Occurrence_Of (Xtyp, Loc),
4421 Attribute_Name => Name_Pos,
4422 Expressions => New_List (
4423 Make_Attribute_Reference (Loc,
4424 Prefix => Duplicate_Subexpr (Pref),
4425 Attribute_Name => Name_Last,
4426 Expressions =>
4427 New_Copy_List (Exprs)))),
4429 Right_Opnd =>
4430 Make_Attribute_Reference (Loc,
4431 Prefix => New_Occurrence_Of (Xtyp, Loc),
4432 Attribute_Name => Name_Pos,
4433 Expressions => New_List (
4434 Make_Attribute_Reference (Loc,
4435 Prefix =>
4436 Duplicate_Subexpr_No_Checks (Pref),
4437 Attribute_Name => Name_First,
4438 Expressions =>
4439 New_Copy_List (Exprs)))))))));
4441 Analyze_And_Resolve (N, Typ);
4442 end if;
4443 end;
4445 -- Otherwise leave it to the back end
4447 else
4448 Apply_Universal_Integer_Attribute_Checks (N);
4449 end if;
4450 end Length;
4452 -- Attribute Loop_Entry is replaced with a reference to a constant value
4453 -- which captures the prefix at the entry point of the related loop. The
4454 -- loop itself may be transformed into a conditional block.
4456 when Attribute_Loop_Entry =>
4457 Expand_Loop_Entry_Attribute (N);
4459 -------------
4460 -- Machine --
4461 -------------
4463 -- Transforms 'Machine into a call to the floating-point attribute
4464 -- function Machine in Fat_xxx (where xxx is the root type).
4465 -- Expansion is avoided for cases the back end can handle directly.
4467 when Attribute_Machine =>
4468 if not Is_Inline_Floating_Point_Attribute (N) then
4469 Expand_Fpt_Attribute_R (N);
4470 end if;
4472 ----------------------
4473 -- Machine_Rounding --
4474 ----------------------
4476 -- Transforms 'Machine_Rounding into a call to the floating-point
4477 -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root
4478 -- type). Expansion is avoided for cases the back end can handle
4479 -- directly.
4481 when Attribute_Machine_Rounding =>
4482 if not Is_Inline_Floating_Point_Attribute (N) then
4483 Expand_Fpt_Attribute_R (N);
4484 end if;
4486 ------------------
4487 -- Machine_Size --
4488 ------------------
4490 -- Machine_Size is equivalent to Object_Size, so transform it into
4491 -- Object_Size and that way the back end never sees Machine_Size.
4493 when Attribute_Machine_Size =>
4494 Rewrite (N,
4495 Make_Attribute_Reference (Loc,
4496 Prefix => Prefix (N),
4497 Attribute_Name => Name_Object_Size));
4499 Analyze_And_Resolve (N, Typ);
4501 --------------
4502 -- Mantissa --
4503 --------------
4505 -- The only case that can get this far is the dynamic case of the old
4506 -- Ada 83 Mantissa attribute for the fixed-point case. For this case,
4507 -- we expand:
4509 -- typ'Mantissa
4511 -- into
4513 -- ityp (System.Mantissa.Mantissa_Value
4514 -- (Integer'Integer_Value (typ'First),
4515 -- Integer'Integer_Value (typ'Last)));
4517 when Attribute_Mantissa =>
4518 Rewrite (N,
4519 Convert_To (Typ,
4520 Make_Function_Call (Loc,
4521 Name =>
4522 New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
4524 Parameter_Associations => New_List (
4525 Make_Attribute_Reference (Loc,
4526 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
4527 Attribute_Name => Name_Integer_Value,
4528 Expressions => New_List (
4529 Make_Attribute_Reference (Loc,
4530 Prefix => New_Occurrence_Of (Ptyp, Loc),
4531 Attribute_Name => Name_First))),
4533 Make_Attribute_Reference (Loc,
4534 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
4535 Attribute_Name => Name_Integer_Value,
4536 Expressions => New_List (
4537 Make_Attribute_Reference (Loc,
4538 Prefix => New_Occurrence_Of (Ptyp, Loc),
4539 Attribute_Name => Name_Last)))))));
4541 Analyze_And_Resolve (N, Typ);
4543 ---------
4544 -- Max --
4545 ---------
4547 when Attribute_Max =>
4548 Expand_Min_Max_Attribute (N);
4550 ----------------------------------
4551 -- Max_Size_In_Storage_Elements --
4552 ----------------------------------
4554 when Attribute_Max_Size_In_Storage_Elements => declare
4555 Typ : constant Entity_Id := Etype (N);
4556 Attr : Node_Id;
4558 Conversion_Added : Boolean := False;
4559 -- A flag which tracks whether the original attribute has been
4560 -- wrapped inside a type conversion.
4562 begin
4563 -- If the prefix is X'Class, we transform it into a direct reference
4564 -- to the class-wide type, because the back end must not see a 'Class
4565 -- reference. See also 'Size.
4567 if Is_Entity_Name (Pref)
4568 and then Is_Class_Wide_Type (Entity (Pref))
4569 then
4570 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
4571 return;
4572 end if;
4574 Apply_Universal_Integer_Attribute_Checks (N);
4576 -- The universal integer check may sometimes add a type conversion,
4577 -- retrieve the original attribute reference from the expression.
4579 Attr := N;
4581 if Nkind (Attr) = N_Type_Conversion then
4582 Attr := Expression (Attr);
4583 Conversion_Added := True;
4584 end if;
4586 pragma Assert (Nkind (Attr) = N_Attribute_Reference);
4588 -- Heap-allocated controlled objects contain two extra pointers which
4589 -- are not part of the actual type. Transform the attribute reference
4590 -- into a runtime expression to add the size of the hidden header.
4592 if Needs_Finalization (Ptyp)
4593 and then not Header_Size_Added (Attr)
4594 then
4595 Set_Header_Size_Added (Attr);
4597 -- Generate:
4598 -- P'Max_Size_In_Storage_Elements +
4599 -- Universal_Integer
4600 -- (Header_Size_With_Padding (Ptyp'Alignment))
4602 Rewrite (Attr,
4603 Make_Op_Add (Loc,
4604 Left_Opnd => Relocate_Node (Attr),
4605 Right_Opnd =>
4606 Convert_To (Universal_Integer,
4607 Make_Function_Call (Loc,
4608 Name =>
4609 New_Occurrence_Of
4610 (RTE (RE_Header_Size_With_Padding), Loc),
4612 Parameter_Associations => New_List (
4613 Make_Attribute_Reference (Loc,
4614 Prefix =>
4615 New_Occurrence_Of (Ptyp, Loc),
4616 Attribute_Name => Name_Alignment))))));
4618 -- Add a conversion to the target type
4620 if not Conversion_Added then
4621 Rewrite (Attr,
4622 Make_Type_Conversion (Loc,
4623 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4624 Expression => Relocate_Node (Attr)));
4625 end if;
4627 Analyze (Attr);
4628 return;
4629 end if;
4630 end;
4632 --------------------
4633 -- Mechanism_Code --
4634 --------------------
4636 when Attribute_Mechanism_Code =>
4638 -- We must replace the prefix in the renamed case
4640 if Is_Entity_Name (Pref)
4641 and then Present (Alias (Entity (Pref)))
4642 then
4643 Set_Renamed_Subprogram (Pref, Alias (Entity (Pref)));
4644 end if;
4646 ---------
4647 -- Min --
4648 ---------
4650 when Attribute_Min =>
4651 Expand_Min_Max_Attribute (N);
4653 ---------
4654 -- Mod --
4655 ---------
4657 when Attribute_Mod => Mod_Case : declare
4658 Arg : constant Node_Id := Relocate_Node (First (Exprs));
4659 Hi : constant Node_Id := Type_High_Bound (Etype (Arg));
4660 Modv : constant Uint := Modulus (Btyp);
4662 begin
4664 -- This is not so simple. The issue is what type to use for the
4665 -- computation of the modular value.
4667 -- The easy case is when the modulus value is within the bounds
4668 -- of the signed integer type of the argument. In this case we can
4669 -- just do the computation in that signed integer type, and then
4670 -- do an ordinary conversion to the target type.
4672 if Modv <= Expr_Value (Hi) then
4673 Rewrite (N,
4674 Convert_To (Btyp,
4675 Make_Op_Mod (Loc,
4676 Left_Opnd => Arg,
4677 Right_Opnd => Make_Integer_Literal (Loc, Modv))));
4679 -- Here we know that the modulus is larger than type'Last of the
4680 -- integer type. There are two cases to consider:
4682 -- a) The integer value is non-negative. In this case, it is
4683 -- returned as the result (since it is less than the modulus).
4685 -- b) The integer value is negative. In this case, we know that the
4686 -- result is modulus + value, where the value might be as small as
4687 -- -modulus. The trouble is what type do we use to do the subtract.
4688 -- No type will do, since modulus can be as big as 2**64, and no
4689 -- integer type accommodates this value. Let's do bit of algebra
4691 -- modulus + value
4692 -- = modulus - (-value)
4693 -- = (modulus - 1) - (-value - 1)
4695 -- Now modulus - 1 is certainly in range of the modular type.
4696 -- -value is in the range 1 .. modulus, so -value -1 is in the
4697 -- range 0 .. modulus-1 which is in range of the modular type.
4698 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
4699 -- which we can compute using the integer base type.
4701 -- Once this is done we analyze the if expression without range
4702 -- checks, because we know everything is in range, and we want
4703 -- to prevent spurious warnings on either branch.
4705 else
4706 Rewrite (N,
4707 Make_If_Expression (Loc,
4708 Expressions => New_List (
4709 Make_Op_Ge (Loc,
4710 Left_Opnd => Duplicate_Subexpr (Arg),
4711 Right_Opnd => Make_Integer_Literal (Loc, 0)),
4713 Convert_To (Btyp,
4714 Duplicate_Subexpr_No_Checks (Arg)),
4716 Make_Op_Subtract (Loc,
4717 Left_Opnd =>
4718 Make_Integer_Literal (Loc,
4719 Intval => Modv - 1),
4720 Right_Opnd =>
4721 Convert_To (Btyp,
4722 Make_Op_Minus (Loc,
4723 Right_Opnd =>
4724 Make_Op_Add (Loc,
4725 Left_Opnd => Duplicate_Subexpr_No_Checks (Arg),
4726 Right_Opnd =>
4727 Make_Integer_Literal (Loc,
4728 Intval => 1))))))));
4730 end if;
4732 Analyze_And_Resolve (N, Btyp, Suppress => All_Checks);
4733 end Mod_Case;
4735 -----------
4736 -- Model --
4737 -----------
4739 -- Transforms 'Model into a call to the floating-point attribute
4740 -- function Model in Fat_xxx (where xxx is the root type).
4741 -- Expansion is avoided for cases the back end can handle directly.
4743 when Attribute_Model =>
4744 if not Is_Inline_Floating_Point_Attribute (N) then
4745 Expand_Fpt_Attribute_R (N);
4746 end if;
4748 -----------------
4749 -- Object_Size --
4750 -----------------
4752 -- The processing for Object_Size shares the processing for Size
4754 ---------
4755 -- Old --
4756 ---------
4758 when Attribute_Old => Old : declare
4759 Typ : constant Entity_Id := Etype (N);
4760 CW_Temp : Entity_Id;
4761 CW_Typ : Entity_Id;
4762 Ins_Nod : Node_Id;
4763 Subp : Node_Id;
4764 Temp : Entity_Id;
4766 begin
4767 -- Generating C code we don't need to expand this attribute when
4768 -- we are analyzing the internally built nested postconditions
4769 -- procedure since it will be expanded inline (and later it will
4770 -- be removed by Expand_N_Subprogram_Body). It this expansion is
4771 -- performed in such case then the compiler generates unreferenced
4772 -- extra temporaries.
4774 if Modify_Tree_For_C
4775 and then Chars (Current_Scope) = Name_uPostconditions
4776 then
4777 return;
4778 end if;
4780 -- Climb the parent chain looking for subprogram _Postconditions
4782 Subp := N;
4783 while Present (Subp) loop
4784 exit when Nkind (Subp) = N_Subprogram_Body
4785 and then Chars (Defining_Entity (Subp)) = Name_uPostconditions;
4787 -- If assertions are disabled, no need to create the declaration
4788 -- that preserves the value. The postcondition pragma in which
4789 -- 'Old appears will be checked or disabled according to the
4790 -- current policy in effect.
4792 if Nkind (Subp) = N_Pragma and then not Is_Checked (Subp) then
4793 return;
4794 end if;
4796 Subp := Parent (Subp);
4797 end loop;
4799 -- 'Old can only appear in a postcondition, the generated body of
4800 -- _Postconditions must be in the tree (or inlined if we are
4801 -- generating C code).
4803 pragma Assert
4804 (Present (Subp)
4805 or else (Modify_Tree_For_C and then In_Inlined_Body));
4807 Temp := Make_Temporary (Loc, 'T', Pref);
4809 -- Set the entity kind now in order to mark the temporary as a
4810 -- handler of attribute 'Old's prefix.
4812 Set_Ekind (Temp, E_Constant);
4813 Set_Stores_Attribute_Old_Prefix (Temp);
4815 -- Push the scope of the related subprogram where _Postcondition
4816 -- resides as this ensures that the object will be analyzed in the
4817 -- proper context.
4819 if Present (Subp) then
4820 Push_Scope (Scope (Defining_Entity (Subp)));
4822 -- No need to push the scope when generating C code since the
4823 -- _Postcondition procedure has been inlined.
4825 else pragma Assert (Modify_Tree_For_C);
4826 pragma Assert (In_Inlined_Body);
4827 null;
4828 end if;
4830 -- Locate the insertion place of the internal temporary that saves
4831 -- the 'Old value.
4833 if Present (Subp) then
4834 Ins_Nod := Subp;
4836 -- Generating C, the postcondition procedure has been inlined and the
4837 -- temporary is added before the first declaration of the enclosing
4838 -- subprogram.
4840 else pragma Assert (Modify_Tree_For_C);
4841 Ins_Nod := N;
4842 while Nkind (Ins_Nod) /= N_Subprogram_Body loop
4843 Ins_Nod := Parent (Ins_Nod);
4844 end loop;
4846 Ins_Nod := First (Declarations (Ins_Nod));
4847 end if;
4849 -- Preserve the tag of the prefix by offering a specific view of the
4850 -- class-wide version of the prefix.
4852 if Is_Tagged_Type (Typ) then
4854 -- Generate:
4855 -- CW_Temp : constant Typ'Class := Typ'Class (Pref);
4857 CW_Temp := Make_Temporary (Loc, 'T');
4858 CW_Typ := Class_Wide_Type (Typ);
4860 Insert_Before_And_Analyze (Ins_Nod,
4861 Make_Object_Declaration (Loc,
4862 Defining_Identifier => CW_Temp,
4863 Constant_Present => True,
4864 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
4865 Expression =>
4866 Convert_To (CW_Typ, Relocate_Node (Pref))));
4868 -- Generate:
4869 -- Temp : Typ renames Typ (CW_Temp);
4871 Insert_Before_And_Analyze (Ins_Nod,
4872 Make_Object_Renaming_Declaration (Loc,
4873 Defining_Identifier => Temp,
4874 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4875 Name =>
4876 Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc))));
4878 -- Non-tagged case
4880 else
4881 -- Generate:
4882 -- Temp : constant Typ := Pref;
4884 Insert_Before_And_Analyze (Ins_Nod,
4885 Make_Object_Declaration (Loc,
4886 Defining_Identifier => Temp,
4887 Constant_Present => True,
4888 Object_Definition => New_Occurrence_Of (Typ, Loc),
4889 Expression => Relocate_Node (Pref)));
4890 end if;
4892 if Present (Subp) then
4893 Pop_Scope;
4894 end if;
4896 -- Ensure that the prefix of attribute 'Old is valid. The check must
4897 -- be inserted after the expansion of the attribute has taken place
4898 -- to reflect the new placement of the prefix.
4900 if Validity_Checks_On and then Validity_Check_Operands then
4901 Ensure_Valid (Pref);
4902 end if;
4904 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4905 end Old;
4907 ----------------------
4908 -- Overlaps_Storage --
4909 ----------------------
4911 when Attribute_Overlaps_Storage => Overlaps_Storage : declare
4912 Loc : constant Source_Ptr := Sloc (N);
4914 X : constant Node_Id := Prefix (N);
4915 Y : constant Node_Id := First (Expressions (N));
4916 -- The arguments
4918 X_Addr, Y_Addr : Node_Id;
4919 -- the expressions for their integer addresses
4921 X_Size, Y_Size : Node_Id;
4922 -- the expressions for their sizes
4924 Cond : Node_Id;
4926 begin
4927 -- Attribute expands into:
4929 -- if X'Address < Y'address then
4930 -- (X'address + X'Size - 1) >= Y'address
4931 -- else
4932 -- (Y'address + Y'size - 1) >= X'Address
4933 -- end if;
4935 -- with the proper address operations. We convert addresses to
4936 -- integer addresses to use predefined arithmetic. The size is
4937 -- expressed in storage units. We add copies of X_Addr and Y_Addr
4938 -- to prevent the appearance of the same node in two places in
4939 -- the tree.
4941 X_Addr :=
4942 Unchecked_Convert_To (RTE (RE_Integer_Address),
4943 Make_Attribute_Reference (Loc,
4944 Attribute_Name => Name_Address,
4945 Prefix => New_Copy_Tree (X)));
4947 Y_Addr :=
4948 Unchecked_Convert_To (RTE (RE_Integer_Address),
4949 Make_Attribute_Reference (Loc,
4950 Attribute_Name => Name_Address,
4951 Prefix => New_Copy_Tree (Y)));
4953 X_Size :=
4954 Make_Op_Divide (Loc,
4955 Left_Opnd =>
4956 Make_Attribute_Reference (Loc,
4957 Attribute_Name => Name_Size,
4958 Prefix => New_Copy_Tree (X)),
4959 Right_Opnd =>
4960 Make_Integer_Literal (Loc, System_Storage_Unit));
4962 Y_Size :=
4963 Make_Op_Divide (Loc,
4964 Left_Opnd =>
4965 Make_Attribute_Reference (Loc,
4966 Attribute_Name => Name_Size,
4967 Prefix => New_Copy_Tree (Y)),
4968 Right_Opnd =>
4969 Make_Integer_Literal (Loc, System_Storage_Unit));
4971 Cond :=
4972 Make_Op_Le (Loc,
4973 Left_Opnd => X_Addr,
4974 Right_Opnd => Y_Addr);
4976 Rewrite (N,
4977 Make_If_Expression (Loc, New_List (
4978 Cond,
4980 Make_Op_Ge (Loc,
4981 Left_Opnd =>
4982 Make_Op_Add (Loc,
4983 Left_Opnd => New_Copy_Tree (X_Addr),
4984 Right_Opnd =>
4985 Make_Op_Subtract (Loc,
4986 Left_Opnd => X_Size,
4987 Right_Opnd => Make_Integer_Literal (Loc, 1))),
4988 Right_Opnd => Y_Addr),
4990 Make_Op_Ge (Loc,
4991 Left_Opnd =>
4992 Make_Op_Add (Loc,
4993 Left_Opnd => New_Copy_Tree (Y_Addr),
4994 Right_Opnd =>
4995 Make_Op_Subtract (Loc,
4996 Left_Opnd => Y_Size,
4997 Right_Opnd => Make_Integer_Literal (Loc, 1))),
4998 Right_Opnd => X_Addr))));
5000 Analyze_And_Resolve (N, Standard_Boolean);
5001 end Overlaps_Storage;
5003 ------------
5004 -- Output --
5005 ------------
5007 when Attribute_Output => Output : declare
5008 P_Type : constant Entity_Id := Entity (Pref);
5009 U_Type : constant Entity_Id := Underlying_Type (P_Type);
5010 Pname : Entity_Id;
5011 Decl : Node_Id;
5012 Prag : Node_Id;
5013 Arg3 : Node_Id;
5014 Wfunc : Node_Id;
5016 begin
5017 -- If no underlying type, we have an error that will be diagnosed
5018 -- elsewhere, so here we just completely ignore the expansion.
5020 if No (U_Type) then
5021 return;
5022 end if;
5024 -- Stream operations can appear in user code even if the restriction
5025 -- No_Streams is active (for example, when instantiating a predefined
5026 -- container). In that case rewrite the attribute as a Raise to
5027 -- prevent any run-time use.
5029 if Restriction_Active (No_Streams) then
5030 Rewrite (N,
5031 Make_Raise_Program_Error (Sloc (N),
5032 Reason => PE_Stream_Operation_Not_Allowed));
5033 Set_Etype (N, Standard_Void_Type);
5034 return;
5035 end if;
5037 -- If TSS for Output is present, just call it
5039 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
5041 if Present (Pname) then
5042 null;
5044 else
5045 -- If there is a Stream_Convert pragma, use it, we rewrite
5047 -- sourcetyp'Output (stream, Item)
5049 -- as
5051 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
5053 -- where strmwrite is the given Write function that converts an
5054 -- argument of type sourcetyp or a type acctyp, from which it is
5055 -- derived to type strmtyp. The conversion to acttyp is required
5056 -- for the derived case.
5058 Prag := Get_Stream_Convert_Pragma (P_Type);
5060 if Present (Prag) then
5061 Arg3 :=
5062 Next (Next (First (Pragma_Argument_Associations (Prag))));
5063 Wfunc := Entity (Expression (Arg3));
5065 Rewrite (N,
5066 Make_Attribute_Reference (Loc,
5067 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
5068 Attribute_Name => Name_Output,
5069 Expressions => New_List (
5070 Relocate_Node (First (Exprs)),
5071 Make_Function_Call (Loc,
5072 Name => New_Occurrence_Of (Wfunc, Loc),
5073 Parameter_Associations => New_List (
5074 OK_Convert_To (Etype (First_Formal (Wfunc)),
5075 Relocate_Node (Next (First (Exprs)))))))));
5077 Analyze (N);
5078 return;
5080 -- For elementary types, we call the W_xxx routine directly. Note
5081 -- that the effect of Write and Output is identical for the case
5082 -- of an elementary type (there are no discriminants or bounds).
5084 elsif Is_Elementary_Type (U_Type) then
5086 -- A special case arises if we have a defined _Write routine,
5087 -- since in this case we are required to call this routine.
5089 declare
5090 Typ : Entity_Id := P_Type;
5091 begin
5092 if Present (Full_View (Typ)) then
5093 Typ := Full_View (Typ);
5094 end if;
5096 if Present (TSS (Base_Type (Typ), TSS_Stream_Write)) then
5097 Build_Record_Or_Elementary_Output_Procedure
5098 (Loc, Typ, Decl, Pname);
5099 Insert_Action (N, Decl);
5101 -- For normal cases, we call the W_xxx routine directly
5103 else
5104 Rewrite (N, Build_Elementary_Write_Call (N));
5105 Analyze (N);
5106 return;
5107 end if;
5108 end;
5110 -- Array type case
5112 elsif Is_Array_Type (U_Type) then
5113 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
5114 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5116 -- Class-wide case, first output external tag, then dispatch
5117 -- to the appropriate primitive Output function (RM 13.13.2(31)).
5119 elsif Is_Class_Wide_Type (P_Type) then
5121 -- No need to do anything else compiling under restriction
5122 -- No_Dispatching_Calls. During the semantic analysis we
5123 -- already notified such violation.
5125 if Restriction_Active (No_Dispatching_Calls) then
5126 return;
5127 end if;
5129 Tag_Write : declare
5130 Strm : constant Node_Id := First (Exprs);
5131 Item : constant Node_Id := Next (Strm);
5133 begin
5134 -- Ada 2005 (AI-344): Check that the accessibility level
5135 -- of the type of the output object is not deeper than
5136 -- that of the attribute's prefix type.
5138 -- if Get_Access_Level (Item'Tag)
5139 -- /= Get_Access_Level (P_Type'Tag)
5140 -- then
5141 -- raise Tag_Error;
5142 -- end if;
5144 -- String'Output (Strm, External_Tag (Item'Tag));
5146 -- We cannot figure out a practical way to implement this
5147 -- accessibility check on virtual machines, so we omit it.
5149 if Ada_Version >= Ada_2005
5150 and then Tagged_Type_Expansion
5151 then
5152 Insert_Action (N,
5153 Make_Implicit_If_Statement (N,
5154 Condition =>
5155 Make_Op_Ne (Loc,
5156 Left_Opnd =>
5157 Build_Get_Access_Level (Loc,
5158 Make_Attribute_Reference (Loc,
5159 Prefix =>
5160 Relocate_Node (
5161 Duplicate_Subexpr (Item,
5162 Name_Req => True)),
5163 Attribute_Name => Name_Tag)),
5165 Right_Opnd =>
5166 Make_Integer_Literal (Loc,
5167 Type_Access_Level (P_Type))),
5169 Then_Statements =>
5170 New_List (Make_Raise_Statement (Loc,
5171 New_Occurrence_Of (
5172 RTE (RE_Tag_Error), Loc)))));
5173 end if;
5175 Insert_Action (N,
5176 Make_Attribute_Reference (Loc,
5177 Prefix => New_Occurrence_Of (Standard_String, Loc),
5178 Attribute_Name => Name_Output,
5179 Expressions => New_List (
5180 Relocate_Node (Duplicate_Subexpr (Strm)),
5181 Make_Function_Call (Loc,
5182 Name =>
5183 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
5184 Parameter_Associations => New_List (
5185 Make_Attribute_Reference (Loc,
5186 Prefix =>
5187 Relocate_Node
5188 (Duplicate_Subexpr (Item, Name_Req => True)),
5189 Attribute_Name => Name_Tag))))));
5190 end Tag_Write;
5192 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
5194 -- Tagged type case, use the primitive Output function
5196 elsif Is_Tagged_Type (U_Type) then
5197 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
5199 -- All other record type cases, including protected records.
5200 -- The latter only arise for expander generated code for
5201 -- handling shared passive partition access.
5203 else
5204 pragma Assert
5205 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5207 -- Ada 2005 (AI-216): Program_Error is raised when executing
5208 -- the default implementation of the Output attribute of an
5209 -- unchecked union type if the type lacks default discriminant
5210 -- values.
5212 if Is_Unchecked_Union (Base_Type (U_Type))
5213 and then No (Discriminant_Constraint (U_Type))
5214 then
5215 Insert_Action (N,
5216 Make_Raise_Program_Error (Loc,
5217 Reason => PE_Unchecked_Union_Restriction));
5219 return;
5220 end if;
5222 Build_Record_Or_Elementary_Output_Procedure
5223 (Loc, Base_Type (U_Type), Decl, Pname);
5224 Insert_Action (N, Decl);
5225 end if;
5226 end if;
5228 -- If we fall through, Pname is the name of the procedure to call
5230 Rewrite_Stream_Proc_Call (Pname);
5231 end Output;
5233 ---------
5234 -- Pos --
5235 ---------
5237 -- For enumeration types with a standard representation, Pos is
5238 -- handled by the back end.
5240 -- For enumeration types, with a non-standard representation we generate
5241 -- a call to the _Rep_To_Pos function created when the type was frozen.
5242 -- The call has the form
5244 -- _rep_to_pos (expr, flag)
5246 -- The parameter flag is True if range checks are enabled, causing
5247 -- Program_Error to be raised if the expression has an invalid
5248 -- representation, and False if range checks are suppressed.
5250 -- For integer types, Pos is equivalent to a simple integer
5251 -- conversion and we rewrite it as such
5253 when Attribute_Pos => Pos : declare
5254 Etyp : Entity_Id := Base_Type (Entity (Pref));
5256 begin
5257 -- Deal with zero/non-zero boolean values
5259 if Is_Boolean_Type (Etyp) then
5260 Adjust_Condition (First (Exprs));
5261 Etyp := Standard_Boolean;
5262 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
5263 end if;
5265 -- Case of enumeration type
5267 if Is_Enumeration_Type (Etyp) then
5269 -- Non-standard enumeration type (generate call)
5271 if Present (Enum_Pos_To_Rep (Etyp)) then
5272 Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
5273 Rewrite (N,
5274 Convert_To (Typ,
5275 Make_Function_Call (Loc,
5276 Name =>
5277 New_Occurrence_Of (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5278 Parameter_Associations => Exprs)));
5280 Analyze_And_Resolve (N, Typ);
5282 -- Standard enumeration type (do universal integer check)
5284 else
5285 Apply_Universal_Integer_Attribute_Checks (N);
5286 end if;
5288 -- Deal with integer types (replace by conversion)
5290 elsif Is_Integer_Type (Etyp) then
5291 Rewrite (N, Convert_To (Typ, First (Exprs)));
5292 Analyze_And_Resolve (N, Typ);
5293 end if;
5295 end Pos;
5297 --------------
5298 -- Position --
5299 --------------
5301 -- We compute this if a component clause was present, otherwise we leave
5302 -- the computation up to the back end, since we don't know what layout
5303 -- will be chosen.
5305 when Attribute_Position => Position_Attr : declare
5306 CE : constant Entity_Id := Entity (Selector_Name (Pref));
5308 begin
5309 if Present (Component_Clause (CE)) then
5311 -- In Ada 2005 (or later) if we have the non-default bit order,
5312 -- then we return the original value as given in the component
5313 -- clause (RM 2005 13.5.2(2/2)).
5315 if Ada_Version >= Ada_2005
5316 and then Reverse_Bit_Order (Scope (CE))
5317 then
5318 Rewrite (N,
5319 Make_Integer_Literal (Loc,
5320 Intval => Expr_Value (Position (Component_Clause (CE)))));
5322 -- Otherwise (Ada 83 or 95, or default bit order specified in
5323 -- later Ada version), return the normalized value.
5325 else
5326 Rewrite (N,
5327 Make_Integer_Literal (Loc,
5328 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
5329 end if;
5331 Analyze_And_Resolve (N, Typ);
5333 -- If back end is doing things, just apply universal integer checks
5335 else
5336 Apply_Universal_Integer_Attribute_Checks (N);
5337 end if;
5338 end Position_Attr;
5340 ----------
5341 -- Pred --
5342 ----------
5344 -- 1. Deal with enumeration types with holes.
5345 -- 2. For floating-point, generate call to attribute function.
5346 -- 3. For other cases, deal with constraint checking.
5348 when Attribute_Pred => Pred : declare
5349 Etyp : constant Entity_Id := Base_Type (Ptyp);
5351 begin
5353 -- For enumeration types with non-standard representations, we
5354 -- expand typ'Pred (x) into
5356 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
5358 -- If the representation is contiguous, we compute instead
5359 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
5360 -- The conversion function Enum_Pos_To_Rep is defined on the
5361 -- base type, not the subtype, so we have to use the base type
5362 -- explicitly for this and other enumeration attributes.
5364 if Is_Enumeration_Type (Ptyp)
5365 and then Present (Enum_Pos_To_Rep (Etyp))
5366 then
5367 if Has_Contiguous_Rep (Etyp) then
5368 Rewrite (N,
5369 Unchecked_Convert_To (Ptyp,
5370 Make_Op_Add (Loc,
5371 Left_Opnd =>
5372 Make_Integer_Literal (Loc,
5373 Enumeration_Rep (First_Literal (Ptyp))),
5374 Right_Opnd =>
5375 Make_Function_Call (Loc,
5376 Name =>
5377 New_Occurrence_Of
5378 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5380 Parameter_Associations =>
5381 New_List (
5382 Unchecked_Convert_To (Ptyp,
5383 Make_Op_Subtract (Loc,
5384 Left_Opnd =>
5385 Unchecked_Convert_To (Standard_Integer,
5386 Relocate_Node (First (Exprs))),
5387 Right_Opnd =>
5388 Make_Integer_Literal (Loc, 1))),
5389 Rep_To_Pos_Flag (Ptyp, Loc))))));
5391 else
5392 -- Add Boolean parameter True, to request program errror if
5393 -- we have a bad representation on our hands. If checks are
5394 -- suppressed, then add False instead
5396 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
5397 Rewrite (N,
5398 Make_Indexed_Component (Loc,
5399 Prefix =>
5400 New_Occurrence_Of
5401 (Enum_Pos_To_Rep (Etyp), Loc),
5402 Expressions => New_List (
5403 Make_Op_Subtract (Loc,
5404 Left_Opnd =>
5405 Make_Function_Call (Loc,
5406 Name =>
5407 New_Occurrence_Of
5408 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5409 Parameter_Associations => Exprs),
5410 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
5411 end if;
5413 Analyze_And_Resolve (N, Typ);
5415 -- For floating-point, we transform 'Pred into a call to the Pred
5416 -- floating-point attribute function in Fat_xxx (xxx is root type).
5417 -- Note that this function takes care of the overflow case.
5419 elsif Is_Floating_Point_Type (Ptyp) then
5420 Expand_Fpt_Attribute_R (N);
5421 Analyze_And_Resolve (N, Typ);
5423 -- For modular types, nothing to do (no overflow, since wraps)
5425 elsif Is_Modular_Integer_Type (Ptyp) then
5426 null;
5428 -- For other types, if argument is marked as needing a range check or
5429 -- overflow checking is enabled, we must generate a check.
5431 elsif not Overflow_Checks_Suppressed (Ptyp)
5432 or else Do_Range_Check (First (Exprs))
5433 then
5434 Set_Do_Range_Check (First (Exprs), False);
5435 Expand_Pred_Succ_Attribute (N);
5436 end if;
5437 end Pred;
5439 --------------
5440 -- Priority --
5441 --------------
5443 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5445 -- We rewrite X'Priority as the following run-time call:
5447 -- Get_Ceiling (X._Object)
5449 -- Note that although X'Priority is notionally an object, it is quite
5450 -- deliberately not defined as an aliased object in the RM. This means
5451 -- that it works fine to rewrite it as a call, without having to worry
5452 -- about complications that would other arise from X'Priority'Access,
5453 -- which is illegal, because of the lack of aliasing.
5455 when Attribute_Priority => Priority : declare
5456 Call : Node_Id;
5457 Conctyp : Entity_Id;
5458 New_Itype : Entity_Id;
5459 Object_Parm : Node_Id;
5460 Subprg : Entity_Id;
5461 RT_Subprg_Name : Node_Id;
5463 begin
5464 -- Look for the enclosing concurrent type
5466 Conctyp := Current_Scope;
5467 while not Is_Concurrent_Type (Conctyp) loop
5468 Conctyp := Scope (Conctyp);
5469 end loop;
5471 pragma Assert (Is_Protected_Type (Conctyp));
5473 -- Generate the actual of the call
5475 Subprg := Current_Scope;
5476 while not Present (Protected_Body_Subprogram (Subprg)) loop
5477 Subprg := Scope (Subprg);
5478 end loop;
5480 -- Use of 'Priority inside protected entries and barriers (in both
5481 -- cases the type of the first formal of their expanded subprogram
5482 -- is Address)
5484 if Etype (First_Entity (Protected_Body_Subprogram (Subprg))) =
5485 RTE (RE_Address)
5486 then
5487 -- In the expansion of protected entries the type of the first
5488 -- formal of the Protected_Body_Subprogram is an Address. In order
5489 -- to reference the _object component we generate:
5491 -- type T is access p__ptTV;
5492 -- freeze T []
5494 New_Itype := Create_Itype (E_Access_Type, N);
5495 Set_Etype (New_Itype, New_Itype);
5496 Set_Directly_Designated_Type (New_Itype,
5497 Corresponding_Record_Type (Conctyp));
5498 Freeze_Itype (New_Itype, N);
5500 -- Generate:
5501 -- T!(O)._object'unchecked_access
5503 Object_Parm :=
5504 Make_Attribute_Reference (Loc,
5505 Prefix =>
5506 Make_Selected_Component (Loc,
5507 Prefix =>
5508 Unchecked_Convert_To (New_Itype,
5509 New_Occurrence_Of
5510 (First_Entity (Protected_Body_Subprogram (Subprg)),
5511 Loc)),
5512 Selector_Name => Make_Identifier (Loc, Name_uObject)),
5513 Attribute_Name => Name_Unchecked_Access);
5515 -- Use of 'Priority inside a protected subprogram
5517 else
5518 Object_Parm :=
5519 Make_Attribute_Reference (Loc,
5520 Prefix =>
5521 Make_Selected_Component (Loc,
5522 Prefix =>
5523 New_Occurrence_Of
5524 (First_Entity (Protected_Body_Subprogram (Subprg)),
5525 Loc),
5526 Selector_Name => Make_Identifier (Loc, Name_uObject)),
5527 Attribute_Name => Name_Unchecked_Access);
5528 end if;
5530 -- Select the appropriate run-time subprogram
5532 if Number_Entries (Conctyp) = 0 then
5533 RT_Subprg_Name := New_Occurrence_Of (RTE (RE_Get_Ceiling), Loc);
5534 else
5535 RT_Subprg_Name := New_Occurrence_Of (RTE (RO_PE_Get_Ceiling), Loc);
5536 end if;
5538 Call :=
5539 Make_Function_Call (Loc,
5540 Name => RT_Subprg_Name,
5541 Parameter_Associations => New_List (Object_Parm));
5543 Rewrite (N, Call);
5545 -- Avoid the generation of extra checks on the pointer to the
5546 -- protected object.
5548 Analyze_And_Resolve (N, Typ, Suppress => Access_Check);
5549 end Priority;
5551 ------------------
5552 -- Range_Length --
5553 ------------------
5555 when Attribute_Range_Length =>
5557 -- The only special processing required is for the case where
5558 -- Range_Length is applied to an enumeration type with holes.
5559 -- In this case we transform
5561 -- X'Range_Length
5563 -- to
5565 -- X'Pos (X'Last) - X'Pos (X'First) + 1
5567 -- So that the result reflects the proper Pos values instead
5568 -- of the underlying representations.
5570 if Is_Enumeration_Type (Ptyp)
5571 and then Has_Non_Standard_Rep (Ptyp)
5572 then
5573 Rewrite (N,
5574 Make_Op_Add (Loc,
5575 Left_Opnd =>
5576 Make_Op_Subtract (Loc,
5577 Left_Opnd =>
5578 Make_Attribute_Reference (Loc,
5579 Attribute_Name => Name_Pos,
5580 Prefix => New_Occurrence_Of (Ptyp, Loc),
5581 Expressions => New_List (
5582 Make_Attribute_Reference (Loc,
5583 Attribute_Name => Name_Last,
5584 Prefix =>
5585 New_Occurrence_Of (Ptyp, Loc)))),
5587 Right_Opnd =>
5588 Make_Attribute_Reference (Loc,
5589 Attribute_Name => Name_Pos,
5590 Prefix => New_Occurrence_Of (Ptyp, Loc),
5591 Expressions => New_List (
5592 Make_Attribute_Reference (Loc,
5593 Attribute_Name => Name_First,
5594 Prefix =>
5595 New_Occurrence_Of (Ptyp, Loc))))),
5597 Right_Opnd => Make_Integer_Literal (Loc, 1)));
5599 Analyze_And_Resolve (N, Typ);
5601 -- For all other cases, the attribute is handled by the back end, but
5602 -- we need to deal with the case of the range check on a universal
5603 -- integer.
5605 else
5606 Apply_Universal_Integer_Attribute_Checks (N);
5607 end if;
5609 ----------
5610 -- Read --
5611 ----------
5613 when Attribute_Read => Read : declare
5614 P_Type : constant Entity_Id := Entity (Pref);
5615 B_Type : constant Entity_Id := Base_Type (P_Type);
5616 U_Type : constant Entity_Id := Underlying_Type (P_Type);
5617 Pname : Entity_Id;
5618 Decl : Node_Id;
5619 Prag : Node_Id;
5620 Arg2 : Node_Id;
5621 Rfunc : Node_Id;
5622 Lhs : Node_Id;
5623 Rhs : Node_Id;
5625 begin
5626 -- If no underlying type, we have an error that will be diagnosed
5627 -- elsewhere, so here we just completely ignore the expansion.
5629 if No (U_Type) then
5630 return;
5631 end if;
5633 -- Stream operations can appear in user code even if the restriction
5634 -- No_Streams is active (for example, when instantiating a predefined
5635 -- container). In that case rewrite the attribute as a Raise to
5636 -- prevent any run-time use.
5638 if Restriction_Active (No_Streams) then
5639 Rewrite (N,
5640 Make_Raise_Program_Error (Sloc (N),
5641 Reason => PE_Stream_Operation_Not_Allowed));
5642 Set_Etype (N, B_Type);
5643 return;
5644 end if;
5646 -- The simple case, if there is a TSS for Read, just call it
5648 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
5650 if Present (Pname) then
5651 null;
5653 else
5654 -- If there is a Stream_Convert pragma, use it, we rewrite
5656 -- sourcetyp'Read (stream, Item)
5658 -- as
5660 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
5662 -- where strmread is the given Read function that converts an
5663 -- argument of type strmtyp to type sourcetyp or a type from which
5664 -- it is derived. The conversion to sourcetyp is required in the
5665 -- latter case.
5667 -- A special case arises if Item is a type conversion in which
5668 -- case, we have to expand to:
5670 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
5672 -- where Itemx is the expression of the type conversion (i.e.
5673 -- the actual object), and typex is the type of Itemx.
5675 Prag := Get_Stream_Convert_Pragma (P_Type);
5677 if Present (Prag) then
5678 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
5679 Rfunc := Entity (Expression (Arg2));
5680 Lhs := Relocate_Node (Next (First (Exprs)));
5681 Rhs :=
5682 OK_Convert_To (B_Type,
5683 Make_Function_Call (Loc,
5684 Name => New_Occurrence_Of (Rfunc, Loc),
5685 Parameter_Associations => New_List (
5686 Make_Attribute_Reference (Loc,
5687 Prefix =>
5688 New_Occurrence_Of
5689 (Etype (First_Formal (Rfunc)), Loc),
5690 Attribute_Name => Name_Input,
5691 Expressions => New_List (
5692 Relocate_Node (First (Exprs)))))));
5694 if Nkind (Lhs) = N_Type_Conversion then
5695 Lhs := Expression (Lhs);
5696 Rhs := Convert_To (Etype (Lhs), Rhs);
5697 end if;
5699 Rewrite (N,
5700 Make_Assignment_Statement (Loc,
5701 Name => Lhs,
5702 Expression => Rhs));
5703 Set_Assignment_OK (Lhs);
5704 Analyze (N);
5705 return;
5707 -- For elementary types, we call the I_xxx routine using the first
5708 -- parameter and then assign the result into the second parameter.
5709 -- We set Assignment_OK to deal with the conversion case.
5711 elsif Is_Elementary_Type (U_Type) then
5712 declare
5713 Lhs : Node_Id;
5714 Rhs : Node_Id;
5716 begin
5717 Lhs := Relocate_Node (Next (First (Exprs)));
5718 Rhs := Build_Elementary_Input_Call (N);
5720 if Nkind (Lhs) = N_Type_Conversion then
5721 Lhs := Expression (Lhs);
5722 Rhs := Convert_To (Etype (Lhs), Rhs);
5723 end if;
5725 Set_Assignment_OK (Lhs);
5727 Rewrite (N,
5728 Make_Assignment_Statement (Loc,
5729 Name => Lhs,
5730 Expression => Rhs));
5732 Analyze (N);
5733 return;
5734 end;
5736 -- Array type case
5738 elsif Is_Array_Type (U_Type) then
5739 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
5740 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5742 -- Tagged type case, use the primitive Read function. Note that
5743 -- this will dispatch in the class-wide case which is what we want
5745 elsif Is_Tagged_Type (U_Type) then
5746 Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
5748 -- All other record type cases, including protected records. The
5749 -- latter only arise for expander generated code for handling
5750 -- shared passive partition access.
5752 else
5753 pragma Assert
5754 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5756 -- Ada 2005 (AI-216): Program_Error is raised when executing
5757 -- the default implementation of the Read attribute of an
5758 -- Unchecked_Union type. We replace the attribute with a
5759 -- raise statement (rather than inserting it before) to handle
5760 -- properly the case of an unchecked union that is a record
5761 -- component.
5763 if Is_Unchecked_Union (Base_Type (U_Type)) then
5764 Rewrite (N,
5765 Make_Raise_Program_Error (Loc,
5766 Reason => PE_Unchecked_Union_Restriction));
5767 Set_Etype (N, B_Type);
5768 return;
5769 end if;
5771 if Has_Discriminants (U_Type)
5772 and then Present
5773 (Discriminant_Default_Value (First_Discriminant (U_Type)))
5774 then
5775 Build_Mutable_Record_Read_Procedure
5776 (Loc, Full_Base (U_Type), Decl, Pname);
5777 else
5778 Build_Record_Read_Procedure
5779 (Loc, Full_Base (U_Type), Decl, Pname);
5780 end if;
5782 -- Suppress checks, uninitialized or otherwise invalid
5783 -- data does not cause constraint errors to be raised for
5784 -- a complete record read.
5786 Insert_Action (N, Decl, All_Checks);
5787 end if;
5788 end if;
5790 Rewrite_Stream_Proc_Call (Pname);
5791 end Read;
5793 ---------
5794 -- Ref --
5795 ---------
5797 -- Ref is identical to To_Address, see To_Address for processing
5799 ---------------
5800 -- Remainder --
5801 ---------------
5803 -- Transforms 'Remainder into a call to the floating-point attribute
5804 -- function Remainder in Fat_xxx (where xxx is the root type)
5806 when Attribute_Remainder =>
5807 Expand_Fpt_Attribute_RR (N);
5809 ------------
5810 -- Result --
5811 ------------
5813 -- Transform 'Result into reference to _Result formal. At the point
5814 -- where a legal 'Result attribute is expanded, we know that we are in
5815 -- the context of a _Postcondition function with a _Result parameter.
5817 when Attribute_Result =>
5818 Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult));
5819 Analyze_And_Resolve (N, Typ);
5821 -----------
5822 -- Round --
5823 -----------
5825 -- The handling of the Round attribute is quite delicate. The processing
5826 -- in Sem_Attr introduced a conversion to universal real, reflecting the
5827 -- semantics of Round, but we do not want anything to do with universal
5828 -- real at runtime, since this corresponds to using floating-point
5829 -- arithmetic.
5831 -- What we have now is that the Etype of the Round attribute correctly
5832 -- indicates the final result type. The operand of the Round is the
5833 -- conversion to universal real, described above, and the operand of
5834 -- this conversion is the actual operand of Round, which may be the
5835 -- special case of a fixed point multiplication or division (Etype =
5836 -- universal fixed)
5838 -- The exapander will expand first the operand of the conversion, then
5839 -- the conversion, and finally the round attribute itself, since we
5840 -- always work inside out. But we cannot simply process naively in this
5841 -- order. In the semantic world where universal fixed and real really
5842 -- exist and have infinite precision, there is no problem, but in the
5843 -- implementation world, where universal real is a floating-point type,
5844 -- we would get the wrong result.
5846 -- So the approach is as follows. First, when expanding a multiply or
5847 -- divide whose type is universal fixed, we do nothing at all, instead
5848 -- deferring the operation till later.
5850 -- The actual processing is done in Expand_N_Type_Conversion which
5851 -- handles the special case of Round by looking at its parent to see if
5852 -- it is a Round attribute, and if it is, handling the conversion (or
5853 -- its fixed multiply/divide child) in an appropriate manner.
5855 -- This means that by the time we get to expanding the Round attribute
5856 -- itself, the Round is nothing more than a type conversion (and will
5857 -- often be a null type conversion), so we just replace it with the
5858 -- appropriate conversion operation.
5860 when Attribute_Round =>
5861 Rewrite (N,
5862 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
5863 Analyze_And_Resolve (N);
5865 --------------
5866 -- Rounding --
5867 --------------
5869 -- Transforms 'Rounding into a call to the floating-point attribute
5870 -- function Rounding in Fat_xxx (where xxx is the root type)
5871 -- Expansion is avoided for cases the back end can handle directly.
5873 when Attribute_Rounding =>
5874 if not Is_Inline_Floating_Point_Attribute (N) then
5875 Expand_Fpt_Attribute_R (N);
5876 end if;
5878 -------------
5879 -- Scaling --
5880 -------------
5882 -- Transforms 'Scaling into a call to the floating-point attribute
5883 -- function Scaling in Fat_xxx (where xxx is the root type)
5885 when Attribute_Scaling =>
5886 Expand_Fpt_Attribute_RI (N);
5888 -------------------------
5889 -- Simple_Storage_Pool --
5890 -------------------------
5892 when Attribute_Simple_Storage_Pool =>
5893 Rewrite (N,
5894 Make_Type_Conversion (Loc,
5895 Subtype_Mark => New_Occurrence_Of (Etype (N), Loc),
5896 Expression => New_Occurrence_Of (Entity (N), Loc)));
5897 Analyze_And_Resolve (N, Typ);
5899 ----------
5900 -- Size --
5901 ----------
5903 when Attribute_Object_Size
5904 | Attribute_Size
5905 | Attribute_Value_Size
5906 | Attribute_VADS_Size
5908 Size : declare
5909 Siz : Uint;
5910 New_Node : Node_Id;
5912 begin
5913 -- Processing for VADS_Size case. Note that this processing
5914 -- removes all traces of VADS_Size from the tree, and completes
5915 -- all required processing for VADS_Size by translating the
5916 -- attribute reference to an appropriate Size or Object_Size
5917 -- reference.
5919 if Id = Attribute_VADS_Size
5920 or else (Use_VADS_Size and then Id = Attribute_Size)
5921 then
5922 -- If the size is specified, then we simply use the specified
5923 -- size. This applies to both types and objects. The size of an
5924 -- object can be specified in the following ways:
5926 -- An explicit size object is given for an object
5927 -- A component size is specified for an indexed component
5928 -- A component clause is specified for a selected component
5929 -- The object is a component of a packed composite object
5931 -- If the size is specified, then VADS_Size of an object
5933 if (Is_Entity_Name (Pref)
5934 and then Present (Size_Clause (Entity (Pref))))
5935 or else
5936 (Nkind (Pref) = N_Component_Clause
5937 and then (Present (Component_Clause
5938 (Entity (Selector_Name (Pref))))
5939 or else Is_Packed (Etype (Prefix (Pref)))))
5940 or else
5941 (Nkind (Pref) = N_Indexed_Component
5942 and then (Component_Size (Etype (Prefix (Pref))) /= 0
5943 or else Is_Packed (Etype (Prefix (Pref)))))
5944 then
5945 Set_Attribute_Name (N, Name_Size);
5947 -- Otherwise if we have an object rather than a type, then
5948 -- the VADS_Size attribute applies to the type of the object,
5949 -- rather than the object itself. This is one of the respects
5950 -- in which VADS_Size differs from Size.
5952 else
5953 if (not Is_Entity_Name (Pref)
5954 or else not Is_Type (Entity (Pref)))
5955 and then (Is_Scalar_Type (Ptyp)
5956 or else Is_Constrained (Ptyp))
5957 then
5958 Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc));
5959 end if;
5961 -- For a scalar type for which no size was explicitly given,
5962 -- VADS_Size means Object_Size. This is the other respect in
5963 -- which VADS_Size differs from Size.
5965 if Is_Scalar_Type (Ptyp)
5966 and then No (Size_Clause (Ptyp))
5967 then
5968 Set_Attribute_Name (N, Name_Object_Size);
5970 -- In all other cases, Size and VADS_Size are the sane
5972 else
5973 Set_Attribute_Name (N, Name_Size);
5974 end if;
5975 end if;
5976 end if;
5978 -- If the prefix is X'Class, transform it into a direct reference
5979 -- to the class-wide type, because the back end must not see a
5980 -- 'Class reference.
5982 if Is_Entity_Name (Pref)
5983 and then Is_Class_Wide_Type (Entity (Pref))
5984 then
5985 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
5986 return;
5988 -- For X'Size applied to an object of a class-wide type, transform
5989 -- X'Size into a call to the primitive operation _Size applied to
5990 -- X.
5992 elsif Is_Class_Wide_Type (Ptyp) then
5994 -- No need to do anything else compiling under restriction
5995 -- No_Dispatching_Calls. During the semantic analysis we
5996 -- already noted this restriction violation.
5998 if Restriction_Active (No_Dispatching_Calls) then
5999 return;
6000 end if;
6002 New_Node :=
6003 Make_Function_Call (Loc,
6004 Name =>
6005 New_Occurrence_Of (Find_Prim_Op (Ptyp, Name_uSize), Loc),
6006 Parameter_Associations => New_List (Pref));
6008 if Typ /= Standard_Long_Long_Integer then
6010 -- The context is a specific integer type with which the
6011 -- original attribute was compatible. The function has a
6012 -- specific type as well, so to preserve the compatibility
6013 -- we must convert explicitly.
6015 New_Node := Convert_To (Typ, New_Node);
6016 end if;
6018 Rewrite (N, New_Node);
6019 Analyze_And_Resolve (N, Typ);
6020 return;
6022 -- Case of known RM_Size of a type
6024 elsif (Id = Attribute_Size or else Id = Attribute_Value_Size)
6025 and then Is_Entity_Name (Pref)
6026 and then Is_Type (Entity (Pref))
6027 and then Known_Static_RM_Size (Entity (Pref))
6028 then
6029 Siz := RM_Size (Entity (Pref));
6031 -- Case of known Esize of a type
6033 elsif Id = Attribute_Object_Size
6034 and then Is_Entity_Name (Pref)
6035 and then Is_Type (Entity (Pref))
6036 and then Known_Static_Esize (Entity (Pref))
6037 then
6038 Siz := Esize (Entity (Pref));
6040 -- Case of known size of object
6042 elsif Id = Attribute_Size
6043 and then Is_Entity_Name (Pref)
6044 and then Is_Object (Entity (Pref))
6045 and then Known_Esize (Entity (Pref))
6046 and then Known_Static_Esize (Entity (Pref))
6047 then
6048 Siz := Esize (Entity (Pref));
6050 -- For an array component, we can do Size in the front end if the
6051 -- component_size of the array is set.
6053 elsif Nkind (Pref) = N_Indexed_Component then
6054 Siz := Component_Size (Etype (Prefix (Pref)));
6056 -- For a record component, we can do Size in the front end if
6057 -- there is a component clause, or if the record is packed and the
6058 -- component's size is known at compile time.
6060 elsif Nkind (Pref) = N_Selected_Component then
6061 declare
6062 Rec : constant Entity_Id := Etype (Prefix (Pref));
6063 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
6065 begin
6066 if Present (Component_Clause (Comp)) then
6067 Siz := Esize (Comp);
6069 elsif Is_Packed (Rec) then
6070 Siz := RM_Size (Ptyp);
6072 else
6073 Apply_Universal_Integer_Attribute_Checks (N);
6074 return;
6075 end if;
6076 end;
6078 -- All other cases are handled by the back end
6080 else
6081 Apply_Universal_Integer_Attribute_Checks (N);
6083 -- If Size is applied to a formal parameter that is of a packed
6084 -- array subtype, then apply Size to the actual subtype.
6086 if Is_Entity_Name (Pref)
6087 and then Is_Formal (Entity (Pref))
6088 and then Is_Array_Type (Ptyp)
6089 and then Is_Packed (Ptyp)
6090 then
6091 Rewrite (N,
6092 Make_Attribute_Reference (Loc,
6093 Prefix =>
6094 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
6095 Attribute_Name => Name_Size));
6096 Analyze_And_Resolve (N, Typ);
6097 end if;
6099 -- If Size applies to a dereference of an access to
6100 -- unconstrained packed array, the back end needs to see its
6101 -- unconstrained nominal type, but also a hint to the actual
6102 -- constrained type.
6104 if Nkind (Pref) = N_Explicit_Dereference
6105 and then Is_Array_Type (Ptyp)
6106 and then not Is_Constrained (Ptyp)
6107 and then Is_Packed (Ptyp)
6108 then
6109 Set_Actual_Designated_Subtype (Pref,
6110 Get_Actual_Subtype (Pref));
6111 end if;
6113 return;
6114 end if;
6116 -- Common processing for record and array component case
6118 if Siz /= No_Uint and then Siz /= 0 then
6119 declare
6120 CS : constant Boolean := Comes_From_Source (N);
6122 begin
6123 Rewrite (N, Make_Integer_Literal (Loc, Siz));
6125 -- This integer literal is not a static expression. We do
6126 -- not call Analyze_And_Resolve here, because this would
6127 -- activate the circuit for deciding that a static value
6128 -- was out of range, and we don't want that.
6130 -- So just manually set the type, mark the expression as
6131 -- non-static, and then ensure that the result is checked
6132 -- properly if the attribute comes from source (if it was
6133 -- internally generated, we never need a constraint check).
6135 Set_Etype (N, Typ);
6136 Set_Is_Static_Expression (N, False);
6138 if CS then
6139 Apply_Constraint_Check (N, Typ);
6140 end if;
6141 end;
6142 end if;
6143 end Size;
6145 ------------------
6146 -- Storage_Pool --
6147 ------------------
6149 when Attribute_Storage_Pool =>
6150 Rewrite (N,
6151 Make_Type_Conversion (Loc,
6152 Subtype_Mark => New_Occurrence_Of (Etype (N), Loc),
6153 Expression => New_Occurrence_Of (Entity (N), Loc)));
6154 Analyze_And_Resolve (N, Typ);
6156 ------------------
6157 -- Storage_Size --
6158 ------------------
6160 when Attribute_Storage_Size => Storage_Size : declare
6161 Alloc_Op : Entity_Id := Empty;
6163 begin
6165 -- Access type case, always go to the root type
6167 -- The case of access types results in a value of zero for the case
6168 -- where no storage size attribute clause has been given. If a
6169 -- storage size has been given, then the attribute is converted
6170 -- to a reference to the variable used to hold this value.
6172 if Is_Access_Type (Ptyp) then
6173 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
6174 Rewrite (N,
6175 Make_Attribute_Reference (Loc,
6176 Prefix => New_Occurrence_Of (Typ, Loc),
6177 Attribute_Name => Name_Max,
6178 Expressions => New_List (
6179 Make_Integer_Literal (Loc, 0),
6180 Convert_To (Typ,
6181 New_Occurrence_Of
6182 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
6184 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
6186 -- If the access type is associated with a simple storage pool
6187 -- object, then attempt to locate the optional Storage_Size
6188 -- function of the simple storage pool type. If not found,
6189 -- then the result will default to zero.
6191 if Present (Get_Rep_Pragma (Root_Type (Ptyp),
6192 Name_Simple_Storage_Pool_Type))
6193 then
6194 declare
6195 Pool_Type : constant Entity_Id :=
6196 Base_Type (Etype (Entity (N)));
6198 begin
6199 Alloc_Op := Get_Name_Entity_Id (Name_Storage_Size);
6200 while Present (Alloc_Op) loop
6201 if Scope (Alloc_Op) = Scope (Pool_Type)
6202 and then Present (First_Formal (Alloc_Op))
6203 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
6204 then
6205 exit;
6206 end if;
6208 Alloc_Op := Homonym (Alloc_Op);
6209 end loop;
6210 end;
6212 -- In the normal Storage_Pool case, retrieve the primitive
6213 -- function associated with the pool type.
6215 else
6216 Alloc_Op :=
6217 Find_Prim_Op
6218 (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
6219 Attribute_Name (N));
6220 end if;
6222 -- If Storage_Size wasn't found (can only occur in the simple
6223 -- storage pool case), then simply use zero for the result.
6225 if not Present (Alloc_Op) then
6226 Rewrite (N, Make_Integer_Literal (Loc, 0));
6228 -- Otherwise, rewrite the allocator as a call to pool type's
6229 -- Storage_Size function.
6231 else
6232 Rewrite (N,
6233 OK_Convert_To (Typ,
6234 Make_Function_Call (Loc,
6235 Name =>
6236 New_Occurrence_Of (Alloc_Op, Loc),
6238 Parameter_Associations => New_List (
6239 New_Occurrence_Of
6240 (Associated_Storage_Pool
6241 (Root_Type (Ptyp)), Loc)))));
6242 end if;
6244 else
6245 Rewrite (N, Make_Integer_Literal (Loc, 0));
6246 end if;
6248 Analyze_And_Resolve (N, Typ);
6250 -- For tasks, we retrieve the size directly from the TCB. The
6251 -- size may depend on a discriminant of the type, and therefore
6252 -- can be a per-object expression, so type-level information is
6253 -- not sufficient in general. There are four cases to consider:
6255 -- a) If the attribute appears within a task body, the designated
6256 -- TCB is obtained by a call to Self.
6258 -- b) If the prefix of the attribute is the name of a task object,
6259 -- the designated TCB is the one stored in the corresponding record.
6261 -- c) If the prefix is a task type, the size is obtained from the
6262 -- size variable created for each task type
6264 -- d) If no Storage_Size was specified for the type, there is no
6265 -- size variable, and the value is a system-specific default.
6267 else
6268 if In_Open_Scopes (Ptyp) then
6270 -- Storage_Size (Self)
6272 Rewrite (N,
6273 Convert_To (Typ,
6274 Make_Function_Call (Loc,
6275 Name =>
6276 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
6277 Parameter_Associations =>
6278 New_List (
6279 Make_Function_Call (Loc,
6280 Name =>
6281 New_Occurrence_Of (RTE (RE_Self), Loc))))));
6283 elsif not Is_Entity_Name (Pref)
6284 or else not Is_Type (Entity (Pref))
6285 then
6286 -- Storage_Size (Rec (Obj).Size)
6288 Rewrite (N,
6289 Convert_To (Typ,
6290 Make_Function_Call (Loc,
6291 Name =>
6292 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
6293 Parameter_Associations =>
6294 New_List (
6295 Make_Selected_Component (Loc,
6296 Prefix =>
6297 Unchecked_Convert_To (
6298 Corresponding_Record_Type (Ptyp),
6299 New_Copy_Tree (Pref)),
6300 Selector_Name =>
6301 Make_Identifier (Loc, Name_uTask_Id))))));
6303 elsif Present (Storage_Size_Variable (Ptyp)) then
6305 -- Static Storage_Size pragma given for type: retrieve value
6306 -- from its allocated storage variable.
6308 Rewrite (N,
6309 Convert_To (Typ,
6310 Make_Function_Call (Loc,
6311 Name => New_Occurrence_Of (
6312 RTE (RE_Adjust_Storage_Size), Loc),
6313 Parameter_Associations =>
6314 New_List (
6315 New_Occurrence_Of (
6316 Storage_Size_Variable (Ptyp), Loc)))));
6317 else
6318 -- Get system default
6320 Rewrite (N,
6321 Convert_To (Typ,
6322 Make_Function_Call (Loc,
6323 Name =>
6324 New_Occurrence_Of (
6325 RTE (RE_Default_Stack_Size), Loc))));
6326 end if;
6328 Analyze_And_Resolve (N, Typ);
6329 end if;
6330 end Storage_Size;
6332 -----------------
6333 -- Stream_Size --
6334 -----------------
6336 when Attribute_Stream_Size =>
6337 Rewrite (N,
6338 Make_Integer_Literal (Loc, Intval => Get_Stream_Size (Ptyp)));
6339 Analyze_And_Resolve (N, Typ);
6341 ----------
6342 -- Succ --
6343 ----------
6345 -- 1. Deal with enumeration types with holes.
6346 -- 2. For floating-point, generate call to attribute function.
6347 -- 3. For other cases, deal with constraint checking.
6349 when Attribute_Succ => Succ : declare
6350 Etyp : constant Entity_Id := Base_Type (Ptyp);
6352 begin
6353 -- For enumeration types with non-standard representations, we
6354 -- expand typ'Succ (x) into
6356 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
6358 -- If the representation is contiguous, we compute instead
6359 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
6361 if Is_Enumeration_Type (Ptyp)
6362 and then Present (Enum_Pos_To_Rep (Etyp))
6363 then
6364 if Has_Contiguous_Rep (Etyp) then
6365 Rewrite (N,
6366 Unchecked_Convert_To (Ptyp,
6367 Make_Op_Add (Loc,
6368 Left_Opnd =>
6369 Make_Integer_Literal (Loc,
6370 Enumeration_Rep (First_Literal (Ptyp))),
6371 Right_Opnd =>
6372 Make_Function_Call (Loc,
6373 Name =>
6374 New_Occurrence_Of
6375 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6377 Parameter_Associations =>
6378 New_List (
6379 Unchecked_Convert_To (Ptyp,
6380 Make_Op_Add (Loc,
6381 Left_Opnd =>
6382 Unchecked_Convert_To (Standard_Integer,
6383 Relocate_Node (First (Exprs))),
6384 Right_Opnd =>
6385 Make_Integer_Literal (Loc, 1))),
6386 Rep_To_Pos_Flag (Ptyp, Loc))))));
6387 else
6388 -- Add Boolean parameter True, to request program errror if
6389 -- we have a bad representation on our hands. Add False if
6390 -- checks are suppressed.
6392 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
6393 Rewrite (N,
6394 Make_Indexed_Component (Loc,
6395 Prefix =>
6396 New_Occurrence_Of
6397 (Enum_Pos_To_Rep (Etyp), Loc),
6398 Expressions => New_List (
6399 Make_Op_Add (Loc,
6400 Left_Opnd =>
6401 Make_Function_Call (Loc,
6402 Name =>
6403 New_Occurrence_Of
6404 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6405 Parameter_Associations => Exprs),
6406 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
6407 end if;
6409 Analyze_And_Resolve (N, Typ);
6411 -- For floating-point, we transform 'Succ into a call to the Succ
6412 -- floating-point attribute function in Fat_xxx (xxx is root type)
6414 elsif Is_Floating_Point_Type (Ptyp) then
6415 Expand_Fpt_Attribute_R (N);
6416 Analyze_And_Resolve (N, Typ);
6418 -- For modular types, nothing to do (no overflow, since wraps)
6420 elsif Is_Modular_Integer_Type (Ptyp) then
6421 null;
6423 -- For other types, if argument is marked as needing a range check or
6424 -- overflow checking is enabled, we must generate a check.
6426 elsif not Overflow_Checks_Suppressed (Ptyp)
6427 or else Do_Range_Check (First (Exprs))
6428 then
6429 Set_Do_Range_Check (First (Exprs), False);
6430 Expand_Pred_Succ_Attribute (N);
6431 end if;
6432 end Succ;
6434 ---------
6435 -- Tag --
6436 ---------
6438 -- Transforms X'Tag into a direct reference to the tag of X
6440 when Attribute_Tag => Tag : declare
6441 Ttyp : Entity_Id;
6442 Prefix_Is_Type : Boolean;
6444 begin
6445 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
6446 Ttyp := Entity (Pref);
6447 Prefix_Is_Type := True;
6448 else
6449 Ttyp := Ptyp;
6450 Prefix_Is_Type := False;
6451 end if;
6453 if Is_Class_Wide_Type (Ttyp) then
6454 Ttyp := Root_Type (Ttyp);
6455 end if;
6457 Ttyp := Underlying_Type (Ttyp);
6459 -- Ada 2005: The type may be a synchronized tagged type, in which
6460 -- case the tag information is stored in the corresponding record.
6462 if Is_Concurrent_Type (Ttyp) then
6463 Ttyp := Corresponding_Record_Type (Ttyp);
6464 end if;
6466 if Prefix_Is_Type then
6468 -- For VMs we leave the type attribute unexpanded because
6469 -- there's not a dispatching table to reference.
6471 if Tagged_Type_Expansion then
6472 Rewrite (N,
6473 Unchecked_Convert_To (RTE (RE_Tag),
6474 New_Occurrence_Of
6475 (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc)));
6476 Analyze_And_Resolve (N, RTE (RE_Tag));
6477 end if;
6479 -- Ada 2005 (AI-251): The use of 'Tag in the sources always
6480 -- references the primary tag of the actual object. If 'Tag is
6481 -- applied to class-wide interface objects we generate code that
6482 -- displaces "this" to reference the base of the object.
6484 elsif Comes_From_Source (N)
6485 and then Is_Class_Wide_Type (Etype (Prefix (N)))
6486 and then Is_Interface (Underlying_Type (Etype (Prefix (N))))
6487 then
6488 -- Generate:
6489 -- (To_Tag_Ptr (Prefix'Address)).all
6491 -- Note that Prefix'Address is recursively expanded into a call
6492 -- to Base_Address (Obj.Tag)
6494 -- Not needed for VM targets, since all handled by the VM
6496 if Tagged_Type_Expansion then
6497 Rewrite (N,
6498 Make_Explicit_Dereference (Loc,
6499 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6500 Make_Attribute_Reference (Loc,
6501 Prefix => Relocate_Node (Pref),
6502 Attribute_Name => Name_Address))));
6503 Analyze_And_Resolve (N, RTE (RE_Tag));
6504 end if;
6506 else
6507 Rewrite (N,
6508 Make_Selected_Component (Loc,
6509 Prefix => Relocate_Node (Pref),
6510 Selector_Name =>
6511 New_Occurrence_Of (First_Tag_Component (Ttyp), Loc)));
6512 Analyze_And_Resolve (N, RTE (RE_Tag));
6513 end if;
6514 end Tag;
6516 ----------------
6517 -- Terminated --
6518 ----------------
6520 -- Transforms 'Terminated attribute into a call to Terminated function
6522 when Attribute_Terminated => Terminated : begin
6524 -- The prefix of Terminated is of a task interface class-wide type.
6525 -- Generate:
6526 -- terminated (Task_Id (_disp_get_task_id (Pref)));
6528 if Ada_Version >= Ada_2005
6529 and then Ekind (Ptyp) = E_Class_Wide_Type
6530 and then Is_Interface (Ptyp)
6531 and then Is_Task_Interface (Ptyp)
6532 then
6533 Rewrite (N,
6534 Make_Function_Call (Loc,
6535 Name =>
6536 New_Occurrence_Of (RTE (RE_Terminated), Loc),
6537 Parameter_Associations => New_List (
6538 Make_Unchecked_Type_Conversion (Loc,
6539 Subtype_Mark =>
6540 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6541 Expression => Build_Disp_Get_Task_Id_Call (Pref)))));
6543 elsif Restricted_Profile then
6544 Rewrite (N,
6545 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
6547 else
6548 Rewrite (N,
6549 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
6550 end if;
6552 Analyze_And_Resolve (N, Standard_Boolean);
6553 end Terminated;
6555 ----------------
6556 -- To_Address --
6557 ----------------
6559 -- Transforms System'To_Address (X) and System.Address'Ref (X) into
6560 -- unchecked conversion from (integral) type of X to type address.
6562 when Attribute_Ref
6563 | Attribute_To_Address
6565 Rewrite (N,
6566 Unchecked_Convert_To (RTE (RE_Address),
6567 Relocate_Node (First (Exprs))));
6568 Analyze_And_Resolve (N, RTE (RE_Address));
6570 ------------
6571 -- To_Any --
6572 ------------
6574 when Attribute_To_Any => To_Any : declare
6575 P_Type : constant Entity_Id := Etype (Pref);
6576 Decls : constant List_Id := New_List;
6577 begin
6578 Rewrite (N,
6579 Build_To_Any_Call
6580 (Loc,
6581 Convert_To (P_Type,
6582 Relocate_Node (First (Exprs))), Decls));
6583 Insert_Actions (N, Decls);
6584 Analyze_And_Resolve (N, RTE (RE_Any));
6585 end To_Any;
6587 ----------------
6588 -- Truncation --
6589 ----------------
6591 -- Transforms 'Truncation into a call to the floating-point attribute
6592 -- function Truncation in Fat_xxx (where xxx is the root type).
6593 -- Expansion is avoided for cases the back end can handle directly.
6595 when Attribute_Truncation =>
6596 if not Is_Inline_Floating_Point_Attribute (N) then
6597 Expand_Fpt_Attribute_R (N);
6598 end if;
6600 --------------
6601 -- TypeCode --
6602 --------------
6604 when Attribute_TypeCode => TypeCode : declare
6605 P_Type : constant Entity_Id := Etype (Pref);
6606 Decls : constant List_Id := New_List;
6607 begin
6608 Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls));
6609 Insert_Actions (N, Decls);
6610 Analyze_And_Resolve (N, RTE (RE_TypeCode));
6611 end TypeCode;
6613 -----------------------
6614 -- Unbiased_Rounding --
6615 -----------------------
6617 -- Transforms 'Unbiased_Rounding into a call to the floating-point
6618 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
6619 -- root type). Expansion is avoided for cases the back end can handle
6620 -- directly.
6622 when Attribute_Unbiased_Rounding =>
6623 if not Is_Inline_Floating_Point_Attribute (N) then
6624 Expand_Fpt_Attribute_R (N);
6625 end if;
6627 ------------
6628 -- Update --
6629 ------------
6631 when Attribute_Update =>
6632 Expand_Update_Attribute (N);
6634 ---------------
6635 -- VADS_Size --
6636 ---------------
6638 -- The processing for VADS_Size is shared with Size
6640 ---------
6641 -- Val --
6642 ---------
6644 -- For enumeration types with a standard representation, and for all
6645 -- other types, Val is handled by the back end. For enumeration types
6646 -- with a non-standard representation we use the _Pos_To_Rep array that
6647 -- was created when the type was frozen.
6649 when Attribute_Val => Val : declare
6650 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
6652 begin
6653 if Is_Enumeration_Type (Etyp)
6654 and then Present (Enum_Pos_To_Rep (Etyp))
6655 then
6656 if Has_Contiguous_Rep (Etyp) then
6657 declare
6658 Rep_Node : constant Node_Id :=
6659 Unchecked_Convert_To (Etyp,
6660 Make_Op_Add (Loc,
6661 Left_Opnd =>
6662 Make_Integer_Literal (Loc,
6663 Enumeration_Rep (First_Literal (Etyp))),
6664 Right_Opnd =>
6665 (Convert_To (Standard_Integer,
6666 Relocate_Node (First (Exprs))))));
6668 begin
6669 Rewrite (N,
6670 Unchecked_Convert_To (Etyp,
6671 Make_Op_Add (Loc,
6672 Left_Opnd =>
6673 Make_Integer_Literal (Loc,
6674 Enumeration_Rep (First_Literal (Etyp))),
6675 Right_Opnd =>
6676 Make_Function_Call (Loc,
6677 Name =>
6678 New_Occurrence_Of
6679 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6680 Parameter_Associations => New_List (
6681 Rep_Node,
6682 Rep_To_Pos_Flag (Etyp, Loc))))));
6683 end;
6685 else
6686 Rewrite (N,
6687 Make_Indexed_Component (Loc,
6688 Prefix => New_Occurrence_Of (Enum_Pos_To_Rep (Etyp), Loc),
6689 Expressions => New_List (
6690 Convert_To (Standard_Integer,
6691 Relocate_Node (First (Exprs))))));
6692 end if;
6694 Analyze_And_Resolve (N, Typ);
6696 -- If the argument is marked as requiring a range check then generate
6697 -- it here.
6699 elsif Do_Range_Check (First (Exprs)) then
6700 Generate_Range_Check (First (Exprs), Etyp, CE_Range_Check_Failed);
6701 end if;
6702 end Val;
6704 -----------
6705 -- Valid --
6706 -----------
6708 -- The code for valid is dependent on the particular types involved.
6709 -- See separate sections below for the generated code in each case.
6711 when Attribute_Valid => Valid : declare
6712 Btyp : Entity_Id := Base_Type (Ptyp);
6714 Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
6715 -- Save the validity checking mode. We always turn off validity
6716 -- checking during process of 'Valid since this is one place
6717 -- where we do not want the implicit validity checks to interfere
6718 -- with the explicit validity check that the programmer is doing.
6720 function Make_Range_Test return Node_Id;
6721 -- Build the code for a range test of the form
6722 -- Btyp!(Pref) in Btyp!(Ptyp'First) .. Btyp!(Ptyp'Last)
6724 ---------------------
6725 -- Make_Range_Test --
6726 ---------------------
6728 function Make_Range_Test return Node_Id is
6729 Temp : Node_Id;
6731 begin
6732 -- The prefix of attribute 'Valid should always denote an object
6733 -- reference. The reference is either coming directly from source
6734 -- or is produced by validity check expansion. The object may be
6735 -- wrapped in a conversion in which case the call to Unqual_Conv
6736 -- will yield it.
6738 -- If the prefix denotes a variable which captures the value of
6739 -- an object for validation purposes, use the variable in the
6740 -- range test. This ensures that no extra copies or extra reads
6741 -- are produced as part of the test. Generate:
6743 -- Temp : ... := Object;
6744 -- if not Temp in ... then
6746 if Is_Validation_Variable_Reference (Pref) then
6747 Temp := New_Occurrence_Of (Entity (Unqual_Conv (Pref)), Loc);
6749 -- Otherwise the prefix is either a source object or a constant
6750 -- produced by validity check expansion. Generate:
6752 -- Temp : constant ... := Pref;
6753 -- if not Temp in ... then
6755 else
6756 Temp := Duplicate_Subexpr (Pref);
6757 end if;
6759 return
6760 Make_In (Loc,
6761 Left_Opnd => Unchecked_Convert_To (Btyp, Temp),
6762 Right_Opnd =>
6763 Make_Range (Loc,
6764 Low_Bound =>
6765 Unchecked_Convert_To (Btyp,
6766 Make_Attribute_Reference (Loc,
6767 Prefix => New_Occurrence_Of (Ptyp, Loc),
6768 Attribute_Name => Name_First)),
6769 High_Bound =>
6770 Unchecked_Convert_To (Btyp,
6771 Make_Attribute_Reference (Loc,
6772 Prefix => New_Occurrence_Of (Ptyp, Loc),
6773 Attribute_Name => Name_Last))));
6774 end Make_Range_Test;
6776 -- Local variables
6778 Tst : Node_Id;
6780 -- Start of processing for Attribute_Valid
6782 begin
6783 -- Do not expand sourced code 'Valid reference in CodePeer mode,
6784 -- will be handled by the back-end directly.
6786 if CodePeer_Mode and then Comes_From_Source (N) then
6787 return;
6788 end if;
6790 -- Turn off validity checks. We do not want any implicit validity
6791 -- checks to intefere with the explicit check from the attribute
6793 Validity_Checks_On := False;
6795 -- Retrieve the base type. Handle the case where the base type is a
6796 -- private enumeration type.
6798 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
6799 Btyp := Full_View (Btyp);
6800 end if;
6802 -- Floating-point case. This case is handled by the Valid attribute
6803 -- code in the floating-point attribute run-time library.
6805 if Is_Floating_Point_Type (Ptyp) then
6806 Float_Valid : declare
6807 Pkg : RE_Id;
6808 Ftp : Entity_Id;
6810 function Get_Fat_Entity (Nam : Name_Id) return Entity_Id;
6811 -- Return entity for Pkg.Nam
6813 --------------------
6814 -- Get_Fat_Entity --
6815 --------------------
6817 function Get_Fat_Entity (Nam : Name_Id) return Entity_Id is
6818 Exp_Name : constant Node_Id :=
6819 Make_Selected_Component (Loc,
6820 Prefix => New_Occurrence_Of (RTE (Pkg), Loc),
6821 Selector_Name => Make_Identifier (Loc, Nam));
6822 begin
6823 Find_Selected_Component (Exp_Name);
6824 return Entity (Exp_Name);
6825 end Get_Fat_Entity;
6827 -- Start of processing for Float_Valid
6829 begin
6830 -- The C and AAMP back-ends handle Valid for fpt types
6832 if Modify_Tree_For_C or else Float_Rep (Btyp) = AAMP then
6833 Analyze_And_Resolve (Pref, Ptyp);
6834 Set_Etype (N, Standard_Boolean);
6835 Set_Analyzed (N);
6837 else
6838 Find_Fat_Info (Ptyp, Ftp, Pkg);
6840 -- If the prefix is a reverse SSO component, or is possibly
6841 -- unaligned, first create a temporary copy that is in
6842 -- native SSO, and properly aligned. Make it Volatile to
6843 -- prevent folding in the back-end. Note that we use an
6844 -- intermediate constrained string type to initialize the
6845 -- temporary, as the value at hand might be invalid, and in
6846 -- that case it cannot be copied using a floating point
6847 -- register.
6849 if In_Reverse_Storage_Order_Object (Pref)
6850 or else Is_Possibly_Unaligned_Object (Pref)
6851 then
6852 declare
6853 Temp : constant Entity_Id :=
6854 Make_Temporary (Loc, 'F');
6856 Fat_S : constant Entity_Id :=
6857 Get_Fat_Entity (Name_S);
6858 -- Constrained string subtype of appropriate size
6860 Fat_P : constant Entity_Id :=
6861 Get_Fat_Entity (Name_P);
6862 -- Access to Fat_S
6864 Decl : constant Node_Id :=
6865 Make_Object_Declaration (Loc,
6866 Defining_Identifier => Temp,
6867 Aliased_Present => True,
6868 Object_Definition =>
6869 New_Occurrence_Of (Ptyp, Loc));
6871 begin
6872 Set_Aspect_Specifications (Decl, New_List (
6873 Make_Aspect_Specification (Loc,
6874 Identifier =>
6875 Make_Identifier (Loc, Name_Volatile))));
6877 Insert_Actions (N,
6878 New_List (
6879 Decl,
6881 Make_Assignment_Statement (Loc,
6882 Name =>
6883 Make_Explicit_Dereference (Loc,
6884 Prefix =>
6885 Unchecked_Convert_To (Fat_P,
6886 Make_Attribute_Reference (Loc,
6887 Prefix =>
6888 New_Occurrence_Of (Temp, Loc),
6889 Attribute_Name =>
6890 Name_Unrestricted_Access))),
6891 Expression =>
6892 Unchecked_Convert_To (Fat_S,
6893 Relocate_Node (Pref)))),
6895 Suppress => All_Checks);
6897 Rewrite (Pref, New_Occurrence_Of (Temp, Loc));
6898 end;
6899 end if;
6901 -- We now have an object of the proper endianness and
6902 -- alignment, and can construct a Valid attribute.
6904 -- We make sure the prefix of this valid attribute is
6905 -- marked as not coming from source, to avoid losing
6906 -- warnings from 'Valid looking like a possible update.
6908 Set_Comes_From_Source (Pref, False);
6910 Expand_Fpt_Attribute
6911 (N, Pkg, Name_Valid,
6912 New_List (
6913 Make_Attribute_Reference (Loc,
6914 Prefix => Unchecked_Convert_To (Ftp, Pref),
6915 Attribute_Name => Name_Unrestricted_Access)));
6916 end if;
6918 -- One more task, we still need a range check. Required
6919 -- only if we have a constraint, since the Valid routine
6920 -- catches infinities properly (infinities are never valid).
6922 -- The way we do the range check is simply to create the
6923 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
6925 if not Subtypes_Statically_Match (Ptyp, Btyp) then
6926 Rewrite (N,
6927 Make_And_Then (Loc,
6928 Left_Opnd => Relocate_Node (N),
6929 Right_Opnd =>
6930 Make_In (Loc,
6931 Left_Opnd => Convert_To (Btyp, Pref),
6932 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
6933 end if;
6934 end Float_Valid;
6936 -- Enumeration type with holes
6938 -- For enumeration types with holes, the Pos value constructed by
6939 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
6940 -- second argument of False returns minus one for an invalid value,
6941 -- and the non-negative pos value for a valid value, so the
6942 -- expansion of X'Valid is simply:
6944 -- type(X)'Pos (X) >= 0
6946 -- We can't quite generate it that way because of the requirement
6947 -- for the non-standard second argument of False in the resulting
6948 -- rep_to_pos call, so we have to explicitly create:
6950 -- _rep_to_pos (X, False) >= 0
6952 -- If we have an enumeration subtype, we also check that the
6953 -- value is in range:
6955 -- _rep_to_pos (X, False) >= 0
6956 -- and then
6957 -- (X >= type(X)'First and then type(X)'Last <= X)
6959 elsif Is_Enumeration_Type (Ptyp)
6960 and then Present (Enum_Pos_To_Rep (Btyp))
6961 then
6962 Tst :=
6963 Make_Op_Ge (Loc,
6964 Left_Opnd =>
6965 Make_Function_Call (Loc,
6966 Name =>
6967 New_Occurrence_Of (TSS (Btyp, TSS_Rep_To_Pos), Loc),
6968 Parameter_Associations => New_List (
6969 Pref,
6970 New_Occurrence_Of (Standard_False, Loc))),
6971 Right_Opnd => Make_Integer_Literal (Loc, 0));
6973 if Ptyp /= Btyp
6974 and then
6975 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
6976 or else
6977 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
6978 then
6979 -- The call to Make_Range_Test will create declarations
6980 -- that need a proper insertion point, but Pref is now
6981 -- attached to a node with no ancestor. Attach to tree
6982 -- even if it is to be rewritten below.
6984 Set_Parent (Tst, Parent (N));
6986 Tst :=
6987 Make_And_Then (Loc,
6988 Left_Opnd => Make_Range_Test,
6989 Right_Opnd => Tst);
6990 end if;
6992 Rewrite (N, Tst);
6994 -- Fortran convention booleans
6996 -- For the very special case of Fortran convention booleans, the
6997 -- value is always valid, since it is an integer with the semantics
6998 -- that non-zero is true, and any value is permissible.
7000 elsif Is_Boolean_Type (Ptyp)
7001 and then Convention (Ptyp) = Convention_Fortran
7002 then
7003 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
7005 -- For biased representations, we will be doing an unchecked
7006 -- conversion without unbiasing the result. That means that the range
7007 -- test has to take this into account, and the proper form of the
7008 -- test is:
7010 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
7012 elsif Has_Biased_Representation (Ptyp) then
7013 Btyp := RTE (RE_Unsigned_32);
7014 Rewrite (N,
7015 Make_Op_Lt (Loc,
7016 Left_Opnd =>
7017 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
7018 Right_Opnd =>
7019 Unchecked_Convert_To (Btyp,
7020 Make_Attribute_Reference (Loc,
7021 Prefix => New_Occurrence_Of (Ptyp, Loc),
7022 Attribute_Name => Name_Range_Length))));
7024 -- For all other scalar types, what we want logically is a
7025 -- range test:
7027 -- X in type(X)'First .. type(X)'Last
7029 -- But that's precisely what won't work because of possible
7030 -- unwanted optimization (and indeed the basic motivation for
7031 -- the Valid attribute is exactly that this test does not work).
7032 -- What will work is:
7034 -- Btyp!(X) >= Btyp!(type(X)'First)
7035 -- and then
7036 -- Btyp!(X) <= Btyp!(type(X)'Last)
7038 -- where Btyp is an integer type large enough to cover the full
7039 -- range of possible stored values (i.e. it is chosen on the basis
7040 -- of the size of the type, not the range of the values). We write
7041 -- this as two tests, rather than a range check, so that static
7042 -- evaluation will easily remove either or both of the checks if
7043 -- they can be -statically determined to be true (this happens
7044 -- when the type of X is static and the range extends to the full
7045 -- range of stored values).
7047 -- Unsigned types. Note: it is safe to consider only whether the
7048 -- subtype is unsigned, since we will in that case be doing all
7049 -- unsigned comparisons based on the subtype range. Since we use the
7050 -- actual subtype object size, this is appropriate.
7052 -- For example, if we have
7054 -- subtype x is integer range 1 .. 200;
7055 -- for x'Object_Size use 8;
7057 -- Now the base type is signed, but objects of this type are bits
7058 -- unsigned, and doing an unsigned test of the range 1 to 200 is
7059 -- correct, even though a value greater than 127 looks signed to a
7060 -- signed comparison.
7062 elsif Is_Unsigned_Type (Ptyp) then
7063 if Esize (Ptyp) <= 32 then
7064 Btyp := RTE (RE_Unsigned_32);
7065 else
7066 Btyp := RTE (RE_Unsigned_64);
7067 end if;
7069 Rewrite (N, Make_Range_Test);
7071 -- Signed types
7073 else
7074 if Esize (Ptyp) <= Esize (Standard_Integer) then
7075 Btyp := Standard_Integer;
7076 else
7077 Btyp := Universal_Integer;
7078 end if;
7080 Rewrite (N, Make_Range_Test);
7081 end if;
7083 -- If a predicate is present, then we do the predicate test, even if
7084 -- within the predicate function (infinite recursion is warned about
7085 -- in Sem_Attr in that case).
7087 declare
7088 Pred_Func : constant Entity_Id := Predicate_Function (Ptyp);
7090 begin
7091 if Present (Pred_Func) then
7092 Rewrite (N,
7093 Make_And_Then (Loc,
7094 Left_Opnd => Relocate_Node (N),
7095 Right_Opnd => Make_Predicate_Call (Ptyp, Pref)));
7096 end if;
7097 end;
7099 Analyze_And_Resolve (N, Standard_Boolean);
7100 Validity_Checks_On := Save_Validity_Checks_On;
7101 end Valid;
7103 -------------------
7104 -- Valid_Scalars --
7105 -------------------
7107 when Attribute_Valid_Scalars => Valid_Scalars : declare
7108 Val_Typ : constant Entity_Id := Validated_View (Ptyp);
7109 Comp_Typ : Entity_Id;
7110 Expr : Node_Id;
7112 begin
7113 -- Assume that the prefix does not need validation
7115 Expr := Empty;
7117 -- Attribute 'Valid_Scalars is not supported on private tagged types
7119 if Is_Private_Type (Ptyp) and then Is_Tagged_Type (Ptyp) then
7120 null;
7122 -- Attribute 'Valid_Scalars evaluates to True when the type lacks
7123 -- scalars.
7125 elsif not Scalar_Part_Present (Val_Typ) then
7126 null;
7128 -- Attribute 'Valid_Scalars is the same as attribute 'Valid when the
7129 -- validated type is a scalar type. Generate:
7131 -- Val_Typ (Pref)'Valid
7133 elsif Is_Scalar_Type (Val_Typ) then
7134 Expr :=
7135 Make_Attribute_Reference (Loc,
7136 Prefix =>
7137 Unchecked_Convert_To (Val_Typ, New_Copy_Tree (Pref)),
7138 Attribute_Name => Name_Valid);
7140 -- Validate the scalar components of an array by iterating over all
7141 -- dimensions of the array while checking individual components.
7143 elsif Is_Array_Type (Val_Typ) then
7144 Comp_Typ := Validated_View (Component_Type (Val_Typ));
7146 if Scalar_Part_Present (Comp_Typ) then
7147 Expr :=
7148 Make_Function_Call (Loc,
7149 Name =>
7150 New_Occurrence_Of
7151 (Build_Array_VS_Func
7152 (Attr => N,
7153 Formal_Typ => Ptyp,
7154 Array_Typ => Val_Typ,
7155 Comp_Typ => Comp_Typ),
7156 Loc),
7157 Parameter_Associations => New_List (Pref));
7158 end if;
7160 -- Validate the scalar components, discriminants of a record type by
7161 -- examining the structure of a record type.
7163 elsif Is_Record_Type (Val_Typ) then
7164 Expr :=
7165 Make_Function_Call (Loc,
7166 Name =>
7167 New_Occurrence_Of
7168 (Build_Record_VS_Func
7169 (Attr => N,
7170 Formal_Typ => Ptyp,
7171 Rec_Typ => Val_Typ),
7172 Loc),
7173 Parameter_Associations => New_List (Pref));
7174 end if;
7176 -- Default the attribute to True when the type of the prefix does not
7177 -- need validation.
7179 if No (Expr) then
7180 Expr := New_Occurrence_Of (Standard_True, Loc);
7181 end if;
7183 Rewrite (N, Expr);
7184 Analyze_And_Resolve (N, Standard_Boolean);
7185 Set_Is_Static_Expression (N, False);
7186 end Valid_Scalars;
7188 -----------
7189 -- Value --
7190 -----------
7192 -- Value attribute is handled in separate unit Exp_Imgv
7194 when Attribute_Value =>
7195 Exp_Imgv.Expand_Value_Attribute (N);
7197 -----------------
7198 -- Value_Size --
7199 -----------------
7201 -- The processing for Value_Size shares the processing for Size
7203 -------------
7204 -- Version --
7205 -------------
7207 -- The processing for Version shares the processing for Body_Version
7209 ----------------
7210 -- Wide_Image --
7211 ----------------
7213 -- Wide_Image attribute is handled in separate unit Exp_Imgv
7215 when Attribute_Wide_Image =>
7216 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
7217 -- back-end knows how to handle this attribute directly.
7219 if CodePeer_Mode then
7220 return;
7221 end if;
7223 Exp_Imgv.Expand_Wide_Image_Attribute (N);
7225 ---------------------
7226 -- Wide_Wide_Image --
7227 ---------------------
7229 -- Wide_Wide_Image attribute is handled in separate unit Exp_Imgv
7231 when Attribute_Wide_Wide_Image =>
7232 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
7233 -- back-end knows how to handle this attribute directly.
7235 if CodePeer_Mode then
7236 return;
7237 end if;
7239 Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N);
7241 ----------------
7242 -- Wide_Value --
7243 ----------------
7245 -- We expand typ'Wide_Value (X) into
7247 -- typ'Value
7248 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
7250 -- Wide_String_To_String is a runtime function that converts its wide
7251 -- string argument to String, converting any non-translatable characters
7252 -- into appropriate escape sequences. This preserves the required
7253 -- semantics of Wide_Value in all cases, and results in a very simple
7254 -- implementation approach.
7256 -- Note: for this approach to be fully standard compliant for the cases
7257 -- where typ is Wide_Character and Wide_Wide_Character, the encoding
7258 -- method must cover the entire character range (e.g. UTF-8). But that
7259 -- is a reasonable requirement when dealing with encoded character
7260 -- sequences. Presumably if one of the restrictive encoding mechanisms
7261 -- is in use such as Shift-JIS, then characters that cannot be
7262 -- represented using this encoding will not appear in any case.
7264 when Attribute_Wide_Value =>
7265 Rewrite (N,
7266 Make_Attribute_Reference (Loc,
7267 Prefix => Pref,
7268 Attribute_Name => Name_Value,
7270 Expressions => New_List (
7271 Make_Function_Call (Loc,
7272 Name =>
7273 New_Occurrence_Of (RTE (RE_Wide_String_To_String), Loc),
7275 Parameter_Associations => New_List (
7276 Relocate_Node (First (Exprs)),
7277 Make_Integer_Literal (Loc,
7278 Intval => Int (Wide_Character_Encoding_Method)))))));
7280 Analyze_And_Resolve (N, Typ);
7282 ---------------------
7283 -- Wide_Wide_Value --
7284 ---------------------
7286 -- We expand typ'Wide_Value_Value (X) into
7288 -- typ'Value
7289 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
7291 -- Wide_Wide_String_To_String is a runtime function that converts its
7292 -- wide string argument to String, converting any non-translatable
7293 -- characters into appropriate escape sequences. This preserves the
7294 -- required semantics of Wide_Wide_Value in all cases, and results in a
7295 -- very simple implementation approach.
7297 -- It's not quite right where typ = Wide_Wide_Character, because the
7298 -- encoding method may not cover the whole character type ???
7300 when Attribute_Wide_Wide_Value =>
7301 Rewrite (N,
7302 Make_Attribute_Reference (Loc,
7303 Prefix => Pref,
7304 Attribute_Name => Name_Value,
7306 Expressions => New_List (
7307 Make_Function_Call (Loc,
7308 Name =>
7309 New_Occurrence_Of
7310 (RTE (RE_Wide_Wide_String_To_String), Loc),
7312 Parameter_Associations => New_List (
7313 Relocate_Node (First (Exprs)),
7314 Make_Integer_Literal (Loc,
7315 Intval => Int (Wide_Character_Encoding_Method)))))));
7317 Analyze_And_Resolve (N, Typ);
7319 ---------------------
7320 -- Wide_Wide_Width --
7321 ---------------------
7323 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
7325 when Attribute_Wide_Wide_Width =>
7326 Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide);
7328 ----------------
7329 -- Wide_Width --
7330 ----------------
7332 -- Wide_Width attribute is handled in separate unit Exp_Imgv
7334 when Attribute_Wide_Width =>
7335 Exp_Imgv.Expand_Width_Attribute (N, Wide);
7337 -----------
7338 -- Width --
7339 -----------
7341 -- Width attribute is handled in separate unit Exp_Imgv
7343 when Attribute_Width =>
7344 Exp_Imgv.Expand_Width_Attribute (N, Normal);
7346 -----------
7347 -- Write --
7348 -----------
7350 when Attribute_Write => Write : declare
7351 P_Type : constant Entity_Id := Entity (Pref);
7352 U_Type : constant Entity_Id := Underlying_Type (P_Type);
7353 Pname : Entity_Id;
7354 Decl : Node_Id;
7355 Prag : Node_Id;
7356 Arg3 : Node_Id;
7357 Wfunc : Node_Id;
7359 begin
7360 -- If no underlying type, we have an error that will be diagnosed
7361 -- elsewhere, so here we just completely ignore the expansion.
7363 if No (U_Type) then
7364 return;
7365 end if;
7367 -- Stream operations can appear in user code even if the restriction
7368 -- No_Streams is active (for example, when instantiating a predefined
7369 -- container). In that case rewrite the attribute as a Raise to
7370 -- prevent any run-time use.
7372 if Restriction_Active (No_Streams) then
7373 Rewrite (N,
7374 Make_Raise_Program_Error (Sloc (N),
7375 Reason => PE_Stream_Operation_Not_Allowed));
7376 Set_Etype (N, U_Type);
7377 return;
7378 end if;
7380 -- The simple case, if there is a TSS for Write, just call it
7382 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
7384 if Present (Pname) then
7385 null;
7387 else
7388 -- If there is a Stream_Convert pragma, use it, we rewrite
7390 -- sourcetyp'Output (stream, Item)
7392 -- as
7394 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
7396 -- where strmwrite is the given Write function that converts an
7397 -- argument of type sourcetyp or a type acctyp, from which it is
7398 -- derived to type strmtyp. The conversion to acttyp is required
7399 -- for the derived case.
7401 Prag := Get_Stream_Convert_Pragma (P_Type);
7403 if Present (Prag) then
7404 Arg3 :=
7405 Next (Next (First (Pragma_Argument_Associations (Prag))));
7406 Wfunc := Entity (Expression (Arg3));
7408 Rewrite (N,
7409 Make_Attribute_Reference (Loc,
7410 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
7411 Attribute_Name => Name_Output,
7412 Expressions => New_List (
7413 Relocate_Node (First (Exprs)),
7414 Make_Function_Call (Loc,
7415 Name => New_Occurrence_Of (Wfunc, Loc),
7416 Parameter_Associations => New_List (
7417 OK_Convert_To (Etype (First_Formal (Wfunc)),
7418 Relocate_Node (Next (First (Exprs)))))))));
7420 Analyze (N);
7421 return;
7423 -- For elementary types, we call the W_xxx routine directly
7425 elsif Is_Elementary_Type (U_Type) then
7426 Rewrite (N, Build_Elementary_Write_Call (N));
7427 Analyze (N);
7428 return;
7430 -- Array type case
7432 elsif Is_Array_Type (U_Type) then
7433 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
7434 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
7436 -- Tagged type case, use the primitive Write function. Note that
7437 -- this will dispatch in the class-wide case which is what we want
7439 elsif Is_Tagged_Type (U_Type) then
7440 Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
7442 -- All other record type cases, including protected records.
7443 -- The latter only arise for expander generated code for
7444 -- handling shared passive partition access.
7446 else
7447 pragma Assert
7448 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
7450 -- Ada 2005 (AI-216): Program_Error is raised when executing
7451 -- the default implementation of the Write attribute of an
7452 -- Unchecked_Union type. However, if the 'Write reference is
7453 -- within the generated Output stream procedure, Write outputs
7454 -- the components, and the default values of the discriminant
7455 -- are streamed by the Output procedure itself. If there are
7456 -- no default values this is also erroneous.
7458 if Is_Unchecked_Union (Base_Type (U_Type)) then
7459 if (not Is_TSS (Current_Scope, TSS_Stream_Output)
7460 and not Is_TSS (Current_Scope, TSS_Stream_Write))
7461 or else No (Discriminant_Default_Value
7462 (First_Discriminant (U_Type)))
7463 then
7464 Rewrite (N,
7465 Make_Raise_Program_Error (Loc,
7466 Reason => PE_Unchecked_Union_Restriction));
7467 Set_Etype (N, U_Type);
7468 return;
7469 end if;
7470 end if;
7472 if Has_Discriminants (U_Type)
7473 and then Present
7474 (Discriminant_Default_Value (First_Discriminant (U_Type)))
7475 then
7476 Build_Mutable_Record_Write_Procedure
7477 (Loc, Full_Base (U_Type), Decl, Pname);
7478 else
7479 Build_Record_Write_Procedure
7480 (Loc, Full_Base (U_Type), Decl, Pname);
7481 end if;
7483 Insert_Action (N, Decl);
7484 end if;
7485 end if;
7487 -- If we fall through, Pname is the procedure to be called
7489 Rewrite_Stream_Proc_Call (Pname);
7490 end Write;
7492 -- Component_Size is handled by the back end, unless the component size
7493 -- is known at compile time, which is always true in the packed array
7494 -- case. It is important that the packed array case is handled in the
7495 -- front end (see Eval_Attribute) since the back end would otherwise get
7496 -- confused by the equivalent packed array type.
7498 when Attribute_Component_Size =>
7499 null;
7501 -- The following attributes are handled by the back end (except that
7502 -- static cases have already been evaluated during semantic processing,
7503 -- but in any case the back end should not count on this).
7505 -- The back end also handles the non-class-wide cases of Size
7507 when Attribute_Bit_Order
7508 | Attribute_Code_Address
7509 | Attribute_Definite
7510 | Attribute_Deref
7511 | Attribute_Null_Parameter
7512 | Attribute_Passed_By_Reference
7513 | Attribute_Pool_Address
7514 | Attribute_Scalar_Storage_Order
7516 null;
7518 -- The following attributes are also handled by the back end, but return
7519 -- a universal integer result, so may need a conversion for checking
7520 -- that the result is in range.
7522 when Attribute_Aft
7523 | Attribute_Max_Alignment_For_Allocation
7525 Apply_Universal_Integer_Attribute_Checks (N);
7527 -- The following attributes should not appear at this stage, since they
7528 -- have already been handled by the analyzer (and properly rewritten
7529 -- with corresponding values or entities to represent the right values)
7531 when Attribute_Abort_Signal
7532 | Attribute_Address_Size
7533 | Attribute_Atomic_Always_Lock_Free
7534 | Attribute_Base
7535 | Attribute_Class
7536 | Attribute_Compiler_Version
7537 | Attribute_Default_Bit_Order
7538 | Attribute_Default_Scalar_Storage_Order
7539 | Attribute_Delta
7540 | Attribute_Denorm
7541 | Attribute_Digits
7542 | Attribute_Emax
7543 | Attribute_Enabled
7544 | Attribute_Epsilon
7545 | Attribute_Fast_Math
7546 | Attribute_First_Valid
7547 | Attribute_Has_Access_Values
7548 | Attribute_Has_Discriminants
7549 | Attribute_Has_Tagged_Values
7550 | Attribute_Large
7551 | Attribute_Last_Valid
7552 | Attribute_Library_Level
7553 | Attribute_Lock_Free
7554 | Attribute_Machine_Emax
7555 | Attribute_Machine_Emin
7556 | Attribute_Machine_Mantissa
7557 | Attribute_Machine_Overflows
7558 | Attribute_Machine_Radix
7559 | Attribute_Machine_Rounds
7560 | Attribute_Maximum_Alignment
7561 | Attribute_Model_Emin
7562 | Attribute_Model_Epsilon
7563 | Attribute_Model_Mantissa
7564 | Attribute_Model_Small
7565 | Attribute_Modulus
7566 | Attribute_Partition_ID
7567 | Attribute_Range
7568 | Attribute_Restriction_Set
7569 | Attribute_Safe_Emax
7570 | Attribute_Safe_First
7571 | Attribute_Safe_Large
7572 | Attribute_Safe_Last
7573 | Attribute_Safe_Small
7574 | Attribute_Scale
7575 | Attribute_Signed_Zeros
7576 | Attribute_Small
7577 | Attribute_Storage_Unit
7578 | Attribute_Stub_Type
7579 | Attribute_System_Allocator_Alignment
7580 | Attribute_Target_Name
7581 | Attribute_Type_Class
7582 | Attribute_Type_Key
7583 | Attribute_Unconstrained_Array
7584 | Attribute_Universal_Literal_String
7585 | Attribute_Wchar_T_Size
7586 | Attribute_Word_Size
7588 raise Program_Error;
7590 -- The Asm_Input and Asm_Output attributes are not expanded at this
7591 -- stage, but will be eliminated in the expansion of the Asm call, see
7592 -- Exp_Intr for details. So the back end will never see these either.
7594 when Attribute_Asm_Input
7595 | Attribute_Asm_Output
7597 null;
7598 end case;
7600 -- Note: as mentioned earlier, individual sections of the above case
7601 -- statement assume there is no code after the case statement, and are
7602 -- legitimately allowed to execute return statements if they have nothing
7603 -- more to do, so DO NOT add code at this point.
7605 exception
7606 when RE_Not_Available =>
7607 return;
7608 end Expand_N_Attribute_Reference;
7610 --------------------------------
7611 -- Expand_Pred_Succ_Attribute --
7612 --------------------------------
7614 -- For typ'Pred (exp), we generate the check
7616 -- [constraint_error when exp = typ'Base'First]
7618 -- Similarly, for typ'Succ (exp), we generate the check
7620 -- [constraint_error when exp = typ'Base'Last]
7622 -- These checks are not generated for modular types, since the proper
7623 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
7624 -- We also suppress these checks if we are the right side of an assignment
7625 -- statement or the expression of an object declaration, where the flag
7626 -- Suppress_Assignment_Checks is set for the assignment/declaration.
7628 procedure Expand_Pred_Succ_Attribute (N : Node_Id) is
7629 Loc : constant Source_Ptr := Sloc (N);
7630 P : constant Node_Id := Parent (N);
7631 Cnam : Name_Id;
7633 begin
7634 if Attribute_Name (N) = Name_Pred then
7635 Cnam := Name_First;
7636 else
7637 Cnam := Name_Last;
7638 end if;
7640 if not Nkind_In (P, N_Assignment_Statement, N_Object_Declaration)
7641 or else not Suppress_Assignment_Checks (P)
7642 then
7643 Insert_Action (N,
7644 Make_Raise_Constraint_Error (Loc,
7645 Condition =>
7646 Make_Op_Eq (Loc,
7647 Left_Opnd =>
7648 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
7649 Right_Opnd =>
7650 Make_Attribute_Reference (Loc,
7651 Prefix =>
7652 New_Occurrence_Of (Base_Type (Etype (Prefix (N))), Loc),
7653 Attribute_Name => Cnam)),
7654 Reason => CE_Overflow_Check_Failed));
7655 end if;
7656 end Expand_Pred_Succ_Attribute;
7658 -----------------------------
7659 -- Expand_Update_Attribute --
7660 -----------------------------
7662 procedure Expand_Update_Attribute (N : Node_Id) is
7663 procedure Process_Component_Or_Element_Update
7664 (Temp : Entity_Id;
7665 Comp : Node_Id;
7666 Expr : Node_Id;
7667 Typ : Entity_Id);
7668 -- Generate the statements necessary to update a single component or an
7669 -- element of the prefix. The code is inserted before the attribute N.
7670 -- Temp denotes the entity of the anonymous object created to reflect
7671 -- the changes in values. Comp is the component/index expression to be
7672 -- updated. Expr is an expression yielding the new value of Comp. Typ
7673 -- is the type of the prefix of attribute Update.
7675 procedure Process_Range_Update
7676 (Temp : Entity_Id;
7677 Comp : Node_Id;
7678 Expr : Node_Id;
7679 Typ : Entity_Id);
7680 -- Generate the statements necessary to update a slice of the prefix.
7681 -- The code is inserted before the attribute N. Temp denotes the entity
7682 -- of the anonymous object created to reflect the changes in values.
7683 -- Comp is range of the slice to be updated. Expr is an expression
7684 -- yielding the new value of Comp. Typ is the type of the prefix of
7685 -- attribute Update.
7687 -----------------------------------------
7688 -- Process_Component_Or_Element_Update --
7689 -----------------------------------------
7691 procedure Process_Component_Or_Element_Update
7692 (Temp : Entity_Id;
7693 Comp : Node_Id;
7694 Expr : Node_Id;
7695 Typ : Entity_Id)
7697 Loc : constant Source_Ptr := Sloc (Comp);
7698 Exprs : List_Id;
7699 LHS : Node_Id;
7701 begin
7702 -- An array element may be modified by the following relations
7703 -- depending on the number of dimensions:
7705 -- 1 => Expr -- one dimensional update
7706 -- (1, ..., N) => Expr -- multi dimensional update
7708 -- The above forms are converted in assignment statements where the
7709 -- left hand side is an indexed component:
7711 -- Temp (1) := Expr; -- one dimensional update
7712 -- Temp (1, ..., N) := Expr; -- multi dimensional update
7714 if Is_Array_Type (Typ) then
7716 -- The index expressions of a multi dimensional array update
7717 -- appear as an aggregate.
7719 if Nkind (Comp) = N_Aggregate then
7720 Exprs := New_Copy_List_Tree (Expressions (Comp));
7721 else
7722 Exprs := New_List (Relocate_Node (Comp));
7723 end if;
7725 LHS :=
7726 Make_Indexed_Component (Loc,
7727 Prefix => New_Occurrence_Of (Temp, Loc),
7728 Expressions => Exprs);
7730 -- A record component update appears in the following form:
7732 -- Comp => Expr
7734 -- The above relation is transformed into an assignment statement
7735 -- where the left hand side is a selected component:
7737 -- Temp.Comp := Expr;
7739 else pragma Assert (Is_Record_Type (Typ));
7740 LHS :=
7741 Make_Selected_Component (Loc,
7742 Prefix => New_Occurrence_Of (Temp, Loc),
7743 Selector_Name => Relocate_Node (Comp));
7744 end if;
7746 Insert_Action (N,
7747 Make_Assignment_Statement (Loc,
7748 Name => LHS,
7749 Expression => Relocate_Node (Expr)));
7750 end Process_Component_Or_Element_Update;
7752 --------------------------
7753 -- Process_Range_Update --
7754 --------------------------
7756 procedure Process_Range_Update
7757 (Temp : Entity_Id;
7758 Comp : Node_Id;
7759 Expr : Node_Id;
7760 Typ : Entity_Id)
7762 Index_Typ : constant Entity_Id := Etype (First_Index (Typ));
7763 Loc : constant Source_Ptr := Sloc (Comp);
7764 Index : Entity_Id;
7766 begin
7767 -- A range update appears as
7769 -- (Low .. High => Expr)
7771 -- The above construct is transformed into a loop that iterates over
7772 -- the given range and modifies the corresponding array values to the
7773 -- value of Expr:
7775 -- for Index in Low .. High loop
7776 -- Temp (<Index_Typ> (Index)) := Expr;
7777 -- end loop;
7779 Index := Make_Temporary (Loc, 'I');
7781 Insert_Action (N,
7782 Make_Loop_Statement (Loc,
7783 Iteration_Scheme =>
7784 Make_Iteration_Scheme (Loc,
7785 Loop_Parameter_Specification =>
7786 Make_Loop_Parameter_Specification (Loc,
7787 Defining_Identifier => Index,
7788 Discrete_Subtype_Definition => Relocate_Node (Comp))),
7790 Statements => New_List (
7791 Make_Assignment_Statement (Loc,
7792 Name =>
7793 Make_Indexed_Component (Loc,
7794 Prefix => New_Occurrence_Of (Temp, Loc),
7795 Expressions => New_List (
7796 Convert_To (Index_Typ,
7797 New_Occurrence_Of (Index, Loc)))),
7798 Expression => Relocate_Node (Expr))),
7800 End_Label => Empty));
7801 end Process_Range_Update;
7803 -- Local variables
7805 Aggr : constant Node_Id := First (Expressions (N));
7806 Loc : constant Source_Ptr := Sloc (N);
7807 Pref : constant Node_Id := Prefix (N);
7808 Typ : constant Entity_Id := Etype (Pref);
7809 Assoc : Node_Id;
7810 Comp : Node_Id;
7811 CW_Temp : Entity_Id;
7812 CW_Typ : Entity_Id;
7813 Expr : Node_Id;
7814 Temp : Entity_Id;
7816 -- Start of processing for Expand_Update_Attribute
7818 begin
7819 -- Create the anonymous object to store the value of the prefix and
7820 -- capture subsequent changes in value.
7822 Temp := Make_Temporary (Loc, 'T', Pref);
7824 -- Preserve the tag of the prefix by offering a specific view of the
7825 -- class-wide version of the prefix.
7827 if Is_Tagged_Type (Typ) then
7829 -- Generate:
7830 -- CW_Temp : Typ'Class := Typ'Class (Pref);
7832 CW_Temp := Make_Temporary (Loc, 'T');
7833 CW_Typ := Class_Wide_Type (Typ);
7835 Insert_Action (N,
7836 Make_Object_Declaration (Loc,
7837 Defining_Identifier => CW_Temp,
7838 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
7839 Expression =>
7840 Convert_To (CW_Typ, Relocate_Node (Pref))));
7842 -- Generate:
7843 -- Temp : Typ renames Typ (CW_Temp);
7845 Insert_Action (N,
7846 Make_Object_Renaming_Declaration (Loc,
7847 Defining_Identifier => Temp,
7848 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
7849 Name =>
7850 Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc))));
7852 -- Non-tagged case
7854 else
7855 -- Generate:
7856 -- Temp : Typ := Pref;
7858 Insert_Action (N,
7859 Make_Object_Declaration (Loc,
7860 Defining_Identifier => Temp,
7861 Object_Definition => New_Occurrence_Of (Typ, Loc),
7862 Expression => Relocate_Node (Pref)));
7863 end if;
7865 -- Process the update aggregate
7867 Assoc := First (Component_Associations (Aggr));
7868 while Present (Assoc) loop
7869 Comp := First (Choices (Assoc));
7870 Expr := Expression (Assoc);
7871 while Present (Comp) loop
7872 if Nkind (Comp) = N_Range then
7873 Process_Range_Update (Temp, Comp, Expr, Typ);
7874 else
7875 Process_Component_Or_Element_Update (Temp, Comp, Expr, Typ);
7876 end if;
7878 Next (Comp);
7879 end loop;
7881 Next (Assoc);
7882 end loop;
7884 -- The attribute is replaced by a reference to the anonymous object
7886 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7887 Analyze (N);
7888 end Expand_Update_Attribute;
7890 -------------------
7891 -- Find_Fat_Info --
7892 -------------------
7894 procedure Find_Fat_Info
7895 (T : Entity_Id;
7896 Fat_Type : out Entity_Id;
7897 Fat_Pkg : out RE_Id)
7899 Rtyp : constant Entity_Id := Root_Type (T);
7901 begin
7902 -- All we do is use the root type (historically this dealt with
7903 -- VAX-float .. to be cleaned up further later ???)
7905 Fat_Type := Rtyp;
7907 if Fat_Type = Standard_Short_Float then
7908 Fat_Pkg := RE_Attr_Short_Float;
7910 elsif Fat_Type = Standard_Float then
7911 Fat_Pkg := RE_Attr_Float;
7913 elsif Fat_Type = Standard_Long_Float then
7914 Fat_Pkg := RE_Attr_Long_Float;
7916 elsif Fat_Type = Standard_Long_Long_Float then
7917 Fat_Pkg := RE_Attr_Long_Long_Float;
7919 -- Universal real (which is its own root type) is treated as being
7920 -- equivalent to Standard.Long_Long_Float, since it is defined to
7921 -- have the same precision as the longest Float type.
7923 elsif Fat_Type = Universal_Real then
7924 Fat_Type := Standard_Long_Long_Float;
7925 Fat_Pkg := RE_Attr_Long_Long_Float;
7927 else
7928 raise Program_Error;
7929 end if;
7930 end Find_Fat_Info;
7932 ----------------------------
7933 -- Find_Stream_Subprogram --
7934 ----------------------------
7936 function Find_Stream_Subprogram
7937 (Typ : Entity_Id;
7938 Nam : TSS_Name_Type) return Entity_Id
7940 Base_Typ : constant Entity_Id := Base_Type (Typ);
7941 Ent : constant Entity_Id := TSS (Typ, Nam);
7943 function Is_Available (Entity : RE_Id) return Boolean;
7944 pragma Inline (Is_Available);
7945 -- Function to check whether the specified run-time call is available
7946 -- in the run time used. In the case of a configurable run time, it
7947 -- is normal that some subprograms are not there.
7949 -- I don't understand this routine at all, why is this not just a
7950 -- call to RTE_Available? And if for some reason we need a different
7951 -- routine with different semantics, why is not in Rtsfind ???
7953 ------------------
7954 -- Is_Available --
7955 ------------------
7957 function Is_Available (Entity : RE_Id) return Boolean is
7958 begin
7959 -- Assume that the unit will always be available when using a
7960 -- "normal" (not configurable) run time.
7962 return not Configurable_Run_Time_Mode or else RTE_Available (Entity);
7963 end Is_Available;
7965 -- Start of processing for Find_Stream_Subprogram
7967 begin
7968 if Present (Ent) then
7969 return Ent;
7970 end if;
7972 -- Stream attributes for strings are expanded into library calls. The
7973 -- following checks are disabled when the run-time is not available or
7974 -- when compiling predefined types due to bootstrap issues. As a result,
7975 -- the compiler will generate in-place stream routines for string types
7976 -- that appear in GNAT's library, but will generate calls via rtsfind
7977 -- to library routines for user code.
7979 -- Note: In the case of using a configurable run time, it is very likely
7980 -- that stream routines for string types are not present (they require
7981 -- file system support). In this case, the specific stream routines for
7982 -- strings are not used, relying on the regular stream mechanism
7983 -- instead. That is why we include the test Is_Available when dealing
7984 -- with these cases.
7986 if not Is_Predefined_Unit (Current_Sem_Unit) then
7987 -- Storage_Array as defined in package System.Storage_Elements
7989 if Is_RTE (Base_Typ, RE_Storage_Array) then
7991 -- Case of No_Stream_Optimizations restriction active
7993 if Restriction_Active (No_Stream_Optimizations) then
7994 if Nam = TSS_Stream_Input
7995 and then Is_Available (RE_Storage_Array_Input)
7996 then
7997 return RTE (RE_Storage_Array_Input);
7999 elsif Nam = TSS_Stream_Output
8000 and then Is_Available (RE_Storage_Array_Output)
8001 then
8002 return RTE (RE_Storage_Array_Output);
8004 elsif Nam = TSS_Stream_Read
8005 and then Is_Available (RE_Storage_Array_Read)
8006 then
8007 return RTE (RE_Storage_Array_Read);
8009 elsif Nam = TSS_Stream_Write
8010 and then Is_Available (RE_Storage_Array_Write)
8011 then
8012 return RTE (RE_Storage_Array_Write);
8014 elsif Nam /= TSS_Stream_Input and then
8015 Nam /= TSS_Stream_Output and then
8016 Nam /= TSS_Stream_Read and then
8017 Nam /= TSS_Stream_Write
8018 then
8019 raise Program_Error;
8020 end if;
8022 -- Restriction No_Stream_Optimizations is not set, so we can go
8023 -- ahead and optimize using the block IO forms of the routines.
8025 else
8026 if Nam = TSS_Stream_Input
8027 and then Is_Available (RE_Storage_Array_Input_Blk_IO)
8028 then
8029 return RTE (RE_Storage_Array_Input_Blk_IO);
8031 elsif Nam = TSS_Stream_Output
8032 and then Is_Available (RE_Storage_Array_Output_Blk_IO)
8033 then
8034 return RTE (RE_Storage_Array_Output_Blk_IO);
8036 elsif Nam = TSS_Stream_Read
8037 and then Is_Available (RE_Storage_Array_Read_Blk_IO)
8038 then
8039 return RTE (RE_Storage_Array_Read_Blk_IO);
8041 elsif Nam = TSS_Stream_Write
8042 and then Is_Available (RE_Storage_Array_Write_Blk_IO)
8043 then
8044 return RTE (RE_Storage_Array_Write_Blk_IO);
8046 elsif Nam /= TSS_Stream_Input and then
8047 Nam /= TSS_Stream_Output and then
8048 Nam /= TSS_Stream_Read and then
8049 Nam /= TSS_Stream_Write
8050 then
8051 raise Program_Error;
8052 end if;
8053 end if;
8055 -- Stream_Element_Array as defined in package Ada.Streams
8057 elsif Is_RTE (Base_Typ, RE_Stream_Element_Array) then
8059 -- Case of No_Stream_Optimizations restriction active
8061 if Restriction_Active (No_Stream_Optimizations) then
8062 if Nam = TSS_Stream_Input
8063 and then Is_Available (RE_Stream_Element_Array_Input)
8064 then
8065 return RTE (RE_Stream_Element_Array_Input);
8067 elsif Nam = TSS_Stream_Output
8068 and then Is_Available (RE_Stream_Element_Array_Output)
8069 then
8070 return RTE (RE_Stream_Element_Array_Output);
8072 elsif Nam = TSS_Stream_Read
8073 and then Is_Available (RE_Stream_Element_Array_Read)
8074 then
8075 return RTE (RE_Stream_Element_Array_Read);
8077 elsif Nam = TSS_Stream_Write
8078 and then Is_Available (RE_Stream_Element_Array_Write)
8079 then
8080 return RTE (RE_Stream_Element_Array_Write);
8082 elsif Nam /= TSS_Stream_Input and then
8083 Nam /= TSS_Stream_Output and then
8084 Nam /= TSS_Stream_Read and then
8085 Nam /= TSS_Stream_Write
8086 then
8087 raise Program_Error;
8088 end if;
8090 -- Restriction No_Stream_Optimizations is not set, so we can go
8091 -- ahead and optimize using the block IO forms of the routines.
8093 else
8094 if Nam = TSS_Stream_Input
8095 and then Is_Available (RE_Stream_Element_Array_Input_Blk_IO)
8096 then
8097 return RTE (RE_Stream_Element_Array_Input_Blk_IO);
8099 elsif Nam = TSS_Stream_Output
8100 and then Is_Available (RE_Stream_Element_Array_Output_Blk_IO)
8101 then
8102 return RTE (RE_Stream_Element_Array_Output_Blk_IO);
8104 elsif Nam = TSS_Stream_Read
8105 and then Is_Available (RE_Stream_Element_Array_Read_Blk_IO)
8106 then
8107 return RTE (RE_Stream_Element_Array_Read_Blk_IO);
8109 elsif Nam = TSS_Stream_Write
8110 and then Is_Available (RE_Stream_Element_Array_Write_Blk_IO)
8111 then
8112 return RTE (RE_Stream_Element_Array_Write_Blk_IO);
8114 elsif Nam /= TSS_Stream_Input and then
8115 Nam /= TSS_Stream_Output and then
8116 Nam /= TSS_Stream_Read and then
8117 Nam /= TSS_Stream_Write
8118 then
8119 raise Program_Error;
8120 end if;
8121 end if;
8123 -- String as defined in package Ada
8125 elsif Base_Typ = Standard_String then
8127 -- Case of No_Stream_Optimizations restriction active
8129 if Restriction_Active (No_Stream_Optimizations) then
8130 if Nam = TSS_Stream_Input
8131 and then Is_Available (RE_String_Input)
8132 then
8133 return RTE (RE_String_Input);
8135 elsif Nam = TSS_Stream_Output
8136 and then Is_Available (RE_String_Output)
8137 then
8138 return RTE (RE_String_Output);
8140 elsif Nam = TSS_Stream_Read
8141 and then Is_Available (RE_String_Read)
8142 then
8143 return RTE (RE_String_Read);
8145 elsif Nam = TSS_Stream_Write
8146 and then Is_Available (RE_String_Write)
8147 then
8148 return RTE (RE_String_Write);
8150 elsif Nam /= TSS_Stream_Input and then
8151 Nam /= TSS_Stream_Output and then
8152 Nam /= TSS_Stream_Read and then
8153 Nam /= TSS_Stream_Write
8154 then
8155 raise Program_Error;
8156 end if;
8158 -- Restriction No_Stream_Optimizations is not set, so we can go
8159 -- ahead and optimize using the block IO forms of the routines.
8161 else
8162 if Nam = TSS_Stream_Input
8163 and then Is_Available (RE_String_Input_Blk_IO)
8164 then
8165 return RTE (RE_String_Input_Blk_IO);
8167 elsif Nam = TSS_Stream_Output
8168 and then Is_Available (RE_String_Output_Blk_IO)
8169 then
8170 return RTE (RE_String_Output_Blk_IO);
8172 elsif Nam = TSS_Stream_Read
8173 and then Is_Available (RE_String_Read_Blk_IO)
8174 then
8175 return RTE (RE_String_Read_Blk_IO);
8177 elsif Nam = TSS_Stream_Write
8178 and then Is_Available (RE_String_Write_Blk_IO)
8179 then
8180 return RTE (RE_String_Write_Blk_IO);
8182 elsif Nam /= TSS_Stream_Input and then
8183 Nam /= TSS_Stream_Output and then
8184 Nam /= TSS_Stream_Read and then
8185 Nam /= TSS_Stream_Write
8186 then
8187 raise Program_Error;
8188 end if;
8189 end if;
8191 -- Wide_String as defined in package Ada
8193 elsif Base_Typ = Standard_Wide_String then
8195 -- Case of No_Stream_Optimizations restriction active
8197 if Restriction_Active (No_Stream_Optimizations) then
8198 if Nam = TSS_Stream_Input
8199 and then Is_Available (RE_Wide_String_Input)
8200 then
8201 return RTE (RE_Wide_String_Input);
8203 elsif Nam = TSS_Stream_Output
8204 and then Is_Available (RE_Wide_String_Output)
8205 then
8206 return RTE (RE_Wide_String_Output);
8208 elsif Nam = TSS_Stream_Read
8209 and then Is_Available (RE_Wide_String_Read)
8210 then
8211 return RTE (RE_Wide_String_Read);
8213 elsif Nam = TSS_Stream_Write
8214 and then Is_Available (RE_Wide_String_Write)
8215 then
8216 return RTE (RE_Wide_String_Write);
8218 elsif Nam /= TSS_Stream_Input and then
8219 Nam /= TSS_Stream_Output and then
8220 Nam /= TSS_Stream_Read and then
8221 Nam /= TSS_Stream_Write
8222 then
8223 raise Program_Error;
8224 end if;
8226 -- Restriction No_Stream_Optimizations is not set, so we can go
8227 -- ahead and optimize using the block IO forms of the routines.
8229 else
8230 if Nam = TSS_Stream_Input
8231 and then Is_Available (RE_Wide_String_Input_Blk_IO)
8232 then
8233 return RTE (RE_Wide_String_Input_Blk_IO);
8235 elsif Nam = TSS_Stream_Output
8236 and then Is_Available (RE_Wide_String_Output_Blk_IO)
8237 then
8238 return RTE (RE_Wide_String_Output_Blk_IO);
8240 elsif Nam = TSS_Stream_Read
8241 and then Is_Available (RE_Wide_String_Read_Blk_IO)
8242 then
8243 return RTE (RE_Wide_String_Read_Blk_IO);
8245 elsif Nam = TSS_Stream_Write
8246 and then Is_Available (RE_Wide_String_Write_Blk_IO)
8247 then
8248 return RTE (RE_Wide_String_Write_Blk_IO);
8250 elsif Nam /= TSS_Stream_Input and then
8251 Nam /= TSS_Stream_Output and then
8252 Nam /= TSS_Stream_Read and then
8253 Nam /= TSS_Stream_Write
8254 then
8255 raise Program_Error;
8256 end if;
8257 end if;
8259 -- Wide_Wide_String as defined in package Ada
8261 elsif Base_Typ = Standard_Wide_Wide_String then
8263 -- Case of No_Stream_Optimizations restriction active
8265 if Restriction_Active (No_Stream_Optimizations) then
8266 if Nam = TSS_Stream_Input
8267 and then Is_Available (RE_Wide_Wide_String_Input)
8268 then
8269 return RTE (RE_Wide_Wide_String_Input);
8271 elsif Nam = TSS_Stream_Output
8272 and then Is_Available (RE_Wide_Wide_String_Output)
8273 then
8274 return RTE (RE_Wide_Wide_String_Output);
8276 elsif Nam = TSS_Stream_Read
8277 and then Is_Available (RE_Wide_Wide_String_Read)
8278 then
8279 return RTE (RE_Wide_Wide_String_Read);
8281 elsif Nam = TSS_Stream_Write
8282 and then Is_Available (RE_Wide_Wide_String_Write)
8283 then
8284 return RTE (RE_Wide_Wide_String_Write);
8286 elsif Nam /= TSS_Stream_Input and then
8287 Nam /= TSS_Stream_Output and then
8288 Nam /= TSS_Stream_Read and then
8289 Nam /= TSS_Stream_Write
8290 then
8291 raise Program_Error;
8292 end if;
8294 -- Restriction No_Stream_Optimizations is not set, so we can go
8295 -- ahead and optimize using the block IO forms of the routines.
8297 else
8298 if Nam = TSS_Stream_Input
8299 and then Is_Available (RE_Wide_Wide_String_Input_Blk_IO)
8300 then
8301 return RTE (RE_Wide_Wide_String_Input_Blk_IO);
8303 elsif Nam = TSS_Stream_Output
8304 and then Is_Available (RE_Wide_Wide_String_Output_Blk_IO)
8305 then
8306 return RTE (RE_Wide_Wide_String_Output_Blk_IO);
8308 elsif Nam = TSS_Stream_Read
8309 and then Is_Available (RE_Wide_Wide_String_Read_Blk_IO)
8310 then
8311 return RTE (RE_Wide_Wide_String_Read_Blk_IO);
8313 elsif Nam = TSS_Stream_Write
8314 and then Is_Available (RE_Wide_Wide_String_Write_Blk_IO)
8315 then
8316 return RTE (RE_Wide_Wide_String_Write_Blk_IO);
8318 elsif Nam /= TSS_Stream_Input and then
8319 Nam /= TSS_Stream_Output and then
8320 Nam /= TSS_Stream_Read and then
8321 Nam /= TSS_Stream_Write
8322 then
8323 raise Program_Error;
8324 end if;
8325 end if;
8326 end if;
8327 end if;
8329 if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then
8330 return Find_Prim_Op (Typ, Nam);
8331 else
8332 return Find_Inherited_TSS (Typ, Nam);
8333 end if;
8334 end Find_Stream_Subprogram;
8336 ---------------
8337 -- Full_Base --
8338 ---------------
8340 function Full_Base (T : Entity_Id) return Entity_Id is
8341 BT : Entity_Id;
8343 begin
8344 BT := Base_Type (T);
8346 if Is_Private_Type (BT)
8347 and then Present (Full_View (BT))
8348 then
8349 BT := Full_View (BT);
8350 end if;
8352 return BT;
8353 end Full_Base;
8355 -----------------------
8356 -- Get_Index_Subtype --
8357 -----------------------
8359 function Get_Index_Subtype (N : Node_Id) return Node_Id is
8360 P_Type : Entity_Id := Etype (Prefix (N));
8361 Indx : Node_Id;
8362 J : Int;
8364 begin
8365 if Is_Access_Type (P_Type) then
8366 P_Type := Designated_Type (P_Type);
8367 end if;
8369 if No (Expressions (N)) then
8370 J := 1;
8371 else
8372 J := UI_To_Int (Expr_Value (First (Expressions (N))));
8373 end if;
8375 Indx := First_Index (P_Type);
8376 while J > 1 loop
8377 Next_Index (Indx);
8378 J := J - 1;
8379 end loop;
8381 return Etype (Indx);
8382 end Get_Index_Subtype;
8384 -------------------------------
8385 -- Get_Stream_Convert_Pragma --
8386 -------------------------------
8388 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is
8389 Typ : Entity_Id;
8390 N : Node_Id;
8392 begin
8393 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
8394 -- that a stream convert pragma for a tagged type is not inherited from
8395 -- its parent. Probably what is wrong here is that it is basically
8396 -- incorrect to consider a stream convert pragma to be a representation
8397 -- pragma at all ???
8399 N := First_Rep_Item (Implementation_Base_Type (T));
8400 while Present (N) loop
8401 if Nkind (N) = N_Pragma
8402 and then Pragma_Name (N) = Name_Stream_Convert
8403 then
8404 -- For tagged types this pragma is not inherited, so we
8405 -- must verify that it is defined for the given type and
8406 -- not an ancestor.
8408 Typ :=
8409 Entity (Expression (First (Pragma_Argument_Associations (N))));
8411 if not Is_Tagged_Type (T)
8412 or else T = Typ
8413 or else (Is_Private_Type (Typ) and then T = Full_View (Typ))
8414 then
8415 return N;
8416 end if;
8417 end if;
8419 Next_Rep_Item (N);
8420 end loop;
8422 return Empty;
8423 end Get_Stream_Convert_Pragma;
8425 ---------------------------------
8426 -- Is_Constrained_Packed_Array --
8427 ---------------------------------
8429 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
8430 Arr : Entity_Id := Typ;
8432 begin
8433 if Is_Access_Type (Arr) then
8434 Arr := Designated_Type (Arr);
8435 end if;
8437 return Is_Array_Type (Arr)
8438 and then Is_Constrained (Arr)
8439 and then Present (Packed_Array_Impl_Type (Arr));
8440 end Is_Constrained_Packed_Array;
8442 ----------------------------------------
8443 -- Is_Inline_Floating_Point_Attribute --
8444 ----------------------------------------
8446 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is
8447 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
8449 function Is_GCC_Target return Boolean;
8450 -- Return True if we are using a GCC target/back-end
8451 -- ??? Note: the implementation is kludgy/fragile
8453 -------------------
8454 -- Is_GCC_Target --
8455 -------------------
8457 function Is_GCC_Target return Boolean is
8458 begin
8459 return not CodePeer_Mode
8460 and then not Modify_Tree_For_C;
8461 end Is_GCC_Target;
8463 -- Start of processing for Is_Inline_Floating_Point_Attribute
8465 begin
8466 -- Machine and Model can be expanded by the GCC back end only
8468 if Id = Attribute_Machine or else Id = Attribute_Model then
8469 return Is_GCC_Target;
8471 -- Remaining cases handled by all back ends are Rounding and Truncation
8472 -- when appearing as the operand of a conversion to some integer type.
8474 elsif Nkind (Parent (N)) /= N_Type_Conversion
8475 or else not Is_Integer_Type (Etype (Parent (N)))
8476 then
8477 return False;
8478 end if;
8480 -- Here we are in the integer conversion context
8482 -- Very probably we should also recognize the cases of Machine_Rounding
8483 -- and unbiased rounding in this conversion context, but the back end is
8484 -- not yet prepared to handle these cases ???
8486 return Id = Attribute_Rounding or else Id = Attribute_Truncation;
8487 end Is_Inline_Floating_Point_Attribute;
8489 end Exp_Attr;