1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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 Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
48 with Output; use Output;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Aggr; use Sem_Aggr;
55 with Sem_Attr; use Sem_Attr;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch4; use Sem_Ch4;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Elim; use Sem_Elim;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Intr; use Sem_Intr;
67 with Sem_Util; use Sem_Util;
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
88 -- type information recursively to the descendants of N. If the node
89 -- is not overloaded, its Etype is established in the first pass. If
90 -- overloaded, the Resolve routines set the correct type. For arith.
91 -- operators, the 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
140 -- declaration, 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
230 -- to the corresponding predefined operator, with suitable conversions.
231 -- Note that this applies only for intrinsic operators that denote
232 -- predefined operators, not operators that are intrinsic imports of
233 -- back-end builtins.
235 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
236 -- Ditto, for unary operators (arithmetic ones and "not" on signed
237 -- integer types for VMS).
239 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
240 -- If an operator node resolves to a call to a user-defined operator,
241 -- rewrite the node as a function call.
243 procedure Make_Call_Into_Operator
247 -- Inverse transformation: if an operator is given in functional notation,
248 -- then after resolving the node, transform into an operator node, so
249 -- that operands are resolved properly. Recall that predefined operators
250 -- do not have a full signature and special resolution rules apply.
252 procedure Rewrite_Renamed_Operator
256 -- An operator can rename another, e.g. in an instantiation. In that
257 -- case, the proper operator node must be constructed and resolved.
259 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
260 -- The String_Literal_Subtype is built for all strings that are not
261 -- operands of a static concatenation operation. If the argument is
262 -- not a N_String_Literal node, then the call has no effect.
264 procedure Set_Slice_Subtype (N : Node_Id);
265 -- Build subtype of array type, with the range specified by the slice
267 procedure Simplify_Type_Conversion (N : Node_Id);
268 -- Called after N has been resolved and evaluated, but before range checks
269 -- have been applied. Currently simplifies a combination of floating-point
270 -- to integer conversion and Truncation attribute.
272 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
273 -- A universal_fixed expression in an universal context is unambiguous
274 -- if there is only one applicable fixed point type. Determining whether
275 -- there is only one requires a search over all visible entities, and
276 -- happens only in very pathological cases (see 6115-006).
278 function Valid_Conversion
281 Operand : Node_Id) return Boolean;
282 -- Verify legality rules given in 4.6 (8-23). Target is the target
283 -- type of the conversion, which may be an implicit conversion of
284 -- an actual parameter to an anonymous access type (in which case
285 -- N denotes the actual parameter and N = Operand).
287 -------------------------
288 -- Ambiguous_Character --
289 -------------------------
291 procedure Ambiguous_Character (C : Node_Id) is
295 if Nkind (C) = N_Character_Literal then
296 Error_Msg_N ("ambiguous character literal", C);
298 -- First the ones in Standard
300 Error_Msg_N ("\\possible interpretation: Character!", C);
301 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
303 -- Include Wide_Wide_Character in Ada 2005 mode
305 if Ada_Version >= Ada_2005 then
306 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
309 -- Now any other types that match
311 E := Current_Entity (C);
312 while Present (E) loop
313 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
317 end Ambiguous_Character;
319 -------------------------
320 -- Analyze_And_Resolve --
321 -------------------------
323 procedure Analyze_And_Resolve (N : Node_Id) is
327 end Analyze_And_Resolve;
329 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
333 end Analyze_And_Resolve;
335 -- Version withs check(s) suppressed
337 procedure Analyze_And_Resolve
342 Scop : constant Entity_Id := Current_Scope;
345 if Suppress = All_Checks then
347 Svg : constant Suppress_Array := Scope_Suppress;
349 Scope_Suppress := (others => True);
350 Analyze_And_Resolve (N, Typ);
351 Scope_Suppress := Svg;
356 Svg : constant Boolean := Scope_Suppress (Suppress);
359 Scope_Suppress (Suppress) := True;
360 Analyze_And_Resolve (N, Typ);
361 Scope_Suppress (Suppress) := Svg;
365 if Current_Scope /= Scop
366 and then Scope_Is_Transient
368 -- This can only happen if a transient scope was created
369 -- for an inner expression, which will be removed upon
370 -- completion of the analysis of an enclosing construct.
371 -- The transient scope must have the suppress status of
372 -- the enclosing environment, not of this Analyze call.
374 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
377 end Analyze_And_Resolve;
379 procedure Analyze_And_Resolve
383 Scop : constant Entity_Id := Current_Scope;
386 if Suppress = All_Checks then
388 Svg : constant Suppress_Array := Scope_Suppress;
390 Scope_Suppress := (others => True);
391 Analyze_And_Resolve (N);
392 Scope_Suppress := Svg;
397 Svg : constant Boolean := Scope_Suppress (Suppress);
400 Scope_Suppress (Suppress) := True;
401 Analyze_And_Resolve (N);
402 Scope_Suppress (Suppress) := Svg;
406 if Current_Scope /= Scop
407 and then Scope_Is_Transient
409 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
412 end Analyze_And_Resolve;
414 ----------------------------------------
415 -- Bad_Unordered_Enumeration_Reference --
416 ----------------------------------------
418 function Bad_Unordered_Enumeration_Reference
420 T : Entity_Id) return Boolean
423 return Is_Enumeration_Type (T)
424 and then Comes_From_Source (N)
425 and then Warn_On_Unordered_Enumeration_Type
426 and then not Has_Pragma_Ordered (T)
427 and then not In_Same_Extended_Unit (N, T);
428 end Bad_Unordered_Enumeration_Reference;
430 ----------------------------
431 -- Check_Discriminant_Use --
432 ----------------------------
434 procedure Check_Discriminant_Use (N : Node_Id) is
435 PN : constant Node_Id := Parent (N);
436 Disc : constant Entity_Id := Entity (N);
441 -- Any use in a spec-expression is legal
443 if In_Spec_Expression then
446 elsif Nkind (PN) = N_Range then
448 -- Discriminant cannot be used to constrain a scalar type
452 if Nkind (P) = N_Range_Constraint
453 and then Nkind (Parent (P)) = N_Subtype_Indication
454 and then Nkind (Parent (Parent (P))) = N_Component_Definition
456 Error_Msg_N ("discriminant cannot constrain scalar type", N);
458 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
460 -- The following check catches the unusual case where
461 -- a discriminant appears within an index constraint
462 -- that is part of a larger expression within a constraint
463 -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
464 -- For now we only check case of record components, and
465 -- note that a similar check should also apply in the
466 -- case of discriminant constraints below. ???
468 -- Note that the check for N_Subtype_Declaration below is to
469 -- detect the valid use of discriminants in the constraints of a
470 -- subtype declaration when this subtype declaration appears
471 -- inside the scope of a record type (which is syntactically
472 -- illegal, but which may be created as part of derived type
473 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
476 if Ekind (Current_Scope) = E_Record_Type
477 and then Scope (Disc) = Current_Scope
479 (Nkind (Parent (P)) = N_Subtype_Indication
481 Nkind_In (Parent (Parent (P)), N_Component_Definition,
482 N_Subtype_Declaration)
483 and then Paren_Count (N) = 0)
486 ("discriminant must appear alone in component constraint", N);
490 -- Detect a common error:
492 -- type R (D : Positive := 100) is record
493 -- Name : String (1 .. D);
496 -- The default value causes an object of type R to be allocated
497 -- with room for Positive'Last characters. The RM does not mandate
498 -- the allocation of the maximum size, but that is what GNAT does
499 -- so we should warn the programmer that there is a problem.
501 Check_Large : declare
507 function Large_Storage_Type (T : Entity_Id) return Boolean;
508 -- Return True if type T has a large enough range that
509 -- any array whose index type covered the whole range of
510 -- the type would likely raise Storage_Error.
512 ------------------------
513 -- Large_Storage_Type --
514 ------------------------
516 function Large_Storage_Type (T : Entity_Id) return Boolean is
518 -- The type is considered large if its bounds are known at
519 -- compile time and if it requires at least as many bits as
520 -- a Positive to store the possible values.
522 return Compile_Time_Known_Value (Type_Low_Bound (T))
523 and then Compile_Time_Known_Value (Type_High_Bound (T))
525 Minimum_Size (T, Biased => True) >=
526 RM_Size (Standard_Positive);
527 end Large_Storage_Type;
529 -- Start of processing for Check_Large
532 -- Check that the Disc has a large range
534 if not Large_Storage_Type (Etype (Disc)) then
538 -- If the enclosing type is limited, we allocate only the
539 -- default value, not the maximum, and there is no need for
542 if Is_Limited_Type (Scope (Disc)) then
546 -- Check that it is the high bound
548 if N /= High_Bound (PN)
549 or else No (Discriminant_Default_Value (Disc))
554 -- Check the array allows a large range at this bound.
555 -- First find the array
559 if Nkind (SI) /= N_Subtype_Indication then
563 T := Entity (Subtype_Mark (SI));
565 if not Is_Array_Type (T) then
569 -- Next, find the dimension
571 TB := First_Index (T);
572 CB := First (Constraints (P));
574 and then Present (TB)
575 and then Present (CB)
586 -- Now, check the dimension has a large range
588 if not Large_Storage_Type (Etype (TB)) then
592 -- Warn about the danger
595 ("?creation of & object may raise Storage_Error!",
604 -- Legal case is in index or discriminant constraint
606 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
607 N_Discriminant_Association)
609 if Paren_Count (N) > 0 then
611 ("discriminant in constraint must appear alone", N);
613 elsif Nkind (N) = N_Expanded_Name
614 and then Comes_From_Source (N)
617 ("discriminant must appear alone as a direct name", N);
622 -- Otherwise, context is an expression. It should not be within
623 -- (i.e. a subexpression of) a constraint for a component.
628 while not Nkind_In (P, N_Component_Declaration,
629 N_Subtype_Indication,
637 -- If the discriminant is used in an expression that is a bound
638 -- of a scalar type, an Itype is created and the bounds are attached
639 -- to its range, not to the original subtype indication. Such use
640 -- is of course a double fault.
642 if (Nkind (P) = N_Subtype_Indication
643 and then Nkind_In (Parent (P), N_Component_Definition,
644 N_Derived_Type_Definition)
645 and then D = Constraint (P))
647 -- The constraint itself may be given by a subtype indication,
648 -- rather than by a more common discrete range.
650 or else (Nkind (P) = N_Subtype_Indication
652 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
653 or else Nkind (P) = N_Entry_Declaration
654 or else Nkind (D) = N_Defining_Identifier
657 ("discriminant in constraint must appear alone", N);
660 end Check_Discriminant_Use;
662 --------------------------------
663 -- Check_For_Visible_Operator --
664 --------------------------------
666 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
668 if Is_Invisible_Operator (N, T) then
669 Error_Msg_NE -- CODEFIX
670 ("operator for} is not directly visible!", N, First_Subtype (T));
671 Error_Msg_N -- CODEFIX
672 ("use clause would make operation legal!", N);
674 end Check_For_Visible_Operator;
676 ----------------------------------
677 -- Check_Fully_Declared_Prefix --
678 ----------------------------------
680 procedure Check_Fully_Declared_Prefix
685 -- Check that the designated type of the prefix of a dereference is
686 -- not an incomplete type. This cannot be done unconditionally, because
687 -- dereferences of private types are legal in default expressions. This
688 -- case is taken care of in Check_Fully_Declared, called below. There
689 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
691 -- This consideration also applies to similar checks for allocators,
692 -- qualified expressions, and type conversions.
694 -- An additional exception concerns other per-object expressions that
695 -- are not directly related to component declarations, in particular
696 -- representation pragmas for tasks. These will be per-object
697 -- expressions if they depend on discriminants or some global entity.
698 -- If the task has access discriminants, the designated type may be
699 -- incomplete at the point the expression is resolved. This resolution
700 -- takes place within the body of the initialization procedure, where
701 -- the discriminant is replaced by its discriminal.
703 if Is_Entity_Name (Pref)
704 and then Ekind (Entity (Pref)) = E_In_Parameter
708 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
709 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
710 -- Analyze_Object_Renaming, and Freeze_Entity.
712 elsif Ada_Version >= Ada_2005
713 and then Is_Entity_Name (Pref)
714 and then Is_Access_Type (Etype (Pref))
715 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
717 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
721 Check_Fully_Declared (Typ, Parent (Pref));
723 end Check_Fully_Declared_Prefix;
725 ------------------------------
726 -- Check_Infinite_Recursion --
727 ------------------------------
729 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
733 function Same_Argument_List return Boolean;
734 -- Check whether list of actuals is identical to list of formals
735 -- of called function (which is also the enclosing scope).
737 ------------------------
738 -- Same_Argument_List --
739 ------------------------
741 function Same_Argument_List return Boolean is
747 if not Is_Entity_Name (Name (N)) then
750 Subp := Entity (Name (N));
753 F := First_Formal (Subp);
754 A := First_Actual (N);
755 while Present (F) and then Present (A) loop
756 if not Is_Entity_Name (A)
757 or else Entity (A) /= F
767 end Same_Argument_List;
769 -- Start of processing for Check_Infinite_Recursion
772 -- Special case, if this is a procedure call and is a call to the
773 -- current procedure with the same argument list, then this is for
774 -- sure an infinite recursion and we insert a call to raise SE.
776 if Is_List_Member (N)
777 and then List_Length (List_Containing (N)) = 1
778 and then Same_Argument_List
781 P : constant Node_Id := Parent (N);
783 if Nkind (P) = N_Handled_Sequence_Of_Statements
784 and then Nkind (Parent (P)) = N_Subprogram_Body
785 and then Is_Empty_List (Declarations (Parent (P)))
787 Error_Msg_N ("!?infinite recursion", N);
788 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
790 Make_Raise_Storage_Error (Sloc (N),
791 Reason => SE_Infinite_Recursion));
797 -- If not that special case, search up tree, quitting if we reach a
798 -- construct (e.g. a conditional) that tells us that this is not a
799 -- case for an infinite recursion warning.
805 -- If no parent, then we were not inside a subprogram, this can for
806 -- example happen when processing certain pragmas in a spec. Just
807 -- return False in this case.
813 -- Done if we get to subprogram body, this is definitely an infinite
814 -- recursion case if we did not find anything to stop us.
816 exit when Nkind (P) = N_Subprogram_Body;
818 -- If appearing in conditional, result is false
820 if Nkind_In (P, N_Or_Else,
824 N_Conditional_Expression,
829 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
830 and then C /= First (Statements (P))
832 -- If the call is the expression of a return statement and the
833 -- actuals are identical to the formals, it's worth a warning.
834 -- However, we skip this if there is an immediately preceding
835 -- raise statement, since the call is never executed.
837 -- Furthermore, this corresponds to a common idiom:
839 -- function F (L : Thing) return Boolean is
841 -- raise Program_Error;
845 -- for generating a stub function
847 if Nkind (Parent (N)) = N_Simple_Return_Statement
848 and then Same_Argument_List
850 exit when not Is_List_Member (Parent (N));
852 -- OK, return statement is in a statement list, look for raise
858 -- Skip past N_Freeze_Entity nodes generated by expansion
860 Nod := Prev (Parent (N));
862 and then Nkind (Nod) = N_Freeze_Entity
867 -- If no raise statement, give warning
869 exit when Nkind (Nod) /= N_Raise_Statement
871 (Nkind (Nod) not in N_Raise_xxx_Error
872 or else Present (Condition (Nod)));
883 Error_Msg_N ("!?possible infinite recursion", N);
884 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
887 end Check_Infinite_Recursion;
889 -------------------------------
890 -- Check_Initialization_Call --
891 -------------------------------
893 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
894 Typ : constant Entity_Id := Etype (First_Formal (Nam));
896 function Uses_SS (T : Entity_Id) return Boolean;
897 -- Check whether the creation of an object of the type will involve
898 -- use of the secondary stack. If T is a record type, this is true
899 -- if the expression for some component uses the secondary stack, e.g.
900 -- through a call to a function that returns an unconstrained value.
901 -- False if T is controlled, because cleanups occur elsewhere.
907 function Uses_SS (T : Entity_Id) return Boolean is
910 Full_Type : Entity_Id := Underlying_Type (T);
913 -- Normally we want to use the underlying type, but if it's not set
914 -- then continue with T.
916 if not Present (Full_Type) then
920 if Is_Controlled (Full_Type) then
923 elsif Is_Array_Type (Full_Type) then
924 return Uses_SS (Component_Type (Full_Type));
926 elsif Is_Record_Type (Full_Type) then
927 Comp := First_Component (Full_Type);
928 while Present (Comp) loop
929 if Ekind (Comp) = E_Component
930 and then Nkind (Parent (Comp)) = N_Component_Declaration
932 -- The expression for a dynamic component may be rewritten
933 -- as a dereference, so retrieve original node.
935 Expr := Original_Node (Expression (Parent (Comp)));
937 -- Return True if the expression is a call to a function
938 -- (including an attribute function such as Image, or a
939 -- user-defined operator) with a result that requires a
942 if (Nkind (Expr) = N_Function_Call
943 or else Nkind (Expr) in N_Op
944 or else (Nkind (Expr) = N_Attribute_Reference
945 and then Present (Expressions (Expr))))
946 and then Requires_Transient_Scope (Etype (Expr))
950 elsif Uses_SS (Etype (Comp)) then
955 Next_Component (Comp);
965 -- Start of processing for Check_Initialization_Call
968 -- Establish a transient scope if the type needs it
970 if Uses_SS (Typ) then
971 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
973 end Check_Initialization_Call;
975 ---------------------------------------
976 -- Check_No_Direct_Boolean_Operators --
977 ---------------------------------------
979 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
981 if Scope (Entity (N)) = Standard_Standard
982 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
984 -- Restriction only applies to original source code
986 if Comes_From_Source (N) then
987 Check_Restriction (No_Direct_Boolean_Operators, N);
992 Check_Boolean_Operator (N);
994 end Check_No_Direct_Boolean_Operators;
996 ------------------------------
997 -- Check_Parameterless_Call --
998 ------------------------------
1000 procedure Check_Parameterless_Call (N : Node_Id) is
1003 function Prefix_Is_Access_Subp return Boolean;
1004 -- If the prefix is of an access_to_subprogram type, the node must be
1005 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1006 -- interpretations are access to subprograms.
1008 ---------------------------
1009 -- Prefix_Is_Access_Subp --
1010 ---------------------------
1012 function Prefix_Is_Access_Subp return Boolean is
1017 -- If the context is an attribute reference that can apply to
1018 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1020 if Nkind (Parent (N)) = N_Attribute_Reference
1021 and then (Attribute_Name (Parent (N)) = Name_Address
1022 or else Attribute_Name (Parent (N)) = Name_Code_Address
1023 or else Attribute_Name (Parent (N)) = Name_Access)
1028 if not Is_Overloaded (N) then
1030 Ekind (Etype (N)) = E_Subprogram_Type
1031 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1033 Get_First_Interp (N, I, It);
1034 while Present (It.Typ) loop
1035 if Ekind (It.Typ) /= E_Subprogram_Type
1036 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1041 Get_Next_Interp (I, It);
1046 end Prefix_Is_Access_Subp;
1048 -- Start of processing for Check_Parameterless_Call
1051 -- Defend against junk stuff if errors already detected
1053 if Total_Errors_Detected /= 0 then
1054 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1056 elsif Nkind (N) in N_Has_Chars
1057 and then Chars (N) in Error_Name_Or_No_Name
1065 -- If the context expects a value, and the name is a procedure, this is
1066 -- most likely a missing 'Access. Don't try to resolve the parameterless
1067 -- call, error will be caught when the outer call is analyzed.
1069 if Is_Entity_Name (N)
1070 and then Ekind (Entity (N)) = E_Procedure
1071 and then not Is_Overloaded (N)
1073 Nkind_In (Parent (N), N_Parameter_Association,
1075 N_Procedure_Call_Statement)
1080 -- Rewrite as call if overloadable entity that is (or could be, in the
1081 -- overloaded case) a function call. If we know for sure that the entity
1082 -- is an enumeration literal, we do not rewrite it.
1084 -- If the entity is the name of an operator, it cannot be a call because
1085 -- operators cannot have default parameters. In this case, this must be
1086 -- a string whose contents coincide with an operator name. Set the kind
1087 -- of the node appropriately.
1089 if (Is_Entity_Name (N)
1090 and then Nkind (N) /= N_Operator_Symbol
1091 and then Is_Overloadable (Entity (N))
1092 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1093 or else Is_Overloaded (N)))
1095 -- Rewrite as call if it is an explicit dereference of an expression of
1096 -- a subprogram access type, and the subprogram type is not that of a
1097 -- procedure or entry.
1100 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1102 -- Rewrite as call if it is a selected component which is a function,
1103 -- this is the case of a call to a protected function (which may be
1104 -- overloaded with other protected operations).
1107 (Nkind (N) = N_Selected_Component
1108 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1110 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1112 and then Is_Overloaded (Selector_Name (N)))))
1114 -- If one of the above three conditions is met, rewrite as call.
1115 -- Apply the rewriting only once.
1118 if Nkind (Parent (N)) /= N_Function_Call
1119 or else N /= Name (Parent (N))
1121 Nam := New_Copy (N);
1123 -- If overloaded, overload set belongs to new copy
1125 Save_Interps (N, Nam);
1127 -- Change node to parameterless function call (note that the
1128 -- Parameter_Associations associations field is left set to Empty,
1129 -- its normal default value since there are no parameters)
1131 Change_Node (N, N_Function_Call);
1133 Set_Sloc (N, Sloc (Nam));
1137 elsif Nkind (N) = N_Parameter_Association then
1138 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1140 elsif Nkind (N) = N_Operator_Symbol then
1141 Change_Operator_Symbol_To_String_Literal (N);
1142 Set_Is_Overloaded (N, False);
1143 Set_Etype (N, Any_String);
1145 end Check_Parameterless_Call;
1147 -----------------------------
1148 -- Is_Definite_Access_Type --
1149 -----------------------------
1151 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1152 Btyp : constant Entity_Id := Base_Type (E);
1154 return Ekind (Btyp) = E_Access_Type
1155 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1156 and then Comes_From_Source (Btyp));
1157 end Is_Definite_Access_Type;
1159 ----------------------
1160 -- Is_Predefined_Op --
1161 ----------------------
1163 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1165 -- Predefined operators are intrinsic subprograms
1167 if not Is_Intrinsic_Subprogram (Nam) then
1171 -- A call to a back-end builtin is never a predefined operator
1173 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1177 return not Is_Generic_Instance (Nam)
1178 and then Chars (Nam) in Any_Operator_Name
1179 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1180 end Is_Predefined_Op;
1182 -----------------------------
1183 -- Make_Call_Into_Operator --
1184 -----------------------------
1186 procedure Make_Call_Into_Operator
1191 Op_Name : constant Name_Id := Chars (Op_Id);
1192 Act1 : Node_Id := First_Actual (N);
1193 Act2 : Node_Id := Next_Actual (Act1);
1194 Error : Boolean := False;
1195 Func : constant Entity_Id := Entity (Name (N));
1196 Is_Binary : constant Boolean := Present (Act2);
1198 Opnd_Type : Entity_Id;
1199 Orig_Type : Entity_Id := Empty;
1202 type Kind_Test is access function (E : Entity_Id) return Boolean;
1204 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1205 -- If the operand is not universal, and the operator is given by an
1206 -- expanded name, verify that the operand has an interpretation with a
1207 -- type defined in the given scope of the operator.
1209 function Type_In_P (Test : Kind_Test) return Entity_Id;
1210 -- Find a type of the given class in package Pack that contains the
1213 ---------------------------
1214 -- Operand_Type_In_Scope --
1215 ---------------------------
1217 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1218 Nod : constant Node_Id := Right_Opnd (Op_Node);
1223 if not Is_Overloaded (Nod) then
1224 return Scope (Base_Type (Etype (Nod))) = S;
1227 Get_First_Interp (Nod, I, It);
1228 while Present (It.Typ) loop
1229 if Scope (Base_Type (It.Typ)) = S then
1233 Get_Next_Interp (I, It);
1238 end Operand_Type_In_Scope;
1244 function Type_In_P (Test : Kind_Test) return Entity_Id is
1247 function In_Decl return Boolean;
1248 -- Verify that node is not part of the type declaration for the
1249 -- candidate type, which would otherwise be invisible.
1255 function In_Decl return Boolean is
1256 Decl_Node : constant Node_Id := Parent (E);
1262 if Etype (E) = Any_Type then
1265 elsif No (Decl_Node) then
1270 and then Nkind (N2) /= N_Compilation_Unit
1272 if N2 = Decl_Node then
1283 -- Start of processing for Type_In_P
1286 -- If the context type is declared in the prefix package, this is the
1287 -- desired base type.
1289 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1290 return Base_Type (Typ);
1293 E := First_Entity (Pack);
1294 while Present (E) loop
1296 and then not In_Decl
1308 -- Start of processing for Make_Call_Into_Operator
1311 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1316 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1317 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1318 Save_Interps (Act1, Left_Opnd (Op_Node));
1319 Save_Interps (Act2, Right_Opnd (Op_Node));
1320 Act1 := Left_Opnd (Op_Node);
1321 Act2 := Right_Opnd (Op_Node);
1326 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1327 Save_Interps (Act1, Right_Opnd (Op_Node));
1328 Act1 := Right_Opnd (Op_Node);
1331 -- If the operator is denoted by an expanded name, and the prefix is
1332 -- not Standard, but the operator is a predefined one whose scope is
1333 -- Standard, then this is an implicit_operator, inserted as an
1334 -- interpretation by the procedure of the same name. This procedure
1335 -- overestimates the presence of implicit operators, because it does
1336 -- not examine the type of the operands. Verify now that the operand
1337 -- type appears in the given scope. If right operand is universal,
1338 -- check the other operand. In the case of concatenation, either
1339 -- argument can be the component type, so check the type of the result.
1340 -- If both arguments are literals, look for a type of the right kind
1341 -- defined in the given scope. This elaborate nonsense is brought to
1342 -- you courtesy of b33302a. The type itself must be frozen, so we must
1343 -- find the type of the proper class in the given scope.
1345 -- A final wrinkle is the multiplication operator for fixed point types,
1346 -- which is defined in Standard only, and not in the scope of the
1347 -- fixed point type itself.
1349 if Nkind (Name (N)) = N_Expanded_Name then
1350 Pack := Entity (Prefix (Name (N)));
1352 -- If the entity being called is defined in the given package, it is
1353 -- a renaming of a predefined operator, and known to be legal.
1355 if Scope (Entity (Name (N))) = Pack
1356 and then Pack /= Standard_Standard
1360 -- Visibility does not need to be checked in an instance: if the
1361 -- operator was not visible in the generic it has been diagnosed
1362 -- already, else there is an implicit copy of it in the instance.
1364 elsif In_Instance then
1367 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1368 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1369 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1371 if Pack /= Standard_Standard then
1375 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1378 elsif Ada_Version >= Ada_2005
1379 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1380 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1385 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1387 if Op_Name = Name_Op_Concat then
1388 Opnd_Type := Base_Type (Typ);
1390 elsif (Scope (Opnd_Type) = Standard_Standard
1392 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1394 and then not Comes_From_Source (Opnd_Type))
1396 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1399 if Scope (Opnd_Type) = Standard_Standard then
1401 -- Verify that the scope contains a type that corresponds to
1402 -- the given literal. Optimize the case where Pack is Standard.
1404 if Pack /= Standard_Standard then
1406 if Opnd_Type = Universal_Integer then
1407 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1409 elsif Opnd_Type = Universal_Real then
1410 Orig_Type := Type_In_P (Is_Real_Type'Access);
1412 elsif Opnd_Type = Any_String then
1413 Orig_Type := Type_In_P (Is_String_Type'Access);
1415 elsif Opnd_Type = Any_Access then
1416 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1418 elsif Opnd_Type = Any_Composite then
1419 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1421 if Present (Orig_Type) then
1422 if Has_Private_Component (Orig_Type) then
1425 Set_Etype (Act1, Orig_Type);
1428 Set_Etype (Act2, Orig_Type);
1437 Error := No (Orig_Type);
1440 elsif Ekind (Opnd_Type) = E_Allocator_Type
1441 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1445 -- If the type is defined elsewhere, and the operator is not
1446 -- defined in the given scope (by a renaming declaration, e.g.)
1447 -- then this is an error as well. If an extension of System is
1448 -- present, and the type may be defined there, Pack must be
1451 elsif Scope (Opnd_Type) /= Pack
1452 and then Scope (Op_Id) /= Pack
1453 and then (No (System_Aux_Id)
1454 or else Scope (Opnd_Type) /= System_Aux_Id
1455 or else Pack /= Scope (System_Aux_Id))
1457 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1460 Error := not Operand_Type_In_Scope (Pack);
1463 elsif Pack = Standard_Standard
1464 and then not Operand_Type_In_Scope (Standard_Standard)
1471 Error_Msg_Node_2 := Pack;
1473 ("& not declared in&", N, Selector_Name (Name (N)));
1474 Set_Etype (N, Any_Type);
1477 -- Detect a mismatch between the context type and the result type
1478 -- in the named package, which is otherwise not detected if the
1479 -- operands are universal. Check is only needed if source entity is
1480 -- an operator, not a function that renames an operator.
1482 elsif Nkind (Parent (N)) /= N_Type_Conversion
1483 and then Ekind (Entity (Name (N))) = E_Operator
1484 and then Is_Numeric_Type (Typ)
1485 and then not Is_Universal_Numeric_Type (Typ)
1486 and then Scope (Base_Type (Typ)) /= Pack
1487 and then not In_Instance
1489 if Is_Fixed_Point_Type (Typ)
1490 and then (Op_Name = Name_Op_Multiply
1492 Op_Name = Name_Op_Divide)
1494 -- Already checked above
1498 -- Operator may be defined in an extension of System
1500 elsif Present (System_Aux_Id)
1501 and then Scope (Opnd_Type) = System_Aux_Id
1506 -- Could we use Wrong_Type here??? (this would require setting
1507 -- Etype (N) to the actual type found where Typ was expected).
1509 Error_Msg_NE ("expect }", N, Typ);
1514 Set_Chars (Op_Node, Op_Name);
1516 if not Is_Private_Type (Etype (N)) then
1517 Set_Etype (Op_Node, Base_Type (Etype (N)));
1519 Set_Etype (Op_Node, Etype (N));
1522 -- If this is a call to a function that renames a predefined equality,
1523 -- the renaming declaration provides a type that must be used to
1524 -- resolve the operands. This must be done now because resolution of
1525 -- the equality node will not resolve any remaining ambiguity, and it
1526 -- assumes that the first operand is not overloaded.
1528 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1529 and then Ekind (Func) = E_Function
1530 and then Is_Overloaded (Act1)
1532 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1533 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1536 Set_Entity (Op_Node, Op_Id);
1537 Generate_Reference (Op_Id, N, ' ');
1539 -- Do rewrite setting Comes_From_Source on the result if the original
1540 -- call came from source. Although it is not strictly the case that the
1541 -- operator as such comes from the source, logically it corresponds
1542 -- exactly to the function call in the source, so it should be marked
1543 -- this way (e.g. to make sure that validity checks work fine).
1546 CS : constant Boolean := Comes_From_Source (N);
1548 Rewrite (N, Op_Node);
1549 Set_Comes_From_Source (N, CS);
1552 -- If this is an arithmetic operator and the result type is private,
1553 -- the operands and the result must be wrapped in conversion to
1554 -- expose the underlying numeric type and expand the proper checks,
1555 -- e.g. on division.
1557 if Is_Private_Type (Typ) then
1559 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1560 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1561 Resolve_Intrinsic_Operator (N, Typ);
1563 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1564 Resolve_Intrinsic_Unary_Operator (N, Typ);
1572 end Make_Call_Into_Operator;
1578 function Operator_Kind
1580 Is_Binary : Boolean) return Node_Kind
1585 -- Use CASE statement or array???
1588 if Op_Name = Name_Op_And then
1590 elsif Op_Name = Name_Op_Or then
1592 elsif Op_Name = Name_Op_Xor then
1594 elsif Op_Name = Name_Op_Eq then
1596 elsif Op_Name = Name_Op_Ne then
1598 elsif Op_Name = Name_Op_Lt then
1600 elsif Op_Name = Name_Op_Le then
1602 elsif Op_Name = Name_Op_Gt then
1604 elsif Op_Name = Name_Op_Ge then
1606 elsif Op_Name = Name_Op_Add then
1608 elsif Op_Name = Name_Op_Subtract then
1609 Kind := N_Op_Subtract;
1610 elsif Op_Name = Name_Op_Concat then
1611 Kind := N_Op_Concat;
1612 elsif Op_Name = Name_Op_Multiply then
1613 Kind := N_Op_Multiply;
1614 elsif Op_Name = Name_Op_Divide then
1615 Kind := N_Op_Divide;
1616 elsif Op_Name = Name_Op_Mod then
1618 elsif Op_Name = Name_Op_Rem then
1620 elsif Op_Name = Name_Op_Expon then
1623 raise Program_Error;
1629 if Op_Name = Name_Op_Add then
1631 elsif Op_Name = Name_Op_Subtract then
1633 elsif Op_Name = Name_Op_Abs then
1635 elsif Op_Name = Name_Op_Not then
1638 raise Program_Error;
1645 ----------------------------
1646 -- Preanalyze_And_Resolve --
1647 ----------------------------
1649 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1650 Save_Full_Analysis : constant Boolean := Full_Analysis;
1653 Full_Analysis := False;
1654 Expander_Mode_Save_And_Set (False);
1656 -- We suppress all checks for this analysis, since the checks will
1657 -- be applied properly, and in the right location, when the default
1658 -- expression is reanalyzed and reexpanded later on.
1660 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1662 Expander_Mode_Restore;
1663 Full_Analysis := Save_Full_Analysis;
1664 end Preanalyze_And_Resolve;
1666 -- Version without context type
1668 procedure Preanalyze_And_Resolve (N : Node_Id) is
1669 Save_Full_Analysis : constant Boolean := Full_Analysis;
1672 Full_Analysis := False;
1673 Expander_Mode_Save_And_Set (False);
1676 Resolve (N, Etype (N), Suppress => All_Checks);
1678 Expander_Mode_Restore;
1679 Full_Analysis := Save_Full_Analysis;
1680 end Preanalyze_And_Resolve;
1682 ----------------------------------
1683 -- Replace_Actual_Discriminants --
1684 ----------------------------------
1686 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1687 Loc : constant Source_Ptr := Sloc (N);
1688 Tsk : Node_Id := Empty;
1690 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1696 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1700 if Nkind (Nod) = N_Identifier then
1701 Ent := Entity (Nod);
1704 and then Ekind (Ent) = E_Discriminant
1707 Make_Selected_Component (Loc,
1708 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1709 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1711 Set_Etype (Nod, Etype (Ent));
1719 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1721 -- Start of processing for Replace_Actual_Discriminants
1724 if not Expander_Active then
1728 if Nkind (Name (N)) = N_Selected_Component then
1729 Tsk := Prefix (Name (N));
1731 elsif Nkind (Name (N)) = N_Indexed_Component then
1732 Tsk := Prefix (Prefix (Name (N)));
1738 Replace_Discrs (Default);
1740 end Replace_Actual_Discriminants;
1746 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1747 Ambiguous : Boolean := False;
1748 Ctx_Type : Entity_Id := Typ;
1749 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1750 Err_Type : Entity_Id := Empty;
1751 Found : Boolean := False;
1754 I1 : Interp_Index := 0; -- prevent junk warning
1757 Seen : Entity_Id := Empty; -- prevent junk warning
1759 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1760 -- Determine whether a node comes from a predefined library unit or
1763 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1764 -- Try and fix up a literal so that it matches its expected type. New
1765 -- literals are manufactured if necessary to avoid cascaded errors.
1767 procedure Report_Ambiguous_Argument;
1768 -- Additional diagnostics when an ambiguous call has an ambiguous
1769 -- argument (typically a controlling actual).
1771 procedure Resolution_Failed;
1772 -- Called when attempt at resolving current expression fails
1774 ------------------------------------
1775 -- Comes_From_Predefined_Lib_Unit --
1776 -------------------------------------
1778 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1781 Sloc (Nod) = Standard_Location
1782 or else Is_Predefined_File_Name (Unit_File_Name (
1783 Get_Source_Unit (Sloc (Nod))));
1784 end Comes_From_Predefined_Lib_Unit;
1786 --------------------
1787 -- Patch_Up_Value --
1788 --------------------
1790 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1792 if Nkind (N) = N_Integer_Literal
1793 and then Is_Real_Type (Typ)
1796 Make_Real_Literal (Sloc (N),
1797 Realval => UR_From_Uint (Intval (N))));
1798 Set_Etype (N, Universal_Real);
1799 Set_Is_Static_Expression (N);
1801 elsif Nkind (N) = N_Real_Literal
1802 and then Is_Integer_Type (Typ)
1805 Make_Integer_Literal (Sloc (N),
1806 Intval => UR_To_Uint (Realval (N))));
1807 Set_Etype (N, Universal_Integer);
1808 Set_Is_Static_Expression (N);
1810 elsif Nkind (N) = N_String_Literal
1811 and then Is_Character_Type (Typ)
1813 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1815 Make_Character_Literal (Sloc (N),
1817 Char_Literal_Value =>
1818 UI_From_Int (Character'Pos ('A'))));
1819 Set_Etype (N, Any_Character);
1820 Set_Is_Static_Expression (N);
1822 elsif Nkind (N) /= N_String_Literal
1823 and then Is_String_Type (Typ)
1826 Make_String_Literal (Sloc (N),
1827 Strval => End_String));
1829 elsif Nkind (N) = N_Range then
1830 Patch_Up_Value (Low_Bound (N), Typ);
1831 Patch_Up_Value (High_Bound (N), Typ);
1835 -------------------------------
1836 -- Report_Ambiguous_Argument --
1837 -------------------------------
1839 procedure Report_Ambiguous_Argument is
1840 Arg : constant Node_Id := First (Parameter_Associations (N));
1845 if Nkind (Arg) = N_Function_Call
1846 and then Is_Entity_Name (Name (Arg))
1847 and then Is_Overloaded (Name (Arg))
1849 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1851 -- Could use comments on what is going on here ???
1853 Get_First_Interp (Name (Arg), I, It);
1854 while Present (It.Nam) loop
1855 Error_Msg_Sloc := Sloc (It.Nam);
1857 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1858 Error_Msg_N ("interpretation (inherited) #!", Arg);
1860 Error_Msg_N ("interpretation #!", Arg);
1863 Get_Next_Interp (I, It);
1866 end Report_Ambiguous_Argument;
1868 -----------------------
1869 -- Resolution_Failed --
1870 -----------------------
1872 procedure Resolution_Failed is
1874 Patch_Up_Value (N, Typ);
1876 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1877 Set_Is_Overloaded (N, False);
1879 -- The caller will return without calling the expander, so we need
1880 -- to set the analyzed flag. Note that it is fine to set Analyzed
1881 -- to True even if we are in the middle of a shallow analysis,
1882 -- (see the spec of sem for more details) since this is an error
1883 -- situation anyway, and there is no point in repeating the
1884 -- analysis later (indeed it won't work to repeat it later, since
1885 -- we haven't got a clear resolution of which entity is being
1888 Set_Analyzed (N, True);
1890 end Resolution_Failed;
1892 -- Start of processing for Resolve
1899 -- Access attribute on remote subprogram cannot be used for
1900 -- a non-remote access-to-subprogram type.
1902 if Nkind (N) = N_Attribute_Reference
1903 and then (Attribute_Name (N) = Name_Access
1904 or else Attribute_Name (N) = Name_Unrestricted_Access
1905 or else Attribute_Name (N) = Name_Unchecked_Access)
1906 and then Comes_From_Source (N)
1907 and then Is_Entity_Name (Prefix (N))
1908 and then Is_Subprogram (Entity (Prefix (N)))
1909 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1910 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1913 ("prefix must statically denote a non-remote subprogram", N);
1916 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1918 -- If the context is a Remote_Access_To_Subprogram, access attributes
1919 -- must be resolved with the corresponding fat pointer. There is no need
1920 -- to check for the attribute name since the return type of an
1921 -- attribute is never a remote type.
1923 if Nkind (N) = N_Attribute_Reference
1924 and then Comes_From_Source (N)
1925 and then (Is_Remote_Call_Interface (Typ)
1926 or else Is_Remote_Types (Typ))
1929 Attr : constant Attribute_Id :=
1930 Get_Attribute_Id (Attribute_Name (N));
1931 Pref : constant Node_Id := Prefix (N);
1934 Is_Remote : Boolean := True;
1937 -- Check that Typ is a remote access-to-subprogram type
1939 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1941 -- Prefix (N) must statically denote a remote subprogram
1942 -- declared in a package specification.
1944 if Attr = Attribute_Access then
1945 Decl := Unit_Declaration_Node (Entity (Pref));
1947 if Nkind (Decl) = N_Subprogram_Body then
1948 Spec := Corresponding_Spec (Decl);
1950 if not No (Spec) then
1951 Decl := Unit_Declaration_Node (Spec);
1955 Spec := Parent (Decl);
1957 if not Is_Entity_Name (Prefix (N))
1958 or else Nkind (Spec) /= N_Package_Specification
1960 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1964 ("prefix must statically denote a remote subprogram ",
1969 -- If we are generating code for a distributed program.
1970 -- perform semantic checks against the corresponding
1973 if (Attr = Attribute_Access
1974 or else Attr = Attribute_Unchecked_Access
1975 or else Attr = Attribute_Unrestricted_Access)
1976 and then Expander_Active
1977 and then Get_PCS_Name /= Name_No_DSA
1979 Check_Subtype_Conformant
1980 (New_Id => Entity (Prefix (N)),
1981 Old_Id => Designated_Type
1982 (Corresponding_Remote_Type (Typ)),
1986 Process_Remote_AST_Attribute (N, Typ);
1993 Debug_A_Entry ("resolving ", N);
1995 if Comes_From_Source (N) then
1996 if Is_Fixed_Point_Type (Typ) then
1997 Check_Restriction (No_Fixed_Point, N);
1999 elsif Is_Floating_Point_Type (Typ)
2000 and then Typ /= Universal_Real
2001 and then Typ /= Any_Real
2003 Check_Restriction (No_Floating_Point, N);
2007 -- Return if already analyzed
2009 if Analyzed (N) then
2010 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2013 -- Return if type = Any_Type (previous error encountered)
2015 elsif Etype (N) = Any_Type then
2016 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2020 Check_Parameterless_Call (N);
2022 -- If not overloaded, then we know the type, and all that needs doing
2023 -- is to check that this type is compatible with the context.
2025 if not Is_Overloaded (N) then
2026 Found := Covers (Typ, Etype (N));
2027 Expr_Type := Etype (N);
2029 -- In the overloaded case, we must select the interpretation that
2030 -- is compatible with the context (i.e. the type passed to Resolve)
2033 -- Loop through possible interpretations
2035 Get_First_Interp (N, I, It);
2036 Interp_Loop : while Present (It.Typ) loop
2038 -- We are only interested in interpretations that are compatible
2039 -- with the expected type, any other interpretations are ignored.
2041 if not Covers (Typ, It.Typ) then
2042 if Debug_Flag_V then
2043 Write_Str (" interpretation incompatible with context");
2048 -- Skip the current interpretation if it is disabled by an
2049 -- abstract operator. This action is performed only when the
2050 -- type against which we are resolving is the same as the
2051 -- type of the interpretation.
2053 if Ada_Version >= Ada_2005
2054 and then It.Typ = Typ
2055 and then Typ /= Universal_Integer
2056 and then Typ /= Universal_Real
2057 and then Present (It.Abstract_Op)
2062 -- First matching interpretation
2068 Expr_Type := It.Typ;
2070 -- Matching interpretation that is not the first, maybe an
2071 -- error, but there are some cases where preference rules are
2072 -- used to choose between the two possibilities. These and
2073 -- some more obscure cases are handled in Disambiguate.
2076 -- If the current statement is part of a predefined library
2077 -- unit, then all interpretations which come from user level
2078 -- packages should not be considered.
2081 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2086 Error_Msg_Sloc := Sloc (Seen);
2087 It1 := Disambiguate (N, I1, I, Typ);
2089 -- Disambiguation has succeeded. Skip the remaining
2092 if It1 /= No_Interp then
2094 Expr_Type := It1.Typ;
2096 while Present (It.Typ) loop
2097 Get_Next_Interp (I, It);
2101 -- Before we issue an ambiguity complaint, check for
2102 -- the case of a subprogram call where at least one
2103 -- of the arguments is Any_Type, and if so, suppress
2104 -- the message, since it is a cascaded error.
2106 if Nkind_In (N, N_Function_Call,
2107 N_Procedure_Call_Statement)
2114 A := First_Actual (N);
2115 while Present (A) loop
2118 if Nkind (E) = N_Parameter_Association then
2119 E := Explicit_Actual_Parameter (E);
2122 if Etype (E) = Any_Type then
2123 if Debug_Flag_V then
2124 Write_Str ("Any_Type in call");
2135 elsif Nkind (N) in N_Binary_Op
2136 and then (Etype (Left_Opnd (N)) = Any_Type
2137 or else Etype (Right_Opnd (N)) = Any_Type)
2141 elsif Nkind (N) in N_Unary_Op
2142 and then Etype (Right_Opnd (N)) = Any_Type
2147 -- Not that special case, so issue message using the
2148 -- flag Ambiguous to control printing of the header
2149 -- message only at the start of an ambiguous set.
2151 if not Ambiguous then
2152 if Nkind (N) = N_Function_Call
2153 and then Nkind (Name (N)) = N_Explicit_Dereference
2156 ("ambiguous expression "
2157 & "(cannot resolve indirect call)!", N);
2159 Error_Msg_NE -- CODEFIX
2160 ("ambiguous expression (cannot resolve&)!",
2166 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2168 ("\\possible interpretation (inherited)#!", N);
2170 Error_Msg_N -- CODEFIX
2171 ("\\possible interpretation#!", N);
2175 (N, N_Procedure_Call_Statement, N_Function_Call)
2176 and then Present (Parameter_Associations (N))
2178 Report_Ambiguous_Argument;
2182 Error_Msg_Sloc := Sloc (It.Nam);
2184 -- By default, the error message refers to the candidate
2185 -- interpretation. But if it is a predefined operator, it
2186 -- is implicitly declared at the declaration of the type
2187 -- of the operand. Recover the sloc of that declaration
2188 -- for the error message.
2190 if Nkind (N) in N_Op
2191 and then Scope (It.Nam) = Standard_Standard
2192 and then not Is_Overloaded (Right_Opnd (N))
2193 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2196 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2198 if Comes_From_Source (Err_Type)
2199 and then Present (Parent (Err_Type))
2201 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2204 elsif Nkind (N) in N_Binary_Op
2205 and then Scope (It.Nam) = Standard_Standard
2206 and then not Is_Overloaded (Left_Opnd (N))
2207 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2210 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2212 if Comes_From_Source (Err_Type)
2213 and then Present (Parent (Err_Type))
2215 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2218 -- If this is an indirect call, use the subprogram_type
2219 -- in the message, to have a meaningful location.
2220 -- Also indicate if this is an inherited operation,
2221 -- created by a type declaration.
2223 elsif Nkind (N) = N_Function_Call
2224 and then Nkind (Name (N)) = N_Explicit_Dereference
2225 and then Is_Type (It.Nam)
2229 Sloc (Associated_Node_For_Itype (Err_Type));
2234 if Nkind (N) in N_Op
2235 and then Scope (It.Nam) = Standard_Standard
2236 and then Present (Err_Type)
2238 -- Special-case the message for universal_fixed
2239 -- operators, which are not declared with the type
2240 -- of the operand, but appear forever in Standard.
2242 if It.Typ = Universal_Fixed
2243 and then Scope (It.Nam) = Standard_Standard
2246 ("\\possible interpretation as " &
2247 "universal_fixed operation " &
2248 "(RM 4.5.5 (19))", N);
2251 ("\\possible interpretation (predefined)#!", N);
2255 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2258 ("\\possible interpretation (inherited)#!", N);
2260 Error_Msg_N -- CODEFIX
2261 ("\\possible interpretation#!", N);
2267 -- We have a matching interpretation, Expr_Type is the type
2268 -- from this interpretation, and Seen is the entity.
2270 -- For an operator, just set the entity name. The type will be
2271 -- set by the specific operator resolution routine.
2273 if Nkind (N) in N_Op then
2274 Set_Entity (N, Seen);
2275 Generate_Reference (Seen, N);
2277 elsif Nkind (N) = N_Case_Expression then
2278 Set_Etype (N, Expr_Type);
2280 elsif Nkind (N) = N_Character_Literal then
2281 Set_Etype (N, Expr_Type);
2283 elsif Nkind (N) = N_Conditional_Expression then
2284 Set_Etype (N, Expr_Type);
2286 -- For an explicit dereference, attribute reference, range,
2287 -- short-circuit form (which is not an operator node), or call
2288 -- with a name that is an explicit dereference, there is
2289 -- nothing to be done at this point.
2291 elsif Nkind_In (N, N_Explicit_Dereference,
2292 N_Attribute_Reference,
2294 N_Indexed_Component,
2297 N_Selected_Component,
2299 or else Nkind (Name (N)) = N_Explicit_Dereference
2303 -- For procedure or function calls, set the type of the name,
2304 -- and also the entity pointer for the prefix.
2306 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2307 and then Is_Entity_Name (Name (N))
2309 Set_Etype (Name (N), Expr_Type);
2310 Set_Entity (Name (N), Seen);
2311 Generate_Reference (Seen, Name (N));
2313 elsif Nkind (N) = N_Function_Call
2314 and then Nkind (Name (N)) = N_Selected_Component
2316 Set_Etype (Name (N), Expr_Type);
2317 Set_Entity (Selector_Name (Name (N)), Seen);
2318 Generate_Reference (Seen, Selector_Name (Name (N)));
2320 -- For all other cases, just set the type of the Name
2323 Set_Etype (Name (N), Expr_Type);
2330 -- Move to next interpretation
2332 exit Interp_Loop when No (It.Typ);
2334 Get_Next_Interp (I, It);
2335 end loop Interp_Loop;
2338 -- At this stage Found indicates whether or not an acceptable
2339 -- interpretation exists. If not, then we have an error, except that if
2340 -- the context is Any_Type as a result of some other error, then we
2341 -- suppress the error report.
2344 if Typ /= Any_Type then
2346 -- If type we are looking for is Void, then this is the procedure
2347 -- call case, and the error is simply that what we gave is not a
2348 -- procedure name (we think of procedure calls as expressions with
2349 -- types internally, but the user doesn't think of them this way!)
2351 if Typ = Standard_Void_Type then
2353 -- Special case message if function used as a procedure
2355 if Nkind (N) = N_Procedure_Call_Statement
2356 and then Is_Entity_Name (Name (N))
2357 and then Ekind (Entity (Name (N))) = E_Function
2360 ("cannot use function & in a procedure call",
2361 Name (N), Entity (Name (N)));
2363 -- Otherwise give general message (not clear what cases this
2364 -- covers, but no harm in providing for them!)
2367 Error_Msg_N ("expect procedure name in procedure call", N);
2372 -- Otherwise we do have a subexpression with the wrong type
2374 -- Check for the case of an allocator which uses an access type
2375 -- instead of the designated type. This is a common error and we
2376 -- specialize the message, posting an error on the operand of the
2377 -- allocator, complaining that we expected the designated type of
2380 elsif Nkind (N) = N_Allocator
2381 and then Ekind (Typ) in Access_Kind
2382 and then Ekind (Etype (N)) in Access_Kind
2383 and then Designated_Type (Etype (N)) = Typ
2385 Wrong_Type (Expression (N), Designated_Type (Typ));
2388 -- Check for view mismatch on Null in instances, for which the
2389 -- view-swapping mechanism has no identifier.
2391 elsif (In_Instance or else In_Inlined_Body)
2392 and then (Nkind (N) = N_Null)
2393 and then Is_Private_Type (Typ)
2394 and then Is_Access_Type (Full_View (Typ))
2396 Resolve (N, Full_View (Typ));
2400 -- Check for an aggregate. Sometimes we can get bogus aggregates
2401 -- from misuse of parentheses, and we are about to complain about
2402 -- the aggregate without even looking inside it.
2404 -- Instead, if we have an aggregate of type Any_Composite, then
2405 -- analyze and resolve the component fields, and then only issue
2406 -- another message if we get no errors doing this (otherwise
2407 -- assume that the errors in the aggregate caused the problem).
2409 elsif Nkind (N) = N_Aggregate
2410 and then Etype (N) = Any_Composite
2412 -- Disable expansion in any case. If there is a type mismatch
2413 -- it may be fatal to try to expand the aggregate. The flag
2414 -- would otherwise be set to false when the error is posted.
2416 Expander_Active := False;
2419 procedure Check_Aggr (Aggr : Node_Id);
2420 -- Check one aggregate, and set Found to True if we have a
2421 -- definite error in any of its elements
2423 procedure Check_Elmt (Aelmt : Node_Id);
2424 -- Check one element of aggregate and set Found to True if
2425 -- we definitely have an error in the element.
2431 procedure Check_Aggr (Aggr : Node_Id) is
2435 if Present (Expressions (Aggr)) then
2436 Elmt := First (Expressions (Aggr));
2437 while Present (Elmt) loop
2443 if Present (Component_Associations (Aggr)) then
2444 Elmt := First (Component_Associations (Aggr));
2445 while Present (Elmt) loop
2447 -- If this is a default-initialized component, then
2448 -- there is nothing to check. The box will be
2449 -- replaced by the appropriate call during late
2452 if not Box_Present (Elmt) then
2453 Check_Elmt (Expression (Elmt));
2465 procedure Check_Elmt (Aelmt : Node_Id) is
2467 -- If we have a nested aggregate, go inside it (to
2468 -- attempt a naked analyze-resolve of the aggregate
2469 -- can cause undesirable cascaded errors). Do not
2470 -- resolve expression if it needs a type from context,
2471 -- as for integer * fixed expression.
2473 if Nkind (Aelmt) = N_Aggregate then
2479 if not Is_Overloaded (Aelmt)
2480 and then Etype (Aelmt) /= Any_Fixed
2485 if Etype (Aelmt) = Any_Type then
2496 -- If an error message was issued already, Found got reset
2497 -- to True, so if it is still False, issue the standard
2498 -- Wrong_Type message.
2501 if Is_Overloaded (N)
2502 and then Nkind (N) = N_Function_Call
2505 Subp_Name : Node_Id;
2507 if Is_Entity_Name (Name (N)) then
2508 Subp_Name := Name (N);
2510 elsif Nkind (Name (N)) = N_Selected_Component then
2512 -- Protected operation: retrieve operation name
2514 Subp_Name := Selector_Name (Name (N));
2516 raise Program_Error;
2519 Error_Msg_Node_2 := Typ;
2520 Error_Msg_NE ("no visible interpretation of&" &
2521 " matches expected type&", N, Subp_Name);
2524 if All_Errors_Mode then
2526 Index : Interp_Index;
2530 Error_Msg_N ("\\possible interpretations:", N);
2532 Get_First_Interp (Name (N), Index, It);
2533 while Present (It.Nam) loop
2534 Error_Msg_Sloc := Sloc (It.Nam);
2535 Error_Msg_Node_2 := It.Nam;
2537 ("\\ type& for & declared#", N, It.Typ);
2538 Get_Next_Interp (Index, It);
2543 Error_Msg_N ("\use -gnatf for details", N);
2546 Wrong_Type (N, Typ);
2554 -- Test if we have more than one interpretation for the context
2556 elsif Ambiguous then
2560 -- Here we have an acceptable interpretation for the context
2563 -- Propagate type information and normalize tree for various
2564 -- predefined operations. If the context only imposes a class of
2565 -- types, rather than a specific type, propagate the actual type
2568 if Typ = Any_Integer
2569 or else Typ = Any_Boolean
2570 or else Typ = Any_Modular
2571 or else Typ = Any_Real
2572 or else Typ = Any_Discrete
2574 Ctx_Type := Expr_Type;
2576 -- Any_Fixed is legal in a real context only if a specific
2577 -- fixed point type is imposed. If Norman Cohen can be
2578 -- confused by this, it deserves a separate message.
2581 and then Expr_Type = Any_Fixed
2583 Error_Msg_N ("illegal context for mixed mode operation", N);
2584 Set_Etype (N, Universal_Real);
2585 Ctx_Type := Universal_Real;
2589 -- A user-defined operator is transformed into a function call at
2590 -- this point, so that further processing knows that operators are
2591 -- really operators (i.e. are predefined operators). User-defined
2592 -- operators that are intrinsic are just renamings of the predefined
2593 -- ones, and need not be turned into calls either, but if they rename
2594 -- a different operator, we must transform the node accordingly.
2595 -- Instantiations of Unchecked_Conversion are intrinsic but are
2596 -- treated as functions, even if given an operator designator.
2598 if Nkind (N) in N_Op
2599 and then Present (Entity (N))
2600 and then Ekind (Entity (N)) /= E_Operator
2603 if not Is_Predefined_Op (Entity (N)) then
2604 Rewrite_Operator_As_Call (N, Entity (N));
2606 elsif Present (Alias (Entity (N)))
2608 Nkind (Parent (Parent (Entity (N)))) =
2609 N_Subprogram_Renaming_Declaration
2611 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2613 -- If the node is rewritten, it will be fully resolved in
2614 -- Rewrite_Renamed_Operator.
2616 if Analyzed (N) then
2622 case N_Subexpr'(Nkind (N)) is
2624 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2626 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2628 when N_Short_Circuit
2629 => Resolve_Short_Circuit (N, Ctx_Type);
2631 when N_Attribute_Reference
2632 => Resolve_Attribute (N, Ctx_Type);
2634 when N_Case_Expression
2635 => Resolve_Case_Expression (N, Ctx_Type);
2637 when N_Character_Literal
2638 => Resolve_Character_Literal (N, Ctx_Type);
2640 when N_Conditional_Expression
2641 => Resolve_Conditional_Expression (N, Ctx_Type);
2643 when N_Expanded_Name
2644 => Resolve_Entity_Name (N, Ctx_Type);
2646 when N_Explicit_Dereference
2647 => Resolve_Explicit_Dereference (N, Ctx_Type);
2649 when N_Expression_With_Actions
2650 => Resolve_Expression_With_Actions (N, Ctx_Type);
2652 when N_Extension_Aggregate
2653 => Resolve_Extension_Aggregate (N, Ctx_Type);
2655 when N_Function_Call
2656 => Resolve_Call (N, Ctx_Type);
2659 => Resolve_Entity_Name (N, Ctx_Type);
2661 when N_Indexed_Component
2662 => Resolve_Indexed_Component (N, Ctx_Type);
2664 when N_Integer_Literal
2665 => Resolve_Integer_Literal (N, Ctx_Type);
2667 when N_Membership_Test
2668 => Resolve_Membership_Op (N, Ctx_Type);
2670 when N_Null => Resolve_Null (N, Ctx_Type);
2672 when N_Op_And | N_Op_Or | N_Op_Xor
2673 => Resolve_Logical_Op (N, Ctx_Type);
2675 when N_Op_Eq | N_Op_Ne
2676 => Resolve_Equality_Op (N, Ctx_Type);
2678 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2679 => Resolve_Comparison_Op (N, Ctx_Type);
2681 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2683 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2684 N_Op_Divide | N_Op_Mod | N_Op_Rem
2686 => Resolve_Arithmetic_Op (N, Ctx_Type);
2688 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2690 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2692 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2693 => Resolve_Unary_Op (N, Ctx_Type);
2695 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2697 when N_Procedure_Call_Statement
2698 => Resolve_Call (N, Ctx_Type);
2700 when N_Operator_Symbol
2701 => Resolve_Operator_Symbol (N, Ctx_Type);
2703 when N_Qualified_Expression
2704 => Resolve_Qualified_Expression (N, Ctx_Type);
2706 when N_Quantified_Expression
2707 => Resolve_Quantified_Expression (N, Ctx_Type);
2709 when N_Raise_xxx_Error
2710 => Set_Etype (N, Ctx_Type);
2712 when N_Range => Resolve_Range (N, Ctx_Type);
2715 => Resolve_Real_Literal (N, Ctx_Type);
2717 when N_Reference => Resolve_Reference (N, Ctx_Type);
2719 when N_Selected_Component
2720 => Resolve_Selected_Component (N, Ctx_Type);
2722 when N_Slice => Resolve_Slice (N, Ctx_Type);
2724 when N_String_Literal
2725 => Resolve_String_Literal (N, Ctx_Type);
2727 when N_Subprogram_Info
2728 => Resolve_Subprogram_Info (N, Ctx_Type);
2730 when N_Type_Conversion
2731 => Resolve_Type_Conversion (N, Ctx_Type);
2733 when N_Unchecked_Expression =>
2734 Resolve_Unchecked_Expression (N, Ctx_Type);
2736 when N_Unchecked_Type_Conversion =>
2737 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2740 -- If the subexpression was replaced by a non-subexpression, then
2741 -- all we do is to expand it. The only legitimate case we know of
2742 -- is converting procedure call statement to entry call statements,
2743 -- but there may be others, so we are making this test general.
2745 if Nkind (N) not in N_Subexpr then
2746 Debug_A_Exit ("resolving ", N, " (done)");
2751 -- AI05-144-2: Check dangerous order dependence within an expression
2752 -- that is not a subexpression. Exclude RHS of an assignment, because
2753 -- both sides may have side-effects and the check must be performed
2754 -- over the statement.
2756 if Nkind (Parent (N)) not in N_Subexpr
2757 and then Nkind (Parent (N)) /= N_Assignment_Statement
2758 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2760 Check_Order_Dependence;
2763 -- The expression is definitely NOT overloaded at this point, so
2764 -- we reset the Is_Overloaded flag to avoid any confusion when
2765 -- reanalyzing the node.
2767 Set_Is_Overloaded (N, False);
2769 -- Freeze expression type, entity if it is a name, and designated
2770 -- type if it is an allocator (RM 13.14(10,11,13)).
2772 -- Now that the resolution of the type of the node is complete,
2773 -- and we did not detect an error, we can expand this node. We
2774 -- skip the expand call if we are in a default expression, see
2775 -- section "Handling of Default Expressions" in Sem spec.
2777 Debug_A_Exit ("resolving ", N, " (done)");
2779 -- We unconditionally freeze the expression, even if we are in
2780 -- default expression mode (the Freeze_Expression routine tests
2781 -- this flag and only freezes static types if it is set).
2783 Freeze_Expression (N);
2785 -- Now we can do the expansion
2795 -- Version with check(s) suppressed
2797 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2799 if Suppress = All_Checks then
2801 Svg : constant Suppress_Array := Scope_Suppress;
2803 Scope_Suppress := (others => True);
2805 Scope_Suppress := Svg;
2810 Svg : constant Boolean := Scope_Suppress (Suppress);
2812 Scope_Suppress (Suppress) := True;
2814 Scope_Suppress (Suppress) := Svg;
2823 -- Version with implicit type
2825 procedure Resolve (N : Node_Id) is
2827 Resolve (N, Etype (N));
2830 ---------------------
2831 -- Resolve_Actuals --
2832 ---------------------
2834 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2835 Loc : constant Source_Ptr := Sloc (N);
2840 Prev : Node_Id := Empty;
2843 procedure Check_Argument_Order;
2844 -- Performs a check for the case where the actuals are all simple
2845 -- identifiers that correspond to the formal names, but in the wrong
2846 -- order, which is considered suspicious and cause for a warning.
2848 procedure Check_Prefixed_Call;
2849 -- If the original node is an overloaded call in prefix notation,
2850 -- insert an 'Access or a dereference as needed over the first actual.
2851 -- Try_Object_Operation has already verified that there is a valid
2852 -- interpretation, but the form of the actual can only be determined
2853 -- once the primitive operation is identified.
2855 procedure Insert_Default;
2856 -- If the actual is missing in a call, insert in the actuals list
2857 -- an instance of the default expression. The insertion is always
2858 -- a named association.
2860 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2861 -- Check whether T1 and T2, or their full views, are derived from a
2862 -- common type. Used to enforce the restrictions on array conversions
2865 function Static_Concatenation (N : Node_Id) return Boolean;
2866 -- Predicate to determine whether an actual that is a concatenation
2867 -- will be evaluated statically and does not need a transient scope.
2868 -- This must be determined before the actual is resolved and expanded
2869 -- because if needed the transient scope must be introduced earlier.
2871 --------------------------
2872 -- Check_Argument_Order --
2873 --------------------------
2875 procedure Check_Argument_Order is
2877 -- Nothing to do if no parameters, or original node is neither a
2878 -- function call nor a procedure call statement (happens in the
2879 -- operator-transformed-to-function call case), or the call does
2880 -- not come from source, or this warning is off.
2882 if not Warn_On_Parameter_Order
2884 No (Parameter_Associations (N))
2886 not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2889 not Comes_From_Source (N)
2895 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2898 -- Nothing to do if only one parameter
2904 -- Here if at least two arguments
2907 Actuals : array (1 .. Nargs) of Node_Id;
2911 Wrong_Order : Boolean := False;
2912 -- Set True if an out of order case is found
2915 -- Collect identifier names of actuals, fail if any actual is
2916 -- not a simple identifier, and record max length of name.
2918 Actual := First (Parameter_Associations (N));
2919 for J in Actuals'Range loop
2920 if Nkind (Actual) /= N_Identifier then
2923 Actuals (J) := Actual;
2928 -- If we got this far, all actuals are identifiers and the list
2929 -- of their names is stored in the Actuals array.
2931 Formal := First_Formal (Nam);
2932 for J in Actuals'Range loop
2934 -- If we ran out of formals, that's odd, probably an error
2935 -- which will be detected elsewhere, but abandon the search.
2941 -- If name matches and is in order OK
2943 if Chars (Formal) = Chars (Actuals (J)) then
2947 -- If no match, see if it is elsewhere in list and if so
2948 -- flag potential wrong order if type is compatible.
2950 for K in Actuals'Range loop
2951 if Chars (Formal) = Chars (Actuals (K))
2953 Has_Compatible_Type (Actuals (K), Etype (Formal))
2955 Wrong_Order := True;
2965 <<Continue>> Next_Formal (Formal);
2968 -- If Formals left over, also probably an error, skip warning
2970 if Present (Formal) then
2974 -- Here we give the warning if something was out of order
2978 ("actuals for this call may be in wrong order?", N);
2982 end Check_Argument_Order;
2984 -------------------------
2985 -- Check_Prefixed_Call --
2986 -------------------------
2988 procedure Check_Prefixed_Call is
2989 Act : constant Node_Id := First_Actual (N);
2990 A_Type : constant Entity_Id := Etype (Act);
2991 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2992 Orig : constant Node_Id := Original_Node (N);
2996 -- Check whether the call is a prefixed call, with or without
2997 -- additional actuals.
2999 if Nkind (Orig) = N_Selected_Component
3001 (Nkind (Orig) = N_Indexed_Component
3002 and then Nkind (Prefix (Orig)) = N_Selected_Component
3003 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3004 and then Is_Entity_Name (Act)
3005 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3007 if Is_Access_Type (A_Type)
3008 and then not Is_Access_Type (F_Type)
3010 -- Introduce dereference on object in prefix
3013 Make_Explicit_Dereference (Sloc (Act),
3014 Prefix => Relocate_Node (Act));
3015 Rewrite (Act, New_A);
3018 elsif Is_Access_Type (F_Type)
3019 and then not Is_Access_Type (A_Type)
3021 -- Introduce an implicit 'Access in prefix
3023 if not Is_Aliased_View (Act) then
3025 ("object in prefixed call to& must be aliased"
3026 & " (RM-2005 4.3.1 (13))",
3031 Make_Attribute_Reference (Loc,
3032 Attribute_Name => Name_Access,
3033 Prefix => Relocate_Node (Act)));
3038 end Check_Prefixed_Call;
3040 --------------------
3041 -- Insert_Default --
3042 --------------------
3044 procedure Insert_Default is
3049 -- Missing argument in call, nothing to insert
3051 if No (Default_Value (F)) then
3055 -- Note that we do a full New_Copy_Tree, so that any associated
3056 -- Itypes are properly copied. This may not be needed any more,
3057 -- but it does no harm as a safety measure! Defaults of a generic
3058 -- formal may be out of bounds of the corresponding actual (see
3059 -- cc1311b) and an additional check may be required.
3064 New_Scope => Current_Scope,
3067 if Is_Concurrent_Type (Scope (Nam))
3068 and then Has_Discriminants (Scope (Nam))
3070 Replace_Actual_Discriminants (N, Actval);
3073 if Is_Overloadable (Nam)
3074 and then Present (Alias (Nam))
3076 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3077 and then not Is_Tagged_Type (Etype (F))
3079 -- If default is a real literal, do not introduce a
3080 -- conversion whose effect may depend on the run-time
3081 -- size of universal real.
3083 if Nkind (Actval) = N_Real_Literal then
3084 Set_Etype (Actval, Base_Type (Etype (F)));
3086 Actval := Unchecked_Convert_To (Etype (F), Actval);
3090 if Is_Scalar_Type (Etype (F)) then
3091 Enable_Range_Check (Actval);
3094 Set_Parent (Actval, N);
3096 -- Resolve aggregates with their base type, to avoid scope
3097 -- anomalies: the subtype was first built in the subprogram
3098 -- declaration, and the current call may be nested.
3100 if Nkind (Actval) = N_Aggregate then
3101 Analyze_And_Resolve (Actval, Etype (F));
3103 Analyze_And_Resolve (Actval, Etype (Actval));
3107 Set_Parent (Actval, N);
3109 -- See note above concerning aggregates
3111 if Nkind (Actval) = N_Aggregate
3112 and then Has_Discriminants (Etype (Actval))
3114 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3116 -- Resolve entities with their own type, which may differ
3117 -- from the type of a reference in a generic context (the
3118 -- view swapping mechanism did not anticipate the re-analysis
3119 -- of default values in calls).
3121 elsif Is_Entity_Name (Actval) then
3122 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3125 Analyze_And_Resolve (Actval, Etype (Actval));
3129 -- If default is a tag indeterminate function call, propagate
3130 -- tag to obtain proper dispatching.
3132 if Is_Controlling_Formal (F)
3133 and then Nkind (Default_Value (F)) = N_Function_Call
3135 Set_Is_Controlling_Actual (Actval);
3140 -- If the default expression raises constraint error, then just
3141 -- silently replace it with an N_Raise_Constraint_Error node,
3142 -- since we already gave the warning on the subprogram spec.
3143 -- If node is already a Raise_Constraint_Error leave as is, to
3144 -- prevent loops in the warnings removal machinery.
3146 if Raises_Constraint_Error (Actval)
3147 and then Nkind (Actval) /= N_Raise_Constraint_Error
3150 Make_Raise_Constraint_Error (Loc,
3151 Reason => CE_Range_Check_Failed));
3152 Set_Raises_Constraint_Error (Actval);
3153 Set_Etype (Actval, Etype (F));
3157 Make_Parameter_Association (Loc,
3158 Explicit_Actual_Parameter => Actval,
3159 Selector_Name => Make_Identifier (Loc, Chars (F)));
3161 -- Case of insertion is first named actual
3163 if No (Prev) or else
3164 Nkind (Parent (Prev)) /= N_Parameter_Association
3166 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3167 Set_First_Named_Actual (N, Actval);
3170 if No (Parameter_Associations (N)) then
3171 Set_Parameter_Associations (N, New_List (Assoc));
3173 Append (Assoc, Parameter_Associations (N));
3177 Insert_After (Prev, Assoc);
3180 -- Case of insertion is not first named actual
3183 Set_Next_Named_Actual
3184 (Assoc, Next_Named_Actual (Parent (Prev)));
3185 Set_Next_Named_Actual (Parent (Prev), Actval);
3186 Append (Assoc, Parameter_Associations (N));
3189 Mark_Rewrite_Insertion (Assoc);
3190 Mark_Rewrite_Insertion (Actval);
3199 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3200 FT1 : Entity_Id := T1;
3201 FT2 : Entity_Id := T2;
3204 if Is_Private_Type (T1)
3205 and then Present (Full_View (T1))
3207 FT1 := Full_View (T1);
3210 if Is_Private_Type (T2)
3211 and then Present (Full_View (T2))
3213 FT2 := Full_View (T2);
3216 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3219 --------------------------
3220 -- Static_Concatenation --
3221 --------------------------
3223 function Static_Concatenation (N : Node_Id) return Boolean is
3226 when N_String_Literal =>
3231 -- Concatenation is static when both operands are static
3232 -- and the concatenation operator is a predefined one.
3234 return Scope (Entity (N)) = Standard_Standard
3236 Static_Concatenation (Left_Opnd (N))
3238 Static_Concatenation (Right_Opnd (N));
3241 if Is_Entity_Name (N) then
3243 Ent : constant Entity_Id := Entity (N);
3245 return Ekind (Ent) = E_Constant
3246 and then Present (Constant_Value (Ent))
3248 Is_Static_Expression (Constant_Value (Ent));
3255 end Static_Concatenation;
3257 -- Start of processing for Resolve_Actuals
3260 Check_Argument_Order;
3262 if Present (First_Actual (N)) then
3263 Check_Prefixed_Call;
3266 A := First_Actual (N);
3267 F := First_Formal (Nam);
3268 while Present (F) loop
3269 if No (A) and then Needs_No_Actuals (Nam) then
3272 -- If we have an error in any actual or formal, indicated by a type
3273 -- of Any_Type, then abandon resolution attempt, and set result type
3276 elsif (Present (A) and then Etype (A) = Any_Type)
3277 or else Etype (F) = Any_Type
3279 Set_Etype (N, Any_Type);
3283 -- Case where actual is present
3285 -- If the actual is an entity, generate a reference to it now. We
3286 -- do this before the actual is resolved, because a formal of some
3287 -- protected subprogram, or a task discriminant, will be rewritten
3288 -- during expansion, and the reference to the source entity may
3292 and then Is_Entity_Name (A)
3293 and then Comes_From_Source (N)
3295 Orig_A := Entity (A);
3297 if Present (Orig_A) then
3298 if Is_Formal (Orig_A)
3299 and then Ekind (F) /= E_In_Parameter
3301 Generate_Reference (Orig_A, A, 'm');
3302 elsif not Is_Overloaded (A) then
3303 Generate_Reference (Orig_A, A);
3309 and then (Nkind (Parent (A)) /= N_Parameter_Association
3311 Chars (Selector_Name (Parent (A))) = Chars (F))
3313 -- If style checking mode on, check match of formal name
3316 if Nkind (Parent (A)) = N_Parameter_Association then
3317 Check_Identifier (Selector_Name (Parent (A)), F);
3321 -- If the formal is Out or In_Out, do not resolve and expand the
3322 -- conversion, because it is subsequently expanded into explicit
3323 -- temporaries and assignments. However, the object of the
3324 -- conversion can be resolved. An exception is the case of tagged
3325 -- type conversion with a class-wide actual. In that case we want
3326 -- the tag check to occur and no temporary will be needed (no
3327 -- representation change can occur) and the parameter is passed by
3328 -- reference, so we go ahead and resolve the type conversion.
3329 -- Another exception is the case of reference to component or
3330 -- subcomponent of a bit-packed array, in which case we want to
3331 -- defer expansion to the point the in and out assignments are
3334 if Ekind (F) /= E_In_Parameter
3335 and then Nkind (A) = N_Type_Conversion
3336 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3338 if Ekind (F) = E_In_Out_Parameter
3339 and then Is_Array_Type (Etype (F))
3341 -- In a view conversion, the conversion must be legal in
3342 -- both directions, and thus both component types must be
3343 -- aliased, or neither (4.6 (8)).
3345 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3346 -- the privacy requirement should not apply to generic
3347 -- types, and should be checked in an instance. ARG query
3350 if Has_Aliased_Components (Etype (Expression (A))) /=
3351 Has_Aliased_Components (Etype (F))
3354 ("both component types in a view conversion must be"
3355 & " aliased, or neither", A);
3357 -- Comment here??? what set of cases???
3360 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3362 -- Check view conv between unrelated by ref array types
3364 if Is_By_Reference_Type (Etype (F))
3365 or else Is_By_Reference_Type (Etype (Expression (A)))
3368 ("view conversion between unrelated by reference " &
3369 "array types not allowed (\'A'I-00246)", A);
3371 -- In Ada 2005 mode, check view conversion component
3372 -- type cannot be private, tagged, or volatile. Note
3373 -- that we only apply this to source conversions. The
3374 -- generated code can contain conversions which are
3375 -- not subject to this test, and we cannot extract the
3376 -- component type in such cases since it is not present.
3378 elsif Comes_From_Source (A)
3379 and then Ada_Version >= Ada_2005
3382 Comp_Type : constant Entity_Id :=
3384 (Etype (Expression (A)));
3386 if (Is_Private_Type (Comp_Type)
3387 and then not Is_Generic_Type (Comp_Type))
3388 or else Is_Tagged_Type (Comp_Type)
3389 or else Is_Volatile (Comp_Type)
3392 ("component type of a view conversion cannot"
3393 & " be private, tagged, or volatile"
3402 -- Resolve expression if conversion is all OK
3404 if (Conversion_OK (A)
3405 or else Valid_Conversion (A, Etype (A), Expression (A)))
3406 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3408 Resolve (Expression (A));
3411 -- If the actual is a function call that returns a limited
3412 -- unconstrained object that needs finalization, create a
3413 -- transient scope for it, so that it can receive the proper
3414 -- finalization list.
3416 elsif Nkind (A) = N_Function_Call
3417 and then Is_Limited_Record (Etype (F))
3418 and then not Is_Constrained (Etype (F))
3419 and then Expander_Active
3421 (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3423 Establish_Transient_Scope (A, False);
3425 -- A small optimization: if one of the actuals is a concatenation
3426 -- create a block around a procedure call to recover stack space.
3427 -- This alleviates stack usage when several procedure calls in
3428 -- the same statement list use concatenation. We do not perform
3429 -- this wrapping for code statements, where the argument is a
3430 -- static string, and we want to preserve warnings involving
3431 -- sequences of such statements.
3433 elsif Nkind (A) = N_Op_Concat
3434 and then Nkind (N) = N_Procedure_Call_Statement
3435 and then Expander_Active
3437 not (Is_Intrinsic_Subprogram (Nam)
3438 and then Chars (Nam) = Name_Asm)
3439 and then not Static_Concatenation (A)
3441 Establish_Transient_Scope (A, False);
3442 Resolve (A, Etype (F));
3445 if Nkind (A) = N_Type_Conversion
3446 and then Is_Array_Type (Etype (F))
3447 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3449 (Is_Limited_Type (Etype (F))
3450 or else Is_Limited_Type (Etype (Expression (A))))
3453 ("conversion between unrelated limited array types " &
3454 "not allowed (\A\I-00246)", A);
3456 if Is_Limited_Type (Etype (F)) then
3457 Explain_Limited_Type (Etype (F), A);
3460 if Is_Limited_Type (Etype (Expression (A))) then
3461 Explain_Limited_Type (Etype (Expression (A)), A);
3465 -- (Ada 2005: AI-251): If the actual is an allocator whose
3466 -- directly designated type is a class-wide interface, we build
3467 -- an anonymous access type to use it as the type of the
3468 -- allocator. Later, when the subprogram call is expanded, if
3469 -- the interface has a secondary dispatch table the expander
3470 -- will add a type conversion to force the correct displacement
3473 if Nkind (A) = N_Allocator then
3475 DDT : constant Entity_Id :=
3476 Directly_Designated_Type (Base_Type (Etype (F)));
3478 New_Itype : Entity_Id;
3481 if Is_Class_Wide_Type (DDT)
3482 and then Is_Interface (DDT)
3484 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3485 Set_Etype (New_Itype, Etype (A));
3486 Set_Directly_Designated_Type (New_Itype,
3487 Directly_Designated_Type (Etype (A)));
3488 Set_Etype (A, New_Itype);
3491 -- Ada 2005, AI-162:If the actual is an allocator, the
3492 -- innermost enclosing statement is the master of the
3493 -- created object. This needs to be done with expansion
3494 -- enabled only, otherwise the transient scope will not
3495 -- be removed in the expansion of the wrapped construct.
3497 if (Is_Controlled (DDT) or else Has_Task (DDT))
3498 and then Expander_Active
3500 Establish_Transient_Scope (A, False);
3505 -- (Ada 2005): The call may be to a primitive operation of
3506 -- a tagged synchronized type, declared outside of the type.
3507 -- In this case the controlling actual must be converted to
3508 -- its corresponding record type, which is the formal type.
3509 -- The actual may be a subtype, either because of a constraint
3510 -- or because it is a generic actual, so use base type to
3511 -- locate concurrent type.
3513 A_Typ := Base_Type (Etype (A));
3514 F_Typ := Base_Type (Etype (F));
3517 Full_A_Typ : Entity_Id;
3520 if Present (Full_View (A_Typ)) then
3521 Full_A_Typ := Base_Type (Full_View (A_Typ));
3523 Full_A_Typ := A_Typ;
3526 -- Tagged synchronized type (case 1): the actual is a
3529 if Is_Concurrent_Type (A_Typ)
3530 and then Corresponding_Record_Type (A_Typ) = F_Typ
3533 Unchecked_Convert_To
3534 (Corresponding_Record_Type (A_Typ), A));
3535 Resolve (A, Etype (F));
3537 -- Tagged synchronized type (case 2): the formal is a
3540 elsif Ekind (Full_A_Typ) = E_Record_Type
3542 (Corresponding_Concurrent_Type (Full_A_Typ))
3543 and then Is_Concurrent_Type (F_Typ)
3544 and then Present (Corresponding_Record_Type (F_Typ))
3545 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3547 Resolve (A, Corresponding_Record_Type (F_Typ));
3552 Resolve (A, Etype (F));
3560 -- In SPARK or ALFA, the only view conversions are those involving
3561 -- ancestor conversion of an extended type.
3563 if Formal_Verification_Mode
3564 and then Comes_From_Source (Original_Node (A))
3565 and then Nkind (A) = N_Type_Conversion
3566 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3569 Operand : constant Node_Id := Expression (A);
3570 Operand_Typ : constant Entity_Id := Etype (Operand);
3571 Target_Typ : constant Entity_Id := A_Typ;
3574 if not (Is_Tagged_Type (Target_Typ)
3575 and then not Is_Class_Wide_Type (Target_Typ)
3576 and then Is_Tagged_Type (Operand_Typ)
3577 and then not Is_Class_Wide_Type (Operand_Typ)
3578 and then Is_Ancestor (Target_Typ, Operand_Typ))
3580 Error_Msg_F ("|~~ancestor conversion is the only "
3581 & "permitted view conversion", A);
3586 -- Save actual for subsequent check on order dependence, and
3587 -- indicate whether actual is modifiable. For AI05-0144-2.
3589 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3591 -- For mode IN, if actual is an entity, and the type of the formal
3592 -- has warnings suppressed, then we reset Never_Set_In_Source for
3593 -- the calling entity. The reason for this is to catch cases like
3594 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3595 -- uses trickery to modify an IN parameter.
3597 if Ekind (F) = E_In_Parameter
3598 and then Is_Entity_Name (A)
3599 and then Present (Entity (A))
3600 and then Ekind (Entity (A)) = E_Variable
3601 and then Has_Warnings_Off (F_Typ)
3603 Set_Never_Set_In_Source (Entity (A), False);
3606 -- Perform error checks for IN and IN OUT parameters
3608 if Ekind (F) /= E_Out_Parameter then
3610 -- Check unset reference. For scalar parameters, it is clearly
3611 -- wrong to pass an uninitialized value as either an IN or
3612 -- IN-OUT parameter. For composites, it is also clearly an
3613 -- error to pass a completely uninitialized value as an IN
3614 -- parameter, but the case of IN OUT is trickier. We prefer
3615 -- not to give a warning here. For example, suppose there is
3616 -- a routine that sets some component of a record to False.
3617 -- It is perfectly reasonable to make this IN-OUT and allow
3618 -- either initialized or uninitialized records to be passed
3621 -- For partially initialized composite values, we also avoid
3622 -- warnings, since it is quite likely that we are passing a
3623 -- partially initialized value and only the initialized fields
3624 -- will in fact be read in the subprogram.
3626 if Is_Scalar_Type (A_Typ)
3627 or else (Ekind (F) = E_In_Parameter
3628 and then not Is_Partially_Initialized_Type (A_Typ))
3630 Check_Unset_Reference (A);
3633 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3634 -- actual to a nested call, since this is case of reading an
3635 -- out parameter, which is not allowed.
3637 if Ada_Version = Ada_83
3638 and then Is_Entity_Name (A)
3639 and then Ekind (Entity (A)) = E_Out_Parameter
3641 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3645 -- Case of OUT or IN OUT parameter
3647 if Ekind (F) /= E_In_Parameter then
3649 -- For an Out parameter, check for useless assignment. Note
3650 -- that we can't set Last_Assignment this early, because we may
3651 -- kill current values in Resolve_Call, and that call would
3652 -- clobber the Last_Assignment field.
3654 -- Note: call Warn_On_Useless_Assignment before doing the check
3655 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3656 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3657 -- reflects the last assignment, not this one!
3659 if Ekind (F) = E_Out_Parameter then
3660 if Warn_On_Modified_As_Out_Parameter (F)
3661 and then Is_Entity_Name (A)
3662 and then Present (Entity (A))
3663 and then Comes_From_Source (N)
3665 Warn_On_Useless_Assignment (Entity (A), A);
3669 -- Validate the form of the actual. Note that the call to
3670 -- Is_OK_Variable_For_Out_Formal generates the required
3671 -- reference in this case.
3673 if not Is_OK_Variable_For_Out_Formal (A) then
3674 Error_Msg_NE ("actual for& must be a variable", A, F);
3677 -- What's the following about???
3679 if Is_Entity_Name (A) then
3680 Kill_Checks (Entity (A));
3686 if Etype (A) = Any_Type then
3687 Set_Etype (N, Any_Type);
3691 -- Apply appropriate range checks for in, out, and in-out
3692 -- parameters. Out and in-out parameters also need a separate
3693 -- check, if there is a type conversion, to make sure the return
3694 -- value meets the constraints of the variable before the
3697 -- Gigi looks at the check flag and uses the appropriate types.
3698 -- For now since one flag is used there is an optimization which
3699 -- might not be done in the In Out case since Gigi does not do
3700 -- any analysis. More thought required about this ???
3702 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3704 -- Apply predicate checks, unless this is a call to the
3705 -- predicate check function itself, which would cause an
3706 -- infinite recursion.
3708 if not (Ekind (Nam) = E_Function
3709 and then Has_Predicates (Nam))
3711 Apply_Predicate_Check (A, F_Typ);
3714 -- Apply required constraint checks
3716 if Is_Scalar_Type (Etype (A)) then
3717 Apply_Scalar_Range_Check (A, F_Typ);
3719 elsif Is_Array_Type (Etype (A)) then
3720 Apply_Length_Check (A, F_Typ);
3722 elsif Is_Record_Type (F_Typ)
3723 and then Has_Discriminants (F_Typ)
3724 and then Is_Constrained (F_Typ)
3725 and then (not Is_Derived_Type (F_Typ)
3726 or else Comes_From_Source (Nam))
3728 Apply_Discriminant_Check (A, F_Typ);
3730 elsif Is_Access_Type (F_Typ)
3731 and then Is_Array_Type (Designated_Type (F_Typ))
3732 and then Is_Constrained (Designated_Type (F_Typ))
3734 Apply_Length_Check (A, F_Typ);
3736 elsif Is_Access_Type (F_Typ)
3737 and then Has_Discriminants (Designated_Type (F_Typ))
3738 and then Is_Constrained (Designated_Type (F_Typ))
3740 Apply_Discriminant_Check (A, F_Typ);
3743 Apply_Range_Check (A, F_Typ);
3746 -- Ada 2005 (AI-231): Note that the controlling parameter case
3747 -- already existed in Ada 95, which is partially checked
3748 -- elsewhere (see Checks), and we don't want the warning
3749 -- message to differ.
3751 if Is_Access_Type (F_Typ)
3752 and then Can_Never_Be_Null (F_Typ)
3753 and then Known_Null (A)
3755 if Is_Controlling_Formal (F) then
3756 Apply_Compile_Time_Constraint_Error
3758 Msg => "null value not allowed here?",
3759 Reason => CE_Access_Check_Failed);
3761 elsif Ada_Version >= Ada_2005 then
3762 Apply_Compile_Time_Constraint_Error
3764 Msg => "(Ada 2005) null not allowed in "
3765 & "null-excluding formal?",
3766 Reason => CE_Null_Not_Allowed);
3771 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3772 if Nkind (A) = N_Type_Conversion then
3773 if Is_Scalar_Type (A_Typ) then
3774 Apply_Scalar_Range_Check
3775 (Expression (A), Etype (Expression (A)), A_Typ);
3778 (Expression (A), Etype (Expression (A)), A_Typ);
3782 if Is_Scalar_Type (F_Typ) then
3783 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3785 elsif Is_Array_Type (F_Typ)
3786 and then Ekind (F) = E_Out_Parameter
3788 Apply_Length_Check (A, F_Typ);
3791 Apply_Range_Check (A, A_Typ, F_Typ);
3796 -- An actual associated with an access parameter is implicitly
3797 -- converted to the anonymous access type of the formal and must
3798 -- satisfy the legality checks for access conversions.
3800 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3801 if not Valid_Conversion (A, F_Typ, A) then
3803 ("invalid implicit conversion for access parameter", A);
3807 -- Check bad case of atomic/volatile argument (RM C.6(12))
3809 if Is_By_Reference_Type (Etype (F))
3810 and then Comes_From_Source (N)
3812 if Is_Atomic_Object (A)
3813 and then not Is_Atomic (Etype (F))
3816 ("cannot pass atomic argument to non-atomic formal",
3819 elsif Is_Volatile_Object (A)
3820 and then not Is_Volatile (Etype (F))
3823 ("cannot pass volatile argument to non-volatile formal",
3828 -- Check that subprograms don't have improper controlling
3829 -- arguments (RM 3.9.2 (9)).
3831 -- A primitive operation may have an access parameter of an
3832 -- incomplete tagged type, but a dispatching call is illegal
3833 -- if the type is still incomplete.
3835 if Is_Controlling_Formal (F) then
3836 Set_Is_Controlling_Actual (A);
3838 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3840 Desig : constant Entity_Id := Designated_Type (Etype (F));
3842 if Ekind (Desig) = E_Incomplete_Type
3843 and then No (Full_View (Desig))
3844 and then No (Non_Limited_View (Desig))
3847 ("premature use of incomplete type& " &
3848 "in dispatching call", A, Desig);
3853 elsif Nkind (A) = N_Explicit_Dereference then
3854 Validate_Remote_Access_To_Class_Wide_Type (A);
3857 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3858 and then not Is_Class_Wide_Type (F_Typ)
3859 and then not Is_Controlling_Formal (F)
3861 Error_Msg_N ("class-wide argument not allowed here!", A);
3863 if Is_Subprogram (Nam)
3864 and then Comes_From_Source (Nam)
3866 Error_Msg_Node_2 := F_Typ;
3868 ("& is not a dispatching operation of &!", A, Nam);
3871 elsif Is_Access_Type (A_Typ)
3872 and then Is_Access_Type (F_Typ)
3873 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3874 and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3875 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3876 or else (Nkind (A) = N_Attribute_Reference
3878 Is_Class_Wide_Type (Etype (Prefix (A)))))
3879 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3880 and then not Is_Controlling_Formal (F)
3882 -- Disable these checks for call to imported C++ subprograms
3885 (Is_Entity_Name (Name (N))
3886 and then Is_Imported (Entity (Name (N)))
3887 and then Convention (Entity (Name (N))) = Convention_CPP)
3890 ("access to class-wide argument not allowed here!", A);
3892 if Is_Subprogram (Nam)
3893 and then Comes_From_Source (Nam)
3895 Error_Msg_Node_2 := Designated_Type (F_Typ);
3897 ("& is not a dispatching operation of &!", A, Nam);
3903 -- If it is a named association, treat the selector_name as a
3904 -- proper identifier, and mark the corresponding entity.
3906 if Nkind (Parent (A)) = N_Parameter_Association then
3907 Set_Entity (Selector_Name (Parent (A)), F);
3908 Generate_Reference (F, Selector_Name (Parent (A)));
3909 Set_Etype (Selector_Name (Parent (A)), F_Typ);
3910 Generate_Reference (F_Typ, N, ' ');
3915 if Ekind (F) /= E_Out_Parameter then
3916 Check_Unset_Reference (A);
3921 -- Case where actual is not present
3929 end Resolve_Actuals;
3931 -----------------------
3932 -- Resolve_Allocator --
3933 -----------------------
3935 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
3936 E : constant Node_Id := Expression (N);
3938 Discrim : Entity_Id;
3941 Assoc : Node_Id := Empty;
3944 procedure Check_Allocator_Discrim_Accessibility
3945 (Disc_Exp : Node_Id;
3946 Alloc_Typ : Entity_Id);
3947 -- Check that accessibility level associated with an access discriminant
3948 -- initialized in an allocator by the expression Disc_Exp is not deeper
3949 -- than the level of the allocator type Alloc_Typ. An error message is
3950 -- issued if this condition is violated. Specialized checks are done for
3951 -- the cases of a constraint expression which is an access attribute or
3952 -- an access discriminant.
3954 function In_Dispatching_Context return Boolean;
3955 -- If the allocator is an actual in a call, it is allowed to be class-
3956 -- wide when the context is not because it is a controlling actual.
3958 procedure Propagate_Coextensions (Root : Node_Id);
3959 -- Propagate all nested coextensions which are located one nesting
3960 -- level down the tree to the node Root. Example:
3963 -- Level_1_Coextension
3964 -- Level_2_Coextension
3966 -- The algorithm is paired with delay actions done by the Expander. In
3967 -- the above example, assume all coextensions are controlled types.
3968 -- The cycle of analysis, resolution and expansion will yield:
3970 -- 1) Analyze Top_Record
3971 -- 2) Analyze Level_1_Coextension
3972 -- 3) Analyze Level_2_Coextension
3973 -- 4) Resolve Level_2_Coextension. The allocator is marked as a
3975 -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is
3976 -- generated to capture the allocated object. Temp_1 is attached
3977 -- to the coextension chain of Level_2_Coextension.
3978 -- 6) Resolve Level_1_Coextension. The allocator is marked as a
3979 -- coextension. A forward tree traversal is performed which finds
3980 -- Level_2_Coextension's list and copies its contents into its
3982 -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is
3983 -- generated to capture the allocated object. Temp_2 is attached
3984 -- to the coextension chain of Level_1_Coextension. Currently, the
3985 -- contents of the list are [Temp_2, Temp_1].
3986 -- 8) Resolve Top_Record. A forward tree traversal is performed which
3987 -- finds Level_1_Coextension's list and copies its contents into
3989 -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and
3990 -- Temp_2 and attach them to Top_Record's finalization list.
3992 -------------------------------------------
3993 -- Check_Allocator_Discrim_Accessibility --
3994 -------------------------------------------
3996 procedure Check_Allocator_Discrim_Accessibility
3997 (Disc_Exp : Node_Id;
3998 Alloc_Typ : Entity_Id)
4001 if Type_Access_Level (Etype (Disc_Exp)) >
4002 Type_Access_Level (Alloc_Typ)
4005 ("operand type has deeper level than allocator type", Disc_Exp);
4007 -- When the expression is an Access attribute the level of the prefix
4008 -- object must not be deeper than that of the allocator's type.
4010 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4011 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
4013 and then Object_Access_Level (Prefix (Disc_Exp))
4014 > Type_Access_Level (Alloc_Typ)
4017 ("prefix of attribute has deeper level than allocator type",
4020 -- When the expression is an access discriminant the check is against
4021 -- the level of the prefix object.
4023 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4024 and then Nkind (Disc_Exp) = N_Selected_Component
4025 and then Object_Access_Level (Prefix (Disc_Exp))
4026 > Type_Access_Level (Alloc_Typ)
4029 ("access discriminant has deeper level than allocator type",
4032 -- All other cases are legal
4037 end Check_Allocator_Discrim_Accessibility;
4039 ----------------------------
4040 -- In_Dispatching_Context --
4041 ----------------------------
4043 function In_Dispatching_Context return Boolean is
4044 Par : constant Node_Id := Parent (N);
4046 return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
4047 and then Is_Entity_Name (Name (Par))
4048 and then Is_Dispatching_Operation (Entity (Name (Par)));
4049 end In_Dispatching_Context;
4051 ----------------------------
4052 -- Propagate_Coextensions --
4053 ----------------------------
4055 procedure Propagate_Coextensions (Root : Node_Id) is
4057 procedure Copy_List (From : Elist_Id; To : Elist_Id);
4058 -- Copy the contents of list From into list To, preserving the
4059 -- order of elements.
4061 function Process_Allocator (Nod : Node_Id) return Traverse_Result;
4062 -- Recognize an allocator or a rewritten allocator node and add it
4063 -- along with its nested coextensions to the list of Root.
4069 procedure Copy_List (From : Elist_Id; To : Elist_Id) is
4070 From_Elmt : Elmt_Id;
4072 From_Elmt := First_Elmt (From);
4073 while Present (From_Elmt) loop
4074 Append_Elmt (Node (From_Elmt), To);
4075 Next_Elmt (From_Elmt);
4079 -----------------------
4080 -- Process_Allocator --
4081 -----------------------
4083 function Process_Allocator (Nod : Node_Id) return Traverse_Result is
4084 Orig_Nod : Node_Id := Nod;
4087 -- This is a possible rewritten subtype indication allocator. Any
4088 -- nested coextensions will appear as discriminant constraints.
4090 if Nkind (Nod) = N_Identifier
4091 and then Present (Original_Node (Nod))
4092 and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
4096 Discr_Elmt : Elmt_Id;
4099 if Is_Record_Type (Entity (Nod)) then
4101 First_Elmt (Discriminant_Constraint (Entity (Nod)));
4102 while Present (Discr_Elmt) loop
4103 Discr := Node (Discr_Elmt);
4105 if Nkind (Discr) = N_Identifier
4106 and then Present (Original_Node (Discr))
4107 and then Nkind (Original_Node (Discr)) = N_Allocator
4108 and then Present (Coextensions (
4109 Original_Node (Discr)))
4111 if No (Coextensions (Root)) then
4112 Set_Coextensions (Root, New_Elmt_List);
4116 (From => Coextensions (Original_Node (Discr)),
4117 To => Coextensions (Root));
4120 Next_Elmt (Discr_Elmt);
4123 -- There is no need to continue the traversal of this
4124 -- subtree since all the information has already been
4131 -- Case of either a stand alone allocator or a rewritten allocator
4132 -- with an aggregate.
4135 if Present (Original_Node (Nod)) then
4136 Orig_Nod := Original_Node (Nod);
4139 if Nkind (Orig_Nod) = N_Allocator then
4141 -- Propagate the list of nested coextensions to the Root
4142 -- allocator. This is done through list copy since a single
4143 -- allocator may have multiple coextensions. Do not touch
4144 -- coextensions roots.
4146 if not Is_Coextension_Root (Orig_Nod)
4147 and then Present (Coextensions (Orig_Nod))
4149 if No (Coextensions (Root)) then
4150 Set_Coextensions (Root, New_Elmt_List);
4154 (From => Coextensions (Orig_Nod),
4155 To => Coextensions (Root));
4158 -- There is no need to continue the traversal of this
4159 -- subtree since all the information has already been
4166 -- Keep on traversing, looking for the next allocator
4169 end Process_Allocator;
4171 procedure Process_Allocators is
4172 new Traverse_Proc (Process_Allocator);
4174 -- Start of processing for Propagate_Coextensions
4177 Process_Allocators (Expression (Root));
4178 end Propagate_Coextensions;
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 (Designated_Type (Typ))
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 (Designated_Type (Typ))
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) > Type_Access_Level (Typ) then
4357 if In_Instance_Body then
4358 Error_Msg_N ("?type in allocator has deeper level than" &
4359 " designated class-wide type", E);
4360 Error_Msg_N ("\?Program_Error will be raised at run time",
4363 Make_Raise_Program_Error (Sloc (N),
4364 Reason => PE_Accessibility_Check_Failed));
4367 -- Do not apply Ada 2005 accessibility checks on a class-wide
4368 -- allocator if the type given in the allocator is a formal
4369 -- type. A run-time check will be performed in the instance.
4371 elsif not Is_Generic_Type (Exp_Typ) then
4372 Error_Msg_N ("type in allocator has deeper level than" &
4373 " designated class-wide type", E);
4379 -- Check for allocation from an empty storage pool
4381 if No_Pool_Assigned (Typ) then
4382 Error_Msg_N ("allocation from empty storage pool!", N);
4384 -- If the context is an unchecked conversion, as may happen within
4385 -- an inlined subprogram, the allocator is being resolved with its
4386 -- own anonymous type. In that case, if the target type has a specific
4387 -- storage pool, it must be inherited explicitly by the allocator type.
4389 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4390 and then No (Associated_Storage_Pool (Typ))
4392 Set_Associated_Storage_Pool
4393 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4396 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4397 Check_Restriction (No_Anonymous_Allocators, N);
4400 -- An erroneous allocator may be rewritten as a raise Program_Error
4403 if Nkind (N) = N_Allocator then
4405 -- An anonymous access discriminant is the definition of a
4408 if Ekind (Typ) = E_Anonymous_Access_Type
4409 and then Nkind (Associated_Node_For_Itype (Typ)) =
4410 N_Discriminant_Specification
4412 -- Avoid marking an allocator as a dynamic coextension if it is
4413 -- within a static construct.
4415 if not Is_Static_Coextension (N) then
4416 Set_Is_Dynamic_Coextension (N);
4419 -- Cleanup for potential static coextensions
4422 Set_Is_Dynamic_Coextension (N, False);
4423 Set_Is_Static_Coextension (N, False);
4426 -- There is no need to propagate any nested coextensions if they
4427 -- are marked as static since they will be rewritten on the spot.
4429 if not Is_Static_Coextension (N) then
4430 Propagate_Coextensions (N);
4433 end Resolve_Allocator;
4435 ---------------------------
4436 -- Resolve_Arithmetic_Op --
4437 ---------------------------
4439 -- Used for resolving all arithmetic operators except exponentiation
4441 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4442 L : constant Node_Id := Left_Opnd (N);
4443 R : constant Node_Id := Right_Opnd (N);
4444 TL : constant Entity_Id := Base_Type (Etype (L));
4445 TR : constant Entity_Id := Base_Type (Etype (R));
4449 B_Typ : constant Entity_Id := Base_Type (Typ);
4450 -- We do the resolution using the base type, because intermediate values
4451 -- in expressions always are of the base type, not a subtype of it.
4453 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4454 -- Returns True if N is in a context that expects "any real type"
4456 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4457 -- Return True iff given type is Integer or universal real/integer
4459 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4460 -- Choose type of integer literal in fixed-point operation to conform
4461 -- to available fixed-point type. T is the type of the other operand,
4462 -- which is needed to determine the expected type of N.
4464 procedure Set_Operand_Type (N : Node_Id);
4465 -- Set operand type to T if universal
4467 -------------------------------
4468 -- Expected_Type_Is_Any_Real --
4469 -------------------------------
4471 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4473 -- N is the expression after "delta" in a fixed_point_definition;
4476 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4477 N_Decimal_Fixed_Point_Definition,
4479 -- N is one of the bounds in a real_range_specification;
4482 N_Real_Range_Specification,
4484 -- N is the expression of a delta_constraint;
4487 N_Delta_Constraint);
4488 end Expected_Type_Is_Any_Real;
4490 -----------------------------
4491 -- Is_Integer_Or_Universal --
4492 -----------------------------
4494 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4496 Index : Interp_Index;
4500 if not Is_Overloaded (N) then
4502 return Base_Type (T) = Base_Type (Standard_Integer)
4503 or else T = Universal_Integer
4504 or else T = Universal_Real;
4506 Get_First_Interp (N, Index, It);
4507 while Present (It.Typ) loop
4508 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4509 or else It.Typ = Universal_Integer
4510 or else It.Typ = Universal_Real
4515 Get_Next_Interp (Index, It);
4520 end Is_Integer_Or_Universal;
4522 ----------------------------
4523 -- Set_Mixed_Mode_Operand --
4524 ----------------------------
4526 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4527 Index : Interp_Index;
4531 if Universal_Interpretation (N) = Universal_Integer then
4533 -- A universal integer literal is resolved as standard integer
4534 -- except in the case of a fixed-point result, where we leave it
4535 -- as universal (to be handled by Exp_Fixd later on)
4537 if Is_Fixed_Point_Type (T) then
4538 Resolve (N, Universal_Integer);
4540 Resolve (N, Standard_Integer);
4543 elsif Universal_Interpretation (N) = Universal_Real
4544 and then (T = Base_Type (Standard_Integer)
4545 or else T = Universal_Integer
4546 or else T = Universal_Real)
4548 -- A universal real can appear in a fixed-type context. We resolve
4549 -- the literal with that context, even though this might raise an
4550 -- exception prematurely (the other operand may be zero).
4554 elsif Etype (N) = Base_Type (Standard_Integer)
4555 and then T = Universal_Real
4556 and then Is_Overloaded (N)
4558 -- Integer arg in mixed-mode operation. Resolve with universal
4559 -- type, in case preference rule must be applied.
4561 Resolve (N, Universal_Integer);
4564 and then B_Typ /= Universal_Fixed
4566 -- Not a mixed-mode operation, resolve with context
4570 elsif Etype (N) = Any_Fixed then
4572 -- N may itself be a mixed-mode operation, so use context type
4576 elsif Is_Fixed_Point_Type (T)
4577 and then B_Typ = Universal_Fixed
4578 and then Is_Overloaded (N)
4580 -- Must be (fixed * fixed) operation, operand must have one
4581 -- compatible interpretation.
4583 Resolve (N, Any_Fixed);
4585 elsif Is_Fixed_Point_Type (B_Typ)
4586 and then (T = Universal_Real
4587 or else Is_Fixed_Point_Type (T))
4588 and then Is_Overloaded (N)
4590 -- C * F(X) in a fixed context, where C is a real literal or a
4591 -- fixed-point expression. F must have either a fixed type
4592 -- interpretation or an integer interpretation, but not both.
4594 Get_First_Interp (N, Index, It);
4595 while Present (It.Typ) loop
4596 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4598 if Analyzed (N) then
4599 Error_Msg_N ("ambiguous operand in fixed operation", N);
4601 Resolve (N, Standard_Integer);
4604 elsif Is_Fixed_Point_Type (It.Typ) then
4606 if Analyzed (N) then
4607 Error_Msg_N ("ambiguous operand in fixed operation", N);
4609 Resolve (N, It.Typ);
4613 Get_Next_Interp (Index, It);
4616 -- Reanalyze the literal with the fixed type of the context. If
4617 -- context is Universal_Fixed, we are within a conversion, leave
4618 -- the literal as a universal real because there is no usable
4619 -- fixed type, and the target of the conversion plays no role in
4633 if B_Typ = Universal_Fixed
4634 and then Nkind (Op2) = N_Real_Literal
4636 T2 := Universal_Real;
4641 Set_Analyzed (Op2, False);
4648 end Set_Mixed_Mode_Operand;
4650 ----------------------
4651 -- Set_Operand_Type --
4652 ----------------------
4654 procedure Set_Operand_Type (N : Node_Id) is
4656 if Etype (N) = Universal_Integer
4657 or else Etype (N) = Universal_Real
4661 end Set_Operand_Type;
4663 -- Start of processing for Resolve_Arithmetic_Op
4666 if Comes_From_Source (N)
4667 and then Ekind (Entity (N)) = E_Function
4668 and then Is_Imported (Entity (N))
4669 and then Is_Intrinsic_Subprogram (Entity (N))
4671 Resolve_Intrinsic_Operator (N, Typ);
4674 -- Special-case for mixed-mode universal expressions or fixed point
4675 -- type operation: each argument is resolved separately. The same
4676 -- treatment is required if one of the operands of a fixed point
4677 -- operation is universal real, since in this case we don't do a
4678 -- conversion to a specific fixed-point type (instead the expander
4679 -- takes care of the case).
4681 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4682 and then Present (Universal_Interpretation (L))
4683 and then Present (Universal_Interpretation (R))
4685 Resolve (L, Universal_Interpretation (L));
4686 Resolve (R, Universal_Interpretation (R));
4687 Set_Etype (N, B_Typ);
4689 elsif (B_Typ = Universal_Real
4690 or else Etype (N) = Universal_Fixed
4691 or else (Etype (N) = Any_Fixed
4692 and then Is_Fixed_Point_Type (B_Typ))
4693 or else (Is_Fixed_Point_Type (B_Typ)
4694 and then (Is_Integer_Or_Universal (L)
4696 Is_Integer_Or_Universal (R))))
4697 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4699 if TL = Universal_Integer or else TR = Universal_Integer then
4700 Check_For_Visible_Operator (N, B_Typ);
4703 -- If context is a fixed type and one operand is integer, the
4704 -- other is resolved with the type of the context.
4706 if Is_Fixed_Point_Type (B_Typ)
4707 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4708 or else TL = Universal_Integer)
4713 elsif Is_Fixed_Point_Type (B_Typ)
4714 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4715 or else TR = Universal_Integer)
4721 Set_Mixed_Mode_Operand (L, TR);
4722 Set_Mixed_Mode_Operand (R, TL);
4725 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4726 -- multiplying operators from being used when the expected type is
4727 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4728 -- some cases where the expected type is actually Any_Real;
4729 -- Expected_Type_Is_Any_Real takes care of that case.
4731 if Etype (N) = Universal_Fixed
4732 or else Etype (N) = Any_Fixed
4734 if B_Typ = Universal_Fixed
4735 and then not Expected_Type_Is_Any_Real (N)
4736 and then not Nkind_In (Parent (N), N_Type_Conversion,
4737 N_Unchecked_Type_Conversion)
4739 Error_Msg_N ("type cannot be determined from context!", N);
4740 Error_Msg_N ("\explicit conversion to result type required", N);
4742 Set_Etype (L, Any_Type);
4743 Set_Etype (R, Any_Type);
4746 if Ada_Version = Ada_83
4747 and then Etype (N) = Universal_Fixed
4749 Nkind_In (Parent (N), N_Type_Conversion,
4750 N_Unchecked_Type_Conversion)
4753 ("(Ada 83) fixed-point operation "
4754 & "needs explicit conversion", N);
4757 -- The expected type is "any real type" in contexts like
4758 -- type T is delta <universal_fixed-expression> ...
4759 -- in which case we need to set the type to Universal_Real
4760 -- so that static expression evaluation will work properly.
4762 if Expected_Type_Is_Any_Real (N) then
4763 Set_Etype (N, Universal_Real);
4765 Set_Etype (N, B_Typ);
4769 elsif Is_Fixed_Point_Type (B_Typ)
4770 and then (Is_Integer_Or_Universal (L)
4771 or else Nkind (L) = N_Real_Literal
4772 or else Nkind (R) = N_Real_Literal
4773 or else Is_Integer_Or_Universal (R))
4775 Set_Etype (N, B_Typ);
4777 elsif Etype (N) = Any_Fixed then
4779 -- If no previous errors, this is only possible if one operand
4780 -- is overloaded and the context is universal. Resolve as such.
4782 Set_Etype (N, B_Typ);
4786 if (TL = Universal_Integer or else TL = Universal_Real)
4788 (TR = Universal_Integer or else TR = Universal_Real)
4790 Check_For_Visible_Operator (N, B_Typ);
4793 -- If the context is Universal_Fixed and the operands are also
4794 -- universal fixed, this is an error, unless there is only one
4795 -- applicable fixed_point type (usually Duration).
4797 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4798 T := Unique_Fixed_Point_Type (N);
4800 if T = Any_Type then
4813 -- If one of the arguments was resolved to a non-universal type.
4814 -- label the result of the operation itself with the same type.
4815 -- Do the same for the universal argument, if any.
4817 T := Intersect_Types (L, R);
4818 Set_Etype (N, Base_Type (T));
4819 Set_Operand_Type (L);
4820 Set_Operand_Type (R);
4823 Generate_Operator_Reference (N, Typ);
4824 Eval_Arithmetic_Op (N);
4826 -- In SPARK and ALFA, a multiplication or division with operands of
4827 -- fixed point types shall be qualified or explicitly converted to
4828 -- identify the result type.
4830 if Formal_Verification_Mode
4831 and then (Is_Fixed_Point_Type (Etype (L))
4832 or else Is_Fixed_Point_Type (Etype (R)))
4833 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4835 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4838 ("|~~operation should be qualified or explicitly converted", N);
4841 -- Set overflow and division checking bit. Much cleverer code needed
4842 -- here eventually and perhaps the Resolve routines should be separated
4843 -- for the various arithmetic operations, since they will need
4844 -- different processing. ???
4846 if Nkind (N) in N_Op then
4847 if not Overflow_Checks_Suppressed (Etype (N)) then
4848 Enable_Overflow_Check (N);
4851 -- Give warning if explicit division by zero
4853 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4854 and then not Division_Checks_Suppressed (Etype (N))
4856 Rop := Right_Opnd (N);
4858 if Compile_Time_Known_Value (Rop)
4859 and then ((Is_Integer_Type (Etype (Rop))
4860 and then Expr_Value (Rop) = Uint_0)
4862 (Is_Real_Type (Etype (Rop))
4863 and then Expr_Value_R (Rop) = Ureal_0))
4865 -- Specialize the warning message according to the operation
4869 Apply_Compile_Time_Constraint_Error
4870 (N, "division by zero?", CE_Divide_By_Zero,
4871 Loc => Sloc (Right_Opnd (N)));
4874 Apply_Compile_Time_Constraint_Error
4875 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4876 Loc => Sloc (Right_Opnd (N)));
4879 Apply_Compile_Time_Constraint_Error
4880 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4881 Loc => Sloc (Right_Opnd (N)));
4883 -- Division by zero can only happen with division, rem,
4884 -- and mod operations.
4887 raise Program_Error;
4890 -- Otherwise just set the flag to check at run time
4893 Activate_Division_Check (N);
4897 -- If Restriction No_Implicit_Conditionals is active, then it is
4898 -- violated if either operand can be negative for mod, or for rem
4899 -- if both operands can be negative.
4901 if Restriction_Check_Required (No_Implicit_Conditionals)
4902 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4911 -- Set if corresponding operand might be negative
4915 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4916 LNeg := (not OK) or else Lo < 0;
4919 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4920 RNeg := (not OK) or else Lo < 0;
4922 -- Check if we will be generating conditionals. There are two
4923 -- cases where that can happen, first for REM, the only case
4924 -- is largest negative integer mod -1, where the division can
4925 -- overflow, but we still have to give the right result. The
4926 -- front end generates a test for this annoying case. Here we
4927 -- just test if both operands can be negative (that's what the
4928 -- expander does, so we match its logic here).
4930 -- The second case is mod where either operand can be negative.
4931 -- In this case, the back end has to generate additional tests.
4933 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4935 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4937 Check_Restriction (No_Implicit_Conditionals, N);
4943 Check_Unset_Reference (L);
4944 Check_Unset_Reference (R);
4945 end Resolve_Arithmetic_Op;
4951 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4952 Loc : constant Source_Ptr := Sloc (N);
4953 Subp : constant Node_Id := Name (N);
4961 function Same_Or_Aliased_Subprograms
4963 E : Entity_Id) return Boolean;
4964 -- Returns True if the subprogram entity S is the same as E or else
4965 -- S is an alias of E.
4967 ---------------------------------
4968 -- Same_Or_Aliased_Subprograms --
4969 ---------------------------------
4971 function Same_Or_Aliased_Subprograms
4973 E : Entity_Id) return Boolean
4975 Subp_Alias : constant Entity_Id := Alias (S);
4978 or else (Present (Subp_Alias) and then Subp_Alias = E);
4979 end Same_Or_Aliased_Subprograms;
4981 -- Start of processing for Resolve_Call
4984 -- The context imposes a unique interpretation with type Typ on a
4985 -- procedure or function call. Find the entity of the subprogram that
4986 -- yields the expected type, and propagate the corresponding formal
4987 -- constraints on the actuals. The caller has established that an
4988 -- interpretation exists, and emitted an error if not unique.
4990 -- First deal with the case of a call to an access-to-subprogram,
4991 -- dereference made explicit in Analyze_Call.
4993 if Ekind (Etype (Subp)) = E_Subprogram_Type then
4994 if not Is_Overloaded (Subp) then
4995 Nam := Etype (Subp);
4998 -- Find the interpretation whose type (a subprogram type) has a
4999 -- return type that is compatible with the context. Analysis of
5000 -- the node has established that one exists.
5004 Get_First_Interp (Subp, I, It);
5005 while Present (It.Typ) loop
5006 if Covers (Typ, Etype (It.Typ)) then
5011 Get_Next_Interp (I, It);
5015 raise Program_Error;
5019 -- If the prefix is not an entity, then resolve it
5021 if not Is_Entity_Name (Subp) then
5022 Resolve (Subp, Nam);
5025 -- For an indirect call, we always invalidate checks, since we do not
5026 -- know whether the subprogram is local or global. Yes we could do
5027 -- better here, e.g. by knowing that there are no local subprograms,
5028 -- but it does not seem worth the effort. Similarly, we kill all
5029 -- knowledge of current constant values.
5031 Kill_Current_Values;
5033 -- If this is a procedure call which is really an entry call, do
5034 -- the conversion of the procedure call to an entry call. Protected
5035 -- operations use the same circuitry because the name in the call
5036 -- can be an arbitrary expression with special resolution rules.
5038 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5039 or else (Is_Entity_Name (Subp)
5040 and then Ekind (Entity (Subp)) = E_Entry)
5042 Resolve_Entry_Call (N, Typ);
5043 Check_Elab_Call (N);
5045 -- Kill checks and constant values, as above for indirect case
5046 -- Who knows what happens when another task is activated?
5048 Kill_Current_Values;
5051 -- Normal subprogram call with name established in Resolve
5053 elsif not (Is_Type (Entity (Subp))) then
5054 Nam := Entity (Subp);
5055 Set_Entity_With_Style_Check (Subp, Nam);
5057 -- Otherwise we must have the case of an overloaded call
5060 pragma Assert (Is_Overloaded (Subp));
5062 -- Initialize Nam to prevent warning (we know it will be assigned
5063 -- in the loop below, but the compiler does not know that).
5067 Get_First_Interp (Subp, I, It);
5068 while Present (It.Typ) loop
5069 if Covers (Typ, It.Typ) then
5071 Set_Entity_With_Style_Check (Subp, Nam);
5075 Get_Next_Interp (I, It);
5079 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5080 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5081 and then Nkind (Subp) /= N_Explicit_Dereference
5082 and then Present (Parameter_Associations (N))
5084 -- The prefix is a parameterless function call that returns an access
5085 -- to subprogram. If parameters are present in the current call, add
5086 -- add an explicit dereference. We use the base type here because
5087 -- within an instance these may be subtypes.
5089 -- The dereference is added either in Analyze_Call or here. Should
5090 -- be consolidated ???
5092 Set_Is_Overloaded (Subp, False);
5093 Set_Etype (Subp, Etype (Nam));
5094 Insert_Explicit_Dereference (Subp);
5095 Nam := Designated_Type (Etype (Nam));
5096 Resolve (Subp, Nam);
5099 -- Check that a call to Current_Task does not occur in an entry body
5101 if Is_RTE (Nam, RE_Current_Task) then
5110 -- Exclude calls that occur within the default of a formal
5111 -- parameter of the entry, since those are evaluated outside
5114 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5116 if Nkind (P) = N_Entry_Body
5117 or else (Nkind (P) = N_Subprogram_Body
5118 and then Is_Entry_Barrier_Function (P))
5122 ("?& should not be used in entry body (RM C.7(17))",
5125 ("\Program_Error will be raised at run time?", N, Nam);
5127 Make_Raise_Program_Error (Loc,
5128 Reason => PE_Current_Task_In_Entry_Body));
5129 Set_Etype (N, Rtype);
5136 -- Check that a procedure call does not occur in the context of the
5137 -- entry call statement of a conditional or timed entry call. Note that
5138 -- the case of a call to a subprogram renaming of an entry will also be
5139 -- rejected. The test for N not being an N_Entry_Call_Statement is
5140 -- defensive, covering the possibility that the processing of entry
5141 -- calls might reach this point due to later modifications of the code
5144 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5145 and then Nkind (N) /= N_Entry_Call_Statement
5146 and then Entry_Call_Statement (Parent (N)) = N
5148 if Ada_Version < Ada_2005 then
5149 Error_Msg_N ("entry call required in select statement", N);
5151 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5152 -- for a procedure_or_entry_call, the procedure_name or
5153 -- procedure_prefix of the procedure_call_statement shall denote
5154 -- an entry renamed by a procedure, or (a view of) a primitive
5155 -- subprogram of a limited interface whose first parameter is
5156 -- a controlling parameter.
5158 elsif Nkind (N) = N_Procedure_Call_Statement
5159 and then not Is_Renamed_Entry (Nam)
5160 and then not Is_Controlling_Limited_Procedure (Nam)
5163 ("entry call or dispatching primitive of interface required", N);
5167 -- Check that this is not a call to a protected procedure or entry from
5168 -- within a protected function.
5170 if Ekind (Current_Scope) = E_Function
5171 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5172 and then Ekind (Nam) /= E_Function
5173 and then Scope (Nam) = Scope (Current_Scope)
5175 Error_Msg_N ("within protected function, protected " &
5176 "object is constant", N);
5177 Error_Msg_N ("\cannot call operation that may modify it", N);
5180 -- Freeze the subprogram name if not in a spec-expression. Note that we
5181 -- freeze procedure calls as well as function calls. Procedure calls are
5182 -- not frozen according to the rules (RM 13.14(14)) because it is
5183 -- impossible to have a procedure call to a non-frozen procedure in pure
5184 -- Ada, but in the code that we generate in the expander, this rule
5185 -- needs extending because we can generate procedure calls that need
5188 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5189 Freeze_Expression (Subp);
5192 -- For a predefined operator, the type of the result is the type imposed
5193 -- by context, except for a predefined operation on universal fixed.
5194 -- Otherwise The type of the call is the type returned by the subprogram
5197 if Is_Predefined_Op (Nam) then
5198 if Etype (N) /= Universal_Fixed then
5202 -- If the subprogram returns an array type, and the context requires the
5203 -- component type of that array type, the node is really an indexing of
5204 -- the parameterless call. Resolve as such. A pathological case occurs
5205 -- when the type of the component is an access to the array type. In
5206 -- this case the call is truly ambiguous.
5208 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5210 ((Is_Array_Type (Etype (Nam))
5211 and then Covers (Typ, Component_Type (Etype (Nam))))
5212 or else (Is_Access_Type (Etype (Nam))
5213 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5216 Component_Type (Designated_Type (Etype (Nam))))))
5219 Index_Node : Node_Id;
5221 Ret_Type : constant Entity_Id := Etype (Nam);
5224 if Is_Access_Type (Ret_Type)
5225 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5228 ("cannot disambiguate function call and indexing", N);
5230 New_Subp := Relocate_Node (Subp);
5231 Set_Entity (Subp, Nam);
5233 if (Is_Array_Type (Ret_Type)
5234 and then Component_Type (Ret_Type) /= Any_Type)
5236 (Is_Access_Type (Ret_Type)
5238 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5240 if Needs_No_Actuals (Nam) then
5242 -- Indexed call to a parameterless function
5245 Make_Indexed_Component (Loc,
5247 Make_Function_Call (Loc,
5249 Expressions => Parameter_Associations (N));
5251 -- An Ada 2005 prefixed call to a primitive operation
5252 -- whose first parameter is the prefix. This prefix was
5253 -- prepended to the parameter list, which is actually a
5254 -- list of indexes. Remove the prefix in order to build
5255 -- the proper indexed component.
5258 Make_Indexed_Component (Loc,
5260 Make_Function_Call (Loc,
5262 Parameter_Associations =>
5264 (Remove_Head (Parameter_Associations (N)))),
5265 Expressions => Parameter_Associations (N));
5268 -- Preserve the parenthesis count of the node
5270 Set_Paren_Count (Index_Node, Paren_Count (N));
5272 -- Since we are correcting a node classification error made
5273 -- by the parser, we call Replace rather than Rewrite.
5275 Replace (N, Index_Node);
5277 Set_Etype (Prefix (N), Ret_Type);
5279 Resolve_Indexed_Component (N, Typ);
5280 Check_Elab_Call (Prefix (N));
5288 Set_Etype (N, Etype (Nam));
5291 -- In the case where the call is to an overloaded subprogram, Analyze
5292 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5293 -- such a case Normalize_Actuals needs to be called once more to order
5294 -- the actuals correctly. Otherwise the call will have the ordering
5295 -- given by the last overloaded subprogram whether this is the correct
5296 -- one being called or not.
5298 if Is_Overloaded (Subp) then
5299 Normalize_Actuals (N, Nam, False, Norm_OK);
5300 pragma Assert (Norm_OK);
5303 -- In any case, call is fully resolved now. Reset Overload flag, to
5304 -- prevent subsequent overload resolution if node is analyzed again
5306 Set_Is_Overloaded (Subp, False);
5307 Set_Is_Overloaded (N, False);
5309 -- If we are calling the current subprogram from immediately within its
5310 -- body, then that is the case where we can sometimes detect cases of
5311 -- infinite recursion statically. Do not try this in case restriction
5312 -- No_Recursion is in effect anyway, and do it only for source calls.
5314 if Comes_From_Source (N) then
5315 Scop := Current_Scope;
5317 -- Issue warning for possible infinite recursion in the absence
5318 -- of the No_Recursion restriction.
5320 if Same_Or_Aliased_Subprograms (Nam, Scop)
5321 and then not Restriction_Active (No_Recursion)
5322 and then Check_Infinite_Recursion (N)
5324 -- Here we detected and flagged an infinite recursion, so we do
5325 -- not need to test the case below for further warnings. Also we
5326 -- are all done if we now have a raise SE node.
5328 if Nkind (N) = N_Raise_Storage_Error then
5332 -- If call is to immediately containing subprogram, then check for
5333 -- the case of a possible run-time detectable infinite recursion.
5336 Scope_Loop : while Scop /= Standard_Standard loop
5337 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5339 -- Although in general case, recursion is not statically
5340 -- checkable, the case of calling an immediately containing
5341 -- subprogram is easy to catch.
5343 Check_Restriction (No_Recursion, N);
5345 -- If the recursive call is to a parameterless subprogram,
5346 -- then even if we can't statically detect infinite
5347 -- recursion, this is pretty suspicious, and we output a
5348 -- warning. Furthermore, we will try later to detect some
5349 -- cases here at run time by expanding checking code (see
5350 -- Detect_Infinite_Recursion in package Exp_Ch6).
5352 -- If the recursive call is within a handler, do not emit a
5353 -- warning, because this is a common idiom: loop until input
5354 -- is correct, catch illegal input in handler and restart.
5356 if No (First_Formal (Nam))
5357 and then Etype (Nam) = Standard_Void_Type
5358 and then not Error_Posted (N)
5359 and then Nkind (Parent (N)) /= N_Exception_Handler
5361 -- For the case of a procedure call. We give the message
5362 -- only if the call is the first statement in a sequence
5363 -- of statements, or if all previous statements are
5364 -- simple assignments. This is simply a heuristic to
5365 -- decrease false positives, without losing too many good
5366 -- warnings. The idea is that these previous statements
5367 -- may affect global variables the procedure depends on.
5369 if Nkind (N) = N_Procedure_Call_Statement
5370 and then Is_List_Member (N)
5376 while Present (P) loop
5377 if Nkind (P) /= N_Assignment_Statement then
5386 -- Do not give warning if we are in a conditional context
5389 K : constant Node_Kind := Nkind (Parent (N));
5391 if (K = N_Loop_Statement
5392 and then Present (Iteration_Scheme (Parent (N))))
5393 or else K = N_If_Statement
5394 or else K = N_Elsif_Part
5395 or else K = N_Case_Statement_Alternative
5401 -- Here warning is to be issued
5403 Set_Has_Recursive_Call (Nam);
5405 ("?possible infinite recursion!", N);
5407 ("\?Storage_Error may be raised at run time!", N);
5413 Scop := Scope (Scop);
5414 end loop Scope_Loop;
5418 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5420 Check_Obsolescent_2005_Entity (Nam, Subp);
5422 -- If subprogram name is a predefined operator, it was given in
5423 -- functional notation. Replace call node with operator node, so
5424 -- that actuals can be resolved appropriately.
5426 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5427 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5430 elsif Present (Alias (Nam))
5431 and then Is_Predefined_Op (Alias (Nam))
5433 Resolve_Actuals (N, Nam);
5434 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5438 -- Create a transient scope if the resulting type requires it
5440 -- There are several notable exceptions:
5442 -- a) In init procs, the transient scope overhead is not needed, and is
5443 -- even incorrect when the call is a nested initialization call for a
5444 -- component whose expansion may generate adjust calls. However, if the
5445 -- call is some other procedure call within an initialization procedure
5446 -- (for example a call to Create_Task in the init_proc of the task
5447 -- run-time record) a transient scope must be created around this call.
5449 -- b) Enumeration literal pseudo-calls need no transient scope
5451 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5452 -- functions) do not use the secondary stack even though the return
5453 -- type may be unconstrained.
5455 -- d) Calls to a build-in-place function, since such functions may
5456 -- allocate their result directly in a target object, and cases where
5457 -- the result does get allocated in the secondary stack are checked for
5458 -- within the specialized Exp_Ch6 procedures for expanding those
5459 -- build-in-place calls.
5461 -- e) If the subprogram is marked Inline_Always, then even if it returns
5462 -- an unconstrained type the call does not require use of the secondary
5463 -- stack. However, inlining will only take place if the body to inline
5464 -- is already present. It may not be available if e.g. the subprogram is
5465 -- declared in a child instance.
5467 -- If this is an initialization call for a type whose construction
5468 -- uses the secondary stack, and it is not a nested call to initialize
5469 -- a component, we do need to create a transient scope for it. We
5470 -- check for this by traversing the type in Check_Initialization_Call.
5473 and then Has_Pragma_Inline_Always (Nam)
5474 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5475 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5479 elsif Ekind (Nam) = E_Enumeration_Literal
5480 or else Is_Build_In_Place_Function (Nam)
5481 or else Is_Intrinsic_Subprogram (Nam)
5485 elsif Expander_Active
5486 and then Is_Type (Etype (Nam))
5487 and then Requires_Transient_Scope (Etype (Nam))
5489 (not Within_Init_Proc
5491 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5493 Establish_Transient_Scope (N, Sec_Stack => True);
5495 -- If the call appears within the bounds of a loop, it will
5496 -- be rewritten and reanalyzed, nothing left to do here.
5498 if Nkind (N) /= N_Function_Call then
5502 elsif Is_Init_Proc (Nam)
5503 and then not Within_Init_Proc
5505 Check_Initialization_Call (N, Nam);
5508 -- A protected function cannot be called within the definition of the
5509 -- enclosing protected type.
5511 if Is_Protected_Type (Scope (Nam))
5512 and then In_Open_Scopes (Scope (Nam))
5513 and then not Has_Completion (Scope (Nam))
5516 ("& cannot be called before end of protected definition", N, Nam);
5519 -- Propagate interpretation to actuals, and add default expressions
5522 if Present (First_Formal (Nam)) then
5523 Resolve_Actuals (N, Nam);
5525 -- Overloaded literals are rewritten as function calls, for purpose of
5526 -- resolution. After resolution, we can replace the call with the
5529 elsif Ekind (Nam) = E_Enumeration_Literal then
5530 Copy_Node (Subp, N);
5531 Resolve_Entity_Name (N, Typ);
5533 -- Avoid validation, since it is a static function call
5535 Generate_Reference (Nam, Subp);
5539 -- If the subprogram is not global, then kill all saved values and
5540 -- checks. This is a bit conservative, since in many cases we could do
5541 -- better, but it is not worth the effort. Similarly, we kill constant
5542 -- values. However we do not need to do this for internal entities
5543 -- (unless they are inherited user-defined subprograms), since they
5544 -- are not in the business of molesting local values.
5546 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5547 -- kill all checks and values for calls to global subprograms. This
5548 -- takes care of the case where an access to a local subprogram is
5549 -- taken, and could be passed directly or indirectly and then called
5550 -- from almost any context.
5552 -- Note: we do not do this step till after resolving the actuals. That
5553 -- way we still take advantage of the current value information while
5554 -- scanning the actuals.
5556 -- We suppress killing values if we are processing the nodes associated
5557 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5558 -- type kills all the values as part of analyzing the code that
5559 -- initializes the dispatch tables.
5561 if Inside_Freezing_Actions = 0
5562 and then (not Is_Library_Level_Entity (Nam)
5563 or else Suppress_Value_Tracking_On_Call
5564 (Nearest_Dynamic_Scope (Current_Scope)))
5565 and then (Comes_From_Source (Nam)
5566 or else (Present (Alias (Nam))
5567 and then Comes_From_Source (Alias (Nam))))
5569 Kill_Current_Values;
5572 -- If we are warning about unread OUT parameters, this is the place to
5573 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5574 -- after the above call to Kill_Current_Values (since that call clears
5575 -- the Last_Assignment field of all local variables).
5577 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5578 and then Comes_From_Source (N)
5579 and then In_Extended_Main_Source_Unit (N)
5586 F := First_Formal (Nam);
5587 A := First_Actual (N);
5588 while Present (F) and then Present (A) loop
5589 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5590 and then Warn_On_Modified_As_Out_Parameter (F)
5591 and then Is_Entity_Name (A)
5592 and then Present (Entity (A))
5593 and then Comes_From_Source (N)
5594 and then Safe_To_Capture_Value (N, Entity (A))
5596 Set_Last_Assignment (Entity (A), A);
5605 -- If the subprogram is a primitive operation, check whether or not
5606 -- it is a correct dispatching call.
5608 if Is_Overloadable (Nam)
5609 and then Is_Dispatching_Operation (Nam)
5611 Check_Dispatching_Call (N);
5613 elsif Ekind (Nam) /= E_Subprogram_Type
5614 and then Is_Abstract_Subprogram (Nam)
5615 and then not In_Instance
5617 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5620 -- If this is a dispatching call, generate the appropriate reference,
5621 -- for better source navigation in GPS.
5623 if Is_Overloadable (Nam)
5624 and then Present (Controlling_Argument (N))
5626 Generate_Reference (Nam, Subp, 'R');
5628 -- Normal case, not a dispatching call. Generate a call reference.
5631 Generate_Reference (Nam, Subp, 's');
5634 if Is_Intrinsic_Subprogram (Nam) then
5635 Check_Intrinsic_Call (N);
5638 -- Check for violation of restriction No_Specific_Termination_Handlers
5639 -- and warn on a potentially blocking call to Abort_Task.
5641 if Is_RTE (Nam, RE_Set_Specific_Handler)
5643 Is_RTE (Nam, RE_Specific_Handler)
5645 Check_Restriction (No_Specific_Termination_Handlers, N);
5647 elsif Is_RTE (Nam, RE_Abort_Task) then
5648 Check_Potentially_Blocking_Operation (N);
5651 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5652 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5653 -- need to check the second argument to determine whether it is an
5654 -- absolute or relative timing event.
5656 if Is_RTE (Nam, RE_Set_Handler)
5657 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5659 Check_Restriction (No_Relative_Delay, N);
5662 -- Issue an error for a call to an eliminated subprogram. We skip this
5663 -- in a spec expression, e.g. a call in a default parameter value, since
5664 -- we are not really doing a call at this time. That's important because
5665 -- the spec expression may itself belong to an eliminated subprogram.
5667 if not In_Spec_Expression then
5668 Check_For_Eliminated_Subprogram (Subp, Nam);
5671 -- All done, evaluate call and deal with elaboration issues
5674 Check_Elab_Call (N);
5675 Warn_On_Overlapping_Actuals (Nam, N);
5678 -----------------------------
5679 -- Resolve_Case_Expression --
5680 -----------------------------
5682 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5686 Alt := First (Alternatives (N));
5687 while Present (Alt) loop
5688 Resolve (Expression (Alt), Typ);
5693 Eval_Case_Expression (N);
5694 end Resolve_Case_Expression;
5696 -------------------------------
5697 -- Resolve_Character_Literal --
5698 -------------------------------
5700 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5701 B_Typ : constant Entity_Id := Base_Type (Typ);
5705 -- Verify that the character does belong to the type of the context
5707 Set_Etype (N, B_Typ);
5708 Eval_Character_Literal (N);
5710 -- Wide_Wide_Character literals must always be defined, since the set
5711 -- of wide wide character literals is complete, i.e. if a character
5712 -- literal is accepted by the parser, then it is OK for wide wide
5713 -- character (out of range character literals are rejected).
5715 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5718 -- Always accept character literal for type Any_Character, which
5719 -- occurs in error situations and in comparisons of literals, both
5720 -- of which should accept all literals.
5722 elsif B_Typ = Any_Character then
5725 -- For Standard.Character or a type derived from it, check that
5726 -- the literal is in range
5728 elsif Root_Type (B_Typ) = Standard_Character then
5729 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5733 -- For Standard.Wide_Character or a type derived from it, check
5734 -- that the literal is in range
5736 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5737 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5741 -- For Standard.Wide_Wide_Character or a type derived from it, we
5742 -- know the literal is in range, since the parser checked!
5744 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5747 -- If the entity is already set, this has already been resolved in a
5748 -- generic context, or comes from expansion. Nothing else to do.
5750 elsif Present (Entity (N)) then
5753 -- Otherwise we have a user defined character type, and we can use the
5754 -- standard visibility mechanisms to locate the referenced entity.
5757 C := Current_Entity (N);
5758 while Present (C) loop
5759 if Etype (C) = B_Typ then
5760 Set_Entity_With_Style_Check (N, C);
5761 Generate_Reference (C, N);
5769 -- If we fall through, then the literal does not match any of the
5770 -- entries of the enumeration type. This isn't just a constraint
5771 -- error situation, it is an illegality (see RM 4.2).
5774 ("character not defined for }", N, First_Subtype (B_Typ));
5775 end Resolve_Character_Literal;
5777 ---------------------------
5778 -- Resolve_Comparison_Op --
5779 ---------------------------
5781 -- Context requires a boolean type, and plays no role in resolution.
5782 -- Processing identical to that for equality operators. The result
5783 -- type is the base type, which matters when pathological subtypes of
5784 -- booleans with limited ranges are used.
5786 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5787 L : constant Node_Id := Left_Opnd (N);
5788 R : constant Node_Id := Right_Opnd (N);
5792 -- If this is an intrinsic operation which is not predefined, use the
5793 -- types of its declared arguments to resolve the possibly overloaded
5794 -- operands. Otherwise the operands are unambiguous and specify the
5797 if Scope (Entity (N)) /= Standard_Standard then
5798 T := Etype (First_Entity (Entity (N)));
5801 T := Find_Unique_Type (L, R);
5803 if T = Any_Fixed then
5804 T := Unique_Fixed_Point_Type (L);
5808 Set_Etype (N, Base_Type (Typ));
5809 Generate_Reference (T, N, ' ');
5811 -- Skip remaining processing if already set to Any_Type
5813 if T = Any_Type then
5817 -- Deal with other error cases
5819 if T = Any_String or else
5820 T = Any_Composite or else
5823 if T = Any_Character then
5824 Ambiguous_Character (L);
5826 Error_Msg_N ("ambiguous operands for comparison", N);
5829 Set_Etype (N, Any_Type);
5833 -- Resolve the operands if types OK
5837 Check_Unset_Reference (L);
5838 Check_Unset_Reference (R);
5839 Generate_Operator_Reference (N, T);
5840 Check_Low_Bound_Tested (N);
5842 -- In SPARK or ALFA, ordering operators <, <=, >, >= are not defined
5843 -- for Boolean types or array types except String.
5845 if Formal_Verification_Mode
5846 and then Comes_From_Source (Original_Node (N))
5848 if Is_Boolean_Type (T) then
5849 Error_Msg_F ("|~~comparison is not defined on Boolean type", N);
5850 elsif Is_Array_Type (T)
5851 and then Base_Type (T) /= Standard_String
5854 ("|~~comparison is not defined on array types " &
5855 "other than String", N);
5859 -- Check comparison on unordered enumeration
5861 if Comes_From_Source (N)
5862 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
5864 Error_Msg_N ("comparison on unordered enumeration type?", N);
5867 -- Evaluate the relation (note we do this after the above check
5868 -- since this Eval call may change N to True/False.
5870 Eval_Relational_Op (N);
5871 end Resolve_Comparison_Op;
5873 ------------------------------------
5874 -- Resolve_Conditional_Expression --
5875 ------------------------------------
5877 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5878 Condition : constant Node_Id := First (Expressions (N));
5879 Then_Expr : constant Node_Id := Next (Condition);
5880 Else_Expr : Node_Id := Next (Then_Expr);
5883 Resolve (Condition, Any_Boolean);
5884 Resolve (Then_Expr, Typ);
5886 -- If ELSE expression present, just resolve using the determined type
5888 if Present (Else_Expr) then
5889 Resolve (Else_Expr, Typ);
5891 -- If no ELSE expression is present, root type must be Standard.Boolean
5892 -- and we provide a Standard.True result converted to the appropriate
5893 -- Boolean type (in case it is a derived boolean type).
5895 elsif Root_Type (Typ) = Standard_Boolean then
5897 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
5898 Analyze_And_Resolve (Else_Expr, Typ);
5899 Append_To (Expressions (N), Else_Expr);
5902 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
5903 Append_To (Expressions (N), Error);
5907 Eval_Conditional_Expression (N);
5908 end Resolve_Conditional_Expression;
5910 -----------------------------------------
5911 -- Resolve_Discrete_Subtype_Indication --
5912 -----------------------------------------
5914 procedure Resolve_Discrete_Subtype_Indication
5922 Analyze (Subtype_Mark (N));
5923 S := Entity (Subtype_Mark (N));
5925 if Nkind (Constraint (N)) /= N_Range_Constraint then
5926 Error_Msg_N ("expect range constraint for discrete type", N);
5927 Set_Etype (N, Any_Type);
5930 R := Range_Expression (Constraint (N));
5938 if Base_Type (S) /= Base_Type (Typ) then
5940 ("expect subtype of }", N, First_Subtype (Typ));
5942 -- Rewrite the constraint as a range of Typ
5943 -- to allow compilation to proceed further.
5946 Rewrite (Low_Bound (R),
5947 Make_Attribute_Reference (Sloc (Low_Bound (R)),
5948 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5949 Attribute_Name => Name_First));
5950 Rewrite (High_Bound (R),
5951 Make_Attribute_Reference (Sloc (High_Bound (R)),
5952 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5953 Attribute_Name => Name_First));
5957 Set_Etype (N, Etype (R));
5959 -- Additionally, we must check that the bounds are compatible
5960 -- with the given subtype, which might be different from the
5961 -- type of the context.
5963 Apply_Range_Check (R, S);
5965 -- ??? If the above check statically detects a Constraint_Error
5966 -- it replaces the offending bound(s) of the range R with a
5967 -- Constraint_Error node. When the itype which uses these bounds
5968 -- is frozen the resulting call to Duplicate_Subexpr generates
5969 -- a new temporary for the bounds.
5971 -- Unfortunately there are other itypes that are also made depend
5972 -- on these bounds, so when Duplicate_Subexpr is called they get
5973 -- a forward reference to the newly created temporaries and Gigi
5974 -- aborts on such forward references. This is probably sign of a
5975 -- more fundamental problem somewhere else in either the order of
5976 -- itype freezing or the way certain itypes are constructed.
5978 -- To get around this problem we call Remove_Side_Effects right
5979 -- away if either bounds of R are a Constraint_Error.
5982 L : constant Node_Id := Low_Bound (R);
5983 H : constant Node_Id := High_Bound (R);
5986 if Nkind (L) = N_Raise_Constraint_Error then
5987 Remove_Side_Effects (L);
5990 if Nkind (H) = N_Raise_Constraint_Error then
5991 Remove_Side_Effects (H);
5995 Check_Unset_Reference (Low_Bound (R));
5996 Check_Unset_Reference (High_Bound (R));
5999 end Resolve_Discrete_Subtype_Indication;
6001 -------------------------
6002 -- Resolve_Entity_Name --
6003 -------------------------
6005 -- Used to resolve identifiers and expanded names
6007 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6008 E : constant Entity_Id := Entity (N);
6011 -- If garbage from errors, set to Any_Type and return
6013 if No (E) and then Total_Errors_Detected /= 0 then
6014 Set_Etype (N, Any_Type);
6018 -- Replace named numbers by corresponding literals. Note that this is
6019 -- the one case where Resolve_Entity_Name must reset the Etype, since
6020 -- it is currently marked as universal.
6022 if Ekind (E) = E_Named_Integer then
6024 Eval_Named_Integer (N);
6026 elsif Ekind (E) = E_Named_Real then
6028 Eval_Named_Real (N);
6030 -- For enumeration literals, we need to make sure that a proper style
6031 -- check is done, since such literals are overloaded, and thus we did
6032 -- not do a style check during the first phase of analysis.
6034 elsif Ekind (E) = E_Enumeration_Literal then
6035 Set_Entity_With_Style_Check (N, E);
6036 Eval_Entity_Name (N);
6038 -- Case of subtype name appearing as an operand in expression
6040 elsif Is_Type (E) then
6042 -- Allow use of subtype if it is a concurrent type where we are
6043 -- currently inside the body. This will eventually be expanded into a
6044 -- call to Self (for tasks) or _object (for protected objects). Any
6045 -- other use of a subtype is invalid.
6047 if Is_Concurrent_Type (E)
6048 and then In_Open_Scopes (E)
6052 -- Any other use is an error
6056 ("invalid use of subtype mark in expression or call", N);
6059 -- Check discriminant use if entity is discriminant in current scope,
6060 -- i.e. discriminant of record or concurrent type currently being
6061 -- analyzed. Uses in corresponding body are unrestricted.
6063 elsif Ekind (E) = E_Discriminant
6064 and then Scope (E) = Current_Scope
6065 and then not Has_Completion (Current_Scope)
6067 Check_Discriminant_Use (N);
6069 -- A parameterless generic function cannot appear in a context that
6070 -- requires resolution.
6072 elsif Ekind (E) = E_Generic_Function then
6073 Error_Msg_N ("illegal use of generic function", N);
6075 elsif Ekind (E) = E_Out_Parameter
6076 and then Ada_Version = Ada_83
6077 and then (Nkind (Parent (N)) in N_Op
6078 or else (Nkind (Parent (N)) = N_Assignment_Statement
6079 and then N = Expression (Parent (N)))
6080 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6082 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6084 -- In all other cases, just do the possible static evaluation
6087 -- A deferred constant that appears in an expression must have a
6088 -- completion, unless it has been removed by in-place expansion of
6091 if Ekind (E) = E_Constant
6092 and then Comes_From_Source (E)
6093 and then No (Constant_Value (E))
6094 and then Is_Frozen (Etype (E))
6095 and then not In_Spec_Expression
6096 and then not Is_Imported (E)
6098 if No_Initialization (Parent (E))
6099 or else (Present (Full_View (E))
6100 and then No_Initialization (Parent (Full_View (E))))
6105 "deferred constant is frozen before completion", N);
6109 Eval_Entity_Name (N);
6111 end Resolve_Entity_Name;
6117 procedure Resolve_Entry (Entry_Name : Node_Id) is
6118 Loc : constant Source_Ptr := Sloc (Entry_Name);
6126 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6127 -- If the bounds of the entry family being called depend on task
6128 -- discriminants, build a new index subtype where a discriminant is
6129 -- replaced with the value of the discriminant of the target task.
6130 -- The target task is the prefix of the entry name in the call.
6132 -----------------------
6133 -- Actual_Index_Type --
6134 -----------------------
6136 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6137 Typ : constant Entity_Id := Entry_Index_Type (E);
6138 Tsk : constant Entity_Id := Scope (E);
6139 Lo : constant Node_Id := Type_Low_Bound (Typ);
6140 Hi : constant Node_Id := Type_High_Bound (Typ);
6143 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6144 -- If the bound is given by a discriminant, replace with a reference
6145 -- to the discriminant of the same name in the target task. If the
6146 -- entry name is the target of a requeue statement and the entry is
6147 -- in the current protected object, the bound to be used is the
6148 -- discriminal of the object (see Apply_Range_Checks for details of
6149 -- the transformation).
6151 -----------------------------
6152 -- Actual_Discriminant_Ref --
6153 -----------------------------
6155 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6156 Typ : constant Entity_Id := Etype (Bound);
6160 Remove_Side_Effects (Bound);
6162 if not Is_Entity_Name (Bound)
6163 or else Ekind (Entity (Bound)) /= E_Discriminant
6167 elsif Is_Protected_Type (Tsk)
6168 and then In_Open_Scopes (Tsk)
6169 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6171 -- Note: here Bound denotes a discriminant of the corresponding
6172 -- record type tskV, whose discriminal is a formal of the
6173 -- init-proc tskVIP. What we want is the body discriminal,
6174 -- which is associated to the discriminant of the original
6175 -- concurrent type tsk.
6177 return New_Occurrence_Of
6178 (Find_Body_Discriminal (Entity (Bound)), Loc);
6182 Make_Selected_Component (Loc,
6183 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6184 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6189 end Actual_Discriminant_Ref;
6191 -- Start of processing for Actual_Index_Type
6194 if not Has_Discriminants (Tsk)
6195 or else (not Is_Entity_Name (Lo)
6197 not Is_Entity_Name (Hi))
6199 return Entry_Index_Type (E);
6202 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6203 Set_Etype (New_T, Base_Type (Typ));
6204 Set_Size_Info (New_T, Typ);
6205 Set_RM_Size (New_T, RM_Size (Typ));
6206 Set_Scalar_Range (New_T,
6207 Make_Range (Sloc (Entry_Name),
6208 Low_Bound => Actual_Discriminant_Ref (Lo),
6209 High_Bound => Actual_Discriminant_Ref (Hi)));
6213 end Actual_Index_Type;
6215 -- Start of processing of Resolve_Entry
6218 -- Find name of entry being called, and resolve prefix of name
6219 -- with its own type. The prefix can be overloaded, and the name
6220 -- and signature of the entry must be taken into account.
6222 if Nkind (Entry_Name) = N_Indexed_Component then
6224 -- Case of dealing with entry family within the current tasks
6226 E_Name := Prefix (Entry_Name);
6229 E_Name := Entry_Name;
6232 if Is_Entity_Name (E_Name) then
6234 -- Entry call to an entry (or entry family) in the current task. This
6235 -- is legal even though the task will deadlock. Rewrite as call to
6238 -- This can also be a call to an entry in an enclosing task. If this
6239 -- is a single task, we have to retrieve its name, because the scope
6240 -- of the entry is the task type, not the object. If the enclosing
6241 -- task is a task type, the identity of the task is given by its own
6244 -- Finally this can be a requeue on an entry of the same task or
6245 -- protected object.
6247 S := Scope (Entity (E_Name));
6249 for J in reverse 0 .. Scope_Stack.Last loop
6250 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6251 and then not Comes_From_Source (S)
6253 -- S is an enclosing task or protected object. The concurrent
6254 -- declaration has been converted into a type declaration, and
6255 -- the object itself has an object declaration that follows
6256 -- the type in the same declarative part.
6258 Tsk := Next_Entity (S);
6259 while Etype (Tsk) /= S loop
6266 elsif S = Scope_Stack.Table (J).Entity then
6268 -- Call to current task. Will be transformed into call to Self
6276 Make_Selected_Component (Loc,
6277 Prefix => New_Occurrence_Of (S, Loc),
6279 New_Occurrence_Of (Entity (E_Name), Loc));
6280 Rewrite (E_Name, New_N);
6283 elsif Nkind (Entry_Name) = N_Selected_Component
6284 and then Is_Overloaded (Prefix (Entry_Name))
6286 -- Use the entry name (which must be unique at this point) to find
6287 -- the prefix that returns the corresponding task type or protected
6291 Pref : constant Node_Id := Prefix (Entry_Name);
6292 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6297 Get_First_Interp (Pref, I, It);
6298 while Present (It.Typ) loop
6299 if Scope (Ent) = It.Typ then
6300 Set_Etype (Pref, It.Typ);
6304 Get_Next_Interp (I, It);
6309 if Nkind (Entry_Name) = N_Selected_Component then
6310 Resolve (Prefix (Entry_Name));
6312 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6313 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6314 Resolve (Prefix (Prefix (Entry_Name)));
6315 Index := First (Expressions (Entry_Name));
6316 Resolve (Index, Entry_Index_Type (Nam));
6318 -- Up to this point the expression could have been the actual in a
6319 -- simple entry call, and be given by a named association.
6321 if Nkind (Index) = N_Parameter_Association then
6322 Error_Msg_N ("expect expression for entry index", Index);
6324 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6329 ------------------------
6330 -- Resolve_Entry_Call --
6331 ------------------------
6333 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6334 Entry_Name : constant Node_Id := Name (N);
6335 Loc : constant Source_Ptr := Sloc (Entry_Name);
6337 First_Named : Node_Id;
6344 -- We kill all checks here, because it does not seem worth the effort to
6345 -- do anything better, an entry call is a big operation.
6349 -- Processing of the name is similar for entry calls and protected
6350 -- operation calls. Once the entity is determined, we can complete
6351 -- the resolution of the actuals.
6353 -- The selector may be overloaded, in the case of a protected object
6354 -- with overloaded functions. The type of the context is used for
6357 if Nkind (Entry_Name) = N_Selected_Component
6358 and then Is_Overloaded (Selector_Name (Entry_Name))
6359 and then Typ /= Standard_Void_Type
6366 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6367 while Present (It.Typ) loop
6368 if Covers (Typ, It.Typ) then
6369 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6370 Set_Etype (Entry_Name, It.Typ);
6372 Generate_Reference (It.Typ, N, ' ');
6375 Get_Next_Interp (I, It);
6380 Resolve_Entry (Entry_Name);
6382 if Nkind (Entry_Name) = N_Selected_Component then
6384 -- Simple entry call
6386 Nam := Entity (Selector_Name (Entry_Name));
6387 Obj := Prefix (Entry_Name);
6388 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6390 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6392 -- Call to member of entry family
6394 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6395 Obj := Prefix (Prefix (Entry_Name));
6396 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6399 -- We cannot in general check the maximum depth of protected entry
6400 -- calls at compile time. But we can tell that any protected entry
6401 -- call at all violates a specified nesting depth of zero.
6403 if Is_Protected_Type (Scope (Nam)) then
6404 Check_Restriction (Max_Entry_Queue_Length, N);
6407 -- Use context type to disambiguate a protected function that can be
6408 -- called without actuals and that returns an array type, and where
6409 -- the argument list may be an indexing of the returned value.
6411 if Ekind (Nam) = E_Function
6412 and then Needs_No_Actuals (Nam)
6413 and then Present (Parameter_Associations (N))
6415 ((Is_Array_Type (Etype (Nam))
6416 and then Covers (Typ, Component_Type (Etype (Nam))))
6418 or else (Is_Access_Type (Etype (Nam))
6419 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6420 and then Covers (Typ,
6421 Component_Type (Designated_Type (Etype (Nam))))))
6424 Index_Node : Node_Id;
6428 Make_Indexed_Component (Loc,
6430 Make_Function_Call (Loc,
6431 Name => Relocate_Node (Entry_Name)),
6432 Expressions => Parameter_Associations (N));
6434 -- Since we are correcting a node classification error made by
6435 -- the parser, we call Replace rather than Rewrite.
6437 Replace (N, Index_Node);
6438 Set_Etype (Prefix (N), Etype (Nam));
6440 Resolve_Indexed_Component (N, Typ);
6445 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6446 and then Present (PPC_Wrapper (Nam))
6447 and then Current_Scope /= PPC_Wrapper (Nam)
6449 -- Rewrite as call to the precondition wrapper, adding the task
6450 -- object to the list of actuals. If the call is to a member of
6451 -- an entry family, include the index as well.
6455 New_Actuals : List_Id;
6457 New_Actuals := New_List (Obj);
6459 if Nkind (Entry_Name) = N_Indexed_Component then
6460 Append_To (New_Actuals,
6461 New_Copy_Tree (First (Expressions (Entry_Name))));
6464 Append_List (Parameter_Associations (N), New_Actuals);
6466 Make_Procedure_Call_Statement (Loc,
6468 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6469 Parameter_Associations => New_Actuals);
6470 Rewrite (N, New_Call);
6471 Analyze_And_Resolve (N);
6476 -- The operation name may have been overloaded. Order the actuals
6477 -- according to the formals of the resolved entity, and set the
6478 -- return type to that of the operation.
6481 Normalize_Actuals (N, Nam, False, Norm_OK);
6482 pragma Assert (Norm_OK);
6483 Set_Etype (N, Etype (Nam));
6486 Resolve_Actuals (N, Nam);
6488 -- Create a call reference to the entry
6490 Generate_Reference (Nam, Entry_Name, 's');
6492 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6493 Check_Potentially_Blocking_Operation (N);
6496 -- Verify that a procedure call cannot masquerade as an entry
6497 -- call where an entry call is expected.
6499 if Ekind (Nam) = E_Procedure then
6500 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6501 and then N = Entry_Call_Statement (Parent (N))
6503 Error_Msg_N ("entry call required in select statement", N);
6505 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6506 and then N = Triggering_Statement (Parent (N))
6508 Error_Msg_N ("triggering statement cannot be procedure call", N);
6510 elsif Ekind (Scope (Nam)) = E_Task_Type
6511 and then not In_Open_Scopes (Scope (Nam))
6513 Error_Msg_N ("task has no entry with this name", Entry_Name);
6517 -- After resolution, entry calls and protected procedure calls are
6518 -- changed into entry calls, for expansion. The structure of the node
6519 -- does not change, so it can safely be done in place. Protected
6520 -- function calls must keep their structure because they are
6523 if Ekind (Nam) /= E_Function then
6525 -- A protected operation that is not a function may modify the
6526 -- corresponding object, and cannot apply to a constant. If this
6527 -- is an internal call, the prefix is the type itself.
6529 if Is_Protected_Type (Scope (Nam))
6530 and then not Is_Variable (Obj)
6531 and then (not Is_Entity_Name (Obj)
6532 or else not Is_Type (Entity (Obj)))
6535 ("prefix of protected procedure or entry call must be variable",
6539 Actuals := Parameter_Associations (N);
6540 First_Named := First_Named_Actual (N);
6543 Make_Entry_Call_Statement (Loc,
6545 Parameter_Associations => Actuals));
6547 Set_First_Named_Actual (N, First_Named);
6548 Set_Analyzed (N, True);
6550 -- Protected functions can return on the secondary stack, in which
6551 -- case we must trigger the transient scope mechanism.
6553 elsif Expander_Active
6554 and then Requires_Transient_Scope (Etype (Nam))
6556 Establish_Transient_Scope (N, Sec_Stack => True);
6558 end Resolve_Entry_Call;
6560 -------------------------
6561 -- Resolve_Equality_Op --
6562 -------------------------
6564 -- Both arguments must have the same type, and the boolean context does
6565 -- not participate in the resolution. The first pass verifies that the
6566 -- interpretation is not ambiguous, and the type of the left argument is
6567 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6568 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6569 -- though they carry a single (universal) type. Diagnose this case here.
6571 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6572 L : constant Node_Id := Left_Opnd (N);
6573 R : constant Node_Id := Right_Opnd (N);
6574 T : Entity_Id := Find_Unique_Type (L, R);
6576 procedure Check_Conditional_Expression (Cond : Node_Id);
6577 -- The resolution rule for conditional expressions requires that each
6578 -- such must have a unique type. This means that if several dependent
6579 -- expressions are of a non-null anonymous access type, and the context
6580 -- does not impose an expected type (as can be the case in an equality
6581 -- operation) the expression must be rejected.
6583 function Find_Unique_Access_Type return Entity_Id;
6584 -- In the case of allocators, make a last-ditch attempt to find a single
6585 -- access type with the right designated type. This is semantically
6586 -- dubious, and of no interest to any real code, but c48008a makes it
6589 ----------------------------------
6590 -- Check_Conditional_Expression --
6591 ----------------------------------
6593 procedure Check_Conditional_Expression (Cond : Node_Id) is
6594 Then_Expr : Node_Id;
6595 Else_Expr : Node_Id;
6598 if Nkind (Cond) = N_Conditional_Expression then
6599 Then_Expr := Next (First (Expressions (Cond)));
6600 Else_Expr := Next (Then_Expr);
6602 if Nkind (Then_Expr) /= N_Null
6603 and then Nkind (Else_Expr) /= N_Null
6606 ("cannot determine type of conditional expression", Cond);
6609 end Check_Conditional_Expression;
6611 -----------------------------
6612 -- Find_Unique_Access_Type --
6613 -----------------------------
6615 function Find_Unique_Access_Type return Entity_Id is
6621 if Ekind (Etype (R)) = E_Allocator_Type then
6622 Acc := Designated_Type (Etype (R));
6623 elsif Ekind (Etype (L)) = E_Allocator_Type then
6624 Acc := Designated_Type (Etype (L));
6630 while S /= Standard_Standard loop
6631 E := First_Entity (S);
6632 while Present (E) loop
6634 and then Is_Access_Type (E)
6635 and then Ekind (E) /= E_Allocator_Type
6636 and then Designated_Type (E) = Base_Type (Acc)
6648 end Find_Unique_Access_Type;
6650 -- Start of processing for Resolve_Equality_Op
6653 Set_Etype (N, Base_Type (Typ));
6654 Generate_Reference (T, N, ' ');
6656 if T = Any_Fixed then
6657 T := Unique_Fixed_Point_Type (L);
6660 if T /= Any_Type then
6662 or else T = Any_Composite
6663 or else T = Any_Character
6665 if T = Any_Character then
6666 Ambiguous_Character (L);
6668 Error_Msg_N ("ambiguous operands for equality", N);
6671 Set_Etype (N, Any_Type);
6674 elsif T = Any_Access
6675 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6677 T := Find_Unique_Access_Type;
6680 Error_Msg_N ("ambiguous operands for equality", N);
6681 Set_Etype (N, Any_Type);
6685 -- Conditional expressions must have a single type, and if the
6686 -- context does not impose one the dependent expressions cannot
6687 -- be anonymous access types.
6689 elsif Ada_Version >= Ada_2012
6690 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6691 E_Anonymous_Access_Subprogram_Type)
6692 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6693 E_Anonymous_Access_Subprogram_Type)
6695 Check_Conditional_Expression (L);
6696 Check_Conditional_Expression (R);
6702 -- In SPARK or ALFA, equality operators = and /= for array types
6703 -- other than String are only defined when, for each index position,
6704 -- the operands have equal static bounds.
6706 if Formal_Verification_Mode
6707 and then Comes_From_Source (Original_Node (N))
6708 and then Is_Array_Type (T)
6709 and then Base_Type (T) /= Standard_String
6710 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6713 ("|~~array types should have matching static bounds", N);
6716 -- If the unique type is a class-wide type then it will be expanded
6717 -- into a dispatching call to the predefined primitive. Therefore we
6718 -- check here for potential violation of such restriction.
6720 if Is_Class_Wide_Type (T) then
6721 Check_Restriction (No_Dispatching_Calls, N);
6724 if Warn_On_Redundant_Constructs
6725 and then Comes_From_Source (N)
6726 and then Is_Entity_Name (R)
6727 and then Entity (R) = Standard_True
6728 and then Comes_From_Source (R)
6730 Error_Msg_N -- CODEFIX
6731 ("?comparison with True is redundant!", R);
6734 Check_Unset_Reference (L);
6735 Check_Unset_Reference (R);
6736 Generate_Operator_Reference (N, T);
6737 Check_Low_Bound_Tested (N);
6739 -- If this is an inequality, it may be the implicit inequality
6740 -- created for a user-defined operation, in which case the corres-
6741 -- ponding equality operation is not intrinsic, and the operation
6742 -- cannot be constant-folded. Else fold.
6744 if Nkind (N) = N_Op_Eq
6745 or else Comes_From_Source (Entity (N))
6746 or else Ekind (Entity (N)) = E_Operator
6747 or else Is_Intrinsic_Subprogram
6748 (Corresponding_Equality (Entity (N)))
6750 Eval_Relational_Op (N);
6752 elsif Nkind (N) = N_Op_Ne
6753 and then Is_Abstract_Subprogram (Entity (N))
6755 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6758 -- Ada 2005: If one operand is an anonymous access type, convert the
6759 -- other operand to it, to ensure that the underlying types match in
6760 -- the back-end. Same for access_to_subprogram, and the conversion
6761 -- verifies that the types are subtype conformant.
6763 -- We apply the same conversion in the case one of the operands is a
6764 -- private subtype of the type of the other.
6766 -- Why the Expander_Active test here ???
6770 (Ekind_In (T, E_Anonymous_Access_Type,
6771 E_Anonymous_Access_Subprogram_Type)
6772 or else Is_Private_Type (T))
6774 if Etype (L) /= T then
6776 Make_Unchecked_Type_Conversion (Sloc (L),
6777 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6778 Expression => Relocate_Node (L)));
6779 Analyze_And_Resolve (L, T);
6782 if (Etype (R)) /= T then
6784 Make_Unchecked_Type_Conversion (Sloc (R),
6785 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6786 Expression => Relocate_Node (R)));
6787 Analyze_And_Resolve (R, T);
6791 end Resolve_Equality_Op;
6793 ----------------------------------
6794 -- Resolve_Explicit_Dereference --
6795 ----------------------------------
6797 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6798 Loc : constant Source_Ptr := Sloc (N);
6800 P : constant Node_Id := Prefix (N);
6805 Check_Fully_Declared_Prefix (Typ, P);
6807 if Is_Overloaded (P) then
6809 -- Use the context type to select the prefix that has the correct
6812 Get_First_Interp (P, I, It);
6813 while Present (It.Typ) loop
6814 exit when Is_Access_Type (It.Typ)
6815 and then Covers (Typ, Designated_Type (It.Typ));
6816 Get_Next_Interp (I, It);
6819 if Present (It.Typ) then
6820 Resolve (P, It.Typ);
6822 -- If no interpretation covers the designated type of the prefix,
6823 -- this is the pathological case where not all implementations of
6824 -- the prefix allow the interpretation of the node as a call. Now
6825 -- that the expected type is known, Remove other interpretations
6826 -- from prefix, rewrite it as a call, and resolve again, so that
6827 -- the proper call node is generated.
6829 Get_First_Interp (P, I, It);
6830 while Present (It.Typ) loop
6831 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6835 Get_Next_Interp (I, It);
6839 Make_Function_Call (Loc,
6841 Make_Explicit_Dereference (Loc,
6843 Parameter_Associations => New_List);
6845 Save_Interps (N, New_N);
6847 Analyze_And_Resolve (N, Typ);
6851 Set_Etype (N, Designated_Type (It.Typ));
6857 if Is_Access_Type (Etype (P)) then
6858 Apply_Access_Check (N);
6861 -- If the designated type is a packed unconstrained array type, and the
6862 -- explicit dereference is not in the context of an attribute reference,
6863 -- then we must compute and set the actual subtype, since it is needed
6864 -- by Gigi. The reason we exclude the attribute case is that this is
6865 -- handled fine by Gigi, and in fact we use such attributes to build the
6866 -- actual subtype. We also exclude generated code (which builds actual
6867 -- subtypes directly if they are needed).
6869 if Is_Array_Type (Etype (N))
6870 and then Is_Packed (Etype (N))
6871 and then not Is_Constrained (Etype (N))
6872 and then Nkind (Parent (N)) /= N_Attribute_Reference
6873 and then Comes_From_Source (N)
6875 Set_Etype (N, Get_Actual_Subtype (N));
6878 -- Note: No Eval processing is required for an explicit dereference,
6879 -- because such a name can never be static.
6881 end Resolve_Explicit_Dereference;
6883 -------------------------------------
6884 -- Resolve_Expression_With_Actions --
6885 -------------------------------------
6887 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
6890 end Resolve_Expression_With_Actions;
6892 -------------------------------
6893 -- Resolve_Indexed_Component --
6894 -------------------------------
6896 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6897 Name : constant Node_Id := Prefix (N);
6899 Array_Type : Entity_Id := Empty; -- to prevent junk warning
6903 if Is_Overloaded (Name) then
6905 -- Use the context type to select the prefix that yields the correct
6911 I1 : Interp_Index := 0;
6912 P : constant Node_Id := Prefix (N);
6913 Found : Boolean := False;
6916 Get_First_Interp (P, I, It);
6917 while Present (It.Typ) loop
6918 if (Is_Array_Type (It.Typ)
6919 and then Covers (Typ, Component_Type (It.Typ)))
6920 or else (Is_Access_Type (It.Typ)
6921 and then Is_Array_Type (Designated_Type (It.Typ))
6923 (Typ, Component_Type (Designated_Type (It.Typ))))
6926 It := Disambiguate (P, I1, I, Any_Type);
6928 if It = No_Interp then
6929 Error_Msg_N ("ambiguous prefix for indexing", N);
6935 Array_Type := It.Typ;
6941 Array_Type := It.Typ;
6946 Get_Next_Interp (I, It);
6951 Array_Type := Etype (Name);
6954 Resolve (Name, Array_Type);
6955 Array_Type := Get_Actual_Subtype_If_Available (Name);
6957 -- If prefix is access type, dereference to get real array type.
6958 -- Note: we do not apply an access check because the expander always
6959 -- introduces an explicit dereference, and the check will happen there.
6961 if Is_Access_Type (Array_Type) then
6962 Array_Type := Designated_Type (Array_Type);
6965 -- If name was overloaded, set component type correctly now
6966 -- If a misplaced call to an entry family (which has no index types)
6967 -- return. Error will be diagnosed from calling context.
6969 if Is_Array_Type (Array_Type) then
6970 Set_Etype (N, Component_Type (Array_Type));
6975 Index := First_Index (Array_Type);
6976 Expr := First (Expressions (N));
6978 -- The prefix may have resolved to a string literal, in which case its
6979 -- etype has a special representation. This is only possible currently
6980 -- if the prefix is a static concatenation, written in functional
6983 if Ekind (Array_Type) = E_String_Literal_Subtype then
6984 Resolve (Expr, Standard_Positive);
6987 while Present (Index) and Present (Expr) loop
6988 Resolve (Expr, Etype (Index));
6989 Check_Unset_Reference (Expr);
6991 if Is_Scalar_Type (Etype (Expr)) then
6992 Apply_Scalar_Range_Check (Expr, Etype (Index));
6994 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7002 -- Do not generate the warning on suspicious index if we are analyzing
7003 -- package Ada.Tags; otherwise we will report the warning with the
7004 -- Prims_Ptr field of the dispatch table.
7006 if Scope (Etype (Prefix (N))) = Standard_Standard
7008 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7011 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7012 Eval_Indexed_Component (N);
7015 -- If the array type is atomic, and is packed, and we are in a left side
7016 -- context, then this is worth a warning, since we have a situation
7017 -- where the access to the component may cause extra read/writes of
7018 -- the atomic array object, which could be considered unexpected.
7020 if Nkind (N) = N_Indexed_Component
7021 and then (Is_Atomic (Array_Type)
7022 or else (Is_Entity_Name (Prefix (N))
7023 and then Is_Atomic (Entity (Prefix (N)))))
7024 and then Is_Bit_Packed_Array (Array_Type)
7027 Error_Msg_N ("?assignment to component of packed atomic array",
7029 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7032 end Resolve_Indexed_Component;
7034 -----------------------------
7035 -- Resolve_Integer_Literal --
7036 -----------------------------
7038 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7041 Eval_Integer_Literal (N);
7042 end Resolve_Integer_Literal;
7044 --------------------------------
7045 -- Resolve_Intrinsic_Operator --
7046 --------------------------------
7048 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7049 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7051 Orig_Op : constant Entity_Id := Entity (N);
7056 -- We must preserve the original entity in a generic setting, so that
7057 -- the legality of the operation can be verified in an instance.
7059 if not Expander_Active then
7064 while Scope (Op) /= Standard_Standard loop
7066 pragma Assert (Present (Op));
7070 Set_Is_Overloaded (N, False);
7072 -- If the operand type is private, rewrite with suitable conversions on
7073 -- the operands and the result, to expose the proper underlying numeric
7076 if Is_Private_Type (Typ) then
7077 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
7079 if Nkind (N) = N_Op_Expon then
7080 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7082 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7085 if Nkind (Arg1) = N_Type_Conversion then
7086 Save_Interps (Left_Opnd (N), Expression (Arg1));
7089 if Nkind (Arg2) = N_Type_Conversion then
7090 Save_Interps (Right_Opnd (N), Expression (Arg2));
7093 Set_Left_Opnd (N, Arg1);
7094 Set_Right_Opnd (N, Arg2);
7096 Set_Etype (N, Btyp);
7097 Rewrite (N, Unchecked_Convert_To (Typ, N));
7100 elsif Typ /= Etype (Left_Opnd (N))
7101 or else Typ /= Etype (Right_Opnd (N))
7103 -- Add explicit conversion where needed, and save interpretations in
7104 -- case operands are overloaded. If the context is a VMS operation,
7105 -- assert that the conversion is legal (the operands have the proper
7106 -- types to select the VMS intrinsic). Note that in rare cases the
7107 -- VMS operators may be visible, but the default System is being used
7108 -- and Address is a private type.
7110 Arg1 := Convert_To (Typ, Left_Opnd (N));
7111 Arg2 := Convert_To (Typ, Right_Opnd (N));
7113 if Nkind (Arg1) = N_Type_Conversion then
7114 Save_Interps (Left_Opnd (N), Expression (Arg1));
7116 if Is_VMS_Operator (Orig_Op) then
7117 Set_Conversion_OK (Arg1);
7120 Save_Interps (Left_Opnd (N), Arg1);
7123 if Nkind (Arg2) = N_Type_Conversion then
7124 Save_Interps (Right_Opnd (N), Expression (Arg2));
7126 if Is_VMS_Operator (Orig_Op) then
7127 Set_Conversion_OK (Arg2);
7130 Save_Interps (Right_Opnd (N), Arg2);
7133 Rewrite (Left_Opnd (N), Arg1);
7134 Rewrite (Right_Opnd (N), Arg2);
7137 Resolve_Arithmetic_Op (N, Typ);
7140 Resolve_Arithmetic_Op (N, Typ);
7142 end Resolve_Intrinsic_Operator;
7144 --------------------------------------
7145 -- Resolve_Intrinsic_Unary_Operator --
7146 --------------------------------------
7148 procedure Resolve_Intrinsic_Unary_Operator
7152 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7158 while Scope (Op) /= Standard_Standard loop
7160 pragma Assert (Present (Op));
7165 if Is_Private_Type (Typ) then
7166 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7167 Save_Interps (Right_Opnd (N), Expression (Arg2));
7169 Set_Right_Opnd (N, Arg2);
7171 Set_Etype (N, Btyp);
7172 Rewrite (N, Unchecked_Convert_To (Typ, N));
7176 Resolve_Unary_Op (N, Typ);
7178 end Resolve_Intrinsic_Unary_Operator;
7180 ------------------------
7181 -- Resolve_Logical_Op --
7182 ------------------------
7184 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7188 Check_No_Direct_Boolean_Operators (N);
7190 -- Predefined operations on scalar types yield the base type. On the
7191 -- other hand, logical operations on arrays yield the type of the
7192 -- arguments (and the context).
7194 if Is_Array_Type (Typ) then
7197 B_Typ := Base_Type (Typ);
7200 -- OK if this is a VMS-specific intrinsic operation
7202 if Is_VMS_Operator (Entity (N)) then
7205 -- The following test is required because the operands of the operation
7206 -- may be literals, in which case the resulting type appears to be
7207 -- compatible with a signed integer type, when in fact it is compatible
7208 -- only with modular types. If the context itself is universal, the
7209 -- operation is illegal.
7211 elsif not Valid_Boolean_Arg (Typ) then
7212 Error_Msg_N ("invalid context for logical operation", N);
7213 Set_Etype (N, Any_Type);
7216 elsif Typ = Any_Modular then
7218 ("no modular type available in this context", N);
7219 Set_Etype (N, Any_Type);
7221 elsif Is_Modular_Integer_Type (Typ)
7222 and then Etype (Left_Opnd (N)) = Universal_Integer
7223 and then Etype (Right_Opnd (N)) = Universal_Integer
7225 Check_For_Visible_Operator (N, B_Typ);
7228 Resolve (Left_Opnd (N), B_Typ);
7229 Resolve (Right_Opnd (N), B_Typ);
7231 Check_Unset_Reference (Left_Opnd (N));
7232 Check_Unset_Reference (Right_Opnd (N));
7234 Set_Etype (N, B_Typ);
7235 Generate_Operator_Reference (N, B_Typ);
7236 Eval_Logical_Op (N);
7238 -- In SPARK or ALFA, logical operations AND, OR and XOR for arrays are
7239 -- defined only when both operands have same static lower and higher
7242 if Formal_Verification_Mode
7243 and then Comes_From_Source (Original_Node (N))
7244 and then Is_Array_Type (B_Typ)
7245 and then not Matching_Static_Array_Bounds (Etype (Left_Opnd (N)),
7246 Etype (Right_Opnd (N)))
7248 Error_Msg_F ("|~~array types should have matching static bounds", N);
7251 end Resolve_Logical_Op;
7253 ---------------------------
7254 -- Resolve_Membership_Op --
7255 ---------------------------
7257 -- The context can only be a boolean type, and does not determine
7258 -- the arguments. Arguments should be unambiguous, but the preference
7259 -- rule for universal types applies.
7261 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7262 pragma Warnings (Off, Typ);
7264 L : constant Node_Id := Left_Opnd (N);
7265 R : constant Node_Id := Right_Opnd (N);
7268 procedure Resolve_Set_Membership;
7269 -- Analysis has determined a unique type for the left operand.
7270 -- Use it to resolve the disjuncts.
7272 ----------------------------
7273 -- Resolve_Set_Membership --
7274 ----------------------------
7276 procedure Resolve_Set_Membership is
7280 Resolve (L, Etype (L));
7282 Alt := First (Alternatives (N));
7283 while Present (Alt) loop
7285 -- Alternative is an expression, a range
7286 -- or a subtype mark.
7288 if not Is_Entity_Name (Alt)
7289 or else not Is_Type (Entity (Alt))
7291 Resolve (Alt, Etype (L));
7296 end Resolve_Set_Membership;
7298 -- Start of processing for Resolve_Membership_Op
7301 if L = Error or else R = Error then
7305 if Present (Alternatives (N)) then
7306 Resolve_Set_Membership;
7309 elsif not Is_Overloaded (R)
7311 (Etype (R) = Universal_Integer or else
7312 Etype (R) = Universal_Real)
7313 and then Is_Overloaded (L)
7317 -- Ada 2005 (AI-251): Support the following case:
7319 -- type I is interface;
7320 -- type T is tagged ...
7322 -- function Test (O : I'Class) is
7324 -- return O in T'Class.
7327 -- In this case we have nothing else to do. The membership test will be
7328 -- done at run time.
7330 elsif Ada_Version >= Ada_2005
7331 and then Is_Class_Wide_Type (Etype (L))
7332 and then Is_Interface (Etype (L))
7333 and then Is_Class_Wide_Type (Etype (R))
7334 and then not Is_Interface (Etype (R))
7339 T := Intersect_Types (L, R);
7342 -- If mixed-mode operations are present and operands are all literal,
7343 -- the only interpretation involves Duration, which is probably not
7344 -- the intention of the programmer.
7346 if T = Any_Fixed then
7347 T := Unique_Fixed_Point_Type (N);
7349 if T = Any_Type then
7355 Check_Unset_Reference (L);
7357 if Nkind (R) = N_Range
7358 and then not Is_Scalar_Type (T)
7360 Error_Msg_N ("scalar type required for range", R);
7363 if Is_Entity_Name (R) then
7364 Freeze_Expression (R);
7367 Check_Unset_Reference (R);
7370 Eval_Membership_Op (N);
7371 end Resolve_Membership_Op;
7377 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7378 Loc : constant Source_Ptr := Sloc (N);
7381 -- Handle restriction against anonymous null access values This
7382 -- restriction can be turned off using -gnatdj.
7384 -- Ada 2005 (AI-231): Remove restriction
7386 if Ada_Version < Ada_2005
7387 and then not Debug_Flag_J
7388 and then Ekind (Typ) = E_Anonymous_Access_Type
7389 and then Comes_From_Source (N)
7391 -- In the common case of a call which uses an explicitly null value
7392 -- for an access parameter, give specialized error message.
7394 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7398 ("null is not allowed as argument for an access parameter", N);
7400 -- Standard message for all other cases (are there any?)
7404 ("null cannot be of an anonymous access type", N);
7408 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7409 -- assignment to a null-excluding object
7411 if Ada_Version >= Ada_2005
7412 and then Can_Never_Be_Null (Typ)
7413 and then Nkind (Parent (N)) = N_Assignment_Statement
7415 if not Inside_Init_Proc then
7417 (Compile_Time_Constraint_Error (N,
7418 "(Ada 2005) null not allowed in null-excluding objects?"),
7419 Make_Raise_Constraint_Error (Loc,
7420 Reason => CE_Access_Check_Failed));
7423 Make_Raise_Constraint_Error (Loc,
7424 Reason => CE_Access_Check_Failed));
7428 -- In a distributed context, null for a remote access to subprogram may
7429 -- need to be replaced with a special record aggregate. In this case,
7430 -- return after having done the transformation.
7432 if (Ekind (Typ) = E_Record_Type
7433 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7434 and then Remote_AST_Null_Value (N, Typ)
7439 -- The null literal takes its type from the context
7444 -----------------------
7445 -- Resolve_Op_Concat --
7446 -----------------------
7448 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7450 -- We wish to avoid deep recursion, because concatenations are often
7451 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7452 -- operands nonrecursively until we find something that is not a simple
7453 -- concatenation (A in this case). We resolve that, and then walk back
7454 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7455 -- to do the rest of the work at each level. The Parent pointers allow
7456 -- us to avoid recursion, and thus avoid running out of memory. See also
7457 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7463 -- The following code is equivalent to:
7465 -- Resolve_Op_Concat_First (NN, Typ);
7466 -- Resolve_Op_Concat_Arg (N, ...);
7467 -- Resolve_Op_Concat_Rest (N, Typ);
7469 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7470 -- operand is a concatenation.
7472 -- Walk down left operands
7475 Resolve_Op_Concat_First (NN, Typ);
7476 Op1 := Left_Opnd (NN);
7477 exit when not (Nkind (Op1) = N_Op_Concat
7478 and then not Is_Array_Type (Component_Type (Typ))
7479 and then Entity (Op1) = Entity (NN));
7483 -- Now (given the above example) NN is A&B and Op1 is A
7485 -- First resolve Op1 ...
7487 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7489 -- ... then walk NN back up until we reach N (where we started), calling
7490 -- Resolve_Op_Concat_Rest along the way.
7493 Resolve_Op_Concat_Rest (NN, Typ);
7498 if Formal_Verification_Mode
7499 and then Base_Type (Etype (N)) /= Standard_String
7501 Error_Msg_F ("|~~result of concatenation should have type String", N);
7503 end Resolve_Op_Concat;
7505 ---------------------------
7506 -- Resolve_Op_Concat_Arg --
7507 ---------------------------
7509 procedure Resolve_Op_Concat_Arg
7515 Btyp : constant Entity_Id := Base_Type (Typ);
7520 or else (not Is_Overloaded (Arg)
7521 and then Etype (Arg) /= Any_Composite
7522 and then Covers (Component_Type (Typ), Etype (Arg)))
7524 Resolve (Arg, Component_Type (Typ));
7526 Resolve (Arg, Btyp);
7529 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
7530 if Nkind (Arg) = N_Aggregate
7531 and then Is_Composite_Type (Component_Type (Typ))
7533 if Is_Private_Type (Component_Type (Typ)) then
7534 Resolve (Arg, Btyp);
7536 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7537 Set_Etype (Arg, Any_Type);
7541 if Is_Overloaded (Arg)
7542 and then Has_Compatible_Type (Arg, Typ)
7543 and then Etype (Arg) /= Any_Type
7551 Get_First_Interp (Arg, I, It);
7553 Get_Next_Interp (I, It);
7555 -- Special-case the error message when the overloading is
7556 -- caused by a function that yields an array and can be
7557 -- called without parameters.
7559 if It.Nam = Func then
7560 Error_Msg_Sloc := Sloc (Func);
7561 Error_Msg_N ("ambiguous call to function#", Arg);
7563 ("\\interpretation as call yields&", Arg, Typ);
7565 ("\\interpretation as indexing of call yields&",
7566 Arg, Component_Type (Typ));
7570 ("ambiguous operand for concatenation!", Arg);
7571 Get_First_Interp (Arg, I, It);
7572 while Present (It.Nam) loop
7573 Error_Msg_Sloc := Sloc (It.Nam);
7575 if Base_Type (It.Typ) = Base_Type (Typ)
7576 or else Base_Type (It.Typ) =
7577 Base_Type (Component_Type (Typ))
7579 Error_Msg_N -- CODEFIX
7580 ("\\possible interpretation#", Arg);
7583 Get_Next_Interp (I, It);
7589 Resolve (Arg, Component_Type (Typ));
7591 if Nkind (Arg) = N_String_Literal then
7592 Set_Etype (Arg, Component_Type (Typ));
7595 if Arg = Left_Opnd (N) then
7596 Set_Is_Component_Left_Opnd (N);
7598 Set_Is_Component_Right_Opnd (N);
7603 Resolve (Arg, Btyp);
7606 -- Concatenation is restricted in SPARK or ALFA: each operand must be
7607 -- either a string literal, a static character expression, or another
7608 -- concatenation. Arg cannot be a concatenation here as callers of
7609 -- Resolve_Op_Concat_Arg call it separately on each final operand, past
7610 -- concatenation operations.
7612 if Formal_Verification_Mode then
7613 if Is_Character_Type (Etype (Arg)) then
7614 if not Is_Static_Expression (Arg) then
7615 Error_Msg_F ("|~~character operand for concatenation should be "
7619 elsif Is_String_Type (Etype (Arg)) then
7620 if Nkind (Arg) /= N_String_Literal then
7621 Error_Msg_F ("|~~string operand for concatenation should be "
7625 -- Do not issue error on an operand that is neither a character nor
7626 -- a string, as the error is issued in Resolve_Op_Concat.
7633 Check_Unset_Reference (Arg);
7634 end Resolve_Op_Concat_Arg;
7636 -----------------------------
7637 -- Resolve_Op_Concat_First --
7638 -----------------------------
7640 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7641 Btyp : constant Entity_Id := Base_Type (Typ);
7642 Op1 : constant Node_Id := Left_Opnd (N);
7643 Op2 : constant Node_Id := Right_Opnd (N);
7646 -- The parser folds an enormous sequence of concatenations of string
7647 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7648 -- in the right operand. If the expression resolves to a predefined "&"
7649 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7650 -- we give an error. See P_Simple_Expression in Par.Ch4.
7652 if Nkind (Op2) = N_String_Literal
7653 and then Is_Folded_In_Parser (Op2)
7654 and then Ekind (Entity (N)) = E_Function
7656 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7657 and then String_Length (Strval (Op1)) = 0);
7658 Error_Msg_N ("too many user-defined concatenations", N);
7662 Set_Etype (N, Btyp);
7664 if Is_Limited_Composite (Btyp) then
7665 Error_Msg_N ("concatenation not available for limited array", N);
7666 Explain_Limited_Type (Btyp, N);
7668 end Resolve_Op_Concat_First;
7670 ----------------------------
7671 -- Resolve_Op_Concat_Rest --
7672 ----------------------------
7674 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7675 Op1 : constant Node_Id := Left_Opnd (N);
7676 Op2 : constant Node_Id := Right_Opnd (N);
7679 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7681 Generate_Operator_Reference (N, Typ);
7683 if Is_String_Type (Typ) then
7684 Eval_Concatenation (N);
7687 -- If this is not a static concatenation, but the result is a string
7688 -- type (and not an array of strings) ensure that static string operands
7689 -- have their subtypes properly constructed.
7691 if Nkind (N) /= N_String_Literal
7692 and then Is_Character_Type (Component_Type (Typ))
7694 Set_String_Literal_Subtype (Op1, Typ);
7695 Set_String_Literal_Subtype (Op2, Typ);
7697 end Resolve_Op_Concat_Rest;
7699 ----------------------
7700 -- Resolve_Op_Expon --
7701 ----------------------
7703 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7704 B_Typ : constant Entity_Id := Base_Type (Typ);
7707 -- Catch attempts to do fixed-point exponentiation with universal
7708 -- operands, which is a case where the illegality is not caught during
7709 -- normal operator analysis.
7711 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7712 Error_Msg_N ("exponentiation not available for fixed point", N);
7716 if Comes_From_Source (N)
7717 and then Ekind (Entity (N)) = E_Function
7718 and then Is_Imported (Entity (N))
7719 and then Is_Intrinsic_Subprogram (Entity (N))
7721 Resolve_Intrinsic_Operator (N, Typ);
7725 if Etype (Left_Opnd (N)) = Universal_Integer
7726 or else Etype (Left_Opnd (N)) = Universal_Real
7728 Check_For_Visible_Operator (N, B_Typ);
7731 -- We do the resolution using the base type, because intermediate values
7732 -- in expressions always are of the base type, not a subtype of it.
7734 Resolve (Left_Opnd (N), B_Typ);
7735 Resolve (Right_Opnd (N), Standard_Integer);
7737 Check_Unset_Reference (Left_Opnd (N));
7738 Check_Unset_Reference (Right_Opnd (N));
7740 Set_Etype (N, B_Typ);
7741 Generate_Operator_Reference (N, B_Typ);
7744 -- Set overflow checking bit. Much cleverer code needed here eventually
7745 -- and perhaps the Resolve routines should be separated for the various
7746 -- arithmetic operations, since they will need different processing. ???
7748 if Nkind (N) in N_Op then
7749 if not Overflow_Checks_Suppressed (Etype (N)) then
7750 Enable_Overflow_Check (N);
7753 end Resolve_Op_Expon;
7755 --------------------
7756 -- Resolve_Op_Not --
7757 --------------------
7759 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7762 function Parent_Is_Boolean return Boolean;
7763 -- This function determines if the parent node is a boolean operator
7764 -- or operation (comparison op, membership test, or short circuit form)
7765 -- and the not in question is the left operand of this operation.
7766 -- Note that if the not is in parens, then false is returned.
7768 -----------------------
7769 -- Parent_Is_Boolean --
7770 -----------------------
7772 function Parent_Is_Boolean return Boolean is
7774 if Paren_Count (N) /= 0 then
7778 case Nkind (Parent (N)) is
7793 return Left_Opnd (Parent (N)) = N;
7799 end Parent_Is_Boolean;
7801 -- Start of processing for Resolve_Op_Not
7804 -- Predefined operations on scalar types yield the base type. On the
7805 -- other hand, logical operations on arrays yield the type of the
7806 -- arguments (and the context).
7808 if Is_Array_Type (Typ) then
7811 B_Typ := Base_Type (Typ);
7814 if Is_VMS_Operator (Entity (N)) then
7817 -- Straightforward case of incorrect arguments
7819 elsif not Valid_Boolean_Arg (Typ) then
7820 Error_Msg_N ("invalid operand type for operator&", N);
7821 Set_Etype (N, Any_Type);
7824 -- Special case of probable missing parens
7826 elsif Typ = Universal_Integer or else Typ = Any_Modular then
7827 if Parent_Is_Boolean then
7829 ("operand of not must be enclosed in parentheses",
7833 ("no modular type available in this context", N);
7836 Set_Etype (N, Any_Type);
7839 -- OK resolution of not
7842 -- Warn if non-boolean types involved. This is a case like not a < b
7843 -- where a and b are modular, where we will get (not a) < b and most
7844 -- likely not (a < b) was intended.
7846 if Warn_On_Questionable_Missing_Parens
7847 and then not Is_Boolean_Type (Typ)
7848 and then Parent_Is_Boolean
7850 Error_Msg_N ("?not expression should be parenthesized here!", N);
7853 -- Warn on double negation if checking redundant constructs
7855 if Warn_On_Redundant_Constructs
7856 and then Comes_From_Source (N)
7857 and then Comes_From_Source (Right_Opnd (N))
7858 and then Root_Type (Typ) = Standard_Boolean
7859 and then Nkind (Right_Opnd (N)) = N_Op_Not
7861 Error_Msg_N ("redundant double negation?", N);
7864 -- Complete resolution and evaluation of NOT
7866 Resolve (Right_Opnd (N), B_Typ);
7867 Check_Unset_Reference (Right_Opnd (N));
7868 Set_Etype (N, B_Typ);
7869 Generate_Operator_Reference (N, B_Typ);
7874 -----------------------------
7875 -- Resolve_Operator_Symbol --
7876 -----------------------------
7878 -- Nothing to be done, all resolved already
7880 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
7881 pragma Warnings (Off, N);
7882 pragma Warnings (Off, Typ);
7886 end Resolve_Operator_Symbol;
7888 ----------------------------------
7889 -- Resolve_Qualified_Expression --
7890 ----------------------------------
7892 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
7893 pragma Warnings (Off, Typ);
7895 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7896 Expr : constant Node_Id := Expression (N);
7899 Resolve (Expr, Target_Typ);
7901 if Formal_Verification_Mode
7902 and then Comes_From_Source (Original_Node (N))
7903 and then Is_Array_Type (Target_Typ)
7904 and then Is_Array_Type (Etype (Expr))
7905 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
7907 Error_Msg_F ("|~~array types should have matching static bounds", N);
7910 -- A qualified expression requires an exact match of the type,
7911 -- class-wide matching is not allowed. However, if the qualifying
7912 -- type is specific and the expression has a class-wide type, it
7913 -- may still be okay, since it can be the result of the expansion
7914 -- of a call to a dispatching function, so we also have to check
7915 -- class-wideness of the type of the expression's original node.
7917 if (Is_Class_Wide_Type (Target_Typ)
7919 (Is_Class_Wide_Type (Etype (Expr))
7920 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
7921 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
7923 Wrong_Type (Expr, Target_Typ);
7926 -- If the target type is unconstrained, then we reset the type of the
7927 -- result from the type of the expression. For other cases, the actual
7928 -- subtype of the expression is the target type.
7930 if Is_Composite_Type (Target_Typ)
7931 and then not Is_Constrained (Target_Typ)
7933 Set_Etype (N, Etype (Expr));
7936 Eval_Qualified_Expression (N);
7937 end Resolve_Qualified_Expression;
7939 -----------------------------------
7940 -- Resolve_Quantified_Expression --
7941 -----------------------------------
7943 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
7945 -- The loop structure is already resolved during its analysis, only the
7946 -- resolution of the condition needs to be done. Expansion is disabled
7947 -- so that checks and other generated code are inserted in the tree
7948 -- after expression has been rewritten as a loop.
7950 Expander_Mode_Save_And_Set (False);
7951 Resolve (Condition (N), Typ);
7952 Expander_Mode_Restore;
7953 end Resolve_Quantified_Expression;
7959 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
7960 L : constant Node_Id := Low_Bound (N);
7961 H : constant Node_Id := High_Bound (N);
7963 function First_Last_Ref return Boolean;
7964 -- Returns True if N is of the form X'First .. X'Last where X is the
7965 -- same entity for both attributes.
7967 --------------------
7968 -- First_Last_Ref --
7969 --------------------
7971 function First_Last_Ref return Boolean is
7972 Lorig : constant Node_Id := Original_Node (L);
7973 Horig : constant Node_Id := Original_Node (H);
7976 if Nkind (Lorig) = N_Attribute_Reference
7977 and then Nkind (Horig) = N_Attribute_Reference
7978 and then Attribute_Name (Lorig) = Name_First
7979 and then Attribute_Name (Horig) = Name_Last
7982 PL : constant Node_Id := Prefix (Lorig);
7983 PH : constant Node_Id := Prefix (Horig);
7985 if Is_Entity_Name (PL)
7986 and then Is_Entity_Name (PH)
7987 and then Entity (PL) = Entity (PH)
7997 -- Start of processing for Resolve_Range
8004 -- Check for inappropriate range on unordered enumeration type
8006 if Bad_Unordered_Enumeration_Reference (N, Typ)
8008 -- Exclude X'First .. X'Last if X is the same entity for both
8010 and then not First_Last_Ref
8012 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
8015 Check_Unset_Reference (L);
8016 Check_Unset_Reference (H);
8018 -- We have to check the bounds for being within the base range as
8019 -- required for a non-static context. Normally this is automatic and
8020 -- done as part of evaluating expressions, but the N_Range node is an
8021 -- exception, since in GNAT we consider this node to be a subexpression,
8022 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8023 -- this, but that would put the test on the main evaluation path for
8026 Check_Non_Static_Context (L);
8027 Check_Non_Static_Context (H);
8029 -- Check for an ambiguous range over character literals. This will
8030 -- happen with a membership test involving only literals.
8032 if Typ = Any_Character then
8033 Ambiguous_Character (L);
8034 Set_Etype (N, Any_Type);
8038 -- If bounds are static, constant-fold them, so size computations
8039 -- are identical between front-end and back-end. Do not perform this
8040 -- transformation while analyzing generic units, as type information
8041 -- would then be lost when reanalyzing the constant node in the
8044 if Is_Discrete_Type (Typ) and then Expander_Active then
8045 if Is_OK_Static_Expression (L) then
8046 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8049 if Is_OK_Static_Expression (H) then
8050 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8055 --------------------------
8056 -- Resolve_Real_Literal --
8057 --------------------------
8059 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8060 Actual_Typ : constant Entity_Id := Etype (N);
8063 -- Special processing for fixed-point literals to make sure that the
8064 -- value is an exact multiple of small where this is required. We
8065 -- skip this for the universal real case, and also for generic types.
8067 if Is_Fixed_Point_Type (Typ)
8068 and then Typ /= Universal_Fixed
8069 and then Typ /= Any_Fixed
8070 and then not Is_Generic_Type (Typ)
8073 Val : constant Ureal := Realval (N);
8074 Cintr : constant Ureal := Val / Small_Value (Typ);
8075 Cint : constant Uint := UR_Trunc (Cintr);
8076 Den : constant Uint := Norm_Den (Cintr);
8080 -- Case of literal is not an exact multiple of the Small
8084 -- For a source program literal for a decimal fixed-point
8085 -- type, this is statically illegal (RM 4.9(36)).
8087 if Is_Decimal_Fixed_Point_Type (Typ)
8088 and then Actual_Typ = Universal_Real
8089 and then Comes_From_Source (N)
8091 Error_Msg_N ("value has extraneous low order digits", N);
8094 -- Generate a warning if literal from source
8096 if Is_Static_Expression (N)
8097 and then Warn_On_Bad_Fixed_Value
8100 ("?static fixed-point value is not a multiple of Small!",
8104 -- Replace literal by a value that is the exact representation
8105 -- of a value of the type, i.e. a multiple of the small value,
8106 -- by truncation, since Machine_Rounds is false for all GNAT
8107 -- fixed-point types (RM 4.9(38)).
8109 Stat := Is_Static_Expression (N);
8111 Make_Real_Literal (Sloc (N),
8112 Realval => Small_Value (Typ) * Cint));
8114 Set_Is_Static_Expression (N, Stat);
8117 -- In all cases, set the corresponding integer field
8119 Set_Corresponding_Integer_Value (N, Cint);
8123 -- Now replace the actual type by the expected type as usual
8126 Eval_Real_Literal (N);
8127 end Resolve_Real_Literal;
8129 -----------------------
8130 -- Resolve_Reference --
8131 -----------------------
8133 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8134 P : constant Node_Id := Prefix (N);
8137 -- Replace general access with specific type
8139 if Ekind (Etype (N)) = E_Allocator_Type then
8140 Set_Etype (N, Base_Type (Typ));
8143 Resolve (P, Designated_Type (Etype (N)));
8145 -- If we are taking the reference of a volatile entity, then treat
8146 -- it as a potential modification of this entity. This is much too
8147 -- conservative, but is necessary because remove side effects can
8148 -- result in transformations of normal assignments into reference
8149 -- sequences that otherwise fail to notice the modification.
8151 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8152 Note_Possible_Modification (P, Sure => False);
8154 end Resolve_Reference;
8156 --------------------------------
8157 -- Resolve_Selected_Component --
8158 --------------------------------
8160 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8162 Comp1 : Entity_Id := Empty; -- prevent junk warning
8163 P : constant Node_Id := Prefix (N);
8164 S : constant Node_Id := Selector_Name (N);
8165 T : Entity_Id := Etype (P);
8167 I1 : Interp_Index := 0; -- prevent junk warning
8172 function Init_Component return Boolean;
8173 -- Check whether this is the initialization of a component within an
8174 -- init proc (by assignment or call to another init proc). If true,
8175 -- there is no need for a discriminant check.
8177 --------------------
8178 -- Init_Component --
8179 --------------------
8181 function Init_Component return Boolean is
8183 return Inside_Init_Proc
8184 and then Nkind (Prefix (N)) = N_Identifier
8185 and then Chars (Prefix (N)) = Name_uInit
8186 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8189 -- Start of processing for Resolve_Selected_Component
8192 if Is_Overloaded (P) then
8194 -- Use the context type to select the prefix that has a selector
8195 -- of the correct name and type.
8198 Get_First_Interp (P, I, It);
8200 Search : while Present (It.Typ) loop
8201 if Is_Access_Type (It.Typ) then
8202 T := Designated_Type (It.Typ);
8207 if Is_Record_Type (T) then
8209 -- The visible components of a class-wide type are those of
8212 if Is_Class_Wide_Type (T) then
8216 Comp := First_Entity (T);
8217 while Present (Comp) loop
8218 if Chars (Comp) = Chars (S)
8219 and then Covers (Etype (Comp), Typ)
8228 It := Disambiguate (P, I1, I, Any_Type);
8230 if It = No_Interp then
8232 ("ambiguous prefix for selected component", N);
8239 -- There may be an implicit dereference. Retrieve
8240 -- designated record type.
8242 if Is_Access_Type (It1.Typ) then
8243 T := Designated_Type (It1.Typ);
8248 if Scope (Comp1) /= T then
8250 -- Resolution chooses the new interpretation.
8251 -- Find the component with the right name.
8253 Comp1 := First_Entity (T);
8254 while Present (Comp1)
8255 and then Chars (Comp1) /= Chars (S)
8257 Comp1 := Next_Entity (Comp1);
8266 Comp := Next_Entity (Comp);
8270 Get_Next_Interp (I, It);
8273 Resolve (P, It1.Typ);
8275 Set_Entity_With_Style_Check (S, Comp1);
8278 -- Resolve prefix with its type
8283 -- Generate cross-reference. We needed to wait until full overloading
8284 -- resolution was complete to do this, since otherwise we can't tell if
8285 -- we are an lvalue or not.
8287 if May_Be_Lvalue (N) then
8288 Generate_Reference (Entity (S), S, 'm');
8290 Generate_Reference (Entity (S), S, 'r');
8293 -- If prefix is an access type, the node will be transformed into an
8294 -- explicit dereference during expansion. The type of the node is the
8295 -- designated type of that of the prefix.
8297 if Is_Access_Type (Etype (P)) then
8298 T := Designated_Type (Etype (P));
8299 Check_Fully_Declared_Prefix (T, P);
8304 if Has_Discriminants (T)
8305 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8306 and then Present (Original_Record_Component (Entity (S)))
8307 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8308 and then Present (Discriminant_Checking_Func
8309 (Original_Record_Component (Entity (S))))
8310 and then not Discriminant_Checks_Suppressed (T)
8311 and then not Init_Component
8313 Set_Do_Discriminant_Check (N);
8316 if Ekind (Entity (S)) = E_Void then
8317 Error_Msg_N ("premature use of component", S);
8320 -- If the prefix is a record conversion, this may be a renamed
8321 -- discriminant whose bounds differ from those of the original
8322 -- one, so we must ensure that a range check is performed.
8324 if Nkind (P) = N_Type_Conversion
8325 and then Ekind (Entity (S)) = E_Discriminant
8326 and then Is_Discrete_Type (Typ)
8328 Set_Etype (N, Base_Type (Typ));
8331 -- Note: No Eval processing is required, because the prefix is of a
8332 -- record type, or protected type, and neither can possibly be static.
8334 -- If the array type is atomic, and is packed, and we are in a left side
8335 -- context, then this is worth a warning, since we have a situation
8336 -- where the access to the component may cause extra read/writes of
8337 -- the atomic array object, which could be considered unexpected.
8339 if Nkind (N) = N_Selected_Component
8340 and then (Is_Atomic (T)
8341 or else (Is_Entity_Name (Prefix (N))
8342 and then Is_Atomic (Entity (Prefix (N)))))
8343 and then Is_Packed (T)
8346 Error_Msg_N ("?assignment to component of packed atomic record",
8348 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8351 end Resolve_Selected_Component;
8357 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8358 B_Typ : constant Entity_Id := Base_Type (Typ);
8359 L : constant Node_Id := Left_Opnd (N);
8360 R : constant Node_Id := Right_Opnd (N);
8363 -- We do the resolution using the base type, because intermediate values
8364 -- in expressions always are of the base type, not a subtype of it.
8367 Resolve (R, Standard_Natural);
8369 Check_Unset_Reference (L);
8370 Check_Unset_Reference (R);
8372 Set_Etype (N, B_Typ);
8373 Generate_Operator_Reference (N, B_Typ);
8377 ---------------------------
8378 -- Resolve_Short_Circuit --
8379 ---------------------------
8381 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8382 B_Typ : constant Entity_Id := Base_Type (Typ);
8383 L : constant Node_Id := Left_Opnd (N);
8384 R : constant Node_Id := Right_Opnd (N);
8390 -- Check for issuing warning for always False assert/check, this happens
8391 -- when assertions are turned off, in which case the pragma Assert/Check
8392 -- was transformed into:
8394 -- if False and then <condition> then ...
8396 -- and we detect this pattern
8398 if Warn_On_Assertion_Failure
8399 and then Is_Entity_Name (R)
8400 and then Entity (R) = Standard_False
8401 and then Nkind (Parent (N)) = N_If_Statement
8402 and then Nkind (N) = N_And_Then
8403 and then Is_Entity_Name (L)
8404 and then Entity (L) = Standard_False
8407 Orig : constant Node_Id := Original_Node (Parent (N));
8410 if Nkind (Orig) = N_Pragma
8411 and then Pragma_Name (Orig) = Name_Assert
8413 -- Don't want to warn if original condition is explicit False
8416 Expr : constant Node_Id :=
8419 (First (Pragma_Argument_Associations (Orig))));
8421 if Is_Entity_Name (Expr)
8422 and then Entity (Expr) = Standard_False
8426 -- Issue warning. We do not want the deletion of the
8427 -- IF/AND-THEN to take this message with it. We achieve
8428 -- this by making sure that the expanded code points to
8429 -- the Sloc of the expression, not the original pragma.
8432 ("?assertion would fail at run time!",
8434 (First (Pragma_Argument_Associations (Orig))));
8438 -- Similar processing for Check pragma
8440 elsif Nkind (Orig) = N_Pragma
8441 and then Pragma_Name (Orig) = Name_Check
8443 -- Don't want to warn if original condition is explicit False
8446 Expr : constant Node_Id :=
8450 (Pragma_Argument_Associations (Orig)))));
8452 if Is_Entity_Name (Expr)
8453 and then Entity (Expr) = Standard_False
8458 ("?check would fail at run time!",
8460 (Last (Pragma_Argument_Associations (Orig))));
8467 -- Continue with processing of short circuit
8469 Check_Unset_Reference (L);
8470 Check_Unset_Reference (R);
8472 Set_Etype (N, B_Typ);
8473 Eval_Short_Circuit (N);
8474 end Resolve_Short_Circuit;
8480 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8481 Name : constant Node_Id := Prefix (N);
8482 Drange : constant Node_Id := Discrete_Range (N);
8483 Array_Type : Entity_Id := Empty;
8487 if Is_Overloaded (Name) then
8489 -- Use the context type to select the prefix that yields the correct
8494 I1 : Interp_Index := 0;
8496 P : constant Node_Id := Prefix (N);
8497 Found : Boolean := False;
8500 Get_First_Interp (P, I, It);
8501 while Present (It.Typ) loop
8502 if (Is_Array_Type (It.Typ)
8503 and then Covers (Typ, It.Typ))
8504 or else (Is_Access_Type (It.Typ)
8505 and then Is_Array_Type (Designated_Type (It.Typ))
8506 and then Covers (Typ, Designated_Type (It.Typ)))
8509 It := Disambiguate (P, I1, I, Any_Type);
8511 if It = No_Interp then
8512 Error_Msg_N ("ambiguous prefix for slicing", N);
8517 Array_Type := It.Typ;
8522 Array_Type := It.Typ;
8527 Get_Next_Interp (I, It);
8532 Array_Type := Etype (Name);
8535 Resolve (Name, Array_Type);
8537 if Is_Access_Type (Array_Type) then
8538 Apply_Access_Check (N);
8539 Array_Type := Designated_Type (Array_Type);
8541 -- If the prefix is an access to an unconstrained array, we must use
8542 -- the actual subtype of the object to perform the index checks. The
8543 -- object denoted by the prefix is implicit in the node, so we build
8544 -- an explicit representation for it in order to compute the actual
8547 if not Is_Constrained (Array_Type) then
8548 Remove_Side_Effects (Prefix (N));
8551 Obj : constant Node_Id :=
8552 Make_Explicit_Dereference (Sloc (N),
8553 Prefix => New_Copy_Tree (Prefix (N)));
8555 Set_Etype (Obj, Array_Type);
8556 Set_Parent (Obj, Parent (N));
8557 Array_Type := Get_Actual_Subtype (Obj);
8561 elsif Is_Entity_Name (Name)
8562 or else Nkind (Name) = N_Explicit_Dereference
8563 or else (Nkind (Name) = N_Function_Call
8564 and then not Is_Constrained (Etype (Name)))
8566 Array_Type := Get_Actual_Subtype (Name);
8568 -- If the name is a selected component that depends on discriminants,
8569 -- build an actual subtype for it. This can happen only when the name
8570 -- itself is overloaded; otherwise the actual subtype is created when
8571 -- the selected component is analyzed.
8573 elsif Nkind (Name) = N_Selected_Component
8574 and then Full_Analysis
8575 and then Depends_On_Discriminant (First_Index (Array_Type))
8578 Act_Decl : constant Node_Id :=
8579 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8581 Insert_Action (N, Act_Decl);
8582 Array_Type := Defining_Identifier (Act_Decl);
8585 -- Maybe this should just be "else", instead of checking for the
8586 -- specific case of slice??? This is needed for the case where
8587 -- the prefix is an Image attribute, which gets expanded to a
8588 -- slice, and so has a constrained subtype which we want to use
8589 -- for the slice range check applied below (the range check won't
8590 -- get done if the unconstrained subtype of the 'Image is used).
8592 elsif Nkind (Name) = N_Slice then
8593 Array_Type := Etype (Name);
8596 -- If name was overloaded, set slice type correctly now
8598 Set_Etype (N, Array_Type);
8600 -- If the range is specified by a subtype mark, no resolution is
8601 -- necessary. Else resolve the bounds, and apply needed checks.
8603 if not Is_Entity_Name (Drange) then
8604 Index := First_Index (Array_Type);
8605 Resolve (Drange, Base_Type (Etype (Index)));
8607 if Nkind (Drange) = N_Range then
8609 -- Ensure that side effects in the bounds are properly handled
8611 Remove_Side_Effects (Low_Bound (Drange), Variable_Ref => True);
8612 Remove_Side_Effects (High_Bound (Drange), Variable_Ref => True);
8614 -- Do not apply the range check to nodes associated with the
8615 -- frontend expansion of the dispatch table. We first check
8616 -- if Ada.Tags is already loaded to avoid the addition of an
8617 -- undesired dependence on such run-time unit.
8619 if not Tagged_Type_Expansion
8621 (RTU_Loaded (Ada_Tags)
8622 and then Nkind (Prefix (N)) = N_Selected_Component
8623 and then Present (Entity (Selector_Name (Prefix (N))))
8624 and then Entity (Selector_Name (Prefix (N))) =
8625 RTE_Record_Component (RE_Prims_Ptr))
8627 Apply_Range_Check (Drange, Etype (Index));
8632 Set_Slice_Subtype (N);
8634 -- Check bad use of type with predicates
8636 if Has_Predicates (Etype (Drange)) then
8637 Bad_Predicated_Subtype_Use
8638 ("subtype& has predicate, not allowed in slice",
8639 Drange, Etype (Drange));
8641 -- Otherwise here is where we check suspicious indexes
8643 elsif Nkind (Drange) = N_Range then
8644 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8645 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8651 ----------------------------
8652 -- Resolve_String_Literal --
8653 ----------------------------
8655 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8656 C_Typ : constant Entity_Id := Component_Type (Typ);
8657 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8658 Loc : constant Source_Ptr := Sloc (N);
8659 Str : constant String_Id := Strval (N);
8660 Strlen : constant Nat := String_Length (Str);
8661 Subtype_Id : Entity_Id;
8662 Need_Check : Boolean;
8665 -- For a string appearing in a concatenation, defer creation of the
8666 -- string_literal_subtype until the end of the resolution of the
8667 -- concatenation, because the literal may be constant-folded away. This
8668 -- is a useful optimization for long concatenation expressions.
8670 -- If the string is an aggregate built for a single character (which
8671 -- happens in a non-static context) or a is null string to which special
8672 -- checks may apply, we build the subtype. Wide strings must also get a
8673 -- string subtype if they come from a one character aggregate. Strings
8674 -- generated by attributes might be static, but it is often hard to
8675 -- determine whether the enclosing context is static, so we generate
8676 -- subtypes for them as well, thus losing some rarer optimizations ???
8677 -- Same for strings that come from a static conversion.
8680 (Strlen = 0 and then Typ /= Standard_String)
8681 or else Nkind (Parent (N)) /= N_Op_Concat
8682 or else (N /= Left_Opnd (Parent (N))
8683 and then N /= Right_Opnd (Parent (N)))
8684 or else ((Typ = Standard_Wide_String
8685 or else Typ = Standard_Wide_Wide_String)
8686 and then Nkind (Original_Node (N)) /= N_String_Literal);
8688 -- If the resolving type is itself a string literal subtype, we can just
8689 -- reuse it, since there is no point in creating another.
8691 if Ekind (Typ) = E_String_Literal_Subtype then
8694 elsif Nkind (Parent (N)) = N_Op_Concat
8695 and then not Need_Check
8696 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8697 N_Attribute_Reference,
8698 N_Qualified_Expression,
8703 -- Otherwise we must create a string literal subtype. Note that the
8704 -- whole idea of string literal subtypes is simply to avoid the need
8705 -- for building a full fledged array subtype for each literal.
8708 Set_String_Literal_Subtype (N, Typ);
8709 Subtype_Id := Etype (N);
8712 if Nkind (Parent (N)) /= N_Op_Concat
8715 Set_Etype (N, Subtype_Id);
8716 Eval_String_Literal (N);
8719 if Is_Limited_Composite (Typ)
8720 or else Is_Private_Composite (Typ)
8722 Error_Msg_N ("string literal not available for private array", N);
8723 Set_Etype (N, Any_Type);
8727 -- The validity of a null string has been checked in the call to
8728 -- Eval_String_Literal.
8733 -- Always accept string literal with component type Any_Character, which
8734 -- occurs in error situations and in comparisons of literals, both of
8735 -- which should accept all literals.
8737 elsif R_Typ = Any_Character then
8740 -- If the type is bit-packed, then we always transform the string
8741 -- literal into a full fledged aggregate.
8743 elsif Is_Bit_Packed_Array (Typ) then
8746 -- Deal with cases of Wide_Wide_String, Wide_String, and String
8749 -- For Standard.Wide_Wide_String, or any other type whose component
8750 -- type is Standard.Wide_Wide_Character, we know that all the
8751 -- characters in the string must be acceptable, since the parser
8752 -- accepted the characters as valid character literals.
8754 if R_Typ = Standard_Wide_Wide_Character then
8757 -- For the case of Standard.String, or any other type whose component
8758 -- type is Standard.Character, we must make sure that there are no
8759 -- wide characters in the string, i.e. that it is entirely composed
8760 -- of characters in range of type Character.
8762 -- If the string literal is the result of a static concatenation, the
8763 -- test has already been performed on the components, and need not be
8766 elsif R_Typ = Standard_Character
8767 and then Nkind (Original_Node (N)) /= N_Op_Concat
8769 for J in 1 .. Strlen loop
8770 if not In_Character_Range (Get_String_Char (Str, J)) then
8772 -- If we are out of range, post error. This is one of the
8773 -- very few places that we place the flag in the middle of
8774 -- a token, right under the offending wide character. Not
8775 -- quite clear if this is right wrt wide character encoding
8776 -- sequences, but it's only an error message!
8779 ("literal out of range of type Standard.Character",
8780 Source_Ptr (Int (Loc) + J));
8785 -- For the case of Standard.Wide_String, or any other type whose
8786 -- component type is Standard.Wide_Character, we must make sure that
8787 -- there are no wide characters in the string, i.e. that it is
8788 -- entirely composed of characters in range of type Wide_Character.
8790 -- If the string literal is the result of a static concatenation,
8791 -- the test has already been performed on the components, and need
8794 elsif R_Typ = Standard_Wide_Character
8795 and then Nkind (Original_Node (N)) /= N_Op_Concat
8797 for J in 1 .. Strlen loop
8798 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8800 -- If we are out of range, post error. This is one of the
8801 -- very few places that we place the flag in the middle of
8802 -- a token, right under the offending wide character.
8804 -- This is not quite right, because characters in general
8805 -- will take more than one character position ???
8808 ("literal out of range of type Standard.Wide_Character",
8809 Source_Ptr (Int (Loc) + J));
8814 -- If the root type is not a standard character, then we will convert
8815 -- the string into an aggregate and will let the aggregate code do
8816 -- the checking. Standard Wide_Wide_Character is also OK here.
8822 -- See if the component type of the array corresponding to the string
8823 -- has compile time known bounds. If yes we can directly check
8824 -- whether the evaluation of the string will raise constraint error.
8825 -- Otherwise we need to transform the string literal into the
8826 -- corresponding character aggregate and let the aggregate
8827 -- code do the checking.
8829 if Is_Standard_Character_Type (R_Typ) then
8831 -- Check for the case of full range, where we are definitely OK
8833 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8837 -- Here the range is not the complete base type range, so check
8840 Comp_Typ_Lo : constant Node_Id :=
8841 Type_Low_Bound (Component_Type (Typ));
8842 Comp_Typ_Hi : constant Node_Id :=
8843 Type_High_Bound (Component_Type (Typ));
8848 if Compile_Time_Known_Value (Comp_Typ_Lo)
8849 and then Compile_Time_Known_Value (Comp_Typ_Hi)
8851 for J in 1 .. Strlen loop
8852 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
8854 if Char_Val < Expr_Value (Comp_Typ_Lo)
8855 or else Char_Val > Expr_Value (Comp_Typ_Hi)
8857 Apply_Compile_Time_Constraint_Error
8858 (N, "character out of range?", CE_Range_Check_Failed,
8859 Loc => Source_Ptr (Int (Loc) + J));
8869 -- If we got here we meed to transform the string literal into the
8870 -- equivalent qualified positional array aggregate. This is rather
8871 -- heavy artillery for this situation, but it is hard work to avoid.
8874 Lits : constant List_Id := New_List;
8875 P : Source_Ptr := Loc + 1;
8879 -- Build the character literals, we give them source locations that
8880 -- correspond to the string positions, which is a bit tricky given
8881 -- the possible presence of wide character escape sequences.
8883 for J in 1 .. Strlen loop
8884 C := Get_String_Char (Str, J);
8885 Set_Character_Literal_Name (C);
8888 Make_Character_Literal (P,
8890 Char_Literal_Value => UI_From_CC (C)));
8892 if In_Character_Range (C) then
8895 -- Should we have a call to Skip_Wide here ???
8903 Make_Qualified_Expression (Loc,
8904 Subtype_Mark => New_Reference_To (Typ, Loc),
8906 Make_Aggregate (Loc, Expressions => Lits)));
8908 Analyze_And_Resolve (N, Typ);
8910 end Resolve_String_Literal;
8912 -----------------------------
8913 -- Resolve_Subprogram_Info --
8914 -----------------------------
8916 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
8919 end Resolve_Subprogram_Info;
8921 -----------------------------
8922 -- Resolve_Type_Conversion --
8923 -----------------------------
8925 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
8926 Conv_OK : constant Boolean := Conversion_OK (N);
8927 Operand : constant Node_Id := Expression (N);
8928 Operand_Typ : constant Entity_Id := Etype (Operand);
8929 Target_Typ : constant Entity_Id := Etype (N);
8934 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
8935 -- Set to False to suppress cases where we want to suppress the test
8936 -- for redundancy to avoid possible false positives on this warning.
8940 and then not Valid_Conversion (N, Target_Typ, Operand)
8945 -- If the Operand Etype is Universal_Fixed, then the conversion is
8946 -- never redundant. We need this check because by the time we have
8947 -- finished the rather complex transformation, the conversion looks
8948 -- redundant when it is not.
8950 if Operand_Typ = Universal_Fixed then
8951 Test_Redundant := False;
8953 -- If the operand is marked as Any_Fixed, then special processing is
8954 -- required. This is also a case where we suppress the test for a
8955 -- redundant conversion, since most certainly it is not redundant.
8957 elsif Operand_Typ = Any_Fixed then
8958 Test_Redundant := False;
8960 -- Mixed-mode operation involving a literal. Context must be a fixed
8961 -- type which is applied to the literal subsequently.
8963 if Is_Fixed_Point_Type (Typ) then
8964 Set_Etype (Operand, Universal_Real);
8966 elsif Is_Numeric_Type (Typ)
8967 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
8968 and then (Etype (Right_Opnd (Operand)) = Universal_Real
8970 Etype (Left_Opnd (Operand)) = Universal_Real)
8972 -- Return if expression is ambiguous
8974 if Unique_Fixed_Point_Type (N) = Any_Type then
8977 -- If nothing else, the available fixed type is Duration
8980 Set_Etype (Operand, Standard_Duration);
8983 -- Resolve the real operand with largest available precision
8985 if Etype (Right_Opnd (Operand)) = Universal_Real then
8986 Rop := New_Copy_Tree (Right_Opnd (Operand));
8988 Rop := New_Copy_Tree (Left_Opnd (Operand));
8991 Resolve (Rop, Universal_Real);
8993 -- If the operand is a literal (it could be a non-static and
8994 -- illegal exponentiation) check whether the use of Duration
8995 -- is potentially inaccurate.
8997 if Nkind (Rop) = N_Real_Literal
8998 and then Realval (Rop) /= Ureal_0
8999 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9002 ("?universal real operand can only " &
9003 "be interpreted as Duration!",
9006 ("\?precision will be lost in the conversion!", Rop);
9009 elsif Is_Numeric_Type (Typ)
9010 and then Nkind (Operand) in N_Op
9011 and then Unique_Fixed_Point_Type (N) /= Any_Type
9013 Set_Etype (Operand, Standard_Duration);
9016 Error_Msg_N ("invalid context for mixed mode operation", N);
9017 Set_Etype (Operand, Any_Type);
9024 -- In SPARK or ALFA, a type conversion between array types should be
9025 -- restricted to types which have matching static bounds.
9027 if Formal_Verification_Mode
9028 and then Comes_From_Source (Original_Node (N))
9029 and then Is_Array_Type (Target_Typ)
9030 and then Is_Array_Type (Operand_Typ)
9031 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9033 Error_Msg_F ("|~~array types should have matching static bounds", N);
9036 -- Note: we do the Eval_Type_Conversion call before applying the
9037 -- required checks for a subtype conversion. This is important, since
9038 -- both are prepared under certain circumstances to change the type
9039 -- conversion to a constraint error node, but in the case of
9040 -- Eval_Type_Conversion this may reflect an illegality in the static
9041 -- case, and we would miss the illegality (getting only a warning
9042 -- message), if we applied the type conversion checks first.
9044 Eval_Type_Conversion (N);
9046 -- Even when evaluation is not possible, we may be able to simplify the
9047 -- conversion or its expression. This needs to be done before applying
9048 -- checks, since otherwise the checks may use the original expression
9049 -- and defeat the simplifications. This is specifically the case for
9050 -- elimination of the floating-point Truncation attribute in
9051 -- float-to-int conversions.
9053 Simplify_Type_Conversion (N);
9055 -- If after evaluation we still have a type conversion, then we may need
9056 -- to apply checks required for a subtype conversion.
9058 -- Skip these type conversion checks if universal fixed operands
9059 -- operands involved, since range checks are handled separately for
9060 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9062 if Nkind (N) = N_Type_Conversion
9063 and then not Is_Generic_Type (Root_Type (Target_Typ))
9064 and then Target_Typ /= Universal_Fixed
9065 and then Operand_Typ /= Universal_Fixed
9067 Apply_Type_Conversion_Checks (N);
9070 -- Issue warning for conversion of simple object to its own type. We
9071 -- have to test the original nodes, since they may have been rewritten
9072 -- by various optimizations.
9074 Orig_N := Original_Node (N);
9076 -- Here we test for a redundant conversion if the warning mode is
9077 -- active (and was not locally reset), and we have a type conversion
9078 -- from source not appearing in a generic instance.
9081 and then Nkind (Orig_N) = N_Type_Conversion
9082 and then Comes_From_Source (Orig_N)
9083 and then not In_Instance
9085 Orig_N := Original_Node (Expression (Orig_N));
9086 Orig_T := Target_Typ;
9088 -- If the node is part of a larger expression, the Target_Type
9089 -- may not be the original type of the node if the context is a
9090 -- condition. Recover original type to see if conversion is needed.
9092 if Is_Boolean_Type (Orig_T)
9093 and then Nkind (Parent (N)) in N_Op
9095 Orig_T := Etype (Parent (N));
9098 -- If we have an entity name, then give the warning if the entity
9099 -- is the right type, or if it is a loop parameter covered by the
9100 -- original type (that's needed because loop parameters have an
9101 -- odd subtype coming from the bounds).
9103 if (Is_Entity_Name (Orig_N)
9105 (Etype (Entity (Orig_N)) = Orig_T
9107 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9108 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9110 -- If not an entity, then type of expression must match
9112 or else Etype (Orig_N) = Orig_T
9114 -- One more check, do not give warning if the analyzed conversion
9115 -- has an expression with non-static bounds, and the bounds of the
9116 -- target are static. This avoids junk warnings in cases where the
9117 -- conversion is necessary to establish staticness, for example in
9118 -- a case statement.
9120 if not Is_OK_Static_Subtype (Operand_Typ)
9121 and then Is_OK_Static_Subtype (Target_Typ)
9125 -- Finally, if this type conversion occurs in a context that
9126 -- requires a prefix, and the expression is a qualified expression
9127 -- then the type conversion is not redundant, because a qualified
9128 -- expression is not a prefix, whereas a type conversion is. For
9129 -- example, "X := T'(Funx(...)).Y;" is illegal because a selected
9130 -- component requires a prefix, but a type conversion makes it
9131 -- legal: "X := T(T'(Funx(...))).Y;"
9133 -- In Ada 2012, a qualified expression is a name, so this idiom is
9134 -- no longer needed, but we still suppress the warning because it
9135 -- seems unfriendly for warnings to pop up when you switch to the
9136 -- newer language version.
9138 elsif Nkind (Orig_N) = N_Qualified_Expression
9139 and then Nkind_In (Parent (N), N_Attribute_Reference,
9140 N_Indexed_Component,
9141 N_Selected_Component,
9143 N_Explicit_Dereference)
9147 -- Here we give the redundant conversion warning. If it is an
9148 -- entity, give the name of the entity in the message. If not,
9149 -- just mention the expression.
9152 if Is_Entity_Name (Orig_N) then
9153 Error_Msg_Node_2 := Orig_T;
9154 Error_Msg_NE -- CODEFIX
9155 ("?redundant conversion, & is of type &!",
9156 N, Entity (Orig_N));
9159 ("?redundant conversion, expression is of type&!",
9166 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9167 -- No need to perform any interface conversion if the type of the
9168 -- expression coincides with the target type.
9170 if Ada_Version >= Ada_2005
9171 and then Expander_Active
9172 and then Operand_Typ /= Target_Typ
9175 Opnd : Entity_Id := Operand_Typ;
9176 Target : Entity_Id := Target_Typ;
9179 if Is_Access_Type (Opnd) then
9180 Opnd := Designated_Type (Opnd);
9183 if Is_Access_Type (Target_Typ) then
9184 Target := Designated_Type (Target);
9187 if Opnd = Target then
9190 -- Conversion from interface type
9192 elsif Is_Interface (Opnd) then
9194 -- Ada 2005 (AI-217): Handle entities from limited views
9196 if From_With_Type (Opnd) then
9197 Error_Msg_Qual_Level := 99;
9198 Error_Msg_NE -- CODEFIX
9199 ("missing WITH clause on package &", N,
9200 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9202 ("type conversions require visibility of the full view",
9205 elsif From_With_Type (Target)
9207 (Is_Access_Type (Target_Typ)
9208 and then Present (Non_Limited_View (Etype (Target))))
9210 Error_Msg_Qual_Level := 99;
9211 Error_Msg_NE -- CODEFIX
9212 ("missing WITH clause on package &", N,
9213 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9215 ("type conversions require visibility of the full view",
9219 Expand_Interface_Conversion (N, Is_Static => False);
9222 -- Conversion to interface type
9224 elsif Is_Interface (Target) then
9228 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9229 Opnd := Etype (Opnd);
9232 if not Interface_Present_In_Ancestor
9236 if Is_Class_Wide_Type (Opnd) then
9238 -- The static analysis is not enough to know if the
9239 -- interface is implemented or not. Hence we must pass
9240 -- the work to the expander to generate code to evaluate
9241 -- the conversion at run time.
9243 Expand_Interface_Conversion (N, Is_Static => False);
9246 Error_Msg_Name_1 := Chars (Etype (Target));
9247 Error_Msg_Name_2 := Chars (Opnd);
9249 ("wrong interface conversion (% is not a progenitor " &
9254 Expand_Interface_Conversion (N);
9259 end Resolve_Type_Conversion;
9261 ----------------------
9262 -- Resolve_Unary_Op --
9263 ----------------------
9265 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9266 B_Typ : constant Entity_Id := Base_Type (Typ);
9267 R : constant Node_Id := Right_Opnd (N);
9273 -- Deal with intrinsic unary operators
9275 if Comes_From_Source (N)
9276 and then Ekind (Entity (N)) = E_Function
9277 and then Is_Imported (Entity (N))
9278 and then Is_Intrinsic_Subprogram (Entity (N))
9280 Resolve_Intrinsic_Unary_Operator (N, Typ);
9284 -- Deal with universal cases
9286 if Etype (R) = Universal_Integer
9288 Etype (R) = Universal_Real
9290 Check_For_Visible_Operator (N, B_Typ);
9293 Set_Etype (N, B_Typ);
9296 -- Generate warning for expressions like abs (x mod 2)
9298 if Warn_On_Redundant_Constructs
9299 and then Nkind (N) = N_Op_Abs
9301 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9303 if OK and then Hi >= Lo and then Lo >= 0 then
9304 Error_Msg_N -- CODEFIX
9305 ("?abs applied to known non-negative value has no effect", N);
9309 -- Deal with reference generation
9311 Check_Unset_Reference (R);
9312 Generate_Operator_Reference (N, B_Typ);
9315 -- Set overflow checking bit. Much cleverer code needed here eventually
9316 -- and perhaps the Resolve routines should be separated for the various
9317 -- arithmetic operations, since they will need different processing ???
9319 if Nkind (N) in N_Op then
9320 if not Overflow_Checks_Suppressed (Etype (N)) then
9321 Enable_Overflow_Check (N);
9325 -- Generate warning for expressions like -5 mod 3 for integers. No need
9326 -- to worry in the floating-point case, since parens do not affect the
9327 -- result so there is no point in giving in a warning.
9330 Norig : constant Node_Id := Original_Node (N);
9339 if Warn_On_Questionable_Missing_Parens
9340 and then Comes_From_Source (Norig)
9341 and then Is_Integer_Type (Typ)
9342 and then Nkind (Norig) = N_Op_Minus
9344 Rorig := Original_Node (Right_Opnd (Norig));
9346 -- We are looking for cases where the right operand is not
9347 -- parenthesized, and is a binary operator, multiply, divide, or
9348 -- mod. These are the cases where the grouping can affect results.
9350 if Paren_Count (Rorig) = 0
9351 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9353 -- For mod, we always give the warning, since the value is
9354 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9355 -- -(5 mod 315)). But for the other cases, the only concern is
9356 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9357 -- overflows, but (-2) * 64 does not). So we try to give the
9358 -- message only when overflow is possible.
9360 if Nkind (Rorig) /= N_Op_Mod
9361 and then Compile_Time_Known_Value (R)
9363 Val := Expr_Value (R);
9365 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9366 HB := Expr_Value (Type_High_Bound (Typ));
9368 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9371 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9372 LB := Expr_Value (Type_Low_Bound (Typ));
9374 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9377 -- Note that the test below is deliberately excluding the
9378 -- largest negative number, since that is a potentially
9379 -- troublesome case (e.g. -2 * x, where the result is the
9380 -- largest negative integer has an overflow with 2 * x).
9382 if Val > LB and then Val <= HB then
9387 -- For the multiplication case, the only case we have to worry
9388 -- about is when (-a)*b is exactly the largest negative number
9389 -- so that -(a*b) can cause overflow. This can only happen if
9390 -- a is a power of 2, and more generally if any operand is a
9391 -- constant that is not a power of 2, then the parentheses
9392 -- cannot affect whether overflow occurs. We only bother to
9393 -- test the left most operand
9395 -- Loop looking at left operands for one that has known value
9398 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9399 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9400 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9402 -- Operand value of 0 or 1 skips warning
9407 -- Otherwise check power of 2, if power of 2, warn, if
9408 -- anything else, skip warning.
9411 while Lval /= 2 loop
9412 if Lval mod 2 = 1 then
9423 -- Keep looking at left operands
9425 Opnd := Left_Opnd (Opnd);
9428 -- For rem or "/" we can only have a problematic situation
9429 -- if the divisor has a value of minus one or one. Otherwise
9430 -- overflow is impossible (divisor > 1) or we have a case of
9431 -- division by zero in any case.
9433 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9434 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9435 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9440 -- If we fall through warning should be issued
9443 ("?unary minus expression should be parenthesized here!", N);
9447 end Resolve_Unary_Op;
9449 ----------------------------------
9450 -- Resolve_Unchecked_Expression --
9451 ----------------------------------
9453 procedure Resolve_Unchecked_Expression
9458 Resolve (Expression (N), Typ, Suppress => All_Checks);
9460 end Resolve_Unchecked_Expression;
9462 ---------------------------------------
9463 -- Resolve_Unchecked_Type_Conversion --
9464 ---------------------------------------
9466 procedure Resolve_Unchecked_Type_Conversion
9470 pragma Warnings (Off, Typ);
9472 Operand : constant Node_Id := Expression (N);
9473 Opnd_Type : constant Entity_Id := Etype (Operand);
9476 -- Resolve operand using its own type
9478 Resolve (Operand, Opnd_Type);
9479 Eval_Unchecked_Conversion (N);
9480 end Resolve_Unchecked_Type_Conversion;
9482 ------------------------------
9483 -- Rewrite_Operator_As_Call --
9484 ------------------------------
9486 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9487 Loc : constant Source_Ptr := Sloc (N);
9488 Actuals : constant List_Id := New_List;
9492 if Nkind (N) in N_Binary_Op then
9493 Append (Left_Opnd (N), Actuals);
9496 Append (Right_Opnd (N), Actuals);
9499 Make_Function_Call (Sloc => Loc,
9500 Name => New_Occurrence_Of (Nam, Loc),
9501 Parameter_Associations => Actuals);
9503 Preserve_Comes_From_Source (New_N, N);
9504 Preserve_Comes_From_Source (Name (New_N), N);
9506 Set_Etype (N, Etype (Nam));
9507 end Rewrite_Operator_As_Call;
9509 ------------------------------
9510 -- Rewrite_Renamed_Operator --
9511 ------------------------------
9513 procedure Rewrite_Renamed_Operator
9518 Nam : constant Name_Id := Chars (Op);
9519 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9523 -- Rewrite the operator node using the real operator, not its renaming.
9524 -- Exclude user-defined intrinsic operations of the same name, which are
9525 -- treated separately and rewritten as calls.
9527 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9528 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9529 Set_Chars (Op_Node, Nam);
9530 Set_Etype (Op_Node, Etype (N));
9531 Set_Entity (Op_Node, Op);
9532 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9534 -- Indicate that both the original entity and its renaming are
9535 -- referenced at this point.
9537 Generate_Reference (Entity (N), N);
9538 Generate_Reference (Op, N);
9541 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9544 Rewrite (N, Op_Node);
9546 -- If the context type is private, add the appropriate conversions so
9547 -- that the operator is applied to the full view. This is done in the
9548 -- routines that resolve intrinsic operators.
9550 if Is_Intrinsic_Subprogram (Op)
9551 and then Is_Private_Type (Typ)
9554 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9555 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9556 Resolve_Intrinsic_Operator (N, Typ);
9558 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9559 Resolve_Intrinsic_Unary_Operator (N, Typ);
9566 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9568 -- Operator renames a user-defined operator of the same name. Use the
9569 -- original operator in the node, which is the one Gigi knows about.
9572 Set_Is_Overloaded (N, False);
9574 end Rewrite_Renamed_Operator;
9576 -----------------------
9577 -- Set_Slice_Subtype --
9578 -----------------------
9580 -- Build an implicit subtype declaration to represent the type delivered by
9581 -- the slice. This is an abbreviated version of an array subtype. We define
9582 -- an index subtype for the slice, using either the subtype name or the
9583 -- discrete range of the slice. To be consistent with index usage elsewhere
9584 -- we create a list header to hold the single index. This list is not
9585 -- otherwise attached to the syntax tree.
9587 procedure Set_Slice_Subtype (N : Node_Id) is
9588 Loc : constant Source_Ptr := Sloc (N);
9589 Index_List : constant List_Id := New_List;
9591 Index_Subtype : Entity_Id;
9592 Index_Type : Entity_Id;
9593 Slice_Subtype : Entity_Id;
9594 Drange : constant Node_Id := Discrete_Range (N);
9597 if Is_Entity_Name (Drange) then
9598 Index_Subtype := Entity (Drange);
9601 -- We force the evaluation of a range. This is definitely needed in
9602 -- the renamed case, and seems safer to do unconditionally. Note in
9603 -- any case that since we will create and insert an Itype referring
9604 -- to this range, we must make sure any side effect removal actions
9605 -- are inserted before the Itype definition.
9607 if Nkind (Drange) = N_Range then
9608 Force_Evaluation (Low_Bound (Drange));
9609 Force_Evaluation (High_Bound (Drange));
9612 Index_Type := Base_Type (Etype (Drange));
9614 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9616 -- Take a new copy of Drange (where bounds have been rewritten to
9617 -- reference side-effect-free names). Using a separate tree ensures
9618 -- that further expansion (e.g. while rewriting a slice assignment
9619 -- into a FOR loop) does not attempt to remove side effects on the
9620 -- bounds again (which would cause the bounds in the index subtype
9621 -- definition to refer to temporaries before they are defined) (the
9622 -- reason is that some names are considered side effect free here
9623 -- for the subtype, but not in the context of a loop iteration
9626 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9627 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
9628 Set_Etype (Index_Subtype, Index_Type);
9629 Set_Size_Info (Index_Subtype, Index_Type);
9630 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9633 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9635 Index := New_Occurrence_Of (Index_Subtype, Loc);
9636 Set_Etype (Index, Index_Subtype);
9637 Append (Index, Index_List);
9639 Set_First_Index (Slice_Subtype, Index);
9640 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9641 Set_Is_Constrained (Slice_Subtype, True);
9643 Check_Compile_Time_Size (Slice_Subtype);
9645 -- The Etype of the existing Slice node is reset to this slice subtype.
9646 -- Its bounds are obtained from its first index.
9648 Set_Etype (N, Slice_Subtype);
9650 -- For packed slice subtypes, freeze immediately (except in the
9651 -- case of being in a "spec expression" where we never freeze
9652 -- when we first see the expression).
9654 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9655 Freeze_Itype (Slice_Subtype, N);
9657 -- For all other cases insert an itype reference in the slice's actions
9658 -- so that the itype is frozen at the proper place in the tree (i.e. at
9659 -- the point where actions for the slice are analyzed). Note that this
9660 -- is different from freezing the itype immediately, which might be
9661 -- premature (e.g. if the slice is within a transient scope).
9664 Ensure_Defined (Typ => Slice_Subtype, N => N);
9666 end Set_Slice_Subtype;
9668 --------------------------------
9669 -- Set_String_Literal_Subtype --
9670 --------------------------------
9672 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9673 Loc : constant Source_Ptr := Sloc (N);
9674 Low_Bound : constant Node_Id :=
9675 Type_Low_Bound (Etype (First_Index (Typ)));
9676 Subtype_Id : Entity_Id;
9679 if Nkind (N) /= N_String_Literal then
9683 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
9684 Set_String_Literal_Length (Subtype_Id, UI_From_Int
9685 (String_Length (Strval (N))));
9686 Set_Etype (Subtype_Id, Base_Type (Typ));
9687 Set_Is_Constrained (Subtype_Id);
9688 Set_Etype (N, Subtype_Id);
9690 if Is_OK_Static_Expression (Low_Bound) then
9692 -- The low bound is set from the low bound of the corresponding index
9693 -- type. Note that we do not store the high bound in the string literal
9694 -- subtype, but it can be deduced if necessary from the length and the
9697 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
9700 Set_String_Literal_Low_Bound
9701 (Subtype_Id, Make_Integer_Literal (Loc, 1));
9702 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
9704 -- Build bona fide subtype for the string, and wrap it in an
9705 -- unchecked conversion, because the backend expects the
9706 -- String_Literal_Subtype to have a static lower bound.
9709 Index_List : constant List_Id := New_List;
9710 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
9711 High_Bound : constant Node_Id :=
9713 Left_Opnd => New_Copy_Tree (Low_Bound),
9715 Make_Integer_Literal (Loc,
9716 String_Length (Strval (N)) - 1));
9717 Array_Subtype : Entity_Id;
9718 Index_Subtype : Entity_Id;
9724 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9725 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
9726 Set_Scalar_Range (Index_Subtype, Drange);
9727 Set_Parent (Drange, N);
9728 Analyze_And_Resolve (Drange, Index_Type);
9730 -- In the context, the Index_Type may already have a constraint,
9731 -- so use common base type on string subtype. The base type may
9732 -- be used when generating attributes of the string, for example
9733 -- in the context of a slice assignment.
9735 Set_Etype (Index_Subtype, Base_Type (Index_Type));
9736 Set_Size_Info (Index_Subtype, Index_Type);
9737 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9739 Array_Subtype := Create_Itype (E_Array_Subtype, N);
9741 Index := New_Occurrence_Of (Index_Subtype, Loc);
9742 Set_Etype (Index, Index_Subtype);
9743 Append (Index, Index_List);
9745 Set_First_Index (Array_Subtype, Index);
9746 Set_Etype (Array_Subtype, Base_Type (Typ));
9747 Set_Is_Constrained (Array_Subtype, True);
9750 Make_Unchecked_Type_Conversion (Loc,
9751 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9752 Expression => Relocate_Node (N)));
9753 Set_Etype (N, Array_Subtype);
9756 end Set_String_Literal_Subtype;
9758 ------------------------------
9759 -- Simplify_Type_Conversion --
9760 ------------------------------
9762 procedure Simplify_Type_Conversion (N : Node_Id) is
9764 if Nkind (N) = N_Type_Conversion then
9766 Operand : constant Node_Id := Expression (N);
9767 Target_Typ : constant Entity_Id := Etype (N);
9768 Opnd_Typ : constant Entity_Id := Etype (Operand);
9771 if Is_Floating_Point_Type (Opnd_Typ)
9773 (Is_Integer_Type (Target_Typ)
9774 or else (Is_Fixed_Point_Type (Target_Typ)
9775 and then Conversion_OK (N)))
9776 and then Nkind (Operand) = N_Attribute_Reference
9777 and then Attribute_Name (Operand) = Name_Truncation
9779 -- Special processing required if the conversion is the expression
9780 -- of a Truncation attribute reference. In this case we replace:
9782 -- ityp (ftyp'Truncation (x))
9788 -- with the Float_Truncate flag set, which is more efficient.
9792 Relocate_Node (First (Expressions (Operand))));
9793 Set_Float_Truncate (N, True);
9797 end Simplify_Type_Conversion;
9799 -----------------------------
9800 -- Unique_Fixed_Point_Type --
9801 -----------------------------
9803 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
9804 T1 : Entity_Id := Empty;
9809 procedure Fixed_Point_Error;
9810 -- Give error messages for true ambiguity. Messages are posted on node
9811 -- N, and entities T1, T2 are the possible interpretations.
9813 -----------------------
9814 -- Fixed_Point_Error --
9815 -----------------------
9817 procedure Fixed_Point_Error is
9819 Error_Msg_N ("ambiguous universal_fixed_expression", N);
9820 Error_Msg_NE ("\\possible interpretation as}", N, T1);
9821 Error_Msg_NE ("\\possible interpretation as}", N, T2);
9822 end Fixed_Point_Error;
9824 -- Start of processing for Unique_Fixed_Point_Type
9827 -- The operations on Duration are visible, so Duration is always a
9828 -- possible interpretation.
9830 T1 := Standard_Duration;
9832 -- Look for fixed-point types in enclosing scopes
9834 Scop := Current_Scope;
9835 while Scop /= Standard_Standard loop
9836 T2 := First_Entity (Scop);
9837 while Present (T2) loop
9838 if Is_Fixed_Point_Type (T2)
9839 and then Current_Entity (T2) = T2
9840 and then Scope (Base_Type (T2)) = Scop
9842 if Present (T1) then
9853 Scop := Scope (Scop);
9856 -- Look for visible fixed type declarations in the context
9858 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
9859 while Present (Item) loop
9860 if Nkind (Item) = N_With_Clause then
9861 Scop := Entity (Name (Item));
9862 T2 := First_Entity (Scop);
9863 while Present (T2) loop
9864 if Is_Fixed_Point_Type (T2)
9865 and then Scope (Base_Type (T2)) = Scop
9866 and then (Is_Potentially_Use_Visible (T2)
9867 or else In_Use (T2))
9869 if Present (T1) then
9884 if Nkind (N) = N_Real_Literal then
9885 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
9887 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
9891 end Unique_Fixed_Point_Type;
9893 ----------------------
9894 -- Valid_Conversion --
9895 ----------------------
9897 function Valid_Conversion
9900 Operand : Node_Id) return Boolean
9902 Target_Type : constant Entity_Id := Base_Type (Target);
9903 Opnd_Type : Entity_Id := Etype (Operand);
9905 function Conversion_Check
9907 Msg : String) return Boolean;
9908 -- Little routine to post Msg if Valid is False, returns Valid value
9910 function Valid_Tagged_Conversion
9911 (Target_Type : Entity_Id;
9912 Opnd_Type : Entity_Id) return Boolean;
9913 -- Specifically test for validity of tagged conversions
9915 function Valid_Array_Conversion return Boolean;
9916 -- Check index and component conformance, and accessibility levels if
9917 -- the component types are anonymous access types (Ada 2005).
9919 ----------------------
9920 -- Conversion_Check --
9921 ----------------------
9923 function Conversion_Check
9925 Msg : String) return Boolean
9929 Error_Msg_N (Msg, Operand);
9933 end Conversion_Check;
9935 ----------------------------
9936 -- Valid_Array_Conversion --
9937 ----------------------------
9939 function Valid_Array_Conversion return Boolean
9941 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
9942 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
9944 Opnd_Index : Node_Id;
9945 Opnd_Index_Type : Entity_Id;
9947 Target_Comp_Type : constant Entity_Id :=
9948 Component_Type (Target_Type);
9949 Target_Comp_Base : constant Entity_Id :=
9950 Base_Type (Target_Comp_Type);
9952 Target_Index : Node_Id;
9953 Target_Index_Type : Entity_Id;
9956 -- Error if wrong number of dimensions
9959 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
9962 ("incompatible number of dimensions for conversion", Operand);
9965 -- Number of dimensions matches
9968 -- Loop through indexes of the two arrays
9970 Target_Index := First_Index (Target_Type);
9971 Opnd_Index := First_Index (Opnd_Type);
9972 while Present (Target_Index) and then Present (Opnd_Index) loop
9973 Target_Index_Type := Etype (Target_Index);
9974 Opnd_Index_Type := Etype (Opnd_Index);
9976 -- Error if index types are incompatible
9978 if not (Is_Integer_Type (Target_Index_Type)
9979 and then Is_Integer_Type (Opnd_Index_Type))
9980 and then (Root_Type (Target_Index_Type)
9981 /= Root_Type (Opnd_Index_Type))
9984 ("incompatible index types for array conversion",
9989 Next_Index (Target_Index);
9990 Next_Index (Opnd_Index);
9993 -- If component types have same base type, all set
9995 if Target_Comp_Base = Opnd_Comp_Base then
9998 -- Here if base types of components are not the same. The only
9999 -- time this is allowed is if we have anonymous access types.
10001 -- The conversion of arrays of anonymous access types can lead
10002 -- to dangling pointers. AI-392 formalizes the accessibility
10003 -- checks that must be applied to such conversions to prevent
10004 -- out-of-scope references.
10007 Ekind_In (Target_Comp_Base, E_Anonymous_Access_Type,
10008 E_Anonymous_Access_Subprogram_Type)
10009 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10011 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10013 if Type_Access_Level (Target_Type) <
10014 Type_Access_Level (Opnd_Type)
10016 if In_Instance_Body then
10017 Error_Msg_N ("?source array type " &
10018 "has deeper accessibility level than target", Operand);
10019 Error_Msg_N ("\?Program_Error will be raised at run time",
10022 Make_Raise_Program_Error (Sloc (N),
10023 Reason => PE_Accessibility_Check_Failed));
10024 Set_Etype (N, Target_Type);
10027 -- Conversion not allowed because of accessibility levels
10030 Error_Msg_N ("source array type " &
10031 "has deeper accessibility level than target", Operand);
10038 -- All other cases where component base types do not match
10042 ("incompatible component types for array conversion",
10047 -- Check that component subtypes statically match. For numeric
10048 -- types this means that both must be either constrained or
10049 -- unconstrained. For enumeration types the bounds must match.
10050 -- All of this is checked in Subtypes_Statically_Match.
10052 if not Subtypes_Statically_Match
10053 (Target_Comp_Type, Opnd_Comp_Type)
10056 ("component subtypes must statically match", Operand);
10062 end Valid_Array_Conversion;
10064 -----------------------------
10065 -- Valid_Tagged_Conversion --
10066 -----------------------------
10068 function Valid_Tagged_Conversion
10069 (Target_Type : Entity_Id;
10070 Opnd_Type : Entity_Id) return Boolean
10073 -- Upward conversions are allowed (RM 4.6(22))
10075 if Covers (Target_Type, Opnd_Type)
10076 or else Is_Ancestor (Target_Type, Opnd_Type)
10080 -- Downward conversion are allowed if the operand is class-wide
10083 elsif Is_Class_Wide_Type (Opnd_Type)
10084 and then Covers (Opnd_Type, Target_Type)
10088 elsif Covers (Opnd_Type, Target_Type)
10089 or else Is_Ancestor (Opnd_Type, Target_Type)
10092 Conversion_Check (False,
10093 "downward conversion of tagged objects not allowed");
10095 -- Ada 2005 (AI-251): The conversion to/from interface types is
10098 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10101 -- If the operand is a class-wide type obtained through a limited_
10102 -- with clause, and the context includes the non-limited view, use
10103 -- it to determine whether the conversion is legal.
10105 elsif Is_Class_Wide_Type (Opnd_Type)
10106 and then From_With_Type (Opnd_Type)
10107 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10108 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10112 elsif Is_Access_Type (Opnd_Type)
10113 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10119 ("invalid tagged conversion, not compatible with}",
10120 N, First_Subtype (Opnd_Type));
10123 end Valid_Tagged_Conversion;
10125 -- Start of processing for Valid_Conversion
10128 Check_Parameterless_Call (Operand);
10130 if Is_Overloaded (Operand) then
10140 -- Remove procedure calls, which syntactically cannot appear in
10141 -- this context, but which cannot be removed by type checking,
10142 -- because the context does not impose a type.
10144 -- When compiling for VMS, spurious ambiguities can be produced
10145 -- when arithmetic operations have a literal operand and return
10146 -- System.Address or a descendant of it. These ambiguities are
10147 -- otherwise resolved by the context, but for conversions there
10148 -- is no context type and the removal of the spurious operations
10149 -- must be done explicitly here.
10151 -- The node may be labelled overloaded, but still contain only one
10152 -- interpretation because others were discarded earlier. If this
10153 -- is the case, retain the single interpretation if legal.
10155 Get_First_Interp (Operand, I, It);
10156 Opnd_Type := It.Typ;
10157 Get_Next_Interp (I, It);
10159 if Present (It.Typ)
10160 and then Opnd_Type /= Standard_Void_Type
10162 -- More than one candidate interpretation is available
10164 Get_First_Interp (Operand, I, It);
10165 while Present (It.Typ) loop
10166 if It.Typ = Standard_Void_Type then
10170 if Present (System_Aux_Id)
10171 and then Is_Descendent_Of_Address (It.Typ)
10176 Get_Next_Interp (I, It);
10180 Get_First_Interp (Operand, I, It);
10184 if No (It.Typ) then
10185 Error_Msg_N ("illegal operand in conversion", Operand);
10189 Get_Next_Interp (I, It);
10191 if Present (It.Typ) then
10194 It1 := Disambiguate (Operand, I1, I, Any_Type);
10196 if It1 = No_Interp then
10197 Error_Msg_N ("ambiguous operand in conversion", Operand);
10199 -- If the interpretation involves a standard operator, use
10200 -- the location of the type, which may be user-defined.
10202 if Sloc (It.Nam) = Standard_Location then
10203 Error_Msg_Sloc := Sloc (It.Typ);
10205 Error_Msg_Sloc := Sloc (It.Nam);
10208 Error_Msg_N -- CODEFIX
10209 ("\\possible interpretation#!", Operand);
10211 if Sloc (N1) = Standard_Location then
10212 Error_Msg_Sloc := Sloc (T1);
10214 Error_Msg_Sloc := Sloc (N1);
10217 Error_Msg_N -- CODEFIX
10218 ("\\possible interpretation#!", Operand);
10224 Set_Etype (Operand, It1.Typ);
10225 Opnd_Type := It1.Typ;
10231 if Is_Numeric_Type (Target_Type) then
10233 -- A universal fixed expression can be converted to any numeric type
10235 if Opnd_Type = Universal_Fixed then
10238 -- Also no need to check when in an instance or inlined body, because
10239 -- the legality has been established when the template was analyzed.
10240 -- Furthermore, numeric conversions may occur where only a private
10241 -- view of the operand type is visible at the instantiation point.
10242 -- This results in a spurious error if we check that the operand type
10243 -- is a numeric type.
10245 -- Note: in a previous version of this unit, the following tests were
10246 -- applied only for generated code (Comes_From_Source set to False),
10247 -- but in fact the test is required for source code as well, since
10248 -- this situation can arise in source code.
10250 elsif In_Instance or else In_Inlined_Body then
10253 -- Otherwise we need the conversion check
10256 return Conversion_Check
10257 (Is_Numeric_Type (Opnd_Type),
10258 "illegal operand for numeric conversion");
10263 elsif Is_Array_Type (Target_Type) then
10264 if not Is_Array_Type (Opnd_Type)
10265 or else Opnd_Type = Any_Composite
10266 or else Opnd_Type = Any_String
10268 Error_Msg_N ("illegal operand for array conversion", Operand);
10271 return Valid_Array_Conversion;
10274 -- Ada 2005 (AI-251): Anonymous access types where target references an
10277 elsif Ekind_In (Target_Type, E_General_Access_Type,
10278 E_Anonymous_Access_Type)
10279 and then Is_Interface (Directly_Designated_Type (Target_Type))
10281 -- Check the static accessibility rule of 4.6(17). Note that the
10282 -- check is not enforced when within an instance body, since the
10283 -- RM requires such cases to be caught at run time.
10285 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10286 if Type_Access_Level (Opnd_Type) >
10287 Type_Access_Level (Target_Type)
10289 -- In an instance, this is a run-time check, but one we know
10290 -- will fail, so generate an appropriate warning. The raise
10291 -- will be generated by Expand_N_Type_Conversion.
10293 if In_Instance_Body then
10295 ("?cannot convert local pointer to non-local access type",
10298 ("\?Program_Error will be raised at run time", Operand);
10301 ("cannot convert local pointer to non-local access type",
10306 -- Special accessibility checks are needed in the case of access
10307 -- discriminants declared for a limited type.
10309 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10310 and then not Is_Local_Anonymous_Access (Opnd_Type)
10312 -- When the operand is a selected access discriminant the check
10313 -- needs to be made against the level of the object denoted by
10314 -- the prefix of the selected name (Object_Access_Level handles
10315 -- checking the prefix of the operand for this case).
10317 if Nkind (Operand) = N_Selected_Component
10318 and then Object_Access_Level (Operand) >
10319 Type_Access_Level (Target_Type)
10321 -- In an instance, this is a run-time check, but one we know
10322 -- will fail, so generate an appropriate warning. The raise
10323 -- will be generated by Expand_N_Type_Conversion.
10325 if In_Instance_Body then
10327 ("?cannot convert access discriminant to non-local" &
10328 " access type", Operand);
10330 ("\?Program_Error will be raised at run time", Operand);
10333 ("cannot convert access discriminant to non-local" &
10334 " access type", Operand);
10339 -- The case of a reference to an access discriminant from
10340 -- within a limited type declaration (which will appear as
10341 -- a discriminal) is always illegal because the level of the
10342 -- discriminant is considered to be deeper than any (nameable)
10345 if Is_Entity_Name (Operand)
10346 and then not Is_Local_Anonymous_Access (Opnd_Type)
10348 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10349 and then Present (Discriminal_Link (Entity (Operand)))
10352 ("discriminant has deeper accessibility level than target",
10361 -- General and anonymous access types
10363 elsif Ekind_In (Target_Type, E_General_Access_Type,
10364 E_Anonymous_Access_Type)
10367 (Is_Access_Type (Opnd_Type)
10369 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10370 E_Access_Protected_Subprogram_Type),
10371 "must be an access-to-object type")
10373 if Is_Access_Constant (Opnd_Type)
10374 and then not Is_Access_Constant (Target_Type)
10377 ("access-to-constant operand type not allowed", Operand);
10381 -- Check the static accessibility rule of 4.6(17). Note that the
10382 -- check is not enforced when within an instance body, since the RM
10383 -- requires such cases to be caught at run time.
10385 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10386 or else Is_Local_Anonymous_Access (Target_Type)
10388 if Type_Access_Level (Opnd_Type)
10389 > Type_Access_Level (Target_Type)
10391 -- In an instance, this is a run-time check, but one we know
10392 -- will fail, so generate an appropriate warning. The raise
10393 -- will be generated by Expand_N_Type_Conversion.
10395 if In_Instance_Body then
10397 ("?cannot convert local pointer to non-local access type",
10400 ("\?Program_Error will be raised at run time", Operand);
10403 -- Avoid generation of spurious error message
10405 if not Error_Posted (N) then
10407 ("cannot convert local pointer to non-local access type",
10414 -- Special accessibility checks are needed in the case of access
10415 -- discriminants declared for a limited type.
10417 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10418 and then not Is_Local_Anonymous_Access (Opnd_Type)
10420 -- When the operand is a selected access discriminant the check
10421 -- needs to be made against the level of the object denoted by
10422 -- the prefix of the selected name (Object_Access_Level handles
10423 -- checking the prefix of the operand for this case).
10425 if Nkind (Operand) = N_Selected_Component
10426 and then Object_Access_Level (Operand) >
10427 Type_Access_Level (Target_Type)
10429 -- In an instance, this is a run-time check, but one we know
10430 -- will fail, so generate an appropriate warning. The raise
10431 -- will be generated by Expand_N_Type_Conversion.
10433 if In_Instance_Body then
10435 ("?cannot convert access discriminant to non-local" &
10436 " access type", Operand);
10438 ("\?Program_Error will be raised at run time",
10443 ("cannot convert access discriminant to non-local" &
10444 " access type", Operand);
10449 -- The case of a reference to an access discriminant from
10450 -- within a limited type declaration (which will appear as
10451 -- a discriminal) is always illegal because the level of the
10452 -- discriminant is considered to be deeper than any (nameable)
10455 if Is_Entity_Name (Operand)
10457 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10458 and then Present (Discriminal_Link (Entity (Operand)))
10461 ("discriminant has deeper accessibility level than target",
10468 -- In the presence of limited_with clauses we have to use non-limited
10469 -- views, if available.
10471 Check_Limited : declare
10472 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10473 -- Helper function to handle limited views
10475 --------------------------
10476 -- Full_Designated_Type --
10477 --------------------------
10479 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10480 Desig : constant Entity_Id := Designated_Type (T);
10483 -- Handle the limited view of a type
10485 if Is_Incomplete_Type (Desig)
10486 and then From_With_Type (Desig)
10487 and then Present (Non_Limited_View (Desig))
10489 return Available_View (Desig);
10493 end Full_Designated_Type;
10495 -- Local Declarations
10497 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10498 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10500 Same_Base : constant Boolean :=
10501 Base_Type (Target) = Base_Type (Opnd);
10503 -- Start of processing for Check_Limited
10506 if Is_Tagged_Type (Target) then
10507 return Valid_Tagged_Conversion (Target, Opnd);
10510 if not Same_Base then
10512 ("target designated type not compatible with }",
10513 N, Base_Type (Opnd));
10516 -- Ada 2005 AI-384: legality rule is symmetric in both
10517 -- designated types. The conversion is legal (with possible
10518 -- constraint check) if either designated type is
10521 elsif Subtypes_Statically_Match (Target, Opnd)
10523 (Has_Discriminants (Target)
10525 (not Is_Constrained (Opnd)
10526 or else not Is_Constrained (Target)))
10528 -- Special case, if Value_Size has been used to make the
10529 -- sizes different, the conversion is not allowed even
10530 -- though the subtypes statically match.
10532 if Known_Static_RM_Size (Target)
10533 and then Known_Static_RM_Size (Opnd)
10534 and then RM_Size (Target) /= RM_Size (Opnd)
10537 ("target designated subtype not compatible with }",
10540 ("\because sizes of the two designated subtypes differ",
10544 -- Normal case where conversion is allowed
10552 ("target designated subtype not compatible with }",
10559 -- Access to subprogram types. If the operand is an access parameter,
10560 -- the type has a deeper accessibility that any master, and cannot be
10561 -- assigned. We must make an exception if the conversion is part of an
10562 -- assignment and the target is the return object of an extended return
10563 -- statement, because in that case the accessibility check takes place
10564 -- after the return.
10566 elsif Is_Access_Subprogram_Type (Target_Type)
10567 and then No (Corresponding_Remote_Type (Opnd_Type))
10569 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10570 and then Is_Entity_Name (Operand)
10571 and then Ekind (Entity (Operand)) = E_In_Parameter
10573 (Nkind (Parent (N)) /= N_Assignment_Statement
10574 or else not Is_Entity_Name (Name (Parent (N)))
10575 or else not Is_Return_Object (Entity (Name (Parent (N)))))
10578 ("illegal attempt to store anonymous access to subprogram",
10581 ("\value has deeper accessibility than any master " &
10582 "(RM 3.10.2 (13))",
10586 ("\use named access type for& instead of access parameter",
10587 Operand, Entity (Operand));
10590 -- Check that the designated types are subtype conformant
10592 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
10593 Old_Id => Designated_Type (Opnd_Type),
10596 -- Check the static accessibility rule of 4.6(20)
10598 if Type_Access_Level (Opnd_Type) >
10599 Type_Access_Level (Target_Type)
10602 ("operand type has deeper accessibility level than target",
10605 -- Check that if the operand type is declared in a generic body,
10606 -- then the target type must be declared within that same body
10607 -- (enforces last sentence of 4.6(20)).
10609 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
10611 O_Gen : constant Node_Id :=
10612 Enclosing_Generic_Body (Opnd_Type);
10617 T_Gen := Enclosing_Generic_Body (Target_Type);
10618 while Present (T_Gen) and then T_Gen /= O_Gen loop
10619 T_Gen := Enclosing_Generic_Body (T_Gen);
10622 if T_Gen /= O_Gen then
10624 ("target type must be declared in same generic body"
10625 & " as operand type", N);
10632 -- Remote subprogram access types
10634 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
10635 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
10637 -- It is valid to convert from one RAS type to another provided
10638 -- that their specification statically match.
10640 Check_Subtype_Conformant
10642 Designated_Type (Corresponding_Remote_Type (Target_Type)),
10644 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
10649 -- If both are tagged types, check legality of view conversions
10651 elsif Is_Tagged_Type (Target_Type)
10653 Is_Tagged_Type (Opnd_Type)
10655 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
10657 -- Types derived from the same root type are convertible
10659 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
10662 -- In an instance or an inlined body, there may be inconsistent views of
10663 -- the same type, or of types derived from a common root.
10665 elsif (In_Instance or In_Inlined_Body)
10667 Root_Type (Underlying_Type (Target_Type)) =
10668 Root_Type (Underlying_Type (Opnd_Type))
10672 -- Special check for common access type error case
10674 elsif Ekind (Target_Type) = E_Access_Type
10675 and then Is_Access_Type (Opnd_Type)
10677 Error_Msg_N ("target type must be general access type!", N);
10678 Error_Msg_NE -- CODEFIX
10679 ("add ALL to }!", N, Target_Type);
10683 Error_Msg_NE ("invalid conversion, not compatible with }",
10687 end Valid_Conversion;