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