1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Output; use Output;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Elab; use Sem_Elab;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Intr; use Sem_Intr;
66 with Sem_Util; use Sem_Util;
67 with Targparm; use Targparm;
68 with Sem_Type; use Sem_Type;
69 with Sem_Warn; use Sem_Warn;
70 with Sinfo; use Sinfo;
71 with Sinfo.CN; use Sinfo.CN;
72 with Snames; use Snames;
73 with Stand; use Stand;
74 with Stringt; use Stringt;
75 with Style; use Style;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
80 package body Sem_Res is
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
86 -- Second pass (top-down) type checking and overload resolution procedures
87 -- Typ is the type required by context. These procedures propagate the type
88 -- information recursively to the descendants of N. If the node is not
89 -- overloaded, its Etype is established in the first pass. If overloaded,
90 -- the Resolve routines set the correct type. For arith. operators, the
91 -- Etype is the base type of the context.
93 -- Note that Resolve_Attribute is separated off in Sem_Attr
95 function Bad_Unordered_Enumeration_Reference
97 T : Entity_Id) return Boolean;
98 -- Node N contains a potentially dubious reference to type T, either an
99 -- explicit comparison, or an explicit range. This function returns True
100 -- if the type T is an enumeration type for which No pragma Order has been
101 -- given, and the reference N is not in the same extended source unit as
102 -- the declaration of T.
104 procedure Check_Discriminant_Use (N : Node_Id);
105 -- Enforce the restrictions on the use of discriminants when constraining
106 -- a component of a discriminated type (record or concurrent type).
108 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
109 -- Given a node for an operator associated with type T, check that
110 -- the operator is visible. Operators all of whose operands are
111 -- universal must be checked for visibility during resolution
112 -- because their type is not determinable based on their operands.
114 procedure Check_Fully_Declared_Prefix
117 -- Check that the type of the prefix of a dereference is not incomplete
119 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
120 -- Given a call node, N, which is known to occur immediately within the
121 -- subprogram being called, determines whether it is a detectable case of
122 -- an infinite recursion, and if so, outputs appropriate messages. Returns
123 -- True if an infinite recursion is detected, and False otherwise.
125 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
126 -- If the type of the object being initialized uses the secondary stack
127 -- directly or indirectly, create a transient scope for the call to the
128 -- init proc. This is because we do not create transient scopes for the
129 -- initialization of individual components within the init proc itself.
130 -- Could be optimized away perhaps?
132 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
133 -- N is the node for a logical operator. If the operator is predefined, and
134 -- the root type of the operands is Standard.Boolean, then a check is made
135 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
136 -- the style check for Style_Check_Boolean_And_Or.
138 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
139 -- Determine whether E is an access type declared by an access declaration,
140 -- and not an (anonymous) allocator type.
142 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
143 -- Utility to check whether the entity for an operator is a predefined
144 -- operator, in which case the expression is left as an operator in the
145 -- tree (else it is rewritten into a call). An instance of an intrinsic
146 -- conversion operation may be given an operator name, but is not treated
147 -- like an operator. Note that an operator that is an imported back-end
148 -- builtin has convention Intrinsic, but is expected to be rewritten into
149 -- a call, so such an operator is not treated as predefined by this
152 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
153 -- If a default expression in entry call N depends on the discriminants
154 -- of the task, it must be replaced with a reference to the discriminant
155 -- of the task being called.
157 procedure Resolve_Op_Concat_Arg
162 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
163 -- concatenation operator. The operand is either of the array type or of
164 -- the component type. If the operand is an aggregate, and the component
165 -- type is composite, this is ambiguous if component type has aggregates.
167 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
168 -- Does the first part of the work of Resolve_Op_Concat
170 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
171 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
172 -- has been resolved. See Resolve_Op_Concat for details.
174 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
210 function Operator_Kind
212 Is_Binary : Boolean) return Node_Kind;
213 -- Utility to map the name of an operator into the corresponding Node. Used
214 -- by other node rewriting procedures.
216 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
217 -- Resolve actuals of call, and add default expressions for missing ones.
218 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
219 -- called subprogram.
221 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
222 -- Called from Resolve_Call, when the prefix denotes an entry or element
223 -- of entry family. Actuals are resolved as for subprograms, and the node
224 -- is rebuilt as an entry call. Also called for protected operations. Typ
225 -- is the context type, which is used when the operation is a protected
226 -- function with no arguments, and the return value is indexed.
228 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
229 -- A call to a user-defined intrinsic operator is rewritten as a call to
230 -- the corresponding predefined operator, with suitable conversions. Note
231 -- that this applies only for intrinsic operators that denote predefined
232 -- operators, not ones that are intrinsic imports of back-end builtins.
234 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
235 -- Ditto, for unary operators (arithmetic ones and "not" on signed
236 -- integer types for VMS).
238 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
239 -- If an operator node resolves to a call to a user-defined operator,
240 -- rewrite the node as a function call.
242 procedure Make_Call_Into_Operator
246 -- Inverse transformation: if an operator is given in functional notation,
247 -- then after resolving the node, transform into an operator node, so
248 -- that operands are resolved properly. Recall that predefined operators
249 -- do not have a full signature and special resolution rules apply.
251 procedure Rewrite_Renamed_Operator
255 -- An operator can rename another, e.g. in an instantiation. In that
256 -- case, the proper operator node must be constructed and resolved.
258 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
259 -- The String_Literal_Subtype is built for all strings that are not
260 -- operands of a static concatenation operation. If the argument is
261 -- not a N_String_Literal node, then the call has no effect.
263 procedure Set_Slice_Subtype (N : Node_Id);
264 -- Build subtype of array type, with the range specified by the slice
266 procedure Simplify_Type_Conversion (N : Node_Id);
267 -- Called after N has been resolved and evaluated, but before range checks
268 -- have been applied. Currently simplifies a combination of floating-point
269 -- to integer conversion and Truncation attribute.
271 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
272 -- A universal_fixed expression in an universal context is unambiguous if
273 -- there is only one applicable fixed point type. Determining whether there
274 -- is only one requires a search over all visible entities, and happens
275 -- only in very pathological cases (see 6115-006).
277 -------------------------
278 -- Ambiguous_Character --
279 -------------------------
281 procedure Ambiguous_Character (C : Node_Id) is
285 if Nkind (C) = N_Character_Literal then
286 Error_Msg_N ("ambiguous character literal", C);
288 -- First the ones in Standard
290 Error_Msg_N ("\\possible interpretation: Character!", C);
291 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
293 -- Include Wide_Wide_Character in Ada 2005 mode
295 if Ada_Version >= Ada_2005 then
296 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
299 -- Now any other types that match
301 E := Current_Entity (C);
302 while Present (E) loop
303 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
307 end Ambiguous_Character;
309 -------------------------
310 -- Analyze_And_Resolve --
311 -------------------------
313 procedure Analyze_And_Resolve (N : Node_Id) is
317 end Analyze_And_Resolve;
319 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
323 end Analyze_And_Resolve;
325 -- Version withs check(s) suppressed
327 procedure Analyze_And_Resolve
332 Scop : constant Entity_Id := Current_Scope;
335 if Suppress = All_Checks then
337 Svg : constant Suppress_Array := Scope_Suppress;
339 Scope_Suppress := (others => True);
340 Analyze_And_Resolve (N, Typ);
341 Scope_Suppress := Svg;
346 Svg : constant Boolean := Scope_Suppress (Suppress);
349 Scope_Suppress (Suppress) := True;
350 Analyze_And_Resolve (N, Typ);
351 Scope_Suppress (Suppress) := Svg;
355 if Current_Scope /= Scop
356 and then Scope_Is_Transient
358 -- This can only happen if a transient scope was created for an inner
359 -- expression, which will be removed upon completion of the analysis
360 -- of an enclosing construct. The transient scope must have the
361 -- suppress status of the enclosing environment, not of this Analyze
364 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
367 end Analyze_And_Resolve;
369 procedure Analyze_And_Resolve
373 Scop : constant Entity_Id := Current_Scope;
376 if Suppress = All_Checks then
378 Svg : constant Suppress_Array := Scope_Suppress;
380 Scope_Suppress := (others => True);
381 Analyze_And_Resolve (N);
382 Scope_Suppress := Svg;
387 Svg : constant Boolean := Scope_Suppress (Suppress);
390 Scope_Suppress (Suppress) := True;
391 Analyze_And_Resolve (N);
392 Scope_Suppress (Suppress) := Svg;
396 if Current_Scope /= Scop
397 and then Scope_Is_Transient
399 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
402 end Analyze_And_Resolve;
404 ----------------------------------------
405 -- Bad_Unordered_Enumeration_Reference --
406 ----------------------------------------
408 function Bad_Unordered_Enumeration_Reference
410 T : Entity_Id) return Boolean
413 return Is_Enumeration_Type (T)
414 and then Comes_From_Source (N)
415 and then Warn_On_Unordered_Enumeration_Type
416 and then not Has_Pragma_Ordered (T)
417 and then not In_Same_Extended_Unit (N, T);
418 end Bad_Unordered_Enumeration_Reference;
420 ----------------------------
421 -- Check_Discriminant_Use --
422 ----------------------------
424 procedure Check_Discriminant_Use (N : Node_Id) is
425 PN : constant Node_Id := Parent (N);
426 Disc : constant Entity_Id := Entity (N);
431 -- Any use in a spec-expression is legal
433 if In_Spec_Expression then
436 elsif Nkind (PN) = N_Range then
438 -- Discriminant cannot be used to constrain a scalar type
442 if Nkind (P) = N_Range_Constraint
443 and then Nkind (Parent (P)) = N_Subtype_Indication
444 and then Nkind (Parent (Parent (P))) = N_Component_Definition
446 Error_Msg_N ("discriminant cannot constrain scalar type", N);
448 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
450 -- The following check catches the unusual case where a
451 -- discriminant appears within an index constraint that is part of
452 -- a larger expression within a constraint on a component, e.g. "C
453 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
454 -- of record components, and note that a similar check should also
455 -- apply in the case of discriminant constraints below. ???
457 -- Note that the check for N_Subtype_Declaration below is to
458 -- detect the valid use of discriminants in the constraints of a
459 -- subtype declaration when this subtype declaration appears
460 -- inside the scope of a record type (which is syntactically
461 -- illegal, but which may be created as part of derived type
462 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
465 if Ekind (Current_Scope) = E_Record_Type
466 and then Scope (Disc) = Current_Scope
468 (Nkind (Parent (P)) = N_Subtype_Indication
470 Nkind_In (Parent (Parent (P)), N_Component_Definition,
471 N_Subtype_Declaration)
472 and then Paren_Count (N) = 0)
475 ("discriminant must appear alone in component constraint", N);
479 -- Detect a common error:
481 -- type R (D : Positive := 100) is record
482 -- Name : String (1 .. D);
485 -- The default value causes an object of type R to be allocated
486 -- with room for Positive'Last characters. The RM does not mandate
487 -- the allocation of the maximum size, but that is what GNAT does
488 -- so we should warn the programmer that there is a problem.
490 Check_Large : declare
496 function Large_Storage_Type (T : Entity_Id) return Boolean;
497 -- Return True if type T has a large enough range that any
498 -- array whose index type covered the whole range of the type
499 -- would likely raise Storage_Error.
501 ------------------------
502 -- Large_Storage_Type --
503 ------------------------
505 function Large_Storage_Type (T : Entity_Id) return Boolean is
507 -- The type is considered large if its bounds are known at
508 -- compile time and if it requires at least as many bits as
509 -- a Positive to store the possible values.
511 return Compile_Time_Known_Value (Type_Low_Bound (T))
512 and then Compile_Time_Known_Value (Type_High_Bound (T))
514 Minimum_Size (T, Biased => True) >=
515 RM_Size (Standard_Positive);
516 end Large_Storage_Type;
518 -- Start of processing for Check_Large
521 -- Check that the Disc has a large range
523 if not Large_Storage_Type (Etype (Disc)) then
527 -- If the enclosing type is limited, we allocate only the
528 -- default value, not the maximum, and there is no need for
531 if Is_Limited_Type (Scope (Disc)) then
535 -- Check that it is the high bound
537 if N /= High_Bound (PN)
538 or else No (Discriminant_Default_Value (Disc))
543 -- Check the array allows a large range at this bound. First
548 if Nkind (SI) /= N_Subtype_Indication then
552 T := Entity (Subtype_Mark (SI));
554 if not Is_Array_Type (T) then
558 -- Next, find the dimension
560 TB := First_Index (T);
561 CB := First (Constraints (P));
563 and then Present (TB)
564 and then Present (CB)
575 -- Now, check the dimension has a large range
577 if not Large_Storage_Type (Etype (TB)) then
581 -- Warn about the danger
584 ("?creation of & object may raise Storage_Error!",
593 -- Legal case is in index or discriminant constraint
595 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
596 N_Discriminant_Association)
598 if Paren_Count (N) > 0 then
600 ("discriminant in constraint must appear alone", N);
602 elsif Nkind (N) = N_Expanded_Name
603 and then Comes_From_Source (N)
606 ("discriminant must appear alone as a direct name", N);
611 -- Otherwise, context is an expression. It should not be within (i.e. a
612 -- subexpression of) a constraint for a component.
617 while not Nkind_In (P, N_Component_Declaration,
618 N_Subtype_Indication,
626 -- If the discriminant is used in an expression that is a bound of a
627 -- scalar type, an Itype is created and the bounds are attached to
628 -- its range, not to the original subtype indication. Such use is of
629 -- course a double fault.
631 if (Nkind (P) = N_Subtype_Indication
632 and then Nkind_In (Parent (P), N_Component_Definition,
633 N_Derived_Type_Definition)
634 and then D = Constraint (P))
636 -- The constraint itself may be given by a subtype indication,
637 -- rather than by a more common discrete range.
639 or else (Nkind (P) = N_Subtype_Indication
641 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
642 or else Nkind (P) = N_Entry_Declaration
643 or else Nkind (D) = N_Defining_Identifier
646 ("discriminant in constraint must appear alone", N);
649 end Check_Discriminant_Use;
651 --------------------------------
652 -- Check_For_Visible_Operator --
653 --------------------------------
655 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
657 if Is_Invisible_Operator (N, T) then
658 Error_Msg_NE -- CODEFIX
659 ("operator for} is not directly visible!", N, First_Subtype (T));
660 Error_Msg_N -- CODEFIX
661 ("use clause would make operation legal!", N);
663 end Check_For_Visible_Operator;
665 ----------------------------------
666 -- Check_Fully_Declared_Prefix --
667 ----------------------------------
669 procedure Check_Fully_Declared_Prefix
674 -- Check that the designated type of the prefix of a dereference is
675 -- not an incomplete type. This cannot be done unconditionally, because
676 -- dereferences of private types are legal in default expressions. This
677 -- case is taken care of in Check_Fully_Declared, called below. There
678 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
680 -- This consideration also applies to similar checks for allocators,
681 -- qualified expressions, and type conversions.
683 -- An additional exception concerns other per-object expressions that
684 -- are not directly related to component declarations, in particular
685 -- representation pragmas for tasks. These will be per-object
686 -- expressions if they depend on discriminants or some global entity.
687 -- If the task has access discriminants, the designated type may be
688 -- incomplete at the point the expression is resolved. This resolution
689 -- takes place within the body of the initialization procedure, where
690 -- the discriminant is replaced by its discriminal.
692 if Is_Entity_Name (Pref)
693 and then Ekind (Entity (Pref)) = E_In_Parameter
697 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
698 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
699 -- Analyze_Object_Renaming, and Freeze_Entity.
701 elsif Ada_Version >= Ada_2005
702 and then Is_Entity_Name (Pref)
703 and then Is_Access_Type (Etype (Pref))
704 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
706 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
710 Check_Fully_Declared (Typ, Parent (Pref));
712 end Check_Fully_Declared_Prefix;
714 ------------------------------
715 -- Check_Infinite_Recursion --
716 ------------------------------
718 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
722 function Same_Argument_List return Boolean;
723 -- Check whether list of actuals is identical to list of formals of
724 -- called function (which is also the enclosing scope).
726 ------------------------
727 -- Same_Argument_List --
728 ------------------------
730 function Same_Argument_List return Boolean is
736 if not Is_Entity_Name (Name (N)) then
739 Subp := Entity (Name (N));
742 F := First_Formal (Subp);
743 A := First_Actual (N);
744 while Present (F) and then Present (A) loop
745 if not Is_Entity_Name (A)
746 or else Entity (A) /= F
756 end Same_Argument_List;
758 -- Start of processing for Check_Infinite_Recursion
761 -- Special case, if this is a procedure call and is a call to the
762 -- current procedure with the same argument list, then this is for
763 -- sure an infinite recursion and we insert a call to raise SE.
765 if Is_List_Member (N)
766 and then List_Length (List_Containing (N)) = 1
767 and then Same_Argument_List
770 P : constant Node_Id := Parent (N);
772 if Nkind (P) = N_Handled_Sequence_Of_Statements
773 and then Nkind (Parent (P)) = N_Subprogram_Body
774 and then Is_Empty_List (Declarations (Parent (P)))
776 Error_Msg_N ("!?infinite recursion", N);
777 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
779 Make_Raise_Storage_Error (Sloc (N),
780 Reason => SE_Infinite_Recursion));
786 -- If not that special case, search up tree, quitting if we reach a
787 -- construct (e.g. a conditional) that tells us that this is not a
788 -- case for an infinite recursion warning.
794 -- If no parent, then we were not inside a subprogram, this can for
795 -- example happen when processing certain pragmas in a spec. Just
796 -- return False in this case.
802 -- Done if we get to subprogram body, this is definitely an infinite
803 -- recursion case if we did not find anything to stop us.
805 exit when Nkind (P) = N_Subprogram_Body;
807 -- If appearing in conditional, result is false
809 if Nkind_In (P, N_Or_Else,
813 N_Conditional_Expression,
818 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
819 and then C /= First (Statements (P))
821 -- If the call is the expression of a return statement and the
822 -- actuals are identical to the formals, it's worth a warning.
823 -- However, we skip this if there is an immediately preceding
824 -- raise statement, since the call is never executed.
826 -- Furthermore, this corresponds to a common idiom:
828 -- function F (L : Thing) return Boolean is
830 -- raise Program_Error;
834 -- for generating a stub function
836 if Nkind (Parent (N)) = N_Simple_Return_Statement
837 and then Same_Argument_List
839 exit when not Is_List_Member (Parent (N));
841 -- OK, return statement is in a statement list, look for raise
847 -- Skip past N_Freeze_Entity nodes generated by expansion
849 Nod := Prev (Parent (N));
851 and then Nkind (Nod) = N_Freeze_Entity
856 -- If no raise statement, give warning
858 exit when Nkind (Nod) /= N_Raise_Statement
860 (Nkind (Nod) not in N_Raise_xxx_Error
861 or else Present (Condition (Nod)));
872 Error_Msg_N ("!?possible infinite recursion", N);
873 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
876 end Check_Infinite_Recursion;
878 -------------------------------
879 -- Check_Initialization_Call --
880 -------------------------------
882 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
883 Typ : constant Entity_Id := Etype (First_Formal (Nam));
885 function Uses_SS (T : Entity_Id) return Boolean;
886 -- Check whether the creation of an object of the type will involve
887 -- use of the secondary stack. If T is a record type, this is true
888 -- if the expression for some component uses the secondary stack, e.g.
889 -- through a call to a function that returns an unconstrained value.
890 -- False if T is controlled, because cleanups occur elsewhere.
896 function Uses_SS (T : Entity_Id) return Boolean is
899 Full_Type : Entity_Id := Underlying_Type (T);
902 -- Normally we want to use the underlying type, but if it's not set
903 -- then continue with T.
905 if not Present (Full_Type) then
909 if Is_Controlled (Full_Type) then
912 elsif Is_Array_Type (Full_Type) then
913 return Uses_SS (Component_Type (Full_Type));
915 elsif Is_Record_Type (Full_Type) then
916 Comp := First_Component (Full_Type);
917 while Present (Comp) loop
918 if Ekind (Comp) = E_Component
919 and then Nkind (Parent (Comp)) = N_Component_Declaration
921 -- The expression for a dynamic component may be rewritten
922 -- as a dereference, so retrieve original node.
924 Expr := Original_Node (Expression (Parent (Comp)));
926 -- Return True if the expression is a call to a function
927 -- (including an attribute function such as Image, or a
928 -- user-defined operator) with a result that requires a
931 if (Nkind (Expr) = N_Function_Call
932 or else Nkind (Expr) in N_Op
933 or else (Nkind (Expr) = N_Attribute_Reference
934 and then Present (Expressions (Expr))))
935 and then Requires_Transient_Scope (Etype (Expr))
939 elsif Uses_SS (Etype (Comp)) then
944 Next_Component (Comp);
954 -- Start of processing for Check_Initialization_Call
957 -- Establish a transient scope if the type needs it
959 if Uses_SS (Typ) then
960 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
962 end Check_Initialization_Call;
964 ---------------------------------------
965 -- Check_No_Direct_Boolean_Operators --
966 ---------------------------------------
968 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
970 if Scope (Entity (N)) = Standard_Standard
971 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
973 -- Restriction only applies to original source code
975 if Comes_From_Source (N) then
976 Check_Restriction (No_Direct_Boolean_Operators, N);
981 Check_Boolean_Operator (N);
983 end Check_No_Direct_Boolean_Operators;
985 ------------------------------
986 -- Check_Parameterless_Call --
987 ------------------------------
989 procedure Check_Parameterless_Call (N : Node_Id) is
992 function Prefix_Is_Access_Subp return Boolean;
993 -- If the prefix is of an access_to_subprogram type, the node must be
994 -- rewritten as a call. Ditto if the prefix is overloaded and all its
995 -- interpretations are access to subprograms.
997 ---------------------------
998 -- Prefix_Is_Access_Subp --
999 ---------------------------
1001 function Prefix_Is_Access_Subp return Boolean is
1006 -- If the context is an attribute reference that can apply to
1007 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1009 if Nkind (Parent (N)) = N_Attribute_Reference
1010 and then (Attribute_Name (Parent (N)) = Name_Address or else
1011 Attribute_Name (Parent (N)) = Name_Code_Address or else
1012 Attribute_Name (Parent (N)) = Name_Access)
1017 if not Is_Overloaded (N) then
1019 Ekind (Etype (N)) = E_Subprogram_Type
1020 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1022 Get_First_Interp (N, I, It);
1023 while Present (It.Typ) loop
1024 if Ekind (It.Typ) /= E_Subprogram_Type
1025 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1030 Get_Next_Interp (I, It);
1035 end Prefix_Is_Access_Subp;
1037 -- Start of processing for Check_Parameterless_Call
1040 -- Defend against junk stuff if errors already detected
1042 if Total_Errors_Detected /= 0 then
1043 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1045 elsif Nkind (N) in N_Has_Chars
1046 and then Chars (N) in Error_Name_Or_No_Name
1054 -- If the context expects a value, and the name is a procedure, this is
1055 -- most likely a missing 'Access. Don't try to resolve the parameterless
1056 -- call, error will be caught when the outer call is analyzed.
1058 if Is_Entity_Name (N)
1059 and then Ekind (Entity (N)) = E_Procedure
1060 and then not Is_Overloaded (N)
1062 Nkind_In (Parent (N), N_Parameter_Association,
1064 N_Procedure_Call_Statement)
1069 -- Rewrite as call if overloadable entity that is (or could be, in the
1070 -- overloaded case) a function call. If we know for sure that the entity
1071 -- is an enumeration literal, we do not rewrite it.
1073 -- If the entity is the name of an operator, it cannot be a call because
1074 -- operators cannot have default parameters. In this case, this must be
1075 -- a string whose contents coincide with an operator name. Set the kind
1076 -- of the node appropriately.
1078 if (Is_Entity_Name (N)
1079 and then Nkind (N) /= N_Operator_Symbol
1080 and then Is_Overloadable (Entity (N))
1081 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1082 or else Is_Overloaded (N)))
1084 -- Rewrite as call if it is an explicit dereference of an expression of
1085 -- a subprogram access type, and the subprogram type is not that of a
1086 -- procedure or entry.
1089 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1091 -- Rewrite as call if it is a selected component which is a function,
1092 -- this is the case of a call to a protected function (which may be
1093 -- overloaded with other protected operations).
1096 (Nkind (N) = N_Selected_Component
1097 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1099 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1101 and then Is_Overloaded (Selector_Name (N)))))
1103 -- If one of the above three conditions is met, rewrite as call. Apply
1104 -- the rewriting only once.
1107 if Nkind (Parent (N)) /= N_Function_Call
1108 or else N /= Name (Parent (N))
1111 -- This may be a prefixed call that was not fully analyzed, e.g.
1112 -- an actual in an instance.
1114 if Ada_Version >= Ada_2005
1115 and then Nkind (N) = N_Selected_Component
1116 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1118 Analyze_Selected_Component (N);
1120 if Nkind (N) /= N_Selected_Component then
1125 Nam := New_Copy (N);
1127 -- If overloaded, overload set belongs to new copy
1129 Save_Interps (N, Nam);
1131 -- Change node to parameterless function call (note that the
1132 -- Parameter_Associations associations field is left set to Empty,
1133 -- its normal default value since there are no parameters)
1135 Change_Node (N, N_Function_Call);
1137 Set_Sloc (N, Sloc (Nam));
1141 elsif Nkind (N) = N_Parameter_Association then
1142 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1144 elsif Nkind (N) = N_Operator_Symbol then
1145 Change_Operator_Symbol_To_String_Literal (N);
1146 Set_Is_Overloaded (N, False);
1147 Set_Etype (N, Any_String);
1149 end Check_Parameterless_Call;
1151 -----------------------------
1152 -- Is_Definite_Access_Type --
1153 -----------------------------
1155 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1156 Btyp : constant Entity_Id := Base_Type (E);
1158 return Ekind (Btyp) = E_Access_Type
1159 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1160 and then Comes_From_Source (Btyp));
1161 end Is_Definite_Access_Type;
1163 ----------------------
1164 -- Is_Predefined_Op --
1165 ----------------------
1167 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1169 -- Predefined operators are intrinsic subprograms
1171 if not Is_Intrinsic_Subprogram (Nam) then
1175 -- A call to a back-end builtin is never a predefined operator
1177 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1181 return not Is_Generic_Instance (Nam)
1182 and then Chars (Nam) in Any_Operator_Name
1183 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1184 end Is_Predefined_Op;
1186 -----------------------------
1187 -- Make_Call_Into_Operator --
1188 -----------------------------
1190 procedure Make_Call_Into_Operator
1195 Op_Name : constant Name_Id := Chars (Op_Id);
1196 Act1 : Node_Id := First_Actual (N);
1197 Act2 : Node_Id := Next_Actual (Act1);
1198 Error : Boolean := False;
1199 Func : constant Entity_Id := Entity (Name (N));
1200 Is_Binary : constant Boolean := Present (Act2);
1202 Opnd_Type : Entity_Id;
1203 Orig_Type : Entity_Id := Empty;
1206 type Kind_Test is access function (E : Entity_Id) return Boolean;
1208 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1209 -- If the operand is not universal, and the operator is given by an
1210 -- expanded name, verify that the operand has an interpretation with a
1211 -- type defined in the given scope of the operator.
1213 function Type_In_P (Test : Kind_Test) return Entity_Id;
1214 -- Find a type of the given class in package Pack that contains the
1217 ---------------------------
1218 -- Operand_Type_In_Scope --
1219 ---------------------------
1221 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1222 Nod : constant Node_Id := Right_Opnd (Op_Node);
1227 if not Is_Overloaded (Nod) then
1228 return Scope (Base_Type (Etype (Nod))) = S;
1231 Get_First_Interp (Nod, I, It);
1232 while Present (It.Typ) loop
1233 if Scope (Base_Type (It.Typ)) = S then
1237 Get_Next_Interp (I, It);
1242 end Operand_Type_In_Scope;
1248 function Type_In_P (Test : Kind_Test) return Entity_Id is
1251 function In_Decl return Boolean;
1252 -- Verify that node is not part of the type declaration for the
1253 -- candidate type, which would otherwise be invisible.
1259 function In_Decl return Boolean is
1260 Decl_Node : constant Node_Id := Parent (E);
1266 if Etype (E) = Any_Type then
1269 elsif No (Decl_Node) then
1274 and then Nkind (N2) /= N_Compilation_Unit
1276 if N2 = Decl_Node then
1287 -- Start of processing for Type_In_P
1290 -- If the context type is declared in the prefix package, this is the
1291 -- desired base type.
1293 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1294 return Base_Type (Typ);
1297 E := First_Entity (Pack);
1298 while Present (E) loop
1300 and then not In_Decl
1312 -- Start of processing for Make_Call_Into_Operator
1315 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1320 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1321 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1322 Save_Interps (Act1, Left_Opnd (Op_Node));
1323 Save_Interps (Act2, Right_Opnd (Op_Node));
1324 Act1 := Left_Opnd (Op_Node);
1325 Act2 := Right_Opnd (Op_Node);
1330 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1331 Save_Interps (Act1, Right_Opnd (Op_Node));
1332 Act1 := Right_Opnd (Op_Node);
1335 -- If the operator is denoted by an expanded name, and the prefix is
1336 -- not Standard, but the operator is a predefined one whose scope is
1337 -- Standard, then this is an implicit_operator, inserted as an
1338 -- interpretation by the procedure of the same name. This procedure
1339 -- overestimates the presence of implicit operators, because it does
1340 -- not examine the type of the operands. Verify now that the operand
1341 -- type appears in the given scope. If right operand is universal,
1342 -- check the other operand. In the case of concatenation, either
1343 -- argument can be the component type, so check the type of the result.
1344 -- If both arguments are literals, look for a type of the right kind
1345 -- defined in the given scope. This elaborate nonsense is brought to
1346 -- you courtesy of b33302a. The type itself must be frozen, so we must
1347 -- find the type of the proper class in the given scope.
1349 -- A final wrinkle is the multiplication operator for fixed point types,
1350 -- which is defined in Standard only, and not in the scope of the
1351 -- fixed point type itself.
1353 if Nkind (Name (N)) = N_Expanded_Name then
1354 Pack := Entity (Prefix (Name (N)));
1356 -- If the entity being called is defined in the given package, it is
1357 -- a renaming of a predefined operator, and known to be legal.
1359 if Scope (Entity (Name (N))) = Pack
1360 and then Pack /= Standard_Standard
1364 -- Visibility does not need to be checked in an instance: if the
1365 -- operator was not visible in the generic it has been diagnosed
1366 -- already, else there is an implicit copy of it in the instance.
1368 elsif In_Instance then
1371 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1372 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1373 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1375 if Pack /= Standard_Standard then
1379 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1382 elsif Ada_Version >= Ada_2005
1383 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1384 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1389 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1391 if Op_Name = Name_Op_Concat then
1392 Opnd_Type := Base_Type (Typ);
1394 elsif (Scope (Opnd_Type) = Standard_Standard
1396 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1398 and then not Comes_From_Source (Opnd_Type))
1400 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1403 if Scope (Opnd_Type) = Standard_Standard then
1405 -- Verify that the scope contains a type that corresponds to
1406 -- the given literal. Optimize the case where Pack is Standard.
1408 if Pack /= Standard_Standard then
1410 if Opnd_Type = Universal_Integer then
1411 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1413 elsif Opnd_Type = Universal_Real then
1414 Orig_Type := Type_In_P (Is_Real_Type'Access);
1416 elsif Opnd_Type = Any_String then
1417 Orig_Type := Type_In_P (Is_String_Type'Access);
1419 elsif Opnd_Type = Any_Access then
1420 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1422 elsif Opnd_Type = Any_Composite then
1423 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1425 if Present (Orig_Type) then
1426 if Has_Private_Component (Orig_Type) then
1429 Set_Etype (Act1, Orig_Type);
1432 Set_Etype (Act2, Orig_Type);
1441 Error := No (Orig_Type);
1444 elsif Ekind (Opnd_Type) = E_Allocator_Type
1445 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1449 -- If the type is defined elsewhere, and the operator is not
1450 -- defined in the given scope (by a renaming declaration, e.g.)
1451 -- then this is an error as well. If an extension of System is
1452 -- present, and the type may be defined there, Pack must be
1455 elsif Scope (Opnd_Type) /= Pack
1456 and then Scope (Op_Id) /= Pack
1457 and then (No (System_Aux_Id)
1458 or else Scope (Opnd_Type) /= System_Aux_Id
1459 or else Pack /= Scope (System_Aux_Id))
1461 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1464 Error := not Operand_Type_In_Scope (Pack);
1467 elsif Pack = Standard_Standard
1468 and then not Operand_Type_In_Scope (Standard_Standard)
1475 Error_Msg_Node_2 := Pack;
1477 ("& not declared in&", N, Selector_Name (Name (N)));
1478 Set_Etype (N, Any_Type);
1481 -- Detect a mismatch between the context type and the result type
1482 -- in the named package, which is otherwise not detected if the
1483 -- operands are universal. Check is only needed if source entity is
1484 -- an operator, not a function that renames an operator.
1486 elsif Nkind (Parent (N)) /= N_Type_Conversion
1487 and then Ekind (Entity (Name (N))) = E_Operator
1488 and then Is_Numeric_Type (Typ)
1489 and then not Is_Universal_Numeric_Type (Typ)
1490 and then Scope (Base_Type (Typ)) /= Pack
1491 and then not In_Instance
1493 if Is_Fixed_Point_Type (Typ)
1494 and then (Op_Name = Name_Op_Multiply
1496 Op_Name = Name_Op_Divide)
1498 -- Already checked above
1502 -- Operator may be defined in an extension of System
1504 elsif Present (System_Aux_Id)
1505 and then Scope (Opnd_Type) = System_Aux_Id
1510 -- Could we use Wrong_Type here??? (this would require setting
1511 -- Etype (N) to the actual type found where Typ was expected).
1513 Error_Msg_NE ("expect }", N, Typ);
1518 Set_Chars (Op_Node, Op_Name);
1520 if not Is_Private_Type (Etype (N)) then
1521 Set_Etype (Op_Node, Base_Type (Etype (N)));
1523 Set_Etype (Op_Node, Etype (N));
1526 -- If this is a call to a function that renames a predefined equality,
1527 -- the renaming declaration provides a type that must be used to
1528 -- resolve the operands. This must be done now because resolution of
1529 -- the equality node will not resolve any remaining ambiguity, and it
1530 -- assumes that the first operand is not overloaded.
1532 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1533 and then Ekind (Func) = E_Function
1534 and then Is_Overloaded (Act1)
1536 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1537 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1540 Set_Entity (Op_Node, Op_Id);
1541 Generate_Reference (Op_Id, N, ' ');
1543 -- Do rewrite setting Comes_From_Source on the result if the original
1544 -- call came from source. Although it is not strictly the case that the
1545 -- operator as such comes from the source, logically it corresponds
1546 -- exactly to the function call in the source, so it should be marked
1547 -- this way (e.g. to make sure that validity checks work fine).
1550 CS : constant Boolean := Comes_From_Source (N);
1552 Rewrite (N, Op_Node);
1553 Set_Comes_From_Source (N, CS);
1556 -- If this is an arithmetic operator and the result type is private,
1557 -- the operands and the result must be wrapped in conversion to
1558 -- expose the underlying numeric type and expand the proper checks,
1559 -- e.g. on division.
1561 if Is_Private_Type (Typ) then
1563 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1564 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1565 Resolve_Intrinsic_Operator (N, Typ);
1567 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1568 Resolve_Intrinsic_Unary_Operator (N, Typ);
1576 end Make_Call_Into_Operator;
1582 function Operator_Kind
1584 Is_Binary : Boolean) return Node_Kind
1589 -- Use CASE statement or array???
1592 if Op_Name = Name_Op_And then
1594 elsif Op_Name = Name_Op_Or then
1596 elsif Op_Name = Name_Op_Xor then
1598 elsif Op_Name = Name_Op_Eq then
1600 elsif Op_Name = Name_Op_Ne then
1602 elsif Op_Name = Name_Op_Lt then
1604 elsif Op_Name = Name_Op_Le then
1606 elsif Op_Name = Name_Op_Gt then
1608 elsif Op_Name = Name_Op_Ge then
1610 elsif Op_Name = Name_Op_Add then
1612 elsif Op_Name = Name_Op_Subtract then
1613 Kind := N_Op_Subtract;
1614 elsif Op_Name = Name_Op_Concat then
1615 Kind := N_Op_Concat;
1616 elsif Op_Name = Name_Op_Multiply then
1617 Kind := N_Op_Multiply;
1618 elsif Op_Name = Name_Op_Divide then
1619 Kind := N_Op_Divide;
1620 elsif Op_Name = Name_Op_Mod then
1622 elsif Op_Name = Name_Op_Rem then
1624 elsif Op_Name = Name_Op_Expon then
1627 raise Program_Error;
1633 if Op_Name = Name_Op_Add then
1635 elsif Op_Name = Name_Op_Subtract then
1637 elsif Op_Name = Name_Op_Abs then
1639 elsif Op_Name = Name_Op_Not then
1642 raise Program_Error;
1649 ----------------------------
1650 -- Preanalyze_And_Resolve --
1651 ----------------------------
1653 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1654 Save_Full_Analysis : constant Boolean := Full_Analysis;
1657 Full_Analysis := False;
1658 Expander_Mode_Save_And_Set (False);
1660 -- We suppress all checks for this analysis, since the checks will
1661 -- be applied properly, and in the right location, when the default
1662 -- expression is reanalyzed and reexpanded later on.
1664 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1666 Expander_Mode_Restore;
1667 Full_Analysis := Save_Full_Analysis;
1668 end Preanalyze_And_Resolve;
1670 -- Version without context type
1672 procedure Preanalyze_And_Resolve (N : Node_Id) is
1673 Save_Full_Analysis : constant Boolean := Full_Analysis;
1676 Full_Analysis := False;
1677 Expander_Mode_Save_And_Set (False);
1680 Resolve (N, Etype (N), Suppress => All_Checks);
1682 Expander_Mode_Restore;
1683 Full_Analysis := Save_Full_Analysis;
1684 end Preanalyze_And_Resolve;
1686 ----------------------------------
1687 -- Replace_Actual_Discriminants --
1688 ----------------------------------
1690 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1691 Loc : constant Source_Ptr := Sloc (N);
1692 Tsk : Node_Id := Empty;
1694 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1695 -- Comment needed???
1701 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1705 if Nkind (Nod) = N_Identifier then
1706 Ent := Entity (Nod);
1709 and then Ekind (Ent) = E_Discriminant
1712 Make_Selected_Component (Loc,
1713 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1714 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1716 Set_Etype (Nod, Etype (Ent));
1724 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1726 -- Start of processing for Replace_Actual_Discriminants
1729 if not Full_Expander_Active then
1733 if Nkind (Name (N)) = N_Selected_Component then
1734 Tsk := Prefix (Name (N));
1736 elsif Nkind (Name (N)) = N_Indexed_Component then
1737 Tsk := Prefix (Prefix (Name (N)));
1743 Replace_Discrs (Default);
1745 end Replace_Actual_Discriminants;
1751 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1752 Ambiguous : Boolean := False;
1753 Ctx_Type : Entity_Id := Typ;
1754 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1755 Err_Type : Entity_Id := Empty;
1756 Found : Boolean := False;
1759 I1 : Interp_Index := 0; -- prevent junk warning
1762 Seen : Entity_Id := Empty; -- prevent junk warning
1764 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1765 -- Determine whether a node comes from a predefined library unit or
1768 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1769 -- Try and fix up a literal so that it matches its expected type. New
1770 -- literals are manufactured if necessary to avoid cascaded errors.
1772 procedure Report_Ambiguous_Argument;
1773 -- Additional diagnostics when an ambiguous call has an ambiguous
1774 -- argument (typically a controlling actual).
1776 procedure Resolution_Failed;
1777 -- Called when attempt at resolving current expression fails
1779 ------------------------------------
1780 -- Comes_From_Predefined_Lib_Unit --
1781 -------------------------------------
1783 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1786 Sloc (Nod) = Standard_Location
1787 or else Is_Predefined_File_Name
1788 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1789 end Comes_From_Predefined_Lib_Unit;
1791 --------------------
1792 -- Patch_Up_Value --
1793 --------------------
1795 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1797 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1799 Make_Real_Literal (Sloc (N),
1800 Realval => UR_From_Uint (Intval (N))));
1801 Set_Etype (N, Universal_Real);
1802 Set_Is_Static_Expression (N);
1804 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1806 Make_Integer_Literal (Sloc (N),
1807 Intval => UR_To_Uint (Realval (N))));
1808 Set_Etype (N, Universal_Integer);
1809 Set_Is_Static_Expression (N);
1811 elsif Nkind (N) = N_String_Literal
1812 and then Is_Character_Type (Typ)
1814 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1816 Make_Character_Literal (Sloc (N),
1818 Char_Literal_Value =>
1819 UI_From_Int (Character'Pos ('A'))));
1820 Set_Etype (N, Any_Character);
1821 Set_Is_Static_Expression (N);
1823 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1825 Make_String_Literal (Sloc (N),
1826 Strval => End_String));
1828 elsif Nkind (N) = N_Range then
1829 Patch_Up_Value (Low_Bound (N), Typ);
1830 Patch_Up_Value (High_Bound (N), Typ);
1834 -------------------------------
1835 -- Report_Ambiguous_Argument --
1836 -------------------------------
1838 procedure Report_Ambiguous_Argument is
1839 Arg : constant Node_Id := First (Parameter_Associations (N));
1844 if Nkind (Arg) = N_Function_Call
1845 and then Is_Entity_Name (Name (Arg))
1846 and then Is_Overloaded (Name (Arg))
1848 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1850 -- Could use comments on what is going on here???
1852 Get_First_Interp (Name (Arg), I, It);
1853 while Present (It.Nam) loop
1854 Error_Msg_Sloc := Sloc (It.Nam);
1856 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1857 Error_Msg_N ("interpretation (inherited) #!", Arg);
1859 Error_Msg_N ("interpretation #!", Arg);
1862 Get_Next_Interp (I, It);
1865 end Report_Ambiguous_Argument;
1867 -----------------------
1868 -- Resolution_Failed --
1869 -----------------------
1871 procedure Resolution_Failed is
1873 Patch_Up_Value (N, Typ);
1875 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1876 Set_Is_Overloaded (N, False);
1878 -- The caller will return without calling the expander, so we need
1879 -- to set the analyzed flag. Note that it is fine to set Analyzed
1880 -- to True even if we are in the middle of a shallow analysis,
1881 -- (see the spec of sem for more details) since this is an error
1882 -- situation anyway, and there is no point in repeating the
1883 -- analysis later (indeed it won't work to repeat it later, since
1884 -- we haven't got a clear resolution of which entity is being
1887 Set_Analyzed (N, True);
1889 end Resolution_Failed;
1891 -- Start of processing for Resolve
1898 -- Access attribute on remote subprogram cannot be used for a non-remote
1899 -- access-to-subprogram type.
1901 if Nkind (N) = N_Attribute_Reference
1902 and then (Attribute_Name (N) = Name_Access or else
1903 Attribute_Name (N) = Name_Unrestricted_Access or else
1904 Attribute_Name (N) = Name_Unchecked_Access)
1905 and then Comes_From_Source (N)
1906 and then Is_Entity_Name (Prefix (N))
1907 and then Is_Subprogram (Entity (Prefix (N)))
1908 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1909 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1912 ("prefix must statically denote a non-remote subprogram", N);
1915 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1917 -- If the context is a Remote_Access_To_Subprogram, access attributes
1918 -- must be resolved with the corresponding fat pointer. There is no need
1919 -- to check for the attribute name since the return type of an
1920 -- attribute is never a remote type.
1922 if Nkind (N) = N_Attribute_Reference
1923 and then Comes_From_Source (N)
1924 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1927 Attr : constant Attribute_Id :=
1928 Get_Attribute_Id (Attribute_Name (N));
1929 Pref : constant Node_Id := Prefix (N);
1932 Is_Remote : Boolean := True;
1935 -- Check that Typ is a remote access-to-subprogram type
1937 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1939 -- Prefix (N) must statically denote a remote subprogram
1940 -- declared in a package specification.
1942 if Attr = Attribute_Access then
1943 Decl := Unit_Declaration_Node (Entity (Pref));
1945 if Nkind (Decl) = N_Subprogram_Body then
1946 Spec := Corresponding_Spec (Decl);
1948 if not No (Spec) then
1949 Decl := Unit_Declaration_Node (Spec);
1953 Spec := Parent (Decl);
1955 if not Is_Entity_Name (Prefix (N))
1956 or else Nkind (Spec) /= N_Package_Specification
1958 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1962 ("prefix must statically denote a remote subprogram ",
1967 -- If we are generating code for a distributed program.
1968 -- perform semantic checks against the corresponding
1971 if (Attr = Attribute_Access or else
1972 Attr = Attribute_Unchecked_Access or else
1973 Attr = Attribute_Unrestricted_Access)
1974 and then Full_Expander_Active
1975 and then Get_PCS_Name /= Name_No_DSA
1977 Check_Subtype_Conformant
1978 (New_Id => Entity (Prefix (N)),
1979 Old_Id => Designated_Type
1980 (Corresponding_Remote_Type (Typ)),
1984 Process_Remote_AST_Attribute (N, Typ);
1991 Debug_A_Entry ("resolving ", N);
1993 if Debug_Flag_V then
1994 Write_Overloads (N);
1997 if Comes_From_Source (N) then
1998 if Is_Fixed_Point_Type (Typ) then
1999 Check_Restriction (No_Fixed_Point, N);
2001 elsif Is_Floating_Point_Type (Typ)
2002 and then Typ /= Universal_Real
2003 and then Typ /= Any_Real
2005 Check_Restriction (No_Floating_Point, N);
2009 -- Return if already analyzed
2011 if Analyzed (N) then
2012 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2015 -- Return if type = Any_Type (previous error encountered)
2017 elsif Etype (N) = Any_Type then
2018 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2022 Check_Parameterless_Call (N);
2024 -- If not overloaded, then we know the type, and all that needs doing
2025 -- is to check that this type is compatible with the context.
2027 if not Is_Overloaded (N) then
2028 Found := Covers (Typ, Etype (N));
2029 Expr_Type := Etype (N);
2031 -- In the overloaded case, we must select the interpretation that
2032 -- is compatible with the context (i.e. the type passed to Resolve)
2035 -- Loop through possible interpretations
2037 Get_First_Interp (N, I, It);
2038 Interp_Loop : while Present (It.Typ) loop
2040 if Debug_Flag_V then
2041 Write_Str ("Interp: ");
2045 -- We are only interested in interpretations that are compatible
2046 -- with the expected type, any other interpretations are ignored.
2048 if not Covers (Typ, It.Typ) then
2049 if Debug_Flag_V then
2050 Write_Str (" interpretation incompatible with context");
2055 -- Skip the current interpretation if it is disabled by an
2056 -- abstract operator. This action is performed only when the
2057 -- type against which we are resolving is the same as the
2058 -- type of the interpretation.
2060 if Ada_Version >= Ada_2005
2061 and then It.Typ = Typ
2062 and then Typ /= Universal_Integer
2063 and then Typ /= Universal_Real
2064 and then Present (It.Abstract_Op)
2066 if Debug_Flag_V then
2067 Write_Line ("Skip.");
2073 -- First matching interpretation
2079 Expr_Type := It.Typ;
2081 -- Matching interpretation that is not the first, maybe an
2082 -- error, but there are some cases where preference rules are
2083 -- used to choose between the two possibilities. These and
2084 -- some more obscure cases are handled in Disambiguate.
2087 -- If the current statement is part of a predefined library
2088 -- unit, then all interpretations which come from user level
2089 -- packages should not be considered.
2092 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2097 Error_Msg_Sloc := Sloc (Seen);
2098 It1 := Disambiguate (N, I1, I, Typ);
2100 -- Disambiguation has succeeded. Skip the remaining
2103 if It1 /= No_Interp then
2105 Expr_Type := It1.Typ;
2107 while Present (It.Typ) loop
2108 Get_Next_Interp (I, It);
2112 -- Before we issue an ambiguity complaint, check for
2113 -- the case of a subprogram call where at least one
2114 -- of the arguments is Any_Type, and if so, suppress
2115 -- the message, since it is a cascaded error.
2117 if Nkind_In (N, N_Function_Call,
2118 N_Procedure_Call_Statement)
2125 A := First_Actual (N);
2126 while Present (A) loop
2129 if Nkind (E) = N_Parameter_Association then
2130 E := Explicit_Actual_Parameter (E);
2133 if Etype (E) = Any_Type then
2134 if Debug_Flag_V then
2135 Write_Str ("Any_Type in call");
2146 elsif Nkind (N) in N_Binary_Op
2147 and then (Etype (Left_Opnd (N)) = Any_Type
2148 or else Etype (Right_Opnd (N)) = Any_Type)
2152 elsif Nkind (N) in N_Unary_Op
2153 and then Etype (Right_Opnd (N)) = Any_Type
2158 -- Not that special case, so issue message using the
2159 -- flag Ambiguous to control printing of the header
2160 -- message only at the start of an ambiguous set.
2162 if not Ambiguous then
2163 if Nkind (N) = N_Function_Call
2164 and then Nkind (Name (N)) = N_Explicit_Dereference
2167 ("ambiguous expression "
2168 & "(cannot resolve indirect call)!", N);
2170 Error_Msg_NE -- CODEFIX
2171 ("ambiguous expression (cannot resolve&)!",
2177 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2179 ("\\possible interpretation (inherited)#!", N);
2181 Error_Msg_N -- CODEFIX
2182 ("\\possible interpretation#!", N);
2186 (N, N_Procedure_Call_Statement, N_Function_Call)
2187 and then Present (Parameter_Associations (N))
2189 Report_Ambiguous_Argument;
2193 Error_Msg_Sloc := Sloc (It.Nam);
2195 -- By default, the error message refers to the candidate
2196 -- interpretation. But if it is a predefined operator, it
2197 -- is implicitly declared at the declaration of the type
2198 -- of the operand. Recover the sloc of that declaration
2199 -- for the error message.
2201 if Nkind (N) in N_Op
2202 and then Scope (It.Nam) = Standard_Standard
2203 and then not Is_Overloaded (Right_Opnd (N))
2204 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2207 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2209 if Comes_From_Source (Err_Type)
2210 and then Present (Parent (Err_Type))
2212 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2215 elsif Nkind (N) in N_Binary_Op
2216 and then Scope (It.Nam) = Standard_Standard
2217 and then not Is_Overloaded (Left_Opnd (N))
2218 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2221 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2223 if Comes_From_Source (Err_Type)
2224 and then Present (Parent (Err_Type))
2226 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2229 -- If this is an indirect call, use the subprogram_type
2230 -- in the message, to have a meaningful location. Also
2231 -- indicate if this is an inherited operation, created
2232 -- by a type declaration.
2234 elsif Nkind (N) = N_Function_Call
2235 and then Nkind (Name (N)) = N_Explicit_Dereference
2236 and then Is_Type (It.Nam)
2240 Sloc (Associated_Node_For_Itype (Err_Type));
2245 if Nkind (N) in N_Op
2246 and then Scope (It.Nam) = Standard_Standard
2247 and then Present (Err_Type)
2249 -- Special-case the message for universal_fixed
2250 -- operators, which are not declared with the type
2251 -- of the operand, but appear forever in Standard.
2253 if It.Typ = Universal_Fixed
2254 and then Scope (It.Nam) = Standard_Standard
2257 ("\\possible interpretation as " &
2258 "universal_fixed operation " &
2259 "(RM 4.5.5 (19))", N);
2262 ("\\possible interpretation (predefined)#!", N);
2266 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2269 ("\\possible interpretation (inherited)#!", N);
2271 Error_Msg_N -- CODEFIX
2272 ("\\possible interpretation#!", N);
2278 -- We have a matching interpretation, Expr_Type is the type
2279 -- from this interpretation, and Seen is the entity.
2281 -- For an operator, just set the entity name. The type will be
2282 -- set by the specific operator resolution routine.
2284 if Nkind (N) in N_Op then
2285 Set_Entity (N, Seen);
2286 Generate_Reference (Seen, N);
2288 elsif Nkind (N) = N_Case_Expression then
2289 Set_Etype (N, Expr_Type);
2291 elsif Nkind (N) = N_Character_Literal then
2292 Set_Etype (N, Expr_Type);
2294 elsif Nkind (N) = N_Conditional_Expression then
2295 Set_Etype (N, Expr_Type);
2297 -- AI05-0139-2: Expression is overloaded because type has
2298 -- implicit dereference. If type matches context, no implicit
2299 -- dereference is involved.
2301 elsif Has_Implicit_Dereference (Expr_Type) then
2302 Set_Etype (N, Expr_Type);
2303 Set_Is_Overloaded (N, False);
2306 elsif Is_Overloaded (N)
2307 and then Present (It.Nam)
2308 and then Ekind (It.Nam) = E_Discriminant
2309 and then Has_Implicit_Dereference (It.Nam)
2311 Build_Explicit_Dereference (N, It.Nam);
2313 -- For an explicit dereference, attribute reference, range,
2314 -- short-circuit form (which is not an operator node), or call
2315 -- with a name that is an explicit dereference, there is
2316 -- nothing to be done at this point.
2318 elsif Nkind_In (N, N_Explicit_Dereference,
2319 N_Attribute_Reference,
2321 N_Indexed_Component,
2324 N_Selected_Component,
2326 or else Nkind (Name (N)) = N_Explicit_Dereference
2330 -- For procedure or function calls, set the type of the name,
2331 -- and also the entity pointer for the prefix.
2333 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2334 and then Is_Entity_Name (Name (N))
2336 Set_Etype (Name (N), Expr_Type);
2337 Set_Entity (Name (N), Seen);
2338 Generate_Reference (Seen, Name (N));
2340 elsif Nkind (N) = N_Function_Call
2341 and then Nkind (Name (N)) = N_Selected_Component
2343 Set_Etype (Name (N), Expr_Type);
2344 Set_Entity (Selector_Name (Name (N)), Seen);
2345 Generate_Reference (Seen, Selector_Name (Name (N)));
2347 -- For all other cases, just set the type of the Name
2350 Set_Etype (Name (N), Expr_Type);
2357 -- Move to next interpretation
2359 exit Interp_Loop when No (It.Typ);
2361 Get_Next_Interp (I, It);
2362 end loop Interp_Loop;
2365 -- At this stage Found indicates whether or not an acceptable
2366 -- interpretation exists. If not, then we have an error, except that if
2367 -- the context is Any_Type as a result of some other error, then we
2368 -- suppress the error report.
2371 if Typ /= Any_Type then
2373 -- If type we are looking for is Void, then this is the procedure
2374 -- call case, and the error is simply that what we gave is not a
2375 -- procedure name (we think of procedure calls as expressions with
2376 -- types internally, but the user doesn't think of them this way!)
2378 if Typ = Standard_Void_Type then
2380 -- Special case message if function used as a procedure
2382 if Nkind (N) = N_Procedure_Call_Statement
2383 and then Is_Entity_Name (Name (N))
2384 and then Ekind (Entity (Name (N))) = E_Function
2387 ("cannot use function & in a procedure call",
2388 Name (N), Entity (Name (N)));
2390 -- Otherwise give general message (not clear what cases this
2391 -- covers, but no harm in providing for them!)
2394 Error_Msg_N ("expect procedure name in procedure call", N);
2399 -- Otherwise we do have a subexpression with the wrong type
2401 -- Check for the case of an allocator which uses an access type
2402 -- instead of the designated type. This is a common error and we
2403 -- specialize the message, posting an error on the operand of the
2404 -- allocator, complaining that we expected the designated type of
2407 elsif Nkind (N) = N_Allocator
2408 and then Ekind (Typ) in Access_Kind
2409 and then Ekind (Etype (N)) in Access_Kind
2410 and then Designated_Type (Etype (N)) = Typ
2412 Wrong_Type (Expression (N), Designated_Type (Typ));
2415 -- Check for view mismatch on Null in instances, for which the
2416 -- view-swapping mechanism has no identifier.
2418 elsif (In_Instance or else In_Inlined_Body)
2419 and then (Nkind (N) = N_Null)
2420 and then Is_Private_Type (Typ)
2421 and then Is_Access_Type (Full_View (Typ))
2423 Resolve (N, Full_View (Typ));
2427 -- Check for an aggregate. Sometimes we can get bogus aggregates
2428 -- from misuse of parentheses, and we are about to complain about
2429 -- the aggregate without even looking inside it.
2431 -- Instead, if we have an aggregate of type Any_Composite, then
2432 -- analyze and resolve the component fields, and then only issue
2433 -- another message if we get no errors doing this (otherwise
2434 -- assume that the errors in the aggregate caused the problem).
2436 elsif Nkind (N) = N_Aggregate
2437 and then Etype (N) = Any_Composite
2439 -- Disable expansion in any case. If there is a type mismatch
2440 -- it may be fatal to try to expand the aggregate. The flag
2441 -- would otherwise be set to false when the error is posted.
2443 Expander_Active := False;
2446 procedure Check_Aggr (Aggr : Node_Id);
2447 -- Check one aggregate, and set Found to True if we have a
2448 -- definite error in any of its elements
2450 procedure Check_Elmt (Aelmt : Node_Id);
2451 -- Check one element of aggregate and set Found to True if
2452 -- we definitely have an error in the element.
2458 procedure Check_Aggr (Aggr : Node_Id) is
2462 if Present (Expressions (Aggr)) then
2463 Elmt := First (Expressions (Aggr));
2464 while Present (Elmt) loop
2470 if Present (Component_Associations (Aggr)) then
2471 Elmt := First (Component_Associations (Aggr));
2472 while Present (Elmt) loop
2474 -- If this is a default-initialized component, then
2475 -- there is nothing to check. The box will be
2476 -- replaced by the appropriate call during late
2479 if not Box_Present (Elmt) then
2480 Check_Elmt (Expression (Elmt));
2492 procedure Check_Elmt (Aelmt : Node_Id) is
2494 -- If we have a nested aggregate, go inside it (to
2495 -- attempt a naked analyze-resolve of the aggregate can
2496 -- cause undesirable cascaded errors). Do not resolve
2497 -- expression if it needs a type from context, as for
2498 -- integer * fixed expression.
2500 if Nkind (Aelmt) = N_Aggregate then
2506 if not Is_Overloaded (Aelmt)
2507 and then Etype (Aelmt) /= Any_Fixed
2512 if Etype (Aelmt) = Any_Type then
2523 -- If an error message was issued already, Found got reset to
2524 -- True, so if it is still False, issue standard Wrong_Type msg.
2527 if Is_Overloaded (N)
2528 and then Nkind (N) = N_Function_Call
2531 Subp_Name : Node_Id;
2533 if Is_Entity_Name (Name (N)) then
2534 Subp_Name := Name (N);
2536 elsif Nkind (Name (N)) = N_Selected_Component then
2538 -- Protected operation: retrieve operation name
2540 Subp_Name := Selector_Name (Name (N));
2543 raise Program_Error;
2546 Error_Msg_Node_2 := Typ;
2547 Error_Msg_NE ("no visible interpretation of&" &
2548 " matches expected type&", N, Subp_Name);
2551 if All_Errors_Mode then
2553 Index : Interp_Index;
2557 Error_Msg_N ("\\possible interpretations:", N);
2559 Get_First_Interp (Name (N), Index, It);
2560 while Present (It.Nam) loop
2561 Error_Msg_Sloc := Sloc (It.Nam);
2562 Error_Msg_Node_2 := It.Nam;
2564 ("\\ type& for & declared#", N, It.Typ);
2565 Get_Next_Interp (Index, It);
2570 Error_Msg_N ("\use -gnatf for details", N);
2574 Wrong_Type (N, Typ);
2582 -- Test if we have more than one interpretation for the context
2584 elsif Ambiguous then
2588 -- Only one intepretation
2591 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2592 -- the "+" on T is abstract, and the operands are of universal type,
2593 -- the above code will have (incorrectly) resolved the "+" to the
2594 -- universal one in Standard. Therefore check for this case and give
2595 -- an error. We can't do this earlier, because it would cause legal
2596 -- cases to get errors (when some other type has an abstract "+").
2598 if Ada_Version >= Ada_2005 and then
2599 Nkind (N) in N_Op and then
2600 Is_Overloaded (N) and then
2601 Is_Universal_Numeric_Type (Etype (Entity (N)))
2603 Get_First_Interp (N, I, It);
2604 while Present (It.Typ) loop
2605 if Present (It.Abstract_Op) and then
2606 Etype (It.Abstract_Op) = Typ
2609 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2613 Get_Next_Interp (I, It);
2617 -- Here we have an acceptable interpretation for the context
2619 -- Propagate type information and normalize tree for various
2620 -- predefined operations. If the context only imposes a class of
2621 -- types, rather than a specific type, propagate the actual type
2624 if Typ = Any_Integer or else
2625 Typ = Any_Boolean or else
2626 Typ = Any_Modular or else
2627 Typ = Any_Real or else
2630 Ctx_Type := Expr_Type;
2632 -- Any_Fixed is legal in a real context only if a specific fixed-
2633 -- point type is imposed. If Norman Cohen can be confused by this,
2634 -- it deserves a separate message.
2637 and then Expr_Type = Any_Fixed
2639 Error_Msg_N ("illegal context for mixed mode operation", N);
2640 Set_Etype (N, Universal_Real);
2641 Ctx_Type := Universal_Real;
2645 -- A user-defined operator is transformed into a function call at
2646 -- this point, so that further processing knows that operators are
2647 -- really operators (i.e. are predefined operators). User-defined
2648 -- operators that are intrinsic are just renamings of the predefined
2649 -- ones, and need not be turned into calls either, but if they rename
2650 -- a different operator, we must transform the node accordingly.
2651 -- Instantiations of Unchecked_Conversion are intrinsic but are
2652 -- treated as functions, even if given an operator designator.
2654 if Nkind (N) in N_Op
2655 and then Present (Entity (N))
2656 and then Ekind (Entity (N)) /= E_Operator
2659 if not Is_Predefined_Op (Entity (N)) then
2660 Rewrite_Operator_As_Call (N, Entity (N));
2662 elsif Present (Alias (Entity (N)))
2664 Nkind (Parent (Parent (Entity (N)))) =
2665 N_Subprogram_Renaming_Declaration
2667 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2669 -- If the node is rewritten, it will be fully resolved in
2670 -- Rewrite_Renamed_Operator.
2672 if Analyzed (N) then
2678 case N_Subexpr'(Nkind (N)) is
2680 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2682 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2684 when N_Short_Circuit
2685 => Resolve_Short_Circuit (N, Ctx_Type);
2687 when N_Attribute_Reference
2688 => Resolve_Attribute (N, Ctx_Type);
2690 when N_Case_Expression
2691 => Resolve_Case_Expression (N, Ctx_Type);
2693 when N_Character_Literal
2694 => Resolve_Character_Literal (N, Ctx_Type);
2696 when N_Conditional_Expression
2697 => Resolve_Conditional_Expression (N, Ctx_Type);
2699 when N_Expanded_Name
2700 => Resolve_Entity_Name (N, Ctx_Type);
2702 when N_Explicit_Dereference
2703 => Resolve_Explicit_Dereference (N, Ctx_Type);
2705 when N_Expression_With_Actions
2706 => Resolve_Expression_With_Actions (N, Ctx_Type);
2708 when N_Extension_Aggregate
2709 => Resolve_Extension_Aggregate (N, Ctx_Type);
2711 when N_Function_Call
2712 => Resolve_Call (N, Ctx_Type);
2715 => Resolve_Entity_Name (N, Ctx_Type);
2717 when N_Indexed_Component
2718 => Resolve_Indexed_Component (N, Ctx_Type);
2720 when N_Integer_Literal
2721 => Resolve_Integer_Literal (N, Ctx_Type);
2723 when N_Membership_Test
2724 => Resolve_Membership_Op (N, Ctx_Type);
2726 when N_Null => Resolve_Null (N, Ctx_Type);
2728 when N_Op_And | N_Op_Or | N_Op_Xor
2729 => Resolve_Logical_Op (N, Ctx_Type);
2731 when N_Op_Eq | N_Op_Ne
2732 => Resolve_Equality_Op (N, Ctx_Type);
2734 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2735 => Resolve_Comparison_Op (N, Ctx_Type);
2737 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2739 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2740 N_Op_Divide | N_Op_Mod | N_Op_Rem
2742 => Resolve_Arithmetic_Op (N, Ctx_Type);
2744 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2746 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2748 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2749 => Resolve_Unary_Op (N, Ctx_Type);
2751 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2753 when N_Procedure_Call_Statement
2754 => Resolve_Call (N, Ctx_Type);
2756 when N_Operator_Symbol
2757 => Resolve_Operator_Symbol (N, Ctx_Type);
2759 when N_Qualified_Expression
2760 => Resolve_Qualified_Expression (N, Ctx_Type);
2762 when N_Quantified_Expression
2763 => Resolve_Quantified_Expression (N, Ctx_Type);
2765 when N_Raise_xxx_Error
2766 => Set_Etype (N, Ctx_Type);
2768 when N_Range => Resolve_Range (N, Ctx_Type);
2771 => Resolve_Real_Literal (N, Ctx_Type);
2773 when N_Reference => Resolve_Reference (N, Ctx_Type);
2775 when N_Selected_Component
2776 => Resolve_Selected_Component (N, Ctx_Type);
2778 when N_Slice => Resolve_Slice (N, Ctx_Type);
2780 when N_String_Literal
2781 => Resolve_String_Literal (N, Ctx_Type);
2783 when N_Subprogram_Info
2784 => Resolve_Subprogram_Info (N, Ctx_Type);
2786 when N_Type_Conversion
2787 => Resolve_Type_Conversion (N, Ctx_Type);
2789 when N_Unchecked_Expression =>
2790 Resolve_Unchecked_Expression (N, Ctx_Type);
2792 when N_Unchecked_Type_Conversion =>
2793 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2796 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2797 -- expression of an anonymous access type that occurs in the context
2798 -- of a named general access type, except when the expression is that
2799 -- of a membership test. This ensures proper legality checking in
2800 -- terms of allowed conversions (expressions that would be illegal to
2801 -- convert implicitly are allowed in membership tests).
2803 if Ada_Version >= Ada_2012
2804 and then Ekind (Ctx_Type) = E_General_Access_Type
2805 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2806 and then Nkind (Parent (N)) not in N_Membership_Test
2808 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2809 Analyze_And_Resolve (N, Ctx_Type);
2812 -- If the subexpression was replaced by a non-subexpression, then
2813 -- all we do is to expand it. The only legitimate case we know of
2814 -- is converting procedure call statement to entry call statements,
2815 -- but there may be others, so we are making this test general.
2817 if Nkind (N) not in N_Subexpr then
2818 Debug_A_Exit ("resolving ", N, " (done)");
2823 -- AI05-144-2: Check dangerous order dependence within an expression
2824 -- that is not a subexpression. Exclude RHS of an assignment, because
2825 -- both sides may have side-effects and the check must be performed
2826 -- over the statement.
2828 if Nkind (Parent (N)) not in N_Subexpr
2829 and then Nkind (Parent (N)) /= N_Assignment_Statement
2830 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2832 Check_Order_Dependence;
2835 -- The expression is definitely NOT overloaded at this point, so
2836 -- we reset the Is_Overloaded flag to avoid any confusion when
2837 -- reanalyzing the node.
2839 Set_Is_Overloaded (N, False);
2841 -- Freeze expression type, entity if it is a name, and designated
2842 -- type if it is an allocator (RM 13.14(10,11,13)).
2844 -- Now that the resolution of the type of the node is complete, and
2845 -- we did not detect an error, we can expand this node. We skip the
2846 -- expand call if we are in a default expression, see section
2847 -- "Handling of Default Expressions" in Sem spec.
2849 Debug_A_Exit ("resolving ", N, " (done)");
2851 -- We unconditionally freeze the expression, even if we are in
2852 -- default expression mode (the Freeze_Expression routine tests this
2853 -- flag and only freezes static types if it is set).
2855 -- Ada 2012 (AI05-177): Expression functions do not freeze. Only
2856 -- their use (in an expanded call) freezes.
2858 if Ekind (Current_Scope) /= E_Function
2860 Nkind (Original_Node (Unit_Declaration_Node (Current_Scope))) /=
2861 N_Expression_Function
2863 Freeze_Expression (N);
2866 -- Now we can do the expansion
2876 -- Version with check(s) suppressed
2878 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2880 if Suppress = All_Checks then
2882 Svg : constant Suppress_Array := Scope_Suppress;
2884 Scope_Suppress := (others => True);
2886 Scope_Suppress := Svg;
2891 Svg : constant Boolean := Scope_Suppress (Suppress);
2893 Scope_Suppress (Suppress) := True;
2895 Scope_Suppress (Suppress) := Svg;
2904 -- Version with implicit type
2906 procedure Resolve (N : Node_Id) is
2908 Resolve (N, Etype (N));
2911 ---------------------
2912 -- Resolve_Actuals --
2913 ---------------------
2915 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2916 Loc : constant Source_Ptr := Sloc (N);
2921 Prev : Node_Id := Empty;
2924 procedure Check_Argument_Order;
2925 -- Performs a check for the case where the actuals are all simple
2926 -- identifiers that correspond to the formal names, but in the wrong
2927 -- order, which is considered suspicious and cause for a warning.
2929 procedure Check_Prefixed_Call;
2930 -- If the original node is an overloaded call in prefix notation,
2931 -- insert an 'Access or a dereference as needed over the first actual.
2932 -- Try_Object_Operation has already verified that there is a valid
2933 -- interpretation, but the form of the actual can only be determined
2934 -- once the primitive operation is identified.
2936 procedure Insert_Default;
2937 -- If the actual is missing in a call, insert in the actuals list
2938 -- an instance of the default expression. The insertion is always
2939 -- a named association.
2941 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2942 -- Check whether T1 and T2, or their full views, are derived from a
2943 -- common type. Used to enforce the restrictions on array conversions
2946 function Static_Concatenation (N : Node_Id) return Boolean;
2947 -- Predicate to determine whether an actual that is a concatenation
2948 -- will be evaluated statically and does not need a transient scope.
2949 -- This must be determined before the actual is resolved and expanded
2950 -- because if needed the transient scope must be introduced earlier.
2952 --------------------------
2953 -- Check_Argument_Order --
2954 --------------------------
2956 procedure Check_Argument_Order is
2958 -- Nothing to do if no parameters, or original node is neither a
2959 -- function call nor a procedure call statement (happens in the
2960 -- operator-transformed-to-function call case), or the call does
2961 -- not come from source, or this warning is off.
2963 if not Warn_On_Parameter_Order
2964 or else No (Parameter_Associations (N))
2965 or else not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2967 or else not Comes_From_Source (N)
2973 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2976 -- Nothing to do if only one parameter
2982 -- Here if at least two arguments
2985 Actuals : array (1 .. Nargs) of Node_Id;
2989 Wrong_Order : Boolean := False;
2990 -- Set True if an out of order case is found
2993 -- Collect identifier names of actuals, fail if any actual is
2994 -- not a simple identifier, and record max length of name.
2996 Actual := First (Parameter_Associations (N));
2997 for J in Actuals'Range loop
2998 if Nkind (Actual) /= N_Identifier then
3001 Actuals (J) := Actual;
3006 -- If we got this far, all actuals are identifiers and the list
3007 -- of their names is stored in the Actuals array.
3009 Formal := First_Formal (Nam);
3010 for J in Actuals'Range loop
3012 -- If we ran out of formals, that's odd, probably an error
3013 -- which will be detected elsewhere, but abandon the search.
3019 -- If name matches and is in order OK
3021 if Chars (Formal) = Chars (Actuals (J)) then
3025 -- If no match, see if it is elsewhere in list and if so
3026 -- flag potential wrong order if type is compatible.
3028 for K in Actuals'Range loop
3029 if Chars (Formal) = Chars (Actuals (K))
3031 Has_Compatible_Type (Actuals (K), Etype (Formal))
3033 Wrong_Order := True;
3043 <<Continue>> Next_Formal (Formal);
3046 -- If Formals left over, also probably an error, skip warning
3048 if Present (Formal) then
3052 -- Here we give the warning if something was out of order
3056 ("actuals for this call may be in wrong order?", N);
3060 end Check_Argument_Order;
3062 -------------------------
3063 -- Check_Prefixed_Call --
3064 -------------------------
3066 procedure Check_Prefixed_Call is
3067 Act : constant Node_Id := First_Actual (N);
3068 A_Type : constant Entity_Id := Etype (Act);
3069 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3070 Orig : constant Node_Id := Original_Node (N);
3074 -- Check whether the call is a prefixed call, with or without
3075 -- additional actuals.
3077 if Nkind (Orig) = N_Selected_Component
3079 (Nkind (Orig) = N_Indexed_Component
3080 and then Nkind (Prefix (Orig)) = N_Selected_Component
3081 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3082 and then Is_Entity_Name (Act)
3083 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3085 if Is_Access_Type (A_Type)
3086 and then not Is_Access_Type (F_Type)
3088 -- Introduce dereference on object in prefix
3091 Make_Explicit_Dereference (Sloc (Act),
3092 Prefix => Relocate_Node (Act));
3093 Rewrite (Act, New_A);
3096 elsif Is_Access_Type (F_Type)
3097 and then not Is_Access_Type (A_Type)
3099 -- Introduce an implicit 'Access in prefix
3101 if not Is_Aliased_View (Act) then
3103 ("object in prefixed call to& must be aliased"
3104 & " (RM-2005 4.3.1 (13))",
3109 Make_Attribute_Reference (Loc,
3110 Attribute_Name => Name_Access,
3111 Prefix => Relocate_Node (Act)));
3116 end Check_Prefixed_Call;
3118 --------------------
3119 -- Insert_Default --
3120 --------------------
3122 procedure Insert_Default is
3127 -- Missing argument in call, nothing to insert
3129 if No (Default_Value (F)) then
3133 -- Note that we do a full New_Copy_Tree, so that any associated
3134 -- Itypes are properly copied. This may not be needed any more,
3135 -- but it does no harm as a safety measure! Defaults of a generic
3136 -- formal may be out of bounds of the corresponding actual (see
3137 -- cc1311b) and an additional check may be required.
3142 New_Scope => Current_Scope,
3145 if Is_Concurrent_Type (Scope (Nam))
3146 and then Has_Discriminants (Scope (Nam))
3148 Replace_Actual_Discriminants (N, Actval);
3151 if Is_Overloadable (Nam)
3152 and then Present (Alias (Nam))
3154 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3155 and then not Is_Tagged_Type (Etype (F))
3157 -- If default is a real literal, do not introduce a
3158 -- conversion whose effect may depend on the run-time
3159 -- size of universal real.
3161 if Nkind (Actval) = N_Real_Literal then
3162 Set_Etype (Actval, Base_Type (Etype (F)));
3164 Actval := Unchecked_Convert_To (Etype (F), Actval);
3168 if Is_Scalar_Type (Etype (F)) then
3169 Enable_Range_Check (Actval);
3172 Set_Parent (Actval, N);
3174 -- Resolve aggregates with their base type, to avoid scope
3175 -- anomalies: the subtype was first built in the subprogram
3176 -- declaration, and the current call may be nested.
3178 if Nkind (Actval) = N_Aggregate then
3179 Analyze_And_Resolve (Actval, Etype (F));
3181 Analyze_And_Resolve (Actval, Etype (Actval));
3185 Set_Parent (Actval, N);
3187 -- See note above concerning aggregates
3189 if Nkind (Actval) = N_Aggregate
3190 and then Has_Discriminants (Etype (Actval))
3192 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3194 -- Resolve entities with their own type, which may differ from
3195 -- the type of a reference in a generic context (the view
3196 -- swapping mechanism did not anticipate the re-analysis of
3197 -- default values in calls).
3199 elsif Is_Entity_Name (Actval) then
3200 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3203 Analyze_And_Resolve (Actval, Etype (Actval));
3207 -- If default is a tag indeterminate function call, propagate tag
3208 -- to obtain proper dispatching.
3210 if Is_Controlling_Formal (F)
3211 and then Nkind (Default_Value (F)) = N_Function_Call
3213 Set_Is_Controlling_Actual (Actval);
3218 -- If the default expression raises constraint error, then just
3219 -- silently replace it with an N_Raise_Constraint_Error node, since
3220 -- we already gave the warning on the subprogram spec. If node is
3221 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3222 -- the warnings removal machinery.
3224 if Raises_Constraint_Error (Actval)
3225 and then Nkind (Actval) /= N_Raise_Constraint_Error
3228 Make_Raise_Constraint_Error (Loc,
3229 Reason => CE_Range_Check_Failed));
3230 Set_Raises_Constraint_Error (Actval);
3231 Set_Etype (Actval, Etype (F));
3235 Make_Parameter_Association (Loc,
3236 Explicit_Actual_Parameter => Actval,
3237 Selector_Name => Make_Identifier (Loc, Chars (F)));
3239 -- Case of insertion is first named actual
3241 if No (Prev) or else
3242 Nkind (Parent (Prev)) /= N_Parameter_Association
3244 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3245 Set_First_Named_Actual (N, Actval);
3248 if No (Parameter_Associations (N)) then
3249 Set_Parameter_Associations (N, New_List (Assoc));
3251 Append (Assoc, Parameter_Associations (N));
3255 Insert_After (Prev, Assoc);
3258 -- Case of insertion is not first named actual
3261 Set_Next_Named_Actual
3262 (Assoc, Next_Named_Actual (Parent (Prev)));
3263 Set_Next_Named_Actual (Parent (Prev), Actval);
3264 Append (Assoc, Parameter_Associations (N));
3267 Mark_Rewrite_Insertion (Assoc);
3268 Mark_Rewrite_Insertion (Actval);
3277 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3278 FT1 : Entity_Id := T1;
3279 FT2 : Entity_Id := T2;
3282 if Is_Private_Type (T1)
3283 and then Present (Full_View (T1))
3285 FT1 := Full_View (T1);
3288 if Is_Private_Type (T2)
3289 and then Present (Full_View (T2))
3291 FT2 := Full_View (T2);
3294 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3297 --------------------------
3298 -- Static_Concatenation --
3299 --------------------------
3301 function Static_Concatenation (N : Node_Id) return Boolean is
3304 when N_String_Literal =>
3309 -- Concatenation is static when both operands are static and
3310 -- the concatenation operator is a predefined one.
3312 return Scope (Entity (N)) = Standard_Standard
3314 Static_Concatenation (Left_Opnd (N))
3316 Static_Concatenation (Right_Opnd (N));
3319 if Is_Entity_Name (N) then
3321 Ent : constant Entity_Id := Entity (N);
3323 return Ekind (Ent) = E_Constant
3324 and then Present (Constant_Value (Ent))
3326 Is_Static_Expression (Constant_Value (Ent));
3333 end Static_Concatenation;
3335 -- Start of processing for Resolve_Actuals
3338 Check_Argument_Order;
3340 if Present (First_Actual (N)) then
3341 Check_Prefixed_Call;
3344 A := First_Actual (N);
3345 F := First_Formal (Nam);
3346 while Present (F) loop
3347 if No (A) and then Needs_No_Actuals (Nam) then
3350 -- If we have an error in any actual or formal, indicated by a type
3351 -- of Any_Type, then abandon resolution attempt, and set result type
3354 elsif (Present (A) and then Etype (A) = Any_Type)
3355 or else Etype (F) = Any_Type
3357 Set_Etype (N, Any_Type);
3361 -- Case where actual is present
3363 -- If the actual is an entity, generate a reference to it now. We
3364 -- do this before the actual is resolved, because a formal of some
3365 -- protected subprogram, or a task discriminant, will be rewritten
3366 -- during expansion, and the source entity reference may be lost.
3369 and then Is_Entity_Name (A)
3370 and then Comes_From_Source (N)
3372 Orig_A := Entity (A);
3374 if Present (Orig_A) then
3375 if Is_Formal (Orig_A)
3376 and then Ekind (F) /= E_In_Parameter
3378 Generate_Reference (Orig_A, A, 'm');
3380 elsif not Is_Overloaded (A) then
3381 Generate_Reference (Orig_A, A);
3387 and then (Nkind (Parent (A)) /= N_Parameter_Association
3388 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3390 -- If style checking mode on, check match of formal name
3393 if Nkind (Parent (A)) = N_Parameter_Association then
3394 Check_Identifier (Selector_Name (Parent (A)), F);
3398 -- If the formal is Out or In_Out, do not resolve and expand the
3399 -- conversion, because it is subsequently expanded into explicit
3400 -- temporaries and assignments. However, the object of the
3401 -- conversion can be resolved. An exception is the case of tagged
3402 -- type conversion with a class-wide actual. In that case we want
3403 -- the tag check to occur and no temporary will be needed (no
3404 -- representation change can occur) and the parameter is passed by
3405 -- reference, so we go ahead and resolve the type conversion.
3406 -- Another exception is the case of reference to component or
3407 -- subcomponent of a bit-packed array, in which case we want to
3408 -- defer expansion to the point the in and out assignments are
3411 if Ekind (F) /= E_In_Parameter
3412 and then Nkind (A) = N_Type_Conversion
3413 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3415 if Ekind (F) = E_In_Out_Parameter
3416 and then Is_Array_Type (Etype (F))
3418 -- In a view conversion, the conversion must be legal in
3419 -- both directions, and thus both component types must be
3420 -- aliased, or neither (4.6 (8)).
3422 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3423 -- the privacy requirement should not apply to generic
3424 -- types, and should be checked in an instance. ARG query
3427 if Has_Aliased_Components (Etype (Expression (A))) /=
3428 Has_Aliased_Components (Etype (F))
3431 ("both component types in a view conversion must be"
3432 & " aliased, or neither", A);
3434 -- Comment here??? what set of cases???
3437 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3439 -- Check view conv between unrelated by ref array types
3441 if Is_By_Reference_Type (Etype (F))
3442 or else Is_By_Reference_Type (Etype (Expression (A)))
3445 ("view conversion between unrelated by reference " &
3446 "array types not allowed (\'A'I-00246)", A);
3448 -- In Ada 2005 mode, check view conversion component
3449 -- type cannot be private, tagged, or volatile. Note
3450 -- that we only apply this to source conversions. The
3451 -- generated code can contain conversions which are
3452 -- not subject to this test, and we cannot extract the
3453 -- component type in such cases since it is not present.
3455 elsif Comes_From_Source (A)
3456 and then Ada_Version >= Ada_2005
3459 Comp_Type : constant Entity_Id :=
3461 (Etype (Expression (A)));
3463 if (Is_Private_Type (Comp_Type)
3464 and then not Is_Generic_Type (Comp_Type))
3465 or else Is_Tagged_Type (Comp_Type)
3466 or else Is_Volatile (Comp_Type)
3469 ("component type of a view conversion cannot"
3470 & " be private, tagged, or volatile"
3479 -- Resolve expression if conversion is all OK
3481 if (Conversion_OK (A)
3482 or else Valid_Conversion (A, Etype (A), Expression (A)))
3483 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3485 Resolve (Expression (A));
3488 -- If the actual is a function call that returns a limited
3489 -- unconstrained object that needs finalization, create a
3490 -- transient scope for it, so that it can receive the proper
3491 -- finalization list.
3493 elsif Nkind (A) = N_Function_Call
3494 and then Is_Limited_Record (Etype (F))
3495 and then not Is_Constrained (Etype (F))
3496 and then Full_Expander_Active
3497 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3499 Establish_Transient_Scope (A, False);
3500 Resolve (A, Etype (F));
3502 -- A small optimization: if one of the actuals is a concatenation
3503 -- create a block around a procedure call to recover stack space.
3504 -- This alleviates stack usage when several procedure calls in
3505 -- the same statement list use concatenation. We do not perform
3506 -- this wrapping for code statements, where the argument is a
3507 -- static string, and we want to preserve warnings involving
3508 -- sequences of such statements.
3510 elsif Nkind (A) = N_Op_Concat
3511 and then Nkind (N) = N_Procedure_Call_Statement
3512 and then Full_Expander_Active
3514 not (Is_Intrinsic_Subprogram (Nam)
3515 and then Chars (Nam) = Name_Asm)
3516 and then not Static_Concatenation (A)
3518 Establish_Transient_Scope (A, False);
3519 Resolve (A, Etype (F));
3522 if Nkind (A) = N_Type_Conversion
3523 and then Is_Array_Type (Etype (F))
3524 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3526 (Is_Limited_Type (Etype (F))
3527 or else Is_Limited_Type (Etype (Expression (A))))
3530 ("conversion between unrelated limited array types " &
3531 "not allowed (\A\I-00246)", A);
3533 if Is_Limited_Type (Etype (F)) then
3534 Explain_Limited_Type (Etype (F), A);
3537 if Is_Limited_Type (Etype (Expression (A))) then
3538 Explain_Limited_Type (Etype (Expression (A)), A);
3542 -- (Ada 2005: AI-251): If the actual is an allocator whose
3543 -- directly designated type is a class-wide interface, we build
3544 -- an anonymous access type to use it as the type of the
3545 -- allocator. Later, when the subprogram call is expanded, if
3546 -- the interface has a secondary dispatch table the expander
3547 -- will add a type conversion to force the correct displacement
3550 if Nkind (A) = N_Allocator then
3552 DDT : constant Entity_Id :=
3553 Directly_Designated_Type (Base_Type (Etype (F)));
3555 New_Itype : Entity_Id;
3558 if Is_Class_Wide_Type (DDT)
3559 and then Is_Interface (DDT)
3561 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3562 Set_Etype (New_Itype, Etype (A));
3563 Set_Directly_Designated_Type (New_Itype,
3564 Directly_Designated_Type (Etype (A)));
3565 Set_Etype (A, New_Itype);
3568 -- Ada 2005, AI-162:If the actual is an allocator, the
3569 -- innermost enclosing statement is the master of the
3570 -- created object. This needs to be done with expansion
3571 -- enabled only, otherwise the transient scope will not
3572 -- be removed in the expansion of the wrapped construct.
3574 if (Is_Controlled (DDT) or else Has_Task (DDT))
3575 and then Full_Expander_Active
3577 Establish_Transient_Scope (A, False);
3582 -- (Ada 2005): The call may be to a primitive operation of
3583 -- a tagged synchronized type, declared outside of the type.
3584 -- In this case the controlling actual must be converted to
3585 -- its corresponding record type, which is the formal type.
3586 -- The actual may be a subtype, either because of a constraint
3587 -- or because it is a generic actual, so use base type to
3588 -- locate concurrent type.
3590 F_Typ := Base_Type (Etype (F));
3592 if Is_Tagged_Type (F_Typ)
3593 and then (Is_Concurrent_Type (F_Typ)
3594 or else Is_Concurrent_Record_Type (F_Typ))
3596 -- If the actual is overloaded, look for an interpretation
3597 -- that has a synchronized type.
3599 if not Is_Overloaded (A) then
3600 A_Typ := Base_Type (Etype (A));
3604 Index : Interp_Index;
3608 Get_First_Interp (A, Index, It);
3609 while Present (It.Typ) loop
3610 if Is_Concurrent_Type (It.Typ)
3611 or else Is_Concurrent_Record_Type (It.Typ)
3613 A_Typ := Base_Type (It.Typ);
3617 Get_Next_Interp (Index, It);
3623 Full_A_Typ : Entity_Id;
3626 if Present (Full_View (A_Typ)) then
3627 Full_A_Typ := Base_Type (Full_View (A_Typ));
3629 Full_A_Typ := A_Typ;
3632 -- Tagged synchronized type (case 1): the actual is a
3635 if Is_Concurrent_Type (A_Typ)
3636 and then Corresponding_Record_Type (A_Typ) = F_Typ
3639 Unchecked_Convert_To
3640 (Corresponding_Record_Type (A_Typ), A));
3641 Resolve (A, Etype (F));
3643 -- Tagged synchronized type (case 2): the formal is a
3646 elsif Ekind (Full_A_Typ) = E_Record_Type
3648 (Corresponding_Concurrent_Type (Full_A_Typ))
3649 and then Is_Concurrent_Type (F_Typ)
3650 and then Present (Corresponding_Record_Type (F_Typ))
3651 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3653 Resolve (A, Corresponding_Record_Type (F_Typ));
3658 Resolve (A, Etype (F));
3663 -- not a synchronized operation.
3665 Resolve (A, Etype (F));
3672 if Comes_From_Source (Original_Node (N))
3673 and then Nkind_In (Original_Node (N), N_Function_Call,
3674 N_Procedure_Call_Statement)
3676 -- In formal mode, check that actual parameters matching
3677 -- formals of tagged types are objects (or ancestor type
3678 -- conversions of objects), not general expressions.
3680 if Is_Actual_Tagged_Parameter (A) then
3681 if Is_SPARK_Object_Reference (A) then
3684 elsif Nkind (A) = N_Type_Conversion then
3686 Operand : constant Node_Id := Expression (A);
3687 Operand_Typ : constant Entity_Id := Etype (Operand);
3688 Target_Typ : constant Entity_Id := A_Typ;
3691 if not Is_SPARK_Object_Reference (Operand) then
3692 Check_SPARK_Restriction
3693 ("object required", Operand);
3695 -- In formal mode, the only view conversions are those
3696 -- involving ancestor conversion of an extended type.
3699 (Is_Tagged_Type (Target_Typ)
3700 and then not Is_Class_Wide_Type (Target_Typ)
3701 and then Is_Tagged_Type (Operand_Typ)
3702 and then not Is_Class_Wide_Type (Operand_Typ)
3703 and then Is_Ancestor (Target_Typ, Operand_Typ))
3706 (F, E_Out_Parameter, E_In_Out_Parameter)
3708 Check_SPARK_Restriction
3709 ("ancestor conversion is the only permitted "
3710 & "view conversion", A);
3712 Check_SPARK_Restriction
3713 ("ancestor conversion required", A);
3722 Check_SPARK_Restriction ("object required", A);
3725 -- In formal mode, the only view conversions are those
3726 -- involving ancestor conversion of an extended type.
3728 elsif Nkind (A) = N_Type_Conversion
3729 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3731 Check_SPARK_Restriction
3732 ("ancestor conversion is the only permitted view "
3737 -- Save actual for subsequent check on order dependence, and
3738 -- indicate whether actual is modifiable. For AI05-0144-2.
3740 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3742 -- For mode IN, if actual is an entity, and the type of the formal
3743 -- has warnings suppressed, then we reset Never_Set_In_Source for
3744 -- the calling entity. The reason for this is to catch cases like
3745 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3746 -- uses trickery to modify an IN parameter.
3748 if Ekind (F) = E_In_Parameter
3749 and then Is_Entity_Name (A)
3750 and then Present (Entity (A))
3751 and then Ekind (Entity (A)) = E_Variable
3752 and then Has_Warnings_Off (F_Typ)
3754 Set_Never_Set_In_Source (Entity (A), False);
3757 -- Perform error checks for IN and IN OUT parameters
3759 if Ekind (F) /= E_Out_Parameter then
3761 -- Check unset reference. For scalar parameters, it is clearly
3762 -- wrong to pass an uninitialized value as either an IN or
3763 -- IN-OUT parameter. For composites, it is also clearly an
3764 -- error to pass a completely uninitialized value as an IN
3765 -- parameter, but the case of IN OUT is trickier. We prefer
3766 -- not to give a warning here. For example, suppose there is
3767 -- a routine that sets some component of a record to False.
3768 -- It is perfectly reasonable to make this IN-OUT and allow
3769 -- either initialized or uninitialized records to be passed
3772 -- For partially initialized composite values, we also avoid
3773 -- warnings, since it is quite likely that we are passing a
3774 -- partially initialized value and only the initialized fields
3775 -- will in fact be read in the subprogram.
3777 if Is_Scalar_Type (A_Typ)
3778 or else (Ekind (F) = E_In_Parameter
3779 and then not Is_Partially_Initialized_Type (A_Typ))
3781 Check_Unset_Reference (A);
3784 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3785 -- actual to a nested call, since this is case of reading an
3786 -- out parameter, which is not allowed.
3788 if Ada_Version = Ada_83
3789 and then Is_Entity_Name (A)
3790 and then Ekind (Entity (A)) = E_Out_Parameter
3792 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3796 -- Case of OUT or IN OUT parameter
3798 if Ekind (F) /= E_In_Parameter then
3800 -- For an Out parameter, check for useless assignment. Note
3801 -- that we can't set Last_Assignment this early, because we may
3802 -- kill current values in Resolve_Call, and that call would
3803 -- clobber the Last_Assignment field.
3805 -- Note: call Warn_On_Useless_Assignment before doing the check
3806 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3807 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3808 -- reflects the last assignment, not this one!
3810 if Ekind (F) = E_Out_Parameter then
3811 if Warn_On_Modified_As_Out_Parameter (F)
3812 and then Is_Entity_Name (A)
3813 and then Present (Entity (A))
3814 and then Comes_From_Source (N)
3816 Warn_On_Useless_Assignment (Entity (A), A);
3820 -- Validate the form of the actual. Note that the call to
3821 -- Is_OK_Variable_For_Out_Formal generates the required
3822 -- reference in this case.
3824 -- A call to an initialization procedure for an aggregate
3825 -- component may initialize a nested component of a constant
3826 -- designated object. In this context the object is variable.
3828 if not Is_OK_Variable_For_Out_Formal (A)
3829 and then not Is_Init_Proc (Nam)
3831 Error_Msg_NE ("actual for& must be a variable", A, F);
3834 -- What's the following about???
3836 if Is_Entity_Name (A) then
3837 Kill_Checks (Entity (A));
3843 if Etype (A) = Any_Type then
3844 Set_Etype (N, Any_Type);
3848 -- Apply appropriate range checks for in, out, and in-out
3849 -- parameters. Out and in-out parameters also need a separate
3850 -- check, if there is a type conversion, to make sure the return
3851 -- value meets the constraints of the variable before the
3854 -- Gigi looks at the check flag and uses the appropriate types.
3855 -- For now since one flag is used there is an optimization which
3856 -- might not be done in the In Out case since Gigi does not do
3857 -- any analysis. More thought required about this ???
3859 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3861 -- Apply predicate checks, unless this is a call to the
3862 -- predicate check function itself, which would cause an
3863 -- infinite recursion.
3865 if not (Ekind (Nam) = E_Function
3866 and then Has_Predicates (Nam))
3868 Apply_Predicate_Check (A, F_Typ);
3871 -- Apply required constraint checks
3873 if Is_Scalar_Type (Etype (A)) then
3874 Apply_Scalar_Range_Check (A, F_Typ);
3876 elsif Is_Array_Type (Etype (A)) then
3877 Apply_Length_Check (A, F_Typ);
3879 elsif Is_Record_Type (F_Typ)
3880 and then Has_Discriminants (F_Typ)
3881 and then Is_Constrained (F_Typ)
3882 and then (not Is_Derived_Type (F_Typ)
3883 or else Comes_From_Source (Nam))
3885 Apply_Discriminant_Check (A, F_Typ);
3887 elsif Is_Access_Type (F_Typ)
3888 and then Is_Array_Type (Designated_Type (F_Typ))
3889 and then Is_Constrained (Designated_Type (F_Typ))
3891 Apply_Length_Check (A, F_Typ);
3893 elsif Is_Access_Type (F_Typ)
3894 and then Has_Discriminants (Designated_Type (F_Typ))
3895 and then Is_Constrained (Designated_Type (F_Typ))
3897 Apply_Discriminant_Check (A, F_Typ);
3900 Apply_Range_Check (A, F_Typ);
3903 -- Ada 2005 (AI-231): Note that the controlling parameter case
3904 -- already existed in Ada 95, which is partially checked
3905 -- elsewhere (see Checks), and we don't want the warning
3906 -- message to differ.
3908 if Is_Access_Type (F_Typ)
3909 and then Can_Never_Be_Null (F_Typ)
3910 and then Known_Null (A)
3912 if Is_Controlling_Formal (F) then
3913 Apply_Compile_Time_Constraint_Error
3915 Msg => "null value not allowed here?",
3916 Reason => CE_Access_Check_Failed);
3918 elsif Ada_Version >= Ada_2005 then
3919 Apply_Compile_Time_Constraint_Error
3921 Msg => "(Ada 2005) null not allowed in "
3922 & "null-excluding formal?",
3923 Reason => CE_Null_Not_Allowed);
3928 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3929 if Nkind (A) = N_Type_Conversion then
3930 if Is_Scalar_Type (A_Typ) then
3931 Apply_Scalar_Range_Check
3932 (Expression (A), Etype (Expression (A)), A_Typ);
3935 (Expression (A), Etype (Expression (A)), A_Typ);
3939 if Is_Scalar_Type (F_Typ) then
3940 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3941 elsif Is_Array_Type (F_Typ)
3942 and then Ekind (F) = E_Out_Parameter
3944 Apply_Length_Check (A, F_Typ);
3946 Apply_Range_Check (A, A_Typ, F_Typ);
3951 -- An actual associated with an access parameter is implicitly
3952 -- converted to the anonymous access type of the formal and must
3953 -- satisfy the legality checks for access conversions.
3955 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3956 if not Valid_Conversion (A, F_Typ, A) then
3958 ("invalid implicit conversion for access parameter", A);
3962 -- Check bad case of atomic/volatile argument (RM C.6(12))
3964 if Is_By_Reference_Type (Etype (F))
3965 and then Comes_From_Source (N)
3967 if Is_Atomic_Object (A)
3968 and then not Is_Atomic (Etype (F))
3971 ("cannot pass atomic argument to non-atomic formal&",
3974 elsif Is_Volatile_Object (A)
3975 and then not Is_Volatile (Etype (F))
3978 ("cannot pass volatile argument to non-volatile formal&",
3983 -- Check that subprograms don't have improper controlling
3984 -- arguments (RM 3.9.2 (9)).
3986 -- A primitive operation may have an access parameter of an
3987 -- incomplete tagged type, but a dispatching call is illegal
3988 -- if the type is still incomplete.
3990 if Is_Controlling_Formal (F) then
3991 Set_Is_Controlling_Actual (A);
3993 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3995 Desig : constant Entity_Id := Designated_Type (Etype (F));
3997 if Ekind (Desig) = E_Incomplete_Type
3998 and then No (Full_View (Desig))
3999 and then No (Non_Limited_View (Desig))
4002 ("premature use of incomplete type& " &
4003 "in dispatching call", A, Desig);
4008 elsif Nkind (A) = N_Explicit_Dereference then
4009 Validate_Remote_Access_To_Class_Wide_Type (A);
4012 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4013 and then not Is_Class_Wide_Type (F_Typ)
4014 and then not Is_Controlling_Formal (F)
4016 Error_Msg_N ("class-wide argument not allowed here!", A);
4018 if Is_Subprogram (Nam)
4019 and then Comes_From_Source (Nam)
4021 Error_Msg_Node_2 := F_Typ;
4023 ("& is not a dispatching operation of &!", A, Nam);
4026 -- Apply the checks described in 3.10.2(27): if the context is a
4027 -- specific access-to-object, the actual cannot be class-wide.
4028 -- Use base type to exclude access_to_subprogram cases.
4030 elsif Is_Access_Type (A_Typ)
4031 and then Is_Access_Type (F_Typ)
4032 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4033 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4034 or else (Nkind (A) = N_Attribute_Reference
4036 Is_Class_Wide_Type (Etype (Prefix (A)))))
4037 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4038 and then not Is_Controlling_Formal (F)
4040 -- Disable these checks for call to imported C++ subprograms
4043 (Is_Entity_Name (Name (N))
4044 and then Is_Imported (Entity (Name (N)))
4045 and then Convention (Entity (Name (N))) = Convention_CPP)
4048 ("access to class-wide argument not allowed here!", A);
4050 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4051 Error_Msg_Node_2 := Designated_Type (F_Typ);
4053 ("& is not a dispatching operation of &!", A, Nam);
4059 -- If it is a named association, treat the selector_name as a
4060 -- proper identifier, and mark the corresponding entity. Ignore
4061 -- this reference in Alfa mode, as it refers to an entity not in
4062 -- scope at the point of reference, so the reference should be
4063 -- ignored for computing effects of subprograms.
4065 if Nkind (Parent (A)) = N_Parameter_Association
4066 and then not Alfa_Mode
4068 Set_Entity (Selector_Name (Parent (A)), F);
4069 Generate_Reference (F, Selector_Name (Parent (A)));
4070 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4071 Generate_Reference (F_Typ, N, ' ');
4076 if Ekind (F) /= E_Out_Parameter then
4077 Check_Unset_Reference (A);
4082 -- Case where actual is not present
4090 end Resolve_Actuals;
4092 -----------------------
4093 -- Resolve_Allocator --
4094 -----------------------
4096 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4097 Desig_T : constant Entity_Id := Designated_Type (Typ);
4098 E : constant Node_Id := Expression (N);
4100 Discrim : Entity_Id;
4103 Assoc : Node_Id := Empty;
4106 procedure Check_Allocator_Discrim_Accessibility
4107 (Disc_Exp : Node_Id;
4108 Alloc_Typ : Entity_Id);
4109 -- Check that accessibility level associated with an access discriminant
4110 -- initialized in an allocator by the expression Disc_Exp is not deeper
4111 -- than the level of the allocator type Alloc_Typ. An error message is
4112 -- issued if this condition is violated. Specialized checks are done for
4113 -- the cases of a constraint expression which is an access attribute or
4114 -- an access discriminant.
4116 function In_Dispatching_Context return Boolean;
4117 -- If the allocator is an actual in a call, it is allowed to be class-
4118 -- wide when the context is not because it is a controlling actual.
4120 -------------------------------------------
4121 -- Check_Allocator_Discrim_Accessibility --
4122 -------------------------------------------
4124 procedure Check_Allocator_Discrim_Accessibility
4125 (Disc_Exp : Node_Id;
4126 Alloc_Typ : Entity_Id)
4129 if Type_Access_Level (Etype (Disc_Exp)) >
4130 Deepest_Type_Access_Level (Alloc_Typ)
4133 ("operand type has deeper level than allocator type", Disc_Exp);
4135 -- When the expression is an Access attribute the level of the prefix
4136 -- object must not be deeper than that of the allocator's type.
4138 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4139 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4141 and then Object_Access_Level (Prefix (Disc_Exp)) >
4142 Deepest_Type_Access_Level (Alloc_Typ)
4145 ("prefix of attribute has deeper level than allocator type",
4148 -- When the expression is an access discriminant the check is against
4149 -- the level of the prefix object.
4151 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4152 and then Nkind (Disc_Exp) = N_Selected_Component
4153 and then Object_Access_Level (Prefix (Disc_Exp)) >
4154 Deepest_Type_Access_Level (Alloc_Typ)
4157 ("access discriminant has deeper level than allocator type",
4160 -- All other cases are legal
4165 end Check_Allocator_Discrim_Accessibility;
4167 ----------------------------
4168 -- In_Dispatching_Context --
4169 ----------------------------
4171 function In_Dispatching_Context return Boolean is
4172 Par : constant Node_Id := Parent (N);
4176 Nkind_In (Par, N_Function_Call,
4177 N_Procedure_Call_Statement)
4178 and then Is_Entity_Name (Name (Par))
4179 and then Is_Dispatching_Operation (Entity (Name (Par)));
4180 end In_Dispatching_Context;
4182 -- Start of processing for Resolve_Allocator
4185 -- Replace general access with specific type
4187 if Ekind (Etype (N)) = E_Allocator_Type then
4188 Set_Etype (N, Base_Type (Typ));
4191 if Is_Abstract_Type (Typ) then
4192 Error_Msg_N ("type of allocator cannot be abstract", N);
4195 -- For qualified expression, resolve the expression using the
4196 -- given subtype (nothing to do for type mark, subtype indication)
4198 if Nkind (E) = N_Qualified_Expression then
4199 if Is_Class_Wide_Type (Etype (E))
4200 and then not Is_Class_Wide_Type (Desig_T)
4201 and then not In_Dispatching_Context
4204 ("class-wide allocator not allowed for this access type", N);
4207 Resolve (Expression (E), Etype (E));
4208 Check_Unset_Reference (Expression (E));
4210 -- A qualified expression requires an exact match of the type,
4211 -- class-wide matching is not allowed.
4213 if (Is_Class_Wide_Type (Etype (Expression (E)))
4214 or else Is_Class_Wide_Type (Etype (E)))
4215 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4217 Wrong_Type (Expression (E), Etype (E));
4220 -- A special accessibility check is needed for allocators that
4221 -- constrain access discriminants. The level of the type of the
4222 -- expression used to constrain an access discriminant cannot be
4223 -- deeper than the type of the allocator (in contrast to access
4224 -- parameters, where the level of the actual can be arbitrary).
4226 -- We can't use Valid_Conversion to perform this check because
4227 -- in general the type of the allocator is unrelated to the type
4228 -- of the access discriminant.
4230 if Ekind (Typ) /= E_Anonymous_Access_Type
4231 or else Is_Local_Anonymous_Access (Typ)
4233 Subtyp := Entity (Subtype_Mark (E));
4235 Aggr := Original_Node (Expression (E));
4237 if Has_Discriminants (Subtyp)
4238 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4240 Discrim := First_Discriminant (Base_Type (Subtyp));
4242 -- Get the first component expression of the aggregate
4244 if Present (Expressions (Aggr)) then
4245 Disc_Exp := First (Expressions (Aggr));
4247 elsif Present (Component_Associations (Aggr)) then
4248 Assoc := First (Component_Associations (Aggr));
4250 if Present (Assoc) then
4251 Disc_Exp := Expression (Assoc);
4260 while Present (Discrim) and then Present (Disc_Exp) loop
4261 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4262 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4265 Next_Discriminant (Discrim);
4267 if Present (Discrim) then
4268 if Present (Assoc) then
4270 Disc_Exp := Expression (Assoc);
4272 elsif Present (Next (Disc_Exp)) then
4276 Assoc := First (Component_Associations (Aggr));
4278 if Present (Assoc) then
4279 Disc_Exp := Expression (Assoc);
4289 -- For a subtype mark or subtype indication, freeze the subtype
4292 Freeze_Expression (E);
4294 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4296 ("initialization required for access-to-constant allocator", N);
4299 -- A special accessibility check is needed for allocators that
4300 -- constrain access discriminants. The level of the type of the
4301 -- expression used to constrain an access discriminant cannot be
4302 -- deeper than the type of the allocator (in contrast to access
4303 -- parameters, where the level of the actual can be arbitrary).
4304 -- We can't use Valid_Conversion to perform this check because
4305 -- in general the type of the allocator is unrelated to the type
4306 -- of the access discriminant.
4308 if Nkind (Original_Node (E)) = N_Subtype_Indication
4309 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4310 or else Is_Local_Anonymous_Access (Typ))
4312 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4314 if Has_Discriminants (Subtyp) then
4315 Discrim := First_Discriminant (Base_Type (Subtyp));
4316 Constr := First (Constraints (Constraint (Original_Node (E))));
4317 while Present (Discrim) and then Present (Constr) loop
4318 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4319 if Nkind (Constr) = N_Discriminant_Association then
4320 Disc_Exp := Original_Node (Expression (Constr));
4322 Disc_Exp := Original_Node (Constr);
4325 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4328 Next_Discriminant (Discrim);
4335 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4336 -- check that the level of the type of the created object is not deeper
4337 -- than the level of the allocator's access type, since extensions can
4338 -- now occur at deeper levels than their ancestor types. This is a
4339 -- static accessibility level check; a run-time check is also needed in
4340 -- the case of an initialized allocator with a class-wide argument (see
4341 -- Expand_Allocator_Expression).
4343 if Ada_Version >= Ada_2005
4344 and then Is_Class_Wide_Type (Desig_T)
4347 Exp_Typ : Entity_Id;
4350 if Nkind (E) = N_Qualified_Expression then
4351 Exp_Typ := Etype (E);
4352 elsif Nkind (E) = N_Subtype_Indication then
4353 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4355 Exp_Typ := Entity (E);
4358 if Type_Access_Level (Exp_Typ) >
4359 Deepest_Type_Access_Level (Typ)
4361 if In_Instance_Body then
4362 Error_Msg_N ("?type in allocator has deeper level than" &
4363 " designated class-wide type", E);
4364 Error_Msg_N ("\?Program_Error will be raised at run time",
4367 Make_Raise_Program_Error (Sloc (N),
4368 Reason => PE_Accessibility_Check_Failed));
4371 -- Do not apply Ada 2005 accessibility checks on a class-wide
4372 -- allocator if the type given in the allocator is a formal
4373 -- type. A run-time check will be performed in the instance.
4375 elsif not Is_Generic_Type (Exp_Typ) then
4376 Error_Msg_N ("type in allocator has deeper level than" &
4377 " designated class-wide type", E);
4383 -- Check for allocation from an empty storage pool
4385 if No_Pool_Assigned (Typ) then
4386 Error_Msg_N ("allocation from empty storage pool!", N);
4388 -- If the context is an unchecked conversion, as may happen within an
4389 -- inlined subprogram, the allocator is being resolved with its own
4390 -- anonymous type. In that case, if the target type has a specific
4391 -- storage pool, it must be inherited explicitly by the allocator type.
4393 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4394 and then No (Associated_Storage_Pool (Typ))
4396 Set_Associated_Storage_Pool
4397 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4400 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4401 Check_Restriction (No_Anonymous_Allocators, N);
4404 -- Check that an allocator with task parts isn't for a nested access
4405 -- type when restriction No_Task_Hierarchy applies.
4407 if not Is_Library_Level_Entity (Base_Type (Typ))
4408 and then Has_Task (Base_Type (Desig_T))
4410 Check_Restriction (No_Task_Hierarchy, N);
4413 -- An erroneous allocator may be rewritten as a raise Program_Error
4416 if Nkind (N) = N_Allocator then
4418 -- An anonymous access discriminant is the definition of a
4421 if Ekind (Typ) = E_Anonymous_Access_Type
4422 and then Nkind (Associated_Node_For_Itype (Typ)) =
4423 N_Discriminant_Specification
4426 Discr : constant Entity_Id :=
4427 Defining_Identifier (Associated_Node_For_Itype (Typ));
4430 -- Ada 2012 AI05-0052: If the designated type of the allocator
4431 -- is limited, then the allocator shall not be used to define
4432 -- the value of an access discriminant unless the discriminated
4433 -- type is immutably limited.
4435 if Ada_Version >= Ada_2012
4436 and then Is_Limited_Type (Desig_T)
4437 and then not Is_Immutably_Limited_Type (Scope (Discr))
4440 ("only immutably limited types can have anonymous "
4441 & "access discriminants designating a limited type", N);
4445 -- Avoid marking an allocator as a dynamic coextension if it is
4446 -- within a static construct.
4448 if not Is_Static_Coextension (N) then
4449 Set_Is_Dynamic_Coextension (N);
4452 -- Cleanup for potential static coextensions
4455 Set_Is_Dynamic_Coextension (N, False);
4456 Set_Is_Static_Coextension (N, False);
4460 -- Report a simple error: if the designated object is a local task,
4461 -- its body has not been seen yet, and its activation will fail an
4462 -- elaboration check.
4464 if Is_Task_Type (Desig_T)
4465 and then Scope (Base_Type (Desig_T)) = Current_Scope
4466 and then Is_Compilation_Unit (Current_Scope)
4467 and then Ekind (Current_Scope) = E_Package
4468 and then not In_Package_Body (Current_Scope)
4470 Error_Msg_N ("cannot activate task before body seen?", N);
4471 Error_Msg_N ("\Program_Error will be raised at run time?", N);
4474 -- Ada 2012 (AI05-0111-3): Issue a warning whenever allocating a task
4475 -- or a type containing tasks on a subpool since the deallocation of
4476 -- the subpool may lead to undefined task behavior. Perform the check
4477 -- only when the allocator has not been converted into a Program_Error
4478 -- due to a previous error.
4480 if Ada_Version >= Ada_2012
4481 and then Nkind (N) = N_Allocator
4482 and then Present (Subpool_Handle_Name (N))
4483 and then Has_Task (Desig_T)
4485 Error_Msg_N ("?allocation of task on subpool may lead to " &
4486 "undefined behavior", N);
4488 end Resolve_Allocator;
4490 ---------------------------
4491 -- Resolve_Arithmetic_Op --
4492 ---------------------------
4494 -- Used for resolving all arithmetic operators except exponentiation
4496 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4497 L : constant Node_Id := Left_Opnd (N);
4498 R : constant Node_Id := Right_Opnd (N);
4499 TL : constant Entity_Id := Base_Type (Etype (L));
4500 TR : constant Entity_Id := Base_Type (Etype (R));
4504 B_Typ : constant Entity_Id := Base_Type (Typ);
4505 -- We do the resolution using the base type, because intermediate values
4506 -- in expressions always are of the base type, not a subtype of it.
4508 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4509 -- Returns True if N is in a context that expects "any real type"
4511 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4512 -- Return True iff given type is Integer or universal real/integer
4514 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4515 -- Choose type of integer literal in fixed-point operation to conform
4516 -- to available fixed-point type. T is the type of the other operand,
4517 -- which is needed to determine the expected type of N.
4519 procedure Set_Operand_Type (N : Node_Id);
4520 -- Set operand type to T if universal
4522 -------------------------------
4523 -- Expected_Type_Is_Any_Real --
4524 -------------------------------
4526 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4528 -- N is the expression after "delta" in a fixed_point_definition;
4531 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4532 N_Decimal_Fixed_Point_Definition,
4534 -- N is one of the bounds in a real_range_specification;
4537 N_Real_Range_Specification,
4539 -- N is the expression of a delta_constraint;
4542 N_Delta_Constraint);
4543 end Expected_Type_Is_Any_Real;
4545 -----------------------------
4546 -- Is_Integer_Or_Universal --
4547 -----------------------------
4549 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4551 Index : Interp_Index;
4555 if not Is_Overloaded (N) then
4557 return Base_Type (T) = Base_Type (Standard_Integer)
4558 or else T = Universal_Integer
4559 or else T = Universal_Real;
4561 Get_First_Interp (N, Index, It);
4562 while Present (It.Typ) loop
4563 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4564 or else It.Typ = Universal_Integer
4565 or else It.Typ = Universal_Real
4570 Get_Next_Interp (Index, It);
4575 end Is_Integer_Or_Universal;
4577 ----------------------------
4578 -- Set_Mixed_Mode_Operand --
4579 ----------------------------
4581 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4582 Index : Interp_Index;
4586 if Universal_Interpretation (N) = Universal_Integer then
4588 -- A universal integer literal is resolved as standard integer
4589 -- except in the case of a fixed-point result, where we leave it
4590 -- as universal (to be handled by Exp_Fixd later on)
4592 if Is_Fixed_Point_Type (T) then
4593 Resolve (N, Universal_Integer);
4595 Resolve (N, Standard_Integer);
4598 elsif Universal_Interpretation (N) = Universal_Real
4599 and then (T = Base_Type (Standard_Integer)
4600 or else T = Universal_Integer
4601 or else T = Universal_Real)
4603 -- A universal real can appear in a fixed-type context. We resolve
4604 -- the literal with that context, even though this might raise an
4605 -- exception prematurely (the other operand may be zero).
4609 elsif Etype (N) = Base_Type (Standard_Integer)
4610 and then T = Universal_Real
4611 and then Is_Overloaded (N)
4613 -- Integer arg in mixed-mode operation. Resolve with universal
4614 -- type, in case preference rule must be applied.
4616 Resolve (N, Universal_Integer);
4619 and then B_Typ /= Universal_Fixed
4621 -- Not a mixed-mode operation, resolve with context
4625 elsif Etype (N) = Any_Fixed then
4627 -- N may itself be a mixed-mode operation, so use context type
4631 elsif Is_Fixed_Point_Type (T)
4632 and then B_Typ = Universal_Fixed
4633 and then Is_Overloaded (N)
4635 -- Must be (fixed * fixed) operation, operand must have one
4636 -- compatible interpretation.
4638 Resolve (N, Any_Fixed);
4640 elsif Is_Fixed_Point_Type (B_Typ)
4641 and then (T = Universal_Real
4642 or else Is_Fixed_Point_Type (T))
4643 and then Is_Overloaded (N)
4645 -- C * F(X) in a fixed context, where C is a real literal or a
4646 -- fixed-point expression. F must have either a fixed type
4647 -- interpretation or an integer interpretation, but not both.
4649 Get_First_Interp (N, Index, It);
4650 while Present (It.Typ) loop
4651 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4652 if Analyzed (N) then
4653 Error_Msg_N ("ambiguous operand in fixed operation", N);
4655 Resolve (N, Standard_Integer);
4658 elsif Is_Fixed_Point_Type (It.Typ) then
4659 if Analyzed (N) then
4660 Error_Msg_N ("ambiguous operand in fixed operation", N);
4662 Resolve (N, It.Typ);
4666 Get_Next_Interp (Index, It);
4669 -- Reanalyze the literal with the fixed type of the context. If
4670 -- context is Universal_Fixed, we are within a conversion, leave
4671 -- the literal as a universal real because there is no usable
4672 -- fixed type, and the target of the conversion plays no role in
4686 if B_Typ = Universal_Fixed
4687 and then Nkind (Op2) = N_Real_Literal
4689 T2 := Universal_Real;
4694 Set_Analyzed (Op2, False);
4701 end Set_Mixed_Mode_Operand;
4703 ----------------------
4704 -- Set_Operand_Type --
4705 ----------------------
4707 procedure Set_Operand_Type (N : Node_Id) is
4709 if Etype (N) = Universal_Integer
4710 or else Etype (N) = Universal_Real
4714 end Set_Operand_Type;
4716 -- Start of processing for Resolve_Arithmetic_Op
4719 if Comes_From_Source (N)
4720 and then Ekind (Entity (N)) = E_Function
4721 and then Is_Imported (Entity (N))
4722 and then Is_Intrinsic_Subprogram (Entity (N))
4724 Resolve_Intrinsic_Operator (N, Typ);
4727 -- Special-case for mixed-mode universal expressions or fixed point type
4728 -- operation: each argument is resolved separately. The same treatment
4729 -- is required if one of the operands of a fixed point operation is
4730 -- universal real, since in this case we don't do a conversion to a
4731 -- specific fixed-point type (instead the expander handles the case).
4733 -- Set the type of the node to its universal interpretation because
4734 -- legality checks on an exponentiation operand need the context.
4736 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4737 and then Present (Universal_Interpretation (L))
4738 and then Present (Universal_Interpretation (R))
4740 Set_Etype (N, B_Typ);
4741 Resolve (L, Universal_Interpretation (L));
4742 Resolve (R, Universal_Interpretation (R));
4744 elsif (B_Typ = Universal_Real
4745 or else Etype (N) = Universal_Fixed
4746 or else (Etype (N) = Any_Fixed
4747 and then Is_Fixed_Point_Type (B_Typ))
4748 or else (Is_Fixed_Point_Type (B_Typ)
4749 and then (Is_Integer_Or_Universal (L)
4751 Is_Integer_Or_Universal (R))))
4752 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4754 if TL = Universal_Integer or else TR = Universal_Integer then
4755 Check_For_Visible_Operator (N, B_Typ);
4758 -- If context is a fixed type and one operand is integer, the other
4759 -- is resolved with the type of the context.
4761 if Is_Fixed_Point_Type (B_Typ)
4762 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4763 or else TL = Universal_Integer)
4768 elsif Is_Fixed_Point_Type (B_Typ)
4769 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4770 or else TR = Universal_Integer)
4776 Set_Mixed_Mode_Operand (L, TR);
4777 Set_Mixed_Mode_Operand (R, TL);
4780 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4781 -- multiplying operators from being used when the expected type is
4782 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4783 -- some cases where the expected type is actually Any_Real;
4784 -- Expected_Type_Is_Any_Real takes care of that case.
4786 if Etype (N) = Universal_Fixed
4787 or else Etype (N) = Any_Fixed
4789 if B_Typ = Universal_Fixed
4790 and then not Expected_Type_Is_Any_Real (N)
4791 and then not Nkind_In (Parent (N), N_Type_Conversion,
4792 N_Unchecked_Type_Conversion)
4794 Error_Msg_N ("type cannot be determined from context!", N);
4795 Error_Msg_N ("\explicit conversion to result type required", N);
4797 Set_Etype (L, Any_Type);
4798 Set_Etype (R, Any_Type);
4801 if Ada_Version = Ada_83
4802 and then Etype (N) = Universal_Fixed
4804 Nkind_In (Parent (N), N_Type_Conversion,
4805 N_Unchecked_Type_Conversion)
4808 ("(Ada 83) fixed-point operation "
4809 & "needs explicit conversion", N);
4812 -- The expected type is "any real type" in contexts like
4814 -- type T is delta <universal_fixed-expression> ...
4816 -- in which case we need to set the type to Universal_Real
4817 -- so that static expression evaluation will work properly.
4819 if Expected_Type_Is_Any_Real (N) then
4820 Set_Etype (N, Universal_Real);
4822 Set_Etype (N, B_Typ);
4826 elsif Is_Fixed_Point_Type (B_Typ)
4827 and then (Is_Integer_Or_Universal (L)
4828 or else Nkind (L) = N_Real_Literal
4829 or else Nkind (R) = N_Real_Literal
4830 or else Is_Integer_Or_Universal (R))
4832 Set_Etype (N, B_Typ);
4834 elsif Etype (N) = Any_Fixed then
4836 -- If no previous errors, this is only possible if one operand is
4837 -- overloaded and the context is universal. Resolve as such.
4839 Set_Etype (N, B_Typ);
4843 if (TL = Universal_Integer or else TL = Universal_Real)
4845 (TR = Universal_Integer or else TR = Universal_Real)
4847 Check_For_Visible_Operator (N, B_Typ);
4850 -- If the context is Universal_Fixed and the operands are also
4851 -- universal fixed, this is an error, unless there is only one
4852 -- applicable fixed_point type (usually Duration).
4854 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4855 T := Unique_Fixed_Point_Type (N);
4857 if T = Any_Type then
4870 -- If one of the arguments was resolved to a non-universal type.
4871 -- label the result of the operation itself with the same type.
4872 -- Do the same for the universal argument, if any.
4874 T := Intersect_Types (L, R);
4875 Set_Etype (N, Base_Type (T));
4876 Set_Operand_Type (L);
4877 Set_Operand_Type (R);
4880 Generate_Operator_Reference (N, Typ);
4881 Eval_Arithmetic_Op (N);
4883 -- In SPARK, a multiplication or division with operands of fixed point
4884 -- types shall be qualified or explicitly converted to identify the
4887 if (Is_Fixed_Point_Type (Etype (L))
4888 or else Is_Fixed_Point_Type (Etype (R)))
4889 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4891 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4893 Check_SPARK_Restriction
4894 ("operation should be qualified or explicitly converted", N);
4897 -- Set overflow and division checking bit. Much cleverer code needed
4898 -- here eventually and perhaps the Resolve routines should be separated
4899 -- for the various arithmetic operations, since they will need
4900 -- different processing. ???
4902 if Nkind (N) in N_Op then
4903 if not Overflow_Checks_Suppressed (Etype (N)) then
4904 Enable_Overflow_Check (N);
4907 -- Give warning if explicit division by zero
4909 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4910 and then not Division_Checks_Suppressed (Etype (N))
4912 Rop := Right_Opnd (N);
4914 if Compile_Time_Known_Value (Rop)
4915 and then ((Is_Integer_Type (Etype (Rop))
4916 and then Expr_Value (Rop) = Uint_0)
4918 (Is_Real_Type (Etype (Rop))
4919 and then Expr_Value_R (Rop) = Ureal_0))
4921 -- Specialize the warning message according to the operation.
4922 -- The following warnings are for the case
4927 -- For division, we have two cases, for float division
4928 -- of an unconstrained float type, on a machine where
4929 -- Machine_Overflows is false, we don't get an exception
4930 -- at run-time, but rather an infinity or Nan. The Nan
4931 -- case is pretty obscure, so just warn about infinities.
4933 if Is_Floating_Point_Type (Typ)
4934 and then not Is_Constrained (Typ)
4935 and then not Machine_Overflows_On_Target
4938 ("float division by zero, " &
4939 "may generate '+'/'- infinity?", Right_Opnd (N));
4941 -- For all other cases, we get a Constraint_Error
4944 Apply_Compile_Time_Constraint_Error
4945 (N, "division by zero?", CE_Divide_By_Zero,
4946 Loc => Sloc (Right_Opnd (N)));
4950 Apply_Compile_Time_Constraint_Error
4951 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4952 Loc => Sloc (Right_Opnd (N)));
4955 Apply_Compile_Time_Constraint_Error
4956 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4957 Loc => Sloc (Right_Opnd (N)));
4959 -- Division by zero can only happen with division, rem,
4960 -- and mod operations.
4963 raise Program_Error;
4966 -- Otherwise just set the flag to check at run time
4969 Activate_Division_Check (N);
4973 -- If Restriction No_Implicit_Conditionals is active, then it is
4974 -- violated if either operand can be negative for mod, or for rem
4975 -- if both operands can be negative.
4977 if Restriction_Check_Required (No_Implicit_Conditionals)
4978 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4987 -- Set if corresponding operand might be negative
4991 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4992 LNeg := (not OK) or else Lo < 0;
4995 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4996 RNeg := (not OK) or else Lo < 0;
4998 -- Check if we will be generating conditionals. There are two
4999 -- cases where that can happen, first for REM, the only case
5000 -- is largest negative integer mod -1, where the division can
5001 -- overflow, but we still have to give the right result. The
5002 -- front end generates a test for this annoying case. Here we
5003 -- just test if both operands can be negative (that's what the
5004 -- expander does, so we match its logic here).
5006 -- The second case is mod where either operand can be negative.
5007 -- In this case, the back end has to generate additional tests.
5009 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5011 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5013 Check_Restriction (No_Implicit_Conditionals, N);
5019 Check_Unset_Reference (L);
5020 Check_Unset_Reference (R);
5021 end Resolve_Arithmetic_Op;
5027 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5028 Loc : constant Source_Ptr := Sloc (N);
5029 Subp : constant Node_Id := Name (N);
5037 function Same_Or_Aliased_Subprograms
5039 E : Entity_Id) return Boolean;
5040 -- Returns True if the subprogram entity S is the same as E or else
5041 -- S is an alias of E.
5043 ---------------------------------
5044 -- Same_Or_Aliased_Subprograms --
5045 ---------------------------------
5047 function Same_Or_Aliased_Subprograms
5049 E : Entity_Id) return Boolean
5051 Subp_Alias : constant Entity_Id := Alias (S);
5054 or else (Present (Subp_Alias) and then Subp_Alias = E);
5055 end Same_Or_Aliased_Subprograms;
5057 -- Start of processing for Resolve_Call
5060 -- The context imposes a unique interpretation with type Typ on a
5061 -- procedure or function call. Find the entity of the subprogram that
5062 -- yields the expected type, and propagate the corresponding formal
5063 -- constraints on the actuals. The caller has established that an
5064 -- interpretation exists, and emitted an error if not unique.
5066 -- First deal with the case of a call to an access-to-subprogram,
5067 -- dereference made explicit in Analyze_Call.
5069 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5070 if not Is_Overloaded (Subp) then
5071 Nam := Etype (Subp);
5074 -- Find the interpretation whose type (a subprogram type) has a
5075 -- return type that is compatible with the context. Analysis of
5076 -- the node has established that one exists.
5080 Get_First_Interp (Subp, I, It);
5081 while Present (It.Typ) loop
5082 if Covers (Typ, Etype (It.Typ)) then
5087 Get_Next_Interp (I, It);
5091 raise Program_Error;
5095 -- If the prefix is not an entity, then resolve it
5097 if not Is_Entity_Name (Subp) then
5098 Resolve (Subp, Nam);
5101 -- For an indirect call, we always invalidate checks, since we do not
5102 -- know whether the subprogram is local or global. Yes we could do
5103 -- better here, e.g. by knowing that there are no local subprograms,
5104 -- but it does not seem worth the effort. Similarly, we kill all
5105 -- knowledge of current constant values.
5107 Kill_Current_Values;
5109 -- If this is a procedure call which is really an entry call, do
5110 -- the conversion of the procedure call to an entry call. Protected
5111 -- operations use the same circuitry because the name in the call
5112 -- can be an arbitrary expression with special resolution rules.
5114 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5115 or else (Is_Entity_Name (Subp)
5116 and then Ekind (Entity (Subp)) = E_Entry)
5118 Resolve_Entry_Call (N, Typ);
5119 Check_Elab_Call (N);
5121 -- Kill checks and constant values, as above for indirect case
5122 -- Who knows what happens when another task is activated?
5124 Kill_Current_Values;
5127 -- Normal subprogram call with name established in Resolve
5129 elsif not (Is_Type (Entity (Subp))) then
5130 Nam := Entity (Subp);
5131 Set_Entity_With_Style_Check (Subp, Nam);
5133 -- Otherwise we must have the case of an overloaded call
5136 pragma Assert (Is_Overloaded (Subp));
5138 -- Initialize Nam to prevent warning (we know it will be assigned
5139 -- in the loop below, but the compiler does not know that).
5143 Get_First_Interp (Subp, I, It);
5144 while Present (It.Typ) loop
5145 if Covers (Typ, It.Typ) then
5147 Set_Entity_With_Style_Check (Subp, Nam);
5151 Get_Next_Interp (I, It);
5155 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5156 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5157 and then Nkind (Subp) /= N_Explicit_Dereference
5158 and then Present (Parameter_Associations (N))
5160 -- The prefix is a parameterless function call that returns an access
5161 -- to subprogram. If parameters are present in the current call, add
5162 -- add an explicit dereference. We use the base type here because
5163 -- within an instance these may be subtypes.
5165 -- The dereference is added either in Analyze_Call or here. Should
5166 -- be consolidated ???
5168 Set_Is_Overloaded (Subp, False);
5169 Set_Etype (Subp, Etype (Nam));
5170 Insert_Explicit_Dereference (Subp);
5171 Nam := Designated_Type (Etype (Nam));
5172 Resolve (Subp, Nam);
5175 -- Check that a call to Current_Task does not occur in an entry body
5177 if Is_RTE (Nam, RE_Current_Task) then
5186 -- Exclude calls that occur within the default of a formal
5187 -- parameter of the entry, since those are evaluated outside
5190 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5192 if Nkind (P) = N_Entry_Body
5193 or else (Nkind (P) = N_Subprogram_Body
5194 and then Is_Entry_Barrier_Function (P))
5198 ("?& should not be used in entry body (RM C.7(17))",
5201 ("\Program_Error will be raised at run time?", N, Nam);
5203 Make_Raise_Program_Error (Loc,
5204 Reason => PE_Current_Task_In_Entry_Body));
5205 Set_Etype (N, Rtype);
5212 -- Check that a procedure call does not occur in the context of the
5213 -- entry call statement of a conditional or timed entry call. Note that
5214 -- the case of a call to a subprogram renaming of an entry will also be
5215 -- rejected. The test for N not being an N_Entry_Call_Statement is
5216 -- defensive, covering the possibility that the processing of entry
5217 -- calls might reach this point due to later modifications of the code
5220 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5221 and then Nkind (N) /= N_Entry_Call_Statement
5222 and then Entry_Call_Statement (Parent (N)) = N
5224 if Ada_Version < Ada_2005 then
5225 Error_Msg_N ("entry call required in select statement", N);
5227 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5228 -- for a procedure_or_entry_call, the procedure_name or
5229 -- procedure_prefix of the procedure_call_statement shall denote
5230 -- an entry renamed by a procedure, or (a view of) a primitive
5231 -- subprogram of a limited interface whose first parameter is
5232 -- a controlling parameter.
5234 elsif Nkind (N) = N_Procedure_Call_Statement
5235 and then not Is_Renamed_Entry (Nam)
5236 and then not Is_Controlling_Limited_Procedure (Nam)
5239 ("entry call or dispatching primitive of interface required", N);
5243 -- Check that this is not a call to a protected procedure or entry from
5244 -- within a protected function.
5246 if Ekind (Current_Scope) = E_Function
5247 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5248 and then Ekind (Nam) /= E_Function
5249 and then Scope (Nam) = Scope (Current_Scope)
5251 Error_Msg_N ("within protected function, protected " &
5252 "object is constant", N);
5253 Error_Msg_N ("\cannot call operation that may modify it", N);
5256 -- Freeze the subprogram name if not in a spec-expression. Note that we
5257 -- freeze procedure calls as well as function calls. Procedure calls are
5258 -- not frozen according to the rules (RM 13.14(14)) because it is
5259 -- impossible to have a procedure call to a non-frozen procedure in pure
5260 -- Ada, but in the code that we generate in the expander, this rule
5261 -- needs extending because we can generate procedure calls that need
5264 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5265 Freeze_Expression (Subp);
5268 -- For a predefined operator, the type of the result is the type imposed
5269 -- by context, except for a predefined operation on universal fixed.
5270 -- Otherwise The type of the call is the type returned by the subprogram
5273 if Is_Predefined_Op (Nam) then
5274 if Etype (N) /= Universal_Fixed then
5278 -- If the subprogram returns an array type, and the context requires the
5279 -- component type of that array type, the node is really an indexing of
5280 -- the parameterless call. Resolve as such. A pathological case occurs
5281 -- when the type of the component is an access to the array type. In
5282 -- this case the call is truly ambiguous.
5284 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5286 ((Is_Array_Type (Etype (Nam))
5287 and then Covers (Typ, Component_Type (Etype (Nam))))
5288 or else (Is_Access_Type (Etype (Nam))
5289 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5293 Component_Type (Designated_Type (Etype (Nam))))))
5296 Index_Node : Node_Id;
5298 Ret_Type : constant Entity_Id := Etype (Nam);
5301 if Is_Access_Type (Ret_Type)
5302 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5305 ("cannot disambiguate function call and indexing", N);
5307 New_Subp := Relocate_Node (Subp);
5308 Set_Entity (Subp, Nam);
5310 if (Is_Array_Type (Ret_Type)
5311 and then Component_Type (Ret_Type) /= Any_Type)
5313 (Is_Access_Type (Ret_Type)
5315 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5317 if Needs_No_Actuals (Nam) then
5319 -- Indexed call to a parameterless function
5322 Make_Indexed_Component (Loc,
5324 Make_Function_Call (Loc,
5326 Expressions => Parameter_Associations (N));
5328 -- An Ada 2005 prefixed call to a primitive operation
5329 -- whose first parameter is the prefix. This prefix was
5330 -- prepended to the parameter list, which is actually a
5331 -- list of indexes. Remove the prefix in order to build
5332 -- the proper indexed component.
5335 Make_Indexed_Component (Loc,
5337 Make_Function_Call (Loc,
5339 Parameter_Associations =>
5341 (Remove_Head (Parameter_Associations (N)))),
5342 Expressions => Parameter_Associations (N));
5345 -- Preserve the parenthesis count of the node
5347 Set_Paren_Count (Index_Node, Paren_Count (N));
5349 -- Since we are correcting a node classification error made
5350 -- by the parser, we call Replace rather than Rewrite.
5352 Replace (N, Index_Node);
5354 Set_Etype (Prefix (N), Ret_Type);
5356 Resolve_Indexed_Component (N, Typ);
5357 Check_Elab_Call (Prefix (N));
5365 Set_Etype (N, Etype (Nam));
5368 -- In the case where the call is to an overloaded subprogram, Analyze
5369 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5370 -- such a case Normalize_Actuals needs to be called once more to order
5371 -- the actuals correctly. Otherwise the call will have the ordering
5372 -- given by the last overloaded subprogram whether this is the correct
5373 -- one being called or not.
5375 if Is_Overloaded (Subp) then
5376 Normalize_Actuals (N, Nam, False, Norm_OK);
5377 pragma Assert (Norm_OK);
5380 -- In any case, call is fully resolved now. Reset Overload flag, to
5381 -- prevent subsequent overload resolution if node is analyzed again
5383 Set_Is_Overloaded (Subp, False);
5384 Set_Is_Overloaded (N, False);
5386 -- If we are calling the current subprogram from immediately within its
5387 -- body, then that is the case where we can sometimes detect cases of
5388 -- infinite recursion statically. Do not try this in case restriction
5389 -- No_Recursion is in effect anyway, and do it only for source calls.
5391 if Comes_From_Source (N) then
5392 Scop := Current_Scope;
5394 -- Issue warning for possible infinite recursion in the absence
5395 -- of the No_Recursion restriction.
5397 if Same_Or_Aliased_Subprograms (Nam, Scop)
5398 and then not Restriction_Active (No_Recursion)
5399 and then Check_Infinite_Recursion (N)
5401 -- Here we detected and flagged an infinite recursion, so we do
5402 -- not need to test the case below for further warnings. Also we
5403 -- are all done if we now have a raise SE node.
5405 if Nkind (N) = N_Raise_Storage_Error then
5409 -- If call is to immediately containing subprogram, then check for
5410 -- the case of a possible run-time detectable infinite recursion.
5413 Scope_Loop : while Scop /= Standard_Standard loop
5414 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5416 -- Although in general case, recursion is not statically
5417 -- checkable, the case of calling an immediately containing
5418 -- subprogram is easy to catch.
5420 Check_Restriction (No_Recursion, N);
5422 -- If the recursive call is to a parameterless subprogram,
5423 -- then even if we can't statically detect infinite
5424 -- recursion, this is pretty suspicious, and we output a
5425 -- warning. Furthermore, we will try later to detect some
5426 -- cases here at run time by expanding checking code (see
5427 -- Detect_Infinite_Recursion in package Exp_Ch6).
5429 -- If the recursive call is within a handler, do not emit a
5430 -- warning, because this is a common idiom: loop until input
5431 -- is correct, catch illegal input in handler and restart.
5433 if No (First_Formal (Nam))
5434 and then Etype (Nam) = Standard_Void_Type
5435 and then not Error_Posted (N)
5436 and then Nkind (Parent (N)) /= N_Exception_Handler
5438 -- For the case of a procedure call. We give the message
5439 -- only if the call is the first statement in a sequence
5440 -- of statements, or if all previous statements are
5441 -- simple assignments. This is simply a heuristic to
5442 -- decrease false positives, without losing too many good
5443 -- warnings. The idea is that these previous statements
5444 -- may affect global variables the procedure depends on.
5445 -- We also exclude raise statements, that may arise from
5446 -- constraint checks and are probably unrelated to the
5447 -- intended control flow.
5449 if Nkind (N) = N_Procedure_Call_Statement
5450 and then Is_List_Member (N)
5456 while Present (P) loop
5458 N_Assignment_Statement,
5459 N_Raise_Constraint_Error)
5469 -- Do not give warning if we are in a conditional context
5472 K : constant Node_Kind := Nkind (Parent (N));
5474 if (K = N_Loop_Statement
5475 and then Present (Iteration_Scheme (Parent (N))))
5476 or else K = N_If_Statement
5477 or else K = N_Elsif_Part
5478 or else K = N_Case_Statement_Alternative
5484 -- Here warning is to be issued
5486 Set_Has_Recursive_Call (Nam);
5488 ("?possible infinite recursion!", N);
5490 ("\?Storage_Error may be raised at run time!", N);
5496 Scop := Scope (Scop);
5497 end loop Scope_Loop;
5501 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5503 Check_Obsolescent_2005_Entity (Nam, Subp);
5505 -- If subprogram name is a predefined operator, it was given in
5506 -- functional notation. Replace call node with operator node, so
5507 -- that actuals can be resolved appropriately.
5509 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5510 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5513 elsif Present (Alias (Nam))
5514 and then Is_Predefined_Op (Alias (Nam))
5516 Resolve_Actuals (N, Nam);
5517 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5521 -- Create a transient scope if the resulting type requires it
5523 -- There are several notable exceptions:
5525 -- a) In init procs, the transient scope overhead is not needed, and is
5526 -- even incorrect when the call is a nested initialization call for a
5527 -- component whose expansion may generate adjust calls. However, if the
5528 -- call is some other procedure call within an initialization procedure
5529 -- (for example a call to Create_Task in the init_proc of the task
5530 -- run-time record) a transient scope must be created around this call.
5532 -- b) Enumeration literal pseudo-calls need no transient scope
5534 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5535 -- functions) do not use the secondary stack even though the return
5536 -- type may be unconstrained.
5538 -- d) Calls to a build-in-place function, since such functions may
5539 -- allocate their result directly in a target object, and cases where
5540 -- the result does get allocated in the secondary stack are checked for
5541 -- within the specialized Exp_Ch6 procedures for expanding those
5542 -- build-in-place calls.
5544 -- e) If the subprogram is marked Inline_Always, then even if it returns
5545 -- an unconstrained type the call does not require use of the secondary
5546 -- stack. However, inlining will only take place if the body to inline
5547 -- is already present. It may not be available if e.g. the subprogram is
5548 -- declared in a child instance.
5550 -- If this is an initialization call for a type whose construction
5551 -- uses the secondary stack, and it is not a nested call to initialize
5552 -- a component, we do need to create a transient scope for it. We
5553 -- check for this by traversing the type in Check_Initialization_Call.
5556 and then Has_Pragma_Inline_Always (Nam)
5557 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5558 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5562 elsif Ekind (Nam) = E_Enumeration_Literal
5563 or else Is_Build_In_Place_Function (Nam)
5564 or else Is_Intrinsic_Subprogram (Nam)
5568 elsif Full_Expander_Active
5569 and then Is_Type (Etype (Nam))
5570 and then Requires_Transient_Scope (Etype (Nam))
5572 (not Within_Init_Proc
5574 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5576 Establish_Transient_Scope (N, Sec_Stack => True);
5578 -- If the call appears within the bounds of a loop, it will
5579 -- be rewritten and reanalyzed, nothing left to do here.
5581 if Nkind (N) /= N_Function_Call then
5585 elsif Is_Init_Proc (Nam)
5586 and then not Within_Init_Proc
5588 Check_Initialization_Call (N, Nam);
5591 -- A protected function cannot be called within the definition of the
5592 -- enclosing protected type.
5594 if Is_Protected_Type (Scope (Nam))
5595 and then In_Open_Scopes (Scope (Nam))
5596 and then not Has_Completion (Scope (Nam))
5599 ("& cannot be called before end of protected definition", N, Nam);
5602 -- Propagate interpretation to actuals, and add default expressions
5605 if Present (First_Formal (Nam)) then
5606 Resolve_Actuals (N, Nam);
5608 -- Overloaded literals are rewritten as function calls, for purpose of
5609 -- resolution. After resolution, we can replace the call with the
5612 elsif Ekind (Nam) = E_Enumeration_Literal then
5613 Copy_Node (Subp, N);
5614 Resolve_Entity_Name (N, Typ);
5616 -- Avoid validation, since it is a static function call
5618 Generate_Reference (Nam, Subp);
5622 -- If the subprogram is not global, then kill all saved values and
5623 -- checks. This is a bit conservative, since in many cases we could do
5624 -- better, but it is not worth the effort. Similarly, we kill constant
5625 -- values. However we do not need to do this for internal entities
5626 -- (unless they are inherited user-defined subprograms), since they
5627 -- are not in the business of molesting local values.
5629 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5630 -- kill all checks and values for calls to global subprograms. This
5631 -- takes care of the case where an access to a local subprogram is
5632 -- taken, and could be passed directly or indirectly and then called
5633 -- from almost any context.
5635 -- Note: we do not do this step till after resolving the actuals. That
5636 -- way we still take advantage of the current value information while
5637 -- scanning the actuals.
5639 -- We suppress killing values if we are processing the nodes associated
5640 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5641 -- type kills all the values as part of analyzing the code that
5642 -- initializes the dispatch tables.
5644 if Inside_Freezing_Actions = 0
5645 and then (not Is_Library_Level_Entity (Nam)
5646 or else Suppress_Value_Tracking_On_Call
5647 (Nearest_Dynamic_Scope (Current_Scope)))
5648 and then (Comes_From_Source (Nam)
5649 or else (Present (Alias (Nam))
5650 and then Comes_From_Source (Alias (Nam))))
5652 Kill_Current_Values;
5655 -- If we are warning about unread OUT parameters, this is the place to
5656 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5657 -- after the above call to Kill_Current_Values (since that call clears
5658 -- the Last_Assignment field of all local variables).
5660 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5661 and then Comes_From_Source (N)
5662 and then In_Extended_Main_Source_Unit (N)
5669 F := First_Formal (Nam);
5670 A := First_Actual (N);
5671 while Present (F) and then Present (A) loop
5672 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5673 and then Warn_On_Modified_As_Out_Parameter (F)
5674 and then Is_Entity_Name (A)
5675 and then Present (Entity (A))
5676 and then Comes_From_Source (N)
5677 and then Safe_To_Capture_Value (N, Entity (A))
5679 Set_Last_Assignment (Entity (A), A);
5688 -- If the subprogram is a primitive operation, check whether or not
5689 -- it is a correct dispatching call.
5691 if Is_Overloadable (Nam)
5692 and then Is_Dispatching_Operation (Nam)
5694 Check_Dispatching_Call (N);
5696 elsif Ekind (Nam) /= E_Subprogram_Type
5697 and then Is_Abstract_Subprogram (Nam)
5698 and then not In_Instance
5700 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5703 -- If this is a dispatching call, generate the appropriate reference,
5704 -- for better source navigation in GPS.
5706 if Is_Overloadable (Nam)
5707 and then Present (Controlling_Argument (N))
5709 Generate_Reference (Nam, Subp, 'R');
5711 -- Normal case, not a dispatching call: generate a call reference
5714 Generate_Reference (Nam, Subp, 's');
5717 if Is_Intrinsic_Subprogram (Nam) then
5718 Check_Intrinsic_Call (N);
5721 -- Check for violation of restriction No_Specific_Termination_Handlers
5722 -- and warn on a potentially blocking call to Abort_Task.
5724 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5725 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5727 Is_RTE (Nam, RE_Specific_Handler))
5729 Check_Restriction (No_Specific_Termination_Handlers, N);
5731 elsif Is_RTE (Nam, RE_Abort_Task) then
5732 Check_Potentially_Blocking_Operation (N);
5735 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5736 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5737 -- need to check the second argument to determine whether it is an
5738 -- absolute or relative timing event.
5740 if Restriction_Check_Required (No_Relative_Delay)
5741 and then Is_RTE (Nam, RE_Set_Handler)
5742 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5744 Check_Restriction (No_Relative_Delay, N);
5747 -- Issue an error for a call to an eliminated subprogram. We skip this
5748 -- in a spec expression, e.g. a call in a default parameter value, since
5749 -- we are not really doing a call at this time. That's important because
5750 -- the spec expression may itself belong to an eliminated subprogram.
5752 if not In_Spec_Expression then
5753 Check_For_Eliminated_Subprogram (Subp, Nam);
5756 -- In formal mode, the primitive operations of a tagged type or type
5757 -- extension do not include functions that return the tagged type.
5759 -- Commented out as the call to Is_Inherited_Operation_For_Type may
5760 -- cause an error because the type entity of the parent node of
5761 -- Entity (Name (N) may not be set. ???
5762 -- So why not just add a guard ???
5764 -- if Nkind (N) = N_Function_Call
5765 -- and then Is_Tagged_Type (Etype (N))
5766 -- and then Is_Entity_Name (Name (N))
5767 -- and then Is_Inherited_Operation_For_Type
5768 -- (Entity (Name (N)), Etype (N))
5770 -- Check_SPARK_Restriction ("function not inherited", N);
5773 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5774 -- class-wide and the call dispatches on result in a context that does
5775 -- not provide a tag, the call raises Program_Error.
5777 if Nkind (N) = N_Function_Call
5778 and then In_Instance
5779 and then Is_Generic_Actual_Type (Typ)
5780 and then Is_Class_Wide_Type (Typ)
5781 and then Has_Controlling_Result (Nam)
5782 and then Nkind (Parent (N)) = N_Object_Declaration
5784 -- Verify that none of the formals are controlling
5787 Call_OK : Boolean := False;
5791 F := First_Formal (Nam);
5792 while Present (F) loop
5793 if Is_Controlling_Formal (F) then
5802 Error_Msg_N ("!? cannot determine tag of result", N);
5803 Error_Msg_N ("!? Program_Error will be raised", N);
5805 Make_Raise_Program_Error (Sloc (N),
5806 Reason => PE_Explicit_Raise));
5811 -- All done, evaluate call and deal with elaboration issues
5814 Check_Elab_Call (N);
5815 Warn_On_Overlapping_Actuals (Nam, N);
5818 -----------------------------
5819 -- Resolve_Case_Expression --
5820 -----------------------------
5822 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5826 Alt := First (Alternatives (N));
5827 while Present (Alt) loop
5828 Resolve (Expression (Alt), Typ);
5833 Eval_Case_Expression (N);
5834 end Resolve_Case_Expression;
5836 -------------------------------
5837 -- Resolve_Character_Literal --
5838 -------------------------------
5840 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5841 B_Typ : constant Entity_Id := Base_Type (Typ);
5845 -- Verify that the character does belong to the type of the context
5847 Set_Etype (N, B_Typ);
5848 Eval_Character_Literal (N);
5850 -- Wide_Wide_Character literals must always be defined, since the set
5851 -- of wide wide character literals is complete, i.e. if a character
5852 -- literal is accepted by the parser, then it is OK for wide wide
5853 -- character (out of range character literals are rejected).
5855 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5858 -- Always accept character literal for type Any_Character, which
5859 -- occurs in error situations and in comparisons of literals, both
5860 -- of which should accept all literals.
5862 elsif B_Typ = Any_Character then
5865 -- For Standard.Character or a type derived from it, check that the
5866 -- literal is in range.
5868 elsif Root_Type (B_Typ) = Standard_Character then
5869 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5873 -- For Standard.Wide_Character or a type derived from it, check that the
5874 -- literal is in range.
5876 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5877 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5881 -- For Standard.Wide_Wide_Character or a type derived from it, we
5882 -- know the literal is in range, since the parser checked!
5884 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5887 -- If the entity is already set, this has already been resolved in a
5888 -- generic context, or comes from expansion. Nothing else to do.
5890 elsif Present (Entity (N)) then
5893 -- Otherwise we have a user defined character type, and we can use the
5894 -- standard visibility mechanisms to locate the referenced entity.
5897 C := Current_Entity (N);
5898 while Present (C) loop
5899 if Etype (C) = B_Typ then
5900 Set_Entity_With_Style_Check (N, C);
5901 Generate_Reference (C, N);
5909 -- If we fall through, then the literal does not match any of the
5910 -- entries of the enumeration type. This isn't just a constraint error
5911 -- situation, it is an illegality (see RM 4.2).
5914 ("character not defined for }", N, First_Subtype (B_Typ));
5915 end Resolve_Character_Literal;
5917 ---------------------------
5918 -- Resolve_Comparison_Op --
5919 ---------------------------
5921 -- Context requires a boolean type, and plays no role in resolution.
5922 -- Processing identical to that for equality operators. The result type is
5923 -- the base type, which matters when pathological subtypes of booleans with
5924 -- limited ranges are used.
5926 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5927 L : constant Node_Id := Left_Opnd (N);
5928 R : constant Node_Id := Right_Opnd (N);
5932 -- If this is an intrinsic operation which is not predefined, use the
5933 -- types of its declared arguments to resolve the possibly overloaded
5934 -- operands. Otherwise the operands are unambiguous and specify the
5937 if Scope (Entity (N)) /= Standard_Standard then
5938 T := Etype (First_Entity (Entity (N)));
5941 T := Find_Unique_Type (L, R);
5943 if T = Any_Fixed then
5944 T := Unique_Fixed_Point_Type (L);
5948 Set_Etype (N, Base_Type (Typ));
5949 Generate_Reference (T, N, ' ');
5951 -- Skip remaining processing if already set to Any_Type
5953 if T = Any_Type then
5957 -- Deal with other error cases
5959 if T = Any_String or else
5960 T = Any_Composite or else
5963 if T = Any_Character then
5964 Ambiguous_Character (L);
5966 Error_Msg_N ("ambiguous operands for comparison", N);
5969 Set_Etype (N, Any_Type);
5973 -- Resolve the operands if types OK
5977 Check_Unset_Reference (L);
5978 Check_Unset_Reference (R);
5979 Generate_Operator_Reference (N, T);
5980 Check_Low_Bound_Tested (N);
5982 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
5983 -- types or array types except String.
5985 if Is_Boolean_Type (T) then
5986 Check_SPARK_Restriction
5987 ("comparison is not defined on Boolean type", N);
5989 elsif Is_Array_Type (T)
5990 and then Base_Type (T) /= Standard_String
5992 Check_SPARK_Restriction
5993 ("comparison is not defined on array types other than String", N);
5996 -- Check comparison on unordered enumeration
5998 if Comes_From_Source (N)
5999 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
6001 Error_Msg_N ("comparison on unordered enumeration type?", N);
6004 -- Evaluate the relation (note we do this after the above check since
6005 -- this Eval call may change N to True/False.
6007 Eval_Relational_Op (N);
6008 end Resolve_Comparison_Op;
6010 ------------------------------------
6011 -- Resolve_Conditional_Expression --
6012 ------------------------------------
6014 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
6015 Condition : constant Node_Id := First (Expressions (N));
6016 Then_Expr : constant Node_Id := Next (Condition);
6017 Else_Expr : Node_Id := Next (Then_Expr);
6020 Resolve (Condition, Any_Boolean);
6021 Resolve (Then_Expr, Typ);
6023 -- If ELSE expression present, just resolve using the determined type
6025 if Present (Else_Expr) then
6026 Resolve (Else_Expr, Typ);
6028 -- If no ELSE expression is present, root type must be Standard.Boolean
6029 -- and we provide a Standard.True result converted to the appropriate
6030 -- Boolean type (in case it is a derived boolean type).
6032 elsif Root_Type (Typ) = Standard_Boolean then
6034 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
6035 Analyze_And_Resolve (Else_Expr, Typ);
6036 Append_To (Expressions (N), Else_Expr);
6039 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
6040 Append_To (Expressions (N), Error);
6044 Eval_Conditional_Expression (N);
6045 end Resolve_Conditional_Expression;
6047 -----------------------------------------
6048 -- Resolve_Discrete_Subtype_Indication --
6049 -----------------------------------------
6051 procedure Resolve_Discrete_Subtype_Indication
6059 Analyze (Subtype_Mark (N));
6060 S := Entity (Subtype_Mark (N));
6062 if Nkind (Constraint (N)) /= N_Range_Constraint then
6063 Error_Msg_N ("expect range constraint for discrete type", N);
6064 Set_Etype (N, Any_Type);
6067 R := Range_Expression (Constraint (N));
6075 if Base_Type (S) /= Base_Type (Typ) then
6077 ("expect subtype of }", N, First_Subtype (Typ));
6079 -- Rewrite the constraint as a range of Typ
6080 -- to allow compilation to proceed further.
6083 Rewrite (Low_Bound (R),
6084 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6085 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6086 Attribute_Name => Name_First));
6087 Rewrite (High_Bound (R),
6088 Make_Attribute_Reference (Sloc (High_Bound (R)),
6089 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6090 Attribute_Name => Name_First));
6094 Set_Etype (N, Etype (R));
6096 -- Additionally, we must check that the bounds are compatible
6097 -- with the given subtype, which might be different from the
6098 -- type of the context.
6100 Apply_Range_Check (R, S);
6102 -- ??? If the above check statically detects a Constraint_Error
6103 -- it replaces the offending bound(s) of the range R with a
6104 -- Constraint_Error node. When the itype which uses these bounds
6105 -- is frozen the resulting call to Duplicate_Subexpr generates
6106 -- a new temporary for the bounds.
6108 -- Unfortunately there are other itypes that are also made depend
6109 -- on these bounds, so when Duplicate_Subexpr is called they get
6110 -- a forward reference to the newly created temporaries and Gigi
6111 -- aborts on such forward references. This is probably sign of a
6112 -- more fundamental problem somewhere else in either the order of
6113 -- itype freezing or the way certain itypes are constructed.
6115 -- To get around this problem we call Remove_Side_Effects right
6116 -- away if either bounds of R are a Constraint_Error.
6119 L : constant Node_Id := Low_Bound (R);
6120 H : constant Node_Id := High_Bound (R);
6123 if Nkind (L) = N_Raise_Constraint_Error then
6124 Remove_Side_Effects (L);
6127 if Nkind (H) = N_Raise_Constraint_Error then
6128 Remove_Side_Effects (H);
6132 Check_Unset_Reference (Low_Bound (R));
6133 Check_Unset_Reference (High_Bound (R));
6136 end Resolve_Discrete_Subtype_Indication;
6138 -------------------------
6139 -- Resolve_Entity_Name --
6140 -------------------------
6142 -- Used to resolve identifiers and expanded names
6144 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6145 E : constant Entity_Id := Entity (N);
6148 -- If garbage from errors, set to Any_Type and return
6150 if No (E) and then Total_Errors_Detected /= 0 then
6151 Set_Etype (N, Any_Type);
6155 -- Replace named numbers by corresponding literals. Note that this is
6156 -- the one case where Resolve_Entity_Name must reset the Etype, since
6157 -- it is currently marked as universal.
6159 if Ekind (E) = E_Named_Integer then
6161 Eval_Named_Integer (N);
6163 elsif Ekind (E) = E_Named_Real then
6165 Eval_Named_Real (N);
6167 -- For enumeration literals, we need to make sure that a proper style
6168 -- check is done, since such literals are overloaded, and thus we did
6169 -- not do a style check during the first phase of analysis.
6171 elsif Ekind (E) = E_Enumeration_Literal then
6172 Set_Entity_With_Style_Check (N, E);
6173 Eval_Entity_Name (N);
6175 -- Case of subtype name appearing as an operand in expression
6177 elsif Is_Type (E) then
6179 -- Allow use of subtype if it is a concurrent type where we are
6180 -- currently inside the body. This will eventually be expanded into a
6181 -- call to Self (for tasks) or _object (for protected objects). Any
6182 -- other use of a subtype is invalid.
6184 if Is_Concurrent_Type (E)
6185 and then In_Open_Scopes (E)
6189 -- Any other use is an error
6193 ("invalid use of subtype mark in expression or call", N);
6196 -- Check discriminant use if entity is discriminant in current scope,
6197 -- i.e. discriminant of record or concurrent type currently being
6198 -- analyzed. Uses in corresponding body are unrestricted.
6200 elsif Ekind (E) = E_Discriminant
6201 and then Scope (E) = Current_Scope
6202 and then not Has_Completion (Current_Scope)
6204 Check_Discriminant_Use (N);
6206 -- A parameterless generic function cannot appear in a context that
6207 -- requires resolution.
6209 elsif Ekind (E) = E_Generic_Function then
6210 Error_Msg_N ("illegal use of generic function", N);
6212 elsif Ekind (E) = E_Out_Parameter
6213 and then Ada_Version = Ada_83
6214 and then (Nkind (Parent (N)) in N_Op
6215 or else (Nkind (Parent (N)) = N_Assignment_Statement
6216 and then N = Expression (Parent (N)))
6217 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6219 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6221 -- In all other cases, just do the possible static evaluation
6224 -- A deferred constant that appears in an expression must have a
6225 -- completion, unless it has been removed by in-place expansion of
6228 if Ekind (E) = E_Constant
6229 and then Comes_From_Source (E)
6230 and then No (Constant_Value (E))
6231 and then Is_Frozen (Etype (E))
6232 and then not In_Spec_Expression
6233 and then not Is_Imported (E)
6235 if No_Initialization (Parent (E))
6236 or else (Present (Full_View (E))
6237 and then No_Initialization (Parent (Full_View (E))))
6242 "deferred constant is frozen before completion", N);
6246 Eval_Entity_Name (N);
6248 end Resolve_Entity_Name;
6254 procedure Resolve_Entry (Entry_Name : Node_Id) is
6255 Loc : constant Source_Ptr := Sloc (Entry_Name);
6263 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6264 -- If the bounds of the entry family being called depend on task
6265 -- discriminants, build a new index subtype where a discriminant is
6266 -- replaced with the value of the discriminant of the target task.
6267 -- The target task is the prefix of the entry name in the call.
6269 -----------------------
6270 -- Actual_Index_Type --
6271 -----------------------
6273 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6274 Typ : constant Entity_Id := Entry_Index_Type (E);
6275 Tsk : constant Entity_Id := Scope (E);
6276 Lo : constant Node_Id := Type_Low_Bound (Typ);
6277 Hi : constant Node_Id := Type_High_Bound (Typ);
6280 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6281 -- If the bound is given by a discriminant, replace with a reference
6282 -- to the discriminant of the same name in the target task. If the
6283 -- entry name is the target of a requeue statement and the entry is
6284 -- in the current protected object, the bound to be used is the
6285 -- discriminal of the object (see Apply_Range_Checks for details of
6286 -- the transformation).
6288 -----------------------------
6289 -- Actual_Discriminant_Ref --
6290 -----------------------------
6292 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6293 Typ : constant Entity_Id := Etype (Bound);
6297 Remove_Side_Effects (Bound);
6299 if not Is_Entity_Name (Bound)
6300 or else Ekind (Entity (Bound)) /= E_Discriminant
6304 elsif Is_Protected_Type (Tsk)
6305 and then In_Open_Scopes (Tsk)
6306 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6308 -- Note: here Bound denotes a discriminant of the corresponding
6309 -- record type tskV, whose discriminal is a formal of the
6310 -- init-proc tskVIP. What we want is the body discriminal,
6311 -- which is associated to the discriminant of the original
6312 -- concurrent type tsk.
6314 return New_Occurrence_Of
6315 (Find_Body_Discriminal (Entity (Bound)), Loc);
6319 Make_Selected_Component (Loc,
6320 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6321 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6326 end Actual_Discriminant_Ref;
6328 -- Start of processing for Actual_Index_Type
6331 if not Has_Discriminants (Tsk)
6332 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6334 return Entry_Index_Type (E);
6337 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6338 Set_Etype (New_T, Base_Type (Typ));
6339 Set_Size_Info (New_T, Typ);
6340 Set_RM_Size (New_T, RM_Size (Typ));
6341 Set_Scalar_Range (New_T,
6342 Make_Range (Sloc (Entry_Name),
6343 Low_Bound => Actual_Discriminant_Ref (Lo),
6344 High_Bound => Actual_Discriminant_Ref (Hi)));
6348 end Actual_Index_Type;
6350 -- Start of processing of Resolve_Entry
6353 -- Find name of entry being called, and resolve prefix of name with its
6354 -- own type. The prefix can be overloaded, and the name and signature of
6355 -- the entry must be taken into account.
6357 if Nkind (Entry_Name) = N_Indexed_Component then
6359 -- Case of dealing with entry family within the current tasks
6361 E_Name := Prefix (Entry_Name);
6364 E_Name := Entry_Name;
6367 if Is_Entity_Name (E_Name) then
6369 -- Entry call to an entry (or entry family) in the current task. This
6370 -- is legal even though the task will deadlock. Rewrite as call to
6373 -- This can also be a call to an entry in an enclosing task. If this
6374 -- is a single task, we have to retrieve its name, because the scope
6375 -- of the entry is the task type, not the object. If the enclosing
6376 -- task is a task type, the identity of the task is given by its own
6379 -- Finally this can be a requeue on an entry of the same task or
6380 -- protected object.
6382 S := Scope (Entity (E_Name));
6384 for J in reverse 0 .. Scope_Stack.Last loop
6385 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6386 and then not Comes_From_Source (S)
6388 -- S is an enclosing task or protected object. The concurrent
6389 -- declaration has been converted into a type declaration, and
6390 -- the object itself has an object declaration that follows
6391 -- the type in the same declarative part.
6393 Tsk := Next_Entity (S);
6394 while Etype (Tsk) /= S loop
6401 elsif S = Scope_Stack.Table (J).Entity then
6403 -- Call to current task. Will be transformed into call to Self
6411 Make_Selected_Component (Loc,
6412 Prefix => New_Occurrence_Of (S, Loc),
6414 New_Occurrence_Of (Entity (E_Name), Loc));
6415 Rewrite (E_Name, New_N);
6418 elsif Nkind (Entry_Name) = N_Selected_Component
6419 and then Is_Overloaded (Prefix (Entry_Name))
6421 -- Use the entry name (which must be unique at this point) to find
6422 -- the prefix that returns the corresponding task/protected type.
6425 Pref : constant Node_Id := Prefix (Entry_Name);
6426 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6431 Get_First_Interp (Pref, I, It);
6432 while Present (It.Typ) loop
6433 if Scope (Ent) = It.Typ then
6434 Set_Etype (Pref, It.Typ);
6438 Get_Next_Interp (I, It);
6443 if Nkind (Entry_Name) = N_Selected_Component then
6444 Resolve (Prefix (Entry_Name));
6446 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6447 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6448 Resolve (Prefix (Prefix (Entry_Name)));
6449 Index := First (Expressions (Entry_Name));
6450 Resolve (Index, Entry_Index_Type (Nam));
6452 -- Up to this point the expression could have been the actual in a
6453 -- simple entry call, and be given by a named association.
6455 if Nkind (Index) = N_Parameter_Association then
6456 Error_Msg_N ("expect expression for entry index", Index);
6458 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6463 ------------------------
6464 -- Resolve_Entry_Call --
6465 ------------------------
6467 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6468 Entry_Name : constant Node_Id := Name (N);
6469 Loc : constant Source_Ptr := Sloc (Entry_Name);
6471 First_Named : Node_Id;
6478 -- We kill all checks here, because it does not seem worth the effort to
6479 -- do anything better, an entry call is a big operation.
6483 -- Processing of the name is similar for entry calls and protected
6484 -- operation calls. Once the entity is determined, we can complete
6485 -- the resolution of the actuals.
6487 -- The selector may be overloaded, in the case of a protected object
6488 -- with overloaded functions. The type of the context is used for
6491 if Nkind (Entry_Name) = N_Selected_Component
6492 and then Is_Overloaded (Selector_Name (Entry_Name))
6493 and then Typ /= Standard_Void_Type
6500 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6501 while Present (It.Typ) loop
6502 if Covers (Typ, It.Typ) then
6503 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6504 Set_Etype (Entry_Name, It.Typ);
6506 Generate_Reference (It.Typ, N, ' ');
6509 Get_Next_Interp (I, It);
6514 Resolve_Entry (Entry_Name);
6516 if Nkind (Entry_Name) = N_Selected_Component then
6518 -- Simple entry call
6520 Nam := Entity (Selector_Name (Entry_Name));
6521 Obj := Prefix (Entry_Name);
6522 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6524 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6526 -- Call to member of entry family
6528 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6529 Obj := Prefix (Prefix (Entry_Name));
6530 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6533 -- We cannot in general check the maximum depth of protected entry calls
6534 -- at compile time. But we can tell that any protected entry call at all
6535 -- violates a specified nesting depth of zero.
6537 if Is_Protected_Type (Scope (Nam)) then
6538 Check_Restriction (Max_Entry_Queue_Length, N);
6541 -- Use context type to disambiguate a protected function that can be
6542 -- called without actuals and that returns an array type, and where the
6543 -- argument list may be an indexing of the returned value.
6545 if Ekind (Nam) = E_Function
6546 and then Needs_No_Actuals (Nam)
6547 and then Present (Parameter_Associations (N))
6549 ((Is_Array_Type (Etype (Nam))
6550 and then Covers (Typ, Component_Type (Etype (Nam))))
6552 or else (Is_Access_Type (Etype (Nam))
6553 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6557 Component_Type (Designated_Type (Etype (Nam))))))
6560 Index_Node : Node_Id;
6564 Make_Indexed_Component (Loc,
6566 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6567 Expressions => Parameter_Associations (N));
6569 -- Since we are correcting a node classification error made by the
6570 -- parser, we call Replace rather than Rewrite.
6572 Replace (N, Index_Node);
6573 Set_Etype (Prefix (N), Etype (Nam));
6575 Resolve_Indexed_Component (N, Typ);
6580 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6581 and then Present (PPC_Wrapper (Nam))
6582 and then Current_Scope /= PPC_Wrapper (Nam)
6584 -- Rewrite as call to the precondition wrapper, adding the task
6585 -- object to the list of actuals. If the call is to a member of an
6586 -- entry family, include the index as well.
6590 New_Actuals : List_Id;
6593 New_Actuals := New_List (Obj);
6595 if Nkind (Entry_Name) = N_Indexed_Component then
6596 Append_To (New_Actuals,
6597 New_Copy_Tree (First (Expressions (Entry_Name))));
6600 Append_List (Parameter_Associations (N), New_Actuals);
6602 Make_Procedure_Call_Statement (Loc,
6604 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6605 Parameter_Associations => New_Actuals);
6606 Rewrite (N, New_Call);
6607 Analyze_And_Resolve (N);
6612 -- The operation name may have been overloaded. Order the actuals
6613 -- according to the formals of the resolved entity, and set the return
6614 -- type to that of the operation.
6617 Normalize_Actuals (N, Nam, False, Norm_OK);
6618 pragma Assert (Norm_OK);
6619 Set_Etype (N, Etype (Nam));
6622 Resolve_Actuals (N, Nam);
6624 -- Create a call reference to the entry
6626 Generate_Reference (Nam, Entry_Name, 's');
6628 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6629 Check_Potentially_Blocking_Operation (N);
6632 -- Verify that a procedure call cannot masquerade as an entry
6633 -- call where an entry call is expected.
6635 if Ekind (Nam) = E_Procedure then
6636 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6637 and then N = Entry_Call_Statement (Parent (N))
6639 Error_Msg_N ("entry call required in select statement", N);
6641 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6642 and then N = Triggering_Statement (Parent (N))
6644 Error_Msg_N ("triggering statement cannot be procedure call", N);
6646 elsif Ekind (Scope (Nam)) = E_Task_Type
6647 and then not In_Open_Scopes (Scope (Nam))
6649 Error_Msg_N ("task has no entry with this name", Entry_Name);
6653 -- After resolution, entry calls and protected procedure calls are
6654 -- changed into entry calls, for expansion. The structure of the node
6655 -- does not change, so it can safely be done in place. Protected
6656 -- function calls must keep their structure because they are
6659 if Ekind (Nam) /= E_Function then
6661 -- A protected operation that is not a function may modify the
6662 -- corresponding object, and cannot apply to a constant. If this
6663 -- is an internal call, the prefix is the type itself.
6665 if Is_Protected_Type (Scope (Nam))
6666 and then not Is_Variable (Obj)
6667 and then (not Is_Entity_Name (Obj)
6668 or else not Is_Type (Entity (Obj)))
6671 ("prefix of protected procedure or entry call must be variable",
6675 Actuals := Parameter_Associations (N);
6676 First_Named := First_Named_Actual (N);
6679 Make_Entry_Call_Statement (Loc,
6681 Parameter_Associations => Actuals));
6683 Set_First_Named_Actual (N, First_Named);
6684 Set_Analyzed (N, True);
6686 -- Protected functions can return on the secondary stack, in which
6687 -- case we must trigger the transient scope mechanism.
6689 elsif Full_Expander_Active
6690 and then Requires_Transient_Scope (Etype (Nam))
6692 Establish_Transient_Scope (N, Sec_Stack => True);
6694 end Resolve_Entry_Call;
6696 -------------------------
6697 -- Resolve_Equality_Op --
6698 -------------------------
6700 -- Both arguments must have the same type, and the boolean context does
6701 -- not participate in the resolution. The first pass verifies that the
6702 -- interpretation is not ambiguous, and the type of the left argument is
6703 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6704 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6705 -- though they carry a single (universal) type. Diagnose this case here.
6707 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6708 L : constant Node_Id := Left_Opnd (N);
6709 R : constant Node_Id := Right_Opnd (N);
6710 T : Entity_Id := Find_Unique_Type (L, R);
6712 procedure Check_Conditional_Expression (Cond : Node_Id);
6713 -- The resolution rule for conditional expressions requires that each
6714 -- such must have a unique type. This means that if several dependent
6715 -- expressions are of a non-null anonymous access type, and the context
6716 -- does not impose an expected type (as can be the case in an equality
6717 -- operation) the expression must be rejected.
6719 function Find_Unique_Access_Type return Entity_Id;
6720 -- In the case of allocators, make a last-ditch attempt to find a single
6721 -- access type with the right designated type. This is semantically
6722 -- dubious, and of no interest to any real code, but c48008a makes it
6725 ----------------------------------
6726 -- Check_Conditional_Expression --
6727 ----------------------------------
6729 procedure Check_Conditional_Expression (Cond : Node_Id) is
6730 Then_Expr : Node_Id;
6731 Else_Expr : Node_Id;
6734 if Nkind (Cond) = N_Conditional_Expression then
6735 Then_Expr := Next (First (Expressions (Cond)));
6736 Else_Expr := Next (Then_Expr);
6738 if Nkind (Then_Expr) /= N_Null
6739 and then Nkind (Else_Expr) /= N_Null
6742 ("cannot determine type of conditional expression", Cond);
6745 end Check_Conditional_Expression;
6747 -----------------------------
6748 -- Find_Unique_Access_Type --
6749 -----------------------------
6751 function Find_Unique_Access_Type return Entity_Id is
6757 if Ekind (Etype (R)) = E_Allocator_Type then
6758 Acc := Designated_Type (Etype (R));
6759 elsif Ekind (Etype (L)) = E_Allocator_Type then
6760 Acc := Designated_Type (Etype (L));
6766 while S /= Standard_Standard loop
6767 E := First_Entity (S);
6768 while Present (E) loop
6770 and then Is_Access_Type (E)
6771 and then Ekind (E) /= E_Allocator_Type
6772 and then Designated_Type (E) = Base_Type (Acc)
6784 end Find_Unique_Access_Type;
6786 -- Start of processing for Resolve_Equality_Op
6789 Set_Etype (N, Base_Type (Typ));
6790 Generate_Reference (T, N, ' ');
6792 if T = Any_Fixed then
6793 T := Unique_Fixed_Point_Type (L);
6796 if T /= Any_Type then
6797 if T = Any_String or else
6798 T = Any_Composite or else
6801 if T = Any_Character then
6802 Ambiguous_Character (L);
6804 Error_Msg_N ("ambiguous operands for equality", N);
6807 Set_Etype (N, Any_Type);
6810 elsif T = Any_Access
6811 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6813 T := Find_Unique_Access_Type;
6816 Error_Msg_N ("ambiguous operands for equality", N);
6817 Set_Etype (N, Any_Type);
6821 -- Conditional expressions must have a single type, and if the
6822 -- context does not impose one the dependent expressions cannot
6823 -- be anonymous access types.
6825 elsif Ada_Version >= Ada_2012
6826 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6827 E_Anonymous_Access_Subprogram_Type)
6828 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6829 E_Anonymous_Access_Subprogram_Type)
6831 Check_Conditional_Expression (L);
6832 Check_Conditional_Expression (R);
6838 -- In SPARK, equality operators = and /= for array types other than
6839 -- String are only defined when, for each index position, the
6840 -- operands have equal static bounds.
6842 if Is_Array_Type (T) then
6843 -- Protect call to Matching_Static_Array_Bounds to avoid costly
6844 -- operation if not needed.
6846 if Restriction_Check_Required (SPARK)
6847 and then Base_Type (T) /= Standard_String
6848 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6849 and then Etype (L) /= Any_Composite -- or else L in error
6850 and then Etype (R) /= Any_Composite -- or else R in error
6851 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6853 Check_SPARK_Restriction
6854 ("array types should have matching static bounds", N);
6858 -- If the unique type is a class-wide type then it will be expanded
6859 -- into a dispatching call to the predefined primitive. Therefore we
6860 -- check here for potential violation of such restriction.
6862 if Is_Class_Wide_Type (T) then
6863 Check_Restriction (No_Dispatching_Calls, N);
6866 if Warn_On_Redundant_Constructs
6867 and then Comes_From_Source (N)
6868 and then Is_Entity_Name (R)
6869 and then Entity (R) = Standard_True
6870 and then Comes_From_Source (R)
6872 Error_Msg_N -- CODEFIX
6873 ("?comparison with True is redundant!", R);
6876 Check_Unset_Reference (L);
6877 Check_Unset_Reference (R);
6878 Generate_Operator_Reference (N, T);
6879 Check_Low_Bound_Tested (N);
6881 -- If this is an inequality, it may be the implicit inequality
6882 -- created for a user-defined operation, in which case the corres-
6883 -- ponding equality operation is not intrinsic, and the operation
6884 -- cannot be constant-folded. Else fold.
6886 if Nkind (N) = N_Op_Eq
6887 or else Comes_From_Source (Entity (N))
6888 or else Ekind (Entity (N)) = E_Operator
6889 or else Is_Intrinsic_Subprogram
6890 (Corresponding_Equality (Entity (N)))
6892 Eval_Relational_Op (N);
6894 elsif Nkind (N) = N_Op_Ne
6895 and then Is_Abstract_Subprogram (Entity (N))
6897 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6900 -- Ada 2005: If one operand is an anonymous access type, convert the
6901 -- other operand to it, to ensure that the underlying types match in
6902 -- the back-end. Same for access_to_subprogram, and the conversion
6903 -- verifies that the types are subtype conformant.
6905 -- We apply the same conversion in the case one of the operands is a
6906 -- private subtype of the type of the other.
6908 -- Why the Expander_Active test here ???
6910 if Full_Expander_Active
6912 (Ekind_In (T, E_Anonymous_Access_Type,
6913 E_Anonymous_Access_Subprogram_Type)
6914 or else Is_Private_Type (T))
6916 if Etype (L) /= T then
6918 Make_Unchecked_Type_Conversion (Sloc (L),
6919 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6920 Expression => Relocate_Node (L)));
6921 Analyze_And_Resolve (L, T);
6924 if (Etype (R)) /= T then
6926 Make_Unchecked_Type_Conversion (Sloc (R),
6927 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6928 Expression => Relocate_Node (R)));
6929 Analyze_And_Resolve (R, T);
6933 end Resolve_Equality_Op;
6935 ----------------------------------
6936 -- Resolve_Explicit_Dereference --
6937 ----------------------------------
6939 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6940 Loc : constant Source_Ptr := Sloc (N);
6942 P : constant Node_Id := Prefix (N);
6947 Check_Fully_Declared_Prefix (Typ, P);
6949 if Is_Overloaded (P) then
6951 -- Use the context type to select the prefix that has the correct
6954 Get_First_Interp (P, I, It);
6955 while Present (It.Typ) loop
6956 exit when Is_Access_Type (It.Typ)
6957 and then Covers (Typ, Designated_Type (It.Typ));
6958 Get_Next_Interp (I, It);
6961 if Present (It.Typ) then
6962 Resolve (P, It.Typ);
6964 -- If no interpretation covers the designated type of the prefix,
6965 -- this is the pathological case where not all implementations of
6966 -- the prefix allow the interpretation of the node as a call. Now
6967 -- that the expected type is known, Remove other interpretations
6968 -- from prefix, rewrite it as a call, and resolve again, so that
6969 -- the proper call node is generated.
6971 Get_First_Interp (P, I, It);
6972 while Present (It.Typ) loop
6973 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6977 Get_Next_Interp (I, It);
6981 Make_Function_Call (Loc,
6983 Make_Explicit_Dereference (Loc,
6985 Parameter_Associations => New_List);
6987 Save_Interps (N, New_N);
6989 Analyze_And_Resolve (N, Typ);
6993 Set_Etype (N, Designated_Type (It.Typ));
6999 if Is_Access_Type (Etype (P)) then
7000 Apply_Access_Check (N);
7003 -- If the designated type is a packed unconstrained array type, and the
7004 -- explicit dereference is not in the context of an attribute reference,
7005 -- then we must compute and set the actual subtype, since it is needed
7006 -- by Gigi. The reason we exclude the attribute case is that this is
7007 -- handled fine by Gigi, and in fact we use such attributes to build the
7008 -- actual subtype. We also exclude generated code (which builds actual
7009 -- subtypes directly if they are needed).
7011 if Is_Array_Type (Etype (N))
7012 and then Is_Packed (Etype (N))
7013 and then not Is_Constrained (Etype (N))
7014 and then Nkind (Parent (N)) /= N_Attribute_Reference
7015 and then Comes_From_Source (N)
7017 Set_Etype (N, Get_Actual_Subtype (N));
7020 -- Note: No Eval processing is required for an explicit dereference,
7021 -- because such a name can never be static.
7023 end Resolve_Explicit_Dereference;
7025 -------------------------------------
7026 -- Resolve_Expression_With_Actions --
7027 -------------------------------------
7029 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7032 end Resolve_Expression_With_Actions;
7034 -------------------------------
7035 -- Resolve_Indexed_Component --
7036 -------------------------------
7038 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7039 Name : constant Node_Id := Prefix (N);
7041 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7045 if Is_Overloaded (Name) then
7047 -- Use the context type to select the prefix that yields the correct
7053 I1 : Interp_Index := 0;
7054 P : constant Node_Id := Prefix (N);
7055 Found : Boolean := False;
7058 Get_First_Interp (P, I, It);
7059 while Present (It.Typ) loop
7060 if (Is_Array_Type (It.Typ)
7061 and then Covers (Typ, Component_Type (It.Typ)))
7062 or else (Is_Access_Type (It.Typ)
7063 and then Is_Array_Type (Designated_Type (It.Typ))
7067 Component_Type (Designated_Type (It.Typ))))
7070 It := Disambiguate (P, I1, I, Any_Type);
7072 if It = No_Interp then
7073 Error_Msg_N ("ambiguous prefix for indexing", N);
7079 Array_Type := It.Typ;
7085 Array_Type := It.Typ;
7090 Get_Next_Interp (I, It);
7095 Array_Type := Etype (Name);
7098 Resolve (Name, Array_Type);
7099 Array_Type := Get_Actual_Subtype_If_Available (Name);
7101 -- If prefix is access type, dereference to get real array type.
7102 -- Note: we do not apply an access check because the expander always
7103 -- introduces an explicit dereference, and the check will happen there.
7105 if Is_Access_Type (Array_Type) then
7106 Array_Type := Designated_Type (Array_Type);
7109 -- If name was overloaded, set component type correctly now
7110 -- If a misplaced call to an entry family (which has no index types)
7111 -- return. Error will be diagnosed from calling context.
7113 if Is_Array_Type (Array_Type) then
7114 Set_Etype (N, Component_Type (Array_Type));
7119 Index := First_Index (Array_Type);
7120 Expr := First (Expressions (N));
7122 -- The prefix may have resolved to a string literal, in which case its
7123 -- etype has a special representation. This is only possible currently
7124 -- if the prefix is a static concatenation, written in functional
7127 if Ekind (Array_Type) = E_String_Literal_Subtype then
7128 Resolve (Expr, Standard_Positive);
7131 while Present (Index) and Present (Expr) loop
7132 Resolve (Expr, Etype (Index));
7133 Check_Unset_Reference (Expr);
7135 if Is_Scalar_Type (Etype (Expr)) then
7136 Apply_Scalar_Range_Check (Expr, Etype (Index));
7138 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7146 -- Do not generate the warning on suspicious index if we are analyzing
7147 -- package Ada.Tags; otherwise we will report the warning with the
7148 -- Prims_Ptr field of the dispatch table.
7150 if Scope (Etype (Prefix (N))) = Standard_Standard
7152 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7155 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7156 Eval_Indexed_Component (N);
7159 -- If the array type is atomic, and is packed, and we are in a left side
7160 -- context, then this is worth a warning, since we have a situation
7161 -- where the access to the component may cause extra read/writes of
7162 -- the atomic array object, which could be considered unexpected.
7164 if Nkind (N) = N_Indexed_Component
7165 and then (Is_Atomic (Array_Type)
7166 or else (Is_Entity_Name (Prefix (N))
7167 and then Is_Atomic (Entity (Prefix (N)))))
7168 and then Is_Bit_Packed_Array (Array_Type)
7171 Error_Msg_N ("?assignment to component of packed atomic array",
7173 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7176 end Resolve_Indexed_Component;
7178 -----------------------------
7179 -- Resolve_Integer_Literal --
7180 -----------------------------
7182 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7185 Eval_Integer_Literal (N);
7186 end Resolve_Integer_Literal;
7188 --------------------------------
7189 -- Resolve_Intrinsic_Operator --
7190 --------------------------------
7192 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7193 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7195 Orig_Op : constant Entity_Id := Entity (N);
7199 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7200 -- If the operand is a literal, it cannot be the expression in a
7201 -- conversion. Use a qualified expression instead.
7203 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7204 Loc : constant Source_Ptr := Sloc (Opnd);
7207 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7209 Make_Qualified_Expression (Loc,
7210 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7211 Expression => Relocate_Node (Opnd));
7215 Res := Unchecked_Convert_To (Btyp, Opnd);
7219 end Convert_Operand;
7221 -- Start of processing for Resolve_Intrinsic_Operator
7224 -- We must preserve the original entity in a generic setting, so that
7225 -- the legality of the operation can be verified in an instance.
7227 if not Full_Expander_Active then
7232 while Scope (Op) /= Standard_Standard loop
7234 pragma Assert (Present (Op));
7238 Set_Is_Overloaded (N, False);
7240 -- If the result or operand types are private, rewrite with unchecked
7241 -- conversions on the operands and the result, to expose the proper
7242 -- underlying numeric type.
7244 if Is_Private_Type (Typ)
7245 or else Is_Private_Type (Etype (Left_Opnd (N)))
7246 or else Is_Private_Type (Etype (Right_Opnd (N)))
7248 Arg1 := Convert_Operand (Left_Opnd (N));
7249 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7250 -- What on earth is this commented out fragment of code???
7252 if Nkind (N) = N_Op_Expon then
7253 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7255 Arg2 := Convert_Operand (Right_Opnd (N));
7258 if Nkind (Arg1) = N_Type_Conversion then
7259 Save_Interps (Left_Opnd (N), Expression (Arg1));
7262 if Nkind (Arg2) = N_Type_Conversion then
7263 Save_Interps (Right_Opnd (N), Expression (Arg2));
7266 Set_Left_Opnd (N, Arg1);
7267 Set_Right_Opnd (N, Arg2);
7269 Set_Etype (N, Btyp);
7270 Rewrite (N, Unchecked_Convert_To (Typ, N));
7273 elsif Typ /= Etype (Left_Opnd (N))
7274 or else Typ /= Etype (Right_Opnd (N))
7276 -- Add explicit conversion where needed, and save interpretations in
7277 -- case operands are overloaded. If the context is a VMS operation,
7278 -- assert that the conversion is legal (the operands have the proper
7279 -- types to select the VMS intrinsic). Note that in rare cases the
7280 -- VMS operators may be visible, but the default System is being used
7281 -- and Address is a private type.
7283 Arg1 := Convert_To (Typ, Left_Opnd (N));
7284 Arg2 := Convert_To (Typ, Right_Opnd (N));
7286 if Nkind (Arg1) = N_Type_Conversion then
7287 Save_Interps (Left_Opnd (N), Expression (Arg1));
7289 if Is_VMS_Operator (Orig_Op) then
7290 Set_Conversion_OK (Arg1);
7293 Save_Interps (Left_Opnd (N), Arg1);
7296 if Nkind (Arg2) = N_Type_Conversion then
7297 Save_Interps (Right_Opnd (N), Expression (Arg2));
7299 if Is_VMS_Operator (Orig_Op) then
7300 Set_Conversion_OK (Arg2);
7303 Save_Interps (Right_Opnd (N), Arg2);
7306 Rewrite (Left_Opnd (N), Arg1);
7307 Rewrite (Right_Opnd (N), Arg2);
7310 Resolve_Arithmetic_Op (N, Typ);
7313 Resolve_Arithmetic_Op (N, Typ);
7315 end Resolve_Intrinsic_Operator;
7317 --------------------------------------
7318 -- Resolve_Intrinsic_Unary_Operator --
7319 --------------------------------------
7321 procedure Resolve_Intrinsic_Unary_Operator
7325 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7331 while Scope (Op) /= Standard_Standard loop
7333 pragma Assert (Present (Op));
7338 if Is_Private_Type (Typ) then
7339 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7340 Save_Interps (Right_Opnd (N), Expression (Arg2));
7342 Set_Right_Opnd (N, Arg2);
7344 Set_Etype (N, Btyp);
7345 Rewrite (N, Unchecked_Convert_To (Typ, N));
7349 Resolve_Unary_Op (N, Typ);
7351 end Resolve_Intrinsic_Unary_Operator;
7353 ------------------------
7354 -- Resolve_Logical_Op --
7355 ------------------------
7357 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7361 Check_No_Direct_Boolean_Operators (N);
7363 -- Predefined operations on scalar types yield the base type. On the
7364 -- other hand, logical operations on arrays yield the type of the
7365 -- arguments (and the context).
7367 if Is_Array_Type (Typ) then
7370 B_Typ := Base_Type (Typ);
7373 -- OK if this is a VMS-specific intrinsic operation
7375 if Is_VMS_Operator (Entity (N)) then
7378 -- The following test is required because the operands of the operation
7379 -- may be literals, in which case the resulting type appears to be
7380 -- compatible with a signed integer type, when in fact it is compatible
7381 -- only with modular types. If the context itself is universal, the
7382 -- operation is illegal.
7384 elsif not Valid_Boolean_Arg (Typ) then
7385 Error_Msg_N ("invalid context for logical operation", N);
7386 Set_Etype (N, Any_Type);
7389 elsif Typ = Any_Modular then
7391 ("no modular type available in this context", N);
7392 Set_Etype (N, Any_Type);
7395 elsif Is_Modular_Integer_Type (Typ)
7396 and then Etype (Left_Opnd (N)) = Universal_Integer
7397 and then Etype (Right_Opnd (N)) = Universal_Integer
7399 Check_For_Visible_Operator (N, B_Typ);
7402 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
7403 -- is active and the result type is standard Boolean (do not mess with
7404 -- ops that return a nonstandard Boolean type, because something strange
7407 -- Note: you might expect this replacement to be done during expansion,
7408 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
7409 -- is used, no part of the right operand of an "and" or "or" operator
7410 -- should be executed if the left operand would short-circuit the
7411 -- evaluation of the corresponding "and then" or "or else". If we left
7412 -- the replacement to expansion time, then run-time checks associated
7413 -- with such operands would be evaluated unconditionally, due to being
7414 -- before the condition prior to the rewriting as short-circuit forms
7415 -- during expansion.
7417 if Short_Circuit_And_Or
7418 and then B_Typ = Standard_Boolean
7419 and then Nkind_In (N, N_Op_And, N_Op_Or)
7421 if Nkind (N) = N_Op_And then
7423 Make_And_Then (Sloc (N),
7424 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7425 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7426 Analyze_And_Resolve (N, B_Typ);
7428 -- Case of OR changed to OR ELSE
7432 Make_Or_Else (Sloc (N),
7433 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7434 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7435 Analyze_And_Resolve (N, B_Typ);
7438 -- Return now, since analysis of the rewritten ops will take care of
7439 -- other reference bookkeeping and expression folding.
7444 Resolve (Left_Opnd (N), B_Typ);
7445 Resolve (Right_Opnd (N), B_Typ);
7447 Check_Unset_Reference (Left_Opnd (N));
7448 Check_Unset_Reference (Right_Opnd (N));
7450 Set_Etype (N, B_Typ);
7451 Generate_Operator_Reference (N, B_Typ);
7452 Eval_Logical_Op (N);
7454 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7455 -- only when both operands have same static lower and higher bounds. Of
7456 -- course the types have to match, so only check if operands are
7457 -- compatible and the node itself has no errors.
7459 if Is_Array_Type (B_Typ)
7460 and then Nkind (N) in N_Binary_Op
7463 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7464 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7467 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7468 -- operation if not needed.
7470 if Restriction_Check_Required (SPARK)
7471 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7472 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7473 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7474 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7476 Check_SPARK_Restriction
7477 ("array types should have matching static bounds", N);
7481 end Resolve_Logical_Op;
7483 ---------------------------
7484 -- Resolve_Membership_Op --
7485 ---------------------------
7487 -- The context can only be a boolean type, and does not determine the
7488 -- arguments. Arguments should be unambiguous, but the preference rule for
7489 -- universal types applies.
7491 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7492 pragma Warnings (Off, Typ);
7494 L : constant Node_Id := Left_Opnd (N);
7495 R : constant Node_Id := Right_Opnd (N);
7498 procedure Resolve_Set_Membership;
7499 -- Analysis has determined a unique type for the left operand. Use it to
7500 -- resolve the disjuncts.
7502 ----------------------------
7503 -- Resolve_Set_Membership --
7504 ----------------------------
7506 procedure Resolve_Set_Membership is
7510 Resolve (L, Etype (L));
7512 Alt := First (Alternatives (N));
7513 while Present (Alt) loop
7515 -- Alternative is an expression, a range
7516 -- or a subtype mark.
7518 if not Is_Entity_Name (Alt)
7519 or else not Is_Type (Entity (Alt))
7521 Resolve (Alt, Etype (L));
7526 end Resolve_Set_Membership;
7528 -- Start of processing for Resolve_Membership_Op
7531 if L = Error or else R = Error then
7535 if Present (Alternatives (N)) then
7536 Resolve_Set_Membership;
7539 elsif not Is_Overloaded (R)
7541 (Etype (R) = Universal_Integer
7543 Etype (R) = Universal_Real)
7544 and then Is_Overloaded (L)
7548 -- Ada 2005 (AI-251): Support the following case:
7550 -- type I is interface;
7551 -- type T is tagged ...
7553 -- function Test (O : I'Class) is
7555 -- return O in T'Class.
7558 -- In this case we have nothing else to do. The membership test will be
7559 -- done at run time.
7561 elsif Ada_Version >= Ada_2005
7562 and then Is_Class_Wide_Type (Etype (L))
7563 and then Is_Interface (Etype (L))
7564 and then Is_Class_Wide_Type (Etype (R))
7565 and then not Is_Interface (Etype (R))
7569 T := Intersect_Types (L, R);
7572 -- If mixed-mode operations are present and operands are all literal,
7573 -- the only interpretation involves Duration, which is probably not
7574 -- the intention of the programmer.
7576 if T = Any_Fixed then
7577 T := Unique_Fixed_Point_Type (N);
7579 if T = Any_Type then
7585 Check_Unset_Reference (L);
7587 if Nkind (R) = N_Range
7588 and then not Is_Scalar_Type (T)
7590 Error_Msg_N ("scalar type required for range", R);
7593 if Is_Entity_Name (R) then
7594 Freeze_Expression (R);
7597 Check_Unset_Reference (R);
7600 Eval_Membership_Op (N);
7601 end Resolve_Membership_Op;
7607 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7608 Loc : constant Source_Ptr := Sloc (N);
7611 -- Handle restriction against anonymous null access values This
7612 -- restriction can be turned off using -gnatdj.
7614 -- Ada 2005 (AI-231): Remove restriction
7616 if Ada_Version < Ada_2005
7617 and then not Debug_Flag_J
7618 and then Ekind (Typ) = E_Anonymous_Access_Type
7619 and then Comes_From_Source (N)
7621 -- In the common case of a call which uses an explicitly null value
7622 -- for an access parameter, give specialized error message.
7624 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7628 ("null is not allowed as argument for an access parameter", N);
7630 -- Standard message for all other cases (are there any?)
7634 ("null cannot be of an anonymous access type", N);
7638 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7639 -- assignment to a null-excluding object
7641 if Ada_Version >= Ada_2005
7642 and then Can_Never_Be_Null (Typ)
7643 and then Nkind (Parent (N)) = N_Assignment_Statement
7645 if not Inside_Init_Proc then
7647 (Compile_Time_Constraint_Error (N,
7648 "(Ada 2005) null not allowed in null-excluding objects?"),
7649 Make_Raise_Constraint_Error (Loc,
7650 Reason => CE_Access_Check_Failed));
7653 Make_Raise_Constraint_Error (Loc,
7654 Reason => CE_Access_Check_Failed));
7658 -- In a distributed context, null for a remote access to subprogram may
7659 -- need to be replaced with a special record aggregate. In this case,
7660 -- return after having done the transformation.
7662 if (Ekind (Typ) = E_Record_Type
7663 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7664 and then Remote_AST_Null_Value (N, Typ)
7669 -- The null literal takes its type from the context
7674 -----------------------
7675 -- Resolve_Op_Concat --
7676 -----------------------
7678 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7680 -- We wish to avoid deep recursion, because concatenations are often
7681 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7682 -- operands nonrecursively until we find something that is not a simple
7683 -- concatenation (A in this case). We resolve that, and then walk back
7684 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7685 -- to do the rest of the work at each level. The Parent pointers allow
7686 -- us to avoid recursion, and thus avoid running out of memory. See also
7687 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7693 -- The following code is equivalent to:
7695 -- Resolve_Op_Concat_First (NN, Typ);
7696 -- Resolve_Op_Concat_Arg (N, ...);
7697 -- Resolve_Op_Concat_Rest (N, Typ);
7699 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7700 -- operand is a concatenation.
7702 -- Walk down left operands
7705 Resolve_Op_Concat_First (NN, Typ);
7706 Op1 := Left_Opnd (NN);
7707 exit when not (Nkind (Op1) = N_Op_Concat
7708 and then not Is_Array_Type (Component_Type (Typ))
7709 and then Entity (Op1) = Entity (NN));
7713 -- Now (given the above example) NN is A&B and Op1 is A
7715 -- First resolve Op1 ...
7717 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7719 -- ... then walk NN back up until we reach N (where we started), calling
7720 -- Resolve_Op_Concat_Rest along the way.
7723 Resolve_Op_Concat_Rest (NN, Typ);
7728 if Base_Type (Etype (N)) /= Standard_String then
7729 Check_SPARK_Restriction
7730 ("result of concatenation should have type String", N);
7732 end Resolve_Op_Concat;
7734 ---------------------------
7735 -- Resolve_Op_Concat_Arg --
7736 ---------------------------
7738 procedure Resolve_Op_Concat_Arg
7744 Btyp : constant Entity_Id := Base_Type (Typ);
7745 Ctyp : constant Entity_Id := Component_Type (Typ);
7750 or else (not Is_Overloaded (Arg)
7751 and then Etype (Arg) /= Any_Composite
7752 and then Covers (Ctyp, Etype (Arg)))
7754 Resolve (Arg, Ctyp);
7756 Resolve (Arg, Btyp);
7759 -- If both Array & Array and Array & Component are visible, there is a
7760 -- potential ambiguity that must be reported.
7762 elsif Has_Compatible_Type (Arg, Ctyp) then
7763 if Nkind (Arg) = N_Aggregate
7764 and then Is_Composite_Type (Ctyp)
7766 if Is_Private_Type (Ctyp) then
7767 Resolve (Arg, Btyp);
7769 -- If the operation is user-defined and not overloaded use its
7770 -- profile. The operation may be a renaming, in which case it has
7771 -- been rewritten, and we want the original profile.
7773 elsif not Is_Overloaded (N)
7774 and then Comes_From_Source (Entity (Original_Node (N)))
7775 and then Ekind (Entity (Original_Node (N))) = E_Function
7779 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
7782 -- Otherwise an aggregate may match both the array type and the
7786 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7787 Set_Etype (Arg, Any_Type);
7791 if Is_Overloaded (Arg)
7792 and then Has_Compatible_Type (Arg, Typ)
7793 and then Etype (Arg) /= Any_Type
7801 Get_First_Interp (Arg, I, It);
7803 Get_Next_Interp (I, It);
7805 -- Special-case the error message when the overloading is
7806 -- caused by a function that yields an array and can be
7807 -- called without parameters.
7809 if It.Nam = Func then
7810 Error_Msg_Sloc := Sloc (Func);
7811 Error_Msg_N ("ambiguous call to function#", Arg);
7813 ("\\interpretation as call yields&", Arg, Typ);
7815 ("\\interpretation as indexing of call yields&",
7816 Arg, Component_Type (Typ));
7819 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
7821 Get_First_Interp (Arg, I, It);
7822 while Present (It.Nam) loop
7823 Error_Msg_Sloc := Sloc (It.Nam);
7825 if Base_Type (It.Typ) = Btyp
7827 Base_Type (It.Typ) = Base_Type (Ctyp)
7829 Error_Msg_N -- CODEFIX
7830 ("\\possible interpretation#", Arg);
7833 Get_Next_Interp (I, It);
7839 Resolve (Arg, Component_Type (Typ));
7841 if Nkind (Arg) = N_String_Literal then
7842 Set_Etype (Arg, Component_Type (Typ));
7845 if Arg = Left_Opnd (N) then
7846 Set_Is_Component_Left_Opnd (N);
7848 Set_Is_Component_Right_Opnd (N);
7853 Resolve (Arg, Btyp);
7856 -- Concatenation is restricted in SPARK: each operand must be either a
7857 -- string literal, the name of a string constant, a static character or
7858 -- string expression, or another concatenation. Arg cannot be a
7859 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
7860 -- separately on each final operand, past concatenation operations.
7862 if Is_Character_Type (Etype (Arg)) then
7863 if not Is_Static_Expression (Arg) then
7864 Check_SPARK_Restriction
7865 ("character operand for concatenation should be static", Arg);
7868 elsif Is_String_Type (Etype (Arg)) then
7869 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
7870 and then Is_Constant_Object (Entity (Arg)))
7871 and then not Is_Static_Expression (Arg)
7873 Check_SPARK_Restriction
7874 ("string operand for concatenation should be static", Arg);
7877 -- Do not issue error on an operand that is neither a character nor a
7878 -- string, as the error is issued in Resolve_Op_Concat.
7884 Check_Unset_Reference (Arg);
7885 end Resolve_Op_Concat_Arg;
7887 -----------------------------
7888 -- Resolve_Op_Concat_First --
7889 -----------------------------
7891 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7892 Btyp : constant Entity_Id := Base_Type (Typ);
7893 Op1 : constant Node_Id := Left_Opnd (N);
7894 Op2 : constant Node_Id := Right_Opnd (N);
7897 -- The parser folds an enormous sequence of concatenations of string
7898 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7899 -- in the right operand. If the expression resolves to a predefined "&"
7900 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7901 -- we give an error. See P_Simple_Expression in Par.Ch4.
7903 if Nkind (Op2) = N_String_Literal
7904 and then Is_Folded_In_Parser (Op2)
7905 and then Ekind (Entity (N)) = E_Function
7907 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7908 and then String_Length (Strval (Op1)) = 0);
7909 Error_Msg_N ("too many user-defined concatenations", N);
7913 Set_Etype (N, Btyp);
7915 if Is_Limited_Composite (Btyp) then
7916 Error_Msg_N ("concatenation not available for limited array", N);
7917 Explain_Limited_Type (Btyp, N);
7919 end Resolve_Op_Concat_First;
7921 ----------------------------
7922 -- Resolve_Op_Concat_Rest --
7923 ----------------------------
7925 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7926 Op1 : constant Node_Id := Left_Opnd (N);
7927 Op2 : constant Node_Id := Right_Opnd (N);
7930 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7932 Generate_Operator_Reference (N, Typ);
7934 if Is_String_Type (Typ) then
7935 Eval_Concatenation (N);
7938 -- If this is not a static concatenation, but the result is a string
7939 -- type (and not an array of strings) ensure that static string operands
7940 -- have their subtypes properly constructed.
7942 if Nkind (N) /= N_String_Literal
7943 and then Is_Character_Type (Component_Type (Typ))
7945 Set_String_Literal_Subtype (Op1, Typ);
7946 Set_String_Literal_Subtype (Op2, Typ);
7948 end Resolve_Op_Concat_Rest;
7950 ----------------------
7951 -- Resolve_Op_Expon --
7952 ----------------------
7954 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7955 B_Typ : constant Entity_Id := Base_Type (Typ);
7958 -- Catch attempts to do fixed-point exponentiation with universal
7959 -- operands, which is a case where the illegality is not caught during
7960 -- normal operator analysis.
7962 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7963 Error_Msg_N ("exponentiation not available for fixed point", N);
7966 elsif Nkind (Parent (N)) in N_Op
7967 and then Is_Fixed_Point_Type (Etype (Parent (N)))
7968 and then Etype (N) = Universal_Real
7969 and then Comes_From_Source (N)
7971 Error_Msg_N ("exponentiation not available for fixed point", N);
7975 if Comes_From_Source (N)
7976 and then Ekind (Entity (N)) = E_Function
7977 and then Is_Imported (Entity (N))
7978 and then Is_Intrinsic_Subprogram (Entity (N))
7980 Resolve_Intrinsic_Operator (N, Typ);
7984 if Etype (Left_Opnd (N)) = Universal_Integer
7985 or else Etype (Left_Opnd (N)) = Universal_Real
7987 Check_For_Visible_Operator (N, B_Typ);
7990 -- We do the resolution using the base type, because intermediate values
7991 -- in expressions always are of the base type, not a subtype of it.
7993 Resolve (Left_Opnd (N), B_Typ);
7994 Resolve (Right_Opnd (N), Standard_Integer);
7996 Check_Unset_Reference (Left_Opnd (N));
7997 Check_Unset_Reference (Right_Opnd (N));
7999 Set_Etype (N, B_Typ);
8000 Generate_Operator_Reference (N, B_Typ);
8003 -- Set overflow checking bit. Much cleverer code needed here eventually
8004 -- and perhaps the Resolve routines should be separated for the various
8005 -- arithmetic operations, since they will need different processing. ???
8007 if Nkind (N) in N_Op then
8008 if not Overflow_Checks_Suppressed (Etype (N)) then
8009 Enable_Overflow_Check (N);
8012 end Resolve_Op_Expon;
8014 --------------------
8015 -- Resolve_Op_Not --
8016 --------------------
8018 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8021 function Parent_Is_Boolean return Boolean;
8022 -- This function determines if the parent node is a boolean operator or
8023 -- operation (comparison op, membership test, or short circuit form) and
8024 -- the not in question is the left operand of this operation. Note that
8025 -- if the not is in parens, then false is returned.
8027 -----------------------
8028 -- Parent_Is_Boolean --
8029 -----------------------
8031 function Parent_Is_Boolean return Boolean is
8033 if Paren_Count (N) /= 0 then
8037 case Nkind (Parent (N)) is
8052 return Left_Opnd (Parent (N)) = N;
8058 end Parent_Is_Boolean;
8060 -- Start of processing for Resolve_Op_Not
8063 -- Predefined operations on scalar types yield the base type. On the
8064 -- other hand, logical operations on arrays yield the type of the
8065 -- arguments (and the context).
8067 if Is_Array_Type (Typ) then
8070 B_Typ := Base_Type (Typ);
8073 if Is_VMS_Operator (Entity (N)) then
8076 -- Straightforward case of incorrect arguments
8078 elsif not Valid_Boolean_Arg (Typ) then
8079 Error_Msg_N ("invalid operand type for operator&", N);
8080 Set_Etype (N, Any_Type);
8083 -- Special case of probable missing parens
8085 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8086 if Parent_Is_Boolean then
8088 ("operand of not must be enclosed in parentheses",
8092 ("no modular type available in this context", N);
8095 Set_Etype (N, Any_Type);
8098 -- OK resolution of NOT
8101 -- Warn if non-boolean types involved. This is a case like not a < b
8102 -- where a and b are modular, where we will get (not a) < b and most
8103 -- likely not (a < b) was intended.
8105 if Warn_On_Questionable_Missing_Parens
8106 and then not Is_Boolean_Type (Typ)
8107 and then Parent_Is_Boolean
8109 Error_Msg_N ("?not expression should be parenthesized here!", N);
8112 -- Warn on double negation if checking redundant constructs
8114 if Warn_On_Redundant_Constructs
8115 and then Comes_From_Source (N)
8116 and then Comes_From_Source (Right_Opnd (N))
8117 and then Root_Type (Typ) = Standard_Boolean
8118 and then Nkind (Right_Opnd (N)) = N_Op_Not
8120 Error_Msg_N ("redundant double negation?", N);
8123 -- Complete resolution and evaluation of NOT
8125 Resolve (Right_Opnd (N), B_Typ);
8126 Check_Unset_Reference (Right_Opnd (N));
8127 Set_Etype (N, B_Typ);
8128 Generate_Operator_Reference (N, B_Typ);
8133 -----------------------------
8134 -- Resolve_Operator_Symbol --
8135 -----------------------------
8137 -- Nothing to be done, all resolved already
8139 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8140 pragma Warnings (Off, N);
8141 pragma Warnings (Off, Typ);
8145 end Resolve_Operator_Symbol;
8147 ----------------------------------
8148 -- Resolve_Qualified_Expression --
8149 ----------------------------------
8151 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8152 pragma Warnings (Off, Typ);
8154 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8155 Expr : constant Node_Id := Expression (N);
8158 Resolve (Expr, Target_Typ);
8160 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8161 -- operation if not needed.
8163 if Restriction_Check_Required (SPARK)
8164 and then Is_Array_Type (Target_Typ)
8165 and then Is_Array_Type (Etype (Expr))
8166 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8167 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8169 Check_SPARK_Restriction
8170 ("array types should have matching static bounds", N);
8173 -- A qualified expression requires an exact match of the type, class-
8174 -- wide matching is not allowed. However, if the qualifying type is
8175 -- specific and the expression has a class-wide type, it may still be
8176 -- okay, since it can be the result of the expansion of a call to a
8177 -- dispatching function, so we also have to check class-wideness of the
8178 -- type of the expression's original node.
8180 if (Is_Class_Wide_Type (Target_Typ)
8182 (Is_Class_Wide_Type (Etype (Expr))
8183 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8184 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8186 Wrong_Type (Expr, Target_Typ);
8189 -- If the target type is unconstrained, then we reset the type of the
8190 -- result from the type of the expression. For other cases, the actual
8191 -- subtype of the expression is the target type.
8193 if Is_Composite_Type (Target_Typ)
8194 and then not Is_Constrained (Target_Typ)
8196 Set_Etype (N, Etype (Expr));
8199 Eval_Qualified_Expression (N);
8200 end Resolve_Qualified_Expression;
8202 -----------------------------------
8203 -- Resolve_Quantified_Expression --
8204 -----------------------------------
8206 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
8208 if not Alfa_Mode then
8210 -- If expansion is enabled, analysis is delayed until the expresssion
8211 -- is rewritten as a loop.
8213 if Operating_Mode /= Check_Semantics then
8217 -- The loop structure is already resolved during its analysis, only
8218 -- the resolution of the condition needs to be done. Expansion is
8219 -- disabled so that checks and other generated code are inserted in
8220 -- the tree after expression has been rewritten as a loop.
8222 Expander_Mode_Save_And_Set (False);
8223 Resolve (Condition (N), Typ);
8224 Expander_Mode_Restore;
8226 -- In Alfa mode, we need normal expansion in order to properly introduce
8227 -- the necessary transient scopes.
8230 Resolve (Condition (N), Typ);
8232 end Resolve_Quantified_Expression;
8238 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8239 L : constant Node_Id := Low_Bound (N);
8240 H : constant Node_Id := High_Bound (N);
8242 function First_Last_Ref return Boolean;
8243 -- Returns True if N is of the form X'First .. X'Last where X is the
8244 -- same entity for both attributes.
8246 --------------------
8247 -- First_Last_Ref --
8248 --------------------
8250 function First_Last_Ref return Boolean is
8251 Lorig : constant Node_Id := Original_Node (L);
8252 Horig : constant Node_Id := Original_Node (H);
8255 if Nkind (Lorig) = N_Attribute_Reference
8256 and then Nkind (Horig) = N_Attribute_Reference
8257 and then Attribute_Name (Lorig) = Name_First
8258 and then Attribute_Name (Horig) = Name_Last
8261 PL : constant Node_Id := Prefix (Lorig);
8262 PH : constant Node_Id := Prefix (Horig);
8264 if Is_Entity_Name (PL)
8265 and then Is_Entity_Name (PH)
8266 and then Entity (PL) = Entity (PH)
8276 -- Start of processing for Resolve_Range
8283 -- Check for inappropriate range on unordered enumeration type
8285 if Bad_Unordered_Enumeration_Reference (N, Typ)
8287 -- Exclude X'First .. X'Last if X is the same entity for both
8289 and then not First_Last_Ref
8291 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
8294 Check_Unset_Reference (L);
8295 Check_Unset_Reference (H);
8297 -- We have to check the bounds for being within the base range as
8298 -- required for a non-static context. Normally this is automatic and
8299 -- done as part of evaluating expressions, but the N_Range node is an
8300 -- exception, since in GNAT we consider this node to be a subexpression,
8301 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8302 -- this, but that would put the test on the main evaluation path for
8305 Check_Non_Static_Context (L);
8306 Check_Non_Static_Context (H);
8308 -- Check for an ambiguous range over character literals. This will
8309 -- happen with a membership test involving only literals.
8311 if Typ = Any_Character then
8312 Ambiguous_Character (L);
8313 Set_Etype (N, Any_Type);
8317 -- If bounds are static, constant-fold them, so size computations are
8318 -- identical between front-end and back-end. Do not perform this
8319 -- transformation while analyzing generic units, as type information
8320 -- would be lost when reanalyzing the constant node in the instance.
8322 if Is_Discrete_Type (Typ) and then Full_Expander_Active then
8323 if Is_OK_Static_Expression (L) then
8324 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8327 if Is_OK_Static_Expression (H) then
8328 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8333 --------------------------
8334 -- Resolve_Real_Literal --
8335 --------------------------
8337 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8338 Actual_Typ : constant Entity_Id := Etype (N);
8341 -- Special processing for fixed-point literals to make sure that the
8342 -- value is an exact multiple of small where this is required. We skip
8343 -- this for the universal real case, and also for generic types.
8345 if Is_Fixed_Point_Type (Typ)
8346 and then Typ /= Universal_Fixed
8347 and then Typ /= Any_Fixed
8348 and then not Is_Generic_Type (Typ)
8351 Val : constant Ureal := Realval (N);
8352 Cintr : constant Ureal := Val / Small_Value (Typ);
8353 Cint : constant Uint := UR_Trunc (Cintr);
8354 Den : constant Uint := Norm_Den (Cintr);
8358 -- Case of literal is not an exact multiple of the Small
8362 -- For a source program literal for a decimal fixed-point type,
8363 -- this is statically illegal (RM 4.9(36)).
8365 if Is_Decimal_Fixed_Point_Type (Typ)
8366 and then Actual_Typ = Universal_Real
8367 and then Comes_From_Source (N)
8369 Error_Msg_N ("value has extraneous low order digits", N);
8372 -- Generate a warning if literal from source
8374 if Is_Static_Expression (N)
8375 and then Warn_On_Bad_Fixed_Value
8378 ("?static fixed-point value is not a multiple of Small!",
8382 -- Replace literal by a value that is the exact representation
8383 -- of a value of the type, i.e. a multiple of the small value,
8384 -- by truncation, since Machine_Rounds is false for all GNAT
8385 -- fixed-point types (RM 4.9(38)).
8387 Stat := Is_Static_Expression (N);
8389 Make_Real_Literal (Sloc (N),
8390 Realval => Small_Value (Typ) * Cint));
8392 Set_Is_Static_Expression (N, Stat);
8395 -- In all cases, set the corresponding integer field
8397 Set_Corresponding_Integer_Value (N, Cint);
8401 -- Now replace the actual type by the expected type as usual
8404 Eval_Real_Literal (N);
8405 end Resolve_Real_Literal;
8407 -----------------------
8408 -- Resolve_Reference --
8409 -----------------------
8411 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8412 P : constant Node_Id := Prefix (N);
8415 -- Replace general access with specific type
8417 if Ekind (Etype (N)) = E_Allocator_Type then
8418 Set_Etype (N, Base_Type (Typ));
8421 Resolve (P, Designated_Type (Etype (N)));
8423 -- If we are taking the reference of a volatile entity, then treat it as
8424 -- a potential modification of this entity. This is too conservative,
8425 -- but necessary because remove side effects can cause transformations
8426 -- of normal assignments into reference sequences that otherwise fail to
8427 -- notice the modification.
8429 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8430 Note_Possible_Modification (P, Sure => False);
8432 end Resolve_Reference;
8434 --------------------------------
8435 -- Resolve_Selected_Component --
8436 --------------------------------
8438 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8440 Comp1 : Entity_Id := Empty; -- prevent junk warning
8441 P : constant Node_Id := Prefix (N);
8442 S : constant Node_Id := Selector_Name (N);
8443 T : Entity_Id := Etype (P);
8445 I1 : Interp_Index := 0; -- prevent junk warning
8450 function Init_Component return Boolean;
8451 -- Check whether this is the initialization of a component within an
8452 -- init proc (by assignment or call to another init proc). If true,
8453 -- there is no need for a discriminant check.
8455 --------------------
8456 -- Init_Component --
8457 --------------------
8459 function Init_Component return Boolean is
8461 return Inside_Init_Proc
8462 and then Nkind (Prefix (N)) = N_Identifier
8463 and then Chars (Prefix (N)) = Name_uInit
8464 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8467 -- Start of processing for Resolve_Selected_Component
8470 if Is_Overloaded (P) then
8472 -- Use the context type to select the prefix that has a selector
8473 -- of the correct name and type.
8476 Get_First_Interp (P, I, It);
8478 Search : while Present (It.Typ) loop
8479 if Is_Access_Type (It.Typ) then
8480 T := Designated_Type (It.Typ);
8485 -- Locate selected component. For a private prefix the selector
8486 -- can denote a discriminant.
8488 if Is_Record_Type (T) or else Is_Private_Type (T) then
8490 -- The visible components of a class-wide type are those of
8493 if Is_Class_Wide_Type (T) then
8497 Comp := First_Entity (T);
8498 while Present (Comp) loop
8499 if Chars (Comp) = Chars (S)
8500 and then Covers (Etype (Comp), Typ)
8509 It := Disambiguate (P, I1, I, Any_Type);
8511 if It = No_Interp then
8513 ("ambiguous prefix for selected component", N);
8520 -- There may be an implicit dereference. Retrieve
8521 -- designated record type.
8523 if Is_Access_Type (It1.Typ) then
8524 T := Designated_Type (It1.Typ);
8529 if Scope (Comp1) /= T then
8531 -- Resolution chooses the new interpretation.
8532 -- Find the component with the right name.
8534 Comp1 := First_Entity (T);
8535 while Present (Comp1)
8536 and then Chars (Comp1) /= Chars (S)
8538 Comp1 := Next_Entity (Comp1);
8547 Comp := Next_Entity (Comp);
8551 Get_Next_Interp (I, It);
8554 Resolve (P, It1.Typ);
8556 Set_Entity_With_Style_Check (S, Comp1);
8559 -- Resolve prefix with its type
8564 -- Generate cross-reference. We needed to wait until full overloading
8565 -- resolution was complete to do this, since otherwise we can't tell if
8566 -- we are an lvalue or not.
8568 if May_Be_Lvalue (N) then
8569 Generate_Reference (Entity (S), S, 'm');
8571 Generate_Reference (Entity (S), S, 'r');
8574 -- If prefix is an access type, the node will be transformed into an
8575 -- explicit dereference during expansion. The type of the node is the
8576 -- designated type of that of the prefix.
8578 if Is_Access_Type (Etype (P)) then
8579 T := Designated_Type (Etype (P));
8580 Check_Fully_Declared_Prefix (T, P);
8585 if Has_Discriminants (T)
8586 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8587 and then Present (Original_Record_Component (Entity (S)))
8588 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8589 and then Present (Discriminant_Checking_Func
8590 (Original_Record_Component (Entity (S))))
8591 and then not Discriminant_Checks_Suppressed (T)
8592 and then not Init_Component
8594 Set_Do_Discriminant_Check (N);
8597 if Ekind (Entity (S)) = E_Void then
8598 Error_Msg_N ("premature use of component", S);
8601 -- If the prefix is a record conversion, this may be a renamed
8602 -- discriminant whose bounds differ from those of the original
8603 -- one, so we must ensure that a range check is performed.
8605 if Nkind (P) = N_Type_Conversion
8606 and then Ekind (Entity (S)) = E_Discriminant
8607 and then Is_Discrete_Type (Typ)
8609 Set_Etype (N, Base_Type (Typ));
8612 -- Note: No Eval processing is required, because the prefix is of a
8613 -- record type, or protected type, and neither can possibly be static.
8615 -- If the array type is atomic, and is packed, and we are in a left side
8616 -- context, then this is worth a warning, since we have a situation
8617 -- where the access to the component may cause extra read/writes of the
8618 -- atomic array object, which could be considered unexpected.
8620 if Nkind (N) = N_Selected_Component
8621 and then (Is_Atomic (T)
8622 or else (Is_Entity_Name (Prefix (N))
8623 and then Is_Atomic (Entity (Prefix (N)))))
8624 and then Is_Packed (T)
8627 Error_Msg_N ("?assignment to component of packed atomic record",
8629 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8632 end Resolve_Selected_Component;
8638 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8639 B_Typ : constant Entity_Id := Base_Type (Typ);
8640 L : constant Node_Id := Left_Opnd (N);
8641 R : constant Node_Id := Right_Opnd (N);
8644 -- We do the resolution using the base type, because intermediate values
8645 -- in expressions always are of the base type, not a subtype of it.
8648 Resolve (R, Standard_Natural);
8650 Check_Unset_Reference (L);
8651 Check_Unset_Reference (R);
8653 Set_Etype (N, B_Typ);
8654 Generate_Operator_Reference (N, B_Typ);
8658 ---------------------------
8659 -- Resolve_Short_Circuit --
8660 ---------------------------
8662 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8663 B_Typ : constant Entity_Id := Base_Type (Typ);
8664 L : constant Node_Id := Left_Opnd (N);
8665 R : constant Node_Id := Right_Opnd (N);
8671 -- Check for issuing warning for always False assert/check, this happens
8672 -- when assertions are turned off, in which case the pragma Assert/Check
8673 -- was transformed into:
8675 -- if False and then <condition> then ...
8677 -- and we detect this pattern
8679 if Warn_On_Assertion_Failure
8680 and then Is_Entity_Name (R)
8681 and then Entity (R) = Standard_False
8682 and then Nkind (Parent (N)) = N_If_Statement
8683 and then Nkind (N) = N_And_Then
8684 and then Is_Entity_Name (L)
8685 and then Entity (L) = Standard_False
8688 Orig : constant Node_Id := Original_Node (Parent (N));
8691 if Nkind (Orig) = N_Pragma
8692 and then Pragma_Name (Orig) = Name_Assert
8694 -- Don't want to warn if original condition is explicit False
8697 Expr : constant Node_Id :=
8700 (First (Pragma_Argument_Associations (Orig))));
8702 if Is_Entity_Name (Expr)
8703 and then Entity (Expr) = Standard_False
8707 -- Issue warning. We do not want the deletion of the
8708 -- IF/AND-THEN to take this message with it. We achieve
8709 -- this by making sure that the expanded code points to
8710 -- the Sloc of the expression, not the original pragma.
8712 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
8713 -- The source location of the expression is not usually
8714 -- the best choice here. For example, it gets located on
8715 -- the last AND keyword in a chain of boolean expressiond
8716 -- AND'ed together. It is best to put the message on the
8717 -- first character of the assertion, which is the effect
8718 -- of the First_Node call here.
8721 ("?assertion would fail at run time!",
8723 (First (Pragma_Argument_Associations (Orig))));
8727 -- Similar processing for Check pragma
8729 elsif Nkind (Orig) = N_Pragma
8730 and then Pragma_Name (Orig) = Name_Check
8732 -- Don't want to warn if original condition is explicit False
8735 Expr : constant Node_Id :=
8739 (Pragma_Argument_Associations (Orig)))));
8741 if Is_Entity_Name (Expr)
8742 and then Entity (Expr) = Standard_False
8749 -- Again use Error_Msg_F rather than Error_Msg_N, see
8750 -- comment above for an explanation of why we do this.
8753 ("?check would fail at run time!",
8755 (Last (Pragma_Argument_Associations (Orig))));
8762 -- Continue with processing of short circuit
8764 Check_Unset_Reference (L);
8765 Check_Unset_Reference (R);
8767 Set_Etype (N, B_Typ);
8768 Eval_Short_Circuit (N);
8769 end Resolve_Short_Circuit;
8775 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8776 Name : constant Node_Id := Prefix (N);
8777 Drange : constant Node_Id := Discrete_Range (N);
8778 Array_Type : Entity_Id := Empty;
8782 if Is_Overloaded (Name) then
8784 -- Use the context type to select the prefix that yields the correct
8789 I1 : Interp_Index := 0;
8791 P : constant Node_Id := Prefix (N);
8792 Found : Boolean := False;
8795 Get_First_Interp (P, I, It);
8796 while Present (It.Typ) loop
8797 if (Is_Array_Type (It.Typ)
8798 and then Covers (Typ, It.Typ))
8799 or else (Is_Access_Type (It.Typ)
8800 and then Is_Array_Type (Designated_Type (It.Typ))
8801 and then Covers (Typ, Designated_Type (It.Typ)))
8804 It := Disambiguate (P, I1, I, Any_Type);
8806 if It = No_Interp then
8807 Error_Msg_N ("ambiguous prefix for slicing", N);
8812 Array_Type := It.Typ;
8817 Array_Type := It.Typ;
8822 Get_Next_Interp (I, It);
8827 Array_Type := Etype (Name);
8830 Resolve (Name, Array_Type);
8832 if Is_Access_Type (Array_Type) then
8833 Apply_Access_Check (N);
8834 Array_Type := Designated_Type (Array_Type);
8836 -- If the prefix is an access to an unconstrained array, we must use
8837 -- the actual subtype of the object to perform the index checks. The
8838 -- object denoted by the prefix is implicit in the node, so we build
8839 -- an explicit representation for it in order to compute the actual
8842 if not Is_Constrained (Array_Type) then
8843 Remove_Side_Effects (Prefix (N));
8846 Obj : constant Node_Id :=
8847 Make_Explicit_Dereference (Sloc (N),
8848 Prefix => New_Copy_Tree (Prefix (N)));
8850 Set_Etype (Obj, Array_Type);
8851 Set_Parent (Obj, Parent (N));
8852 Array_Type := Get_Actual_Subtype (Obj);
8856 elsif Is_Entity_Name (Name)
8857 or else Nkind (Name) = N_Explicit_Dereference
8858 or else (Nkind (Name) = N_Function_Call
8859 and then not Is_Constrained (Etype (Name)))
8861 Array_Type := Get_Actual_Subtype (Name);
8863 -- If the name is a selected component that depends on discriminants,
8864 -- build an actual subtype for it. This can happen only when the name
8865 -- itself is overloaded; otherwise the actual subtype is created when
8866 -- the selected component is analyzed.
8868 elsif Nkind (Name) = N_Selected_Component
8869 and then Full_Analysis
8870 and then Depends_On_Discriminant (First_Index (Array_Type))
8873 Act_Decl : constant Node_Id :=
8874 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8876 Insert_Action (N, Act_Decl);
8877 Array_Type := Defining_Identifier (Act_Decl);
8880 -- Maybe this should just be "else", instead of checking for the
8881 -- specific case of slice??? This is needed for the case where the
8882 -- prefix is an Image attribute, which gets expanded to a slice, and so
8883 -- has a constrained subtype which we want to use for the slice range
8884 -- check applied below (the range check won't get done if the
8885 -- unconstrained subtype of the 'Image is used).
8887 elsif Nkind (Name) = N_Slice then
8888 Array_Type := Etype (Name);
8891 -- If name was overloaded, set slice type correctly now
8893 Set_Etype (N, Array_Type);
8895 -- If the range is specified by a subtype mark, no resolution is
8896 -- necessary. Else resolve the bounds, and apply needed checks.
8898 if not Is_Entity_Name (Drange) then
8899 Index := First_Index (Array_Type);
8900 Resolve (Drange, Base_Type (Etype (Index)));
8902 if Nkind (Drange) = N_Range then
8904 -- Ensure that side effects in the bounds are properly handled
8906 Force_Evaluation (Low_Bound (Drange));
8907 Force_Evaluation (High_Bound (Drange));
8909 -- Do not apply the range check to nodes associated with the
8910 -- frontend expansion of the dispatch table. We first check
8911 -- if Ada.Tags is already loaded to avoid the addition of an
8912 -- undesired dependence on such run-time unit.
8914 if not Tagged_Type_Expansion
8916 (RTU_Loaded (Ada_Tags)
8917 and then Nkind (Prefix (N)) = N_Selected_Component
8918 and then Present (Entity (Selector_Name (Prefix (N))))
8919 and then Entity (Selector_Name (Prefix (N))) =
8920 RTE_Record_Component (RE_Prims_Ptr))
8922 Apply_Range_Check (Drange, Etype (Index));
8927 Set_Slice_Subtype (N);
8929 -- Check bad use of type with predicates
8931 if Has_Predicates (Etype (Drange)) then
8932 Bad_Predicated_Subtype_Use
8933 ("subtype& has predicate, not allowed in slice",
8934 Drange, Etype (Drange));
8936 -- Otherwise here is where we check suspicious indexes
8938 elsif Nkind (Drange) = N_Range then
8939 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8940 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8946 ----------------------------
8947 -- Resolve_String_Literal --
8948 ----------------------------
8950 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8951 C_Typ : constant Entity_Id := Component_Type (Typ);
8952 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8953 Loc : constant Source_Ptr := Sloc (N);
8954 Str : constant String_Id := Strval (N);
8955 Strlen : constant Nat := String_Length (Str);
8956 Subtype_Id : Entity_Id;
8957 Need_Check : Boolean;
8960 -- For a string appearing in a concatenation, defer creation of the
8961 -- string_literal_subtype until the end of the resolution of the
8962 -- concatenation, because the literal may be constant-folded away. This
8963 -- is a useful optimization for long concatenation expressions.
8965 -- If the string is an aggregate built for a single character (which
8966 -- happens in a non-static context) or a is null string to which special
8967 -- checks may apply, we build the subtype. Wide strings must also get a
8968 -- string subtype if they come from a one character aggregate. Strings
8969 -- generated by attributes might be static, but it is often hard to
8970 -- determine whether the enclosing context is static, so we generate
8971 -- subtypes for them as well, thus losing some rarer optimizations ???
8972 -- Same for strings that come from a static conversion.
8975 (Strlen = 0 and then Typ /= Standard_String)
8976 or else Nkind (Parent (N)) /= N_Op_Concat
8977 or else (N /= Left_Opnd (Parent (N))
8978 and then N /= Right_Opnd (Parent (N)))
8979 or else ((Typ = Standard_Wide_String
8980 or else Typ = Standard_Wide_Wide_String)
8981 and then Nkind (Original_Node (N)) /= N_String_Literal);
8983 -- If the resolving type is itself a string literal subtype, we can just
8984 -- reuse it, since there is no point in creating another.
8986 if Ekind (Typ) = E_String_Literal_Subtype then
8989 elsif Nkind (Parent (N)) = N_Op_Concat
8990 and then not Need_Check
8991 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8992 N_Attribute_Reference,
8993 N_Qualified_Expression,
8998 -- Otherwise we must create a string literal subtype. Note that the
8999 -- whole idea of string literal subtypes is simply to avoid the need
9000 -- for building a full fledged array subtype for each literal.
9003 Set_String_Literal_Subtype (N, Typ);
9004 Subtype_Id := Etype (N);
9007 if Nkind (Parent (N)) /= N_Op_Concat
9010 Set_Etype (N, Subtype_Id);
9011 Eval_String_Literal (N);
9014 if Is_Limited_Composite (Typ)
9015 or else Is_Private_Composite (Typ)
9017 Error_Msg_N ("string literal not available for private array", N);
9018 Set_Etype (N, Any_Type);
9022 -- The validity of a null string has been checked in the call to
9023 -- Eval_String_Literal.
9028 -- Always accept string literal with component type Any_Character, which
9029 -- occurs in error situations and in comparisons of literals, both of
9030 -- which should accept all literals.
9032 elsif R_Typ = Any_Character then
9035 -- If the type is bit-packed, then we always transform the string
9036 -- literal into a full fledged aggregate.
9038 elsif Is_Bit_Packed_Array (Typ) then
9041 -- Deal with cases of Wide_Wide_String, Wide_String, and String
9044 -- For Standard.Wide_Wide_String, or any other type whose component
9045 -- type is Standard.Wide_Wide_Character, we know that all the
9046 -- characters in the string must be acceptable, since the parser
9047 -- accepted the characters as valid character literals.
9049 if R_Typ = Standard_Wide_Wide_Character then
9052 -- For the case of Standard.String, or any other type whose component
9053 -- type is Standard.Character, we must make sure that there are no
9054 -- wide characters in the string, i.e. that it is entirely composed
9055 -- of characters in range of type Character.
9057 -- If the string literal is the result of a static concatenation, the
9058 -- test has already been performed on the components, and need not be
9061 elsif R_Typ = Standard_Character
9062 and then Nkind (Original_Node (N)) /= N_Op_Concat
9064 for J in 1 .. Strlen loop
9065 if not In_Character_Range (Get_String_Char (Str, J)) then
9067 -- If we are out of range, post error. This is one of the
9068 -- very few places that we place the flag in the middle of
9069 -- a token, right under the offending wide character. Not
9070 -- quite clear if this is right wrt wide character encoding
9071 -- sequences, but it's only an error message!
9074 ("literal out of range of type Standard.Character",
9075 Source_Ptr (Int (Loc) + J));
9080 -- For the case of Standard.Wide_String, or any other type whose
9081 -- component type is Standard.Wide_Character, we must make sure that
9082 -- there are no wide characters in the string, i.e. that it is
9083 -- entirely composed of characters in range of type Wide_Character.
9085 -- If the string literal is the result of a static concatenation,
9086 -- the test has already been performed on the components, and need
9089 elsif R_Typ = Standard_Wide_Character
9090 and then Nkind (Original_Node (N)) /= N_Op_Concat
9092 for J in 1 .. Strlen loop
9093 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
9095 -- If we are out of range, post error. This is one of the
9096 -- very few places that we place the flag in the middle of
9097 -- a token, right under the offending wide character.
9099 -- This is not quite right, because characters in general
9100 -- will take more than one character position ???
9103 ("literal out of range of type Standard.Wide_Character",
9104 Source_Ptr (Int (Loc) + J));
9109 -- If the root type is not a standard character, then we will convert
9110 -- the string into an aggregate and will let the aggregate code do
9111 -- the checking. Standard Wide_Wide_Character is also OK here.
9117 -- See if the component type of the array corresponding to the string
9118 -- has compile time known bounds. If yes we can directly check
9119 -- whether the evaluation of the string will raise constraint error.
9120 -- Otherwise we need to transform the string literal into the
9121 -- corresponding character aggregate and let the aggregate code do
9124 if Is_Standard_Character_Type (R_Typ) then
9126 -- Check for the case of full range, where we are definitely OK
9128 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
9132 -- Here the range is not the complete base type range, so check
9135 Comp_Typ_Lo : constant Node_Id :=
9136 Type_Low_Bound (Component_Type (Typ));
9137 Comp_Typ_Hi : constant Node_Id :=
9138 Type_High_Bound (Component_Type (Typ));
9143 if Compile_Time_Known_Value (Comp_Typ_Lo)
9144 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9146 for J in 1 .. Strlen loop
9147 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9149 if Char_Val < Expr_Value (Comp_Typ_Lo)
9150 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9152 Apply_Compile_Time_Constraint_Error
9153 (N, "character out of range?", CE_Range_Check_Failed,
9154 Loc => Source_Ptr (Int (Loc) + J));
9164 -- If we got here we meed to transform the string literal into the
9165 -- equivalent qualified positional array aggregate. This is rather
9166 -- heavy artillery for this situation, but it is hard work to avoid.
9169 Lits : constant List_Id := New_List;
9170 P : Source_Ptr := Loc + 1;
9174 -- Build the character literals, we give them source locations that
9175 -- correspond to the string positions, which is a bit tricky given
9176 -- the possible presence of wide character escape sequences.
9178 for J in 1 .. Strlen loop
9179 C := Get_String_Char (Str, J);
9180 Set_Character_Literal_Name (C);
9183 Make_Character_Literal (P,
9185 Char_Literal_Value => UI_From_CC (C)));
9187 if In_Character_Range (C) then
9190 -- Should we have a call to Skip_Wide here ???
9199 Make_Qualified_Expression (Loc,
9200 Subtype_Mark => New_Reference_To (Typ, Loc),
9202 Make_Aggregate (Loc, Expressions => Lits)));
9204 Analyze_And_Resolve (N, Typ);
9206 end Resolve_String_Literal;
9208 -----------------------------
9209 -- Resolve_Subprogram_Info --
9210 -----------------------------
9212 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9215 end Resolve_Subprogram_Info;
9217 -----------------------------
9218 -- Resolve_Type_Conversion --
9219 -----------------------------
9221 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9222 Conv_OK : constant Boolean := Conversion_OK (N);
9223 Operand : constant Node_Id := Expression (N);
9224 Operand_Typ : constant Entity_Id := Etype (Operand);
9225 Target_Typ : constant Entity_Id := Etype (N);
9230 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9231 -- Set to False to suppress cases where we want to suppress the test
9232 -- for redundancy to avoid possible false positives on this warning.
9236 and then not Valid_Conversion (N, Target_Typ, Operand)
9241 -- If the Operand Etype is Universal_Fixed, then the conversion is
9242 -- never redundant. We need this check because by the time we have
9243 -- finished the rather complex transformation, the conversion looks
9244 -- redundant when it is not.
9246 if Operand_Typ = Universal_Fixed then
9247 Test_Redundant := False;
9249 -- If the operand is marked as Any_Fixed, then special processing is
9250 -- required. This is also a case where we suppress the test for a
9251 -- redundant conversion, since most certainly it is not redundant.
9253 elsif Operand_Typ = Any_Fixed then
9254 Test_Redundant := False;
9256 -- Mixed-mode operation involving a literal. Context must be a fixed
9257 -- type which is applied to the literal subsequently.
9259 if Is_Fixed_Point_Type (Typ) then
9260 Set_Etype (Operand, Universal_Real);
9262 elsif Is_Numeric_Type (Typ)
9263 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9264 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9266 Etype (Left_Opnd (Operand)) = Universal_Real)
9268 -- Return if expression is ambiguous
9270 if Unique_Fixed_Point_Type (N) = Any_Type then
9273 -- If nothing else, the available fixed type is Duration
9276 Set_Etype (Operand, Standard_Duration);
9279 -- Resolve the real operand with largest available precision
9281 if Etype (Right_Opnd (Operand)) = Universal_Real then
9282 Rop := New_Copy_Tree (Right_Opnd (Operand));
9284 Rop := New_Copy_Tree (Left_Opnd (Operand));
9287 Resolve (Rop, Universal_Real);
9289 -- If the operand is a literal (it could be a non-static and
9290 -- illegal exponentiation) check whether the use of Duration
9291 -- is potentially inaccurate.
9293 if Nkind (Rop) = N_Real_Literal
9294 and then Realval (Rop) /= Ureal_0
9295 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9298 ("?universal real operand can only " &
9299 "be interpreted as Duration!",
9302 ("\?precision will be lost in the conversion!", Rop);
9305 elsif Is_Numeric_Type (Typ)
9306 and then Nkind (Operand) in N_Op
9307 and then Unique_Fixed_Point_Type (N) /= Any_Type
9309 Set_Etype (Operand, Standard_Duration);
9312 Error_Msg_N ("invalid context for mixed mode operation", N);
9313 Set_Etype (Operand, Any_Type);
9320 -- In SPARK, a type conversion between array types should be restricted
9321 -- to types which have matching static bounds.
9323 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9324 -- operation if not needed.
9326 if Restriction_Check_Required (SPARK)
9327 and then Is_Array_Type (Target_Typ)
9328 and then Is_Array_Type (Operand_Typ)
9329 and then Operand_Typ /= Any_Composite -- or else Operand in error
9330 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9332 Check_SPARK_Restriction
9333 ("array types should have matching static bounds", N);
9336 -- In formal mode, the operand of an ancestor type conversion must be an
9337 -- object (not an expression).
9339 if Is_Tagged_Type (Target_Typ)
9340 and then not Is_Class_Wide_Type (Target_Typ)
9341 and then Is_Tagged_Type (Operand_Typ)
9342 and then not Is_Class_Wide_Type (Operand_Typ)
9343 and then Is_Ancestor (Target_Typ, Operand_Typ)
9344 and then not Is_SPARK_Object_Reference (Operand)
9346 Check_SPARK_Restriction ("object required", Operand);
9349 -- Note: we do the Eval_Type_Conversion call before applying the
9350 -- required checks for a subtype conversion. This is important, since
9351 -- both are prepared under certain circumstances to change the type
9352 -- conversion to a constraint error node, but in the case of
9353 -- Eval_Type_Conversion this may reflect an illegality in the static
9354 -- case, and we would miss the illegality (getting only a warning
9355 -- message), if we applied the type conversion checks first.
9357 Eval_Type_Conversion (N);
9359 -- Even when evaluation is not possible, we may be able to simplify the
9360 -- conversion or its expression. This needs to be done before applying
9361 -- checks, since otherwise the checks may use the original expression
9362 -- and defeat the simplifications. This is specifically the case for
9363 -- elimination of the floating-point Truncation attribute in
9364 -- float-to-int conversions.
9366 Simplify_Type_Conversion (N);
9368 -- If after evaluation we still have a type conversion, then we may need
9369 -- to apply checks required for a subtype conversion.
9371 -- Skip these type conversion checks if universal fixed operands
9372 -- operands involved, since range checks are handled separately for
9373 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9375 if Nkind (N) = N_Type_Conversion
9376 and then not Is_Generic_Type (Root_Type (Target_Typ))
9377 and then Target_Typ /= Universal_Fixed
9378 and then Operand_Typ /= Universal_Fixed
9380 Apply_Type_Conversion_Checks (N);
9383 -- Issue warning for conversion of simple object to its own type. We
9384 -- have to test the original nodes, since they may have been rewritten
9385 -- by various optimizations.
9387 Orig_N := Original_Node (N);
9389 -- Here we test for a redundant conversion if the warning mode is
9390 -- active (and was not locally reset), and we have a type conversion
9391 -- from source not appearing in a generic instance.
9394 and then Nkind (Orig_N) = N_Type_Conversion
9395 and then Comes_From_Source (Orig_N)
9396 and then not In_Instance
9398 Orig_N := Original_Node (Expression (Orig_N));
9399 Orig_T := Target_Typ;
9401 -- If the node is part of a larger expression, the Target_Type
9402 -- may not be the original type of the node if the context is a
9403 -- condition. Recover original type to see if conversion is needed.
9405 if Is_Boolean_Type (Orig_T)
9406 and then Nkind (Parent (N)) in N_Op
9408 Orig_T := Etype (Parent (N));
9411 -- If we have an entity name, then give the warning if the entity
9412 -- is the right type, or if it is a loop parameter covered by the
9413 -- original type (that's needed because loop parameters have an
9414 -- odd subtype coming from the bounds).
9416 if (Is_Entity_Name (Orig_N)
9418 (Etype (Entity (Orig_N)) = Orig_T
9420 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9421 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9423 -- If not an entity, then type of expression must match
9425 or else Etype (Orig_N) = Orig_T
9427 -- One more check, do not give warning if the analyzed conversion
9428 -- has an expression with non-static bounds, and the bounds of the
9429 -- target are static. This avoids junk warnings in cases where the
9430 -- conversion is necessary to establish staticness, for example in
9431 -- a case statement.
9433 if not Is_OK_Static_Subtype (Operand_Typ)
9434 and then Is_OK_Static_Subtype (Target_Typ)
9438 -- Finally, if this type conversion occurs in a context requiring
9439 -- a prefix, and the expression is a qualified expression then the
9440 -- type conversion is not redundant, since a qualified expression
9441 -- is not a prefix, whereas a type conversion is. For example, "X
9442 -- := T'(Funx(...)).Y;" is illegal because a selected component
9443 -- requires a prefix, but a type conversion makes it legal: "X :=
9444 -- T(T'(Funx(...))).Y;"
9446 -- In Ada 2012, a qualified expression is a name, so this idiom is
9447 -- no longer needed, but we still suppress the warning because it
9448 -- seems unfriendly for warnings to pop up when you switch to the
9449 -- newer language version.
9451 elsif Nkind (Orig_N) = N_Qualified_Expression
9452 and then Nkind_In (Parent (N), N_Attribute_Reference,
9453 N_Indexed_Component,
9454 N_Selected_Component,
9456 N_Explicit_Dereference)
9460 -- Here we give the redundant conversion warning. If it is an
9461 -- entity, give the name of the entity in the message. If not,
9462 -- just mention the expression.
9465 if Is_Entity_Name (Orig_N) then
9466 Error_Msg_Node_2 := Orig_T;
9467 Error_Msg_NE -- CODEFIX
9468 ("?redundant conversion, & is of type &!",
9469 N, Entity (Orig_N));
9472 ("?redundant conversion, expression is of type&!",
9479 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9480 -- No need to perform any interface conversion if the type of the
9481 -- expression coincides with the target type.
9483 if Ada_Version >= Ada_2005
9484 and then Full_Expander_Active
9485 and then Operand_Typ /= Target_Typ
9488 Opnd : Entity_Id := Operand_Typ;
9489 Target : Entity_Id := Target_Typ;
9492 if Is_Access_Type (Opnd) then
9493 Opnd := Designated_Type (Opnd);
9496 if Is_Access_Type (Target_Typ) then
9497 Target := Designated_Type (Target);
9500 if Opnd = Target then
9503 -- Conversion from interface type
9505 elsif Is_Interface (Opnd) then
9507 -- Ada 2005 (AI-217): Handle entities from limited views
9509 if From_With_Type (Opnd) then
9510 Error_Msg_Qual_Level := 99;
9511 Error_Msg_NE -- CODEFIX
9512 ("missing WITH clause on package &", N,
9513 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9515 ("type conversions require visibility of the full view",
9518 elsif From_With_Type (Target)
9520 (Is_Access_Type (Target_Typ)
9521 and then Present (Non_Limited_View (Etype (Target))))
9523 Error_Msg_Qual_Level := 99;
9524 Error_Msg_NE -- CODEFIX
9525 ("missing WITH clause on package &", N,
9526 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9528 ("type conversions require visibility of the full view",
9532 Expand_Interface_Conversion (N, Is_Static => False);
9535 -- Conversion to interface type
9537 elsif Is_Interface (Target) then
9541 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9542 Opnd := Etype (Opnd);
9545 if not Interface_Present_In_Ancestor
9549 if Is_Class_Wide_Type (Opnd) then
9551 -- The static analysis is not enough to know if the
9552 -- interface is implemented or not. Hence we must pass
9553 -- the work to the expander to generate code to evaluate
9554 -- the conversion at run time.
9556 Expand_Interface_Conversion (N, Is_Static => False);
9559 Error_Msg_Name_1 := Chars (Etype (Target));
9560 Error_Msg_Name_2 := Chars (Opnd);
9562 ("wrong interface conversion (% is not a progenitor " &
9567 Expand_Interface_Conversion (N);
9572 end Resolve_Type_Conversion;
9574 ----------------------
9575 -- Resolve_Unary_Op --
9576 ----------------------
9578 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9579 B_Typ : constant Entity_Id := Base_Type (Typ);
9580 R : constant Node_Id := Right_Opnd (N);
9586 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9587 Error_Msg_Name_1 := Chars (Typ);
9588 Check_SPARK_Restriction
9589 ("unary operator not defined for modular type%", N);
9592 -- Deal with intrinsic unary operators
9594 if Comes_From_Source (N)
9595 and then Ekind (Entity (N)) = E_Function
9596 and then Is_Imported (Entity (N))
9597 and then Is_Intrinsic_Subprogram (Entity (N))
9599 Resolve_Intrinsic_Unary_Operator (N, Typ);
9603 -- Deal with universal cases
9605 if Etype (R) = Universal_Integer
9607 Etype (R) = Universal_Real
9609 Check_For_Visible_Operator (N, B_Typ);
9612 Set_Etype (N, B_Typ);
9615 -- Generate warning for expressions like abs (x mod 2)
9617 if Warn_On_Redundant_Constructs
9618 and then Nkind (N) = N_Op_Abs
9620 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9622 if OK and then Hi >= Lo and then Lo >= 0 then
9623 Error_Msg_N -- CODEFIX
9624 ("?abs applied to known non-negative value has no effect", N);
9628 -- Deal with reference generation
9630 Check_Unset_Reference (R);
9631 Generate_Operator_Reference (N, B_Typ);
9634 -- Set overflow checking bit. Much cleverer code needed here eventually
9635 -- and perhaps the Resolve routines should be separated for the various
9636 -- arithmetic operations, since they will need different processing ???
9638 if Nkind (N) in N_Op then
9639 if not Overflow_Checks_Suppressed (Etype (N)) then
9640 Enable_Overflow_Check (N);
9644 -- Generate warning for expressions like -5 mod 3 for integers. No need
9645 -- to worry in the floating-point case, since parens do not affect the
9646 -- result so there is no point in giving in a warning.
9649 Norig : constant Node_Id := Original_Node (N);
9658 if Warn_On_Questionable_Missing_Parens
9659 and then Comes_From_Source (Norig)
9660 and then Is_Integer_Type (Typ)
9661 and then Nkind (Norig) = N_Op_Minus
9663 Rorig := Original_Node (Right_Opnd (Norig));
9665 -- We are looking for cases where the right operand is not
9666 -- parenthesized, and is a binary operator, multiply, divide, or
9667 -- mod. These are the cases where the grouping can affect results.
9669 if Paren_Count (Rorig) = 0
9670 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9672 -- For mod, we always give the warning, since the value is
9673 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9674 -- -(5 mod 315)). But for the other cases, the only concern is
9675 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9676 -- overflows, but (-2) * 64 does not). So we try to give the
9677 -- message only when overflow is possible.
9679 if Nkind (Rorig) /= N_Op_Mod
9680 and then Compile_Time_Known_Value (R)
9682 Val := Expr_Value (R);
9684 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9685 HB := Expr_Value (Type_High_Bound (Typ));
9687 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9690 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9691 LB := Expr_Value (Type_Low_Bound (Typ));
9693 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9696 -- Note that the test below is deliberately excluding the
9697 -- largest negative number, since that is a potentially
9698 -- troublesome case (e.g. -2 * x, where the result is the
9699 -- largest negative integer has an overflow with 2 * x).
9701 if Val > LB and then Val <= HB then
9706 -- For the multiplication case, the only case we have to worry
9707 -- about is when (-a)*b is exactly the largest negative number
9708 -- so that -(a*b) can cause overflow. This can only happen if
9709 -- a is a power of 2, and more generally if any operand is a
9710 -- constant that is not a power of 2, then the parentheses
9711 -- cannot affect whether overflow occurs. We only bother to
9712 -- test the left most operand
9714 -- Loop looking at left operands for one that has known value
9717 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9718 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9719 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9721 -- Operand value of 0 or 1 skips warning
9726 -- Otherwise check power of 2, if power of 2, warn, if
9727 -- anything else, skip warning.
9730 while Lval /= 2 loop
9731 if Lval mod 2 = 1 then
9742 -- Keep looking at left operands
9744 Opnd := Left_Opnd (Opnd);
9747 -- For rem or "/" we can only have a problematic situation
9748 -- if the divisor has a value of minus one or one. Otherwise
9749 -- overflow is impossible (divisor > 1) or we have a case of
9750 -- division by zero in any case.
9752 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9753 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9754 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9759 -- If we fall through warning should be issued
9762 ("?unary minus expression should be parenthesized here!", N);
9766 end Resolve_Unary_Op;
9768 ----------------------------------
9769 -- Resolve_Unchecked_Expression --
9770 ----------------------------------
9772 procedure Resolve_Unchecked_Expression
9777 Resolve (Expression (N), Typ, Suppress => All_Checks);
9779 end Resolve_Unchecked_Expression;
9781 ---------------------------------------
9782 -- Resolve_Unchecked_Type_Conversion --
9783 ---------------------------------------
9785 procedure Resolve_Unchecked_Type_Conversion
9789 pragma Warnings (Off, Typ);
9791 Operand : constant Node_Id := Expression (N);
9792 Opnd_Type : constant Entity_Id := Etype (Operand);
9795 -- Resolve operand using its own type
9797 Resolve (Operand, Opnd_Type);
9798 Eval_Unchecked_Conversion (N);
9799 end Resolve_Unchecked_Type_Conversion;
9801 ------------------------------
9802 -- Rewrite_Operator_As_Call --
9803 ------------------------------
9805 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9806 Loc : constant Source_Ptr := Sloc (N);
9807 Actuals : constant List_Id := New_List;
9811 if Nkind (N) in N_Binary_Op then
9812 Append (Left_Opnd (N), Actuals);
9815 Append (Right_Opnd (N), Actuals);
9818 Make_Function_Call (Sloc => Loc,
9819 Name => New_Occurrence_Of (Nam, Loc),
9820 Parameter_Associations => Actuals);
9822 Preserve_Comes_From_Source (New_N, N);
9823 Preserve_Comes_From_Source (Name (New_N), N);
9825 Set_Etype (N, Etype (Nam));
9826 end Rewrite_Operator_As_Call;
9828 ------------------------------
9829 -- Rewrite_Renamed_Operator --
9830 ------------------------------
9832 procedure Rewrite_Renamed_Operator
9837 Nam : constant Name_Id := Chars (Op);
9838 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9842 -- Rewrite the operator node using the real operator, not its renaming.
9843 -- Exclude user-defined intrinsic operations of the same name, which are
9844 -- treated separately and rewritten as calls.
9846 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9847 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9848 Set_Chars (Op_Node, Nam);
9849 Set_Etype (Op_Node, Etype (N));
9850 Set_Entity (Op_Node, Op);
9851 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9853 -- Indicate that both the original entity and its renaming are
9854 -- referenced at this point.
9856 Generate_Reference (Entity (N), N);
9857 Generate_Reference (Op, N);
9860 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9863 Rewrite (N, Op_Node);
9865 -- If the context type is private, add the appropriate conversions so
9866 -- that the operator is applied to the full view. This is done in the
9867 -- routines that resolve intrinsic operators.
9869 if Is_Intrinsic_Subprogram (Op)
9870 and then Is_Private_Type (Typ)
9873 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9874 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9875 Resolve_Intrinsic_Operator (N, Typ);
9877 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9878 Resolve_Intrinsic_Unary_Operator (N, Typ);
9885 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9887 -- Operator renames a user-defined operator of the same name. Use the
9888 -- original operator in the node, which is the one Gigi knows about.
9891 Set_Is_Overloaded (N, False);
9893 end Rewrite_Renamed_Operator;
9895 -----------------------
9896 -- Set_Slice_Subtype --
9897 -----------------------
9899 -- Build an implicit subtype declaration to represent the type delivered by
9900 -- the slice. This is an abbreviated version of an array subtype. We define
9901 -- an index subtype for the slice, using either the subtype name or the
9902 -- discrete range of the slice. To be consistent with index usage elsewhere
9903 -- we create a list header to hold the single index. This list is not
9904 -- otherwise attached to the syntax tree.
9906 procedure Set_Slice_Subtype (N : Node_Id) is
9907 Loc : constant Source_Ptr := Sloc (N);
9908 Index_List : constant List_Id := New_List;
9910 Index_Subtype : Entity_Id;
9911 Index_Type : Entity_Id;
9912 Slice_Subtype : Entity_Id;
9913 Drange : constant Node_Id := Discrete_Range (N);
9916 if Is_Entity_Name (Drange) then
9917 Index_Subtype := Entity (Drange);
9920 -- We force the evaluation of a range. This is definitely needed in
9921 -- the renamed case, and seems safer to do unconditionally. Note in
9922 -- any case that since we will create and insert an Itype referring
9923 -- to this range, we must make sure any side effect removal actions
9924 -- are inserted before the Itype definition.
9926 if Nkind (Drange) = N_Range then
9927 Force_Evaluation (Low_Bound (Drange));
9928 Force_Evaluation (High_Bound (Drange));
9931 Index_Type := Base_Type (Etype (Drange));
9933 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9935 -- Take a new copy of Drange (where bounds have been rewritten to
9936 -- reference side-effect-free names). Using a separate tree ensures
9937 -- that further expansion (e.g. while rewriting a slice assignment
9938 -- into a FOR loop) does not attempt to remove side effects on the
9939 -- bounds again (which would cause the bounds in the index subtype
9940 -- definition to refer to temporaries before they are defined) (the
9941 -- reason is that some names are considered side effect free here
9942 -- for the subtype, but not in the context of a loop iteration
9945 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9946 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
9947 Set_Etype (Index_Subtype, Index_Type);
9948 Set_Size_Info (Index_Subtype, Index_Type);
9949 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9952 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9954 Index := New_Occurrence_Of (Index_Subtype, Loc);
9955 Set_Etype (Index, Index_Subtype);
9956 Append (Index, Index_List);
9958 Set_First_Index (Slice_Subtype, Index);
9959 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9960 Set_Is_Constrained (Slice_Subtype, True);
9962 Check_Compile_Time_Size (Slice_Subtype);
9964 -- The Etype of the existing Slice node is reset to this slice subtype.
9965 -- Its bounds are obtained from its first index.
9967 Set_Etype (N, Slice_Subtype);
9969 -- For packed slice subtypes, freeze immediately (except in the case of
9970 -- being in a "spec expression" where we never freeze when we first see
9973 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9974 Freeze_Itype (Slice_Subtype, N);
9976 -- For all other cases insert an itype reference in the slice's actions
9977 -- so that the itype is frozen at the proper place in the tree (i.e. at
9978 -- the point where actions for the slice are analyzed). Note that this
9979 -- is different from freezing the itype immediately, which might be
9980 -- premature (e.g. if the slice is within a transient scope). This needs
9981 -- to be done only if expansion is enabled.
9983 elsif Full_Expander_Active then
9984 Ensure_Defined (Typ => Slice_Subtype, N => N);
9986 end Set_Slice_Subtype;
9988 --------------------------------
9989 -- Set_String_Literal_Subtype --
9990 --------------------------------
9992 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9993 Loc : constant Source_Ptr := Sloc (N);
9994 Low_Bound : constant Node_Id :=
9995 Type_Low_Bound (Etype (First_Index (Typ)));
9996 Subtype_Id : Entity_Id;
9999 if Nkind (N) /= N_String_Literal then
10003 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
10004 Set_String_Literal_Length (Subtype_Id, UI_From_Int
10005 (String_Length (Strval (N))));
10006 Set_Etype (Subtype_Id, Base_Type (Typ));
10007 Set_Is_Constrained (Subtype_Id);
10008 Set_Etype (N, Subtype_Id);
10010 if Is_OK_Static_Expression (Low_Bound) then
10012 -- The low bound is set from the low bound of the corresponding index
10013 -- type. Note that we do not store the high bound in the string literal
10014 -- subtype, but it can be deduced if necessary from the length and the
10017 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
10020 -- If the lower bound is not static we create a range for the string
10021 -- literal, using the index type and the known length of the literal.
10022 -- The index type is not necessarily Positive, so the upper bound is
10023 -- computed as T'Val (T'Pos (Low_Bound) + L - 1)
10026 Index_List : constant List_Id := New_List;
10027 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
10029 High_Bound : constant Node_Id :=
10030 Make_Attribute_Reference (Loc,
10031 Attribute_Name => Name_Val,
10033 New_Occurrence_Of (Index_Type, Loc),
10034 Expressions => New_List (
10037 Make_Attribute_Reference (Loc,
10038 Attribute_Name => Name_Pos,
10040 New_Occurrence_Of (Index_Type, Loc),
10042 New_List (New_Copy_Tree (Low_Bound))),
10044 Make_Integer_Literal (Loc,
10045 String_Length (Strval (N)) - 1))));
10047 Array_Subtype : Entity_Id;
10048 Index_Subtype : Entity_Id;
10053 if Is_Integer_Type (Index_Type) then
10054 Set_String_Literal_Low_Bound
10055 (Subtype_Id, Make_Integer_Literal (Loc, 1));
10058 -- If the index type is an enumeration type, build bounds
10059 -- expression with attributes.
10061 Set_String_Literal_Low_Bound
10063 Make_Attribute_Reference (Loc,
10064 Attribute_Name => Name_First,
10066 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
10067 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
10070 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
10072 -- Build bona fide subtype for the string, and wrap it in an
10073 -- unchecked conversion, because the backend expects the
10074 -- String_Literal_Subtype to have a static lower bound.
10077 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10078 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
10079 Set_Scalar_Range (Index_Subtype, Drange);
10080 Set_Parent (Drange, N);
10081 Analyze_And_Resolve (Drange, Index_Type);
10083 -- In the context, the Index_Type may already have a constraint,
10084 -- so use common base type on string subtype. The base type may
10085 -- be used when generating attributes of the string, for example
10086 -- in the context of a slice assignment.
10088 Set_Etype (Index_Subtype, Base_Type (Index_Type));
10089 Set_Size_Info (Index_Subtype, Index_Type);
10090 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10092 Array_Subtype := Create_Itype (E_Array_Subtype, N);
10094 Index := New_Occurrence_Of (Index_Subtype, Loc);
10095 Set_Etype (Index, Index_Subtype);
10096 Append (Index, Index_List);
10098 Set_First_Index (Array_Subtype, Index);
10099 Set_Etype (Array_Subtype, Base_Type (Typ));
10100 Set_Is_Constrained (Array_Subtype, True);
10103 Make_Unchecked_Type_Conversion (Loc,
10104 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
10105 Expression => Relocate_Node (N)));
10106 Set_Etype (N, Array_Subtype);
10109 end Set_String_Literal_Subtype;
10111 ------------------------------
10112 -- Simplify_Type_Conversion --
10113 ------------------------------
10115 procedure Simplify_Type_Conversion (N : Node_Id) is
10117 if Nkind (N) = N_Type_Conversion then
10119 Operand : constant Node_Id := Expression (N);
10120 Target_Typ : constant Entity_Id := Etype (N);
10121 Opnd_Typ : constant Entity_Id := Etype (Operand);
10124 if Is_Floating_Point_Type (Opnd_Typ)
10126 (Is_Integer_Type (Target_Typ)
10127 or else (Is_Fixed_Point_Type (Target_Typ)
10128 and then Conversion_OK (N)))
10129 and then Nkind (Operand) = N_Attribute_Reference
10130 and then Attribute_Name (Operand) = Name_Truncation
10132 -- Special processing required if the conversion is the expression
10133 -- of a Truncation attribute reference. In this case we replace:
10135 -- ityp (ftyp'Truncation (x))
10141 -- with the Float_Truncate flag set, which is more efficient.
10145 Relocate_Node (First (Expressions (Operand))));
10146 Set_Float_Truncate (N, True);
10150 end Simplify_Type_Conversion;
10152 -----------------------------
10153 -- Unique_Fixed_Point_Type --
10154 -----------------------------
10156 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10157 T1 : Entity_Id := Empty;
10162 procedure Fixed_Point_Error;
10163 -- Give error messages for true ambiguity. Messages are posted on node
10164 -- N, and entities T1, T2 are the possible interpretations.
10166 -----------------------
10167 -- Fixed_Point_Error --
10168 -----------------------
10170 procedure Fixed_Point_Error is
10172 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10173 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10174 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10175 end Fixed_Point_Error;
10177 -- Start of processing for Unique_Fixed_Point_Type
10180 -- The operations on Duration are visible, so Duration is always a
10181 -- possible interpretation.
10183 T1 := Standard_Duration;
10185 -- Look for fixed-point types in enclosing scopes
10187 Scop := Current_Scope;
10188 while Scop /= Standard_Standard loop
10189 T2 := First_Entity (Scop);
10190 while Present (T2) loop
10191 if Is_Fixed_Point_Type (T2)
10192 and then Current_Entity (T2) = T2
10193 and then Scope (Base_Type (T2)) = Scop
10195 if Present (T1) then
10206 Scop := Scope (Scop);
10209 -- Look for visible fixed type declarations in the context
10211 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10212 while Present (Item) loop
10213 if Nkind (Item) = N_With_Clause then
10214 Scop := Entity (Name (Item));
10215 T2 := First_Entity (Scop);
10216 while Present (T2) loop
10217 if Is_Fixed_Point_Type (T2)
10218 and then Scope (Base_Type (T2)) = Scop
10219 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10221 if Present (T1) then
10236 if Nkind (N) = N_Real_Literal then
10237 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
10239 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
10243 end Unique_Fixed_Point_Type;
10245 ----------------------
10246 -- Valid_Conversion --
10247 ----------------------
10249 function Valid_Conversion
10251 Target : Entity_Id;
10253 Report_Errs : Boolean := True) return Boolean
10255 Target_Type : constant Entity_Id := Base_Type (Target);
10256 Opnd_Type : Entity_Id := Etype (Operand);
10258 function Conversion_Check
10260 Msg : String) return Boolean;
10261 -- Little routine to post Msg if Valid is False, returns Valid value
10263 -- The following are badly named, this kind of overloading is actively
10264 -- confusing in reading code, please rename to something like
10265 -- Error_Msg_N_If_Reporting ???
10267 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id);
10268 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10270 procedure Error_Msg_NE
10272 N : Node_Or_Entity_Id;
10273 E : Node_Or_Entity_Id);
10274 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10276 function Valid_Tagged_Conversion
10277 (Target_Type : Entity_Id;
10278 Opnd_Type : Entity_Id) return Boolean;
10279 -- Specifically test for validity of tagged conversions
10281 function Valid_Array_Conversion return Boolean;
10282 -- Check index and component conformance, and accessibility levels if
10283 -- the component types are anonymous access types (Ada 2005).
10285 ----------------------
10286 -- Conversion_Check --
10287 ----------------------
10289 function Conversion_Check
10291 Msg : String) return Boolean
10296 -- A generic unit has already been analyzed and we have verified
10297 -- that a particular conversion is OK in that context. Since the
10298 -- instance is reanalyzed without relying on the relationships
10299 -- established during the analysis of the generic, it is possible
10300 -- to end up with inconsistent views of private types. Do not emit
10301 -- the error message in such cases. The rest of the machinery in
10302 -- Valid_Conversion still ensures the proper compatibility of
10303 -- target and operand types.
10305 and then not In_Instance
10307 Error_Msg_N (Msg, Operand);
10311 end Conversion_Check;
10317 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id) is
10319 if Report_Errs then
10320 Errout.Error_Msg_N (Msg, N);
10328 procedure Error_Msg_NE
10330 N : Node_Or_Entity_Id;
10331 E : Node_Or_Entity_Id)
10334 if Report_Errs then
10335 Errout.Error_Msg_NE (Msg, N, E);
10339 ----------------------------
10340 -- Valid_Array_Conversion --
10341 ----------------------------
10343 function Valid_Array_Conversion return Boolean
10345 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10346 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10348 Opnd_Index : Node_Id;
10349 Opnd_Index_Type : Entity_Id;
10351 Target_Comp_Type : constant Entity_Id :=
10352 Component_Type (Target_Type);
10353 Target_Comp_Base : constant Entity_Id :=
10354 Base_Type (Target_Comp_Type);
10356 Target_Index : Node_Id;
10357 Target_Index_Type : Entity_Id;
10360 -- Error if wrong number of dimensions
10363 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10366 ("incompatible number of dimensions for conversion", Operand);
10369 -- Number of dimensions matches
10372 -- Loop through indexes of the two arrays
10374 Target_Index := First_Index (Target_Type);
10375 Opnd_Index := First_Index (Opnd_Type);
10376 while Present (Target_Index) and then Present (Opnd_Index) loop
10377 Target_Index_Type := Etype (Target_Index);
10378 Opnd_Index_Type := Etype (Opnd_Index);
10380 -- Error if index types are incompatible
10382 if not (Is_Integer_Type (Target_Index_Type)
10383 and then Is_Integer_Type (Opnd_Index_Type))
10384 and then (Root_Type (Target_Index_Type)
10385 /= Root_Type (Opnd_Index_Type))
10388 ("incompatible index types for array conversion",
10393 Next_Index (Target_Index);
10394 Next_Index (Opnd_Index);
10397 -- If component types have same base type, all set
10399 if Target_Comp_Base = Opnd_Comp_Base then
10402 -- Here if base types of components are not the same. The only
10403 -- time this is allowed is if we have anonymous access types.
10405 -- The conversion of arrays of anonymous access types can lead
10406 -- to dangling pointers. AI-392 formalizes the accessibility
10407 -- checks that must be applied to such conversions to prevent
10408 -- out-of-scope references.
10411 (Target_Comp_Base, E_Anonymous_Access_Type,
10412 E_Anonymous_Access_Subprogram_Type)
10413 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10415 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10417 if Type_Access_Level (Target_Type) <
10418 Deepest_Type_Access_Level (Opnd_Type)
10420 if In_Instance_Body then
10422 ("?source array type has " &
10423 "deeper accessibility level than target", Operand);
10425 ("\?Program_Error will be raised at run time",
10428 Make_Raise_Program_Error (Sloc (N),
10429 Reason => PE_Accessibility_Check_Failed));
10430 Set_Etype (N, Target_Type);
10433 -- Conversion not allowed because of accessibility levels
10437 ("source array type has " &
10438 "deeper accessibility level than target", Operand);
10446 -- All other cases where component base types do not match
10450 ("incompatible component types for array conversion",
10455 -- Check that component subtypes statically match. For numeric
10456 -- types this means that both must be either constrained or
10457 -- unconstrained. For enumeration types the bounds must match.
10458 -- All of this is checked in Subtypes_Statically_Match.
10460 if not Subtypes_Statically_Match
10461 (Target_Comp_Type, Opnd_Comp_Type)
10464 ("component subtypes must statically match", Operand);
10470 end Valid_Array_Conversion;
10472 -----------------------------
10473 -- Valid_Tagged_Conversion --
10474 -----------------------------
10476 function Valid_Tagged_Conversion
10477 (Target_Type : Entity_Id;
10478 Opnd_Type : Entity_Id) return Boolean
10481 -- Upward conversions are allowed (RM 4.6(22))
10483 if Covers (Target_Type, Opnd_Type)
10484 or else Is_Ancestor (Target_Type, Opnd_Type)
10488 -- Downward conversion are allowed if the operand is class-wide
10491 elsif Is_Class_Wide_Type (Opnd_Type)
10492 and then Covers (Opnd_Type, Target_Type)
10496 elsif Covers (Opnd_Type, Target_Type)
10497 or else Is_Ancestor (Opnd_Type, Target_Type)
10500 Conversion_Check (False,
10501 "downward conversion of tagged objects not allowed");
10503 -- Ada 2005 (AI-251): The conversion to/from interface types is
10506 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10509 -- If the operand is a class-wide type obtained through a limited_
10510 -- with clause, and the context includes the non-limited view, use
10511 -- it to determine whether the conversion is legal.
10513 elsif Is_Class_Wide_Type (Opnd_Type)
10514 and then From_With_Type (Opnd_Type)
10515 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10516 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10520 elsif Is_Access_Type (Opnd_Type)
10521 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10527 ("invalid tagged conversion, not compatible with}",
10528 N, First_Subtype (Opnd_Type));
10531 end Valid_Tagged_Conversion;
10533 -- Start of processing for Valid_Conversion
10536 Check_Parameterless_Call (Operand);
10538 if Is_Overloaded (Operand) then
10548 -- Remove procedure calls, which syntactically cannot appear in
10549 -- this context, but which cannot be removed by type checking,
10550 -- because the context does not impose a type.
10552 -- When compiling for VMS, spurious ambiguities can be produced
10553 -- when arithmetic operations have a literal operand and return
10554 -- System.Address or a descendant of it. These ambiguities are
10555 -- otherwise resolved by the context, but for conversions there
10556 -- is no context type and the removal of the spurious operations
10557 -- must be done explicitly here.
10559 -- The node may be labelled overloaded, but still contain only one
10560 -- interpretation because others were discarded earlier. If this
10561 -- is the case, retain the single interpretation if legal.
10563 Get_First_Interp (Operand, I, It);
10564 Opnd_Type := It.Typ;
10565 Get_Next_Interp (I, It);
10567 if Present (It.Typ)
10568 and then Opnd_Type /= Standard_Void_Type
10570 -- More than one candidate interpretation is available
10572 Get_First_Interp (Operand, I, It);
10573 while Present (It.Typ) loop
10574 if It.Typ = Standard_Void_Type then
10578 if Present (System_Aux_Id)
10579 and then Is_Descendent_Of_Address (It.Typ)
10584 Get_Next_Interp (I, It);
10588 Get_First_Interp (Operand, I, It);
10592 if No (It.Typ) then
10593 Error_Msg_N ("illegal operand in conversion", Operand);
10597 Get_Next_Interp (I, It);
10599 if Present (It.Typ) then
10602 It1 := Disambiguate (Operand, I1, I, Any_Type);
10604 if It1 = No_Interp then
10605 Error_Msg_N ("ambiguous operand in conversion", Operand);
10607 -- If the interpretation involves a standard operator, use
10608 -- the location of the type, which may be user-defined.
10610 if Sloc (It.Nam) = Standard_Location then
10611 Error_Msg_Sloc := Sloc (It.Typ);
10613 Error_Msg_Sloc := Sloc (It.Nam);
10616 Error_Msg_N -- CODEFIX
10617 ("\\possible interpretation#!", Operand);
10619 if Sloc (N1) = Standard_Location then
10620 Error_Msg_Sloc := Sloc (T1);
10622 Error_Msg_Sloc := Sloc (N1);
10625 Error_Msg_N -- CODEFIX
10626 ("\\possible interpretation#!", Operand);
10632 Set_Etype (Operand, It1.Typ);
10633 Opnd_Type := It1.Typ;
10639 if Is_Numeric_Type (Target_Type) then
10641 -- A universal fixed expression can be converted to any numeric type
10643 if Opnd_Type = Universal_Fixed then
10646 -- Also no need to check when in an instance or inlined body, because
10647 -- the legality has been established when the template was analyzed.
10648 -- Furthermore, numeric conversions may occur where only a private
10649 -- view of the operand type is visible at the instantiation point.
10650 -- This results in a spurious error if we check that the operand type
10651 -- is a numeric type.
10653 -- Note: in a previous version of this unit, the following tests were
10654 -- applied only for generated code (Comes_From_Source set to False),
10655 -- but in fact the test is required for source code as well, since
10656 -- this situation can arise in source code.
10658 elsif In_Instance or else In_Inlined_Body then
10661 -- Otherwise we need the conversion check
10664 return Conversion_Check
10665 (Is_Numeric_Type (Opnd_Type),
10666 "illegal operand for numeric conversion");
10671 elsif Is_Array_Type (Target_Type) then
10672 if not Is_Array_Type (Opnd_Type)
10673 or else Opnd_Type = Any_Composite
10674 or else Opnd_Type = Any_String
10676 Error_Msg_N ("illegal operand for array conversion", Operand);
10679 return Valid_Array_Conversion;
10682 -- Ada 2005 (AI-251): Anonymous access types where target references an
10685 elsif Ekind_In (Target_Type, E_General_Access_Type,
10686 E_Anonymous_Access_Type)
10687 and then Is_Interface (Directly_Designated_Type (Target_Type))
10689 -- Check the static accessibility rule of 4.6(17). Note that the
10690 -- check is not enforced when within an instance body, since the
10691 -- RM requires such cases to be caught at run time.
10693 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10694 if Type_Access_Level (Opnd_Type) >
10695 Deepest_Type_Access_Level (Target_Type)
10697 -- In an instance, this is a run-time check, but one we know
10698 -- will fail, so generate an appropriate warning. The raise
10699 -- will be generated by Expand_N_Type_Conversion.
10701 if In_Instance_Body then
10703 ("?cannot convert local pointer to non-local access type",
10706 ("\?Program_Error will be raised at run time", Operand);
10710 ("cannot convert local pointer to non-local access type",
10715 -- Special accessibility checks are needed in the case of access
10716 -- discriminants declared for a limited type.
10718 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10719 and then not Is_Local_Anonymous_Access (Opnd_Type)
10721 -- When the operand is a selected access discriminant the check
10722 -- needs to be made against the level of the object denoted by
10723 -- the prefix of the selected name (Object_Access_Level handles
10724 -- checking the prefix of the operand for this case).
10726 if Nkind (Operand) = N_Selected_Component
10727 and then Object_Access_Level (Operand) >
10728 Deepest_Type_Access_Level (Target_Type)
10730 -- In an instance, this is a run-time check, but one we know
10731 -- will fail, so generate an appropriate warning. The raise
10732 -- will be generated by Expand_N_Type_Conversion.
10734 if In_Instance_Body then
10736 ("?cannot convert access discriminant to non-local" &
10737 " access type", Operand);
10739 ("\?Program_Error will be raised at run time", Operand);
10742 ("cannot convert access discriminant to non-local" &
10743 " access type", Operand);
10748 -- The case of a reference to an access discriminant from
10749 -- within a limited type declaration (which will appear as
10750 -- a discriminal) is always illegal because the level of the
10751 -- discriminant is considered to be deeper than any (nameable)
10754 if Is_Entity_Name (Operand)
10755 and then not Is_Local_Anonymous_Access (Opnd_Type)
10757 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10758 and then Present (Discriminal_Link (Entity (Operand)))
10761 ("discriminant has deeper accessibility level than target",
10770 -- General and anonymous access types
10772 elsif Ekind_In (Target_Type, E_General_Access_Type,
10773 E_Anonymous_Access_Type)
10776 (Is_Access_Type (Opnd_Type)
10778 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10779 E_Access_Protected_Subprogram_Type),
10780 "must be an access-to-object type")
10782 if Is_Access_Constant (Opnd_Type)
10783 and then not Is_Access_Constant (Target_Type)
10786 ("access-to-constant operand type not allowed", Operand);
10790 -- Check the static accessibility rule of 4.6(17). Note that the
10791 -- check is not enforced when within an instance body, since the RM
10792 -- requires such cases to be caught at run time.
10794 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10795 or else Is_Local_Anonymous_Access (Target_Type)
10796 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
10797 N_Object_Declaration
10799 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
10800 -- conversions from an anonymous access type to a named general
10801 -- access type. Such conversions are not allowed in the case of
10802 -- access parameters and stand-alone objects of an anonymous
10803 -- access type. The implicit conversion case is recognized by
10804 -- testing that Comes_From_Source is False and that it's been
10805 -- rewritten. The Comes_From_Source test isn't sufficient because
10806 -- nodes in inlined calls to predefined library routines can have
10807 -- Comes_From_Source set to False. (Is there a better way to test
10808 -- for implicit conversions???)
10810 if Ada_Version >= Ada_2012
10811 and then not Comes_From_Source (N)
10812 and then N /= Original_Node (N)
10813 and then Ekind (Target_Type) = E_General_Access_Type
10814 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
10816 if Is_Itype (Opnd_Type) then
10818 -- Implicit conversions aren't allowed for objects of an
10819 -- anonymous access type, since such objects have nonstatic
10820 -- levels in Ada 2012.
10822 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
10823 N_Object_Declaration
10826 ("implicit conversion of stand-alone anonymous " &
10827 "access object not allowed", Operand);
10830 -- Implicit conversions aren't allowed for anonymous access
10831 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
10832 -- is done to exclude anonymous access results.
10834 elsif not Is_Local_Anonymous_Access (Opnd_Type)
10835 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
10836 N_Function_Specification,
10837 N_Procedure_Specification)
10840 ("implicit conversion of anonymous access formal " &
10841 "not allowed", Operand);
10844 -- This is a case where there's an enclosing object whose
10845 -- to which the "statically deeper than" relationship does
10846 -- not apply (such as an access discriminant selected from
10847 -- a dereference of an access parameter).
10849 elsif Object_Access_Level (Operand)
10850 = Scope_Depth (Standard_Standard)
10853 ("implicit conversion of anonymous access value " &
10854 "not allowed", Operand);
10857 -- In other cases, the level of the operand's type must be
10858 -- statically less deep than that of the target type, else
10859 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
10861 elsif Type_Access_Level (Opnd_Type) >
10862 Deepest_Type_Access_Level (Target_Type)
10865 ("implicit conversion of anonymous access value " &
10866 "violates accessibility", Operand);
10871 elsif Type_Access_Level (Opnd_Type) >
10872 Deepest_Type_Access_Level (Target_Type)
10874 -- In an instance, this is a run-time check, but one we know
10875 -- will fail, so generate an appropriate warning. The raise
10876 -- will be generated by Expand_N_Type_Conversion.
10878 if In_Instance_Body then
10880 ("?cannot convert local pointer to non-local access type",
10883 ("\?Program_Error will be raised at run time", Operand);
10886 -- Avoid generation of spurious error message
10888 if not Error_Posted (N) then
10890 ("cannot convert local pointer to non-local access type",
10897 -- Special accessibility checks are needed in the case of access
10898 -- discriminants declared for a limited type.
10900 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10901 and then not Is_Local_Anonymous_Access (Opnd_Type)
10903 -- When the operand is a selected access discriminant the check
10904 -- needs to be made against the level of the object denoted by
10905 -- the prefix of the selected name (Object_Access_Level handles
10906 -- checking the prefix of the operand for this case).
10908 if Nkind (Operand) = N_Selected_Component
10909 and then Object_Access_Level (Operand) >
10910 Deepest_Type_Access_Level (Target_Type)
10912 -- In an instance, this is a run-time check, but one we know
10913 -- will fail, so generate an appropriate warning. The raise
10914 -- will be generated by Expand_N_Type_Conversion.
10916 if In_Instance_Body then
10918 ("?cannot convert access discriminant to non-local" &
10919 " access type", Operand);
10921 ("\?Program_Error will be raised at run time",
10926 ("cannot convert access discriminant to non-local" &
10927 " access type", Operand);
10932 -- The case of a reference to an access discriminant from
10933 -- within a limited type declaration (which will appear as
10934 -- a discriminal) is always illegal because the level of the
10935 -- discriminant is considered to be deeper than any (nameable)
10938 if Is_Entity_Name (Operand)
10940 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10941 and then Present (Discriminal_Link (Entity (Operand)))
10944 ("discriminant has deeper accessibility level than target",
10951 -- In the presence of limited_with clauses we have to use non-limited
10952 -- views, if available.
10954 Check_Limited : declare
10955 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10956 -- Helper function to handle limited views
10958 --------------------------
10959 -- Full_Designated_Type --
10960 --------------------------
10962 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10963 Desig : constant Entity_Id := Designated_Type (T);
10966 -- Handle the limited view of a type
10968 if Is_Incomplete_Type (Desig)
10969 and then From_With_Type (Desig)
10970 and then Present (Non_Limited_View (Desig))
10972 return Available_View (Desig);
10976 end Full_Designated_Type;
10978 -- Local Declarations
10980 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10981 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10983 Same_Base : constant Boolean :=
10984 Base_Type (Target) = Base_Type (Opnd);
10986 -- Start of processing for Check_Limited
10989 if Is_Tagged_Type (Target) then
10990 return Valid_Tagged_Conversion (Target, Opnd);
10993 if not Same_Base then
10995 ("target designated type not compatible with }",
10996 N, Base_Type (Opnd));
10999 -- Ada 2005 AI-384: legality rule is symmetric in both
11000 -- designated types. The conversion is legal (with possible
11001 -- constraint check) if either designated type is
11004 elsif Subtypes_Statically_Match (Target, Opnd)
11006 (Has_Discriminants (Target)
11008 (not Is_Constrained (Opnd)
11009 or else not Is_Constrained (Target)))
11011 -- Special case, if Value_Size has been used to make the
11012 -- sizes different, the conversion is not allowed even
11013 -- though the subtypes statically match.
11015 if Known_Static_RM_Size (Target)
11016 and then Known_Static_RM_Size (Opnd)
11017 and then RM_Size (Target) /= RM_Size (Opnd)
11020 ("target designated subtype not compatible with }",
11023 ("\because sizes of the two designated subtypes differ",
11027 -- Normal case where conversion is allowed
11035 ("target designated subtype not compatible with }",
11042 -- Access to subprogram types. If the operand is an access parameter,
11043 -- the type has a deeper accessibility that any master, and cannot be
11044 -- assigned. We must make an exception if the conversion is part of an
11045 -- assignment and the target is the return object of an extended return
11046 -- statement, because in that case the accessibility check takes place
11047 -- after the return.
11049 elsif Is_Access_Subprogram_Type (Target_Type)
11050 and then No (Corresponding_Remote_Type (Opnd_Type))
11052 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
11053 and then Is_Entity_Name (Operand)
11054 and then Ekind (Entity (Operand)) = E_In_Parameter
11056 (Nkind (Parent (N)) /= N_Assignment_Statement
11057 or else not Is_Entity_Name (Name (Parent (N)))
11058 or else not Is_Return_Object (Entity (Name (Parent (N)))))
11061 ("illegal attempt to store anonymous access to subprogram",
11064 ("\value has deeper accessibility than any master " &
11065 "(RM 3.10.2 (13))",
11069 ("\use named access type for& instead of access parameter",
11070 Operand, Entity (Operand));
11073 -- Check that the designated types are subtype conformant
11075 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
11076 Old_Id => Designated_Type (Opnd_Type),
11079 -- Check the static accessibility rule of 4.6(20)
11081 if Type_Access_Level (Opnd_Type) >
11082 Deepest_Type_Access_Level (Target_Type)
11085 ("operand type has deeper accessibility level than target",
11088 -- Check that if the operand type is declared in a generic body,
11089 -- then the target type must be declared within that same body
11090 -- (enforces last sentence of 4.6(20)).
11092 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
11094 O_Gen : constant Node_Id :=
11095 Enclosing_Generic_Body (Opnd_Type);
11100 T_Gen := Enclosing_Generic_Body (Target_Type);
11101 while Present (T_Gen) and then T_Gen /= O_Gen loop
11102 T_Gen := Enclosing_Generic_Body (T_Gen);
11105 if T_Gen /= O_Gen then
11107 ("target type must be declared in same generic body"
11108 & " as operand type", N);
11115 -- Remote subprogram access types
11117 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
11118 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
11120 -- It is valid to convert from one RAS type to another provided
11121 -- that their specification statically match.
11123 Check_Subtype_Conformant
11125 Designated_Type (Corresponding_Remote_Type (Target_Type)),
11127 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
11132 -- If it was legal in the generic, it's legal in the instance
11134 elsif In_Instance_Body then
11137 -- If both are tagged types, check legality of view conversions
11139 elsif Is_Tagged_Type (Target_Type)
11141 Is_Tagged_Type (Opnd_Type)
11143 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
11145 -- Types derived from the same root type are convertible
11147 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
11150 -- In an instance or an inlined body, there may be inconsistent views of
11151 -- the same type, or of types derived from a common root.
11153 elsif (In_Instance or In_Inlined_Body)
11155 Root_Type (Underlying_Type (Target_Type)) =
11156 Root_Type (Underlying_Type (Opnd_Type))
11160 -- Special check for common access type error case
11162 elsif Ekind (Target_Type) = E_Access_Type
11163 and then Is_Access_Type (Opnd_Type)
11165 Error_Msg_N ("target type must be general access type!", N);
11166 Error_Msg_NE -- CODEFIX
11167 ("add ALL to }!", N, Target_Type);
11171 Error_Msg_NE ("invalid conversion, not compatible with }",
11175 end Valid_Conversion;