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