1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Output; use Output;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Dim; use Sem_Dim;
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 Targparm; use Targparm;
69 with Sem_Type; use Sem_Type;
70 with Sem_Warn; use Sem_Warn;
71 with Sinfo; use Sinfo;
72 with Sinfo.CN; use Sinfo.CN;
73 with Snames; use Snames;
74 with Stand; use Stand;
75 with Stringt; use Stringt;
76 with Style; use Style;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
81 package body Sem_Res is
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
87 -- Second pass (top-down) type checking and overload resolution procedures
88 -- Typ is the type required by context. These procedures propagate the type
89 -- information recursively to the descendants of N. If the node is not
90 -- overloaded, its Etype is established in the first pass. If overloaded,
91 -- the Resolve routines set the correct type. For arith. operators, the
92 -- Etype is the base type of the context.
94 -- Note that Resolve_Attribute is separated off in Sem_Attr
96 function Bad_Unordered_Enumeration_Reference
98 T : Entity_Id) return Boolean;
99 -- Node N contains a potentially dubious reference to type T, either an
100 -- explicit comparison, or an explicit range. This function returns True
101 -- if the type T is an enumeration type for which No pragma Order has been
102 -- given, and the reference N is not in the same extended source unit as
103 -- the declaration of T.
105 procedure Check_Discriminant_Use (N : Node_Id);
106 -- Enforce the restrictions on the use of discriminants when constraining
107 -- a component of a discriminated type (record or concurrent type).
109 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
110 -- Given a node for an operator associated with type T, check that
111 -- the operator is visible. Operators all of whose operands are
112 -- universal must be checked for visibility during resolution
113 -- because their type is not determinable based on their operands.
115 procedure Check_Fully_Declared_Prefix
118 -- Check that the type of the prefix of a dereference is not incomplete
120 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
121 -- Given a call node, N, which is known to occur immediately within the
122 -- subprogram being called, determines whether it is a detectable case of
123 -- an infinite recursion, and if so, outputs appropriate messages. Returns
124 -- True if an infinite recursion is detected, and False otherwise.
126 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
127 -- If the type of the object being initialized uses the secondary stack
128 -- directly or indirectly, create a transient scope for the call to the
129 -- init proc. This is because we do not create transient scopes for the
130 -- initialization of individual components within the init proc itself.
131 -- Could be optimized away perhaps?
133 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
134 -- N is the node for a logical operator. If the operator is predefined, and
135 -- the root type of the operands is Standard.Boolean, then a check is made
136 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
137 -- the style check for Style_Check_Boolean_And_Or.
139 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
140 -- Determine whether E is an access type declared by an access declaration,
141 -- and not an (anonymous) allocator type.
143 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
144 -- Utility to check whether the entity for an operator is a predefined
145 -- operator, in which case the expression is left as an operator in the
146 -- tree (else it is rewritten into a call). An instance of an intrinsic
147 -- conversion operation may be given an operator name, but is not treated
148 -- like an operator. Note that an operator that is an imported back-end
149 -- builtin has convention Intrinsic, but is expected to be rewritten into
150 -- a call, so such an operator is not treated as predefined by this
153 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
154 -- If a default expression in entry call N depends on the discriminants
155 -- of the task, it must be replaced with a reference to the discriminant
156 -- of the task being called.
158 procedure Resolve_Op_Concat_Arg
163 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
164 -- concatenation operator. The operand is either of the array type or of
165 -- the component type. If the operand is an aggregate, and the component
166 -- type is composite, this is ambiguous if component type has aggregates.
168 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
169 -- Does the first part of the work of Resolve_Op_Concat
171 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
172 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
173 -- has been resolved. See Resolve_Op_Concat for details.
175 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
211 function Operator_Kind
213 Is_Binary : Boolean) return Node_Kind;
214 -- Utility to map the name of an operator into the corresponding Node. Used
215 -- by other node rewriting procedures.
217 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
218 -- Resolve actuals of call, and add default expressions for missing ones.
219 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
220 -- called subprogram.
222 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
223 -- Called from Resolve_Call, when the prefix denotes an entry or element
224 -- of entry family. Actuals are resolved as for subprograms, and the node
225 -- is rebuilt as an entry call. Also called for protected operations. Typ
226 -- is the context type, which is used when the operation is a protected
227 -- function with no arguments, and the return value is indexed.
229 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
230 -- A call to a user-defined intrinsic operator is rewritten as a call to
231 -- the corresponding predefined operator, with suitable conversions. Note
232 -- that this applies only for intrinsic operators that denote predefined
233 -- operators, not ones that are intrinsic imports of back-end builtins.
235 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
236 -- Ditto, for unary operators (arithmetic ones and "not" on signed
237 -- integer types for VMS).
239 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
240 -- If an operator node resolves to a call to a user-defined operator,
241 -- rewrite the node as a function call.
243 procedure Make_Call_Into_Operator
247 -- Inverse transformation: if an operator is given in functional notation,
248 -- then after resolving the node, transform into an operator node, so
249 -- that operands are resolved properly. Recall that predefined operators
250 -- do not have a full signature and special resolution rules apply.
252 procedure Rewrite_Renamed_Operator
256 -- An operator can rename another, e.g. in an instantiation. In that
257 -- case, the proper operator node must be constructed and resolved.
259 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
260 -- The String_Literal_Subtype is built for all strings that are not
261 -- operands of a static concatenation operation. If the argument is
262 -- not a N_String_Literal node, then the call has no effect.
264 procedure Set_Slice_Subtype (N : Node_Id);
265 -- Build subtype of array type, with the range specified by the slice
267 procedure Simplify_Type_Conversion (N : Node_Id);
268 -- Called after N has been resolved and evaluated, but before range checks
269 -- have been applied. Currently simplifies a combination of floating-point
270 -- to integer conversion and Truncation attribute.
272 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
273 -- A universal_fixed expression in an universal context is unambiguous if
274 -- there is only one applicable fixed point type. Determining whether there
275 -- is only one requires a search over all visible entities, and happens
276 -- only in very pathological cases (see 6115-006).
278 -------------------------
279 -- Ambiguous_Character --
280 -------------------------
282 procedure Ambiguous_Character (C : Node_Id) is
286 if Nkind (C) = N_Character_Literal then
287 Error_Msg_N ("ambiguous character literal", C);
289 -- First the ones in Standard
291 Error_Msg_N ("\\possible interpretation: Character!", C);
292 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
294 -- Include Wide_Wide_Character in Ada 2005 mode
296 if Ada_Version >= Ada_2005 then
297 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
300 -- Now any other types that match
302 E := Current_Entity (C);
303 while Present (E) loop
304 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
308 end Ambiguous_Character;
310 -------------------------
311 -- Analyze_And_Resolve --
312 -------------------------
314 procedure Analyze_And_Resolve (N : Node_Id) is
318 end Analyze_And_Resolve;
320 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
324 end Analyze_And_Resolve;
326 -- Version withs check(s) suppressed
328 procedure Analyze_And_Resolve
333 Scop : constant Entity_Id := Current_Scope;
336 if Suppress = All_Checks then
338 Svg : constant Suppress_Array := Scope_Suppress;
340 Scope_Suppress := (others => True);
341 Analyze_And_Resolve (N, Typ);
342 Scope_Suppress := Svg;
347 Svg : constant Boolean := Scope_Suppress (Suppress);
350 Scope_Suppress (Suppress) := True;
351 Analyze_And_Resolve (N, Typ);
352 Scope_Suppress (Suppress) := Svg;
356 if Current_Scope /= Scop
357 and then Scope_Is_Transient
359 -- This can only happen if a transient scope was created for an inner
360 -- expression, which will be removed upon completion of the analysis
361 -- of an enclosing construct. The transient scope must have the
362 -- suppress status of the enclosing environment, not of this Analyze
365 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
368 end Analyze_And_Resolve;
370 procedure Analyze_And_Resolve
374 Scop : constant Entity_Id := Current_Scope;
377 if Suppress = All_Checks then
379 Svg : constant Suppress_Array := Scope_Suppress;
381 Scope_Suppress := (others => True);
382 Analyze_And_Resolve (N);
383 Scope_Suppress := Svg;
388 Svg : constant Boolean := Scope_Suppress (Suppress);
391 Scope_Suppress (Suppress) := True;
392 Analyze_And_Resolve (N);
393 Scope_Suppress (Suppress) := Svg;
397 if Current_Scope /= Scop
398 and then Scope_Is_Transient
400 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
403 end Analyze_And_Resolve;
405 ----------------------------------------
406 -- Bad_Unordered_Enumeration_Reference --
407 ----------------------------------------
409 function Bad_Unordered_Enumeration_Reference
411 T : Entity_Id) return Boolean
414 return Is_Enumeration_Type (T)
415 and then Comes_From_Source (N)
416 and then Warn_On_Unordered_Enumeration_Type
417 and then not Has_Pragma_Ordered (T)
418 and then not In_Same_Extended_Unit (N, T);
419 end Bad_Unordered_Enumeration_Reference;
421 ----------------------------
422 -- Check_Discriminant_Use --
423 ----------------------------
425 procedure Check_Discriminant_Use (N : Node_Id) is
426 PN : constant Node_Id := Parent (N);
427 Disc : constant Entity_Id := Entity (N);
432 -- Any use in a spec-expression is legal
434 if In_Spec_Expression then
437 elsif Nkind (PN) = N_Range then
439 -- Discriminant cannot be used to constrain a scalar type
443 if Nkind (P) = N_Range_Constraint
444 and then Nkind (Parent (P)) = N_Subtype_Indication
445 and then Nkind (Parent (Parent (P))) = N_Component_Definition
447 Error_Msg_N ("discriminant cannot constrain scalar type", N);
449 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
451 -- The following check catches the unusual case where a
452 -- discriminant appears within an index constraint that is part of
453 -- a larger expression within a constraint on a component, e.g. "C
454 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
455 -- of record components, and note that a similar check should also
456 -- apply in the case of discriminant constraints below. ???
458 -- Note that the check for N_Subtype_Declaration below is to
459 -- detect the valid use of discriminants in the constraints of a
460 -- subtype declaration when this subtype declaration appears
461 -- inside the scope of a record type (which is syntactically
462 -- illegal, but which may be created as part of derived type
463 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
466 if Ekind (Current_Scope) = E_Record_Type
467 and then Scope (Disc) = Current_Scope
469 (Nkind (Parent (P)) = N_Subtype_Indication
471 Nkind_In (Parent (Parent (P)), N_Component_Definition,
472 N_Subtype_Declaration)
473 and then Paren_Count (N) = 0)
476 ("discriminant must appear alone in component constraint", N);
480 -- Detect a common error:
482 -- type R (D : Positive := 100) is record
483 -- Name : String (1 .. D);
486 -- The default value causes an object of type R to be allocated
487 -- with room for Positive'Last characters. The RM does not mandate
488 -- the allocation of the maximum size, but that is what GNAT does
489 -- so we should warn the programmer that there is a problem.
491 Check_Large : declare
497 function Large_Storage_Type (T : Entity_Id) return Boolean;
498 -- Return True if type T has a large enough range that any
499 -- array whose index type covered the whole range of the type
500 -- would likely raise Storage_Error.
502 ------------------------
503 -- Large_Storage_Type --
504 ------------------------
506 function Large_Storage_Type (T : Entity_Id) return Boolean is
508 -- The type is considered large if its bounds are known at
509 -- compile time and if it requires at least as many bits as
510 -- a Positive to store the possible values.
512 return Compile_Time_Known_Value (Type_Low_Bound (T))
513 and then Compile_Time_Known_Value (Type_High_Bound (T))
515 Minimum_Size (T, Biased => True) >=
516 RM_Size (Standard_Positive);
517 end Large_Storage_Type;
519 -- Start of processing for Check_Large
522 -- Check that the Disc has a large range
524 if not Large_Storage_Type (Etype (Disc)) then
528 -- If the enclosing type is limited, we allocate only the
529 -- default value, not the maximum, and there is no need for
532 if Is_Limited_Type (Scope (Disc)) then
536 -- Check that it is the high bound
538 if N /= High_Bound (PN)
539 or else No (Discriminant_Default_Value (Disc))
544 -- Check the array allows a large range at this bound. First
549 if Nkind (SI) /= N_Subtype_Indication then
553 T := Entity (Subtype_Mark (SI));
555 if not Is_Array_Type (T) then
559 -- Next, find the dimension
561 TB := First_Index (T);
562 CB := First (Constraints (P));
564 and then Present (TB)
565 and then Present (CB)
576 -- Now, check the dimension has a large range
578 if not Large_Storage_Type (Etype (TB)) then
582 -- Warn about the danger
585 ("?creation of & object may raise Storage_Error!",
594 -- Legal case is in index or discriminant constraint
596 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
597 N_Discriminant_Association)
599 if Paren_Count (N) > 0 then
601 ("discriminant in constraint must appear alone", N);
603 elsif Nkind (N) = N_Expanded_Name
604 and then Comes_From_Source (N)
607 ("discriminant must appear alone as a direct name", N);
612 -- Otherwise, context is an expression. It should not be within (i.e. a
613 -- subexpression of) a constraint for a component.
618 while not Nkind_In (P, N_Component_Declaration,
619 N_Subtype_Indication,
627 -- If the discriminant is used in an expression that is a bound of a
628 -- scalar type, an Itype is created and the bounds are attached to
629 -- its range, not to the original subtype indication. Such use is of
630 -- course a double fault.
632 if (Nkind (P) = N_Subtype_Indication
633 and then Nkind_In (Parent (P), N_Component_Definition,
634 N_Derived_Type_Definition)
635 and then D = Constraint (P))
637 -- The constraint itself may be given by a subtype indication,
638 -- rather than by a more common discrete range.
640 or else (Nkind (P) = N_Subtype_Indication
642 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
643 or else Nkind (P) = N_Entry_Declaration
644 or else Nkind (D) = N_Defining_Identifier
647 ("discriminant in constraint must appear alone", N);
650 end Check_Discriminant_Use;
652 --------------------------------
653 -- Check_For_Visible_Operator --
654 --------------------------------
656 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
658 if Is_Invisible_Operator (N, T) then
659 Error_Msg_NE -- CODEFIX
660 ("operator for} is not directly visible!", N, First_Subtype (T));
661 Error_Msg_N -- CODEFIX
662 ("use clause would make operation legal!", N);
664 end Check_For_Visible_Operator;
666 ----------------------------------
667 -- Check_Fully_Declared_Prefix --
668 ----------------------------------
670 procedure Check_Fully_Declared_Prefix
675 -- Check that the designated type of the prefix of a dereference is
676 -- not an incomplete type. This cannot be done unconditionally, because
677 -- dereferences of private types are legal in default expressions. This
678 -- case is taken care of in Check_Fully_Declared, called below. There
679 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
681 -- This consideration also applies to similar checks for allocators,
682 -- qualified expressions, and type conversions.
684 -- An additional exception concerns other per-object expressions that
685 -- are not directly related to component declarations, in particular
686 -- representation pragmas for tasks. These will be per-object
687 -- expressions if they depend on discriminants or some global entity.
688 -- If the task has access discriminants, the designated type may be
689 -- incomplete at the point the expression is resolved. This resolution
690 -- takes place within the body of the initialization procedure, where
691 -- the discriminant is replaced by its discriminal.
693 if Is_Entity_Name (Pref)
694 and then Ekind (Entity (Pref)) = E_In_Parameter
698 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
699 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
700 -- Analyze_Object_Renaming, and Freeze_Entity.
702 elsif Ada_Version >= Ada_2005
703 and then Is_Entity_Name (Pref)
704 and then Is_Access_Type (Etype (Pref))
705 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
707 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
711 Check_Fully_Declared (Typ, Parent (Pref));
713 end Check_Fully_Declared_Prefix;
715 ------------------------------
716 -- Check_Infinite_Recursion --
717 ------------------------------
719 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
723 function Same_Argument_List return Boolean;
724 -- Check whether list of actuals is identical to list of formals of
725 -- called function (which is also the enclosing scope).
727 ------------------------
728 -- Same_Argument_List --
729 ------------------------
731 function Same_Argument_List return Boolean is
737 if not Is_Entity_Name (Name (N)) then
740 Subp := Entity (Name (N));
743 F := First_Formal (Subp);
744 A := First_Actual (N);
745 while Present (F) and then Present (A) loop
746 if not Is_Entity_Name (A)
747 or else Entity (A) /= F
757 end Same_Argument_List;
759 -- Start of processing for Check_Infinite_Recursion
762 -- Special case, if this is a procedure call and is a call to the
763 -- current procedure with the same argument list, then this is for
764 -- sure an infinite recursion and we insert a call to raise SE.
766 if Is_List_Member (N)
767 and then List_Length (List_Containing (N)) = 1
768 and then Same_Argument_List
771 P : constant Node_Id := Parent (N);
773 if Nkind (P) = N_Handled_Sequence_Of_Statements
774 and then Nkind (Parent (P)) = N_Subprogram_Body
775 and then Is_Empty_List (Declarations (Parent (P)))
777 Error_Msg_N ("!?infinite recursion", N);
778 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
780 Make_Raise_Storage_Error (Sloc (N),
781 Reason => SE_Infinite_Recursion));
787 -- If not that special case, search up tree, quitting if we reach a
788 -- construct (e.g. a conditional) that tells us that this is not a
789 -- case for an infinite recursion warning.
795 -- If no parent, then we were not inside a subprogram, this can for
796 -- example happen when processing certain pragmas in a spec. Just
797 -- return False in this case.
803 -- Done if we get to subprogram body, this is definitely an infinite
804 -- recursion case if we did not find anything to stop us.
806 exit when Nkind (P) = N_Subprogram_Body;
808 -- If appearing in conditional, result is false
810 if Nkind_In (P, N_Or_Else,
814 N_Conditional_Expression,
819 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
820 and then C /= First (Statements (P))
822 -- If the call is the expression of a return statement and the
823 -- actuals are identical to the formals, it's worth a warning.
824 -- However, we skip this if there is an immediately preceding
825 -- raise statement, since the call is never executed.
827 -- Furthermore, this corresponds to a common idiom:
829 -- function F (L : Thing) return Boolean is
831 -- raise Program_Error;
835 -- for generating a stub function
837 if Nkind (Parent (N)) = N_Simple_Return_Statement
838 and then Same_Argument_List
840 exit when not Is_List_Member (Parent (N));
842 -- OK, return statement is in a statement list, look for raise
848 -- Skip past N_Freeze_Entity nodes generated by expansion
850 Nod := Prev (Parent (N));
852 and then Nkind (Nod) = N_Freeze_Entity
857 -- If no raise statement, give warning
859 exit when Nkind (Nod) /= N_Raise_Statement
861 (Nkind (Nod) not in N_Raise_xxx_Error
862 or else Present (Condition (Nod)));
873 Error_Msg_N ("!?possible infinite recursion", N);
874 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
877 end Check_Infinite_Recursion;
879 -------------------------------
880 -- Check_Initialization_Call --
881 -------------------------------
883 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
884 Typ : constant Entity_Id := Etype (First_Formal (Nam));
886 function Uses_SS (T : Entity_Id) return Boolean;
887 -- Check whether the creation of an object of the type will involve
888 -- use of the secondary stack. If T is a record type, this is true
889 -- if the expression for some component uses the secondary stack, e.g.
890 -- through a call to a function that returns an unconstrained value.
891 -- False if T is controlled, because cleanups occur elsewhere.
897 function Uses_SS (T : Entity_Id) return Boolean is
900 Full_Type : Entity_Id := Underlying_Type (T);
903 -- Normally we want to use the underlying type, but if it's not set
904 -- then continue with T.
906 if not Present (Full_Type) then
910 if Is_Controlled (Full_Type) then
913 elsif Is_Array_Type (Full_Type) then
914 return Uses_SS (Component_Type (Full_Type));
916 elsif Is_Record_Type (Full_Type) then
917 Comp := First_Component (Full_Type);
918 while Present (Comp) loop
919 if Ekind (Comp) = E_Component
920 and then Nkind (Parent (Comp)) = N_Component_Declaration
922 -- The expression for a dynamic component may be rewritten
923 -- as a dereference, so retrieve original node.
925 Expr := Original_Node (Expression (Parent (Comp)));
927 -- Return True if the expression is a call to a function
928 -- (including an attribute function such as Image, or a
929 -- user-defined operator) with a result that requires a
932 if (Nkind (Expr) = N_Function_Call
933 or else Nkind (Expr) in N_Op
934 or else (Nkind (Expr) = N_Attribute_Reference
935 and then Present (Expressions (Expr))))
936 and then Requires_Transient_Scope (Etype (Expr))
940 elsif Uses_SS (Etype (Comp)) then
945 Next_Component (Comp);
955 -- Start of processing for Check_Initialization_Call
958 -- Establish a transient scope if the type needs it
960 if Uses_SS (Typ) then
961 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
963 end Check_Initialization_Call;
965 ---------------------------------------
966 -- Check_No_Direct_Boolean_Operators --
967 ---------------------------------------
969 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
971 if Scope (Entity (N)) = Standard_Standard
972 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
974 -- Restriction only applies to original source code
976 if Comes_From_Source (N) then
977 Check_Restriction (No_Direct_Boolean_Operators, N);
982 Check_Boolean_Operator (N);
984 end Check_No_Direct_Boolean_Operators;
986 ------------------------------
987 -- Check_Parameterless_Call --
988 ------------------------------
990 procedure Check_Parameterless_Call (N : Node_Id) is
993 function Prefix_Is_Access_Subp return Boolean;
994 -- If the prefix is of an access_to_subprogram type, the node must be
995 -- rewritten as a call. Ditto if the prefix is overloaded and all its
996 -- interpretations are access to subprograms.
998 ---------------------------
999 -- Prefix_Is_Access_Subp --
1000 ---------------------------
1002 function Prefix_Is_Access_Subp return Boolean is
1007 -- If the context is an attribute reference that can apply to
1008 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1010 if Nkind (Parent (N)) = N_Attribute_Reference
1011 and then (Attribute_Name (Parent (N)) = Name_Address or else
1012 Attribute_Name (Parent (N)) = Name_Code_Address or else
1013 Attribute_Name (Parent (N)) = Name_Access)
1018 if not Is_Overloaded (N) then
1020 Ekind (Etype (N)) = E_Subprogram_Type
1021 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1023 Get_First_Interp (N, I, It);
1024 while Present (It.Typ) loop
1025 if Ekind (It.Typ) /= E_Subprogram_Type
1026 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1031 Get_Next_Interp (I, It);
1036 end Prefix_Is_Access_Subp;
1038 -- Start of processing for Check_Parameterless_Call
1041 -- Defend against junk stuff if errors already detected
1043 if Total_Errors_Detected /= 0 then
1044 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1046 elsif Nkind (N) in N_Has_Chars
1047 and then Chars (N) in Error_Name_Or_No_Name
1055 -- If the context expects a value, and the name is a procedure, this is
1056 -- most likely a missing 'Access. Don't try to resolve the parameterless
1057 -- call, error will be caught when the outer call is analyzed.
1059 if Is_Entity_Name (N)
1060 and then Ekind (Entity (N)) = E_Procedure
1061 and then not Is_Overloaded (N)
1063 Nkind_In (Parent (N), N_Parameter_Association,
1065 N_Procedure_Call_Statement)
1070 -- Rewrite as call if overloadable entity that is (or could be, in the
1071 -- overloaded case) a function call. If we know for sure that the entity
1072 -- is an enumeration literal, we do not rewrite it.
1074 -- If the entity is the name of an operator, it cannot be a call because
1075 -- operators cannot have default parameters. In this case, this must be
1076 -- a string whose contents coincide with an operator name. Set the kind
1077 -- of the node appropriately.
1079 if (Is_Entity_Name (N)
1080 and then Nkind (N) /= N_Operator_Symbol
1081 and then Is_Overloadable (Entity (N))
1082 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1083 or else Is_Overloaded (N)))
1085 -- Rewrite as call if it is an explicit dereference of an expression of
1086 -- a subprogram access type, and the subprogram type is not that of a
1087 -- procedure or entry.
1090 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1092 -- Rewrite as call if it is a selected component which is a function,
1093 -- this is the case of a call to a protected function (which may be
1094 -- overloaded with other protected operations).
1097 (Nkind (N) = N_Selected_Component
1098 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1100 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1102 and then Is_Overloaded (Selector_Name (N)))))
1104 -- If one of the above three conditions is met, rewrite as call. Apply
1105 -- the rewriting only once.
1108 if Nkind (Parent (N)) /= N_Function_Call
1109 or else N /= Name (Parent (N))
1112 -- This may be a prefixed call that was not fully analyzed, e.g.
1113 -- an actual in an instance.
1115 if Ada_Version >= Ada_2005
1116 and then Nkind (N) = N_Selected_Component
1117 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1119 Analyze_Selected_Component (N);
1121 if Nkind (N) /= N_Selected_Component then
1126 Nam := New_Copy (N);
1128 -- If overloaded, overload set belongs to new copy
1130 Save_Interps (N, Nam);
1132 -- Change node to parameterless function call (note that the
1133 -- Parameter_Associations associations field is left set to Empty,
1134 -- its normal default value since there are no parameters)
1136 Change_Node (N, N_Function_Call);
1138 Set_Sloc (N, Sloc (Nam));
1142 elsif Nkind (N) = N_Parameter_Association then
1143 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1145 elsif Nkind (N) = N_Operator_Symbol then
1146 Change_Operator_Symbol_To_String_Literal (N);
1147 Set_Is_Overloaded (N, False);
1148 Set_Etype (N, Any_String);
1150 end Check_Parameterless_Call;
1152 -----------------------------
1153 -- Is_Definite_Access_Type --
1154 -----------------------------
1156 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1157 Btyp : constant Entity_Id := Base_Type (E);
1159 return Ekind (Btyp) = E_Access_Type
1160 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1161 and then Comes_From_Source (Btyp));
1162 end Is_Definite_Access_Type;
1164 ----------------------
1165 -- Is_Predefined_Op --
1166 ----------------------
1168 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1170 -- Predefined operators are intrinsic subprograms
1172 if not Is_Intrinsic_Subprogram (Nam) then
1176 -- A call to a back-end builtin is never a predefined operator
1178 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1182 return not Is_Generic_Instance (Nam)
1183 and then Chars (Nam) in Any_Operator_Name
1184 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1185 end Is_Predefined_Op;
1187 -----------------------------
1188 -- Make_Call_Into_Operator --
1189 -----------------------------
1191 procedure Make_Call_Into_Operator
1196 Op_Name : constant Name_Id := Chars (Op_Id);
1197 Act1 : Node_Id := First_Actual (N);
1198 Act2 : Node_Id := Next_Actual (Act1);
1199 Error : Boolean := False;
1200 Func : constant Entity_Id := Entity (Name (N));
1201 Is_Binary : constant Boolean := Present (Act2);
1203 Opnd_Type : Entity_Id;
1204 Orig_Type : Entity_Id := Empty;
1207 type Kind_Test is access function (E : Entity_Id) return Boolean;
1209 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1210 -- If the operand is not universal, and the operator is given by an
1211 -- expanded name, verify that the operand has an interpretation with a
1212 -- type defined in the given scope of the operator.
1214 function Type_In_P (Test : Kind_Test) return Entity_Id;
1215 -- Find a type of the given class in package Pack that contains the
1218 ---------------------------
1219 -- Operand_Type_In_Scope --
1220 ---------------------------
1222 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1223 Nod : constant Node_Id := Right_Opnd (Op_Node);
1228 if not Is_Overloaded (Nod) then
1229 return Scope (Base_Type (Etype (Nod))) = S;
1232 Get_First_Interp (Nod, I, It);
1233 while Present (It.Typ) loop
1234 if Scope (Base_Type (It.Typ)) = S then
1238 Get_Next_Interp (I, It);
1243 end Operand_Type_In_Scope;
1249 function Type_In_P (Test : Kind_Test) return Entity_Id is
1252 function In_Decl return Boolean;
1253 -- Verify that node is not part of the type declaration for the
1254 -- candidate type, which would otherwise be invisible.
1260 function In_Decl return Boolean is
1261 Decl_Node : constant Node_Id := Parent (E);
1267 if Etype (E) = Any_Type then
1270 elsif No (Decl_Node) then
1275 and then Nkind (N2) /= N_Compilation_Unit
1277 if N2 = Decl_Node then
1288 -- Start of processing for Type_In_P
1291 -- If the context type is declared in the prefix package, this is the
1292 -- desired base type.
1294 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1295 return Base_Type (Typ);
1298 E := First_Entity (Pack);
1299 while Present (E) loop
1301 and then not In_Decl
1313 -- Start of processing for Make_Call_Into_Operator
1316 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1321 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1322 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1323 Save_Interps (Act1, Left_Opnd (Op_Node));
1324 Save_Interps (Act2, Right_Opnd (Op_Node));
1325 Act1 := Left_Opnd (Op_Node);
1326 Act2 := Right_Opnd (Op_Node);
1331 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1332 Save_Interps (Act1, Right_Opnd (Op_Node));
1333 Act1 := Right_Opnd (Op_Node);
1336 -- If the operator is denoted by an expanded name, and the prefix is
1337 -- not Standard, but the operator is a predefined one whose scope is
1338 -- Standard, then this is an implicit_operator, inserted as an
1339 -- interpretation by the procedure of the same name. This procedure
1340 -- overestimates the presence of implicit operators, because it does
1341 -- not examine the type of the operands. Verify now that the operand
1342 -- type appears in the given scope. If right operand is universal,
1343 -- check the other operand. In the case of concatenation, either
1344 -- argument can be the component type, so check the type of the result.
1345 -- If both arguments are literals, look for a type of the right kind
1346 -- defined in the given scope. This elaborate nonsense is brought to
1347 -- you courtesy of b33302a. The type itself must be frozen, so we must
1348 -- find the type of the proper class in the given scope.
1350 -- A final wrinkle is the multiplication operator for fixed point types,
1351 -- which is defined in Standard only, and not in the scope of the
1352 -- fixed point type itself.
1354 if Nkind (Name (N)) = N_Expanded_Name then
1355 Pack := Entity (Prefix (Name (N)));
1357 -- If the entity being called is defined in the given package, it is
1358 -- a renaming of a predefined operator, and known to be legal.
1360 if Scope (Entity (Name (N))) = Pack
1361 and then Pack /= Standard_Standard
1365 -- Visibility does not need to be checked in an instance: if the
1366 -- operator was not visible in the generic it has been diagnosed
1367 -- already, else there is an implicit copy of it in the instance.
1369 elsif In_Instance then
1372 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1373 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1374 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1376 if Pack /= Standard_Standard then
1380 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1383 elsif Ada_Version >= Ada_2005
1384 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1385 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1390 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1392 if Op_Name = Name_Op_Concat then
1393 Opnd_Type := Base_Type (Typ);
1395 elsif (Scope (Opnd_Type) = Standard_Standard
1397 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1399 and then not Comes_From_Source (Opnd_Type))
1401 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1404 if Scope (Opnd_Type) = Standard_Standard then
1406 -- Verify that the scope contains a type that corresponds to
1407 -- the given literal. Optimize the case where Pack is Standard.
1409 if Pack /= Standard_Standard then
1411 if Opnd_Type = Universal_Integer then
1412 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1414 elsif Opnd_Type = Universal_Real then
1415 Orig_Type := Type_In_P (Is_Real_Type'Access);
1417 elsif Opnd_Type = Any_String then
1418 Orig_Type := Type_In_P (Is_String_Type'Access);
1420 elsif Opnd_Type = Any_Access then
1421 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1423 elsif Opnd_Type = Any_Composite then
1424 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1426 if Present (Orig_Type) then
1427 if Has_Private_Component (Orig_Type) then
1430 Set_Etype (Act1, Orig_Type);
1433 Set_Etype (Act2, Orig_Type);
1442 Error := No (Orig_Type);
1445 elsif Ekind (Opnd_Type) = E_Allocator_Type
1446 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1450 -- If the type is defined elsewhere, and the operator is not
1451 -- defined in the given scope (by a renaming declaration, e.g.)
1452 -- then this is an error as well. If an extension of System is
1453 -- present, and the type may be defined there, Pack must be
1456 elsif Scope (Opnd_Type) /= Pack
1457 and then Scope (Op_Id) /= Pack
1458 and then (No (System_Aux_Id)
1459 or else Scope (Opnd_Type) /= System_Aux_Id
1460 or else Pack /= Scope (System_Aux_Id))
1462 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1465 Error := not Operand_Type_In_Scope (Pack);
1468 elsif Pack = Standard_Standard
1469 and then not Operand_Type_In_Scope (Standard_Standard)
1476 Error_Msg_Node_2 := Pack;
1478 ("& not declared in&", N, Selector_Name (Name (N)));
1479 Set_Etype (N, Any_Type);
1482 -- Detect a mismatch between the context type and the result type
1483 -- in the named package, which is otherwise not detected if the
1484 -- operands are universal. Check is only needed if source entity is
1485 -- an operator, not a function that renames an operator.
1487 elsif Nkind (Parent (N)) /= N_Type_Conversion
1488 and then Ekind (Entity (Name (N))) = E_Operator
1489 and then Is_Numeric_Type (Typ)
1490 and then not Is_Universal_Numeric_Type (Typ)
1491 and then Scope (Base_Type (Typ)) /= Pack
1492 and then not In_Instance
1494 if Is_Fixed_Point_Type (Typ)
1495 and then (Op_Name = Name_Op_Multiply
1497 Op_Name = Name_Op_Divide)
1499 -- Already checked above
1503 -- Operator may be defined in an extension of System
1505 elsif Present (System_Aux_Id)
1506 and then Scope (Opnd_Type) = System_Aux_Id
1511 -- Could we use Wrong_Type here??? (this would require setting
1512 -- Etype (N) to the actual type found where Typ was expected).
1514 Error_Msg_NE ("expect }", N, Typ);
1519 Set_Chars (Op_Node, Op_Name);
1521 if not Is_Private_Type (Etype (N)) then
1522 Set_Etype (Op_Node, Base_Type (Etype (N)));
1524 Set_Etype (Op_Node, Etype (N));
1527 -- If this is a call to a function that renames a predefined equality,
1528 -- the renaming declaration provides a type that must be used to
1529 -- resolve the operands. This must be done now because resolution of
1530 -- the equality node will not resolve any remaining ambiguity, and it
1531 -- assumes that the first operand is not overloaded.
1533 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1534 and then Ekind (Func) = E_Function
1535 and then Is_Overloaded (Act1)
1537 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1538 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1541 Set_Entity (Op_Node, Op_Id);
1542 Generate_Reference (Op_Id, N, ' ');
1544 -- Do rewrite setting Comes_From_Source on the result if the original
1545 -- call came from source. Although it is not strictly the case that the
1546 -- operator as such comes from the source, logically it corresponds
1547 -- exactly to the function call in the source, so it should be marked
1548 -- this way (e.g. to make sure that validity checks work fine).
1551 CS : constant Boolean := Comes_From_Source (N);
1553 Rewrite (N, Op_Node);
1554 Set_Comes_From_Source (N, CS);
1557 -- If this is an arithmetic operator and the result type is private,
1558 -- the operands and the result must be wrapped in conversion to
1559 -- expose the underlying numeric type and expand the proper checks,
1560 -- e.g. on division.
1562 if Is_Private_Type (Typ) then
1564 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1565 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1566 Resolve_Intrinsic_Operator (N, Typ);
1568 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1569 Resolve_Intrinsic_Unary_Operator (N, Typ);
1577 end Make_Call_Into_Operator;
1583 function Operator_Kind
1585 Is_Binary : Boolean) return Node_Kind
1590 -- Use CASE statement or array???
1593 if Op_Name = Name_Op_And then
1595 elsif Op_Name = Name_Op_Or then
1597 elsif Op_Name = Name_Op_Xor then
1599 elsif Op_Name = Name_Op_Eq then
1601 elsif Op_Name = Name_Op_Ne then
1603 elsif Op_Name = Name_Op_Lt then
1605 elsif Op_Name = Name_Op_Le then
1607 elsif Op_Name = Name_Op_Gt then
1609 elsif Op_Name = Name_Op_Ge then
1611 elsif Op_Name = Name_Op_Add then
1613 elsif Op_Name = Name_Op_Subtract then
1614 Kind := N_Op_Subtract;
1615 elsif Op_Name = Name_Op_Concat then
1616 Kind := N_Op_Concat;
1617 elsif Op_Name = Name_Op_Multiply then
1618 Kind := N_Op_Multiply;
1619 elsif Op_Name = Name_Op_Divide then
1620 Kind := N_Op_Divide;
1621 elsif Op_Name = Name_Op_Mod then
1623 elsif Op_Name = Name_Op_Rem then
1625 elsif Op_Name = Name_Op_Expon then
1628 raise Program_Error;
1634 if Op_Name = Name_Op_Add then
1636 elsif Op_Name = Name_Op_Subtract then
1638 elsif Op_Name = Name_Op_Abs then
1640 elsif Op_Name = Name_Op_Not then
1643 raise Program_Error;
1650 ----------------------------
1651 -- Preanalyze_And_Resolve --
1652 ----------------------------
1654 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1655 Save_Full_Analysis : constant Boolean := Full_Analysis;
1658 Full_Analysis := False;
1659 Expander_Mode_Save_And_Set (False);
1661 -- We suppress all checks for this analysis, since the checks will
1662 -- be applied properly, and in the right location, when the default
1663 -- expression is reanalyzed and reexpanded later on.
1665 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1667 Expander_Mode_Restore;
1668 Full_Analysis := Save_Full_Analysis;
1669 end Preanalyze_And_Resolve;
1671 -- Version without context type
1673 procedure Preanalyze_And_Resolve (N : Node_Id) is
1674 Save_Full_Analysis : constant Boolean := Full_Analysis;
1677 Full_Analysis := False;
1678 Expander_Mode_Save_And_Set (False);
1681 Resolve (N, Etype (N), Suppress => All_Checks);
1683 Expander_Mode_Restore;
1684 Full_Analysis := Save_Full_Analysis;
1685 end Preanalyze_And_Resolve;
1687 ----------------------------------
1688 -- Replace_Actual_Discriminants --
1689 ----------------------------------
1691 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1692 Loc : constant Source_Ptr := Sloc (N);
1693 Tsk : Node_Id := Empty;
1695 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1696 -- Comment needed???
1702 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1706 if Nkind (Nod) = N_Identifier then
1707 Ent := Entity (Nod);
1710 and then Ekind (Ent) = E_Discriminant
1713 Make_Selected_Component (Loc,
1714 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1715 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1717 Set_Etype (Nod, Etype (Ent));
1725 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1727 -- Start of processing for Replace_Actual_Discriminants
1730 if not Full_Expander_Active then
1734 if Nkind (Name (N)) = N_Selected_Component then
1735 Tsk := Prefix (Name (N));
1737 elsif Nkind (Name (N)) = N_Indexed_Component then
1738 Tsk := Prefix (Prefix (Name (N)));
1744 Replace_Discrs (Default);
1746 end Replace_Actual_Discriminants;
1752 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1753 Ambiguous : Boolean := False;
1754 Ctx_Type : Entity_Id := Typ;
1755 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1756 Err_Type : Entity_Id := Empty;
1757 Found : Boolean := False;
1760 I1 : Interp_Index := 0; -- prevent junk warning
1763 Seen : Entity_Id := Empty; -- prevent junk warning
1765 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1766 -- Determine whether a node comes from a predefined library unit or
1769 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1770 -- Try and fix up a literal so that it matches its expected type. New
1771 -- literals are manufactured if necessary to avoid cascaded errors.
1773 procedure Report_Ambiguous_Argument;
1774 -- Additional diagnostics when an ambiguous call has an ambiguous
1775 -- argument (typically a controlling actual).
1777 procedure Resolution_Failed;
1778 -- Called when attempt at resolving current expression fails
1780 ------------------------------------
1781 -- Comes_From_Predefined_Lib_Unit --
1782 -------------------------------------
1784 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1787 Sloc (Nod) = Standard_Location
1788 or else Is_Predefined_File_Name
1789 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1790 end Comes_From_Predefined_Lib_Unit;
1792 --------------------
1793 -- Patch_Up_Value --
1794 --------------------
1796 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1798 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1800 Make_Real_Literal (Sloc (N),
1801 Realval => UR_From_Uint (Intval (N))));
1802 Set_Etype (N, Universal_Real);
1803 Set_Is_Static_Expression (N);
1805 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1807 Make_Integer_Literal (Sloc (N),
1808 Intval => UR_To_Uint (Realval (N))));
1809 Set_Etype (N, Universal_Integer);
1810 Set_Is_Static_Expression (N);
1812 elsif Nkind (N) = N_String_Literal
1813 and then Is_Character_Type (Typ)
1815 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1817 Make_Character_Literal (Sloc (N),
1819 Char_Literal_Value =>
1820 UI_From_Int (Character'Pos ('A'))));
1821 Set_Etype (N, Any_Character);
1822 Set_Is_Static_Expression (N);
1824 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1826 Make_String_Literal (Sloc (N),
1827 Strval => End_String));
1829 elsif Nkind (N) = N_Range then
1830 Patch_Up_Value (Low_Bound (N), Typ);
1831 Patch_Up_Value (High_Bound (N), Typ);
1835 -------------------------------
1836 -- Report_Ambiguous_Argument --
1837 -------------------------------
1839 procedure Report_Ambiguous_Argument is
1840 Arg : constant Node_Id := First (Parameter_Associations (N));
1845 if Nkind (Arg) = N_Function_Call
1846 and then Is_Entity_Name (Name (Arg))
1847 and then Is_Overloaded (Name (Arg))
1849 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1851 -- Could use comments on what is going on here???
1853 Get_First_Interp (Name (Arg), I, It);
1854 while Present (It.Nam) loop
1855 Error_Msg_Sloc := Sloc (It.Nam);
1857 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1858 Error_Msg_N ("interpretation (inherited) #!", Arg);
1860 Error_Msg_N ("interpretation #!", Arg);
1863 Get_Next_Interp (I, It);
1866 end Report_Ambiguous_Argument;
1868 -----------------------
1869 -- Resolution_Failed --
1870 -----------------------
1872 procedure Resolution_Failed is
1874 Patch_Up_Value (N, Typ);
1876 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1877 Set_Is_Overloaded (N, False);
1879 -- The caller will return without calling the expander, so we need
1880 -- to set the analyzed flag. Note that it is fine to set Analyzed
1881 -- to True even if we are in the middle of a shallow analysis,
1882 -- (see the spec of sem for more details) since this is an error
1883 -- situation anyway, and there is no point in repeating the
1884 -- analysis later (indeed it won't work to repeat it later, since
1885 -- we haven't got a clear resolution of which entity is being
1888 Set_Analyzed (N, True);
1890 end Resolution_Failed;
1892 -- Start of processing for Resolve
1899 -- Access attribute on remote subprogram cannot be used for a non-remote
1900 -- access-to-subprogram type.
1902 if Nkind (N) = N_Attribute_Reference
1903 and then (Attribute_Name (N) = Name_Access or else
1904 Attribute_Name (N) = Name_Unrestricted_Access or else
1905 Attribute_Name (N) = Name_Unchecked_Access)
1906 and then Comes_From_Source (N)
1907 and then Is_Entity_Name (Prefix (N))
1908 and then Is_Subprogram (Entity (Prefix (N)))
1909 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1910 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1913 ("prefix must statically denote a non-remote subprogram", N);
1916 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1918 -- If the context is a Remote_Access_To_Subprogram, access attributes
1919 -- must be resolved with the corresponding fat pointer. There is no need
1920 -- to check for the attribute name since the return type of an
1921 -- attribute is never a remote type.
1923 if Nkind (N) = N_Attribute_Reference
1924 and then Comes_From_Source (N)
1925 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1928 Attr : constant Attribute_Id :=
1929 Get_Attribute_Id (Attribute_Name (N));
1930 Pref : constant Node_Id := Prefix (N);
1933 Is_Remote : Boolean := True;
1936 -- Check that Typ is a remote access-to-subprogram type
1938 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1940 -- Prefix (N) must statically denote a remote subprogram
1941 -- declared in a package specification.
1943 if Attr = Attribute_Access then
1944 Decl := Unit_Declaration_Node (Entity (Pref));
1946 if Nkind (Decl) = N_Subprogram_Body then
1947 Spec := Corresponding_Spec (Decl);
1949 if not No (Spec) then
1950 Decl := Unit_Declaration_Node (Spec);
1954 Spec := Parent (Decl);
1956 if not Is_Entity_Name (Prefix (N))
1957 or else Nkind (Spec) /= N_Package_Specification
1959 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1963 ("prefix must statically denote a remote subprogram ",
1968 -- If we are generating code for a distributed program.
1969 -- perform semantic checks against the corresponding
1972 if (Attr = Attribute_Access or else
1973 Attr = Attribute_Unchecked_Access or else
1974 Attr = Attribute_Unrestricted_Access)
1975 and then Full_Expander_Active
1976 and then Get_PCS_Name /= Name_No_DSA
1978 Check_Subtype_Conformant
1979 (New_Id => Entity (Prefix (N)),
1980 Old_Id => Designated_Type
1981 (Corresponding_Remote_Type (Typ)),
1985 Process_Remote_AST_Attribute (N, Typ);
1992 Debug_A_Entry ("resolving ", N);
1994 if Debug_Flag_V then
1995 Write_Overloads (N);
1998 if Comes_From_Source (N) then
1999 if Is_Fixed_Point_Type (Typ) then
2000 Check_Restriction (No_Fixed_Point, N);
2002 elsif Is_Floating_Point_Type (Typ)
2003 and then Typ /= Universal_Real
2004 and then Typ /= Any_Real
2006 Check_Restriction (No_Floating_Point, N);
2010 -- Return if already analyzed
2012 if Analyzed (N) then
2013 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2014 Analyze_Dimension (N);
2017 -- Return if type = Any_Type (previous error encountered)
2019 elsif Etype (N) = Any_Type then
2020 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2024 Check_Parameterless_Call (N);
2026 -- If not overloaded, then we know the type, and all that needs doing
2027 -- is to check that this type is compatible with the context.
2029 if not Is_Overloaded (N) then
2030 Found := Covers (Typ, Etype (N));
2031 Expr_Type := Etype (N);
2033 -- In the overloaded case, we must select the interpretation that
2034 -- is compatible with the context (i.e. the type passed to Resolve)
2037 -- Loop through possible interpretations
2039 Get_First_Interp (N, I, It);
2040 Interp_Loop : while Present (It.Typ) loop
2042 if Debug_Flag_V then
2043 Write_Str ("Interp: ");
2047 -- We are only interested in interpretations that are compatible
2048 -- with the expected type, any other interpretations are ignored.
2050 if not Covers (Typ, It.Typ) then
2051 if Debug_Flag_V then
2052 Write_Str (" interpretation incompatible with context");
2057 -- Skip the current interpretation if it is disabled by an
2058 -- abstract operator. This action is performed only when the
2059 -- type against which we are resolving is the same as the
2060 -- type of the interpretation.
2062 if Ada_Version >= Ada_2005
2063 and then It.Typ = Typ
2064 and then Typ /= Universal_Integer
2065 and then Typ /= Universal_Real
2066 and then Present (It.Abstract_Op)
2068 if Debug_Flag_V then
2069 Write_Line ("Skip.");
2075 -- First matching interpretation
2081 Expr_Type := It.Typ;
2083 -- Matching interpretation that is not the first, maybe an
2084 -- error, but there are some cases where preference rules are
2085 -- used to choose between the two possibilities. These and
2086 -- some more obscure cases are handled in Disambiguate.
2089 -- If the current statement is part of a predefined library
2090 -- unit, then all interpretations which come from user level
2091 -- packages should not be considered.
2094 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2099 Error_Msg_Sloc := Sloc (Seen);
2100 It1 := Disambiguate (N, I1, I, Typ);
2102 -- Disambiguation has succeeded. Skip the remaining
2105 if It1 /= No_Interp then
2107 Expr_Type := It1.Typ;
2109 while Present (It.Typ) loop
2110 Get_Next_Interp (I, It);
2114 -- Before we issue an ambiguity complaint, check for
2115 -- the case of a subprogram call where at least one
2116 -- of the arguments is Any_Type, and if so, suppress
2117 -- the message, since it is a cascaded error.
2119 if Nkind_In (N, N_Function_Call,
2120 N_Procedure_Call_Statement)
2127 A := First_Actual (N);
2128 while Present (A) loop
2131 if Nkind (E) = N_Parameter_Association then
2132 E := Explicit_Actual_Parameter (E);
2135 if Etype (E) = Any_Type then
2136 if Debug_Flag_V then
2137 Write_Str ("Any_Type in call");
2148 elsif Nkind (N) in N_Binary_Op
2149 and then (Etype (Left_Opnd (N)) = Any_Type
2150 or else Etype (Right_Opnd (N)) = Any_Type)
2154 elsif Nkind (N) in N_Unary_Op
2155 and then Etype (Right_Opnd (N)) = Any_Type
2160 -- Not that special case, so issue message using the
2161 -- flag Ambiguous to control printing of the header
2162 -- message only at the start of an ambiguous set.
2164 if not Ambiguous then
2165 if Nkind (N) = N_Function_Call
2166 and then Nkind (Name (N)) = N_Explicit_Dereference
2169 ("ambiguous expression "
2170 & "(cannot resolve indirect call)!", N);
2172 Error_Msg_NE -- CODEFIX
2173 ("ambiguous expression (cannot resolve&)!",
2179 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2181 ("\\possible interpretation (inherited)#!", N);
2183 Error_Msg_N -- CODEFIX
2184 ("\\possible interpretation#!", N);
2188 (N, N_Procedure_Call_Statement, N_Function_Call)
2189 and then Present (Parameter_Associations (N))
2191 Report_Ambiguous_Argument;
2195 Error_Msg_Sloc := Sloc (It.Nam);
2197 -- By default, the error message refers to the candidate
2198 -- interpretation. But if it is a predefined operator, it
2199 -- is implicitly declared at the declaration of the type
2200 -- of the operand. Recover the sloc of that declaration
2201 -- for the error message.
2203 if Nkind (N) in N_Op
2204 and then Scope (It.Nam) = Standard_Standard
2205 and then not Is_Overloaded (Right_Opnd (N))
2206 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2209 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2211 if Comes_From_Source (Err_Type)
2212 and then Present (Parent (Err_Type))
2214 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2217 elsif Nkind (N) in N_Binary_Op
2218 and then Scope (It.Nam) = Standard_Standard
2219 and then not Is_Overloaded (Left_Opnd (N))
2220 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2223 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2225 if Comes_From_Source (Err_Type)
2226 and then Present (Parent (Err_Type))
2228 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2231 -- If this is an indirect call, use the subprogram_type
2232 -- in the message, to have a meaningful location. Also
2233 -- indicate if this is an inherited operation, created
2234 -- by a type declaration.
2236 elsif Nkind (N) = N_Function_Call
2237 and then Nkind (Name (N)) = N_Explicit_Dereference
2238 and then Is_Type (It.Nam)
2242 Sloc (Associated_Node_For_Itype (Err_Type));
2247 if Nkind (N) in N_Op
2248 and then Scope (It.Nam) = Standard_Standard
2249 and then Present (Err_Type)
2251 -- Special-case the message for universal_fixed
2252 -- operators, which are not declared with the type
2253 -- of the operand, but appear forever in Standard.
2255 if It.Typ = Universal_Fixed
2256 and then Scope (It.Nam) = Standard_Standard
2259 ("\\possible interpretation as " &
2260 "universal_fixed operation " &
2261 "(RM 4.5.5 (19))", N);
2264 ("\\possible interpretation (predefined)#!", N);
2268 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2271 ("\\possible interpretation (inherited)#!", N);
2273 Error_Msg_N -- CODEFIX
2274 ("\\possible interpretation#!", N);
2280 -- We have a matching interpretation, Expr_Type is the type
2281 -- from this interpretation, and Seen is the entity.
2283 -- For an operator, just set the entity name. The type will be
2284 -- set by the specific operator resolution routine.
2286 if Nkind (N) in N_Op then
2287 Set_Entity (N, Seen);
2288 Generate_Reference (Seen, N);
2290 elsif Nkind (N) = N_Case_Expression then
2291 Set_Etype (N, Expr_Type);
2293 elsif Nkind (N) = N_Character_Literal then
2294 Set_Etype (N, Expr_Type);
2296 elsif Nkind (N) = N_Conditional_Expression then
2297 Set_Etype (N, Expr_Type);
2299 -- AI05-0139-2: Expression is overloaded because type has
2300 -- implicit dereference. If type matches context, no implicit
2301 -- dereference is involved.
2303 elsif Has_Implicit_Dereference (Expr_Type) then
2304 Set_Etype (N, Expr_Type);
2305 Set_Is_Overloaded (N, False);
2308 elsif Is_Overloaded (N)
2309 and then Present (It.Nam)
2310 and then Ekind (It.Nam) = E_Discriminant
2311 and then Has_Implicit_Dereference (It.Nam)
2313 Build_Explicit_Dereference (N, It.Nam);
2315 -- For an explicit dereference, attribute reference, range,
2316 -- short-circuit form (which is not an operator node), or call
2317 -- with a name that is an explicit dereference, there is
2318 -- nothing to be done at this point.
2320 elsif Nkind_In (N, N_Explicit_Dereference,
2321 N_Attribute_Reference,
2323 N_Indexed_Component,
2326 N_Selected_Component,
2328 or else Nkind (Name (N)) = N_Explicit_Dereference
2332 -- For procedure or function calls, set the type of the name,
2333 -- and also the entity pointer for the prefix.
2335 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2336 and then Is_Entity_Name (Name (N))
2338 Set_Etype (Name (N), Expr_Type);
2339 Set_Entity (Name (N), Seen);
2340 Generate_Reference (Seen, Name (N));
2342 elsif Nkind (N) = N_Function_Call
2343 and then Nkind (Name (N)) = N_Selected_Component
2345 Set_Etype (Name (N), Expr_Type);
2346 Set_Entity (Selector_Name (Name (N)), Seen);
2347 Generate_Reference (Seen, Selector_Name (Name (N)));
2349 -- For all other cases, just set the type of the Name
2352 Set_Etype (Name (N), Expr_Type);
2359 -- Move to next interpretation
2361 exit Interp_Loop when No (It.Typ);
2363 Get_Next_Interp (I, It);
2364 end loop Interp_Loop;
2367 -- At this stage Found indicates whether or not an acceptable
2368 -- interpretation exists. If not, then we have an error, except that if
2369 -- the context is Any_Type as a result of some other error, then we
2370 -- suppress the error report.
2373 if Typ /= Any_Type then
2375 -- If type we are looking for is Void, then this is the procedure
2376 -- call case, and the error is simply that what we gave is not a
2377 -- procedure name (we think of procedure calls as expressions with
2378 -- types internally, but the user doesn't think of them this way!)
2380 if Typ = Standard_Void_Type then
2382 -- Special case message if function used as a procedure
2384 if Nkind (N) = N_Procedure_Call_Statement
2385 and then Is_Entity_Name (Name (N))
2386 and then Ekind (Entity (Name (N))) = E_Function
2389 ("cannot use function & in a procedure call",
2390 Name (N), Entity (Name (N)));
2392 -- Otherwise give general message (not clear what cases this
2393 -- covers, but no harm in providing for them!)
2396 Error_Msg_N ("expect procedure name in procedure call", N);
2401 -- Otherwise we do have a subexpression with the wrong type
2403 -- Check for the case of an allocator which uses an access type
2404 -- instead of the designated type. This is a common error and we
2405 -- specialize the message, posting an error on the operand of the
2406 -- allocator, complaining that we expected the designated type of
2409 elsif Nkind (N) = N_Allocator
2410 and then Ekind (Typ) in Access_Kind
2411 and then Ekind (Etype (N)) in Access_Kind
2412 and then Designated_Type (Etype (N)) = Typ
2414 Wrong_Type (Expression (N), Designated_Type (Typ));
2417 -- Check for view mismatch on Null in instances, for which the
2418 -- view-swapping mechanism has no identifier.
2420 elsif (In_Instance or else In_Inlined_Body)
2421 and then (Nkind (N) = N_Null)
2422 and then Is_Private_Type (Typ)
2423 and then Is_Access_Type (Full_View (Typ))
2425 Resolve (N, Full_View (Typ));
2429 -- Check for an aggregate. Sometimes we can get bogus aggregates
2430 -- from misuse of parentheses, and we are about to complain about
2431 -- the aggregate without even looking inside it.
2433 -- Instead, if we have an aggregate of type Any_Composite, then
2434 -- analyze and resolve the component fields, and then only issue
2435 -- another message if we get no errors doing this (otherwise
2436 -- assume that the errors in the aggregate caused the problem).
2438 elsif Nkind (N) = N_Aggregate
2439 and then Etype (N) = Any_Composite
2441 -- Disable expansion in any case. If there is a type mismatch
2442 -- it may be fatal to try to expand the aggregate. The flag
2443 -- would otherwise be set to false when the error is posted.
2445 Expander_Active := False;
2448 procedure Check_Aggr (Aggr : Node_Id);
2449 -- Check one aggregate, and set Found to True if we have a
2450 -- definite error in any of its elements
2452 procedure Check_Elmt (Aelmt : Node_Id);
2453 -- Check one element of aggregate and set Found to True if
2454 -- we definitely have an error in the element.
2460 procedure Check_Aggr (Aggr : Node_Id) is
2464 if Present (Expressions (Aggr)) then
2465 Elmt := First (Expressions (Aggr));
2466 while Present (Elmt) loop
2472 if Present (Component_Associations (Aggr)) then
2473 Elmt := First (Component_Associations (Aggr));
2474 while Present (Elmt) loop
2476 -- If this is a default-initialized component, then
2477 -- there is nothing to check. The box will be
2478 -- replaced by the appropriate call during late
2481 if not Box_Present (Elmt) then
2482 Check_Elmt (Expression (Elmt));
2494 procedure Check_Elmt (Aelmt : Node_Id) is
2496 -- If we have a nested aggregate, go inside it (to
2497 -- attempt a naked analyze-resolve of the aggregate can
2498 -- cause undesirable cascaded errors). Do not resolve
2499 -- expression if it needs a type from context, as for
2500 -- integer * fixed expression.
2502 if Nkind (Aelmt) = N_Aggregate then
2508 if not Is_Overloaded (Aelmt)
2509 and then Etype (Aelmt) /= Any_Fixed
2514 if Etype (Aelmt) = Any_Type then
2525 -- If an error message was issued already, Found got reset to
2526 -- True, so if it is still False, issue standard Wrong_Type msg.
2529 if Is_Overloaded (N)
2530 and then Nkind (N) = N_Function_Call
2533 Subp_Name : Node_Id;
2535 if Is_Entity_Name (Name (N)) then
2536 Subp_Name := Name (N);
2538 elsif Nkind (Name (N)) = N_Selected_Component then
2540 -- Protected operation: retrieve operation name
2542 Subp_Name := Selector_Name (Name (N));
2545 raise Program_Error;
2548 Error_Msg_Node_2 := Typ;
2549 Error_Msg_NE ("no visible interpretation of&" &
2550 " matches expected type&", N, Subp_Name);
2553 if All_Errors_Mode then
2555 Index : Interp_Index;
2559 Error_Msg_N ("\\possible interpretations:", N);
2561 Get_First_Interp (Name (N), Index, It);
2562 while Present (It.Nam) loop
2563 Error_Msg_Sloc := Sloc (It.Nam);
2564 Error_Msg_Node_2 := It.Nam;
2566 ("\\ type& for & declared#", N, It.Typ);
2567 Get_Next_Interp (Index, It);
2572 Error_Msg_N ("\use -gnatf for details", N);
2576 Wrong_Type (N, Typ);
2584 -- Test if we have more than one interpretation for the context
2586 elsif Ambiguous then
2590 -- Only one intepretation
2593 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2594 -- the "+" on T is abstract, and the operands are of universal type,
2595 -- the above code will have (incorrectly) resolved the "+" to the
2596 -- universal one in Standard. Therefore check for this case and give
2597 -- an error. We can't do this earlier, because it would cause legal
2598 -- cases to get errors (when some other type has an abstract "+").
2600 if Ada_Version >= Ada_2005 and then
2601 Nkind (N) in N_Op and then
2602 Is_Overloaded (N) and then
2603 Is_Universal_Numeric_Type (Etype (Entity (N)))
2605 Get_First_Interp (N, I, It);
2606 while Present (It.Typ) loop
2607 if Present (It.Abstract_Op) and then
2608 Etype (It.Abstract_Op) = Typ
2611 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2615 Get_Next_Interp (I, It);
2619 -- Here we have an acceptable interpretation for the context
2621 -- Propagate type information and normalize tree for various
2622 -- predefined operations. If the context only imposes a class of
2623 -- types, rather than a specific type, propagate the actual type
2626 if Typ = Any_Integer or else
2627 Typ = Any_Boolean or else
2628 Typ = Any_Modular or else
2629 Typ = Any_Real or else
2632 Ctx_Type := Expr_Type;
2634 -- Any_Fixed is legal in a real context only if a specific fixed-
2635 -- point type is imposed. If Norman Cohen can be confused by this,
2636 -- it deserves a separate message.
2639 and then Expr_Type = Any_Fixed
2641 Error_Msg_N ("illegal context for mixed mode operation", N);
2642 Set_Etype (N, Universal_Real);
2643 Ctx_Type := Universal_Real;
2647 -- A user-defined operator is transformed into a function call at
2648 -- this point, so that further processing knows that operators are
2649 -- really operators (i.e. are predefined operators). User-defined
2650 -- operators that are intrinsic are just renamings of the predefined
2651 -- ones, and need not be turned into calls either, but if they rename
2652 -- a different operator, we must transform the node accordingly.
2653 -- Instantiations of Unchecked_Conversion are intrinsic but are
2654 -- treated as functions, even if given an operator designator.
2656 if Nkind (N) in N_Op
2657 and then Present (Entity (N))
2658 and then Ekind (Entity (N)) /= E_Operator
2661 if not Is_Predefined_Op (Entity (N)) then
2662 Rewrite_Operator_As_Call (N, Entity (N));
2664 elsif Present (Alias (Entity (N)))
2666 Nkind (Parent (Parent (Entity (N)))) =
2667 N_Subprogram_Renaming_Declaration
2669 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2671 -- If the node is rewritten, it will be fully resolved in
2672 -- Rewrite_Renamed_Operator.
2674 if Analyzed (N) then
2680 case N_Subexpr'(Nkind (N)) is
2682 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2684 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2686 when N_Short_Circuit
2687 => Resolve_Short_Circuit (N, Ctx_Type);
2689 when N_Attribute_Reference
2690 => Resolve_Attribute (N, Ctx_Type);
2692 when N_Case_Expression
2693 => Resolve_Case_Expression (N, Ctx_Type);
2695 when N_Character_Literal
2696 => Resolve_Character_Literal (N, Ctx_Type);
2698 when N_Conditional_Expression
2699 => Resolve_Conditional_Expression (N, Ctx_Type);
2701 when N_Expanded_Name
2702 => Resolve_Entity_Name (N, Ctx_Type);
2704 when N_Explicit_Dereference
2705 => Resolve_Explicit_Dereference (N, Ctx_Type);
2707 when N_Expression_With_Actions
2708 => Resolve_Expression_With_Actions (N, Ctx_Type);
2710 when N_Extension_Aggregate
2711 => Resolve_Extension_Aggregate (N, Ctx_Type);
2713 when N_Function_Call
2714 => Resolve_Call (N, Ctx_Type);
2717 => Resolve_Entity_Name (N, Ctx_Type);
2719 when N_Indexed_Component
2720 => Resolve_Indexed_Component (N, Ctx_Type);
2722 when N_Integer_Literal
2723 => Resolve_Integer_Literal (N, Ctx_Type);
2725 when N_Membership_Test
2726 => Resolve_Membership_Op (N, Ctx_Type);
2728 when N_Null => Resolve_Null (N, Ctx_Type);
2730 when N_Op_And | N_Op_Or | N_Op_Xor
2731 => Resolve_Logical_Op (N, Ctx_Type);
2733 when N_Op_Eq | N_Op_Ne
2734 => Resolve_Equality_Op (N, Ctx_Type);
2736 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2737 => Resolve_Comparison_Op (N, Ctx_Type);
2739 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2741 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2742 N_Op_Divide | N_Op_Mod | N_Op_Rem
2744 => Resolve_Arithmetic_Op (N, Ctx_Type);
2746 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2748 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2750 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2751 => Resolve_Unary_Op (N, Ctx_Type);
2753 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2755 when N_Procedure_Call_Statement
2756 => Resolve_Call (N, Ctx_Type);
2758 when N_Operator_Symbol
2759 => Resolve_Operator_Symbol (N, Ctx_Type);
2761 when N_Qualified_Expression
2762 => Resolve_Qualified_Expression (N, Ctx_Type);
2764 when N_Quantified_Expression
2765 => Resolve_Quantified_Expression (N, Ctx_Type);
2767 when N_Raise_xxx_Error
2768 => Set_Etype (N, Ctx_Type);
2770 when N_Range => Resolve_Range (N, Ctx_Type);
2773 => Resolve_Real_Literal (N, Ctx_Type);
2775 when N_Reference => Resolve_Reference (N, Ctx_Type);
2777 when N_Selected_Component
2778 => Resolve_Selected_Component (N, Ctx_Type);
2780 when N_Slice => Resolve_Slice (N, Ctx_Type);
2782 when N_String_Literal
2783 => Resolve_String_Literal (N, Ctx_Type);
2785 when N_Subprogram_Info
2786 => Resolve_Subprogram_Info (N, Ctx_Type);
2788 when N_Type_Conversion
2789 => Resolve_Type_Conversion (N, Ctx_Type);
2791 when N_Unchecked_Expression =>
2792 Resolve_Unchecked_Expression (N, Ctx_Type);
2794 when N_Unchecked_Type_Conversion =>
2795 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2798 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2799 -- expression of an anonymous access type that occurs in the context
2800 -- of a named general access type, except when the expression is that
2801 -- of a membership test. This ensures proper legality checking in
2802 -- terms of allowed conversions (expressions that would be illegal to
2803 -- convert implicitly are allowed in membership tests).
2805 if Ada_Version >= Ada_2012
2806 and then Ekind (Ctx_Type) = E_General_Access_Type
2807 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2808 and then Nkind (Parent (N)) not in N_Membership_Test
2810 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2811 Analyze_And_Resolve (N, Ctx_Type);
2814 -- If the subexpression was replaced by a non-subexpression, then
2815 -- all we do is to expand it. The only legitimate case we know of
2816 -- is converting procedure call statement to entry call statements,
2817 -- but there may be others, so we are making this test general.
2819 if Nkind (N) not in N_Subexpr then
2820 Debug_A_Exit ("resolving ", N, " (done)");
2825 -- AI05-144-2: Check dangerous order dependence within an expression
2826 -- that is not a subexpression. Exclude RHS of an assignment, because
2827 -- both sides may have side-effects and the check must be performed
2828 -- over the statement.
2830 if Nkind (Parent (N)) not in N_Subexpr
2831 and then Nkind (Parent (N)) /= N_Assignment_Statement
2832 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2834 Check_Order_Dependence;
2837 -- The expression is definitely NOT overloaded at this point, so
2838 -- we reset the Is_Overloaded flag to avoid any confusion when
2839 -- reanalyzing the node.
2841 Set_Is_Overloaded (N, False);
2843 -- Freeze expression type, entity if it is a name, and designated
2844 -- type if it is an allocator (RM 13.14(10,11,13)).
2846 -- Now that the resolution of the type of the node is complete, and
2847 -- we did not detect an error, we can expand this node. We skip the
2848 -- expand call if we are in a default expression, see section
2849 -- "Handling of Default Expressions" in Sem spec.
2851 Debug_A_Exit ("resolving ", N, " (done)");
2853 -- We unconditionally freeze the expression, even if we are in
2854 -- default expression mode (the Freeze_Expression routine tests this
2855 -- flag and only freezes static types if it is set).
2857 -- Ada 2012 (AI05-177): Expression functions do not freeze. Only
2858 -- their use (in an expanded call) freezes.
2860 if Ekind (Current_Scope) /= E_Function
2862 Nkind (Original_Node (Unit_Declaration_Node (Current_Scope))) /=
2863 N_Expression_Function
2865 Freeze_Expression (N);
2868 -- Now we can do the expansion
2878 -- Version with check(s) suppressed
2880 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2882 if Suppress = All_Checks then
2884 Svg : constant Suppress_Array := Scope_Suppress;
2886 Scope_Suppress := (others => True);
2888 Scope_Suppress := Svg;
2893 Svg : constant Boolean := Scope_Suppress (Suppress);
2895 Scope_Suppress (Suppress) := True;
2897 Scope_Suppress (Suppress) := Svg;
2906 -- Version with implicit type
2908 procedure Resolve (N : Node_Id) is
2910 Resolve (N, Etype (N));
2913 ---------------------
2914 -- Resolve_Actuals --
2915 ---------------------
2917 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2918 Loc : constant Source_Ptr := Sloc (N);
2923 Prev : Node_Id := Empty;
2926 procedure Check_Argument_Order;
2927 -- Performs a check for the case where the actuals are all simple
2928 -- identifiers that correspond to the formal names, but in the wrong
2929 -- order, which is considered suspicious and cause for a warning.
2931 procedure Check_Prefixed_Call;
2932 -- If the original node is an overloaded call in prefix notation,
2933 -- insert an 'Access or a dereference as needed over the first actual.
2934 -- Try_Object_Operation has already verified that there is a valid
2935 -- interpretation, but the form of the actual can only be determined
2936 -- once the primitive operation is identified.
2938 procedure Insert_Default;
2939 -- If the actual is missing in a call, insert in the actuals list
2940 -- an instance of the default expression. The insertion is always
2941 -- a named association.
2943 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2944 -- Check whether T1 and T2, or their full views, are derived from a
2945 -- common type. Used to enforce the restrictions on array conversions
2948 function Static_Concatenation (N : Node_Id) return Boolean;
2949 -- Predicate to determine whether an actual that is a concatenation
2950 -- will be evaluated statically and does not need a transient scope.
2951 -- This must be determined before the actual is resolved and expanded
2952 -- because if needed the transient scope must be introduced earlier.
2954 --------------------------
2955 -- Check_Argument_Order --
2956 --------------------------
2958 procedure Check_Argument_Order is
2960 -- Nothing to do if no parameters, or original node is neither a
2961 -- function call nor a procedure call statement (happens in the
2962 -- operator-transformed-to-function call case), or the call does
2963 -- not come from source, or this warning is off.
2965 if not Warn_On_Parameter_Order
2966 or else No (Parameter_Associations (N))
2967 or else not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2969 or else not Comes_From_Source (N)
2975 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2978 -- Nothing to do if only one parameter
2984 -- Here if at least two arguments
2987 Actuals : array (1 .. Nargs) of Node_Id;
2991 Wrong_Order : Boolean := False;
2992 -- Set True if an out of order case is found
2995 -- Collect identifier names of actuals, fail if any actual is
2996 -- not a simple identifier, and record max length of name.
2998 Actual := First (Parameter_Associations (N));
2999 for J in Actuals'Range loop
3000 if Nkind (Actual) /= N_Identifier then
3003 Actuals (J) := Actual;
3008 -- If we got this far, all actuals are identifiers and the list
3009 -- of their names is stored in the Actuals array.
3011 Formal := First_Formal (Nam);
3012 for J in Actuals'Range loop
3014 -- If we ran out of formals, that's odd, probably an error
3015 -- which will be detected elsewhere, but abandon the search.
3021 -- If name matches and is in order OK
3023 if Chars (Formal) = Chars (Actuals (J)) then
3027 -- If no match, see if it is elsewhere in list and if so
3028 -- flag potential wrong order if type is compatible.
3030 for K in Actuals'Range loop
3031 if Chars (Formal) = Chars (Actuals (K))
3033 Has_Compatible_Type (Actuals (K), Etype (Formal))
3035 Wrong_Order := True;
3045 <<Continue>> Next_Formal (Formal);
3048 -- If Formals left over, also probably an error, skip warning
3050 if Present (Formal) then
3054 -- Here we give the warning if something was out of order
3058 ("actuals for this call may be in wrong order?", N);
3062 end Check_Argument_Order;
3064 -------------------------
3065 -- Check_Prefixed_Call --
3066 -------------------------
3068 procedure Check_Prefixed_Call is
3069 Act : constant Node_Id := First_Actual (N);
3070 A_Type : constant Entity_Id := Etype (Act);
3071 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3072 Orig : constant Node_Id := Original_Node (N);
3076 -- Check whether the call is a prefixed call, with or without
3077 -- additional actuals.
3079 if Nkind (Orig) = N_Selected_Component
3081 (Nkind (Orig) = N_Indexed_Component
3082 and then Nkind (Prefix (Orig)) = N_Selected_Component
3083 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3084 and then Is_Entity_Name (Act)
3085 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3087 if Is_Access_Type (A_Type)
3088 and then not Is_Access_Type (F_Type)
3090 -- Introduce dereference on object in prefix
3093 Make_Explicit_Dereference (Sloc (Act),
3094 Prefix => Relocate_Node (Act));
3095 Rewrite (Act, New_A);
3098 elsif Is_Access_Type (F_Type)
3099 and then not Is_Access_Type (A_Type)
3101 -- Introduce an implicit 'Access in prefix
3103 if not Is_Aliased_View (Act) then
3105 ("object in prefixed call to& must be aliased"
3106 & " (RM-2005 4.3.1 (13))",
3111 Make_Attribute_Reference (Loc,
3112 Attribute_Name => Name_Access,
3113 Prefix => Relocate_Node (Act)));
3118 end Check_Prefixed_Call;
3120 --------------------
3121 -- Insert_Default --
3122 --------------------
3124 procedure Insert_Default is
3129 -- Missing argument in call, nothing to insert
3131 if No (Default_Value (F)) then
3135 -- Note that we do a full New_Copy_Tree, so that any associated
3136 -- Itypes are properly copied. This may not be needed any more,
3137 -- but it does no harm as a safety measure! Defaults of a generic
3138 -- formal may be out of bounds of the corresponding actual (see
3139 -- cc1311b) and an additional check may be required.
3144 New_Scope => Current_Scope,
3147 if Is_Concurrent_Type (Scope (Nam))
3148 and then Has_Discriminants (Scope (Nam))
3150 Replace_Actual_Discriminants (N, Actval);
3153 if Is_Overloadable (Nam)
3154 and then Present (Alias (Nam))
3156 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3157 and then not Is_Tagged_Type (Etype (F))
3159 -- If default is a real literal, do not introduce a
3160 -- conversion whose effect may depend on the run-time
3161 -- size of universal real.
3163 if Nkind (Actval) = N_Real_Literal then
3164 Set_Etype (Actval, Base_Type (Etype (F)));
3166 Actval := Unchecked_Convert_To (Etype (F), Actval);
3170 if Is_Scalar_Type (Etype (F)) then
3171 Enable_Range_Check (Actval);
3174 Set_Parent (Actval, N);
3176 -- Resolve aggregates with their base type, to avoid scope
3177 -- anomalies: the subtype was first built in the subprogram
3178 -- declaration, and the current call may be nested.
3180 if Nkind (Actval) = N_Aggregate then
3181 Analyze_And_Resolve (Actval, Etype (F));
3183 Analyze_And_Resolve (Actval, Etype (Actval));
3187 Set_Parent (Actval, N);
3189 -- See note above concerning aggregates
3191 if Nkind (Actval) = N_Aggregate
3192 and then Has_Discriminants (Etype (Actval))
3194 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3196 -- Resolve entities with their own type, which may differ from
3197 -- the type of a reference in a generic context (the view
3198 -- swapping mechanism did not anticipate the re-analysis of
3199 -- default values in calls).
3201 elsif Is_Entity_Name (Actval) then
3202 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3205 Analyze_And_Resolve (Actval, Etype (Actval));
3209 -- If default is a tag indeterminate function call, propagate tag
3210 -- to obtain proper dispatching.
3212 if Is_Controlling_Formal (F)
3213 and then Nkind (Default_Value (F)) = N_Function_Call
3215 Set_Is_Controlling_Actual (Actval);
3220 -- If the default expression raises constraint error, then just
3221 -- silently replace it with an N_Raise_Constraint_Error node, since
3222 -- we already gave the warning on the subprogram spec. If node is
3223 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3224 -- the warnings removal machinery.
3226 if Raises_Constraint_Error (Actval)
3227 and then Nkind (Actval) /= N_Raise_Constraint_Error
3230 Make_Raise_Constraint_Error (Loc,
3231 Reason => CE_Range_Check_Failed));
3232 Set_Raises_Constraint_Error (Actval);
3233 Set_Etype (Actval, Etype (F));
3237 Make_Parameter_Association (Loc,
3238 Explicit_Actual_Parameter => Actval,
3239 Selector_Name => Make_Identifier (Loc, Chars (F)));
3241 -- Case of insertion is first named actual
3243 if No (Prev) or else
3244 Nkind (Parent (Prev)) /= N_Parameter_Association
3246 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3247 Set_First_Named_Actual (N, Actval);
3250 if No (Parameter_Associations (N)) then
3251 Set_Parameter_Associations (N, New_List (Assoc));
3253 Append (Assoc, Parameter_Associations (N));
3257 Insert_After (Prev, Assoc);
3260 -- Case of insertion is not first named actual
3263 Set_Next_Named_Actual
3264 (Assoc, Next_Named_Actual (Parent (Prev)));
3265 Set_Next_Named_Actual (Parent (Prev), Actval);
3266 Append (Assoc, Parameter_Associations (N));
3269 Mark_Rewrite_Insertion (Assoc);
3270 Mark_Rewrite_Insertion (Actval);
3279 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3280 FT1 : Entity_Id := T1;
3281 FT2 : Entity_Id := T2;
3284 if Is_Private_Type (T1)
3285 and then Present (Full_View (T1))
3287 FT1 := Full_View (T1);
3290 if Is_Private_Type (T2)
3291 and then Present (Full_View (T2))
3293 FT2 := Full_View (T2);
3296 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3299 --------------------------
3300 -- Static_Concatenation --
3301 --------------------------
3303 function Static_Concatenation (N : Node_Id) return Boolean is
3306 when N_String_Literal =>
3311 -- Concatenation is static when both operands are static and
3312 -- the concatenation operator is a predefined one.
3314 return Scope (Entity (N)) = Standard_Standard
3316 Static_Concatenation (Left_Opnd (N))
3318 Static_Concatenation (Right_Opnd (N));
3321 if Is_Entity_Name (N) then
3323 Ent : constant Entity_Id := Entity (N);
3325 return Ekind (Ent) = E_Constant
3326 and then Present (Constant_Value (Ent))
3328 Is_Static_Expression (Constant_Value (Ent));
3335 end Static_Concatenation;
3337 -- Start of processing for Resolve_Actuals
3340 Check_Argument_Order;
3342 if Present (First_Actual (N)) then
3343 Check_Prefixed_Call;
3346 A := First_Actual (N);
3347 F := First_Formal (Nam);
3348 while Present (F) loop
3349 if No (A) and then Needs_No_Actuals (Nam) then
3352 -- If we have an error in any actual or formal, indicated by a type
3353 -- of Any_Type, then abandon resolution attempt, and set result type
3356 elsif (Present (A) and then Etype (A) = Any_Type)
3357 or else Etype (F) = Any_Type
3359 Set_Etype (N, Any_Type);
3363 -- Case where actual is present
3365 -- If the actual is an entity, generate a reference to it now. We
3366 -- do this before the actual is resolved, because a formal of some
3367 -- protected subprogram, or a task discriminant, will be rewritten
3368 -- during expansion, and the source entity reference may be lost.
3371 and then Is_Entity_Name (A)
3372 and then Comes_From_Source (N)
3374 Orig_A := Entity (A);
3376 if Present (Orig_A) then
3377 if Is_Formal (Orig_A)
3378 and then Ekind (F) /= E_In_Parameter
3380 Generate_Reference (Orig_A, A, 'm');
3382 elsif not Is_Overloaded (A) then
3383 Generate_Reference (Orig_A, A);
3389 and then (Nkind (Parent (A)) /= N_Parameter_Association
3390 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3392 -- If style checking mode on, check match of formal name
3395 if Nkind (Parent (A)) = N_Parameter_Association then
3396 Check_Identifier (Selector_Name (Parent (A)), F);
3400 -- If the formal is Out or In_Out, do not resolve and expand the
3401 -- conversion, because it is subsequently expanded into explicit
3402 -- temporaries and assignments. However, the object of the
3403 -- conversion can be resolved. An exception is the case of tagged
3404 -- type conversion with a class-wide actual. In that case we want
3405 -- the tag check to occur and no temporary will be needed (no
3406 -- representation change can occur) and the parameter is passed by
3407 -- reference, so we go ahead and resolve the type conversion.
3408 -- Another exception is the case of reference to component or
3409 -- subcomponent of a bit-packed array, in which case we want to
3410 -- defer expansion to the point the in and out assignments are
3413 if Ekind (F) /= E_In_Parameter
3414 and then Nkind (A) = N_Type_Conversion
3415 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3417 if Ekind (F) = E_In_Out_Parameter
3418 and then Is_Array_Type (Etype (F))
3420 -- In a view conversion, the conversion must be legal in
3421 -- both directions, and thus both component types must be
3422 -- aliased, or neither (4.6 (8)).
3424 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3425 -- the privacy requirement should not apply to generic
3426 -- types, and should be checked in an instance. ARG query
3429 if Has_Aliased_Components (Etype (Expression (A))) /=
3430 Has_Aliased_Components (Etype (F))
3433 ("both component types in a view conversion must be"
3434 & " aliased, or neither", A);
3436 -- Comment here??? what set of cases???
3439 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3441 -- Check view conv between unrelated by ref array types
3443 if Is_By_Reference_Type (Etype (F))
3444 or else Is_By_Reference_Type (Etype (Expression (A)))
3447 ("view conversion between unrelated by reference " &
3448 "array types not allowed (\'A'I-00246)", A);
3450 -- In Ada 2005 mode, check view conversion component
3451 -- type cannot be private, tagged, or volatile. Note
3452 -- that we only apply this to source conversions. The
3453 -- generated code can contain conversions which are
3454 -- not subject to this test, and we cannot extract the
3455 -- component type in such cases since it is not present.
3457 elsif Comes_From_Source (A)
3458 and then Ada_Version >= Ada_2005
3461 Comp_Type : constant Entity_Id :=
3463 (Etype (Expression (A)));
3465 if (Is_Private_Type (Comp_Type)
3466 and then not Is_Generic_Type (Comp_Type))
3467 or else Is_Tagged_Type (Comp_Type)
3468 or else Is_Volatile (Comp_Type)
3471 ("component type of a view conversion cannot"
3472 & " be private, tagged, or volatile"
3481 -- Resolve expression if conversion is all OK
3483 if (Conversion_OK (A)
3484 or else Valid_Conversion (A, Etype (A), Expression (A)))
3485 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3487 Resolve (Expression (A));
3490 -- If the actual is a function call that returns a limited
3491 -- unconstrained object that needs finalization, create a
3492 -- transient scope for it, so that it can receive the proper
3493 -- finalization list.
3495 elsif Nkind (A) = N_Function_Call
3496 and then Is_Limited_Record (Etype (F))
3497 and then not Is_Constrained (Etype (F))
3498 and then Full_Expander_Active
3499 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3501 Establish_Transient_Scope (A, False);
3502 Resolve (A, Etype (F));
3504 -- A small optimization: if one of the actuals is a concatenation
3505 -- create a block around a procedure call to recover stack space.
3506 -- This alleviates stack usage when several procedure calls in
3507 -- the same statement list use concatenation. We do not perform
3508 -- this wrapping for code statements, where the argument is a
3509 -- static string, and we want to preserve warnings involving
3510 -- sequences of such statements.
3512 elsif Nkind (A) = N_Op_Concat
3513 and then Nkind (N) = N_Procedure_Call_Statement
3514 and then Full_Expander_Active
3516 not (Is_Intrinsic_Subprogram (Nam)
3517 and then Chars (Nam) = Name_Asm)
3518 and then not Static_Concatenation (A)
3520 Establish_Transient_Scope (A, False);
3521 Resolve (A, Etype (F));
3524 if Nkind (A) = N_Type_Conversion
3525 and then Is_Array_Type (Etype (F))
3526 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3528 (Is_Limited_Type (Etype (F))
3529 or else Is_Limited_Type (Etype (Expression (A))))
3532 ("conversion between unrelated limited array types " &
3533 "not allowed (\A\I-00246)", A);
3535 if Is_Limited_Type (Etype (F)) then
3536 Explain_Limited_Type (Etype (F), A);
3539 if Is_Limited_Type (Etype (Expression (A))) then
3540 Explain_Limited_Type (Etype (Expression (A)), A);
3544 -- (Ada 2005: AI-251): If the actual is an allocator whose
3545 -- directly designated type is a class-wide interface, we build
3546 -- an anonymous access type to use it as the type of the
3547 -- allocator. Later, when the subprogram call is expanded, if
3548 -- the interface has a secondary dispatch table the expander
3549 -- will add a type conversion to force the correct displacement
3552 if Nkind (A) = N_Allocator then
3554 DDT : constant Entity_Id :=
3555 Directly_Designated_Type (Base_Type (Etype (F)));
3557 New_Itype : Entity_Id;
3560 if Is_Class_Wide_Type (DDT)
3561 and then Is_Interface (DDT)
3563 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3564 Set_Etype (New_Itype, Etype (A));
3565 Set_Directly_Designated_Type (New_Itype,
3566 Directly_Designated_Type (Etype (A)));
3567 Set_Etype (A, New_Itype);
3570 -- Ada 2005, AI-162:If the actual is an allocator, the
3571 -- innermost enclosing statement is the master of the
3572 -- created object. This needs to be done with expansion
3573 -- enabled only, otherwise the transient scope will not
3574 -- be removed in the expansion of the wrapped construct.
3576 if (Is_Controlled (DDT) or else Has_Task (DDT))
3577 and then Full_Expander_Active
3579 Establish_Transient_Scope (A, False);
3584 -- (Ada 2005): The call may be to a primitive operation of
3585 -- a tagged synchronized type, declared outside of the type.
3586 -- In this case the controlling actual must be converted to
3587 -- its corresponding record type, which is the formal type.
3588 -- The actual may be a subtype, either because of a constraint
3589 -- or because it is a generic actual, so use base type to
3590 -- locate concurrent type.
3592 F_Typ := Base_Type (Etype (F));
3594 if Is_Tagged_Type (F_Typ)
3595 and then (Is_Concurrent_Type (F_Typ)
3596 or else Is_Concurrent_Record_Type (F_Typ))
3598 -- If the actual is overloaded, look for an interpretation
3599 -- that has a synchronized type.
3601 if not Is_Overloaded (A) then
3602 A_Typ := Base_Type (Etype (A));
3606 Index : Interp_Index;
3610 Get_First_Interp (A, Index, It);
3611 while Present (It.Typ) loop
3612 if Is_Concurrent_Type (It.Typ)
3613 or else Is_Concurrent_Record_Type (It.Typ)
3615 A_Typ := Base_Type (It.Typ);
3619 Get_Next_Interp (Index, It);
3625 Full_A_Typ : Entity_Id;
3628 if Present (Full_View (A_Typ)) then
3629 Full_A_Typ := Base_Type (Full_View (A_Typ));
3631 Full_A_Typ := A_Typ;
3634 -- Tagged synchronized type (case 1): the actual is a
3637 if Is_Concurrent_Type (A_Typ)
3638 and then Corresponding_Record_Type (A_Typ) = F_Typ
3641 Unchecked_Convert_To
3642 (Corresponding_Record_Type (A_Typ), A));
3643 Resolve (A, Etype (F));
3645 -- Tagged synchronized type (case 2): the formal is a
3648 elsif Ekind (Full_A_Typ) = E_Record_Type
3650 (Corresponding_Concurrent_Type (Full_A_Typ))
3651 and then Is_Concurrent_Type (F_Typ)
3652 and then Present (Corresponding_Record_Type (F_Typ))
3653 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3655 Resolve (A, Corresponding_Record_Type (F_Typ));
3660 Resolve (A, Etype (F));
3665 -- not a synchronized operation.
3667 Resolve (A, Etype (F));
3674 if Comes_From_Source (Original_Node (N))
3675 and then Nkind_In (Original_Node (N), N_Function_Call,
3676 N_Procedure_Call_Statement)
3678 -- In formal mode, check that actual parameters matching
3679 -- formals of tagged types are objects (or ancestor type
3680 -- conversions of objects), not general expressions.
3682 if Is_Actual_Tagged_Parameter (A) then
3683 if Is_SPARK_Object_Reference (A) then
3686 elsif Nkind (A) = N_Type_Conversion then
3688 Operand : constant Node_Id := Expression (A);
3689 Operand_Typ : constant Entity_Id := Etype (Operand);
3690 Target_Typ : constant Entity_Id := A_Typ;
3693 if not Is_SPARK_Object_Reference (Operand) then
3694 Check_SPARK_Restriction
3695 ("object required", Operand);
3697 -- In formal mode, the only view conversions are those
3698 -- involving ancestor conversion of an extended type.
3701 (Is_Tagged_Type (Target_Typ)
3702 and then not Is_Class_Wide_Type (Target_Typ)
3703 and then Is_Tagged_Type (Operand_Typ)
3704 and then not Is_Class_Wide_Type (Operand_Typ)
3705 and then Is_Ancestor (Target_Typ, Operand_Typ))
3708 (F, E_Out_Parameter, E_In_Out_Parameter)
3710 Check_SPARK_Restriction
3711 ("ancestor conversion is the only permitted "
3712 & "view conversion", A);
3714 Check_SPARK_Restriction
3715 ("ancestor conversion required", A);
3724 Check_SPARK_Restriction ("object required", A);
3727 -- In formal mode, the only view conversions are those
3728 -- involving ancestor conversion of an extended type.
3730 elsif Nkind (A) = N_Type_Conversion
3731 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3733 Check_SPARK_Restriction
3734 ("ancestor conversion is the only permitted view "
3739 -- Save actual for subsequent check on order dependence, and
3740 -- indicate whether actual is modifiable. For AI05-0144-2.
3742 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3744 -- For mode IN, if actual is an entity, and the type of the formal
3745 -- has warnings suppressed, then we reset Never_Set_In_Source for
3746 -- the calling entity. The reason for this is to catch cases like
3747 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3748 -- uses trickery to modify an IN parameter.
3750 if Ekind (F) = E_In_Parameter
3751 and then Is_Entity_Name (A)
3752 and then Present (Entity (A))
3753 and then Ekind (Entity (A)) = E_Variable
3754 and then Has_Warnings_Off (F_Typ)
3756 Set_Never_Set_In_Source (Entity (A), False);
3759 -- Perform error checks for IN and IN OUT parameters
3761 if Ekind (F) /= E_Out_Parameter then
3763 -- Check unset reference. For scalar parameters, it is clearly
3764 -- wrong to pass an uninitialized value as either an IN or
3765 -- IN-OUT parameter. For composites, it is also clearly an
3766 -- error to pass a completely uninitialized value as an IN
3767 -- parameter, but the case of IN OUT is trickier. We prefer
3768 -- not to give a warning here. For example, suppose there is
3769 -- a routine that sets some component of a record to False.
3770 -- It is perfectly reasonable to make this IN-OUT and allow
3771 -- either initialized or uninitialized records to be passed
3774 -- For partially initialized composite values, we also avoid
3775 -- warnings, since it is quite likely that we are passing a
3776 -- partially initialized value and only the initialized fields
3777 -- will in fact be read in the subprogram.
3779 if Is_Scalar_Type (A_Typ)
3780 or else (Ekind (F) = E_In_Parameter
3781 and then not Is_Partially_Initialized_Type (A_Typ))
3783 Check_Unset_Reference (A);
3786 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3787 -- actual to a nested call, since this is case of reading an
3788 -- out parameter, which is not allowed.
3790 if Ada_Version = Ada_83
3791 and then Is_Entity_Name (A)
3792 and then Ekind (Entity (A)) = E_Out_Parameter
3794 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3798 -- Case of OUT or IN OUT parameter
3800 if Ekind (F) /= E_In_Parameter then
3802 -- For an Out parameter, check for useless assignment. Note
3803 -- that we can't set Last_Assignment this early, because we may
3804 -- kill current values in Resolve_Call, and that call would
3805 -- clobber the Last_Assignment field.
3807 -- Note: call Warn_On_Useless_Assignment before doing the check
3808 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3809 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3810 -- reflects the last assignment, not this one!
3812 if Ekind (F) = E_Out_Parameter then
3813 if Warn_On_Modified_As_Out_Parameter (F)
3814 and then Is_Entity_Name (A)
3815 and then Present (Entity (A))
3816 and then Comes_From_Source (N)
3818 Warn_On_Useless_Assignment (Entity (A), A);
3822 -- Validate the form of the actual. Note that the call to
3823 -- Is_OK_Variable_For_Out_Formal generates the required
3824 -- reference in this case.
3826 -- A call to an initialization procedure for an aggregate
3827 -- component may initialize a nested component of a constant
3828 -- designated object. In this context the object is variable.
3830 if not Is_OK_Variable_For_Out_Formal (A)
3831 and then not Is_Init_Proc (Nam)
3833 Error_Msg_NE ("actual for& must be a variable", A, F);
3836 -- What's the following about???
3838 if Is_Entity_Name (A) then
3839 Kill_Checks (Entity (A));
3845 if Etype (A) = Any_Type then
3846 Set_Etype (N, Any_Type);
3850 -- Apply appropriate range checks for in, out, and in-out
3851 -- parameters. Out and in-out parameters also need a separate
3852 -- check, if there is a type conversion, to make sure the return
3853 -- value meets the constraints of the variable before the
3856 -- Gigi looks at the check flag and uses the appropriate types.
3857 -- For now since one flag is used there is an optimization which
3858 -- might not be done in the In Out case since Gigi does not do
3859 -- any analysis. More thought required about this ???
3861 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3863 -- Apply predicate checks, unless this is a call to the
3864 -- predicate check function itself, which would cause an
3865 -- infinite recursion.
3867 if not (Ekind (Nam) = E_Function
3868 and then Has_Predicates (Nam))
3870 Apply_Predicate_Check (A, F_Typ);
3873 -- Apply required constraint checks
3875 if Is_Scalar_Type (Etype (A)) then
3876 Apply_Scalar_Range_Check (A, F_Typ);
3878 elsif Is_Array_Type (Etype (A)) then
3879 Apply_Length_Check (A, F_Typ);
3881 elsif Is_Record_Type (F_Typ)
3882 and then Has_Discriminants (F_Typ)
3883 and then Is_Constrained (F_Typ)
3884 and then (not Is_Derived_Type (F_Typ)
3885 or else Comes_From_Source (Nam))
3887 Apply_Discriminant_Check (A, F_Typ);
3889 elsif Is_Access_Type (F_Typ)
3890 and then Is_Array_Type (Designated_Type (F_Typ))
3891 and then Is_Constrained (Designated_Type (F_Typ))
3893 Apply_Length_Check (A, F_Typ);
3895 elsif Is_Access_Type (F_Typ)
3896 and then Has_Discriminants (Designated_Type (F_Typ))
3897 and then Is_Constrained (Designated_Type (F_Typ))
3899 Apply_Discriminant_Check (A, F_Typ);
3902 Apply_Range_Check (A, F_Typ);
3905 -- Ada 2005 (AI-231): Note that the controlling parameter case
3906 -- already existed in Ada 95, which is partially checked
3907 -- elsewhere (see Checks), and we don't want the warning
3908 -- message to differ.
3910 if Is_Access_Type (F_Typ)
3911 and then Can_Never_Be_Null (F_Typ)
3912 and then Known_Null (A)
3914 if Is_Controlling_Formal (F) then
3915 Apply_Compile_Time_Constraint_Error
3917 Msg => "null value not allowed here?",
3918 Reason => CE_Access_Check_Failed);
3920 elsif Ada_Version >= Ada_2005 then
3921 Apply_Compile_Time_Constraint_Error
3923 Msg => "(Ada 2005) null not allowed in "
3924 & "null-excluding formal?",
3925 Reason => CE_Null_Not_Allowed);
3930 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3931 if Nkind (A) = N_Type_Conversion then
3932 if Is_Scalar_Type (A_Typ) then
3933 Apply_Scalar_Range_Check
3934 (Expression (A), Etype (Expression (A)), A_Typ);
3937 (Expression (A), Etype (Expression (A)), A_Typ);
3941 if Is_Scalar_Type (F_Typ) then
3942 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3943 elsif Is_Array_Type (F_Typ)
3944 and then Ekind (F) = E_Out_Parameter
3946 Apply_Length_Check (A, F_Typ);
3948 Apply_Range_Check (A, A_Typ, F_Typ);
3953 -- An actual associated with an access parameter is implicitly
3954 -- converted to the anonymous access type of the formal and must
3955 -- satisfy the legality checks for access conversions.
3957 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3958 if not Valid_Conversion (A, F_Typ, A) then
3960 ("invalid implicit conversion for access parameter", A);
3964 -- Check bad case of atomic/volatile argument (RM C.6(12))
3966 if Is_By_Reference_Type (Etype (F))
3967 and then Comes_From_Source (N)
3969 if Is_Atomic_Object (A)
3970 and then not Is_Atomic (Etype (F))
3973 ("cannot pass atomic argument to non-atomic formal&",
3976 elsif Is_Volatile_Object (A)
3977 and then not Is_Volatile (Etype (F))
3980 ("cannot pass volatile argument to non-volatile formal&",
3985 -- Check that subprograms don't have improper controlling
3986 -- arguments (RM 3.9.2 (9)).
3988 -- A primitive operation may have an access parameter of an
3989 -- incomplete tagged type, but a dispatching call is illegal
3990 -- if the type is still incomplete.
3992 if Is_Controlling_Formal (F) then
3993 Set_Is_Controlling_Actual (A);
3995 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3997 Desig : constant Entity_Id := Designated_Type (Etype (F));
3999 if Ekind (Desig) = E_Incomplete_Type
4000 and then No (Full_View (Desig))
4001 and then No (Non_Limited_View (Desig))
4004 ("premature use of incomplete type& " &
4005 "in dispatching call", A, Desig);
4010 elsif Nkind (A) = N_Explicit_Dereference then
4011 Validate_Remote_Access_To_Class_Wide_Type (A);
4014 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4015 and then not Is_Class_Wide_Type (F_Typ)
4016 and then not Is_Controlling_Formal (F)
4018 Error_Msg_N ("class-wide argument not allowed here!", A);
4020 if Is_Subprogram (Nam)
4021 and then Comes_From_Source (Nam)
4023 Error_Msg_Node_2 := F_Typ;
4025 ("& is not a dispatching operation of &!", A, Nam);
4028 -- Apply the checks described in 3.10.2(27): if the context is a
4029 -- specific access-to-object, the actual cannot be class-wide.
4030 -- Use base type to exclude access_to_subprogram cases.
4032 elsif Is_Access_Type (A_Typ)
4033 and then Is_Access_Type (F_Typ)
4034 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4035 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4036 or else (Nkind (A) = N_Attribute_Reference
4038 Is_Class_Wide_Type (Etype (Prefix (A)))))
4039 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4040 and then not Is_Controlling_Formal (F)
4042 -- Disable these checks for call to imported C++ subprograms
4045 (Is_Entity_Name (Name (N))
4046 and then Is_Imported (Entity (Name (N)))
4047 and then Convention (Entity (Name (N))) = Convention_CPP)
4050 ("access to class-wide argument not allowed here!", A);
4052 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4053 Error_Msg_Node_2 := Designated_Type (F_Typ);
4055 ("& is not a dispatching operation of &!", A, Nam);
4061 -- If it is a named association, treat the selector_name as a
4062 -- proper identifier, and mark the corresponding entity. Ignore
4063 -- this reference in Alfa mode, as it refers to an entity not in
4064 -- scope at the point of reference, so the reference should be
4065 -- ignored for computing effects of subprograms.
4067 if Nkind (Parent (A)) = N_Parameter_Association
4068 and then not Alfa_Mode
4070 Set_Entity (Selector_Name (Parent (A)), F);
4071 Generate_Reference (F, Selector_Name (Parent (A)));
4072 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4073 Generate_Reference (F_Typ, N, ' ');
4078 if Ekind (F) /= E_Out_Parameter then
4079 Check_Unset_Reference (A);
4084 -- Case where actual is not present
4092 end Resolve_Actuals;
4094 -----------------------
4095 -- Resolve_Allocator --
4096 -----------------------
4098 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4099 Desig_T : constant Entity_Id := Designated_Type (Typ);
4100 E : constant Node_Id := Expression (N);
4102 Discrim : Entity_Id;
4105 Assoc : Node_Id := Empty;
4108 procedure Check_Allocator_Discrim_Accessibility
4109 (Disc_Exp : Node_Id;
4110 Alloc_Typ : Entity_Id);
4111 -- Check that accessibility level associated with an access discriminant
4112 -- initialized in an allocator by the expression Disc_Exp is not deeper
4113 -- than the level of the allocator type Alloc_Typ. An error message is
4114 -- issued if this condition is violated. Specialized checks are done for
4115 -- the cases of a constraint expression which is an access attribute or
4116 -- an access discriminant.
4118 function In_Dispatching_Context return Boolean;
4119 -- If the allocator is an actual in a call, it is allowed to be class-
4120 -- wide when the context is not because it is a controlling actual.
4122 -------------------------------------------
4123 -- Check_Allocator_Discrim_Accessibility --
4124 -------------------------------------------
4126 procedure Check_Allocator_Discrim_Accessibility
4127 (Disc_Exp : Node_Id;
4128 Alloc_Typ : Entity_Id)
4131 if Type_Access_Level (Etype (Disc_Exp)) >
4132 Deepest_Type_Access_Level (Alloc_Typ)
4135 ("operand type has deeper level than allocator type", Disc_Exp);
4137 -- When the expression is an Access attribute the level of the prefix
4138 -- object must not be deeper than that of the allocator's type.
4140 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4141 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4143 and then Object_Access_Level (Prefix (Disc_Exp)) >
4144 Deepest_Type_Access_Level (Alloc_Typ)
4147 ("prefix of attribute has deeper level than allocator type",
4150 -- When the expression is an access discriminant the check is against
4151 -- the level of the prefix object.
4153 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4154 and then Nkind (Disc_Exp) = N_Selected_Component
4155 and then Object_Access_Level (Prefix (Disc_Exp)) >
4156 Deepest_Type_Access_Level (Alloc_Typ)
4159 ("access discriminant has deeper level than allocator type",
4162 -- All other cases are legal
4167 end Check_Allocator_Discrim_Accessibility;
4169 ----------------------------
4170 -- In_Dispatching_Context --
4171 ----------------------------
4173 function In_Dispatching_Context return Boolean is
4174 Par : constant Node_Id := Parent (N);
4178 Nkind_In (Par, N_Function_Call,
4179 N_Procedure_Call_Statement)
4180 and then Is_Entity_Name (Name (Par))
4181 and then Is_Dispatching_Operation (Entity (Name (Par)));
4182 end In_Dispatching_Context;
4184 -- Start of processing for Resolve_Allocator
4187 -- Replace general access with specific type
4189 if Ekind (Etype (N)) = E_Allocator_Type then
4190 Set_Etype (N, Base_Type (Typ));
4193 if Is_Abstract_Type (Typ) then
4194 Error_Msg_N ("type of allocator cannot be abstract", N);
4197 -- For qualified expression, resolve the expression using the
4198 -- given subtype (nothing to do for type mark, subtype indication)
4200 if Nkind (E) = N_Qualified_Expression then
4201 if Is_Class_Wide_Type (Etype (E))
4202 and then not Is_Class_Wide_Type (Desig_T)
4203 and then not In_Dispatching_Context
4206 ("class-wide allocator not allowed for this access type", N);
4209 Resolve (Expression (E), Etype (E));
4210 Check_Unset_Reference (Expression (E));
4212 -- A qualified expression requires an exact match of the type,
4213 -- class-wide matching is not allowed.
4215 if (Is_Class_Wide_Type (Etype (Expression (E)))
4216 or else Is_Class_Wide_Type (Etype (E)))
4217 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4219 Wrong_Type (Expression (E), Etype (E));
4222 -- A special accessibility check is needed for allocators that
4223 -- constrain access discriminants. The level of the type of the
4224 -- expression used to constrain an access discriminant cannot be
4225 -- deeper than the type of the allocator (in contrast to access
4226 -- parameters, where the level of the actual can be arbitrary).
4228 -- We can't use Valid_Conversion to perform this check because
4229 -- in general the type of the allocator is unrelated to the type
4230 -- of the access discriminant.
4232 if Ekind (Typ) /= E_Anonymous_Access_Type
4233 or else Is_Local_Anonymous_Access (Typ)
4235 Subtyp := Entity (Subtype_Mark (E));
4237 Aggr := Original_Node (Expression (E));
4239 if Has_Discriminants (Subtyp)
4240 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4242 Discrim := First_Discriminant (Base_Type (Subtyp));
4244 -- Get the first component expression of the aggregate
4246 if Present (Expressions (Aggr)) then
4247 Disc_Exp := First (Expressions (Aggr));
4249 elsif Present (Component_Associations (Aggr)) then
4250 Assoc := First (Component_Associations (Aggr));
4252 if Present (Assoc) then
4253 Disc_Exp := Expression (Assoc);
4262 while Present (Discrim) and then Present (Disc_Exp) loop
4263 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4264 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4267 Next_Discriminant (Discrim);
4269 if Present (Discrim) then
4270 if Present (Assoc) then
4272 Disc_Exp := Expression (Assoc);
4274 elsif Present (Next (Disc_Exp)) then
4278 Assoc := First (Component_Associations (Aggr));
4280 if Present (Assoc) then
4281 Disc_Exp := Expression (Assoc);
4291 -- For a subtype mark or subtype indication, freeze the subtype
4294 Freeze_Expression (E);
4296 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4298 ("initialization required for access-to-constant allocator", N);
4301 -- A special accessibility check is needed for allocators that
4302 -- constrain access discriminants. The level of the type of the
4303 -- expression used to constrain an access discriminant cannot be
4304 -- deeper than the type of the allocator (in contrast to access
4305 -- parameters, where the level of the actual can be arbitrary).
4306 -- We can't use Valid_Conversion to perform this check because
4307 -- in general the type of the allocator is unrelated to the type
4308 -- of the access discriminant.
4310 if Nkind (Original_Node (E)) = N_Subtype_Indication
4311 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4312 or else Is_Local_Anonymous_Access (Typ))
4314 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4316 if Has_Discriminants (Subtyp) then
4317 Discrim := First_Discriminant (Base_Type (Subtyp));
4318 Constr := First (Constraints (Constraint (Original_Node (E))));
4319 while Present (Discrim) and then Present (Constr) loop
4320 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4321 if Nkind (Constr) = N_Discriminant_Association then
4322 Disc_Exp := Original_Node (Expression (Constr));
4324 Disc_Exp := Original_Node (Constr);
4327 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4330 Next_Discriminant (Discrim);
4337 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4338 -- check that the level of the type of the created object is not deeper
4339 -- than the level of the allocator's access type, since extensions can
4340 -- now occur at deeper levels than their ancestor types. This is a
4341 -- static accessibility level check; a run-time check is also needed in
4342 -- the case of an initialized allocator with a class-wide argument (see
4343 -- Expand_Allocator_Expression).
4345 if Ada_Version >= Ada_2005
4346 and then Is_Class_Wide_Type (Desig_T)
4349 Exp_Typ : Entity_Id;
4352 if Nkind (E) = N_Qualified_Expression then
4353 Exp_Typ := Etype (E);
4354 elsif Nkind (E) = N_Subtype_Indication then
4355 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4357 Exp_Typ := Entity (E);
4360 if Type_Access_Level (Exp_Typ) >
4361 Deepest_Type_Access_Level (Typ)
4363 if In_Instance_Body then
4364 Error_Msg_N ("?type in allocator has deeper level than" &
4365 " designated class-wide type", E);
4366 Error_Msg_N ("\?Program_Error will be raised at run time",
4369 Make_Raise_Program_Error (Sloc (N),
4370 Reason => PE_Accessibility_Check_Failed));
4373 -- Do not apply Ada 2005 accessibility checks on a class-wide
4374 -- allocator if the type given in the allocator is a formal
4375 -- type. A run-time check will be performed in the instance.
4377 elsif not Is_Generic_Type (Exp_Typ) then
4378 Error_Msg_N ("type in allocator has deeper level than" &
4379 " designated class-wide type", E);
4385 -- Check for allocation from an empty storage pool
4387 if No_Pool_Assigned (Typ) then
4388 Error_Msg_N ("allocation from empty storage pool!", N);
4390 -- If the context is an unchecked conversion, as may happen within an
4391 -- inlined subprogram, the allocator is being resolved with its own
4392 -- anonymous type. In that case, if the target type has a specific
4393 -- storage pool, it must be inherited explicitly by the allocator type.
4395 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4396 and then No (Associated_Storage_Pool (Typ))
4398 Set_Associated_Storage_Pool
4399 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4402 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4403 Check_Restriction (No_Anonymous_Allocators, N);
4406 -- Check that an allocator with task parts isn't for a nested access
4407 -- type when restriction No_Task_Hierarchy applies.
4409 if not Is_Library_Level_Entity (Base_Type (Typ))
4410 and then Has_Task (Base_Type (Desig_T))
4412 Check_Restriction (No_Task_Hierarchy, N);
4415 -- An erroneous allocator may be rewritten as a raise Program_Error
4418 if Nkind (N) = N_Allocator then
4420 -- An anonymous access discriminant is the definition of a
4423 if Ekind (Typ) = E_Anonymous_Access_Type
4424 and then Nkind (Associated_Node_For_Itype (Typ)) =
4425 N_Discriminant_Specification
4428 Discr : constant Entity_Id :=
4429 Defining_Identifier (Associated_Node_For_Itype (Typ));
4432 -- Ada 2012 AI05-0052: If the designated type of the allocator
4433 -- is limited, then the allocator shall not be used to define
4434 -- the value of an access discriminant unless the discriminated
4435 -- type is immutably limited.
4437 if Ada_Version >= Ada_2012
4438 and then Is_Limited_Type (Desig_T)
4439 and then not Is_Immutably_Limited_Type (Scope (Discr))
4442 ("only immutably limited types can have anonymous "
4443 & "access discriminants designating a limited type", N);
4447 -- Avoid marking an allocator as a dynamic coextension if it is
4448 -- within a static construct.
4450 if not Is_Static_Coextension (N) then
4451 Set_Is_Dynamic_Coextension (N);
4454 -- Cleanup for potential static coextensions
4457 Set_Is_Dynamic_Coextension (N, False);
4458 Set_Is_Static_Coextension (N, False);
4462 -- Report a simple error: if the designated object is a local task,
4463 -- its body has not been seen yet, and its activation will fail an
4464 -- elaboration check.
4466 if Is_Task_Type (Desig_T)
4467 and then Scope (Base_Type (Desig_T)) = Current_Scope
4468 and then Is_Compilation_Unit (Current_Scope)
4469 and then Ekind (Current_Scope) = E_Package
4470 and then not In_Package_Body (Current_Scope)
4472 Error_Msg_N ("?cannot activate task before body seen", N);
4473 Error_Msg_N ("\?Program_Error will be raised at run time", N);
4476 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4477 -- type with a task component on a subpool. This action must raise
4478 -- Program_Error at runtime.
4480 if Ada_Version >= Ada_2012
4481 and then Nkind (N) = N_Allocator
4482 and then Present (Subpool_Handle_Name (N))
4483 and then Has_Task (Desig_T)
4485 Error_Msg_N ("?cannot allocate task on subpool", N);
4486 Error_Msg_N ("\?Program_Error will be raised at run time", N);
4489 Make_Raise_Program_Error (Sloc (N),
4490 Reason => PE_Explicit_Raise));
4493 end Resolve_Allocator;
4495 ---------------------------
4496 -- Resolve_Arithmetic_Op --
4497 ---------------------------
4499 -- Used for resolving all arithmetic operators except exponentiation
4501 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4502 L : constant Node_Id := Left_Opnd (N);
4503 R : constant Node_Id := Right_Opnd (N);
4504 TL : constant Entity_Id := Base_Type (Etype (L));
4505 TR : constant Entity_Id := Base_Type (Etype (R));
4509 B_Typ : constant Entity_Id := Base_Type (Typ);
4510 -- We do the resolution using the base type, because intermediate values
4511 -- in expressions always are of the base type, not a subtype of it.
4513 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4514 -- Returns True if N is in a context that expects "any real type"
4516 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4517 -- Return True iff given type is Integer or universal real/integer
4519 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4520 -- Choose type of integer literal in fixed-point operation to conform
4521 -- to available fixed-point type. T is the type of the other operand,
4522 -- which is needed to determine the expected type of N.
4524 procedure Set_Operand_Type (N : Node_Id);
4525 -- Set operand type to T if universal
4527 -------------------------------
4528 -- Expected_Type_Is_Any_Real --
4529 -------------------------------
4531 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4533 -- N is the expression after "delta" in a fixed_point_definition;
4536 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4537 N_Decimal_Fixed_Point_Definition,
4539 -- N is one of the bounds in a real_range_specification;
4542 N_Real_Range_Specification,
4544 -- N is the expression of a delta_constraint;
4547 N_Delta_Constraint);
4548 end Expected_Type_Is_Any_Real;
4550 -----------------------------
4551 -- Is_Integer_Or_Universal --
4552 -----------------------------
4554 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4556 Index : Interp_Index;
4560 if not Is_Overloaded (N) then
4562 return Base_Type (T) = Base_Type (Standard_Integer)
4563 or else T = Universal_Integer
4564 or else T = Universal_Real;
4566 Get_First_Interp (N, Index, It);
4567 while Present (It.Typ) loop
4568 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4569 or else It.Typ = Universal_Integer
4570 or else It.Typ = Universal_Real
4575 Get_Next_Interp (Index, It);
4580 end Is_Integer_Or_Universal;
4582 ----------------------------
4583 -- Set_Mixed_Mode_Operand --
4584 ----------------------------
4586 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4587 Index : Interp_Index;
4591 if Universal_Interpretation (N) = Universal_Integer then
4593 -- A universal integer literal is resolved as standard integer
4594 -- except in the case of a fixed-point result, where we leave it
4595 -- as universal (to be handled by Exp_Fixd later on)
4597 if Is_Fixed_Point_Type (T) then
4598 Resolve (N, Universal_Integer);
4600 Resolve (N, Standard_Integer);
4603 elsif Universal_Interpretation (N) = Universal_Real
4604 and then (T = Base_Type (Standard_Integer)
4605 or else T = Universal_Integer
4606 or else T = Universal_Real)
4608 -- A universal real can appear in a fixed-type context. We resolve
4609 -- the literal with that context, even though this might raise an
4610 -- exception prematurely (the other operand may be zero).
4614 elsif Etype (N) = Base_Type (Standard_Integer)
4615 and then T = Universal_Real
4616 and then Is_Overloaded (N)
4618 -- Integer arg in mixed-mode operation. Resolve with universal
4619 -- type, in case preference rule must be applied.
4621 Resolve (N, Universal_Integer);
4624 and then B_Typ /= Universal_Fixed
4626 -- Not a mixed-mode operation, resolve with context
4630 elsif Etype (N) = Any_Fixed then
4632 -- N may itself be a mixed-mode operation, so use context type
4636 elsif Is_Fixed_Point_Type (T)
4637 and then B_Typ = Universal_Fixed
4638 and then Is_Overloaded (N)
4640 -- Must be (fixed * fixed) operation, operand must have one
4641 -- compatible interpretation.
4643 Resolve (N, Any_Fixed);
4645 elsif Is_Fixed_Point_Type (B_Typ)
4646 and then (T = Universal_Real
4647 or else Is_Fixed_Point_Type (T))
4648 and then Is_Overloaded (N)
4650 -- C * F(X) in a fixed context, where C is a real literal or a
4651 -- fixed-point expression. F must have either a fixed type
4652 -- interpretation or an integer interpretation, but not both.
4654 Get_First_Interp (N, Index, It);
4655 while Present (It.Typ) loop
4656 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4657 if Analyzed (N) then
4658 Error_Msg_N ("ambiguous operand in fixed operation", N);
4660 Resolve (N, Standard_Integer);
4663 elsif Is_Fixed_Point_Type (It.Typ) then
4664 if Analyzed (N) then
4665 Error_Msg_N ("ambiguous operand in fixed operation", N);
4667 Resolve (N, It.Typ);
4671 Get_Next_Interp (Index, It);
4674 -- Reanalyze the literal with the fixed type of the context. If
4675 -- context is Universal_Fixed, we are within a conversion, leave
4676 -- the literal as a universal real because there is no usable
4677 -- fixed type, and the target of the conversion plays no role in
4691 if B_Typ = Universal_Fixed
4692 and then Nkind (Op2) = N_Real_Literal
4694 T2 := Universal_Real;
4699 Set_Analyzed (Op2, False);
4706 end Set_Mixed_Mode_Operand;
4708 ----------------------
4709 -- Set_Operand_Type --
4710 ----------------------
4712 procedure Set_Operand_Type (N : Node_Id) is
4714 if Etype (N) = Universal_Integer
4715 or else Etype (N) = Universal_Real
4719 end Set_Operand_Type;
4721 -- Start of processing for Resolve_Arithmetic_Op
4724 if Comes_From_Source (N)
4725 and then Ekind (Entity (N)) = E_Function
4726 and then Is_Imported (Entity (N))
4727 and then Is_Intrinsic_Subprogram (Entity (N))
4729 Resolve_Intrinsic_Operator (N, Typ);
4732 -- Special-case for mixed-mode universal expressions or fixed point type
4733 -- operation: each argument is resolved separately. The same treatment
4734 -- is required if one of the operands of a fixed point operation is
4735 -- universal real, since in this case we don't do a conversion to a
4736 -- specific fixed-point type (instead the expander handles the case).
4738 -- Set the type of the node to its universal interpretation because
4739 -- legality checks on an exponentiation operand need the context.
4741 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4742 and then Present (Universal_Interpretation (L))
4743 and then Present (Universal_Interpretation (R))
4745 Set_Etype (N, B_Typ);
4746 Resolve (L, Universal_Interpretation (L));
4747 Resolve (R, Universal_Interpretation (R));
4749 elsif (B_Typ = Universal_Real
4750 or else Etype (N) = Universal_Fixed
4751 or else (Etype (N) = Any_Fixed
4752 and then Is_Fixed_Point_Type (B_Typ))
4753 or else (Is_Fixed_Point_Type (B_Typ)
4754 and then (Is_Integer_Or_Universal (L)
4756 Is_Integer_Or_Universal (R))))
4757 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4759 if TL = Universal_Integer or else TR = Universal_Integer then
4760 Check_For_Visible_Operator (N, B_Typ);
4763 -- If context is a fixed type and one operand is integer, the other
4764 -- is resolved with the type of the context.
4766 if Is_Fixed_Point_Type (B_Typ)
4767 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4768 or else TL = Universal_Integer)
4773 elsif Is_Fixed_Point_Type (B_Typ)
4774 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4775 or else TR = Universal_Integer)
4781 Set_Mixed_Mode_Operand (L, TR);
4782 Set_Mixed_Mode_Operand (R, TL);
4785 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4786 -- multiplying operators from being used when the expected type is
4787 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4788 -- some cases where the expected type is actually Any_Real;
4789 -- Expected_Type_Is_Any_Real takes care of that case.
4791 if Etype (N) = Universal_Fixed
4792 or else Etype (N) = Any_Fixed
4794 if B_Typ = Universal_Fixed
4795 and then not Expected_Type_Is_Any_Real (N)
4796 and then not Nkind_In (Parent (N), N_Type_Conversion,
4797 N_Unchecked_Type_Conversion)
4799 Error_Msg_N ("type cannot be determined from context!", N);
4800 Error_Msg_N ("\explicit conversion to result type required", N);
4802 Set_Etype (L, Any_Type);
4803 Set_Etype (R, Any_Type);
4806 if Ada_Version = Ada_83
4807 and then Etype (N) = Universal_Fixed
4809 Nkind_In (Parent (N), N_Type_Conversion,
4810 N_Unchecked_Type_Conversion)
4813 ("(Ada 83) fixed-point operation "
4814 & "needs explicit conversion", N);
4817 -- The expected type is "any real type" in contexts like
4819 -- type T is delta <universal_fixed-expression> ...
4821 -- in which case we need to set the type to Universal_Real
4822 -- so that static expression evaluation will work properly.
4824 if Expected_Type_Is_Any_Real (N) then
4825 Set_Etype (N, Universal_Real);
4827 Set_Etype (N, B_Typ);
4831 elsif Is_Fixed_Point_Type (B_Typ)
4832 and then (Is_Integer_Or_Universal (L)
4833 or else Nkind (L) = N_Real_Literal
4834 or else Nkind (R) = N_Real_Literal
4835 or else Is_Integer_Or_Universal (R))
4837 Set_Etype (N, B_Typ);
4839 elsif Etype (N) = Any_Fixed then
4841 -- If no previous errors, this is only possible if one operand is
4842 -- overloaded and the context is universal. Resolve as such.
4844 Set_Etype (N, B_Typ);
4848 if (TL = Universal_Integer or else TL = Universal_Real)
4850 (TR = Universal_Integer or else TR = Universal_Real)
4852 Check_For_Visible_Operator (N, B_Typ);
4855 -- If the context is Universal_Fixed and the operands are also
4856 -- universal fixed, this is an error, unless there is only one
4857 -- applicable fixed_point type (usually Duration).
4859 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4860 T := Unique_Fixed_Point_Type (N);
4862 if T = Any_Type then
4875 -- If one of the arguments was resolved to a non-universal type.
4876 -- label the result of the operation itself with the same type.
4877 -- Do the same for the universal argument, if any.
4879 T := Intersect_Types (L, R);
4880 Set_Etype (N, Base_Type (T));
4881 Set_Operand_Type (L);
4882 Set_Operand_Type (R);
4885 Generate_Operator_Reference (N, Typ);
4886 Analyze_Dimension (N);
4887 Eval_Arithmetic_Op (N);
4889 -- In SPARK, a multiplication or division with operands of fixed point
4890 -- types shall be qualified or explicitly converted to identify the
4893 if (Is_Fixed_Point_Type (Etype (L))
4894 or else Is_Fixed_Point_Type (Etype (R)))
4895 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4897 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4899 Check_SPARK_Restriction
4900 ("operation should be qualified or explicitly converted", N);
4903 -- Set overflow and division checking bit. Much cleverer code needed
4904 -- here eventually and perhaps the Resolve routines should be separated
4905 -- for the various arithmetic operations, since they will need
4906 -- different processing. ???
4908 if Nkind (N) in N_Op then
4909 if not Overflow_Checks_Suppressed (Etype (N)) then
4910 Enable_Overflow_Check (N);
4913 -- Give warning if explicit division by zero
4915 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4916 and then not Division_Checks_Suppressed (Etype (N))
4918 Rop := Right_Opnd (N);
4920 if Compile_Time_Known_Value (Rop)
4921 and then ((Is_Integer_Type (Etype (Rop))
4922 and then Expr_Value (Rop) = Uint_0)
4924 (Is_Real_Type (Etype (Rop))
4925 and then Expr_Value_R (Rop) = Ureal_0))
4927 -- Specialize the warning message according to the operation.
4928 -- The following warnings are for the case
4933 -- For division, we have two cases, for float division
4934 -- of an unconstrained float type, on a machine where
4935 -- Machine_Overflows is false, we don't get an exception
4936 -- at run-time, but rather an infinity or Nan. The Nan
4937 -- case is pretty obscure, so just warn about infinities.
4939 if Is_Floating_Point_Type (Typ)
4940 and then not Is_Constrained (Typ)
4941 and then not Machine_Overflows_On_Target
4944 ("float division by zero, " &
4945 "may generate '+'/'- infinity?", Right_Opnd (N));
4947 -- For all other cases, we get a Constraint_Error
4950 Apply_Compile_Time_Constraint_Error
4951 (N, "division by zero?", CE_Divide_By_Zero,
4952 Loc => Sloc (Right_Opnd (N)));
4956 Apply_Compile_Time_Constraint_Error
4957 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4958 Loc => Sloc (Right_Opnd (N)));
4961 Apply_Compile_Time_Constraint_Error
4962 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4963 Loc => Sloc (Right_Opnd (N)));
4965 -- Division by zero can only happen with division, rem,
4966 -- and mod operations.
4969 raise Program_Error;
4972 -- Otherwise just set the flag to check at run time
4975 Activate_Division_Check (N);
4979 -- If Restriction No_Implicit_Conditionals is active, then it is
4980 -- violated if either operand can be negative for mod, or for rem
4981 -- if both operands can be negative.
4983 if Restriction_Check_Required (No_Implicit_Conditionals)
4984 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4993 -- Set if corresponding operand might be negative
4997 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4998 LNeg := (not OK) or else Lo < 0;
5001 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5002 RNeg := (not OK) or else Lo < 0;
5004 -- Check if we will be generating conditionals. There are two
5005 -- cases where that can happen, first for REM, the only case
5006 -- is largest negative integer mod -1, where the division can
5007 -- overflow, but we still have to give the right result. The
5008 -- front end generates a test for this annoying case. Here we
5009 -- just test if both operands can be negative (that's what the
5010 -- expander does, so we match its logic here).
5012 -- The second case is mod where either operand can be negative.
5013 -- In this case, the back end has to generate additional tests.
5015 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5017 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5019 Check_Restriction (No_Implicit_Conditionals, N);
5025 Check_Unset_Reference (L);
5026 Check_Unset_Reference (R);
5027 end Resolve_Arithmetic_Op;
5033 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5034 Loc : constant Source_Ptr := Sloc (N);
5035 Subp : constant Node_Id := Name (N);
5043 function Same_Or_Aliased_Subprograms
5045 E : Entity_Id) return Boolean;
5046 -- Returns True if the subprogram entity S is the same as E or else
5047 -- S is an alias of E.
5049 ---------------------------------
5050 -- Same_Or_Aliased_Subprograms --
5051 ---------------------------------
5053 function Same_Or_Aliased_Subprograms
5055 E : Entity_Id) return Boolean
5057 Subp_Alias : constant Entity_Id := Alias (S);
5060 or else (Present (Subp_Alias) and then Subp_Alias = E);
5061 end Same_Or_Aliased_Subprograms;
5063 -- Start of processing for Resolve_Call
5066 -- The context imposes a unique interpretation with type Typ on a
5067 -- procedure or function call. Find the entity of the subprogram that
5068 -- yields the expected type, and propagate the corresponding formal
5069 -- constraints on the actuals. The caller has established that an
5070 -- interpretation exists, and emitted an error if not unique.
5072 -- First deal with the case of a call to an access-to-subprogram,
5073 -- dereference made explicit in Analyze_Call.
5075 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5076 if not Is_Overloaded (Subp) then
5077 Nam := Etype (Subp);
5080 -- Find the interpretation whose type (a subprogram type) has a
5081 -- return type that is compatible with the context. Analysis of
5082 -- the node has established that one exists.
5086 Get_First_Interp (Subp, I, It);
5087 while Present (It.Typ) loop
5088 if Covers (Typ, Etype (It.Typ)) then
5093 Get_Next_Interp (I, It);
5097 raise Program_Error;
5101 -- If the prefix is not an entity, then resolve it
5103 if not Is_Entity_Name (Subp) then
5104 Resolve (Subp, Nam);
5107 -- For an indirect call, we always invalidate checks, since we do not
5108 -- know whether the subprogram is local or global. Yes we could do
5109 -- better here, e.g. by knowing that there are no local subprograms,
5110 -- but it does not seem worth the effort. Similarly, we kill all
5111 -- knowledge of current constant values.
5113 Kill_Current_Values;
5115 -- If this is a procedure call which is really an entry call, do
5116 -- the conversion of the procedure call to an entry call. Protected
5117 -- operations use the same circuitry because the name in the call
5118 -- can be an arbitrary expression with special resolution rules.
5120 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5121 or else (Is_Entity_Name (Subp)
5122 and then Ekind (Entity (Subp)) = E_Entry)
5124 Resolve_Entry_Call (N, Typ);
5125 Check_Elab_Call (N);
5127 -- Kill checks and constant values, as above for indirect case
5128 -- Who knows what happens when another task is activated?
5130 Kill_Current_Values;
5133 -- Normal subprogram call with name established in Resolve
5135 elsif not (Is_Type (Entity (Subp))) then
5136 Nam := Entity (Subp);
5137 Set_Entity_With_Style_Check (Subp, Nam);
5139 -- Otherwise we must have the case of an overloaded call
5142 pragma Assert (Is_Overloaded (Subp));
5144 -- Initialize Nam to prevent warning (we know it will be assigned
5145 -- in the loop below, but the compiler does not know that).
5149 Get_First_Interp (Subp, I, It);
5150 while Present (It.Typ) loop
5151 if Covers (Typ, It.Typ) then
5153 Set_Entity_With_Style_Check (Subp, Nam);
5157 Get_Next_Interp (I, It);
5161 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5162 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5163 and then Nkind (Subp) /= N_Explicit_Dereference
5164 and then Present (Parameter_Associations (N))
5166 -- The prefix is a parameterless function call that returns an access
5167 -- to subprogram. If parameters are present in the current call, add
5168 -- add an explicit dereference. We use the base type here because
5169 -- within an instance these may be subtypes.
5171 -- The dereference is added either in Analyze_Call or here. Should
5172 -- be consolidated ???
5174 Set_Is_Overloaded (Subp, False);
5175 Set_Etype (Subp, Etype (Nam));
5176 Insert_Explicit_Dereference (Subp);
5177 Nam := Designated_Type (Etype (Nam));
5178 Resolve (Subp, Nam);
5181 -- Check that a call to Current_Task does not occur in an entry body
5183 if Is_RTE (Nam, RE_Current_Task) then
5192 -- Exclude calls that occur within the default of a formal
5193 -- parameter of the entry, since those are evaluated outside
5196 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5198 if Nkind (P) = N_Entry_Body
5199 or else (Nkind (P) = N_Subprogram_Body
5200 and then Is_Entry_Barrier_Function (P))
5204 ("?& should not be used in entry body (RM C.7(17))",
5207 ("\Program_Error will be raised at run time?", N, Nam);
5209 Make_Raise_Program_Error (Loc,
5210 Reason => PE_Current_Task_In_Entry_Body));
5211 Set_Etype (N, Rtype);
5218 -- Check that a procedure call does not occur in the context of the
5219 -- entry call statement of a conditional or timed entry call. Note that
5220 -- the case of a call to a subprogram renaming of an entry will also be
5221 -- rejected. The test for N not being an N_Entry_Call_Statement is
5222 -- defensive, covering the possibility that the processing of entry
5223 -- calls might reach this point due to later modifications of the code
5226 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5227 and then Nkind (N) /= N_Entry_Call_Statement
5228 and then Entry_Call_Statement (Parent (N)) = N
5230 if Ada_Version < Ada_2005 then
5231 Error_Msg_N ("entry call required in select statement", N);
5233 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5234 -- for a procedure_or_entry_call, the procedure_name or
5235 -- procedure_prefix of the procedure_call_statement shall denote
5236 -- an entry renamed by a procedure, or (a view of) a primitive
5237 -- subprogram of a limited interface whose first parameter is
5238 -- a controlling parameter.
5240 elsif Nkind (N) = N_Procedure_Call_Statement
5241 and then not Is_Renamed_Entry (Nam)
5242 and then not Is_Controlling_Limited_Procedure (Nam)
5245 ("entry call or dispatching primitive of interface required", N);
5249 -- Check that this is not a call to a protected procedure or entry from
5250 -- within a protected function.
5252 if Ekind (Current_Scope) = E_Function
5253 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5254 and then Ekind (Nam) /= E_Function
5255 and then Scope (Nam) = Scope (Current_Scope)
5257 Error_Msg_N ("within protected function, protected " &
5258 "object is constant", N);
5259 Error_Msg_N ("\cannot call operation that may modify it", N);
5262 -- Freeze the subprogram name if not in a spec-expression. Note that we
5263 -- freeze procedure calls as well as function calls. Procedure calls are
5264 -- not frozen according to the rules (RM 13.14(14)) because it is
5265 -- impossible to have a procedure call to a non-frozen procedure in pure
5266 -- Ada, but in the code that we generate in the expander, this rule
5267 -- needs extending because we can generate procedure calls that need
5270 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5271 Freeze_Expression (Subp);
5274 -- For a predefined operator, the type of the result is the type imposed
5275 -- by context, except for a predefined operation on universal fixed.
5276 -- Otherwise The type of the call is the type returned by the subprogram
5279 if Is_Predefined_Op (Nam) then
5280 if Etype (N) /= Universal_Fixed then
5284 -- If the subprogram returns an array type, and the context requires the
5285 -- component type of that array type, the node is really an indexing of
5286 -- the parameterless call. Resolve as such. A pathological case occurs
5287 -- when the type of the component is an access to the array type. In
5288 -- this case the call is truly ambiguous.
5290 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5292 ((Is_Array_Type (Etype (Nam))
5293 and then Covers (Typ, Component_Type (Etype (Nam))))
5294 or else (Is_Access_Type (Etype (Nam))
5295 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5299 Component_Type (Designated_Type (Etype (Nam))))))
5302 Index_Node : Node_Id;
5304 Ret_Type : constant Entity_Id := Etype (Nam);
5307 if Is_Access_Type (Ret_Type)
5308 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5311 ("cannot disambiguate function call and indexing", N);
5313 New_Subp := Relocate_Node (Subp);
5314 Set_Entity (Subp, Nam);
5316 if (Is_Array_Type (Ret_Type)
5317 and then Component_Type (Ret_Type) /= Any_Type)
5319 (Is_Access_Type (Ret_Type)
5321 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5323 if Needs_No_Actuals (Nam) then
5325 -- Indexed call to a parameterless function
5328 Make_Indexed_Component (Loc,
5330 Make_Function_Call (Loc,
5332 Expressions => Parameter_Associations (N));
5334 -- An Ada 2005 prefixed call to a primitive operation
5335 -- whose first parameter is the prefix. This prefix was
5336 -- prepended to the parameter list, which is actually a
5337 -- list of indexes. Remove the prefix in order to build
5338 -- the proper indexed component.
5341 Make_Indexed_Component (Loc,
5343 Make_Function_Call (Loc,
5345 Parameter_Associations =>
5347 (Remove_Head (Parameter_Associations (N)))),
5348 Expressions => Parameter_Associations (N));
5351 -- Preserve the parenthesis count of the node
5353 Set_Paren_Count (Index_Node, Paren_Count (N));
5355 -- Since we are correcting a node classification error made
5356 -- by the parser, we call Replace rather than Rewrite.
5358 Replace (N, Index_Node);
5360 Set_Etype (Prefix (N), Ret_Type);
5362 Resolve_Indexed_Component (N, Typ);
5363 Check_Elab_Call (Prefix (N));
5371 Set_Etype (N, Etype (Nam));
5374 -- In the case where the call is to an overloaded subprogram, Analyze
5375 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5376 -- such a case Normalize_Actuals needs to be called once more to order
5377 -- the actuals correctly. Otherwise the call will have the ordering
5378 -- given by the last overloaded subprogram whether this is the correct
5379 -- one being called or not.
5381 if Is_Overloaded (Subp) then
5382 Normalize_Actuals (N, Nam, False, Norm_OK);
5383 pragma Assert (Norm_OK);
5386 -- In any case, call is fully resolved now. Reset Overload flag, to
5387 -- prevent subsequent overload resolution if node is analyzed again
5389 Set_Is_Overloaded (Subp, False);
5390 Set_Is_Overloaded (N, False);
5392 -- If we are calling the current subprogram from immediately within its
5393 -- body, then that is the case where we can sometimes detect cases of
5394 -- infinite recursion statically. Do not try this in case restriction
5395 -- No_Recursion is in effect anyway, and do it only for source calls.
5397 if Comes_From_Source (N) then
5398 Scop := Current_Scope;
5400 -- Issue warning for possible infinite recursion in the absence
5401 -- of the No_Recursion restriction.
5403 if Same_Or_Aliased_Subprograms (Nam, Scop)
5404 and then not Restriction_Active (No_Recursion)
5405 and then Check_Infinite_Recursion (N)
5407 -- Here we detected and flagged an infinite recursion, so we do
5408 -- not need to test the case below for further warnings. Also we
5409 -- are all done if we now have a raise SE node.
5411 if Nkind (N) = N_Raise_Storage_Error then
5415 -- If call is to immediately containing subprogram, then check for
5416 -- the case of a possible run-time detectable infinite recursion.
5419 Scope_Loop : while Scop /= Standard_Standard loop
5420 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5422 -- Although in general case, recursion is not statically
5423 -- checkable, the case of calling an immediately containing
5424 -- subprogram is easy to catch.
5426 Check_Restriction (No_Recursion, N);
5428 -- If the recursive call is to a parameterless subprogram,
5429 -- then even if we can't statically detect infinite
5430 -- recursion, this is pretty suspicious, and we output a
5431 -- warning. Furthermore, we will try later to detect some
5432 -- cases here at run time by expanding checking code (see
5433 -- Detect_Infinite_Recursion in package Exp_Ch6).
5435 -- If the recursive call is within a handler, do not emit a
5436 -- warning, because this is a common idiom: loop until input
5437 -- is correct, catch illegal input in handler and restart.
5439 if No (First_Formal (Nam))
5440 and then Etype (Nam) = Standard_Void_Type
5441 and then not Error_Posted (N)
5442 and then Nkind (Parent (N)) /= N_Exception_Handler
5444 -- For the case of a procedure call. We give the message
5445 -- only if the call is the first statement in a sequence
5446 -- of statements, or if all previous statements are
5447 -- simple assignments. This is simply a heuristic to
5448 -- decrease false positives, without losing too many good
5449 -- warnings. The idea is that these previous statements
5450 -- may affect global variables the procedure depends on.
5451 -- We also exclude raise statements, that may arise from
5452 -- constraint checks and are probably unrelated to the
5453 -- intended control flow.
5455 if Nkind (N) = N_Procedure_Call_Statement
5456 and then Is_List_Member (N)
5462 while Present (P) loop
5464 N_Assignment_Statement,
5465 N_Raise_Constraint_Error)
5475 -- Do not give warning if we are in a conditional context
5478 K : constant Node_Kind := Nkind (Parent (N));
5480 if (K = N_Loop_Statement
5481 and then Present (Iteration_Scheme (Parent (N))))
5482 or else K = N_If_Statement
5483 or else K = N_Elsif_Part
5484 or else K = N_Case_Statement_Alternative
5490 -- Here warning is to be issued
5492 Set_Has_Recursive_Call (Nam);
5494 ("?possible infinite recursion!", N);
5496 ("\?Storage_Error may be raised at run time!", N);
5502 Scop := Scope (Scop);
5503 end loop Scope_Loop;
5507 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5509 Check_Obsolescent_2005_Entity (Nam, Subp);
5511 -- If subprogram name is a predefined operator, it was given in
5512 -- functional notation. Replace call node with operator node, so
5513 -- that actuals can be resolved appropriately.
5515 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5516 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5519 elsif Present (Alias (Nam))
5520 and then Is_Predefined_Op (Alias (Nam))
5522 Resolve_Actuals (N, Nam);
5523 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5527 -- Create a transient scope if the resulting type requires it
5529 -- There are several notable exceptions:
5531 -- a) In init procs, the transient scope overhead is not needed, and is
5532 -- even incorrect when the call is a nested initialization call for a
5533 -- component whose expansion may generate adjust calls. However, if the
5534 -- call is some other procedure call within an initialization procedure
5535 -- (for example a call to Create_Task in the init_proc of the task
5536 -- run-time record) a transient scope must be created around this call.
5538 -- b) Enumeration literal pseudo-calls need no transient scope
5540 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5541 -- functions) do not use the secondary stack even though the return
5542 -- type may be unconstrained.
5544 -- d) Calls to a build-in-place function, since such functions may
5545 -- allocate their result directly in a target object, and cases where
5546 -- the result does get allocated in the secondary stack are checked for
5547 -- within the specialized Exp_Ch6 procedures for expanding those
5548 -- build-in-place calls.
5550 -- e) If the subprogram is marked Inline_Always, then even if it returns
5551 -- an unconstrained type the call does not require use of the secondary
5552 -- stack. However, inlining will only take place if the body to inline
5553 -- is already present. It may not be available if e.g. the subprogram is
5554 -- declared in a child instance.
5556 -- If this is an initialization call for a type whose construction
5557 -- uses the secondary stack, and it is not a nested call to initialize
5558 -- a component, we do need to create a transient scope for it. We
5559 -- check for this by traversing the type in Check_Initialization_Call.
5562 and then Has_Pragma_Inline_Always (Nam)
5563 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5564 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5568 elsif Ekind (Nam) = E_Enumeration_Literal
5569 or else Is_Build_In_Place_Function (Nam)
5570 or else Is_Intrinsic_Subprogram (Nam)
5574 elsif Full_Expander_Active
5575 and then Is_Type (Etype (Nam))
5576 and then Requires_Transient_Scope (Etype (Nam))
5578 (not Within_Init_Proc
5580 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5582 Establish_Transient_Scope (N, Sec_Stack => True);
5584 -- If the call appears within the bounds of a loop, it will
5585 -- be rewritten and reanalyzed, nothing left to do here.
5587 if Nkind (N) /= N_Function_Call then
5591 elsif Is_Init_Proc (Nam)
5592 and then not Within_Init_Proc
5594 Check_Initialization_Call (N, Nam);
5597 -- A protected function cannot be called within the definition of the
5598 -- enclosing protected type.
5600 if Is_Protected_Type (Scope (Nam))
5601 and then In_Open_Scopes (Scope (Nam))
5602 and then not Has_Completion (Scope (Nam))
5605 ("& cannot be called before end of protected definition", N, Nam);
5608 -- Propagate interpretation to actuals, and add default expressions
5611 if Present (First_Formal (Nam)) then
5612 Resolve_Actuals (N, Nam);
5614 -- Overloaded literals are rewritten as function calls, for purpose of
5615 -- resolution. After resolution, we can replace the call with the
5618 elsif Ekind (Nam) = E_Enumeration_Literal then
5619 Copy_Node (Subp, N);
5620 Resolve_Entity_Name (N, Typ);
5622 -- Avoid validation, since it is a static function call
5624 Generate_Reference (Nam, Subp);
5628 -- If the subprogram is not global, then kill all saved values and
5629 -- checks. This is a bit conservative, since in many cases we could do
5630 -- better, but it is not worth the effort. Similarly, we kill constant
5631 -- values. However we do not need to do this for internal entities
5632 -- (unless they are inherited user-defined subprograms), since they
5633 -- are not in the business of molesting local values.
5635 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5636 -- kill all checks and values for calls to global subprograms. This
5637 -- takes care of the case where an access to a local subprogram is
5638 -- taken, and could be passed directly or indirectly and then called
5639 -- from almost any context.
5641 -- Note: we do not do this step till after resolving the actuals. That
5642 -- way we still take advantage of the current value information while
5643 -- scanning the actuals.
5645 -- We suppress killing values if we are processing the nodes associated
5646 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5647 -- type kills all the values as part of analyzing the code that
5648 -- initializes the dispatch tables.
5650 if Inside_Freezing_Actions = 0
5651 and then (not Is_Library_Level_Entity (Nam)
5652 or else Suppress_Value_Tracking_On_Call
5653 (Nearest_Dynamic_Scope (Current_Scope)))
5654 and then (Comes_From_Source (Nam)
5655 or else (Present (Alias (Nam))
5656 and then Comes_From_Source (Alias (Nam))))
5658 Kill_Current_Values;
5661 -- If we are warning about unread OUT parameters, this is the place to
5662 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5663 -- after the above call to Kill_Current_Values (since that call clears
5664 -- the Last_Assignment field of all local variables).
5666 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5667 and then Comes_From_Source (N)
5668 and then In_Extended_Main_Source_Unit (N)
5675 F := First_Formal (Nam);
5676 A := First_Actual (N);
5677 while Present (F) and then Present (A) loop
5678 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5679 and then Warn_On_Modified_As_Out_Parameter (F)
5680 and then Is_Entity_Name (A)
5681 and then Present (Entity (A))
5682 and then Comes_From_Source (N)
5683 and then Safe_To_Capture_Value (N, Entity (A))
5685 Set_Last_Assignment (Entity (A), A);
5694 -- If the subprogram is a primitive operation, check whether or not
5695 -- it is a correct dispatching call.
5697 if Is_Overloadable (Nam)
5698 and then Is_Dispatching_Operation (Nam)
5700 Check_Dispatching_Call (N);
5702 elsif Ekind (Nam) /= E_Subprogram_Type
5703 and then Is_Abstract_Subprogram (Nam)
5704 and then not In_Instance
5706 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5709 -- If this is a dispatching call, generate the appropriate reference,
5710 -- for better source navigation in GPS.
5712 if Is_Overloadable (Nam)
5713 and then Present (Controlling_Argument (N))
5715 Generate_Reference (Nam, Subp, 'R');
5717 -- Normal case, not a dispatching call: generate a call reference
5720 Generate_Reference (Nam, Subp, 's');
5723 if Is_Intrinsic_Subprogram (Nam) then
5724 Check_Intrinsic_Call (N);
5727 -- Check for violation of restriction No_Specific_Termination_Handlers
5728 -- and warn on a potentially blocking call to Abort_Task.
5730 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5731 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5733 Is_RTE (Nam, RE_Specific_Handler))
5735 Check_Restriction (No_Specific_Termination_Handlers, N);
5737 elsif Is_RTE (Nam, RE_Abort_Task) then
5738 Check_Potentially_Blocking_Operation (N);
5741 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5742 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5743 -- need to check the second argument to determine whether it is an
5744 -- absolute or relative timing event.
5746 if Restriction_Check_Required (No_Relative_Delay)
5747 and then Is_RTE (Nam, RE_Set_Handler)
5748 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5750 Check_Restriction (No_Relative_Delay, N);
5753 -- Issue an error for a call to an eliminated subprogram. We skip this
5754 -- in a spec expression, e.g. a call in a default parameter value, since
5755 -- we are not really doing a call at this time. That's important because
5756 -- the spec expression may itself belong to an eliminated subprogram.
5758 if not In_Spec_Expression then
5759 Check_For_Eliminated_Subprogram (Subp, Nam);
5762 -- In formal mode, the primitive operations of a tagged type or type
5763 -- extension do not include functions that return the tagged type.
5765 -- Commented out as the call to Is_Inherited_Operation_For_Type may
5766 -- cause an error because the type entity of the parent node of
5767 -- Entity (Name (N) may not be set. ???
5768 -- So why not just add a guard ???
5770 -- if Nkind (N) = N_Function_Call
5771 -- and then Is_Tagged_Type (Etype (N))
5772 -- and then Is_Entity_Name (Name (N))
5773 -- and then Is_Inherited_Operation_For_Type
5774 -- (Entity (Name (N)), Etype (N))
5776 -- Check_SPARK_Restriction ("function not inherited", N);
5779 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5780 -- class-wide and the call dispatches on result in a context that does
5781 -- not provide a tag, the call raises Program_Error.
5783 if Nkind (N) = N_Function_Call
5784 and then In_Instance
5785 and then Is_Generic_Actual_Type (Typ)
5786 and then Is_Class_Wide_Type (Typ)
5787 and then Has_Controlling_Result (Nam)
5788 and then Nkind (Parent (N)) = N_Object_Declaration
5790 -- Verify that none of the formals are controlling
5793 Call_OK : Boolean := False;
5797 F := First_Formal (Nam);
5798 while Present (F) loop
5799 if Is_Controlling_Formal (F) then
5808 Error_Msg_N ("!? cannot determine tag of result", N);
5809 Error_Msg_N ("!? Program_Error will be raised", N);
5811 Make_Raise_Program_Error (Sloc (N),
5812 Reason => PE_Explicit_Raise));
5817 -- dimension analysis
5819 Analyze_Dimension (N);
5821 -- All done, evaluate call and deal with elaboration issues
5824 Check_Elab_Call (N);
5825 Warn_On_Overlapping_Actuals (Nam, N);
5828 -----------------------------
5829 -- Resolve_Case_Expression --
5830 -----------------------------
5832 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5836 Alt := First (Alternatives (N));
5837 while Present (Alt) loop
5838 Resolve (Expression (Alt), Typ);
5843 Eval_Case_Expression (N);
5844 end Resolve_Case_Expression;
5846 -------------------------------
5847 -- Resolve_Character_Literal --
5848 -------------------------------
5850 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5851 B_Typ : constant Entity_Id := Base_Type (Typ);
5855 -- Verify that the character does belong to the type of the context
5857 Set_Etype (N, B_Typ);
5858 Eval_Character_Literal (N);
5860 -- Wide_Wide_Character literals must always be defined, since the set
5861 -- of wide wide character literals is complete, i.e. if a character
5862 -- literal is accepted by the parser, then it is OK for wide wide
5863 -- character (out of range character literals are rejected).
5865 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5868 -- Always accept character literal for type Any_Character, which
5869 -- occurs in error situations and in comparisons of literals, both
5870 -- of which should accept all literals.
5872 elsif B_Typ = Any_Character then
5875 -- For Standard.Character or a type derived from it, check that the
5876 -- literal is in range.
5878 elsif Root_Type (B_Typ) = Standard_Character then
5879 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5883 -- For Standard.Wide_Character or a type derived from it, check that the
5884 -- literal is in range.
5886 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5887 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5891 -- For Standard.Wide_Wide_Character or a type derived from it, we
5892 -- know the literal is in range, since the parser checked!
5894 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5897 -- If the entity is already set, this has already been resolved in a
5898 -- generic context, or comes from expansion. Nothing else to do.
5900 elsif Present (Entity (N)) then
5903 -- Otherwise we have a user defined character type, and we can use the
5904 -- standard visibility mechanisms to locate the referenced entity.
5907 C := Current_Entity (N);
5908 while Present (C) loop
5909 if Etype (C) = B_Typ then
5910 Set_Entity_With_Style_Check (N, C);
5911 Generate_Reference (C, N);
5919 -- If we fall through, then the literal does not match any of the
5920 -- entries of the enumeration type. This isn't just a constraint error
5921 -- situation, it is an illegality (see RM 4.2).
5924 ("character not defined for }", N, First_Subtype (B_Typ));
5925 end Resolve_Character_Literal;
5927 ---------------------------
5928 -- Resolve_Comparison_Op --
5929 ---------------------------
5931 -- Context requires a boolean type, and plays no role in resolution.
5932 -- Processing identical to that for equality operators. The result type is
5933 -- the base type, which matters when pathological subtypes of booleans with
5934 -- limited ranges are used.
5936 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5937 L : constant Node_Id := Left_Opnd (N);
5938 R : constant Node_Id := Right_Opnd (N);
5942 -- If this is an intrinsic operation which is not predefined, use the
5943 -- types of its declared arguments to resolve the possibly overloaded
5944 -- operands. Otherwise the operands are unambiguous and specify the
5947 if Scope (Entity (N)) /= Standard_Standard then
5948 T := Etype (First_Entity (Entity (N)));
5951 T := Find_Unique_Type (L, R);
5953 if T = Any_Fixed then
5954 T := Unique_Fixed_Point_Type (L);
5958 Set_Etype (N, Base_Type (Typ));
5959 Generate_Reference (T, N, ' ');
5961 -- Skip remaining processing if already set to Any_Type
5963 if T = Any_Type then
5967 -- Deal with other error cases
5969 if T = Any_String or else
5970 T = Any_Composite or else
5973 if T = Any_Character then
5974 Ambiguous_Character (L);
5976 Error_Msg_N ("ambiguous operands for comparison", N);
5979 Set_Etype (N, Any_Type);
5983 -- Resolve the operands if types OK
5987 Check_Unset_Reference (L);
5988 Check_Unset_Reference (R);
5989 Generate_Operator_Reference (N, T);
5990 Check_Low_Bound_Tested (N);
5992 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
5993 -- types or array types except String.
5995 if Is_Boolean_Type (T) then
5996 Check_SPARK_Restriction
5997 ("comparison is not defined on Boolean type", N);
5999 elsif Is_Array_Type (T)
6000 and then Base_Type (T) /= Standard_String
6002 Check_SPARK_Restriction
6003 ("comparison is not defined on array types other than String", N);
6006 -- Check comparison on unordered enumeration
6008 if Comes_From_Source (N)
6009 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
6011 Error_Msg_N ("comparison on unordered enumeration type?", N);
6014 -- Evaluate the relation (note we do this after the above check since
6015 -- this Eval call may change N to True/False.
6017 Analyze_Dimension (N);
6018 Eval_Relational_Op (N);
6019 end Resolve_Comparison_Op;
6021 ------------------------------------
6022 -- Resolve_Conditional_Expression --
6023 ------------------------------------
6025 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
6026 Condition : constant Node_Id := First (Expressions (N));
6027 Then_Expr : constant Node_Id := Next (Condition);
6028 Else_Expr : Node_Id := Next (Then_Expr);
6031 Resolve (Condition, Any_Boolean);
6032 Resolve (Then_Expr, Typ);
6034 -- If ELSE expression present, just resolve using the determined type
6036 if Present (Else_Expr) then
6037 Resolve (Else_Expr, Typ);
6039 -- If no ELSE expression is present, root type must be Standard.Boolean
6040 -- and we provide a Standard.True result converted to the appropriate
6041 -- Boolean type (in case it is a derived boolean type).
6043 elsif Root_Type (Typ) = Standard_Boolean then
6045 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
6046 Analyze_And_Resolve (Else_Expr, Typ);
6047 Append_To (Expressions (N), Else_Expr);
6050 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
6051 Append_To (Expressions (N), Error);
6055 Eval_Conditional_Expression (N);
6056 end Resolve_Conditional_Expression;
6058 -----------------------------------------
6059 -- Resolve_Discrete_Subtype_Indication --
6060 -----------------------------------------
6062 procedure Resolve_Discrete_Subtype_Indication
6070 Analyze (Subtype_Mark (N));
6071 S := Entity (Subtype_Mark (N));
6073 if Nkind (Constraint (N)) /= N_Range_Constraint then
6074 Error_Msg_N ("expect range constraint for discrete type", N);
6075 Set_Etype (N, Any_Type);
6078 R := Range_Expression (Constraint (N));
6086 if Base_Type (S) /= Base_Type (Typ) then
6088 ("expect subtype of }", N, First_Subtype (Typ));
6090 -- Rewrite the constraint as a range of Typ
6091 -- to allow compilation to proceed further.
6094 Rewrite (Low_Bound (R),
6095 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6096 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6097 Attribute_Name => Name_First));
6098 Rewrite (High_Bound (R),
6099 Make_Attribute_Reference (Sloc (High_Bound (R)),
6100 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6101 Attribute_Name => Name_First));
6105 Set_Etype (N, Etype (R));
6107 -- Additionally, we must check that the bounds are compatible
6108 -- with the given subtype, which might be different from the
6109 -- type of the context.
6111 Apply_Range_Check (R, S);
6113 -- ??? If the above check statically detects a Constraint_Error
6114 -- it replaces the offending bound(s) of the range R with a
6115 -- Constraint_Error node. When the itype which uses these bounds
6116 -- is frozen the resulting call to Duplicate_Subexpr generates
6117 -- a new temporary for the bounds.
6119 -- Unfortunately there are other itypes that are also made depend
6120 -- on these bounds, so when Duplicate_Subexpr is called they get
6121 -- a forward reference to the newly created temporaries and Gigi
6122 -- aborts on such forward references. This is probably sign of a
6123 -- more fundamental problem somewhere else in either the order of
6124 -- itype freezing or the way certain itypes are constructed.
6126 -- To get around this problem we call Remove_Side_Effects right
6127 -- away if either bounds of R are a Constraint_Error.
6130 L : constant Node_Id := Low_Bound (R);
6131 H : constant Node_Id := High_Bound (R);
6134 if Nkind (L) = N_Raise_Constraint_Error then
6135 Remove_Side_Effects (L);
6138 if Nkind (H) = N_Raise_Constraint_Error then
6139 Remove_Side_Effects (H);
6143 Check_Unset_Reference (Low_Bound (R));
6144 Check_Unset_Reference (High_Bound (R));
6147 end Resolve_Discrete_Subtype_Indication;
6149 -------------------------
6150 -- Resolve_Entity_Name --
6151 -------------------------
6153 -- Used to resolve identifiers and expanded names
6155 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6156 E : constant Entity_Id := Entity (N);
6159 -- If garbage from errors, set to Any_Type and return
6161 if No (E) and then Total_Errors_Detected /= 0 then
6162 Set_Etype (N, Any_Type);
6166 -- Replace named numbers by corresponding literals. Note that this is
6167 -- the one case where Resolve_Entity_Name must reset the Etype, since
6168 -- it is currently marked as universal.
6170 if Ekind (E) = E_Named_Integer then
6172 Eval_Named_Integer (N);
6174 elsif Ekind (E) = E_Named_Real then
6176 Eval_Named_Real (N);
6178 -- For enumeration literals, we need to make sure that a proper style
6179 -- check is done, since such literals are overloaded, and thus we did
6180 -- not do a style check during the first phase of analysis.
6182 elsif Ekind (E) = E_Enumeration_Literal then
6183 Set_Entity_With_Style_Check (N, E);
6184 Eval_Entity_Name (N);
6186 -- Case of subtype name appearing as an operand in expression
6188 elsif Is_Type (E) then
6190 -- Allow use of subtype if it is a concurrent type where we are
6191 -- currently inside the body. This will eventually be expanded into a
6192 -- call to Self (for tasks) or _object (for protected objects). Any
6193 -- other use of a subtype is invalid.
6195 if Is_Concurrent_Type (E)
6196 and then In_Open_Scopes (E)
6200 -- Any other use is an error
6204 ("invalid use of subtype mark in expression or call", N);
6207 -- Check discriminant use if entity is discriminant in current scope,
6208 -- i.e. discriminant of record or concurrent type currently being
6209 -- analyzed. Uses in corresponding body are unrestricted.
6211 elsif Ekind (E) = E_Discriminant
6212 and then Scope (E) = Current_Scope
6213 and then not Has_Completion (Current_Scope)
6215 Check_Discriminant_Use (N);
6217 -- A parameterless generic function cannot appear in a context that
6218 -- requires resolution.
6220 elsif Ekind (E) = E_Generic_Function then
6221 Error_Msg_N ("illegal use of generic function", N);
6223 elsif Ekind (E) = E_Out_Parameter
6224 and then Ada_Version = Ada_83
6225 and then (Nkind (Parent (N)) in N_Op
6226 or else (Nkind (Parent (N)) = N_Assignment_Statement
6227 and then N = Expression (Parent (N)))
6228 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6230 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6232 -- In all other cases, just do the possible static evaluation
6235 -- A deferred constant that appears in an expression must have a
6236 -- completion, unless it has been removed by in-place expansion of
6239 if Ekind (E) = E_Constant
6240 and then Comes_From_Source (E)
6241 and then No (Constant_Value (E))
6242 and then Is_Frozen (Etype (E))
6243 and then not In_Spec_Expression
6244 and then not Is_Imported (E)
6246 if No_Initialization (Parent (E))
6247 or else (Present (Full_View (E))
6248 and then No_Initialization (Parent (Full_View (E))))
6253 "deferred constant is frozen before completion", N);
6257 Eval_Entity_Name (N);
6259 end Resolve_Entity_Name;
6265 procedure Resolve_Entry (Entry_Name : Node_Id) is
6266 Loc : constant Source_Ptr := Sloc (Entry_Name);
6274 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6275 -- If the bounds of the entry family being called depend on task
6276 -- discriminants, build a new index subtype where a discriminant is
6277 -- replaced with the value of the discriminant of the target task.
6278 -- The target task is the prefix of the entry name in the call.
6280 -----------------------
6281 -- Actual_Index_Type --
6282 -----------------------
6284 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6285 Typ : constant Entity_Id := Entry_Index_Type (E);
6286 Tsk : constant Entity_Id := Scope (E);
6287 Lo : constant Node_Id := Type_Low_Bound (Typ);
6288 Hi : constant Node_Id := Type_High_Bound (Typ);
6291 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6292 -- If the bound is given by a discriminant, replace with a reference
6293 -- to the discriminant of the same name in the target task. If the
6294 -- entry name is the target of a requeue statement and the entry is
6295 -- in the current protected object, the bound to be used is the
6296 -- discriminal of the object (see Apply_Range_Checks for details of
6297 -- the transformation).
6299 -----------------------------
6300 -- Actual_Discriminant_Ref --
6301 -----------------------------
6303 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6304 Typ : constant Entity_Id := Etype (Bound);
6308 Remove_Side_Effects (Bound);
6310 if not Is_Entity_Name (Bound)
6311 or else Ekind (Entity (Bound)) /= E_Discriminant
6315 elsif Is_Protected_Type (Tsk)
6316 and then In_Open_Scopes (Tsk)
6317 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6319 -- Note: here Bound denotes a discriminant of the corresponding
6320 -- record type tskV, whose discriminal is a formal of the
6321 -- init-proc tskVIP. What we want is the body discriminal,
6322 -- which is associated to the discriminant of the original
6323 -- concurrent type tsk.
6325 return New_Occurrence_Of
6326 (Find_Body_Discriminal (Entity (Bound)), Loc);
6330 Make_Selected_Component (Loc,
6331 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6332 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6337 end Actual_Discriminant_Ref;
6339 -- Start of processing for Actual_Index_Type
6342 if not Has_Discriminants (Tsk)
6343 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6345 return Entry_Index_Type (E);
6348 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6349 Set_Etype (New_T, Base_Type (Typ));
6350 Set_Size_Info (New_T, Typ);
6351 Set_RM_Size (New_T, RM_Size (Typ));
6352 Set_Scalar_Range (New_T,
6353 Make_Range (Sloc (Entry_Name),
6354 Low_Bound => Actual_Discriminant_Ref (Lo),
6355 High_Bound => Actual_Discriminant_Ref (Hi)));
6359 end Actual_Index_Type;
6361 -- Start of processing of Resolve_Entry
6364 -- Find name of entry being called, and resolve prefix of name with its
6365 -- own type. The prefix can be overloaded, and the name and signature of
6366 -- the entry must be taken into account.
6368 if Nkind (Entry_Name) = N_Indexed_Component then
6370 -- Case of dealing with entry family within the current tasks
6372 E_Name := Prefix (Entry_Name);
6375 E_Name := Entry_Name;
6378 if Is_Entity_Name (E_Name) then
6380 -- Entry call to an entry (or entry family) in the current task. This
6381 -- is legal even though the task will deadlock. Rewrite as call to
6384 -- This can also be a call to an entry in an enclosing task. If this
6385 -- is a single task, we have to retrieve its name, because the scope
6386 -- of the entry is the task type, not the object. If the enclosing
6387 -- task is a task type, the identity of the task is given by its own
6390 -- Finally this can be a requeue on an entry of the same task or
6391 -- protected object.
6393 S := Scope (Entity (E_Name));
6395 for J in reverse 0 .. Scope_Stack.Last loop
6396 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6397 and then not Comes_From_Source (S)
6399 -- S is an enclosing task or protected object. The concurrent
6400 -- declaration has been converted into a type declaration, and
6401 -- the object itself has an object declaration that follows
6402 -- the type in the same declarative part.
6404 Tsk := Next_Entity (S);
6405 while Etype (Tsk) /= S loop
6412 elsif S = Scope_Stack.Table (J).Entity then
6414 -- Call to current task. Will be transformed into call to Self
6422 Make_Selected_Component (Loc,
6423 Prefix => New_Occurrence_Of (S, Loc),
6425 New_Occurrence_Of (Entity (E_Name), Loc));
6426 Rewrite (E_Name, New_N);
6429 elsif Nkind (Entry_Name) = N_Selected_Component
6430 and then Is_Overloaded (Prefix (Entry_Name))
6432 -- Use the entry name (which must be unique at this point) to find
6433 -- the prefix that returns the corresponding task/protected type.
6436 Pref : constant Node_Id := Prefix (Entry_Name);
6437 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6442 Get_First_Interp (Pref, I, It);
6443 while Present (It.Typ) loop
6444 if Scope (Ent) = It.Typ then
6445 Set_Etype (Pref, It.Typ);
6449 Get_Next_Interp (I, It);
6454 if Nkind (Entry_Name) = N_Selected_Component then
6455 Resolve (Prefix (Entry_Name));
6457 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6458 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6459 Resolve (Prefix (Prefix (Entry_Name)));
6460 Index := First (Expressions (Entry_Name));
6461 Resolve (Index, Entry_Index_Type (Nam));
6463 -- Up to this point the expression could have been the actual in a
6464 -- simple entry call, and be given by a named association.
6466 if Nkind (Index) = N_Parameter_Association then
6467 Error_Msg_N ("expect expression for entry index", Index);
6469 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6474 ------------------------
6475 -- Resolve_Entry_Call --
6476 ------------------------
6478 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6479 Entry_Name : constant Node_Id := Name (N);
6480 Loc : constant Source_Ptr := Sloc (Entry_Name);
6482 First_Named : Node_Id;
6489 -- We kill all checks here, because it does not seem worth the effort to
6490 -- do anything better, an entry call is a big operation.
6494 -- Processing of the name is similar for entry calls and protected
6495 -- operation calls. Once the entity is determined, we can complete
6496 -- the resolution of the actuals.
6498 -- The selector may be overloaded, in the case of a protected object
6499 -- with overloaded functions. The type of the context is used for
6502 if Nkind (Entry_Name) = N_Selected_Component
6503 and then Is_Overloaded (Selector_Name (Entry_Name))
6504 and then Typ /= Standard_Void_Type
6511 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6512 while Present (It.Typ) loop
6513 if Covers (Typ, It.Typ) then
6514 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6515 Set_Etype (Entry_Name, It.Typ);
6517 Generate_Reference (It.Typ, N, ' ');
6520 Get_Next_Interp (I, It);
6525 Resolve_Entry (Entry_Name);
6527 if Nkind (Entry_Name) = N_Selected_Component then
6529 -- Simple entry call
6531 Nam := Entity (Selector_Name (Entry_Name));
6532 Obj := Prefix (Entry_Name);
6533 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6535 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6537 -- Call to member of entry family
6539 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6540 Obj := Prefix (Prefix (Entry_Name));
6541 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6544 -- We cannot in general check the maximum depth of protected entry calls
6545 -- at compile time. But we can tell that any protected entry call at all
6546 -- violates a specified nesting depth of zero.
6548 if Is_Protected_Type (Scope (Nam)) then
6549 Check_Restriction (Max_Entry_Queue_Length, N);
6552 -- Use context type to disambiguate a protected function that can be
6553 -- called without actuals and that returns an array type, and where the
6554 -- argument list may be an indexing of the returned value.
6556 if Ekind (Nam) = E_Function
6557 and then Needs_No_Actuals (Nam)
6558 and then Present (Parameter_Associations (N))
6560 ((Is_Array_Type (Etype (Nam))
6561 and then Covers (Typ, Component_Type (Etype (Nam))))
6563 or else (Is_Access_Type (Etype (Nam))
6564 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6568 Component_Type (Designated_Type (Etype (Nam))))))
6571 Index_Node : Node_Id;
6575 Make_Indexed_Component (Loc,
6577 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6578 Expressions => Parameter_Associations (N));
6580 -- Since we are correcting a node classification error made by the
6581 -- parser, we call Replace rather than Rewrite.
6583 Replace (N, Index_Node);
6584 Set_Etype (Prefix (N), Etype (Nam));
6586 Resolve_Indexed_Component (N, Typ);
6591 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6592 and then Present (PPC_Wrapper (Nam))
6593 and then Current_Scope /= PPC_Wrapper (Nam)
6595 -- Rewrite as call to the precondition wrapper, adding the task
6596 -- object to the list of actuals. If the call is to a member of an
6597 -- entry family, include the index as well.
6601 New_Actuals : List_Id;
6604 New_Actuals := New_List (Obj);
6606 if Nkind (Entry_Name) = N_Indexed_Component then
6607 Append_To (New_Actuals,
6608 New_Copy_Tree (First (Expressions (Entry_Name))));
6611 Append_List (Parameter_Associations (N), New_Actuals);
6613 Make_Procedure_Call_Statement (Loc,
6615 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6616 Parameter_Associations => New_Actuals);
6617 Rewrite (N, New_Call);
6618 Analyze_And_Resolve (N);
6623 -- The operation name may have been overloaded. Order the actuals
6624 -- according to the formals of the resolved entity, and set the return
6625 -- type to that of the operation.
6628 Normalize_Actuals (N, Nam, False, Norm_OK);
6629 pragma Assert (Norm_OK);
6630 Set_Etype (N, Etype (Nam));
6633 Resolve_Actuals (N, Nam);
6635 -- Create a call reference to the entry
6637 Generate_Reference (Nam, Entry_Name, 's');
6639 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6640 Check_Potentially_Blocking_Operation (N);
6643 -- Verify that a procedure call cannot masquerade as an entry
6644 -- call where an entry call is expected.
6646 if Ekind (Nam) = E_Procedure then
6647 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6648 and then N = Entry_Call_Statement (Parent (N))
6650 Error_Msg_N ("entry call required in select statement", N);
6652 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6653 and then N = Triggering_Statement (Parent (N))
6655 Error_Msg_N ("triggering statement cannot be procedure call", N);
6657 elsif Ekind (Scope (Nam)) = E_Task_Type
6658 and then not In_Open_Scopes (Scope (Nam))
6660 Error_Msg_N ("task has no entry with this name", Entry_Name);
6664 -- After resolution, entry calls and protected procedure calls are
6665 -- changed into entry calls, for expansion. The structure of the node
6666 -- does not change, so it can safely be done in place. Protected
6667 -- function calls must keep their structure because they are
6670 if Ekind (Nam) /= E_Function then
6672 -- A protected operation that is not a function may modify the
6673 -- corresponding object, and cannot apply to a constant. If this
6674 -- is an internal call, the prefix is the type itself.
6676 if Is_Protected_Type (Scope (Nam))
6677 and then not Is_Variable (Obj)
6678 and then (not Is_Entity_Name (Obj)
6679 or else not Is_Type (Entity (Obj)))
6682 ("prefix of protected procedure or entry call must be variable",
6686 Actuals := Parameter_Associations (N);
6687 First_Named := First_Named_Actual (N);
6690 Make_Entry_Call_Statement (Loc,
6692 Parameter_Associations => Actuals));
6694 Set_First_Named_Actual (N, First_Named);
6695 Set_Analyzed (N, True);
6697 -- Protected functions can return on the secondary stack, in which
6698 -- case we must trigger the transient scope mechanism.
6700 elsif Full_Expander_Active
6701 and then Requires_Transient_Scope (Etype (Nam))
6703 Establish_Transient_Scope (N, Sec_Stack => True);
6705 end Resolve_Entry_Call;
6707 -------------------------
6708 -- Resolve_Equality_Op --
6709 -------------------------
6711 -- Both arguments must have the same type, and the boolean context does
6712 -- not participate in the resolution. The first pass verifies that the
6713 -- interpretation is not ambiguous, and the type of the left argument is
6714 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6715 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6716 -- though they carry a single (universal) type. Diagnose this case here.
6718 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6719 L : constant Node_Id := Left_Opnd (N);
6720 R : constant Node_Id := Right_Opnd (N);
6721 T : Entity_Id := Find_Unique_Type (L, R);
6723 procedure Check_Conditional_Expression (Cond : Node_Id);
6724 -- The resolution rule for conditional expressions requires that each
6725 -- such must have a unique type. This means that if several dependent
6726 -- expressions are of a non-null anonymous access type, and the context
6727 -- does not impose an expected type (as can be the case in an equality
6728 -- operation) the expression must be rejected.
6730 function Find_Unique_Access_Type return Entity_Id;
6731 -- In the case of allocators, make a last-ditch attempt to find a single
6732 -- access type with the right designated type. This is semantically
6733 -- dubious, and of no interest to any real code, but c48008a makes it
6736 ----------------------------------
6737 -- Check_Conditional_Expression --
6738 ----------------------------------
6740 procedure Check_Conditional_Expression (Cond : Node_Id) is
6741 Then_Expr : Node_Id;
6742 Else_Expr : Node_Id;
6745 if Nkind (Cond) = N_Conditional_Expression then
6746 Then_Expr := Next (First (Expressions (Cond)));
6747 Else_Expr := Next (Then_Expr);
6749 if Nkind (Then_Expr) /= N_Null
6750 and then Nkind (Else_Expr) /= N_Null
6753 ("cannot determine type of conditional expression", Cond);
6756 end Check_Conditional_Expression;
6758 -----------------------------
6759 -- Find_Unique_Access_Type --
6760 -----------------------------
6762 function Find_Unique_Access_Type return Entity_Id is
6768 if Ekind (Etype (R)) = E_Allocator_Type then
6769 Acc := Designated_Type (Etype (R));
6770 elsif Ekind (Etype (L)) = E_Allocator_Type then
6771 Acc := Designated_Type (Etype (L));
6777 while S /= Standard_Standard loop
6778 E := First_Entity (S);
6779 while Present (E) loop
6781 and then Is_Access_Type (E)
6782 and then Ekind (E) /= E_Allocator_Type
6783 and then Designated_Type (E) = Base_Type (Acc)
6795 end Find_Unique_Access_Type;
6797 -- Start of processing for Resolve_Equality_Op
6800 Set_Etype (N, Base_Type (Typ));
6801 Generate_Reference (T, N, ' ');
6803 if T = Any_Fixed then
6804 T := Unique_Fixed_Point_Type (L);
6807 if T /= Any_Type then
6808 if T = Any_String or else
6809 T = Any_Composite or else
6812 if T = Any_Character then
6813 Ambiguous_Character (L);
6815 Error_Msg_N ("ambiguous operands for equality", N);
6818 Set_Etype (N, Any_Type);
6821 elsif T = Any_Access
6822 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6824 T := Find_Unique_Access_Type;
6827 Error_Msg_N ("ambiguous operands for equality", N);
6828 Set_Etype (N, Any_Type);
6832 -- Conditional expressions must have a single type, and if the
6833 -- context does not impose one the dependent expressions cannot
6834 -- be anonymous access types.
6836 elsif Ada_Version >= Ada_2012
6837 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6838 E_Anonymous_Access_Subprogram_Type)
6839 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6840 E_Anonymous_Access_Subprogram_Type)
6842 Check_Conditional_Expression (L);
6843 Check_Conditional_Expression (R);
6849 -- In SPARK, equality operators = and /= for array types other than
6850 -- String are only defined when, for each index position, the
6851 -- operands have equal static bounds.
6853 if Is_Array_Type (T) then
6854 -- Protect call to Matching_Static_Array_Bounds to avoid costly
6855 -- operation if not needed.
6857 if Restriction_Check_Required (SPARK)
6858 and then Base_Type (T) /= Standard_String
6859 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6860 and then Etype (L) /= Any_Composite -- or else L in error
6861 and then Etype (R) /= Any_Composite -- or else R in error
6862 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6864 Check_SPARK_Restriction
6865 ("array types should have matching static bounds", N);
6869 -- If the unique type is a class-wide type then it will be expanded
6870 -- into a dispatching call to the predefined primitive. Therefore we
6871 -- check here for potential violation of such restriction.
6873 if Is_Class_Wide_Type (T) then
6874 Check_Restriction (No_Dispatching_Calls, N);
6877 if Warn_On_Redundant_Constructs
6878 and then Comes_From_Source (N)
6879 and then Is_Entity_Name (R)
6880 and then Entity (R) = Standard_True
6881 and then Comes_From_Source (R)
6883 Error_Msg_N -- CODEFIX
6884 ("?comparison with True is redundant!", R);
6887 Check_Unset_Reference (L);
6888 Check_Unset_Reference (R);
6889 Generate_Operator_Reference (N, T);
6890 Check_Low_Bound_Tested (N);
6892 -- If this is an inequality, it may be the implicit inequality
6893 -- created for a user-defined operation, in which case the corres-
6894 -- ponding equality operation is not intrinsic, and the operation
6895 -- cannot be constant-folded. Else fold.
6897 if Nkind (N) = N_Op_Eq
6898 or else Comes_From_Source (Entity (N))
6899 or else Ekind (Entity (N)) = E_Operator
6900 or else Is_Intrinsic_Subprogram
6901 (Corresponding_Equality (Entity (N)))
6903 Analyze_Dimension (N);
6904 Eval_Relational_Op (N);
6906 elsif Nkind (N) = N_Op_Ne
6907 and then Is_Abstract_Subprogram (Entity (N))
6909 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6912 -- Ada 2005: If one operand is an anonymous access type, convert the
6913 -- other operand to it, to ensure that the underlying types match in
6914 -- the back-end. Same for access_to_subprogram, and the conversion
6915 -- verifies that the types are subtype conformant.
6917 -- We apply the same conversion in the case one of the operands is a
6918 -- private subtype of the type of the other.
6920 -- Why the Expander_Active test here ???
6922 if Full_Expander_Active
6924 (Ekind_In (T, E_Anonymous_Access_Type,
6925 E_Anonymous_Access_Subprogram_Type)
6926 or else Is_Private_Type (T))
6928 if Etype (L) /= T then
6930 Make_Unchecked_Type_Conversion (Sloc (L),
6931 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6932 Expression => Relocate_Node (L)));
6933 Analyze_And_Resolve (L, T);
6936 if (Etype (R)) /= T then
6938 Make_Unchecked_Type_Conversion (Sloc (R),
6939 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6940 Expression => Relocate_Node (R)));
6941 Analyze_And_Resolve (R, T);
6945 end Resolve_Equality_Op;
6947 ----------------------------------
6948 -- Resolve_Explicit_Dereference --
6949 ----------------------------------
6951 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6952 Loc : constant Source_Ptr := Sloc (N);
6954 P : constant Node_Id := Prefix (N);
6959 Check_Fully_Declared_Prefix (Typ, P);
6961 if Is_Overloaded (P) then
6963 -- Use the context type to select the prefix that has the correct
6966 Get_First_Interp (P, I, It);
6967 while Present (It.Typ) loop
6968 exit when Is_Access_Type (It.Typ)
6969 and then Covers (Typ, Designated_Type (It.Typ));
6970 Get_Next_Interp (I, It);
6973 if Present (It.Typ) then
6974 Resolve (P, It.Typ);
6976 -- If no interpretation covers the designated type of the prefix,
6977 -- this is the pathological case where not all implementations of
6978 -- the prefix allow the interpretation of the node as a call. Now
6979 -- that the expected type is known, Remove other interpretations
6980 -- from prefix, rewrite it as a call, and resolve again, so that
6981 -- the proper call node is generated.
6983 Get_First_Interp (P, I, It);
6984 while Present (It.Typ) loop
6985 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6989 Get_Next_Interp (I, It);
6993 Make_Function_Call (Loc,
6995 Make_Explicit_Dereference (Loc,
6997 Parameter_Associations => New_List);
6999 Save_Interps (N, New_N);
7001 Analyze_And_Resolve (N, Typ);
7005 Set_Etype (N, Designated_Type (It.Typ));
7011 if Is_Access_Type (Etype (P)) then
7012 Apply_Access_Check (N);
7015 -- If the designated type is a packed unconstrained array type, and the
7016 -- explicit dereference is not in the context of an attribute reference,
7017 -- then we must compute and set the actual subtype, since it is needed
7018 -- by Gigi. The reason we exclude the attribute case is that this is
7019 -- handled fine by Gigi, and in fact we use such attributes to build the
7020 -- actual subtype. We also exclude generated code (which builds actual
7021 -- subtypes directly if they are needed).
7023 if Is_Array_Type (Etype (N))
7024 and then Is_Packed (Etype (N))
7025 and then not Is_Constrained (Etype (N))
7026 and then Nkind (Parent (N)) /= N_Attribute_Reference
7027 and then Comes_From_Source (N)
7029 Set_Etype (N, Get_Actual_Subtype (N));
7032 -- Note: No Eval processing is required for an explicit dereference,
7033 -- because such a name can never be static.
7035 end Resolve_Explicit_Dereference;
7037 -------------------------------------
7038 -- Resolve_Expression_With_Actions --
7039 -------------------------------------
7041 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7044 end Resolve_Expression_With_Actions;
7046 -------------------------------
7047 -- Resolve_Indexed_Component --
7048 -------------------------------
7050 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7051 Name : constant Node_Id := Prefix (N);
7053 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7057 if Is_Overloaded (Name) then
7059 -- Use the context type to select the prefix that yields the correct
7065 I1 : Interp_Index := 0;
7066 P : constant Node_Id := Prefix (N);
7067 Found : Boolean := False;
7070 Get_First_Interp (P, I, It);
7071 while Present (It.Typ) loop
7072 if (Is_Array_Type (It.Typ)
7073 and then Covers (Typ, Component_Type (It.Typ)))
7074 or else (Is_Access_Type (It.Typ)
7075 and then Is_Array_Type (Designated_Type (It.Typ))
7079 Component_Type (Designated_Type (It.Typ))))
7082 It := Disambiguate (P, I1, I, Any_Type);
7084 if It = No_Interp then
7085 Error_Msg_N ("ambiguous prefix for indexing", N);
7091 Array_Type := It.Typ;
7097 Array_Type := It.Typ;
7102 Get_Next_Interp (I, It);
7107 Array_Type := Etype (Name);
7110 Resolve (Name, Array_Type);
7111 Array_Type := Get_Actual_Subtype_If_Available (Name);
7113 -- If prefix is access type, dereference to get real array type.
7114 -- Note: we do not apply an access check because the expander always
7115 -- introduces an explicit dereference, and the check will happen there.
7117 if Is_Access_Type (Array_Type) then
7118 Array_Type := Designated_Type (Array_Type);
7121 -- If name was overloaded, set component type correctly now
7122 -- If a misplaced call to an entry family (which has no index types)
7123 -- return. Error will be diagnosed from calling context.
7125 if Is_Array_Type (Array_Type) then
7126 Set_Etype (N, Component_Type (Array_Type));
7131 Index := First_Index (Array_Type);
7132 Expr := First (Expressions (N));
7134 -- The prefix may have resolved to a string literal, in which case its
7135 -- etype has a special representation. This is only possible currently
7136 -- if the prefix is a static concatenation, written in functional
7139 if Ekind (Array_Type) = E_String_Literal_Subtype then
7140 Resolve (Expr, Standard_Positive);
7143 while Present (Index) and Present (Expr) loop
7144 Resolve (Expr, Etype (Index));
7145 Check_Unset_Reference (Expr);
7147 if Is_Scalar_Type (Etype (Expr)) then
7148 Apply_Scalar_Range_Check (Expr, Etype (Index));
7150 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7158 Analyze_Dimension (N);
7160 -- Do not generate the warning on suspicious index if we are analyzing
7161 -- package Ada.Tags; otherwise we will report the warning with the
7162 -- Prims_Ptr field of the dispatch table.
7164 if Scope (Etype (Prefix (N))) = Standard_Standard
7166 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7169 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7170 Eval_Indexed_Component (N);
7173 -- If the array type is atomic, and is packed, and we are in a left side
7174 -- context, then this is worth a warning, since we have a situation
7175 -- where the access to the component may cause extra read/writes of
7176 -- the atomic array object, which could be considered unexpected.
7178 if Nkind (N) = N_Indexed_Component
7179 and then (Is_Atomic (Array_Type)
7180 or else (Is_Entity_Name (Prefix (N))
7181 and then Is_Atomic (Entity (Prefix (N)))))
7182 and then Is_Bit_Packed_Array (Array_Type)
7185 Error_Msg_N ("?assignment to component of packed atomic array",
7187 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7190 end Resolve_Indexed_Component;
7192 -----------------------------
7193 -- Resolve_Integer_Literal --
7194 -----------------------------
7196 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7199 Eval_Integer_Literal (N);
7200 end Resolve_Integer_Literal;
7202 --------------------------------
7203 -- Resolve_Intrinsic_Operator --
7204 --------------------------------
7206 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7207 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7209 Orig_Op : constant Entity_Id := Entity (N);
7213 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7214 -- If the operand is a literal, it cannot be the expression in a
7215 -- conversion. Use a qualified expression instead.
7217 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7218 Loc : constant Source_Ptr := Sloc (Opnd);
7221 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7223 Make_Qualified_Expression (Loc,
7224 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7225 Expression => Relocate_Node (Opnd));
7229 Res := Unchecked_Convert_To (Btyp, Opnd);
7233 end Convert_Operand;
7235 -- Start of processing for Resolve_Intrinsic_Operator
7238 -- We must preserve the original entity in a generic setting, so that
7239 -- the legality of the operation can be verified in an instance.
7241 if not Full_Expander_Active then
7246 while Scope (Op) /= Standard_Standard loop
7248 pragma Assert (Present (Op));
7252 Set_Is_Overloaded (N, False);
7254 -- If the result or operand types are private, rewrite with unchecked
7255 -- conversions on the operands and the result, to expose the proper
7256 -- underlying numeric type.
7258 if Is_Private_Type (Typ)
7259 or else Is_Private_Type (Etype (Left_Opnd (N)))
7260 or else Is_Private_Type (Etype (Right_Opnd (N)))
7262 Arg1 := Convert_Operand (Left_Opnd (N));
7263 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7264 -- What on earth is this commented out fragment of code???
7266 if Nkind (N) = N_Op_Expon then
7267 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7269 Arg2 := Convert_Operand (Right_Opnd (N));
7272 if Nkind (Arg1) = N_Type_Conversion then
7273 Save_Interps (Left_Opnd (N), Expression (Arg1));
7276 if Nkind (Arg2) = N_Type_Conversion then
7277 Save_Interps (Right_Opnd (N), Expression (Arg2));
7280 Set_Left_Opnd (N, Arg1);
7281 Set_Right_Opnd (N, Arg2);
7283 Set_Etype (N, Btyp);
7284 Rewrite (N, Unchecked_Convert_To (Typ, N));
7287 elsif Typ /= Etype (Left_Opnd (N))
7288 or else Typ /= Etype (Right_Opnd (N))
7290 -- Add explicit conversion where needed, and save interpretations in
7291 -- case operands are overloaded. If the context is a VMS operation,
7292 -- assert that the conversion is legal (the operands have the proper
7293 -- types to select the VMS intrinsic). Note that in rare cases the
7294 -- VMS operators may be visible, but the default System is being used
7295 -- and Address is a private type.
7297 Arg1 := Convert_To (Typ, Left_Opnd (N));
7298 Arg2 := Convert_To (Typ, Right_Opnd (N));
7300 if Nkind (Arg1) = N_Type_Conversion then
7301 Save_Interps (Left_Opnd (N), Expression (Arg1));
7303 if Is_VMS_Operator (Orig_Op) then
7304 Set_Conversion_OK (Arg1);
7307 Save_Interps (Left_Opnd (N), Arg1);
7310 if Nkind (Arg2) = N_Type_Conversion then
7311 Save_Interps (Right_Opnd (N), Expression (Arg2));
7313 if Is_VMS_Operator (Orig_Op) then
7314 Set_Conversion_OK (Arg2);
7317 Save_Interps (Right_Opnd (N), Arg2);
7320 Rewrite (Left_Opnd (N), Arg1);
7321 Rewrite (Right_Opnd (N), Arg2);
7324 Resolve_Arithmetic_Op (N, Typ);
7327 Resolve_Arithmetic_Op (N, Typ);
7329 end Resolve_Intrinsic_Operator;
7331 --------------------------------------
7332 -- Resolve_Intrinsic_Unary_Operator --
7333 --------------------------------------
7335 procedure Resolve_Intrinsic_Unary_Operator
7339 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7345 while Scope (Op) /= Standard_Standard loop
7347 pragma Assert (Present (Op));
7352 if Is_Private_Type (Typ) then
7353 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7354 Save_Interps (Right_Opnd (N), Expression (Arg2));
7356 Set_Right_Opnd (N, Arg2);
7358 Set_Etype (N, Btyp);
7359 Rewrite (N, Unchecked_Convert_To (Typ, N));
7363 Resolve_Unary_Op (N, Typ);
7365 end Resolve_Intrinsic_Unary_Operator;
7367 ------------------------
7368 -- Resolve_Logical_Op --
7369 ------------------------
7371 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7375 Check_No_Direct_Boolean_Operators (N);
7377 -- Predefined operations on scalar types yield the base type. On the
7378 -- other hand, logical operations on arrays yield the type of the
7379 -- arguments (and the context).
7381 if Is_Array_Type (Typ) then
7384 B_Typ := Base_Type (Typ);
7387 -- OK if this is a VMS-specific intrinsic operation
7389 if Is_VMS_Operator (Entity (N)) then
7392 -- The following test is required because the operands of the operation
7393 -- may be literals, in which case the resulting type appears to be
7394 -- compatible with a signed integer type, when in fact it is compatible
7395 -- only with modular types. If the context itself is universal, the
7396 -- operation is illegal.
7398 elsif not Valid_Boolean_Arg (Typ) then
7399 Error_Msg_N ("invalid context for logical operation", N);
7400 Set_Etype (N, Any_Type);
7403 elsif Typ = Any_Modular then
7405 ("no modular type available in this context", N);
7406 Set_Etype (N, Any_Type);
7409 elsif Is_Modular_Integer_Type (Typ)
7410 and then Etype (Left_Opnd (N)) = Universal_Integer
7411 and then Etype (Right_Opnd (N)) = Universal_Integer
7413 Check_For_Visible_Operator (N, B_Typ);
7416 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
7417 -- is active and the result type is standard Boolean (do not mess with
7418 -- ops that return a nonstandard Boolean type, because something strange
7421 -- Note: you might expect this replacement to be done during expansion,
7422 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
7423 -- is used, no part of the right operand of an "and" or "or" operator
7424 -- should be executed if the left operand would short-circuit the
7425 -- evaluation of the corresponding "and then" or "or else". If we left
7426 -- the replacement to expansion time, then run-time checks associated
7427 -- with such operands would be evaluated unconditionally, due to being
7428 -- before the condition prior to the rewriting as short-circuit forms
7429 -- during expansion.
7431 if Short_Circuit_And_Or
7432 and then B_Typ = Standard_Boolean
7433 and then Nkind_In (N, N_Op_And, N_Op_Or)
7435 if Nkind (N) = N_Op_And then
7437 Make_And_Then (Sloc (N),
7438 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7439 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7440 Analyze_And_Resolve (N, B_Typ);
7442 -- Case of OR changed to OR ELSE
7446 Make_Or_Else (Sloc (N),
7447 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7448 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7449 Analyze_And_Resolve (N, B_Typ);
7452 -- Return now, since analysis of the rewritten ops will take care of
7453 -- other reference bookkeeping and expression folding.
7458 Resolve (Left_Opnd (N), B_Typ);
7459 Resolve (Right_Opnd (N), B_Typ);
7461 Check_Unset_Reference (Left_Opnd (N));
7462 Check_Unset_Reference (Right_Opnd (N));
7464 Set_Etype (N, B_Typ);
7465 Generate_Operator_Reference (N, B_Typ);
7466 Eval_Logical_Op (N);
7468 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7469 -- only when both operands have same static lower and higher bounds. Of
7470 -- course the types have to match, so only check if operands are
7471 -- compatible and the node itself has no errors.
7473 if Is_Array_Type (B_Typ)
7474 and then Nkind (N) in N_Binary_Op
7477 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7478 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7481 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7482 -- operation if not needed.
7484 if Restriction_Check_Required (SPARK)
7485 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7486 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7487 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7488 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7490 Check_SPARK_Restriction
7491 ("array types should have matching static bounds", N);
7495 end Resolve_Logical_Op;
7497 ---------------------------
7498 -- Resolve_Membership_Op --
7499 ---------------------------
7501 -- The context can only be a boolean type, and does not determine the
7502 -- arguments. Arguments should be unambiguous, but the preference rule for
7503 -- universal types applies.
7505 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7506 pragma Warnings (Off, Typ);
7508 L : constant Node_Id := Left_Opnd (N);
7509 R : constant Node_Id := Right_Opnd (N);
7512 procedure Resolve_Set_Membership;
7513 -- Analysis has determined a unique type for the left operand. Use it to
7514 -- resolve the disjuncts.
7516 ----------------------------
7517 -- Resolve_Set_Membership --
7518 ----------------------------
7520 procedure Resolve_Set_Membership is
7524 Resolve (L, Etype (L));
7526 Alt := First (Alternatives (N));
7527 while Present (Alt) loop
7529 -- Alternative is an expression, a range
7530 -- or a subtype mark.
7532 if not Is_Entity_Name (Alt)
7533 or else not Is_Type (Entity (Alt))
7535 Resolve (Alt, Etype (L));
7540 end Resolve_Set_Membership;
7542 -- Start of processing for Resolve_Membership_Op
7545 if L = Error or else R = Error then
7549 if Present (Alternatives (N)) then
7550 Resolve_Set_Membership;
7553 elsif not Is_Overloaded (R)
7555 (Etype (R) = Universal_Integer
7557 Etype (R) = Universal_Real)
7558 and then Is_Overloaded (L)
7562 -- Ada 2005 (AI-251): Support the following case:
7564 -- type I is interface;
7565 -- type T is tagged ...
7567 -- function Test (O : I'Class) is
7569 -- return O in T'Class.
7572 -- In this case we have nothing else to do. The membership test will be
7573 -- done at run time.
7575 elsif Ada_Version >= Ada_2005
7576 and then Is_Class_Wide_Type (Etype (L))
7577 and then Is_Interface (Etype (L))
7578 and then Is_Class_Wide_Type (Etype (R))
7579 and then not Is_Interface (Etype (R))
7583 T := Intersect_Types (L, R);
7586 -- If mixed-mode operations are present and operands are all literal,
7587 -- the only interpretation involves Duration, which is probably not
7588 -- the intention of the programmer.
7590 if T = Any_Fixed then
7591 T := Unique_Fixed_Point_Type (N);
7593 if T = Any_Type then
7599 Check_Unset_Reference (L);
7601 if Nkind (R) = N_Range
7602 and then not Is_Scalar_Type (T)
7604 Error_Msg_N ("scalar type required for range", R);
7607 if Is_Entity_Name (R) then
7608 Freeze_Expression (R);
7611 Check_Unset_Reference (R);
7614 Eval_Membership_Op (N);
7615 end Resolve_Membership_Op;
7621 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7622 Loc : constant Source_Ptr := Sloc (N);
7625 -- Handle restriction against anonymous null access values This
7626 -- restriction can be turned off using -gnatdj.
7628 -- Ada 2005 (AI-231): Remove restriction
7630 if Ada_Version < Ada_2005
7631 and then not Debug_Flag_J
7632 and then Ekind (Typ) = E_Anonymous_Access_Type
7633 and then Comes_From_Source (N)
7635 -- In the common case of a call which uses an explicitly null value
7636 -- for an access parameter, give specialized error message.
7638 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7642 ("null is not allowed as argument for an access parameter", N);
7644 -- Standard message for all other cases (are there any?)
7648 ("null cannot be of an anonymous access type", N);
7652 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7653 -- assignment to a null-excluding object
7655 if Ada_Version >= Ada_2005
7656 and then Can_Never_Be_Null (Typ)
7657 and then Nkind (Parent (N)) = N_Assignment_Statement
7659 if not Inside_Init_Proc then
7661 (Compile_Time_Constraint_Error (N,
7662 "(Ada 2005) null not allowed in null-excluding objects?"),
7663 Make_Raise_Constraint_Error (Loc,
7664 Reason => CE_Access_Check_Failed));
7667 Make_Raise_Constraint_Error (Loc,
7668 Reason => CE_Access_Check_Failed));
7672 -- In a distributed context, null for a remote access to subprogram may
7673 -- need to be replaced with a special record aggregate. In this case,
7674 -- return after having done the transformation.
7676 if (Ekind (Typ) = E_Record_Type
7677 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7678 and then Remote_AST_Null_Value (N, Typ)
7683 -- The null literal takes its type from the context
7688 -----------------------
7689 -- Resolve_Op_Concat --
7690 -----------------------
7692 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7694 -- We wish to avoid deep recursion, because concatenations are often
7695 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7696 -- operands nonrecursively until we find something that is not a simple
7697 -- concatenation (A in this case). We resolve that, and then walk back
7698 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7699 -- to do the rest of the work at each level. The Parent pointers allow
7700 -- us to avoid recursion, and thus avoid running out of memory. See also
7701 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7707 -- The following code is equivalent to:
7709 -- Resolve_Op_Concat_First (NN, Typ);
7710 -- Resolve_Op_Concat_Arg (N, ...);
7711 -- Resolve_Op_Concat_Rest (N, Typ);
7713 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7714 -- operand is a concatenation.
7716 -- Walk down left operands
7719 Resolve_Op_Concat_First (NN, Typ);
7720 Op1 := Left_Opnd (NN);
7721 exit when not (Nkind (Op1) = N_Op_Concat
7722 and then not Is_Array_Type (Component_Type (Typ))
7723 and then Entity (Op1) = Entity (NN));
7727 -- Now (given the above example) NN is A&B and Op1 is A
7729 -- First resolve Op1 ...
7731 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7733 -- ... then walk NN back up until we reach N (where we started), calling
7734 -- Resolve_Op_Concat_Rest along the way.
7737 Resolve_Op_Concat_Rest (NN, Typ);
7742 if Base_Type (Etype (N)) /= Standard_String then
7743 Check_SPARK_Restriction
7744 ("result of concatenation should have type String", N);
7746 end Resolve_Op_Concat;
7748 ---------------------------
7749 -- Resolve_Op_Concat_Arg --
7750 ---------------------------
7752 procedure Resolve_Op_Concat_Arg
7758 Btyp : constant Entity_Id := Base_Type (Typ);
7759 Ctyp : constant Entity_Id := Component_Type (Typ);
7764 or else (not Is_Overloaded (Arg)
7765 and then Etype (Arg) /= Any_Composite
7766 and then Covers (Ctyp, Etype (Arg)))
7768 Resolve (Arg, Ctyp);
7770 Resolve (Arg, Btyp);
7773 -- If both Array & Array and Array & Component are visible, there is a
7774 -- potential ambiguity that must be reported.
7776 elsif Has_Compatible_Type (Arg, Ctyp) then
7777 if Nkind (Arg) = N_Aggregate
7778 and then Is_Composite_Type (Ctyp)
7780 if Is_Private_Type (Ctyp) then
7781 Resolve (Arg, Btyp);
7783 -- If the operation is user-defined and not overloaded use its
7784 -- profile. The operation may be a renaming, in which case it has
7785 -- been rewritten, and we want the original profile.
7787 elsif not Is_Overloaded (N)
7788 and then Comes_From_Source (Entity (Original_Node (N)))
7789 and then Ekind (Entity (Original_Node (N))) = E_Function
7793 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
7796 -- Otherwise an aggregate may match both the array type and the
7800 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7801 Set_Etype (Arg, Any_Type);
7805 if Is_Overloaded (Arg)
7806 and then Has_Compatible_Type (Arg, Typ)
7807 and then Etype (Arg) /= Any_Type
7815 Get_First_Interp (Arg, I, It);
7817 Get_Next_Interp (I, It);
7819 -- Special-case the error message when the overloading is
7820 -- caused by a function that yields an array and can be
7821 -- called without parameters.
7823 if It.Nam = Func then
7824 Error_Msg_Sloc := Sloc (Func);
7825 Error_Msg_N ("ambiguous call to function#", Arg);
7827 ("\\interpretation as call yields&", Arg, Typ);
7829 ("\\interpretation as indexing of call yields&",
7830 Arg, Component_Type (Typ));
7833 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
7835 Get_First_Interp (Arg, I, It);
7836 while Present (It.Nam) loop
7837 Error_Msg_Sloc := Sloc (It.Nam);
7839 if Base_Type (It.Typ) = Btyp
7841 Base_Type (It.Typ) = Base_Type (Ctyp)
7843 Error_Msg_N -- CODEFIX
7844 ("\\possible interpretation#", Arg);
7847 Get_Next_Interp (I, It);
7853 Resolve (Arg, Component_Type (Typ));
7855 if Nkind (Arg) = N_String_Literal then
7856 Set_Etype (Arg, Component_Type (Typ));
7859 if Arg = Left_Opnd (N) then
7860 Set_Is_Component_Left_Opnd (N);
7862 Set_Is_Component_Right_Opnd (N);
7867 Resolve (Arg, Btyp);
7870 -- Concatenation is restricted in SPARK: each operand must be either a
7871 -- string literal, the name of a string constant, a static character or
7872 -- string expression, or another concatenation. Arg cannot be a
7873 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
7874 -- separately on each final operand, past concatenation operations.
7876 if Is_Character_Type (Etype (Arg)) then
7877 if not Is_Static_Expression (Arg) then
7878 Check_SPARK_Restriction
7879 ("character operand for concatenation should be static", Arg);
7882 elsif Is_String_Type (Etype (Arg)) then
7883 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
7884 and then Is_Constant_Object (Entity (Arg)))
7885 and then not Is_Static_Expression (Arg)
7887 Check_SPARK_Restriction
7888 ("string operand for concatenation should be static", Arg);
7891 -- Do not issue error on an operand that is neither a character nor a
7892 -- string, as the error is issued in Resolve_Op_Concat.
7898 Check_Unset_Reference (Arg);
7899 end Resolve_Op_Concat_Arg;
7901 -----------------------------
7902 -- Resolve_Op_Concat_First --
7903 -----------------------------
7905 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7906 Btyp : constant Entity_Id := Base_Type (Typ);
7907 Op1 : constant Node_Id := Left_Opnd (N);
7908 Op2 : constant Node_Id := Right_Opnd (N);
7911 -- The parser folds an enormous sequence of concatenations of string
7912 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7913 -- in the right operand. If the expression resolves to a predefined "&"
7914 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7915 -- we give an error. See P_Simple_Expression in Par.Ch4.
7917 if Nkind (Op2) = N_String_Literal
7918 and then Is_Folded_In_Parser (Op2)
7919 and then Ekind (Entity (N)) = E_Function
7921 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7922 and then String_Length (Strval (Op1)) = 0);
7923 Error_Msg_N ("too many user-defined concatenations", N);
7927 Set_Etype (N, Btyp);
7929 if Is_Limited_Composite (Btyp) then
7930 Error_Msg_N ("concatenation not available for limited array", N);
7931 Explain_Limited_Type (Btyp, N);
7933 end Resolve_Op_Concat_First;
7935 ----------------------------
7936 -- Resolve_Op_Concat_Rest --
7937 ----------------------------
7939 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7940 Op1 : constant Node_Id := Left_Opnd (N);
7941 Op2 : constant Node_Id := Right_Opnd (N);
7944 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7946 Generate_Operator_Reference (N, Typ);
7948 if Is_String_Type (Typ) then
7949 Eval_Concatenation (N);
7952 -- If this is not a static concatenation, but the result is a string
7953 -- type (and not an array of strings) ensure that static string operands
7954 -- have their subtypes properly constructed.
7956 if Nkind (N) /= N_String_Literal
7957 and then Is_Character_Type (Component_Type (Typ))
7959 Set_String_Literal_Subtype (Op1, Typ);
7960 Set_String_Literal_Subtype (Op2, Typ);
7962 end Resolve_Op_Concat_Rest;
7964 ----------------------
7965 -- Resolve_Op_Expon --
7966 ----------------------
7968 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7969 B_Typ : constant Entity_Id := Base_Type (Typ);
7972 -- Catch attempts to do fixed-point exponentiation with universal
7973 -- operands, which is a case where the illegality is not caught during
7974 -- normal operator analysis.
7976 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7977 Error_Msg_N ("exponentiation not available for fixed point", N);
7980 elsif Nkind (Parent (N)) in N_Op
7981 and then Is_Fixed_Point_Type (Etype (Parent (N)))
7982 and then Etype (N) = Universal_Real
7983 and then Comes_From_Source (N)
7985 Error_Msg_N ("exponentiation not available for fixed point", N);
7989 if Comes_From_Source (N)
7990 and then Ekind (Entity (N)) = E_Function
7991 and then Is_Imported (Entity (N))
7992 and then Is_Intrinsic_Subprogram (Entity (N))
7994 Resolve_Intrinsic_Operator (N, Typ);
7998 if Etype (Left_Opnd (N)) = Universal_Integer
7999 or else Etype (Left_Opnd (N)) = Universal_Real
8001 Check_For_Visible_Operator (N, B_Typ);
8004 -- We do the resolution using the base type, because intermediate values
8005 -- in expressions always are of the base type, not a subtype of it.
8007 Resolve (Left_Opnd (N), B_Typ);
8008 Resolve (Right_Opnd (N), Standard_Integer);
8010 Check_Unset_Reference (Left_Opnd (N));
8011 Check_Unset_Reference (Right_Opnd (N));
8013 Set_Etype (N, B_Typ);
8014 Generate_Operator_Reference (N, B_Typ);
8016 Analyze_Dimension (N);
8018 -- Evaluate the Expon operator for dimensioned type with rational
8021 if Ada_Version >= Ada_2012
8022 and then Is_Dimensioned_Type (B_Typ)
8024 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
8026 -- Skip the Eval_Op_Expon if the node has already been evaluated
8028 if Nkind (N) = N_Type_Conversion then
8035 -- Set overflow checking bit. Much cleverer code needed here eventually
8036 -- and perhaps the Resolve routines should be separated for the various
8037 -- arithmetic operations, since they will need different processing. ???
8039 if Nkind (N) in N_Op then
8040 if not Overflow_Checks_Suppressed (Etype (N)) then
8041 Enable_Overflow_Check (N);
8044 end Resolve_Op_Expon;
8046 --------------------
8047 -- Resolve_Op_Not --
8048 --------------------
8050 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8053 function Parent_Is_Boolean return Boolean;
8054 -- This function determines if the parent node is a boolean operator or
8055 -- operation (comparison op, membership test, or short circuit form) and
8056 -- the not in question is the left operand of this operation. Note that
8057 -- if the not is in parens, then false is returned.
8059 -----------------------
8060 -- Parent_Is_Boolean --
8061 -----------------------
8063 function Parent_Is_Boolean return Boolean is
8065 if Paren_Count (N) /= 0 then
8069 case Nkind (Parent (N)) is
8084 return Left_Opnd (Parent (N)) = N;
8090 end Parent_Is_Boolean;
8092 -- Start of processing for Resolve_Op_Not
8095 -- Predefined operations on scalar types yield the base type. On the
8096 -- other hand, logical operations on arrays yield the type of the
8097 -- arguments (and the context).
8099 if Is_Array_Type (Typ) then
8102 B_Typ := Base_Type (Typ);
8105 if Is_VMS_Operator (Entity (N)) then
8108 -- Straightforward case of incorrect arguments
8110 elsif not Valid_Boolean_Arg (Typ) then
8111 Error_Msg_N ("invalid operand type for operator&", N);
8112 Set_Etype (N, Any_Type);
8115 -- Special case of probable missing parens
8117 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8118 if Parent_Is_Boolean then
8120 ("operand of not must be enclosed in parentheses",
8124 ("no modular type available in this context", N);
8127 Set_Etype (N, Any_Type);
8130 -- OK resolution of NOT
8133 -- Warn if non-boolean types involved. This is a case like not a < b
8134 -- where a and b are modular, where we will get (not a) < b and most
8135 -- likely not (a < b) was intended.
8137 if Warn_On_Questionable_Missing_Parens
8138 and then not Is_Boolean_Type (Typ)
8139 and then Parent_Is_Boolean
8141 Error_Msg_N ("?not expression should be parenthesized here!", N);
8144 -- Warn on double negation if checking redundant constructs
8146 if Warn_On_Redundant_Constructs
8147 and then Comes_From_Source (N)
8148 and then Comes_From_Source (Right_Opnd (N))
8149 and then Root_Type (Typ) = Standard_Boolean
8150 and then Nkind (Right_Opnd (N)) = N_Op_Not
8152 Error_Msg_N ("redundant double negation?", N);
8155 -- Complete resolution and evaluation of NOT
8157 Resolve (Right_Opnd (N), B_Typ);
8158 Check_Unset_Reference (Right_Opnd (N));
8159 Set_Etype (N, B_Typ);
8160 Generate_Operator_Reference (N, B_Typ);
8165 -----------------------------
8166 -- Resolve_Operator_Symbol --
8167 -----------------------------
8169 -- Nothing to be done, all resolved already
8171 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8172 pragma Warnings (Off, N);
8173 pragma Warnings (Off, Typ);
8177 end Resolve_Operator_Symbol;
8179 ----------------------------------
8180 -- Resolve_Qualified_Expression --
8181 ----------------------------------
8183 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8184 pragma Warnings (Off, Typ);
8186 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8187 Expr : constant Node_Id := Expression (N);
8190 Resolve (Expr, Target_Typ);
8192 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8193 -- operation if not needed.
8195 if Restriction_Check_Required (SPARK)
8196 and then Is_Array_Type (Target_Typ)
8197 and then Is_Array_Type (Etype (Expr))
8198 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8199 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8201 Check_SPARK_Restriction
8202 ("array types should have matching static bounds", N);
8205 -- A qualified expression requires an exact match of the type, class-
8206 -- wide matching is not allowed. However, if the qualifying type is
8207 -- specific and the expression has a class-wide type, it may still be
8208 -- okay, since it can be the result of the expansion of a call to a
8209 -- dispatching function, so we also have to check class-wideness of the
8210 -- type of the expression's original node.
8212 if (Is_Class_Wide_Type (Target_Typ)
8214 (Is_Class_Wide_Type (Etype (Expr))
8215 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8216 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8218 Wrong_Type (Expr, Target_Typ);
8221 -- If the target type is unconstrained, then we reset the type of the
8222 -- result from the type of the expression. For other cases, the actual
8223 -- subtype of the expression is the target type.
8225 if Is_Composite_Type (Target_Typ)
8226 and then not Is_Constrained (Target_Typ)
8228 Set_Etype (N, Etype (Expr));
8231 Analyze_Dimension (N);
8232 Eval_Qualified_Expression (N);
8233 end Resolve_Qualified_Expression;
8235 -----------------------------------
8236 -- Resolve_Quantified_Expression --
8237 -----------------------------------
8239 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
8241 if not Alfa_Mode then
8243 -- If expansion is enabled, analysis is delayed until the expresssion
8244 -- is rewritten as a loop.
8246 if Operating_Mode /= Check_Semantics then
8250 -- The loop structure is already resolved during its analysis, only
8251 -- the resolution of the condition needs to be done. Expansion is
8252 -- disabled so that checks and other generated code are inserted in
8253 -- the tree after expression has been rewritten as a loop.
8255 Expander_Mode_Save_And_Set (False);
8256 Resolve (Condition (N), Typ);
8257 Expander_Mode_Restore;
8259 -- In Alfa mode, we need normal expansion in order to properly introduce
8260 -- the necessary transient scopes.
8263 Resolve (Condition (N), Typ);
8265 end Resolve_Quantified_Expression;
8271 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8272 L : constant Node_Id := Low_Bound (N);
8273 H : constant Node_Id := High_Bound (N);
8275 function First_Last_Ref return Boolean;
8276 -- Returns True if N is of the form X'First .. X'Last where X is the
8277 -- same entity for both attributes.
8279 --------------------
8280 -- First_Last_Ref --
8281 --------------------
8283 function First_Last_Ref return Boolean is
8284 Lorig : constant Node_Id := Original_Node (L);
8285 Horig : constant Node_Id := Original_Node (H);
8288 if Nkind (Lorig) = N_Attribute_Reference
8289 and then Nkind (Horig) = N_Attribute_Reference
8290 and then Attribute_Name (Lorig) = Name_First
8291 and then Attribute_Name (Horig) = Name_Last
8294 PL : constant Node_Id := Prefix (Lorig);
8295 PH : constant Node_Id := Prefix (Horig);
8297 if Is_Entity_Name (PL)
8298 and then Is_Entity_Name (PH)
8299 and then Entity (PL) = Entity (PH)
8309 -- Start of processing for Resolve_Range
8316 -- Check for inappropriate range on unordered enumeration type
8318 if Bad_Unordered_Enumeration_Reference (N, Typ)
8320 -- Exclude X'First .. X'Last if X is the same entity for both
8322 and then not First_Last_Ref
8324 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
8327 Check_Unset_Reference (L);
8328 Check_Unset_Reference (H);
8330 -- We have to check the bounds for being within the base range as
8331 -- required for a non-static context. Normally this is automatic and
8332 -- done as part of evaluating expressions, but the N_Range node is an
8333 -- exception, since in GNAT we consider this node to be a subexpression,
8334 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8335 -- this, but that would put the test on the main evaluation path for
8338 Check_Non_Static_Context (L);
8339 Check_Non_Static_Context (H);
8341 -- Check for an ambiguous range over character literals. This will
8342 -- happen with a membership test involving only literals.
8344 if Typ = Any_Character then
8345 Ambiguous_Character (L);
8346 Set_Etype (N, Any_Type);
8350 -- If bounds are static, constant-fold them, so size computations are
8351 -- identical between front-end and back-end. Do not perform this
8352 -- transformation while analyzing generic units, as type information
8353 -- would be lost when reanalyzing the constant node in the instance.
8355 if Is_Discrete_Type (Typ) and then Full_Expander_Active then
8356 if Is_OK_Static_Expression (L) then
8357 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8360 if Is_OK_Static_Expression (H) then
8361 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8366 --------------------------
8367 -- Resolve_Real_Literal --
8368 --------------------------
8370 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8371 Actual_Typ : constant Entity_Id := Etype (N);
8374 -- Special processing for fixed-point literals to make sure that the
8375 -- value is an exact multiple of small where this is required. We skip
8376 -- this for the universal real case, and also for generic types.
8378 if Is_Fixed_Point_Type (Typ)
8379 and then Typ /= Universal_Fixed
8380 and then Typ /= Any_Fixed
8381 and then not Is_Generic_Type (Typ)
8384 Val : constant Ureal := Realval (N);
8385 Cintr : constant Ureal := Val / Small_Value (Typ);
8386 Cint : constant Uint := UR_Trunc (Cintr);
8387 Den : constant Uint := Norm_Den (Cintr);
8391 -- Case of literal is not an exact multiple of the Small
8395 -- For a source program literal for a decimal fixed-point type,
8396 -- this is statically illegal (RM 4.9(36)).
8398 if Is_Decimal_Fixed_Point_Type (Typ)
8399 and then Actual_Typ = Universal_Real
8400 and then Comes_From_Source (N)
8402 Error_Msg_N ("value has extraneous low order digits", N);
8405 -- Generate a warning if literal from source
8407 if Is_Static_Expression (N)
8408 and then Warn_On_Bad_Fixed_Value
8411 ("?static fixed-point value is not a multiple of Small!",
8415 -- Replace literal by a value that is the exact representation
8416 -- of a value of the type, i.e. a multiple of the small value,
8417 -- by truncation, since Machine_Rounds is false for all GNAT
8418 -- fixed-point types (RM 4.9(38)).
8420 Stat := Is_Static_Expression (N);
8422 Make_Real_Literal (Sloc (N),
8423 Realval => Small_Value (Typ) * Cint));
8425 Set_Is_Static_Expression (N, Stat);
8428 -- In all cases, set the corresponding integer field
8430 Set_Corresponding_Integer_Value (N, Cint);
8434 -- Now replace the actual type by the expected type as usual
8437 Eval_Real_Literal (N);
8438 end Resolve_Real_Literal;
8440 -----------------------
8441 -- Resolve_Reference --
8442 -----------------------
8444 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8445 P : constant Node_Id := Prefix (N);
8448 -- Replace general access with specific type
8450 if Ekind (Etype (N)) = E_Allocator_Type then
8451 Set_Etype (N, Base_Type (Typ));
8454 Resolve (P, Designated_Type (Etype (N)));
8456 -- If we are taking the reference of a volatile entity, then treat it as
8457 -- a potential modification of this entity. This is too conservative,
8458 -- but necessary because remove side effects can cause transformations
8459 -- of normal assignments into reference sequences that otherwise fail to
8460 -- notice the modification.
8462 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8463 Note_Possible_Modification (P, Sure => False);
8465 end Resolve_Reference;
8467 --------------------------------
8468 -- Resolve_Selected_Component --
8469 --------------------------------
8471 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8473 Comp1 : Entity_Id := Empty; -- prevent junk warning
8474 P : constant Node_Id := Prefix (N);
8475 S : constant Node_Id := Selector_Name (N);
8476 T : Entity_Id := Etype (P);
8478 I1 : Interp_Index := 0; -- prevent junk warning
8483 function Init_Component return Boolean;
8484 -- Check whether this is the initialization of a component within an
8485 -- init proc (by assignment or call to another init proc). If true,
8486 -- there is no need for a discriminant check.
8488 --------------------
8489 -- Init_Component --
8490 --------------------
8492 function Init_Component return Boolean is
8494 return Inside_Init_Proc
8495 and then Nkind (Prefix (N)) = N_Identifier
8496 and then Chars (Prefix (N)) = Name_uInit
8497 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8500 -- Start of processing for Resolve_Selected_Component
8503 if Is_Overloaded (P) then
8505 -- Use the context type to select the prefix that has a selector
8506 -- of the correct name and type.
8509 Get_First_Interp (P, I, It);
8511 Search : while Present (It.Typ) loop
8512 if Is_Access_Type (It.Typ) then
8513 T := Designated_Type (It.Typ);
8518 -- Locate selected component. For a private prefix the selector
8519 -- can denote a discriminant.
8521 if Is_Record_Type (T) or else Is_Private_Type (T) then
8523 -- The visible components of a class-wide type are those of
8526 if Is_Class_Wide_Type (T) then
8530 Comp := First_Entity (T);
8531 while Present (Comp) loop
8532 if Chars (Comp) = Chars (S)
8533 and then Covers (Etype (Comp), Typ)
8542 It := Disambiguate (P, I1, I, Any_Type);
8544 if It = No_Interp then
8546 ("ambiguous prefix for selected component", N);
8553 -- There may be an implicit dereference. Retrieve
8554 -- designated record type.
8556 if Is_Access_Type (It1.Typ) then
8557 T := Designated_Type (It1.Typ);
8562 if Scope (Comp1) /= T then
8564 -- Resolution chooses the new interpretation.
8565 -- Find the component with the right name.
8567 Comp1 := First_Entity (T);
8568 while Present (Comp1)
8569 and then Chars (Comp1) /= Chars (S)
8571 Comp1 := Next_Entity (Comp1);
8580 Comp := Next_Entity (Comp);
8584 Get_Next_Interp (I, It);
8587 Resolve (P, It1.Typ);
8589 Set_Entity_With_Style_Check (S, Comp1);
8592 -- Resolve prefix with its type
8597 -- Generate cross-reference. We needed to wait until full overloading
8598 -- resolution was complete to do this, since otherwise we can't tell if
8599 -- we are an lvalue or not.
8601 if May_Be_Lvalue (N) then
8602 Generate_Reference (Entity (S), S, 'm');
8604 Generate_Reference (Entity (S), S, 'r');
8607 -- If prefix is an access type, the node will be transformed into an
8608 -- explicit dereference during expansion. The type of the node is the
8609 -- designated type of that of the prefix.
8611 if Is_Access_Type (Etype (P)) then
8612 T := Designated_Type (Etype (P));
8613 Check_Fully_Declared_Prefix (T, P);
8618 if Has_Discriminants (T)
8619 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8620 and then Present (Original_Record_Component (Entity (S)))
8621 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8622 and then Present (Discriminant_Checking_Func
8623 (Original_Record_Component (Entity (S))))
8624 and then not Discriminant_Checks_Suppressed (T)
8625 and then not Init_Component
8627 Set_Do_Discriminant_Check (N);
8630 if Ekind (Entity (S)) = E_Void then
8631 Error_Msg_N ("premature use of component", S);
8634 -- If the prefix is a record conversion, this may be a renamed
8635 -- discriminant whose bounds differ from those of the original
8636 -- one, so we must ensure that a range check is performed.
8638 if Nkind (P) = N_Type_Conversion
8639 and then Ekind (Entity (S)) = E_Discriminant
8640 and then Is_Discrete_Type (Typ)
8642 Set_Etype (N, Base_Type (Typ));
8645 -- Note: No Eval processing is required, because the prefix is of a
8646 -- record type, or protected type, and neither can possibly be static.
8648 -- If the array type is atomic, and is packed, and we are in a left side
8649 -- context, then this is worth a warning, since we have a situation
8650 -- where the access to the component may cause extra read/writes of the
8651 -- atomic array object, which could be considered unexpected.
8653 if Nkind (N) = N_Selected_Component
8654 and then (Is_Atomic (T)
8655 or else (Is_Entity_Name (Prefix (N))
8656 and then Is_Atomic (Entity (Prefix (N)))))
8657 and then Is_Packed (T)
8660 Error_Msg_N ("?assignment to component of packed atomic record",
8662 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8665 Analyze_Dimension (N);
8666 end Resolve_Selected_Component;
8672 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8673 B_Typ : constant Entity_Id := Base_Type (Typ);
8674 L : constant Node_Id := Left_Opnd (N);
8675 R : constant Node_Id := Right_Opnd (N);
8678 -- We do the resolution using the base type, because intermediate values
8679 -- in expressions always are of the base type, not a subtype of it.
8682 Resolve (R, Standard_Natural);
8684 Check_Unset_Reference (L);
8685 Check_Unset_Reference (R);
8687 Set_Etype (N, B_Typ);
8688 Generate_Operator_Reference (N, B_Typ);
8692 ---------------------------
8693 -- Resolve_Short_Circuit --
8694 ---------------------------
8696 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8697 B_Typ : constant Entity_Id := Base_Type (Typ);
8698 L : constant Node_Id := Left_Opnd (N);
8699 R : constant Node_Id := Right_Opnd (N);
8705 -- Check for issuing warning for always False assert/check, this happens
8706 -- when assertions are turned off, in which case the pragma Assert/Check
8707 -- was transformed into:
8709 -- if False and then <condition> then ...
8711 -- and we detect this pattern
8713 if Warn_On_Assertion_Failure
8714 and then Is_Entity_Name (R)
8715 and then Entity (R) = Standard_False
8716 and then Nkind (Parent (N)) = N_If_Statement
8717 and then Nkind (N) = N_And_Then
8718 and then Is_Entity_Name (L)
8719 and then Entity (L) = Standard_False
8722 Orig : constant Node_Id := Original_Node (Parent (N));
8725 if Nkind (Orig) = N_Pragma
8726 and then Pragma_Name (Orig) = Name_Assert
8728 -- Don't want to warn if original condition is explicit False
8731 Expr : constant Node_Id :=
8734 (First (Pragma_Argument_Associations (Orig))));
8736 if Is_Entity_Name (Expr)
8737 and then Entity (Expr) = Standard_False
8741 -- Issue warning. We do not want the deletion of the
8742 -- IF/AND-THEN to take this message with it. We achieve
8743 -- this by making sure that the expanded code points to
8744 -- the Sloc of the expression, not the original pragma.
8746 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
8747 -- The source location of the expression is not usually
8748 -- the best choice here. For example, it gets located on
8749 -- the last AND keyword in a chain of boolean expressiond
8750 -- AND'ed together. It is best to put the message on the
8751 -- first character of the assertion, which is the effect
8752 -- of the First_Node call here.
8755 ("?assertion would fail at run time!",
8757 (First (Pragma_Argument_Associations (Orig))));
8761 -- Similar processing for Check pragma
8763 elsif Nkind (Orig) = N_Pragma
8764 and then Pragma_Name (Orig) = Name_Check
8766 -- Don't want to warn if original condition is explicit False
8769 Expr : constant Node_Id :=
8773 (Pragma_Argument_Associations (Orig)))));
8775 if Is_Entity_Name (Expr)
8776 and then Entity (Expr) = Standard_False
8783 -- Again use Error_Msg_F rather than Error_Msg_N, see
8784 -- comment above for an explanation of why we do this.
8787 ("?check would fail at run time!",
8789 (Last (Pragma_Argument_Associations (Orig))));
8796 -- Continue with processing of short circuit
8798 Check_Unset_Reference (L);
8799 Check_Unset_Reference (R);
8801 Set_Etype (N, B_Typ);
8802 Eval_Short_Circuit (N);
8803 end Resolve_Short_Circuit;
8809 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8810 Name : constant Node_Id := Prefix (N);
8811 Drange : constant Node_Id := Discrete_Range (N);
8812 Array_Type : Entity_Id := Empty;
8816 if Is_Overloaded (Name) then
8818 -- Use the context type to select the prefix that yields the correct
8823 I1 : Interp_Index := 0;
8825 P : constant Node_Id := Prefix (N);
8826 Found : Boolean := False;
8829 Get_First_Interp (P, I, It);
8830 while Present (It.Typ) loop
8831 if (Is_Array_Type (It.Typ)
8832 and then Covers (Typ, It.Typ))
8833 or else (Is_Access_Type (It.Typ)
8834 and then Is_Array_Type (Designated_Type (It.Typ))
8835 and then Covers (Typ, Designated_Type (It.Typ)))
8838 It := Disambiguate (P, I1, I, Any_Type);
8840 if It = No_Interp then
8841 Error_Msg_N ("ambiguous prefix for slicing", N);
8846 Array_Type := It.Typ;
8851 Array_Type := It.Typ;
8856 Get_Next_Interp (I, It);
8861 Array_Type := Etype (Name);
8864 Resolve (Name, Array_Type);
8866 if Is_Access_Type (Array_Type) then
8867 Apply_Access_Check (N);
8868 Array_Type := Designated_Type (Array_Type);
8870 -- If the prefix is an access to an unconstrained array, we must use
8871 -- the actual subtype of the object to perform the index checks. The
8872 -- object denoted by the prefix is implicit in the node, so we build
8873 -- an explicit representation for it in order to compute the actual
8876 if not Is_Constrained (Array_Type) then
8877 Remove_Side_Effects (Prefix (N));
8880 Obj : constant Node_Id :=
8881 Make_Explicit_Dereference (Sloc (N),
8882 Prefix => New_Copy_Tree (Prefix (N)));
8884 Set_Etype (Obj, Array_Type);
8885 Set_Parent (Obj, Parent (N));
8886 Array_Type := Get_Actual_Subtype (Obj);
8890 elsif Is_Entity_Name (Name)
8891 or else Nkind (Name) = N_Explicit_Dereference
8892 or else (Nkind (Name) = N_Function_Call
8893 and then not Is_Constrained (Etype (Name)))
8895 Array_Type := Get_Actual_Subtype (Name);
8897 -- If the name is a selected component that depends on discriminants,
8898 -- build an actual subtype for it. This can happen only when the name
8899 -- itself is overloaded; otherwise the actual subtype is created when
8900 -- the selected component is analyzed.
8902 elsif Nkind (Name) = N_Selected_Component
8903 and then Full_Analysis
8904 and then Depends_On_Discriminant (First_Index (Array_Type))
8907 Act_Decl : constant Node_Id :=
8908 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8910 Insert_Action (N, Act_Decl);
8911 Array_Type := Defining_Identifier (Act_Decl);
8914 -- Maybe this should just be "else", instead of checking for the
8915 -- specific case of slice??? This is needed for the case where the
8916 -- prefix is an Image attribute, which gets expanded to a slice, and so
8917 -- has a constrained subtype which we want to use for the slice range
8918 -- check applied below (the range check won't get done if the
8919 -- unconstrained subtype of the 'Image is used).
8921 elsif Nkind (Name) = N_Slice then
8922 Array_Type := Etype (Name);
8925 -- If name was overloaded, set slice type correctly now
8927 Set_Etype (N, Array_Type);
8929 -- If the range is specified by a subtype mark, no resolution is
8930 -- necessary. Else resolve the bounds, and apply needed checks.
8932 if not Is_Entity_Name (Drange) then
8933 Index := First_Index (Array_Type);
8934 Resolve (Drange, Base_Type (Etype (Index)));
8936 if Nkind (Drange) = N_Range then
8938 -- Ensure that side effects in the bounds are properly handled
8940 Force_Evaluation (Low_Bound (Drange));
8941 Force_Evaluation (High_Bound (Drange));
8943 -- Do not apply the range check to nodes associated with the
8944 -- frontend expansion of the dispatch table. We first check
8945 -- if Ada.Tags is already loaded to avoid the addition of an
8946 -- undesired dependence on such run-time unit.
8948 if not Tagged_Type_Expansion
8950 (RTU_Loaded (Ada_Tags)
8951 and then Nkind (Prefix (N)) = N_Selected_Component
8952 and then Present (Entity (Selector_Name (Prefix (N))))
8953 and then Entity (Selector_Name (Prefix (N))) =
8954 RTE_Record_Component (RE_Prims_Ptr))
8956 Apply_Range_Check (Drange, Etype (Index));
8961 Set_Slice_Subtype (N);
8963 -- Check bad use of type with predicates
8965 if Has_Predicates (Etype (Drange)) then
8966 Bad_Predicated_Subtype_Use
8967 ("subtype& has predicate, not allowed in slice",
8968 Drange, Etype (Drange));
8970 -- Otherwise here is where we check suspicious indexes
8972 elsif Nkind (Drange) = N_Range then
8973 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8974 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8977 Analyze_Dimension (N);
8981 ----------------------------
8982 -- Resolve_String_Literal --
8983 ----------------------------
8985 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8986 C_Typ : constant Entity_Id := Component_Type (Typ);
8987 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8988 Loc : constant Source_Ptr := Sloc (N);
8989 Str : constant String_Id := Strval (N);
8990 Strlen : constant Nat := String_Length (Str);
8991 Subtype_Id : Entity_Id;
8992 Need_Check : Boolean;
8995 -- For a string appearing in a concatenation, defer creation of the
8996 -- string_literal_subtype until the end of the resolution of the
8997 -- concatenation, because the literal may be constant-folded away. This
8998 -- is a useful optimization for long concatenation expressions.
9000 -- If the string is an aggregate built for a single character (which
9001 -- happens in a non-static context) or a is null string to which special
9002 -- checks may apply, we build the subtype. Wide strings must also get a
9003 -- string subtype if they come from a one character aggregate. Strings
9004 -- generated by attributes might be static, but it is often hard to
9005 -- determine whether the enclosing context is static, so we generate
9006 -- subtypes for them as well, thus losing some rarer optimizations ???
9007 -- Same for strings that come from a static conversion.
9010 (Strlen = 0 and then Typ /= Standard_String)
9011 or else Nkind (Parent (N)) /= N_Op_Concat
9012 or else (N /= Left_Opnd (Parent (N))
9013 and then N /= Right_Opnd (Parent (N)))
9014 or else ((Typ = Standard_Wide_String
9015 or else Typ = Standard_Wide_Wide_String)
9016 and then Nkind (Original_Node (N)) /= N_String_Literal);
9018 -- If the resolving type is itself a string literal subtype, we can just
9019 -- reuse it, since there is no point in creating another.
9021 if Ekind (Typ) = E_String_Literal_Subtype then
9024 elsif Nkind (Parent (N)) = N_Op_Concat
9025 and then not Need_Check
9026 and then not Nkind_In (Original_Node (N), N_Character_Literal,
9027 N_Attribute_Reference,
9028 N_Qualified_Expression,
9033 -- Otherwise we must create a string literal subtype. Note that the
9034 -- whole idea of string literal subtypes is simply to avoid the need
9035 -- for building a full fledged array subtype for each literal.
9038 Set_String_Literal_Subtype (N, Typ);
9039 Subtype_Id := Etype (N);
9042 if Nkind (Parent (N)) /= N_Op_Concat
9045 Set_Etype (N, Subtype_Id);
9046 Eval_String_Literal (N);
9049 if Is_Limited_Composite (Typ)
9050 or else Is_Private_Composite (Typ)
9052 Error_Msg_N ("string literal not available for private array", N);
9053 Set_Etype (N, Any_Type);
9057 -- The validity of a null string has been checked in the call to
9058 -- Eval_String_Literal.
9063 -- Always accept string literal with component type Any_Character, which
9064 -- occurs in error situations and in comparisons of literals, both of
9065 -- which should accept all literals.
9067 elsif R_Typ = Any_Character then
9070 -- If the type is bit-packed, then we always transform the string
9071 -- literal into a full fledged aggregate.
9073 elsif Is_Bit_Packed_Array (Typ) then
9076 -- Deal with cases of Wide_Wide_String, Wide_String, and String
9079 -- For Standard.Wide_Wide_String, or any other type whose component
9080 -- type is Standard.Wide_Wide_Character, we know that all the
9081 -- characters in the string must be acceptable, since the parser
9082 -- accepted the characters as valid character literals.
9084 if R_Typ = Standard_Wide_Wide_Character then
9087 -- For the case of Standard.String, or any other type whose component
9088 -- type is Standard.Character, we must make sure that there are no
9089 -- wide characters in the string, i.e. that it is entirely composed
9090 -- of characters in range of type Character.
9092 -- If the string literal is the result of a static concatenation, the
9093 -- test has already been performed on the components, and need not be
9096 elsif R_Typ = Standard_Character
9097 and then Nkind (Original_Node (N)) /= N_Op_Concat
9099 for J in 1 .. Strlen loop
9100 if not In_Character_Range (Get_String_Char (Str, J)) then
9102 -- If we are out of range, post error. This is one of the
9103 -- very few places that we place the flag in the middle of
9104 -- a token, right under the offending wide character. Not
9105 -- quite clear if this is right wrt wide character encoding
9106 -- sequences, but it's only an error message!
9109 ("literal out of range of type Standard.Character",
9110 Source_Ptr (Int (Loc) + J));
9115 -- For the case of Standard.Wide_String, or any other type whose
9116 -- component type is Standard.Wide_Character, we must make sure that
9117 -- there are no wide characters in the string, i.e. that it is
9118 -- entirely composed of characters in range of type Wide_Character.
9120 -- If the string literal is the result of a static concatenation,
9121 -- the test has already been performed on the components, and need
9124 elsif R_Typ = Standard_Wide_Character
9125 and then Nkind (Original_Node (N)) /= N_Op_Concat
9127 for J in 1 .. Strlen loop
9128 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
9130 -- If we are out of range, post error. This is one of the
9131 -- very few places that we place the flag in the middle of
9132 -- a token, right under the offending wide character.
9134 -- This is not quite right, because characters in general
9135 -- will take more than one character position ???
9138 ("literal out of range of type Standard.Wide_Character",
9139 Source_Ptr (Int (Loc) + J));
9144 -- If the root type is not a standard character, then we will convert
9145 -- the string into an aggregate and will let the aggregate code do
9146 -- the checking. Standard Wide_Wide_Character is also OK here.
9152 -- See if the component type of the array corresponding to the string
9153 -- has compile time known bounds. If yes we can directly check
9154 -- whether the evaluation of the string will raise constraint error.
9155 -- Otherwise we need to transform the string literal into the
9156 -- corresponding character aggregate and let the aggregate code do
9159 if Is_Standard_Character_Type (R_Typ) then
9161 -- Check for the case of full range, where we are definitely OK
9163 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
9167 -- Here the range is not the complete base type range, so check
9170 Comp_Typ_Lo : constant Node_Id :=
9171 Type_Low_Bound (Component_Type (Typ));
9172 Comp_Typ_Hi : constant Node_Id :=
9173 Type_High_Bound (Component_Type (Typ));
9178 if Compile_Time_Known_Value (Comp_Typ_Lo)
9179 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9181 for J in 1 .. Strlen loop
9182 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9184 if Char_Val < Expr_Value (Comp_Typ_Lo)
9185 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9187 Apply_Compile_Time_Constraint_Error
9188 (N, "character out of range?", CE_Range_Check_Failed,
9189 Loc => Source_Ptr (Int (Loc) + J));
9199 -- If we got here we meed to transform the string literal into the
9200 -- equivalent qualified positional array aggregate. This is rather
9201 -- heavy artillery for this situation, but it is hard work to avoid.
9204 Lits : constant List_Id := New_List;
9205 P : Source_Ptr := Loc + 1;
9209 -- Build the character literals, we give them source locations that
9210 -- correspond to the string positions, which is a bit tricky given
9211 -- the possible presence of wide character escape sequences.
9213 for J in 1 .. Strlen loop
9214 C := Get_String_Char (Str, J);
9215 Set_Character_Literal_Name (C);
9218 Make_Character_Literal (P,
9220 Char_Literal_Value => UI_From_CC (C)));
9222 if In_Character_Range (C) then
9225 -- Should we have a call to Skip_Wide here ???
9234 Make_Qualified_Expression (Loc,
9235 Subtype_Mark => New_Reference_To (Typ, Loc),
9237 Make_Aggregate (Loc, Expressions => Lits)));
9239 Analyze_And_Resolve (N, Typ);
9241 end Resolve_String_Literal;
9243 -----------------------------
9244 -- Resolve_Subprogram_Info --
9245 -----------------------------
9247 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9250 end Resolve_Subprogram_Info;
9252 -----------------------------
9253 -- Resolve_Type_Conversion --
9254 -----------------------------
9256 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9257 Conv_OK : constant Boolean := Conversion_OK (N);
9258 Operand : constant Node_Id := Expression (N);
9259 Operand_Typ : constant Entity_Id := Etype (Operand);
9260 Target_Typ : constant Entity_Id := Etype (N);
9265 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9266 -- Set to False to suppress cases where we want to suppress the test
9267 -- for redundancy to avoid possible false positives on this warning.
9271 and then not Valid_Conversion (N, Target_Typ, Operand)
9276 -- If the Operand Etype is Universal_Fixed, then the conversion is
9277 -- never redundant. We need this check because by the time we have
9278 -- finished the rather complex transformation, the conversion looks
9279 -- redundant when it is not.
9281 if Operand_Typ = Universal_Fixed then
9282 Test_Redundant := False;
9284 -- If the operand is marked as Any_Fixed, then special processing is
9285 -- required. This is also a case where we suppress the test for a
9286 -- redundant conversion, since most certainly it is not redundant.
9288 elsif Operand_Typ = Any_Fixed then
9289 Test_Redundant := False;
9291 -- Mixed-mode operation involving a literal. Context must be a fixed
9292 -- type which is applied to the literal subsequently.
9294 if Is_Fixed_Point_Type (Typ) then
9295 Set_Etype (Operand, Universal_Real);
9297 elsif Is_Numeric_Type (Typ)
9298 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9299 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9301 Etype (Left_Opnd (Operand)) = Universal_Real)
9303 -- Return if expression is ambiguous
9305 if Unique_Fixed_Point_Type (N) = Any_Type then
9308 -- If nothing else, the available fixed type is Duration
9311 Set_Etype (Operand, Standard_Duration);
9314 -- Resolve the real operand with largest available precision
9316 if Etype (Right_Opnd (Operand)) = Universal_Real then
9317 Rop := New_Copy_Tree (Right_Opnd (Operand));
9319 Rop := New_Copy_Tree (Left_Opnd (Operand));
9322 Resolve (Rop, Universal_Real);
9324 -- If the operand is a literal (it could be a non-static and
9325 -- illegal exponentiation) check whether the use of Duration
9326 -- is potentially inaccurate.
9328 if Nkind (Rop) = N_Real_Literal
9329 and then Realval (Rop) /= Ureal_0
9330 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9333 ("?universal real operand can only " &
9334 "be interpreted as Duration!",
9337 ("\?precision will be lost in the conversion!", Rop);
9340 elsif Is_Numeric_Type (Typ)
9341 and then Nkind (Operand) in N_Op
9342 and then Unique_Fixed_Point_Type (N) /= Any_Type
9344 Set_Etype (Operand, Standard_Duration);
9347 Error_Msg_N ("invalid context for mixed mode operation", N);
9348 Set_Etype (Operand, Any_Type);
9355 -- In SPARK, a type conversion between array types should be restricted
9356 -- to types which have matching static bounds.
9358 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9359 -- operation if not needed.
9361 if Restriction_Check_Required (SPARK)
9362 and then Is_Array_Type (Target_Typ)
9363 and then Is_Array_Type (Operand_Typ)
9364 and then Operand_Typ /= Any_Composite -- or else Operand in error
9365 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9367 Check_SPARK_Restriction
9368 ("array types should have matching static bounds", N);
9371 -- In formal mode, the operand of an ancestor type conversion must be an
9372 -- object (not an expression).
9374 if Is_Tagged_Type (Target_Typ)
9375 and then not Is_Class_Wide_Type (Target_Typ)
9376 and then Is_Tagged_Type (Operand_Typ)
9377 and then not Is_Class_Wide_Type (Operand_Typ)
9378 and then Is_Ancestor (Target_Typ, Operand_Typ)
9379 and then not Is_SPARK_Object_Reference (Operand)
9381 Check_SPARK_Restriction ("object required", Operand);
9384 Analyze_Dimension (N);
9386 -- Note: we do the Eval_Type_Conversion call before applying the
9387 -- required checks for a subtype conversion. This is important, since
9388 -- both are prepared under certain circumstances to change the type
9389 -- conversion to a constraint error node, but in the case of
9390 -- Eval_Type_Conversion this may reflect an illegality in the static
9391 -- case, and we would miss the illegality (getting only a warning
9392 -- message), if we applied the type conversion checks first.
9394 Eval_Type_Conversion (N);
9396 -- Even when evaluation is not possible, we may be able to simplify the
9397 -- conversion or its expression. This needs to be done before applying
9398 -- checks, since otherwise the checks may use the original expression
9399 -- and defeat the simplifications. This is specifically the case for
9400 -- elimination of the floating-point Truncation attribute in
9401 -- float-to-int conversions.
9403 Simplify_Type_Conversion (N);
9405 -- If after evaluation we still have a type conversion, then we may need
9406 -- to apply checks required for a subtype conversion.
9408 -- Skip these type conversion checks if universal fixed operands
9409 -- operands involved, since range checks are handled separately for
9410 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9412 if Nkind (N) = N_Type_Conversion
9413 and then not Is_Generic_Type (Root_Type (Target_Typ))
9414 and then Target_Typ /= Universal_Fixed
9415 and then Operand_Typ /= Universal_Fixed
9417 Apply_Type_Conversion_Checks (N);
9420 -- Issue warning for conversion of simple object to its own type. We
9421 -- have to test the original nodes, since they may have been rewritten
9422 -- by various optimizations.
9424 Orig_N := Original_Node (N);
9426 -- Here we test for a redundant conversion if the warning mode is
9427 -- active (and was not locally reset), and we have a type conversion
9428 -- from source not appearing in a generic instance.
9431 and then Nkind (Orig_N) = N_Type_Conversion
9432 and then Comes_From_Source (Orig_N)
9433 and then not In_Instance
9435 Orig_N := Original_Node (Expression (Orig_N));
9436 Orig_T := Target_Typ;
9438 -- If the node is part of a larger expression, the Target_Type
9439 -- may not be the original type of the node if the context is a
9440 -- condition. Recover original type to see if conversion is needed.
9442 if Is_Boolean_Type (Orig_T)
9443 and then Nkind (Parent (N)) in N_Op
9445 Orig_T := Etype (Parent (N));
9448 -- If we have an entity name, then give the warning if the entity
9449 -- is the right type, or if it is a loop parameter covered by the
9450 -- original type (that's needed because loop parameters have an
9451 -- odd subtype coming from the bounds).
9453 if (Is_Entity_Name (Orig_N)
9455 (Etype (Entity (Orig_N)) = Orig_T
9457 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9458 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9460 -- If not an entity, then type of expression must match
9462 or else Etype (Orig_N) = Orig_T
9464 -- One more check, do not give warning if the analyzed conversion
9465 -- has an expression with non-static bounds, and the bounds of the
9466 -- target are static. This avoids junk warnings in cases where the
9467 -- conversion is necessary to establish staticness, for example in
9468 -- a case statement.
9470 if not Is_OK_Static_Subtype (Operand_Typ)
9471 and then Is_OK_Static_Subtype (Target_Typ)
9475 -- Finally, if this type conversion occurs in a context requiring
9476 -- a prefix, and the expression is a qualified expression then the
9477 -- type conversion is not redundant, since a qualified expression
9478 -- is not a prefix, whereas a type conversion is. For example, "X
9479 -- := T'(Funx(...)).Y;" is illegal because a selected component
9480 -- requires a prefix, but a type conversion makes it legal: "X :=
9481 -- T(T'(Funx(...))).Y;"
9483 -- In Ada 2012, a qualified expression is a name, so this idiom is
9484 -- no longer needed, but we still suppress the warning because it
9485 -- seems unfriendly for warnings to pop up when you switch to the
9486 -- newer language version.
9488 elsif Nkind (Orig_N) = N_Qualified_Expression
9489 and then Nkind_In (Parent (N), N_Attribute_Reference,
9490 N_Indexed_Component,
9491 N_Selected_Component,
9493 N_Explicit_Dereference)
9497 -- Here we give the redundant conversion warning. If it is an
9498 -- entity, give the name of the entity in the message. If not,
9499 -- just mention the expression.
9502 if Is_Entity_Name (Orig_N) then
9503 Error_Msg_Node_2 := Orig_T;
9504 Error_Msg_NE -- CODEFIX
9505 ("?redundant conversion, & is of type &!",
9506 N, Entity (Orig_N));
9509 ("?redundant conversion, expression is of type&!",
9516 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9517 -- No need to perform any interface conversion if the type of the
9518 -- expression coincides with the target type.
9520 if Ada_Version >= Ada_2005
9521 and then Full_Expander_Active
9522 and then Operand_Typ /= Target_Typ
9525 Opnd : Entity_Id := Operand_Typ;
9526 Target : Entity_Id := Target_Typ;
9529 if Is_Access_Type (Opnd) then
9530 Opnd := Designated_Type (Opnd);
9533 if Is_Access_Type (Target_Typ) then
9534 Target := Designated_Type (Target);
9537 if Opnd = Target then
9540 -- Conversion from interface type
9542 elsif Is_Interface (Opnd) then
9544 -- Ada 2005 (AI-217): Handle entities from limited views
9546 if From_With_Type (Opnd) then
9547 Error_Msg_Qual_Level := 99;
9548 Error_Msg_NE -- CODEFIX
9549 ("missing WITH clause on package &", N,
9550 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9552 ("type conversions require visibility of the full view",
9555 elsif From_With_Type (Target)
9557 (Is_Access_Type (Target_Typ)
9558 and then Present (Non_Limited_View (Etype (Target))))
9560 Error_Msg_Qual_Level := 99;
9561 Error_Msg_NE -- CODEFIX
9562 ("missing WITH clause on package &", N,
9563 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9565 ("type conversions require visibility of the full view",
9569 Expand_Interface_Conversion (N, Is_Static => False);
9572 -- Conversion to interface type
9574 elsif Is_Interface (Target) then
9578 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9579 Opnd := Etype (Opnd);
9582 if not Interface_Present_In_Ancestor
9586 if Is_Class_Wide_Type (Opnd) then
9588 -- The static analysis is not enough to know if the
9589 -- interface is implemented or not. Hence we must pass
9590 -- the work to the expander to generate code to evaluate
9591 -- the conversion at run time.
9593 Expand_Interface_Conversion (N, Is_Static => False);
9596 Error_Msg_Name_1 := Chars (Etype (Target));
9597 Error_Msg_Name_2 := Chars (Opnd);
9599 ("wrong interface conversion (% is not a progenitor " &
9604 Expand_Interface_Conversion (N);
9609 end Resolve_Type_Conversion;
9611 ----------------------
9612 -- Resolve_Unary_Op --
9613 ----------------------
9615 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9616 B_Typ : constant Entity_Id := Base_Type (Typ);
9617 R : constant Node_Id := Right_Opnd (N);
9623 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9624 Error_Msg_Name_1 := Chars (Typ);
9625 Check_SPARK_Restriction
9626 ("unary operator not defined for modular type%", N);
9629 -- Deal with intrinsic unary operators
9631 if Comes_From_Source (N)
9632 and then Ekind (Entity (N)) = E_Function
9633 and then Is_Imported (Entity (N))
9634 and then Is_Intrinsic_Subprogram (Entity (N))
9636 Resolve_Intrinsic_Unary_Operator (N, Typ);
9640 -- Deal with universal cases
9642 if Etype (R) = Universal_Integer
9644 Etype (R) = Universal_Real
9646 Check_For_Visible_Operator (N, B_Typ);
9649 Set_Etype (N, B_Typ);
9652 -- Generate warning for expressions like abs (x mod 2)
9654 if Warn_On_Redundant_Constructs
9655 and then Nkind (N) = N_Op_Abs
9657 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9659 if OK and then Hi >= Lo and then Lo >= 0 then
9660 Error_Msg_N -- CODEFIX
9661 ("?abs applied to known non-negative value has no effect", N);
9665 -- Deal with reference generation
9667 Check_Unset_Reference (R);
9668 Generate_Operator_Reference (N, B_Typ);
9669 Analyze_Dimension (N);
9672 -- Set overflow checking bit. Much cleverer code needed here eventually
9673 -- and perhaps the Resolve routines should be separated for the various
9674 -- arithmetic operations, since they will need different processing ???
9676 if Nkind (N) in N_Op then
9677 if not Overflow_Checks_Suppressed (Etype (N)) then
9678 Enable_Overflow_Check (N);
9682 -- Generate warning for expressions like -5 mod 3 for integers. No need
9683 -- to worry in the floating-point case, since parens do not affect the
9684 -- result so there is no point in giving in a warning.
9687 Norig : constant Node_Id := Original_Node (N);
9696 if Warn_On_Questionable_Missing_Parens
9697 and then Comes_From_Source (Norig)
9698 and then Is_Integer_Type (Typ)
9699 and then Nkind (Norig) = N_Op_Minus
9701 Rorig := Original_Node (Right_Opnd (Norig));
9703 -- We are looking for cases where the right operand is not
9704 -- parenthesized, and is a binary operator, multiply, divide, or
9705 -- mod. These are the cases where the grouping can affect results.
9707 if Paren_Count (Rorig) = 0
9708 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9710 -- For mod, we always give the warning, since the value is
9711 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9712 -- -(5 mod 315)). But for the other cases, the only concern is
9713 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9714 -- overflows, but (-2) * 64 does not). So we try to give the
9715 -- message only when overflow is possible.
9717 if Nkind (Rorig) /= N_Op_Mod
9718 and then Compile_Time_Known_Value (R)
9720 Val := Expr_Value (R);
9722 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9723 HB := Expr_Value (Type_High_Bound (Typ));
9725 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9728 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9729 LB := Expr_Value (Type_Low_Bound (Typ));
9731 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9734 -- Note that the test below is deliberately excluding the
9735 -- largest negative number, since that is a potentially
9736 -- troublesome case (e.g. -2 * x, where the result is the
9737 -- largest negative integer has an overflow with 2 * x).
9739 if Val > LB and then Val <= HB then
9744 -- For the multiplication case, the only case we have to worry
9745 -- about is when (-a)*b is exactly the largest negative number
9746 -- so that -(a*b) can cause overflow. This can only happen if
9747 -- a is a power of 2, and more generally if any operand is a
9748 -- constant that is not a power of 2, then the parentheses
9749 -- cannot affect whether overflow occurs. We only bother to
9750 -- test the left most operand
9752 -- Loop looking at left operands for one that has known value
9755 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9756 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9757 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9759 -- Operand value of 0 or 1 skips warning
9764 -- Otherwise check power of 2, if power of 2, warn, if
9765 -- anything else, skip warning.
9768 while Lval /= 2 loop
9769 if Lval mod 2 = 1 then
9780 -- Keep looking at left operands
9782 Opnd := Left_Opnd (Opnd);
9785 -- For rem or "/" we can only have a problematic situation
9786 -- if the divisor has a value of minus one or one. Otherwise
9787 -- overflow is impossible (divisor > 1) or we have a case of
9788 -- division by zero in any case.
9790 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9791 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9792 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9797 -- If we fall through warning should be issued
9800 ("?unary minus expression should be parenthesized here!", N);
9804 end Resolve_Unary_Op;
9806 ----------------------------------
9807 -- Resolve_Unchecked_Expression --
9808 ----------------------------------
9810 procedure Resolve_Unchecked_Expression
9815 Resolve (Expression (N), Typ, Suppress => All_Checks);
9817 end Resolve_Unchecked_Expression;
9819 ---------------------------------------
9820 -- Resolve_Unchecked_Type_Conversion --
9821 ---------------------------------------
9823 procedure Resolve_Unchecked_Type_Conversion
9827 pragma Warnings (Off, Typ);
9829 Operand : constant Node_Id := Expression (N);
9830 Opnd_Type : constant Entity_Id := Etype (Operand);
9833 -- Resolve operand using its own type
9835 Resolve (Operand, Opnd_Type);
9836 Analyze_Dimension (N);
9837 Eval_Unchecked_Conversion (N);
9838 end Resolve_Unchecked_Type_Conversion;
9840 ------------------------------
9841 -- Rewrite_Operator_As_Call --
9842 ------------------------------
9844 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9845 Loc : constant Source_Ptr := Sloc (N);
9846 Actuals : constant List_Id := New_List;
9850 if Nkind (N) in N_Binary_Op then
9851 Append (Left_Opnd (N), Actuals);
9854 Append (Right_Opnd (N), Actuals);
9857 Make_Function_Call (Sloc => Loc,
9858 Name => New_Occurrence_Of (Nam, Loc),
9859 Parameter_Associations => Actuals);
9861 Preserve_Comes_From_Source (New_N, N);
9862 Preserve_Comes_From_Source (Name (New_N), N);
9864 Set_Etype (N, Etype (Nam));
9865 end Rewrite_Operator_As_Call;
9867 ------------------------------
9868 -- Rewrite_Renamed_Operator --
9869 ------------------------------
9871 procedure Rewrite_Renamed_Operator
9876 Nam : constant Name_Id := Chars (Op);
9877 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9881 -- Rewrite the operator node using the real operator, not its renaming.
9882 -- Exclude user-defined intrinsic operations of the same name, which are
9883 -- treated separately and rewritten as calls.
9885 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9886 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9887 Set_Chars (Op_Node, Nam);
9888 Set_Etype (Op_Node, Etype (N));
9889 Set_Entity (Op_Node, Op);
9890 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9892 -- Indicate that both the original entity and its renaming are
9893 -- referenced at this point.
9895 Generate_Reference (Entity (N), N);
9896 Generate_Reference (Op, N);
9899 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9902 Rewrite (N, Op_Node);
9904 -- If the context type is private, add the appropriate conversions so
9905 -- that the operator is applied to the full view. This is done in the
9906 -- routines that resolve intrinsic operators.
9908 if Is_Intrinsic_Subprogram (Op)
9909 and then Is_Private_Type (Typ)
9912 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9913 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9914 Resolve_Intrinsic_Operator (N, Typ);
9916 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9917 Resolve_Intrinsic_Unary_Operator (N, Typ);
9924 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9926 -- Operator renames a user-defined operator of the same name. Use the
9927 -- original operator in the node, which is the one Gigi knows about.
9930 Set_Is_Overloaded (N, False);
9932 end Rewrite_Renamed_Operator;
9934 -----------------------
9935 -- Set_Slice_Subtype --
9936 -----------------------
9938 -- Build an implicit subtype declaration to represent the type delivered by
9939 -- the slice. This is an abbreviated version of an array subtype. We define
9940 -- an index subtype for the slice, using either the subtype name or the
9941 -- discrete range of the slice. To be consistent with index usage elsewhere
9942 -- we create a list header to hold the single index. This list is not
9943 -- otherwise attached to the syntax tree.
9945 procedure Set_Slice_Subtype (N : Node_Id) is
9946 Loc : constant Source_Ptr := Sloc (N);
9947 Index_List : constant List_Id := New_List;
9949 Index_Subtype : Entity_Id;
9950 Index_Type : Entity_Id;
9951 Slice_Subtype : Entity_Id;
9952 Drange : constant Node_Id := Discrete_Range (N);
9955 if Is_Entity_Name (Drange) then
9956 Index_Subtype := Entity (Drange);
9959 -- We force the evaluation of a range. This is definitely needed in
9960 -- the renamed case, and seems safer to do unconditionally. Note in
9961 -- any case that since we will create and insert an Itype referring
9962 -- to this range, we must make sure any side effect removal actions
9963 -- are inserted before the Itype definition.
9965 if Nkind (Drange) = N_Range then
9966 Force_Evaluation (Low_Bound (Drange));
9967 Force_Evaluation (High_Bound (Drange));
9970 Index_Type := Base_Type (Etype (Drange));
9972 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9974 -- Take a new copy of Drange (where bounds have been rewritten to
9975 -- reference side-effect-free names). Using a separate tree ensures
9976 -- that further expansion (e.g. while rewriting a slice assignment
9977 -- into a FOR loop) does not attempt to remove side effects on the
9978 -- bounds again (which would cause the bounds in the index subtype
9979 -- definition to refer to temporaries before they are defined) (the
9980 -- reason is that some names are considered side effect free here
9981 -- for the subtype, but not in the context of a loop iteration
9984 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9985 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
9986 Set_Etype (Index_Subtype, Index_Type);
9987 Set_Size_Info (Index_Subtype, Index_Type);
9988 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9991 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9993 Index := New_Occurrence_Of (Index_Subtype, Loc);
9994 Set_Etype (Index, Index_Subtype);
9995 Append (Index, Index_List);
9997 Set_First_Index (Slice_Subtype, Index);
9998 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9999 Set_Is_Constrained (Slice_Subtype, True);
10001 Check_Compile_Time_Size (Slice_Subtype);
10003 -- The Etype of the existing Slice node is reset to this slice subtype.
10004 -- Its bounds are obtained from its first index.
10006 Set_Etype (N, Slice_Subtype);
10008 -- For packed slice subtypes, freeze immediately (except in the case of
10009 -- being in a "spec expression" where we never freeze when we first see
10010 -- the expression).
10012 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
10013 Freeze_Itype (Slice_Subtype, N);
10015 -- For all other cases insert an itype reference in the slice's actions
10016 -- so that the itype is frozen at the proper place in the tree (i.e. at
10017 -- the point where actions for the slice are analyzed). Note that this
10018 -- is different from freezing the itype immediately, which might be
10019 -- premature (e.g. if the slice is within a transient scope). This needs
10020 -- to be done only if expansion is enabled.
10022 elsif Full_Expander_Active then
10023 Ensure_Defined (Typ => Slice_Subtype, N => N);
10025 end Set_Slice_Subtype;
10027 --------------------------------
10028 -- Set_String_Literal_Subtype --
10029 --------------------------------
10031 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
10032 Loc : constant Source_Ptr := Sloc (N);
10033 Low_Bound : constant Node_Id :=
10034 Type_Low_Bound (Etype (First_Index (Typ)));
10035 Subtype_Id : Entity_Id;
10038 if Nkind (N) /= N_String_Literal then
10042 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
10043 Set_String_Literal_Length (Subtype_Id, UI_From_Int
10044 (String_Length (Strval (N))));
10045 Set_Etype (Subtype_Id, Base_Type (Typ));
10046 Set_Is_Constrained (Subtype_Id);
10047 Set_Etype (N, Subtype_Id);
10049 if Is_OK_Static_Expression (Low_Bound) then
10051 -- The low bound is set from the low bound of the corresponding index
10052 -- type. Note that we do not store the high bound in the string literal
10053 -- subtype, but it can be deduced if necessary from the length and the
10056 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
10059 -- If the lower bound is not static we create a range for the string
10060 -- literal, using the index type and the known length of the literal.
10061 -- The index type is not necessarily Positive, so the upper bound is
10062 -- computed as T'Val (T'Pos (Low_Bound) + L - 1)
10065 Index_List : constant List_Id := New_List;
10066 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
10068 High_Bound : constant Node_Id :=
10069 Make_Attribute_Reference (Loc,
10070 Attribute_Name => Name_Val,
10072 New_Occurrence_Of (Index_Type, Loc),
10073 Expressions => New_List (
10076 Make_Attribute_Reference (Loc,
10077 Attribute_Name => Name_Pos,
10079 New_Occurrence_Of (Index_Type, Loc),
10081 New_List (New_Copy_Tree (Low_Bound))),
10083 Make_Integer_Literal (Loc,
10084 String_Length (Strval (N)) - 1))));
10086 Array_Subtype : Entity_Id;
10087 Index_Subtype : Entity_Id;
10092 if Is_Integer_Type (Index_Type) then
10093 Set_String_Literal_Low_Bound
10094 (Subtype_Id, Make_Integer_Literal (Loc, 1));
10097 -- If the index type is an enumeration type, build bounds
10098 -- expression with attributes.
10100 Set_String_Literal_Low_Bound
10102 Make_Attribute_Reference (Loc,
10103 Attribute_Name => Name_First,
10105 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
10106 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
10109 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
10111 -- Build bona fide subtype for the string, and wrap it in an
10112 -- unchecked conversion, because the backend expects the
10113 -- String_Literal_Subtype to have a static lower bound.
10116 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10117 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
10118 Set_Scalar_Range (Index_Subtype, Drange);
10119 Set_Parent (Drange, N);
10120 Analyze_And_Resolve (Drange, Index_Type);
10122 -- In the context, the Index_Type may already have a constraint,
10123 -- so use common base type on string subtype. The base type may
10124 -- be used when generating attributes of the string, for example
10125 -- in the context of a slice assignment.
10127 Set_Etype (Index_Subtype, Base_Type (Index_Type));
10128 Set_Size_Info (Index_Subtype, Index_Type);
10129 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10131 Array_Subtype := Create_Itype (E_Array_Subtype, N);
10133 Index := New_Occurrence_Of (Index_Subtype, Loc);
10134 Set_Etype (Index, Index_Subtype);
10135 Append (Index, Index_List);
10137 Set_First_Index (Array_Subtype, Index);
10138 Set_Etype (Array_Subtype, Base_Type (Typ));
10139 Set_Is_Constrained (Array_Subtype, True);
10142 Make_Unchecked_Type_Conversion (Loc,
10143 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
10144 Expression => Relocate_Node (N)));
10145 Set_Etype (N, Array_Subtype);
10148 end Set_String_Literal_Subtype;
10150 ------------------------------
10151 -- Simplify_Type_Conversion --
10152 ------------------------------
10154 procedure Simplify_Type_Conversion (N : Node_Id) is
10156 if Nkind (N) = N_Type_Conversion then
10158 Operand : constant Node_Id := Expression (N);
10159 Target_Typ : constant Entity_Id := Etype (N);
10160 Opnd_Typ : constant Entity_Id := Etype (Operand);
10163 if Is_Floating_Point_Type (Opnd_Typ)
10165 (Is_Integer_Type (Target_Typ)
10166 or else (Is_Fixed_Point_Type (Target_Typ)
10167 and then Conversion_OK (N)))
10168 and then Nkind (Operand) = N_Attribute_Reference
10169 and then Attribute_Name (Operand) = Name_Truncation
10171 -- Special processing required if the conversion is the expression
10172 -- of a Truncation attribute reference. In this case we replace:
10174 -- ityp (ftyp'Truncation (x))
10180 -- with the Float_Truncate flag set, which is more efficient.
10184 Relocate_Node (First (Expressions (Operand))));
10185 Set_Float_Truncate (N, True);
10189 end Simplify_Type_Conversion;
10191 -----------------------------
10192 -- Unique_Fixed_Point_Type --
10193 -----------------------------
10195 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10196 T1 : Entity_Id := Empty;
10201 procedure Fixed_Point_Error;
10202 -- Give error messages for true ambiguity. Messages are posted on node
10203 -- N, and entities T1, T2 are the possible interpretations.
10205 -----------------------
10206 -- Fixed_Point_Error --
10207 -----------------------
10209 procedure Fixed_Point_Error is
10211 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10212 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10213 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10214 end Fixed_Point_Error;
10216 -- Start of processing for Unique_Fixed_Point_Type
10219 -- The operations on Duration are visible, so Duration is always a
10220 -- possible interpretation.
10222 T1 := Standard_Duration;
10224 -- Look for fixed-point types in enclosing scopes
10226 Scop := Current_Scope;
10227 while Scop /= Standard_Standard loop
10228 T2 := First_Entity (Scop);
10229 while Present (T2) loop
10230 if Is_Fixed_Point_Type (T2)
10231 and then Current_Entity (T2) = T2
10232 and then Scope (Base_Type (T2)) = Scop
10234 if Present (T1) then
10245 Scop := Scope (Scop);
10248 -- Look for visible fixed type declarations in the context
10250 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10251 while Present (Item) loop
10252 if Nkind (Item) = N_With_Clause then
10253 Scop := Entity (Name (Item));
10254 T2 := First_Entity (Scop);
10255 while Present (T2) loop
10256 if Is_Fixed_Point_Type (T2)
10257 and then Scope (Base_Type (T2)) = Scop
10258 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10260 if Present (T1) then
10275 if Nkind (N) = N_Real_Literal then
10276 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
10278 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
10282 end Unique_Fixed_Point_Type;
10284 ----------------------
10285 -- Valid_Conversion --
10286 ----------------------
10288 function Valid_Conversion
10290 Target : Entity_Id;
10292 Report_Errs : Boolean := True) return Boolean
10294 Target_Type : constant Entity_Id := Base_Type (Target);
10295 Opnd_Type : Entity_Id := Etype (Operand);
10297 function Conversion_Check
10299 Msg : String) return Boolean;
10300 -- Little routine to post Msg if Valid is False, returns Valid value
10302 -- The following are badly named, this kind of overloading is actively
10303 -- confusing in reading code, please rename to something like
10304 -- Error_Msg_N_If_Reporting ???
10306 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id);
10307 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10309 procedure Error_Msg_NE
10311 N : Node_Or_Entity_Id;
10312 E : Node_Or_Entity_Id);
10313 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10315 function Valid_Tagged_Conversion
10316 (Target_Type : Entity_Id;
10317 Opnd_Type : Entity_Id) return Boolean;
10318 -- Specifically test for validity of tagged conversions
10320 function Valid_Array_Conversion return Boolean;
10321 -- Check index and component conformance, and accessibility levels if
10322 -- the component types are anonymous access types (Ada 2005).
10324 ----------------------
10325 -- Conversion_Check --
10326 ----------------------
10328 function Conversion_Check
10330 Msg : String) return Boolean
10335 -- A generic unit has already been analyzed and we have verified
10336 -- that a particular conversion is OK in that context. Since the
10337 -- instance is reanalyzed without relying on the relationships
10338 -- established during the analysis of the generic, it is possible
10339 -- to end up with inconsistent views of private types. Do not emit
10340 -- the error message in such cases. The rest of the machinery in
10341 -- Valid_Conversion still ensures the proper compatibility of
10342 -- target and operand types.
10344 and then not In_Instance
10346 Error_Msg_N (Msg, Operand);
10350 end Conversion_Check;
10356 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id) is
10358 if Report_Errs then
10359 Errout.Error_Msg_N (Msg, N);
10367 procedure Error_Msg_NE
10369 N : Node_Or_Entity_Id;
10370 E : Node_Or_Entity_Id)
10373 if Report_Errs then
10374 Errout.Error_Msg_NE (Msg, N, E);
10378 ----------------------------
10379 -- Valid_Array_Conversion --
10380 ----------------------------
10382 function Valid_Array_Conversion return Boolean
10384 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10385 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10387 Opnd_Index : Node_Id;
10388 Opnd_Index_Type : Entity_Id;
10390 Target_Comp_Type : constant Entity_Id :=
10391 Component_Type (Target_Type);
10392 Target_Comp_Base : constant Entity_Id :=
10393 Base_Type (Target_Comp_Type);
10395 Target_Index : Node_Id;
10396 Target_Index_Type : Entity_Id;
10399 -- Error if wrong number of dimensions
10402 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10405 ("incompatible number of dimensions for conversion", Operand);
10408 -- Number of dimensions matches
10411 -- Loop through indexes of the two arrays
10413 Target_Index := First_Index (Target_Type);
10414 Opnd_Index := First_Index (Opnd_Type);
10415 while Present (Target_Index) and then Present (Opnd_Index) loop
10416 Target_Index_Type := Etype (Target_Index);
10417 Opnd_Index_Type := Etype (Opnd_Index);
10419 -- Error if index types are incompatible
10421 if not (Is_Integer_Type (Target_Index_Type)
10422 and then Is_Integer_Type (Opnd_Index_Type))
10423 and then (Root_Type (Target_Index_Type)
10424 /= Root_Type (Opnd_Index_Type))
10427 ("incompatible index types for array conversion",
10432 Next_Index (Target_Index);
10433 Next_Index (Opnd_Index);
10436 -- If component types have same base type, all set
10438 if Target_Comp_Base = Opnd_Comp_Base then
10441 -- Here if base types of components are not the same. The only
10442 -- time this is allowed is if we have anonymous access types.
10444 -- The conversion of arrays of anonymous access types can lead
10445 -- to dangling pointers. AI-392 formalizes the accessibility
10446 -- checks that must be applied to such conversions to prevent
10447 -- out-of-scope references.
10450 (Target_Comp_Base, E_Anonymous_Access_Type,
10451 E_Anonymous_Access_Subprogram_Type)
10452 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10454 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10456 if Type_Access_Level (Target_Type) <
10457 Deepest_Type_Access_Level (Opnd_Type)
10459 if In_Instance_Body then
10461 ("?source array type has " &
10462 "deeper accessibility level than target", Operand);
10464 ("\?Program_Error will be raised at run time",
10467 Make_Raise_Program_Error (Sloc (N),
10468 Reason => PE_Accessibility_Check_Failed));
10469 Set_Etype (N, Target_Type);
10472 -- Conversion not allowed because of accessibility levels
10476 ("source array type has " &
10477 "deeper accessibility level than target", Operand);
10485 -- All other cases where component base types do not match
10489 ("incompatible component types for array conversion",
10494 -- Check that component subtypes statically match. For numeric
10495 -- types this means that both must be either constrained or
10496 -- unconstrained. For enumeration types the bounds must match.
10497 -- All of this is checked in Subtypes_Statically_Match.
10499 if not Subtypes_Statically_Match
10500 (Target_Comp_Type, Opnd_Comp_Type)
10503 ("component subtypes must statically match", Operand);
10509 end Valid_Array_Conversion;
10511 -----------------------------
10512 -- Valid_Tagged_Conversion --
10513 -----------------------------
10515 function Valid_Tagged_Conversion
10516 (Target_Type : Entity_Id;
10517 Opnd_Type : Entity_Id) return Boolean
10520 -- Upward conversions are allowed (RM 4.6(22))
10522 if Covers (Target_Type, Opnd_Type)
10523 or else Is_Ancestor (Target_Type, Opnd_Type)
10527 -- Downward conversion are allowed if the operand is class-wide
10530 elsif Is_Class_Wide_Type (Opnd_Type)
10531 and then Covers (Opnd_Type, Target_Type)
10535 elsif Covers (Opnd_Type, Target_Type)
10536 or else Is_Ancestor (Opnd_Type, Target_Type)
10539 Conversion_Check (False,
10540 "downward conversion of tagged objects not allowed");
10542 -- Ada 2005 (AI-251): The conversion to/from interface types is
10545 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10548 -- If the operand is a class-wide type obtained through a limited_
10549 -- with clause, and the context includes the non-limited view, use
10550 -- it to determine whether the conversion is legal.
10552 elsif Is_Class_Wide_Type (Opnd_Type)
10553 and then From_With_Type (Opnd_Type)
10554 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10555 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10559 elsif Is_Access_Type (Opnd_Type)
10560 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10566 ("invalid tagged conversion, not compatible with}",
10567 N, First_Subtype (Opnd_Type));
10570 end Valid_Tagged_Conversion;
10572 -- Start of processing for Valid_Conversion
10575 Check_Parameterless_Call (Operand);
10577 if Is_Overloaded (Operand) then
10587 -- Remove procedure calls, which syntactically cannot appear in
10588 -- this context, but which cannot be removed by type checking,
10589 -- because the context does not impose a type.
10591 -- When compiling for VMS, spurious ambiguities can be produced
10592 -- when arithmetic operations have a literal operand and return
10593 -- System.Address or a descendant of it. These ambiguities are
10594 -- otherwise resolved by the context, but for conversions there
10595 -- is no context type and the removal of the spurious operations
10596 -- must be done explicitly here.
10598 -- The node may be labelled overloaded, but still contain only one
10599 -- interpretation because others were discarded earlier. If this
10600 -- is the case, retain the single interpretation if legal.
10602 Get_First_Interp (Operand, I, It);
10603 Opnd_Type := It.Typ;
10604 Get_Next_Interp (I, It);
10606 if Present (It.Typ)
10607 and then Opnd_Type /= Standard_Void_Type
10609 -- More than one candidate interpretation is available
10611 Get_First_Interp (Operand, I, It);
10612 while Present (It.Typ) loop
10613 if It.Typ = Standard_Void_Type then
10617 if Present (System_Aux_Id)
10618 and then Is_Descendent_Of_Address (It.Typ)
10623 Get_Next_Interp (I, It);
10627 Get_First_Interp (Operand, I, It);
10631 if No (It.Typ) then
10632 Error_Msg_N ("illegal operand in conversion", Operand);
10636 Get_Next_Interp (I, It);
10638 if Present (It.Typ) then
10641 It1 := Disambiguate (Operand, I1, I, Any_Type);
10643 if It1 = No_Interp then
10644 Error_Msg_N ("ambiguous operand in conversion", Operand);
10646 -- If the interpretation involves a standard operator, use
10647 -- the location of the type, which may be user-defined.
10649 if Sloc (It.Nam) = Standard_Location then
10650 Error_Msg_Sloc := Sloc (It.Typ);
10652 Error_Msg_Sloc := Sloc (It.Nam);
10655 Error_Msg_N -- CODEFIX
10656 ("\\possible interpretation#!", Operand);
10658 if Sloc (N1) = Standard_Location then
10659 Error_Msg_Sloc := Sloc (T1);
10661 Error_Msg_Sloc := Sloc (N1);
10664 Error_Msg_N -- CODEFIX
10665 ("\\possible interpretation#!", Operand);
10671 Set_Etype (Operand, It1.Typ);
10672 Opnd_Type := It1.Typ;
10678 if Is_Numeric_Type (Target_Type) then
10680 -- A universal fixed expression can be converted to any numeric type
10682 if Opnd_Type = Universal_Fixed then
10685 -- Also no need to check when in an instance or inlined body, because
10686 -- the legality has been established when the template was analyzed.
10687 -- Furthermore, numeric conversions may occur where only a private
10688 -- view of the operand type is visible at the instantiation point.
10689 -- This results in a spurious error if we check that the operand type
10690 -- is a numeric type.
10692 -- Note: in a previous version of this unit, the following tests were
10693 -- applied only for generated code (Comes_From_Source set to False),
10694 -- but in fact the test is required for source code as well, since
10695 -- this situation can arise in source code.
10697 elsif In_Instance or else In_Inlined_Body then
10700 -- Otherwise we need the conversion check
10703 return Conversion_Check
10704 (Is_Numeric_Type (Opnd_Type),
10705 "illegal operand for numeric conversion");
10710 elsif Is_Array_Type (Target_Type) then
10711 if not Is_Array_Type (Opnd_Type)
10712 or else Opnd_Type = Any_Composite
10713 or else Opnd_Type = Any_String
10715 Error_Msg_N ("illegal operand for array conversion", Operand);
10718 return Valid_Array_Conversion;
10721 -- Ada 2005 (AI-251): Anonymous access types where target references an
10724 elsif Ekind_In (Target_Type, E_General_Access_Type,
10725 E_Anonymous_Access_Type)
10726 and then Is_Interface (Directly_Designated_Type (Target_Type))
10728 -- Check the static accessibility rule of 4.6(17). Note that the
10729 -- check is not enforced when within an instance body, since the
10730 -- RM requires such cases to be caught at run time.
10732 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10733 if Type_Access_Level (Opnd_Type) >
10734 Deepest_Type_Access_Level (Target_Type)
10736 -- In an instance, this is a run-time check, but one we know
10737 -- will fail, so generate an appropriate warning. The raise
10738 -- will be generated by Expand_N_Type_Conversion.
10740 if In_Instance_Body then
10742 ("?cannot convert local pointer to non-local access type",
10745 ("\?Program_Error will be raised at run time", Operand);
10749 ("cannot convert local pointer to non-local access type",
10754 -- Special accessibility checks are needed in the case of access
10755 -- discriminants declared for a limited type.
10757 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10758 and then not Is_Local_Anonymous_Access (Opnd_Type)
10760 -- When the operand is a selected access discriminant the check
10761 -- needs to be made against the level of the object denoted by
10762 -- the prefix of the selected name (Object_Access_Level handles
10763 -- checking the prefix of the operand for this case).
10765 if Nkind (Operand) = N_Selected_Component
10766 and then Object_Access_Level (Operand) >
10767 Deepest_Type_Access_Level (Target_Type)
10769 -- In an instance, this is a run-time check, but one we know
10770 -- will fail, so generate an appropriate warning. The raise
10771 -- will be generated by Expand_N_Type_Conversion.
10773 if In_Instance_Body then
10775 ("?cannot convert access discriminant to non-local" &
10776 " access type", Operand);
10778 ("\?Program_Error will be raised at run time", Operand);
10781 ("cannot convert access discriminant to non-local" &
10782 " access type", Operand);
10787 -- The case of a reference to an access discriminant from
10788 -- within a limited type declaration (which will appear as
10789 -- a discriminal) is always illegal because the level of the
10790 -- discriminant is considered to be deeper than any (nameable)
10793 if Is_Entity_Name (Operand)
10794 and then not Is_Local_Anonymous_Access (Opnd_Type)
10796 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10797 and then Present (Discriminal_Link (Entity (Operand)))
10800 ("discriminant has deeper accessibility level than target",
10809 -- General and anonymous access types
10811 elsif Ekind_In (Target_Type, E_General_Access_Type,
10812 E_Anonymous_Access_Type)
10815 (Is_Access_Type (Opnd_Type)
10817 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10818 E_Access_Protected_Subprogram_Type),
10819 "must be an access-to-object type")
10821 if Is_Access_Constant (Opnd_Type)
10822 and then not Is_Access_Constant (Target_Type)
10825 ("access-to-constant operand type not allowed", Operand);
10829 -- Check the static accessibility rule of 4.6(17). Note that the
10830 -- check is not enforced when within an instance body, since the RM
10831 -- requires such cases to be caught at run time.
10833 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10834 or else Is_Local_Anonymous_Access (Target_Type)
10835 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
10836 N_Object_Declaration
10838 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
10839 -- conversions from an anonymous access type to a named general
10840 -- access type. Such conversions are not allowed in the case of
10841 -- access parameters and stand-alone objects of an anonymous
10842 -- access type. The implicit conversion case is recognized by
10843 -- testing that Comes_From_Source is False and that it's been
10844 -- rewritten. The Comes_From_Source test isn't sufficient because
10845 -- nodes in inlined calls to predefined library routines can have
10846 -- Comes_From_Source set to False. (Is there a better way to test
10847 -- for implicit conversions???)
10849 if Ada_Version >= Ada_2012
10850 and then not Comes_From_Source (N)
10851 and then N /= Original_Node (N)
10852 and then Ekind (Target_Type) = E_General_Access_Type
10853 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
10855 if Is_Itype (Opnd_Type) then
10857 -- Implicit conversions aren't allowed for objects of an
10858 -- anonymous access type, since such objects have nonstatic
10859 -- levels in Ada 2012.
10861 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
10862 N_Object_Declaration
10865 ("implicit conversion of stand-alone anonymous " &
10866 "access object not allowed", Operand);
10869 -- Implicit conversions aren't allowed for anonymous access
10870 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
10871 -- is done to exclude anonymous access results.
10873 elsif not Is_Local_Anonymous_Access (Opnd_Type)
10874 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
10875 N_Function_Specification,
10876 N_Procedure_Specification)
10879 ("implicit conversion of anonymous access formal " &
10880 "not allowed", Operand);
10883 -- This is a case where there's an enclosing object whose
10884 -- to which the "statically deeper than" relationship does
10885 -- not apply (such as an access discriminant selected from
10886 -- a dereference of an access parameter).
10888 elsif Object_Access_Level (Operand)
10889 = Scope_Depth (Standard_Standard)
10892 ("implicit conversion of anonymous access value " &
10893 "not allowed", Operand);
10896 -- In other cases, the level of the operand's type must be
10897 -- statically less deep than that of the target type, else
10898 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
10900 elsif Type_Access_Level (Opnd_Type) >
10901 Deepest_Type_Access_Level (Target_Type)
10904 ("implicit conversion of anonymous access value " &
10905 "violates accessibility", Operand);
10910 elsif Type_Access_Level (Opnd_Type) >
10911 Deepest_Type_Access_Level (Target_Type)
10913 -- In an instance, this is a run-time check, but one we know
10914 -- will fail, so generate an appropriate warning. The raise
10915 -- will be generated by Expand_N_Type_Conversion.
10917 if In_Instance_Body then
10919 ("?cannot convert local pointer to non-local access type",
10922 ("\?Program_Error will be raised at run time", Operand);
10925 -- Avoid generation of spurious error message
10927 if not Error_Posted (N) then
10929 ("cannot convert local pointer to non-local access type",
10936 -- Special accessibility checks are needed in the case of access
10937 -- discriminants declared for a limited type.
10939 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10940 and then not Is_Local_Anonymous_Access (Opnd_Type)
10942 -- When the operand is a selected access discriminant the check
10943 -- needs to be made against the level of the object denoted by
10944 -- the prefix of the selected name (Object_Access_Level handles
10945 -- checking the prefix of the operand for this case).
10947 if Nkind (Operand) = N_Selected_Component
10948 and then Object_Access_Level (Operand) >
10949 Deepest_Type_Access_Level (Target_Type)
10951 -- In an instance, this is a run-time check, but one we know
10952 -- will fail, so generate an appropriate warning. The raise
10953 -- will be generated by Expand_N_Type_Conversion.
10955 if In_Instance_Body then
10957 ("?cannot convert access discriminant to non-local" &
10958 " access type", Operand);
10960 ("\?Program_Error will be raised at run time",
10965 ("cannot convert access discriminant to non-local" &
10966 " access type", Operand);
10971 -- The case of a reference to an access discriminant from
10972 -- within a limited type declaration (which will appear as
10973 -- a discriminal) is always illegal because the level of the
10974 -- discriminant is considered to be deeper than any (nameable)
10977 if Is_Entity_Name (Operand)
10979 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10980 and then Present (Discriminal_Link (Entity (Operand)))
10983 ("discriminant has deeper accessibility level than target",
10990 -- In the presence of limited_with clauses we have to use non-limited
10991 -- views, if available.
10993 Check_Limited : declare
10994 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10995 -- Helper function to handle limited views
10997 --------------------------
10998 -- Full_Designated_Type --
10999 --------------------------
11001 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
11002 Desig : constant Entity_Id := Designated_Type (T);
11005 -- Handle the limited view of a type
11007 if Is_Incomplete_Type (Desig)
11008 and then From_With_Type (Desig)
11009 and then Present (Non_Limited_View (Desig))
11011 return Available_View (Desig);
11015 end Full_Designated_Type;
11017 -- Local Declarations
11019 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
11020 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
11022 Same_Base : constant Boolean :=
11023 Base_Type (Target) = Base_Type (Opnd);
11025 -- Start of processing for Check_Limited
11028 if Is_Tagged_Type (Target) then
11029 return Valid_Tagged_Conversion (Target, Opnd);
11032 if not Same_Base then
11034 ("target designated type not compatible with }",
11035 N, Base_Type (Opnd));
11038 -- Ada 2005 AI-384: legality rule is symmetric in both
11039 -- designated types. The conversion is legal (with possible
11040 -- constraint check) if either designated type is
11043 elsif Subtypes_Statically_Match (Target, Opnd)
11045 (Has_Discriminants (Target)
11047 (not Is_Constrained (Opnd)
11048 or else not Is_Constrained (Target)))
11050 -- Special case, if Value_Size has been used to make the
11051 -- sizes different, the conversion is not allowed even
11052 -- though the subtypes statically match.
11054 if Known_Static_RM_Size (Target)
11055 and then Known_Static_RM_Size (Opnd)
11056 and then RM_Size (Target) /= RM_Size (Opnd)
11059 ("target designated subtype not compatible with }",
11062 ("\because sizes of the two designated subtypes differ",
11066 -- Normal case where conversion is allowed
11074 ("target designated subtype not compatible with }",
11081 -- Access to subprogram types. If the operand is an access parameter,
11082 -- the type has a deeper accessibility that any master, and cannot be
11083 -- assigned. We must make an exception if the conversion is part of an
11084 -- assignment and the target is the return object of an extended return
11085 -- statement, because in that case the accessibility check takes place
11086 -- after the return.
11088 elsif Is_Access_Subprogram_Type (Target_Type)
11089 and then No (Corresponding_Remote_Type (Opnd_Type))
11091 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
11092 and then Is_Entity_Name (Operand)
11093 and then Ekind (Entity (Operand)) = E_In_Parameter
11095 (Nkind (Parent (N)) /= N_Assignment_Statement
11096 or else not Is_Entity_Name (Name (Parent (N)))
11097 or else not Is_Return_Object (Entity (Name (Parent (N)))))
11100 ("illegal attempt to store anonymous access to subprogram",
11103 ("\value has deeper accessibility than any master " &
11104 "(RM 3.10.2 (13))",
11108 ("\use named access type for& instead of access parameter",
11109 Operand, Entity (Operand));
11112 -- Check that the designated types are subtype conformant
11114 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
11115 Old_Id => Designated_Type (Opnd_Type),
11118 -- Check the static accessibility rule of 4.6(20)
11120 if Type_Access_Level (Opnd_Type) >
11121 Deepest_Type_Access_Level (Target_Type)
11124 ("operand type has deeper accessibility level than target",
11127 -- Check that if the operand type is declared in a generic body,
11128 -- then the target type must be declared within that same body
11129 -- (enforces last sentence of 4.6(20)).
11131 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
11133 O_Gen : constant Node_Id :=
11134 Enclosing_Generic_Body (Opnd_Type);
11139 T_Gen := Enclosing_Generic_Body (Target_Type);
11140 while Present (T_Gen) and then T_Gen /= O_Gen loop
11141 T_Gen := Enclosing_Generic_Body (T_Gen);
11144 if T_Gen /= O_Gen then
11146 ("target type must be declared in same generic body"
11147 & " as operand type", N);
11154 -- Remote subprogram access types
11156 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
11157 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
11159 -- It is valid to convert from one RAS type to another provided
11160 -- that their specification statically match.
11162 Check_Subtype_Conformant
11164 Designated_Type (Corresponding_Remote_Type (Target_Type)),
11166 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
11171 -- If it was legal in the generic, it's legal in the instance
11173 elsif In_Instance_Body then
11176 -- If both are tagged types, check legality of view conversions
11178 elsif Is_Tagged_Type (Target_Type)
11180 Is_Tagged_Type (Opnd_Type)
11182 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
11184 -- Types derived from the same root type are convertible
11186 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
11189 -- In an instance or an inlined body, there may be inconsistent views of
11190 -- the same type, or of types derived from a common root.
11192 elsif (In_Instance or In_Inlined_Body)
11194 Root_Type (Underlying_Type (Target_Type)) =
11195 Root_Type (Underlying_Type (Opnd_Type))
11199 -- Special check for common access type error case
11201 elsif Ekind (Target_Type) = E_Access_Type
11202 and then Is_Access_Type (Opnd_Type)
11204 Error_Msg_N ("target type must be general access type!", N);
11205 Error_Msg_NE -- CODEFIX
11206 ("add ALL to }!", N, Target_Type);
11210 Error_Msg_NE ("invalid conversion, not compatible with }",
11214 end Valid_Conversion;