1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Exp_Ch2; use Exp_Ch2;
31 with Exp_Ch9; use Exp_Ch9;
32 with Exp_Imgv; use Exp_Imgv;
33 with Exp_Pakd; use Exp_Pakd;
34 with Exp_Strm; use Exp_Strm;
35 with Exp_Tss; use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Gnatvsn; use Gnatvsn;
38 with Hostparm; use Hostparm;
40 with Namet; use Namet;
41 with Nmake; use Nmake;
42 with Nlists; use Nlists;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
47 with Sem_Ch7; use Sem_Ch7;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Res; use Sem_Res;
51 with Sem_Util; use Sem_Util;
52 with Sinfo; use Sinfo;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Stringt; use Stringt;
56 with Tbuild; use Tbuild;
57 with Ttypes; use Ttypes;
58 with Uintp; use Uintp;
59 with Uname; use Uname;
60 with Validsw; use Validsw;
62 package body Exp_Attr is
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 procedure Compile_Stream_Body_In_Scope
73 -- The body for a stream subprogram may be generated outside of the scope
74 -- of the type. If the type is fully private, it may depend on the full
75 -- view of other types (e.g. indices) that are currently private as well.
76 -- We install the declarations of the package in which the type is declared
77 -- before compiling the body in what is its proper environment. The Check
78 -- parameter indicates if checks are to be suppressed for the stream body.
79 -- We suppress checks for array/record reads, since the rule is that these
80 -- are like assignments, out of range values due to uninitialized storage,
81 -- or other invalid values do NOT cause a Constraint_Error to be raised.
83 procedure Expand_Fpt_Attribute
87 -- This procedure expands a call to a floating-point attribute function.
88 -- N is the attribute reference node, and Args is a list of arguments to
89 -- be passed to the function call. Rtp is the root type of the floating
90 -- point type involved (used to select the proper generic instantiation
91 -- of the package containing the attribute routines).
93 procedure Expand_Fpt_Attribute_R (N : Node_Id);
94 -- This procedure expands a call to a floating-point attribute function
95 -- that takes a single floating-point argument.
97 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
98 -- This procedure expands a call to a floating-point attribute function
99 -- that takes one floating-point argument and one integer argument.
101 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
102 -- This procedure expands a call to a floating-point attribute function
103 -- that takes two floating-point arguments.
105 procedure Expand_Pred_Succ (N : Node_Id);
106 -- Handles expansion of Pred or Succ attributes for case of non-real
107 -- operand with overflow checking required.
109 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
110 -- Used for Last, Last, and Length, when the prefix is an array type,
111 -- Obtains the corresponding index subtype.
113 procedure Expand_Access_To_Type (N : Node_Id);
114 -- A reference to a type within its own scope is resolved to a reference
115 -- to the current instance of the type in its initialization procedure.
117 function Find_Inherited_TSS
119 Nam : Name_Id) return Entity_Id;
121 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
122 -- Utility for array attributes, returns true on packed constrained
123 -- arrays, and on access to same.
125 ----------------------------------
126 -- Compile_Stream_Body_In_Scope --
127 ----------------------------------
129 procedure Compile_Stream_Body_In_Scope
135 Installed : Boolean := False;
136 Scop : constant Entity_Id := Scope (Arr);
137 Curr : constant Entity_Id := Current_Scope;
141 and then not In_Open_Scopes (Scop)
142 and then Ekind (Scop) = E_Package
145 Install_Visible_Declarations (Scop);
146 Install_Private_Declarations (Scop);
149 -- The entities in the package are now visible, but the generated
150 -- stream entity must appear in the current scope (usually an
151 -- enclosing stream function) so that itypes all have their proper
158 Insert_Action (N, Decl);
160 Insert_Action (N, Decl, All_Checks);
165 -- Remove extra copy of current scope, and package itself
168 End_Package_Scope (Scop);
170 end Compile_Stream_Body_In_Scope;
172 ---------------------------
173 -- Expand_Access_To_Type --
174 ---------------------------
176 procedure Expand_Access_To_Type (N : Node_Id) is
177 Loc : constant Source_Ptr := Sloc (N);
178 Typ : constant Entity_Id := Etype (N);
179 Pref : constant Node_Id := Prefix (N);
184 if Is_Entity_Name (Pref)
185 and then Is_Type (Entity (Pref))
187 -- If the current instance name denotes a task type,
188 -- then the access attribute is rewritten to be the
189 -- name of the "_task" parameter associated with the
190 -- task type's task body procedure. An unchecked
191 -- conversion is applied to ensure a type match in
192 -- cases of expander-generated calls (e.g., init procs).
194 if Is_Task_Type (Entity (Pref)) then
196 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
198 while Present (Formal) loop
199 exit when Chars (Formal) = Name_uTask;
200 Next_Entity (Formal);
203 pragma Assert (Present (Formal));
206 Unchecked_Convert_To (Typ, New_Occurrence_Of (Formal, Loc)));
209 -- The expression must appear in a default expression,
210 -- (which in the initialization procedure is the rhs of
211 -- an assignment), and not in a discriminant constraint.
216 while Present (Par) loop
217 exit when Nkind (Par) = N_Assignment_Statement;
219 if Nkind (Par) = N_Component_Declaration then
226 if Present (Par) then
228 Make_Attribute_Reference (Loc,
229 Prefix => Make_Identifier (Loc, Name_uInit),
230 Attribute_Name => Attribute_Name (N)));
232 Analyze_And_Resolve (N, Typ);
236 end Expand_Access_To_Type;
238 --------------------------
239 -- Expand_Fpt_Attribute --
240 --------------------------
242 procedure Expand_Fpt_Attribute
247 Loc : constant Source_Ptr := Sloc (N);
248 Typ : constant Entity_Id := Etype (N);
253 -- The function name is the selected component Fat_xxx.yyy where xxx
254 -- is the floating-point root type, and yyy is the attribute name
256 -- Note: it would be more usual to have separate RE entries for each
257 -- of the entities in the Fat packages, but first they have identical
258 -- names (so we would have to have lots of renaming declarations to
259 -- meet the normal RE rule of separate names for all runtime entities),
260 -- and second there would be an awful lot of them!
262 if Rtp = Standard_Short_Float then
263 Pkg := RE_Fat_Short_Float;
264 elsif Rtp = Standard_Float then
266 elsif Rtp = Standard_Long_Float then
267 Pkg := RE_Fat_Long_Float;
269 Pkg := RE_Fat_Long_Long_Float;
273 Make_Selected_Component (Loc,
274 Prefix => New_Reference_To (RTE (Pkg), Loc),
275 Selector_Name => Make_Identifier (Loc, Attribute_Name (N)));
277 -- The generated call is given the provided set of parameters, and then
278 -- wrapped in a conversion which converts the result to the target type
281 Unchecked_Convert_To (Etype (N),
282 Make_Function_Call (Loc,
284 Parameter_Associations => Args)));
286 Analyze_And_Resolve (N, Typ);
288 end Expand_Fpt_Attribute;
290 ----------------------------
291 -- Expand_Fpt_Attribute_R --
292 ----------------------------
294 -- The single argument is converted to its root type to call the
295 -- appropriate runtime function, with the actual call being built
296 -- by Expand_Fpt_Attribute
298 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
299 E1 : constant Node_Id := First (Expressions (N));
300 Rtp : constant Entity_Id := Root_Type (Etype (E1));
303 Expand_Fpt_Attribute (N, Rtp, New_List (
304 Unchecked_Convert_To (Rtp, Relocate_Node (E1))));
305 end Expand_Fpt_Attribute_R;
307 -----------------------------
308 -- Expand_Fpt_Attribute_RI --
309 -----------------------------
311 -- The first argument is converted to its root type and the second
312 -- argument is converted to standard long long integer to call the
313 -- appropriate runtime function, with the actual call being built
314 -- by Expand_Fpt_Attribute
316 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
317 E1 : constant Node_Id := First (Expressions (N));
318 Rtp : constant Entity_Id := Root_Type (Etype (E1));
319 E2 : constant Node_Id := Next (E1);
322 Expand_Fpt_Attribute (N, Rtp, New_List (
323 Unchecked_Convert_To (Rtp, Relocate_Node (E1)),
324 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
325 end Expand_Fpt_Attribute_RI;
327 -----------------------------
328 -- Expand_Fpt_Attribute_RR --
329 -----------------------------
331 -- The two arguments is converted to their root types to call the
332 -- appropriate runtime function, with the actual call being built
333 -- by Expand_Fpt_Attribute
335 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
336 E1 : constant Node_Id := First (Expressions (N));
337 Rtp : constant Entity_Id := Root_Type (Etype (E1));
338 E2 : constant Node_Id := Next (E1);
341 Expand_Fpt_Attribute (N, Rtp, New_List (
342 Unchecked_Convert_To (Rtp, Relocate_Node (E1)),
343 Unchecked_Convert_To (Rtp, Relocate_Node (E2))));
344 end Expand_Fpt_Attribute_RR;
346 ----------------------------------
347 -- Expand_N_Attribute_Reference --
348 ----------------------------------
350 procedure Expand_N_Attribute_Reference (N : Node_Id) is
351 Loc : constant Source_Ptr := Sloc (N);
352 Typ : constant Entity_Id := Etype (N);
353 Btyp : constant Entity_Id := Base_Type (Typ);
354 Pref : constant Node_Id := Prefix (N);
355 Exprs : constant List_Id := Expressions (N);
356 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
358 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
359 -- Rewrites a stream attribute for Read, Write or Output with the
360 -- procedure call. Pname is the entity for the procedure to call.
362 ------------------------------
363 -- Rewrite_Stream_Proc_Call --
364 ------------------------------
366 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
367 Item : constant Node_Id := Next (First (Exprs));
368 Formal_Typ : constant Entity_Id :=
369 Etype (Next_Formal (First_Formal (Pname)));
372 -- We have to worry about the type of the second argument
374 -- For the class-wide dispatching cases, and for cases in which
375 -- the base type of the second argument matches the base type of
376 -- the corresponding formal parameter, we are all set, and can use
377 -- the argument unchanged.
379 -- For all other cases we do an unchecked conversion of the second
380 -- parameter to the type of the formal of the procedure we are
381 -- calling. This deals with the private type cases, and with going
382 -- to the root type as required in elementary type case.
384 if not Is_Class_Wide_Type (Entity (Pref))
385 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
388 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
390 -- For untagged derived types set Assignment_OK, to prevent
391 -- copies from being created when the unchecked conversion
392 -- is expanded (which would happen in Remove_Side_Effects
393 -- if Expand_N_Unchecked_Conversion were allowed to call
394 -- Force_Evaluation). The copy could violate Ada semantics
395 -- in cases such as an actual that is an out parameter.
396 -- Note that this approach is also used in exp_ch7 for calls
397 -- to controlled type operations to prevent problems with
398 -- actuals wrapped in unchecked conversions.
400 if Is_Untagged_Derivation (Etype (Expression (Item))) then
401 Set_Assignment_OK (Item);
405 -- And now rewrite the call
408 Make_Procedure_Call_Statement (Loc,
409 Name => New_Occurrence_Of (Pname, Loc),
410 Parameter_Associations => Exprs));
413 end Rewrite_Stream_Proc_Call;
415 -- Start of processing for Expand_N_Attribute_Reference
418 -- Do required validity checking
420 if Validity_Checks_On and Validity_Check_Operands then
425 Expr := First (Expressions (N));
426 while Present (Expr) loop
433 -- Remaining processing depends on specific attribute
441 when Attribute_Access =>
443 if Ekind (Btyp) = E_Access_Protected_Subprogram_Type then
445 -- The value of the attribute_reference is a record containing
446 -- two fields: an access to the protected object, and an access
447 -- to the subprogram itself. The prefix is a selected component.
452 E_T : constant Entity_Id := Equivalent_Type (Btyp);
453 Acc : constant Entity_Id :=
454 Etype (Next_Component (First_Component (E_T)));
459 -- Within the body of the protected type, the prefix
460 -- designates a local operation, and the object is the first
461 -- parameter of the corresponding protected body of the
462 -- current enclosing operation.
464 if Is_Entity_Name (Pref) then
465 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
468 (Protected_Body_Subprogram (Entity (Pref)), Loc);
469 Curr := Current_Scope;
471 while Scope (Curr) /= Scope (Entity (Pref)) loop
472 Curr := Scope (Curr);
476 Make_Attribute_Reference (Loc,
480 (Protected_Body_Subprogram (Curr)), Loc),
481 Attribute_Name => Name_Address);
483 -- Case where the prefix is not an entity name. Find the
484 -- version of the protected operation to be called from
485 -- outside the protected object.
491 (Entity (Selector_Name (Pref))), Loc);
494 Make_Attribute_Reference (Loc,
495 Prefix => Relocate_Node (Prefix (Pref)),
496 Attribute_Name => Name_Address);
504 Unchecked_Convert_To (Acc,
505 Make_Attribute_Reference (Loc,
507 Attribute_Name => Name_Address))));
511 Analyze_And_Resolve (N, E_T);
513 -- For subsequent analysis, the node must retain its type.
514 -- The backend will replace it with the equivalent type where
520 elsif Ekind (Btyp) = E_General_Access_Type then
522 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
523 Parm_Ent : Entity_Id;
524 Conversion : Node_Id;
527 -- If the prefix of an Access attribute is a dereference of an
528 -- access parameter (or a renaming of such a dereference) and
529 -- the context is a general access type (but not an anonymous
530 -- access type), then rewrite the attribute as a conversion of
531 -- the access parameter to the context access type. This will
532 -- result in an accessibility check being performed, if needed.
534 -- (X.all'Access => Acc_Type (X))
536 if Nkind (Ref_Object) = N_Explicit_Dereference
537 and then Is_Entity_Name (Prefix (Ref_Object))
539 Parm_Ent := Entity (Prefix (Ref_Object));
541 if Ekind (Parm_Ent) in Formal_Kind
542 and then Ekind (Etype (Parm_Ent)) = E_Anonymous_Access_Type
543 and then Present (Extra_Accessibility (Parm_Ent))
546 Convert_To (Typ, New_Copy_Tree (Prefix (Ref_Object)));
548 Rewrite (N, Conversion);
549 Analyze_And_Resolve (N, Typ);
554 -- If the prefix is a type name, this is a reference to the current
555 -- instance of the type, within its initialization procedure.
558 Expand_Access_To_Type (N);
565 -- Transforms 'Adjacent into a call to the floating-point attribute
566 -- function Adjacent in Fat_xxx (where xxx is the root type)
568 when Attribute_Adjacent =>
569 Expand_Fpt_Attribute_RR (N);
575 when Attribute_Address => Address : declare
576 Task_Proc : Entity_Id;
579 -- If the prefix is a task or a task type, the useful address
580 -- is that of the procedure for the task body, i.e. the actual
581 -- program unit. We replace the original entity with that of
584 if Is_Entity_Name (Pref)
585 and then Is_Task_Type (Entity (Pref))
587 Task_Proc := Next_Entity (Root_Type (Etype (Pref)));
589 while Present (Task_Proc) loop
590 exit when Ekind (Task_Proc) = E_Procedure
591 and then Etype (First_Formal (Task_Proc)) =
592 Corresponding_Record_Type (Etype (Pref));
593 Next_Entity (Task_Proc);
596 if Present (Task_Proc) then
597 Set_Entity (Pref, Task_Proc);
598 Set_Etype (Pref, Etype (Task_Proc));
601 -- Similarly, the address of a protected operation is the address
602 -- of the corresponding protected body, regardless of the protected
603 -- object from which it is selected.
605 elsif Nkind (Pref) = N_Selected_Component
606 and then Is_Subprogram (Entity (Selector_Name (Pref)))
607 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
611 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
613 elsif Nkind (Pref) = N_Explicit_Dereference
614 and then Ekind (Etype (Pref)) = E_Subprogram_Type
615 and then Convention (Etype (Pref)) = Convention_Protected
617 -- The prefix is be a dereference of an access_to_protected_
618 -- subprogram. The desired address is the second component of
619 -- the record that represents the access.
622 Addr : constant Entity_Id := Etype (N);
623 Ptr : constant Node_Id := Prefix (Pref);
624 T : constant Entity_Id :=
625 Equivalent_Type (Base_Type (Etype (Ptr)));
629 Unchecked_Convert_To (Addr,
630 Make_Selected_Component (Loc,
631 Prefix => Unchecked_Convert_To (T, Ptr),
632 Selector_Name => New_Occurrence_Of (
633 Next_Entity (First_Entity (T)), Loc))));
635 Analyze_And_Resolve (N, Addr);
639 -- Deal with packed array reference, other cases are handled by gigi
641 if Involves_Packed_Array_Reference (Pref) then
642 Expand_Packed_Address_Reference (N);
650 when Attribute_AST_Entry => AST_Entry : declare
656 -- The reference to the entry or entry family
659 -- The index expression for an entry family reference, or
660 -- the Empty if Entry_Ref references a simple entry.
663 if Nkind (Pref) = N_Indexed_Component then
664 Entry_Ref := Prefix (Pref);
665 Index := First (Expressions (Pref));
671 -- Get expression for Task_Id and the entry entity
673 if Nkind (Entry_Ref) = N_Selected_Component then
675 Make_Attribute_Reference (Loc,
676 Attribute_Name => Name_Identity,
677 Prefix => Prefix (Entry_Ref));
679 Ttyp := Etype (Prefix (Entry_Ref));
680 Eent := Entity (Selector_Name (Entry_Ref));
684 Make_Function_Call (Loc,
685 Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc));
687 Eent := Entity (Entry_Ref);
689 -- We have to find the enclosing task to get the task type
690 -- There must be one, since we already validated this earlier
692 Ttyp := Current_Scope;
693 while not Is_Task_Type (Ttyp) loop
694 Ttyp := Scope (Ttyp);
698 -- Now rewrite the attribute with a call to Create_AST_Handler
701 Make_Function_Call (Loc,
702 Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc),
703 Parameter_Associations => New_List (
705 Entry_Index_Expression (Loc, Eent, Index, Ttyp))));
707 Analyze_And_Resolve (N, RTE (RE_AST_Handler));
714 -- We compute this if a component clause was present, otherwise
715 -- we leave the computation up to Gigi, since we don't know what
716 -- layout will be chosen.
718 -- Note that the attribute can apply to a naked record component
719 -- in generated code (i.e. the prefix is an identifier that
720 -- references the component or discriminant entity).
722 when Attribute_Bit_Position => Bit_Position :
727 if Nkind (Pref) = N_Identifier then
730 CE := Entity (Selector_Name (Pref));
733 if Known_Static_Component_Bit_Offset (CE) then
735 Make_Integer_Literal (Loc,
736 Intval => Component_Bit_Offset (CE)));
737 Analyze_And_Resolve (N, Typ);
740 Apply_Universal_Integer_Attribute_Checks (N);
748 -- A reference to P'Body_Version or P'Version is expanded to
751 -- pragma Import (C, Vnn, "uuuuT";
753 -- Get_Version_String (Vnn)
755 -- where uuuu is the unit name (dots replaced by double underscore)
756 -- and T is B for the cases of Body_Version, or Version applied to a
757 -- subprogram acting as its own spec, and S for Version applied to a
758 -- subprogram spec or package. This sequence of code references the
759 -- the unsigned constant created in the main program by the binder.
761 -- A special exception occurs for Standard, where the string
762 -- returned is a copy of the library string in gnatvsn.ads.
764 when Attribute_Body_Version | Attribute_Version => Version : declare
765 E : constant Entity_Id :=
766 Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
767 Pent : Entity_Id := Entity (Pref);
771 -- If not library unit, get to containing library unit
773 while Pent /= Standard_Standard
774 and then Scope (Pent) /= Standard_Standard
776 Pent := Scope (Pent);
779 -- Special case Standard
781 if Pent = Standard_Standard
782 or else Pent = Standard_ASCII
784 Name_Buffer (1 .. Library_Version'Length) := Library_Version;
785 Name_Len := Library_Version'Length;
787 Make_String_Literal (Loc,
788 Strval => String_From_Name_Buffer));
793 -- Build required string constant
795 Get_Name_String (Get_Unit_Name (Pent));
798 for J in 1 .. Name_Len - 2 loop
799 if Name_Buffer (J) = '.' then
800 Store_String_Chars ("__");
802 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
806 -- Case of subprogram acting as its own spec, always use body
808 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
809 and then Nkind (Parent (Declaration_Node (Pent))) =
811 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
813 Store_String_Chars ("B");
815 -- Case of no body present, always use spec
817 elsif not Unit_Requires_Body (Pent) then
818 Store_String_Chars ("S");
820 -- Otherwise use B for Body_Version, S for spec
822 elsif Id = Attribute_Body_Version then
823 Store_String_Chars ("B");
825 Store_String_Chars ("S");
829 Lib.Version_Referenced (S);
831 -- Insert the object declaration
833 Insert_Actions (N, New_List (
834 Make_Object_Declaration (Loc,
835 Defining_Identifier => E,
837 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
839 -- Set entity as imported with correct external name
842 Set_Interface_Name (E, Make_String_Literal (Loc, S));
844 -- And now rewrite original reference
847 Make_Function_Call (Loc,
848 Name => New_Reference_To (RTE (RE_Get_Version_String), Loc),
849 Parameter_Associations => New_List (
850 New_Occurrence_Of (E, Loc))));
853 Analyze_And_Resolve (N, RTE (RE_Version_String));
860 -- Transforms 'Ceiling into a call to the floating-point attribute
861 -- function Ceiling in Fat_xxx (where xxx is the root type)
863 when Attribute_Ceiling =>
864 Expand_Fpt_Attribute_R (N);
870 -- Transforms 'Callable attribute into a call to the Callable function.
872 when Attribute_Callable => Callable :
875 Build_Call_With_Task (Pref, RTE (RE_Callable)));
876 Analyze_And_Resolve (N, Standard_Boolean);
883 -- Transforms 'Caller attribute into a call to either the
884 -- Task_Entry_Caller or the Protected_Entry_Caller function.
886 when Attribute_Caller => Caller : declare
887 Id_Kind : Entity_Id := RTE (RO_AT_Task_ID);
888 Ent : Entity_Id := Entity (Pref);
889 Conctype : Entity_Id := Scope (Ent);
890 Nest_Depth : Integer := 0;
897 if Is_Protected_Type (Conctype) then
899 or else Restrictions (No_Entry_Queue) = False
900 or else Number_Entries (Conctype) > 1
904 (RTE (RE_Protected_Entry_Caller), Loc);
908 (RTE (RE_Protected_Single_Entry_Caller), Loc);
912 Unchecked_Convert_To (Id_Kind,
913 Make_Function_Call (Loc,
915 Parameter_Associations => New_List
918 (Corresponding_Body (Parent (Conctype))), Loc)))));
923 -- Determine the nesting depth of the E'Caller attribute, that
924 -- is, how many accept statements are nested within the accept
925 -- statement for E at the point of E'Caller. The runtime uses
926 -- this depth to find the specified entry call.
928 for J in reverse 0 .. Scope_Stack.Last loop
929 S := Scope_Stack.Table (J).Entity;
931 -- We should not reach the scope of the entry, as it should
932 -- already have been checked in Sem_Attr that this attribute
933 -- reference is within a matching accept statement.
935 pragma Assert (S /= Conctype);
940 elsif Is_Entry (S) then
941 Nest_Depth := Nest_Depth + 1;
946 Unchecked_Convert_To (Id_Kind,
947 Make_Function_Call (Loc,
948 Name => New_Reference_To (
949 RTE (RE_Task_Entry_Caller), Loc),
950 Parameter_Associations => New_List (
951 Make_Integer_Literal (Loc,
952 Intval => Int (Nest_Depth))))));
955 Analyze_And_Resolve (N, Id_Kind);
962 -- Transforms 'Compose into a call to the floating-point attribute
963 -- function Compose in Fat_xxx (where xxx is the root type)
965 -- Note: we strictly should have special code here to deal with the
966 -- case of absurdly negative arguments (less than Integer'First)
967 -- which will return a (signed) zero value, but it hardly seems
968 -- worth the effort. Absurdly large positive arguments will raise
969 -- constraint error which is fine.
971 when Attribute_Compose =>
972 Expand_Fpt_Attribute_RI (N);
978 when Attribute_Constrained => Constrained : declare
979 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
982 -- Reference to a parameter where the value is passed as an extra
983 -- actual, corresponding to the extra formal referenced by the
984 -- Extra_Constrained field of the corresponding formal.
986 if Present (Formal_Ent)
987 and then Present (Extra_Constrained (Formal_Ent))
991 (Extra_Constrained (Formal_Ent), Sloc (N)));
993 -- For variables with a Extra_Constrained field, we use the
994 -- corresponding entity.
996 elsif Nkind (Pref) = N_Identifier
997 and then Ekind (Entity (Pref)) = E_Variable
998 and then Present (Extra_Constrained (Entity (Pref)))
1002 (Extra_Constrained (Entity (Pref)), Sloc (N)));
1004 -- For all other entity names, we can tell at compile time
1006 elsif Is_Entity_Name (Pref) then
1008 Ent : constant Entity_Id := Entity (Pref);
1012 -- (RM J.4) obsolescent cases
1014 if Is_Type (Ent) then
1018 if Is_Private_Type (Ent) then
1019 Res := not Has_Discriminants (Ent)
1020 or else Is_Constrained (Ent);
1022 -- It not a private type, must be a generic actual type
1023 -- that corresponded to a private type. We know that this
1024 -- correspondence holds, since otherwise the reference
1025 -- within the generic template would have been illegal.
1029 UT : Entity_Id := Underlying_Type (Ent);
1032 if Is_Composite_Type (UT) then
1033 Res := Is_Constrained (Ent);
1040 -- If the prefix is not a variable or is aliased, then
1041 -- definitely true; if it's a formal parameter without
1042 -- an associated extra formal, then treat it as constrained.
1044 elsif not Is_Variable (Pref)
1045 or else Present (Formal_Ent)
1046 or else Is_Aliased_View (Pref)
1050 -- Variable case, just look at type to see if it is
1051 -- constrained. Note that the one case where this is
1052 -- not accurate (the procedure formal case), has been
1056 Res := Is_Constrained (Etype (Ent));
1061 New_Reference_To (Standard_True, Loc));
1064 New_Reference_To (Standard_False, Loc));
1068 -- Prefix is not an entity name. These are also cases where
1069 -- we can always tell at compile time by looking at the form
1070 -- and type of the prefix.
1073 if not Is_Variable (Pref)
1074 or else Nkind (Pref) = N_Explicit_Dereference
1075 or else Is_Constrained (Etype (Pref))
1078 New_Reference_To (Standard_True, Loc));
1081 New_Reference_To (Standard_False, Loc));
1085 Analyze_And_Resolve (N, Standard_Boolean);
1092 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1093 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1095 when Attribute_Copy_Sign =>
1096 Expand_Fpt_Attribute_RR (N);
1102 -- Transforms 'Count attribute into a call to the Count function
1104 when Attribute_Count => Count :
1110 Conctyp : Entity_Id;
1113 -- If the prefix is a member of an entry family, retrieve both
1114 -- entry name and index. For a simple entry there is no index.
1116 if Nkind (Pref) = N_Indexed_Component then
1117 Entnam := Prefix (Pref);
1118 Index := First (Expressions (Pref));
1124 -- Find the concurrent type in which this attribute is referenced
1125 -- (there had better be one).
1127 Conctyp := Current_Scope;
1128 while not Is_Concurrent_Type (Conctyp) loop
1129 Conctyp := Scope (Conctyp);
1134 if Is_Protected_Type (Conctyp) then
1137 or else Restrictions (No_Entry_Queue) = False
1138 or else Number_Entries (Conctyp) > 1
1140 Name := New_Reference_To (RTE (RE_Protected_Count), Loc);
1143 Make_Function_Call (Loc,
1145 Parameter_Associations => New_List (
1148 Corresponding_Body (Parent (Conctyp))), Loc),
1149 Entry_Index_Expression (
1150 Loc, Entity (Entnam), Index, Scope (Entity (Entnam)))));
1152 Name := New_Reference_To (RTE (RE_Protected_Count_Entry), Loc);
1154 Call := Make_Function_Call (Loc,
1156 Parameter_Associations => New_List (
1159 Corresponding_Body (Parent (Conctyp))), Loc)));
1166 Make_Function_Call (Loc,
1167 Name => New_Reference_To (RTE (RE_Task_Count), Loc),
1168 Parameter_Associations => New_List (
1169 Entry_Index_Expression
1170 (Loc, Entity (Entnam), Index, Scope (Entity (Entnam)))));
1173 -- The call returns type Natural but the context is universal integer
1174 -- so any integer type is allowed. The attribute was already resolved
1175 -- so its Etype is the required result type. If the base type of the
1176 -- context type is other than Standard.Integer we put in a conversion
1177 -- to the required type. This can be a normal typed conversion since
1178 -- both input and output types of the conversion are integer types
1180 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
1181 Rewrite (N, Convert_To (Typ, Call));
1186 Analyze_And_Resolve (N, Typ);
1193 -- This processing is shared by Elab_Spec
1195 -- What we do is to insert the following declarations
1198 -- pragma Import (C, enn, "name___elabb/s");
1200 -- and then the Elab_Body/Spec attribute is replaced by a reference
1201 -- to this defining identifier.
1203 when Attribute_Elab_Body |
1204 Attribute_Elab_Spec =>
1207 Ent : constant Entity_Id :=
1208 Make_Defining_Identifier (Loc,
1209 New_Internal_Name ('E'));
1213 procedure Make_Elab_String (Nod : Node_Id);
1214 -- Given Nod, an identifier, or a selected component, put the
1215 -- image into the current string literal, with double underline
1216 -- between components.
1218 procedure Make_Elab_String (Nod : Node_Id) is
1220 if Nkind (Nod) = N_Selected_Component then
1221 Make_Elab_String (Prefix (Nod));
1223 Store_String_Char ('$');
1225 Store_String_Char ('_');
1226 Store_String_Char ('_');
1229 Get_Name_String (Chars (Selector_Name (Nod)));
1232 pragma Assert (Nkind (Nod) = N_Identifier);
1233 Get_Name_String (Chars (Nod));
1236 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1237 end Make_Elab_String;
1239 -- Start of processing for Elab_Body/Elab_Spec
1242 -- First we need to prepare the string literal for the name of
1243 -- the elaboration routine to be referenced.
1246 Make_Elab_String (Pref);
1249 Store_String_Chars ("._elab");
1250 Lang := Make_Identifier (Loc, Name_Ada);
1252 Store_String_Chars ("___elab");
1253 Lang := Make_Identifier (Loc, Name_C);
1256 if Id = Attribute_Elab_Body then
1257 Store_String_Char ('b');
1259 Store_String_Char ('s');
1264 Insert_Actions (N, New_List (
1265 Make_Subprogram_Declaration (Loc,
1267 Make_Procedure_Specification (Loc,
1268 Defining_Unit_Name => Ent)),
1271 Chars => Name_Import,
1272 Pragma_Argument_Associations => New_List (
1273 Make_Pragma_Argument_Association (Loc,
1274 Expression => Lang),
1276 Make_Pragma_Argument_Association (Loc,
1278 Make_Identifier (Loc, Chars (Ent))),
1280 Make_Pragma_Argument_Association (Loc,
1282 Make_String_Literal (Loc, Str))))));
1284 Set_Entity (N, Ent);
1285 Rewrite (N, New_Occurrence_Of (Ent, Loc));
1292 -- Elaborated is always True for preelaborated units, predefined
1293 -- units, pure units and units which have Elaborate_Body pragmas.
1294 -- These units have no elaboration entity.
1296 -- Note: The Elaborated attribute is never passed through to Gigi
1298 when Attribute_Elaborated => Elaborated : declare
1299 Ent : constant Entity_Id := Entity (Pref);
1302 if Present (Elaboration_Entity (Ent)) then
1304 New_Occurrence_Of (Elaboration_Entity (Ent), Loc));
1306 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
1314 when Attribute_Enum_Rep => Enum_Rep :
1316 -- X'Enum_Rep (Y) expands to
1320 -- This is simply a direct conversion from the enumeration type
1321 -- to the target integer type, which is treated by Gigi as a normal
1322 -- integer conversion, treating the enumeration type as an integer,
1323 -- which is exactly what we want! We set Conversion_OK to make sure
1324 -- that the analyzer does not complain about what otherwise might
1325 -- be an illegal conversion.
1327 if Is_Non_Empty_List (Exprs) then
1329 OK_Convert_To (Typ, Relocate_Node (First (Exprs))));
1331 -- X'Enum_Rep where X is an enumeration literal is replaced by
1332 -- the literal value.
1334 elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then
1336 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref))));
1338 -- X'Enum_Rep where X is an object does a direct unchecked conversion
1339 -- of the object value, as described for the type case above.
1343 OK_Convert_To (Typ, Relocate_Node (Pref)));
1347 Analyze_And_Resolve (N, Typ);
1355 -- Transforms 'Exponent into a call to the floating-point attribute
1356 -- function Exponent in Fat_xxx (where xxx is the root type)
1358 when Attribute_Exponent =>
1359 Expand_Fpt_Attribute_R (N);
1365 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
1367 when Attribute_External_Tag => External_Tag :
1370 Make_Function_Call (Loc,
1371 Name => New_Reference_To (RTE (RE_External_Tag), Loc),
1372 Parameter_Associations => New_List (
1373 Make_Attribute_Reference (Loc,
1374 Attribute_Name => Name_Tag,
1375 Prefix => Prefix (N)))));
1377 Analyze_And_Resolve (N, Standard_String);
1384 when Attribute_First => declare
1385 Ptyp : constant Entity_Id := Etype (Pref);
1388 -- If the prefix type is a constrained packed array type which
1389 -- already has a Packed_Array_Type representation defined, then
1390 -- replace this attribute with a direct reference to 'First of the
1391 -- appropriate index subtype (since otherwise Gigi will try to give
1392 -- us the value of 'First for this implementation type).
1394 if Is_Constrained_Packed_Array (Ptyp) then
1396 Make_Attribute_Reference (Loc,
1397 Attribute_Name => Name_First,
1398 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
1399 Analyze_And_Resolve (N, Typ);
1401 elsif Is_Access_Type (Ptyp) then
1402 Apply_Access_Check (N);
1410 -- We compute this if a component clause was present, otherwise
1411 -- we leave the computation up to Gigi, since we don't know what
1412 -- layout will be chosen.
1414 when Attribute_First_Bit => First_Bit :
1416 CE : constant Entity_Id := Entity (Selector_Name (Pref));
1419 if Known_Static_Component_Bit_Offset (CE) then
1421 Make_Integer_Literal (Loc,
1422 Component_Bit_Offset (CE) mod System_Storage_Unit));
1424 Analyze_And_Resolve (N, Typ);
1427 Apply_Universal_Integer_Attribute_Checks (N);
1437 -- fixtype'Fixed_Value (integer-value)
1441 -- fixtype(integer-value)
1443 -- we do all the required analysis of the conversion here, because
1444 -- we do not want this to go through the fixed-point conversion
1445 -- circuits. Note that gigi always treats fixed-point as equivalent
1446 -- to the corresponding integer type anyway.
1448 when Attribute_Fixed_Value => Fixed_Value :
1451 Make_Type_Conversion (Loc,
1452 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
1453 Expression => Relocate_Node (First (Exprs))));
1454 Set_Etype (N, Entity (Pref));
1456 Apply_Type_Conversion_Checks (N);
1463 -- Transforms 'Floor into a call to the floating-point attribute
1464 -- function Floor in Fat_xxx (where xxx is the root type)
1466 when Attribute_Floor =>
1467 Expand_Fpt_Attribute_R (N);
1473 -- For the fixed-point type Typ:
1479 -- Result_Type (System.Fore (Long_Long_Float (Type'First)),
1480 -- Long_Long_Float (Type'Last))
1482 -- Note that we know that the type is a non-static subtype, or Fore
1483 -- would have itself been computed dynamically in Eval_Attribute.
1485 when Attribute_Fore => Fore :
1487 Ptyp : constant Entity_Id := Etype (Pref);
1492 Make_Function_Call (Loc,
1493 Name => New_Reference_To (RTE (RE_Fore), Loc),
1495 Parameter_Associations => New_List (
1496 Convert_To (Standard_Long_Long_Float,
1497 Make_Attribute_Reference (Loc,
1498 Prefix => New_Reference_To (Ptyp, Loc),
1499 Attribute_Name => Name_First)),
1501 Convert_To (Standard_Long_Long_Float,
1502 Make_Attribute_Reference (Loc,
1503 Prefix => New_Reference_To (Ptyp, Loc),
1504 Attribute_Name => Name_Last))))));
1506 Analyze_And_Resolve (N, Typ);
1513 -- Transforms 'Fraction into a call to the floating-point attribute
1514 -- function Fraction in Fat_xxx (where xxx is the root type)
1516 when Attribute_Fraction =>
1517 Expand_Fpt_Attribute_R (N);
1523 -- For an exception returns a reference to the exception data:
1524 -- Exception_Id!(Prefix'Reference)
1526 -- For a task it returns a reference to the _task_id component of
1527 -- corresponding record:
1529 -- taskV!(Prefix)._Task_Id, converted to the type Task_ID defined
1531 -- in Ada.Task_Identification.
1533 when Attribute_Identity => Identity : declare
1534 Id_Kind : Entity_Id;
1537 if Etype (Pref) = Standard_Exception_Type then
1538 Id_Kind := RTE (RE_Exception_Id);
1540 if Present (Renamed_Object (Entity (Pref))) then
1541 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
1545 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
1547 Id_Kind := RTE (RO_AT_Task_ID);
1550 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
1553 Analyze_And_Resolve (N, Id_Kind);
1560 -- Image attribute is handled in separate unit Exp_Imgv
1562 when Attribute_Image =>
1563 Exp_Imgv.Expand_Image_Attribute (N);
1569 -- X'Img is expanded to typ'Image (X), where typ is the type of X
1571 when Attribute_Img => Img :
1574 Make_Attribute_Reference (Loc,
1575 Prefix => New_Reference_To (Etype (Pref), Loc),
1576 Attribute_Name => Name_Image,
1577 Expressions => New_List (Relocate_Node (Pref))));
1579 Analyze_And_Resolve (N, Standard_String);
1586 when Attribute_Input => Input : declare
1587 P_Type : constant Entity_Id := Entity (Pref);
1588 B_Type : constant Entity_Id := Base_Type (P_Type);
1589 U_Type : constant Entity_Id := Underlying_Type (P_Type);
1590 Strm : constant Node_Id := First (Exprs);
1598 Cntrl : Node_Id := Empty;
1599 -- Value for controlling argument in call. Always Empty except in
1600 -- the dispatching (class-wide type) case, where it is a reference
1601 -- to the dummy object initialized to the right internal tag.
1604 -- If no underlying type, we have an error that will be diagnosed
1605 -- elsewhere, so here we just completely ignore the expansion.
1611 -- If there is a TSS for Input, just call it
1613 Fname := Find_Inherited_TSS (P_Type, Name_uInput);
1615 if Present (Fname) then
1619 -- If there is a Stream_Convert pragma, use it, we rewrite
1621 -- sourcetyp'Input (stream)
1625 -- sourcetyp (streamread (strmtyp'Input (stream)));
1627 -- where stmrearead is the given Read function that converts
1628 -- an argument of type strmtyp to type sourcetyp or a type
1629 -- from which it is derived. The extra conversion is required
1630 -- for the derived case.
1634 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
1636 if Present (Prag) then
1637 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
1638 Rfunc := Entity (Expression (Arg2));
1642 Make_Function_Call (Loc,
1643 Name => New_Occurrence_Of (Rfunc, Loc),
1644 Parameter_Associations => New_List (
1645 Make_Attribute_Reference (Loc,
1648 (Etype (First_Formal (Rfunc)), Loc),
1649 Attribute_Name => Name_Input,
1650 Expressions => Exprs)))));
1652 Analyze_And_Resolve (N, B_Type);
1657 elsif Is_Elementary_Type (U_Type) then
1659 -- A special case arises if we have a defined _Read routine,
1660 -- since in this case we are required to call this routine.
1662 if Present (TSS (B_Type, Name_uRead)) then
1663 Build_Record_Or_Elementary_Input_Function
1664 (Loc, U_Type, Decl, Fname);
1665 Insert_Action (N, Decl);
1667 -- For normal cases, we call the I_xxx routine directly
1670 Rewrite (N, Build_Elementary_Input_Call (N));
1671 Analyze_And_Resolve (N, P_Type);
1677 elsif Is_Array_Type (U_Type) then
1678 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
1679 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
1681 -- Dispatching case with class-wide type
1683 elsif Is_Class_Wide_Type (P_Type) then
1686 Rtyp : constant Entity_Id := Root_Type (P_Type);
1691 -- Read the internal tag (RM 13.13.2(34)) and use it to
1692 -- initialize a dummy tag object:
1694 -- Dnn : Ada.Tags.Tag
1695 -- := Internal_Tag (String'Input (Strm));
1697 -- This dummy object is used only to provide a controlling
1698 -- argument for the eventual _Input call.
1701 Make_Defining_Identifier (Loc,
1702 Chars => New_Internal_Name ('D'));
1705 Make_Object_Declaration (Loc,
1706 Defining_Identifier => Dnn,
1707 Object_Definition =>
1708 New_Occurrence_Of (RTE (RE_Tag), Loc),
1710 Make_Function_Call (Loc,
1712 New_Occurrence_Of (RTE (RE_Internal_Tag), Loc),
1713 Parameter_Associations => New_List (
1714 Make_Attribute_Reference (Loc,
1716 New_Occurrence_Of (Standard_String, Loc),
1717 Attribute_Name => Name_Input,
1718 Expressions => New_List (
1720 (Duplicate_Subexpr (Strm)))))));
1722 Insert_Action (N, Decl);
1724 -- Now we need to get the entity for the call, and construct
1725 -- a function call node, where we preset a reference to Dnn
1726 -- as the controlling argument (doing an unchecked
1727 -- conversion to the tagged type to make it look like
1728 -- a real tagged object).
1730 Fname := Find_Prim_Op (Rtyp, Name_uInput);
1731 Cntrl := Unchecked_Convert_To (Rtyp,
1732 New_Occurrence_Of (Dnn, Loc));
1733 Set_Etype (Cntrl, Rtyp);
1734 Set_Parent (Cntrl, N);
1737 -- For tagged types, use the primitive Input function
1739 elsif Is_Tagged_Type (U_Type) then
1740 Fname := Find_Prim_Op (U_Type, Name_uInput);
1742 -- All other record type cases, including protected records.
1743 -- The latter only arise for expander generated code for
1744 -- handling shared passive partition access.
1748 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
1750 Build_Record_Or_Elementary_Input_Function
1751 (Loc, Base_Type (U_Type), Decl, Fname);
1752 Insert_Action (N, Decl);
1756 -- If we fall through, Fname is the function to be called. The
1757 -- result is obtained by calling the appropriate function, then
1758 -- converting the result. The conversion does a subtype check.
1761 Make_Function_Call (Loc,
1762 Name => New_Occurrence_Of (Fname, Loc),
1763 Parameter_Associations => New_List (
1764 Relocate_Node (Strm)));
1766 Set_Controlling_Argument (Call, Cntrl);
1767 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
1768 Analyze_And_Resolve (N, P_Type);
1777 -- inttype'Fixed_Value (fixed-value)
1781 -- inttype(integer-value))
1783 -- we do all the required analysis of the conversion here, because
1784 -- we do not want this to go through the fixed-point conversion
1785 -- circuits. Note that gigi always treats fixed-point as equivalent
1786 -- to the corresponding integer type anyway.
1788 when Attribute_Integer_Value => Integer_Value :
1791 Make_Type_Conversion (Loc,
1792 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
1793 Expression => Relocate_Node (First (Exprs))));
1794 Set_Etype (N, Entity (Pref));
1796 Apply_Type_Conversion_Checks (N);
1803 when Attribute_Last => declare
1804 Ptyp : constant Entity_Id := Etype (Pref);
1807 -- If the prefix type is a constrained packed array type which
1808 -- already has a Packed_Array_Type representation defined, then
1809 -- replace this attribute with a direct reference to 'Last of the
1810 -- appropriate index subtype (since otherwise Gigi will try to give
1811 -- us the value of 'Last for this implementation type).
1813 if Is_Constrained_Packed_Array (Ptyp) then
1815 Make_Attribute_Reference (Loc,
1816 Attribute_Name => Name_Last,
1817 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
1818 Analyze_And_Resolve (N, Typ);
1820 elsif Is_Access_Type (Ptyp) then
1821 Apply_Access_Check (N);
1829 -- We compute this if a component clause was present, otherwise
1830 -- we leave the computation up to Gigi, since we don't know what
1831 -- layout will be chosen.
1833 when Attribute_Last_Bit => Last_Bit :
1835 CE : constant Entity_Id := Entity (Selector_Name (Pref));
1838 if Known_Static_Component_Bit_Offset (CE)
1839 and then Known_Static_Esize (CE)
1842 Make_Integer_Literal (Loc,
1843 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
1846 Analyze_And_Resolve (N, Typ);
1849 Apply_Universal_Integer_Attribute_Checks (N);
1857 -- Transforms 'Leading_Part into a call to the floating-point attribute
1858 -- function Leading_Part in Fat_xxx (where xxx is the root type)
1860 -- Note: strictly, we should have special case code to deal with
1861 -- absurdly large positive arguments (greater than Integer'Last),
1862 -- which result in returning the first argument unchanged, but it
1863 -- hardly seems worth the effort. We raise constraint error for
1864 -- absurdly negative arguments which is fine.
1866 when Attribute_Leading_Part =>
1867 Expand_Fpt_Attribute_RI (N);
1873 when Attribute_Length => declare
1874 Ptyp : constant Entity_Id := Etype (Pref);
1879 -- Processing for packed array types
1881 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
1882 Ityp := Get_Index_Subtype (N);
1884 -- If the index type, Ityp, is an enumeration type with
1885 -- holes, then we calculate X'Length explicitly using
1888 -- (0, Ityp'Pos (X'Last (N)) -
1889 -- Ityp'Pos (X'First (N)) + 1);
1891 -- Since the bounds in the template are the representation
1892 -- values and gigi would get the wrong value.
1894 if Is_Enumeration_Type (Ityp)
1895 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
1900 Xnum := Expr_Value (First (Expressions (N)));
1904 Make_Attribute_Reference (Loc,
1905 Prefix => New_Occurrence_Of (Typ, Loc),
1906 Attribute_Name => Name_Max,
1907 Expressions => New_List
1908 (Make_Integer_Literal (Loc, 0),
1912 Make_Op_Subtract (Loc,
1914 Make_Attribute_Reference (Loc,
1915 Prefix => New_Occurrence_Of (Ityp, Loc),
1916 Attribute_Name => Name_Pos,
1918 Expressions => New_List (
1919 Make_Attribute_Reference (Loc,
1920 Prefix => Duplicate_Subexpr (Pref),
1921 Attribute_Name => Name_Last,
1922 Expressions => New_List (
1923 Make_Integer_Literal (Loc, Xnum))))),
1926 Make_Attribute_Reference (Loc,
1927 Prefix => New_Occurrence_Of (Ityp, Loc),
1928 Attribute_Name => Name_Pos,
1930 Expressions => New_List (
1931 Make_Attribute_Reference (Loc,
1932 Prefix => Duplicate_Subexpr (Pref),
1933 Attribute_Name => Name_First,
1934 Expressions => New_List (
1935 Make_Integer_Literal (Loc, Xnum)))))),
1937 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
1939 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1942 -- If the prefix type is a constrained packed array type which
1943 -- already has a Packed_Array_Type representation defined, then
1944 -- replace this attribute with a direct reference to 'Range_Length
1945 -- of the appropriate index subtype (since otherwise Gigi will try
1946 -- to give us the value of 'Length for this implementation type).
1948 elsif Is_Constrained (Ptyp) then
1950 Make_Attribute_Reference (Loc,
1951 Attribute_Name => Name_Range_Length,
1952 Prefix => New_Reference_To (Ityp, Loc)));
1953 Analyze_And_Resolve (N, Typ);
1956 -- If we have a packed array that is not bit packed, which was
1960 elsif Is_Access_Type (Ptyp) then
1961 Apply_Access_Check (N);
1963 -- If the designated type is a packed array type, then we
1964 -- convert the reference to:
1967 -- xtyp'Pos (Pref'Last (Expr)) -
1968 -- xtyp'Pos (Pref'First (Expr)));
1970 -- This is a bit complex, but it is the easiest thing to do
1971 -- that works in all cases including enum types with holes
1972 -- xtyp here is the appropriate index type.
1975 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
1979 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
1980 Xtyp := Get_Index_Subtype (N);
1983 Make_Attribute_Reference (Loc,
1984 Prefix => New_Occurrence_Of (Typ, Loc),
1985 Attribute_Name => Name_Max,
1986 Expressions => New_List (
1987 Make_Integer_Literal (Loc, 0),
1990 Make_Integer_Literal (Loc, 1),
1991 Make_Op_Subtract (Loc,
1993 Make_Attribute_Reference (Loc,
1994 Prefix => New_Occurrence_Of (Xtyp, Loc),
1995 Attribute_Name => Name_Pos,
1996 Expressions => New_List (
1997 Make_Attribute_Reference (Loc,
1998 Prefix => Duplicate_Subexpr (Pref),
1999 Attribute_Name => Name_Last,
2001 New_Copy_List (Exprs)))),
2004 Make_Attribute_Reference (Loc,
2005 Prefix => New_Occurrence_Of (Xtyp, Loc),
2006 Attribute_Name => Name_Pos,
2007 Expressions => New_List (
2008 Make_Attribute_Reference (Loc,
2009 Prefix => Duplicate_Subexpr (Pref),
2010 Attribute_Name => Name_First,
2012 New_Copy_List (Exprs)))))))));
2014 Analyze_And_Resolve (N, Typ);
2018 -- Otherwise leave it to gigi
2021 Apply_Universal_Integer_Attribute_Checks (N);
2029 -- Transforms 'Machine into a call to the floating-point attribute
2030 -- function Machine in Fat_xxx (where xxx is the root type)
2032 when Attribute_Machine =>
2033 Expand_Fpt_Attribute_R (N);
2039 -- Machine_Size is equivalent to Object_Size, so transform it into
2040 -- Object_Size and that way Gigi never sees Machine_Size.
2042 when Attribute_Machine_Size =>
2044 Make_Attribute_Reference (Loc,
2045 Prefix => Prefix (N),
2046 Attribute_Name => Name_Object_Size));
2048 Analyze_And_Resolve (N, Typ);
2054 -- The only case that can get this far is the dynamic case of the
2055 -- old Ada 83 Mantissa attribute for the fixed-point case. For this
2062 -- ityp (System.Mantissa.Mantissa_Value
2063 -- (Integer'Integer_Value (typ'First),
2064 -- Integer'Integer_Value (typ'Last)));
2066 when Attribute_Mantissa => Mantissa : declare
2067 Ptyp : constant Entity_Id := Etype (Pref);
2072 Make_Function_Call (Loc,
2073 Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
2075 Parameter_Associations => New_List (
2077 Make_Attribute_Reference (Loc,
2078 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2079 Attribute_Name => Name_Integer_Value,
2080 Expressions => New_List (
2082 Make_Attribute_Reference (Loc,
2083 Prefix => New_Occurrence_Of (Ptyp, Loc),
2084 Attribute_Name => Name_First))),
2086 Make_Attribute_Reference (Loc,
2087 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2088 Attribute_Name => Name_Integer_Value,
2089 Expressions => New_List (
2091 Make_Attribute_Reference (Loc,
2092 Prefix => New_Occurrence_Of (Ptyp, Loc),
2093 Attribute_Name => Name_Last)))))));
2095 Analyze_And_Resolve (N, Typ);
2102 -- Transforms 'Model into a call to the floating-point attribute
2103 -- function Model in Fat_xxx (where xxx is the root type)
2105 when Attribute_Model =>
2106 Expand_Fpt_Attribute_R (N);
2112 -- The processing for Object_Size shares the processing for Size
2118 when Attribute_Output => Output : declare
2119 P_Type : constant Entity_Id := Entity (Pref);
2120 B_Type : constant Entity_Id := Base_Type (P_Type);
2121 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2129 -- If no underlying type, we have an error that will be diagnosed
2130 -- elsewhere, so here we just completely ignore the expansion.
2136 -- If TSS for Output is present, just call it
2138 Pname := Find_Inherited_TSS (P_Type, Name_uOutput);
2140 if Present (Pname) then
2144 -- If there is a Stream_Convert pragma, use it, we rewrite
2146 -- sourcetyp'Output (stream, Item)
2150 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
2152 -- where strmwrite is the given Write function that converts
2153 -- an argument of type sourcetyp or a type acctyp, from which
2154 -- it is derived to type strmtyp. The conversion to acttyp is
2155 -- required for the derived case.
2159 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
2161 if Present (Prag) then
2163 Next (Next (First (Pragma_Argument_Associations (Prag))));
2164 Wfunc := Entity (Expression (Arg3));
2167 Make_Attribute_Reference (Loc,
2168 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
2169 Attribute_Name => Name_Output,
2170 Expressions => New_List (
2171 Relocate_Node (First (Exprs)),
2172 Make_Function_Call (Loc,
2173 Name => New_Occurrence_Of (Wfunc, Loc),
2174 Parameter_Associations => New_List (
2175 Convert_To (Etype (First_Formal (Wfunc)),
2176 Relocate_Node (Next (First (Exprs)))))))));
2181 -- For elementary types, we call the W_xxx routine directly.
2182 -- Note that the effect of Write and Output is identical for
2183 -- the case of an elementary type, since there are no
2184 -- discriminants or bounds.
2186 elsif Is_Elementary_Type (U_Type) then
2188 -- A special case arises if we have a defined _Write routine,
2189 -- since in this case we are required to call this routine.
2191 if Present (TSS (B_Type, Name_uWrite)) then
2192 Build_Record_Or_Elementary_Output_Procedure
2193 (Loc, U_Type, Decl, Pname);
2194 Insert_Action (N, Decl);
2196 -- For normal cases, we call the W_xxx routine directly
2199 Rewrite (N, Build_Elementary_Write_Call (N));
2206 elsif Is_Array_Type (U_Type) then
2207 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
2208 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2210 -- Class-wide case, first output external tag, then dispatch
2211 -- to the appropriate primitive Output function (RM 13.13.2(31)).
2213 elsif Is_Class_Wide_Type (P_Type) then
2215 Strm : constant Node_Id := First (Exprs);
2216 Item : constant Node_Id := Next (Strm);
2220 -- String'Output (Strm, External_Tag (Item'Tag))
2223 Make_Attribute_Reference (Loc,
2224 Prefix => New_Occurrence_Of (Standard_String, Loc),
2225 Attribute_Name => Name_Output,
2226 Expressions => New_List (
2227 Relocate_Node (Duplicate_Subexpr (Strm)),
2228 Make_Function_Call (Loc,
2230 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
2231 Parameter_Associations => New_List (
2232 Make_Attribute_Reference (Loc,
2235 (Duplicate_Subexpr (Item, Name_Req => True)),
2236 Attribute_Name => Name_Tag))))));
2239 Pname := Find_Prim_Op (U_Type, Name_uOutput);
2241 -- Tagged type case, use the primitive Output function
2243 elsif Is_Tagged_Type (U_Type) then
2244 Pname := Find_Prim_Op (U_Type, Name_uOutput);
2246 -- All other record type cases, including protected records.
2247 -- The latter only arise for expander generated code for
2248 -- handling shared passive partition access.
2252 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2254 Build_Record_Or_Elementary_Output_Procedure
2255 (Loc, Base_Type (U_Type), Decl, Pname);
2256 Insert_Action (N, Decl);
2260 -- If we fall through, Pname is the name of the procedure to call
2262 Rewrite_Stream_Proc_Call (Pname);
2269 -- For enumeration types with a standard representation, Pos is
2272 -- For enumeration types, with a non-standard representation we
2273 -- generate a call to the _Rep_To_Pos function created when the
2274 -- type was frozen. The call has the form
2276 -- _rep_to_pos (expr, True)
2278 -- The parameter True causes Program_Error to be raised if the
2279 -- expression has an invalid representation.
2281 -- For integer types, Pos is equivalent to a simple integer
2282 -- conversion and we rewrite it as such
2284 when Attribute_Pos => Pos :
2286 Etyp : Entity_Id := Base_Type (Entity (Pref));
2289 -- Deal with zero/non-zero boolean values
2291 if Is_Boolean_Type (Etyp) then
2292 Adjust_Condition (First (Exprs));
2293 Etyp := Standard_Boolean;
2294 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
2297 -- Case of enumeration type
2299 if Is_Enumeration_Type (Etyp) then
2301 -- Non-standard enumeration type (generate call)
2303 if Present (Enum_Pos_To_Rep (Etyp)) then
2304 Append_To (Exprs, New_Occurrence_Of (Standard_True, Loc));
2308 Make_Function_Call (Loc,
2310 New_Reference_To (TSS (Etyp, Name_uRep_To_Pos), Loc),
2311 Parameter_Associations => Exprs)));
2313 Analyze_And_Resolve (N, Typ);
2315 -- Standard enumeration type (do universal integer check)
2318 Apply_Universal_Integer_Attribute_Checks (N);
2321 -- Deal with integer types (replace by conversion)
2323 elsif Is_Integer_Type (Etyp) then
2324 Rewrite (N, Convert_To (Typ, First (Exprs)));
2325 Analyze_And_Resolve (N, Typ);
2334 -- We compute this if a component clause was present, otherwise
2335 -- we leave the computation up to Gigi, since we don't know what
2336 -- layout will be chosen.
2338 when Attribute_Position => Position :
2340 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2343 if Present (Component_Clause (CE)) then
2345 Make_Integer_Literal (Loc,
2346 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
2347 Analyze_And_Resolve (N, Typ);
2350 Apply_Universal_Integer_Attribute_Checks (N);
2358 -- 1. Deal with enumeration types with holes
2359 -- 2. For floating-point, generate call to attribute function
2360 -- 3. For other cases, deal with constraint checking
2362 when Attribute_Pred => Pred :
2364 Ptyp : constant Entity_Id := Base_Type (Etype (Pref));
2367 -- For enumeration types with non-standard representations, we
2368 -- expand typ'Pred (x) into
2370 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
2372 if Is_Enumeration_Type (Ptyp)
2373 and then Present (Enum_Pos_To_Rep (Ptyp))
2375 -- Add Boolean parameter True, to request program errror if
2376 -- we have a bad representation on our hands.
2378 Append_To (Exprs, New_Occurrence_Of (Standard_True, Loc));
2381 Make_Indexed_Component (Loc,
2382 Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc),
2383 Expressions => New_List (
2384 Make_Op_Subtract (Loc,
2386 Make_Function_Call (Loc,
2388 New_Reference_To (TSS (Ptyp, Name_uRep_To_Pos), Loc),
2389 Parameter_Associations => Exprs),
2390 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
2392 Analyze_And_Resolve (N, Typ);
2394 -- For floating-point, we transform 'Pred into a call to the Pred
2395 -- floating-point attribute function in Fat_xxx (xxx is root type)
2397 elsif Is_Floating_Point_Type (Ptyp) then
2398 Expand_Fpt_Attribute_R (N);
2399 Analyze_And_Resolve (N, Typ);
2401 -- For modular types, nothing to do (no overflow, since wraps)
2403 elsif Is_Modular_Integer_Type (Ptyp) then
2406 -- For other types, if range checking is enabled, we must generate
2407 -- a check if overflow checking is enabled.
2409 elsif not Overflow_Checks_Suppressed (Ptyp) then
2410 Expand_Pred_Succ (N);
2419 when Attribute_Range_Length => Range_Length : declare
2420 P_Type : constant Entity_Id := Etype (Pref);
2423 -- The only special processing required is for the case where
2424 -- Range_Length is applied to an enumeration type with holes.
2425 -- In this case we transform
2431 -- X'Pos (X'Last) - X'Pos (X'First) + 1
2433 -- So that the result reflects the proper Pos values instead
2434 -- of the underlying representations.
2436 if Is_Enumeration_Type (P_Type)
2437 and then Has_Non_Standard_Rep (P_Type)
2442 Make_Op_Subtract (Loc,
2444 Make_Attribute_Reference (Loc,
2445 Attribute_Name => Name_Pos,
2446 Prefix => New_Occurrence_Of (P_Type, Loc),
2447 Expressions => New_List (
2448 Make_Attribute_Reference (Loc,
2449 Attribute_Name => Name_Last,
2450 Prefix => New_Occurrence_Of (P_Type, Loc)))),
2453 Make_Attribute_Reference (Loc,
2454 Attribute_Name => Name_Pos,
2455 Prefix => New_Occurrence_Of (P_Type, Loc),
2456 Expressions => New_List (
2457 Make_Attribute_Reference (Loc,
2458 Attribute_Name => Name_First,
2459 Prefix => New_Occurrence_Of (P_Type, Loc))))),
2462 Make_Integer_Literal (Loc, 1)));
2464 Analyze_And_Resolve (N, Typ);
2466 -- For all other cases, attribute is handled by Gigi, but we need
2467 -- to deal with the case of the range check on a universal integer.
2470 Apply_Universal_Integer_Attribute_Checks (N);
2479 when Attribute_Read => Read : declare
2480 P_Type : constant Entity_Id := Entity (Pref);
2481 B_Type : constant Entity_Id := Base_Type (P_Type);
2482 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2492 -- If no underlying type, we have an error that will be diagnosed
2493 -- elsewhere, so here we just completely ignore the expansion.
2499 -- The simple case, if there is a TSS for Read, just call it
2501 Pname := Find_Inherited_TSS (P_Type, Name_uRead);
2503 if Present (Pname) then
2507 -- If there is a Stream_Convert pragma, use it, we rewrite
2509 -- sourcetyp'Read (stream, Item)
2513 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
2515 -- where strmread is the given Read function that converts
2516 -- an argument of type strmtyp to type sourcetyp or a type
2517 -- from which it is derived. The conversion to sourcetyp
2518 -- is required in the latter case.
2520 -- A special case arises if Item is a type conversion in which
2521 -- case, we have to expand to:
2523 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
2525 -- where Itemx is the expression of the type conversion (i.e.
2526 -- the actual object), and typex is the type of Itemx.
2530 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
2532 if Present (Prag) then
2533 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
2534 Rfunc := Entity (Expression (Arg2));
2535 Lhs := Relocate_Node (Next (First (Exprs)));
2538 Make_Function_Call (Loc,
2539 Name => New_Occurrence_Of (Rfunc, Loc),
2540 Parameter_Associations => New_List (
2541 Make_Attribute_Reference (Loc,
2544 (Etype (First_Formal (Rfunc)), Loc),
2545 Attribute_Name => Name_Input,
2546 Expressions => New_List (
2547 Relocate_Node (First (Exprs)))))));
2549 if Nkind (Lhs) = N_Type_Conversion then
2550 Lhs := Expression (Lhs);
2551 Rhs := Convert_To (Etype (Lhs), Rhs);
2555 Make_Assignment_Statement (Loc,
2557 Expression => Rhs));
2558 Set_Assignment_OK (Lhs);
2562 -- For elementary types, we call the I_xxx routine using the first
2563 -- parameter and then assign the result into the second parameter.
2564 -- We set Assignment_OK to deal with the conversion case.
2566 elsif Is_Elementary_Type (U_Type) then
2572 Lhs := Relocate_Node (Next (First (Exprs)));
2573 Rhs := Build_Elementary_Input_Call (N);
2575 if Nkind (Lhs) = N_Type_Conversion then
2576 Lhs := Expression (Lhs);
2577 Rhs := Convert_To (Etype (Lhs), Rhs);
2580 Set_Assignment_OK (Lhs);
2583 Make_Assignment_Statement (Loc,
2585 Expression => Rhs));
2593 elsif Is_Array_Type (U_Type) then
2594 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
2595 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2597 -- Tagged type case, use the primitive Read function. Note that
2598 -- this will dispatch in the class-wide case which is what we want
2600 elsif Is_Tagged_Type (U_Type) then
2601 Pname := Find_Prim_Op (U_Type, Name_uRead);
2603 -- All other record type cases, including protected records.
2604 -- The latter only arise for expander generated code for
2605 -- handling shared passive partition access.
2609 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2611 if Has_Discriminants (U_Type)
2613 (Discriminant_Default_Value (First_Discriminant (U_Type)))
2615 Build_Mutable_Record_Read_Procedure
2616 (Loc, Base_Type (U_Type), Decl, Pname);
2619 Build_Record_Read_Procedure
2620 (Loc, Base_Type (U_Type), Decl, Pname);
2623 -- Suppress checks, uninitialized or otherwise invalid
2624 -- data does not cause constraint errors to be raised for
2625 -- a complete record read.
2627 Insert_Action (N, Decl, All_Checks);
2631 Rewrite_Stream_Proc_Call (Pname);
2638 -- Transforms 'Remainder into a call to the floating-point attribute
2639 -- function Remainder in Fat_xxx (where xxx is the root type)
2641 when Attribute_Remainder =>
2642 Expand_Fpt_Attribute_RR (N);
2648 -- The handling of the Round attribute is quite delicate. The
2649 -- processing in Sem_Attr introduced a conversion to universal
2650 -- real, reflecting the semantics of Round, but we do not want
2651 -- anything to do with universal real at runtime, since this
2652 -- corresponds to using floating-point arithmetic.
2654 -- What we have now is that the Etype of the Round attribute
2655 -- correctly indicates the final result type. The operand of
2656 -- the Round is the conversion to universal real, described
2657 -- above, and the operand of this conversion is the actual
2658 -- operand of Round, which may be the special case of a fixed
2659 -- point multiplication or division (Etype = universal fixed)
2661 -- The exapander will expand first the operand of the conversion,
2662 -- then the conversion, and finally the round attribute itself,
2663 -- since we always work inside out. But we cannot simply process
2664 -- naively in this order. In the semantic world where universal
2665 -- fixed and real really exist and have infinite precision, there
2666 -- is no problem, but in the implementation world, where universal
2667 -- real is a floating-point type, we would get the wrong result.
2669 -- So the approach is as follows. First, when expanding a multiply
2670 -- or divide whose type is universal fixed, we do nothing at all,
2671 -- instead deferring the operation till later.
2673 -- The actual processing is done in Expand_N_Type_Conversion which
2674 -- handles the special case of Round by looking at its parent to
2675 -- see if it is a Round attribute, and if it is, handling the
2676 -- conversion (or its fixed multiply/divide child) in an appropriate
2679 -- This means that by the time we get to expanding the Round attribute
2680 -- itself, the Round is nothing more than a type conversion (and will
2681 -- often be a null type conversion), so we just replace it with the
2682 -- appropriate conversion operation.
2684 when Attribute_Round =>
2686 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
2687 Analyze_And_Resolve (N);
2693 -- Transforms 'Rounding into a call to the floating-point attribute
2694 -- function Rounding in Fat_xxx (where xxx is the root type)
2696 when Attribute_Rounding =>
2697 Expand_Fpt_Attribute_R (N);
2703 -- Transforms 'Scaling into a call to the floating-point attribute
2704 -- function Scaling in Fat_xxx (where xxx is the root type)
2706 when Attribute_Scaling =>
2707 Expand_Fpt_Attribute_RI (N);
2713 when Attribute_Size |
2714 Attribute_Object_Size |
2715 Attribute_Value_Size |
2716 Attribute_VADS_Size => Size :
2719 Ptyp : constant Entity_Id := Etype (Pref);
2724 -- Processing for VADS_Size case. Note that this processing removes
2725 -- all traces of VADS_Size from the tree, and completes all required
2726 -- processing for VADS_Size by translating the attribute reference
2727 -- to an appropriate Size or Object_Size reference.
2729 if Id = Attribute_VADS_Size
2730 or else (Use_VADS_Size and then Id = Attribute_Size)
2732 -- If the size is specified, then we simply use the specified
2733 -- size. This applies to both types and objects. The size of an
2734 -- object can be specified in the following ways:
2736 -- An explicit size object is given for an object
2737 -- A component size is specified for an indexed component
2738 -- A component clause is specified for a selected component
2739 -- The object is a component of a packed composite object
2741 -- If the size is specified, then VADS_Size of an object
2743 if (Is_Entity_Name (Pref)
2744 and then Present (Size_Clause (Entity (Pref))))
2746 (Nkind (Pref) = N_Component_Clause
2747 and then (Present (Component_Clause
2748 (Entity (Selector_Name (Pref))))
2749 or else Is_Packed (Etype (Prefix (Pref)))))
2751 (Nkind (Pref) = N_Indexed_Component
2752 and then (Component_Size (Etype (Prefix (Pref))) /= 0
2753 or else Is_Packed (Etype (Prefix (Pref)))))
2755 Set_Attribute_Name (N, Name_Size);
2757 -- Otherwise if we have an object rather than a type, then the
2758 -- VADS_Size attribute applies to the type of the object, rather
2759 -- than the object itself. This is one of the respects in which
2760 -- VADS_Size differs from Size.
2763 if (not Is_Entity_Name (Pref)
2764 or else not Is_Type (Entity (Pref)))
2765 and then (Is_Scalar_Type (Etype (Pref))
2766 or else Is_Constrained (Etype (Pref)))
2768 Rewrite (Pref, New_Occurrence_Of (Etype (Pref), Loc));
2771 -- For a scalar type for which no size was
2772 -- explicitly given, VADS_Size means Object_Size. This is the
2773 -- other respect in which VADS_Size differs from Size.
2775 if Is_Scalar_Type (Etype (Pref))
2776 and then No (Size_Clause (Etype (Pref)))
2778 Set_Attribute_Name (N, Name_Object_Size);
2780 -- In all other cases, Size and VADS_Size are the sane
2783 Set_Attribute_Name (N, Name_Size);
2788 -- For class-wide types, transform X'Size into a call to
2789 -- the primitive operation _Size
2791 if Is_Class_Wide_Type (Ptyp) then
2793 Make_Function_Call (Loc,
2794 Name => New_Reference_To
2795 (Find_Prim_Op (Ptyp, Name_uSize), Loc),
2796 Parameter_Associations => New_List (Pref));
2798 if Typ /= Standard_Long_Long_Integer then
2800 -- The context is a specific integer type with which the
2801 -- original attribute was compatible. The function has a
2802 -- specific type as well, so to preserve the compatibility
2803 -- we must convert explicitly.
2805 New_Node := Convert_To (Typ, New_Node);
2808 Rewrite (N, New_Node);
2809 Analyze_And_Resolve (N, Typ);
2812 -- For an array component, we can do Size in the front end
2813 -- if the component_size of the array is set.
2815 elsif Nkind (Pref) = N_Indexed_Component then
2816 Siz := Component_Size (Etype (Prefix (Pref)));
2818 -- For a record component, we can do Size in the front end
2819 -- if there is a component clause, or if the record is packed
2820 -- and the component's size is known at compile time.
2822 elsif Nkind (Pref) = N_Selected_Component then
2824 Rec : constant Entity_Id := Etype (Prefix (Pref));
2825 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
2828 if Present (Component_Clause (Comp)) then
2829 Siz := Esize (Comp);
2831 elsif Is_Packed (Rec) then
2832 Siz := RM_Size (Ptyp);
2835 Apply_Universal_Integer_Attribute_Checks (N);
2840 -- All other cases are handled by Gigi
2843 Apply_Universal_Integer_Attribute_Checks (N);
2845 -- If we have Size applied to a formal parameter, that is a
2846 -- packed array subtype, then apply size to the actual subtype.
2848 if Is_Entity_Name (Pref)
2849 and then Is_Formal (Entity (Pref))
2850 and then Is_Array_Type (Etype (Pref))
2851 and then Is_Packed (Etype (Pref))
2854 Make_Attribute_Reference (Loc,
2856 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
2857 Attribute_Name => Name_Size));
2858 Analyze_And_Resolve (N, Typ);
2864 -- Common processing for record and array component case
2868 Make_Integer_Literal (Loc, Siz));
2870 Analyze_And_Resolve (N, Typ);
2872 -- The result is not a static expression
2874 Set_Is_Static_Expression (N, False);
2882 when Attribute_Storage_Pool =>
2884 Make_Type_Conversion (Loc,
2885 Subtype_Mark => New_Reference_To (Etype (N), Loc),
2886 Expression => New_Reference_To (Entity (N), Loc)));
2887 Analyze_And_Resolve (N, Typ);
2893 when Attribute_Storage_Size => Storage_Size :
2895 Ptyp : constant Entity_Id := Etype (Pref);
2898 -- Access type case, always go to the root type
2900 -- The case of access types results in a value of zero for the case
2901 -- where no storage size attribute clause has been given. If a
2902 -- storage size has been given, then the attribute is converted
2903 -- to a reference to the variable used to hold this value.
2905 if Is_Access_Type (Ptyp) then
2906 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
2908 Make_Attribute_Reference (Loc,
2909 Prefix => New_Reference_To (Typ, Loc),
2910 Attribute_Name => Name_Max,
2911 Expressions => New_List (
2912 Make_Integer_Literal (Loc, 0),
2915 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
2917 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
2920 Make_Function_Call (Loc,
2921 Name => New_Reference_To (Find_Prim_Op (Etype (
2922 Associated_Storage_Pool (Root_Type (Ptyp))),
2923 Attribute_Name (N)), Loc),
2925 Parameter_Associations => New_List (New_Reference_To (
2926 Associated_Storage_Pool (Root_Type (Ptyp)), Loc)))));
2928 Rewrite (N, Make_Integer_Literal (Loc, 0));
2931 Analyze_And_Resolve (N, Typ);
2933 -- The case of a task type (an obsolescent feature) is handled the
2934 -- same way, seems as reasonable as anything, and it is what the
2935 -- ACVC tests (e.g. CD1009K) seem to expect.
2937 -- If there is no Storage_Size variable, then we return the default
2938 -- task stack size, otherwise, expand a Storage_Size attribute as
2941 -- Typ (Adjust_Storage_Size (taskZ))
2943 -- except for the case of a task object which has a Storage_Size
2946 -- Typ (Adjust_Storage_Size (taskV!(name)._Size))
2949 if not Present (Storage_Size_Variable (Ptyp)) then
2952 Make_Function_Call (Loc,
2954 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc))));
2957 if not (Is_Entity_Name (Pref) and then
2958 Is_Task_Type (Entity (Pref))) and then
2959 Chars (Last_Entity (Corresponding_Record_Type (Ptyp))) =
2964 Make_Function_Call (Loc,
2965 Name => New_Occurrence_Of (
2966 RTE (RE_Adjust_Storage_Size), Loc),
2967 Parameter_Associations =>
2969 Make_Selected_Component (Loc,
2971 Unchecked_Convert_To (
2972 Corresponding_Record_Type (Ptyp),
2973 New_Copy_Tree (Pref)),
2975 Make_Identifier (Loc, Name_uSize))))));
2977 -- Task not having Storage_Size pragma
2982 Make_Function_Call (Loc,
2983 Name => New_Occurrence_Of (
2984 RTE (RE_Adjust_Storage_Size), Loc),
2985 Parameter_Associations =>
2988 Storage_Size_Variable (Ptyp), Loc)))));
2991 Analyze_And_Resolve (N, Typ);
3000 -- 1. Deal with enumeration types with holes
3001 -- 2. For floating-point, generate call to attribute function
3002 -- 3. For other cases, deal with constraint checking
3004 when Attribute_Succ => Succ :
3006 Ptyp : constant Entity_Id := Base_Type (Etype (Pref));
3009 -- For enumeration types with non-standard representations, we
3010 -- expand typ'Succ (x) into
3012 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
3014 if Is_Enumeration_Type (Ptyp)
3015 and then Present (Enum_Pos_To_Rep (Ptyp))
3017 -- Add Boolean parameter True, to request program errror if
3018 -- we have a bad representation on our hands.
3020 Append_To (Exprs, New_Occurrence_Of (Standard_True, Loc));
3023 Make_Indexed_Component (Loc,
3024 Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc),
3025 Expressions => New_List (
3028 Make_Function_Call (Loc,
3030 New_Reference_To (TSS (Ptyp, Name_uRep_To_Pos), Loc),
3031 Parameter_Associations => Exprs),
3032 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3034 Analyze_And_Resolve (N, Typ);
3036 -- For floating-point, we transform 'Succ into a call to the Succ
3037 -- floating-point attribute function in Fat_xxx (xxx is root type)
3039 elsif Is_Floating_Point_Type (Ptyp) then
3040 Expand_Fpt_Attribute_R (N);
3041 Analyze_And_Resolve (N, Typ);
3043 -- For modular types, nothing to do (no overflow, since wraps)
3045 elsif Is_Modular_Integer_Type (Ptyp) then
3048 -- For other types, if range checking is enabled, we must generate
3049 -- a check if overflow checking is enabled.
3051 elsif not Overflow_Checks_Suppressed (Ptyp) then
3052 Expand_Pred_Succ (N);
3060 -- Transforms X'Tag into a direct reference to the tag of X
3062 when Attribute_Tag => Tag :
3065 Prefix_Is_Type : Boolean;
3068 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
3069 Ttyp := Entity (Pref);
3070 Prefix_Is_Type := True;
3072 Ttyp := Etype (Pref);
3073 Prefix_Is_Type := False;
3076 if Is_Class_Wide_Type (Ttyp) then
3077 Ttyp := Root_Type (Ttyp);
3080 Ttyp := Underlying_Type (Ttyp);
3082 if Prefix_Is_Type then
3084 -- For JGNAT we leave the type attribute unexpanded because
3085 -- there's not a dispatching table to reference.
3089 Unchecked_Convert_To (RTE (RE_Tag),
3090 New_Reference_To (Access_Disp_Table (Ttyp), Loc)));
3091 Analyze_And_Resolve (N, RTE (RE_Tag));
3096 Make_Selected_Component (Loc,
3097 Prefix => Relocate_Node (Pref),
3099 New_Reference_To (Tag_Component (Ttyp), Loc)));
3100 Analyze_And_Resolve (N, RTE (RE_Tag));
3108 -- Transforms 'Terminated attribute into a call to Terminated function.
3110 when Attribute_Terminated => Terminated :
3112 if Restricted_Profile then
3114 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
3118 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
3121 Analyze_And_Resolve (N, Standard_Boolean);
3128 -- Transforms System'To_Address (X) into unchecked conversion
3129 -- from (integral) type of X to type address.
3131 when Attribute_To_Address =>
3133 Unchecked_Convert_To (RTE (RE_Address),
3134 Relocate_Node (First (Exprs))));
3135 Analyze_And_Resolve (N, RTE (RE_Address));
3141 -- Transforms 'Truncation into a call to the floating-point attribute
3142 -- function Truncation in Fat_xxx (where xxx is the root type)
3144 when Attribute_Truncation =>
3145 Expand_Fpt_Attribute_R (N);
3147 -----------------------
3148 -- Unbiased_Rounding --
3149 -----------------------
3151 -- Transforms 'Unbiased_Rounding into a call to the floating-point
3152 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
3155 when Attribute_Unbiased_Rounding =>
3156 Expand_Fpt_Attribute_R (N);
3158 ----------------------
3159 -- Unchecked_Access --
3160 ----------------------
3162 when Attribute_Unchecked_Access =>
3163 Expand_Access_To_Type (N);
3169 when Attribute_UET_Address => UET_Address : declare
3170 Ent : constant Entity_Id :=
3171 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3175 Make_Object_Declaration (Loc,
3176 Defining_Identifier => Ent,
3177 Aliased_Present => True,
3178 Object_Definition =>
3179 New_Occurrence_Of (RTE (RE_Address), Loc)));
3181 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
3182 -- in normal external form.
3184 Get_External_Unit_Name_String (Get_Unit_Name (Pref));
3185 Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
3186 Name_Len := Name_Len + 7;
3187 Name_Buffer (1 .. 7) := "__gnat_";
3188 Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP";
3189 Name_Len := Name_Len + 5;
3191 Set_Is_Imported (Ent);
3192 Set_Interface_Name (Ent,
3193 Make_String_Literal (Loc,
3194 Strval => String_From_Name_Buffer));
3197 Make_Attribute_Reference (Loc,
3198 Prefix => New_Occurrence_Of (Ent, Loc),
3199 Attribute_Name => Name_Address));
3201 Analyze_And_Resolve (N, Typ);
3204 -------------------------
3205 -- Unrestricted_Access --
3206 -------------------------
3208 when Attribute_Unrestricted_Access =>
3209 Expand_Access_To_Type (N);
3215 -- The processing for VADS_Size is shared with Size
3221 -- For enumeration types with a standard representation, and for all
3222 -- other types, Val is handled by Gigi. For enumeration types with
3223 -- a non-standard representation we use the _Pos_To_Rep array that
3224 -- was created when the type was frozen.
3226 when Attribute_Val => Val :
3228 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
3231 if Is_Enumeration_Type (Etyp)
3232 and then Present (Enum_Pos_To_Rep (Etyp))
3235 Make_Indexed_Component (Loc,
3236 Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc),
3237 Expressions => New_List (
3238 Convert_To (Standard_Integer,
3239 Relocate_Node (First (Exprs))))));
3241 Analyze_And_Resolve (N, Typ);
3249 -- The code for valid is dependent on the particular types involved.
3250 -- See separate sections below for the generated code in each case.
3252 when Attribute_Valid => Valid :
3254 Ptyp : constant Entity_Id := Etype (Pref);
3255 Btyp : Entity_Id := Base_Type (Ptyp);
3258 function Make_Range_Test return Node_Id;
3259 -- Build the code for a range test of the form
3260 -- Btyp!(Pref) >= Btyp!(Ptyp'First)
3262 -- Btyp!(Pref) <= Btyp!(Ptyp'Last)
3264 function Make_Range_Test return Node_Id is
3271 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3274 Unchecked_Convert_To (Btyp,
3275 Make_Attribute_Reference (Loc,
3276 Prefix => New_Occurrence_Of (Ptyp, Loc),
3277 Attribute_Name => Name_First))),
3282 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3285 Unchecked_Convert_To (Btyp,
3286 Make_Attribute_Reference (Loc,
3287 Prefix => New_Occurrence_Of (Ptyp, Loc),
3288 Attribute_Name => Name_Last))));
3289 end Make_Range_Test;
3291 -- Start of processing for Attribute_Valid
3294 -- Floating-point case. This case is handled by the Valid attribute
3295 -- code in the floating-point attribute run-time library.
3297 if Is_Floating_Point_Type (Ptyp) then
3299 Rtp : constant Entity_Id := Root_Type (Etype (Pref));
3302 Expand_Fpt_Attribute (N, Rtp, New_List (
3303 Make_Attribute_Reference (Loc,
3304 Prefix => Unchecked_Convert_To (Rtp, Pref),
3305 Attribute_Name => Name_Unrestricted_Access)));
3307 -- One more task, we still need a range check. Required
3308 -- only if we have a constraint, since the Valid routine
3309 -- catches infinities properly (infinities are never valid).
3311 -- The way we do the range check is simply to create the
3312 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
3314 if not Subtypes_Statically_Match (Ptyp, Btyp) then
3317 Left_Opnd => Relocate_Node (N),
3320 Left_Opnd => Convert_To (Btyp, Pref),
3321 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
3325 -- Enumeration type with holes
3327 -- For enumeration types with holes, the Pos value constructed by
3328 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
3329 -- second argument of False returns minus one for an invalid value,
3330 -- and the non-negative pos value for a valid value, so the
3331 -- expansion of X'Valid is simply:
3333 -- type(X)'Pos (X) >= 0
3335 -- We can't quite generate it that way because of the requirement
3336 -- for the non-standard second argument of False, so we have to
3337 -- explicitly create:
3339 -- _rep_to_pos (X, False) >= 0
3341 -- If we have an enumeration subtype, we also check that the
3342 -- value is in range:
3344 -- _rep_to_pos (X, False) >= 0
3346 -- (X >= type(X)'First and then type(X)'Last <= X)
3348 elsif Is_Enumeration_Type (Ptyp)
3349 and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp)))
3354 Make_Function_Call (Loc,
3357 (TSS (Base_Type (Ptyp), Name_uRep_To_Pos), Loc),
3358 Parameter_Associations => New_List (
3360 New_Occurrence_Of (Standard_False, Loc))),
3361 Right_Opnd => Make_Integer_Literal (Loc, 0));
3365 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
3367 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
3369 -- The call to Make_Range_Test will create declarations
3370 -- that need a proper insertion point, but Pref is now
3371 -- attached to a node with no ancestor. Attach to tree
3372 -- even if it is to be rewritten below.
3374 Set_Parent (Tst, Parent (N));
3378 Left_Opnd => Make_Range_Test,
3384 -- Fortran convention booleans
3386 -- For the very special case of Fortran convention booleans, the
3387 -- value is always valid, since it is an integer with the semantics
3388 -- that non-zero is true, and any value is permissible.
3390 elsif Is_Boolean_Type (Ptyp)
3391 and then Convention (Ptyp) = Convention_Fortran
3393 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3395 -- For biased representations, we will be doing an unchecked
3396 -- conversion without unbiasing the result. That means that
3397 -- the range test has to take this into account, and the
3398 -- proper form of the test is:
3400 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
3402 elsif Has_Biased_Representation (Ptyp) then
3403 Btyp := RTE (RE_Unsigned_32);
3407 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3409 Unchecked_Convert_To (Btyp,
3410 Make_Attribute_Reference (Loc,
3411 Prefix => New_Occurrence_Of (Ptyp, Loc),
3412 Attribute_Name => Name_Range_Length))));
3414 -- For all other scalar types, what we want logically is a
3417 -- X in type(X)'First .. type(X)'Last
3419 -- But that's precisely what won't work because of possible
3420 -- unwanted optimization (and indeed the basic motivation for
3421 -- the Valid attribute -is exactly that this test does not work.
3422 -- What will work is:
3424 -- Btyp!(X) >= Btyp!(type(X)'First)
3426 -- Btyp!(X) <= Btyp!(type(X)'Last)
3428 -- where Btyp is an integer type large enough to cover the full
3429 -- range of possible stored values (i.e. it is chosen on the basis
3430 -- of the size of the type, not the range of the values). We write
3431 -- this as two tests, rather than a range check, so that static
3432 -- evaluation will easily remove either or both of the checks if
3433 -- they can be -statically determined to be true (this happens
3434 -- when the type of X is static and the range extends to the full
3435 -- range of stored values).
3437 -- Unsigned types. Note: it is safe to consider only whether the
3438 -- subtype is unsigned, since we will in that case be doing all
3439 -- unsigned comparisons based on the subtype range. Since we use
3440 -- the actual subtype object size, this is appropriate.
3442 -- For example, if we have
3444 -- subtype x is integer range 1 .. 200;
3445 -- for x'Object_Size use 8;
3447 -- Now the base type is signed, but objects of this type are 8
3448 -- bits unsigned, and doing an unsigned test of the range 1 to
3449 -- 200 is correct, even though a value greater than 127 looks
3450 -- signed to a signed comparison.
3452 elsif Is_Unsigned_Type (Ptyp) then
3453 if Esize (Ptyp) <= 32 then
3454 Btyp := RTE (RE_Unsigned_32);
3456 Btyp := RTE (RE_Unsigned_64);
3459 Rewrite (N, Make_Range_Test);
3464 if Esize (Ptyp) <= Esize (Standard_Integer) then
3465 Btyp := Standard_Integer;
3467 Btyp := Universal_Integer;
3470 Rewrite (N, Make_Range_Test);
3473 Analyze_And_Resolve (N, Standard_Boolean);
3480 -- Value attribute is handled in separate unti Exp_Imgv
3482 when Attribute_Value =>
3483 Exp_Imgv.Expand_Value_Attribute (N);
3489 -- The processing for Value_Size shares the processing for Size
3495 -- The processing for Version shares the processing for Body_Version
3501 -- We expand typ'Wide_Image (X) into
3503 -- String_To_Wide_String
3504 -- (typ'Image (X), Wide_Character_Encoding_Method)
3506 -- This works in all cases because String_To_Wide_String converts any
3507 -- wide character escape sequences resulting from the Image call to the
3508 -- proper Wide_Character equivalent
3510 -- not quite right for typ = Wide_Character ???
3512 when Attribute_Wide_Image => Wide_Image :
3515 Make_Function_Call (Loc,
3516 Name => New_Reference_To (RTE (RE_String_To_Wide_String), Loc),
3517 Parameter_Associations => New_List (
3518 Make_Attribute_Reference (Loc,
3520 Attribute_Name => Name_Image,
3521 Expressions => Exprs),
3523 Make_Integer_Literal (Loc,
3524 Intval => Int (Wide_Character_Encoding_Method)))));
3526 Analyze_And_Resolve (N, Standard_Wide_String);
3533 -- We expand typ'Wide_Value (X) into
3536 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
3538 -- Wide_String_To_String is a runtime function that converts its wide
3539 -- string argument to String, converting any non-translatable characters
3540 -- into appropriate escape sequences. This preserves the required
3541 -- semantics of Wide_Value in all cases, and results in a very simple
3542 -- implementation approach.
3544 -- It's not quite right where typ = Wide_Character, because the encoding
3545 -- method may not cover the whole character type ???
3547 when Attribute_Wide_Value => Wide_Value :
3550 Make_Attribute_Reference (Loc,
3552 Attribute_Name => Name_Value,
3554 Expressions => New_List (
3555 Make_Function_Call (Loc,
3557 New_Reference_To (RTE (RE_Wide_String_To_String), Loc),
3559 Parameter_Associations => New_List (
3560 Relocate_Node (First (Exprs)),
3561 Make_Integer_Literal (Loc,
3562 Intval => Int (Wide_Character_Encoding_Method)))))));
3564 Analyze_And_Resolve (N, Typ);
3571 -- Wide_Width attribute is handled in separate unit Exp_Imgv
3573 when Attribute_Wide_Width =>
3574 Exp_Imgv.Expand_Width_Attribute (N, Wide => True);
3580 -- Width attribute is handled in separate unit Exp_Imgv
3582 when Attribute_Width =>
3583 Exp_Imgv.Expand_Width_Attribute (N, Wide => False);
3589 when Attribute_Write => Write : declare
3590 P_Type : constant Entity_Id := Entity (Pref);
3591 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3599 -- If no underlying type, we have an error that will be diagnosed
3600 -- elsewhere, so here we just completely ignore the expansion.
3606 -- The simple case, if there is a TSS for Write, just call it
3608 Pname := Find_Inherited_TSS (P_Type, Name_uWrite);
3610 if Present (Pname) then
3614 -- If there is a Stream_Convert pragma, use it, we rewrite
3616 -- sourcetyp'Output (stream, Item)
3620 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
3622 -- where strmwrite is the given Write function that converts
3623 -- an argument of type sourcetyp or a type acctyp, from which
3624 -- it is derived to type strmtyp. The conversion to acttyp is
3625 -- required for the derived case.
3629 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
3631 if Present (Prag) then
3633 Next (Next (First (Pragma_Argument_Associations (Prag))));
3634 Wfunc := Entity (Expression (Arg3));
3637 Make_Attribute_Reference (Loc,
3638 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
3639 Attribute_Name => Name_Output,
3640 Expressions => New_List (
3641 Relocate_Node (First (Exprs)),
3642 Make_Function_Call (Loc,
3643 Name => New_Occurrence_Of (Wfunc, Loc),
3644 Parameter_Associations => New_List (
3645 Convert_To (Etype (First_Formal (Wfunc)),
3646 Relocate_Node (Next (First (Exprs)))))))));
3651 -- For elementary types, we call the W_xxx routine directly
3653 elsif Is_Elementary_Type (U_Type) then
3654 Rewrite (N, Build_Elementary_Write_Call (N));
3660 elsif Is_Array_Type (U_Type) then
3661 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
3662 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3664 -- Tagged type case, use the primitive Write function. Note that
3665 -- this will dispatch in the class-wide case which is what we want
3667 elsif Is_Tagged_Type (U_Type) then
3668 Pname := Find_Prim_Op (U_Type, Name_uWrite);
3670 -- All other record type cases, including protected records.
3671 -- The latter only arise for expander generated code for
3672 -- handling shared passive partition access.
3676 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3678 if Has_Discriminants (U_Type)
3680 (Discriminant_Default_Value (First_Discriminant (U_Type)))
3682 Build_Mutable_Record_Write_Procedure
3683 (Loc, Base_Type (U_Type), Decl, Pname);
3686 Build_Record_Write_Procedure
3687 (Loc, Base_Type (U_Type), Decl, Pname);
3690 Insert_Action (N, Decl);
3694 -- If we fall through, Pname is the procedure to be called
3696 Rewrite_Stream_Proc_Call (Pname);
3699 -- Component_Size is handled by Gigi, unless the component size is
3700 -- known at compile time, which is always true in the packed array
3701 -- case. It is important that the packed array case is handled in
3702 -- the front end (see Eval_Attribute) since Gigi would otherwise
3703 -- get confused by the equivalent packed array type.
3705 when Attribute_Component_Size =>
3708 -- The following attributes are handled by Gigi (except that static
3709 -- cases have already been evaluated by the semantics, but in any
3710 -- case Gigi should not count on that).
3712 -- In addition Gigi handles the non-floating-point cases of Pred
3713 -- and Succ (including the fixed-point cases, which can just be
3714 -- treated as integer increment/decrement operations)
3716 -- Gigi also handles the non-class-wide cases of Size
3718 when Attribute_Bit_Order |
3719 Attribute_Code_Address |
3720 Attribute_Definite |
3722 Attribute_Mechanism_Code |
3724 Attribute_Null_Parameter |
3725 Attribute_Passed_By_Reference =>
3728 -- The following attributes are also handled by Gigi, but return a
3729 -- universal integer result, so may need a conversion for checking
3730 -- that the result is in range.
3732 when Attribute_Aft |
3733 Attribute_Alignment |
3735 Attribute_Max_Size_In_Storage_Elements
3737 Apply_Universal_Integer_Attribute_Checks (N);
3739 -- The following attributes should not appear at this stage, since they
3740 -- have already been handled by the analyzer (and properly rewritten
3741 -- with corresponding values or entities to represent the right values)
3743 when Attribute_Abort_Signal |
3744 Attribute_Address_Size |
3747 Attribute_Default_Bit_Order |
3753 Attribute_Has_Discriminants |
3755 Attribute_Machine_Emax |
3756 Attribute_Machine_Emin |
3757 Attribute_Machine_Mantissa |
3758 Attribute_Machine_Overflows |
3759 Attribute_Machine_Radix |
3760 Attribute_Machine_Rounds |
3761 Attribute_Maximum_Alignment |
3762 Attribute_Model_Emin |
3763 Attribute_Model_Epsilon |
3764 Attribute_Model_Mantissa |
3765 Attribute_Model_Small |
3767 Attribute_Partition_ID |
3769 Attribute_Safe_Emax |
3770 Attribute_Safe_First |
3771 Attribute_Safe_Large |
3772 Attribute_Safe_Last |
3773 Attribute_Safe_Small |
3775 Attribute_Signed_Zeros |
3777 Attribute_Storage_Unit |
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 =>
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);
3818 if Attribute_Name (N) = Name_Pred then
3825 Make_Raise_Constraint_Error (Loc,
3828 Left_Opnd => Duplicate_Subexpr (First (Expressions (N))),
3830 Make_Attribute_Reference (Loc,
3832 New_Reference_To (Base_Type (Etype (Prefix (N))), Loc),
3833 Attribute_Name => Cnam)),
3834 Reason => CE_Overflow_Check_Failed));
3836 end Expand_Pred_Succ;
3838 ------------------------
3839 -- Find_Inherited_TSS --
3840 ------------------------
3842 function Find_Inherited_TSS
3844 Nam : Name_Id) return Entity_Id
3846 P_Type : Entity_Id := Typ;
3850 Proc := TSS (Base_Type (Typ), Nam);
3852 -- Check first if there is a TSS given for the type itself.
3854 if Present (Proc) then
3858 -- If Typ is a derived type, it may inherit attributes from some
3859 -- ancestor which is not the ultimate underlying one.
3860 -- If Typ is a derived tagged type, the corresponding primitive
3861 -- operation has been created explicitly.
3863 if Is_Derived_Type (P_Type) then
3864 if Is_Tagged_Type (P_Type) then
3865 return Find_Prim_Op (P_Type, Nam);
3867 while Is_Derived_Type (P_Type) loop
3868 Proc := TSS (Base_Type (Etype (Typ)), Nam);
3870 if Present (Proc) then
3873 P_Type := Base_Type (Etype (P_Type));
3879 -- If nothing else, use the TSS of the root type.
3881 return TSS (Base_Type (Underlying_Type (Typ)), Nam);
3882 end Find_Inherited_TSS;
3884 -----------------------
3885 -- Get_Index_Subtype --
3886 -----------------------
3888 function Get_Index_Subtype (N : Node_Id) return Node_Id is
3889 P_Type : Entity_Id := Etype (Prefix (N));
3894 if Is_Access_Type (P_Type) then
3895 P_Type := Designated_Type (P_Type);
3898 if No (Expressions (N)) then
3901 J := UI_To_Int (Expr_Value (First (Expressions (N))));
3904 Indx := First_Index (P_Type);
3910 return Etype (Indx);
3911 end Get_Index_Subtype;
3913 ---------------------------------
3914 -- Is_Constrained_Packed_Array --
3915 ---------------------------------
3917 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
3918 Arr : Entity_Id := Typ;
3921 if Is_Access_Type (Arr) then
3922 Arr := Designated_Type (Arr);
3925 return Is_Array_Type (Arr)
3926 and then Is_Constrained (Arr)
3927 and then Present (Packed_Array_Type (Arr));
3928 end Is_Constrained_Packed_Array;