1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Output; use Output;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Elab; use Sem_Elab;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Intr; use Sem_Intr;
66 with Sem_Util; use Sem_Util;
67 with Targparm; use Targparm;
68 with Sem_Type; use Sem_Type;
69 with Sem_Warn; use Sem_Warn;
70 with Sinfo; use Sinfo;
71 with Sinfo.CN; use Sinfo.CN;
72 with Snames; use Snames;
73 with Stand; use Stand;
74 with Stringt; use Stringt;
75 with Style; use Style;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
80 package body Sem_Res is
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
86 -- Second pass (top-down) type checking and overload resolution procedures
87 -- Typ is the type required by context. These procedures propagate the type
88 -- information recursively to the descendants of N. If the node is not
89 -- overloaded, its Etype is established in the first pass. If overloaded,
90 -- the Resolve routines set the correct type. For arith. operators, the
91 -- Etype is the base type of the context.
93 -- Note that Resolve_Attribute is separated off in Sem_Attr
95 function Bad_Unordered_Enumeration_Reference
97 T : Entity_Id) return Boolean;
98 -- Node N contains a potentially dubious reference to type T, either an
99 -- explicit comparison, or an explicit range. This function returns True
100 -- if the type T is an enumeration type for which No pragma Order has been
101 -- given, and the reference N is not in the same extended source unit as
102 -- the declaration of T.
104 procedure Check_Discriminant_Use (N : Node_Id);
105 -- Enforce the restrictions on the use of discriminants when constraining
106 -- a component of a discriminated type (record or concurrent type).
108 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
109 -- Given a node for an operator associated with type T, check that
110 -- the operator is visible. Operators all of whose operands are
111 -- universal must be checked for visibility during resolution
112 -- because their type is not determinable based on their operands.
114 procedure Check_Fully_Declared_Prefix
117 -- Check that the type of the prefix of a dereference is not incomplete
119 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
120 -- Given a call node, N, which is known to occur immediately within the
121 -- subprogram being called, determines whether it is a detectable case of
122 -- an infinite recursion, and if so, outputs appropriate messages. Returns
123 -- True if an infinite recursion is detected, and False otherwise.
125 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
126 -- If the type of the object being initialized uses the secondary stack
127 -- directly or indirectly, create a transient scope for the call to the
128 -- init proc. This is because we do not create transient scopes for the
129 -- initialization of individual components within the init proc itself.
130 -- Could be optimized away perhaps?
132 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
133 -- N is the node for a logical operator. If the operator is predefined, and
134 -- the root type of the operands is Standard.Boolean, then a check is made
135 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
136 -- the style check for Style_Check_Boolean_And_Or.
138 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
139 -- Determine whether E is an access type declared by an access declaration,
140 -- and not an (anonymous) allocator type.
142 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
143 -- Utility to check whether the entity for an operator is a predefined
144 -- operator, in which case the expression is left as an operator in the
145 -- tree (else it is rewritten into a call). An instance of an intrinsic
146 -- conversion operation may be given an operator name, but is not treated
147 -- like an operator. Note that an operator that is an imported back-end
148 -- builtin has convention Intrinsic, but is expected to be rewritten into
149 -- a call, so such an operator is not treated as predefined by this
152 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
153 -- If a default expression in entry call N depends on the discriminants
154 -- of the task, it must be replaced with a reference to the discriminant
155 -- of the task being called.
157 procedure Resolve_Op_Concat_Arg
162 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
163 -- concatenation operator. The operand is either of the array type or of
164 -- the component type. If the operand is an aggregate, and the component
165 -- type is composite, this is ambiguous if component type has aggregates.
167 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
168 -- Does the first part of the work of Resolve_Op_Concat
170 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
171 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
172 -- has been resolved. See Resolve_Op_Concat for details.
174 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
210 function Operator_Kind
212 Is_Binary : Boolean) return Node_Kind;
213 -- Utility to map the name of an operator into the corresponding Node. Used
214 -- by other node rewriting procedures.
216 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
217 -- Resolve actuals of call, and add default expressions for missing ones.
218 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
219 -- called subprogram.
221 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
222 -- Called from Resolve_Call, when the prefix denotes an entry or element
223 -- of entry family. Actuals are resolved as for subprograms, and the node
224 -- is rebuilt as an entry call. Also called for protected operations. Typ
225 -- is the context type, which is used when the operation is a protected
226 -- function with no arguments, and the return value is indexed.
228 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
229 -- A call to a user-defined intrinsic operator is rewritten as a call to
230 -- the corresponding predefined operator, with suitable conversions. Note
231 -- that this applies only for intrinsic operators that denote predefined
232 -- operators, not ones that are intrinsic imports of back-end builtins.
234 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
235 -- Ditto, for unary operators (arithmetic ones and "not" on signed
236 -- integer types for VMS).
238 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
239 -- If an operator node resolves to a call to a user-defined operator,
240 -- rewrite the node as a function call.
242 procedure Make_Call_Into_Operator
246 -- Inverse transformation: if an operator is given in functional notation,
247 -- then after resolving the node, transform into an operator node, so
248 -- that operands are resolved properly. Recall that predefined operators
249 -- do not have a full signature and special resolution rules apply.
251 procedure Rewrite_Renamed_Operator
255 -- An operator can rename another, e.g. in an instantiation. In that
256 -- case, the proper operator node must be constructed and resolved.
258 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
259 -- The String_Literal_Subtype is built for all strings that are not
260 -- operands of a static concatenation operation. If the argument is
261 -- not a N_String_Literal node, then the call has no effect.
263 procedure Set_Slice_Subtype (N : Node_Id);
264 -- Build subtype of array type, with the range specified by the slice
266 procedure Simplify_Type_Conversion (N : Node_Id);
267 -- Called after N has been resolved and evaluated, but before range checks
268 -- have been applied. Currently simplifies a combination of floating-point
269 -- to integer conversion and Truncation attribute.
271 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
272 -- A universal_fixed expression in an universal context is unambiguous if
273 -- there is only one applicable fixed point type. Determining whether there
274 -- is only one requires a search over all visible entities, and happens
275 -- only in very pathological cases (see 6115-006).
277 -------------------------
278 -- Ambiguous_Character --
279 -------------------------
281 procedure Ambiguous_Character (C : Node_Id) is
285 if Nkind (C) = N_Character_Literal then
286 Error_Msg_N ("ambiguous character literal", C);
288 -- First the ones in Standard
290 Error_Msg_N ("\\possible interpretation: Character!", C);
291 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
293 -- Include Wide_Wide_Character in Ada 2005 mode
295 if Ada_Version >= Ada_2005 then
296 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
299 -- Now any other types that match
301 E := Current_Entity (C);
302 while Present (E) loop
303 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
307 end Ambiguous_Character;
309 -------------------------
310 -- Analyze_And_Resolve --
311 -------------------------
313 procedure Analyze_And_Resolve (N : Node_Id) is
317 end Analyze_And_Resolve;
319 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
323 end Analyze_And_Resolve;
325 -- Version withs check(s) suppressed
327 procedure Analyze_And_Resolve
332 Scop : constant Entity_Id := Current_Scope;
335 if Suppress = All_Checks then
337 Svg : constant Suppress_Array := Scope_Suppress;
339 Scope_Suppress := (others => True);
340 Analyze_And_Resolve (N, Typ);
341 Scope_Suppress := Svg;
346 Svg : constant Boolean := Scope_Suppress (Suppress);
349 Scope_Suppress (Suppress) := True;
350 Analyze_And_Resolve (N, Typ);
351 Scope_Suppress (Suppress) := Svg;
355 if Current_Scope /= Scop
356 and then Scope_Is_Transient
358 -- This can only happen if a transient scope was created for an inner
359 -- expression, which will be removed upon completion of the analysis
360 -- of an enclosing construct. The transient scope must have the
361 -- suppress status of the enclosing environment, not of this Analyze
364 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
367 end Analyze_And_Resolve;
369 procedure Analyze_And_Resolve
373 Scop : constant Entity_Id := Current_Scope;
376 if Suppress = All_Checks then
378 Svg : constant Suppress_Array := Scope_Suppress;
380 Scope_Suppress := (others => True);
381 Analyze_And_Resolve (N);
382 Scope_Suppress := Svg;
387 Svg : constant Boolean := Scope_Suppress (Suppress);
390 Scope_Suppress (Suppress) := True;
391 Analyze_And_Resolve (N);
392 Scope_Suppress (Suppress) := Svg;
396 if Current_Scope /= Scop
397 and then Scope_Is_Transient
399 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
402 end Analyze_And_Resolve;
404 ----------------------------------------
405 -- Bad_Unordered_Enumeration_Reference --
406 ----------------------------------------
408 function Bad_Unordered_Enumeration_Reference
410 T : Entity_Id) return Boolean
413 return Is_Enumeration_Type (T)
414 and then Comes_From_Source (N)
415 and then Warn_On_Unordered_Enumeration_Type
416 and then not Has_Pragma_Ordered (T)
417 and then not In_Same_Extended_Unit (N, T);
418 end Bad_Unordered_Enumeration_Reference;
420 ----------------------------
421 -- Check_Discriminant_Use --
422 ----------------------------
424 procedure Check_Discriminant_Use (N : Node_Id) is
425 PN : constant Node_Id := Parent (N);
426 Disc : constant Entity_Id := Entity (N);
431 -- Any use in a spec-expression is legal
433 if In_Spec_Expression then
436 elsif Nkind (PN) = N_Range then
438 -- Discriminant cannot be used to constrain a scalar type
442 if Nkind (P) = N_Range_Constraint
443 and then Nkind (Parent (P)) = N_Subtype_Indication
444 and then Nkind (Parent (Parent (P))) = N_Component_Definition
446 Error_Msg_N ("discriminant cannot constrain scalar type", N);
448 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
450 -- The following check catches the unusual case where a
451 -- discriminant appears within an index constraint that is part of
452 -- a larger expression within a constraint on a component, e.g. "C
453 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
454 -- of record components, and note that a similar check should also
455 -- apply in the case of discriminant constraints below. ???
457 -- Note that the check for N_Subtype_Declaration below is to
458 -- detect the valid use of discriminants in the constraints of a
459 -- subtype declaration when this subtype declaration appears
460 -- inside the scope of a record type (which is syntactically
461 -- illegal, but which may be created as part of derived type
462 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
465 if Ekind (Current_Scope) = E_Record_Type
466 and then Scope (Disc) = Current_Scope
468 (Nkind (Parent (P)) = N_Subtype_Indication
470 Nkind_In (Parent (Parent (P)), N_Component_Definition,
471 N_Subtype_Declaration)
472 and then Paren_Count (N) = 0)
475 ("discriminant must appear alone in component constraint", N);
479 -- Detect a common error:
481 -- type R (D : Positive := 100) is record
482 -- Name : String (1 .. D);
485 -- The default value causes an object of type R to be allocated
486 -- with room for Positive'Last characters. The RM does not mandate
487 -- the allocation of the maximum size, but that is what GNAT does
488 -- so we should warn the programmer that there is a problem.
490 Check_Large : declare
496 function Large_Storage_Type (T : Entity_Id) return Boolean;
497 -- Return True if type T has a large enough range that any
498 -- array whose index type covered the whole range of the type
499 -- would likely raise Storage_Error.
501 ------------------------
502 -- Large_Storage_Type --
503 ------------------------
505 function Large_Storage_Type (T : Entity_Id) return Boolean is
507 -- The type is considered large if its bounds are known at
508 -- compile time and if it requires at least as many bits as
509 -- a Positive to store the possible values.
511 return Compile_Time_Known_Value (Type_Low_Bound (T))
512 and then Compile_Time_Known_Value (Type_High_Bound (T))
514 Minimum_Size (T, Biased => True) >=
515 RM_Size (Standard_Positive);
516 end Large_Storage_Type;
518 -- Start of processing for Check_Large
521 -- Check that the Disc has a large range
523 if not Large_Storage_Type (Etype (Disc)) then
527 -- If the enclosing type is limited, we allocate only the
528 -- default value, not the maximum, and there is no need for
531 if Is_Limited_Type (Scope (Disc)) then
535 -- Check that it is the high bound
537 if N /= High_Bound (PN)
538 or else No (Discriminant_Default_Value (Disc))
543 -- Check the array allows a large range at this bound. First
548 if Nkind (SI) /= N_Subtype_Indication then
552 T := Entity (Subtype_Mark (SI));
554 if not Is_Array_Type (T) then
558 -- Next, find the dimension
560 TB := First_Index (T);
561 CB := First (Constraints (P));
563 and then Present (TB)
564 and then Present (CB)
575 -- Now, check the dimension has a large range
577 if not Large_Storage_Type (Etype (TB)) then
581 -- Warn about the danger
584 ("?creation of & object may raise Storage_Error!",
593 -- Legal case is in index or discriminant constraint
595 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
596 N_Discriminant_Association)
598 if Paren_Count (N) > 0 then
600 ("discriminant in constraint must appear alone", N);
602 elsif Nkind (N) = N_Expanded_Name
603 and then Comes_From_Source (N)
606 ("discriminant must appear alone as a direct name", N);
611 -- Otherwise, context is an expression. It should not be within (i.e. a
612 -- subexpression of) a constraint for a component.
617 while not Nkind_In (P, N_Component_Declaration,
618 N_Subtype_Indication,
626 -- If the discriminant is used in an expression that is a bound of a
627 -- scalar type, an Itype is created and the bounds are attached to
628 -- its range, not to the original subtype indication. Such use is of
629 -- course a double fault.
631 if (Nkind (P) = N_Subtype_Indication
632 and then Nkind_In (Parent (P), N_Component_Definition,
633 N_Derived_Type_Definition)
634 and then D = Constraint (P))
636 -- The constraint itself may be given by a subtype indication,
637 -- rather than by a more common discrete range.
639 or else (Nkind (P) = N_Subtype_Indication
641 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
642 or else Nkind (P) = N_Entry_Declaration
643 or else Nkind (D) = N_Defining_Identifier
646 ("discriminant in constraint must appear alone", N);
649 end Check_Discriminant_Use;
651 --------------------------------
652 -- Check_For_Visible_Operator --
653 --------------------------------
655 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
657 if Is_Invisible_Operator (N, T) then
658 Error_Msg_NE -- CODEFIX
659 ("operator for} is not directly visible!", N, First_Subtype (T));
660 Error_Msg_N -- CODEFIX
661 ("use clause would make operation legal!", N);
663 end Check_For_Visible_Operator;
665 ----------------------------------
666 -- Check_Fully_Declared_Prefix --
667 ----------------------------------
669 procedure Check_Fully_Declared_Prefix
674 -- Check that the designated type of the prefix of a dereference is
675 -- not an incomplete type. This cannot be done unconditionally, because
676 -- dereferences of private types are legal in default expressions. This
677 -- case is taken care of in Check_Fully_Declared, called below. There
678 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
680 -- This consideration also applies to similar checks for allocators,
681 -- qualified expressions, and type conversions.
683 -- An additional exception concerns other per-object expressions that
684 -- are not directly related to component declarations, in particular
685 -- representation pragmas for tasks. These will be per-object
686 -- expressions if they depend on discriminants or some global entity.
687 -- If the task has access discriminants, the designated type may be
688 -- incomplete at the point the expression is resolved. This resolution
689 -- takes place within the body of the initialization procedure, where
690 -- the discriminant is replaced by its discriminal.
692 if Is_Entity_Name (Pref)
693 and then Ekind (Entity (Pref)) = E_In_Parameter
697 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
698 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
699 -- Analyze_Object_Renaming, and Freeze_Entity.
701 elsif Ada_Version >= Ada_2005
702 and then Is_Entity_Name (Pref)
703 and then Is_Access_Type (Etype (Pref))
704 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
706 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
710 Check_Fully_Declared (Typ, Parent (Pref));
712 end Check_Fully_Declared_Prefix;
714 ------------------------------
715 -- Check_Infinite_Recursion --
716 ------------------------------
718 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
722 function Same_Argument_List return Boolean;
723 -- Check whether list of actuals is identical to list of formals of
724 -- called function (which is also the enclosing scope).
726 ------------------------
727 -- Same_Argument_List --
728 ------------------------
730 function Same_Argument_List return Boolean is
736 if not Is_Entity_Name (Name (N)) then
739 Subp := Entity (Name (N));
742 F := First_Formal (Subp);
743 A := First_Actual (N);
744 while Present (F) and then Present (A) loop
745 if not Is_Entity_Name (A)
746 or else Entity (A) /= F
756 end Same_Argument_List;
758 -- Start of processing for Check_Infinite_Recursion
761 -- Special case, if this is a procedure call and is a call to the
762 -- current procedure with the same argument list, then this is for
763 -- sure an infinite recursion and we insert a call to raise SE.
765 if Is_List_Member (N)
766 and then List_Length (List_Containing (N)) = 1
767 and then Same_Argument_List
770 P : constant Node_Id := Parent (N);
772 if Nkind (P) = N_Handled_Sequence_Of_Statements
773 and then Nkind (Parent (P)) = N_Subprogram_Body
774 and then Is_Empty_List (Declarations (Parent (P)))
776 Error_Msg_N ("!?infinite recursion", N);
777 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
779 Make_Raise_Storage_Error (Sloc (N),
780 Reason => SE_Infinite_Recursion));
786 -- If not that special case, search up tree, quitting if we reach a
787 -- construct (e.g. a conditional) that tells us that this is not a
788 -- case for an infinite recursion warning.
794 -- If no parent, then we were not inside a subprogram, this can for
795 -- example happen when processing certain pragmas in a spec. Just
796 -- return False in this case.
802 -- Done if we get to subprogram body, this is definitely an infinite
803 -- recursion case if we did not find anything to stop us.
805 exit when Nkind (P) = N_Subprogram_Body;
807 -- If appearing in conditional, result is false
809 if Nkind_In (P, N_Or_Else,
813 N_Conditional_Expression,
818 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
819 and then C /= First (Statements (P))
821 -- If the call is the expression of a return statement and the
822 -- actuals are identical to the formals, it's worth a warning.
823 -- However, we skip this if there is an immediately preceding
824 -- raise statement, since the call is never executed.
826 -- Furthermore, this corresponds to a common idiom:
828 -- function F (L : Thing) return Boolean is
830 -- raise Program_Error;
834 -- for generating a stub function
836 if Nkind (Parent (N)) = N_Simple_Return_Statement
837 and then Same_Argument_List
839 exit when not Is_List_Member (Parent (N));
841 -- OK, return statement is in a statement list, look for raise
847 -- Skip past N_Freeze_Entity nodes generated by expansion
849 Nod := Prev (Parent (N));
851 and then Nkind (Nod) = N_Freeze_Entity
856 -- If no raise statement, give warning
858 exit when Nkind (Nod) /= N_Raise_Statement
860 (Nkind (Nod) not in N_Raise_xxx_Error
861 or else Present (Condition (Nod)));
872 Error_Msg_N ("!?possible infinite recursion", N);
873 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
876 end Check_Infinite_Recursion;
878 -------------------------------
879 -- Check_Initialization_Call --
880 -------------------------------
882 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
883 Typ : constant Entity_Id := Etype (First_Formal (Nam));
885 function Uses_SS (T : Entity_Id) return Boolean;
886 -- Check whether the creation of an object of the type will involve
887 -- use of the secondary stack. If T is a record type, this is true
888 -- if the expression for some component uses the secondary stack, e.g.
889 -- through a call to a function that returns an unconstrained value.
890 -- False if T is controlled, because cleanups occur elsewhere.
896 function Uses_SS (T : Entity_Id) return Boolean is
899 Full_Type : Entity_Id := Underlying_Type (T);
902 -- Normally we want to use the underlying type, but if it's not set
903 -- then continue with T.
905 if not Present (Full_Type) then
909 if Is_Controlled (Full_Type) then
912 elsif Is_Array_Type (Full_Type) then
913 return Uses_SS (Component_Type (Full_Type));
915 elsif Is_Record_Type (Full_Type) then
916 Comp := First_Component (Full_Type);
917 while Present (Comp) loop
918 if Ekind (Comp) = E_Component
919 and then Nkind (Parent (Comp)) = N_Component_Declaration
921 -- The expression for a dynamic component may be rewritten
922 -- as a dereference, so retrieve original node.
924 Expr := Original_Node (Expression (Parent (Comp)));
926 -- Return True if the expression is a call to a function
927 -- (including an attribute function such as Image, or a
928 -- user-defined operator) with a result that requires a
931 if (Nkind (Expr) = N_Function_Call
932 or else Nkind (Expr) in N_Op
933 or else (Nkind (Expr) = N_Attribute_Reference
934 and then Present (Expressions (Expr))))
935 and then Requires_Transient_Scope (Etype (Expr))
939 elsif Uses_SS (Etype (Comp)) then
944 Next_Component (Comp);
954 -- Start of processing for Check_Initialization_Call
957 -- Establish a transient scope if the type needs it
959 if Uses_SS (Typ) then
960 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
962 end Check_Initialization_Call;
964 ---------------------------------------
965 -- Check_No_Direct_Boolean_Operators --
966 ---------------------------------------
968 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
970 if Scope (Entity (N)) = Standard_Standard
971 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
973 -- Restriction only applies to original source code
975 if Comes_From_Source (N) then
976 Check_Restriction (No_Direct_Boolean_Operators, N);
981 Check_Boolean_Operator (N);
983 end Check_No_Direct_Boolean_Operators;
985 ------------------------------
986 -- Check_Parameterless_Call --
987 ------------------------------
989 procedure Check_Parameterless_Call (N : Node_Id) is
992 function Prefix_Is_Access_Subp return Boolean;
993 -- If the prefix is of an access_to_subprogram type, the node must be
994 -- rewritten as a call. Ditto if the prefix is overloaded and all its
995 -- interpretations are access to subprograms.
997 ---------------------------
998 -- Prefix_Is_Access_Subp --
999 ---------------------------
1001 function Prefix_Is_Access_Subp return Boolean is
1006 -- If the context is an attribute reference that can apply to
1007 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1009 if Nkind (Parent (N)) = N_Attribute_Reference
1010 and then (Attribute_Name (Parent (N)) = Name_Address or else
1011 Attribute_Name (Parent (N)) = Name_Code_Address or else
1012 Attribute_Name (Parent (N)) = Name_Access)
1017 if not Is_Overloaded (N) then
1019 Ekind (Etype (N)) = E_Subprogram_Type
1020 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1022 Get_First_Interp (N, I, It);
1023 while Present (It.Typ) loop
1024 if Ekind (It.Typ) /= E_Subprogram_Type
1025 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1030 Get_Next_Interp (I, It);
1035 end Prefix_Is_Access_Subp;
1037 -- Start of processing for Check_Parameterless_Call
1040 -- Defend against junk stuff if errors already detected
1042 if Total_Errors_Detected /= 0 then
1043 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1045 elsif Nkind (N) in N_Has_Chars
1046 and then Chars (N) in Error_Name_Or_No_Name
1054 -- If the context expects a value, and the name is a procedure, this is
1055 -- most likely a missing 'Access. Don't try to resolve the parameterless
1056 -- call, error will be caught when the outer call is analyzed.
1058 if Is_Entity_Name (N)
1059 and then Ekind (Entity (N)) = E_Procedure
1060 and then not Is_Overloaded (N)
1062 Nkind_In (Parent (N), N_Parameter_Association,
1064 N_Procedure_Call_Statement)
1069 -- Rewrite as call if overloadable entity that is (or could be, in the
1070 -- overloaded case) a function call. If we know for sure that the entity
1071 -- is an enumeration literal, we do not rewrite it.
1073 -- If the entity is the name of an operator, it cannot be a call because
1074 -- operators cannot have default parameters. In this case, this must be
1075 -- a string whose contents coincide with an operator name. Set the kind
1076 -- of the node appropriately.
1078 if (Is_Entity_Name (N)
1079 and then Nkind (N) /= N_Operator_Symbol
1080 and then Is_Overloadable (Entity (N))
1081 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1082 or else Is_Overloaded (N)))
1084 -- Rewrite as call if it is an explicit dereference of an expression of
1085 -- a subprogram access type, and the subprogram type is not that of a
1086 -- procedure or entry.
1089 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1091 -- Rewrite as call if it is a selected component which is a function,
1092 -- this is the case of a call to a protected function (which may be
1093 -- overloaded with other protected operations).
1096 (Nkind (N) = N_Selected_Component
1097 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1099 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1101 and then Is_Overloaded (Selector_Name (N)))))
1103 -- If one of the above three conditions is met, rewrite as call. Apply
1104 -- the rewriting only once.
1107 if Nkind (Parent (N)) /= N_Function_Call
1108 or else N /= Name (Parent (N))
1111 -- This may be a prefixed call that was not fully analyzed, e.g.
1112 -- an actual in an instance.
1114 if Ada_Version >= Ada_2005
1115 and then Nkind (N) = N_Selected_Component
1116 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1118 Analyze_Selected_Component (N);
1120 if Nkind (N) /= N_Selected_Component then
1125 Nam := New_Copy (N);
1127 -- If overloaded, overload set belongs to new copy
1129 Save_Interps (N, Nam);
1131 -- Change node to parameterless function call (note that the
1132 -- Parameter_Associations associations field is left set to Empty,
1133 -- its normal default value since there are no parameters)
1135 Change_Node (N, N_Function_Call);
1137 Set_Sloc (N, Sloc (Nam));
1141 elsif Nkind (N) = N_Parameter_Association then
1142 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1144 elsif Nkind (N) = N_Operator_Symbol then
1145 Change_Operator_Symbol_To_String_Literal (N);
1146 Set_Is_Overloaded (N, False);
1147 Set_Etype (N, Any_String);
1149 end Check_Parameterless_Call;
1151 -----------------------------
1152 -- Is_Definite_Access_Type --
1153 -----------------------------
1155 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1156 Btyp : constant Entity_Id := Base_Type (E);
1158 return Ekind (Btyp) = E_Access_Type
1159 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1160 and then Comes_From_Source (Btyp));
1161 end Is_Definite_Access_Type;
1163 ----------------------
1164 -- Is_Predefined_Op --
1165 ----------------------
1167 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1169 -- Predefined operators are intrinsic subprograms
1171 if not Is_Intrinsic_Subprogram (Nam) then
1175 -- A call to a back-end builtin is never a predefined operator
1177 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1181 return not Is_Generic_Instance (Nam)
1182 and then Chars (Nam) in Any_Operator_Name
1183 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1184 end Is_Predefined_Op;
1186 -----------------------------
1187 -- Make_Call_Into_Operator --
1188 -----------------------------
1190 procedure Make_Call_Into_Operator
1195 Op_Name : constant Name_Id := Chars (Op_Id);
1196 Act1 : Node_Id := First_Actual (N);
1197 Act2 : Node_Id := Next_Actual (Act1);
1198 Error : Boolean := False;
1199 Func : constant Entity_Id := Entity (Name (N));
1200 Is_Binary : constant Boolean := Present (Act2);
1202 Opnd_Type : Entity_Id;
1203 Orig_Type : Entity_Id := Empty;
1206 type Kind_Test is access function (E : Entity_Id) return Boolean;
1208 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1209 -- If the operand is not universal, and the operator is given by an
1210 -- expanded name, verify that the operand has an interpretation with a
1211 -- type defined in the given scope of the operator.
1213 function Type_In_P (Test : Kind_Test) return Entity_Id;
1214 -- Find a type of the given class in package Pack that contains the
1217 ---------------------------
1218 -- Operand_Type_In_Scope --
1219 ---------------------------
1221 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1222 Nod : constant Node_Id := Right_Opnd (Op_Node);
1227 if not Is_Overloaded (Nod) then
1228 return Scope (Base_Type (Etype (Nod))) = S;
1231 Get_First_Interp (Nod, I, It);
1232 while Present (It.Typ) loop
1233 if Scope (Base_Type (It.Typ)) = S then
1237 Get_Next_Interp (I, It);
1242 end Operand_Type_In_Scope;
1248 function Type_In_P (Test : Kind_Test) return Entity_Id is
1251 function In_Decl return Boolean;
1252 -- Verify that node is not part of the type declaration for the
1253 -- candidate type, which would otherwise be invisible.
1259 function In_Decl return Boolean is
1260 Decl_Node : constant Node_Id := Parent (E);
1266 if Etype (E) = Any_Type then
1269 elsif No (Decl_Node) then
1274 and then Nkind (N2) /= N_Compilation_Unit
1276 if N2 = Decl_Node then
1287 -- Start of processing for Type_In_P
1290 -- If the context type is declared in the prefix package, this is the
1291 -- desired base type.
1293 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1294 return Base_Type (Typ);
1297 E := First_Entity (Pack);
1298 while Present (E) loop
1300 and then not In_Decl
1312 -- Start of processing for Make_Call_Into_Operator
1315 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1320 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1321 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1322 Save_Interps (Act1, Left_Opnd (Op_Node));
1323 Save_Interps (Act2, Right_Opnd (Op_Node));
1324 Act1 := Left_Opnd (Op_Node);
1325 Act2 := Right_Opnd (Op_Node);
1330 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1331 Save_Interps (Act1, Right_Opnd (Op_Node));
1332 Act1 := Right_Opnd (Op_Node);
1335 -- If the operator is denoted by an expanded name, and the prefix is
1336 -- not Standard, but the operator is a predefined one whose scope is
1337 -- Standard, then this is an implicit_operator, inserted as an
1338 -- interpretation by the procedure of the same name. This procedure
1339 -- overestimates the presence of implicit operators, because it does
1340 -- not examine the type of the operands. Verify now that the operand
1341 -- type appears in the given scope. If right operand is universal,
1342 -- check the other operand. In the case of concatenation, either
1343 -- argument can be the component type, so check the type of the result.
1344 -- If both arguments are literals, look for a type of the right kind
1345 -- defined in the given scope. This elaborate nonsense is brought to
1346 -- you courtesy of b33302a. The type itself must be frozen, so we must
1347 -- find the type of the proper class in the given scope.
1349 -- A final wrinkle is the multiplication operator for fixed point types,
1350 -- which is defined in Standard only, and not in the scope of the
1351 -- fixed point type itself.
1353 if Nkind (Name (N)) = N_Expanded_Name then
1354 Pack := Entity (Prefix (Name (N)));
1356 -- If the entity being called is defined in the given package, it is
1357 -- a renaming of a predefined operator, and known to be legal.
1359 if Scope (Entity (Name (N))) = Pack
1360 and then Pack /= Standard_Standard
1364 -- Visibility does not need to be checked in an instance: if the
1365 -- operator was not visible in the generic it has been diagnosed
1366 -- already, else there is an implicit copy of it in the instance.
1368 elsif In_Instance then
1371 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1372 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1373 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1375 if Pack /= Standard_Standard then
1379 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1382 elsif Ada_Version >= Ada_2005
1383 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1384 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1389 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1391 if Op_Name = Name_Op_Concat then
1392 Opnd_Type := Base_Type (Typ);
1394 elsif (Scope (Opnd_Type) = Standard_Standard
1396 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1398 and then not Comes_From_Source (Opnd_Type))
1400 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1403 if Scope (Opnd_Type) = Standard_Standard then
1405 -- Verify that the scope contains a type that corresponds to
1406 -- the given literal. Optimize the case where Pack is Standard.
1408 if Pack /= Standard_Standard then
1410 if Opnd_Type = Universal_Integer then
1411 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1413 elsif Opnd_Type = Universal_Real then
1414 Orig_Type := Type_In_P (Is_Real_Type'Access);
1416 elsif Opnd_Type = Any_String then
1417 Orig_Type := Type_In_P (Is_String_Type'Access);
1419 elsif Opnd_Type = Any_Access then
1420 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1422 elsif Opnd_Type = Any_Composite then
1423 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1425 if Present (Orig_Type) then
1426 if Has_Private_Component (Orig_Type) then
1429 Set_Etype (Act1, Orig_Type);
1432 Set_Etype (Act2, Orig_Type);
1441 Error := No (Orig_Type);
1444 elsif Ekind (Opnd_Type) = E_Allocator_Type
1445 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1449 -- If the type is defined elsewhere, and the operator is not
1450 -- defined in the given scope (by a renaming declaration, e.g.)
1451 -- then this is an error as well. If an extension of System is
1452 -- present, and the type may be defined there, Pack must be
1455 elsif Scope (Opnd_Type) /= Pack
1456 and then Scope (Op_Id) /= Pack
1457 and then (No (System_Aux_Id)
1458 or else Scope (Opnd_Type) /= System_Aux_Id
1459 or else Pack /= Scope (System_Aux_Id))
1461 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1464 Error := not Operand_Type_In_Scope (Pack);
1467 elsif Pack = Standard_Standard
1468 and then not Operand_Type_In_Scope (Standard_Standard)
1475 Error_Msg_Node_2 := Pack;
1477 ("& not declared in&", N, Selector_Name (Name (N)));
1478 Set_Etype (N, Any_Type);
1481 -- Detect a mismatch between the context type and the result type
1482 -- in the named package, which is otherwise not detected if the
1483 -- operands are universal. Check is only needed if source entity is
1484 -- an operator, not a function that renames an operator.
1486 elsif Nkind (Parent (N)) /= N_Type_Conversion
1487 and then Ekind (Entity (Name (N))) = E_Operator
1488 and then Is_Numeric_Type (Typ)
1489 and then not Is_Universal_Numeric_Type (Typ)
1490 and then Scope (Base_Type (Typ)) /= Pack
1491 and then not In_Instance
1493 if Is_Fixed_Point_Type (Typ)
1494 and then (Op_Name = Name_Op_Multiply
1496 Op_Name = Name_Op_Divide)
1498 -- Already checked above
1502 -- Operator may be defined in an extension of System
1504 elsif Present (System_Aux_Id)
1505 and then Scope (Opnd_Type) = System_Aux_Id
1510 -- Could we use Wrong_Type here??? (this would require setting
1511 -- Etype (N) to the actual type found where Typ was expected).
1513 Error_Msg_NE ("expect }", N, Typ);
1518 Set_Chars (Op_Node, Op_Name);
1520 if not Is_Private_Type (Etype (N)) then
1521 Set_Etype (Op_Node, Base_Type (Etype (N)));
1523 Set_Etype (Op_Node, Etype (N));
1526 -- If this is a call to a function that renames a predefined equality,
1527 -- the renaming declaration provides a type that must be used to
1528 -- resolve the operands. This must be done now because resolution of
1529 -- the equality node will not resolve any remaining ambiguity, and it
1530 -- assumes that the first operand is not overloaded.
1532 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1533 and then Ekind (Func) = E_Function
1534 and then Is_Overloaded (Act1)
1536 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1537 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1540 Set_Entity (Op_Node, Op_Id);
1541 Generate_Reference (Op_Id, N, ' ');
1543 -- Do rewrite setting Comes_From_Source on the result if the original
1544 -- call came from source. Although it is not strictly the case that the
1545 -- operator as such comes from the source, logically it corresponds
1546 -- exactly to the function call in the source, so it should be marked
1547 -- this way (e.g. to make sure that validity checks work fine).
1550 CS : constant Boolean := Comes_From_Source (N);
1552 Rewrite (N, Op_Node);
1553 Set_Comes_From_Source (N, CS);
1556 -- If this is an arithmetic operator and the result type is private,
1557 -- the operands and the result must be wrapped in conversion to
1558 -- expose the underlying numeric type and expand the proper checks,
1559 -- e.g. on division.
1561 if Is_Private_Type (Typ) then
1563 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1564 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1565 Resolve_Intrinsic_Operator (N, Typ);
1567 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1568 Resolve_Intrinsic_Unary_Operator (N, Typ);
1576 end Make_Call_Into_Operator;
1582 function Operator_Kind
1584 Is_Binary : Boolean) return Node_Kind
1589 -- Use CASE statement or array???
1592 if Op_Name = Name_Op_And then
1594 elsif Op_Name = Name_Op_Or then
1596 elsif Op_Name = Name_Op_Xor then
1598 elsif Op_Name = Name_Op_Eq then
1600 elsif Op_Name = Name_Op_Ne then
1602 elsif Op_Name = Name_Op_Lt then
1604 elsif Op_Name = Name_Op_Le then
1606 elsif Op_Name = Name_Op_Gt then
1608 elsif Op_Name = Name_Op_Ge then
1610 elsif Op_Name = Name_Op_Add then
1612 elsif Op_Name = Name_Op_Subtract then
1613 Kind := N_Op_Subtract;
1614 elsif Op_Name = Name_Op_Concat then
1615 Kind := N_Op_Concat;
1616 elsif Op_Name = Name_Op_Multiply then
1617 Kind := N_Op_Multiply;
1618 elsif Op_Name = Name_Op_Divide then
1619 Kind := N_Op_Divide;
1620 elsif Op_Name = Name_Op_Mod then
1622 elsif Op_Name = Name_Op_Rem then
1624 elsif Op_Name = Name_Op_Expon then
1627 raise Program_Error;
1633 if Op_Name = Name_Op_Add then
1635 elsif Op_Name = Name_Op_Subtract then
1637 elsif Op_Name = Name_Op_Abs then
1639 elsif Op_Name = Name_Op_Not then
1642 raise Program_Error;
1649 ----------------------------
1650 -- Preanalyze_And_Resolve --
1651 ----------------------------
1653 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1654 Save_Full_Analysis : constant Boolean := Full_Analysis;
1657 Full_Analysis := False;
1658 Expander_Mode_Save_And_Set (False);
1660 -- We suppress all checks for this analysis, since the checks will
1661 -- be applied properly, and in the right location, when the default
1662 -- expression is reanalyzed and reexpanded later on.
1664 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1666 Expander_Mode_Restore;
1667 Full_Analysis := Save_Full_Analysis;
1668 end Preanalyze_And_Resolve;
1670 -- Version without context type
1672 procedure Preanalyze_And_Resolve (N : Node_Id) is
1673 Save_Full_Analysis : constant Boolean := Full_Analysis;
1676 Full_Analysis := False;
1677 Expander_Mode_Save_And_Set (False);
1680 Resolve (N, Etype (N), Suppress => All_Checks);
1682 Expander_Mode_Restore;
1683 Full_Analysis := Save_Full_Analysis;
1684 end Preanalyze_And_Resolve;
1686 ----------------------------------
1687 -- Replace_Actual_Discriminants --
1688 ----------------------------------
1690 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1691 Loc : constant Source_Ptr := Sloc (N);
1692 Tsk : Node_Id := Empty;
1694 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1695 -- Comment needed???
1701 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1705 if Nkind (Nod) = N_Identifier then
1706 Ent := Entity (Nod);
1709 and then Ekind (Ent) = E_Discriminant
1712 Make_Selected_Component (Loc,
1713 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1714 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1716 Set_Etype (Nod, Etype (Ent));
1724 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1726 -- Start of processing for Replace_Actual_Discriminants
1729 if not Full_Expander_Active then
1733 if Nkind (Name (N)) = N_Selected_Component then
1734 Tsk := Prefix (Name (N));
1736 elsif Nkind (Name (N)) = N_Indexed_Component then
1737 Tsk := Prefix (Prefix (Name (N)));
1743 Replace_Discrs (Default);
1745 end Replace_Actual_Discriminants;
1751 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1752 Ambiguous : Boolean := False;
1753 Ctx_Type : Entity_Id := Typ;
1754 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1755 Err_Type : Entity_Id := Empty;
1756 Found : Boolean := False;
1759 I1 : Interp_Index := 0; -- prevent junk warning
1762 Seen : Entity_Id := Empty; -- prevent junk warning
1764 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1765 -- Determine whether a node comes from a predefined library unit or
1768 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1769 -- Try and fix up a literal so that it matches its expected type. New
1770 -- literals are manufactured if necessary to avoid cascaded errors.
1772 procedure Report_Ambiguous_Argument;
1773 -- Additional diagnostics when an ambiguous call has an ambiguous
1774 -- argument (typically a controlling actual).
1776 procedure Resolution_Failed;
1777 -- Called when attempt at resolving current expression fails
1779 ------------------------------------
1780 -- Comes_From_Predefined_Lib_Unit --
1781 -------------------------------------
1783 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1786 Sloc (Nod) = Standard_Location
1787 or else Is_Predefined_File_Name
1788 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1789 end Comes_From_Predefined_Lib_Unit;
1791 --------------------
1792 -- Patch_Up_Value --
1793 --------------------
1795 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1797 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1799 Make_Real_Literal (Sloc (N),
1800 Realval => UR_From_Uint (Intval (N))));
1801 Set_Etype (N, Universal_Real);
1802 Set_Is_Static_Expression (N);
1804 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1806 Make_Integer_Literal (Sloc (N),
1807 Intval => UR_To_Uint (Realval (N))));
1808 Set_Etype (N, Universal_Integer);
1809 Set_Is_Static_Expression (N);
1811 elsif Nkind (N) = N_String_Literal
1812 and then Is_Character_Type (Typ)
1814 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1816 Make_Character_Literal (Sloc (N),
1818 Char_Literal_Value =>
1819 UI_From_Int (Character'Pos ('A'))));
1820 Set_Etype (N, Any_Character);
1821 Set_Is_Static_Expression (N);
1823 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1825 Make_String_Literal (Sloc (N),
1826 Strval => End_String));
1828 elsif Nkind (N) = N_Range then
1829 Patch_Up_Value (Low_Bound (N), Typ);
1830 Patch_Up_Value (High_Bound (N), Typ);
1834 -------------------------------
1835 -- Report_Ambiguous_Argument --
1836 -------------------------------
1838 procedure Report_Ambiguous_Argument is
1839 Arg : constant Node_Id := First (Parameter_Associations (N));
1844 if Nkind (Arg) = N_Function_Call
1845 and then Is_Entity_Name (Name (Arg))
1846 and then Is_Overloaded (Name (Arg))
1848 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1850 -- Could use comments on what is going on here???
1852 Get_First_Interp (Name (Arg), I, It);
1853 while Present (It.Nam) loop
1854 Error_Msg_Sloc := Sloc (It.Nam);
1856 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1857 Error_Msg_N ("interpretation (inherited) #!", Arg);
1859 Error_Msg_N ("interpretation #!", Arg);
1862 Get_Next_Interp (I, It);
1865 end Report_Ambiguous_Argument;
1867 -----------------------
1868 -- Resolution_Failed --
1869 -----------------------
1871 procedure Resolution_Failed is
1873 Patch_Up_Value (N, Typ);
1875 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1876 Set_Is_Overloaded (N, False);
1878 -- The caller will return without calling the expander, so we need
1879 -- to set the analyzed flag. Note that it is fine to set Analyzed
1880 -- to True even if we are in the middle of a shallow analysis,
1881 -- (see the spec of sem for more details) since this is an error
1882 -- situation anyway, and there is no point in repeating the
1883 -- analysis later (indeed it won't work to repeat it later, since
1884 -- we haven't got a clear resolution of which entity is being
1887 Set_Analyzed (N, True);
1889 end Resolution_Failed;
1891 -- Start of processing for Resolve
1898 -- Access attribute on remote subprogram cannot be used for a non-remote
1899 -- access-to-subprogram type.
1901 if Nkind (N) = N_Attribute_Reference
1902 and then (Attribute_Name (N) = Name_Access or else
1903 Attribute_Name (N) = Name_Unrestricted_Access or else
1904 Attribute_Name (N) = Name_Unchecked_Access)
1905 and then Comes_From_Source (N)
1906 and then Is_Entity_Name (Prefix (N))
1907 and then Is_Subprogram (Entity (Prefix (N)))
1908 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1909 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1912 ("prefix must statically denote a non-remote subprogram", N);
1915 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1917 -- If the context is a Remote_Access_To_Subprogram, access attributes
1918 -- must be resolved with the corresponding fat pointer. There is no need
1919 -- to check for the attribute name since the return type of an
1920 -- attribute is never a remote type.
1922 if Nkind (N) = N_Attribute_Reference
1923 and then Comes_From_Source (N)
1924 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1927 Attr : constant Attribute_Id :=
1928 Get_Attribute_Id (Attribute_Name (N));
1929 Pref : constant Node_Id := Prefix (N);
1932 Is_Remote : Boolean := True;
1935 -- Check that Typ is a remote access-to-subprogram type
1937 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1939 -- Prefix (N) must statically denote a remote subprogram
1940 -- declared in a package specification.
1942 if Attr = Attribute_Access then
1943 Decl := Unit_Declaration_Node (Entity (Pref));
1945 if Nkind (Decl) = N_Subprogram_Body then
1946 Spec := Corresponding_Spec (Decl);
1948 if not No (Spec) then
1949 Decl := Unit_Declaration_Node (Spec);
1953 Spec := Parent (Decl);
1955 if not Is_Entity_Name (Prefix (N))
1956 or else Nkind (Spec) /= N_Package_Specification
1958 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1962 ("prefix must statically denote a remote subprogram ",
1967 -- If we are generating code for a distributed program.
1968 -- perform semantic checks against the corresponding
1971 if (Attr = Attribute_Access or else
1972 Attr = Attribute_Unchecked_Access or else
1973 Attr = Attribute_Unrestricted_Access)
1974 and then Full_Expander_Active
1975 and then Get_PCS_Name /= Name_No_DSA
1977 Check_Subtype_Conformant
1978 (New_Id => Entity (Prefix (N)),
1979 Old_Id => Designated_Type
1980 (Corresponding_Remote_Type (Typ)),
1984 Process_Remote_AST_Attribute (N, Typ);
1991 Debug_A_Entry ("resolving ", N);
1993 if Comes_From_Source (N) then
1994 if Is_Fixed_Point_Type (Typ) then
1995 Check_Restriction (No_Fixed_Point, N);
1997 elsif Is_Floating_Point_Type (Typ)
1998 and then Typ /= Universal_Real
1999 and then Typ /= Any_Real
2001 Check_Restriction (No_Floating_Point, N);
2005 -- Return if already analyzed
2007 if Analyzed (N) then
2008 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2011 -- Return if type = Any_Type (previous error encountered)
2013 elsif Etype (N) = Any_Type then
2014 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2018 Check_Parameterless_Call (N);
2020 -- If not overloaded, then we know the type, and all that needs doing
2021 -- is to check that this type is compatible with the context.
2023 if not Is_Overloaded (N) then
2024 Found := Covers (Typ, Etype (N));
2025 Expr_Type := Etype (N);
2027 -- In the overloaded case, we must select the interpretation that
2028 -- is compatible with the context (i.e. the type passed to Resolve)
2031 -- Loop through possible interpretations
2033 Get_First_Interp (N, I, It);
2034 Interp_Loop : while Present (It.Typ) loop
2036 -- We are only interested in interpretations that are compatible
2037 -- with the expected type, any other interpretations are ignored.
2039 if not Covers (Typ, It.Typ) then
2040 if Debug_Flag_V then
2041 Write_Str (" interpretation incompatible with context");
2046 -- Skip the current interpretation if it is disabled by an
2047 -- abstract operator. This action is performed only when the
2048 -- type against which we are resolving is the same as the
2049 -- type of the interpretation.
2051 if Ada_Version >= Ada_2005
2052 and then It.Typ = Typ
2053 and then Typ /= Universal_Integer
2054 and then Typ /= Universal_Real
2055 and then Present (It.Abstract_Op)
2060 -- First matching interpretation
2066 Expr_Type := It.Typ;
2068 -- Matching interpretation that is not the first, maybe an
2069 -- error, but there are some cases where preference rules are
2070 -- used to choose between the two possibilities. These and
2071 -- some more obscure cases are handled in Disambiguate.
2074 -- If the current statement is part of a predefined library
2075 -- unit, then all interpretations which come from user level
2076 -- packages should not be considered.
2079 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2084 Error_Msg_Sloc := Sloc (Seen);
2085 It1 := Disambiguate (N, I1, I, Typ);
2087 -- Disambiguation has succeeded. Skip the remaining
2090 if It1 /= No_Interp then
2092 Expr_Type := It1.Typ;
2094 while Present (It.Typ) loop
2095 Get_Next_Interp (I, It);
2099 -- Before we issue an ambiguity complaint, check for
2100 -- the case of a subprogram call where at least one
2101 -- of the arguments is Any_Type, and if so, suppress
2102 -- the message, since it is a cascaded error.
2104 if Nkind_In (N, N_Function_Call,
2105 N_Procedure_Call_Statement)
2112 A := First_Actual (N);
2113 while Present (A) loop
2116 if Nkind (E) = N_Parameter_Association then
2117 E := Explicit_Actual_Parameter (E);
2120 if Etype (E) = Any_Type then
2121 if Debug_Flag_V then
2122 Write_Str ("Any_Type in call");
2133 elsif Nkind (N) in N_Binary_Op
2134 and then (Etype (Left_Opnd (N)) = Any_Type
2135 or else Etype (Right_Opnd (N)) = Any_Type)
2139 elsif Nkind (N) in N_Unary_Op
2140 and then Etype (Right_Opnd (N)) = Any_Type
2145 -- Not that special case, so issue message using the
2146 -- flag Ambiguous to control printing of the header
2147 -- message only at the start of an ambiguous set.
2149 if not Ambiguous then
2150 if Nkind (N) = N_Function_Call
2151 and then Nkind (Name (N)) = N_Explicit_Dereference
2154 ("ambiguous expression "
2155 & "(cannot resolve indirect call)!", N);
2157 Error_Msg_NE -- CODEFIX
2158 ("ambiguous expression (cannot resolve&)!",
2164 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2166 ("\\possible interpretation (inherited)#!", N);
2168 Error_Msg_N -- CODEFIX
2169 ("\\possible interpretation#!", N);
2173 (N, N_Procedure_Call_Statement, N_Function_Call)
2174 and then Present (Parameter_Associations (N))
2176 Report_Ambiguous_Argument;
2180 Error_Msg_Sloc := Sloc (It.Nam);
2182 -- By default, the error message refers to the candidate
2183 -- interpretation. But if it is a predefined operator, it
2184 -- is implicitly declared at the declaration of the type
2185 -- of the operand. Recover the sloc of that declaration
2186 -- for the error message.
2188 if Nkind (N) in N_Op
2189 and then Scope (It.Nam) = Standard_Standard
2190 and then not Is_Overloaded (Right_Opnd (N))
2191 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2194 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2196 if Comes_From_Source (Err_Type)
2197 and then Present (Parent (Err_Type))
2199 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2202 elsif Nkind (N) in N_Binary_Op
2203 and then Scope (It.Nam) = Standard_Standard
2204 and then not Is_Overloaded (Left_Opnd (N))
2205 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2208 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2210 if Comes_From_Source (Err_Type)
2211 and then Present (Parent (Err_Type))
2213 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2216 -- If this is an indirect call, use the subprogram_type
2217 -- in the message, to have a meaningful location. Also
2218 -- indicate if this is an inherited operation, created
2219 -- by a type declaration.
2221 elsif Nkind (N) = N_Function_Call
2222 and then Nkind (Name (N)) = N_Explicit_Dereference
2223 and then Is_Type (It.Nam)
2227 Sloc (Associated_Node_For_Itype (Err_Type));
2232 if Nkind (N) in N_Op
2233 and then Scope (It.Nam) = Standard_Standard
2234 and then Present (Err_Type)
2236 -- Special-case the message for universal_fixed
2237 -- operators, which are not declared with the type
2238 -- of the operand, but appear forever in Standard.
2240 if It.Typ = Universal_Fixed
2241 and then Scope (It.Nam) = Standard_Standard
2244 ("\\possible interpretation as " &
2245 "universal_fixed operation " &
2246 "(RM 4.5.5 (19))", N);
2249 ("\\possible interpretation (predefined)#!", N);
2253 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2256 ("\\possible interpretation (inherited)#!", N);
2258 Error_Msg_N -- CODEFIX
2259 ("\\possible interpretation#!", N);
2265 -- We have a matching interpretation, Expr_Type is the type
2266 -- from this interpretation, and Seen is the entity.
2268 -- For an operator, just set the entity name. The type will be
2269 -- set by the specific operator resolution routine.
2271 if Nkind (N) in N_Op then
2272 Set_Entity (N, Seen);
2273 Generate_Reference (Seen, N);
2275 elsif Nkind (N) = N_Case_Expression then
2276 Set_Etype (N, Expr_Type);
2278 elsif Nkind (N) = N_Character_Literal then
2279 Set_Etype (N, Expr_Type);
2281 elsif Nkind (N) = N_Conditional_Expression then
2282 Set_Etype (N, Expr_Type);
2284 -- AI05-0139-2: Expression is overloaded because type has
2285 -- implicit dereference. If type matches context, no implicit
2286 -- dereference is involved.
2288 elsif Has_Implicit_Dereference (Expr_Type) then
2289 Set_Etype (N, Expr_Type);
2290 Set_Is_Overloaded (N, False);
2293 elsif Is_Overloaded (N)
2294 and then Present (It.Nam)
2295 and then Ekind (It.Nam) = E_Discriminant
2296 and then Has_Implicit_Dereference (It.Nam)
2298 Build_Explicit_Dereference (N, It.Nam);
2300 -- For an explicit dereference, attribute reference, range,
2301 -- short-circuit form (which is not an operator node), or call
2302 -- with a name that is an explicit dereference, there is
2303 -- nothing to be done at this point.
2305 elsif Nkind_In (N, N_Explicit_Dereference,
2306 N_Attribute_Reference,
2308 N_Indexed_Component,
2311 N_Selected_Component,
2313 or else Nkind (Name (N)) = N_Explicit_Dereference
2317 -- For procedure or function calls, set the type of the name,
2318 -- and also the entity pointer for the prefix.
2320 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2321 and then Is_Entity_Name (Name (N))
2323 Set_Etype (Name (N), Expr_Type);
2324 Set_Entity (Name (N), Seen);
2325 Generate_Reference (Seen, Name (N));
2327 elsif Nkind (N) = N_Function_Call
2328 and then Nkind (Name (N)) = N_Selected_Component
2330 Set_Etype (Name (N), Expr_Type);
2331 Set_Entity (Selector_Name (Name (N)), Seen);
2332 Generate_Reference (Seen, Selector_Name (Name (N)));
2334 -- For all other cases, just set the type of the Name
2337 Set_Etype (Name (N), Expr_Type);
2344 -- Move to next interpretation
2346 exit Interp_Loop when No (It.Typ);
2348 Get_Next_Interp (I, It);
2349 end loop Interp_Loop;
2352 -- At this stage Found indicates whether or not an acceptable
2353 -- interpretation exists. If not, then we have an error, except that if
2354 -- the context is Any_Type as a result of some other error, then we
2355 -- suppress the error report.
2358 if Typ /= Any_Type then
2360 -- If type we are looking for is Void, then this is the procedure
2361 -- call case, and the error is simply that what we gave is not a
2362 -- procedure name (we think of procedure calls as expressions with
2363 -- types internally, but the user doesn't think of them this way!)
2365 if Typ = Standard_Void_Type then
2367 -- Special case message if function used as a procedure
2369 if Nkind (N) = N_Procedure_Call_Statement
2370 and then Is_Entity_Name (Name (N))
2371 and then Ekind (Entity (Name (N))) = E_Function
2374 ("cannot use function & in a procedure call",
2375 Name (N), Entity (Name (N)));
2377 -- Otherwise give general message (not clear what cases this
2378 -- covers, but no harm in providing for them!)
2381 Error_Msg_N ("expect procedure name in procedure call", N);
2386 -- Otherwise we do have a subexpression with the wrong type
2388 -- Check for the case of an allocator which uses an access type
2389 -- instead of the designated type. This is a common error and we
2390 -- specialize the message, posting an error on the operand of the
2391 -- allocator, complaining that we expected the designated type of
2394 elsif Nkind (N) = N_Allocator
2395 and then Ekind (Typ) in Access_Kind
2396 and then Ekind (Etype (N)) in Access_Kind
2397 and then Designated_Type (Etype (N)) = Typ
2399 Wrong_Type (Expression (N), Designated_Type (Typ));
2402 -- Check for view mismatch on Null in instances, for which the
2403 -- view-swapping mechanism has no identifier.
2405 elsif (In_Instance or else In_Inlined_Body)
2406 and then (Nkind (N) = N_Null)
2407 and then Is_Private_Type (Typ)
2408 and then Is_Access_Type (Full_View (Typ))
2410 Resolve (N, Full_View (Typ));
2414 -- Check for an aggregate. Sometimes we can get bogus aggregates
2415 -- from misuse of parentheses, and we are about to complain about
2416 -- the aggregate without even looking inside it.
2418 -- Instead, if we have an aggregate of type Any_Composite, then
2419 -- analyze and resolve the component fields, and then only issue
2420 -- another message if we get no errors doing this (otherwise
2421 -- assume that the errors in the aggregate caused the problem).
2423 elsif Nkind (N) = N_Aggregate
2424 and then Etype (N) = Any_Composite
2426 -- Disable expansion in any case. If there is a type mismatch
2427 -- it may be fatal to try to expand the aggregate. The flag
2428 -- would otherwise be set to false when the error is posted.
2430 Expander_Active := False;
2433 procedure Check_Aggr (Aggr : Node_Id);
2434 -- Check one aggregate, and set Found to True if we have a
2435 -- definite error in any of its elements
2437 procedure Check_Elmt (Aelmt : Node_Id);
2438 -- Check one element of aggregate and set Found to True if
2439 -- we definitely have an error in the element.
2445 procedure Check_Aggr (Aggr : Node_Id) is
2449 if Present (Expressions (Aggr)) then
2450 Elmt := First (Expressions (Aggr));
2451 while Present (Elmt) loop
2457 if Present (Component_Associations (Aggr)) then
2458 Elmt := First (Component_Associations (Aggr));
2459 while Present (Elmt) loop
2461 -- If this is a default-initialized component, then
2462 -- there is nothing to check. The box will be
2463 -- replaced by the appropriate call during late
2466 if not Box_Present (Elmt) then
2467 Check_Elmt (Expression (Elmt));
2479 procedure Check_Elmt (Aelmt : Node_Id) is
2481 -- If we have a nested aggregate, go inside it (to
2482 -- attempt a naked analyze-resolve of the aggregate can
2483 -- cause undesirable cascaded errors). Do not resolve
2484 -- expression if it needs a type from context, as for
2485 -- integer * fixed expression.
2487 if Nkind (Aelmt) = N_Aggregate then
2493 if not Is_Overloaded (Aelmt)
2494 and then Etype (Aelmt) /= Any_Fixed
2499 if Etype (Aelmt) = Any_Type then
2510 -- If an error message was issued already, Found got reset to
2511 -- True, so if it is still False, issue standard Wrong_Type msg.
2514 if Is_Overloaded (N)
2515 and then Nkind (N) = N_Function_Call
2518 Subp_Name : Node_Id;
2520 if Is_Entity_Name (Name (N)) then
2521 Subp_Name := Name (N);
2523 elsif Nkind (Name (N)) = N_Selected_Component then
2525 -- Protected operation: retrieve operation name
2527 Subp_Name := Selector_Name (Name (N));
2530 raise Program_Error;
2533 Error_Msg_Node_2 := Typ;
2534 Error_Msg_NE ("no visible interpretation of&" &
2535 " matches expected type&", N, Subp_Name);
2538 if All_Errors_Mode then
2540 Index : Interp_Index;
2544 Error_Msg_N ("\\possible interpretations:", N);
2546 Get_First_Interp (Name (N), Index, It);
2547 while Present (It.Nam) loop
2548 Error_Msg_Sloc := Sloc (It.Nam);
2549 Error_Msg_Node_2 := It.Nam;
2551 ("\\ type& for & declared#", N, It.Typ);
2552 Get_Next_Interp (Index, It);
2557 Error_Msg_N ("\use -gnatf for details", N);
2561 Wrong_Type (N, Typ);
2569 -- Test if we have more than one interpretation for the context
2571 elsif Ambiguous then
2575 -- Here we have an acceptable interpretation for the context
2578 -- Propagate type information and normalize tree for various
2579 -- predefined operations. If the context only imposes a class of
2580 -- types, rather than a specific type, propagate the actual type
2583 if Typ = Any_Integer or else
2584 Typ = Any_Boolean or else
2585 Typ = Any_Modular or else
2586 Typ = Any_Real or else
2589 Ctx_Type := Expr_Type;
2591 -- Any_Fixed is legal in a real context only if a specific fixed-
2592 -- point type is imposed. If Norman Cohen can be confused by this,
2593 -- it deserves a separate message.
2596 and then Expr_Type = Any_Fixed
2598 Error_Msg_N ("illegal context for mixed mode operation", N);
2599 Set_Etype (N, Universal_Real);
2600 Ctx_Type := Universal_Real;
2604 -- A user-defined operator is transformed into a function call at
2605 -- this point, so that further processing knows that operators are
2606 -- really operators (i.e. are predefined operators). User-defined
2607 -- operators that are intrinsic are just renamings of the predefined
2608 -- ones, and need not be turned into calls either, but if they rename
2609 -- a different operator, we must transform the node accordingly.
2610 -- Instantiations of Unchecked_Conversion are intrinsic but are
2611 -- treated as functions, even if given an operator designator.
2613 if Nkind (N) in N_Op
2614 and then Present (Entity (N))
2615 and then Ekind (Entity (N)) /= E_Operator
2618 if not Is_Predefined_Op (Entity (N)) then
2619 Rewrite_Operator_As_Call (N, Entity (N));
2621 elsif Present (Alias (Entity (N)))
2623 Nkind (Parent (Parent (Entity (N)))) =
2624 N_Subprogram_Renaming_Declaration
2626 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2628 -- If the node is rewritten, it will be fully resolved in
2629 -- Rewrite_Renamed_Operator.
2631 if Analyzed (N) then
2637 case N_Subexpr'(Nkind (N)) is
2639 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2641 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2643 when N_Short_Circuit
2644 => Resolve_Short_Circuit (N, Ctx_Type);
2646 when N_Attribute_Reference
2647 => Resolve_Attribute (N, Ctx_Type);
2649 when N_Case_Expression
2650 => Resolve_Case_Expression (N, Ctx_Type);
2652 when N_Character_Literal
2653 => Resolve_Character_Literal (N, Ctx_Type);
2655 when N_Conditional_Expression
2656 => Resolve_Conditional_Expression (N, Ctx_Type);
2658 when N_Expanded_Name
2659 => Resolve_Entity_Name (N, Ctx_Type);
2661 when N_Explicit_Dereference
2662 => Resolve_Explicit_Dereference (N, Ctx_Type);
2664 when N_Expression_With_Actions
2665 => Resolve_Expression_With_Actions (N, Ctx_Type);
2667 when N_Extension_Aggregate
2668 => Resolve_Extension_Aggregate (N, Ctx_Type);
2670 when N_Function_Call
2671 => Resolve_Call (N, Ctx_Type);
2674 => Resolve_Entity_Name (N, Ctx_Type);
2676 when N_Indexed_Component
2677 => Resolve_Indexed_Component (N, Ctx_Type);
2679 when N_Integer_Literal
2680 => Resolve_Integer_Literal (N, Ctx_Type);
2682 when N_Membership_Test
2683 => Resolve_Membership_Op (N, Ctx_Type);
2685 when N_Null => Resolve_Null (N, Ctx_Type);
2687 when N_Op_And | N_Op_Or | N_Op_Xor
2688 => Resolve_Logical_Op (N, Ctx_Type);
2690 when N_Op_Eq | N_Op_Ne
2691 => Resolve_Equality_Op (N, Ctx_Type);
2693 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2694 => Resolve_Comparison_Op (N, Ctx_Type);
2696 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2698 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2699 N_Op_Divide | N_Op_Mod | N_Op_Rem
2701 => Resolve_Arithmetic_Op (N, Ctx_Type);
2703 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2705 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2707 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2708 => Resolve_Unary_Op (N, Ctx_Type);
2710 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2712 when N_Procedure_Call_Statement
2713 => Resolve_Call (N, Ctx_Type);
2715 when N_Operator_Symbol
2716 => Resolve_Operator_Symbol (N, Ctx_Type);
2718 when N_Qualified_Expression
2719 => Resolve_Qualified_Expression (N, Ctx_Type);
2721 when N_Quantified_Expression
2722 => Resolve_Quantified_Expression (N, Ctx_Type);
2724 when N_Raise_xxx_Error
2725 => Set_Etype (N, Ctx_Type);
2727 when N_Range => Resolve_Range (N, Ctx_Type);
2730 => Resolve_Real_Literal (N, Ctx_Type);
2732 when N_Reference => Resolve_Reference (N, Ctx_Type);
2734 when N_Selected_Component
2735 => Resolve_Selected_Component (N, Ctx_Type);
2737 when N_Slice => Resolve_Slice (N, Ctx_Type);
2739 when N_String_Literal
2740 => Resolve_String_Literal (N, Ctx_Type);
2742 when N_Subprogram_Info
2743 => Resolve_Subprogram_Info (N, Ctx_Type);
2745 when N_Type_Conversion
2746 => Resolve_Type_Conversion (N, Ctx_Type);
2748 when N_Unchecked_Expression =>
2749 Resolve_Unchecked_Expression (N, Ctx_Type);
2751 when N_Unchecked_Type_Conversion =>
2752 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2755 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2756 -- expression of an anonymous access type that occurs in the context
2757 -- of a named general access type, except when the expression is that
2758 -- of a membership test. This ensures proper legality checking in
2759 -- terms of allowed conversions (expressions that would be illegal to
2760 -- convert implicitly are allowed in membership tests).
2762 if Ada_Version >= Ada_2012
2763 and then Ekind (Ctx_Type) = E_General_Access_Type
2764 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2765 and then Nkind (Parent (N)) not in N_Membership_Test
2767 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2768 Analyze_And_Resolve (N, Ctx_Type);
2771 -- If the subexpression was replaced by a non-subexpression, then
2772 -- all we do is to expand it. The only legitimate case we know of
2773 -- is converting procedure call statement to entry call statements,
2774 -- but there may be others, so we are making this test general.
2776 if Nkind (N) not in N_Subexpr then
2777 Debug_A_Exit ("resolving ", N, " (done)");
2782 -- AI05-144-2: Check dangerous order dependence within an expression
2783 -- that is not a subexpression. Exclude RHS of an assignment, because
2784 -- both sides may have side-effects and the check must be performed
2785 -- over the statement.
2787 if Nkind (Parent (N)) not in N_Subexpr
2788 and then Nkind (Parent (N)) /= N_Assignment_Statement
2789 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2791 Check_Order_Dependence;
2794 -- The expression is definitely NOT overloaded at this point, so
2795 -- we reset the Is_Overloaded flag to avoid any confusion when
2796 -- reanalyzing the node.
2798 Set_Is_Overloaded (N, False);
2800 -- Freeze expression type, entity if it is a name, and designated
2801 -- type if it is an allocator (RM 13.14(10,11,13)).
2803 -- Now that the resolution of the type of the node is complete, and
2804 -- we did not detect an error, we can expand this node. We skip the
2805 -- expand call if we are in a default expression, see section
2806 -- "Handling of Default Expressions" in Sem spec.
2808 Debug_A_Exit ("resolving ", N, " (done)");
2810 -- We unconditionally freeze the expression, even if we are in
2811 -- default expression mode (the Freeze_Expression routine tests this
2812 -- flag and only freezes static types if it is set).
2814 -- AI05-177 (Ada2012): Expression functions do not freeze. Only
2815 -- their use (in an expanded call) freezes.
2817 if Ekind (Current_Scope) /= E_Function
2819 Nkind (Original_Node (Unit_Declaration_Node (Current_Scope))) /=
2820 N_Expression_Function
2822 Freeze_Expression (N);
2825 -- Now we can do the expansion
2835 -- Version with check(s) suppressed
2837 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2839 if Suppress = All_Checks then
2841 Svg : constant Suppress_Array := Scope_Suppress;
2843 Scope_Suppress := (others => True);
2845 Scope_Suppress := Svg;
2850 Svg : constant Boolean := Scope_Suppress (Suppress);
2852 Scope_Suppress (Suppress) := True;
2854 Scope_Suppress (Suppress) := Svg;
2863 -- Version with implicit type
2865 procedure Resolve (N : Node_Id) is
2867 Resolve (N, Etype (N));
2870 ---------------------
2871 -- Resolve_Actuals --
2872 ---------------------
2874 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2875 Loc : constant Source_Ptr := Sloc (N);
2880 Prev : Node_Id := Empty;
2883 procedure Check_Argument_Order;
2884 -- Performs a check for the case where the actuals are all simple
2885 -- identifiers that correspond to the formal names, but in the wrong
2886 -- order, which is considered suspicious and cause for a warning.
2888 procedure Check_Prefixed_Call;
2889 -- If the original node is an overloaded call in prefix notation,
2890 -- insert an 'Access or a dereference as needed over the first actual.
2891 -- Try_Object_Operation has already verified that there is a valid
2892 -- interpretation, but the form of the actual can only be determined
2893 -- once the primitive operation is identified.
2895 procedure Insert_Default;
2896 -- If the actual is missing in a call, insert in the actuals list
2897 -- an instance of the default expression. The insertion is always
2898 -- a named association.
2900 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2901 -- Check whether T1 and T2, or their full views, are derived from a
2902 -- common type. Used to enforce the restrictions on array conversions
2905 function Static_Concatenation (N : Node_Id) return Boolean;
2906 -- Predicate to determine whether an actual that is a concatenation
2907 -- will be evaluated statically and does not need a transient scope.
2908 -- This must be determined before the actual is resolved and expanded
2909 -- because if needed the transient scope must be introduced earlier.
2911 --------------------------
2912 -- Check_Argument_Order --
2913 --------------------------
2915 procedure Check_Argument_Order is
2917 -- Nothing to do if no parameters, or original node is neither a
2918 -- function call nor a procedure call statement (happens in the
2919 -- operator-transformed-to-function call case), or the call does
2920 -- not come from source, or this warning is off.
2922 if not Warn_On_Parameter_Order
2923 or else No (Parameter_Associations (N))
2924 or else not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2926 or else not Comes_From_Source (N)
2932 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2935 -- Nothing to do if only one parameter
2941 -- Here if at least two arguments
2944 Actuals : array (1 .. Nargs) of Node_Id;
2948 Wrong_Order : Boolean := False;
2949 -- Set True if an out of order case is found
2952 -- Collect identifier names of actuals, fail if any actual is
2953 -- not a simple identifier, and record max length of name.
2955 Actual := First (Parameter_Associations (N));
2956 for J in Actuals'Range loop
2957 if Nkind (Actual) /= N_Identifier then
2960 Actuals (J) := Actual;
2965 -- If we got this far, all actuals are identifiers and the list
2966 -- of their names is stored in the Actuals array.
2968 Formal := First_Formal (Nam);
2969 for J in Actuals'Range loop
2971 -- If we ran out of formals, that's odd, probably an error
2972 -- which will be detected elsewhere, but abandon the search.
2978 -- If name matches and is in order OK
2980 if Chars (Formal) = Chars (Actuals (J)) then
2984 -- If no match, see if it is elsewhere in list and if so
2985 -- flag potential wrong order if type is compatible.
2987 for K in Actuals'Range loop
2988 if Chars (Formal) = Chars (Actuals (K))
2990 Has_Compatible_Type (Actuals (K), Etype (Formal))
2992 Wrong_Order := True;
3002 <<Continue>> Next_Formal (Formal);
3005 -- If Formals left over, also probably an error, skip warning
3007 if Present (Formal) then
3011 -- Here we give the warning if something was out of order
3015 ("actuals for this call may be in wrong order?", N);
3019 end Check_Argument_Order;
3021 -------------------------
3022 -- Check_Prefixed_Call --
3023 -------------------------
3025 procedure Check_Prefixed_Call is
3026 Act : constant Node_Id := First_Actual (N);
3027 A_Type : constant Entity_Id := Etype (Act);
3028 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3029 Orig : constant Node_Id := Original_Node (N);
3033 -- Check whether the call is a prefixed call, with or without
3034 -- additional actuals.
3036 if Nkind (Orig) = N_Selected_Component
3038 (Nkind (Orig) = N_Indexed_Component
3039 and then Nkind (Prefix (Orig)) = N_Selected_Component
3040 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3041 and then Is_Entity_Name (Act)
3042 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3044 if Is_Access_Type (A_Type)
3045 and then not Is_Access_Type (F_Type)
3047 -- Introduce dereference on object in prefix
3050 Make_Explicit_Dereference (Sloc (Act),
3051 Prefix => Relocate_Node (Act));
3052 Rewrite (Act, New_A);
3055 elsif Is_Access_Type (F_Type)
3056 and then not Is_Access_Type (A_Type)
3058 -- Introduce an implicit 'Access in prefix
3060 if not Is_Aliased_View (Act) then
3062 ("object in prefixed call to& must be aliased"
3063 & " (RM-2005 4.3.1 (13))",
3068 Make_Attribute_Reference (Loc,
3069 Attribute_Name => Name_Access,
3070 Prefix => Relocate_Node (Act)));
3075 end Check_Prefixed_Call;
3077 --------------------
3078 -- Insert_Default --
3079 --------------------
3081 procedure Insert_Default is
3086 -- Missing argument in call, nothing to insert
3088 if No (Default_Value (F)) then
3092 -- Note that we do a full New_Copy_Tree, so that any associated
3093 -- Itypes are properly copied. This may not be needed any more,
3094 -- but it does no harm as a safety measure! Defaults of a generic
3095 -- formal may be out of bounds of the corresponding actual (see
3096 -- cc1311b) and an additional check may be required.
3101 New_Scope => Current_Scope,
3104 if Is_Concurrent_Type (Scope (Nam))
3105 and then Has_Discriminants (Scope (Nam))
3107 Replace_Actual_Discriminants (N, Actval);
3110 if Is_Overloadable (Nam)
3111 and then Present (Alias (Nam))
3113 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3114 and then not Is_Tagged_Type (Etype (F))
3116 -- If default is a real literal, do not introduce a
3117 -- conversion whose effect may depend on the run-time
3118 -- size of universal real.
3120 if Nkind (Actval) = N_Real_Literal then
3121 Set_Etype (Actval, Base_Type (Etype (F)));
3123 Actval := Unchecked_Convert_To (Etype (F), Actval);
3127 if Is_Scalar_Type (Etype (F)) then
3128 Enable_Range_Check (Actval);
3131 Set_Parent (Actval, N);
3133 -- Resolve aggregates with their base type, to avoid scope
3134 -- anomalies: the subtype was first built in the subprogram
3135 -- declaration, and the current call may be nested.
3137 if Nkind (Actval) = N_Aggregate then
3138 Analyze_And_Resolve (Actval, Etype (F));
3140 Analyze_And_Resolve (Actval, Etype (Actval));
3144 Set_Parent (Actval, N);
3146 -- See note above concerning aggregates
3148 if Nkind (Actval) = N_Aggregate
3149 and then Has_Discriminants (Etype (Actval))
3151 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3153 -- Resolve entities with their own type, which may differ from
3154 -- the type of a reference in a generic context (the view
3155 -- swapping mechanism did not anticipate the re-analysis of
3156 -- default values in calls).
3158 elsif Is_Entity_Name (Actval) then
3159 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3162 Analyze_And_Resolve (Actval, Etype (Actval));
3166 -- If default is a tag indeterminate function call, propagate tag
3167 -- to obtain proper dispatching.
3169 if Is_Controlling_Formal (F)
3170 and then Nkind (Default_Value (F)) = N_Function_Call
3172 Set_Is_Controlling_Actual (Actval);
3177 -- If the default expression raises constraint error, then just
3178 -- silently replace it with an N_Raise_Constraint_Error node, since
3179 -- we already gave the warning on the subprogram spec. If node is
3180 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3181 -- the warnings removal machinery.
3183 if Raises_Constraint_Error (Actval)
3184 and then Nkind (Actval) /= N_Raise_Constraint_Error
3187 Make_Raise_Constraint_Error (Loc,
3188 Reason => CE_Range_Check_Failed));
3189 Set_Raises_Constraint_Error (Actval);
3190 Set_Etype (Actval, Etype (F));
3194 Make_Parameter_Association (Loc,
3195 Explicit_Actual_Parameter => Actval,
3196 Selector_Name => Make_Identifier (Loc, Chars (F)));
3198 -- Case of insertion is first named actual
3200 if No (Prev) or else
3201 Nkind (Parent (Prev)) /= N_Parameter_Association
3203 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3204 Set_First_Named_Actual (N, Actval);
3207 if No (Parameter_Associations (N)) then
3208 Set_Parameter_Associations (N, New_List (Assoc));
3210 Append (Assoc, Parameter_Associations (N));
3214 Insert_After (Prev, Assoc);
3217 -- Case of insertion is not first named actual
3220 Set_Next_Named_Actual
3221 (Assoc, Next_Named_Actual (Parent (Prev)));
3222 Set_Next_Named_Actual (Parent (Prev), Actval);
3223 Append (Assoc, Parameter_Associations (N));
3226 Mark_Rewrite_Insertion (Assoc);
3227 Mark_Rewrite_Insertion (Actval);
3236 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3237 FT1 : Entity_Id := T1;
3238 FT2 : Entity_Id := T2;
3241 if Is_Private_Type (T1)
3242 and then Present (Full_View (T1))
3244 FT1 := Full_View (T1);
3247 if Is_Private_Type (T2)
3248 and then Present (Full_View (T2))
3250 FT2 := Full_View (T2);
3253 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3256 --------------------------
3257 -- Static_Concatenation --
3258 --------------------------
3260 function Static_Concatenation (N : Node_Id) return Boolean is
3263 when N_String_Literal =>
3268 -- Concatenation is static when both operands are static and
3269 -- the concatenation operator is a predefined one.
3271 return Scope (Entity (N)) = Standard_Standard
3273 Static_Concatenation (Left_Opnd (N))
3275 Static_Concatenation (Right_Opnd (N));
3278 if Is_Entity_Name (N) then
3280 Ent : constant Entity_Id := Entity (N);
3282 return Ekind (Ent) = E_Constant
3283 and then Present (Constant_Value (Ent))
3285 Is_Static_Expression (Constant_Value (Ent));
3292 end Static_Concatenation;
3294 -- Start of processing for Resolve_Actuals
3297 Check_Argument_Order;
3299 if Present (First_Actual (N)) then
3300 Check_Prefixed_Call;
3303 A := First_Actual (N);
3304 F := First_Formal (Nam);
3305 while Present (F) loop
3306 if No (A) and then Needs_No_Actuals (Nam) then
3309 -- If we have an error in any actual or formal, indicated by a type
3310 -- of Any_Type, then abandon resolution attempt, and set result type
3313 elsif (Present (A) and then Etype (A) = Any_Type)
3314 or else Etype (F) = Any_Type
3316 Set_Etype (N, Any_Type);
3320 -- Case where actual is present
3322 -- If the actual is an entity, generate a reference to it now. We
3323 -- do this before the actual is resolved, because a formal of some
3324 -- protected subprogram, or a task discriminant, will be rewritten
3325 -- during expansion, and the source entity reference may be lost.
3328 and then Is_Entity_Name (A)
3329 and then Comes_From_Source (N)
3331 Orig_A := Entity (A);
3333 if Present (Orig_A) then
3334 if Is_Formal (Orig_A)
3335 and then Ekind (F) /= E_In_Parameter
3337 Generate_Reference (Orig_A, A, 'm');
3339 elsif not Is_Overloaded (A) then
3340 Generate_Reference (Orig_A, A);
3346 and then (Nkind (Parent (A)) /= N_Parameter_Association
3347 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3349 -- If style checking mode on, check match of formal name
3352 if Nkind (Parent (A)) = N_Parameter_Association then
3353 Check_Identifier (Selector_Name (Parent (A)), F);
3357 -- If the formal is Out or In_Out, do not resolve and expand the
3358 -- conversion, because it is subsequently expanded into explicit
3359 -- temporaries and assignments. However, the object of the
3360 -- conversion can be resolved. An exception is the case of tagged
3361 -- type conversion with a class-wide actual. In that case we want
3362 -- the tag check to occur and no temporary will be needed (no
3363 -- representation change can occur) and the parameter is passed by
3364 -- reference, so we go ahead and resolve the type conversion.
3365 -- Another exception is the case of reference to component or
3366 -- subcomponent of a bit-packed array, in which case we want to
3367 -- defer expansion to the point the in and out assignments are
3370 if Ekind (F) /= E_In_Parameter
3371 and then Nkind (A) = N_Type_Conversion
3372 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3374 if Ekind (F) = E_In_Out_Parameter
3375 and then Is_Array_Type (Etype (F))
3377 -- In a view conversion, the conversion must be legal in
3378 -- both directions, and thus both component types must be
3379 -- aliased, or neither (4.6 (8)).
3381 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3382 -- the privacy requirement should not apply to generic
3383 -- types, and should be checked in an instance. ARG query
3386 if Has_Aliased_Components (Etype (Expression (A))) /=
3387 Has_Aliased_Components (Etype (F))
3390 ("both component types in a view conversion must be"
3391 & " aliased, or neither", A);
3393 -- Comment here??? what set of cases???
3396 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3398 -- Check view conv between unrelated by ref array types
3400 if Is_By_Reference_Type (Etype (F))
3401 or else Is_By_Reference_Type (Etype (Expression (A)))
3404 ("view conversion between unrelated by reference " &
3405 "array types not allowed (\'A'I-00246)", A);
3407 -- In Ada 2005 mode, check view conversion component
3408 -- type cannot be private, tagged, or volatile. Note
3409 -- that we only apply this to source conversions. The
3410 -- generated code can contain conversions which are
3411 -- not subject to this test, and we cannot extract the
3412 -- component type in such cases since it is not present.
3414 elsif Comes_From_Source (A)
3415 and then Ada_Version >= Ada_2005
3418 Comp_Type : constant Entity_Id :=
3420 (Etype (Expression (A)));
3422 if (Is_Private_Type (Comp_Type)
3423 and then not Is_Generic_Type (Comp_Type))
3424 or else Is_Tagged_Type (Comp_Type)
3425 or else Is_Volatile (Comp_Type)
3428 ("component type of a view conversion cannot"
3429 & " be private, tagged, or volatile"
3438 -- Resolve expression if conversion is all OK
3440 if (Conversion_OK (A)
3441 or else Valid_Conversion (A, Etype (A), Expression (A)))
3442 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3444 Resolve (Expression (A));
3447 -- If the actual is a function call that returns a limited
3448 -- unconstrained object that needs finalization, create a
3449 -- transient scope for it, so that it can receive the proper
3450 -- finalization list.
3452 elsif Nkind (A) = N_Function_Call
3453 and then Is_Limited_Record (Etype (F))
3454 and then not Is_Constrained (Etype (F))
3455 and then Full_Expander_Active
3456 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3458 Establish_Transient_Scope (A, False);
3459 Resolve (A, Etype (F));
3461 -- A small optimization: if one of the actuals is a concatenation
3462 -- create a block around a procedure call to recover stack space.
3463 -- This alleviates stack usage when several procedure calls in
3464 -- the same statement list use concatenation. We do not perform
3465 -- this wrapping for code statements, where the argument is a
3466 -- static string, and we want to preserve warnings involving
3467 -- sequences of such statements.
3469 elsif Nkind (A) = N_Op_Concat
3470 and then Nkind (N) = N_Procedure_Call_Statement
3471 and then Full_Expander_Active
3473 not (Is_Intrinsic_Subprogram (Nam)
3474 and then Chars (Nam) = Name_Asm)
3475 and then not Static_Concatenation (A)
3477 Establish_Transient_Scope (A, False);
3478 Resolve (A, Etype (F));
3481 if Nkind (A) = N_Type_Conversion
3482 and then Is_Array_Type (Etype (F))
3483 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3485 (Is_Limited_Type (Etype (F))
3486 or else Is_Limited_Type (Etype (Expression (A))))
3489 ("conversion between unrelated limited array types " &
3490 "not allowed (\A\I-00246)", A);
3492 if Is_Limited_Type (Etype (F)) then
3493 Explain_Limited_Type (Etype (F), A);
3496 if Is_Limited_Type (Etype (Expression (A))) then
3497 Explain_Limited_Type (Etype (Expression (A)), A);
3501 -- (Ada 2005: AI-251): If the actual is an allocator whose
3502 -- directly designated type is a class-wide interface, we build
3503 -- an anonymous access type to use it as the type of the
3504 -- allocator. Later, when the subprogram call is expanded, if
3505 -- the interface has a secondary dispatch table the expander
3506 -- will add a type conversion to force the correct displacement
3509 if Nkind (A) = N_Allocator then
3511 DDT : constant Entity_Id :=
3512 Directly_Designated_Type (Base_Type (Etype (F)));
3514 New_Itype : Entity_Id;
3517 if Is_Class_Wide_Type (DDT)
3518 and then Is_Interface (DDT)
3520 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3521 Set_Etype (New_Itype, Etype (A));
3522 Set_Directly_Designated_Type (New_Itype,
3523 Directly_Designated_Type (Etype (A)));
3524 Set_Etype (A, New_Itype);
3527 -- Ada 2005, AI-162:If the actual is an allocator, the
3528 -- innermost enclosing statement is the master of the
3529 -- created object. This needs to be done with expansion
3530 -- enabled only, otherwise the transient scope will not
3531 -- be removed in the expansion of the wrapped construct.
3533 if (Is_Controlled (DDT) or else Has_Task (DDT))
3534 and then Full_Expander_Active
3536 Establish_Transient_Scope (A, False);
3541 -- (Ada 2005): The call may be to a primitive operation of
3542 -- a tagged synchronized type, declared outside of the type.
3543 -- In this case the controlling actual must be converted to
3544 -- its corresponding record type, which is the formal type.
3545 -- The actual may be a subtype, either because of a constraint
3546 -- or because it is a generic actual, so use base type to
3547 -- locate concurrent type.
3549 F_Typ := Base_Type (Etype (F));
3551 if Is_Tagged_Type (F_Typ)
3552 and then (Is_Concurrent_Type (F_Typ)
3553 or else Is_Concurrent_Record_Type (F_Typ))
3555 -- If the actual is overloaded, look for an interpretation
3556 -- that has a synchronized type.
3558 if not Is_Overloaded (A) then
3559 A_Typ := Base_Type (Etype (A));
3563 Index : Interp_Index;
3567 Get_First_Interp (A, Index, It);
3568 while Present (It.Typ) loop
3569 if Is_Concurrent_Type (It.Typ)
3570 or else Is_Concurrent_Record_Type (It.Typ)
3572 A_Typ := Base_Type (It.Typ);
3576 Get_Next_Interp (Index, It);
3582 Full_A_Typ : Entity_Id;
3585 if Present (Full_View (A_Typ)) then
3586 Full_A_Typ := Base_Type (Full_View (A_Typ));
3588 Full_A_Typ := A_Typ;
3591 -- Tagged synchronized type (case 1): the actual is a
3594 if Is_Concurrent_Type (A_Typ)
3595 and then Corresponding_Record_Type (A_Typ) = F_Typ
3598 Unchecked_Convert_To
3599 (Corresponding_Record_Type (A_Typ), A));
3600 Resolve (A, Etype (F));
3602 -- Tagged synchronized type (case 2): the formal is a
3605 elsif Ekind (Full_A_Typ) = E_Record_Type
3607 (Corresponding_Concurrent_Type (Full_A_Typ))
3608 and then Is_Concurrent_Type (F_Typ)
3609 and then Present (Corresponding_Record_Type (F_Typ))
3610 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3612 Resolve (A, Corresponding_Record_Type (F_Typ));
3617 Resolve (A, Etype (F));
3622 -- not a synchronized operation.
3624 Resolve (A, Etype (F));
3631 if Comes_From_Source (Original_Node (N))
3632 and then Nkind_In (Original_Node (N), N_Function_Call,
3633 N_Procedure_Call_Statement)
3635 -- In formal mode, check that actual parameters matching
3636 -- formals of tagged types are objects (or ancestor type
3637 -- conversions of objects), not general expressions.
3639 if Is_Actual_Tagged_Parameter (A) then
3640 if Is_SPARK_Object_Reference (A) then
3643 elsif Nkind (A) = N_Type_Conversion then
3645 Operand : constant Node_Id := Expression (A);
3646 Operand_Typ : constant Entity_Id := Etype (Operand);
3647 Target_Typ : constant Entity_Id := A_Typ;
3650 if not Is_SPARK_Object_Reference (Operand) then
3651 Check_SPARK_Restriction
3652 ("object required", Operand);
3654 -- In formal mode, the only view conversions are those
3655 -- involving ancestor conversion of an extended type.
3658 (Is_Tagged_Type (Target_Typ)
3659 and then not Is_Class_Wide_Type (Target_Typ)
3660 and then Is_Tagged_Type (Operand_Typ)
3661 and then not Is_Class_Wide_Type (Operand_Typ)
3662 and then Is_Ancestor (Target_Typ, Operand_Typ))
3665 (F, E_Out_Parameter, E_In_Out_Parameter)
3667 Check_SPARK_Restriction
3668 ("ancestor conversion is the only permitted "
3669 & "view conversion", A);
3671 Check_SPARK_Restriction
3672 ("ancestor conversion required", A);
3681 Check_SPARK_Restriction ("object required", A);
3684 -- In formal mode, the only view conversions are those
3685 -- involving ancestor conversion of an extended type.
3687 elsif Nkind (A) = N_Type_Conversion
3688 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3690 Check_SPARK_Restriction
3691 ("ancestor conversion is the only permitted view "
3696 -- Save actual for subsequent check on order dependence, and
3697 -- indicate whether actual is modifiable. For AI05-0144-2.
3699 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3701 -- For mode IN, if actual is an entity, and the type of the formal
3702 -- has warnings suppressed, then we reset Never_Set_In_Source for
3703 -- the calling entity. The reason for this is to catch cases like
3704 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3705 -- uses trickery to modify an IN parameter.
3707 if Ekind (F) = E_In_Parameter
3708 and then Is_Entity_Name (A)
3709 and then Present (Entity (A))
3710 and then Ekind (Entity (A)) = E_Variable
3711 and then Has_Warnings_Off (F_Typ)
3713 Set_Never_Set_In_Source (Entity (A), False);
3716 -- Perform error checks for IN and IN OUT parameters
3718 if Ekind (F) /= E_Out_Parameter then
3720 -- Check unset reference. For scalar parameters, it is clearly
3721 -- wrong to pass an uninitialized value as either an IN or
3722 -- IN-OUT parameter. For composites, it is also clearly an
3723 -- error to pass a completely uninitialized value as an IN
3724 -- parameter, but the case of IN OUT is trickier. We prefer
3725 -- not to give a warning here. For example, suppose there is
3726 -- a routine that sets some component of a record to False.
3727 -- It is perfectly reasonable to make this IN-OUT and allow
3728 -- either initialized or uninitialized records to be passed
3731 -- For partially initialized composite values, we also avoid
3732 -- warnings, since it is quite likely that we are passing a
3733 -- partially initialized value and only the initialized fields
3734 -- will in fact be read in the subprogram.
3736 if Is_Scalar_Type (A_Typ)
3737 or else (Ekind (F) = E_In_Parameter
3738 and then not Is_Partially_Initialized_Type (A_Typ))
3740 Check_Unset_Reference (A);
3743 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3744 -- actual to a nested call, since this is case of reading an
3745 -- out parameter, which is not allowed.
3747 if Ada_Version = Ada_83
3748 and then Is_Entity_Name (A)
3749 and then Ekind (Entity (A)) = E_Out_Parameter
3751 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3755 -- Case of OUT or IN OUT parameter
3757 if Ekind (F) /= E_In_Parameter then
3759 -- For an Out parameter, check for useless assignment. Note
3760 -- that we can't set Last_Assignment this early, because we may
3761 -- kill current values in Resolve_Call, and that call would
3762 -- clobber the Last_Assignment field.
3764 -- Note: call Warn_On_Useless_Assignment before doing the check
3765 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3766 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3767 -- reflects the last assignment, not this one!
3769 if Ekind (F) = E_Out_Parameter then
3770 if Warn_On_Modified_As_Out_Parameter (F)
3771 and then Is_Entity_Name (A)
3772 and then Present (Entity (A))
3773 and then Comes_From_Source (N)
3775 Warn_On_Useless_Assignment (Entity (A), A);
3779 -- Validate the form of the actual. Note that the call to
3780 -- Is_OK_Variable_For_Out_Formal generates the required
3781 -- reference in this case.
3783 -- A call to an initialization procedure for an aggregate
3784 -- component may initialize a nested component of a constant
3785 -- designated object. In this context the object is variable.
3787 if not Is_OK_Variable_For_Out_Formal (A)
3788 and then not Is_Init_Proc (Nam)
3790 Error_Msg_NE ("actual for& must be a variable", A, F);
3793 -- What's the following about???
3795 if Is_Entity_Name (A) then
3796 Kill_Checks (Entity (A));
3802 if Etype (A) = Any_Type then
3803 Set_Etype (N, Any_Type);
3807 -- Apply appropriate range checks for in, out, and in-out
3808 -- parameters. Out and in-out parameters also need a separate
3809 -- check, if there is a type conversion, to make sure the return
3810 -- value meets the constraints of the variable before the
3813 -- Gigi looks at the check flag and uses the appropriate types.
3814 -- For now since one flag is used there is an optimization which
3815 -- might not be done in the In Out case since Gigi does not do
3816 -- any analysis. More thought required about this ???
3818 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3820 -- Apply predicate checks, unless this is a call to the
3821 -- predicate check function itself, which would cause an
3822 -- infinite recursion.
3824 if not (Ekind (Nam) = E_Function
3825 and then Has_Predicates (Nam))
3827 Apply_Predicate_Check (A, F_Typ);
3830 -- Apply required constraint checks
3832 if Is_Scalar_Type (Etype (A)) then
3833 Apply_Scalar_Range_Check (A, F_Typ);
3835 elsif Is_Array_Type (Etype (A)) then
3836 Apply_Length_Check (A, F_Typ);
3838 elsif Is_Record_Type (F_Typ)
3839 and then Has_Discriminants (F_Typ)
3840 and then Is_Constrained (F_Typ)
3841 and then (not Is_Derived_Type (F_Typ)
3842 or else Comes_From_Source (Nam))
3844 Apply_Discriminant_Check (A, F_Typ);
3846 elsif Is_Access_Type (F_Typ)
3847 and then Is_Array_Type (Designated_Type (F_Typ))
3848 and then Is_Constrained (Designated_Type (F_Typ))
3850 Apply_Length_Check (A, F_Typ);
3852 elsif Is_Access_Type (F_Typ)
3853 and then Has_Discriminants (Designated_Type (F_Typ))
3854 and then Is_Constrained (Designated_Type (F_Typ))
3856 Apply_Discriminant_Check (A, F_Typ);
3859 Apply_Range_Check (A, F_Typ);
3862 -- Ada 2005 (AI-231): Note that the controlling parameter case
3863 -- already existed in Ada 95, which is partially checked
3864 -- elsewhere (see Checks), and we don't want the warning
3865 -- message to differ.
3867 if Is_Access_Type (F_Typ)
3868 and then Can_Never_Be_Null (F_Typ)
3869 and then Known_Null (A)
3871 if Is_Controlling_Formal (F) then
3872 Apply_Compile_Time_Constraint_Error
3874 Msg => "null value not allowed here?",
3875 Reason => CE_Access_Check_Failed);
3877 elsif Ada_Version >= Ada_2005 then
3878 Apply_Compile_Time_Constraint_Error
3880 Msg => "(Ada 2005) null not allowed in "
3881 & "null-excluding formal?",
3882 Reason => CE_Null_Not_Allowed);
3887 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3888 if Nkind (A) = N_Type_Conversion then
3889 if Is_Scalar_Type (A_Typ) then
3890 Apply_Scalar_Range_Check
3891 (Expression (A), Etype (Expression (A)), A_Typ);
3894 (Expression (A), Etype (Expression (A)), A_Typ);
3898 if Is_Scalar_Type (F_Typ) then
3899 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3900 elsif Is_Array_Type (F_Typ)
3901 and then Ekind (F) = E_Out_Parameter
3903 Apply_Length_Check (A, F_Typ);
3905 Apply_Range_Check (A, A_Typ, F_Typ);
3910 -- An actual associated with an access parameter is implicitly
3911 -- converted to the anonymous access type of the formal and must
3912 -- satisfy the legality checks for access conversions.
3914 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3915 if not Valid_Conversion (A, F_Typ, A) then
3917 ("invalid implicit conversion for access parameter", A);
3921 -- Check bad case of atomic/volatile argument (RM C.6(12))
3923 if Is_By_Reference_Type (Etype (F))
3924 and then Comes_From_Source (N)
3926 if Is_Atomic_Object (A)
3927 and then not Is_Atomic (Etype (F))
3930 ("cannot pass atomic argument to non-atomic formal&",
3933 elsif Is_Volatile_Object (A)
3934 and then not Is_Volatile (Etype (F))
3937 ("cannot pass volatile argument to non-volatile formal&",
3942 -- Check that subprograms don't have improper controlling
3943 -- arguments (RM 3.9.2 (9)).
3945 -- A primitive operation may have an access parameter of an
3946 -- incomplete tagged type, but a dispatching call is illegal
3947 -- if the type is still incomplete.
3949 if Is_Controlling_Formal (F) then
3950 Set_Is_Controlling_Actual (A);
3952 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3954 Desig : constant Entity_Id := Designated_Type (Etype (F));
3956 if Ekind (Desig) = E_Incomplete_Type
3957 and then No (Full_View (Desig))
3958 and then No (Non_Limited_View (Desig))
3961 ("premature use of incomplete type& " &
3962 "in dispatching call", A, Desig);
3967 elsif Nkind (A) = N_Explicit_Dereference then
3968 Validate_Remote_Access_To_Class_Wide_Type (A);
3971 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3972 and then not Is_Class_Wide_Type (F_Typ)
3973 and then not Is_Controlling_Formal (F)
3975 Error_Msg_N ("class-wide argument not allowed here!", A);
3977 if Is_Subprogram (Nam)
3978 and then Comes_From_Source (Nam)
3980 Error_Msg_Node_2 := F_Typ;
3982 ("& is not a dispatching operation of &!", A, Nam);
3985 -- Apply the checks described in 3.10.2(27): if the context is a
3986 -- specific access-to-object, the actual cannot be class-wide.
3987 -- Use base type to exclude access_to_subprogram cases.
3989 elsif Is_Access_Type (A_Typ)
3990 and then Is_Access_Type (F_Typ)
3991 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
3992 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3993 or else (Nkind (A) = N_Attribute_Reference
3995 Is_Class_Wide_Type (Etype (Prefix (A)))))
3996 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3997 and then not Is_Controlling_Formal (F)
3999 -- Disable these checks for call to imported C++ subprograms
4002 (Is_Entity_Name (Name (N))
4003 and then Is_Imported (Entity (Name (N)))
4004 and then Convention (Entity (Name (N))) = Convention_CPP)
4007 ("access to class-wide argument not allowed here!", A);
4009 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4010 Error_Msg_Node_2 := Designated_Type (F_Typ);
4012 ("& is not a dispatching operation of &!", A, Nam);
4018 -- If it is a named association, treat the selector_name as a
4019 -- proper identifier, and mark the corresponding entity. Ignore
4020 -- this reference in Alfa mode, as it refers to an entity not in
4021 -- scope at the point of reference, so the reference should be
4022 -- ignored for computing effects of subprograms.
4024 if Nkind (Parent (A)) = N_Parameter_Association
4025 and then not Alfa_Mode
4027 Set_Entity (Selector_Name (Parent (A)), F);
4028 Generate_Reference (F, Selector_Name (Parent (A)));
4029 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4030 Generate_Reference (F_Typ, N, ' ');
4035 if Ekind (F) /= E_Out_Parameter then
4036 Check_Unset_Reference (A);
4041 -- Case where actual is not present
4049 end Resolve_Actuals;
4051 -----------------------
4052 -- Resolve_Allocator --
4053 -----------------------
4055 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4056 Desig_T : constant Entity_Id := Designated_Type (Typ);
4057 E : constant Node_Id := Expression (N);
4059 Discrim : Entity_Id;
4062 Assoc : Node_Id := Empty;
4065 procedure Check_Allocator_Discrim_Accessibility
4066 (Disc_Exp : Node_Id;
4067 Alloc_Typ : Entity_Id);
4068 -- Check that accessibility level associated with an access discriminant
4069 -- initialized in an allocator by the expression Disc_Exp is not deeper
4070 -- than the level of the allocator type Alloc_Typ. An error message is
4071 -- issued if this condition is violated. Specialized checks are done for
4072 -- the cases of a constraint expression which is an access attribute or
4073 -- an access discriminant.
4075 function In_Dispatching_Context return Boolean;
4076 -- If the allocator is an actual in a call, it is allowed to be class-
4077 -- wide when the context is not because it is a controlling actual.
4079 -------------------------------------------
4080 -- Check_Allocator_Discrim_Accessibility --
4081 -------------------------------------------
4083 procedure Check_Allocator_Discrim_Accessibility
4084 (Disc_Exp : Node_Id;
4085 Alloc_Typ : Entity_Id)
4088 if Type_Access_Level (Etype (Disc_Exp)) >
4089 Deepest_Type_Access_Level (Alloc_Typ)
4092 ("operand type has deeper level than allocator type", Disc_Exp);
4094 -- When the expression is an Access attribute the level of the prefix
4095 -- object must not be deeper than that of the allocator's type.
4097 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4098 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4100 and then Object_Access_Level (Prefix (Disc_Exp)) >
4101 Deepest_Type_Access_Level (Alloc_Typ)
4104 ("prefix of attribute has deeper level than allocator type",
4107 -- When the expression is an access discriminant the check is against
4108 -- the level of the prefix object.
4110 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4111 and then Nkind (Disc_Exp) = N_Selected_Component
4112 and then Object_Access_Level (Prefix (Disc_Exp)) >
4113 Deepest_Type_Access_Level (Alloc_Typ)
4116 ("access discriminant has deeper level than allocator type",
4119 -- All other cases are legal
4124 end Check_Allocator_Discrim_Accessibility;
4126 ----------------------------
4127 -- In_Dispatching_Context --
4128 ----------------------------
4130 function In_Dispatching_Context return Boolean is
4131 Par : constant Node_Id := Parent (N);
4135 Nkind_In (Par, N_Function_Call,
4136 N_Procedure_Call_Statement)
4137 and then Is_Entity_Name (Name (Par))
4138 and then Is_Dispatching_Operation (Entity (Name (Par)));
4139 end In_Dispatching_Context;
4141 -- Start of processing for Resolve_Allocator
4144 -- Replace general access with specific type
4146 if Ekind (Etype (N)) = E_Allocator_Type then
4147 Set_Etype (N, Base_Type (Typ));
4150 if Is_Abstract_Type (Typ) then
4151 Error_Msg_N ("type of allocator cannot be abstract", N);
4154 -- For qualified expression, resolve the expression using the
4155 -- given subtype (nothing to do for type mark, subtype indication)
4157 if Nkind (E) = N_Qualified_Expression then
4158 if Is_Class_Wide_Type (Etype (E))
4159 and then not Is_Class_Wide_Type (Desig_T)
4160 and then not In_Dispatching_Context
4163 ("class-wide allocator not allowed for this access type", N);
4166 Resolve (Expression (E), Etype (E));
4167 Check_Unset_Reference (Expression (E));
4169 -- A qualified expression requires an exact match of the type,
4170 -- class-wide matching is not allowed.
4172 if (Is_Class_Wide_Type (Etype (Expression (E)))
4173 or else Is_Class_Wide_Type (Etype (E)))
4174 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4176 Wrong_Type (Expression (E), Etype (E));
4179 -- A special accessibility check is needed for allocators that
4180 -- constrain access discriminants. The level of the type of the
4181 -- expression used to constrain an access discriminant cannot be
4182 -- deeper than the type of the allocator (in contrast to access
4183 -- parameters, where the level of the actual can be arbitrary).
4185 -- We can't use Valid_Conversion to perform this check because
4186 -- in general the type of the allocator is unrelated to the type
4187 -- of the access discriminant.
4189 if Ekind (Typ) /= E_Anonymous_Access_Type
4190 or else Is_Local_Anonymous_Access (Typ)
4192 Subtyp := Entity (Subtype_Mark (E));
4194 Aggr := Original_Node (Expression (E));
4196 if Has_Discriminants (Subtyp)
4197 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4199 Discrim := First_Discriminant (Base_Type (Subtyp));
4201 -- Get the first component expression of the aggregate
4203 if Present (Expressions (Aggr)) then
4204 Disc_Exp := First (Expressions (Aggr));
4206 elsif Present (Component_Associations (Aggr)) then
4207 Assoc := First (Component_Associations (Aggr));
4209 if Present (Assoc) then
4210 Disc_Exp := Expression (Assoc);
4219 while Present (Discrim) and then Present (Disc_Exp) loop
4220 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4221 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4224 Next_Discriminant (Discrim);
4226 if Present (Discrim) then
4227 if Present (Assoc) then
4229 Disc_Exp := Expression (Assoc);
4231 elsif Present (Next (Disc_Exp)) then
4235 Assoc := First (Component_Associations (Aggr));
4237 if Present (Assoc) then
4238 Disc_Exp := Expression (Assoc);
4248 -- For a subtype mark or subtype indication, freeze the subtype
4251 Freeze_Expression (E);
4253 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4255 ("initialization required for access-to-constant allocator", N);
4258 -- A special accessibility check is needed for allocators that
4259 -- constrain access discriminants. The level of the type of the
4260 -- expression used to constrain an access discriminant cannot be
4261 -- deeper than the type of the allocator (in contrast to access
4262 -- parameters, where the level of the actual can be arbitrary).
4263 -- We can't use Valid_Conversion to perform this check because
4264 -- in general the type of the allocator is unrelated to the type
4265 -- of the access discriminant.
4267 if Nkind (Original_Node (E)) = N_Subtype_Indication
4268 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4269 or else Is_Local_Anonymous_Access (Typ))
4271 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4273 if Has_Discriminants (Subtyp) then
4274 Discrim := First_Discriminant (Base_Type (Subtyp));
4275 Constr := First (Constraints (Constraint (Original_Node (E))));
4276 while Present (Discrim) and then Present (Constr) loop
4277 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4278 if Nkind (Constr) = N_Discriminant_Association then
4279 Disc_Exp := Original_Node (Expression (Constr));
4281 Disc_Exp := Original_Node (Constr);
4284 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4287 Next_Discriminant (Discrim);
4294 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4295 -- check that the level of the type of the created object is not deeper
4296 -- than the level of the allocator's access type, since extensions can
4297 -- now occur at deeper levels than their ancestor types. This is a
4298 -- static accessibility level check; a run-time check is also needed in
4299 -- the case of an initialized allocator with a class-wide argument (see
4300 -- Expand_Allocator_Expression).
4302 if Ada_Version >= Ada_2005
4303 and then Is_Class_Wide_Type (Desig_T)
4306 Exp_Typ : Entity_Id;
4309 if Nkind (E) = N_Qualified_Expression then
4310 Exp_Typ := Etype (E);
4311 elsif Nkind (E) = N_Subtype_Indication then
4312 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4314 Exp_Typ := Entity (E);
4317 if Type_Access_Level (Exp_Typ) >
4318 Deepest_Type_Access_Level (Typ)
4320 if In_Instance_Body then
4321 Error_Msg_N ("?type in allocator has deeper level than" &
4322 " designated class-wide type", E);
4323 Error_Msg_N ("\?Program_Error will be raised at run time",
4326 Make_Raise_Program_Error (Sloc (N),
4327 Reason => PE_Accessibility_Check_Failed));
4330 -- Do not apply Ada 2005 accessibility checks on a class-wide
4331 -- allocator if the type given in the allocator is a formal
4332 -- type. A run-time check will be performed in the instance.
4334 elsif not Is_Generic_Type (Exp_Typ) then
4335 Error_Msg_N ("type in allocator has deeper level than" &
4336 " designated class-wide type", E);
4342 -- Check for allocation from an empty storage pool
4344 if No_Pool_Assigned (Typ) then
4345 Error_Msg_N ("allocation from empty storage pool!", N);
4347 -- If the context is an unchecked conversion, as may happen within an
4348 -- inlined subprogram, the allocator is being resolved with its own
4349 -- anonymous type. In that case, if the target type has a specific
4350 -- storage pool, it must be inherited explicitly by the allocator type.
4352 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4353 and then No (Associated_Storage_Pool (Typ))
4355 Set_Associated_Storage_Pool
4356 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4359 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4360 Check_Restriction (No_Anonymous_Allocators, N);
4363 -- Check that an allocator with task parts isn't for a nested access
4364 -- type when restriction No_Task_Hierarchy applies.
4366 if not Is_Library_Level_Entity (Base_Type (Typ))
4367 and then Has_Task (Base_Type (Desig_T))
4369 Check_Restriction (No_Task_Hierarchy, N);
4372 -- An erroneous allocator may be rewritten as a raise Program_Error
4375 if Nkind (N) = N_Allocator then
4377 -- An anonymous access discriminant is the definition of a
4380 if Ekind (Typ) = E_Anonymous_Access_Type
4381 and then Nkind (Associated_Node_For_Itype (Typ)) =
4382 N_Discriminant_Specification
4385 Discr : constant Entity_Id :=
4386 Defining_Identifier (Associated_Node_For_Itype (Typ));
4389 -- Ada 2012 AI05-0052: If the designated type of the allocator
4390 -- is limited, then the allocator shall not be used to define
4391 -- the value of an access discriminant unless the discriminated
4392 -- type is immutably limited.
4394 if Ada_Version >= Ada_2012
4395 and then Is_Limited_Type (Desig_T)
4396 and then not Is_Immutably_Limited_Type (Scope (Discr))
4399 ("only immutably limited types can have anonymous "
4400 & "access discriminants designating a limited type", N);
4404 -- Avoid marking an allocator as a dynamic coextension if it is
4405 -- within a static construct.
4407 if not Is_Static_Coextension (N) then
4408 Set_Is_Dynamic_Coextension (N);
4411 -- Cleanup for potential static coextensions
4414 Set_Is_Dynamic_Coextension (N, False);
4415 Set_Is_Static_Coextension (N, False);
4419 -- Report a simple error: if the designated object is a local task,
4420 -- its body has not been seen yet, and its activation will fail an
4421 -- elaboration check.
4423 if Is_Task_Type (Desig_T)
4424 and then Scope (Base_Type (Desig_T)) = Current_Scope
4425 and then Is_Compilation_Unit (Current_Scope)
4426 and then Ekind (Current_Scope) = E_Package
4427 and then not In_Package_Body (Current_Scope)
4429 Error_Msg_N ("cannot activate task before body seen?", N);
4430 Error_Msg_N ("\Program_Error will be raised at run time?", N);
4433 -- Ada 2012 (AI05-0111-3): Issue a warning whenever allocating a task
4434 -- or a type containing tasks on a subpool since the deallocation of
4435 -- the subpool may lead to undefined task behavior. Perform the check
4436 -- only when the allocator has not been converted into a Program_Error
4437 -- due to a previous error.
4439 if Ada_Version >= Ada_2012
4440 and then Nkind (N) = N_Allocator
4441 and then Present (Subpool_Handle_Name (N))
4442 and then Has_Task (Desig_T)
4444 Error_Msg_N ("?allocation of task on subpool may lead to " &
4445 "undefined behavior", N);
4447 end Resolve_Allocator;
4449 ---------------------------
4450 -- Resolve_Arithmetic_Op --
4451 ---------------------------
4453 -- Used for resolving all arithmetic operators except exponentiation
4455 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4456 L : constant Node_Id := Left_Opnd (N);
4457 R : constant Node_Id := Right_Opnd (N);
4458 TL : constant Entity_Id := Base_Type (Etype (L));
4459 TR : constant Entity_Id := Base_Type (Etype (R));
4463 B_Typ : constant Entity_Id := Base_Type (Typ);
4464 -- We do the resolution using the base type, because intermediate values
4465 -- in expressions always are of the base type, not a subtype of it.
4467 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4468 -- Returns True if N is in a context that expects "any real type"
4470 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4471 -- Return True iff given type is Integer or universal real/integer
4473 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4474 -- Choose type of integer literal in fixed-point operation to conform
4475 -- to available fixed-point type. T is the type of the other operand,
4476 -- which is needed to determine the expected type of N.
4478 procedure Set_Operand_Type (N : Node_Id);
4479 -- Set operand type to T if universal
4481 -------------------------------
4482 -- Expected_Type_Is_Any_Real --
4483 -------------------------------
4485 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4487 -- N is the expression after "delta" in a fixed_point_definition;
4490 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4491 N_Decimal_Fixed_Point_Definition,
4493 -- N is one of the bounds in a real_range_specification;
4496 N_Real_Range_Specification,
4498 -- N is the expression of a delta_constraint;
4501 N_Delta_Constraint);
4502 end Expected_Type_Is_Any_Real;
4504 -----------------------------
4505 -- Is_Integer_Or_Universal --
4506 -----------------------------
4508 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4510 Index : Interp_Index;
4514 if not Is_Overloaded (N) then
4516 return Base_Type (T) = Base_Type (Standard_Integer)
4517 or else T = Universal_Integer
4518 or else T = Universal_Real;
4520 Get_First_Interp (N, Index, It);
4521 while Present (It.Typ) loop
4522 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4523 or else It.Typ = Universal_Integer
4524 or else It.Typ = Universal_Real
4529 Get_Next_Interp (Index, It);
4534 end Is_Integer_Or_Universal;
4536 ----------------------------
4537 -- Set_Mixed_Mode_Operand --
4538 ----------------------------
4540 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4541 Index : Interp_Index;
4545 if Universal_Interpretation (N) = Universal_Integer then
4547 -- A universal integer literal is resolved as standard integer
4548 -- except in the case of a fixed-point result, where we leave it
4549 -- as universal (to be handled by Exp_Fixd later on)
4551 if Is_Fixed_Point_Type (T) then
4552 Resolve (N, Universal_Integer);
4554 Resolve (N, Standard_Integer);
4557 elsif Universal_Interpretation (N) = Universal_Real
4558 and then (T = Base_Type (Standard_Integer)
4559 or else T = Universal_Integer
4560 or else T = Universal_Real)
4562 -- A universal real can appear in a fixed-type context. We resolve
4563 -- the literal with that context, even though this might raise an
4564 -- exception prematurely (the other operand may be zero).
4568 elsif Etype (N) = Base_Type (Standard_Integer)
4569 and then T = Universal_Real
4570 and then Is_Overloaded (N)
4572 -- Integer arg in mixed-mode operation. Resolve with universal
4573 -- type, in case preference rule must be applied.
4575 Resolve (N, Universal_Integer);
4578 and then B_Typ /= Universal_Fixed
4580 -- Not a mixed-mode operation, resolve with context
4584 elsif Etype (N) = Any_Fixed then
4586 -- N may itself be a mixed-mode operation, so use context type
4590 elsif Is_Fixed_Point_Type (T)
4591 and then B_Typ = Universal_Fixed
4592 and then Is_Overloaded (N)
4594 -- Must be (fixed * fixed) operation, operand must have one
4595 -- compatible interpretation.
4597 Resolve (N, Any_Fixed);
4599 elsif Is_Fixed_Point_Type (B_Typ)
4600 and then (T = Universal_Real
4601 or else Is_Fixed_Point_Type (T))
4602 and then Is_Overloaded (N)
4604 -- C * F(X) in a fixed context, where C is a real literal or a
4605 -- fixed-point expression. F must have either a fixed type
4606 -- interpretation or an integer interpretation, but not both.
4608 Get_First_Interp (N, Index, It);
4609 while Present (It.Typ) loop
4610 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4611 if Analyzed (N) then
4612 Error_Msg_N ("ambiguous operand in fixed operation", N);
4614 Resolve (N, Standard_Integer);
4617 elsif Is_Fixed_Point_Type (It.Typ) then
4618 if Analyzed (N) then
4619 Error_Msg_N ("ambiguous operand in fixed operation", N);
4621 Resolve (N, It.Typ);
4625 Get_Next_Interp (Index, It);
4628 -- Reanalyze the literal with the fixed type of the context. If
4629 -- context is Universal_Fixed, we are within a conversion, leave
4630 -- the literal as a universal real because there is no usable
4631 -- fixed type, and the target of the conversion plays no role in
4645 if B_Typ = Universal_Fixed
4646 and then Nkind (Op2) = N_Real_Literal
4648 T2 := Universal_Real;
4653 Set_Analyzed (Op2, False);
4660 end Set_Mixed_Mode_Operand;
4662 ----------------------
4663 -- Set_Operand_Type --
4664 ----------------------
4666 procedure Set_Operand_Type (N : Node_Id) is
4668 if Etype (N) = Universal_Integer
4669 or else Etype (N) = Universal_Real
4673 end Set_Operand_Type;
4675 -- Start of processing for Resolve_Arithmetic_Op
4678 if Comes_From_Source (N)
4679 and then Ekind (Entity (N)) = E_Function
4680 and then Is_Imported (Entity (N))
4681 and then Is_Intrinsic_Subprogram (Entity (N))
4683 Resolve_Intrinsic_Operator (N, Typ);
4686 -- Special-case for mixed-mode universal expressions or fixed point type
4687 -- operation: each argument is resolved separately. The same treatment
4688 -- is required if one of the operands of a fixed point operation is
4689 -- universal real, since in this case we don't do a conversion to a
4690 -- specific fixed-point type (instead the expander handles the case).
4692 -- Set the type of the node to its universal interpretation because
4693 -- legality checks on an exponentiation operand need the context.
4695 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4696 and then Present (Universal_Interpretation (L))
4697 and then Present (Universal_Interpretation (R))
4699 Set_Etype (N, B_Typ);
4700 Resolve (L, Universal_Interpretation (L));
4701 Resolve (R, Universal_Interpretation (R));
4703 elsif (B_Typ = Universal_Real
4704 or else Etype (N) = Universal_Fixed
4705 or else (Etype (N) = Any_Fixed
4706 and then Is_Fixed_Point_Type (B_Typ))
4707 or else (Is_Fixed_Point_Type (B_Typ)
4708 and then (Is_Integer_Or_Universal (L)
4710 Is_Integer_Or_Universal (R))))
4711 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4713 if TL = Universal_Integer or else TR = Universal_Integer then
4714 Check_For_Visible_Operator (N, B_Typ);
4717 -- If context is a fixed type and one operand is integer, the other
4718 -- is resolved with the type of the context.
4720 if Is_Fixed_Point_Type (B_Typ)
4721 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4722 or else TL = Universal_Integer)
4727 elsif Is_Fixed_Point_Type (B_Typ)
4728 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4729 or else TR = Universal_Integer)
4735 Set_Mixed_Mode_Operand (L, TR);
4736 Set_Mixed_Mode_Operand (R, TL);
4739 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4740 -- multiplying operators from being used when the expected type is
4741 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4742 -- some cases where the expected type is actually Any_Real;
4743 -- Expected_Type_Is_Any_Real takes care of that case.
4745 if Etype (N) = Universal_Fixed
4746 or else Etype (N) = Any_Fixed
4748 if B_Typ = Universal_Fixed
4749 and then not Expected_Type_Is_Any_Real (N)
4750 and then not Nkind_In (Parent (N), N_Type_Conversion,
4751 N_Unchecked_Type_Conversion)
4753 Error_Msg_N ("type cannot be determined from context!", N);
4754 Error_Msg_N ("\explicit conversion to result type required", N);
4756 Set_Etype (L, Any_Type);
4757 Set_Etype (R, Any_Type);
4760 if Ada_Version = Ada_83
4761 and then Etype (N) = Universal_Fixed
4763 Nkind_In (Parent (N), N_Type_Conversion,
4764 N_Unchecked_Type_Conversion)
4767 ("(Ada 83) fixed-point operation "
4768 & "needs explicit conversion", N);
4771 -- The expected type is "any real type" in contexts like
4773 -- type T is delta <universal_fixed-expression> ...
4775 -- in which case we need to set the type to Universal_Real
4776 -- so that static expression evaluation will work properly.
4778 if Expected_Type_Is_Any_Real (N) then
4779 Set_Etype (N, Universal_Real);
4781 Set_Etype (N, B_Typ);
4785 elsif Is_Fixed_Point_Type (B_Typ)
4786 and then (Is_Integer_Or_Universal (L)
4787 or else Nkind (L) = N_Real_Literal
4788 or else Nkind (R) = N_Real_Literal
4789 or else Is_Integer_Or_Universal (R))
4791 Set_Etype (N, B_Typ);
4793 elsif Etype (N) = Any_Fixed then
4795 -- If no previous errors, this is only possible if one operand is
4796 -- overloaded and the context is universal. Resolve as such.
4798 Set_Etype (N, B_Typ);
4802 if (TL = Universal_Integer or else TL = Universal_Real)
4804 (TR = Universal_Integer or else TR = Universal_Real)
4806 Check_For_Visible_Operator (N, B_Typ);
4809 -- If the context is Universal_Fixed and the operands are also
4810 -- universal fixed, this is an error, unless there is only one
4811 -- applicable fixed_point type (usually Duration).
4813 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4814 T := Unique_Fixed_Point_Type (N);
4816 if T = Any_Type then
4829 -- If one of the arguments was resolved to a non-universal type.
4830 -- label the result of the operation itself with the same type.
4831 -- Do the same for the universal argument, if any.
4833 T := Intersect_Types (L, R);
4834 Set_Etype (N, Base_Type (T));
4835 Set_Operand_Type (L);
4836 Set_Operand_Type (R);
4839 Generate_Operator_Reference (N, Typ);
4840 Eval_Arithmetic_Op (N);
4842 -- In SPARK, a multiplication or division with operands of fixed point
4843 -- types shall be qualified or explicitly converted to identify the
4846 if (Is_Fixed_Point_Type (Etype (L))
4847 or else Is_Fixed_Point_Type (Etype (R)))
4848 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4850 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4852 Check_SPARK_Restriction
4853 ("operation should be qualified or explicitly converted", N);
4856 -- Set overflow and division checking bit. Much cleverer code needed
4857 -- here eventually and perhaps the Resolve routines should be separated
4858 -- for the various arithmetic operations, since they will need
4859 -- different processing. ???
4861 if Nkind (N) in N_Op then
4862 if not Overflow_Checks_Suppressed (Etype (N)) then
4863 Enable_Overflow_Check (N);
4866 -- Give warning if explicit division by zero
4868 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4869 and then not Division_Checks_Suppressed (Etype (N))
4871 Rop := Right_Opnd (N);
4873 if Compile_Time_Known_Value (Rop)
4874 and then ((Is_Integer_Type (Etype (Rop))
4875 and then Expr_Value (Rop) = Uint_0)
4877 (Is_Real_Type (Etype (Rop))
4878 and then Expr_Value_R (Rop) = Ureal_0))
4880 -- Specialize the warning message according to the operation.
4881 -- The following warnings are for the case
4886 -- For division, we have two cases, for float division
4887 -- of an unconstrained float type, on a machine where
4888 -- Machine_Overflows is false, we don't get an exception
4889 -- at run-time, but rather an infinity or Nan. The Nan
4890 -- case is pretty obscure, so just warn about infinities.
4892 if Is_Floating_Point_Type (Typ)
4893 and then not Is_Constrained (Typ)
4894 and then not Machine_Overflows_On_Target
4897 ("float division by zero, " &
4898 "may generate '+'/'- infinity?", Right_Opnd (N));
4900 -- For all other cases, we get a Constraint_Error
4903 Apply_Compile_Time_Constraint_Error
4904 (N, "division by zero?", CE_Divide_By_Zero,
4905 Loc => Sloc (Right_Opnd (N)));
4909 Apply_Compile_Time_Constraint_Error
4910 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4911 Loc => Sloc (Right_Opnd (N)));
4914 Apply_Compile_Time_Constraint_Error
4915 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4916 Loc => Sloc (Right_Opnd (N)));
4918 -- Division by zero can only happen with division, rem,
4919 -- and mod operations.
4922 raise Program_Error;
4925 -- Otherwise just set the flag to check at run time
4928 Activate_Division_Check (N);
4932 -- If Restriction No_Implicit_Conditionals is active, then it is
4933 -- violated if either operand can be negative for mod, or for rem
4934 -- if both operands can be negative.
4936 if Restriction_Check_Required (No_Implicit_Conditionals)
4937 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4946 -- Set if corresponding operand might be negative
4950 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4951 LNeg := (not OK) or else Lo < 0;
4954 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4955 RNeg := (not OK) or else Lo < 0;
4957 -- Check if we will be generating conditionals. There are two
4958 -- cases where that can happen, first for REM, the only case
4959 -- is largest negative integer mod -1, where the division can
4960 -- overflow, but we still have to give the right result. The
4961 -- front end generates a test for this annoying case. Here we
4962 -- just test if both operands can be negative (that's what the
4963 -- expander does, so we match its logic here).
4965 -- The second case is mod where either operand can be negative.
4966 -- In this case, the back end has to generate additional tests.
4968 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4970 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4972 Check_Restriction (No_Implicit_Conditionals, N);
4978 Check_Unset_Reference (L);
4979 Check_Unset_Reference (R);
4980 end Resolve_Arithmetic_Op;
4986 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4987 Loc : constant Source_Ptr := Sloc (N);
4988 Subp : constant Node_Id := Name (N);
4996 function Same_Or_Aliased_Subprograms
4998 E : Entity_Id) return Boolean;
4999 -- Returns True if the subprogram entity S is the same as E or else
5000 -- S is an alias of E.
5002 ---------------------------------
5003 -- Same_Or_Aliased_Subprograms --
5004 ---------------------------------
5006 function Same_Or_Aliased_Subprograms
5008 E : Entity_Id) return Boolean
5010 Subp_Alias : constant Entity_Id := Alias (S);
5013 or else (Present (Subp_Alias) and then Subp_Alias = E);
5014 end Same_Or_Aliased_Subprograms;
5016 -- Start of processing for Resolve_Call
5019 -- The context imposes a unique interpretation with type Typ on a
5020 -- procedure or function call. Find the entity of the subprogram that
5021 -- yields the expected type, and propagate the corresponding formal
5022 -- constraints on the actuals. The caller has established that an
5023 -- interpretation exists, and emitted an error if not unique.
5025 -- First deal with the case of a call to an access-to-subprogram,
5026 -- dereference made explicit in Analyze_Call.
5028 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5029 if not Is_Overloaded (Subp) then
5030 Nam := Etype (Subp);
5033 -- Find the interpretation whose type (a subprogram type) has a
5034 -- return type that is compatible with the context. Analysis of
5035 -- the node has established that one exists.
5039 Get_First_Interp (Subp, I, It);
5040 while Present (It.Typ) loop
5041 if Covers (Typ, Etype (It.Typ)) then
5046 Get_Next_Interp (I, It);
5050 raise Program_Error;
5054 -- If the prefix is not an entity, then resolve it
5056 if not Is_Entity_Name (Subp) then
5057 Resolve (Subp, Nam);
5060 -- For an indirect call, we always invalidate checks, since we do not
5061 -- know whether the subprogram is local or global. Yes we could do
5062 -- better here, e.g. by knowing that there are no local subprograms,
5063 -- but it does not seem worth the effort. Similarly, we kill all
5064 -- knowledge of current constant values.
5066 Kill_Current_Values;
5068 -- If this is a procedure call which is really an entry call, do
5069 -- the conversion of the procedure call to an entry call. Protected
5070 -- operations use the same circuitry because the name in the call
5071 -- can be an arbitrary expression with special resolution rules.
5073 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5074 or else (Is_Entity_Name (Subp)
5075 and then Ekind (Entity (Subp)) = E_Entry)
5077 Resolve_Entry_Call (N, Typ);
5078 Check_Elab_Call (N);
5080 -- Kill checks and constant values, as above for indirect case
5081 -- Who knows what happens when another task is activated?
5083 Kill_Current_Values;
5086 -- Normal subprogram call with name established in Resolve
5088 elsif not (Is_Type (Entity (Subp))) then
5089 Nam := Entity (Subp);
5090 Set_Entity_With_Style_Check (Subp, Nam);
5092 -- Otherwise we must have the case of an overloaded call
5095 pragma Assert (Is_Overloaded (Subp));
5097 -- Initialize Nam to prevent warning (we know it will be assigned
5098 -- in the loop below, but the compiler does not know that).
5102 Get_First_Interp (Subp, I, It);
5103 while Present (It.Typ) loop
5104 if Covers (Typ, It.Typ) then
5106 Set_Entity_With_Style_Check (Subp, Nam);
5110 Get_Next_Interp (I, It);
5114 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5115 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5116 and then Nkind (Subp) /= N_Explicit_Dereference
5117 and then Present (Parameter_Associations (N))
5119 -- The prefix is a parameterless function call that returns an access
5120 -- to subprogram. If parameters are present in the current call, add
5121 -- add an explicit dereference. We use the base type here because
5122 -- within an instance these may be subtypes.
5124 -- The dereference is added either in Analyze_Call or here. Should
5125 -- be consolidated ???
5127 Set_Is_Overloaded (Subp, False);
5128 Set_Etype (Subp, Etype (Nam));
5129 Insert_Explicit_Dereference (Subp);
5130 Nam := Designated_Type (Etype (Nam));
5131 Resolve (Subp, Nam);
5134 -- Check that a call to Current_Task does not occur in an entry body
5136 if Is_RTE (Nam, RE_Current_Task) then
5145 -- Exclude calls that occur within the default of a formal
5146 -- parameter of the entry, since those are evaluated outside
5149 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5151 if Nkind (P) = N_Entry_Body
5152 or else (Nkind (P) = N_Subprogram_Body
5153 and then Is_Entry_Barrier_Function (P))
5157 ("?& should not be used in entry body (RM C.7(17))",
5160 ("\Program_Error will be raised at run time?", N, Nam);
5162 Make_Raise_Program_Error (Loc,
5163 Reason => PE_Current_Task_In_Entry_Body));
5164 Set_Etype (N, Rtype);
5171 -- Check that a procedure call does not occur in the context of the
5172 -- entry call statement of a conditional or timed entry call. Note that
5173 -- the case of a call to a subprogram renaming of an entry will also be
5174 -- rejected. The test for N not being an N_Entry_Call_Statement is
5175 -- defensive, covering the possibility that the processing of entry
5176 -- calls might reach this point due to later modifications of the code
5179 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5180 and then Nkind (N) /= N_Entry_Call_Statement
5181 and then Entry_Call_Statement (Parent (N)) = N
5183 if Ada_Version < Ada_2005 then
5184 Error_Msg_N ("entry call required in select statement", N);
5186 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5187 -- for a procedure_or_entry_call, the procedure_name or
5188 -- procedure_prefix of the procedure_call_statement shall denote
5189 -- an entry renamed by a procedure, or (a view of) a primitive
5190 -- subprogram of a limited interface whose first parameter is
5191 -- a controlling parameter.
5193 elsif Nkind (N) = N_Procedure_Call_Statement
5194 and then not Is_Renamed_Entry (Nam)
5195 and then not Is_Controlling_Limited_Procedure (Nam)
5198 ("entry call or dispatching primitive of interface required", N);
5202 -- Check that this is not a call to a protected procedure or entry from
5203 -- within a protected function.
5205 if Ekind (Current_Scope) = E_Function
5206 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5207 and then Ekind (Nam) /= E_Function
5208 and then Scope (Nam) = Scope (Current_Scope)
5210 Error_Msg_N ("within protected function, protected " &
5211 "object is constant", N);
5212 Error_Msg_N ("\cannot call operation that may modify it", N);
5215 -- Freeze the subprogram name if not in a spec-expression. Note that we
5216 -- freeze procedure calls as well as function calls. Procedure calls are
5217 -- not frozen according to the rules (RM 13.14(14)) because it is
5218 -- impossible to have a procedure call to a non-frozen procedure in pure
5219 -- Ada, but in the code that we generate in the expander, this rule
5220 -- needs extending because we can generate procedure calls that need
5223 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5224 Freeze_Expression (Subp);
5227 -- For a predefined operator, the type of the result is the type imposed
5228 -- by context, except for a predefined operation on universal fixed.
5229 -- Otherwise The type of the call is the type returned by the subprogram
5232 if Is_Predefined_Op (Nam) then
5233 if Etype (N) /= Universal_Fixed then
5237 -- If the subprogram returns an array type, and the context requires the
5238 -- component type of that array type, the node is really an indexing of
5239 -- the parameterless call. Resolve as such. A pathological case occurs
5240 -- when the type of the component is an access to the array type. In
5241 -- this case the call is truly ambiguous.
5243 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5245 ((Is_Array_Type (Etype (Nam))
5246 and then Covers (Typ, Component_Type (Etype (Nam))))
5247 or else (Is_Access_Type (Etype (Nam))
5248 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5252 Component_Type (Designated_Type (Etype (Nam))))))
5255 Index_Node : Node_Id;
5257 Ret_Type : constant Entity_Id := Etype (Nam);
5260 if Is_Access_Type (Ret_Type)
5261 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5264 ("cannot disambiguate function call and indexing", N);
5266 New_Subp := Relocate_Node (Subp);
5267 Set_Entity (Subp, Nam);
5269 if (Is_Array_Type (Ret_Type)
5270 and then Component_Type (Ret_Type) /= Any_Type)
5272 (Is_Access_Type (Ret_Type)
5274 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5276 if Needs_No_Actuals (Nam) then
5278 -- Indexed call to a parameterless function
5281 Make_Indexed_Component (Loc,
5283 Make_Function_Call (Loc,
5285 Expressions => Parameter_Associations (N));
5287 -- An Ada 2005 prefixed call to a primitive operation
5288 -- whose first parameter is the prefix. This prefix was
5289 -- prepended to the parameter list, which is actually a
5290 -- list of indexes. Remove the prefix in order to build
5291 -- the proper indexed component.
5294 Make_Indexed_Component (Loc,
5296 Make_Function_Call (Loc,
5298 Parameter_Associations =>
5300 (Remove_Head (Parameter_Associations (N)))),
5301 Expressions => Parameter_Associations (N));
5304 -- Preserve the parenthesis count of the node
5306 Set_Paren_Count (Index_Node, Paren_Count (N));
5308 -- Since we are correcting a node classification error made
5309 -- by the parser, we call Replace rather than Rewrite.
5311 Replace (N, Index_Node);
5313 Set_Etype (Prefix (N), Ret_Type);
5315 Resolve_Indexed_Component (N, Typ);
5316 Check_Elab_Call (Prefix (N));
5324 Set_Etype (N, Etype (Nam));
5327 -- In the case where the call is to an overloaded subprogram, Analyze
5328 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5329 -- such a case Normalize_Actuals needs to be called once more to order
5330 -- the actuals correctly. Otherwise the call will have the ordering
5331 -- given by the last overloaded subprogram whether this is the correct
5332 -- one being called or not.
5334 if Is_Overloaded (Subp) then
5335 Normalize_Actuals (N, Nam, False, Norm_OK);
5336 pragma Assert (Norm_OK);
5339 -- In any case, call is fully resolved now. Reset Overload flag, to
5340 -- prevent subsequent overload resolution if node is analyzed again
5342 Set_Is_Overloaded (Subp, False);
5343 Set_Is_Overloaded (N, False);
5345 -- If we are calling the current subprogram from immediately within its
5346 -- body, then that is the case where we can sometimes detect cases of
5347 -- infinite recursion statically. Do not try this in case restriction
5348 -- No_Recursion is in effect anyway, and do it only for source calls.
5350 if Comes_From_Source (N) then
5351 Scop := Current_Scope;
5353 -- Issue warning for possible infinite recursion in the absence
5354 -- of the No_Recursion restriction.
5356 if Same_Or_Aliased_Subprograms (Nam, Scop)
5357 and then not Restriction_Active (No_Recursion)
5358 and then Check_Infinite_Recursion (N)
5360 -- Here we detected and flagged an infinite recursion, so we do
5361 -- not need to test the case below for further warnings. Also we
5362 -- are all done if we now have a raise SE node.
5364 if Nkind (N) = N_Raise_Storage_Error then
5368 -- If call is to immediately containing subprogram, then check for
5369 -- the case of a possible run-time detectable infinite recursion.
5372 Scope_Loop : while Scop /= Standard_Standard loop
5373 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5375 -- Although in general case, recursion is not statically
5376 -- checkable, the case of calling an immediately containing
5377 -- subprogram is easy to catch.
5379 Check_Restriction (No_Recursion, N);
5381 -- If the recursive call is to a parameterless subprogram,
5382 -- then even if we can't statically detect infinite
5383 -- recursion, this is pretty suspicious, and we output a
5384 -- warning. Furthermore, we will try later to detect some
5385 -- cases here at run time by expanding checking code (see
5386 -- Detect_Infinite_Recursion in package Exp_Ch6).
5388 -- If the recursive call is within a handler, do not emit a
5389 -- warning, because this is a common idiom: loop until input
5390 -- is correct, catch illegal input in handler and restart.
5392 if No (First_Formal (Nam))
5393 and then Etype (Nam) = Standard_Void_Type
5394 and then not Error_Posted (N)
5395 and then Nkind (Parent (N)) /= N_Exception_Handler
5397 -- For the case of a procedure call. We give the message
5398 -- only if the call is the first statement in a sequence
5399 -- of statements, or if all previous statements are
5400 -- simple assignments. This is simply a heuristic to
5401 -- decrease false positives, without losing too many good
5402 -- warnings. The idea is that these previous statements
5403 -- may affect global variables the procedure depends on.
5404 -- We also exclude raise statements, that may arise from
5405 -- constraint checks and are probably unrelated to the
5406 -- intended control flow.
5408 if Nkind (N) = N_Procedure_Call_Statement
5409 and then Is_List_Member (N)
5415 while Present (P) loop
5417 N_Assignment_Statement,
5418 N_Raise_Constraint_Error)
5428 -- Do not give warning if we are in a conditional context
5431 K : constant Node_Kind := Nkind (Parent (N));
5433 if (K = N_Loop_Statement
5434 and then Present (Iteration_Scheme (Parent (N))))
5435 or else K = N_If_Statement
5436 or else K = N_Elsif_Part
5437 or else K = N_Case_Statement_Alternative
5443 -- Here warning is to be issued
5445 Set_Has_Recursive_Call (Nam);
5447 ("?possible infinite recursion!", N);
5449 ("\?Storage_Error may be raised at run time!", N);
5455 Scop := Scope (Scop);
5456 end loop Scope_Loop;
5460 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5462 Check_Obsolescent_2005_Entity (Nam, Subp);
5464 -- If subprogram name is a predefined operator, it was given in
5465 -- functional notation. Replace call node with operator node, so
5466 -- that actuals can be resolved appropriately.
5468 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5469 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5472 elsif Present (Alias (Nam))
5473 and then Is_Predefined_Op (Alias (Nam))
5475 Resolve_Actuals (N, Nam);
5476 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5480 -- Create a transient scope if the resulting type requires it
5482 -- There are several notable exceptions:
5484 -- a) In init procs, the transient scope overhead is not needed, and is
5485 -- even incorrect when the call is a nested initialization call for a
5486 -- component whose expansion may generate adjust calls. However, if the
5487 -- call is some other procedure call within an initialization procedure
5488 -- (for example a call to Create_Task in the init_proc of the task
5489 -- run-time record) a transient scope must be created around this call.
5491 -- b) Enumeration literal pseudo-calls need no transient scope
5493 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5494 -- functions) do not use the secondary stack even though the return
5495 -- type may be unconstrained.
5497 -- d) Calls to a build-in-place function, since such functions may
5498 -- allocate their result directly in a target object, and cases where
5499 -- the result does get allocated in the secondary stack are checked for
5500 -- within the specialized Exp_Ch6 procedures for expanding those
5501 -- build-in-place calls.
5503 -- e) If the subprogram is marked Inline_Always, then even if it returns
5504 -- an unconstrained type the call does not require use of the secondary
5505 -- stack. However, inlining will only take place if the body to inline
5506 -- is already present. It may not be available if e.g. the subprogram is
5507 -- declared in a child instance.
5509 -- If this is an initialization call for a type whose construction
5510 -- uses the secondary stack, and it is not a nested call to initialize
5511 -- a component, we do need to create a transient scope for it. We
5512 -- check for this by traversing the type in Check_Initialization_Call.
5515 and then Has_Pragma_Inline_Always (Nam)
5516 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5517 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5521 elsif Ekind (Nam) = E_Enumeration_Literal
5522 or else Is_Build_In_Place_Function (Nam)
5523 or else Is_Intrinsic_Subprogram (Nam)
5527 elsif Full_Expander_Active
5528 and then Is_Type (Etype (Nam))
5529 and then Requires_Transient_Scope (Etype (Nam))
5531 (not Within_Init_Proc
5533 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5535 Establish_Transient_Scope (N, Sec_Stack => True);
5537 -- If the call appears within the bounds of a loop, it will
5538 -- be rewritten and reanalyzed, nothing left to do here.
5540 if Nkind (N) /= N_Function_Call then
5544 elsif Is_Init_Proc (Nam)
5545 and then not Within_Init_Proc
5547 Check_Initialization_Call (N, Nam);
5550 -- A protected function cannot be called within the definition of the
5551 -- enclosing protected type.
5553 if Is_Protected_Type (Scope (Nam))
5554 and then In_Open_Scopes (Scope (Nam))
5555 and then not Has_Completion (Scope (Nam))
5558 ("& cannot be called before end of protected definition", N, Nam);
5561 -- Propagate interpretation to actuals, and add default expressions
5564 if Present (First_Formal (Nam)) then
5565 Resolve_Actuals (N, Nam);
5567 -- Overloaded literals are rewritten as function calls, for purpose of
5568 -- resolution. After resolution, we can replace the call with the
5571 elsif Ekind (Nam) = E_Enumeration_Literal then
5572 Copy_Node (Subp, N);
5573 Resolve_Entity_Name (N, Typ);
5575 -- Avoid validation, since it is a static function call
5577 Generate_Reference (Nam, Subp);
5581 -- If the subprogram is not global, then kill all saved values and
5582 -- checks. This is a bit conservative, since in many cases we could do
5583 -- better, but it is not worth the effort. Similarly, we kill constant
5584 -- values. However we do not need to do this for internal entities
5585 -- (unless they are inherited user-defined subprograms), since they
5586 -- are not in the business of molesting local values.
5588 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5589 -- kill all checks and values for calls to global subprograms. This
5590 -- takes care of the case where an access to a local subprogram is
5591 -- taken, and could be passed directly or indirectly and then called
5592 -- from almost any context.
5594 -- Note: we do not do this step till after resolving the actuals. That
5595 -- way we still take advantage of the current value information while
5596 -- scanning the actuals.
5598 -- We suppress killing values if we are processing the nodes associated
5599 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5600 -- type kills all the values as part of analyzing the code that
5601 -- initializes the dispatch tables.
5603 if Inside_Freezing_Actions = 0
5604 and then (not Is_Library_Level_Entity (Nam)
5605 or else Suppress_Value_Tracking_On_Call
5606 (Nearest_Dynamic_Scope (Current_Scope)))
5607 and then (Comes_From_Source (Nam)
5608 or else (Present (Alias (Nam))
5609 and then Comes_From_Source (Alias (Nam))))
5611 Kill_Current_Values;
5614 -- If we are warning about unread OUT parameters, this is the place to
5615 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5616 -- after the above call to Kill_Current_Values (since that call clears
5617 -- the Last_Assignment field of all local variables).
5619 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5620 and then Comes_From_Source (N)
5621 and then In_Extended_Main_Source_Unit (N)
5628 F := First_Formal (Nam);
5629 A := First_Actual (N);
5630 while Present (F) and then Present (A) loop
5631 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5632 and then Warn_On_Modified_As_Out_Parameter (F)
5633 and then Is_Entity_Name (A)
5634 and then Present (Entity (A))
5635 and then Comes_From_Source (N)
5636 and then Safe_To_Capture_Value (N, Entity (A))
5638 Set_Last_Assignment (Entity (A), A);
5647 -- If the subprogram is a primitive operation, check whether or not
5648 -- it is a correct dispatching call.
5650 if Is_Overloadable (Nam)
5651 and then Is_Dispatching_Operation (Nam)
5653 Check_Dispatching_Call (N);
5655 elsif Ekind (Nam) /= E_Subprogram_Type
5656 and then Is_Abstract_Subprogram (Nam)
5657 and then not In_Instance
5659 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5662 -- If this is a dispatching call, generate the appropriate reference,
5663 -- for better source navigation in GPS.
5665 if Is_Overloadable (Nam)
5666 and then Present (Controlling_Argument (N))
5668 Generate_Reference (Nam, Subp, 'R');
5670 -- Normal case, not a dispatching call: generate a call reference
5673 Generate_Reference (Nam, Subp, 's');
5676 if Is_Intrinsic_Subprogram (Nam) then
5677 Check_Intrinsic_Call (N);
5680 -- Check for violation of restriction No_Specific_Termination_Handlers
5681 -- and warn on a potentially blocking call to Abort_Task.
5683 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5684 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5686 Is_RTE (Nam, RE_Specific_Handler))
5688 Check_Restriction (No_Specific_Termination_Handlers, N);
5690 elsif Is_RTE (Nam, RE_Abort_Task) then
5691 Check_Potentially_Blocking_Operation (N);
5694 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5695 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5696 -- need to check the second argument to determine whether it is an
5697 -- absolute or relative timing event.
5699 if Restriction_Check_Required (No_Relative_Delay)
5700 and then Is_RTE (Nam, RE_Set_Handler)
5701 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5703 Check_Restriction (No_Relative_Delay, N);
5706 -- Issue an error for a call to an eliminated subprogram. We skip this
5707 -- in a spec expression, e.g. a call in a default parameter value, since
5708 -- we are not really doing a call at this time. That's important because
5709 -- the spec expression may itself belong to an eliminated subprogram.
5711 if not In_Spec_Expression then
5712 Check_For_Eliminated_Subprogram (Subp, Nam);
5715 -- In formal mode, the primitive operations of a tagged type or type
5716 -- extension do not include functions that return the tagged type.
5718 -- Commented out as the call to Is_Inherited_Operation_For_Type may
5719 -- cause an error because the type entity of the parent node of
5720 -- Entity (Name (N) may not be set. ???
5721 -- So why not just add a guard ???
5723 -- if Nkind (N) = N_Function_Call
5724 -- and then Is_Tagged_Type (Etype (N))
5725 -- and then Is_Entity_Name (Name (N))
5726 -- and then Is_Inherited_Operation_For_Type
5727 -- (Entity (Name (N)), Etype (N))
5729 -- Check_SPARK_Restriction ("function not inherited", N);
5732 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5733 -- class-wide and the call dispatches on result in a context that does
5734 -- not provide a tag, the call raises Program_Error.
5736 if Nkind (N) = N_Function_Call
5737 and then In_Instance
5738 and then Is_Generic_Actual_Type (Typ)
5739 and then Is_Class_Wide_Type (Typ)
5740 and then Has_Controlling_Result (Nam)
5741 and then Nkind (Parent (N)) = N_Object_Declaration
5743 -- Verify that none of the formals are controlling
5746 Call_OK : Boolean := False;
5750 F := First_Formal (Nam);
5751 while Present (F) loop
5752 if Is_Controlling_Formal (F) then
5761 Error_Msg_N ("!? cannot determine tag of result", N);
5762 Error_Msg_N ("!? Program_Error will be raised", N);
5764 Make_Raise_Program_Error (Sloc (N),
5765 Reason => PE_Explicit_Raise));
5770 -- All done, evaluate call and deal with elaboration issues
5773 Check_Elab_Call (N);
5774 Warn_On_Overlapping_Actuals (Nam, N);
5777 -----------------------------
5778 -- Resolve_Case_Expression --
5779 -----------------------------
5781 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5785 Alt := First (Alternatives (N));
5786 while Present (Alt) loop
5787 Resolve (Expression (Alt), Typ);
5792 Eval_Case_Expression (N);
5793 end Resolve_Case_Expression;
5795 -------------------------------
5796 -- Resolve_Character_Literal --
5797 -------------------------------
5799 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5800 B_Typ : constant Entity_Id := Base_Type (Typ);
5804 -- Verify that the character does belong to the type of the context
5806 Set_Etype (N, B_Typ);
5807 Eval_Character_Literal (N);
5809 -- Wide_Wide_Character literals must always be defined, since the set
5810 -- of wide wide character literals is complete, i.e. if a character
5811 -- literal is accepted by the parser, then it is OK for wide wide
5812 -- character (out of range character literals are rejected).
5814 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5817 -- Always accept character literal for type Any_Character, which
5818 -- occurs in error situations and in comparisons of literals, both
5819 -- of which should accept all literals.
5821 elsif B_Typ = Any_Character then
5824 -- For Standard.Character or a type derived from it, check that the
5825 -- literal is in range.
5827 elsif Root_Type (B_Typ) = Standard_Character then
5828 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5832 -- For Standard.Wide_Character or a type derived from it, check that the
5833 -- literal is in range.
5835 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5836 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5840 -- For Standard.Wide_Wide_Character or a type derived from it, we
5841 -- know the literal is in range, since the parser checked!
5843 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5846 -- If the entity is already set, this has already been resolved in a
5847 -- generic context, or comes from expansion. Nothing else to do.
5849 elsif Present (Entity (N)) then
5852 -- Otherwise we have a user defined character type, and we can use the
5853 -- standard visibility mechanisms to locate the referenced entity.
5856 C := Current_Entity (N);
5857 while Present (C) loop
5858 if Etype (C) = B_Typ then
5859 Set_Entity_With_Style_Check (N, C);
5860 Generate_Reference (C, N);
5868 -- If we fall through, then the literal does not match any of the
5869 -- entries of the enumeration type. This isn't just a constraint error
5870 -- situation, it is an illegality (see RM 4.2).
5873 ("character not defined for }", N, First_Subtype (B_Typ));
5874 end Resolve_Character_Literal;
5876 ---------------------------
5877 -- Resolve_Comparison_Op --
5878 ---------------------------
5880 -- Context requires a boolean type, and plays no role in resolution.
5881 -- Processing identical to that for equality operators. The result type is
5882 -- the base type, which matters when pathological subtypes of booleans with
5883 -- limited ranges are used.
5885 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5886 L : constant Node_Id := Left_Opnd (N);
5887 R : constant Node_Id := Right_Opnd (N);
5891 -- If this is an intrinsic operation which is not predefined, use the
5892 -- types of its declared arguments to resolve the possibly overloaded
5893 -- operands. Otherwise the operands are unambiguous and specify the
5896 if Scope (Entity (N)) /= Standard_Standard then
5897 T := Etype (First_Entity (Entity (N)));
5900 T := Find_Unique_Type (L, R);
5902 if T = Any_Fixed then
5903 T := Unique_Fixed_Point_Type (L);
5907 Set_Etype (N, Base_Type (Typ));
5908 Generate_Reference (T, N, ' ');
5910 -- Skip remaining processing if already set to Any_Type
5912 if T = Any_Type then
5916 -- Deal with other error cases
5918 if T = Any_String or else
5919 T = Any_Composite or else
5922 if T = Any_Character then
5923 Ambiguous_Character (L);
5925 Error_Msg_N ("ambiguous operands for comparison", N);
5928 Set_Etype (N, Any_Type);
5932 -- Resolve the operands if types OK
5936 Check_Unset_Reference (L);
5937 Check_Unset_Reference (R);
5938 Generate_Operator_Reference (N, T);
5939 Check_Low_Bound_Tested (N);
5941 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
5942 -- types or array types except String.
5944 if Is_Boolean_Type (T) then
5945 Check_SPARK_Restriction
5946 ("comparison is not defined on Boolean type", N);
5948 elsif Is_Array_Type (T)
5949 and then Base_Type (T) /= Standard_String
5951 Check_SPARK_Restriction
5952 ("comparison is not defined on array types other than String", N);
5955 -- Check comparison on unordered enumeration
5957 if Comes_From_Source (N)
5958 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
5960 Error_Msg_N ("comparison on unordered enumeration type?", N);
5963 -- Evaluate the relation (note we do this after the above check since
5964 -- this Eval call may change N to True/False.
5966 Eval_Relational_Op (N);
5967 end Resolve_Comparison_Op;
5969 ------------------------------------
5970 -- Resolve_Conditional_Expression --
5971 ------------------------------------
5973 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5974 Condition : constant Node_Id := First (Expressions (N));
5975 Then_Expr : constant Node_Id := Next (Condition);
5976 Else_Expr : Node_Id := Next (Then_Expr);
5979 Resolve (Condition, Any_Boolean);
5980 Resolve (Then_Expr, Typ);
5982 -- If ELSE expression present, just resolve using the determined type
5984 if Present (Else_Expr) then
5985 Resolve (Else_Expr, Typ);
5987 -- If no ELSE expression is present, root type must be Standard.Boolean
5988 -- and we provide a Standard.True result converted to the appropriate
5989 -- Boolean type (in case it is a derived boolean type).
5991 elsif Root_Type (Typ) = Standard_Boolean then
5993 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
5994 Analyze_And_Resolve (Else_Expr, Typ);
5995 Append_To (Expressions (N), Else_Expr);
5998 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
5999 Append_To (Expressions (N), Error);
6003 Eval_Conditional_Expression (N);
6004 end Resolve_Conditional_Expression;
6006 -----------------------------------------
6007 -- Resolve_Discrete_Subtype_Indication --
6008 -----------------------------------------
6010 procedure Resolve_Discrete_Subtype_Indication
6018 Analyze (Subtype_Mark (N));
6019 S := Entity (Subtype_Mark (N));
6021 if Nkind (Constraint (N)) /= N_Range_Constraint then
6022 Error_Msg_N ("expect range constraint for discrete type", N);
6023 Set_Etype (N, Any_Type);
6026 R := Range_Expression (Constraint (N));
6034 if Base_Type (S) /= Base_Type (Typ) then
6036 ("expect subtype of }", N, First_Subtype (Typ));
6038 -- Rewrite the constraint as a range of Typ
6039 -- to allow compilation to proceed further.
6042 Rewrite (Low_Bound (R),
6043 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6044 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6045 Attribute_Name => Name_First));
6046 Rewrite (High_Bound (R),
6047 Make_Attribute_Reference (Sloc (High_Bound (R)),
6048 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6049 Attribute_Name => Name_First));
6053 Set_Etype (N, Etype (R));
6055 -- Additionally, we must check that the bounds are compatible
6056 -- with the given subtype, which might be different from the
6057 -- type of the context.
6059 Apply_Range_Check (R, S);
6061 -- ??? If the above check statically detects a Constraint_Error
6062 -- it replaces the offending bound(s) of the range R with a
6063 -- Constraint_Error node. When the itype which uses these bounds
6064 -- is frozen the resulting call to Duplicate_Subexpr generates
6065 -- a new temporary for the bounds.
6067 -- Unfortunately there are other itypes that are also made depend
6068 -- on these bounds, so when Duplicate_Subexpr is called they get
6069 -- a forward reference to the newly created temporaries and Gigi
6070 -- aborts on such forward references. This is probably sign of a
6071 -- more fundamental problem somewhere else in either the order of
6072 -- itype freezing or the way certain itypes are constructed.
6074 -- To get around this problem we call Remove_Side_Effects right
6075 -- away if either bounds of R are a Constraint_Error.
6078 L : constant Node_Id := Low_Bound (R);
6079 H : constant Node_Id := High_Bound (R);
6082 if Nkind (L) = N_Raise_Constraint_Error then
6083 Remove_Side_Effects (L);
6086 if Nkind (H) = N_Raise_Constraint_Error then
6087 Remove_Side_Effects (H);
6091 Check_Unset_Reference (Low_Bound (R));
6092 Check_Unset_Reference (High_Bound (R));
6095 end Resolve_Discrete_Subtype_Indication;
6097 -------------------------
6098 -- Resolve_Entity_Name --
6099 -------------------------
6101 -- Used to resolve identifiers and expanded names
6103 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6104 E : constant Entity_Id := Entity (N);
6107 -- If garbage from errors, set to Any_Type and return
6109 if No (E) and then Total_Errors_Detected /= 0 then
6110 Set_Etype (N, Any_Type);
6114 -- Replace named numbers by corresponding literals. Note that this is
6115 -- the one case where Resolve_Entity_Name must reset the Etype, since
6116 -- it is currently marked as universal.
6118 if Ekind (E) = E_Named_Integer then
6120 Eval_Named_Integer (N);
6122 elsif Ekind (E) = E_Named_Real then
6124 Eval_Named_Real (N);
6126 -- For enumeration literals, we need to make sure that a proper style
6127 -- check is done, since such literals are overloaded, and thus we did
6128 -- not do a style check during the first phase of analysis.
6130 elsif Ekind (E) = E_Enumeration_Literal then
6131 Set_Entity_With_Style_Check (N, E);
6132 Eval_Entity_Name (N);
6134 -- Case of subtype name appearing as an operand in expression
6136 elsif Is_Type (E) then
6138 -- Allow use of subtype if it is a concurrent type where we are
6139 -- currently inside the body. This will eventually be expanded into a
6140 -- call to Self (for tasks) or _object (for protected objects). Any
6141 -- other use of a subtype is invalid.
6143 if Is_Concurrent_Type (E)
6144 and then In_Open_Scopes (E)
6148 -- Any other use is an error
6152 ("invalid use of subtype mark in expression or call", N);
6155 -- Check discriminant use if entity is discriminant in current scope,
6156 -- i.e. discriminant of record or concurrent type currently being
6157 -- analyzed. Uses in corresponding body are unrestricted.
6159 elsif Ekind (E) = E_Discriminant
6160 and then Scope (E) = Current_Scope
6161 and then not Has_Completion (Current_Scope)
6163 Check_Discriminant_Use (N);
6165 -- A parameterless generic function cannot appear in a context that
6166 -- requires resolution.
6168 elsif Ekind (E) = E_Generic_Function then
6169 Error_Msg_N ("illegal use of generic function", N);
6171 elsif Ekind (E) = E_Out_Parameter
6172 and then Ada_Version = Ada_83
6173 and then (Nkind (Parent (N)) in N_Op
6174 or else (Nkind (Parent (N)) = N_Assignment_Statement
6175 and then N = Expression (Parent (N)))
6176 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6178 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6180 -- In all other cases, just do the possible static evaluation
6183 -- A deferred constant that appears in an expression must have a
6184 -- completion, unless it has been removed by in-place expansion of
6187 if Ekind (E) = E_Constant
6188 and then Comes_From_Source (E)
6189 and then No (Constant_Value (E))
6190 and then Is_Frozen (Etype (E))
6191 and then not In_Spec_Expression
6192 and then not Is_Imported (E)
6194 if No_Initialization (Parent (E))
6195 or else (Present (Full_View (E))
6196 and then No_Initialization (Parent (Full_View (E))))
6201 "deferred constant is frozen before completion", N);
6205 Eval_Entity_Name (N);
6207 end Resolve_Entity_Name;
6213 procedure Resolve_Entry (Entry_Name : Node_Id) is
6214 Loc : constant Source_Ptr := Sloc (Entry_Name);
6222 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6223 -- If the bounds of the entry family being called depend on task
6224 -- discriminants, build a new index subtype where a discriminant is
6225 -- replaced with the value of the discriminant of the target task.
6226 -- The target task is the prefix of the entry name in the call.
6228 -----------------------
6229 -- Actual_Index_Type --
6230 -----------------------
6232 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6233 Typ : constant Entity_Id := Entry_Index_Type (E);
6234 Tsk : constant Entity_Id := Scope (E);
6235 Lo : constant Node_Id := Type_Low_Bound (Typ);
6236 Hi : constant Node_Id := Type_High_Bound (Typ);
6239 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6240 -- If the bound is given by a discriminant, replace with a reference
6241 -- to the discriminant of the same name in the target task. If the
6242 -- entry name is the target of a requeue statement and the entry is
6243 -- in the current protected object, the bound to be used is the
6244 -- discriminal of the object (see Apply_Range_Checks for details of
6245 -- the transformation).
6247 -----------------------------
6248 -- Actual_Discriminant_Ref --
6249 -----------------------------
6251 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6252 Typ : constant Entity_Id := Etype (Bound);
6256 Remove_Side_Effects (Bound);
6258 if not Is_Entity_Name (Bound)
6259 or else Ekind (Entity (Bound)) /= E_Discriminant
6263 elsif Is_Protected_Type (Tsk)
6264 and then In_Open_Scopes (Tsk)
6265 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6267 -- Note: here Bound denotes a discriminant of the corresponding
6268 -- record type tskV, whose discriminal is a formal of the
6269 -- init-proc tskVIP. What we want is the body discriminal,
6270 -- which is associated to the discriminant of the original
6271 -- concurrent type tsk.
6273 return New_Occurrence_Of
6274 (Find_Body_Discriminal (Entity (Bound)), Loc);
6278 Make_Selected_Component (Loc,
6279 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6280 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6285 end Actual_Discriminant_Ref;
6287 -- Start of processing for Actual_Index_Type
6290 if not Has_Discriminants (Tsk)
6291 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6293 return Entry_Index_Type (E);
6296 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6297 Set_Etype (New_T, Base_Type (Typ));
6298 Set_Size_Info (New_T, Typ);
6299 Set_RM_Size (New_T, RM_Size (Typ));
6300 Set_Scalar_Range (New_T,
6301 Make_Range (Sloc (Entry_Name),
6302 Low_Bound => Actual_Discriminant_Ref (Lo),
6303 High_Bound => Actual_Discriminant_Ref (Hi)));
6307 end Actual_Index_Type;
6309 -- Start of processing of Resolve_Entry
6312 -- Find name of entry being called, and resolve prefix of name with its
6313 -- own type. The prefix can be overloaded, and the name and signature of
6314 -- the entry must be taken into account.
6316 if Nkind (Entry_Name) = N_Indexed_Component then
6318 -- Case of dealing with entry family within the current tasks
6320 E_Name := Prefix (Entry_Name);
6323 E_Name := Entry_Name;
6326 if Is_Entity_Name (E_Name) then
6328 -- Entry call to an entry (or entry family) in the current task. This
6329 -- is legal even though the task will deadlock. Rewrite as call to
6332 -- This can also be a call to an entry in an enclosing task. If this
6333 -- is a single task, we have to retrieve its name, because the scope
6334 -- of the entry is the task type, not the object. If the enclosing
6335 -- task is a task type, the identity of the task is given by its own
6338 -- Finally this can be a requeue on an entry of the same task or
6339 -- protected object.
6341 S := Scope (Entity (E_Name));
6343 for J in reverse 0 .. Scope_Stack.Last loop
6344 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6345 and then not Comes_From_Source (S)
6347 -- S is an enclosing task or protected object. The concurrent
6348 -- declaration has been converted into a type declaration, and
6349 -- the object itself has an object declaration that follows
6350 -- the type in the same declarative part.
6352 Tsk := Next_Entity (S);
6353 while Etype (Tsk) /= S loop
6360 elsif S = Scope_Stack.Table (J).Entity then
6362 -- Call to current task. Will be transformed into call to Self
6370 Make_Selected_Component (Loc,
6371 Prefix => New_Occurrence_Of (S, Loc),
6373 New_Occurrence_Of (Entity (E_Name), Loc));
6374 Rewrite (E_Name, New_N);
6377 elsif Nkind (Entry_Name) = N_Selected_Component
6378 and then Is_Overloaded (Prefix (Entry_Name))
6380 -- Use the entry name (which must be unique at this point) to find
6381 -- the prefix that returns the corresponding task/protected type.
6384 Pref : constant Node_Id := Prefix (Entry_Name);
6385 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6390 Get_First_Interp (Pref, I, It);
6391 while Present (It.Typ) loop
6392 if Scope (Ent) = It.Typ then
6393 Set_Etype (Pref, It.Typ);
6397 Get_Next_Interp (I, It);
6402 if Nkind (Entry_Name) = N_Selected_Component then
6403 Resolve (Prefix (Entry_Name));
6405 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6406 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6407 Resolve (Prefix (Prefix (Entry_Name)));
6408 Index := First (Expressions (Entry_Name));
6409 Resolve (Index, Entry_Index_Type (Nam));
6411 -- Up to this point the expression could have been the actual in a
6412 -- simple entry call, and be given by a named association.
6414 if Nkind (Index) = N_Parameter_Association then
6415 Error_Msg_N ("expect expression for entry index", Index);
6417 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6422 ------------------------
6423 -- Resolve_Entry_Call --
6424 ------------------------
6426 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6427 Entry_Name : constant Node_Id := Name (N);
6428 Loc : constant Source_Ptr := Sloc (Entry_Name);
6430 First_Named : Node_Id;
6437 -- We kill all checks here, because it does not seem worth the effort to
6438 -- do anything better, an entry call is a big operation.
6442 -- Processing of the name is similar for entry calls and protected
6443 -- operation calls. Once the entity is determined, we can complete
6444 -- the resolution of the actuals.
6446 -- The selector may be overloaded, in the case of a protected object
6447 -- with overloaded functions. The type of the context is used for
6450 if Nkind (Entry_Name) = N_Selected_Component
6451 and then Is_Overloaded (Selector_Name (Entry_Name))
6452 and then Typ /= Standard_Void_Type
6459 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6460 while Present (It.Typ) loop
6461 if Covers (Typ, It.Typ) then
6462 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6463 Set_Etype (Entry_Name, It.Typ);
6465 Generate_Reference (It.Typ, N, ' ');
6468 Get_Next_Interp (I, It);
6473 Resolve_Entry (Entry_Name);
6475 if Nkind (Entry_Name) = N_Selected_Component then
6477 -- Simple entry call
6479 Nam := Entity (Selector_Name (Entry_Name));
6480 Obj := Prefix (Entry_Name);
6481 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6483 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6485 -- Call to member of entry family
6487 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6488 Obj := Prefix (Prefix (Entry_Name));
6489 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6492 -- We cannot in general check the maximum depth of protected entry calls
6493 -- at compile time. But we can tell that any protected entry call at all
6494 -- violates a specified nesting depth of zero.
6496 if Is_Protected_Type (Scope (Nam)) then
6497 Check_Restriction (Max_Entry_Queue_Length, N);
6500 -- Use context type to disambiguate a protected function that can be
6501 -- called without actuals and that returns an array type, and where the
6502 -- argument list may be an indexing of the returned value.
6504 if Ekind (Nam) = E_Function
6505 and then Needs_No_Actuals (Nam)
6506 and then Present (Parameter_Associations (N))
6508 ((Is_Array_Type (Etype (Nam))
6509 and then Covers (Typ, Component_Type (Etype (Nam))))
6511 or else (Is_Access_Type (Etype (Nam))
6512 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6516 Component_Type (Designated_Type (Etype (Nam))))))
6519 Index_Node : Node_Id;
6523 Make_Indexed_Component (Loc,
6525 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6526 Expressions => Parameter_Associations (N));
6528 -- Since we are correcting a node classification error made by the
6529 -- parser, we call Replace rather than Rewrite.
6531 Replace (N, Index_Node);
6532 Set_Etype (Prefix (N), Etype (Nam));
6534 Resolve_Indexed_Component (N, Typ);
6539 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6540 and then Present (PPC_Wrapper (Nam))
6541 and then Current_Scope /= PPC_Wrapper (Nam)
6543 -- Rewrite as call to the precondition wrapper, adding the task
6544 -- object to the list of actuals. If the call is to a member of an
6545 -- entry family, include the index as well.
6549 New_Actuals : List_Id;
6552 New_Actuals := New_List (Obj);
6554 if Nkind (Entry_Name) = N_Indexed_Component then
6555 Append_To (New_Actuals,
6556 New_Copy_Tree (First (Expressions (Entry_Name))));
6559 Append_List (Parameter_Associations (N), New_Actuals);
6561 Make_Procedure_Call_Statement (Loc,
6563 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6564 Parameter_Associations => New_Actuals);
6565 Rewrite (N, New_Call);
6566 Analyze_And_Resolve (N);
6571 -- The operation name may have been overloaded. Order the actuals
6572 -- according to the formals of the resolved entity, and set the return
6573 -- type to that of the operation.
6576 Normalize_Actuals (N, Nam, False, Norm_OK);
6577 pragma Assert (Norm_OK);
6578 Set_Etype (N, Etype (Nam));
6581 Resolve_Actuals (N, Nam);
6583 -- Create a call reference to the entry
6585 Generate_Reference (Nam, Entry_Name, 's');
6587 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6588 Check_Potentially_Blocking_Operation (N);
6591 -- Verify that a procedure call cannot masquerade as an entry
6592 -- call where an entry call is expected.
6594 if Ekind (Nam) = E_Procedure then
6595 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6596 and then N = Entry_Call_Statement (Parent (N))
6598 Error_Msg_N ("entry call required in select statement", N);
6600 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6601 and then N = Triggering_Statement (Parent (N))
6603 Error_Msg_N ("triggering statement cannot be procedure call", N);
6605 elsif Ekind (Scope (Nam)) = E_Task_Type
6606 and then not In_Open_Scopes (Scope (Nam))
6608 Error_Msg_N ("task has no entry with this name", Entry_Name);
6612 -- After resolution, entry calls and protected procedure calls are
6613 -- changed into entry calls, for expansion. The structure of the node
6614 -- does not change, so it can safely be done in place. Protected
6615 -- function calls must keep their structure because they are
6618 if Ekind (Nam) /= E_Function then
6620 -- A protected operation that is not a function may modify the
6621 -- corresponding object, and cannot apply to a constant. If this
6622 -- is an internal call, the prefix is the type itself.
6624 if Is_Protected_Type (Scope (Nam))
6625 and then not Is_Variable (Obj)
6626 and then (not Is_Entity_Name (Obj)
6627 or else not Is_Type (Entity (Obj)))
6630 ("prefix of protected procedure or entry call must be variable",
6634 Actuals := Parameter_Associations (N);
6635 First_Named := First_Named_Actual (N);
6638 Make_Entry_Call_Statement (Loc,
6640 Parameter_Associations => Actuals));
6642 Set_First_Named_Actual (N, First_Named);
6643 Set_Analyzed (N, True);
6645 -- Protected functions can return on the secondary stack, in which
6646 -- case we must trigger the transient scope mechanism.
6648 elsif Full_Expander_Active
6649 and then Requires_Transient_Scope (Etype (Nam))
6651 Establish_Transient_Scope (N, Sec_Stack => True);
6653 end Resolve_Entry_Call;
6655 -------------------------
6656 -- Resolve_Equality_Op --
6657 -------------------------
6659 -- Both arguments must have the same type, and the boolean context does
6660 -- not participate in the resolution. The first pass verifies that the
6661 -- interpretation is not ambiguous, and the type of the left argument is
6662 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6663 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6664 -- though they carry a single (universal) type. Diagnose this case here.
6666 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6667 L : constant Node_Id := Left_Opnd (N);
6668 R : constant Node_Id := Right_Opnd (N);
6669 T : Entity_Id := Find_Unique_Type (L, R);
6671 procedure Check_Conditional_Expression (Cond : Node_Id);
6672 -- The resolution rule for conditional expressions requires that each
6673 -- such must have a unique type. This means that if several dependent
6674 -- expressions are of a non-null anonymous access type, and the context
6675 -- does not impose an expected type (as can be the case in an equality
6676 -- operation) the expression must be rejected.
6678 function Find_Unique_Access_Type return Entity_Id;
6679 -- In the case of allocators, make a last-ditch attempt to find a single
6680 -- access type with the right designated type. This is semantically
6681 -- dubious, and of no interest to any real code, but c48008a makes it
6684 ----------------------------------
6685 -- Check_Conditional_Expression --
6686 ----------------------------------
6688 procedure Check_Conditional_Expression (Cond : Node_Id) is
6689 Then_Expr : Node_Id;
6690 Else_Expr : Node_Id;
6693 if Nkind (Cond) = N_Conditional_Expression then
6694 Then_Expr := Next (First (Expressions (Cond)));
6695 Else_Expr := Next (Then_Expr);
6697 if Nkind (Then_Expr) /= N_Null
6698 and then Nkind (Else_Expr) /= N_Null
6701 ("cannot determine type of conditional expression", Cond);
6704 end Check_Conditional_Expression;
6706 -----------------------------
6707 -- Find_Unique_Access_Type --
6708 -----------------------------
6710 function Find_Unique_Access_Type return Entity_Id is
6716 if Ekind (Etype (R)) = E_Allocator_Type then
6717 Acc := Designated_Type (Etype (R));
6718 elsif Ekind (Etype (L)) = E_Allocator_Type then
6719 Acc := Designated_Type (Etype (L));
6725 while S /= Standard_Standard loop
6726 E := First_Entity (S);
6727 while Present (E) loop
6729 and then Is_Access_Type (E)
6730 and then Ekind (E) /= E_Allocator_Type
6731 and then Designated_Type (E) = Base_Type (Acc)
6743 end Find_Unique_Access_Type;
6745 -- Start of processing for Resolve_Equality_Op
6748 Set_Etype (N, Base_Type (Typ));
6749 Generate_Reference (T, N, ' ');
6751 if T = Any_Fixed then
6752 T := Unique_Fixed_Point_Type (L);
6755 if T /= Any_Type then
6756 if T = Any_String or else
6757 T = Any_Composite or else
6760 if T = Any_Character then
6761 Ambiguous_Character (L);
6763 Error_Msg_N ("ambiguous operands for equality", N);
6766 Set_Etype (N, Any_Type);
6769 elsif T = Any_Access
6770 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6772 T := Find_Unique_Access_Type;
6775 Error_Msg_N ("ambiguous operands for equality", N);
6776 Set_Etype (N, Any_Type);
6780 -- Conditional expressions must have a single type, and if the
6781 -- context does not impose one the dependent expressions cannot
6782 -- be anonymous access types.
6784 elsif Ada_Version >= Ada_2012
6785 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6786 E_Anonymous_Access_Subprogram_Type)
6787 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6788 E_Anonymous_Access_Subprogram_Type)
6790 Check_Conditional_Expression (L);
6791 Check_Conditional_Expression (R);
6797 -- In SPARK, equality operators = and /= for array types other than
6798 -- String are only defined when, for each index position, the
6799 -- operands have equal static bounds.
6801 if Is_Array_Type (T) then
6802 -- Protect call to Matching_Static_Array_Bounds to avoid costly
6803 -- operation if not needed.
6805 if Restriction_Check_Required (SPARK)
6806 and then Base_Type (T) /= Standard_String
6807 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6808 and then Etype (L) /= Any_Composite -- or else L in error
6809 and then Etype (R) /= Any_Composite -- or else R in error
6810 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6812 Check_SPARK_Restriction
6813 ("array types should have matching static bounds", N);
6817 -- If the unique type is a class-wide type then it will be expanded
6818 -- into a dispatching call to the predefined primitive. Therefore we
6819 -- check here for potential violation of such restriction.
6821 if Is_Class_Wide_Type (T) then
6822 Check_Restriction (No_Dispatching_Calls, N);
6825 if Warn_On_Redundant_Constructs
6826 and then Comes_From_Source (N)
6827 and then Is_Entity_Name (R)
6828 and then Entity (R) = Standard_True
6829 and then Comes_From_Source (R)
6831 Error_Msg_N -- CODEFIX
6832 ("?comparison with True is redundant!", R);
6835 Check_Unset_Reference (L);
6836 Check_Unset_Reference (R);
6837 Generate_Operator_Reference (N, T);
6838 Check_Low_Bound_Tested (N);
6840 -- If this is an inequality, it may be the implicit inequality
6841 -- created for a user-defined operation, in which case the corres-
6842 -- ponding equality operation is not intrinsic, and the operation
6843 -- cannot be constant-folded. Else fold.
6845 if Nkind (N) = N_Op_Eq
6846 or else Comes_From_Source (Entity (N))
6847 or else Ekind (Entity (N)) = E_Operator
6848 or else Is_Intrinsic_Subprogram
6849 (Corresponding_Equality (Entity (N)))
6851 Eval_Relational_Op (N);
6853 elsif Nkind (N) = N_Op_Ne
6854 and then Is_Abstract_Subprogram (Entity (N))
6856 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6859 -- Ada 2005: If one operand is an anonymous access type, convert the
6860 -- other operand to it, to ensure that the underlying types match in
6861 -- the back-end. Same for access_to_subprogram, and the conversion
6862 -- verifies that the types are subtype conformant.
6864 -- We apply the same conversion in the case one of the operands is a
6865 -- private subtype of the type of the other.
6867 -- Why the Expander_Active test here ???
6869 if Full_Expander_Active
6871 (Ekind_In (T, E_Anonymous_Access_Type,
6872 E_Anonymous_Access_Subprogram_Type)
6873 or else Is_Private_Type (T))
6875 if Etype (L) /= T then
6877 Make_Unchecked_Type_Conversion (Sloc (L),
6878 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6879 Expression => Relocate_Node (L)));
6880 Analyze_And_Resolve (L, T);
6883 if (Etype (R)) /= T then
6885 Make_Unchecked_Type_Conversion (Sloc (R),
6886 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6887 Expression => Relocate_Node (R)));
6888 Analyze_And_Resolve (R, T);
6892 end Resolve_Equality_Op;
6894 ----------------------------------
6895 -- Resolve_Explicit_Dereference --
6896 ----------------------------------
6898 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6899 Loc : constant Source_Ptr := Sloc (N);
6901 P : constant Node_Id := Prefix (N);
6906 Check_Fully_Declared_Prefix (Typ, P);
6908 if Is_Overloaded (P) then
6910 -- Use the context type to select the prefix that has the correct
6913 Get_First_Interp (P, I, It);
6914 while Present (It.Typ) loop
6915 exit when Is_Access_Type (It.Typ)
6916 and then Covers (Typ, Designated_Type (It.Typ));
6917 Get_Next_Interp (I, It);
6920 if Present (It.Typ) then
6921 Resolve (P, It.Typ);
6923 -- If no interpretation covers the designated type of the prefix,
6924 -- this is the pathological case where not all implementations of
6925 -- the prefix allow the interpretation of the node as a call. Now
6926 -- that the expected type is known, Remove other interpretations
6927 -- from prefix, rewrite it as a call, and resolve again, so that
6928 -- the proper call node is generated.
6930 Get_First_Interp (P, I, It);
6931 while Present (It.Typ) loop
6932 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6936 Get_Next_Interp (I, It);
6940 Make_Function_Call (Loc,
6942 Make_Explicit_Dereference (Loc,
6944 Parameter_Associations => New_List);
6946 Save_Interps (N, New_N);
6948 Analyze_And_Resolve (N, Typ);
6952 Set_Etype (N, Designated_Type (It.Typ));
6958 if Is_Access_Type (Etype (P)) then
6959 Apply_Access_Check (N);
6962 -- If the designated type is a packed unconstrained array type, and the
6963 -- explicit dereference is not in the context of an attribute reference,
6964 -- then we must compute and set the actual subtype, since it is needed
6965 -- by Gigi. The reason we exclude the attribute case is that this is
6966 -- handled fine by Gigi, and in fact we use such attributes to build the
6967 -- actual subtype. We also exclude generated code (which builds actual
6968 -- subtypes directly if they are needed).
6970 if Is_Array_Type (Etype (N))
6971 and then Is_Packed (Etype (N))
6972 and then not Is_Constrained (Etype (N))
6973 and then Nkind (Parent (N)) /= N_Attribute_Reference
6974 and then Comes_From_Source (N)
6976 Set_Etype (N, Get_Actual_Subtype (N));
6979 -- Note: No Eval processing is required for an explicit dereference,
6980 -- because such a name can never be static.
6982 end Resolve_Explicit_Dereference;
6984 -------------------------------------
6985 -- Resolve_Expression_With_Actions --
6986 -------------------------------------
6988 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
6991 end Resolve_Expression_With_Actions;
6993 -------------------------------
6994 -- Resolve_Indexed_Component --
6995 -------------------------------
6997 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6998 Name : constant Node_Id := Prefix (N);
7000 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7004 if Is_Overloaded (Name) then
7006 -- Use the context type to select the prefix that yields the correct
7012 I1 : Interp_Index := 0;
7013 P : constant Node_Id := Prefix (N);
7014 Found : Boolean := False;
7017 Get_First_Interp (P, I, It);
7018 while Present (It.Typ) loop
7019 if (Is_Array_Type (It.Typ)
7020 and then Covers (Typ, Component_Type (It.Typ)))
7021 or else (Is_Access_Type (It.Typ)
7022 and then Is_Array_Type (Designated_Type (It.Typ))
7026 Component_Type (Designated_Type (It.Typ))))
7029 It := Disambiguate (P, I1, I, Any_Type);
7031 if It = No_Interp then
7032 Error_Msg_N ("ambiguous prefix for indexing", N);
7038 Array_Type := It.Typ;
7044 Array_Type := It.Typ;
7049 Get_Next_Interp (I, It);
7054 Array_Type := Etype (Name);
7057 Resolve (Name, Array_Type);
7058 Array_Type := Get_Actual_Subtype_If_Available (Name);
7060 -- If prefix is access type, dereference to get real array type.
7061 -- Note: we do not apply an access check because the expander always
7062 -- introduces an explicit dereference, and the check will happen there.
7064 if Is_Access_Type (Array_Type) then
7065 Array_Type := Designated_Type (Array_Type);
7068 -- If name was overloaded, set component type correctly now
7069 -- If a misplaced call to an entry family (which has no index types)
7070 -- return. Error will be diagnosed from calling context.
7072 if Is_Array_Type (Array_Type) then
7073 Set_Etype (N, Component_Type (Array_Type));
7078 Index := First_Index (Array_Type);
7079 Expr := First (Expressions (N));
7081 -- The prefix may have resolved to a string literal, in which case its
7082 -- etype has a special representation. This is only possible currently
7083 -- if the prefix is a static concatenation, written in functional
7086 if Ekind (Array_Type) = E_String_Literal_Subtype then
7087 Resolve (Expr, Standard_Positive);
7090 while Present (Index) and Present (Expr) loop
7091 Resolve (Expr, Etype (Index));
7092 Check_Unset_Reference (Expr);
7094 if Is_Scalar_Type (Etype (Expr)) then
7095 Apply_Scalar_Range_Check (Expr, Etype (Index));
7097 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7105 -- Do not generate the warning on suspicious index if we are analyzing
7106 -- package Ada.Tags; otherwise we will report the warning with the
7107 -- Prims_Ptr field of the dispatch table.
7109 if Scope (Etype (Prefix (N))) = Standard_Standard
7111 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7114 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7115 Eval_Indexed_Component (N);
7118 -- If the array type is atomic, and is packed, and we are in a left side
7119 -- context, then this is worth a warning, since we have a situation
7120 -- where the access to the component may cause extra read/writes of
7121 -- the atomic array object, which could be considered unexpected.
7123 if Nkind (N) = N_Indexed_Component
7124 and then (Is_Atomic (Array_Type)
7125 or else (Is_Entity_Name (Prefix (N))
7126 and then Is_Atomic (Entity (Prefix (N)))))
7127 and then Is_Bit_Packed_Array (Array_Type)
7130 Error_Msg_N ("?assignment to component of packed atomic array",
7132 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7135 end Resolve_Indexed_Component;
7137 -----------------------------
7138 -- Resolve_Integer_Literal --
7139 -----------------------------
7141 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7144 Eval_Integer_Literal (N);
7145 end Resolve_Integer_Literal;
7147 --------------------------------
7148 -- Resolve_Intrinsic_Operator --
7149 --------------------------------
7151 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7152 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7154 Orig_Op : constant Entity_Id := Entity (N);
7158 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7159 -- If the operand is a literal, it cannot be the expression in a
7160 -- conversion. Use a qualified expression instead.
7162 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7163 Loc : constant Source_Ptr := Sloc (Opnd);
7166 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7168 Make_Qualified_Expression (Loc,
7169 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7170 Expression => Relocate_Node (Opnd));
7174 Res := Unchecked_Convert_To (Btyp, Opnd);
7178 end Convert_Operand;
7180 -- Start of processing for Resolve_Intrinsic_Operator
7183 -- We must preserve the original entity in a generic setting, so that
7184 -- the legality of the operation can be verified in an instance.
7186 if not Full_Expander_Active then
7191 while Scope (Op) /= Standard_Standard loop
7193 pragma Assert (Present (Op));
7197 Set_Is_Overloaded (N, False);
7199 -- If the result or operand types are private, rewrite with unchecked
7200 -- conversions on the operands and the result, to expose the proper
7201 -- underlying numeric type.
7203 if Is_Private_Type (Typ)
7204 or else Is_Private_Type (Etype (Left_Opnd (N)))
7205 or else Is_Private_Type (Etype (Right_Opnd (N)))
7207 Arg1 := Convert_Operand (Left_Opnd (N));
7208 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7209 -- What on earth is this commented out fragment of code???
7211 if Nkind (N) = N_Op_Expon then
7212 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7214 Arg2 := Convert_Operand (Right_Opnd (N));
7217 if Nkind (Arg1) = N_Type_Conversion then
7218 Save_Interps (Left_Opnd (N), Expression (Arg1));
7221 if Nkind (Arg2) = N_Type_Conversion then
7222 Save_Interps (Right_Opnd (N), Expression (Arg2));
7225 Set_Left_Opnd (N, Arg1);
7226 Set_Right_Opnd (N, Arg2);
7228 Set_Etype (N, Btyp);
7229 Rewrite (N, Unchecked_Convert_To (Typ, N));
7232 elsif Typ /= Etype (Left_Opnd (N))
7233 or else Typ /= Etype (Right_Opnd (N))
7235 -- Add explicit conversion where needed, and save interpretations in
7236 -- case operands are overloaded. If the context is a VMS operation,
7237 -- assert that the conversion is legal (the operands have the proper
7238 -- types to select the VMS intrinsic). Note that in rare cases the
7239 -- VMS operators may be visible, but the default System is being used
7240 -- and Address is a private type.
7242 Arg1 := Convert_To (Typ, Left_Opnd (N));
7243 Arg2 := Convert_To (Typ, Right_Opnd (N));
7245 if Nkind (Arg1) = N_Type_Conversion then
7246 Save_Interps (Left_Opnd (N), Expression (Arg1));
7248 if Is_VMS_Operator (Orig_Op) then
7249 Set_Conversion_OK (Arg1);
7252 Save_Interps (Left_Opnd (N), Arg1);
7255 if Nkind (Arg2) = N_Type_Conversion then
7256 Save_Interps (Right_Opnd (N), Expression (Arg2));
7258 if Is_VMS_Operator (Orig_Op) then
7259 Set_Conversion_OK (Arg2);
7262 Save_Interps (Right_Opnd (N), Arg2);
7265 Rewrite (Left_Opnd (N), Arg1);
7266 Rewrite (Right_Opnd (N), Arg2);
7269 Resolve_Arithmetic_Op (N, Typ);
7272 Resolve_Arithmetic_Op (N, Typ);
7274 end Resolve_Intrinsic_Operator;
7276 --------------------------------------
7277 -- Resolve_Intrinsic_Unary_Operator --
7278 --------------------------------------
7280 procedure Resolve_Intrinsic_Unary_Operator
7284 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7290 while Scope (Op) /= Standard_Standard loop
7292 pragma Assert (Present (Op));
7297 if Is_Private_Type (Typ) then
7298 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7299 Save_Interps (Right_Opnd (N), Expression (Arg2));
7301 Set_Right_Opnd (N, Arg2);
7303 Set_Etype (N, Btyp);
7304 Rewrite (N, Unchecked_Convert_To (Typ, N));
7308 Resolve_Unary_Op (N, Typ);
7310 end Resolve_Intrinsic_Unary_Operator;
7312 ------------------------
7313 -- Resolve_Logical_Op --
7314 ------------------------
7316 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7320 Check_No_Direct_Boolean_Operators (N);
7322 -- Predefined operations on scalar types yield the base type. On the
7323 -- other hand, logical operations on arrays yield the type of the
7324 -- arguments (and the context).
7326 if Is_Array_Type (Typ) then
7329 B_Typ := Base_Type (Typ);
7332 -- OK if this is a VMS-specific intrinsic operation
7334 if Is_VMS_Operator (Entity (N)) then
7337 -- The following test is required because the operands of the operation
7338 -- may be literals, in which case the resulting type appears to be
7339 -- compatible with a signed integer type, when in fact it is compatible
7340 -- only with modular types. If the context itself is universal, the
7341 -- operation is illegal.
7343 elsif not Valid_Boolean_Arg (Typ) then
7344 Error_Msg_N ("invalid context for logical operation", N);
7345 Set_Etype (N, Any_Type);
7348 elsif Typ = Any_Modular then
7350 ("no modular type available in this context", N);
7351 Set_Etype (N, Any_Type);
7354 elsif Is_Modular_Integer_Type (Typ)
7355 and then Etype (Left_Opnd (N)) = Universal_Integer
7356 and then Etype (Right_Opnd (N)) = Universal_Integer
7358 Check_For_Visible_Operator (N, B_Typ);
7361 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
7362 -- is active and the result type is standard Boolean (do not mess with
7363 -- ops that return a nonstandard Boolean type, because something strange
7366 -- Note: you might expect this replacement to be done during expansion,
7367 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
7368 -- is used, no part of the right operand of an "and" or "or" operator
7369 -- should be executed if the left operand would short-circuit the
7370 -- evaluation of the corresponding "and then" or "or else". If we left
7371 -- the replacement to expansion time, then run-time checks associated
7372 -- with such operands would be evaluated unconditionally, due to being
7373 -- before the condition prior to the rewriting as short-circuit forms
7374 -- during expansion.
7376 if Short_Circuit_And_Or
7377 and then B_Typ = Standard_Boolean
7378 and then Nkind_In (N, N_Op_And, N_Op_Or)
7380 if Nkind (N) = N_Op_And then
7382 Make_And_Then (Sloc (N),
7383 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7384 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7385 Analyze_And_Resolve (N, B_Typ);
7387 -- Case of OR changed to OR ELSE
7391 Make_Or_Else (Sloc (N),
7392 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7393 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7394 Analyze_And_Resolve (N, B_Typ);
7397 -- Return now, since analysis of the rewritten ops will take care of
7398 -- other reference bookkeeping and expression folding.
7403 Resolve (Left_Opnd (N), B_Typ);
7404 Resolve (Right_Opnd (N), B_Typ);
7406 Check_Unset_Reference (Left_Opnd (N));
7407 Check_Unset_Reference (Right_Opnd (N));
7409 Set_Etype (N, B_Typ);
7410 Generate_Operator_Reference (N, B_Typ);
7411 Eval_Logical_Op (N);
7413 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7414 -- only when both operands have same static lower and higher bounds. Of
7415 -- course the types have to match, so only check if operands are
7416 -- compatible and the node itself has no errors.
7418 if Is_Array_Type (B_Typ)
7419 and then Nkind (N) in N_Binary_Op
7422 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7423 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7426 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7427 -- operation if not needed.
7429 if Restriction_Check_Required (SPARK)
7430 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7431 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7432 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7433 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7435 Check_SPARK_Restriction
7436 ("array types should have matching static bounds", N);
7440 end Resolve_Logical_Op;
7442 ---------------------------
7443 -- Resolve_Membership_Op --
7444 ---------------------------
7446 -- The context can only be a boolean type, and does not determine the
7447 -- arguments. Arguments should be unambiguous, but the preference rule for
7448 -- universal types applies.
7450 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7451 pragma Warnings (Off, Typ);
7453 L : constant Node_Id := Left_Opnd (N);
7454 R : constant Node_Id := Right_Opnd (N);
7457 procedure Resolve_Set_Membership;
7458 -- Analysis has determined a unique type for the left operand. Use it to
7459 -- resolve the disjuncts.
7461 ----------------------------
7462 -- Resolve_Set_Membership --
7463 ----------------------------
7465 procedure Resolve_Set_Membership is
7469 Resolve (L, Etype (L));
7471 Alt := First (Alternatives (N));
7472 while Present (Alt) loop
7474 -- Alternative is an expression, a range
7475 -- or a subtype mark.
7477 if not Is_Entity_Name (Alt)
7478 or else not Is_Type (Entity (Alt))
7480 Resolve (Alt, Etype (L));
7485 end Resolve_Set_Membership;
7487 -- Start of processing for Resolve_Membership_Op
7490 if L = Error or else R = Error then
7494 if Present (Alternatives (N)) then
7495 Resolve_Set_Membership;
7498 elsif not Is_Overloaded (R)
7500 (Etype (R) = Universal_Integer
7502 Etype (R) = Universal_Real)
7503 and then Is_Overloaded (L)
7507 -- Ada 2005 (AI-251): Support the following case:
7509 -- type I is interface;
7510 -- type T is tagged ...
7512 -- function Test (O : I'Class) is
7514 -- return O in T'Class.
7517 -- In this case we have nothing else to do. The membership test will be
7518 -- done at run time.
7520 elsif Ada_Version >= Ada_2005
7521 and then Is_Class_Wide_Type (Etype (L))
7522 and then Is_Interface (Etype (L))
7523 and then Is_Class_Wide_Type (Etype (R))
7524 and then not Is_Interface (Etype (R))
7528 T := Intersect_Types (L, R);
7531 -- If mixed-mode operations are present and operands are all literal,
7532 -- the only interpretation involves Duration, which is probably not
7533 -- the intention of the programmer.
7535 if T = Any_Fixed then
7536 T := Unique_Fixed_Point_Type (N);
7538 if T = Any_Type then
7544 Check_Unset_Reference (L);
7546 if Nkind (R) = N_Range
7547 and then not Is_Scalar_Type (T)
7549 Error_Msg_N ("scalar type required for range", R);
7552 if Is_Entity_Name (R) then
7553 Freeze_Expression (R);
7556 Check_Unset_Reference (R);
7559 Eval_Membership_Op (N);
7560 end Resolve_Membership_Op;
7566 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7567 Loc : constant Source_Ptr := Sloc (N);
7570 -- Handle restriction against anonymous null access values This
7571 -- restriction can be turned off using -gnatdj.
7573 -- Ada 2005 (AI-231): Remove restriction
7575 if Ada_Version < Ada_2005
7576 and then not Debug_Flag_J
7577 and then Ekind (Typ) = E_Anonymous_Access_Type
7578 and then Comes_From_Source (N)
7580 -- In the common case of a call which uses an explicitly null value
7581 -- for an access parameter, give specialized error message.
7583 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7587 ("null is not allowed as argument for an access parameter", N);
7589 -- Standard message for all other cases (are there any?)
7593 ("null cannot be of an anonymous access type", N);
7597 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7598 -- assignment to a null-excluding object
7600 if Ada_Version >= Ada_2005
7601 and then Can_Never_Be_Null (Typ)
7602 and then Nkind (Parent (N)) = N_Assignment_Statement
7604 if not Inside_Init_Proc then
7606 (Compile_Time_Constraint_Error (N,
7607 "(Ada 2005) null not allowed in null-excluding objects?"),
7608 Make_Raise_Constraint_Error (Loc,
7609 Reason => CE_Access_Check_Failed));
7612 Make_Raise_Constraint_Error (Loc,
7613 Reason => CE_Access_Check_Failed));
7617 -- In a distributed context, null for a remote access to subprogram may
7618 -- need to be replaced with a special record aggregate. In this case,
7619 -- return after having done the transformation.
7621 if (Ekind (Typ) = E_Record_Type
7622 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7623 and then Remote_AST_Null_Value (N, Typ)
7628 -- The null literal takes its type from the context
7633 -----------------------
7634 -- Resolve_Op_Concat --
7635 -----------------------
7637 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7639 -- We wish to avoid deep recursion, because concatenations are often
7640 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7641 -- operands nonrecursively until we find something that is not a simple
7642 -- concatenation (A in this case). We resolve that, and then walk back
7643 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7644 -- to do the rest of the work at each level. The Parent pointers allow
7645 -- us to avoid recursion, and thus avoid running out of memory. See also
7646 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7652 -- The following code is equivalent to:
7654 -- Resolve_Op_Concat_First (NN, Typ);
7655 -- Resolve_Op_Concat_Arg (N, ...);
7656 -- Resolve_Op_Concat_Rest (N, Typ);
7658 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7659 -- operand is a concatenation.
7661 -- Walk down left operands
7664 Resolve_Op_Concat_First (NN, Typ);
7665 Op1 := Left_Opnd (NN);
7666 exit when not (Nkind (Op1) = N_Op_Concat
7667 and then not Is_Array_Type (Component_Type (Typ))
7668 and then Entity (Op1) = Entity (NN));
7672 -- Now (given the above example) NN is A&B and Op1 is A
7674 -- First resolve Op1 ...
7676 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7678 -- ... then walk NN back up until we reach N (where we started), calling
7679 -- Resolve_Op_Concat_Rest along the way.
7682 Resolve_Op_Concat_Rest (NN, Typ);
7687 if Base_Type (Etype (N)) /= Standard_String then
7688 Check_SPARK_Restriction
7689 ("result of concatenation should have type String", N);
7691 end Resolve_Op_Concat;
7693 ---------------------------
7694 -- Resolve_Op_Concat_Arg --
7695 ---------------------------
7697 procedure Resolve_Op_Concat_Arg
7703 Btyp : constant Entity_Id := Base_Type (Typ);
7704 Ctyp : constant Entity_Id := Component_Type (Typ);
7709 or else (not Is_Overloaded (Arg)
7710 and then Etype (Arg) /= Any_Composite
7711 and then Covers (Ctyp, Etype (Arg)))
7713 Resolve (Arg, Ctyp);
7715 Resolve (Arg, Btyp);
7718 -- If both Array & Array and Array & Component are visible, there is a
7719 -- potential ambiguity that must be reported.
7721 elsif Has_Compatible_Type (Arg, Ctyp) then
7722 if Nkind (Arg) = N_Aggregate
7723 and then Is_Composite_Type (Ctyp)
7725 if Is_Private_Type (Ctyp) then
7726 Resolve (Arg, Btyp);
7728 -- If the operation is user-defined and not overloaded use its
7729 -- profile. The operation may be a renaming, in which case it has
7730 -- been rewritten, and we want the original profile.
7732 elsif not Is_Overloaded (N)
7733 and then Comes_From_Source (Entity (Original_Node (N)))
7734 and then Ekind (Entity (Original_Node (N))) = E_Function
7738 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
7741 -- Otherwise an aggregate may match both the array type and the
7745 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7746 Set_Etype (Arg, Any_Type);
7750 if Is_Overloaded (Arg)
7751 and then Has_Compatible_Type (Arg, Typ)
7752 and then Etype (Arg) /= Any_Type
7760 Get_First_Interp (Arg, I, It);
7762 Get_Next_Interp (I, It);
7764 -- Special-case the error message when the overloading is
7765 -- caused by a function that yields an array and can be
7766 -- called without parameters.
7768 if It.Nam = Func then
7769 Error_Msg_Sloc := Sloc (Func);
7770 Error_Msg_N ("ambiguous call to function#", Arg);
7772 ("\\interpretation as call yields&", Arg, Typ);
7774 ("\\interpretation as indexing of call yields&",
7775 Arg, Component_Type (Typ));
7778 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
7780 Get_First_Interp (Arg, I, It);
7781 while Present (It.Nam) loop
7782 Error_Msg_Sloc := Sloc (It.Nam);
7784 if Base_Type (It.Typ) = Btyp
7786 Base_Type (It.Typ) = Base_Type (Ctyp)
7788 Error_Msg_N -- CODEFIX
7789 ("\\possible interpretation#", Arg);
7792 Get_Next_Interp (I, It);
7798 Resolve (Arg, Component_Type (Typ));
7800 if Nkind (Arg) = N_String_Literal then
7801 Set_Etype (Arg, Component_Type (Typ));
7804 if Arg = Left_Opnd (N) then
7805 Set_Is_Component_Left_Opnd (N);
7807 Set_Is_Component_Right_Opnd (N);
7812 Resolve (Arg, Btyp);
7815 -- Concatenation is restricted in SPARK: each operand must be either a
7816 -- string literal, the name of a string constant, a static character or
7817 -- string expression, or another concatenation. Arg cannot be a
7818 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
7819 -- separately on each final operand, past concatenation operations.
7821 if Is_Character_Type (Etype (Arg)) then
7822 if not Is_Static_Expression (Arg) then
7823 Check_SPARK_Restriction
7824 ("character operand for concatenation should be static", Arg);
7827 elsif Is_String_Type (Etype (Arg)) then
7828 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
7829 and then Is_Constant_Object (Entity (Arg)))
7830 and then not Is_Static_Expression (Arg)
7832 Check_SPARK_Restriction
7833 ("string operand for concatenation should be static", Arg);
7836 -- Do not issue error on an operand that is neither a character nor a
7837 -- string, as the error is issued in Resolve_Op_Concat.
7843 Check_Unset_Reference (Arg);
7844 end Resolve_Op_Concat_Arg;
7846 -----------------------------
7847 -- Resolve_Op_Concat_First --
7848 -----------------------------
7850 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7851 Btyp : constant Entity_Id := Base_Type (Typ);
7852 Op1 : constant Node_Id := Left_Opnd (N);
7853 Op2 : constant Node_Id := Right_Opnd (N);
7856 -- The parser folds an enormous sequence of concatenations of string
7857 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7858 -- in the right operand. If the expression resolves to a predefined "&"
7859 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7860 -- we give an error. See P_Simple_Expression in Par.Ch4.
7862 if Nkind (Op2) = N_String_Literal
7863 and then Is_Folded_In_Parser (Op2)
7864 and then Ekind (Entity (N)) = E_Function
7866 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7867 and then String_Length (Strval (Op1)) = 0);
7868 Error_Msg_N ("too many user-defined concatenations", N);
7872 Set_Etype (N, Btyp);
7874 if Is_Limited_Composite (Btyp) then
7875 Error_Msg_N ("concatenation not available for limited array", N);
7876 Explain_Limited_Type (Btyp, N);
7878 end Resolve_Op_Concat_First;
7880 ----------------------------
7881 -- Resolve_Op_Concat_Rest --
7882 ----------------------------
7884 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7885 Op1 : constant Node_Id := Left_Opnd (N);
7886 Op2 : constant Node_Id := Right_Opnd (N);
7889 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7891 Generate_Operator_Reference (N, Typ);
7893 if Is_String_Type (Typ) then
7894 Eval_Concatenation (N);
7897 -- If this is not a static concatenation, but the result is a string
7898 -- type (and not an array of strings) ensure that static string operands
7899 -- have their subtypes properly constructed.
7901 if Nkind (N) /= N_String_Literal
7902 and then Is_Character_Type (Component_Type (Typ))
7904 Set_String_Literal_Subtype (Op1, Typ);
7905 Set_String_Literal_Subtype (Op2, Typ);
7907 end Resolve_Op_Concat_Rest;
7909 ----------------------
7910 -- Resolve_Op_Expon --
7911 ----------------------
7913 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7914 B_Typ : constant Entity_Id := Base_Type (Typ);
7917 -- Catch attempts to do fixed-point exponentiation with universal
7918 -- operands, which is a case where the illegality is not caught during
7919 -- normal operator analysis.
7921 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7922 Error_Msg_N ("exponentiation not available for fixed point", N);
7925 elsif Nkind (Parent (N)) in N_Op
7926 and then Is_Fixed_Point_Type (Etype (Parent (N)))
7927 and then Etype (N) = Universal_Real
7928 and then Comes_From_Source (N)
7930 Error_Msg_N ("exponentiation not available for fixed point", N);
7934 if Comes_From_Source (N)
7935 and then Ekind (Entity (N)) = E_Function
7936 and then Is_Imported (Entity (N))
7937 and then Is_Intrinsic_Subprogram (Entity (N))
7939 Resolve_Intrinsic_Operator (N, Typ);
7943 if Etype (Left_Opnd (N)) = Universal_Integer
7944 or else Etype (Left_Opnd (N)) = Universal_Real
7946 Check_For_Visible_Operator (N, B_Typ);
7949 -- We do the resolution using the base type, because intermediate values
7950 -- in expressions always are of the base type, not a subtype of it.
7952 Resolve (Left_Opnd (N), B_Typ);
7953 Resolve (Right_Opnd (N), Standard_Integer);
7955 Check_Unset_Reference (Left_Opnd (N));
7956 Check_Unset_Reference (Right_Opnd (N));
7958 Set_Etype (N, B_Typ);
7959 Generate_Operator_Reference (N, B_Typ);
7962 -- Set overflow checking bit. Much cleverer code needed here eventually
7963 -- and perhaps the Resolve routines should be separated for the various
7964 -- arithmetic operations, since they will need different processing. ???
7966 if Nkind (N) in N_Op then
7967 if not Overflow_Checks_Suppressed (Etype (N)) then
7968 Enable_Overflow_Check (N);
7971 end Resolve_Op_Expon;
7973 --------------------
7974 -- Resolve_Op_Not --
7975 --------------------
7977 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7980 function Parent_Is_Boolean return Boolean;
7981 -- This function determines if the parent node is a boolean operator or
7982 -- operation (comparison op, membership test, or short circuit form) and
7983 -- the not in question is the left operand of this operation. Note that
7984 -- if the not is in parens, then false is returned.
7986 -----------------------
7987 -- Parent_Is_Boolean --
7988 -----------------------
7990 function Parent_Is_Boolean return Boolean is
7992 if Paren_Count (N) /= 0 then
7996 case Nkind (Parent (N)) is
8011 return Left_Opnd (Parent (N)) = N;
8017 end Parent_Is_Boolean;
8019 -- Start of processing for Resolve_Op_Not
8022 -- Predefined operations on scalar types yield the base type. On the
8023 -- other hand, logical operations on arrays yield the type of the
8024 -- arguments (and the context).
8026 if Is_Array_Type (Typ) then
8029 B_Typ := Base_Type (Typ);
8032 if Is_VMS_Operator (Entity (N)) then
8035 -- Straightforward case of incorrect arguments
8037 elsif not Valid_Boolean_Arg (Typ) then
8038 Error_Msg_N ("invalid operand type for operator&", N);
8039 Set_Etype (N, Any_Type);
8042 -- Special case of probable missing parens
8044 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8045 if Parent_Is_Boolean then
8047 ("operand of not must be enclosed in parentheses",
8051 ("no modular type available in this context", N);
8054 Set_Etype (N, Any_Type);
8057 -- OK resolution of NOT
8060 -- Warn if non-boolean types involved. This is a case like not a < b
8061 -- where a and b are modular, where we will get (not a) < b and most
8062 -- likely not (a < b) was intended.
8064 if Warn_On_Questionable_Missing_Parens
8065 and then not Is_Boolean_Type (Typ)
8066 and then Parent_Is_Boolean
8068 Error_Msg_N ("?not expression should be parenthesized here!", N);
8071 -- Warn on double negation if checking redundant constructs
8073 if Warn_On_Redundant_Constructs
8074 and then Comes_From_Source (N)
8075 and then Comes_From_Source (Right_Opnd (N))
8076 and then Root_Type (Typ) = Standard_Boolean
8077 and then Nkind (Right_Opnd (N)) = N_Op_Not
8079 Error_Msg_N ("redundant double negation?", N);
8082 -- Complete resolution and evaluation of NOT
8084 Resolve (Right_Opnd (N), B_Typ);
8085 Check_Unset_Reference (Right_Opnd (N));
8086 Set_Etype (N, B_Typ);
8087 Generate_Operator_Reference (N, B_Typ);
8092 -----------------------------
8093 -- Resolve_Operator_Symbol --
8094 -----------------------------
8096 -- Nothing to be done, all resolved already
8098 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8099 pragma Warnings (Off, N);
8100 pragma Warnings (Off, Typ);
8104 end Resolve_Operator_Symbol;
8106 ----------------------------------
8107 -- Resolve_Qualified_Expression --
8108 ----------------------------------
8110 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8111 pragma Warnings (Off, Typ);
8113 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8114 Expr : constant Node_Id := Expression (N);
8117 Resolve (Expr, Target_Typ);
8119 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8120 -- operation if not needed.
8122 if Restriction_Check_Required (SPARK)
8123 and then Is_Array_Type (Target_Typ)
8124 and then Is_Array_Type (Etype (Expr))
8125 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8126 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8128 Check_SPARK_Restriction
8129 ("array types should have matching static bounds", N);
8132 -- A qualified expression requires an exact match of the type, class-
8133 -- wide matching is not allowed. However, if the qualifying type is
8134 -- specific and the expression has a class-wide type, it may still be
8135 -- okay, since it can be the result of the expansion of a call to a
8136 -- dispatching function, so we also have to check class-wideness of the
8137 -- type of the expression's original node.
8139 if (Is_Class_Wide_Type (Target_Typ)
8141 (Is_Class_Wide_Type (Etype (Expr))
8142 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8143 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8145 Wrong_Type (Expr, Target_Typ);
8148 -- If the target type is unconstrained, then we reset the type of the
8149 -- result from the type of the expression. For other cases, the actual
8150 -- subtype of the expression is the target type.
8152 if Is_Composite_Type (Target_Typ)
8153 and then not Is_Constrained (Target_Typ)
8155 Set_Etype (N, Etype (Expr));
8158 Eval_Qualified_Expression (N);
8159 end Resolve_Qualified_Expression;
8161 -----------------------------------
8162 -- Resolve_Quantified_Expression --
8163 -----------------------------------
8165 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
8167 if not Alfa_Mode then
8169 -- If expansion is enabled, analysis is delayed until the expresssion
8170 -- is rewritten as a loop.
8172 if Operating_Mode /= Check_Semantics then
8176 -- The loop structure is already resolved during its analysis, only
8177 -- the resolution of the condition needs to be done. Expansion is
8178 -- disabled so that checks and other generated code are inserted in
8179 -- the tree after expression has been rewritten as a loop.
8181 Expander_Mode_Save_And_Set (False);
8182 Resolve (Condition (N), Typ);
8183 Expander_Mode_Restore;
8185 -- In Alfa mode, we need normal expansion in order to properly introduce
8186 -- the necessary transient scopes.
8189 Resolve (Condition (N), Typ);
8191 end Resolve_Quantified_Expression;
8197 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8198 L : constant Node_Id := Low_Bound (N);
8199 H : constant Node_Id := High_Bound (N);
8201 function First_Last_Ref return Boolean;
8202 -- Returns True if N is of the form X'First .. X'Last where X is the
8203 -- same entity for both attributes.
8205 --------------------
8206 -- First_Last_Ref --
8207 --------------------
8209 function First_Last_Ref return Boolean is
8210 Lorig : constant Node_Id := Original_Node (L);
8211 Horig : constant Node_Id := Original_Node (H);
8214 if Nkind (Lorig) = N_Attribute_Reference
8215 and then Nkind (Horig) = N_Attribute_Reference
8216 and then Attribute_Name (Lorig) = Name_First
8217 and then Attribute_Name (Horig) = Name_Last
8220 PL : constant Node_Id := Prefix (Lorig);
8221 PH : constant Node_Id := Prefix (Horig);
8223 if Is_Entity_Name (PL)
8224 and then Is_Entity_Name (PH)
8225 and then Entity (PL) = Entity (PH)
8235 -- Start of processing for Resolve_Range
8242 -- Check for inappropriate range on unordered enumeration type
8244 if Bad_Unordered_Enumeration_Reference (N, Typ)
8246 -- Exclude X'First .. X'Last if X is the same entity for both
8248 and then not First_Last_Ref
8250 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
8253 Check_Unset_Reference (L);
8254 Check_Unset_Reference (H);
8256 -- We have to check the bounds for being within the base range as
8257 -- required for a non-static context. Normally this is automatic and
8258 -- done as part of evaluating expressions, but the N_Range node is an
8259 -- exception, since in GNAT we consider this node to be a subexpression,
8260 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8261 -- this, but that would put the test on the main evaluation path for
8264 Check_Non_Static_Context (L);
8265 Check_Non_Static_Context (H);
8267 -- Check for an ambiguous range over character literals. This will
8268 -- happen with a membership test involving only literals.
8270 if Typ = Any_Character then
8271 Ambiguous_Character (L);
8272 Set_Etype (N, Any_Type);
8276 -- If bounds are static, constant-fold them, so size computations are
8277 -- identical between front-end and back-end. Do not perform this
8278 -- transformation while analyzing generic units, as type information
8279 -- would be lost when reanalyzing the constant node in the instance.
8281 if Is_Discrete_Type (Typ) and then Full_Expander_Active then
8282 if Is_OK_Static_Expression (L) then
8283 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8286 if Is_OK_Static_Expression (H) then
8287 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8292 --------------------------
8293 -- Resolve_Real_Literal --
8294 --------------------------
8296 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8297 Actual_Typ : constant Entity_Id := Etype (N);
8300 -- Special processing for fixed-point literals to make sure that the
8301 -- value is an exact multiple of small where this is required. We skip
8302 -- this for the universal real case, and also for generic types.
8304 if Is_Fixed_Point_Type (Typ)
8305 and then Typ /= Universal_Fixed
8306 and then Typ /= Any_Fixed
8307 and then not Is_Generic_Type (Typ)
8310 Val : constant Ureal := Realval (N);
8311 Cintr : constant Ureal := Val / Small_Value (Typ);
8312 Cint : constant Uint := UR_Trunc (Cintr);
8313 Den : constant Uint := Norm_Den (Cintr);
8317 -- Case of literal is not an exact multiple of the Small
8321 -- For a source program literal for a decimal fixed-point type,
8322 -- this is statically illegal (RM 4.9(36)).
8324 if Is_Decimal_Fixed_Point_Type (Typ)
8325 and then Actual_Typ = Universal_Real
8326 and then Comes_From_Source (N)
8328 Error_Msg_N ("value has extraneous low order digits", N);
8331 -- Generate a warning if literal from source
8333 if Is_Static_Expression (N)
8334 and then Warn_On_Bad_Fixed_Value
8337 ("?static fixed-point value is not a multiple of Small!",
8341 -- Replace literal by a value that is the exact representation
8342 -- of a value of the type, i.e. a multiple of the small value,
8343 -- by truncation, since Machine_Rounds is false for all GNAT
8344 -- fixed-point types (RM 4.9(38)).
8346 Stat := Is_Static_Expression (N);
8348 Make_Real_Literal (Sloc (N),
8349 Realval => Small_Value (Typ) * Cint));
8351 Set_Is_Static_Expression (N, Stat);
8354 -- In all cases, set the corresponding integer field
8356 Set_Corresponding_Integer_Value (N, Cint);
8360 -- Now replace the actual type by the expected type as usual
8363 Eval_Real_Literal (N);
8364 end Resolve_Real_Literal;
8366 -----------------------
8367 -- Resolve_Reference --
8368 -----------------------
8370 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8371 P : constant Node_Id := Prefix (N);
8374 -- Replace general access with specific type
8376 if Ekind (Etype (N)) = E_Allocator_Type then
8377 Set_Etype (N, Base_Type (Typ));
8380 Resolve (P, Designated_Type (Etype (N)));
8382 -- If we are taking the reference of a volatile entity, then treat it as
8383 -- a potential modification of this entity. This is too conservative,
8384 -- but necessary because remove side effects can cause transformations
8385 -- of normal assignments into reference sequences that otherwise fail to
8386 -- notice the modification.
8388 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8389 Note_Possible_Modification (P, Sure => False);
8391 end Resolve_Reference;
8393 --------------------------------
8394 -- Resolve_Selected_Component --
8395 --------------------------------
8397 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8399 Comp1 : Entity_Id := Empty; -- prevent junk warning
8400 P : constant Node_Id := Prefix (N);
8401 S : constant Node_Id := Selector_Name (N);
8402 T : Entity_Id := Etype (P);
8404 I1 : Interp_Index := 0; -- prevent junk warning
8409 function Init_Component return Boolean;
8410 -- Check whether this is the initialization of a component within an
8411 -- init proc (by assignment or call to another init proc). If true,
8412 -- there is no need for a discriminant check.
8414 --------------------
8415 -- Init_Component --
8416 --------------------
8418 function Init_Component return Boolean is
8420 return Inside_Init_Proc
8421 and then Nkind (Prefix (N)) = N_Identifier
8422 and then Chars (Prefix (N)) = Name_uInit
8423 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8426 -- Start of processing for Resolve_Selected_Component
8429 if Is_Overloaded (P) then
8431 -- Use the context type to select the prefix that has a selector
8432 -- of the correct name and type.
8435 Get_First_Interp (P, I, It);
8437 Search : while Present (It.Typ) loop
8438 if Is_Access_Type (It.Typ) then
8439 T := Designated_Type (It.Typ);
8444 -- Locate selected component. For a private prefix the selector
8445 -- can denote a discriminant.
8447 if Is_Record_Type (T) or else Is_Private_Type (T) then
8449 -- The visible components of a class-wide type are those of
8452 if Is_Class_Wide_Type (T) then
8456 Comp := First_Entity (T);
8457 while Present (Comp) loop
8458 if Chars (Comp) = Chars (S)
8459 and then Covers (Etype (Comp), Typ)
8468 It := Disambiguate (P, I1, I, Any_Type);
8470 if It = No_Interp then
8472 ("ambiguous prefix for selected component", N);
8479 -- There may be an implicit dereference. Retrieve
8480 -- designated record type.
8482 if Is_Access_Type (It1.Typ) then
8483 T := Designated_Type (It1.Typ);
8488 if Scope (Comp1) /= T then
8490 -- Resolution chooses the new interpretation.
8491 -- Find the component with the right name.
8493 Comp1 := First_Entity (T);
8494 while Present (Comp1)
8495 and then Chars (Comp1) /= Chars (S)
8497 Comp1 := Next_Entity (Comp1);
8506 Comp := Next_Entity (Comp);
8510 Get_Next_Interp (I, It);
8513 Resolve (P, It1.Typ);
8515 Set_Entity_With_Style_Check (S, Comp1);
8518 -- Resolve prefix with its type
8523 -- Generate cross-reference. We needed to wait until full overloading
8524 -- resolution was complete to do this, since otherwise we can't tell if
8525 -- we are an lvalue or not.
8527 if May_Be_Lvalue (N) then
8528 Generate_Reference (Entity (S), S, 'm');
8530 Generate_Reference (Entity (S), S, 'r');
8533 -- If prefix is an access type, the node will be transformed into an
8534 -- explicit dereference during expansion. The type of the node is the
8535 -- designated type of that of the prefix.
8537 if Is_Access_Type (Etype (P)) then
8538 T := Designated_Type (Etype (P));
8539 Check_Fully_Declared_Prefix (T, P);
8544 if Has_Discriminants (T)
8545 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8546 and then Present (Original_Record_Component (Entity (S)))
8547 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8548 and then Present (Discriminant_Checking_Func
8549 (Original_Record_Component (Entity (S))))
8550 and then not Discriminant_Checks_Suppressed (T)
8551 and then not Init_Component
8553 Set_Do_Discriminant_Check (N);
8556 if Ekind (Entity (S)) = E_Void then
8557 Error_Msg_N ("premature use of component", S);
8560 -- If the prefix is a record conversion, this may be a renamed
8561 -- discriminant whose bounds differ from those of the original
8562 -- one, so we must ensure that a range check is performed.
8564 if Nkind (P) = N_Type_Conversion
8565 and then Ekind (Entity (S)) = E_Discriminant
8566 and then Is_Discrete_Type (Typ)
8568 Set_Etype (N, Base_Type (Typ));
8571 -- Note: No Eval processing is required, because the prefix is of a
8572 -- record type, or protected type, and neither can possibly be static.
8574 -- If the array type is atomic, and is packed, and we are in a left side
8575 -- context, then this is worth a warning, since we have a situation
8576 -- where the access to the component may cause extra read/writes of the
8577 -- atomic array object, which could be considered unexpected.
8579 if Nkind (N) = N_Selected_Component
8580 and then (Is_Atomic (T)
8581 or else (Is_Entity_Name (Prefix (N))
8582 and then Is_Atomic (Entity (Prefix (N)))))
8583 and then Is_Packed (T)
8586 Error_Msg_N ("?assignment to component of packed atomic record",
8588 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8591 end Resolve_Selected_Component;
8597 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8598 B_Typ : constant Entity_Id := Base_Type (Typ);
8599 L : constant Node_Id := Left_Opnd (N);
8600 R : constant Node_Id := Right_Opnd (N);
8603 -- We do the resolution using the base type, because intermediate values
8604 -- in expressions always are of the base type, not a subtype of it.
8607 Resolve (R, Standard_Natural);
8609 Check_Unset_Reference (L);
8610 Check_Unset_Reference (R);
8612 Set_Etype (N, B_Typ);
8613 Generate_Operator_Reference (N, B_Typ);
8617 ---------------------------
8618 -- Resolve_Short_Circuit --
8619 ---------------------------
8621 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8622 B_Typ : constant Entity_Id := Base_Type (Typ);
8623 L : constant Node_Id := Left_Opnd (N);
8624 R : constant Node_Id := Right_Opnd (N);
8630 -- Check for issuing warning for always False assert/check, this happens
8631 -- when assertions are turned off, in which case the pragma Assert/Check
8632 -- was transformed into:
8634 -- if False and then <condition> then ...
8636 -- and we detect this pattern
8638 if Warn_On_Assertion_Failure
8639 and then Is_Entity_Name (R)
8640 and then Entity (R) = Standard_False
8641 and then Nkind (Parent (N)) = N_If_Statement
8642 and then Nkind (N) = N_And_Then
8643 and then Is_Entity_Name (L)
8644 and then Entity (L) = Standard_False
8647 Orig : constant Node_Id := Original_Node (Parent (N));
8650 if Nkind (Orig) = N_Pragma
8651 and then Pragma_Name (Orig) = Name_Assert
8653 -- Don't want to warn if original condition is explicit False
8656 Expr : constant Node_Id :=
8659 (First (Pragma_Argument_Associations (Orig))));
8661 if Is_Entity_Name (Expr)
8662 and then Entity (Expr) = Standard_False
8666 -- Issue warning. We do not want the deletion of the
8667 -- IF/AND-THEN to take this message with it. We achieve
8668 -- this by making sure that the expanded code points to
8669 -- the Sloc of the expression, not the original pragma.
8672 ("?assertion would fail at run time!",
8674 (First (Pragma_Argument_Associations (Orig))));
8678 -- Similar processing for Check pragma
8680 elsif Nkind (Orig) = N_Pragma
8681 and then Pragma_Name (Orig) = Name_Check
8683 -- Don't want to warn if original condition is explicit False
8686 Expr : constant Node_Id :=
8690 (Pragma_Argument_Associations (Orig)))));
8692 if Is_Entity_Name (Expr)
8693 and then Entity (Expr) = Standard_False
8698 ("?check would fail at run time!",
8700 (Last (Pragma_Argument_Associations (Orig))));
8707 -- Continue with processing of short circuit
8709 Check_Unset_Reference (L);
8710 Check_Unset_Reference (R);
8712 Set_Etype (N, B_Typ);
8713 Eval_Short_Circuit (N);
8714 end Resolve_Short_Circuit;
8720 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8721 Name : constant Node_Id := Prefix (N);
8722 Drange : constant Node_Id := Discrete_Range (N);
8723 Array_Type : Entity_Id := Empty;
8727 if Is_Overloaded (Name) then
8729 -- Use the context type to select the prefix that yields the correct
8734 I1 : Interp_Index := 0;
8736 P : constant Node_Id := Prefix (N);
8737 Found : Boolean := False;
8740 Get_First_Interp (P, I, It);
8741 while Present (It.Typ) loop
8742 if (Is_Array_Type (It.Typ)
8743 and then Covers (Typ, It.Typ))
8744 or else (Is_Access_Type (It.Typ)
8745 and then Is_Array_Type (Designated_Type (It.Typ))
8746 and then Covers (Typ, Designated_Type (It.Typ)))
8749 It := Disambiguate (P, I1, I, Any_Type);
8751 if It = No_Interp then
8752 Error_Msg_N ("ambiguous prefix for slicing", N);
8757 Array_Type := It.Typ;
8762 Array_Type := It.Typ;
8767 Get_Next_Interp (I, It);
8772 Array_Type := Etype (Name);
8775 Resolve (Name, Array_Type);
8777 if Is_Access_Type (Array_Type) then
8778 Apply_Access_Check (N);
8779 Array_Type := Designated_Type (Array_Type);
8781 -- If the prefix is an access to an unconstrained array, we must use
8782 -- the actual subtype of the object to perform the index checks. The
8783 -- object denoted by the prefix is implicit in the node, so we build
8784 -- an explicit representation for it in order to compute the actual
8787 if not Is_Constrained (Array_Type) then
8788 Remove_Side_Effects (Prefix (N));
8791 Obj : constant Node_Id :=
8792 Make_Explicit_Dereference (Sloc (N),
8793 Prefix => New_Copy_Tree (Prefix (N)));
8795 Set_Etype (Obj, Array_Type);
8796 Set_Parent (Obj, Parent (N));
8797 Array_Type := Get_Actual_Subtype (Obj);
8801 elsif Is_Entity_Name (Name)
8802 or else Nkind (Name) = N_Explicit_Dereference
8803 or else (Nkind (Name) = N_Function_Call
8804 and then not Is_Constrained (Etype (Name)))
8806 Array_Type := Get_Actual_Subtype (Name);
8808 -- If the name is a selected component that depends on discriminants,
8809 -- build an actual subtype for it. This can happen only when the name
8810 -- itself is overloaded; otherwise the actual subtype is created when
8811 -- the selected component is analyzed.
8813 elsif Nkind (Name) = N_Selected_Component
8814 and then Full_Analysis
8815 and then Depends_On_Discriminant (First_Index (Array_Type))
8818 Act_Decl : constant Node_Id :=
8819 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8821 Insert_Action (N, Act_Decl);
8822 Array_Type := Defining_Identifier (Act_Decl);
8825 -- Maybe this should just be "else", instead of checking for the
8826 -- specific case of slice??? This is needed for the case where the
8827 -- prefix is an Image attribute, which gets expanded to a slice, and so
8828 -- has a constrained subtype which we want to use for the slice range
8829 -- check applied below (the range check won't get done if the
8830 -- unconstrained subtype of the 'Image is used).
8832 elsif Nkind (Name) = N_Slice then
8833 Array_Type := Etype (Name);
8836 -- If name was overloaded, set slice type correctly now
8838 Set_Etype (N, Array_Type);
8840 -- If the range is specified by a subtype mark, no resolution is
8841 -- necessary. Else resolve the bounds, and apply needed checks.
8843 if not Is_Entity_Name (Drange) then
8844 Index := First_Index (Array_Type);
8845 Resolve (Drange, Base_Type (Etype (Index)));
8847 if Nkind (Drange) = N_Range then
8849 -- Ensure that side effects in the bounds are properly handled
8851 Force_Evaluation (Low_Bound (Drange));
8852 Force_Evaluation (High_Bound (Drange));
8854 -- Do not apply the range check to nodes associated with the
8855 -- frontend expansion of the dispatch table. We first check
8856 -- if Ada.Tags is already loaded to avoid the addition of an
8857 -- undesired dependence on such run-time unit.
8859 if not Tagged_Type_Expansion
8861 (RTU_Loaded (Ada_Tags)
8862 and then Nkind (Prefix (N)) = N_Selected_Component
8863 and then Present (Entity (Selector_Name (Prefix (N))))
8864 and then Entity (Selector_Name (Prefix (N))) =
8865 RTE_Record_Component (RE_Prims_Ptr))
8867 Apply_Range_Check (Drange, Etype (Index));
8872 Set_Slice_Subtype (N);
8874 -- Check bad use of type with predicates
8876 if Has_Predicates (Etype (Drange)) then
8877 Bad_Predicated_Subtype_Use
8878 ("subtype& has predicate, not allowed in slice",
8879 Drange, Etype (Drange));
8881 -- Otherwise here is where we check suspicious indexes
8883 elsif Nkind (Drange) = N_Range then
8884 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8885 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8891 ----------------------------
8892 -- Resolve_String_Literal --
8893 ----------------------------
8895 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8896 C_Typ : constant Entity_Id := Component_Type (Typ);
8897 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8898 Loc : constant Source_Ptr := Sloc (N);
8899 Str : constant String_Id := Strval (N);
8900 Strlen : constant Nat := String_Length (Str);
8901 Subtype_Id : Entity_Id;
8902 Need_Check : Boolean;
8905 -- For a string appearing in a concatenation, defer creation of the
8906 -- string_literal_subtype until the end of the resolution of the
8907 -- concatenation, because the literal may be constant-folded away. This
8908 -- is a useful optimization for long concatenation expressions.
8910 -- If the string is an aggregate built for a single character (which
8911 -- happens in a non-static context) or a is null string to which special
8912 -- checks may apply, we build the subtype. Wide strings must also get a
8913 -- string subtype if they come from a one character aggregate. Strings
8914 -- generated by attributes might be static, but it is often hard to
8915 -- determine whether the enclosing context is static, so we generate
8916 -- subtypes for them as well, thus losing some rarer optimizations ???
8917 -- Same for strings that come from a static conversion.
8920 (Strlen = 0 and then Typ /= Standard_String)
8921 or else Nkind (Parent (N)) /= N_Op_Concat
8922 or else (N /= Left_Opnd (Parent (N))
8923 and then N /= Right_Opnd (Parent (N)))
8924 or else ((Typ = Standard_Wide_String
8925 or else Typ = Standard_Wide_Wide_String)
8926 and then Nkind (Original_Node (N)) /= N_String_Literal);
8928 -- If the resolving type is itself a string literal subtype, we can just
8929 -- reuse it, since there is no point in creating another.
8931 if Ekind (Typ) = E_String_Literal_Subtype then
8934 elsif Nkind (Parent (N)) = N_Op_Concat
8935 and then not Need_Check
8936 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8937 N_Attribute_Reference,
8938 N_Qualified_Expression,
8943 -- Otherwise we must create a string literal subtype. Note that the
8944 -- whole idea of string literal subtypes is simply to avoid the need
8945 -- for building a full fledged array subtype for each literal.
8948 Set_String_Literal_Subtype (N, Typ);
8949 Subtype_Id := Etype (N);
8952 if Nkind (Parent (N)) /= N_Op_Concat
8955 Set_Etype (N, Subtype_Id);
8956 Eval_String_Literal (N);
8959 if Is_Limited_Composite (Typ)
8960 or else Is_Private_Composite (Typ)
8962 Error_Msg_N ("string literal not available for private array", N);
8963 Set_Etype (N, Any_Type);
8967 -- The validity of a null string has been checked in the call to
8968 -- Eval_String_Literal.
8973 -- Always accept string literal with component type Any_Character, which
8974 -- occurs in error situations and in comparisons of literals, both of
8975 -- which should accept all literals.
8977 elsif R_Typ = Any_Character then
8980 -- If the type is bit-packed, then we always transform the string
8981 -- literal into a full fledged aggregate.
8983 elsif Is_Bit_Packed_Array (Typ) then
8986 -- Deal with cases of Wide_Wide_String, Wide_String, and String
8989 -- For Standard.Wide_Wide_String, or any other type whose component
8990 -- type is Standard.Wide_Wide_Character, we know that all the
8991 -- characters in the string must be acceptable, since the parser
8992 -- accepted the characters as valid character literals.
8994 if R_Typ = Standard_Wide_Wide_Character then
8997 -- For the case of Standard.String, or any other type whose component
8998 -- type is Standard.Character, we must make sure that there are no
8999 -- wide characters in the string, i.e. that it is entirely composed
9000 -- of characters in range of type Character.
9002 -- If the string literal is the result of a static concatenation, the
9003 -- test has already been performed on the components, and need not be
9006 elsif R_Typ = Standard_Character
9007 and then Nkind (Original_Node (N)) /= N_Op_Concat
9009 for J in 1 .. Strlen loop
9010 if not In_Character_Range (Get_String_Char (Str, J)) then
9012 -- If we are out of range, post error. This is one of the
9013 -- very few places that we place the flag in the middle of
9014 -- a token, right under the offending wide character. Not
9015 -- quite clear if this is right wrt wide character encoding
9016 -- sequences, but it's only an error message!
9019 ("literal out of range of type Standard.Character",
9020 Source_Ptr (Int (Loc) + J));
9025 -- For the case of Standard.Wide_String, or any other type whose
9026 -- component type is Standard.Wide_Character, we must make sure that
9027 -- there are no wide characters in the string, i.e. that it is
9028 -- entirely composed of characters in range of type Wide_Character.
9030 -- If the string literal is the result of a static concatenation,
9031 -- the test has already been performed on the components, and need
9034 elsif R_Typ = Standard_Wide_Character
9035 and then Nkind (Original_Node (N)) /= N_Op_Concat
9037 for J in 1 .. Strlen loop
9038 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
9040 -- If we are out of range, post error. This is one of the
9041 -- very few places that we place the flag in the middle of
9042 -- a token, right under the offending wide character.
9044 -- This is not quite right, because characters in general
9045 -- will take more than one character position ???
9048 ("literal out of range of type Standard.Wide_Character",
9049 Source_Ptr (Int (Loc) + J));
9054 -- If the root type is not a standard character, then we will convert
9055 -- the string into an aggregate and will let the aggregate code do
9056 -- the checking. Standard Wide_Wide_Character is also OK here.
9062 -- See if the component type of the array corresponding to the string
9063 -- has compile time known bounds. If yes we can directly check
9064 -- whether the evaluation of the string will raise constraint error.
9065 -- Otherwise we need to transform the string literal into the
9066 -- corresponding character aggregate and let the aggregate code do
9069 if Is_Standard_Character_Type (R_Typ) then
9071 -- Check for the case of full range, where we are definitely OK
9073 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
9077 -- Here the range is not the complete base type range, so check
9080 Comp_Typ_Lo : constant Node_Id :=
9081 Type_Low_Bound (Component_Type (Typ));
9082 Comp_Typ_Hi : constant Node_Id :=
9083 Type_High_Bound (Component_Type (Typ));
9088 if Compile_Time_Known_Value (Comp_Typ_Lo)
9089 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9091 for J in 1 .. Strlen loop
9092 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9094 if Char_Val < Expr_Value (Comp_Typ_Lo)
9095 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9097 Apply_Compile_Time_Constraint_Error
9098 (N, "character out of range?", CE_Range_Check_Failed,
9099 Loc => Source_Ptr (Int (Loc) + J));
9109 -- If we got here we meed to transform the string literal into the
9110 -- equivalent qualified positional array aggregate. This is rather
9111 -- heavy artillery for this situation, but it is hard work to avoid.
9114 Lits : constant List_Id := New_List;
9115 P : Source_Ptr := Loc + 1;
9119 -- Build the character literals, we give them source locations that
9120 -- correspond to the string positions, which is a bit tricky given
9121 -- the possible presence of wide character escape sequences.
9123 for J in 1 .. Strlen loop
9124 C := Get_String_Char (Str, J);
9125 Set_Character_Literal_Name (C);
9128 Make_Character_Literal (P,
9130 Char_Literal_Value => UI_From_CC (C)));
9132 if In_Character_Range (C) then
9135 -- Should we have a call to Skip_Wide here ???
9144 Make_Qualified_Expression (Loc,
9145 Subtype_Mark => New_Reference_To (Typ, Loc),
9147 Make_Aggregate (Loc, Expressions => Lits)));
9149 Analyze_And_Resolve (N, Typ);
9151 end Resolve_String_Literal;
9153 -----------------------------
9154 -- Resolve_Subprogram_Info --
9155 -----------------------------
9157 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9160 end Resolve_Subprogram_Info;
9162 -----------------------------
9163 -- Resolve_Type_Conversion --
9164 -----------------------------
9166 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9167 Conv_OK : constant Boolean := Conversion_OK (N);
9168 Operand : constant Node_Id := Expression (N);
9169 Operand_Typ : constant Entity_Id := Etype (Operand);
9170 Target_Typ : constant Entity_Id := Etype (N);
9175 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9176 -- Set to False to suppress cases where we want to suppress the test
9177 -- for redundancy to avoid possible false positives on this warning.
9181 and then not Valid_Conversion (N, Target_Typ, Operand)
9186 -- If the Operand Etype is Universal_Fixed, then the conversion is
9187 -- never redundant. We need this check because by the time we have
9188 -- finished the rather complex transformation, the conversion looks
9189 -- redundant when it is not.
9191 if Operand_Typ = Universal_Fixed then
9192 Test_Redundant := False;
9194 -- If the operand is marked as Any_Fixed, then special processing is
9195 -- required. This is also a case where we suppress the test for a
9196 -- redundant conversion, since most certainly it is not redundant.
9198 elsif Operand_Typ = Any_Fixed then
9199 Test_Redundant := False;
9201 -- Mixed-mode operation involving a literal. Context must be a fixed
9202 -- type which is applied to the literal subsequently.
9204 if Is_Fixed_Point_Type (Typ) then
9205 Set_Etype (Operand, Universal_Real);
9207 elsif Is_Numeric_Type (Typ)
9208 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9209 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9211 Etype (Left_Opnd (Operand)) = Universal_Real)
9213 -- Return if expression is ambiguous
9215 if Unique_Fixed_Point_Type (N) = Any_Type then
9218 -- If nothing else, the available fixed type is Duration
9221 Set_Etype (Operand, Standard_Duration);
9224 -- Resolve the real operand with largest available precision
9226 if Etype (Right_Opnd (Operand)) = Universal_Real then
9227 Rop := New_Copy_Tree (Right_Opnd (Operand));
9229 Rop := New_Copy_Tree (Left_Opnd (Operand));
9232 Resolve (Rop, Universal_Real);
9234 -- If the operand is a literal (it could be a non-static and
9235 -- illegal exponentiation) check whether the use of Duration
9236 -- is potentially inaccurate.
9238 if Nkind (Rop) = N_Real_Literal
9239 and then Realval (Rop) /= Ureal_0
9240 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9243 ("?universal real operand can only " &
9244 "be interpreted as Duration!",
9247 ("\?precision will be lost in the conversion!", Rop);
9250 elsif Is_Numeric_Type (Typ)
9251 and then Nkind (Operand) in N_Op
9252 and then Unique_Fixed_Point_Type (N) /= Any_Type
9254 Set_Etype (Operand, Standard_Duration);
9257 Error_Msg_N ("invalid context for mixed mode operation", N);
9258 Set_Etype (Operand, Any_Type);
9265 -- In SPARK, a type conversion between array types should be restricted
9266 -- to types which have matching static bounds.
9268 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9269 -- operation if not needed.
9271 if Restriction_Check_Required (SPARK)
9272 and then Is_Array_Type (Target_Typ)
9273 and then Is_Array_Type (Operand_Typ)
9274 and then Operand_Typ /= Any_Composite -- or else Operand in error
9275 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9277 Check_SPARK_Restriction
9278 ("array types should have matching static bounds", N);
9281 -- In formal mode, the operand of an ancestor type conversion must be an
9282 -- object (not an expression).
9284 if Is_Tagged_Type (Target_Typ)
9285 and then not Is_Class_Wide_Type (Target_Typ)
9286 and then Is_Tagged_Type (Operand_Typ)
9287 and then not Is_Class_Wide_Type (Operand_Typ)
9288 and then Is_Ancestor (Target_Typ, Operand_Typ)
9289 and then not Is_SPARK_Object_Reference (Operand)
9291 Check_SPARK_Restriction ("object required", Operand);
9294 -- Note: we do the Eval_Type_Conversion call before applying the
9295 -- required checks for a subtype conversion. This is important, since
9296 -- both are prepared under certain circumstances to change the type
9297 -- conversion to a constraint error node, but in the case of
9298 -- Eval_Type_Conversion this may reflect an illegality in the static
9299 -- case, and we would miss the illegality (getting only a warning
9300 -- message), if we applied the type conversion checks first.
9302 Eval_Type_Conversion (N);
9304 -- Even when evaluation is not possible, we may be able to simplify the
9305 -- conversion or its expression. This needs to be done before applying
9306 -- checks, since otherwise the checks may use the original expression
9307 -- and defeat the simplifications. This is specifically the case for
9308 -- elimination of the floating-point Truncation attribute in
9309 -- float-to-int conversions.
9311 Simplify_Type_Conversion (N);
9313 -- If after evaluation we still have a type conversion, then we may need
9314 -- to apply checks required for a subtype conversion.
9316 -- Skip these type conversion checks if universal fixed operands
9317 -- operands involved, since range checks are handled separately for
9318 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9320 if Nkind (N) = N_Type_Conversion
9321 and then not Is_Generic_Type (Root_Type (Target_Typ))
9322 and then Target_Typ /= Universal_Fixed
9323 and then Operand_Typ /= Universal_Fixed
9325 Apply_Type_Conversion_Checks (N);
9328 -- Issue warning for conversion of simple object to its own type. We
9329 -- have to test the original nodes, since they may have been rewritten
9330 -- by various optimizations.
9332 Orig_N := Original_Node (N);
9334 -- Here we test for a redundant conversion if the warning mode is
9335 -- active (and was not locally reset), and we have a type conversion
9336 -- from source not appearing in a generic instance.
9339 and then Nkind (Orig_N) = N_Type_Conversion
9340 and then Comes_From_Source (Orig_N)
9341 and then not In_Instance
9343 Orig_N := Original_Node (Expression (Orig_N));
9344 Orig_T := Target_Typ;
9346 -- If the node is part of a larger expression, the Target_Type
9347 -- may not be the original type of the node if the context is a
9348 -- condition. Recover original type to see if conversion is needed.
9350 if Is_Boolean_Type (Orig_T)
9351 and then Nkind (Parent (N)) in N_Op
9353 Orig_T := Etype (Parent (N));
9356 -- If we have an entity name, then give the warning if the entity
9357 -- is the right type, or if it is a loop parameter covered by the
9358 -- original type (that's needed because loop parameters have an
9359 -- odd subtype coming from the bounds).
9361 if (Is_Entity_Name (Orig_N)
9363 (Etype (Entity (Orig_N)) = Orig_T
9365 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9366 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9368 -- If not an entity, then type of expression must match
9370 or else Etype (Orig_N) = Orig_T
9372 -- One more check, do not give warning if the analyzed conversion
9373 -- has an expression with non-static bounds, and the bounds of the
9374 -- target are static. This avoids junk warnings in cases where the
9375 -- conversion is necessary to establish staticness, for example in
9376 -- a case statement.
9378 if not Is_OK_Static_Subtype (Operand_Typ)
9379 and then Is_OK_Static_Subtype (Target_Typ)
9383 -- Finally, if this type conversion occurs in a context requiring
9384 -- a prefix, and the expression is a qualified expression then the
9385 -- type conversion is not redundant, since a qualified expression
9386 -- is not a prefix, whereas a type conversion is. For example, "X
9387 -- := T'(Funx(...)).Y;" is illegal because a selected component
9388 -- requires a prefix, but a type conversion makes it legal: "X :=
9389 -- T(T'(Funx(...))).Y;"
9391 -- In Ada 2012, a qualified expression is a name, so this idiom is
9392 -- no longer needed, but we still suppress the warning because it
9393 -- seems unfriendly for warnings to pop up when you switch to the
9394 -- newer language version.
9396 elsif Nkind (Orig_N) = N_Qualified_Expression
9397 and then Nkind_In (Parent (N), N_Attribute_Reference,
9398 N_Indexed_Component,
9399 N_Selected_Component,
9401 N_Explicit_Dereference)
9405 -- Here we give the redundant conversion warning. If it is an
9406 -- entity, give the name of the entity in the message. If not,
9407 -- just mention the expression.
9410 if Is_Entity_Name (Orig_N) then
9411 Error_Msg_Node_2 := Orig_T;
9412 Error_Msg_NE -- CODEFIX
9413 ("?redundant conversion, & is of type &!",
9414 N, Entity (Orig_N));
9417 ("?redundant conversion, expression is of type&!",
9424 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9425 -- No need to perform any interface conversion if the type of the
9426 -- expression coincides with the target type.
9428 if Ada_Version >= Ada_2005
9429 and then Full_Expander_Active
9430 and then Operand_Typ /= Target_Typ
9433 Opnd : Entity_Id := Operand_Typ;
9434 Target : Entity_Id := Target_Typ;
9437 if Is_Access_Type (Opnd) then
9438 Opnd := Designated_Type (Opnd);
9441 if Is_Access_Type (Target_Typ) then
9442 Target := Designated_Type (Target);
9445 if Opnd = Target then
9448 -- Conversion from interface type
9450 elsif Is_Interface (Opnd) then
9452 -- Ada 2005 (AI-217): Handle entities from limited views
9454 if From_With_Type (Opnd) then
9455 Error_Msg_Qual_Level := 99;
9456 Error_Msg_NE -- CODEFIX
9457 ("missing WITH clause on package &", N,
9458 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9460 ("type conversions require visibility of the full view",
9463 elsif From_With_Type (Target)
9465 (Is_Access_Type (Target_Typ)
9466 and then Present (Non_Limited_View (Etype (Target))))
9468 Error_Msg_Qual_Level := 99;
9469 Error_Msg_NE -- CODEFIX
9470 ("missing WITH clause on package &", N,
9471 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9473 ("type conversions require visibility of the full view",
9477 Expand_Interface_Conversion (N, Is_Static => False);
9480 -- Conversion to interface type
9482 elsif Is_Interface (Target) then
9486 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9487 Opnd := Etype (Opnd);
9490 if not Interface_Present_In_Ancestor
9494 if Is_Class_Wide_Type (Opnd) then
9496 -- The static analysis is not enough to know if the
9497 -- interface is implemented or not. Hence we must pass
9498 -- the work to the expander to generate code to evaluate
9499 -- the conversion at run time.
9501 Expand_Interface_Conversion (N, Is_Static => False);
9504 Error_Msg_Name_1 := Chars (Etype (Target));
9505 Error_Msg_Name_2 := Chars (Opnd);
9507 ("wrong interface conversion (% is not a progenitor " &
9512 Expand_Interface_Conversion (N);
9517 end Resolve_Type_Conversion;
9519 ----------------------
9520 -- Resolve_Unary_Op --
9521 ----------------------
9523 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9524 B_Typ : constant Entity_Id := Base_Type (Typ);
9525 R : constant Node_Id := Right_Opnd (N);
9531 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9532 Error_Msg_Name_1 := Chars (Typ);
9533 Check_SPARK_Restriction
9534 ("unary operator not defined for modular type%", N);
9537 -- Deal with intrinsic unary operators
9539 if Comes_From_Source (N)
9540 and then Ekind (Entity (N)) = E_Function
9541 and then Is_Imported (Entity (N))
9542 and then Is_Intrinsic_Subprogram (Entity (N))
9544 Resolve_Intrinsic_Unary_Operator (N, Typ);
9548 -- Deal with universal cases
9550 if Etype (R) = Universal_Integer
9552 Etype (R) = Universal_Real
9554 Check_For_Visible_Operator (N, B_Typ);
9557 Set_Etype (N, B_Typ);
9560 -- Generate warning for expressions like abs (x mod 2)
9562 if Warn_On_Redundant_Constructs
9563 and then Nkind (N) = N_Op_Abs
9565 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9567 if OK and then Hi >= Lo and then Lo >= 0 then
9568 Error_Msg_N -- CODEFIX
9569 ("?abs applied to known non-negative value has no effect", N);
9573 -- Deal with reference generation
9575 Check_Unset_Reference (R);
9576 Generate_Operator_Reference (N, B_Typ);
9579 -- Set overflow checking bit. Much cleverer code needed here eventually
9580 -- and perhaps the Resolve routines should be separated for the various
9581 -- arithmetic operations, since they will need different processing ???
9583 if Nkind (N) in N_Op then
9584 if not Overflow_Checks_Suppressed (Etype (N)) then
9585 Enable_Overflow_Check (N);
9589 -- Generate warning for expressions like -5 mod 3 for integers. No need
9590 -- to worry in the floating-point case, since parens do not affect the
9591 -- result so there is no point in giving in a warning.
9594 Norig : constant Node_Id := Original_Node (N);
9603 if Warn_On_Questionable_Missing_Parens
9604 and then Comes_From_Source (Norig)
9605 and then Is_Integer_Type (Typ)
9606 and then Nkind (Norig) = N_Op_Minus
9608 Rorig := Original_Node (Right_Opnd (Norig));
9610 -- We are looking for cases where the right operand is not
9611 -- parenthesized, and is a binary operator, multiply, divide, or
9612 -- mod. These are the cases where the grouping can affect results.
9614 if Paren_Count (Rorig) = 0
9615 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9617 -- For mod, we always give the warning, since the value is
9618 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9619 -- -(5 mod 315)). But for the other cases, the only concern is
9620 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9621 -- overflows, but (-2) * 64 does not). So we try to give the
9622 -- message only when overflow is possible.
9624 if Nkind (Rorig) /= N_Op_Mod
9625 and then Compile_Time_Known_Value (R)
9627 Val := Expr_Value (R);
9629 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9630 HB := Expr_Value (Type_High_Bound (Typ));
9632 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9635 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9636 LB := Expr_Value (Type_Low_Bound (Typ));
9638 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9641 -- Note that the test below is deliberately excluding the
9642 -- largest negative number, since that is a potentially
9643 -- troublesome case (e.g. -2 * x, where the result is the
9644 -- largest negative integer has an overflow with 2 * x).
9646 if Val > LB and then Val <= HB then
9651 -- For the multiplication case, the only case we have to worry
9652 -- about is when (-a)*b is exactly the largest negative number
9653 -- so that -(a*b) can cause overflow. This can only happen if
9654 -- a is a power of 2, and more generally if any operand is a
9655 -- constant that is not a power of 2, then the parentheses
9656 -- cannot affect whether overflow occurs. We only bother to
9657 -- test the left most operand
9659 -- Loop looking at left operands for one that has known value
9662 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9663 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9664 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9666 -- Operand value of 0 or 1 skips warning
9671 -- Otherwise check power of 2, if power of 2, warn, if
9672 -- anything else, skip warning.
9675 while Lval /= 2 loop
9676 if Lval mod 2 = 1 then
9687 -- Keep looking at left operands
9689 Opnd := Left_Opnd (Opnd);
9692 -- For rem or "/" we can only have a problematic situation
9693 -- if the divisor has a value of minus one or one. Otherwise
9694 -- overflow is impossible (divisor > 1) or we have a case of
9695 -- division by zero in any case.
9697 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9698 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9699 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9704 -- If we fall through warning should be issued
9707 ("?unary minus expression should be parenthesized here!", N);
9711 end Resolve_Unary_Op;
9713 ----------------------------------
9714 -- Resolve_Unchecked_Expression --
9715 ----------------------------------
9717 procedure Resolve_Unchecked_Expression
9722 Resolve (Expression (N), Typ, Suppress => All_Checks);
9724 end Resolve_Unchecked_Expression;
9726 ---------------------------------------
9727 -- Resolve_Unchecked_Type_Conversion --
9728 ---------------------------------------
9730 procedure Resolve_Unchecked_Type_Conversion
9734 pragma Warnings (Off, Typ);
9736 Operand : constant Node_Id := Expression (N);
9737 Opnd_Type : constant Entity_Id := Etype (Operand);
9740 -- Resolve operand using its own type
9742 Resolve (Operand, Opnd_Type);
9743 Eval_Unchecked_Conversion (N);
9744 end Resolve_Unchecked_Type_Conversion;
9746 ------------------------------
9747 -- Rewrite_Operator_As_Call --
9748 ------------------------------
9750 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9751 Loc : constant Source_Ptr := Sloc (N);
9752 Actuals : constant List_Id := New_List;
9756 if Nkind (N) in N_Binary_Op then
9757 Append (Left_Opnd (N), Actuals);
9760 Append (Right_Opnd (N), Actuals);
9763 Make_Function_Call (Sloc => Loc,
9764 Name => New_Occurrence_Of (Nam, Loc),
9765 Parameter_Associations => Actuals);
9767 Preserve_Comes_From_Source (New_N, N);
9768 Preserve_Comes_From_Source (Name (New_N), N);
9770 Set_Etype (N, Etype (Nam));
9771 end Rewrite_Operator_As_Call;
9773 ------------------------------
9774 -- Rewrite_Renamed_Operator --
9775 ------------------------------
9777 procedure Rewrite_Renamed_Operator
9782 Nam : constant Name_Id := Chars (Op);
9783 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9787 -- Rewrite the operator node using the real operator, not its renaming.
9788 -- Exclude user-defined intrinsic operations of the same name, which are
9789 -- treated separately and rewritten as calls.
9791 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9792 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9793 Set_Chars (Op_Node, Nam);
9794 Set_Etype (Op_Node, Etype (N));
9795 Set_Entity (Op_Node, Op);
9796 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9798 -- Indicate that both the original entity and its renaming are
9799 -- referenced at this point.
9801 Generate_Reference (Entity (N), N);
9802 Generate_Reference (Op, N);
9805 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9808 Rewrite (N, Op_Node);
9810 -- If the context type is private, add the appropriate conversions so
9811 -- that the operator is applied to the full view. This is done in the
9812 -- routines that resolve intrinsic operators.
9814 if Is_Intrinsic_Subprogram (Op)
9815 and then Is_Private_Type (Typ)
9818 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9819 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9820 Resolve_Intrinsic_Operator (N, Typ);
9822 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9823 Resolve_Intrinsic_Unary_Operator (N, Typ);
9830 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9832 -- Operator renames a user-defined operator of the same name. Use the
9833 -- original operator in the node, which is the one Gigi knows about.
9836 Set_Is_Overloaded (N, False);
9838 end Rewrite_Renamed_Operator;
9840 -----------------------
9841 -- Set_Slice_Subtype --
9842 -----------------------
9844 -- Build an implicit subtype declaration to represent the type delivered by
9845 -- the slice. This is an abbreviated version of an array subtype. We define
9846 -- an index subtype for the slice, using either the subtype name or the
9847 -- discrete range of the slice. To be consistent with index usage elsewhere
9848 -- we create a list header to hold the single index. This list is not
9849 -- otherwise attached to the syntax tree.
9851 procedure Set_Slice_Subtype (N : Node_Id) is
9852 Loc : constant Source_Ptr := Sloc (N);
9853 Index_List : constant List_Id := New_List;
9855 Index_Subtype : Entity_Id;
9856 Index_Type : Entity_Id;
9857 Slice_Subtype : Entity_Id;
9858 Drange : constant Node_Id := Discrete_Range (N);
9861 if Is_Entity_Name (Drange) then
9862 Index_Subtype := Entity (Drange);
9865 -- We force the evaluation of a range. This is definitely needed in
9866 -- the renamed case, and seems safer to do unconditionally. Note in
9867 -- any case that since we will create and insert an Itype referring
9868 -- to this range, we must make sure any side effect removal actions
9869 -- are inserted before the Itype definition.
9871 if Nkind (Drange) = N_Range then
9872 Force_Evaluation (Low_Bound (Drange));
9873 Force_Evaluation (High_Bound (Drange));
9876 Index_Type := Base_Type (Etype (Drange));
9878 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9880 -- Take a new copy of Drange (where bounds have been rewritten to
9881 -- reference side-effect-free names). Using a separate tree ensures
9882 -- that further expansion (e.g. while rewriting a slice assignment
9883 -- into a FOR loop) does not attempt to remove side effects on the
9884 -- bounds again (which would cause the bounds in the index subtype
9885 -- definition to refer to temporaries before they are defined) (the
9886 -- reason is that some names are considered side effect free here
9887 -- for the subtype, but not in the context of a loop iteration
9890 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9891 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
9892 Set_Etype (Index_Subtype, Index_Type);
9893 Set_Size_Info (Index_Subtype, Index_Type);
9894 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9897 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9899 Index := New_Occurrence_Of (Index_Subtype, Loc);
9900 Set_Etype (Index, Index_Subtype);
9901 Append (Index, Index_List);
9903 Set_First_Index (Slice_Subtype, Index);
9904 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9905 Set_Is_Constrained (Slice_Subtype, True);
9907 Check_Compile_Time_Size (Slice_Subtype);
9909 -- The Etype of the existing Slice node is reset to this slice subtype.
9910 -- Its bounds are obtained from its first index.
9912 Set_Etype (N, Slice_Subtype);
9914 -- For packed slice subtypes, freeze immediately (except in the case of
9915 -- being in a "spec expression" where we never freeze when we first see
9918 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9919 Freeze_Itype (Slice_Subtype, N);
9921 -- For all other cases insert an itype reference in the slice's actions
9922 -- so that the itype is frozen at the proper place in the tree (i.e. at
9923 -- the point where actions for the slice are analyzed). Note that this
9924 -- is different from freezing the itype immediately, which might be
9925 -- premature (e.g. if the slice is within a transient scope). This needs
9926 -- to be done only if expansion is enabled.
9928 elsif Full_Expander_Active then
9929 Ensure_Defined (Typ => Slice_Subtype, N => N);
9931 end Set_Slice_Subtype;
9933 --------------------------------
9934 -- Set_String_Literal_Subtype --
9935 --------------------------------
9937 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9938 Loc : constant Source_Ptr := Sloc (N);
9939 Low_Bound : constant Node_Id :=
9940 Type_Low_Bound (Etype (First_Index (Typ)));
9941 Subtype_Id : Entity_Id;
9944 if Nkind (N) /= N_String_Literal then
9948 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
9949 Set_String_Literal_Length (Subtype_Id, UI_From_Int
9950 (String_Length (Strval (N))));
9951 Set_Etype (Subtype_Id, Base_Type (Typ));
9952 Set_Is_Constrained (Subtype_Id);
9953 Set_Etype (N, Subtype_Id);
9955 if Is_OK_Static_Expression (Low_Bound) then
9957 -- The low bound is set from the low bound of the corresponding index
9958 -- type. Note that we do not store the high bound in the string literal
9959 -- subtype, but it can be deduced if necessary from the length and the
9962 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
9965 -- If the lower bound is not static we create a range for the string
9966 -- literal, using the index type and the known length of the literal.
9967 -- The index type is not necessarily Positive, so the upper bound is
9968 -- computed as T'Val (T'Pos (Low_Bound) + L - 1)
9971 Index_List : constant List_Id := New_List;
9972 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
9974 High_Bound : constant Node_Id :=
9975 Make_Attribute_Reference (Loc,
9976 Attribute_Name => Name_Val,
9978 New_Occurrence_Of (Index_Type, Loc),
9979 Expressions => New_List (
9982 Make_Attribute_Reference (Loc,
9983 Attribute_Name => Name_Pos,
9985 New_Occurrence_Of (Index_Type, Loc),
9987 New_List (New_Copy_Tree (Low_Bound))),
9989 Make_Integer_Literal (Loc,
9990 String_Length (Strval (N)) - 1))));
9992 Array_Subtype : Entity_Id;
9993 Index_Subtype : Entity_Id;
9998 if Is_Integer_Type (Index_Type) then
9999 Set_String_Literal_Low_Bound
10000 (Subtype_Id, Make_Integer_Literal (Loc, 1));
10003 -- If the index type is an enumeration type, build bounds
10004 -- expression with attributes.
10006 Set_String_Literal_Low_Bound
10008 Make_Attribute_Reference (Loc,
10009 Attribute_Name => Name_First,
10011 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
10012 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
10015 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
10017 -- Build bona fide subtype for the string, and wrap it in an
10018 -- unchecked conversion, because the backend expects the
10019 -- String_Literal_Subtype to have a static lower bound.
10022 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10023 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
10024 Set_Scalar_Range (Index_Subtype, Drange);
10025 Set_Parent (Drange, N);
10026 Analyze_And_Resolve (Drange, Index_Type);
10028 -- In the context, the Index_Type may already have a constraint,
10029 -- so use common base type on string subtype. The base type may
10030 -- be used when generating attributes of the string, for example
10031 -- in the context of a slice assignment.
10033 Set_Etype (Index_Subtype, Base_Type (Index_Type));
10034 Set_Size_Info (Index_Subtype, Index_Type);
10035 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10037 Array_Subtype := Create_Itype (E_Array_Subtype, N);
10039 Index := New_Occurrence_Of (Index_Subtype, Loc);
10040 Set_Etype (Index, Index_Subtype);
10041 Append (Index, Index_List);
10043 Set_First_Index (Array_Subtype, Index);
10044 Set_Etype (Array_Subtype, Base_Type (Typ));
10045 Set_Is_Constrained (Array_Subtype, True);
10048 Make_Unchecked_Type_Conversion (Loc,
10049 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
10050 Expression => Relocate_Node (N)));
10051 Set_Etype (N, Array_Subtype);
10054 end Set_String_Literal_Subtype;
10056 ------------------------------
10057 -- Simplify_Type_Conversion --
10058 ------------------------------
10060 procedure Simplify_Type_Conversion (N : Node_Id) is
10062 if Nkind (N) = N_Type_Conversion then
10064 Operand : constant Node_Id := Expression (N);
10065 Target_Typ : constant Entity_Id := Etype (N);
10066 Opnd_Typ : constant Entity_Id := Etype (Operand);
10069 if Is_Floating_Point_Type (Opnd_Typ)
10071 (Is_Integer_Type (Target_Typ)
10072 or else (Is_Fixed_Point_Type (Target_Typ)
10073 and then Conversion_OK (N)))
10074 and then Nkind (Operand) = N_Attribute_Reference
10075 and then Attribute_Name (Operand) = Name_Truncation
10077 -- Special processing required if the conversion is the expression
10078 -- of a Truncation attribute reference. In this case we replace:
10080 -- ityp (ftyp'Truncation (x))
10086 -- with the Float_Truncate flag set, which is more efficient.
10090 Relocate_Node (First (Expressions (Operand))));
10091 Set_Float_Truncate (N, True);
10095 end Simplify_Type_Conversion;
10097 -----------------------------
10098 -- Unique_Fixed_Point_Type --
10099 -----------------------------
10101 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10102 T1 : Entity_Id := Empty;
10107 procedure Fixed_Point_Error;
10108 -- Give error messages for true ambiguity. Messages are posted on node
10109 -- N, and entities T1, T2 are the possible interpretations.
10111 -----------------------
10112 -- Fixed_Point_Error --
10113 -----------------------
10115 procedure Fixed_Point_Error is
10117 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10118 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10119 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10120 end Fixed_Point_Error;
10122 -- Start of processing for Unique_Fixed_Point_Type
10125 -- The operations on Duration are visible, so Duration is always a
10126 -- possible interpretation.
10128 T1 := Standard_Duration;
10130 -- Look for fixed-point types in enclosing scopes
10132 Scop := Current_Scope;
10133 while Scop /= Standard_Standard loop
10134 T2 := First_Entity (Scop);
10135 while Present (T2) loop
10136 if Is_Fixed_Point_Type (T2)
10137 and then Current_Entity (T2) = T2
10138 and then Scope (Base_Type (T2)) = Scop
10140 if Present (T1) then
10151 Scop := Scope (Scop);
10154 -- Look for visible fixed type declarations in the context
10156 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10157 while Present (Item) loop
10158 if Nkind (Item) = N_With_Clause then
10159 Scop := Entity (Name (Item));
10160 T2 := First_Entity (Scop);
10161 while Present (T2) loop
10162 if Is_Fixed_Point_Type (T2)
10163 and then Scope (Base_Type (T2)) = Scop
10164 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10166 if Present (T1) then
10181 if Nkind (N) = N_Real_Literal then
10182 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
10184 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
10188 end Unique_Fixed_Point_Type;
10190 ----------------------
10191 -- Valid_Conversion --
10192 ----------------------
10194 function Valid_Conversion
10196 Target : Entity_Id;
10198 Report_Errs : Boolean := True) return Boolean
10200 Target_Type : constant Entity_Id := Base_Type (Target);
10201 Opnd_Type : Entity_Id := Etype (Operand);
10203 function Conversion_Check
10205 Msg : String) return Boolean;
10206 -- Little routine to post Msg if Valid is False, returns Valid value
10208 -- The following are badly named, this kind of overloading is actively
10209 -- confusing in reading code, please rename to something like
10210 -- Error_Msg_N_If_Reporting ???
10212 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id);
10213 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10215 procedure Error_Msg_NE
10217 N : Node_Or_Entity_Id;
10218 E : Node_Or_Entity_Id);
10219 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10221 function Valid_Tagged_Conversion
10222 (Target_Type : Entity_Id;
10223 Opnd_Type : Entity_Id) return Boolean;
10224 -- Specifically test for validity of tagged conversions
10226 function Valid_Array_Conversion return Boolean;
10227 -- Check index and component conformance, and accessibility levels if
10228 -- the component types are anonymous access types (Ada 2005).
10230 ----------------------
10231 -- Conversion_Check --
10232 ----------------------
10234 function Conversion_Check
10236 Msg : String) return Boolean
10241 -- A generic unit has already been analyzed and we have verified
10242 -- that a particular conversion is OK in that context. Since the
10243 -- instance is reanalyzed without relying on the relationships
10244 -- established during the analysis of the generic, it is possible
10245 -- to end up with inconsistent views of private types. Do not emit
10246 -- the error message in such cases. The rest of the machinery in
10247 -- Valid_Conversion still ensures the proper compatibility of
10248 -- target and operand types.
10250 and then not In_Instance
10252 Error_Msg_N (Msg, Operand);
10256 end Conversion_Check;
10262 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id) is
10264 if Report_Errs then
10265 Errout.Error_Msg_N (Msg, N);
10273 procedure Error_Msg_NE
10275 N : Node_Or_Entity_Id;
10276 E : Node_Or_Entity_Id)
10279 if Report_Errs then
10280 Errout.Error_Msg_NE (Msg, N, E);
10284 ----------------------------
10285 -- Valid_Array_Conversion --
10286 ----------------------------
10288 function Valid_Array_Conversion return Boolean
10290 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10291 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10293 Opnd_Index : Node_Id;
10294 Opnd_Index_Type : Entity_Id;
10296 Target_Comp_Type : constant Entity_Id :=
10297 Component_Type (Target_Type);
10298 Target_Comp_Base : constant Entity_Id :=
10299 Base_Type (Target_Comp_Type);
10301 Target_Index : Node_Id;
10302 Target_Index_Type : Entity_Id;
10305 -- Error if wrong number of dimensions
10308 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10311 ("incompatible number of dimensions for conversion", Operand);
10314 -- Number of dimensions matches
10317 -- Loop through indexes of the two arrays
10319 Target_Index := First_Index (Target_Type);
10320 Opnd_Index := First_Index (Opnd_Type);
10321 while Present (Target_Index) and then Present (Opnd_Index) loop
10322 Target_Index_Type := Etype (Target_Index);
10323 Opnd_Index_Type := Etype (Opnd_Index);
10325 -- Error if index types are incompatible
10327 if not (Is_Integer_Type (Target_Index_Type)
10328 and then Is_Integer_Type (Opnd_Index_Type))
10329 and then (Root_Type (Target_Index_Type)
10330 /= Root_Type (Opnd_Index_Type))
10333 ("incompatible index types for array conversion",
10338 Next_Index (Target_Index);
10339 Next_Index (Opnd_Index);
10342 -- If component types have same base type, all set
10344 if Target_Comp_Base = Opnd_Comp_Base then
10347 -- Here if base types of components are not the same. The only
10348 -- time this is allowed is if we have anonymous access types.
10350 -- The conversion of arrays of anonymous access types can lead
10351 -- to dangling pointers. AI-392 formalizes the accessibility
10352 -- checks that must be applied to such conversions to prevent
10353 -- out-of-scope references.
10356 (Target_Comp_Base, E_Anonymous_Access_Type,
10357 E_Anonymous_Access_Subprogram_Type)
10358 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10360 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10362 if Type_Access_Level (Target_Type) <
10363 Deepest_Type_Access_Level (Opnd_Type)
10365 if In_Instance_Body then
10367 ("?source array type has " &
10368 "deeper accessibility level than target", Operand);
10370 ("\?Program_Error will be raised at run time",
10373 Make_Raise_Program_Error (Sloc (N),
10374 Reason => PE_Accessibility_Check_Failed));
10375 Set_Etype (N, Target_Type);
10378 -- Conversion not allowed because of accessibility levels
10382 ("source array type has " &
10383 "deeper accessibility level than target", Operand);
10391 -- All other cases where component base types do not match
10395 ("incompatible component types for array conversion",
10400 -- Check that component subtypes statically match. For numeric
10401 -- types this means that both must be either constrained or
10402 -- unconstrained. For enumeration types the bounds must match.
10403 -- All of this is checked in Subtypes_Statically_Match.
10405 if not Subtypes_Statically_Match
10406 (Target_Comp_Type, Opnd_Comp_Type)
10409 ("component subtypes must statically match", Operand);
10415 end Valid_Array_Conversion;
10417 -----------------------------
10418 -- Valid_Tagged_Conversion --
10419 -----------------------------
10421 function Valid_Tagged_Conversion
10422 (Target_Type : Entity_Id;
10423 Opnd_Type : Entity_Id) return Boolean
10426 -- Upward conversions are allowed (RM 4.6(22))
10428 if Covers (Target_Type, Opnd_Type)
10429 or else Is_Ancestor (Target_Type, Opnd_Type)
10433 -- Downward conversion are allowed if the operand is class-wide
10436 elsif Is_Class_Wide_Type (Opnd_Type)
10437 and then Covers (Opnd_Type, Target_Type)
10441 elsif Covers (Opnd_Type, Target_Type)
10442 or else Is_Ancestor (Opnd_Type, Target_Type)
10445 Conversion_Check (False,
10446 "downward conversion of tagged objects not allowed");
10448 -- Ada 2005 (AI-251): The conversion to/from interface types is
10451 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10454 -- If the operand is a class-wide type obtained through a limited_
10455 -- with clause, and the context includes the non-limited view, use
10456 -- it to determine whether the conversion is legal.
10458 elsif Is_Class_Wide_Type (Opnd_Type)
10459 and then From_With_Type (Opnd_Type)
10460 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10461 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10465 elsif Is_Access_Type (Opnd_Type)
10466 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10472 ("invalid tagged conversion, not compatible with}",
10473 N, First_Subtype (Opnd_Type));
10476 end Valid_Tagged_Conversion;
10478 -- Start of processing for Valid_Conversion
10481 Check_Parameterless_Call (Operand);
10483 if Is_Overloaded (Operand) then
10493 -- Remove procedure calls, which syntactically cannot appear in
10494 -- this context, but which cannot be removed by type checking,
10495 -- because the context does not impose a type.
10497 -- When compiling for VMS, spurious ambiguities can be produced
10498 -- when arithmetic operations have a literal operand and return
10499 -- System.Address or a descendant of it. These ambiguities are
10500 -- otherwise resolved by the context, but for conversions there
10501 -- is no context type and the removal of the spurious operations
10502 -- must be done explicitly here.
10504 -- The node may be labelled overloaded, but still contain only one
10505 -- interpretation because others were discarded earlier. If this
10506 -- is the case, retain the single interpretation if legal.
10508 Get_First_Interp (Operand, I, It);
10509 Opnd_Type := It.Typ;
10510 Get_Next_Interp (I, It);
10512 if Present (It.Typ)
10513 and then Opnd_Type /= Standard_Void_Type
10515 -- More than one candidate interpretation is available
10517 Get_First_Interp (Operand, I, It);
10518 while Present (It.Typ) loop
10519 if It.Typ = Standard_Void_Type then
10523 if Present (System_Aux_Id)
10524 and then Is_Descendent_Of_Address (It.Typ)
10529 Get_Next_Interp (I, It);
10533 Get_First_Interp (Operand, I, It);
10537 if No (It.Typ) then
10538 Error_Msg_N ("illegal operand in conversion", Operand);
10542 Get_Next_Interp (I, It);
10544 if Present (It.Typ) then
10547 It1 := Disambiguate (Operand, I1, I, Any_Type);
10549 if It1 = No_Interp then
10550 Error_Msg_N ("ambiguous operand in conversion", Operand);
10552 -- If the interpretation involves a standard operator, use
10553 -- the location of the type, which may be user-defined.
10555 if Sloc (It.Nam) = Standard_Location then
10556 Error_Msg_Sloc := Sloc (It.Typ);
10558 Error_Msg_Sloc := Sloc (It.Nam);
10561 Error_Msg_N -- CODEFIX
10562 ("\\possible interpretation#!", Operand);
10564 if Sloc (N1) = Standard_Location then
10565 Error_Msg_Sloc := Sloc (T1);
10567 Error_Msg_Sloc := Sloc (N1);
10570 Error_Msg_N -- CODEFIX
10571 ("\\possible interpretation#!", Operand);
10577 Set_Etype (Operand, It1.Typ);
10578 Opnd_Type := It1.Typ;
10584 if Is_Numeric_Type (Target_Type) then
10586 -- A universal fixed expression can be converted to any numeric type
10588 if Opnd_Type = Universal_Fixed then
10591 -- Also no need to check when in an instance or inlined body, because
10592 -- the legality has been established when the template was analyzed.
10593 -- Furthermore, numeric conversions may occur where only a private
10594 -- view of the operand type is visible at the instantiation point.
10595 -- This results in a spurious error if we check that the operand type
10596 -- is a numeric type.
10598 -- Note: in a previous version of this unit, the following tests were
10599 -- applied only for generated code (Comes_From_Source set to False),
10600 -- but in fact the test is required for source code as well, since
10601 -- this situation can arise in source code.
10603 elsif In_Instance or else In_Inlined_Body then
10606 -- Otherwise we need the conversion check
10609 return Conversion_Check
10610 (Is_Numeric_Type (Opnd_Type),
10611 "illegal operand for numeric conversion");
10616 elsif Is_Array_Type (Target_Type) then
10617 if not Is_Array_Type (Opnd_Type)
10618 or else Opnd_Type = Any_Composite
10619 or else Opnd_Type = Any_String
10621 Error_Msg_N ("illegal operand for array conversion", Operand);
10624 return Valid_Array_Conversion;
10627 -- Ada 2005 (AI-251): Anonymous access types where target references an
10630 elsif Ekind_In (Target_Type, E_General_Access_Type,
10631 E_Anonymous_Access_Type)
10632 and then Is_Interface (Directly_Designated_Type (Target_Type))
10634 -- Check the static accessibility rule of 4.6(17). Note that the
10635 -- check is not enforced when within an instance body, since the
10636 -- RM requires such cases to be caught at run time.
10638 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10639 if Type_Access_Level (Opnd_Type) >
10640 Deepest_Type_Access_Level (Target_Type)
10642 -- In an instance, this is a run-time check, but one we know
10643 -- will fail, so generate an appropriate warning. The raise
10644 -- will be generated by Expand_N_Type_Conversion.
10646 if In_Instance_Body then
10648 ("?cannot convert local pointer to non-local access type",
10651 ("\?Program_Error will be raised at run time", Operand);
10655 ("cannot convert local pointer to non-local access type",
10660 -- Special accessibility checks are needed in the case of access
10661 -- discriminants declared for a limited type.
10663 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10664 and then not Is_Local_Anonymous_Access (Opnd_Type)
10666 -- When the operand is a selected access discriminant the check
10667 -- needs to be made against the level of the object denoted by
10668 -- the prefix of the selected name (Object_Access_Level handles
10669 -- checking the prefix of the operand for this case).
10671 if Nkind (Operand) = N_Selected_Component
10672 and then Object_Access_Level (Operand) >
10673 Deepest_Type_Access_Level (Target_Type)
10675 -- In an instance, this is a run-time check, but one we know
10676 -- will fail, so generate an appropriate warning. The raise
10677 -- will be generated by Expand_N_Type_Conversion.
10679 if In_Instance_Body then
10681 ("?cannot convert access discriminant to non-local" &
10682 " access type", Operand);
10684 ("\?Program_Error will be raised at run time", Operand);
10687 ("cannot convert access discriminant to non-local" &
10688 " access type", Operand);
10693 -- The case of a reference to an access discriminant from
10694 -- within a limited type declaration (which will appear as
10695 -- a discriminal) is always illegal because the level of the
10696 -- discriminant is considered to be deeper than any (nameable)
10699 if Is_Entity_Name (Operand)
10700 and then not Is_Local_Anonymous_Access (Opnd_Type)
10702 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10703 and then Present (Discriminal_Link (Entity (Operand)))
10706 ("discriminant has deeper accessibility level than target",
10715 -- General and anonymous access types
10717 elsif Ekind_In (Target_Type, E_General_Access_Type,
10718 E_Anonymous_Access_Type)
10721 (Is_Access_Type (Opnd_Type)
10723 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10724 E_Access_Protected_Subprogram_Type),
10725 "must be an access-to-object type")
10727 if Is_Access_Constant (Opnd_Type)
10728 and then not Is_Access_Constant (Target_Type)
10731 ("access-to-constant operand type not allowed", Operand);
10735 -- Check the static accessibility rule of 4.6(17). Note that the
10736 -- check is not enforced when within an instance body, since the RM
10737 -- requires such cases to be caught at run time.
10739 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10740 or else Is_Local_Anonymous_Access (Target_Type)
10741 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
10742 N_Object_Declaration
10744 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
10745 -- conversions from an anonymous access type to a named general
10746 -- access type. Such conversions are not allowed in the case of
10747 -- access parameters and stand-alone objects of an anonymous
10748 -- access type. The implicit conversion case is recognized by
10749 -- testing that Comes_From_Source is False and that it's been
10750 -- rewritten. The Comes_From_Source test isn't sufficient because
10751 -- nodes in inlined calls to predefined library routines can have
10752 -- Comes_From_Source set to False. (Is there a better way to test
10753 -- for implicit conversions???)
10755 if Ada_Version >= Ada_2012
10756 and then not Comes_From_Source (N)
10757 and then N /= Original_Node (N)
10758 and then Ekind (Target_Type) = E_General_Access_Type
10759 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
10761 if Is_Itype (Opnd_Type) then
10763 -- Implicit conversions aren't allowed for objects of an
10764 -- anonymous access type, since such objects have nonstatic
10765 -- levels in Ada 2012.
10767 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
10768 N_Object_Declaration
10771 ("implicit conversion of stand-alone anonymous " &
10772 "access object not allowed", Operand);
10775 -- Implicit conversions aren't allowed for anonymous access
10776 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
10777 -- is done to exclude anonymous access results.
10779 elsif not Is_Local_Anonymous_Access (Opnd_Type)
10780 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
10781 N_Function_Specification,
10782 N_Procedure_Specification)
10785 ("implicit conversion of anonymous access formal " &
10786 "not allowed", Operand);
10789 -- This is a case where there's an enclosing object whose
10790 -- to which the "statically deeper than" relationship does
10791 -- not apply (such as an access discriminant selected from
10792 -- a dereference of an access parameter).
10794 elsif Object_Access_Level (Operand)
10795 = Scope_Depth (Standard_Standard)
10798 ("implicit conversion of anonymous access value " &
10799 "not allowed", Operand);
10802 -- In other cases, the level of the operand's type must be
10803 -- statically less deep than that of the target type, else
10804 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
10806 elsif Type_Access_Level (Opnd_Type) >
10807 Deepest_Type_Access_Level (Target_Type)
10810 ("implicit conversion of anonymous access value " &
10811 "violates accessibility", Operand);
10816 elsif Type_Access_Level (Opnd_Type) >
10817 Deepest_Type_Access_Level (Target_Type)
10819 -- In an instance, this is a run-time check, but one we know
10820 -- will fail, so generate an appropriate warning. The raise
10821 -- will be generated by Expand_N_Type_Conversion.
10823 if In_Instance_Body then
10825 ("?cannot convert local pointer to non-local access type",
10828 ("\?Program_Error will be raised at run time", Operand);
10831 -- Avoid generation of spurious error message
10833 if not Error_Posted (N) then
10835 ("cannot convert local pointer to non-local access type",
10842 -- Special accessibility checks are needed in the case of access
10843 -- discriminants declared for a limited type.
10845 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10846 and then not Is_Local_Anonymous_Access (Opnd_Type)
10848 -- When the operand is a selected access discriminant the check
10849 -- needs to be made against the level of the object denoted by
10850 -- the prefix of the selected name (Object_Access_Level handles
10851 -- checking the prefix of the operand for this case).
10853 if Nkind (Operand) = N_Selected_Component
10854 and then Object_Access_Level (Operand) >
10855 Deepest_Type_Access_Level (Target_Type)
10857 -- In an instance, this is a run-time check, but one we know
10858 -- will fail, so generate an appropriate warning. The raise
10859 -- will be generated by Expand_N_Type_Conversion.
10861 if In_Instance_Body then
10863 ("?cannot convert access discriminant to non-local" &
10864 " access type", Operand);
10866 ("\?Program_Error will be raised at run time",
10871 ("cannot convert access discriminant to non-local" &
10872 " access type", Operand);
10877 -- The case of a reference to an access discriminant from
10878 -- within a limited type declaration (which will appear as
10879 -- a discriminal) is always illegal because the level of the
10880 -- discriminant is considered to be deeper than any (nameable)
10883 if Is_Entity_Name (Operand)
10885 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10886 and then Present (Discriminal_Link (Entity (Operand)))
10889 ("discriminant has deeper accessibility level than target",
10896 -- In the presence of limited_with clauses we have to use non-limited
10897 -- views, if available.
10899 Check_Limited : declare
10900 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10901 -- Helper function to handle limited views
10903 --------------------------
10904 -- Full_Designated_Type --
10905 --------------------------
10907 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10908 Desig : constant Entity_Id := Designated_Type (T);
10911 -- Handle the limited view of a type
10913 if Is_Incomplete_Type (Desig)
10914 and then From_With_Type (Desig)
10915 and then Present (Non_Limited_View (Desig))
10917 return Available_View (Desig);
10921 end Full_Designated_Type;
10923 -- Local Declarations
10925 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10926 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10928 Same_Base : constant Boolean :=
10929 Base_Type (Target) = Base_Type (Opnd);
10931 -- Start of processing for Check_Limited
10934 if Is_Tagged_Type (Target) then
10935 return Valid_Tagged_Conversion (Target, Opnd);
10938 if not Same_Base then
10940 ("target designated type not compatible with }",
10941 N, Base_Type (Opnd));
10944 -- Ada 2005 AI-384: legality rule is symmetric in both
10945 -- designated types. The conversion is legal (with possible
10946 -- constraint check) if either designated type is
10949 elsif Subtypes_Statically_Match (Target, Opnd)
10951 (Has_Discriminants (Target)
10953 (not Is_Constrained (Opnd)
10954 or else not Is_Constrained (Target)))
10956 -- Special case, if Value_Size has been used to make the
10957 -- sizes different, the conversion is not allowed even
10958 -- though the subtypes statically match.
10960 if Known_Static_RM_Size (Target)
10961 and then Known_Static_RM_Size (Opnd)
10962 and then RM_Size (Target) /= RM_Size (Opnd)
10965 ("target designated subtype not compatible with }",
10968 ("\because sizes of the two designated subtypes differ",
10972 -- Normal case where conversion is allowed
10980 ("target designated subtype not compatible with }",
10987 -- Access to subprogram types. If the operand is an access parameter,
10988 -- the type has a deeper accessibility that any master, and cannot be
10989 -- assigned. We must make an exception if the conversion is part of an
10990 -- assignment and the target is the return object of an extended return
10991 -- statement, because in that case the accessibility check takes place
10992 -- after the return.
10994 elsif Is_Access_Subprogram_Type (Target_Type)
10995 and then No (Corresponding_Remote_Type (Opnd_Type))
10997 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10998 and then Is_Entity_Name (Operand)
10999 and then Ekind (Entity (Operand)) = E_In_Parameter
11001 (Nkind (Parent (N)) /= N_Assignment_Statement
11002 or else not Is_Entity_Name (Name (Parent (N)))
11003 or else not Is_Return_Object (Entity (Name (Parent (N)))))
11006 ("illegal attempt to store anonymous access to subprogram",
11009 ("\value has deeper accessibility than any master " &
11010 "(RM 3.10.2 (13))",
11014 ("\use named access type for& instead of access parameter",
11015 Operand, Entity (Operand));
11018 -- Check that the designated types are subtype conformant
11020 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
11021 Old_Id => Designated_Type (Opnd_Type),
11024 -- Check the static accessibility rule of 4.6(20)
11026 if Type_Access_Level (Opnd_Type) >
11027 Deepest_Type_Access_Level (Target_Type)
11030 ("operand type has deeper accessibility level than target",
11033 -- Check that if the operand type is declared in a generic body,
11034 -- then the target type must be declared within that same body
11035 -- (enforces last sentence of 4.6(20)).
11037 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
11039 O_Gen : constant Node_Id :=
11040 Enclosing_Generic_Body (Opnd_Type);
11045 T_Gen := Enclosing_Generic_Body (Target_Type);
11046 while Present (T_Gen) and then T_Gen /= O_Gen loop
11047 T_Gen := Enclosing_Generic_Body (T_Gen);
11050 if T_Gen /= O_Gen then
11052 ("target type must be declared in same generic body"
11053 & " as operand type", N);
11060 -- Remote subprogram access types
11062 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
11063 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
11065 -- It is valid to convert from one RAS type to another provided
11066 -- that their specification statically match.
11068 Check_Subtype_Conformant
11070 Designated_Type (Corresponding_Remote_Type (Target_Type)),
11072 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
11077 -- If it was legal in the generic, it's legal in the instance
11079 elsif In_Instance_Body then
11082 -- If both are tagged types, check legality of view conversions
11084 elsif Is_Tagged_Type (Target_Type)
11086 Is_Tagged_Type (Opnd_Type)
11088 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
11090 -- Types derived from the same root type are convertible
11092 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
11095 -- In an instance or an inlined body, there may be inconsistent views of
11096 -- the same type, or of types derived from a common root.
11098 elsif (In_Instance or In_Inlined_Body)
11100 Root_Type (Underlying_Type (Target_Type)) =
11101 Root_Type (Underlying_Type (Opnd_Type))
11105 -- Special check for common access type error case
11107 elsif Ekind (Target_Type) = E_Access_Type
11108 and then Is_Access_Type (Opnd_Type)
11110 Error_Msg_N ("target type must be general access type!", N);
11111 Error_Msg_NE -- CODEFIX
11112 ("add ALL to }!", N, Target_Type);
11116 Error_Msg_NE ("invalid conversion, not compatible with }",
11120 end Valid_Conversion;