]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/exp_attr.adb
* exp_dist.adb (Build_RPC_Receiver_Body): New subprogram. This
[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-- --
1e16c51c 9-- Copyright (C) 1992-2004 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- --
13-- ware Foundation; either version 2, or (at your option) any later ver- --
14-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17-- for more details. You should have received a copy of the GNU General --
18-- Public License distributed with GNAT; see file COPYING. If not, write --
19-- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20-- MA 02111-1307, USA. --
21-- --
22-- GNAT was originally developed by the GNAT team at New York University. --
e78e8c8e 23-- Extensive contributions were provided by Ada Core Technologies Inc. --
ee6ba406 24-- --
25------------------------------------------------------------------------------
26
27with Atree; use Atree;
28with Checks; use Checks;
29with Einfo; use Einfo;
00f91aef 30with Elists; use Elists;
ee6ba406 31with Exp_Ch2; use Exp_Ch2;
32with Exp_Ch9; use Exp_Ch9;
33with Exp_Imgv; use Exp_Imgv;
34with Exp_Pakd; use Exp_Pakd;
35with Exp_Strm; use Exp_Strm;
36with Exp_Tss; use Exp_Tss;
37with Exp_Util; use Exp_Util;
38with Gnatvsn; use Gnatvsn;
39with Hostparm; use Hostparm;
40with Lib; use Lib;
41with Namet; use Namet;
42with Nmake; use Nmake;
43with Nlists; use Nlists;
44with Opt; use Opt;
45with Restrict; use Restrict;
1e16c51c 46with Rident; use Rident;
ee6ba406 47with Rtsfind; use Rtsfind;
48with Sem; use Sem;
49with Sem_Ch7; use Sem_Ch7;
50with Sem_Ch8; use Sem_Ch8;
ee6ba406 51with Sem_Eval; use Sem_Eval;
52with Sem_Res; use Sem_Res;
53with Sem_Util; use Sem_Util;
54with Sinfo; use Sinfo;
55with Snames; use Snames;
56with Stand; use Stand;
57with Stringt; use Stringt;
58with Tbuild; use Tbuild;
59with Ttypes; use Ttypes;
60with Uintp; use Uintp;
61with Uname; use Uname;
62with Validsw; use Validsw;
63
64package body Exp_Attr is
65
66 -----------------------
67 -- Local Subprograms --
68 -----------------------
69
70 procedure Compile_Stream_Body_In_Scope
71 (N : Node_Id;
72 Decl : Node_Id;
73 Arr : Entity_Id;
74 Check : Boolean);
75 -- The body for a stream subprogram may be generated outside of the scope
76 -- of the type. If the type is fully private, it may depend on the full
77 -- view of other types (e.g. indices) that are currently private as well.
78 -- We install the declarations of the package in which the type is declared
79 -- before compiling the body in what is its proper environment. The Check
80 -- parameter indicates if checks are to be suppressed for the stream body.
81 -- We suppress checks for array/record reads, since the rule is that these
82 -- are like assignments, out of range values due to uninitialized storage,
83 -- or other invalid values do NOT cause a Constraint_Error to be raised.
84
85 procedure Expand_Fpt_Attribute
9dfe12ae 86 (N : Node_Id;
87 Rtp : Entity_Id;
88 Nam : Name_Id;
ee6ba406 89 Args : List_Id);
90 -- This procedure expands a call to a floating-point attribute function.
91 -- N is the attribute reference node, and Args is a list of arguments to
92 -- be passed to the function call. Rtp is the root type of the floating
93 -- point type involved (used to select the proper generic instantiation
9dfe12ae 94 -- of the package containing the attribute routines). The Nam argument
95 -- is the attribute processing routine to be called. This is normally
96 -- the same as the attribute name, except in the Unaligned_Valid case.
ee6ba406 97
98 procedure Expand_Fpt_Attribute_R (N : Node_Id);
99 -- This procedure expands a call to a floating-point attribute function
9dfe12ae 100 -- that takes a single floating-point argument. The function to be called
101 -- is always the same as the attribute name.
ee6ba406 102
103 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
104 -- This procedure expands a call to a floating-point attribute function
9dfe12ae 105 -- that takes one floating-point argument and one integer argument. The
106 -- function to be called is always the same as the attribute name.
ee6ba406 107
108 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
109 -- This procedure expands a call to a floating-point attribute function
9dfe12ae 110 -- that takes two floating-point arguments. The function to be called
111 -- is always the same as the attribute name.
ee6ba406 112
113 procedure Expand_Pred_Succ (N : Node_Id);
114 -- Handles expansion of Pred or Succ attributes for case of non-real
115 -- operand with overflow checking required.
116
117 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
118 -- Used for Last, Last, and Length, when the prefix is an array type,
119 -- Obtains the corresponding index subtype.
120
121 procedure Expand_Access_To_Type (N : Node_Id);
122 -- A reference to a type within its own scope is resolved to a reference
123 -- to the current instance of the type in its initialization procedure.
124
125 function Find_Inherited_TSS
126 (Typ : Entity_Id;
9dfe12ae 127 Nam : TSS_Name_Type) return Entity_Id;
128 -- Returns the TSS of name Nam of Typ, or of its closest ancestor defining
129 -- such a TSS. Empty is returned is neither Typ nor any of its ancestors
130 -- have such a TSS.
131
132 function Find_Stream_Subprogram
133 (Typ : Entity_Id;
134 Nam : TSS_Name_Type) return Entity_Id;
135 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
136 -- types, the corresponding primitive operation is looked up, else the
137 -- appropriate TSS from the type itself, or from its closest ancestor
138 -- defining it, is returned. In both cases, inheritance of representation
139 -- aspects is thus taken into account.
ee6ba406 140
141 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
142 -- Utility for array attributes, returns true on packed constrained
143 -- arrays, and on access to same.
144
145 ----------------------------------
146 -- Compile_Stream_Body_In_Scope --
147 ----------------------------------
148
149 procedure Compile_Stream_Body_In_Scope
150 (N : Node_Id;
151 Decl : Node_Id;
152 Arr : Entity_Id;
153 Check : Boolean)
154 is
155 Installed : Boolean := False;
156 Scop : constant Entity_Id := Scope (Arr);
157 Curr : constant Entity_Id := Current_Scope;
158
159 begin
160 if Is_Hidden (Arr)
161 and then not In_Open_Scopes (Scop)
162 and then Ekind (Scop) = E_Package
163 then
164 New_Scope (Scop);
165 Install_Visible_Declarations (Scop);
166 Install_Private_Declarations (Scop);
167 Installed := True;
168
169 -- The entities in the package are now visible, but the generated
170 -- stream entity must appear in the current scope (usually an
171 -- enclosing stream function) so that itypes all have their proper
172 -- scopes.
173
174 New_Scope (Curr);
175 end if;
176
177 if Check then
178 Insert_Action (N, Decl);
179 else
180 Insert_Action (N, Decl, All_Checks);
181 end if;
182
183 if Installed then
184
185 -- Remove extra copy of current scope, and package itself
186
187 Pop_Scope;
188 End_Package_Scope (Scop);
189 end if;
190 end Compile_Stream_Body_In_Scope;
191
192 ---------------------------
193 -- Expand_Access_To_Type --
194 ---------------------------
195
196 procedure Expand_Access_To_Type (N : Node_Id) is
197 Loc : constant Source_Ptr := Sloc (N);
198 Typ : constant Entity_Id := Etype (N);
199 Pref : constant Node_Id := Prefix (N);
200 Par : Node_Id;
201 Formal : Entity_Id;
202
203 begin
204 if Is_Entity_Name (Pref)
205 and then Is_Type (Entity (Pref))
206 then
207 -- If the current instance name denotes a task type,
208 -- then the access attribute is rewritten to be the
209 -- name of the "_task" parameter associated with the
210 -- task type's task body procedure. An unchecked
211 -- conversion is applied to ensure a type match in
212 -- cases of expander-generated calls (e.g., init procs).
213
214 if Is_Task_Type (Entity (Pref)) then
215 Formal :=
216 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
217
218 while Present (Formal) loop
219 exit when Chars (Formal) = Name_uTask;
220 Next_Entity (Formal);
221 end loop;
222
223 pragma Assert (Present (Formal));
224
225 Rewrite (N,
226 Unchecked_Convert_To (Typ, New_Occurrence_Of (Formal, Loc)));
227 Set_Etype (N, Typ);
228
229 -- The expression must appear in a default expression,
230 -- (which in the initialization procedure is the rhs of
231 -- an assignment), and not in a discriminant constraint.
232
233 else
234 Par := Parent (N);
235
236 while Present (Par) loop
237 exit when Nkind (Par) = N_Assignment_Statement;
238
239 if Nkind (Par) = N_Component_Declaration then
240 return;
241 end if;
242
243 Par := Parent (Par);
244 end loop;
245
246 if Present (Par) then
247 Rewrite (N,
248 Make_Attribute_Reference (Loc,
249 Prefix => Make_Identifier (Loc, Name_uInit),
250 Attribute_Name => Attribute_Name (N)));
251
252 Analyze_And_Resolve (N, Typ);
253 end if;
254 end if;
255 end if;
256 end Expand_Access_To_Type;
257
258 --------------------------
259 -- Expand_Fpt_Attribute --
260 --------------------------
261
262 procedure Expand_Fpt_Attribute
263 (N : Node_Id;
264 Rtp : Entity_Id;
9dfe12ae 265 Nam : Name_Id;
ee6ba406 266 Args : List_Id)
267 is
268 Loc : constant Source_Ptr := Sloc (N);
269 Typ : constant Entity_Id := Etype (N);
270 Pkg : RE_Id;
271 Fnm : Node_Id;
272
273 begin
274 -- The function name is the selected component Fat_xxx.yyy where xxx
9dfe12ae 275 -- is the floating-point root type, and yyy is the argument Nam.
ee6ba406 276
277 -- Note: it would be more usual to have separate RE entries for each
278 -- of the entities in the Fat packages, but first they have identical
279 -- names (so we would have to have lots of renaming declarations to
280 -- meet the normal RE rule of separate names for all runtime entities),
281 -- and second there would be an awful lot of them!
282
283 if Rtp = Standard_Short_Float then
284 Pkg := RE_Fat_Short_Float;
285 elsif Rtp = Standard_Float then
286 Pkg := RE_Fat_Float;
287 elsif Rtp = Standard_Long_Float then
288 Pkg := RE_Fat_Long_Float;
289 else
290 Pkg := RE_Fat_Long_Long_Float;
291 end if;
292
293 Fnm :=
294 Make_Selected_Component (Loc,
295 Prefix => New_Reference_To (RTE (Pkg), Loc),
9dfe12ae 296 Selector_Name => Make_Identifier (Loc, Nam));
ee6ba406 297
298 -- The generated call is given the provided set of parameters, and then
299 -- wrapped in a conversion which converts the result to the target type
300
301 Rewrite (N,
302 Unchecked_Convert_To (Etype (N),
303 Make_Function_Call (Loc,
304 Name => Fnm,
305 Parameter_Associations => Args)));
306
307 Analyze_And_Resolve (N, Typ);
ee6ba406 308 end Expand_Fpt_Attribute;
309
310 ----------------------------
311 -- Expand_Fpt_Attribute_R --
312 ----------------------------
313
314 -- The single argument is converted to its root type to call the
315 -- appropriate runtime function, with the actual call being built
316 -- by Expand_Fpt_Attribute
317
318 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
319 E1 : constant Node_Id := First (Expressions (N));
320 Rtp : constant Entity_Id := Root_Type (Etype (E1));
321
322 begin
9dfe12ae 323 Expand_Fpt_Attribute
324 (N, Rtp, Attribute_Name (N),
325 New_List (Unchecked_Convert_To (Rtp, Relocate_Node (E1))));
ee6ba406 326 end Expand_Fpt_Attribute_R;
327
328 -----------------------------
329 -- Expand_Fpt_Attribute_RI --
330 -----------------------------
331
332 -- The first argument is converted to its root type and the second
333 -- argument is converted to standard long long integer to call the
334 -- appropriate runtime function, with the actual call being built
335 -- by Expand_Fpt_Attribute
336
337 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
338 E1 : constant Node_Id := First (Expressions (N));
339 Rtp : constant Entity_Id := Root_Type (Etype (E1));
340 E2 : constant Node_Id := Next (E1);
341
342 begin
9dfe12ae 343 Expand_Fpt_Attribute
344 (N, Rtp, Attribute_Name (N),
345 New_List (
346 Unchecked_Convert_To (Rtp, Relocate_Node (E1)),
347 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
ee6ba406 348 end Expand_Fpt_Attribute_RI;
349
350 -----------------------------
351 -- Expand_Fpt_Attribute_RR --
352 -----------------------------
353
354 -- The two arguments is converted to their root types to call the
355 -- appropriate runtime function, with the actual call being built
356 -- by Expand_Fpt_Attribute
357
358 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
359 E1 : constant Node_Id := First (Expressions (N));
360 Rtp : constant Entity_Id := Root_Type (Etype (E1));
361 E2 : constant Node_Id := Next (E1);
362
363 begin
9dfe12ae 364 Expand_Fpt_Attribute
365 (N, Rtp, Attribute_Name (N),
366 New_List (
367 Unchecked_Convert_To (Rtp, Relocate_Node (E1)),
368 Unchecked_Convert_To (Rtp, Relocate_Node (E2))));
ee6ba406 369 end Expand_Fpt_Attribute_RR;
370
371 ----------------------------------
372 -- Expand_N_Attribute_Reference --
373 ----------------------------------
374
375 procedure Expand_N_Attribute_Reference (N : Node_Id) is
376 Loc : constant Source_Ptr := Sloc (N);
377 Typ : constant Entity_Id := Etype (N);
378 Btyp : constant Entity_Id := Base_Type (Typ);
379 Pref : constant Node_Id := Prefix (N);
380 Exprs : constant List_Id := Expressions (N);
381 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
382
383 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
384 -- Rewrites a stream attribute for Read, Write or Output with the
385 -- procedure call. Pname is the entity for the procedure to call.
386
387 ------------------------------
388 -- Rewrite_Stream_Proc_Call --
389 ------------------------------
390
391 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
392 Item : constant Node_Id := Next (First (Exprs));
9dfe12ae 393 Formal : constant Entity_Id := Next_Formal (First_Formal (Pname));
394 Formal_Typ : constant Entity_Id := Etype (Formal);
395 Is_Written : constant Boolean := (Ekind (Formal) /= E_In_Parameter);
ee6ba406 396
397 begin
9dfe12ae 398 -- The expansion depends on Item, the second actual, which is
399 -- the object being streamed in or out.
400
401 -- If the item is a component of a packed array type, and
402 -- a conversion is needed on exit, we introduce a temporary to
403 -- hold the value, because otherwise the packed reference will
404 -- not be properly expanded.
405
406 if Nkind (Item) = N_Indexed_Component
407 and then Is_Packed (Base_Type (Etype (Prefix (Item))))
408 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
409 and then Is_Written
410 then
411 declare
412 Temp : constant Entity_Id :=
413 Make_Defining_Identifier
414 (Loc, New_Internal_Name ('V'));
415 Decl : Node_Id;
416 Assn : Node_Id;
417
418 begin
419 Decl :=
420 Make_Object_Declaration (Loc,
421 Defining_Identifier => Temp,
422 Object_Definition =>
423 New_Occurrence_Of (Formal_Typ, Loc));
424 Set_Etype (Temp, Formal_Typ);
425
426 Assn :=
427 Make_Assignment_Statement (Loc,
428 Name => New_Copy_Tree (Item),
429 Expression =>
430 Unchecked_Convert_To
431 (Etype (Item), New_Occurrence_Of (Temp, Loc)));
432
433 Rewrite (Item, New_Occurrence_Of (Temp, Loc));
434 Insert_Actions (N,
435 New_List (
436 Decl,
437 Make_Procedure_Call_Statement (Loc,
438 Name => New_Occurrence_Of (Pname, Loc),
439 Parameter_Associations => Exprs),
440 Assn));
441
442 Rewrite (N, Make_Null_Statement (Loc));
443 return;
444 end;
445 end if;
ee6ba406 446
447 -- For the class-wide dispatching cases, and for cases in which
448 -- the base type of the second argument matches the base type of
9dfe12ae 449 -- the corresponding formal parameter (that is to say the stream
450 -- operation is not inherited), we are all set, and can use the
451 -- argument unchanged.
ee6ba406 452
453 -- For all other cases we do an unchecked conversion of the second
454 -- parameter to the type of the formal of the procedure we are
455 -- calling. This deals with the private type cases, and with going
456 -- to the root type as required in elementary type case.
457
458 if not Is_Class_Wide_Type (Entity (Pref))
9dfe12ae 459 and then not Is_Class_Wide_Type (Etype (Item))
ee6ba406 460 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
461 then
462 Rewrite (Item,
463 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
464
465 -- For untagged derived types set Assignment_OK, to prevent
466 -- copies from being created when the unchecked conversion
467 -- is expanded (which would happen in Remove_Side_Effects
468 -- if Expand_N_Unchecked_Conversion were allowed to call
469 -- Force_Evaluation). The copy could violate Ada semantics
470 -- in cases such as an actual that is an out parameter.
471 -- Note that this approach is also used in exp_ch7 for calls
472 -- to controlled type operations to prevent problems with
473 -- actuals wrapped in unchecked conversions.
474
475 if Is_Untagged_Derivation (Etype (Expression (Item))) then
476 Set_Assignment_OK (Item);
477 end if;
478 end if;
479
480 -- And now rewrite the call
481
482 Rewrite (N,
483 Make_Procedure_Call_Statement (Loc,
484 Name => New_Occurrence_Of (Pname, Loc),
485 Parameter_Associations => Exprs));
486
487 Analyze (N);
488 end Rewrite_Stream_Proc_Call;
489
490 -- Start of processing for Expand_N_Attribute_Reference
491
492 begin
493 -- Do required validity checking
494
495 if Validity_Checks_On and Validity_Check_Operands then
496 declare
497 Expr : Node_Id;
498
499 begin
500 Expr := First (Expressions (N));
501 while Present (Expr) loop
502 Ensure_Valid (Expr);
503 Next (Expr);
504 end loop;
505 end;
506 end if;
507
508 -- Remaining processing depends on specific attribute
509
510 case Id is
511
512 ------------
513 -- Access --
514 ------------
515
516 when Attribute_Access =>
517
518 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type then
519
520 -- The value of the attribute_reference is a record containing
521 -- two fields: an access to the protected object, and an access
522 -- to the subprogram itself. The prefix is a selected component.
523
524 declare
525 Agg : Node_Id;
526 Sub : Entity_Id;
f15731c4 527 E_T : constant Entity_Id := Equivalent_Type (Btyp);
ee6ba406 528 Acc : constant Entity_Id :=
529 Etype (Next_Component (First_Component (E_T)));
530 Obj_Ref : Node_Id;
531 Curr : Entity_Id;
532
533 begin
534 -- Within the body of the protected type, the prefix
535 -- designates a local operation, and the object is the first
536 -- parameter of the corresponding protected body of the
537 -- current enclosing operation.
538
539 if Is_Entity_Name (Pref) then
540 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
541 Sub :=
542 New_Occurrence_Of
543 (Protected_Body_Subprogram (Entity (Pref)), Loc);
544 Curr := Current_Scope;
545
546 while Scope (Curr) /= Scope (Entity (Pref)) loop
547 Curr := Scope (Curr);
548 end loop;
549
550 Obj_Ref :=
551 Make_Attribute_Reference (Loc,
552 Prefix =>
553 New_Occurrence_Of
554 (First_Formal
555 (Protected_Body_Subprogram (Curr)), Loc),
556 Attribute_Name => Name_Address);
557
558 -- Case where the prefix is not an entity name. Find the
559 -- version of the protected operation to be called from
560 -- outside the protected object.
561
562 else
563 Sub :=
564 New_Occurrence_Of
565 (External_Subprogram
566 (Entity (Selector_Name (Pref))), Loc);
567
568 Obj_Ref :=
569 Make_Attribute_Reference (Loc,
570 Prefix => Relocate_Node (Prefix (Pref)),
571 Attribute_Name => Name_Address);
572 end if;
573
574 Agg :=
575 Make_Aggregate (Loc,
576 Expressions =>
577 New_List (
578 Obj_Ref,
579 Unchecked_Convert_To (Acc,
580 Make_Attribute_Reference (Loc,
581 Prefix => Sub,
582 Attribute_Name => Name_Address))));
583
584 Rewrite (N, Agg);
585
f15731c4 586 Analyze_And_Resolve (N, E_T);
ee6ba406 587
588 -- For subsequent analysis, the node must retain its type.
589 -- The backend will replace it with the equivalent type where
590 -- needed.
591
592 Set_Etype (N, Typ);
593 end;
594
595 elsif Ekind (Btyp) = E_General_Access_Type then
596 declare
597 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
598 Parm_Ent : Entity_Id;
599 Conversion : Node_Id;
600
601 begin
602 -- If the prefix of an Access attribute is a dereference of an
603 -- access parameter (or a renaming of such a dereference) and
604 -- the context is a general access type (but not an anonymous
605 -- access type), then rewrite the attribute as a conversion of
606 -- the access parameter to the context access type. This will
607 -- result in an accessibility check being performed, if needed.
608
609 -- (X.all'Access => Acc_Type (X))
610
611 if Nkind (Ref_Object) = N_Explicit_Dereference
612 and then Is_Entity_Name (Prefix (Ref_Object))
613 then
614 Parm_Ent := Entity (Prefix (Ref_Object));
615
616 if Ekind (Parm_Ent) in Formal_Kind
617 and then Ekind (Etype (Parm_Ent)) = E_Anonymous_Access_Type
618 and then Present (Extra_Accessibility (Parm_Ent))
619 then
620 Conversion :=
621 Convert_To (Typ, New_Copy_Tree (Prefix (Ref_Object)));
622
623 Rewrite (N, Conversion);
624 Analyze_And_Resolve (N, Typ);
625 end if;
626 end if;
627 end;
628
629 -- If the prefix is a type name, this is a reference to the current
630 -- instance of the type, within its initialization procedure.
631
632 else
633 Expand_Access_To_Type (N);
634 end if;
635
636 --------------
637 -- Adjacent --
638 --------------
639
640 -- Transforms 'Adjacent into a call to the floating-point attribute
641 -- function Adjacent in Fat_xxx (where xxx is the root type)
642
643 when Attribute_Adjacent =>
644 Expand_Fpt_Attribute_RR (N);
645
646 -------------
647 -- Address --
648 -------------
649
650 when Attribute_Address => Address : declare
651 Task_Proc : Entity_Id;
652
653 begin
654 -- If the prefix is a task or a task type, the useful address
655 -- is that of the procedure for the task body, i.e. the actual
656 -- program unit. We replace the original entity with that of
657 -- the procedure.
658
659 if Is_Entity_Name (Pref)
660 and then Is_Task_Type (Entity (Pref))
661 then
662 Task_Proc := Next_Entity (Root_Type (Etype (Pref)));
663
664 while Present (Task_Proc) loop
665 exit when Ekind (Task_Proc) = E_Procedure
666 and then Etype (First_Formal (Task_Proc)) =
667 Corresponding_Record_Type (Etype (Pref));
668 Next_Entity (Task_Proc);
669 end loop;
670
671 if Present (Task_Proc) then
672 Set_Entity (Pref, Task_Proc);
673 Set_Etype (Pref, Etype (Task_Proc));
674 end if;
675
676 -- Similarly, the address of a protected operation is the address
677 -- of the corresponding protected body, regardless of the protected
678 -- object from which it is selected.
679
680 elsif Nkind (Pref) = N_Selected_Component
681 and then Is_Subprogram (Entity (Selector_Name (Pref)))
682 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
683 then
684 Rewrite (Pref,
685 New_Occurrence_Of (
686 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
687
688 elsif Nkind (Pref) = N_Explicit_Dereference
689 and then Ekind (Etype (Pref)) = E_Subprogram_Type
690 and then Convention (Etype (Pref)) = Convention_Protected
691 then
692 -- The prefix is be a dereference of an access_to_protected_
693 -- subprogram. The desired address is the second component of
694 -- the record that represents the access.
695
696 declare
697 Addr : constant Entity_Id := Etype (N);
698 Ptr : constant Node_Id := Prefix (Pref);
699 T : constant Entity_Id :=
700 Equivalent_Type (Base_Type (Etype (Ptr)));
701
702 begin
703 Rewrite (N,
704 Unchecked_Convert_To (Addr,
705 Make_Selected_Component (Loc,
706 Prefix => Unchecked_Convert_To (T, Ptr),
707 Selector_Name => New_Occurrence_Of (
708 Next_Entity (First_Entity (T)), Loc))));
709
710 Analyze_And_Resolve (N, Addr);
711 end;
712 end if;
713
714 -- Deal with packed array reference, other cases are handled by gigi
715
716 if Involves_Packed_Array_Reference (Pref) then
717 Expand_Packed_Address_Reference (N);
718 end if;
719 end Address;
720
9dfe12ae 721 ---------------
722 -- Alignment --
723 ---------------
724
725 when Attribute_Alignment => Alignment : declare
726 Ptyp : constant Entity_Id := Etype (Pref);
727 New_Node : Node_Id;
728
729 begin
730 -- For class-wide types, X'Class'Alignment is transformed into a
731 -- direct reference to the Alignment of the class type, so that the
732 -- back end does not have to deal with the X'Class'Alignment
733 -- reference.
734
735 if Is_Entity_Name (Pref)
736 and then Is_Class_Wide_Type (Entity (Pref))
737 then
738 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
739 return;
740
741 -- For x'Alignment applied to an object of a class wide type,
742 -- transform X'Alignment into a call to the predefined primitive
743 -- operation _Alignment applied to X.
744
745 elsif Is_Class_Wide_Type (Ptyp) then
746 New_Node :=
747 Make_Function_Call (Loc,
748 Name => New_Reference_To
749 (Find_Prim_Op (Ptyp, Name_uAlignment), Loc),
750 Parameter_Associations => New_List (Pref));
751
752 if Typ /= Standard_Integer then
753
754 -- The context is a specific integer type with which the
755 -- original attribute was compatible. The function has a
756 -- specific type as well, so to preserve the compatibility
757 -- we must convert explicitly.
758
759 New_Node := Convert_To (Typ, New_Node);
760 end if;
761
762 Rewrite (N, New_Node);
763 Analyze_And_Resolve (N, Typ);
764 return;
765
766 -- For all other cases, we just have to deal with the case of
767 -- the fact that the result can be universal.
768
769 else
770 Apply_Universal_Integer_Attribute_Checks (N);
771 end if;
772 end Alignment;
773
ee6ba406 774 ---------------
775 -- AST_Entry --
776 ---------------
777
778 when Attribute_AST_Entry => AST_Entry : declare
779 Ttyp : Entity_Id;
780 T_Id : Node_Id;
781 Eent : Entity_Id;
782
783 Entry_Ref : Node_Id;
784 -- The reference to the entry or entry family
785
786 Index : Node_Id;
787 -- The index expression for an entry family reference, or
788 -- the Empty if Entry_Ref references a simple entry.
789
790 begin
791 if Nkind (Pref) = N_Indexed_Component then
792 Entry_Ref := Prefix (Pref);
793 Index := First (Expressions (Pref));
794 else
795 Entry_Ref := Pref;
796 Index := Empty;
797 end if;
798
799 -- Get expression for Task_Id and the entry entity
800
801 if Nkind (Entry_Ref) = N_Selected_Component then
802 T_Id :=
803 Make_Attribute_Reference (Loc,
804 Attribute_Name => Name_Identity,
805 Prefix => Prefix (Entry_Ref));
806
807 Ttyp := Etype (Prefix (Entry_Ref));
808 Eent := Entity (Selector_Name (Entry_Ref));
809
810 else
811 T_Id :=
812 Make_Function_Call (Loc,
813 Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc));
814
815 Eent := Entity (Entry_Ref);
816
817 -- We have to find the enclosing task to get the task type
818 -- There must be one, since we already validated this earlier
819
820 Ttyp := Current_Scope;
821 while not Is_Task_Type (Ttyp) loop
822 Ttyp := Scope (Ttyp);
823 end loop;
824 end if;
825
826 -- Now rewrite the attribute with a call to Create_AST_Handler
827
828 Rewrite (N,
829 Make_Function_Call (Loc,
830 Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc),
831 Parameter_Associations => New_List (
832 T_Id,
833 Entry_Index_Expression (Loc, Eent, Index, Ttyp))));
834
835 Analyze_And_Resolve (N, RTE (RE_AST_Handler));
836 end AST_Entry;
837
838 ------------------
839 -- Bit_Position --
840 ------------------
841
842 -- We compute this if a component clause was present, otherwise
843 -- we leave the computation up to Gigi, since we don't know what
844 -- layout will be chosen.
845
846 -- Note that the attribute can apply to a naked record component
847 -- in generated code (i.e. the prefix is an identifier that
848 -- references the component or discriminant entity).
849
850 when Attribute_Bit_Position => Bit_Position :
851 declare
852 CE : Entity_Id;
853
854 begin
855 if Nkind (Pref) = N_Identifier then
856 CE := Entity (Pref);
857 else
858 CE := Entity (Selector_Name (Pref));
859 end if;
860
861 if Known_Static_Component_Bit_Offset (CE) then
862 Rewrite (N,
863 Make_Integer_Literal (Loc,
864 Intval => Component_Bit_Offset (CE)));
865 Analyze_And_Resolve (N, Typ);
866
867 else
868 Apply_Universal_Integer_Attribute_Checks (N);
869 end if;
870 end Bit_Position;
871
872 ------------------
873 -- Body_Version --
874 ------------------
875
876 -- A reference to P'Body_Version or P'Version is expanded to
877
878 -- Vnn : Unsigned;
879 -- pragma Import (C, Vnn, "uuuuT";
880 -- ...
881 -- Get_Version_String (Vnn)
882
883 -- where uuuu is the unit name (dots replaced by double underscore)
884 -- and T is B for the cases of Body_Version, or Version applied to a
885 -- subprogram acting as its own spec, and S for Version applied to a
886 -- subprogram spec or package. This sequence of code references the
887 -- the unsigned constant created in the main program by the binder.
888
889 -- A special exception occurs for Standard, where the string
890 -- returned is a copy of the library string in gnatvsn.ads.
891
892 when Attribute_Body_Version | Attribute_Version => Version : declare
893 E : constant Entity_Id :=
894 Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
895 Pent : Entity_Id := Entity (Pref);
896 S : String_Id;
897
898 begin
899 -- If not library unit, get to containing library unit
900
901 while Pent /= Standard_Standard
902 and then Scope (Pent) /= Standard_Standard
903 loop
904 Pent := Scope (Pent);
905 end loop;
906
907 -- Special case Standard
908
909 if Pent = Standard_Standard
910 or else Pent = Standard_ASCII
911 then
cf6d853e 912 Name_Buffer (1 .. Verbose_Library_Version'Length) :=
913 Verbose_Library_Version;
914 Name_Len := Verbose_Library_Version'Length;
ee6ba406 915 Rewrite (N,
916 Make_String_Literal (Loc,
917 Strval => String_From_Name_Buffer));
918
919 -- All other cases
920
921 else
922 -- Build required string constant
923
924 Get_Name_String (Get_Unit_Name (Pent));
925
926 Start_String;
927 for J in 1 .. Name_Len - 2 loop
928 if Name_Buffer (J) = '.' then
929 Store_String_Chars ("__");
930 else
931 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
932 end if;
933 end loop;
934
935 -- Case of subprogram acting as its own spec, always use body
936
937 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
938 and then Nkind (Parent (Declaration_Node (Pent))) =
939 N_Subprogram_Body
940 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
941 then
942 Store_String_Chars ("B");
943
944 -- Case of no body present, always use spec
945
946 elsif not Unit_Requires_Body (Pent) then
947 Store_String_Chars ("S");
948
949 -- Otherwise use B for Body_Version, S for spec
950
951 elsif Id = Attribute_Body_Version then
952 Store_String_Chars ("B");
953 else
954 Store_String_Chars ("S");
955 end if;
956
957 S := End_String;
958 Lib.Version_Referenced (S);
959
960 -- Insert the object declaration
961
962 Insert_Actions (N, New_List (
963 Make_Object_Declaration (Loc,
964 Defining_Identifier => E,
965 Object_Definition =>
966 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
967
968 -- Set entity as imported with correct external name
969
970 Set_Is_Imported (E);
971 Set_Interface_Name (E, Make_String_Literal (Loc, S));
972
973 -- And now rewrite original reference
974
975 Rewrite (N,
976 Make_Function_Call (Loc,
977 Name => New_Reference_To (RTE (RE_Get_Version_String), Loc),
978 Parameter_Associations => New_List (
979 New_Occurrence_Of (E, Loc))));
980 end if;
981
982 Analyze_And_Resolve (N, RTE (RE_Version_String));
983 end Version;
984
985 -------------
986 -- Ceiling --
987 -------------
988
989 -- Transforms 'Ceiling into a call to the floating-point attribute
990 -- function Ceiling in Fat_xxx (where xxx is the root type)
991
992 when Attribute_Ceiling =>
993 Expand_Fpt_Attribute_R (N);
994
995 --------------
996 -- Callable --
997 --------------
998
999 -- Transforms 'Callable attribute into a call to the Callable function.
1000
1001 when Attribute_Callable => Callable :
1002 begin
1003 Rewrite (N,
1004 Build_Call_With_Task (Pref, RTE (RE_Callable)));
1005 Analyze_And_Resolve (N, Standard_Boolean);
1006 end Callable;
1007
1008 ------------
1009 -- Caller --
1010 ------------
1011
1012 -- Transforms 'Caller attribute into a call to either the
1013 -- Task_Entry_Caller or the Protected_Entry_Caller function.
1014
1015 when Attribute_Caller => Caller : declare
7f9be362 1016 Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id);
9dfe12ae 1017 Ent : constant Entity_Id := Entity (Pref);
1018 Conctype : constant Entity_Id := Scope (Ent);
1019 Nest_Depth : Integer := 0;
ee6ba406 1020 Name : Node_Id;
1021 S : Entity_Id;
1022
1023 begin
1024 -- Protected case
1025
1026 if Is_Protected_Type (Conctype) then
1027 if Abort_Allowed
1e16c51c 1028 or else Restriction_Active (No_Entry_Queue) = False
ee6ba406 1029 or else Number_Entries (Conctype) > 1
1030 then
1031 Name :=
1032 New_Reference_To
1033 (RTE (RE_Protected_Entry_Caller), Loc);
1034 else
1035 Name :=
1036 New_Reference_To
1037 (RTE (RE_Protected_Single_Entry_Caller), Loc);
1038 end if;
1039
1040 Rewrite (N,
1041 Unchecked_Convert_To (Id_Kind,
1042 Make_Function_Call (Loc,
1043 Name => Name,
1044 Parameter_Associations => New_List
1045 (New_Reference_To (
1046 Object_Ref
1047 (Corresponding_Body (Parent (Conctype))), Loc)))));
1048
1049 -- Task case
1050
1051 else
1052 -- Determine the nesting depth of the E'Caller attribute, that
1053 -- is, how many accept statements are nested within the accept
1054 -- statement for E at the point of E'Caller. The runtime uses
1055 -- this depth to find the specified entry call.
1056
1057 for J in reverse 0 .. Scope_Stack.Last loop
1058 S := Scope_Stack.Table (J).Entity;
1059
1060 -- We should not reach the scope of the entry, as it should
1061 -- already have been checked in Sem_Attr that this attribute
1062 -- reference is within a matching accept statement.
1063
1064 pragma Assert (S /= Conctype);
1065
1066 if S = Ent then
1067 exit;
1068
1069 elsif Is_Entry (S) then
1070 Nest_Depth := Nest_Depth + 1;
1071 end if;
1072 end loop;
1073
1074 Rewrite (N,
1075 Unchecked_Convert_To (Id_Kind,
1076 Make_Function_Call (Loc,
1077 Name => New_Reference_To (
1078 RTE (RE_Task_Entry_Caller), Loc),
1079 Parameter_Associations => New_List (
1080 Make_Integer_Literal (Loc,
1081 Intval => Int (Nest_Depth))))));
1082 end if;
1083
1084 Analyze_And_Resolve (N, Id_Kind);
1085 end Caller;
1086
1087 -------------
1088 -- Compose --
1089 -------------
1090
1091 -- Transforms 'Compose into a call to the floating-point attribute
1092 -- function Compose in Fat_xxx (where xxx is the root type)
1093
1094 -- Note: we strictly should have special code here to deal with the
1095 -- case of absurdly negative arguments (less than Integer'First)
1096 -- which will return a (signed) zero value, but it hardly seems
1097 -- worth the effort. Absurdly large positive arguments will raise
1098 -- constraint error which is fine.
1099
1100 when Attribute_Compose =>
1101 Expand_Fpt_Attribute_RI (N);
1102
1103 -----------------
1104 -- Constrained --
1105 -----------------
1106
1107 when Attribute_Constrained => Constrained : declare
1108 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
1109
1110 begin
1111 -- Reference to a parameter where the value is passed as an extra
1112 -- actual, corresponding to the extra formal referenced by the
9dfe12ae 1113 -- Extra_Constrained field of the corresponding formal. If this
1114 -- is an entry in-parameter, it is replaced by a constant renaming
1115 -- for which Extra_Constrained is never created.
ee6ba406 1116
1117 if Present (Formal_Ent)
9dfe12ae 1118 and then Ekind (Formal_Ent) /= E_Constant
ee6ba406 1119 and then Present (Extra_Constrained (Formal_Ent))
1120 then
1121 Rewrite (N,
1122 New_Occurrence_Of
1123 (Extra_Constrained (Formal_Ent), Sloc (N)));
1124
1125 -- For variables with a Extra_Constrained field, we use the
1126 -- corresponding entity.
1127
1128 elsif Nkind (Pref) = N_Identifier
1129 and then Ekind (Entity (Pref)) = E_Variable
1130 and then Present (Extra_Constrained (Entity (Pref)))
1131 then
1132 Rewrite (N,
1133 New_Occurrence_Of
1134 (Extra_Constrained (Entity (Pref)), Sloc (N)));
1135
1136 -- For all other entity names, we can tell at compile time
1137
1138 elsif Is_Entity_Name (Pref) then
1139 declare
1140 Ent : constant Entity_Id := Entity (Pref);
1141 Res : Boolean;
1142
1143 begin
1144 -- (RM J.4) obsolescent cases
1145
1146 if Is_Type (Ent) then
1147
1148 -- Private type
1149
1150 if Is_Private_Type (Ent) then
1151 Res := not Has_Discriminants (Ent)
1152 or else Is_Constrained (Ent);
1153
1154 -- It not a private type, must be a generic actual type
1155 -- that corresponded to a private type. We know that this
1156 -- correspondence holds, since otherwise the reference
1157 -- within the generic template would have been illegal.
1158
1159 else
9dfe12ae 1160 if Is_Composite_Type (Underlying_Type (Ent)) then
1161 Res := Is_Constrained (Ent);
1162 else
1163 Res := True;
1164 end if;
ee6ba406 1165 end if;
1166
1167 -- If the prefix is not a variable or is aliased, then
1168 -- definitely true; if it's a formal parameter without
1169 -- an associated extra formal, then treat it as constrained.
1170
1171 elsif not Is_Variable (Pref)
1172 or else Present (Formal_Ent)
1173 or else Is_Aliased_View (Pref)
1174 then
1175 Res := True;
1176
1177 -- Variable case, just look at type to see if it is
1178 -- constrained. Note that the one case where this is
1179 -- not accurate (the procedure formal case), has been
1180 -- handled above.
1181
1182 else
1183 Res := Is_Constrained (Etype (Ent));
1184 end if;
1185
1bbc9831 1186 Rewrite (N,
1187 New_Reference_To (Boolean_Literals (Res), Loc));
ee6ba406 1188 end;
1189
1190 -- Prefix is not an entity name. These are also cases where
1191 -- we can always tell at compile time by looking at the form
1192 -- and type of the prefix.
1193
1194 else
1bbc9831 1195 Rewrite (N,
1196 New_Reference_To (
1197 Boolean_Literals (
1198 not Is_Variable (Pref)
1199 or else Nkind (Pref) = N_Explicit_Dereference
1200 or else Is_Constrained (Etype (Pref))),
1201 Loc));
ee6ba406 1202 end if;
1203
1204 Analyze_And_Resolve (N, Standard_Boolean);
1205 end Constrained;
1206
1207 ---------------
1208 -- Copy_Sign --
1209 ---------------
1210
1211 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1212 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1213
1214 when Attribute_Copy_Sign =>
1215 Expand_Fpt_Attribute_RR (N);
1216
1217 -----------
1218 -- Count --
1219 -----------
1220
1221 -- Transforms 'Count attribute into a call to the Count function
1222
1223 when Attribute_Count => Count :
1224 declare
1225 Entnam : Node_Id;
1226 Index : Node_Id;
1227 Name : Node_Id;
1228 Call : Node_Id;
1229 Conctyp : Entity_Id;
1230
1231 begin
1232 -- If the prefix is a member of an entry family, retrieve both
1233 -- entry name and index. For a simple entry there is no index.
1234
1235 if Nkind (Pref) = N_Indexed_Component then
1236 Entnam := Prefix (Pref);
1237 Index := First (Expressions (Pref));
1238 else
1239 Entnam := Pref;
1240 Index := Empty;
1241 end if;
1242
1243 -- Find the concurrent type in which this attribute is referenced
1244 -- (there had better be one).
1245
1246 Conctyp := Current_Scope;
1247 while not Is_Concurrent_Type (Conctyp) loop
1248 Conctyp := Scope (Conctyp);
1249 end loop;
1250
1251 -- Protected case
1252
1253 if Is_Protected_Type (Conctyp) then
1254
1255 if Abort_Allowed
1e16c51c 1256 or else Restriction_Active (No_Entry_Queue) = False
ee6ba406 1257 or else Number_Entries (Conctyp) > 1
1258 then
1259 Name := New_Reference_To (RTE (RE_Protected_Count), Loc);
1260
1261 Call :=
1262 Make_Function_Call (Loc,
1263 Name => Name,
1264 Parameter_Associations => New_List (
1265 New_Reference_To (
1266 Object_Ref (
1267 Corresponding_Body (Parent (Conctyp))), Loc),
1268 Entry_Index_Expression (
1269 Loc, Entity (Entnam), Index, Scope (Entity (Entnam)))));
1270 else
1271 Name := New_Reference_To (RTE (RE_Protected_Count_Entry), Loc);
1272
1273 Call := Make_Function_Call (Loc,
1274 Name => Name,
1275 Parameter_Associations => New_List (
1276 New_Reference_To (
1277 Object_Ref (
1278 Corresponding_Body (Parent (Conctyp))), Loc)));
1279 end if;
1280
1281 -- Task case
1282
1283 else
1284 Call :=
1285 Make_Function_Call (Loc,
1286 Name => New_Reference_To (RTE (RE_Task_Count), Loc),
1287 Parameter_Associations => New_List (
1288 Entry_Index_Expression
1289 (Loc, Entity (Entnam), Index, Scope (Entity (Entnam)))));
1290 end if;
1291
1292 -- The call returns type Natural but the context is universal integer
1293 -- so any integer type is allowed. The attribute was already resolved
1294 -- so its Etype is the required result type. If the base type of the
1295 -- context type is other than Standard.Integer we put in a conversion
1296 -- to the required type. This can be a normal typed conversion since
1297 -- both input and output types of the conversion are integer types
1298
1299 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
1300 Rewrite (N, Convert_To (Typ, Call));
1301 else
1302 Rewrite (N, Call);
1303 end if;
1304
1305 Analyze_And_Resolve (N, Typ);
1306 end Count;
1307
1308 ---------------
1309 -- Elab_Body --
1310 ---------------
1311
1312 -- This processing is shared by Elab_Spec
1313
1314 -- What we do is to insert the following declarations
1315
1316 -- procedure tnn;
1317 -- pragma Import (C, enn, "name___elabb/s");
1318
1319 -- and then the Elab_Body/Spec attribute is replaced by a reference
1320 -- to this defining identifier.
1321
1322 when Attribute_Elab_Body |
1323 Attribute_Elab_Spec =>
1324
1325 Elab_Body : declare
1326 Ent : constant Entity_Id :=
1327 Make_Defining_Identifier (Loc,
1328 New_Internal_Name ('E'));
1329 Str : String_Id;
1330 Lang : Node_Id;
1331
1332 procedure Make_Elab_String (Nod : Node_Id);
1333 -- Given Nod, an identifier, or a selected component, put the
1334 -- image into the current string literal, with double underline
1335 -- between components.
1336
1337 procedure Make_Elab_String (Nod : Node_Id) is
1338 begin
1339 if Nkind (Nod) = N_Selected_Component then
1340 Make_Elab_String (Prefix (Nod));
1341 if Java_VM then
1342 Store_String_Char ('$');
1343 else
1344 Store_String_Char ('_');
1345 Store_String_Char ('_');
1346 end if;
1347
1348 Get_Name_String (Chars (Selector_Name (Nod)));
1349
1350 else
1351 pragma Assert (Nkind (Nod) = N_Identifier);
1352 Get_Name_String (Chars (Nod));
1353 end if;
1354
1355 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1356 end Make_Elab_String;
1357
1358 -- Start of processing for Elab_Body/Elab_Spec
1359
1360 begin
1361 -- First we need to prepare the string literal for the name of
1362 -- the elaboration routine to be referenced.
1363
1364 Start_String;
1365 Make_Elab_String (Pref);
1366
1367 if Java_VM then
1368 Store_String_Chars ("._elab");
1369 Lang := Make_Identifier (Loc, Name_Ada);
1370 else
1371 Store_String_Chars ("___elab");
1372 Lang := Make_Identifier (Loc, Name_C);
1373 end if;
1374
1375 if Id = Attribute_Elab_Body then
1376 Store_String_Char ('b');
1377 else
1378 Store_String_Char ('s');
1379 end if;
1380
1381 Str := End_String;
1382
1383 Insert_Actions (N, New_List (
1384 Make_Subprogram_Declaration (Loc,
1385 Specification =>
1386 Make_Procedure_Specification (Loc,
1387 Defining_Unit_Name => Ent)),
1388
1389 Make_Pragma (Loc,
1390 Chars => Name_Import,
1391 Pragma_Argument_Associations => New_List (
1392 Make_Pragma_Argument_Association (Loc,
1393 Expression => Lang),
1394
1395 Make_Pragma_Argument_Association (Loc,
1396 Expression =>
1397 Make_Identifier (Loc, Chars (Ent))),
1398
1399 Make_Pragma_Argument_Association (Loc,
1400 Expression =>
1401 Make_String_Literal (Loc, Str))))));
1402
1403 Set_Entity (N, Ent);
1404 Rewrite (N, New_Occurrence_Of (Ent, Loc));
1405 end Elab_Body;
1406
1407 ----------------
1408 -- Elaborated --
1409 ----------------
1410
1411 -- Elaborated is always True for preelaborated units, predefined
1412 -- units, pure units and units which have Elaborate_Body pragmas.
1413 -- These units have no elaboration entity.
1414
1415 -- Note: The Elaborated attribute is never passed through to Gigi
1416
1417 when Attribute_Elaborated => Elaborated : declare
1418 Ent : constant Entity_Id := Entity (Pref);
1419
1420 begin
1421 if Present (Elaboration_Entity (Ent)) then
1422 Rewrite (N,
1423 New_Occurrence_Of (Elaboration_Entity (Ent), Loc));
1424 else
1425 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
1426 end if;
1427 end Elaborated;
1428
1429 --------------
1430 -- Enum_Rep --
1431 --------------
1432
1433 when Attribute_Enum_Rep => Enum_Rep :
1434 begin
1435 -- X'Enum_Rep (Y) expands to
1436
1437 -- target-type (Y)
1438
1439 -- This is simply a direct conversion from the enumeration type
1440 -- to the target integer type, which is treated by Gigi as a normal
1441 -- integer conversion, treating the enumeration type as an integer,
1442 -- which is exactly what we want! We set Conversion_OK to make sure
1443 -- that the analyzer does not complain about what otherwise might
1444 -- be an illegal conversion.
1445
1446 if Is_Non_Empty_List (Exprs) then
1447 Rewrite (N,
1448 OK_Convert_To (Typ, Relocate_Node (First (Exprs))));
1449
1450 -- X'Enum_Rep where X is an enumeration literal is replaced by
1451 -- the literal value.
1452
1453 elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then
1454 Rewrite (N,
1455 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref))));
1456
9dfe12ae 1457 -- If this is a renaming of a literal, recover the representation
1458 -- of the original.
1459
1460 elsif Ekind (Entity (Pref)) = E_Constant
1461 and then Present (Renamed_Object (Entity (Pref)))
1462 and then
1463 Ekind (Entity (Renamed_Object (Entity (Pref))))
1464 = E_Enumeration_Literal
1465 then
1466 Rewrite (N,
1467 Make_Integer_Literal (Loc,
1468 Enumeration_Rep (Entity (Renamed_Object (Entity (Pref))))));
1469
ee6ba406 1470 -- X'Enum_Rep where X is an object does a direct unchecked conversion
1471 -- of the object value, as described for the type case above.
1472
1473 else
1474 Rewrite (N,
1475 OK_Convert_To (Typ, Relocate_Node (Pref)));
1476 end if;
1477
1478 Set_Etype (N, Typ);
1479 Analyze_And_Resolve (N, Typ);
1480
1481 end Enum_Rep;
1482
1483 --------------
1484 -- Exponent --
1485 --------------
1486
1487 -- Transforms 'Exponent into a call to the floating-point attribute
1488 -- function Exponent in Fat_xxx (where xxx is the root type)
1489
1490 when Attribute_Exponent =>
1491 Expand_Fpt_Attribute_R (N);
1492
1493 ------------------
1494 -- External_Tag --
1495 ------------------
1496
1497 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
1498
1499 when Attribute_External_Tag => External_Tag :
1500 begin
1501 Rewrite (N,
1502 Make_Function_Call (Loc,
1503 Name => New_Reference_To (RTE (RE_External_Tag), Loc),
1504 Parameter_Associations => New_List (
1505 Make_Attribute_Reference (Loc,
1506 Attribute_Name => Name_Tag,
1507 Prefix => Prefix (N)))));
1508
1509 Analyze_And_Resolve (N, Standard_String);
1510 end External_Tag;
1511
1512 -----------
1513 -- First --
1514 -----------
1515
1516 when Attribute_First => declare
1517 Ptyp : constant Entity_Id := Etype (Pref);
1518
1519 begin
1520 -- If the prefix type is a constrained packed array type which
1521 -- already has a Packed_Array_Type representation defined, then
1522 -- replace this attribute with a direct reference to 'First of the
1523 -- appropriate index subtype (since otherwise Gigi will try to give
1524 -- us the value of 'First for this implementation type).
1525
1526 if Is_Constrained_Packed_Array (Ptyp) then
1527 Rewrite (N,
1528 Make_Attribute_Reference (Loc,
1529 Attribute_Name => Name_First,
1530 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
1531 Analyze_And_Resolve (N, Typ);
1532
1533 elsif Is_Access_Type (Ptyp) then
1534 Apply_Access_Check (N);
1535 end if;
1536 end;
1537
1538 ---------------
1539 -- First_Bit --
1540 ---------------
1541
1542 -- We compute this if a component clause was present, otherwise
1543 -- we leave the computation up to Gigi, since we don't know what
1544 -- layout will be chosen.
1545
1546 when Attribute_First_Bit => First_Bit :
1547 declare
1548 CE : constant Entity_Id := Entity (Selector_Name (Pref));
1549
1550 begin
1551 if Known_Static_Component_Bit_Offset (CE) then
1552 Rewrite (N,
1553 Make_Integer_Literal (Loc,
1554 Component_Bit_Offset (CE) mod System_Storage_Unit));
1555
1556 Analyze_And_Resolve (N, Typ);
1557
1558 else
1559 Apply_Universal_Integer_Attribute_Checks (N);
1560 end if;
1561 end First_Bit;
1562
1563 -----------------
1564 -- Fixed_Value --
1565 -----------------
1566
1567 -- We transform:
1568
1569 -- fixtype'Fixed_Value (integer-value)
1570
1571 -- into
1572
1573 -- fixtype(integer-value)
1574
1575 -- we do all the required analysis of the conversion here, because
1576 -- we do not want this to go through the fixed-point conversion
1577 -- circuits. Note that gigi always treats fixed-point as equivalent
1578 -- to the corresponding integer type anyway.
1579
1580 when Attribute_Fixed_Value => Fixed_Value :
1581 begin
1582 Rewrite (N,
1583 Make_Type_Conversion (Loc,
1584 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
1585 Expression => Relocate_Node (First (Exprs))));
1586 Set_Etype (N, Entity (Pref));
1587 Set_Analyzed (N);
9dfe12ae 1588
1589 -- Note: it might appear that a properly analyzed unchecked conversion
1590 -- would be just fine here, but that's not the case, since the full
1591 -- range checks performed by the following call are critical!
1592
ee6ba406 1593 Apply_Type_Conversion_Checks (N);
1594 end Fixed_Value;
1595
1596 -----------
1597 -- Floor --
1598 -----------
1599
1600 -- Transforms 'Floor into a call to the floating-point attribute
1601 -- function Floor in Fat_xxx (where xxx is the root type)
1602
1603 when Attribute_Floor =>
1604 Expand_Fpt_Attribute_R (N);
1605
1606 ----------
1607 -- Fore --
1608 ----------
1609
1610 -- For the fixed-point type Typ:
1611
1612 -- Typ'Fore
1613
1614 -- expands into
1615
1616 -- Result_Type (System.Fore (Long_Long_Float (Type'First)),
1617 -- Long_Long_Float (Type'Last))
1618
1619 -- Note that we know that the type is a non-static subtype, or Fore
1620 -- would have itself been computed dynamically in Eval_Attribute.
1621
1622 when Attribute_Fore => Fore :
1623 declare
1624 Ptyp : constant Entity_Id := Etype (Pref);
1625
1626 begin
1627 Rewrite (N,
1628 Convert_To (Typ,
1629 Make_Function_Call (Loc,
1630 Name => New_Reference_To (RTE (RE_Fore), Loc),
1631
1632 Parameter_Associations => New_List (
1633 Convert_To (Standard_Long_Long_Float,
1634 Make_Attribute_Reference (Loc,
1635 Prefix => New_Reference_To (Ptyp, Loc),
1636 Attribute_Name => Name_First)),
1637
1638 Convert_To (Standard_Long_Long_Float,
1639 Make_Attribute_Reference (Loc,
1640 Prefix => New_Reference_To (Ptyp, Loc),
1641 Attribute_Name => Name_Last))))));
1642
1643 Analyze_And_Resolve (N, Typ);
1644 end Fore;
1645
1646 --------------
1647 -- Fraction --
1648 --------------
1649
1650 -- Transforms 'Fraction into a call to the floating-point attribute
1651 -- function Fraction in Fat_xxx (where xxx is the root type)
1652
1653 when Attribute_Fraction =>
1654 Expand_Fpt_Attribute_R (N);
1655
1656 --------------
1657 -- Identity --
1658 --------------
1659
1660 -- For an exception returns a reference to the exception data:
1661 -- Exception_Id!(Prefix'Reference)
1662
1663 -- For a task it returns a reference to the _task_id component of
1664 -- corresponding record:
1665
7f9be362 1666 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
ee6ba406 1667
1668 -- in Ada.Task_Identification.
1669
1670 when Attribute_Identity => Identity : declare
1671 Id_Kind : Entity_Id;
1672
1673 begin
1674 if Etype (Pref) = Standard_Exception_Type then
1675 Id_Kind := RTE (RE_Exception_Id);
1676
1677 if Present (Renamed_Object (Entity (Pref))) then
1678 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
1679 end if;
1680
1681 Rewrite (N,
1682 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
1683 else
7f9be362 1684 Id_Kind := RTE (RO_AT_Task_Id);
ee6ba406 1685
1686 Rewrite (N,
1687 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
1688 end if;
1689
1690 Analyze_And_Resolve (N, Id_Kind);
1691 end Identity;
1692
1693 -----------
1694 -- Image --
1695 -----------
1696
1697 -- Image attribute is handled in separate unit Exp_Imgv
1698
1699 when Attribute_Image =>
1700 Exp_Imgv.Expand_Image_Attribute (N);
1701
1702 ---------
1703 -- Img --
1704 ---------
1705
1706 -- X'Img is expanded to typ'Image (X), where typ is the type of X
1707
1708 when Attribute_Img => Img :
1709 begin
1710 Rewrite (N,
1711 Make_Attribute_Reference (Loc,
1712 Prefix => New_Reference_To (Etype (Pref), Loc),
1713 Attribute_Name => Name_Image,
1714 Expressions => New_List (Relocate_Node (Pref))));
1715
1716 Analyze_And_Resolve (N, Standard_String);
1717 end Img;
1718
1719 -----------
1720 -- Input --
1721 -----------
1722
1723 when Attribute_Input => Input : declare
1724 P_Type : constant Entity_Id := Entity (Pref);
1725 B_Type : constant Entity_Id := Base_Type (P_Type);
1726 U_Type : constant Entity_Id := Underlying_Type (P_Type);
1727 Strm : constant Node_Id := First (Exprs);
1728 Fname : Entity_Id;
1729 Decl : Node_Id;
1730 Call : Node_Id;
1731 Prag : Node_Id;
1732 Arg2 : Node_Id;
1733 Rfunc : Node_Id;
1734
1735 Cntrl : Node_Id := Empty;
1736 -- Value for controlling argument in call. Always Empty except in
1737 -- the dispatching (class-wide type) case, where it is a reference
1738 -- to the dummy object initialized to the right internal tag.
1739
d53a018a 1740 procedure Freeze_Stream_Subprogram (F : Entity_Id);
1741 -- The expansion of the attribute reference may generate a call to
1742 -- a user-defined stream subprogram that is frozen by the call. This
1743 -- can lead to access-before-elaboration problem if the reference
1744 -- appears in an object declaration and the subprogram body has not
1745 -- been seen. The freezing of the subprogram requires special code
1746 -- because it appears in an expanded context where expressions do
1747 -- not freeze their constituents.
1748
1749 ------------------------------
1750 -- Freeze_Stream_Subprogram --
1751 ------------------------------
1752
1753 procedure Freeze_Stream_Subprogram (F : Entity_Id) is
1754 Decl : constant Node_Id := Unit_Declaration_Node (F);
1755 Bod : Node_Id;
1756
1757 begin
1758 -- If this is user-defined subprogram, the corresponding
1759 -- stream function appears as a renaming-as-body, and the
1760 -- user subprogram must be retrieved by tree traversal.
1761
1762 if Present (Decl)
1763 and then Nkind (Decl) = N_Subprogram_Declaration
1764 and then Present (Corresponding_Body (Decl))
1765 then
1766 Bod := Corresponding_Body (Decl);
1767
1768 if Nkind (Unit_Declaration_Node (Bod)) =
1769 N_Subprogram_Renaming_Declaration
1770 then
1771 Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
1772 end if;
1773 end if;
1774 end Freeze_Stream_Subprogram;
1775
1776 -- Start of processing for Input
1777
ee6ba406 1778 begin
1779 -- If no underlying type, we have an error that will be diagnosed
1780 -- elsewhere, so here we just completely ignore the expansion.
1781
1782 if No (U_Type) then
1783 return;
1784 end if;
1785
1786 -- If there is a TSS for Input, just call it
1787
9dfe12ae 1788 Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
ee6ba406 1789
1790 if Present (Fname) then
1791 null;
1792
1793 else
1794 -- If there is a Stream_Convert pragma, use it, we rewrite
1795
1796 -- sourcetyp'Input (stream)
1797
1798 -- as
1799
1800 -- sourcetyp (streamread (strmtyp'Input (stream)));
1801
1802 -- where stmrearead is the given Read function that converts
1803 -- an argument of type strmtyp to type sourcetyp or a type
1804 -- from which it is derived. The extra conversion is required
1805 -- for the derived case.
1806
1807 Prag :=
1808 Get_Rep_Pragma
1809 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
1810
1811 if Present (Prag) then
1812 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
1813 Rfunc := Entity (Expression (Arg2));
1814
1815 Rewrite (N,
1816 Convert_To (B_Type,
1817 Make_Function_Call (Loc,
1818 Name => New_Occurrence_Of (Rfunc, Loc),
1819 Parameter_Associations => New_List (
1820 Make_Attribute_Reference (Loc,
1821 Prefix =>
1822 New_Occurrence_Of
1823 (Etype (First_Formal (Rfunc)), Loc),
1824 Attribute_Name => Name_Input,
1825 Expressions => Exprs)))));
1826
1827 Analyze_And_Resolve (N, B_Type);
1828 return;
1829
1830 -- Elementary types
1831
1832 elsif Is_Elementary_Type (U_Type) then
1833
1834 -- A special case arises if we have a defined _Read routine,
1835 -- since in this case we are required to call this routine.
1836
9dfe12ae 1837 if Present (TSS (Base_Type (U_Type), TSS_Stream_Read)) then
ee6ba406 1838 Build_Record_Or_Elementary_Input_Function
1839 (Loc, U_Type, Decl, Fname);
1840 Insert_Action (N, Decl);
1841
1842 -- For normal cases, we call the I_xxx routine directly
1843
1844 else
1845 Rewrite (N, Build_Elementary_Input_Call (N));
1846 Analyze_And_Resolve (N, P_Type);
1847 return;
1848 end if;
1849
1850 -- Array type case
1851
1852 elsif Is_Array_Type (U_Type) then
1853 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
1854 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
1855
1856 -- Dispatching case with class-wide type
1857
1858 elsif Is_Class_Wide_Type (P_Type) then
1859
1860 declare
1861 Rtyp : constant Entity_Id := Root_Type (P_Type);
1862 Dnn : Entity_Id;
1863 Decl : Node_Id;
1864
1865 begin
1866 -- Read the internal tag (RM 13.13.2(34)) and use it to
1867 -- initialize a dummy tag object:
1868
1869 -- Dnn : Ada.Tags.Tag
1870 -- := Internal_Tag (String'Input (Strm));
1871
1872 -- This dummy object is used only to provide a controlling
1873 -- argument for the eventual _Input call.
1874
1875 Dnn :=
1876 Make_Defining_Identifier (Loc,
1877 Chars => New_Internal_Name ('D'));
1878
1879 Decl :=
1880 Make_Object_Declaration (Loc,
1881 Defining_Identifier => Dnn,
1882 Object_Definition =>
1883 New_Occurrence_Of (RTE (RE_Tag), Loc),
1884 Expression =>
1885 Make_Function_Call (Loc,
1886 Name =>
1887 New_Occurrence_Of (RTE (RE_Internal_Tag), Loc),
1888 Parameter_Associations => New_List (
1889 Make_Attribute_Reference (Loc,
1890 Prefix =>
1891 New_Occurrence_Of (Standard_String, Loc),
1892 Attribute_Name => Name_Input,
1893 Expressions => New_List (
1894 Relocate_Node
1895 (Duplicate_Subexpr (Strm)))))));
1896
1897 Insert_Action (N, Decl);
1898
1899 -- Now we need to get the entity for the call, and construct
1900 -- a function call node, where we preset a reference to Dnn
1901 -- as the controlling argument (doing an unchecked
9dfe12ae 1902 -- conversion to the classwide tagged type to make it
1903 -- look like a real tagged object).
ee6ba406 1904
9dfe12ae 1905 Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
1906 Cntrl := Unchecked_Convert_To (P_Type,
ee6ba406 1907 New_Occurrence_Of (Dnn, Loc));
9dfe12ae 1908 Set_Etype (Cntrl, P_Type);
ee6ba406 1909 Set_Parent (Cntrl, N);
1910 end;
1911
1912 -- For tagged types, use the primitive Input function
1913
1914 elsif Is_Tagged_Type (U_Type) then
9dfe12ae 1915 Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
ee6ba406 1916
1917 -- All other record type cases, including protected records.
1918 -- The latter only arise for expander generated code for
1919 -- handling shared passive partition access.
1920
1921 else
1922 pragma Assert
1923 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
1924
00f91aef 1925 -- Ada 2005 (AI-216): Program_Error is raised when executing
1926 -- the default implementation of the Input attribute of an
1927 -- unchecked union type if the type lacks default discriminant
1928 -- values.
1929
1930 if Is_Unchecked_Union (Base_Type (U_Type))
1931 and then not Present (Discriminant_Constraint (U_Type))
1932 then
1933 Insert_Action (N,
1934 Make_Raise_Program_Error (Loc,
1935 Reason => PE_Unchecked_Union_Restriction));
1936
1937 return;
1938 end if;
1939
ee6ba406 1940 Build_Record_Or_Elementary_Input_Function
1941 (Loc, Base_Type (U_Type), Decl, Fname);
1942 Insert_Action (N, Decl);
d53a018a 1943
1944 if Nkind (Parent (N)) = N_Object_Declaration
1945 and then Is_Record_Type (U_Type)
1946 then
1947 -- The stream function may contain calls to user-defined
1948 -- Read procedures for individual components.
1949
1950 declare
1951 Comp : Entity_Id;
1952 Func : Entity_Id;
1953
1954 begin
1955 Comp := First_Component (U_Type);
1956 while Present (Comp) loop
1957 Func :=
1958 Find_Stream_Subprogram
1959 (Etype (Comp), TSS_Stream_Read);
1960
1961 if Present (Func) then
1962 Freeze_Stream_Subprogram (Func);
1963 end if;
1964
1965 Next_Component (Comp);
1966 end loop;
1967 end;
1968 end if;
ee6ba406 1969 end if;
1970 end if;
1971
1972 -- If we fall through, Fname is the function to be called. The
1973 -- result is obtained by calling the appropriate function, then
1974 -- converting the result. The conversion does a subtype check.
1975
1976 Call :=
1977 Make_Function_Call (Loc,
1978 Name => New_Occurrence_Of (Fname, Loc),
1979 Parameter_Associations => New_List (
1980 Relocate_Node (Strm)));
1981
1982 Set_Controlling_Argument (Call, Cntrl);
1983 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
1984 Analyze_And_Resolve (N, P_Type);
d53a018a 1985
1986 if Nkind (Parent (N)) = N_Object_Declaration then
1987 Freeze_Stream_Subprogram (Fname);
1988 end if;
ee6ba406 1989 end Input;
1990
1991 -------------------
1992 -- Integer_Value --
1993 -------------------
1994
1995 -- We transform
1996
1997 -- inttype'Fixed_Value (fixed-value)
1998
1999 -- into
2000
2001 -- inttype(integer-value))
2002
2003 -- we do all the required analysis of the conversion here, because
2004 -- we do not want this to go through the fixed-point conversion
2005 -- circuits. Note that gigi always treats fixed-point as equivalent
2006 -- to the corresponding integer type anyway.
2007
2008 when Attribute_Integer_Value => Integer_Value :
2009 begin
2010 Rewrite (N,
2011 Make_Type_Conversion (Loc,
2012 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2013 Expression => Relocate_Node (First (Exprs))));
2014 Set_Etype (N, Entity (Pref));
2015 Set_Analyzed (N);
9dfe12ae 2016
2017 -- Note: it might appear that a properly analyzed unchecked conversion
2018 -- would be just fine here, but that's not the case, since the full
2019 -- range checks performed by the following call are critical!
2020
ee6ba406 2021 Apply_Type_Conversion_Checks (N);
2022 end Integer_Value;
2023
2024 ----------
2025 -- Last --
2026 ----------
2027
2028 when Attribute_Last => declare
2029 Ptyp : constant Entity_Id := Etype (Pref);
2030
2031 begin
2032 -- If the prefix type is a constrained packed array type which
2033 -- already has a Packed_Array_Type representation defined, then
2034 -- replace this attribute with a direct reference to 'Last of the
2035 -- appropriate index subtype (since otherwise Gigi will try to give
2036 -- us the value of 'Last for this implementation type).
2037
2038 if Is_Constrained_Packed_Array (Ptyp) then
2039 Rewrite (N,
2040 Make_Attribute_Reference (Loc,
2041 Attribute_Name => Name_Last,
2042 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2043 Analyze_And_Resolve (N, Typ);
2044
2045 elsif Is_Access_Type (Ptyp) then
2046 Apply_Access_Check (N);
2047 end if;
2048 end;
2049
2050 --------------
2051 -- Last_Bit --
2052 --------------
2053
2054 -- We compute this if a component clause was present, otherwise
2055 -- we leave the computation up to Gigi, since we don't know what
2056 -- layout will be chosen.
2057
2058 when Attribute_Last_Bit => Last_Bit :
2059 declare
2060 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2061
2062 begin
2063 if Known_Static_Component_Bit_Offset (CE)
2064 and then Known_Static_Esize (CE)
2065 then
2066 Rewrite (N,
2067 Make_Integer_Literal (Loc,
2068 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
2069 + Esize (CE) - 1));
2070
2071 Analyze_And_Resolve (N, Typ);
2072
2073 else
2074 Apply_Universal_Integer_Attribute_Checks (N);
2075 end if;
2076 end Last_Bit;
2077
2078 ------------------
2079 -- Leading_Part --
2080 ------------------
2081
2082 -- Transforms 'Leading_Part into a call to the floating-point attribute
2083 -- function Leading_Part in Fat_xxx (where xxx is the root type)
2084
2085 -- Note: strictly, we should have special case code to deal with
2086 -- absurdly large positive arguments (greater than Integer'Last),
2087 -- which result in returning the first argument unchanged, but it
2088 -- hardly seems worth the effort. We raise constraint error for
2089 -- absurdly negative arguments which is fine.
2090
2091 when Attribute_Leading_Part =>
2092 Expand_Fpt_Attribute_RI (N);
2093
2094 ------------
2095 -- Length --
2096 ------------
2097
2098 when Attribute_Length => declare
2099 Ptyp : constant Entity_Id := Etype (Pref);
2100 Ityp : Entity_Id;
2101 Xnum : Uint;
2102
2103 begin
2104 -- Processing for packed array types
2105
2106 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
2107 Ityp := Get_Index_Subtype (N);
2108
2109 -- If the index type, Ityp, is an enumeration type with
2110 -- holes, then we calculate X'Length explicitly using
2111
2112 -- Typ'Max
2113 -- (0, Ityp'Pos (X'Last (N)) -
2114 -- Ityp'Pos (X'First (N)) + 1);
2115
2116 -- Since the bounds in the template are the representation
2117 -- values and gigi would get the wrong value.
2118
2119 if Is_Enumeration_Type (Ityp)
2120 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
2121 then
2122 if No (Exprs) then
2123 Xnum := Uint_1;
2124 else
2125 Xnum := Expr_Value (First (Expressions (N)));
2126 end if;
2127
2128 Rewrite (N,
2129 Make_Attribute_Reference (Loc,
2130 Prefix => New_Occurrence_Of (Typ, Loc),
2131 Attribute_Name => Name_Max,
2132 Expressions => New_List
2133 (Make_Integer_Literal (Loc, 0),
2134
2135 Make_Op_Add (Loc,
2136 Left_Opnd =>
2137 Make_Op_Subtract (Loc,
2138 Left_Opnd =>
2139 Make_Attribute_Reference (Loc,
2140 Prefix => New_Occurrence_Of (Ityp, Loc),
2141 Attribute_Name => Name_Pos,
2142
2143 Expressions => New_List (
2144 Make_Attribute_Reference (Loc,
2145 Prefix => Duplicate_Subexpr (Pref),
2146 Attribute_Name => Name_Last,
2147 Expressions => New_List (
2148 Make_Integer_Literal (Loc, Xnum))))),
2149
2150 Right_Opnd =>
2151 Make_Attribute_Reference (Loc,
2152 Prefix => New_Occurrence_Of (Ityp, Loc),
2153 Attribute_Name => Name_Pos,
2154
2155 Expressions => New_List (
2156 Make_Attribute_Reference (Loc,
9dfe12ae 2157 Prefix =>
2158 Duplicate_Subexpr_No_Checks (Pref),
ee6ba406 2159 Attribute_Name => Name_First,
2160 Expressions => New_List (
2161 Make_Integer_Literal (Loc, Xnum)))))),
2162
2163 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
2164
2165 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2166 return;
2167
2168 -- If the prefix type is a constrained packed array type which
2169 -- already has a Packed_Array_Type representation defined, then
2170 -- replace this attribute with a direct reference to 'Range_Length
2171 -- of the appropriate index subtype (since otherwise Gigi will try
2172 -- to give us the value of 'Length for this implementation type).
2173
2174 elsif Is_Constrained (Ptyp) then
2175 Rewrite (N,
2176 Make_Attribute_Reference (Loc,
2177 Attribute_Name => Name_Range_Length,
2178 Prefix => New_Reference_To (Ityp, Loc)));
2179 Analyze_And_Resolve (N, Typ);
2180 end if;
2181
2182 -- If we have a packed array that is not bit packed, which was
2183
2184 -- Access type case
2185
2186 elsif Is_Access_Type (Ptyp) then
2187 Apply_Access_Check (N);
2188
2189 -- If the designated type is a packed array type, then we
2190 -- convert the reference to:
2191
2192 -- typ'Max (0, 1 +
2193 -- xtyp'Pos (Pref'Last (Expr)) -
2194 -- xtyp'Pos (Pref'First (Expr)));
2195
2196 -- This is a bit complex, but it is the easiest thing to do
2197 -- that works in all cases including enum types with holes
2198 -- xtyp here is the appropriate index type.
2199
2200 declare
2201 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
2202 Xtyp : Entity_Id;
2203
2204 begin
2205 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
2206 Xtyp := Get_Index_Subtype (N);
2207
2208 Rewrite (N,
2209 Make_Attribute_Reference (Loc,
2210 Prefix => New_Occurrence_Of (Typ, Loc),
2211 Attribute_Name => Name_Max,
2212 Expressions => New_List (
2213 Make_Integer_Literal (Loc, 0),
2214
2215 Make_Op_Add (Loc,
2216 Make_Integer_Literal (Loc, 1),
2217 Make_Op_Subtract (Loc,
2218 Left_Opnd =>
2219 Make_Attribute_Reference (Loc,
2220 Prefix => New_Occurrence_Of (Xtyp, Loc),
2221 Attribute_Name => Name_Pos,
2222 Expressions => New_List (
2223 Make_Attribute_Reference (Loc,
2224 Prefix => Duplicate_Subexpr (Pref),
2225 Attribute_Name => Name_Last,
2226 Expressions =>
2227 New_Copy_List (Exprs)))),
2228
2229 Right_Opnd =>
2230 Make_Attribute_Reference (Loc,
2231 Prefix => New_Occurrence_Of (Xtyp, Loc),
2232 Attribute_Name => Name_Pos,
2233 Expressions => New_List (
2234 Make_Attribute_Reference (Loc,
9dfe12ae 2235 Prefix =>
2236 Duplicate_Subexpr_No_Checks (Pref),
ee6ba406 2237 Attribute_Name => Name_First,
2238 Expressions =>
2239 New_Copy_List (Exprs)))))))));
2240
2241 Analyze_And_Resolve (N, Typ);
2242 end if;
2243 end;
2244
2245 -- Otherwise leave it to gigi
2246
2247 else
2248 Apply_Universal_Integer_Attribute_Checks (N);
2249 end if;
2250 end;
2251
2252 -------------
2253 -- Machine --
2254 -------------
2255
2256 -- Transforms 'Machine into a call to the floating-point attribute
2257 -- function Machine in Fat_xxx (where xxx is the root type)
2258
2259 when Attribute_Machine =>
2260 Expand_Fpt_Attribute_R (N);
2261
2262 ------------------
2263 -- Machine_Size --
2264 ------------------
2265
2266 -- Machine_Size is equivalent to Object_Size, so transform it into
2267 -- Object_Size and that way Gigi never sees Machine_Size.
2268
2269 when Attribute_Machine_Size =>
2270 Rewrite (N,
2271 Make_Attribute_Reference (Loc,
2272 Prefix => Prefix (N),
2273 Attribute_Name => Name_Object_Size));
2274
2275 Analyze_And_Resolve (N, Typ);
2276
2277 --------------
2278 -- Mantissa --
2279 --------------
2280
2281 -- The only case that can get this far is the dynamic case of the
2282 -- old Ada 83 Mantissa attribute for the fixed-point case. For this
2283 -- case, we expand:
2284
2285 -- typ'Mantissa
2286
2287 -- into
2288
2289 -- ityp (System.Mantissa.Mantissa_Value
2290 -- (Integer'Integer_Value (typ'First),
2291 -- Integer'Integer_Value (typ'Last)));
2292
2293 when Attribute_Mantissa => Mantissa : declare
2294 Ptyp : constant Entity_Id := Etype (Pref);
2295
2296 begin
2297 Rewrite (N,
2298 Convert_To (Typ,
2299 Make_Function_Call (Loc,
2300 Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
2301
2302 Parameter_Associations => New_List (
2303
2304 Make_Attribute_Reference (Loc,
2305 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2306 Attribute_Name => Name_Integer_Value,
2307 Expressions => New_List (
2308
2309 Make_Attribute_Reference (Loc,
2310 Prefix => New_Occurrence_Of (Ptyp, Loc),
2311 Attribute_Name => Name_First))),
2312
2313 Make_Attribute_Reference (Loc,
2314 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2315 Attribute_Name => Name_Integer_Value,
2316 Expressions => New_List (
2317
2318 Make_Attribute_Reference (Loc,
2319 Prefix => New_Occurrence_Of (Ptyp, Loc),
2320 Attribute_Name => Name_Last)))))));
2321
2322 Analyze_And_Resolve (N, Typ);
2323 end Mantissa;
2324
2325 -----------
2326 -- Model --
2327 -----------
2328
2329 -- Transforms 'Model into a call to the floating-point attribute
2330 -- function Model in Fat_xxx (where xxx is the root type)
2331
2332 when Attribute_Model =>
2333 Expand_Fpt_Attribute_R (N);
2334
2335 -----------------
2336 -- Object_Size --
2337 -----------------
2338
2339 -- The processing for Object_Size shares the processing for Size
2340
2341 ------------
2342 -- Output --
2343 ------------
2344
2345 when Attribute_Output => Output : declare
2346 P_Type : constant Entity_Id := Entity (Pref);
ee6ba406 2347 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2348 Pname : Entity_Id;
2349 Decl : Node_Id;
2350 Prag : Node_Id;
2351 Arg3 : Node_Id;
2352 Wfunc : Node_Id;
2353
2354 begin
2355 -- If no underlying type, we have an error that will be diagnosed
2356 -- elsewhere, so here we just completely ignore the expansion.
2357
2358 if No (U_Type) then
2359 return;
2360 end if;
2361
2362 -- If TSS for Output is present, just call it
2363
9dfe12ae 2364 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
ee6ba406 2365
2366 if Present (Pname) then
2367 null;
2368
2369 else
2370 -- If there is a Stream_Convert pragma, use it, we rewrite
2371
2372 -- sourcetyp'Output (stream, Item)
2373
2374 -- as
2375
2376 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
2377
2378 -- where strmwrite is the given Write function that converts
2379 -- an argument of type sourcetyp or a type acctyp, from which
2380 -- it is derived to type strmtyp. The conversion to acttyp is
2381 -- required for the derived case.
2382
2383 Prag :=
2384 Get_Rep_Pragma
2385 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
2386
2387 if Present (Prag) then
2388 Arg3 :=
2389 Next (Next (First (Pragma_Argument_Associations (Prag))));
2390 Wfunc := Entity (Expression (Arg3));
2391
2392 Rewrite (N,
2393 Make_Attribute_Reference (Loc,
2394 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
2395 Attribute_Name => Name_Output,
2396 Expressions => New_List (
2397 Relocate_Node (First (Exprs)),
2398 Make_Function_Call (Loc,
2399 Name => New_Occurrence_Of (Wfunc, Loc),
2400 Parameter_Associations => New_List (
2401 Convert_To (Etype (First_Formal (Wfunc)),
2402 Relocate_Node (Next (First (Exprs)))))))));
2403
2404 Analyze (N);
2405 return;
2406
2407 -- For elementary types, we call the W_xxx routine directly.
2408 -- Note that the effect of Write and Output is identical for
2409 -- the case of an elementary type, since there are no
2410 -- discriminants or bounds.
2411
2412 elsif Is_Elementary_Type (U_Type) then
2413
2414 -- A special case arises if we have a defined _Write routine,
2415 -- since in this case we are required to call this routine.
2416
9dfe12ae 2417 if Present (TSS (Base_Type (U_Type), TSS_Stream_Write)) then
ee6ba406 2418 Build_Record_Or_Elementary_Output_Procedure
2419 (Loc, U_Type, Decl, Pname);
2420 Insert_Action (N, Decl);
2421
2422 -- For normal cases, we call the W_xxx routine directly
2423
2424 else
2425 Rewrite (N, Build_Elementary_Write_Call (N));
2426 Analyze (N);
2427 return;
2428 end if;
2429
2430 -- Array type case
2431
2432 elsif Is_Array_Type (U_Type) then
2433 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
2434 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2435
2436 -- Class-wide case, first output external tag, then dispatch
2437 -- to the appropriate primitive Output function (RM 13.13.2(31)).
2438
2439 elsif Is_Class_Wide_Type (P_Type) then
2440 Tag_Write : declare
2441 Strm : constant Node_Id := First (Exprs);
2442 Item : constant Node_Id := Next (Strm);
2443
2444 begin
2445 -- The code is:
2446 -- String'Output (Strm, External_Tag (Item'Tag))
2447
2448 Insert_Action (N,
2449 Make_Attribute_Reference (Loc,
2450 Prefix => New_Occurrence_Of (Standard_String, Loc),
2451 Attribute_Name => Name_Output,
2452 Expressions => New_List (
2453 Relocate_Node (Duplicate_Subexpr (Strm)),
2454 Make_Function_Call (Loc,
2455 Name =>
2456 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
2457 Parameter_Associations => New_List (
2458 Make_Attribute_Reference (Loc,
2459 Prefix =>
2460 Relocate_Node
2461 (Duplicate_Subexpr (Item, Name_Req => True)),
2462 Attribute_Name => Name_Tag))))));
2463 end Tag_Write;
2464
9dfe12ae 2465 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
ee6ba406 2466
2467 -- Tagged type case, use the primitive Output function
2468
2469 elsif Is_Tagged_Type (U_Type) then
9dfe12ae 2470 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
ee6ba406 2471
2472 -- All other record type cases, including protected records.
2473 -- The latter only arise for expander generated code for
2474 -- handling shared passive partition access.
2475
2476 else
2477 pragma Assert
2478 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2479
00f91aef 2480 -- Ada 2005 (AI-216): Program_Error is raised when executing
2481 -- the default implementation of the Output attribute of an
2482 -- unchecked union type if the type lacks default discriminant
2483 -- values.
2484
2485 if Is_Unchecked_Union (Base_Type (U_Type))
2486 and then not Present (Discriminant_Constraint (U_Type))
2487 then
2488 Insert_Action (N,
2489 Make_Raise_Program_Error (Loc,
2490 Reason => PE_Unchecked_Union_Restriction));
2491
2492 return;
2493 end if;
2494
ee6ba406 2495 Build_Record_Or_Elementary_Output_Procedure
2496 (Loc, Base_Type (U_Type), Decl, Pname);
2497 Insert_Action (N, Decl);
2498 end if;
2499 end if;
2500
2501 -- If we fall through, Pname is the name of the procedure to call
2502
2503 Rewrite_Stream_Proc_Call (Pname);
2504 end Output;
2505
2506 ---------
2507 -- Pos --
2508 ---------
2509
2510 -- For enumeration types with a standard representation, Pos is
2511 -- handled by Gigi.
2512
2513 -- For enumeration types, with a non-standard representation we
2514 -- generate a call to the _Rep_To_Pos function created when the
2515 -- type was frozen. The call has the form
2516
9dfe12ae 2517 -- _rep_to_pos (expr, flag)
ee6ba406 2518
9dfe12ae 2519 -- The parameter flag is True if range checks are enabled, causing
2520 -- Program_Error to be raised if the expression has an invalid
2521 -- representation, and False if range checks are suppressed.
ee6ba406 2522
2523 -- For integer types, Pos is equivalent to a simple integer
2524 -- conversion and we rewrite it as such
2525
2526 when Attribute_Pos => Pos :
2527 declare
2528 Etyp : Entity_Id := Base_Type (Entity (Pref));
2529
2530 begin
2531 -- Deal with zero/non-zero boolean values
2532
2533 if Is_Boolean_Type (Etyp) then
2534 Adjust_Condition (First (Exprs));
2535 Etyp := Standard_Boolean;
2536 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
2537 end if;
2538
2539 -- Case of enumeration type
2540
2541 if Is_Enumeration_Type (Etyp) then
2542
2543 -- Non-standard enumeration type (generate call)
2544
2545 if Present (Enum_Pos_To_Rep (Etyp)) then
9dfe12ae 2546 Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
ee6ba406 2547 Rewrite (N,
2548 Convert_To (Typ,
2549 Make_Function_Call (Loc,
2550 Name =>
9dfe12ae 2551 New_Reference_To (TSS (Etyp, TSS_Rep_To_Pos), Loc),
ee6ba406 2552 Parameter_Associations => Exprs)));
2553
2554 Analyze_And_Resolve (N, Typ);
2555
2556 -- Standard enumeration type (do universal integer check)
2557
2558 else
2559 Apply_Universal_Integer_Attribute_Checks (N);
2560 end if;
2561
2562 -- Deal with integer types (replace by conversion)
2563
2564 elsif Is_Integer_Type (Etyp) then
2565 Rewrite (N, Convert_To (Typ, First (Exprs)));
2566 Analyze_And_Resolve (N, Typ);
2567 end if;
2568
2569 end Pos;
2570
2571 --------------
2572 -- Position --
2573 --------------
2574
2575 -- We compute this if a component clause was present, otherwise
2576 -- we leave the computation up to Gigi, since we don't know what
2577 -- layout will be chosen.
2578
2579 when Attribute_Position => Position :
2580 declare
2581 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2582
2583 begin
2584 if Present (Component_Clause (CE)) then
2585 Rewrite (N,
2586 Make_Integer_Literal (Loc,
2587 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
2588 Analyze_And_Resolve (N, Typ);
2589
2590 else
2591 Apply_Universal_Integer_Attribute_Checks (N);
2592 end if;
2593 end Position;
2594
2595 ----------
2596 -- Pred --
2597 ----------
2598
2599 -- 1. Deal with enumeration types with holes
2600 -- 2. For floating-point, generate call to attribute function
2601 -- 3. For other cases, deal with constraint checking
2602
2603 when Attribute_Pred => Pred :
2604 declare
2605 Ptyp : constant Entity_Id := Base_Type (Etype (Pref));
2606
2607 begin
2608 -- For enumeration types with non-standard representations, we
2609 -- expand typ'Pred (x) into
2610
2611 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
2612
9dfe12ae 2613 -- If the representation is contiguous, we compute instead
2614 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
2615
ee6ba406 2616 if Is_Enumeration_Type (Ptyp)
2617 and then Present (Enum_Pos_To_Rep (Ptyp))
2618 then
9dfe12ae 2619 if Has_Contiguous_Rep (Ptyp) then
2620 Rewrite (N,
2621 Unchecked_Convert_To (Ptyp,
2622 Make_Op_Add (Loc,
2623 Left_Opnd =>
2624 Make_Integer_Literal (Loc,
2625 Enumeration_Rep (First_Literal (Ptyp))),
2626 Right_Opnd =>
2627 Make_Function_Call (Loc,
2628 Name =>
2629 New_Reference_To
2630 (TSS (Ptyp, TSS_Rep_To_Pos), Loc),
2631
2632 Parameter_Associations =>
2633 New_List (
2634 Unchecked_Convert_To (Ptyp,
2635 Make_Op_Subtract (Loc,
2636 Left_Opnd =>
2637 Unchecked_Convert_To (Standard_Integer,
2638 Relocate_Node (First (Exprs))),
2639 Right_Opnd =>
2640 Make_Integer_Literal (Loc, 1))),
2641 Rep_To_Pos_Flag (Ptyp, Loc))))));
ee6ba406 2642
9dfe12ae 2643 else
2644 -- Add Boolean parameter True, to request program errror if
2645 -- we have a bad representation on our hands. If checks are
2646 -- suppressed, then add False instead
ee6ba406 2647
9dfe12ae 2648 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
2649 Rewrite (N,
2650 Make_Indexed_Component (Loc,
2651 Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc),
2652 Expressions => New_List (
2653 Make_Op_Subtract (Loc,
ee6ba406 2654 Left_Opnd =>
2655 Make_Function_Call (Loc,
2656 Name =>
9dfe12ae 2657 New_Reference_To (TSS (Ptyp, TSS_Rep_To_Pos), Loc),
2658 Parameter_Associations => Exprs),
ee6ba406 2659 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
9dfe12ae 2660 end if;
ee6ba406 2661
2662 Analyze_And_Resolve (N, Typ);
2663
2664 -- For floating-point, we transform 'Pred into a call to the Pred
2665 -- floating-point attribute function in Fat_xxx (xxx is root type)
2666
2667 elsif Is_Floating_Point_Type (Ptyp) then
2668 Expand_Fpt_Attribute_R (N);
2669 Analyze_And_Resolve (N, Typ);
2670
2671 -- For modular types, nothing to do (no overflow, since wraps)
2672
2673 elsif Is_Modular_Integer_Type (Ptyp) then
2674 null;
2675
2676 -- For other types, if range checking is enabled, we must generate
2677 -- a check if overflow checking is enabled.
2678
2679 elsif not Overflow_Checks_Suppressed (Ptyp) then
2680 Expand_Pred_Succ (N);
2681 end if;
2682
2683 end Pred;
2684
2685 ------------------
2686 -- Range_Length --
2687 ------------------
2688
2689 when Attribute_Range_Length => Range_Length : declare
2690 P_Type : constant Entity_Id := Etype (Pref);
2691
2692 begin
2693 -- The only special processing required is for the case where
2694 -- Range_Length is applied to an enumeration type with holes.
2695 -- In this case we transform
2696
2697 -- X'Range_Length
2698
2699 -- to
2700
2701 -- X'Pos (X'Last) - X'Pos (X'First) + 1
2702
2703 -- So that the result reflects the proper Pos values instead
2704 -- of the underlying representations.
2705
2706 if Is_Enumeration_Type (P_Type)
2707 and then Has_Non_Standard_Rep (P_Type)
2708 then
2709 Rewrite (N,
2710 Make_Op_Add (Loc,
2711 Left_Opnd =>
2712 Make_Op_Subtract (Loc,
2713 Left_Opnd =>
2714 Make_Attribute_Reference (Loc,
2715 Attribute_Name => Name_Pos,
2716 Prefix => New_Occurrence_Of (P_Type, Loc),
2717 Expressions => New_List (
2718 Make_Attribute_Reference (Loc,
2719 Attribute_Name => Name_Last,
2720 Prefix => New_Occurrence_Of (P_Type, Loc)))),
2721
2722 Right_Opnd =>
2723 Make_Attribute_Reference (Loc,
2724 Attribute_Name => Name_Pos,
2725 Prefix => New_Occurrence_Of (P_Type, Loc),
2726 Expressions => New_List (
2727 Make_Attribute_Reference (Loc,
2728 Attribute_Name => Name_First,
2729 Prefix => New_Occurrence_Of (P_Type, Loc))))),
2730
2731 Right_Opnd =>
2732 Make_Integer_Literal (Loc, 1)));
2733
2734 Analyze_And_Resolve (N, Typ);
2735
2736 -- For all other cases, attribute is handled by Gigi, but we need
2737 -- to deal with the case of the range check on a universal integer.
2738
2739 else
2740 Apply_Universal_Integer_Attribute_Checks (N);
2741 end if;
2742
2743 end Range_Length;
2744
2745 ----------
2746 -- Read --
2747 ----------
2748
2749 when Attribute_Read => Read : declare
2750 P_Type : constant Entity_Id := Entity (Pref);
2751 B_Type : constant Entity_Id := Base_Type (P_Type);
2752 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2753 Pname : Entity_Id;
2754 Decl : Node_Id;
2755 Prag : Node_Id;
2756 Arg2 : Node_Id;
2757 Rfunc : Node_Id;
2758 Lhs : Node_Id;
2759 Rhs : Node_Id;
2760
2761 begin
2762 -- If no underlying type, we have an error that will be diagnosed
2763 -- elsewhere, so here we just completely ignore the expansion.
2764
2765 if No (U_Type) then
2766 return;
2767 end if;
2768
2769 -- The simple case, if there is a TSS for Read, just call it
2770
9dfe12ae 2771 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
ee6ba406 2772
2773 if Present (Pname) then
2774 null;
2775
2776 else
2777 -- If there is a Stream_Convert pragma, use it, we rewrite
2778
2779 -- sourcetyp'Read (stream, Item)
2780
2781 -- as
2782
2783 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
2784
2785 -- where strmread is the given Read function that converts
2786 -- an argument of type strmtyp to type sourcetyp or a type
2787 -- from which it is derived. The conversion to sourcetyp
2788 -- is required in the latter case.
2789
2790 -- A special case arises if Item is a type conversion in which
2791 -- case, we have to expand to:
2792
2793 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
2794
2795 -- where Itemx is the expression of the type conversion (i.e.
2796 -- the actual object), and typex is the type of Itemx.
2797
2798 Prag :=
2799 Get_Rep_Pragma
2800 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
2801
2802 if Present (Prag) then
2803 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
2804 Rfunc := Entity (Expression (Arg2));
2805 Lhs := Relocate_Node (Next (First (Exprs)));
2806 Rhs :=
2807 Convert_To (B_Type,
2808 Make_Function_Call (Loc,
2809 Name => New_Occurrence_Of (Rfunc, Loc),
2810 Parameter_Associations => New_List (
2811 Make_Attribute_Reference (Loc,
2812 Prefix =>
2813 New_Occurrence_Of
2814 (Etype (First_Formal (Rfunc)), Loc),
2815 Attribute_Name => Name_Input,
2816 Expressions => New_List (
2817 Relocate_Node (First (Exprs)))))));
2818
2819 if Nkind (Lhs) = N_Type_Conversion then
2820 Lhs := Expression (Lhs);
2821 Rhs := Convert_To (Etype (Lhs), Rhs);
2822 end if;
2823
2824 Rewrite (N,
2825 Make_Assignment_Statement (Loc,
9dfe12ae 2826 Name => Lhs,
ee6ba406 2827 Expression => Rhs));
2828 Set_Assignment_OK (Lhs);
2829 Analyze (N);
2830 return;
2831
2832 -- For elementary types, we call the I_xxx routine using the first
2833 -- parameter and then assign the result into the second parameter.
2834 -- We set Assignment_OK to deal with the conversion case.
2835
2836 elsif Is_Elementary_Type (U_Type) then
2837 declare
2838 Lhs : Node_Id;
2839 Rhs : Node_Id;
2840
2841 begin
2842 Lhs := Relocate_Node (Next (First (Exprs)));
2843 Rhs := Build_Elementary_Input_Call (N);
2844
2845 if Nkind (Lhs) = N_Type_Conversion then
2846 Lhs := Expression (Lhs);
2847 Rhs := Convert_To (Etype (Lhs), Rhs);
2848 end if;
2849
2850 Set_Assignment_OK (Lhs);
2851
2852 Rewrite (N,
2853 Make_Assignment_Statement (Loc,
2854 Name => Lhs,
2855 Expression => Rhs));
2856
2857 Analyze (N);
2858 return;
2859 end;
2860
2861 -- Array type case
2862
2863 elsif Is_Array_Type (U_Type) then
2864 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
2865 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2866
2867 -- Tagged type case, use the primitive Read function. Note that
2868 -- this will dispatch in the class-wide case which is what we want
2869
2870 elsif Is_Tagged_Type (U_Type) then
9dfe12ae 2871 Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
ee6ba406 2872
2873 -- All other record type cases, including protected records.
2874 -- The latter only arise for expander generated code for
2875 -- handling shared passive partition access.
2876
2877 else
2878 pragma Assert
2879 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2880
00f91aef 2881 -- Ada 2005 (AI-216): Program_Error is raised when executing
2882 -- the default implementation of the Read attribute of an
2883 -- Unchecked_Union type.
2884
2885 if Is_Unchecked_Union (Base_Type (U_Type)) then
2886 Insert_Action (N,
2887 Make_Raise_Program_Error (Loc,
2888 Reason => PE_Unchecked_Union_Restriction));
2889 end if;
2890
ee6ba406 2891 if Has_Discriminants (U_Type)
2892 and then Present
2893 (Discriminant_Default_Value (First_Discriminant (U_Type)))
2894 then
2895 Build_Mutable_Record_Read_Procedure
2896 (Loc, Base_Type (U_Type), Decl, Pname);
ee6ba406 2897 else
2898 Build_Record_Read_Procedure
2899 (Loc, Base_Type (U_Type), Decl, Pname);
2900 end if;
2901
2902 -- Suppress checks, uninitialized or otherwise invalid
2903 -- data does not cause constraint errors to be raised for
2904 -- a complete record read.
2905
2906 Insert_Action (N, Decl, All_Checks);
2907 end if;
2908 end if;
2909
2910 Rewrite_Stream_Proc_Call (Pname);
2911 end Read;
2912
2913 ---------------
2914 -- Remainder --
2915 ---------------
2916
2917 -- Transforms 'Remainder into a call to the floating-point attribute
2918 -- function Remainder in Fat_xxx (where xxx is the root type)
2919
2920 when Attribute_Remainder =>
2921 Expand_Fpt_Attribute_RR (N);
2922
2923 -----------
2924 -- Round --
2925 -----------
2926
2927 -- The handling of the Round attribute is quite delicate. The
2928 -- processing in Sem_Attr introduced a conversion to universal
2929 -- real, reflecting the semantics of Round, but we do not want
2930 -- anything to do with universal real at runtime, since this
2931 -- corresponds to using floating-point arithmetic.
2932
2933 -- What we have now is that the Etype of the Round attribute
2934 -- correctly indicates the final result type. The operand of
2935 -- the Round is the conversion to universal real, described
2936 -- above, and the operand of this conversion is the actual
2937 -- operand of Round, which may be the special case of a fixed
2938 -- point multiplication or division (Etype = universal fixed)
2939
2940 -- The exapander will expand first the operand of the conversion,
2941 -- then the conversion, and finally the round attribute itself,
2942 -- since we always work inside out. But we cannot simply process
2943 -- naively in this order. In the semantic world where universal
2944 -- fixed and real really exist and have infinite precision, there
2945 -- is no problem, but in the implementation world, where universal
2946 -- real is a floating-point type, we would get the wrong result.
2947
2948 -- So the approach is as follows. First, when expanding a multiply
2949 -- or divide whose type is universal fixed, we do nothing at all,
2950 -- instead deferring the operation till later.
2951
2952 -- The actual processing is done in Expand_N_Type_Conversion which
2953 -- handles the special case of Round by looking at its parent to
2954 -- see if it is a Round attribute, and if it is, handling the
2955 -- conversion (or its fixed multiply/divide child) in an appropriate
2956 -- manner.
2957
2958 -- This means that by the time we get to expanding the Round attribute
2959 -- itself, the Round is nothing more than a type conversion (and will
2960 -- often be a null type conversion), so we just replace it with the
2961 -- appropriate conversion operation.
2962
2963 when Attribute_Round =>
2964 Rewrite (N,
2965 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
2966 Analyze_And_Resolve (N);
2967
2968 --------------
2969 -- Rounding --
2970 --------------
2971
2972 -- Transforms 'Rounding into a call to the floating-point attribute
2973 -- function Rounding in Fat_xxx (where xxx is the root type)
2974
2975 when Attribute_Rounding =>
2976 Expand_Fpt_Attribute_R (N);
2977
2978 -------------
2979 -- Scaling --
2980 -------------
2981
2982 -- Transforms 'Scaling into a call to the floating-point attribute
2983 -- function Scaling in Fat_xxx (where xxx is the root type)
2984
2985 when Attribute_Scaling =>
2986 Expand_Fpt_Attribute_RI (N);
2987
2988 ----------
2989 -- Size --
2990 ----------
2991
2992 when Attribute_Size |
2993 Attribute_Object_Size |
2994 Attribute_Value_Size |
2995 Attribute_VADS_Size => Size :
2996
2997 declare
2998 Ptyp : constant Entity_Id := Etype (Pref);
ee6ba406 2999 Siz : Uint;
9dfe12ae 3000 New_Node : Node_Id;
ee6ba406 3001
3002 begin
3003 -- Processing for VADS_Size case. Note that this processing removes
3004 -- all traces of VADS_Size from the tree, and completes all required
3005 -- processing for VADS_Size by translating the attribute reference
3006 -- to an appropriate Size or Object_Size reference.
3007
3008 if Id = Attribute_VADS_Size
3009 or else (Use_VADS_Size and then Id = Attribute_Size)
3010 then
3011 -- If the size is specified, then we simply use the specified
3012 -- size. This applies to both types and objects. The size of an
3013 -- object can be specified in the following ways:
3014
3015 -- An explicit size object is given for an object
3016 -- A component size is specified for an indexed component
3017 -- A component clause is specified for a selected component
3018 -- The object is a component of a packed composite object
3019
3020 -- If the size is specified, then VADS_Size of an object
3021
3022 if (Is_Entity_Name (Pref)
3023 and then Present (Size_Clause (Entity (Pref))))
3024 or else
3025 (Nkind (Pref) = N_Component_Clause
3026 and then (Present (Component_Clause
3027 (Entity (Selector_Name (Pref))))
3028 or else Is_Packed (Etype (Prefix (Pref)))))
3029 or else
3030 (Nkind (Pref) = N_Indexed_Component
3031 and then (Component_Size (Etype (Prefix (Pref))) /= 0
3032 or else Is_Packed (Etype (Prefix (Pref)))))
3033 then
3034 Set_Attribute_Name (N, Name_Size);
3035
3036 -- Otherwise if we have an object rather than a type, then the
3037 -- VADS_Size attribute applies to the type of the object, rather
3038 -- than the object itself. This is one of the respects in which
3039 -- VADS_Size differs from Size.
3040
3041 else
3042 if (not Is_Entity_Name (Pref)
3043 or else not Is_Type (Entity (Pref)))
3044 and then (Is_Scalar_Type (Etype (Pref))
3045 or else Is_Constrained (Etype (Pref)))
3046 then
3047 Rewrite (Pref, New_Occurrence_Of (Etype (Pref), Loc));
3048 end if;
3049
3050 -- For a scalar type for which no size was
3051 -- explicitly given, VADS_Size means Object_Size. This is the
3052 -- other respect in which VADS_Size differs from Size.
3053
3054 if Is_Scalar_Type (Etype (Pref))
3055 and then No (Size_Clause (Etype (Pref)))
3056 then
3057 Set_Attribute_Name (N, Name_Object_Size);
3058
3059 -- In all other cases, Size and VADS_Size are the sane
3060
3061 else
3062 Set_Attribute_Name (N, Name_Size);
3063 end if;
3064 end if;
3065 end if;
3066
9dfe12ae 3067 -- For class-wide types, X'Class'Size is transformed into a
3068 -- direct reference to the Size of the class type, so that gigi
3069 -- does not have to deal with the X'Class'Size reference.
ee6ba406 3070
9dfe12ae 3071 if Is_Entity_Name (Pref)
3072 and then Is_Class_Wide_Type (Entity (Pref))
3073 then
3074 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
3075 return;
3076
3077 -- For x'Size applied to an object of a class wide type, transform
3078 -- X'Size into a call to the primitive operation _Size applied to X.
3079
3080 elsif Is_Class_Wide_Type (Ptyp) then
ee6ba406 3081 New_Node :=
3082 Make_Function_Call (Loc,
3083 Name => New_Reference_To
3084 (Find_Prim_Op (Ptyp, Name_uSize), Loc),
3085 Parameter_Associations => New_List (Pref));
3086
3087 if Typ /= Standard_Long_Long_Integer then
3088
3089 -- The context is a specific integer type with which the
3090 -- original attribute was compatible. The function has a
3091 -- specific type as well, so to preserve the compatibility
3092 -- we must convert explicitly.
3093
3094 New_Node := Convert_To (Typ, New_Node);
3095 end if;
3096
3097 Rewrite (N, New_Node);
3098 Analyze_And_Resolve (N, Typ);
3099 return;
3100
3101 -- For an array component, we can do Size in the front end
3102 -- if the component_size of the array is set.
3103
3104 elsif Nkind (Pref) = N_Indexed_Component then
3105 Siz := Component_Size (Etype (Prefix (Pref)));
3106
3107 -- For a record component, we can do Size in the front end
3108 -- if there is a component clause, or if the record is packed
3109 -- and the component's size is known at compile time.
3110
3111 elsif Nkind (Pref) = N_Selected_Component then
3112 declare
3113 Rec : constant Entity_Id := Etype (Prefix (Pref));
3114 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
3115
3116 begin
3117 if Present (Component_Clause (Comp)) then
3118 Siz := Esize (Comp);
3119
3120 elsif Is_Packed (Rec) then
3121 Siz := RM_Size (Ptyp);
3122
3123 else
3124 Apply_Universal_Integer_Attribute_Checks (N);
3125 return;
3126 end if;
3127 end;
3128
3129 -- All other cases are handled by Gigi
3130
3131 else
3132 Apply_Universal_Integer_Attribute_Checks (N);
3133
3134 -- If we have Size applied to a formal parameter, that is a
3135 -- packed array subtype, then apply size to the actual subtype.
3136
3137 if Is_Entity_Name (Pref)
3138 and then Is_Formal (Entity (Pref))
3139 and then Is_Array_Type (Etype (Pref))
3140 and then Is_Packed (Etype (Pref))
3141 then
3142 Rewrite (N,
3143 Make_Attribute_Reference (Loc,
3144 Prefix =>
3145 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
3146 Attribute_Name => Name_Size));
3147 Analyze_And_Resolve (N, Typ);
3148 end if;
3149
3150 return;
3151 end if;
3152
3153 -- Common processing for record and array component case
3154
3155 if Siz /= 0 then
3156 Rewrite (N,
3157 Make_Integer_Literal (Loc, Siz));
3158
3159 Analyze_And_Resolve (N, Typ);
3160
3161 -- The result is not a static expression
3162
3163 Set_Is_Static_Expression (N, False);
3164 end if;
3165 end Size;
3166
3167 ------------------
3168 -- Storage_Pool --
3169 ------------------
3170
3171 when Attribute_Storage_Pool =>
3172 Rewrite (N,
3173 Make_Type_Conversion (Loc,
3174 Subtype_Mark => New_Reference_To (Etype (N), Loc),
3175 Expression => New_Reference_To (Entity (N), Loc)));
3176 Analyze_And_Resolve (N, Typ);
3177
3178 ------------------
3179 -- Storage_Size --
3180 ------------------
3181
3182 when Attribute_Storage_Size => Storage_Size :
3183 declare
3184 Ptyp : constant Entity_Id := Etype (Pref);
3185
3186 begin
3187 -- Access type case, always go to the root type
3188
3189 -- The case of access types results in a value of zero for the case
3190 -- where no storage size attribute clause has been given. If a
3191 -- storage size has been given, then the attribute is converted
3192 -- to a reference to the variable used to hold this value.
3193
3194 if Is_Access_Type (Ptyp) then
3195 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
3196 Rewrite (N,
3197 Make_Attribute_Reference (Loc,
3198 Prefix => New_Reference_To (Typ, Loc),
3199 Attribute_Name => Name_Max,
3200 Expressions => New_List (
3201 Make_Integer_Literal (Loc, 0),
3202 Convert_To (Typ,
3203 New_Reference_To
3204 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
3205
3206 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
3207 Rewrite (N,
3208 OK_Convert_To (Typ,
3209 Make_Function_Call (Loc,
9dfe12ae 3210 Name =>
3211 New_Reference_To
3212 (Find_Prim_Op
3213 (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
3214 Attribute_Name (N)),
3215 Loc),
ee6ba406 3216
3217 Parameter_Associations => New_List (New_Reference_To (
3218 Associated_Storage_Pool (Root_Type (Ptyp)), Loc)))));
3219 else
3220 Rewrite (N, Make_Integer_Literal (Loc, 0));
3221 end if;
3222
3223 Analyze_And_Resolve (N, Typ);
3224
3225 -- The case of a task type (an obsolescent feature) is handled the
3226 -- same way, seems as reasonable as anything, and it is what the
3227 -- ACVC tests (e.g. CD1009K) seem to expect.
3228
3229 -- If there is no Storage_Size variable, then we return the default
3230 -- task stack size, otherwise, expand a Storage_Size attribute as
3231 -- follows:
3232
3233 -- Typ (Adjust_Storage_Size (taskZ))
3234
3235 -- except for the case of a task object which has a Storage_Size
3236 -- pragma:
3237
3238 -- Typ (Adjust_Storage_Size (taskV!(name)._Size))
3239
3240 else
3241 if not Present (Storage_Size_Variable (Ptyp)) then
3242 Rewrite (N,
3243 Convert_To (Typ,
3244 Make_Function_Call (Loc,
3245 Name =>
3246 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc))));
3247
3248 else
3249 if not (Is_Entity_Name (Pref) and then
3250 Is_Task_Type (Entity (Pref))) and then
3251 Chars (Last_Entity (Corresponding_Record_Type (Ptyp))) =
3252 Name_uSize
3253 then
3254 Rewrite (N,
3255 Convert_To (Typ,
3256 Make_Function_Call (Loc,
3257 Name => New_Occurrence_Of (
3258 RTE (RE_Adjust_Storage_Size), Loc),
3259 Parameter_Associations =>
3260 New_List (
3261 Make_Selected_Component (Loc,
3262 Prefix =>
3263 Unchecked_Convert_To (
3264 Corresponding_Record_Type (Ptyp),
3265 New_Copy_Tree (Pref)),
3266 Selector_Name =>
3267 Make_Identifier (Loc, Name_uSize))))));
3268
3269 -- Task not having Storage_Size pragma
3270
3271 else
3272 Rewrite (N,
3273 Convert_To (Typ,
3274 Make_Function_Call (Loc,
3275 Name => New_Occurrence_Of (
3276 RTE (RE_Adjust_Storage_Size), Loc),
3277 Parameter_Associations =>
3278 New_List (
3279 New_Reference_To (
3280 Storage_Size_Variable (Ptyp), Loc)))));
3281 end if;
3282
3283 Analyze_And_Resolve (N, Typ);
3284 end if;
3285 end if;
3286 end Storage_Size;
3287
3288 ----------
3289 -- Succ --
3290 ----------
3291
3292 -- 1. Deal with enumeration types with holes
3293 -- 2. For floating-point, generate call to attribute function
3294 -- 3. For other cases, deal with constraint checking
3295
3296 when Attribute_Succ => Succ :
3297 declare
3298 Ptyp : constant Entity_Id := Base_Type (Etype (Pref));
3299
3300 begin
3301 -- For enumeration types with non-standard representations, we
3302 -- expand typ'Succ (x) into
3303
3304 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
3305
9dfe12ae 3306 -- If the representation is contiguous, we compute instead
3307 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
3308
ee6ba406 3309 if Is_Enumeration_Type (Ptyp)
3310 and then Present (Enum_Pos_To_Rep (Ptyp))
3311 then
9dfe12ae 3312 if Has_Contiguous_Rep (Ptyp) then
3313 Rewrite (N,
3314 Unchecked_Convert_To (Ptyp,
3315 Make_Op_Add (Loc,
3316 Left_Opnd =>
3317 Make_Integer_Literal (Loc,
3318 Enumeration_Rep (First_Literal (Ptyp))),
3319 Right_Opnd =>
3320 Make_Function_Call (Loc,
3321 Name =>
3322 New_Reference_To
3323 (TSS (Ptyp, TSS_Rep_To_Pos), Loc),
3324
3325 Parameter_Associations =>
3326 New_List (
3327 Unchecked_Convert_To (Ptyp,
3328 Make_Op_Add (Loc,
3329 Left_Opnd =>
3330 Unchecked_Convert_To (Standard_Integer,
3331 Relocate_Node (First (Exprs))),
3332 Right_Opnd =>
3333 Make_Integer_Literal (Loc, 1))),
3334 Rep_To_Pos_Flag (Ptyp, Loc))))));
3335 else
3336 -- Add Boolean parameter True, to request program errror if
3337 -- we have a bad representation on our hands. Add False if
3338 -- checks are suppressed.
ee6ba406 3339
9dfe12ae 3340 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
3341 Rewrite (N,
3342 Make_Indexed_Component (Loc,
3343 Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc),
3344 Expressions => New_List (
3345 Make_Op_Add (Loc,
3346 Left_Opnd =>
3347 Make_Function_Call (Loc,
3348 Name =>
3349 New_Reference_To
3350 (TSS (Ptyp, TSS_Rep_To_Pos), Loc),
3351 Parameter_Associations => Exprs),
3352 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3353 end if;
ee6ba406 3354
3355 Analyze_And_Resolve (N, Typ);
3356
3357 -- For floating-point, we transform 'Succ into a call to the Succ
3358 -- floating-point attribute function in Fat_xxx (xxx is root type)
3359
3360 elsif Is_Floating_Point_Type (Ptyp) then
3361 Expand_Fpt_Attribute_R (N);
3362 Analyze_And_Resolve (N, Typ);
3363
3364 -- For modular types, nothing to do (no overflow, since wraps)
3365
3366 elsif Is_Modular_Integer_Type (Ptyp) then
3367 null;
3368
3369 -- For other types, if range checking is enabled, we must generate
3370 -- a check if overflow checking is enabled.
3371
3372 elsif not Overflow_Checks_Suppressed (Ptyp) then
3373 Expand_Pred_Succ (N);
3374 end if;
3375 end Succ;
3376
3377 ---------
3378 -- Tag --
3379 ---------
3380
3381 -- Transforms X'Tag into a direct reference to the tag of X
3382
3383 when Attribute_Tag => Tag :
3384 declare
3385 Ttyp : Entity_Id;
3386 Prefix_Is_Type : Boolean;
3387
3388 begin
3389 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
3390 Ttyp := Entity (Pref);
3391 Prefix_Is_Type := True;
3392 else
3393 Ttyp := Etype (Pref);
3394 Prefix_Is_Type := False;
3395 end if;
3396
3397 if Is_Class_Wide_Type (Ttyp) then
3398 Ttyp := Root_Type (Ttyp);
3399 end if;
3400
3401 Ttyp := Underlying_Type (Ttyp);
3402
3403 if Prefix_Is_Type then
1d7e0b5b 3404
3405 -- For JGNAT we leave the type attribute unexpanded because
3406 -- there's not a dispatching table to reference.
3407
3408 if not Java_VM then
3409 Rewrite (N,
3410 Unchecked_Convert_To (RTE (RE_Tag),
3411 New_Reference_To (Access_Disp_Table (Ttyp), Loc)));
3412 Analyze_And_Resolve (N, RTE (RE_Tag));
3413 end if;
ee6ba406 3414
3415 else
3416 Rewrite (N,
3417 Make_Selected_Component (Loc,
3418 Prefix => Relocate_Node (Pref),
3419 Selector_Name =>
3420 New_Reference_To (Tag_Component (Ttyp), Loc)));
1d7e0b5b 3421 Analyze_And_Resolve (N, RTE (RE_Tag));
ee6ba406 3422 end if;
ee6ba406 3423 end Tag;
3424
3425 ----------------
3426 -- Terminated --
3427 ----------------
3428
3429 -- Transforms 'Terminated attribute into a call to Terminated function.
3430
3431 when Attribute_Terminated => Terminated :
3432 begin
3433 if Restricted_Profile then
3434 Rewrite (N,
3435 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
3436
3437 else
3438 Rewrite (N,
3439 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
3440 end if;
3441
3442 Analyze_And_Resolve (N, Standard_Boolean);
3443 end Terminated;
3444
3445 ----------------
3446 -- To_Address --
3447 ----------------
3448
3449 -- Transforms System'To_Address (X) into unchecked conversion
3450 -- from (integral) type of X to type address.
3451
3452 when Attribute_To_Address =>
3453 Rewrite (N,
3454 Unchecked_Convert_To (RTE (RE_Address),
3455 Relocate_Node (First (Exprs))));
3456 Analyze_And_Resolve (N, RTE (RE_Address));
3457
3458 ----------------
3459 -- Truncation --
3460 ----------------
3461
3462 -- Transforms 'Truncation into a call to the floating-point attribute
3463 -- function Truncation in Fat_xxx (where xxx is the root type)
3464
3465 when Attribute_Truncation =>
3466 Expand_Fpt_Attribute_R (N);
3467
3468 -----------------------
3469 -- Unbiased_Rounding --
3470 -----------------------
3471
3472 -- Transforms 'Unbiased_Rounding into a call to the floating-point
3473 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
3474 -- root type)
3475
3476 when Attribute_Unbiased_Rounding =>
3477 Expand_Fpt_Attribute_R (N);
3478
3479 ----------------------
3480 -- Unchecked_Access --
3481 ----------------------
3482
3483 when Attribute_Unchecked_Access =>
3484 Expand_Access_To_Type (N);
3485
3486 -----------------
3487 -- UET_Address --
3488 -----------------
3489
3490 when Attribute_UET_Address => UET_Address : declare
3491 Ent : constant Entity_Id :=
3492 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3493
3494 begin
3495 Insert_Action (N,
3496 Make_Object_Declaration (Loc,
3497 Defining_Identifier => Ent,
3498 Aliased_Present => True,
3499 Object_Definition =>
3500 New_Occurrence_Of (RTE (RE_Address), Loc)));
3501
3502 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
3503 -- in normal external form.
3504
3505 Get_External_Unit_Name_String (Get_Unit_Name (Pref));
3506 Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
3507 Name_Len := Name_Len + 7;
3508 Name_Buffer (1 .. 7) := "__gnat_";
3509 Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP";
3510 Name_Len := Name_Len + 5;
3511
3512 Set_Is_Imported (Ent);
3513 Set_Interface_Name (Ent,
3514 Make_String_Literal (Loc,
3515 Strval => String_From_Name_Buffer));
3516
3517 Rewrite (N,
3518 Make_Attribute_Reference (Loc,
3519 Prefix => New_Occurrence_Of (Ent, Loc),
3520 Attribute_Name => Name_Address));
3521
3522 Analyze_And_Resolve (N, Typ);
3523 end UET_Address;
3524
3525 -------------------------
3526 -- Unrestricted_Access --
3527 -------------------------
3528
3529 when Attribute_Unrestricted_Access =>
3530 Expand_Access_To_Type (N);
3531
3532 ---------------
3533 -- VADS_Size --
3534 ---------------
3535
3536 -- The processing for VADS_Size is shared with Size
3537
3538 ---------
3539 -- Val --
3540 ---------
3541
3542 -- For enumeration types with a standard representation, and for all
3543 -- other types, Val is handled by Gigi. For enumeration types with
3544 -- a non-standard representation we use the _Pos_To_Rep array that
3545 -- was created when the type was frozen.
3546
3547 when Attribute_Val => Val :
3548 declare
3549 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
3550
3551 begin
3552 if Is_Enumeration_Type (Etyp)
3553 and then Present (Enum_Pos_To_Rep (Etyp))
3554 then
9dfe12ae 3555 if Has_Contiguous_Rep (Etyp) then
3556 declare
3557 Rep_Node : constant Node_Id :=
3558 Unchecked_Convert_To (Etyp,
3559 Make_Op_Add (Loc,
3560 Left_Opnd =>
3561 Make_Integer_Literal (Loc,
3562 Enumeration_Rep (First_Literal (Etyp))),
3563 Right_Opnd =>
3564 (Convert_To (Standard_Integer,
3565 Relocate_Node (First (Exprs))))));
3566
3567 begin
3568 Rewrite (N,
3569 Unchecked_Convert_To (Etyp,
3570 Make_Op_Add (Loc,
3571 Left_Opnd =>
3572 Make_Integer_Literal (Loc,
3573 Enumeration_Rep (First_Literal (Etyp))),
3574 Right_Opnd =>
3575 Make_Function_Call (Loc,
3576 Name =>
3577 New_Reference_To
3578 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3579 Parameter_Associations => New_List (
3580 Rep_Node,
3581 Rep_To_Pos_Flag (Etyp, Loc))))));
3582 end;
3583
3584 else
3585 Rewrite (N,
3586 Make_Indexed_Component (Loc,
3587 Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc),
3588 Expressions => New_List (
3589 Convert_To (Standard_Integer,
3590 Relocate_Node (First (Exprs))))));
3591 end if;
ee6ba406 3592
3593 Analyze_And_Resolve (N, Typ);
3594 end if;
3595 end Val;
3596
3597 -----------
3598 -- Valid --
3599 -----------
3600
3601 -- The code for valid is dependent on the particular types involved.
3602 -- See separate sections below for the generated code in each case.
3603
3604 when Attribute_Valid => Valid :
3605 declare
3606 Ptyp : constant Entity_Id := Etype (Pref);
9dfe12ae 3607 Btyp : Entity_Id := Base_Type (Ptyp);
ee6ba406 3608 Tst : Node_Id;
3609
9dfe12ae 3610 Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
3611 -- Save the validity checking mode. We always turn off validity
3612 -- checking during process of 'Valid since this is one place
3613 -- where we do not want the implicit validity checks to intefere
3614 -- with the explicit validity check that the programmer is doing.
3615
ee6ba406 3616 function Make_Range_Test return Node_Id;
3617 -- Build the code for a range test of the form
3618 -- Btyp!(Pref) >= Btyp!(Ptyp'First)
3619 -- and then
3620 -- Btyp!(Pref) <= Btyp!(Ptyp'Last)
3621
9dfe12ae 3622 ---------------------
3623 -- Make_Range_Test --
3624 ---------------------
3625
ee6ba406 3626 function Make_Range_Test return Node_Id is
3627 begin
3628 return
3629 Make_And_Then (Loc,
3630 Left_Opnd =>
3631 Make_Op_Ge (Loc,
3632 Left_Opnd =>
3633 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3634
3635 Right_Opnd =>
3636 Unchecked_Convert_To (Btyp,
3637 Make_Attribute_Reference (Loc,
3638 Prefix => New_Occurrence_Of (Ptyp, Loc),
3639 Attribute_Name => Name_First))),
3640
3641 Right_Opnd =>
3642 Make_Op_Le (Loc,
3643 Left_Opnd =>
9dfe12ae 3644 Unchecked_Convert_To (Btyp,
3645 Duplicate_Subexpr_No_Checks (Pref)),
ee6ba406 3646
3647 Right_Opnd =>
3648 Unchecked_Convert_To (Btyp,
3649 Make_Attribute_Reference (Loc,
3650 Prefix => New_Occurrence_Of (Ptyp, Loc),
3651 Attribute_Name => Name_Last))));
3652 end Make_Range_Test;
3653
3654 -- Start of processing for Attribute_Valid
3655
3656 begin
9dfe12ae 3657 -- Turn off validity checks. We do not want any implicit validity
3658 -- checks to intefere with the explicit check from the attribute
3659
3660 Validity_Checks_On := False;
3661
ee6ba406 3662 -- Floating-point case. This case is handled by the Valid attribute
3663 -- code in the floating-point attribute run-time library.
3664
3665 if Is_Floating_Point_Type (Ptyp) then
3666 declare
3667 Rtp : constant Entity_Id := Root_Type (Etype (Pref));
3668
3669 begin
9dfe12ae 3670 -- If the floating-point object might be unaligned, we need
3671 -- to call the special routine Unaligned_Valid, which makes
3672 -- the needed copy, being careful not to load the value into
3673 -- any floating-point register. The argument in this case is
3674 -- obj'Address (see Unchecked_Valid routine in s-fatgen.ads).
3675
3676 if Is_Possibly_Unaligned_Object (Pref) then
3677 Set_Attribute_Name (N, Name_Unaligned_Valid);
3678 Expand_Fpt_Attribute
3679 (N, Rtp, Name_Unaligned_Valid,
3680 New_List (
3681 Make_Attribute_Reference (Loc,
3682 Prefix => Relocate_Node (Pref),
3683 Attribute_Name => Name_Address)));
3684
3685 -- In the normal case where we are sure the object is aligned,
3686 -- we generate a caqll to Valid, and the argument in this case
3687 -- is obj'Unrestricted_Access (after converting obj to the
3688 -- right floating-point type).
3689
3690 else
3691 Expand_Fpt_Attribute
3692 (N, Rtp, Name_Valid,
3693 New_List (
3694 Make_Attribute_Reference (Loc,
3695 Prefix => Unchecked_Convert_To (Rtp, Pref),
3696 Attribute_Name => Name_Unrestricted_Access)));
3697 end if;
ee6ba406 3698
3699 -- One more task, we still need a range check. Required
3700 -- only if we have a constraint, since the Valid routine
3701 -- catches infinities properly (infinities are never valid).
3702
3703 -- The way we do the range check is simply to create the
3704 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
3705
3706 if not Subtypes_Statically_Match (Ptyp, Btyp) then
3707 Rewrite (N,
3708 Make_And_Then (Loc,
3709 Left_Opnd => Relocate_Node (N),
3710 Right_Opnd =>
3711 Make_In (Loc,
3712 Left_Opnd => Convert_To (Btyp, Pref),
3713 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
3714 end if;
3715 end;
3716
3717 -- Enumeration type with holes
3718
3719 -- For enumeration types with holes, the Pos value constructed by
3720 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
3721 -- second argument of False returns minus one for an invalid value,
3722 -- and the non-negative pos value for a valid value, so the
3723 -- expansion of X'Valid is simply:
3724
3725 -- type(X)'Pos (X) >= 0
3726
3727 -- We can't quite generate it that way because of the requirement
5329ca64 3728 -- for the non-standard second argument of False in the resulting
3729 -- rep_to_pos call, so we have to explicitly create:
ee6ba406 3730
3731 -- _rep_to_pos (X, False) >= 0
3732
3733 -- If we have an enumeration subtype, we also check that the
3734 -- value is in range:
3735
3736 -- _rep_to_pos (X, False) >= 0
3737 -- and then
5329ca64 3738 -- (X >= type(X)'First and then type(X)'Last <= X)
ee6ba406 3739
3740 elsif Is_Enumeration_Type (Ptyp)
3741 and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp)))
3742 then
3743 Tst :=
3744 Make_Op_Ge (Loc,
3745 Left_Opnd =>
3746 Make_Function_Call (Loc,
3747 Name =>
3748 New_Reference_To
9dfe12ae 3749 (TSS (Base_Type (Ptyp), TSS_Rep_To_Pos), Loc),
ee6ba406 3750 Parameter_Associations => New_List (
3751 Pref,
3752 New_Occurrence_Of (Standard_False, Loc))),
3753 Right_Opnd => Make_Integer_Literal (Loc, 0));
3754
3755 if Ptyp /= Btyp
3756 and then
3757 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
3758 or else
3759 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
3760 then
3761 -- The call to Make_Range_Test will create declarations
3762 -- that need a proper insertion point, but Pref is now
3763 -- attached to a node with no ancestor. Attach to tree
3764 -- even if it is to be rewritten below.
3765
3766 Set_Parent (Tst, Parent (N));
3767
3768 Tst :=
3769 Make_And_Then (Loc,
3770 Left_Opnd => Make_Range_Test,
3771 Right_Opnd => Tst);
3772 end if;
3773
3774 Rewrite (N, Tst);
3775
3776 -- Fortran convention booleans
3777
3778 -- For the very special case of Fortran convention booleans, the
3779 -- value is always valid, since it is an integer with the semantics
3780 -- that non-zero is true, and any value is permissible.
3781
3782 elsif Is_Boolean_Type (Ptyp)
3783 and then Convention (Ptyp) = Convention_Fortran
3784 then
3785 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3786
3787 -- For biased representations, we will be doing an unchecked
3788 -- conversion without unbiasing the result. That means that
3789 -- the range test has to take this into account, and the
3790 -- proper form of the test is:
3791
3792 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
3793
3794 elsif Has_Biased_Representation (Ptyp) then
3795 Btyp := RTE (RE_Unsigned_32);
3796 Rewrite (N,
3797 Make_Op_Lt (Loc,
3798 Left_Opnd =>
3799 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3800 Right_Opnd =>
3801 Unchecked_Convert_To (Btyp,
3802 Make_Attribute_Reference (Loc,
3803 Prefix => New_Occurrence_Of (Ptyp, Loc),
3804 Attribute_Name => Name_Range_Length))));
3805
3806 -- For all other scalar types, what we want logically is a
3807 -- range test:
3808
3809 -- X in type(X)'First .. type(X)'Last
3810
3811 -- But that's precisely what won't work because of possible
3812 -- unwanted optimization (and indeed the basic motivation for
5329ca64 3813 -- the Valid attribute is exactly that this test does not work!)
ee6ba406 3814 -- What will work is:
3815
3816 -- Btyp!(X) >= Btyp!(type(X)'First)
3817 -- and then
3818 -- Btyp!(X) <= Btyp!(type(X)'Last)
3819
3820 -- where Btyp is an integer type large enough to cover the full
3821 -- range of possible stored values (i.e. it is chosen on the basis
3822 -- of the size of the type, not the range of the values). We write
3823 -- this as two tests, rather than a range check, so that static
3824 -- evaluation will easily remove either or both of the checks if
3825 -- they can be -statically determined to be true (this happens
3826 -- when the type of X is static and the range extends to the full
3827 -- range of stored values).
3828
3829 -- Unsigned types. Note: it is safe to consider only whether the
3830 -- subtype is unsigned, since we will in that case be doing all
3831 -- unsigned comparisons based on the subtype range. Since we use
3832 -- the actual subtype object size, this is appropriate.
3833
3834 -- For example, if we have
3835
3836 -- subtype x is integer range 1 .. 200;
3837 -- for x'Object_Size use 8;
3838
3839 -- Now the base type is signed, but objects of this type are 8
3840 -- bits unsigned, and doing an unsigned test of the range 1 to
3841 -- 200 is correct, even though a value greater than 127 looks
3842 -- signed to a signed comparison.
3843
3844 elsif Is_Unsigned_Type (Ptyp) then
3845 if Esize (Ptyp) <= 32 then
3846 Btyp := RTE (RE_Unsigned_32);
3847 else
3848 Btyp := RTE (RE_Unsigned_64);
3849 end if;
3850
3851 Rewrite (N, Make_Range_Test);
3852
3853 -- Signed types
3854
3855 else
3856 if Esize (Ptyp) <= Esize (Standard_Integer) then
3857 Btyp := Standard_Integer;
3858 else
3859 Btyp := Universal_Integer;
3860 end if;
3861
3862 Rewrite (N, Make_Range_Test);
3863 end if;
3864
3865 Analyze_And_Resolve (N, Standard_Boolean);
9dfe12ae 3866 Validity_Checks_On := Save_Validity_Checks_On;
ee6ba406 3867 end Valid;
3868
3869 -----------
3870 -- Value --
3871 -----------
3872
3873 -- Value attribute is handled in separate unti Exp_Imgv
3874
3875 when Attribute_Value =>
3876 Exp_Imgv.Expand_Value_Attribute (N);
3877
3878 -----------------
3879 -- Value_Size --
3880 -----------------
3881
3882 -- The processing for Value_Size shares the processing for Size
3883
3884 -------------
3885 -- Version --
3886 -------------
3887
3888 -- The processing for Version shares the processing for Body_Version
3889
3890 ----------------
3891 -- Wide_Image --
3892 ----------------
3893
3894 -- We expand typ'Wide_Image (X) into
3895
3896 -- String_To_Wide_String
3897 -- (typ'Image (X), Wide_Character_Encoding_Method)
3898
3899 -- This works in all cases because String_To_Wide_String converts any
3900 -- wide character escape sequences resulting from the Image call to the
3901 -- proper Wide_Character equivalent
3902
3903 -- not quite right for typ = Wide_Character ???
3904
3905 when Attribute_Wide_Image => Wide_Image :
3906 begin
3907 Rewrite (N,
3908 Make_Function_Call (Loc,
3909 Name => New_Reference_To (RTE (RE_String_To_Wide_String), Loc),
3910 Parameter_Associations => New_List (
3911 Make_Attribute_Reference (Loc,
3912 Prefix => Pref,
3913 Attribute_Name => Name_Image,
3914 Expressions => Exprs),
3915
3916 Make_Integer_Literal (Loc,
3917 Intval => Int (Wide_Character_Encoding_Method)))));
3918
3919 Analyze_And_Resolve (N, Standard_Wide_String);
3920 end Wide_Image;
3921
3922 ----------------
3923 -- Wide_Value --
3924 ----------------
3925
3926 -- We expand typ'Wide_Value (X) into
3927
3928 -- typ'Value
3929 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
3930
3931 -- Wide_String_To_String is a runtime function that converts its wide
3932 -- string argument to String, converting any non-translatable characters
3933 -- into appropriate escape sequences. This preserves the required
3934 -- semantics of Wide_Value in all cases, and results in a very simple
3935 -- implementation approach.
3936
3937 -- It's not quite right where typ = Wide_Character, because the encoding
3938 -- method may not cover the whole character type ???
3939
3940 when Attribute_Wide_Value => Wide_Value :
3941 begin
3942 Rewrite (N,
3943 Make_Attribute_Reference (Loc,
3944 Prefix => Pref,
3945 Attribute_Name => Name_Value,
3946
3947 Expressions => New_List (
3948 Make_Function_Call (Loc,
3949 Name =>
3950 New_Reference_To (RTE (RE_Wide_String_To_String), Loc),
3951
3952 Parameter_Associations => New_List (
3953 Relocate_Node (First (Exprs)),
3954 Make_Integer_Literal (Loc,
3955 Intval => Int (Wide_Character_Encoding_Method)))))));
3956
3957 Analyze_And_Resolve (N, Typ);
3958 end Wide_Value;
3959
3960 ----------------
3961 -- Wide_Width --
3962 ----------------
3963
3964 -- Wide_Width attribute is handled in separate unit Exp_Imgv
3965
3966 when Attribute_Wide_Width =>
3967 Exp_Imgv.Expand_Width_Attribute (N, Wide => True);
3968
3969 -----------
3970 -- Width --
3971 -----------
3972
3973 -- Width attribute is handled in separate unit Exp_Imgv
3974
3975 when Attribute_Width =>
3976 Exp_Imgv.Expand_Width_Attribute (N, Wide => False);
3977
3978 -----------
3979 -- Write --
3980 -----------
3981
3982 when Attribute_Write => Write : declare
3983 P_Type : constant Entity_Id := Entity (Pref);
3984 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3985 Pname : Entity_Id;
3986 Decl : Node_Id;
3987 Prag : Node_Id;
3988 Arg3 : Node_Id;
3989 Wfunc : Node_Id;
3990
3991 begin
3992 -- If no underlying type, we have an error that will be diagnosed
3993 -- elsewhere, so here we just completely ignore the expansion.
3994
3995 if No (U_Type) then
3996 return;
3997 end if;
3998
3999 -- The simple case, if there is a TSS for Write, just call it
4000
9dfe12ae 4001 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
ee6ba406 4002
4003 if Present (Pname) then
4004 null;
4005
4006 else
4007 -- If there is a Stream_Convert pragma, use it, we rewrite
4008
4009 -- sourcetyp'Output (stream, Item)
4010
4011 -- as
4012
4013 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
4014
4015 -- where strmwrite is the given Write function that converts
4016 -- an argument of type sourcetyp or a type acctyp, from which
4017 -- it is derived to type strmtyp. The conversion to acttyp is
4018 -- required for the derived case.
4019
4020 Prag :=
4021 Get_Rep_Pragma
4022 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
4023
4024 if Present (Prag) then
4025 Arg3 :=
4026 Next (Next (First (Pragma_Argument_Associations (Prag))));
4027 Wfunc := Entity (Expression (Arg3));
4028
4029 Rewrite (N,
4030 Make_Attribute_Reference (Loc,
4031 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
4032 Attribute_Name => Name_Output,
4033 Expressions => New_List (
4034 Relocate_Node (First (Exprs)),
4035 Make_Function_Call (Loc,
4036 Name => New_Occurrence_Of (Wfunc, Loc),
4037 Parameter_Associations => New_List (
4038 Convert_To (Etype (First_Formal (Wfunc)),
4039 Relocate_Node (Next (First (Exprs)))))))));
4040
4041 Analyze (N);
4042 return;
4043
4044 -- For elementary types, we call the W_xxx routine directly
4045
4046 elsif Is_Elementary_Type (U_Type) then
4047 Rewrite (N, Build_Elementary_Write_Call (N));
4048 Analyze (N);
4049 return;
4050
4051 -- Array type case
4052
4053 elsif Is_Array_Type (U_Type) then
4054 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
4055 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
4056
4057 -- Tagged type case, use the primitive Write function. Note that
4058 -- this will dispatch in the class-wide case which is what we want
4059
4060 elsif Is_Tagged_Type (U_Type) then
9dfe12ae 4061 Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
ee6ba406 4062
4063 -- All other record type cases, including protected records.
4064 -- The latter only arise for expander generated code for
4065 -- handling shared passive partition access.
4066
4067 else
4068 pragma Assert
4069 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
4070
00f91aef 4071 -- Ada 2005 (AI-216): Program_Error is raised when executing
4072 -- the default implementation of the Write attribute of an
4073 -- Unchecked_Union type.
4074
4075 if Is_Unchecked_Union (Base_Type (U_Type)) then
4076 Insert_Action (N,
4077 Make_Raise_Program_Error (Loc,
4078 Reason => PE_Unchecked_Union_Restriction));
4079 end if;
4080
ee6ba406 4081 if Has_Discriminants (U_Type)
4082 and then Present
4083 (Discriminant_Default_Value (First_Discriminant (U_Type)))
4084 then
4085 Build_Mutable_Record_Write_Procedure
4086 (Loc, Base_Type (U_Type), Decl, Pname);
ee6ba406 4087 else
4088 Build_Record_Write_Procedure
4089 (Loc, Base_Type (U_Type), Decl, Pname);
4090 end if;
4091
4092 Insert_Action (N, Decl);
4093 end if;
4094 end if;
4095
4096 -- If we fall through, Pname is the procedure to be called
4097
4098 Rewrite_Stream_Proc_Call (Pname);
4099 end Write;
4100
4101 -- Component_Size is handled by Gigi, unless the component size is
4102 -- known at compile time, which is always true in the packed array
4103 -- case. It is important that the packed array case is handled in
4104 -- the front end (see Eval_Attribute) since Gigi would otherwise
4105 -- get confused by the equivalent packed array type.
4106
4107 when Attribute_Component_Size =>
4108 null;
4109
4110 -- The following attributes are handled by Gigi (except that static
4111 -- cases have already been evaluated by the semantics, but in any
4112 -- case Gigi should not count on that).
4113
4114 -- In addition Gigi handles the non-floating-point cases of Pred
4115 -- and Succ (including the fixed-point cases, which can just be
4116 -- treated as integer increment/decrement operations)
4117
4118 -- Gigi also handles the non-class-wide cases of Size
4119
4120 when Attribute_Bit_Order |
4121 Attribute_Code_Address |
4122 Attribute_Definite |
4123 Attribute_Max |
4124 Attribute_Mechanism_Code |
4125 Attribute_Min |
4126 Attribute_Null_Parameter |
9dfe12ae 4127 Attribute_Passed_By_Reference |
4128 Attribute_Pool_Address =>
ee6ba406 4129 null;
4130
4131 -- The following attributes are also handled by Gigi, but return a
4132 -- universal integer result, so may need a conversion for checking
4133 -- that the result is in range.
4134
4135 when Attribute_Aft |
ee6ba406 4136 Attribute_Bit |
4137 Attribute_Max_Size_In_Storage_Elements
4138 =>
4139 Apply_Universal_Integer_Attribute_Checks (N);
4140
4141 -- The following attributes should not appear at this stage, since they
4142 -- have already been handled by the analyzer (and properly rewritten
4143 -- with corresponding values or entities to represent the right values)
4144
4145 when Attribute_Abort_Signal |
4146 Attribute_Address_Size |
4147 Attribute_Base |
4148 Attribute_Class |
4149 Attribute_Default_Bit_Order |
4150 Attribute_Delta |
4151 Attribute_Denorm |
4152 Attribute_Digits |
4153 Attribute_Emax |
4154 Attribute_Epsilon |
5c99c290 4155 Attribute_Has_Access_Values |
ee6ba406 4156 Attribute_Has_Discriminants |
4157 Attribute_Large |
4158 Attribute_Machine_Emax |
4159 Attribute_Machine_Emin |
4160 Attribute_Machine_Mantissa |
4161 Attribute_Machine_Overflows |
4162 Attribute_Machine_Radix |
4163 Attribute_Machine_Rounds |
ee6ba406 4164 Attribute_Maximum_Alignment |
4165 Attribute_Model_Emin |
4166 Attribute_Model_Epsilon |
4167 Attribute_Model_Mantissa |
4168 Attribute_Model_Small |
4169 Attribute_Modulus |
4170 Attribute_Partition_ID |
4171 Attribute_Range |
4172 Attribute_Safe_Emax |
4173 Attribute_Safe_First |
4174 Attribute_Safe_Large |
4175 Attribute_Safe_Last |
4176 Attribute_Safe_Small |
4177 Attribute_Scale |
4178 Attribute_Signed_Zeros |
4179 Attribute_Small |
4180 Attribute_Storage_Unit |
9dfe12ae 4181 Attribute_Target_Name |
ee6ba406 4182 Attribute_Type_Class |
9dfe12ae 4183 Attribute_Unconstrained_Array |
ee6ba406 4184 Attribute_Universal_Literal_String |
4185 Attribute_Wchar_T_Size |
4186 Attribute_Word_Size =>
4187
4188 raise Program_Error;
4189
4190 -- The Asm_Input and Asm_Output attributes are not expanded at this
4191 -- stage, but will be eliminated in the expansion of the Asm call,
4192 -- see Exp_Intr for details. So Gigi will never see these either.
4193
4194 when Attribute_Asm_Input |
4195 Attribute_Asm_Output =>
4196
4197 null;
4198
4199 end case;
4200
9dfe12ae 4201 exception
4202 when RE_Not_Available =>
4203 return;
ee6ba406 4204 end Expand_N_Attribute_Reference;
4205
4206 ----------------------
4207 -- Expand_Pred_Succ --
4208 ----------------------
4209
4210 -- For typ'Pred (exp), we generate the check
4211
4212 -- [constraint_error when exp = typ'Base'First]
4213
4214 -- Similarly, for typ'Succ (exp), we generate the check
4215
4216 -- [constraint_error when exp = typ'Base'Last]
4217
4218 -- These checks are not generated for modular types, since the proper
4219 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
4220
4221 procedure Expand_Pred_Succ (N : Node_Id) is
4222 Loc : constant Source_Ptr := Sloc (N);
4223 Cnam : Name_Id;
4224
4225 begin
4226 if Attribute_Name (N) = Name_Pred then
4227 Cnam := Name_First;
4228 else
4229 Cnam := Name_Last;
4230 end if;
4231
4232 Insert_Action (N,
4233 Make_Raise_Constraint_Error (Loc,
4234 Condition =>
4235 Make_Op_Eq (Loc,
9dfe12ae 4236 Left_Opnd =>
4237 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
ee6ba406 4238 Right_Opnd =>
4239 Make_Attribute_Reference (Loc,
4240 Prefix =>
4241 New_Reference_To (Base_Type (Etype (Prefix (N))), Loc),
f15731c4 4242 Attribute_Name => Cnam)),
4243 Reason => CE_Overflow_Check_Failed));
ee6ba406 4244
4245 end Expand_Pred_Succ;
4246
4247 ------------------------
4248 -- Find_Inherited_TSS --
4249 ------------------------
4250
4251 function Find_Inherited_TSS
4252 (Typ : Entity_Id;
9dfe12ae 4253 Nam : TSS_Name_Type) return Entity_Id
ee6ba406 4254 is
9dfe12ae 4255 Btyp : Entity_Id := Typ;
4256 Proc : Entity_Id;
ee6ba406 4257
4258 begin
9dfe12ae 4259 loop
4260 Btyp := Base_Type (Btyp);
4261 Proc := TSS (Btyp, Nam);
ee6ba406 4262
9dfe12ae 4263 exit when Present (Proc)
4264 or else not Is_Derived_Type (Btyp);
ee6ba406 4265
9dfe12ae 4266 -- If Typ is a derived type, it may inherit attributes from
4267 -- some ancestor.
ee6ba406 4268
9dfe12ae 4269 Btyp := Etype (Btyp);
4270 end loop;
ee6ba406 4271
9dfe12ae 4272 if No (Proc) then
ee6ba406 4273
9dfe12ae 4274 -- If nothing else, use the TSS of the root type
4275
4276 Proc := TSS (Base_Type (Underlying_Type (Typ)), Nam);
ee6ba406 4277 end if;
4278
9dfe12ae 4279 return Proc;
ee6ba406 4280
ee6ba406 4281 end Find_Inherited_TSS;
4282
9dfe12ae 4283 ----------------------------
4284 -- Find_Stream_Subprogram --
4285 ----------------------------
4286
4287 function Find_Stream_Subprogram
4288 (Typ : Entity_Id;
4289 Nam : TSS_Name_Type) return Entity_Id is
4290 begin
4291 if Is_Tagged_Type (Typ)
4292 and then Is_Derived_Type (Typ)
4293 then
4294 return Find_Prim_Op (Typ, Nam);
4295 else
4296 return Find_Inherited_TSS (Typ, Nam);
4297 end if;
4298 end Find_Stream_Subprogram;
4299
ee6ba406 4300 -----------------------
4301 -- Get_Index_Subtype --
4302 -----------------------
4303
4304 function Get_Index_Subtype (N : Node_Id) return Node_Id is
4305 P_Type : Entity_Id := Etype (Prefix (N));
4306 Indx : Node_Id;
4307 J : Int;
4308
4309 begin
4310 if Is_Access_Type (P_Type) then
4311 P_Type := Designated_Type (P_Type);
4312 end if;
4313
4314 if No (Expressions (N)) then
4315 J := 1;
4316 else
4317 J := UI_To_Int (Expr_Value (First (Expressions (N))));
4318 end if;
4319
4320 Indx := First_Index (P_Type);
4321 while J > 1 loop
4322 Next_Index (Indx);
4323 J := J - 1;
4324 end loop;
4325
4326 return Etype (Indx);
4327 end Get_Index_Subtype;
4328
4329 ---------------------------------
4330 -- Is_Constrained_Packed_Array --
4331 ---------------------------------
4332
4333 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
4334 Arr : Entity_Id := Typ;
4335
4336 begin
4337 if Is_Access_Type (Arr) then
4338 Arr := Designated_Type (Arr);
4339 end if;
4340
4341 return Is_Array_Type (Arr)
4342 and then Is_Constrained (Arr)
4343 and then Present (Packed_Array_Type (Arr));
4344 end Is_Constrained_Packed_Array;
4345
4346end Exp_Attr;