1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Debug; use Debug;
30 with Debug_A; use Debug_A;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
47 with Nlists; use Nlists;
49 with Output; use Output;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aggr; use Sem_Aggr;
55 with Sem_Attr; use Sem_Attr;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch4; use Sem_Ch4;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elab; use Sem_Elab;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Intr; use Sem_Intr;
65 with Sem_Util; use Sem_Util;
66 with Sem_Type; use Sem_Type;
67 with Sem_Warn; use Sem_Warn;
68 with Sinfo; use Sinfo;
69 with Snames; use Snames;
70 with Stand; use Stand;
71 with Stringt; use Stringt;
72 with Targparm; use Targparm;
73 with Tbuild; use Tbuild;
74 with Uintp; use Uintp;
75 with Urealp; use Urealp;
77 package body Sem_Res is
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 -- Second pass (top-down) type checking and overload resolution procedures
84 -- Typ is the type required by context. These procedures propagate the
85 -- type information recursively to the descendants of N. If the node
86 -- is not overloaded, its Etype is established in the first pass. If
87 -- overloaded, the Resolve routines set the correct type. For arith.
88 -- operators, the Etype is the base type of the context.
90 -- Note that Resolve_Attribute is separated off in Sem_Attr
92 procedure Check_Discriminant_Use (N : Node_Id);
93 -- Enforce the restrictions on the use of discriminants when constraining
94 -- a component of a discriminated type (record or concurrent type).
96 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
97 -- Given a node for an operator associated with type T, check that
98 -- the operator is visible. Operators all of whose operands are
99 -- universal must be checked for visibility during resolution
100 -- because their type is not determinable based on their operands.
102 procedure Check_Fully_Declared_Prefix
105 -- Check that the type of the prefix of a dereference is not incomplete
107 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
108 -- Given a call node, N, which is known to occur immediately within the
109 -- subprogram being called, determines whether it is a detectable case of
110 -- an infinite recursion, and if so, outputs appropriate messages. Returns
111 -- True if an infinite recursion is detected, and False otherwise.
113 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
114 -- If the type of the object being initialized uses the secondary stack
115 -- directly or indirectly, create a transient scope for the call to the
116 -- init proc. This is because we do not create transient scopes for the
117 -- initialization of individual components within the init proc itself.
118 -- Could be optimized away perhaps?
120 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
121 -- Utility to check whether the name in the call is a predefined
122 -- operator, in which case the call is made into an operator node.
123 -- An instance of an intrinsic conversion operation may be given
124 -- an operator name, but is not treated like an operator.
126 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
127 -- If a default expression in entry call N depends on the discriminants
128 -- of the task, it must be replaced with a reference to the discriminant
129 -- of the task being called.
131 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
132 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
133 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
134 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
135 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
136 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
137 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
138 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
139 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
140 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
141 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
142 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
143 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
144 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
145 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
146 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
147 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
148 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
149 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
150 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
151 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
152 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
153 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
154 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
155 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
156 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
157 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
158 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
159 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
160 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
161 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
162 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
164 function Operator_Kind
166 Is_Binary : Boolean) return Node_Kind;
167 -- Utility to map the name of an operator into the corresponding Node. Used
168 -- by other node rewriting procedures.
170 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
171 -- Resolve actuals of call, and add default expressions for missing ones.
172 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
173 -- called subprogram.
175 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
176 -- Called from Resolve_Call, when the prefix denotes an entry or element
177 -- of entry family. Actuals are resolved as for subprograms, and the node
178 -- is rebuilt as an entry call. Also called for protected operations. Typ
179 -- is the context type, which is used when the operation is a protected
180 -- function with no arguments, and the return value is indexed.
182 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
183 -- A call to a user-defined intrinsic operator is rewritten as a call
184 -- to the corresponding predefined operator, with suitable conversions.
186 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
187 -- Ditto, for unary operators (only arithmetic ones)
189 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
190 -- If an operator node resolves to a call to a user-defined operator,
191 -- rewrite the node as a function call.
193 procedure Make_Call_Into_Operator
197 -- Inverse transformation: if an operator is given in functional notation,
198 -- then after resolving the node, transform into an operator node, so
199 -- that operands are resolved properly. Recall that predefined operators
200 -- do not have a full signature and special resolution rules apply.
202 procedure Rewrite_Renamed_Operator
206 -- An operator can rename another, e.g. in an instantiation. In that
207 -- case, the proper operator node must be constructed and resolved.
209 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
210 -- The String_Literal_Subtype is built for all strings that are not
211 -- operands of a static concatenation operation. If the argument is
212 -- not a N_String_Literal node, then the call has no effect.
214 procedure Set_Slice_Subtype (N : Node_Id);
215 -- Build subtype of array type, with the range specified by the slice
217 procedure Simplify_Type_Conversion (N : Node_Id);
218 -- Called after N has been resolved and evaluated, but before range checks
219 -- have been applied. Currently simplifies a combination of floating-point
220 -- to integer conversion and Truncation attribute.
222 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
223 -- A universal_fixed expression in an universal context is unambiguous
224 -- if there is only one applicable fixed point type. Determining whether
225 -- there is only one requires a search over all visible entities, and
226 -- happens only in very pathological cases (see 6115-006).
228 function Valid_Conversion
231 Operand : Node_Id) return Boolean;
232 -- Verify legality rules given in 4.6 (8-23). Target is the target
233 -- type of the conversion, which may be an implicit conversion of
234 -- an actual parameter to an anonymous access type (in which case
235 -- N denotes the actual parameter and N = Operand).
237 -------------------------
238 -- Ambiguous_Character --
239 -------------------------
241 procedure Ambiguous_Character (C : Node_Id) is
245 if Nkind (C) = N_Character_Literal then
246 Error_Msg_N ("ambiguous character literal", C);
248 -- First the ones in Standard
251 ("\\possible interpretation: Character!", C);
253 ("\\possible interpretation: Wide_Character!", C);
255 -- Include Wide_Wide_Character in Ada 2005 mode
257 if Ada_Version >= Ada_05 then
259 ("\\possible interpretation: Wide_Wide_Character!", C);
262 -- Now any other types that match
264 E := Current_Entity (C);
265 while Present (E) loop
266 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
270 end Ambiguous_Character;
272 -------------------------
273 -- Analyze_And_Resolve --
274 -------------------------
276 procedure Analyze_And_Resolve (N : Node_Id) is
280 end Analyze_And_Resolve;
282 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
286 end Analyze_And_Resolve;
288 -- Version withs check(s) suppressed
290 procedure Analyze_And_Resolve
295 Scop : constant Entity_Id := Current_Scope;
298 if Suppress = All_Checks then
300 Svg : constant Suppress_Array := Scope_Suppress;
302 Scope_Suppress := (others => True);
303 Analyze_And_Resolve (N, Typ);
304 Scope_Suppress := Svg;
309 Svg : constant Boolean := Scope_Suppress (Suppress);
312 Scope_Suppress (Suppress) := True;
313 Analyze_And_Resolve (N, Typ);
314 Scope_Suppress (Suppress) := Svg;
318 if Current_Scope /= Scop
319 and then Scope_Is_Transient
321 -- This can only happen if a transient scope was created
322 -- for an inner expression, which will be removed upon
323 -- completion of the analysis of an enclosing construct.
324 -- The transient scope must have the suppress status of
325 -- the enclosing environment, not of this Analyze call.
327 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
330 end Analyze_And_Resolve;
332 procedure Analyze_And_Resolve
336 Scop : constant Entity_Id := Current_Scope;
339 if Suppress = All_Checks then
341 Svg : constant Suppress_Array := Scope_Suppress;
343 Scope_Suppress := (others => True);
344 Analyze_And_Resolve (N);
345 Scope_Suppress := Svg;
350 Svg : constant Boolean := Scope_Suppress (Suppress);
353 Scope_Suppress (Suppress) := True;
354 Analyze_And_Resolve (N);
355 Scope_Suppress (Suppress) := Svg;
359 if Current_Scope /= Scop
360 and then Scope_Is_Transient
362 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
365 end Analyze_And_Resolve;
367 ----------------------------
368 -- Check_Discriminant_Use --
369 ----------------------------
371 procedure Check_Discriminant_Use (N : Node_Id) is
372 PN : constant Node_Id := Parent (N);
373 Disc : constant Entity_Id := Entity (N);
378 -- Any use in a default expression is legal
380 if In_Default_Expression then
383 elsif Nkind (PN) = N_Range then
385 -- Discriminant cannot be used to constrain a scalar type
389 if Nkind (P) = N_Range_Constraint
390 and then Nkind (Parent (P)) = N_Subtype_Indication
391 and then Nkind (Parent (Parent (P))) = N_Component_Definition
393 Error_Msg_N ("discriminant cannot constrain scalar type", N);
395 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
397 -- The following check catches the unusual case where
398 -- a discriminant appears within an index constraint
399 -- that is part of a larger expression within a constraint
400 -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
401 -- For now we only check case of record components, and
402 -- note that a similar check should also apply in the
403 -- case of discriminant constraints below. ???
405 -- Note that the check for N_Subtype_Declaration below is to
406 -- detect the valid use of discriminants in the constraints of a
407 -- subtype declaration when this subtype declaration appears
408 -- inside the scope of a record type (which is syntactically
409 -- illegal, but which may be created as part of derived type
410 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
413 if Ekind (Current_Scope) = E_Record_Type
414 and then Scope (Disc) = Current_Scope
416 (Nkind (Parent (P)) = N_Subtype_Indication
418 (Nkind (Parent (Parent (P))) = N_Component_Definition
420 Nkind (Parent (Parent (P))) = N_Subtype_Declaration)
421 and then Paren_Count (N) = 0)
424 ("discriminant must appear alone in component constraint", N);
428 -- Detect a common beginner error:
430 -- type R (D : Positive := 100) is record
431 -- Name : String (1 .. D);
434 -- The default value causes an object of type R to be
435 -- allocated with room for Positive'Last characters.
443 function Large_Storage_Type (T : Entity_Id) return Boolean;
444 -- Return True if type T has a large enough range that
445 -- any array whose index type covered the whole range of
446 -- the type would likely raise Storage_Error.
448 ------------------------
449 -- Large_Storage_Type --
450 ------------------------
452 function Large_Storage_Type (T : Entity_Id) return Boolean is
457 T = Standard_Positive
459 T = Standard_Natural;
460 end Large_Storage_Type;
463 -- Check that the Disc has a large range
465 if not Large_Storage_Type (Etype (Disc)) then
469 -- If the enclosing type is limited, we allocate only the
470 -- default value, not the maximum, and there is no need for
473 if Is_Limited_Type (Scope (Disc)) then
477 -- Check that it is the high bound
479 if N /= High_Bound (PN)
480 or else No (Discriminant_Default_Value (Disc))
485 -- Check the array allows a large range at this bound.
486 -- First find the array
490 if Nkind (SI) /= N_Subtype_Indication then
494 T := Entity (Subtype_Mark (SI));
496 if not Is_Array_Type (T) then
500 -- Next, find the dimension
502 TB := First_Index (T);
503 CB := First (Constraints (P));
505 and then Present (TB)
506 and then Present (CB)
517 -- Now, check the dimension has a large range
519 if not Large_Storage_Type (Etype (TB)) then
523 -- Warn about the danger
526 ("?creation of & object may raise Storage_Error!",
535 -- Legal case is in index or discriminant constraint
537 elsif Nkind (PN) = N_Index_Or_Discriminant_Constraint
538 or else Nkind (PN) = N_Discriminant_Association
540 if Paren_Count (N) > 0 then
542 ("discriminant in constraint must appear alone", N);
544 elsif Nkind (N) = N_Expanded_Name
545 and then Comes_From_Source (N)
548 ("discriminant must appear alone as a direct name", N);
553 -- Otherwise, context is an expression. It should not be within
554 -- (i.e. a subexpression of) a constraint for a component.
559 while Nkind (P) /= N_Component_Declaration
560 and then Nkind (P) /= N_Subtype_Indication
561 and then Nkind (P) /= N_Entry_Declaration
568 -- If the discriminant is used in an expression that is a bound
569 -- of a scalar type, an Itype is created and the bounds are attached
570 -- to its range, not to the original subtype indication. Such use
571 -- is of course a double fault.
573 if (Nkind (P) = N_Subtype_Indication
575 (Nkind (Parent (P)) = N_Component_Definition
577 Nkind (Parent (P)) = N_Derived_Type_Definition)
578 and then D = Constraint (P))
580 -- The constraint itself may be given by a subtype indication,
581 -- rather than by a more common discrete range.
583 or else (Nkind (P) = N_Subtype_Indication
585 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
586 or else Nkind (P) = N_Entry_Declaration
587 or else Nkind (D) = N_Defining_Identifier
590 ("discriminant in constraint must appear alone", N);
593 end Check_Discriminant_Use;
595 --------------------------------
596 -- Check_For_Visible_Operator --
597 --------------------------------
599 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
601 if Is_Invisible_Operator (N, T) then
603 ("operator for} is not directly visible!", N, First_Subtype (T));
604 Error_Msg_N ("use clause would make operation legal!", N);
606 end Check_For_Visible_Operator;
608 ----------------------------------
609 -- Check_Fully_Declared_Prefix --
610 ----------------------------------
612 procedure Check_Fully_Declared_Prefix
617 -- Check that the designated type of the prefix of a dereference is
618 -- not an incomplete type. This cannot be done unconditionally, because
619 -- dereferences of private types are legal in default expressions. This
620 -- case is taken care of in Check_Fully_Declared, called below. There
621 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
623 -- This consideration also applies to similar checks for allocators,
624 -- qualified expressions, and type conversions.
626 -- An additional exception concerns other per-object expressions that
627 -- are not directly related to component declarations, in particular
628 -- representation pragmas for tasks. These will be per-object
629 -- expressions if they depend on discriminants or some global entity.
630 -- If the task has access discriminants, the designated type may be
631 -- incomplete at the point the expression is resolved. This resolution
632 -- takes place within the body of the initialization procedure, where
633 -- the discriminant is replaced by its discriminal.
635 if Is_Entity_Name (Pref)
636 and then Ekind (Entity (Pref)) = E_In_Parameter
640 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
641 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
642 -- Analyze_Object_Renaming, and Freeze_Entity.
644 elsif Ada_Version >= Ada_05
645 and then Is_Entity_Name (Pref)
646 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
648 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
652 Check_Fully_Declared (Typ, Parent (Pref));
654 end Check_Fully_Declared_Prefix;
656 ------------------------------
657 -- Check_Infinite_Recursion --
658 ------------------------------
660 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
664 function Same_Argument_List return Boolean;
665 -- Check whether list of actuals is identical to list of formals
666 -- of called function (which is also the enclosing scope).
668 ------------------------
669 -- Same_Argument_List --
670 ------------------------
672 function Same_Argument_List return Boolean is
678 if not Is_Entity_Name (Name (N)) then
681 Subp := Entity (Name (N));
684 F := First_Formal (Subp);
685 A := First_Actual (N);
686 while Present (F) and then Present (A) loop
687 if not Is_Entity_Name (A)
688 or else Entity (A) /= F
698 end Same_Argument_List;
700 -- Start of processing for Check_Infinite_Recursion
703 -- Loop moving up tree, quitting if something tells us we are
704 -- definitely not in an infinite recursion situation.
709 exit when Nkind (P) = N_Subprogram_Body;
711 if Nkind (P) = N_Or_Else or else
712 Nkind (P) = N_And_Then or else
713 Nkind (P) = N_If_Statement or else
714 Nkind (P) = N_Case_Statement
718 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
719 and then C /= First (Statements (P))
721 -- If the call is the expression of a return statement and
722 -- the actuals are identical to the formals, it's worth a
723 -- warning. However, we skip this if there is an immediately
724 -- preceding raise statement, since the call is never executed.
726 -- Furthermore, this corresponds to a common idiom:
728 -- function F (L : Thing) return Boolean is
730 -- raise Program_Error;
734 -- for generating a stub function
736 if Nkind (Parent (N)) = N_Simple_Return_Statement
737 and then Same_Argument_List
739 exit when not Is_List_Member (Parent (N));
741 -- OK, return statement is in a statement list, look for raise
747 -- Skip past N_Freeze_Entity nodes generated by expansion
749 Nod := Prev (Parent (N));
751 and then Nkind (Nod) = N_Freeze_Entity
756 -- If no raise statement, give warning
758 exit when Nkind (Nod) /= N_Raise_Statement
760 (Nkind (Nod) not in N_Raise_xxx_Error
761 or else Present (Condition (Nod)));
772 Error_Msg_N ("!?possible infinite recursion", N);
773 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
776 end Check_Infinite_Recursion;
778 -------------------------------
779 -- Check_Initialization_Call --
780 -------------------------------
782 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
783 Typ : constant Entity_Id := Etype (First_Formal (Nam));
785 function Uses_SS (T : Entity_Id) return Boolean;
786 -- Check whether the creation of an object of the type will involve
787 -- use of the secondary stack. If T is a record type, this is true
788 -- if the expression for some component uses the secondary stack, eg.
789 -- through a call to a function that returns an unconstrained value.
790 -- False if T is controlled, because cleanups occur elsewhere.
796 function Uses_SS (T : Entity_Id) return Boolean is
799 Full_Type : Entity_Id := Underlying_Type (T);
802 -- Normally we want to use the underlying type, but if it's not set
803 -- then continue with T.
805 if not Present (Full_Type) then
809 if Is_Controlled (Full_Type) then
812 elsif Is_Array_Type (Full_Type) then
813 return Uses_SS (Component_Type (Full_Type));
815 elsif Is_Record_Type (Full_Type) then
816 Comp := First_Component (Full_Type);
817 while Present (Comp) loop
818 if Ekind (Comp) = E_Component
819 and then Nkind (Parent (Comp)) = N_Component_Declaration
821 -- The expression for a dynamic component may be rewritten
822 -- as a dereference, so retrieve original node.
824 Expr := Original_Node (Expression (Parent (Comp)));
826 -- Return True if the expression is a call to a function
827 -- (including an attribute function such as Image) with
828 -- a result that requires a transient scope.
830 if (Nkind (Expr) = N_Function_Call
831 or else (Nkind (Expr) = N_Attribute_Reference
832 and then Present (Expressions (Expr))))
833 and then Requires_Transient_Scope (Etype (Expr))
837 elsif Uses_SS (Etype (Comp)) then
842 Next_Component (Comp);
852 -- Start of processing for Check_Initialization_Call
855 -- Establish a transient scope if the type needs it
857 if Uses_SS (Typ) then
858 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
860 end Check_Initialization_Call;
862 ------------------------------
863 -- Check_Parameterless_Call --
864 ------------------------------
866 procedure Check_Parameterless_Call (N : Node_Id) is
869 function Prefix_Is_Access_Subp return Boolean;
870 -- If the prefix is of an access_to_subprogram type, the node must be
871 -- rewritten as a call. Ditto if the prefix is overloaded and all its
872 -- interpretations are access to subprograms.
874 ---------------------------
875 -- Prefix_Is_Access_Subp --
876 ---------------------------
878 function Prefix_Is_Access_Subp return Boolean is
883 if not Is_Overloaded (N) then
885 Ekind (Etype (N)) = E_Subprogram_Type
886 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
888 Get_First_Interp (N, I, It);
889 while Present (It.Typ) loop
890 if Ekind (It.Typ) /= E_Subprogram_Type
891 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
896 Get_Next_Interp (I, It);
901 end Prefix_Is_Access_Subp;
903 -- Start of processing for Check_Parameterless_Call
906 -- Defend against junk stuff if errors already detected
908 if Total_Errors_Detected /= 0 then
909 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
911 elsif Nkind (N) in N_Has_Chars
912 and then Chars (N) in Error_Name_Or_No_Name
920 -- If the context expects a value, and the name is a procedure,
921 -- this is most likely a missing 'Access. Do not try to resolve
922 -- the parameterless call, error will be caught when the outer
925 if Is_Entity_Name (N)
926 and then Ekind (Entity (N)) = E_Procedure
927 and then not Is_Overloaded (N)
929 (Nkind (Parent (N)) = N_Parameter_Association
930 or else Nkind (Parent (N)) = N_Function_Call
931 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
936 -- Rewrite as call if overloadable entity that is (or could be, in
937 -- the overloaded case) a function call. If we know for sure that
938 -- the entity is an enumeration literal, we do not rewrite it.
940 if (Is_Entity_Name (N)
941 and then Is_Overloadable (Entity (N))
942 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
943 or else Is_Overloaded (N)))
945 -- Rewrite as call if it is an explicit deference of an expression of
946 -- a subprogram access type, and the suprogram type is not that of a
947 -- procedure or entry.
950 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
952 -- Rewrite as call if it is a selected component which is a function,
953 -- this is the case of a call to a protected function (which may be
954 -- overloaded with other protected operations).
957 (Nkind (N) = N_Selected_Component
958 and then (Ekind (Entity (Selector_Name (N))) = E_Function
960 ((Ekind (Entity (Selector_Name (N))) = E_Entry
962 Ekind (Entity (Selector_Name (N))) = E_Procedure)
963 and then Is_Overloaded (Selector_Name (N)))))
965 -- If one of the above three conditions is met, rewrite as call.
966 -- Apply the rewriting only once.
969 if Nkind (Parent (N)) /= N_Function_Call
970 or else N /= Name (Parent (N))
974 -- If overloaded, overload set belongs to new copy
976 Save_Interps (N, Nam);
978 -- Change node to parameterless function call (note that the
979 -- Parameter_Associations associations field is left set to Empty,
980 -- its normal default value since there are no parameters)
982 Change_Node (N, N_Function_Call);
984 Set_Sloc (N, Sloc (Nam));
988 elsif Nkind (N) = N_Parameter_Association then
989 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
991 end Check_Parameterless_Call;
993 ----------------------
994 -- Is_Predefined_Op --
995 ----------------------
997 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
999 return Is_Intrinsic_Subprogram (Nam)
1000 and then not Is_Generic_Instance (Nam)
1001 and then Chars (Nam) in Any_Operator_Name
1002 and then (No (Alias (Nam))
1003 or else Is_Predefined_Op (Alias (Nam)));
1004 end Is_Predefined_Op;
1006 -----------------------------
1007 -- Make_Call_Into_Operator --
1008 -----------------------------
1010 procedure Make_Call_Into_Operator
1015 Op_Name : constant Name_Id := Chars (Op_Id);
1016 Act1 : Node_Id := First_Actual (N);
1017 Act2 : Node_Id := Next_Actual (Act1);
1018 Error : Boolean := False;
1019 Func : constant Entity_Id := Entity (Name (N));
1020 Is_Binary : constant Boolean := Present (Act2);
1022 Opnd_Type : Entity_Id;
1023 Orig_Type : Entity_Id := Empty;
1026 type Kind_Test is access function (E : Entity_Id) return Boolean;
1028 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
1029 -- Determine whether E is an access type declared by an access decla-
1030 -- ration, and not an (anonymous) allocator type.
1032 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1033 -- If the operand is not universal, and the operator is given by a
1034 -- expanded name, verify that the operand has an interpretation with
1035 -- a type defined in the given scope of the operator.
1037 function Type_In_P (Test : Kind_Test) return Entity_Id;
1038 -- Find a type of the given class in the package Pack that contains
1041 -----------------------------
1042 -- Is_Definite_Access_Type --
1043 -----------------------------
1045 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1046 Btyp : constant Entity_Id := Base_Type (E);
1048 return Ekind (Btyp) = E_Access_Type
1049 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1050 and then Comes_From_Source (Btyp));
1051 end Is_Definite_Access_Type;
1053 ---------------------------
1054 -- Operand_Type_In_Scope --
1055 ---------------------------
1057 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1058 Nod : constant Node_Id := Right_Opnd (Op_Node);
1063 if not Is_Overloaded (Nod) then
1064 return Scope (Base_Type (Etype (Nod))) = S;
1067 Get_First_Interp (Nod, I, It);
1068 while Present (It.Typ) loop
1069 if Scope (Base_Type (It.Typ)) = S then
1073 Get_Next_Interp (I, It);
1078 end Operand_Type_In_Scope;
1084 function Type_In_P (Test : Kind_Test) return Entity_Id is
1087 function In_Decl return Boolean;
1088 -- Verify that node is not part of the type declaration for the
1089 -- candidate type, which would otherwise be invisible.
1095 function In_Decl return Boolean is
1096 Decl_Node : constant Node_Id := Parent (E);
1102 if Etype (E) = Any_Type then
1105 elsif No (Decl_Node) then
1110 and then Nkind (N2) /= N_Compilation_Unit
1112 if N2 = Decl_Node then
1123 -- Start of processing for Type_In_P
1126 -- If the context type is declared in the prefix package, this
1127 -- is the desired base type.
1129 if Scope (Base_Type (Typ)) = Pack
1132 return Base_Type (Typ);
1135 E := First_Entity (Pack);
1136 while Present (E) loop
1138 and then not In_Decl
1150 -- Start of processing for Make_Call_Into_Operator
1153 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1158 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1159 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1160 Save_Interps (Act1, Left_Opnd (Op_Node));
1161 Save_Interps (Act2, Right_Opnd (Op_Node));
1162 Act1 := Left_Opnd (Op_Node);
1163 Act2 := Right_Opnd (Op_Node);
1168 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1169 Save_Interps (Act1, Right_Opnd (Op_Node));
1170 Act1 := Right_Opnd (Op_Node);
1173 -- If the operator is denoted by an expanded name, and the prefix is
1174 -- not Standard, but the operator is a predefined one whose scope is
1175 -- Standard, then this is an implicit_operator, inserted as an
1176 -- interpretation by the procedure of the same name. This procedure
1177 -- overestimates the presence of implicit operators, because it does
1178 -- not examine the type of the operands. Verify now that the operand
1179 -- type appears in the given scope. If right operand is universal,
1180 -- check the other operand. In the case of concatenation, either
1181 -- argument can be the component type, so check the type of the result.
1182 -- If both arguments are literals, look for a type of the right kind
1183 -- defined in the given scope. This elaborate nonsense is brought to
1184 -- you courtesy of b33302a. The type itself must be frozen, so we must
1185 -- find the type of the proper class in the given scope.
1187 -- A final wrinkle is the multiplication operator for fixed point
1188 -- types, which is defined in Standard only, and not in the scope of
1189 -- the fixed_point type itself.
1191 if Nkind (Name (N)) = N_Expanded_Name then
1192 Pack := Entity (Prefix (Name (N)));
1194 -- If the entity being called is defined in the given package,
1195 -- it is a renaming of a predefined operator, and known to be
1198 if Scope (Entity (Name (N))) = Pack
1199 and then Pack /= Standard_Standard
1203 -- Visibility does not need to be checked in an instance: if the
1204 -- operator was not visible in the generic it has been diagnosed
1205 -- already, else there is an implicit copy of it in the instance.
1207 elsif In_Instance then
1210 elsif (Op_Name = Name_Op_Multiply
1211 or else Op_Name = Name_Op_Divide)
1212 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1213 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1215 if Pack /= Standard_Standard then
1219 -- Ada 2005, AI-420: Predefined equality on Universal_Access
1222 elsif Ada_Version >= Ada_05
1223 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1224 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1229 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1231 if Op_Name = Name_Op_Concat then
1232 Opnd_Type := Base_Type (Typ);
1234 elsif (Scope (Opnd_Type) = Standard_Standard
1236 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1238 and then not Comes_From_Source (Opnd_Type))
1240 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1243 if Scope (Opnd_Type) = Standard_Standard then
1245 -- Verify that the scope contains a type that corresponds to
1246 -- the given literal. Optimize the case where Pack is Standard.
1248 if Pack /= Standard_Standard then
1250 if Opnd_Type = Universal_Integer then
1251 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1253 elsif Opnd_Type = Universal_Real then
1254 Orig_Type := Type_In_P (Is_Real_Type'Access);
1256 elsif Opnd_Type = Any_String then
1257 Orig_Type := Type_In_P (Is_String_Type'Access);
1259 elsif Opnd_Type = Any_Access then
1260 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1262 elsif Opnd_Type = Any_Composite then
1263 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1265 if Present (Orig_Type) then
1266 if Has_Private_Component (Orig_Type) then
1269 Set_Etype (Act1, Orig_Type);
1272 Set_Etype (Act2, Orig_Type);
1281 Error := No (Orig_Type);
1284 elsif Ekind (Opnd_Type) = E_Allocator_Type
1285 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1289 -- If the type is defined elsewhere, and the operator is not
1290 -- defined in the given scope (by a renaming declaration, e.g.)
1291 -- then this is an error as well. If an extension of System is
1292 -- present, and the type may be defined there, Pack must be
1295 elsif Scope (Opnd_Type) /= Pack
1296 and then Scope (Op_Id) /= Pack
1297 and then (No (System_Aux_Id)
1298 or else Scope (Opnd_Type) /= System_Aux_Id
1299 or else Pack /= Scope (System_Aux_Id))
1301 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1304 Error := not Operand_Type_In_Scope (Pack);
1307 elsif Pack = Standard_Standard
1308 and then not Operand_Type_In_Scope (Standard_Standard)
1315 Error_Msg_Node_2 := Pack;
1317 ("& not declared in&", N, Selector_Name (Name (N)));
1318 Set_Etype (N, Any_Type);
1323 Set_Chars (Op_Node, Op_Name);
1325 if not Is_Private_Type (Etype (N)) then
1326 Set_Etype (Op_Node, Base_Type (Etype (N)));
1328 Set_Etype (Op_Node, Etype (N));
1331 -- If this is a call to a function that renames a predefined equality,
1332 -- the renaming declaration provides a type that must be used to
1333 -- resolve the operands. This must be done now because resolution of
1334 -- the equality node will not resolve any remaining ambiguity, and it
1335 -- assumes that the first operand is not overloaded.
1337 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1338 and then Ekind (Func) = E_Function
1339 and then Is_Overloaded (Act1)
1341 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1342 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1345 Set_Entity (Op_Node, Op_Id);
1346 Generate_Reference (Op_Id, N, ' ');
1347 Rewrite (N, Op_Node);
1349 -- If this is an arithmetic operator and the result type is private,
1350 -- the operands and the result must be wrapped in conversion to
1351 -- expose the underlying numeric type and expand the proper checks,
1352 -- e.g. on division.
1354 if Is_Private_Type (Typ) then
1356 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1357 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1358 Resolve_Intrinsic_Operator (N, Typ);
1360 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1361 Resolve_Intrinsic_Unary_Operator (N, Typ);
1370 -- For predefined operators on literals, the operation freezes
1373 if Present (Orig_Type) then
1374 Set_Etype (Act1, Orig_Type);
1375 Freeze_Expression (Act1);
1377 end Make_Call_Into_Operator;
1383 function Operator_Kind
1385 Is_Binary : Boolean) return Node_Kind
1391 if Op_Name = Name_Op_And then
1393 elsif Op_Name = Name_Op_Or then
1395 elsif Op_Name = Name_Op_Xor then
1397 elsif Op_Name = Name_Op_Eq then
1399 elsif Op_Name = Name_Op_Ne then
1401 elsif Op_Name = Name_Op_Lt then
1403 elsif Op_Name = Name_Op_Le then
1405 elsif Op_Name = Name_Op_Gt then
1407 elsif Op_Name = Name_Op_Ge then
1409 elsif Op_Name = Name_Op_Add then
1411 elsif Op_Name = Name_Op_Subtract then
1412 Kind := N_Op_Subtract;
1413 elsif Op_Name = Name_Op_Concat then
1414 Kind := N_Op_Concat;
1415 elsif Op_Name = Name_Op_Multiply then
1416 Kind := N_Op_Multiply;
1417 elsif Op_Name = Name_Op_Divide then
1418 Kind := N_Op_Divide;
1419 elsif Op_Name = Name_Op_Mod then
1421 elsif Op_Name = Name_Op_Rem then
1423 elsif Op_Name = Name_Op_Expon then
1426 raise Program_Error;
1432 if Op_Name = Name_Op_Add then
1434 elsif Op_Name = Name_Op_Subtract then
1436 elsif Op_Name = Name_Op_Abs then
1438 elsif Op_Name = Name_Op_Not then
1441 raise Program_Error;
1448 -----------------------------
1449 -- Pre_Analyze_And_Resolve --
1450 -----------------------------
1452 procedure Pre_Analyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1453 Save_Full_Analysis : constant Boolean := Full_Analysis;
1456 Full_Analysis := False;
1457 Expander_Mode_Save_And_Set (False);
1459 -- We suppress all checks for this analysis, since the checks will
1460 -- be applied properly, and in the right location, when the default
1461 -- expression is reanalyzed and reexpanded later on.
1463 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1465 Expander_Mode_Restore;
1466 Full_Analysis := Save_Full_Analysis;
1467 end Pre_Analyze_And_Resolve;
1469 -- Version without context type
1471 procedure Pre_Analyze_And_Resolve (N : Node_Id) is
1472 Save_Full_Analysis : constant Boolean := Full_Analysis;
1475 Full_Analysis := False;
1476 Expander_Mode_Save_And_Set (False);
1479 Resolve (N, Etype (N), Suppress => All_Checks);
1481 Expander_Mode_Restore;
1482 Full_Analysis := Save_Full_Analysis;
1483 end Pre_Analyze_And_Resolve;
1485 ----------------------------------
1486 -- Replace_Actual_Discriminants --
1487 ----------------------------------
1489 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1490 Loc : constant Source_Ptr := Sloc (N);
1491 Tsk : Node_Id := Empty;
1493 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1499 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1503 if Nkind (Nod) = N_Identifier then
1504 Ent := Entity (Nod);
1507 and then Ekind (Ent) = E_Discriminant
1510 Make_Selected_Component (Loc,
1511 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1512 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1514 Set_Etype (Nod, Etype (Ent));
1522 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1524 -- Start of processing for Replace_Actual_Discriminants
1527 if not Expander_Active then
1531 if Nkind (Name (N)) = N_Selected_Component then
1532 Tsk := Prefix (Name (N));
1534 elsif Nkind (Name (N)) = N_Indexed_Component then
1535 Tsk := Prefix (Prefix (Name (N)));
1541 Replace_Discrs (Default);
1543 end Replace_Actual_Discriminants;
1549 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1550 Ambiguous : Boolean := False;
1551 Ctx_Type : Entity_Id := Typ;
1552 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1553 Err_Type : Entity_Id := Empty;
1554 Found : Boolean := False;
1557 I1 : Interp_Index := 0; -- prevent junk warning
1560 Seen : Entity_Id := Empty; -- prevent junk warning
1562 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1563 -- Determine whether a node comes from a predefined library unit or
1566 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1567 -- Try and fix up a literal so that it matches its expected type. New
1568 -- literals are manufactured if necessary to avoid cascaded errors.
1570 procedure Resolution_Failed;
1571 -- Called when attempt at resolving current expression fails
1573 ------------------------------------
1574 -- Comes_From_Predefined_Lib_Unit --
1575 -------------------------------------
1577 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1580 Sloc (Nod) = Standard_Location
1581 or else Is_Predefined_File_Name (Unit_File_Name (
1582 Get_Source_Unit (Sloc (Nod))));
1583 end Comes_From_Predefined_Lib_Unit;
1585 --------------------
1586 -- Patch_Up_Value --
1587 --------------------
1589 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1591 if Nkind (N) = N_Integer_Literal
1592 and then Is_Real_Type (Typ)
1595 Make_Real_Literal (Sloc (N),
1596 Realval => UR_From_Uint (Intval (N))));
1597 Set_Etype (N, Universal_Real);
1598 Set_Is_Static_Expression (N);
1600 elsif Nkind (N) = N_Real_Literal
1601 and then Is_Integer_Type (Typ)
1604 Make_Integer_Literal (Sloc (N),
1605 Intval => UR_To_Uint (Realval (N))));
1606 Set_Etype (N, Universal_Integer);
1607 Set_Is_Static_Expression (N);
1608 elsif Nkind (N) = N_String_Literal
1609 and then Is_Character_Type (Typ)
1611 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1613 Make_Character_Literal (Sloc (N),
1615 Char_Literal_Value =>
1616 UI_From_Int (Character'Pos ('A'))));
1617 Set_Etype (N, Any_Character);
1618 Set_Is_Static_Expression (N);
1620 elsif Nkind (N) /= N_String_Literal
1621 and then Is_String_Type (Typ)
1624 Make_String_Literal (Sloc (N),
1625 Strval => End_String));
1627 elsif Nkind (N) = N_Range then
1628 Patch_Up_Value (Low_Bound (N), Typ);
1629 Patch_Up_Value (High_Bound (N), Typ);
1633 -----------------------
1634 -- Resolution_Failed --
1635 -----------------------
1637 procedure Resolution_Failed is
1639 Patch_Up_Value (N, Typ);
1641 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1642 Set_Is_Overloaded (N, False);
1644 -- The caller will return without calling the expander, so we need
1645 -- to set the analyzed flag. Note that it is fine to set Analyzed
1646 -- to True even if we are in the middle of a shallow analysis,
1647 -- (see the spec of sem for more details) since this is an error
1648 -- situation anyway, and there is no point in repeating the
1649 -- analysis later (indeed it won't work to repeat it later, since
1650 -- we haven't got a clear resolution of which entity is being
1653 Set_Analyzed (N, True);
1655 end Resolution_Failed;
1657 -- Start of processing for Resolve
1664 -- Access attribute on remote subprogram cannot be used for
1665 -- a non-remote access-to-subprogram type.
1667 if Nkind (N) = N_Attribute_Reference
1668 and then (Attribute_Name (N) = Name_Access
1669 or else Attribute_Name (N) = Name_Unrestricted_Access
1670 or else Attribute_Name (N) = Name_Unchecked_Access)
1671 and then Comes_From_Source (N)
1672 and then Is_Entity_Name (Prefix (N))
1673 and then Is_Subprogram (Entity (Prefix (N)))
1674 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1675 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1678 ("prefix must statically denote a non-remote subprogram", N);
1681 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1683 -- If the context is a Remote_Access_To_Subprogram, access attributes
1684 -- must be resolved with the corresponding fat pointer. There is no need
1685 -- to check for the attribute name since the return type of an
1686 -- attribute is never a remote type.
1688 if Nkind (N) = N_Attribute_Reference
1689 and then Comes_From_Source (N)
1690 and then (Is_Remote_Call_Interface (Typ)
1691 or else Is_Remote_Types (Typ))
1694 Attr : constant Attribute_Id :=
1695 Get_Attribute_Id (Attribute_Name (N));
1696 Pref : constant Node_Id := Prefix (N);
1699 Is_Remote : Boolean := True;
1702 -- Check that Typ is a remote access-to-subprogram type
1704 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1705 -- Prefix (N) must statically denote a remote subprogram
1706 -- declared in a package specification.
1708 if Attr = Attribute_Access then
1709 Decl := Unit_Declaration_Node (Entity (Pref));
1711 if Nkind (Decl) = N_Subprogram_Body then
1712 Spec := Corresponding_Spec (Decl);
1714 if not No (Spec) then
1715 Decl := Unit_Declaration_Node (Spec);
1719 Spec := Parent (Decl);
1721 if not Is_Entity_Name (Prefix (N))
1722 or else Nkind (Spec) /= N_Package_Specification
1724 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1728 ("prefix must statically denote a remote subprogram ",
1733 -- If we are generating code for a distributed program.
1734 -- perform semantic checks against the corresponding
1737 if (Attr = Attribute_Access
1738 or else Attr = Attribute_Unchecked_Access
1739 or else Attr = Attribute_Unrestricted_Access)
1740 and then Expander_Active
1741 and then Get_PCS_Name /= Name_No_DSA
1743 Check_Subtype_Conformant
1744 (New_Id => Entity (Prefix (N)),
1745 Old_Id => Designated_Type
1746 (Corresponding_Remote_Type (Typ)),
1750 Process_Remote_AST_Attribute (N, Typ);
1757 Debug_A_Entry ("resolving ", N);
1759 if Comes_From_Source (N) then
1760 if Is_Fixed_Point_Type (Typ) then
1761 Check_Restriction (No_Fixed_Point, N);
1763 elsif Is_Floating_Point_Type (Typ)
1764 and then Typ /= Universal_Real
1765 and then Typ /= Any_Real
1767 Check_Restriction (No_Floating_Point, N);
1771 -- Return if already analyzed
1773 if Analyzed (N) then
1774 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
1777 -- Return if type = Any_Type (previous error encountered)
1779 elsif Etype (N) = Any_Type then
1780 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
1784 Check_Parameterless_Call (N);
1786 -- If not overloaded, then we know the type, and all that needs doing
1787 -- is to check that this type is compatible with the context.
1789 if not Is_Overloaded (N) then
1790 Found := Covers (Typ, Etype (N));
1791 Expr_Type := Etype (N);
1793 -- In the overloaded case, we must select the interpretation that
1794 -- is compatible with the context (i.e. the type passed to Resolve)
1797 -- Loop through possible interpretations
1799 Get_First_Interp (N, I, It);
1800 Interp_Loop : while Present (It.Typ) loop
1802 -- We are only interested in interpretations that are compatible
1803 -- with the expected type, any other interpretations are ignored.
1805 if not Covers (Typ, It.Typ) then
1806 if Debug_Flag_V then
1807 Write_Str (" interpretation incompatible with context");
1812 -- Skip the current interpretation if it is disabled by an
1813 -- abstract operator. This action is performed only when the
1814 -- type against which we are resolving is the same as the
1815 -- type of the interpretation.
1817 if Ada_Version >= Ada_05
1818 and then It.Typ = Typ
1819 and then Typ /= Universal_Integer
1820 and then Typ /= Universal_Real
1821 and then Present (It.Abstract_Op)
1826 -- First matching interpretation
1832 Expr_Type := It.Typ;
1834 -- Matching interpretation that is not the first, maybe an
1835 -- error, but there are some cases where preference rules are
1836 -- used to choose between the two possibilities. These and
1837 -- some more obscure cases are handled in Disambiguate.
1840 -- If the current statement is part of a predefined library
1841 -- unit, then all interpretations which come from user level
1842 -- packages should not be considered.
1845 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
1850 Error_Msg_Sloc := Sloc (Seen);
1851 It1 := Disambiguate (N, I1, I, Typ);
1853 -- Disambiguation has succeeded. Skip the remaining
1856 if It1 /= No_Interp then
1858 Expr_Type := It1.Typ;
1860 while Present (It.Typ) loop
1861 Get_Next_Interp (I, It);
1865 -- Before we issue an ambiguity complaint, check for
1866 -- the case of a subprogram call where at least one
1867 -- of the arguments is Any_Type, and if so, suppress
1868 -- the message, since it is a cascaded error.
1870 if Nkind (N) = N_Function_Call
1871 or else Nkind (N) = N_Procedure_Call_Statement
1878 A := First_Actual (N);
1879 while Present (A) loop
1882 if Nkind (E) = N_Parameter_Association then
1883 E := Explicit_Actual_Parameter (E);
1886 if Etype (E) = Any_Type then
1887 if Debug_Flag_V then
1888 Write_Str ("Any_Type in call");
1899 elsif Nkind (N) in N_Binary_Op
1900 and then (Etype (Left_Opnd (N)) = Any_Type
1901 or else Etype (Right_Opnd (N)) = Any_Type)
1905 elsif Nkind (N) in N_Unary_Op
1906 and then Etype (Right_Opnd (N)) = Any_Type
1911 -- Not that special case, so issue message using the
1912 -- flag Ambiguous to control printing of the header
1913 -- message only at the start of an ambiguous set.
1915 if not Ambiguous then
1916 if Nkind (N) = N_Function_Call
1917 and then Nkind (Name (N)) = N_Explicit_Dereference
1920 ("ambiguous expression "
1921 & "(cannot resolve indirect call)!", N);
1924 ("ambiguous expression (cannot resolve&)!",
1930 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
1932 ("\\possible interpretation (inherited)#!", N);
1934 Error_Msg_N ("\\possible interpretation#!", N);
1938 Error_Msg_Sloc := Sloc (It.Nam);
1940 -- By default, the error message refers to the candidate
1941 -- interpretation. But if it is a predefined operator, it
1942 -- is implicitly declared at the declaration of the type
1943 -- of the operand. Recover the sloc of that declaration
1944 -- for the error message.
1946 if Nkind (N) in N_Op
1947 and then Scope (It.Nam) = Standard_Standard
1948 and then not Is_Overloaded (Right_Opnd (N))
1949 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
1952 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
1954 if Comes_From_Source (Err_Type)
1955 and then Present (Parent (Err_Type))
1957 Error_Msg_Sloc := Sloc (Parent (Err_Type));
1960 elsif Nkind (N) in N_Binary_Op
1961 and then Scope (It.Nam) = Standard_Standard
1962 and then not Is_Overloaded (Left_Opnd (N))
1963 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
1966 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
1968 if Comes_From_Source (Err_Type)
1969 and then Present (Parent (Err_Type))
1971 Error_Msg_Sloc := Sloc (Parent (Err_Type));
1974 -- If this is an indirect call, use the subprogram_type
1975 -- in the message, to have a meaningful location.
1976 -- Indicate as well if this is an inherited operation,
1977 -- created by a type declaration.
1979 elsif Nkind (N) = N_Function_Call
1980 and then Nkind (Name (N)) = N_Explicit_Dereference
1981 and then Is_Type (It.Nam)
1985 Sloc (Associated_Node_For_Itype (Err_Type));
1990 if Nkind (N) in N_Op
1991 and then Scope (It.Nam) = Standard_Standard
1992 and then Present (Err_Type)
1994 -- Special-case the message for universal_fixed
1995 -- operators, which are not declared with the type
1996 -- of the operand, but appear forever in Standard.
1998 if It.Typ = Universal_Fixed
1999 and then Scope (It.Nam) = Standard_Standard
2002 ("\\possible interpretation as " &
2003 "universal_fixed operation " &
2004 "(RM 4.5.5 (19))", N);
2007 ("\\possible interpretation (predefined)#!", N);
2011 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2014 ("\\possible interpretation (inherited)#!", N);
2016 Error_Msg_N ("\\possible interpretation#!", N);
2022 -- We have a matching interpretation, Expr_Type is the type
2023 -- from this interpretation, and Seen is the entity.
2025 -- For an operator, just set the entity name. The type will be
2026 -- set by the specific operator resolution routine.
2028 if Nkind (N) in N_Op then
2029 Set_Entity (N, Seen);
2030 Generate_Reference (Seen, N);
2032 elsif Nkind (N) = N_Character_Literal then
2033 Set_Etype (N, Expr_Type);
2035 -- For an explicit dereference, attribute reference, range,
2036 -- short-circuit form (which is not an operator node), or call
2037 -- with a name that is an explicit dereference, there is
2038 -- nothing to be done at this point.
2040 elsif Nkind (N) = N_Explicit_Dereference
2041 or else Nkind (N) = N_Attribute_Reference
2042 or else Nkind (N) = N_And_Then
2043 or else Nkind (N) = N_Indexed_Component
2044 or else Nkind (N) = N_Or_Else
2045 or else Nkind (N) = N_Range
2046 or else Nkind (N) = N_Selected_Component
2047 or else Nkind (N) = N_Slice
2048 or else Nkind (Name (N)) = N_Explicit_Dereference
2052 -- For procedure or function calls, set the type of the name,
2053 -- and also the entity pointer for the prefix
2055 elsif (Nkind (N) = N_Procedure_Call_Statement
2056 or else Nkind (N) = N_Function_Call)
2057 and then (Is_Entity_Name (Name (N))
2058 or else Nkind (Name (N)) = N_Operator_Symbol)
2060 Set_Etype (Name (N), Expr_Type);
2061 Set_Entity (Name (N), Seen);
2062 Generate_Reference (Seen, Name (N));
2064 elsif Nkind (N) = N_Function_Call
2065 and then Nkind (Name (N)) = N_Selected_Component
2067 Set_Etype (Name (N), Expr_Type);
2068 Set_Entity (Selector_Name (Name (N)), Seen);
2069 Generate_Reference (Seen, Selector_Name (Name (N)));
2071 -- For all other cases, just set the type of the Name
2074 Set_Etype (Name (N), Expr_Type);
2081 -- Move to next interpretation
2083 exit Interp_Loop when No (It.Typ);
2085 Get_Next_Interp (I, It);
2086 end loop Interp_Loop;
2089 -- At this stage Found indicates whether or not an acceptable
2090 -- interpretation exists. If not, then we have an error, except
2091 -- that if the context is Any_Type as a result of some other error,
2092 -- then we suppress the error report.
2095 if Typ /= Any_Type then
2097 -- If type we are looking for is Void, then this is the procedure
2098 -- call case, and the error is simply that what we gave is not a
2099 -- procedure name (we think of procedure calls as expressions with
2100 -- types internally, but the user doesn't think of them this way!)
2102 if Typ = Standard_Void_Type then
2104 -- Special case message if function used as a procedure
2106 if Nkind (N) = N_Procedure_Call_Statement
2107 and then Is_Entity_Name (Name (N))
2108 and then Ekind (Entity (Name (N))) = E_Function
2111 ("cannot use function & in a procedure call",
2112 Name (N), Entity (Name (N)));
2114 -- Otherwise give general message (not clear what cases this
2115 -- covers, but no harm in providing for them!)
2118 Error_Msg_N ("expect procedure name in procedure call", N);
2123 -- Otherwise we do have a subexpression with the wrong type
2125 -- Check for the case of an allocator which uses an access type
2126 -- instead of the designated type. This is a common error and we
2127 -- specialize the message, posting an error on the operand of the
2128 -- allocator, complaining that we expected the designated type of
2131 elsif Nkind (N) = N_Allocator
2132 and then Ekind (Typ) in Access_Kind
2133 and then Ekind (Etype (N)) in Access_Kind
2134 and then Designated_Type (Etype (N)) = Typ
2136 Wrong_Type (Expression (N), Designated_Type (Typ));
2139 -- Check for view mismatch on Null in instances, for which the
2140 -- view-swapping mechanism has no identifier.
2142 elsif (In_Instance or else In_Inlined_Body)
2143 and then (Nkind (N) = N_Null)
2144 and then Is_Private_Type (Typ)
2145 and then Is_Access_Type (Full_View (Typ))
2147 Resolve (N, Full_View (Typ));
2151 -- Check for an aggregate. Sometimes we can get bogus aggregates
2152 -- from misuse of parentheses, and we are about to complain about
2153 -- the aggregate without even looking inside it.
2155 -- Instead, if we have an aggregate of type Any_Composite, then
2156 -- analyze and resolve the component fields, and then only issue
2157 -- another message if we get no errors doing this (otherwise
2158 -- assume that the errors in the aggregate caused the problem).
2160 elsif Nkind (N) = N_Aggregate
2161 and then Etype (N) = Any_Composite
2163 -- Disable expansion in any case. If there is a type mismatch
2164 -- it may be fatal to try to expand the aggregate. The flag
2165 -- would otherwise be set to false when the error is posted.
2167 Expander_Active := False;
2170 procedure Check_Aggr (Aggr : Node_Id);
2171 -- Check one aggregate, and set Found to True if we have a
2172 -- definite error in any of its elements
2174 procedure Check_Elmt (Aelmt : Node_Id);
2175 -- Check one element of aggregate and set Found to True if
2176 -- we definitely have an error in the element.
2182 procedure Check_Aggr (Aggr : Node_Id) is
2186 if Present (Expressions (Aggr)) then
2187 Elmt := First (Expressions (Aggr));
2188 while Present (Elmt) loop
2194 if Present (Component_Associations (Aggr)) then
2195 Elmt := First (Component_Associations (Aggr));
2196 while Present (Elmt) loop
2198 -- If this is a default-initialized component, then
2199 -- there is nothing to check. The box will be
2200 -- replaced by the appropriate call during late
2203 if not Box_Present (Elmt) then
2204 Check_Elmt (Expression (Elmt));
2216 procedure Check_Elmt (Aelmt : Node_Id) is
2218 -- If we have a nested aggregate, go inside it (to
2219 -- attempt a naked analyze-resolve of the aggregate
2220 -- can cause undesirable cascaded errors). Do not
2221 -- resolve expression if it needs a type from context,
2222 -- as for integer * fixed expression.
2224 if Nkind (Aelmt) = N_Aggregate then
2230 if not Is_Overloaded (Aelmt)
2231 and then Etype (Aelmt) /= Any_Fixed
2236 if Etype (Aelmt) = Any_Type then
2247 -- If an error message was issued already, Found got reset
2248 -- to True, so if it is still False, issue the standard
2249 -- Wrong_Type message.
2252 if Is_Overloaded (N)
2253 and then Nkind (N) = N_Function_Call
2256 Subp_Name : Node_Id;
2258 if Is_Entity_Name (Name (N)) then
2259 Subp_Name := Name (N);
2261 elsif Nkind (Name (N)) = N_Selected_Component then
2263 -- Protected operation: retrieve operation name
2265 Subp_Name := Selector_Name (Name (N));
2267 raise Program_Error;
2270 Error_Msg_Node_2 := Typ;
2271 Error_Msg_NE ("no visible interpretation of&" &
2272 " matches expected type&", N, Subp_Name);
2275 if All_Errors_Mode then
2277 Index : Interp_Index;
2281 Error_Msg_N ("\\possible interpretations:", N);
2283 Get_First_Interp (Name (N), Index, It);
2284 while Present (It.Nam) loop
2285 Error_Msg_Sloc := Sloc (It.Nam);
2286 Error_Msg_Node_2 := It.Nam;
2288 ("\\ type& for & declared#", N, It.Typ);
2289 Get_Next_Interp (Index, It);
2294 Error_Msg_N ("\use -gnatf for details", N);
2297 Wrong_Type (N, Typ);
2305 -- Test if we have more than one interpretation for the context
2307 elsif Ambiguous then
2311 -- Here we have an acceptable interpretation for the context
2314 -- Propagate type information and normalize tree for various
2315 -- predefined operations. If the context only imposes a class of
2316 -- types, rather than a specific type, propagate the actual type
2319 if Typ = Any_Integer
2320 or else Typ = Any_Boolean
2321 or else Typ = Any_Modular
2322 or else Typ = Any_Real
2323 or else Typ = Any_Discrete
2325 Ctx_Type := Expr_Type;
2327 -- Any_Fixed is legal in a real context only if a specific
2328 -- fixed point type is imposed. If Norman Cohen can be
2329 -- confused by this, it deserves a separate message.
2332 and then Expr_Type = Any_Fixed
2334 Error_Msg_N ("illegal context for mixed mode operation", N);
2335 Set_Etype (N, Universal_Real);
2336 Ctx_Type := Universal_Real;
2340 -- A user-defined operator is tranformed into a function call at
2341 -- this point, so that further processing knows that operators are
2342 -- really operators (i.e. are predefined operators). User-defined
2343 -- operators that are intrinsic are just renamings of the predefined
2344 -- ones, and need not be turned into calls either, but if they rename
2345 -- a different operator, we must transform the node accordingly.
2346 -- Instantiations of Unchecked_Conversion are intrinsic but are
2347 -- treated as functions, even if given an operator designator.
2349 if Nkind (N) in N_Op
2350 and then Present (Entity (N))
2351 and then Ekind (Entity (N)) /= E_Operator
2354 if not Is_Predefined_Op (Entity (N)) then
2355 Rewrite_Operator_As_Call (N, Entity (N));
2357 elsif Present (Alias (Entity (N)))
2359 Nkind (Parent (Parent (Entity (N))))
2360 = N_Subprogram_Renaming_Declaration
2362 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2364 -- If the node is rewritten, it will be fully resolved in
2365 -- Rewrite_Renamed_Operator.
2367 if Analyzed (N) then
2373 case N_Subexpr'(Nkind (N)) is
2375 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2377 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2379 when N_And_Then | N_Or_Else
2380 => Resolve_Short_Circuit (N, Ctx_Type);
2382 when N_Attribute_Reference
2383 => Resolve_Attribute (N, Ctx_Type);
2385 when N_Character_Literal
2386 => Resolve_Character_Literal (N, Ctx_Type);
2388 when N_Conditional_Expression
2389 => Resolve_Conditional_Expression (N, Ctx_Type);
2391 when N_Expanded_Name
2392 => Resolve_Entity_Name (N, Ctx_Type);
2394 when N_Extension_Aggregate
2395 => Resolve_Extension_Aggregate (N, Ctx_Type);
2397 when N_Explicit_Dereference
2398 => Resolve_Explicit_Dereference (N, Ctx_Type);
2400 when N_Function_Call
2401 => Resolve_Call (N, Ctx_Type);
2404 => Resolve_Entity_Name (N, Ctx_Type);
2406 when N_Indexed_Component
2407 => Resolve_Indexed_Component (N, Ctx_Type);
2409 when N_Integer_Literal
2410 => Resolve_Integer_Literal (N, Ctx_Type);
2412 when N_Membership_Test
2413 => Resolve_Membership_Op (N, Ctx_Type);
2415 when N_Null => Resolve_Null (N, Ctx_Type);
2417 when N_Op_And | N_Op_Or | N_Op_Xor
2418 => Resolve_Logical_Op (N, Ctx_Type);
2420 when N_Op_Eq | N_Op_Ne
2421 => Resolve_Equality_Op (N, Ctx_Type);
2423 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2424 => Resolve_Comparison_Op (N, Ctx_Type);
2426 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2428 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2429 N_Op_Divide | N_Op_Mod | N_Op_Rem
2431 => Resolve_Arithmetic_Op (N, Ctx_Type);
2433 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2435 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2437 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2438 => Resolve_Unary_Op (N, Ctx_Type);
2440 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2442 when N_Procedure_Call_Statement
2443 => Resolve_Call (N, Ctx_Type);
2445 when N_Operator_Symbol
2446 => Resolve_Operator_Symbol (N, Ctx_Type);
2448 when N_Qualified_Expression
2449 => Resolve_Qualified_Expression (N, Ctx_Type);
2451 when N_Raise_xxx_Error
2452 => Set_Etype (N, Ctx_Type);
2454 when N_Range => Resolve_Range (N, Ctx_Type);
2457 => Resolve_Real_Literal (N, Ctx_Type);
2459 when N_Reference => Resolve_Reference (N, Ctx_Type);
2461 when N_Selected_Component
2462 => Resolve_Selected_Component (N, Ctx_Type);
2464 when N_Slice => Resolve_Slice (N, Ctx_Type);
2466 when N_String_Literal
2467 => Resolve_String_Literal (N, Ctx_Type);
2469 when N_Subprogram_Info
2470 => Resolve_Subprogram_Info (N, Ctx_Type);
2472 when N_Type_Conversion
2473 => Resolve_Type_Conversion (N, Ctx_Type);
2475 when N_Unchecked_Expression =>
2476 Resolve_Unchecked_Expression (N, Ctx_Type);
2478 when N_Unchecked_Type_Conversion =>
2479 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2483 -- If the subexpression was replaced by a non-subexpression, then
2484 -- all we do is to expand it. The only legitimate case we know of
2485 -- is converting procedure call statement to entry call statements,
2486 -- but there may be others, so we are making this test general.
2488 if Nkind (N) not in N_Subexpr then
2489 Debug_A_Exit ("resolving ", N, " (done)");
2494 -- The expression is definitely NOT overloaded at this point, so
2495 -- we reset the Is_Overloaded flag to avoid any confusion when
2496 -- reanalyzing the node.
2498 Set_Is_Overloaded (N, False);
2500 -- Freeze expression type, entity if it is a name, and designated
2501 -- type if it is an allocator (RM 13.14(10,11,13)).
2503 -- Now that the resolution of the type of the node is complete,
2504 -- and we did not detect an error, we can expand this node. We
2505 -- skip the expand call if we are in a default expression, see
2506 -- section "Handling of Default Expressions" in Sem spec.
2508 Debug_A_Exit ("resolving ", N, " (done)");
2510 -- We unconditionally freeze the expression, even if we are in
2511 -- default expression mode (the Freeze_Expression routine tests
2512 -- this flag and only freezes static types if it is set).
2514 Freeze_Expression (N);
2516 -- Now we can do the expansion
2526 -- Version with check(s) suppressed
2528 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2530 if Suppress = All_Checks then
2532 Svg : constant Suppress_Array := Scope_Suppress;
2534 Scope_Suppress := (others => True);
2536 Scope_Suppress := Svg;
2541 Svg : constant Boolean := Scope_Suppress (Suppress);
2543 Scope_Suppress (Suppress) := True;
2545 Scope_Suppress (Suppress) := Svg;
2554 -- Version with implicit type
2556 procedure Resolve (N : Node_Id) is
2558 Resolve (N, Etype (N));
2561 ---------------------
2562 -- Resolve_Actuals --
2563 ---------------------
2565 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2566 Loc : constant Source_Ptr := Sloc (N);
2571 Prev : Node_Id := Empty;
2573 procedure Check_Prefixed_Call;
2574 -- If the original node is an overloaded call in prefix notation,
2575 -- insert an 'Access or a dereference as needed over the first actual.
2576 -- Try_Object_Operation has already verified that there is a valid
2577 -- interpretation, but the form of the actual can only be determined
2578 -- once the primitive operation is identified.
2580 procedure Insert_Default;
2581 -- If the actual is missing in a call, insert in the actuals list
2582 -- an instance of the default expression. The insertion is always
2583 -- a named association.
2585 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2586 -- Check whether T1 and T2, or their full views, are derived from a
2587 -- common type. Used to enforce the restrictions on array conversions
2590 -------------------------
2591 -- Check_Prefixed_Call --
2592 -------------------------
2594 procedure Check_Prefixed_Call is
2595 Act : constant Node_Id := First_Actual (N);
2596 A_Type : constant Entity_Id := Etype (Act);
2597 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2598 Orig : constant Node_Id := Original_Node (N);
2602 -- Check whether the call is a prefixed call, with or without
2603 -- additional actuals.
2605 if Nkind (Orig) = N_Selected_Component
2607 (Nkind (Orig) = N_Indexed_Component
2608 and then Nkind (Prefix (Orig)) = N_Selected_Component
2609 and then Is_Entity_Name (Prefix (Prefix (Orig)))
2610 and then Is_Entity_Name (Act)
2611 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
2613 if Is_Access_Type (A_Type)
2614 and then not Is_Access_Type (F_Type)
2616 -- Introduce dereference on object in prefix
2619 Make_Explicit_Dereference (Sloc (Act),
2620 Prefix => Relocate_Node (Act));
2621 Rewrite (Act, New_A);
2624 elsif Is_Access_Type (F_Type)
2625 and then not Is_Access_Type (A_Type)
2627 -- Introduce an implicit 'Access in prefix
2629 if not Is_Aliased_View (Act) then
2631 ("object in prefixed call to& must be aliased"
2632 & " (RM-2005 4.3.1 (13))",
2637 Make_Attribute_Reference (Loc,
2638 Attribute_Name => Name_Access,
2639 Prefix => Relocate_Node (Act)));
2644 end Check_Prefixed_Call;
2646 --------------------
2647 -- Insert_Default --
2648 --------------------
2650 procedure Insert_Default is
2655 -- Missing argument in call, nothing to insert
2657 if No (Default_Value (F)) then
2661 -- Note that we do a full New_Copy_Tree, so that any associated
2662 -- Itypes are properly copied. This may not be needed any more,
2663 -- but it does no harm as a safety measure! Defaults of a generic
2664 -- formal may be out of bounds of the corresponding actual (see
2665 -- cc1311b) and an additional check may be required.
2670 New_Scope => Current_Scope,
2673 if Is_Concurrent_Type (Scope (Nam))
2674 and then Has_Discriminants (Scope (Nam))
2676 Replace_Actual_Discriminants (N, Actval);
2679 if Is_Overloadable (Nam)
2680 and then Present (Alias (Nam))
2682 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
2683 and then not Is_Tagged_Type (Etype (F))
2685 -- If default is a real literal, do not introduce a
2686 -- conversion whose effect may depend on the run-time
2687 -- size of universal real.
2689 if Nkind (Actval) = N_Real_Literal then
2690 Set_Etype (Actval, Base_Type (Etype (F)));
2692 Actval := Unchecked_Convert_To (Etype (F), Actval);
2696 if Is_Scalar_Type (Etype (F)) then
2697 Enable_Range_Check (Actval);
2700 Set_Parent (Actval, N);
2702 -- Resolve aggregates with their base type, to avoid scope
2703 -- anomalies: the subtype was first built in the suprogram
2704 -- declaration, and the current call may be nested.
2706 if Nkind (Actval) = N_Aggregate
2707 and then Has_Discriminants (Etype (Actval))
2709 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2711 Analyze_And_Resolve (Actval, Etype (Actval));
2715 Set_Parent (Actval, N);
2717 -- See note above concerning aggregates
2719 if Nkind (Actval) = N_Aggregate
2720 and then Has_Discriminants (Etype (Actval))
2722 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2724 -- Resolve entities with their own type, which may differ
2725 -- from the type of a reference in a generic context (the
2726 -- view swapping mechanism did not anticipate the re-analysis
2727 -- of default values in calls).
2729 elsif Is_Entity_Name (Actval) then
2730 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
2733 Analyze_And_Resolve (Actval, Etype (Actval));
2737 -- If default is a tag indeterminate function call, propagate
2738 -- tag to obtain proper dispatching.
2740 if Is_Controlling_Formal (F)
2741 and then Nkind (Default_Value (F)) = N_Function_Call
2743 Set_Is_Controlling_Actual (Actval);
2748 -- If the default expression raises constraint error, then just
2749 -- silently replace it with an N_Raise_Constraint_Error node,
2750 -- since we already gave the warning on the subprogram spec.
2752 if Raises_Constraint_Error (Actval) then
2754 Make_Raise_Constraint_Error (Loc,
2755 Reason => CE_Range_Check_Failed));
2756 Set_Raises_Constraint_Error (Actval);
2757 Set_Etype (Actval, Etype (F));
2761 Make_Parameter_Association (Loc,
2762 Explicit_Actual_Parameter => Actval,
2763 Selector_Name => Make_Identifier (Loc, Chars (F)));
2765 -- Case of insertion is first named actual
2767 if No (Prev) or else
2768 Nkind (Parent (Prev)) /= N_Parameter_Association
2770 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
2771 Set_First_Named_Actual (N, Actval);
2774 if No (Parameter_Associations (N)) then
2775 Set_Parameter_Associations (N, New_List (Assoc));
2777 Append (Assoc, Parameter_Associations (N));
2781 Insert_After (Prev, Assoc);
2784 -- Case of insertion is not first named actual
2787 Set_Next_Named_Actual
2788 (Assoc, Next_Named_Actual (Parent (Prev)));
2789 Set_Next_Named_Actual (Parent (Prev), Actval);
2790 Append (Assoc, Parameter_Associations (N));
2793 Mark_Rewrite_Insertion (Assoc);
2794 Mark_Rewrite_Insertion (Actval);
2803 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
2804 FT1 : Entity_Id := T1;
2805 FT2 : Entity_Id := T2;
2808 if Is_Private_Type (T1)
2809 and then Present (Full_View (T1))
2811 FT1 := Full_View (T1);
2814 if Is_Private_Type (T2)
2815 and then Present (Full_View (T2))
2817 FT2 := Full_View (T2);
2820 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
2823 -- Start of processing for Resolve_Actuals
2826 if Present (First_Actual (N)) then
2827 Check_Prefixed_Call;
2830 A := First_Actual (N);
2831 F := First_Formal (Nam);
2832 while Present (F) loop
2833 if No (A) and then Needs_No_Actuals (Nam) then
2836 -- If we have an error in any actual or formal, indicated by
2837 -- a type of Any_Type, then abandon resolution attempt, and
2838 -- set result type to Any_Type.
2840 elsif (Present (A) and then Etype (A) = Any_Type)
2841 or else Etype (F) = Any_Type
2843 Set_Etype (N, Any_Type);
2848 and then (Nkind (Parent (A)) /= N_Parameter_Association
2850 Chars (Selector_Name (Parent (A))) = Chars (F))
2852 -- If the formal is Out or In_Out, do not resolve and expand the
2853 -- conversion, because it is subsequently expanded into explicit
2854 -- temporaries and assignments. However, the object of the
2855 -- conversion can be resolved. An exception is the case of tagged
2856 -- type conversion with a class-wide actual. In that case we want
2857 -- the tag check to occur and no temporary will be needed (no
2858 -- representation change can occur) and the parameter is passed by
2859 -- reference, so we go ahead and resolve the type conversion.
2860 -- Another exception is the case of reference to component or
2861 -- subcomponent of a bit-packed array, in which case we want to
2862 -- defer expansion to the point the in and out assignments are
2865 if Ekind (F) /= E_In_Parameter
2866 and then Nkind (A) = N_Type_Conversion
2867 and then not Is_Class_Wide_Type (Etype (Expression (A)))
2869 if Ekind (F) = E_In_Out_Parameter
2870 and then Is_Array_Type (Etype (F))
2872 if Has_Aliased_Components (Etype (Expression (A)))
2873 /= Has_Aliased_Components (Etype (F))
2875 if Ada_Version < Ada_05 then
2877 ("both component types in a view conversion must be"
2878 & " aliased, or neither", A);
2880 -- Ada 2005: rule is relaxed (see AI-363)
2882 elsif Has_Aliased_Components (Etype (F))
2884 not Has_Aliased_Components (Etype (Expression (A)))
2887 ("view conversion operand must have aliased " &
2890 ("\since target type has aliased components", N);
2893 elsif not Same_Ancestor (Etype (F), Etype (Expression (A)))
2895 (Is_By_Reference_Type (Etype (F))
2896 or else Is_By_Reference_Type (Etype (Expression (A))))
2899 ("view conversion between unrelated by reference " &
2900 "array types not allowed (\'A'I-00246)", A);
2904 if (Conversion_OK (A)
2905 or else Valid_Conversion (A, Etype (A), Expression (A)))
2906 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
2908 Resolve (Expression (A));
2911 -- If the actual is a function call that returns a limited
2912 -- unconstrained object that needs finalization, create a
2913 -- transient scope for it, so that it can receive the proper
2914 -- finalization list.
2916 elsif Nkind (A) = N_Function_Call
2917 and then Is_Limited_Record (Etype (F))
2918 and then not Is_Constrained (Etype (F))
2919 and then Expander_Active
2921 (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
2923 Establish_Transient_Scope (A, False);
2926 if Nkind (A) = N_Type_Conversion
2927 and then Is_Array_Type (Etype (F))
2928 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
2930 (Is_Limited_Type (Etype (F))
2931 or else Is_Limited_Type (Etype (Expression (A))))
2934 ("conversion between unrelated limited array types " &
2935 "not allowed (\A\I-00246)", A);
2937 if Is_Limited_Type (Etype (F)) then
2938 Explain_Limited_Type (Etype (F), A);
2941 if Is_Limited_Type (Etype (Expression (A))) then
2942 Explain_Limited_Type (Etype (Expression (A)), A);
2946 -- (Ada 2005: AI-251): If the actual is an allocator whose
2947 -- directly designated type is a class-wide interface, we build
2948 -- an anonymous access type to use it as the type of the
2949 -- allocator. Later, when the subprogram call is expanded, if
2950 -- the interface has a secondary dispatch table the expander
2951 -- will add a type conversion to force the correct displacement
2954 if Nkind (A) = N_Allocator then
2956 DDT : constant Entity_Id :=
2957 Directly_Designated_Type (Base_Type (Etype (F)));
2958 New_Itype : Entity_Id;
2960 if Is_Class_Wide_Type (DDT)
2961 and then Is_Interface (DDT)
2963 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
2964 Set_Etype (New_Itype, Etype (A));
2965 Init_Size_Align (New_Itype);
2966 Set_Directly_Designated_Type (New_Itype,
2967 Directly_Designated_Type (Etype (A)));
2968 Set_Etype (A, New_Itype);
2971 -- Ada 2005, AI-162:If the actual is an allocator, the
2972 -- innermost enclosing statement is the master of the
2973 -- created object. This needs to be done with expansion
2974 -- enabled only, otherwise the transient scope will not
2975 -- be removed in the expansion of the wrapped construct.
2977 if (Is_Controlled (DDT)
2978 or else Has_Task (DDT))
2979 and then Expander_Active
2981 Establish_Transient_Scope (A, False);
2986 -- (Ada 2005): The call may be to a primitive operation of
2987 -- a tagged synchronized type, declared outside of the type.
2988 -- In this case the controlling actual must be converted to
2989 -- its corresponding record type, which is the formal type.
2991 if Is_Concurrent_Type (Etype (A))
2992 and then Etype (F) = Corresponding_Record_Type (Etype (A))
2995 Unchecked_Convert_To
2996 (Corresponding_Record_Type (Etype (A)), A));
2999 Resolve (A, Etype (F));
3005 -- Perform error checks for IN and IN OUT parameters
3007 if Ekind (F) /= E_Out_Parameter then
3009 -- Check unset reference. For scalar parameters, it is clearly
3010 -- wrong to pass an uninitialized value as either an IN or
3011 -- IN-OUT parameter. For composites, it is also clearly an
3012 -- error to pass a completely uninitialized value as an IN
3013 -- parameter, but the case of IN OUT is trickier. We prefer
3014 -- not to give a warning here. For example, suppose there is
3015 -- a routine that sets some component of a record to False.
3016 -- It is perfectly reasonable to make this IN-OUT and allow
3017 -- either initialized or uninitialized records to be passed
3020 -- For partially initialized composite values, we also avoid
3021 -- warnings, since it is quite likely that we are passing a
3022 -- partially initialized value and only the initialized fields
3023 -- will in fact be read in the subprogram.
3025 if Is_Scalar_Type (A_Typ)
3026 or else (Ekind (F) = E_In_Parameter
3027 and then not Is_Partially_Initialized_Type (A_Typ))
3029 Check_Unset_Reference (A);
3032 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3033 -- actual to a nested call, since this is case of reading an
3034 -- out parameter, which is not allowed.
3036 if Ada_Version = Ada_83
3037 and then Is_Entity_Name (A)
3038 and then Ekind (Entity (A)) = E_Out_Parameter
3040 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3044 if Ekind (F) /= E_In_Parameter
3045 and then not Is_OK_Variable_For_Out_Formal (A)
3047 Error_Msg_NE ("actual for& must be a variable", A, F);
3049 if Is_Entity_Name (A) then
3050 Kill_Checks (Entity (A));
3056 if Etype (A) = Any_Type then
3057 Set_Etype (N, Any_Type);
3061 -- Apply appropriate range checks for in, out, and in-out
3062 -- parameters. Out and in-out parameters also need a separate
3063 -- check, if there is a type conversion, to make sure the return
3064 -- value meets the constraints of the variable before the
3067 -- Gigi looks at the check flag and uses the appropriate types.
3068 -- For now since one flag is used there is an optimization which
3069 -- might not be done in the In Out case since Gigi does not do
3070 -- any analysis. More thought required about this ???
3072 if Ekind (F) = E_In_Parameter
3073 or else Ekind (F) = E_In_Out_Parameter
3075 if Is_Scalar_Type (Etype (A)) then
3076 Apply_Scalar_Range_Check (A, F_Typ);
3078 elsif Is_Array_Type (Etype (A)) then
3079 Apply_Length_Check (A, F_Typ);
3081 elsif Is_Record_Type (F_Typ)
3082 and then Has_Discriminants (F_Typ)
3083 and then Is_Constrained (F_Typ)
3084 and then (not Is_Derived_Type (F_Typ)
3085 or else Comes_From_Source (Nam))
3087 Apply_Discriminant_Check (A, F_Typ);
3089 elsif Is_Access_Type (F_Typ)
3090 and then Is_Array_Type (Designated_Type (F_Typ))
3091 and then Is_Constrained (Designated_Type (F_Typ))
3093 Apply_Length_Check (A, F_Typ);
3095 elsif Is_Access_Type (F_Typ)
3096 and then Has_Discriminants (Designated_Type (F_Typ))
3097 and then Is_Constrained (Designated_Type (F_Typ))
3099 Apply_Discriminant_Check (A, F_Typ);
3102 Apply_Range_Check (A, F_Typ);
3105 -- Ada 2005 (AI-231)
3107 if Ada_Version >= Ada_05
3108 and then Is_Access_Type (F_Typ)
3109 and then Can_Never_Be_Null (F_Typ)
3110 and then Known_Null (A)
3112 Apply_Compile_Time_Constraint_Error
3114 Msg => "(Ada 2005) null not allowed in "
3115 & "null-excluding formal?",
3116 Reason => CE_Null_Not_Allowed);
3120 if Ekind (F) = E_Out_Parameter
3121 or else Ekind (F) = E_In_Out_Parameter
3123 if Nkind (A) = N_Type_Conversion then
3124 if Is_Scalar_Type (A_Typ) then
3125 Apply_Scalar_Range_Check
3126 (Expression (A), Etype (Expression (A)), A_Typ);
3129 (Expression (A), Etype (Expression (A)), A_Typ);
3133 if Is_Scalar_Type (F_Typ) then
3134 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3136 elsif Is_Array_Type (F_Typ)
3137 and then Ekind (F) = E_Out_Parameter
3139 Apply_Length_Check (A, F_Typ);
3142 Apply_Range_Check (A, A_Typ, F_Typ);
3147 -- An actual associated with an access parameter is implicitly
3148 -- converted to the anonymous access type of the formal and
3149 -- must satisfy the legality checks for access conversions.
3151 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3152 if not Valid_Conversion (A, F_Typ, A) then
3154 ("invalid implicit conversion for access parameter", A);
3158 -- Check bad case of atomic/volatile argument (RM C.6(12))
3160 if Is_By_Reference_Type (Etype (F))
3161 and then Comes_From_Source (N)
3163 if Is_Atomic_Object (A)
3164 and then not Is_Atomic (Etype (F))
3167 ("cannot pass atomic argument to non-atomic formal",
3170 elsif Is_Volatile_Object (A)
3171 and then not Is_Volatile (Etype (F))
3174 ("cannot pass volatile argument to non-volatile formal",
3179 -- Check that subprograms don't have improper controlling
3180 -- arguments (RM 3.9.2 (9))
3182 -- A primitive operation may have an access parameter of an
3183 -- incomplete tagged type, but a dispatching call is illegal
3184 -- if the type is still incomplete.
3186 if Is_Controlling_Formal (F) then
3187 Set_Is_Controlling_Actual (A);
3189 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3191 Desig : constant Entity_Id := Designated_Type (Etype (F));
3193 if Ekind (Desig) = E_Incomplete_Type
3194 and then No (Full_View (Desig))
3195 and then No (Non_Limited_View (Desig))
3198 ("premature use of incomplete type& " &
3199 "in dispatching call", A, Desig);
3204 elsif Nkind (A) = N_Explicit_Dereference then
3205 Validate_Remote_Access_To_Class_Wide_Type (A);
3208 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3209 and then not Is_Class_Wide_Type (F_Typ)
3210 and then not Is_Controlling_Formal (F)
3212 Error_Msg_N ("class-wide argument not allowed here!", A);
3214 if Is_Subprogram (Nam)
3215 and then Comes_From_Source (Nam)
3217 Error_Msg_Node_2 := F_Typ;
3219 ("& is not a dispatching operation of &!", A, Nam);
3222 elsif Is_Access_Type (A_Typ)
3223 and then Is_Access_Type (F_Typ)
3224 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3225 and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3226 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3227 or else (Nkind (A) = N_Attribute_Reference
3229 Is_Class_Wide_Type (Etype (Prefix (A)))))
3230 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3231 and then not Is_Controlling_Formal (F)
3234 ("access to class-wide argument not allowed here!", A);
3236 if Is_Subprogram (Nam)
3237 and then Comes_From_Source (Nam)
3239 Error_Msg_Node_2 := Designated_Type (F_Typ);
3241 ("& is not a dispatching operation of &!", A, Nam);
3247 -- If it is a named association, treat the selector_name as
3248 -- a proper identifier, and mark the corresponding entity.
3250 if Nkind (Parent (A)) = N_Parameter_Association then
3251 Set_Entity (Selector_Name (Parent (A)), F);
3252 Generate_Reference (F, Selector_Name (Parent (A)));
3253 Set_Etype (Selector_Name (Parent (A)), F_Typ);
3254 Generate_Reference (F_Typ, N, ' ');
3259 if Ekind (F) /= E_Out_Parameter then
3260 Check_Unset_Reference (A);
3265 -- Case where actual is not present
3273 end Resolve_Actuals;
3275 -----------------------
3276 -- Resolve_Allocator --
3277 -----------------------
3279 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
3280 E : constant Node_Id := Expression (N);
3282 Discrim : Entity_Id;
3285 Assoc : Node_Id := Empty;
3288 procedure Check_Allocator_Discrim_Accessibility
3289 (Disc_Exp : Node_Id;
3290 Alloc_Typ : Entity_Id);
3291 -- Check that accessibility level associated with an access discriminant
3292 -- initialized in an allocator by the expression Disc_Exp is not deeper
3293 -- than the level of the allocator type Alloc_Typ. An error message is
3294 -- issued if this condition is violated. Specialized checks are done for
3295 -- the cases of a constraint expression which is an access attribute or
3296 -- an access discriminant.
3298 function In_Dispatching_Context return Boolean;
3299 -- If the allocator is an actual in a call, it is allowed to be class-
3300 -- wide when the context is not because it is a controlling actual.
3302 procedure Propagate_Coextensions (Root : Node_Id);
3303 -- Propagate all nested coextensions which are located one nesting
3304 -- level down the tree to the node Root. Example:
3307 -- Level_1_Coextension
3308 -- Level_2_Coextension
3310 -- The algorithm is paired with delay actions done by the Expander. In
3311 -- the above example, assume all coextensions are controlled types.
3312 -- The cycle of analysis, resolution and expansion will yield:
3314 -- 1) Analyze Top_Record
3315 -- 2) Analyze Level_1_Coextension
3316 -- 3) Analyze Level_2_Coextension
3317 -- 4) Resolve Level_2_Coextnesion. The allocator is marked as a
3319 -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is
3320 -- generated to capture the allocated object. Temp_1 is attached
3321 -- to the coextension chain of Level_2_Coextension.
3322 -- 6) Resolve Level_1_Coextension. The allocator is marked as a
3323 -- coextension. A forward tree traversal is performed which finds
3324 -- Level_2_Coextension's list and copies its contents into its
3326 -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is
3327 -- generated to capture the allocated object. Temp_2 is attached
3328 -- to the coextension chain of Level_1_Coextension. Currently, the
3329 -- contents of the list are [Temp_2, Temp_1].
3330 -- 8) Resolve Top_Record. A forward tree traversal is performed which
3331 -- finds Level_1_Coextension's list and copies its contents into
3333 -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and
3334 -- Temp_2 and attach them to Top_Record's finalization list.
3336 -------------------------------------------
3337 -- Check_Allocator_Discrim_Accessibility --
3338 -------------------------------------------
3340 procedure Check_Allocator_Discrim_Accessibility
3341 (Disc_Exp : Node_Id;
3342 Alloc_Typ : Entity_Id)
3345 if Type_Access_Level (Etype (Disc_Exp)) >
3346 Type_Access_Level (Alloc_Typ)
3349 ("operand type has deeper level than allocator type", Disc_Exp);
3351 -- When the expression is an Access attribute the level of the prefix
3352 -- object must not be deeper than that of the allocator's type.
3354 elsif Nkind (Disc_Exp) = N_Attribute_Reference
3355 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
3357 and then Object_Access_Level (Prefix (Disc_Exp))
3358 > Type_Access_Level (Alloc_Typ)
3361 ("prefix of attribute has deeper level than allocator type",
3364 -- When the expression is an access discriminant the check is against
3365 -- the level of the prefix object.
3367 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
3368 and then Nkind (Disc_Exp) = N_Selected_Component
3369 and then Object_Access_Level (Prefix (Disc_Exp))
3370 > Type_Access_Level (Alloc_Typ)
3373 ("access discriminant has deeper level than allocator type",
3376 -- All other cases are legal
3381 end Check_Allocator_Discrim_Accessibility;
3383 ----------------------------
3384 -- In_Dispatching_Context --
3385 ----------------------------
3387 function In_Dispatching_Context return Boolean is
3388 Par : constant Node_Id := Parent (N);
3390 return (Nkind (Par) = N_Function_Call
3391 or else Nkind (Par) = N_Procedure_Call_Statement)
3392 and then Is_Entity_Name (Name (Par))
3393 and then Is_Dispatching_Operation (Entity (Name (Par)));
3394 end In_Dispatching_Context;
3396 ----------------------------
3397 -- Propagate_Coextensions --
3398 ----------------------------
3400 procedure Propagate_Coextensions (Root : Node_Id) is
3402 procedure Copy_List (From : Elist_Id; To : Elist_Id);
3403 -- Copy the contents of list From into list To, preserving the
3404 -- order of elements.
3406 function Process_Allocator (Nod : Node_Id) return Traverse_Result;
3407 -- Recognize an allocator or a rewritten allocator node and add it
3408 -- allong with its nested coextensions to the list of Root.
3414 procedure Copy_List (From : Elist_Id; To : Elist_Id) is
3415 From_Elmt : Elmt_Id;
3417 From_Elmt := First_Elmt (From);
3418 while Present (From_Elmt) loop
3419 Append_Elmt (Node (From_Elmt), To);
3420 Next_Elmt (From_Elmt);
3424 -----------------------
3425 -- Process_Allocator --
3426 -----------------------
3428 function Process_Allocator (Nod : Node_Id) return Traverse_Result is
3429 Orig_Nod : Node_Id := Nod;
3432 -- This is a possible rewritten subtype indication allocator. Any
3433 -- nested coextensions will appear as discriminant constraints.
3435 if Nkind (Nod) = N_Identifier
3436 and then Present (Original_Node (Nod))
3437 and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
3441 Discr_Elmt : Elmt_Id;
3444 if Is_Record_Type (Entity (Nod)) then
3446 First_Elmt (Discriminant_Constraint (Entity (Nod)));
3447 while Present (Discr_Elmt) loop
3448 Discr := Node (Discr_Elmt);
3450 if Nkind (Discr) = N_Identifier
3451 and then Present (Original_Node (Discr))
3452 and then Nkind (Original_Node (Discr)) = N_Allocator
3453 and then Present (Coextensions (
3454 Original_Node (Discr)))
3456 if No (Coextensions (Root)) then
3457 Set_Coextensions (Root, New_Elmt_List);
3461 (From => Coextensions (Original_Node (Discr)),
3462 To => Coextensions (Root));
3465 Next_Elmt (Discr_Elmt);
3468 -- There is no need to continue the traversal of this
3469 -- subtree since all the information has already been
3476 -- Case of either a stand alone allocator or a rewritten allocator
3477 -- with an aggregate.
3480 if Present (Original_Node (Nod)) then
3481 Orig_Nod := Original_Node (Nod);
3484 if Nkind (Orig_Nod) = N_Allocator then
3486 -- Propagate the list of nested coextensions to the Root
3487 -- allocator. This is done through list copy since a single
3488 -- allocator may have multiple coextensions. Do not touch
3489 -- coextensions roots.
3491 if not Is_Coextension_Root (Orig_Nod)
3492 and then Present (Coextensions (Orig_Nod))
3494 if No (Coextensions (Root)) then
3495 Set_Coextensions (Root, New_Elmt_List);
3499 (From => Coextensions (Orig_Nod),
3500 To => Coextensions (Root));
3503 -- There is no need to continue the traversal of this
3504 -- subtree since all the information has already been
3511 -- Keep on traversing, looking for the next allocator
3514 end Process_Allocator;
3516 procedure Process_Allocators is
3517 new Traverse_Proc (Process_Allocator);
3519 -- Start of processing for Propagate_Coextensions
3522 Process_Allocators (Expression (Root));
3523 end Propagate_Coextensions;
3525 -- Start of processing for Resolve_Allocator
3528 -- Replace general access with specific type
3530 if Ekind (Etype (N)) = E_Allocator_Type then
3531 Set_Etype (N, Base_Type (Typ));
3534 if Is_Abstract_Type (Typ) then
3535 Error_Msg_N ("type of allocator cannot be abstract", N);
3538 -- For qualified expression, resolve the expression using the
3539 -- given subtype (nothing to do for type mark, subtype indication)
3541 if Nkind (E) = N_Qualified_Expression then
3542 if Is_Class_Wide_Type (Etype (E))
3543 and then not Is_Class_Wide_Type (Designated_Type (Typ))
3544 and then not In_Dispatching_Context
3547 ("class-wide allocator not allowed for this access type", N);
3550 Resolve (Expression (E), Etype (E));
3551 Check_Unset_Reference (Expression (E));
3553 -- A qualified expression requires an exact match of the type,
3554 -- class-wide matching is not allowed.
3556 if (Is_Class_Wide_Type (Etype (Expression (E)))
3557 or else Is_Class_Wide_Type (Etype (E)))
3558 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
3560 Wrong_Type (Expression (E), Etype (E));
3563 -- A special accessibility check is needed for allocators that
3564 -- constrain access discriminants. The level of the type of the
3565 -- expression used to constrain an access discriminant cannot be
3566 -- deeper than the type of the allocator (in constrast to access
3567 -- parameters, where the level of the actual can be arbitrary).
3569 -- We can't use Valid_Conversion to perform this check because
3570 -- in general the type of the allocator is unrelated to the type
3571 -- of the access discriminant.
3573 if Ekind (Typ) /= E_Anonymous_Access_Type
3574 or else Is_Local_Anonymous_Access (Typ)
3576 Subtyp := Entity (Subtype_Mark (E));
3578 Aggr := Original_Node (Expression (E));
3580 if Has_Discriminants (Subtyp)
3582 (Nkind (Aggr) = N_Aggregate
3584 Nkind (Aggr) = N_Extension_Aggregate)
3586 Discrim := First_Discriminant (Base_Type (Subtyp));
3588 -- Get the first component expression of the aggregate
3590 if Present (Expressions (Aggr)) then
3591 Disc_Exp := First (Expressions (Aggr));
3593 elsif Present (Component_Associations (Aggr)) then
3594 Assoc := First (Component_Associations (Aggr));
3596 if Present (Assoc) then
3597 Disc_Exp := Expression (Assoc);
3606 while Present (Discrim) and then Present (Disc_Exp) loop
3607 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
3608 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
3611 Next_Discriminant (Discrim);
3613 if Present (Discrim) then
3614 if Present (Assoc) then
3616 Disc_Exp := Expression (Assoc);
3618 elsif Present (Next (Disc_Exp)) then
3622 Assoc := First (Component_Associations (Aggr));
3624 if Present (Assoc) then
3625 Disc_Exp := Expression (Assoc);
3635 -- For a subtype mark or subtype indication, freeze the subtype
3638 Freeze_Expression (E);
3640 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
3642 ("initialization required for access-to-constant allocator", N);
3645 -- A special accessibility check is needed for allocators that
3646 -- constrain access discriminants. The level of the type of the
3647 -- expression used to constrain an access discriminant cannot be
3648 -- deeper than the type of the allocator (in constrast to access
3649 -- parameters, where the level of the actual can be arbitrary).
3650 -- We can't use Valid_Conversion to perform this check because
3651 -- in general the type of the allocator is unrelated to the type
3652 -- of the access discriminant.
3654 if Nkind (Original_Node (E)) = N_Subtype_Indication
3655 and then (Ekind (Typ) /= E_Anonymous_Access_Type
3656 or else Is_Local_Anonymous_Access (Typ))
3658 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
3660 if Has_Discriminants (Subtyp) then
3661 Discrim := First_Discriminant (Base_Type (Subtyp));
3662 Constr := First (Constraints (Constraint (Original_Node (E))));
3663 while Present (Discrim) and then Present (Constr) loop
3664 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
3665 if Nkind (Constr) = N_Discriminant_Association then
3666 Disc_Exp := Original_Node (Expression (Constr));
3668 Disc_Exp := Original_Node (Constr);
3671 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
3674 Next_Discriminant (Discrim);
3681 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
3682 -- check that the level of the type of the created object is not deeper
3683 -- than the level of the allocator's access type, since extensions can
3684 -- now occur at deeper levels than their ancestor types. This is a
3685 -- static accessibility level check; a run-time check is also needed in
3686 -- the case of an initialized allocator with a class-wide argument (see
3687 -- Expand_Allocator_Expression).
3689 if Ada_Version >= Ada_05
3690 and then Is_Class_Wide_Type (Designated_Type (Typ))
3693 Exp_Typ : Entity_Id;
3696 if Nkind (E) = N_Qualified_Expression then
3697 Exp_Typ := Etype (E);
3698 elsif Nkind (E) = N_Subtype_Indication then
3699 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
3701 Exp_Typ := Entity (E);
3704 if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
3705 if In_Instance_Body then
3706 Error_Msg_N ("?type in allocator has deeper level than" &
3707 " designated class-wide type", E);
3708 Error_Msg_N ("\?Program_Error will be raised at run time",
3711 Make_Raise_Program_Error (Sloc (N),
3712 Reason => PE_Accessibility_Check_Failed));
3715 -- Do not apply Ada 2005 accessibility checks on a class-wide
3716 -- allocator if the type given in the allocator is a formal
3717 -- type. A run-time check will be performed in the instance.
3719 elsif not Is_Generic_Type (Exp_Typ) then
3720 Error_Msg_N ("type in allocator has deeper level than" &
3721 " designated class-wide type", E);
3727 -- Check for allocation from an empty storage pool
3729 if No_Pool_Assigned (Typ) then
3731 Loc : constant Source_Ptr := Sloc (N);
3733 Error_Msg_N ("?allocation from empty storage pool!", N);
3734 Error_Msg_N ("\?Storage_Error will be raised at run time!", N);
3736 Make_Raise_Storage_Error (Loc,
3737 Reason => SE_Empty_Storage_Pool));
3740 -- If the context is an unchecked conversion, as may happen within
3741 -- an inlined subprogram, the allocator is being resolved with its
3742 -- own anonymous type. In that case, if the target type has a specific
3743 -- storage pool, it must be inherited explicitly by the allocator type.
3745 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
3746 and then No (Associated_Storage_Pool (Typ))
3748 Set_Associated_Storage_Pool
3749 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
3752 -- An erroneous allocator may be rewritten as a raise Program_Error
3755 if Nkind (N) = N_Allocator then
3757 -- An anonymous access discriminant is the definition of a
3760 if Ekind (Typ) = E_Anonymous_Access_Type
3761 and then Nkind (Associated_Node_For_Itype (Typ)) =
3762 N_Discriminant_Specification
3764 -- Avoid marking an allocator as a dynamic coextension if it is
3765 -- within a static construct.
3767 if not Is_Static_Coextension (N) then
3768 Set_Is_Dynamic_Coextension (N);
3771 -- Cleanup for potential static coextensions
3774 Set_Is_Dynamic_Coextension (N, False);
3775 Set_Is_Static_Coextension (N, False);
3778 -- There is no need to propagate any nested coextensions if they
3779 -- are marked as static since they will be rewritten on the spot.
3781 if not Is_Static_Coextension (N) then
3782 Propagate_Coextensions (N);
3785 end Resolve_Allocator;
3787 ---------------------------
3788 -- Resolve_Arithmetic_Op --
3789 ---------------------------
3791 -- Used for resolving all arithmetic operators except exponentiation
3793 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
3794 L : constant Node_Id := Left_Opnd (N);
3795 R : constant Node_Id := Right_Opnd (N);
3796 TL : constant Entity_Id := Base_Type (Etype (L));
3797 TR : constant Entity_Id := Base_Type (Etype (R));
3801 B_Typ : constant Entity_Id := Base_Type (Typ);
3802 -- We do the resolution using the base type, because intermediate values
3803 -- in expressions always are of the base type, not a subtype of it.
3805 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
3806 -- Returns True if N is in a context that expects "any real type"
3808 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
3809 -- Return True iff given type is Integer or universal real/integer
3811 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
3812 -- Choose type of integer literal in fixed-point operation to conform
3813 -- to available fixed-point type. T is the type of the other operand,
3814 -- which is needed to determine the expected type of N.
3816 procedure Set_Operand_Type (N : Node_Id);
3817 -- Set operand type to T if universal
3819 -------------------------------
3820 -- Expected_Type_Is_Any_Real --
3821 -------------------------------
3823 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
3825 -- N is the expression after "delta" in a fixed_point_definition;
3828 return Nkind (Parent (N)) = N_Ordinary_Fixed_Point_Definition
3829 or else Nkind (Parent (N)) = N_Decimal_Fixed_Point_Definition
3831 -- N is one of the bounds in a real_range_specification;
3834 or else Nkind (Parent (N)) = N_Real_Range_Specification
3836 -- N is the expression of a delta_constraint;
3839 or else Nkind (Parent (N)) = N_Delta_Constraint;
3840 end Expected_Type_Is_Any_Real;
3842 -----------------------------
3843 -- Is_Integer_Or_Universal --
3844 -----------------------------
3846 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
3848 Index : Interp_Index;
3852 if not Is_Overloaded (N) then
3854 return Base_Type (T) = Base_Type (Standard_Integer)
3855 or else T = Universal_Integer
3856 or else T = Universal_Real;
3858 Get_First_Interp (N, Index, It);
3859 while Present (It.Typ) loop
3860 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
3861 or else It.Typ = Universal_Integer
3862 or else It.Typ = Universal_Real
3867 Get_Next_Interp (Index, It);
3872 end Is_Integer_Or_Universal;
3874 ----------------------------
3875 -- Set_Mixed_Mode_Operand --
3876 ----------------------------
3878 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
3879 Index : Interp_Index;
3883 if Universal_Interpretation (N) = Universal_Integer then
3885 -- A universal integer literal is resolved as standard integer
3886 -- except in the case of a fixed-point result, where we leave it
3887 -- as universal (to be handled by Exp_Fixd later on)
3889 if Is_Fixed_Point_Type (T) then
3890 Resolve (N, Universal_Integer);
3892 Resolve (N, Standard_Integer);
3895 elsif Universal_Interpretation (N) = Universal_Real
3896 and then (T = Base_Type (Standard_Integer)
3897 or else T = Universal_Integer
3898 or else T = Universal_Real)
3900 -- A universal real can appear in a fixed-type context. We resolve
3901 -- the literal with that context, even though this might raise an
3902 -- exception prematurely (the other operand may be zero).
3906 elsif Etype (N) = Base_Type (Standard_Integer)
3907 and then T = Universal_Real
3908 and then Is_Overloaded (N)
3910 -- Integer arg in mixed-mode operation. Resolve with universal
3911 -- type, in case preference rule must be applied.
3913 Resolve (N, Universal_Integer);
3916 and then B_Typ /= Universal_Fixed
3918 -- Not a mixed-mode operation, resolve with context
3922 elsif Etype (N) = Any_Fixed then
3924 -- N may itself be a mixed-mode operation, so use context type
3928 elsif Is_Fixed_Point_Type (T)
3929 and then B_Typ = Universal_Fixed
3930 and then Is_Overloaded (N)
3932 -- Must be (fixed * fixed) operation, operand must have one
3933 -- compatible interpretation.
3935 Resolve (N, Any_Fixed);
3937 elsif Is_Fixed_Point_Type (B_Typ)
3938 and then (T = Universal_Real
3939 or else Is_Fixed_Point_Type (T))
3940 and then Is_Overloaded (N)
3942 -- C * F(X) in a fixed context, where C is a real literal or a
3943 -- fixed-point expression. F must have either a fixed type
3944 -- interpretation or an integer interpretation, but not both.
3946 Get_First_Interp (N, Index, It);
3947 while Present (It.Typ) loop
3948 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
3950 if Analyzed (N) then
3951 Error_Msg_N ("ambiguous operand in fixed operation", N);
3953 Resolve (N, Standard_Integer);
3956 elsif Is_Fixed_Point_Type (It.Typ) then
3958 if Analyzed (N) then
3959 Error_Msg_N ("ambiguous operand in fixed operation", N);
3961 Resolve (N, It.Typ);
3965 Get_Next_Interp (Index, It);
3968 -- Reanalyze the literal with the fixed type of the context. If
3969 -- context is Universal_Fixed, we are within a conversion, leave
3970 -- the literal as a universal real because there is no usable
3971 -- fixed type, and the target of the conversion plays no role in
3985 if B_Typ = Universal_Fixed
3986 and then Nkind (Op2) = N_Real_Literal
3988 T2 := Universal_Real;
3993 Set_Analyzed (Op2, False);
4000 end Set_Mixed_Mode_Operand;
4002 ----------------------
4003 -- Set_Operand_Type --
4004 ----------------------
4006 procedure Set_Operand_Type (N : Node_Id) is
4008 if Etype (N) = Universal_Integer
4009 or else Etype (N) = Universal_Real
4013 end Set_Operand_Type;
4015 -- Start of processing for Resolve_Arithmetic_Op
4018 if Comes_From_Source (N)
4019 and then Ekind (Entity (N)) = E_Function
4020 and then Is_Imported (Entity (N))
4021 and then Is_Intrinsic_Subprogram (Entity (N))
4023 Resolve_Intrinsic_Operator (N, Typ);
4026 -- Special-case for mixed-mode universal expressions or fixed point
4027 -- type operation: each argument is resolved separately. The same
4028 -- treatment is required if one of the operands of a fixed point
4029 -- operation is universal real, since in this case we don't do a
4030 -- conversion to a specific fixed-point type (instead the expander
4031 -- takes care of the case).
4033 elsif (B_Typ = Universal_Integer
4034 or else B_Typ = Universal_Real)
4035 and then Present (Universal_Interpretation (L))
4036 and then Present (Universal_Interpretation (R))
4038 Resolve (L, Universal_Interpretation (L));
4039 Resolve (R, Universal_Interpretation (R));
4040 Set_Etype (N, B_Typ);
4042 elsif (B_Typ = Universal_Real
4043 or else Etype (N) = Universal_Fixed
4044 or else (Etype (N) = Any_Fixed
4045 and then Is_Fixed_Point_Type (B_Typ))
4046 or else (Is_Fixed_Point_Type (B_Typ)
4047 and then (Is_Integer_Or_Universal (L)
4049 Is_Integer_Or_Universal (R))))
4050 and then (Nkind (N) = N_Op_Multiply or else
4051 Nkind (N) = N_Op_Divide)
4053 if TL = Universal_Integer or else TR = Universal_Integer then
4054 Check_For_Visible_Operator (N, B_Typ);
4057 -- If context is a fixed type and one operand is integer, the
4058 -- other is resolved with the type of the context.
4060 if Is_Fixed_Point_Type (B_Typ)
4061 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4062 or else TL = Universal_Integer)
4067 elsif Is_Fixed_Point_Type (B_Typ)
4068 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4069 or else TR = Universal_Integer)
4075 Set_Mixed_Mode_Operand (L, TR);
4076 Set_Mixed_Mode_Operand (R, TL);
4079 -- Check the rule in RM05-4.5.5(19.1/2) disallowing the
4080 -- universal_fixed multiplying operators from being used when the
4081 -- expected type is also universal_fixed. Note that B_Typ will be
4082 -- Universal_Fixed in some cases where the expected type is actually
4083 -- Any_Real; Expected_Type_Is_Any_Real takes care of that case.
4085 if Etype (N) = Universal_Fixed
4086 or else Etype (N) = Any_Fixed
4088 if B_Typ = Universal_Fixed
4089 and then not Expected_Type_Is_Any_Real (N)
4090 and then Nkind (Parent (N)) /= N_Type_Conversion
4091 and then Nkind (Parent (N)) /= N_Unchecked_Type_Conversion
4094 ("type cannot be determined from context!", N);
4096 ("\explicit conversion to result type required", N);
4098 Set_Etype (L, Any_Type);
4099 Set_Etype (R, Any_Type);
4102 if Ada_Version = Ada_83
4103 and then Etype (N) = Universal_Fixed
4104 and then Nkind (Parent (N)) /= N_Type_Conversion
4105 and then Nkind (Parent (N)) /= N_Unchecked_Type_Conversion
4108 ("(Ada 83) fixed-point operation " &
4109 "needs explicit conversion",
4113 -- The expected type is "any real type" in contexts like
4114 -- type T is delta <universal_fixed-expression> ...
4115 -- in which case we need to set the type to Universal_Real
4116 -- so that static expression evaluation will work properly.
4118 if Expected_Type_Is_Any_Real (N) then
4119 Set_Etype (N, Universal_Real);
4121 Set_Etype (N, B_Typ);
4125 elsif Is_Fixed_Point_Type (B_Typ)
4126 and then (Is_Integer_Or_Universal (L)
4127 or else Nkind (L) = N_Real_Literal
4128 or else Nkind (R) = N_Real_Literal
4130 Is_Integer_Or_Universal (R))
4132 Set_Etype (N, B_Typ);
4134 elsif Etype (N) = Any_Fixed then
4136 -- If no previous errors, this is only possible if one operand
4137 -- is overloaded and the context is universal. Resolve as such.
4139 Set_Etype (N, B_Typ);
4143 if (TL = Universal_Integer or else TL = Universal_Real)
4144 and then (TR = Universal_Integer or else TR = Universal_Real)
4146 Check_For_Visible_Operator (N, B_Typ);
4149 -- If the context is Universal_Fixed and the operands are also
4150 -- universal fixed, this is an error, unless there is only one
4151 -- applicable fixed_point type (usually duration).
4153 if B_Typ = Universal_Fixed
4154 and then Etype (L) = Universal_Fixed
4156 T := Unique_Fixed_Point_Type (N);
4158 if T = Any_Type then
4171 -- If one of the arguments was resolved to a non-universal type.
4172 -- label the result of the operation itself with the same type.
4173 -- Do the same for the universal argument, if any.
4175 T := Intersect_Types (L, R);
4176 Set_Etype (N, Base_Type (T));
4177 Set_Operand_Type (L);
4178 Set_Operand_Type (R);
4181 Generate_Operator_Reference (N, Typ);
4182 Eval_Arithmetic_Op (N);
4184 -- Set overflow and division checking bit. Much cleverer code needed
4185 -- here eventually and perhaps the Resolve routines should be separated
4186 -- for the various arithmetic operations, since they will need
4187 -- different processing. ???
4189 if Nkind (N) in N_Op then
4190 if not Overflow_Checks_Suppressed (Etype (N)) then
4191 Enable_Overflow_Check (N);
4194 -- Give warning if explicit division by zero
4196 if (Nkind (N) = N_Op_Divide
4197 or else Nkind (N) = N_Op_Rem
4198 or else Nkind (N) = N_Op_Mod)
4199 and then not Division_Checks_Suppressed (Etype (N))
4201 Rop := Right_Opnd (N);
4203 if Compile_Time_Known_Value (Rop)
4204 and then ((Is_Integer_Type (Etype (Rop))
4205 and then Expr_Value (Rop) = Uint_0)
4207 (Is_Real_Type (Etype (Rop))
4208 and then Expr_Value_R (Rop) = Ureal_0))
4210 -- Specialize the warning message according to the operation
4214 Apply_Compile_Time_Constraint_Error
4215 (N, "division by zero?", CE_Divide_By_Zero,
4216 Loc => Sloc (Right_Opnd (N)));
4219 Apply_Compile_Time_Constraint_Error
4220 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4221 Loc => Sloc (Right_Opnd (N)));
4224 Apply_Compile_Time_Constraint_Error
4225 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4226 Loc => Sloc (Right_Opnd (N)));
4228 -- Division by zero can only happen with division, rem,
4229 -- and mod operations.
4232 raise Program_Error;
4235 -- Otherwise just set the flag to check at run time
4238 Activate_Division_Check (N);
4243 Check_Unset_Reference (L);
4244 Check_Unset_Reference (R);
4245 end Resolve_Arithmetic_Op;
4251 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4252 Loc : constant Source_Ptr := Sloc (N);
4253 Subp : constant Node_Id := Name (N);
4262 -- The context imposes a unique interpretation with type Typ on a
4263 -- procedure or function call. Find the entity of the subprogram that
4264 -- yields the expected type, and propagate the corresponding formal
4265 -- constraints on the actuals. The caller has established that an
4266 -- interpretation exists, and emitted an error if not unique.
4268 -- First deal with the case of a call to an access-to-subprogram,
4269 -- dereference made explicit in Analyze_Call.
4271 if Ekind (Etype (Subp)) = E_Subprogram_Type then
4272 if not Is_Overloaded (Subp) then
4273 Nam := Etype (Subp);
4276 -- Find the interpretation whose type (a subprogram type) has a
4277 -- return type that is compatible with the context. Analysis of
4278 -- the node has established that one exists.
4282 Get_First_Interp (Subp, I, It);
4283 while Present (It.Typ) loop
4284 if Covers (Typ, Etype (It.Typ)) then
4289 Get_Next_Interp (I, It);
4293 raise Program_Error;
4297 -- If the prefix is not an entity, then resolve it
4299 if not Is_Entity_Name (Subp) then
4300 Resolve (Subp, Nam);
4303 -- For an indirect call, we always invalidate checks, since we do not
4304 -- know whether the subprogram is local or global. Yes we could do
4305 -- better here, e.g. by knowing that there are no local subprograms,
4306 -- but it does not seem worth the effort. Similarly, we kill all
4307 -- knowledge of current constant values.
4309 Kill_Current_Values;
4311 -- If this is a procedure call which is really an entry call, do
4312 -- the conversion of the procedure call to an entry call. Protected
4313 -- operations use the same circuitry because the name in the call
4314 -- can be an arbitrary expression with special resolution rules.
4316 elsif Nkind (Subp) = N_Selected_Component
4317 or else Nkind (Subp) = N_Indexed_Component
4318 or else (Is_Entity_Name (Subp)
4319 and then Ekind (Entity (Subp)) = E_Entry)
4321 Resolve_Entry_Call (N, Typ);
4322 Check_Elab_Call (N);
4324 -- Kill checks and constant values, as above for indirect case
4325 -- Who knows what happens when another task is activated?
4327 Kill_Current_Values;
4330 -- Normal subprogram call with name established in Resolve
4332 elsif not (Is_Type (Entity (Subp))) then
4333 Nam := Entity (Subp);
4334 Set_Entity_With_Style_Check (Subp, Nam);
4335 Generate_Reference (Nam, Subp);
4337 -- Otherwise we must have the case of an overloaded call
4340 pragma Assert (Is_Overloaded (Subp));
4341 Nam := Empty; -- We know that it will be assigned in loop below
4343 Get_First_Interp (Subp, I, It);
4344 while Present (It.Typ) loop
4345 if Covers (Typ, It.Typ) then
4347 Set_Entity_With_Style_Check (Subp, Nam);
4348 Generate_Reference (Nam, Subp);
4352 Get_Next_Interp (I, It);
4356 -- Check that a call to Current_Task does not occur in an entry body
4358 if Is_RTE (Nam, RE_Current_Task) then
4368 if Nkind (P) = N_Entry_Body
4369 or else (Nkind (P) = N_Subprogram_Body
4370 and then Is_Entry_Barrier_Function (P))
4374 ("?& should not be used in entry body (RM C.7(17))",
4377 ("\Program_Error will be raised at run time?", N, Nam);
4379 Make_Raise_Program_Error (Loc,
4380 Reason => PE_Current_Task_In_Entry_Body));
4381 Set_Etype (N, Rtype);
4388 -- Check that a procedure call does not occur in the context of the
4389 -- entry call statement of a conditional or timed entry call. Note that
4390 -- the case of a call to a subprogram renaming of an entry will also be
4391 -- rejected. The test for N not being an N_Entry_Call_Statement is
4392 -- defensive, covering the possibility that the processing of entry
4393 -- calls might reach this point due to later modifications of the code
4396 if Nkind (Parent (N)) = N_Entry_Call_Alternative
4397 and then Nkind (N) /= N_Entry_Call_Statement
4398 and then Entry_Call_Statement (Parent (N)) = N
4400 if Ada_Version < Ada_05 then
4401 Error_Msg_N ("entry call required in select statement", N);
4403 -- Ada 2005 (AI-345): If a procedure_call_statement is used
4404 -- for a procedure_or_entry_call, the procedure_name or pro-
4405 -- cedure_prefix of the procedure_call_statement shall denote
4406 -- an entry renamed by a procedure, or (a view of) a primitive
4407 -- subprogram of a limited interface whose first parameter is
4408 -- a controlling parameter.
4410 elsif Nkind (N) = N_Procedure_Call_Statement
4411 and then not Is_Renamed_Entry (Nam)
4412 and then not Is_Controlling_Limited_Procedure (Nam)
4415 ("entry call or dispatching primitive of interface required", N);
4419 -- Check that this is not a call to a protected procedure or
4420 -- entry from within a protected function.
4422 if Ekind (Current_Scope) = E_Function
4423 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
4424 and then Ekind (Nam) /= E_Function
4425 and then Scope (Nam) = Scope (Current_Scope)
4427 Error_Msg_N ("within protected function, protected " &
4428 "object is constant", N);
4429 Error_Msg_N ("\cannot call operation that may modify it", N);
4432 -- Freeze the subprogram name if not in default expression. Note that we
4433 -- freeze procedure calls as well as function calls. Procedure calls are
4434 -- not frozen according to the rules (RM 13.14(14)) because it is
4435 -- impossible to have a procedure call to a non-frozen procedure in pure
4436 -- Ada, but in the code that we generate in the expander, this rule
4437 -- needs extending because we can generate procedure calls that need
4440 if Is_Entity_Name (Subp) and then not In_Default_Expression then
4441 Freeze_Expression (Subp);
4444 -- For a predefined operator, the type of the result is the type imposed
4445 -- by context, except for a predefined operation on universal fixed.
4446 -- Otherwise The type of the call is the type returned by the subprogram
4449 if Is_Predefined_Op (Nam) then
4450 if Etype (N) /= Universal_Fixed then
4454 -- If the subprogram returns an array type, and the context requires the
4455 -- component type of that array type, the node is really an indexing of
4456 -- the parameterless call. Resolve as such. A pathological case occurs
4457 -- when the type of the component is an access to the array type. In
4458 -- this case the call is truly ambiguous.
4460 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
4462 ((Is_Array_Type (Etype (Nam))
4463 and then Covers (Typ, Component_Type (Etype (Nam))))
4464 or else (Is_Access_Type (Etype (Nam))
4465 and then Is_Array_Type (Designated_Type (Etype (Nam)))
4468 Component_Type (Designated_Type (Etype (Nam))))))
4471 Index_Node : Node_Id;
4473 Ret_Type : constant Entity_Id := Etype (Nam);
4476 if Is_Access_Type (Ret_Type)
4477 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
4480 ("cannot disambiguate function call and indexing", N);
4482 New_Subp := Relocate_Node (Subp);
4483 Set_Entity (Subp, Nam);
4485 if Component_Type (Ret_Type) /= Any_Type then
4486 if Needs_No_Actuals (Nam) then
4488 -- Indexed call to a parameterless function
4491 Make_Indexed_Component (Loc,
4493 Make_Function_Call (Loc,
4495 Expressions => Parameter_Associations (N));
4497 -- An Ada 2005 prefixed call to a primitive operation
4498 -- whose first parameter is the prefix. This prefix was
4499 -- prepended to the parameter list, which is actually a
4500 -- list of indices. Remove the prefix in order to build
4501 -- the proper indexed component.
4504 Make_Indexed_Component (Loc,
4506 Make_Function_Call (Loc,
4508 Parameter_Associations =>
4510 (Remove_Head (Parameter_Associations (N)))),
4511 Expressions => Parameter_Associations (N));
4514 -- Since we are correcting a node classification error made
4515 -- by the parser, we call Replace rather than Rewrite.
4517 Replace (N, Index_Node);
4518 Set_Etype (Prefix (N), Ret_Type);
4520 Resolve_Indexed_Component (N, Typ);
4521 Check_Elab_Call (Prefix (N));
4529 Set_Etype (N, Etype (Nam));
4532 -- In the case where the call is to an overloaded subprogram, Analyze
4533 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
4534 -- such a case Normalize_Actuals needs to be called once more to order
4535 -- the actuals correctly. Otherwise the call will have the ordering
4536 -- given by the last overloaded subprogram whether this is the correct
4537 -- one being called or not.
4539 if Is_Overloaded (Subp) then
4540 Normalize_Actuals (N, Nam, False, Norm_OK);
4541 pragma Assert (Norm_OK);
4544 -- In any case, call is fully resolved now. Reset Overload flag, to
4545 -- prevent subsequent overload resolution if node is analyzed again
4547 Set_Is_Overloaded (Subp, False);
4548 Set_Is_Overloaded (N, False);
4550 -- If we are calling the current subprogram from immediately within its
4551 -- body, then that is the case where we can sometimes detect cases of
4552 -- infinite recursion statically. Do not try this in case restriction
4553 -- No_Recursion is in effect anyway, and do it only for source calls.
4555 if Comes_From_Source (N) then
4556 Scop := Current_Scope;
4559 and then not Restriction_Active (No_Recursion)
4560 and then Check_Infinite_Recursion (N)
4562 -- Here we detected and flagged an infinite recursion, so we do
4563 -- not need to test the case below for further warnings.
4567 -- If call is to immediately containing subprogram, then check for
4568 -- the case of a possible run-time detectable infinite recursion.
4571 Scope_Loop : while Scop /= Standard_Standard loop
4574 -- Although in general case, recursion is not statically
4575 -- checkable, the case of calling an immediately containing
4576 -- subprogram is easy to catch.
4578 Check_Restriction (No_Recursion, N);
4580 -- If the recursive call is to a parameterless subprogram,
4581 -- then even if we can't statically detect infinite
4582 -- recursion, this is pretty suspicious, and we output a
4583 -- warning. Furthermore, we will try later to detect some
4584 -- cases here at run time by expanding checking code (see
4585 -- Detect_Infinite_Recursion in package Exp_Ch6).
4587 -- If the recursive call is within a handler, do not emit a
4588 -- warning, because this is a common idiom: loop until input
4589 -- is correct, catch illegal input in handler and restart.
4591 if No (First_Formal (Nam))
4592 and then Etype (Nam) = Standard_Void_Type
4593 and then not Error_Posted (N)
4594 and then Nkind (Parent (N)) /= N_Exception_Handler
4596 -- For the case of a procedure call. We give the message
4597 -- only if the call is the first statement in a sequence
4598 -- of statements, or if all previous statements are
4599 -- simple assignments. This is simply a heuristic to
4600 -- decrease false positives, without losing too many good
4601 -- warnings. The idea is that these previous statements
4602 -- may affect global variables the procedure depends on.
4604 if Nkind (N) = N_Procedure_Call_Statement
4605 and then Is_List_Member (N)
4611 while Present (P) loop
4612 if Nkind (P) /= N_Assignment_Statement then
4621 -- Do not give warning if we are in a conditional context
4624 K : constant Node_Kind := Nkind (Parent (N));
4626 if (K = N_Loop_Statement
4627 and then Present (Iteration_Scheme (Parent (N))))
4628 or else K = N_If_Statement
4629 or else K = N_Elsif_Part
4630 or else K = N_Case_Statement_Alternative
4636 -- Here warning is to be issued
4638 Set_Has_Recursive_Call (Nam);
4640 ("?possible infinite recursion!", N);
4642 ("\?Storage_Error may be raised at run time!", N);
4648 Scop := Scope (Scop);
4649 end loop Scope_Loop;
4653 -- If subprogram name is a predefined operator, it was given in
4654 -- functional notation. Replace call node with operator node, so
4655 -- that actuals can be resolved appropriately.
4657 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
4658 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
4661 elsif Present (Alias (Nam))
4662 and then Is_Predefined_Op (Alias (Nam))
4664 Resolve_Actuals (N, Nam);
4665 Make_Call_Into_Operator (N, Typ, Alias (Nam));
4669 -- Create a transient scope if the resulting type requires it
4671 -- There are 4 notable exceptions: in init procs, the transient scope
4672 -- overhead is not needed and even incorrect due to the actual expansion
4673 -- of adjust calls; the second case is enumeration literal pseudo calls;
4674 -- the third case is intrinsic subprograms (Unchecked_Conversion and
4675 -- source information functions) that do not use the secondary stack
4676 -- even though the return type is unconstrained; the fourth case is a
4677 -- call to a build-in-place function, since such functions may allocate
4678 -- their result directly in a target object, and cases where the result
4679 -- does get allocated in the secondary stack are checked for within the
4680 -- specialized Exp_Ch6 procedures for expanding build-in-place calls.
4682 -- If this is an initialization call for a type whose initialization
4683 -- uses the secondary stack, we also need to create a transient scope
4684 -- for it, precisely because we will not do it within the init proc
4687 -- If the subprogram is marked Inlined_Always, then even if it returns
4688 -- an unconstrained type the call does not require use of the secondary
4692 and then Present (First_Rep_Item (Nam))
4693 and then Nkind (First_Rep_Item (Nam)) = N_Pragma
4694 and then Chars (First_Rep_Item (Nam)) = Name_Inline_Always
4698 elsif Expander_Active
4699 and then Is_Type (Etype (Nam))
4700 and then Requires_Transient_Scope (Etype (Nam))
4701 and then not Is_Build_In_Place_Function (Nam)
4702 and then Ekind (Nam) /= E_Enumeration_Literal
4703 and then not Within_Init_Proc
4704 and then not Is_Intrinsic_Subprogram (Nam)
4706 Establish_Transient_Scope (N, Sec_Stack => True);
4708 -- If the call appears within the bounds of a loop, it will
4709 -- be rewritten and reanalyzed, nothing left to do here.
4711 if Nkind (N) /= N_Function_Call then
4715 elsif Is_Init_Proc (Nam)
4716 and then not Within_Init_Proc
4718 Check_Initialization_Call (N, Nam);
4721 -- A protected function cannot be called within the definition of the
4722 -- enclosing protected type.
4724 if Is_Protected_Type (Scope (Nam))
4725 and then In_Open_Scopes (Scope (Nam))
4726 and then not Has_Completion (Scope (Nam))
4729 ("& cannot be called before end of protected definition", N, Nam);
4732 -- Propagate interpretation to actuals, and add default expressions
4735 if Present (First_Formal (Nam)) then
4736 Resolve_Actuals (N, Nam);
4738 -- Overloaded literals are rewritten as function calls, for
4739 -- purpose of resolution. After resolution, we can replace
4740 -- the call with the literal itself.
4742 elsif Ekind (Nam) = E_Enumeration_Literal then
4743 Copy_Node (Subp, N);
4744 Resolve_Entity_Name (N, Typ);
4746 -- Avoid validation, since it is a static function call
4751 -- If the subprogram is not global, then kill all saved values and
4752 -- checks. This is a bit conservative, since in many cases we could do
4753 -- better, but it is not worth the effort. Similarly, we kill constant
4754 -- values. However we do not need to do this for internal entities
4755 -- (unless they are inherited user-defined subprograms), since they
4756 -- are not in the business of molesting local values.
4758 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
4759 -- kill all checks and values for calls to global subprograms. This
4760 -- takes care of the case where an access to a local subprogram is
4761 -- taken, and could be passed directly or indirectly and then called
4762 -- from almost any context.
4764 -- Note: we do not do this step till after resolving the actuals. That
4765 -- way we still take advantage of the current value information while
4766 -- scanning the actuals.
4768 if (not Is_Library_Level_Entity (Nam)
4769 or else Suppress_Value_Tracking_On_Call (Current_Scope))
4770 and then (Comes_From_Source (Nam)
4771 or else (Present (Alias (Nam))
4772 and then Comes_From_Source (Alias (Nam))))
4774 Kill_Current_Values;
4777 -- If the subprogram is a primitive operation, check whether or not
4778 -- it is a correct dispatching call.
4780 if Is_Overloadable (Nam)
4781 and then Is_Dispatching_Operation (Nam)
4783 Check_Dispatching_Call (N);
4785 elsif Ekind (Nam) /= E_Subprogram_Type
4786 and then Is_Abstract_Subprogram (Nam)
4787 and then not In_Instance
4789 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
4792 if Is_Intrinsic_Subprogram (Nam) then
4793 Check_Intrinsic_Call (N);
4797 Check_Elab_Call (N);
4800 -------------------------------
4801 -- Resolve_Character_Literal --
4802 -------------------------------
4804 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
4805 B_Typ : constant Entity_Id := Base_Type (Typ);
4809 -- Verify that the character does belong to the type of the context
4811 Set_Etype (N, B_Typ);
4812 Eval_Character_Literal (N);
4814 -- Wide_Wide_Character literals must always be defined, since the set
4815 -- of wide wide character literals is complete, i.e. if a character
4816 -- literal is accepted by the parser, then it is OK for wide wide
4817 -- character (out of range character literals are rejected).
4819 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
4822 -- Always accept character literal for type Any_Character, which
4823 -- occurs in error situations and in comparisons of literals, both
4824 -- of which should accept all literals.
4826 elsif B_Typ = Any_Character then
4829 -- For Standard.Character or a type derived from it, check that
4830 -- the literal is in range
4832 elsif Root_Type (B_Typ) = Standard_Character then
4833 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
4837 -- For Standard.Wide_Character or a type derived from it, check
4838 -- that the literal is in range
4840 elsif Root_Type (B_Typ) = Standard_Wide_Character then
4841 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
4845 -- For Standard.Wide_Wide_Character or a type derived from it, we
4846 -- know the literal is in range, since the parser checked!
4848 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
4851 -- If the entity is already set, this has already been resolved in
4852 -- a generic context, or comes from expansion. Nothing else to do.
4854 elsif Present (Entity (N)) then
4857 -- Otherwise we have a user defined character type, and we can use
4858 -- the standard visibility mechanisms to locate the referenced entity
4861 C := Current_Entity (N);
4862 while Present (C) loop
4863 if Etype (C) = B_Typ then
4864 Set_Entity_With_Style_Check (N, C);
4865 Generate_Reference (C, N);
4873 -- If we fall through, then the literal does not match any of the
4874 -- entries of the enumeration type. This isn't just a constraint
4875 -- error situation, it is an illegality (see RM 4.2).
4878 ("character not defined for }", N, First_Subtype (B_Typ));
4879 end Resolve_Character_Literal;
4881 ---------------------------
4882 -- Resolve_Comparison_Op --
4883 ---------------------------
4885 -- Context requires a boolean type, and plays no role in resolution.
4886 -- Processing identical to that for equality operators. The result
4887 -- type is the base type, which matters when pathological subtypes of
4888 -- booleans with limited ranges are used.
4890 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
4891 L : constant Node_Id := Left_Opnd (N);
4892 R : constant Node_Id := Right_Opnd (N);
4896 -- If this is an intrinsic operation which is not predefined, use
4897 -- the types of its declared arguments to resolve the possibly
4898 -- overloaded operands. Otherwise the operands are unambiguous and
4899 -- specify the expected type.
4901 if Scope (Entity (N)) /= Standard_Standard then
4902 T := Etype (First_Entity (Entity (N)));
4905 T := Find_Unique_Type (L, R);
4907 if T = Any_Fixed then
4908 T := Unique_Fixed_Point_Type (L);
4912 Set_Etype (N, Base_Type (Typ));
4913 Generate_Reference (T, N, ' ');
4915 if T /= Any_Type then
4917 or else T = Any_Composite
4918 or else T = Any_Character
4920 if T = Any_Character then
4921 Ambiguous_Character (L);
4923 Error_Msg_N ("ambiguous operands for comparison", N);
4926 Set_Etype (N, Any_Type);
4932 Check_Unset_Reference (L);
4933 Check_Unset_Reference (R);
4934 Generate_Operator_Reference (N, T);
4935 Eval_Relational_Op (N);
4938 end Resolve_Comparison_Op;
4940 ------------------------------------
4941 -- Resolve_Conditional_Expression --
4942 ------------------------------------
4944 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
4945 Condition : constant Node_Id := First (Expressions (N));
4946 Then_Expr : constant Node_Id := Next (Condition);
4947 Else_Expr : constant Node_Id := Next (Then_Expr);
4950 Resolve (Condition, Standard_Boolean);
4951 Resolve (Then_Expr, Typ);
4952 Resolve (Else_Expr, Typ);
4955 Eval_Conditional_Expression (N);
4956 end Resolve_Conditional_Expression;
4958 -----------------------------------------
4959 -- Resolve_Discrete_Subtype_Indication --
4960 -----------------------------------------
4962 procedure Resolve_Discrete_Subtype_Indication
4970 Analyze (Subtype_Mark (N));
4971 S := Entity (Subtype_Mark (N));
4973 if Nkind (Constraint (N)) /= N_Range_Constraint then
4974 Error_Msg_N ("expect range constraint for discrete type", N);
4975 Set_Etype (N, Any_Type);
4978 R := Range_Expression (Constraint (N));
4986 if Base_Type (S) /= Base_Type (Typ) then
4988 ("expect subtype of }", N, First_Subtype (Typ));
4990 -- Rewrite the constraint as a range of Typ
4991 -- to allow compilation to proceed further.
4994 Rewrite (Low_Bound (R),
4995 Make_Attribute_Reference (Sloc (Low_Bound (R)),
4996 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
4997 Attribute_Name => Name_First));
4998 Rewrite (High_Bound (R),
4999 Make_Attribute_Reference (Sloc (High_Bound (R)),
5000 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5001 Attribute_Name => Name_First));
5005 Set_Etype (N, Etype (R));
5007 -- Additionally, we must check that the bounds are compatible
5008 -- with the given subtype, which might be different from the
5009 -- type of the context.
5011 Apply_Range_Check (R, S);
5013 -- ??? If the above check statically detects a Constraint_Error
5014 -- it replaces the offending bound(s) of the range R with a
5015 -- Constraint_Error node. When the itype which uses these bounds
5016 -- is frozen the resulting call to Duplicate_Subexpr generates
5017 -- a new temporary for the bounds.
5019 -- Unfortunately there are other itypes that are also made depend
5020 -- on these bounds, so when Duplicate_Subexpr is called they get
5021 -- a forward reference to the newly created temporaries and Gigi
5022 -- aborts on such forward references. This is probably sign of a
5023 -- more fundamental problem somewhere else in either the order of
5024 -- itype freezing or the way certain itypes are constructed.
5026 -- To get around this problem we call Remove_Side_Effects right
5027 -- away if either bounds of R are a Constraint_Error.
5030 L : constant Node_Id := Low_Bound (R);
5031 H : constant Node_Id := High_Bound (R);
5034 if Nkind (L) = N_Raise_Constraint_Error then
5035 Remove_Side_Effects (L);
5038 if Nkind (H) = N_Raise_Constraint_Error then
5039 Remove_Side_Effects (H);
5043 Check_Unset_Reference (Low_Bound (R));
5044 Check_Unset_Reference (High_Bound (R));
5047 end Resolve_Discrete_Subtype_Indication;
5049 -------------------------
5050 -- Resolve_Entity_Name --
5051 -------------------------
5053 -- Used to resolve identifiers and expanded names
5055 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
5056 E : constant Entity_Id := Entity (N);
5059 -- If garbage from errors, set to Any_Type and return
5061 if No (E) and then Total_Errors_Detected /= 0 then
5062 Set_Etype (N, Any_Type);
5066 -- Replace named numbers by corresponding literals. Note that this is
5067 -- the one case where Resolve_Entity_Name must reset the Etype, since
5068 -- it is currently marked as universal.
5070 if Ekind (E) = E_Named_Integer then
5072 Eval_Named_Integer (N);
5074 elsif Ekind (E) = E_Named_Real then
5076 Eval_Named_Real (N);
5078 -- Allow use of subtype only if it is a concurrent type where we are
5079 -- currently inside the body. This will eventually be expanded
5080 -- into a call to Self (for tasks) or _object (for protected
5081 -- objects). Any other use of a subtype is invalid.
5083 elsif Is_Type (E) then
5084 if Is_Concurrent_Type (E)
5085 and then In_Open_Scopes (E)
5090 ("invalid use of subtype mark in expression or call", N);
5093 -- Check discriminant use if entity is discriminant in current scope,
5094 -- i.e. discriminant of record or concurrent type currently being
5095 -- analyzed. Uses in corresponding body are unrestricted.
5097 elsif Ekind (E) = E_Discriminant
5098 and then Scope (E) = Current_Scope
5099 and then not Has_Completion (Current_Scope)
5101 Check_Discriminant_Use (N);
5103 -- A parameterless generic function cannot appear in a context that
5104 -- requires resolution.
5106 elsif Ekind (E) = E_Generic_Function then
5107 Error_Msg_N ("illegal use of generic function", N);
5109 elsif Ekind (E) = E_Out_Parameter
5110 and then Ada_Version = Ada_83
5111 and then (Nkind (Parent (N)) in N_Op
5112 or else (Nkind (Parent (N)) = N_Assignment_Statement
5113 and then N = Expression (Parent (N)))
5114 or else Nkind (Parent (N)) = N_Explicit_Dereference)
5116 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
5118 -- In all other cases, just do the possible static evaluation
5121 -- A deferred constant that appears in an expression must have
5122 -- a completion, unless it has been removed by in-place expansion
5125 if Ekind (E) = E_Constant
5126 and then Comes_From_Source (E)
5127 and then No (Constant_Value (E))
5128 and then Is_Frozen (Etype (E))
5129 and then not In_Default_Expression
5130 and then not Is_Imported (E)
5133 if No_Initialization (Parent (E))
5134 or else (Present (Full_View (E))
5135 and then No_Initialization (Parent (Full_View (E))))
5140 "deferred constant is frozen before completion", N);
5144 Eval_Entity_Name (N);
5146 end Resolve_Entity_Name;
5152 procedure Resolve_Entry (Entry_Name : Node_Id) is
5153 Loc : constant Source_Ptr := Sloc (Entry_Name);
5161 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
5162 -- If the bounds of the entry family being called depend on task
5163 -- discriminants, build a new index subtype where a discriminant is
5164 -- replaced with the value of the discriminant of the target task.
5165 -- The target task is the prefix of the entry name in the call.
5167 -----------------------
5168 -- Actual_Index_Type --
5169 -----------------------
5171 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
5172 Typ : constant Entity_Id := Entry_Index_Type (E);
5173 Tsk : constant Entity_Id := Scope (E);
5174 Lo : constant Node_Id := Type_Low_Bound (Typ);
5175 Hi : constant Node_Id := Type_High_Bound (Typ);
5178 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
5179 -- If the bound is given by a discriminant, replace with a reference
5180 -- to the discriminant of the same name in the target task.
5181 -- If the entry name is the target of a requeue statement and the
5182 -- entry is in the current protected object, the bound to be used
5183 -- is the discriminal of the object (see apply_range_checks for
5184 -- details of the transformation).
5186 -----------------------------
5187 -- Actual_Discriminant_Ref --
5188 -----------------------------
5190 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
5191 Typ : constant Entity_Id := Etype (Bound);
5195 Remove_Side_Effects (Bound);
5197 if not Is_Entity_Name (Bound)
5198 or else Ekind (Entity (Bound)) /= E_Discriminant
5202 elsif Is_Protected_Type (Tsk)
5203 and then In_Open_Scopes (Tsk)
5204 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
5206 return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
5210 Make_Selected_Component (Loc,
5211 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
5212 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
5217 end Actual_Discriminant_Ref;
5219 -- Start of processing for Actual_Index_Type
5222 if not Has_Discriminants (Tsk)
5223 or else (not Is_Entity_Name (Lo)
5224 and then not Is_Entity_Name (Hi))
5226 return Entry_Index_Type (E);
5229 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
5230 Set_Etype (New_T, Base_Type (Typ));
5231 Set_Size_Info (New_T, Typ);
5232 Set_RM_Size (New_T, RM_Size (Typ));
5233 Set_Scalar_Range (New_T,
5234 Make_Range (Sloc (Entry_Name),
5235 Low_Bound => Actual_Discriminant_Ref (Lo),
5236 High_Bound => Actual_Discriminant_Ref (Hi)));
5240 end Actual_Index_Type;
5242 -- Start of processing of Resolve_Entry
5245 -- Find name of entry being called, and resolve prefix of name
5246 -- with its own type. The prefix can be overloaded, and the name
5247 -- and signature of the entry must be taken into account.
5249 if Nkind (Entry_Name) = N_Indexed_Component then
5251 -- Case of dealing with entry family within the current tasks
5253 E_Name := Prefix (Entry_Name);
5256 E_Name := Entry_Name;
5259 if Is_Entity_Name (E_Name) then
5260 -- Entry call to an entry (or entry family) in the current task.
5261 -- This is legal even though the task will deadlock. Rewrite as
5262 -- call to current task.
5264 -- This can also be a call to an entry in an enclosing task.
5265 -- If this is a single task, we have to retrieve its name,
5266 -- because the scope of the entry is the task type, not the
5267 -- object. If the enclosing task is a task type, the identity
5268 -- of the task is given by its own self variable.
5270 -- Finally this can be a requeue on an entry of the same task
5271 -- or protected object.
5273 S := Scope (Entity (E_Name));
5275 for J in reverse 0 .. Scope_Stack.Last loop
5277 if Is_Task_Type (Scope_Stack.Table (J).Entity)
5278 and then not Comes_From_Source (S)
5280 -- S is an enclosing task or protected object. The concurrent
5281 -- declaration has been converted into a type declaration, and
5282 -- the object itself has an object declaration that follows
5283 -- the type in the same declarative part.
5285 Tsk := Next_Entity (S);
5286 while Etype (Tsk) /= S loop
5293 elsif S = Scope_Stack.Table (J).Entity then
5295 -- Call to current task. Will be transformed into call to Self
5303 Make_Selected_Component (Loc,
5304 Prefix => New_Occurrence_Of (S, Loc),
5306 New_Occurrence_Of (Entity (E_Name), Loc));
5307 Rewrite (E_Name, New_N);
5310 elsif Nkind (Entry_Name) = N_Selected_Component
5311 and then Is_Overloaded (Prefix (Entry_Name))
5313 -- Use the entry name (which must be unique at this point) to
5314 -- find the prefix that returns the corresponding task type or
5318 Pref : constant Node_Id := Prefix (Entry_Name);
5319 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
5324 Get_First_Interp (Pref, I, It);
5325 while Present (It.Typ) loop
5326 if Scope (Ent) = It.Typ then
5327 Set_Etype (Pref, It.Typ);
5331 Get_Next_Interp (I, It);
5336 if Nkind (Entry_Name) = N_Selected_Component then
5337 Resolve (Prefix (Entry_Name));
5339 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
5340 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
5341 Resolve (Prefix (Prefix (Entry_Name)));
5342 Index := First (Expressions (Entry_Name));
5343 Resolve (Index, Entry_Index_Type (Nam));
5345 -- Up to this point the expression could have been the actual
5346 -- in a simple entry call, and be given by a named association.
5348 if Nkind (Index) = N_Parameter_Association then
5349 Error_Msg_N ("expect expression for entry index", Index);
5351 Apply_Range_Check (Index, Actual_Index_Type (Nam));
5356 ------------------------
5357 -- Resolve_Entry_Call --
5358 ------------------------
5360 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
5361 Entry_Name : constant Node_Id := Name (N);
5362 Loc : constant Source_Ptr := Sloc (Entry_Name);
5364 First_Named : Node_Id;
5371 -- We kill all checks here, because it does not seem worth the
5372 -- effort to do anything better, an entry call is a big operation.
5376 -- Processing of the name is similar for entry calls and protected
5377 -- operation calls. Once the entity is determined, we can complete
5378 -- the resolution of the actuals.
5380 -- The selector may be overloaded, in the case of a protected object
5381 -- with overloaded functions. The type of the context is used for
5384 if Nkind (Entry_Name) = N_Selected_Component
5385 and then Is_Overloaded (Selector_Name (Entry_Name))
5386 and then Typ /= Standard_Void_Type
5393 Get_First_Interp (Selector_Name (Entry_Name), I, It);
5394 while Present (It.Typ) loop
5395 if Covers (Typ, It.Typ) then
5396 Set_Entity (Selector_Name (Entry_Name), It.Nam);
5397 Set_Etype (Entry_Name, It.Typ);
5399 Generate_Reference (It.Typ, N, ' ');
5402 Get_Next_Interp (I, It);
5407 Resolve_Entry (Entry_Name);
5409 if Nkind (Entry_Name) = N_Selected_Component then
5411 -- Simple entry call
5413 Nam := Entity (Selector_Name (Entry_Name));
5414 Obj := Prefix (Entry_Name);
5415 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
5417 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
5419 -- Call to member of entry family
5421 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
5422 Obj := Prefix (Prefix (Entry_Name));
5423 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
5426 -- We cannot in general check the maximum depth of protected entry
5427 -- calls at compile time. But we can tell that any protected entry
5428 -- call at all violates a specified nesting depth of zero.
5430 if Is_Protected_Type (Scope (Nam)) then
5431 Check_Restriction (Max_Entry_Queue_Length, N);
5434 -- Use context type to disambiguate a protected function that can be
5435 -- called without actuals and that returns an array type, and where
5436 -- the argument list may be an indexing of the returned value.
5438 if Ekind (Nam) = E_Function
5439 and then Needs_No_Actuals (Nam)
5440 and then Present (Parameter_Associations (N))
5442 ((Is_Array_Type (Etype (Nam))
5443 and then Covers (Typ, Component_Type (Etype (Nam))))
5445 or else (Is_Access_Type (Etype (Nam))
5446 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5447 and then Covers (Typ,
5448 Component_Type (Designated_Type (Etype (Nam))))))
5451 Index_Node : Node_Id;
5455 Make_Indexed_Component (Loc,
5457 Make_Function_Call (Loc,
5458 Name => Relocate_Node (Entry_Name)),
5459 Expressions => Parameter_Associations (N));
5461 -- Since we are correcting a node classification error made by
5462 -- the parser, we call Replace rather than Rewrite.
5464 Replace (N, Index_Node);
5465 Set_Etype (Prefix (N), Etype (Nam));
5467 Resolve_Indexed_Component (N, Typ);
5472 -- The operation name may have been overloaded. Order the actuals
5473 -- according to the formals of the resolved entity, and set the
5474 -- return type to that of the operation.
5477 Normalize_Actuals (N, Nam, False, Norm_OK);
5478 pragma Assert (Norm_OK);
5479 Set_Etype (N, Etype (Nam));
5482 Resolve_Actuals (N, Nam);
5483 Generate_Reference (Nam, Entry_Name);
5485 if Ekind (Nam) = E_Entry
5486 or else Ekind (Nam) = E_Entry_Family
5488 Check_Potentially_Blocking_Operation (N);
5491 -- Verify that a procedure call cannot masquerade as an entry
5492 -- call where an entry call is expected.
5494 if Ekind (Nam) = E_Procedure then
5495 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5496 and then N = Entry_Call_Statement (Parent (N))
5498 Error_Msg_N ("entry call required in select statement", N);
5500 elsif Nkind (Parent (N)) = N_Triggering_Alternative
5501 and then N = Triggering_Statement (Parent (N))
5503 Error_Msg_N ("triggering statement cannot be procedure call", N);
5505 elsif Ekind (Scope (Nam)) = E_Task_Type
5506 and then not In_Open_Scopes (Scope (Nam))
5508 Error_Msg_N ("task has no entry with this name", Entry_Name);
5512 -- After resolution, entry calls and protected procedure calls
5513 -- are changed into entry calls, for expansion. The structure
5514 -- of the node does not change, so it can safely be done in place.
5515 -- Protected function calls must keep their structure because they
5516 -- are subexpressions.
5518 if Ekind (Nam) /= E_Function then
5520 -- A protected operation that is not a function may modify the
5521 -- corresponding object, and cannot apply to a constant.
5522 -- If this is an internal call, the prefix is the type itself.
5524 if Is_Protected_Type (Scope (Nam))
5525 and then not Is_Variable (Obj)
5526 and then (not Is_Entity_Name (Obj)
5527 or else not Is_Type (Entity (Obj)))
5530 ("prefix of protected procedure or entry call must be variable",
5534 Actuals := Parameter_Associations (N);
5535 First_Named := First_Named_Actual (N);
5538 Make_Entry_Call_Statement (Loc,
5540 Parameter_Associations => Actuals));
5542 Set_First_Named_Actual (N, First_Named);
5543 Set_Analyzed (N, True);
5545 -- Protected functions can return on the secondary stack, in which
5546 -- case we must trigger the transient scope mechanism.
5548 elsif Expander_Active
5549 and then Requires_Transient_Scope (Etype (Nam))
5551 Establish_Transient_Scope (N, Sec_Stack => True);
5553 end Resolve_Entry_Call;
5555 -------------------------
5556 -- Resolve_Equality_Op --
5557 -------------------------
5559 -- Both arguments must have the same type, and the boolean context
5560 -- does not participate in the resolution. The first pass verifies
5561 -- that the interpretation is not ambiguous, and the type of the left
5562 -- argument is correctly set, or is Any_Type in case of ambiguity.
5563 -- If both arguments are strings or aggregates, allocators, or Null,
5564 -- they are ambiguous even though they carry a single (universal) type.
5565 -- Diagnose this case here.
5567 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
5568 L : constant Node_Id := Left_Opnd (N);
5569 R : constant Node_Id := Right_Opnd (N);
5570 T : Entity_Id := Find_Unique_Type (L, R);
5572 function Find_Unique_Access_Type return Entity_Id;
5573 -- In the case of allocators, make a last-ditch attempt to find a single
5574 -- access type with the right designated type. This is semantically
5575 -- dubious, and of no interest to any real code, but c48008a makes it
5578 -----------------------------
5579 -- Find_Unique_Access_Type --
5580 -----------------------------
5582 function Find_Unique_Access_Type return Entity_Id is
5588 if Ekind (Etype (R)) = E_Allocator_Type then
5589 Acc := Designated_Type (Etype (R));
5590 elsif Ekind (Etype (L)) = E_Allocator_Type then
5591 Acc := Designated_Type (Etype (L));
5597 while S /= Standard_Standard loop
5598 E := First_Entity (S);
5599 while Present (E) loop
5601 and then Is_Access_Type (E)
5602 and then Ekind (E) /= E_Allocator_Type
5603 and then Designated_Type (E) = Base_Type (Acc)
5615 end Find_Unique_Access_Type;
5617 -- Start of processing for Resolve_Equality_Op
5620 Set_Etype (N, Base_Type (Typ));
5621 Generate_Reference (T, N, ' ');
5623 if T = Any_Fixed then
5624 T := Unique_Fixed_Point_Type (L);
5627 if T /= Any_Type then
5629 or else T = Any_Composite
5630 or else T = Any_Character
5632 if T = Any_Character then
5633 Ambiguous_Character (L);
5635 Error_Msg_N ("ambiguous operands for equality", N);
5638 Set_Etype (N, Any_Type);
5641 elsif T = Any_Access
5642 or else Ekind (T) = E_Allocator_Type
5643 or else Ekind (T) = E_Access_Attribute_Type
5645 T := Find_Unique_Access_Type;
5648 Error_Msg_N ("ambiguous operands for equality", N);
5649 Set_Etype (N, Any_Type);
5657 -- If the unique type is a class-wide type then it will be expanded
5658 -- into a dispatching call to the predefined primitive. Therefore we
5659 -- check here for potential violation of such restriction.
5661 if Is_Class_Wide_Type (T) then
5662 Check_Restriction (No_Dispatching_Calls, N);
5665 if Warn_On_Redundant_Constructs
5666 and then Comes_From_Source (N)
5667 and then Is_Entity_Name (R)
5668 and then Entity (R) = Standard_True
5669 and then Comes_From_Source (R)
5671 Error_Msg_N ("?comparison with True is redundant!", R);
5674 Check_Unset_Reference (L);
5675 Check_Unset_Reference (R);
5676 Generate_Operator_Reference (N, T);
5678 -- If this is an inequality, it may be the implicit inequality
5679 -- created for a user-defined operation, in which case the corres-
5680 -- ponding equality operation is not intrinsic, and the operation
5681 -- cannot be constant-folded. Else fold.
5683 if Nkind (N) = N_Op_Eq
5684 or else Comes_From_Source (Entity (N))
5685 or else Ekind (Entity (N)) = E_Operator
5686 or else Is_Intrinsic_Subprogram
5687 (Corresponding_Equality (Entity (N)))
5689 Eval_Relational_Op (N);
5690 elsif Nkind (N) = N_Op_Ne
5691 and then Is_Abstract_Subprogram (Entity (N))
5693 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
5696 -- Ada 2005: If one operand is an anonymous access type, convert
5697 -- the other operand to it, to ensure that the underlying types
5698 -- match in the back-end. Same for access_to_subprogram, and the
5699 -- conversion verifies that the types are subtype conformant.
5701 -- We apply the same conversion in the case one of the operands is
5702 -- a private subtype of the type of the other.
5704 -- Why the Expander_Active test here ???
5708 (Ekind (T) = E_Anonymous_Access_Type
5709 or else Ekind (T) = E_Anonymous_Access_Subprogram_Type
5710 or else Is_Private_Type (T))
5712 if Etype (L) /= T then
5714 Make_Unchecked_Type_Conversion (Sloc (L),
5715 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
5716 Expression => Relocate_Node (L)));
5717 Analyze_And_Resolve (L, T);
5720 if (Etype (R)) /= T then
5722 Make_Unchecked_Type_Conversion (Sloc (R),
5723 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
5724 Expression => Relocate_Node (R)));
5725 Analyze_And_Resolve (R, T);
5729 end Resolve_Equality_Op;
5731 ----------------------------------
5732 -- Resolve_Explicit_Dereference --
5733 ----------------------------------
5735 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
5736 Loc : constant Source_Ptr := Sloc (N);
5738 P : constant Node_Id := Prefix (N);
5743 Check_Fully_Declared_Prefix (Typ, P);
5745 if Is_Overloaded (P) then
5747 -- Use the context type to select the prefix that has the correct
5750 Get_First_Interp (P, I, It);
5751 while Present (It.Typ) loop
5752 exit when Is_Access_Type (It.Typ)
5753 and then Covers (Typ, Designated_Type (It.Typ));
5754 Get_Next_Interp (I, It);
5757 if Present (It.Typ) then
5758 Resolve (P, It.Typ);
5760 -- If no interpretation covers the designated type of the prefix,
5761 -- this is the pathological case where not all implementations of
5762 -- the prefix allow the interpretation of the node as a call. Now
5763 -- that the expected type is known, Remove other interpretations
5764 -- from prefix, rewrite it as a call, and resolve again, so that
5765 -- the proper call node is generated.
5767 Get_First_Interp (P, I, It);
5768 while Present (It.Typ) loop
5769 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
5773 Get_Next_Interp (I, It);
5777 Make_Function_Call (Loc,
5779 Make_Explicit_Dereference (Loc,
5781 Parameter_Associations => New_List);
5783 Save_Interps (N, New_N);
5785 Analyze_And_Resolve (N, Typ);
5789 Set_Etype (N, Designated_Type (It.Typ));
5795 if Is_Access_Type (Etype (P)) then
5796 Apply_Access_Check (N);
5799 -- If the designated type is a packed unconstrained array type, and the
5800 -- explicit dereference is not in the context of an attribute reference,
5801 -- then we must compute and set the actual subtype, since it is needed
5802 -- by Gigi. The reason we exclude the attribute case is that this is
5803 -- handled fine by Gigi, and in fact we use such attributes to build the
5804 -- actual subtype. We also exclude generated code (which builds actual
5805 -- subtypes directly if they are needed).
5807 if Is_Array_Type (Etype (N))
5808 and then Is_Packed (Etype (N))
5809 and then not Is_Constrained (Etype (N))
5810 and then Nkind (Parent (N)) /= N_Attribute_Reference
5811 and then Comes_From_Source (N)
5813 Set_Etype (N, Get_Actual_Subtype (N));
5816 -- Note: there is no Eval processing required for an explicit deference,
5817 -- because the type is known to be an allocators, and allocator
5818 -- expressions can never be static.
5820 end Resolve_Explicit_Dereference;
5822 -------------------------------
5823 -- Resolve_Indexed_Component --
5824 -------------------------------
5826 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
5827 Name : constant Node_Id := Prefix (N);
5829 Array_Type : Entity_Id := Empty; -- to prevent junk warning
5833 if Is_Overloaded (Name) then
5835 -- Use the context type to select the prefix that yields the correct
5841 I1 : Interp_Index := 0;
5842 P : constant Node_Id := Prefix (N);
5843 Found : Boolean := False;
5846 Get_First_Interp (P, I, It);
5847 while Present (It.Typ) loop
5848 if (Is_Array_Type (It.Typ)
5849 and then Covers (Typ, Component_Type (It.Typ)))
5850 or else (Is_Access_Type (It.Typ)
5851 and then Is_Array_Type (Designated_Type (It.Typ))
5853 (Typ, Component_Type (Designated_Type (It.Typ))))
5856 It := Disambiguate (P, I1, I, Any_Type);
5858 if It = No_Interp then
5859 Error_Msg_N ("ambiguous prefix for indexing", N);
5865 Array_Type := It.Typ;
5871 Array_Type := It.Typ;
5876 Get_Next_Interp (I, It);
5881 Array_Type := Etype (Name);
5884 Resolve (Name, Array_Type);
5885 Array_Type := Get_Actual_Subtype_If_Available (Name);
5887 -- If prefix is access type, dereference to get real array type.
5888 -- Note: we do not apply an access check because the expander always
5889 -- introduces an explicit dereference, and the check will happen there.
5891 if Is_Access_Type (Array_Type) then
5892 Array_Type := Designated_Type (Array_Type);
5895 -- If name was overloaded, set component type correctly now
5896 -- If a misplaced call to an entry family (which has no index typs)
5897 -- return. Error will be diagnosed from calling context.
5899 if Is_Array_Type (Array_Type) then
5900 Set_Etype (N, Component_Type (Array_Type));
5905 Index := First_Index (Array_Type);
5906 Expr := First (Expressions (N));
5908 -- The prefix may have resolved to a string literal, in which case its
5909 -- etype has a special representation. This is only possible currently
5910 -- if the prefix is a static concatenation, written in functional
5913 if Ekind (Array_Type) = E_String_Literal_Subtype then
5914 Resolve (Expr, Standard_Positive);
5917 while Present (Index) and Present (Expr) loop
5918 Resolve (Expr, Etype (Index));
5919 Check_Unset_Reference (Expr);
5921 if Is_Scalar_Type (Etype (Expr)) then
5922 Apply_Scalar_Range_Check (Expr, Etype (Index));
5924 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
5932 -- Do not generate the warning on suspicious index if we are analyzing
5933 -- package Ada.Tags; otherwise we will report the warning with the
5934 -- Prims_Ptr field of the dispatch table.
5936 if Scope (Etype (Prefix (N))) = Standard_Standard
5938 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
5941 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
5942 Eval_Indexed_Component (N);
5944 end Resolve_Indexed_Component;
5946 -----------------------------
5947 -- Resolve_Integer_Literal --
5948 -----------------------------
5950 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
5953 Eval_Integer_Literal (N);
5954 end Resolve_Integer_Literal;
5956 --------------------------------
5957 -- Resolve_Intrinsic_Operator --
5958 --------------------------------
5960 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
5961 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
5968 while Scope (Op) /= Standard_Standard loop
5970 pragma Assert (Present (Op));
5974 Set_Is_Overloaded (N, False);
5976 -- If the operand type is private, rewrite with suitable conversions on
5977 -- the operands and the result, to expose the proper underlying numeric
5980 if Is_Private_Type (Typ) then
5981 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
5983 if Nkind (N) = N_Op_Expon then
5984 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
5986 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
5989 Save_Interps (Left_Opnd (N), Expression (Arg1));
5990 Save_Interps (Right_Opnd (N), Expression (Arg2));
5992 Set_Left_Opnd (N, Arg1);
5993 Set_Right_Opnd (N, Arg2);
5995 Set_Etype (N, Btyp);
5996 Rewrite (N, Unchecked_Convert_To (Typ, N));
5999 elsif Typ /= Etype (Left_Opnd (N))
6000 or else Typ /= Etype (Right_Opnd (N))
6002 -- Add explicit conversion where needed, and save interpretations
6003 -- in case operands are overloaded.
6005 Arg1 := Convert_To (Typ, Left_Opnd (N));
6006 Arg2 := Convert_To (Typ, Right_Opnd (N));
6008 if Nkind (Arg1) = N_Type_Conversion then
6009 Save_Interps (Left_Opnd (N), Expression (Arg1));
6011 Save_Interps (Left_Opnd (N), Arg1);
6014 if Nkind (Arg2) = N_Type_Conversion then
6015 Save_Interps (Right_Opnd (N), Expression (Arg2));
6017 Save_Interps (Right_Opnd (N), Arg2);
6020 Rewrite (Left_Opnd (N), Arg1);
6021 Rewrite (Right_Opnd (N), Arg2);
6024 Resolve_Arithmetic_Op (N, Typ);
6027 Resolve_Arithmetic_Op (N, Typ);
6029 end Resolve_Intrinsic_Operator;
6031 --------------------------------------
6032 -- Resolve_Intrinsic_Unary_Operator --
6033 --------------------------------------
6035 procedure Resolve_Intrinsic_Unary_Operator
6039 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6045 while Scope (Op) /= Standard_Standard loop
6047 pragma Assert (Present (Op));
6052 if Is_Private_Type (Typ) then
6053 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6054 Save_Interps (Right_Opnd (N), Expression (Arg2));
6056 Set_Right_Opnd (N, Arg2);
6058 Set_Etype (N, Btyp);
6059 Rewrite (N, Unchecked_Convert_To (Typ, N));
6063 Resolve_Unary_Op (N, Typ);
6065 end Resolve_Intrinsic_Unary_Operator;
6067 ------------------------
6068 -- Resolve_Logical_Op --
6069 ------------------------
6071 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
6073 N_Opr : constant Node_Kind := Nkind (N);
6076 -- Predefined operations on scalar types yield the base type. On the
6077 -- other hand, logical operations on arrays yield the type of the
6078 -- arguments (and the context).
6080 if Is_Array_Type (Typ) then
6083 B_Typ := Base_Type (Typ);
6086 -- The following test is required because the operands of the operation
6087 -- may be literals, in which case the resulting type appears to be
6088 -- compatible with a signed integer type, when in fact it is compatible
6089 -- only with modular types. If the context itself is universal, the
6090 -- operation is illegal.
6092 if not Valid_Boolean_Arg (Typ) then
6093 Error_Msg_N ("invalid context for logical operation", N);
6094 Set_Etype (N, Any_Type);
6097 elsif Typ = Any_Modular then
6099 ("no modular type available in this context", N);
6100 Set_Etype (N, Any_Type);
6102 elsif Is_Modular_Integer_Type (Typ)
6103 and then Etype (Left_Opnd (N)) = Universal_Integer
6104 and then Etype (Right_Opnd (N)) = Universal_Integer
6106 Check_For_Visible_Operator (N, B_Typ);
6109 Resolve (Left_Opnd (N), B_Typ);
6110 Resolve (Right_Opnd (N), B_Typ);
6112 Check_Unset_Reference (Left_Opnd (N));
6113 Check_Unset_Reference (Right_Opnd (N));
6115 Set_Etype (N, B_Typ);
6116 Generate_Operator_Reference (N, B_Typ);
6117 Eval_Logical_Op (N);
6119 -- Check for violation of restriction No_Direct_Boolean_Operators
6120 -- if the operator was not eliminated by the Eval_Logical_Op call.
6122 if Nkind (N) = N_Opr
6123 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
6125 Check_Restriction (No_Direct_Boolean_Operators, N);
6127 end Resolve_Logical_Op;
6129 ---------------------------
6130 -- Resolve_Membership_Op --
6131 ---------------------------
6133 -- The context can only be a boolean type, and does not determine
6134 -- the arguments. Arguments should be unambiguous, but the preference
6135 -- rule for universal types applies.
6137 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
6138 pragma Warnings (Off, Typ);
6140 L : constant Node_Id := Left_Opnd (N);
6141 R : constant Node_Id := Right_Opnd (N);
6145 if L = Error or else R = Error then
6149 if not Is_Overloaded (R)
6151 (Etype (R) = Universal_Integer or else
6152 Etype (R) = Universal_Real)
6153 and then Is_Overloaded (L)
6157 -- Ada 2005 (AI-251): Give support to the following case:
6159 -- type I is interface;
6160 -- type T is tagged ...
6162 -- function Test (O : I'Class) is
6164 -- return O in T'Class.
6167 -- In this case we have nothing else to do; the membership test will be
6168 -- done at run-time.
6170 elsif Ada_Version >= Ada_05
6171 and then Is_Class_Wide_Type (Etype (L))
6172 and then Is_Interface (Etype (L))
6173 and then Is_Class_Wide_Type (Etype (R))
6174 and then not Is_Interface (Etype (R))
6179 T := Intersect_Types (L, R);
6183 Check_Unset_Reference (L);
6185 if Nkind (R) = N_Range
6186 and then not Is_Scalar_Type (T)
6188 Error_Msg_N ("scalar type required for range", R);
6191 if Is_Entity_Name (R) then
6192 Freeze_Expression (R);
6195 Check_Unset_Reference (R);
6198 Eval_Membership_Op (N);
6199 end Resolve_Membership_Op;
6205 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
6207 -- Handle restriction against anonymous null access values This
6208 -- restriction can be turned off using -gnatdh.
6210 -- Ada 2005 (AI-231): Remove restriction
6212 if Ada_Version < Ada_05
6213 and then not Debug_Flag_J
6214 and then Ekind (Typ) = E_Anonymous_Access_Type
6215 and then Comes_From_Source (N)
6217 -- In the common case of a call which uses an explicitly null
6218 -- value for an access parameter, give specialized error msg
6220 if Nkind (Parent (N)) = N_Procedure_Call_Statement
6222 Nkind (Parent (N)) = N_Function_Call
6225 ("null is not allowed as argument for an access parameter", N);
6227 -- Standard message for all other cases (are there any?)
6231 ("null cannot be of an anonymous access type", N);
6235 -- In a distributed context, null for a remote access to subprogram
6236 -- may need to be replaced with a special record aggregate. In this
6237 -- case, return after having done the transformation.
6239 if (Ekind (Typ) = E_Record_Type
6240 or else Is_Remote_Access_To_Subprogram_Type (Typ))
6241 and then Remote_AST_Null_Value (N, Typ)
6246 -- The null literal takes its type from the context
6251 -----------------------
6252 -- Resolve_Op_Concat --
6253 -----------------------
6255 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
6256 Btyp : constant Entity_Id := Base_Type (Typ);
6257 Op1 : constant Node_Id := Left_Opnd (N);
6258 Op2 : constant Node_Id := Right_Opnd (N);
6260 procedure Resolve_Concatenation_Arg (Arg : Node_Id; Is_Comp : Boolean);
6261 -- Internal procedure to resolve one operand of concatenation operator.
6262 -- The operand is either of the array type or of the component type.
6263 -- If the operand is an aggregate, and the component type is composite,
6264 -- this is ambiguous if component type has aggregates.
6266 -------------------------------
6267 -- Resolve_Concatenation_Arg --
6268 -------------------------------
6270 procedure Resolve_Concatenation_Arg (Arg : Node_Id; Is_Comp : Boolean) is
6274 or else (not Is_Overloaded (Arg)
6275 and then Etype (Arg) /= Any_Composite
6276 and then Covers (Component_Type (Typ), Etype (Arg)))
6278 Resolve (Arg, Component_Type (Typ));
6280 Resolve (Arg, Btyp);
6283 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
6285 if Nkind (Arg) = N_Aggregate
6286 and then Is_Composite_Type (Component_Type (Typ))
6288 if Is_Private_Type (Component_Type (Typ)) then
6289 Resolve (Arg, Btyp);
6292 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
6293 Set_Etype (Arg, Any_Type);
6297 if Is_Overloaded (Arg)
6298 and then Has_Compatible_Type (Arg, Typ)
6299 and then Etype (Arg) /= Any_Type
6308 Get_First_Interp (Arg, I, It);
6310 Get_Next_Interp (I, It);
6312 -- Special-case the error message when the overloading
6313 -- is caused by a function that yields and array and
6314 -- can be called without parameters.
6316 if It.Nam = Func then
6317 Error_Msg_Sloc := Sloc (Func);
6318 Error_Msg_N ("ambiguous call to function#", Arg);
6320 ("\\interpretation as call yields&", Arg, Typ);
6322 ("\\interpretation as indexing of call yields&",
6323 Arg, Component_Type (Typ));
6327 ("ambiguous operand for concatenation!", Arg);
6328 Get_First_Interp (Arg, I, It);
6329 while Present (It.Nam) loop
6330 Error_Msg_Sloc := Sloc (It.Nam);
6332 if Base_Type (It.Typ) = Base_Type (Typ)
6333 or else Base_Type (It.Typ) =
6334 Base_Type (Component_Type (Typ))
6336 Error_Msg_N ("\\possible interpretation#", Arg);
6339 Get_Next_Interp (I, It);
6345 Resolve (Arg, Component_Type (Typ));
6347 if Nkind (Arg) = N_String_Literal then
6348 Set_Etype (Arg, Component_Type (Typ));
6351 if Arg = Left_Opnd (N) then
6352 Set_Is_Component_Left_Opnd (N);
6354 Set_Is_Component_Right_Opnd (N);
6359 Resolve (Arg, Btyp);
6362 Check_Unset_Reference (Arg);
6363 end Resolve_Concatenation_Arg;
6365 -- Start of processing for Resolve_Op_Concat
6368 -- The parser folds an enormous sequence of concatenations of string
6369 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
6370 -- in the right. If the expression resolves to a predefined "&"
6371 -- operator, all is well. Otherwise, the parser's folding is wrong, so
6372 -- we give an error. See P_Simple_Expression in Par.Ch4.
6374 if Nkind (Op2) = N_String_Literal
6375 and then Is_Folded_In_Parser (Op2)
6376 and then Ekind (Entity (N)) = E_Function
6378 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
6379 and then String_Length (Strval (Op1)) = 0);
6380 Error_Msg_N ("too many user-defined concatenations", N);
6384 Set_Etype (N, Btyp);
6386 if Is_Limited_Composite (Btyp) then
6387 Error_Msg_N ("concatenation not available for limited array", N);
6388 Explain_Limited_Type (Btyp, N);
6391 -- If the operands are themselves concatenations, resolve them as such
6392 -- directly. This removes several layers of recursion and allows GNAT to
6393 -- handle larger multiple concatenations.
6395 if Nkind (Op1) = N_Op_Concat
6396 and then not Is_Array_Type (Component_Type (Typ))
6397 and then Entity (Op1) = Entity (N)
6399 Resolve_Op_Concat (Op1, Typ);
6401 Resolve_Concatenation_Arg
6402 (Op1, Is_Component_Left_Opnd (N));
6405 if Nkind (Op2) = N_Op_Concat
6406 and then not Is_Array_Type (Component_Type (Typ))
6407 and then Entity (Op2) = Entity (N)
6409 Resolve_Op_Concat (Op2, Typ);
6411 Resolve_Concatenation_Arg
6412 (Op2, Is_Component_Right_Opnd (N));
6415 Generate_Operator_Reference (N, Typ);
6417 if Is_String_Type (Typ) then
6418 Eval_Concatenation (N);
6421 -- If this is not a static concatenation, but the result is a
6422 -- string type (and not an array of strings) insure that static
6423 -- string operands have their subtypes properly constructed.
6425 if Nkind (N) /= N_String_Literal
6426 and then Is_Character_Type (Component_Type (Typ))
6428 Set_String_Literal_Subtype (Op1, Typ);
6429 Set_String_Literal_Subtype (Op2, Typ);
6431 end Resolve_Op_Concat;
6433 ----------------------
6434 -- Resolve_Op_Expon --
6435 ----------------------
6437 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
6438 B_Typ : constant Entity_Id := Base_Type (Typ);
6441 -- Catch attempts to do fixed-point exponentation with universal
6442 -- operands, which is a case where the illegality is not caught during
6443 -- normal operator analysis.
6445 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
6446 Error_Msg_N ("exponentiation not available for fixed point", N);
6450 if Comes_From_Source (N)
6451 and then Ekind (Entity (N)) = E_Function
6452 and then Is_Imported (Entity (N))
6453 and then Is_Intrinsic_Subprogram (Entity (N))
6455 Resolve_Intrinsic_Operator (N, Typ);
6459 if Etype (Left_Opnd (N)) = Universal_Integer
6460 or else Etype (Left_Opnd (N)) = Universal_Real
6462 Check_For_Visible_Operator (N, B_Typ);
6465 -- We do the resolution using the base type, because intermediate values
6466 -- in expressions always are of the base type, not a subtype of it.
6468 Resolve (Left_Opnd (N), B_Typ);
6469 Resolve (Right_Opnd (N), Standard_Integer);
6471 Check_Unset_Reference (Left_Opnd (N));
6472 Check_Unset_Reference (Right_Opnd (N));
6474 Set_Etype (N, B_Typ);
6475 Generate_Operator_Reference (N, B_Typ);
6478 -- Set overflow checking bit. Much cleverer code needed here eventually
6479 -- and perhaps the Resolve routines should be separated for the various
6480 -- arithmetic operations, since they will need different processing. ???
6482 if Nkind (N) in N_Op then
6483 if not Overflow_Checks_Suppressed (Etype (N)) then
6484 Enable_Overflow_Check (N);
6487 end Resolve_Op_Expon;
6489 --------------------
6490 -- Resolve_Op_Not --
6491 --------------------
6493 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
6496 function Parent_Is_Boolean return Boolean;
6497 -- This function determines if the parent node is a boolean operator
6498 -- or operation (comparison op, membership test, or short circuit form)
6499 -- and the not in question is the left operand of this operation.
6500 -- Note that if the not is in parens, then false is returned.
6502 -----------------------
6503 -- Parent_Is_Boolean --
6504 -----------------------
6506 function Parent_Is_Boolean return Boolean is
6508 if Paren_Count (N) /= 0 then
6512 case Nkind (Parent (N)) is
6527 return Left_Opnd (Parent (N)) = N;
6533 end Parent_Is_Boolean;
6535 -- Start of processing for Resolve_Op_Not
6538 -- Predefined operations on scalar types yield the base type. On the
6539 -- other hand, logical operations on arrays yield the type of the
6540 -- arguments (and the context).
6542 if Is_Array_Type (Typ) then
6545 B_Typ := Base_Type (Typ);
6548 -- Straigtforward case of incorrect arguments
6550 if not Valid_Boolean_Arg (Typ) then
6551 Error_Msg_N ("invalid operand type for operator&", N);
6552 Set_Etype (N, Any_Type);
6555 -- Special case of probable missing parens
6557 elsif Typ = Universal_Integer or else Typ = Any_Modular then
6558 if Parent_Is_Boolean then
6560 ("operand of not must be enclosed in parentheses",
6564 ("no modular type available in this context", N);
6567 Set_Etype (N, Any_Type);
6570 -- OK resolution of not
6573 -- Warn if non-boolean types involved. This is a case like not a < b
6574 -- where a and b are modular, where we will get (not a) < b and most
6575 -- likely not (a < b) was intended.
6577 if Warn_On_Questionable_Missing_Parens
6578 and then not Is_Boolean_Type (Typ)
6579 and then Parent_Is_Boolean
6581 Error_Msg_N ("?not expression should be parenthesized here!", N);
6584 Resolve (Right_Opnd (N), B_Typ);
6585 Check_Unset_Reference (Right_Opnd (N));
6586 Set_Etype (N, B_Typ);
6587 Generate_Operator_Reference (N, B_Typ);
6592 -----------------------------
6593 -- Resolve_Operator_Symbol --
6594 -----------------------------
6596 -- Nothing to be done, all resolved already
6598 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
6599 pragma Warnings (Off, N);
6600 pragma Warnings (Off, Typ);
6604 end Resolve_Operator_Symbol;
6606 ----------------------------------
6607 -- Resolve_Qualified_Expression --
6608 ----------------------------------
6610 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
6611 pragma Warnings (Off, Typ);
6613 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
6614 Expr : constant Node_Id := Expression (N);
6617 Resolve (Expr, Target_Typ);
6619 -- A qualified expression requires an exact match of the type,
6620 -- class-wide matching is not allowed. However, if the qualifying
6621 -- type is specific and the expression has a class-wide type, it
6622 -- may still be okay, since it can be the result of the expansion
6623 -- of a call to a dispatching function, so we also have to check
6624 -- class-wideness of the type of the expression's original node.
6626 if (Is_Class_Wide_Type (Target_Typ)
6628 (Is_Class_Wide_Type (Etype (Expr))
6629 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
6630 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
6632 Wrong_Type (Expr, Target_Typ);
6635 -- If the target type is unconstrained, then we reset the type of
6636 -- the result from the type of the expression. For other cases, the
6637 -- actual subtype of the expression is the target type.
6639 if Is_Composite_Type (Target_Typ)
6640 and then not Is_Constrained (Target_Typ)
6642 Set_Etype (N, Etype (Expr));
6645 Eval_Qualified_Expression (N);
6646 end Resolve_Qualified_Expression;
6652 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
6653 L : constant Node_Id := Low_Bound (N);
6654 H : constant Node_Id := High_Bound (N);
6661 Check_Unset_Reference (L);
6662 Check_Unset_Reference (H);
6664 -- We have to check the bounds for being within the base range as
6665 -- required for a non-static context. Normally this is automatic and
6666 -- done as part of evaluating expressions, but the N_Range node is an
6667 -- exception, since in GNAT we consider this node to be a subexpression,
6668 -- even though in Ada it is not. The circuit in Sem_Eval could check for
6669 -- this, but that would put the test on the main evaluation path for
6672 Check_Non_Static_Context (L);
6673 Check_Non_Static_Context (H);
6675 -- Check for an ambiguous range over character literals. This will
6676 -- happen with a membership test involving only literals.
6678 if Typ = Any_Character then
6679 Ambiguous_Character (L);
6680 Set_Etype (N, Any_Type);
6684 -- If bounds are static, constant-fold them, so size computations
6685 -- are identical between front-end and back-end. Do not perform this
6686 -- transformation while analyzing generic units, as type information
6687 -- would then be lost when reanalyzing the constant node in the
6690 if Is_Discrete_Type (Typ) and then Expander_Active then
6691 if Is_OK_Static_Expression (L) then
6692 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
6695 if Is_OK_Static_Expression (H) then
6696 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
6701 --------------------------
6702 -- Resolve_Real_Literal --
6703 --------------------------
6705 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
6706 Actual_Typ : constant Entity_Id := Etype (N);
6709 -- Special processing for fixed-point literals to make sure that the
6710 -- value is an exact multiple of small where this is required. We
6711 -- skip this for the universal real case, and also for generic types.
6713 if Is_Fixed_Point_Type (Typ)
6714 and then Typ /= Universal_Fixed
6715 and then Typ /= Any_Fixed
6716 and then not Is_Generic_Type (Typ)
6719 Val : constant Ureal := Realval (N);
6720 Cintr : constant Ureal := Val / Small_Value (Typ);
6721 Cint : constant Uint := UR_Trunc (Cintr);
6722 Den : constant Uint := Norm_Den (Cintr);
6726 -- Case of literal is not an exact multiple of the Small
6730 -- For a source program literal for a decimal fixed-point
6731 -- type, this is statically illegal (RM 4.9(36)).
6733 if Is_Decimal_Fixed_Point_Type (Typ)
6734 and then Actual_Typ = Universal_Real
6735 and then Comes_From_Source (N)
6737 Error_Msg_N ("value has extraneous low order digits", N);
6740 -- Generate a warning if literal from source
6742 if Is_Static_Expression (N)
6743 and then Warn_On_Bad_Fixed_Value
6746 ("?static fixed-point value is not a multiple of Small!",
6750 -- Replace literal by a value that is the exact representation
6751 -- of a value of the type, i.e. a multiple of the small value,
6752 -- by truncation, since Machine_Rounds is false for all GNAT
6753 -- fixed-point types (RM 4.9(38)).
6755 Stat := Is_Static_Expression (N);
6757 Make_Real_Literal (Sloc (N),
6758 Realval => Small_Value (Typ) * Cint));
6760 Set_Is_Static_Expression (N, Stat);
6763 -- In all cases, set the corresponding integer field
6765 Set_Corresponding_Integer_Value (N, Cint);
6769 -- Now replace the actual type by the expected type as usual
6772 Eval_Real_Literal (N);
6773 end Resolve_Real_Literal;
6775 -----------------------
6776 -- Resolve_Reference --
6777 -----------------------
6779 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
6780 P : constant Node_Id := Prefix (N);
6783 -- Replace general access with specific type
6785 if Ekind (Etype (N)) = E_Allocator_Type then
6786 Set_Etype (N, Base_Type (Typ));
6789 Resolve (P, Designated_Type (Etype (N)));
6791 -- If we are taking the reference of a volatile entity, then treat
6792 -- it as a potential modification of this entity. This is much too
6793 -- conservative, but is necessary because remove side effects can
6794 -- result in transformations of normal assignments into reference
6795 -- sequences that otherwise fail to notice the modification.
6797 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
6798 Note_Possible_Modification (P);
6800 end Resolve_Reference;
6802 --------------------------------
6803 -- Resolve_Selected_Component --
6804 --------------------------------
6806 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
6808 Comp1 : Entity_Id := Empty; -- prevent junk warning
6809 P : constant Node_Id := Prefix (N);
6810 S : constant Node_Id := Selector_Name (N);
6811 T : Entity_Id := Etype (P);
6813 I1 : Interp_Index := 0; -- prevent junk warning
6818 function Init_Component return Boolean;
6819 -- Check whether this is the initialization of a component within an
6820 -- init proc (by assignment or call to another init proc). If true,
6821 -- there is no need for a discriminant check.
6823 --------------------
6824 -- Init_Component --
6825 --------------------
6827 function Init_Component return Boolean is
6829 return Inside_Init_Proc
6830 and then Nkind (Prefix (N)) = N_Identifier
6831 and then Chars (Prefix (N)) = Name_uInit
6832 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
6835 -- Start of processing for Resolve_Selected_Component
6838 if Is_Overloaded (P) then
6840 -- Use the context type to select the prefix that has a selector
6841 -- of the correct name and type.
6844 Get_First_Interp (P, I, It);
6846 Search : while Present (It.Typ) loop
6847 if Is_Access_Type (It.Typ) then
6848 T := Designated_Type (It.Typ);
6853 if Is_Record_Type (T) then
6854 Comp := First_Entity (T);
6855 while Present (Comp) loop
6856 if Chars (Comp) = Chars (S)
6857 and then Covers (Etype (Comp), Typ)
6866 It := Disambiguate (P, I1, I, Any_Type);
6868 if It = No_Interp then
6870 ("ambiguous prefix for selected component", N);
6877 -- There may be an implicit dereference. Retrieve
6878 -- designated record type.
6880 if Is_Access_Type (It1.Typ) then
6881 T := Designated_Type (It1.Typ);
6886 if Scope (Comp1) /= T then
6888 -- Resolution chooses the new interpretation.
6889 -- Find the component with the right name.
6891 Comp1 := First_Entity (T);
6892 while Present (Comp1)
6893 and then Chars (Comp1) /= Chars (S)
6895 Comp1 := Next_Entity (Comp1);
6904 Comp := Next_Entity (Comp);
6909 Get_Next_Interp (I, It);
6912 Resolve (P, It1.Typ);
6914 Set_Entity_With_Style_Check (S, Comp1);
6917 -- Resolve prefix with its type
6922 -- Generate cross-reference. We needed to wait until full overloading
6923 -- resolution was complete to do this, since otherwise we can't tell if
6924 -- we are an Lvalue of not.
6926 if May_Be_Lvalue (N) then
6927 Generate_Reference (Entity (S), S, 'm');
6929 Generate_Reference (Entity (S), S, 'r');
6932 -- If prefix is an access type, the node will be transformed into an
6933 -- explicit dereference during expansion. The type of the node is the
6934 -- designated type of that of the prefix.
6936 if Is_Access_Type (Etype (P)) then
6937 T := Designated_Type (Etype (P));
6938 Check_Fully_Declared_Prefix (T, P);
6943 if Has_Discriminants (T)
6944 and then (Ekind (Entity (S)) = E_Component
6946 Ekind (Entity (S)) = E_Discriminant)
6947 and then Present (Original_Record_Component (Entity (S)))
6948 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
6949 and then Present (Discriminant_Checking_Func
6950 (Original_Record_Component (Entity (S))))
6951 and then not Discriminant_Checks_Suppressed (T)
6952 and then not Init_Component
6954 Set_Do_Discriminant_Check (N);
6957 if Ekind (Entity (S)) = E_Void then
6958 Error_Msg_N ("premature use of component", S);
6961 -- If the prefix is a record conversion, this may be a renamed
6962 -- discriminant whose bounds differ from those of the original
6963 -- one, so we must ensure that a range check is performed.
6965 if Nkind (P) = N_Type_Conversion
6966 and then Ekind (Entity (S)) = E_Discriminant
6967 and then Is_Discrete_Type (Typ)
6969 Set_Etype (N, Base_Type (Typ));
6972 -- Note: No Eval processing is required, because the prefix is of a
6973 -- record type, or protected type, and neither can possibly be static.
6975 end Resolve_Selected_Component;
6981 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
6982 B_Typ : constant Entity_Id := Base_Type (Typ);
6983 L : constant Node_Id := Left_Opnd (N);
6984 R : constant Node_Id := Right_Opnd (N);
6987 -- We do the resolution using the base type, because intermediate values
6988 -- in expressions always are of the base type, not a subtype of it.
6991 Resolve (R, Standard_Natural);
6993 Check_Unset_Reference (L);
6994 Check_Unset_Reference (R);
6996 Set_Etype (N, B_Typ);
6997 Generate_Operator_Reference (N, B_Typ);
7001 ---------------------------
7002 -- Resolve_Short_Circuit --
7003 ---------------------------
7005 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
7006 B_Typ : constant Entity_Id := Base_Type (Typ);
7007 L : constant Node_Id := Left_Opnd (N);
7008 R : constant Node_Id := Right_Opnd (N);
7014 Check_Unset_Reference (L);
7015 Check_Unset_Reference (R);
7017 Set_Etype (N, B_Typ);
7018 Eval_Short_Circuit (N);
7019 end Resolve_Short_Circuit;
7025 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
7026 Name : constant Node_Id := Prefix (N);
7027 Drange : constant Node_Id := Discrete_Range (N);
7028 Array_Type : Entity_Id := Empty;
7032 if Is_Overloaded (Name) then
7034 -- Use the context type to select the prefix that yields the
7035 -- correct array type.
7039 I1 : Interp_Index := 0;
7041 P : constant Node_Id := Prefix (N);
7042 Found : Boolean := False;
7045 Get_First_Interp (P, I, It);
7046 while Present (It.Typ) loop
7047 if (Is_Array_Type (It.Typ)
7048 and then Covers (Typ, It.Typ))
7049 or else (Is_Access_Type (It.Typ)
7050 and then Is_Array_Type (Designated_Type (It.Typ))
7051 and then Covers (Typ, Designated_Type (It.Typ)))
7054 It := Disambiguate (P, I1, I, Any_Type);
7056 if It = No_Interp then
7057 Error_Msg_N ("ambiguous prefix for slicing", N);
7062 Array_Type := It.Typ;
7067 Array_Type := It.Typ;
7072 Get_Next_Interp (I, It);
7077 Array_Type := Etype (Name);
7080 Resolve (Name, Array_Type);
7082 if Is_Access_Type (Array_Type) then
7083 Apply_Access_Check (N);
7084 Array_Type := Designated_Type (Array_Type);
7086 -- If the prefix is an access to an unconstrained array, we must use
7087 -- the actual subtype of the object to perform the index checks. The
7088 -- object denoted by the prefix is implicit in the node, so we build
7089 -- an explicit representation for it in order to compute the actual
7092 if not Is_Constrained (Array_Type) then
7093 Remove_Side_Effects (Prefix (N));
7096 Obj : constant Node_Id :=
7097 Make_Explicit_Dereference (Sloc (N),
7098 Prefix => New_Copy_Tree (Prefix (N)));
7100 Set_Etype (Obj, Array_Type);
7101 Set_Parent (Obj, Parent (N));
7102 Array_Type := Get_Actual_Subtype (Obj);
7106 elsif Is_Entity_Name (Name)
7107 or else (Nkind (Name) = N_Function_Call
7108 and then not Is_Constrained (Etype (Name)))
7110 Array_Type := Get_Actual_Subtype (Name);
7112 -- If the name is a selected component that depends on discriminants,
7113 -- build an actual subtype for it. This can happen only when the name
7114 -- itself is overloaded; otherwise the actual subtype is created when
7115 -- the selected component is analyzed.
7117 elsif Nkind (Name) = N_Selected_Component
7118 and then Full_Analysis
7119 and then Depends_On_Discriminant (First_Index (Array_Type))
7122 Act_Decl : constant Node_Id :=
7123 Build_Actual_Subtype_Of_Component (Array_Type, Name);
7125 Insert_Action (N, Act_Decl);
7126 Array_Type := Defining_Identifier (Act_Decl);
7130 -- If name was overloaded, set slice type correctly now
7132 Set_Etype (N, Array_Type);
7134 -- If the range is specified by a subtype mark, no resolution is
7135 -- necessary. Else resolve the bounds, and apply needed checks.
7137 if not Is_Entity_Name (Drange) then
7138 Index := First_Index (Array_Type);
7139 Resolve (Drange, Base_Type (Etype (Index)));
7141 if Nkind (Drange) = N_Range
7143 -- Do not apply the range check to nodes associated with the
7144 -- frontend expansion of the dispatch table. We first check
7145 -- if Ada.Tags is already loaded to void the addition of an
7146 -- undesired dependence on such run-time unit.
7151 (RTU_Loaded (Ada_Tags)
7152 and then Nkind (Prefix (N)) = N_Selected_Component
7153 and then Present (Entity (Selector_Name (Prefix (N))))
7154 and then Entity (Selector_Name (Prefix (N))) =
7155 RTE_Record_Component (RE_Prims_Ptr)))
7157 Apply_Range_Check (Drange, Etype (Index));
7161 Set_Slice_Subtype (N);
7163 if Nkind (Drange) = N_Range then
7164 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
7165 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
7171 ----------------------------
7172 -- Resolve_String_Literal --
7173 ----------------------------
7175 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
7176 C_Typ : constant Entity_Id := Component_Type (Typ);
7177 R_Typ : constant Entity_Id := Root_Type (C_Typ);
7178 Loc : constant Source_Ptr := Sloc (N);
7179 Str : constant String_Id := Strval (N);
7180 Strlen : constant Nat := String_Length (Str);
7181 Subtype_Id : Entity_Id;
7182 Need_Check : Boolean;
7185 -- For a string appearing in a concatenation, defer creation of the
7186 -- string_literal_subtype until the end of the resolution of the
7187 -- concatenation, because the literal may be constant-folded away. This
7188 -- is a useful optimization for long concatenation expressions.
7190 -- If the string is an aggregate built for a single character (which
7191 -- happens in a non-static context) or a is null string to which special
7192 -- checks may apply, we build the subtype. Wide strings must also get a
7193 -- string subtype if they come from a one character aggregate. Strings
7194 -- generated by attributes might be static, but it is often hard to
7195 -- determine whether the enclosing context is static, so we generate
7196 -- subtypes for them as well, thus losing some rarer optimizations ???
7197 -- Same for strings that come from a static conversion.
7200 (Strlen = 0 and then Typ /= Standard_String)
7201 or else Nkind (Parent (N)) /= N_Op_Concat
7202 or else (N /= Left_Opnd (Parent (N))
7203 and then N /= Right_Opnd (Parent (N)))
7204 or else ((Typ = Standard_Wide_String
7205 or else Typ = Standard_Wide_Wide_String)
7206 and then Nkind (Original_Node (N)) /= N_String_Literal);
7208 -- If the resolving type is itself a string literal subtype, we
7209 -- can just reuse it, since there is no point in creating another.
7211 if Ekind (Typ) = E_String_Literal_Subtype then
7214 elsif Nkind (Parent (N)) = N_Op_Concat
7215 and then not Need_Check
7216 and then Nkind (Original_Node (N)) /= N_Character_Literal
7217 and then Nkind (Original_Node (N)) /= N_Attribute_Reference
7218 and then Nkind (Original_Node (N)) /= N_Qualified_Expression
7219 and then Nkind (Original_Node (N)) /= N_Type_Conversion
7223 -- Otherwise we must create a string literal subtype. Note that the
7224 -- whole idea of string literal subtypes is simply to avoid the need
7225 -- for building a full fledged array subtype for each literal.
7227 Set_String_Literal_Subtype (N, Typ);
7228 Subtype_Id := Etype (N);
7231 if Nkind (Parent (N)) /= N_Op_Concat
7234 Set_Etype (N, Subtype_Id);
7235 Eval_String_Literal (N);
7238 if Is_Limited_Composite (Typ)
7239 or else Is_Private_Composite (Typ)
7241 Error_Msg_N ("string literal not available for private array", N);
7242 Set_Etype (N, Any_Type);
7246 -- The validity of a null string has been checked in the
7247 -- call to Eval_String_Literal.
7252 -- Always accept string literal with component type Any_Character, which
7253 -- occurs in error situations and in comparisons of literals, both of
7254 -- which should accept all literals.
7256 elsif R_Typ = Any_Character then
7259 -- If the type is bit-packed, then we always tranform the string literal
7260 -- into a full fledged aggregate.
7262 elsif Is_Bit_Packed_Array (Typ) then
7265 -- Deal with cases of Wide_Wide_String, Wide_String, and String
7268 -- For Standard.Wide_Wide_String, or any other type whose component
7269 -- type is Standard.Wide_Wide_Character, we know that all the
7270 -- characters in the string must be acceptable, since the parser
7271 -- accepted the characters as valid character literals.
7273 if R_Typ = Standard_Wide_Wide_Character then
7276 -- For the case of Standard.String, or any other type whose component
7277 -- type is Standard.Character, we must make sure that there are no
7278 -- wide characters in the string, i.e. that it is entirely composed
7279 -- of characters in range of type Character.
7281 -- If the string literal is the result of a static concatenation, the
7282 -- test has already been performed on the components, and need not be
7285 elsif R_Typ = Standard_Character
7286 and then Nkind (Original_Node (N)) /= N_Op_Concat
7288 for J in 1 .. Strlen loop
7289 if not In_Character_Range (Get_String_Char (Str, J)) then
7291 -- If we are out of range, post error. This is one of the
7292 -- very few places that we place the flag in the middle of
7293 -- a token, right under the offending wide character.
7296 ("literal out of range of type Standard.Character",
7297 Source_Ptr (Int (Loc) + J));
7302 -- For the case of Standard.Wide_String, or any other type whose
7303 -- component type is Standard.Wide_Character, we must make sure that
7304 -- there are no wide characters in the string, i.e. that it is
7305 -- entirely composed of characters in range of type Wide_Character.
7307 -- If the string literal is the result of a static concatenation,
7308 -- the test has already been performed on the components, and need
7311 elsif R_Typ = Standard_Wide_Character
7312 and then Nkind (Original_Node (N)) /= N_Op_Concat
7314 for J in 1 .. Strlen loop
7315 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
7317 -- If we are out of range, post error. This is one of the
7318 -- very few places that we place the flag in the middle of
7319 -- a token, right under the offending wide character.
7321 -- This is not quite right, because characters in general
7322 -- will take more than one character position ???
7325 ("literal out of range of type Standard.Wide_Character",
7326 Source_Ptr (Int (Loc) + J));
7331 -- If the root type is not a standard character, then we will convert
7332 -- the string into an aggregate and will let the aggregate code do
7333 -- the checking. Standard Wide_Wide_Character is also OK here.
7339 -- See if the component type of the array corresponding to the string
7340 -- has compile time known bounds. If yes we can directly check
7341 -- whether the evaluation of the string will raise constraint error.
7342 -- Otherwise we need to transform the string literal into the
7343 -- corresponding character aggregate and let the aggregate
7344 -- code do the checking.
7346 if R_Typ = Standard_Character
7347 or else R_Typ = Standard_Wide_Character
7348 or else R_Typ = Standard_Wide_Wide_Character
7350 -- Check for the case of full range, where we are definitely OK
7352 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
7356 -- Here the range is not the complete base type range, so check
7359 Comp_Typ_Lo : constant Node_Id :=
7360 Type_Low_Bound (Component_Type (Typ));
7361 Comp_Typ_Hi : constant Node_Id :=
7362 Type_High_Bound (Component_Type (Typ));
7367 if Compile_Time_Known_Value (Comp_Typ_Lo)
7368 and then Compile_Time_Known_Value (Comp_Typ_Hi)
7370 for J in 1 .. Strlen loop
7371 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
7373 if Char_Val < Expr_Value (Comp_Typ_Lo)
7374 or else Char_Val > Expr_Value (Comp_Typ_Hi)
7376 Apply_Compile_Time_Constraint_Error
7377 (N, "character out of range?", CE_Range_Check_Failed,
7378 Loc => Source_Ptr (Int (Loc) + J));
7388 -- If we got here we meed to transform the string literal into the
7389 -- equivalent qualified positional array aggregate. This is rather
7390 -- heavy artillery for this situation, but it is hard work to avoid.
7393 Lits : constant List_Id := New_List;
7394 P : Source_Ptr := Loc + 1;
7398 -- Build the character literals, we give them source locations that
7399 -- correspond to the string positions, which is a bit tricky given
7400 -- the possible presence of wide character escape sequences.
7402 for J in 1 .. Strlen loop
7403 C := Get_String_Char (Str, J);
7404 Set_Character_Literal_Name (C);
7407 Make_Character_Literal (P,
7409 Char_Literal_Value => UI_From_CC (C)));
7411 if In_Character_Range (C) then
7414 -- Should we have a call to Skip_Wide here ???
7422 Make_Qualified_Expression (Loc,
7423 Subtype_Mark => New_Reference_To (Typ, Loc),
7425 Make_Aggregate (Loc, Expressions => Lits)));
7427 Analyze_And_Resolve (N, Typ);
7429 end Resolve_String_Literal;
7431 -----------------------------
7432 -- Resolve_Subprogram_Info --
7433 -----------------------------
7435 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
7438 end Resolve_Subprogram_Info;
7440 -----------------------------
7441 -- Resolve_Type_Conversion --
7442 -----------------------------
7444 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
7445 Conv_OK : constant Boolean := Conversion_OK (N);
7446 Operand : constant Node_Id := Expression (N);
7447 Operand_Typ : constant Entity_Id := Etype (Operand);
7448 Target_Typ : constant Entity_Id := Etype (N);
7455 and then not Valid_Conversion (N, Target_Typ, Operand)
7460 if Etype (Operand) = Any_Fixed then
7462 -- Mixed-mode operation involving a literal. Context must be a fixed
7463 -- type which is applied to the literal subsequently.
7465 if Is_Fixed_Point_Type (Typ) then
7466 Set_Etype (Operand, Universal_Real);
7468 elsif Is_Numeric_Type (Typ)
7469 and then (Nkind (Operand) = N_Op_Multiply
7470 or else Nkind (Operand) = N_Op_Divide)
7471 and then (Etype (Right_Opnd (Operand)) = Universal_Real
7472 or else Etype (Left_Opnd (Operand)) = Universal_Real)
7474 -- Return if expression is ambiguous
7476 if Unique_Fixed_Point_Type (N) = Any_Type then
7479 -- If nothing else, the available fixed type is Duration
7482 Set_Etype (Operand, Standard_Duration);
7485 -- Resolve the real operand with largest available precision
7487 if Etype (Right_Opnd (Operand)) = Universal_Real then
7488 Rop := New_Copy_Tree (Right_Opnd (Operand));
7490 Rop := New_Copy_Tree (Left_Opnd (Operand));
7493 Resolve (Rop, Universal_Real);
7495 -- If the operand is a literal (it could be a non-static and
7496 -- illegal exponentiation) check whether the use of Duration
7497 -- is potentially inaccurate.
7499 if Nkind (Rop) = N_Real_Literal
7500 and then Realval (Rop) /= Ureal_0
7501 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
7504 ("?universal real operand can only " &
7505 "be interpreted as Duration!",
7508 ("\?precision will be lost in the conversion!", Rop);
7511 elsif Is_Numeric_Type (Typ)
7512 and then Nkind (Operand) in N_Op
7513 and then Unique_Fixed_Point_Type (N) /= Any_Type
7515 Set_Etype (Operand, Standard_Duration);
7518 Error_Msg_N ("invalid context for mixed mode operation", N);
7519 Set_Etype (Operand, Any_Type);
7526 -- Note: we do the Eval_Type_Conversion call before applying the
7527 -- required checks for a subtype conversion. This is important,
7528 -- since both are prepared under certain circumstances to change
7529 -- the type conversion to a constraint error node, but in the case
7530 -- of Eval_Type_Conversion this may reflect an illegality in the
7531 -- static case, and we would miss the illegality (getting only a
7532 -- warning message), if we applied the type conversion checks first.
7534 Eval_Type_Conversion (N);
7536 -- Even when evaluation is not possible, we may be able to simplify
7537 -- the conversion or its expression. This needs to be done before
7538 -- applying checks, since otherwise the checks may use the original
7539 -- expression and defeat the simplifications. This is specifically
7540 -- the case for elimination of the floating-point Truncation
7541 -- attribute in float-to-int conversions.
7543 Simplify_Type_Conversion (N);
7545 -- If after evaluation we still have a type conversion, then we
7546 -- may need to apply checks required for a subtype conversion.
7548 -- Skip these type conversion checks if universal fixed operands
7549 -- operands involved, since range checks are handled separately for
7550 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
7552 if Nkind (N) = N_Type_Conversion
7553 and then not Is_Generic_Type (Root_Type (Target_Typ))
7554 and then Target_Typ /= Universal_Fixed
7555 and then Operand_Typ /= Universal_Fixed
7557 Apply_Type_Conversion_Checks (N);
7560 -- Issue warning for conversion of simple object to its own type
7561 -- We have to test the original nodes, since they may have been
7562 -- rewritten by various optimizations.
7564 Orig_N := Original_Node (N);
7566 if Warn_On_Redundant_Constructs
7567 and then Comes_From_Source (Orig_N)
7568 and then Nkind (Orig_N) = N_Type_Conversion
7569 and then not In_Instance
7571 Orig_N := Original_Node (Expression (Orig_N));
7572 Orig_T := Target_Typ;
7574 -- If the node is part of a larger expression, the Target_Type
7575 -- may not be the original type of the node if the context is a
7576 -- condition. Recover original type to see if conversion is needed.
7578 if Is_Boolean_Type (Orig_T)
7579 and then Nkind (Parent (N)) in N_Op
7581 Orig_T := Etype (Parent (N));
7584 if Is_Entity_Name (Orig_N)
7585 and then Etype (Entity (Orig_N)) = Orig_T
7588 ("?useless conversion, & has this type!", N, Entity (Orig_N));
7592 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
7593 -- No need to perform any interface conversion if the type of the
7594 -- expression coincides with the target type.
7596 if Ada_Version >= Ada_05
7597 and then Expander_Active
7598 and then Operand_Typ /= Target_Typ
7601 Opnd : Entity_Id := Operand_Typ;
7602 Target : Entity_Id := Target_Typ;
7605 if Is_Access_Type (Opnd) then
7606 Opnd := Directly_Designated_Type (Opnd);
7609 if Is_Access_Type (Target_Typ) then
7610 Target := Directly_Designated_Type (Target);
7613 if Opnd = Target then
7616 -- Conversion from interface type
7618 elsif Is_Interface (Opnd) then
7620 -- Ada 2005 (AI-217): Handle entities from limited views
7622 if From_With_Type (Opnd) then
7623 Error_Msg_Qual_Level := 99;
7624 Error_Msg_NE ("missing with-clause on package &", N,
7625 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
7627 ("type conversions require visibility of the full view",
7630 elsif From_With_Type (Target)
7632 (Is_Access_Type (Target_Typ)
7633 and then Present (Non_Limited_View (Etype (Target))))
7635 Error_Msg_Qual_Level := 99;
7636 Error_Msg_NE ("missing with-clause on package &", N,
7637 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
7639 ("type conversions require visibility of the full view",
7643 Expand_Interface_Conversion (N, Is_Static => False);
7646 -- Conversion to interface type
7648 elsif Is_Interface (Target) then
7652 if Ekind (Opnd) = E_Protected_Subtype
7653 or else Ekind (Opnd) = E_Task_Subtype
7655 Opnd := Etype (Opnd);
7658 if not Interface_Present_In_Ancestor
7662 if Is_Class_Wide_Type (Opnd) then
7664 -- The static analysis is not enough to know if the
7665 -- interface is implemented or not. Hence we must pass
7666 -- the work to the expander to generate code to evaluate
7667 -- the conversion at run-time.
7669 Expand_Interface_Conversion (N, Is_Static => False);
7672 Error_Msg_Name_1 := Chars (Etype (Target));
7673 Error_Msg_Name_2 := Chars (Opnd);
7675 ("wrong interface conversion (% is not a progenitor " &
7680 Expand_Interface_Conversion (N);
7685 end Resolve_Type_Conversion;
7687 ----------------------
7688 -- Resolve_Unary_Op --
7689 ----------------------
7691 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
7692 B_Typ : constant Entity_Id := Base_Type (Typ);
7693 R : constant Node_Id := Right_Opnd (N);
7699 -- Deal with intrinsic unary operators
7701 if Comes_From_Source (N)
7702 and then Ekind (Entity (N)) = E_Function
7703 and then Is_Imported (Entity (N))
7704 and then Is_Intrinsic_Subprogram (Entity (N))
7706 Resolve_Intrinsic_Unary_Operator (N, Typ);
7710 -- Deal with universal cases
7712 if Etype (R) = Universal_Integer
7714 Etype (R) = Universal_Real
7716 Check_For_Visible_Operator (N, B_Typ);
7719 Set_Etype (N, B_Typ);
7722 -- Generate warning for expressions like abs (x mod 2)
7724 if Warn_On_Redundant_Constructs
7725 and then Nkind (N) = N_Op_Abs
7727 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
7729 if OK and then Hi >= Lo and then Lo >= 0 then
7731 ("?abs applied to known non-negative value has no effect", N);
7735 -- Deal with reference generation
7737 Check_Unset_Reference (R);
7738 Generate_Operator_Reference (N, B_Typ);
7741 -- Set overflow checking bit. Much cleverer code needed here eventually
7742 -- and perhaps the Resolve routines should be separated for the various
7743 -- arithmetic operations, since they will need different processing ???
7745 if Nkind (N) in N_Op then
7746 if not Overflow_Checks_Suppressed (Etype (N)) then
7747 Enable_Overflow_Check (N);
7751 -- Generate warning for expressions like -5 mod 3 for integers. No
7752 -- need to worry in the floating-point case, since parens do not affect
7753 -- the result so there is no point in giving in a warning.
7756 Norig : constant Node_Id := Original_Node (N);
7765 if Warn_On_Questionable_Missing_Parens
7766 and then Comes_From_Source (Norig)
7767 and then Is_Integer_Type (Typ)
7768 and then Nkind (Norig) = N_Op_Minus
7770 Rorig := Original_Node (Right_Opnd (Norig));
7772 -- We are looking for cases where the right operand is not
7773 -- parenthesized, and is a bianry operator, multiply, divide, or
7774 -- mod. These are the cases where the grouping can affect results.
7776 if Paren_Count (Rorig) = 0
7777 and then (Nkind (Rorig) = N_Op_Mod
7779 Nkind (Rorig) = N_Op_Multiply
7781 Nkind (Rorig) = N_Op_Divide)
7783 -- For mod, we always give the warning, since the value is
7784 -- affected by the parenthesization (e.g. (-5) mod 315 /=
7785 -- (5 mod 315)). But for the other cases, the only concern is
7786 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
7787 -- overflows, but (-2) * 64 does not). So we try to give the
7788 -- message only when overflow is possible.
7790 if Nkind (Rorig) /= N_Op_Mod
7791 and then Compile_Time_Known_Value (R)
7793 Val := Expr_Value (R);
7795 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7796 HB := Expr_Value (Type_High_Bound (Typ));
7798 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
7801 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7802 LB := Expr_Value (Type_Low_Bound (Typ));
7804 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
7807 -- Note that the test below is deliberately excluding
7808 -- the largest negative number, since that is a potentially
7809 -- troublesome case (e.g. -2 * x, where the result is the
7810 -- largest negative integer has an overflow with 2 * x).
7812 if Val > LB and then Val <= HB then
7817 -- For the multiplication case, the only case we have to worry
7818 -- about is when (-a)*b is exactly the largest negative number
7819 -- so that -(a*b) can cause overflow. This can only happen if
7820 -- a is a power of 2, and more generally if any operand is a
7821 -- constant that is not a power of 2, then the parentheses
7822 -- cannot affect whether overflow occurs. We only bother to
7823 -- test the left most operand
7825 -- Loop looking at left operands for one that has known value
7828 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
7829 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
7830 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
7832 -- Operand value of 0 or 1 skips warning
7837 -- Otherwise check power of 2, if power of 2, warn, if
7838 -- anything else, skip warning.
7841 while Lval /= 2 loop
7842 if Lval mod 2 = 1 then
7853 -- Keep looking at left operands
7855 Opnd := Left_Opnd (Opnd);
7858 -- For rem or "/" we can only have a problematic situation
7859 -- if the divisor has a value of minus one or one. Otherwise
7860 -- overflow is impossible (divisor > 1) or we have a case of
7861 -- division by zero in any case.
7863 if (Nkind (Rorig) = N_Op_Divide
7865 Nkind (Rorig) = N_Op_Rem)
7866 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
7867 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
7872 -- If we fall through warning should be issued
7875 ("?unary minus expression should be parenthesized here!", N);
7879 end Resolve_Unary_Op;
7881 ----------------------------------
7882 -- Resolve_Unchecked_Expression --
7883 ----------------------------------
7885 procedure Resolve_Unchecked_Expression
7890 Resolve (Expression (N), Typ, Suppress => All_Checks);
7892 end Resolve_Unchecked_Expression;
7894 ---------------------------------------
7895 -- Resolve_Unchecked_Type_Conversion --
7896 ---------------------------------------
7898 procedure Resolve_Unchecked_Type_Conversion
7902 pragma Warnings (Off, Typ);
7904 Operand : constant Node_Id := Expression (N);
7905 Opnd_Type : constant Entity_Id := Etype (Operand);
7908 -- Resolve operand using its own type
7910 Resolve (Operand, Opnd_Type);
7911 Eval_Unchecked_Conversion (N);
7913 end Resolve_Unchecked_Type_Conversion;
7915 ------------------------------
7916 -- Rewrite_Operator_As_Call --
7917 ------------------------------
7919 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
7920 Loc : constant Source_Ptr := Sloc (N);
7921 Actuals : constant List_Id := New_List;
7925 if Nkind (N) in N_Binary_Op then
7926 Append (Left_Opnd (N), Actuals);
7929 Append (Right_Opnd (N), Actuals);
7932 Make_Function_Call (Sloc => Loc,
7933 Name => New_Occurrence_Of (Nam, Loc),
7934 Parameter_Associations => Actuals);
7936 Preserve_Comes_From_Source (New_N, N);
7937 Preserve_Comes_From_Source (Name (New_N), N);
7939 Set_Etype (N, Etype (Nam));
7940 end Rewrite_Operator_As_Call;
7942 ------------------------------
7943 -- Rewrite_Renamed_Operator --
7944 ------------------------------
7946 procedure Rewrite_Renamed_Operator
7951 Nam : constant Name_Id := Chars (Op);
7952 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
7956 -- Rewrite the operator node using the real operator, not its
7957 -- renaming. Exclude user-defined intrinsic operations of the same
7958 -- name, which are treated separately and rewritten as calls.
7960 if Ekind (Op) /= E_Function
7961 or else Chars (N) /= Nam
7963 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
7964 Set_Chars (Op_Node, Nam);
7965 Set_Etype (Op_Node, Etype (N));
7966 Set_Entity (Op_Node, Op);
7967 Set_Right_Opnd (Op_Node, Right_Opnd (N));
7969 -- Indicate that both the original entity and its renaming are
7970 -- referenced at this point.
7972 Generate_Reference (Entity (N), N);
7973 Generate_Reference (Op, N);
7976 Set_Left_Opnd (Op_Node, Left_Opnd (N));
7979 Rewrite (N, Op_Node);
7981 -- If the context type is private, add the appropriate conversions
7982 -- so that the operator is applied to the full view. This is done
7983 -- in the routines that resolve intrinsic operators,
7985 if Is_Intrinsic_Subprogram (Op)
7986 and then Is_Private_Type (Typ)
7989 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
7990 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
7991 Resolve_Intrinsic_Operator (N, Typ);
7993 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
7994 Resolve_Intrinsic_Unary_Operator (N, Typ);
8001 elsif Ekind (Op) = E_Function
8002 and then Is_Intrinsic_Subprogram (Op)
8004 -- Operator renames a user-defined operator of the same name. Use
8005 -- the original operator in the node, which is the one that Gigi
8009 Set_Is_Overloaded (N, False);
8011 end Rewrite_Renamed_Operator;
8013 -----------------------
8014 -- Set_Slice_Subtype --
8015 -----------------------
8017 -- Build an implicit subtype declaration to represent the type delivered
8018 -- by the slice. This is an abbreviated version of an array subtype. We
8019 -- define an index subtype for the slice, using either the subtype name
8020 -- or the discrete range of the slice. To be consistent with index usage
8021 -- elsewhere, we create a list header to hold the single index. This list
8022 -- is not otherwise attached to the syntax tree.
8024 procedure Set_Slice_Subtype (N : Node_Id) is
8025 Loc : constant Source_Ptr := Sloc (N);
8026 Index_List : constant List_Id := New_List;
8028 Index_Subtype : Entity_Id;
8029 Index_Type : Entity_Id;
8030 Slice_Subtype : Entity_Id;
8031 Drange : constant Node_Id := Discrete_Range (N);
8034 if Is_Entity_Name (Drange) then
8035 Index_Subtype := Entity (Drange);
8038 -- We force the evaluation of a range. This is definitely needed in
8039 -- the renamed case, and seems safer to do unconditionally. Note in
8040 -- any case that since we will create and insert an Itype referring
8041 -- to this range, we must make sure any side effect removal actions
8042 -- are inserted before the Itype definition.
8044 if Nkind (Drange) = N_Range then
8045 Force_Evaluation (Low_Bound (Drange));
8046 Force_Evaluation (High_Bound (Drange));
8049 Index_Type := Base_Type (Etype (Drange));
8051 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8053 Set_Scalar_Range (Index_Subtype, Drange);
8054 Set_Etype (Index_Subtype, Index_Type);
8055 Set_Size_Info (Index_Subtype, Index_Type);
8056 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
8059 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
8061 Index := New_Occurrence_Of (Index_Subtype, Loc);
8062 Set_Etype (Index, Index_Subtype);
8063 Append (Index, Index_List);
8065 Set_First_Index (Slice_Subtype, Index);
8066 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
8067 Set_Is_Constrained (Slice_Subtype, True);
8068 Init_Size_Align (Slice_Subtype);
8070 Check_Compile_Time_Size (Slice_Subtype);
8072 -- The Etype of the existing Slice node is reset to this slice subtype.
8073 -- Its bounds are obtained from its first index.
8075 Set_Etype (N, Slice_Subtype);
8077 -- In the packed case, this must be immediately frozen
8079 -- Couldn't we always freeze here??? and if we did, then the above
8080 -- call to Check_Compile_Time_Size could be eliminated, which would
8081 -- be nice, because then that routine could be made private to Freeze.
8083 if Is_Packed (Slice_Subtype) and not In_Default_Expression then
8084 Freeze_Itype (Slice_Subtype, N);
8087 end Set_Slice_Subtype;
8089 --------------------------------
8090 -- Set_String_Literal_Subtype --
8091 --------------------------------
8093 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
8094 Loc : constant Source_Ptr := Sloc (N);
8095 Low_Bound : constant Node_Id :=
8096 Type_Low_Bound (Etype (First_Index (Typ)));
8097 Subtype_Id : Entity_Id;
8100 if Nkind (N) /= N_String_Literal then
8104 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
8105 Set_String_Literal_Length (Subtype_Id, UI_From_Int
8106 (String_Length (Strval (N))));
8107 Set_Etype (Subtype_Id, Base_Type (Typ));
8108 Set_Is_Constrained (Subtype_Id);
8109 Set_Etype (N, Subtype_Id);
8111 if Is_OK_Static_Expression (Low_Bound) then
8113 -- The low bound is set from the low bound of the corresponding
8114 -- index type. Note that we do not store the high bound in the
8115 -- string literal subtype, but it can be deduced if necessary
8116 -- from the length and the low bound.
8118 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
8121 Set_String_Literal_Low_Bound
8122 (Subtype_Id, Make_Integer_Literal (Loc, 1));
8123 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
8125 -- Build bona fide subtype for the string, and wrap it in an
8126 -- unchecked conversion, because the backend expects the
8127 -- String_Literal_Subtype to have a static lower bound.
8130 Index_List : constant List_Id := New_List;
8131 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
8132 High_Bound : constant Node_Id :=
8134 Left_Opnd => New_Copy_Tree (Low_Bound),
8136 Make_Integer_Literal (Loc,
8137 String_Length (Strval (N)) - 1));
8138 Array_Subtype : Entity_Id;
8139 Index_Subtype : Entity_Id;
8145 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8146 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
8147 Set_Scalar_Range (Index_Subtype, Drange);
8148 Set_Parent (Drange, N);
8149 Analyze_And_Resolve (Drange, Index_Type);
8151 Set_Etype (Index_Subtype, Index_Type);
8152 Set_Size_Info (Index_Subtype, Index_Type);
8153 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
8155 Array_Subtype := Create_Itype (E_Array_Subtype, N);
8157 Index := New_Occurrence_Of (Index_Subtype, Loc);
8158 Set_Etype (Index, Index_Subtype);
8159 Append (Index, Index_List);
8161 Set_First_Index (Array_Subtype, Index);
8162 Set_Etype (Array_Subtype, Base_Type (Typ));
8163 Set_Is_Constrained (Array_Subtype, True);
8164 Init_Size_Align (Array_Subtype);
8167 Make_Unchecked_Type_Conversion (Loc,
8168 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
8169 Expression => Relocate_Node (N)));
8170 Set_Etype (N, Array_Subtype);
8173 end Set_String_Literal_Subtype;
8175 ------------------------------
8176 -- Simplify_Type_Conversion --
8177 ------------------------------
8179 procedure Simplify_Type_Conversion (N : Node_Id) is
8181 if Nkind (N) = N_Type_Conversion then
8183 Operand : constant Node_Id := Expression (N);
8184 Target_Typ : constant Entity_Id := Etype (N);
8185 Opnd_Typ : constant Entity_Id := Etype (Operand);
8188 if Is_Floating_Point_Type (Opnd_Typ)
8190 (Is_Integer_Type (Target_Typ)
8191 or else (Is_Fixed_Point_Type (Target_Typ)
8192 and then Conversion_OK (N)))
8193 and then Nkind (Operand) = N_Attribute_Reference
8194 and then Attribute_Name (Operand) = Name_Truncation
8196 -- Special processing required if the conversion is the expression
8197 -- of a Truncation attribute reference. In this case we replace:
8199 -- ityp (ftyp'Truncation (x))
8205 -- with the Float_Truncate flag set, which is more efficient
8209 Relocate_Node (First (Expressions (Operand))));
8210 Set_Float_Truncate (N, True);
8214 end Simplify_Type_Conversion;
8216 -----------------------------
8217 -- Unique_Fixed_Point_Type --
8218 -----------------------------
8220 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
8221 T1 : Entity_Id := Empty;
8226 procedure Fixed_Point_Error;
8227 -- If true ambiguity, give details
8229 -----------------------
8230 -- Fixed_Point_Error --
8231 -----------------------
8233 procedure Fixed_Point_Error is
8235 Error_Msg_N ("ambiguous universal_fixed_expression", N);
8236 Error_Msg_NE ("\\possible interpretation as}", N, T1);
8237 Error_Msg_NE ("\\possible interpretation as}", N, T2);
8238 end Fixed_Point_Error;
8240 -- Start of processing for Unique_Fixed_Point_Type
8243 -- The operations on Duration are visible, so Duration is always a
8244 -- possible interpretation.
8246 T1 := Standard_Duration;
8248 -- Look for fixed-point types in enclosing scopes
8250 Scop := Current_Scope;
8251 while Scop /= Standard_Standard loop
8252 T2 := First_Entity (Scop);
8253 while Present (T2) loop
8254 if Is_Fixed_Point_Type (T2)
8255 and then Current_Entity (T2) = T2
8256 and then Scope (Base_Type (T2)) = Scop
8258 if Present (T1) then
8269 Scop := Scope (Scop);
8272 -- Look for visible fixed type declarations in the context
8274 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
8275 while Present (Item) loop
8276 if Nkind (Item) = N_With_Clause then
8277 Scop := Entity (Name (Item));
8278 T2 := First_Entity (Scop);
8279 while Present (T2) loop
8280 if Is_Fixed_Point_Type (T2)
8281 and then Scope (Base_Type (T2)) = Scop
8282 and then (Is_Potentially_Use_Visible (T2)
8283 or else In_Use (T2))
8285 if Present (T1) then
8300 if Nkind (N) = N_Real_Literal then
8301 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
8304 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
8308 end Unique_Fixed_Point_Type;
8310 ----------------------
8311 -- Valid_Conversion --
8312 ----------------------
8314 function Valid_Conversion
8317 Operand : Node_Id) return Boolean
8319 Target_Type : constant Entity_Id := Base_Type (Target);
8320 Opnd_Type : Entity_Id := Etype (Operand);
8322 function Conversion_Check
8324 Msg : String) return Boolean;
8325 -- Little routine to post Msg if Valid is False, returns Valid value
8327 function Valid_Tagged_Conversion
8328 (Target_Type : Entity_Id;
8329 Opnd_Type : Entity_Id) return Boolean;
8330 -- Specifically test for validity of tagged conversions
8332 function Valid_Array_Conversion return Boolean;
8333 -- Check index and component conformance, and accessibility levels
8334 -- if the component types are anonymous access types (Ada 2005)
8336 ----------------------
8337 -- Conversion_Check --
8338 ----------------------
8340 function Conversion_Check
8342 Msg : String) return Boolean
8346 Error_Msg_N (Msg, Operand);
8350 end Conversion_Check;
8352 ----------------------------
8353 -- Valid_Array_Conversion --
8354 ----------------------------
8356 function Valid_Array_Conversion return Boolean
8358 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
8359 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
8361 Opnd_Index : Node_Id;
8362 Opnd_Index_Type : Entity_Id;
8364 Target_Comp_Type : constant Entity_Id :=
8365 Component_Type (Target_Type);
8366 Target_Comp_Base : constant Entity_Id :=
8367 Base_Type (Target_Comp_Type);
8369 Target_Index : Node_Id;
8370 Target_Index_Type : Entity_Id;
8373 -- Error if wrong number of dimensions
8376 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
8379 ("incompatible number of dimensions for conversion", Operand);
8382 -- Number of dimensions matches
8385 -- Loop through indexes of the two arrays
8387 Target_Index := First_Index (Target_Type);
8388 Opnd_Index := First_Index (Opnd_Type);
8389 while Present (Target_Index) and then Present (Opnd_Index) loop
8390 Target_Index_Type := Etype (Target_Index);
8391 Opnd_Index_Type := Etype (Opnd_Index);
8393 -- Error if index types are incompatible
8395 if not (Is_Integer_Type (Target_Index_Type)
8396 and then Is_Integer_Type (Opnd_Index_Type))
8397 and then (Root_Type (Target_Index_Type)
8398 /= Root_Type (Opnd_Index_Type))
8401 ("incompatible index types for array conversion",
8406 Next_Index (Target_Index);
8407 Next_Index (Opnd_Index);
8410 -- If component types have same base type, all set
8412 if Target_Comp_Base = Opnd_Comp_Base then
8415 -- Here if base types of components are not the same. The only
8416 -- time this is allowed is if we have anonymous access types.
8418 -- The conversion of arrays of anonymous access types can lead
8419 -- to dangling pointers. AI-392 formalizes the accessibility
8420 -- checks that must be applied to such conversions to prevent
8421 -- out-of-scope references.
8424 (Ekind (Target_Comp_Base) = E_Anonymous_Access_Type
8426 Ekind (Target_Comp_Base) = E_Anonymous_Access_Subprogram_Type)
8427 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
8429 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
8431 if Type_Access_Level (Target_Type) <
8432 Type_Access_Level (Opnd_Type)
8434 if In_Instance_Body then
8435 Error_Msg_N ("?source array type " &
8436 "has deeper accessibility level than target", Operand);
8437 Error_Msg_N ("\?Program_Error will be raised at run time",
8440 Make_Raise_Program_Error (Sloc (N),
8441 Reason => PE_Accessibility_Check_Failed));
8442 Set_Etype (N, Target_Type);
8445 -- Conversion not allowed because of accessibility levels
8448 Error_Msg_N ("source array type " &
8449 "has deeper accessibility level than target", Operand);
8456 -- All other cases where component base types do not match
8460 ("incompatible component types for array conversion",
8465 -- Check that component subtypes statically match
8467 if Is_Constrained (Target_Comp_Type) /=
8468 Is_Constrained (Opnd_Comp_Type)
8469 or else not Subtypes_Statically_Match
8470 (Target_Comp_Type, Opnd_Comp_Type)
8473 ("component subtypes must statically match", Operand);
8479 end Valid_Array_Conversion;
8481 -----------------------------
8482 -- Valid_Tagged_Conversion --
8483 -----------------------------
8485 function Valid_Tagged_Conversion
8486 (Target_Type : Entity_Id;
8487 Opnd_Type : Entity_Id) return Boolean
8490 -- Upward conversions are allowed (RM 4.6(22))
8492 if Covers (Target_Type, Opnd_Type)
8493 or else Is_Ancestor (Target_Type, Opnd_Type)
8497 -- Downward conversion are allowed if the operand is class-wide
8500 elsif Is_Class_Wide_Type (Opnd_Type)
8501 and then Covers (Opnd_Type, Target_Type)
8505 elsif Covers (Opnd_Type, Target_Type)
8506 or else Is_Ancestor (Opnd_Type, Target_Type)
8509 Conversion_Check (False,
8510 "downward conversion of tagged objects not allowed");
8512 -- Ada 2005 (AI-251): The conversion to/from interface types is
8515 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
8518 -- If the operand is a class-wide type obtained through a limited_
8519 -- with clause, and the context includes the non-limited view, use
8520 -- it to determine whether the conversion is legal.
8522 elsif Is_Class_Wide_Type (Opnd_Type)
8523 and then From_With_Type (Opnd_Type)
8524 and then Present (Non_Limited_View (Etype (Opnd_Type)))
8525 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
8529 elsif Is_Access_Type (Opnd_Type)
8530 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
8536 ("invalid tagged conversion, not compatible with}",
8537 N, First_Subtype (Opnd_Type));
8540 end Valid_Tagged_Conversion;
8542 -- Start of processing for Valid_Conversion
8545 Check_Parameterless_Call (Operand);
8547 if Is_Overloaded (Operand) then
8556 -- Remove procedure calls, which syntactically cannot appear
8557 -- in this context, but which cannot be removed by type checking,
8558 -- because the context does not impose a type.
8560 -- When compiling for VMS, spurious ambiguities can be produced
8561 -- when arithmetic operations have a literal operand and return
8562 -- System.Address or a descendant of it. These ambiguities are
8563 -- otherwise resolved by the context, but for conversions there
8564 -- is no context type and the removal of the spurious operations
8565 -- must be done explicitly here.
8567 -- The node may be labelled overloaded, but still contain only
8568 -- one interpretation because others were discarded in previous
8569 -- filters. If this is the case, retain the single interpretation
8572 Get_First_Interp (Operand, I, It);
8573 Opnd_Type := It.Typ;
8574 Get_Next_Interp (I, It);
8577 and then Opnd_Type /= Standard_Void_Type
8579 -- More than one candidate interpretation is available
8581 Get_First_Interp (Operand, I, It);
8582 while Present (It.Typ) loop
8583 if It.Typ = Standard_Void_Type then
8587 if Present (System_Aux_Id)
8588 and then Is_Descendent_Of_Address (It.Typ)
8593 Get_Next_Interp (I, It);
8597 Get_First_Interp (Operand, I, It);
8602 Error_Msg_N ("illegal operand in conversion", Operand);
8606 Get_Next_Interp (I, It);
8608 if Present (It.Typ) then
8610 It1 := Disambiguate (Operand, I1, I, Any_Type);
8612 if It1 = No_Interp then
8613 Error_Msg_N ("ambiguous operand in conversion", Operand);
8615 Error_Msg_Sloc := Sloc (It.Nam);
8616 Error_Msg_N ("\\possible interpretation#!", Operand);
8618 Error_Msg_Sloc := Sloc (N1);
8619 Error_Msg_N ("\\possible interpretation#!", Operand);
8625 Set_Etype (Operand, It1.Typ);
8626 Opnd_Type := It1.Typ;
8632 if Is_Numeric_Type (Target_Type) then
8634 -- A universal fixed expression can be converted to any numeric type
8636 if Opnd_Type = Universal_Fixed then
8639 -- Also no need to check when in an instance or inlined body, because
8640 -- the legality has been established when the template was analyzed.
8641 -- Furthermore, numeric conversions may occur where only a private
8642 -- view of the operand type is visible at the instanciation point.
8643 -- This results in a spurious error if we check that the operand type
8644 -- is a numeric type.
8646 -- Note: in a previous version of this unit, the following tests were
8647 -- applied only for generated code (Comes_From_Source set to False),
8648 -- but in fact the test is required for source code as well, since
8649 -- this situation can arise in source code.
8651 elsif In_Instance or else In_Inlined_Body then
8654 -- Otherwise we need the conversion check
8657 return Conversion_Check
8658 (Is_Numeric_Type (Opnd_Type),
8659 "illegal operand for numeric conversion");
8664 elsif Is_Array_Type (Target_Type) then
8665 if not Is_Array_Type (Opnd_Type)
8666 or else Opnd_Type = Any_Composite
8667 or else Opnd_Type = Any_String
8670 ("illegal operand for array conversion", Operand);
8673 return Valid_Array_Conversion;
8676 -- Anonymous access types where target references an interface
8678 elsif (Ekind (Target_Type) = E_General_Access_Type
8680 Ekind (Target_Type) = E_Anonymous_Access_Type)
8681 and then Is_Interface (Directly_Designated_Type (Target_Type))
8683 -- Check the static accessibility rule of 4.6(17). Note that the
8684 -- check is not enforced when within an instance body, since the RM
8685 -- requires such cases to be caught at run time.
8687 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
8688 if Type_Access_Level (Opnd_Type) >
8689 Type_Access_Level (Target_Type)
8691 -- In an instance, this is a run-time check, but one we know
8692 -- will fail, so generate an appropriate warning. The raise
8693 -- will be generated by Expand_N_Type_Conversion.
8695 if In_Instance_Body then
8697 ("?cannot convert local pointer to non-local access type",
8700 ("\?Program_Error will be raised at run time", Operand);
8703 ("cannot convert local pointer to non-local access type",
8708 -- Special accessibility checks are needed in the case of access
8709 -- discriminants declared for a limited type.
8711 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
8712 and then not Is_Local_Anonymous_Access (Opnd_Type)
8714 -- When the operand is a selected access discriminant the check
8715 -- needs to be made against the level of the object denoted by
8716 -- the prefix of the selected name. (Object_Access_Level
8717 -- handles checking the prefix of the operand for this case.)
8719 if Nkind (Operand) = N_Selected_Component
8720 and then Object_Access_Level (Operand) >
8721 Type_Access_Level (Target_Type)
8723 -- In an instance, this is a run-time check, but one we
8724 -- know will fail, so generate an appropriate warning.
8725 -- The raise will be generated by Expand_N_Type_Conversion.
8727 if In_Instance_Body then
8729 ("?cannot convert access discriminant to non-local" &
8730 " access type", Operand);
8732 ("\?Program_Error will be raised at run time", Operand);
8735 ("cannot convert access discriminant to non-local" &
8736 " access type", Operand);
8741 -- The case of a reference to an access discriminant from
8742 -- within a limited type declaration (which will appear as
8743 -- a discriminal) is always illegal because the level of the
8744 -- discriminant is considered to be deeper than any (namable)
8747 if Is_Entity_Name (Operand)
8748 and then not Is_Local_Anonymous_Access (Opnd_Type)
8749 and then (Ekind (Entity (Operand)) = E_In_Parameter
8750 or else Ekind (Entity (Operand)) = E_Constant)
8751 and then Present (Discriminal_Link (Entity (Operand)))
8754 ("discriminant has deeper accessibility level than target",
8763 -- General and anonymous access types
8765 elsif (Ekind (Target_Type) = E_General_Access_Type
8766 or else Ekind (Target_Type) = E_Anonymous_Access_Type)
8769 (Is_Access_Type (Opnd_Type)
8770 and then Ekind (Opnd_Type) /=
8771 E_Access_Subprogram_Type
8772 and then Ekind (Opnd_Type) /=
8773 E_Access_Protected_Subprogram_Type,
8774 "must be an access-to-object type")
8776 if Is_Access_Constant (Opnd_Type)
8777 and then not Is_Access_Constant (Target_Type)
8780 ("access-to-constant operand type not allowed", Operand);
8784 -- Check the static accessibility rule of 4.6(17). Note that the
8785 -- check is not enforced when within an instance body, since the RM
8786 -- requires such cases to be caught at run time.
8788 if Ekind (Target_Type) /= E_Anonymous_Access_Type
8789 or else Is_Local_Anonymous_Access (Target_Type)
8791 if Type_Access_Level (Opnd_Type)
8792 > Type_Access_Level (Target_Type)
8794 -- In an instance, this is a run-time check, but one we
8795 -- know will fail, so generate an appropriate warning.
8796 -- The raise will be generated by Expand_N_Type_Conversion.
8798 if In_Instance_Body then
8800 ("?cannot convert local pointer to non-local access type",
8803 ("\?Program_Error will be raised at run time", Operand);
8807 ("cannot convert local pointer to non-local access type",
8812 -- Special accessibility checks are needed in the case of access
8813 -- discriminants declared for a limited type.
8815 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
8816 and then not Is_Local_Anonymous_Access (Opnd_Type)
8819 -- When the operand is a selected access discriminant the check
8820 -- needs to be made against the level of the object denoted by
8821 -- the prefix of the selected name. (Object_Access_Level
8822 -- handles checking the prefix of the operand for this case.)
8824 if Nkind (Operand) = N_Selected_Component
8825 and then Object_Access_Level (Operand)
8826 > Type_Access_Level (Target_Type)
8828 -- In an instance, this is a run-time check, but one we
8829 -- know will fail, so generate an appropriate warning.
8830 -- The raise will be generated by Expand_N_Type_Conversion.
8832 if In_Instance_Body then
8834 ("?cannot convert access discriminant to non-local" &
8835 " access type", Operand);
8837 ("\?Program_Error will be raised at run time",
8842 ("cannot convert access discriminant to non-local" &
8843 " access type", Operand);
8848 -- The case of a reference to an access discriminant from
8849 -- within a limited type declaration (which will appear as
8850 -- a discriminal) is always illegal because the level of the
8851 -- discriminant is considered to be deeper than any (namable)
8854 if Is_Entity_Name (Operand)
8855 and then (Ekind (Entity (Operand)) = E_In_Parameter
8856 or else Ekind (Entity (Operand)) = E_Constant)
8857 and then Present (Discriminal_Link (Entity (Operand)))
8860 ("discriminant has deeper accessibility level than target",
8868 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
8869 -- Helper function to handle limited views
8871 --------------------------
8872 -- Full_Designated_Type --
8873 --------------------------
8875 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
8876 Desig : constant Entity_Id := Designated_Type (T);
8878 if From_With_Type (Desig)
8879 and then Is_Incomplete_Type (Desig)
8880 and then Present (Non_Limited_View (Desig))
8882 return Non_Limited_View (Desig);
8886 end Full_Designated_Type;
8888 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
8889 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
8891 Same_Base : constant Boolean :=
8892 Base_Type (Target) = Base_Type (Opnd);
8895 if Is_Tagged_Type (Target) then
8896 return Valid_Tagged_Conversion (Target, Opnd);
8899 if not Same_Base then
8901 ("target designated type not compatible with }",
8902 N, Base_Type (Opnd));
8905 -- Ada 2005 AI-384: legality rule is symmetric in both
8906 -- designated types. The conversion is legal (with possible
8907 -- constraint check) if either designated type is
8910 elsif Subtypes_Statically_Match (Target, Opnd)
8912 (Has_Discriminants (Target)
8914 (not Is_Constrained (Opnd)
8915 or else not Is_Constrained (Target)))
8921 ("target designated subtype not compatible with }",
8928 -- Subprogram access types
8930 elsif (Ekind (Target_Type) = E_Access_Subprogram_Type
8932 Ekind (Target_Type) = E_Anonymous_Access_Subprogram_Type)
8933 and then No (Corresponding_Remote_Type (Opnd_Type))
8936 Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
8939 ("illegal attempt to store anonymous access to subprogram",
8942 ("\value has deeper accessibility than any master " &
8946 if Is_Entity_Name (Operand)
8947 and then Ekind (Entity (Operand)) = E_In_Parameter
8950 ("\use named access type for& instead of access parameter",
8951 Operand, Entity (Operand));
8955 -- Check that the designated types are subtype conformant
8957 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
8958 Old_Id => Designated_Type (Opnd_Type),
8961 -- Check the static accessibility rule of 4.6(20)
8963 if Type_Access_Level (Opnd_Type) >
8964 Type_Access_Level (Target_Type)
8967 ("operand type has deeper accessibility level than target",
8970 -- Check that if the operand type is declared in a generic body,
8971 -- then the target type must be declared within that same body
8972 -- (enforces last sentence of 4.6(20)).
8974 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
8976 O_Gen : constant Node_Id :=
8977 Enclosing_Generic_Body (Opnd_Type);
8982 T_Gen := Enclosing_Generic_Body (Target_Type);
8983 while Present (T_Gen) and then T_Gen /= O_Gen loop
8984 T_Gen := Enclosing_Generic_Body (T_Gen);
8987 if T_Gen /= O_Gen then
8989 ("target type must be declared in same generic body"
8990 & " as operand type", N);
8997 -- Remote subprogram access types
8999 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
9000 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
9002 -- It is valid to convert from one RAS type to another provided
9003 -- that their specification statically match.
9005 Check_Subtype_Conformant
9007 Designated_Type (Corresponding_Remote_Type (Target_Type)),
9009 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
9016 elsif Is_Tagged_Type (Target_Type) then
9017 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
9019 -- Types derived from the same root type are convertible
9021 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
9024 -- In an instance or an inlined body, there may be inconsistent
9025 -- views of the same type, or of types derived from a common root.
9027 elsif (In_Instance or In_Inlined_Body)
9029 Root_Type (Underlying_Type (Target_Type)) =
9030 Root_Type (Underlying_Type (Opnd_Type))
9034 -- Special check for common access type error case
9036 elsif Ekind (Target_Type) = E_Access_Type
9037 and then Is_Access_Type (Opnd_Type)
9039 Error_Msg_N ("target type must be general access type!", N);
9040 Error_Msg_NE ("add ALL to }!", N, Target_Type);
9045 Error_Msg_NE ("invalid conversion, not compatible with }",
9050 end Valid_Conversion;