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 Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
48 with Output; use Output;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Aggr; use Sem_Aggr;
55 with Sem_Attr; use Sem_Attr;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch4; use Sem_Ch4;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Elim; use Sem_Elim;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Intr; use Sem_Intr;
67 with Sem_Util; use Sem_Util;
68 with Sem_Type; use Sem_Type;
69 with Sem_Warn; use Sem_Warn;
70 with Sinfo; use Sinfo;
71 with Sinfo.CN; use Sinfo.CN;
72 with Snames; use Snames;
73 with Stand; use Stand;
74 with Stringt; use Stringt;
75 with Style; use Style;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
80 package body Sem_Res is
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
86 -- Second pass (top-down) type checking and overload resolution procedures
87 -- Typ is the type required by context. These procedures propagate the type
88 -- information recursively to the descendants of N. If the node is not
89 -- overloaded, its Etype is established in the first pass. If overloaded,
90 -- the Resolve routines set the correct type. For arith. operators, the
91 -- Etype is the base type of the context.
93 -- Note that Resolve_Attribute is separated off in Sem_Attr
95 function Bad_Unordered_Enumeration_Reference
97 T : Entity_Id) return Boolean;
98 -- Node N contains a potentially dubious reference to type T, either an
99 -- explicit comparison, or an explicit range. This function returns True
100 -- if the type T is an enumeration type for which No pragma Order has been
101 -- given, and the reference N is not in the same extended source unit as
102 -- the declaration of T.
104 procedure Check_Discriminant_Use (N : Node_Id);
105 -- Enforce the restrictions on the use of discriminants when constraining
106 -- a component of a discriminated type (record or concurrent type).
108 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
109 -- Given a node for an operator associated with type T, check that
110 -- the operator is visible. Operators all of whose operands are
111 -- universal must be checked for visibility during resolution
112 -- because their type is not determinable based on their operands.
114 procedure Check_Fully_Declared_Prefix
117 -- Check that the type of the prefix of a dereference is not incomplete
119 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
120 -- Given a call node, N, which is known to occur immediately within the
121 -- subprogram being called, determines whether it is a detectable case of
122 -- an infinite recursion, and if so, outputs appropriate messages. Returns
123 -- True if an infinite recursion is detected, and False otherwise.
125 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
126 -- If the type of the object being initialized uses the secondary stack
127 -- directly or indirectly, create a transient scope for the call to the
128 -- init proc. This is because we do not create transient scopes for the
129 -- initialization of individual components within the init proc itself.
130 -- Could be optimized away perhaps?
132 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
133 -- N is the node for a logical operator. If the operator is predefined, and
134 -- the root type of the operands is Standard.Boolean, then a check is made
135 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
136 -- the style check for Style_Check_Boolean_And_Or.
138 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
139 -- Determine whether E is an access type declared by an access declaration,
140 -- and not an (anonymous) allocator type.
142 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
143 -- Utility to check whether the entity for an operator is a predefined
144 -- operator, in which case the expression is left as an operator in the
145 -- tree (else it is rewritten into a call). An instance of an intrinsic
146 -- conversion operation may be given an operator name, but is not treated
147 -- like an operator. Note that an operator that is an imported back-end
148 -- builtin has convention Intrinsic, but is expected to be rewritten into
149 -- a call, so such an operator is not treated as predefined by this
152 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
153 -- If a default expression in entry call N depends on the discriminants
154 -- of the task, it must be replaced with a reference to the discriminant
155 -- of the task being called.
157 procedure Resolve_Op_Concat_Arg
162 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
163 -- concatenation operator. The operand is either of the array type or of
164 -- the component type. If the operand is an aggregate, and the component
165 -- type is composite, this is ambiguous if component type has aggregates.
167 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
168 -- Does the first part of the work of Resolve_Op_Concat
170 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
171 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
172 -- has been resolved. See Resolve_Op_Concat for details.
174 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
210 function Operator_Kind
212 Is_Binary : Boolean) return Node_Kind;
213 -- Utility to map the name of an operator into the corresponding Node. Used
214 -- by other node rewriting procedures.
216 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
217 -- Resolve actuals of call, and add default expressions for missing ones.
218 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
219 -- called subprogram.
221 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
222 -- Called from Resolve_Call, when the prefix denotes an entry or element
223 -- of entry family. Actuals are resolved as for subprograms, and the node
224 -- is rebuilt as an entry call. Also called for protected operations. Typ
225 -- is the context type, which is used when the operation is a protected
226 -- function with no arguments, and the return value is indexed.
228 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
229 -- A call to a user-defined intrinsic operator is rewritten as a call to
230 -- the corresponding predefined operator, with suitable conversions. Note
231 -- that this applies only for intrinsic operators that denote predefined
232 -- operators, not ones that are intrinsic imports of back-end builtins.
234 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
235 -- Ditto, for unary operators (arithmetic ones and "not" on signed
236 -- integer types for VMS).
238 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
239 -- If an operator node resolves to a call to a user-defined operator,
240 -- rewrite the node as a function call.
242 procedure Make_Call_Into_Operator
246 -- Inverse transformation: if an operator is given in functional notation,
247 -- then after resolving the node, transform into an operator node, so
248 -- that operands are resolved properly. Recall that predefined operators
249 -- do not have a full signature and special resolution rules apply.
251 procedure Rewrite_Renamed_Operator
255 -- An operator can rename another, e.g. in an instantiation. In that
256 -- case, the proper operator node must be constructed and resolved.
258 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
259 -- The String_Literal_Subtype is built for all strings that are not
260 -- operands of a static concatenation operation. If the argument is
261 -- not a N_String_Literal node, then the call has no effect.
263 procedure Set_Slice_Subtype (N : Node_Id);
264 -- Build subtype of array type, with the range specified by the slice
266 procedure Simplify_Type_Conversion (N : Node_Id);
267 -- Called after N has been resolved and evaluated, but before range checks
268 -- have been applied. Currently simplifies a combination of floating-point
269 -- to integer conversion and Truncation attribute.
271 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
272 -- A universal_fixed expression in an universal context is unambiguous if
273 -- there is only one applicable fixed point type. Determining whether there
274 -- is only one requires a search over all visible entities, and happens
275 -- only in very pathological cases (see 6115-006).
277 function Valid_Conversion
280 Operand : Node_Id) return Boolean;
281 -- Verify legality rules given in 4.6 (8-23). Target is the target type
282 -- of the conversion, which may be an implicit conversion of an actual
283 -- parameter to an anonymous access type (in which case N denotes the
284 -- actual parameter and N = Operand).
286 -------------------------
287 -- Ambiguous_Character --
288 -------------------------
290 procedure Ambiguous_Character (C : Node_Id) is
294 if Nkind (C) = N_Character_Literal then
295 Error_Msg_N ("ambiguous character literal", C);
297 -- First the ones in Standard
299 Error_Msg_N ("\\possible interpretation: Character!", C);
300 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
302 -- Include Wide_Wide_Character in Ada 2005 mode
304 if Ada_Version >= Ada_2005 then
305 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
308 -- Now any other types that match
310 E := Current_Entity (C);
311 while Present (E) loop
312 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
316 end Ambiguous_Character;
318 -------------------------
319 -- Analyze_And_Resolve --
320 -------------------------
322 procedure Analyze_And_Resolve (N : Node_Id) is
326 end Analyze_And_Resolve;
328 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
332 end Analyze_And_Resolve;
334 -- Version withs check(s) suppressed
336 procedure Analyze_And_Resolve
341 Scop : constant Entity_Id := Current_Scope;
344 if Suppress = All_Checks then
346 Svg : constant Suppress_Array := Scope_Suppress;
348 Scope_Suppress := (others => True);
349 Analyze_And_Resolve (N, Typ);
350 Scope_Suppress := Svg;
355 Svg : constant Boolean := Scope_Suppress (Suppress);
358 Scope_Suppress (Suppress) := True;
359 Analyze_And_Resolve (N, Typ);
360 Scope_Suppress (Suppress) := Svg;
364 if Current_Scope /= Scop
365 and then Scope_Is_Transient
367 -- This can only happen if a transient scope was created for an inner
368 -- expression, which will be removed upon completion of the analysis
369 -- of an enclosing construct. The transient scope must have the
370 -- suppress status of the enclosing environment, not of this Analyze
373 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
376 end Analyze_And_Resolve;
378 procedure Analyze_And_Resolve
382 Scop : constant Entity_Id := Current_Scope;
385 if Suppress = All_Checks then
387 Svg : constant Suppress_Array := Scope_Suppress;
389 Scope_Suppress := (others => True);
390 Analyze_And_Resolve (N);
391 Scope_Suppress := Svg;
396 Svg : constant Boolean := Scope_Suppress (Suppress);
399 Scope_Suppress (Suppress) := True;
400 Analyze_And_Resolve (N);
401 Scope_Suppress (Suppress) := Svg;
405 if Current_Scope /= Scop
406 and then Scope_Is_Transient
408 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
411 end Analyze_And_Resolve;
413 ----------------------------------------
414 -- Bad_Unordered_Enumeration_Reference --
415 ----------------------------------------
417 function Bad_Unordered_Enumeration_Reference
419 T : Entity_Id) return Boolean
422 return Is_Enumeration_Type (T)
423 and then Comes_From_Source (N)
424 and then Warn_On_Unordered_Enumeration_Type
425 and then not Has_Pragma_Ordered (T)
426 and then not In_Same_Extended_Unit (N, T);
427 end Bad_Unordered_Enumeration_Reference;
429 ----------------------------
430 -- Check_Discriminant_Use --
431 ----------------------------
433 procedure Check_Discriminant_Use (N : Node_Id) is
434 PN : constant Node_Id := Parent (N);
435 Disc : constant Entity_Id := Entity (N);
440 -- Any use in a spec-expression is legal
442 if In_Spec_Expression then
445 elsif Nkind (PN) = N_Range then
447 -- Discriminant cannot be used to constrain a scalar type
451 if Nkind (P) = N_Range_Constraint
452 and then Nkind (Parent (P)) = N_Subtype_Indication
453 and then Nkind (Parent (Parent (P))) = N_Component_Definition
455 Error_Msg_N ("discriminant cannot constrain scalar type", N);
457 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
459 -- The following check catches the unusual case where a
460 -- discriminant appears within an index constraint that is part of
461 -- a larger expression within a constraint on a component, e.g. "C
462 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
463 -- of record components, and note that a similar check should also
464 -- apply in the case of discriminant constraints below. ???
466 -- Note that the check for N_Subtype_Declaration below is to
467 -- detect the valid use of discriminants in the constraints of a
468 -- subtype declaration when this subtype declaration appears
469 -- inside the scope of a record type (which is syntactically
470 -- illegal, but which may be created as part of derived type
471 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
474 if Ekind (Current_Scope) = E_Record_Type
475 and then Scope (Disc) = Current_Scope
477 (Nkind (Parent (P)) = N_Subtype_Indication
479 Nkind_In (Parent (Parent (P)), N_Component_Definition,
480 N_Subtype_Declaration)
481 and then Paren_Count (N) = 0)
484 ("discriminant must appear alone in component constraint", N);
488 -- Detect a common error:
490 -- type R (D : Positive := 100) is record
491 -- Name : String (1 .. D);
494 -- The default value causes an object of type R to be allocated
495 -- with room for Positive'Last characters. The RM does not mandate
496 -- the allocation of the maximum size, but that is what GNAT does
497 -- so we should warn the programmer that there is a problem.
499 Check_Large : declare
505 function Large_Storage_Type (T : Entity_Id) return Boolean;
506 -- Return True if type T has a large enough range that any
507 -- array whose index type covered the whole range of the type
508 -- would likely raise Storage_Error.
510 ------------------------
511 -- Large_Storage_Type --
512 ------------------------
514 function Large_Storage_Type (T : Entity_Id) return Boolean is
516 -- The type is considered large if its bounds are known at
517 -- compile time and if it requires at least as many bits as
518 -- a Positive to store the possible values.
520 return Compile_Time_Known_Value (Type_Low_Bound (T))
521 and then Compile_Time_Known_Value (Type_High_Bound (T))
523 Minimum_Size (T, Biased => True) >=
524 RM_Size (Standard_Positive);
525 end Large_Storage_Type;
527 -- Start of processing for Check_Large
530 -- Check that the Disc has a large range
532 if not Large_Storage_Type (Etype (Disc)) then
536 -- If the enclosing type is limited, we allocate only the
537 -- default value, not the maximum, and there is no need for
540 if Is_Limited_Type (Scope (Disc)) then
544 -- Check that it is the high bound
546 if N /= High_Bound (PN)
547 or else No (Discriminant_Default_Value (Disc))
552 -- Check the array allows a large range at this bound. First
557 if Nkind (SI) /= N_Subtype_Indication then
561 T := Entity (Subtype_Mark (SI));
563 if not Is_Array_Type (T) then
567 -- Next, find the dimension
569 TB := First_Index (T);
570 CB := First (Constraints (P));
572 and then Present (TB)
573 and then Present (CB)
584 -- Now, check the dimension has a large range
586 if not Large_Storage_Type (Etype (TB)) then
590 -- Warn about the danger
593 ("?creation of & object may raise Storage_Error!",
602 -- Legal case is in index or discriminant constraint
604 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
605 N_Discriminant_Association)
607 if Paren_Count (N) > 0 then
609 ("discriminant in constraint must appear alone", N);
611 elsif Nkind (N) = N_Expanded_Name
612 and then Comes_From_Source (N)
615 ("discriminant must appear alone as a direct name", N);
620 -- Otherwise, context is an expression. It should not be within (i.e. a
621 -- subexpression of) a constraint for a component.
626 while not Nkind_In (P, N_Component_Declaration,
627 N_Subtype_Indication,
635 -- If the discriminant is used in an expression that is a bound of a
636 -- scalar type, an Itype is created and the bounds are attached to
637 -- its range, not to the original subtype indication. Such use is of
638 -- course a double fault.
640 if (Nkind (P) = N_Subtype_Indication
641 and then Nkind_In (Parent (P), N_Component_Definition,
642 N_Derived_Type_Definition)
643 and then D = Constraint (P))
645 -- The constraint itself may be given by a subtype indication,
646 -- rather than by a more common discrete range.
648 or else (Nkind (P) = N_Subtype_Indication
650 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
651 or else Nkind (P) = N_Entry_Declaration
652 or else Nkind (D) = N_Defining_Identifier
655 ("discriminant in constraint must appear alone", N);
658 end Check_Discriminant_Use;
660 --------------------------------
661 -- Check_For_Visible_Operator --
662 --------------------------------
664 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
666 if Is_Invisible_Operator (N, T) then
667 Error_Msg_NE -- CODEFIX
668 ("operator for} is not directly visible!", N, First_Subtype (T));
669 Error_Msg_N -- CODEFIX
670 ("use clause would make operation legal!", N);
672 end Check_For_Visible_Operator;
674 ----------------------------------
675 -- Check_Fully_Declared_Prefix --
676 ----------------------------------
678 procedure Check_Fully_Declared_Prefix
683 -- Check that the designated type of the prefix of a dereference is
684 -- not an incomplete type. This cannot be done unconditionally, because
685 -- dereferences of private types are legal in default expressions. This
686 -- case is taken care of in Check_Fully_Declared, called below. There
687 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
689 -- This consideration also applies to similar checks for allocators,
690 -- qualified expressions, and type conversions.
692 -- An additional exception concerns other per-object expressions that
693 -- are not directly related to component declarations, in particular
694 -- representation pragmas for tasks. These will be per-object
695 -- expressions if they depend on discriminants or some global entity.
696 -- If the task has access discriminants, the designated type may be
697 -- incomplete at the point the expression is resolved. This resolution
698 -- takes place within the body of the initialization procedure, where
699 -- the discriminant is replaced by its discriminal.
701 if Is_Entity_Name (Pref)
702 and then Ekind (Entity (Pref)) = E_In_Parameter
706 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
707 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
708 -- Analyze_Object_Renaming, and Freeze_Entity.
710 elsif Ada_Version >= Ada_2005
711 and then Is_Entity_Name (Pref)
712 and then Is_Access_Type (Etype (Pref))
713 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
715 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
719 Check_Fully_Declared (Typ, Parent (Pref));
721 end Check_Fully_Declared_Prefix;
723 ------------------------------
724 -- Check_Infinite_Recursion --
725 ------------------------------
727 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
731 function Same_Argument_List return Boolean;
732 -- Check whether list of actuals is identical to list of formals of
733 -- called function (which is also the enclosing scope).
735 ------------------------
736 -- Same_Argument_List --
737 ------------------------
739 function Same_Argument_List return Boolean is
745 if not Is_Entity_Name (Name (N)) then
748 Subp := Entity (Name (N));
751 F := First_Formal (Subp);
752 A := First_Actual (N);
753 while Present (F) and then Present (A) loop
754 if not Is_Entity_Name (A)
755 or else Entity (A) /= F
765 end Same_Argument_List;
767 -- Start of processing for Check_Infinite_Recursion
770 -- Special case, if this is a procedure call and is a call to the
771 -- current procedure with the same argument list, then this is for
772 -- sure an infinite recursion and we insert a call to raise SE.
774 if Is_List_Member (N)
775 and then List_Length (List_Containing (N)) = 1
776 and then Same_Argument_List
779 P : constant Node_Id := Parent (N);
781 if Nkind (P) = N_Handled_Sequence_Of_Statements
782 and then Nkind (Parent (P)) = N_Subprogram_Body
783 and then Is_Empty_List (Declarations (Parent (P)))
785 Error_Msg_N ("!?infinite recursion", N);
786 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
788 Make_Raise_Storage_Error (Sloc (N),
789 Reason => SE_Infinite_Recursion));
795 -- If not that special case, search up tree, quitting if we reach a
796 -- construct (e.g. a conditional) that tells us that this is not a
797 -- case for an infinite recursion warning.
803 -- If no parent, then we were not inside a subprogram, this can for
804 -- example happen when processing certain pragmas in a spec. Just
805 -- return False in this case.
811 -- Done if we get to subprogram body, this is definitely an infinite
812 -- recursion case if we did not find anything to stop us.
814 exit when Nkind (P) = N_Subprogram_Body;
816 -- If appearing in conditional, result is false
818 if Nkind_In (P, N_Or_Else,
822 N_Conditional_Expression,
827 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
828 and then C /= First (Statements (P))
830 -- If the call is the expression of a return statement and the
831 -- actuals are identical to the formals, it's worth a warning.
832 -- However, we skip this if there is an immediately preceding
833 -- raise statement, since the call is never executed.
835 -- Furthermore, this corresponds to a common idiom:
837 -- function F (L : Thing) return Boolean is
839 -- raise Program_Error;
843 -- for generating a stub function
845 if Nkind (Parent (N)) = N_Simple_Return_Statement
846 and then Same_Argument_List
848 exit when not Is_List_Member (Parent (N));
850 -- OK, return statement is in a statement list, look for raise
856 -- Skip past N_Freeze_Entity nodes generated by expansion
858 Nod := Prev (Parent (N));
860 and then Nkind (Nod) = N_Freeze_Entity
865 -- If no raise statement, give warning
867 exit when Nkind (Nod) /= N_Raise_Statement
869 (Nkind (Nod) not in N_Raise_xxx_Error
870 or else Present (Condition (Nod)));
881 Error_Msg_N ("!?possible infinite recursion", N);
882 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
885 end Check_Infinite_Recursion;
887 -------------------------------
888 -- Check_Initialization_Call --
889 -------------------------------
891 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
892 Typ : constant Entity_Id := Etype (First_Formal (Nam));
894 function Uses_SS (T : Entity_Id) return Boolean;
895 -- Check whether the creation of an object of the type will involve
896 -- use of the secondary stack. If T is a record type, this is true
897 -- if the expression for some component uses the secondary stack, e.g.
898 -- through a call to a function that returns an unconstrained value.
899 -- False if T is controlled, because cleanups occur elsewhere.
905 function Uses_SS (T : Entity_Id) return Boolean is
908 Full_Type : Entity_Id := Underlying_Type (T);
911 -- Normally we want to use the underlying type, but if it's not set
912 -- then continue with T.
914 if not Present (Full_Type) then
918 if Is_Controlled (Full_Type) then
921 elsif Is_Array_Type (Full_Type) then
922 return Uses_SS (Component_Type (Full_Type));
924 elsif Is_Record_Type (Full_Type) then
925 Comp := First_Component (Full_Type);
926 while Present (Comp) loop
927 if Ekind (Comp) = E_Component
928 and then Nkind (Parent (Comp)) = N_Component_Declaration
930 -- The expression for a dynamic component may be rewritten
931 -- as a dereference, so retrieve original node.
933 Expr := Original_Node (Expression (Parent (Comp)));
935 -- Return True if the expression is a call to a function
936 -- (including an attribute function such as Image, or a
937 -- user-defined operator) with a result that requires a
940 if (Nkind (Expr) = N_Function_Call
941 or else Nkind (Expr) in N_Op
942 or else (Nkind (Expr) = N_Attribute_Reference
943 and then Present (Expressions (Expr))))
944 and then Requires_Transient_Scope (Etype (Expr))
948 elsif Uses_SS (Etype (Comp)) then
953 Next_Component (Comp);
963 -- Start of processing for Check_Initialization_Call
966 -- Establish a transient scope if the type needs it
968 if Uses_SS (Typ) then
969 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
971 end Check_Initialization_Call;
973 ---------------------------------------
974 -- Check_No_Direct_Boolean_Operators --
975 ---------------------------------------
977 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
979 if Scope (Entity (N)) = Standard_Standard
980 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
982 -- Restriction only applies to original source code
984 if Comes_From_Source (N) then
985 Check_Restriction (No_Direct_Boolean_Operators, N);
990 Check_Boolean_Operator (N);
992 end Check_No_Direct_Boolean_Operators;
994 ------------------------------
995 -- Check_Parameterless_Call --
996 ------------------------------
998 procedure Check_Parameterless_Call (N : Node_Id) is
1001 function Prefix_Is_Access_Subp return Boolean;
1002 -- If the prefix is of an access_to_subprogram type, the node must be
1003 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1004 -- interpretations are access to subprograms.
1006 ---------------------------
1007 -- Prefix_Is_Access_Subp --
1008 ---------------------------
1010 function Prefix_Is_Access_Subp return Boolean is
1015 -- If the context is an attribute reference that can apply to
1016 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1018 if Nkind (Parent (N)) = N_Attribute_Reference
1019 and then (Attribute_Name (Parent (N)) = Name_Address or else
1020 Attribute_Name (Parent (N)) = Name_Code_Address or else
1021 Attribute_Name (Parent (N)) = Name_Access)
1026 if not Is_Overloaded (N) then
1028 Ekind (Etype (N)) = E_Subprogram_Type
1029 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1031 Get_First_Interp (N, I, It);
1032 while Present (It.Typ) loop
1033 if Ekind (It.Typ) /= E_Subprogram_Type
1034 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1039 Get_Next_Interp (I, It);
1044 end Prefix_Is_Access_Subp;
1046 -- Start of processing for Check_Parameterless_Call
1049 -- Defend against junk stuff if errors already detected
1051 if Total_Errors_Detected /= 0 then
1052 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1054 elsif Nkind (N) in N_Has_Chars
1055 and then Chars (N) in Error_Name_Or_No_Name
1063 -- If the context expects a value, and the name is a procedure, this is
1064 -- most likely a missing 'Access. Don't try to resolve the parameterless
1065 -- call, error will be caught when the outer call is analyzed.
1067 if Is_Entity_Name (N)
1068 and then Ekind (Entity (N)) = E_Procedure
1069 and then not Is_Overloaded (N)
1071 Nkind_In (Parent (N), N_Parameter_Association,
1073 N_Procedure_Call_Statement)
1078 -- Rewrite as call if overloadable entity that is (or could be, in the
1079 -- overloaded case) a function call. If we know for sure that the entity
1080 -- is an enumeration literal, we do not rewrite it.
1082 -- If the entity is the name of an operator, it cannot be a call because
1083 -- operators cannot have default parameters. In this case, this must be
1084 -- a string whose contents coincide with an operator name. Set the kind
1085 -- of the node appropriately.
1087 if (Is_Entity_Name (N)
1088 and then Nkind (N) /= N_Operator_Symbol
1089 and then Is_Overloadable (Entity (N))
1090 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1091 or else Is_Overloaded (N)))
1093 -- Rewrite as call if it is an explicit dereference of an expression of
1094 -- a subprogram access type, and the subprogram type is not that of a
1095 -- procedure or entry.
1098 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1100 -- Rewrite as call if it is a selected component which is a function,
1101 -- this is the case of a call to a protected function (which may be
1102 -- overloaded with other protected operations).
1105 (Nkind (N) = N_Selected_Component
1106 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1108 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1110 and then Is_Overloaded (Selector_Name (N)))))
1112 -- If one of the above three conditions is met, rewrite as call. Apply
1113 -- the rewriting only once.
1116 if Nkind (Parent (N)) /= N_Function_Call
1117 or else N /= Name (Parent (N))
1119 Nam := New_Copy (N);
1121 -- If overloaded, overload set belongs to new copy
1123 Save_Interps (N, Nam);
1125 -- Change node to parameterless function call (note that the
1126 -- Parameter_Associations associations field is left set to Empty,
1127 -- its normal default value since there are no parameters)
1129 Change_Node (N, N_Function_Call);
1131 Set_Sloc (N, Sloc (Nam));
1135 elsif Nkind (N) = N_Parameter_Association then
1136 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1138 elsif Nkind (N) = N_Operator_Symbol then
1139 Change_Operator_Symbol_To_String_Literal (N);
1140 Set_Is_Overloaded (N, False);
1141 Set_Etype (N, Any_String);
1143 end Check_Parameterless_Call;
1145 -----------------------------
1146 -- Is_Definite_Access_Type --
1147 -----------------------------
1149 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1150 Btyp : constant Entity_Id := Base_Type (E);
1152 return Ekind (Btyp) = E_Access_Type
1153 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1154 and then Comes_From_Source (Btyp));
1155 end Is_Definite_Access_Type;
1157 ----------------------
1158 -- Is_Predefined_Op --
1159 ----------------------
1161 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1163 -- Predefined operators are intrinsic subprograms
1165 if not Is_Intrinsic_Subprogram (Nam) then
1169 -- A call to a back-end builtin is never a predefined operator
1171 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1175 return not Is_Generic_Instance (Nam)
1176 and then Chars (Nam) in Any_Operator_Name
1177 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1178 end Is_Predefined_Op;
1180 -----------------------------
1181 -- Make_Call_Into_Operator --
1182 -----------------------------
1184 procedure Make_Call_Into_Operator
1189 Op_Name : constant Name_Id := Chars (Op_Id);
1190 Act1 : Node_Id := First_Actual (N);
1191 Act2 : Node_Id := Next_Actual (Act1);
1192 Error : Boolean := False;
1193 Func : constant Entity_Id := Entity (Name (N));
1194 Is_Binary : constant Boolean := Present (Act2);
1196 Opnd_Type : Entity_Id;
1197 Orig_Type : Entity_Id := Empty;
1200 type Kind_Test is access function (E : Entity_Id) return Boolean;
1202 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1203 -- If the operand is not universal, and the operator is given by an
1204 -- expanded name, verify that the operand has an interpretation with a
1205 -- type defined in the given scope of the operator.
1207 function Type_In_P (Test : Kind_Test) return Entity_Id;
1208 -- Find a type of the given class in package Pack that contains the
1211 ---------------------------
1212 -- Operand_Type_In_Scope --
1213 ---------------------------
1215 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1216 Nod : constant Node_Id := Right_Opnd (Op_Node);
1221 if not Is_Overloaded (Nod) then
1222 return Scope (Base_Type (Etype (Nod))) = S;
1225 Get_First_Interp (Nod, I, It);
1226 while Present (It.Typ) loop
1227 if Scope (Base_Type (It.Typ)) = S then
1231 Get_Next_Interp (I, It);
1236 end Operand_Type_In_Scope;
1242 function Type_In_P (Test : Kind_Test) return Entity_Id is
1245 function In_Decl return Boolean;
1246 -- Verify that node is not part of the type declaration for the
1247 -- candidate type, which would otherwise be invisible.
1253 function In_Decl return Boolean is
1254 Decl_Node : constant Node_Id := Parent (E);
1260 if Etype (E) = Any_Type then
1263 elsif No (Decl_Node) then
1268 and then Nkind (N2) /= N_Compilation_Unit
1270 if N2 = Decl_Node then
1281 -- Start of processing for Type_In_P
1284 -- If the context type is declared in the prefix package, this is the
1285 -- desired base type.
1287 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1288 return Base_Type (Typ);
1291 E := First_Entity (Pack);
1292 while Present (E) loop
1294 and then not In_Decl
1306 -- Start of processing for Make_Call_Into_Operator
1309 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1314 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1315 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1316 Save_Interps (Act1, Left_Opnd (Op_Node));
1317 Save_Interps (Act2, Right_Opnd (Op_Node));
1318 Act1 := Left_Opnd (Op_Node);
1319 Act2 := Right_Opnd (Op_Node);
1324 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1325 Save_Interps (Act1, Right_Opnd (Op_Node));
1326 Act1 := Right_Opnd (Op_Node);
1329 -- If the operator is denoted by an expanded name, and the prefix is
1330 -- not Standard, but the operator is a predefined one whose scope is
1331 -- Standard, then this is an implicit_operator, inserted as an
1332 -- interpretation by the procedure of the same name. This procedure
1333 -- overestimates the presence of implicit operators, because it does
1334 -- not examine the type of the operands. Verify now that the operand
1335 -- type appears in the given scope. If right operand is universal,
1336 -- check the other operand. In the case of concatenation, either
1337 -- argument can be the component type, so check the type of the result.
1338 -- If both arguments are literals, look for a type of the right kind
1339 -- defined in the given scope. This elaborate nonsense is brought to
1340 -- you courtesy of b33302a. The type itself must be frozen, so we must
1341 -- find the type of the proper class in the given scope.
1343 -- A final wrinkle is the multiplication operator for fixed point types,
1344 -- which is defined in Standard only, and not in the scope of the
1345 -- fixed point type itself.
1347 if Nkind (Name (N)) = N_Expanded_Name then
1348 Pack := Entity (Prefix (Name (N)));
1350 -- If the entity being called is defined in the given package, it is
1351 -- a renaming of a predefined operator, and known to be legal.
1353 if Scope (Entity (Name (N))) = Pack
1354 and then Pack /= Standard_Standard
1358 -- Visibility does not need to be checked in an instance: if the
1359 -- operator was not visible in the generic it has been diagnosed
1360 -- already, else there is an implicit copy of it in the instance.
1362 elsif In_Instance then
1365 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1366 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1367 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1369 if Pack /= Standard_Standard then
1373 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1376 elsif Ada_Version >= Ada_2005
1377 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1378 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1383 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1385 if Op_Name = Name_Op_Concat then
1386 Opnd_Type := Base_Type (Typ);
1388 elsif (Scope (Opnd_Type) = Standard_Standard
1390 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1392 and then not Comes_From_Source (Opnd_Type))
1394 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1397 if Scope (Opnd_Type) = Standard_Standard then
1399 -- Verify that the scope contains a type that corresponds to
1400 -- the given literal. Optimize the case where Pack is Standard.
1402 if Pack /= Standard_Standard then
1404 if Opnd_Type = Universal_Integer then
1405 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1407 elsif Opnd_Type = Universal_Real then
1408 Orig_Type := Type_In_P (Is_Real_Type'Access);
1410 elsif Opnd_Type = Any_String then
1411 Orig_Type := Type_In_P (Is_String_Type'Access);
1413 elsif Opnd_Type = Any_Access then
1414 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1416 elsif Opnd_Type = Any_Composite then
1417 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1419 if Present (Orig_Type) then
1420 if Has_Private_Component (Orig_Type) then
1423 Set_Etype (Act1, Orig_Type);
1426 Set_Etype (Act2, Orig_Type);
1435 Error := No (Orig_Type);
1438 elsif Ekind (Opnd_Type) = E_Allocator_Type
1439 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1443 -- If the type is defined elsewhere, and the operator is not
1444 -- defined in the given scope (by a renaming declaration, e.g.)
1445 -- then this is an error as well. If an extension of System is
1446 -- present, and the type may be defined there, Pack must be
1449 elsif Scope (Opnd_Type) /= Pack
1450 and then Scope (Op_Id) /= Pack
1451 and then (No (System_Aux_Id)
1452 or else Scope (Opnd_Type) /= System_Aux_Id
1453 or else Pack /= Scope (System_Aux_Id))
1455 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1458 Error := not Operand_Type_In_Scope (Pack);
1461 elsif Pack = Standard_Standard
1462 and then not Operand_Type_In_Scope (Standard_Standard)
1469 Error_Msg_Node_2 := Pack;
1471 ("& not declared in&", N, Selector_Name (Name (N)));
1472 Set_Etype (N, Any_Type);
1475 -- Detect a mismatch between the context type and the result type
1476 -- in the named package, which is otherwise not detected if the
1477 -- operands are universal. Check is only needed if source entity is
1478 -- an operator, not a function that renames an operator.
1480 elsif Nkind (Parent (N)) /= N_Type_Conversion
1481 and then Ekind (Entity (Name (N))) = E_Operator
1482 and then Is_Numeric_Type (Typ)
1483 and then not Is_Universal_Numeric_Type (Typ)
1484 and then Scope (Base_Type (Typ)) /= Pack
1485 and then not In_Instance
1487 if Is_Fixed_Point_Type (Typ)
1488 and then (Op_Name = Name_Op_Multiply
1490 Op_Name = Name_Op_Divide)
1492 -- Already checked above
1496 -- Operator may be defined in an extension of System
1498 elsif Present (System_Aux_Id)
1499 and then Scope (Opnd_Type) = System_Aux_Id
1504 -- Could we use Wrong_Type here??? (this would require setting
1505 -- Etype (N) to the actual type found where Typ was expected).
1507 Error_Msg_NE ("expect }", N, Typ);
1512 Set_Chars (Op_Node, Op_Name);
1514 if not Is_Private_Type (Etype (N)) then
1515 Set_Etype (Op_Node, Base_Type (Etype (N)));
1517 Set_Etype (Op_Node, Etype (N));
1520 -- If this is a call to a function that renames a predefined equality,
1521 -- the renaming declaration provides a type that must be used to
1522 -- resolve the operands. This must be done now because resolution of
1523 -- the equality node will not resolve any remaining ambiguity, and it
1524 -- assumes that the first operand is not overloaded.
1526 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1527 and then Ekind (Func) = E_Function
1528 and then Is_Overloaded (Act1)
1530 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1531 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1534 Set_Entity (Op_Node, Op_Id);
1535 Generate_Reference (Op_Id, N, ' ');
1537 -- Do rewrite setting Comes_From_Source on the result if the original
1538 -- call came from source. Although it is not strictly the case that the
1539 -- operator as such comes from the source, logically it corresponds
1540 -- exactly to the function call in the source, so it should be marked
1541 -- this way (e.g. to make sure that validity checks work fine).
1544 CS : constant Boolean := Comes_From_Source (N);
1546 Rewrite (N, Op_Node);
1547 Set_Comes_From_Source (N, CS);
1550 -- If this is an arithmetic operator and the result type is private,
1551 -- the operands and the result must be wrapped in conversion to
1552 -- expose the underlying numeric type and expand the proper checks,
1553 -- e.g. on division.
1555 if Is_Private_Type (Typ) then
1557 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1558 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1559 Resolve_Intrinsic_Operator (N, Typ);
1561 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1562 Resolve_Intrinsic_Unary_Operator (N, Typ);
1570 end Make_Call_Into_Operator;
1576 function Operator_Kind
1578 Is_Binary : Boolean) return Node_Kind
1583 -- Use CASE statement or array???
1586 if Op_Name = Name_Op_And then
1588 elsif Op_Name = Name_Op_Or then
1590 elsif Op_Name = Name_Op_Xor then
1592 elsif Op_Name = Name_Op_Eq then
1594 elsif Op_Name = Name_Op_Ne then
1596 elsif Op_Name = Name_Op_Lt then
1598 elsif Op_Name = Name_Op_Le then
1600 elsif Op_Name = Name_Op_Gt then
1602 elsif Op_Name = Name_Op_Ge then
1604 elsif Op_Name = Name_Op_Add then
1606 elsif Op_Name = Name_Op_Subtract then
1607 Kind := N_Op_Subtract;
1608 elsif Op_Name = Name_Op_Concat then
1609 Kind := N_Op_Concat;
1610 elsif Op_Name = Name_Op_Multiply then
1611 Kind := N_Op_Multiply;
1612 elsif Op_Name = Name_Op_Divide then
1613 Kind := N_Op_Divide;
1614 elsif Op_Name = Name_Op_Mod then
1616 elsif Op_Name = Name_Op_Rem then
1618 elsif Op_Name = Name_Op_Expon then
1621 raise Program_Error;
1627 if Op_Name = Name_Op_Add then
1629 elsif Op_Name = Name_Op_Subtract then
1631 elsif Op_Name = Name_Op_Abs then
1633 elsif Op_Name = Name_Op_Not then
1636 raise Program_Error;
1643 ----------------------------
1644 -- Preanalyze_And_Resolve --
1645 ----------------------------
1647 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1648 Save_Full_Analysis : constant Boolean := Full_Analysis;
1651 Full_Analysis := False;
1652 Expander_Mode_Save_And_Set (False);
1654 -- We suppress all checks for this analysis, since the checks will
1655 -- be applied properly, and in the right location, when the default
1656 -- expression is reanalyzed and reexpanded later on.
1658 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1660 Expander_Mode_Restore;
1661 Full_Analysis := Save_Full_Analysis;
1662 end Preanalyze_And_Resolve;
1664 -- Version without context type
1666 procedure Preanalyze_And_Resolve (N : Node_Id) is
1667 Save_Full_Analysis : constant Boolean := Full_Analysis;
1670 Full_Analysis := False;
1671 Expander_Mode_Save_And_Set (False);
1674 Resolve (N, Etype (N), Suppress => All_Checks);
1676 Expander_Mode_Restore;
1677 Full_Analysis := Save_Full_Analysis;
1678 end Preanalyze_And_Resolve;
1680 ----------------------------------
1681 -- Replace_Actual_Discriminants --
1682 ----------------------------------
1684 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1685 Loc : constant Source_Ptr := Sloc (N);
1686 Tsk : Node_Id := Empty;
1688 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1694 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1698 if Nkind (Nod) = N_Identifier then
1699 Ent := Entity (Nod);
1702 and then Ekind (Ent) = E_Discriminant
1705 Make_Selected_Component (Loc,
1706 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1707 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1709 Set_Etype (Nod, Etype (Ent));
1717 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1719 -- Start of processing for Replace_Actual_Discriminants
1722 if not Expander_Active then
1726 if Nkind (Name (N)) = N_Selected_Component then
1727 Tsk := Prefix (Name (N));
1729 elsif Nkind (Name (N)) = N_Indexed_Component then
1730 Tsk := Prefix (Prefix (Name (N)));
1736 Replace_Discrs (Default);
1738 end Replace_Actual_Discriminants;
1744 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1745 Ambiguous : Boolean := False;
1746 Ctx_Type : Entity_Id := Typ;
1747 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1748 Err_Type : Entity_Id := Empty;
1749 Found : Boolean := False;
1752 I1 : Interp_Index := 0; -- prevent junk warning
1755 Seen : Entity_Id := Empty; -- prevent junk warning
1757 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1758 -- Determine whether a node comes from a predefined library unit or
1761 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1762 -- Try and fix up a literal so that it matches its expected type. New
1763 -- literals are manufactured if necessary to avoid cascaded errors.
1765 procedure Report_Ambiguous_Argument;
1766 -- Additional diagnostics when an ambiguous call has an ambiguous
1767 -- argument (typically a controlling actual).
1769 procedure Resolution_Failed;
1770 -- Called when attempt at resolving current expression fails
1772 ------------------------------------
1773 -- Comes_From_Predefined_Lib_Unit --
1774 -------------------------------------
1776 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1779 Sloc (Nod) = Standard_Location
1780 or else Is_Predefined_File_Name
1781 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1782 end Comes_From_Predefined_Lib_Unit;
1784 --------------------
1785 -- Patch_Up_Value --
1786 --------------------
1788 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1790 if Nkind (N) = N_Integer_Literal
1791 and then Is_Real_Type (Typ)
1794 Make_Real_Literal (Sloc (N),
1795 Realval => UR_From_Uint (Intval (N))));
1796 Set_Etype (N, Universal_Real);
1797 Set_Is_Static_Expression (N);
1799 elsif Nkind (N) = N_Real_Literal
1800 and then Is_Integer_Type (Typ)
1803 Make_Integer_Literal (Sloc (N),
1804 Intval => UR_To_Uint (Realval (N))));
1805 Set_Etype (N, Universal_Integer);
1806 Set_Is_Static_Expression (N);
1808 elsif Nkind (N) = N_String_Literal
1809 and then Is_Character_Type (Typ)
1811 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1813 Make_Character_Literal (Sloc (N),
1815 Char_Literal_Value =>
1816 UI_From_Int (Character'Pos ('A'))));
1817 Set_Etype (N, Any_Character);
1818 Set_Is_Static_Expression (N);
1820 elsif Nkind (N) /= N_String_Literal
1821 and then Is_String_Type (Typ)
1824 Make_String_Literal (Sloc (N),
1825 Strval => End_String));
1827 elsif Nkind (N) = N_Range then
1828 Patch_Up_Value (Low_Bound (N), Typ);
1829 Patch_Up_Value (High_Bound (N), Typ);
1833 -------------------------------
1834 -- Report_Ambiguous_Argument --
1835 -------------------------------
1837 procedure Report_Ambiguous_Argument is
1838 Arg : constant Node_Id := First (Parameter_Associations (N));
1843 if Nkind (Arg) = N_Function_Call
1844 and then Is_Entity_Name (Name (Arg))
1845 and then Is_Overloaded (Name (Arg))
1847 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1849 -- Could use comments on what is going on here ???
1851 Get_First_Interp (Name (Arg), I, It);
1852 while Present (It.Nam) loop
1853 Error_Msg_Sloc := Sloc (It.Nam);
1855 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1856 Error_Msg_N ("interpretation (inherited) #!", Arg);
1858 Error_Msg_N ("interpretation #!", Arg);
1861 Get_Next_Interp (I, It);
1864 end Report_Ambiguous_Argument;
1866 -----------------------
1867 -- Resolution_Failed --
1868 -----------------------
1870 procedure Resolution_Failed is
1872 Patch_Up_Value (N, Typ);
1874 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1875 Set_Is_Overloaded (N, False);
1877 -- The caller will return without calling the expander, so we need
1878 -- to set the analyzed flag. Note that it is fine to set Analyzed
1879 -- to True even if we are in the middle of a shallow analysis,
1880 -- (see the spec of sem for more details) since this is an error
1881 -- situation anyway, and there is no point in repeating the
1882 -- analysis later (indeed it won't work to repeat it later, since
1883 -- we haven't got a clear resolution of which entity is being
1886 Set_Analyzed (N, True);
1888 end Resolution_Failed;
1890 -- Start of processing for Resolve
1897 -- Access attribute on remote subprogram cannot be used for
1898 -- a non-remote access-to-subprogram type.
1900 if Nkind (N) = N_Attribute_Reference
1901 and then (Attribute_Name (N) = Name_Access or else
1902 Attribute_Name (N) = Name_Unrestricted_Access or else
1903 Attribute_Name (N) = Name_Unchecked_Access)
1904 and then Comes_From_Source (N)
1905 and then Is_Entity_Name (Prefix (N))
1906 and then Is_Subprogram (Entity (Prefix (N)))
1907 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1908 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1911 ("prefix must statically denote a non-remote subprogram", N);
1914 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1916 -- If the context is a Remote_Access_To_Subprogram, access attributes
1917 -- must be resolved with the corresponding fat pointer. There is no need
1918 -- to check for the attribute name since the return type of an
1919 -- attribute is never a remote type.
1921 if Nkind (N) = N_Attribute_Reference
1922 and then Comes_From_Source (N)
1923 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1926 Attr : constant Attribute_Id :=
1927 Get_Attribute_Id (Attribute_Name (N));
1928 Pref : constant Node_Id := Prefix (N);
1931 Is_Remote : Boolean := True;
1934 -- Check that Typ is a remote access-to-subprogram type
1936 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1938 -- Prefix (N) must statically denote a remote subprogram
1939 -- declared in a package specification.
1941 if Attr = Attribute_Access then
1942 Decl := Unit_Declaration_Node (Entity (Pref));
1944 if Nkind (Decl) = N_Subprogram_Body then
1945 Spec := Corresponding_Spec (Decl);
1947 if not No (Spec) then
1948 Decl := Unit_Declaration_Node (Spec);
1952 Spec := Parent (Decl);
1954 if not Is_Entity_Name (Prefix (N))
1955 or else Nkind (Spec) /= N_Package_Specification
1957 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1961 ("prefix must statically denote a remote subprogram ",
1966 -- If we are generating code for a distributed program.
1967 -- perform semantic checks against the corresponding
1970 if (Attr = Attribute_Access or else
1971 Attr = Attribute_Unchecked_Access or else
1972 Attr = Attribute_Unrestricted_Access)
1973 and then Expander_Active
1974 and then Get_PCS_Name /= Name_No_DSA
1976 Check_Subtype_Conformant
1977 (New_Id => Entity (Prefix (N)),
1978 Old_Id => Designated_Type
1979 (Corresponding_Remote_Type (Typ)),
1983 Process_Remote_AST_Attribute (N, Typ);
1990 Debug_A_Entry ("resolving ", N);
1992 if Comes_From_Source (N) then
1993 if Is_Fixed_Point_Type (Typ) then
1994 Check_Restriction (No_Fixed_Point, N);
1996 elsif Is_Floating_Point_Type (Typ)
1997 and then Typ /= Universal_Real
1998 and then Typ /= Any_Real
2000 Check_Restriction (No_Floating_Point, N);
2004 -- Return if already analyzed
2006 if Analyzed (N) then
2007 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2010 -- Return if type = Any_Type (previous error encountered)
2012 elsif Etype (N) = Any_Type then
2013 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2017 Check_Parameterless_Call (N);
2019 -- If not overloaded, then we know the type, and all that needs doing
2020 -- is to check that this type is compatible with the context.
2022 if not Is_Overloaded (N) then
2023 Found := Covers (Typ, Etype (N));
2024 Expr_Type := Etype (N);
2026 -- In the overloaded case, we must select the interpretation that
2027 -- is compatible with the context (i.e. the type passed to Resolve)
2030 -- Loop through possible interpretations
2032 Get_First_Interp (N, I, It);
2033 Interp_Loop : while Present (It.Typ) loop
2035 -- We are only interested in interpretations that are compatible
2036 -- with the expected type, any other interpretations are ignored.
2038 if not Covers (Typ, It.Typ) then
2039 if Debug_Flag_V then
2040 Write_Str (" interpretation incompatible with context");
2045 -- Skip the current interpretation if it is disabled by an
2046 -- abstract operator. This action is performed only when the
2047 -- type against which we are resolving is the same as the
2048 -- type of the interpretation.
2050 if Ada_Version >= Ada_2005
2051 and then It.Typ = Typ
2052 and then Typ /= Universal_Integer
2053 and then Typ /= Universal_Real
2054 and then Present (It.Abstract_Op)
2059 -- First matching interpretation
2065 Expr_Type := It.Typ;
2067 -- Matching interpretation that is not the first, maybe an
2068 -- error, but there are some cases where preference rules are
2069 -- used to choose between the two possibilities. These and
2070 -- some more obscure cases are handled in Disambiguate.
2073 -- If the current statement is part of a predefined library
2074 -- unit, then all interpretations which come from user level
2075 -- packages should not be considered.
2078 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2083 Error_Msg_Sloc := Sloc (Seen);
2084 It1 := Disambiguate (N, I1, I, Typ);
2086 -- Disambiguation has succeeded. Skip the remaining
2089 if It1 /= No_Interp then
2091 Expr_Type := It1.Typ;
2093 while Present (It.Typ) loop
2094 Get_Next_Interp (I, It);
2098 -- Before we issue an ambiguity complaint, check for
2099 -- the case of a subprogram call where at least one
2100 -- of the arguments is Any_Type, and if so, suppress
2101 -- the message, since it is a cascaded error.
2103 if Nkind_In (N, N_Function_Call,
2104 N_Procedure_Call_Statement)
2111 A := First_Actual (N);
2112 while Present (A) loop
2115 if Nkind (E) = N_Parameter_Association then
2116 E := Explicit_Actual_Parameter (E);
2119 if Etype (E) = Any_Type then
2120 if Debug_Flag_V then
2121 Write_Str ("Any_Type in call");
2132 elsif Nkind (N) in N_Binary_Op
2133 and then (Etype (Left_Opnd (N)) = Any_Type
2134 or else Etype (Right_Opnd (N)) = Any_Type)
2138 elsif Nkind (N) in N_Unary_Op
2139 and then Etype (Right_Opnd (N)) = Any_Type
2144 -- Not that special case, so issue message using the
2145 -- flag Ambiguous to control printing of the header
2146 -- message only at the start of an ambiguous set.
2148 if not Ambiguous then
2149 if Nkind (N) = N_Function_Call
2150 and then Nkind (Name (N)) = N_Explicit_Dereference
2153 ("ambiguous expression "
2154 & "(cannot resolve indirect call)!", N);
2156 Error_Msg_NE -- CODEFIX
2157 ("ambiguous expression (cannot resolve&)!",
2163 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2165 ("\\possible interpretation (inherited)#!", N);
2167 Error_Msg_N -- CODEFIX
2168 ("\\possible interpretation#!", N);
2172 (N, N_Procedure_Call_Statement, N_Function_Call)
2173 and then Present (Parameter_Associations (N))
2175 Report_Ambiguous_Argument;
2179 Error_Msg_Sloc := Sloc (It.Nam);
2181 -- By default, the error message refers to the candidate
2182 -- interpretation. But if it is a predefined operator, it
2183 -- is implicitly declared at the declaration of the type
2184 -- of the operand. Recover the sloc of that declaration
2185 -- for the error message.
2187 if Nkind (N) in N_Op
2188 and then Scope (It.Nam) = Standard_Standard
2189 and then not Is_Overloaded (Right_Opnd (N))
2190 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2193 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2195 if Comes_From_Source (Err_Type)
2196 and then Present (Parent (Err_Type))
2198 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2201 elsif Nkind (N) in N_Binary_Op
2202 and then Scope (It.Nam) = Standard_Standard
2203 and then not Is_Overloaded (Left_Opnd (N))
2204 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2207 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2209 if Comes_From_Source (Err_Type)
2210 and then Present (Parent (Err_Type))
2212 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2215 -- If this is an indirect call, use the subprogram_type
2216 -- in the message, to have a meaningful location. Also
2217 -- indicate if this is an inherited operation, created
2218 -- by a type declaration.
2220 elsif Nkind (N) = N_Function_Call
2221 and then Nkind (Name (N)) = N_Explicit_Dereference
2222 and then Is_Type (It.Nam)
2226 Sloc (Associated_Node_For_Itype (Err_Type));
2231 if Nkind (N) in N_Op
2232 and then Scope (It.Nam) = Standard_Standard
2233 and then Present (Err_Type)
2235 -- Special-case the message for universal_fixed
2236 -- operators, which are not declared with the type
2237 -- of the operand, but appear forever in Standard.
2239 if It.Typ = Universal_Fixed
2240 and then Scope (It.Nam) = Standard_Standard
2243 ("\\possible interpretation as " &
2244 "universal_fixed operation " &
2245 "(RM 4.5.5 (19))", N);
2248 ("\\possible interpretation (predefined)#!", N);
2252 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2255 ("\\possible interpretation (inherited)#!", N);
2257 Error_Msg_N -- CODEFIX
2258 ("\\possible interpretation#!", N);
2264 -- We have a matching interpretation, Expr_Type is the type
2265 -- from this interpretation, and Seen is the entity.
2267 -- For an operator, just set the entity name. The type will be
2268 -- set by the specific operator resolution routine.
2270 if Nkind (N) in N_Op then
2271 Set_Entity (N, Seen);
2272 Generate_Reference (Seen, N);
2274 elsif Nkind (N) = N_Case_Expression then
2275 Set_Etype (N, Expr_Type);
2277 elsif Nkind (N) = N_Character_Literal then
2278 Set_Etype (N, Expr_Type);
2280 elsif Nkind (N) = N_Conditional_Expression then
2281 Set_Etype (N, Expr_Type);
2283 -- For an explicit dereference, attribute reference, range,
2284 -- short-circuit form (which is not an operator node), or call
2285 -- with a name that is an explicit dereference, there is
2286 -- nothing to be done at this point.
2288 elsif Nkind_In (N, N_Explicit_Dereference,
2289 N_Attribute_Reference,
2291 N_Indexed_Component,
2294 N_Selected_Component,
2296 or else Nkind (Name (N)) = N_Explicit_Dereference
2300 -- For procedure or function calls, set the type of the name,
2301 -- and also the entity pointer for the prefix.
2303 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2304 and then Is_Entity_Name (Name (N))
2306 Set_Etype (Name (N), Expr_Type);
2307 Set_Entity (Name (N), Seen);
2308 Generate_Reference (Seen, Name (N));
2310 elsif Nkind (N) = N_Function_Call
2311 and then Nkind (Name (N)) = N_Selected_Component
2313 Set_Etype (Name (N), Expr_Type);
2314 Set_Entity (Selector_Name (Name (N)), Seen);
2315 Generate_Reference (Seen, Selector_Name (Name (N)));
2317 -- For all other cases, just set the type of the Name
2320 Set_Etype (Name (N), Expr_Type);
2327 -- Move to next interpretation
2329 exit Interp_Loop when No (It.Typ);
2331 Get_Next_Interp (I, It);
2332 end loop Interp_Loop;
2335 -- At this stage Found indicates whether or not an acceptable
2336 -- interpretation exists. If not, then we have an error, except that if
2337 -- the context is Any_Type as a result of some other error, then we
2338 -- suppress the error report.
2341 if Typ /= Any_Type then
2343 -- If type we are looking for is Void, then this is the procedure
2344 -- call case, and the error is simply that what we gave is not a
2345 -- procedure name (we think of procedure calls as expressions with
2346 -- types internally, but the user doesn't think of them this way!)
2348 if Typ = Standard_Void_Type then
2350 -- Special case message if function used as a procedure
2352 if Nkind (N) = N_Procedure_Call_Statement
2353 and then Is_Entity_Name (Name (N))
2354 and then Ekind (Entity (Name (N))) = E_Function
2357 ("cannot use function & in a procedure call",
2358 Name (N), Entity (Name (N)));
2360 -- Otherwise give general message (not clear what cases this
2361 -- covers, but no harm in providing for them!)
2364 Error_Msg_N ("expect procedure name in procedure call", N);
2369 -- Otherwise we do have a subexpression with the wrong type
2371 -- Check for the case of an allocator which uses an access type
2372 -- instead of the designated type. This is a common error and we
2373 -- specialize the message, posting an error on the operand of the
2374 -- allocator, complaining that we expected the designated type of
2377 elsif Nkind (N) = N_Allocator
2378 and then Ekind (Typ) in Access_Kind
2379 and then Ekind (Etype (N)) in Access_Kind
2380 and then Designated_Type (Etype (N)) = Typ
2382 Wrong_Type (Expression (N), Designated_Type (Typ));
2385 -- Check for view mismatch on Null in instances, for which the
2386 -- view-swapping mechanism has no identifier.
2388 elsif (In_Instance or else In_Inlined_Body)
2389 and then (Nkind (N) = N_Null)
2390 and then Is_Private_Type (Typ)
2391 and then Is_Access_Type (Full_View (Typ))
2393 Resolve (N, Full_View (Typ));
2397 -- Check for an aggregate. Sometimes we can get bogus aggregates
2398 -- from misuse of parentheses, and we are about to complain about
2399 -- the aggregate without even looking inside it.
2401 -- Instead, if we have an aggregate of type Any_Composite, then
2402 -- analyze and resolve the component fields, and then only issue
2403 -- another message if we get no errors doing this (otherwise
2404 -- assume that the errors in the aggregate caused the problem).
2406 elsif Nkind (N) = N_Aggregate
2407 and then Etype (N) = Any_Composite
2409 -- Disable expansion in any case. If there is a type mismatch
2410 -- it may be fatal to try to expand the aggregate. The flag
2411 -- would otherwise be set to false when the error is posted.
2413 Expander_Active := False;
2416 procedure Check_Aggr (Aggr : Node_Id);
2417 -- Check one aggregate, and set Found to True if we have a
2418 -- definite error in any of its elements
2420 procedure Check_Elmt (Aelmt : Node_Id);
2421 -- Check one element of aggregate and set Found to True if
2422 -- we definitely have an error in the element.
2428 procedure Check_Aggr (Aggr : Node_Id) is
2432 if Present (Expressions (Aggr)) then
2433 Elmt := First (Expressions (Aggr));
2434 while Present (Elmt) loop
2440 if Present (Component_Associations (Aggr)) then
2441 Elmt := First (Component_Associations (Aggr));
2442 while Present (Elmt) loop
2444 -- If this is a default-initialized component, then
2445 -- there is nothing to check. The box will be
2446 -- replaced by the appropriate call during late
2449 if not Box_Present (Elmt) then
2450 Check_Elmt (Expression (Elmt));
2462 procedure Check_Elmt (Aelmt : Node_Id) is
2464 -- If we have a nested aggregate, go inside it (to
2465 -- attempt a naked analyze-resolve of the aggregate can
2466 -- cause undesirable cascaded errors). Do not resolve
2467 -- expression if it needs a type from context, as for
2468 -- integer * fixed expression.
2470 if Nkind (Aelmt) = N_Aggregate then
2476 if not Is_Overloaded (Aelmt)
2477 and then Etype (Aelmt) /= Any_Fixed
2482 if Etype (Aelmt) = Any_Type then
2493 -- If an error message was issued already, Found got reset to
2494 -- True, so if it is still False, issue standard Wrong_Type msg.
2497 if Is_Overloaded (N)
2498 and then Nkind (N) = N_Function_Call
2501 Subp_Name : Node_Id;
2503 if Is_Entity_Name (Name (N)) then
2504 Subp_Name := Name (N);
2506 elsif Nkind (Name (N)) = N_Selected_Component then
2508 -- Protected operation: retrieve operation name
2510 Subp_Name := Selector_Name (Name (N));
2513 raise Program_Error;
2516 Error_Msg_Node_2 := Typ;
2517 Error_Msg_NE ("no visible interpretation of&" &
2518 " matches expected type&", N, Subp_Name);
2521 if All_Errors_Mode then
2523 Index : Interp_Index;
2527 Error_Msg_N ("\\possible interpretations:", N);
2529 Get_First_Interp (Name (N), Index, It);
2530 while Present (It.Nam) loop
2531 Error_Msg_Sloc := Sloc (It.Nam);
2532 Error_Msg_Node_2 := It.Nam;
2534 ("\\ type& for & declared#", N, It.Typ);
2535 Get_Next_Interp (Index, It);
2540 Error_Msg_N ("\use -gnatf for details", N);
2544 Wrong_Type (N, Typ);
2552 -- Test if we have more than one interpretation for the context
2554 elsif Ambiguous then
2558 -- Here we have an acceptable interpretation for the context
2561 -- Propagate type information and normalize tree for various
2562 -- predefined operations. If the context only imposes a class of
2563 -- types, rather than a specific type, propagate the actual type
2566 if Typ = Any_Integer or else
2567 Typ = Any_Boolean or else
2568 Typ = Any_Modular or else
2569 Typ = Any_Real or else
2572 Ctx_Type := Expr_Type;
2574 -- Any_Fixed is legal in a real context only if a specific fixed-
2575 -- point type is imposed. If Norman Cohen can be confused by this,
2576 -- it deserves a separate message.
2579 and then Expr_Type = Any_Fixed
2581 Error_Msg_N ("illegal context for mixed mode operation", N);
2582 Set_Etype (N, Universal_Real);
2583 Ctx_Type := Universal_Real;
2587 -- A user-defined operator is transformed into a function call at
2588 -- this point, so that further processing knows that operators are
2589 -- really operators (i.e. are predefined operators). User-defined
2590 -- operators that are intrinsic are just renamings of the predefined
2591 -- ones, and need not be turned into calls either, but if they rename
2592 -- a different operator, we must transform the node accordingly.
2593 -- Instantiations of Unchecked_Conversion are intrinsic but are
2594 -- treated as functions, even if given an operator designator.
2596 if Nkind (N) in N_Op
2597 and then Present (Entity (N))
2598 and then Ekind (Entity (N)) /= E_Operator
2601 if not Is_Predefined_Op (Entity (N)) then
2602 Rewrite_Operator_As_Call (N, Entity (N));
2604 elsif Present (Alias (Entity (N)))
2606 Nkind (Parent (Parent (Entity (N)))) =
2607 N_Subprogram_Renaming_Declaration
2609 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2611 -- If the node is rewritten, it will be fully resolved in
2612 -- Rewrite_Renamed_Operator.
2614 if Analyzed (N) then
2620 case N_Subexpr'(Nkind (N)) is
2622 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2624 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2626 when N_Short_Circuit
2627 => Resolve_Short_Circuit (N, Ctx_Type);
2629 when N_Attribute_Reference
2630 => Resolve_Attribute (N, Ctx_Type);
2632 when N_Case_Expression
2633 => Resolve_Case_Expression (N, Ctx_Type);
2635 when N_Character_Literal
2636 => Resolve_Character_Literal (N, Ctx_Type);
2638 when N_Conditional_Expression
2639 => Resolve_Conditional_Expression (N, Ctx_Type);
2641 when N_Expanded_Name
2642 => Resolve_Entity_Name (N, Ctx_Type);
2644 when N_Explicit_Dereference
2645 => Resolve_Explicit_Dereference (N, Ctx_Type);
2647 when N_Expression_With_Actions
2648 => Resolve_Expression_With_Actions (N, Ctx_Type);
2650 when N_Extension_Aggregate
2651 => Resolve_Extension_Aggregate (N, Ctx_Type);
2653 when N_Function_Call
2654 => Resolve_Call (N, Ctx_Type);
2657 => Resolve_Entity_Name (N, Ctx_Type);
2659 when N_Indexed_Component
2660 => Resolve_Indexed_Component (N, Ctx_Type);
2662 when N_Integer_Literal
2663 => Resolve_Integer_Literal (N, Ctx_Type);
2665 when N_Membership_Test
2666 => Resolve_Membership_Op (N, Ctx_Type);
2668 when N_Null => Resolve_Null (N, Ctx_Type);
2670 when N_Op_And | N_Op_Or | N_Op_Xor
2671 => Resolve_Logical_Op (N, Ctx_Type);
2673 when N_Op_Eq | N_Op_Ne
2674 => Resolve_Equality_Op (N, Ctx_Type);
2676 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2677 => Resolve_Comparison_Op (N, Ctx_Type);
2679 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2681 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2682 N_Op_Divide | N_Op_Mod | N_Op_Rem
2684 => Resolve_Arithmetic_Op (N, Ctx_Type);
2686 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2688 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2690 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2691 => Resolve_Unary_Op (N, Ctx_Type);
2693 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2695 when N_Procedure_Call_Statement
2696 => Resolve_Call (N, Ctx_Type);
2698 when N_Operator_Symbol
2699 => Resolve_Operator_Symbol (N, Ctx_Type);
2701 when N_Qualified_Expression
2702 => Resolve_Qualified_Expression (N, Ctx_Type);
2704 when N_Quantified_Expression
2705 => Resolve_Quantified_Expression (N, Ctx_Type);
2707 when N_Raise_xxx_Error
2708 => Set_Etype (N, Ctx_Type);
2710 when N_Range => Resolve_Range (N, Ctx_Type);
2713 => Resolve_Real_Literal (N, Ctx_Type);
2715 when N_Reference => Resolve_Reference (N, Ctx_Type);
2717 when N_Selected_Component
2718 => Resolve_Selected_Component (N, Ctx_Type);
2720 when N_Slice => Resolve_Slice (N, Ctx_Type);
2722 when N_String_Literal
2723 => Resolve_String_Literal (N, Ctx_Type);
2725 when N_Subprogram_Info
2726 => Resolve_Subprogram_Info (N, Ctx_Type);
2728 when N_Type_Conversion
2729 => Resolve_Type_Conversion (N, Ctx_Type);
2731 when N_Unchecked_Expression =>
2732 Resolve_Unchecked_Expression (N, Ctx_Type);
2734 when N_Unchecked_Type_Conversion =>
2735 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2738 -- If the subexpression was replaced by a non-subexpression, then
2739 -- all we do is to expand it. The only legitimate case we know of
2740 -- is converting procedure call statement to entry call statements,
2741 -- but there may be others, so we are making this test general.
2743 if Nkind (N) not in N_Subexpr then
2744 Debug_A_Exit ("resolving ", N, " (done)");
2749 -- AI05-144-2: Check dangerous order dependence within an expression
2750 -- that is not a subexpression. Exclude RHS of an assignment, because
2751 -- both sides may have side-effects and the check must be performed
2752 -- over the statement.
2754 if Nkind (Parent (N)) not in N_Subexpr
2755 and then Nkind (Parent (N)) /= N_Assignment_Statement
2756 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2758 Check_Order_Dependence;
2761 -- The expression is definitely NOT overloaded at this point, so
2762 -- we reset the Is_Overloaded flag to avoid any confusion when
2763 -- reanalyzing the node.
2765 Set_Is_Overloaded (N, False);
2767 -- Freeze expression type, entity if it is a name, and designated
2768 -- type if it is an allocator (RM 13.14(10,11,13)).
2770 -- Now that the resolution of the type of the node is complete, and
2771 -- we did not detect an error, we can expand this node. We skip the
2772 -- expand call if we are in a default expression, see section
2773 -- "Handling of Default Expressions" in Sem spec.
2775 Debug_A_Exit ("resolving ", N, " (done)");
2777 -- We unconditionally freeze the expression, even if we are in
2778 -- default expression mode (the Freeze_Expression routine tests this
2779 -- flag and only freezes static types if it is set).
2781 Freeze_Expression (N);
2783 -- Now we can do the expansion
2793 -- Version with check(s) suppressed
2795 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2797 if Suppress = All_Checks then
2799 Svg : constant Suppress_Array := Scope_Suppress;
2801 Scope_Suppress := (others => True);
2803 Scope_Suppress := Svg;
2808 Svg : constant Boolean := Scope_Suppress (Suppress);
2810 Scope_Suppress (Suppress) := True;
2812 Scope_Suppress (Suppress) := Svg;
2821 -- Version with implicit type
2823 procedure Resolve (N : Node_Id) is
2825 Resolve (N, Etype (N));
2828 ---------------------
2829 -- Resolve_Actuals --
2830 ---------------------
2832 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2833 Loc : constant Source_Ptr := Sloc (N);
2838 Prev : Node_Id := Empty;
2841 procedure Check_Argument_Order;
2842 -- Performs a check for the case where the actuals are all simple
2843 -- identifiers that correspond to the formal names, but in the wrong
2844 -- order, which is considered suspicious and cause for a warning.
2846 procedure Check_Prefixed_Call;
2847 -- If the original node is an overloaded call in prefix notation,
2848 -- insert an 'Access or a dereference as needed over the first actual.
2849 -- Try_Object_Operation has already verified that there is a valid
2850 -- interpretation, but the form of the actual can only be determined
2851 -- once the primitive operation is identified.
2853 procedure Insert_Default;
2854 -- If the actual is missing in a call, insert in the actuals list
2855 -- an instance of the default expression. The insertion is always
2856 -- a named association.
2858 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2859 -- Check whether T1 and T2, or their full views, are derived from a
2860 -- common type. Used to enforce the restrictions on array conversions
2863 function Static_Concatenation (N : Node_Id) return Boolean;
2864 -- Predicate to determine whether an actual that is a concatenation
2865 -- will be evaluated statically and does not need a transient scope.
2866 -- This must be determined before the actual is resolved and expanded
2867 -- because if needed the transient scope must be introduced earlier.
2869 --------------------------
2870 -- Check_Argument_Order --
2871 --------------------------
2873 procedure Check_Argument_Order is
2875 -- Nothing to do if no parameters, or original node is neither a
2876 -- function call nor a procedure call statement (happens in the
2877 -- operator-transformed-to-function call case), or the call does
2878 -- not come from source, or this warning is off.
2880 if not Warn_On_Parameter_Order
2881 or else No (Parameter_Associations (N))
2882 or else not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2884 or else not Comes_From_Source (N)
2890 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2893 -- Nothing to do if only one parameter
2899 -- Here if at least two arguments
2902 Actuals : array (1 .. Nargs) of Node_Id;
2906 Wrong_Order : Boolean := False;
2907 -- Set True if an out of order case is found
2910 -- Collect identifier names of actuals, fail if any actual is
2911 -- not a simple identifier, and record max length of name.
2913 Actual := First (Parameter_Associations (N));
2914 for J in Actuals'Range loop
2915 if Nkind (Actual) /= N_Identifier then
2918 Actuals (J) := Actual;
2923 -- If we got this far, all actuals are identifiers and the list
2924 -- of their names is stored in the Actuals array.
2926 Formal := First_Formal (Nam);
2927 for J in Actuals'Range loop
2929 -- If we ran out of formals, that's odd, probably an error
2930 -- which will be detected elsewhere, but abandon the search.
2936 -- If name matches and is in order OK
2938 if Chars (Formal) = Chars (Actuals (J)) then
2942 -- If no match, see if it is elsewhere in list and if so
2943 -- flag potential wrong order if type is compatible.
2945 for K in Actuals'Range loop
2946 if Chars (Formal) = Chars (Actuals (K))
2948 Has_Compatible_Type (Actuals (K), Etype (Formal))
2950 Wrong_Order := True;
2960 <<Continue>> Next_Formal (Formal);
2963 -- If Formals left over, also probably an error, skip warning
2965 if Present (Formal) then
2969 -- Here we give the warning if something was out of order
2973 ("actuals for this call may be in wrong order?", N);
2977 end Check_Argument_Order;
2979 -------------------------
2980 -- Check_Prefixed_Call --
2981 -------------------------
2983 procedure Check_Prefixed_Call is
2984 Act : constant Node_Id := First_Actual (N);
2985 A_Type : constant Entity_Id := Etype (Act);
2986 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2987 Orig : constant Node_Id := Original_Node (N);
2991 -- Check whether the call is a prefixed call, with or without
2992 -- additional actuals.
2994 if Nkind (Orig) = N_Selected_Component
2996 (Nkind (Orig) = N_Indexed_Component
2997 and then Nkind (Prefix (Orig)) = N_Selected_Component
2998 and then Is_Entity_Name (Prefix (Prefix (Orig)))
2999 and then Is_Entity_Name (Act)
3000 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3002 if Is_Access_Type (A_Type)
3003 and then not Is_Access_Type (F_Type)
3005 -- Introduce dereference on object in prefix
3008 Make_Explicit_Dereference (Sloc (Act),
3009 Prefix => Relocate_Node (Act));
3010 Rewrite (Act, New_A);
3013 elsif Is_Access_Type (F_Type)
3014 and then not Is_Access_Type (A_Type)
3016 -- Introduce an implicit 'Access in prefix
3018 if not Is_Aliased_View (Act) then
3020 ("object in prefixed call to& must be aliased"
3021 & " (RM-2005 4.3.1 (13))",
3026 Make_Attribute_Reference (Loc,
3027 Attribute_Name => Name_Access,
3028 Prefix => Relocate_Node (Act)));
3033 end Check_Prefixed_Call;
3035 --------------------
3036 -- Insert_Default --
3037 --------------------
3039 procedure Insert_Default is
3044 -- Missing argument in call, nothing to insert
3046 if No (Default_Value (F)) then
3050 -- Note that we do a full New_Copy_Tree, so that any associated
3051 -- Itypes are properly copied. This may not be needed any more,
3052 -- but it does no harm as a safety measure! Defaults of a generic
3053 -- formal may be out of bounds of the corresponding actual (see
3054 -- cc1311b) and an additional check may be required.
3059 New_Scope => Current_Scope,
3062 if Is_Concurrent_Type (Scope (Nam))
3063 and then Has_Discriminants (Scope (Nam))
3065 Replace_Actual_Discriminants (N, Actval);
3068 if Is_Overloadable (Nam)
3069 and then Present (Alias (Nam))
3071 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3072 and then not Is_Tagged_Type (Etype (F))
3074 -- If default is a real literal, do not introduce a
3075 -- conversion whose effect may depend on the run-time
3076 -- size of universal real.
3078 if Nkind (Actval) = N_Real_Literal then
3079 Set_Etype (Actval, Base_Type (Etype (F)));
3081 Actval := Unchecked_Convert_To (Etype (F), Actval);
3085 if Is_Scalar_Type (Etype (F)) then
3086 Enable_Range_Check (Actval);
3089 Set_Parent (Actval, N);
3091 -- Resolve aggregates with their base type, to avoid scope
3092 -- anomalies: the subtype was first built in the subprogram
3093 -- declaration, and the current call may be nested.
3095 if Nkind (Actval) = N_Aggregate then
3096 Analyze_And_Resolve (Actval, Etype (F));
3098 Analyze_And_Resolve (Actval, Etype (Actval));
3102 Set_Parent (Actval, N);
3104 -- See note above concerning aggregates
3106 if Nkind (Actval) = N_Aggregate
3107 and then Has_Discriminants (Etype (Actval))
3109 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3111 -- Resolve entities with their own type, which may differ from
3112 -- the type of a reference in a generic context (the view
3113 -- swapping mechanism did not anticipate the re-analysis of
3114 -- default values in calls).
3116 elsif Is_Entity_Name (Actval) then
3117 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3120 Analyze_And_Resolve (Actval, Etype (Actval));
3124 -- If default is a tag indeterminate function call, propagate tag
3125 -- to obtain proper dispatching.
3127 if Is_Controlling_Formal (F)
3128 and then Nkind (Default_Value (F)) = N_Function_Call
3130 Set_Is_Controlling_Actual (Actval);
3135 -- If the default expression raises constraint error, then just
3136 -- silently replace it with an N_Raise_Constraint_Error node, since
3137 -- we already gave the warning on the subprogram spec. If node is
3138 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3139 -- the warnings removal machinery.
3141 if Raises_Constraint_Error (Actval)
3142 and then Nkind (Actval) /= N_Raise_Constraint_Error
3145 Make_Raise_Constraint_Error (Loc,
3146 Reason => CE_Range_Check_Failed));
3147 Set_Raises_Constraint_Error (Actval);
3148 Set_Etype (Actval, Etype (F));
3152 Make_Parameter_Association (Loc,
3153 Explicit_Actual_Parameter => Actval,
3154 Selector_Name => Make_Identifier (Loc, Chars (F)));
3156 -- Case of insertion is first named actual
3158 if No (Prev) or else
3159 Nkind (Parent (Prev)) /= N_Parameter_Association
3161 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3162 Set_First_Named_Actual (N, Actval);
3165 if No (Parameter_Associations (N)) then
3166 Set_Parameter_Associations (N, New_List (Assoc));
3168 Append (Assoc, Parameter_Associations (N));
3172 Insert_After (Prev, Assoc);
3175 -- Case of insertion is not first named actual
3178 Set_Next_Named_Actual
3179 (Assoc, Next_Named_Actual (Parent (Prev)));
3180 Set_Next_Named_Actual (Parent (Prev), Actval);
3181 Append (Assoc, Parameter_Associations (N));
3184 Mark_Rewrite_Insertion (Assoc);
3185 Mark_Rewrite_Insertion (Actval);
3194 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3195 FT1 : Entity_Id := T1;
3196 FT2 : Entity_Id := T2;
3199 if Is_Private_Type (T1)
3200 and then Present (Full_View (T1))
3202 FT1 := Full_View (T1);
3205 if Is_Private_Type (T2)
3206 and then Present (Full_View (T2))
3208 FT2 := Full_View (T2);
3211 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3214 --------------------------
3215 -- Static_Concatenation --
3216 --------------------------
3218 function Static_Concatenation (N : Node_Id) return Boolean is
3221 when N_String_Literal =>
3226 -- Concatenation is static when both operands are static and
3227 -- the concatenation operator is a predefined one.
3229 return Scope (Entity (N)) = Standard_Standard
3231 Static_Concatenation (Left_Opnd (N))
3233 Static_Concatenation (Right_Opnd (N));
3236 if Is_Entity_Name (N) then
3238 Ent : constant Entity_Id := Entity (N);
3240 return Ekind (Ent) = E_Constant
3241 and then Present (Constant_Value (Ent))
3243 Is_Static_Expression (Constant_Value (Ent));
3250 end Static_Concatenation;
3252 -- Start of processing for Resolve_Actuals
3255 Check_Argument_Order;
3257 if Present (First_Actual (N)) then
3258 Check_Prefixed_Call;
3261 A := First_Actual (N);
3262 F := First_Formal (Nam);
3263 while Present (F) loop
3264 if No (A) and then Needs_No_Actuals (Nam) then
3267 -- If we have an error in any actual or formal, indicated by a type
3268 -- of Any_Type, then abandon resolution attempt, and set result type
3271 elsif (Present (A) and then Etype (A) = Any_Type)
3272 or else Etype (F) = Any_Type
3274 Set_Etype (N, Any_Type);
3278 -- Case where actual is present
3280 -- If the actual is an entity, generate a reference to it now. We
3281 -- do this before the actual is resolved, because a formal of some
3282 -- protected subprogram, or a task discriminant, will be rewritten
3283 -- during expansion, and the source entity reference may be lost.
3286 and then Is_Entity_Name (A)
3287 and then Comes_From_Source (N)
3289 Orig_A := Entity (A);
3291 if Present (Orig_A) then
3292 if Is_Formal (Orig_A)
3293 and then Ekind (F) /= E_In_Parameter
3295 Generate_Reference (Orig_A, A, 'm');
3297 elsif not Is_Overloaded (A) then
3298 Generate_Reference (Orig_A, A);
3304 and then (Nkind (Parent (A)) /= N_Parameter_Association
3305 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3307 -- If style checking mode on, check match of formal name
3310 if Nkind (Parent (A)) = N_Parameter_Association then
3311 Check_Identifier (Selector_Name (Parent (A)), F);
3315 -- If the formal is Out or In_Out, do not resolve and expand the
3316 -- conversion, because it is subsequently expanded into explicit
3317 -- temporaries and assignments. However, the object of the
3318 -- conversion can be resolved. An exception is the case of tagged
3319 -- type conversion with a class-wide actual. In that case we want
3320 -- the tag check to occur and no temporary will be needed (no
3321 -- representation change can occur) and the parameter is passed by
3322 -- reference, so we go ahead and resolve the type conversion.
3323 -- Another exception is the case of reference to component or
3324 -- subcomponent of a bit-packed array, in which case we want to
3325 -- defer expansion to the point the in and out assignments are
3328 if Ekind (F) /= E_In_Parameter
3329 and then Nkind (A) = N_Type_Conversion
3330 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3332 if Ekind (F) = E_In_Out_Parameter
3333 and then Is_Array_Type (Etype (F))
3335 -- In a view conversion, the conversion must be legal in
3336 -- both directions, and thus both component types must be
3337 -- aliased, or neither (4.6 (8)).
3339 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3340 -- the privacy requirement should not apply to generic
3341 -- types, and should be checked in an instance. ARG query
3344 if Has_Aliased_Components (Etype (Expression (A))) /=
3345 Has_Aliased_Components (Etype (F))
3348 ("both component types in a view conversion must be"
3349 & " aliased, or neither", A);
3351 -- Comment here??? what set of cases???
3354 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3356 -- Check view conv between unrelated by ref array types
3358 if Is_By_Reference_Type (Etype (F))
3359 or else Is_By_Reference_Type (Etype (Expression (A)))
3362 ("view conversion between unrelated by reference " &
3363 "array types not allowed (\'A'I-00246)", A);
3365 -- In Ada 2005 mode, check view conversion component
3366 -- type cannot be private, tagged, or volatile. Note
3367 -- that we only apply this to source conversions. The
3368 -- generated code can contain conversions which are
3369 -- not subject to this test, and we cannot extract the
3370 -- component type in such cases since it is not present.
3372 elsif Comes_From_Source (A)
3373 and then Ada_Version >= Ada_2005
3376 Comp_Type : constant Entity_Id :=
3378 (Etype (Expression (A)));
3380 if (Is_Private_Type (Comp_Type)
3381 and then not Is_Generic_Type (Comp_Type))
3382 or else Is_Tagged_Type (Comp_Type)
3383 or else Is_Volatile (Comp_Type)
3386 ("component type of a view conversion cannot"
3387 & " be private, tagged, or volatile"
3396 -- Resolve expression if conversion is all OK
3398 if (Conversion_OK (A)
3399 or else Valid_Conversion (A, Etype (A), Expression (A)))
3400 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3402 Resolve (Expression (A));
3405 -- If the actual is a function call that returns a limited
3406 -- unconstrained object that needs finalization, create a
3407 -- transient scope for it, so that it can receive the proper
3408 -- finalization list.
3410 elsif Nkind (A) = N_Function_Call
3411 and then Is_Limited_Record (Etype (F))
3412 and then not Is_Constrained (Etype (F))
3413 and then Expander_Active
3414 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3416 Establish_Transient_Scope (A, False);
3418 -- A small optimization: if one of the actuals is a concatenation
3419 -- create a block around a procedure call to recover stack space.
3420 -- This alleviates stack usage when several procedure calls in
3421 -- the same statement list use concatenation. We do not perform
3422 -- this wrapping for code statements, where the argument is a
3423 -- static string, and we want to preserve warnings involving
3424 -- sequences of such statements.
3426 elsif Nkind (A) = N_Op_Concat
3427 and then Nkind (N) = N_Procedure_Call_Statement
3428 and then Expander_Active
3430 not (Is_Intrinsic_Subprogram (Nam)
3431 and then Chars (Nam) = Name_Asm)
3432 and then not Static_Concatenation (A)
3434 Establish_Transient_Scope (A, False);
3435 Resolve (A, Etype (F));
3438 if Nkind (A) = N_Type_Conversion
3439 and then Is_Array_Type (Etype (F))
3440 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3442 (Is_Limited_Type (Etype (F))
3443 or else Is_Limited_Type (Etype (Expression (A))))
3446 ("conversion between unrelated limited array types " &
3447 "not allowed (\A\I-00246)", A);
3449 if Is_Limited_Type (Etype (F)) then
3450 Explain_Limited_Type (Etype (F), A);
3453 if Is_Limited_Type (Etype (Expression (A))) then
3454 Explain_Limited_Type (Etype (Expression (A)), A);
3458 -- (Ada 2005: AI-251): If the actual is an allocator whose
3459 -- directly designated type is a class-wide interface, we build
3460 -- an anonymous access type to use it as the type of the
3461 -- allocator. Later, when the subprogram call is expanded, if
3462 -- the interface has a secondary dispatch table the expander
3463 -- will add a type conversion to force the correct displacement
3466 if Nkind (A) = N_Allocator then
3468 DDT : constant Entity_Id :=
3469 Directly_Designated_Type (Base_Type (Etype (F)));
3471 New_Itype : Entity_Id;
3474 if Is_Class_Wide_Type (DDT)
3475 and then Is_Interface (DDT)
3477 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3478 Set_Etype (New_Itype, Etype (A));
3479 Set_Directly_Designated_Type (New_Itype,
3480 Directly_Designated_Type (Etype (A)));
3481 Set_Etype (A, New_Itype);
3484 -- Ada 2005, AI-162:If the actual is an allocator, the
3485 -- innermost enclosing statement is the master of the
3486 -- created object. This needs to be done with expansion
3487 -- enabled only, otherwise the transient scope will not
3488 -- be removed in the expansion of the wrapped construct.
3490 if (Is_Controlled (DDT) or else Has_Task (DDT))
3491 and then Expander_Active
3493 Establish_Transient_Scope (A, False);
3498 -- (Ada 2005): The call may be to a primitive operation of
3499 -- a tagged synchronized type, declared outside of the type.
3500 -- In this case the controlling actual must be converted to
3501 -- its corresponding record type, which is the formal type.
3502 -- The actual may be a subtype, either because of a constraint
3503 -- or because it is a generic actual, so use base type to
3504 -- locate concurrent type.
3506 F_Typ := Base_Type (Etype (F));
3508 if Is_Tagged_Type (F_Typ)
3509 and then (Is_Concurrent_Type (F_Typ)
3510 or else Is_Concurrent_Record_Type (F_Typ))
3512 -- If the actual is overloaded, look for an interpretation
3513 -- that has a synchronized type.
3515 if not Is_Overloaded (A) then
3516 A_Typ := Base_Type (Etype (A));
3520 Index : Interp_Index;
3524 Get_First_Interp (A, Index, It);
3525 while Present (It.Typ) loop
3526 if Is_Concurrent_Type (It.Typ)
3527 or else Is_Concurrent_Record_Type (It.Typ)
3529 A_Typ := Base_Type (It.Typ);
3533 Get_Next_Interp (Index, It);
3539 Full_A_Typ : Entity_Id;
3542 if Present (Full_View (A_Typ)) then
3543 Full_A_Typ := Base_Type (Full_View (A_Typ));
3545 Full_A_Typ := A_Typ;
3548 -- Tagged synchronized type (case 1): the actual is a
3551 if Is_Concurrent_Type (A_Typ)
3552 and then Corresponding_Record_Type (A_Typ) = F_Typ
3555 Unchecked_Convert_To
3556 (Corresponding_Record_Type (A_Typ), A));
3557 Resolve (A, Etype (F));
3559 -- Tagged synchronized type (case 2): the formal is a
3562 elsif Ekind (Full_A_Typ) = E_Record_Type
3564 (Corresponding_Concurrent_Type (Full_A_Typ))
3565 and then Is_Concurrent_Type (F_Typ)
3566 and then Present (Corresponding_Record_Type (F_Typ))
3567 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3569 Resolve (A, Corresponding_Record_Type (F_Typ));
3574 Resolve (A, Etype (F));
3579 -- not a synchronized operation.
3581 Resolve (A, Etype (F));
3588 if Comes_From_Source (Original_Node (N))
3589 and then Nkind_In (Original_Node (N), N_Function_Call,
3590 N_Procedure_Call_Statement)
3592 -- In formal mode, check that actual parameters matching
3593 -- formals of tagged types are objects (or ancestor type
3594 -- conversions of objects), not general expressions.
3596 if Is_Actual_Tagged_Parameter (A) then
3597 if Is_SPARK_Object_Reference (A) then
3600 elsif Nkind (A) = N_Type_Conversion then
3602 Operand : constant Node_Id := Expression (A);
3603 Operand_Typ : constant Entity_Id := Etype (Operand);
3604 Target_Typ : constant Entity_Id := A_Typ;
3607 if not Is_SPARK_Object_Reference (Operand) then
3608 Check_SPARK_Restriction
3609 ("object required", Operand);
3611 -- In formal mode, the only view conversions are those
3612 -- involving ancestor conversion of an extended type.
3615 (Is_Tagged_Type (Target_Typ)
3616 and then not Is_Class_Wide_Type (Target_Typ)
3617 and then Is_Tagged_Type (Operand_Typ)
3618 and then not Is_Class_Wide_Type (Operand_Typ)
3619 and then Is_Ancestor (Target_Typ, Operand_Typ))
3622 (F, E_Out_Parameter, E_In_Out_Parameter)
3624 Check_SPARK_Restriction
3625 ("ancestor conversion is the only permitted "
3626 & "view conversion", A);
3628 Check_SPARK_Restriction
3629 ("ancestor conversion required", A);
3638 Check_SPARK_Restriction ("object required", A);
3641 -- In formal mode, the only view conversions are those
3642 -- involving ancestor conversion of an extended type.
3644 elsif Nkind (A) = N_Type_Conversion
3645 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3647 Check_SPARK_Restriction
3648 ("ancestor conversion is the only permitted view "
3653 -- Save actual for subsequent check on order dependence, and
3654 -- indicate whether actual is modifiable. For AI05-0144-2.
3656 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3658 -- For mode IN, if actual is an entity, and the type of the formal
3659 -- has warnings suppressed, then we reset Never_Set_In_Source for
3660 -- the calling entity. The reason for this is to catch cases like
3661 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3662 -- uses trickery to modify an IN parameter.
3664 if Ekind (F) = E_In_Parameter
3665 and then Is_Entity_Name (A)
3666 and then Present (Entity (A))
3667 and then Ekind (Entity (A)) = E_Variable
3668 and then Has_Warnings_Off (F_Typ)
3670 Set_Never_Set_In_Source (Entity (A), False);
3673 -- Perform error checks for IN and IN OUT parameters
3675 if Ekind (F) /= E_Out_Parameter then
3677 -- Check unset reference. For scalar parameters, it is clearly
3678 -- wrong to pass an uninitialized value as either an IN or
3679 -- IN-OUT parameter. For composites, it is also clearly an
3680 -- error to pass a completely uninitialized value as an IN
3681 -- parameter, but the case of IN OUT is trickier. We prefer
3682 -- not to give a warning here. For example, suppose there is
3683 -- a routine that sets some component of a record to False.
3684 -- It is perfectly reasonable to make this IN-OUT and allow
3685 -- either initialized or uninitialized records to be passed
3688 -- For partially initialized composite values, we also avoid
3689 -- warnings, since it is quite likely that we are passing a
3690 -- partially initialized value and only the initialized fields
3691 -- will in fact be read in the subprogram.
3693 if Is_Scalar_Type (A_Typ)
3694 or else (Ekind (F) = E_In_Parameter
3695 and then not Is_Partially_Initialized_Type (A_Typ))
3697 Check_Unset_Reference (A);
3700 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3701 -- actual to a nested call, since this is case of reading an
3702 -- out parameter, which is not allowed.
3704 if Ada_Version = Ada_83
3705 and then Is_Entity_Name (A)
3706 and then Ekind (Entity (A)) = E_Out_Parameter
3708 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3712 -- Case of OUT or IN OUT parameter
3714 if Ekind (F) /= E_In_Parameter then
3716 -- For an Out parameter, check for useless assignment. Note
3717 -- that we can't set Last_Assignment this early, because we may
3718 -- kill current values in Resolve_Call, and that call would
3719 -- clobber the Last_Assignment field.
3721 -- Note: call Warn_On_Useless_Assignment before doing the check
3722 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3723 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3724 -- reflects the last assignment, not this one!
3726 if Ekind (F) = E_Out_Parameter then
3727 if Warn_On_Modified_As_Out_Parameter (F)
3728 and then Is_Entity_Name (A)
3729 and then Present (Entity (A))
3730 and then Comes_From_Source (N)
3732 Warn_On_Useless_Assignment (Entity (A), A);
3736 -- Validate the form of the actual. Note that the call to
3737 -- Is_OK_Variable_For_Out_Formal generates the required
3738 -- reference in this case.
3740 if not Is_OK_Variable_For_Out_Formal (A) then
3741 Error_Msg_NE ("actual for& must be a variable", A, F);
3744 -- What's the following about???
3746 if Is_Entity_Name (A) then
3747 Kill_Checks (Entity (A));
3753 if Etype (A) = Any_Type then
3754 Set_Etype (N, Any_Type);
3758 -- Apply appropriate range checks for in, out, and in-out
3759 -- parameters. Out and in-out parameters also need a separate
3760 -- check, if there is a type conversion, to make sure the return
3761 -- value meets the constraints of the variable before the
3764 -- Gigi looks at the check flag and uses the appropriate types.
3765 -- For now since one flag is used there is an optimization which
3766 -- might not be done in the In Out case since Gigi does not do
3767 -- any analysis. More thought required about this ???
3769 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3771 -- Apply predicate checks, unless this is a call to the
3772 -- predicate check function itself, which would cause an
3773 -- infinite recursion.
3775 if not (Ekind (Nam) = E_Function
3776 and then Has_Predicates (Nam))
3778 Apply_Predicate_Check (A, F_Typ);
3781 -- Apply required constraint checks
3783 if Is_Scalar_Type (Etype (A)) then
3784 Apply_Scalar_Range_Check (A, F_Typ);
3786 elsif Is_Array_Type (Etype (A)) then
3787 Apply_Length_Check (A, F_Typ);
3789 elsif Is_Record_Type (F_Typ)
3790 and then Has_Discriminants (F_Typ)
3791 and then Is_Constrained (F_Typ)
3792 and then (not Is_Derived_Type (F_Typ)
3793 or else Comes_From_Source (Nam))
3795 Apply_Discriminant_Check (A, F_Typ);
3797 elsif Is_Access_Type (F_Typ)
3798 and then Is_Array_Type (Designated_Type (F_Typ))
3799 and then Is_Constrained (Designated_Type (F_Typ))
3801 Apply_Length_Check (A, F_Typ);
3803 elsif Is_Access_Type (F_Typ)
3804 and then Has_Discriminants (Designated_Type (F_Typ))
3805 and then Is_Constrained (Designated_Type (F_Typ))
3807 Apply_Discriminant_Check (A, F_Typ);
3810 Apply_Range_Check (A, F_Typ);
3813 -- Ada 2005 (AI-231): Note that the controlling parameter case
3814 -- already existed in Ada 95, which is partially checked
3815 -- elsewhere (see Checks), and we don't want the warning
3816 -- message to differ.
3818 if Is_Access_Type (F_Typ)
3819 and then Can_Never_Be_Null (F_Typ)
3820 and then Known_Null (A)
3822 if Is_Controlling_Formal (F) then
3823 Apply_Compile_Time_Constraint_Error
3825 Msg => "null value not allowed here?",
3826 Reason => CE_Access_Check_Failed);
3828 elsif Ada_Version >= Ada_2005 then
3829 Apply_Compile_Time_Constraint_Error
3831 Msg => "(Ada 2005) null not allowed in "
3832 & "null-excluding formal?",
3833 Reason => CE_Null_Not_Allowed);
3838 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3839 if Nkind (A) = N_Type_Conversion then
3840 if Is_Scalar_Type (A_Typ) then
3841 Apply_Scalar_Range_Check
3842 (Expression (A), Etype (Expression (A)), A_Typ);
3845 (Expression (A), Etype (Expression (A)), A_Typ);
3849 if Is_Scalar_Type (F_Typ) then
3850 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3851 elsif Is_Array_Type (F_Typ)
3852 and then Ekind (F) = E_Out_Parameter
3854 Apply_Length_Check (A, F_Typ);
3856 Apply_Range_Check (A, A_Typ, F_Typ);
3861 -- An actual associated with an access parameter is implicitly
3862 -- converted to the anonymous access type of the formal and must
3863 -- satisfy the legality checks for access conversions.
3865 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3866 if not Valid_Conversion (A, F_Typ, A) then
3868 ("invalid implicit conversion for access parameter", A);
3872 -- Check bad case of atomic/volatile argument (RM C.6(12))
3874 if Is_By_Reference_Type (Etype (F))
3875 and then Comes_From_Source (N)
3877 if Is_Atomic_Object (A)
3878 and then not Is_Atomic (Etype (F))
3881 ("cannot pass atomic argument to non-atomic formal",
3884 elsif Is_Volatile_Object (A)
3885 and then not Is_Volatile (Etype (F))
3888 ("cannot pass volatile argument to non-volatile formal",
3893 -- Check that subprograms don't have improper controlling
3894 -- arguments (RM 3.9.2 (9)).
3896 -- A primitive operation may have an access parameter of an
3897 -- incomplete tagged type, but a dispatching call is illegal
3898 -- if the type is still incomplete.
3900 if Is_Controlling_Formal (F) then
3901 Set_Is_Controlling_Actual (A);
3903 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3905 Desig : constant Entity_Id := Designated_Type (Etype (F));
3907 if Ekind (Desig) = E_Incomplete_Type
3908 and then No (Full_View (Desig))
3909 and then No (Non_Limited_View (Desig))
3912 ("premature use of incomplete type& " &
3913 "in dispatching call", A, Desig);
3918 elsif Nkind (A) = N_Explicit_Dereference then
3919 Validate_Remote_Access_To_Class_Wide_Type (A);
3922 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3923 and then not Is_Class_Wide_Type (F_Typ)
3924 and then not Is_Controlling_Formal (F)
3926 Error_Msg_N ("class-wide argument not allowed here!", A);
3928 if Is_Subprogram (Nam)
3929 and then Comes_From_Source (Nam)
3931 Error_Msg_Node_2 := F_Typ;
3933 ("& is not a dispatching operation of &!", A, Nam);
3936 elsif Is_Access_Type (A_Typ)
3937 and then Is_Access_Type (F_Typ)
3938 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3939 and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3940 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3941 or else (Nkind (A) = N_Attribute_Reference
3943 Is_Class_Wide_Type (Etype (Prefix (A)))))
3944 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3945 and then not Is_Controlling_Formal (F)
3947 -- Disable these checks for call to imported C++ subprograms
3950 (Is_Entity_Name (Name (N))
3951 and then Is_Imported (Entity (Name (N)))
3952 and then Convention (Entity (Name (N))) = Convention_CPP)
3955 ("access to class-wide argument not allowed here!", A);
3957 if Is_Subprogram (Nam)
3958 and then Comes_From_Source (Nam)
3960 Error_Msg_Node_2 := Designated_Type (F_Typ);
3962 ("& is not a dispatching operation of &!", A, Nam);
3968 -- If it is a named association, treat the selector_name as a
3969 -- proper identifier, and mark the corresponding entity.
3971 if Nkind (Parent (A)) = N_Parameter_Association then
3972 Set_Entity (Selector_Name (Parent (A)), F);
3973 Generate_Reference (F, Selector_Name (Parent (A)));
3974 Set_Etype (Selector_Name (Parent (A)), F_Typ);
3975 Generate_Reference (F_Typ, N, ' ');
3980 if Ekind (F) /= E_Out_Parameter then
3981 Check_Unset_Reference (A);
3986 -- Case where actual is not present
3994 end Resolve_Actuals;
3996 -----------------------
3997 -- Resolve_Allocator --
3998 -----------------------
4000 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4001 E : constant Node_Id := Expression (N);
4003 Discrim : Entity_Id;
4006 Assoc : Node_Id := Empty;
4009 procedure Check_Allocator_Discrim_Accessibility
4010 (Disc_Exp : Node_Id;
4011 Alloc_Typ : Entity_Id);
4012 -- Check that accessibility level associated with an access discriminant
4013 -- initialized in an allocator by the expression Disc_Exp is not deeper
4014 -- than the level of the allocator type Alloc_Typ. An error message is
4015 -- issued if this condition is violated. Specialized checks are done for
4016 -- the cases of a constraint expression which is an access attribute or
4017 -- an access discriminant.
4019 function In_Dispatching_Context return Boolean;
4020 -- If the allocator is an actual in a call, it is allowed to be class-
4021 -- wide when the context is not because it is a controlling actual.
4023 procedure Propagate_Coextensions (Root : Node_Id);
4024 -- Propagate all nested coextensions which are located one nesting
4025 -- level down the tree to the node Root. Example:
4028 -- Level_1_Coextension
4029 -- Level_2_Coextension
4031 -- The algorithm is paired with delay actions done by the Expander. In
4032 -- the above example, assume all coextensions are controlled types.
4033 -- The cycle of analysis, resolution and expansion will yield:
4035 -- 1) Analyze Top_Record
4036 -- 2) Analyze Level_1_Coextension
4037 -- 3) Analyze Level_2_Coextension
4038 -- 4) Resolve Level_2_Coextension. The allocator is marked as a
4040 -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is
4041 -- generated to capture the allocated object. Temp_1 is attached
4042 -- to the coextension chain of Level_2_Coextension.
4043 -- 6) Resolve Level_1_Coextension. The allocator is marked as a
4044 -- coextension. A forward tree traversal is performed which finds
4045 -- Level_2_Coextension's list and copies its contents into its
4047 -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is
4048 -- generated to capture the allocated object. Temp_2 is attached
4049 -- to the coextension chain of Level_1_Coextension. Currently, the
4050 -- contents of the list are [Temp_2, Temp_1].
4051 -- 8) Resolve Top_Record. A forward tree traversal is performed which
4052 -- finds Level_1_Coextension's list and copies its contents into
4054 -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and
4055 -- Temp_2 and attach them to Top_Record's finalization list.
4057 -------------------------------------------
4058 -- Check_Allocator_Discrim_Accessibility --
4059 -------------------------------------------
4061 procedure Check_Allocator_Discrim_Accessibility
4062 (Disc_Exp : Node_Id;
4063 Alloc_Typ : Entity_Id)
4066 if Type_Access_Level (Etype (Disc_Exp)) >
4067 Type_Access_Level (Alloc_Typ)
4070 ("operand type has deeper level than allocator type", Disc_Exp);
4072 -- When the expression is an Access attribute the level of the prefix
4073 -- object must not be deeper than that of the allocator's type.
4075 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4076 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
4078 and then Object_Access_Level (Prefix (Disc_Exp))
4079 > Type_Access_Level (Alloc_Typ)
4082 ("prefix of attribute has deeper level than allocator type",
4085 -- When the expression is an access discriminant the check is against
4086 -- the level of the prefix object.
4088 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4089 and then Nkind (Disc_Exp) = N_Selected_Component
4090 and then Object_Access_Level (Prefix (Disc_Exp))
4091 > Type_Access_Level (Alloc_Typ)
4094 ("access discriminant has deeper level than allocator type",
4097 -- All other cases are legal
4102 end Check_Allocator_Discrim_Accessibility;
4104 ----------------------------
4105 -- In_Dispatching_Context --
4106 ----------------------------
4108 function In_Dispatching_Context return Boolean is
4109 Par : constant Node_Id := Parent (N);
4111 return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
4112 and then Is_Entity_Name (Name (Par))
4113 and then Is_Dispatching_Operation (Entity (Name (Par)));
4114 end In_Dispatching_Context;
4116 ----------------------------
4117 -- Propagate_Coextensions --
4118 ----------------------------
4120 procedure Propagate_Coextensions (Root : Node_Id) is
4122 procedure Copy_List (From : Elist_Id; To : Elist_Id);
4123 -- Copy the contents of list From into list To, preserving the
4124 -- order of elements.
4126 function Process_Allocator (Nod : Node_Id) return Traverse_Result;
4127 -- Recognize an allocator or a rewritten allocator node and add it
4128 -- along with its nested coextensions to the list of Root.
4134 procedure Copy_List (From : Elist_Id; To : Elist_Id) is
4135 From_Elmt : Elmt_Id;
4137 From_Elmt := First_Elmt (From);
4138 while Present (From_Elmt) loop
4139 Append_Elmt (Node (From_Elmt), To);
4140 Next_Elmt (From_Elmt);
4144 -----------------------
4145 -- Process_Allocator --
4146 -----------------------
4148 function Process_Allocator (Nod : Node_Id) return Traverse_Result is
4149 Orig_Nod : Node_Id := Nod;
4152 -- This is a possible rewritten subtype indication allocator. Any
4153 -- nested coextensions will appear as discriminant constraints.
4155 if Nkind (Nod) = N_Identifier
4156 and then Present (Original_Node (Nod))
4157 and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
4161 Discr_Elmt : Elmt_Id;
4164 if Is_Record_Type (Entity (Nod)) then
4166 First_Elmt (Discriminant_Constraint (Entity (Nod)));
4167 while Present (Discr_Elmt) loop
4168 Discr := Node (Discr_Elmt);
4170 if Nkind (Discr) = N_Identifier
4171 and then Present (Original_Node (Discr))
4172 and then Nkind (Original_Node (Discr)) = N_Allocator
4173 and then Present (Coextensions (
4174 Original_Node (Discr)))
4176 if No (Coextensions (Root)) then
4177 Set_Coextensions (Root, New_Elmt_List);
4181 (From => Coextensions (Original_Node (Discr)),
4182 To => Coextensions (Root));
4185 Next_Elmt (Discr_Elmt);
4188 -- There is no need to continue the traversal of this
4189 -- subtree since all the information has already been
4196 -- Case of either a stand alone allocator or a rewritten allocator
4197 -- with an aggregate.
4200 if Present (Original_Node (Nod)) then
4201 Orig_Nod := Original_Node (Nod);
4204 if Nkind (Orig_Nod) = N_Allocator then
4206 -- Propagate the list of nested coextensions to the Root
4207 -- allocator. This is done through list copy since a single
4208 -- allocator may have multiple coextensions. Do not touch
4209 -- coextensions roots.
4211 if not Is_Coextension_Root (Orig_Nod)
4212 and then Present (Coextensions (Orig_Nod))
4214 if No (Coextensions (Root)) then
4215 Set_Coextensions (Root, New_Elmt_List);
4219 (From => Coextensions (Orig_Nod),
4220 To => Coextensions (Root));
4223 -- There is no need to continue the traversal of this
4224 -- subtree since all the information has already been
4231 -- Keep on traversing, looking for the next allocator
4234 end Process_Allocator;
4236 procedure Process_Allocators is
4237 new Traverse_Proc (Process_Allocator);
4239 -- Start of processing for Propagate_Coextensions
4242 Process_Allocators (Expression (Root));
4243 end Propagate_Coextensions;
4245 -- Start of processing for Resolve_Allocator
4248 -- Replace general access with specific type
4250 if Ekind (Etype (N)) = E_Allocator_Type then
4251 Set_Etype (N, Base_Type (Typ));
4254 if Is_Abstract_Type (Typ) then
4255 Error_Msg_N ("type of allocator cannot be abstract", N);
4258 -- For qualified expression, resolve the expression using the
4259 -- given subtype (nothing to do for type mark, subtype indication)
4261 if Nkind (E) = N_Qualified_Expression then
4262 if Is_Class_Wide_Type (Etype (E))
4263 and then not Is_Class_Wide_Type (Designated_Type (Typ))
4264 and then not In_Dispatching_Context
4267 ("class-wide allocator not allowed for this access type", N);
4270 Resolve (Expression (E), Etype (E));
4271 Check_Unset_Reference (Expression (E));
4273 -- A qualified expression requires an exact match of the type,
4274 -- class-wide matching is not allowed.
4276 if (Is_Class_Wide_Type (Etype (Expression (E)))
4277 or else Is_Class_Wide_Type (Etype (E)))
4278 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4280 Wrong_Type (Expression (E), Etype (E));
4283 -- A special accessibility check is needed for allocators that
4284 -- constrain access discriminants. The level of the type of the
4285 -- expression used to constrain an access discriminant cannot be
4286 -- deeper than the type of the allocator (in contrast to access
4287 -- parameters, where the level of the actual can be arbitrary).
4289 -- We can't use Valid_Conversion to perform this check because
4290 -- in general the type of the allocator is unrelated to the type
4291 -- of the access discriminant.
4293 if Ekind (Typ) /= E_Anonymous_Access_Type
4294 or else Is_Local_Anonymous_Access (Typ)
4296 Subtyp := Entity (Subtype_Mark (E));
4298 Aggr := Original_Node (Expression (E));
4300 if Has_Discriminants (Subtyp)
4301 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4303 Discrim := First_Discriminant (Base_Type (Subtyp));
4305 -- Get the first component expression of the aggregate
4307 if Present (Expressions (Aggr)) then
4308 Disc_Exp := First (Expressions (Aggr));
4310 elsif Present (Component_Associations (Aggr)) then
4311 Assoc := First (Component_Associations (Aggr));
4313 if Present (Assoc) then
4314 Disc_Exp := Expression (Assoc);
4323 while Present (Discrim) and then Present (Disc_Exp) loop
4324 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4325 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4328 Next_Discriminant (Discrim);
4330 if Present (Discrim) then
4331 if Present (Assoc) then
4333 Disc_Exp := Expression (Assoc);
4335 elsif Present (Next (Disc_Exp)) then
4339 Assoc := First (Component_Associations (Aggr));
4341 if Present (Assoc) then
4342 Disc_Exp := Expression (Assoc);
4352 -- For a subtype mark or subtype indication, freeze the subtype
4355 Freeze_Expression (E);
4357 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4359 ("initialization required for access-to-constant allocator", N);
4362 -- A special accessibility check is needed for allocators that
4363 -- constrain access discriminants. The level of the type of the
4364 -- expression used to constrain an access discriminant cannot be
4365 -- deeper than the type of the allocator (in contrast to access
4366 -- parameters, where the level of the actual can be arbitrary).
4367 -- We can't use Valid_Conversion to perform this check because
4368 -- in general the type of the allocator is unrelated to the type
4369 -- of the access discriminant.
4371 if Nkind (Original_Node (E)) = N_Subtype_Indication
4372 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4373 or else Is_Local_Anonymous_Access (Typ))
4375 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4377 if Has_Discriminants (Subtyp) then
4378 Discrim := First_Discriminant (Base_Type (Subtyp));
4379 Constr := First (Constraints (Constraint (Original_Node (E))));
4380 while Present (Discrim) and then Present (Constr) loop
4381 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4382 if Nkind (Constr) = N_Discriminant_Association then
4383 Disc_Exp := Original_Node (Expression (Constr));
4385 Disc_Exp := Original_Node (Constr);
4388 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4391 Next_Discriminant (Discrim);
4398 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4399 -- check that the level of the type of the created object is not deeper
4400 -- than the level of the allocator's access type, since extensions can
4401 -- now occur at deeper levels than their ancestor types. This is a
4402 -- static accessibility level check; a run-time check is also needed in
4403 -- the case of an initialized allocator with a class-wide argument (see
4404 -- Expand_Allocator_Expression).
4406 if Ada_Version >= Ada_2005
4407 and then Is_Class_Wide_Type (Designated_Type (Typ))
4410 Exp_Typ : Entity_Id;
4413 if Nkind (E) = N_Qualified_Expression then
4414 Exp_Typ := Etype (E);
4415 elsif Nkind (E) = N_Subtype_Indication then
4416 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4418 Exp_Typ := Entity (E);
4421 if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
4422 if In_Instance_Body then
4423 Error_Msg_N ("?type in allocator has deeper level than" &
4424 " designated class-wide type", E);
4425 Error_Msg_N ("\?Program_Error will be raised at run time",
4428 Make_Raise_Program_Error (Sloc (N),
4429 Reason => PE_Accessibility_Check_Failed));
4432 -- Do not apply Ada 2005 accessibility checks on a class-wide
4433 -- allocator if the type given in the allocator is a formal
4434 -- type. A run-time check will be performed in the instance.
4436 elsif not Is_Generic_Type (Exp_Typ) then
4437 Error_Msg_N ("type in allocator has deeper level than" &
4438 " designated class-wide type", E);
4444 -- Check for allocation from an empty storage pool
4446 if No_Pool_Assigned (Typ) then
4447 Error_Msg_N ("allocation from empty storage pool!", N);
4449 -- If the context is an unchecked conversion, as may happen within an
4450 -- inlined subprogram, the allocator is being resolved with its own
4451 -- anonymous type. In that case, if the target type has a specific
4452 -- storage pool, it must be inherited explicitly by the allocator type.
4454 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4455 and then No (Associated_Storage_Pool (Typ))
4457 Set_Associated_Storage_Pool
4458 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4461 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4462 Check_Restriction (No_Anonymous_Allocators, N);
4465 -- An erroneous allocator may be rewritten as a raise Program_Error
4468 if Nkind (N) = N_Allocator then
4470 -- An anonymous access discriminant is the definition of a
4473 if Ekind (Typ) = E_Anonymous_Access_Type
4474 and then Nkind (Associated_Node_For_Itype (Typ)) =
4475 N_Discriminant_Specification
4477 -- Avoid marking an allocator as a dynamic coextension if it is
4478 -- within a static construct.
4480 if not Is_Static_Coextension (N) then
4481 Set_Is_Dynamic_Coextension (N);
4484 -- Cleanup for potential static coextensions
4487 Set_Is_Dynamic_Coextension (N, False);
4488 Set_Is_Static_Coextension (N, False);
4491 -- There is no need to propagate any nested coextensions if they
4492 -- are marked as static since they will be rewritten on the spot.
4494 if not Is_Static_Coextension (N) then
4495 Propagate_Coextensions (N);
4498 end Resolve_Allocator;
4500 ---------------------------
4501 -- Resolve_Arithmetic_Op --
4502 ---------------------------
4504 -- Used for resolving all arithmetic operators except exponentiation
4506 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4507 L : constant Node_Id := Left_Opnd (N);
4508 R : constant Node_Id := Right_Opnd (N);
4509 TL : constant Entity_Id := Base_Type (Etype (L));
4510 TR : constant Entity_Id := Base_Type (Etype (R));
4514 B_Typ : constant Entity_Id := Base_Type (Typ);
4515 -- We do the resolution using the base type, because intermediate values
4516 -- in expressions always are of the base type, not a subtype of it.
4518 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4519 -- Returns True if N is in a context that expects "any real type"
4521 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4522 -- Return True iff given type is Integer or universal real/integer
4524 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4525 -- Choose type of integer literal in fixed-point operation to conform
4526 -- to available fixed-point type. T is the type of the other operand,
4527 -- which is needed to determine the expected type of N.
4529 procedure Set_Operand_Type (N : Node_Id);
4530 -- Set operand type to T if universal
4532 -------------------------------
4533 -- Expected_Type_Is_Any_Real --
4534 -------------------------------
4536 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4538 -- N is the expression after "delta" in a fixed_point_definition;
4541 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4542 N_Decimal_Fixed_Point_Definition,
4544 -- N is one of the bounds in a real_range_specification;
4547 N_Real_Range_Specification,
4549 -- N is the expression of a delta_constraint;
4552 N_Delta_Constraint);
4553 end Expected_Type_Is_Any_Real;
4555 -----------------------------
4556 -- Is_Integer_Or_Universal --
4557 -----------------------------
4559 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4561 Index : Interp_Index;
4565 if not Is_Overloaded (N) then
4567 return Base_Type (T) = Base_Type (Standard_Integer)
4568 or else T = Universal_Integer
4569 or else T = Universal_Real;
4571 Get_First_Interp (N, Index, It);
4572 while Present (It.Typ) loop
4573 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4574 or else It.Typ = Universal_Integer
4575 or else It.Typ = Universal_Real
4580 Get_Next_Interp (Index, It);
4585 end Is_Integer_Or_Universal;
4587 ----------------------------
4588 -- Set_Mixed_Mode_Operand --
4589 ----------------------------
4591 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4592 Index : Interp_Index;
4596 if Universal_Interpretation (N) = Universal_Integer then
4598 -- A universal integer literal is resolved as standard integer
4599 -- except in the case of a fixed-point result, where we leave it
4600 -- as universal (to be handled by Exp_Fixd later on)
4602 if Is_Fixed_Point_Type (T) then
4603 Resolve (N, Universal_Integer);
4605 Resolve (N, Standard_Integer);
4608 elsif Universal_Interpretation (N) = Universal_Real
4609 and then (T = Base_Type (Standard_Integer)
4610 or else T = Universal_Integer
4611 or else T = Universal_Real)
4613 -- A universal real can appear in a fixed-type context. We resolve
4614 -- the literal with that context, even though this might raise an
4615 -- exception prematurely (the other operand may be zero).
4619 elsif Etype (N) = Base_Type (Standard_Integer)
4620 and then T = Universal_Real
4621 and then Is_Overloaded (N)
4623 -- Integer arg in mixed-mode operation. Resolve with universal
4624 -- type, in case preference rule must be applied.
4626 Resolve (N, Universal_Integer);
4629 and then B_Typ /= Universal_Fixed
4631 -- Not a mixed-mode operation, resolve with context
4635 elsif Etype (N) = Any_Fixed then
4637 -- N may itself be a mixed-mode operation, so use context type
4641 elsif Is_Fixed_Point_Type (T)
4642 and then B_Typ = Universal_Fixed
4643 and then Is_Overloaded (N)
4645 -- Must be (fixed * fixed) operation, operand must have one
4646 -- compatible interpretation.
4648 Resolve (N, Any_Fixed);
4650 elsif Is_Fixed_Point_Type (B_Typ)
4651 and then (T = Universal_Real
4652 or else Is_Fixed_Point_Type (T))
4653 and then Is_Overloaded (N)
4655 -- C * F(X) in a fixed context, where C is a real literal or a
4656 -- fixed-point expression. F must have either a fixed type
4657 -- interpretation or an integer interpretation, but not both.
4659 Get_First_Interp (N, Index, It);
4660 while Present (It.Typ) loop
4661 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4662 if Analyzed (N) then
4663 Error_Msg_N ("ambiguous operand in fixed operation", N);
4665 Resolve (N, Standard_Integer);
4668 elsif Is_Fixed_Point_Type (It.Typ) then
4669 if Analyzed (N) then
4670 Error_Msg_N ("ambiguous operand in fixed operation", N);
4672 Resolve (N, It.Typ);
4676 Get_Next_Interp (Index, It);
4679 -- Reanalyze the literal with the fixed type of the context. If
4680 -- context is Universal_Fixed, we are within a conversion, leave
4681 -- the literal as a universal real because there is no usable
4682 -- fixed type, and the target of the conversion plays no role in
4696 if B_Typ = Universal_Fixed
4697 and then Nkind (Op2) = N_Real_Literal
4699 T2 := Universal_Real;
4704 Set_Analyzed (Op2, False);
4711 end Set_Mixed_Mode_Operand;
4713 ----------------------
4714 -- Set_Operand_Type --
4715 ----------------------
4717 procedure Set_Operand_Type (N : Node_Id) is
4719 if Etype (N) = Universal_Integer
4720 or else Etype (N) = Universal_Real
4724 end Set_Operand_Type;
4726 -- Start of processing for Resolve_Arithmetic_Op
4729 if Comes_From_Source (N)
4730 and then Ekind (Entity (N)) = E_Function
4731 and then Is_Imported (Entity (N))
4732 and then Is_Intrinsic_Subprogram (Entity (N))
4734 Resolve_Intrinsic_Operator (N, Typ);
4737 -- Special-case for mixed-mode universal expressions or fixed point type
4738 -- operation: each argument is resolved separately. The same treatment
4739 -- is required if one of the operands of a fixed point operation is
4740 -- universal real, since in this case we don't do a conversion to a
4741 -- specific fixed-point type (instead the expander handles the case).
4743 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4744 and then Present (Universal_Interpretation (L))
4745 and then Present (Universal_Interpretation (R))
4747 Resolve (L, Universal_Interpretation (L));
4748 Resolve (R, Universal_Interpretation (R));
4749 Set_Etype (N, B_Typ);
4751 elsif (B_Typ = Universal_Real
4752 or else Etype (N) = Universal_Fixed
4753 or else (Etype (N) = Any_Fixed
4754 and then Is_Fixed_Point_Type (B_Typ))
4755 or else (Is_Fixed_Point_Type (B_Typ)
4756 and then (Is_Integer_Or_Universal (L)
4758 Is_Integer_Or_Universal (R))))
4759 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4761 if TL = Universal_Integer or else TR = Universal_Integer then
4762 Check_For_Visible_Operator (N, B_Typ);
4765 -- If context is a fixed type and one operand is integer, the other
4766 -- is resolved with the type of the context.
4768 if Is_Fixed_Point_Type (B_Typ)
4769 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4770 or else TL = Universal_Integer)
4775 elsif Is_Fixed_Point_Type (B_Typ)
4776 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4777 or else TR = Universal_Integer)
4783 Set_Mixed_Mode_Operand (L, TR);
4784 Set_Mixed_Mode_Operand (R, TL);
4787 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4788 -- multiplying operators from being used when the expected type is
4789 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4790 -- some cases where the expected type is actually Any_Real;
4791 -- Expected_Type_Is_Any_Real takes care of that case.
4793 if Etype (N) = Universal_Fixed
4794 or else Etype (N) = Any_Fixed
4796 if B_Typ = Universal_Fixed
4797 and then not Expected_Type_Is_Any_Real (N)
4798 and then not Nkind_In (Parent (N), N_Type_Conversion,
4799 N_Unchecked_Type_Conversion)
4801 Error_Msg_N ("type cannot be determined from context!", N);
4802 Error_Msg_N ("\explicit conversion to result type required", N);
4804 Set_Etype (L, Any_Type);
4805 Set_Etype (R, Any_Type);
4808 if Ada_Version = Ada_83
4809 and then Etype (N) = Universal_Fixed
4811 Nkind_In (Parent (N), N_Type_Conversion,
4812 N_Unchecked_Type_Conversion)
4815 ("(Ada 83) fixed-point operation "
4816 & "needs explicit conversion", N);
4819 -- The expected type is "any real type" in contexts like
4821 -- type T is delta <universal_fixed-expression> ...
4823 -- in which case we need to set the type to Universal_Real
4824 -- so that static expression evaluation will work properly.
4826 if Expected_Type_Is_Any_Real (N) then
4827 Set_Etype (N, Universal_Real);
4829 Set_Etype (N, B_Typ);
4833 elsif Is_Fixed_Point_Type (B_Typ)
4834 and then (Is_Integer_Or_Universal (L)
4835 or else Nkind (L) = N_Real_Literal
4836 or else Nkind (R) = N_Real_Literal
4837 or else Is_Integer_Or_Universal (R))
4839 Set_Etype (N, B_Typ);
4841 elsif Etype (N) = Any_Fixed then
4843 -- If no previous errors, this is only possible if one operand is
4844 -- overloaded and the context is universal. Resolve as such.
4846 Set_Etype (N, B_Typ);
4850 if (TL = Universal_Integer or else TL = Universal_Real)
4852 (TR = Universal_Integer or else TR = Universal_Real)
4854 Check_For_Visible_Operator (N, B_Typ);
4857 -- If the context is Universal_Fixed and the operands are also
4858 -- universal fixed, this is an error, unless there is only one
4859 -- applicable fixed_point type (usually Duration).
4861 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4862 T := Unique_Fixed_Point_Type (N);
4864 if T = Any_Type then
4877 -- If one of the arguments was resolved to a non-universal type.
4878 -- label the result of the operation itself with the same type.
4879 -- Do the same for the universal argument, if any.
4881 T := Intersect_Types (L, R);
4882 Set_Etype (N, Base_Type (T));
4883 Set_Operand_Type (L);
4884 Set_Operand_Type (R);
4887 Generate_Operator_Reference (N, Typ);
4888 Eval_Arithmetic_Op (N);
4890 -- In SPARK, a multiplication or division with operands of fixed point
4891 -- types shall be qualified or explicitly converted to identify the
4894 if (Is_Fixed_Point_Type (Etype (L))
4895 or else Is_Fixed_Point_Type (Etype (R)))
4896 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4898 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4900 Check_SPARK_Restriction
4901 ("operation should be qualified or explicitly converted", N);
4904 -- Set overflow and division checking bit. Much cleverer code needed
4905 -- here eventually and perhaps the Resolve routines should be separated
4906 -- for the various arithmetic operations, since they will need
4907 -- different processing. ???
4909 if Nkind (N) in N_Op then
4910 if not Overflow_Checks_Suppressed (Etype (N)) then
4911 Enable_Overflow_Check (N);
4914 -- Give warning if explicit division by zero
4916 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4917 and then not Division_Checks_Suppressed (Etype (N))
4919 Rop := Right_Opnd (N);
4921 if Compile_Time_Known_Value (Rop)
4922 and then ((Is_Integer_Type (Etype (Rop))
4923 and then Expr_Value (Rop) = Uint_0)
4925 (Is_Real_Type (Etype (Rop))
4926 and then Expr_Value_R (Rop) = Ureal_0))
4928 -- Specialize the warning message according to the operation
4932 Apply_Compile_Time_Constraint_Error
4933 (N, "division by zero?", CE_Divide_By_Zero,
4934 Loc => Sloc (Right_Opnd (N)));
4937 Apply_Compile_Time_Constraint_Error
4938 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4939 Loc => Sloc (Right_Opnd (N)));
4942 Apply_Compile_Time_Constraint_Error
4943 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4944 Loc => Sloc (Right_Opnd (N)));
4946 -- Division by zero can only happen with division, rem,
4947 -- and mod operations.
4950 raise Program_Error;
4953 -- Otherwise just set the flag to check at run time
4956 Activate_Division_Check (N);
4960 -- If Restriction No_Implicit_Conditionals is active, then it is
4961 -- violated if either operand can be negative for mod, or for rem
4962 -- if both operands can be negative.
4964 if Restriction_Check_Required (No_Implicit_Conditionals)
4965 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4974 -- Set if corresponding operand might be negative
4978 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4979 LNeg := (not OK) or else Lo < 0;
4982 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4983 RNeg := (not OK) or else Lo < 0;
4985 -- Check if we will be generating conditionals. There are two
4986 -- cases where that can happen, first for REM, the only case
4987 -- is largest negative integer mod -1, where the division can
4988 -- overflow, but we still have to give the right result. The
4989 -- front end generates a test for this annoying case. Here we
4990 -- just test if both operands can be negative (that's what the
4991 -- expander does, so we match its logic here).
4993 -- The second case is mod where either operand can be negative.
4994 -- In this case, the back end has to generate additional tests.
4996 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4998 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5000 Check_Restriction (No_Implicit_Conditionals, N);
5006 Check_Unset_Reference (L);
5007 Check_Unset_Reference (R);
5008 end Resolve_Arithmetic_Op;
5014 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5015 Loc : constant Source_Ptr := Sloc (N);
5016 Subp : constant Node_Id := Name (N);
5024 function Same_Or_Aliased_Subprograms
5026 E : Entity_Id) return Boolean;
5027 -- Returns True if the subprogram entity S is the same as E or else
5028 -- S is an alias of E.
5030 ---------------------------------
5031 -- Same_Or_Aliased_Subprograms --
5032 ---------------------------------
5034 function Same_Or_Aliased_Subprograms
5036 E : Entity_Id) return Boolean
5038 Subp_Alias : constant Entity_Id := Alias (S);
5041 or else (Present (Subp_Alias) and then Subp_Alias = E);
5042 end Same_Or_Aliased_Subprograms;
5044 -- Start of processing for Resolve_Call
5047 -- The context imposes a unique interpretation with type Typ on a
5048 -- procedure or function call. Find the entity of the subprogram that
5049 -- yields the expected type, and propagate the corresponding formal
5050 -- constraints on the actuals. The caller has established that an
5051 -- interpretation exists, and emitted an error if not unique.
5053 -- First deal with the case of a call to an access-to-subprogram,
5054 -- dereference made explicit in Analyze_Call.
5056 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5057 if not Is_Overloaded (Subp) then
5058 Nam := Etype (Subp);
5061 -- Find the interpretation whose type (a subprogram type) has a
5062 -- return type that is compatible with the context. Analysis of
5063 -- the node has established that one exists.
5067 Get_First_Interp (Subp, I, It);
5068 while Present (It.Typ) loop
5069 if Covers (Typ, Etype (It.Typ)) then
5074 Get_Next_Interp (I, It);
5078 raise Program_Error;
5082 -- If the prefix is not an entity, then resolve it
5084 if not Is_Entity_Name (Subp) then
5085 Resolve (Subp, Nam);
5088 -- For an indirect call, we always invalidate checks, since we do not
5089 -- know whether the subprogram is local or global. Yes we could do
5090 -- better here, e.g. by knowing that there are no local subprograms,
5091 -- but it does not seem worth the effort. Similarly, we kill all
5092 -- knowledge of current constant values.
5094 Kill_Current_Values;
5096 -- If this is a procedure call which is really an entry call, do
5097 -- the conversion of the procedure call to an entry call. Protected
5098 -- operations use the same circuitry because the name in the call
5099 -- can be an arbitrary expression with special resolution rules.
5101 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5102 or else (Is_Entity_Name (Subp)
5103 and then Ekind (Entity (Subp)) = E_Entry)
5105 Resolve_Entry_Call (N, Typ);
5106 Check_Elab_Call (N);
5108 -- Kill checks and constant values, as above for indirect case
5109 -- Who knows what happens when another task is activated?
5111 Kill_Current_Values;
5114 -- Normal subprogram call with name established in Resolve
5116 elsif not (Is_Type (Entity (Subp))) then
5117 Nam := Entity (Subp);
5118 Set_Entity_With_Style_Check (Subp, Nam);
5120 -- Otherwise we must have the case of an overloaded call
5123 pragma Assert (Is_Overloaded (Subp));
5125 -- Initialize Nam to prevent warning (we know it will be assigned
5126 -- in the loop below, but the compiler does not know that).
5130 Get_First_Interp (Subp, I, It);
5131 while Present (It.Typ) loop
5132 if Covers (Typ, It.Typ) then
5134 Set_Entity_With_Style_Check (Subp, Nam);
5138 Get_Next_Interp (I, It);
5142 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5143 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5144 and then Nkind (Subp) /= N_Explicit_Dereference
5145 and then Present (Parameter_Associations (N))
5147 -- The prefix is a parameterless function call that returns an access
5148 -- to subprogram. If parameters are present in the current call, add
5149 -- add an explicit dereference. We use the base type here because
5150 -- within an instance these may be subtypes.
5152 -- The dereference is added either in Analyze_Call or here. Should
5153 -- be consolidated ???
5155 Set_Is_Overloaded (Subp, False);
5156 Set_Etype (Subp, Etype (Nam));
5157 Insert_Explicit_Dereference (Subp);
5158 Nam := Designated_Type (Etype (Nam));
5159 Resolve (Subp, Nam);
5162 -- Check that a call to Current_Task does not occur in an entry body
5164 if Is_RTE (Nam, RE_Current_Task) then
5173 -- Exclude calls that occur within the default of a formal
5174 -- parameter of the entry, since those are evaluated outside
5177 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5179 if Nkind (P) = N_Entry_Body
5180 or else (Nkind (P) = N_Subprogram_Body
5181 and then Is_Entry_Barrier_Function (P))
5185 ("?& should not be used in entry body (RM C.7(17))",
5188 ("\Program_Error will be raised at run time?", N, Nam);
5190 Make_Raise_Program_Error (Loc,
5191 Reason => PE_Current_Task_In_Entry_Body));
5192 Set_Etype (N, Rtype);
5199 -- Check that a procedure call does not occur in the context of the
5200 -- entry call statement of a conditional or timed entry call. Note that
5201 -- the case of a call to a subprogram renaming of an entry will also be
5202 -- rejected. The test for N not being an N_Entry_Call_Statement is
5203 -- defensive, covering the possibility that the processing of entry
5204 -- calls might reach this point due to later modifications of the code
5207 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5208 and then Nkind (N) /= N_Entry_Call_Statement
5209 and then Entry_Call_Statement (Parent (N)) = N
5211 if Ada_Version < Ada_2005 then
5212 Error_Msg_N ("entry call required in select statement", N);
5214 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5215 -- for a procedure_or_entry_call, the procedure_name or
5216 -- procedure_prefix of the procedure_call_statement shall denote
5217 -- an entry renamed by a procedure, or (a view of) a primitive
5218 -- subprogram of a limited interface whose first parameter is
5219 -- a controlling parameter.
5221 elsif Nkind (N) = N_Procedure_Call_Statement
5222 and then not Is_Renamed_Entry (Nam)
5223 and then not Is_Controlling_Limited_Procedure (Nam)
5226 ("entry call or dispatching primitive of interface required", N);
5230 -- Check that this is not a call to a protected procedure or entry from
5231 -- within a protected function.
5233 if Ekind (Current_Scope) = E_Function
5234 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5235 and then Ekind (Nam) /= E_Function
5236 and then Scope (Nam) = Scope (Current_Scope)
5238 Error_Msg_N ("within protected function, protected " &
5239 "object is constant", N);
5240 Error_Msg_N ("\cannot call operation that may modify it", N);
5243 -- Freeze the subprogram name if not in a spec-expression. Note that we
5244 -- freeze procedure calls as well as function calls. Procedure calls are
5245 -- not frozen according to the rules (RM 13.14(14)) because it is
5246 -- impossible to have a procedure call to a non-frozen procedure in pure
5247 -- Ada, but in the code that we generate in the expander, this rule
5248 -- needs extending because we can generate procedure calls that need
5251 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5252 Freeze_Expression (Subp);
5255 -- For a predefined operator, the type of the result is the type imposed
5256 -- by context, except for a predefined operation on universal fixed.
5257 -- Otherwise The type of the call is the type returned by the subprogram
5260 if Is_Predefined_Op (Nam) then
5261 if Etype (N) /= Universal_Fixed then
5265 -- If the subprogram returns an array type, and the context requires the
5266 -- component type of that array type, the node is really an indexing of
5267 -- the parameterless call. Resolve as such. A pathological case occurs
5268 -- when the type of the component is an access to the array type. In
5269 -- this case the call is truly ambiguous.
5271 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5273 ((Is_Array_Type (Etype (Nam))
5274 and then Covers (Typ, Component_Type (Etype (Nam))))
5275 or else (Is_Access_Type (Etype (Nam))
5276 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5280 Component_Type (Designated_Type (Etype (Nam))))))
5283 Index_Node : Node_Id;
5285 Ret_Type : constant Entity_Id := Etype (Nam);
5288 if Is_Access_Type (Ret_Type)
5289 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5292 ("cannot disambiguate function call and indexing", N);
5294 New_Subp := Relocate_Node (Subp);
5295 Set_Entity (Subp, Nam);
5297 if (Is_Array_Type (Ret_Type)
5298 and then Component_Type (Ret_Type) /= Any_Type)
5300 (Is_Access_Type (Ret_Type)
5302 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5304 if Needs_No_Actuals (Nam) then
5306 -- Indexed call to a parameterless function
5309 Make_Indexed_Component (Loc,
5311 Make_Function_Call (Loc,
5313 Expressions => Parameter_Associations (N));
5315 -- An Ada 2005 prefixed call to a primitive operation
5316 -- whose first parameter is the prefix. This prefix was
5317 -- prepended to the parameter list, which is actually a
5318 -- list of indexes. Remove the prefix in order to build
5319 -- the proper indexed component.
5322 Make_Indexed_Component (Loc,
5324 Make_Function_Call (Loc,
5326 Parameter_Associations =>
5328 (Remove_Head (Parameter_Associations (N)))),
5329 Expressions => Parameter_Associations (N));
5332 -- Preserve the parenthesis count of the node
5334 Set_Paren_Count (Index_Node, Paren_Count (N));
5336 -- Since we are correcting a node classification error made
5337 -- by the parser, we call Replace rather than Rewrite.
5339 Replace (N, Index_Node);
5341 Set_Etype (Prefix (N), Ret_Type);
5343 Resolve_Indexed_Component (N, Typ);
5344 Check_Elab_Call (Prefix (N));
5352 Set_Etype (N, Etype (Nam));
5355 -- In the case where the call is to an overloaded subprogram, Analyze
5356 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5357 -- such a case Normalize_Actuals needs to be called once more to order
5358 -- the actuals correctly. Otherwise the call will have the ordering
5359 -- given by the last overloaded subprogram whether this is the correct
5360 -- one being called or not.
5362 if Is_Overloaded (Subp) then
5363 Normalize_Actuals (N, Nam, False, Norm_OK);
5364 pragma Assert (Norm_OK);
5367 -- In any case, call is fully resolved now. Reset Overload flag, to
5368 -- prevent subsequent overload resolution if node is analyzed again
5370 Set_Is_Overloaded (Subp, False);
5371 Set_Is_Overloaded (N, False);
5373 -- If we are calling the current subprogram from immediately within its
5374 -- body, then that is the case where we can sometimes detect cases of
5375 -- infinite recursion statically. Do not try this in case restriction
5376 -- No_Recursion is in effect anyway, and do it only for source calls.
5378 if Comes_From_Source (N) then
5379 Scop := Current_Scope;
5381 -- Issue warning for possible infinite recursion in the absence
5382 -- of the No_Recursion restriction.
5384 if Same_Or_Aliased_Subprograms (Nam, Scop)
5385 and then not Restriction_Active (No_Recursion)
5386 and then Check_Infinite_Recursion (N)
5388 -- Here we detected and flagged an infinite recursion, so we do
5389 -- not need to test the case below for further warnings. Also we
5390 -- are all done if we now have a raise SE node.
5392 if Nkind (N) = N_Raise_Storage_Error then
5396 -- If call is to immediately containing subprogram, then check for
5397 -- the case of a possible run-time detectable infinite recursion.
5400 Scope_Loop : while Scop /= Standard_Standard loop
5401 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5403 -- Although in general case, recursion is not statically
5404 -- checkable, the case of calling an immediately containing
5405 -- subprogram is easy to catch.
5407 Check_Restriction (No_Recursion, N);
5409 -- If the recursive call is to a parameterless subprogram,
5410 -- then even if we can't statically detect infinite
5411 -- recursion, this is pretty suspicious, and we output a
5412 -- warning. Furthermore, we will try later to detect some
5413 -- cases here at run time by expanding checking code (see
5414 -- Detect_Infinite_Recursion in package Exp_Ch6).
5416 -- If the recursive call is within a handler, do not emit a
5417 -- warning, because this is a common idiom: loop until input
5418 -- is correct, catch illegal input in handler and restart.
5420 if No (First_Formal (Nam))
5421 and then Etype (Nam) = Standard_Void_Type
5422 and then not Error_Posted (N)
5423 and then Nkind (Parent (N)) /= N_Exception_Handler
5425 -- For the case of a procedure call. We give the message
5426 -- only if the call is the first statement in a sequence
5427 -- of statements, or if all previous statements are
5428 -- simple assignments. This is simply a heuristic to
5429 -- decrease false positives, without losing too many good
5430 -- warnings. The idea is that these previous statements
5431 -- may affect global variables the procedure depends on.
5433 if Nkind (N) = N_Procedure_Call_Statement
5434 and then Is_List_Member (N)
5440 while Present (P) loop
5441 if Nkind (P) /= N_Assignment_Statement then
5450 -- Do not give warning if we are in a conditional context
5453 K : constant Node_Kind := Nkind (Parent (N));
5455 if (K = N_Loop_Statement
5456 and then Present (Iteration_Scheme (Parent (N))))
5457 or else K = N_If_Statement
5458 or else K = N_Elsif_Part
5459 or else K = N_Case_Statement_Alternative
5465 -- Here warning is to be issued
5467 Set_Has_Recursive_Call (Nam);
5469 ("?possible infinite recursion!", N);
5471 ("\?Storage_Error may be raised at run time!", N);
5477 Scop := Scope (Scop);
5478 end loop Scope_Loop;
5482 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5484 Check_Obsolescent_2005_Entity (Nam, Subp);
5486 -- If subprogram name is a predefined operator, it was given in
5487 -- functional notation. Replace call node with operator node, so
5488 -- that actuals can be resolved appropriately.
5490 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5491 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5494 elsif Present (Alias (Nam))
5495 and then Is_Predefined_Op (Alias (Nam))
5497 Resolve_Actuals (N, Nam);
5498 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5502 -- Create a transient scope if the resulting type requires it
5504 -- There are several notable exceptions:
5506 -- a) In init procs, the transient scope overhead is not needed, and is
5507 -- even incorrect when the call is a nested initialization call for a
5508 -- component whose expansion may generate adjust calls. However, if the
5509 -- call is some other procedure call within an initialization procedure
5510 -- (for example a call to Create_Task in the init_proc of the task
5511 -- run-time record) a transient scope must be created around this call.
5513 -- b) Enumeration literal pseudo-calls need no transient scope
5515 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5516 -- functions) do not use the secondary stack even though the return
5517 -- type may be unconstrained.
5519 -- d) Calls to a build-in-place function, since such functions may
5520 -- allocate their result directly in a target object, and cases where
5521 -- the result does get allocated in the secondary stack are checked for
5522 -- within the specialized Exp_Ch6 procedures for expanding those
5523 -- build-in-place calls.
5525 -- e) If the subprogram is marked Inline_Always, then even if it returns
5526 -- an unconstrained type the call does not require use of the secondary
5527 -- stack. However, inlining will only take place if the body to inline
5528 -- is already present. It may not be available if e.g. the subprogram is
5529 -- declared in a child instance.
5531 -- If this is an initialization call for a type whose construction
5532 -- uses the secondary stack, and it is not a nested call to initialize
5533 -- a component, we do need to create a transient scope for it. We
5534 -- check for this by traversing the type in Check_Initialization_Call.
5537 and then Has_Pragma_Inline_Always (Nam)
5538 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5539 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5543 elsif Ekind (Nam) = E_Enumeration_Literal
5544 or else Is_Build_In_Place_Function (Nam)
5545 or else Is_Intrinsic_Subprogram (Nam)
5549 elsif Expander_Active
5550 and then Is_Type (Etype (Nam))
5551 and then Requires_Transient_Scope (Etype (Nam))
5553 (not Within_Init_Proc
5555 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5557 Establish_Transient_Scope (N, Sec_Stack => True);
5559 -- If the call appears within the bounds of a loop, it will
5560 -- be rewritten and reanalyzed, nothing left to do here.
5562 if Nkind (N) /= N_Function_Call then
5566 elsif Is_Init_Proc (Nam)
5567 and then not Within_Init_Proc
5569 Check_Initialization_Call (N, Nam);
5572 -- A protected function cannot be called within the definition of the
5573 -- enclosing protected type.
5575 if Is_Protected_Type (Scope (Nam))
5576 and then In_Open_Scopes (Scope (Nam))
5577 and then not Has_Completion (Scope (Nam))
5580 ("& cannot be called before end of protected definition", N, Nam);
5583 -- Propagate interpretation to actuals, and add default expressions
5586 if Present (First_Formal (Nam)) then
5587 Resolve_Actuals (N, Nam);
5589 -- Overloaded literals are rewritten as function calls, for purpose of
5590 -- resolution. After resolution, we can replace the call with the
5593 elsif Ekind (Nam) = E_Enumeration_Literal then
5594 Copy_Node (Subp, N);
5595 Resolve_Entity_Name (N, Typ);
5597 -- Avoid validation, since it is a static function call
5599 Generate_Reference (Nam, Subp);
5603 -- If the subprogram is not global, then kill all saved values and
5604 -- checks. This is a bit conservative, since in many cases we could do
5605 -- better, but it is not worth the effort. Similarly, we kill constant
5606 -- values. However we do not need to do this for internal entities
5607 -- (unless they are inherited user-defined subprograms), since they
5608 -- are not in the business of molesting local values.
5610 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5611 -- kill all checks and values for calls to global subprograms. This
5612 -- takes care of the case where an access to a local subprogram is
5613 -- taken, and could be passed directly or indirectly and then called
5614 -- from almost any context.
5616 -- Note: we do not do this step till after resolving the actuals. That
5617 -- way we still take advantage of the current value information while
5618 -- scanning the actuals.
5620 -- We suppress killing values if we are processing the nodes associated
5621 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5622 -- type kills all the values as part of analyzing the code that
5623 -- initializes the dispatch tables.
5625 if Inside_Freezing_Actions = 0
5626 and then (not Is_Library_Level_Entity (Nam)
5627 or else Suppress_Value_Tracking_On_Call
5628 (Nearest_Dynamic_Scope (Current_Scope)))
5629 and then (Comes_From_Source (Nam)
5630 or else (Present (Alias (Nam))
5631 and then Comes_From_Source (Alias (Nam))))
5633 Kill_Current_Values;
5636 -- If we are warning about unread OUT parameters, this is the place to
5637 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5638 -- after the above call to Kill_Current_Values (since that call clears
5639 -- the Last_Assignment field of all local variables).
5641 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5642 and then Comes_From_Source (N)
5643 and then In_Extended_Main_Source_Unit (N)
5650 F := First_Formal (Nam);
5651 A := First_Actual (N);
5652 while Present (F) and then Present (A) loop
5653 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5654 and then Warn_On_Modified_As_Out_Parameter (F)
5655 and then Is_Entity_Name (A)
5656 and then Present (Entity (A))
5657 and then Comes_From_Source (N)
5658 and then Safe_To_Capture_Value (N, Entity (A))
5660 Set_Last_Assignment (Entity (A), A);
5669 -- If the subprogram is a primitive operation, check whether or not
5670 -- it is a correct dispatching call.
5672 if Is_Overloadable (Nam)
5673 and then Is_Dispatching_Operation (Nam)
5675 Check_Dispatching_Call (N);
5677 elsif Ekind (Nam) /= E_Subprogram_Type
5678 and then Is_Abstract_Subprogram (Nam)
5679 and then not In_Instance
5681 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5684 -- If this is a dispatching call, generate the appropriate reference,
5685 -- for better source navigation in GPS.
5687 if Is_Overloadable (Nam)
5688 and then Present (Controlling_Argument (N))
5690 Generate_Reference (Nam, Subp, 'R');
5692 -- Normal case, not a dispatching call: generate a call reference
5695 Generate_Reference (Nam, Subp, 's');
5698 if Is_Intrinsic_Subprogram (Nam) then
5699 Check_Intrinsic_Call (N);
5702 -- Check for violation of restriction No_Specific_Termination_Handlers
5703 -- and warn on a potentially blocking call to Abort_Task.
5705 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5706 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5708 Is_RTE (Nam, RE_Specific_Handler))
5710 Check_Restriction (No_Specific_Termination_Handlers, N);
5712 elsif Is_RTE (Nam, RE_Abort_Task) then
5713 Check_Potentially_Blocking_Operation (N);
5716 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5717 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5718 -- need to check the second argument to determine whether it is an
5719 -- absolute or relative timing event.
5721 if Restriction_Check_Required (No_Relative_Delay)
5722 and then Is_RTE (Nam, RE_Set_Handler)
5723 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5725 Check_Restriction (No_Relative_Delay, N);
5728 -- Issue an error for a call to an eliminated subprogram. We skip this
5729 -- in a spec expression, e.g. a call in a default parameter value, since
5730 -- we are not really doing a call at this time. That's important because
5731 -- the spec expression may itself belong to an eliminated subprogram.
5733 if not In_Spec_Expression then
5734 Check_For_Eliminated_Subprogram (Subp, Nam);
5737 -- In formal mode, the primitive operations of a tagged type or type
5738 -- extension do not include functions that return the tagged type.
5740 -- Commented out as the call to Is_Inherited_Operation_For_Type may
5741 -- cause an error because the type entity of the parent node of
5742 -- Entity (Name (N) may not be set. ???
5743 -- So why not just add a guard ???
5745 -- if Nkind (N) = N_Function_Call
5746 -- and then Is_Tagged_Type (Etype (N))
5747 -- and then Is_Entity_Name (Name (N))
5748 -- and then Is_Inherited_Operation_For_Type
5749 -- (Entity (Name (N)), Etype (N))
5751 -- Check_Formal_Restriction ("function not inherited", N);
5754 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5755 -- class-wide and the call dispatches on result in a context that does
5756 -- not provide a tag, the call raises Program_Error.
5758 if Nkind (N) = N_Function_Call
5759 and then In_Instance
5760 and then Is_Generic_Actual_Type (Typ)
5761 and then Is_Class_Wide_Type (Typ)
5762 and then Has_Controlling_Result (Nam)
5763 and then Nkind (Parent (N)) = N_Object_Declaration
5765 -- Verify that none of the formals are controlling
5768 Call_OK : Boolean := False;
5772 F := First_Formal (Nam);
5773 while Present (F) loop
5774 if Is_Controlling_Formal (F) then
5783 Error_Msg_N ("!? cannot determine tag of result", N);
5784 Error_Msg_N ("!? Program_Error will be raised", N);
5786 Make_Raise_Program_Error (Sloc (N),
5787 Reason => PE_Explicit_Raise));
5792 -- All done, evaluate call and deal with elaboration issues
5795 Check_Elab_Call (N);
5796 Warn_On_Overlapping_Actuals (Nam, N);
5799 -----------------------------
5800 -- Resolve_Case_Expression --
5801 -----------------------------
5803 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5807 Alt := First (Alternatives (N));
5808 while Present (Alt) loop
5809 Resolve (Expression (Alt), Typ);
5814 Eval_Case_Expression (N);
5815 end Resolve_Case_Expression;
5817 -------------------------------
5818 -- Resolve_Character_Literal --
5819 -------------------------------
5821 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5822 B_Typ : constant Entity_Id := Base_Type (Typ);
5826 -- Verify that the character does belong to the type of the context
5828 Set_Etype (N, B_Typ);
5829 Eval_Character_Literal (N);
5831 -- Wide_Wide_Character literals must always be defined, since the set
5832 -- of wide wide character literals is complete, i.e. if a character
5833 -- literal is accepted by the parser, then it is OK for wide wide
5834 -- character (out of range character literals are rejected).
5836 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5839 -- Always accept character literal for type Any_Character, which
5840 -- occurs in error situations and in comparisons of literals, both
5841 -- of which should accept all literals.
5843 elsif B_Typ = Any_Character then
5846 -- For Standard.Character or a type derived from it, check that the
5847 -- literal is in range.
5849 elsif Root_Type (B_Typ) = Standard_Character then
5850 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5854 -- For Standard.Wide_Character or a type derived from it, check that the
5855 -- literal is in range.
5857 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5858 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5862 -- For Standard.Wide_Wide_Character or a type derived from it, we
5863 -- know the literal is in range, since the parser checked!
5865 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5868 -- If the entity is already set, this has already been resolved in a
5869 -- generic context, or comes from expansion. Nothing else to do.
5871 elsif Present (Entity (N)) then
5874 -- Otherwise we have a user defined character type, and we can use the
5875 -- standard visibility mechanisms to locate the referenced entity.
5878 C := Current_Entity (N);
5879 while Present (C) loop
5880 if Etype (C) = B_Typ then
5881 Set_Entity_With_Style_Check (N, C);
5882 Generate_Reference (C, N);
5890 -- If we fall through, then the literal does not match any of the
5891 -- entries of the enumeration type. This isn't just a constraint error
5892 -- situation, it is an illegality (see RM 4.2).
5895 ("character not defined for }", N, First_Subtype (B_Typ));
5896 end Resolve_Character_Literal;
5898 ---------------------------
5899 -- Resolve_Comparison_Op --
5900 ---------------------------
5902 -- Context requires a boolean type, and plays no role in resolution.
5903 -- Processing identical to that for equality operators. The result type is
5904 -- the base type, which matters when pathological subtypes of booleans with
5905 -- limited ranges are used.
5907 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5908 L : constant Node_Id := Left_Opnd (N);
5909 R : constant Node_Id := Right_Opnd (N);
5913 -- If this is an intrinsic operation which is not predefined, use the
5914 -- types of its declared arguments to resolve the possibly overloaded
5915 -- operands. Otherwise the operands are unambiguous and specify the
5918 if Scope (Entity (N)) /= Standard_Standard then
5919 T := Etype (First_Entity (Entity (N)));
5922 T := Find_Unique_Type (L, R);
5924 if T = Any_Fixed then
5925 T := Unique_Fixed_Point_Type (L);
5929 Set_Etype (N, Base_Type (Typ));
5930 Generate_Reference (T, N, ' ');
5932 -- Skip remaining processing if already set to Any_Type
5934 if T = Any_Type then
5938 -- Deal with other error cases
5940 if T = Any_String or else
5941 T = Any_Composite or else
5944 if T = Any_Character then
5945 Ambiguous_Character (L);
5947 Error_Msg_N ("ambiguous operands for comparison", N);
5950 Set_Etype (N, Any_Type);
5954 -- Resolve the operands if types OK
5958 Check_Unset_Reference (L);
5959 Check_Unset_Reference (R);
5960 Generate_Operator_Reference (N, T);
5961 Check_Low_Bound_Tested (N);
5963 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
5964 -- types or array types except String.
5966 if Is_Boolean_Type (T) then
5967 Current_Subprogram_Body_Is_Not_In_ALFA;
5968 Check_SPARK_Restriction
5969 ("comparison is not defined on Boolean type", N);
5971 elsif Is_Array_Type (T) then
5972 Current_Subprogram_Body_Is_Not_In_ALFA;
5974 if Base_Type (T) /= Standard_String then
5975 Check_SPARK_Restriction
5976 ("comparison is not defined on array types other than String",
5984 -- Check comparison on unordered enumeration
5986 if Comes_From_Source (N)
5987 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
5989 Error_Msg_N ("comparison on unordered enumeration type?", N);
5992 -- Evaluate the relation (note we do this after the above check since
5993 -- this Eval call may change N to True/False.
5995 Eval_Relational_Op (N);
5996 end Resolve_Comparison_Op;
5998 ------------------------------------
5999 -- Resolve_Conditional_Expression --
6000 ------------------------------------
6002 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
6003 Condition : constant Node_Id := First (Expressions (N));
6004 Then_Expr : constant Node_Id := Next (Condition);
6005 Else_Expr : Node_Id := Next (Then_Expr);
6008 Resolve (Condition, Any_Boolean);
6009 Resolve (Then_Expr, Typ);
6011 -- If ELSE expression present, just resolve using the determined type
6013 if Present (Else_Expr) then
6014 Resolve (Else_Expr, Typ);
6016 -- If no ELSE expression is present, root type must be Standard.Boolean
6017 -- and we provide a Standard.True result converted to the appropriate
6018 -- Boolean type (in case it is a derived boolean type).
6020 elsif Root_Type (Typ) = Standard_Boolean then
6022 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
6023 Analyze_And_Resolve (Else_Expr, Typ);
6024 Append_To (Expressions (N), Else_Expr);
6027 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
6028 Append_To (Expressions (N), Error);
6032 Eval_Conditional_Expression (N);
6033 end Resolve_Conditional_Expression;
6035 -----------------------------------------
6036 -- Resolve_Discrete_Subtype_Indication --
6037 -----------------------------------------
6039 procedure Resolve_Discrete_Subtype_Indication
6047 Analyze (Subtype_Mark (N));
6048 S := Entity (Subtype_Mark (N));
6050 if Nkind (Constraint (N)) /= N_Range_Constraint then
6051 Error_Msg_N ("expect range constraint for discrete type", N);
6052 Set_Etype (N, Any_Type);
6055 R := Range_Expression (Constraint (N));
6063 if Base_Type (S) /= Base_Type (Typ) then
6065 ("expect subtype of }", N, First_Subtype (Typ));
6067 -- Rewrite the constraint as a range of Typ
6068 -- to allow compilation to proceed further.
6071 Rewrite (Low_Bound (R),
6072 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6073 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6074 Attribute_Name => Name_First));
6075 Rewrite (High_Bound (R),
6076 Make_Attribute_Reference (Sloc (High_Bound (R)),
6077 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6078 Attribute_Name => Name_First));
6082 Set_Etype (N, Etype (R));
6084 -- Additionally, we must check that the bounds are compatible
6085 -- with the given subtype, which might be different from the
6086 -- type of the context.
6088 Apply_Range_Check (R, S);
6090 -- ??? If the above check statically detects a Constraint_Error
6091 -- it replaces the offending bound(s) of the range R with a
6092 -- Constraint_Error node. When the itype which uses these bounds
6093 -- is frozen the resulting call to Duplicate_Subexpr generates
6094 -- a new temporary for the bounds.
6096 -- Unfortunately there are other itypes that are also made depend
6097 -- on these bounds, so when Duplicate_Subexpr is called they get
6098 -- a forward reference to the newly created temporaries and Gigi
6099 -- aborts on such forward references. This is probably sign of a
6100 -- more fundamental problem somewhere else in either the order of
6101 -- itype freezing or the way certain itypes are constructed.
6103 -- To get around this problem we call Remove_Side_Effects right
6104 -- away if either bounds of R are a Constraint_Error.
6107 L : constant Node_Id := Low_Bound (R);
6108 H : constant Node_Id := High_Bound (R);
6111 if Nkind (L) = N_Raise_Constraint_Error then
6112 Remove_Side_Effects (L);
6115 if Nkind (H) = N_Raise_Constraint_Error then
6116 Remove_Side_Effects (H);
6120 Check_Unset_Reference (Low_Bound (R));
6121 Check_Unset_Reference (High_Bound (R));
6124 end Resolve_Discrete_Subtype_Indication;
6126 -------------------------
6127 -- Resolve_Entity_Name --
6128 -------------------------
6130 -- Used to resolve identifiers and expanded names
6132 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6133 E : constant Entity_Id := Entity (N);
6136 -- If garbage from errors, set to Any_Type and return
6138 if No (E) and then Total_Errors_Detected /= 0 then
6139 Set_Etype (N, Any_Type);
6143 -- Replace named numbers by corresponding literals. Note that this is
6144 -- the one case where Resolve_Entity_Name must reset the Etype, since
6145 -- it is currently marked as universal.
6147 if Ekind (E) = E_Named_Integer then
6149 Eval_Named_Integer (N);
6151 elsif Ekind (E) = E_Named_Real then
6153 Eval_Named_Real (N);
6155 -- For enumeration literals, we need to make sure that a proper style
6156 -- check is done, since such literals are overloaded, and thus we did
6157 -- not do a style check during the first phase of analysis.
6159 elsif Ekind (E) = E_Enumeration_Literal then
6160 Set_Entity_With_Style_Check (N, E);
6161 Eval_Entity_Name (N);
6163 -- Case of subtype name appearing as an operand in expression
6165 elsif Is_Type (E) then
6167 -- Allow use of subtype if it is a concurrent type where we are
6168 -- currently inside the body. This will eventually be expanded into a
6169 -- call to Self (for tasks) or _object (for protected objects). Any
6170 -- other use of a subtype is invalid.
6172 if Is_Concurrent_Type (E)
6173 and then In_Open_Scopes (E)
6177 -- Any other use is an error
6181 ("invalid use of subtype mark in expression or call", N);
6184 -- Check discriminant use if entity is discriminant in current scope,
6185 -- i.e. discriminant of record or concurrent type currently being
6186 -- analyzed. Uses in corresponding body are unrestricted.
6188 elsif Ekind (E) = E_Discriminant
6189 and then Scope (E) = Current_Scope
6190 and then not Has_Completion (Current_Scope)
6192 Check_Discriminant_Use (N);
6194 -- A parameterless generic function cannot appear in a context that
6195 -- requires resolution.
6197 elsif Ekind (E) = E_Generic_Function then
6198 Error_Msg_N ("illegal use of generic function", N);
6200 elsif Ekind (E) = E_Out_Parameter
6201 and then Ada_Version = Ada_83
6202 and then (Nkind (Parent (N)) in N_Op
6203 or else (Nkind (Parent (N)) = N_Assignment_Statement
6204 and then N = Expression (Parent (N)))
6205 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6207 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6209 -- In all other cases, just do the possible static evaluation
6212 -- A deferred constant that appears in an expression must have a
6213 -- completion, unless it has been removed by in-place expansion of
6216 if Ekind (E) = E_Constant
6217 and then Comes_From_Source (E)
6218 and then No (Constant_Value (E))
6219 and then Is_Frozen (Etype (E))
6220 and then not In_Spec_Expression
6221 and then not Is_Imported (E)
6223 if No_Initialization (Parent (E))
6224 or else (Present (Full_View (E))
6225 and then No_Initialization (Parent (Full_View (E))))
6230 "deferred constant is frozen before completion", N);
6234 Eval_Entity_Name (N);
6236 end Resolve_Entity_Name;
6242 procedure Resolve_Entry (Entry_Name : Node_Id) is
6243 Loc : constant Source_Ptr := Sloc (Entry_Name);
6251 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6252 -- If the bounds of the entry family being called depend on task
6253 -- discriminants, build a new index subtype where a discriminant is
6254 -- replaced with the value of the discriminant of the target task.
6255 -- The target task is the prefix of the entry name in the call.
6257 -----------------------
6258 -- Actual_Index_Type --
6259 -----------------------
6261 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6262 Typ : constant Entity_Id := Entry_Index_Type (E);
6263 Tsk : constant Entity_Id := Scope (E);
6264 Lo : constant Node_Id := Type_Low_Bound (Typ);
6265 Hi : constant Node_Id := Type_High_Bound (Typ);
6268 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6269 -- If the bound is given by a discriminant, replace with a reference
6270 -- to the discriminant of the same name in the target task. If the
6271 -- entry name is the target of a requeue statement and the entry is
6272 -- in the current protected object, the bound to be used is the
6273 -- discriminal of the object (see Apply_Range_Checks for details of
6274 -- the transformation).
6276 -----------------------------
6277 -- Actual_Discriminant_Ref --
6278 -----------------------------
6280 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6281 Typ : constant Entity_Id := Etype (Bound);
6285 Remove_Side_Effects (Bound);
6287 if not Is_Entity_Name (Bound)
6288 or else Ekind (Entity (Bound)) /= E_Discriminant
6292 elsif Is_Protected_Type (Tsk)
6293 and then In_Open_Scopes (Tsk)
6294 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6296 -- Note: here Bound denotes a discriminant of the corresponding
6297 -- record type tskV, whose discriminal is a formal of the
6298 -- init-proc tskVIP. What we want is the body discriminal,
6299 -- which is associated to the discriminant of the original
6300 -- concurrent type tsk.
6302 return New_Occurrence_Of
6303 (Find_Body_Discriminal (Entity (Bound)), Loc);
6307 Make_Selected_Component (Loc,
6308 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6309 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6314 end Actual_Discriminant_Ref;
6316 -- Start of processing for Actual_Index_Type
6319 if not Has_Discriminants (Tsk)
6320 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6322 return Entry_Index_Type (E);
6325 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6326 Set_Etype (New_T, Base_Type (Typ));
6327 Set_Size_Info (New_T, Typ);
6328 Set_RM_Size (New_T, RM_Size (Typ));
6329 Set_Scalar_Range (New_T,
6330 Make_Range (Sloc (Entry_Name),
6331 Low_Bound => Actual_Discriminant_Ref (Lo),
6332 High_Bound => Actual_Discriminant_Ref (Hi)));
6336 end Actual_Index_Type;
6338 -- Start of processing of Resolve_Entry
6341 -- Find name of entry being called, and resolve prefix of name with its
6342 -- own type. The prefix can be overloaded, and the name and signature of
6343 -- the entry must be taken into account.
6345 if Nkind (Entry_Name) = N_Indexed_Component then
6347 -- Case of dealing with entry family within the current tasks
6349 E_Name := Prefix (Entry_Name);
6352 E_Name := Entry_Name;
6355 if Is_Entity_Name (E_Name) then
6357 -- Entry call to an entry (or entry family) in the current task. This
6358 -- is legal even though the task will deadlock. Rewrite as call to
6361 -- This can also be a call to an entry in an enclosing task. If this
6362 -- is a single task, we have to retrieve its name, because the scope
6363 -- of the entry is the task type, not the object. If the enclosing
6364 -- task is a task type, the identity of the task is given by its own
6367 -- Finally this can be a requeue on an entry of the same task or
6368 -- protected object.
6370 S := Scope (Entity (E_Name));
6372 for J in reverse 0 .. Scope_Stack.Last loop
6373 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6374 and then not Comes_From_Source (S)
6376 -- S is an enclosing task or protected object. The concurrent
6377 -- declaration has been converted into a type declaration, and
6378 -- the object itself has an object declaration that follows
6379 -- the type in the same declarative part.
6381 Tsk := Next_Entity (S);
6382 while Etype (Tsk) /= S loop
6389 elsif S = Scope_Stack.Table (J).Entity then
6391 -- Call to current task. Will be transformed into call to Self
6399 Make_Selected_Component (Loc,
6400 Prefix => New_Occurrence_Of (S, Loc),
6402 New_Occurrence_Of (Entity (E_Name), Loc));
6403 Rewrite (E_Name, New_N);
6406 elsif Nkind (Entry_Name) = N_Selected_Component
6407 and then Is_Overloaded (Prefix (Entry_Name))
6409 -- Use the entry name (which must be unique at this point) to find
6410 -- the prefix that returns the corresponding task/protected type.
6413 Pref : constant Node_Id := Prefix (Entry_Name);
6414 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6419 Get_First_Interp (Pref, I, It);
6420 while Present (It.Typ) loop
6421 if Scope (Ent) = It.Typ then
6422 Set_Etype (Pref, It.Typ);
6426 Get_Next_Interp (I, It);
6431 if Nkind (Entry_Name) = N_Selected_Component then
6432 Resolve (Prefix (Entry_Name));
6434 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6435 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6436 Resolve (Prefix (Prefix (Entry_Name)));
6437 Index := First (Expressions (Entry_Name));
6438 Resolve (Index, Entry_Index_Type (Nam));
6440 -- Up to this point the expression could have been the actual in a
6441 -- simple entry call, and be given by a named association.
6443 if Nkind (Index) = N_Parameter_Association then
6444 Error_Msg_N ("expect expression for entry index", Index);
6446 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6451 ------------------------
6452 -- Resolve_Entry_Call --
6453 ------------------------
6455 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6456 Entry_Name : constant Node_Id := Name (N);
6457 Loc : constant Source_Ptr := Sloc (Entry_Name);
6459 First_Named : Node_Id;
6466 -- We kill all checks here, because it does not seem worth the effort to
6467 -- do anything better, an entry call is a big operation.
6471 -- Processing of the name is similar for entry calls and protected
6472 -- operation calls. Once the entity is determined, we can complete
6473 -- the resolution of the actuals.
6475 -- The selector may be overloaded, in the case of a protected object
6476 -- with overloaded functions. The type of the context is used for
6479 if Nkind (Entry_Name) = N_Selected_Component
6480 and then Is_Overloaded (Selector_Name (Entry_Name))
6481 and then Typ /= Standard_Void_Type
6488 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6489 while Present (It.Typ) loop
6490 if Covers (Typ, It.Typ) then
6491 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6492 Set_Etype (Entry_Name, It.Typ);
6494 Generate_Reference (It.Typ, N, ' ');
6497 Get_Next_Interp (I, It);
6502 Resolve_Entry (Entry_Name);
6504 if Nkind (Entry_Name) = N_Selected_Component then
6506 -- Simple entry call
6508 Nam := Entity (Selector_Name (Entry_Name));
6509 Obj := Prefix (Entry_Name);
6510 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6512 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6514 -- Call to member of entry family
6516 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6517 Obj := Prefix (Prefix (Entry_Name));
6518 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6521 -- We cannot in general check the maximum depth of protected entry calls
6522 -- at compile time. But we can tell that any protected entry call at all
6523 -- violates a specified nesting depth of zero.
6525 if Is_Protected_Type (Scope (Nam)) then
6526 Check_Restriction (Max_Entry_Queue_Length, N);
6529 -- Use context type to disambiguate a protected function that can be
6530 -- called without actuals and that returns an array type, and where the
6531 -- argument list may be an indexing of the returned value.
6533 if Ekind (Nam) = E_Function
6534 and then Needs_No_Actuals (Nam)
6535 and then Present (Parameter_Associations (N))
6537 ((Is_Array_Type (Etype (Nam))
6538 and then Covers (Typ, Component_Type (Etype (Nam))))
6540 or else (Is_Access_Type (Etype (Nam))
6541 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6545 Component_Type (Designated_Type (Etype (Nam))))))
6548 Index_Node : Node_Id;
6552 Make_Indexed_Component (Loc,
6554 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6555 Expressions => Parameter_Associations (N));
6557 -- Since we are correcting a node classification error made by the
6558 -- parser, we call Replace rather than Rewrite.
6560 Replace (N, Index_Node);
6561 Set_Etype (Prefix (N), Etype (Nam));
6563 Resolve_Indexed_Component (N, Typ);
6568 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6569 and then Present (PPC_Wrapper (Nam))
6570 and then Current_Scope /= PPC_Wrapper (Nam)
6572 -- Rewrite as call to the precondition wrapper, adding the task
6573 -- object to the list of actuals. If the call is to a member of an
6574 -- entry family, include the index as well.
6578 New_Actuals : List_Id;
6581 New_Actuals := New_List (Obj);
6583 if Nkind (Entry_Name) = N_Indexed_Component then
6584 Append_To (New_Actuals,
6585 New_Copy_Tree (First (Expressions (Entry_Name))));
6588 Append_List (Parameter_Associations (N), New_Actuals);
6590 Make_Procedure_Call_Statement (Loc,
6592 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6593 Parameter_Associations => New_Actuals);
6594 Rewrite (N, New_Call);
6595 Analyze_And_Resolve (N);
6600 -- The operation name may have been overloaded. Order the actuals
6601 -- according to the formals of the resolved entity, and set the return
6602 -- type to that of the operation.
6605 Normalize_Actuals (N, Nam, False, Norm_OK);
6606 pragma Assert (Norm_OK);
6607 Set_Etype (N, Etype (Nam));
6610 Resolve_Actuals (N, Nam);
6612 -- Create a call reference to the entry
6614 Generate_Reference (Nam, Entry_Name, 's');
6616 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6617 Check_Potentially_Blocking_Operation (N);
6620 -- Verify that a procedure call cannot masquerade as an entry
6621 -- call where an entry call is expected.
6623 if Ekind (Nam) = E_Procedure then
6624 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6625 and then N = Entry_Call_Statement (Parent (N))
6627 Error_Msg_N ("entry call required in select statement", N);
6629 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6630 and then N = Triggering_Statement (Parent (N))
6632 Error_Msg_N ("triggering statement cannot be procedure call", N);
6634 elsif Ekind (Scope (Nam)) = E_Task_Type
6635 and then not In_Open_Scopes (Scope (Nam))
6637 Error_Msg_N ("task has no entry with this name", Entry_Name);
6641 -- After resolution, entry calls and protected procedure calls are
6642 -- changed into entry calls, for expansion. The structure of the node
6643 -- does not change, so it can safely be done in place. Protected
6644 -- function calls must keep their structure because they are
6647 if Ekind (Nam) /= E_Function then
6649 -- A protected operation that is not a function may modify the
6650 -- corresponding object, and cannot apply to a constant. If this
6651 -- is an internal call, the prefix is the type itself.
6653 if Is_Protected_Type (Scope (Nam))
6654 and then not Is_Variable (Obj)
6655 and then (not Is_Entity_Name (Obj)
6656 or else not Is_Type (Entity (Obj)))
6659 ("prefix of protected procedure or entry call must be variable",
6663 Actuals := Parameter_Associations (N);
6664 First_Named := First_Named_Actual (N);
6667 Make_Entry_Call_Statement (Loc,
6669 Parameter_Associations => Actuals));
6671 Set_First_Named_Actual (N, First_Named);
6672 Set_Analyzed (N, True);
6674 -- Protected functions can return on the secondary stack, in which
6675 -- case we must trigger the transient scope mechanism.
6677 elsif Expander_Active
6678 and then Requires_Transient_Scope (Etype (Nam))
6680 Establish_Transient_Scope (N, Sec_Stack => True);
6682 end Resolve_Entry_Call;
6684 -------------------------
6685 -- Resolve_Equality_Op --
6686 -------------------------
6688 -- Both arguments must have the same type, and the boolean context does
6689 -- not participate in the resolution. The first pass verifies that the
6690 -- interpretation is not ambiguous, and the type of the left argument is
6691 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6692 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6693 -- though they carry a single (universal) type. Diagnose this case here.
6695 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6696 L : constant Node_Id := Left_Opnd (N);
6697 R : constant Node_Id := Right_Opnd (N);
6698 T : Entity_Id := Find_Unique_Type (L, R);
6700 procedure Check_Conditional_Expression (Cond : Node_Id);
6701 -- The resolution rule for conditional expressions requires that each
6702 -- such must have a unique type. This means that if several dependent
6703 -- expressions are of a non-null anonymous access type, and the context
6704 -- does not impose an expected type (as can be the case in an equality
6705 -- operation) the expression must be rejected.
6707 function Find_Unique_Access_Type return Entity_Id;
6708 -- In the case of allocators, make a last-ditch attempt to find a single
6709 -- access type with the right designated type. This is semantically
6710 -- dubious, and of no interest to any real code, but c48008a makes it
6713 ----------------------------------
6714 -- Check_Conditional_Expression --
6715 ----------------------------------
6717 procedure Check_Conditional_Expression (Cond : Node_Id) is
6718 Then_Expr : Node_Id;
6719 Else_Expr : Node_Id;
6722 if Nkind (Cond) = N_Conditional_Expression then
6723 Then_Expr := Next (First (Expressions (Cond)));
6724 Else_Expr := Next (Then_Expr);
6726 if Nkind (Then_Expr) /= N_Null
6727 and then Nkind (Else_Expr) /= N_Null
6730 ("cannot determine type of conditional expression", Cond);
6733 end Check_Conditional_Expression;
6735 -----------------------------
6736 -- Find_Unique_Access_Type --
6737 -----------------------------
6739 function Find_Unique_Access_Type return Entity_Id is
6745 if Ekind (Etype (R)) = E_Allocator_Type then
6746 Acc := Designated_Type (Etype (R));
6747 elsif Ekind (Etype (L)) = E_Allocator_Type then
6748 Acc := Designated_Type (Etype (L));
6754 while S /= Standard_Standard loop
6755 E := First_Entity (S);
6756 while Present (E) loop
6758 and then Is_Access_Type (E)
6759 and then Ekind (E) /= E_Allocator_Type
6760 and then Designated_Type (E) = Base_Type (Acc)
6772 end Find_Unique_Access_Type;
6774 -- Start of processing for Resolve_Equality_Op
6777 Set_Etype (N, Base_Type (Typ));
6778 Generate_Reference (T, N, ' ');
6780 if T = Any_Fixed then
6781 T := Unique_Fixed_Point_Type (L);
6784 if T /= Any_Type then
6785 if T = Any_String or else
6786 T = Any_Composite or else
6789 if T = Any_Character then
6790 Ambiguous_Character (L);
6792 Error_Msg_N ("ambiguous operands for equality", N);
6795 Set_Etype (N, Any_Type);
6798 elsif T = Any_Access
6799 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6801 T := Find_Unique_Access_Type;
6804 Error_Msg_N ("ambiguous operands for equality", N);
6805 Set_Etype (N, Any_Type);
6809 -- Conditional expressions must have a single type, and if the
6810 -- context does not impose one the dependent expressions cannot
6811 -- be anonymous access types.
6813 elsif Ada_Version >= Ada_2012
6814 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6815 E_Anonymous_Access_Subprogram_Type)
6816 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6817 E_Anonymous_Access_Subprogram_Type)
6819 Check_Conditional_Expression (L);
6820 Check_Conditional_Expression (R);
6826 -- In SPARK, equality operators = and /= for array types other than
6827 -- String are only defined when, for each index position, the
6828 -- operands have equal static bounds.
6830 if Is_Array_Type (T) then
6831 Current_Subprogram_Body_Is_Not_In_ALFA;
6833 if Base_Type (T) /= Standard_String
6834 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6835 and then Etype (L) /= Any_Composite -- or else L in error
6836 and then Etype (R) /= Any_Composite -- or else R in error
6837 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6839 Check_SPARK_Restriction
6840 ("array types should have matching static bounds", N);
6844 -- If the unique type is a class-wide type then it will be expanded
6845 -- into a dispatching call to the predefined primitive. Therefore we
6846 -- check here for potential violation of such restriction.
6848 if Is_Class_Wide_Type (T) then
6849 Check_Restriction (No_Dispatching_Calls, N);
6852 if Warn_On_Redundant_Constructs
6853 and then Comes_From_Source (N)
6854 and then Is_Entity_Name (R)
6855 and then Entity (R) = Standard_True
6856 and then Comes_From_Source (R)
6858 Error_Msg_N -- CODEFIX
6859 ("?comparison with True is redundant!", R);
6862 Check_Unset_Reference (L);
6863 Check_Unset_Reference (R);
6864 Generate_Operator_Reference (N, T);
6865 Check_Low_Bound_Tested (N);
6867 -- If this is an inequality, it may be the implicit inequality
6868 -- created for a user-defined operation, in which case the corres-
6869 -- ponding equality operation is not intrinsic, and the operation
6870 -- cannot be constant-folded. Else fold.
6872 if Nkind (N) = N_Op_Eq
6873 or else Comes_From_Source (Entity (N))
6874 or else Ekind (Entity (N)) = E_Operator
6875 or else Is_Intrinsic_Subprogram
6876 (Corresponding_Equality (Entity (N)))
6878 Eval_Relational_Op (N);
6880 elsif Nkind (N) = N_Op_Ne
6881 and then Is_Abstract_Subprogram (Entity (N))
6883 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6886 -- Ada 2005: If one operand is an anonymous access type, convert the
6887 -- other operand to it, to ensure that the underlying types match in
6888 -- the back-end. Same for access_to_subprogram, and the conversion
6889 -- verifies that the types are subtype conformant.
6891 -- We apply the same conversion in the case one of the operands is a
6892 -- private subtype of the type of the other.
6894 -- Why the Expander_Active test here ???
6898 (Ekind_In (T, E_Anonymous_Access_Type,
6899 E_Anonymous_Access_Subprogram_Type)
6900 or else Is_Private_Type (T))
6902 if Etype (L) /= T then
6904 Make_Unchecked_Type_Conversion (Sloc (L),
6905 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6906 Expression => Relocate_Node (L)));
6907 Analyze_And_Resolve (L, T);
6910 if (Etype (R)) /= T then
6912 Make_Unchecked_Type_Conversion (Sloc (R),
6913 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6914 Expression => Relocate_Node (R)));
6915 Analyze_And_Resolve (R, T);
6919 end Resolve_Equality_Op;
6921 ----------------------------------
6922 -- Resolve_Explicit_Dereference --
6923 ----------------------------------
6925 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6926 Loc : constant Source_Ptr := Sloc (N);
6928 P : constant Node_Id := Prefix (N);
6933 Check_Fully_Declared_Prefix (Typ, P);
6935 if Is_Overloaded (P) then
6937 -- Use the context type to select the prefix that has the correct
6940 Get_First_Interp (P, I, It);
6941 while Present (It.Typ) loop
6942 exit when Is_Access_Type (It.Typ)
6943 and then Covers (Typ, Designated_Type (It.Typ));
6944 Get_Next_Interp (I, It);
6947 if Present (It.Typ) then
6948 Resolve (P, It.Typ);
6950 -- If no interpretation covers the designated type of the prefix,
6951 -- this is the pathological case where not all implementations of
6952 -- the prefix allow the interpretation of the node as a call. Now
6953 -- that the expected type is known, Remove other interpretations
6954 -- from prefix, rewrite it as a call, and resolve again, so that
6955 -- the proper call node is generated.
6957 Get_First_Interp (P, I, It);
6958 while Present (It.Typ) loop
6959 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6963 Get_Next_Interp (I, It);
6967 Make_Function_Call (Loc,
6969 Make_Explicit_Dereference (Loc,
6971 Parameter_Associations => New_List);
6973 Save_Interps (N, New_N);
6975 Analyze_And_Resolve (N, Typ);
6979 Set_Etype (N, Designated_Type (It.Typ));
6985 if Is_Access_Type (Etype (P)) then
6986 Apply_Access_Check (N);
6989 -- If the designated type is a packed unconstrained array type, and the
6990 -- explicit dereference is not in the context of an attribute reference,
6991 -- then we must compute and set the actual subtype, since it is needed
6992 -- by Gigi. The reason we exclude the attribute case is that this is
6993 -- handled fine by Gigi, and in fact we use such attributes to build the
6994 -- actual subtype. We also exclude generated code (which builds actual
6995 -- subtypes directly if they are needed).
6997 if Is_Array_Type (Etype (N))
6998 and then Is_Packed (Etype (N))
6999 and then not Is_Constrained (Etype (N))
7000 and then Nkind (Parent (N)) /= N_Attribute_Reference
7001 and then Comes_From_Source (N)
7003 Set_Etype (N, Get_Actual_Subtype (N));
7006 -- Note: No Eval processing is required for an explicit dereference,
7007 -- because such a name can never be static.
7009 end Resolve_Explicit_Dereference;
7011 -------------------------------------
7012 -- Resolve_Expression_With_Actions --
7013 -------------------------------------
7015 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7018 end Resolve_Expression_With_Actions;
7020 -------------------------------
7021 -- Resolve_Indexed_Component --
7022 -------------------------------
7024 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7025 Name : constant Node_Id := Prefix (N);
7027 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7031 if Is_Overloaded (Name) then
7033 -- Use the context type to select the prefix that yields the correct
7039 I1 : Interp_Index := 0;
7040 P : constant Node_Id := Prefix (N);
7041 Found : Boolean := False;
7044 Get_First_Interp (P, I, It);
7045 while Present (It.Typ) loop
7046 if (Is_Array_Type (It.Typ)
7047 and then Covers (Typ, Component_Type (It.Typ)))
7048 or else (Is_Access_Type (It.Typ)
7049 and then Is_Array_Type (Designated_Type (It.Typ))
7053 Component_Type (Designated_Type (It.Typ))))
7056 It := Disambiguate (P, I1, I, Any_Type);
7058 if It = No_Interp then
7059 Error_Msg_N ("ambiguous prefix for indexing", N);
7065 Array_Type := It.Typ;
7071 Array_Type := It.Typ;
7076 Get_Next_Interp (I, It);
7081 Array_Type := Etype (Name);
7084 Resolve (Name, Array_Type);
7085 Array_Type := Get_Actual_Subtype_If_Available (Name);
7087 -- If prefix is access type, dereference to get real array type.
7088 -- Note: we do not apply an access check because the expander always
7089 -- introduces an explicit dereference, and the check will happen there.
7091 if Is_Access_Type (Array_Type) then
7092 Array_Type := Designated_Type (Array_Type);
7095 -- If name was overloaded, set component type correctly now
7096 -- If a misplaced call to an entry family (which has no index types)
7097 -- return. Error will be diagnosed from calling context.
7099 if Is_Array_Type (Array_Type) then
7100 Set_Etype (N, Component_Type (Array_Type));
7105 Index := First_Index (Array_Type);
7106 Expr := First (Expressions (N));
7108 -- The prefix may have resolved to a string literal, in which case its
7109 -- etype has a special representation. This is only possible currently
7110 -- if the prefix is a static concatenation, written in functional
7113 if Ekind (Array_Type) = E_String_Literal_Subtype then
7114 Resolve (Expr, Standard_Positive);
7117 while Present (Index) and Present (Expr) loop
7118 Resolve (Expr, Etype (Index));
7119 Check_Unset_Reference (Expr);
7121 if Is_Scalar_Type (Etype (Expr)) then
7122 Apply_Scalar_Range_Check (Expr, Etype (Index));
7124 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7132 -- Do not generate the warning on suspicious index if we are analyzing
7133 -- package Ada.Tags; otherwise we will report the warning with the
7134 -- Prims_Ptr field of the dispatch table.
7136 if Scope (Etype (Prefix (N))) = Standard_Standard
7138 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7141 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7142 Eval_Indexed_Component (N);
7145 -- If the array type is atomic, and is packed, and we are in a left side
7146 -- context, then this is worth a warning, since we have a situation
7147 -- where the access to the component may cause extra read/writes of
7148 -- the atomic array object, which could be considered unexpected.
7150 if Nkind (N) = N_Indexed_Component
7151 and then (Is_Atomic (Array_Type)
7152 or else (Is_Entity_Name (Prefix (N))
7153 and then Is_Atomic (Entity (Prefix (N)))))
7154 and then Is_Bit_Packed_Array (Array_Type)
7157 Error_Msg_N ("?assignment to component of packed atomic array",
7159 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7162 end Resolve_Indexed_Component;
7164 -----------------------------
7165 -- Resolve_Integer_Literal --
7166 -----------------------------
7168 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7171 Eval_Integer_Literal (N);
7172 end Resolve_Integer_Literal;
7174 --------------------------------
7175 -- Resolve_Intrinsic_Operator --
7176 --------------------------------
7178 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7179 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7181 Orig_Op : constant Entity_Id := Entity (N);
7186 -- We must preserve the original entity in a generic setting, so that
7187 -- the legality of the operation can be verified in an instance.
7189 if not Expander_Active then
7194 while Scope (Op) /= Standard_Standard loop
7196 pragma Assert (Present (Op));
7200 Set_Is_Overloaded (N, False);
7202 -- If the operand type is private, rewrite with suitable conversions on
7203 -- the operands and the result, to expose the proper underlying numeric
7206 if Is_Private_Type (Typ) then
7207 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
7209 if Nkind (N) = N_Op_Expon then
7210 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7212 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7215 if Nkind (Arg1) = N_Type_Conversion then
7216 Save_Interps (Left_Opnd (N), Expression (Arg1));
7219 if Nkind (Arg2) = N_Type_Conversion then
7220 Save_Interps (Right_Opnd (N), Expression (Arg2));
7223 Set_Left_Opnd (N, Arg1);
7224 Set_Right_Opnd (N, Arg2);
7226 Set_Etype (N, Btyp);
7227 Rewrite (N, Unchecked_Convert_To (Typ, N));
7230 elsif Typ /= Etype (Left_Opnd (N))
7231 or else Typ /= Etype (Right_Opnd (N))
7233 -- Add explicit conversion where needed, and save interpretations in
7234 -- case operands are overloaded. If the context is a VMS operation,
7235 -- assert that the conversion is legal (the operands have the proper
7236 -- types to select the VMS intrinsic). Note that in rare cases the
7237 -- VMS operators may be visible, but the default System is being used
7238 -- and Address is a private type.
7240 Arg1 := Convert_To (Typ, Left_Opnd (N));
7241 Arg2 := Convert_To (Typ, Right_Opnd (N));
7243 if Nkind (Arg1) = N_Type_Conversion then
7244 Save_Interps (Left_Opnd (N), Expression (Arg1));
7246 if Is_VMS_Operator (Orig_Op) then
7247 Set_Conversion_OK (Arg1);
7250 Save_Interps (Left_Opnd (N), Arg1);
7253 if Nkind (Arg2) = N_Type_Conversion then
7254 Save_Interps (Right_Opnd (N), Expression (Arg2));
7256 if Is_VMS_Operator (Orig_Op) then
7257 Set_Conversion_OK (Arg2);
7260 Save_Interps (Right_Opnd (N), Arg2);
7263 Rewrite (Left_Opnd (N), Arg1);
7264 Rewrite (Right_Opnd (N), Arg2);
7267 Resolve_Arithmetic_Op (N, Typ);
7270 Resolve_Arithmetic_Op (N, Typ);
7272 end Resolve_Intrinsic_Operator;
7274 --------------------------------------
7275 -- Resolve_Intrinsic_Unary_Operator --
7276 --------------------------------------
7278 procedure Resolve_Intrinsic_Unary_Operator
7282 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7288 while Scope (Op) /= Standard_Standard loop
7290 pragma Assert (Present (Op));
7295 if Is_Private_Type (Typ) then
7296 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7297 Save_Interps (Right_Opnd (N), Expression (Arg2));
7299 Set_Right_Opnd (N, Arg2);
7301 Set_Etype (N, Btyp);
7302 Rewrite (N, Unchecked_Convert_To (Typ, N));
7306 Resolve_Unary_Op (N, Typ);
7308 end Resolve_Intrinsic_Unary_Operator;
7310 ------------------------
7311 -- Resolve_Logical_Op --
7312 ------------------------
7314 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7318 Check_No_Direct_Boolean_Operators (N);
7320 -- Predefined operations on scalar types yield the base type. On the
7321 -- other hand, logical operations on arrays yield the type of the
7322 -- arguments (and the context).
7324 if Is_Array_Type (Typ) then
7327 B_Typ := Base_Type (Typ);
7330 -- OK if this is a VMS-specific intrinsic operation
7332 if Is_VMS_Operator (Entity (N)) then
7335 -- The following test is required because the operands of the operation
7336 -- may be literals, in which case the resulting type appears to be
7337 -- compatible with a signed integer type, when in fact it is compatible
7338 -- only with modular types. If the context itself is universal, the
7339 -- operation is illegal.
7341 elsif not Valid_Boolean_Arg (Typ) then
7342 Error_Msg_N ("invalid context for logical operation", N);
7343 Set_Etype (N, Any_Type);
7346 elsif Typ = Any_Modular then
7348 ("no modular type available in this context", N);
7349 Set_Etype (N, Any_Type);
7352 elsif Is_Modular_Integer_Type (Typ)
7353 and then Etype (Left_Opnd (N)) = Universal_Integer
7354 and then Etype (Right_Opnd (N)) = Universal_Integer
7356 Check_For_Visible_Operator (N, B_Typ);
7359 Resolve (Left_Opnd (N), B_Typ);
7360 Resolve (Right_Opnd (N), B_Typ);
7362 Check_Unset_Reference (Left_Opnd (N));
7363 Check_Unset_Reference (Right_Opnd (N));
7365 Set_Etype (N, B_Typ);
7366 Generate_Operator_Reference (N, B_Typ);
7367 Eval_Logical_Op (N);
7369 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7370 -- only when both operands have same static lower and higher bounds. Of
7371 -- course the types have to match, so only check if operands are
7372 -- compatible and the node itself has no errors.
7374 if Is_Array_Type (B_Typ)
7375 and then Nkind (N) in N_Binary_Op
7377 Current_Subprogram_Body_Is_Not_In_ALFA;
7380 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7381 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7383 if Base_Type (Left_Typ) = Base_Type (Right_Typ)
7384 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7385 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7386 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7388 Check_SPARK_Restriction
7389 ("array types should have matching static bounds", N);
7393 end Resolve_Logical_Op;
7395 ---------------------------
7396 -- Resolve_Membership_Op --
7397 ---------------------------
7399 -- The context can only be a boolean type, and does not determine the
7400 -- arguments. Arguments should be unambiguous, but the preference rule for
7401 -- universal types applies.
7403 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7404 pragma Warnings (Off, Typ);
7406 L : constant Node_Id := Left_Opnd (N);
7407 R : constant Node_Id := Right_Opnd (N);
7410 procedure Resolve_Set_Membership;
7411 -- Analysis has determined a unique type for the left operand. Use it to
7412 -- resolve the disjuncts.
7414 ----------------------------
7415 -- Resolve_Set_Membership --
7416 ----------------------------
7418 procedure Resolve_Set_Membership is
7422 Resolve (L, Etype (L));
7424 Alt := First (Alternatives (N));
7425 while Present (Alt) loop
7427 -- Alternative is an expression, a range
7428 -- or a subtype mark.
7430 if not Is_Entity_Name (Alt)
7431 or else not Is_Type (Entity (Alt))
7433 Resolve (Alt, Etype (L));
7438 end Resolve_Set_Membership;
7440 -- Start of processing for Resolve_Membership_Op
7443 if L = Error or else R = Error then
7447 if Present (Alternatives (N)) then
7448 Resolve_Set_Membership;
7451 elsif not Is_Overloaded (R)
7453 (Etype (R) = Universal_Integer
7455 Etype (R) = Universal_Real)
7456 and then Is_Overloaded (L)
7460 -- Ada 2005 (AI-251): Support the following case:
7462 -- type I is interface;
7463 -- type T is tagged ...
7465 -- function Test (O : I'Class) is
7467 -- return O in T'Class.
7470 -- In this case we have nothing else to do. The membership test will be
7471 -- done at run time.
7473 elsif Ada_Version >= Ada_2005
7474 and then Is_Class_Wide_Type (Etype (L))
7475 and then Is_Interface (Etype (L))
7476 and then Is_Class_Wide_Type (Etype (R))
7477 and then not Is_Interface (Etype (R))
7481 T := Intersect_Types (L, R);
7484 -- If mixed-mode operations are present and operands are all literal,
7485 -- the only interpretation involves Duration, which is probably not
7486 -- the intention of the programmer.
7488 if T = Any_Fixed then
7489 T := Unique_Fixed_Point_Type (N);
7491 if T = Any_Type then
7497 Check_Unset_Reference (L);
7499 if Nkind (R) = N_Range
7500 and then not Is_Scalar_Type (T)
7502 Error_Msg_N ("scalar type required for range", R);
7505 if Is_Entity_Name (R) then
7506 Freeze_Expression (R);
7509 Check_Unset_Reference (R);
7512 Eval_Membership_Op (N);
7513 end Resolve_Membership_Op;
7519 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7520 Loc : constant Source_Ptr := Sloc (N);
7523 -- Handle restriction against anonymous null access values This
7524 -- restriction can be turned off using -gnatdj.
7526 -- Ada 2005 (AI-231): Remove restriction
7528 if Ada_Version < Ada_2005
7529 and then not Debug_Flag_J
7530 and then Ekind (Typ) = E_Anonymous_Access_Type
7531 and then Comes_From_Source (N)
7533 -- In the common case of a call which uses an explicitly null value
7534 -- for an access parameter, give specialized error message.
7536 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7540 ("null is not allowed as argument for an access parameter", N);
7542 -- Standard message for all other cases (are there any?)
7546 ("null cannot be of an anonymous access type", N);
7550 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7551 -- assignment to a null-excluding object
7553 if Ada_Version >= Ada_2005
7554 and then Can_Never_Be_Null (Typ)
7555 and then Nkind (Parent (N)) = N_Assignment_Statement
7557 if not Inside_Init_Proc then
7559 (Compile_Time_Constraint_Error (N,
7560 "(Ada 2005) null not allowed in null-excluding objects?"),
7561 Make_Raise_Constraint_Error (Loc,
7562 Reason => CE_Access_Check_Failed));
7565 Make_Raise_Constraint_Error (Loc,
7566 Reason => CE_Access_Check_Failed));
7570 -- In a distributed context, null for a remote access to subprogram may
7571 -- need to be replaced with a special record aggregate. In this case,
7572 -- return after having done the transformation.
7574 if (Ekind (Typ) = E_Record_Type
7575 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7576 and then Remote_AST_Null_Value (N, Typ)
7581 -- The null literal takes its type from the context
7586 -----------------------
7587 -- Resolve_Op_Concat --
7588 -----------------------
7590 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7592 -- We wish to avoid deep recursion, because concatenations are often
7593 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7594 -- operands nonrecursively until we find something that is not a simple
7595 -- concatenation (A in this case). We resolve that, and then walk back
7596 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7597 -- to do the rest of the work at each level. The Parent pointers allow
7598 -- us to avoid recursion, and thus avoid running out of memory. See also
7599 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7605 -- The following code is equivalent to:
7607 -- Resolve_Op_Concat_First (NN, Typ);
7608 -- Resolve_Op_Concat_Arg (N, ...);
7609 -- Resolve_Op_Concat_Rest (N, Typ);
7611 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7612 -- operand is a concatenation.
7614 -- Walk down left operands
7617 Resolve_Op_Concat_First (NN, Typ);
7618 Op1 := Left_Opnd (NN);
7619 exit when not (Nkind (Op1) = N_Op_Concat
7620 and then not Is_Array_Type (Component_Type (Typ))
7621 and then Entity (Op1) = Entity (NN));
7625 -- Now (given the above example) NN is A&B and Op1 is A
7627 -- First resolve Op1 ...
7629 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7631 -- ... then walk NN back up until we reach N (where we started), calling
7632 -- Resolve_Op_Concat_Rest along the way.
7635 Resolve_Op_Concat_Rest (NN, Typ);
7640 if Base_Type (Etype (N)) /= Standard_String then
7641 Check_SPARK_Restriction
7642 ("result of concatenation should have type String", N);
7644 end Resolve_Op_Concat;
7646 ---------------------------
7647 -- Resolve_Op_Concat_Arg --
7648 ---------------------------
7650 procedure Resolve_Op_Concat_Arg
7656 Btyp : constant Entity_Id := Base_Type (Typ);
7661 or else (not Is_Overloaded (Arg)
7662 and then Etype (Arg) /= Any_Composite
7663 and then Covers (Component_Type (Typ), Etype (Arg)))
7665 Resolve (Arg, Component_Type (Typ));
7667 Resolve (Arg, Btyp);
7670 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
7671 if Nkind (Arg) = N_Aggregate
7672 and then Is_Composite_Type (Component_Type (Typ))
7674 if Is_Private_Type (Component_Type (Typ)) then
7675 Resolve (Arg, Btyp);
7677 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7678 Set_Etype (Arg, Any_Type);
7682 if Is_Overloaded (Arg)
7683 and then Has_Compatible_Type (Arg, Typ)
7684 and then Etype (Arg) /= Any_Type
7692 Get_First_Interp (Arg, I, It);
7694 Get_Next_Interp (I, It);
7696 -- Special-case the error message when the overloading is
7697 -- caused by a function that yields an array and can be
7698 -- called without parameters.
7700 if It.Nam = Func then
7701 Error_Msg_Sloc := Sloc (Func);
7702 Error_Msg_N ("ambiguous call to function#", Arg);
7704 ("\\interpretation as call yields&", Arg, Typ);
7706 ("\\interpretation as indexing of call yields&",
7707 Arg, Component_Type (Typ));
7711 ("ambiguous operand for concatenation!", Arg);
7713 Get_First_Interp (Arg, I, It);
7714 while Present (It.Nam) loop
7715 Error_Msg_Sloc := Sloc (It.Nam);
7717 if Base_Type (It.Typ) = Base_Type (Typ)
7718 or else Base_Type (It.Typ) =
7719 Base_Type (Component_Type (Typ))
7721 Error_Msg_N -- CODEFIX
7722 ("\\possible interpretation#", Arg);
7725 Get_Next_Interp (I, It);
7731 Resolve (Arg, Component_Type (Typ));
7733 if Nkind (Arg) = N_String_Literal then
7734 Set_Etype (Arg, Component_Type (Typ));
7737 if Arg = Left_Opnd (N) then
7738 Set_Is_Component_Left_Opnd (N);
7740 Set_Is_Component_Right_Opnd (N);
7745 Resolve (Arg, Btyp);
7748 -- Concatenation is restricted in SPARK: each operand must be either a
7749 -- string literal, a static character expression, or another
7750 -- concatenation. Arg cannot be a concatenation here as callers of
7751 -- Resolve_Op_Concat_Arg call it separately on each final operand, past
7752 -- concatenation operations.
7754 if Is_Character_Type (Etype (Arg)) then
7755 if not Is_Static_Expression (Arg) then
7756 Check_SPARK_Restriction
7757 ("character operand for concatenation should be static", N);
7760 elsif Is_String_Type (Etype (Arg)) then
7761 if not Is_Static_Expression (Arg) then
7762 Check_SPARK_Restriction
7763 ("string operand for concatenation should be static", N);
7766 -- Do not issue error on an operand that is neither a character nor a
7767 -- string, as the error is issued in Resolve_Op_Concat.
7773 Check_Unset_Reference (Arg);
7774 end Resolve_Op_Concat_Arg;
7776 -----------------------------
7777 -- Resolve_Op_Concat_First --
7778 -----------------------------
7780 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7781 Btyp : constant Entity_Id := Base_Type (Typ);
7782 Op1 : constant Node_Id := Left_Opnd (N);
7783 Op2 : constant Node_Id := Right_Opnd (N);
7786 -- The parser folds an enormous sequence of concatenations of string
7787 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7788 -- in the right operand. If the expression resolves to a predefined "&"
7789 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7790 -- we give an error. See P_Simple_Expression in Par.Ch4.
7792 if Nkind (Op2) = N_String_Literal
7793 and then Is_Folded_In_Parser (Op2)
7794 and then Ekind (Entity (N)) = E_Function
7796 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7797 and then String_Length (Strval (Op1)) = 0);
7798 Error_Msg_N ("too many user-defined concatenations", N);
7802 Set_Etype (N, Btyp);
7804 if Is_Limited_Composite (Btyp) then
7805 Error_Msg_N ("concatenation not available for limited array", N);
7806 Explain_Limited_Type (Btyp, N);
7808 end Resolve_Op_Concat_First;
7810 ----------------------------
7811 -- Resolve_Op_Concat_Rest --
7812 ----------------------------
7814 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7815 Op1 : constant Node_Id := Left_Opnd (N);
7816 Op2 : constant Node_Id := Right_Opnd (N);
7819 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7821 Generate_Operator_Reference (N, Typ);
7823 if Is_String_Type (Typ) then
7824 Eval_Concatenation (N);
7827 -- If this is not a static concatenation, but the result is a string
7828 -- type (and not an array of strings) ensure that static string operands
7829 -- have their subtypes properly constructed.
7831 if Nkind (N) /= N_String_Literal
7832 and then Is_Character_Type (Component_Type (Typ))
7834 Set_String_Literal_Subtype (Op1, Typ);
7835 Set_String_Literal_Subtype (Op2, Typ);
7837 end Resolve_Op_Concat_Rest;
7839 ----------------------
7840 -- Resolve_Op_Expon --
7841 ----------------------
7843 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7844 B_Typ : constant Entity_Id := Base_Type (Typ);
7847 -- Catch attempts to do fixed-point exponentiation with universal
7848 -- operands, which is a case where the illegality is not caught during
7849 -- normal operator analysis.
7851 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7852 Error_Msg_N ("exponentiation not available for fixed point", N);
7856 if Comes_From_Source (N)
7857 and then Ekind (Entity (N)) = E_Function
7858 and then Is_Imported (Entity (N))
7859 and then Is_Intrinsic_Subprogram (Entity (N))
7861 Resolve_Intrinsic_Operator (N, Typ);
7865 if Etype (Left_Opnd (N)) = Universal_Integer
7866 or else Etype (Left_Opnd (N)) = Universal_Real
7868 Check_For_Visible_Operator (N, B_Typ);
7871 -- We do the resolution using the base type, because intermediate values
7872 -- in expressions always are of the base type, not a subtype of it.
7874 Resolve (Left_Opnd (N), B_Typ);
7875 Resolve (Right_Opnd (N), Standard_Integer);
7877 Check_Unset_Reference (Left_Opnd (N));
7878 Check_Unset_Reference (Right_Opnd (N));
7880 Set_Etype (N, B_Typ);
7881 Generate_Operator_Reference (N, B_Typ);
7884 -- Set overflow checking bit. Much cleverer code needed here eventually
7885 -- and perhaps the Resolve routines should be separated for the various
7886 -- arithmetic operations, since they will need different processing. ???
7888 if Nkind (N) in N_Op then
7889 if not Overflow_Checks_Suppressed (Etype (N)) then
7890 Enable_Overflow_Check (N);
7893 end Resolve_Op_Expon;
7895 --------------------
7896 -- Resolve_Op_Not --
7897 --------------------
7899 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7902 function Parent_Is_Boolean return Boolean;
7903 -- This function determines if the parent node is a boolean operator or
7904 -- operation (comparison op, membership test, or short circuit form) and
7905 -- the not in question is the left operand of this operation. Note that
7906 -- if the not is in parens, then false is returned.
7908 -----------------------
7909 -- Parent_Is_Boolean --
7910 -----------------------
7912 function Parent_Is_Boolean return Boolean is
7914 if Paren_Count (N) /= 0 then
7918 case Nkind (Parent (N)) is
7933 return Left_Opnd (Parent (N)) = N;
7939 end Parent_Is_Boolean;
7941 -- Start of processing for Resolve_Op_Not
7944 -- Predefined operations on scalar types yield the base type. On the
7945 -- other hand, logical operations on arrays yield the type of the
7946 -- arguments (and the context).
7948 if Is_Array_Type (Typ) then
7951 B_Typ := Base_Type (Typ);
7954 if Is_VMS_Operator (Entity (N)) then
7957 -- Straightforward case of incorrect arguments
7959 elsif not Valid_Boolean_Arg (Typ) then
7960 Error_Msg_N ("invalid operand type for operator&", N);
7961 Set_Etype (N, Any_Type);
7964 -- Special case of probable missing parens
7966 elsif Typ = Universal_Integer or else Typ = Any_Modular then
7967 if Parent_Is_Boolean then
7969 ("operand of not must be enclosed in parentheses",
7973 ("no modular type available in this context", N);
7976 Set_Etype (N, Any_Type);
7979 -- OK resolution of NOT
7982 -- Warn if non-boolean types involved. This is a case like not a < b
7983 -- where a and b are modular, where we will get (not a) < b and most
7984 -- likely not (a < b) was intended.
7986 if Warn_On_Questionable_Missing_Parens
7987 and then not Is_Boolean_Type (Typ)
7988 and then Parent_Is_Boolean
7990 Error_Msg_N ("?not expression should be parenthesized here!", N);
7993 -- Warn on double negation if checking redundant constructs
7995 if Warn_On_Redundant_Constructs
7996 and then Comes_From_Source (N)
7997 and then Comes_From_Source (Right_Opnd (N))
7998 and then Root_Type (Typ) = Standard_Boolean
7999 and then Nkind (Right_Opnd (N)) = N_Op_Not
8001 Error_Msg_N ("redundant double negation?", N);
8004 -- Complete resolution and evaluation of NOT
8006 Resolve (Right_Opnd (N), B_Typ);
8007 Check_Unset_Reference (Right_Opnd (N));
8008 Set_Etype (N, B_Typ);
8009 Generate_Operator_Reference (N, B_Typ);
8014 -----------------------------
8015 -- Resolve_Operator_Symbol --
8016 -----------------------------
8018 -- Nothing to be done, all resolved already
8020 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8021 pragma Warnings (Off, N);
8022 pragma Warnings (Off, Typ);
8026 end Resolve_Operator_Symbol;
8028 ----------------------------------
8029 -- Resolve_Qualified_Expression --
8030 ----------------------------------
8032 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8033 pragma Warnings (Off, Typ);
8035 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8036 Expr : constant Node_Id := Expression (N);
8039 Resolve (Expr, Target_Typ);
8041 if Is_Array_Type (Target_Typ)
8042 and then Is_Array_Type (Etype (Expr))
8043 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8044 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8046 Check_SPARK_Restriction
8047 ("array types should have matching static bounds", N);
8050 -- A qualified expression requires an exact match of the type, class-
8051 -- wide matching is not allowed. However, if the qualifying type is
8052 -- specific and the expression has a class-wide type, it may still be
8053 -- okay, since it can be the result of the expansion of a call to a
8054 -- dispatching function, so we also have to check class-wideness of the
8055 -- type of the expression's original node.
8057 if (Is_Class_Wide_Type (Target_Typ)
8059 (Is_Class_Wide_Type (Etype (Expr))
8060 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8061 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8063 Wrong_Type (Expr, Target_Typ);
8066 -- If the target type is unconstrained, then we reset the type of the
8067 -- result from the type of the expression. For other cases, the actual
8068 -- subtype of the expression is the target type.
8070 if Is_Composite_Type (Target_Typ)
8071 and then not Is_Constrained (Target_Typ)
8073 Set_Etype (N, Etype (Expr));
8076 Eval_Qualified_Expression (N);
8077 end Resolve_Qualified_Expression;
8079 -----------------------------------
8080 -- Resolve_Quantified_Expression --
8081 -----------------------------------
8083 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
8085 -- The loop structure is already resolved during its analysis, only the
8086 -- resolution of the condition needs to be done. Expansion is disabled
8087 -- so that checks and other generated code are inserted in the tree
8088 -- after expression has been rewritten as a loop.
8090 Expander_Mode_Save_And_Set (False);
8091 Resolve (Condition (N), Typ);
8092 Expander_Mode_Restore;
8093 end Resolve_Quantified_Expression;
8099 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8100 L : constant Node_Id := Low_Bound (N);
8101 H : constant Node_Id := High_Bound (N);
8103 function First_Last_Ref return Boolean;
8104 -- Returns True if N is of the form X'First .. X'Last where X is the
8105 -- same entity for both attributes.
8107 --------------------
8108 -- First_Last_Ref --
8109 --------------------
8111 function First_Last_Ref return Boolean is
8112 Lorig : constant Node_Id := Original_Node (L);
8113 Horig : constant Node_Id := Original_Node (H);
8116 if Nkind (Lorig) = N_Attribute_Reference
8117 and then Nkind (Horig) = N_Attribute_Reference
8118 and then Attribute_Name (Lorig) = Name_First
8119 and then Attribute_Name (Horig) = Name_Last
8122 PL : constant Node_Id := Prefix (Lorig);
8123 PH : constant Node_Id := Prefix (Horig);
8125 if Is_Entity_Name (PL)
8126 and then Is_Entity_Name (PH)
8127 and then Entity (PL) = Entity (PH)
8137 -- Start of processing for Resolve_Range
8144 -- Check for inappropriate range on unordered enumeration type
8146 if Bad_Unordered_Enumeration_Reference (N, Typ)
8148 -- Exclude X'First .. X'Last if X is the same entity for both
8150 and then not First_Last_Ref
8152 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
8155 Check_Unset_Reference (L);
8156 Check_Unset_Reference (H);
8158 -- We have to check the bounds for being within the base range as
8159 -- required for a non-static context. Normally this is automatic and
8160 -- done as part of evaluating expressions, but the N_Range node is an
8161 -- exception, since in GNAT we consider this node to be a subexpression,
8162 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8163 -- this, but that would put the test on the main evaluation path for
8166 Check_Non_Static_Context (L);
8167 Check_Non_Static_Context (H);
8169 -- Check for an ambiguous range over character literals. This will
8170 -- happen with a membership test involving only literals.
8172 if Typ = Any_Character then
8173 Ambiguous_Character (L);
8174 Set_Etype (N, Any_Type);
8178 -- If bounds are static, constant-fold them, so size computations are
8179 -- identical between front-end and back-end. Do not perform this
8180 -- transformation while analyzing generic units, as type information
8181 -- would be lost when reanalyzing the constant node in the instance.
8183 if Is_Discrete_Type (Typ) and then Expander_Active then
8184 if Is_OK_Static_Expression (L) then
8185 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8188 if Is_OK_Static_Expression (H) then
8189 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8194 --------------------------
8195 -- Resolve_Real_Literal --
8196 --------------------------
8198 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8199 Actual_Typ : constant Entity_Id := Etype (N);
8202 -- Special processing for fixed-point literals to make sure that the
8203 -- value is an exact multiple of small where this is required. We skip
8204 -- this for the universal real case, and also for generic types.
8206 if Is_Fixed_Point_Type (Typ)
8207 and then Typ /= Universal_Fixed
8208 and then Typ /= Any_Fixed
8209 and then not Is_Generic_Type (Typ)
8212 Val : constant Ureal := Realval (N);
8213 Cintr : constant Ureal := Val / Small_Value (Typ);
8214 Cint : constant Uint := UR_Trunc (Cintr);
8215 Den : constant Uint := Norm_Den (Cintr);
8219 -- Case of literal is not an exact multiple of the Small
8223 -- For a source program literal for a decimal fixed-point type,
8224 -- this is statically illegal (RM 4.9(36)).
8226 if Is_Decimal_Fixed_Point_Type (Typ)
8227 and then Actual_Typ = Universal_Real
8228 and then Comes_From_Source (N)
8230 Error_Msg_N ("value has extraneous low order digits", N);
8233 -- Generate a warning if literal from source
8235 if Is_Static_Expression (N)
8236 and then Warn_On_Bad_Fixed_Value
8239 ("?static fixed-point value is not a multiple of Small!",
8243 -- Replace literal by a value that is the exact representation
8244 -- of a value of the type, i.e. a multiple of the small value,
8245 -- by truncation, since Machine_Rounds is false for all GNAT
8246 -- fixed-point types (RM 4.9(38)).
8248 Stat := Is_Static_Expression (N);
8250 Make_Real_Literal (Sloc (N),
8251 Realval => Small_Value (Typ) * Cint));
8253 Set_Is_Static_Expression (N, Stat);
8256 -- In all cases, set the corresponding integer field
8258 Set_Corresponding_Integer_Value (N, Cint);
8262 -- Now replace the actual type by the expected type as usual
8265 Eval_Real_Literal (N);
8266 end Resolve_Real_Literal;
8268 -----------------------
8269 -- Resolve_Reference --
8270 -----------------------
8272 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8273 P : constant Node_Id := Prefix (N);
8276 -- Replace general access with specific type
8278 if Ekind (Etype (N)) = E_Allocator_Type then
8279 Set_Etype (N, Base_Type (Typ));
8282 Resolve (P, Designated_Type (Etype (N)));
8284 -- If we are taking the reference of a volatile entity, then treat it as
8285 -- a potential modification of this entity. This is too conservative,
8286 -- but necessary because remove side effects can cause transformations
8287 -- of normal assignments into reference sequences that otherwise fail to
8288 -- notice the modification.
8290 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8291 Note_Possible_Modification (P, Sure => False);
8293 end Resolve_Reference;
8295 --------------------------------
8296 -- Resolve_Selected_Component --
8297 --------------------------------
8299 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8301 Comp1 : Entity_Id := Empty; -- prevent junk warning
8302 P : constant Node_Id := Prefix (N);
8303 S : constant Node_Id := Selector_Name (N);
8304 T : Entity_Id := Etype (P);
8306 I1 : Interp_Index := 0; -- prevent junk warning
8311 function Init_Component return Boolean;
8312 -- Check whether this is the initialization of a component within an
8313 -- init proc (by assignment or call to another init proc). If true,
8314 -- there is no need for a discriminant check.
8316 --------------------
8317 -- Init_Component --
8318 --------------------
8320 function Init_Component return Boolean is
8322 return Inside_Init_Proc
8323 and then Nkind (Prefix (N)) = N_Identifier
8324 and then Chars (Prefix (N)) = Name_uInit
8325 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8328 -- Start of processing for Resolve_Selected_Component
8331 if Is_Overloaded (P) then
8333 -- Use the context type to select the prefix that has a selector
8334 -- of the correct name and type.
8337 Get_First_Interp (P, I, It);
8339 Search : while Present (It.Typ) loop
8340 if Is_Access_Type (It.Typ) then
8341 T := Designated_Type (It.Typ);
8346 if Is_Record_Type (T) then
8348 -- The visible components of a class-wide type are those of
8351 if Is_Class_Wide_Type (T) then
8355 Comp := First_Entity (T);
8356 while Present (Comp) loop
8357 if Chars (Comp) = Chars (S)
8358 and then Covers (Etype (Comp), Typ)
8367 It := Disambiguate (P, I1, I, Any_Type);
8369 if It = No_Interp then
8371 ("ambiguous prefix for selected component", N);
8378 -- There may be an implicit dereference. Retrieve
8379 -- designated record type.
8381 if Is_Access_Type (It1.Typ) then
8382 T := Designated_Type (It1.Typ);
8387 if Scope (Comp1) /= T then
8389 -- Resolution chooses the new interpretation.
8390 -- Find the component with the right name.
8392 Comp1 := First_Entity (T);
8393 while Present (Comp1)
8394 and then Chars (Comp1) /= Chars (S)
8396 Comp1 := Next_Entity (Comp1);
8405 Comp := Next_Entity (Comp);
8409 Get_Next_Interp (I, It);
8412 Resolve (P, It1.Typ);
8414 Set_Entity_With_Style_Check (S, Comp1);
8417 -- Resolve prefix with its type
8422 -- Generate cross-reference. We needed to wait until full overloading
8423 -- resolution was complete to do this, since otherwise we can't tell if
8424 -- we are an lvalue or not.
8426 if May_Be_Lvalue (N) then
8427 Generate_Reference (Entity (S), S, 'm');
8429 Generate_Reference (Entity (S), S, 'r');
8432 -- If prefix is an access type, the node will be transformed into an
8433 -- explicit dereference during expansion. The type of the node is the
8434 -- designated type of that of the prefix.
8436 if Is_Access_Type (Etype (P)) then
8437 T := Designated_Type (Etype (P));
8438 Check_Fully_Declared_Prefix (T, P);
8443 if Has_Discriminants (T)
8444 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8445 and then Present (Original_Record_Component (Entity (S)))
8446 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8447 and then Present (Discriminant_Checking_Func
8448 (Original_Record_Component (Entity (S))))
8449 and then not Discriminant_Checks_Suppressed (T)
8450 and then not Init_Component
8452 Set_Do_Discriminant_Check (N);
8455 if Ekind (Entity (S)) = E_Void then
8456 Error_Msg_N ("premature use of component", S);
8459 -- If the prefix is a record conversion, this may be a renamed
8460 -- discriminant whose bounds differ from those of the original
8461 -- one, so we must ensure that a range check is performed.
8463 if Nkind (P) = N_Type_Conversion
8464 and then Ekind (Entity (S)) = E_Discriminant
8465 and then Is_Discrete_Type (Typ)
8467 Set_Etype (N, Base_Type (Typ));
8470 -- Note: No Eval processing is required, because the prefix is of a
8471 -- record type, or protected type, and neither can possibly be static.
8473 -- If the array type is atomic, and is packed, and we are in a left side
8474 -- context, then this is worth a warning, since we have a situation
8475 -- where the access to the component may cause extra read/writes of the
8476 -- atomic array object, which could be considered unexpected.
8478 if Nkind (N) = N_Selected_Component
8479 and then (Is_Atomic (T)
8480 or else (Is_Entity_Name (Prefix (N))
8481 and then Is_Atomic (Entity (Prefix (N)))))
8482 and then Is_Packed (T)
8485 Error_Msg_N ("?assignment to component of packed atomic record",
8487 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8490 end Resolve_Selected_Component;
8496 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8497 B_Typ : constant Entity_Id := Base_Type (Typ);
8498 L : constant Node_Id := Left_Opnd (N);
8499 R : constant Node_Id := Right_Opnd (N);
8502 -- We do the resolution using the base type, because intermediate values
8503 -- in expressions always are of the base type, not a subtype of it.
8506 Resolve (R, Standard_Natural);
8508 Check_Unset_Reference (L);
8509 Check_Unset_Reference (R);
8511 Set_Etype (N, B_Typ);
8512 Generate_Operator_Reference (N, B_Typ);
8516 ---------------------------
8517 -- Resolve_Short_Circuit --
8518 ---------------------------
8520 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8521 B_Typ : constant Entity_Id := Base_Type (Typ);
8522 L : constant Node_Id := Left_Opnd (N);
8523 R : constant Node_Id := Right_Opnd (N);
8529 -- Check for issuing warning for always False assert/check, this happens
8530 -- when assertions are turned off, in which case the pragma Assert/Check
8531 -- was transformed into:
8533 -- if False and then <condition> then ...
8535 -- and we detect this pattern
8537 if Warn_On_Assertion_Failure
8538 and then Is_Entity_Name (R)
8539 and then Entity (R) = Standard_False
8540 and then Nkind (Parent (N)) = N_If_Statement
8541 and then Nkind (N) = N_And_Then
8542 and then Is_Entity_Name (L)
8543 and then Entity (L) = Standard_False
8546 Orig : constant Node_Id := Original_Node (Parent (N));
8549 if Nkind (Orig) = N_Pragma
8550 and then Pragma_Name (Orig) = Name_Assert
8552 -- Don't want to warn if original condition is explicit False
8555 Expr : constant Node_Id :=
8558 (First (Pragma_Argument_Associations (Orig))));
8560 if Is_Entity_Name (Expr)
8561 and then Entity (Expr) = Standard_False
8565 -- Issue warning. We do not want the deletion of the
8566 -- IF/AND-THEN to take this message with it. We achieve
8567 -- this by making sure that the expanded code points to
8568 -- the Sloc of the expression, not the original pragma.
8571 ("?assertion would fail at run time!",
8573 (First (Pragma_Argument_Associations (Orig))));
8577 -- Similar processing for Check pragma
8579 elsif Nkind (Orig) = N_Pragma
8580 and then Pragma_Name (Orig) = Name_Check
8582 -- Don't want to warn if original condition is explicit False
8585 Expr : constant Node_Id :=
8589 (Pragma_Argument_Associations (Orig)))));
8591 if Is_Entity_Name (Expr)
8592 and then Entity (Expr) = Standard_False
8597 ("?check would fail at run time!",
8599 (Last (Pragma_Argument_Associations (Orig))));
8606 -- Continue with processing of short circuit
8608 Check_Unset_Reference (L);
8609 Check_Unset_Reference (R);
8611 Set_Etype (N, B_Typ);
8612 Eval_Short_Circuit (N);
8613 end Resolve_Short_Circuit;
8619 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8620 Name : constant Node_Id := Prefix (N);
8621 Drange : constant Node_Id := Discrete_Range (N);
8622 Array_Type : Entity_Id := Empty;
8626 if Is_Overloaded (Name) then
8628 -- Use the context type to select the prefix that yields the correct
8633 I1 : Interp_Index := 0;
8635 P : constant Node_Id := Prefix (N);
8636 Found : Boolean := False;
8639 Get_First_Interp (P, I, It);
8640 while Present (It.Typ) loop
8641 if (Is_Array_Type (It.Typ)
8642 and then Covers (Typ, It.Typ))
8643 or else (Is_Access_Type (It.Typ)
8644 and then Is_Array_Type (Designated_Type (It.Typ))
8645 and then Covers (Typ, Designated_Type (It.Typ)))
8648 It := Disambiguate (P, I1, I, Any_Type);
8650 if It = No_Interp then
8651 Error_Msg_N ("ambiguous prefix for slicing", N);
8656 Array_Type := It.Typ;
8661 Array_Type := It.Typ;
8666 Get_Next_Interp (I, It);
8671 Array_Type := Etype (Name);
8674 Resolve (Name, Array_Type);
8676 if Is_Access_Type (Array_Type) then
8677 Apply_Access_Check (N);
8678 Array_Type := Designated_Type (Array_Type);
8680 -- If the prefix is an access to an unconstrained array, we must use
8681 -- the actual subtype of the object to perform the index checks. The
8682 -- object denoted by the prefix is implicit in the node, so we build
8683 -- an explicit representation for it in order to compute the actual
8686 if not Is_Constrained (Array_Type) then
8687 Remove_Side_Effects (Prefix (N));
8690 Obj : constant Node_Id :=
8691 Make_Explicit_Dereference (Sloc (N),
8692 Prefix => New_Copy_Tree (Prefix (N)));
8694 Set_Etype (Obj, Array_Type);
8695 Set_Parent (Obj, Parent (N));
8696 Array_Type := Get_Actual_Subtype (Obj);
8700 elsif Is_Entity_Name (Name)
8701 or else Nkind (Name) = N_Explicit_Dereference
8702 or else (Nkind (Name) = N_Function_Call
8703 and then not Is_Constrained (Etype (Name)))
8705 Array_Type := Get_Actual_Subtype (Name);
8707 -- If the name is a selected component that depends on discriminants,
8708 -- build an actual subtype for it. This can happen only when the name
8709 -- itself is overloaded; otherwise the actual subtype is created when
8710 -- the selected component is analyzed.
8712 elsif Nkind (Name) = N_Selected_Component
8713 and then Full_Analysis
8714 and then Depends_On_Discriminant (First_Index (Array_Type))
8717 Act_Decl : constant Node_Id :=
8718 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8720 Insert_Action (N, Act_Decl);
8721 Array_Type := Defining_Identifier (Act_Decl);
8724 -- Maybe this should just be "else", instead of checking for the
8725 -- specific case of slice??? This is needed for the case where the
8726 -- prefix is an Image attribute, which gets expanded to a slice, and so
8727 -- has a constrained subtype which we want to use for the slice range
8728 -- check applied below (the range check won't get done if the
8729 -- unconstrained subtype of the 'Image is used).
8731 elsif Nkind (Name) = N_Slice then
8732 Array_Type := Etype (Name);
8735 -- If name was overloaded, set slice type correctly now
8737 Set_Etype (N, Array_Type);
8739 -- If the range is specified by a subtype mark, no resolution is
8740 -- necessary. Else resolve the bounds, and apply needed checks.
8742 if not Is_Entity_Name (Drange) then
8743 Index := First_Index (Array_Type);
8744 Resolve (Drange, Base_Type (Etype (Index)));
8746 if Nkind (Drange) = N_Range then
8748 -- Ensure that side effects in the bounds are properly handled
8750 Force_Evaluation (Low_Bound (Drange));
8751 Force_Evaluation (High_Bound (Drange));
8753 -- Do not apply the range check to nodes associated with the
8754 -- frontend expansion of the dispatch table. We first check
8755 -- if Ada.Tags is already loaded to avoid the addition of an
8756 -- undesired dependence on such run-time unit.
8758 if not Tagged_Type_Expansion
8760 (RTU_Loaded (Ada_Tags)
8761 and then Nkind (Prefix (N)) = N_Selected_Component
8762 and then Present (Entity (Selector_Name (Prefix (N))))
8763 and then Entity (Selector_Name (Prefix (N))) =
8764 RTE_Record_Component (RE_Prims_Ptr))
8766 Apply_Range_Check (Drange, Etype (Index));
8771 Set_Slice_Subtype (N);
8773 -- Check bad use of type with predicates
8775 if Has_Predicates (Etype (Drange)) then
8776 Bad_Predicated_Subtype_Use
8777 ("subtype& has predicate, not allowed in slice",
8778 Drange, Etype (Drange));
8780 -- Otherwise here is where we check suspicious indexes
8782 elsif Nkind (Drange) = N_Range then
8783 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8784 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8790 ----------------------------
8791 -- Resolve_String_Literal --
8792 ----------------------------
8794 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8795 C_Typ : constant Entity_Id := Component_Type (Typ);
8796 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8797 Loc : constant Source_Ptr := Sloc (N);
8798 Str : constant String_Id := Strval (N);
8799 Strlen : constant Nat := String_Length (Str);
8800 Subtype_Id : Entity_Id;
8801 Need_Check : Boolean;
8804 -- For a string appearing in a concatenation, defer creation of the
8805 -- string_literal_subtype until the end of the resolution of the
8806 -- concatenation, because the literal may be constant-folded away. This
8807 -- is a useful optimization for long concatenation expressions.
8809 -- If the string is an aggregate built for a single character (which
8810 -- happens in a non-static context) or a is null string to which special
8811 -- checks may apply, we build the subtype. Wide strings must also get a
8812 -- string subtype if they come from a one character aggregate. Strings
8813 -- generated by attributes might be static, but it is often hard to
8814 -- determine whether the enclosing context is static, so we generate
8815 -- subtypes for them as well, thus losing some rarer optimizations ???
8816 -- Same for strings that come from a static conversion.
8819 (Strlen = 0 and then Typ /= Standard_String)
8820 or else Nkind (Parent (N)) /= N_Op_Concat
8821 or else (N /= Left_Opnd (Parent (N))
8822 and then N /= Right_Opnd (Parent (N)))
8823 or else ((Typ = Standard_Wide_String
8824 or else Typ = Standard_Wide_Wide_String)
8825 and then Nkind (Original_Node (N)) /= N_String_Literal);
8827 -- If the resolving type is itself a string literal subtype, we can just
8828 -- reuse it, since there is no point in creating another.
8830 if Ekind (Typ) = E_String_Literal_Subtype then
8833 elsif Nkind (Parent (N)) = N_Op_Concat
8834 and then not Need_Check
8835 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8836 N_Attribute_Reference,
8837 N_Qualified_Expression,
8842 -- Otherwise we must create a string literal subtype. Note that the
8843 -- whole idea of string literal subtypes is simply to avoid the need
8844 -- for building a full fledged array subtype for each literal.
8847 Set_String_Literal_Subtype (N, Typ);
8848 Subtype_Id := Etype (N);
8851 if Nkind (Parent (N)) /= N_Op_Concat
8854 Set_Etype (N, Subtype_Id);
8855 Eval_String_Literal (N);
8858 if Is_Limited_Composite (Typ)
8859 or else Is_Private_Composite (Typ)
8861 Error_Msg_N ("string literal not available for private array", N);
8862 Set_Etype (N, Any_Type);
8866 -- The validity of a null string has been checked in the call to
8867 -- Eval_String_Literal.
8872 -- Always accept string literal with component type Any_Character, which
8873 -- occurs in error situations and in comparisons of literals, both of
8874 -- which should accept all literals.
8876 elsif R_Typ = Any_Character then
8879 -- If the type is bit-packed, then we always transform the string
8880 -- literal into a full fledged aggregate.
8882 elsif Is_Bit_Packed_Array (Typ) then
8885 -- Deal with cases of Wide_Wide_String, Wide_String, and String
8888 -- For Standard.Wide_Wide_String, or any other type whose component
8889 -- type is Standard.Wide_Wide_Character, we know that all the
8890 -- characters in the string must be acceptable, since the parser
8891 -- accepted the characters as valid character literals.
8893 if R_Typ = Standard_Wide_Wide_Character then
8896 -- For the case of Standard.String, or any other type whose component
8897 -- type is Standard.Character, we must make sure that there are no
8898 -- wide characters in the string, i.e. that it is entirely composed
8899 -- of characters in range of type Character.
8901 -- If the string literal is the result of a static concatenation, the
8902 -- test has already been performed on the components, and need not be
8905 elsif R_Typ = Standard_Character
8906 and then Nkind (Original_Node (N)) /= N_Op_Concat
8908 for J in 1 .. Strlen loop
8909 if not In_Character_Range (Get_String_Char (Str, J)) then
8911 -- If we are out of range, post error. This is one of the
8912 -- very few places that we place the flag in the middle of
8913 -- a token, right under the offending wide character. Not
8914 -- quite clear if this is right wrt wide character encoding
8915 -- sequences, but it's only an error message!
8918 ("literal out of range of type Standard.Character",
8919 Source_Ptr (Int (Loc) + J));
8924 -- For the case of Standard.Wide_String, or any other type whose
8925 -- component type is Standard.Wide_Character, we must make sure that
8926 -- there are no wide characters in the string, i.e. that it is
8927 -- entirely composed of characters in range of type Wide_Character.
8929 -- If the string literal is the result of a static concatenation,
8930 -- the test has already been performed on the components, and need
8933 elsif R_Typ = Standard_Wide_Character
8934 and then Nkind (Original_Node (N)) /= N_Op_Concat
8936 for J in 1 .. Strlen loop
8937 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8939 -- If we are out of range, post error. This is one of the
8940 -- very few places that we place the flag in the middle of
8941 -- a token, right under the offending wide character.
8943 -- This is not quite right, because characters in general
8944 -- will take more than one character position ???
8947 ("literal out of range of type Standard.Wide_Character",
8948 Source_Ptr (Int (Loc) + J));
8953 -- If the root type is not a standard character, then we will convert
8954 -- the string into an aggregate and will let the aggregate code do
8955 -- the checking. Standard Wide_Wide_Character is also OK here.
8961 -- See if the component type of the array corresponding to the string
8962 -- has compile time known bounds. If yes we can directly check
8963 -- whether the evaluation of the string will raise constraint error.
8964 -- Otherwise we need to transform the string literal into the
8965 -- corresponding character aggregate and let the aggregate code do
8968 if Is_Standard_Character_Type (R_Typ) then
8970 -- Check for the case of full range, where we are definitely OK
8972 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8976 -- Here the range is not the complete base type range, so check
8979 Comp_Typ_Lo : constant Node_Id :=
8980 Type_Low_Bound (Component_Type (Typ));
8981 Comp_Typ_Hi : constant Node_Id :=
8982 Type_High_Bound (Component_Type (Typ));
8987 if Compile_Time_Known_Value (Comp_Typ_Lo)
8988 and then Compile_Time_Known_Value (Comp_Typ_Hi)
8990 for J in 1 .. Strlen loop
8991 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
8993 if Char_Val < Expr_Value (Comp_Typ_Lo)
8994 or else Char_Val > Expr_Value (Comp_Typ_Hi)
8996 Apply_Compile_Time_Constraint_Error
8997 (N, "character out of range?", CE_Range_Check_Failed,
8998 Loc => Source_Ptr (Int (Loc) + J));
9008 -- If we got here we meed to transform the string literal into the
9009 -- equivalent qualified positional array aggregate. This is rather
9010 -- heavy artillery for this situation, but it is hard work to avoid.
9013 Lits : constant List_Id := New_List;
9014 P : Source_Ptr := Loc + 1;
9018 -- Build the character literals, we give them source locations that
9019 -- correspond to the string positions, which is a bit tricky given
9020 -- the possible presence of wide character escape sequences.
9022 for J in 1 .. Strlen loop
9023 C := Get_String_Char (Str, J);
9024 Set_Character_Literal_Name (C);
9027 Make_Character_Literal (P,
9029 Char_Literal_Value => UI_From_CC (C)));
9031 if In_Character_Range (C) then
9034 -- Should we have a call to Skip_Wide here ???
9043 Make_Qualified_Expression (Loc,
9044 Subtype_Mark => New_Reference_To (Typ, Loc),
9046 Make_Aggregate (Loc, Expressions => Lits)));
9048 Analyze_And_Resolve (N, Typ);
9050 end Resolve_String_Literal;
9052 -----------------------------
9053 -- Resolve_Subprogram_Info --
9054 -----------------------------
9056 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9059 end Resolve_Subprogram_Info;
9061 -----------------------------
9062 -- Resolve_Type_Conversion --
9063 -----------------------------
9065 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9066 Conv_OK : constant Boolean := Conversion_OK (N);
9067 Operand : constant Node_Id := Expression (N);
9068 Operand_Typ : constant Entity_Id := Etype (Operand);
9069 Target_Typ : constant Entity_Id := Etype (N);
9074 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9075 -- Set to False to suppress cases where we want to suppress the test
9076 -- for redundancy to avoid possible false positives on this warning.
9080 and then not Valid_Conversion (N, Target_Typ, Operand)
9085 -- If the Operand Etype is Universal_Fixed, then the conversion is
9086 -- never redundant. We need this check because by the time we have
9087 -- finished the rather complex transformation, the conversion looks
9088 -- redundant when it is not.
9090 if Operand_Typ = Universal_Fixed then
9091 Test_Redundant := False;
9093 -- If the operand is marked as Any_Fixed, then special processing is
9094 -- required. This is also a case where we suppress the test for a
9095 -- redundant conversion, since most certainly it is not redundant.
9097 elsif Operand_Typ = Any_Fixed then
9098 Test_Redundant := False;
9100 -- Mixed-mode operation involving a literal. Context must be a fixed
9101 -- type which is applied to the literal subsequently.
9103 if Is_Fixed_Point_Type (Typ) then
9104 Set_Etype (Operand, Universal_Real);
9106 elsif Is_Numeric_Type (Typ)
9107 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9108 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9110 Etype (Left_Opnd (Operand)) = Universal_Real)
9112 -- Return if expression is ambiguous
9114 if Unique_Fixed_Point_Type (N) = Any_Type then
9117 -- If nothing else, the available fixed type is Duration
9120 Set_Etype (Operand, Standard_Duration);
9123 -- Resolve the real operand with largest available precision
9125 if Etype (Right_Opnd (Operand)) = Universal_Real then
9126 Rop := New_Copy_Tree (Right_Opnd (Operand));
9128 Rop := New_Copy_Tree (Left_Opnd (Operand));
9131 Resolve (Rop, Universal_Real);
9133 -- If the operand is a literal (it could be a non-static and
9134 -- illegal exponentiation) check whether the use of Duration
9135 -- is potentially inaccurate.
9137 if Nkind (Rop) = N_Real_Literal
9138 and then Realval (Rop) /= Ureal_0
9139 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9142 ("?universal real operand can only " &
9143 "be interpreted as Duration!",
9146 ("\?precision will be lost in the conversion!", Rop);
9149 elsif Is_Numeric_Type (Typ)
9150 and then Nkind (Operand) in N_Op
9151 and then Unique_Fixed_Point_Type (N) /= Any_Type
9153 Set_Etype (Operand, Standard_Duration);
9156 Error_Msg_N ("invalid context for mixed mode operation", N);
9157 Set_Etype (Operand, Any_Type);
9164 -- In SPARK, a type conversion between array types should be restricted
9165 -- to types which have matching static bounds.
9167 if Is_Array_Type (Target_Typ)
9168 and then Is_Array_Type (Operand_Typ)
9169 and then Operand_Typ /= Any_Composite -- or else Operand in error
9170 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9172 Check_SPARK_Restriction
9173 ("array types should have matching static bounds", N);
9176 -- In formal mode, the operand of an ancestor type conversion must be an
9177 -- object (not an expression).
9179 if Is_Tagged_Type (Target_Typ)
9180 and then not Is_Class_Wide_Type (Target_Typ)
9181 and then Is_Tagged_Type (Operand_Typ)
9182 and then not Is_Class_Wide_Type (Operand_Typ)
9183 and then Is_Ancestor (Target_Typ, Operand_Typ)
9184 and then not Is_SPARK_Object_Reference (Operand)
9186 Check_SPARK_Restriction ("object required", Operand);
9189 -- Note: we do the Eval_Type_Conversion call before applying the
9190 -- required checks for a subtype conversion. This is important, since
9191 -- both are prepared under certain circumstances to change the type
9192 -- conversion to a constraint error node, but in the case of
9193 -- Eval_Type_Conversion this may reflect an illegality in the static
9194 -- case, and we would miss the illegality (getting only a warning
9195 -- message), if we applied the type conversion checks first.
9197 Eval_Type_Conversion (N);
9199 -- Even when evaluation is not possible, we may be able to simplify the
9200 -- conversion or its expression. This needs to be done before applying
9201 -- checks, since otherwise the checks may use the original expression
9202 -- and defeat the simplifications. This is specifically the case for
9203 -- elimination of the floating-point Truncation attribute in
9204 -- float-to-int conversions.
9206 Simplify_Type_Conversion (N);
9208 -- If after evaluation we still have a type conversion, then we may need
9209 -- to apply checks required for a subtype conversion.
9211 -- Skip these type conversion checks if universal fixed operands
9212 -- operands involved, since range checks are handled separately for
9213 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9215 if Nkind (N) = N_Type_Conversion
9216 and then not Is_Generic_Type (Root_Type (Target_Typ))
9217 and then Target_Typ /= Universal_Fixed
9218 and then Operand_Typ /= Universal_Fixed
9220 Apply_Type_Conversion_Checks (N);
9223 -- Issue warning for conversion of simple object to its own type. We
9224 -- have to test the original nodes, since they may have been rewritten
9225 -- by various optimizations.
9227 Orig_N := Original_Node (N);
9229 -- Here we test for a redundant conversion if the warning mode is
9230 -- active (and was not locally reset), and we have a type conversion
9231 -- from source not appearing in a generic instance.
9234 and then Nkind (Orig_N) = N_Type_Conversion
9235 and then Comes_From_Source (Orig_N)
9236 and then not In_Instance
9238 Orig_N := Original_Node (Expression (Orig_N));
9239 Orig_T := Target_Typ;
9241 -- If the node is part of a larger expression, the Target_Type
9242 -- may not be the original type of the node if the context is a
9243 -- condition. Recover original type to see if conversion is needed.
9245 if Is_Boolean_Type (Orig_T)
9246 and then Nkind (Parent (N)) in N_Op
9248 Orig_T := Etype (Parent (N));
9251 -- If we have an entity name, then give the warning if the entity
9252 -- is the right type, or if it is a loop parameter covered by the
9253 -- original type (that's needed because loop parameters have an
9254 -- odd subtype coming from the bounds).
9256 if (Is_Entity_Name (Orig_N)
9258 (Etype (Entity (Orig_N)) = Orig_T
9260 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9261 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9263 -- If not an entity, then type of expression must match
9265 or else Etype (Orig_N) = Orig_T
9267 -- One more check, do not give warning if the analyzed conversion
9268 -- has an expression with non-static bounds, and the bounds of the
9269 -- target are static. This avoids junk warnings in cases where the
9270 -- conversion is necessary to establish staticness, for example in
9271 -- a case statement.
9273 if not Is_OK_Static_Subtype (Operand_Typ)
9274 and then Is_OK_Static_Subtype (Target_Typ)
9278 -- Finally, if this type conversion occurs in a context requiring
9279 -- a prefix, and the expression is a qualified expression then the
9280 -- type conversion is not redundant, since a qualified expression
9281 -- is not a prefix, whereas a type conversion is. For example, "X
9282 -- := T'(Funx(...)).Y;" is illegal because a selected component
9283 -- requires a prefix, but a type conversion makes it legal: "X :=
9284 -- T(T'(Funx(...))).Y;"
9286 -- In Ada 2012, a qualified expression is a name, so this idiom is
9287 -- no longer needed, but we still suppress the warning because it
9288 -- seems unfriendly for warnings to pop up when you switch to the
9289 -- newer language version.
9291 elsif Nkind (Orig_N) = N_Qualified_Expression
9292 and then Nkind_In (Parent (N), N_Attribute_Reference,
9293 N_Indexed_Component,
9294 N_Selected_Component,
9296 N_Explicit_Dereference)
9300 -- Here we give the redundant conversion warning. If it is an
9301 -- entity, give the name of the entity in the message. If not,
9302 -- just mention the expression.
9305 if Is_Entity_Name (Orig_N) then
9306 Error_Msg_Node_2 := Orig_T;
9307 Error_Msg_NE -- CODEFIX
9308 ("?redundant conversion, & is of type &!",
9309 N, Entity (Orig_N));
9312 ("?redundant conversion, expression is of type&!",
9319 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9320 -- No need to perform any interface conversion if the type of the
9321 -- expression coincides with the target type.
9323 if Ada_Version >= Ada_2005
9324 and then Expander_Active
9325 and then Operand_Typ /= Target_Typ
9328 Opnd : Entity_Id := Operand_Typ;
9329 Target : Entity_Id := Target_Typ;
9332 if Is_Access_Type (Opnd) then
9333 Opnd := Designated_Type (Opnd);
9336 if Is_Access_Type (Target_Typ) then
9337 Target := Designated_Type (Target);
9340 if Opnd = Target then
9343 -- Conversion from interface type
9345 elsif Is_Interface (Opnd) then
9347 -- Ada 2005 (AI-217): Handle entities from limited views
9349 if From_With_Type (Opnd) then
9350 Error_Msg_Qual_Level := 99;
9351 Error_Msg_NE -- CODEFIX
9352 ("missing WITH clause on package &", N,
9353 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9355 ("type conversions require visibility of the full view",
9358 elsif From_With_Type (Target)
9360 (Is_Access_Type (Target_Typ)
9361 and then Present (Non_Limited_View (Etype (Target))))
9363 Error_Msg_Qual_Level := 99;
9364 Error_Msg_NE -- CODEFIX
9365 ("missing WITH clause on package &", N,
9366 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9368 ("type conversions require visibility of the full view",
9372 Expand_Interface_Conversion (N, Is_Static => False);
9375 -- Conversion to interface type
9377 elsif Is_Interface (Target) then
9381 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9382 Opnd := Etype (Opnd);
9385 if not Interface_Present_In_Ancestor
9389 if Is_Class_Wide_Type (Opnd) then
9391 -- The static analysis is not enough to know if the
9392 -- interface is implemented or not. Hence we must pass
9393 -- the work to the expander to generate code to evaluate
9394 -- the conversion at run time.
9396 Expand_Interface_Conversion (N, Is_Static => False);
9399 Error_Msg_Name_1 := Chars (Etype (Target));
9400 Error_Msg_Name_2 := Chars (Opnd);
9402 ("wrong interface conversion (% is not a progenitor " &
9407 Expand_Interface_Conversion (N);
9412 end Resolve_Type_Conversion;
9414 ----------------------
9415 -- Resolve_Unary_Op --
9416 ----------------------
9418 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9419 B_Typ : constant Entity_Id := Base_Type (Typ);
9420 R : constant Node_Id := Right_Opnd (N);
9426 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9427 Error_Msg_Name_1 := Chars (Typ);
9428 Check_SPARK_Restriction
9429 ("unary operator not defined for modular type%", N);
9432 -- Deal with intrinsic unary operators
9434 if Comes_From_Source (N)
9435 and then Ekind (Entity (N)) = E_Function
9436 and then Is_Imported (Entity (N))
9437 and then Is_Intrinsic_Subprogram (Entity (N))
9439 Resolve_Intrinsic_Unary_Operator (N, Typ);
9443 -- Deal with universal cases
9445 if Etype (R) = Universal_Integer
9447 Etype (R) = Universal_Real
9449 Check_For_Visible_Operator (N, B_Typ);
9452 Set_Etype (N, B_Typ);
9455 -- Generate warning for expressions like abs (x mod 2)
9457 if Warn_On_Redundant_Constructs
9458 and then Nkind (N) = N_Op_Abs
9460 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9462 if OK and then Hi >= Lo and then Lo >= 0 then
9463 Error_Msg_N -- CODEFIX
9464 ("?abs applied to known non-negative value has no effect", N);
9468 -- Deal with reference generation
9470 Check_Unset_Reference (R);
9471 Generate_Operator_Reference (N, B_Typ);
9474 -- Set overflow checking bit. Much cleverer code needed here eventually
9475 -- and perhaps the Resolve routines should be separated for the various
9476 -- arithmetic operations, since they will need different processing ???
9478 if Nkind (N) in N_Op then
9479 if not Overflow_Checks_Suppressed (Etype (N)) then
9480 Enable_Overflow_Check (N);
9484 -- Generate warning for expressions like -5 mod 3 for integers. No need
9485 -- to worry in the floating-point case, since parens do not affect the
9486 -- result so there is no point in giving in a warning.
9489 Norig : constant Node_Id := Original_Node (N);
9498 if Warn_On_Questionable_Missing_Parens
9499 and then Comes_From_Source (Norig)
9500 and then Is_Integer_Type (Typ)
9501 and then Nkind (Norig) = N_Op_Minus
9503 Rorig := Original_Node (Right_Opnd (Norig));
9505 -- We are looking for cases where the right operand is not
9506 -- parenthesized, and is a binary operator, multiply, divide, or
9507 -- mod. These are the cases where the grouping can affect results.
9509 if Paren_Count (Rorig) = 0
9510 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9512 -- For mod, we always give the warning, since the value is
9513 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9514 -- -(5 mod 315)). But for the other cases, the only concern is
9515 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9516 -- overflows, but (-2) * 64 does not). So we try to give the
9517 -- message only when overflow is possible.
9519 if Nkind (Rorig) /= N_Op_Mod
9520 and then Compile_Time_Known_Value (R)
9522 Val := Expr_Value (R);
9524 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9525 HB := Expr_Value (Type_High_Bound (Typ));
9527 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9530 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9531 LB := Expr_Value (Type_Low_Bound (Typ));
9533 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9536 -- Note that the test below is deliberately excluding the
9537 -- largest negative number, since that is a potentially
9538 -- troublesome case (e.g. -2 * x, where the result is the
9539 -- largest negative integer has an overflow with 2 * x).
9541 if Val > LB and then Val <= HB then
9546 -- For the multiplication case, the only case we have to worry
9547 -- about is when (-a)*b is exactly the largest negative number
9548 -- so that -(a*b) can cause overflow. This can only happen if
9549 -- a is a power of 2, and more generally if any operand is a
9550 -- constant that is not a power of 2, then the parentheses
9551 -- cannot affect whether overflow occurs. We only bother to
9552 -- test the left most operand
9554 -- Loop looking at left operands for one that has known value
9557 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9558 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9559 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9561 -- Operand value of 0 or 1 skips warning
9566 -- Otherwise check power of 2, if power of 2, warn, if
9567 -- anything else, skip warning.
9570 while Lval /= 2 loop
9571 if Lval mod 2 = 1 then
9582 -- Keep looking at left operands
9584 Opnd := Left_Opnd (Opnd);
9587 -- For rem or "/" we can only have a problematic situation
9588 -- if the divisor has a value of minus one or one. Otherwise
9589 -- overflow is impossible (divisor > 1) or we have a case of
9590 -- division by zero in any case.
9592 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9593 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9594 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9599 -- If we fall through warning should be issued
9602 ("?unary minus expression should be parenthesized here!", N);
9606 end Resolve_Unary_Op;
9608 ----------------------------------
9609 -- Resolve_Unchecked_Expression --
9610 ----------------------------------
9612 procedure Resolve_Unchecked_Expression
9617 Resolve (Expression (N), Typ, Suppress => All_Checks);
9619 end Resolve_Unchecked_Expression;
9621 ---------------------------------------
9622 -- Resolve_Unchecked_Type_Conversion --
9623 ---------------------------------------
9625 procedure Resolve_Unchecked_Type_Conversion
9629 pragma Warnings (Off, Typ);
9631 Operand : constant Node_Id := Expression (N);
9632 Opnd_Type : constant Entity_Id := Etype (Operand);
9635 -- Resolve operand using its own type
9637 Resolve (Operand, Opnd_Type);
9638 Eval_Unchecked_Conversion (N);
9639 end Resolve_Unchecked_Type_Conversion;
9641 ------------------------------
9642 -- Rewrite_Operator_As_Call --
9643 ------------------------------
9645 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9646 Loc : constant Source_Ptr := Sloc (N);
9647 Actuals : constant List_Id := New_List;
9651 if Nkind (N) in N_Binary_Op then
9652 Append (Left_Opnd (N), Actuals);
9655 Append (Right_Opnd (N), Actuals);
9658 Make_Function_Call (Sloc => Loc,
9659 Name => New_Occurrence_Of (Nam, Loc),
9660 Parameter_Associations => Actuals);
9662 Preserve_Comes_From_Source (New_N, N);
9663 Preserve_Comes_From_Source (Name (New_N), N);
9665 Set_Etype (N, Etype (Nam));
9666 end Rewrite_Operator_As_Call;
9668 ------------------------------
9669 -- Rewrite_Renamed_Operator --
9670 ------------------------------
9672 procedure Rewrite_Renamed_Operator
9677 Nam : constant Name_Id := Chars (Op);
9678 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9682 -- Rewrite the operator node using the real operator, not its renaming.
9683 -- Exclude user-defined intrinsic operations of the same name, which are
9684 -- treated separately and rewritten as calls.
9686 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9687 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9688 Set_Chars (Op_Node, Nam);
9689 Set_Etype (Op_Node, Etype (N));
9690 Set_Entity (Op_Node, Op);
9691 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9693 -- Indicate that both the original entity and its renaming are
9694 -- referenced at this point.
9696 Generate_Reference (Entity (N), N);
9697 Generate_Reference (Op, N);
9700 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9703 Rewrite (N, Op_Node);
9705 -- If the context type is private, add the appropriate conversions so
9706 -- that the operator is applied to the full view. This is done in the
9707 -- routines that resolve intrinsic operators.
9709 if Is_Intrinsic_Subprogram (Op)
9710 and then Is_Private_Type (Typ)
9713 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9714 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9715 Resolve_Intrinsic_Operator (N, Typ);
9717 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9718 Resolve_Intrinsic_Unary_Operator (N, Typ);
9725 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9727 -- Operator renames a user-defined operator of the same name. Use the
9728 -- original operator in the node, which is the one Gigi knows about.
9731 Set_Is_Overloaded (N, False);
9733 end Rewrite_Renamed_Operator;
9735 -----------------------
9736 -- Set_Slice_Subtype --
9737 -----------------------
9739 -- Build an implicit subtype declaration to represent the type delivered by
9740 -- the slice. This is an abbreviated version of an array subtype. We define
9741 -- an index subtype for the slice, using either the subtype name or the
9742 -- discrete range of the slice. To be consistent with index usage elsewhere
9743 -- we create a list header to hold the single index. This list is not
9744 -- otherwise attached to the syntax tree.
9746 procedure Set_Slice_Subtype (N : Node_Id) is
9747 Loc : constant Source_Ptr := Sloc (N);
9748 Index_List : constant List_Id := New_List;
9750 Index_Subtype : Entity_Id;
9751 Index_Type : Entity_Id;
9752 Slice_Subtype : Entity_Id;
9753 Drange : constant Node_Id := Discrete_Range (N);
9756 if Is_Entity_Name (Drange) then
9757 Index_Subtype := Entity (Drange);
9760 -- We force the evaluation of a range. This is definitely needed in
9761 -- the renamed case, and seems safer to do unconditionally. Note in
9762 -- any case that since we will create and insert an Itype referring
9763 -- to this range, we must make sure any side effect removal actions
9764 -- are inserted before the Itype definition.
9766 if Nkind (Drange) = N_Range then
9767 Force_Evaluation (Low_Bound (Drange));
9768 Force_Evaluation (High_Bound (Drange));
9771 Index_Type := Base_Type (Etype (Drange));
9773 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9775 -- Take a new copy of Drange (where bounds have been rewritten to
9776 -- reference side-effect-free names). Using a separate tree ensures
9777 -- that further expansion (e.g. while rewriting a slice assignment
9778 -- into a FOR loop) does not attempt to remove side effects on the
9779 -- bounds again (which would cause the bounds in the index subtype
9780 -- definition to refer to temporaries before they are defined) (the
9781 -- reason is that some names are considered side effect free here
9782 -- for the subtype, but not in the context of a loop iteration
9785 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9786 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
9787 Set_Etype (Index_Subtype, Index_Type);
9788 Set_Size_Info (Index_Subtype, Index_Type);
9789 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9792 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9794 Index := New_Occurrence_Of (Index_Subtype, Loc);
9795 Set_Etype (Index, Index_Subtype);
9796 Append (Index, Index_List);
9798 Set_First_Index (Slice_Subtype, Index);
9799 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9800 Set_Is_Constrained (Slice_Subtype, True);
9802 Check_Compile_Time_Size (Slice_Subtype);
9804 -- The Etype of the existing Slice node is reset to this slice subtype.
9805 -- Its bounds are obtained from its first index.
9807 Set_Etype (N, Slice_Subtype);
9809 -- For packed slice subtypes, freeze immediately (except in the case of
9810 -- being in a "spec expression" where we never freeze when we first see
9813 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9814 Freeze_Itype (Slice_Subtype, N);
9816 -- For all other cases insert an itype reference in the slice's actions
9817 -- so that the itype is frozen at the proper place in the tree (i.e. at
9818 -- the point where actions for the slice are analyzed). Note that this
9819 -- is different from freezing the itype immediately, which might be
9820 -- premature (e.g. if the slice is within a transient scope). This needs
9821 -- to be done only if expansion is enabled.
9823 elsif Expander_Active then
9824 Ensure_Defined (Typ => Slice_Subtype, N => N);
9826 end Set_Slice_Subtype;
9828 --------------------------------
9829 -- Set_String_Literal_Subtype --
9830 --------------------------------
9832 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9833 Loc : constant Source_Ptr := Sloc (N);
9834 Low_Bound : constant Node_Id :=
9835 Type_Low_Bound (Etype (First_Index (Typ)));
9836 Subtype_Id : Entity_Id;
9839 if Nkind (N) /= N_String_Literal then
9843 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
9844 Set_String_Literal_Length (Subtype_Id, UI_From_Int
9845 (String_Length (Strval (N))));
9846 Set_Etype (Subtype_Id, Base_Type (Typ));
9847 Set_Is_Constrained (Subtype_Id);
9848 Set_Etype (N, Subtype_Id);
9850 if Is_OK_Static_Expression (Low_Bound) then
9852 -- The low bound is set from the low bound of the corresponding index
9853 -- type. Note that we do not store the high bound in the string literal
9854 -- subtype, but it can be deduced if necessary from the length and the
9857 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
9860 Set_String_Literal_Low_Bound
9861 (Subtype_Id, Make_Integer_Literal (Loc, 1));
9862 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
9864 -- Build bona fide subtype for the string, and wrap it in an
9865 -- unchecked conversion, because the backend expects the
9866 -- String_Literal_Subtype to have a static lower bound.
9869 Index_List : constant List_Id := New_List;
9870 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
9871 High_Bound : constant Node_Id :=
9873 Left_Opnd => New_Copy_Tree (Low_Bound),
9875 Make_Integer_Literal (Loc,
9876 String_Length (Strval (N)) - 1));
9877 Array_Subtype : Entity_Id;
9878 Index_Subtype : Entity_Id;
9884 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9885 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
9886 Set_Scalar_Range (Index_Subtype, Drange);
9887 Set_Parent (Drange, N);
9888 Analyze_And_Resolve (Drange, Index_Type);
9890 -- In the context, the Index_Type may already have a constraint,
9891 -- so use common base type on string subtype. The base type may
9892 -- be used when generating attributes of the string, for example
9893 -- in the context of a slice assignment.
9895 Set_Etype (Index_Subtype, Base_Type (Index_Type));
9896 Set_Size_Info (Index_Subtype, Index_Type);
9897 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9899 Array_Subtype := Create_Itype (E_Array_Subtype, N);
9901 Index := New_Occurrence_Of (Index_Subtype, Loc);
9902 Set_Etype (Index, Index_Subtype);
9903 Append (Index, Index_List);
9905 Set_First_Index (Array_Subtype, Index);
9906 Set_Etype (Array_Subtype, Base_Type (Typ));
9907 Set_Is_Constrained (Array_Subtype, True);
9910 Make_Unchecked_Type_Conversion (Loc,
9911 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9912 Expression => Relocate_Node (N)));
9913 Set_Etype (N, Array_Subtype);
9916 end Set_String_Literal_Subtype;
9918 ------------------------------
9919 -- Simplify_Type_Conversion --
9920 ------------------------------
9922 procedure Simplify_Type_Conversion (N : Node_Id) is
9924 if Nkind (N) = N_Type_Conversion then
9926 Operand : constant Node_Id := Expression (N);
9927 Target_Typ : constant Entity_Id := Etype (N);
9928 Opnd_Typ : constant Entity_Id := Etype (Operand);
9931 if Is_Floating_Point_Type (Opnd_Typ)
9933 (Is_Integer_Type (Target_Typ)
9934 or else (Is_Fixed_Point_Type (Target_Typ)
9935 and then Conversion_OK (N)))
9936 and then Nkind (Operand) = N_Attribute_Reference
9937 and then Attribute_Name (Operand) = Name_Truncation
9939 -- Special processing required if the conversion is the expression
9940 -- of a Truncation attribute reference. In this case we replace:
9942 -- ityp (ftyp'Truncation (x))
9948 -- with the Float_Truncate flag set, which is more efficient.
9952 Relocate_Node (First (Expressions (Operand))));
9953 Set_Float_Truncate (N, True);
9957 end Simplify_Type_Conversion;
9959 -----------------------------
9960 -- Unique_Fixed_Point_Type --
9961 -----------------------------
9963 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
9964 T1 : Entity_Id := Empty;
9969 procedure Fixed_Point_Error;
9970 -- Give error messages for true ambiguity. Messages are posted on node
9971 -- N, and entities T1, T2 are the possible interpretations.
9973 -----------------------
9974 -- Fixed_Point_Error --
9975 -----------------------
9977 procedure Fixed_Point_Error is
9979 Error_Msg_N ("ambiguous universal_fixed_expression", N);
9980 Error_Msg_NE ("\\possible interpretation as}", N, T1);
9981 Error_Msg_NE ("\\possible interpretation as}", N, T2);
9982 end Fixed_Point_Error;
9984 -- Start of processing for Unique_Fixed_Point_Type
9987 -- The operations on Duration are visible, so Duration is always a
9988 -- possible interpretation.
9990 T1 := Standard_Duration;
9992 -- Look for fixed-point types in enclosing scopes
9994 Scop := Current_Scope;
9995 while Scop /= Standard_Standard loop
9996 T2 := First_Entity (Scop);
9997 while Present (T2) loop
9998 if Is_Fixed_Point_Type (T2)
9999 and then Current_Entity (T2) = T2
10000 and then Scope (Base_Type (T2)) = Scop
10002 if Present (T1) then
10013 Scop := Scope (Scop);
10016 -- Look for visible fixed type declarations in the context
10018 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10019 while Present (Item) loop
10020 if Nkind (Item) = N_With_Clause then
10021 Scop := Entity (Name (Item));
10022 T2 := First_Entity (Scop);
10023 while Present (T2) loop
10024 if Is_Fixed_Point_Type (T2)
10025 and then Scope (Base_Type (T2)) = Scop
10026 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10028 if Present (T1) then
10043 if Nkind (N) = N_Real_Literal then
10044 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
10046 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
10050 end Unique_Fixed_Point_Type;
10052 ----------------------
10053 -- Valid_Conversion --
10054 ----------------------
10056 function Valid_Conversion
10058 Target : Entity_Id;
10059 Operand : Node_Id) return Boolean
10061 Target_Type : constant Entity_Id := Base_Type (Target);
10062 Opnd_Type : Entity_Id := Etype (Operand);
10064 function Conversion_Check
10066 Msg : String) return Boolean;
10067 -- Little routine to post Msg if Valid is False, returns Valid value
10069 function Valid_Tagged_Conversion
10070 (Target_Type : Entity_Id;
10071 Opnd_Type : Entity_Id) return Boolean;
10072 -- Specifically test for validity of tagged conversions
10074 function Valid_Array_Conversion return Boolean;
10075 -- Check index and component conformance, and accessibility levels if
10076 -- the component types are anonymous access types (Ada 2005).
10078 ----------------------
10079 -- Conversion_Check --
10080 ----------------------
10082 function Conversion_Check
10084 Msg : String) return Boolean
10088 Error_Msg_N (Msg, Operand);
10092 end Conversion_Check;
10094 ----------------------------
10095 -- Valid_Array_Conversion --
10096 ----------------------------
10098 function Valid_Array_Conversion return Boolean
10100 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10101 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10103 Opnd_Index : Node_Id;
10104 Opnd_Index_Type : Entity_Id;
10106 Target_Comp_Type : constant Entity_Id :=
10107 Component_Type (Target_Type);
10108 Target_Comp_Base : constant Entity_Id :=
10109 Base_Type (Target_Comp_Type);
10111 Target_Index : Node_Id;
10112 Target_Index_Type : Entity_Id;
10115 -- Error if wrong number of dimensions
10118 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10121 ("incompatible number of dimensions for conversion", Operand);
10124 -- Number of dimensions matches
10127 -- Loop through indexes of the two arrays
10129 Target_Index := First_Index (Target_Type);
10130 Opnd_Index := First_Index (Opnd_Type);
10131 while Present (Target_Index) and then Present (Opnd_Index) loop
10132 Target_Index_Type := Etype (Target_Index);
10133 Opnd_Index_Type := Etype (Opnd_Index);
10135 -- Error if index types are incompatible
10137 if not (Is_Integer_Type (Target_Index_Type)
10138 and then Is_Integer_Type (Opnd_Index_Type))
10139 and then (Root_Type (Target_Index_Type)
10140 /= Root_Type (Opnd_Index_Type))
10143 ("incompatible index types for array conversion",
10148 Next_Index (Target_Index);
10149 Next_Index (Opnd_Index);
10152 -- If component types have same base type, all set
10154 if Target_Comp_Base = Opnd_Comp_Base then
10157 -- Here if base types of components are not the same. The only
10158 -- time this is allowed is if we have anonymous access types.
10160 -- The conversion of arrays of anonymous access types can lead
10161 -- to dangling pointers. AI-392 formalizes the accessibility
10162 -- checks that must be applied to such conversions to prevent
10163 -- out-of-scope references.
10166 (Target_Comp_Base, E_Anonymous_Access_Type,
10167 E_Anonymous_Access_Subprogram_Type)
10168 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10170 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10172 if Type_Access_Level (Target_Type) <
10173 Type_Access_Level (Opnd_Type)
10175 if In_Instance_Body then
10176 Error_Msg_N ("?source array type " &
10177 "has deeper accessibility level than target", Operand);
10178 Error_Msg_N ("\?Program_Error will be raised at run time",
10181 Make_Raise_Program_Error (Sloc (N),
10182 Reason => PE_Accessibility_Check_Failed));
10183 Set_Etype (N, Target_Type);
10186 -- Conversion not allowed because of accessibility levels
10189 Error_Msg_N ("source array type " &
10190 "has deeper accessibility level than target", Operand);
10198 -- All other cases where component base types do not match
10202 ("incompatible component types for array conversion",
10207 -- Check that component subtypes statically match. For numeric
10208 -- types this means that both must be either constrained or
10209 -- unconstrained. For enumeration types the bounds must match.
10210 -- All of this is checked in Subtypes_Statically_Match.
10212 if not Subtypes_Statically_Match
10213 (Target_Comp_Type, Opnd_Comp_Type)
10216 ("component subtypes must statically match", Operand);
10222 end Valid_Array_Conversion;
10224 -----------------------------
10225 -- Valid_Tagged_Conversion --
10226 -----------------------------
10228 function Valid_Tagged_Conversion
10229 (Target_Type : Entity_Id;
10230 Opnd_Type : Entity_Id) return Boolean
10233 -- Upward conversions are allowed (RM 4.6(22))
10235 if Covers (Target_Type, Opnd_Type)
10236 or else Is_Ancestor (Target_Type, Opnd_Type)
10240 -- Downward conversion are allowed if the operand is class-wide
10243 elsif Is_Class_Wide_Type (Opnd_Type)
10244 and then Covers (Opnd_Type, Target_Type)
10248 elsif Covers (Opnd_Type, Target_Type)
10249 or else Is_Ancestor (Opnd_Type, Target_Type)
10252 Conversion_Check (False,
10253 "downward conversion of tagged objects not allowed");
10255 -- Ada 2005 (AI-251): The conversion to/from interface types is
10258 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10261 -- If the operand is a class-wide type obtained through a limited_
10262 -- with clause, and the context includes the non-limited view, use
10263 -- it to determine whether the conversion is legal.
10265 elsif Is_Class_Wide_Type (Opnd_Type)
10266 and then From_With_Type (Opnd_Type)
10267 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10268 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10272 elsif Is_Access_Type (Opnd_Type)
10273 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10279 ("invalid tagged conversion, not compatible with}",
10280 N, First_Subtype (Opnd_Type));
10283 end Valid_Tagged_Conversion;
10285 -- Start of processing for Valid_Conversion
10288 Check_Parameterless_Call (Operand);
10290 if Is_Overloaded (Operand) then
10300 -- Remove procedure calls, which syntactically cannot appear in
10301 -- this context, but which cannot be removed by type checking,
10302 -- because the context does not impose a type.
10304 -- When compiling for VMS, spurious ambiguities can be produced
10305 -- when arithmetic operations have a literal operand and return
10306 -- System.Address or a descendant of it. These ambiguities are
10307 -- otherwise resolved by the context, but for conversions there
10308 -- is no context type and the removal of the spurious operations
10309 -- must be done explicitly here.
10311 -- The node may be labelled overloaded, but still contain only one
10312 -- interpretation because others were discarded earlier. If this
10313 -- is the case, retain the single interpretation if legal.
10315 Get_First_Interp (Operand, I, It);
10316 Opnd_Type := It.Typ;
10317 Get_Next_Interp (I, It);
10319 if Present (It.Typ)
10320 and then Opnd_Type /= Standard_Void_Type
10322 -- More than one candidate interpretation is available
10324 Get_First_Interp (Operand, I, It);
10325 while Present (It.Typ) loop
10326 if It.Typ = Standard_Void_Type then
10330 if Present (System_Aux_Id)
10331 and then Is_Descendent_Of_Address (It.Typ)
10336 Get_Next_Interp (I, It);
10340 Get_First_Interp (Operand, I, It);
10344 if No (It.Typ) then
10345 Error_Msg_N ("illegal operand in conversion", Operand);
10349 Get_Next_Interp (I, It);
10351 if Present (It.Typ) then
10354 It1 := Disambiguate (Operand, I1, I, Any_Type);
10356 if It1 = No_Interp then
10357 Error_Msg_N ("ambiguous operand in conversion", Operand);
10359 -- If the interpretation involves a standard operator, use
10360 -- the location of the type, which may be user-defined.
10362 if Sloc (It.Nam) = Standard_Location then
10363 Error_Msg_Sloc := Sloc (It.Typ);
10365 Error_Msg_Sloc := Sloc (It.Nam);
10368 Error_Msg_N -- CODEFIX
10369 ("\\possible interpretation#!", Operand);
10371 if Sloc (N1) = Standard_Location then
10372 Error_Msg_Sloc := Sloc (T1);
10374 Error_Msg_Sloc := Sloc (N1);
10377 Error_Msg_N -- CODEFIX
10378 ("\\possible interpretation#!", Operand);
10384 Set_Etype (Operand, It1.Typ);
10385 Opnd_Type := It1.Typ;
10391 if Is_Numeric_Type (Target_Type) then
10393 -- A universal fixed expression can be converted to any numeric type
10395 if Opnd_Type = Universal_Fixed then
10398 -- Also no need to check when in an instance or inlined body, because
10399 -- the legality has been established when the template was analyzed.
10400 -- Furthermore, numeric conversions may occur where only a private
10401 -- view of the operand type is visible at the instantiation point.
10402 -- This results in a spurious error if we check that the operand type
10403 -- is a numeric type.
10405 -- Note: in a previous version of this unit, the following tests were
10406 -- applied only for generated code (Comes_From_Source set to False),
10407 -- but in fact the test is required for source code as well, since
10408 -- this situation can arise in source code.
10410 elsif In_Instance or else In_Inlined_Body then
10413 -- Otherwise we need the conversion check
10416 return Conversion_Check
10417 (Is_Numeric_Type (Opnd_Type),
10418 "illegal operand for numeric conversion");
10423 elsif Is_Array_Type (Target_Type) then
10424 if not Is_Array_Type (Opnd_Type)
10425 or else Opnd_Type = Any_Composite
10426 or else Opnd_Type = Any_String
10428 Error_Msg_N ("illegal operand for array conversion", Operand);
10431 return Valid_Array_Conversion;
10434 -- Ada 2005 (AI-251): Anonymous access types where target references an
10437 elsif Ekind_In (Target_Type, E_General_Access_Type,
10438 E_Anonymous_Access_Type)
10439 and then Is_Interface (Directly_Designated_Type (Target_Type))
10441 -- Check the static accessibility rule of 4.6(17). Note that the
10442 -- check is not enforced when within an instance body, since the
10443 -- RM requires such cases to be caught at run time.
10445 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10446 if Type_Access_Level (Opnd_Type) >
10447 Type_Access_Level (Target_Type)
10449 -- In an instance, this is a run-time check, but one we know
10450 -- will fail, so generate an appropriate warning. The raise
10451 -- will be generated by Expand_N_Type_Conversion.
10453 if In_Instance_Body then
10455 ("?cannot convert local pointer to non-local access type",
10458 ("\?Program_Error will be raised at run time", Operand);
10461 ("cannot convert local pointer to non-local access type",
10466 -- Special accessibility checks are needed in the case of access
10467 -- discriminants declared for a limited type.
10469 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10470 and then not Is_Local_Anonymous_Access (Opnd_Type)
10472 -- When the operand is a selected access discriminant the check
10473 -- needs to be made against the level of the object denoted by
10474 -- the prefix of the selected name (Object_Access_Level handles
10475 -- checking the prefix of the operand for this case).
10477 if Nkind (Operand) = N_Selected_Component
10478 and then Object_Access_Level (Operand) >
10479 Type_Access_Level (Target_Type)
10481 -- In an instance, this is a run-time check, but one we know
10482 -- will fail, so generate an appropriate warning. The raise
10483 -- will be generated by Expand_N_Type_Conversion.
10485 if In_Instance_Body then
10487 ("?cannot convert access discriminant to non-local" &
10488 " access type", Operand);
10490 ("\?Program_Error will be raised at run time", Operand);
10493 ("cannot convert access discriminant to non-local" &
10494 " access type", Operand);
10499 -- The case of a reference to an access discriminant from
10500 -- within a limited type declaration (which will appear as
10501 -- a discriminal) is always illegal because the level of the
10502 -- discriminant is considered to be deeper than any (nameable)
10505 if Is_Entity_Name (Operand)
10506 and then not Is_Local_Anonymous_Access (Opnd_Type)
10508 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10509 and then Present (Discriminal_Link (Entity (Operand)))
10512 ("discriminant has deeper accessibility level than target",
10521 -- General and anonymous access types
10523 elsif Ekind_In (Target_Type, E_General_Access_Type,
10524 E_Anonymous_Access_Type)
10527 (Is_Access_Type (Opnd_Type)
10529 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10530 E_Access_Protected_Subprogram_Type),
10531 "must be an access-to-object type")
10533 if Is_Access_Constant (Opnd_Type)
10534 and then not Is_Access_Constant (Target_Type)
10537 ("access-to-constant operand type not allowed", Operand);
10541 -- Check the static accessibility rule of 4.6(17). Note that the
10542 -- check is not enforced when within an instance body, since the RM
10543 -- requires such cases to be caught at run time.
10545 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10546 or else Is_Local_Anonymous_Access (Target_Type)
10548 if Type_Access_Level (Opnd_Type)
10549 > Type_Access_Level (Target_Type)
10551 -- In an instance, this is a run-time check, but one we know
10552 -- will fail, so generate an appropriate warning. The raise
10553 -- will be generated by Expand_N_Type_Conversion.
10555 if In_Instance_Body then
10557 ("?cannot convert local pointer to non-local access type",
10560 ("\?Program_Error will be raised at run time", Operand);
10563 -- Avoid generation of spurious error message
10565 if not Error_Posted (N) then
10567 ("cannot convert local pointer to non-local access type",
10574 -- Special accessibility checks are needed in the case of access
10575 -- discriminants declared for a limited type.
10577 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10578 and then not Is_Local_Anonymous_Access (Opnd_Type)
10580 -- When the operand is a selected access discriminant the check
10581 -- needs to be made against the level of the object denoted by
10582 -- the prefix of the selected name (Object_Access_Level handles
10583 -- checking the prefix of the operand for this case).
10585 if Nkind (Operand) = N_Selected_Component
10586 and then Object_Access_Level (Operand) >
10587 Type_Access_Level (Target_Type)
10589 -- In an instance, this is a run-time check, but one we know
10590 -- will fail, so generate an appropriate warning. The raise
10591 -- will be generated by Expand_N_Type_Conversion.
10593 if In_Instance_Body then
10595 ("?cannot convert access discriminant to non-local" &
10596 " access type", Operand);
10598 ("\?Program_Error will be raised at run time",
10603 ("cannot convert access discriminant to non-local" &
10604 " access type", Operand);
10609 -- The case of a reference to an access discriminant from
10610 -- within a limited type declaration (which will appear as
10611 -- a discriminal) is always illegal because the level of the
10612 -- discriminant is considered to be deeper than any (nameable)
10615 if Is_Entity_Name (Operand)
10617 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10618 and then Present (Discriminal_Link (Entity (Operand)))
10621 ("discriminant has deeper accessibility level than target",
10628 -- In the presence of limited_with clauses we have to use non-limited
10629 -- views, if available.
10631 Check_Limited : declare
10632 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10633 -- Helper function to handle limited views
10635 --------------------------
10636 -- Full_Designated_Type --
10637 --------------------------
10639 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10640 Desig : constant Entity_Id := Designated_Type (T);
10643 -- Handle the limited view of a type
10645 if Is_Incomplete_Type (Desig)
10646 and then From_With_Type (Desig)
10647 and then Present (Non_Limited_View (Desig))
10649 return Available_View (Desig);
10653 end Full_Designated_Type;
10655 -- Local Declarations
10657 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10658 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10660 Same_Base : constant Boolean :=
10661 Base_Type (Target) = Base_Type (Opnd);
10663 -- Start of processing for Check_Limited
10666 if Is_Tagged_Type (Target) then
10667 return Valid_Tagged_Conversion (Target, Opnd);
10670 if not Same_Base then
10672 ("target designated type not compatible with }",
10673 N, Base_Type (Opnd));
10676 -- Ada 2005 AI-384: legality rule is symmetric in both
10677 -- designated types. The conversion is legal (with possible
10678 -- constraint check) if either designated type is
10681 elsif Subtypes_Statically_Match (Target, Opnd)
10683 (Has_Discriminants (Target)
10685 (not Is_Constrained (Opnd)
10686 or else not Is_Constrained (Target)))
10688 -- Special case, if Value_Size has been used to make the
10689 -- sizes different, the conversion is not allowed even
10690 -- though the subtypes statically match.
10692 if Known_Static_RM_Size (Target)
10693 and then Known_Static_RM_Size (Opnd)
10694 and then RM_Size (Target) /= RM_Size (Opnd)
10697 ("target designated subtype not compatible with }",
10700 ("\because sizes of the two designated subtypes differ",
10704 -- Normal case where conversion is allowed
10712 ("target designated subtype not compatible with }",
10719 -- Access to subprogram types. If the operand is an access parameter,
10720 -- the type has a deeper accessibility that any master, and cannot be
10721 -- assigned. We must make an exception if the conversion is part of an
10722 -- assignment and the target is the return object of an extended return
10723 -- statement, because in that case the accessibility check takes place
10724 -- after the return.
10726 elsif Is_Access_Subprogram_Type (Target_Type)
10727 and then No (Corresponding_Remote_Type (Opnd_Type))
10729 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10730 and then Is_Entity_Name (Operand)
10731 and then Ekind (Entity (Operand)) = E_In_Parameter
10733 (Nkind (Parent (N)) /= N_Assignment_Statement
10734 or else not Is_Entity_Name (Name (Parent (N)))
10735 or else not Is_Return_Object (Entity (Name (Parent (N)))))
10738 ("illegal attempt to store anonymous access to subprogram",
10741 ("\value has deeper accessibility than any master " &
10742 "(RM 3.10.2 (13))",
10746 ("\use named access type for& instead of access parameter",
10747 Operand, Entity (Operand));
10750 -- Check that the designated types are subtype conformant
10752 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
10753 Old_Id => Designated_Type (Opnd_Type),
10756 -- Check the static accessibility rule of 4.6(20)
10758 if Type_Access_Level (Opnd_Type) >
10759 Type_Access_Level (Target_Type)
10762 ("operand type has deeper accessibility level than target",
10765 -- Check that if the operand type is declared in a generic body,
10766 -- then the target type must be declared within that same body
10767 -- (enforces last sentence of 4.6(20)).
10769 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
10771 O_Gen : constant Node_Id :=
10772 Enclosing_Generic_Body (Opnd_Type);
10777 T_Gen := Enclosing_Generic_Body (Target_Type);
10778 while Present (T_Gen) and then T_Gen /= O_Gen loop
10779 T_Gen := Enclosing_Generic_Body (T_Gen);
10782 if T_Gen /= O_Gen then
10784 ("target type must be declared in same generic body"
10785 & " as operand type", N);
10792 -- Remote subprogram access types
10794 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
10795 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
10797 -- It is valid to convert from one RAS type to another provided
10798 -- that their specification statically match.
10800 Check_Subtype_Conformant
10802 Designated_Type (Corresponding_Remote_Type (Target_Type)),
10804 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
10809 -- If both are tagged types, check legality of view conversions
10811 elsif Is_Tagged_Type (Target_Type)
10813 Is_Tagged_Type (Opnd_Type)
10815 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
10817 -- Types derived from the same root type are convertible
10819 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
10822 -- In an instance or an inlined body, there may be inconsistent views of
10823 -- the same type, or of types derived from a common root.
10825 elsif (In_Instance or In_Inlined_Body)
10827 Root_Type (Underlying_Type (Target_Type)) =
10828 Root_Type (Underlying_Type (Opnd_Type))
10832 -- Special check for common access type error case
10834 elsif Ekind (Target_Type) = E_Access_Type
10835 and then Is_Access_Type (Opnd_Type)
10837 Error_Msg_N ("target type must be general access type!", N);
10838 Error_Msg_NE -- CODEFIX
10839 ("add ALL to }!", N, Target_Type);
10843 Error_Msg_NE ("invalid conversion, not compatible with }",
10847 end Valid_Conversion;