1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Output; use Output;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Elab; use Sem_Elab;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Intr; use Sem_Intr;
66 with Sem_Util; use Sem_Util;
67 with Sem_Type; use Sem_Type;
68 with Sem_Warn; use Sem_Warn;
69 with Sinfo; use Sinfo;
70 with Sinfo.CN; use Sinfo.CN;
71 with Snames; use Snames;
72 with Stand; use Stand;
73 with Stringt; use Stringt;
74 with Style; use Style;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
79 package body Sem_Res is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 -- Second pass (top-down) type checking and overload resolution procedures
86 -- Typ is the type required by context. These procedures propagate the type
87 -- information recursively to the descendants of N. If the node is not
88 -- overloaded, its Etype is established in the first pass. If overloaded,
89 -- the Resolve routines set the correct type. For arith. operators, the
90 -- Etype is the base type of the context.
92 -- Note that Resolve_Attribute is separated off in Sem_Attr
94 function Bad_Unordered_Enumeration_Reference
96 T : Entity_Id) return Boolean;
97 -- Node N contains a potentially dubious reference to type T, either an
98 -- explicit comparison, or an explicit range. This function returns True
99 -- if the type T is an enumeration type for which No pragma Order has been
100 -- given, and the reference N is not in the same extended source unit as
101 -- the declaration of T.
103 procedure Check_Discriminant_Use (N : Node_Id);
104 -- Enforce the restrictions on the use of discriminants when constraining
105 -- a component of a discriminated type (record or concurrent type).
107 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
108 -- Given a node for an operator associated with type T, check that
109 -- the operator is visible. Operators all of whose operands are
110 -- universal must be checked for visibility during resolution
111 -- because their type is not determinable based on their operands.
113 procedure Check_Fully_Declared_Prefix
116 -- Check that the type of the prefix of a dereference is not incomplete
118 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
119 -- Given a call node, N, which is known to occur immediately within the
120 -- subprogram being called, determines whether it is a detectable case of
121 -- an infinite recursion, and if so, outputs appropriate messages. Returns
122 -- True if an infinite recursion is detected, and False otherwise.
124 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
125 -- If the type of the object being initialized uses the secondary stack
126 -- directly or indirectly, create a transient scope for the call to the
127 -- init proc. This is because we do not create transient scopes for the
128 -- initialization of individual components within the init proc itself.
129 -- Could be optimized away perhaps?
131 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
132 -- N is the node for a logical operator. If the operator is predefined, and
133 -- the root type of the operands is Standard.Boolean, then a check is made
134 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
135 -- the style check for Style_Check_Boolean_And_Or.
137 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
138 -- Determine whether E is an access type declared by an access declaration,
139 -- and not an (anonymous) allocator type.
141 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
142 -- Utility to check whether the entity for an operator is a predefined
143 -- operator, in which case the expression is left as an operator in the
144 -- tree (else it is rewritten into a call). An instance of an intrinsic
145 -- conversion operation may be given an operator name, but is not treated
146 -- like an operator. Note that an operator that is an imported back-end
147 -- builtin has convention Intrinsic, but is expected to be rewritten into
148 -- a call, so such an operator is not treated as predefined by this
151 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
152 -- If a default expression in entry call N depends on the discriminants
153 -- of the task, it must be replaced with a reference to the discriminant
154 -- of the task being called.
156 procedure Resolve_Op_Concat_Arg
161 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
162 -- concatenation operator. The operand is either of the array type or of
163 -- the component type. If the operand is an aggregate, and the component
164 -- type is composite, this is ambiguous if component type has aggregates.
166 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
167 -- Does the first part of the work of Resolve_Op_Concat
169 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
170 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
171 -- has been resolved. See Resolve_Op_Concat for details.
173 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
174 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
209 function Operator_Kind
211 Is_Binary : Boolean) return Node_Kind;
212 -- Utility to map the name of an operator into the corresponding Node. Used
213 -- by other node rewriting procedures.
215 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
216 -- Resolve actuals of call, and add default expressions for missing ones.
217 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
218 -- called subprogram.
220 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
221 -- Called from Resolve_Call, when the prefix denotes an entry or element
222 -- of entry family. Actuals are resolved as for subprograms, and the node
223 -- is rebuilt as an entry call. Also called for protected operations. Typ
224 -- is the context type, which is used when the operation is a protected
225 -- function with no arguments, and the return value is indexed.
227 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
228 -- A call to a user-defined intrinsic operator is rewritten as a call to
229 -- the corresponding predefined operator, with suitable conversions. Note
230 -- that this applies only for intrinsic operators that denote predefined
231 -- operators, not ones that are intrinsic imports of back-end builtins.
233 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
234 -- Ditto, for unary operators (arithmetic ones and "not" on signed
235 -- integer types for VMS).
237 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
238 -- If an operator node resolves to a call to a user-defined operator,
239 -- rewrite the node as a function call.
241 procedure Make_Call_Into_Operator
245 -- Inverse transformation: if an operator is given in functional notation,
246 -- then after resolving the node, transform into an operator node, so
247 -- that operands are resolved properly. Recall that predefined operators
248 -- do not have a full signature and special resolution rules apply.
250 procedure Rewrite_Renamed_Operator
254 -- An operator can rename another, e.g. in an instantiation. In that
255 -- case, the proper operator node must be constructed and resolved.
257 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
258 -- The String_Literal_Subtype is built for all strings that are not
259 -- operands of a static concatenation operation. If the argument is
260 -- not a N_String_Literal node, then the call has no effect.
262 procedure Set_Slice_Subtype (N : Node_Id);
263 -- Build subtype of array type, with the range specified by the slice
265 procedure Simplify_Type_Conversion (N : Node_Id);
266 -- Called after N has been resolved and evaluated, but before range checks
267 -- have been applied. Currently simplifies a combination of floating-point
268 -- to integer conversion and Truncation attribute.
270 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
271 -- A universal_fixed expression in an universal context is unambiguous if
272 -- there is only one applicable fixed point type. Determining whether there
273 -- is only one requires a search over all visible entities, and happens
274 -- only in very pathological cases (see 6115-006).
276 function Valid_Conversion
279 Operand : Node_Id) return Boolean;
280 -- Verify legality rules given in 4.6 (8-23). Target is the target type
281 -- of the conversion, which may be an implicit conversion of an actual
282 -- parameter to an anonymous access type (in which case N denotes the
283 -- actual parameter and N = Operand).
285 -------------------------
286 -- Ambiguous_Character --
287 -------------------------
289 procedure Ambiguous_Character (C : Node_Id) is
293 if Nkind (C) = N_Character_Literal then
294 Error_Msg_N ("ambiguous character literal", C);
296 -- First the ones in Standard
298 Error_Msg_N ("\\possible interpretation: Character!", C);
299 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
301 -- Include Wide_Wide_Character in Ada 2005 mode
303 if Ada_Version >= Ada_2005 then
304 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
307 -- Now any other types that match
309 E := Current_Entity (C);
310 while Present (E) loop
311 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
315 end Ambiguous_Character;
317 -------------------------
318 -- Analyze_And_Resolve --
319 -------------------------
321 procedure Analyze_And_Resolve (N : Node_Id) is
325 end Analyze_And_Resolve;
327 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
331 end Analyze_And_Resolve;
333 -- Version withs check(s) suppressed
335 procedure Analyze_And_Resolve
340 Scop : constant Entity_Id := Current_Scope;
343 if Suppress = All_Checks then
345 Svg : constant Suppress_Array := Scope_Suppress;
347 Scope_Suppress := (others => True);
348 Analyze_And_Resolve (N, Typ);
349 Scope_Suppress := Svg;
354 Svg : constant Boolean := Scope_Suppress (Suppress);
357 Scope_Suppress (Suppress) := True;
358 Analyze_And_Resolve (N, Typ);
359 Scope_Suppress (Suppress) := Svg;
363 if Current_Scope /= Scop
364 and then Scope_Is_Transient
366 -- This can only happen if a transient scope was created for an inner
367 -- expression, which will be removed upon completion of the analysis
368 -- of an enclosing construct. The transient scope must have the
369 -- suppress status of the enclosing environment, not of this Analyze
372 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
375 end Analyze_And_Resolve;
377 procedure Analyze_And_Resolve
381 Scop : constant Entity_Id := Current_Scope;
384 if Suppress = All_Checks then
386 Svg : constant Suppress_Array := Scope_Suppress;
388 Scope_Suppress := (others => True);
389 Analyze_And_Resolve (N);
390 Scope_Suppress := Svg;
395 Svg : constant Boolean := Scope_Suppress (Suppress);
398 Scope_Suppress (Suppress) := True;
399 Analyze_And_Resolve (N);
400 Scope_Suppress (Suppress) := Svg;
404 if Current_Scope /= Scop
405 and then Scope_Is_Transient
407 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
410 end Analyze_And_Resolve;
412 ----------------------------------------
413 -- Bad_Unordered_Enumeration_Reference --
414 ----------------------------------------
416 function Bad_Unordered_Enumeration_Reference
418 T : Entity_Id) return Boolean
421 return Is_Enumeration_Type (T)
422 and then Comes_From_Source (N)
423 and then Warn_On_Unordered_Enumeration_Type
424 and then not Has_Pragma_Ordered (T)
425 and then not In_Same_Extended_Unit (N, T);
426 end Bad_Unordered_Enumeration_Reference;
428 ----------------------------
429 -- Check_Discriminant_Use --
430 ----------------------------
432 procedure Check_Discriminant_Use (N : Node_Id) is
433 PN : constant Node_Id := Parent (N);
434 Disc : constant Entity_Id := Entity (N);
439 -- Any use in a spec-expression is legal
441 if In_Spec_Expression then
444 elsif Nkind (PN) = N_Range then
446 -- Discriminant cannot be used to constrain a scalar type
450 if Nkind (P) = N_Range_Constraint
451 and then Nkind (Parent (P)) = N_Subtype_Indication
452 and then Nkind (Parent (Parent (P))) = N_Component_Definition
454 Error_Msg_N ("discriminant cannot constrain scalar type", N);
456 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
458 -- The following check catches the unusual case where a
459 -- discriminant appears within an index constraint that is part of
460 -- a larger expression within a constraint on a component, e.g. "C
461 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
462 -- of record components, and note that a similar check should also
463 -- apply in the case of discriminant constraints below. ???
465 -- Note that the check for N_Subtype_Declaration below is to
466 -- detect the valid use of discriminants in the constraints of a
467 -- subtype declaration when this subtype declaration appears
468 -- inside the scope of a record type (which is syntactically
469 -- illegal, but which may be created as part of derived type
470 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
473 if Ekind (Current_Scope) = E_Record_Type
474 and then Scope (Disc) = Current_Scope
476 (Nkind (Parent (P)) = N_Subtype_Indication
478 Nkind_In (Parent (Parent (P)), N_Component_Definition,
479 N_Subtype_Declaration)
480 and then Paren_Count (N) = 0)
483 ("discriminant must appear alone in component constraint", N);
487 -- Detect a common error:
489 -- type R (D : Positive := 100) is record
490 -- Name : String (1 .. D);
493 -- The default value causes an object of type R to be allocated
494 -- with room for Positive'Last characters. The RM does not mandate
495 -- the allocation of the maximum size, but that is what GNAT does
496 -- so we should warn the programmer that there is a problem.
498 Check_Large : declare
504 function Large_Storage_Type (T : Entity_Id) return Boolean;
505 -- Return True if type T has a large enough range that any
506 -- array whose index type covered the whole range of the type
507 -- would likely raise Storage_Error.
509 ------------------------
510 -- Large_Storage_Type --
511 ------------------------
513 function Large_Storage_Type (T : Entity_Id) return Boolean is
515 -- The type is considered large if its bounds are known at
516 -- compile time and if it requires at least as many bits as
517 -- a Positive to store the possible values.
519 return Compile_Time_Known_Value (Type_Low_Bound (T))
520 and then Compile_Time_Known_Value (Type_High_Bound (T))
522 Minimum_Size (T, Biased => True) >=
523 RM_Size (Standard_Positive);
524 end Large_Storage_Type;
526 -- Start of processing for Check_Large
529 -- Check that the Disc has a large range
531 if not Large_Storage_Type (Etype (Disc)) then
535 -- If the enclosing type is limited, we allocate only the
536 -- default value, not the maximum, and there is no need for
539 if Is_Limited_Type (Scope (Disc)) then
543 -- Check that it is the high bound
545 if N /= High_Bound (PN)
546 or else No (Discriminant_Default_Value (Disc))
551 -- Check the array allows a large range at this bound. First
556 if Nkind (SI) /= N_Subtype_Indication then
560 T := Entity (Subtype_Mark (SI));
562 if not Is_Array_Type (T) then
566 -- Next, find the dimension
568 TB := First_Index (T);
569 CB := First (Constraints (P));
571 and then Present (TB)
572 and then Present (CB)
583 -- Now, check the dimension has a large range
585 if not Large_Storage_Type (Etype (TB)) then
589 -- Warn about the danger
592 ("?creation of & object may raise Storage_Error!",
601 -- Legal case is in index or discriminant constraint
603 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
604 N_Discriminant_Association)
606 if Paren_Count (N) > 0 then
608 ("discriminant in constraint must appear alone", N);
610 elsif Nkind (N) = N_Expanded_Name
611 and then Comes_From_Source (N)
614 ("discriminant must appear alone as a direct name", N);
619 -- Otherwise, context is an expression. It should not be within (i.e. a
620 -- subexpression of) a constraint for a component.
625 while not Nkind_In (P, N_Component_Declaration,
626 N_Subtype_Indication,
634 -- If the discriminant is used in an expression that is a bound of a
635 -- scalar type, an Itype is created and the bounds are attached to
636 -- its range, not to the original subtype indication. Such use is of
637 -- course a double fault.
639 if (Nkind (P) = N_Subtype_Indication
640 and then Nkind_In (Parent (P), N_Component_Definition,
641 N_Derived_Type_Definition)
642 and then D = Constraint (P))
644 -- The constraint itself may be given by a subtype indication,
645 -- rather than by a more common discrete range.
647 or else (Nkind (P) = N_Subtype_Indication
649 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
650 or else Nkind (P) = N_Entry_Declaration
651 or else Nkind (D) = N_Defining_Identifier
654 ("discriminant in constraint must appear alone", N);
657 end Check_Discriminant_Use;
659 --------------------------------
660 -- Check_For_Visible_Operator --
661 --------------------------------
663 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
665 if Is_Invisible_Operator (N, T) then
666 Error_Msg_NE -- CODEFIX
667 ("operator for} is not directly visible!", N, First_Subtype (T));
668 Error_Msg_N -- CODEFIX
669 ("use clause would make operation legal!", N);
671 end Check_For_Visible_Operator;
673 ----------------------------------
674 -- Check_Fully_Declared_Prefix --
675 ----------------------------------
677 procedure Check_Fully_Declared_Prefix
682 -- Check that the designated type of the prefix of a dereference is
683 -- not an incomplete type. This cannot be done unconditionally, because
684 -- dereferences of private types are legal in default expressions. This
685 -- case is taken care of in Check_Fully_Declared, called below. There
686 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
688 -- This consideration also applies to similar checks for allocators,
689 -- qualified expressions, and type conversions.
691 -- An additional exception concerns other per-object expressions that
692 -- are not directly related to component declarations, in particular
693 -- representation pragmas for tasks. These will be per-object
694 -- expressions if they depend on discriminants or some global entity.
695 -- If the task has access discriminants, the designated type may be
696 -- incomplete at the point the expression is resolved. This resolution
697 -- takes place within the body of the initialization procedure, where
698 -- the discriminant is replaced by its discriminal.
700 if Is_Entity_Name (Pref)
701 and then Ekind (Entity (Pref)) = E_In_Parameter
705 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
706 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
707 -- Analyze_Object_Renaming, and Freeze_Entity.
709 elsif Ada_Version >= Ada_2005
710 and then Is_Entity_Name (Pref)
711 and then Is_Access_Type (Etype (Pref))
712 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
714 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
718 Check_Fully_Declared (Typ, Parent (Pref));
720 end Check_Fully_Declared_Prefix;
722 ------------------------------
723 -- Check_Infinite_Recursion --
724 ------------------------------
726 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
730 function Same_Argument_List return Boolean;
731 -- Check whether list of actuals is identical to list of formals of
732 -- called function (which is also the enclosing scope).
734 ------------------------
735 -- Same_Argument_List --
736 ------------------------
738 function Same_Argument_List return Boolean is
744 if not Is_Entity_Name (Name (N)) then
747 Subp := Entity (Name (N));
750 F := First_Formal (Subp);
751 A := First_Actual (N);
752 while Present (F) and then Present (A) loop
753 if not Is_Entity_Name (A)
754 or else Entity (A) /= F
764 end Same_Argument_List;
766 -- Start of processing for Check_Infinite_Recursion
769 -- Special case, if this is a procedure call and is a call to the
770 -- current procedure with the same argument list, then this is for
771 -- sure an infinite recursion and we insert a call to raise SE.
773 if Is_List_Member (N)
774 and then List_Length (List_Containing (N)) = 1
775 and then Same_Argument_List
778 P : constant Node_Id := Parent (N);
780 if Nkind (P) = N_Handled_Sequence_Of_Statements
781 and then Nkind (Parent (P)) = N_Subprogram_Body
782 and then Is_Empty_List (Declarations (Parent (P)))
784 Error_Msg_N ("!?infinite recursion", N);
785 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
787 Make_Raise_Storage_Error (Sloc (N),
788 Reason => SE_Infinite_Recursion));
794 -- If not that special case, search up tree, quitting if we reach a
795 -- construct (e.g. a conditional) that tells us that this is not a
796 -- case for an infinite recursion warning.
802 -- If no parent, then we were not inside a subprogram, this can for
803 -- example happen when processing certain pragmas in a spec. Just
804 -- return False in this case.
810 -- Done if we get to subprogram body, this is definitely an infinite
811 -- recursion case if we did not find anything to stop us.
813 exit when Nkind (P) = N_Subprogram_Body;
815 -- If appearing in conditional, result is false
817 if Nkind_In (P, N_Or_Else,
821 N_Conditional_Expression,
826 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
827 and then C /= First (Statements (P))
829 -- If the call is the expression of a return statement and the
830 -- actuals are identical to the formals, it's worth a warning.
831 -- However, we skip this if there is an immediately preceding
832 -- raise statement, since the call is never executed.
834 -- Furthermore, this corresponds to a common idiom:
836 -- function F (L : Thing) return Boolean is
838 -- raise Program_Error;
842 -- for generating a stub function
844 if Nkind (Parent (N)) = N_Simple_Return_Statement
845 and then Same_Argument_List
847 exit when not Is_List_Member (Parent (N));
849 -- OK, return statement is in a statement list, look for raise
855 -- Skip past N_Freeze_Entity nodes generated by expansion
857 Nod := Prev (Parent (N));
859 and then Nkind (Nod) = N_Freeze_Entity
864 -- If no raise statement, give warning
866 exit when Nkind (Nod) /= N_Raise_Statement
868 (Nkind (Nod) not in N_Raise_xxx_Error
869 or else Present (Condition (Nod)));
880 Error_Msg_N ("!?possible infinite recursion", N);
881 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
884 end Check_Infinite_Recursion;
886 -------------------------------
887 -- Check_Initialization_Call --
888 -------------------------------
890 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
891 Typ : constant Entity_Id := Etype (First_Formal (Nam));
893 function Uses_SS (T : Entity_Id) return Boolean;
894 -- Check whether the creation of an object of the type will involve
895 -- use of the secondary stack. If T is a record type, this is true
896 -- if the expression for some component uses the secondary stack, e.g.
897 -- through a call to a function that returns an unconstrained value.
898 -- False if T is controlled, because cleanups occur elsewhere.
904 function Uses_SS (T : Entity_Id) return Boolean is
907 Full_Type : Entity_Id := Underlying_Type (T);
910 -- Normally we want to use the underlying type, but if it's not set
911 -- then continue with T.
913 if not Present (Full_Type) then
917 if Is_Controlled (Full_Type) then
920 elsif Is_Array_Type (Full_Type) then
921 return Uses_SS (Component_Type (Full_Type));
923 elsif Is_Record_Type (Full_Type) then
924 Comp := First_Component (Full_Type);
925 while Present (Comp) loop
926 if Ekind (Comp) = E_Component
927 and then Nkind (Parent (Comp)) = N_Component_Declaration
929 -- The expression for a dynamic component may be rewritten
930 -- as a dereference, so retrieve original node.
932 Expr := Original_Node (Expression (Parent (Comp)));
934 -- Return True if the expression is a call to a function
935 -- (including an attribute function such as Image, or a
936 -- user-defined operator) with a result that requires a
939 if (Nkind (Expr) = N_Function_Call
940 or else Nkind (Expr) in N_Op
941 or else (Nkind (Expr) = N_Attribute_Reference
942 and then Present (Expressions (Expr))))
943 and then Requires_Transient_Scope (Etype (Expr))
947 elsif Uses_SS (Etype (Comp)) then
952 Next_Component (Comp);
962 -- Start of processing for Check_Initialization_Call
965 -- Establish a transient scope if the type needs it
967 if Uses_SS (Typ) then
968 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
970 end Check_Initialization_Call;
972 ---------------------------------------
973 -- Check_No_Direct_Boolean_Operators --
974 ---------------------------------------
976 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
978 if Scope (Entity (N)) = Standard_Standard
979 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
981 -- Restriction only applies to original source code
983 if Comes_From_Source (N) then
984 Check_Restriction (No_Direct_Boolean_Operators, N);
989 Check_Boolean_Operator (N);
991 end Check_No_Direct_Boolean_Operators;
993 ------------------------------
994 -- Check_Parameterless_Call --
995 ------------------------------
997 procedure Check_Parameterless_Call (N : Node_Id) is
1000 function Prefix_Is_Access_Subp return Boolean;
1001 -- If the prefix is of an access_to_subprogram type, the node must be
1002 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1003 -- interpretations are access to subprograms.
1005 ---------------------------
1006 -- Prefix_Is_Access_Subp --
1007 ---------------------------
1009 function Prefix_Is_Access_Subp return Boolean is
1014 -- If the context is an attribute reference that can apply to
1015 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1017 if Nkind (Parent (N)) = N_Attribute_Reference
1018 and then (Attribute_Name (Parent (N)) = Name_Address or else
1019 Attribute_Name (Parent (N)) = Name_Code_Address or else
1020 Attribute_Name (Parent (N)) = Name_Access)
1025 if not Is_Overloaded (N) then
1027 Ekind (Etype (N)) = E_Subprogram_Type
1028 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1030 Get_First_Interp (N, I, It);
1031 while Present (It.Typ) loop
1032 if Ekind (It.Typ) /= E_Subprogram_Type
1033 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1038 Get_Next_Interp (I, It);
1043 end Prefix_Is_Access_Subp;
1045 -- Start of processing for Check_Parameterless_Call
1048 -- Defend against junk stuff if errors already detected
1050 if Total_Errors_Detected /= 0 then
1051 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1053 elsif Nkind (N) in N_Has_Chars
1054 and then Chars (N) in Error_Name_Or_No_Name
1062 -- If the context expects a value, and the name is a procedure, this is
1063 -- most likely a missing 'Access. Don't try to resolve the parameterless
1064 -- call, error will be caught when the outer call is analyzed.
1066 if Is_Entity_Name (N)
1067 and then Ekind (Entity (N)) = E_Procedure
1068 and then not Is_Overloaded (N)
1070 Nkind_In (Parent (N), N_Parameter_Association,
1072 N_Procedure_Call_Statement)
1077 -- Rewrite as call if overloadable entity that is (or could be, in the
1078 -- overloaded case) a function call. If we know for sure that the entity
1079 -- is an enumeration literal, we do not rewrite it.
1081 -- If the entity is the name of an operator, it cannot be a call because
1082 -- operators cannot have default parameters. In this case, this must be
1083 -- a string whose contents coincide with an operator name. Set the kind
1084 -- of the node appropriately.
1086 if (Is_Entity_Name (N)
1087 and then Nkind (N) /= N_Operator_Symbol
1088 and then Is_Overloadable (Entity (N))
1089 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1090 or else Is_Overloaded (N)))
1092 -- Rewrite as call if it is an explicit dereference of an expression of
1093 -- a subprogram access type, and the subprogram type is not that of a
1094 -- procedure or entry.
1097 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1099 -- Rewrite as call if it is a selected component which is a function,
1100 -- this is the case of a call to a protected function (which may be
1101 -- overloaded with other protected operations).
1104 (Nkind (N) = N_Selected_Component
1105 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1107 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1109 and then Is_Overloaded (Selector_Name (N)))))
1111 -- If one of the above three conditions is met, rewrite as call. Apply
1112 -- the rewriting only once.
1115 if Nkind (Parent (N)) /= N_Function_Call
1116 or else N /= Name (Parent (N))
1118 Nam := New_Copy (N);
1120 -- If overloaded, overload set belongs to new copy
1122 Save_Interps (N, Nam);
1124 -- Change node to parameterless function call (note that the
1125 -- Parameter_Associations associations field is left set to Empty,
1126 -- its normal default value since there are no parameters)
1128 Change_Node (N, N_Function_Call);
1130 Set_Sloc (N, Sloc (Nam));
1134 elsif Nkind (N) = N_Parameter_Association then
1135 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1137 elsif Nkind (N) = N_Operator_Symbol then
1138 Change_Operator_Symbol_To_String_Literal (N);
1139 Set_Is_Overloaded (N, False);
1140 Set_Etype (N, Any_String);
1142 end Check_Parameterless_Call;
1144 -----------------------------
1145 -- Is_Definite_Access_Type --
1146 -----------------------------
1148 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1149 Btyp : constant Entity_Id := Base_Type (E);
1151 return Ekind (Btyp) = E_Access_Type
1152 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1153 and then Comes_From_Source (Btyp));
1154 end Is_Definite_Access_Type;
1156 ----------------------
1157 -- Is_Predefined_Op --
1158 ----------------------
1160 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1162 -- Predefined operators are intrinsic subprograms
1164 if not Is_Intrinsic_Subprogram (Nam) then
1168 -- A call to a back-end builtin is never a predefined operator
1170 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1174 return not Is_Generic_Instance (Nam)
1175 and then Chars (Nam) in Any_Operator_Name
1176 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1177 end Is_Predefined_Op;
1179 -----------------------------
1180 -- Make_Call_Into_Operator --
1181 -----------------------------
1183 procedure Make_Call_Into_Operator
1188 Op_Name : constant Name_Id := Chars (Op_Id);
1189 Act1 : Node_Id := First_Actual (N);
1190 Act2 : Node_Id := Next_Actual (Act1);
1191 Error : Boolean := False;
1192 Func : constant Entity_Id := Entity (Name (N));
1193 Is_Binary : constant Boolean := Present (Act2);
1195 Opnd_Type : Entity_Id;
1196 Orig_Type : Entity_Id := Empty;
1199 type Kind_Test is access function (E : Entity_Id) return Boolean;
1201 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1202 -- If the operand is not universal, and the operator is given by an
1203 -- expanded name, verify that the operand has an interpretation with a
1204 -- type defined in the given scope of the operator.
1206 function Type_In_P (Test : Kind_Test) return Entity_Id;
1207 -- Find a type of the given class in package Pack that contains the
1210 ---------------------------
1211 -- Operand_Type_In_Scope --
1212 ---------------------------
1214 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1215 Nod : constant Node_Id := Right_Opnd (Op_Node);
1220 if not Is_Overloaded (Nod) then
1221 return Scope (Base_Type (Etype (Nod))) = S;
1224 Get_First_Interp (Nod, I, It);
1225 while Present (It.Typ) loop
1226 if Scope (Base_Type (It.Typ)) = S then
1230 Get_Next_Interp (I, It);
1235 end Operand_Type_In_Scope;
1241 function Type_In_P (Test : Kind_Test) return Entity_Id is
1244 function In_Decl return Boolean;
1245 -- Verify that node is not part of the type declaration for the
1246 -- candidate type, which would otherwise be invisible.
1252 function In_Decl return Boolean is
1253 Decl_Node : constant Node_Id := Parent (E);
1259 if Etype (E) = Any_Type then
1262 elsif No (Decl_Node) then
1267 and then Nkind (N2) /= N_Compilation_Unit
1269 if N2 = Decl_Node then
1280 -- Start of processing for Type_In_P
1283 -- If the context type is declared in the prefix package, this is the
1284 -- desired base type.
1286 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1287 return Base_Type (Typ);
1290 E := First_Entity (Pack);
1291 while Present (E) loop
1293 and then not In_Decl
1305 -- Start of processing for Make_Call_Into_Operator
1308 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1313 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1314 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1315 Save_Interps (Act1, Left_Opnd (Op_Node));
1316 Save_Interps (Act2, Right_Opnd (Op_Node));
1317 Act1 := Left_Opnd (Op_Node);
1318 Act2 := Right_Opnd (Op_Node);
1323 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1324 Save_Interps (Act1, Right_Opnd (Op_Node));
1325 Act1 := Right_Opnd (Op_Node);
1328 -- If the operator is denoted by an expanded name, and the prefix is
1329 -- not Standard, but the operator is a predefined one whose scope is
1330 -- Standard, then this is an implicit_operator, inserted as an
1331 -- interpretation by the procedure of the same name. This procedure
1332 -- overestimates the presence of implicit operators, because it does
1333 -- not examine the type of the operands. Verify now that the operand
1334 -- type appears in the given scope. If right operand is universal,
1335 -- check the other operand. In the case of concatenation, either
1336 -- argument can be the component type, so check the type of the result.
1337 -- If both arguments are literals, look for a type of the right kind
1338 -- defined in the given scope. This elaborate nonsense is brought to
1339 -- you courtesy of b33302a. The type itself must be frozen, so we must
1340 -- find the type of the proper class in the given scope.
1342 -- A final wrinkle is the multiplication operator for fixed point types,
1343 -- which is defined in Standard only, and not in the scope of the
1344 -- fixed point type itself.
1346 if Nkind (Name (N)) = N_Expanded_Name then
1347 Pack := Entity (Prefix (Name (N)));
1349 -- If the entity being called is defined in the given package, it is
1350 -- a renaming of a predefined operator, and known to be legal.
1352 if Scope (Entity (Name (N))) = Pack
1353 and then Pack /= Standard_Standard
1357 -- Visibility does not need to be checked in an instance: if the
1358 -- operator was not visible in the generic it has been diagnosed
1359 -- already, else there is an implicit copy of it in the instance.
1361 elsif In_Instance then
1364 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1365 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1366 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1368 if Pack /= Standard_Standard then
1372 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1375 elsif Ada_Version >= Ada_2005
1376 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1377 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1382 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1384 if Op_Name = Name_Op_Concat then
1385 Opnd_Type := Base_Type (Typ);
1387 elsif (Scope (Opnd_Type) = Standard_Standard
1389 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1391 and then not Comes_From_Source (Opnd_Type))
1393 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1396 if Scope (Opnd_Type) = Standard_Standard then
1398 -- Verify that the scope contains a type that corresponds to
1399 -- the given literal. Optimize the case where Pack is Standard.
1401 if Pack /= Standard_Standard then
1403 if Opnd_Type = Universal_Integer then
1404 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1406 elsif Opnd_Type = Universal_Real then
1407 Orig_Type := Type_In_P (Is_Real_Type'Access);
1409 elsif Opnd_Type = Any_String then
1410 Orig_Type := Type_In_P (Is_String_Type'Access);
1412 elsif Opnd_Type = Any_Access then
1413 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1415 elsif Opnd_Type = Any_Composite then
1416 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1418 if Present (Orig_Type) then
1419 if Has_Private_Component (Orig_Type) then
1422 Set_Etype (Act1, Orig_Type);
1425 Set_Etype (Act2, Orig_Type);
1434 Error := No (Orig_Type);
1437 elsif Ekind (Opnd_Type) = E_Allocator_Type
1438 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1442 -- If the type is defined elsewhere, and the operator is not
1443 -- defined in the given scope (by a renaming declaration, e.g.)
1444 -- then this is an error as well. If an extension of System is
1445 -- present, and the type may be defined there, Pack must be
1448 elsif Scope (Opnd_Type) /= Pack
1449 and then Scope (Op_Id) /= Pack
1450 and then (No (System_Aux_Id)
1451 or else Scope (Opnd_Type) /= System_Aux_Id
1452 or else Pack /= Scope (System_Aux_Id))
1454 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1457 Error := not Operand_Type_In_Scope (Pack);
1460 elsif Pack = Standard_Standard
1461 and then not Operand_Type_In_Scope (Standard_Standard)
1468 Error_Msg_Node_2 := Pack;
1470 ("& not declared in&", N, Selector_Name (Name (N)));
1471 Set_Etype (N, Any_Type);
1474 -- Detect a mismatch between the context type and the result type
1475 -- in the named package, which is otherwise not detected if the
1476 -- operands are universal. Check is only needed if source entity is
1477 -- an operator, not a function that renames an operator.
1479 elsif Nkind (Parent (N)) /= N_Type_Conversion
1480 and then Ekind (Entity (Name (N))) = E_Operator
1481 and then Is_Numeric_Type (Typ)
1482 and then not Is_Universal_Numeric_Type (Typ)
1483 and then Scope (Base_Type (Typ)) /= Pack
1484 and then not In_Instance
1486 if Is_Fixed_Point_Type (Typ)
1487 and then (Op_Name = Name_Op_Multiply
1489 Op_Name = Name_Op_Divide)
1491 -- Already checked above
1495 -- Operator may be defined in an extension of System
1497 elsif Present (System_Aux_Id)
1498 and then Scope (Opnd_Type) = System_Aux_Id
1503 -- Could we use Wrong_Type here??? (this would require setting
1504 -- Etype (N) to the actual type found where Typ was expected).
1506 Error_Msg_NE ("expect }", N, Typ);
1511 Set_Chars (Op_Node, Op_Name);
1513 if not Is_Private_Type (Etype (N)) then
1514 Set_Etype (Op_Node, Base_Type (Etype (N)));
1516 Set_Etype (Op_Node, Etype (N));
1519 -- If this is a call to a function that renames a predefined equality,
1520 -- the renaming declaration provides a type that must be used to
1521 -- resolve the operands. This must be done now because resolution of
1522 -- the equality node will not resolve any remaining ambiguity, and it
1523 -- assumes that the first operand is not overloaded.
1525 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1526 and then Ekind (Func) = E_Function
1527 and then Is_Overloaded (Act1)
1529 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1530 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1533 Set_Entity (Op_Node, Op_Id);
1534 Generate_Reference (Op_Id, N, ' ');
1536 -- Do rewrite setting Comes_From_Source on the result if the original
1537 -- call came from source. Although it is not strictly the case that the
1538 -- operator as such comes from the source, logically it corresponds
1539 -- exactly to the function call in the source, so it should be marked
1540 -- this way (e.g. to make sure that validity checks work fine).
1543 CS : constant Boolean := Comes_From_Source (N);
1545 Rewrite (N, Op_Node);
1546 Set_Comes_From_Source (N, CS);
1549 -- If this is an arithmetic operator and the result type is private,
1550 -- the operands and the result must be wrapped in conversion to
1551 -- expose the underlying numeric type and expand the proper checks,
1552 -- e.g. on division.
1554 if Is_Private_Type (Typ) then
1556 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1557 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1558 Resolve_Intrinsic_Operator (N, Typ);
1560 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1561 Resolve_Intrinsic_Unary_Operator (N, Typ);
1569 end Make_Call_Into_Operator;
1575 function Operator_Kind
1577 Is_Binary : Boolean) return Node_Kind
1582 -- Use CASE statement or array???
1585 if Op_Name = Name_Op_And then
1587 elsif Op_Name = Name_Op_Or then
1589 elsif Op_Name = Name_Op_Xor then
1591 elsif Op_Name = Name_Op_Eq then
1593 elsif Op_Name = Name_Op_Ne then
1595 elsif Op_Name = Name_Op_Lt then
1597 elsif Op_Name = Name_Op_Le then
1599 elsif Op_Name = Name_Op_Gt then
1601 elsif Op_Name = Name_Op_Ge then
1603 elsif Op_Name = Name_Op_Add then
1605 elsif Op_Name = Name_Op_Subtract then
1606 Kind := N_Op_Subtract;
1607 elsif Op_Name = Name_Op_Concat then
1608 Kind := N_Op_Concat;
1609 elsif Op_Name = Name_Op_Multiply then
1610 Kind := N_Op_Multiply;
1611 elsif Op_Name = Name_Op_Divide then
1612 Kind := N_Op_Divide;
1613 elsif Op_Name = Name_Op_Mod then
1615 elsif Op_Name = Name_Op_Rem then
1617 elsif Op_Name = Name_Op_Expon then
1620 raise Program_Error;
1626 if Op_Name = Name_Op_Add then
1628 elsif Op_Name = Name_Op_Subtract then
1630 elsif Op_Name = Name_Op_Abs then
1632 elsif Op_Name = Name_Op_Not then
1635 raise Program_Error;
1642 ----------------------------
1643 -- Preanalyze_And_Resolve --
1644 ----------------------------
1646 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1647 Save_Full_Analysis : constant Boolean := Full_Analysis;
1650 Full_Analysis := False;
1651 Expander_Mode_Save_And_Set (False);
1653 -- We suppress all checks for this analysis, since the checks will
1654 -- be applied properly, and in the right location, when the default
1655 -- expression is reanalyzed and reexpanded later on.
1657 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1659 Expander_Mode_Restore;
1660 Full_Analysis := Save_Full_Analysis;
1661 end Preanalyze_And_Resolve;
1663 -- Version without context type
1665 procedure Preanalyze_And_Resolve (N : Node_Id) is
1666 Save_Full_Analysis : constant Boolean := Full_Analysis;
1669 Full_Analysis := False;
1670 Expander_Mode_Save_And_Set (False);
1673 Resolve (N, Etype (N), Suppress => All_Checks);
1675 Expander_Mode_Restore;
1676 Full_Analysis := Save_Full_Analysis;
1677 end Preanalyze_And_Resolve;
1679 ----------------------------------
1680 -- Replace_Actual_Discriminants --
1681 ----------------------------------
1683 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1684 Loc : constant Source_Ptr := Sloc (N);
1685 Tsk : Node_Id := Empty;
1687 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1693 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1697 if Nkind (Nod) = N_Identifier then
1698 Ent := Entity (Nod);
1701 and then Ekind (Ent) = E_Discriminant
1704 Make_Selected_Component (Loc,
1705 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1706 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1708 Set_Etype (Nod, Etype (Ent));
1716 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1718 -- Start of processing for Replace_Actual_Discriminants
1721 if not Expander_Active then
1725 if Nkind (Name (N)) = N_Selected_Component then
1726 Tsk := Prefix (Name (N));
1728 elsif Nkind (Name (N)) = N_Indexed_Component then
1729 Tsk := Prefix (Prefix (Name (N)));
1735 Replace_Discrs (Default);
1737 end Replace_Actual_Discriminants;
1743 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1744 Ambiguous : Boolean := False;
1745 Ctx_Type : Entity_Id := Typ;
1746 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1747 Err_Type : Entity_Id := Empty;
1748 Found : Boolean := False;
1751 I1 : Interp_Index := 0; -- prevent junk warning
1754 Seen : Entity_Id := Empty; -- prevent junk warning
1756 procedure Build_Explicit_Dereference
1759 -- AI05-139: Names with implicit dereference. If the expression N is a
1760 -- reference type and the context imposes the corresponding designated
1761 -- type, convert N into N.Disc.all. Such expressions are always over-
1762 -- loaded with both interpretations, and the dereference interpretation
1763 -- carries the name of the reference discriminant.
1765 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1766 -- Determine whether a node comes from a predefined library unit or
1769 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1770 -- Try and fix up a literal so that it matches its expected type. New
1771 -- literals are manufactured if necessary to avoid cascaded errors.
1773 procedure Report_Ambiguous_Argument;
1774 -- Additional diagnostics when an ambiguous call has an ambiguous
1775 -- argument (typically a controlling actual).
1777 procedure Resolution_Failed;
1778 -- Called when attempt at resolving current expression fails
1780 --------------------------------
1781 -- Build_Explicit_Dereference --
1782 --------------------------------
1784 procedure Build_Explicit_Dereference
1788 Loc : constant Source_Ptr := Sloc (Expr);
1791 Set_Is_Overloaded (Expr, False);
1793 Make_Explicit_Dereference (Loc,
1795 Make_Selected_Component (Loc,
1796 Prefix => Relocate_Node (Expr),
1798 New_Occurrence_Of (Disc, Loc))));
1800 Set_Etype (Prefix (Expr), Etype (Disc));
1801 Set_Etype (Expr, Typ);
1802 end Build_Explicit_Dereference;
1804 ------------------------------------
1805 -- Comes_From_Predefined_Lib_Unit --
1806 -------------------------------------
1808 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1811 Sloc (Nod) = Standard_Location
1812 or else Is_Predefined_File_Name
1813 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1814 end Comes_From_Predefined_Lib_Unit;
1816 --------------------
1817 -- Patch_Up_Value --
1818 --------------------
1820 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1822 if Nkind (N) = N_Integer_Literal
1823 and then Is_Real_Type (Typ)
1826 Make_Real_Literal (Sloc (N),
1827 Realval => UR_From_Uint (Intval (N))));
1828 Set_Etype (N, Universal_Real);
1829 Set_Is_Static_Expression (N);
1831 elsif Nkind (N) = N_Real_Literal
1832 and then Is_Integer_Type (Typ)
1835 Make_Integer_Literal (Sloc (N),
1836 Intval => UR_To_Uint (Realval (N))));
1837 Set_Etype (N, Universal_Integer);
1838 Set_Is_Static_Expression (N);
1840 elsif Nkind (N) = N_String_Literal
1841 and then Is_Character_Type (Typ)
1843 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1845 Make_Character_Literal (Sloc (N),
1847 Char_Literal_Value =>
1848 UI_From_Int (Character'Pos ('A'))));
1849 Set_Etype (N, Any_Character);
1850 Set_Is_Static_Expression (N);
1852 elsif Nkind (N) /= N_String_Literal
1853 and then Is_String_Type (Typ)
1856 Make_String_Literal (Sloc (N),
1857 Strval => End_String));
1859 elsif Nkind (N) = N_Range then
1860 Patch_Up_Value (Low_Bound (N), Typ);
1861 Patch_Up_Value (High_Bound (N), Typ);
1865 -------------------------------
1866 -- Report_Ambiguous_Argument --
1867 -------------------------------
1869 procedure Report_Ambiguous_Argument is
1870 Arg : constant Node_Id := First (Parameter_Associations (N));
1875 if Nkind (Arg) = N_Function_Call
1876 and then Is_Entity_Name (Name (Arg))
1877 and then Is_Overloaded (Name (Arg))
1879 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1881 -- Could use comments on what is going on here ???
1883 Get_First_Interp (Name (Arg), I, It);
1884 while Present (It.Nam) loop
1885 Error_Msg_Sloc := Sloc (It.Nam);
1887 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1888 Error_Msg_N ("interpretation (inherited) #!", Arg);
1890 Error_Msg_N ("interpretation #!", Arg);
1893 Get_Next_Interp (I, It);
1896 end Report_Ambiguous_Argument;
1898 -----------------------
1899 -- Resolution_Failed --
1900 -----------------------
1902 procedure Resolution_Failed is
1904 Patch_Up_Value (N, Typ);
1906 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1907 Set_Is_Overloaded (N, False);
1909 -- The caller will return without calling the expander, so we need
1910 -- to set the analyzed flag. Note that it is fine to set Analyzed
1911 -- to True even if we are in the middle of a shallow analysis,
1912 -- (see the spec of sem for more details) since this is an error
1913 -- situation anyway, and there is no point in repeating the
1914 -- analysis later (indeed it won't work to repeat it later, since
1915 -- we haven't got a clear resolution of which entity is being
1918 Set_Analyzed (N, True);
1920 end Resolution_Failed;
1922 -- Start of processing for Resolve
1929 -- Access attribute on remote subprogram cannot be used for
1930 -- a non-remote access-to-subprogram type.
1932 if Nkind (N) = N_Attribute_Reference
1933 and then (Attribute_Name (N) = Name_Access or else
1934 Attribute_Name (N) = Name_Unrestricted_Access or else
1935 Attribute_Name (N) = Name_Unchecked_Access)
1936 and then Comes_From_Source (N)
1937 and then Is_Entity_Name (Prefix (N))
1938 and then Is_Subprogram (Entity (Prefix (N)))
1939 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1940 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1943 ("prefix must statically denote a non-remote subprogram", N);
1946 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1948 -- If the context is a Remote_Access_To_Subprogram, access attributes
1949 -- must be resolved with the corresponding fat pointer. There is no need
1950 -- to check for the attribute name since the return type of an
1951 -- attribute is never a remote type.
1953 if Nkind (N) = N_Attribute_Reference
1954 and then Comes_From_Source (N)
1955 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1958 Attr : constant Attribute_Id :=
1959 Get_Attribute_Id (Attribute_Name (N));
1960 Pref : constant Node_Id := Prefix (N);
1963 Is_Remote : Boolean := True;
1966 -- Check that Typ is a remote access-to-subprogram type
1968 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1970 -- Prefix (N) must statically denote a remote subprogram
1971 -- declared in a package specification.
1973 if Attr = Attribute_Access then
1974 Decl := Unit_Declaration_Node (Entity (Pref));
1976 if Nkind (Decl) = N_Subprogram_Body then
1977 Spec := Corresponding_Spec (Decl);
1979 if not No (Spec) then
1980 Decl := Unit_Declaration_Node (Spec);
1984 Spec := Parent (Decl);
1986 if not Is_Entity_Name (Prefix (N))
1987 or else Nkind (Spec) /= N_Package_Specification
1989 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1993 ("prefix must statically denote a remote subprogram ",
1998 -- If we are generating code for a distributed program.
1999 -- perform semantic checks against the corresponding
2002 if (Attr = Attribute_Access or else
2003 Attr = Attribute_Unchecked_Access or else
2004 Attr = Attribute_Unrestricted_Access)
2005 and then Expander_Active
2006 and then Get_PCS_Name /= Name_No_DSA
2008 Check_Subtype_Conformant
2009 (New_Id => Entity (Prefix (N)),
2010 Old_Id => Designated_Type
2011 (Corresponding_Remote_Type (Typ)),
2015 Process_Remote_AST_Attribute (N, Typ);
2022 Debug_A_Entry ("resolving ", N);
2024 if Comes_From_Source (N) then
2025 if Is_Fixed_Point_Type (Typ) then
2026 Check_Restriction (No_Fixed_Point, N);
2028 elsif Is_Floating_Point_Type (Typ)
2029 and then Typ /= Universal_Real
2030 and then Typ /= Any_Real
2032 Check_Restriction (No_Floating_Point, N);
2036 -- Return if already analyzed
2038 if Analyzed (N) then
2039 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2042 -- Return if type = Any_Type (previous error encountered)
2044 elsif Etype (N) = Any_Type then
2045 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2049 Check_Parameterless_Call (N);
2051 -- If not overloaded, then we know the type, and all that needs doing
2052 -- is to check that this type is compatible with the context.
2054 if not Is_Overloaded (N) then
2055 Found := Covers (Typ, Etype (N));
2056 Expr_Type := Etype (N);
2058 -- In the overloaded case, we must select the interpretation that
2059 -- is compatible with the context (i.e. the type passed to Resolve)
2062 -- Loop through possible interpretations
2064 Get_First_Interp (N, I, It);
2065 Interp_Loop : while Present (It.Typ) loop
2067 -- We are only interested in interpretations that are compatible
2068 -- with the expected type, any other interpretations are ignored.
2070 if not Covers (Typ, It.Typ) then
2071 if Debug_Flag_V then
2072 Write_Str (" interpretation incompatible with context");
2077 -- Skip the current interpretation if it is disabled by an
2078 -- abstract operator. This action is performed only when the
2079 -- type against which we are resolving is the same as the
2080 -- type of the interpretation.
2082 if Ada_Version >= Ada_2005
2083 and then It.Typ = Typ
2084 and then Typ /= Universal_Integer
2085 and then Typ /= Universal_Real
2086 and then Present (It.Abstract_Op)
2091 -- First matching interpretation
2097 Expr_Type := It.Typ;
2099 -- Matching interpretation that is not the first, maybe an
2100 -- error, but there are some cases where preference rules are
2101 -- used to choose between the two possibilities. These and
2102 -- some more obscure cases are handled in Disambiguate.
2105 -- If the current statement is part of a predefined library
2106 -- unit, then all interpretations which come from user level
2107 -- packages should not be considered.
2110 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2115 Error_Msg_Sloc := Sloc (Seen);
2116 It1 := Disambiguate (N, I1, I, Typ);
2118 -- Disambiguation has succeeded. Skip the remaining
2121 if It1 /= No_Interp then
2123 Expr_Type := It1.Typ;
2125 while Present (It.Typ) loop
2126 Get_Next_Interp (I, It);
2130 -- Before we issue an ambiguity complaint, check for
2131 -- the case of a subprogram call where at least one
2132 -- of the arguments is Any_Type, and if so, suppress
2133 -- the message, since it is a cascaded error.
2135 if Nkind_In (N, N_Function_Call,
2136 N_Procedure_Call_Statement)
2143 A := First_Actual (N);
2144 while Present (A) loop
2147 if Nkind (E) = N_Parameter_Association then
2148 E := Explicit_Actual_Parameter (E);
2151 if Etype (E) = Any_Type then
2152 if Debug_Flag_V then
2153 Write_Str ("Any_Type in call");
2164 elsif Nkind (N) in N_Binary_Op
2165 and then (Etype (Left_Opnd (N)) = Any_Type
2166 or else Etype (Right_Opnd (N)) = Any_Type)
2170 elsif Nkind (N) in N_Unary_Op
2171 and then Etype (Right_Opnd (N)) = Any_Type
2176 -- Not that special case, so issue message using the
2177 -- flag Ambiguous to control printing of the header
2178 -- message only at the start of an ambiguous set.
2180 if not Ambiguous then
2181 if Nkind (N) = N_Function_Call
2182 and then Nkind (Name (N)) = N_Explicit_Dereference
2185 ("ambiguous expression "
2186 & "(cannot resolve indirect call)!", N);
2188 Error_Msg_NE -- CODEFIX
2189 ("ambiguous expression (cannot resolve&)!",
2195 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2197 ("\\possible interpretation (inherited)#!", N);
2199 Error_Msg_N -- CODEFIX
2200 ("\\possible interpretation#!", N);
2204 (N, N_Procedure_Call_Statement, N_Function_Call)
2205 and then Present (Parameter_Associations (N))
2207 Report_Ambiguous_Argument;
2211 Error_Msg_Sloc := Sloc (It.Nam);
2213 -- By default, the error message refers to the candidate
2214 -- interpretation. But if it is a predefined operator, it
2215 -- is implicitly declared at the declaration of the type
2216 -- of the operand. Recover the sloc of that declaration
2217 -- for the error message.
2219 if Nkind (N) in N_Op
2220 and then Scope (It.Nam) = Standard_Standard
2221 and then not Is_Overloaded (Right_Opnd (N))
2222 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2225 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2227 if Comes_From_Source (Err_Type)
2228 and then Present (Parent (Err_Type))
2230 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2233 elsif Nkind (N) in N_Binary_Op
2234 and then Scope (It.Nam) = Standard_Standard
2235 and then not Is_Overloaded (Left_Opnd (N))
2236 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2239 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2241 if Comes_From_Source (Err_Type)
2242 and then Present (Parent (Err_Type))
2244 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2247 -- If this is an indirect call, use the subprogram_type
2248 -- in the message, to have a meaningful location. Also
2249 -- indicate if this is an inherited operation, created
2250 -- by a type declaration.
2252 elsif Nkind (N) = N_Function_Call
2253 and then Nkind (Name (N)) = N_Explicit_Dereference
2254 and then Is_Type (It.Nam)
2258 Sloc (Associated_Node_For_Itype (Err_Type));
2263 if Nkind (N) in N_Op
2264 and then Scope (It.Nam) = Standard_Standard
2265 and then Present (Err_Type)
2267 -- Special-case the message for universal_fixed
2268 -- operators, which are not declared with the type
2269 -- of the operand, but appear forever in Standard.
2271 if It.Typ = Universal_Fixed
2272 and then Scope (It.Nam) = Standard_Standard
2275 ("\\possible interpretation as " &
2276 "universal_fixed operation " &
2277 "(RM 4.5.5 (19))", N);
2280 ("\\possible interpretation (predefined)#!", N);
2284 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2287 ("\\possible interpretation (inherited)#!", N);
2289 Error_Msg_N -- CODEFIX
2290 ("\\possible interpretation#!", N);
2296 -- We have a matching interpretation, Expr_Type is the type
2297 -- from this interpretation, and Seen is the entity.
2299 -- For an operator, just set the entity name. The type will be
2300 -- set by the specific operator resolution routine.
2302 if Nkind (N) in N_Op then
2303 Set_Entity (N, Seen);
2304 Generate_Reference (Seen, N);
2306 elsif Nkind (N) = N_Case_Expression then
2307 Set_Etype (N, Expr_Type);
2309 elsif Nkind (N) = N_Character_Literal then
2310 Set_Etype (N, Expr_Type);
2312 elsif Nkind (N) = N_Conditional_Expression then
2313 Set_Etype (N, Expr_Type);
2315 -- AI05-0139-2: Expression is overloaded because type has
2316 -- implicit dereference. If type matches context, no implicit
2317 -- dereference is involved.
2319 elsif Has_Implicit_Dereference (Expr_Type) then
2320 Set_Etype (N, Expr_Type);
2321 Set_Is_Overloaded (N, False);
2324 elsif Is_Overloaded (N)
2325 and then Present (It.Nam)
2326 and then Ekind (It.Nam) = E_Discriminant
2327 and then Has_Implicit_Dereference (It.Nam)
2329 Build_Explicit_Dereference (N, It.Nam);
2331 -- For an explicit dereference, attribute reference, range,
2332 -- short-circuit form (which is not an operator node), or call
2333 -- with a name that is an explicit dereference, there is
2334 -- nothing to be done at this point.
2336 elsif Nkind_In (N, N_Explicit_Dereference,
2337 N_Attribute_Reference,
2339 N_Indexed_Component,
2342 N_Selected_Component,
2344 or else Nkind (Name (N)) = N_Explicit_Dereference
2348 -- For procedure or function calls, set the type of the name,
2349 -- and also the entity pointer for the prefix.
2351 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2352 and then Is_Entity_Name (Name (N))
2354 Set_Etype (Name (N), Expr_Type);
2355 Set_Entity (Name (N), Seen);
2356 Generate_Reference (Seen, Name (N));
2358 elsif Nkind (N) = N_Function_Call
2359 and then Nkind (Name (N)) = N_Selected_Component
2361 Set_Etype (Name (N), Expr_Type);
2362 Set_Entity (Selector_Name (Name (N)), Seen);
2363 Generate_Reference (Seen, Selector_Name (Name (N)));
2365 -- For all other cases, just set the type of the Name
2368 Set_Etype (Name (N), Expr_Type);
2375 -- Move to next interpretation
2377 exit Interp_Loop when No (It.Typ);
2379 Get_Next_Interp (I, It);
2380 end loop Interp_Loop;
2383 -- At this stage Found indicates whether or not an acceptable
2384 -- interpretation exists. If not, then we have an error, except that if
2385 -- the context is Any_Type as a result of some other error, then we
2386 -- suppress the error report.
2389 if Typ /= Any_Type then
2391 -- If type we are looking for is Void, then this is the procedure
2392 -- call case, and the error is simply that what we gave is not a
2393 -- procedure name (we think of procedure calls as expressions with
2394 -- types internally, but the user doesn't think of them this way!)
2396 if Typ = Standard_Void_Type then
2398 -- Special case message if function used as a procedure
2400 if Nkind (N) = N_Procedure_Call_Statement
2401 and then Is_Entity_Name (Name (N))
2402 and then Ekind (Entity (Name (N))) = E_Function
2405 ("cannot use function & in a procedure call",
2406 Name (N), Entity (Name (N)));
2408 -- Otherwise give general message (not clear what cases this
2409 -- covers, but no harm in providing for them!)
2412 Error_Msg_N ("expect procedure name in procedure call", N);
2417 -- Otherwise we do have a subexpression with the wrong type
2419 -- Check for the case of an allocator which uses an access type
2420 -- instead of the designated type. This is a common error and we
2421 -- specialize the message, posting an error on the operand of the
2422 -- allocator, complaining that we expected the designated type of
2425 elsif Nkind (N) = N_Allocator
2426 and then Ekind (Typ) in Access_Kind
2427 and then Ekind (Etype (N)) in Access_Kind
2428 and then Designated_Type (Etype (N)) = Typ
2430 Wrong_Type (Expression (N), Designated_Type (Typ));
2433 -- Check for view mismatch on Null in instances, for which the
2434 -- view-swapping mechanism has no identifier.
2436 elsif (In_Instance or else In_Inlined_Body)
2437 and then (Nkind (N) = N_Null)
2438 and then Is_Private_Type (Typ)
2439 and then Is_Access_Type (Full_View (Typ))
2441 Resolve (N, Full_View (Typ));
2445 -- Check for an aggregate. Sometimes we can get bogus aggregates
2446 -- from misuse of parentheses, and we are about to complain about
2447 -- the aggregate without even looking inside it.
2449 -- Instead, if we have an aggregate of type Any_Composite, then
2450 -- analyze and resolve the component fields, and then only issue
2451 -- another message if we get no errors doing this (otherwise
2452 -- assume that the errors in the aggregate caused the problem).
2454 elsif Nkind (N) = N_Aggregate
2455 and then Etype (N) = Any_Composite
2457 -- Disable expansion in any case. If there is a type mismatch
2458 -- it may be fatal to try to expand the aggregate. The flag
2459 -- would otherwise be set to false when the error is posted.
2461 Expander_Active := False;
2464 procedure Check_Aggr (Aggr : Node_Id);
2465 -- Check one aggregate, and set Found to True if we have a
2466 -- definite error in any of its elements
2468 procedure Check_Elmt (Aelmt : Node_Id);
2469 -- Check one element of aggregate and set Found to True if
2470 -- we definitely have an error in the element.
2476 procedure Check_Aggr (Aggr : Node_Id) is
2480 if Present (Expressions (Aggr)) then
2481 Elmt := First (Expressions (Aggr));
2482 while Present (Elmt) loop
2488 if Present (Component_Associations (Aggr)) then
2489 Elmt := First (Component_Associations (Aggr));
2490 while Present (Elmt) loop
2492 -- If this is a default-initialized component, then
2493 -- there is nothing to check. The box will be
2494 -- replaced by the appropriate call during late
2497 if not Box_Present (Elmt) then
2498 Check_Elmt (Expression (Elmt));
2510 procedure Check_Elmt (Aelmt : Node_Id) is
2512 -- If we have a nested aggregate, go inside it (to
2513 -- attempt a naked analyze-resolve of the aggregate can
2514 -- cause undesirable cascaded errors). Do not resolve
2515 -- expression if it needs a type from context, as for
2516 -- integer * fixed expression.
2518 if Nkind (Aelmt) = N_Aggregate then
2524 if not Is_Overloaded (Aelmt)
2525 and then Etype (Aelmt) /= Any_Fixed
2530 if Etype (Aelmt) = Any_Type then
2541 -- If an error message was issued already, Found got reset to
2542 -- True, so if it is still False, issue standard Wrong_Type msg.
2545 if Is_Overloaded (N)
2546 and then Nkind (N) = N_Function_Call
2549 Subp_Name : Node_Id;
2551 if Is_Entity_Name (Name (N)) then
2552 Subp_Name := Name (N);
2554 elsif Nkind (Name (N)) = N_Selected_Component then
2556 -- Protected operation: retrieve operation name
2558 Subp_Name := Selector_Name (Name (N));
2561 raise Program_Error;
2564 Error_Msg_Node_2 := Typ;
2565 Error_Msg_NE ("no visible interpretation of&" &
2566 " matches expected type&", N, Subp_Name);
2569 if All_Errors_Mode then
2571 Index : Interp_Index;
2575 Error_Msg_N ("\\possible interpretations:", N);
2577 Get_First_Interp (Name (N), Index, It);
2578 while Present (It.Nam) loop
2579 Error_Msg_Sloc := Sloc (It.Nam);
2580 Error_Msg_Node_2 := It.Nam;
2582 ("\\ type& for & declared#", N, It.Typ);
2583 Get_Next_Interp (Index, It);
2588 Error_Msg_N ("\use -gnatf for details", N);
2592 Wrong_Type (N, Typ);
2600 -- Test if we have more than one interpretation for the context
2602 elsif Ambiguous then
2606 -- Here we have an acceptable interpretation for the context
2609 -- Propagate type information and normalize tree for various
2610 -- predefined operations. If the context only imposes a class of
2611 -- types, rather than a specific type, propagate the actual type
2614 if Typ = Any_Integer or else
2615 Typ = Any_Boolean or else
2616 Typ = Any_Modular or else
2617 Typ = Any_Real or else
2620 Ctx_Type := Expr_Type;
2622 -- Any_Fixed is legal in a real context only if a specific fixed-
2623 -- point type is imposed. If Norman Cohen can be confused by this,
2624 -- it deserves a separate message.
2627 and then Expr_Type = Any_Fixed
2629 Error_Msg_N ("illegal context for mixed mode operation", N);
2630 Set_Etype (N, Universal_Real);
2631 Ctx_Type := Universal_Real;
2635 -- A user-defined operator is transformed into a function call at
2636 -- this point, so that further processing knows that operators are
2637 -- really operators (i.e. are predefined operators). User-defined
2638 -- operators that are intrinsic are just renamings of the predefined
2639 -- ones, and need not be turned into calls either, but if they rename
2640 -- a different operator, we must transform the node accordingly.
2641 -- Instantiations of Unchecked_Conversion are intrinsic but are
2642 -- treated as functions, even if given an operator designator.
2644 if Nkind (N) in N_Op
2645 and then Present (Entity (N))
2646 and then Ekind (Entity (N)) /= E_Operator
2649 if not Is_Predefined_Op (Entity (N)) then
2650 Rewrite_Operator_As_Call (N, Entity (N));
2652 elsif Present (Alias (Entity (N)))
2654 Nkind (Parent (Parent (Entity (N)))) =
2655 N_Subprogram_Renaming_Declaration
2657 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2659 -- If the node is rewritten, it will be fully resolved in
2660 -- Rewrite_Renamed_Operator.
2662 if Analyzed (N) then
2668 case N_Subexpr'(Nkind (N)) is
2670 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2672 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2674 when N_Short_Circuit
2675 => Resolve_Short_Circuit (N, Ctx_Type);
2677 when N_Attribute_Reference
2678 => Resolve_Attribute (N, Ctx_Type);
2680 when N_Case_Expression
2681 => Resolve_Case_Expression (N, Ctx_Type);
2683 when N_Character_Literal
2684 => Resolve_Character_Literal (N, Ctx_Type);
2686 when N_Conditional_Expression
2687 => Resolve_Conditional_Expression (N, Ctx_Type);
2689 when N_Expanded_Name
2690 => Resolve_Entity_Name (N, Ctx_Type);
2692 when N_Explicit_Dereference
2693 => Resolve_Explicit_Dereference (N, Ctx_Type);
2695 when N_Expression_With_Actions
2696 => Resolve_Expression_With_Actions (N, Ctx_Type);
2698 when N_Extension_Aggregate
2699 => Resolve_Extension_Aggregate (N, Ctx_Type);
2701 when N_Function_Call
2702 => Resolve_Call (N, Ctx_Type);
2705 => Resolve_Entity_Name (N, Ctx_Type);
2707 when N_Indexed_Component
2708 => Resolve_Indexed_Component (N, Ctx_Type);
2710 when N_Integer_Literal
2711 => Resolve_Integer_Literal (N, Ctx_Type);
2713 when N_Membership_Test
2714 => Resolve_Membership_Op (N, Ctx_Type);
2716 when N_Null => Resolve_Null (N, Ctx_Type);
2718 when N_Op_And | N_Op_Or | N_Op_Xor
2719 => Resolve_Logical_Op (N, Ctx_Type);
2721 when N_Op_Eq | N_Op_Ne
2722 => Resolve_Equality_Op (N, Ctx_Type);
2724 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2725 => Resolve_Comparison_Op (N, Ctx_Type);
2727 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2729 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2730 N_Op_Divide | N_Op_Mod | N_Op_Rem
2732 => Resolve_Arithmetic_Op (N, Ctx_Type);
2734 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2736 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2738 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2739 => Resolve_Unary_Op (N, Ctx_Type);
2741 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2743 when N_Procedure_Call_Statement
2744 => Resolve_Call (N, Ctx_Type);
2746 when N_Operator_Symbol
2747 => Resolve_Operator_Symbol (N, Ctx_Type);
2749 when N_Qualified_Expression
2750 => Resolve_Qualified_Expression (N, Ctx_Type);
2752 when N_Quantified_Expression
2753 => Resolve_Quantified_Expression (N, Ctx_Type);
2755 when N_Raise_xxx_Error
2756 => Set_Etype (N, Ctx_Type);
2758 when N_Range => Resolve_Range (N, Ctx_Type);
2761 => Resolve_Real_Literal (N, Ctx_Type);
2763 when N_Reference => Resolve_Reference (N, Ctx_Type);
2765 when N_Selected_Component
2766 => Resolve_Selected_Component (N, Ctx_Type);
2768 when N_Slice => Resolve_Slice (N, Ctx_Type);
2770 when N_String_Literal
2771 => Resolve_String_Literal (N, Ctx_Type);
2773 when N_Subprogram_Info
2774 => Resolve_Subprogram_Info (N, Ctx_Type);
2776 when N_Type_Conversion
2777 => Resolve_Type_Conversion (N, Ctx_Type);
2779 when N_Unchecked_Expression =>
2780 Resolve_Unchecked_Expression (N, Ctx_Type);
2782 when N_Unchecked_Type_Conversion =>
2783 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2786 -- If the subexpression was replaced by a non-subexpression, then
2787 -- all we do is to expand it. The only legitimate case we know of
2788 -- is converting procedure call statement to entry call statements,
2789 -- but there may be others, so we are making this test general.
2791 if Nkind (N) not in N_Subexpr then
2792 Debug_A_Exit ("resolving ", N, " (done)");
2797 -- AI05-144-2: Check dangerous order dependence within an expression
2798 -- that is not a subexpression. Exclude RHS of an assignment, because
2799 -- both sides may have side-effects and the check must be performed
2800 -- over the statement.
2802 if Nkind (Parent (N)) not in N_Subexpr
2803 and then Nkind (Parent (N)) /= N_Assignment_Statement
2804 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2806 Check_Order_Dependence;
2809 -- The expression is definitely NOT overloaded at this point, so
2810 -- we reset the Is_Overloaded flag to avoid any confusion when
2811 -- reanalyzing the node.
2813 Set_Is_Overloaded (N, False);
2815 -- Freeze expression type, entity if it is a name, and designated
2816 -- type if it is an allocator (RM 13.14(10,11,13)).
2818 -- Now that the resolution of the type of the node is complete, and
2819 -- we did not detect an error, we can expand this node. We skip the
2820 -- expand call if we are in a default expression, see section
2821 -- "Handling of Default Expressions" in Sem spec.
2823 Debug_A_Exit ("resolving ", N, " (done)");
2825 -- We unconditionally freeze the expression, even if we are in
2826 -- default expression mode (the Freeze_Expression routine tests this
2827 -- flag and only freezes static types if it is set).
2829 Freeze_Expression (N);
2831 -- Now we can do the expansion
2841 -- Version with check(s) suppressed
2843 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2845 if Suppress = All_Checks then
2847 Svg : constant Suppress_Array := Scope_Suppress;
2849 Scope_Suppress := (others => True);
2851 Scope_Suppress := Svg;
2856 Svg : constant Boolean := Scope_Suppress (Suppress);
2858 Scope_Suppress (Suppress) := True;
2860 Scope_Suppress (Suppress) := Svg;
2869 -- Version with implicit type
2871 procedure Resolve (N : Node_Id) is
2873 Resolve (N, Etype (N));
2876 ---------------------
2877 -- Resolve_Actuals --
2878 ---------------------
2880 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2881 Loc : constant Source_Ptr := Sloc (N);
2886 Prev : Node_Id := Empty;
2889 procedure Check_Argument_Order;
2890 -- Performs a check for the case where the actuals are all simple
2891 -- identifiers that correspond to the formal names, but in the wrong
2892 -- order, which is considered suspicious and cause for a warning.
2894 procedure Check_Prefixed_Call;
2895 -- If the original node is an overloaded call in prefix notation,
2896 -- insert an 'Access or a dereference as needed over the first actual.
2897 -- Try_Object_Operation has already verified that there is a valid
2898 -- interpretation, but the form of the actual can only be determined
2899 -- once the primitive operation is identified.
2901 procedure Insert_Default;
2902 -- If the actual is missing in a call, insert in the actuals list
2903 -- an instance of the default expression. The insertion is always
2904 -- a named association.
2906 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2907 -- Check whether T1 and T2, or their full views, are derived from a
2908 -- common type. Used to enforce the restrictions on array conversions
2911 function Static_Concatenation (N : Node_Id) return Boolean;
2912 -- Predicate to determine whether an actual that is a concatenation
2913 -- will be evaluated statically and does not need a transient scope.
2914 -- This must be determined before the actual is resolved and expanded
2915 -- because if needed the transient scope must be introduced earlier.
2917 --------------------------
2918 -- Check_Argument_Order --
2919 --------------------------
2921 procedure Check_Argument_Order is
2923 -- Nothing to do if no parameters, or original node is neither a
2924 -- function call nor a procedure call statement (happens in the
2925 -- operator-transformed-to-function call case), or the call does
2926 -- not come from source, or this warning is off.
2928 if not Warn_On_Parameter_Order
2929 or else No (Parameter_Associations (N))
2930 or else not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2932 or else not Comes_From_Source (N)
2938 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2941 -- Nothing to do if only one parameter
2947 -- Here if at least two arguments
2950 Actuals : array (1 .. Nargs) of Node_Id;
2954 Wrong_Order : Boolean := False;
2955 -- Set True if an out of order case is found
2958 -- Collect identifier names of actuals, fail if any actual is
2959 -- not a simple identifier, and record max length of name.
2961 Actual := First (Parameter_Associations (N));
2962 for J in Actuals'Range loop
2963 if Nkind (Actual) /= N_Identifier then
2966 Actuals (J) := Actual;
2971 -- If we got this far, all actuals are identifiers and the list
2972 -- of their names is stored in the Actuals array.
2974 Formal := First_Formal (Nam);
2975 for J in Actuals'Range loop
2977 -- If we ran out of formals, that's odd, probably an error
2978 -- which will be detected elsewhere, but abandon the search.
2984 -- If name matches and is in order OK
2986 if Chars (Formal) = Chars (Actuals (J)) then
2990 -- If no match, see if it is elsewhere in list and if so
2991 -- flag potential wrong order if type is compatible.
2993 for K in Actuals'Range loop
2994 if Chars (Formal) = Chars (Actuals (K))
2996 Has_Compatible_Type (Actuals (K), Etype (Formal))
2998 Wrong_Order := True;
3008 <<Continue>> Next_Formal (Formal);
3011 -- If Formals left over, also probably an error, skip warning
3013 if Present (Formal) then
3017 -- Here we give the warning if something was out of order
3021 ("actuals for this call may be in wrong order?", N);
3025 end Check_Argument_Order;
3027 -------------------------
3028 -- Check_Prefixed_Call --
3029 -------------------------
3031 procedure Check_Prefixed_Call is
3032 Act : constant Node_Id := First_Actual (N);
3033 A_Type : constant Entity_Id := Etype (Act);
3034 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3035 Orig : constant Node_Id := Original_Node (N);
3039 -- Check whether the call is a prefixed call, with or without
3040 -- additional actuals.
3042 if Nkind (Orig) = N_Selected_Component
3044 (Nkind (Orig) = N_Indexed_Component
3045 and then Nkind (Prefix (Orig)) = N_Selected_Component
3046 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3047 and then Is_Entity_Name (Act)
3048 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3050 if Is_Access_Type (A_Type)
3051 and then not Is_Access_Type (F_Type)
3053 -- Introduce dereference on object in prefix
3056 Make_Explicit_Dereference (Sloc (Act),
3057 Prefix => Relocate_Node (Act));
3058 Rewrite (Act, New_A);
3061 elsif Is_Access_Type (F_Type)
3062 and then not Is_Access_Type (A_Type)
3064 -- Introduce an implicit 'Access in prefix
3066 if not Is_Aliased_View (Act) then
3068 ("object in prefixed call to& must be aliased"
3069 & " (RM-2005 4.3.1 (13))",
3074 Make_Attribute_Reference (Loc,
3075 Attribute_Name => Name_Access,
3076 Prefix => Relocate_Node (Act)));
3081 end Check_Prefixed_Call;
3083 --------------------
3084 -- Insert_Default --
3085 --------------------
3087 procedure Insert_Default is
3092 -- Missing argument in call, nothing to insert
3094 if No (Default_Value (F)) then
3098 -- Note that we do a full New_Copy_Tree, so that any associated
3099 -- Itypes are properly copied. This may not be needed any more,
3100 -- but it does no harm as a safety measure! Defaults of a generic
3101 -- formal may be out of bounds of the corresponding actual (see
3102 -- cc1311b) and an additional check may be required.
3107 New_Scope => Current_Scope,
3110 if Is_Concurrent_Type (Scope (Nam))
3111 and then Has_Discriminants (Scope (Nam))
3113 Replace_Actual_Discriminants (N, Actval);
3116 if Is_Overloadable (Nam)
3117 and then Present (Alias (Nam))
3119 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3120 and then not Is_Tagged_Type (Etype (F))
3122 -- If default is a real literal, do not introduce a
3123 -- conversion whose effect may depend on the run-time
3124 -- size of universal real.
3126 if Nkind (Actval) = N_Real_Literal then
3127 Set_Etype (Actval, Base_Type (Etype (F)));
3129 Actval := Unchecked_Convert_To (Etype (F), Actval);
3133 if Is_Scalar_Type (Etype (F)) then
3134 Enable_Range_Check (Actval);
3137 Set_Parent (Actval, N);
3139 -- Resolve aggregates with their base type, to avoid scope
3140 -- anomalies: the subtype was first built in the subprogram
3141 -- declaration, and the current call may be nested.
3143 if Nkind (Actval) = N_Aggregate then
3144 Analyze_And_Resolve (Actval, Etype (F));
3146 Analyze_And_Resolve (Actval, Etype (Actval));
3150 Set_Parent (Actval, N);
3152 -- See note above concerning aggregates
3154 if Nkind (Actval) = N_Aggregate
3155 and then Has_Discriminants (Etype (Actval))
3157 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3159 -- Resolve entities with their own type, which may differ from
3160 -- the type of a reference in a generic context (the view
3161 -- swapping mechanism did not anticipate the re-analysis of
3162 -- default values in calls).
3164 elsif Is_Entity_Name (Actval) then
3165 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3168 Analyze_And_Resolve (Actval, Etype (Actval));
3172 -- If default is a tag indeterminate function call, propagate tag
3173 -- to obtain proper dispatching.
3175 if Is_Controlling_Formal (F)
3176 and then Nkind (Default_Value (F)) = N_Function_Call
3178 Set_Is_Controlling_Actual (Actval);
3183 -- If the default expression raises constraint error, then just
3184 -- silently replace it with an N_Raise_Constraint_Error node, since
3185 -- we already gave the warning on the subprogram spec. If node is
3186 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3187 -- the warnings removal machinery.
3189 if Raises_Constraint_Error (Actval)
3190 and then Nkind (Actval) /= N_Raise_Constraint_Error
3193 Make_Raise_Constraint_Error (Loc,
3194 Reason => CE_Range_Check_Failed));
3195 Set_Raises_Constraint_Error (Actval);
3196 Set_Etype (Actval, Etype (F));
3200 Make_Parameter_Association (Loc,
3201 Explicit_Actual_Parameter => Actval,
3202 Selector_Name => Make_Identifier (Loc, Chars (F)));
3204 -- Case of insertion is first named actual
3206 if No (Prev) or else
3207 Nkind (Parent (Prev)) /= N_Parameter_Association
3209 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3210 Set_First_Named_Actual (N, Actval);
3213 if No (Parameter_Associations (N)) then
3214 Set_Parameter_Associations (N, New_List (Assoc));
3216 Append (Assoc, Parameter_Associations (N));
3220 Insert_After (Prev, Assoc);
3223 -- Case of insertion is not first named actual
3226 Set_Next_Named_Actual
3227 (Assoc, Next_Named_Actual (Parent (Prev)));
3228 Set_Next_Named_Actual (Parent (Prev), Actval);
3229 Append (Assoc, Parameter_Associations (N));
3232 Mark_Rewrite_Insertion (Assoc);
3233 Mark_Rewrite_Insertion (Actval);
3242 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3243 FT1 : Entity_Id := T1;
3244 FT2 : Entity_Id := T2;
3247 if Is_Private_Type (T1)
3248 and then Present (Full_View (T1))
3250 FT1 := Full_View (T1);
3253 if Is_Private_Type (T2)
3254 and then Present (Full_View (T2))
3256 FT2 := Full_View (T2);
3259 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3262 --------------------------
3263 -- Static_Concatenation --
3264 --------------------------
3266 function Static_Concatenation (N : Node_Id) return Boolean is
3269 when N_String_Literal =>
3274 -- Concatenation is static when both operands are static and
3275 -- the concatenation operator is a predefined one.
3277 return Scope (Entity (N)) = Standard_Standard
3279 Static_Concatenation (Left_Opnd (N))
3281 Static_Concatenation (Right_Opnd (N));
3284 if Is_Entity_Name (N) then
3286 Ent : constant Entity_Id := Entity (N);
3288 return Ekind (Ent) = E_Constant
3289 and then Present (Constant_Value (Ent))
3291 Is_Static_Expression (Constant_Value (Ent));
3298 end Static_Concatenation;
3300 -- Start of processing for Resolve_Actuals
3303 Check_Argument_Order;
3305 if Present (First_Actual (N)) then
3306 Check_Prefixed_Call;
3309 A := First_Actual (N);
3310 F := First_Formal (Nam);
3311 while Present (F) loop
3312 if No (A) and then Needs_No_Actuals (Nam) then
3315 -- If we have an error in any actual or formal, indicated by a type
3316 -- of Any_Type, then abandon resolution attempt, and set result type
3319 elsif (Present (A) and then Etype (A) = Any_Type)
3320 or else Etype (F) = Any_Type
3322 Set_Etype (N, Any_Type);
3326 -- Case where actual is present
3328 -- If the actual is an entity, generate a reference to it now. We
3329 -- do this before the actual is resolved, because a formal of some
3330 -- protected subprogram, or a task discriminant, will be rewritten
3331 -- during expansion, and the source entity reference may be lost.
3334 and then Is_Entity_Name (A)
3335 and then Comes_From_Source (N)
3337 Orig_A := Entity (A);
3339 if Present (Orig_A) then
3340 if Is_Formal (Orig_A)
3341 and then Ekind (F) /= E_In_Parameter
3343 Generate_Reference (Orig_A, A, 'm');
3345 elsif not Is_Overloaded (A) then
3346 Generate_Reference (Orig_A, A);
3352 and then (Nkind (Parent (A)) /= N_Parameter_Association
3353 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3355 -- If style checking mode on, check match of formal name
3358 if Nkind (Parent (A)) = N_Parameter_Association then
3359 Check_Identifier (Selector_Name (Parent (A)), F);
3363 -- If the formal is Out or In_Out, do not resolve and expand the
3364 -- conversion, because it is subsequently expanded into explicit
3365 -- temporaries and assignments. However, the object of the
3366 -- conversion can be resolved. An exception is the case of tagged
3367 -- type conversion with a class-wide actual. In that case we want
3368 -- the tag check to occur and no temporary will be needed (no
3369 -- representation change can occur) and the parameter is passed by
3370 -- reference, so we go ahead and resolve the type conversion.
3371 -- Another exception is the case of reference to component or
3372 -- subcomponent of a bit-packed array, in which case we want to
3373 -- defer expansion to the point the in and out assignments are
3376 if Ekind (F) /= E_In_Parameter
3377 and then Nkind (A) = N_Type_Conversion
3378 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3380 if Ekind (F) = E_In_Out_Parameter
3381 and then Is_Array_Type (Etype (F))
3383 -- In a view conversion, the conversion must be legal in
3384 -- both directions, and thus both component types must be
3385 -- aliased, or neither (4.6 (8)).
3387 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3388 -- the privacy requirement should not apply to generic
3389 -- types, and should be checked in an instance. ARG query
3392 if Has_Aliased_Components (Etype (Expression (A))) /=
3393 Has_Aliased_Components (Etype (F))
3396 ("both component types in a view conversion must be"
3397 & " aliased, or neither", A);
3399 -- Comment here??? what set of cases???
3402 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3404 -- Check view conv between unrelated by ref array types
3406 if Is_By_Reference_Type (Etype (F))
3407 or else Is_By_Reference_Type (Etype (Expression (A)))
3410 ("view conversion between unrelated by reference " &
3411 "array types not allowed (\'A'I-00246)", A);
3413 -- In Ada 2005 mode, check view conversion component
3414 -- type cannot be private, tagged, or volatile. Note
3415 -- that we only apply this to source conversions. The
3416 -- generated code can contain conversions which are
3417 -- not subject to this test, and we cannot extract the
3418 -- component type in such cases since it is not present.
3420 elsif Comes_From_Source (A)
3421 and then Ada_Version >= Ada_2005
3424 Comp_Type : constant Entity_Id :=
3426 (Etype (Expression (A)));
3428 if (Is_Private_Type (Comp_Type)
3429 and then not Is_Generic_Type (Comp_Type))
3430 or else Is_Tagged_Type (Comp_Type)
3431 or else Is_Volatile (Comp_Type)
3434 ("component type of a view conversion cannot"
3435 & " be private, tagged, or volatile"
3444 -- Resolve expression if conversion is all OK
3446 if (Conversion_OK (A)
3447 or else Valid_Conversion (A, Etype (A), Expression (A)))
3448 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3450 Resolve (Expression (A));
3453 -- If the actual is a function call that returns a limited
3454 -- unconstrained object that needs finalization, create a
3455 -- transient scope for it, so that it can receive the proper
3456 -- finalization list.
3458 elsif Nkind (A) = N_Function_Call
3459 and then Is_Limited_Record (Etype (F))
3460 and then not Is_Constrained (Etype (F))
3461 and then Expander_Active
3462 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3464 Establish_Transient_Scope (A, False);
3466 -- A small optimization: if one of the actuals is a concatenation
3467 -- create a block around a procedure call to recover stack space.
3468 -- This alleviates stack usage when several procedure calls in
3469 -- the same statement list use concatenation. We do not perform
3470 -- this wrapping for code statements, where the argument is a
3471 -- static string, and we want to preserve warnings involving
3472 -- sequences of such statements.
3474 elsif Nkind (A) = N_Op_Concat
3475 and then Nkind (N) = N_Procedure_Call_Statement
3476 and then Expander_Active
3478 not (Is_Intrinsic_Subprogram (Nam)
3479 and then Chars (Nam) = Name_Asm)
3480 and then not Static_Concatenation (A)
3482 Establish_Transient_Scope (A, False);
3483 Resolve (A, Etype (F));
3486 if Nkind (A) = N_Type_Conversion
3487 and then Is_Array_Type (Etype (F))
3488 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3490 (Is_Limited_Type (Etype (F))
3491 or else Is_Limited_Type (Etype (Expression (A))))
3494 ("conversion between unrelated limited array types " &
3495 "not allowed (\A\I-00246)", A);
3497 if Is_Limited_Type (Etype (F)) then
3498 Explain_Limited_Type (Etype (F), A);
3501 if Is_Limited_Type (Etype (Expression (A))) then
3502 Explain_Limited_Type (Etype (Expression (A)), A);
3506 -- (Ada 2005: AI-251): If the actual is an allocator whose
3507 -- directly designated type is a class-wide interface, we build
3508 -- an anonymous access type to use it as the type of the
3509 -- allocator. Later, when the subprogram call is expanded, if
3510 -- the interface has a secondary dispatch table the expander
3511 -- will add a type conversion to force the correct displacement
3514 if Nkind (A) = N_Allocator then
3516 DDT : constant Entity_Id :=
3517 Directly_Designated_Type (Base_Type (Etype (F)));
3519 New_Itype : Entity_Id;
3522 if Is_Class_Wide_Type (DDT)
3523 and then Is_Interface (DDT)
3525 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3526 Set_Etype (New_Itype, Etype (A));
3527 Set_Directly_Designated_Type (New_Itype,
3528 Directly_Designated_Type (Etype (A)));
3529 Set_Etype (A, New_Itype);
3532 -- Ada 2005, AI-162:If the actual is an allocator, the
3533 -- innermost enclosing statement is the master of the
3534 -- created object. This needs to be done with expansion
3535 -- enabled only, otherwise the transient scope will not
3536 -- be removed in the expansion of the wrapped construct.
3538 if (Is_Controlled (DDT) or else Has_Task (DDT))
3539 and then Expander_Active
3541 Establish_Transient_Scope (A, False);
3546 -- (Ada 2005): The call may be to a primitive operation of
3547 -- a tagged synchronized type, declared outside of the type.
3548 -- In this case the controlling actual must be converted to
3549 -- its corresponding record type, which is the formal type.
3550 -- The actual may be a subtype, either because of a constraint
3551 -- or because it is a generic actual, so use base type to
3552 -- locate concurrent type.
3554 F_Typ := Base_Type (Etype (F));
3556 if Is_Tagged_Type (F_Typ)
3557 and then (Is_Concurrent_Type (F_Typ)
3558 or else Is_Concurrent_Record_Type (F_Typ))
3560 -- If the actual is overloaded, look for an interpretation
3561 -- that has a synchronized type.
3563 if not Is_Overloaded (A) then
3564 A_Typ := Base_Type (Etype (A));
3568 Index : Interp_Index;
3572 Get_First_Interp (A, Index, It);
3573 while Present (It.Typ) loop
3574 if Is_Concurrent_Type (It.Typ)
3575 or else Is_Concurrent_Record_Type (It.Typ)
3577 A_Typ := Base_Type (It.Typ);
3581 Get_Next_Interp (Index, It);
3587 Full_A_Typ : Entity_Id;
3590 if Present (Full_View (A_Typ)) then
3591 Full_A_Typ := Base_Type (Full_View (A_Typ));
3593 Full_A_Typ := A_Typ;
3596 -- Tagged synchronized type (case 1): the actual is a
3599 if Is_Concurrent_Type (A_Typ)
3600 and then Corresponding_Record_Type (A_Typ) = F_Typ
3603 Unchecked_Convert_To
3604 (Corresponding_Record_Type (A_Typ), A));
3605 Resolve (A, Etype (F));
3607 -- Tagged synchronized type (case 2): the formal is a
3610 elsif Ekind (Full_A_Typ) = E_Record_Type
3612 (Corresponding_Concurrent_Type (Full_A_Typ))
3613 and then Is_Concurrent_Type (F_Typ)
3614 and then Present (Corresponding_Record_Type (F_Typ))
3615 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3617 Resolve (A, Corresponding_Record_Type (F_Typ));
3622 Resolve (A, Etype (F));
3627 -- not a synchronized operation.
3629 Resolve (A, Etype (F));
3636 if Comes_From_Source (Original_Node (N))
3637 and then Nkind_In (Original_Node (N), N_Function_Call,
3638 N_Procedure_Call_Statement)
3640 -- In formal mode, check that actual parameters matching
3641 -- formals of tagged types are objects (or ancestor type
3642 -- conversions of objects), not general expressions.
3644 if Is_Actual_Tagged_Parameter (A) then
3645 if Is_SPARK_Object_Reference (A) then
3648 elsif Nkind (A) = N_Type_Conversion then
3650 Operand : constant Node_Id := Expression (A);
3651 Operand_Typ : constant Entity_Id := Etype (Operand);
3652 Target_Typ : constant Entity_Id := A_Typ;
3655 if not Is_SPARK_Object_Reference (Operand) then
3656 Check_SPARK_Restriction
3657 ("object required", Operand);
3659 -- In formal mode, the only view conversions are those
3660 -- involving ancestor conversion of an extended type.
3663 (Is_Tagged_Type (Target_Typ)
3664 and then not Is_Class_Wide_Type (Target_Typ)
3665 and then Is_Tagged_Type (Operand_Typ)
3666 and then not Is_Class_Wide_Type (Operand_Typ)
3667 and then Is_Ancestor (Target_Typ, Operand_Typ))
3670 (F, E_Out_Parameter, E_In_Out_Parameter)
3672 Check_SPARK_Restriction
3673 ("ancestor conversion is the only permitted "
3674 & "view conversion", A);
3676 Check_SPARK_Restriction
3677 ("ancestor conversion required", A);
3686 Check_SPARK_Restriction ("object required", A);
3689 -- In formal mode, the only view conversions are those
3690 -- involving ancestor conversion of an extended type.
3692 elsif Nkind (A) = N_Type_Conversion
3693 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3695 Check_SPARK_Restriction
3696 ("ancestor conversion is the only permitted view "
3701 -- Save actual for subsequent check on order dependence, and
3702 -- indicate whether actual is modifiable. For AI05-0144-2.
3704 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3706 -- For mode IN, if actual is an entity, and the type of the formal
3707 -- has warnings suppressed, then we reset Never_Set_In_Source for
3708 -- the calling entity. The reason for this is to catch cases like
3709 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3710 -- uses trickery to modify an IN parameter.
3712 if Ekind (F) = E_In_Parameter
3713 and then Is_Entity_Name (A)
3714 and then Present (Entity (A))
3715 and then Ekind (Entity (A)) = E_Variable
3716 and then Has_Warnings_Off (F_Typ)
3718 Set_Never_Set_In_Source (Entity (A), False);
3721 -- Perform error checks for IN and IN OUT parameters
3723 if Ekind (F) /= E_Out_Parameter then
3725 -- Check unset reference. For scalar parameters, it is clearly
3726 -- wrong to pass an uninitialized value as either an IN or
3727 -- IN-OUT parameter. For composites, it is also clearly an
3728 -- error to pass a completely uninitialized value as an IN
3729 -- parameter, but the case of IN OUT is trickier. We prefer
3730 -- not to give a warning here. For example, suppose there is
3731 -- a routine that sets some component of a record to False.
3732 -- It is perfectly reasonable to make this IN-OUT and allow
3733 -- either initialized or uninitialized records to be passed
3736 -- For partially initialized composite values, we also avoid
3737 -- warnings, since it is quite likely that we are passing a
3738 -- partially initialized value and only the initialized fields
3739 -- will in fact be read in the subprogram.
3741 if Is_Scalar_Type (A_Typ)
3742 or else (Ekind (F) = E_In_Parameter
3743 and then not Is_Partially_Initialized_Type (A_Typ))
3745 Check_Unset_Reference (A);
3748 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3749 -- actual to a nested call, since this is case of reading an
3750 -- out parameter, which is not allowed.
3752 if Ada_Version = Ada_83
3753 and then Is_Entity_Name (A)
3754 and then Ekind (Entity (A)) = E_Out_Parameter
3756 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3760 -- Case of OUT or IN OUT parameter
3762 if Ekind (F) /= E_In_Parameter then
3764 -- For an Out parameter, check for useless assignment. Note
3765 -- that we can't set Last_Assignment this early, because we may
3766 -- kill current values in Resolve_Call, and that call would
3767 -- clobber the Last_Assignment field.
3769 -- Note: call Warn_On_Useless_Assignment before doing the check
3770 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3771 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3772 -- reflects the last assignment, not this one!
3774 if Ekind (F) = E_Out_Parameter then
3775 if Warn_On_Modified_As_Out_Parameter (F)
3776 and then Is_Entity_Name (A)
3777 and then Present (Entity (A))
3778 and then Comes_From_Source (N)
3780 Warn_On_Useless_Assignment (Entity (A), A);
3784 -- Validate the form of the actual. Note that the call to
3785 -- Is_OK_Variable_For_Out_Formal generates the required
3786 -- reference in this case.
3788 -- A call to an initialization procedure for an aggregate
3789 -- component may initialize a nested component of a constant
3790 -- designated object. In this context the object is variable.
3792 if not Is_OK_Variable_For_Out_Formal (A)
3793 and then not Is_Init_Proc (Nam)
3795 Error_Msg_NE ("actual for& must be a variable", A, F);
3798 -- What's the following about???
3800 if Is_Entity_Name (A) then
3801 Kill_Checks (Entity (A));
3807 if Etype (A) = Any_Type then
3808 Set_Etype (N, Any_Type);
3812 -- Apply appropriate range checks for in, out, and in-out
3813 -- parameters. Out and in-out parameters also need a separate
3814 -- check, if there is a type conversion, to make sure the return
3815 -- value meets the constraints of the variable before the
3818 -- Gigi looks at the check flag and uses the appropriate types.
3819 -- For now since one flag is used there is an optimization which
3820 -- might not be done in the In Out case since Gigi does not do
3821 -- any analysis. More thought required about this ???
3823 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3825 -- Apply predicate checks, unless this is a call to the
3826 -- predicate check function itself, which would cause an
3827 -- infinite recursion.
3829 if not (Ekind (Nam) = E_Function
3830 and then Has_Predicates (Nam))
3832 Apply_Predicate_Check (A, F_Typ);
3835 -- Apply required constraint checks
3837 if Is_Scalar_Type (Etype (A)) then
3838 Apply_Scalar_Range_Check (A, F_Typ);
3840 elsif Is_Array_Type (Etype (A)) then
3841 Apply_Length_Check (A, F_Typ);
3843 elsif Is_Record_Type (F_Typ)
3844 and then Has_Discriminants (F_Typ)
3845 and then Is_Constrained (F_Typ)
3846 and then (not Is_Derived_Type (F_Typ)
3847 or else Comes_From_Source (Nam))
3849 Apply_Discriminant_Check (A, F_Typ);
3851 elsif Is_Access_Type (F_Typ)
3852 and then Is_Array_Type (Designated_Type (F_Typ))
3853 and then Is_Constrained (Designated_Type (F_Typ))
3855 Apply_Length_Check (A, F_Typ);
3857 elsif Is_Access_Type (F_Typ)
3858 and then Has_Discriminants (Designated_Type (F_Typ))
3859 and then Is_Constrained (Designated_Type (F_Typ))
3861 Apply_Discriminant_Check (A, F_Typ);
3864 Apply_Range_Check (A, F_Typ);
3867 -- Ada 2005 (AI-231): Note that the controlling parameter case
3868 -- already existed in Ada 95, which is partially checked
3869 -- elsewhere (see Checks), and we don't want the warning
3870 -- message to differ.
3872 if Is_Access_Type (F_Typ)
3873 and then Can_Never_Be_Null (F_Typ)
3874 and then Known_Null (A)
3876 if Is_Controlling_Formal (F) then
3877 Apply_Compile_Time_Constraint_Error
3879 Msg => "null value not allowed here?",
3880 Reason => CE_Access_Check_Failed);
3882 elsif Ada_Version >= Ada_2005 then
3883 Apply_Compile_Time_Constraint_Error
3885 Msg => "(Ada 2005) null not allowed in "
3886 & "null-excluding formal?",
3887 Reason => CE_Null_Not_Allowed);
3892 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3893 if Nkind (A) = N_Type_Conversion then
3894 if Is_Scalar_Type (A_Typ) then
3895 Apply_Scalar_Range_Check
3896 (Expression (A), Etype (Expression (A)), A_Typ);
3899 (Expression (A), Etype (Expression (A)), A_Typ);
3903 if Is_Scalar_Type (F_Typ) then
3904 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3905 elsif Is_Array_Type (F_Typ)
3906 and then Ekind (F) = E_Out_Parameter
3908 Apply_Length_Check (A, F_Typ);
3910 Apply_Range_Check (A, A_Typ, F_Typ);
3915 -- An actual associated with an access parameter is implicitly
3916 -- converted to the anonymous access type of the formal and must
3917 -- satisfy the legality checks for access conversions.
3919 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3920 if not Valid_Conversion (A, F_Typ, A) then
3922 ("invalid implicit conversion for access parameter", A);
3926 -- Check bad case of atomic/volatile argument (RM C.6(12))
3928 if Is_By_Reference_Type (Etype (F))
3929 and then Comes_From_Source (N)
3931 if Is_Atomic_Object (A)
3932 and then not Is_Atomic (Etype (F))
3935 ("cannot pass atomic argument to non-atomic formal",
3938 elsif Is_Volatile_Object (A)
3939 and then not Is_Volatile (Etype (F))
3942 ("cannot pass volatile argument to non-volatile formal",
3947 -- Check that subprograms don't have improper controlling
3948 -- arguments (RM 3.9.2 (9)).
3950 -- A primitive operation may have an access parameter of an
3951 -- incomplete tagged type, but a dispatching call is illegal
3952 -- if the type is still incomplete.
3954 if Is_Controlling_Formal (F) then
3955 Set_Is_Controlling_Actual (A);
3957 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3959 Desig : constant Entity_Id := Designated_Type (Etype (F));
3961 if Ekind (Desig) = E_Incomplete_Type
3962 and then No (Full_View (Desig))
3963 and then No (Non_Limited_View (Desig))
3966 ("premature use of incomplete type& " &
3967 "in dispatching call", A, Desig);
3972 elsif Nkind (A) = N_Explicit_Dereference then
3973 Validate_Remote_Access_To_Class_Wide_Type (A);
3976 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3977 and then not Is_Class_Wide_Type (F_Typ)
3978 and then not Is_Controlling_Formal (F)
3980 Error_Msg_N ("class-wide argument not allowed here!", A);
3982 if Is_Subprogram (Nam)
3983 and then Comes_From_Source (Nam)
3985 Error_Msg_Node_2 := F_Typ;
3987 ("& is not a dispatching operation of &!", A, Nam);
3990 elsif Is_Access_Type (A_Typ)
3991 and then Is_Access_Type (F_Typ)
3992 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3993 and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3994 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3995 or else (Nkind (A) = N_Attribute_Reference
3997 Is_Class_Wide_Type (Etype (Prefix (A)))))
3998 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3999 and then not Is_Controlling_Formal (F)
4001 -- Disable these checks for call to imported C++ subprograms
4004 (Is_Entity_Name (Name (N))
4005 and then Is_Imported (Entity (Name (N)))
4006 and then Convention (Entity (Name (N))) = Convention_CPP)
4009 ("access to class-wide argument not allowed here!", A);
4011 if Is_Subprogram (Nam)
4012 and then Comes_From_Source (Nam)
4014 Error_Msg_Node_2 := Designated_Type (F_Typ);
4016 ("& is not a dispatching operation of &!", A, Nam);
4022 -- If it is a named association, treat the selector_name as a
4023 -- proper identifier, and mark the corresponding entity. Ignore
4024 -- this reference in ALFA mode, as it refers to an entity not in
4025 -- scope at the point of reference, so the reference should be
4026 -- ignored for computing effects of subprograms.
4028 if Nkind (Parent (A)) = N_Parameter_Association
4029 and then not ALFA_Mode
4031 Set_Entity (Selector_Name (Parent (A)), F);
4032 Generate_Reference (F, Selector_Name (Parent (A)));
4033 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4034 Generate_Reference (F_Typ, N, ' ');
4039 if Ekind (F) /= E_Out_Parameter then
4040 Check_Unset_Reference (A);
4045 -- Case where actual is not present
4053 end Resolve_Actuals;
4055 -----------------------
4056 -- Resolve_Allocator --
4057 -----------------------
4059 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4060 E : constant Node_Id := Expression (N);
4062 Discrim : Entity_Id;
4065 Assoc : Node_Id := Empty;
4068 procedure Check_Allocator_Discrim_Accessibility
4069 (Disc_Exp : Node_Id;
4070 Alloc_Typ : Entity_Id);
4071 -- Check that accessibility level associated with an access discriminant
4072 -- initialized in an allocator by the expression Disc_Exp is not deeper
4073 -- than the level of the allocator type Alloc_Typ. An error message is
4074 -- issued if this condition is violated. Specialized checks are done for
4075 -- the cases of a constraint expression which is an access attribute or
4076 -- an access discriminant.
4078 function In_Dispatching_Context return Boolean;
4079 -- If the allocator is an actual in a call, it is allowed to be class-
4080 -- wide when the context is not because it is a controlling actual.
4082 -------------------------------------------
4083 -- Check_Allocator_Discrim_Accessibility --
4084 -------------------------------------------
4086 procedure Check_Allocator_Discrim_Accessibility
4087 (Disc_Exp : Node_Id;
4088 Alloc_Typ : Entity_Id)
4091 if Type_Access_Level (Etype (Disc_Exp)) >
4092 Type_Access_Level (Alloc_Typ)
4095 ("operand type has deeper level than allocator type", Disc_Exp);
4097 -- When the expression is an Access attribute the level of the prefix
4098 -- object must not be deeper than that of the allocator's type.
4100 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4101 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
4103 and then Object_Access_Level (Prefix (Disc_Exp))
4104 > Type_Access_Level (Alloc_Typ)
4107 ("prefix of attribute has deeper level than allocator type",
4110 -- When the expression is an access discriminant the check is against
4111 -- the level of the prefix object.
4113 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4114 and then Nkind (Disc_Exp) = N_Selected_Component
4115 and then Object_Access_Level (Prefix (Disc_Exp))
4116 > Type_Access_Level (Alloc_Typ)
4119 ("access discriminant has deeper level than allocator type",
4122 -- All other cases are legal
4127 end Check_Allocator_Discrim_Accessibility;
4129 ----------------------------
4130 -- In_Dispatching_Context --
4131 ----------------------------
4133 function In_Dispatching_Context return Boolean is
4134 Par : constant Node_Id := Parent (N);
4138 Nkind_In (Par, N_Function_Call,
4139 N_Procedure_Call_Statement)
4140 and then Is_Entity_Name (Name (Par))
4141 and then Is_Dispatching_Operation (Entity (Name (Par)));
4142 end In_Dispatching_Context;
4144 -- Start of processing for Resolve_Allocator
4147 -- Replace general access with specific type
4149 if Ekind (Etype (N)) = E_Allocator_Type then
4150 Set_Etype (N, Base_Type (Typ));
4153 if Is_Abstract_Type (Typ) then
4154 Error_Msg_N ("type of allocator cannot be abstract", N);
4157 -- For qualified expression, resolve the expression using the
4158 -- given subtype (nothing to do for type mark, subtype indication)
4160 if Nkind (E) = N_Qualified_Expression then
4161 if Is_Class_Wide_Type (Etype (E))
4162 and then not Is_Class_Wide_Type (Designated_Type (Typ))
4163 and then not In_Dispatching_Context
4166 ("class-wide allocator not allowed for this access type", N);
4169 Resolve (Expression (E), Etype (E));
4170 Check_Unset_Reference (Expression (E));
4172 -- A qualified expression requires an exact match of the type,
4173 -- class-wide matching is not allowed.
4175 if (Is_Class_Wide_Type (Etype (Expression (E)))
4176 or else Is_Class_Wide_Type (Etype (E)))
4177 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4179 Wrong_Type (Expression (E), Etype (E));
4182 -- A special accessibility check is needed for allocators that
4183 -- constrain access discriminants. The level of the type of the
4184 -- expression used to constrain an access discriminant cannot be
4185 -- deeper than the type of the allocator (in contrast to access
4186 -- parameters, where the level of the actual can be arbitrary).
4188 -- We can't use Valid_Conversion to perform this check because
4189 -- in general the type of the allocator is unrelated to the type
4190 -- of the access discriminant.
4192 if Ekind (Typ) /= E_Anonymous_Access_Type
4193 or else Is_Local_Anonymous_Access (Typ)
4195 Subtyp := Entity (Subtype_Mark (E));
4197 Aggr := Original_Node (Expression (E));
4199 if Has_Discriminants (Subtyp)
4200 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4202 Discrim := First_Discriminant (Base_Type (Subtyp));
4204 -- Get the first component expression of the aggregate
4206 if Present (Expressions (Aggr)) then
4207 Disc_Exp := First (Expressions (Aggr));
4209 elsif Present (Component_Associations (Aggr)) then
4210 Assoc := First (Component_Associations (Aggr));
4212 if Present (Assoc) then
4213 Disc_Exp := Expression (Assoc);
4222 while Present (Discrim) and then Present (Disc_Exp) loop
4223 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4224 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4227 Next_Discriminant (Discrim);
4229 if Present (Discrim) then
4230 if Present (Assoc) then
4232 Disc_Exp := Expression (Assoc);
4234 elsif Present (Next (Disc_Exp)) then
4238 Assoc := First (Component_Associations (Aggr));
4240 if Present (Assoc) then
4241 Disc_Exp := Expression (Assoc);
4251 -- For a subtype mark or subtype indication, freeze the subtype
4254 Freeze_Expression (E);
4256 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4258 ("initialization required for access-to-constant allocator", N);
4261 -- A special accessibility check is needed for allocators that
4262 -- constrain access discriminants. The level of the type of the
4263 -- expression used to constrain an access discriminant cannot be
4264 -- deeper than the type of the allocator (in contrast to access
4265 -- parameters, where the level of the actual can be arbitrary).
4266 -- We can't use Valid_Conversion to perform this check because
4267 -- in general the type of the allocator is unrelated to the type
4268 -- of the access discriminant.
4270 if Nkind (Original_Node (E)) = N_Subtype_Indication
4271 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4272 or else Is_Local_Anonymous_Access (Typ))
4274 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4276 if Has_Discriminants (Subtyp) then
4277 Discrim := First_Discriminant (Base_Type (Subtyp));
4278 Constr := First (Constraints (Constraint (Original_Node (E))));
4279 while Present (Discrim) and then Present (Constr) loop
4280 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4281 if Nkind (Constr) = N_Discriminant_Association then
4282 Disc_Exp := Original_Node (Expression (Constr));
4284 Disc_Exp := Original_Node (Constr);
4287 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4290 Next_Discriminant (Discrim);
4297 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4298 -- check that the level of the type of the created object is not deeper
4299 -- than the level of the allocator's access type, since extensions can
4300 -- now occur at deeper levels than their ancestor types. This is a
4301 -- static accessibility level check; a run-time check is also needed in
4302 -- the case of an initialized allocator with a class-wide argument (see
4303 -- Expand_Allocator_Expression).
4305 if Ada_Version >= Ada_2005
4306 and then Is_Class_Wide_Type (Designated_Type (Typ))
4309 Exp_Typ : Entity_Id;
4312 if Nkind (E) = N_Qualified_Expression then
4313 Exp_Typ := Etype (E);
4314 elsif Nkind (E) = N_Subtype_Indication then
4315 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4317 Exp_Typ := Entity (E);
4320 if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
4321 if In_Instance_Body then
4322 Error_Msg_N ("?type in allocator has deeper level than" &
4323 " designated class-wide type", E);
4324 Error_Msg_N ("\?Program_Error will be raised at run time",
4327 Make_Raise_Program_Error (Sloc (N),
4328 Reason => PE_Accessibility_Check_Failed));
4331 -- Do not apply Ada 2005 accessibility checks on a class-wide
4332 -- allocator if the type given in the allocator is a formal
4333 -- type. A run-time check will be performed in the instance.
4335 elsif not Is_Generic_Type (Exp_Typ) then
4336 Error_Msg_N ("type in allocator has deeper level than" &
4337 " designated class-wide type", E);
4343 -- Check for allocation from an empty storage pool
4345 if No_Pool_Assigned (Typ) then
4346 Error_Msg_N ("allocation from empty storage pool!", N);
4348 -- If the context is an unchecked conversion, as may happen within an
4349 -- inlined subprogram, the allocator is being resolved with its own
4350 -- anonymous type. In that case, if the target type has a specific
4351 -- storage pool, it must be inherited explicitly by the allocator type.
4353 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4354 and then No (Associated_Storage_Pool (Typ))
4356 Set_Associated_Storage_Pool
4357 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4360 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4361 Check_Restriction (No_Anonymous_Allocators, N);
4364 -- Check that an allocator with task parts isn't for a nested access
4365 -- type when restriction No_Task_Hierarchy applies.
4367 if not Is_Library_Level_Entity (Base_Type (Typ))
4368 and then Has_Task (Base_Type (Designated_Type (Typ)))
4370 Check_Restriction (No_Task_Hierarchy, N);
4373 -- An erroneous allocator may be rewritten as a raise Program_Error
4376 if Nkind (N) = N_Allocator then
4378 -- An anonymous access discriminant is the definition of a
4381 if Ekind (Typ) = E_Anonymous_Access_Type
4382 and then Nkind (Associated_Node_For_Itype (Typ)) =
4383 N_Discriminant_Specification
4385 -- Avoid marking an allocator as a dynamic coextension if it is
4386 -- within a static construct.
4388 if not Is_Static_Coextension (N) then
4389 Set_Is_Dynamic_Coextension (N);
4392 -- Cleanup for potential static coextensions
4395 Set_Is_Dynamic_Coextension (N, False);
4396 Set_Is_Static_Coextension (N, False);
4400 -- Report a simple error: if the designated object is a local task,
4401 -- its body has not been seen yet, and its activation will fail
4402 -- an elaboration check.
4404 if Is_Task_Type (Designated_Type (Typ))
4405 and then Scope (Base_Type (Designated_Type (Typ))) = Current_Scope
4406 and then Is_Compilation_Unit (Current_Scope)
4407 and then Ekind (Current_Scope) = E_Package
4408 and then not In_Package_Body (Current_Scope)
4411 ("cannot activate task before body seen?", N);
4412 Error_Msg_N ("\Program_Error will be raised at run time?", N);
4414 end Resolve_Allocator;
4416 ---------------------------
4417 -- Resolve_Arithmetic_Op --
4418 ---------------------------
4420 -- Used for resolving all arithmetic operators except exponentiation
4422 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4423 L : constant Node_Id := Left_Opnd (N);
4424 R : constant Node_Id := Right_Opnd (N);
4425 TL : constant Entity_Id := Base_Type (Etype (L));
4426 TR : constant Entity_Id := Base_Type (Etype (R));
4430 B_Typ : constant Entity_Id := Base_Type (Typ);
4431 -- We do the resolution using the base type, because intermediate values
4432 -- in expressions always are of the base type, not a subtype of it.
4434 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4435 -- Returns True if N is in a context that expects "any real type"
4437 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4438 -- Return True iff given type is Integer or universal real/integer
4440 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4441 -- Choose type of integer literal in fixed-point operation to conform
4442 -- to available fixed-point type. T is the type of the other operand,
4443 -- which is needed to determine the expected type of N.
4445 procedure Set_Operand_Type (N : Node_Id);
4446 -- Set operand type to T if universal
4448 -------------------------------
4449 -- Expected_Type_Is_Any_Real --
4450 -------------------------------
4452 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4454 -- N is the expression after "delta" in a fixed_point_definition;
4457 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4458 N_Decimal_Fixed_Point_Definition,
4460 -- N is one of the bounds in a real_range_specification;
4463 N_Real_Range_Specification,
4465 -- N is the expression of a delta_constraint;
4468 N_Delta_Constraint);
4469 end Expected_Type_Is_Any_Real;
4471 -----------------------------
4472 -- Is_Integer_Or_Universal --
4473 -----------------------------
4475 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4477 Index : Interp_Index;
4481 if not Is_Overloaded (N) then
4483 return Base_Type (T) = Base_Type (Standard_Integer)
4484 or else T = Universal_Integer
4485 or else T = Universal_Real;
4487 Get_First_Interp (N, Index, It);
4488 while Present (It.Typ) loop
4489 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4490 or else It.Typ = Universal_Integer
4491 or else It.Typ = Universal_Real
4496 Get_Next_Interp (Index, It);
4501 end Is_Integer_Or_Universal;
4503 ----------------------------
4504 -- Set_Mixed_Mode_Operand --
4505 ----------------------------
4507 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4508 Index : Interp_Index;
4512 if Universal_Interpretation (N) = Universal_Integer then
4514 -- A universal integer literal is resolved as standard integer
4515 -- except in the case of a fixed-point result, where we leave it
4516 -- as universal (to be handled by Exp_Fixd later on)
4518 if Is_Fixed_Point_Type (T) then
4519 Resolve (N, Universal_Integer);
4521 Resolve (N, Standard_Integer);
4524 elsif Universal_Interpretation (N) = Universal_Real
4525 and then (T = Base_Type (Standard_Integer)
4526 or else T = Universal_Integer
4527 or else T = Universal_Real)
4529 -- A universal real can appear in a fixed-type context. We resolve
4530 -- the literal with that context, even though this might raise an
4531 -- exception prematurely (the other operand may be zero).
4535 elsif Etype (N) = Base_Type (Standard_Integer)
4536 and then T = Universal_Real
4537 and then Is_Overloaded (N)
4539 -- Integer arg in mixed-mode operation. Resolve with universal
4540 -- type, in case preference rule must be applied.
4542 Resolve (N, Universal_Integer);
4545 and then B_Typ /= Universal_Fixed
4547 -- Not a mixed-mode operation, resolve with context
4551 elsif Etype (N) = Any_Fixed then
4553 -- N may itself be a mixed-mode operation, so use context type
4557 elsif Is_Fixed_Point_Type (T)
4558 and then B_Typ = Universal_Fixed
4559 and then Is_Overloaded (N)
4561 -- Must be (fixed * fixed) operation, operand must have one
4562 -- compatible interpretation.
4564 Resolve (N, Any_Fixed);
4566 elsif Is_Fixed_Point_Type (B_Typ)
4567 and then (T = Universal_Real
4568 or else Is_Fixed_Point_Type (T))
4569 and then Is_Overloaded (N)
4571 -- C * F(X) in a fixed context, where C is a real literal or a
4572 -- fixed-point expression. F must have either a fixed type
4573 -- interpretation or an integer interpretation, but not both.
4575 Get_First_Interp (N, Index, It);
4576 while Present (It.Typ) loop
4577 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4578 if Analyzed (N) then
4579 Error_Msg_N ("ambiguous operand in fixed operation", N);
4581 Resolve (N, Standard_Integer);
4584 elsif Is_Fixed_Point_Type (It.Typ) then
4585 if Analyzed (N) then
4586 Error_Msg_N ("ambiguous operand in fixed operation", N);
4588 Resolve (N, It.Typ);
4592 Get_Next_Interp (Index, It);
4595 -- Reanalyze the literal with the fixed type of the context. If
4596 -- context is Universal_Fixed, we are within a conversion, leave
4597 -- the literal as a universal real because there is no usable
4598 -- fixed type, and the target of the conversion plays no role in
4612 if B_Typ = Universal_Fixed
4613 and then Nkind (Op2) = N_Real_Literal
4615 T2 := Universal_Real;
4620 Set_Analyzed (Op2, False);
4627 end Set_Mixed_Mode_Operand;
4629 ----------------------
4630 -- Set_Operand_Type --
4631 ----------------------
4633 procedure Set_Operand_Type (N : Node_Id) is
4635 if Etype (N) = Universal_Integer
4636 or else Etype (N) = Universal_Real
4640 end Set_Operand_Type;
4642 -- Start of processing for Resolve_Arithmetic_Op
4645 if Comes_From_Source (N)
4646 and then Ekind (Entity (N)) = E_Function
4647 and then Is_Imported (Entity (N))
4648 and then Is_Intrinsic_Subprogram (Entity (N))
4650 Resolve_Intrinsic_Operator (N, Typ);
4653 -- Special-case for mixed-mode universal expressions or fixed point type
4654 -- operation: each argument is resolved separately. The same treatment
4655 -- is required if one of the operands of a fixed point operation is
4656 -- universal real, since in this case we don't do a conversion to a
4657 -- specific fixed-point type (instead the expander handles the case).
4659 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4660 and then Present (Universal_Interpretation (L))
4661 and then Present (Universal_Interpretation (R))
4663 Resolve (L, Universal_Interpretation (L));
4664 Resolve (R, Universal_Interpretation (R));
4665 Set_Etype (N, B_Typ);
4667 elsif (B_Typ = Universal_Real
4668 or else Etype (N) = Universal_Fixed
4669 or else (Etype (N) = Any_Fixed
4670 and then Is_Fixed_Point_Type (B_Typ))
4671 or else (Is_Fixed_Point_Type (B_Typ)
4672 and then (Is_Integer_Or_Universal (L)
4674 Is_Integer_Or_Universal (R))))
4675 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4677 if TL = Universal_Integer or else TR = Universal_Integer then
4678 Check_For_Visible_Operator (N, B_Typ);
4681 -- If context is a fixed type and one operand is integer, the other
4682 -- is resolved with the type of the context.
4684 if Is_Fixed_Point_Type (B_Typ)
4685 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4686 or else TL = Universal_Integer)
4691 elsif Is_Fixed_Point_Type (B_Typ)
4692 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4693 or else TR = Universal_Integer)
4699 Set_Mixed_Mode_Operand (L, TR);
4700 Set_Mixed_Mode_Operand (R, TL);
4703 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4704 -- multiplying operators from being used when the expected type is
4705 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4706 -- some cases where the expected type is actually Any_Real;
4707 -- Expected_Type_Is_Any_Real takes care of that case.
4709 if Etype (N) = Universal_Fixed
4710 or else Etype (N) = Any_Fixed
4712 if B_Typ = Universal_Fixed
4713 and then not Expected_Type_Is_Any_Real (N)
4714 and then not Nkind_In (Parent (N), N_Type_Conversion,
4715 N_Unchecked_Type_Conversion)
4717 Error_Msg_N ("type cannot be determined from context!", N);
4718 Error_Msg_N ("\explicit conversion to result type required", N);
4720 Set_Etype (L, Any_Type);
4721 Set_Etype (R, Any_Type);
4724 if Ada_Version = Ada_83
4725 and then Etype (N) = Universal_Fixed
4727 Nkind_In (Parent (N), N_Type_Conversion,
4728 N_Unchecked_Type_Conversion)
4731 ("(Ada 83) fixed-point operation "
4732 & "needs explicit conversion", N);
4735 -- The expected type is "any real type" in contexts like
4737 -- type T is delta <universal_fixed-expression> ...
4739 -- in which case we need to set the type to Universal_Real
4740 -- so that static expression evaluation will work properly.
4742 if Expected_Type_Is_Any_Real (N) then
4743 Set_Etype (N, Universal_Real);
4745 Set_Etype (N, B_Typ);
4749 elsif Is_Fixed_Point_Type (B_Typ)
4750 and then (Is_Integer_Or_Universal (L)
4751 or else Nkind (L) = N_Real_Literal
4752 or else Nkind (R) = N_Real_Literal
4753 or else Is_Integer_Or_Universal (R))
4755 Set_Etype (N, B_Typ);
4757 elsif Etype (N) = Any_Fixed then
4759 -- If no previous errors, this is only possible if one operand is
4760 -- overloaded and the context is universal. Resolve as such.
4762 Set_Etype (N, B_Typ);
4766 if (TL = Universal_Integer or else TL = Universal_Real)
4768 (TR = Universal_Integer or else TR = Universal_Real)
4770 Check_For_Visible_Operator (N, B_Typ);
4773 -- If the context is Universal_Fixed and the operands are also
4774 -- universal fixed, this is an error, unless there is only one
4775 -- applicable fixed_point type (usually Duration).
4777 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4778 T := Unique_Fixed_Point_Type (N);
4780 if T = Any_Type then
4793 -- If one of the arguments was resolved to a non-universal type.
4794 -- label the result of the operation itself with the same type.
4795 -- Do the same for the universal argument, if any.
4797 T := Intersect_Types (L, R);
4798 Set_Etype (N, Base_Type (T));
4799 Set_Operand_Type (L);
4800 Set_Operand_Type (R);
4803 Generate_Operator_Reference (N, Typ);
4804 Eval_Arithmetic_Op (N);
4806 -- In SPARK, a multiplication or division with operands of fixed point
4807 -- types shall be qualified or explicitly converted to identify the
4810 if (Is_Fixed_Point_Type (Etype (L))
4811 or else Is_Fixed_Point_Type (Etype (R)))
4812 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4814 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4816 Check_SPARK_Restriction
4817 ("operation should be qualified or explicitly converted", N);
4820 -- Set overflow and division checking bit. Much cleverer code needed
4821 -- here eventually and perhaps the Resolve routines should be separated
4822 -- for the various arithmetic operations, since they will need
4823 -- different processing. ???
4825 if Nkind (N) in N_Op then
4826 if not Overflow_Checks_Suppressed (Etype (N)) then
4827 Enable_Overflow_Check (N);
4830 -- Give warning if explicit division by zero
4832 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4833 and then not Division_Checks_Suppressed (Etype (N))
4835 Rop := Right_Opnd (N);
4837 if Compile_Time_Known_Value (Rop)
4838 and then ((Is_Integer_Type (Etype (Rop))
4839 and then Expr_Value (Rop) = Uint_0)
4841 (Is_Real_Type (Etype (Rop))
4842 and then Expr_Value_R (Rop) = Ureal_0))
4844 -- Specialize the warning message according to the operation
4848 Apply_Compile_Time_Constraint_Error
4849 (N, "division by zero?", CE_Divide_By_Zero,
4850 Loc => Sloc (Right_Opnd (N)));
4853 Apply_Compile_Time_Constraint_Error
4854 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4855 Loc => Sloc (Right_Opnd (N)));
4858 Apply_Compile_Time_Constraint_Error
4859 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4860 Loc => Sloc (Right_Opnd (N)));
4862 -- Division by zero can only happen with division, rem,
4863 -- and mod operations.
4866 raise Program_Error;
4869 -- Otherwise just set the flag to check at run time
4872 Activate_Division_Check (N);
4876 -- If Restriction No_Implicit_Conditionals is active, then it is
4877 -- violated if either operand can be negative for mod, or for rem
4878 -- if both operands can be negative.
4880 if Restriction_Check_Required (No_Implicit_Conditionals)
4881 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4890 -- Set if corresponding operand might be negative
4894 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4895 LNeg := (not OK) or else Lo < 0;
4898 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4899 RNeg := (not OK) or else Lo < 0;
4901 -- Check if we will be generating conditionals. There are two
4902 -- cases where that can happen, first for REM, the only case
4903 -- is largest negative integer mod -1, where the division can
4904 -- overflow, but we still have to give the right result. The
4905 -- front end generates a test for this annoying case. Here we
4906 -- just test if both operands can be negative (that's what the
4907 -- expander does, so we match its logic here).
4909 -- The second case is mod where either operand can be negative.
4910 -- In this case, the back end has to generate additional tests.
4912 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4914 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4916 Check_Restriction (No_Implicit_Conditionals, N);
4922 Check_Unset_Reference (L);
4923 Check_Unset_Reference (R);
4924 end Resolve_Arithmetic_Op;
4930 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4931 Loc : constant Source_Ptr := Sloc (N);
4932 Subp : constant Node_Id := Name (N);
4940 function Same_Or_Aliased_Subprograms
4942 E : Entity_Id) return Boolean;
4943 -- Returns True if the subprogram entity S is the same as E or else
4944 -- S is an alias of E.
4946 ---------------------------------
4947 -- Same_Or_Aliased_Subprograms --
4948 ---------------------------------
4950 function Same_Or_Aliased_Subprograms
4952 E : Entity_Id) return Boolean
4954 Subp_Alias : constant Entity_Id := Alias (S);
4957 or else (Present (Subp_Alias) and then Subp_Alias = E);
4958 end Same_Or_Aliased_Subprograms;
4960 -- Start of processing for Resolve_Call
4963 -- The context imposes a unique interpretation with type Typ on a
4964 -- procedure or function call. Find the entity of the subprogram that
4965 -- yields the expected type, and propagate the corresponding formal
4966 -- constraints on the actuals. The caller has established that an
4967 -- interpretation exists, and emitted an error if not unique.
4969 -- First deal with the case of a call to an access-to-subprogram,
4970 -- dereference made explicit in Analyze_Call.
4972 if Ekind (Etype (Subp)) = E_Subprogram_Type then
4973 if not Is_Overloaded (Subp) then
4974 Nam := Etype (Subp);
4977 -- Find the interpretation whose type (a subprogram type) has a
4978 -- return type that is compatible with the context. Analysis of
4979 -- the node has established that one exists.
4983 Get_First_Interp (Subp, I, It);
4984 while Present (It.Typ) loop
4985 if Covers (Typ, Etype (It.Typ)) then
4990 Get_Next_Interp (I, It);
4994 raise Program_Error;
4998 -- If the prefix is not an entity, then resolve it
5000 if not Is_Entity_Name (Subp) then
5001 Resolve (Subp, Nam);
5004 -- For an indirect call, we always invalidate checks, since we do not
5005 -- know whether the subprogram is local or global. Yes we could do
5006 -- better here, e.g. by knowing that there are no local subprograms,
5007 -- but it does not seem worth the effort. Similarly, we kill all
5008 -- knowledge of current constant values.
5010 Kill_Current_Values;
5012 -- If this is a procedure call which is really an entry call, do
5013 -- the conversion of the procedure call to an entry call. Protected
5014 -- operations use the same circuitry because the name in the call
5015 -- can be an arbitrary expression with special resolution rules.
5017 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5018 or else (Is_Entity_Name (Subp)
5019 and then Ekind (Entity (Subp)) = E_Entry)
5021 Resolve_Entry_Call (N, Typ);
5022 Check_Elab_Call (N);
5024 -- Kill checks and constant values, as above for indirect case
5025 -- Who knows what happens when another task is activated?
5027 Kill_Current_Values;
5030 -- Normal subprogram call with name established in Resolve
5032 elsif not (Is_Type (Entity (Subp))) then
5033 Nam := Entity (Subp);
5034 Set_Entity_With_Style_Check (Subp, Nam);
5036 -- Otherwise we must have the case of an overloaded call
5039 pragma Assert (Is_Overloaded (Subp));
5041 -- Initialize Nam to prevent warning (we know it will be assigned
5042 -- in the loop below, but the compiler does not know that).
5046 Get_First_Interp (Subp, I, It);
5047 while Present (It.Typ) loop
5048 if Covers (Typ, It.Typ) then
5050 Set_Entity_With_Style_Check (Subp, Nam);
5054 Get_Next_Interp (I, It);
5058 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5059 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5060 and then Nkind (Subp) /= N_Explicit_Dereference
5061 and then Present (Parameter_Associations (N))
5063 -- The prefix is a parameterless function call that returns an access
5064 -- to subprogram. If parameters are present in the current call, add
5065 -- add an explicit dereference. We use the base type here because
5066 -- within an instance these may be subtypes.
5068 -- The dereference is added either in Analyze_Call or here. Should
5069 -- be consolidated ???
5071 Set_Is_Overloaded (Subp, False);
5072 Set_Etype (Subp, Etype (Nam));
5073 Insert_Explicit_Dereference (Subp);
5074 Nam := Designated_Type (Etype (Nam));
5075 Resolve (Subp, Nam);
5078 -- Check that a call to Current_Task does not occur in an entry body
5080 if Is_RTE (Nam, RE_Current_Task) then
5089 -- Exclude calls that occur within the default of a formal
5090 -- parameter of the entry, since those are evaluated outside
5093 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5095 if Nkind (P) = N_Entry_Body
5096 or else (Nkind (P) = N_Subprogram_Body
5097 and then Is_Entry_Barrier_Function (P))
5101 ("?& should not be used in entry body (RM C.7(17))",
5104 ("\Program_Error will be raised at run time?", N, Nam);
5106 Make_Raise_Program_Error (Loc,
5107 Reason => PE_Current_Task_In_Entry_Body));
5108 Set_Etype (N, Rtype);
5115 -- Check that a procedure call does not occur in the context of the
5116 -- entry call statement of a conditional or timed entry call. Note that
5117 -- the case of a call to a subprogram renaming of an entry will also be
5118 -- rejected. The test for N not being an N_Entry_Call_Statement is
5119 -- defensive, covering the possibility that the processing of entry
5120 -- calls might reach this point due to later modifications of the code
5123 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5124 and then Nkind (N) /= N_Entry_Call_Statement
5125 and then Entry_Call_Statement (Parent (N)) = N
5127 if Ada_Version < Ada_2005 then
5128 Error_Msg_N ("entry call required in select statement", N);
5130 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5131 -- for a procedure_or_entry_call, the procedure_name or
5132 -- procedure_prefix of the procedure_call_statement shall denote
5133 -- an entry renamed by a procedure, or (a view of) a primitive
5134 -- subprogram of a limited interface whose first parameter is
5135 -- a controlling parameter.
5137 elsif Nkind (N) = N_Procedure_Call_Statement
5138 and then not Is_Renamed_Entry (Nam)
5139 and then not Is_Controlling_Limited_Procedure (Nam)
5142 ("entry call or dispatching primitive of interface required", N);
5146 -- Check that this is not a call to a protected procedure or entry from
5147 -- within a protected function.
5149 if Ekind (Current_Scope) = E_Function
5150 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5151 and then Ekind (Nam) /= E_Function
5152 and then Scope (Nam) = Scope (Current_Scope)
5154 Error_Msg_N ("within protected function, protected " &
5155 "object is constant", N);
5156 Error_Msg_N ("\cannot call operation that may modify it", N);
5159 -- Freeze the subprogram name if not in a spec-expression. Note that we
5160 -- freeze procedure calls as well as function calls. Procedure calls are
5161 -- not frozen according to the rules (RM 13.14(14)) because it is
5162 -- impossible to have a procedure call to a non-frozen procedure in pure
5163 -- Ada, but in the code that we generate in the expander, this rule
5164 -- needs extending because we can generate procedure calls that need
5167 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5168 Freeze_Expression (Subp);
5171 -- For a predefined operator, the type of the result is the type imposed
5172 -- by context, except for a predefined operation on universal fixed.
5173 -- Otherwise The type of the call is the type returned by the subprogram
5176 if Is_Predefined_Op (Nam) then
5177 if Etype (N) /= Universal_Fixed then
5181 -- If the subprogram returns an array type, and the context requires the
5182 -- component type of that array type, the node is really an indexing of
5183 -- the parameterless call. Resolve as such. A pathological case occurs
5184 -- when the type of the component is an access to the array type. In
5185 -- this case the call is truly ambiguous.
5187 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5189 ((Is_Array_Type (Etype (Nam))
5190 and then Covers (Typ, Component_Type (Etype (Nam))))
5191 or else (Is_Access_Type (Etype (Nam))
5192 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5196 Component_Type (Designated_Type (Etype (Nam))))))
5199 Index_Node : Node_Id;
5201 Ret_Type : constant Entity_Id := Etype (Nam);
5204 if Is_Access_Type (Ret_Type)
5205 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5208 ("cannot disambiguate function call and indexing", N);
5210 New_Subp := Relocate_Node (Subp);
5211 Set_Entity (Subp, Nam);
5213 if (Is_Array_Type (Ret_Type)
5214 and then Component_Type (Ret_Type) /= Any_Type)
5216 (Is_Access_Type (Ret_Type)
5218 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5220 if Needs_No_Actuals (Nam) then
5222 -- Indexed call to a parameterless function
5225 Make_Indexed_Component (Loc,
5227 Make_Function_Call (Loc,
5229 Expressions => Parameter_Associations (N));
5231 -- An Ada 2005 prefixed call to a primitive operation
5232 -- whose first parameter is the prefix. This prefix was
5233 -- prepended to the parameter list, which is actually a
5234 -- list of indexes. Remove the prefix in order to build
5235 -- the proper indexed component.
5238 Make_Indexed_Component (Loc,
5240 Make_Function_Call (Loc,
5242 Parameter_Associations =>
5244 (Remove_Head (Parameter_Associations (N)))),
5245 Expressions => Parameter_Associations (N));
5248 -- Preserve the parenthesis count of the node
5250 Set_Paren_Count (Index_Node, Paren_Count (N));
5252 -- Since we are correcting a node classification error made
5253 -- by the parser, we call Replace rather than Rewrite.
5255 Replace (N, Index_Node);
5257 Set_Etype (Prefix (N), Ret_Type);
5259 Resolve_Indexed_Component (N, Typ);
5260 Check_Elab_Call (Prefix (N));
5268 Set_Etype (N, Etype (Nam));
5271 -- In the case where the call is to an overloaded subprogram, Analyze
5272 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5273 -- such a case Normalize_Actuals needs to be called once more to order
5274 -- the actuals correctly. Otherwise the call will have the ordering
5275 -- given by the last overloaded subprogram whether this is the correct
5276 -- one being called or not.
5278 if Is_Overloaded (Subp) then
5279 Normalize_Actuals (N, Nam, False, Norm_OK);
5280 pragma Assert (Norm_OK);
5283 -- In any case, call is fully resolved now. Reset Overload flag, to
5284 -- prevent subsequent overload resolution if node is analyzed again
5286 Set_Is_Overloaded (Subp, False);
5287 Set_Is_Overloaded (N, False);
5289 -- If we are calling the current subprogram from immediately within its
5290 -- body, then that is the case where we can sometimes detect cases of
5291 -- infinite recursion statically. Do not try this in case restriction
5292 -- No_Recursion is in effect anyway, and do it only for source calls.
5294 if Comes_From_Source (N) then
5295 Scop := Current_Scope;
5297 -- Issue warning for possible infinite recursion in the absence
5298 -- of the No_Recursion restriction.
5300 if Same_Or_Aliased_Subprograms (Nam, Scop)
5301 and then not Restriction_Active (No_Recursion)
5302 and then Check_Infinite_Recursion (N)
5304 -- Here we detected and flagged an infinite recursion, so we do
5305 -- not need to test the case below for further warnings. Also we
5306 -- are all done if we now have a raise SE node.
5308 if Nkind (N) = N_Raise_Storage_Error then
5312 -- If call is to immediately containing subprogram, then check for
5313 -- the case of a possible run-time detectable infinite recursion.
5316 Scope_Loop : while Scop /= Standard_Standard loop
5317 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5319 -- Although in general case, recursion is not statically
5320 -- checkable, the case of calling an immediately containing
5321 -- subprogram is easy to catch.
5323 Check_Restriction (No_Recursion, N);
5325 -- If the recursive call is to a parameterless subprogram,
5326 -- then even if we can't statically detect infinite
5327 -- recursion, this is pretty suspicious, and we output a
5328 -- warning. Furthermore, we will try later to detect some
5329 -- cases here at run time by expanding checking code (see
5330 -- Detect_Infinite_Recursion in package Exp_Ch6).
5332 -- If the recursive call is within a handler, do not emit a
5333 -- warning, because this is a common idiom: loop until input
5334 -- is correct, catch illegal input in handler and restart.
5336 if No (First_Formal (Nam))
5337 and then Etype (Nam) = Standard_Void_Type
5338 and then not Error_Posted (N)
5339 and then Nkind (Parent (N)) /= N_Exception_Handler
5341 -- For the case of a procedure call. We give the message
5342 -- only if the call is the first statement in a sequence
5343 -- of statements, or if all previous statements are
5344 -- simple assignments. This is simply a heuristic to
5345 -- decrease false positives, without losing too many good
5346 -- warnings. The idea is that these previous statements
5347 -- may affect global variables the procedure depends on.
5348 -- We also exclude raise statements, that may arise from
5349 -- constraint checks and are probably unrelated to the
5350 -- intended control flow.
5352 if Nkind (N) = N_Procedure_Call_Statement
5353 and then Is_List_Member (N)
5359 while Present (P) loop
5361 N_Assignment_Statement,
5362 N_Raise_Constraint_Error)
5372 -- Do not give warning if we are in a conditional context
5375 K : constant Node_Kind := Nkind (Parent (N));
5377 if (K = N_Loop_Statement
5378 and then Present (Iteration_Scheme (Parent (N))))
5379 or else K = N_If_Statement
5380 or else K = N_Elsif_Part
5381 or else K = N_Case_Statement_Alternative
5387 -- Here warning is to be issued
5389 Set_Has_Recursive_Call (Nam);
5391 ("?possible infinite recursion!", N);
5393 ("\?Storage_Error may be raised at run time!", N);
5399 Scop := Scope (Scop);
5400 end loop Scope_Loop;
5404 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5406 Check_Obsolescent_2005_Entity (Nam, Subp);
5408 -- If subprogram name is a predefined operator, it was given in
5409 -- functional notation. Replace call node with operator node, so
5410 -- that actuals can be resolved appropriately.
5412 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5413 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5416 elsif Present (Alias (Nam))
5417 and then Is_Predefined_Op (Alias (Nam))
5419 Resolve_Actuals (N, Nam);
5420 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5424 -- Create a transient scope if the resulting type requires it
5426 -- There are several notable exceptions:
5428 -- a) In init procs, the transient scope overhead is not needed, and is
5429 -- even incorrect when the call is a nested initialization call for a
5430 -- component whose expansion may generate adjust calls. However, if the
5431 -- call is some other procedure call within an initialization procedure
5432 -- (for example a call to Create_Task in the init_proc of the task
5433 -- run-time record) a transient scope must be created around this call.
5435 -- b) Enumeration literal pseudo-calls need no transient scope
5437 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5438 -- functions) do not use the secondary stack even though the return
5439 -- type may be unconstrained.
5441 -- d) Calls to a build-in-place function, since such functions may
5442 -- allocate their result directly in a target object, and cases where
5443 -- the result does get allocated in the secondary stack are checked for
5444 -- within the specialized Exp_Ch6 procedures for expanding those
5445 -- build-in-place calls.
5447 -- e) If the subprogram is marked Inline_Always, then even if it returns
5448 -- an unconstrained type the call does not require use of the secondary
5449 -- stack. However, inlining will only take place if the body to inline
5450 -- is already present. It may not be available if e.g. the subprogram is
5451 -- declared in a child instance.
5453 -- If this is an initialization call for a type whose construction
5454 -- uses the secondary stack, and it is not a nested call to initialize
5455 -- a component, we do need to create a transient scope for it. We
5456 -- check for this by traversing the type in Check_Initialization_Call.
5459 and then Has_Pragma_Inline_Always (Nam)
5460 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5461 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5465 elsif Ekind (Nam) = E_Enumeration_Literal
5466 or else Is_Build_In_Place_Function (Nam)
5467 or else Is_Intrinsic_Subprogram (Nam)
5471 elsif Expander_Active
5472 and then Is_Type (Etype (Nam))
5473 and then Requires_Transient_Scope (Etype (Nam))
5475 (not Within_Init_Proc
5477 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5479 Establish_Transient_Scope (N, Sec_Stack => True);
5481 -- If the call appears within the bounds of a loop, it will
5482 -- be rewritten and reanalyzed, nothing left to do here.
5484 if Nkind (N) /= N_Function_Call then
5488 elsif Is_Init_Proc (Nam)
5489 and then not Within_Init_Proc
5491 Check_Initialization_Call (N, Nam);
5494 -- A protected function cannot be called within the definition of the
5495 -- enclosing protected type.
5497 if Is_Protected_Type (Scope (Nam))
5498 and then In_Open_Scopes (Scope (Nam))
5499 and then not Has_Completion (Scope (Nam))
5502 ("& cannot be called before end of protected definition", N, Nam);
5505 -- Propagate interpretation to actuals, and add default expressions
5508 if Present (First_Formal (Nam)) then
5509 Resolve_Actuals (N, Nam);
5511 -- Overloaded literals are rewritten as function calls, for purpose of
5512 -- resolution. After resolution, we can replace the call with the
5515 elsif Ekind (Nam) = E_Enumeration_Literal then
5516 Copy_Node (Subp, N);
5517 Resolve_Entity_Name (N, Typ);
5519 -- Avoid validation, since it is a static function call
5521 Generate_Reference (Nam, Subp);
5525 -- If the subprogram is not global, then kill all saved values and
5526 -- checks. This is a bit conservative, since in many cases we could do
5527 -- better, but it is not worth the effort. Similarly, we kill constant
5528 -- values. However we do not need to do this for internal entities
5529 -- (unless they are inherited user-defined subprograms), since they
5530 -- are not in the business of molesting local values.
5532 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5533 -- kill all checks and values for calls to global subprograms. This
5534 -- takes care of the case where an access to a local subprogram is
5535 -- taken, and could be passed directly or indirectly and then called
5536 -- from almost any context.
5538 -- Note: we do not do this step till after resolving the actuals. That
5539 -- way we still take advantage of the current value information while
5540 -- scanning the actuals.
5542 -- We suppress killing values if we are processing the nodes associated
5543 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5544 -- type kills all the values as part of analyzing the code that
5545 -- initializes the dispatch tables.
5547 if Inside_Freezing_Actions = 0
5548 and then (not Is_Library_Level_Entity (Nam)
5549 or else Suppress_Value_Tracking_On_Call
5550 (Nearest_Dynamic_Scope (Current_Scope)))
5551 and then (Comes_From_Source (Nam)
5552 or else (Present (Alias (Nam))
5553 and then Comes_From_Source (Alias (Nam))))
5555 Kill_Current_Values;
5558 -- If we are warning about unread OUT parameters, this is the place to
5559 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5560 -- after the above call to Kill_Current_Values (since that call clears
5561 -- the Last_Assignment field of all local variables).
5563 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5564 and then Comes_From_Source (N)
5565 and then In_Extended_Main_Source_Unit (N)
5572 F := First_Formal (Nam);
5573 A := First_Actual (N);
5574 while Present (F) and then Present (A) loop
5575 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5576 and then Warn_On_Modified_As_Out_Parameter (F)
5577 and then Is_Entity_Name (A)
5578 and then Present (Entity (A))
5579 and then Comes_From_Source (N)
5580 and then Safe_To_Capture_Value (N, Entity (A))
5582 Set_Last_Assignment (Entity (A), A);
5591 -- If the subprogram is a primitive operation, check whether or not
5592 -- it is a correct dispatching call.
5594 if Is_Overloadable (Nam)
5595 and then Is_Dispatching_Operation (Nam)
5597 Check_Dispatching_Call (N);
5599 elsif Ekind (Nam) /= E_Subprogram_Type
5600 and then Is_Abstract_Subprogram (Nam)
5601 and then not In_Instance
5603 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5606 -- If this is a dispatching call, generate the appropriate reference,
5607 -- for better source navigation in GPS.
5609 if Is_Overloadable (Nam)
5610 and then Present (Controlling_Argument (N))
5612 Generate_Reference (Nam, Subp, 'R');
5614 -- Normal case, not a dispatching call: generate a call reference
5617 Generate_Reference (Nam, Subp, 's');
5620 if Is_Intrinsic_Subprogram (Nam) then
5621 Check_Intrinsic_Call (N);
5624 -- Check for violation of restriction No_Specific_Termination_Handlers
5625 -- and warn on a potentially blocking call to Abort_Task.
5627 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5628 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5630 Is_RTE (Nam, RE_Specific_Handler))
5632 Check_Restriction (No_Specific_Termination_Handlers, N);
5634 elsif Is_RTE (Nam, RE_Abort_Task) then
5635 Check_Potentially_Blocking_Operation (N);
5638 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5639 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5640 -- need to check the second argument to determine whether it is an
5641 -- absolute or relative timing event.
5643 if Restriction_Check_Required (No_Relative_Delay)
5644 and then Is_RTE (Nam, RE_Set_Handler)
5645 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5647 Check_Restriction (No_Relative_Delay, N);
5650 -- Issue an error for a call to an eliminated subprogram. We skip this
5651 -- in a spec expression, e.g. a call in a default parameter value, since
5652 -- we are not really doing a call at this time. That's important because
5653 -- the spec expression may itself belong to an eliminated subprogram.
5655 if not In_Spec_Expression then
5656 Check_For_Eliminated_Subprogram (Subp, Nam);
5659 -- In formal mode, the primitive operations of a tagged type or type
5660 -- extension do not include functions that return the tagged type.
5662 -- Commented out as the call to Is_Inherited_Operation_For_Type may
5663 -- cause an error because the type entity of the parent node of
5664 -- Entity (Name (N) may not be set. ???
5665 -- So why not just add a guard ???
5667 -- if Nkind (N) = N_Function_Call
5668 -- and then Is_Tagged_Type (Etype (N))
5669 -- and then Is_Entity_Name (Name (N))
5670 -- and then Is_Inherited_Operation_For_Type
5671 -- (Entity (Name (N)), Etype (N))
5673 -- Check_SPARK_Restriction ("function not inherited", N);
5676 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5677 -- class-wide and the call dispatches on result in a context that does
5678 -- not provide a tag, the call raises Program_Error.
5680 if Nkind (N) = N_Function_Call
5681 and then In_Instance
5682 and then Is_Generic_Actual_Type (Typ)
5683 and then Is_Class_Wide_Type (Typ)
5684 and then Has_Controlling_Result (Nam)
5685 and then Nkind (Parent (N)) = N_Object_Declaration
5687 -- Verify that none of the formals are controlling
5690 Call_OK : Boolean := False;
5694 F := First_Formal (Nam);
5695 while Present (F) loop
5696 if Is_Controlling_Formal (F) then
5705 Error_Msg_N ("!? cannot determine tag of result", N);
5706 Error_Msg_N ("!? Program_Error will be raised", N);
5708 Make_Raise_Program_Error (Sloc (N),
5709 Reason => PE_Explicit_Raise));
5714 -- All done, evaluate call and deal with elaboration issues
5717 Check_Elab_Call (N);
5718 Warn_On_Overlapping_Actuals (Nam, N);
5721 -----------------------------
5722 -- Resolve_Case_Expression --
5723 -----------------------------
5725 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5729 Alt := First (Alternatives (N));
5730 while Present (Alt) loop
5731 Resolve (Expression (Alt), Typ);
5736 Eval_Case_Expression (N);
5737 end Resolve_Case_Expression;
5739 -------------------------------
5740 -- Resolve_Character_Literal --
5741 -------------------------------
5743 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5744 B_Typ : constant Entity_Id := Base_Type (Typ);
5748 -- Verify that the character does belong to the type of the context
5750 Set_Etype (N, B_Typ);
5751 Eval_Character_Literal (N);
5753 -- Wide_Wide_Character literals must always be defined, since the set
5754 -- of wide wide character literals is complete, i.e. if a character
5755 -- literal is accepted by the parser, then it is OK for wide wide
5756 -- character (out of range character literals are rejected).
5758 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5761 -- Always accept character literal for type Any_Character, which
5762 -- occurs in error situations and in comparisons of literals, both
5763 -- of which should accept all literals.
5765 elsif B_Typ = Any_Character then
5768 -- For Standard.Character or a type derived from it, check that the
5769 -- literal is in range.
5771 elsif Root_Type (B_Typ) = Standard_Character then
5772 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5776 -- For Standard.Wide_Character or a type derived from it, check that the
5777 -- literal is in range.
5779 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5780 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5784 -- For Standard.Wide_Wide_Character or a type derived from it, we
5785 -- know the literal is in range, since the parser checked!
5787 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5790 -- If the entity is already set, this has already been resolved in a
5791 -- generic context, or comes from expansion. Nothing else to do.
5793 elsif Present (Entity (N)) then
5796 -- Otherwise we have a user defined character type, and we can use the
5797 -- standard visibility mechanisms to locate the referenced entity.
5800 C := Current_Entity (N);
5801 while Present (C) loop
5802 if Etype (C) = B_Typ then
5803 Set_Entity_With_Style_Check (N, C);
5804 Generate_Reference (C, N);
5812 -- If we fall through, then the literal does not match any of the
5813 -- entries of the enumeration type. This isn't just a constraint error
5814 -- situation, it is an illegality (see RM 4.2).
5817 ("character not defined for }", N, First_Subtype (B_Typ));
5818 end Resolve_Character_Literal;
5820 ---------------------------
5821 -- Resolve_Comparison_Op --
5822 ---------------------------
5824 -- Context requires a boolean type, and plays no role in resolution.
5825 -- Processing identical to that for equality operators. The result type is
5826 -- the base type, which matters when pathological subtypes of booleans with
5827 -- limited ranges are used.
5829 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5830 L : constant Node_Id := Left_Opnd (N);
5831 R : constant Node_Id := Right_Opnd (N);
5835 -- If this is an intrinsic operation which is not predefined, use the
5836 -- types of its declared arguments to resolve the possibly overloaded
5837 -- operands. Otherwise the operands are unambiguous and specify the
5840 if Scope (Entity (N)) /= Standard_Standard then
5841 T := Etype (First_Entity (Entity (N)));
5844 T := Find_Unique_Type (L, R);
5846 if T = Any_Fixed then
5847 T := Unique_Fixed_Point_Type (L);
5851 Set_Etype (N, Base_Type (Typ));
5852 Generate_Reference (T, N, ' ');
5854 -- Skip remaining processing if already set to Any_Type
5856 if T = Any_Type then
5860 -- Deal with other error cases
5862 if T = Any_String or else
5863 T = Any_Composite or else
5866 if T = Any_Character then
5867 Ambiguous_Character (L);
5869 Error_Msg_N ("ambiguous operands for comparison", N);
5872 Set_Etype (N, Any_Type);
5876 -- Resolve the operands if types OK
5880 Check_Unset_Reference (L);
5881 Check_Unset_Reference (R);
5882 Generate_Operator_Reference (N, T);
5883 Check_Low_Bound_Tested (N);
5885 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
5886 -- types or array types except String.
5888 if Is_Boolean_Type (T) then
5889 Check_SPARK_Restriction
5890 ("comparison is not defined on Boolean type", N);
5892 elsif Is_Array_Type (T)
5893 and then Base_Type (T) /= Standard_String
5895 Check_SPARK_Restriction
5896 ("comparison is not defined on array types other than String", N);
5899 -- Check comparison on unordered enumeration
5901 if Comes_From_Source (N)
5902 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
5904 Error_Msg_N ("comparison on unordered enumeration type?", N);
5907 -- Evaluate the relation (note we do this after the above check since
5908 -- this Eval call may change N to True/False.
5910 Eval_Relational_Op (N);
5911 end Resolve_Comparison_Op;
5913 ------------------------------------
5914 -- Resolve_Conditional_Expression --
5915 ------------------------------------
5917 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5918 Condition : constant Node_Id := First (Expressions (N));
5919 Then_Expr : constant Node_Id := Next (Condition);
5920 Else_Expr : Node_Id := Next (Then_Expr);
5923 Resolve (Condition, Any_Boolean);
5924 Resolve (Then_Expr, Typ);
5926 -- If ELSE expression present, just resolve using the determined type
5928 if Present (Else_Expr) then
5929 Resolve (Else_Expr, Typ);
5931 -- If no ELSE expression is present, root type must be Standard.Boolean
5932 -- and we provide a Standard.True result converted to the appropriate
5933 -- Boolean type (in case it is a derived boolean type).
5935 elsif Root_Type (Typ) = Standard_Boolean then
5937 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
5938 Analyze_And_Resolve (Else_Expr, Typ);
5939 Append_To (Expressions (N), Else_Expr);
5942 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
5943 Append_To (Expressions (N), Error);
5947 Eval_Conditional_Expression (N);
5948 end Resolve_Conditional_Expression;
5950 -----------------------------------------
5951 -- Resolve_Discrete_Subtype_Indication --
5952 -----------------------------------------
5954 procedure Resolve_Discrete_Subtype_Indication
5962 Analyze (Subtype_Mark (N));
5963 S := Entity (Subtype_Mark (N));
5965 if Nkind (Constraint (N)) /= N_Range_Constraint then
5966 Error_Msg_N ("expect range constraint for discrete type", N);
5967 Set_Etype (N, Any_Type);
5970 R := Range_Expression (Constraint (N));
5978 if Base_Type (S) /= Base_Type (Typ) then
5980 ("expect subtype of }", N, First_Subtype (Typ));
5982 -- Rewrite the constraint as a range of Typ
5983 -- to allow compilation to proceed further.
5986 Rewrite (Low_Bound (R),
5987 Make_Attribute_Reference (Sloc (Low_Bound (R)),
5988 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5989 Attribute_Name => Name_First));
5990 Rewrite (High_Bound (R),
5991 Make_Attribute_Reference (Sloc (High_Bound (R)),
5992 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5993 Attribute_Name => Name_First));
5997 Set_Etype (N, Etype (R));
5999 -- Additionally, we must check that the bounds are compatible
6000 -- with the given subtype, which might be different from the
6001 -- type of the context.
6003 Apply_Range_Check (R, S);
6005 -- ??? If the above check statically detects a Constraint_Error
6006 -- it replaces the offending bound(s) of the range R with a
6007 -- Constraint_Error node. When the itype which uses these bounds
6008 -- is frozen the resulting call to Duplicate_Subexpr generates
6009 -- a new temporary for the bounds.
6011 -- Unfortunately there are other itypes that are also made depend
6012 -- on these bounds, so when Duplicate_Subexpr is called they get
6013 -- a forward reference to the newly created temporaries and Gigi
6014 -- aborts on such forward references. This is probably sign of a
6015 -- more fundamental problem somewhere else in either the order of
6016 -- itype freezing or the way certain itypes are constructed.
6018 -- To get around this problem we call Remove_Side_Effects right
6019 -- away if either bounds of R are a Constraint_Error.
6022 L : constant Node_Id := Low_Bound (R);
6023 H : constant Node_Id := High_Bound (R);
6026 if Nkind (L) = N_Raise_Constraint_Error then
6027 Remove_Side_Effects (L);
6030 if Nkind (H) = N_Raise_Constraint_Error then
6031 Remove_Side_Effects (H);
6035 Check_Unset_Reference (Low_Bound (R));
6036 Check_Unset_Reference (High_Bound (R));
6039 end Resolve_Discrete_Subtype_Indication;
6041 -------------------------
6042 -- Resolve_Entity_Name --
6043 -------------------------
6045 -- Used to resolve identifiers and expanded names
6047 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6048 E : constant Entity_Id := Entity (N);
6051 -- If garbage from errors, set to Any_Type and return
6053 if No (E) and then Total_Errors_Detected /= 0 then
6054 Set_Etype (N, Any_Type);
6058 -- Replace named numbers by corresponding literals. Note that this is
6059 -- the one case where Resolve_Entity_Name must reset the Etype, since
6060 -- it is currently marked as universal.
6062 if Ekind (E) = E_Named_Integer then
6064 Eval_Named_Integer (N);
6066 elsif Ekind (E) = E_Named_Real then
6068 Eval_Named_Real (N);
6070 -- For enumeration literals, we need to make sure that a proper style
6071 -- check is done, since such literals are overloaded, and thus we did
6072 -- not do a style check during the first phase of analysis.
6074 elsif Ekind (E) = E_Enumeration_Literal then
6075 Set_Entity_With_Style_Check (N, E);
6076 Eval_Entity_Name (N);
6078 -- Case of subtype name appearing as an operand in expression
6080 elsif Is_Type (E) then
6082 -- Allow use of subtype if it is a concurrent type where we are
6083 -- currently inside the body. This will eventually be expanded into a
6084 -- call to Self (for tasks) or _object (for protected objects). Any
6085 -- other use of a subtype is invalid.
6087 if Is_Concurrent_Type (E)
6088 and then In_Open_Scopes (E)
6092 -- Any other use is an error
6096 ("invalid use of subtype mark in expression or call", N);
6099 -- Check discriminant use if entity is discriminant in current scope,
6100 -- i.e. discriminant of record or concurrent type currently being
6101 -- analyzed. Uses in corresponding body are unrestricted.
6103 elsif Ekind (E) = E_Discriminant
6104 and then Scope (E) = Current_Scope
6105 and then not Has_Completion (Current_Scope)
6107 Check_Discriminant_Use (N);
6109 -- A parameterless generic function cannot appear in a context that
6110 -- requires resolution.
6112 elsif Ekind (E) = E_Generic_Function then
6113 Error_Msg_N ("illegal use of generic function", N);
6115 elsif Ekind (E) = E_Out_Parameter
6116 and then Ada_Version = Ada_83
6117 and then (Nkind (Parent (N)) in N_Op
6118 or else (Nkind (Parent (N)) = N_Assignment_Statement
6119 and then N = Expression (Parent (N)))
6120 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6122 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6124 -- In all other cases, just do the possible static evaluation
6127 -- A deferred constant that appears in an expression must have a
6128 -- completion, unless it has been removed by in-place expansion of
6131 if Ekind (E) = E_Constant
6132 and then Comes_From_Source (E)
6133 and then No (Constant_Value (E))
6134 and then Is_Frozen (Etype (E))
6135 and then not In_Spec_Expression
6136 and then not Is_Imported (E)
6138 if No_Initialization (Parent (E))
6139 or else (Present (Full_View (E))
6140 and then No_Initialization (Parent (Full_View (E))))
6145 "deferred constant is frozen before completion", N);
6149 Eval_Entity_Name (N);
6151 end Resolve_Entity_Name;
6157 procedure Resolve_Entry (Entry_Name : Node_Id) is
6158 Loc : constant Source_Ptr := Sloc (Entry_Name);
6166 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6167 -- If the bounds of the entry family being called depend on task
6168 -- discriminants, build a new index subtype where a discriminant is
6169 -- replaced with the value of the discriminant of the target task.
6170 -- The target task is the prefix of the entry name in the call.
6172 -----------------------
6173 -- Actual_Index_Type --
6174 -----------------------
6176 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6177 Typ : constant Entity_Id := Entry_Index_Type (E);
6178 Tsk : constant Entity_Id := Scope (E);
6179 Lo : constant Node_Id := Type_Low_Bound (Typ);
6180 Hi : constant Node_Id := Type_High_Bound (Typ);
6183 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6184 -- If the bound is given by a discriminant, replace with a reference
6185 -- to the discriminant of the same name in the target task. If the
6186 -- entry name is the target of a requeue statement and the entry is
6187 -- in the current protected object, the bound to be used is the
6188 -- discriminal of the object (see Apply_Range_Checks for details of
6189 -- the transformation).
6191 -----------------------------
6192 -- Actual_Discriminant_Ref --
6193 -----------------------------
6195 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6196 Typ : constant Entity_Id := Etype (Bound);
6200 Remove_Side_Effects (Bound);
6202 if not Is_Entity_Name (Bound)
6203 or else Ekind (Entity (Bound)) /= E_Discriminant
6207 elsif Is_Protected_Type (Tsk)
6208 and then In_Open_Scopes (Tsk)
6209 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6211 -- Note: here Bound denotes a discriminant of the corresponding
6212 -- record type tskV, whose discriminal is a formal of the
6213 -- init-proc tskVIP. What we want is the body discriminal,
6214 -- which is associated to the discriminant of the original
6215 -- concurrent type tsk.
6217 return New_Occurrence_Of
6218 (Find_Body_Discriminal (Entity (Bound)), Loc);
6222 Make_Selected_Component (Loc,
6223 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6224 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6229 end Actual_Discriminant_Ref;
6231 -- Start of processing for Actual_Index_Type
6234 if not Has_Discriminants (Tsk)
6235 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6237 return Entry_Index_Type (E);
6240 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6241 Set_Etype (New_T, Base_Type (Typ));
6242 Set_Size_Info (New_T, Typ);
6243 Set_RM_Size (New_T, RM_Size (Typ));
6244 Set_Scalar_Range (New_T,
6245 Make_Range (Sloc (Entry_Name),
6246 Low_Bound => Actual_Discriminant_Ref (Lo),
6247 High_Bound => Actual_Discriminant_Ref (Hi)));
6251 end Actual_Index_Type;
6253 -- Start of processing of Resolve_Entry
6256 -- Find name of entry being called, and resolve prefix of name with its
6257 -- own type. The prefix can be overloaded, and the name and signature of
6258 -- the entry must be taken into account.
6260 if Nkind (Entry_Name) = N_Indexed_Component then
6262 -- Case of dealing with entry family within the current tasks
6264 E_Name := Prefix (Entry_Name);
6267 E_Name := Entry_Name;
6270 if Is_Entity_Name (E_Name) then
6272 -- Entry call to an entry (or entry family) in the current task. This
6273 -- is legal even though the task will deadlock. Rewrite as call to
6276 -- This can also be a call to an entry in an enclosing task. If this
6277 -- is a single task, we have to retrieve its name, because the scope
6278 -- of the entry is the task type, not the object. If the enclosing
6279 -- task is a task type, the identity of the task is given by its own
6282 -- Finally this can be a requeue on an entry of the same task or
6283 -- protected object.
6285 S := Scope (Entity (E_Name));
6287 for J in reverse 0 .. Scope_Stack.Last loop
6288 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6289 and then not Comes_From_Source (S)
6291 -- S is an enclosing task or protected object. The concurrent
6292 -- declaration has been converted into a type declaration, and
6293 -- the object itself has an object declaration that follows
6294 -- the type in the same declarative part.
6296 Tsk := Next_Entity (S);
6297 while Etype (Tsk) /= S loop
6304 elsif S = Scope_Stack.Table (J).Entity then
6306 -- Call to current task. Will be transformed into call to Self
6314 Make_Selected_Component (Loc,
6315 Prefix => New_Occurrence_Of (S, Loc),
6317 New_Occurrence_Of (Entity (E_Name), Loc));
6318 Rewrite (E_Name, New_N);
6321 elsif Nkind (Entry_Name) = N_Selected_Component
6322 and then Is_Overloaded (Prefix (Entry_Name))
6324 -- Use the entry name (which must be unique at this point) to find
6325 -- the prefix that returns the corresponding task/protected type.
6328 Pref : constant Node_Id := Prefix (Entry_Name);
6329 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6334 Get_First_Interp (Pref, I, It);
6335 while Present (It.Typ) loop
6336 if Scope (Ent) = It.Typ then
6337 Set_Etype (Pref, It.Typ);
6341 Get_Next_Interp (I, It);
6346 if Nkind (Entry_Name) = N_Selected_Component then
6347 Resolve (Prefix (Entry_Name));
6349 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6350 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6351 Resolve (Prefix (Prefix (Entry_Name)));
6352 Index := First (Expressions (Entry_Name));
6353 Resolve (Index, Entry_Index_Type (Nam));
6355 -- Up to this point the expression could have been the actual in a
6356 -- simple entry call, and be given by a named association.
6358 if Nkind (Index) = N_Parameter_Association then
6359 Error_Msg_N ("expect expression for entry index", Index);
6361 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6366 ------------------------
6367 -- Resolve_Entry_Call --
6368 ------------------------
6370 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6371 Entry_Name : constant Node_Id := Name (N);
6372 Loc : constant Source_Ptr := Sloc (Entry_Name);
6374 First_Named : Node_Id;
6381 -- We kill all checks here, because it does not seem worth the effort to
6382 -- do anything better, an entry call is a big operation.
6386 -- Processing of the name is similar for entry calls and protected
6387 -- operation calls. Once the entity is determined, we can complete
6388 -- the resolution of the actuals.
6390 -- The selector may be overloaded, in the case of a protected object
6391 -- with overloaded functions. The type of the context is used for
6394 if Nkind (Entry_Name) = N_Selected_Component
6395 and then Is_Overloaded (Selector_Name (Entry_Name))
6396 and then Typ /= Standard_Void_Type
6403 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6404 while Present (It.Typ) loop
6405 if Covers (Typ, It.Typ) then
6406 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6407 Set_Etype (Entry_Name, It.Typ);
6409 Generate_Reference (It.Typ, N, ' ');
6412 Get_Next_Interp (I, It);
6417 Resolve_Entry (Entry_Name);
6419 if Nkind (Entry_Name) = N_Selected_Component then
6421 -- Simple entry call
6423 Nam := Entity (Selector_Name (Entry_Name));
6424 Obj := Prefix (Entry_Name);
6425 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6427 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6429 -- Call to member of entry family
6431 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6432 Obj := Prefix (Prefix (Entry_Name));
6433 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6436 -- We cannot in general check the maximum depth of protected entry calls
6437 -- at compile time. But we can tell that any protected entry call at all
6438 -- violates a specified nesting depth of zero.
6440 if Is_Protected_Type (Scope (Nam)) then
6441 Check_Restriction (Max_Entry_Queue_Length, N);
6444 -- Use context type to disambiguate a protected function that can be
6445 -- called without actuals and that returns an array type, and where the
6446 -- argument list may be an indexing of the returned value.
6448 if Ekind (Nam) = E_Function
6449 and then Needs_No_Actuals (Nam)
6450 and then Present (Parameter_Associations (N))
6452 ((Is_Array_Type (Etype (Nam))
6453 and then Covers (Typ, Component_Type (Etype (Nam))))
6455 or else (Is_Access_Type (Etype (Nam))
6456 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6460 Component_Type (Designated_Type (Etype (Nam))))))
6463 Index_Node : Node_Id;
6467 Make_Indexed_Component (Loc,
6469 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6470 Expressions => Parameter_Associations (N));
6472 -- Since we are correcting a node classification error made by the
6473 -- parser, we call Replace rather than Rewrite.
6475 Replace (N, Index_Node);
6476 Set_Etype (Prefix (N), Etype (Nam));
6478 Resolve_Indexed_Component (N, Typ);
6483 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6484 and then Present (PPC_Wrapper (Nam))
6485 and then Current_Scope /= PPC_Wrapper (Nam)
6487 -- Rewrite as call to the precondition wrapper, adding the task
6488 -- object to the list of actuals. If the call is to a member of an
6489 -- entry family, include the index as well.
6493 New_Actuals : List_Id;
6496 New_Actuals := New_List (Obj);
6498 if Nkind (Entry_Name) = N_Indexed_Component then
6499 Append_To (New_Actuals,
6500 New_Copy_Tree (First (Expressions (Entry_Name))));
6503 Append_List (Parameter_Associations (N), New_Actuals);
6505 Make_Procedure_Call_Statement (Loc,
6507 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6508 Parameter_Associations => New_Actuals);
6509 Rewrite (N, New_Call);
6510 Analyze_And_Resolve (N);
6515 -- The operation name may have been overloaded. Order the actuals
6516 -- according to the formals of the resolved entity, and set the return
6517 -- type to that of the operation.
6520 Normalize_Actuals (N, Nam, False, Norm_OK);
6521 pragma Assert (Norm_OK);
6522 Set_Etype (N, Etype (Nam));
6525 Resolve_Actuals (N, Nam);
6527 -- Create a call reference to the entry
6529 Generate_Reference (Nam, Entry_Name, 's');
6531 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6532 Check_Potentially_Blocking_Operation (N);
6535 -- Verify that a procedure call cannot masquerade as an entry
6536 -- call where an entry call is expected.
6538 if Ekind (Nam) = E_Procedure then
6539 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6540 and then N = Entry_Call_Statement (Parent (N))
6542 Error_Msg_N ("entry call required in select statement", N);
6544 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6545 and then N = Triggering_Statement (Parent (N))
6547 Error_Msg_N ("triggering statement cannot be procedure call", N);
6549 elsif Ekind (Scope (Nam)) = E_Task_Type
6550 and then not In_Open_Scopes (Scope (Nam))
6552 Error_Msg_N ("task has no entry with this name", Entry_Name);
6556 -- After resolution, entry calls and protected procedure calls are
6557 -- changed into entry calls, for expansion. The structure of the node
6558 -- does not change, so it can safely be done in place. Protected
6559 -- function calls must keep their structure because they are
6562 if Ekind (Nam) /= E_Function then
6564 -- A protected operation that is not a function may modify the
6565 -- corresponding object, and cannot apply to a constant. If this
6566 -- is an internal call, the prefix is the type itself.
6568 if Is_Protected_Type (Scope (Nam))
6569 and then not Is_Variable (Obj)
6570 and then (not Is_Entity_Name (Obj)
6571 or else not Is_Type (Entity (Obj)))
6574 ("prefix of protected procedure or entry call must be variable",
6578 Actuals := Parameter_Associations (N);
6579 First_Named := First_Named_Actual (N);
6582 Make_Entry_Call_Statement (Loc,
6584 Parameter_Associations => Actuals));
6586 Set_First_Named_Actual (N, First_Named);
6587 Set_Analyzed (N, True);
6589 -- Protected functions can return on the secondary stack, in which
6590 -- case we must trigger the transient scope mechanism.
6592 elsif Expander_Active
6593 and then Requires_Transient_Scope (Etype (Nam))
6595 Establish_Transient_Scope (N, Sec_Stack => True);
6597 end Resolve_Entry_Call;
6599 -------------------------
6600 -- Resolve_Equality_Op --
6601 -------------------------
6603 -- Both arguments must have the same type, and the boolean context does
6604 -- not participate in the resolution. The first pass verifies that the
6605 -- interpretation is not ambiguous, and the type of the left argument is
6606 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6607 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6608 -- though they carry a single (universal) type. Diagnose this case here.
6610 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6611 L : constant Node_Id := Left_Opnd (N);
6612 R : constant Node_Id := Right_Opnd (N);
6613 T : Entity_Id := Find_Unique_Type (L, R);
6615 procedure Check_Conditional_Expression (Cond : Node_Id);
6616 -- The resolution rule for conditional expressions requires that each
6617 -- such must have a unique type. This means that if several dependent
6618 -- expressions are of a non-null anonymous access type, and the context
6619 -- does not impose an expected type (as can be the case in an equality
6620 -- operation) the expression must be rejected.
6622 function Find_Unique_Access_Type return Entity_Id;
6623 -- In the case of allocators, make a last-ditch attempt to find a single
6624 -- access type with the right designated type. This is semantically
6625 -- dubious, and of no interest to any real code, but c48008a makes it
6628 ----------------------------------
6629 -- Check_Conditional_Expression --
6630 ----------------------------------
6632 procedure Check_Conditional_Expression (Cond : Node_Id) is
6633 Then_Expr : Node_Id;
6634 Else_Expr : Node_Id;
6637 if Nkind (Cond) = N_Conditional_Expression then
6638 Then_Expr := Next (First (Expressions (Cond)));
6639 Else_Expr := Next (Then_Expr);
6641 if Nkind (Then_Expr) /= N_Null
6642 and then Nkind (Else_Expr) /= N_Null
6645 ("cannot determine type of conditional expression", Cond);
6648 end Check_Conditional_Expression;
6650 -----------------------------
6651 -- Find_Unique_Access_Type --
6652 -----------------------------
6654 function Find_Unique_Access_Type return Entity_Id is
6660 if Ekind (Etype (R)) = E_Allocator_Type then
6661 Acc := Designated_Type (Etype (R));
6662 elsif Ekind (Etype (L)) = E_Allocator_Type then
6663 Acc := Designated_Type (Etype (L));
6669 while S /= Standard_Standard loop
6670 E := First_Entity (S);
6671 while Present (E) loop
6673 and then Is_Access_Type (E)
6674 and then Ekind (E) /= E_Allocator_Type
6675 and then Designated_Type (E) = Base_Type (Acc)
6687 end Find_Unique_Access_Type;
6689 -- Start of processing for Resolve_Equality_Op
6692 Set_Etype (N, Base_Type (Typ));
6693 Generate_Reference (T, N, ' ');
6695 if T = Any_Fixed then
6696 T := Unique_Fixed_Point_Type (L);
6699 if T /= Any_Type then
6700 if T = Any_String or else
6701 T = Any_Composite or else
6704 if T = Any_Character then
6705 Ambiguous_Character (L);
6707 Error_Msg_N ("ambiguous operands for equality", N);
6710 Set_Etype (N, Any_Type);
6713 elsif T = Any_Access
6714 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6716 T := Find_Unique_Access_Type;
6719 Error_Msg_N ("ambiguous operands for equality", N);
6720 Set_Etype (N, Any_Type);
6724 -- Conditional expressions must have a single type, and if the
6725 -- context does not impose one the dependent expressions cannot
6726 -- be anonymous access types.
6728 elsif Ada_Version >= Ada_2012
6729 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6730 E_Anonymous_Access_Subprogram_Type)
6731 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6732 E_Anonymous_Access_Subprogram_Type)
6734 Check_Conditional_Expression (L);
6735 Check_Conditional_Expression (R);
6741 -- In SPARK, equality operators = and /= for array types other than
6742 -- String are only defined when, for each index position, the
6743 -- operands have equal static bounds.
6745 if Is_Array_Type (T) then
6746 -- Protect call to Matching_Static_Array_Bounds to avoid costly
6747 -- operation if not needed.
6749 if Restriction_Check_Required (SPARK)
6750 and then Base_Type (T) /= Standard_String
6751 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6752 and then Etype (L) /= Any_Composite -- or else L in error
6753 and then Etype (R) /= Any_Composite -- or else R in error
6754 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6756 Check_SPARK_Restriction
6757 ("array types should have matching static bounds", N);
6761 -- If the unique type is a class-wide type then it will be expanded
6762 -- into a dispatching call to the predefined primitive. Therefore we
6763 -- check here for potential violation of such restriction.
6765 if Is_Class_Wide_Type (T) then
6766 Check_Restriction (No_Dispatching_Calls, N);
6769 if Warn_On_Redundant_Constructs
6770 and then Comes_From_Source (N)
6771 and then Is_Entity_Name (R)
6772 and then Entity (R) = Standard_True
6773 and then Comes_From_Source (R)
6775 Error_Msg_N -- CODEFIX
6776 ("?comparison with True is redundant!", R);
6779 Check_Unset_Reference (L);
6780 Check_Unset_Reference (R);
6781 Generate_Operator_Reference (N, T);
6782 Check_Low_Bound_Tested (N);
6784 -- If this is an inequality, it may be the implicit inequality
6785 -- created for a user-defined operation, in which case the corres-
6786 -- ponding equality operation is not intrinsic, and the operation
6787 -- cannot be constant-folded. Else fold.
6789 if Nkind (N) = N_Op_Eq
6790 or else Comes_From_Source (Entity (N))
6791 or else Ekind (Entity (N)) = E_Operator
6792 or else Is_Intrinsic_Subprogram
6793 (Corresponding_Equality (Entity (N)))
6795 Eval_Relational_Op (N);
6797 elsif Nkind (N) = N_Op_Ne
6798 and then Is_Abstract_Subprogram (Entity (N))
6800 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6803 -- Ada 2005: If one operand is an anonymous access type, convert the
6804 -- other operand to it, to ensure that the underlying types match in
6805 -- the back-end. Same for access_to_subprogram, and the conversion
6806 -- verifies that the types are subtype conformant.
6808 -- We apply the same conversion in the case one of the operands is a
6809 -- private subtype of the type of the other.
6811 -- Why the Expander_Active test here ???
6815 (Ekind_In (T, E_Anonymous_Access_Type,
6816 E_Anonymous_Access_Subprogram_Type)
6817 or else Is_Private_Type (T))
6819 if Etype (L) /= T then
6821 Make_Unchecked_Type_Conversion (Sloc (L),
6822 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6823 Expression => Relocate_Node (L)));
6824 Analyze_And_Resolve (L, T);
6827 if (Etype (R)) /= T then
6829 Make_Unchecked_Type_Conversion (Sloc (R),
6830 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6831 Expression => Relocate_Node (R)));
6832 Analyze_And_Resolve (R, T);
6836 end Resolve_Equality_Op;
6838 ----------------------------------
6839 -- Resolve_Explicit_Dereference --
6840 ----------------------------------
6842 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6843 Loc : constant Source_Ptr := Sloc (N);
6845 P : constant Node_Id := Prefix (N);
6850 Check_Fully_Declared_Prefix (Typ, P);
6852 if Is_Overloaded (P) then
6854 -- Use the context type to select the prefix that has the correct
6857 Get_First_Interp (P, I, It);
6858 while Present (It.Typ) loop
6859 exit when Is_Access_Type (It.Typ)
6860 and then Covers (Typ, Designated_Type (It.Typ));
6861 Get_Next_Interp (I, It);
6864 if Present (It.Typ) then
6865 Resolve (P, It.Typ);
6867 -- If no interpretation covers the designated type of the prefix,
6868 -- this is the pathological case where not all implementations of
6869 -- the prefix allow the interpretation of the node as a call. Now
6870 -- that the expected type is known, Remove other interpretations
6871 -- from prefix, rewrite it as a call, and resolve again, so that
6872 -- the proper call node is generated.
6874 Get_First_Interp (P, I, It);
6875 while Present (It.Typ) loop
6876 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6880 Get_Next_Interp (I, It);
6884 Make_Function_Call (Loc,
6886 Make_Explicit_Dereference (Loc,
6888 Parameter_Associations => New_List);
6890 Save_Interps (N, New_N);
6892 Analyze_And_Resolve (N, Typ);
6896 Set_Etype (N, Designated_Type (It.Typ));
6902 if Is_Access_Type (Etype (P)) then
6903 Apply_Access_Check (N);
6906 -- If the designated type is a packed unconstrained array type, and the
6907 -- explicit dereference is not in the context of an attribute reference,
6908 -- then we must compute and set the actual subtype, since it is needed
6909 -- by Gigi. The reason we exclude the attribute case is that this is
6910 -- handled fine by Gigi, and in fact we use such attributes to build the
6911 -- actual subtype. We also exclude generated code (which builds actual
6912 -- subtypes directly if they are needed).
6914 if Is_Array_Type (Etype (N))
6915 and then Is_Packed (Etype (N))
6916 and then not Is_Constrained (Etype (N))
6917 and then Nkind (Parent (N)) /= N_Attribute_Reference
6918 and then Comes_From_Source (N)
6920 Set_Etype (N, Get_Actual_Subtype (N));
6923 -- Note: No Eval processing is required for an explicit dereference,
6924 -- because such a name can never be static.
6926 end Resolve_Explicit_Dereference;
6928 -------------------------------------
6929 -- Resolve_Expression_With_Actions --
6930 -------------------------------------
6932 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
6935 end Resolve_Expression_With_Actions;
6937 -------------------------------
6938 -- Resolve_Indexed_Component --
6939 -------------------------------
6941 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6942 Name : constant Node_Id := Prefix (N);
6944 Array_Type : Entity_Id := Empty; -- to prevent junk warning
6948 if Is_Overloaded (Name) then
6950 -- Use the context type to select the prefix that yields the correct
6956 I1 : Interp_Index := 0;
6957 P : constant Node_Id := Prefix (N);
6958 Found : Boolean := False;
6961 Get_First_Interp (P, I, It);
6962 while Present (It.Typ) loop
6963 if (Is_Array_Type (It.Typ)
6964 and then Covers (Typ, Component_Type (It.Typ)))
6965 or else (Is_Access_Type (It.Typ)
6966 and then Is_Array_Type (Designated_Type (It.Typ))
6970 Component_Type (Designated_Type (It.Typ))))
6973 It := Disambiguate (P, I1, I, Any_Type);
6975 if It = No_Interp then
6976 Error_Msg_N ("ambiguous prefix for indexing", N);
6982 Array_Type := It.Typ;
6988 Array_Type := It.Typ;
6993 Get_Next_Interp (I, It);
6998 Array_Type := Etype (Name);
7001 Resolve (Name, Array_Type);
7002 Array_Type := Get_Actual_Subtype_If_Available (Name);
7004 -- If prefix is access type, dereference to get real array type.
7005 -- Note: we do not apply an access check because the expander always
7006 -- introduces an explicit dereference, and the check will happen there.
7008 if Is_Access_Type (Array_Type) then
7009 Array_Type := Designated_Type (Array_Type);
7012 -- If name was overloaded, set component type correctly now
7013 -- If a misplaced call to an entry family (which has no index types)
7014 -- return. Error will be diagnosed from calling context.
7016 if Is_Array_Type (Array_Type) then
7017 Set_Etype (N, Component_Type (Array_Type));
7022 Index := First_Index (Array_Type);
7023 Expr := First (Expressions (N));
7025 -- The prefix may have resolved to a string literal, in which case its
7026 -- etype has a special representation. This is only possible currently
7027 -- if the prefix is a static concatenation, written in functional
7030 if Ekind (Array_Type) = E_String_Literal_Subtype then
7031 Resolve (Expr, Standard_Positive);
7034 while Present (Index) and Present (Expr) loop
7035 Resolve (Expr, Etype (Index));
7036 Check_Unset_Reference (Expr);
7038 if Is_Scalar_Type (Etype (Expr)) then
7039 Apply_Scalar_Range_Check (Expr, Etype (Index));
7041 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7049 -- Do not generate the warning on suspicious index if we are analyzing
7050 -- package Ada.Tags; otherwise we will report the warning with the
7051 -- Prims_Ptr field of the dispatch table.
7053 if Scope (Etype (Prefix (N))) = Standard_Standard
7055 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7058 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7059 Eval_Indexed_Component (N);
7062 -- If the array type is atomic, and is packed, and we are in a left side
7063 -- context, then this is worth a warning, since we have a situation
7064 -- where the access to the component may cause extra read/writes of
7065 -- the atomic array object, which could be considered unexpected.
7067 if Nkind (N) = N_Indexed_Component
7068 and then (Is_Atomic (Array_Type)
7069 or else (Is_Entity_Name (Prefix (N))
7070 and then Is_Atomic (Entity (Prefix (N)))))
7071 and then Is_Bit_Packed_Array (Array_Type)
7074 Error_Msg_N ("?assignment to component of packed atomic array",
7076 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7079 end Resolve_Indexed_Component;
7081 -----------------------------
7082 -- Resolve_Integer_Literal --
7083 -----------------------------
7085 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7088 Eval_Integer_Literal (N);
7089 end Resolve_Integer_Literal;
7091 --------------------------------
7092 -- Resolve_Intrinsic_Operator --
7093 --------------------------------
7095 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7096 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7098 Orig_Op : constant Entity_Id := Entity (N);
7102 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7103 -- If the operand is a literal, it cannot be the expression in a
7104 -- conversion. Use a qualified expression instead.
7106 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7107 Loc : constant Source_Ptr := Sloc (Opnd);
7110 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7112 Make_Qualified_Expression (Loc,
7113 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7114 Expression => Relocate_Node (Opnd));
7118 Res := Unchecked_Convert_To (Btyp, Opnd);
7122 end Convert_Operand;
7125 -- We must preserve the original entity in a generic setting, so that
7126 -- the legality of the operation can be verified in an instance.
7128 if not Expander_Active then
7133 while Scope (Op) /= Standard_Standard loop
7135 pragma Assert (Present (Op));
7139 Set_Is_Overloaded (N, False);
7141 -- If the operand type is private, rewrite with suitable conversions on
7142 -- the operands and the result, to expose the proper underlying numeric
7145 if Is_Private_Type (Typ) then
7146 Arg1 := Convert_Operand (Left_Opnd (N));
7147 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7149 if Nkind (N) = N_Op_Expon then
7150 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7152 Arg2 := Convert_Operand (Right_Opnd (N));
7155 if Nkind (Arg1) = N_Type_Conversion then
7156 Save_Interps (Left_Opnd (N), Expression (Arg1));
7159 if Nkind (Arg2) = N_Type_Conversion then
7160 Save_Interps (Right_Opnd (N), Expression (Arg2));
7163 Set_Left_Opnd (N, Arg1);
7164 Set_Right_Opnd (N, Arg2);
7166 Set_Etype (N, Btyp);
7167 Rewrite (N, Unchecked_Convert_To (Typ, N));
7170 elsif Typ /= Etype (Left_Opnd (N))
7171 or else Typ /= Etype (Right_Opnd (N))
7173 -- Add explicit conversion where needed, and save interpretations in
7174 -- case operands are overloaded. If the context is a VMS operation,
7175 -- assert that the conversion is legal (the operands have the proper
7176 -- types to select the VMS intrinsic). Note that in rare cases the
7177 -- VMS operators may be visible, but the default System is being used
7178 -- and Address is a private type.
7180 Arg1 := Convert_To (Typ, Left_Opnd (N));
7181 Arg2 := Convert_To (Typ, Right_Opnd (N));
7183 if Nkind (Arg1) = N_Type_Conversion then
7184 Save_Interps (Left_Opnd (N), Expression (Arg1));
7186 if Is_VMS_Operator (Orig_Op) then
7187 Set_Conversion_OK (Arg1);
7190 Save_Interps (Left_Opnd (N), Arg1);
7193 if Nkind (Arg2) = N_Type_Conversion then
7194 Save_Interps (Right_Opnd (N), Expression (Arg2));
7196 if Is_VMS_Operator (Orig_Op) then
7197 Set_Conversion_OK (Arg2);
7200 Save_Interps (Right_Opnd (N), Arg2);
7203 Rewrite (Left_Opnd (N), Arg1);
7204 Rewrite (Right_Opnd (N), Arg2);
7207 Resolve_Arithmetic_Op (N, Typ);
7210 Resolve_Arithmetic_Op (N, Typ);
7212 end Resolve_Intrinsic_Operator;
7214 --------------------------------------
7215 -- Resolve_Intrinsic_Unary_Operator --
7216 --------------------------------------
7218 procedure Resolve_Intrinsic_Unary_Operator
7222 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7228 while Scope (Op) /= Standard_Standard loop
7230 pragma Assert (Present (Op));
7235 if Is_Private_Type (Typ) then
7236 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7237 Save_Interps (Right_Opnd (N), Expression (Arg2));
7239 Set_Right_Opnd (N, Arg2);
7241 Set_Etype (N, Btyp);
7242 Rewrite (N, Unchecked_Convert_To (Typ, N));
7246 Resolve_Unary_Op (N, Typ);
7248 end Resolve_Intrinsic_Unary_Operator;
7250 ------------------------
7251 -- Resolve_Logical_Op --
7252 ------------------------
7254 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7258 Check_No_Direct_Boolean_Operators (N);
7260 -- Predefined operations on scalar types yield the base type. On the
7261 -- other hand, logical operations on arrays yield the type of the
7262 -- arguments (and the context).
7264 if Is_Array_Type (Typ) then
7267 B_Typ := Base_Type (Typ);
7270 -- OK if this is a VMS-specific intrinsic operation
7272 if Is_VMS_Operator (Entity (N)) then
7275 -- The following test is required because the operands of the operation
7276 -- may be literals, in which case the resulting type appears to be
7277 -- compatible with a signed integer type, when in fact it is compatible
7278 -- only with modular types. If the context itself is universal, the
7279 -- operation is illegal.
7281 elsif not Valid_Boolean_Arg (Typ) then
7282 Error_Msg_N ("invalid context for logical operation", N);
7283 Set_Etype (N, Any_Type);
7286 elsif Typ = Any_Modular then
7288 ("no modular type available in this context", N);
7289 Set_Etype (N, Any_Type);
7292 elsif Is_Modular_Integer_Type (Typ)
7293 and then Etype (Left_Opnd (N)) = Universal_Integer
7294 and then Etype (Right_Opnd (N)) = Universal_Integer
7296 Check_For_Visible_Operator (N, B_Typ);
7299 Resolve (Left_Opnd (N), B_Typ);
7300 Resolve (Right_Opnd (N), B_Typ);
7302 Check_Unset_Reference (Left_Opnd (N));
7303 Check_Unset_Reference (Right_Opnd (N));
7305 Set_Etype (N, B_Typ);
7306 Generate_Operator_Reference (N, B_Typ);
7307 Eval_Logical_Op (N);
7309 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7310 -- only when both operands have same static lower and higher bounds. Of
7311 -- course the types have to match, so only check if operands are
7312 -- compatible and the node itself has no errors.
7314 if Is_Array_Type (B_Typ)
7315 and then Nkind (N) in N_Binary_Op
7318 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7319 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7322 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7323 -- operation if not needed.
7325 if Restriction_Check_Required (SPARK)
7326 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7327 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7328 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7329 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7331 Check_SPARK_Restriction
7332 ("array types should have matching static bounds", N);
7336 end Resolve_Logical_Op;
7338 ---------------------------
7339 -- Resolve_Membership_Op --
7340 ---------------------------
7342 -- The context can only be a boolean type, and does not determine the
7343 -- arguments. Arguments should be unambiguous, but the preference rule for
7344 -- universal types applies.
7346 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7347 pragma Warnings (Off, Typ);
7349 L : constant Node_Id := Left_Opnd (N);
7350 R : constant Node_Id := Right_Opnd (N);
7353 procedure Resolve_Set_Membership;
7354 -- Analysis has determined a unique type for the left operand. Use it to
7355 -- resolve the disjuncts.
7357 ----------------------------
7358 -- Resolve_Set_Membership --
7359 ----------------------------
7361 procedure Resolve_Set_Membership is
7365 Resolve (L, Etype (L));
7367 Alt := First (Alternatives (N));
7368 while Present (Alt) loop
7370 -- Alternative is an expression, a range
7371 -- or a subtype mark.
7373 if not Is_Entity_Name (Alt)
7374 or else not Is_Type (Entity (Alt))
7376 Resolve (Alt, Etype (L));
7381 end Resolve_Set_Membership;
7383 -- Start of processing for Resolve_Membership_Op
7386 if L = Error or else R = Error then
7390 if Present (Alternatives (N)) then
7391 Resolve_Set_Membership;
7394 elsif not Is_Overloaded (R)
7396 (Etype (R) = Universal_Integer
7398 Etype (R) = Universal_Real)
7399 and then Is_Overloaded (L)
7403 -- Ada 2005 (AI-251): Support the following case:
7405 -- type I is interface;
7406 -- type T is tagged ...
7408 -- function Test (O : I'Class) is
7410 -- return O in T'Class.
7413 -- In this case we have nothing else to do. The membership test will be
7414 -- done at run time.
7416 elsif Ada_Version >= Ada_2005
7417 and then Is_Class_Wide_Type (Etype (L))
7418 and then Is_Interface (Etype (L))
7419 and then Is_Class_Wide_Type (Etype (R))
7420 and then not Is_Interface (Etype (R))
7424 T := Intersect_Types (L, R);
7427 -- If mixed-mode operations are present and operands are all literal,
7428 -- the only interpretation involves Duration, which is probably not
7429 -- the intention of the programmer.
7431 if T = Any_Fixed then
7432 T := Unique_Fixed_Point_Type (N);
7434 if T = Any_Type then
7440 Check_Unset_Reference (L);
7442 if Nkind (R) = N_Range
7443 and then not Is_Scalar_Type (T)
7445 Error_Msg_N ("scalar type required for range", R);
7448 if Is_Entity_Name (R) then
7449 Freeze_Expression (R);
7452 Check_Unset_Reference (R);
7455 Eval_Membership_Op (N);
7456 end Resolve_Membership_Op;
7462 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7463 Loc : constant Source_Ptr := Sloc (N);
7466 -- Handle restriction against anonymous null access values This
7467 -- restriction can be turned off using -gnatdj.
7469 -- Ada 2005 (AI-231): Remove restriction
7471 if Ada_Version < Ada_2005
7472 and then not Debug_Flag_J
7473 and then Ekind (Typ) = E_Anonymous_Access_Type
7474 and then Comes_From_Source (N)
7476 -- In the common case of a call which uses an explicitly null value
7477 -- for an access parameter, give specialized error message.
7479 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7483 ("null is not allowed as argument for an access parameter", N);
7485 -- Standard message for all other cases (are there any?)
7489 ("null cannot be of an anonymous access type", N);
7493 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7494 -- assignment to a null-excluding object
7496 if Ada_Version >= Ada_2005
7497 and then Can_Never_Be_Null (Typ)
7498 and then Nkind (Parent (N)) = N_Assignment_Statement
7500 if not Inside_Init_Proc then
7502 (Compile_Time_Constraint_Error (N,
7503 "(Ada 2005) null not allowed in null-excluding objects?"),
7504 Make_Raise_Constraint_Error (Loc,
7505 Reason => CE_Access_Check_Failed));
7508 Make_Raise_Constraint_Error (Loc,
7509 Reason => CE_Access_Check_Failed));
7513 -- In a distributed context, null for a remote access to subprogram may
7514 -- need to be replaced with a special record aggregate. In this case,
7515 -- return after having done the transformation.
7517 if (Ekind (Typ) = E_Record_Type
7518 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7519 and then Remote_AST_Null_Value (N, Typ)
7524 -- The null literal takes its type from the context
7529 -----------------------
7530 -- Resolve_Op_Concat --
7531 -----------------------
7533 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7535 -- We wish to avoid deep recursion, because concatenations are often
7536 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7537 -- operands nonrecursively until we find something that is not a simple
7538 -- concatenation (A in this case). We resolve that, and then walk back
7539 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7540 -- to do the rest of the work at each level. The Parent pointers allow
7541 -- us to avoid recursion, and thus avoid running out of memory. See also
7542 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7548 -- The following code is equivalent to:
7550 -- Resolve_Op_Concat_First (NN, Typ);
7551 -- Resolve_Op_Concat_Arg (N, ...);
7552 -- Resolve_Op_Concat_Rest (N, Typ);
7554 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7555 -- operand is a concatenation.
7557 -- Walk down left operands
7560 Resolve_Op_Concat_First (NN, Typ);
7561 Op1 := Left_Opnd (NN);
7562 exit when not (Nkind (Op1) = N_Op_Concat
7563 and then not Is_Array_Type (Component_Type (Typ))
7564 and then Entity (Op1) = Entity (NN));
7568 -- Now (given the above example) NN is A&B and Op1 is A
7570 -- First resolve Op1 ...
7572 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7574 -- ... then walk NN back up until we reach N (where we started), calling
7575 -- Resolve_Op_Concat_Rest along the way.
7578 Resolve_Op_Concat_Rest (NN, Typ);
7583 if Base_Type (Etype (N)) /= Standard_String then
7584 Check_SPARK_Restriction
7585 ("result of concatenation should have type String", N);
7587 end Resolve_Op_Concat;
7589 ---------------------------
7590 -- Resolve_Op_Concat_Arg --
7591 ---------------------------
7593 procedure Resolve_Op_Concat_Arg
7599 Btyp : constant Entity_Id := Base_Type (Typ);
7600 Ctyp : constant Entity_Id := Component_Type (Typ);
7605 or else (not Is_Overloaded (Arg)
7606 and then Etype (Arg) /= Any_Composite
7607 and then Covers (Ctyp, Etype (Arg)))
7609 Resolve (Arg, Ctyp);
7611 Resolve (Arg, Btyp);
7614 -- If both Array & Array and Array & Component are visible, there is a
7615 -- potential ambiguity that must be reported.
7617 elsif Has_Compatible_Type (Arg, Ctyp) then
7618 if Nkind (Arg) = N_Aggregate
7619 and then Is_Composite_Type (Ctyp)
7621 if Is_Private_Type (Ctyp) then
7622 Resolve (Arg, Btyp);
7624 -- If the operation is user-defined and not overloaded use its
7625 -- profile. The operation may be a renaming, in which case it has
7626 -- been rewritten, and we want the original profile.
7628 elsif not Is_Overloaded (N)
7629 and then Comes_From_Source (Entity (Original_Node (N)))
7630 and then Ekind (Entity (Original_Node (N))) = E_Function
7634 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
7637 -- Otherwise an aggregate may match both the array type and the
7641 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7642 Set_Etype (Arg, Any_Type);
7646 if Is_Overloaded (Arg)
7647 and then Has_Compatible_Type (Arg, Typ)
7648 and then Etype (Arg) /= Any_Type
7656 Get_First_Interp (Arg, I, It);
7658 Get_Next_Interp (I, It);
7660 -- Special-case the error message when the overloading is
7661 -- caused by a function that yields an array and can be
7662 -- called without parameters.
7664 if It.Nam = Func then
7665 Error_Msg_Sloc := Sloc (Func);
7666 Error_Msg_N ("ambiguous call to function#", Arg);
7668 ("\\interpretation as call yields&", Arg, Typ);
7670 ("\\interpretation as indexing of call yields&",
7671 Arg, Component_Type (Typ));
7674 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
7676 Get_First_Interp (Arg, I, It);
7677 while Present (It.Nam) loop
7678 Error_Msg_Sloc := Sloc (It.Nam);
7680 if Base_Type (It.Typ) = Btyp
7682 Base_Type (It.Typ) = Base_Type (Ctyp)
7684 Error_Msg_N -- CODEFIX
7685 ("\\possible interpretation#", Arg);
7688 Get_Next_Interp (I, It);
7694 Resolve (Arg, Component_Type (Typ));
7696 if Nkind (Arg) = N_String_Literal then
7697 Set_Etype (Arg, Component_Type (Typ));
7700 if Arg = Left_Opnd (N) then
7701 Set_Is_Component_Left_Opnd (N);
7703 Set_Is_Component_Right_Opnd (N);
7708 Resolve (Arg, Btyp);
7711 -- Concatenation is restricted in SPARK: each operand must be either a
7712 -- string literal, the name of a string constant, a static character or
7713 -- string expression, or another concatenation. Arg cannot be a
7714 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
7715 -- separately on each final operand, past concatenation operations.
7717 if Is_Character_Type (Etype (Arg)) then
7718 if not Is_Static_Expression (Arg) then
7719 Check_SPARK_Restriction
7720 ("character operand for concatenation should be static", N);
7723 elsif Is_String_Type (Etype (Arg)) then
7724 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
7725 and then Is_Constant_Object (Entity (Arg)))
7726 and then not Is_Static_Expression (Arg)
7728 Check_SPARK_Restriction
7729 ("string operand for concatenation should be static", N);
7732 -- Do not issue error on an operand that is neither a character nor a
7733 -- string, as the error is issued in Resolve_Op_Concat.
7739 Check_Unset_Reference (Arg);
7740 end Resolve_Op_Concat_Arg;
7742 -----------------------------
7743 -- Resolve_Op_Concat_First --
7744 -----------------------------
7746 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7747 Btyp : constant Entity_Id := Base_Type (Typ);
7748 Op1 : constant Node_Id := Left_Opnd (N);
7749 Op2 : constant Node_Id := Right_Opnd (N);
7752 -- The parser folds an enormous sequence of concatenations of string
7753 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7754 -- in the right operand. If the expression resolves to a predefined "&"
7755 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7756 -- we give an error. See P_Simple_Expression in Par.Ch4.
7758 if Nkind (Op2) = N_String_Literal
7759 and then Is_Folded_In_Parser (Op2)
7760 and then Ekind (Entity (N)) = E_Function
7762 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7763 and then String_Length (Strval (Op1)) = 0);
7764 Error_Msg_N ("too many user-defined concatenations", N);
7768 Set_Etype (N, Btyp);
7770 if Is_Limited_Composite (Btyp) then
7771 Error_Msg_N ("concatenation not available for limited array", N);
7772 Explain_Limited_Type (Btyp, N);
7774 end Resolve_Op_Concat_First;
7776 ----------------------------
7777 -- Resolve_Op_Concat_Rest --
7778 ----------------------------
7780 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7781 Op1 : constant Node_Id := Left_Opnd (N);
7782 Op2 : constant Node_Id := Right_Opnd (N);
7785 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7787 Generate_Operator_Reference (N, Typ);
7789 if Is_String_Type (Typ) then
7790 Eval_Concatenation (N);
7793 -- If this is not a static concatenation, but the result is a string
7794 -- type (and not an array of strings) ensure that static string operands
7795 -- have their subtypes properly constructed.
7797 if Nkind (N) /= N_String_Literal
7798 and then Is_Character_Type (Component_Type (Typ))
7800 Set_String_Literal_Subtype (Op1, Typ);
7801 Set_String_Literal_Subtype (Op2, Typ);
7803 end Resolve_Op_Concat_Rest;
7805 ----------------------
7806 -- Resolve_Op_Expon --
7807 ----------------------
7809 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7810 B_Typ : constant Entity_Id := Base_Type (Typ);
7813 -- Catch attempts to do fixed-point exponentiation with universal
7814 -- operands, which is a case where the illegality is not caught during
7815 -- normal operator analysis.
7817 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7818 Error_Msg_N ("exponentiation not available for fixed point", N);
7822 if Comes_From_Source (N)
7823 and then Ekind (Entity (N)) = E_Function
7824 and then Is_Imported (Entity (N))
7825 and then Is_Intrinsic_Subprogram (Entity (N))
7827 Resolve_Intrinsic_Operator (N, Typ);
7831 if Etype (Left_Opnd (N)) = Universal_Integer
7832 or else Etype (Left_Opnd (N)) = Universal_Real
7834 Check_For_Visible_Operator (N, B_Typ);
7837 -- We do the resolution using the base type, because intermediate values
7838 -- in expressions always are of the base type, not a subtype of it.
7840 Resolve (Left_Opnd (N), B_Typ);
7841 Resolve (Right_Opnd (N), Standard_Integer);
7843 Check_Unset_Reference (Left_Opnd (N));
7844 Check_Unset_Reference (Right_Opnd (N));
7846 Set_Etype (N, B_Typ);
7847 Generate_Operator_Reference (N, B_Typ);
7850 -- Set overflow checking bit. Much cleverer code needed here eventually
7851 -- and perhaps the Resolve routines should be separated for the various
7852 -- arithmetic operations, since they will need different processing. ???
7854 if Nkind (N) in N_Op then
7855 if not Overflow_Checks_Suppressed (Etype (N)) then
7856 Enable_Overflow_Check (N);
7859 end Resolve_Op_Expon;
7861 --------------------
7862 -- Resolve_Op_Not --
7863 --------------------
7865 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7868 function Parent_Is_Boolean return Boolean;
7869 -- This function determines if the parent node is a boolean operator or
7870 -- operation (comparison op, membership test, or short circuit form) and
7871 -- the not in question is the left operand of this operation. Note that
7872 -- if the not is in parens, then false is returned.
7874 -----------------------
7875 -- Parent_Is_Boolean --
7876 -----------------------
7878 function Parent_Is_Boolean return Boolean is
7880 if Paren_Count (N) /= 0 then
7884 case Nkind (Parent (N)) is
7899 return Left_Opnd (Parent (N)) = N;
7905 end Parent_Is_Boolean;
7907 -- Start of processing for Resolve_Op_Not
7910 -- Predefined operations on scalar types yield the base type. On the
7911 -- other hand, logical operations on arrays yield the type of the
7912 -- arguments (and the context).
7914 if Is_Array_Type (Typ) then
7917 B_Typ := Base_Type (Typ);
7920 if Is_VMS_Operator (Entity (N)) then
7923 -- Straightforward case of incorrect arguments
7925 elsif not Valid_Boolean_Arg (Typ) then
7926 Error_Msg_N ("invalid operand type for operator&", N);
7927 Set_Etype (N, Any_Type);
7930 -- Special case of probable missing parens
7932 elsif Typ = Universal_Integer or else Typ = Any_Modular then
7933 if Parent_Is_Boolean then
7935 ("operand of not must be enclosed in parentheses",
7939 ("no modular type available in this context", N);
7942 Set_Etype (N, Any_Type);
7945 -- OK resolution of NOT
7948 -- Warn if non-boolean types involved. This is a case like not a < b
7949 -- where a and b are modular, where we will get (not a) < b and most
7950 -- likely not (a < b) was intended.
7952 if Warn_On_Questionable_Missing_Parens
7953 and then not Is_Boolean_Type (Typ)
7954 and then Parent_Is_Boolean
7956 Error_Msg_N ("?not expression should be parenthesized here!", N);
7959 -- Warn on double negation if checking redundant constructs
7961 if Warn_On_Redundant_Constructs
7962 and then Comes_From_Source (N)
7963 and then Comes_From_Source (Right_Opnd (N))
7964 and then Root_Type (Typ) = Standard_Boolean
7965 and then Nkind (Right_Opnd (N)) = N_Op_Not
7967 Error_Msg_N ("redundant double negation?", N);
7970 -- Complete resolution and evaluation of NOT
7972 Resolve (Right_Opnd (N), B_Typ);
7973 Check_Unset_Reference (Right_Opnd (N));
7974 Set_Etype (N, B_Typ);
7975 Generate_Operator_Reference (N, B_Typ);
7980 -----------------------------
7981 -- Resolve_Operator_Symbol --
7982 -----------------------------
7984 -- Nothing to be done, all resolved already
7986 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
7987 pragma Warnings (Off, N);
7988 pragma Warnings (Off, Typ);
7992 end Resolve_Operator_Symbol;
7994 ----------------------------------
7995 -- Resolve_Qualified_Expression --
7996 ----------------------------------
7998 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
7999 pragma Warnings (Off, Typ);
8001 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8002 Expr : constant Node_Id := Expression (N);
8005 Resolve (Expr, Target_Typ);
8007 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8008 -- operation if not needed.
8010 if Restriction_Check_Required (SPARK)
8011 and then Is_Array_Type (Target_Typ)
8012 and then Is_Array_Type (Etype (Expr))
8013 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8014 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8016 Check_SPARK_Restriction
8017 ("array types should have matching static bounds", N);
8020 -- A qualified expression requires an exact match of the type, class-
8021 -- wide matching is not allowed. However, if the qualifying type is
8022 -- specific and the expression has a class-wide type, it may still be
8023 -- okay, since it can be the result of the expansion of a call to a
8024 -- dispatching function, so we also have to check class-wideness of the
8025 -- type of the expression's original node.
8027 if (Is_Class_Wide_Type (Target_Typ)
8029 (Is_Class_Wide_Type (Etype (Expr))
8030 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8031 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8033 Wrong_Type (Expr, Target_Typ);
8036 -- If the target type is unconstrained, then we reset the type of the
8037 -- result from the type of the expression. For other cases, the actual
8038 -- subtype of the expression is the target type.
8040 if Is_Composite_Type (Target_Typ)
8041 and then not Is_Constrained (Target_Typ)
8043 Set_Etype (N, Etype (Expr));
8046 Eval_Qualified_Expression (N);
8047 end Resolve_Qualified_Expression;
8049 -----------------------------------
8050 -- Resolve_Quantified_Expression --
8051 -----------------------------------
8053 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
8055 -- The loop structure is already resolved during its analysis, only the
8056 -- resolution of the condition needs to be done. Expansion is disabled
8057 -- so that checks and other generated code are inserted in the tree
8058 -- after expression has been rewritten as a loop.
8060 Expander_Mode_Save_And_Set (False);
8061 Resolve (Condition (N), Typ);
8062 Expander_Mode_Restore;
8063 end Resolve_Quantified_Expression;
8069 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8070 L : constant Node_Id := Low_Bound (N);
8071 H : constant Node_Id := High_Bound (N);
8073 function First_Last_Ref return Boolean;
8074 -- Returns True if N is of the form X'First .. X'Last where X is the
8075 -- same entity for both attributes.
8077 --------------------
8078 -- First_Last_Ref --
8079 --------------------
8081 function First_Last_Ref return Boolean is
8082 Lorig : constant Node_Id := Original_Node (L);
8083 Horig : constant Node_Id := Original_Node (H);
8086 if Nkind (Lorig) = N_Attribute_Reference
8087 and then Nkind (Horig) = N_Attribute_Reference
8088 and then Attribute_Name (Lorig) = Name_First
8089 and then Attribute_Name (Horig) = Name_Last
8092 PL : constant Node_Id := Prefix (Lorig);
8093 PH : constant Node_Id := Prefix (Horig);
8095 if Is_Entity_Name (PL)
8096 and then Is_Entity_Name (PH)
8097 and then Entity (PL) = Entity (PH)
8107 -- Start of processing for Resolve_Range
8114 -- Check for inappropriate range on unordered enumeration type
8116 if Bad_Unordered_Enumeration_Reference (N, Typ)
8118 -- Exclude X'First .. X'Last if X is the same entity for both
8120 and then not First_Last_Ref
8122 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
8125 Check_Unset_Reference (L);
8126 Check_Unset_Reference (H);
8128 -- We have to check the bounds for being within the base range as
8129 -- required for a non-static context. Normally this is automatic and
8130 -- done as part of evaluating expressions, but the N_Range node is an
8131 -- exception, since in GNAT we consider this node to be a subexpression,
8132 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8133 -- this, but that would put the test on the main evaluation path for
8136 Check_Non_Static_Context (L);
8137 Check_Non_Static_Context (H);
8139 -- Check for an ambiguous range over character literals. This will
8140 -- happen with a membership test involving only literals.
8142 if Typ = Any_Character then
8143 Ambiguous_Character (L);
8144 Set_Etype (N, Any_Type);
8148 -- If bounds are static, constant-fold them, so size computations are
8149 -- identical between front-end and back-end. Do not perform this
8150 -- transformation while analyzing generic units, as type information
8151 -- would be lost when reanalyzing the constant node in the instance.
8153 if Is_Discrete_Type (Typ) and then Expander_Active then
8154 if Is_OK_Static_Expression (L) then
8155 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8158 if Is_OK_Static_Expression (H) then
8159 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8164 --------------------------
8165 -- Resolve_Real_Literal --
8166 --------------------------
8168 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8169 Actual_Typ : constant Entity_Id := Etype (N);
8172 -- Special processing for fixed-point literals to make sure that the
8173 -- value is an exact multiple of small where this is required. We skip
8174 -- this for the universal real case, and also for generic types.
8176 if Is_Fixed_Point_Type (Typ)
8177 and then Typ /= Universal_Fixed
8178 and then Typ /= Any_Fixed
8179 and then not Is_Generic_Type (Typ)
8182 Val : constant Ureal := Realval (N);
8183 Cintr : constant Ureal := Val / Small_Value (Typ);
8184 Cint : constant Uint := UR_Trunc (Cintr);
8185 Den : constant Uint := Norm_Den (Cintr);
8189 -- Case of literal is not an exact multiple of the Small
8193 -- For a source program literal for a decimal fixed-point type,
8194 -- this is statically illegal (RM 4.9(36)).
8196 if Is_Decimal_Fixed_Point_Type (Typ)
8197 and then Actual_Typ = Universal_Real
8198 and then Comes_From_Source (N)
8200 Error_Msg_N ("value has extraneous low order digits", N);
8203 -- Generate a warning if literal from source
8205 if Is_Static_Expression (N)
8206 and then Warn_On_Bad_Fixed_Value
8209 ("?static fixed-point value is not a multiple of Small!",
8213 -- Replace literal by a value that is the exact representation
8214 -- of a value of the type, i.e. a multiple of the small value,
8215 -- by truncation, since Machine_Rounds is false for all GNAT
8216 -- fixed-point types (RM 4.9(38)).
8218 Stat := Is_Static_Expression (N);
8220 Make_Real_Literal (Sloc (N),
8221 Realval => Small_Value (Typ) * Cint));
8223 Set_Is_Static_Expression (N, Stat);
8226 -- In all cases, set the corresponding integer field
8228 Set_Corresponding_Integer_Value (N, Cint);
8232 -- Now replace the actual type by the expected type as usual
8235 Eval_Real_Literal (N);
8236 end Resolve_Real_Literal;
8238 -----------------------
8239 -- Resolve_Reference --
8240 -----------------------
8242 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8243 P : constant Node_Id := Prefix (N);
8246 -- Replace general access with specific type
8248 if Ekind (Etype (N)) = E_Allocator_Type then
8249 Set_Etype (N, Base_Type (Typ));
8252 Resolve (P, Designated_Type (Etype (N)));
8254 -- If we are taking the reference of a volatile entity, then treat it as
8255 -- a potential modification of this entity. This is too conservative,
8256 -- but necessary because remove side effects can cause transformations
8257 -- of normal assignments into reference sequences that otherwise fail to
8258 -- notice the modification.
8260 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8261 Note_Possible_Modification (P, Sure => False);
8263 end Resolve_Reference;
8265 --------------------------------
8266 -- Resolve_Selected_Component --
8267 --------------------------------
8269 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8271 Comp1 : Entity_Id := Empty; -- prevent junk warning
8272 P : constant Node_Id := Prefix (N);
8273 S : constant Node_Id := Selector_Name (N);
8274 T : Entity_Id := Etype (P);
8276 I1 : Interp_Index := 0; -- prevent junk warning
8281 function Init_Component return Boolean;
8282 -- Check whether this is the initialization of a component within an
8283 -- init proc (by assignment or call to another init proc). If true,
8284 -- there is no need for a discriminant check.
8286 --------------------
8287 -- Init_Component --
8288 --------------------
8290 function Init_Component return Boolean is
8292 return Inside_Init_Proc
8293 and then Nkind (Prefix (N)) = N_Identifier
8294 and then Chars (Prefix (N)) = Name_uInit
8295 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8298 -- Start of processing for Resolve_Selected_Component
8301 if Is_Overloaded (P) then
8303 -- Use the context type to select the prefix that has a selector
8304 -- of the correct name and type.
8307 Get_First_Interp (P, I, It);
8309 Search : while Present (It.Typ) loop
8310 if Is_Access_Type (It.Typ) then
8311 T := Designated_Type (It.Typ);
8316 -- Locate selected component. For a private prefix the selector
8317 -- can denote a discriminant.
8319 if Is_Record_Type (T) or else Is_Private_Type (T) then
8321 -- The visible components of a class-wide type are those of
8324 if Is_Class_Wide_Type (T) then
8328 Comp := First_Entity (T);
8329 while Present (Comp) loop
8330 if Chars (Comp) = Chars (S)
8331 and then Covers (Etype (Comp), Typ)
8340 It := Disambiguate (P, I1, I, Any_Type);
8342 if It = No_Interp then
8344 ("ambiguous prefix for selected component", N);
8351 -- There may be an implicit dereference. Retrieve
8352 -- designated record type.
8354 if Is_Access_Type (It1.Typ) then
8355 T := Designated_Type (It1.Typ);
8360 if Scope (Comp1) /= T then
8362 -- Resolution chooses the new interpretation.
8363 -- Find the component with the right name.
8365 Comp1 := First_Entity (T);
8366 while Present (Comp1)
8367 and then Chars (Comp1) /= Chars (S)
8369 Comp1 := Next_Entity (Comp1);
8378 Comp := Next_Entity (Comp);
8382 Get_Next_Interp (I, It);
8385 Resolve (P, It1.Typ);
8387 Set_Entity_With_Style_Check (S, Comp1);
8390 -- Resolve prefix with its type
8395 -- Generate cross-reference. We needed to wait until full overloading
8396 -- resolution was complete to do this, since otherwise we can't tell if
8397 -- we are an lvalue or not.
8399 if May_Be_Lvalue (N) then
8400 Generate_Reference (Entity (S), S, 'm');
8402 Generate_Reference (Entity (S), S, 'r');
8405 -- If prefix is an access type, the node will be transformed into an
8406 -- explicit dereference during expansion. The type of the node is the
8407 -- designated type of that of the prefix.
8409 if Is_Access_Type (Etype (P)) then
8410 T := Designated_Type (Etype (P));
8411 Check_Fully_Declared_Prefix (T, P);
8416 if Has_Discriminants (T)
8417 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8418 and then Present (Original_Record_Component (Entity (S)))
8419 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8420 and then Present (Discriminant_Checking_Func
8421 (Original_Record_Component (Entity (S))))
8422 and then not Discriminant_Checks_Suppressed (T)
8423 and then not Init_Component
8425 Set_Do_Discriminant_Check (N);
8428 if Ekind (Entity (S)) = E_Void then
8429 Error_Msg_N ("premature use of component", S);
8432 -- If the prefix is a record conversion, this may be a renamed
8433 -- discriminant whose bounds differ from those of the original
8434 -- one, so we must ensure that a range check is performed.
8436 if Nkind (P) = N_Type_Conversion
8437 and then Ekind (Entity (S)) = E_Discriminant
8438 and then Is_Discrete_Type (Typ)
8440 Set_Etype (N, Base_Type (Typ));
8443 -- Note: No Eval processing is required, because the prefix is of a
8444 -- record type, or protected type, and neither can possibly be static.
8446 -- If the array type is atomic, and is packed, and we are in a left side
8447 -- context, then this is worth a warning, since we have a situation
8448 -- where the access to the component may cause extra read/writes of the
8449 -- atomic array object, which could be considered unexpected.
8451 if Nkind (N) = N_Selected_Component
8452 and then (Is_Atomic (T)
8453 or else (Is_Entity_Name (Prefix (N))
8454 and then Is_Atomic (Entity (Prefix (N)))))
8455 and then Is_Packed (T)
8458 Error_Msg_N ("?assignment to component of packed atomic record",
8460 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8463 end Resolve_Selected_Component;
8469 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8470 B_Typ : constant Entity_Id := Base_Type (Typ);
8471 L : constant Node_Id := Left_Opnd (N);
8472 R : constant Node_Id := Right_Opnd (N);
8475 -- We do the resolution using the base type, because intermediate values
8476 -- in expressions always are of the base type, not a subtype of it.
8479 Resolve (R, Standard_Natural);
8481 Check_Unset_Reference (L);
8482 Check_Unset_Reference (R);
8484 Set_Etype (N, B_Typ);
8485 Generate_Operator_Reference (N, B_Typ);
8489 ---------------------------
8490 -- Resolve_Short_Circuit --
8491 ---------------------------
8493 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8494 B_Typ : constant Entity_Id := Base_Type (Typ);
8495 L : constant Node_Id := Left_Opnd (N);
8496 R : constant Node_Id := Right_Opnd (N);
8502 -- Check for issuing warning for always False assert/check, this happens
8503 -- when assertions are turned off, in which case the pragma Assert/Check
8504 -- was transformed into:
8506 -- if False and then <condition> then ...
8508 -- and we detect this pattern
8510 if Warn_On_Assertion_Failure
8511 and then Is_Entity_Name (R)
8512 and then Entity (R) = Standard_False
8513 and then Nkind (Parent (N)) = N_If_Statement
8514 and then Nkind (N) = N_And_Then
8515 and then Is_Entity_Name (L)
8516 and then Entity (L) = Standard_False
8519 Orig : constant Node_Id := Original_Node (Parent (N));
8522 if Nkind (Orig) = N_Pragma
8523 and then Pragma_Name (Orig) = Name_Assert
8525 -- Don't want to warn if original condition is explicit False
8528 Expr : constant Node_Id :=
8531 (First (Pragma_Argument_Associations (Orig))));
8533 if Is_Entity_Name (Expr)
8534 and then Entity (Expr) = Standard_False
8538 -- Issue warning. We do not want the deletion of the
8539 -- IF/AND-THEN to take this message with it. We achieve
8540 -- this by making sure that the expanded code points to
8541 -- the Sloc of the expression, not the original pragma.
8544 ("?assertion would fail at run time!",
8546 (First (Pragma_Argument_Associations (Orig))));
8550 -- Similar processing for Check pragma
8552 elsif Nkind (Orig) = N_Pragma
8553 and then Pragma_Name (Orig) = Name_Check
8555 -- Don't want to warn if original condition is explicit False
8558 Expr : constant Node_Id :=
8562 (Pragma_Argument_Associations (Orig)))));
8564 if Is_Entity_Name (Expr)
8565 and then Entity (Expr) = Standard_False
8570 ("?check would fail at run time!",
8572 (Last (Pragma_Argument_Associations (Orig))));
8579 -- Continue with processing of short circuit
8581 Check_Unset_Reference (L);
8582 Check_Unset_Reference (R);
8584 Set_Etype (N, B_Typ);
8585 Eval_Short_Circuit (N);
8586 end Resolve_Short_Circuit;
8592 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8593 Name : constant Node_Id := Prefix (N);
8594 Drange : constant Node_Id := Discrete_Range (N);
8595 Array_Type : Entity_Id := Empty;
8599 if Is_Overloaded (Name) then
8601 -- Use the context type to select the prefix that yields the correct
8606 I1 : Interp_Index := 0;
8608 P : constant Node_Id := Prefix (N);
8609 Found : Boolean := False;
8612 Get_First_Interp (P, I, It);
8613 while Present (It.Typ) loop
8614 if (Is_Array_Type (It.Typ)
8615 and then Covers (Typ, It.Typ))
8616 or else (Is_Access_Type (It.Typ)
8617 and then Is_Array_Type (Designated_Type (It.Typ))
8618 and then Covers (Typ, Designated_Type (It.Typ)))
8621 It := Disambiguate (P, I1, I, Any_Type);
8623 if It = No_Interp then
8624 Error_Msg_N ("ambiguous prefix for slicing", N);
8629 Array_Type := It.Typ;
8634 Array_Type := It.Typ;
8639 Get_Next_Interp (I, It);
8644 Array_Type := Etype (Name);
8647 Resolve (Name, Array_Type);
8649 if Is_Access_Type (Array_Type) then
8650 Apply_Access_Check (N);
8651 Array_Type := Designated_Type (Array_Type);
8653 -- If the prefix is an access to an unconstrained array, we must use
8654 -- the actual subtype of the object to perform the index checks. The
8655 -- object denoted by the prefix is implicit in the node, so we build
8656 -- an explicit representation for it in order to compute the actual
8659 if not Is_Constrained (Array_Type) then
8660 Remove_Side_Effects (Prefix (N));
8663 Obj : constant Node_Id :=
8664 Make_Explicit_Dereference (Sloc (N),
8665 Prefix => New_Copy_Tree (Prefix (N)));
8667 Set_Etype (Obj, Array_Type);
8668 Set_Parent (Obj, Parent (N));
8669 Array_Type := Get_Actual_Subtype (Obj);
8673 elsif Is_Entity_Name (Name)
8674 or else Nkind (Name) = N_Explicit_Dereference
8675 or else (Nkind (Name) = N_Function_Call
8676 and then not Is_Constrained (Etype (Name)))
8678 Array_Type := Get_Actual_Subtype (Name);
8680 -- If the name is a selected component that depends on discriminants,
8681 -- build an actual subtype for it. This can happen only when the name
8682 -- itself is overloaded; otherwise the actual subtype is created when
8683 -- the selected component is analyzed.
8685 elsif Nkind (Name) = N_Selected_Component
8686 and then Full_Analysis
8687 and then Depends_On_Discriminant (First_Index (Array_Type))
8690 Act_Decl : constant Node_Id :=
8691 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8693 Insert_Action (N, Act_Decl);
8694 Array_Type := Defining_Identifier (Act_Decl);
8697 -- Maybe this should just be "else", instead of checking for the
8698 -- specific case of slice??? This is needed for the case where the
8699 -- prefix is an Image attribute, which gets expanded to a slice, and so
8700 -- has a constrained subtype which we want to use for the slice range
8701 -- check applied below (the range check won't get done if the
8702 -- unconstrained subtype of the 'Image is used).
8704 elsif Nkind (Name) = N_Slice then
8705 Array_Type := Etype (Name);
8708 -- If name was overloaded, set slice type correctly now
8710 Set_Etype (N, Array_Type);
8712 -- If the range is specified by a subtype mark, no resolution is
8713 -- necessary. Else resolve the bounds, and apply needed checks.
8715 if not Is_Entity_Name (Drange) then
8716 Index := First_Index (Array_Type);
8717 Resolve (Drange, Base_Type (Etype (Index)));
8719 if Nkind (Drange) = N_Range then
8721 -- Ensure that side effects in the bounds are properly handled
8723 Force_Evaluation (Low_Bound (Drange));
8724 Force_Evaluation (High_Bound (Drange));
8726 -- Do not apply the range check to nodes associated with the
8727 -- frontend expansion of the dispatch table. We first check
8728 -- if Ada.Tags is already loaded to avoid the addition of an
8729 -- undesired dependence on such run-time unit.
8731 if not Tagged_Type_Expansion
8733 (RTU_Loaded (Ada_Tags)
8734 and then Nkind (Prefix (N)) = N_Selected_Component
8735 and then Present (Entity (Selector_Name (Prefix (N))))
8736 and then Entity (Selector_Name (Prefix (N))) =
8737 RTE_Record_Component (RE_Prims_Ptr))
8739 Apply_Range_Check (Drange, Etype (Index));
8744 Set_Slice_Subtype (N);
8746 -- Check bad use of type with predicates
8748 if Has_Predicates (Etype (Drange)) then
8749 Bad_Predicated_Subtype_Use
8750 ("subtype& has predicate, not allowed in slice",
8751 Drange, Etype (Drange));
8753 -- Otherwise here is where we check suspicious indexes
8755 elsif Nkind (Drange) = N_Range then
8756 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8757 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8763 ----------------------------
8764 -- Resolve_String_Literal --
8765 ----------------------------
8767 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8768 C_Typ : constant Entity_Id := Component_Type (Typ);
8769 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8770 Loc : constant Source_Ptr := Sloc (N);
8771 Str : constant String_Id := Strval (N);
8772 Strlen : constant Nat := String_Length (Str);
8773 Subtype_Id : Entity_Id;
8774 Need_Check : Boolean;
8777 -- For a string appearing in a concatenation, defer creation of the
8778 -- string_literal_subtype until the end of the resolution of the
8779 -- concatenation, because the literal may be constant-folded away. This
8780 -- is a useful optimization for long concatenation expressions.
8782 -- If the string is an aggregate built for a single character (which
8783 -- happens in a non-static context) or a is null string to which special
8784 -- checks may apply, we build the subtype. Wide strings must also get a
8785 -- string subtype if they come from a one character aggregate. Strings
8786 -- generated by attributes might be static, but it is often hard to
8787 -- determine whether the enclosing context is static, so we generate
8788 -- subtypes for them as well, thus losing some rarer optimizations ???
8789 -- Same for strings that come from a static conversion.
8792 (Strlen = 0 and then Typ /= Standard_String)
8793 or else Nkind (Parent (N)) /= N_Op_Concat
8794 or else (N /= Left_Opnd (Parent (N))
8795 and then N /= Right_Opnd (Parent (N)))
8796 or else ((Typ = Standard_Wide_String
8797 or else Typ = Standard_Wide_Wide_String)
8798 and then Nkind (Original_Node (N)) /= N_String_Literal);
8800 -- If the resolving type is itself a string literal subtype, we can just
8801 -- reuse it, since there is no point in creating another.
8803 if Ekind (Typ) = E_String_Literal_Subtype then
8806 elsif Nkind (Parent (N)) = N_Op_Concat
8807 and then not Need_Check
8808 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8809 N_Attribute_Reference,
8810 N_Qualified_Expression,
8815 -- Otherwise we must create a string literal subtype. Note that the
8816 -- whole idea of string literal subtypes is simply to avoid the need
8817 -- for building a full fledged array subtype for each literal.
8820 Set_String_Literal_Subtype (N, Typ);
8821 Subtype_Id := Etype (N);
8824 if Nkind (Parent (N)) /= N_Op_Concat
8827 Set_Etype (N, Subtype_Id);
8828 Eval_String_Literal (N);
8831 if Is_Limited_Composite (Typ)
8832 or else Is_Private_Composite (Typ)
8834 Error_Msg_N ("string literal not available for private array", N);
8835 Set_Etype (N, Any_Type);
8839 -- The validity of a null string has been checked in the call to
8840 -- Eval_String_Literal.
8845 -- Always accept string literal with component type Any_Character, which
8846 -- occurs in error situations and in comparisons of literals, both of
8847 -- which should accept all literals.
8849 elsif R_Typ = Any_Character then
8852 -- If the type is bit-packed, then we always transform the string
8853 -- literal into a full fledged aggregate.
8855 elsif Is_Bit_Packed_Array (Typ) then
8858 -- Deal with cases of Wide_Wide_String, Wide_String, and String
8861 -- For Standard.Wide_Wide_String, or any other type whose component
8862 -- type is Standard.Wide_Wide_Character, we know that all the
8863 -- characters in the string must be acceptable, since the parser
8864 -- accepted the characters as valid character literals.
8866 if R_Typ = Standard_Wide_Wide_Character then
8869 -- For the case of Standard.String, or any other type whose component
8870 -- type is Standard.Character, we must make sure that there are no
8871 -- wide characters in the string, i.e. that it is entirely composed
8872 -- of characters in range of type Character.
8874 -- If the string literal is the result of a static concatenation, the
8875 -- test has already been performed on the components, and need not be
8878 elsif R_Typ = Standard_Character
8879 and then Nkind (Original_Node (N)) /= N_Op_Concat
8881 for J in 1 .. Strlen loop
8882 if not In_Character_Range (Get_String_Char (Str, J)) then
8884 -- If we are out of range, post error. This is one of the
8885 -- very few places that we place the flag in the middle of
8886 -- a token, right under the offending wide character. Not
8887 -- quite clear if this is right wrt wide character encoding
8888 -- sequences, but it's only an error message!
8891 ("literal out of range of type Standard.Character",
8892 Source_Ptr (Int (Loc) + J));
8897 -- For the case of Standard.Wide_String, or any other type whose
8898 -- component type is Standard.Wide_Character, we must make sure that
8899 -- there are no wide characters in the string, i.e. that it is
8900 -- entirely composed of characters in range of type Wide_Character.
8902 -- If the string literal is the result of a static concatenation,
8903 -- the test has already been performed on the components, and need
8906 elsif R_Typ = Standard_Wide_Character
8907 and then Nkind (Original_Node (N)) /= N_Op_Concat
8909 for J in 1 .. Strlen loop
8910 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8912 -- If we are out of range, post error. This is one of the
8913 -- very few places that we place the flag in the middle of
8914 -- a token, right under the offending wide character.
8916 -- This is not quite right, because characters in general
8917 -- will take more than one character position ???
8920 ("literal out of range of type Standard.Wide_Character",
8921 Source_Ptr (Int (Loc) + J));
8926 -- If the root type is not a standard character, then we will convert
8927 -- the string into an aggregate and will let the aggregate code do
8928 -- the checking. Standard Wide_Wide_Character is also OK here.
8934 -- See if the component type of the array corresponding to the string
8935 -- has compile time known bounds. If yes we can directly check
8936 -- whether the evaluation of the string will raise constraint error.
8937 -- Otherwise we need to transform the string literal into the
8938 -- corresponding character aggregate and let the aggregate code do
8941 if Is_Standard_Character_Type (R_Typ) then
8943 -- Check for the case of full range, where we are definitely OK
8945 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8949 -- Here the range is not the complete base type range, so check
8952 Comp_Typ_Lo : constant Node_Id :=
8953 Type_Low_Bound (Component_Type (Typ));
8954 Comp_Typ_Hi : constant Node_Id :=
8955 Type_High_Bound (Component_Type (Typ));
8960 if Compile_Time_Known_Value (Comp_Typ_Lo)
8961 and then Compile_Time_Known_Value (Comp_Typ_Hi)
8963 for J in 1 .. Strlen loop
8964 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
8966 if Char_Val < Expr_Value (Comp_Typ_Lo)
8967 or else Char_Val > Expr_Value (Comp_Typ_Hi)
8969 Apply_Compile_Time_Constraint_Error
8970 (N, "character out of range?", CE_Range_Check_Failed,
8971 Loc => Source_Ptr (Int (Loc) + J));
8981 -- If we got here we meed to transform the string literal into the
8982 -- equivalent qualified positional array aggregate. This is rather
8983 -- heavy artillery for this situation, but it is hard work to avoid.
8986 Lits : constant List_Id := New_List;
8987 P : Source_Ptr := Loc + 1;
8991 -- Build the character literals, we give them source locations that
8992 -- correspond to the string positions, which is a bit tricky given
8993 -- the possible presence of wide character escape sequences.
8995 for J in 1 .. Strlen loop
8996 C := Get_String_Char (Str, J);
8997 Set_Character_Literal_Name (C);
9000 Make_Character_Literal (P,
9002 Char_Literal_Value => UI_From_CC (C)));
9004 if In_Character_Range (C) then
9007 -- Should we have a call to Skip_Wide here ???
9016 Make_Qualified_Expression (Loc,
9017 Subtype_Mark => New_Reference_To (Typ, Loc),
9019 Make_Aggregate (Loc, Expressions => Lits)));
9021 Analyze_And_Resolve (N, Typ);
9023 end Resolve_String_Literal;
9025 -----------------------------
9026 -- Resolve_Subprogram_Info --
9027 -----------------------------
9029 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9032 end Resolve_Subprogram_Info;
9034 -----------------------------
9035 -- Resolve_Type_Conversion --
9036 -----------------------------
9038 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9039 Conv_OK : constant Boolean := Conversion_OK (N);
9040 Operand : constant Node_Id := Expression (N);
9041 Operand_Typ : constant Entity_Id := Etype (Operand);
9042 Target_Typ : constant Entity_Id := Etype (N);
9047 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9048 -- Set to False to suppress cases where we want to suppress the test
9049 -- for redundancy to avoid possible false positives on this warning.
9053 and then not Valid_Conversion (N, Target_Typ, Operand)
9058 -- If the Operand Etype is Universal_Fixed, then the conversion is
9059 -- never redundant. We need this check because by the time we have
9060 -- finished the rather complex transformation, the conversion looks
9061 -- redundant when it is not.
9063 if Operand_Typ = Universal_Fixed then
9064 Test_Redundant := False;
9066 -- If the operand is marked as Any_Fixed, then special processing is
9067 -- required. This is also a case where we suppress the test for a
9068 -- redundant conversion, since most certainly it is not redundant.
9070 elsif Operand_Typ = Any_Fixed then
9071 Test_Redundant := False;
9073 -- Mixed-mode operation involving a literal. Context must be a fixed
9074 -- type which is applied to the literal subsequently.
9076 if Is_Fixed_Point_Type (Typ) then
9077 Set_Etype (Operand, Universal_Real);
9079 elsif Is_Numeric_Type (Typ)
9080 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9081 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9083 Etype (Left_Opnd (Operand)) = Universal_Real)
9085 -- Return if expression is ambiguous
9087 if Unique_Fixed_Point_Type (N) = Any_Type then
9090 -- If nothing else, the available fixed type is Duration
9093 Set_Etype (Operand, Standard_Duration);
9096 -- Resolve the real operand with largest available precision
9098 if Etype (Right_Opnd (Operand)) = Universal_Real then
9099 Rop := New_Copy_Tree (Right_Opnd (Operand));
9101 Rop := New_Copy_Tree (Left_Opnd (Operand));
9104 Resolve (Rop, Universal_Real);
9106 -- If the operand is a literal (it could be a non-static and
9107 -- illegal exponentiation) check whether the use of Duration
9108 -- is potentially inaccurate.
9110 if Nkind (Rop) = N_Real_Literal
9111 and then Realval (Rop) /= Ureal_0
9112 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9115 ("?universal real operand can only " &
9116 "be interpreted as Duration!",
9119 ("\?precision will be lost in the conversion!", Rop);
9122 elsif Is_Numeric_Type (Typ)
9123 and then Nkind (Operand) in N_Op
9124 and then Unique_Fixed_Point_Type (N) /= Any_Type
9126 Set_Etype (Operand, Standard_Duration);
9129 Error_Msg_N ("invalid context for mixed mode operation", N);
9130 Set_Etype (Operand, Any_Type);
9137 -- In SPARK, a type conversion between array types should be restricted
9138 -- to types which have matching static bounds.
9140 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9141 -- operation if not needed.
9143 if Restriction_Check_Required (SPARK)
9144 and then Is_Array_Type (Target_Typ)
9145 and then Is_Array_Type (Operand_Typ)
9146 and then Operand_Typ /= Any_Composite -- or else Operand in error
9147 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9149 Check_SPARK_Restriction
9150 ("array types should have matching static bounds", N);
9153 -- In formal mode, the operand of an ancestor type conversion must be an
9154 -- object (not an expression).
9156 if Is_Tagged_Type (Target_Typ)
9157 and then not Is_Class_Wide_Type (Target_Typ)
9158 and then Is_Tagged_Type (Operand_Typ)
9159 and then not Is_Class_Wide_Type (Operand_Typ)
9160 and then Is_Ancestor (Target_Typ, Operand_Typ)
9161 and then not Is_SPARK_Object_Reference (Operand)
9163 Check_SPARK_Restriction ("object required", Operand);
9166 -- Note: we do the Eval_Type_Conversion call before applying the
9167 -- required checks for a subtype conversion. This is important, since
9168 -- both are prepared under certain circumstances to change the type
9169 -- conversion to a constraint error node, but in the case of
9170 -- Eval_Type_Conversion this may reflect an illegality in the static
9171 -- case, and we would miss the illegality (getting only a warning
9172 -- message), if we applied the type conversion checks first.
9174 Eval_Type_Conversion (N);
9176 -- Even when evaluation is not possible, we may be able to simplify the
9177 -- conversion or its expression. This needs to be done before applying
9178 -- checks, since otherwise the checks may use the original expression
9179 -- and defeat the simplifications. This is specifically the case for
9180 -- elimination of the floating-point Truncation attribute in
9181 -- float-to-int conversions.
9183 Simplify_Type_Conversion (N);
9185 -- If after evaluation we still have a type conversion, then we may need
9186 -- to apply checks required for a subtype conversion.
9188 -- Skip these type conversion checks if universal fixed operands
9189 -- operands involved, since range checks are handled separately for
9190 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9192 if Nkind (N) = N_Type_Conversion
9193 and then not Is_Generic_Type (Root_Type (Target_Typ))
9194 and then Target_Typ /= Universal_Fixed
9195 and then Operand_Typ /= Universal_Fixed
9197 Apply_Type_Conversion_Checks (N);
9200 -- Issue warning for conversion of simple object to its own type. We
9201 -- have to test the original nodes, since they may have been rewritten
9202 -- by various optimizations.
9204 Orig_N := Original_Node (N);
9206 -- Here we test for a redundant conversion if the warning mode is
9207 -- active (and was not locally reset), and we have a type conversion
9208 -- from source not appearing in a generic instance.
9211 and then Nkind (Orig_N) = N_Type_Conversion
9212 and then Comes_From_Source (Orig_N)
9213 and then not In_Instance
9215 Orig_N := Original_Node (Expression (Orig_N));
9216 Orig_T := Target_Typ;
9218 -- If the node is part of a larger expression, the Target_Type
9219 -- may not be the original type of the node if the context is a
9220 -- condition. Recover original type to see if conversion is needed.
9222 if Is_Boolean_Type (Orig_T)
9223 and then Nkind (Parent (N)) in N_Op
9225 Orig_T := Etype (Parent (N));
9228 -- If we have an entity name, then give the warning if the entity
9229 -- is the right type, or if it is a loop parameter covered by the
9230 -- original type (that's needed because loop parameters have an
9231 -- odd subtype coming from the bounds).
9233 if (Is_Entity_Name (Orig_N)
9235 (Etype (Entity (Orig_N)) = Orig_T
9237 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9238 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9240 -- If not an entity, then type of expression must match
9242 or else Etype (Orig_N) = Orig_T
9244 -- One more check, do not give warning if the analyzed conversion
9245 -- has an expression with non-static bounds, and the bounds of the
9246 -- target are static. This avoids junk warnings in cases where the
9247 -- conversion is necessary to establish staticness, for example in
9248 -- a case statement.
9250 if not Is_OK_Static_Subtype (Operand_Typ)
9251 and then Is_OK_Static_Subtype (Target_Typ)
9255 -- Finally, if this type conversion occurs in a context requiring
9256 -- a prefix, and the expression is a qualified expression then the
9257 -- type conversion is not redundant, since a qualified expression
9258 -- is not a prefix, whereas a type conversion is. For example, "X
9259 -- := T'(Funx(...)).Y;" is illegal because a selected component
9260 -- requires a prefix, but a type conversion makes it legal: "X :=
9261 -- T(T'(Funx(...))).Y;"
9263 -- In Ada 2012, a qualified expression is a name, so this idiom is
9264 -- no longer needed, but we still suppress the warning because it
9265 -- seems unfriendly for warnings to pop up when you switch to the
9266 -- newer language version.
9268 elsif Nkind (Orig_N) = N_Qualified_Expression
9269 and then Nkind_In (Parent (N), N_Attribute_Reference,
9270 N_Indexed_Component,
9271 N_Selected_Component,
9273 N_Explicit_Dereference)
9277 -- Here we give the redundant conversion warning. If it is an
9278 -- entity, give the name of the entity in the message. If not,
9279 -- just mention the expression.
9282 if Is_Entity_Name (Orig_N) then
9283 Error_Msg_Node_2 := Orig_T;
9284 Error_Msg_NE -- CODEFIX
9285 ("?redundant conversion, & is of type &!",
9286 N, Entity (Orig_N));
9289 ("?redundant conversion, expression is of type&!",
9296 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9297 -- No need to perform any interface conversion if the type of the
9298 -- expression coincides with the target type.
9300 if Ada_Version >= Ada_2005
9301 and then Expander_Active
9302 and then Operand_Typ /= Target_Typ
9305 Opnd : Entity_Id := Operand_Typ;
9306 Target : Entity_Id := Target_Typ;
9309 if Is_Access_Type (Opnd) then
9310 Opnd := Designated_Type (Opnd);
9313 if Is_Access_Type (Target_Typ) then
9314 Target := Designated_Type (Target);
9317 if Opnd = Target then
9320 -- Conversion from interface type
9322 elsif Is_Interface (Opnd) then
9324 -- Ada 2005 (AI-217): Handle entities from limited views
9326 if From_With_Type (Opnd) then
9327 Error_Msg_Qual_Level := 99;
9328 Error_Msg_NE -- CODEFIX
9329 ("missing WITH clause on package &", N,
9330 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9332 ("type conversions require visibility of the full view",
9335 elsif From_With_Type (Target)
9337 (Is_Access_Type (Target_Typ)
9338 and then Present (Non_Limited_View (Etype (Target))))
9340 Error_Msg_Qual_Level := 99;
9341 Error_Msg_NE -- CODEFIX
9342 ("missing WITH clause on package &", N,
9343 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9345 ("type conversions require visibility of the full view",
9349 Expand_Interface_Conversion (N, Is_Static => False);
9352 -- Conversion to interface type
9354 elsif Is_Interface (Target) then
9358 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9359 Opnd := Etype (Opnd);
9362 if not Interface_Present_In_Ancestor
9366 if Is_Class_Wide_Type (Opnd) then
9368 -- The static analysis is not enough to know if the
9369 -- interface is implemented or not. Hence we must pass
9370 -- the work to the expander to generate code to evaluate
9371 -- the conversion at run time.
9373 Expand_Interface_Conversion (N, Is_Static => False);
9376 Error_Msg_Name_1 := Chars (Etype (Target));
9377 Error_Msg_Name_2 := Chars (Opnd);
9379 ("wrong interface conversion (% is not a progenitor " &
9384 Expand_Interface_Conversion (N);
9389 end Resolve_Type_Conversion;
9391 ----------------------
9392 -- Resolve_Unary_Op --
9393 ----------------------
9395 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9396 B_Typ : constant Entity_Id := Base_Type (Typ);
9397 R : constant Node_Id := Right_Opnd (N);
9403 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9404 Error_Msg_Name_1 := Chars (Typ);
9405 Check_SPARK_Restriction
9406 ("unary operator not defined for modular type%", N);
9409 -- Deal with intrinsic unary operators
9411 if Comes_From_Source (N)
9412 and then Ekind (Entity (N)) = E_Function
9413 and then Is_Imported (Entity (N))
9414 and then Is_Intrinsic_Subprogram (Entity (N))
9416 Resolve_Intrinsic_Unary_Operator (N, Typ);
9420 -- Deal with universal cases
9422 if Etype (R) = Universal_Integer
9424 Etype (R) = Universal_Real
9426 Check_For_Visible_Operator (N, B_Typ);
9429 Set_Etype (N, B_Typ);
9432 -- Generate warning for expressions like abs (x mod 2)
9434 if Warn_On_Redundant_Constructs
9435 and then Nkind (N) = N_Op_Abs
9437 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9439 if OK and then Hi >= Lo and then Lo >= 0 then
9440 Error_Msg_N -- CODEFIX
9441 ("?abs applied to known non-negative value has no effect", N);
9445 -- Deal with reference generation
9447 Check_Unset_Reference (R);
9448 Generate_Operator_Reference (N, B_Typ);
9451 -- Set overflow checking bit. Much cleverer code needed here eventually
9452 -- and perhaps the Resolve routines should be separated for the various
9453 -- arithmetic operations, since they will need different processing ???
9455 if Nkind (N) in N_Op then
9456 if not Overflow_Checks_Suppressed (Etype (N)) then
9457 Enable_Overflow_Check (N);
9461 -- Generate warning for expressions like -5 mod 3 for integers. No need
9462 -- to worry in the floating-point case, since parens do not affect the
9463 -- result so there is no point in giving in a warning.
9466 Norig : constant Node_Id := Original_Node (N);
9475 if Warn_On_Questionable_Missing_Parens
9476 and then Comes_From_Source (Norig)
9477 and then Is_Integer_Type (Typ)
9478 and then Nkind (Norig) = N_Op_Minus
9480 Rorig := Original_Node (Right_Opnd (Norig));
9482 -- We are looking for cases where the right operand is not
9483 -- parenthesized, and is a binary operator, multiply, divide, or
9484 -- mod. These are the cases where the grouping can affect results.
9486 if Paren_Count (Rorig) = 0
9487 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9489 -- For mod, we always give the warning, since the value is
9490 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9491 -- -(5 mod 315)). But for the other cases, the only concern is
9492 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9493 -- overflows, but (-2) * 64 does not). So we try to give the
9494 -- message only when overflow is possible.
9496 if Nkind (Rorig) /= N_Op_Mod
9497 and then Compile_Time_Known_Value (R)
9499 Val := Expr_Value (R);
9501 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9502 HB := Expr_Value (Type_High_Bound (Typ));
9504 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9507 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9508 LB := Expr_Value (Type_Low_Bound (Typ));
9510 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9513 -- Note that the test below is deliberately excluding the
9514 -- largest negative number, since that is a potentially
9515 -- troublesome case (e.g. -2 * x, where the result is the
9516 -- largest negative integer has an overflow with 2 * x).
9518 if Val > LB and then Val <= HB then
9523 -- For the multiplication case, the only case we have to worry
9524 -- about is when (-a)*b is exactly the largest negative number
9525 -- so that -(a*b) can cause overflow. This can only happen if
9526 -- a is a power of 2, and more generally if any operand is a
9527 -- constant that is not a power of 2, then the parentheses
9528 -- cannot affect whether overflow occurs. We only bother to
9529 -- test the left most operand
9531 -- Loop looking at left operands for one that has known value
9534 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9535 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9536 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9538 -- Operand value of 0 or 1 skips warning
9543 -- Otherwise check power of 2, if power of 2, warn, if
9544 -- anything else, skip warning.
9547 while Lval /= 2 loop
9548 if Lval mod 2 = 1 then
9559 -- Keep looking at left operands
9561 Opnd := Left_Opnd (Opnd);
9564 -- For rem or "/" we can only have a problematic situation
9565 -- if the divisor has a value of minus one or one. Otherwise
9566 -- overflow is impossible (divisor > 1) or we have a case of
9567 -- division by zero in any case.
9569 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9570 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9571 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9576 -- If we fall through warning should be issued
9579 ("?unary minus expression should be parenthesized here!", N);
9583 end Resolve_Unary_Op;
9585 ----------------------------------
9586 -- Resolve_Unchecked_Expression --
9587 ----------------------------------
9589 procedure Resolve_Unchecked_Expression
9594 Resolve (Expression (N), Typ, Suppress => All_Checks);
9596 end Resolve_Unchecked_Expression;
9598 ---------------------------------------
9599 -- Resolve_Unchecked_Type_Conversion --
9600 ---------------------------------------
9602 procedure Resolve_Unchecked_Type_Conversion
9606 pragma Warnings (Off, Typ);
9608 Operand : constant Node_Id := Expression (N);
9609 Opnd_Type : constant Entity_Id := Etype (Operand);
9612 -- Resolve operand using its own type
9614 Resolve (Operand, Opnd_Type);
9615 Eval_Unchecked_Conversion (N);
9616 end Resolve_Unchecked_Type_Conversion;
9618 ------------------------------
9619 -- Rewrite_Operator_As_Call --
9620 ------------------------------
9622 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9623 Loc : constant Source_Ptr := Sloc (N);
9624 Actuals : constant List_Id := New_List;
9628 if Nkind (N) in N_Binary_Op then
9629 Append (Left_Opnd (N), Actuals);
9632 Append (Right_Opnd (N), Actuals);
9635 Make_Function_Call (Sloc => Loc,
9636 Name => New_Occurrence_Of (Nam, Loc),
9637 Parameter_Associations => Actuals);
9639 Preserve_Comes_From_Source (New_N, N);
9640 Preserve_Comes_From_Source (Name (New_N), N);
9642 Set_Etype (N, Etype (Nam));
9643 end Rewrite_Operator_As_Call;
9645 ------------------------------
9646 -- Rewrite_Renamed_Operator --
9647 ------------------------------
9649 procedure Rewrite_Renamed_Operator
9654 Nam : constant Name_Id := Chars (Op);
9655 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9659 -- Rewrite the operator node using the real operator, not its renaming.
9660 -- Exclude user-defined intrinsic operations of the same name, which are
9661 -- treated separately and rewritten as calls.
9663 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9664 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9665 Set_Chars (Op_Node, Nam);
9666 Set_Etype (Op_Node, Etype (N));
9667 Set_Entity (Op_Node, Op);
9668 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9670 -- Indicate that both the original entity and its renaming are
9671 -- referenced at this point.
9673 Generate_Reference (Entity (N), N);
9674 Generate_Reference (Op, N);
9677 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9680 Rewrite (N, Op_Node);
9682 -- If the context type is private, add the appropriate conversions so
9683 -- that the operator is applied to the full view. This is done in the
9684 -- routines that resolve intrinsic operators.
9686 if Is_Intrinsic_Subprogram (Op)
9687 and then Is_Private_Type (Typ)
9690 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9691 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9692 Resolve_Intrinsic_Operator (N, Typ);
9694 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9695 Resolve_Intrinsic_Unary_Operator (N, Typ);
9702 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9704 -- Operator renames a user-defined operator of the same name. Use the
9705 -- original operator in the node, which is the one Gigi knows about.
9708 Set_Is_Overloaded (N, False);
9710 end Rewrite_Renamed_Operator;
9712 -----------------------
9713 -- Set_Slice_Subtype --
9714 -----------------------
9716 -- Build an implicit subtype declaration to represent the type delivered by
9717 -- the slice. This is an abbreviated version of an array subtype. We define
9718 -- an index subtype for the slice, using either the subtype name or the
9719 -- discrete range of the slice. To be consistent with index usage elsewhere
9720 -- we create a list header to hold the single index. This list is not
9721 -- otherwise attached to the syntax tree.
9723 procedure Set_Slice_Subtype (N : Node_Id) is
9724 Loc : constant Source_Ptr := Sloc (N);
9725 Index_List : constant List_Id := New_List;
9727 Index_Subtype : Entity_Id;
9728 Index_Type : Entity_Id;
9729 Slice_Subtype : Entity_Id;
9730 Drange : constant Node_Id := Discrete_Range (N);
9733 if Is_Entity_Name (Drange) then
9734 Index_Subtype := Entity (Drange);
9737 -- We force the evaluation of a range. This is definitely needed in
9738 -- the renamed case, and seems safer to do unconditionally. Note in
9739 -- any case that since we will create and insert an Itype referring
9740 -- to this range, we must make sure any side effect removal actions
9741 -- are inserted before the Itype definition.
9743 if Nkind (Drange) = N_Range then
9744 Force_Evaluation (Low_Bound (Drange));
9745 Force_Evaluation (High_Bound (Drange));
9748 Index_Type := Base_Type (Etype (Drange));
9750 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9752 -- Take a new copy of Drange (where bounds have been rewritten to
9753 -- reference side-effect-free names). Using a separate tree ensures
9754 -- that further expansion (e.g. while rewriting a slice assignment
9755 -- into a FOR loop) does not attempt to remove side effects on the
9756 -- bounds again (which would cause the bounds in the index subtype
9757 -- definition to refer to temporaries before they are defined) (the
9758 -- reason is that some names are considered side effect free here
9759 -- for the subtype, but not in the context of a loop iteration
9762 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9763 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
9764 Set_Etype (Index_Subtype, Index_Type);
9765 Set_Size_Info (Index_Subtype, Index_Type);
9766 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9769 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9771 Index := New_Occurrence_Of (Index_Subtype, Loc);
9772 Set_Etype (Index, Index_Subtype);
9773 Append (Index, Index_List);
9775 Set_First_Index (Slice_Subtype, Index);
9776 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9777 Set_Is_Constrained (Slice_Subtype, True);
9779 Check_Compile_Time_Size (Slice_Subtype);
9781 -- The Etype of the existing Slice node is reset to this slice subtype.
9782 -- Its bounds are obtained from its first index.
9784 Set_Etype (N, Slice_Subtype);
9786 -- For packed slice subtypes, freeze immediately (except in the case of
9787 -- being in a "spec expression" where we never freeze when we first see
9790 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9791 Freeze_Itype (Slice_Subtype, N);
9793 -- For all other cases insert an itype reference in the slice's actions
9794 -- so that the itype is frozen at the proper place in the tree (i.e. at
9795 -- the point where actions for the slice are analyzed). Note that this
9796 -- is different from freezing the itype immediately, which might be
9797 -- premature (e.g. if the slice is within a transient scope). This needs
9798 -- to be done only if expansion is enabled.
9800 elsif Expander_Active then
9801 Ensure_Defined (Typ => Slice_Subtype, N => N);
9803 end Set_Slice_Subtype;
9805 --------------------------------
9806 -- Set_String_Literal_Subtype --
9807 --------------------------------
9809 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9810 Loc : constant Source_Ptr := Sloc (N);
9811 Low_Bound : constant Node_Id :=
9812 Type_Low_Bound (Etype (First_Index (Typ)));
9813 Subtype_Id : Entity_Id;
9816 if Nkind (N) /= N_String_Literal then
9820 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
9821 Set_String_Literal_Length (Subtype_Id, UI_From_Int
9822 (String_Length (Strval (N))));
9823 Set_Etype (Subtype_Id, Base_Type (Typ));
9824 Set_Is_Constrained (Subtype_Id);
9825 Set_Etype (N, Subtype_Id);
9827 if Is_OK_Static_Expression (Low_Bound) then
9829 -- The low bound is set from the low bound of the corresponding index
9830 -- type. Note that we do not store the high bound in the string literal
9831 -- subtype, but it can be deduced if necessary from the length and the
9834 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
9837 -- If the lower bound is not static we create a range for the string
9838 -- literal, using the index type and the known length of the literal.
9839 -- The index type is not necessarily Positive, so the upper bound is
9840 -- computed as T'Val (T'Pos (Low_Bound) + L - 1)
9843 Index_List : constant List_Id := New_List;
9844 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
9846 High_Bound : constant Node_Id :=
9847 Make_Attribute_Reference (Loc,
9848 Attribute_Name => Name_Val,
9850 New_Occurrence_Of (Index_Type, Loc),
9851 Expressions => New_List (
9854 Make_Attribute_Reference (Loc,
9855 Attribute_Name => Name_Pos,
9857 New_Occurrence_Of (Index_Type, Loc),
9859 New_List (New_Copy_Tree (Low_Bound))),
9861 Make_Integer_Literal (Loc,
9862 String_Length (Strval (N)) - 1))));
9864 Array_Subtype : Entity_Id;
9865 Index_Subtype : Entity_Id;
9870 if Is_Integer_Type (Index_Type) then
9871 Set_String_Literal_Low_Bound
9872 (Subtype_Id, Make_Integer_Literal (Loc, 1));
9875 -- If the index type is an enumeration type, build bounds
9876 -- expression with attributes.
9878 Set_String_Literal_Low_Bound
9880 Make_Attribute_Reference (Loc,
9881 Attribute_Name => Name_First,
9883 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
9884 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
9887 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
9889 -- Build bona fide subtype for the string, and wrap it in an
9890 -- unchecked conversion, because the backend expects the
9891 -- String_Literal_Subtype to have a static lower bound.
9894 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9895 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
9896 Set_Scalar_Range (Index_Subtype, Drange);
9897 Set_Parent (Drange, N);
9898 Analyze_And_Resolve (Drange, Index_Type);
9900 -- In the context, the Index_Type may already have a constraint,
9901 -- so use common base type on string subtype. The base type may
9902 -- be used when generating attributes of the string, for example
9903 -- in the context of a slice assignment.
9905 Set_Etype (Index_Subtype, Base_Type (Index_Type));
9906 Set_Size_Info (Index_Subtype, Index_Type);
9907 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9909 Array_Subtype := Create_Itype (E_Array_Subtype, N);
9911 Index := New_Occurrence_Of (Index_Subtype, Loc);
9912 Set_Etype (Index, Index_Subtype);
9913 Append (Index, Index_List);
9915 Set_First_Index (Array_Subtype, Index);
9916 Set_Etype (Array_Subtype, Base_Type (Typ));
9917 Set_Is_Constrained (Array_Subtype, True);
9920 Make_Unchecked_Type_Conversion (Loc,
9921 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9922 Expression => Relocate_Node (N)));
9923 Set_Etype (N, Array_Subtype);
9926 end Set_String_Literal_Subtype;
9928 ------------------------------
9929 -- Simplify_Type_Conversion --
9930 ------------------------------
9932 procedure Simplify_Type_Conversion (N : Node_Id) is
9934 if Nkind (N) = N_Type_Conversion then
9936 Operand : constant Node_Id := Expression (N);
9937 Target_Typ : constant Entity_Id := Etype (N);
9938 Opnd_Typ : constant Entity_Id := Etype (Operand);
9941 if Is_Floating_Point_Type (Opnd_Typ)
9943 (Is_Integer_Type (Target_Typ)
9944 or else (Is_Fixed_Point_Type (Target_Typ)
9945 and then Conversion_OK (N)))
9946 and then Nkind (Operand) = N_Attribute_Reference
9947 and then Attribute_Name (Operand) = Name_Truncation
9949 -- Special processing required if the conversion is the expression
9950 -- of a Truncation attribute reference. In this case we replace:
9952 -- ityp (ftyp'Truncation (x))
9958 -- with the Float_Truncate flag set, which is more efficient.
9962 Relocate_Node (First (Expressions (Operand))));
9963 Set_Float_Truncate (N, True);
9967 end Simplify_Type_Conversion;
9969 -----------------------------
9970 -- Unique_Fixed_Point_Type --
9971 -----------------------------
9973 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
9974 T1 : Entity_Id := Empty;
9979 procedure Fixed_Point_Error;
9980 -- Give error messages for true ambiguity. Messages are posted on node
9981 -- N, and entities T1, T2 are the possible interpretations.
9983 -----------------------
9984 -- Fixed_Point_Error --
9985 -----------------------
9987 procedure Fixed_Point_Error is
9989 Error_Msg_N ("ambiguous universal_fixed_expression", N);
9990 Error_Msg_NE ("\\possible interpretation as}", N, T1);
9991 Error_Msg_NE ("\\possible interpretation as}", N, T2);
9992 end Fixed_Point_Error;
9994 -- Start of processing for Unique_Fixed_Point_Type
9997 -- The operations on Duration are visible, so Duration is always a
9998 -- possible interpretation.
10000 T1 := Standard_Duration;
10002 -- Look for fixed-point types in enclosing scopes
10004 Scop := Current_Scope;
10005 while Scop /= Standard_Standard loop
10006 T2 := First_Entity (Scop);
10007 while Present (T2) loop
10008 if Is_Fixed_Point_Type (T2)
10009 and then Current_Entity (T2) = T2
10010 and then Scope (Base_Type (T2)) = Scop
10012 if Present (T1) then
10023 Scop := Scope (Scop);
10026 -- Look for visible fixed type declarations in the context
10028 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10029 while Present (Item) loop
10030 if Nkind (Item) = N_With_Clause then
10031 Scop := Entity (Name (Item));
10032 T2 := First_Entity (Scop);
10033 while Present (T2) loop
10034 if Is_Fixed_Point_Type (T2)
10035 and then Scope (Base_Type (T2)) = Scop
10036 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10038 if Present (T1) then
10053 if Nkind (N) = N_Real_Literal then
10054 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
10056 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
10060 end Unique_Fixed_Point_Type;
10062 ----------------------
10063 -- Valid_Conversion --
10064 ----------------------
10066 function Valid_Conversion
10068 Target : Entity_Id;
10069 Operand : Node_Id) return Boolean
10071 Target_Type : constant Entity_Id := Base_Type (Target);
10072 Opnd_Type : Entity_Id := Etype (Operand);
10074 function Conversion_Check
10076 Msg : String) return Boolean;
10077 -- Little routine to post Msg if Valid is False, returns Valid value
10079 function Valid_Tagged_Conversion
10080 (Target_Type : Entity_Id;
10081 Opnd_Type : Entity_Id) return Boolean;
10082 -- Specifically test for validity of tagged conversions
10084 function Valid_Array_Conversion return Boolean;
10085 -- Check index and component conformance, and accessibility levels if
10086 -- the component types are anonymous access types (Ada 2005).
10088 ----------------------
10089 -- Conversion_Check --
10090 ----------------------
10092 function Conversion_Check
10094 Msg : String) return Boolean
10098 Error_Msg_N (Msg, Operand);
10102 end Conversion_Check;
10104 ----------------------------
10105 -- Valid_Array_Conversion --
10106 ----------------------------
10108 function Valid_Array_Conversion return Boolean
10110 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10111 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10113 Opnd_Index : Node_Id;
10114 Opnd_Index_Type : Entity_Id;
10116 Target_Comp_Type : constant Entity_Id :=
10117 Component_Type (Target_Type);
10118 Target_Comp_Base : constant Entity_Id :=
10119 Base_Type (Target_Comp_Type);
10121 Target_Index : Node_Id;
10122 Target_Index_Type : Entity_Id;
10125 -- Error if wrong number of dimensions
10128 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10131 ("incompatible number of dimensions for conversion", Operand);
10134 -- Number of dimensions matches
10137 -- Loop through indexes of the two arrays
10139 Target_Index := First_Index (Target_Type);
10140 Opnd_Index := First_Index (Opnd_Type);
10141 while Present (Target_Index) and then Present (Opnd_Index) loop
10142 Target_Index_Type := Etype (Target_Index);
10143 Opnd_Index_Type := Etype (Opnd_Index);
10145 -- Error if index types are incompatible
10147 if not (Is_Integer_Type (Target_Index_Type)
10148 and then Is_Integer_Type (Opnd_Index_Type))
10149 and then (Root_Type (Target_Index_Type)
10150 /= Root_Type (Opnd_Index_Type))
10153 ("incompatible index types for array conversion",
10158 Next_Index (Target_Index);
10159 Next_Index (Opnd_Index);
10162 -- If component types have same base type, all set
10164 if Target_Comp_Base = Opnd_Comp_Base then
10167 -- Here if base types of components are not the same. The only
10168 -- time this is allowed is if we have anonymous access types.
10170 -- The conversion of arrays of anonymous access types can lead
10171 -- to dangling pointers. AI-392 formalizes the accessibility
10172 -- checks that must be applied to such conversions to prevent
10173 -- out-of-scope references.
10176 (Target_Comp_Base, E_Anonymous_Access_Type,
10177 E_Anonymous_Access_Subprogram_Type)
10178 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10180 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10182 if Type_Access_Level (Target_Type) <
10183 Type_Access_Level (Opnd_Type)
10185 if In_Instance_Body then
10186 Error_Msg_N ("?source array type " &
10187 "has deeper accessibility level than target", Operand);
10188 Error_Msg_N ("\?Program_Error will be raised at run time",
10191 Make_Raise_Program_Error (Sloc (N),
10192 Reason => PE_Accessibility_Check_Failed));
10193 Set_Etype (N, Target_Type);
10196 -- Conversion not allowed because of accessibility levels
10199 Error_Msg_N ("source array type " &
10200 "has deeper accessibility level than target", Operand);
10208 -- All other cases where component base types do not match
10212 ("incompatible component types for array conversion",
10217 -- Check that component subtypes statically match. For numeric
10218 -- types this means that both must be either constrained or
10219 -- unconstrained. For enumeration types the bounds must match.
10220 -- All of this is checked in Subtypes_Statically_Match.
10222 if not Subtypes_Statically_Match
10223 (Target_Comp_Type, Opnd_Comp_Type)
10226 ("component subtypes must statically match", Operand);
10232 end Valid_Array_Conversion;
10234 -----------------------------
10235 -- Valid_Tagged_Conversion --
10236 -----------------------------
10238 function Valid_Tagged_Conversion
10239 (Target_Type : Entity_Id;
10240 Opnd_Type : Entity_Id) return Boolean
10243 -- Upward conversions are allowed (RM 4.6(22))
10245 if Covers (Target_Type, Opnd_Type)
10246 or else Is_Ancestor (Target_Type, Opnd_Type)
10250 -- Downward conversion are allowed if the operand is class-wide
10253 elsif Is_Class_Wide_Type (Opnd_Type)
10254 and then Covers (Opnd_Type, Target_Type)
10258 elsif Covers (Opnd_Type, Target_Type)
10259 or else Is_Ancestor (Opnd_Type, Target_Type)
10262 Conversion_Check (False,
10263 "downward conversion of tagged objects not allowed");
10265 -- Ada 2005 (AI-251): The conversion to/from interface types is
10268 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10271 -- If the operand is a class-wide type obtained through a limited_
10272 -- with clause, and the context includes the non-limited view, use
10273 -- it to determine whether the conversion is legal.
10275 elsif Is_Class_Wide_Type (Opnd_Type)
10276 and then From_With_Type (Opnd_Type)
10277 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10278 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10282 elsif Is_Access_Type (Opnd_Type)
10283 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10289 ("invalid tagged conversion, not compatible with}",
10290 N, First_Subtype (Opnd_Type));
10293 end Valid_Tagged_Conversion;
10295 -- Start of processing for Valid_Conversion
10298 Check_Parameterless_Call (Operand);
10300 if Is_Overloaded (Operand) then
10310 -- Remove procedure calls, which syntactically cannot appear in
10311 -- this context, but which cannot be removed by type checking,
10312 -- because the context does not impose a type.
10314 -- When compiling for VMS, spurious ambiguities can be produced
10315 -- when arithmetic operations have a literal operand and return
10316 -- System.Address or a descendant of it. These ambiguities are
10317 -- otherwise resolved by the context, but for conversions there
10318 -- is no context type and the removal of the spurious operations
10319 -- must be done explicitly here.
10321 -- The node may be labelled overloaded, but still contain only one
10322 -- interpretation because others were discarded earlier. If this
10323 -- is the case, retain the single interpretation if legal.
10325 Get_First_Interp (Operand, I, It);
10326 Opnd_Type := It.Typ;
10327 Get_Next_Interp (I, It);
10329 if Present (It.Typ)
10330 and then Opnd_Type /= Standard_Void_Type
10332 -- More than one candidate interpretation is available
10334 Get_First_Interp (Operand, I, It);
10335 while Present (It.Typ) loop
10336 if It.Typ = Standard_Void_Type then
10340 if Present (System_Aux_Id)
10341 and then Is_Descendent_Of_Address (It.Typ)
10346 Get_Next_Interp (I, It);
10350 Get_First_Interp (Operand, I, It);
10354 if No (It.Typ) then
10355 Error_Msg_N ("illegal operand in conversion", Operand);
10359 Get_Next_Interp (I, It);
10361 if Present (It.Typ) then
10364 It1 := Disambiguate (Operand, I1, I, Any_Type);
10366 if It1 = No_Interp then
10367 Error_Msg_N ("ambiguous operand in conversion", Operand);
10369 -- If the interpretation involves a standard operator, use
10370 -- the location of the type, which may be user-defined.
10372 if Sloc (It.Nam) = Standard_Location then
10373 Error_Msg_Sloc := Sloc (It.Typ);
10375 Error_Msg_Sloc := Sloc (It.Nam);
10378 Error_Msg_N -- CODEFIX
10379 ("\\possible interpretation#!", Operand);
10381 if Sloc (N1) = Standard_Location then
10382 Error_Msg_Sloc := Sloc (T1);
10384 Error_Msg_Sloc := Sloc (N1);
10387 Error_Msg_N -- CODEFIX
10388 ("\\possible interpretation#!", Operand);
10394 Set_Etype (Operand, It1.Typ);
10395 Opnd_Type := It1.Typ;
10401 if Is_Numeric_Type (Target_Type) then
10403 -- A universal fixed expression can be converted to any numeric type
10405 if Opnd_Type = Universal_Fixed then
10408 -- Also no need to check when in an instance or inlined body, because
10409 -- the legality has been established when the template was analyzed.
10410 -- Furthermore, numeric conversions may occur where only a private
10411 -- view of the operand type is visible at the instantiation point.
10412 -- This results in a spurious error if we check that the operand type
10413 -- is a numeric type.
10415 -- Note: in a previous version of this unit, the following tests were
10416 -- applied only for generated code (Comes_From_Source set to False),
10417 -- but in fact the test is required for source code as well, since
10418 -- this situation can arise in source code.
10420 elsif In_Instance or else In_Inlined_Body then
10423 -- Otherwise we need the conversion check
10426 return Conversion_Check
10427 (Is_Numeric_Type (Opnd_Type),
10428 "illegal operand for numeric conversion");
10433 elsif Is_Array_Type (Target_Type) then
10434 if not Is_Array_Type (Opnd_Type)
10435 or else Opnd_Type = Any_Composite
10436 or else Opnd_Type = Any_String
10438 Error_Msg_N ("illegal operand for array conversion", Operand);
10441 return Valid_Array_Conversion;
10444 -- Ada 2005 (AI-251): Anonymous access types where target references an
10447 elsif Ekind_In (Target_Type, E_General_Access_Type,
10448 E_Anonymous_Access_Type)
10449 and then Is_Interface (Directly_Designated_Type (Target_Type))
10451 -- Check the static accessibility rule of 4.6(17). Note that the
10452 -- check is not enforced when within an instance body, since the
10453 -- RM requires such cases to be caught at run time.
10455 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10456 if Type_Access_Level (Opnd_Type) >
10457 Type_Access_Level (Target_Type)
10459 -- In an instance, this is a run-time check, but one we know
10460 -- will fail, so generate an appropriate warning. The raise
10461 -- will be generated by Expand_N_Type_Conversion.
10463 if In_Instance_Body then
10465 ("?cannot convert local pointer to non-local access type",
10468 ("\?Program_Error will be raised at run time", Operand);
10471 ("cannot convert local pointer to non-local access type",
10476 -- Special accessibility checks are needed in the case of access
10477 -- discriminants declared for a limited type.
10479 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10480 and then not Is_Local_Anonymous_Access (Opnd_Type)
10482 -- When the operand is a selected access discriminant the check
10483 -- needs to be made against the level of the object denoted by
10484 -- the prefix of the selected name (Object_Access_Level handles
10485 -- checking the prefix of the operand for this case).
10487 if Nkind (Operand) = N_Selected_Component
10488 and then Object_Access_Level (Operand) >
10489 Type_Access_Level (Target_Type)
10491 -- In an instance, this is a run-time check, but one we know
10492 -- will fail, so generate an appropriate warning. The raise
10493 -- will be generated by Expand_N_Type_Conversion.
10495 if In_Instance_Body then
10497 ("?cannot convert access discriminant to non-local" &
10498 " access type", Operand);
10500 ("\?Program_Error will be raised at run time", Operand);
10503 ("cannot convert access discriminant to non-local" &
10504 " access type", Operand);
10509 -- The case of a reference to an access discriminant from
10510 -- within a limited type declaration (which will appear as
10511 -- a discriminal) is always illegal because the level of the
10512 -- discriminant is considered to be deeper than any (nameable)
10515 if Is_Entity_Name (Operand)
10516 and then not Is_Local_Anonymous_Access (Opnd_Type)
10518 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10519 and then Present (Discriminal_Link (Entity (Operand)))
10522 ("discriminant has deeper accessibility level than target",
10531 -- General and anonymous access types
10533 elsif Ekind_In (Target_Type, E_General_Access_Type,
10534 E_Anonymous_Access_Type)
10537 (Is_Access_Type (Opnd_Type)
10539 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10540 E_Access_Protected_Subprogram_Type),
10541 "must be an access-to-object type")
10543 if Is_Access_Constant (Opnd_Type)
10544 and then not Is_Access_Constant (Target_Type)
10547 ("access-to-constant operand type not allowed", Operand);
10551 -- Check the static accessibility rule of 4.6(17). Note that the
10552 -- check is not enforced when within an instance body, since the RM
10553 -- requires such cases to be caught at run time.
10555 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10556 or else Is_Local_Anonymous_Access (Target_Type)
10558 if Type_Access_Level (Opnd_Type)
10559 > Type_Access_Level (Target_Type)
10561 -- In an instance, this is a run-time check, but one we know
10562 -- will fail, so generate an appropriate warning. The raise
10563 -- will be generated by Expand_N_Type_Conversion.
10565 if In_Instance_Body then
10567 ("?cannot convert local pointer to non-local access type",
10570 ("\?Program_Error will be raised at run time", Operand);
10573 -- Avoid generation of spurious error message
10575 if not Error_Posted (N) then
10577 ("cannot convert local pointer to non-local access type",
10584 -- Special accessibility checks are needed in the case of access
10585 -- discriminants declared for a limited type.
10587 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10588 and then not Is_Local_Anonymous_Access (Opnd_Type)
10590 -- When the operand is a selected access discriminant the check
10591 -- needs to be made against the level of the object denoted by
10592 -- the prefix of the selected name (Object_Access_Level handles
10593 -- checking the prefix of the operand for this case).
10595 if Nkind (Operand) = N_Selected_Component
10596 and then Object_Access_Level (Operand) >
10597 Type_Access_Level (Target_Type)
10599 -- In an instance, this is a run-time check, but one we know
10600 -- will fail, so generate an appropriate warning. The raise
10601 -- will be generated by Expand_N_Type_Conversion.
10603 if In_Instance_Body then
10605 ("?cannot convert access discriminant to non-local" &
10606 " access type", Operand);
10608 ("\?Program_Error will be raised at run time",
10613 ("cannot convert access discriminant to non-local" &
10614 " access type", Operand);
10619 -- The case of a reference to an access discriminant from
10620 -- within a limited type declaration (which will appear as
10621 -- a discriminal) is always illegal because the level of the
10622 -- discriminant is considered to be deeper than any (nameable)
10625 if Is_Entity_Name (Operand)
10627 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10628 and then Present (Discriminal_Link (Entity (Operand)))
10631 ("discriminant has deeper accessibility level than target",
10638 -- In the presence of limited_with clauses we have to use non-limited
10639 -- views, if available.
10641 Check_Limited : declare
10642 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10643 -- Helper function to handle limited views
10645 --------------------------
10646 -- Full_Designated_Type --
10647 --------------------------
10649 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10650 Desig : constant Entity_Id := Designated_Type (T);
10653 -- Handle the limited view of a type
10655 if Is_Incomplete_Type (Desig)
10656 and then From_With_Type (Desig)
10657 and then Present (Non_Limited_View (Desig))
10659 return Available_View (Desig);
10663 end Full_Designated_Type;
10665 -- Local Declarations
10667 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10668 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10670 Same_Base : constant Boolean :=
10671 Base_Type (Target) = Base_Type (Opnd);
10673 -- Start of processing for Check_Limited
10676 if Is_Tagged_Type (Target) then
10677 return Valid_Tagged_Conversion (Target, Opnd);
10680 if not Same_Base then
10682 ("target designated type not compatible with }",
10683 N, Base_Type (Opnd));
10686 -- Ada 2005 AI-384: legality rule is symmetric in both
10687 -- designated types. The conversion is legal (with possible
10688 -- constraint check) if either designated type is
10691 elsif Subtypes_Statically_Match (Target, Opnd)
10693 (Has_Discriminants (Target)
10695 (not Is_Constrained (Opnd)
10696 or else not Is_Constrained (Target)))
10698 -- Special case, if Value_Size has been used to make the
10699 -- sizes different, the conversion is not allowed even
10700 -- though the subtypes statically match.
10702 if Known_Static_RM_Size (Target)
10703 and then Known_Static_RM_Size (Opnd)
10704 and then RM_Size (Target) /= RM_Size (Opnd)
10707 ("target designated subtype not compatible with }",
10710 ("\because sizes of the two designated subtypes differ",
10714 -- Normal case where conversion is allowed
10722 ("target designated subtype not compatible with }",
10729 -- Access to subprogram types. If the operand is an access parameter,
10730 -- the type has a deeper accessibility that any master, and cannot be
10731 -- assigned. We must make an exception if the conversion is part of an
10732 -- assignment and the target is the return object of an extended return
10733 -- statement, because in that case the accessibility check takes place
10734 -- after the return.
10736 elsif Is_Access_Subprogram_Type (Target_Type)
10737 and then No (Corresponding_Remote_Type (Opnd_Type))
10739 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10740 and then Is_Entity_Name (Operand)
10741 and then Ekind (Entity (Operand)) = E_In_Parameter
10743 (Nkind (Parent (N)) /= N_Assignment_Statement
10744 or else not Is_Entity_Name (Name (Parent (N)))
10745 or else not Is_Return_Object (Entity (Name (Parent (N)))))
10748 ("illegal attempt to store anonymous access to subprogram",
10751 ("\value has deeper accessibility than any master " &
10752 "(RM 3.10.2 (13))",
10756 ("\use named access type for& instead of access parameter",
10757 Operand, Entity (Operand));
10760 -- Check that the designated types are subtype conformant
10762 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
10763 Old_Id => Designated_Type (Opnd_Type),
10766 -- Check the static accessibility rule of 4.6(20)
10768 if Type_Access_Level (Opnd_Type) >
10769 Type_Access_Level (Target_Type)
10772 ("operand type has deeper accessibility level than target",
10775 -- Check that if the operand type is declared in a generic body,
10776 -- then the target type must be declared within that same body
10777 -- (enforces last sentence of 4.6(20)).
10779 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
10781 O_Gen : constant Node_Id :=
10782 Enclosing_Generic_Body (Opnd_Type);
10787 T_Gen := Enclosing_Generic_Body (Target_Type);
10788 while Present (T_Gen) and then T_Gen /= O_Gen loop
10789 T_Gen := Enclosing_Generic_Body (T_Gen);
10792 if T_Gen /= O_Gen then
10794 ("target type must be declared in same generic body"
10795 & " as operand type", N);
10802 -- Remote subprogram access types
10804 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
10805 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
10807 -- It is valid to convert from one RAS type to another provided
10808 -- that their specification statically match.
10810 Check_Subtype_Conformant
10812 Designated_Type (Corresponding_Remote_Type (Target_Type)),
10814 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
10819 -- If both are tagged types, check legality of view conversions
10821 elsif Is_Tagged_Type (Target_Type)
10823 Is_Tagged_Type (Opnd_Type)
10825 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
10827 -- Types derived from the same root type are convertible
10829 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
10832 -- In an instance or an inlined body, there may be inconsistent views of
10833 -- the same type, or of types derived from a common root.
10835 elsif (In_Instance or In_Inlined_Body)
10837 Root_Type (Underlying_Type (Target_Type)) =
10838 Root_Type (Underlying_Type (Opnd_Type))
10842 -- Special check for common access type error case
10844 elsif Ekind (Target_Type) = E_Access_Type
10845 and then Is_Access_Type (Opnd_Type)
10847 Error_Msg_N ("target type must be general access type!", N);
10848 Error_Msg_NE -- CODEFIX
10849 ("add ALL to }!", N, Target_Type);
10853 Error_Msg_NE ("invalid conversion, not compatible with }",
10857 end Valid_Conversion;