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