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);
1992 if Debug_Flag_V then
1993 Write_Overloads (N);
1996 if Comes_From_Source (N) then
1997 if Is_Fixed_Point_Type (Typ) then
1998 Check_Restriction (No_Fixed_Point, N);
2000 elsif Is_Floating_Point_Type (Typ)
2001 and then Typ /= Universal_Real
2002 and then Typ /= Any_Real
2004 Check_Restriction (No_Floating_Point, N);
2008 -- Return if already analyzed
2010 if Analyzed (N) then
2011 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2014 -- Return if type = Any_Type (previous error encountered)
2016 elsif Etype (N) = Any_Type then
2017 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2021 Check_Parameterless_Call (N);
2023 -- If not overloaded, then we know the type, and all that needs doing
2024 -- is to check that this type is compatible with the context.
2026 if not Is_Overloaded (N) then
2027 Found := Covers (Typ, Etype (N));
2028 Expr_Type := Etype (N);
2030 -- In the overloaded case, we must select the interpretation that
2031 -- is compatible with the context (i.e. the type passed to Resolve)
2034 -- Loop through possible interpretations
2036 Get_First_Interp (N, I, It);
2037 Interp_Loop : while Present (It.Typ) loop
2039 if Debug_Flag_V then
2040 Write_Str ("Interp: ");
2044 -- We are only interested in interpretations that are compatible
2045 -- with the expected type, any other interpretations are ignored.
2047 if not Covers (Typ, It.Typ) then
2048 if Debug_Flag_V then
2049 Write_Str (" interpretation incompatible with context");
2054 -- Skip the current interpretation if it is disabled by an
2055 -- abstract operator. This action is performed only when the
2056 -- type against which we are resolving is the same as the
2057 -- type of the interpretation.
2059 if Ada_Version >= Ada_2005
2060 and then It.Typ = Typ
2061 and then Typ /= Universal_Integer
2062 and then Typ /= Universal_Real
2063 and then Present (It.Abstract_Op)
2065 if Debug_Flag_V then
2066 Write_Line ("Skip.");
2072 -- First matching interpretation
2078 Expr_Type := It.Typ;
2080 -- Matching interpretation that is not the first, maybe an
2081 -- error, but there are some cases where preference rules are
2082 -- used to choose between the two possibilities. These and
2083 -- some more obscure cases are handled in Disambiguate.
2086 -- If the current statement is part of a predefined library
2087 -- unit, then all interpretations which come from user level
2088 -- packages should not be considered.
2091 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2096 Error_Msg_Sloc := Sloc (Seen);
2097 It1 := Disambiguate (N, I1, I, Typ);
2099 -- Disambiguation has succeeded. Skip the remaining
2102 if It1 /= No_Interp then
2104 Expr_Type := It1.Typ;
2106 while Present (It.Typ) loop
2107 Get_Next_Interp (I, It);
2111 -- Before we issue an ambiguity complaint, check for
2112 -- the case of a subprogram call where at least one
2113 -- of the arguments is Any_Type, and if so, suppress
2114 -- the message, since it is a cascaded error.
2116 if Nkind_In (N, N_Function_Call,
2117 N_Procedure_Call_Statement)
2124 A := First_Actual (N);
2125 while Present (A) loop
2128 if Nkind (E) = N_Parameter_Association then
2129 E := Explicit_Actual_Parameter (E);
2132 if Etype (E) = Any_Type then
2133 if Debug_Flag_V then
2134 Write_Str ("Any_Type in call");
2145 elsif Nkind (N) in N_Binary_Op
2146 and then (Etype (Left_Opnd (N)) = Any_Type
2147 or else Etype (Right_Opnd (N)) = Any_Type)
2151 elsif Nkind (N) in N_Unary_Op
2152 and then Etype (Right_Opnd (N)) = Any_Type
2157 -- Not that special case, so issue message using the
2158 -- flag Ambiguous to control printing of the header
2159 -- message only at the start of an ambiguous set.
2161 if not Ambiguous then
2162 if Nkind (N) = N_Function_Call
2163 and then Nkind (Name (N)) = N_Explicit_Dereference
2166 ("ambiguous expression "
2167 & "(cannot resolve indirect call)!", N);
2169 Error_Msg_NE -- CODEFIX
2170 ("ambiguous expression (cannot resolve&)!",
2176 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2178 ("\\possible interpretation (inherited)#!", N);
2180 Error_Msg_N -- CODEFIX
2181 ("\\possible interpretation#!", N);
2185 (N, N_Procedure_Call_Statement, N_Function_Call)
2186 and then Present (Parameter_Associations (N))
2188 Report_Ambiguous_Argument;
2192 Error_Msg_Sloc := Sloc (It.Nam);
2194 -- By default, the error message refers to the candidate
2195 -- interpretation. But if it is a predefined operator, it
2196 -- is implicitly declared at the declaration of the type
2197 -- of the operand. Recover the sloc of that declaration
2198 -- for the error message.
2200 if Nkind (N) in N_Op
2201 and then Scope (It.Nam) = Standard_Standard
2202 and then not Is_Overloaded (Right_Opnd (N))
2203 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2206 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2208 if Comes_From_Source (Err_Type)
2209 and then Present (Parent (Err_Type))
2211 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2214 elsif Nkind (N) in N_Binary_Op
2215 and then Scope (It.Nam) = Standard_Standard
2216 and then not Is_Overloaded (Left_Opnd (N))
2217 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2220 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2222 if Comes_From_Source (Err_Type)
2223 and then Present (Parent (Err_Type))
2225 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2228 -- If this is an indirect call, use the subprogram_type
2229 -- in the message, to have a meaningful location. Also
2230 -- indicate if this is an inherited operation, created
2231 -- by a type declaration.
2233 elsif Nkind (N) = N_Function_Call
2234 and then Nkind (Name (N)) = N_Explicit_Dereference
2235 and then Is_Type (It.Nam)
2239 Sloc (Associated_Node_For_Itype (Err_Type));
2244 if Nkind (N) in N_Op
2245 and then Scope (It.Nam) = Standard_Standard
2246 and then Present (Err_Type)
2248 -- Special-case the message for universal_fixed
2249 -- operators, which are not declared with the type
2250 -- of the operand, but appear forever in Standard.
2252 if It.Typ = Universal_Fixed
2253 and then Scope (It.Nam) = Standard_Standard
2256 ("\\possible interpretation as " &
2257 "universal_fixed operation " &
2258 "(RM 4.5.5 (19))", N);
2261 ("\\possible interpretation (predefined)#!", N);
2265 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2268 ("\\possible interpretation (inherited)#!", N);
2270 Error_Msg_N -- CODEFIX
2271 ("\\possible interpretation#!", N);
2277 -- We have a matching interpretation, Expr_Type is the type
2278 -- from this interpretation, and Seen is the entity.
2280 -- For an operator, just set the entity name. The type will be
2281 -- set by the specific operator resolution routine.
2283 if Nkind (N) in N_Op then
2284 Set_Entity (N, Seen);
2285 Generate_Reference (Seen, N);
2287 elsif Nkind (N) = N_Case_Expression then
2288 Set_Etype (N, Expr_Type);
2290 elsif Nkind (N) = N_Character_Literal then
2291 Set_Etype (N, Expr_Type);
2293 elsif Nkind (N) = N_Conditional_Expression then
2294 Set_Etype (N, Expr_Type);
2296 -- AI05-0139-2: Expression is overloaded because type has
2297 -- implicit dereference. If type matches context, no implicit
2298 -- dereference is involved.
2300 elsif Has_Implicit_Dereference (Expr_Type) then
2301 Set_Etype (N, Expr_Type);
2302 Set_Is_Overloaded (N, False);
2305 elsif Is_Overloaded (N)
2306 and then Present (It.Nam)
2307 and then Ekind (It.Nam) = E_Discriminant
2308 and then Has_Implicit_Dereference (It.Nam)
2310 Build_Explicit_Dereference (N, It.Nam);
2312 -- For an explicit dereference, attribute reference, range,
2313 -- short-circuit form (which is not an operator node), or call
2314 -- with a name that is an explicit dereference, there is
2315 -- nothing to be done at this point.
2317 elsif Nkind_In (N, N_Explicit_Dereference,
2318 N_Attribute_Reference,
2320 N_Indexed_Component,
2323 N_Selected_Component,
2325 or else Nkind (Name (N)) = N_Explicit_Dereference
2329 -- For procedure or function calls, set the type of the name,
2330 -- and also the entity pointer for the prefix.
2332 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2333 and then Is_Entity_Name (Name (N))
2335 Set_Etype (Name (N), Expr_Type);
2336 Set_Entity (Name (N), Seen);
2337 Generate_Reference (Seen, Name (N));
2339 elsif Nkind (N) = N_Function_Call
2340 and then Nkind (Name (N)) = N_Selected_Component
2342 Set_Etype (Name (N), Expr_Type);
2343 Set_Entity (Selector_Name (Name (N)), Seen);
2344 Generate_Reference (Seen, Selector_Name (Name (N)));
2346 -- For all other cases, just set the type of the Name
2349 Set_Etype (Name (N), Expr_Type);
2356 -- Move to next interpretation
2358 exit Interp_Loop when No (It.Typ);
2360 Get_Next_Interp (I, It);
2361 end loop Interp_Loop;
2364 -- At this stage Found indicates whether or not an acceptable
2365 -- interpretation exists. If not, then we have an error, except that if
2366 -- the context is Any_Type as a result of some other error, then we
2367 -- suppress the error report.
2370 if Typ /= Any_Type then
2372 -- If type we are looking for is Void, then this is the procedure
2373 -- call case, and the error is simply that what we gave is not a
2374 -- procedure name (we think of procedure calls as expressions with
2375 -- types internally, but the user doesn't think of them this way!)
2377 if Typ = Standard_Void_Type then
2379 -- Special case message if function used as a procedure
2381 if Nkind (N) = N_Procedure_Call_Statement
2382 and then Is_Entity_Name (Name (N))
2383 and then Ekind (Entity (Name (N))) = E_Function
2386 ("cannot use function & in a procedure call",
2387 Name (N), Entity (Name (N)));
2389 -- Otherwise give general message (not clear what cases this
2390 -- covers, but no harm in providing for them!)
2393 Error_Msg_N ("expect procedure name in procedure call", N);
2398 -- Otherwise we do have a subexpression with the wrong type
2400 -- Check for the case of an allocator which uses an access type
2401 -- instead of the designated type. This is a common error and we
2402 -- specialize the message, posting an error on the operand of the
2403 -- allocator, complaining that we expected the designated type of
2406 elsif Nkind (N) = N_Allocator
2407 and then Ekind (Typ) in Access_Kind
2408 and then Ekind (Etype (N)) in Access_Kind
2409 and then Designated_Type (Etype (N)) = Typ
2411 Wrong_Type (Expression (N), Designated_Type (Typ));
2414 -- Check for view mismatch on Null in instances, for which the
2415 -- view-swapping mechanism has no identifier.
2417 elsif (In_Instance or else In_Inlined_Body)
2418 and then (Nkind (N) = N_Null)
2419 and then Is_Private_Type (Typ)
2420 and then Is_Access_Type (Full_View (Typ))
2422 Resolve (N, Full_View (Typ));
2426 -- Check for an aggregate. Sometimes we can get bogus aggregates
2427 -- from misuse of parentheses, and we are about to complain about
2428 -- the aggregate without even looking inside it.
2430 -- Instead, if we have an aggregate of type Any_Composite, then
2431 -- analyze and resolve the component fields, and then only issue
2432 -- another message if we get no errors doing this (otherwise
2433 -- assume that the errors in the aggregate caused the problem).
2435 elsif Nkind (N) = N_Aggregate
2436 and then Etype (N) = Any_Composite
2438 -- Disable expansion in any case. If there is a type mismatch
2439 -- it may be fatal to try to expand the aggregate. The flag
2440 -- would otherwise be set to false when the error is posted.
2442 Expander_Active := False;
2445 procedure Check_Aggr (Aggr : Node_Id);
2446 -- Check one aggregate, and set Found to True if we have a
2447 -- definite error in any of its elements
2449 procedure Check_Elmt (Aelmt : Node_Id);
2450 -- Check one element of aggregate and set Found to True if
2451 -- we definitely have an error in the element.
2457 procedure Check_Aggr (Aggr : Node_Id) is
2461 if Present (Expressions (Aggr)) then
2462 Elmt := First (Expressions (Aggr));
2463 while Present (Elmt) loop
2469 if Present (Component_Associations (Aggr)) then
2470 Elmt := First (Component_Associations (Aggr));
2471 while Present (Elmt) loop
2473 -- If this is a default-initialized component, then
2474 -- there is nothing to check. The box will be
2475 -- replaced by the appropriate call during late
2478 if not Box_Present (Elmt) then
2479 Check_Elmt (Expression (Elmt));
2491 procedure Check_Elmt (Aelmt : Node_Id) is
2493 -- If we have a nested aggregate, go inside it (to
2494 -- attempt a naked analyze-resolve of the aggregate can
2495 -- cause undesirable cascaded errors). Do not resolve
2496 -- expression if it needs a type from context, as for
2497 -- integer * fixed expression.
2499 if Nkind (Aelmt) = N_Aggregate then
2505 if not Is_Overloaded (Aelmt)
2506 and then Etype (Aelmt) /= Any_Fixed
2511 if Etype (Aelmt) = Any_Type then
2522 -- If an error message was issued already, Found got reset to
2523 -- True, so if it is still False, issue standard Wrong_Type msg.
2526 if Is_Overloaded (N)
2527 and then Nkind (N) = N_Function_Call
2530 Subp_Name : Node_Id;
2532 if Is_Entity_Name (Name (N)) then
2533 Subp_Name := Name (N);
2535 elsif Nkind (Name (N)) = N_Selected_Component then
2537 -- Protected operation: retrieve operation name
2539 Subp_Name := Selector_Name (Name (N));
2542 raise Program_Error;
2545 Error_Msg_Node_2 := Typ;
2546 Error_Msg_NE ("no visible interpretation of&" &
2547 " matches expected type&", N, Subp_Name);
2550 if All_Errors_Mode then
2552 Index : Interp_Index;
2556 Error_Msg_N ("\\possible interpretations:", N);
2558 Get_First_Interp (Name (N), Index, It);
2559 while Present (It.Nam) loop
2560 Error_Msg_Sloc := Sloc (It.Nam);
2561 Error_Msg_Node_2 := It.Nam;
2563 ("\\ type& for & declared#", N, It.Typ);
2564 Get_Next_Interp (Index, It);
2569 Error_Msg_N ("\use -gnatf for details", N);
2573 Wrong_Type (N, Typ);
2581 -- Test if we have more than one interpretation for the context
2583 elsif Ambiguous then
2588 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2589 -- the "+" on T is abstract, and the operands are of universal type,
2590 -- the above code will have (incorrectly) resolved the "+" to the
2591 -- universal one in Standard. Therefore, we check for this case, and
2592 -- give an error. We can't do this earlier, because it would cause
2593 -- legal cases to get errors (when some other type has an abstract
2596 if Ada_Version >= Ada_2005 and then
2597 Nkind (N) in N_Op and then
2598 Is_Overloaded (N) and then
2599 Is_Universal_Numeric_Type (Etype (Entity (N)))
2601 Get_First_Interp (N, I, It);
2602 while Present (It.Typ) loop
2603 if Present (It.Abstract_Op) and then
2604 Etype (It.Abstract_Op) = Typ
2607 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2611 Get_Next_Interp (I, It);
2615 -- Here we have an acceptable interpretation for the context
2617 -- Propagate type information and normalize tree for various
2618 -- predefined operations. If the context only imposes a class of
2619 -- types, rather than a specific type, propagate the actual type
2622 if Typ = Any_Integer or else
2623 Typ = Any_Boolean or else
2624 Typ = Any_Modular or else
2625 Typ = Any_Real or else
2628 Ctx_Type := Expr_Type;
2630 -- Any_Fixed is legal in a real context only if a specific fixed-
2631 -- point type is imposed. If Norman Cohen can be confused by this,
2632 -- it deserves a separate message.
2635 and then Expr_Type = Any_Fixed
2637 Error_Msg_N ("illegal context for mixed mode operation", N);
2638 Set_Etype (N, Universal_Real);
2639 Ctx_Type := Universal_Real;
2643 -- A user-defined operator is transformed into a function call at
2644 -- this point, so that further processing knows that operators are
2645 -- really operators (i.e. are predefined operators). User-defined
2646 -- operators that are intrinsic are just renamings of the predefined
2647 -- ones, and need not be turned into calls either, but if they rename
2648 -- a different operator, we must transform the node accordingly.
2649 -- Instantiations of Unchecked_Conversion are intrinsic but are
2650 -- treated as functions, even if given an operator designator.
2652 if Nkind (N) in N_Op
2653 and then Present (Entity (N))
2654 and then Ekind (Entity (N)) /= E_Operator
2657 if not Is_Predefined_Op (Entity (N)) then
2658 Rewrite_Operator_As_Call (N, Entity (N));
2660 elsif Present (Alias (Entity (N)))
2662 Nkind (Parent (Parent (Entity (N)))) =
2663 N_Subprogram_Renaming_Declaration
2665 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2667 -- If the node is rewritten, it will be fully resolved in
2668 -- Rewrite_Renamed_Operator.
2670 if Analyzed (N) then
2676 case N_Subexpr'(Nkind (N)) is
2678 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2680 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2682 when N_Short_Circuit
2683 => Resolve_Short_Circuit (N, Ctx_Type);
2685 when N_Attribute_Reference
2686 => Resolve_Attribute (N, Ctx_Type);
2688 when N_Case_Expression
2689 => Resolve_Case_Expression (N, Ctx_Type);
2691 when N_Character_Literal
2692 => Resolve_Character_Literal (N, Ctx_Type);
2694 when N_Conditional_Expression
2695 => Resolve_Conditional_Expression (N, Ctx_Type);
2697 when N_Expanded_Name
2698 => Resolve_Entity_Name (N, Ctx_Type);
2700 when N_Explicit_Dereference
2701 => Resolve_Explicit_Dereference (N, Ctx_Type);
2703 when N_Expression_With_Actions
2704 => Resolve_Expression_With_Actions (N, Ctx_Type);
2706 when N_Extension_Aggregate
2707 => Resolve_Extension_Aggregate (N, Ctx_Type);
2709 when N_Function_Call
2710 => Resolve_Call (N, Ctx_Type);
2713 => Resolve_Entity_Name (N, Ctx_Type);
2715 when N_Indexed_Component
2716 => Resolve_Indexed_Component (N, Ctx_Type);
2718 when N_Integer_Literal
2719 => Resolve_Integer_Literal (N, Ctx_Type);
2721 when N_Membership_Test
2722 => Resolve_Membership_Op (N, Ctx_Type);
2724 when N_Null => Resolve_Null (N, Ctx_Type);
2726 when N_Op_And | N_Op_Or | N_Op_Xor
2727 => Resolve_Logical_Op (N, Ctx_Type);
2729 when N_Op_Eq | N_Op_Ne
2730 => Resolve_Equality_Op (N, Ctx_Type);
2732 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2733 => Resolve_Comparison_Op (N, Ctx_Type);
2735 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2737 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2738 N_Op_Divide | N_Op_Mod | N_Op_Rem
2740 => Resolve_Arithmetic_Op (N, Ctx_Type);
2742 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2744 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2746 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2747 => Resolve_Unary_Op (N, Ctx_Type);
2749 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2751 when N_Procedure_Call_Statement
2752 => Resolve_Call (N, Ctx_Type);
2754 when N_Operator_Symbol
2755 => Resolve_Operator_Symbol (N, Ctx_Type);
2757 when N_Qualified_Expression
2758 => Resolve_Qualified_Expression (N, Ctx_Type);
2760 when N_Quantified_Expression
2761 => Resolve_Quantified_Expression (N, Ctx_Type);
2763 when N_Raise_xxx_Error
2764 => Set_Etype (N, Ctx_Type);
2766 when N_Range => Resolve_Range (N, Ctx_Type);
2769 => Resolve_Real_Literal (N, Ctx_Type);
2771 when N_Reference => Resolve_Reference (N, Ctx_Type);
2773 when N_Selected_Component
2774 => Resolve_Selected_Component (N, Ctx_Type);
2776 when N_Slice => Resolve_Slice (N, Ctx_Type);
2778 when N_String_Literal
2779 => Resolve_String_Literal (N, Ctx_Type);
2781 when N_Subprogram_Info
2782 => Resolve_Subprogram_Info (N, Ctx_Type);
2784 when N_Type_Conversion
2785 => Resolve_Type_Conversion (N, Ctx_Type);
2787 when N_Unchecked_Expression =>
2788 Resolve_Unchecked_Expression (N, Ctx_Type);
2790 when N_Unchecked_Type_Conversion =>
2791 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2794 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2795 -- expression of an anonymous access type that occurs in the context
2796 -- of a named general access type, except when the expression is that
2797 -- of a membership test. This ensures proper legality checking in
2798 -- terms of allowed conversions (expressions that would be illegal to
2799 -- convert implicitly are allowed in membership tests).
2801 if Ada_Version >= Ada_2012
2802 and then Ekind (Ctx_Type) = E_General_Access_Type
2803 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2804 and then Nkind (Parent (N)) not in N_Membership_Test
2806 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2807 Analyze_And_Resolve (N, Ctx_Type);
2810 -- If the subexpression was replaced by a non-subexpression, then
2811 -- all we do is to expand it. The only legitimate case we know of
2812 -- is converting procedure call statement to entry call statements,
2813 -- but there may be others, so we are making this test general.
2815 if Nkind (N) not in N_Subexpr then
2816 Debug_A_Exit ("resolving ", N, " (done)");
2821 -- AI05-144-2: Check dangerous order dependence within an expression
2822 -- that is not a subexpression. Exclude RHS of an assignment, because
2823 -- both sides may have side-effects and the check must be performed
2824 -- over the statement.
2826 if Nkind (Parent (N)) not in N_Subexpr
2827 and then Nkind (Parent (N)) /= N_Assignment_Statement
2828 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2830 Check_Order_Dependence;
2833 -- The expression is definitely NOT overloaded at this point, so
2834 -- we reset the Is_Overloaded flag to avoid any confusion when
2835 -- reanalyzing the node.
2837 Set_Is_Overloaded (N, False);
2839 -- Freeze expression type, entity if it is a name, and designated
2840 -- type if it is an allocator (RM 13.14(10,11,13)).
2842 -- Now that the resolution of the type of the node is complete, and
2843 -- we did not detect an error, we can expand this node. We skip the
2844 -- expand call if we are in a default expression, see section
2845 -- "Handling of Default Expressions" in Sem spec.
2847 Debug_A_Exit ("resolving ", N, " (done)");
2849 -- We unconditionally freeze the expression, even if we are in
2850 -- default expression mode (the Freeze_Expression routine tests this
2851 -- flag and only freezes static types if it is set).
2853 -- Ada 2012 (AI05-177): Expression functions do not freeze. Only
2854 -- their use (in an expanded call) freezes.
2856 if Ekind (Current_Scope) /= E_Function
2858 Nkind (Original_Node (Unit_Declaration_Node (Current_Scope))) /=
2859 N_Expression_Function
2861 Freeze_Expression (N);
2864 -- Now we can do the expansion
2874 -- Version with check(s) suppressed
2876 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2878 if Suppress = All_Checks then
2880 Svg : constant Suppress_Array := Scope_Suppress;
2882 Scope_Suppress := (others => True);
2884 Scope_Suppress := Svg;
2889 Svg : constant Boolean := Scope_Suppress (Suppress);
2891 Scope_Suppress (Suppress) := True;
2893 Scope_Suppress (Suppress) := Svg;
2902 -- Version with implicit type
2904 procedure Resolve (N : Node_Id) is
2906 Resolve (N, Etype (N));
2909 ---------------------
2910 -- Resolve_Actuals --
2911 ---------------------
2913 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2914 Loc : constant Source_Ptr := Sloc (N);
2919 Prev : Node_Id := Empty;
2922 procedure Check_Argument_Order;
2923 -- Performs a check for the case where the actuals are all simple
2924 -- identifiers that correspond to the formal names, but in the wrong
2925 -- order, which is considered suspicious and cause for a warning.
2927 procedure Check_Prefixed_Call;
2928 -- If the original node is an overloaded call in prefix notation,
2929 -- insert an 'Access or a dereference as needed over the first actual.
2930 -- Try_Object_Operation has already verified that there is a valid
2931 -- interpretation, but the form of the actual can only be determined
2932 -- once the primitive operation is identified.
2934 procedure Insert_Default;
2935 -- If the actual is missing in a call, insert in the actuals list
2936 -- an instance of the default expression. The insertion is always
2937 -- a named association.
2939 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2940 -- Check whether T1 and T2, or their full views, are derived from a
2941 -- common type. Used to enforce the restrictions on array conversions
2944 function Static_Concatenation (N : Node_Id) return Boolean;
2945 -- Predicate to determine whether an actual that is a concatenation
2946 -- will be evaluated statically and does not need a transient scope.
2947 -- This must be determined before the actual is resolved and expanded
2948 -- because if needed the transient scope must be introduced earlier.
2950 --------------------------
2951 -- Check_Argument_Order --
2952 --------------------------
2954 procedure Check_Argument_Order is
2956 -- Nothing to do if no parameters, or original node is neither a
2957 -- function call nor a procedure call statement (happens in the
2958 -- operator-transformed-to-function call case), or the call does
2959 -- not come from source, or this warning is off.
2961 if not Warn_On_Parameter_Order
2962 or else No (Parameter_Associations (N))
2963 or else not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2965 or else not Comes_From_Source (N)
2971 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2974 -- Nothing to do if only one parameter
2980 -- Here if at least two arguments
2983 Actuals : array (1 .. Nargs) of Node_Id;
2987 Wrong_Order : Boolean := False;
2988 -- Set True if an out of order case is found
2991 -- Collect identifier names of actuals, fail if any actual is
2992 -- not a simple identifier, and record max length of name.
2994 Actual := First (Parameter_Associations (N));
2995 for J in Actuals'Range loop
2996 if Nkind (Actual) /= N_Identifier then
2999 Actuals (J) := Actual;
3004 -- If we got this far, all actuals are identifiers and the list
3005 -- of their names is stored in the Actuals array.
3007 Formal := First_Formal (Nam);
3008 for J in Actuals'Range loop
3010 -- If we ran out of formals, that's odd, probably an error
3011 -- which will be detected elsewhere, but abandon the search.
3017 -- If name matches and is in order OK
3019 if Chars (Formal) = Chars (Actuals (J)) then
3023 -- If no match, see if it is elsewhere in list and if so
3024 -- flag potential wrong order if type is compatible.
3026 for K in Actuals'Range loop
3027 if Chars (Formal) = Chars (Actuals (K))
3029 Has_Compatible_Type (Actuals (K), Etype (Formal))
3031 Wrong_Order := True;
3041 <<Continue>> Next_Formal (Formal);
3044 -- If Formals left over, also probably an error, skip warning
3046 if Present (Formal) then
3050 -- Here we give the warning if something was out of order
3054 ("actuals for this call may be in wrong order?", N);
3058 end Check_Argument_Order;
3060 -------------------------
3061 -- Check_Prefixed_Call --
3062 -------------------------
3064 procedure Check_Prefixed_Call is
3065 Act : constant Node_Id := First_Actual (N);
3066 A_Type : constant Entity_Id := Etype (Act);
3067 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3068 Orig : constant Node_Id := Original_Node (N);
3072 -- Check whether the call is a prefixed call, with or without
3073 -- additional actuals.
3075 if Nkind (Orig) = N_Selected_Component
3077 (Nkind (Orig) = N_Indexed_Component
3078 and then Nkind (Prefix (Orig)) = N_Selected_Component
3079 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3080 and then Is_Entity_Name (Act)
3081 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3083 if Is_Access_Type (A_Type)
3084 and then not Is_Access_Type (F_Type)
3086 -- Introduce dereference on object in prefix
3089 Make_Explicit_Dereference (Sloc (Act),
3090 Prefix => Relocate_Node (Act));
3091 Rewrite (Act, New_A);
3094 elsif Is_Access_Type (F_Type)
3095 and then not Is_Access_Type (A_Type)
3097 -- Introduce an implicit 'Access in prefix
3099 if not Is_Aliased_View (Act) then
3101 ("object in prefixed call to& must be aliased"
3102 & " (RM-2005 4.3.1 (13))",
3107 Make_Attribute_Reference (Loc,
3108 Attribute_Name => Name_Access,
3109 Prefix => Relocate_Node (Act)));
3114 end Check_Prefixed_Call;
3116 --------------------
3117 -- Insert_Default --
3118 --------------------
3120 procedure Insert_Default is
3125 -- Missing argument in call, nothing to insert
3127 if No (Default_Value (F)) then
3131 -- Note that we do a full New_Copy_Tree, so that any associated
3132 -- Itypes are properly copied. This may not be needed any more,
3133 -- but it does no harm as a safety measure! Defaults of a generic
3134 -- formal may be out of bounds of the corresponding actual (see
3135 -- cc1311b) and an additional check may be required.
3140 New_Scope => Current_Scope,
3143 if Is_Concurrent_Type (Scope (Nam))
3144 and then Has_Discriminants (Scope (Nam))
3146 Replace_Actual_Discriminants (N, Actval);
3149 if Is_Overloadable (Nam)
3150 and then Present (Alias (Nam))
3152 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3153 and then not Is_Tagged_Type (Etype (F))
3155 -- If default is a real literal, do not introduce a
3156 -- conversion whose effect may depend on the run-time
3157 -- size of universal real.
3159 if Nkind (Actval) = N_Real_Literal then
3160 Set_Etype (Actval, Base_Type (Etype (F)));
3162 Actval := Unchecked_Convert_To (Etype (F), Actval);
3166 if Is_Scalar_Type (Etype (F)) then
3167 Enable_Range_Check (Actval);
3170 Set_Parent (Actval, N);
3172 -- Resolve aggregates with their base type, to avoid scope
3173 -- anomalies: the subtype was first built in the subprogram
3174 -- declaration, and the current call may be nested.
3176 if Nkind (Actval) = N_Aggregate then
3177 Analyze_And_Resolve (Actval, Etype (F));
3179 Analyze_And_Resolve (Actval, Etype (Actval));
3183 Set_Parent (Actval, N);
3185 -- See note above concerning aggregates
3187 if Nkind (Actval) = N_Aggregate
3188 and then Has_Discriminants (Etype (Actval))
3190 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3192 -- Resolve entities with their own type, which may differ from
3193 -- the type of a reference in a generic context (the view
3194 -- swapping mechanism did not anticipate the re-analysis of
3195 -- default values in calls).
3197 elsif Is_Entity_Name (Actval) then
3198 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3201 Analyze_And_Resolve (Actval, Etype (Actval));
3205 -- If default is a tag indeterminate function call, propagate tag
3206 -- to obtain proper dispatching.
3208 if Is_Controlling_Formal (F)
3209 and then Nkind (Default_Value (F)) = N_Function_Call
3211 Set_Is_Controlling_Actual (Actval);
3216 -- If the default expression raises constraint error, then just
3217 -- silently replace it with an N_Raise_Constraint_Error node, since
3218 -- we already gave the warning on the subprogram spec. If node is
3219 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3220 -- the warnings removal machinery.
3222 if Raises_Constraint_Error (Actval)
3223 and then Nkind (Actval) /= N_Raise_Constraint_Error
3226 Make_Raise_Constraint_Error (Loc,
3227 Reason => CE_Range_Check_Failed));
3228 Set_Raises_Constraint_Error (Actval);
3229 Set_Etype (Actval, Etype (F));
3233 Make_Parameter_Association (Loc,
3234 Explicit_Actual_Parameter => Actval,
3235 Selector_Name => Make_Identifier (Loc, Chars (F)));
3237 -- Case of insertion is first named actual
3239 if No (Prev) or else
3240 Nkind (Parent (Prev)) /= N_Parameter_Association
3242 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3243 Set_First_Named_Actual (N, Actval);
3246 if No (Parameter_Associations (N)) then
3247 Set_Parameter_Associations (N, New_List (Assoc));
3249 Append (Assoc, Parameter_Associations (N));
3253 Insert_After (Prev, Assoc);
3256 -- Case of insertion is not first named actual
3259 Set_Next_Named_Actual
3260 (Assoc, Next_Named_Actual (Parent (Prev)));
3261 Set_Next_Named_Actual (Parent (Prev), Actval);
3262 Append (Assoc, Parameter_Associations (N));
3265 Mark_Rewrite_Insertion (Assoc);
3266 Mark_Rewrite_Insertion (Actval);
3275 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3276 FT1 : Entity_Id := T1;
3277 FT2 : Entity_Id := T2;
3280 if Is_Private_Type (T1)
3281 and then Present (Full_View (T1))
3283 FT1 := Full_View (T1);
3286 if Is_Private_Type (T2)
3287 and then Present (Full_View (T2))
3289 FT2 := Full_View (T2);
3292 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3295 --------------------------
3296 -- Static_Concatenation --
3297 --------------------------
3299 function Static_Concatenation (N : Node_Id) return Boolean is
3302 when N_String_Literal =>
3307 -- Concatenation is static when both operands are static and
3308 -- the concatenation operator is a predefined one.
3310 return Scope (Entity (N)) = Standard_Standard
3312 Static_Concatenation (Left_Opnd (N))
3314 Static_Concatenation (Right_Opnd (N));
3317 if Is_Entity_Name (N) then
3319 Ent : constant Entity_Id := Entity (N);
3321 return Ekind (Ent) = E_Constant
3322 and then Present (Constant_Value (Ent))
3324 Is_Static_Expression (Constant_Value (Ent));
3331 end Static_Concatenation;
3333 -- Start of processing for Resolve_Actuals
3336 Check_Argument_Order;
3338 if Present (First_Actual (N)) then
3339 Check_Prefixed_Call;
3342 A := First_Actual (N);
3343 F := First_Formal (Nam);
3344 while Present (F) loop
3345 if No (A) and then Needs_No_Actuals (Nam) then
3348 -- If we have an error in any actual or formal, indicated by a type
3349 -- of Any_Type, then abandon resolution attempt, and set result type
3352 elsif (Present (A) and then Etype (A) = Any_Type)
3353 or else Etype (F) = Any_Type
3355 Set_Etype (N, Any_Type);
3359 -- Case where actual is present
3361 -- If the actual is an entity, generate a reference to it now. We
3362 -- do this before the actual is resolved, because a formal of some
3363 -- protected subprogram, or a task discriminant, will be rewritten
3364 -- during expansion, and the source entity reference may be lost.
3367 and then Is_Entity_Name (A)
3368 and then Comes_From_Source (N)
3370 Orig_A := Entity (A);
3372 if Present (Orig_A) then
3373 if Is_Formal (Orig_A)
3374 and then Ekind (F) /= E_In_Parameter
3376 Generate_Reference (Orig_A, A, 'm');
3378 elsif not Is_Overloaded (A) then
3379 Generate_Reference (Orig_A, A);
3385 and then (Nkind (Parent (A)) /= N_Parameter_Association
3386 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3388 -- If style checking mode on, check match of formal name
3391 if Nkind (Parent (A)) = N_Parameter_Association then
3392 Check_Identifier (Selector_Name (Parent (A)), F);
3396 -- If the formal is Out or In_Out, do not resolve and expand the
3397 -- conversion, because it is subsequently expanded into explicit
3398 -- temporaries and assignments. However, the object of the
3399 -- conversion can be resolved. An exception is the case of tagged
3400 -- type conversion with a class-wide actual. In that case we want
3401 -- the tag check to occur and no temporary will be needed (no
3402 -- representation change can occur) and the parameter is passed by
3403 -- reference, so we go ahead and resolve the type conversion.
3404 -- Another exception is the case of reference to component or
3405 -- subcomponent of a bit-packed array, in which case we want to
3406 -- defer expansion to the point the in and out assignments are
3409 if Ekind (F) /= E_In_Parameter
3410 and then Nkind (A) = N_Type_Conversion
3411 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3413 if Ekind (F) = E_In_Out_Parameter
3414 and then Is_Array_Type (Etype (F))
3416 -- In a view conversion, the conversion must be legal in
3417 -- both directions, and thus both component types must be
3418 -- aliased, or neither (4.6 (8)).
3420 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3421 -- the privacy requirement should not apply to generic
3422 -- types, and should be checked in an instance. ARG query
3425 if Has_Aliased_Components (Etype (Expression (A))) /=
3426 Has_Aliased_Components (Etype (F))
3429 ("both component types in a view conversion must be"
3430 & " aliased, or neither", A);
3432 -- Comment here??? what set of cases???
3435 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3437 -- Check view conv between unrelated by ref array types
3439 if Is_By_Reference_Type (Etype (F))
3440 or else Is_By_Reference_Type (Etype (Expression (A)))
3443 ("view conversion between unrelated by reference " &
3444 "array types not allowed (\'A'I-00246)", A);
3446 -- In Ada 2005 mode, check view conversion component
3447 -- type cannot be private, tagged, or volatile. Note
3448 -- that we only apply this to source conversions. The
3449 -- generated code can contain conversions which are
3450 -- not subject to this test, and we cannot extract the
3451 -- component type in such cases since it is not present.
3453 elsif Comes_From_Source (A)
3454 and then Ada_Version >= Ada_2005
3457 Comp_Type : constant Entity_Id :=
3459 (Etype (Expression (A)));
3461 if (Is_Private_Type (Comp_Type)
3462 and then not Is_Generic_Type (Comp_Type))
3463 or else Is_Tagged_Type (Comp_Type)
3464 or else Is_Volatile (Comp_Type)
3467 ("component type of a view conversion cannot"
3468 & " be private, tagged, or volatile"
3477 -- Resolve expression if conversion is all OK
3479 if (Conversion_OK (A)
3480 or else Valid_Conversion (A, Etype (A), Expression (A)))
3481 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3483 Resolve (Expression (A));
3486 -- If the actual is a function call that returns a limited
3487 -- unconstrained object that needs finalization, create a
3488 -- transient scope for it, so that it can receive the proper
3489 -- finalization list.
3491 elsif Nkind (A) = N_Function_Call
3492 and then Is_Limited_Record (Etype (F))
3493 and then not Is_Constrained (Etype (F))
3494 and then Full_Expander_Active
3495 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3497 Establish_Transient_Scope (A, False);
3498 Resolve (A, Etype (F));
3500 -- A small optimization: if one of the actuals is a concatenation
3501 -- create a block around a procedure call to recover stack space.
3502 -- This alleviates stack usage when several procedure calls in
3503 -- the same statement list use concatenation. We do not perform
3504 -- this wrapping for code statements, where the argument is a
3505 -- static string, and we want to preserve warnings involving
3506 -- sequences of such statements.
3508 elsif Nkind (A) = N_Op_Concat
3509 and then Nkind (N) = N_Procedure_Call_Statement
3510 and then Full_Expander_Active
3512 not (Is_Intrinsic_Subprogram (Nam)
3513 and then Chars (Nam) = Name_Asm)
3514 and then not Static_Concatenation (A)
3516 Establish_Transient_Scope (A, False);
3517 Resolve (A, Etype (F));
3520 if Nkind (A) = N_Type_Conversion
3521 and then Is_Array_Type (Etype (F))
3522 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3524 (Is_Limited_Type (Etype (F))
3525 or else Is_Limited_Type (Etype (Expression (A))))
3528 ("conversion between unrelated limited array types " &
3529 "not allowed (\A\I-00246)", A);
3531 if Is_Limited_Type (Etype (F)) then
3532 Explain_Limited_Type (Etype (F), A);
3535 if Is_Limited_Type (Etype (Expression (A))) then
3536 Explain_Limited_Type (Etype (Expression (A)), A);
3540 -- (Ada 2005: AI-251): If the actual is an allocator whose
3541 -- directly designated type is a class-wide interface, we build
3542 -- an anonymous access type to use it as the type of the
3543 -- allocator. Later, when the subprogram call is expanded, if
3544 -- the interface has a secondary dispatch table the expander
3545 -- will add a type conversion to force the correct displacement
3548 if Nkind (A) = N_Allocator then
3550 DDT : constant Entity_Id :=
3551 Directly_Designated_Type (Base_Type (Etype (F)));
3553 New_Itype : Entity_Id;
3556 if Is_Class_Wide_Type (DDT)
3557 and then Is_Interface (DDT)
3559 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3560 Set_Etype (New_Itype, Etype (A));
3561 Set_Directly_Designated_Type (New_Itype,
3562 Directly_Designated_Type (Etype (A)));
3563 Set_Etype (A, New_Itype);
3566 -- Ada 2005, AI-162:If the actual is an allocator, the
3567 -- innermost enclosing statement is the master of the
3568 -- created object. This needs to be done with expansion
3569 -- enabled only, otherwise the transient scope will not
3570 -- be removed in the expansion of the wrapped construct.
3572 if (Is_Controlled (DDT) or else Has_Task (DDT))
3573 and then Full_Expander_Active
3575 Establish_Transient_Scope (A, False);
3580 -- (Ada 2005): The call may be to a primitive operation of
3581 -- a tagged synchronized type, declared outside of the type.
3582 -- In this case the controlling actual must be converted to
3583 -- its corresponding record type, which is the formal type.
3584 -- The actual may be a subtype, either because of a constraint
3585 -- or because it is a generic actual, so use base type to
3586 -- locate concurrent type.
3588 F_Typ := Base_Type (Etype (F));
3590 if Is_Tagged_Type (F_Typ)
3591 and then (Is_Concurrent_Type (F_Typ)
3592 or else Is_Concurrent_Record_Type (F_Typ))
3594 -- If the actual is overloaded, look for an interpretation
3595 -- that has a synchronized type.
3597 if not Is_Overloaded (A) then
3598 A_Typ := Base_Type (Etype (A));
3602 Index : Interp_Index;
3606 Get_First_Interp (A, Index, It);
3607 while Present (It.Typ) loop
3608 if Is_Concurrent_Type (It.Typ)
3609 or else Is_Concurrent_Record_Type (It.Typ)
3611 A_Typ := Base_Type (It.Typ);
3615 Get_Next_Interp (Index, It);
3621 Full_A_Typ : Entity_Id;
3624 if Present (Full_View (A_Typ)) then
3625 Full_A_Typ := Base_Type (Full_View (A_Typ));
3627 Full_A_Typ := A_Typ;
3630 -- Tagged synchronized type (case 1): the actual is a
3633 if Is_Concurrent_Type (A_Typ)
3634 and then Corresponding_Record_Type (A_Typ) = F_Typ
3637 Unchecked_Convert_To
3638 (Corresponding_Record_Type (A_Typ), A));
3639 Resolve (A, Etype (F));
3641 -- Tagged synchronized type (case 2): the formal is a
3644 elsif Ekind (Full_A_Typ) = E_Record_Type
3646 (Corresponding_Concurrent_Type (Full_A_Typ))
3647 and then Is_Concurrent_Type (F_Typ)
3648 and then Present (Corresponding_Record_Type (F_Typ))
3649 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3651 Resolve (A, Corresponding_Record_Type (F_Typ));
3656 Resolve (A, Etype (F));
3661 -- not a synchronized operation.
3663 Resolve (A, Etype (F));
3670 if Comes_From_Source (Original_Node (N))
3671 and then Nkind_In (Original_Node (N), N_Function_Call,
3672 N_Procedure_Call_Statement)
3674 -- In formal mode, check that actual parameters matching
3675 -- formals of tagged types are objects (or ancestor type
3676 -- conversions of objects), not general expressions.
3678 if Is_Actual_Tagged_Parameter (A) then
3679 if Is_SPARK_Object_Reference (A) then
3682 elsif Nkind (A) = N_Type_Conversion then
3684 Operand : constant Node_Id := Expression (A);
3685 Operand_Typ : constant Entity_Id := Etype (Operand);
3686 Target_Typ : constant Entity_Id := A_Typ;
3689 if not Is_SPARK_Object_Reference (Operand) then
3690 Check_SPARK_Restriction
3691 ("object required", Operand);
3693 -- In formal mode, the only view conversions are those
3694 -- involving ancestor conversion of an extended type.
3697 (Is_Tagged_Type (Target_Typ)
3698 and then not Is_Class_Wide_Type (Target_Typ)
3699 and then Is_Tagged_Type (Operand_Typ)
3700 and then not Is_Class_Wide_Type (Operand_Typ)
3701 and then Is_Ancestor (Target_Typ, Operand_Typ))
3704 (F, E_Out_Parameter, E_In_Out_Parameter)
3706 Check_SPARK_Restriction
3707 ("ancestor conversion is the only permitted "
3708 & "view conversion", A);
3710 Check_SPARK_Restriction
3711 ("ancestor conversion required", A);
3720 Check_SPARK_Restriction ("object required", A);
3723 -- In formal mode, the only view conversions are those
3724 -- involving ancestor conversion of an extended type.
3726 elsif Nkind (A) = N_Type_Conversion
3727 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3729 Check_SPARK_Restriction
3730 ("ancestor conversion is the only permitted view "
3735 -- Save actual for subsequent check on order dependence, and
3736 -- indicate whether actual is modifiable. For AI05-0144-2.
3738 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3740 -- For mode IN, if actual is an entity, and the type of the formal
3741 -- has warnings suppressed, then we reset Never_Set_In_Source for
3742 -- the calling entity. The reason for this is to catch cases like
3743 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3744 -- uses trickery to modify an IN parameter.
3746 if Ekind (F) = E_In_Parameter
3747 and then Is_Entity_Name (A)
3748 and then Present (Entity (A))
3749 and then Ekind (Entity (A)) = E_Variable
3750 and then Has_Warnings_Off (F_Typ)
3752 Set_Never_Set_In_Source (Entity (A), False);
3755 -- Perform error checks for IN and IN OUT parameters
3757 if Ekind (F) /= E_Out_Parameter then
3759 -- Check unset reference. For scalar parameters, it is clearly
3760 -- wrong to pass an uninitialized value as either an IN or
3761 -- IN-OUT parameter. For composites, it is also clearly an
3762 -- error to pass a completely uninitialized value as an IN
3763 -- parameter, but the case of IN OUT is trickier. We prefer
3764 -- not to give a warning here. For example, suppose there is
3765 -- a routine that sets some component of a record to False.
3766 -- It is perfectly reasonable to make this IN-OUT and allow
3767 -- either initialized or uninitialized records to be passed
3770 -- For partially initialized composite values, we also avoid
3771 -- warnings, since it is quite likely that we are passing a
3772 -- partially initialized value and only the initialized fields
3773 -- will in fact be read in the subprogram.
3775 if Is_Scalar_Type (A_Typ)
3776 or else (Ekind (F) = E_In_Parameter
3777 and then not Is_Partially_Initialized_Type (A_Typ))
3779 Check_Unset_Reference (A);
3782 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3783 -- actual to a nested call, since this is case of reading an
3784 -- out parameter, which is not allowed.
3786 if Ada_Version = Ada_83
3787 and then Is_Entity_Name (A)
3788 and then Ekind (Entity (A)) = E_Out_Parameter
3790 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3794 -- Case of OUT or IN OUT parameter
3796 if Ekind (F) /= E_In_Parameter then
3798 -- For an Out parameter, check for useless assignment. Note
3799 -- that we can't set Last_Assignment this early, because we may
3800 -- kill current values in Resolve_Call, and that call would
3801 -- clobber the Last_Assignment field.
3803 -- Note: call Warn_On_Useless_Assignment before doing the check
3804 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3805 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3806 -- reflects the last assignment, not this one!
3808 if Ekind (F) = E_Out_Parameter then
3809 if Warn_On_Modified_As_Out_Parameter (F)
3810 and then Is_Entity_Name (A)
3811 and then Present (Entity (A))
3812 and then Comes_From_Source (N)
3814 Warn_On_Useless_Assignment (Entity (A), A);
3818 -- Validate the form of the actual. Note that the call to
3819 -- Is_OK_Variable_For_Out_Formal generates the required
3820 -- reference in this case.
3822 -- A call to an initialization procedure for an aggregate
3823 -- component may initialize a nested component of a constant
3824 -- designated object. In this context the object is variable.
3826 if not Is_OK_Variable_For_Out_Formal (A)
3827 and then not Is_Init_Proc (Nam)
3829 Error_Msg_NE ("actual for& must be a variable", A, F);
3832 -- What's the following about???
3834 if Is_Entity_Name (A) then
3835 Kill_Checks (Entity (A));
3841 if Etype (A) = Any_Type then
3842 Set_Etype (N, Any_Type);
3846 -- Apply appropriate range checks for in, out, and in-out
3847 -- parameters. Out and in-out parameters also need a separate
3848 -- check, if there is a type conversion, to make sure the return
3849 -- value meets the constraints of the variable before the
3852 -- Gigi looks at the check flag and uses the appropriate types.
3853 -- For now since one flag is used there is an optimization which
3854 -- might not be done in the In Out case since Gigi does not do
3855 -- any analysis. More thought required about this ???
3857 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3859 -- Apply predicate checks, unless this is a call to the
3860 -- predicate check function itself, which would cause an
3861 -- infinite recursion.
3863 if not (Ekind (Nam) = E_Function
3864 and then Has_Predicates (Nam))
3866 Apply_Predicate_Check (A, F_Typ);
3869 -- Apply required constraint checks
3871 if Is_Scalar_Type (Etype (A)) then
3872 Apply_Scalar_Range_Check (A, F_Typ);
3874 elsif Is_Array_Type (Etype (A)) then
3875 Apply_Length_Check (A, F_Typ);
3877 elsif Is_Record_Type (F_Typ)
3878 and then Has_Discriminants (F_Typ)
3879 and then Is_Constrained (F_Typ)
3880 and then (not Is_Derived_Type (F_Typ)
3881 or else Comes_From_Source (Nam))
3883 Apply_Discriminant_Check (A, F_Typ);
3885 elsif Is_Access_Type (F_Typ)
3886 and then Is_Array_Type (Designated_Type (F_Typ))
3887 and then Is_Constrained (Designated_Type (F_Typ))
3889 Apply_Length_Check (A, F_Typ);
3891 elsif Is_Access_Type (F_Typ)
3892 and then Has_Discriminants (Designated_Type (F_Typ))
3893 and then Is_Constrained (Designated_Type (F_Typ))
3895 Apply_Discriminant_Check (A, F_Typ);
3898 Apply_Range_Check (A, F_Typ);
3901 -- Ada 2005 (AI-231): Note that the controlling parameter case
3902 -- already existed in Ada 95, which is partially checked
3903 -- elsewhere (see Checks), and we don't want the warning
3904 -- message to differ.
3906 if Is_Access_Type (F_Typ)
3907 and then Can_Never_Be_Null (F_Typ)
3908 and then Known_Null (A)
3910 if Is_Controlling_Formal (F) then
3911 Apply_Compile_Time_Constraint_Error
3913 Msg => "null value not allowed here?",
3914 Reason => CE_Access_Check_Failed);
3916 elsif Ada_Version >= Ada_2005 then
3917 Apply_Compile_Time_Constraint_Error
3919 Msg => "(Ada 2005) null not allowed in "
3920 & "null-excluding formal?",
3921 Reason => CE_Null_Not_Allowed);
3926 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3927 if Nkind (A) = N_Type_Conversion then
3928 if Is_Scalar_Type (A_Typ) then
3929 Apply_Scalar_Range_Check
3930 (Expression (A), Etype (Expression (A)), A_Typ);
3933 (Expression (A), Etype (Expression (A)), A_Typ);
3937 if Is_Scalar_Type (F_Typ) then
3938 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3939 elsif Is_Array_Type (F_Typ)
3940 and then Ekind (F) = E_Out_Parameter
3942 Apply_Length_Check (A, F_Typ);
3944 Apply_Range_Check (A, A_Typ, F_Typ);
3949 -- An actual associated with an access parameter is implicitly
3950 -- converted to the anonymous access type of the formal and must
3951 -- satisfy the legality checks for access conversions.
3953 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3954 if not Valid_Conversion (A, F_Typ, A) then
3956 ("invalid implicit conversion for access parameter", A);
3960 -- Check bad case of atomic/volatile argument (RM C.6(12))
3962 if Is_By_Reference_Type (Etype (F))
3963 and then Comes_From_Source (N)
3965 if Is_Atomic_Object (A)
3966 and then not Is_Atomic (Etype (F))
3969 ("cannot pass atomic argument to non-atomic formal&",
3972 elsif Is_Volatile_Object (A)
3973 and then not Is_Volatile (Etype (F))
3976 ("cannot pass volatile argument to non-volatile formal&",
3981 -- Check that subprograms don't have improper controlling
3982 -- arguments (RM 3.9.2 (9)).
3984 -- A primitive operation may have an access parameter of an
3985 -- incomplete tagged type, but a dispatching call is illegal
3986 -- if the type is still incomplete.
3988 if Is_Controlling_Formal (F) then
3989 Set_Is_Controlling_Actual (A);
3991 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3993 Desig : constant Entity_Id := Designated_Type (Etype (F));
3995 if Ekind (Desig) = E_Incomplete_Type
3996 and then No (Full_View (Desig))
3997 and then No (Non_Limited_View (Desig))
4000 ("premature use of incomplete type& " &
4001 "in dispatching call", A, Desig);
4006 elsif Nkind (A) = N_Explicit_Dereference then
4007 Validate_Remote_Access_To_Class_Wide_Type (A);
4010 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4011 and then not Is_Class_Wide_Type (F_Typ)
4012 and then not Is_Controlling_Formal (F)
4014 Error_Msg_N ("class-wide argument not allowed here!", A);
4016 if Is_Subprogram (Nam)
4017 and then Comes_From_Source (Nam)
4019 Error_Msg_Node_2 := F_Typ;
4021 ("& is not a dispatching operation of &!", A, Nam);
4024 -- Apply the checks described in 3.10.2(27): if the context is a
4025 -- specific access-to-object, the actual cannot be class-wide.
4026 -- Use base type to exclude access_to_subprogram cases.
4028 elsif Is_Access_Type (A_Typ)
4029 and then Is_Access_Type (F_Typ)
4030 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4031 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4032 or else (Nkind (A) = N_Attribute_Reference
4034 Is_Class_Wide_Type (Etype (Prefix (A)))))
4035 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4036 and then not Is_Controlling_Formal (F)
4038 -- Disable these checks for call to imported C++ subprograms
4041 (Is_Entity_Name (Name (N))
4042 and then Is_Imported (Entity (Name (N)))
4043 and then Convention (Entity (Name (N))) = Convention_CPP)
4046 ("access to class-wide argument not allowed here!", A);
4048 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4049 Error_Msg_Node_2 := Designated_Type (F_Typ);
4051 ("& is not a dispatching operation of &!", A, Nam);
4057 -- If it is a named association, treat the selector_name as a
4058 -- proper identifier, and mark the corresponding entity. Ignore
4059 -- this reference in Alfa mode, as it refers to an entity not in
4060 -- scope at the point of reference, so the reference should be
4061 -- ignored for computing effects of subprograms.
4063 if Nkind (Parent (A)) = N_Parameter_Association
4064 and then not Alfa_Mode
4066 Set_Entity (Selector_Name (Parent (A)), F);
4067 Generate_Reference (F, Selector_Name (Parent (A)));
4068 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4069 Generate_Reference (F_Typ, N, ' ');
4074 if Ekind (F) /= E_Out_Parameter then
4075 Check_Unset_Reference (A);
4080 -- Case where actual is not present
4088 end Resolve_Actuals;
4090 -----------------------
4091 -- Resolve_Allocator --
4092 -----------------------
4094 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4095 Desig_T : constant Entity_Id := Designated_Type (Typ);
4096 E : constant Node_Id := Expression (N);
4098 Discrim : Entity_Id;
4101 Assoc : Node_Id := Empty;
4104 procedure Check_Allocator_Discrim_Accessibility
4105 (Disc_Exp : Node_Id;
4106 Alloc_Typ : Entity_Id);
4107 -- Check that accessibility level associated with an access discriminant
4108 -- initialized in an allocator by the expression Disc_Exp is not deeper
4109 -- than the level of the allocator type Alloc_Typ. An error message is
4110 -- issued if this condition is violated. Specialized checks are done for
4111 -- the cases of a constraint expression which is an access attribute or
4112 -- an access discriminant.
4114 function In_Dispatching_Context return Boolean;
4115 -- If the allocator is an actual in a call, it is allowed to be class-
4116 -- wide when the context is not because it is a controlling actual.
4118 -------------------------------------------
4119 -- Check_Allocator_Discrim_Accessibility --
4120 -------------------------------------------
4122 procedure Check_Allocator_Discrim_Accessibility
4123 (Disc_Exp : Node_Id;
4124 Alloc_Typ : Entity_Id)
4127 if Type_Access_Level (Etype (Disc_Exp)) >
4128 Deepest_Type_Access_Level (Alloc_Typ)
4131 ("operand type has deeper level than allocator type", Disc_Exp);
4133 -- When the expression is an Access attribute the level of the prefix
4134 -- object must not be deeper than that of the allocator's type.
4136 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4137 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4139 and then Object_Access_Level (Prefix (Disc_Exp)) >
4140 Deepest_Type_Access_Level (Alloc_Typ)
4143 ("prefix of attribute has deeper level than allocator type",
4146 -- When the expression is an access discriminant the check is against
4147 -- the level of the prefix object.
4149 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4150 and then Nkind (Disc_Exp) = N_Selected_Component
4151 and then Object_Access_Level (Prefix (Disc_Exp)) >
4152 Deepest_Type_Access_Level (Alloc_Typ)
4155 ("access discriminant has deeper level than allocator type",
4158 -- All other cases are legal
4163 end Check_Allocator_Discrim_Accessibility;
4165 ----------------------------
4166 -- In_Dispatching_Context --
4167 ----------------------------
4169 function In_Dispatching_Context return Boolean is
4170 Par : constant Node_Id := Parent (N);
4174 Nkind_In (Par, N_Function_Call,
4175 N_Procedure_Call_Statement)
4176 and then Is_Entity_Name (Name (Par))
4177 and then Is_Dispatching_Operation (Entity (Name (Par)));
4178 end In_Dispatching_Context;
4180 -- Start of processing for Resolve_Allocator
4183 -- Replace general access with specific type
4185 if Ekind (Etype (N)) = E_Allocator_Type then
4186 Set_Etype (N, Base_Type (Typ));
4189 if Is_Abstract_Type (Typ) then
4190 Error_Msg_N ("type of allocator cannot be abstract", N);
4193 -- For qualified expression, resolve the expression using the
4194 -- given subtype (nothing to do for type mark, subtype indication)
4196 if Nkind (E) = N_Qualified_Expression then
4197 if Is_Class_Wide_Type (Etype (E))
4198 and then not Is_Class_Wide_Type (Desig_T)
4199 and then not In_Dispatching_Context
4202 ("class-wide allocator not allowed for this access type", N);
4205 Resolve (Expression (E), Etype (E));
4206 Check_Unset_Reference (Expression (E));
4208 -- A qualified expression requires an exact match of the type,
4209 -- class-wide matching is not allowed.
4211 if (Is_Class_Wide_Type (Etype (Expression (E)))
4212 or else Is_Class_Wide_Type (Etype (E)))
4213 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4215 Wrong_Type (Expression (E), Etype (E));
4218 -- A special accessibility check is needed for allocators that
4219 -- constrain access discriminants. The level of the type of the
4220 -- expression used to constrain an access discriminant cannot be
4221 -- deeper than the type of the allocator (in contrast to access
4222 -- parameters, where the level of the actual can be arbitrary).
4224 -- We can't use Valid_Conversion to perform this check because
4225 -- in general the type of the allocator is unrelated to the type
4226 -- of the access discriminant.
4228 if Ekind (Typ) /= E_Anonymous_Access_Type
4229 or else Is_Local_Anonymous_Access (Typ)
4231 Subtyp := Entity (Subtype_Mark (E));
4233 Aggr := Original_Node (Expression (E));
4235 if Has_Discriminants (Subtyp)
4236 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4238 Discrim := First_Discriminant (Base_Type (Subtyp));
4240 -- Get the first component expression of the aggregate
4242 if Present (Expressions (Aggr)) then
4243 Disc_Exp := First (Expressions (Aggr));
4245 elsif Present (Component_Associations (Aggr)) then
4246 Assoc := First (Component_Associations (Aggr));
4248 if Present (Assoc) then
4249 Disc_Exp := Expression (Assoc);
4258 while Present (Discrim) and then Present (Disc_Exp) loop
4259 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4260 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4263 Next_Discriminant (Discrim);
4265 if Present (Discrim) then
4266 if Present (Assoc) then
4268 Disc_Exp := Expression (Assoc);
4270 elsif Present (Next (Disc_Exp)) then
4274 Assoc := First (Component_Associations (Aggr));
4276 if Present (Assoc) then
4277 Disc_Exp := Expression (Assoc);
4287 -- For a subtype mark or subtype indication, freeze the subtype
4290 Freeze_Expression (E);
4292 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4294 ("initialization required for access-to-constant allocator", N);
4297 -- A special accessibility check is needed for allocators that
4298 -- constrain access discriminants. The level of the type of the
4299 -- expression used to constrain an access discriminant cannot be
4300 -- deeper than the type of the allocator (in contrast to access
4301 -- parameters, where the level of the actual can be arbitrary).
4302 -- We can't use Valid_Conversion to perform this check because
4303 -- in general the type of the allocator is unrelated to the type
4304 -- of the access discriminant.
4306 if Nkind (Original_Node (E)) = N_Subtype_Indication
4307 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4308 or else Is_Local_Anonymous_Access (Typ))
4310 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4312 if Has_Discriminants (Subtyp) then
4313 Discrim := First_Discriminant (Base_Type (Subtyp));
4314 Constr := First (Constraints (Constraint (Original_Node (E))));
4315 while Present (Discrim) and then Present (Constr) loop
4316 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4317 if Nkind (Constr) = N_Discriminant_Association then
4318 Disc_Exp := Original_Node (Expression (Constr));
4320 Disc_Exp := Original_Node (Constr);
4323 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4326 Next_Discriminant (Discrim);
4333 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4334 -- check that the level of the type of the created object is not deeper
4335 -- than the level of the allocator's access type, since extensions can
4336 -- now occur at deeper levels than their ancestor types. This is a
4337 -- static accessibility level check; a run-time check is also needed in
4338 -- the case of an initialized allocator with a class-wide argument (see
4339 -- Expand_Allocator_Expression).
4341 if Ada_Version >= Ada_2005
4342 and then Is_Class_Wide_Type (Desig_T)
4345 Exp_Typ : Entity_Id;
4348 if Nkind (E) = N_Qualified_Expression then
4349 Exp_Typ := Etype (E);
4350 elsif Nkind (E) = N_Subtype_Indication then
4351 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4353 Exp_Typ := Entity (E);
4356 if Type_Access_Level (Exp_Typ) >
4357 Deepest_Type_Access_Level (Typ)
4359 if In_Instance_Body then
4360 Error_Msg_N ("?type in allocator has deeper level than" &
4361 " designated class-wide type", E);
4362 Error_Msg_N ("\?Program_Error will be raised at run time",
4365 Make_Raise_Program_Error (Sloc (N),
4366 Reason => PE_Accessibility_Check_Failed));
4369 -- Do not apply Ada 2005 accessibility checks on a class-wide
4370 -- allocator if the type given in the allocator is a formal
4371 -- type. A run-time check will be performed in the instance.
4373 elsif not Is_Generic_Type (Exp_Typ) then
4374 Error_Msg_N ("type in allocator has deeper level than" &
4375 " designated class-wide type", E);
4381 -- Check for allocation from an empty storage pool
4383 if No_Pool_Assigned (Typ) then
4384 Error_Msg_N ("allocation from empty storage pool!", N);
4386 -- If the context is an unchecked conversion, as may happen within an
4387 -- inlined subprogram, the allocator is being resolved with its own
4388 -- anonymous type. In that case, if the target type has a specific
4389 -- storage pool, it must be inherited explicitly by the allocator type.
4391 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4392 and then No (Associated_Storage_Pool (Typ))
4394 Set_Associated_Storage_Pool
4395 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4398 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4399 Check_Restriction (No_Anonymous_Allocators, N);
4402 -- Check that an allocator with task parts isn't for a nested access
4403 -- type when restriction No_Task_Hierarchy applies.
4405 if not Is_Library_Level_Entity (Base_Type (Typ))
4406 and then Has_Task (Base_Type (Desig_T))
4408 Check_Restriction (No_Task_Hierarchy, N);
4411 -- An erroneous allocator may be rewritten as a raise Program_Error
4414 if Nkind (N) = N_Allocator then
4416 -- An anonymous access discriminant is the definition of a
4419 if Ekind (Typ) = E_Anonymous_Access_Type
4420 and then Nkind (Associated_Node_For_Itype (Typ)) =
4421 N_Discriminant_Specification
4424 Discr : constant Entity_Id :=
4425 Defining_Identifier (Associated_Node_For_Itype (Typ));
4428 -- Ada 2012 AI05-0052: If the designated type of the allocator
4429 -- is limited, then the allocator shall not be used to define
4430 -- the value of an access discriminant unless the discriminated
4431 -- type is immutably limited.
4433 if Ada_Version >= Ada_2012
4434 and then Is_Limited_Type (Desig_T)
4435 and then not Is_Immutably_Limited_Type (Scope (Discr))
4438 ("only immutably limited types can have anonymous "
4439 & "access discriminants designating a limited type", N);
4443 -- Avoid marking an allocator as a dynamic coextension if it is
4444 -- within a static construct.
4446 if not Is_Static_Coextension (N) then
4447 Set_Is_Dynamic_Coextension (N);
4450 -- Cleanup for potential static coextensions
4453 Set_Is_Dynamic_Coextension (N, False);
4454 Set_Is_Static_Coextension (N, False);
4458 -- Report a simple error: if the designated object is a local task,
4459 -- its body has not been seen yet, and its activation will fail an
4460 -- elaboration check.
4462 if Is_Task_Type (Desig_T)
4463 and then Scope (Base_Type (Desig_T)) = Current_Scope
4464 and then Is_Compilation_Unit (Current_Scope)
4465 and then Ekind (Current_Scope) = E_Package
4466 and then not In_Package_Body (Current_Scope)
4468 Error_Msg_N ("cannot activate task before body seen?", N);
4469 Error_Msg_N ("\Program_Error will be raised at run time?", N);
4472 -- Ada 2012 (AI05-0111-3): Issue a warning whenever allocating a task
4473 -- or a type containing tasks on a subpool since the deallocation of
4474 -- the subpool may lead to undefined task behavior. Perform the check
4475 -- only when the allocator has not been converted into a Program_Error
4476 -- due to a previous error.
4478 if Ada_Version >= Ada_2012
4479 and then Nkind (N) = N_Allocator
4480 and then Present (Subpool_Handle_Name (N))
4481 and then Has_Task (Desig_T)
4483 Error_Msg_N ("?allocation of task on subpool may lead to " &
4484 "undefined behavior", N);
4486 end Resolve_Allocator;
4488 ---------------------------
4489 -- Resolve_Arithmetic_Op --
4490 ---------------------------
4492 -- Used for resolving all arithmetic operators except exponentiation
4494 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4495 L : constant Node_Id := Left_Opnd (N);
4496 R : constant Node_Id := Right_Opnd (N);
4497 TL : constant Entity_Id := Base_Type (Etype (L));
4498 TR : constant Entity_Id := Base_Type (Etype (R));
4502 B_Typ : constant Entity_Id := Base_Type (Typ);
4503 -- We do the resolution using the base type, because intermediate values
4504 -- in expressions always are of the base type, not a subtype of it.
4506 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4507 -- Returns True if N is in a context that expects "any real type"
4509 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4510 -- Return True iff given type is Integer or universal real/integer
4512 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4513 -- Choose type of integer literal in fixed-point operation to conform
4514 -- to available fixed-point type. T is the type of the other operand,
4515 -- which is needed to determine the expected type of N.
4517 procedure Set_Operand_Type (N : Node_Id);
4518 -- Set operand type to T if universal
4520 -------------------------------
4521 -- Expected_Type_Is_Any_Real --
4522 -------------------------------
4524 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4526 -- N is the expression after "delta" in a fixed_point_definition;
4529 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4530 N_Decimal_Fixed_Point_Definition,
4532 -- N is one of the bounds in a real_range_specification;
4535 N_Real_Range_Specification,
4537 -- N is the expression of a delta_constraint;
4540 N_Delta_Constraint);
4541 end Expected_Type_Is_Any_Real;
4543 -----------------------------
4544 -- Is_Integer_Or_Universal --
4545 -----------------------------
4547 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4549 Index : Interp_Index;
4553 if not Is_Overloaded (N) then
4555 return Base_Type (T) = Base_Type (Standard_Integer)
4556 or else T = Universal_Integer
4557 or else T = Universal_Real;
4559 Get_First_Interp (N, Index, It);
4560 while Present (It.Typ) loop
4561 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4562 or else It.Typ = Universal_Integer
4563 or else It.Typ = Universal_Real
4568 Get_Next_Interp (Index, It);
4573 end Is_Integer_Or_Universal;
4575 ----------------------------
4576 -- Set_Mixed_Mode_Operand --
4577 ----------------------------
4579 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4580 Index : Interp_Index;
4584 if Universal_Interpretation (N) = Universal_Integer then
4586 -- A universal integer literal is resolved as standard integer
4587 -- except in the case of a fixed-point result, where we leave it
4588 -- as universal (to be handled by Exp_Fixd later on)
4590 if Is_Fixed_Point_Type (T) then
4591 Resolve (N, Universal_Integer);
4593 Resolve (N, Standard_Integer);
4596 elsif Universal_Interpretation (N) = Universal_Real
4597 and then (T = Base_Type (Standard_Integer)
4598 or else T = Universal_Integer
4599 or else T = Universal_Real)
4601 -- A universal real can appear in a fixed-type context. We resolve
4602 -- the literal with that context, even though this might raise an
4603 -- exception prematurely (the other operand may be zero).
4607 elsif Etype (N) = Base_Type (Standard_Integer)
4608 and then T = Universal_Real
4609 and then Is_Overloaded (N)
4611 -- Integer arg in mixed-mode operation. Resolve with universal
4612 -- type, in case preference rule must be applied.
4614 Resolve (N, Universal_Integer);
4617 and then B_Typ /= Universal_Fixed
4619 -- Not a mixed-mode operation, resolve with context
4623 elsif Etype (N) = Any_Fixed then
4625 -- N may itself be a mixed-mode operation, so use context type
4629 elsif Is_Fixed_Point_Type (T)
4630 and then B_Typ = Universal_Fixed
4631 and then Is_Overloaded (N)
4633 -- Must be (fixed * fixed) operation, operand must have one
4634 -- compatible interpretation.
4636 Resolve (N, Any_Fixed);
4638 elsif Is_Fixed_Point_Type (B_Typ)
4639 and then (T = Universal_Real
4640 or else Is_Fixed_Point_Type (T))
4641 and then Is_Overloaded (N)
4643 -- C * F(X) in a fixed context, where C is a real literal or a
4644 -- fixed-point expression. F must have either a fixed type
4645 -- interpretation or an integer interpretation, but not both.
4647 Get_First_Interp (N, Index, It);
4648 while Present (It.Typ) loop
4649 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4650 if Analyzed (N) then
4651 Error_Msg_N ("ambiguous operand in fixed operation", N);
4653 Resolve (N, Standard_Integer);
4656 elsif Is_Fixed_Point_Type (It.Typ) then
4657 if Analyzed (N) then
4658 Error_Msg_N ("ambiguous operand in fixed operation", N);
4660 Resolve (N, It.Typ);
4664 Get_Next_Interp (Index, It);
4667 -- Reanalyze the literal with the fixed type of the context. If
4668 -- context is Universal_Fixed, we are within a conversion, leave
4669 -- the literal as a universal real because there is no usable
4670 -- fixed type, and the target of the conversion plays no role in
4684 if B_Typ = Universal_Fixed
4685 and then Nkind (Op2) = N_Real_Literal
4687 T2 := Universal_Real;
4692 Set_Analyzed (Op2, False);
4699 end Set_Mixed_Mode_Operand;
4701 ----------------------
4702 -- Set_Operand_Type --
4703 ----------------------
4705 procedure Set_Operand_Type (N : Node_Id) is
4707 if Etype (N) = Universal_Integer
4708 or else Etype (N) = Universal_Real
4712 end Set_Operand_Type;
4714 -- Start of processing for Resolve_Arithmetic_Op
4717 if Comes_From_Source (N)
4718 and then Ekind (Entity (N)) = E_Function
4719 and then Is_Imported (Entity (N))
4720 and then Is_Intrinsic_Subprogram (Entity (N))
4722 Resolve_Intrinsic_Operator (N, Typ);
4725 -- Special-case for mixed-mode universal expressions or fixed point type
4726 -- operation: each argument is resolved separately. The same treatment
4727 -- is required if one of the operands of a fixed point operation is
4728 -- universal real, since in this case we don't do a conversion to a
4729 -- specific fixed-point type (instead the expander handles the case).
4731 -- Set the type of the node to its universal interpretation because
4732 -- legality checks on an exponentiation operand need the context.
4734 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4735 and then Present (Universal_Interpretation (L))
4736 and then Present (Universal_Interpretation (R))
4738 Set_Etype (N, B_Typ);
4739 Resolve (L, Universal_Interpretation (L));
4740 Resolve (R, Universal_Interpretation (R));
4742 elsif (B_Typ = Universal_Real
4743 or else Etype (N) = Universal_Fixed
4744 or else (Etype (N) = Any_Fixed
4745 and then Is_Fixed_Point_Type (B_Typ))
4746 or else (Is_Fixed_Point_Type (B_Typ)
4747 and then (Is_Integer_Or_Universal (L)
4749 Is_Integer_Or_Universal (R))))
4750 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4752 if TL = Universal_Integer or else TR = Universal_Integer then
4753 Check_For_Visible_Operator (N, B_Typ);
4756 -- If context is a fixed type and one operand is integer, the other
4757 -- is resolved with the type of the context.
4759 if Is_Fixed_Point_Type (B_Typ)
4760 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4761 or else TL = Universal_Integer)
4766 elsif Is_Fixed_Point_Type (B_Typ)
4767 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4768 or else TR = Universal_Integer)
4774 Set_Mixed_Mode_Operand (L, TR);
4775 Set_Mixed_Mode_Operand (R, TL);
4778 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4779 -- multiplying operators from being used when the expected type is
4780 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4781 -- some cases where the expected type is actually Any_Real;
4782 -- Expected_Type_Is_Any_Real takes care of that case.
4784 if Etype (N) = Universal_Fixed
4785 or else Etype (N) = Any_Fixed
4787 if B_Typ = Universal_Fixed
4788 and then not Expected_Type_Is_Any_Real (N)
4789 and then not Nkind_In (Parent (N), N_Type_Conversion,
4790 N_Unchecked_Type_Conversion)
4792 Error_Msg_N ("type cannot be determined from context!", N);
4793 Error_Msg_N ("\explicit conversion to result type required", N);
4795 Set_Etype (L, Any_Type);
4796 Set_Etype (R, Any_Type);
4799 if Ada_Version = Ada_83
4800 and then Etype (N) = Universal_Fixed
4802 Nkind_In (Parent (N), N_Type_Conversion,
4803 N_Unchecked_Type_Conversion)
4806 ("(Ada 83) fixed-point operation "
4807 & "needs explicit conversion", N);
4810 -- The expected type is "any real type" in contexts like
4812 -- type T is delta <universal_fixed-expression> ...
4814 -- in which case we need to set the type to Universal_Real
4815 -- so that static expression evaluation will work properly.
4817 if Expected_Type_Is_Any_Real (N) then
4818 Set_Etype (N, Universal_Real);
4820 Set_Etype (N, B_Typ);
4824 elsif Is_Fixed_Point_Type (B_Typ)
4825 and then (Is_Integer_Or_Universal (L)
4826 or else Nkind (L) = N_Real_Literal
4827 or else Nkind (R) = N_Real_Literal
4828 or else Is_Integer_Or_Universal (R))
4830 Set_Etype (N, B_Typ);
4832 elsif Etype (N) = Any_Fixed then
4834 -- If no previous errors, this is only possible if one operand is
4835 -- overloaded and the context is universal. Resolve as such.
4837 Set_Etype (N, B_Typ);
4841 if (TL = Universal_Integer or else TL = Universal_Real)
4843 (TR = Universal_Integer or else TR = Universal_Real)
4845 Check_For_Visible_Operator (N, B_Typ);
4848 -- If the context is Universal_Fixed and the operands are also
4849 -- universal fixed, this is an error, unless there is only one
4850 -- applicable fixed_point type (usually Duration).
4852 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4853 T := Unique_Fixed_Point_Type (N);
4855 if T = Any_Type then
4868 -- If one of the arguments was resolved to a non-universal type.
4869 -- label the result of the operation itself with the same type.
4870 -- Do the same for the universal argument, if any.
4872 T := Intersect_Types (L, R);
4873 Set_Etype (N, Base_Type (T));
4874 Set_Operand_Type (L);
4875 Set_Operand_Type (R);
4878 Generate_Operator_Reference (N, Typ);
4879 Eval_Arithmetic_Op (N);
4881 -- In SPARK, a multiplication or division with operands of fixed point
4882 -- types shall be qualified or explicitly converted to identify the
4885 if (Is_Fixed_Point_Type (Etype (L))
4886 or else Is_Fixed_Point_Type (Etype (R)))
4887 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4889 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4891 Check_SPARK_Restriction
4892 ("operation should be qualified or explicitly converted", N);
4895 -- Set overflow and division checking bit. Much cleverer code needed
4896 -- here eventually and perhaps the Resolve routines should be separated
4897 -- for the various arithmetic operations, since they will need
4898 -- different processing. ???
4900 if Nkind (N) in N_Op then
4901 if not Overflow_Checks_Suppressed (Etype (N)) then
4902 Enable_Overflow_Check (N);
4905 -- Give warning if explicit division by zero
4907 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4908 and then not Division_Checks_Suppressed (Etype (N))
4910 Rop := Right_Opnd (N);
4912 if Compile_Time_Known_Value (Rop)
4913 and then ((Is_Integer_Type (Etype (Rop))
4914 and then Expr_Value (Rop) = Uint_0)
4916 (Is_Real_Type (Etype (Rop))
4917 and then Expr_Value_R (Rop) = Ureal_0))
4919 -- Specialize the warning message according to the operation.
4920 -- The following warnings are for the case
4925 -- For division, we have two cases, for float division
4926 -- of an unconstrained float type, on a machine where
4927 -- Machine_Overflows is false, we don't get an exception
4928 -- at run-time, but rather an infinity or Nan. The Nan
4929 -- case is pretty obscure, so just warn about infinities.
4931 if Is_Floating_Point_Type (Typ)
4932 and then not Is_Constrained (Typ)
4933 and then not Machine_Overflows_On_Target
4936 ("float division by zero, " &
4937 "may generate '+'/'- infinity?", Right_Opnd (N));
4939 -- For all other cases, we get a Constraint_Error
4942 Apply_Compile_Time_Constraint_Error
4943 (N, "division by zero?", CE_Divide_By_Zero,
4944 Loc => Sloc (Right_Opnd (N)));
4948 Apply_Compile_Time_Constraint_Error
4949 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4950 Loc => Sloc (Right_Opnd (N)));
4953 Apply_Compile_Time_Constraint_Error
4954 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4955 Loc => Sloc (Right_Opnd (N)));
4957 -- Division by zero can only happen with division, rem,
4958 -- and mod operations.
4961 raise Program_Error;
4964 -- Otherwise just set the flag to check at run time
4967 Activate_Division_Check (N);
4971 -- If Restriction No_Implicit_Conditionals is active, then it is
4972 -- violated if either operand can be negative for mod, or for rem
4973 -- if both operands can be negative.
4975 if Restriction_Check_Required (No_Implicit_Conditionals)
4976 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4985 -- Set if corresponding operand might be negative
4989 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4990 LNeg := (not OK) or else Lo < 0;
4993 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4994 RNeg := (not OK) or else Lo < 0;
4996 -- Check if we will be generating conditionals. There are two
4997 -- cases where that can happen, first for REM, the only case
4998 -- is largest negative integer mod -1, where the division can
4999 -- overflow, but we still have to give the right result. The
5000 -- front end generates a test for this annoying case. Here we
5001 -- just test if both operands can be negative (that's what the
5002 -- expander does, so we match its logic here).
5004 -- The second case is mod where either operand can be negative.
5005 -- In this case, the back end has to generate additional tests.
5007 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5009 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5011 Check_Restriction (No_Implicit_Conditionals, N);
5017 Check_Unset_Reference (L);
5018 Check_Unset_Reference (R);
5019 end Resolve_Arithmetic_Op;
5025 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5026 Loc : constant Source_Ptr := Sloc (N);
5027 Subp : constant Node_Id := Name (N);
5035 function Same_Or_Aliased_Subprograms
5037 E : Entity_Id) return Boolean;
5038 -- Returns True if the subprogram entity S is the same as E or else
5039 -- S is an alias of E.
5041 ---------------------------------
5042 -- Same_Or_Aliased_Subprograms --
5043 ---------------------------------
5045 function Same_Or_Aliased_Subprograms
5047 E : Entity_Id) return Boolean
5049 Subp_Alias : constant Entity_Id := Alias (S);
5052 or else (Present (Subp_Alias) and then Subp_Alias = E);
5053 end Same_Or_Aliased_Subprograms;
5055 -- Start of processing for Resolve_Call
5058 -- The context imposes a unique interpretation with type Typ on a
5059 -- procedure or function call. Find the entity of the subprogram that
5060 -- yields the expected type, and propagate the corresponding formal
5061 -- constraints on the actuals. The caller has established that an
5062 -- interpretation exists, and emitted an error if not unique.
5064 -- First deal with the case of a call to an access-to-subprogram,
5065 -- dereference made explicit in Analyze_Call.
5067 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5068 if not Is_Overloaded (Subp) then
5069 Nam := Etype (Subp);
5072 -- Find the interpretation whose type (a subprogram type) has a
5073 -- return type that is compatible with the context. Analysis of
5074 -- the node has established that one exists.
5078 Get_First_Interp (Subp, I, It);
5079 while Present (It.Typ) loop
5080 if Covers (Typ, Etype (It.Typ)) then
5085 Get_Next_Interp (I, It);
5089 raise Program_Error;
5093 -- If the prefix is not an entity, then resolve it
5095 if not Is_Entity_Name (Subp) then
5096 Resolve (Subp, Nam);
5099 -- For an indirect call, we always invalidate checks, since we do not
5100 -- know whether the subprogram is local or global. Yes we could do
5101 -- better here, e.g. by knowing that there are no local subprograms,
5102 -- but it does not seem worth the effort. Similarly, we kill all
5103 -- knowledge of current constant values.
5105 Kill_Current_Values;
5107 -- If this is a procedure call which is really an entry call, do
5108 -- the conversion of the procedure call to an entry call. Protected
5109 -- operations use the same circuitry because the name in the call
5110 -- can be an arbitrary expression with special resolution rules.
5112 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5113 or else (Is_Entity_Name (Subp)
5114 and then Ekind (Entity (Subp)) = E_Entry)
5116 Resolve_Entry_Call (N, Typ);
5117 Check_Elab_Call (N);
5119 -- Kill checks and constant values, as above for indirect case
5120 -- Who knows what happens when another task is activated?
5122 Kill_Current_Values;
5125 -- Normal subprogram call with name established in Resolve
5127 elsif not (Is_Type (Entity (Subp))) then
5128 Nam := Entity (Subp);
5129 Set_Entity_With_Style_Check (Subp, Nam);
5131 -- Otherwise we must have the case of an overloaded call
5134 pragma Assert (Is_Overloaded (Subp));
5136 -- Initialize Nam to prevent warning (we know it will be assigned
5137 -- in the loop below, but the compiler does not know that).
5141 Get_First_Interp (Subp, I, It);
5142 while Present (It.Typ) loop
5143 if Covers (Typ, It.Typ) then
5145 Set_Entity_With_Style_Check (Subp, Nam);
5149 Get_Next_Interp (I, It);
5153 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5154 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5155 and then Nkind (Subp) /= N_Explicit_Dereference
5156 and then Present (Parameter_Associations (N))
5158 -- The prefix is a parameterless function call that returns an access
5159 -- to subprogram. If parameters are present in the current call, add
5160 -- add an explicit dereference. We use the base type here because
5161 -- within an instance these may be subtypes.
5163 -- The dereference is added either in Analyze_Call or here. Should
5164 -- be consolidated ???
5166 Set_Is_Overloaded (Subp, False);
5167 Set_Etype (Subp, Etype (Nam));
5168 Insert_Explicit_Dereference (Subp);
5169 Nam := Designated_Type (Etype (Nam));
5170 Resolve (Subp, Nam);
5173 -- Check that a call to Current_Task does not occur in an entry body
5175 if Is_RTE (Nam, RE_Current_Task) then
5184 -- Exclude calls that occur within the default of a formal
5185 -- parameter of the entry, since those are evaluated outside
5188 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5190 if Nkind (P) = N_Entry_Body
5191 or else (Nkind (P) = N_Subprogram_Body
5192 and then Is_Entry_Barrier_Function (P))
5196 ("?& should not be used in entry body (RM C.7(17))",
5199 ("\Program_Error will be raised at run time?", N, Nam);
5201 Make_Raise_Program_Error (Loc,
5202 Reason => PE_Current_Task_In_Entry_Body));
5203 Set_Etype (N, Rtype);
5210 -- Check that a procedure call does not occur in the context of the
5211 -- entry call statement of a conditional or timed entry call. Note that
5212 -- the case of a call to a subprogram renaming of an entry will also be
5213 -- rejected. The test for N not being an N_Entry_Call_Statement is
5214 -- defensive, covering the possibility that the processing of entry
5215 -- calls might reach this point due to later modifications of the code
5218 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5219 and then Nkind (N) /= N_Entry_Call_Statement
5220 and then Entry_Call_Statement (Parent (N)) = N
5222 if Ada_Version < Ada_2005 then
5223 Error_Msg_N ("entry call required in select statement", N);
5225 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5226 -- for a procedure_or_entry_call, the procedure_name or
5227 -- procedure_prefix of the procedure_call_statement shall denote
5228 -- an entry renamed by a procedure, or (a view of) a primitive
5229 -- subprogram of a limited interface whose first parameter is
5230 -- a controlling parameter.
5232 elsif Nkind (N) = N_Procedure_Call_Statement
5233 and then not Is_Renamed_Entry (Nam)
5234 and then not Is_Controlling_Limited_Procedure (Nam)
5237 ("entry call or dispatching primitive of interface required", N);
5241 -- Check that this is not a call to a protected procedure or entry from
5242 -- within a protected function.
5244 if Ekind (Current_Scope) = E_Function
5245 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5246 and then Ekind (Nam) /= E_Function
5247 and then Scope (Nam) = Scope (Current_Scope)
5249 Error_Msg_N ("within protected function, protected " &
5250 "object is constant", N);
5251 Error_Msg_N ("\cannot call operation that may modify it", N);
5254 -- Freeze the subprogram name if not in a spec-expression. Note that we
5255 -- freeze procedure calls as well as function calls. Procedure calls are
5256 -- not frozen according to the rules (RM 13.14(14)) because it is
5257 -- impossible to have a procedure call to a non-frozen procedure in pure
5258 -- Ada, but in the code that we generate in the expander, this rule
5259 -- needs extending because we can generate procedure calls that need
5262 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5263 Freeze_Expression (Subp);
5266 -- For a predefined operator, the type of the result is the type imposed
5267 -- by context, except for a predefined operation on universal fixed.
5268 -- Otherwise The type of the call is the type returned by the subprogram
5271 if Is_Predefined_Op (Nam) then
5272 if Etype (N) /= Universal_Fixed then
5276 -- If the subprogram returns an array type, and the context requires the
5277 -- component type of that array type, the node is really an indexing of
5278 -- the parameterless call. Resolve as such. A pathological case occurs
5279 -- when the type of the component is an access to the array type. In
5280 -- this case the call is truly ambiguous.
5282 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5284 ((Is_Array_Type (Etype (Nam))
5285 and then Covers (Typ, Component_Type (Etype (Nam))))
5286 or else (Is_Access_Type (Etype (Nam))
5287 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5291 Component_Type (Designated_Type (Etype (Nam))))))
5294 Index_Node : Node_Id;
5296 Ret_Type : constant Entity_Id := Etype (Nam);
5299 if Is_Access_Type (Ret_Type)
5300 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5303 ("cannot disambiguate function call and indexing", N);
5305 New_Subp := Relocate_Node (Subp);
5306 Set_Entity (Subp, Nam);
5308 if (Is_Array_Type (Ret_Type)
5309 and then Component_Type (Ret_Type) /= Any_Type)
5311 (Is_Access_Type (Ret_Type)
5313 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5315 if Needs_No_Actuals (Nam) then
5317 -- Indexed call to a parameterless function
5320 Make_Indexed_Component (Loc,
5322 Make_Function_Call (Loc,
5324 Expressions => Parameter_Associations (N));
5326 -- An Ada 2005 prefixed call to a primitive operation
5327 -- whose first parameter is the prefix. This prefix was
5328 -- prepended to the parameter list, which is actually a
5329 -- list of indexes. Remove the prefix in order to build
5330 -- the proper indexed component.
5333 Make_Indexed_Component (Loc,
5335 Make_Function_Call (Loc,
5337 Parameter_Associations =>
5339 (Remove_Head (Parameter_Associations (N)))),
5340 Expressions => Parameter_Associations (N));
5343 -- Preserve the parenthesis count of the node
5345 Set_Paren_Count (Index_Node, Paren_Count (N));
5347 -- Since we are correcting a node classification error made
5348 -- by the parser, we call Replace rather than Rewrite.
5350 Replace (N, Index_Node);
5352 Set_Etype (Prefix (N), Ret_Type);
5354 Resolve_Indexed_Component (N, Typ);
5355 Check_Elab_Call (Prefix (N));
5363 Set_Etype (N, Etype (Nam));
5366 -- In the case where the call is to an overloaded subprogram, Analyze
5367 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5368 -- such a case Normalize_Actuals needs to be called once more to order
5369 -- the actuals correctly. Otherwise the call will have the ordering
5370 -- given by the last overloaded subprogram whether this is the correct
5371 -- one being called or not.
5373 if Is_Overloaded (Subp) then
5374 Normalize_Actuals (N, Nam, False, Norm_OK);
5375 pragma Assert (Norm_OK);
5378 -- In any case, call is fully resolved now. Reset Overload flag, to
5379 -- prevent subsequent overload resolution if node is analyzed again
5381 Set_Is_Overloaded (Subp, False);
5382 Set_Is_Overloaded (N, False);
5384 -- If we are calling the current subprogram from immediately within its
5385 -- body, then that is the case where we can sometimes detect cases of
5386 -- infinite recursion statically. Do not try this in case restriction
5387 -- No_Recursion is in effect anyway, and do it only for source calls.
5389 if Comes_From_Source (N) then
5390 Scop := Current_Scope;
5392 -- Issue warning for possible infinite recursion in the absence
5393 -- of the No_Recursion restriction.
5395 if Same_Or_Aliased_Subprograms (Nam, Scop)
5396 and then not Restriction_Active (No_Recursion)
5397 and then Check_Infinite_Recursion (N)
5399 -- Here we detected and flagged an infinite recursion, so we do
5400 -- not need to test the case below for further warnings. Also we
5401 -- are all done if we now have a raise SE node.
5403 if Nkind (N) = N_Raise_Storage_Error then
5407 -- If call is to immediately containing subprogram, then check for
5408 -- the case of a possible run-time detectable infinite recursion.
5411 Scope_Loop : while Scop /= Standard_Standard loop
5412 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5414 -- Although in general case, recursion is not statically
5415 -- checkable, the case of calling an immediately containing
5416 -- subprogram is easy to catch.
5418 Check_Restriction (No_Recursion, N);
5420 -- If the recursive call is to a parameterless subprogram,
5421 -- then even if we can't statically detect infinite
5422 -- recursion, this is pretty suspicious, and we output a
5423 -- warning. Furthermore, we will try later to detect some
5424 -- cases here at run time by expanding checking code (see
5425 -- Detect_Infinite_Recursion in package Exp_Ch6).
5427 -- If the recursive call is within a handler, do not emit a
5428 -- warning, because this is a common idiom: loop until input
5429 -- is correct, catch illegal input in handler and restart.
5431 if No (First_Formal (Nam))
5432 and then Etype (Nam) = Standard_Void_Type
5433 and then not Error_Posted (N)
5434 and then Nkind (Parent (N)) /= N_Exception_Handler
5436 -- For the case of a procedure call. We give the message
5437 -- only if the call is the first statement in a sequence
5438 -- of statements, or if all previous statements are
5439 -- simple assignments. This is simply a heuristic to
5440 -- decrease false positives, without losing too many good
5441 -- warnings. The idea is that these previous statements
5442 -- may affect global variables the procedure depends on.
5443 -- We also exclude raise statements, that may arise from
5444 -- constraint checks and are probably unrelated to the
5445 -- intended control flow.
5447 if Nkind (N) = N_Procedure_Call_Statement
5448 and then Is_List_Member (N)
5454 while Present (P) loop
5456 N_Assignment_Statement,
5457 N_Raise_Constraint_Error)
5467 -- Do not give warning if we are in a conditional context
5470 K : constant Node_Kind := Nkind (Parent (N));
5472 if (K = N_Loop_Statement
5473 and then Present (Iteration_Scheme (Parent (N))))
5474 or else K = N_If_Statement
5475 or else K = N_Elsif_Part
5476 or else K = N_Case_Statement_Alternative
5482 -- Here warning is to be issued
5484 Set_Has_Recursive_Call (Nam);
5486 ("?possible infinite recursion!", N);
5488 ("\?Storage_Error may be raised at run time!", N);
5494 Scop := Scope (Scop);
5495 end loop Scope_Loop;
5499 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5501 Check_Obsolescent_2005_Entity (Nam, Subp);
5503 -- If subprogram name is a predefined operator, it was given in
5504 -- functional notation. Replace call node with operator node, so
5505 -- that actuals can be resolved appropriately.
5507 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5508 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5511 elsif Present (Alias (Nam))
5512 and then Is_Predefined_Op (Alias (Nam))
5514 Resolve_Actuals (N, Nam);
5515 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5519 -- Create a transient scope if the resulting type requires it
5521 -- There are several notable exceptions:
5523 -- a) In init procs, the transient scope overhead is not needed, and is
5524 -- even incorrect when the call is a nested initialization call for a
5525 -- component whose expansion may generate adjust calls. However, if the
5526 -- call is some other procedure call within an initialization procedure
5527 -- (for example a call to Create_Task in the init_proc of the task
5528 -- run-time record) a transient scope must be created around this call.
5530 -- b) Enumeration literal pseudo-calls need no transient scope
5532 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5533 -- functions) do not use the secondary stack even though the return
5534 -- type may be unconstrained.
5536 -- d) Calls to a build-in-place function, since such functions may
5537 -- allocate their result directly in a target object, and cases where
5538 -- the result does get allocated in the secondary stack are checked for
5539 -- within the specialized Exp_Ch6 procedures for expanding those
5540 -- build-in-place calls.
5542 -- e) If the subprogram is marked Inline_Always, then even if it returns
5543 -- an unconstrained type the call does not require use of the secondary
5544 -- stack. However, inlining will only take place if the body to inline
5545 -- is already present. It may not be available if e.g. the subprogram is
5546 -- declared in a child instance.
5548 -- If this is an initialization call for a type whose construction
5549 -- uses the secondary stack, and it is not a nested call to initialize
5550 -- a component, we do need to create a transient scope for it. We
5551 -- check for this by traversing the type in Check_Initialization_Call.
5554 and then Has_Pragma_Inline_Always (Nam)
5555 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5556 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5560 elsif Ekind (Nam) = E_Enumeration_Literal
5561 or else Is_Build_In_Place_Function (Nam)
5562 or else Is_Intrinsic_Subprogram (Nam)
5566 elsif Full_Expander_Active
5567 and then Is_Type (Etype (Nam))
5568 and then Requires_Transient_Scope (Etype (Nam))
5570 (not Within_Init_Proc
5572 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5574 Establish_Transient_Scope (N, Sec_Stack => True);
5576 -- If the call appears within the bounds of a loop, it will
5577 -- be rewritten and reanalyzed, nothing left to do here.
5579 if Nkind (N) /= N_Function_Call then
5583 elsif Is_Init_Proc (Nam)
5584 and then not Within_Init_Proc
5586 Check_Initialization_Call (N, Nam);
5589 -- A protected function cannot be called within the definition of the
5590 -- enclosing protected type.
5592 if Is_Protected_Type (Scope (Nam))
5593 and then In_Open_Scopes (Scope (Nam))
5594 and then not Has_Completion (Scope (Nam))
5597 ("& cannot be called before end of protected definition", N, Nam);
5600 -- Propagate interpretation to actuals, and add default expressions
5603 if Present (First_Formal (Nam)) then
5604 Resolve_Actuals (N, Nam);
5606 -- Overloaded literals are rewritten as function calls, for purpose of
5607 -- resolution. After resolution, we can replace the call with the
5610 elsif Ekind (Nam) = E_Enumeration_Literal then
5611 Copy_Node (Subp, N);
5612 Resolve_Entity_Name (N, Typ);
5614 -- Avoid validation, since it is a static function call
5616 Generate_Reference (Nam, Subp);
5620 -- If the subprogram is not global, then kill all saved values and
5621 -- checks. This is a bit conservative, since in many cases we could do
5622 -- better, but it is not worth the effort. Similarly, we kill constant
5623 -- values. However we do not need to do this for internal entities
5624 -- (unless they are inherited user-defined subprograms), since they
5625 -- are not in the business of molesting local values.
5627 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5628 -- kill all checks and values for calls to global subprograms. This
5629 -- takes care of the case where an access to a local subprogram is
5630 -- taken, and could be passed directly or indirectly and then called
5631 -- from almost any context.
5633 -- Note: we do not do this step till after resolving the actuals. That
5634 -- way we still take advantage of the current value information while
5635 -- scanning the actuals.
5637 -- We suppress killing values if we are processing the nodes associated
5638 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5639 -- type kills all the values as part of analyzing the code that
5640 -- initializes the dispatch tables.
5642 if Inside_Freezing_Actions = 0
5643 and then (not Is_Library_Level_Entity (Nam)
5644 or else Suppress_Value_Tracking_On_Call
5645 (Nearest_Dynamic_Scope (Current_Scope)))
5646 and then (Comes_From_Source (Nam)
5647 or else (Present (Alias (Nam))
5648 and then Comes_From_Source (Alias (Nam))))
5650 Kill_Current_Values;
5653 -- If we are warning about unread OUT parameters, this is the place to
5654 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5655 -- after the above call to Kill_Current_Values (since that call clears
5656 -- the Last_Assignment field of all local variables).
5658 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5659 and then Comes_From_Source (N)
5660 and then In_Extended_Main_Source_Unit (N)
5667 F := First_Formal (Nam);
5668 A := First_Actual (N);
5669 while Present (F) and then Present (A) loop
5670 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5671 and then Warn_On_Modified_As_Out_Parameter (F)
5672 and then Is_Entity_Name (A)
5673 and then Present (Entity (A))
5674 and then Comes_From_Source (N)
5675 and then Safe_To_Capture_Value (N, Entity (A))
5677 Set_Last_Assignment (Entity (A), A);
5686 -- If the subprogram is a primitive operation, check whether or not
5687 -- it is a correct dispatching call.
5689 if Is_Overloadable (Nam)
5690 and then Is_Dispatching_Operation (Nam)
5692 Check_Dispatching_Call (N);
5694 elsif Ekind (Nam) /= E_Subprogram_Type
5695 and then Is_Abstract_Subprogram (Nam)
5696 and then not In_Instance
5698 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5701 -- If this is a dispatching call, generate the appropriate reference,
5702 -- for better source navigation in GPS.
5704 if Is_Overloadable (Nam)
5705 and then Present (Controlling_Argument (N))
5707 Generate_Reference (Nam, Subp, 'R');
5709 -- Normal case, not a dispatching call: generate a call reference
5712 Generate_Reference (Nam, Subp, 's');
5715 if Is_Intrinsic_Subprogram (Nam) then
5716 Check_Intrinsic_Call (N);
5719 -- Check for violation of restriction No_Specific_Termination_Handlers
5720 -- and warn on a potentially blocking call to Abort_Task.
5722 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5723 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5725 Is_RTE (Nam, RE_Specific_Handler))
5727 Check_Restriction (No_Specific_Termination_Handlers, N);
5729 elsif Is_RTE (Nam, RE_Abort_Task) then
5730 Check_Potentially_Blocking_Operation (N);
5733 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5734 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5735 -- need to check the second argument to determine whether it is an
5736 -- absolute or relative timing event.
5738 if Restriction_Check_Required (No_Relative_Delay)
5739 and then Is_RTE (Nam, RE_Set_Handler)
5740 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5742 Check_Restriction (No_Relative_Delay, N);
5745 -- Issue an error for a call to an eliminated subprogram. We skip this
5746 -- in a spec expression, e.g. a call in a default parameter value, since
5747 -- we are not really doing a call at this time. That's important because
5748 -- the spec expression may itself belong to an eliminated subprogram.
5750 if not In_Spec_Expression then
5751 Check_For_Eliminated_Subprogram (Subp, Nam);
5754 -- In formal mode, the primitive operations of a tagged type or type
5755 -- extension do not include functions that return the tagged type.
5757 -- Commented out as the call to Is_Inherited_Operation_For_Type may
5758 -- cause an error because the type entity of the parent node of
5759 -- Entity (Name (N) may not be set. ???
5760 -- So why not just add a guard ???
5762 -- if Nkind (N) = N_Function_Call
5763 -- and then Is_Tagged_Type (Etype (N))
5764 -- and then Is_Entity_Name (Name (N))
5765 -- and then Is_Inherited_Operation_For_Type
5766 -- (Entity (Name (N)), Etype (N))
5768 -- Check_SPARK_Restriction ("function not inherited", N);
5771 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5772 -- class-wide and the call dispatches on result in a context that does
5773 -- not provide a tag, the call raises Program_Error.
5775 if Nkind (N) = N_Function_Call
5776 and then In_Instance
5777 and then Is_Generic_Actual_Type (Typ)
5778 and then Is_Class_Wide_Type (Typ)
5779 and then Has_Controlling_Result (Nam)
5780 and then Nkind (Parent (N)) = N_Object_Declaration
5782 -- Verify that none of the formals are controlling
5785 Call_OK : Boolean := False;
5789 F := First_Formal (Nam);
5790 while Present (F) loop
5791 if Is_Controlling_Formal (F) then
5800 Error_Msg_N ("!? cannot determine tag of result", N);
5801 Error_Msg_N ("!? Program_Error will be raised", N);
5803 Make_Raise_Program_Error (Sloc (N),
5804 Reason => PE_Explicit_Raise));
5809 -- All done, evaluate call and deal with elaboration issues
5812 Check_Elab_Call (N);
5813 Warn_On_Overlapping_Actuals (Nam, N);
5816 -----------------------------
5817 -- Resolve_Case_Expression --
5818 -----------------------------
5820 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5824 Alt := First (Alternatives (N));
5825 while Present (Alt) loop
5826 Resolve (Expression (Alt), Typ);
5831 Eval_Case_Expression (N);
5832 end Resolve_Case_Expression;
5834 -------------------------------
5835 -- Resolve_Character_Literal --
5836 -------------------------------
5838 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5839 B_Typ : constant Entity_Id := Base_Type (Typ);
5843 -- Verify that the character does belong to the type of the context
5845 Set_Etype (N, B_Typ);
5846 Eval_Character_Literal (N);
5848 -- Wide_Wide_Character literals must always be defined, since the set
5849 -- of wide wide character literals is complete, i.e. if a character
5850 -- literal is accepted by the parser, then it is OK for wide wide
5851 -- character (out of range character literals are rejected).
5853 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5856 -- Always accept character literal for type Any_Character, which
5857 -- occurs in error situations and in comparisons of literals, both
5858 -- of which should accept all literals.
5860 elsif B_Typ = Any_Character then
5863 -- For Standard.Character or a type derived from it, check that the
5864 -- literal is in range.
5866 elsif Root_Type (B_Typ) = Standard_Character then
5867 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5871 -- For Standard.Wide_Character or a type derived from it, check that the
5872 -- literal is in range.
5874 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5875 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5879 -- For Standard.Wide_Wide_Character or a type derived from it, we
5880 -- know the literal is in range, since the parser checked!
5882 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5885 -- If the entity is already set, this has already been resolved in a
5886 -- generic context, or comes from expansion. Nothing else to do.
5888 elsif Present (Entity (N)) then
5891 -- Otherwise we have a user defined character type, and we can use the
5892 -- standard visibility mechanisms to locate the referenced entity.
5895 C := Current_Entity (N);
5896 while Present (C) loop
5897 if Etype (C) = B_Typ then
5898 Set_Entity_With_Style_Check (N, C);
5899 Generate_Reference (C, N);
5907 -- If we fall through, then the literal does not match any of the
5908 -- entries of the enumeration type. This isn't just a constraint error
5909 -- situation, it is an illegality (see RM 4.2).
5912 ("character not defined for }", N, First_Subtype (B_Typ));
5913 end Resolve_Character_Literal;
5915 ---------------------------
5916 -- Resolve_Comparison_Op --
5917 ---------------------------
5919 -- Context requires a boolean type, and plays no role in resolution.
5920 -- Processing identical to that for equality operators. The result type is
5921 -- the base type, which matters when pathological subtypes of booleans with
5922 -- limited ranges are used.
5924 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5925 L : constant Node_Id := Left_Opnd (N);
5926 R : constant Node_Id := Right_Opnd (N);
5930 -- If this is an intrinsic operation which is not predefined, use the
5931 -- types of its declared arguments to resolve the possibly overloaded
5932 -- operands. Otherwise the operands are unambiguous and specify the
5935 if Scope (Entity (N)) /= Standard_Standard then
5936 T := Etype (First_Entity (Entity (N)));
5939 T := Find_Unique_Type (L, R);
5941 if T = Any_Fixed then
5942 T := Unique_Fixed_Point_Type (L);
5946 Set_Etype (N, Base_Type (Typ));
5947 Generate_Reference (T, N, ' ');
5949 -- Skip remaining processing if already set to Any_Type
5951 if T = Any_Type then
5955 -- Deal with other error cases
5957 if T = Any_String or else
5958 T = Any_Composite or else
5961 if T = Any_Character then
5962 Ambiguous_Character (L);
5964 Error_Msg_N ("ambiguous operands for comparison", N);
5967 Set_Etype (N, Any_Type);
5971 -- Resolve the operands if types OK
5975 Check_Unset_Reference (L);
5976 Check_Unset_Reference (R);
5977 Generate_Operator_Reference (N, T);
5978 Check_Low_Bound_Tested (N);
5980 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
5981 -- types or array types except String.
5983 if Is_Boolean_Type (T) then
5984 Check_SPARK_Restriction
5985 ("comparison is not defined on Boolean type", N);
5987 elsif Is_Array_Type (T)
5988 and then Base_Type (T) /= Standard_String
5990 Check_SPARK_Restriction
5991 ("comparison is not defined on array types other than String", N);
5994 -- Check comparison on unordered enumeration
5996 if Comes_From_Source (N)
5997 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
5999 Error_Msg_N ("comparison on unordered enumeration type?", N);
6002 -- Evaluate the relation (note we do this after the above check since
6003 -- this Eval call may change N to True/False.
6005 Eval_Relational_Op (N);
6006 end Resolve_Comparison_Op;
6008 ------------------------------------
6009 -- Resolve_Conditional_Expression --
6010 ------------------------------------
6012 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
6013 Condition : constant Node_Id := First (Expressions (N));
6014 Then_Expr : constant Node_Id := Next (Condition);
6015 Else_Expr : Node_Id := Next (Then_Expr);
6018 Resolve (Condition, Any_Boolean);
6019 Resolve (Then_Expr, Typ);
6021 -- If ELSE expression present, just resolve using the determined type
6023 if Present (Else_Expr) then
6024 Resolve (Else_Expr, Typ);
6026 -- If no ELSE expression is present, root type must be Standard.Boolean
6027 -- and we provide a Standard.True result converted to the appropriate
6028 -- Boolean type (in case it is a derived boolean type).
6030 elsif Root_Type (Typ) = Standard_Boolean then
6032 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
6033 Analyze_And_Resolve (Else_Expr, Typ);
6034 Append_To (Expressions (N), Else_Expr);
6037 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
6038 Append_To (Expressions (N), Error);
6042 Eval_Conditional_Expression (N);
6043 end Resolve_Conditional_Expression;
6045 -----------------------------------------
6046 -- Resolve_Discrete_Subtype_Indication --
6047 -----------------------------------------
6049 procedure Resolve_Discrete_Subtype_Indication
6057 Analyze (Subtype_Mark (N));
6058 S := Entity (Subtype_Mark (N));
6060 if Nkind (Constraint (N)) /= N_Range_Constraint then
6061 Error_Msg_N ("expect range constraint for discrete type", N);
6062 Set_Etype (N, Any_Type);
6065 R := Range_Expression (Constraint (N));
6073 if Base_Type (S) /= Base_Type (Typ) then
6075 ("expect subtype of }", N, First_Subtype (Typ));
6077 -- Rewrite the constraint as a range of Typ
6078 -- to allow compilation to proceed further.
6081 Rewrite (Low_Bound (R),
6082 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6083 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6084 Attribute_Name => Name_First));
6085 Rewrite (High_Bound (R),
6086 Make_Attribute_Reference (Sloc (High_Bound (R)),
6087 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6088 Attribute_Name => Name_First));
6092 Set_Etype (N, Etype (R));
6094 -- Additionally, we must check that the bounds are compatible
6095 -- with the given subtype, which might be different from the
6096 -- type of the context.
6098 Apply_Range_Check (R, S);
6100 -- ??? If the above check statically detects a Constraint_Error
6101 -- it replaces the offending bound(s) of the range R with a
6102 -- Constraint_Error node. When the itype which uses these bounds
6103 -- is frozen the resulting call to Duplicate_Subexpr generates
6104 -- a new temporary for the bounds.
6106 -- Unfortunately there are other itypes that are also made depend
6107 -- on these bounds, so when Duplicate_Subexpr is called they get
6108 -- a forward reference to the newly created temporaries and Gigi
6109 -- aborts on such forward references. This is probably sign of a
6110 -- more fundamental problem somewhere else in either the order of
6111 -- itype freezing or the way certain itypes are constructed.
6113 -- To get around this problem we call Remove_Side_Effects right
6114 -- away if either bounds of R are a Constraint_Error.
6117 L : constant Node_Id := Low_Bound (R);
6118 H : constant Node_Id := High_Bound (R);
6121 if Nkind (L) = N_Raise_Constraint_Error then
6122 Remove_Side_Effects (L);
6125 if Nkind (H) = N_Raise_Constraint_Error then
6126 Remove_Side_Effects (H);
6130 Check_Unset_Reference (Low_Bound (R));
6131 Check_Unset_Reference (High_Bound (R));
6134 end Resolve_Discrete_Subtype_Indication;
6136 -------------------------
6137 -- Resolve_Entity_Name --
6138 -------------------------
6140 -- Used to resolve identifiers and expanded names
6142 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6143 E : constant Entity_Id := Entity (N);
6146 -- If garbage from errors, set to Any_Type and return
6148 if No (E) and then Total_Errors_Detected /= 0 then
6149 Set_Etype (N, Any_Type);
6153 -- Replace named numbers by corresponding literals. Note that this is
6154 -- the one case where Resolve_Entity_Name must reset the Etype, since
6155 -- it is currently marked as universal.
6157 if Ekind (E) = E_Named_Integer then
6159 Eval_Named_Integer (N);
6161 elsif Ekind (E) = E_Named_Real then
6163 Eval_Named_Real (N);
6165 -- For enumeration literals, we need to make sure that a proper style
6166 -- check is done, since such literals are overloaded, and thus we did
6167 -- not do a style check during the first phase of analysis.
6169 elsif Ekind (E) = E_Enumeration_Literal then
6170 Set_Entity_With_Style_Check (N, E);
6171 Eval_Entity_Name (N);
6173 -- Case of subtype name appearing as an operand in expression
6175 elsif Is_Type (E) then
6177 -- Allow use of subtype if it is a concurrent type where we are
6178 -- currently inside the body. This will eventually be expanded into a
6179 -- call to Self (for tasks) or _object (for protected objects). Any
6180 -- other use of a subtype is invalid.
6182 if Is_Concurrent_Type (E)
6183 and then In_Open_Scopes (E)
6187 -- Any other use is an error
6191 ("invalid use of subtype mark in expression or call", N);
6194 -- Check discriminant use if entity is discriminant in current scope,
6195 -- i.e. discriminant of record or concurrent type currently being
6196 -- analyzed. Uses in corresponding body are unrestricted.
6198 elsif Ekind (E) = E_Discriminant
6199 and then Scope (E) = Current_Scope
6200 and then not Has_Completion (Current_Scope)
6202 Check_Discriminant_Use (N);
6204 -- A parameterless generic function cannot appear in a context that
6205 -- requires resolution.
6207 elsif Ekind (E) = E_Generic_Function then
6208 Error_Msg_N ("illegal use of generic function", N);
6210 elsif Ekind (E) = E_Out_Parameter
6211 and then Ada_Version = Ada_83
6212 and then (Nkind (Parent (N)) in N_Op
6213 or else (Nkind (Parent (N)) = N_Assignment_Statement
6214 and then N = Expression (Parent (N)))
6215 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6217 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6219 -- In all other cases, just do the possible static evaluation
6222 -- A deferred constant that appears in an expression must have a
6223 -- completion, unless it has been removed by in-place expansion of
6226 if Ekind (E) = E_Constant
6227 and then Comes_From_Source (E)
6228 and then No (Constant_Value (E))
6229 and then Is_Frozen (Etype (E))
6230 and then not In_Spec_Expression
6231 and then not Is_Imported (E)
6233 if No_Initialization (Parent (E))
6234 or else (Present (Full_View (E))
6235 and then No_Initialization (Parent (Full_View (E))))
6240 "deferred constant is frozen before completion", N);
6244 Eval_Entity_Name (N);
6246 end Resolve_Entity_Name;
6252 procedure Resolve_Entry (Entry_Name : Node_Id) is
6253 Loc : constant Source_Ptr := Sloc (Entry_Name);
6261 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6262 -- If the bounds of the entry family being called depend on task
6263 -- discriminants, build a new index subtype where a discriminant is
6264 -- replaced with the value of the discriminant of the target task.
6265 -- The target task is the prefix of the entry name in the call.
6267 -----------------------
6268 -- Actual_Index_Type --
6269 -----------------------
6271 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6272 Typ : constant Entity_Id := Entry_Index_Type (E);
6273 Tsk : constant Entity_Id := Scope (E);
6274 Lo : constant Node_Id := Type_Low_Bound (Typ);
6275 Hi : constant Node_Id := Type_High_Bound (Typ);
6278 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6279 -- If the bound is given by a discriminant, replace with a reference
6280 -- to the discriminant of the same name in the target task. If the
6281 -- entry name is the target of a requeue statement and the entry is
6282 -- in the current protected object, the bound to be used is the
6283 -- discriminal of the object (see Apply_Range_Checks for details of
6284 -- the transformation).
6286 -----------------------------
6287 -- Actual_Discriminant_Ref --
6288 -----------------------------
6290 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6291 Typ : constant Entity_Id := Etype (Bound);
6295 Remove_Side_Effects (Bound);
6297 if not Is_Entity_Name (Bound)
6298 or else Ekind (Entity (Bound)) /= E_Discriminant
6302 elsif Is_Protected_Type (Tsk)
6303 and then In_Open_Scopes (Tsk)
6304 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6306 -- Note: here Bound denotes a discriminant of the corresponding
6307 -- record type tskV, whose discriminal is a formal of the
6308 -- init-proc tskVIP. What we want is the body discriminal,
6309 -- which is associated to the discriminant of the original
6310 -- concurrent type tsk.
6312 return New_Occurrence_Of
6313 (Find_Body_Discriminal (Entity (Bound)), Loc);
6317 Make_Selected_Component (Loc,
6318 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6319 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6324 end Actual_Discriminant_Ref;
6326 -- Start of processing for Actual_Index_Type
6329 if not Has_Discriminants (Tsk)
6330 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6332 return Entry_Index_Type (E);
6335 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6336 Set_Etype (New_T, Base_Type (Typ));
6337 Set_Size_Info (New_T, Typ);
6338 Set_RM_Size (New_T, RM_Size (Typ));
6339 Set_Scalar_Range (New_T,
6340 Make_Range (Sloc (Entry_Name),
6341 Low_Bound => Actual_Discriminant_Ref (Lo),
6342 High_Bound => Actual_Discriminant_Ref (Hi)));
6346 end Actual_Index_Type;
6348 -- Start of processing of Resolve_Entry
6351 -- Find name of entry being called, and resolve prefix of name with its
6352 -- own type. The prefix can be overloaded, and the name and signature of
6353 -- the entry must be taken into account.
6355 if Nkind (Entry_Name) = N_Indexed_Component then
6357 -- Case of dealing with entry family within the current tasks
6359 E_Name := Prefix (Entry_Name);
6362 E_Name := Entry_Name;
6365 if Is_Entity_Name (E_Name) then
6367 -- Entry call to an entry (or entry family) in the current task. This
6368 -- is legal even though the task will deadlock. Rewrite as call to
6371 -- This can also be a call to an entry in an enclosing task. If this
6372 -- is a single task, we have to retrieve its name, because the scope
6373 -- of the entry is the task type, not the object. If the enclosing
6374 -- task is a task type, the identity of the task is given by its own
6377 -- Finally this can be a requeue on an entry of the same task or
6378 -- protected object.
6380 S := Scope (Entity (E_Name));
6382 for J in reverse 0 .. Scope_Stack.Last loop
6383 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6384 and then not Comes_From_Source (S)
6386 -- S is an enclosing task or protected object. The concurrent
6387 -- declaration has been converted into a type declaration, and
6388 -- the object itself has an object declaration that follows
6389 -- the type in the same declarative part.
6391 Tsk := Next_Entity (S);
6392 while Etype (Tsk) /= S loop
6399 elsif S = Scope_Stack.Table (J).Entity then
6401 -- Call to current task. Will be transformed into call to Self
6409 Make_Selected_Component (Loc,
6410 Prefix => New_Occurrence_Of (S, Loc),
6412 New_Occurrence_Of (Entity (E_Name), Loc));
6413 Rewrite (E_Name, New_N);
6416 elsif Nkind (Entry_Name) = N_Selected_Component
6417 and then Is_Overloaded (Prefix (Entry_Name))
6419 -- Use the entry name (which must be unique at this point) to find
6420 -- the prefix that returns the corresponding task/protected type.
6423 Pref : constant Node_Id := Prefix (Entry_Name);
6424 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6429 Get_First_Interp (Pref, I, It);
6430 while Present (It.Typ) loop
6431 if Scope (Ent) = It.Typ then
6432 Set_Etype (Pref, It.Typ);
6436 Get_Next_Interp (I, It);
6441 if Nkind (Entry_Name) = N_Selected_Component then
6442 Resolve (Prefix (Entry_Name));
6444 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6445 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6446 Resolve (Prefix (Prefix (Entry_Name)));
6447 Index := First (Expressions (Entry_Name));
6448 Resolve (Index, Entry_Index_Type (Nam));
6450 -- Up to this point the expression could have been the actual in a
6451 -- simple entry call, and be given by a named association.
6453 if Nkind (Index) = N_Parameter_Association then
6454 Error_Msg_N ("expect expression for entry index", Index);
6456 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6461 ------------------------
6462 -- Resolve_Entry_Call --
6463 ------------------------
6465 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6466 Entry_Name : constant Node_Id := Name (N);
6467 Loc : constant Source_Ptr := Sloc (Entry_Name);
6469 First_Named : Node_Id;
6476 -- We kill all checks here, because it does not seem worth the effort to
6477 -- do anything better, an entry call is a big operation.
6481 -- Processing of the name is similar for entry calls and protected
6482 -- operation calls. Once the entity is determined, we can complete
6483 -- the resolution of the actuals.
6485 -- The selector may be overloaded, in the case of a protected object
6486 -- with overloaded functions. The type of the context is used for
6489 if Nkind (Entry_Name) = N_Selected_Component
6490 and then Is_Overloaded (Selector_Name (Entry_Name))
6491 and then Typ /= Standard_Void_Type
6498 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6499 while Present (It.Typ) loop
6500 if Covers (Typ, It.Typ) then
6501 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6502 Set_Etype (Entry_Name, It.Typ);
6504 Generate_Reference (It.Typ, N, ' ');
6507 Get_Next_Interp (I, It);
6512 Resolve_Entry (Entry_Name);
6514 if Nkind (Entry_Name) = N_Selected_Component then
6516 -- Simple entry call
6518 Nam := Entity (Selector_Name (Entry_Name));
6519 Obj := Prefix (Entry_Name);
6520 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6522 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6524 -- Call to member of entry family
6526 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6527 Obj := Prefix (Prefix (Entry_Name));
6528 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6531 -- We cannot in general check the maximum depth of protected entry calls
6532 -- at compile time. But we can tell that any protected entry call at all
6533 -- violates a specified nesting depth of zero.
6535 if Is_Protected_Type (Scope (Nam)) then
6536 Check_Restriction (Max_Entry_Queue_Length, N);
6539 -- Use context type to disambiguate a protected function that can be
6540 -- called without actuals and that returns an array type, and where the
6541 -- argument list may be an indexing of the returned value.
6543 if Ekind (Nam) = E_Function
6544 and then Needs_No_Actuals (Nam)
6545 and then Present (Parameter_Associations (N))
6547 ((Is_Array_Type (Etype (Nam))
6548 and then Covers (Typ, Component_Type (Etype (Nam))))
6550 or else (Is_Access_Type (Etype (Nam))
6551 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6555 Component_Type (Designated_Type (Etype (Nam))))))
6558 Index_Node : Node_Id;
6562 Make_Indexed_Component (Loc,
6564 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6565 Expressions => Parameter_Associations (N));
6567 -- Since we are correcting a node classification error made by the
6568 -- parser, we call Replace rather than Rewrite.
6570 Replace (N, Index_Node);
6571 Set_Etype (Prefix (N), Etype (Nam));
6573 Resolve_Indexed_Component (N, Typ);
6578 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6579 and then Present (PPC_Wrapper (Nam))
6580 and then Current_Scope /= PPC_Wrapper (Nam)
6582 -- Rewrite as call to the precondition wrapper, adding the task
6583 -- object to the list of actuals. If the call is to a member of an
6584 -- entry family, include the index as well.
6588 New_Actuals : List_Id;
6591 New_Actuals := New_List (Obj);
6593 if Nkind (Entry_Name) = N_Indexed_Component then
6594 Append_To (New_Actuals,
6595 New_Copy_Tree (First (Expressions (Entry_Name))));
6598 Append_List (Parameter_Associations (N), New_Actuals);
6600 Make_Procedure_Call_Statement (Loc,
6602 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6603 Parameter_Associations => New_Actuals);
6604 Rewrite (N, New_Call);
6605 Analyze_And_Resolve (N);
6610 -- The operation name may have been overloaded. Order the actuals
6611 -- according to the formals of the resolved entity, and set the return
6612 -- type to that of the operation.
6615 Normalize_Actuals (N, Nam, False, Norm_OK);
6616 pragma Assert (Norm_OK);
6617 Set_Etype (N, Etype (Nam));
6620 Resolve_Actuals (N, Nam);
6622 -- Create a call reference to the entry
6624 Generate_Reference (Nam, Entry_Name, 's');
6626 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6627 Check_Potentially_Blocking_Operation (N);
6630 -- Verify that a procedure call cannot masquerade as an entry
6631 -- call where an entry call is expected.
6633 if Ekind (Nam) = E_Procedure then
6634 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6635 and then N = Entry_Call_Statement (Parent (N))
6637 Error_Msg_N ("entry call required in select statement", N);
6639 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6640 and then N = Triggering_Statement (Parent (N))
6642 Error_Msg_N ("triggering statement cannot be procedure call", N);
6644 elsif Ekind (Scope (Nam)) = E_Task_Type
6645 and then not In_Open_Scopes (Scope (Nam))
6647 Error_Msg_N ("task has no entry with this name", Entry_Name);
6651 -- After resolution, entry calls and protected procedure calls are
6652 -- changed into entry calls, for expansion. The structure of the node
6653 -- does not change, so it can safely be done in place. Protected
6654 -- function calls must keep their structure because they are
6657 if Ekind (Nam) /= E_Function then
6659 -- A protected operation that is not a function may modify the
6660 -- corresponding object, and cannot apply to a constant. If this
6661 -- is an internal call, the prefix is the type itself.
6663 if Is_Protected_Type (Scope (Nam))
6664 and then not Is_Variable (Obj)
6665 and then (not Is_Entity_Name (Obj)
6666 or else not Is_Type (Entity (Obj)))
6669 ("prefix of protected procedure or entry call must be variable",
6673 Actuals := Parameter_Associations (N);
6674 First_Named := First_Named_Actual (N);
6677 Make_Entry_Call_Statement (Loc,
6679 Parameter_Associations => Actuals));
6681 Set_First_Named_Actual (N, First_Named);
6682 Set_Analyzed (N, True);
6684 -- Protected functions can return on the secondary stack, in which
6685 -- case we must trigger the transient scope mechanism.
6687 elsif Full_Expander_Active
6688 and then Requires_Transient_Scope (Etype (Nam))
6690 Establish_Transient_Scope (N, Sec_Stack => True);
6692 end Resolve_Entry_Call;
6694 -------------------------
6695 -- Resolve_Equality_Op --
6696 -------------------------
6698 -- Both arguments must have the same type, and the boolean context does
6699 -- not participate in the resolution. The first pass verifies that the
6700 -- interpretation is not ambiguous, and the type of the left argument is
6701 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6702 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6703 -- though they carry a single (universal) type. Diagnose this case here.
6705 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6706 L : constant Node_Id := Left_Opnd (N);
6707 R : constant Node_Id := Right_Opnd (N);
6708 T : Entity_Id := Find_Unique_Type (L, R);
6710 procedure Check_Conditional_Expression (Cond : Node_Id);
6711 -- The resolution rule for conditional expressions requires that each
6712 -- such must have a unique type. This means that if several dependent
6713 -- expressions are of a non-null anonymous access type, and the context
6714 -- does not impose an expected type (as can be the case in an equality
6715 -- operation) the expression must be rejected.
6717 function Find_Unique_Access_Type return Entity_Id;
6718 -- In the case of allocators, make a last-ditch attempt to find a single
6719 -- access type with the right designated type. This is semantically
6720 -- dubious, and of no interest to any real code, but c48008a makes it
6723 ----------------------------------
6724 -- Check_Conditional_Expression --
6725 ----------------------------------
6727 procedure Check_Conditional_Expression (Cond : Node_Id) is
6728 Then_Expr : Node_Id;
6729 Else_Expr : Node_Id;
6732 if Nkind (Cond) = N_Conditional_Expression then
6733 Then_Expr := Next (First (Expressions (Cond)));
6734 Else_Expr := Next (Then_Expr);
6736 if Nkind (Then_Expr) /= N_Null
6737 and then Nkind (Else_Expr) /= N_Null
6740 ("cannot determine type of conditional expression", Cond);
6743 end Check_Conditional_Expression;
6745 -----------------------------
6746 -- Find_Unique_Access_Type --
6747 -----------------------------
6749 function Find_Unique_Access_Type return Entity_Id is
6755 if Ekind (Etype (R)) = E_Allocator_Type then
6756 Acc := Designated_Type (Etype (R));
6757 elsif Ekind (Etype (L)) = E_Allocator_Type then
6758 Acc := Designated_Type (Etype (L));
6764 while S /= Standard_Standard loop
6765 E := First_Entity (S);
6766 while Present (E) loop
6768 and then Is_Access_Type (E)
6769 and then Ekind (E) /= E_Allocator_Type
6770 and then Designated_Type (E) = Base_Type (Acc)
6782 end Find_Unique_Access_Type;
6784 -- Start of processing for Resolve_Equality_Op
6787 Set_Etype (N, Base_Type (Typ));
6788 Generate_Reference (T, N, ' ');
6790 if T = Any_Fixed then
6791 T := Unique_Fixed_Point_Type (L);
6794 if T /= Any_Type then
6795 if T = Any_String or else
6796 T = Any_Composite or else
6799 if T = Any_Character then
6800 Ambiguous_Character (L);
6802 Error_Msg_N ("ambiguous operands for equality", N);
6805 Set_Etype (N, Any_Type);
6808 elsif T = Any_Access
6809 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6811 T := Find_Unique_Access_Type;
6814 Error_Msg_N ("ambiguous operands for equality", N);
6815 Set_Etype (N, Any_Type);
6819 -- Conditional expressions must have a single type, and if the
6820 -- context does not impose one the dependent expressions cannot
6821 -- be anonymous access types.
6823 elsif Ada_Version >= Ada_2012
6824 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6825 E_Anonymous_Access_Subprogram_Type)
6826 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6827 E_Anonymous_Access_Subprogram_Type)
6829 Check_Conditional_Expression (L);
6830 Check_Conditional_Expression (R);
6836 -- In SPARK, equality operators = and /= for array types other than
6837 -- String are only defined when, for each index position, the
6838 -- operands have equal static bounds.
6840 if Is_Array_Type (T) then
6841 -- Protect call to Matching_Static_Array_Bounds to avoid costly
6842 -- operation if not needed.
6844 if Restriction_Check_Required (SPARK)
6845 and then Base_Type (T) /= Standard_String
6846 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6847 and then Etype (L) /= Any_Composite -- or else L in error
6848 and then Etype (R) /= Any_Composite -- or else R in error
6849 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6851 Check_SPARK_Restriction
6852 ("array types should have matching static bounds", N);
6856 -- If the unique type is a class-wide type then it will be expanded
6857 -- into a dispatching call to the predefined primitive. Therefore we
6858 -- check here for potential violation of such restriction.
6860 if Is_Class_Wide_Type (T) then
6861 Check_Restriction (No_Dispatching_Calls, N);
6864 if Warn_On_Redundant_Constructs
6865 and then Comes_From_Source (N)
6866 and then Is_Entity_Name (R)
6867 and then Entity (R) = Standard_True
6868 and then Comes_From_Source (R)
6870 Error_Msg_N -- CODEFIX
6871 ("?comparison with True is redundant!", R);
6874 Check_Unset_Reference (L);
6875 Check_Unset_Reference (R);
6876 Generate_Operator_Reference (N, T);
6877 Check_Low_Bound_Tested (N);
6879 -- If this is an inequality, it may be the implicit inequality
6880 -- created for a user-defined operation, in which case the corres-
6881 -- ponding equality operation is not intrinsic, and the operation
6882 -- cannot be constant-folded. Else fold.
6884 if Nkind (N) = N_Op_Eq
6885 or else Comes_From_Source (Entity (N))
6886 or else Ekind (Entity (N)) = E_Operator
6887 or else Is_Intrinsic_Subprogram
6888 (Corresponding_Equality (Entity (N)))
6890 Eval_Relational_Op (N);
6892 elsif Nkind (N) = N_Op_Ne
6893 and then Is_Abstract_Subprogram (Entity (N))
6895 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6898 -- Ada 2005: If one operand is an anonymous access type, convert the
6899 -- other operand to it, to ensure that the underlying types match in
6900 -- the back-end. Same for access_to_subprogram, and the conversion
6901 -- verifies that the types are subtype conformant.
6903 -- We apply the same conversion in the case one of the operands is a
6904 -- private subtype of the type of the other.
6906 -- Why the Expander_Active test here ???
6908 if Full_Expander_Active
6910 (Ekind_In (T, E_Anonymous_Access_Type,
6911 E_Anonymous_Access_Subprogram_Type)
6912 or else Is_Private_Type (T))
6914 if Etype (L) /= T then
6916 Make_Unchecked_Type_Conversion (Sloc (L),
6917 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6918 Expression => Relocate_Node (L)));
6919 Analyze_And_Resolve (L, T);
6922 if (Etype (R)) /= T then
6924 Make_Unchecked_Type_Conversion (Sloc (R),
6925 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6926 Expression => Relocate_Node (R)));
6927 Analyze_And_Resolve (R, T);
6931 end Resolve_Equality_Op;
6933 ----------------------------------
6934 -- Resolve_Explicit_Dereference --
6935 ----------------------------------
6937 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6938 Loc : constant Source_Ptr := Sloc (N);
6940 P : constant Node_Id := Prefix (N);
6945 Check_Fully_Declared_Prefix (Typ, P);
6947 if Is_Overloaded (P) then
6949 -- Use the context type to select the prefix that has the correct
6952 Get_First_Interp (P, I, It);
6953 while Present (It.Typ) loop
6954 exit when Is_Access_Type (It.Typ)
6955 and then Covers (Typ, Designated_Type (It.Typ));
6956 Get_Next_Interp (I, It);
6959 if Present (It.Typ) then
6960 Resolve (P, It.Typ);
6962 -- If no interpretation covers the designated type of the prefix,
6963 -- this is the pathological case where not all implementations of
6964 -- the prefix allow the interpretation of the node as a call. Now
6965 -- that the expected type is known, Remove other interpretations
6966 -- from prefix, rewrite it as a call, and resolve again, so that
6967 -- the proper call node is generated.
6969 Get_First_Interp (P, I, It);
6970 while Present (It.Typ) loop
6971 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6975 Get_Next_Interp (I, It);
6979 Make_Function_Call (Loc,
6981 Make_Explicit_Dereference (Loc,
6983 Parameter_Associations => New_List);
6985 Save_Interps (N, New_N);
6987 Analyze_And_Resolve (N, Typ);
6991 Set_Etype (N, Designated_Type (It.Typ));
6997 if Is_Access_Type (Etype (P)) then
6998 Apply_Access_Check (N);
7001 -- If the designated type is a packed unconstrained array type, and the
7002 -- explicit dereference is not in the context of an attribute reference,
7003 -- then we must compute and set the actual subtype, since it is needed
7004 -- by Gigi. The reason we exclude the attribute case is that this is
7005 -- handled fine by Gigi, and in fact we use such attributes to build the
7006 -- actual subtype. We also exclude generated code (which builds actual
7007 -- subtypes directly if they are needed).
7009 if Is_Array_Type (Etype (N))
7010 and then Is_Packed (Etype (N))
7011 and then not Is_Constrained (Etype (N))
7012 and then Nkind (Parent (N)) /= N_Attribute_Reference
7013 and then Comes_From_Source (N)
7015 Set_Etype (N, Get_Actual_Subtype (N));
7018 -- Note: No Eval processing is required for an explicit dereference,
7019 -- because such a name can never be static.
7021 end Resolve_Explicit_Dereference;
7023 -------------------------------------
7024 -- Resolve_Expression_With_Actions --
7025 -------------------------------------
7027 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7030 end Resolve_Expression_With_Actions;
7032 -------------------------------
7033 -- Resolve_Indexed_Component --
7034 -------------------------------
7036 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7037 Name : constant Node_Id := Prefix (N);
7039 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7043 if Is_Overloaded (Name) then
7045 -- Use the context type to select the prefix that yields the correct
7051 I1 : Interp_Index := 0;
7052 P : constant Node_Id := Prefix (N);
7053 Found : Boolean := False;
7056 Get_First_Interp (P, I, It);
7057 while Present (It.Typ) loop
7058 if (Is_Array_Type (It.Typ)
7059 and then Covers (Typ, Component_Type (It.Typ)))
7060 or else (Is_Access_Type (It.Typ)
7061 and then Is_Array_Type (Designated_Type (It.Typ))
7065 Component_Type (Designated_Type (It.Typ))))
7068 It := Disambiguate (P, I1, I, Any_Type);
7070 if It = No_Interp then
7071 Error_Msg_N ("ambiguous prefix for indexing", N);
7077 Array_Type := It.Typ;
7083 Array_Type := It.Typ;
7088 Get_Next_Interp (I, It);
7093 Array_Type := Etype (Name);
7096 Resolve (Name, Array_Type);
7097 Array_Type := Get_Actual_Subtype_If_Available (Name);
7099 -- If prefix is access type, dereference to get real array type.
7100 -- Note: we do not apply an access check because the expander always
7101 -- introduces an explicit dereference, and the check will happen there.
7103 if Is_Access_Type (Array_Type) then
7104 Array_Type := Designated_Type (Array_Type);
7107 -- If name was overloaded, set component type correctly now
7108 -- If a misplaced call to an entry family (which has no index types)
7109 -- return. Error will be diagnosed from calling context.
7111 if Is_Array_Type (Array_Type) then
7112 Set_Etype (N, Component_Type (Array_Type));
7117 Index := First_Index (Array_Type);
7118 Expr := First (Expressions (N));
7120 -- The prefix may have resolved to a string literal, in which case its
7121 -- etype has a special representation. This is only possible currently
7122 -- if the prefix is a static concatenation, written in functional
7125 if Ekind (Array_Type) = E_String_Literal_Subtype then
7126 Resolve (Expr, Standard_Positive);
7129 while Present (Index) and Present (Expr) loop
7130 Resolve (Expr, Etype (Index));
7131 Check_Unset_Reference (Expr);
7133 if Is_Scalar_Type (Etype (Expr)) then
7134 Apply_Scalar_Range_Check (Expr, Etype (Index));
7136 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7144 -- Do not generate the warning on suspicious index if we are analyzing
7145 -- package Ada.Tags; otherwise we will report the warning with the
7146 -- Prims_Ptr field of the dispatch table.
7148 if Scope (Etype (Prefix (N))) = Standard_Standard
7150 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7153 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7154 Eval_Indexed_Component (N);
7157 -- If the array type is atomic, and is packed, and we are in a left side
7158 -- context, then this is worth a warning, since we have a situation
7159 -- where the access to the component may cause extra read/writes of
7160 -- the atomic array object, which could be considered unexpected.
7162 if Nkind (N) = N_Indexed_Component
7163 and then (Is_Atomic (Array_Type)
7164 or else (Is_Entity_Name (Prefix (N))
7165 and then Is_Atomic (Entity (Prefix (N)))))
7166 and then Is_Bit_Packed_Array (Array_Type)
7169 Error_Msg_N ("?assignment to component of packed atomic array",
7171 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7174 end Resolve_Indexed_Component;
7176 -----------------------------
7177 -- Resolve_Integer_Literal --
7178 -----------------------------
7180 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7183 Eval_Integer_Literal (N);
7184 end Resolve_Integer_Literal;
7186 --------------------------------
7187 -- Resolve_Intrinsic_Operator --
7188 --------------------------------
7190 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7191 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7193 Orig_Op : constant Entity_Id := Entity (N);
7197 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7198 -- If the operand is a literal, it cannot be the expression in a
7199 -- conversion. Use a qualified expression instead.
7201 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7202 Loc : constant Source_Ptr := Sloc (Opnd);
7205 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7207 Make_Qualified_Expression (Loc,
7208 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7209 Expression => Relocate_Node (Opnd));
7213 Res := Unchecked_Convert_To (Btyp, Opnd);
7217 end Convert_Operand;
7219 -- Start of processing for Resolve_Intrinsic_Operator
7222 -- We must preserve the original entity in a generic setting, so that
7223 -- the legality of the operation can be verified in an instance.
7225 if not Full_Expander_Active then
7230 while Scope (Op) /= Standard_Standard loop
7232 pragma Assert (Present (Op));
7236 Set_Is_Overloaded (N, False);
7238 -- If the result or operand types are private, rewrite with unchecked
7239 -- conversions on the operands and the result, to expose the proper
7240 -- underlying numeric type.
7242 if Is_Private_Type (Typ)
7243 or else Is_Private_Type (Etype (Left_Opnd (N)))
7244 or else Is_Private_Type (Etype (Right_Opnd (N)))
7246 Arg1 := Convert_Operand (Left_Opnd (N));
7247 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7248 -- What on earth is this commented out fragment of code???
7250 if Nkind (N) = N_Op_Expon then
7251 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7253 Arg2 := Convert_Operand (Right_Opnd (N));
7256 if Nkind (Arg1) = N_Type_Conversion then
7257 Save_Interps (Left_Opnd (N), Expression (Arg1));
7260 if Nkind (Arg2) = N_Type_Conversion then
7261 Save_Interps (Right_Opnd (N), Expression (Arg2));
7264 Set_Left_Opnd (N, Arg1);
7265 Set_Right_Opnd (N, Arg2);
7267 Set_Etype (N, Btyp);
7268 Rewrite (N, Unchecked_Convert_To (Typ, N));
7271 elsif Typ /= Etype (Left_Opnd (N))
7272 or else Typ /= Etype (Right_Opnd (N))
7274 -- Add explicit conversion where needed, and save interpretations in
7275 -- case operands are overloaded. If the context is a VMS operation,
7276 -- assert that the conversion is legal (the operands have the proper
7277 -- types to select the VMS intrinsic). Note that in rare cases the
7278 -- VMS operators may be visible, but the default System is being used
7279 -- and Address is a private type.
7281 Arg1 := Convert_To (Typ, Left_Opnd (N));
7282 Arg2 := Convert_To (Typ, Right_Opnd (N));
7284 if Nkind (Arg1) = N_Type_Conversion then
7285 Save_Interps (Left_Opnd (N), Expression (Arg1));
7287 if Is_VMS_Operator (Orig_Op) then
7288 Set_Conversion_OK (Arg1);
7291 Save_Interps (Left_Opnd (N), Arg1);
7294 if Nkind (Arg2) = N_Type_Conversion then
7295 Save_Interps (Right_Opnd (N), Expression (Arg2));
7297 if Is_VMS_Operator (Orig_Op) then
7298 Set_Conversion_OK (Arg2);
7301 Save_Interps (Right_Opnd (N), Arg2);
7304 Rewrite (Left_Opnd (N), Arg1);
7305 Rewrite (Right_Opnd (N), Arg2);
7308 Resolve_Arithmetic_Op (N, Typ);
7311 Resolve_Arithmetic_Op (N, Typ);
7313 end Resolve_Intrinsic_Operator;
7315 --------------------------------------
7316 -- Resolve_Intrinsic_Unary_Operator --
7317 --------------------------------------
7319 procedure Resolve_Intrinsic_Unary_Operator
7323 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7329 while Scope (Op) /= Standard_Standard loop
7331 pragma Assert (Present (Op));
7336 if Is_Private_Type (Typ) then
7337 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7338 Save_Interps (Right_Opnd (N), Expression (Arg2));
7340 Set_Right_Opnd (N, Arg2);
7342 Set_Etype (N, Btyp);
7343 Rewrite (N, Unchecked_Convert_To (Typ, N));
7347 Resolve_Unary_Op (N, Typ);
7349 end Resolve_Intrinsic_Unary_Operator;
7351 ------------------------
7352 -- Resolve_Logical_Op --
7353 ------------------------
7355 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7359 Check_No_Direct_Boolean_Operators (N);
7361 -- Predefined operations on scalar types yield the base type. On the
7362 -- other hand, logical operations on arrays yield the type of the
7363 -- arguments (and the context).
7365 if Is_Array_Type (Typ) then
7368 B_Typ := Base_Type (Typ);
7371 -- OK if this is a VMS-specific intrinsic operation
7373 if Is_VMS_Operator (Entity (N)) then
7376 -- The following test is required because the operands of the operation
7377 -- may be literals, in which case the resulting type appears to be
7378 -- compatible with a signed integer type, when in fact it is compatible
7379 -- only with modular types. If the context itself is universal, the
7380 -- operation is illegal.
7382 elsif not Valid_Boolean_Arg (Typ) then
7383 Error_Msg_N ("invalid context for logical operation", N);
7384 Set_Etype (N, Any_Type);
7387 elsif Typ = Any_Modular then
7389 ("no modular type available in this context", N);
7390 Set_Etype (N, Any_Type);
7393 elsif Is_Modular_Integer_Type (Typ)
7394 and then Etype (Left_Opnd (N)) = Universal_Integer
7395 and then Etype (Right_Opnd (N)) = Universal_Integer
7397 Check_For_Visible_Operator (N, B_Typ);
7400 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
7401 -- is active and the result type is standard Boolean (do not mess with
7402 -- ops that return a nonstandard Boolean type, because something strange
7405 -- Note: you might expect this replacement to be done during expansion,
7406 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
7407 -- is used, no part of the right operand of an "and" or "or" operator
7408 -- should be executed if the left operand would short-circuit the
7409 -- evaluation of the corresponding "and then" or "or else". If we left
7410 -- the replacement to expansion time, then run-time checks associated
7411 -- with such operands would be evaluated unconditionally, due to being
7412 -- before the condition prior to the rewriting as short-circuit forms
7413 -- during expansion.
7415 if Short_Circuit_And_Or
7416 and then B_Typ = Standard_Boolean
7417 and then Nkind_In (N, N_Op_And, N_Op_Or)
7419 if Nkind (N) = N_Op_And then
7421 Make_And_Then (Sloc (N),
7422 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7423 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7424 Analyze_And_Resolve (N, B_Typ);
7426 -- Case of OR changed to OR ELSE
7430 Make_Or_Else (Sloc (N),
7431 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7432 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7433 Analyze_And_Resolve (N, B_Typ);
7436 -- Return now, since analysis of the rewritten ops will take care of
7437 -- other reference bookkeeping and expression folding.
7442 Resolve (Left_Opnd (N), B_Typ);
7443 Resolve (Right_Opnd (N), B_Typ);
7445 Check_Unset_Reference (Left_Opnd (N));
7446 Check_Unset_Reference (Right_Opnd (N));
7448 Set_Etype (N, B_Typ);
7449 Generate_Operator_Reference (N, B_Typ);
7450 Eval_Logical_Op (N);
7452 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7453 -- only when both operands have same static lower and higher bounds. Of
7454 -- course the types have to match, so only check if operands are
7455 -- compatible and the node itself has no errors.
7457 if Is_Array_Type (B_Typ)
7458 and then Nkind (N) in N_Binary_Op
7461 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7462 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7465 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7466 -- operation if not needed.
7468 if Restriction_Check_Required (SPARK)
7469 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7470 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7471 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7472 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7474 Check_SPARK_Restriction
7475 ("array types should have matching static bounds", N);
7479 end Resolve_Logical_Op;
7481 ---------------------------
7482 -- Resolve_Membership_Op --
7483 ---------------------------
7485 -- The context can only be a boolean type, and does not determine the
7486 -- arguments. Arguments should be unambiguous, but the preference rule for
7487 -- universal types applies.
7489 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7490 pragma Warnings (Off, Typ);
7492 L : constant Node_Id := Left_Opnd (N);
7493 R : constant Node_Id := Right_Opnd (N);
7496 procedure Resolve_Set_Membership;
7497 -- Analysis has determined a unique type for the left operand. Use it to
7498 -- resolve the disjuncts.
7500 ----------------------------
7501 -- Resolve_Set_Membership --
7502 ----------------------------
7504 procedure Resolve_Set_Membership is
7508 Resolve (L, Etype (L));
7510 Alt := First (Alternatives (N));
7511 while Present (Alt) loop
7513 -- Alternative is an expression, a range
7514 -- or a subtype mark.
7516 if not Is_Entity_Name (Alt)
7517 or else not Is_Type (Entity (Alt))
7519 Resolve (Alt, Etype (L));
7524 end Resolve_Set_Membership;
7526 -- Start of processing for Resolve_Membership_Op
7529 if L = Error or else R = Error then
7533 if Present (Alternatives (N)) then
7534 Resolve_Set_Membership;
7537 elsif not Is_Overloaded (R)
7539 (Etype (R) = Universal_Integer
7541 Etype (R) = Universal_Real)
7542 and then Is_Overloaded (L)
7546 -- Ada 2005 (AI-251): Support the following case:
7548 -- type I is interface;
7549 -- type T is tagged ...
7551 -- function Test (O : I'Class) is
7553 -- return O in T'Class.
7556 -- In this case we have nothing else to do. The membership test will be
7557 -- done at run time.
7559 elsif Ada_Version >= Ada_2005
7560 and then Is_Class_Wide_Type (Etype (L))
7561 and then Is_Interface (Etype (L))
7562 and then Is_Class_Wide_Type (Etype (R))
7563 and then not Is_Interface (Etype (R))
7567 T := Intersect_Types (L, R);
7570 -- If mixed-mode operations are present and operands are all literal,
7571 -- the only interpretation involves Duration, which is probably not
7572 -- the intention of the programmer.
7574 if T = Any_Fixed then
7575 T := Unique_Fixed_Point_Type (N);
7577 if T = Any_Type then
7583 Check_Unset_Reference (L);
7585 if Nkind (R) = N_Range
7586 and then not Is_Scalar_Type (T)
7588 Error_Msg_N ("scalar type required for range", R);
7591 if Is_Entity_Name (R) then
7592 Freeze_Expression (R);
7595 Check_Unset_Reference (R);
7598 Eval_Membership_Op (N);
7599 end Resolve_Membership_Op;
7605 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7606 Loc : constant Source_Ptr := Sloc (N);
7609 -- Handle restriction against anonymous null access values This
7610 -- restriction can be turned off using -gnatdj.
7612 -- Ada 2005 (AI-231): Remove restriction
7614 if Ada_Version < Ada_2005
7615 and then not Debug_Flag_J
7616 and then Ekind (Typ) = E_Anonymous_Access_Type
7617 and then Comes_From_Source (N)
7619 -- In the common case of a call which uses an explicitly null value
7620 -- for an access parameter, give specialized error message.
7622 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7626 ("null is not allowed as argument for an access parameter", N);
7628 -- Standard message for all other cases (are there any?)
7632 ("null cannot be of an anonymous access type", N);
7636 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7637 -- assignment to a null-excluding object
7639 if Ada_Version >= Ada_2005
7640 and then Can_Never_Be_Null (Typ)
7641 and then Nkind (Parent (N)) = N_Assignment_Statement
7643 if not Inside_Init_Proc then
7645 (Compile_Time_Constraint_Error (N,
7646 "(Ada 2005) null not allowed in null-excluding objects?"),
7647 Make_Raise_Constraint_Error (Loc,
7648 Reason => CE_Access_Check_Failed));
7651 Make_Raise_Constraint_Error (Loc,
7652 Reason => CE_Access_Check_Failed));
7656 -- In a distributed context, null for a remote access to subprogram may
7657 -- need to be replaced with a special record aggregate. In this case,
7658 -- return after having done the transformation.
7660 if (Ekind (Typ) = E_Record_Type
7661 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7662 and then Remote_AST_Null_Value (N, Typ)
7667 -- The null literal takes its type from the context
7672 -----------------------
7673 -- Resolve_Op_Concat --
7674 -----------------------
7676 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7678 -- We wish to avoid deep recursion, because concatenations are often
7679 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7680 -- operands nonrecursively until we find something that is not a simple
7681 -- concatenation (A in this case). We resolve that, and then walk back
7682 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7683 -- to do the rest of the work at each level. The Parent pointers allow
7684 -- us to avoid recursion, and thus avoid running out of memory. See also
7685 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7691 -- The following code is equivalent to:
7693 -- Resolve_Op_Concat_First (NN, Typ);
7694 -- Resolve_Op_Concat_Arg (N, ...);
7695 -- Resolve_Op_Concat_Rest (N, Typ);
7697 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7698 -- operand is a concatenation.
7700 -- Walk down left operands
7703 Resolve_Op_Concat_First (NN, Typ);
7704 Op1 := Left_Opnd (NN);
7705 exit when not (Nkind (Op1) = N_Op_Concat
7706 and then not Is_Array_Type (Component_Type (Typ))
7707 and then Entity (Op1) = Entity (NN));
7711 -- Now (given the above example) NN is A&B and Op1 is A
7713 -- First resolve Op1 ...
7715 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7717 -- ... then walk NN back up until we reach N (where we started), calling
7718 -- Resolve_Op_Concat_Rest along the way.
7721 Resolve_Op_Concat_Rest (NN, Typ);
7726 if Base_Type (Etype (N)) /= Standard_String then
7727 Check_SPARK_Restriction
7728 ("result of concatenation should have type String", N);
7730 end Resolve_Op_Concat;
7732 ---------------------------
7733 -- Resolve_Op_Concat_Arg --
7734 ---------------------------
7736 procedure Resolve_Op_Concat_Arg
7742 Btyp : constant Entity_Id := Base_Type (Typ);
7743 Ctyp : constant Entity_Id := Component_Type (Typ);
7748 or else (not Is_Overloaded (Arg)
7749 and then Etype (Arg) /= Any_Composite
7750 and then Covers (Ctyp, Etype (Arg)))
7752 Resolve (Arg, Ctyp);
7754 Resolve (Arg, Btyp);
7757 -- If both Array & Array and Array & Component are visible, there is a
7758 -- potential ambiguity that must be reported.
7760 elsif Has_Compatible_Type (Arg, Ctyp) then
7761 if Nkind (Arg) = N_Aggregate
7762 and then Is_Composite_Type (Ctyp)
7764 if Is_Private_Type (Ctyp) then
7765 Resolve (Arg, Btyp);
7767 -- If the operation is user-defined and not overloaded use its
7768 -- profile. The operation may be a renaming, in which case it has
7769 -- been rewritten, and we want the original profile.
7771 elsif not Is_Overloaded (N)
7772 and then Comes_From_Source (Entity (Original_Node (N)))
7773 and then Ekind (Entity (Original_Node (N))) = E_Function
7777 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
7780 -- Otherwise an aggregate may match both the array type and the
7784 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7785 Set_Etype (Arg, Any_Type);
7789 if Is_Overloaded (Arg)
7790 and then Has_Compatible_Type (Arg, Typ)
7791 and then Etype (Arg) /= Any_Type
7799 Get_First_Interp (Arg, I, It);
7801 Get_Next_Interp (I, It);
7803 -- Special-case the error message when the overloading is
7804 -- caused by a function that yields an array and can be
7805 -- called without parameters.
7807 if It.Nam = Func then
7808 Error_Msg_Sloc := Sloc (Func);
7809 Error_Msg_N ("ambiguous call to function#", Arg);
7811 ("\\interpretation as call yields&", Arg, Typ);
7813 ("\\interpretation as indexing of call yields&",
7814 Arg, Component_Type (Typ));
7817 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
7819 Get_First_Interp (Arg, I, It);
7820 while Present (It.Nam) loop
7821 Error_Msg_Sloc := Sloc (It.Nam);
7823 if Base_Type (It.Typ) = Btyp
7825 Base_Type (It.Typ) = Base_Type (Ctyp)
7827 Error_Msg_N -- CODEFIX
7828 ("\\possible interpretation#", Arg);
7831 Get_Next_Interp (I, It);
7837 Resolve (Arg, Component_Type (Typ));
7839 if Nkind (Arg) = N_String_Literal then
7840 Set_Etype (Arg, Component_Type (Typ));
7843 if Arg = Left_Opnd (N) then
7844 Set_Is_Component_Left_Opnd (N);
7846 Set_Is_Component_Right_Opnd (N);
7851 Resolve (Arg, Btyp);
7854 -- Concatenation is restricted in SPARK: each operand must be either a
7855 -- string literal, the name of a string constant, a static character or
7856 -- string expression, or another concatenation. Arg cannot be a
7857 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
7858 -- separately on each final operand, past concatenation operations.
7860 if Is_Character_Type (Etype (Arg)) then
7861 if not Is_Static_Expression (Arg) then
7862 Check_SPARK_Restriction
7863 ("character operand for concatenation should be static", Arg);
7866 elsif Is_String_Type (Etype (Arg)) then
7867 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
7868 and then Is_Constant_Object (Entity (Arg)))
7869 and then not Is_Static_Expression (Arg)
7871 Check_SPARK_Restriction
7872 ("string operand for concatenation should be static", Arg);
7875 -- Do not issue error on an operand that is neither a character nor a
7876 -- string, as the error is issued in Resolve_Op_Concat.
7882 Check_Unset_Reference (Arg);
7883 end Resolve_Op_Concat_Arg;
7885 -----------------------------
7886 -- Resolve_Op_Concat_First --
7887 -----------------------------
7889 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7890 Btyp : constant Entity_Id := Base_Type (Typ);
7891 Op1 : constant Node_Id := Left_Opnd (N);
7892 Op2 : constant Node_Id := Right_Opnd (N);
7895 -- The parser folds an enormous sequence of concatenations of string
7896 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7897 -- in the right operand. If the expression resolves to a predefined "&"
7898 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7899 -- we give an error. See P_Simple_Expression in Par.Ch4.
7901 if Nkind (Op2) = N_String_Literal
7902 and then Is_Folded_In_Parser (Op2)
7903 and then Ekind (Entity (N)) = E_Function
7905 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7906 and then String_Length (Strval (Op1)) = 0);
7907 Error_Msg_N ("too many user-defined concatenations", N);
7911 Set_Etype (N, Btyp);
7913 if Is_Limited_Composite (Btyp) then
7914 Error_Msg_N ("concatenation not available for limited array", N);
7915 Explain_Limited_Type (Btyp, N);
7917 end Resolve_Op_Concat_First;
7919 ----------------------------
7920 -- Resolve_Op_Concat_Rest --
7921 ----------------------------
7923 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7924 Op1 : constant Node_Id := Left_Opnd (N);
7925 Op2 : constant Node_Id := Right_Opnd (N);
7928 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7930 Generate_Operator_Reference (N, Typ);
7932 if Is_String_Type (Typ) then
7933 Eval_Concatenation (N);
7936 -- If this is not a static concatenation, but the result is a string
7937 -- type (and not an array of strings) ensure that static string operands
7938 -- have their subtypes properly constructed.
7940 if Nkind (N) /= N_String_Literal
7941 and then Is_Character_Type (Component_Type (Typ))
7943 Set_String_Literal_Subtype (Op1, Typ);
7944 Set_String_Literal_Subtype (Op2, Typ);
7946 end Resolve_Op_Concat_Rest;
7948 ----------------------
7949 -- Resolve_Op_Expon --
7950 ----------------------
7952 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7953 B_Typ : constant Entity_Id := Base_Type (Typ);
7956 -- Catch attempts to do fixed-point exponentiation with universal
7957 -- operands, which is a case where the illegality is not caught during
7958 -- normal operator analysis.
7960 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7961 Error_Msg_N ("exponentiation not available for fixed point", N);
7964 elsif Nkind (Parent (N)) in N_Op
7965 and then Is_Fixed_Point_Type (Etype (Parent (N)))
7966 and then Etype (N) = Universal_Real
7967 and then Comes_From_Source (N)
7969 Error_Msg_N ("exponentiation not available for fixed point", N);
7973 if Comes_From_Source (N)
7974 and then Ekind (Entity (N)) = E_Function
7975 and then Is_Imported (Entity (N))
7976 and then Is_Intrinsic_Subprogram (Entity (N))
7978 Resolve_Intrinsic_Operator (N, Typ);
7982 if Etype (Left_Opnd (N)) = Universal_Integer
7983 or else Etype (Left_Opnd (N)) = Universal_Real
7985 Check_For_Visible_Operator (N, B_Typ);
7988 -- We do the resolution using the base type, because intermediate values
7989 -- in expressions always are of the base type, not a subtype of it.
7991 Resolve (Left_Opnd (N), B_Typ);
7992 Resolve (Right_Opnd (N), Standard_Integer);
7994 Check_Unset_Reference (Left_Opnd (N));
7995 Check_Unset_Reference (Right_Opnd (N));
7997 Set_Etype (N, B_Typ);
7998 Generate_Operator_Reference (N, B_Typ);
8001 -- Set overflow checking bit. Much cleverer code needed here eventually
8002 -- and perhaps the Resolve routines should be separated for the various
8003 -- arithmetic operations, since they will need different processing. ???
8005 if Nkind (N) in N_Op then
8006 if not Overflow_Checks_Suppressed (Etype (N)) then
8007 Enable_Overflow_Check (N);
8010 end Resolve_Op_Expon;
8012 --------------------
8013 -- Resolve_Op_Not --
8014 --------------------
8016 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8019 function Parent_Is_Boolean return Boolean;
8020 -- This function determines if the parent node is a boolean operator or
8021 -- operation (comparison op, membership test, or short circuit form) and
8022 -- the not in question is the left operand of this operation. Note that
8023 -- if the not is in parens, then false is returned.
8025 -----------------------
8026 -- Parent_Is_Boolean --
8027 -----------------------
8029 function Parent_Is_Boolean return Boolean is
8031 if Paren_Count (N) /= 0 then
8035 case Nkind (Parent (N)) is
8050 return Left_Opnd (Parent (N)) = N;
8056 end Parent_Is_Boolean;
8058 -- Start of processing for Resolve_Op_Not
8061 -- Predefined operations on scalar types yield the base type. On the
8062 -- other hand, logical operations on arrays yield the type of the
8063 -- arguments (and the context).
8065 if Is_Array_Type (Typ) then
8068 B_Typ := Base_Type (Typ);
8071 if Is_VMS_Operator (Entity (N)) then
8074 -- Straightforward case of incorrect arguments
8076 elsif not Valid_Boolean_Arg (Typ) then
8077 Error_Msg_N ("invalid operand type for operator&", N);
8078 Set_Etype (N, Any_Type);
8081 -- Special case of probable missing parens
8083 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8084 if Parent_Is_Boolean then
8086 ("operand of not must be enclosed in parentheses",
8090 ("no modular type available in this context", N);
8093 Set_Etype (N, Any_Type);
8096 -- OK resolution of NOT
8099 -- Warn if non-boolean types involved. This is a case like not a < b
8100 -- where a and b are modular, where we will get (not a) < b and most
8101 -- likely not (a < b) was intended.
8103 if Warn_On_Questionable_Missing_Parens
8104 and then not Is_Boolean_Type (Typ)
8105 and then Parent_Is_Boolean
8107 Error_Msg_N ("?not expression should be parenthesized here!", N);
8110 -- Warn on double negation if checking redundant constructs
8112 if Warn_On_Redundant_Constructs
8113 and then Comes_From_Source (N)
8114 and then Comes_From_Source (Right_Opnd (N))
8115 and then Root_Type (Typ) = Standard_Boolean
8116 and then Nkind (Right_Opnd (N)) = N_Op_Not
8118 Error_Msg_N ("redundant double negation?", N);
8121 -- Complete resolution and evaluation of NOT
8123 Resolve (Right_Opnd (N), B_Typ);
8124 Check_Unset_Reference (Right_Opnd (N));
8125 Set_Etype (N, B_Typ);
8126 Generate_Operator_Reference (N, B_Typ);
8131 -----------------------------
8132 -- Resolve_Operator_Symbol --
8133 -----------------------------
8135 -- Nothing to be done, all resolved already
8137 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8138 pragma Warnings (Off, N);
8139 pragma Warnings (Off, Typ);
8143 end Resolve_Operator_Symbol;
8145 ----------------------------------
8146 -- Resolve_Qualified_Expression --
8147 ----------------------------------
8149 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8150 pragma Warnings (Off, Typ);
8152 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8153 Expr : constant Node_Id := Expression (N);
8156 Resolve (Expr, Target_Typ);
8158 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8159 -- operation if not needed.
8161 if Restriction_Check_Required (SPARK)
8162 and then Is_Array_Type (Target_Typ)
8163 and then Is_Array_Type (Etype (Expr))
8164 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8165 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8167 Check_SPARK_Restriction
8168 ("array types should have matching static bounds", N);
8171 -- A qualified expression requires an exact match of the type, class-
8172 -- wide matching is not allowed. However, if the qualifying type is
8173 -- specific and the expression has a class-wide type, it may still be
8174 -- okay, since it can be the result of the expansion of a call to a
8175 -- dispatching function, so we also have to check class-wideness of the
8176 -- type of the expression's original node.
8178 if (Is_Class_Wide_Type (Target_Typ)
8180 (Is_Class_Wide_Type (Etype (Expr))
8181 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8182 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8184 Wrong_Type (Expr, Target_Typ);
8187 -- If the target type is unconstrained, then we reset the type of the
8188 -- result from the type of the expression. For other cases, the actual
8189 -- subtype of the expression is the target type.
8191 if Is_Composite_Type (Target_Typ)
8192 and then not Is_Constrained (Target_Typ)
8194 Set_Etype (N, Etype (Expr));
8197 Eval_Qualified_Expression (N);
8198 end Resolve_Qualified_Expression;
8200 -----------------------------------
8201 -- Resolve_Quantified_Expression --
8202 -----------------------------------
8204 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
8206 if not Alfa_Mode then
8208 -- If expansion is enabled, analysis is delayed until the expresssion
8209 -- is rewritten as a loop.
8211 if Operating_Mode /= Check_Semantics then
8215 -- The loop structure is already resolved during its analysis, only
8216 -- the resolution of the condition needs to be done. Expansion is
8217 -- disabled so that checks and other generated code are inserted in
8218 -- the tree after expression has been rewritten as a loop.
8220 Expander_Mode_Save_And_Set (False);
8221 Resolve (Condition (N), Typ);
8222 Expander_Mode_Restore;
8224 -- In Alfa mode, we need normal expansion in order to properly introduce
8225 -- the necessary transient scopes.
8228 Resolve (Condition (N), Typ);
8230 end Resolve_Quantified_Expression;
8236 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8237 L : constant Node_Id := Low_Bound (N);
8238 H : constant Node_Id := High_Bound (N);
8240 function First_Last_Ref return Boolean;
8241 -- Returns True if N is of the form X'First .. X'Last where X is the
8242 -- same entity for both attributes.
8244 --------------------
8245 -- First_Last_Ref --
8246 --------------------
8248 function First_Last_Ref return Boolean is
8249 Lorig : constant Node_Id := Original_Node (L);
8250 Horig : constant Node_Id := Original_Node (H);
8253 if Nkind (Lorig) = N_Attribute_Reference
8254 and then Nkind (Horig) = N_Attribute_Reference
8255 and then Attribute_Name (Lorig) = Name_First
8256 and then Attribute_Name (Horig) = Name_Last
8259 PL : constant Node_Id := Prefix (Lorig);
8260 PH : constant Node_Id := Prefix (Horig);
8262 if Is_Entity_Name (PL)
8263 and then Is_Entity_Name (PH)
8264 and then Entity (PL) = Entity (PH)
8274 -- Start of processing for Resolve_Range
8281 -- Check for inappropriate range on unordered enumeration type
8283 if Bad_Unordered_Enumeration_Reference (N, Typ)
8285 -- Exclude X'First .. X'Last if X is the same entity for both
8287 and then not First_Last_Ref
8289 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
8292 Check_Unset_Reference (L);
8293 Check_Unset_Reference (H);
8295 -- We have to check the bounds for being within the base range as
8296 -- required for a non-static context. Normally this is automatic and
8297 -- done as part of evaluating expressions, but the N_Range node is an
8298 -- exception, since in GNAT we consider this node to be a subexpression,
8299 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8300 -- this, but that would put the test on the main evaluation path for
8303 Check_Non_Static_Context (L);
8304 Check_Non_Static_Context (H);
8306 -- Check for an ambiguous range over character literals. This will
8307 -- happen with a membership test involving only literals.
8309 if Typ = Any_Character then
8310 Ambiguous_Character (L);
8311 Set_Etype (N, Any_Type);
8315 -- If bounds are static, constant-fold them, so size computations are
8316 -- identical between front-end and back-end. Do not perform this
8317 -- transformation while analyzing generic units, as type information
8318 -- would be lost when reanalyzing the constant node in the instance.
8320 if Is_Discrete_Type (Typ) and then Full_Expander_Active then
8321 if Is_OK_Static_Expression (L) then
8322 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8325 if Is_OK_Static_Expression (H) then
8326 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8331 --------------------------
8332 -- Resolve_Real_Literal --
8333 --------------------------
8335 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8336 Actual_Typ : constant Entity_Id := Etype (N);
8339 -- Special processing for fixed-point literals to make sure that the
8340 -- value is an exact multiple of small where this is required. We skip
8341 -- this for the universal real case, and also for generic types.
8343 if Is_Fixed_Point_Type (Typ)
8344 and then Typ /= Universal_Fixed
8345 and then Typ /= Any_Fixed
8346 and then not Is_Generic_Type (Typ)
8349 Val : constant Ureal := Realval (N);
8350 Cintr : constant Ureal := Val / Small_Value (Typ);
8351 Cint : constant Uint := UR_Trunc (Cintr);
8352 Den : constant Uint := Norm_Den (Cintr);
8356 -- Case of literal is not an exact multiple of the Small
8360 -- For a source program literal for a decimal fixed-point type,
8361 -- this is statically illegal (RM 4.9(36)).
8363 if Is_Decimal_Fixed_Point_Type (Typ)
8364 and then Actual_Typ = Universal_Real
8365 and then Comes_From_Source (N)
8367 Error_Msg_N ("value has extraneous low order digits", N);
8370 -- Generate a warning if literal from source
8372 if Is_Static_Expression (N)
8373 and then Warn_On_Bad_Fixed_Value
8376 ("?static fixed-point value is not a multiple of Small!",
8380 -- Replace literal by a value that is the exact representation
8381 -- of a value of the type, i.e. a multiple of the small value,
8382 -- by truncation, since Machine_Rounds is false for all GNAT
8383 -- fixed-point types (RM 4.9(38)).
8385 Stat := Is_Static_Expression (N);
8387 Make_Real_Literal (Sloc (N),
8388 Realval => Small_Value (Typ) * Cint));
8390 Set_Is_Static_Expression (N, Stat);
8393 -- In all cases, set the corresponding integer field
8395 Set_Corresponding_Integer_Value (N, Cint);
8399 -- Now replace the actual type by the expected type as usual
8402 Eval_Real_Literal (N);
8403 end Resolve_Real_Literal;
8405 -----------------------
8406 -- Resolve_Reference --
8407 -----------------------
8409 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8410 P : constant Node_Id := Prefix (N);
8413 -- Replace general access with specific type
8415 if Ekind (Etype (N)) = E_Allocator_Type then
8416 Set_Etype (N, Base_Type (Typ));
8419 Resolve (P, Designated_Type (Etype (N)));
8421 -- If we are taking the reference of a volatile entity, then treat it as
8422 -- a potential modification of this entity. This is too conservative,
8423 -- but necessary because remove side effects can cause transformations
8424 -- of normal assignments into reference sequences that otherwise fail to
8425 -- notice the modification.
8427 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8428 Note_Possible_Modification (P, Sure => False);
8430 end Resolve_Reference;
8432 --------------------------------
8433 -- Resolve_Selected_Component --
8434 --------------------------------
8436 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8438 Comp1 : Entity_Id := Empty; -- prevent junk warning
8439 P : constant Node_Id := Prefix (N);
8440 S : constant Node_Id := Selector_Name (N);
8441 T : Entity_Id := Etype (P);
8443 I1 : Interp_Index := 0; -- prevent junk warning
8448 function Init_Component return Boolean;
8449 -- Check whether this is the initialization of a component within an
8450 -- init proc (by assignment or call to another init proc). If true,
8451 -- there is no need for a discriminant check.
8453 --------------------
8454 -- Init_Component --
8455 --------------------
8457 function Init_Component return Boolean is
8459 return Inside_Init_Proc
8460 and then Nkind (Prefix (N)) = N_Identifier
8461 and then Chars (Prefix (N)) = Name_uInit
8462 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8465 -- Start of processing for Resolve_Selected_Component
8468 if Is_Overloaded (P) then
8470 -- Use the context type to select the prefix that has a selector
8471 -- of the correct name and type.
8474 Get_First_Interp (P, I, It);
8476 Search : while Present (It.Typ) loop
8477 if Is_Access_Type (It.Typ) then
8478 T := Designated_Type (It.Typ);
8483 -- Locate selected component. For a private prefix the selector
8484 -- can denote a discriminant.
8486 if Is_Record_Type (T) or else Is_Private_Type (T) then
8488 -- The visible components of a class-wide type are those of
8491 if Is_Class_Wide_Type (T) then
8495 Comp := First_Entity (T);
8496 while Present (Comp) loop
8497 if Chars (Comp) = Chars (S)
8498 and then Covers (Etype (Comp), Typ)
8507 It := Disambiguate (P, I1, I, Any_Type);
8509 if It = No_Interp then
8511 ("ambiguous prefix for selected component", N);
8518 -- There may be an implicit dereference. Retrieve
8519 -- designated record type.
8521 if Is_Access_Type (It1.Typ) then
8522 T := Designated_Type (It1.Typ);
8527 if Scope (Comp1) /= T then
8529 -- Resolution chooses the new interpretation.
8530 -- Find the component with the right name.
8532 Comp1 := First_Entity (T);
8533 while Present (Comp1)
8534 and then Chars (Comp1) /= Chars (S)
8536 Comp1 := Next_Entity (Comp1);
8545 Comp := Next_Entity (Comp);
8549 Get_Next_Interp (I, It);
8552 Resolve (P, It1.Typ);
8554 Set_Entity_With_Style_Check (S, Comp1);
8557 -- Resolve prefix with its type
8562 -- Generate cross-reference. We needed to wait until full overloading
8563 -- resolution was complete to do this, since otherwise we can't tell if
8564 -- we are an lvalue or not.
8566 if May_Be_Lvalue (N) then
8567 Generate_Reference (Entity (S), S, 'm');
8569 Generate_Reference (Entity (S), S, 'r');
8572 -- If prefix is an access type, the node will be transformed into an
8573 -- explicit dereference during expansion. The type of the node is the
8574 -- designated type of that of the prefix.
8576 if Is_Access_Type (Etype (P)) then
8577 T := Designated_Type (Etype (P));
8578 Check_Fully_Declared_Prefix (T, P);
8583 if Has_Discriminants (T)
8584 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8585 and then Present (Original_Record_Component (Entity (S)))
8586 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8587 and then Present (Discriminant_Checking_Func
8588 (Original_Record_Component (Entity (S))))
8589 and then not Discriminant_Checks_Suppressed (T)
8590 and then not Init_Component
8592 Set_Do_Discriminant_Check (N);
8595 if Ekind (Entity (S)) = E_Void then
8596 Error_Msg_N ("premature use of component", S);
8599 -- If the prefix is a record conversion, this may be a renamed
8600 -- discriminant whose bounds differ from those of the original
8601 -- one, so we must ensure that a range check is performed.
8603 if Nkind (P) = N_Type_Conversion
8604 and then Ekind (Entity (S)) = E_Discriminant
8605 and then Is_Discrete_Type (Typ)
8607 Set_Etype (N, Base_Type (Typ));
8610 -- Note: No Eval processing is required, because the prefix is of a
8611 -- record type, or protected type, and neither can possibly be static.
8613 -- If the array type is atomic, and is packed, and we are in a left side
8614 -- context, then this is worth a warning, since we have a situation
8615 -- where the access to the component may cause extra read/writes of the
8616 -- atomic array object, which could be considered unexpected.
8618 if Nkind (N) = N_Selected_Component
8619 and then (Is_Atomic (T)
8620 or else (Is_Entity_Name (Prefix (N))
8621 and then Is_Atomic (Entity (Prefix (N)))))
8622 and then Is_Packed (T)
8625 Error_Msg_N ("?assignment to component of packed atomic record",
8627 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8630 end Resolve_Selected_Component;
8636 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8637 B_Typ : constant Entity_Id := Base_Type (Typ);
8638 L : constant Node_Id := Left_Opnd (N);
8639 R : constant Node_Id := Right_Opnd (N);
8642 -- We do the resolution using the base type, because intermediate values
8643 -- in expressions always are of the base type, not a subtype of it.
8646 Resolve (R, Standard_Natural);
8648 Check_Unset_Reference (L);
8649 Check_Unset_Reference (R);
8651 Set_Etype (N, B_Typ);
8652 Generate_Operator_Reference (N, B_Typ);
8656 ---------------------------
8657 -- Resolve_Short_Circuit --
8658 ---------------------------
8660 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8661 B_Typ : constant Entity_Id := Base_Type (Typ);
8662 L : constant Node_Id := Left_Opnd (N);
8663 R : constant Node_Id := Right_Opnd (N);
8669 -- Check for issuing warning for always False assert/check, this happens
8670 -- when assertions are turned off, in which case the pragma Assert/Check
8671 -- was transformed into:
8673 -- if False and then <condition> then ...
8675 -- and we detect this pattern
8677 if Warn_On_Assertion_Failure
8678 and then Is_Entity_Name (R)
8679 and then Entity (R) = Standard_False
8680 and then Nkind (Parent (N)) = N_If_Statement
8681 and then Nkind (N) = N_And_Then
8682 and then Is_Entity_Name (L)
8683 and then Entity (L) = Standard_False
8686 Orig : constant Node_Id := Original_Node (Parent (N));
8689 if Nkind (Orig) = N_Pragma
8690 and then Pragma_Name (Orig) = Name_Assert
8692 -- Don't want to warn if original condition is explicit False
8695 Expr : constant Node_Id :=
8698 (First (Pragma_Argument_Associations (Orig))));
8700 if Is_Entity_Name (Expr)
8701 and then Entity (Expr) = Standard_False
8705 -- Issue warning. We do not want the deletion of the
8706 -- IF/AND-THEN to take this message with it. We achieve
8707 -- this by making sure that the expanded code points to
8708 -- the Sloc of the expression, not the original pragma.
8710 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
8711 -- The source location of the expression is not usually
8712 -- the best choice here. For example, it gets located on
8713 -- the last AND keyword in a chain of boolean expressiond
8714 -- AND'ed together. It is best to put the message on the
8715 -- first character of the assertion, which is the effect
8716 -- of the First_Node call here.
8719 ("?assertion would fail at run time!",
8721 (First (Pragma_Argument_Associations (Orig))));
8725 -- Similar processing for Check pragma
8727 elsif Nkind (Orig) = N_Pragma
8728 and then Pragma_Name (Orig) = Name_Check
8730 -- Don't want to warn if original condition is explicit False
8733 Expr : constant Node_Id :=
8737 (Pragma_Argument_Associations (Orig)))));
8739 if Is_Entity_Name (Expr)
8740 and then Entity (Expr) = Standard_False
8747 -- Again use Error_Msg_F rather than Error_Msg_N, see
8748 -- comment above for an explanation of why we do this.
8751 ("?check would fail at run time!",
8753 (Last (Pragma_Argument_Associations (Orig))));
8760 -- Continue with processing of short circuit
8762 Check_Unset_Reference (L);
8763 Check_Unset_Reference (R);
8765 Set_Etype (N, B_Typ);
8766 Eval_Short_Circuit (N);
8767 end Resolve_Short_Circuit;
8773 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8774 Name : constant Node_Id := Prefix (N);
8775 Drange : constant Node_Id := Discrete_Range (N);
8776 Array_Type : Entity_Id := Empty;
8780 if Is_Overloaded (Name) then
8782 -- Use the context type to select the prefix that yields the correct
8787 I1 : Interp_Index := 0;
8789 P : constant Node_Id := Prefix (N);
8790 Found : Boolean := False;
8793 Get_First_Interp (P, I, It);
8794 while Present (It.Typ) loop
8795 if (Is_Array_Type (It.Typ)
8796 and then Covers (Typ, It.Typ))
8797 or else (Is_Access_Type (It.Typ)
8798 and then Is_Array_Type (Designated_Type (It.Typ))
8799 and then Covers (Typ, Designated_Type (It.Typ)))
8802 It := Disambiguate (P, I1, I, Any_Type);
8804 if It = No_Interp then
8805 Error_Msg_N ("ambiguous prefix for slicing", N);
8810 Array_Type := It.Typ;
8815 Array_Type := It.Typ;
8820 Get_Next_Interp (I, It);
8825 Array_Type := Etype (Name);
8828 Resolve (Name, Array_Type);
8830 if Is_Access_Type (Array_Type) then
8831 Apply_Access_Check (N);
8832 Array_Type := Designated_Type (Array_Type);
8834 -- If the prefix is an access to an unconstrained array, we must use
8835 -- the actual subtype of the object to perform the index checks. The
8836 -- object denoted by the prefix is implicit in the node, so we build
8837 -- an explicit representation for it in order to compute the actual
8840 if not Is_Constrained (Array_Type) then
8841 Remove_Side_Effects (Prefix (N));
8844 Obj : constant Node_Id :=
8845 Make_Explicit_Dereference (Sloc (N),
8846 Prefix => New_Copy_Tree (Prefix (N)));
8848 Set_Etype (Obj, Array_Type);
8849 Set_Parent (Obj, Parent (N));
8850 Array_Type := Get_Actual_Subtype (Obj);
8854 elsif Is_Entity_Name (Name)
8855 or else Nkind (Name) = N_Explicit_Dereference
8856 or else (Nkind (Name) = N_Function_Call
8857 and then not Is_Constrained (Etype (Name)))
8859 Array_Type := Get_Actual_Subtype (Name);
8861 -- If the name is a selected component that depends on discriminants,
8862 -- build an actual subtype for it. This can happen only when the name
8863 -- itself is overloaded; otherwise the actual subtype is created when
8864 -- the selected component is analyzed.
8866 elsif Nkind (Name) = N_Selected_Component
8867 and then Full_Analysis
8868 and then Depends_On_Discriminant (First_Index (Array_Type))
8871 Act_Decl : constant Node_Id :=
8872 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8874 Insert_Action (N, Act_Decl);
8875 Array_Type := Defining_Identifier (Act_Decl);
8878 -- Maybe this should just be "else", instead of checking for the
8879 -- specific case of slice??? This is needed for the case where the
8880 -- prefix is an Image attribute, which gets expanded to a slice, and so
8881 -- has a constrained subtype which we want to use for the slice range
8882 -- check applied below (the range check won't get done if the
8883 -- unconstrained subtype of the 'Image is used).
8885 elsif Nkind (Name) = N_Slice then
8886 Array_Type := Etype (Name);
8889 -- If name was overloaded, set slice type correctly now
8891 Set_Etype (N, Array_Type);
8893 -- If the range is specified by a subtype mark, no resolution is
8894 -- necessary. Else resolve the bounds, and apply needed checks.
8896 if not Is_Entity_Name (Drange) then
8897 Index := First_Index (Array_Type);
8898 Resolve (Drange, Base_Type (Etype (Index)));
8900 if Nkind (Drange) = N_Range then
8902 -- Ensure that side effects in the bounds are properly handled
8904 Force_Evaluation (Low_Bound (Drange));
8905 Force_Evaluation (High_Bound (Drange));
8907 -- Do not apply the range check to nodes associated with the
8908 -- frontend expansion of the dispatch table. We first check
8909 -- if Ada.Tags is already loaded to avoid the addition of an
8910 -- undesired dependence on such run-time unit.
8912 if not Tagged_Type_Expansion
8914 (RTU_Loaded (Ada_Tags)
8915 and then Nkind (Prefix (N)) = N_Selected_Component
8916 and then Present (Entity (Selector_Name (Prefix (N))))
8917 and then Entity (Selector_Name (Prefix (N))) =
8918 RTE_Record_Component (RE_Prims_Ptr))
8920 Apply_Range_Check (Drange, Etype (Index));
8925 Set_Slice_Subtype (N);
8927 -- Check bad use of type with predicates
8929 if Has_Predicates (Etype (Drange)) then
8930 Bad_Predicated_Subtype_Use
8931 ("subtype& has predicate, not allowed in slice",
8932 Drange, Etype (Drange));
8934 -- Otherwise here is where we check suspicious indexes
8936 elsif Nkind (Drange) = N_Range then
8937 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8938 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8944 ----------------------------
8945 -- Resolve_String_Literal --
8946 ----------------------------
8948 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8949 C_Typ : constant Entity_Id := Component_Type (Typ);
8950 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8951 Loc : constant Source_Ptr := Sloc (N);
8952 Str : constant String_Id := Strval (N);
8953 Strlen : constant Nat := String_Length (Str);
8954 Subtype_Id : Entity_Id;
8955 Need_Check : Boolean;
8958 -- For a string appearing in a concatenation, defer creation of the
8959 -- string_literal_subtype until the end of the resolution of the
8960 -- concatenation, because the literal may be constant-folded away. This
8961 -- is a useful optimization for long concatenation expressions.
8963 -- If the string is an aggregate built for a single character (which
8964 -- happens in a non-static context) or a is null string to which special
8965 -- checks may apply, we build the subtype. Wide strings must also get a
8966 -- string subtype if they come from a one character aggregate. Strings
8967 -- generated by attributes might be static, but it is often hard to
8968 -- determine whether the enclosing context is static, so we generate
8969 -- subtypes for them as well, thus losing some rarer optimizations ???
8970 -- Same for strings that come from a static conversion.
8973 (Strlen = 0 and then Typ /= Standard_String)
8974 or else Nkind (Parent (N)) /= N_Op_Concat
8975 or else (N /= Left_Opnd (Parent (N))
8976 and then N /= Right_Opnd (Parent (N)))
8977 or else ((Typ = Standard_Wide_String
8978 or else Typ = Standard_Wide_Wide_String)
8979 and then Nkind (Original_Node (N)) /= N_String_Literal);
8981 -- If the resolving type is itself a string literal subtype, we can just
8982 -- reuse it, since there is no point in creating another.
8984 if Ekind (Typ) = E_String_Literal_Subtype then
8987 elsif Nkind (Parent (N)) = N_Op_Concat
8988 and then not Need_Check
8989 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8990 N_Attribute_Reference,
8991 N_Qualified_Expression,
8996 -- Otherwise we must create a string literal subtype. Note that the
8997 -- whole idea of string literal subtypes is simply to avoid the need
8998 -- for building a full fledged array subtype for each literal.
9001 Set_String_Literal_Subtype (N, Typ);
9002 Subtype_Id := Etype (N);
9005 if Nkind (Parent (N)) /= N_Op_Concat
9008 Set_Etype (N, Subtype_Id);
9009 Eval_String_Literal (N);
9012 if Is_Limited_Composite (Typ)
9013 or else Is_Private_Composite (Typ)
9015 Error_Msg_N ("string literal not available for private array", N);
9016 Set_Etype (N, Any_Type);
9020 -- The validity of a null string has been checked in the call to
9021 -- Eval_String_Literal.
9026 -- Always accept string literal with component type Any_Character, which
9027 -- occurs in error situations and in comparisons of literals, both of
9028 -- which should accept all literals.
9030 elsif R_Typ = Any_Character then
9033 -- If the type is bit-packed, then we always transform the string
9034 -- literal into a full fledged aggregate.
9036 elsif Is_Bit_Packed_Array (Typ) then
9039 -- Deal with cases of Wide_Wide_String, Wide_String, and String
9042 -- For Standard.Wide_Wide_String, or any other type whose component
9043 -- type is Standard.Wide_Wide_Character, we know that all the
9044 -- characters in the string must be acceptable, since the parser
9045 -- accepted the characters as valid character literals.
9047 if R_Typ = Standard_Wide_Wide_Character then
9050 -- For the case of Standard.String, or any other type whose component
9051 -- type is Standard.Character, we must make sure that there are no
9052 -- wide characters in the string, i.e. that it is entirely composed
9053 -- of characters in range of type Character.
9055 -- If the string literal is the result of a static concatenation, the
9056 -- test has already been performed on the components, and need not be
9059 elsif R_Typ = Standard_Character
9060 and then Nkind (Original_Node (N)) /= N_Op_Concat
9062 for J in 1 .. Strlen loop
9063 if not In_Character_Range (Get_String_Char (Str, J)) then
9065 -- If we are out of range, post error. This is one of the
9066 -- very few places that we place the flag in the middle of
9067 -- a token, right under the offending wide character. Not
9068 -- quite clear if this is right wrt wide character encoding
9069 -- sequences, but it's only an error message!
9072 ("literal out of range of type Standard.Character",
9073 Source_Ptr (Int (Loc) + J));
9078 -- For the case of Standard.Wide_String, or any other type whose
9079 -- component type is Standard.Wide_Character, we must make sure that
9080 -- there are no wide characters in the string, i.e. that it is
9081 -- entirely composed of characters in range of type Wide_Character.
9083 -- If the string literal is the result of a static concatenation,
9084 -- the test has already been performed on the components, and need
9087 elsif R_Typ = Standard_Wide_Character
9088 and then Nkind (Original_Node (N)) /= N_Op_Concat
9090 for J in 1 .. Strlen loop
9091 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
9093 -- If we are out of range, post error. This is one of the
9094 -- very few places that we place the flag in the middle of
9095 -- a token, right under the offending wide character.
9097 -- This is not quite right, because characters in general
9098 -- will take more than one character position ???
9101 ("literal out of range of type Standard.Wide_Character",
9102 Source_Ptr (Int (Loc) + J));
9107 -- If the root type is not a standard character, then we will convert
9108 -- the string into an aggregate and will let the aggregate code do
9109 -- the checking. Standard Wide_Wide_Character is also OK here.
9115 -- See if the component type of the array corresponding to the string
9116 -- has compile time known bounds. If yes we can directly check
9117 -- whether the evaluation of the string will raise constraint error.
9118 -- Otherwise we need to transform the string literal into the
9119 -- corresponding character aggregate and let the aggregate code do
9122 if Is_Standard_Character_Type (R_Typ) then
9124 -- Check for the case of full range, where we are definitely OK
9126 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
9130 -- Here the range is not the complete base type range, so check
9133 Comp_Typ_Lo : constant Node_Id :=
9134 Type_Low_Bound (Component_Type (Typ));
9135 Comp_Typ_Hi : constant Node_Id :=
9136 Type_High_Bound (Component_Type (Typ));
9141 if Compile_Time_Known_Value (Comp_Typ_Lo)
9142 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9144 for J in 1 .. Strlen loop
9145 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9147 if Char_Val < Expr_Value (Comp_Typ_Lo)
9148 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9150 Apply_Compile_Time_Constraint_Error
9151 (N, "character out of range?", CE_Range_Check_Failed,
9152 Loc => Source_Ptr (Int (Loc) + J));
9162 -- If we got here we meed to transform the string literal into the
9163 -- equivalent qualified positional array aggregate. This is rather
9164 -- heavy artillery for this situation, but it is hard work to avoid.
9167 Lits : constant List_Id := New_List;
9168 P : Source_Ptr := Loc + 1;
9172 -- Build the character literals, we give them source locations that
9173 -- correspond to the string positions, which is a bit tricky given
9174 -- the possible presence of wide character escape sequences.
9176 for J in 1 .. Strlen loop
9177 C := Get_String_Char (Str, J);
9178 Set_Character_Literal_Name (C);
9181 Make_Character_Literal (P,
9183 Char_Literal_Value => UI_From_CC (C)));
9185 if In_Character_Range (C) then
9188 -- Should we have a call to Skip_Wide here ???
9197 Make_Qualified_Expression (Loc,
9198 Subtype_Mark => New_Reference_To (Typ, Loc),
9200 Make_Aggregate (Loc, Expressions => Lits)));
9202 Analyze_And_Resolve (N, Typ);
9204 end Resolve_String_Literal;
9206 -----------------------------
9207 -- Resolve_Subprogram_Info --
9208 -----------------------------
9210 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9213 end Resolve_Subprogram_Info;
9215 -----------------------------
9216 -- Resolve_Type_Conversion --
9217 -----------------------------
9219 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9220 Conv_OK : constant Boolean := Conversion_OK (N);
9221 Operand : constant Node_Id := Expression (N);
9222 Operand_Typ : constant Entity_Id := Etype (Operand);
9223 Target_Typ : constant Entity_Id := Etype (N);
9228 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9229 -- Set to False to suppress cases where we want to suppress the test
9230 -- for redundancy to avoid possible false positives on this warning.
9234 and then not Valid_Conversion (N, Target_Typ, Operand)
9239 -- If the Operand Etype is Universal_Fixed, then the conversion is
9240 -- never redundant. We need this check because by the time we have
9241 -- finished the rather complex transformation, the conversion looks
9242 -- redundant when it is not.
9244 if Operand_Typ = Universal_Fixed then
9245 Test_Redundant := False;
9247 -- If the operand is marked as Any_Fixed, then special processing is
9248 -- required. This is also a case where we suppress the test for a
9249 -- redundant conversion, since most certainly it is not redundant.
9251 elsif Operand_Typ = Any_Fixed then
9252 Test_Redundant := False;
9254 -- Mixed-mode operation involving a literal. Context must be a fixed
9255 -- type which is applied to the literal subsequently.
9257 if Is_Fixed_Point_Type (Typ) then
9258 Set_Etype (Operand, Universal_Real);
9260 elsif Is_Numeric_Type (Typ)
9261 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9262 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9264 Etype (Left_Opnd (Operand)) = Universal_Real)
9266 -- Return if expression is ambiguous
9268 if Unique_Fixed_Point_Type (N) = Any_Type then
9271 -- If nothing else, the available fixed type is Duration
9274 Set_Etype (Operand, Standard_Duration);
9277 -- Resolve the real operand with largest available precision
9279 if Etype (Right_Opnd (Operand)) = Universal_Real then
9280 Rop := New_Copy_Tree (Right_Opnd (Operand));
9282 Rop := New_Copy_Tree (Left_Opnd (Operand));
9285 Resolve (Rop, Universal_Real);
9287 -- If the operand is a literal (it could be a non-static and
9288 -- illegal exponentiation) check whether the use of Duration
9289 -- is potentially inaccurate.
9291 if Nkind (Rop) = N_Real_Literal
9292 and then Realval (Rop) /= Ureal_0
9293 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9296 ("?universal real operand can only " &
9297 "be interpreted as Duration!",
9300 ("\?precision will be lost in the conversion!", Rop);
9303 elsif Is_Numeric_Type (Typ)
9304 and then Nkind (Operand) in N_Op
9305 and then Unique_Fixed_Point_Type (N) /= Any_Type
9307 Set_Etype (Operand, Standard_Duration);
9310 Error_Msg_N ("invalid context for mixed mode operation", N);
9311 Set_Etype (Operand, Any_Type);
9318 -- In SPARK, a type conversion between array types should be restricted
9319 -- to types which have matching static bounds.
9321 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9322 -- operation if not needed.
9324 if Restriction_Check_Required (SPARK)
9325 and then Is_Array_Type (Target_Typ)
9326 and then Is_Array_Type (Operand_Typ)
9327 and then Operand_Typ /= Any_Composite -- or else Operand in error
9328 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9330 Check_SPARK_Restriction
9331 ("array types should have matching static bounds", N);
9334 -- In formal mode, the operand of an ancestor type conversion must be an
9335 -- object (not an expression).
9337 if Is_Tagged_Type (Target_Typ)
9338 and then not Is_Class_Wide_Type (Target_Typ)
9339 and then Is_Tagged_Type (Operand_Typ)
9340 and then not Is_Class_Wide_Type (Operand_Typ)
9341 and then Is_Ancestor (Target_Typ, Operand_Typ)
9342 and then not Is_SPARK_Object_Reference (Operand)
9344 Check_SPARK_Restriction ("object required", Operand);
9347 -- Note: we do the Eval_Type_Conversion call before applying the
9348 -- required checks for a subtype conversion. This is important, since
9349 -- both are prepared under certain circumstances to change the type
9350 -- conversion to a constraint error node, but in the case of
9351 -- Eval_Type_Conversion this may reflect an illegality in the static
9352 -- case, and we would miss the illegality (getting only a warning
9353 -- message), if we applied the type conversion checks first.
9355 Eval_Type_Conversion (N);
9357 -- Even when evaluation is not possible, we may be able to simplify the
9358 -- conversion or its expression. This needs to be done before applying
9359 -- checks, since otherwise the checks may use the original expression
9360 -- and defeat the simplifications. This is specifically the case for
9361 -- elimination of the floating-point Truncation attribute in
9362 -- float-to-int conversions.
9364 Simplify_Type_Conversion (N);
9366 -- If after evaluation we still have a type conversion, then we may need
9367 -- to apply checks required for a subtype conversion.
9369 -- Skip these type conversion checks if universal fixed operands
9370 -- operands involved, since range checks are handled separately for
9371 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9373 if Nkind (N) = N_Type_Conversion
9374 and then not Is_Generic_Type (Root_Type (Target_Typ))
9375 and then Target_Typ /= Universal_Fixed
9376 and then Operand_Typ /= Universal_Fixed
9378 Apply_Type_Conversion_Checks (N);
9381 -- Issue warning for conversion of simple object to its own type. We
9382 -- have to test the original nodes, since they may have been rewritten
9383 -- by various optimizations.
9385 Orig_N := Original_Node (N);
9387 -- Here we test for a redundant conversion if the warning mode is
9388 -- active (and was not locally reset), and we have a type conversion
9389 -- from source not appearing in a generic instance.
9392 and then Nkind (Orig_N) = N_Type_Conversion
9393 and then Comes_From_Source (Orig_N)
9394 and then not In_Instance
9396 Orig_N := Original_Node (Expression (Orig_N));
9397 Orig_T := Target_Typ;
9399 -- If the node is part of a larger expression, the Target_Type
9400 -- may not be the original type of the node if the context is a
9401 -- condition. Recover original type to see if conversion is needed.
9403 if Is_Boolean_Type (Orig_T)
9404 and then Nkind (Parent (N)) in N_Op
9406 Orig_T := Etype (Parent (N));
9409 -- If we have an entity name, then give the warning if the entity
9410 -- is the right type, or if it is a loop parameter covered by the
9411 -- original type (that's needed because loop parameters have an
9412 -- odd subtype coming from the bounds).
9414 if (Is_Entity_Name (Orig_N)
9416 (Etype (Entity (Orig_N)) = Orig_T
9418 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9419 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9421 -- If not an entity, then type of expression must match
9423 or else Etype (Orig_N) = Orig_T
9425 -- One more check, do not give warning if the analyzed conversion
9426 -- has an expression with non-static bounds, and the bounds of the
9427 -- target are static. This avoids junk warnings in cases where the
9428 -- conversion is necessary to establish staticness, for example in
9429 -- a case statement.
9431 if not Is_OK_Static_Subtype (Operand_Typ)
9432 and then Is_OK_Static_Subtype (Target_Typ)
9436 -- Finally, if this type conversion occurs in a context requiring
9437 -- a prefix, and the expression is a qualified expression then the
9438 -- type conversion is not redundant, since a qualified expression
9439 -- is not a prefix, whereas a type conversion is. For example, "X
9440 -- := T'(Funx(...)).Y;" is illegal because a selected component
9441 -- requires a prefix, but a type conversion makes it legal: "X :=
9442 -- T(T'(Funx(...))).Y;"
9444 -- In Ada 2012, a qualified expression is a name, so this idiom is
9445 -- no longer needed, but we still suppress the warning because it
9446 -- seems unfriendly for warnings to pop up when you switch to the
9447 -- newer language version.
9449 elsif Nkind (Orig_N) = N_Qualified_Expression
9450 and then Nkind_In (Parent (N), N_Attribute_Reference,
9451 N_Indexed_Component,
9452 N_Selected_Component,
9454 N_Explicit_Dereference)
9458 -- Here we give the redundant conversion warning. If it is an
9459 -- entity, give the name of the entity in the message. If not,
9460 -- just mention the expression.
9463 if Is_Entity_Name (Orig_N) then
9464 Error_Msg_Node_2 := Orig_T;
9465 Error_Msg_NE -- CODEFIX
9466 ("?redundant conversion, & is of type &!",
9467 N, Entity (Orig_N));
9470 ("?redundant conversion, expression is of type&!",
9477 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9478 -- No need to perform any interface conversion if the type of the
9479 -- expression coincides with the target type.
9481 if Ada_Version >= Ada_2005
9482 and then Full_Expander_Active
9483 and then Operand_Typ /= Target_Typ
9486 Opnd : Entity_Id := Operand_Typ;
9487 Target : Entity_Id := Target_Typ;
9490 if Is_Access_Type (Opnd) then
9491 Opnd := Designated_Type (Opnd);
9494 if Is_Access_Type (Target_Typ) then
9495 Target := Designated_Type (Target);
9498 if Opnd = Target then
9501 -- Conversion from interface type
9503 elsif Is_Interface (Opnd) then
9505 -- Ada 2005 (AI-217): Handle entities from limited views
9507 if From_With_Type (Opnd) then
9508 Error_Msg_Qual_Level := 99;
9509 Error_Msg_NE -- CODEFIX
9510 ("missing WITH clause on package &", N,
9511 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9513 ("type conversions require visibility of the full view",
9516 elsif From_With_Type (Target)
9518 (Is_Access_Type (Target_Typ)
9519 and then Present (Non_Limited_View (Etype (Target))))
9521 Error_Msg_Qual_Level := 99;
9522 Error_Msg_NE -- CODEFIX
9523 ("missing WITH clause on package &", N,
9524 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9526 ("type conversions require visibility of the full view",
9530 Expand_Interface_Conversion (N, Is_Static => False);
9533 -- Conversion to interface type
9535 elsif Is_Interface (Target) then
9539 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9540 Opnd := Etype (Opnd);
9543 if not Interface_Present_In_Ancestor
9547 if Is_Class_Wide_Type (Opnd) then
9549 -- The static analysis is not enough to know if the
9550 -- interface is implemented or not. Hence we must pass
9551 -- the work to the expander to generate code to evaluate
9552 -- the conversion at run time.
9554 Expand_Interface_Conversion (N, Is_Static => False);
9557 Error_Msg_Name_1 := Chars (Etype (Target));
9558 Error_Msg_Name_2 := Chars (Opnd);
9560 ("wrong interface conversion (% is not a progenitor " &
9565 Expand_Interface_Conversion (N);
9570 end Resolve_Type_Conversion;
9572 ----------------------
9573 -- Resolve_Unary_Op --
9574 ----------------------
9576 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9577 B_Typ : constant Entity_Id := Base_Type (Typ);
9578 R : constant Node_Id := Right_Opnd (N);
9584 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9585 Error_Msg_Name_1 := Chars (Typ);
9586 Check_SPARK_Restriction
9587 ("unary operator not defined for modular type%", N);
9590 -- Deal with intrinsic unary operators
9592 if Comes_From_Source (N)
9593 and then Ekind (Entity (N)) = E_Function
9594 and then Is_Imported (Entity (N))
9595 and then Is_Intrinsic_Subprogram (Entity (N))
9597 Resolve_Intrinsic_Unary_Operator (N, Typ);
9601 -- Deal with universal cases
9603 if Etype (R) = Universal_Integer
9605 Etype (R) = Universal_Real
9607 Check_For_Visible_Operator (N, B_Typ);
9610 Set_Etype (N, B_Typ);
9613 -- Generate warning for expressions like abs (x mod 2)
9615 if Warn_On_Redundant_Constructs
9616 and then Nkind (N) = N_Op_Abs
9618 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9620 if OK and then Hi >= Lo and then Lo >= 0 then
9621 Error_Msg_N -- CODEFIX
9622 ("?abs applied to known non-negative value has no effect", N);
9626 -- Deal with reference generation
9628 Check_Unset_Reference (R);
9629 Generate_Operator_Reference (N, B_Typ);
9632 -- Set overflow checking bit. Much cleverer code needed here eventually
9633 -- and perhaps the Resolve routines should be separated for the various
9634 -- arithmetic operations, since they will need different processing ???
9636 if Nkind (N) in N_Op then
9637 if not Overflow_Checks_Suppressed (Etype (N)) then
9638 Enable_Overflow_Check (N);
9642 -- Generate warning for expressions like -5 mod 3 for integers. No need
9643 -- to worry in the floating-point case, since parens do not affect the
9644 -- result so there is no point in giving in a warning.
9647 Norig : constant Node_Id := Original_Node (N);
9656 if Warn_On_Questionable_Missing_Parens
9657 and then Comes_From_Source (Norig)
9658 and then Is_Integer_Type (Typ)
9659 and then Nkind (Norig) = N_Op_Minus
9661 Rorig := Original_Node (Right_Opnd (Norig));
9663 -- We are looking for cases where the right operand is not
9664 -- parenthesized, and is a binary operator, multiply, divide, or
9665 -- mod. These are the cases where the grouping can affect results.
9667 if Paren_Count (Rorig) = 0
9668 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9670 -- For mod, we always give the warning, since the value is
9671 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9672 -- -(5 mod 315)). But for the other cases, the only concern is
9673 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9674 -- overflows, but (-2) * 64 does not). So we try to give the
9675 -- message only when overflow is possible.
9677 if Nkind (Rorig) /= N_Op_Mod
9678 and then Compile_Time_Known_Value (R)
9680 Val := Expr_Value (R);
9682 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9683 HB := Expr_Value (Type_High_Bound (Typ));
9685 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9688 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9689 LB := Expr_Value (Type_Low_Bound (Typ));
9691 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9694 -- Note that the test below is deliberately excluding the
9695 -- largest negative number, since that is a potentially
9696 -- troublesome case (e.g. -2 * x, where the result is the
9697 -- largest negative integer has an overflow with 2 * x).
9699 if Val > LB and then Val <= HB then
9704 -- For the multiplication case, the only case we have to worry
9705 -- about is when (-a)*b is exactly the largest negative number
9706 -- so that -(a*b) can cause overflow. This can only happen if
9707 -- a is a power of 2, and more generally if any operand is a
9708 -- constant that is not a power of 2, then the parentheses
9709 -- cannot affect whether overflow occurs. We only bother to
9710 -- test the left most operand
9712 -- Loop looking at left operands for one that has known value
9715 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9716 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9717 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9719 -- Operand value of 0 or 1 skips warning
9724 -- Otherwise check power of 2, if power of 2, warn, if
9725 -- anything else, skip warning.
9728 while Lval /= 2 loop
9729 if Lval mod 2 = 1 then
9740 -- Keep looking at left operands
9742 Opnd := Left_Opnd (Opnd);
9745 -- For rem or "/" we can only have a problematic situation
9746 -- if the divisor has a value of minus one or one. Otherwise
9747 -- overflow is impossible (divisor > 1) or we have a case of
9748 -- division by zero in any case.
9750 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9751 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9752 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9757 -- If we fall through warning should be issued
9760 ("?unary minus expression should be parenthesized here!", N);
9764 end Resolve_Unary_Op;
9766 ----------------------------------
9767 -- Resolve_Unchecked_Expression --
9768 ----------------------------------
9770 procedure Resolve_Unchecked_Expression
9775 Resolve (Expression (N), Typ, Suppress => All_Checks);
9777 end Resolve_Unchecked_Expression;
9779 ---------------------------------------
9780 -- Resolve_Unchecked_Type_Conversion --
9781 ---------------------------------------
9783 procedure Resolve_Unchecked_Type_Conversion
9787 pragma Warnings (Off, Typ);
9789 Operand : constant Node_Id := Expression (N);
9790 Opnd_Type : constant Entity_Id := Etype (Operand);
9793 -- Resolve operand using its own type
9795 Resolve (Operand, Opnd_Type);
9796 Eval_Unchecked_Conversion (N);
9797 end Resolve_Unchecked_Type_Conversion;
9799 ------------------------------
9800 -- Rewrite_Operator_As_Call --
9801 ------------------------------
9803 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9804 Loc : constant Source_Ptr := Sloc (N);
9805 Actuals : constant List_Id := New_List;
9809 if Nkind (N) in N_Binary_Op then
9810 Append (Left_Opnd (N), Actuals);
9813 Append (Right_Opnd (N), Actuals);
9816 Make_Function_Call (Sloc => Loc,
9817 Name => New_Occurrence_Of (Nam, Loc),
9818 Parameter_Associations => Actuals);
9820 Preserve_Comes_From_Source (New_N, N);
9821 Preserve_Comes_From_Source (Name (New_N), N);
9823 Set_Etype (N, Etype (Nam));
9824 end Rewrite_Operator_As_Call;
9826 ------------------------------
9827 -- Rewrite_Renamed_Operator --
9828 ------------------------------
9830 procedure Rewrite_Renamed_Operator
9835 Nam : constant Name_Id := Chars (Op);
9836 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9840 -- Rewrite the operator node using the real operator, not its renaming.
9841 -- Exclude user-defined intrinsic operations of the same name, which are
9842 -- treated separately and rewritten as calls.
9844 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9845 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9846 Set_Chars (Op_Node, Nam);
9847 Set_Etype (Op_Node, Etype (N));
9848 Set_Entity (Op_Node, Op);
9849 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9851 -- Indicate that both the original entity and its renaming are
9852 -- referenced at this point.
9854 Generate_Reference (Entity (N), N);
9855 Generate_Reference (Op, N);
9858 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9861 Rewrite (N, Op_Node);
9863 -- If the context type is private, add the appropriate conversions so
9864 -- that the operator is applied to the full view. This is done in the
9865 -- routines that resolve intrinsic operators.
9867 if Is_Intrinsic_Subprogram (Op)
9868 and then Is_Private_Type (Typ)
9871 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9872 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9873 Resolve_Intrinsic_Operator (N, Typ);
9875 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9876 Resolve_Intrinsic_Unary_Operator (N, Typ);
9883 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9885 -- Operator renames a user-defined operator of the same name. Use the
9886 -- original operator in the node, which is the one Gigi knows about.
9889 Set_Is_Overloaded (N, False);
9891 end Rewrite_Renamed_Operator;
9893 -----------------------
9894 -- Set_Slice_Subtype --
9895 -----------------------
9897 -- Build an implicit subtype declaration to represent the type delivered by
9898 -- the slice. This is an abbreviated version of an array subtype. We define
9899 -- an index subtype for the slice, using either the subtype name or the
9900 -- discrete range of the slice. To be consistent with index usage elsewhere
9901 -- we create a list header to hold the single index. This list is not
9902 -- otherwise attached to the syntax tree.
9904 procedure Set_Slice_Subtype (N : Node_Id) is
9905 Loc : constant Source_Ptr := Sloc (N);
9906 Index_List : constant List_Id := New_List;
9908 Index_Subtype : Entity_Id;
9909 Index_Type : Entity_Id;
9910 Slice_Subtype : Entity_Id;
9911 Drange : constant Node_Id := Discrete_Range (N);
9914 if Is_Entity_Name (Drange) then
9915 Index_Subtype := Entity (Drange);
9918 -- We force the evaluation of a range. This is definitely needed in
9919 -- the renamed case, and seems safer to do unconditionally. Note in
9920 -- any case that since we will create and insert an Itype referring
9921 -- to this range, we must make sure any side effect removal actions
9922 -- are inserted before the Itype definition.
9924 if Nkind (Drange) = N_Range then
9925 Force_Evaluation (Low_Bound (Drange));
9926 Force_Evaluation (High_Bound (Drange));
9929 Index_Type := Base_Type (Etype (Drange));
9931 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9933 -- Take a new copy of Drange (where bounds have been rewritten to
9934 -- reference side-effect-free names). Using a separate tree ensures
9935 -- that further expansion (e.g. while rewriting a slice assignment
9936 -- into a FOR loop) does not attempt to remove side effects on the
9937 -- bounds again (which would cause the bounds in the index subtype
9938 -- definition to refer to temporaries before they are defined) (the
9939 -- reason is that some names are considered side effect free here
9940 -- for the subtype, but not in the context of a loop iteration
9943 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9944 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
9945 Set_Etype (Index_Subtype, Index_Type);
9946 Set_Size_Info (Index_Subtype, Index_Type);
9947 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9950 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9952 Index := New_Occurrence_Of (Index_Subtype, Loc);
9953 Set_Etype (Index, Index_Subtype);
9954 Append (Index, Index_List);
9956 Set_First_Index (Slice_Subtype, Index);
9957 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9958 Set_Is_Constrained (Slice_Subtype, True);
9960 Check_Compile_Time_Size (Slice_Subtype);
9962 -- The Etype of the existing Slice node is reset to this slice subtype.
9963 -- Its bounds are obtained from its first index.
9965 Set_Etype (N, Slice_Subtype);
9967 -- For packed slice subtypes, freeze immediately (except in the case of
9968 -- being in a "spec expression" where we never freeze when we first see
9971 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9972 Freeze_Itype (Slice_Subtype, N);
9974 -- For all other cases insert an itype reference in the slice's actions
9975 -- so that the itype is frozen at the proper place in the tree (i.e. at
9976 -- the point where actions for the slice are analyzed). Note that this
9977 -- is different from freezing the itype immediately, which might be
9978 -- premature (e.g. if the slice is within a transient scope). This needs
9979 -- to be done only if expansion is enabled.
9981 elsif Full_Expander_Active then
9982 Ensure_Defined (Typ => Slice_Subtype, N => N);
9984 end Set_Slice_Subtype;
9986 --------------------------------
9987 -- Set_String_Literal_Subtype --
9988 --------------------------------
9990 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9991 Loc : constant Source_Ptr := Sloc (N);
9992 Low_Bound : constant Node_Id :=
9993 Type_Low_Bound (Etype (First_Index (Typ)));
9994 Subtype_Id : Entity_Id;
9997 if Nkind (N) /= N_String_Literal then
10001 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
10002 Set_String_Literal_Length (Subtype_Id, UI_From_Int
10003 (String_Length (Strval (N))));
10004 Set_Etype (Subtype_Id, Base_Type (Typ));
10005 Set_Is_Constrained (Subtype_Id);
10006 Set_Etype (N, Subtype_Id);
10008 if Is_OK_Static_Expression (Low_Bound) then
10010 -- The low bound is set from the low bound of the corresponding index
10011 -- type. Note that we do not store the high bound in the string literal
10012 -- subtype, but it can be deduced if necessary from the length and the
10015 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
10018 -- If the lower bound is not static we create a range for the string
10019 -- literal, using the index type and the known length of the literal.
10020 -- The index type is not necessarily Positive, so the upper bound is
10021 -- computed as T'Val (T'Pos (Low_Bound) + L - 1)
10024 Index_List : constant List_Id := New_List;
10025 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
10027 High_Bound : constant Node_Id :=
10028 Make_Attribute_Reference (Loc,
10029 Attribute_Name => Name_Val,
10031 New_Occurrence_Of (Index_Type, Loc),
10032 Expressions => New_List (
10035 Make_Attribute_Reference (Loc,
10036 Attribute_Name => Name_Pos,
10038 New_Occurrence_Of (Index_Type, Loc),
10040 New_List (New_Copy_Tree (Low_Bound))),
10042 Make_Integer_Literal (Loc,
10043 String_Length (Strval (N)) - 1))));
10045 Array_Subtype : Entity_Id;
10046 Index_Subtype : Entity_Id;
10051 if Is_Integer_Type (Index_Type) then
10052 Set_String_Literal_Low_Bound
10053 (Subtype_Id, Make_Integer_Literal (Loc, 1));
10056 -- If the index type is an enumeration type, build bounds
10057 -- expression with attributes.
10059 Set_String_Literal_Low_Bound
10061 Make_Attribute_Reference (Loc,
10062 Attribute_Name => Name_First,
10064 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
10065 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
10068 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
10070 -- Build bona fide subtype for the string, and wrap it in an
10071 -- unchecked conversion, because the backend expects the
10072 -- String_Literal_Subtype to have a static lower bound.
10075 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10076 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
10077 Set_Scalar_Range (Index_Subtype, Drange);
10078 Set_Parent (Drange, N);
10079 Analyze_And_Resolve (Drange, Index_Type);
10081 -- In the context, the Index_Type may already have a constraint,
10082 -- so use common base type on string subtype. The base type may
10083 -- be used when generating attributes of the string, for example
10084 -- in the context of a slice assignment.
10086 Set_Etype (Index_Subtype, Base_Type (Index_Type));
10087 Set_Size_Info (Index_Subtype, Index_Type);
10088 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10090 Array_Subtype := Create_Itype (E_Array_Subtype, N);
10092 Index := New_Occurrence_Of (Index_Subtype, Loc);
10093 Set_Etype (Index, Index_Subtype);
10094 Append (Index, Index_List);
10096 Set_First_Index (Array_Subtype, Index);
10097 Set_Etype (Array_Subtype, Base_Type (Typ));
10098 Set_Is_Constrained (Array_Subtype, True);
10101 Make_Unchecked_Type_Conversion (Loc,
10102 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
10103 Expression => Relocate_Node (N)));
10104 Set_Etype (N, Array_Subtype);
10107 end Set_String_Literal_Subtype;
10109 ------------------------------
10110 -- Simplify_Type_Conversion --
10111 ------------------------------
10113 procedure Simplify_Type_Conversion (N : Node_Id) is
10115 if Nkind (N) = N_Type_Conversion then
10117 Operand : constant Node_Id := Expression (N);
10118 Target_Typ : constant Entity_Id := Etype (N);
10119 Opnd_Typ : constant Entity_Id := Etype (Operand);
10122 if Is_Floating_Point_Type (Opnd_Typ)
10124 (Is_Integer_Type (Target_Typ)
10125 or else (Is_Fixed_Point_Type (Target_Typ)
10126 and then Conversion_OK (N)))
10127 and then Nkind (Operand) = N_Attribute_Reference
10128 and then Attribute_Name (Operand) = Name_Truncation
10130 -- Special processing required if the conversion is the expression
10131 -- of a Truncation attribute reference. In this case we replace:
10133 -- ityp (ftyp'Truncation (x))
10139 -- with the Float_Truncate flag set, which is more efficient.
10143 Relocate_Node (First (Expressions (Operand))));
10144 Set_Float_Truncate (N, True);
10148 end Simplify_Type_Conversion;
10150 -----------------------------
10151 -- Unique_Fixed_Point_Type --
10152 -----------------------------
10154 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10155 T1 : Entity_Id := Empty;
10160 procedure Fixed_Point_Error;
10161 -- Give error messages for true ambiguity. Messages are posted on node
10162 -- N, and entities T1, T2 are the possible interpretations.
10164 -----------------------
10165 -- Fixed_Point_Error --
10166 -----------------------
10168 procedure Fixed_Point_Error is
10170 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10171 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10172 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10173 end Fixed_Point_Error;
10175 -- Start of processing for Unique_Fixed_Point_Type
10178 -- The operations on Duration are visible, so Duration is always a
10179 -- possible interpretation.
10181 T1 := Standard_Duration;
10183 -- Look for fixed-point types in enclosing scopes
10185 Scop := Current_Scope;
10186 while Scop /= Standard_Standard loop
10187 T2 := First_Entity (Scop);
10188 while Present (T2) loop
10189 if Is_Fixed_Point_Type (T2)
10190 and then Current_Entity (T2) = T2
10191 and then Scope (Base_Type (T2)) = Scop
10193 if Present (T1) then
10204 Scop := Scope (Scop);
10207 -- Look for visible fixed type declarations in the context
10209 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10210 while Present (Item) loop
10211 if Nkind (Item) = N_With_Clause then
10212 Scop := Entity (Name (Item));
10213 T2 := First_Entity (Scop);
10214 while Present (T2) loop
10215 if Is_Fixed_Point_Type (T2)
10216 and then Scope (Base_Type (T2)) = Scop
10217 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10219 if Present (T1) then
10234 if Nkind (N) = N_Real_Literal then
10235 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
10237 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
10241 end Unique_Fixed_Point_Type;
10243 ----------------------
10244 -- Valid_Conversion --
10245 ----------------------
10247 function Valid_Conversion
10249 Target : Entity_Id;
10251 Report_Errs : Boolean := True) return Boolean
10253 Target_Type : constant Entity_Id := Base_Type (Target);
10254 Opnd_Type : Entity_Id := Etype (Operand);
10256 function Conversion_Check
10258 Msg : String) return Boolean;
10259 -- Little routine to post Msg if Valid is False, returns Valid value
10261 -- The following are badly named, this kind of overloading is actively
10262 -- confusing in reading code, please rename to something like
10263 -- Error_Msg_N_If_Reporting ???
10265 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id);
10266 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10268 procedure Error_Msg_NE
10270 N : Node_Or_Entity_Id;
10271 E : Node_Or_Entity_Id);
10272 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10274 function Valid_Tagged_Conversion
10275 (Target_Type : Entity_Id;
10276 Opnd_Type : Entity_Id) return Boolean;
10277 -- Specifically test for validity of tagged conversions
10279 function Valid_Array_Conversion return Boolean;
10280 -- Check index and component conformance, and accessibility levels if
10281 -- the component types are anonymous access types (Ada 2005).
10283 ----------------------
10284 -- Conversion_Check --
10285 ----------------------
10287 function Conversion_Check
10289 Msg : String) return Boolean
10294 -- A generic unit has already been analyzed and we have verified
10295 -- that a particular conversion is OK in that context. Since the
10296 -- instance is reanalyzed without relying on the relationships
10297 -- established during the analysis of the generic, it is possible
10298 -- to end up with inconsistent views of private types. Do not emit
10299 -- the error message in such cases. The rest of the machinery in
10300 -- Valid_Conversion still ensures the proper compatibility of
10301 -- target and operand types.
10303 and then not In_Instance
10305 Error_Msg_N (Msg, Operand);
10309 end Conversion_Check;
10315 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id) is
10317 if Report_Errs then
10318 Errout.Error_Msg_N (Msg, N);
10326 procedure Error_Msg_NE
10328 N : Node_Or_Entity_Id;
10329 E : Node_Or_Entity_Id)
10332 if Report_Errs then
10333 Errout.Error_Msg_NE (Msg, N, E);
10337 ----------------------------
10338 -- Valid_Array_Conversion --
10339 ----------------------------
10341 function Valid_Array_Conversion return Boolean
10343 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10344 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10346 Opnd_Index : Node_Id;
10347 Opnd_Index_Type : Entity_Id;
10349 Target_Comp_Type : constant Entity_Id :=
10350 Component_Type (Target_Type);
10351 Target_Comp_Base : constant Entity_Id :=
10352 Base_Type (Target_Comp_Type);
10354 Target_Index : Node_Id;
10355 Target_Index_Type : Entity_Id;
10358 -- Error if wrong number of dimensions
10361 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10364 ("incompatible number of dimensions for conversion", Operand);
10367 -- Number of dimensions matches
10370 -- Loop through indexes of the two arrays
10372 Target_Index := First_Index (Target_Type);
10373 Opnd_Index := First_Index (Opnd_Type);
10374 while Present (Target_Index) and then Present (Opnd_Index) loop
10375 Target_Index_Type := Etype (Target_Index);
10376 Opnd_Index_Type := Etype (Opnd_Index);
10378 -- Error if index types are incompatible
10380 if not (Is_Integer_Type (Target_Index_Type)
10381 and then Is_Integer_Type (Opnd_Index_Type))
10382 and then (Root_Type (Target_Index_Type)
10383 /= Root_Type (Opnd_Index_Type))
10386 ("incompatible index types for array conversion",
10391 Next_Index (Target_Index);
10392 Next_Index (Opnd_Index);
10395 -- If component types have same base type, all set
10397 if Target_Comp_Base = Opnd_Comp_Base then
10400 -- Here if base types of components are not the same. The only
10401 -- time this is allowed is if we have anonymous access types.
10403 -- The conversion of arrays of anonymous access types can lead
10404 -- to dangling pointers. AI-392 formalizes the accessibility
10405 -- checks that must be applied to such conversions to prevent
10406 -- out-of-scope references.
10409 (Target_Comp_Base, E_Anonymous_Access_Type,
10410 E_Anonymous_Access_Subprogram_Type)
10411 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10413 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10415 if Type_Access_Level (Target_Type) <
10416 Deepest_Type_Access_Level (Opnd_Type)
10418 if In_Instance_Body then
10420 ("?source array type has " &
10421 "deeper accessibility level than target", Operand);
10423 ("\?Program_Error will be raised at run time",
10426 Make_Raise_Program_Error (Sloc (N),
10427 Reason => PE_Accessibility_Check_Failed));
10428 Set_Etype (N, Target_Type);
10431 -- Conversion not allowed because of accessibility levels
10435 ("source array type has " &
10436 "deeper accessibility level than target", Operand);
10444 -- All other cases where component base types do not match
10448 ("incompatible component types for array conversion",
10453 -- Check that component subtypes statically match. For numeric
10454 -- types this means that both must be either constrained or
10455 -- unconstrained. For enumeration types the bounds must match.
10456 -- All of this is checked in Subtypes_Statically_Match.
10458 if not Subtypes_Statically_Match
10459 (Target_Comp_Type, Opnd_Comp_Type)
10462 ("component subtypes must statically match", Operand);
10468 end Valid_Array_Conversion;
10470 -----------------------------
10471 -- Valid_Tagged_Conversion --
10472 -----------------------------
10474 function Valid_Tagged_Conversion
10475 (Target_Type : Entity_Id;
10476 Opnd_Type : Entity_Id) return Boolean
10479 -- Upward conversions are allowed (RM 4.6(22))
10481 if Covers (Target_Type, Opnd_Type)
10482 or else Is_Ancestor (Target_Type, Opnd_Type)
10486 -- Downward conversion are allowed if the operand is class-wide
10489 elsif Is_Class_Wide_Type (Opnd_Type)
10490 and then Covers (Opnd_Type, Target_Type)
10494 elsif Covers (Opnd_Type, Target_Type)
10495 or else Is_Ancestor (Opnd_Type, Target_Type)
10498 Conversion_Check (False,
10499 "downward conversion of tagged objects not allowed");
10501 -- Ada 2005 (AI-251): The conversion to/from interface types is
10504 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10507 -- If the operand is a class-wide type obtained through a limited_
10508 -- with clause, and the context includes the non-limited view, use
10509 -- it to determine whether the conversion is legal.
10511 elsif Is_Class_Wide_Type (Opnd_Type)
10512 and then From_With_Type (Opnd_Type)
10513 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10514 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10518 elsif Is_Access_Type (Opnd_Type)
10519 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10525 ("invalid tagged conversion, not compatible with}",
10526 N, First_Subtype (Opnd_Type));
10529 end Valid_Tagged_Conversion;
10531 -- Start of processing for Valid_Conversion
10534 Check_Parameterless_Call (Operand);
10536 if Is_Overloaded (Operand) then
10546 -- Remove procedure calls, which syntactically cannot appear in
10547 -- this context, but which cannot be removed by type checking,
10548 -- because the context does not impose a type.
10550 -- When compiling for VMS, spurious ambiguities can be produced
10551 -- when arithmetic operations have a literal operand and return
10552 -- System.Address or a descendant of it. These ambiguities are
10553 -- otherwise resolved by the context, but for conversions there
10554 -- is no context type and the removal of the spurious operations
10555 -- must be done explicitly here.
10557 -- The node may be labelled overloaded, but still contain only one
10558 -- interpretation because others were discarded earlier. If this
10559 -- is the case, retain the single interpretation if legal.
10561 Get_First_Interp (Operand, I, It);
10562 Opnd_Type := It.Typ;
10563 Get_Next_Interp (I, It);
10565 if Present (It.Typ)
10566 and then Opnd_Type /= Standard_Void_Type
10568 -- More than one candidate interpretation is available
10570 Get_First_Interp (Operand, I, It);
10571 while Present (It.Typ) loop
10572 if It.Typ = Standard_Void_Type then
10576 if Present (System_Aux_Id)
10577 and then Is_Descendent_Of_Address (It.Typ)
10582 Get_Next_Interp (I, It);
10586 Get_First_Interp (Operand, I, It);
10590 if No (It.Typ) then
10591 Error_Msg_N ("illegal operand in conversion", Operand);
10595 Get_Next_Interp (I, It);
10597 if Present (It.Typ) then
10600 It1 := Disambiguate (Operand, I1, I, Any_Type);
10602 if It1 = No_Interp then
10603 Error_Msg_N ("ambiguous operand in conversion", Operand);
10605 -- If the interpretation involves a standard operator, use
10606 -- the location of the type, which may be user-defined.
10608 if Sloc (It.Nam) = Standard_Location then
10609 Error_Msg_Sloc := Sloc (It.Typ);
10611 Error_Msg_Sloc := Sloc (It.Nam);
10614 Error_Msg_N -- CODEFIX
10615 ("\\possible interpretation#!", Operand);
10617 if Sloc (N1) = Standard_Location then
10618 Error_Msg_Sloc := Sloc (T1);
10620 Error_Msg_Sloc := Sloc (N1);
10623 Error_Msg_N -- CODEFIX
10624 ("\\possible interpretation#!", Operand);
10630 Set_Etype (Operand, It1.Typ);
10631 Opnd_Type := It1.Typ;
10637 if Is_Numeric_Type (Target_Type) then
10639 -- A universal fixed expression can be converted to any numeric type
10641 if Opnd_Type = Universal_Fixed then
10644 -- Also no need to check when in an instance or inlined body, because
10645 -- the legality has been established when the template was analyzed.
10646 -- Furthermore, numeric conversions may occur where only a private
10647 -- view of the operand type is visible at the instantiation point.
10648 -- This results in a spurious error if we check that the operand type
10649 -- is a numeric type.
10651 -- Note: in a previous version of this unit, the following tests were
10652 -- applied only for generated code (Comes_From_Source set to False),
10653 -- but in fact the test is required for source code as well, since
10654 -- this situation can arise in source code.
10656 elsif In_Instance or else In_Inlined_Body then
10659 -- Otherwise we need the conversion check
10662 return Conversion_Check
10663 (Is_Numeric_Type (Opnd_Type),
10664 "illegal operand for numeric conversion");
10669 elsif Is_Array_Type (Target_Type) then
10670 if not Is_Array_Type (Opnd_Type)
10671 or else Opnd_Type = Any_Composite
10672 or else Opnd_Type = Any_String
10674 Error_Msg_N ("illegal operand for array conversion", Operand);
10677 return Valid_Array_Conversion;
10680 -- Ada 2005 (AI-251): Anonymous access types where target references an
10683 elsif Ekind_In (Target_Type, E_General_Access_Type,
10684 E_Anonymous_Access_Type)
10685 and then Is_Interface (Directly_Designated_Type (Target_Type))
10687 -- Check the static accessibility rule of 4.6(17). Note that the
10688 -- check is not enforced when within an instance body, since the
10689 -- RM requires such cases to be caught at run time.
10691 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10692 if Type_Access_Level (Opnd_Type) >
10693 Deepest_Type_Access_Level (Target_Type)
10695 -- In an instance, this is a run-time check, but one we know
10696 -- will fail, so generate an appropriate warning. The raise
10697 -- will be generated by Expand_N_Type_Conversion.
10699 if In_Instance_Body then
10701 ("?cannot convert local pointer to non-local access type",
10704 ("\?Program_Error will be raised at run time", Operand);
10708 ("cannot convert local pointer to non-local access type",
10713 -- Special accessibility checks are needed in the case of access
10714 -- discriminants declared for a limited type.
10716 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10717 and then not Is_Local_Anonymous_Access (Opnd_Type)
10719 -- When the operand is a selected access discriminant the check
10720 -- needs to be made against the level of the object denoted by
10721 -- the prefix of the selected name (Object_Access_Level handles
10722 -- checking the prefix of the operand for this case).
10724 if Nkind (Operand) = N_Selected_Component
10725 and then Object_Access_Level (Operand) >
10726 Deepest_Type_Access_Level (Target_Type)
10728 -- In an instance, this is a run-time check, but one we know
10729 -- will fail, so generate an appropriate warning. The raise
10730 -- will be generated by Expand_N_Type_Conversion.
10732 if In_Instance_Body then
10734 ("?cannot convert access discriminant to non-local" &
10735 " access type", Operand);
10737 ("\?Program_Error will be raised at run time", Operand);
10740 ("cannot convert access discriminant to non-local" &
10741 " access type", Operand);
10746 -- The case of a reference to an access discriminant from
10747 -- within a limited type declaration (which will appear as
10748 -- a discriminal) is always illegal because the level of the
10749 -- discriminant is considered to be deeper than any (nameable)
10752 if Is_Entity_Name (Operand)
10753 and then not Is_Local_Anonymous_Access (Opnd_Type)
10755 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10756 and then Present (Discriminal_Link (Entity (Operand)))
10759 ("discriminant has deeper accessibility level than target",
10768 -- General and anonymous access types
10770 elsif Ekind_In (Target_Type, E_General_Access_Type,
10771 E_Anonymous_Access_Type)
10774 (Is_Access_Type (Opnd_Type)
10776 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10777 E_Access_Protected_Subprogram_Type),
10778 "must be an access-to-object type")
10780 if Is_Access_Constant (Opnd_Type)
10781 and then not Is_Access_Constant (Target_Type)
10784 ("access-to-constant operand type not allowed", Operand);
10788 -- Check the static accessibility rule of 4.6(17). Note that the
10789 -- check is not enforced when within an instance body, since the RM
10790 -- requires such cases to be caught at run time.
10792 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10793 or else Is_Local_Anonymous_Access (Target_Type)
10794 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
10795 N_Object_Declaration
10797 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
10798 -- conversions from an anonymous access type to a named general
10799 -- access type. Such conversions are not allowed in the case of
10800 -- access parameters and stand-alone objects of an anonymous
10801 -- access type. The implicit conversion case is recognized by
10802 -- testing that Comes_From_Source is False and that it's been
10803 -- rewritten. The Comes_From_Source test isn't sufficient because
10804 -- nodes in inlined calls to predefined library routines can have
10805 -- Comes_From_Source set to False. (Is there a better way to test
10806 -- for implicit conversions???)
10808 if Ada_Version >= Ada_2012
10809 and then not Comes_From_Source (N)
10810 and then N /= Original_Node (N)
10811 and then Ekind (Target_Type) = E_General_Access_Type
10812 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
10814 if Is_Itype (Opnd_Type) then
10816 -- Implicit conversions aren't allowed for objects of an
10817 -- anonymous access type, since such objects have nonstatic
10818 -- levels in Ada 2012.
10820 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
10821 N_Object_Declaration
10824 ("implicit conversion of stand-alone anonymous " &
10825 "access object not allowed", Operand);
10828 -- Implicit conversions aren't allowed for anonymous access
10829 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
10830 -- is done to exclude anonymous access results.
10832 elsif not Is_Local_Anonymous_Access (Opnd_Type)
10833 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
10834 N_Function_Specification,
10835 N_Procedure_Specification)
10838 ("implicit conversion of anonymous access formal " &
10839 "not allowed", Operand);
10842 -- This is a case where there's an enclosing object whose
10843 -- to which the "statically deeper than" relationship does
10844 -- not apply (such as an access discriminant selected from
10845 -- a dereference of an access parameter).
10847 elsif Object_Access_Level (Operand)
10848 = Scope_Depth (Standard_Standard)
10851 ("implicit conversion of anonymous access value " &
10852 "not allowed", Operand);
10855 -- In other cases, the level of the operand's type must be
10856 -- statically less deep than that of the target type, else
10857 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
10859 elsif Type_Access_Level (Opnd_Type) >
10860 Deepest_Type_Access_Level (Target_Type)
10863 ("implicit conversion of anonymous access value " &
10864 "violates accessibility", Operand);
10869 elsif Type_Access_Level (Opnd_Type) >
10870 Deepest_Type_Access_Level (Target_Type)
10872 -- In an instance, this is a run-time check, but one we know
10873 -- will fail, so generate an appropriate warning. The raise
10874 -- will be generated by Expand_N_Type_Conversion.
10876 if In_Instance_Body then
10878 ("?cannot convert local pointer to non-local access type",
10881 ("\?Program_Error will be raised at run time", Operand);
10884 -- Avoid generation of spurious error message
10886 if not Error_Posted (N) then
10888 ("cannot convert local pointer to non-local access type",
10895 -- Special accessibility checks are needed in the case of access
10896 -- discriminants declared for a limited type.
10898 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10899 and then not Is_Local_Anonymous_Access (Opnd_Type)
10901 -- When the operand is a selected access discriminant the check
10902 -- needs to be made against the level of the object denoted by
10903 -- the prefix of the selected name (Object_Access_Level handles
10904 -- checking the prefix of the operand for this case).
10906 if Nkind (Operand) = N_Selected_Component
10907 and then Object_Access_Level (Operand) >
10908 Deepest_Type_Access_Level (Target_Type)
10910 -- In an instance, this is a run-time check, but one we know
10911 -- will fail, so generate an appropriate warning. The raise
10912 -- will be generated by Expand_N_Type_Conversion.
10914 if In_Instance_Body then
10916 ("?cannot convert access discriminant to non-local" &
10917 " access type", Operand);
10919 ("\?Program_Error will be raised at run time",
10924 ("cannot convert access discriminant to non-local" &
10925 " access type", Operand);
10930 -- The case of a reference to an access discriminant from
10931 -- within a limited type declaration (which will appear as
10932 -- a discriminal) is always illegal because the level of the
10933 -- discriminant is considered to be deeper than any (nameable)
10936 if Is_Entity_Name (Operand)
10938 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10939 and then Present (Discriminal_Link (Entity (Operand)))
10942 ("discriminant has deeper accessibility level than target",
10949 -- In the presence of limited_with clauses we have to use non-limited
10950 -- views, if available.
10952 Check_Limited : declare
10953 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10954 -- Helper function to handle limited views
10956 --------------------------
10957 -- Full_Designated_Type --
10958 --------------------------
10960 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10961 Desig : constant Entity_Id := Designated_Type (T);
10964 -- Handle the limited view of a type
10966 if Is_Incomplete_Type (Desig)
10967 and then From_With_Type (Desig)
10968 and then Present (Non_Limited_View (Desig))
10970 return Available_View (Desig);
10974 end Full_Designated_Type;
10976 -- Local Declarations
10978 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10979 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10981 Same_Base : constant Boolean :=
10982 Base_Type (Target) = Base_Type (Opnd);
10984 -- Start of processing for Check_Limited
10987 if Is_Tagged_Type (Target) then
10988 return Valid_Tagged_Conversion (Target, Opnd);
10991 if not Same_Base then
10993 ("target designated type not compatible with }",
10994 N, Base_Type (Opnd));
10997 -- Ada 2005 AI-384: legality rule is symmetric in both
10998 -- designated types. The conversion is legal (with possible
10999 -- constraint check) if either designated type is
11002 elsif Subtypes_Statically_Match (Target, Opnd)
11004 (Has_Discriminants (Target)
11006 (not Is_Constrained (Opnd)
11007 or else not Is_Constrained (Target)))
11009 -- Special case, if Value_Size has been used to make the
11010 -- sizes different, the conversion is not allowed even
11011 -- though the subtypes statically match.
11013 if Known_Static_RM_Size (Target)
11014 and then Known_Static_RM_Size (Opnd)
11015 and then RM_Size (Target) /= RM_Size (Opnd)
11018 ("target designated subtype not compatible with }",
11021 ("\because sizes of the two designated subtypes differ",
11025 -- Normal case where conversion is allowed
11033 ("target designated subtype not compatible with }",
11040 -- Access to subprogram types. If the operand is an access parameter,
11041 -- the type has a deeper accessibility that any master, and cannot be
11042 -- assigned. We must make an exception if the conversion is part of an
11043 -- assignment and the target is the return object of an extended return
11044 -- statement, because in that case the accessibility check takes place
11045 -- after the return.
11047 elsif Is_Access_Subprogram_Type (Target_Type)
11048 and then No (Corresponding_Remote_Type (Opnd_Type))
11050 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
11051 and then Is_Entity_Name (Operand)
11052 and then Ekind (Entity (Operand)) = E_In_Parameter
11054 (Nkind (Parent (N)) /= N_Assignment_Statement
11055 or else not Is_Entity_Name (Name (Parent (N)))
11056 or else not Is_Return_Object (Entity (Name (Parent (N)))))
11059 ("illegal attempt to store anonymous access to subprogram",
11062 ("\value has deeper accessibility than any master " &
11063 "(RM 3.10.2 (13))",
11067 ("\use named access type for& instead of access parameter",
11068 Operand, Entity (Operand));
11071 -- Check that the designated types are subtype conformant
11073 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
11074 Old_Id => Designated_Type (Opnd_Type),
11077 -- Check the static accessibility rule of 4.6(20)
11079 if Type_Access_Level (Opnd_Type) >
11080 Deepest_Type_Access_Level (Target_Type)
11083 ("operand type has deeper accessibility level than target",
11086 -- Check that if the operand type is declared in a generic body,
11087 -- then the target type must be declared within that same body
11088 -- (enforces last sentence of 4.6(20)).
11090 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
11092 O_Gen : constant Node_Id :=
11093 Enclosing_Generic_Body (Opnd_Type);
11098 T_Gen := Enclosing_Generic_Body (Target_Type);
11099 while Present (T_Gen) and then T_Gen /= O_Gen loop
11100 T_Gen := Enclosing_Generic_Body (T_Gen);
11103 if T_Gen /= O_Gen then
11105 ("target type must be declared in same generic body"
11106 & " as operand type", N);
11113 -- Remote subprogram access types
11115 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
11116 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
11118 -- It is valid to convert from one RAS type to another provided
11119 -- that their specification statically match.
11121 Check_Subtype_Conformant
11123 Designated_Type (Corresponding_Remote_Type (Target_Type)),
11125 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
11130 -- If it was legal in the generic, it's legal in the instance
11132 elsif In_Instance_Body then
11135 -- If both are tagged types, check legality of view conversions
11137 elsif Is_Tagged_Type (Target_Type)
11139 Is_Tagged_Type (Opnd_Type)
11141 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
11143 -- Types derived from the same root type are convertible
11145 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
11148 -- In an instance or an inlined body, there may be inconsistent views of
11149 -- the same type, or of types derived from a common root.
11151 elsif (In_Instance or In_Inlined_Body)
11153 Root_Type (Underlying_Type (Target_Type)) =
11154 Root_Type (Underlying_Type (Opnd_Type))
11158 -- Special check for common access type error case
11160 elsif Ekind (Target_Type) = E_Access_Type
11161 and then Is_Access_Type (Opnd_Type)
11163 Error_Msg_N ("target type must be general access type!", N);
11164 Error_Msg_NE -- CODEFIX
11165 ("add ALL to }!", N, Target_Type);
11169 Error_Msg_NE ("invalid conversion, not compatible with }",
11173 end Valid_Conversion;