* emit-rtl.c (widen_memory_access): New.
[official-gcc.git] / gcc / ada / exp_attr.adb
blob2fada3e36a57ec2560a3cc577d0341ce3d114a3d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ A T T R --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision: 1.304 $
10 -- --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 -- --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Exp_Ch2; use Exp_Ch2;
33 with Exp_Ch9; use Exp_Ch9;
34 with Exp_Imgv; use Exp_Imgv;
35 with Exp_Pakd; use Exp_Pakd;
36 with Exp_Strm; use Exp_Strm;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Gnatvsn; use Gnatvsn;
40 with Hostparm; use Hostparm;
41 with Lib; use Lib;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Nlists; use Nlists;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Ch7; use Sem_Ch7;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch13; use Sem_Ch13;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Util; use Sem_Util;
55 with Sinfo; use Sinfo;
56 with Snames; use Snames;
57 with Stand; use Stand;
58 with Stringt; use Stringt;
59 with Tbuild; use Tbuild;
60 with Ttypes; use Ttypes;
61 with Uintp; use Uintp;
62 with Uname; use Uname;
63 with Validsw; use Validsw;
65 package body Exp_Attr is
67 -----------------------
68 -- Local Subprograms --
69 -----------------------
71 procedure Compile_Stream_Body_In_Scope
72 (N : Node_Id;
73 Decl : Node_Id;
74 Arr : Entity_Id;
75 Check : Boolean);
76 -- The body for a stream subprogram may be generated outside of the scope
77 -- of the type. If the type is fully private, it may depend on the full
78 -- view of other types (e.g. indices) that are currently private as well.
79 -- We install the declarations of the package in which the type is declared
80 -- before compiling the body in what is its proper environment. The Check
81 -- parameter indicates if checks are to be suppressed for the stream body.
82 -- We suppress checks for array/record reads, since the rule is that these
83 -- are like assignments, out of range values due to uninitialized storage,
84 -- or other invalid values do NOT cause a Constraint_Error to be raised.
86 procedure Expand_Fpt_Attribute
87 (N : Node_Id;
88 Rtp : Entity_Id;
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
94 -- of the package containing the attribute routines).
96 procedure Expand_Fpt_Attribute_R (N : Node_Id);
97 -- This procedure expands a call to a floating-point attribute function
98 -- that takes a single floating-point argument.
100 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
101 -- This procedure expands a call to a floating-point attribute function
102 -- that takes one floating-point argument and one integer argument.
104 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
105 -- This procedure expands a call to a floating-point attribute function
106 -- that takes two floating-point arguments.
108 procedure Expand_Pred_Succ (N : Node_Id);
109 -- Handles expansion of Pred or Succ attributes for case of non-real
110 -- operand with overflow checking required.
112 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
113 -- Used for Last, Last, and Length, when the prefix is an array type,
114 -- Obtains the corresponding index subtype.
116 procedure Expand_Access_To_Type (N : Node_Id);
117 -- A reference to a type within its own scope is resolved to a reference
118 -- to the current instance of the type in its initialization procedure.
120 function Find_Inherited_TSS
121 (Typ : Entity_Id;
122 Nam : Name_Id) return Entity_Id;
124 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
125 -- Utility for array attributes, returns true on packed constrained
126 -- arrays, and on access to same.
128 ----------------------------------
129 -- Compile_Stream_Body_In_Scope --
130 ----------------------------------
132 procedure Compile_Stream_Body_In_Scope
133 (N : Node_Id;
134 Decl : Node_Id;
135 Arr : Entity_Id;
136 Check : Boolean)
138 Installed : Boolean := False;
139 Scop : constant Entity_Id := Scope (Arr);
140 Curr : constant Entity_Id := Current_Scope;
142 begin
143 if Is_Hidden (Arr)
144 and then not In_Open_Scopes (Scop)
145 and then Ekind (Scop) = E_Package
146 then
147 New_Scope (Scop);
148 Install_Visible_Declarations (Scop);
149 Install_Private_Declarations (Scop);
150 Installed := True;
152 -- The entities in the package are now visible, but the generated
153 -- stream entity must appear in the current scope (usually an
154 -- enclosing stream function) so that itypes all have their proper
155 -- scopes.
157 New_Scope (Curr);
158 end if;
160 if Check then
161 Insert_Action (N, Decl);
162 else
163 Insert_Action (N, Decl, All_Checks);
164 end if;
166 if Installed then
168 -- Remove extra copy of current scope, and package itself
170 Pop_Scope;
171 End_Package_Scope (Scop);
172 end if;
173 end Compile_Stream_Body_In_Scope;
175 ---------------------------
176 -- Expand_Access_To_Type --
177 ---------------------------
179 procedure Expand_Access_To_Type (N : Node_Id) is
180 Loc : constant Source_Ptr := Sloc (N);
181 Typ : constant Entity_Id := Etype (N);
182 Pref : constant Node_Id := Prefix (N);
183 Par : Node_Id;
184 Formal : Entity_Id;
186 begin
187 if Is_Entity_Name (Pref)
188 and then Is_Type (Entity (Pref))
189 then
190 -- If the current instance name denotes a task type,
191 -- then the access attribute is rewritten to be the
192 -- name of the "_task" parameter associated with the
193 -- task type's task body procedure. An unchecked
194 -- conversion is applied to ensure a type match in
195 -- cases of expander-generated calls (e.g., init procs).
197 if Is_Task_Type (Entity (Pref)) then
198 Formal :=
199 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
201 while Present (Formal) loop
202 exit when Chars (Formal) = Name_uTask;
203 Next_Entity (Formal);
204 end loop;
206 pragma Assert (Present (Formal));
208 Rewrite (N,
209 Unchecked_Convert_To (Typ, New_Occurrence_Of (Formal, Loc)));
210 Set_Etype (N, Typ);
212 -- The expression must appear in a default expression,
213 -- (which in the initialization procedure is the rhs of
214 -- an assignment), and not in a discriminant constraint.
216 else
217 Par := Parent (N);
219 while Present (Par) loop
220 exit when Nkind (Par) = N_Assignment_Statement;
222 if Nkind (Par) = N_Component_Declaration then
223 return;
224 end if;
226 Par := Parent (Par);
227 end loop;
229 if Present (Par) then
230 Rewrite (N,
231 Make_Attribute_Reference (Loc,
232 Prefix => Make_Identifier (Loc, Name_uInit),
233 Attribute_Name => Attribute_Name (N)));
235 Analyze_And_Resolve (N, Typ);
236 end if;
237 end if;
238 end if;
239 end Expand_Access_To_Type;
241 --------------------------
242 -- Expand_Fpt_Attribute --
243 --------------------------
245 procedure Expand_Fpt_Attribute
246 (N : Node_Id;
247 Rtp : Entity_Id;
248 Args : List_Id)
250 Loc : constant Source_Ptr := Sloc (N);
251 Typ : constant Entity_Id := Etype (N);
252 Pkg : RE_Id;
253 Fnm : Node_Id;
255 begin
256 -- The function name is the selected component Fat_xxx.yyy where xxx
257 -- is the floating-point root type, and yyy is the attribute name
259 -- Note: it would be more usual to have separate RE entries for each
260 -- of the entities in the Fat packages, but first they have identical
261 -- names (so we would have to have lots of renaming declarations to
262 -- meet the normal RE rule of separate names for all runtime entities),
263 -- and second there would be an awful lot of them!
265 if Rtp = Standard_Short_Float then
266 Pkg := RE_Fat_Short_Float;
267 elsif Rtp = Standard_Float then
268 Pkg := RE_Fat_Float;
269 elsif Rtp = Standard_Long_Float then
270 Pkg := RE_Fat_Long_Float;
271 else
272 Pkg := RE_Fat_Long_Long_Float;
273 end if;
275 Fnm :=
276 Make_Selected_Component (Loc,
277 Prefix => New_Reference_To (RTE (Pkg), Loc),
278 Selector_Name => Make_Identifier (Loc, Attribute_Name (N)));
280 -- The generated call is given the provided set of parameters, and then
281 -- wrapped in a conversion which converts the result to the target type
283 Rewrite (N,
284 Unchecked_Convert_To (Etype (N),
285 Make_Function_Call (Loc,
286 Name => Fnm,
287 Parameter_Associations => Args)));
289 Analyze_And_Resolve (N, Typ);
291 end Expand_Fpt_Attribute;
293 ----------------------------
294 -- Expand_Fpt_Attribute_R --
295 ----------------------------
297 -- The single argument is converted to its root type to call the
298 -- appropriate runtime function, with the actual call being built
299 -- by Expand_Fpt_Attribute
301 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
302 E1 : constant Node_Id := First (Expressions (N));
303 Rtp : constant Entity_Id := Root_Type (Etype (E1));
305 begin
306 Expand_Fpt_Attribute (N, Rtp, New_List (
307 Unchecked_Convert_To (Rtp, Relocate_Node (E1))));
308 end Expand_Fpt_Attribute_R;
310 -----------------------------
311 -- Expand_Fpt_Attribute_RI --
312 -----------------------------
314 -- The first argument is converted to its root type and the second
315 -- argument is converted to standard long long integer to call the
316 -- appropriate runtime function, with the actual call being built
317 -- by Expand_Fpt_Attribute
319 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
320 E1 : constant Node_Id := First (Expressions (N));
321 Rtp : constant Entity_Id := Root_Type (Etype (E1));
322 E2 : constant Node_Id := Next (E1);
324 begin
325 Expand_Fpt_Attribute (N, Rtp, New_List (
326 Unchecked_Convert_To (Rtp, Relocate_Node (E1)),
327 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
328 end Expand_Fpt_Attribute_RI;
330 -----------------------------
331 -- Expand_Fpt_Attribute_RR --
332 -----------------------------
334 -- The two arguments is converted to their root types to call the
335 -- appropriate runtime function, with the actual call being built
336 -- by Expand_Fpt_Attribute
338 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
339 E1 : constant Node_Id := First (Expressions (N));
340 Rtp : constant Entity_Id := Root_Type (Etype (E1));
341 E2 : constant Node_Id := Next (E1);
343 begin
344 Expand_Fpt_Attribute (N, Rtp, New_List (
345 Unchecked_Convert_To (Rtp, Relocate_Node (E1)),
346 Unchecked_Convert_To (Rtp, Relocate_Node (E2))));
347 end Expand_Fpt_Attribute_RR;
349 ----------------------------------
350 -- Expand_N_Attribute_Reference --
351 ----------------------------------
353 procedure Expand_N_Attribute_Reference (N : Node_Id) is
354 Loc : constant Source_Ptr := Sloc (N);
355 Typ : constant Entity_Id := Etype (N);
356 Btyp : constant Entity_Id := Base_Type (Typ);
357 Pref : constant Node_Id := Prefix (N);
358 Exprs : constant List_Id := Expressions (N);
359 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
361 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
362 -- Rewrites a stream attribute for Read, Write or Output with the
363 -- procedure call. Pname is the entity for the procedure to call.
365 ------------------------------
366 -- Rewrite_Stream_Proc_Call --
367 ------------------------------
369 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
370 Item : constant Node_Id := Next (First (Exprs));
371 Formal_Typ : constant Entity_Id :=
372 Etype (Next_Formal (First_Formal (Pname)));
374 begin
375 -- We have to worry about the type of the second argument
377 -- For the class-wide dispatching cases, and for cases in which
378 -- the base type of the second argument matches the base type of
379 -- the corresponding formal parameter, we are all set, and can use
380 -- the argument unchanged.
382 -- For all other cases we do an unchecked conversion of the second
383 -- parameter to the type of the formal of the procedure we are
384 -- calling. This deals with the private type cases, and with going
385 -- to the root type as required in elementary type case.
387 if not Is_Class_Wide_Type (Entity (Pref))
388 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
389 then
390 Rewrite (Item,
391 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
393 -- For untagged derived types set Assignment_OK, to prevent
394 -- copies from being created when the unchecked conversion
395 -- is expanded (which would happen in Remove_Side_Effects
396 -- if Expand_N_Unchecked_Conversion were allowed to call
397 -- Force_Evaluation). The copy could violate Ada semantics
398 -- in cases such as an actual that is an out parameter.
399 -- Note that this approach is also used in exp_ch7 for calls
400 -- to controlled type operations to prevent problems with
401 -- actuals wrapped in unchecked conversions.
403 if Is_Untagged_Derivation (Etype (Expression (Item))) then
404 Set_Assignment_OK (Item);
405 end if;
406 end if;
408 -- And now rewrite the call
410 Rewrite (N,
411 Make_Procedure_Call_Statement (Loc,
412 Name => New_Occurrence_Of (Pname, Loc),
413 Parameter_Associations => Exprs));
415 Analyze (N);
416 end Rewrite_Stream_Proc_Call;
418 -- Start of processing for Expand_N_Attribute_Reference
420 begin
421 -- Do required validity checking
423 if Validity_Checks_On and Validity_Check_Operands then
424 declare
425 Expr : Node_Id;
427 begin
428 Expr := First (Expressions (N));
429 while Present (Expr) loop
430 Ensure_Valid (Expr);
431 Next (Expr);
432 end loop;
433 end;
434 end if;
436 -- Remaining processing depends on specific attribute
438 case Id is
440 ------------
441 -- Access --
442 ------------
444 when Attribute_Access =>
446 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type then
448 -- The value of the attribute_reference is a record containing
449 -- two fields: an access to the protected object, and an access
450 -- to the subprogram itself. The prefix is a selected component.
452 declare
453 Agg : Node_Id;
454 Sub : Entity_Id;
455 E_T : constant Entity_Id := Equivalent_Type (Typ);
456 Acc : constant Entity_Id :=
457 Etype (Next_Component (First_Component (E_T)));
458 Obj_Ref : Node_Id;
459 Curr : Entity_Id;
461 begin
462 -- Within the body of the protected type, the prefix
463 -- designates a local operation, and the object is the first
464 -- parameter of the corresponding protected body of the
465 -- current enclosing operation.
467 if Is_Entity_Name (Pref) then
468 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
469 Sub :=
470 New_Occurrence_Of
471 (Protected_Body_Subprogram (Entity (Pref)), Loc);
472 Curr := Current_Scope;
474 while Scope (Curr) /= Scope (Entity (Pref)) loop
475 Curr := Scope (Curr);
476 end loop;
478 Obj_Ref :=
479 Make_Attribute_Reference (Loc,
480 Prefix =>
481 New_Occurrence_Of
482 (First_Formal
483 (Protected_Body_Subprogram (Curr)), Loc),
484 Attribute_Name => Name_Address);
486 -- Case where the prefix is not an entity name. Find the
487 -- version of the protected operation to be called from
488 -- outside the protected object.
490 else
491 Sub :=
492 New_Occurrence_Of
493 (External_Subprogram
494 (Entity (Selector_Name (Pref))), Loc);
496 Obj_Ref :=
497 Make_Attribute_Reference (Loc,
498 Prefix => Relocate_Node (Prefix (Pref)),
499 Attribute_Name => Name_Address);
500 end if;
502 Agg :=
503 Make_Aggregate (Loc,
504 Expressions =>
505 New_List (
506 Obj_Ref,
507 Unchecked_Convert_To (Acc,
508 Make_Attribute_Reference (Loc,
509 Prefix => Sub,
510 Attribute_Name => Name_Address))));
512 Rewrite (N, Agg);
514 Analyze_And_Resolve (N, Equivalent_Type (Typ));
516 -- For subsequent analysis, the node must retain its type.
517 -- The backend will replace it with the equivalent type where
518 -- needed.
520 Set_Etype (N, Typ);
521 end;
523 elsif Ekind (Btyp) = E_General_Access_Type then
524 declare
525 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
526 Parm_Ent : Entity_Id;
527 Conversion : Node_Id;
529 begin
530 -- If the prefix of an Access attribute is a dereference of an
531 -- access parameter (or a renaming of such a dereference) and
532 -- the context is a general access type (but not an anonymous
533 -- access type), then rewrite the attribute as a conversion of
534 -- the access parameter to the context access type. This will
535 -- result in an accessibility check being performed, if needed.
537 -- (X.all'Access => Acc_Type (X))
539 if Nkind (Ref_Object) = N_Explicit_Dereference
540 and then Is_Entity_Name (Prefix (Ref_Object))
541 then
542 Parm_Ent := Entity (Prefix (Ref_Object));
544 if Ekind (Parm_Ent) in Formal_Kind
545 and then Ekind (Etype (Parm_Ent)) = E_Anonymous_Access_Type
546 and then Present (Extra_Accessibility (Parm_Ent))
547 then
548 Conversion :=
549 Convert_To (Typ, New_Copy_Tree (Prefix (Ref_Object)));
551 Rewrite (N, Conversion);
552 Analyze_And_Resolve (N, Typ);
553 end if;
554 end if;
555 end;
557 -- If the prefix is a type name, this is a reference to the current
558 -- instance of the type, within its initialization procedure.
560 else
561 Expand_Access_To_Type (N);
562 end if;
564 --------------
565 -- Adjacent --
566 --------------
568 -- Transforms 'Adjacent into a call to the floating-point attribute
569 -- function Adjacent in Fat_xxx (where xxx is the root type)
571 when Attribute_Adjacent =>
572 Expand_Fpt_Attribute_RR (N);
574 -------------
575 -- Address --
576 -------------
578 when Attribute_Address => Address : declare
579 Task_Proc : Entity_Id;
581 begin
582 -- If the prefix is a task or a task type, the useful address
583 -- is that of the procedure for the task body, i.e. the actual
584 -- program unit. We replace the original entity with that of
585 -- the procedure.
587 if Is_Entity_Name (Pref)
588 and then Is_Task_Type (Entity (Pref))
589 then
590 Task_Proc := Next_Entity (Root_Type (Etype (Pref)));
592 while Present (Task_Proc) loop
593 exit when Ekind (Task_Proc) = E_Procedure
594 and then Etype (First_Formal (Task_Proc)) =
595 Corresponding_Record_Type (Etype (Pref));
596 Next_Entity (Task_Proc);
597 end loop;
599 if Present (Task_Proc) then
600 Set_Entity (Pref, Task_Proc);
601 Set_Etype (Pref, Etype (Task_Proc));
602 end if;
604 -- Similarly, the address of a protected operation is the address
605 -- of the corresponding protected body, regardless of the protected
606 -- object from which it is selected.
608 elsif Nkind (Pref) = N_Selected_Component
609 and then Is_Subprogram (Entity (Selector_Name (Pref)))
610 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
611 then
612 Rewrite (Pref,
613 New_Occurrence_Of (
614 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
616 elsif Nkind (Pref) = N_Explicit_Dereference
617 and then Ekind (Etype (Pref)) = E_Subprogram_Type
618 and then Convention (Etype (Pref)) = Convention_Protected
619 then
620 -- The prefix is be a dereference of an access_to_protected_
621 -- subprogram. The desired address is the second component of
622 -- the record that represents the access.
624 declare
625 Addr : constant Entity_Id := Etype (N);
626 Ptr : constant Node_Id := Prefix (Pref);
627 T : constant Entity_Id :=
628 Equivalent_Type (Base_Type (Etype (Ptr)));
630 begin
631 Rewrite (N,
632 Unchecked_Convert_To (Addr,
633 Make_Selected_Component (Loc,
634 Prefix => Unchecked_Convert_To (T, Ptr),
635 Selector_Name => New_Occurrence_Of (
636 Next_Entity (First_Entity (T)), Loc))));
638 Analyze_And_Resolve (N, Addr);
639 end;
640 end if;
642 -- Deal with packed array reference, other cases are handled by gigi
644 if Involves_Packed_Array_Reference (Pref) then
645 Expand_Packed_Address_Reference (N);
646 end if;
647 end Address;
649 ---------------
650 -- AST_Entry --
651 ---------------
653 when Attribute_AST_Entry => AST_Entry : declare
654 Ttyp : Entity_Id;
655 T_Id : Node_Id;
656 Eent : Entity_Id;
658 Entry_Ref : Node_Id;
659 -- The reference to the entry or entry family
661 Index : Node_Id;
662 -- The index expression for an entry family reference, or
663 -- the Empty if Entry_Ref references a simple entry.
665 begin
666 if Nkind (Pref) = N_Indexed_Component then
667 Entry_Ref := Prefix (Pref);
668 Index := First (Expressions (Pref));
669 else
670 Entry_Ref := Pref;
671 Index := Empty;
672 end if;
674 -- Get expression for Task_Id and the entry entity
676 if Nkind (Entry_Ref) = N_Selected_Component then
677 T_Id :=
678 Make_Attribute_Reference (Loc,
679 Attribute_Name => Name_Identity,
680 Prefix => Prefix (Entry_Ref));
682 Ttyp := Etype (Prefix (Entry_Ref));
683 Eent := Entity (Selector_Name (Entry_Ref));
685 else
686 T_Id :=
687 Make_Function_Call (Loc,
688 Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc));
690 Eent := Entity (Entry_Ref);
692 -- We have to find the enclosing task to get the task type
693 -- There must be one, since we already validated this earlier
695 Ttyp := Current_Scope;
696 while not Is_Task_Type (Ttyp) loop
697 Ttyp := Scope (Ttyp);
698 end loop;
699 end if;
701 -- Now rewrite the attribute with a call to Create_AST_Handler
703 Rewrite (N,
704 Make_Function_Call (Loc,
705 Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc),
706 Parameter_Associations => New_List (
707 T_Id,
708 Entry_Index_Expression (Loc, Eent, Index, Ttyp))));
710 Analyze_And_Resolve (N, RTE (RE_AST_Handler));
711 end AST_Entry;
713 ------------------
714 -- Bit_Position --
715 ------------------
717 -- We compute this if a component clause was present, otherwise
718 -- we leave the computation up to Gigi, since we don't know what
719 -- layout will be chosen.
721 -- Note that the attribute can apply to a naked record component
722 -- in generated code (i.e. the prefix is an identifier that
723 -- references the component or discriminant entity).
725 when Attribute_Bit_Position => Bit_Position :
726 declare
727 CE : Entity_Id;
729 begin
730 if Nkind (Pref) = N_Identifier then
731 CE := Entity (Pref);
732 else
733 CE := Entity (Selector_Name (Pref));
734 end if;
736 if Known_Static_Component_Bit_Offset (CE) then
737 Rewrite (N,
738 Make_Integer_Literal (Loc,
739 Intval => Component_Bit_Offset (CE)));
740 Analyze_And_Resolve (N, Typ);
742 else
743 Apply_Universal_Integer_Attribute_Checks (N);
744 end if;
745 end Bit_Position;
747 ------------------
748 -- Body_Version --
749 ------------------
751 -- A reference to P'Body_Version or P'Version is expanded to
753 -- Vnn : Unsigned;
754 -- pragma Import (C, Vnn, "uuuuT";
755 -- ...
756 -- Get_Version_String (Vnn)
758 -- where uuuu is the unit name (dots replaced by double underscore)
759 -- and T is B for the cases of Body_Version, or Version applied to a
760 -- subprogram acting as its own spec, and S for Version applied to a
761 -- subprogram spec or package. This sequence of code references the
762 -- the unsigned constant created in the main program by the binder.
764 -- A special exception occurs for Standard, where the string
765 -- returned is a copy of the library string in gnatvsn.ads.
767 when Attribute_Body_Version | Attribute_Version => Version : declare
768 E : constant Entity_Id :=
769 Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
770 Pent : Entity_Id := Entity (Pref);
771 S : String_Id;
773 begin
774 -- If not library unit, get to containing library unit
776 while Pent /= Standard_Standard
777 and then Scope (Pent) /= Standard_Standard
778 loop
779 Pent := Scope (Pent);
780 end loop;
782 -- Special case Standard
784 if Pent = Standard_Standard
785 or else Pent = Standard_ASCII
786 then
787 Name_Buffer (1 .. Library_Version'Length) := Library_Version;
788 Name_Len := Library_Version'Length;
789 Rewrite (N,
790 Make_String_Literal (Loc,
791 Strval => String_From_Name_Buffer));
793 -- All other cases
795 else
796 -- Build required string constant
798 Get_Name_String (Get_Unit_Name (Pent));
800 Start_String;
801 for J in 1 .. Name_Len - 2 loop
802 if Name_Buffer (J) = '.' then
803 Store_String_Chars ("__");
804 else
805 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
806 end if;
807 end loop;
809 -- Case of subprogram acting as its own spec, always use body
811 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
812 and then Nkind (Parent (Declaration_Node (Pent))) =
813 N_Subprogram_Body
814 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
815 then
816 Store_String_Chars ("B");
818 -- Case of no body present, always use spec
820 elsif not Unit_Requires_Body (Pent) then
821 Store_String_Chars ("S");
823 -- Otherwise use B for Body_Version, S for spec
825 elsif Id = Attribute_Body_Version then
826 Store_String_Chars ("B");
827 else
828 Store_String_Chars ("S");
829 end if;
831 S := End_String;
832 Lib.Version_Referenced (S);
834 -- Insert the object declaration
836 Insert_Actions (N, New_List (
837 Make_Object_Declaration (Loc,
838 Defining_Identifier => E,
839 Object_Definition =>
840 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
842 -- Set entity as imported with correct external name
844 Set_Is_Imported (E);
845 Set_Interface_Name (E, Make_String_Literal (Loc, S));
847 -- And now rewrite original reference
849 Rewrite (N,
850 Make_Function_Call (Loc,
851 Name => New_Reference_To (RTE (RE_Get_Version_String), Loc),
852 Parameter_Associations => New_List (
853 New_Occurrence_Of (E, Loc))));
854 end if;
856 Analyze_And_Resolve (N, RTE (RE_Version_String));
857 end Version;
859 -------------
860 -- Ceiling --
861 -------------
863 -- Transforms 'Ceiling into a call to the floating-point attribute
864 -- function Ceiling in Fat_xxx (where xxx is the root type)
866 when Attribute_Ceiling =>
867 Expand_Fpt_Attribute_R (N);
869 --------------
870 -- Callable --
871 --------------
873 -- Transforms 'Callable attribute into a call to the Callable function.
875 when Attribute_Callable => Callable :
876 begin
877 Rewrite (N,
878 Build_Call_With_Task (Pref, RTE (RE_Callable)));
879 Analyze_And_Resolve (N, Standard_Boolean);
880 end Callable;
882 ------------
883 -- Caller --
884 ------------
886 -- Transforms 'Caller attribute into a call to either the
887 -- Task_Entry_Caller or the Protected_Entry_Caller function.
889 when Attribute_Caller => Caller : declare
890 Id_Kind : Entity_Id := RTE (RO_AT_Task_ID);
891 Ent : Entity_Id := Entity (Pref);
892 Conctype : Entity_Id := Scope (Ent);
893 Nest_Depth : Integer := 0;
894 Name : Node_Id;
895 S : Entity_Id;
897 begin
898 -- Protected case
900 if Is_Protected_Type (Conctype) then
901 if Abort_Allowed
902 or else Restrictions (No_Entry_Queue) = False
903 or else Number_Entries (Conctype) > 1
904 then
905 Name :=
906 New_Reference_To
907 (RTE (RE_Protected_Entry_Caller), Loc);
908 else
909 Name :=
910 New_Reference_To
911 (RTE (RE_Protected_Single_Entry_Caller), Loc);
912 end if;
914 Rewrite (N,
915 Unchecked_Convert_To (Id_Kind,
916 Make_Function_Call (Loc,
917 Name => Name,
918 Parameter_Associations => New_List
919 (New_Reference_To (
920 Object_Ref
921 (Corresponding_Body (Parent (Conctype))), Loc)))));
923 -- Task case
925 else
926 -- Determine the nesting depth of the E'Caller attribute, that
927 -- is, how many accept statements are nested within the accept
928 -- statement for E at the point of E'Caller. The runtime uses
929 -- this depth to find the specified entry call.
931 for J in reverse 0 .. Scope_Stack.Last loop
932 S := Scope_Stack.Table (J).Entity;
934 -- We should not reach the scope of the entry, as it should
935 -- already have been checked in Sem_Attr that this attribute
936 -- reference is within a matching accept statement.
938 pragma Assert (S /= Conctype);
940 if S = Ent then
941 exit;
943 elsif Is_Entry (S) then
944 Nest_Depth := Nest_Depth + 1;
945 end if;
946 end loop;
948 Rewrite (N,
949 Unchecked_Convert_To (Id_Kind,
950 Make_Function_Call (Loc,
951 Name => New_Reference_To (
952 RTE (RE_Task_Entry_Caller), Loc),
953 Parameter_Associations => New_List (
954 Make_Integer_Literal (Loc,
955 Intval => Int (Nest_Depth))))));
956 end if;
958 Analyze_And_Resolve (N, Id_Kind);
959 end Caller;
961 -------------
962 -- Compose --
963 -------------
965 -- Transforms 'Compose into a call to the floating-point attribute
966 -- function Compose in Fat_xxx (where xxx is the root type)
968 -- Note: we strictly should have special code here to deal with the
969 -- case of absurdly negative arguments (less than Integer'First)
970 -- which will return a (signed) zero value, but it hardly seems
971 -- worth the effort. Absurdly large positive arguments will raise
972 -- constraint error which is fine.
974 when Attribute_Compose =>
975 Expand_Fpt_Attribute_RI (N);
977 -----------------
978 -- Constrained --
979 -----------------
981 when Attribute_Constrained => Constrained : declare
982 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
984 begin
985 -- Reference to a parameter where the value is passed as an extra
986 -- actual, corresponding to the extra formal referenced by the
987 -- Extra_Constrained field of the corresponding formal.
989 if Present (Formal_Ent)
990 and then Present (Extra_Constrained (Formal_Ent))
991 then
992 Rewrite (N,
993 New_Occurrence_Of
994 (Extra_Constrained (Formal_Ent), Sloc (N)));
996 -- For variables with a Extra_Constrained field, we use the
997 -- corresponding entity.
999 elsif Nkind (Pref) = N_Identifier
1000 and then Ekind (Entity (Pref)) = E_Variable
1001 and then Present (Extra_Constrained (Entity (Pref)))
1002 then
1003 Rewrite (N,
1004 New_Occurrence_Of
1005 (Extra_Constrained (Entity (Pref)), Sloc (N)));
1007 -- For all other entity names, we can tell at compile time
1009 elsif Is_Entity_Name (Pref) then
1010 declare
1011 Ent : constant Entity_Id := Entity (Pref);
1012 Res : Boolean;
1014 begin
1015 -- (RM J.4) obsolescent cases
1017 if Is_Type (Ent) then
1019 -- Private type
1021 if Is_Private_Type (Ent) then
1022 Res := not Has_Discriminants (Ent)
1023 or else Is_Constrained (Ent);
1025 -- It not a private type, must be a generic actual type
1026 -- that corresponded to a private type. We know that this
1027 -- correspondence holds, since otherwise the reference
1028 -- within the generic template would have been illegal.
1030 else
1031 declare
1032 UT : Entity_Id := Underlying_Type (Ent);
1034 begin
1035 if Is_Composite_Type (UT) then
1036 Res := Is_Constrained (Ent);
1037 else
1038 Res := True;
1039 end if;
1040 end;
1041 end if;
1043 -- If the prefix is not a variable or is aliased, then
1044 -- definitely true; if it's a formal parameter without
1045 -- an associated extra formal, then treat it as constrained.
1047 elsif not Is_Variable (Pref)
1048 or else Present (Formal_Ent)
1049 or else Is_Aliased_View (Pref)
1050 then
1051 Res := True;
1053 -- Variable case, just look at type to see if it is
1054 -- constrained. Note that the one case where this is
1055 -- not accurate (the procedure formal case), has been
1056 -- handled above.
1058 else
1059 Res := Is_Constrained (Etype (Ent));
1060 end if;
1062 if Res then
1063 Rewrite (N,
1064 New_Reference_To (Standard_True, Loc));
1065 else
1066 Rewrite (N,
1067 New_Reference_To (Standard_False, Loc));
1068 end if;
1069 end;
1071 -- Prefix is not an entity name. These are also cases where
1072 -- we can always tell at compile time by looking at the form
1073 -- and type of the prefix.
1075 else
1076 if not Is_Variable (Pref)
1077 or else Nkind (Pref) = N_Explicit_Dereference
1078 or else Is_Constrained (Etype (Pref))
1079 then
1080 Rewrite (N,
1081 New_Reference_To (Standard_True, Loc));
1082 else
1083 Rewrite (N,
1084 New_Reference_To (Standard_False, Loc));
1085 end if;
1086 end if;
1088 Analyze_And_Resolve (N, Standard_Boolean);
1089 end Constrained;
1091 ---------------
1092 -- Copy_Sign --
1093 ---------------
1095 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1096 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1098 when Attribute_Copy_Sign =>
1099 Expand_Fpt_Attribute_RR (N);
1101 -----------
1102 -- Count --
1103 -----------
1105 -- Transforms 'Count attribute into a call to the Count function
1107 when Attribute_Count => Count :
1108 declare
1109 Entnam : Node_Id;
1110 Index : Node_Id;
1111 Name : Node_Id;
1112 Call : Node_Id;
1113 Conctyp : Entity_Id;
1115 begin
1116 -- If the prefix is a member of an entry family, retrieve both
1117 -- entry name and index. For a simple entry there is no index.
1119 if Nkind (Pref) = N_Indexed_Component then
1120 Entnam := Prefix (Pref);
1121 Index := First (Expressions (Pref));
1122 else
1123 Entnam := Pref;
1124 Index := Empty;
1125 end if;
1127 -- Find the concurrent type in which this attribute is referenced
1128 -- (there had better be one).
1130 Conctyp := Current_Scope;
1131 while not Is_Concurrent_Type (Conctyp) loop
1132 Conctyp := Scope (Conctyp);
1133 end loop;
1135 -- Protected case
1137 if Is_Protected_Type (Conctyp) then
1139 if Abort_Allowed
1140 or else Restrictions (No_Entry_Queue) = False
1141 or else Number_Entries (Conctyp) > 1
1142 then
1143 Name := New_Reference_To (RTE (RE_Protected_Count), Loc);
1145 Call :=
1146 Make_Function_Call (Loc,
1147 Name => Name,
1148 Parameter_Associations => New_List (
1149 New_Reference_To (
1150 Object_Ref (
1151 Corresponding_Body (Parent (Conctyp))), Loc),
1152 Entry_Index_Expression (
1153 Loc, Entity (Entnam), Index, Scope (Entity (Entnam)))));
1154 else
1155 Name := New_Reference_To (RTE (RE_Protected_Count_Entry), Loc);
1157 Call := Make_Function_Call (Loc,
1158 Name => Name,
1159 Parameter_Associations => New_List (
1160 New_Reference_To (
1161 Object_Ref (
1162 Corresponding_Body (Parent (Conctyp))), Loc)));
1163 end if;
1165 -- Task case
1167 else
1168 Call :=
1169 Make_Function_Call (Loc,
1170 Name => New_Reference_To (RTE (RE_Task_Count), Loc),
1171 Parameter_Associations => New_List (
1172 Entry_Index_Expression
1173 (Loc, Entity (Entnam), Index, Scope (Entity (Entnam)))));
1174 end if;
1176 -- The call returns type Natural but the context is universal integer
1177 -- so any integer type is allowed. The attribute was already resolved
1178 -- so its Etype is the required result type. If the base type of the
1179 -- context type is other than Standard.Integer we put in a conversion
1180 -- to the required type. This can be a normal typed conversion since
1181 -- both input and output types of the conversion are integer types
1183 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
1184 Rewrite (N, Convert_To (Typ, Call));
1185 else
1186 Rewrite (N, Call);
1187 end if;
1189 Analyze_And_Resolve (N, Typ);
1190 end Count;
1192 ---------------
1193 -- Elab_Body --
1194 ---------------
1196 -- This processing is shared by Elab_Spec
1198 -- What we do is to insert the following declarations
1200 -- procedure tnn;
1201 -- pragma Import (C, enn, "name___elabb/s");
1203 -- and then the Elab_Body/Spec attribute is replaced by a reference
1204 -- to this defining identifier.
1206 when Attribute_Elab_Body |
1207 Attribute_Elab_Spec =>
1209 Elab_Body : declare
1210 Ent : constant Entity_Id :=
1211 Make_Defining_Identifier (Loc,
1212 New_Internal_Name ('E'));
1213 Str : String_Id;
1214 Lang : Node_Id;
1216 procedure Make_Elab_String (Nod : Node_Id);
1217 -- Given Nod, an identifier, or a selected component, put the
1218 -- image into the current string literal, with double underline
1219 -- between components.
1221 procedure Make_Elab_String (Nod : Node_Id) is
1222 begin
1223 if Nkind (Nod) = N_Selected_Component then
1224 Make_Elab_String (Prefix (Nod));
1225 if Java_VM then
1226 Store_String_Char ('$');
1227 else
1228 Store_String_Char ('_');
1229 Store_String_Char ('_');
1230 end if;
1232 Get_Name_String (Chars (Selector_Name (Nod)));
1234 else
1235 pragma Assert (Nkind (Nod) = N_Identifier);
1236 Get_Name_String (Chars (Nod));
1237 end if;
1239 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1240 end Make_Elab_String;
1242 -- Start of processing for Elab_Body/Elab_Spec
1244 begin
1245 -- First we need to prepare the string literal for the name of
1246 -- the elaboration routine to be referenced.
1248 Start_String;
1249 Make_Elab_String (Pref);
1251 if Java_VM then
1252 Store_String_Chars ("._elab");
1253 Lang := Make_Identifier (Loc, Name_Ada);
1254 else
1255 Store_String_Chars ("___elab");
1256 Lang := Make_Identifier (Loc, Name_C);
1257 end if;
1259 if Id = Attribute_Elab_Body then
1260 Store_String_Char ('b');
1261 else
1262 Store_String_Char ('s');
1263 end if;
1265 Str := End_String;
1267 Insert_Actions (N, New_List (
1268 Make_Subprogram_Declaration (Loc,
1269 Specification =>
1270 Make_Procedure_Specification (Loc,
1271 Defining_Unit_Name => Ent)),
1273 Make_Pragma (Loc,
1274 Chars => Name_Import,
1275 Pragma_Argument_Associations => New_List (
1276 Make_Pragma_Argument_Association (Loc,
1277 Expression => Lang),
1279 Make_Pragma_Argument_Association (Loc,
1280 Expression =>
1281 Make_Identifier (Loc, Chars (Ent))),
1283 Make_Pragma_Argument_Association (Loc,
1284 Expression =>
1285 Make_String_Literal (Loc, Str))))));
1287 Set_Entity (N, Ent);
1288 Rewrite (N, New_Occurrence_Of (Ent, Loc));
1289 end Elab_Body;
1291 ----------------
1292 -- Elaborated --
1293 ----------------
1295 -- Elaborated is always True for preelaborated units, predefined
1296 -- units, pure units and units which have Elaborate_Body pragmas.
1297 -- These units have no elaboration entity.
1299 -- Note: The Elaborated attribute is never passed through to Gigi
1301 when Attribute_Elaborated => Elaborated : declare
1302 Ent : constant Entity_Id := Entity (Pref);
1304 begin
1305 if Present (Elaboration_Entity (Ent)) then
1306 Rewrite (N,
1307 New_Occurrence_Of (Elaboration_Entity (Ent), Loc));
1308 else
1309 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
1310 end if;
1311 end Elaborated;
1313 --------------
1314 -- Enum_Rep --
1315 --------------
1317 when Attribute_Enum_Rep => Enum_Rep :
1318 begin
1319 -- X'Enum_Rep (Y) expands to
1321 -- target-type (Y)
1323 -- This is simply a direct conversion from the enumeration type
1324 -- to the target integer type, which is treated by Gigi as a normal
1325 -- integer conversion, treating the enumeration type as an integer,
1326 -- which is exactly what we want! We set Conversion_OK to make sure
1327 -- that the analyzer does not complain about what otherwise might
1328 -- be an illegal conversion.
1330 if Is_Non_Empty_List (Exprs) then
1331 Rewrite (N,
1332 OK_Convert_To (Typ, Relocate_Node (First (Exprs))));
1334 -- X'Enum_Rep where X is an enumeration literal is replaced by
1335 -- the literal value.
1337 elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then
1338 Rewrite (N,
1339 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref))));
1341 -- X'Enum_Rep where X is an object does a direct unchecked conversion
1342 -- of the object value, as described for the type case above.
1344 else
1345 Rewrite (N,
1346 OK_Convert_To (Typ, Relocate_Node (Pref)));
1347 end if;
1349 Set_Etype (N, Typ);
1350 Analyze_And_Resolve (N, Typ);
1352 end Enum_Rep;
1354 --------------
1355 -- Exponent --
1356 --------------
1358 -- Transforms 'Exponent into a call to the floating-point attribute
1359 -- function Exponent in Fat_xxx (where xxx is the root type)
1361 when Attribute_Exponent =>
1362 Expand_Fpt_Attribute_R (N);
1364 ------------------
1365 -- External_Tag --
1366 ------------------
1368 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
1370 when Attribute_External_Tag => External_Tag :
1371 begin
1372 Rewrite (N,
1373 Make_Function_Call (Loc,
1374 Name => New_Reference_To (RTE (RE_External_Tag), Loc),
1375 Parameter_Associations => New_List (
1376 Make_Attribute_Reference (Loc,
1377 Attribute_Name => Name_Tag,
1378 Prefix => Prefix (N)))));
1380 Analyze_And_Resolve (N, Standard_String);
1381 end External_Tag;
1383 -----------
1384 -- First --
1385 -----------
1387 when Attribute_First => declare
1388 Ptyp : constant Entity_Id := Etype (Pref);
1390 begin
1391 -- If the prefix type is a constrained packed array type which
1392 -- already has a Packed_Array_Type representation defined, then
1393 -- replace this attribute with a direct reference to 'First of the
1394 -- appropriate index subtype (since otherwise Gigi will try to give
1395 -- us the value of 'First for this implementation type).
1397 if Is_Constrained_Packed_Array (Ptyp) then
1398 Rewrite (N,
1399 Make_Attribute_Reference (Loc,
1400 Attribute_Name => Name_First,
1401 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
1402 Analyze_And_Resolve (N, Typ);
1404 elsif Is_Access_Type (Ptyp) then
1405 Apply_Access_Check (N);
1406 end if;
1407 end;
1409 ---------------
1410 -- First_Bit --
1411 ---------------
1413 -- We compute this if a component clause was present, otherwise
1414 -- we leave the computation up to Gigi, since we don't know what
1415 -- layout will be chosen.
1417 when Attribute_First_Bit => First_Bit :
1418 declare
1419 CE : constant Entity_Id := Entity (Selector_Name (Pref));
1421 begin
1422 if Known_Static_Component_Bit_Offset (CE) then
1423 Rewrite (N,
1424 Make_Integer_Literal (Loc,
1425 Component_Bit_Offset (CE) mod System_Storage_Unit));
1427 Analyze_And_Resolve (N, Typ);
1429 else
1430 Apply_Universal_Integer_Attribute_Checks (N);
1431 end if;
1432 end First_Bit;
1434 -----------------
1435 -- Fixed_Value --
1436 -----------------
1438 -- We transform:
1440 -- fixtype'Fixed_Value (integer-value)
1442 -- into
1444 -- fixtype(integer-value)
1446 -- we do all the required analysis of the conversion here, because
1447 -- we do not want this to go through the fixed-point conversion
1448 -- circuits. Note that gigi always treats fixed-point as equivalent
1449 -- to the corresponding integer type anyway.
1451 when Attribute_Fixed_Value => Fixed_Value :
1452 begin
1453 Rewrite (N,
1454 Make_Type_Conversion (Loc,
1455 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
1456 Expression => Relocate_Node (First (Exprs))));
1457 Set_Etype (N, Entity (Pref));
1458 Set_Analyzed (N);
1459 Apply_Type_Conversion_Checks (N);
1460 end Fixed_Value;
1462 -----------
1463 -- Floor --
1464 -----------
1466 -- Transforms 'Floor into a call to the floating-point attribute
1467 -- function Floor in Fat_xxx (where xxx is the root type)
1469 when Attribute_Floor =>
1470 Expand_Fpt_Attribute_R (N);
1472 ----------
1473 -- Fore --
1474 ----------
1476 -- For the fixed-point type Typ:
1478 -- Typ'Fore
1480 -- expands into
1482 -- Result_Type (System.Fore (Long_Long_Float (Type'First)),
1483 -- Long_Long_Float (Type'Last))
1485 -- Note that we know that the type is a non-static subtype, or Fore
1486 -- would have itself been computed dynamically in Eval_Attribute.
1488 when Attribute_Fore => Fore :
1489 declare
1490 Ptyp : constant Entity_Id := Etype (Pref);
1492 begin
1493 Rewrite (N,
1494 Convert_To (Typ,
1495 Make_Function_Call (Loc,
1496 Name => New_Reference_To (RTE (RE_Fore), Loc),
1498 Parameter_Associations => New_List (
1499 Convert_To (Standard_Long_Long_Float,
1500 Make_Attribute_Reference (Loc,
1501 Prefix => New_Reference_To (Ptyp, Loc),
1502 Attribute_Name => Name_First)),
1504 Convert_To (Standard_Long_Long_Float,
1505 Make_Attribute_Reference (Loc,
1506 Prefix => New_Reference_To (Ptyp, Loc),
1507 Attribute_Name => Name_Last))))));
1509 Analyze_And_Resolve (N, Typ);
1510 end Fore;
1512 --------------
1513 -- Fraction --
1514 --------------
1516 -- Transforms 'Fraction into a call to the floating-point attribute
1517 -- function Fraction in Fat_xxx (where xxx is the root type)
1519 when Attribute_Fraction =>
1520 Expand_Fpt_Attribute_R (N);
1522 --------------
1523 -- Identity --
1524 --------------
1526 -- For an exception returns a reference to the exception data:
1527 -- Exception_Id!(Prefix'Reference)
1529 -- For a task it returns a reference to the _task_id component of
1530 -- corresponding record:
1532 -- taskV!(Prefix)._Task_Id, converted to the type Task_ID defined
1534 -- in Ada.Task_Identification.
1536 when Attribute_Identity => Identity : declare
1537 Id_Kind : Entity_Id;
1539 begin
1540 if Etype (Pref) = Standard_Exception_Type then
1541 Id_Kind := RTE (RE_Exception_Id);
1543 if Present (Renamed_Object (Entity (Pref))) then
1544 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
1545 end if;
1547 Rewrite (N,
1548 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
1549 else
1550 Id_Kind := RTE (RO_AT_Task_ID);
1552 Rewrite (N,
1553 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
1554 end if;
1556 Analyze_And_Resolve (N, Id_Kind);
1557 end Identity;
1559 -----------
1560 -- Image --
1561 -----------
1563 -- Image attribute is handled in separate unit Exp_Imgv
1565 when Attribute_Image =>
1566 Exp_Imgv.Expand_Image_Attribute (N);
1568 ---------
1569 -- Img --
1570 ---------
1572 -- X'Img is expanded to typ'Image (X), where typ is the type of X
1574 when Attribute_Img => Img :
1575 begin
1576 Rewrite (N,
1577 Make_Attribute_Reference (Loc,
1578 Prefix => New_Reference_To (Etype (Pref), Loc),
1579 Attribute_Name => Name_Image,
1580 Expressions => New_List (Relocate_Node (Pref))));
1582 Analyze_And_Resolve (N, Standard_String);
1583 end Img;
1585 -----------
1586 -- Input --
1587 -----------
1589 when Attribute_Input => Input : declare
1590 P_Type : constant Entity_Id := Entity (Pref);
1591 B_Type : constant Entity_Id := Base_Type (P_Type);
1592 U_Type : constant Entity_Id := Underlying_Type (P_Type);
1593 Strm : constant Node_Id := First (Exprs);
1594 Fname : Entity_Id;
1595 Decl : Node_Id;
1596 Call : Node_Id;
1597 Prag : Node_Id;
1598 Arg2 : Node_Id;
1599 Rfunc : Node_Id;
1601 Cntrl : Node_Id := Empty;
1602 -- Value for controlling argument in call. Always Empty except in
1603 -- the dispatching (class-wide type) case, where it is a reference
1604 -- to the dummy object initialized to the right internal tag.
1606 begin
1607 -- If no underlying type, we have an error that will be diagnosed
1608 -- elsewhere, so here we just completely ignore the expansion.
1610 if No (U_Type) then
1611 return;
1612 end if;
1614 -- If there is a TSS for Input, just call it
1616 Fname := Find_Inherited_TSS (P_Type, Name_uInput);
1618 if Present (Fname) then
1619 null;
1621 else
1622 -- If there is a Stream_Convert pragma, use it, we rewrite
1624 -- sourcetyp'Input (stream)
1626 -- as
1628 -- sourcetyp (streamread (strmtyp'Input (stream)));
1630 -- where stmrearead is the given Read function that converts
1631 -- an argument of type strmtyp to type sourcetyp or a type
1632 -- from which it is derived. The extra conversion is required
1633 -- for the derived case.
1635 Prag :=
1636 Get_Rep_Pragma
1637 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
1639 if Present (Prag) then
1640 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
1641 Rfunc := Entity (Expression (Arg2));
1643 Rewrite (N,
1644 Convert_To (B_Type,
1645 Make_Function_Call (Loc,
1646 Name => New_Occurrence_Of (Rfunc, Loc),
1647 Parameter_Associations => New_List (
1648 Make_Attribute_Reference (Loc,
1649 Prefix =>
1650 New_Occurrence_Of
1651 (Etype (First_Formal (Rfunc)), Loc),
1652 Attribute_Name => Name_Input,
1653 Expressions => Exprs)))));
1655 Analyze_And_Resolve (N, B_Type);
1656 return;
1658 -- Elementary types
1660 elsif Is_Elementary_Type (U_Type) then
1662 -- A special case arises if we have a defined _Read routine,
1663 -- since in this case we are required to call this routine.
1665 if Present (TSS (B_Type, Name_uRead)) then
1666 Build_Record_Or_Elementary_Input_Function
1667 (Loc, U_Type, Decl, Fname);
1668 Insert_Action (N, Decl);
1670 -- For normal cases, we call the I_xxx routine directly
1672 else
1673 Rewrite (N, Build_Elementary_Input_Call (N));
1674 Analyze_And_Resolve (N, P_Type);
1675 return;
1676 end if;
1678 -- Array type case
1680 elsif Is_Array_Type (U_Type) then
1681 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
1682 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
1684 -- Dispatching case with class-wide type
1686 elsif Is_Class_Wide_Type (P_Type) then
1688 declare
1689 Rtyp : constant Entity_Id := Root_Type (P_Type);
1690 Dnn : Entity_Id;
1691 Decl : Node_Id;
1693 begin
1694 -- Read the internal tag (RM 13.13.2(34)) and use it to
1695 -- initialize a dummy tag object:
1697 -- Dnn : Ada.Tags.Tag
1698 -- := Internal_Tag (String'Input (Strm));
1700 -- This dummy object is used only to provide a controlling
1701 -- argument for the eventual _Input call.
1703 Dnn :=
1704 Make_Defining_Identifier (Loc,
1705 Chars => New_Internal_Name ('D'));
1707 Decl :=
1708 Make_Object_Declaration (Loc,
1709 Defining_Identifier => Dnn,
1710 Object_Definition =>
1711 New_Occurrence_Of (RTE (RE_Tag), Loc),
1712 Expression =>
1713 Make_Function_Call (Loc,
1714 Name =>
1715 New_Occurrence_Of (RTE (RE_Internal_Tag), Loc),
1716 Parameter_Associations => New_List (
1717 Make_Attribute_Reference (Loc,
1718 Prefix =>
1719 New_Occurrence_Of (Standard_String, Loc),
1720 Attribute_Name => Name_Input,
1721 Expressions => New_List (
1722 Relocate_Node
1723 (Duplicate_Subexpr (Strm)))))));
1725 Insert_Action (N, Decl);
1727 -- Now we need to get the entity for the call, and construct
1728 -- a function call node, where we preset a reference to Dnn
1729 -- as the controlling argument (doing an unchecked
1730 -- conversion to the tagged type to make it look like
1731 -- a real tagged object).
1733 Fname := Find_Prim_Op (Rtyp, Name_uInput);
1734 Cntrl := Unchecked_Convert_To (Rtyp,
1735 New_Occurrence_Of (Dnn, Loc));
1736 Set_Etype (Cntrl, Rtyp);
1737 Set_Parent (Cntrl, N);
1738 end;
1740 -- For tagged types, use the primitive Input function
1742 elsif Is_Tagged_Type (U_Type) then
1743 Fname := Find_Prim_Op (U_Type, Name_uInput);
1745 -- All other record type cases, including protected records.
1746 -- The latter only arise for expander generated code for
1747 -- handling shared passive partition access.
1749 else
1750 pragma Assert
1751 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
1753 Build_Record_Or_Elementary_Input_Function
1754 (Loc, Base_Type (U_Type), Decl, Fname);
1755 Insert_Action (N, Decl);
1756 end if;
1757 end if;
1759 -- If we fall through, Fname is the function to be called. The
1760 -- result is obtained by calling the appropriate function, then
1761 -- converting the result. The conversion does a subtype check.
1763 Call :=
1764 Make_Function_Call (Loc,
1765 Name => New_Occurrence_Of (Fname, Loc),
1766 Parameter_Associations => New_List (
1767 Relocate_Node (Strm)));
1769 Set_Controlling_Argument (Call, Cntrl);
1770 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
1771 Analyze_And_Resolve (N, P_Type);
1772 end Input;
1774 -------------------
1775 -- Integer_Value --
1776 -------------------
1778 -- We transform
1780 -- inttype'Fixed_Value (fixed-value)
1782 -- into
1784 -- inttype(integer-value))
1786 -- we do all the required analysis of the conversion here, because
1787 -- we do not want this to go through the fixed-point conversion
1788 -- circuits. Note that gigi always treats fixed-point as equivalent
1789 -- to the corresponding integer type anyway.
1791 when Attribute_Integer_Value => Integer_Value :
1792 begin
1793 Rewrite (N,
1794 Make_Type_Conversion (Loc,
1795 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
1796 Expression => Relocate_Node (First (Exprs))));
1797 Set_Etype (N, Entity (Pref));
1798 Set_Analyzed (N);
1799 Apply_Type_Conversion_Checks (N);
1800 end Integer_Value;
1802 ----------
1803 -- Last --
1804 ----------
1806 when Attribute_Last => declare
1807 Ptyp : constant Entity_Id := Etype (Pref);
1809 begin
1810 -- If the prefix type is a constrained packed array type which
1811 -- already has a Packed_Array_Type representation defined, then
1812 -- replace this attribute with a direct reference to 'Last of the
1813 -- appropriate index subtype (since otherwise Gigi will try to give
1814 -- us the value of 'Last for this implementation type).
1816 if Is_Constrained_Packed_Array (Ptyp) then
1817 Rewrite (N,
1818 Make_Attribute_Reference (Loc,
1819 Attribute_Name => Name_Last,
1820 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
1821 Analyze_And_Resolve (N, Typ);
1823 elsif Is_Access_Type (Ptyp) then
1824 Apply_Access_Check (N);
1825 end if;
1826 end;
1828 --------------
1829 -- Last_Bit --
1830 --------------
1832 -- We compute this if a component clause was present, otherwise
1833 -- we leave the computation up to Gigi, since we don't know what
1834 -- layout will be chosen.
1836 when Attribute_Last_Bit => Last_Bit :
1837 declare
1838 CE : constant Entity_Id := Entity (Selector_Name (Pref));
1840 begin
1841 if Known_Static_Component_Bit_Offset (CE)
1842 and then Known_Static_Esize (CE)
1843 then
1844 Rewrite (N,
1845 Make_Integer_Literal (Loc,
1846 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
1847 + Esize (CE) - 1));
1849 Analyze_And_Resolve (N, Typ);
1851 else
1852 Apply_Universal_Integer_Attribute_Checks (N);
1853 end if;
1854 end Last_Bit;
1856 ------------------
1857 -- Leading_Part --
1858 ------------------
1860 -- Transforms 'Leading_Part into a call to the floating-point attribute
1861 -- function Leading_Part in Fat_xxx (where xxx is the root type)
1863 -- Note: strictly, we should have special case code to deal with
1864 -- absurdly large positive arguments (greater than Integer'Last),
1865 -- which result in returning the first argument unchanged, but it
1866 -- hardly seems worth the effort. We raise constraint error for
1867 -- absurdly negative arguments which is fine.
1869 when Attribute_Leading_Part =>
1870 Expand_Fpt_Attribute_RI (N);
1872 ------------
1873 -- Length --
1874 ------------
1876 when Attribute_Length => declare
1877 Ptyp : constant Entity_Id := Etype (Pref);
1878 Ityp : Entity_Id;
1879 Xnum : Uint;
1881 begin
1882 -- Processing for packed array types
1884 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
1885 Ityp := Get_Index_Subtype (N);
1887 -- If the index type, Ityp, is an enumeration type with
1888 -- holes, then we calculate X'Length explicitly using
1890 -- Typ'Max
1891 -- (0, Ityp'Pos (X'Last (N)) -
1892 -- Ityp'Pos (X'First (N)) + 1);
1894 -- Since the bounds in the template are the representation
1895 -- values and gigi would get the wrong value.
1897 if Is_Enumeration_Type (Ityp)
1898 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
1899 then
1900 if No (Exprs) then
1901 Xnum := Uint_1;
1902 else
1903 Xnum := Expr_Value (First (Expressions (N)));
1904 end if;
1906 Rewrite (N,
1907 Make_Attribute_Reference (Loc,
1908 Prefix => New_Occurrence_Of (Typ, Loc),
1909 Attribute_Name => Name_Max,
1910 Expressions => New_List
1911 (Make_Integer_Literal (Loc, 0),
1913 Make_Op_Add (Loc,
1914 Left_Opnd =>
1915 Make_Op_Subtract (Loc,
1916 Left_Opnd =>
1917 Make_Attribute_Reference (Loc,
1918 Prefix => New_Occurrence_Of (Ityp, Loc),
1919 Attribute_Name => Name_Pos,
1921 Expressions => New_List (
1922 Make_Attribute_Reference (Loc,
1923 Prefix => Duplicate_Subexpr (Pref),
1924 Attribute_Name => Name_Last,
1925 Expressions => New_List (
1926 Make_Integer_Literal (Loc, Xnum))))),
1928 Right_Opnd =>
1929 Make_Attribute_Reference (Loc,
1930 Prefix => New_Occurrence_Of (Ityp, Loc),
1931 Attribute_Name => Name_Pos,
1933 Expressions => New_List (
1934 Make_Attribute_Reference (Loc,
1935 Prefix => Duplicate_Subexpr (Pref),
1936 Attribute_Name => Name_First,
1937 Expressions => New_List (
1938 Make_Integer_Literal (Loc, Xnum)))))),
1940 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
1942 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1943 return;
1945 -- If the prefix type is a constrained packed array type which
1946 -- already has a Packed_Array_Type representation defined, then
1947 -- replace this attribute with a direct reference to 'Range_Length
1948 -- of the appropriate index subtype (since otherwise Gigi will try
1949 -- to give us the value of 'Length for this implementation type).
1951 elsif Is_Constrained (Ptyp) then
1952 Rewrite (N,
1953 Make_Attribute_Reference (Loc,
1954 Attribute_Name => Name_Range_Length,
1955 Prefix => New_Reference_To (Ityp, Loc)));
1956 Analyze_And_Resolve (N, Typ);
1957 end if;
1959 -- If we have a packed array that is not bit packed, which was
1961 -- Access type case
1963 elsif Is_Access_Type (Ptyp) then
1964 Apply_Access_Check (N);
1966 -- If the designated type is a packed array type, then we
1967 -- convert the reference to:
1969 -- typ'Max (0, 1 +
1970 -- xtyp'Pos (Pref'Last (Expr)) -
1971 -- xtyp'Pos (Pref'First (Expr)));
1973 -- This is a bit complex, but it is the easiest thing to do
1974 -- that works in all cases including enum types with holes
1975 -- xtyp here is the appropriate index type.
1977 declare
1978 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
1979 Xtyp : Entity_Id;
1981 begin
1982 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
1983 Xtyp := Get_Index_Subtype (N);
1985 Rewrite (N,
1986 Make_Attribute_Reference (Loc,
1987 Prefix => New_Occurrence_Of (Typ, Loc),
1988 Attribute_Name => Name_Max,
1989 Expressions => New_List (
1990 Make_Integer_Literal (Loc, 0),
1992 Make_Op_Add (Loc,
1993 Make_Integer_Literal (Loc, 1),
1994 Make_Op_Subtract (Loc,
1995 Left_Opnd =>
1996 Make_Attribute_Reference (Loc,
1997 Prefix => New_Occurrence_Of (Xtyp, Loc),
1998 Attribute_Name => Name_Pos,
1999 Expressions => New_List (
2000 Make_Attribute_Reference (Loc,
2001 Prefix => Duplicate_Subexpr (Pref),
2002 Attribute_Name => Name_Last,
2003 Expressions =>
2004 New_Copy_List (Exprs)))),
2006 Right_Opnd =>
2007 Make_Attribute_Reference (Loc,
2008 Prefix => New_Occurrence_Of (Xtyp, Loc),
2009 Attribute_Name => Name_Pos,
2010 Expressions => New_List (
2011 Make_Attribute_Reference (Loc,
2012 Prefix => Duplicate_Subexpr (Pref),
2013 Attribute_Name => Name_First,
2014 Expressions =>
2015 New_Copy_List (Exprs)))))))));
2017 Analyze_And_Resolve (N, Typ);
2018 end if;
2019 end;
2021 -- Otherwise leave it to gigi
2023 else
2024 Apply_Universal_Integer_Attribute_Checks (N);
2025 end if;
2026 end;
2028 -------------
2029 -- Machine --
2030 -------------
2032 -- Transforms 'Machine into a call to the floating-point attribute
2033 -- function Machine in Fat_xxx (where xxx is the root type)
2035 when Attribute_Machine =>
2036 Expand_Fpt_Attribute_R (N);
2038 ------------------
2039 -- Machine_Size --
2040 ------------------
2042 -- Machine_Size is equivalent to Object_Size, so transform it into
2043 -- Object_Size and that way Gigi never sees Machine_Size.
2045 when Attribute_Machine_Size =>
2046 Rewrite (N,
2047 Make_Attribute_Reference (Loc,
2048 Prefix => Prefix (N),
2049 Attribute_Name => Name_Object_Size));
2051 Analyze_And_Resolve (N, Typ);
2053 --------------
2054 -- Mantissa --
2055 --------------
2057 -- The only case that can get this far is the dynamic case of the
2058 -- old Ada 83 Mantissa attribute for the fixed-point case. For this
2059 -- case, we expand:
2061 -- typ'Mantissa
2063 -- into
2065 -- ityp (System.Mantissa.Mantissa_Value
2066 -- (Integer'Integer_Value (typ'First),
2067 -- Integer'Integer_Value (typ'Last)));
2069 when Attribute_Mantissa => Mantissa : declare
2070 Ptyp : constant Entity_Id := Etype (Pref);
2072 begin
2073 Rewrite (N,
2074 Convert_To (Typ,
2075 Make_Function_Call (Loc,
2076 Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
2078 Parameter_Associations => New_List (
2080 Make_Attribute_Reference (Loc,
2081 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2082 Attribute_Name => Name_Integer_Value,
2083 Expressions => New_List (
2085 Make_Attribute_Reference (Loc,
2086 Prefix => New_Occurrence_Of (Ptyp, Loc),
2087 Attribute_Name => Name_First))),
2089 Make_Attribute_Reference (Loc,
2090 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2091 Attribute_Name => Name_Integer_Value,
2092 Expressions => New_List (
2094 Make_Attribute_Reference (Loc,
2095 Prefix => New_Occurrence_Of (Ptyp, Loc),
2096 Attribute_Name => Name_Last)))))));
2098 Analyze_And_Resolve (N, Typ);
2099 end Mantissa;
2101 -----------
2102 -- Model --
2103 -----------
2105 -- Transforms 'Model into a call to the floating-point attribute
2106 -- function Model in Fat_xxx (where xxx is the root type)
2108 when Attribute_Model =>
2109 Expand_Fpt_Attribute_R (N);
2111 -----------------
2112 -- Object_Size --
2113 -----------------
2115 -- The processing for Object_Size shares the processing for Size
2117 ------------
2118 -- Output --
2119 ------------
2121 when Attribute_Output => Output : declare
2122 P_Type : constant Entity_Id := Entity (Pref);
2123 B_Type : constant Entity_Id := Base_Type (P_Type);
2124 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2125 Pname : Entity_Id;
2126 Decl : Node_Id;
2127 Prag : Node_Id;
2128 Arg3 : Node_Id;
2129 Wfunc : Node_Id;
2131 begin
2132 -- If no underlying type, we have an error that will be diagnosed
2133 -- elsewhere, so here we just completely ignore the expansion.
2135 if No (U_Type) then
2136 return;
2137 end if;
2139 -- If TSS for Output is present, just call it
2141 Pname := Find_Inherited_TSS (P_Type, Name_uOutput);
2143 if Present (Pname) then
2144 null;
2146 else
2147 -- If there is a Stream_Convert pragma, use it, we rewrite
2149 -- sourcetyp'Output (stream, Item)
2151 -- as
2153 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
2155 -- where strmwrite is the given Write function that converts
2156 -- an argument of type sourcetyp or a type acctyp, from which
2157 -- it is derived to type strmtyp. The conversion to acttyp is
2158 -- required for the derived case.
2160 Prag :=
2161 Get_Rep_Pragma
2162 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
2164 if Present (Prag) then
2165 Arg3 :=
2166 Next (Next (First (Pragma_Argument_Associations (Prag))));
2167 Wfunc := Entity (Expression (Arg3));
2169 Rewrite (N,
2170 Make_Attribute_Reference (Loc,
2171 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
2172 Attribute_Name => Name_Output,
2173 Expressions => New_List (
2174 Relocate_Node (First (Exprs)),
2175 Make_Function_Call (Loc,
2176 Name => New_Occurrence_Of (Wfunc, Loc),
2177 Parameter_Associations => New_List (
2178 Convert_To (Etype (First_Formal (Wfunc)),
2179 Relocate_Node (Next (First (Exprs)))))))));
2181 Analyze (N);
2182 return;
2184 -- For elementary types, we call the W_xxx routine directly.
2185 -- Note that the effect of Write and Output is identical for
2186 -- the case of an elementary type, since there are no
2187 -- discriminants or bounds.
2189 elsif Is_Elementary_Type (U_Type) then
2191 -- A special case arises if we have a defined _Write routine,
2192 -- since in this case we are required to call this routine.
2194 if Present (TSS (B_Type, Name_uWrite)) then
2195 Build_Record_Or_Elementary_Output_Procedure
2196 (Loc, U_Type, Decl, Pname);
2197 Insert_Action (N, Decl);
2199 -- For normal cases, we call the W_xxx routine directly
2201 else
2202 Rewrite (N, Build_Elementary_Write_Call (N));
2203 Analyze (N);
2204 return;
2205 end if;
2207 -- Array type case
2209 elsif Is_Array_Type (U_Type) then
2210 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
2211 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2213 -- Class-wide case, first output external tag, then dispatch
2214 -- to the appropriate primitive Output function (RM 13.13.2(31)).
2216 elsif Is_Class_Wide_Type (P_Type) then
2217 Tag_Write : declare
2218 Strm : constant Node_Id := First (Exprs);
2219 Item : constant Node_Id := Next (Strm);
2221 begin
2222 -- The code is:
2223 -- String'Output (Strm, External_Tag (Item'Tag))
2225 Insert_Action (N,
2226 Make_Attribute_Reference (Loc,
2227 Prefix => New_Occurrence_Of (Standard_String, Loc),
2228 Attribute_Name => Name_Output,
2229 Expressions => New_List (
2230 Relocate_Node (Duplicate_Subexpr (Strm)),
2231 Make_Function_Call (Loc,
2232 Name =>
2233 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
2234 Parameter_Associations => New_List (
2235 Make_Attribute_Reference (Loc,
2236 Prefix =>
2237 Relocate_Node
2238 (Duplicate_Subexpr (Item, Name_Req => True)),
2239 Attribute_Name => Name_Tag))))));
2240 end Tag_Write;
2242 Pname := Find_Prim_Op (U_Type, Name_uOutput);
2244 -- Tagged type case, use the primitive Output function
2246 elsif Is_Tagged_Type (U_Type) then
2247 Pname := Find_Prim_Op (U_Type, Name_uOutput);
2249 -- All other record type cases, including protected records.
2250 -- The latter only arise for expander generated code for
2251 -- handling shared passive partition access.
2253 else
2254 pragma Assert
2255 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2257 Build_Record_Or_Elementary_Output_Procedure
2258 (Loc, Base_Type (U_Type), Decl, Pname);
2259 Insert_Action (N, Decl);
2260 end if;
2261 end if;
2263 -- If we fall through, Pname is the name of the procedure to call
2265 Rewrite_Stream_Proc_Call (Pname);
2266 end Output;
2268 ---------
2269 -- Pos --
2270 ---------
2272 -- For enumeration types with a standard representation, Pos is
2273 -- handled by Gigi.
2275 -- For enumeration types, with a non-standard representation we
2276 -- generate a call to the _Rep_To_Pos function created when the
2277 -- type was frozen. The call has the form
2279 -- _rep_to_pos (expr, True)
2281 -- The parameter True causes Program_Error to be raised if the
2282 -- expression has an invalid representation.
2284 -- For integer types, Pos is equivalent to a simple integer
2285 -- conversion and we rewrite it as such
2287 when Attribute_Pos => Pos :
2288 declare
2289 Etyp : Entity_Id := Base_Type (Entity (Pref));
2291 begin
2292 -- Deal with zero/non-zero boolean values
2294 if Is_Boolean_Type (Etyp) then
2295 Adjust_Condition (First (Exprs));
2296 Etyp := Standard_Boolean;
2297 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
2298 end if;
2300 -- Case of enumeration type
2302 if Is_Enumeration_Type (Etyp) then
2304 -- Non-standard enumeration type (generate call)
2306 if Present (Enum_Pos_To_Rep (Etyp)) then
2307 Append_To (Exprs, New_Occurrence_Of (Standard_True, Loc));
2309 Rewrite (N,
2310 Convert_To (Typ,
2311 Make_Function_Call (Loc,
2312 Name =>
2313 New_Reference_To (TSS (Etyp, Name_uRep_To_Pos), Loc),
2314 Parameter_Associations => Exprs)));
2316 Analyze_And_Resolve (N, Typ);
2318 -- Standard enumeration type (do universal integer check)
2320 else
2321 Apply_Universal_Integer_Attribute_Checks (N);
2322 end if;
2324 -- Deal with integer types (replace by conversion)
2326 elsif Is_Integer_Type (Etyp) then
2327 Rewrite (N, Convert_To (Typ, First (Exprs)));
2328 Analyze_And_Resolve (N, Typ);
2329 end if;
2331 end Pos;
2333 --------------
2334 -- Position --
2335 --------------
2337 -- We compute this if a component clause was present, otherwise
2338 -- we leave the computation up to Gigi, since we don't know what
2339 -- layout will be chosen.
2341 when Attribute_Position => Position :
2342 declare
2343 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2345 begin
2346 if Present (Component_Clause (CE)) then
2347 Rewrite (N,
2348 Make_Integer_Literal (Loc,
2349 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
2350 Analyze_And_Resolve (N, Typ);
2352 else
2353 Apply_Universal_Integer_Attribute_Checks (N);
2354 end if;
2355 end Position;
2357 ----------
2358 -- Pred --
2359 ----------
2361 -- 1. Deal with enumeration types with holes
2362 -- 2. For floating-point, generate call to attribute function
2363 -- 3. For other cases, deal with constraint checking
2365 when Attribute_Pred => Pred :
2366 declare
2367 Ptyp : constant Entity_Id := Base_Type (Etype (Pref));
2369 begin
2370 -- For enumeration types with non-standard representations, we
2371 -- expand typ'Pred (x) into
2373 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
2375 if Is_Enumeration_Type (Ptyp)
2376 and then Present (Enum_Pos_To_Rep (Ptyp))
2377 then
2378 -- Add Boolean parameter True, to request program errror if
2379 -- we have a bad representation on our hands.
2381 Append_To (Exprs, New_Occurrence_Of (Standard_True, Loc));
2383 Rewrite (N,
2384 Make_Indexed_Component (Loc,
2385 Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc),
2386 Expressions => New_List (
2387 Make_Op_Subtract (Loc,
2388 Left_Opnd =>
2389 Make_Function_Call (Loc,
2390 Name =>
2391 New_Reference_To (TSS (Ptyp, Name_uRep_To_Pos), Loc),
2392 Parameter_Associations => Exprs),
2393 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
2395 Analyze_And_Resolve (N, Typ);
2397 -- For floating-point, we transform 'Pred into a call to the Pred
2398 -- floating-point attribute function in Fat_xxx (xxx is root type)
2400 elsif Is_Floating_Point_Type (Ptyp) then
2401 Expand_Fpt_Attribute_R (N);
2402 Analyze_And_Resolve (N, Typ);
2404 -- For modular types, nothing to do (no overflow, since wraps)
2406 elsif Is_Modular_Integer_Type (Ptyp) then
2407 null;
2409 -- For other types, if range checking is enabled, we must generate
2410 -- a check if overflow checking is enabled.
2412 elsif not Overflow_Checks_Suppressed (Ptyp) then
2413 Expand_Pred_Succ (N);
2414 end if;
2416 end Pred;
2418 ------------------
2419 -- Range_Length --
2420 ------------------
2422 when Attribute_Range_Length => Range_Length : declare
2423 P_Type : constant Entity_Id := Etype (Pref);
2425 begin
2426 -- The only special processing required is for the case where
2427 -- Range_Length is applied to an enumeration type with holes.
2428 -- In this case we transform
2430 -- X'Range_Length
2432 -- to
2434 -- X'Pos (X'Last) - X'Pos (X'First) + 1
2436 -- So that the result reflects the proper Pos values instead
2437 -- of the underlying representations.
2439 if Is_Enumeration_Type (P_Type)
2440 and then Has_Non_Standard_Rep (P_Type)
2441 then
2442 Rewrite (N,
2443 Make_Op_Add (Loc,
2444 Left_Opnd =>
2445 Make_Op_Subtract (Loc,
2446 Left_Opnd =>
2447 Make_Attribute_Reference (Loc,
2448 Attribute_Name => Name_Pos,
2449 Prefix => New_Occurrence_Of (P_Type, Loc),
2450 Expressions => New_List (
2451 Make_Attribute_Reference (Loc,
2452 Attribute_Name => Name_Last,
2453 Prefix => New_Occurrence_Of (P_Type, Loc)))),
2455 Right_Opnd =>
2456 Make_Attribute_Reference (Loc,
2457 Attribute_Name => Name_Pos,
2458 Prefix => New_Occurrence_Of (P_Type, Loc),
2459 Expressions => New_List (
2460 Make_Attribute_Reference (Loc,
2461 Attribute_Name => Name_First,
2462 Prefix => New_Occurrence_Of (P_Type, Loc))))),
2464 Right_Opnd =>
2465 Make_Integer_Literal (Loc, 1)));
2467 Analyze_And_Resolve (N, Typ);
2469 -- For all other cases, attribute is handled by Gigi, but we need
2470 -- to deal with the case of the range check on a universal integer.
2472 else
2473 Apply_Universal_Integer_Attribute_Checks (N);
2474 end if;
2476 end Range_Length;
2478 ----------
2479 -- Read --
2480 ----------
2482 when Attribute_Read => Read : declare
2483 P_Type : constant Entity_Id := Entity (Pref);
2484 B_Type : constant Entity_Id := Base_Type (P_Type);
2485 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2486 Pname : Entity_Id;
2487 Decl : Node_Id;
2488 Prag : Node_Id;
2489 Arg2 : Node_Id;
2490 Rfunc : Node_Id;
2491 Lhs : Node_Id;
2492 Rhs : Node_Id;
2494 begin
2495 -- If no underlying type, we have an error that will be diagnosed
2496 -- elsewhere, so here we just completely ignore the expansion.
2498 if No (U_Type) then
2499 return;
2500 end if;
2502 -- The simple case, if there is a TSS for Read, just call it
2504 Pname := Find_Inherited_TSS (P_Type, Name_uRead);
2506 if Present (Pname) then
2507 null;
2509 else
2510 -- If there is a Stream_Convert pragma, use it, we rewrite
2512 -- sourcetyp'Read (stream, Item)
2514 -- as
2516 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
2518 -- where strmread is the given Read function that converts
2519 -- an argument of type strmtyp to type sourcetyp or a type
2520 -- from which it is derived. The conversion to sourcetyp
2521 -- is required in the latter case.
2523 -- A special case arises if Item is a type conversion in which
2524 -- case, we have to expand to:
2526 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
2528 -- where Itemx is the expression of the type conversion (i.e.
2529 -- the actual object), and typex is the type of Itemx.
2531 Prag :=
2532 Get_Rep_Pragma
2533 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
2535 if Present (Prag) then
2536 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
2537 Rfunc := Entity (Expression (Arg2));
2538 Lhs := Relocate_Node (Next (First (Exprs)));
2539 Rhs :=
2540 Convert_To (B_Type,
2541 Make_Function_Call (Loc,
2542 Name => New_Occurrence_Of (Rfunc, Loc),
2543 Parameter_Associations => New_List (
2544 Make_Attribute_Reference (Loc,
2545 Prefix =>
2546 New_Occurrence_Of
2547 (Etype (First_Formal (Rfunc)), Loc),
2548 Attribute_Name => Name_Input,
2549 Expressions => New_List (
2550 Relocate_Node (First (Exprs)))))));
2552 if Nkind (Lhs) = N_Type_Conversion then
2553 Lhs := Expression (Lhs);
2554 Rhs := Convert_To (Etype (Lhs), Rhs);
2555 end if;
2557 Rewrite (N,
2558 Make_Assignment_Statement (Loc,
2559 Name => Lhs,
2560 Expression => Rhs));
2561 Set_Assignment_OK (Lhs);
2562 Analyze (N);
2563 return;
2565 -- For elementary types, we call the I_xxx routine using the first
2566 -- parameter and then assign the result into the second parameter.
2567 -- We set Assignment_OK to deal with the conversion case.
2569 elsif Is_Elementary_Type (U_Type) then
2570 declare
2571 Lhs : Node_Id;
2572 Rhs : Node_Id;
2574 begin
2575 Lhs := Relocate_Node (Next (First (Exprs)));
2576 Rhs := Build_Elementary_Input_Call (N);
2578 if Nkind (Lhs) = N_Type_Conversion then
2579 Lhs := Expression (Lhs);
2580 Rhs := Convert_To (Etype (Lhs), Rhs);
2581 end if;
2583 Set_Assignment_OK (Lhs);
2585 Rewrite (N,
2586 Make_Assignment_Statement (Loc,
2587 Name => Lhs,
2588 Expression => Rhs));
2590 Analyze (N);
2591 return;
2592 end;
2594 -- Array type case
2596 elsif Is_Array_Type (U_Type) then
2597 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
2598 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2600 -- Tagged type case, use the primitive Read function. Note that
2601 -- this will dispatch in the class-wide case which is what we want
2603 elsif Is_Tagged_Type (U_Type) then
2604 Pname := Find_Prim_Op (U_Type, Name_uRead);
2606 -- All other record type cases, including protected records.
2607 -- The latter only arise for expander generated code for
2608 -- handling shared passive partition access.
2610 else
2611 pragma Assert
2612 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2614 if Has_Discriminants (U_Type)
2615 and then Present
2616 (Discriminant_Default_Value (First_Discriminant (U_Type)))
2617 then
2618 Build_Mutable_Record_Read_Procedure
2619 (Loc, Base_Type (U_Type), Decl, Pname);
2621 else
2622 Build_Record_Read_Procedure
2623 (Loc, Base_Type (U_Type), Decl, Pname);
2624 end if;
2626 -- Suppress checks, uninitialized or otherwise invalid
2627 -- data does not cause constraint errors to be raised for
2628 -- a complete record read.
2630 Insert_Action (N, Decl, All_Checks);
2631 end if;
2632 end if;
2634 Rewrite_Stream_Proc_Call (Pname);
2635 end Read;
2637 ---------------
2638 -- Remainder --
2639 ---------------
2641 -- Transforms 'Remainder into a call to the floating-point attribute
2642 -- function Remainder in Fat_xxx (where xxx is the root type)
2644 when Attribute_Remainder =>
2645 Expand_Fpt_Attribute_RR (N);
2647 -----------
2648 -- Round --
2649 -----------
2651 -- The handling of the Round attribute is quite delicate. The
2652 -- processing in Sem_Attr introduced a conversion to universal
2653 -- real, reflecting the semantics of Round, but we do not want
2654 -- anything to do with universal real at runtime, since this
2655 -- corresponds to using floating-point arithmetic.
2657 -- What we have now is that the Etype of the Round attribute
2658 -- correctly indicates the final result type. The operand of
2659 -- the Round is the conversion to universal real, described
2660 -- above, and the operand of this conversion is the actual
2661 -- operand of Round, which may be the special case of a fixed
2662 -- point multiplication or division (Etype = universal fixed)
2664 -- The exapander will expand first the operand of the conversion,
2665 -- then the conversion, and finally the round attribute itself,
2666 -- since we always work inside out. But we cannot simply process
2667 -- naively in this order. In the semantic world where universal
2668 -- fixed and real really exist and have infinite precision, there
2669 -- is no problem, but in the implementation world, where universal
2670 -- real is a floating-point type, we would get the wrong result.
2672 -- So the approach is as follows. First, when expanding a multiply
2673 -- or divide whose type is universal fixed, we do nothing at all,
2674 -- instead deferring the operation till later.
2676 -- The actual processing is done in Expand_N_Type_Conversion which
2677 -- handles the special case of Round by looking at its parent to
2678 -- see if it is a Round attribute, and if it is, handling the
2679 -- conversion (or its fixed multiply/divide child) in an appropriate
2680 -- manner.
2682 -- This means that by the time we get to expanding the Round attribute
2683 -- itself, the Round is nothing more than a type conversion (and will
2684 -- often be a null type conversion), so we just replace it with the
2685 -- appropriate conversion operation.
2687 when Attribute_Round =>
2688 Rewrite (N,
2689 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
2690 Analyze_And_Resolve (N);
2692 --------------
2693 -- Rounding --
2694 --------------
2696 -- Transforms 'Rounding into a call to the floating-point attribute
2697 -- function Rounding in Fat_xxx (where xxx is the root type)
2699 when Attribute_Rounding =>
2700 Expand_Fpt_Attribute_R (N);
2702 -------------
2703 -- Scaling --
2704 -------------
2706 -- Transforms 'Scaling into a call to the floating-point attribute
2707 -- function Scaling in Fat_xxx (where xxx is the root type)
2709 when Attribute_Scaling =>
2710 Expand_Fpt_Attribute_RI (N);
2712 ----------
2713 -- Size --
2714 ----------
2716 when Attribute_Size |
2717 Attribute_Object_Size |
2718 Attribute_Value_Size |
2719 Attribute_VADS_Size => Size :
2721 declare
2722 Ptyp : constant Entity_Id := Etype (Pref);
2723 New_Node : Node_Id;
2724 Siz : Uint;
2726 begin
2727 -- Processing for VADS_Size case. Note that this processing removes
2728 -- all traces of VADS_Size from the tree, and completes all required
2729 -- processing for VADS_Size by translating the attribute reference
2730 -- to an appropriate Size or Object_Size reference.
2732 if Id = Attribute_VADS_Size
2733 or else (Use_VADS_Size and then Id = Attribute_Size)
2734 then
2735 -- If the size is specified, then we simply use the specified
2736 -- size. This applies to both types and objects. The size of an
2737 -- object can be specified in the following ways:
2739 -- An explicit size object is given for an object
2740 -- A component size is specified for an indexed component
2741 -- A component clause is specified for a selected component
2742 -- The object is a component of a packed composite object
2744 -- If the size is specified, then VADS_Size of an object
2746 if (Is_Entity_Name (Pref)
2747 and then Present (Size_Clause (Entity (Pref))))
2748 or else
2749 (Nkind (Pref) = N_Component_Clause
2750 and then (Present (Component_Clause
2751 (Entity (Selector_Name (Pref))))
2752 or else Is_Packed (Etype (Prefix (Pref)))))
2753 or else
2754 (Nkind (Pref) = N_Indexed_Component
2755 and then (Component_Size (Etype (Prefix (Pref))) /= 0
2756 or else Is_Packed (Etype (Prefix (Pref)))))
2757 then
2758 Set_Attribute_Name (N, Name_Size);
2760 -- Otherwise if we have an object rather than a type, then the
2761 -- VADS_Size attribute applies to the type of the object, rather
2762 -- than the object itself. This is one of the respects in which
2763 -- VADS_Size differs from Size.
2765 else
2766 if (not Is_Entity_Name (Pref)
2767 or else not Is_Type (Entity (Pref)))
2768 and then (Is_Scalar_Type (Etype (Pref))
2769 or else Is_Constrained (Etype (Pref)))
2770 then
2771 Rewrite (Pref, New_Occurrence_Of (Etype (Pref), Loc));
2772 end if;
2774 -- For a scalar type for which no size was
2775 -- explicitly given, VADS_Size means Object_Size. This is the
2776 -- other respect in which VADS_Size differs from Size.
2778 if Is_Scalar_Type (Etype (Pref))
2779 and then No (Size_Clause (Etype (Pref)))
2780 then
2781 Set_Attribute_Name (N, Name_Object_Size);
2783 -- In all other cases, Size and VADS_Size are the sane
2785 else
2786 Set_Attribute_Name (N, Name_Size);
2787 end if;
2788 end if;
2789 end if;
2791 -- For class-wide types, transform X'Size into a call to
2792 -- the primitive operation _Size
2794 if Is_Class_Wide_Type (Ptyp) then
2795 New_Node :=
2796 Make_Function_Call (Loc,
2797 Name => New_Reference_To
2798 (Find_Prim_Op (Ptyp, Name_uSize), Loc),
2799 Parameter_Associations => New_List (Pref));
2801 if Typ /= Standard_Long_Long_Integer then
2803 -- The context is a specific integer type with which the
2804 -- original attribute was compatible. The function has a
2805 -- specific type as well, so to preserve the compatibility
2806 -- we must convert explicitly.
2808 New_Node := Convert_To (Typ, New_Node);
2809 end if;
2811 Rewrite (N, New_Node);
2812 Analyze_And_Resolve (N, Typ);
2813 return;
2815 -- For an array component, we can do Size in the front end
2816 -- if the component_size of the array is set.
2818 elsif Nkind (Pref) = N_Indexed_Component then
2819 Siz := Component_Size (Etype (Prefix (Pref)));
2821 -- For a record component, we can do Size in the front end
2822 -- if there is a component clause, or if the record is packed
2823 -- and the component's size is known at compile time.
2825 elsif Nkind (Pref) = N_Selected_Component then
2826 declare
2827 Rec : constant Entity_Id := Etype (Prefix (Pref));
2828 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
2830 begin
2831 if Present (Component_Clause (Comp)) then
2832 Siz := Esize (Comp);
2834 elsif Is_Packed (Rec) then
2835 Siz := RM_Size (Ptyp);
2837 else
2838 Apply_Universal_Integer_Attribute_Checks (N);
2839 return;
2840 end if;
2841 end;
2843 -- All other cases are handled by Gigi
2845 else
2846 Apply_Universal_Integer_Attribute_Checks (N);
2848 -- If we have Size applied to a formal parameter, that is a
2849 -- packed array subtype, then apply size to the actual subtype.
2851 if Is_Entity_Name (Pref)
2852 and then Is_Formal (Entity (Pref))
2853 and then Is_Array_Type (Etype (Pref))
2854 and then Is_Packed (Etype (Pref))
2855 then
2856 Rewrite (N,
2857 Make_Attribute_Reference (Loc,
2858 Prefix =>
2859 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
2860 Attribute_Name => Name_Size));
2861 Analyze_And_Resolve (N, Typ);
2862 end if;
2864 return;
2865 end if;
2867 -- Common processing for record and array component case
2869 if Siz /= 0 then
2870 Rewrite (N,
2871 Make_Integer_Literal (Loc, Siz));
2873 Analyze_And_Resolve (N, Typ);
2875 -- The result is not a static expression
2877 Set_Is_Static_Expression (N, False);
2878 end if;
2879 end Size;
2881 ------------------
2882 -- Storage_Pool --
2883 ------------------
2885 when Attribute_Storage_Pool =>
2886 Rewrite (N,
2887 Make_Type_Conversion (Loc,
2888 Subtype_Mark => New_Reference_To (Etype (N), Loc),
2889 Expression => New_Reference_To (Entity (N), Loc)));
2890 Analyze_And_Resolve (N, Typ);
2892 ------------------
2893 -- Storage_Size --
2894 ------------------
2896 when Attribute_Storage_Size => Storage_Size :
2897 declare
2898 Ptyp : constant Entity_Id := Etype (Pref);
2900 begin
2901 -- Access type case, always go to the root type
2903 -- The case of access types results in a value of zero for the case
2904 -- where no storage size attribute clause has been given. If a
2905 -- storage size has been given, then the attribute is converted
2906 -- to a reference to the variable used to hold this value.
2908 if Is_Access_Type (Ptyp) then
2909 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
2910 Rewrite (N,
2911 Make_Attribute_Reference (Loc,
2912 Prefix => New_Reference_To (Typ, Loc),
2913 Attribute_Name => Name_Max,
2914 Expressions => New_List (
2915 Make_Integer_Literal (Loc, 0),
2916 Convert_To (Typ,
2917 New_Reference_To
2918 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
2920 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
2921 Rewrite (N,
2922 OK_Convert_To (Typ,
2923 Make_Function_Call (Loc,
2924 Name => New_Reference_To (Find_Prim_Op (Etype (
2925 Associated_Storage_Pool (Root_Type (Ptyp))),
2926 Attribute_Name (N)), Loc),
2928 Parameter_Associations => New_List (New_Reference_To (
2929 Associated_Storage_Pool (Root_Type (Ptyp)), Loc)))));
2930 else
2931 Rewrite (N, Make_Integer_Literal (Loc, 0));
2932 end if;
2934 Analyze_And_Resolve (N, Typ);
2936 -- The case of a task type (an obsolescent feature) is handled the
2937 -- same way, seems as reasonable as anything, and it is what the
2938 -- ACVC tests (e.g. CD1009K) seem to expect.
2940 -- If there is no Storage_Size variable, then we return the default
2941 -- task stack size, otherwise, expand a Storage_Size attribute as
2942 -- follows:
2944 -- Typ (Adjust_Storage_Size (taskZ))
2946 -- except for the case of a task object which has a Storage_Size
2947 -- pragma:
2949 -- Typ (Adjust_Storage_Size (taskV!(name)._Size))
2951 else
2952 if not Present (Storage_Size_Variable (Ptyp)) then
2953 Rewrite (N,
2954 Convert_To (Typ,
2955 Make_Function_Call (Loc,
2956 Name =>
2957 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc))));
2959 else
2960 if not (Is_Entity_Name (Pref) and then
2961 Is_Task_Type (Entity (Pref))) and then
2962 Chars (Last_Entity (Corresponding_Record_Type (Ptyp))) =
2963 Name_uSize
2964 then
2965 Rewrite (N,
2966 Convert_To (Typ,
2967 Make_Function_Call (Loc,
2968 Name => New_Occurrence_Of (
2969 RTE (RE_Adjust_Storage_Size), Loc),
2970 Parameter_Associations =>
2971 New_List (
2972 Make_Selected_Component (Loc,
2973 Prefix =>
2974 Unchecked_Convert_To (
2975 Corresponding_Record_Type (Ptyp),
2976 New_Copy_Tree (Pref)),
2977 Selector_Name =>
2978 Make_Identifier (Loc, Name_uSize))))));
2980 -- Task not having Storage_Size pragma
2982 else
2983 Rewrite (N,
2984 Convert_To (Typ,
2985 Make_Function_Call (Loc,
2986 Name => New_Occurrence_Of (
2987 RTE (RE_Adjust_Storage_Size), Loc),
2988 Parameter_Associations =>
2989 New_List (
2990 New_Reference_To (
2991 Storage_Size_Variable (Ptyp), Loc)))));
2992 end if;
2994 Analyze_And_Resolve (N, Typ);
2995 end if;
2996 end if;
2997 end Storage_Size;
2999 ----------
3000 -- Succ --
3001 ----------
3003 -- 1. Deal with enumeration types with holes
3004 -- 2. For floating-point, generate call to attribute function
3005 -- 3. For other cases, deal with constraint checking
3007 when Attribute_Succ => Succ :
3008 declare
3009 Ptyp : constant Entity_Id := Base_Type (Etype (Pref));
3011 begin
3012 -- For enumeration types with non-standard representations, we
3013 -- expand typ'Succ (x) into
3015 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
3017 if Is_Enumeration_Type (Ptyp)
3018 and then Present (Enum_Pos_To_Rep (Ptyp))
3019 then
3020 -- Add Boolean parameter True, to request program errror if
3021 -- we have a bad representation on our hands.
3023 Append_To (Exprs, New_Occurrence_Of (Standard_True, Loc));
3025 Rewrite (N,
3026 Make_Indexed_Component (Loc,
3027 Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc),
3028 Expressions => New_List (
3029 Make_Op_Add (Loc,
3030 Left_Opnd =>
3031 Make_Function_Call (Loc,
3032 Name =>
3033 New_Reference_To (TSS (Ptyp, Name_uRep_To_Pos), Loc),
3034 Parameter_Associations => Exprs),
3035 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3037 Analyze_And_Resolve (N, Typ);
3039 -- For floating-point, we transform 'Succ into a call to the Succ
3040 -- floating-point attribute function in Fat_xxx (xxx is root type)
3042 elsif Is_Floating_Point_Type (Ptyp) then
3043 Expand_Fpt_Attribute_R (N);
3044 Analyze_And_Resolve (N, Typ);
3046 -- For modular types, nothing to do (no overflow, since wraps)
3048 elsif Is_Modular_Integer_Type (Ptyp) then
3049 null;
3051 -- For other types, if range checking is enabled, we must generate
3052 -- a check if overflow checking is enabled.
3054 elsif not Overflow_Checks_Suppressed (Ptyp) then
3055 Expand_Pred_Succ (N);
3056 end if;
3057 end Succ;
3059 ---------
3060 -- Tag --
3061 ---------
3063 -- Transforms X'Tag into a direct reference to the tag of X
3065 when Attribute_Tag => Tag :
3066 declare
3067 Ttyp : Entity_Id;
3068 Prefix_Is_Type : Boolean;
3070 begin
3071 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
3072 Ttyp := Entity (Pref);
3073 Prefix_Is_Type := True;
3074 else
3075 Ttyp := Etype (Pref);
3076 Prefix_Is_Type := False;
3077 end if;
3079 if Is_Class_Wide_Type (Ttyp) then
3080 Ttyp := Root_Type (Ttyp);
3081 end if;
3083 Ttyp := Underlying_Type (Ttyp);
3085 if Prefix_Is_Type then
3086 Rewrite (N,
3087 Unchecked_Convert_To (RTE (RE_Tag),
3088 New_Reference_To (Access_Disp_Table (Ttyp), Loc)));
3090 else
3091 Rewrite (N,
3092 Make_Selected_Component (Loc,
3093 Prefix => Relocate_Node (Pref),
3094 Selector_Name =>
3095 New_Reference_To (Tag_Component (Ttyp), Loc)));
3096 end if;
3098 Analyze_And_Resolve (N, RTE (RE_Tag));
3099 end Tag;
3101 ----------------
3102 -- Terminated --
3103 ----------------
3105 -- Transforms 'Terminated attribute into a call to Terminated function.
3107 when Attribute_Terminated => Terminated :
3108 begin
3109 if Restricted_Profile then
3110 Rewrite (N,
3111 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
3113 else
3114 Rewrite (N,
3115 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
3116 end if;
3118 Analyze_And_Resolve (N, Standard_Boolean);
3119 end Terminated;
3121 ----------------
3122 -- To_Address --
3123 ----------------
3125 -- Transforms System'To_Address (X) into unchecked conversion
3126 -- from (integral) type of X to type address.
3128 when Attribute_To_Address =>
3129 Rewrite (N,
3130 Unchecked_Convert_To (RTE (RE_Address),
3131 Relocate_Node (First (Exprs))));
3132 Analyze_And_Resolve (N, RTE (RE_Address));
3134 ----------------
3135 -- Truncation --
3136 ----------------
3138 -- Transforms 'Truncation into a call to the floating-point attribute
3139 -- function Truncation in Fat_xxx (where xxx is the root type)
3141 when Attribute_Truncation =>
3142 Expand_Fpt_Attribute_R (N);
3144 -----------------------
3145 -- Unbiased_Rounding --
3146 -----------------------
3148 -- Transforms 'Unbiased_Rounding into a call to the floating-point
3149 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
3150 -- root type)
3152 when Attribute_Unbiased_Rounding =>
3153 Expand_Fpt_Attribute_R (N);
3155 ----------------------
3156 -- Unchecked_Access --
3157 ----------------------
3159 when Attribute_Unchecked_Access =>
3160 Expand_Access_To_Type (N);
3162 -----------------
3163 -- UET_Address --
3164 -----------------
3166 when Attribute_UET_Address => UET_Address : declare
3167 Ent : constant Entity_Id :=
3168 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3170 begin
3171 Insert_Action (N,
3172 Make_Object_Declaration (Loc,
3173 Defining_Identifier => Ent,
3174 Aliased_Present => True,
3175 Object_Definition =>
3176 New_Occurrence_Of (RTE (RE_Address), Loc)));
3178 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
3179 -- in normal external form.
3181 Get_External_Unit_Name_String (Get_Unit_Name (Pref));
3182 Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
3183 Name_Len := Name_Len + 7;
3184 Name_Buffer (1 .. 7) := "__gnat_";
3185 Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP";
3186 Name_Len := Name_Len + 5;
3188 Set_Is_Imported (Ent);
3189 Set_Interface_Name (Ent,
3190 Make_String_Literal (Loc,
3191 Strval => String_From_Name_Buffer));
3193 Rewrite (N,
3194 Make_Attribute_Reference (Loc,
3195 Prefix => New_Occurrence_Of (Ent, Loc),
3196 Attribute_Name => Name_Address));
3198 Analyze_And_Resolve (N, Typ);
3199 end UET_Address;
3201 -------------------------
3202 -- Unrestricted_Access --
3203 -------------------------
3205 when Attribute_Unrestricted_Access =>
3206 Expand_Access_To_Type (N);
3208 ---------------
3209 -- VADS_Size --
3210 ---------------
3212 -- The processing for VADS_Size is shared with Size
3214 ---------
3215 -- Val --
3216 ---------
3218 -- For enumeration types with a standard representation, and for all
3219 -- other types, Val is handled by Gigi. For enumeration types with
3220 -- a non-standard representation we use the _Pos_To_Rep array that
3221 -- was created when the type was frozen.
3223 when Attribute_Val => Val :
3224 declare
3225 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
3227 begin
3228 if Is_Enumeration_Type (Etyp)
3229 and then Present (Enum_Pos_To_Rep (Etyp))
3230 then
3231 Rewrite (N,
3232 Make_Indexed_Component (Loc,
3233 Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc),
3234 Expressions => New_List (
3235 Convert_To (Standard_Integer,
3236 Relocate_Node (First (Exprs))))));
3238 Analyze_And_Resolve (N, Typ);
3239 end if;
3240 end Val;
3242 -----------
3243 -- Valid --
3244 -----------
3246 -- The code for valid is dependent on the particular types involved.
3247 -- See separate sections below for the generated code in each case.
3249 when Attribute_Valid => Valid :
3250 declare
3251 Ptyp : constant Entity_Id := Etype (Pref);
3252 Btyp : Entity_Id := Base_Type (Ptyp);
3253 Tst : Node_Id;
3255 function Make_Range_Test return Node_Id;
3256 -- Build the code for a range test of the form
3257 -- Btyp!(Pref) >= Btyp!(Ptyp'First)
3258 -- and then
3259 -- Btyp!(Pref) <= Btyp!(Ptyp'Last)
3261 function Make_Range_Test return Node_Id is
3262 begin
3263 return
3264 Make_And_Then (Loc,
3265 Left_Opnd =>
3266 Make_Op_Ge (Loc,
3267 Left_Opnd =>
3268 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3270 Right_Opnd =>
3271 Unchecked_Convert_To (Btyp,
3272 Make_Attribute_Reference (Loc,
3273 Prefix => New_Occurrence_Of (Ptyp, Loc),
3274 Attribute_Name => Name_First))),
3276 Right_Opnd =>
3277 Make_Op_Le (Loc,
3278 Left_Opnd =>
3279 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3281 Right_Opnd =>
3282 Unchecked_Convert_To (Btyp,
3283 Make_Attribute_Reference (Loc,
3284 Prefix => New_Occurrence_Of (Ptyp, Loc),
3285 Attribute_Name => Name_Last))));
3286 end Make_Range_Test;
3288 -- Start of processing for Attribute_Valid
3290 begin
3291 -- Floating-point case. This case is handled by the Valid attribute
3292 -- code in the floating-point attribute run-time library.
3294 if Is_Floating_Point_Type (Ptyp) then
3295 declare
3296 Rtp : constant Entity_Id := Root_Type (Etype (Pref));
3298 begin
3299 Expand_Fpt_Attribute (N, Rtp, New_List (
3300 Make_Attribute_Reference (Loc,
3301 Prefix => Unchecked_Convert_To (Rtp, Pref),
3302 Attribute_Name => Name_Unrestricted_Access)));
3304 -- One more task, we still need a range check. Required
3305 -- only if we have a constraint, since the Valid routine
3306 -- catches infinities properly (infinities are never valid).
3308 -- The way we do the range check is simply to create the
3309 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
3311 if not Subtypes_Statically_Match (Ptyp, Btyp) then
3312 Rewrite (N,
3313 Make_And_Then (Loc,
3314 Left_Opnd => Relocate_Node (N),
3315 Right_Opnd =>
3316 Make_In (Loc,
3317 Left_Opnd => Convert_To (Btyp, Pref),
3318 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
3319 end if;
3320 end;
3322 -- Enumeration type with holes
3324 -- For enumeration types with holes, the Pos value constructed by
3325 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
3326 -- second argument of False returns minus one for an invalid value,
3327 -- and the non-negative pos value for a valid value, so the
3328 -- expansion of X'Valid is simply:
3330 -- type(X)'Pos (X) >= 0
3332 -- We can't quite generate it that way because of the requirement
3333 -- for the non-standard second argument of False, so we have to
3334 -- explicitly create:
3336 -- _rep_to_pos (X, False) >= 0
3338 -- If we have an enumeration subtype, we also check that the
3339 -- value is in range:
3341 -- _rep_to_pos (X, False) >= 0
3342 -- and then
3343 -- (X >= type(X)'First and then type(X)'Last <= X)
3345 elsif Is_Enumeration_Type (Ptyp)
3346 and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp)))
3347 then
3348 Tst :=
3349 Make_Op_Ge (Loc,
3350 Left_Opnd =>
3351 Make_Function_Call (Loc,
3352 Name =>
3353 New_Reference_To
3354 (TSS (Base_Type (Ptyp), Name_uRep_To_Pos), Loc),
3355 Parameter_Associations => New_List (
3356 Pref,
3357 New_Occurrence_Of (Standard_False, Loc))),
3358 Right_Opnd => Make_Integer_Literal (Loc, 0));
3360 if Ptyp /= Btyp
3361 and then
3362 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
3363 or else
3364 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
3365 then
3366 -- The call to Make_Range_Test will create declarations
3367 -- that need a proper insertion point, but Pref is now
3368 -- attached to a node with no ancestor. Attach to tree
3369 -- even if it is to be rewritten below.
3371 Set_Parent (Tst, Parent (N));
3373 Tst :=
3374 Make_And_Then (Loc,
3375 Left_Opnd => Make_Range_Test,
3376 Right_Opnd => Tst);
3377 end if;
3379 Rewrite (N, Tst);
3381 -- Fortran convention booleans
3383 -- For the very special case of Fortran convention booleans, the
3384 -- value is always valid, since it is an integer with the semantics
3385 -- that non-zero is true, and any value is permissible.
3387 elsif Is_Boolean_Type (Ptyp)
3388 and then Convention (Ptyp) = Convention_Fortran
3389 then
3390 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3392 -- For biased representations, we will be doing an unchecked
3393 -- conversion without unbiasing the result. That means that
3394 -- the range test has to take this into account, and the
3395 -- proper form of the test is:
3397 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
3399 elsif Has_Biased_Representation (Ptyp) then
3400 Btyp := RTE (RE_Unsigned_32);
3401 Rewrite (N,
3402 Make_Op_Lt (Loc,
3403 Left_Opnd =>
3404 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3405 Right_Opnd =>
3406 Unchecked_Convert_To (Btyp,
3407 Make_Attribute_Reference (Loc,
3408 Prefix => New_Occurrence_Of (Ptyp, Loc),
3409 Attribute_Name => Name_Range_Length))));
3411 -- For all other scalar types, what we want logically is a
3412 -- range test:
3414 -- X in type(X)'First .. type(X)'Last
3416 -- But that's precisely what won't work because of possible
3417 -- unwanted optimization (and indeed the basic motivation for
3418 -- the Valid attribute -is exactly that this test does not work.
3419 -- What will work is:
3421 -- Btyp!(X) >= Btyp!(type(X)'First)
3422 -- and then
3423 -- Btyp!(X) <= Btyp!(type(X)'Last)
3425 -- where Btyp is an integer type large enough to cover the full
3426 -- range of possible stored values (i.e. it is chosen on the basis
3427 -- of the size of the type, not the range of the values). We write
3428 -- this as two tests, rather than a range check, so that static
3429 -- evaluation will easily remove either or both of the checks if
3430 -- they can be -statically determined to be true (this happens
3431 -- when the type of X is static and the range extends to the full
3432 -- range of stored values).
3434 -- Unsigned types. Note: it is safe to consider only whether the
3435 -- subtype is unsigned, since we will in that case be doing all
3436 -- unsigned comparisons based on the subtype range. Since we use
3437 -- the actual subtype object size, this is appropriate.
3439 -- For example, if we have
3441 -- subtype x is integer range 1 .. 200;
3442 -- for x'Object_Size use 8;
3444 -- Now the base type is signed, but objects of this type are 8
3445 -- bits unsigned, and doing an unsigned test of the range 1 to
3446 -- 200 is correct, even though a value greater than 127 looks
3447 -- signed to a signed comparison.
3449 elsif Is_Unsigned_Type (Ptyp) then
3450 if Esize (Ptyp) <= 32 then
3451 Btyp := RTE (RE_Unsigned_32);
3452 else
3453 Btyp := RTE (RE_Unsigned_64);
3454 end if;
3456 Rewrite (N, Make_Range_Test);
3458 -- Signed types
3460 else
3461 if Esize (Ptyp) <= Esize (Standard_Integer) then
3462 Btyp := Standard_Integer;
3463 else
3464 Btyp := Universal_Integer;
3465 end if;
3467 Rewrite (N, Make_Range_Test);
3468 end if;
3470 Analyze_And_Resolve (N, Standard_Boolean);
3471 end Valid;
3473 -----------
3474 -- Value --
3475 -----------
3477 -- Value attribute is handled in separate unti Exp_Imgv
3479 when Attribute_Value =>
3480 Exp_Imgv.Expand_Value_Attribute (N);
3482 -----------------
3483 -- Value_Size --
3484 -----------------
3486 -- The processing for Value_Size shares the processing for Size
3488 -------------
3489 -- Version --
3490 -------------
3492 -- The processing for Version shares the processing for Body_Version
3494 ----------------
3495 -- Wide_Image --
3496 ----------------
3498 -- We expand typ'Wide_Image (X) into
3500 -- String_To_Wide_String
3501 -- (typ'Image (X), Wide_Character_Encoding_Method)
3503 -- This works in all cases because String_To_Wide_String converts any
3504 -- wide character escape sequences resulting from the Image call to the
3505 -- proper Wide_Character equivalent
3507 -- not quite right for typ = Wide_Character ???
3509 when Attribute_Wide_Image => Wide_Image :
3510 begin
3511 Rewrite (N,
3512 Make_Function_Call (Loc,
3513 Name => New_Reference_To (RTE (RE_String_To_Wide_String), Loc),
3514 Parameter_Associations => New_List (
3515 Make_Attribute_Reference (Loc,
3516 Prefix => Pref,
3517 Attribute_Name => Name_Image,
3518 Expressions => Exprs),
3520 Make_Integer_Literal (Loc,
3521 Intval => Int (Wide_Character_Encoding_Method)))));
3523 Analyze_And_Resolve (N, Standard_Wide_String);
3524 end Wide_Image;
3526 ----------------
3527 -- Wide_Value --
3528 ----------------
3530 -- We expand typ'Wide_Value (X) into
3532 -- typ'Value
3533 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
3535 -- Wide_String_To_String is a runtime function that converts its wide
3536 -- string argument to String, converting any non-translatable characters
3537 -- into appropriate escape sequences. This preserves the required
3538 -- semantics of Wide_Value in all cases, and results in a very simple
3539 -- implementation approach.
3541 -- It's not quite right where typ = Wide_Character, because the encoding
3542 -- method may not cover the whole character type ???
3544 when Attribute_Wide_Value => Wide_Value :
3545 begin
3546 Rewrite (N,
3547 Make_Attribute_Reference (Loc,
3548 Prefix => Pref,
3549 Attribute_Name => Name_Value,
3551 Expressions => New_List (
3552 Make_Function_Call (Loc,
3553 Name =>
3554 New_Reference_To (RTE (RE_Wide_String_To_String), Loc),
3556 Parameter_Associations => New_List (
3557 Relocate_Node (First (Exprs)),
3558 Make_Integer_Literal (Loc,
3559 Intval => Int (Wide_Character_Encoding_Method)))))));
3561 Analyze_And_Resolve (N, Typ);
3562 end Wide_Value;
3564 ----------------
3565 -- Wide_Width --
3566 ----------------
3568 -- Wide_Width attribute is handled in separate unit Exp_Imgv
3570 when Attribute_Wide_Width =>
3571 Exp_Imgv.Expand_Width_Attribute (N, Wide => True);
3573 -----------
3574 -- Width --
3575 -----------
3577 -- Width attribute is handled in separate unit Exp_Imgv
3579 when Attribute_Width =>
3580 Exp_Imgv.Expand_Width_Attribute (N, Wide => False);
3582 -----------
3583 -- Write --
3584 -----------
3586 when Attribute_Write => Write : declare
3587 P_Type : constant Entity_Id := Entity (Pref);
3588 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3589 Pname : Entity_Id;
3590 Decl : Node_Id;
3591 Prag : Node_Id;
3592 Arg3 : Node_Id;
3593 Wfunc : Node_Id;
3595 begin
3596 -- If no underlying type, we have an error that will be diagnosed
3597 -- elsewhere, so here we just completely ignore the expansion.
3599 if No (U_Type) then
3600 return;
3601 end if;
3603 -- The simple case, if there is a TSS for Write, just call it
3605 Pname := Find_Inherited_TSS (P_Type, Name_uWrite);
3607 if Present (Pname) then
3608 null;
3610 else
3611 -- If there is a Stream_Convert pragma, use it, we rewrite
3613 -- sourcetyp'Output (stream, Item)
3615 -- as
3617 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
3619 -- where strmwrite is the given Write function that converts
3620 -- an argument of type sourcetyp or a type acctyp, from which
3621 -- it is derived to type strmtyp. The conversion to acttyp is
3622 -- required for the derived case.
3624 Prag :=
3625 Get_Rep_Pragma
3626 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
3628 if Present (Prag) then
3629 Arg3 :=
3630 Next (Next (First (Pragma_Argument_Associations (Prag))));
3631 Wfunc := Entity (Expression (Arg3));
3633 Rewrite (N,
3634 Make_Attribute_Reference (Loc,
3635 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
3636 Attribute_Name => Name_Output,
3637 Expressions => New_List (
3638 Relocate_Node (First (Exprs)),
3639 Make_Function_Call (Loc,
3640 Name => New_Occurrence_Of (Wfunc, Loc),
3641 Parameter_Associations => New_List (
3642 Convert_To (Etype (First_Formal (Wfunc)),
3643 Relocate_Node (Next (First (Exprs)))))))));
3645 Analyze (N);
3646 return;
3648 -- For elementary types, we call the W_xxx routine directly
3650 elsif Is_Elementary_Type (U_Type) then
3651 Rewrite (N, Build_Elementary_Write_Call (N));
3652 Analyze (N);
3653 return;
3655 -- Array type case
3657 elsif Is_Array_Type (U_Type) then
3658 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
3659 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3661 -- Tagged type case, use the primitive Write function. Note that
3662 -- this will dispatch in the class-wide case which is what we want
3664 elsif Is_Tagged_Type (U_Type) then
3665 Pname := Find_Prim_Op (U_Type, Name_uWrite);
3667 -- All other record type cases, including protected records.
3668 -- The latter only arise for expander generated code for
3669 -- handling shared passive partition access.
3671 else
3672 pragma Assert
3673 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3675 if Has_Discriminants (U_Type)
3676 and then Present
3677 (Discriminant_Default_Value (First_Discriminant (U_Type)))
3678 then
3679 Build_Mutable_Record_Write_Procedure
3680 (Loc, Base_Type (U_Type), Decl, Pname);
3682 else
3683 Build_Record_Write_Procedure
3684 (Loc, Base_Type (U_Type), Decl, Pname);
3685 end if;
3687 Insert_Action (N, Decl);
3688 end if;
3689 end if;
3691 -- If we fall through, Pname is the procedure to be called
3693 Rewrite_Stream_Proc_Call (Pname);
3694 end Write;
3696 -- Component_Size is handled by Gigi, unless the component size is
3697 -- known at compile time, which is always true in the packed array
3698 -- case. It is important that the packed array case is handled in
3699 -- the front end (see Eval_Attribute) since Gigi would otherwise
3700 -- get confused by the equivalent packed array type.
3702 when Attribute_Component_Size =>
3703 null;
3705 -- The following attributes are handled by Gigi (except that static
3706 -- cases have already been evaluated by the semantics, but in any
3707 -- case Gigi should not count on that).
3709 -- In addition Gigi handles the non-floating-point cases of Pred
3710 -- and Succ (including the fixed-point cases, which can just be
3711 -- treated as integer increment/decrement operations)
3713 -- Gigi also handles the non-class-wide cases of Size
3715 when Attribute_Bit_Order |
3716 Attribute_Code_Address |
3717 Attribute_Definite |
3718 Attribute_Max |
3719 Attribute_Mechanism_Code |
3720 Attribute_Min |
3721 Attribute_Null_Parameter |
3722 Attribute_Passed_By_Reference =>
3723 null;
3725 -- The following attributes are also handled by Gigi, but return a
3726 -- universal integer result, so may need a conversion for checking
3727 -- that the result is in range.
3729 when Attribute_Aft |
3730 Attribute_Alignment |
3731 Attribute_Bit |
3732 Attribute_Max_Size_In_Storage_Elements
3734 Apply_Universal_Integer_Attribute_Checks (N);
3736 -- The following attributes should not appear at this stage, since they
3737 -- have already been handled by the analyzer (and properly rewritten
3738 -- with corresponding values or entities to represent the right values)
3740 when Attribute_Abort_Signal |
3741 Attribute_Address_Size |
3742 Attribute_Base |
3743 Attribute_Class |
3744 Attribute_Default_Bit_Order |
3745 Attribute_Delta |
3746 Attribute_Denorm |
3747 Attribute_Digits |
3748 Attribute_Emax |
3749 Attribute_Epsilon |
3750 Attribute_Has_Discriminants |
3751 Attribute_Large |
3752 Attribute_Machine_Emax |
3753 Attribute_Machine_Emin |
3754 Attribute_Machine_Mantissa |
3755 Attribute_Machine_Overflows |
3756 Attribute_Machine_Radix |
3757 Attribute_Machine_Rounds |
3758 Attribute_Max_Interrupt_Priority |
3759 Attribute_Max_Priority |
3760 Attribute_Maximum_Alignment |
3761 Attribute_Model_Emin |
3762 Attribute_Model_Epsilon |
3763 Attribute_Model_Mantissa |
3764 Attribute_Model_Small |
3765 Attribute_Modulus |
3766 Attribute_Partition_ID |
3767 Attribute_Range |
3768 Attribute_Safe_Emax |
3769 Attribute_Safe_First |
3770 Attribute_Safe_Large |
3771 Attribute_Safe_Last |
3772 Attribute_Safe_Small |
3773 Attribute_Scale |
3774 Attribute_Signed_Zeros |
3775 Attribute_Small |
3776 Attribute_Storage_Unit |
3777 Attribute_Tick |
3778 Attribute_Type_Class |
3779 Attribute_Universal_Literal_String |
3780 Attribute_Wchar_T_Size |
3781 Attribute_Word_Size =>
3783 raise Program_Error;
3785 -- The Asm_Input and Asm_Output attributes are not expanded at this
3786 -- stage, but will be eliminated in the expansion of the Asm call,
3787 -- see Exp_Intr for details. So Gigi will never see these either.
3789 when Attribute_Asm_Input |
3790 Attribute_Asm_Output =>
3792 null;
3794 end case;
3796 end Expand_N_Attribute_Reference;
3798 ----------------------
3799 -- Expand_Pred_Succ --
3800 ----------------------
3802 -- For typ'Pred (exp), we generate the check
3804 -- [constraint_error when exp = typ'Base'First]
3806 -- Similarly, for typ'Succ (exp), we generate the check
3808 -- [constraint_error when exp = typ'Base'Last]
3810 -- These checks are not generated for modular types, since the proper
3811 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
3813 procedure Expand_Pred_Succ (N : Node_Id) is
3814 Loc : constant Source_Ptr := Sloc (N);
3815 Cnam : Name_Id;
3817 begin
3818 if Attribute_Name (N) = Name_Pred then
3819 Cnam := Name_First;
3820 else
3821 Cnam := Name_Last;
3822 end if;
3824 Insert_Action (N,
3825 Make_Raise_Constraint_Error (Loc,
3826 Condition =>
3827 Make_Op_Eq (Loc,
3828 Left_Opnd => Duplicate_Subexpr (First (Expressions (N))),
3829 Right_Opnd =>
3830 Make_Attribute_Reference (Loc,
3831 Prefix =>
3832 New_Reference_To (Base_Type (Etype (Prefix (N))), Loc),
3833 Attribute_Name => Cnam))));
3835 end Expand_Pred_Succ;
3837 ------------------------
3838 -- Find_Inherited_TSS --
3839 ------------------------
3841 function Find_Inherited_TSS
3842 (Typ : Entity_Id;
3843 Nam : Name_Id) return Entity_Id
3845 P_Type : Entity_Id := Typ;
3846 Proc : Entity_Id;
3848 begin
3849 Proc := TSS (Base_Type (Typ), Nam);
3851 -- Check first if there is a TSS given for the type itself.
3853 if Present (Proc) then
3854 return Proc;
3855 end if;
3857 -- If Typ is a derived type, it may inherit attributes from some
3858 -- ancestor which is not the ultimate underlying one.
3860 if Is_Derived_Type (P_Type) then
3862 while Is_Derived_Type (P_Type) loop
3863 Proc := TSS (Base_Type (Etype (Typ)), Nam);
3865 if Present (Proc) then
3866 return Proc;
3867 else
3868 P_Type := Base_Type (Etype (P_Type));
3869 end if;
3870 end loop;
3871 end if;
3873 -- If nothing else, use the TSS of the root type.
3875 return TSS (Base_Type (Underlying_Type (Typ)), Nam);
3876 end Find_Inherited_TSS;
3878 -----------------------
3879 -- Get_Index_Subtype --
3880 -----------------------
3882 function Get_Index_Subtype (N : Node_Id) return Node_Id is
3883 P_Type : Entity_Id := Etype (Prefix (N));
3884 Indx : Node_Id;
3885 J : Int;
3887 begin
3888 if Is_Access_Type (P_Type) then
3889 P_Type := Designated_Type (P_Type);
3890 end if;
3892 if No (Expressions (N)) then
3893 J := 1;
3894 else
3895 J := UI_To_Int (Expr_Value (First (Expressions (N))));
3896 end if;
3898 Indx := First_Index (P_Type);
3899 while J > 1 loop
3900 Next_Index (Indx);
3901 J := J - 1;
3902 end loop;
3904 return Etype (Indx);
3905 end Get_Index_Subtype;
3907 ---------------------------------
3908 -- Is_Constrained_Packed_Array --
3909 ---------------------------------
3911 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
3912 Arr : Entity_Id := Typ;
3914 begin
3915 if Is_Access_Type (Arr) then
3916 Arr := Designated_Type (Arr);
3917 end if;
3919 return Is_Array_Type (Arr)
3920 and then Is_Constrained (Arr)
3921 and then Present (Packed_Array_Type (Arr));
3922 end Is_Constrained_Packed_Array;
3924 end Exp_Attr;