1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Output; use Output;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Dim; use Sem_Dim;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Elim; use Sem_Elim;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Intr; use Sem_Intr;
67 with Sem_Util; use Sem_Util;
68 with Targparm; use Targparm;
69 with Sem_Type; use Sem_Type;
70 with Sem_Warn; use Sem_Warn;
71 with Sinfo; use Sinfo;
72 with Sinfo.CN; use Sinfo.CN;
73 with Snames; use Snames;
74 with Stand; use Stand;
75 with Stringt; use Stringt;
76 with Style; use Style;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
81 package body Sem_Res is
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
87 -- Second pass (top-down) type checking and overload resolution procedures
88 -- Typ is the type required by context. These procedures propagate the type
89 -- information recursively to the descendants of N. If the node is not
90 -- overloaded, its Etype is established in the first pass. If overloaded,
91 -- the Resolve routines set the correct type. For arith. operators, the
92 -- Etype is the base type of the context.
94 -- Note that Resolve_Attribute is separated off in Sem_Attr
96 function Bad_Unordered_Enumeration_Reference
98 T : Entity_Id) return Boolean;
99 -- Node N contains a potentially dubious reference to type T, either an
100 -- explicit comparison, or an explicit range. This function returns True
101 -- if the type T is an enumeration type for which No pragma Order has been
102 -- given, and the reference N is not in the same extended source unit as
103 -- the declaration of T.
105 procedure Check_Discriminant_Use (N : Node_Id);
106 -- Enforce the restrictions on the use of discriminants when constraining
107 -- a component of a discriminated type (record or concurrent type).
109 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
110 -- Given a node for an operator associated with type T, check that
111 -- the operator is visible. Operators all of whose operands are
112 -- universal must be checked for visibility during resolution
113 -- because their type is not determinable based on their operands.
115 procedure Check_Fully_Declared_Prefix
118 -- Check that the type of the prefix of a dereference is not incomplete
120 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
121 -- Given a call node, N, which is known to occur immediately within the
122 -- subprogram being called, determines whether it is a detectable case of
123 -- an infinite recursion, and if so, outputs appropriate messages. Returns
124 -- True if an infinite recursion is detected, and False otherwise.
126 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
127 -- If the type of the object being initialized uses the secondary stack
128 -- directly or indirectly, create a transient scope for the call to the
129 -- init proc. This is because we do not create transient scopes for the
130 -- initialization of individual components within the init proc itself.
131 -- Could be optimized away perhaps?
133 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
134 -- N is the node for a logical operator. If the operator is predefined, and
135 -- the root type of the operands is Standard.Boolean, then a check is made
136 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
137 -- the style check for Style_Check_Boolean_And_Or.
139 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
140 -- Determine whether E is an access type declared by an access declaration,
141 -- and not an (anonymous) allocator type.
143 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
144 -- Utility to check whether the entity for an operator is a predefined
145 -- operator, in which case the expression is left as an operator in the
146 -- tree (else it is rewritten into a call). An instance of an intrinsic
147 -- conversion operation may be given an operator name, but is not treated
148 -- like an operator. Note that an operator that is an imported back-end
149 -- builtin has convention Intrinsic, but is expected to be rewritten into
150 -- a call, so such an operator is not treated as predefined by this
153 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
154 -- If a default expression in entry call N depends on the discriminants
155 -- of the task, it must be replaced with a reference to the discriminant
156 -- of the task being called.
158 procedure Resolve_Op_Concat_Arg
163 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
164 -- concatenation operator. The operand is either of the array type or of
165 -- the component type. If the operand is an aggregate, and the component
166 -- type is composite, this is ambiguous if component type has aggregates.
168 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
169 -- Does the first part of the work of Resolve_Op_Concat
171 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
172 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
173 -- has been resolved. See Resolve_Op_Concat for details.
175 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_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_If_Expression (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_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 -- Versions with check(s) suppressed
327 procedure Analyze_And_Resolve
332 Scop : constant Entity_Id := Current_Scope;
335 if Suppress = All_Checks then
337 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
339 Scope_Suppress.Suppress := (others => True);
340 Analyze_And_Resolve (N, Typ);
341 Scope_Suppress.Suppress := Sva;
346 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
348 Scope_Suppress.Suppress (Suppress) := True;
349 Analyze_And_Resolve (N, Typ);
350 Scope_Suppress.Suppress (Suppress) := Svg;
354 if Current_Scope /= Scop
355 and then Scope_Is_Transient
357 -- This can only happen if a transient scope was created for an inner
358 -- expression, which will be removed upon completion of the analysis
359 -- of an enclosing construct. The transient scope must have the
360 -- suppress status of the enclosing environment, not of this Analyze
363 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
366 end Analyze_And_Resolve;
368 procedure Analyze_And_Resolve
372 Scop : constant Entity_Id := Current_Scope;
375 if Suppress = All_Checks then
377 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
379 Scope_Suppress.Suppress := (others => True);
380 Analyze_And_Resolve (N);
381 Scope_Suppress.Suppress := Sva;
386 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
388 Scope_Suppress.Suppress (Suppress) := True;
389 Analyze_And_Resolve (N);
390 Scope_Suppress.Suppress (Suppress) := Svg;
394 if Current_Scope /= Scop and then Scope_Is_Transient then
395 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
398 end Analyze_And_Resolve;
400 ----------------------------------------
401 -- Bad_Unordered_Enumeration_Reference --
402 ----------------------------------------
404 function Bad_Unordered_Enumeration_Reference
406 T : Entity_Id) return Boolean
409 return Is_Enumeration_Type (T)
410 and then Comes_From_Source (N)
411 and then Warn_On_Unordered_Enumeration_Type
412 and then not Has_Pragma_Ordered (T)
413 and then not In_Same_Extended_Unit (N, T);
414 end Bad_Unordered_Enumeration_Reference;
416 ----------------------------
417 -- Check_Discriminant_Use --
418 ----------------------------
420 procedure Check_Discriminant_Use (N : Node_Id) is
421 PN : constant Node_Id := Parent (N);
422 Disc : constant Entity_Id := Entity (N);
427 -- Any use in a spec-expression is legal
429 if In_Spec_Expression then
432 elsif Nkind (PN) = N_Range then
434 -- Discriminant cannot be used to constrain a scalar type
438 if Nkind (P) = N_Range_Constraint
439 and then Nkind (Parent (P)) = N_Subtype_Indication
440 and then Nkind (Parent (Parent (P))) = N_Component_Definition
442 Error_Msg_N ("discriminant cannot constrain scalar type", N);
444 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
446 -- The following check catches the unusual case where a
447 -- discriminant appears within an index constraint that is part of
448 -- a larger expression within a constraint on a component, e.g. "C
449 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
450 -- of record components, and note that a similar check should also
451 -- apply in the case of discriminant constraints below. ???
453 -- Note that the check for N_Subtype_Declaration below is to
454 -- detect the valid use of discriminants in the constraints of a
455 -- subtype declaration when this subtype declaration appears
456 -- inside the scope of a record type (which is syntactically
457 -- illegal, but which may be created as part of derived type
458 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
461 if Ekind (Current_Scope) = E_Record_Type
462 and then Scope (Disc) = Current_Scope
464 (Nkind (Parent (P)) = N_Subtype_Indication
466 Nkind_In (Parent (Parent (P)), N_Component_Definition,
467 N_Subtype_Declaration)
468 and then Paren_Count (N) = 0)
471 ("discriminant must appear alone in component constraint", N);
475 -- Detect a common error:
477 -- type R (D : Positive := 100) is record
478 -- Name : String (1 .. D);
481 -- The default value causes an object of type R to be allocated
482 -- with room for Positive'Last characters. The RM does not mandate
483 -- the allocation of the maximum size, but that is what GNAT does
484 -- so we should warn the programmer that there is a problem.
486 Check_Large : declare
492 function Large_Storage_Type (T : Entity_Id) return Boolean;
493 -- Return True if type T has a large enough range that any
494 -- array whose index type covered the whole range of the type
495 -- would likely raise Storage_Error.
497 ------------------------
498 -- Large_Storage_Type --
499 ------------------------
501 function Large_Storage_Type (T : Entity_Id) return Boolean is
503 -- The type is considered large if its bounds are known at
504 -- compile time and if it requires at least as many bits as
505 -- a Positive to store the possible values.
507 return Compile_Time_Known_Value (Type_Low_Bound (T))
508 and then Compile_Time_Known_Value (Type_High_Bound (T))
510 Minimum_Size (T, Biased => True) >=
511 RM_Size (Standard_Positive);
512 end Large_Storage_Type;
514 -- Start of processing for Check_Large
517 -- Check that the Disc has a large range
519 if not Large_Storage_Type (Etype (Disc)) then
523 -- If the enclosing type is limited, we allocate only the
524 -- default value, not the maximum, and there is no need for
527 if Is_Limited_Type (Scope (Disc)) then
531 -- Check that it is the high bound
533 if N /= High_Bound (PN)
534 or else No (Discriminant_Default_Value (Disc))
539 -- Check the array allows a large range at this bound. First
544 if Nkind (SI) /= N_Subtype_Indication then
548 T := Entity (Subtype_Mark (SI));
550 if not Is_Array_Type (T) then
554 -- Next, find the dimension
556 TB := First_Index (T);
557 CB := First (Constraints (P));
559 and then Present (TB)
560 and then Present (CB)
571 -- Now, check the dimension has a large range
573 if not Large_Storage_Type (Etype (TB)) then
577 -- Warn about the danger
580 ("??creation of & object may raise Storage_Error!",
589 -- Legal case is in index or discriminant constraint
591 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
592 N_Discriminant_Association)
594 if Paren_Count (N) > 0 then
596 ("discriminant in constraint must appear alone", N);
598 elsif Nkind (N) = N_Expanded_Name
599 and then Comes_From_Source (N)
602 ("discriminant must appear alone as a direct name", N);
607 -- Otherwise, context is an expression. It should not be within (i.e. a
608 -- subexpression of) a constraint for a component.
613 while not Nkind_In (P, N_Component_Declaration,
614 N_Subtype_Indication,
622 -- If the discriminant is used in an expression that is a bound of a
623 -- scalar type, an Itype is created and the bounds are attached to
624 -- its range, not to the original subtype indication. Such use is of
625 -- course a double fault.
627 if (Nkind (P) = N_Subtype_Indication
628 and then Nkind_In (Parent (P), N_Component_Definition,
629 N_Derived_Type_Definition)
630 and then D = Constraint (P))
632 -- The constraint itself may be given by a subtype indication,
633 -- rather than by a more common discrete range.
635 or else (Nkind (P) = N_Subtype_Indication
637 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
638 or else Nkind (P) = N_Entry_Declaration
639 or else Nkind (D) = N_Defining_Identifier
642 ("discriminant in constraint must appear alone", N);
645 end Check_Discriminant_Use;
647 --------------------------------
648 -- Check_For_Visible_Operator --
649 --------------------------------
651 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
653 if Is_Invisible_Operator (N, T) then
654 Error_Msg_NE -- CODEFIX
655 ("operator for} is not directly visible!", N, First_Subtype (T));
656 Error_Msg_N -- CODEFIX
657 ("use clause would make operation legal!", N);
659 end Check_For_Visible_Operator;
661 ----------------------------------
662 -- Check_Fully_Declared_Prefix --
663 ----------------------------------
665 procedure Check_Fully_Declared_Prefix
670 -- Check that the designated type of the prefix of a dereference is
671 -- not an incomplete type. This cannot be done unconditionally, because
672 -- dereferences of private types are legal in default expressions. This
673 -- case is taken care of in Check_Fully_Declared, called below. There
674 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
676 -- This consideration also applies to similar checks for allocators,
677 -- qualified expressions, and type conversions.
679 -- An additional exception concerns other per-object expressions that
680 -- are not directly related to component declarations, in particular
681 -- representation pragmas for tasks. These will be per-object
682 -- expressions if they depend on discriminants or some global entity.
683 -- If the task has access discriminants, the designated type may be
684 -- incomplete at the point the expression is resolved. This resolution
685 -- takes place within the body of the initialization procedure, where
686 -- the discriminant is replaced by its discriminal.
688 if Is_Entity_Name (Pref)
689 and then Ekind (Entity (Pref)) = E_In_Parameter
693 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
694 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
695 -- Analyze_Object_Renaming, and Freeze_Entity.
697 elsif Ada_Version >= Ada_2005
698 and then Is_Entity_Name (Pref)
699 and then Is_Access_Type (Etype (Pref))
700 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
702 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
706 Check_Fully_Declared (Typ, Parent (Pref));
708 end Check_Fully_Declared_Prefix;
710 ------------------------------
711 -- Check_Infinite_Recursion --
712 ------------------------------
714 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
718 function Same_Argument_List return Boolean;
719 -- Check whether list of actuals is identical to list of formals of
720 -- called function (which is also the enclosing scope).
722 ------------------------
723 -- Same_Argument_List --
724 ------------------------
726 function Same_Argument_List return Boolean is
732 if not Is_Entity_Name (Name (N)) then
735 Subp := Entity (Name (N));
738 F := First_Formal (Subp);
739 A := First_Actual (N);
740 while Present (F) and then Present (A) loop
741 if not Is_Entity_Name (A)
742 or else Entity (A) /= F
752 end Same_Argument_List;
754 -- Start of processing for Check_Infinite_Recursion
757 -- Special case, if this is a procedure call and is a call to the
758 -- current procedure with the same argument list, then this is for
759 -- sure an infinite recursion and we insert a call to raise SE.
761 if Is_List_Member (N)
762 and then List_Length (List_Containing (N)) = 1
763 and then Same_Argument_List
766 P : constant Node_Id := Parent (N);
768 if Nkind (P) = N_Handled_Sequence_Of_Statements
769 and then Nkind (Parent (P)) = N_Subprogram_Body
770 and then Is_Empty_List (Declarations (Parent (P)))
772 Error_Msg_N ("!??infinite recursion", N);
773 Error_Msg_N ("\!??Storage_Error will be raised at run time", N);
775 Make_Raise_Storage_Error (Sloc (N),
776 Reason => SE_Infinite_Recursion));
782 -- If not that special case, search up tree, quitting if we reach a
783 -- construct (e.g. a conditional) that tells us that this is not a
784 -- case for an infinite recursion warning.
790 -- If no parent, then we were not inside a subprogram, this can for
791 -- example happen when processing certain pragmas in a spec. Just
792 -- return False in this case.
798 -- Done if we get to subprogram body, this is definitely an infinite
799 -- recursion case if we did not find anything to stop us.
801 exit when Nkind (P) = N_Subprogram_Body;
803 -- If appearing in conditional, result is false
805 if Nkind_In (P, N_Or_Else,
814 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
815 and then C /= First (Statements (P))
817 -- If the call is the expression of a return statement and the
818 -- actuals are identical to the formals, it's worth a warning.
819 -- However, we skip this if there is an immediately preceding
820 -- raise statement, since the call is never executed.
822 -- Furthermore, this corresponds to a common idiom:
824 -- function F (L : Thing) return Boolean is
826 -- raise Program_Error;
830 -- for generating a stub function
832 if Nkind (Parent (N)) = N_Simple_Return_Statement
833 and then Same_Argument_List
835 exit when not Is_List_Member (Parent (N));
837 -- OK, return statement is in a statement list, look for raise
843 -- Skip past N_Freeze_Entity nodes generated by expansion
845 Nod := Prev (Parent (N));
847 and then Nkind (Nod) = N_Freeze_Entity
852 -- If no raise statement, give warning. We look at the
853 -- original node, because in the case of "raise ... with
854 -- ...", the node has been transformed into a call.
856 exit when Nkind (Original_Node (Nod)) /= N_Raise_Statement
858 (Nkind (Nod) not in N_Raise_xxx_Error
859 or else Present (Condition (Nod)));
870 Error_Msg_N ("!??possible infinite recursion", N);
871 Error_Msg_N ("\!??Storage_Error may be raised at run time", N);
874 end Check_Infinite_Recursion;
876 -------------------------------
877 -- Check_Initialization_Call --
878 -------------------------------
880 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
881 Typ : constant Entity_Id := Etype (First_Formal (Nam));
883 function Uses_SS (T : Entity_Id) return Boolean;
884 -- Check whether the creation of an object of the type will involve
885 -- use of the secondary stack. If T is a record type, this is true
886 -- if the expression for some component uses the secondary stack, e.g.
887 -- through a call to a function that returns an unconstrained value.
888 -- False if T is controlled, because cleanups occur elsewhere.
894 function Uses_SS (T : Entity_Id) return Boolean is
897 Full_Type : Entity_Id := Underlying_Type (T);
900 -- Normally we want to use the underlying type, but if it's not set
901 -- then continue with T.
903 if not Present (Full_Type) then
907 if Is_Controlled (Full_Type) then
910 elsif Is_Array_Type (Full_Type) then
911 return Uses_SS (Component_Type (Full_Type));
913 elsif Is_Record_Type (Full_Type) then
914 Comp := First_Component (Full_Type);
915 while Present (Comp) loop
916 if Ekind (Comp) = E_Component
917 and then Nkind (Parent (Comp)) = N_Component_Declaration
919 -- The expression for a dynamic component may be rewritten
920 -- as a dereference, so retrieve original node.
922 Expr := Original_Node (Expression (Parent (Comp)));
924 -- Return True if the expression is a call to a function
925 -- (including an attribute function such as Image, or a
926 -- user-defined operator) with a result that requires a
929 if (Nkind (Expr) = N_Function_Call
930 or else Nkind (Expr) in N_Op
931 or else (Nkind (Expr) = N_Attribute_Reference
932 and then Present (Expressions (Expr))))
933 and then Requires_Transient_Scope (Etype (Expr))
937 elsif Uses_SS (Etype (Comp)) then
942 Next_Component (Comp);
952 -- Start of processing for Check_Initialization_Call
955 -- Establish a transient scope if the type needs it
957 if Uses_SS (Typ) then
958 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
960 end Check_Initialization_Call;
962 ---------------------------------------
963 -- Check_No_Direct_Boolean_Operators --
964 ---------------------------------------
966 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
968 if Scope (Entity (N)) = Standard_Standard
969 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
971 -- Restriction only applies to original source code
973 if Comes_From_Source (N) then
974 Check_Restriction (No_Direct_Boolean_Operators, N);
979 Check_Boolean_Operator (N);
981 end Check_No_Direct_Boolean_Operators;
983 ------------------------------
984 -- Check_Parameterless_Call --
985 ------------------------------
987 procedure Check_Parameterless_Call (N : Node_Id) is
990 function Prefix_Is_Access_Subp return Boolean;
991 -- If the prefix is of an access_to_subprogram type, the node must be
992 -- rewritten as a call. Ditto if the prefix is overloaded and all its
993 -- interpretations are access to subprograms.
995 ---------------------------
996 -- Prefix_Is_Access_Subp --
997 ---------------------------
999 function Prefix_Is_Access_Subp return Boolean is
1004 -- If the context is an attribute reference that can apply to
1005 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1007 if Nkind (Parent (N)) = N_Attribute_Reference
1008 and then (Attribute_Name (Parent (N)) = Name_Address or else
1009 Attribute_Name (Parent (N)) = Name_Code_Address or else
1010 Attribute_Name (Parent (N)) = Name_Access)
1015 if not Is_Overloaded (N) then
1017 Ekind (Etype (N)) = E_Subprogram_Type
1018 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1020 Get_First_Interp (N, I, It);
1021 while Present (It.Typ) loop
1022 if Ekind (It.Typ) /= E_Subprogram_Type
1023 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1028 Get_Next_Interp (I, It);
1033 end Prefix_Is_Access_Subp;
1035 -- Start of processing for Check_Parameterless_Call
1038 -- Defend against junk stuff if errors already detected
1040 if Total_Errors_Detected /= 0 then
1041 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1043 elsif Nkind (N) in N_Has_Chars
1044 and then Chars (N) in Error_Name_Or_No_Name
1052 -- If the context expects a value, and the name is a procedure, this is
1053 -- most likely a missing 'Access. Don't try to resolve the parameterless
1054 -- call, error will be caught when the outer call is analyzed.
1056 if Is_Entity_Name (N)
1057 and then Ekind (Entity (N)) = E_Procedure
1058 and then not Is_Overloaded (N)
1060 Nkind_In (Parent (N), N_Parameter_Association,
1062 N_Procedure_Call_Statement)
1067 -- Rewrite as call if overloadable entity that is (or could be, in the
1068 -- overloaded case) a function call. If we know for sure that the entity
1069 -- is an enumeration literal, we do not rewrite it.
1071 -- If the entity is the name of an operator, it cannot be a call because
1072 -- operators cannot have default parameters. In this case, this must be
1073 -- a string whose contents coincide with an operator name. Set the kind
1074 -- of the node appropriately.
1076 if (Is_Entity_Name (N)
1077 and then Nkind (N) /= N_Operator_Symbol
1078 and then Is_Overloadable (Entity (N))
1079 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1080 or else Is_Overloaded (N)))
1082 -- Rewrite as call if it is an explicit dereference of an expression of
1083 -- a subprogram access type, and the subprogram type is not that of a
1084 -- procedure or entry.
1087 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1089 -- Rewrite as call if it is a selected component which is a function,
1090 -- this is the case of a call to a protected function (which may be
1091 -- overloaded with other protected operations).
1094 (Nkind (N) = N_Selected_Component
1095 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1097 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1099 and then Is_Overloaded (Selector_Name (N)))))
1101 -- If one of the above three conditions is met, rewrite as call. Apply
1102 -- the rewriting only once.
1105 if Nkind (Parent (N)) /= N_Function_Call
1106 or else N /= Name (Parent (N))
1109 -- This may be a prefixed call that was not fully analyzed, e.g.
1110 -- an actual in an instance.
1112 if Ada_Version >= Ada_2005
1113 and then Nkind (N) = N_Selected_Component
1114 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1116 Analyze_Selected_Component (N);
1118 if Nkind (N) /= N_Selected_Component then
1123 Nam := New_Copy (N);
1125 -- If overloaded, overload set belongs to new copy
1127 Save_Interps (N, Nam);
1129 -- Change node to parameterless function call (note that the
1130 -- Parameter_Associations associations field is left set to Empty,
1131 -- its normal default value since there are no parameters)
1133 Change_Node (N, N_Function_Call);
1135 Set_Sloc (N, Sloc (Nam));
1139 elsif Nkind (N) = N_Parameter_Association then
1140 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1142 elsif Nkind (N) = N_Operator_Symbol then
1143 Change_Operator_Symbol_To_String_Literal (N);
1144 Set_Is_Overloaded (N, False);
1145 Set_Etype (N, Any_String);
1147 end Check_Parameterless_Call;
1149 -----------------------------
1150 -- Is_Definite_Access_Type --
1151 -----------------------------
1153 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1154 Btyp : constant Entity_Id := Base_Type (E);
1156 return Ekind (Btyp) = E_Access_Type
1157 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1158 and then Comes_From_Source (Btyp));
1159 end Is_Definite_Access_Type;
1161 ----------------------
1162 -- Is_Predefined_Op --
1163 ----------------------
1165 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1167 -- Predefined operators are intrinsic subprograms
1169 if not Is_Intrinsic_Subprogram (Nam) then
1173 -- A call to a back-end builtin is never a predefined operator
1175 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1179 return not Is_Generic_Instance (Nam)
1180 and then Chars (Nam) in Any_Operator_Name
1181 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1182 end Is_Predefined_Op;
1184 -----------------------------
1185 -- Make_Call_Into_Operator --
1186 -----------------------------
1188 procedure Make_Call_Into_Operator
1193 Op_Name : constant Name_Id := Chars (Op_Id);
1194 Act1 : Node_Id := First_Actual (N);
1195 Act2 : Node_Id := Next_Actual (Act1);
1196 Error : Boolean := False;
1197 Func : constant Entity_Id := Entity (Name (N));
1198 Is_Binary : constant Boolean := Present (Act2);
1200 Opnd_Type : Entity_Id;
1201 Orig_Type : Entity_Id := Empty;
1204 type Kind_Test is access function (E : Entity_Id) return Boolean;
1206 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1207 -- If the operand is not universal, and the operator is given by an
1208 -- expanded name, verify that the operand has an interpretation with a
1209 -- type defined in the given scope of the operator.
1211 function Type_In_P (Test : Kind_Test) return Entity_Id;
1212 -- Find a type of the given class in package Pack that contains the
1215 ---------------------------
1216 -- Operand_Type_In_Scope --
1217 ---------------------------
1219 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1220 Nod : constant Node_Id := Right_Opnd (Op_Node);
1225 if not Is_Overloaded (Nod) then
1226 return Scope (Base_Type (Etype (Nod))) = S;
1229 Get_First_Interp (Nod, I, It);
1230 while Present (It.Typ) loop
1231 if Scope (Base_Type (It.Typ)) = S then
1235 Get_Next_Interp (I, It);
1240 end Operand_Type_In_Scope;
1246 function Type_In_P (Test : Kind_Test) return Entity_Id is
1249 function In_Decl return Boolean;
1250 -- Verify that node is not part of the type declaration for the
1251 -- candidate type, which would otherwise be invisible.
1257 function In_Decl return Boolean is
1258 Decl_Node : constant Node_Id := Parent (E);
1264 if Etype (E) = Any_Type then
1267 elsif No (Decl_Node) then
1272 and then Nkind (N2) /= N_Compilation_Unit
1274 if N2 = Decl_Node then
1285 -- Start of processing for Type_In_P
1288 -- If the context type is declared in the prefix package, this is the
1289 -- desired base type.
1291 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1292 return Base_Type (Typ);
1295 E := First_Entity (Pack);
1296 while Present (E) loop
1298 and then not In_Decl
1310 -- Start of processing for Make_Call_Into_Operator
1313 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1318 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1319 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1320 Save_Interps (Act1, Left_Opnd (Op_Node));
1321 Save_Interps (Act2, Right_Opnd (Op_Node));
1322 Act1 := Left_Opnd (Op_Node);
1323 Act2 := Right_Opnd (Op_Node);
1328 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1329 Save_Interps (Act1, Right_Opnd (Op_Node));
1330 Act1 := Right_Opnd (Op_Node);
1333 -- If the operator is denoted by an expanded name, and the prefix is
1334 -- not Standard, but the operator is a predefined one whose scope is
1335 -- Standard, then this is an implicit_operator, inserted as an
1336 -- interpretation by the procedure of the same name. This procedure
1337 -- overestimates the presence of implicit operators, because it does
1338 -- not examine the type of the operands. Verify now that the operand
1339 -- type appears in the given scope. If right operand is universal,
1340 -- check the other operand. In the case of concatenation, either
1341 -- argument can be the component type, so check the type of the result.
1342 -- If both arguments are literals, look for a type of the right kind
1343 -- defined in the given scope. This elaborate nonsense is brought to
1344 -- you courtesy of b33302a. The type itself must be frozen, so we must
1345 -- find the type of the proper class in the given scope.
1347 -- A final wrinkle is the multiplication operator for fixed point types,
1348 -- which is defined in Standard only, and not in the scope of the
1349 -- fixed point type itself.
1351 if Nkind (Name (N)) = N_Expanded_Name then
1352 Pack := Entity (Prefix (Name (N)));
1354 -- If this is a package renaming, get renamed entity, which will be
1355 -- the scope of the operands if operaton is type-correct.
1357 if Present (Renamed_Entity (Pack)) then
1358 Pack := Renamed_Entity (Pack);
1361 -- If the entity being called is defined in the given package, it is
1362 -- a renaming of a predefined operator, and known to be legal.
1364 if Scope (Entity (Name (N))) = Pack
1365 and then Pack /= Standard_Standard
1369 -- Visibility does not need to be checked in an instance: if the
1370 -- operator was not visible in the generic it has been diagnosed
1371 -- already, else there is an implicit copy of it in the instance.
1373 elsif In_Instance then
1376 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1377 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1378 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1380 if Pack /= Standard_Standard then
1384 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1387 elsif Ada_Version >= Ada_2005
1388 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1389 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1394 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1396 if Op_Name = Name_Op_Concat then
1397 Opnd_Type := Base_Type (Typ);
1399 elsif (Scope (Opnd_Type) = Standard_Standard
1401 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1403 and then not Comes_From_Source (Opnd_Type))
1405 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1408 if Scope (Opnd_Type) = Standard_Standard then
1410 -- Verify that the scope contains a type that corresponds to
1411 -- the given literal. Optimize the case where Pack is Standard.
1413 if Pack /= Standard_Standard then
1415 if Opnd_Type = Universal_Integer then
1416 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1418 elsif Opnd_Type = Universal_Real then
1419 Orig_Type := Type_In_P (Is_Real_Type'Access);
1421 elsif Opnd_Type = Any_String then
1422 Orig_Type := Type_In_P (Is_String_Type'Access);
1424 elsif Opnd_Type = Any_Access then
1425 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1427 elsif Opnd_Type = Any_Composite then
1428 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1430 if Present (Orig_Type) then
1431 if Has_Private_Component (Orig_Type) then
1434 Set_Etype (Act1, Orig_Type);
1437 Set_Etype (Act2, Orig_Type);
1446 Error := No (Orig_Type);
1449 elsif Ekind (Opnd_Type) = E_Allocator_Type
1450 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1454 -- If the type is defined elsewhere, and the operator is not
1455 -- defined in the given scope (by a renaming declaration, e.g.)
1456 -- then this is an error as well. If an extension of System is
1457 -- present, and the type may be defined there, Pack must be
1460 elsif Scope (Opnd_Type) /= Pack
1461 and then Scope (Op_Id) /= Pack
1462 and then (No (System_Aux_Id)
1463 or else Scope (Opnd_Type) /= System_Aux_Id
1464 or else Pack /= Scope (System_Aux_Id))
1466 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1469 Error := not Operand_Type_In_Scope (Pack);
1472 elsif Pack = Standard_Standard
1473 and then not Operand_Type_In_Scope (Standard_Standard)
1480 Error_Msg_Node_2 := Pack;
1482 ("& not declared in&", N, Selector_Name (Name (N)));
1483 Set_Etype (N, Any_Type);
1486 -- Detect a mismatch between the context type and the result type
1487 -- in the named package, which is otherwise not detected if the
1488 -- operands are universal. Check is only needed if source entity is
1489 -- an operator, not a function that renames an operator.
1491 elsif Nkind (Parent (N)) /= N_Type_Conversion
1492 and then Ekind (Entity (Name (N))) = E_Operator
1493 and then Is_Numeric_Type (Typ)
1494 and then not Is_Universal_Numeric_Type (Typ)
1495 and then Scope (Base_Type (Typ)) /= Pack
1496 and then not In_Instance
1498 if Is_Fixed_Point_Type (Typ)
1499 and then (Op_Name = Name_Op_Multiply
1501 Op_Name = Name_Op_Divide)
1503 -- Already checked above
1507 -- Operator may be defined in an extension of System
1509 elsif Present (System_Aux_Id)
1510 and then Scope (Opnd_Type) = System_Aux_Id
1515 -- Could we use Wrong_Type here??? (this would require setting
1516 -- Etype (N) to the actual type found where Typ was expected).
1518 Error_Msg_NE ("expect }", N, Typ);
1523 Set_Chars (Op_Node, Op_Name);
1525 if not Is_Private_Type (Etype (N)) then
1526 Set_Etype (Op_Node, Base_Type (Etype (N)));
1528 Set_Etype (Op_Node, Etype (N));
1531 -- If this is a call to a function that renames a predefined equality,
1532 -- the renaming declaration provides a type that must be used to
1533 -- resolve the operands. This must be done now because resolution of
1534 -- the equality node will not resolve any remaining ambiguity, and it
1535 -- assumes that the first operand is not overloaded.
1537 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1538 and then Ekind (Func) = E_Function
1539 and then Is_Overloaded (Act1)
1541 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1542 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1545 Set_Entity (Op_Node, Op_Id);
1546 Generate_Reference (Op_Id, N, ' ');
1548 -- Do rewrite setting Comes_From_Source on the result if the original
1549 -- call came from source. Although it is not strictly the case that the
1550 -- operator as such comes from the source, logically it corresponds
1551 -- exactly to the function call in the source, so it should be marked
1552 -- this way (e.g. to make sure that validity checks work fine).
1555 CS : constant Boolean := Comes_From_Source (N);
1557 Rewrite (N, Op_Node);
1558 Set_Comes_From_Source (N, CS);
1561 -- If this is an arithmetic operator and the result type is private,
1562 -- the operands and the result must be wrapped in conversion to
1563 -- expose the underlying numeric type and expand the proper checks,
1564 -- e.g. on division.
1566 if Is_Private_Type (Typ) then
1568 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1569 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1570 Resolve_Intrinsic_Operator (N, Typ);
1572 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1573 Resolve_Intrinsic_Unary_Operator (N, Typ);
1581 end Make_Call_Into_Operator;
1587 function Operator_Kind
1589 Is_Binary : Boolean) return Node_Kind
1594 -- Use CASE statement or array???
1597 if Op_Name = Name_Op_And then
1599 elsif Op_Name = Name_Op_Or then
1601 elsif Op_Name = Name_Op_Xor then
1603 elsif Op_Name = Name_Op_Eq then
1605 elsif Op_Name = Name_Op_Ne then
1607 elsif Op_Name = Name_Op_Lt then
1609 elsif Op_Name = Name_Op_Le then
1611 elsif Op_Name = Name_Op_Gt then
1613 elsif Op_Name = Name_Op_Ge then
1615 elsif Op_Name = Name_Op_Add then
1617 elsif Op_Name = Name_Op_Subtract then
1618 Kind := N_Op_Subtract;
1619 elsif Op_Name = Name_Op_Concat then
1620 Kind := N_Op_Concat;
1621 elsif Op_Name = Name_Op_Multiply then
1622 Kind := N_Op_Multiply;
1623 elsif Op_Name = Name_Op_Divide then
1624 Kind := N_Op_Divide;
1625 elsif Op_Name = Name_Op_Mod then
1627 elsif Op_Name = Name_Op_Rem then
1629 elsif Op_Name = Name_Op_Expon then
1632 raise Program_Error;
1638 if Op_Name = Name_Op_Add then
1640 elsif Op_Name = Name_Op_Subtract then
1642 elsif Op_Name = Name_Op_Abs then
1644 elsif Op_Name = Name_Op_Not then
1647 raise Program_Error;
1654 ----------------------------
1655 -- Preanalyze_And_Resolve --
1656 ----------------------------
1658 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1659 Save_Full_Analysis : constant Boolean := Full_Analysis;
1662 Full_Analysis := False;
1663 Expander_Mode_Save_And_Set (False);
1665 -- Normally, we suppress all checks for this preanalysis. There is no
1666 -- point in processing them now, since they will be applied properly
1667 -- and in the proper location when the default expressions reanalyzed
1668 -- and reexpanded later on. We will also have more information at that
1669 -- point for possible suppression of individual checks.
1671 -- However, in Alfa mode, most expansion is suppressed, and this
1672 -- later reanalysis and reexpansion may not occur. Alfa mode does
1673 -- require the setting of checking flags for proof purposes, so we
1674 -- do the Alfa preanalysis without suppressing checks.
1676 -- This special handling for Alfa mode is required for example in the
1677 -- case of Ada 2012 constructs such as quantified expressions, which are
1678 -- expanded in two separate steps.
1681 Analyze_And_Resolve (N, T);
1683 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1686 Expander_Mode_Restore;
1687 Full_Analysis := Save_Full_Analysis;
1688 end Preanalyze_And_Resolve;
1690 -- Version without context type
1692 procedure Preanalyze_And_Resolve (N : Node_Id) is
1693 Save_Full_Analysis : constant Boolean := Full_Analysis;
1696 Full_Analysis := False;
1697 Expander_Mode_Save_And_Set (False);
1700 Resolve (N, Etype (N), Suppress => All_Checks);
1702 Expander_Mode_Restore;
1703 Full_Analysis := Save_Full_Analysis;
1704 end Preanalyze_And_Resolve;
1706 ----------------------------------
1707 -- Replace_Actual_Discriminants --
1708 ----------------------------------
1710 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1711 Loc : constant Source_Ptr := Sloc (N);
1712 Tsk : Node_Id := Empty;
1714 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1715 -- Comment needed???
1721 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1725 if Nkind (Nod) = N_Identifier then
1726 Ent := Entity (Nod);
1729 and then Ekind (Ent) = E_Discriminant
1732 Make_Selected_Component (Loc,
1733 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1734 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1736 Set_Etype (Nod, Etype (Ent));
1744 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1746 -- Start of processing for Replace_Actual_Discriminants
1749 if not Full_Expander_Active then
1753 if Nkind (Name (N)) = N_Selected_Component then
1754 Tsk := Prefix (Name (N));
1756 elsif Nkind (Name (N)) = N_Indexed_Component then
1757 Tsk := Prefix (Prefix (Name (N)));
1763 Replace_Discrs (Default);
1765 end Replace_Actual_Discriminants;
1771 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1772 Ambiguous : Boolean := False;
1773 Ctx_Type : Entity_Id := Typ;
1774 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1775 Err_Type : Entity_Id := Empty;
1776 Found : Boolean := False;
1779 I1 : Interp_Index := 0; -- prevent junk warning
1782 Seen : Entity_Id := Empty; -- prevent junk warning
1784 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1785 -- Determine whether a node comes from a predefined library unit or
1788 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1789 -- Try and fix up a literal so that it matches its expected type. New
1790 -- literals are manufactured if necessary to avoid cascaded errors.
1792 function Proper_Current_Scope return Entity_Id;
1793 -- Return the current scope. Skip loop scopes created for the purpose of
1794 -- quantified expression analysis since those do not appear in the tree.
1796 procedure Report_Ambiguous_Argument;
1797 -- Additional diagnostics when an ambiguous call has an ambiguous
1798 -- argument (typically a controlling actual).
1800 procedure Resolution_Failed;
1801 -- Called when attempt at resolving current expression fails
1803 ------------------------------------
1804 -- Comes_From_Predefined_Lib_Unit --
1805 -------------------------------------
1807 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1810 Sloc (Nod) = Standard_Location
1811 or else Is_Predefined_File_Name
1812 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1813 end Comes_From_Predefined_Lib_Unit;
1815 --------------------
1816 -- Patch_Up_Value --
1817 --------------------
1819 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1821 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1823 Make_Real_Literal (Sloc (N),
1824 Realval => UR_From_Uint (Intval (N))));
1825 Set_Etype (N, Universal_Real);
1826 Set_Is_Static_Expression (N);
1828 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1830 Make_Integer_Literal (Sloc (N),
1831 Intval => UR_To_Uint (Realval (N))));
1832 Set_Etype (N, Universal_Integer);
1833 Set_Is_Static_Expression (N);
1835 elsif Nkind (N) = N_String_Literal
1836 and then Is_Character_Type (Typ)
1838 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1840 Make_Character_Literal (Sloc (N),
1842 Char_Literal_Value =>
1843 UI_From_Int (Character'Pos ('A'))));
1844 Set_Etype (N, Any_Character);
1845 Set_Is_Static_Expression (N);
1847 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1849 Make_String_Literal (Sloc (N),
1850 Strval => End_String));
1852 elsif Nkind (N) = N_Range then
1853 Patch_Up_Value (Low_Bound (N), Typ);
1854 Patch_Up_Value (High_Bound (N), Typ);
1858 --------------------------
1859 -- Proper_Current_Scope --
1860 --------------------------
1862 function Proper_Current_Scope return Entity_Id is
1863 S : Entity_Id := Current_Scope;
1866 while Present (S) loop
1868 -- Skip a loop scope created for quantified expression analysis
1870 if Ekind (S) = E_Loop
1871 and then Nkind (Parent (S)) = N_Quantified_Expression
1880 end Proper_Current_Scope;
1882 -------------------------------
1883 -- Report_Ambiguous_Argument --
1884 -------------------------------
1886 procedure Report_Ambiguous_Argument is
1887 Arg : constant Node_Id := First (Parameter_Associations (N));
1892 if Nkind (Arg) = N_Function_Call
1893 and then Is_Entity_Name (Name (Arg))
1894 and then Is_Overloaded (Name (Arg))
1896 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1898 -- Could use comments on what is going on here???
1900 Get_First_Interp (Name (Arg), I, It);
1901 while Present (It.Nam) loop
1902 Error_Msg_Sloc := Sloc (It.Nam);
1904 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1905 Error_Msg_N ("interpretation (inherited) #!", Arg);
1907 Error_Msg_N ("interpretation #!", Arg);
1910 Get_Next_Interp (I, It);
1913 end Report_Ambiguous_Argument;
1915 -----------------------
1916 -- Resolution_Failed --
1917 -----------------------
1919 procedure Resolution_Failed is
1921 Patch_Up_Value (N, Typ);
1923 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1924 Set_Is_Overloaded (N, False);
1926 -- The caller will return without calling the expander, so we need
1927 -- to set the analyzed flag. Note that it is fine to set Analyzed
1928 -- to True even if we are in the middle of a shallow analysis,
1929 -- (see the spec of sem for more details) since this is an error
1930 -- situation anyway, and there is no point in repeating the
1931 -- analysis later (indeed it won't work to repeat it later, since
1932 -- we haven't got a clear resolution of which entity is being
1935 Set_Analyzed (N, True);
1937 end Resolution_Failed;
1939 -- Start of processing for Resolve
1946 -- Access attribute on remote subprogram cannot be used for a non-remote
1947 -- access-to-subprogram type.
1949 if Nkind (N) = N_Attribute_Reference
1950 and then (Attribute_Name (N) = Name_Access or else
1951 Attribute_Name (N) = Name_Unrestricted_Access or else
1952 Attribute_Name (N) = Name_Unchecked_Access)
1953 and then Comes_From_Source (N)
1954 and then Is_Entity_Name (Prefix (N))
1955 and then Is_Subprogram (Entity (Prefix (N)))
1956 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1957 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1960 ("prefix must statically denote a non-remote subprogram", N);
1963 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1965 -- If the context is a Remote_Access_To_Subprogram, access attributes
1966 -- must be resolved with the corresponding fat pointer. There is no need
1967 -- to check for the attribute name since the return type of an
1968 -- attribute is never a remote type.
1970 if Nkind (N) = N_Attribute_Reference
1971 and then Comes_From_Source (N)
1972 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1975 Attr : constant Attribute_Id :=
1976 Get_Attribute_Id (Attribute_Name (N));
1977 Pref : constant Node_Id := Prefix (N);
1980 Is_Remote : Boolean := True;
1983 -- Check that Typ is a remote access-to-subprogram type
1985 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1987 -- Prefix (N) must statically denote a remote subprogram
1988 -- declared in a package specification.
1990 if Attr = Attribute_Access or else
1991 Attr = Attribute_Unchecked_Access or else
1992 Attr = Attribute_Unrestricted_Access
1994 Decl := Unit_Declaration_Node (Entity (Pref));
1996 if Nkind (Decl) = N_Subprogram_Body then
1997 Spec := Corresponding_Spec (Decl);
1999 if not No (Spec) then
2000 Decl := Unit_Declaration_Node (Spec);
2004 Spec := Parent (Decl);
2006 if not Is_Entity_Name (Prefix (N))
2007 or else Nkind (Spec) /= N_Package_Specification
2009 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2013 ("prefix must statically denote a remote subprogram ",
2017 -- If we are generating code in distributed mode, perform
2018 -- semantic checks against corresponding remote entities.
2020 if Full_Expander_Active
2021 and then Get_PCS_Name /= Name_No_DSA
2023 Check_Subtype_Conformant
2024 (New_Id => Entity (Prefix (N)),
2025 Old_Id => Designated_Type
2026 (Corresponding_Remote_Type (Typ)),
2030 Process_Remote_AST_Attribute (N, Typ);
2038 Debug_A_Entry ("resolving ", N);
2040 if Debug_Flag_V then
2041 Write_Overloads (N);
2044 if Comes_From_Source (N) then
2045 if Is_Fixed_Point_Type (Typ) then
2046 Check_Restriction (No_Fixed_Point, N);
2048 elsif Is_Floating_Point_Type (Typ)
2049 and then Typ /= Universal_Real
2050 and then Typ /= Any_Real
2052 Check_Restriction (No_Floating_Point, N);
2056 -- Return if already analyzed
2058 if Analyzed (N) then
2059 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2060 Analyze_Dimension (N);
2063 -- A Raise_Expression takes its type from context. The Etype was set
2064 -- to Any_Type, reflecting the fact that the expression itself does
2065 -- not specify any possible interpretation. So we set the type to the
2066 -- resolution type here and now. We need to do this before Resolve sees
2067 -- the Any_Type value.
2069 elsif Nkind (N) = N_Raise_Expression then
2072 -- Any other case of Any_Type as the Etype value means that we had
2073 -- a previous error.
2075 elsif Etype (N) = Any_Type then
2076 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2080 Check_Parameterless_Call (N);
2082 -- If not overloaded, then we know the type, and all that needs doing
2083 -- is to check that this type is compatible with the context.
2085 if not Is_Overloaded (N) then
2086 Found := Covers (Typ, Etype (N));
2087 Expr_Type := Etype (N);
2089 -- In the overloaded case, we must select the interpretation that
2090 -- is compatible with the context (i.e. the type passed to Resolve)
2093 -- Loop through possible interpretations
2095 Get_First_Interp (N, I, It);
2096 Interp_Loop : while Present (It.Typ) loop
2098 if Debug_Flag_V then
2099 Write_Str ("Interp: ");
2103 -- We are only interested in interpretations that are compatible
2104 -- with the expected type, any other interpretations are ignored.
2106 if not Covers (Typ, It.Typ) then
2107 if Debug_Flag_V then
2108 Write_Str (" interpretation incompatible with context");
2113 -- Skip the current interpretation if it is disabled by an
2114 -- abstract operator. This action is performed only when the
2115 -- type against which we are resolving is the same as the
2116 -- type of the interpretation.
2118 if Ada_Version >= Ada_2005
2119 and then It.Typ = Typ
2120 and then Typ /= Universal_Integer
2121 and then Typ /= Universal_Real
2122 and then Present (It.Abstract_Op)
2124 if Debug_Flag_V then
2125 Write_Line ("Skip.");
2131 -- First matching interpretation
2137 Expr_Type := It.Typ;
2139 -- Matching interpretation that is not the first, maybe an
2140 -- error, but there are some cases where preference rules are
2141 -- used to choose between the two possibilities. These and
2142 -- some more obscure cases are handled in Disambiguate.
2145 -- If the current statement is part of a predefined library
2146 -- unit, then all interpretations which come from user level
2147 -- packages should not be considered.
2150 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2155 Error_Msg_Sloc := Sloc (Seen);
2156 It1 := Disambiguate (N, I1, I, Typ);
2158 -- Disambiguation has succeeded. Skip the remaining
2161 if It1 /= No_Interp then
2163 Expr_Type := It1.Typ;
2165 while Present (It.Typ) loop
2166 Get_Next_Interp (I, It);
2170 -- Before we issue an ambiguity complaint, check for
2171 -- the case of a subprogram call where at least one
2172 -- of the arguments is Any_Type, and if so, suppress
2173 -- the message, since it is a cascaded error.
2175 if Nkind (N) in N_Subprogram_Call then
2181 A := First_Actual (N);
2182 while Present (A) loop
2185 if Nkind (E) = N_Parameter_Association then
2186 E := Explicit_Actual_Parameter (E);
2189 if Etype (E) = Any_Type then
2190 if Debug_Flag_V then
2191 Write_Str ("Any_Type in call");
2202 elsif Nkind (N) in N_Binary_Op
2203 and then (Etype (Left_Opnd (N)) = Any_Type
2204 or else Etype (Right_Opnd (N)) = Any_Type)
2208 elsif Nkind (N) in N_Unary_Op
2209 and then Etype (Right_Opnd (N)) = Any_Type
2214 -- Not that special case, so issue message using the
2215 -- flag Ambiguous to control printing of the header
2216 -- message only at the start of an ambiguous set.
2218 if not Ambiguous then
2219 if Nkind (N) = N_Function_Call
2220 and then Nkind (Name (N)) = N_Explicit_Dereference
2223 ("ambiguous expression "
2224 & "(cannot resolve indirect call)!", N);
2226 Error_Msg_NE -- CODEFIX
2227 ("ambiguous expression (cannot resolve&)!",
2233 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2235 ("\\possible interpretation (inherited)#!", N);
2237 Error_Msg_N -- CODEFIX
2238 ("\\possible interpretation#!", N);
2241 if Nkind (N) in N_Subprogram_Call
2242 and then Present (Parameter_Associations (N))
2244 Report_Ambiguous_Argument;
2248 Error_Msg_Sloc := Sloc (It.Nam);
2250 -- By default, the error message refers to the candidate
2251 -- interpretation. But if it is a predefined operator, it
2252 -- is implicitly declared at the declaration of the type
2253 -- of the operand. Recover the sloc of that declaration
2254 -- for the error message.
2256 if Nkind (N) in N_Op
2257 and then Scope (It.Nam) = Standard_Standard
2258 and then not Is_Overloaded (Right_Opnd (N))
2259 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2262 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2264 if Comes_From_Source (Err_Type)
2265 and then Present (Parent (Err_Type))
2267 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2270 elsif Nkind (N) in N_Binary_Op
2271 and then Scope (It.Nam) = Standard_Standard
2272 and then not Is_Overloaded (Left_Opnd (N))
2273 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2276 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2278 if Comes_From_Source (Err_Type)
2279 and then Present (Parent (Err_Type))
2281 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2284 -- If this is an indirect call, use the subprogram_type
2285 -- in the message, to have a meaningful location. Also
2286 -- indicate if this is an inherited operation, created
2287 -- by a type declaration.
2289 elsif Nkind (N) = N_Function_Call
2290 and then Nkind (Name (N)) = N_Explicit_Dereference
2291 and then Is_Type (It.Nam)
2295 Sloc (Associated_Node_For_Itype (Err_Type));
2300 if Nkind (N) in N_Op
2301 and then Scope (It.Nam) = Standard_Standard
2302 and then Present (Err_Type)
2304 -- Special-case the message for universal_fixed
2305 -- operators, which are not declared with the type
2306 -- of the operand, but appear forever in Standard.
2308 if It.Typ = Universal_Fixed
2309 and then Scope (It.Nam) = Standard_Standard
2312 ("\\possible interpretation as universal_fixed "
2313 & "operation (RM 4.5.5 (19))", N);
2316 ("\\possible interpretation (predefined)#!", N);
2320 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2323 ("\\possible interpretation (inherited)#!", N);
2325 Error_Msg_N -- CODEFIX
2326 ("\\possible interpretation#!", N);
2332 -- We have a matching interpretation, Expr_Type is the type
2333 -- from this interpretation, and Seen is the entity.
2335 -- For an operator, just set the entity name. The type will be
2336 -- set by the specific operator resolution routine.
2338 if Nkind (N) in N_Op then
2339 Set_Entity (N, Seen);
2340 Generate_Reference (Seen, N);
2342 elsif Nkind (N) = N_Case_Expression then
2343 Set_Etype (N, Expr_Type);
2345 elsif Nkind (N) = N_Character_Literal then
2346 Set_Etype (N, Expr_Type);
2348 elsif Nkind (N) = N_If_Expression then
2349 Set_Etype (N, Expr_Type);
2351 -- AI05-0139-2: Expression is overloaded because type has
2352 -- implicit dereference. If type matches context, no implicit
2353 -- dereference is involved.
2355 elsif Has_Implicit_Dereference (Expr_Type) then
2356 Set_Etype (N, Expr_Type);
2357 Set_Is_Overloaded (N, False);
2360 elsif Is_Overloaded (N)
2361 and then Present (It.Nam)
2362 and then Ekind (It.Nam) = E_Discriminant
2363 and then Has_Implicit_Dereference (It.Nam)
2365 Build_Explicit_Dereference (N, It.Nam);
2367 -- For an explicit dereference, attribute reference, range,
2368 -- short-circuit form (which is not an operator node), or call
2369 -- with a name that is an explicit dereference, there is
2370 -- nothing to be done at this point.
2372 elsif Nkind_In (N, N_Explicit_Dereference,
2373 N_Attribute_Reference,
2375 N_Indexed_Component,
2378 N_Selected_Component,
2380 or else Nkind (Name (N)) = N_Explicit_Dereference
2384 -- For procedure or function calls, set the type of the name,
2385 -- and also the entity pointer for the prefix.
2387 elsif Nkind (N) in N_Subprogram_Call
2388 and then Is_Entity_Name (Name (N))
2390 Set_Etype (Name (N), Expr_Type);
2391 Set_Entity (Name (N), Seen);
2392 Generate_Reference (Seen, Name (N));
2394 elsif Nkind (N) = N_Function_Call
2395 and then Nkind (Name (N)) = N_Selected_Component
2397 Set_Etype (Name (N), Expr_Type);
2398 Set_Entity (Selector_Name (Name (N)), Seen);
2399 Generate_Reference (Seen, Selector_Name (Name (N)));
2401 -- For all other cases, just set the type of the Name
2404 Set_Etype (Name (N), Expr_Type);
2411 -- Move to next interpretation
2413 exit Interp_Loop when No (It.Typ);
2415 Get_Next_Interp (I, It);
2416 end loop Interp_Loop;
2419 -- At this stage Found indicates whether or not an acceptable
2420 -- interpretation exists. If not, then we have an error, except that if
2421 -- the context is Any_Type as a result of some other error, then we
2422 -- suppress the error report.
2425 if Typ /= Any_Type then
2427 -- If type we are looking for is Void, then this is the procedure
2428 -- call case, and the error is simply that what we gave is not a
2429 -- procedure name (we think of procedure calls as expressions with
2430 -- types internally, but the user doesn't think of them this way!)
2432 if Typ = Standard_Void_Type then
2434 -- Special case message if function used as a procedure
2436 if Nkind (N) = N_Procedure_Call_Statement
2437 and then Is_Entity_Name (Name (N))
2438 and then Ekind (Entity (Name (N))) = E_Function
2441 ("cannot use function & in a procedure call",
2442 Name (N), Entity (Name (N)));
2444 -- Otherwise give general message (not clear what cases this
2445 -- covers, but no harm in providing for them!)
2448 Error_Msg_N ("expect procedure name in procedure call", N);
2453 -- Otherwise we do have a subexpression with the wrong type
2455 -- Check for the case of an allocator which uses an access type
2456 -- instead of the designated type. This is a common error and we
2457 -- specialize the message, posting an error on the operand of the
2458 -- allocator, complaining that we expected the designated type of
2461 elsif Nkind (N) = N_Allocator
2462 and then Ekind (Typ) in Access_Kind
2463 and then Ekind (Etype (N)) in Access_Kind
2464 and then Designated_Type (Etype (N)) = Typ
2466 Wrong_Type (Expression (N), Designated_Type (Typ));
2469 -- Check for view mismatch on Null in instances, for which the
2470 -- view-swapping mechanism has no identifier.
2472 elsif (In_Instance or else In_Inlined_Body)
2473 and then (Nkind (N) = N_Null)
2474 and then Is_Private_Type (Typ)
2475 and then Is_Access_Type (Full_View (Typ))
2477 Resolve (N, Full_View (Typ));
2481 -- Check for an aggregate. Sometimes we can get bogus aggregates
2482 -- from misuse of parentheses, and we are about to complain about
2483 -- the aggregate without even looking inside it.
2485 -- Instead, if we have an aggregate of type Any_Composite, then
2486 -- analyze and resolve the component fields, and then only issue
2487 -- another message if we get no errors doing this (otherwise
2488 -- assume that the errors in the aggregate caused the problem).
2490 elsif Nkind (N) = N_Aggregate
2491 and then Etype (N) = Any_Composite
2493 -- Disable expansion in any case. If there is a type mismatch
2494 -- it may be fatal to try to expand the aggregate. The flag
2495 -- would otherwise be set to false when the error is posted.
2497 Expander_Active := False;
2500 procedure Check_Aggr (Aggr : Node_Id);
2501 -- Check one aggregate, and set Found to True if we have a
2502 -- definite error in any of its elements
2504 procedure Check_Elmt (Aelmt : Node_Id);
2505 -- Check one element of aggregate and set Found to True if
2506 -- we definitely have an error in the element.
2512 procedure Check_Aggr (Aggr : Node_Id) is
2516 if Present (Expressions (Aggr)) then
2517 Elmt := First (Expressions (Aggr));
2518 while Present (Elmt) loop
2524 if Present (Component_Associations (Aggr)) then
2525 Elmt := First (Component_Associations (Aggr));
2526 while Present (Elmt) loop
2528 -- If this is a default-initialized component, then
2529 -- there is nothing to check. The box will be
2530 -- replaced by the appropriate call during late
2533 if not Box_Present (Elmt) then
2534 Check_Elmt (Expression (Elmt));
2546 procedure Check_Elmt (Aelmt : Node_Id) is
2548 -- If we have a nested aggregate, go inside it (to
2549 -- attempt a naked analyze-resolve of the aggregate can
2550 -- cause undesirable cascaded errors). Do not resolve
2551 -- expression if it needs a type from context, as for
2552 -- integer * fixed expression.
2554 if Nkind (Aelmt) = N_Aggregate then
2560 if not Is_Overloaded (Aelmt)
2561 and then Etype (Aelmt) /= Any_Fixed
2566 if Etype (Aelmt) = Any_Type then
2577 -- If an error message was issued already, Found got reset to
2578 -- True, so if it is still False, issue standard Wrong_Type msg.
2581 if Is_Overloaded (N)
2582 and then Nkind (N) = N_Function_Call
2585 Subp_Name : Node_Id;
2587 if Is_Entity_Name (Name (N)) then
2588 Subp_Name := Name (N);
2590 elsif Nkind (Name (N)) = N_Selected_Component then
2592 -- Protected operation: retrieve operation name
2594 Subp_Name := Selector_Name (Name (N));
2597 raise Program_Error;
2600 Error_Msg_Node_2 := Typ;
2602 ("no visible interpretation of& "
2603 & "matches expected type&", N, Subp_Name);
2606 if All_Errors_Mode then
2608 Index : Interp_Index;
2612 Error_Msg_N ("\\possible interpretations:", N);
2614 Get_First_Interp (Name (N), Index, It);
2615 while Present (It.Nam) loop
2616 Error_Msg_Sloc := Sloc (It.Nam);
2617 Error_Msg_Node_2 := It.Nam;
2619 ("\\ type& for & declared#", N, It.Typ);
2620 Get_Next_Interp (Index, It);
2625 Error_Msg_N ("\use -gnatf for details", N);
2629 Wrong_Type (N, Typ);
2637 -- Test if we have more than one interpretation for the context
2639 elsif Ambiguous then
2643 -- Only one intepretation
2646 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2647 -- the "+" on T is abstract, and the operands are of universal type,
2648 -- the above code will have (incorrectly) resolved the "+" to the
2649 -- universal one in Standard. Therefore check for this case and give
2650 -- an error. We can't do this earlier, because it would cause legal
2651 -- cases to get errors (when some other type has an abstract "+").
2653 if Ada_Version >= Ada_2005
2654 and then Nkind (N) in N_Op
2655 and then Is_Overloaded (N)
2656 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
2658 Get_First_Interp (N, I, It);
2659 while Present (It.Typ) loop
2660 if Present (It.Abstract_Op) and then
2661 Etype (It.Abstract_Op) = Typ
2664 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2668 Get_Next_Interp (I, It);
2672 -- Here we have an acceptable interpretation for the context
2674 -- Propagate type information and normalize tree for various
2675 -- predefined operations. If the context only imposes a class of
2676 -- types, rather than a specific type, propagate the actual type
2679 if Typ = Any_Integer or else
2680 Typ = Any_Boolean or else
2681 Typ = Any_Modular or else
2682 Typ = Any_Real or else
2685 Ctx_Type := Expr_Type;
2687 -- Any_Fixed is legal in a real context only if a specific fixed-
2688 -- point type is imposed. If Norman Cohen can be confused by this,
2689 -- it deserves a separate message.
2692 and then Expr_Type = Any_Fixed
2694 Error_Msg_N ("illegal context for mixed mode operation", N);
2695 Set_Etype (N, Universal_Real);
2696 Ctx_Type := Universal_Real;
2700 -- A user-defined operator is transformed into a function call at
2701 -- this point, so that further processing knows that operators are
2702 -- really operators (i.e. are predefined operators). User-defined
2703 -- operators that are intrinsic are just renamings of the predefined
2704 -- ones, and need not be turned into calls either, but if they rename
2705 -- a different operator, we must transform the node accordingly.
2706 -- Instantiations of Unchecked_Conversion are intrinsic but are
2707 -- treated as functions, even if given an operator designator.
2709 if Nkind (N) in N_Op
2710 and then Present (Entity (N))
2711 and then Ekind (Entity (N)) /= E_Operator
2714 if not Is_Predefined_Op (Entity (N)) then
2715 Rewrite_Operator_As_Call (N, Entity (N));
2717 elsif Present (Alias (Entity (N)))
2719 Nkind (Parent (Parent (Entity (N)))) =
2720 N_Subprogram_Renaming_Declaration
2722 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2724 -- If the node is rewritten, it will be fully resolved in
2725 -- Rewrite_Renamed_Operator.
2727 if Analyzed (N) then
2733 case N_Subexpr'(Nkind (N)) is
2735 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2737 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2739 when N_Short_Circuit
2740 => Resolve_Short_Circuit (N, Ctx_Type);
2742 when N_Attribute_Reference
2743 => Resolve_Attribute (N, Ctx_Type);
2745 when N_Case_Expression
2746 => Resolve_Case_Expression (N, Ctx_Type);
2748 when N_Character_Literal
2749 => Resolve_Character_Literal (N, Ctx_Type);
2751 when N_Expanded_Name
2752 => Resolve_Entity_Name (N, Ctx_Type);
2754 when N_Explicit_Dereference
2755 => Resolve_Explicit_Dereference (N, Ctx_Type);
2757 when N_Expression_With_Actions
2758 => Resolve_Expression_With_Actions (N, Ctx_Type);
2760 when N_Extension_Aggregate
2761 => Resolve_Extension_Aggregate (N, Ctx_Type);
2763 when N_Function_Call
2764 => Resolve_Call (N, Ctx_Type);
2767 => Resolve_Entity_Name (N, Ctx_Type);
2769 when N_If_Expression
2770 => Resolve_If_Expression (N, Ctx_Type);
2772 when N_Indexed_Component
2773 => Resolve_Indexed_Component (N, Ctx_Type);
2775 when N_Integer_Literal
2776 => Resolve_Integer_Literal (N, Ctx_Type);
2778 when N_Membership_Test
2779 => Resolve_Membership_Op (N, Ctx_Type);
2781 when N_Null => Resolve_Null (N, Ctx_Type);
2783 when N_Op_And | N_Op_Or | N_Op_Xor
2784 => Resolve_Logical_Op (N, Ctx_Type);
2786 when N_Op_Eq | N_Op_Ne
2787 => Resolve_Equality_Op (N, Ctx_Type);
2789 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2790 => Resolve_Comparison_Op (N, Ctx_Type);
2792 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2794 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2795 N_Op_Divide | N_Op_Mod | N_Op_Rem
2797 => Resolve_Arithmetic_Op (N, Ctx_Type);
2799 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2801 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2803 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2804 => Resolve_Unary_Op (N, Ctx_Type);
2806 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2808 when N_Procedure_Call_Statement
2809 => Resolve_Call (N, Ctx_Type);
2811 when N_Operator_Symbol
2812 => Resolve_Operator_Symbol (N, Ctx_Type);
2814 when N_Qualified_Expression
2815 => Resolve_Qualified_Expression (N, Ctx_Type);
2817 -- Why is the following null, needs a comment ???
2819 when N_Quantified_Expression
2822 -- Nothing to do for Raise_Expression, since we took care of
2823 -- setting the Etype earlier, and no other processing is needed.
2825 when N_Raise_Expression
2828 when N_Raise_xxx_Error
2829 => Set_Etype (N, Ctx_Type);
2831 when N_Range => Resolve_Range (N, Ctx_Type);
2834 => Resolve_Real_Literal (N, Ctx_Type);
2836 when N_Reference => Resolve_Reference (N, Ctx_Type);
2838 when N_Selected_Component
2839 => Resolve_Selected_Component (N, Ctx_Type);
2841 when N_Slice => Resolve_Slice (N, Ctx_Type);
2843 when N_String_Literal
2844 => Resolve_String_Literal (N, Ctx_Type);
2846 when N_Subprogram_Info
2847 => Resolve_Subprogram_Info (N, Ctx_Type);
2849 when N_Type_Conversion
2850 => Resolve_Type_Conversion (N, Ctx_Type);
2852 when N_Unchecked_Expression =>
2853 Resolve_Unchecked_Expression (N, Ctx_Type);
2855 when N_Unchecked_Type_Conversion =>
2856 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2859 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2860 -- expression of an anonymous access type that occurs in the context
2861 -- of a named general access type, except when the expression is that
2862 -- of a membership test. This ensures proper legality checking in
2863 -- terms of allowed conversions (expressions that would be illegal to
2864 -- convert implicitly are allowed in membership tests).
2866 if Ada_Version >= Ada_2012
2867 and then Ekind (Ctx_Type) = E_General_Access_Type
2868 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2869 and then Nkind (Parent (N)) not in N_Membership_Test
2871 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2872 Analyze_And_Resolve (N, Ctx_Type);
2875 -- If the subexpression was replaced by a non-subexpression, then
2876 -- all we do is to expand it. The only legitimate case we know of
2877 -- is converting procedure call statement to entry call statements,
2878 -- but there may be others, so we are making this test general.
2880 if Nkind (N) not in N_Subexpr then
2881 Debug_A_Exit ("resolving ", N, " (done)");
2886 -- The expression is definitely NOT overloaded at this point, so
2887 -- we reset the Is_Overloaded flag to avoid any confusion when
2888 -- reanalyzing the node.
2890 Set_Is_Overloaded (N, False);
2892 -- Freeze expression type, entity if it is a name, and designated
2893 -- type if it is an allocator (RM 13.14(10,11,13)).
2895 -- Now that the resolution of the type of the node is complete, and
2896 -- we did not detect an error, we can expand this node. We skip the
2897 -- expand call if we are in a default expression, see section
2898 -- "Handling of Default Expressions" in Sem spec.
2900 Debug_A_Exit ("resolving ", N, " (done)");
2902 -- We unconditionally freeze the expression, even if we are in
2903 -- default expression mode (the Freeze_Expression routine tests this
2904 -- flag and only freezes static types if it is set).
2906 -- Ada 2012 (AI05-177): Expression functions do not freeze. Only
2907 -- their use (in an expanded call) freezes.
2909 if Ekind (Proper_Current_Scope) /= E_Function
2910 or else Nkind (Original_Node (Unit_Declaration_Node
2911 (Proper_Current_Scope))) /= N_Expression_Function
2913 Freeze_Expression (N);
2916 -- Now we can do the expansion
2926 -- Version with check(s) suppressed
2928 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2930 if Suppress = All_Checks then
2932 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
2934 Scope_Suppress.Suppress := (others => True);
2936 Scope_Suppress.Suppress := Sva;
2941 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
2943 Scope_Suppress.Suppress (Suppress) := True;
2945 Scope_Suppress.Suppress (Suppress) := Svg;
2954 -- Version with implicit type
2956 procedure Resolve (N : Node_Id) is
2958 Resolve (N, Etype (N));
2961 ---------------------
2962 -- Resolve_Actuals --
2963 ---------------------
2965 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2966 Loc : constant Source_Ptr := Sloc (N);
2971 Prev : Node_Id := Empty;
2974 procedure Check_Argument_Order;
2975 -- Performs a check for the case where the actuals are all simple
2976 -- identifiers that correspond to the formal names, but in the wrong
2977 -- order, which is considered suspicious and cause for a warning.
2979 procedure Check_Prefixed_Call;
2980 -- If the original node is an overloaded call in prefix notation,
2981 -- insert an 'Access or a dereference as needed over the first actual.
2982 -- Try_Object_Operation has already verified that there is a valid
2983 -- interpretation, but the form of the actual can only be determined
2984 -- once the primitive operation is identified.
2986 procedure Insert_Default;
2987 -- If the actual is missing in a call, insert in the actuals list
2988 -- an instance of the default expression. The insertion is always
2989 -- a named association.
2991 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2992 -- Check whether T1 and T2, or their full views, are derived from a
2993 -- common type. Used to enforce the restrictions on array conversions
2996 function Static_Concatenation (N : Node_Id) return Boolean;
2997 -- Predicate to determine whether an actual that is a concatenation
2998 -- will be evaluated statically and does not need a transient scope.
2999 -- This must be determined before the actual is resolved and expanded
3000 -- because if needed the transient scope must be introduced earlier.
3002 --------------------------
3003 -- Check_Argument_Order --
3004 --------------------------
3006 procedure Check_Argument_Order is
3008 -- Nothing to do if no parameters, or original node is neither a
3009 -- function call nor a procedure call statement (happens in the
3010 -- operator-transformed-to-function call case), or the call does
3011 -- not come from source, or this warning is off.
3013 if not Warn_On_Parameter_Order
3014 or else No (Parameter_Associations (N))
3015 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3016 or else not Comes_From_Source (N)
3022 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3025 -- Nothing to do if only one parameter
3031 -- Here if at least two arguments
3034 Actuals : array (1 .. Nargs) of Node_Id;
3038 Wrong_Order : Boolean := False;
3039 -- Set True if an out of order case is found
3042 -- Collect identifier names of actuals, fail if any actual is
3043 -- not a simple identifier, and record max length of name.
3045 Actual := First (Parameter_Associations (N));
3046 for J in Actuals'Range loop
3047 if Nkind (Actual) /= N_Identifier then
3050 Actuals (J) := Actual;
3055 -- If we got this far, all actuals are identifiers and the list
3056 -- of their names is stored in the Actuals array.
3058 Formal := First_Formal (Nam);
3059 for J in Actuals'Range loop
3061 -- If we ran out of formals, that's odd, probably an error
3062 -- which will be detected elsewhere, but abandon the search.
3068 -- If name matches and is in order OK
3070 if Chars (Formal) = Chars (Actuals (J)) then
3074 -- If no match, see if it is elsewhere in list and if so
3075 -- flag potential wrong order if type is compatible.
3077 for K in Actuals'Range loop
3078 if Chars (Formal) = Chars (Actuals (K))
3080 Has_Compatible_Type (Actuals (K), Etype (Formal))
3082 Wrong_Order := True;
3092 <<Continue>> Next_Formal (Formal);
3095 -- If Formals left over, also probably an error, skip warning
3097 if Present (Formal) then
3101 -- Here we give the warning if something was out of order
3105 ("?P?actuals for this call may be in wrong order", N);
3109 end Check_Argument_Order;
3111 -------------------------
3112 -- Check_Prefixed_Call --
3113 -------------------------
3115 procedure Check_Prefixed_Call is
3116 Act : constant Node_Id := First_Actual (N);
3117 A_Type : constant Entity_Id := Etype (Act);
3118 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3119 Orig : constant Node_Id := Original_Node (N);
3123 -- Check whether the call is a prefixed call, with or without
3124 -- additional actuals.
3126 if Nkind (Orig) = N_Selected_Component
3128 (Nkind (Orig) = N_Indexed_Component
3129 and then Nkind (Prefix (Orig)) = N_Selected_Component
3130 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3131 and then Is_Entity_Name (Act)
3132 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3134 if Is_Access_Type (A_Type)
3135 and then not Is_Access_Type (F_Type)
3137 -- Introduce dereference on object in prefix
3140 Make_Explicit_Dereference (Sloc (Act),
3141 Prefix => Relocate_Node (Act));
3142 Rewrite (Act, New_A);
3145 elsif Is_Access_Type (F_Type)
3146 and then not Is_Access_Type (A_Type)
3148 -- Introduce an implicit 'Access in prefix
3150 if not Is_Aliased_View (Act) then
3152 ("object in prefixed call to& must be aliased"
3153 & " (RM-2005 4.3.1 (13))",
3158 Make_Attribute_Reference (Loc,
3159 Attribute_Name => Name_Access,
3160 Prefix => Relocate_Node (Act)));
3165 end Check_Prefixed_Call;
3167 --------------------
3168 -- Insert_Default --
3169 --------------------
3171 procedure Insert_Default is
3176 -- Missing argument in call, nothing to insert
3178 if No (Default_Value (F)) then
3182 -- Note that we do a full New_Copy_Tree, so that any associated
3183 -- Itypes are properly copied. This may not be needed any more,
3184 -- but it does no harm as a safety measure! Defaults of a generic
3185 -- formal may be out of bounds of the corresponding actual (see
3186 -- cc1311b) and an additional check may be required.
3191 New_Scope => Current_Scope,
3194 if Is_Concurrent_Type (Scope (Nam))
3195 and then Has_Discriminants (Scope (Nam))
3197 Replace_Actual_Discriminants (N, Actval);
3200 if Is_Overloadable (Nam)
3201 and then Present (Alias (Nam))
3203 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3204 and then not Is_Tagged_Type (Etype (F))
3206 -- If default is a real literal, do not introduce a
3207 -- conversion whose effect may depend on the run-time
3208 -- size of universal real.
3210 if Nkind (Actval) = N_Real_Literal then
3211 Set_Etype (Actval, Base_Type (Etype (F)));
3213 Actval := Unchecked_Convert_To (Etype (F), Actval);
3217 if Is_Scalar_Type (Etype (F)) then
3218 Enable_Range_Check (Actval);
3221 Set_Parent (Actval, N);
3223 -- Resolve aggregates with their base type, to avoid scope
3224 -- anomalies: the subtype was first built in the subprogram
3225 -- declaration, and the current call may be nested.
3227 if Nkind (Actval) = N_Aggregate then
3228 Analyze_And_Resolve (Actval, Etype (F));
3230 Analyze_And_Resolve (Actval, Etype (Actval));
3234 Set_Parent (Actval, N);
3236 -- See note above concerning aggregates
3238 if Nkind (Actval) = N_Aggregate
3239 and then Has_Discriminants (Etype (Actval))
3241 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3243 -- Resolve entities with their own type, which may differ from
3244 -- the type of a reference in a generic context (the view
3245 -- swapping mechanism did not anticipate the re-analysis of
3246 -- default values in calls).
3248 elsif Is_Entity_Name (Actval) then
3249 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3252 Analyze_And_Resolve (Actval, Etype (Actval));
3256 -- If default is a tag indeterminate function call, propagate tag
3257 -- to obtain proper dispatching.
3259 if Is_Controlling_Formal (F)
3260 and then Nkind (Default_Value (F)) = N_Function_Call
3262 Set_Is_Controlling_Actual (Actval);
3267 -- If the default expression raises constraint error, then just
3268 -- silently replace it with an N_Raise_Constraint_Error node, since
3269 -- we already gave the warning on the subprogram spec. If node is
3270 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3271 -- the warnings removal machinery.
3273 if Raises_Constraint_Error (Actval)
3274 and then Nkind (Actval) /= N_Raise_Constraint_Error
3277 Make_Raise_Constraint_Error (Loc,
3278 Reason => CE_Range_Check_Failed));
3279 Set_Raises_Constraint_Error (Actval);
3280 Set_Etype (Actval, Etype (F));
3284 Make_Parameter_Association (Loc,
3285 Explicit_Actual_Parameter => Actval,
3286 Selector_Name => Make_Identifier (Loc, Chars (F)));
3288 -- Case of insertion is first named actual
3290 if No (Prev) or else
3291 Nkind (Parent (Prev)) /= N_Parameter_Association
3293 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3294 Set_First_Named_Actual (N, Actval);
3297 if No (Parameter_Associations (N)) then
3298 Set_Parameter_Associations (N, New_List (Assoc));
3300 Append (Assoc, Parameter_Associations (N));
3304 Insert_After (Prev, Assoc);
3307 -- Case of insertion is not first named actual
3310 Set_Next_Named_Actual
3311 (Assoc, Next_Named_Actual (Parent (Prev)));
3312 Set_Next_Named_Actual (Parent (Prev), Actval);
3313 Append (Assoc, Parameter_Associations (N));
3316 Mark_Rewrite_Insertion (Assoc);
3317 Mark_Rewrite_Insertion (Actval);
3326 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3327 FT1 : Entity_Id := T1;
3328 FT2 : Entity_Id := T2;
3331 if Is_Private_Type (T1)
3332 and then Present (Full_View (T1))
3334 FT1 := Full_View (T1);
3337 if Is_Private_Type (T2)
3338 and then Present (Full_View (T2))
3340 FT2 := Full_View (T2);
3343 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3346 --------------------------
3347 -- Static_Concatenation --
3348 --------------------------
3350 function Static_Concatenation (N : Node_Id) return Boolean is
3353 when N_String_Literal =>
3358 -- Concatenation is static when both operands are static and
3359 -- the concatenation operator is a predefined one.
3361 return Scope (Entity (N)) = Standard_Standard
3363 Static_Concatenation (Left_Opnd (N))
3365 Static_Concatenation (Right_Opnd (N));
3368 if Is_Entity_Name (N) then
3370 Ent : constant Entity_Id := Entity (N);
3372 return Ekind (Ent) = E_Constant
3373 and then Present (Constant_Value (Ent))
3375 Is_Static_Expression (Constant_Value (Ent));
3382 end Static_Concatenation;
3384 -- Start of processing for Resolve_Actuals
3387 Check_Argument_Order;
3388 Check_Function_Writable_Actuals (N);
3390 if Present (First_Actual (N)) then
3391 Check_Prefixed_Call;
3394 A := First_Actual (N);
3395 F := First_Formal (Nam);
3396 while Present (F) loop
3397 if No (A) and then Needs_No_Actuals (Nam) then
3400 -- If we have an error in any actual or formal, indicated by a type
3401 -- of Any_Type, then abandon resolution attempt, and set result type
3404 elsif (Present (A) and then Etype (A) = Any_Type)
3405 or else Etype (F) = Any_Type
3407 Set_Etype (N, Any_Type);
3411 -- Case where actual is present
3413 -- If the actual is an entity, generate a reference to it now. We
3414 -- do this before the actual is resolved, because a formal of some
3415 -- protected subprogram, or a task discriminant, will be rewritten
3416 -- during expansion, and the source entity reference may be lost.
3419 and then Is_Entity_Name (A)
3420 and then Comes_From_Source (N)
3422 Orig_A := Entity (A);
3424 if Present (Orig_A) then
3425 if Is_Formal (Orig_A)
3426 and then Ekind (F) /= E_In_Parameter
3428 Generate_Reference (Orig_A, A, 'm');
3430 elsif not Is_Overloaded (A) then
3431 if Ekind (F) /= E_Out_Parameter then
3432 Generate_Reference (Orig_A, A);
3434 -- RM 6.4.1(12): For an out parameter that is passed by
3435 -- copy, the formal parameter object is created, and:
3437 -- * For an access type, the formal parameter is initialized
3438 -- from the value of the actual, without checking that the
3439 -- value satisfies any constraint, any predicate, or any
3440 -- exclusion of the null value.
3442 -- * For a scalar type that has the Default_Value aspect
3443 -- specified, the formal parameter is initialized from the
3444 -- value of the actual, without checking that the value
3445 -- satisfies any constraint or any predicate.
3446 -- I do not understand why this case is included??? this is
3447 -- not a case where an OUT parameter is treated as IN OUT.
3449 -- * For a composite type with discriminants or that has
3450 -- implicit initial values for any subcomponents, the
3451 -- behavior is as for an in out parameter passed by copy.
3453 -- Hence for these cases we generate the read reference now
3454 -- (the write reference will be generated later by
3455 -- Note_Possible_Modification).
3457 elsif Is_By_Copy_Type (Etype (F))
3459 (Is_Access_Type (Etype (F))
3461 (Is_Scalar_Type (Etype (F))
3463 Present (Default_Aspect_Value (Etype (F))))
3465 (Is_Composite_Type (Etype (F))
3466 and then (Has_Discriminants (Etype (F))
3467 or else Is_Partially_Initialized_Type
3470 Generate_Reference (Orig_A, A);
3477 and then (Nkind (Parent (A)) /= N_Parameter_Association
3478 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3480 -- If style checking mode on, check match of formal name
3483 if Nkind (Parent (A)) = N_Parameter_Association then
3484 Check_Identifier (Selector_Name (Parent (A)), F);
3488 -- If the formal is Out or In_Out, do not resolve and expand the
3489 -- conversion, because it is subsequently expanded into explicit
3490 -- temporaries and assignments. However, the object of the
3491 -- conversion can be resolved. An exception is the case of tagged
3492 -- type conversion with a class-wide actual. In that case we want
3493 -- the tag check to occur and no temporary will be needed (no
3494 -- representation change can occur) and the parameter is passed by
3495 -- reference, so we go ahead and resolve the type conversion.
3496 -- Another exception is the case of reference to component or
3497 -- subcomponent of a bit-packed array, in which case we want to
3498 -- defer expansion to the point the in and out assignments are
3501 if Ekind (F) /= E_In_Parameter
3502 and then Nkind (A) = N_Type_Conversion
3503 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3505 if Ekind (F) = E_In_Out_Parameter
3506 and then Is_Array_Type (Etype (F))
3508 -- In a view conversion, the conversion must be legal in
3509 -- both directions, and thus both component types must be
3510 -- aliased, or neither (4.6 (8)).
3512 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3513 -- the privacy requirement should not apply to generic
3514 -- types, and should be checked in an instance. ARG query
3517 if Has_Aliased_Components (Etype (Expression (A))) /=
3518 Has_Aliased_Components (Etype (F))
3521 ("both component types in a view conversion must be"
3522 & " aliased, or neither", A);
3524 -- Comment here??? what set of cases???
3527 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3529 -- Check view conv between unrelated by ref array types
3531 if Is_By_Reference_Type (Etype (F))
3532 or else Is_By_Reference_Type (Etype (Expression (A)))
3535 ("view conversion between unrelated by reference "
3536 & "array types not allowed (\'A'I-00246)", A);
3538 -- In Ada 2005 mode, check view conversion component
3539 -- type cannot be private, tagged, or volatile. Note
3540 -- that we only apply this to source conversions. The
3541 -- generated code can contain conversions which are
3542 -- not subject to this test, and we cannot extract the
3543 -- component type in such cases since it is not present.
3545 elsif Comes_From_Source (A)
3546 and then Ada_Version >= Ada_2005
3549 Comp_Type : constant Entity_Id :=
3551 (Etype (Expression (A)));
3553 if (Is_Private_Type (Comp_Type)
3554 and then not Is_Generic_Type (Comp_Type))
3555 or else Is_Tagged_Type (Comp_Type)
3556 or else Is_Volatile (Comp_Type)
3559 ("component type of a view conversion cannot"
3560 & " be private, tagged, or volatile"
3569 -- Resolve expression if conversion is all OK
3571 if (Conversion_OK (A)
3572 or else Valid_Conversion (A, Etype (A), Expression (A)))
3573 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3575 Resolve (Expression (A));
3578 -- If the actual is a function call that returns a limited
3579 -- unconstrained object that needs finalization, create a
3580 -- transient scope for it, so that it can receive the proper
3581 -- finalization list.
3583 elsif Nkind (A) = N_Function_Call
3584 and then Is_Limited_Record (Etype (F))
3585 and then not Is_Constrained (Etype (F))
3586 and then Full_Expander_Active
3587 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3589 Establish_Transient_Scope (A, False);
3590 Resolve (A, Etype (F));
3592 -- A small optimization: if one of the actuals is a concatenation
3593 -- create a block around a procedure call to recover stack space.
3594 -- This alleviates stack usage when several procedure calls in
3595 -- the same statement list use concatenation. We do not perform
3596 -- this wrapping for code statements, where the argument is a
3597 -- static string, and we want to preserve warnings involving
3598 -- sequences of such statements.
3600 elsif Nkind (A) = N_Op_Concat
3601 and then Nkind (N) = N_Procedure_Call_Statement
3602 and then Full_Expander_Active
3604 not (Is_Intrinsic_Subprogram (Nam)
3605 and then Chars (Nam) = Name_Asm)
3606 and then not Static_Concatenation (A)
3608 Establish_Transient_Scope (A, False);
3609 Resolve (A, Etype (F));
3612 if Nkind (A) = N_Type_Conversion
3613 and then Is_Array_Type (Etype (F))
3614 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3616 (Is_Limited_Type (Etype (F))
3617 or else Is_Limited_Type (Etype (Expression (A))))
3620 ("conversion between unrelated limited array types "
3621 & "not allowed (\A\I-00246)", A);
3623 if Is_Limited_Type (Etype (F)) then
3624 Explain_Limited_Type (Etype (F), A);
3627 if Is_Limited_Type (Etype (Expression (A))) then
3628 Explain_Limited_Type (Etype (Expression (A)), A);
3632 -- (Ada 2005: AI-251): If the actual is an allocator whose
3633 -- directly designated type is a class-wide interface, we build
3634 -- an anonymous access type to use it as the type of the
3635 -- allocator. Later, when the subprogram call is expanded, if
3636 -- the interface has a secondary dispatch table the expander
3637 -- will add a type conversion to force the correct displacement
3640 if Nkind (A) = N_Allocator then
3642 DDT : constant Entity_Id :=
3643 Directly_Designated_Type (Base_Type (Etype (F)));
3645 New_Itype : Entity_Id;
3648 if Is_Class_Wide_Type (DDT)
3649 and then Is_Interface (DDT)
3651 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3652 Set_Etype (New_Itype, Etype (A));
3653 Set_Directly_Designated_Type (New_Itype,
3654 Directly_Designated_Type (Etype (A)));
3655 Set_Etype (A, New_Itype);
3658 -- Ada 2005, AI-162:If the actual is an allocator, the
3659 -- innermost enclosing statement is the master of the
3660 -- created object. This needs to be done with expansion
3661 -- enabled only, otherwise the transient scope will not
3662 -- be removed in the expansion of the wrapped construct.
3664 if (Is_Controlled (DDT) or else Has_Task (DDT))
3665 and then Full_Expander_Active
3667 Establish_Transient_Scope (A, False);
3672 -- (Ada 2005): The call may be to a primitive operation of
3673 -- a tagged synchronized type, declared outside of the type.
3674 -- In this case the controlling actual must be converted to
3675 -- its corresponding record type, which is the formal type.
3676 -- The actual may be a subtype, either because of a constraint
3677 -- or because it is a generic actual, so use base type to
3678 -- locate concurrent type.
3680 F_Typ := Base_Type (Etype (F));
3682 if Is_Tagged_Type (F_Typ)
3683 and then (Is_Concurrent_Type (F_Typ)
3684 or else Is_Concurrent_Record_Type (F_Typ))
3686 -- If the actual is overloaded, look for an interpretation
3687 -- that has a synchronized type.
3689 if not Is_Overloaded (A) then
3690 A_Typ := Base_Type (Etype (A));
3694 Index : Interp_Index;
3698 Get_First_Interp (A, Index, It);
3699 while Present (It.Typ) loop
3700 if Is_Concurrent_Type (It.Typ)
3701 or else Is_Concurrent_Record_Type (It.Typ)
3703 A_Typ := Base_Type (It.Typ);
3707 Get_Next_Interp (Index, It);
3713 Full_A_Typ : Entity_Id;
3716 if Present (Full_View (A_Typ)) then
3717 Full_A_Typ := Base_Type (Full_View (A_Typ));
3719 Full_A_Typ := A_Typ;
3722 -- Tagged synchronized type (case 1): the actual is a
3725 if Is_Concurrent_Type (A_Typ)
3726 and then Corresponding_Record_Type (A_Typ) = F_Typ
3729 Unchecked_Convert_To
3730 (Corresponding_Record_Type (A_Typ), A));
3731 Resolve (A, Etype (F));
3733 -- Tagged synchronized type (case 2): the formal is a
3736 elsif Ekind (Full_A_Typ) = E_Record_Type
3738 (Corresponding_Concurrent_Type (Full_A_Typ))
3739 and then Is_Concurrent_Type (F_Typ)
3740 and then Present (Corresponding_Record_Type (F_Typ))
3741 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3743 Resolve (A, Corresponding_Record_Type (F_Typ));
3748 Resolve (A, Etype (F));
3753 -- not a synchronized operation.
3755 Resolve (A, Etype (F));
3762 if Comes_From_Source (Original_Node (N))
3763 and then Nkind_In (Original_Node (N), N_Function_Call,
3764 N_Procedure_Call_Statement)
3766 -- In formal mode, check that actual parameters matching
3767 -- formals of tagged types are objects (or ancestor type
3768 -- conversions of objects), not general expressions.
3770 if Is_Actual_Tagged_Parameter (A) then
3771 if Is_SPARK_Object_Reference (A) then
3774 elsif Nkind (A) = N_Type_Conversion then
3776 Operand : constant Node_Id := Expression (A);
3777 Operand_Typ : constant Entity_Id := Etype (Operand);
3778 Target_Typ : constant Entity_Id := A_Typ;
3781 if not Is_SPARK_Object_Reference (Operand) then
3782 Check_SPARK_Restriction
3783 ("object required", Operand);
3785 -- In formal mode, the only view conversions are those
3786 -- involving ancestor conversion of an extended type.
3789 (Is_Tagged_Type (Target_Typ)
3790 and then not Is_Class_Wide_Type (Target_Typ)
3791 and then Is_Tagged_Type (Operand_Typ)
3792 and then not Is_Class_Wide_Type (Operand_Typ)
3793 and then Is_Ancestor (Target_Typ, Operand_Typ))
3796 (F, E_Out_Parameter, E_In_Out_Parameter)
3798 Check_SPARK_Restriction
3799 ("ancestor conversion is the only permitted "
3800 & "view conversion", A);
3802 Check_SPARK_Restriction
3803 ("ancestor conversion required", A);
3812 Check_SPARK_Restriction ("object required", A);
3815 -- In formal mode, the only view conversions are those
3816 -- involving ancestor conversion of an extended type.
3818 elsif Nkind (A) = N_Type_Conversion
3819 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3821 Check_SPARK_Restriction
3822 ("ancestor conversion is the only permitted view "
3827 -- has warnings suppressed, then we reset Never_Set_In_Source for
3828 -- the calling entity. The reason for this is to catch cases like
3829 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3830 -- uses trickery to modify an IN parameter.
3832 if Ekind (F) = E_In_Parameter
3833 and then Is_Entity_Name (A)
3834 and then Present (Entity (A))
3835 and then Ekind (Entity (A)) = E_Variable
3836 and then Has_Warnings_Off (F_Typ)
3838 Set_Never_Set_In_Source (Entity (A), False);
3841 -- Perform error checks for IN and IN OUT parameters
3843 if Ekind (F) /= E_Out_Parameter then
3845 -- Check unset reference. For scalar parameters, it is clearly
3846 -- wrong to pass an uninitialized value as either an IN or
3847 -- IN-OUT parameter. For composites, it is also clearly an
3848 -- error to pass a completely uninitialized value as an IN
3849 -- parameter, but the case of IN OUT is trickier. We prefer
3850 -- not to give a warning here. For example, suppose there is
3851 -- a routine that sets some component of a record to False.
3852 -- It is perfectly reasonable to make this IN-OUT and allow
3853 -- either initialized or uninitialized records to be passed
3856 -- For partially initialized composite values, we also avoid
3857 -- warnings, since it is quite likely that we are passing a
3858 -- partially initialized value and only the initialized fields
3859 -- will in fact be read in the subprogram.
3861 if Is_Scalar_Type (A_Typ)
3862 or else (Ekind (F) = E_In_Parameter
3863 and then not Is_Partially_Initialized_Type (A_Typ))
3865 Check_Unset_Reference (A);
3868 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3869 -- actual to a nested call, since this is case of reading an
3870 -- out parameter, which is not allowed.
3872 if Ada_Version = Ada_83
3873 and then Is_Entity_Name (A)
3874 and then Ekind (Entity (A)) = E_Out_Parameter
3876 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3880 -- Case of OUT or IN OUT parameter
3882 if Ekind (F) /= E_In_Parameter then
3884 -- For an Out parameter, check for useless assignment. Note
3885 -- that we can't set Last_Assignment this early, because we may
3886 -- kill current values in Resolve_Call, and that call would
3887 -- clobber the Last_Assignment field.
3889 -- Note: call Warn_On_Useless_Assignment before doing the check
3890 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3891 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3892 -- reflects the last assignment, not this one!
3894 if Ekind (F) = E_Out_Parameter then
3895 if Warn_On_Modified_As_Out_Parameter (F)
3896 and then Is_Entity_Name (A)
3897 and then Present (Entity (A))
3898 and then Comes_From_Source (N)
3900 Warn_On_Useless_Assignment (Entity (A), A);
3904 -- Validate the form of the actual. Note that the call to
3905 -- Is_OK_Variable_For_Out_Formal generates the required
3906 -- reference in this case.
3908 -- A call to an initialization procedure for an aggregate
3909 -- component may initialize a nested component of a constant
3910 -- designated object. In this context the object is variable.
3912 if not Is_OK_Variable_For_Out_Formal (A)
3913 and then not Is_Init_Proc (Nam)
3915 Error_Msg_NE ("actual for& must be a variable", A, F);
3918 -- What's the following about???
3920 if Is_Entity_Name (A) then
3921 Kill_Checks (Entity (A));
3927 if Etype (A) = Any_Type then
3928 Set_Etype (N, Any_Type);
3932 -- Apply appropriate range checks for in, out, and in-out
3933 -- parameters. Out and in-out parameters also need a separate
3934 -- check, if there is a type conversion, to make sure the return
3935 -- value meets the constraints of the variable before the
3938 -- Gigi looks at the check flag and uses the appropriate types.
3939 -- For now since one flag is used there is an optimization which
3940 -- might not be done in the In Out case since Gigi does not do
3941 -- any analysis. More thought required about this ???
3943 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3945 -- Apply predicate checks, unless this is a call to the
3946 -- predicate check function itself, which would cause an
3947 -- infinite recursion.
3949 if not (Ekind (Nam) = E_Function
3950 and then (Is_Predicate_Function (Nam)
3952 Is_Predicate_Function_M (Nam)))
3954 Apply_Predicate_Check (A, F_Typ);
3957 -- Apply required constraint checks
3959 if Is_Scalar_Type (Etype (A)) then
3960 Apply_Scalar_Range_Check (A, F_Typ);
3962 elsif Is_Array_Type (Etype (A)) then
3963 Apply_Length_Check (A, F_Typ);
3965 elsif Is_Record_Type (F_Typ)
3966 and then Has_Discriminants (F_Typ)
3967 and then Is_Constrained (F_Typ)
3968 and then (not Is_Derived_Type (F_Typ)
3969 or else Comes_From_Source (Nam))
3971 Apply_Discriminant_Check (A, F_Typ);
3973 elsif Is_Access_Type (F_Typ)
3974 and then Is_Array_Type (Designated_Type (F_Typ))
3975 and then Is_Constrained (Designated_Type (F_Typ))
3977 Apply_Length_Check (A, F_Typ);
3979 elsif Is_Access_Type (F_Typ)
3980 and then Has_Discriminants (Designated_Type (F_Typ))
3981 and then Is_Constrained (Designated_Type (F_Typ))
3983 Apply_Discriminant_Check (A, F_Typ);
3986 Apply_Range_Check (A, F_Typ);
3989 -- Ada 2005 (AI-231): Note that the controlling parameter case
3990 -- already existed in Ada 95, which is partially checked
3991 -- elsewhere (see Checks), and we don't want the warning
3992 -- message to differ.
3994 if Is_Access_Type (F_Typ)
3995 and then Can_Never_Be_Null (F_Typ)
3996 and then Known_Null (A)
3998 if Is_Controlling_Formal (F) then
3999 Apply_Compile_Time_Constraint_Error
4001 Msg => "null value not allowed here??",
4002 Reason => CE_Access_Check_Failed);
4004 elsif Ada_Version >= Ada_2005 then
4005 Apply_Compile_Time_Constraint_Error
4007 Msg => "(Ada 2005) null not allowed in "
4008 & "null-excluding formal??",
4009 Reason => CE_Null_Not_Allowed);
4014 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
4015 if Nkind (A) = N_Type_Conversion then
4016 if Is_Scalar_Type (A_Typ) then
4017 Apply_Scalar_Range_Check
4018 (Expression (A), Etype (Expression (A)), A_Typ);
4021 (Expression (A), Etype (Expression (A)), A_Typ);
4025 if Is_Scalar_Type (F_Typ) then
4026 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4027 elsif Is_Array_Type (F_Typ)
4028 and then Ekind (F) = E_Out_Parameter
4030 Apply_Length_Check (A, F_Typ);
4032 Apply_Range_Check (A, A_Typ, F_Typ);
4037 -- An actual associated with an access parameter is implicitly
4038 -- converted to the anonymous access type of the formal and must
4039 -- satisfy the legality checks for access conversions.
4041 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4042 if not Valid_Conversion (A, F_Typ, A) then
4044 ("invalid implicit conversion for access parameter", A);
4047 -- If the actual is an access selected component of a variable,
4048 -- the call may modify its designated object. It is reasonable
4049 -- to treat this as a potential modification of the enclosing
4050 -- record, to prevent spurious warnings that it should be
4051 -- declared as a constant, because intuitively programmers
4052 -- regard the designated subcomponent as part of the record.
4054 if Nkind (A) = N_Selected_Component
4055 and then Is_Entity_Name (Prefix (A))
4056 and then not Is_Constant_Object (Entity (Prefix (A)))
4058 Note_Possible_Modification (A, Sure => False);
4062 -- Check bad case of atomic/volatile argument (RM C.6(12))
4064 if Is_By_Reference_Type (Etype (F))
4065 and then Comes_From_Source (N)
4067 if Is_Atomic_Object (A)
4068 and then not Is_Atomic (Etype (F))
4071 ("cannot pass atomic argument to non-atomic formal&",
4074 elsif Is_Volatile_Object (A)
4075 and then not Is_Volatile (Etype (F))
4078 ("cannot pass volatile argument to non-volatile formal&",
4083 -- Check that subprograms don't have improper controlling
4084 -- arguments (RM 3.9.2 (9)).
4086 -- A primitive operation may have an access parameter of an
4087 -- incomplete tagged type, but a dispatching call is illegal
4088 -- if the type is still incomplete.
4090 if Is_Controlling_Formal (F) then
4091 Set_Is_Controlling_Actual (A);
4093 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4095 Desig : constant Entity_Id := Designated_Type (Etype (F));
4097 if Ekind (Desig) = E_Incomplete_Type
4098 and then No (Full_View (Desig))
4099 and then No (Non_Limited_View (Desig))
4102 ("premature use of incomplete type& "
4103 & "in dispatching call", A, Desig);
4108 elsif Nkind (A) = N_Explicit_Dereference then
4109 Validate_Remote_Access_To_Class_Wide_Type (A);
4112 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4113 and then not Is_Class_Wide_Type (F_Typ)
4114 and then not Is_Controlling_Formal (F)
4116 Error_Msg_N ("class-wide argument not allowed here!", A);
4118 if Is_Subprogram (Nam)
4119 and then Comes_From_Source (Nam)
4121 Error_Msg_Node_2 := F_Typ;
4123 ("& is not a dispatching operation of &!", A, Nam);
4126 -- Apply the checks described in 3.10.2(27): if the context is a
4127 -- specific access-to-object, the actual cannot be class-wide.
4128 -- Use base type to exclude access_to_subprogram cases.
4130 elsif Is_Access_Type (A_Typ)
4131 and then Is_Access_Type (F_Typ)
4132 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4133 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4134 or else (Nkind (A) = N_Attribute_Reference
4136 Is_Class_Wide_Type (Etype (Prefix (A)))))
4137 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4138 and then not Is_Controlling_Formal (F)
4140 -- Disable these checks for call to imported C++ subprograms
4143 (Is_Entity_Name (Name (N))
4144 and then Is_Imported (Entity (Name (N)))
4145 and then Convention (Entity (Name (N))) = Convention_CPP)
4148 ("access to class-wide argument not allowed here!", A);
4150 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4151 Error_Msg_Node_2 := Designated_Type (F_Typ);
4153 ("& is not a dispatching operation of &!", A, Nam);
4159 -- If it is a named association, treat the selector_name as a
4160 -- proper identifier, and mark the corresponding entity. Ignore
4161 -- this reference in Alfa mode, as it refers to an entity not in
4162 -- scope at the point of reference, so the reference should be
4163 -- ignored for computing effects of subprograms.
4165 if Nkind (Parent (A)) = N_Parameter_Association
4166 and then not Alfa_Mode
4168 Set_Entity (Selector_Name (Parent (A)), F);
4169 Generate_Reference (F, Selector_Name (Parent (A)));
4170 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4171 Generate_Reference (F_Typ, N, ' ');
4176 if Ekind (F) /= E_Out_Parameter then
4177 Check_Unset_Reference (A);
4182 -- Case where actual is not present
4190 end Resolve_Actuals;
4192 -----------------------
4193 -- Resolve_Allocator --
4194 -----------------------
4196 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4197 Desig_T : constant Entity_Id := Designated_Type (Typ);
4198 E : constant Node_Id := Expression (N);
4200 Discrim : Entity_Id;
4203 Assoc : Node_Id := Empty;
4206 procedure Check_Allocator_Discrim_Accessibility
4207 (Disc_Exp : Node_Id;
4208 Alloc_Typ : Entity_Id);
4209 -- Check that accessibility level associated with an access discriminant
4210 -- initialized in an allocator by the expression Disc_Exp is not deeper
4211 -- than the level of the allocator type Alloc_Typ. An error message is
4212 -- issued if this condition is violated. Specialized checks are done for
4213 -- the cases of a constraint expression which is an access attribute or
4214 -- an access discriminant.
4216 function In_Dispatching_Context return Boolean;
4217 -- If the allocator is an actual in a call, it is allowed to be class-
4218 -- wide when the context is not because it is a controlling actual.
4220 -------------------------------------------
4221 -- Check_Allocator_Discrim_Accessibility --
4222 -------------------------------------------
4224 procedure Check_Allocator_Discrim_Accessibility
4225 (Disc_Exp : Node_Id;
4226 Alloc_Typ : Entity_Id)
4229 if Type_Access_Level (Etype (Disc_Exp)) >
4230 Deepest_Type_Access_Level (Alloc_Typ)
4233 ("operand type has deeper level than allocator type", Disc_Exp);
4235 -- When the expression is an Access attribute the level of the prefix
4236 -- object must not be deeper than that of the allocator's type.
4238 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4239 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4241 and then Object_Access_Level (Prefix (Disc_Exp)) >
4242 Deepest_Type_Access_Level (Alloc_Typ)
4245 ("prefix of attribute has deeper level than allocator type",
4248 -- When the expression is an access discriminant the check is against
4249 -- the level of the prefix object.
4251 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4252 and then Nkind (Disc_Exp) = N_Selected_Component
4253 and then Object_Access_Level (Prefix (Disc_Exp)) >
4254 Deepest_Type_Access_Level (Alloc_Typ)
4257 ("access discriminant has deeper level than allocator type",
4260 -- All other cases are legal
4265 end Check_Allocator_Discrim_Accessibility;
4267 ----------------------------
4268 -- In_Dispatching_Context --
4269 ----------------------------
4271 function In_Dispatching_Context return Boolean is
4272 Par : constant Node_Id := Parent (N);
4275 return Nkind (Par) in N_Subprogram_Call
4276 and then Is_Entity_Name (Name (Par))
4277 and then Is_Dispatching_Operation (Entity (Name (Par)));
4278 end In_Dispatching_Context;
4280 -- Start of processing for Resolve_Allocator
4283 -- Replace general access with specific type
4285 if Ekind (Etype (N)) = E_Allocator_Type then
4286 Set_Etype (N, Base_Type (Typ));
4289 if Is_Abstract_Type (Typ) then
4290 Error_Msg_N ("type of allocator cannot be abstract", N);
4293 -- For qualified expression, resolve the expression using the
4294 -- given subtype (nothing to do for type mark, subtype indication)
4296 if Nkind (E) = N_Qualified_Expression then
4297 if Is_Class_Wide_Type (Etype (E))
4298 and then not Is_Class_Wide_Type (Desig_T)
4299 and then not In_Dispatching_Context
4302 ("class-wide allocator not allowed for this access type", N);
4305 Resolve (Expression (E), Etype (E));
4306 Check_Unset_Reference (Expression (E));
4308 -- A qualified expression requires an exact match of the type,
4309 -- class-wide matching is not allowed.
4311 if (Is_Class_Wide_Type (Etype (Expression (E)))
4312 or else Is_Class_Wide_Type (Etype (E)))
4313 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4315 Wrong_Type (Expression (E), Etype (E));
4318 -- Calls to build-in-place functions are not currently supported in
4319 -- allocators for access types associated with a simple storage pool.
4320 -- Supporting such allocators may require passing additional implicit
4321 -- parameters to build-in-place functions (or a significant revision
4322 -- of the current b-i-p implementation to unify the handling for
4323 -- multiple kinds of storage pools). ???
4325 if Is_Immutably_Limited_Type (Desig_T)
4326 and then Nkind (Expression (E)) = N_Function_Call
4329 Pool : constant Entity_Id :=
4330 Associated_Storage_Pool (Root_Type (Typ));
4334 Present (Get_Rep_Pragma
4335 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4338 ("limited function calls not yet supported in simple "
4339 & "storage pool allocators", Expression (E));
4344 -- A special accessibility check is needed for allocators that
4345 -- constrain access discriminants. The level of the type of the
4346 -- expression used to constrain an access discriminant cannot be
4347 -- deeper than the type of the allocator (in contrast to access
4348 -- parameters, where the level of the actual can be arbitrary).
4350 -- We can't use Valid_Conversion to perform this check because
4351 -- in general the type of the allocator is unrelated to the type
4352 -- of the access discriminant.
4354 if Ekind (Typ) /= E_Anonymous_Access_Type
4355 or else Is_Local_Anonymous_Access (Typ)
4357 Subtyp := Entity (Subtype_Mark (E));
4359 Aggr := Original_Node (Expression (E));
4361 if Has_Discriminants (Subtyp)
4362 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4364 Discrim := First_Discriminant (Base_Type (Subtyp));
4366 -- Get the first component expression of the aggregate
4368 if Present (Expressions (Aggr)) then
4369 Disc_Exp := First (Expressions (Aggr));
4371 elsif Present (Component_Associations (Aggr)) then
4372 Assoc := First (Component_Associations (Aggr));
4374 if Present (Assoc) then
4375 Disc_Exp := Expression (Assoc);
4384 while Present (Discrim) and then Present (Disc_Exp) loop
4385 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4386 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4389 Next_Discriminant (Discrim);
4391 if Present (Discrim) then
4392 if Present (Assoc) then
4394 Disc_Exp := Expression (Assoc);
4396 elsif Present (Next (Disc_Exp)) then
4400 Assoc := First (Component_Associations (Aggr));
4402 if Present (Assoc) then
4403 Disc_Exp := Expression (Assoc);
4413 -- For a subtype mark or subtype indication, freeze the subtype
4416 Freeze_Expression (E);
4418 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4420 ("initialization required for access-to-constant allocator", N);
4423 -- A special accessibility check is needed for allocators that
4424 -- constrain access discriminants. The level of the type of the
4425 -- expression used to constrain an access discriminant cannot be
4426 -- deeper than the type of the allocator (in contrast to access
4427 -- parameters, where the level of the actual can be arbitrary).
4428 -- We can't use Valid_Conversion to perform this check because
4429 -- in general the type of the allocator is unrelated to the type
4430 -- of the access discriminant.
4432 if Nkind (Original_Node (E)) = N_Subtype_Indication
4433 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4434 or else Is_Local_Anonymous_Access (Typ))
4436 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4438 if Has_Discriminants (Subtyp) then
4439 Discrim := First_Discriminant (Base_Type (Subtyp));
4440 Constr := First (Constraints (Constraint (Original_Node (E))));
4441 while Present (Discrim) and then Present (Constr) loop
4442 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4443 if Nkind (Constr) = N_Discriminant_Association then
4444 Disc_Exp := Original_Node (Expression (Constr));
4446 Disc_Exp := Original_Node (Constr);
4449 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4452 Next_Discriminant (Discrim);
4459 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4460 -- check that the level of the type of the created object is not deeper
4461 -- than the level of the allocator's access type, since extensions can
4462 -- now occur at deeper levels than their ancestor types. This is a
4463 -- static accessibility level check; a run-time check is also needed in
4464 -- the case of an initialized allocator with a class-wide argument (see
4465 -- Expand_Allocator_Expression).
4467 if Ada_Version >= Ada_2005
4468 and then Is_Class_Wide_Type (Desig_T)
4471 Exp_Typ : Entity_Id;
4474 if Nkind (E) = N_Qualified_Expression then
4475 Exp_Typ := Etype (E);
4476 elsif Nkind (E) = N_Subtype_Indication then
4477 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4479 Exp_Typ := Entity (E);
4482 if Type_Access_Level (Exp_Typ) >
4483 Deepest_Type_Access_Level (Typ)
4485 if In_Instance_Body then
4487 ("??type in allocator has deeper level than "
4488 & "designated class-wide type", E);
4490 ("\??Program_Error will be raised at run time", E);
4492 Make_Raise_Program_Error (Sloc (N),
4493 Reason => PE_Accessibility_Check_Failed));
4496 -- Do not apply Ada 2005 accessibility checks on a class-wide
4497 -- allocator if the type given in the allocator is a formal
4498 -- type. A run-time check will be performed in the instance.
4500 elsif not Is_Generic_Type (Exp_Typ) then
4501 Error_Msg_N ("type in allocator has deeper level than "
4502 & "designated class-wide type", E);
4508 -- Check for allocation from an empty storage pool
4510 if No_Pool_Assigned (Typ) then
4511 Error_Msg_N ("allocation from empty storage pool!", N);
4513 -- If the context is an unchecked conversion, as may happen within an
4514 -- inlined subprogram, the allocator is being resolved with its own
4515 -- anonymous type. In that case, if the target type has a specific
4516 -- storage pool, it must be inherited explicitly by the allocator type.
4518 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4519 and then No (Associated_Storage_Pool (Typ))
4521 Set_Associated_Storage_Pool
4522 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4525 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4526 Check_Restriction (No_Anonymous_Allocators, N);
4529 -- Check that an allocator with task parts isn't for a nested access
4530 -- type when restriction No_Task_Hierarchy applies.
4532 if not Is_Library_Level_Entity (Base_Type (Typ))
4533 and then Has_Task (Base_Type (Desig_T))
4535 Check_Restriction (No_Task_Hierarchy, N);
4538 -- An erroneous allocator may be rewritten as a raise Program_Error
4541 if Nkind (N) = N_Allocator then
4543 -- An anonymous access discriminant is the definition of a
4546 if Ekind (Typ) = E_Anonymous_Access_Type
4547 and then Nkind (Associated_Node_For_Itype (Typ)) =
4548 N_Discriminant_Specification
4551 Discr : constant Entity_Id :=
4552 Defining_Identifier (Associated_Node_For_Itype (Typ));
4555 -- Ada 2012 AI05-0052: If the designated type of the allocator
4556 -- is limited, then the allocator shall not be used to define
4557 -- the value of an access discriminant unless the discriminated
4558 -- type is immutably limited.
4560 if Ada_Version >= Ada_2012
4561 and then Is_Limited_Type (Desig_T)
4562 and then not Is_Immutably_Limited_Type (Scope (Discr))
4565 ("only immutably limited types can have anonymous "
4566 & "access discriminants designating a limited type", N);
4570 -- Avoid marking an allocator as a dynamic coextension if it is
4571 -- within a static construct.
4573 if not Is_Static_Coextension (N) then
4574 Set_Is_Dynamic_Coextension (N);
4577 -- Cleanup for potential static coextensions
4580 Set_Is_Dynamic_Coextension (N, False);
4581 Set_Is_Static_Coextension (N, False);
4585 -- Report a simple error: if the designated object is a local task,
4586 -- its body has not been seen yet, and its activation will fail an
4587 -- elaboration check.
4589 if Is_Task_Type (Desig_T)
4590 and then Scope (Base_Type (Desig_T)) = Current_Scope
4591 and then Is_Compilation_Unit (Current_Scope)
4592 and then Ekind (Current_Scope) = E_Package
4593 and then not In_Package_Body (Current_Scope)
4595 Error_Msg_N ("??cannot activate task before body seen", N);
4596 Error_Msg_N ("\??Program_Error will be raised at run time", N);
4599 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4600 -- type with a task component on a subpool. This action must raise
4601 -- Program_Error at runtime.
4603 if Ada_Version >= Ada_2012
4604 and then Nkind (N) = N_Allocator
4605 and then Present (Subpool_Handle_Name (N))
4606 and then Has_Task (Desig_T)
4608 Error_Msg_N ("??cannot allocate task on subpool", N);
4609 Error_Msg_N ("\??Program_Error will be raised at run time", N);
4612 Make_Raise_Program_Error (Sloc (N),
4613 Reason => PE_Explicit_Raise));
4616 end Resolve_Allocator;
4618 ---------------------------
4619 -- Resolve_Arithmetic_Op --
4620 ---------------------------
4622 -- Used for resolving all arithmetic operators except exponentiation
4624 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4625 L : constant Node_Id := Left_Opnd (N);
4626 R : constant Node_Id := Right_Opnd (N);
4627 TL : constant Entity_Id := Base_Type (Etype (L));
4628 TR : constant Entity_Id := Base_Type (Etype (R));
4632 B_Typ : constant Entity_Id := Base_Type (Typ);
4633 -- We do the resolution using the base type, because intermediate values
4634 -- in expressions always are of the base type, not a subtype of it.
4636 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4637 -- Returns True if N is in a context that expects "any real type"
4639 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4640 -- Return True iff given type is Integer or universal real/integer
4642 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4643 -- Choose type of integer literal in fixed-point operation to conform
4644 -- to available fixed-point type. T is the type of the other operand,
4645 -- which is needed to determine the expected type of N.
4647 procedure Set_Operand_Type (N : Node_Id);
4648 -- Set operand type to T if universal
4650 -------------------------------
4651 -- Expected_Type_Is_Any_Real --
4652 -------------------------------
4654 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4656 -- N is the expression after "delta" in a fixed_point_definition;
4659 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4660 N_Decimal_Fixed_Point_Definition,
4662 -- N is one of the bounds in a real_range_specification;
4665 N_Real_Range_Specification,
4667 -- N is the expression of a delta_constraint;
4670 N_Delta_Constraint);
4671 end Expected_Type_Is_Any_Real;
4673 -----------------------------
4674 -- Is_Integer_Or_Universal --
4675 -----------------------------
4677 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4679 Index : Interp_Index;
4683 if not Is_Overloaded (N) then
4685 return Base_Type (T) = Base_Type (Standard_Integer)
4686 or else T = Universal_Integer
4687 or else T = Universal_Real;
4689 Get_First_Interp (N, Index, It);
4690 while Present (It.Typ) loop
4691 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4692 or else It.Typ = Universal_Integer
4693 or else It.Typ = Universal_Real
4698 Get_Next_Interp (Index, It);
4703 end Is_Integer_Or_Universal;
4705 ----------------------------
4706 -- Set_Mixed_Mode_Operand --
4707 ----------------------------
4709 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4710 Index : Interp_Index;
4714 if Universal_Interpretation (N) = Universal_Integer then
4716 -- A universal integer literal is resolved as standard integer
4717 -- except in the case of a fixed-point result, where we leave it
4718 -- as universal (to be handled by Exp_Fixd later on)
4720 if Is_Fixed_Point_Type (T) then
4721 Resolve (N, Universal_Integer);
4723 Resolve (N, Standard_Integer);
4726 elsif Universal_Interpretation (N) = Universal_Real
4727 and then (T = Base_Type (Standard_Integer)
4728 or else T = Universal_Integer
4729 or else T = Universal_Real)
4731 -- A universal real can appear in a fixed-type context. We resolve
4732 -- the literal with that context, even though this might raise an
4733 -- exception prematurely (the other operand may be zero).
4737 elsif Etype (N) = Base_Type (Standard_Integer)
4738 and then T = Universal_Real
4739 and then Is_Overloaded (N)
4741 -- Integer arg in mixed-mode operation. Resolve with universal
4742 -- type, in case preference rule must be applied.
4744 Resolve (N, Universal_Integer);
4747 and then B_Typ /= Universal_Fixed
4749 -- Not a mixed-mode operation, resolve with context
4753 elsif Etype (N) = Any_Fixed then
4755 -- N may itself be a mixed-mode operation, so use context type
4759 elsif Is_Fixed_Point_Type (T)
4760 and then B_Typ = Universal_Fixed
4761 and then Is_Overloaded (N)
4763 -- Must be (fixed * fixed) operation, operand must have one
4764 -- compatible interpretation.
4766 Resolve (N, Any_Fixed);
4768 elsif Is_Fixed_Point_Type (B_Typ)
4769 and then (T = Universal_Real
4770 or else Is_Fixed_Point_Type (T))
4771 and then Is_Overloaded (N)
4773 -- C * F(X) in a fixed context, where C is a real literal or a
4774 -- fixed-point expression. F must have either a fixed type
4775 -- interpretation or an integer interpretation, but not both.
4777 Get_First_Interp (N, Index, It);
4778 while Present (It.Typ) loop
4779 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4780 if Analyzed (N) then
4781 Error_Msg_N ("ambiguous operand in fixed operation", N);
4783 Resolve (N, Standard_Integer);
4786 elsif Is_Fixed_Point_Type (It.Typ) then
4787 if Analyzed (N) then
4788 Error_Msg_N ("ambiguous operand in fixed operation", N);
4790 Resolve (N, It.Typ);
4794 Get_Next_Interp (Index, It);
4797 -- Reanalyze the literal with the fixed type of the context. If
4798 -- context is Universal_Fixed, we are within a conversion, leave
4799 -- the literal as a universal real because there is no usable
4800 -- fixed type, and the target of the conversion plays no role in
4814 if B_Typ = Universal_Fixed
4815 and then Nkind (Op2) = N_Real_Literal
4817 T2 := Universal_Real;
4822 Set_Analyzed (Op2, False);
4829 end Set_Mixed_Mode_Operand;
4831 ----------------------
4832 -- Set_Operand_Type --
4833 ----------------------
4835 procedure Set_Operand_Type (N : Node_Id) is
4837 if Etype (N) = Universal_Integer
4838 or else Etype (N) = Universal_Real
4842 end Set_Operand_Type;
4844 -- Start of processing for Resolve_Arithmetic_Op
4847 if Comes_From_Source (N)
4848 and then Ekind (Entity (N)) = E_Function
4849 and then Is_Imported (Entity (N))
4850 and then Is_Intrinsic_Subprogram (Entity (N))
4852 Resolve_Intrinsic_Operator (N, Typ);
4855 -- Special-case for mixed-mode universal expressions or fixed point type
4856 -- operation: each argument is resolved separately. The same treatment
4857 -- is required if one of the operands of a fixed point operation is
4858 -- universal real, since in this case we don't do a conversion to a
4859 -- specific fixed-point type (instead the expander handles the case).
4861 -- Set the type of the node to its universal interpretation because
4862 -- legality checks on an exponentiation operand need the context.
4864 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4865 and then Present (Universal_Interpretation (L))
4866 and then Present (Universal_Interpretation (R))
4868 Set_Etype (N, B_Typ);
4869 Resolve (L, Universal_Interpretation (L));
4870 Resolve (R, Universal_Interpretation (R));
4872 elsif (B_Typ = Universal_Real
4873 or else Etype (N) = Universal_Fixed
4874 or else (Etype (N) = Any_Fixed
4875 and then Is_Fixed_Point_Type (B_Typ))
4876 or else (Is_Fixed_Point_Type (B_Typ)
4877 and then (Is_Integer_Or_Universal (L)
4879 Is_Integer_Or_Universal (R))))
4880 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4882 if TL = Universal_Integer or else TR = Universal_Integer then
4883 Check_For_Visible_Operator (N, B_Typ);
4886 -- If context is a fixed type and one operand is integer, the other
4887 -- is resolved with the type of the context.
4889 if Is_Fixed_Point_Type (B_Typ)
4890 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4891 or else TL = Universal_Integer)
4896 elsif Is_Fixed_Point_Type (B_Typ)
4897 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4898 or else TR = Universal_Integer)
4904 Set_Mixed_Mode_Operand (L, TR);
4905 Set_Mixed_Mode_Operand (R, TL);
4908 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4909 -- multiplying operators from being used when the expected type is
4910 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4911 -- some cases where the expected type is actually Any_Real;
4912 -- Expected_Type_Is_Any_Real takes care of that case.
4914 if Etype (N) = Universal_Fixed
4915 or else Etype (N) = Any_Fixed
4917 if B_Typ = Universal_Fixed
4918 and then not Expected_Type_Is_Any_Real (N)
4919 and then not Nkind_In (Parent (N), N_Type_Conversion,
4920 N_Unchecked_Type_Conversion)
4922 Error_Msg_N ("type cannot be determined from context!", N);
4923 Error_Msg_N ("\explicit conversion to result type required", N);
4925 Set_Etype (L, Any_Type);
4926 Set_Etype (R, Any_Type);
4929 if Ada_Version = Ada_83
4930 and then Etype (N) = Universal_Fixed
4932 Nkind_In (Parent (N), N_Type_Conversion,
4933 N_Unchecked_Type_Conversion)
4936 ("(Ada 83) fixed-point operation "
4937 & "needs explicit conversion", N);
4940 -- The expected type is "any real type" in contexts like
4942 -- type T is delta <universal_fixed-expression> ...
4944 -- in which case we need to set the type to Universal_Real
4945 -- so that static expression evaluation will work properly.
4947 if Expected_Type_Is_Any_Real (N) then
4948 Set_Etype (N, Universal_Real);
4950 Set_Etype (N, B_Typ);
4954 elsif Is_Fixed_Point_Type (B_Typ)
4955 and then (Is_Integer_Or_Universal (L)
4956 or else Nkind (L) = N_Real_Literal
4957 or else Nkind (R) = N_Real_Literal
4958 or else Is_Integer_Or_Universal (R))
4960 Set_Etype (N, B_Typ);
4962 elsif Etype (N) = Any_Fixed then
4964 -- If no previous errors, this is only possible if one operand is
4965 -- overloaded and the context is universal. Resolve as such.
4967 Set_Etype (N, B_Typ);
4971 if (TL = Universal_Integer or else TL = Universal_Real)
4973 (TR = Universal_Integer or else TR = Universal_Real)
4975 Check_For_Visible_Operator (N, B_Typ);
4978 -- If the context is Universal_Fixed and the operands are also
4979 -- universal fixed, this is an error, unless there is only one
4980 -- applicable fixed_point type (usually Duration).
4982 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4983 T := Unique_Fixed_Point_Type (N);
4985 if T = Any_Type then
4998 -- If one of the arguments was resolved to a non-universal type.
4999 -- label the result of the operation itself with the same type.
5000 -- Do the same for the universal argument, if any.
5002 T := Intersect_Types (L, R);
5003 Set_Etype (N, Base_Type (T));
5004 Set_Operand_Type (L);
5005 Set_Operand_Type (R);
5008 Generate_Operator_Reference (N, Typ);
5009 Analyze_Dimension (N);
5010 Eval_Arithmetic_Op (N);
5012 -- In SPARK, a multiplication or division with operands of fixed point
5013 -- types shall be qualified or explicitly converted to identify the
5016 if (Is_Fixed_Point_Type (Etype (L))
5017 or else Is_Fixed_Point_Type (Etype (R)))
5018 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5020 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
5022 Check_SPARK_Restriction
5023 ("operation should be qualified or explicitly converted", N);
5026 -- Set overflow and division checking bit
5028 if Nkind (N) in N_Op then
5029 if not Overflow_Checks_Suppressed (Etype (N)) then
5030 Enable_Overflow_Check (N);
5033 -- Give warning if explicit division by zero
5035 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
5036 and then not Division_Checks_Suppressed (Etype (N))
5038 Rop := Right_Opnd (N);
5040 if Compile_Time_Known_Value (Rop)
5041 and then ((Is_Integer_Type (Etype (Rop))
5042 and then Expr_Value (Rop) = Uint_0)
5044 (Is_Real_Type (Etype (Rop))
5045 and then Expr_Value_R (Rop) = Ureal_0))
5047 -- Specialize the warning message according to the operation.
5048 -- The following warnings are for the case
5053 -- For division, we have two cases, for float division
5054 -- of an unconstrained float type, on a machine where
5055 -- Machine_Overflows is false, we don't get an exception
5056 -- at run-time, but rather an infinity or Nan. The Nan
5057 -- case is pretty obscure, so just warn about infinities.
5059 if Is_Floating_Point_Type (Typ)
5060 and then not Is_Constrained (Typ)
5061 and then not Machine_Overflows_On_Target
5064 ("float division by zero, may generate "
5065 & "'+'/'- infinity??", Right_Opnd (N));
5067 -- For all other cases, we get a Constraint_Error
5070 Apply_Compile_Time_Constraint_Error
5071 (N, "division by zero??", CE_Divide_By_Zero,
5072 Loc => Sloc (Right_Opnd (N)));
5076 Apply_Compile_Time_Constraint_Error
5077 (N, "rem with zero divisor??", CE_Divide_By_Zero,
5078 Loc => Sloc (Right_Opnd (N)));
5081 Apply_Compile_Time_Constraint_Error
5082 (N, "mod with zero divisor??", CE_Divide_By_Zero,
5083 Loc => Sloc (Right_Opnd (N)));
5085 -- Division by zero can only happen with division, rem,
5086 -- and mod operations.
5089 raise Program_Error;
5092 -- Otherwise just set the flag to check at run time
5095 Activate_Division_Check (N);
5099 -- If Restriction No_Implicit_Conditionals is active, then it is
5100 -- violated if either operand can be negative for mod, or for rem
5101 -- if both operands can be negative.
5103 if Restriction_Check_Required (No_Implicit_Conditionals)
5104 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5113 -- Set if corresponding operand might be negative
5117 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5118 LNeg := (not OK) or else Lo < 0;
5121 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5122 RNeg := (not OK) or else Lo < 0;
5124 -- Check if we will be generating conditionals. There are two
5125 -- cases where that can happen, first for REM, the only case
5126 -- is largest negative integer mod -1, where the division can
5127 -- overflow, but we still have to give the right result. The
5128 -- front end generates a test for this annoying case. Here we
5129 -- just test if both operands can be negative (that's what the
5130 -- expander does, so we match its logic here).
5132 -- The second case is mod where either operand can be negative.
5133 -- In this case, the back end has to generate additional tests.
5135 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5137 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5139 Check_Restriction (No_Implicit_Conditionals, N);
5145 Check_Unset_Reference (L);
5146 Check_Unset_Reference (R);
5147 Check_Function_Writable_Actuals (N);
5148 end Resolve_Arithmetic_Op;
5154 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5155 Loc : constant Source_Ptr := Sloc (N);
5156 Subp : constant Node_Id := Name (N);
5164 function Same_Or_Aliased_Subprograms
5166 E : Entity_Id) return Boolean;
5167 -- Returns True if the subprogram entity S is the same as E or else
5168 -- S is an alias of E.
5170 ---------------------------------
5171 -- Same_Or_Aliased_Subprograms --
5172 ---------------------------------
5174 function Same_Or_Aliased_Subprograms
5176 E : Entity_Id) return Boolean
5178 Subp_Alias : constant Entity_Id := Alias (S);
5181 or else (Present (Subp_Alias) and then Subp_Alias = E);
5182 end Same_Or_Aliased_Subprograms;
5184 -- Start of processing for Resolve_Call
5187 -- The context imposes a unique interpretation with type Typ on a
5188 -- procedure or function call. Find the entity of the subprogram that
5189 -- yields the expected type, and propagate the corresponding formal
5190 -- constraints on the actuals. The caller has established that an
5191 -- interpretation exists, and emitted an error if not unique.
5193 -- First deal with the case of a call to an access-to-subprogram,
5194 -- dereference made explicit in Analyze_Call.
5196 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5197 if not Is_Overloaded (Subp) then
5198 Nam := Etype (Subp);
5201 -- Find the interpretation whose type (a subprogram type) has a
5202 -- return type that is compatible with the context. Analysis of
5203 -- the node has established that one exists.
5207 Get_First_Interp (Subp, I, It);
5208 while Present (It.Typ) loop
5209 if Covers (Typ, Etype (It.Typ)) then
5214 Get_Next_Interp (I, It);
5218 raise Program_Error;
5222 -- If the prefix is not an entity, then resolve it
5224 if not Is_Entity_Name (Subp) then
5225 Resolve (Subp, Nam);
5228 -- For an indirect call, we always invalidate checks, since we do not
5229 -- know whether the subprogram is local or global. Yes we could do
5230 -- better here, e.g. by knowing that there are no local subprograms,
5231 -- but it does not seem worth the effort. Similarly, we kill all
5232 -- knowledge of current constant values.
5234 Kill_Current_Values;
5236 -- If this is a procedure call which is really an entry call, do
5237 -- the conversion of the procedure call to an entry call. Protected
5238 -- operations use the same circuitry because the name in the call
5239 -- can be an arbitrary expression with special resolution rules.
5241 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5242 or else (Is_Entity_Name (Subp)
5243 and then Ekind (Entity (Subp)) = E_Entry)
5245 Resolve_Entry_Call (N, Typ);
5246 Check_Elab_Call (N);
5248 -- Kill checks and constant values, as above for indirect case
5249 -- Who knows what happens when another task is activated?
5251 Kill_Current_Values;
5254 -- Normal subprogram call with name established in Resolve
5256 elsif not (Is_Type (Entity (Subp))) then
5257 Nam := Entity (Subp);
5258 Set_Entity_With_Style_Check (Subp, Nam);
5260 -- Otherwise we must have the case of an overloaded call
5263 pragma Assert (Is_Overloaded (Subp));
5265 -- Initialize Nam to prevent warning (we know it will be assigned
5266 -- in the loop below, but the compiler does not know that).
5270 Get_First_Interp (Subp, I, It);
5271 while Present (It.Typ) loop
5272 if Covers (Typ, It.Typ) then
5274 Set_Entity_With_Style_Check (Subp, Nam);
5278 Get_Next_Interp (I, It);
5282 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5283 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5284 and then Nkind (Subp) /= N_Explicit_Dereference
5285 and then Present (Parameter_Associations (N))
5287 -- The prefix is a parameterless function call that returns an access
5288 -- to subprogram. If parameters are present in the current call, add
5289 -- add an explicit dereference. We use the base type here because
5290 -- within an instance these may be subtypes.
5292 -- The dereference is added either in Analyze_Call or here. Should
5293 -- be consolidated ???
5295 Set_Is_Overloaded (Subp, False);
5296 Set_Etype (Subp, Etype (Nam));
5297 Insert_Explicit_Dereference (Subp);
5298 Nam := Designated_Type (Etype (Nam));
5299 Resolve (Subp, Nam);
5302 -- Check that a call to Current_Task does not occur in an entry body
5304 if Is_RTE (Nam, RE_Current_Task) then
5313 -- Exclude calls that occur within the default of a formal
5314 -- parameter of the entry, since those are evaluated outside
5317 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5319 if Nkind (P) = N_Entry_Body
5320 or else (Nkind (P) = N_Subprogram_Body
5321 and then Is_Entry_Barrier_Function (P))
5325 ("??& should not be used in entry body (RM C.7(17))",
5328 ("\Program_Error will be raised at run time??", N, Nam);
5330 Make_Raise_Program_Error (Loc,
5331 Reason => PE_Current_Task_In_Entry_Body));
5332 Set_Etype (N, Rtype);
5339 -- Check that a procedure call does not occur in the context of the
5340 -- entry call statement of a conditional or timed entry call. Note that
5341 -- the case of a call to a subprogram renaming of an entry will also be
5342 -- rejected. The test for N not being an N_Entry_Call_Statement is
5343 -- defensive, covering the possibility that the processing of entry
5344 -- calls might reach this point due to later modifications of the code
5347 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5348 and then Nkind (N) /= N_Entry_Call_Statement
5349 and then Entry_Call_Statement (Parent (N)) = N
5351 if Ada_Version < Ada_2005 then
5352 Error_Msg_N ("entry call required in select statement", N);
5354 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5355 -- for a procedure_or_entry_call, the procedure_name or
5356 -- procedure_prefix of the procedure_call_statement shall denote
5357 -- an entry renamed by a procedure, or (a view of) a primitive
5358 -- subprogram of a limited interface whose first parameter is
5359 -- a controlling parameter.
5361 elsif Nkind (N) = N_Procedure_Call_Statement
5362 and then not Is_Renamed_Entry (Nam)
5363 and then not Is_Controlling_Limited_Procedure (Nam)
5366 ("entry call or dispatching primitive of interface required", N);
5370 -- Check that this is not a call to a protected procedure or entry from
5371 -- within a protected function.
5373 Check_Internal_Protected_Use (N, Nam);
5375 -- Freeze the subprogram name if not in a spec-expression. Note that we
5376 -- freeze procedure calls as well as function calls. Procedure calls are
5377 -- not frozen according to the rules (RM 13.14(14)) because it is
5378 -- impossible to have a procedure call to a non-frozen procedure in pure
5379 -- Ada, but in the code that we generate in the expander, this rule
5380 -- needs extending because we can generate procedure calls that need
5383 -- In Ada 2012, expression functions may be called within pre/post
5384 -- conditions of subsequent functions or expression functions. Such
5385 -- calls do not freeze when they appear within generated bodies, which
5386 -- would place the freeze node in the wrong scope. An expression
5387 -- function is frozen in the usual fashion, by the appearance of a real
5388 -- body, or at the end of a declarative part.
5390 if Is_Entity_Name (Subp) and then not In_Spec_Expression
5392 (not Is_Expression_Function (Entity (Subp))
5393 or else Scope (Entity (Subp)) = Current_Scope)
5395 Freeze_Expression (Subp);
5398 -- For a predefined operator, the type of the result is the type imposed
5399 -- by context, except for a predefined operation on universal fixed.
5400 -- Otherwise The type of the call is the type returned by the subprogram
5403 if Is_Predefined_Op (Nam) then
5404 if Etype (N) /= Universal_Fixed then
5408 -- If the subprogram returns an array type, and the context requires the
5409 -- component type of that array type, the node is really an indexing of
5410 -- the parameterless call. Resolve as such. A pathological case occurs
5411 -- when the type of the component is an access to the array type. In
5412 -- this case the call is truly ambiguous.
5414 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5416 ((Is_Array_Type (Etype (Nam))
5417 and then Covers (Typ, Component_Type (Etype (Nam))))
5418 or else (Is_Access_Type (Etype (Nam))
5419 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5423 Component_Type (Designated_Type (Etype (Nam))))))
5426 Index_Node : Node_Id;
5428 Ret_Type : constant Entity_Id := Etype (Nam);
5431 if Is_Access_Type (Ret_Type)
5432 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5435 ("cannot disambiguate function call and indexing", N);
5437 New_Subp := Relocate_Node (Subp);
5438 Set_Entity (Subp, Nam);
5440 if (Is_Array_Type (Ret_Type)
5441 and then Component_Type (Ret_Type) /= Any_Type)
5443 (Is_Access_Type (Ret_Type)
5445 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5447 if Needs_No_Actuals (Nam) then
5449 -- Indexed call to a parameterless function
5452 Make_Indexed_Component (Loc,
5454 Make_Function_Call (Loc,
5456 Expressions => Parameter_Associations (N));
5458 -- An Ada 2005 prefixed call to a primitive operation
5459 -- whose first parameter is the prefix. This prefix was
5460 -- prepended to the parameter list, which is actually a
5461 -- list of indexes. Remove the prefix in order to build
5462 -- the proper indexed component.
5465 Make_Indexed_Component (Loc,
5467 Make_Function_Call (Loc,
5469 Parameter_Associations =>
5471 (Remove_Head (Parameter_Associations (N)))),
5472 Expressions => Parameter_Associations (N));
5475 -- Preserve the parenthesis count of the node
5477 Set_Paren_Count (Index_Node, Paren_Count (N));
5479 -- Since we are correcting a node classification error made
5480 -- by the parser, we call Replace rather than Rewrite.
5482 Replace (N, Index_Node);
5484 Set_Etype (Prefix (N), Ret_Type);
5486 Resolve_Indexed_Component (N, Typ);
5487 Check_Elab_Call (Prefix (N));
5495 Set_Etype (N, Etype (Nam));
5498 -- In the case where the call is to an overloaded subprogram, Analyze
5499 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5500 -- such a case Normalize_Actuals needs to be called once more to order
5501 -- the actuals correctly. Otherwise the call will have the ordering
5502 -- given by the last overloaded subprogram whether this is the correct
5503 -- one being called or not.
5505 if Is_Overloaded (Subp) then
5506 Normalize_Actuals (N, Nam, False, Norm_OK);
5507 pragma Assert (Norm_OK);
5510 -- In any case, call is fully resolved now. Reset Overload flag, to
5511 -- prevent subsequent overload resolution if node is analyzed again
5513 Set_Is_Overloaded (Subp, False);
5514 Set_Is_Overloaded (N, False);
5516 -- If we are calling the current subprogram from immediately within its
5517 -- body, then that is the case where we can sometimes detect cases of
5518 -- infinite recursion statically. Do not try this in case restriction
5519 -- No_Recursion is in effect anyway, and do it only for source calls.
5521 if Comes_From_Source (N) then
5522 Scop := Current_Scope;
5524 -- Issue warning for possible infinite recursion in the absence
5525 -- of the No_Recursion restriction.
5527 if Same_Or_Aliased_Subprograms (Nam, Scop)
5528 and then not Restriction_Active (No_Recursion)
5529 and then Check_Infinite_Recursion (N)
5531 -- Here we detected and flagged an infinite recursion, so we do
5532 -- not need to test the case below for further warnings. Also we
5533 -- are all done if we now have a raise SE node.
5535 if Nkind (N) = N_Raise_Storage_Error then
5539 -- If call is to immediately containing subprogram, then check for
5540 -- the case of a possible run-time detectable infinite recursion.
5543 Scope_Loop : while Scop /= Standard_Standard loop
5544 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5546 -- Although in general case, recursion is not statically
5547 -- checkable, the case of calling an immediately containing
5548 -- subprogram is easy to catch.
5550 Check_Restriction (No_Recursion, N);
5552 -- If the recursive call is to a parameterless subprogram,
5553 -- then even if we can't statically detect infinite
5554 -- recursion, this is pretty suspicious, and we output a
5555 -- warning. Furthermore, we will try later to detect some
5556 -- cases here at run time by expanding checking code (see
5557 -- Detect_Infinite_Recursion in package Exp_Ch6).
5559 -- If the recursive call is within a handler, do not emit a
5560 -- warning, because this is a common idiom: loop until input
5561 -- is correct, catch illegal input in handler and restart.
5563 if No (First_Formal (Nam))
5564 and then Etype (Nam) = Standard_Void_Type
5565 and then not Error_Posted (N)
5566 and then Nkind (Parent (N)) /= N_Exception_Handler
5568 -- For the case of a procedure call. We give the message
5569 -- only if the call is the first statement in a sequence
5570 -- of statements, or if all previous statements are
5571 -- simple assignments. This is simply a heuristic to
5572 -- decrease false positives, without losing too many good
5573 -- warnings. The idea is that these previous statements
5574 -- may affect global variables the procedure depends on.
5575 -- We also exclude raise statements, that may arise from
5576 -- constraint checks and are probably unrelated to the
5577 -- intended control flow.
5579 if Nkind (N) = N_Procedure_Call_Statement
5580 and then Is_List_Member (N)
5586 while Present (P) loop
5588 N_Assignment_Statement,
5589 N_Raise_Constraint_Error)
5599 -- Do not give warning if we are in a conditional context
5602 K : constant Node_Kind := Nkind (Parent (N));
5604 if (K = N_Loop_Statement
5605 and then Present (Iteration_Scheme (Parent (N))))
5606 or else K = N_If_Statement
5607 or else K = N_Elsif_Part
5608 or else K = N_Case_Statement_Alternative
5614 -- Here warning is to be issued
5616 Set_Has_Recursive_Call (Nam);
5618 ("??possible infinite recursion!", N);
5620 ("\??Storage_Error may be raised at run time!", N);
5626 Scop := Scope (Scop);
5627 end loop Scope_Loop;
5631 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5633 Check_Obsolescent_2005_Entity (Nam, Subp);
5635 -- If subprogram name is a predefined operator, it was given in
5636 -- functional notation. Replace call node with operator node, so
5637 -- that actuals can be resolved appropriately.
5639 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5640 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5643 elsif Present (Alias (Nam))
5644 and then Is_Predefined_Op (Alias (Nam))
5646 Resolve_Actuals (N, Nam);
5647 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5651 -- Create a transient scope if the resulting type requires it
5653 -- There are several notable exceptions:
5655 -- a) In init procs, the transient scope overhead is not needed, and is
5656 -- even incorrect when the call is a nested initialization call for a
5657 -- component whose expansion may generate adjust calls. However, if the
5658 -- call is some other procedure call within an initialization procedure
5659 -- (for example a call to Create_Task in the init_proc of the task
5660 -- run-time record) a transient scope must be created around this call.
5662 -- b) Enumeration literal pseudo-calls need no transient scope
5664 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5665 -- functions) do not use the secondary stack even though the return
5666 -- type may be unconstrained.
5668 -- d) Calls to a build-in-place function, since such functions may
5669 -- allocate their result directly in a target object, and cases where
5670 -- the result does get allocated in the secondary stack are checked for
5671 -- within the specialized Exp_Ch6 procedures for expanding those
5672 -- build-in-place calls.
5674 -- e) If the subprogram is marked Inline_Always, then even if it returns
5675 -- an unconstrained type the call does not require use of the secondary
5676 -- stack. However, inlining will only take place if the body to inline
5677 -- is already present. It may not be available if e.g. the subprogram is
5678 -- declared in a child instance.
5680 -- If this is an initialization call for a type whose construction
5681 -- uses the secondary stack, and it is not a nested call to initialize
5682 -- a component, we do need to create a transient scope for it. We
5683 -- check for this by traversing the type in Check_Initialization_Call.
5686 and then Has_Pragma_Inline_Always (Nam)
5687 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5688 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5689 and then not Debug_Flag_Dot_K
5693 elsif Is_Inlined (Nam)
5694 and then Has_Pragma_Inline (Nam)
5695 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5696 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5697 and then Debug_Flag_Dot_K
5701 elsif Ekind (Nam) = E_Enumeration_Literal
5702 or else Is_Build_In_Place_Function (Nam)
5703 or else Is_Intrinsic_Subprogram (Nam)
5707 elsif Full_Expander_Active
5708 and then Is_Type (Etype (Nam))
5709 and then Requires_Transient_Scope (Etype (Nam))
5711 (not Within_Init_Proc
5713 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5715 Establish_Transient_Scope (N, Sec_Stack => True);
5717 -- If the call appears within the bounds of a loop, it will
5718 -- be rewritten and reanalyzed, nothing left to do here.
5720 if Nkind (N) /= N_Function_Call then
5724 elsif Is_Init_Proc (Nam)
5725 and then not Within_Init_Proc
5727 Check_Initialization_Call (N, Nam);
5730 -- A protected function cannot be called within the definition of the
5731 -- enclosing protected type.
5733 if Is_Protected_Type (Scope (Nam))
5734 and then In_Open_Scopes (Scope (Nam))
5735 and then not Has_Completion (Scope (Nam))
5738 ("& cannot be called before end of protected definition", N, Nam);
5741 -- Propagate interpretation to actuals, and add default expressions
5744 if Present (First_Formal (Nam)) then
5745 Resolve_Actuals (N, Nam);
5747 -- Overloaded literals are rewritten as function calls, for purpose of
5748 -- resolution. After resolution, we can replace the call with the
5751 elsif Ekind (Nam) = E_Enumeration_Literal then
5752 Copy_Node (Subp, N);
5753 Resolve_Entity_Name (N, Typ);
5755 -- Avoid validation, since it is a static function call
5757 Generate_Reference (Nam, Subp);
5761 -- If the subprogram is not global, then kill all saved values and
5762 -- checks. This is a bit conservative, since in many cases we could do
5763 -- better, but it is not worth the effort. Similarly, we kill constant
5764 -- values. However we do not need to do this for internal entities
5765 -- (unless they are inherited user-defined subprograms), since they
5766 -- are not in the business of molesting local values.
5768 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5769 -- kill all checks and values for calls to global subprograms. This
5770 -- takes care of the case where an access to a local subprogram is
5771 -- taken, and could be passed directly or indirectly and then called
5772 -- from almost any context.
5774 -- Note: we do not do this step till after resolving the actuals. That
5775 -- way we still take advantage of the current value information while
5776 -- scanning the actuals.
5778 -- We suppress killing values if we are processing the nodes associated
5779 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5780 -- type kills all the values as part of analyzing the code that
5781 -- initializes the dispatch tables.
5783 if Inside_Freezing_Actions = 0
5784 and then (not Is_Library_Level_Entity (Nam)
5785 or else Suppress_Value_Tracking_On_Call
5786 (Nearest_Dynamic_Scope (Current_Scope)))
5787 and then (Comes_From_Source (Nam)
5788 or else (Present (Alias (Nam))
5789 and then Comes_From_Source (Alias (Nam))))
5791 Kill_Current_Values;
5794 -- If we are warning about unread OUT parameters, this is the place to
5795 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5796 -- after the above call to Kill_Current_Values (since that call clears
5797 -- the Last_Assignment field of all local variables).
5799 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5800 and then Comes_From_Source (N)
5801 and then In_Extended_Main_Source_Unit (N)
5808 F := First_Formal (Nam);
5809 A := First_Actual (N);
5810 while Present (F) and then Present (A) loop
5811 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5812 and then Warn_On_Modified_As_Out_Parameter (F)
5813 and then Is_Entity_Name (A)
5814 and then Present (Entity (A))
5815 and then Comes_From_Source (N)
5816 and then Safe_To_Capture_Value (N, Entity (A))
5818 Set_Last_Assignment (Entity (A), A);
5827 -- If the subprogram is a primitive operation, check whether or not
5828 -- it is a correct dispatching call.
5830 if Is_Overloadable (Nam)
5831 and then Is_Dispatching_Operation (Nam)
5833 Check_Dispatching_Call (N);
5835 elsif Ekind (Nam) /= E_Subprogram_Type
5836 and then Is_Abstract_Subprogram (Nam)
5837 and then not In_Instance
5839 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5842 -- If this is a dispatching call, generate the appropriate reference,
5843 -- for better source navigation in GPS.
5845 if Is_Overloadable (Nam)
5846 and then Present (Controlling_Argument (N))
5848 Generate_Reference (Nam, Subp, 'R');
5850 -- Normal case, not a dispatching call: generate a call reference
5853 Generate_Reference (Nam, Subp, 's');
5856 if Is_Intrinsic_Subprogram (Nam) then
5857 Check_Intrinsic_Call (N);
5860 -- Check for violation of restriction No_Specific_Termination_Handlers
5861 -- and warn on a potentially blocking call to Abort_Task.
5863 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5864 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5866 Is_RTE (Nam, RE_Specific_Handler))
5868 Check_Restriction (No_Specific_Termination_Handlers, N);
5870 elsif Is_RTE (Nam, RE_Abort_Task) then
5871 Check_Potentially_Blocking_Operation (N);
5874 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5875 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5876 -- need to check the second argument to determine whether it is an
5877 -- absolute or relative timing event.
5879 if Restriction_Check_Required (No_Relative_Delay)
5880 and then Is_RTE (Nam, RE_Set_Handler)
5881 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5883 Check_Restriction (No_Relative_Delay, N);
5886 -- Issue an error for a call to an eliminated subprogram. This routine
5887 -- will not perform the check if the call appears within a default
5890 Check_For_Eliminated_Subprogram (Subp, Nam);
5892 -- In formal mode, the primitive operations of a tagged type or type
5893 -- extension do not include functions that return the tagged type.
5895 -- Commented out as the call to Is_Inherited_Operation_For_Type may
5896 -- cause an error because the type entity of the parent node of
5897 -- Entity (Name (N) may not be set. ???
5898 -- So why not just add a guard ???
5900 -- if Nkind (N) = N_Function_Call
5901 -- and then Is_Tagged_Type (Etype (N))
5902 -- and then Is_Entity_Name (Name (N))
5903 -- and then Is_Inherited_Operation_For_Type
5904 -- (Entity (Name (N)), Etype (N))
5906 -- Check_SPARK_Restriction ("function not inherited", N);
5909 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5910 -- class-wide and the call dispatches on result in a context that does
5911 -- not provide a tag, the call raises Program_Error.
5913 if Nkind (N) = N_Function_Call
5914 and then In_Instance
5915 and then Is_Generic_Actual_Type (Typ)
5916 and then Is_Class_Wide_Type (Typ)
5917 and then Has_Controlling_Result (Nam)
5918 and then Nkind (Parent (N)) = N_Object_Declaration
5920 -- Verify that none of the formals are controlling
5923 Call_OK : Boolean := False;
5927 F := First_Formal (Nam);
5928 while Present (F) loop
5929 if Is_Controlling_Formal (F) then
5938 Error_Msg_N ("!?? cannot determine tag of result", N);
5939 Error_Msg_N ("!?? Program_Error will be raised", N);
5941 Make_Raise_Program_Error (Sloc (N),
5942 Reason => PE_Explicit_Raise));
5947 -- Check the dimensions of the actuals in the call. For function calls,
5948 -- propagate the dimensions from the returned type to N.
5950 Analyze_Dimension_Call (N, Nam);
5952 -- All done, evaluate call and deal with elaboration issues
5955 Check_Elab_Call (N);
5956 Warn_On_Overlapping_Actuals (Nam, N);
5959 -----------------------------
5960 -- Resolve_Case_Expression --
5961 -----------------------------
5963 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5967 Alt := First (Alternatives (N));
5968 while Present (Alt) loop
5969 Resolve (Expression (Alt), Typ);
5974 Eval_Case_Expression (N);
5975 end Resolve_Case_Expression;
5977 -------------------------------
5978 -- Resolve_Character_Literal --
5979 -------------------------------
5981 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5982 B_Typ : constant Entity_Id := Base_Type (Typ);
5986 -- Verify that the character does belong to the type of the context
5988 Set_Etype (N, B_Typ);
5989 Eval_Character_Literal (N);
5991 -- Wide_Wide_Character literals must always be defined, since the set
5992 -- of wide wide character literals is complete, i.e. if a character
5993 -- literal is accepted by the parser, then it is OK for wide wide
5994 -- character (out of range character literals are rejected).
5996 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5999 -- Always accept character literal for type Any_Character, which
6000 -- occurs in error situations and in comparisons of literals, both
6001 -- of which should accept all literals.
6003 elsif B_Typ = Any_Character then
6006 -- For Standard.Character or a type derived from it, check that the
6007 -- literal is in range.
6009 elsif Root_Type (B_Typ) = Standard_Character then
6010 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6014 -- For Standard.Wide_Character or a type derived from it, check that the
6015 -- literal is in range.
6017 elsif Root_Type (B_Typ) = Standard_Wide_Character then
6018 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6022 -- For Standard.Wide_Wide_Character or a type derived from it, we
6023 -- know the literal is in range, since the parser checked!
6025 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6028 -- If the entity is already set, this has already been resolved in a
6029 -- generic context, or comes from expansion. Nothing else to do.
6031 elsif Present (Entity (N)) then
6034 -- Otherwise we have a user defined character type, and we can use the
6035 -- standard visibility mechanisms to locate the referenced entity.
6038 C := Current_Entity (N);
6039 while Present (C) loop
6040 if Etype (C) = B_Typ then
6041 Set_Entity_With_Style_Check (N, C);
6042 Generate_Reference (C, N);
6050 -- If we fall through, then the literal does not match any of the
6051 -- entries of the enumeration type. This isn't just a constraint error
6052 -- situation, it is an illegality (see RM 4.2).
6055 ("character not defined for }", N, First_Subtype (B_Typ));
6056 end Resolve_Character_Literal;
6058 ---------------------------
6059 -- Resolve_Comparison_Op --
6060 ---------------------------
6062 -- Context requires a boolean type, and plays no role in resolution.
6063 -- Processing identical to that for equality operators. The result type is
6064 -- the base type, which matters when pathological subtypes of booleans with
6065 -- limited ranges are used.
6067 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6068 L : constant Node_Id := Left_Opnd (N);
6069 R : constant Node_Id := Right_Opnd (N);
6073 -- If this is an intrinsic operation which is not predefined, use the
6074 -- types of its declared arguments to resolve the possibly overloaded
6075 -- operands. Otherwise the operands are unambiguous and specify the
6078 if Scope (Entity (N)) /= Standard_Standard then
6079 T := Etype (First_Entity (Entity (N)));
6082 T := Find_Unique_Type (L, R);
6084 if T = Any_Fixed then
6085 T := Unique_Fixed_Point_Type (L);
6089 Set_Etype (N, Base_Type (Typ));
6090 Generate_Reference (T, N, ' ');
6092 -- Skip remaining processing if already set to Any_Type
6094 if T = Any_Type then
6098 -- Deal with other error cases
6100 if T = Any_String or else
6101 T = Any_Composite or else
6104 if T = Any_Character then
6105 Ambiguous_Character (L);
6107 Error_Msg_N ("ambiguous operands for comparison", N);
6110 Set_Etype (N, Any_Type);
6114 -- Resolve the operands if types OK
6118 Check_Unset_Reference (L);
6119 Check_Unset_Reference (R);
6120 Generate_Operator_Reference (N, T);
6121 Check_Low_Bound_Tested (N);
6123 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6124 -- types or array types except String.
6126 if Is_Boolean_Type (T) then
6127 Check_SPARK_Restriction
6128 ("comparison is not defined on Boolean type", N);
6130 elsif Is_Array_Type (T)
6131 and then Base_Type (T) /= Standard_String
6133 Check_SPARK_Restriction
6134 ("comparison is not defined on array types other than String", N);
6137 -- Check comparison on unordered enumeration
6139 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
6140 Error_Msg_N ("comparison on unordered enumeration type?U?", N);
6143 -- Evaluate the relation (note we do this after the above check since
6144 -- this Eval call may change N to True/False.
6146 Analyze_Dimension (N);
6147 Eval_Relational_Op (N);
6148 end Resolve_Comparison_Op;
6150 -----------------------------------------
6151 -- Resolve_Discrete_Subtype_Indication --
6152 -----------------------------------------
6154 procedure Resolve_Discrete_Subtype_Indication
6162 Analyze (Subtype_Mark (N));
6163 S := Entity (Subtype_Mark (N));
6165 if Nkind (Constraint (N)) /= N_Range_Constraint then
6166 Error_Msg_N ("expect range constraint for discrete type", N);
6167 Set_Etype (N, Any_Type);
6170 R := Range_Expression (Constraint (N));
6178 if Base_Type (S) /= Base_Type (Typ) then
6180 ("expect subtype of }", N, First_Subtype (Typ));
6182 -- Rewrite the constraint as a range of Typ
6183 -- to allow compilation to proceed further.
6186 Rewrite (Low_Bound (R),
6187 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6188 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6189 Attribute_Name => Name_First));
6190 Rewrite (High_Bound (R),
6191 Make_Attribute_Reference (Sloc (High_Bound (R)),
6192 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6193 Attribute_Name => Name_First));
6197 Set_Etype (N, Etype (R));
6199 -- Additionally, we must check that the bounds are compatible
6200 -- with the given subtype, which might be different from the
6201 -- type of the context.
6203 Apply_Range_Check (R, S);
6205 -- ??? If the above check statically detects a Constraint_Error
6206 -- it replaces the offending bound(s) of the range R with a
6207 -- Constraint_Error node. When the itype which uses these bounds
6208 -- is frozen the resulting call to Duplicate_Subexpr generates
6209 -- a new temporary for the bounds.
6211 -- Unfortunately there are other itypes that are also made depend
6212 -- on these bounds, so when Duplicate_Subexpr is called they get
6213 -- a forward reference to the newly created temporaries and Gigi
6214 -- aborts on such forward references. This is probably sign of a
6215 -- more fundamental problem somewhere else in either the order of
6216 -- itype freezing or the way certain itypes are constructed.
6218 -- To get around this problem we call Remove_Side_Effects right
6219 -- away if either bounds of R are a Constraint_Error.
6222 L : constant Node_Id := Low_Bound (R);
6223 H : constant Node_Id := High_Bound (R);
6226 if Nkind (L) = N_Raise_Constraint_Error then
6227 Remove_Side_Effects (L);
6230 if Nkind (H) = N_Raise_Constraint_Error then
6231 Remove_Side_Effects (H);
6235 Check_Unset_Reference (Low_Bound (R));
6236 Check_Unset_Reference (High_Bound (R));
6239 end Resolve_Discrete_Subtype_Indication;
6241 -------------------------
6242 -- Resolve_Entity_Name --
6243 -------------------------
6245 -- Used to resolve identifiers and expanded names
6247 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6248 E : constant Entity_Id := Entity (N);
6251 -- If garbage from errors, set to Any_Type and return
6253 if No (E) and then Total_Errors_Detected /= 0 then
6254 Set_Etype (N, Any_Type);
6258 -- Replace named numbers by corresponding literals. Note that this is
6259 -- the one case where Resolve_Entity_Name must reset the Etype, since
6260 -- it is currently marked as universal.
6262 if Ekind (E) = E_Named_Integer then
6264 Eval_Named_Integer (N);
6266 elsif Ekind (E) = E_Named_Real then
6268 Eval_Named_Real (N);
6270 -- For enumeration literals, we need to make sure that a proper style
6271 -- check is done, since such literals are overloaded, and thus we did
6272 -- not do a style check during the first phase of analysis.
6274 elsif Ekind (E) = E_Enumeration_Literal then
6275 Set_Entity_With_Style_Check (N, E);
6276 Eval_Entity_Name (N);
6278 -- Case of subtype name appearing as an operand in expression
6280 elsif Is_Type (E) then
6282 -- Allow use of subtype if it is a concurrent type where we are
6283 -- currently inside the body. This will eventually be expanded into a
6284 -- call to Self (for tasks) or _object (for protected objects). Any
6285 -- other use of a subtype is invalid.
6287 if Is_Concurrent_Type (E)
6288 and then In_Open_Scopes (E)
6292 -- Any other use is an error
6296 ("invalid use of subtype mark in expression or call", N);
6299 -- Check discriminant use if entity is discriminant in current scope,
6300 -- i.e. discriminant of record or concurrent type currently being
6301 -- analyzed. Uses in corresponding body are unrestricted.
6303 elsif Ekind (E) = E_Discriminant
6304 and then Scope (E) = Current_Scope
6305 and then not Has_Completion (Current_Scope)
6307 Check_Discriminant_Use (N);
6309 -- A parameterless generic function cannot appear in a context that
6310 -- requires resolution.
6312 elsif Ekind (E) = E_Generic_Function then
6313 Error_Msg_N ("illegal use of generic function", N);
6315 elsif Ekind (E) = E_Out_Parameter
6316 and then Ada_Version = Ada_83
6317 and then (Nkind (Parent (N)) in N_Op
6318 or else (Nkind (Parent (N)) = N_Assignment_Statement
6319 and then N = Expression (Parent (N)))
6320 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6322 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6324 -- In all other cases, just do the possible static evaluation
6327 -- A deferred constant that appears in an expression must have a
6328 -- completion, unless it has been removed by in-place expansion of
6331 if Ekind (E) = E_Constant
6332 and then Comes_From_Source (E)
6333 and then No (Constant_Value (E))
6334 and then Is_Frozen (Etype (E))
6335 and then not In_Spec_Expression
6336 and then not Is_Imported (E)
6338 if No_Initialization (Parent (E))
6339 or else (Present (Full_View (E))
6340 and then No_Initialization (Parent (Full_View (E))))
6345 "deferred constant is frozen before completion", N);
6349 Eval_Entity_Name (N);
6351 end Resolve_Entity_Name;
6357 procedure Resolve_Entry (Entry_Name : Node_Id) is
6358 Loc : constant Source_Ptr := Sloc (Entry_Name);
6366 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6367 -- If the bounds of the entry family being called depend on task
6368 -- discriminants, build a new index subtype where a discriminant is
6369 -- replaced with the value of the discriminant of the target task.
6370 -- The target task is the prefix of the entry name in the call.
6372 -----------------------
6373 -- Actual_Index_Type --
6374 -----------------------
6376 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6377 Typ : constant Entity_Id := Entry_Index_Type (E);
6378 Tsk : constant Entity_Id := Scope (E);
6379 Lo : constant Node_Id := Type_Low_Bound (Typ);
6380 Hi : constant Node_Id := Type_High_Bound (Typ);
6383 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6384 -- If the bound is given by a discriminant, replace with a reference
6385 -- to the discriminant of the same name in the target task. If the
6386 -- entry name is the target of a requeue statement and the entry is
6387 -- in the current protected object, the bound to be used is the
6388 -- discriminal of the object (see Apply_Range_Checks for details of
6389 -- the transformation).
6391 -----------------------------
6392 -- Actual_Discriminant_Ref --
6393 -----------------------------
6395 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6396 Typ : constant Entity_Id := Etype (Bound);
6400 Remove_Side_Effects (Bound);
6402 if not Is_Entity_Name (Bound)
6403 or else Ekind (Entity (Bound)) /= E_Discriminant
6407 elsif Is_Protected_Type (Tsk)
6408 and then In_Open_Scopes (Tsk)
6409 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6411 -- Note: here Bound denotes a discriminant of the corresponding
6412 -- record type tskV, whose discriminal is a formal of the
6413 -- init-proc tskVIP. What we want is the body discriminal,
6414 -- which is associated to the discriminant of the original
6415 -- concurrent type tsk.
6417 return New_Occurrence_Of
6418 (Find_Body_Discriminal (Entity (Bound)), Loc);
6422 Make_Selected_Component (Loc,
6423 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6424 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6429 end Actual_Discriminant_Ref;
6431 -- Start of processing for Actual_Index_Type
6434 if not Has_Discriminants (Tsk)
6435 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6437 return Entry_Index_Type (E);
6440 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6441 Set_Etype (New_T, Base_Type (Typ));
6442 Set_Size_Info (New_T, Typ);
6443 Set_RM_Size (New_T, RM_Size (Typ));
6444 Set_Scalar_Range (New_T,
6445 Make_Range (Sloc (Entry_Name),
6446 Low_Bound => Actual_Discriminant_Ref (Lo),
6447 High_Bound => Actual_Discriminant_Ref (Hi)));
6451 end Actual_Index_Type;
6453 -- Start of processing of Resolve_Entry
6456 -- Find name of entry being called, and resolve prefix of name with its
6457 -- own type. The prefix can be overloaded, and the name and signature of
6458 -- the entry must be taken into account.
6460 if Nkind (Entry_Name) = N_Indexed_Component then
6462 -- Case of dealing with entry family within the current tasks
6464 E_Name := Prefix (Entry_Name);
6467 E_Name := Entry_Name;
6470 if Is_Entity_Name (E_Name) then
6472 -- Entry call to an entry (or entry family) in the current task. This
6473 -- is legal even though the task will deadlock. Rewrite as call to
6476 -- This can also be a call to an entry in an enclosing task. If this
6477 -- is a single task, we have to retrieve its name, because the scope
6478 -- of the entry is the task type, not the object. If the enclosing
6479 -- task is a task type, the identity of the task is given by its own
6482 -- Finally this can be a requeue on an entry of the same task or
6483 -- protected object.
6485 S := Scope (Entity (E_Name));
6487 for J in reverse 0 .. Scope_Stack.Last loop
6488 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6489 and then not Comes_From_Source (S)
6491 -- S is an enclosing task or protected object. The concurrent
6492 -- declaration has been converted into a type declaration, and
6493 -- the object itself has an object declaration that follows
6494 -- the type in the same declarative part.
6496 Tsk := Next_Entity (S);
6497 while Etype (Tsk) /= S loop
6504 elsif S = Scope_Stack.Table (J).Entity then
6506 -- Call to current task. Will be transformed into call to Self
6514 Make_Selected_Component (Loc,
6515 Prefix => New_Occurrence_Of (S, Loc),
6517 New_Occurrence_Of (Entity (E_Name), Loc));
6518 Rewrite (E_Name, New_N);
6521 elsif Nkind (Entry_Name) = N_Selected_Component
6522 and then Is_Overloaded (Prefix (Entry_Name))
6524 -- Use the entry name (which must be unique at this point) to find
6525 -- the prefix that returns the corresponding task/protected type.
6528 Pref : constant Node_Id := Prefix (Entry_Name);
6529 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6534 Get_First_Interp (Pref, I, It);
6535 while Present (It.Typ) loop
6536 if Scope (Ent) = It.Typ then
6537 Set_Etype (Pref, It.Typ);
6541 Get_Next_Interp (I, It);
6546 if Nkind (Entry_Name) = N_Selected_Component then
6547 Resolve (Prefix (Entry_Name));
6549 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6550 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6551 Resolve (Prefix (Prefix (Entry_Name)));
6552 Index := First (Expressions (Entry_Name));
6553 Resolve (Index, Entry_Index_Type (Nam));
6555 -- Up to this point the expression could have been the actual in a
6556 -- simple entry call, and be given by a named association.
6558 if Nkind (Index) = N_Parameter_Association then
6559 Error_Msg_N ("expect expression for entry index", Index);
6561 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6566 ------------------------
6567 -- Resolve_Entry_Call --
6568 ------------------------
6570 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6571 Entry_Name : constant Node_Id := Name (N);
6572 Loc : constant Source_Ptr := Sloc (Entry_Name);
6574 First_Named : Node_Id;
6581 -- We kill all checks here, because it does not seem worth the effort to
6582 -- do anything better, an entry call is a big operation.
6586 -- Processing of the name is similar for entry calls and protected
6587 -- operation calls. Once the entity is determined, we can complete
6588 -- the resolution of the actuals.
6590 -- The selector may be overloaded, in the case of a protected object
6591 -- with overloaded functions. The type of the context is used for
6594 if Nkind (Entry_Name) = N_Selected_Component
6595 and then Is_Overloaded (Selector_Name (Entry_Name))
6596 and then Typ /= Standard_Void_Type
6603 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6604 while Present (It.Typ) loop
6605 if Covers (Typ, It.Typ) then
6606 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6607 Set_Etype (Entry_Name, It.Typ);
6609 Generate_Reference (It.Typ, N, ' ');
6612 Get_Next_Interp (I, It);
6617 Resolve_Entry (Entry_Name);
6619 if Nkind (Entry_Name) = N_Selected_Component then
6621 -- Simple entry call
6623 Nam := Entity (Selector_Name (Entry_Name));
6624 Obj := Prefix (Entry_Name);
6625 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6627 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6629 -- Call to member of entry family
6631 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6632 Obj := Prefix (Prefix (Entry_Name));
6633 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6636 -- We cannot in general check the maximum depth of protected entry calls
6637 -- at compile time. But we can tell that any protected entry call at all
6638 -- violates a specified nesting depth of zero.
6640 if Is_Protected_Type (Scope (Nam)) then
6641 Check_Restriction (Max_Entry_Queue_Length, N);
6644 -- Use context type to disambiguate a protected function that can be
6645 -- called without actuals and that returns an array type, and where the
6646 -- argument list may be an indexing of the returned value.
6648 if Ekind (Nam) = E_Function
6649 and then Needs_No_Actuals (Nam)
6650 and then Present (Parameter_Associations (N))
6652 ((Is_Array_Type (Etype (Nam))
6653 and then Covers (Typ, Component_Type (Etype (Nam))))
6655 or else (Is_Access_Type (Etype (Nam))
6656 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6660 Component_Type (Designated_Type (Etype (Nam))))))
6663 Index_Node : Node_Id;
6667 Make_Indexed_Component (Loc,
6669 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6670 Expressions => Parameter_Associations (N));
6672 -- Since we are correcting a node classification error made by the
6673 -- parser, we call Replace rather than Rewrite.
6675 Replace (N, Index_Node);
6676 Set_Etype (Prefix (N), Etype (Nam));
6678 Resolve_Indexed_Component (N, Typ);
6683 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6684 and then Present (PPC_Wrapper (Nam))
6685 and then Current_Scope /= PPC_Wrapper (Nam)
6687 -- Rewrite as call to the precondition wrapper, adding the task
6688 -- object to the list of actuals. If the call is to a member of an
6689 -- entry family, include the index as well.
6693 New_Actuals : List_Id;
6696 New_Actuals := New_List (Obj);
6698 if Nkind (Entry_Name) = N_Indexed_Component then
6699 Append_To (New_Actuals,
6700 New_Copy_Tree (First (Expressions (Entry_Name))));
6703 Append_List (Parameter_Associations (N), New_Actuals);
6705 Make_Procedure_Call_Statement (Loc,
6707 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6708 Parameter_Associations => New_Actuals);
6709 Rewrite (N, New_Call);
6710 Analyze_And_Resolve (N);
6715 -- The operation name may have been overloaded. Order the actuals
6716 -- according to the formals of the resolved entity, and set the return
6717 -- type to that of the operation.
6720 Normalize_Actuals (N, Nam, False, Norm_OK);
6721 pragma Assert (Norm_OK);
6722 Set_Etype (N, Etype (Nam));
6725 Resolve_Actuals (N, Nam);
6726 Check_Internal_Protected_Use (N, Nam);
6728 -- Create a call reference to the entry
6730 Generate_Reference (Nam, Entry_Name, 's');
6732 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6733 Check_Potentially_Blocking_Operation (N);
6736 -- Verify that a procedure call cannot masquerade as an entry
6737 -- call where an entry call is expected.
6739 if Ekind (Nam) = E_Procedure then
6740 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6741 and then N = Entry_Call_Statement (Parent (N))
6743 Error_Msg_N ("entry call required in select statement", N);
6745 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6746 and then N = Triggering_Statement (Parent (N))
6748 Error_Msg_N ("triggering statement cannot be procedure call", N);
6750 elsif Ekind (Scope (Nam)) = E_Task_Type
6751 and then not In_Open_Scopes (Scope (Nam))
6753 Error_Msg_N ("task has no entry with this name", Entry_Name);
6757 -- After resolution, entry calls and protected procedure calls are
6758 -- changed into entry calls, for expansion. The structure of the node
6759 -- does not change, so it can safely be done in place. Protected
6760 -- function calls must keep their structure because they are
6763 if Ekind (Nam) /= E_Function then
6765 -- A protected operation that is not a function may modify the
6766 -- corresponding object, and cannot apply to a constant. If this
6767 -- is an internal call, the prefix is the type itself.
6769 if Is_Protected_Type (Scope (Nam))
6770 and then not Is_Variable (Obj)
6771 and then (not Is_Entity_Name (Obj)
6772 or else not Is_Type (Entity (Obj)))
6775 ("prefix of protected procedure or entry call must be variable",
6779 Actuals := Parameter_Associations (N);
6780 First_Named := First_Named_Actual (N);
6783 Make_Entry_Call_Statement (Loc,
6785 Parameter_Associations => Actuals));
6787 Set_First_Named_Actual (N, First_Named);
6788 Set_Analyzed (N, True);
6790 -- Protected functions can return on the secondary stack, in which
6791 -- case we must trigger the transient scope mechanism.
6793 elsif Full_Expander_Active
6794 and then Requires_Transient_Scope (Etype (Nam))
6796 Establish_Transient_Scope (N, Sec_Stack => True);
6798 end Resolve_Entry_Call;
6800 -------------------------
6801 -- Resolve_Equality_Op --
6802 -------------------------
6804 -- Both arguments must have the same type, and the boolean context does
6805 -- not participate in the resolution. The first pass verifies that the
6806 -- interpretation is not ambiguous, and the type of the left argument is
6807 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6808 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6809 -- though they carry a single (universal) type. Diagnose this case here.
6811 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6812 L : constant Node_Id := Left_Opnd (N);
6813 R : constant Node_Id := Right_Opnd (N);
6814 T : Entity_Id := Find_Unique_Type (L, R);
6816 procedure Check_If_Expression (Cond : Node_Id);
6817 -- The resolution rule for if expressions requires that each such must
6818 -- have a unique type. This means that if several dependent expressions
6819 -- are of a non-null anonymous access type, and the context does not
6820 -- impose an expected type (as can be the case in an equality operation)
6821 -- the expression must be rejected.
6823 function Find_Unique_Access_Type return Entity_Id;
6824 -- In the case of allocators and access attributes, the context must
6825 -- provide an indication of the specific access type to be used. If
6826 -- one operand is of such a "generic" access type, check whether there
6827 -- is a specific visible access type that has the same designated type.
6828 -- This is semantically dubious, and of no interest to any real code,
6829 -- but c48008a makes it all worthwhile.
6831 -------------------------
6832 -- Check_If_Expression --
6833 -------------------------
6835 procedure Check_If_Expression (Cond : Node_Id) is
6836 Then_Expr : Node_Id;
6837 Else_Expr : Node_Id;
6840 if Nkind (Cond) = N_If_Expression then
6841 Then_Expr := Next (First (Expressions (Cond)));
6842 Else_Expr := Next (Then_Expr);
6844 if Nkind (Then_Expr) /= N_Null
6845 and then Nkind (Else_Expr) /= N_Null
6847 Error_Msg_N ("cannot determine type of if expression", Cond);
6850 end Check_If_Expression;
6852 -----------------------------
6853 -- Find_Unique_Access_Type --
6854 -----------------------------
6856 function Find_Unique_Access_Type return Entity_Id is
6862 if Ekind (Etype (R)) = E_Allocator_Type
6863 or else Ekind (Etype (R)) = E_Access_Attribute_Type
6865 Acc := Designated_Type (Etype (R));
6867 elsif Ekind (Etype (L)) = E_Allocator_Type
6868 or else Ekind (Etype (L)) = E_Access_Attribute_Type
6870 Acc := Designated_Type (Etype (L));
6876 while S /= Standard_Standard loop
6877 E := First_Entity (S);
6878 while Present (E) loop
6880 and then Is_Access_Type (E)
6881 and then Ekind (E) /= E_Allocator_Type
6882 and then Designated_Type (E) = Base_Type (Acc)
6894 end Find_Unique_Access_Type;
6896 -- Start of processing for Resolve_Equality_Op
6899 Set_Etype (N, Base_Type (Typ));
6900 Generate_Reference (T, N, ' ');
6902 if T = Any_Fixed then
6903 T := Unique_Fixed_Point_Type (L);
6906 if T /= Any_Type then
6907 if T = Any_String or else
6908 T = Any_Composite or else
6911 if T = Any_Character then
6912 Ambiguous_Character (L);
6914 Error_Msg_N ("ambiguous operands for equality", N);
6917 Set_Etype (N, Any_Type);
6920 elsif T = Any_Access
6921 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6923 T := Find_Unique_Access_Type;
6926 Error_Msg_N ("ambiguous operands for equality", N);
6927 Set_Etype (N, Any_Type);
6931 -- If expressions must have a single type, and if the context does
6932 -- not impose one the dependent expressions cannot be anonymous
6935 -- Why no similar processing for case expressions???
6937 elsif Ada_Version >= Ada_2012
6938 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6939 E_Anonymous_Access_Subprogram_Type)
6940 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6941 E_Anonymous_Access_Subprogram_Type)
6943 Check_If_Expression (L);
6944 Check_If_Expression (R);
6950 -- In SPARK, equality operators = and /= for array types other than
6951 -- String are only defined when, for each index position, the
6952 -- operands have equal static bounds.
6954 if Is_Array_Type (T) then
6956 -- Protect call to Matching_Static_Array_Bounds to avoid costly
6957 -- operation if not needed.
6959 if Restriction_Check_Required (SPARK)
6960 and then Base_Type (T) /= Standard_String
6961 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6962 and then Etype (L) /= Any_Composite -- or else L in error
6963 and then Etype (R) /= Any_Composite -- or else R in error
6964 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6966 Check_SPARK_Restriction
6967 ("array types should have matching static bounds", N);
6971 -- If the unique type is a class-wide type then it will be expanded
6972 -- into a dispatching call to the predefined primitive. Therefore we
6973 -- check here for potential violation of such restriction.
6975 if Is_Class_Wide_Type (T) then
6976 Check_Restriction (No_Dispatching_Calls, N);
6979 if Warn_On_Redundant_Constructs
6980 and then Comes_From_Source (N)
6981 and then Is_Entity_Name (R)
6982 and then Entity (R) = Standard_True
6983 and then Comes_From_Source (R)
6985 Error_Msg_N -- CODEFIX
6986 ("?r?comparison with True is redundant!", R);
6989 Check_Unset_Reference (L);
6990 Check_Unset_Reference (R);
6991 Generate_Operator_Reference (N, T);
6992 Check_Low_Bound_Tested (N);
6994 -- If this is an inequality, it may be the implicit inequality
6995 -- created for a user-defined operation, in which case the corres-
6996 -- ponding equality operation is not intrinsic, and the operation
6997 -- cannot be constant-folded. Else fold.
6999 if Nkind (N) = N_Op_Eq
7000 or else Comes_From_Source (Entity (N))
7001 or else Ekind (Entity (N)) = E_Operator
7002 or else Is_Intrinsic_Subprogram
7003 (Corresponding_Equality (Entity (N)))
7005 Analyze_Dimension (N);
7006 Eval_Relational_Op (N);
7008 elsif Nkind (N) = N_Op_Ne
7009 and then Is_Abstract_Subprogram (Entity (N))
7011 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7014 -- Ada 2005: If one operand is an anonymous access type, convert the
7015 -- other operand to it, to ensure that the underlying types match in
7016 -- the back-end. Same for access_to_subprogram, and the conversion
7017 -- verifies that the types are subtype conformant.
7019 -- We apply the same conversion in the case one of the operands is a
7020 -- private subtype of the type of the other.
7022 -- Why the Expander_Active test here ???
7024 if Full_Expander_Active
7026 (Ekind_In (T, E_Anonymous_Access_Type,
7027 E_Anonymous_Access_Subprogram_Type)
7028 or else Is_Private_Type (T))
7030 if Etype (L) /= T then
7032 Make_Unchecked_Type_Conversion (Sloc (L),
7033 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7034 Expression => Relocate_Node (L)));
7035 Analyze_And_Resolve (L, T);
7038 if (Etype (R)) /= T then
7040 Make_Unchecked_Type_Conversion (Sloc (R),
7041 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7042 Expression => Relocate_Node (R)));
7043 Analyze_And_Resolve (R, T);
7047 end Resolve_Equality_Op;
7049 ----------------------------------
7050 -- Resolve_Explicit_Dereference --
7051 ----------------------------------
7053 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
7054 Loc : constant Source_Ptr := Sloc (N);
7056 P : constant Node_Id := Prefix (N);
7059 -- The candidate prefix type, if overloaded
7065 Check_Fully_Declared_Prefix (Typ, P);
7068 if Is_Overloaded (P) then
7070 -- Use the context type to select the prefix that has the correct
7071 -- designated type. Keep the first match, which will be the inner-
7074 Get_First_Interp (P, I, It);
7076 while Present (It.Typ) loop
7077 if Is_Access_Type (It.Typ)
7078 and then Covers (Typ, Designated_Type (It.Typ))
7084 -- Remove access types that do not match, but preserve access
7085 -- to subprogram interpretations, in case a further dereference
7086 -- is needed (see below).
7088 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
7092 Get_Next_Interp (I, It);
7095 if Present (P_Typ) then
7097 Set_Etype (N, Designated_Type (P_Typ));
7100 -- If no interpretation covers the designated type of the prefix,
7101 -- this is the pathological case where not all implementations of
7102 -- the prefix allow the interpretation of the node as a call. Now
7103 -- that the expected type is known, Remove other interpretations
7104 -- from prefix, rewrite it as a call, and resolve again, so that
7105 -- the proper call node is generated.
7107 Get_First_Interp (P, I, It);
7108 while Present (It.Typ) loop
7109 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
7113 Get_Next_Interp (I, It);
7117 Make_Function_Call (Loc,
7119 Make_Explicit_Dereference (Loc,
7121 Parameter_Associations => New_List);
7123 Save_Interps (N, New_N);
7125 Analyze_And_Resolve (N, Typ);
7129 -- If not overloaded, resolve P with its own type
7135 if Is_Access_Type (Etype (P)) then
7136 Apply_Access_Check (N);
7139 -- If the designated type is a packed unconstrained array type, and the
7140 -- explicit dereference is not in the context of an attribute reference,
7141 -- then we must compute and set the actual subtype, since it is needed
7142 -- by Gigi. The reason we exclude the attribute case is that this is
7143 -- handled fine by Gigi, and in fact we use such attributes to build the
7144 -- actual subtype. We also exclude generated code (which builds actual
7145 -- subtypes directly if they are needed).
7147 if Is_Array_Type (Etype (N))
7148 and then Is_Packed (Etype (N))
7149 and then not Is_Constrained (Etype (N))
7150 and then Nkind (Parent (N)) /= N_Attribute_Reference
7151 and then Comes_From_Source (N)
7153 Set_Etype (N, Get_Actual_Subtype (N));
7156 -- Note: No Eval processing is required for an explicit dereference,
7157 -- because such a name can never be static.
7159 end Resolve_Explicit_Dereference;
7161 -------------------------------------
7162 -- Resolve_Expression_With_Actions --
7163 -------------------------------------
7165 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7168 end Resolve_Expression_With_Actions;
7170 ---------------------------
7171 -- Resolve_If_Expression --
7172 ---------------------------
7174 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
7175 Condition : constant Node_Id := First (Expressions (N));
7176 Then_Expr : constant Node_Id := Next (Condition);
7177 Else_Expr : Node_Id := Next (Then_Expr);
7178 Else_Typ : Entity_Id;
7179 Then_Typ : Entity_Id;
7182 Resolve (Condition, Any_Boolean);
7183 Resolve (Then_Expr, Typ);
7184 Then_Typ := Etype (Then_Expr);
7186 -- When the "then" expression is of a scalar subtype different from the
7187 -- result subtype, then insert a conversion to ensure the generation of
7188 -- a constraint check. The same is done for the else part below, again
7189 -- comparing subtypes rather than base types.
7191 if Is_Scalar_Type (Then_Typ)
7192 and then Then_Typ /= Typ
7194 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
7195 Analyze_And_Resolve (Then_Expr, Typ);
7198 -- If ELSE expression present, just resolve using the determined type
7200 if Present (Else_Expr) then
7201 Resolve (Else_Expr, Typ);
7202 Else_Typ := Etype (Else_Expr);
7204 if Is_Scalar_Type (Else_Typ)
7205 and then Else_Typ /= Typ
7207 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
7208 Analyze_And_Resolve (Else_Expr, Typ);
7211 -- If no ELSE expression is present, root type must be Standard.Boolean
7212 -- and we provide a Standard.True result converted to the appropriate
7213 -- Boolean type (in case it is a derived boolean type).
7215 elsif Root_Type (Typ) = Standard_Boolean then
7217 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
7218 Analyze_And_Resolve (Else_Expr, Typ);
7219 Append_To (Expressions (N), Else_Expr);
7222 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
7223 Append_To (Expressions (N), Error);
7227 Eval_If_Expression (N);
7228 end Resolve_If_Expression;
7230 -------------------------------
7231 -- Resolve_Indexed_Component --
7232 -------------------------------
7234 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7235 Name : constant Node_Id := Prefix (N);
7237 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7241 if Is_Overloaded (Name) then
7243 -- Use the context type to select the prefix that yields the correct
7249 I1 : Interp_Index := 0;
7250 P : constant Node_Id := Prefix (N);
7251 Found : Boolean := False;
7254 Get_First_Interp (P, I, It);
7255 while Present (It.Typ) loop
7256 if (Is_Array_Type (It.Typ)
7257 and then Covers (Typ, Component_Type (It.Typ)))
7258 or else (Is_Access_Type (It.Typ)
7259 and then Is_Array_Type (Designated_Type (It.Typ))
7263 Component_Type (Designated_Type (It.Typ))))
7266 It := Disambiguate (P, I1, I, Any_Type);
7268 if It = No_Interp then
7269 Error_Msg_N ("ambiguous prefix for indexing", N);
7275 Array_Type := It.Typ;
7281 Array_Type := It.Typ;
7286 Get_Next_Interp (I, It);
7291 Array_Type := Etype (Name);
7294 Resolve (Name, Array_Type);
7295 Array_Type := Get_Actual_Subtype_If_Available (Name);
7297 -- If prefix is access type, dereference to get real array type.
7298 -- Note: we do not apply an access check because the expander always
7299 -- introduces an explicit dereference, and the check will happen there.
7301 if Is_Access_Type (Array_Type) then
7302 Array_Type := Designated_Type (Array_Type);
7305 -- If name was overloaded, set component type correctly now
7306 -- If a misplaced call to an entry family (which has no index types)
7307 -- return. Error will be diagnosed from calling context.
7309 if Is_Array_Type (Array_Type) then
7310 Set_Etype (N, Component_Type (Array_Type));
7315 Index := First_Index (Array_Type);
7316 Expr := First (Expressions (N));
7318 -- The prefix may have resolved to a string literal, in which case its
7319 -- etype has a special representation. This is only possible currently
7320 -- if the prefix is a static concatenation, written in functional
7323 if Ekind (Array_Type) = E_String_Literal_Subtype then
7324 Resolve (Expr, Standard_Positive);
7327 while Present (Index) and Present (Expr) loop
7328 Resolve (Expr, Etype (Index));
7329 Check_Unset_Reference (Expr);
7331 if Is_Scalar_Type (Etype (Expr)) then
7332 Apply_Scalar_Range_Check (Expr, Etype (Index));
7334 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7342 Analyze_Dimension (N);
7344 -- Do not generate the warning on suspicious index if we are analyzing
7345 -- package Ada.Tags; otherwise we will report the warning with the
7346 -- Prims_Ptr field of the dispatch table.
7348 if Scope (Etype (Prefix (N))) = Standard_Standard
7350 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7353 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7354 Eval_Indexed_Component (N);
7357 -- If the array type is atomic, and is packed, and we are in a left side
7358 -- context, then this is worth a warning, since we have a situation
7359 -- where the access to the component may cause extra read/writes of
7360 -- the atomic array object, which could be considered unexpected.
7362 if Nkind (N) = N_Indexed_Component
7363 and then (Is_Atomic (Array_Type)
7364 or else (Is_Entity_Name (Prefix (N))
7365 and then Is_Atomic (Entity (Prefix (N)))))
7366 and then Is_Bit_Packed_Array (Array_Type)
7369 Error_Msg_N ("??assignment to component of packed atomic array",
7371 Error_Msg_N ("??\may cause unexpected accesses to atomic object",
7374 end Resolve_Indexed_Component;
7376 -----------------------------
7377 -- Resolve_Integer_Literal --
7378 -----------------------------
7380 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7383 Eval_Integer_Literal (N);
7384 end Resolve_Integer_Literal;
7386 --------------------------------
7387 -- Resolve_Intrinsic_Operator --
7388 --------------------------------
7390 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7391 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7393 Orig_Op : constant Entity_Id := Entity (N);
7397 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7398 -- If the operand is a literal, it cannot be the expression in a
7399 -- conversion. Use a qualified expression instead.
7401 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7402 Loc : constant Source_Ptr := Sloc (Opnd);
7405 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7407 Make_Qualified_Expression (Loc,
7408 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7409 Expression => Relocate_Node (Opnd));
7413 Res := Unchecked_Convert_To (Btyp, Opnd);
7417 end Convert_Operand;
7419 -- Start of processing for Resolve_Intrinsic_Operator
7422 -- We must preserve the original entity in a generic setting, so that
7423 -- the legality of the operation can be verified in an instance.
7425 if not Full_Expander_Active then
7430 while Scope (Op) /= Standard_Standard loop
7432 pragma Assert (Present (Op));
7436 Set_Is_Overloaded (N, False);
7438 -- If the result or operand types are private, rewrite with unchecked
7439 -- conversions on the operands and the result, to expose the proper
7440 -- underlying numeric type.
7442 if Is_Private_Type (Typ)
7443 or else Is_Private_Type (Etype (Left_Opnd (N)))
7444 or else Is_Private_Type (Etype (Right_Opnd (N)))
7446 Arg1 := Convert_Operand (Left_Opnd (N));
7447 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7448 -- What on earth is this commented out fragment of code???
7450 if Nkind (N) = N_Op_Expon then
7451 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7453 Arg2 := Convert_Operand (Right_Opnd (N));
7456 if Nkind (Arg1) = N_Type_Conversion then
7457 Save_Interps (Left_Opnd (N), Expression (Arg1));
7460 if Nkind (Arg2) = N_Type_Conversion then
7461 Save_Interps (Right_Opnd (N), Expression (Arg2));
7464 Set_Left_Opnd (N, Arg1);
7465 Set_Right_Opnd (N, Arg2);
7467 Set_Etype (N, Btyp);
7468 Rewrite (N, Unchecked_Convert_To (Typ, N));
7471 elsif Typ /= Etype (Left_Opnd (N))
7472 or else Typ /= Etype (Right_Opnd (N))
7474 -- Add explicit conversion where needed, and save interpretations in
7475 -- case operands are overloaded. If the context is a VMS operation,
7476 -- assert that the conversion is legal (the operands have the proper
7477 -- types to select the VMS intrinsic). Note that in rare cases the
7478 -- VMS operators may be visible, but the default System is being used
7479 -- and Address is a private type.
7481 Arg1 := Convert_To (Typ, Left_Opnd (N));
7482 Arg2 := Convert_To (Typ, Right_Opnd (N));
7484 if Nkind (Arg1) = N_Type_Conversion then
7485 Save_Interps (Left_Opnd (N), Expression (Arg1));
7487 if Is_VMS_Operator (Orig_Op) then
7488 Set_Conversion_OK (Arg1);
7491 Save_Interps (Left_Opnd (N), Arg1);
7494 if Nkind (Arg2) = N_Type_Conversion then
7495 Save_Interps (Right_Opnd (N), Expression (Arg2));
7497 if Is_VMS_Operator (Orig_Op) then
7498 Set_Conversion_OK (Arg2);
7501 Save_Interps (Right_Opnd (N), Arg2);
7504 Rewrite (Left_Opnd (N), Arg1);
7505 Rewrite (Right_Opnd (N), Arg2);
7508 Resolve_Arithmetic_Op (N, Typ);
7511 Resolve_Arithmetic_Op (N, Typ);
7513 end Resolve_Intrinsic_Operator;
7515 --------------------------------------
7516 -- Resolve_Intrinsic_Unary_Operator --
7517 --------------------------------------
7519 procedure Resolve_Intrinsic_Unary_Operator
7523 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7529 while Scope (Op) /= Standard_Standard loop
7531 pragma Assert (Present (Op));
7536 if Is_Private_Type (Typ) then
7537 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7538 Save_Interps (Right_Opnd (N), Expression (Arg2));
7540 Set_Right_Opnd (N, Arg2);
7542 Set_Etype (N, Btyp);
7543 Rewrite (N, Unchecked_Convert_To (Typ, N));
7547 Resolve_Unary_Op (N, Typ);
7549 end Resolve_Intrinsic_Unary_Operator;
7551 ------------------------
7552 -- Resolve_Logical_Op --
7553 ------------------------
7555 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7559 Check_No_Direct_Boolean_Operators (N);
7561 -- Predefined operations on scalar types yield the base type. On the
7562 -- other hand, logical operations on arrays yield the type of the
7563 -- arguments (and the context).
7565 if Is_Array_Type (Typ) then
7568 B_Typ := Base_Type (Typ);
7571 -- OK if this is a VMS-specific intrinsic operation
7573 if Is_VMS_Operator (Entity (N)) then
7576 -- The following test is required because the operands of the operation
7577 -- may be literals, in which case the resulting type appears to be
7578 -- compatible with a signed integer type, when in fact it is compatible
7579 -- only with modular types. If the context itself is universal, the
7580 -- operation is illegal.
7582 elsif not Valid_Boolean_Arg (Typ) then
7583 Error_Msg_N ("invalid context for logical operation", N);
7584 Set_Etype (N, Any_Type);
7587 elsif Typ = Any_Modular then
7589 ("no modular type available in this context", N);
7590 Set_Etype (N, Any_Type);
7593 elsif Is_Modular_Integer_Type (Typ)
7594 and then Etype (Left_Opnd (N)) = Universal_Integer
7595 and then Etype (Right_Opnd (N)) = Universal_Integer
7597 Check_For_Visible_Operator (N, B_Typ);
7600 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
7601 -- is active and the result type is standard Boolean (do not mess with
7602 -- ops that return a nonstandard Boolean type, because something strange
7605 -- Note: you might expect this replacement to be done during expansion,
7606 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
7607 -- is used, no part of the right operand of an "and" or "or" operator
7608 -- should be executed if the left operand would short-circuit the
7609 -- evaluation of the corresponding "and then" or "or else". If we left
7610 -- the replacement to expansion time, then run-time checks associated
7611 -- with such operands would be evaluated unconditionally, due to being
7612 -- before the condition prior to the rewriting as short-circuit forms
7613 -- during expansion.
7615 if Short_Circuit_And_Or
7616 and then B_Typ = Standard_Boolean
7617 and then Nkind_In (N, N_Op_And, N_Op_Or)
7619 if Nkind (N) = N_Op_And then
7621 Make_And_Then (Sloc (N),
7622 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7623 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7624 Analyze_And_Resolve (N, B_Typ);
7626 -- Case of OR changed to OR ELSE
7630 Make_Or_Else (Sloc (N),
7631 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7632 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7633 Analyze_And_Resolve (N, B_Typ);
7636 -- Return now, since analysis of the rewritten ops will take care of
7637 -- other reference bookkeeping and expression folding.
7642 Resolve (Left_Opnd (N), B_Typ);
7643 Resolve (Right_Opnd (N), B_Typ);
7645 Check_Unset_Reference (Left_Opnd (N));
7646 Check_Unset_Reference (Right_Opnd (N));
7648 Set_Etype (N, B_Typ);
7649 Generate_Operator_Reference (N, B_Typ);
7650 Eval_Logical_Op (N);
7652 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7653 -- only when both operands have same static lower and higher bounds. Of
7654 -- course the types have to match, so only check if operands are
7655 -- compatible and the node itself has no errors.
7657 if Is_Array_Type (B_Typ)
7658 and then Nkind (N) in N_Binary_Op
7661 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7662 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7665 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7666 -- operation if not needed.
7668 if Restriction_Check_Required (SPARK)
7669 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7670 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7671 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7672 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7674 Check_SPARK_Restriction
7675 ("array types should have matching static bounds", N);
7680 Check_Function_Writable_Actuals (N);
7681 end Resolve_Logical_Op;
7683 ---------------------------
7684 -- Resolve_Membership_Op --
7685 ---------------------------
7687 -- The context can only be a boolean type, and does not determine the
7688 -- arguments. Arguments should be unambiguous, but the preference rule for
7689 -- universal types applies.
7691 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7692 pragma Warnings (Off, Typ);
7694 L : constant Node_Id := Left_Opnd (N);
7695 R : constant Node_Id := Right_Opnd (N);
7698 procedure Resolve_Set_Membership;
7699 -- Analysis has determined a unique type for the left operand. Use it to
7700 -- resolve the disjuncts.
7702 ----------------------------
7703 -- Resolve_Set_Membership --
7704 ----------------------------
7706 procedure Resolve_Set_Membership is
7708 Ltyp : constant Entity_Id := Etype (L);
7713 Alt := First (Alternatives (N));
7714 while Present (Alt) loop
7716 -- Alternative is an expression, a range
7717 -- or a subtype mark.
7719 if not Is_Entity_Name (Alt)
7720 or else not Is_Type (Entity (Alt))
7722 Resolve (Alt, Ltyp);
7728 -- Check for duplicates for discrete case
7730 if Is_Discrete_Type (Ltyp) then
7737 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
7741 -- Loop checking duplicates. This is quadratic, but giant sets
7742 -- are unlikely in this context so it's a reasonable choice.
7745 Alt := First (Alternatives (N));
7746 while Present (Alt) loop
7747 if Is_Static_Expression (Alt)
7748 and then (Nkind_In (Alt, N_Integer_Literal,
7749 N_Character_Literal)
7750 or else Nkind (Alt) in N_Has_Entity)
7753 Alts (Nalts) := (Alt, Expr_Value (Alt));
7755 for J in 1 .. Nalts - 1 loop
7756 if Alts (J).Val = Alts (Nalts).Val then
7757 Error_Msg_Sloc := Sloc (Alts (J).Alt);
7758 Error_Msg_N ("duplicate of value given#??", Alt);
7767 end Resolve_Set_Membership;
7769 -- Start of processing for Resolve_Membership_Op
7772 if L = Error or else R = Error then
7776 if Present (Alternatives (N)) then
7777 Resolve_Set_Membership;
7778 Check_Function_Writable_Actuals (N);
7781 elsif not Is_Overloaded (R)
7783 (Etype (R) = Universal_Integer
7785 Etype (R) = Universal_Real)
7786 and then Is_Overloaded (L)
7790 -- Ada 2005 (AI-251): Support the following case:
7792 -- type I is interface;
7793 -- type T is tagged ...
7795 -- function Test (O : I'Class) is
7797 -- return O in T'Class.
7800 -- In this case we have nothing else to do. The membership test will be
7801 -- done at run time.
7803 elsif Ada_Version >= Ada_2005
7804 and then Is_Class_Wide_Type (Etype (L))
7805 and then Is_Interface (Etype (L))
7806 and then Is_Class_Wide_Type (Etype (R))
7807 and then not Is_Interface (Etype (R))
7811 T := Intersect_Types (L, R);
7814 -- If mixed-mode operations are present and operands are all literal,
7815 -- the only interpretation involves Duration, which is probably not
7816 -- the intention of the programmer.
7818 if T = Any_Fixed then
7819 T := Unique_Fixed_Point_Type (N);
7821 if T = Any_Type then
7827 Check_Unset_Reference (L);
7829 if Nkind (R) = N_Range
7830 and then not Is_Scalar_Type (T)
7832 Error_Msg_N ("scalar type required for range", R);
7835 if Is_Entity_Name (R) then
7836 Freeze_Expression (R);
7839 Check_Unset_Reference (R);
7842 Eval_Membership_Op (N);
7843 Check_Function_Writable_Actuals (N);
7844 end Resolve_Membership_Op;
7850 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7851 Loc : constant Source_Ptr := Sloc (N);
7854 -- Handle restriction against anonymous null access values This
7855 -- restriction can be turned off using -gnatdj.
7857 -- Ada 2005 (AI-231): Remove restriction
7859 if Ada_Version < Ada_2005
7860 and then not Debug_Flag_J
7861 and then Ekind (Typ) = E_Anonymous_Access_Type
7862 and then Comes_From_Source (N)
7864 -- In the common case of a call which uses an explicitly null value
7865 -- for an access parameter, give specialized error message.
7867 if Nkind (Parent (N)) in N_Subprogram_Call then
7869 ("null is not allowed as argument for an access parameter", N);
7871 -- Standard message for all other cases (are there any?)
7875 ("null cannot be of an anonymous access type", N);
7879 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7880 -- assignment to a null-excluding object
7882 if Ada_Version >= Ada_2005
7883 and then Can_Never_Be_Null (Typ)
7884 and then Nkind (Parent (N)) = N_Assignment_Statement
7886 if not Inside_Init_Proc then
7888 (Compile_Time_Constraint_Error (N,
7889 "(Ada 2005) null not allowed in null-excluding objects??"),
7890 Make_Raise_Constraint_Error (Loc,
7891 Reason => CE_Access_Check_Failed));
7894 Make_Raise_Constraint_Error (Loc,
7895 Reason => CE_Access_Check_Failed));
7899 -- In a distributed context, null for a remote access to subprogram may
7900 -- need to be replaced with a special record aggregate. In this case,
7901 -- return after having done the transformation.
7903 if (Ekind (Typ) = E_Record_Type
7904 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7905 and then Remote_AST_Null_Value (N, Typ)
7910 -- The null literal takes its type from the context
7915 -----------------------
7916 -- Resolve_Op_Concat --
7917 -----------------------
7919 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7921 -- We wish to avoid deep recursion, because concatenations are often
7922 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7923 -- operands nonrecursively until we find something that is not a simple
7924 -- concatenation (A in this case). We resolve that, and then walk back
7925 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7926 -- to do the rest of the work at each level. The Parent pointers allow
7927 -- us to avoid recursion, and thus avoid running out of memory. See also
7928 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7934 -- The following code is equivalent to:
7936 -- Resolve_Op_Concat_First (NN, Typ);
7937 -- Resolve_Op_Concat_Arg (N, ...);
7938 -- Resolve_Op_Concat_Rest (N, Typ);
7940 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7941 -- operand is a concatenation.
7943 -- Walk down left operands
7946 Resolve_Op_Concat_First (NN, Typ);
7947 Op1 := Left_Opnd (NN);
7948 exit when not (Nkind (Op1) = N_Op_Concat
7949 and then not Is_Array_Type (Component_Type (Typ))
7950 and then Entity (Op1) = Entity (NN));
7954 -- Now (given the above example) NN is A&B and Op1 is A
7956 -- First resolve Op1 ...
7958 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7960 -- ... then walk NN back up until we reach N (where we started), calling
7961 -- Resolve_Op_Concat_Rest along the way.
7964 Resolve_Op_Concat_Rest (NN, Typ);
7969 if Base_Type (Etype (N)) /= Standard_String then
7970 Check_SPARK_Restriction
7971 ("result of concatenation should have type String", N);
7973 end Resolve_Op_Concat;
7975 ---------------------------
7976 -- Resolve_Op_Concat_Arg --
7977 ---------------------------
7979 procedure Resolve_Op_Concat_Arg
7985 Btyp : constant Entity_Id := Base_Type (Typ);
7986 Ctyp : constant Entity_Id := Component_Type (Typ);
7991 or else (not Is_Overloaded (Arg)
7992 and then Etype (Arg) /= Any_Composite
7993 and then Covers (Ctyp, Etype (Arg)))
7995 Resolve (Arg, Ctyp);
7997 Resolve (Arg, Btyp);
8000 -- If both Array & Array and Array & Component are visible, there is a
8001 -- potential ambiguity that must be reported.
8003 elsif Has_Compatible_Type (Arg, Ctyp) then
8004 if Nkind (Arg) = N_Aggregate
8005 and then Is_Composite_Type (Ctyp)
8007 if Is_Private_Type (Ctyp) then
8008 Resolve (Arg, Btyp);
8010 -- If the operation is user-defined and not overloaded use its
8011 -- profile. The operation may be a renaming, in which case it has
8012 -- been rewritten, and we want the original profile.
8014 elsif not Is_Overloaded (N)
8015 and then Comes_From_Source (Entity (Original_Node (N)))
8016 and then Ekind (Entity (Original_Node (N))) = E_Function
8020 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
8023 -- Otherwise an aggregate may match both the array type and the
8027 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
8028 Set_Etype (Arg, Any_Type);
8032 if Is_Overloaded (Arg)
8033 and then Has_Compatible_Type (Arg, Typ)
8034 and then Etype (Arg) /= Any_Type
8042 Get_First_Interp (Arg, I, It);
8044 Get_Next_Interp (I, It);
8046 -- Special-case the error message when the overloading is
8047 -- caused by a function that yields an array and can be
8048 -- called without parameters.
8050 if It.Nam = Func then
8051 Error_Msg_Sloc := Sloc (Func);
8052 Error_Msg_N ("ambiguous call to function#", Arg);
8054 ("\\interpretation as call yields&", Arg, Typ);
8056 ("\\interpretation as indexing of call yields&",
8057 Arg, Component_Type (Typ));
8060 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
8062 Get_First_Interp (Arg, I, It);
8063 while Present (It.Nam) loop
8064 Error_Msg_Sloc := Sloc (It.Nam);
8066 if Base_Type (It.Typ) = Btyp
8068 Base_Type (It.Typ) = Base_Type (Ctyp)
8070 Error_Msg_N -- CODEFIX
8071 ("\\possible interpretation#", Arg);
8074 Get_Next_Interp (I, It);
8080 Resolve (Arg, Component_Type (Typ));
8082 if Nkind (Arg) = N_String_Literal then
8083 Set_Etype (Arg, Component_Type (Typ));
8086 if Arg = Left_Opnd (N) then
8087 Set_Is_Component_Left_Opnd (N);
8089 Set_Is_Component_Right_Opnd (N);
8094 Resolve (Arg, Btyp);
8097 -- Concatenation is restricted in SPARK: each operand must be either a
8098 -- string literal, the name of a string constant, a static character or
8099 -- string expression, or another concatenation. Arg cannot be a
8100 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
8101 -- separately on each final operand, past concatenation operations.
8103 if Is_Character_Type (Etype (Arg)) then
8104 if not Is_Static_Expression (Arg) then
8105 Check_SPARK_Restriction
8106 ("character operand for concatenation should be static", Arg);
8109 elsif Is_String_Type (Etype (Arg)) then
8110 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
8111 and then Is_Constant_Object (Entity (Arg)))
8112 and then not Is_Static_Expression (Arg)
8114 Check_SPARK_Restriction
8115 ("string operand for concatenation should be static", Arg);
8118 -- Do not issue error on an operand that is neither a character nor a
8119 -- string, as the error is issued in Resolve_Op_Concat.
8125 Check_Unset_Reference (Arg);
8126 end Resolve_Op_Concat_Arg;
8128 -----------------------------
8129 -- Resolve_Op_Concat_First --
8130 -----------------------------
8132 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
8133 Btyp : constant Entity_Id := Base_Type (Typ);
8134 Op1 : constant Node_Id := Left_Opnd (N);
8135 Op2 : constant Node_Id := Right_Opnd (N);
8138 -- The parser folds an enormous sequence of concatenations of string
8139 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
8140 -- in the right operand. If the expression resolves to a predefined "&"
8141 -- operator, all is well. Otherwise, the parser's folding is wrong, so
8142 -- we give an error. See P_Simple_Expression in Par.Ch4.
8144 if Nkind (Op2) = N_String_Literal
8145 and then Is_Folded_In_Parser (Op2)
8146 and then Ekind (Entity (N)) = E_Function
8148 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
8149 and then String_Length (Strval (Op1)) = 0);
8150 Error_Msg_N ("too many user-defined concatenations", N);
8154 Set_Etype (N, Btyp);
8156 if Is_Limited_Composite (Btyp) then
8157 Error_Msg_N ("concatenation not available for limited array", N);
8158 Explain_Limited_Type (Btyp, N);
8160 end Resolve_Op_Concat_First;
8162 ----------------------------
8163 -- Resolve_Op_Concat_Rest --
8164 ----------------------------
8166 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
8167 Op1 : constant Node_Id := Left_Opnd (N);
8168 Op2 : constant Node_Id := Right_Opnd (N);
8171 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
8173 Generate_Operator_Reference (N, Typ);
8175 if Is_String_Type (Typ) then
8176 Eval_Concatenation (N);
8179 -- If this is not a static concatenation, but the result is a string
8180 -- type (and not an array of strings) ensure that static string operands
8181 -- have their subtypes properly constructed.
8183 if Nkind (N) /= N_String_Literal
8184 and then Is_Character_Type (Component_Type (Typ))
8186 Set_String_Literal_Subtype (Op1, Typ);
8187 Set_String_Literal_Subtype (Op2, Typ);
8189 end Resolve_Op_Concat_Rest;
8191 ----------------------
8192 -- Resolve_Op_Expon --
8193 ----------------------
8195 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
8196 B_Typ : constant Entity_Id := Base_Type (Typ);
8199 -- Catch attempts to do fixed-point exponentiation with universal
8200 -- operands, which is a case where the illegality is not caught during
8201 -- normal operator analysis.
8203 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
8204 Error_Msg_N ("exponentiation not available for fixed point", N);
8207 elsif Nkind (Parent (N)) in N_Op
8208 and then Is_Fixed_Point_Type (Etype (Parent (N)))
8209 and then Etype (N) = Universal_Real
8210 and then Comes_From_Source (N)
8212 Error_Msg_N ("exponentiation not available for fixed point", N);
8216 if Comes_From_Source (N)
8217 and then Ekind (Entity (N)) = E_Function
8218 and then Is_Imported (Entity (N))
8219 and then Is_Intrinsic_Subprogram (Entity (N))
8221 Resolve_Intrinsic_Operator (N, Typ);
8225 if Etype (Left_Opnd (N)) = Universal_Integer
8226 or else Etype (Left_Opnd (N)) = Universal_Real
8228 Check_For_Visible_Operator (N, B_Typ);
8231 -- We do the resolution using the base type, because intermediate values
8232 -- in expressions always are of the base type, not a subtype of it.
8234 Resolve (Left_Opnd (N), B_Typ);
8235 Resolve (Right_Opnd (N), Standard_Integer);
8237 Check_Unset_Reference (Left_Opnd (N));
8238 Check_Unset_Reference (Right_Opnd (N));
8240 Set_Etype (N, B_Typ);
8241 Generate_Operator_Reference (N, B_Typ);
8243 Analyze_Dimension (N);
8245 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
8246 -- Evaluate the exponentiation operator for dimensioned type
8248 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
8253 -- Set overflow checking bit. Much cleverer code needed here eventually
8254 -- and perhaps the Resolve routines should be separated for the various
8255 -- arithmetic operations, since they will need different processing. ???
8257 if Nkind (N) in N_Op then
8258 if not Overflow_Checks_Suppressed (Etype (N)) then
8259 Enable_Overflow_Check (N);
8262 end Resolve_Op_Expon;
8264 --------------------
8265 -- Resolve_Op_Not --
8266 --------------------
8268 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8271 function Parent_Is_Boolean return Boolean;
8272 -- This function determines if the parent node is a boolean operator or
8273 -- operation (comparison op, membership test, or short circuit form) and
8274 -- the not in question is the left operand of this operation. Note that
8275 -- if the not is in parens, then false is returned.
8277 -----------------------
8278 -- Parent_Is_Boolean --
8279 -----------------------
8281 function Parent_Is_Boolean return Boolean is
8283 if Paren_Count (N) /= 0 then
8287 case Nkind (Parent (N)) is
8302 return Left_Opnd (Parent (N)) = N;
8308 end Parent_Is_Boolean;
8310 -- Start of processing for Resolve_Op_Not
8313 -- Predefined operations on scalar types yield the base type. On the
8314 -- other hand, logical operations on arrays yield the type of the
8315 -- arguments (and the context).
8317 if Is_Array_Type (Typ) then
8320 B_Typ := Base_Type (Typ);
8323 if Is_VMS_Operator (Entity (N)) then
8326 -- Straightforward case of incorrect arguments
8328 elsif not Valid_Boolean_Arg (Typ) then
8329 Error_Msg_N ("invalid operand type for operator&", N);
8330 Set_Etype (N, Any_Type);
8333 -- Special case of probable missing parens
8335 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8336 if Parent_Is_Boolean then
8338 ("operand of not must be enclosed in parentheses",
8342 ("no modular type available in this context", N);
8345 Set_Etype (N, Any_Type);
8348 -- OK resolution of NOT
8351 -- Warn if non-boolean types involved. This is a case like not a < b
8352 -- where a and b are modular, where we will get (not a) < b and most
8353 -- likely not (a < b) was intended.
8355 if Warn_On_Questionable_Missing_Parens
8356 and then not Is_Boolean_Type (Typ)
8357 and then Parent_Is_Boolean
8359 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
8362 -- Warn on double negation if checking redundant constructs
8364 if Warn_On_Redundant_Constructs
8365 and then Comes_From_Source (N)
8366 and then Comes_From_Source (Right_Opnd (N))
8367 and then Root_Type (Typ) = Standard_Boolean
8368 and then Nkind (Right_Opnd (N)) = N_Op_Not
8370 Error_Msg_N ("redundant double negation?r?", N);
8373 -- Complete resolution and evaluation of NOT
8375 Resolve (Right_Opnd (N), B_Typ);
8376 Check_Unset_Reference (Right_Opnd (N));
8377 Set_Etype (N, B_Typ);
8378 Generate_Operator_Reference (N, B_Typ);
8383 -----------------------------
8384 -- Resolve_Operator_Symbol --
8385 -----------------------------
8387 -- Nothing to be done, all resolved already
8389 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8390 pragma Warnings (Off, N);
8391 pragma Warnings (Off, Typ);
8395 end Resolve_Operator_Symbol;
8397 ----------------------------------
8398 -- Resolve_Qualified_Expression --
8399 ----------------------------------
8401 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8402 pragma Warnings (Off, Typ);
8404 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8405 Expr : constant Node_Id := Expression (N);
8408 Resolve (Expr, Target_Typ);
8410 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8411 -- operation if not needed.
8413 if Restriction_Check_Required (SPARK)
8414 and then Is_Array_Type (Target_Typ)
8415 and then Is_Array_Type (Etype (Expr))
8416 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8417 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8419 Check_SPARK_Restriction
8420 ("array types should have matching static bounds", N);
8423 -- A qualified expression requires an exact match of the type, class-
8424 -- wide matching is not allowed. However, if the qualifying type is
8425 -- specific and the expression has a class-wide type, it may still be
8426 -- okay, since it can be the result of the expansion of a call to a
8427 -- dispatching function, so we also have to check class-wideness of the
8428 -- type of the expression's original node.
8430 if (Is_Class_Wide_Type (Target_Typ)
8432 (Is_Class_Wide_Type (Etype (Expr))
8433 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8434 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8436 Wrong_Type (Expr, Target_Typ);
8439 -- If the target type is unconstrained, then we reset the type of the
8440 -- result from the type of the expression. For other cases, the actual
8441 -- subtype of the expression is the target type.
8443 if Is_Composite_Type (Target_Typ)
8444 and then not Is_Constrained (Target_Typ)
8446 Set_Etype (N, Etype (Expr));
8449 Analyze_Dimension (N);
8450 Eval_Qualified_Expression (N);
8451 end Resolve_Qualified_Expression;
8457 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8458 L : constant Node_Id := Low_Bound (N);
8459 H : constant Node_Id := High_Bound (N);
8461 function First_Last_Ref return Boolean;
8462 -- Returns True if N is of the form X'First .. X'Last where X is the
8463 -- same entity for both attributes.
8465 --------------------
8466 -- First_Last_Ref --
8467 --------------------
8469 function First_Last_Ref return Boolean is
8470 Lorig : constant Node_Id := Original_Node (L);
8471 Horig : constant Node_Id := Original_Node (H);
8474 if Nkind (Lorig) = N_Attribute_Reference
8475 and then Nkind (Horig) = N_Attribute_Reference
8476 and then Attribute_Name (Lorig) = Name_First
8477 and then Attribute_Name (Horig) = Name_Last
8480 PL : constant Node_Id := Prefix (Lorig);
8481 PH : constant Node_Id := Prefix (Horig);
8483 if Is_Entity_Name (PL)
8484 and then Is_Entity_Name (PH)
8485 and then Entity (PL) = Entity (PH)
8495 -- Start of processing for Resolve_Range
8502 -- Check for inappropriate range on unordered enumeration type
8504 if Bad_Unordered_Enumeration_Reference (N, Typ)
8506 -- Exclude X'First .. X'Last if X is the same entity for both
8508 and then not First_Last_Ref
8510 Error_Msg ("subrange of unordered enumeration type?U?", Sloc (N));
8513 Check_Unset_Reference (L);
8514 Check_Unset_Reference (H);
8516 -- We have to check the bounds for being within the base range as
8517 -- required for a non-static context. Normally this is automatic and
8518 -- done as part of evaluating expressions, but the N_Range node is an
8519 -- exception, since in GNAT we consider this node to be a subexpression,
8520 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8521 -- this, but that would put the test on the main evaluation path for
8524 Check_Non_Static_Context (L);
8525 Check_Non_Static_Context (H);
8527 -- Check for an ambiguous range over character literals. This will
8528 -- happen with a membership test involving only literals.
8530 if Typ = Any_Character then
8531 Ambiguous_Character (L);
8532 Set_Etype (N, Any_Type);
8536 -- If bounds are static, constant-fold them, so size computations are
8537 -- identical between front-end and back-end. Do not perform this
8538 -- transformation while analyzing generic units, as type information
8539 -- would be lost when reanalyzing the constant node in the instance.
8541 if Is_Discrete_Type (Typ) and then Full_Expander_Active then
8542 if Is_OK_Static_Expression (L) then
8543 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8546 if Is_OK_Static_Expression (H) then
8547 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8552 --------------------------
8553 -- Resolve_Real_Literal --
8554 --------------------------
8556 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8557 Actual_Typ : constant Entity_Id := Etype (N);
8560 -- Special processing for fixed-point literals to make sure that the
8561 -- value is an exact multiple of small where this is required. We skip
8562 -- this for the universal real case, and also for generic types.
8564 if Is_Fixed_Point_Type (Typ)
8565 and then Typ /= Universal_Fixed
8566 and then Typ /= Any_Fixed
8567 and then not Is_Generic_Type (Typ)
8570 Val : constant Ureal := Realval (N);
8571 Cintr : constant Ureal := Val / Small_Value (Typ);
8572 Cint : constant Uint := UR_Trunc (Cintr);
8573 Den : constant Uint := Norm_Den (Cintr);
8577 -- Case of literal is not an exact multiple of the Small
8581 -- For a source program literal for a decimal fixed-point type,
8582 -- this is statically illegal (RM 4.9(36)).
8584 if Is_Decimal_Fixed_Point_Type (Typ)
8585 and then Actual_Typ = Universal_Real
8586 and then Comes_From_Source (N)
8588 Error_Msg_N ("value has extraneous low order digits", N);
8591 -- Generate a warning if literal from source
8593 if Is_Static_Expression (N)
8594 and then Warn_On_Bad_Fixed_Value
8597 ("?b?static fixed-point value is not a multiple of Small!",
8601 -- Replace literal by a value that is the exact representation
8602 -- of a value of the type, i.e. a multiple of the small value,
8603 -- by truncation, since Machine_Rounds is false for all GNAT
8604 -- fixed-point types (RM 4.9(38)).
8606 Stat := Is_Static_Expression (N);
8608 Make_Real_Literal (Sloc (N),
8609 Realval => Small_Value (Typ) * Cint));
8611 Set_Is_Static_Expression (N, Stat);
8614 -- In all cases, set the corresponding integer field
8616 Set_Corresponding_Integer_Value (N, Cint);
8620 -- Now replace the actual type by the expected type as usual
8623 Eval_Real_Literal (N);
8624 end Resolve_Real_Literal;
8626 -----------------------
8627 -- Resolve_Reference --
8628 -----------------------
8630 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8631 P : constant Node_Id := Prefix (N);
8634 -- Replace general access with specific type
8636 if Ekind (Etype (N)) = E_Allocator_Type then
8637 Set_Etype (N, Base_Type (Typ));
8640 Resolve (P, Designated_Type (Etype (N)));
8642 -- If we are taking the reference of a volatile entity, then treat it as
8643 -- a potential modification of this entity. This is too conservative,
8644 -- but necessary because remove side effects can cause transformations
8645 -- of normal assignments into reference sequences that otherwise fail to
8646 -- notice the modification.
8648 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8649 Note_Possible_Modification (P, Sure => False);
8651 end Resolve_Reference;
8653 --------------------------------
8654 -- Resolve_Selected_Component --
8655 --------------------------------
8657 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8659 Comp1 : Entity_Id := Empty; -- prevent junk warning
8660 P : constant Node_Id := Prefix (N);
8661 S : constant Node_Id := Selector_Name (N);
8662 T : Entity_Id := Etype (P);
8664 I1 : Interp_Index := 0; -- prevent junk warning
8669 function Init_Component return Boolean;
8670 -- Check whether this is the initialization of a component within an
8671 -- init proc (by assignment or call to another init proc). If true,
8672 -- there is no need for a discriminant check.
8674 --------------------
8675 -- Init_Component --
8676 --------------------
8678 function Init_Component return Boolean is
8680 return Inside_Init_Proc
8681 and then Nkind (Prefix (N)) = N_Identifier
8682 and then Chars (Prefix (N)) = Name_uInit
8683 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8686 -- Start of processing for Resolve_Selected_Component
8689 if Is_Overloaded (P) then
8691 -- Use the context type to select the prefix that has a selector
8692 -- of the correct name and type.
8695 Get_First_Interp (P, I, It);
8697 Search : while Present (It.Typ) loop
8698 if Is_Access_Type (It.Typ) then
8699 T := Designated_Type (It.Typ);
8704 -- Locate selected component. For a private prefix the selector
8705 -- can denote a discriminant.
8707 if Is_Record_Type (T) or else Is_Private_Type (T) then
8709 -- The visible components of a class-wide type are those of
8712 if Is_Class_Wide_Type (T) then
8716 Comp := First_Entity (T);
8717 while Present (Comp) loop
8718 if Chars (Comp) = Chars (S)
8719 and then Covers (Etype (Comp), Typ)
8728 It := Disambiguate (P, I1, I, Any_Type);
8730 if It = No_Interp then
8732 ("ambiguous prefix for selected component", N);
8739 -- There may be an implicit dereference. Retrieve
8740 -- designated record type.
8742 if Is_Access_Type (It1.Typ) then
8743 T := Designated_Type (It1.Typ);
8748 if Scope (Comp1) /= T then
8750 -- Resolution chooses the new interpretation.
8751 -- Find the component with the right name.
8753 Comp1 := First_Entity (T);
8754 while Present (Comp1)
8755 and then Chars (Comp1) /= Chars (S)
8757 Comp1 := Next_Entity (Comp1);
8766 Comp := Next_Entity (Comp);
8770 Get_Next_Interp (I, It);
8773 Resolve (P, It1.Typ);
8775 Set_Entity_With_Style_Check (S, Comp1);
8778 -- Resolve prefix with its type
8783 -- Generate cross-reference. We needed to wait until full overloading
8784 -- resolution was complete to do this, since otherwise we can't tell if
8785 -- we are an lvalue or not.
8787 if May_Be_Lvalue (N) then
8788 Generate_Reference (Entity (S), S, 'm');
8790 Generate_Reference (Entity (S), S, 'r');
8793 -- If prefix is an access type, the node will be transformed into an
8794 -- explicit dereference during expansion. The type of the node is the
8795 -- designated type of that of the prefix.
8797 if Is_Access_Type (Etype (P)) then
8798 T := Designated_Type (Etype (P));
8799 Check_Fully_Declared_Prefix (T, P);
8804 if Has_Discriminants (T)
8805 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8806 and then Present (Original_Record_Component (Entity (S)))
8807 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8808 and then not Discriminant_Checks_Suppressed (T)
8809 and then not Init_Component
8811 Set_Do_Discriminant_Check (N);
8814 if Ekind (Entity (S)) = E_Void then
8815 Error_Msg_N ("premature use of component", S);
8818 -- If the prefix is a record conversion, this may be a renamed
8819 -- discriminant whose bounds differ from those of the original
8820 -- one, so we must ensure that a range check is performed.
8822 if Nkind (P) = N_Type_Conversion
8823 and then Ekind (Entity (S)) = E_Discriminant
8824 and then Is_Discrete_Type (Typ)
8826 Set_Etype (N, Base_Type (Typ));
8829 -- Note: No Eval processing is required, because the prefix is of a
8830 -- record type, or protected type, and neither can possibly be static.
8832 -- If the array type is atomic, and is packed, and we are in a left side
8833 -- context, then this is worth a warning, since we have a situation
8834 -- where the access to the component may cause extra read/writes of the
8835 -- atomic array object, which could be considered unexpected.
8837 if Nkind (N) = N_Selected_Component
8838 and then (Is_Atomic (T)
8839 or else (Is_Entity_Name (Prefix (N))
8840 and then Is_Atomic (Entity (Prefix (N)))))
8841 and then Is_Packed (T)
8845 ("??assignment to component of packed atomic record", Prefix (N));
8847 ("\??may cause unexpected accesses to atomic object", Prefix (N));
8850 Analyze_Dimension (N);
8851 end Resolve_Selected_Component;
8857 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8858 B_Typ : constant Entity_Id := Base_Type (Typ);
8859 L : constant Node_Id := Left_Opnd (N);
8860 R : constant Node_Id := Right_Opnd (N);
8863 -- We do the resolution using the base type, because intermediate values
8864 -- in expressions always are of the base type, not a subtype of it.
8867 Resolve (R, Standard_Natural);
8869 Check_Unset_Reference (L);
8870 Check_Unset_Reference (R);
8872 Set_Etype (N, B_Typ);
8873 Generate_Operator_Reference (N, B_Typ);
8877 ---------------------------
8878 -- Resolve_Short_Circuit --
8879 ---------------------------
8881 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8882 B_Typ : constant Entity_Id := Base_Type (Typ);
8883 L : constant Node_Id := Left_Opnd (N);
8884 R : constant Node_Id := Right_Opnd (N);
8890 -- Check for issuing warning for always False assert/check, this happens
8891 -- when assertions are turned off, in which case the pragma Assert/Check
8892 -- was transformed into:
8894 -- if False and then <condition> then ...
8896 -- and we detect this pattern
8898 if Warn_On_Assertion_Failure
8899 and then Is_Entity_Name (R)
8900 and then Entity (R) = Standard_False
8901 and then Nkind (Parent (N)) = N_If_Statement
8902 and then Nkind (N) = N_And_Then
8903 and then Is_Entity_Name (L)
8904 and then Entity (L) = Standard_False
8907 Orig : constant Node_Id := Original_Node (Parent (N));
8910 if Nkind (Orig) = N_Pragma
8911 and then Pragma_Name (Orig) = Name_Assert
8913 -- Don't want to warn if original condition is explicit False
8916 Expr : constant Node_Id :=
8919 (First (Pragma_Argument_Associations (Orig))));
8921 if Is_Entity_Name (Expr)
8922 and then Entity (Expr) = Standard_False
8926 -- Issue warning. We do not want the deletion of the
8927 -- IF/AND-THEN to take this message with it. We achieve
8928 -- this by making sure that the expanded code points to
8929 -- the Sloc of the expression, not the original pragma.
8931 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
8932 -- The source location of the expression is not usually
8933 -- the best choice here. For example, it gets located on
8934 -- the last AND keyword in a chain of boolean expressiond
8935 -- AND'ed together. It is best to put the message on the
8936 -- first character of the assertion, which is the effect
8937 -- of the First_Node call here.
8940 ("?A?assertion would fail at run time!",
8942 (First (Pragma_Argument_Associations (Orig))));
8946 -- Similar processing for Check pragma
8948 elsif Nkind (Orig) = N_Pragma
8949 and then Pragma_Name (Orig) = Name_Check
8951 -- Don't want to warn if original condition is explicit False
8954 Expr : constant Node_Id :=
8957 (Next (First (Pragma_Argument_Associations (Orig)))));
8959 if Is_Entity_Name (Expr)
8960 and then Entity (Expr) = Standard_False
8967 -- Again use Error_Msg_F rather than Error_Msg_N, see
8968 -- comment above for an explanation of why we do this.
8971 ("?A?check would fail at run time!",
8973 (Last (Pragma_Argument_Associations (Orig))));
8980 -- Continue with processing of short circuit
8982 Check_Unset_Reference (L);
8983 Check_Unset_Reference (R);
8985 Set_Etype (N, B_Typ);
8986 Eval_Short_Circuit (N);
8987 end Resolve_Short_Circuit;
8993 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8994 Drange : constant Node_Id := Discrete_Range (N);
8995 Name : constant Node_Id := Prefix (N);
8996 Array_Type : Entity_Id := Empty;
8997 Index_Type : Entity_Id;
9000 if Is_Overloaded (Name) then
9002 -- Use the context type to select the prefix that yields the correct
9007 I1 : Interp_Index := 0;
9009 P : constant Node_Id := Prefix (N);
9010 Found : Boolean := False;
9013 Get_First_Interp (P, I, It);
9014 while Present (It.Typ) loop
9015 if (Is_Array_Type (It.Typ)
9016 and then Covers (Typ, It.Typ))
9017 or else (Is_Access_Type (It.Typ)
9018 and then Is_Array_Type (Designated_Type (It.Typ))
9019 and then Covers (Typ, Designated_Type (It.Typ)))
9022 It := Disambiguate (P, I1, I, Any_Type);
9024 if It = No_Interp then
9025 Error_Msg_N ("ambiguous prefix for slicing", N);
9030 Array_Type := It.Typ;
9035 Array_Type := It.Typ;
9040 Get_Next_Interp (I, It);
9045 Array_Type := Etype (Name);
9048 Resolve (Name, Array_Type);
9050 if Is_Access_Type (Array_Type) then
9051 Apply_Access_Check (N);
9052 Array_Type := Designated_Type (Array_Type);
9054 -- If the prefix is an access to an unconstrained array, we must use
9055 -- the actual subtype of the object to perform the index checks. The
9056 -- object denoted by the prefix is implicit in the node, so we build
9057 -- an explicit representation for it in order to compute the actual
9060 if not Is_Constrained (Array_Type) then
9061 Remove_Side_Effects (Prefix (N));
9064 Obj : constant Node_Id :=
9065 Make_Explicit_Dereference (Sloc (N),
9066 Prefix => New_Copy_Tree (Prefix (N)));
9068 Set_Etype (Obj, Array_Type);
9069 Set_Parent (Obj, Parent (N));
9070 Array_Type := Get_Actual_Subtype (Obj);
9074 elsif Is_Entity_Name (Name)
9075 or else Nkind (Name) = N_Explicit_Dereference
9076 or else (Nkind (Name) = N_Function_Call
9077 and then not Is_Constrained (Etype (Name)))
9079 Array_Type := Get_Actual_Subtype (Name);
9081 -- If the name is a selected component that depends on discriminants,
9082 -- build an actual subtype for it. This can happen only when the name
9083 -- itself is overloaded; otherwise the actual subtype is created when
9084 -- the selected component is analyzed.
9086 elsif Nkind (Name) = N_Selected_Component
9087 and then Full_Analysis
9088 and then Depends_On_Discriminant (First_Index (Array_Type))
9091 Act_Decl : constant Node_Id :=
9092 Build_Actual_Subtype_Of_Component (Array_Type, Name);
9094 Insert_Action (N, Act_Decl);
9095 Array_Type := Defining_Identifier (Act_Decl);
9098 -- Maybe this should just be "else", instead of checking for the
9099 -- specific case of slice??? This is needed for the case where the
9100 -- prefix is an Image attribute, which gets expanded to a slice, and so
9101 -- has a constrained subtype which we want to use for the slice range
9102 -- check applied below (the range check won't get done if the
9103 -- unconstrained subtype of the 'Image is used).
9105 elsif Nkind (Name) = N_Slice then
9106 Array_Type := Etype (Name);
9109 -- If name was overloaded, set slice type correctly now
9111 Set_Etype (N, Array_Type);
9113 -- If the range is specified by a subtype mark, no resolution is
9114 -- necessary. Else resolve the bounds, and apply needed checks.
9116 if not Is_Entity_Name (Drange) then
9117 if Ekind (Array_Type) = E_String_Literal_Subtype then
9118 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
9120 Index_Type := Etype (First_Index (Array_Type));
9123 Resolve (Drange, Base_Type (Index_Type));
9125 if Nkind (Drange) = N_Range then
9127 -- Ensure that side effects in the bounds are properly handled
9129 Force_Evaluation (Low_Bound (Drange));
9130 Force_Evaluation (High_Bound (Drange));
9132 -- Do not apply the range check to nodes associated with the
9133 -- frontend expansion of the dispatch table. We first check
9134 -- if Ada.Tags is already loaded to avoid the addition of an
9135 -- undesired dependence on such run-time unit.
9137 if not Tagged_Type_Expansion
9139 (RTU_Loaded (Ada_Tags)
9140 and then Nkind (Prefix (N)) = N_Selected_Component
9141 and then Present (Entity (Selector_Name (Prefix (N))))
9142 and then Entity (Selector_Name (Prefix (N))) =
9143 RTE_Record_Component (RE_Prims_Ptr))
9145 Apply_Range_Check (Drange, Index_Type);
9150 Set_Slice_Subtype (N);
9152 -- Check bad use of type with predicates
9154 if Has_Predicates (Etype (Drange)) then
9155 Bad_Predicated_Subtype_Use
9156 ("subtype& has predicate, not allowed in slice",
9157 Drange, Etype (Drange));
9159 -- Otherwise here is where we check suspicious indexes
9161 elsif Nkind (Drange) = N_Range then
9162 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
9163 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
9166 Analyze_Dimension (N);
9170 ----------------------------
9171 -- Resolve_String_Literal --
9172 ----------------------------
9174 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
9175 C_Typ : constant Entity_Id := Component_Type (Typ);
9176 R_Typ : constant Entity_Id := Root_Type (C_Typ);
9177 Loc : constant Source_Ptr := Sloc (N);
9178 Str : constant String_Id := Strval (N);
9179 Strlen : constant Nat := String_Length (Str);
9180 Subtype_Id : Entity_Id;
9181 Need_Check : Boolean;
9184 -- For a string appearing in a concatenation, defer creation of the
9185 -- string_literal_subtype until the end of the resolution of the
9186 -- concatenation, because the literal may be constant-folded away. This
9187 -- is a useful optimization for long concatenation expressions.
9189 -- If the string is an aggregate built for a single character (which
9190 -- happens in a non-static context) or a is null string to which special
9191 -- checks may apply, we build the subtype. Wide strings must also get a
9192 -- string subtype if they come from a one character aggregate. Strings
9193 -- generated by attributes might be static, but it is often hard to
9194 -- determine whether the enclosing context is static, so we generate
9195 -- subtypes for them as well, thus losing some rarer optimizations ???
9196 -- Same for strings that come from a static conversion.
9199 (Strlen = 0 and then Typ /= Standard_String)
9200 or else Nkind (Parent (N)) /= N_Op_Concat
9201 or else (N /= Left_Opnd (Parent (N))
9202 and then N /= Right_Opnd (Parent (N)))
9203 or else ((Typ = Standard_Wide_String
9204 or else Typ = Standard_Wide_Wide_String)
9205 and then Nkind (Original_Node (N)) /= N_String_Literal);
9207 -- If the resolving type is itself a string literal subtype, we can just
9208 -- reuse it, since there is no point in creating another.
9210 if Ekind (Typ) = E_String_Literal_Subtype then
9213 elsif Nkind (Parent (N)) = N_Op_Concat
9214 and then not Need_Check
9215 and then not Nkind_In (Original_Node (N), N_Character_Literal,
9216 N_Attribute_Reference,
9217 N_Qualified_Expression,
9222 -- Otherwise we must create a string literal subtype. Note that the
9223 -- whole idea of string literal subtypes is simply to avoid the need
9224 -- for building a full fledged array subtype for each literal.
9227 Set_String_Literal_Subtype (N, Typ);
9228 Subtype_Id := Etype (N);
9231 if Nkind (Parent (N)) /= N_Op_Concat
9234 Set_Etype (N, Subtype_Id);
9235 Eval_String_Literal (N);
9238 if Is_Limited_Composite (Typ)
9239 or else Is_Private_Composite (Typ)
9241 Error_Msg_N ("string literal not available for private array", N);
9242 Set_Etype (N, Any_Type);
9246 -- The validity of a null string has been checked in the call to
9247 -- Eval_String_Literal.
9252 -- Always accept string literal with component type Any_Character, which
9253 -- occurs in error situations and in comparisons of literals, both of
9254 -- which should accept all literals.
9256 elsif R_Typ = Any_Character then
9259 -- If the type is bit-packed, then we always transform the string
9260 -- literal into a full fledged aggregate.
9262 elsif Is_Bit_Packed_Array (Typ) then
9265 -- Deal with cases of Wide_Wide_String, Wide_String, and String
9268 -- For Standard.Wide_Wide_String, or any other type whose component
9269 -- type is Standard.Wide_Wide_Character, we know that all the
9270 -- characters in the string must be acceptable, since the parser
9271 -- accepted the characters as valid character literals.
9273 if R_Typ = Standard_Wide_Wide_Character then
9276 -- For the case of Standard.String, or any other type whose component
9277 -- type is Standard.Character, we must make sure that there are no
9278 -- wide characters in the string, i.e. that it is entirely composed
9279 -- of characters in range of type Character.
9281 -- If the string literal is the result of a static concatenation, the
9282 -- test has already been performed on the components, and need not be
9285 elsif R_Typ = Standard_Character
9286 and then Nkind (Original_Node (N)) /= N_Op_Concat
9288 for J in 1 .. Strlen loop
9289 if not In_Character_Range (Get_String_Char (Str, J)) then
9291 -- If we are out of range, post error. This is one of the
9292 -- very few places that we place the flag in the middle of
9293 -- a token, right under the offending wide character. Not
9294 -- quite clear if this is right wrt wide character encoding
9295 -- sequences, but it's only an error message!
9298 ("literal out of range of type Standard.Character",
9299 Source_Ptr (Int (Loc) + J));
9304 -- For the case of Standard.Wide_String, or any other type whose
9305 -- component type is Standard.Wide_Character, we must make sure that
9306 -- there are no wide characters in the string, i.e. that it is
9307 -- entirely composed of characters in range of type Wide_Character.
9309 -- If the string literal is the result of a static concatenation,
9310 -- the test has already been performed on the components, and need
9313 elsif R_Typ = Standard_Wide_Character
9314 and then Nkind (Original_Node (N)) /= N_Op_Concat
9316 for J in 1 .. Strlen loop
9317 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
9319 -- If we are out of range, post error. This is one of the
9320 -- very few places that we place the flag in the middle of
9321 -- a token, right under the offending wide character.
9323 -- This is not quite right, because characters in general
9324 -- will take more than one character position ???
9327 ("literal out of range of type Standard.Wide_Character",
9328 Source_Ptr (Int (Loc) + J));
9333 -- If the root type is not a standard character, then we will convert
9334 -- the string into an aggregate and will let the aggregate code do
9335 -- the checking. Standard Wide_Wide_Character is also OK here.
9341 -- See if the component type of the array corresponding to the string
9342 -- has compile time known bounds. If yes we can directly check
9343 -- whether the evaluation of the string will raise constraint error.
9344 -- Otherwise we need to transform the string literal into the
9345 -- corresponding character aggregate and let the aggregate code do
9348 if Is_Standard_Character_Type (R_Typ) then
9350 -- Check for the case of full range, where we are definitely OK
9352 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
9356 -- Here the range is not the complete base type range, so check
9359 Comp_Typ_Lo : constant Node_Id :=
9360 Type_Low_Bound (Component_Type (Typ));
9361 Comp_Typ_Hi : constant Node_Id :=
9362 Type_High_Bound (Component_Type (Typ));
9367 if Compile_Time_Known_Value (Comp_Typ_Lo)
9368 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9370 for J in 1 .. Strlen loop
9371 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9373 if Char_Val < Expr_Value (Comp_Typ_Lo)
9374 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9376 Apply_Compile_Time_Constraint_Error
9377 (N, "character out of range??",
9378 CE_Range_Check_Failed,
9379 Loc => Source_Ptr (Int (Loc) + J));
9389 -- If we got here we meed to transform the string literal into the
9390 -- equivalent qualified positional array aggregate. This is rather
9391 -- heavy artillery for this situation, but it is hard work to avoid.
9394 Lits : constant List_Id := New_List;
9395 P : Source_Ptr := Loc + 1;
9399 -- Build the character literals, we give them source locations that
9400 -- correspond to the string positions, which is a bit tricky given
9401 -- the possible presence of wide character escape sequences.
9403 for J in 1 .. Strlen loop
9404 C := Get_String_Char (Str, J);
9405 Set_Character_Literal_Name (C);
9408 Make_Character_Literal (P,
9410 Char_Literal_Value => UI_From_CC (C)));
9412 if In_Character_Range (C) then
9415 -- Should we have a call to Skip_Wide here ???
9424 Make_Qualified_Expression (Loc,
9425 Subtype_Mark => New_Reference_To (Typ, Loc),
9427 Make_Aggregate (Loc, Expressions => Lits)));
9429 Analyze_And_Resolve (N, Typ);
9431 end Resolve_String_Literal;
9433 -----------------------------
9434 -- Resolve_Subprogram_Info --
9435 -----------------------------
9437 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9440 end Resolve_Subprogram_Info;
9442 -----------------------------
9443 -- Resolve_Type_Conversion --
9444 -----------------------------
9446 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9447 Conv_OK : constant Boolean := Conversion_OK (N);
9448 Operand : constant Node_Id := Expression (N);
9449 Operand_Typ : constant Entity_Id := Etype (Operand);
9450 Target_Typ : constant Entity_Id := Etype (N);
9455 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9456 -- Set to False to suppress cases where we want to suppress the test
9457 -- for redundancy to avoid possible false positives on this warning.
9461 and then not Valid_Conversion (N, Target_Typ, Operand)
9466 -- If the Operand Etype is Universal_Fixed, then the conversion is
9467 -- never redundant. We need this check because by the time we have
9468 -- finished the rather complex transformation, the conversion looks
9469 -- redundant when it is not.
9471 if Operand_Typ = Universal_Fixed then
9472 Test_Redundant := False;
9474 -- If the operand is marked as Any_Fixed, then special processing is
9475 -- required. This is also a case where we suppress the test for a
9476 -- redundant conversion, since most certainly it is not redundant.
9478 elsif Operand_Typ = Any_Fixed then
9479 Test_Redundant := False;
9481 -- Mixed-mode operation involving a literal. Context must be a fixed
9482 -- type which is applied to the literal subsequently.
9484 if Is_Fixed_Point_Type (Typ) then
9485 Set_Etype (Operand, Universal_Real);
9487 elsif Is_Numeric_Type (Typ)
9488 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9489 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9491 Etype (Left_Opnd (Operand)) = Universal_Real)
9493 -- Return if expression is ambiguous
9495 if Unique_Fixed_Point_Type (N) = Any_Type then
9498 -- If nothing else, the available fixed type is Duration
9501 Set_Etype (Operand, Standard_Duration);
9504 -- Resolve the real operand with largest available precision
9506 if Etype (Right_Opnd (Operand)) = Universal_Real then
9507 Rop := New_Copy_Tree (Right_Opnd (Operand));
9509 Rop := New_Copy_Tree (Left_Opnd (Operand));
9512 Resolve (Rop, Universal_Real);
9514 -- If the operand is a literal (it could be a non-static and
9515 -- illegal exponentiation) check whether the use of Duration
9516 -- is potentially inaccurate.
9518 if Nkind (Rop) = N_Real_Literal
9519 and then Realval (Rop) /= Ureal_0
9520 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9523 ("??universal real operand can only "
9524 & "be interpreted as Duration!", Rop);
9526 ("\??precision will be lost in the conversion!", Rop);
9529 elsif Is_Numeric_Type (Typ)
9530 and then Nkind (Operand) in N_Op
9531 and then Unique_Fixed_Point_Type (N) /= Any_Type
9533 Set_Etype (Operand, Standard_Duration);
9536 Error_Msg_N ("invalid context for mixed mode operation", N);
9537 Set_Etype (Operand, Any_Type);
9544 -- In SPARK, a type conversion between array types should be restricted
9545 -- to types which have matching static bounds.
9547 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9548 -- operation if not needed.
9550 if Restriction_Check_Required (SPARK)
9551 and then Is_Array_Type (Target_Typ)
9552 and then Is_Array_Type (Operand_Typ)
9553 and then Operand_Typ /= Any_Composite -- or else Operand in error
9554 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9556 Check_SPARK_Restriction
9557 ("array types should have matching static bounds", N);
9560 -- In formal mode, the operand of an ancestor type conversion must be an
9561 -- object (not an expression).
9563 if Is_Tagged_Type (Target_Typ)
9564 and then not Is_Class_Wide_Type (Target_Typ)
9565 and then Is_Tagged_Type (Operand_Typ)
9566 and then not Is_Class_Wide_Type (Operand_Typ)
9567 and then Is_Ancestor (Target_Typ, Operand_Typ)
9568 and then not Is_SPARK_Object_Reference (Operand)
9570 Check_SPARK_Restriction ("object required", Operand);
9573 Analyze_Dimension (N);
9575 -- Note: we do the Eval_Type_Conversion call before applying the
9576 -- required checks for a subtype conversion. This is important, since
9577 -- both are prepared under certain circumstances to change the type
9578 -- conversion to a constraint error node, but in the case of
9579 -- Eval_Type_Conversion this may reflect an illegality in the static
9580 -- case, and we would miss the illegality (getting only a warning
9581 -- message), if we applied the type conversion checks first.
9583 Eval_Type_Conversion (N);
9585 -- Even when evaluation is not possible, we may be able to simplify the
9586 -- conversion or its expression. This needs to be done before applying
9587 -- checks, since otherwise the checks may use the original expression
9588 -- and defeat the simplifications. This is specifically the case for
9589 -- elimination of the floating-point Truncation attribute in
9590 -- float-to-int conversions.
9592 Simplify_Type_Conversion (N);
9594 -- If after evaluation we still have a type conversion, then we may need
9595 -- to apply checks required for a subtype conversion.
9597 -- Skip these type conversion checks if universal fixed operands
9598 -- operands involved, since range checks are handled separately for
9599 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9601 if Nkind (N) = N_Type_Conversion
9602 and then not Is_Generic_Type (Root_Type (Target_Typ))
9603 and then Target_Typ /= Universal_Fixed
9604 and then Operand_Typ /= Universal_Fixed
9606 Apply_Type_Conversion_Checks (N);
9609 -- Issue warning for conversion of simple object to its own type. We
9610 -- have to test the original nodes, since they may have been rewritten
9611 -- by various optimizations.
9613 Orig_N := Original_Node (N);
9615 -- Here we test for a redundant conversion if the warning mode is
9616 -- active (and was not locally reset), and we have a type conversion
9617 -- from source not appearing in a generic instance.
9620 and then Nkind (Orig_N) = N_Type_Conversion
9621 and then Comes_From_Source (Orig_N)
9622 and then not In_Instance
9624 Orig_N := Original_Node (Expression (Orig_N));
9625 Orig_T := Target_Typ;
9627 -- If the node is part of a larger expression, the Target_Type
9628 -- may not be the original type of the node if the context is a
9629 -- condition. Recover original type to see if conversion is needed.
9631 if Is_Boolean_Type (Orig_T)
9632 and then Nkind (Parent (N)) in N_Op
9634 Orig_T := Etype (Parent (N));
9637 -- If we have an entity name, then give the warning if the entity
9638 -- is the right type, or if it is a loop parameter covered by the
9639 -- original type (that's needed because loop parameters have an
9640 -- odd subtype coming from the bounds).
9642 if (Is_Entity_Name (Orig_N)
9644 (Etype (Entity (Orig_N)) = Orig_T
9646 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9647 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9649 -- If not an entity, then type of expression must match
9651 or else Etype (Orig_N) = Orig_T
9653 -- One more check, do not give warning if the analyzed conversion
9654 -- has an expression with non-static bounds, and the bounds of the
9655 -- target are static. This avoids junk warnings in cases where the
9656 -- conversion is necessary to establish staticness, for example in
9657 -- a case statement.
9659 if not Is_OK_Static_Subtype (Operand_Typ)
9660 and then Is_OK_Static_Subtype (Target_Typ)
9664 -- Finally, if this type conversion occurs in a context requiring
9665 -- a prefix, and the expression is a qualified expression then the
9666 -- type conversion is not redundant, since a qualified expression
9667 -- is not a prefix, whereas a type conversion is. For example, "X
9668 -- := T'(Funx(...)).Y;" is illegal because a selected component
9669 -- requires a prefix, but a type conversion makes it legal: "X :=
9670 -- T(T'(Funx(...))).Y;"
9672 -- In Ada 2012, a qualified expression is a name, so this idiom is
9673 -- no longer needed, but we still suppress the warning because it
9674 -- seems unfriendly for warnings to pop up when you switch to the
9675 -- newer language version.
9677 elsif Nkind (Orig_N) = N_Qualified_Expression
9678 and then Nkind_In (Parent (N), N_Attribute_Reference,
9679 N_Indexed_Component,
9680 N_Selected_Component,
9682 N_Explicit_Dereference)
9686 -- Never warn on conversion to Long_Long_Integer'Base since
9687 -- that is most likely an artifact of the extended overflow
9688 -- checking and comes from complex expanded code.
9690 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
9693 -- Here we give the redundant conversion warning. If it is an
9694 -- entity, give the name of the entity in the message. If not,
9695 -- just mention the expression.
9697 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
9700 if Is_Entity_Name (Orig_N) then
9701 Error_Msg_Node_2 := Orig_T;
9702 Error_Msg_NE -- CODEFIX
9703 ("??redundant conversion, & is of type &!",
9704 N, Entity (Orig_N));
9707 ("??redundant conversion, expression is of type&!",
9714 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9715 -- No need to perform any interface conversion if the type of the
9716 -- expression coincides with the target type.
9718 if Ada_Version >= Ada_2005
9719 and then Full_Expander_Active
9720 and then Operand_Typ /= Target_Typ
9723 Opnd : Entity_Id := Operand_Typ;
9724 Target : Entity_Id := Target_Typ;
9727 if Is_Access_Type (Opnd) then
9728 Opnd := Designated_Type (Opnd);
9731 if Is_Access_Type (Target_Typ) then
9732 Target := Designated_Type (Target);
9735 if Opnd = Target then
9738 -- Conversion from interface type
9740 elsif Is_Interface (Opnd) then
9742 -- Ada 2005 (AI-217): Handle entities from limited views
9744 if From_With_Type (Opnd) then
9745 Error_Msg_Qual_Level := 99;
9746 Error_Msg_NE -- CODEFIX
9747 ("missing WITH clause on package &", N,
9748 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9750 ("type conversions require visibility of the full view",
9753 elsif From_With_Type (Target)
9755 (Is_Access_Type (Target_Typ)
9756 and then Present (Non_Limited_View (Etype (Target))))
9758 Error_Msg_Qual_Level := 99;
9759 Error_Msg_NE -- CODEFIX
9760 ("missing WITH clause on package &", N,
9761 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9763 ("type conversions require visibility of the full view",
9767 Expand_Interface_Conversion (N);
9770 -- Conversion to interface type
9772 elsif Is_Interface (Target) then
9776 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9777 Opnd := Etype (Opnd);
9780 if Is_Class_Wide_Type (Opnd)
9781 or else Interface_Present_In_Ancestor
9785 Expand_Interface_Conversion (N);
9787 Error_Msg_Name_1 := Chars (Etype (Target));
9788 Error_Msg_Name_2 := Chars (Opnd);
9790 ("wrong interface conversion (% is not a progenitor "
9797 -- Ada 2012: if target type has predicates, the result requires a
9798 -- predicate check. If the context is a call to another predicate
9799 -- check we must prevent infinite recursion.
9801 if Has_Predicates (Target_Typ) then
9802 if Nkind (Parent (N)) = N_Function_Call
9803 and then Present (Name (Parent (N)))
9804 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
9806 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
9811 Apply_Predicate_Check (N, Target_Typ);
9814 end Resolve_Type_Conversion;
9816 ----------------------
9817 -- Resolve_Unary_Op --
9818 ----------------------
9820 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9821 B_Typ : constant Entity_Id := Base_Type (Typ);
9822 R : constant Node_Id := Right_Opnd (N);
9828 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9829 Error_Msg_Name_1 := Chars (Typ);
9830 Check_SPARK_Restriction
9831 ("unary operator not defined for modular type%", N);
9834 -- Deal with intrinsic unary operators
9836 if Comes_From_Source (N)
9837 and then Ekind (Entity (N)) = E_Function
9838 and then Is_Imported (Entity (N))
9839 and then Is_Intrinsic_Subprogram (Entity (N))
9841 Resolve_Intrinsic_Unary_Operator (N, Typ);
9845 -- Deal with universal cases
9847 if Etype (R) = Universal_Integer
9849 Etype (R) = Universal_Real
9851 Check_For_Visible_Operator (N, B_Typ);
9854 Set_Etype (N, B_Typ);
9857 -- Generate warning for expressions like abs (x mod 2)
9859 if Warn_On_Redundant_Constructs
9860 and then Nkind (N) = N_Op_Abs
9862 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9864 if OK and then Hi >= Lo and then Lo >= 0 then
9865 Error_Msg_N -- CODEFIX
9866 ("?r?abs applied to known non-negative value has no effect", N);
9870 -- Deal with reference generation
9872 Check_Unset_Reference (R);
9873 Generate_Operator_Reference (N, B_Typ);
9874 Analyze_Dimension (N);
9877 -- Set overflow checking bit. Much cleverer code needed here eventually
9878 -- and perhaps the Resolve routines should be separated for the various
9879 -- arithmetic operations, since they will need different processing ???
9881 if Nkind (N) in N_Op then
9882 if not Overflow_Checks_Suppressed (Etype (N)) then
9883 Enable_Overflow_Check (N);
9887 -- Generate warning for expressions like -5 mod 3 for integers. No need
9888 -- to worry in the floating-point case, since parens do not affect the
9889 -- result so there is no point in giving in a warning.
9892 Norig : constant Node_Id := Original_Node (N);
9901 if Warn_On_Questionable_Missing_Parens
9902 and then Comes_From_Source (Norig)
9903 and then Is_Integer_Type (Typ)
9904 and then Nkind (Norig) = N_Op_Minus
9906 Rorig := Original_Node (Right_Opnd (Norig));
9908 -- We are looking for cases where the right operand is not
9909 -- parenthesized, and is a binary operator, multiply, divide, or
9910 -- mod. These are the cases where the grouping can affect results.
9912 if Paren_Count (Rorig) = 0
9913 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9915 -- For mod, we always give the warning, since the value is
9916 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9917 -- -(5 mod 315)). But for the other cases, the only concern is
9918 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9919 -- overflows, but (-2) * 64 does not). So we try to give the
9920 -- message only when overflow is possible.
9922 if Nkind (Rorig) /= N_Op_Mod
9923 and then Compile_Time_Known_Value (R)
9925 Val := Expr_Value (R);
9927 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9928 HB := Expr_Value (Type_High_Bound (Typ));
9930 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9933 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9934 LB := Expr_Value (Type_Low_Bound (Typ));
9936 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9939 -- Note that the test below is deliberately excluding the
9940 -- largest negative number, since that is a potentially
9941 -- troublesome case (e.g. -2 * x, where the result is the
9942 -- largest negative integer has an overflow with 2 * x).
9944 if Val > LB and then Val <= HB then
9949 -- For the multiplication case, the only case we have to worry
9950 -- about is when (-a)*b is exactly the largest negative number
9951 -- so that -(a*b) can cause overflow. This can only happen if
9952 -- a is a power of 2, and more generally if any operand is a
9953 -- constant that is not a power of 2, then the parentheses
9954 -- cannot affect whether overflow occurs. We only bother to
9955 -- test the left most operand
9957 -- Loop looking at left operands for one that has known value
9960 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9961 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9962 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9964 -- Operand value of 0 or 1 skips warning
9969 -- Otherwise check power of 2, if power of 2, warn, if
9970 -- anything else, skip warning.
9973 while Lval /= 2 loop
9974 if Lval mod 2 = 1 then
9985 -- Keep looking at left operands
9987 Opnd := Left_Opnd (Opnd);
9990 -- For rem or "/" we can only have a problematic situation
9991 -- if the divisor has a value of minus one or one. Otherwise
9992 -- overflow is impossible (divisor > 1) or we have a case of
9993 -- division by zero in any case.
9995 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9996 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9997 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
10002 -- If we fall through warning should be issued
10004 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
10007 ("??unary minus expression should be parenthesized here!", N);
10011 end Resolve_Unary_Op;
10013 ----------------------------------
10014 -- Resolve_Unchecked_Expression --
10015 ----------------------------------
10017 procedure Resolve_Unchecked_Expression
10022 Resolve (Expression (N), Typ, Suppress => All_Checks);
10023 Set_Etype (N, Typ);
10024 end Resolve_Unchecked_Expression;
10026 ---------------------------------------
10027 -- Resolve_Unchecked_Type_Conversion --
10028 ---------------------------------------
10030 procedure Resolve_Unchecked_Type_Conversion
10034 pragma Warnings (Off, Typ);
10036 Operand : constant Node_Id := Expression (N);
10037 Opnd_Type : constant Entity_Id := Etype (Operand);
10040 -- Resolve operand using its own type
10042 Resolve (Operand, Opnd_Type);
10043 Analyze_Dimension (N);
10044 Eval_Unchecked_Conversion (N);
10045 end Resolve_Unchecked_Type_Conversion;
10047 ------------------------------
10048 -- Rewrite_Operator_As_Call --
10049 ------------------------------
10051 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
10052 Loc : constant Source_Ptr := Sloc (N);
10053 Actuals : constant List_Id := New_List;
10057 if Nkind (N) in N_Binary_Op then
10058 Append (Left_Opnd (N), Actuals);
10061 Append (Right_Opnd (N), Actuals);
10064 Make_Function_Call (Sloc => Loc,
10065 Name => New_Occurrence_Of (Nam, Loc),
10066 Parameter_Associations => Actuals);
10068 Preserve_Comes_From_Source (New_N, N);
10069 Preserve_Comes_From_Source (Name (New_N), N);
10070 Rewrite (N, New_N);
10071 Set_Etype (N, Etype (Nam));
10072 end Rewrite_Operator_As_Call;
10074 ------------------------------
10075 -- Rewrite_Renamed_Operator --
10076 ------------------------------
10078 procedure Rewrite_Renamed_Operator
10083 Nam : constant Name_Id := Chars (Op);
10084 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
10088 -- Rewrite the operator node using the real operator, not its renaming.
10089 -- Exclude user-defined intrinsic operations of the same name, which are
10090 -- treated separately and rewritten as calls.
10092 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
10093 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
10094 Set_Chars (Op_Node, Nam);
10095 Set_Etype (Op_Node, Etype (N));
10096 Set_Entity (Op_Node, Op);
10097 Set_Right_Opnd (Op_Node, Right_Opnd (N));
10099 -- Indicate that both the original entity and its renaming are
10100 -- referenced at this point.
10102 Generate_Reference (Entity (N), N);
10103 Generate_Reference (Op, N);
10106 Set_Left_Opnd (Op_Node, Left_Opnd (N));
10109 Rewrite (N, Op_Node);
10111 -- If the context type is private, add the appropriate conversions so
10112 -- that the operator is applied to the full view. This is done in the
10113 -- routines that resolve intrinsic operators.
10115 if Is_Intrinsic_Subprogram (Op)
10116 and then Is_Private_Type (Typ)
10119 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
10120 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
10121 Resolve_Intrinsic_Operator (N, Typ);
10123 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
10124 Resolve_Intrinsic_Unary_Operator (N, Typ);
10131 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
10133 -- Operator renames a user-defined operator of the same name. Use the
10134 -- original operator in the node, which is the one Gigi knows about.
10136 Set_Entity (N, Op);
10137 Set_Is_Overloaded (N, False);
10139 end Rewrite_Renamed_Operator;
10141 -----------------------
10142 -- Set_Slice_Subtype --
10143 -----------------------
10145 -- Build an implicit subtype declaration to represent the type delivered by
10146 -- the slice. This is an abbreviated version of an array subtype. We define
10147 -- an index subtype for the slice, using either the subtype name or the
10148 -- discrete range of the slice. To be consistent with index usage elsewhere
10149 -- we create a list header to hold the single index. This list is not
10150 -- otherwise attached to the syntax tree.
10152 procedure Set_Slice_Subtype (N : Node_Id) is
10153 Loc : constant Source_Ptr := Sloc (N);
10154 Index_List : constant List_Id := New_List;
10156 Index_Subtype : Entity_Id;
10157 Index_Type : Entity_Id;
10158 Slice_Subtype : Entity_Id;
10159 Drange : constant Node_Id := Discrete_Range (N);
10162 if Is_Entity_Name (Drange) then
10163 Index_Subtype := Entity (Drange);
10166 -- We force the evaluation of a range. This is definitely needed in
10167 -- the renamed case, and seems safer to do unconditionally. Note in
10168 -- any case that since we will create and insert an Itype referring
10169 -- to this range, we must make sure any side effect removal actions
10170 -- are inserted before the Itype definition.
10172 if Nkind (Drange) = N_Range then
10173 Force_Evaluation (Low_Bound (Drange));
10174 Force_Evaluation (High_Bound (Drange));
10177 Index_Type := Base_Type (Etype (Drange));
10179 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10181 -- Take a new copy of Drange (where bounds have been rewritten to
10182 -- reference side-effect-free names). Using a separate tree ensures
10183 -- that further expansion (e.g. while rewriting a slice assignment
10184 -- into a FOR loop) does not attempt to remove side effects on the
10185 -- bounds again (which would cause the bounds in the index subtype
10186 -- definition to refer to temporaries before they are defined) (the
10187 -- reason is that some names are considered side effect free here
10188 -- for the subtype, but not in the context of a loop iteration
10191 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
10192 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
10193 Set_Etype (Index_Subtype, Index_Type);
10194 Set_Size_Info (Index_Subtype, Index_Type);
10195 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10198 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
10200 Index := New_Occurrence_Of (Index_Subtype, Loc);
10201 Set_Etype (Index, Index_Subtype);
10202 Append (Index, Index_List);
10204 Set_First_Index (Slice_Subtype, Index);
10205 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
10206 Set_Is_Constrained (Slice_Subtype, True);
10208 Check_Compile_Time_Size (Slice_Subtype);
10210 -- The Etype of the existing Slice node is reset to this slice subtype.
10211 -- Its bounds are obtained from its first index.
10213 Set_Etype (N, Slice_Subtype);
10215 -- For packed slice subtypes, freeze immediately (except in the case of
10216 -- being in a "spec expression" where we never freeze when we first see
10217 -- the expression).
10219 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
10220 Freeze_Itype (Slice_Subtype, N);
10222 -- For all other cases insert an itype reference in the slice's actions
10223 -- so that the itype is frozen at the proper place in the tree (i.e. at
10224 -- the point where actions for the slice are analyzed). Note that this
10225 -- is different from freezing the itype immediately, which might be
10226 -- premature (e.g. if the slice is within a transient scope). This needs
10227 -- to be done only if expansion is enabled.
10229 elsif Full_Expander_Active then
10230 Ensure_Defined (Typ => Slice_Subtype, N => N);
10232 end Set_Slice_Subtype;
10234 --------------------------------
10235 -- Set_String_Literal_Subtype --
10236 --------------------------------
10238 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
10239 Loc : constant Source_Ptr := Sloc (N);
10240 Low_Bound : constant Node_Id :=
10241 Type_Low_Bound (Etype (First_Index (Typ)));
10242 Subtype_Id : Entity_Id;
10245 if Nkind (N) /= N_String_Literal then
10249 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
10250 Set_String_Literal_Length (Subtype_Id, UI_From_Int
10251 (String_Length (Strval (N))));
10252 Set_Etype (Subtype_Id, Base_Type (Typ));
10253 Set_Is_Constrained (Subtype_Id);
10254 Set_Etype (N, Subtype_Id);
10256 -- The low bound is set from the low bound of the corresponding index
10257 -- type. Note that we do not store the high bound in the string literal
10258 -- subtype, but it can be deduced if necessary from the length and the
10261 if Is_OK_Static_Expression (Low_Bound) then
10262 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
10264 -- If the lower bound is not static we create a range for the string
10265 -- literal, using the index type and the known length of the literal.
10266 -- The index type is not necessarily Positive, so the upper bound is
10267 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
10271 Index_List : constant List_Id := New_List;
10272 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
10273 High_Bound : constant Node_Id :=
10274 Make_Attribute_Reference (Loc,
10275 Attribute_Name => Name_Val,
10277 New_Occurrence_Of (Index_Type, Loc),
10278 Expressions => New_List (
10281 Make_Attribute_Reference (Loc,
10282 Attribute_Name => Name_Pos,
10284 New_Occurrence_Of (Index_Type, Loc),
10286 New_List (New_Copy_Tree (Low_Bound))),
10288 Make_Integer_Literal (Loc,
10289 String_Length (Strval (N)) - 1))));
10291 Array_Subtype : Entity_Id;
10294 Index_Subtype : Entity_Id;
10297 if Is_Integer_Type (Index_Type) then
10298 Set_String_Literal_Low_Bound
10299 (Subtype_Id, Make_Integer_Literal (Loc, 1));
10302 -- If the index type is an enumeration type, build bounds
10303 -- expression with attributes.
10305 Set_String_Literal_Low_Bound
10307 Make_Attribute_Reference (Loc,
10308 Attribute_Name => Name_First,
10310 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
10311 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
10314 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
10316 -- Build bona fide subtype for the string, and wrap it in an
10317 -- unchecked conversion, because the backend expects the
10318 -- String_Literal_Subtype to have a static lower bound.
10321 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10322 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
10323 Set_Scalar_Range (Index_Subtype, Drange);
10324 Set_Parent (Drange, N);
10325 Analyze_And_Resolve (Drange, Index_Type);
10327 -- In the context, the Index_Type may already have a constraint,
10328 -- so use common base type on string subtype. The base type may
10329 -- be used when generating attributes of the string, for example
10330 -- in the context of a slice assignment.
10332 Set_Etype (Index_Subtype, Base_Type (Index_Type));
10333 Set_Size_Info (Index_Subtype, Index_Type);
10334 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10336 Array_Subtype := Create_Itype (E_Array_Subtype, N);
10338 Index := New_Occurrence_Of (Index_Subtype, Loc);
10339 Set_Etype (Index, Index_Subtype);
10340 Append (Index, Index_List);
10342 Set_First_Index (Array_Subtype, Index);
10343 Set_Etype (Array_Subtype, Base_Type (Typ));
10344 Set_Is_Constrained (Array_Subtype, True);
10347 Make_Unchecked_Type_Conversion (Loc,
10348 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
10349 Expression => Relocate_Node (N)));
10350 Set_Etype (N, Array_Subtype);
10353 end Set_String_Literal_Subtype;
10355 ------------------------------
10356 -- Simplify_Type_Conversion --
10357 ------------------------------
10359 procedure Simplify_Type_Conversion (N : Node_Id) is
10361 if Nkind (N) = N_Type_Conversion then
10363 Operand : constant Node_Id := Expression (N);
10364 Target_Typ : constant Entity_Id := Etype (N);
10365 Opnd_Typ : constant Entity_Id := Etype (Operand);
10368 if Is_Floating_Point_Type (Opnd_Typ)
10370 (Is_Integer_Type (Target_Typ)
10371 or else (Is_Fixed_Point_Type (Target_Typ)
10372 and then Conversion_OK (N)))
10373 and then Nkind (Operand) = N_Attribute_Reference
10374 and then Attribute_Name (Operand) = Name_Truncation
10376 -- Special processing required if the conversion is the expression
10377 -- of a Truncation attribute reference. In this case we replace:
10379 -- ityp (ftyp'Truncation (x))
10385 -- with the Float_Truncate flag set, which is more efficient.
10389 Relocate_Node (First (Expressions (Operand))));
10390 Set_Float_Truncate (N, True);
10394 end Simplify_Type_Conversion;
10396 -----------------------------
10397 -- Unique_Fixed_Point_Type --
10398 -----------------------------
10400 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10401 T1 : Entity_Id := Empty;
10406 procedure Fixed_Point_Error;
10407 -- Give error messages for true ambiguity. Messages are posted on node
10408 -- N, and entities T1, T2 are the possible interpretations.
10410 -----------------------
10411 -- Fixed_Point_Error --
10412 -----------------------
10414 procedure Fixed_Point_Error is
10416 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10417 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10418 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10419 end Fixed_Point_Error;
10421 -- Start of processing for Unique_Fixed_Point_Type
10424 -- The operations on Duration are visible, so Duration is always a
10425 -- possible interpretation.
10427 T1 := Standard_Duration;
10429 -- Look for fixed-point types in enclosing scopes
10431 Scop := Current_Scope;
10432 while Scop /= Standard_Standard loop
10433 T2 := First_Entity (Scop);
10434 while Present (T2) loop
10435 if Is_Fixed_Point_Type (T2)
10436 and then Current_Entity (T2) = T2
10437 and then Scope (Base_Type (T2)) = Scop
10439 if Present (T1) then
10450 Scop := Scope (Scop);
10453 -- Look for visible fixed type declarations in the context
10455 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10456 while Present (Item) loop
10457 if Nkind (Item) = N_With_Clause then
10458 Scop := Entity (Name (Item));
10459 T2 := First_Entity (Scop);
10460 while Present (T2) loop
10461 if Is_Fixed_Point_Type (T2)
10462 and then Scope (Base_Type (T2)) = Scop
10463 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10465 if Present (T1) then
10480 if Nkind (N) = N_Real_Literal then
10482 ("??real literal interpreted as }!", N, T1);
10485 ("??universal_fixed expression interpreted as }!", N, T1);
10489 end Unique_Fixed_Point_Type;
10491 ----------------------
10492 -- Valid_Conversion --
10493 ----------------------
10495 function Valid_Conversion
10497 Target : Entity_Id;
10499 Report_Errs : Boolean := True) return Boolean
10501 Target_Type : constant Entity_Id := Base_Type (Target);
10502 Opnd_Type : Entity_Id := Etype (Operand);
10503 Inc_Ancestor : Entity_Id;
10505 function Conversion_Check
10507 Msg : String) return Boolean;
10508 -- Little routine to post Msg if Valid is False, returns Valid value
10510 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
10511 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10513 procedure Conversion_Error_NE
10515 N : Node_Or_Entity_Id;
10516 E : Node_Or_Entity_Id);
10517 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10519 function Valid_Tagged_Conversion
10520 (Target_Type : Entity_Id;
10521 Opnd_Type : Entity_Id) return Boolean;
10522 -- Specifically test for validity of tagged conversions
10524 function Valid_Array_Conversion return Boolean;
10525 -- Check index and component conformance, and accessibility levels if
10526 -- the component types are anonymous access types (Ada 2005).
10528 ----------------------
10529 -- Conversion_Check --
10530 ----------------------
10532 function Conversion_Check
10534 Msg : String) return Boolean
10539 -- A generic unit has already been analyzed and we have verified
10540 -- that a particular conversion is OK in that context. Since the
10541 -- instance is reanalyzed without relying on the relationships
10542 -- established during the analysis of the generic, it is possible
10543 -- to end up with inconsistent views of private types. Do not emit
10544 -- the error message in such cases. The rest of the machinery in
10545 -- Valid_Conversion still ensures the proper compatibility of
10546 -- target and operand types.
10548 and then not In_Instance
10550 Conversion_Error_N (Msg, Operand);
10554 end Conversion_Check;
10556 ------------------------
10557 -- Conversion_Error_N --
10558 ------------------------
10560 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
10562 if Report_Errs then
10563 Error_Msg_N (Msg, N);
10565 end Conversion_Error_N;
10567 -------------------------
10568 -- Conversion_Error_NE --
10569 -------------------------
10571 procedure Conversion_Error_NE
10573 N : Node_Or_Entity_Id;
10574 E : Node_Or_Entity_Id)
10577 if Report_Errs then
10578 Error_Msg_NE (Msg, N, E);
10580 end Conversion_Error_NE;
10582 ----------------------------
10583 -- Valid_Array_Conversion --
10584 ----------------------------
10586 function Valid_Array_Conversion return Boolean
10588 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10589 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10591 Opnd_Index : Node_Id;
10592 Opnd_Index_Type : Entity_Id;
10594 Target_Comp_Type : constant Entity_Id :=
10595 Component_Type (Target_Type);
10596 Target_Comp_Base : constant Entity_Id :=
10597 Base_Type (Target_Comp_Type);
10599 Target_Index : Node_Id;
10600 Target_Index_Type : Entity_Id;
10603 -- Error if wrong number of dimensions
10606 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10609 ("incompatible number of dimensions for conversion", Operand);
10612 -- Number of dimensions matches
10615 -- Loop through indexes of the two arrays
10617 Target_Index := First_Index (Target_Type);
10618 Opnd_Index := First_Index (Opnd_Type);
10619 while Present (Target_Index) and then Present (Opnd_Index) loop
10620 Target_Index_Type := Etype (Target_Index);
10621 Opnd_Index_Type := Etype (Opnd_Index);
10623 -- Error if index types are incompatible
10625 if not (Is_Integer_Type (Target_Index_Type)
10626 and then Is_Integer_Type (Opnd_Index_Type))
10627 and then (Root_Type (Target_Index_Type)
10628 /= Root_Type (Opnd_Index_Type))
10631 ("incompatible index types for array conversion",
10636 Next_Index (Target_Index);
10637 Next_Index (Opnd_Index);
10640 -- If component types have same base type, all set
10642 if Target_Comp_Base = Opnd_Comp_Base then
10645 -- Here if base types of components are not the same. The only
10646 -- time this is allowed is if we have anonymous access types.
10648 -- The conversion of arrays of anonymous access types can lead
10649 -- to dangling pointers. AI-392 formalizes the accessibility
10650 -- checks that must be applied to such conversions to prevent
10651 -- out-of-scope references.
10654 (Target_Comp_Base, E_Anonymous_Access_Type,
10655 E_Anonymous_Access_Subprogram_Type)
10656 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10658 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10660 if Type_Access_Level (Target_Type) <
10661 Deepest_Type_Access_Level (Opnd_Type)
10663 if In_Instance_Body then
10665 ("??source array type has deeper accessibility "
10666 & "level than target", Operand);
10668 ("\??Program_Error will be raised at run time",
10671 Make_Raise_Program_Error (Sloc (N),
10672 Reason => PE_Accessibility_Check_Failed));
10673 Set_Etype (N, Target_Type);
10676 -- Conversion not allowed because of accessibility levels
10680 ("source array type has deeper accessibility "
10681 & "level than target", Operand);
10689 -- All other cases where component base types do not match
10693 ("incompatible component types for array conversion",
10698 -- Check that component subtypes statically match. For numeric
10699 -- types this means that both must be either constrained or
10700 -- unconstrained. For enumeration types the bounds must match.
10701 -- All of this is checked in Subtypes_Statically_Match.
10703 if not Subtypes_Statically_Match
10704 (Target_Comp_Type, Opnd_Comp_Type)
10707 ("component subtypes must statically match", Operand);
10713 end Valid_Array_Conversion;
10715 -----------------------------
10716 -- Valid_Tagged_Conversion --
10717 -----------------------------
10719 function Valid_Tagged_Conversion
10720 (Target_Type : Entity_Id;
10721 Opnd_Type : Entity_Id) return Boolean
10724 -- Upward conversions are allowed (RM 4.6(22))
10726 if Covers (Target_Type, Opnd_Type)
10727 or else Is_Ancestor (Target_Type, Opnd_Type)
10731 -- Downward conversion are allowed if the operand is class-wide
10734 elsif Is_Class_Wide_Type (Opnd_Type)
10735 and then Covers (Opnd_Type, Target_Type)
10739 elsif Covers (Opnd_Type, Target_Type)
10740 or else Is_Ancestor (Opnd_Type, Target_Type)
10743 Conversion_Check (False,
10744 "downward conversion of tagged objects not allowed");
10746 -- Ada 2005 (AI-251): The conversion to/from interface types is
10749 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10752 -- If the operand is a class-wide type obtained through a limited_
10753 -- with clause, and the context includes the non-limited view, use
10754 -- it to determine whether the conversion is legal.
10756 elsif Is_Class_Wide_Type (Opnd_Type)
10757 and then From_With_Type (Opnd_Type)
10758 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10759 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10763 elsif Is_Access_Type (Opnd_Type)
10764 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10769 Conversion_Error_NE
10770 ("invalid tagged conversion, not compatible with}",
10771 N, First_Subtype (Opnd_Type));
10774 end Valid_Tagged_Conversion;
10776 -- Start of processing for Valid_Conversion
10779 Check_Parameterless_Call (Operand);
10781 if Is_Overloaded (Operand) then
10791 -- Remove procedure calls, which syntactically cannot appear in
10792 -- this context, but which cannot be removed by type checking,
10793 -- because the context does not impose a type.
10795 -- When compiling for VMS, spurious ambiguities can be produced
10796 -- when arithmetic operations have a literal operand and return
10797 -- System.Address or a descendant of it. These ambiguities are
10798 -- otherwise resolved by the context, but for conversions there
10799 -- is no context type and the removal of the spurious operations
10800 -- must be done explicitly here.
10802 -- The node may be labelled overloaded, but still contain only one
10803 -- interpretation because others were discarded earlier. If this
10804 -- is the case, retain the single interpretation if legal.
10806 Get_First_Interp (Operand, I, It);
10807 Opnd_Type := It.Typ;
10808 Get_Next_Interp (I, It);
10810 if Present (It.Typ)
10811 and then Opnd_Type /= Standard_Void_Type
10813 -- More than one candidate interpretation is available
10815 Get_First_Interp (Operand, I, It);
10816 while Present (It.Typ) loop
10817 if It.Typ = Standard_Void_Type then
10821 if Present (System_Aux_Id)
10822 and then Is_Descendent_Of_Address (It.Typ)
10827 Get_Next_Interp (I, It);
10831 Get_First_Interp (Operand, I, It);
10835 if No (It.Typ) then
10836 Conversion_Error_N ("illegal operand in conversion", Operand);
10840 Get_Next_Interp (I, It);
10842 if Present (It.Typ) then
10845 It1 := Disambiguate (Operand, I1, I, Any_Type);
10847 if It1 = No_Interp then
10849 ("ambiguous operand in conversion", Operand);
10851 -- If the interpretation involves a standard operator, use
10852 -- the location of the type, which may be user-defined.
10854 if Sloc (It.Nam) = Standard_Location then
10855 Error_Msg_Sloc := Sloc (It.Typ);
10857 Error_Msg_Sloc := Sloc (It.Nam);
10860 Conversion_Error_N -- CODEFIX
10861 ("\\possible interpretation#!", Operand);
10863 if Sloc (N1) = Standard_Location then
10864 Error_Msg_Sloc := Sloc (T1);
10866 Error_Msg_Sloc := Sloc (N1);
10869 Conversion_Error_N -- CODEFIX
10870 ("\\possible interpretation#!", Operand);
10876 Set_Etype (Operand, It1.Typ);
10877 Opnd_Type := It1.Typ;
10881 -- If we are within a child unit, check whether the type of the
10882 -- expression has an ancestor in a parent unit, in which case it
10883 -- belongs to its derivation class even if the ancestor is private.
10884 -- See RM 7.3.1 (5.2/3).
10886 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
10890 if Is_Numeric_Type (Target_Type) then
10892 -- A universal fixed expression can be converted to any numeric type
10894 if Opnd_Type = Universal_Fixed then
10897 -- Also no need to check when in an instance or inlined body, because
10898 -- the legality has been established when the template was analyzed.
10899 -- Furthermore, numeric conversions may occur where only a private
10900 -- view of the operand type is visible at the instantiation point.
10901 -- This results in a spurious error if we check that the operand type
10902 -- is a numeric type.
10904 -- Note: in a previous version of this unit, the following tests were
10905 -- applied only for generated code (Comes_From_Source set to False),
10906 -- but in fact the test is required for source code as well, since
10907 -- this situation can arise in source code.
10909 elsif In_Instance or else In_Inlined_Body then
10912 -- Otherwise we need the conversion check
10915 return Conversion_Check
10916 (Is_Numeric_Type (Opnd_Type)
10918 (Present (Inc_Ancestor)
10919 and then Is_Numeric_Type (Inc_Ancestor)),
10920 "illegal operand for numeric conversion");
10925 elsif Is_Array_Type (Target_Type) then
10926 if not Is_Array_Type (Opnd_Type)
10927 or else Opnd_Type = Any_Composite
10928 or else Opnd_Type = Any_String
10931 ("illegal operand for array conversion", Operand);
10935 return Valid_Array_Conversion;
10938 -- Ada 2005 (AI-251): Anonymous access types where target references an
10941 elsif Ekind_In (Target_Type, E_General_Access_Type,
10942 E_Anonymous_Access_Type)
10943 and then Is_Interface (Directly_Designated_Type (Target_Type))
10945 -- Check the static accessibility rule of 4.6(17). Note that the
10946 -- check is not enforced when within an instance body, since the
10947 -- RM requires such cases to be caught at run time.
10949 -- If the operand is a rewriting of an allocator no check is needed
10950 -- because there are no accessibility issues.
10952 if Nkind (Original_Node (N)) = N_Allocator then
10955 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
10956 if Type_Access_Level (Opnd_Type) >
10957 Deepest_Type_Access_Level (Target_Type)
10959 -- In an instance, this is a run-time check, but one we know
10960 -- will fail, so generate an appropriate warning. The raise
10961 -- will be generated by Expand_N_Type_Conversion.
10963 if In_Instance_Body then
10965 ("??cannot convert local pointer to non-local access type",
10968 ("\??Program_Error will be raised at run time", Operand);
10972 ("cannot convert local pointer to non-local access type",
10977 -- Special accessibility checks are needed in the case of access
10978 -- discriminants declared for a limited type.
10980 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10981 and then not Is_Local_Anonymous_Access (Opnd_Type)
10983 -- When the operand is a selected access discriminant the check
10984 -- needs to be made against the level of the object denoted by
10985 -- the prefix of the selected name (Object_Access_Level handles
10986 -- checking the prefix of the operand for this case).
10988 if Nkind (Operand) = N_Selected_Component
10989 and then Object_Access_Level (Operand) >
10990 Deepest_Type_Access_Level (Target_Type)
10992 -- In an instance, this is a run-time check, but one we know
10993 -- will fail, so generate an appropriate warning. The raise
10994 -- will be generated by Expand_N_Type_Conversion.
10996 if In_Instance_Body then
10998 ("??cannot convert access discriminant to non-local "
10999 & "access type", Operand);
11001 ("\??Program_Error will be raised at run time",
11005 ("cannot convert access discriminant to non-local "
11006 & "access type", Operand);
11011 -- The case of a reference to an access discriminant from
11012 -- within a limited type declaration (which will appear as
11013 -- a discriminal) is always illegal because the level of the
11014 -- discriminant is considered to be deeper than any (nameable)
11017 if Is_Entity_Name (Operand)
11018 and then not Is_Local_Anonymous_Access (Opnd_Type)
11020 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11021 and then Present (Discriminal_Link (Entity (Operand)))
11024 ("discriminant has deeper accessibility level than target",
11033 -- General and anonymous access types
11035 elsif Ekind_In (Target_Type, E_General_Access_Type,
11036 E_Anonymous_Access_Type)
11039 (Is_Access_Type (Opnd_Type)
11041 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
11042 E_Access_Protected_Subprogram_Type),
11043 "must be an access-to-object type")
11045 if Is_Access_Constant (Opnd_Type)
11046 and then not Is_Access_Constant (Target_Type)
11049 ("access-to-constant operand type not allowed", Operand);
11053 -- Check the static accessibility rule of 4.6(17). Note that the
11054 -- check is not enforced when within an instance body, since the RM
11055 -- requires such cases to be caught at run time.
11057 if Ekind (Target_Type) /= E_Anonymous_Access_Type
11058 or else Is_Local_Anonymous_Access (Target_Type)
11059 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
11060 N_Object_Declaration
11062 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
11063 -- conversions from an anonymous access type to a named general
11064 -- access type. Such conversions are not allowed in the case of
11065 -- access parameters and stand-alone objects of an anonymous
11066 -- access type. The implicit conversion case is recognized by
11067 -- testing that Comes_From_Source is False and that it's been
11068 -- rewritten. The Comes_From_Source test isn't sufficient because
11069 -- nodes in inlined calls to predefined library routines can have
11070 -- Comes_From_Source set to False. (Is there a better way to test
11071 -- for implicit conversions???)
11073 if Ada_Version >= Ada_2012
11074 and then not Comes_From_Source (N)
11075 and then N /= Original_Node (N)
11076 and then Ekind (Target_Type) = E_General_Access_Type
11077 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
11079 if Is_Itype (Opnd_Type) then
11081 -- Implicit conversions aren't allowed for objects of an
11082 -- anonymous access type, since such objects have nonstatic
11083 -- levels in Ada 2012.
11085 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
11086 N_Object_Declaration
11089 ("implicit conversion of stand-alone anonymous "
11090 & "access object not allowed", Operand);
11093 -- Implicit conversions aren't allowed for anonymous access
11094 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
11095 -- is done to exclude anonymous access results.
11097 elsif not Is_Local_Anonymous_Access (Opnd_Type)
11098 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
11099 N_Function_Specification,
11100 N_Procedure_Specification)
11103 ("implicit conversion of anonymous access formal "
11104 & "not allowed", Operand);
11107 -- This is a case where there's an enclosing object whose
11108 -- to which the "statically deeper than" relationship does
11109 -- not apply (such as an access discriminant selected from
11110 -- a dereference of an access parameter).
11112 elsif Object_Access_Level (Operand)
11113 = Scope_Depth (Standard_Standard)
11116 ("implicit conversion of anonymous access value "
11117 & "not allowed", Operand);
11120 -- In other cases, the level of the operand's type must be
11121 -- statically less deep than that of the target type, else
11122 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
11124 elsif Type_Access_Level (Opnd_Type) >
11125 Deepest_Type_Access_Level (Target_Type)
11128 ("implicit conversion of anonymous access value "
11129 & "violates accessibility", Operand);
11134 elsif Type_Access_Level (Opnd_Type) >
11135 Deepest_Type_Access_Level (Target_Type)
11137 -- In an instance, this is a run-time check, but one we know
11138 -- will fail, so generate an appropriate warning. The raise
11139 -- will be generated by Expand_N_Type_Conversion.
11141 if In_Instance_Body then
11143 ("??cannot convert local pointer to non-local access type",
11146 ("\??Program_Error will be raised at run time", Operand);
11149 -- Avoid generation of spurious error message
11151 if not Error_Posted (N) then
11153 ("cannot convert local pointer to non-local access type",
11160 -- Special accessibility checks are needed in the case of access
11161 -- discriminants declared for a limited type.
11163 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11164 and then not Is_Local_Anonymous_Access (Opnd_Type)
11166 -- When the operand is a selected access discriminant the check
11167 -- needs to be made against the level of the object denoted by
11168 -- the prefix of the selected name (Object_Access_Level handles
11169 -- checking the prefix of the operand for this case).
11171 if Nkind (Operand) = N_Selected_Component
11172 and then Object_Access_Level (Operand) >
11173 Deepest_Type_Access_Level (Target_Type)
11175 -- In an instance, this is a run-time check, but one we know
11176 -- will fail, so generate an appropriate warning. The raise
11177 -- will be generated by Expand_N_Type_Conversion.
11179 if In_Instance_Body then
11181 ("??cannot convert access discriminant to non-local "
11182 & "access type", Operand);
11184 ("\??Program_Error will be raised at run time",
11189 ("cannot convert access discriminant to non-local "
11190 & "access type", Operand);
11195 -- The case of a reference to an access discriminant from
11196 -- within a limited type declaration (which will appear as
11197 -- a discriminal) is always illegal because the level of the
11198 -- discriminant is considered to be deeper than any (nameable)
11201 if Is_Entity_Name (Operand)
11203 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11204 and then Present (Discriminal_Link (Entity (Operand)))
11207 ("discriminant has deeper accessibility level than target",
11214 -- In the presence of limited_with clauses we have to use non-limited
11215 -- views, if available.
11217 Check_Limited : declare
11218 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
11219 -- Helper function to handle limited views
11221 --------------------------
11222 -- Full_Designated_Type --
11223 --------------------------
11225 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
11226 Desig : constant Entity_Id := Designated_Type (T);
11229 -- Handle the limited view of a type
11231 if Is_Incomplete_Type (Desig)
11232 and then From_With_Type (Desig)
11233 and then Present (Non_Limited_View (Desig))
11235 return Available_View (Desig);
11239 end Full_Designated_Type;
11241 -- Local Declarations
11243 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
11244 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
11246 Same_Base : constant Boolean :=
11247 Base_Type (Target) = Base_Type (Opnd);
11249 -- Start of processing for Check_Limited
11252 if Is_Tagged_Type (Target) then
11253 return Valid_Tagged_Conversion (Target, Opnd);
11256 if not Same_Base then
11257 Conversion_Error_NE
11258 ("target designated type not compatible with }",
11259 N, Base_Type (Opnd));
11262 -- Ada 2005 AI-384: legality rule is symmetric in both
11263 -- designated types. The conversion is legal (with possible
11264 -- constraint check) if either designated type is
11267 elsif Subtypes_Statically_Match (Target, Opnd)
11269 (Has_Discriminants (Target)
11271 (not Is_Constrained (Opnd)
11272 or else not Is_Constrained (Target)))
11274 -- Special case, if Value_Size has been used to make the
11275 -- sizes different, the conversion is not allowed even
11276 -- though the subtypes statically match.
11278 if Known_Static_RM_Size (Target)
11279 and then Known_Static_RM_Size (Opnd)
11280 and then RM_Size (Target) /= RM_Size (Opnd)
11282 Conversion_Error_NE
11283 ("target designated subtype not compatible with }",
11285 Conversion_Error_NE
11286 ("\because sizes of the two designated subtypes differ",
11290 -- Normal case where conversion is allowed
11298 ("target designated subtype not compatible with }",
11305 -- Access to subprogram types. If the operand is an access parameter,
11306 -- the type has a deeper accessibility that any master, and cannot be
11307 -- assigned. We must make an exception if the conversion is part of an
11308 -- assignment and the target is the return object of an extended return
11309 -- statement, because in that case the accessibility check takes place
11310 -- after the return.
11312 elsif Is_Access_Subprogram_Type (Target_Type)
11313 and then No (Corresponding_Remote_Type (Opnd_Type))
11315 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
11316 and then Is_Entity_Name (Operand)
11317 and then Ekind (Entity (Operand)) = E_In_Parameter
11319 (Nkind (Parent (N)) /= N_Assignment_Statement
11320 or else not Is_Entity_Name (Name (Parent (N)))
11321 or else not Is_Return_Object (Entity (Name (Parent (N)))))
11324 ("illegal attempt to store anonymous access to subprogram",
11327 ("\value has deeper accessibility than any master "
11328 & "(RM 3.10.2 (13))",
11332 ("\use named access type for& instead of access parameter",
11333 Operand, Entity (Operand));
11336 -- Check that the designated types are subtype conformant
11338 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
11339 Old_Id => Designated_Type (Opnd_Type),
11342 -- Check the static accessibility rule of 4.6(20)
11344 if Type_Access_Level (Opnd_Type) >
11345 Deepest_Type_Access_Level (Target_Type)
11348 ("operand type has deeper accessibility level than target",
11351 -- Check that if the operand type is declared in a generic body,
11352 -- then the target type must be declared within that same body
11353 -- (enforces last sentence of 4.6(20)).
11355 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
11357 O_Gen : constant Node_Id :=
11358 Enclosing_Generic_Body (Opnd_Type);
11363 T_Gen := Enclosing_Generic_Body (Target_Type);
11364 while Present (T_Gen) and then T_Gen /= O_Gen loop
11365 T_Gen := Enclosing_Generic_Body (T_Gen);
11368 if T_Gen /= O_Gen then
11370 ("target type must be declared in same generic body "
11371 & "as operand type", N);
11378 -- Remote subprogram access types
11380 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
11381 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
11383 -- It is valid to convert from one RAS type to another provided
11384 -- that their specification statically match.
11386 Check_Subtype_Conformant
11388 Designated_Type (Corresponding_Remote_Type (Target_Type)),
11390 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
11395 -- If it was legal in the generic, it's legal in the instance
11397 elsif In_Instance_Body then
11400 -- If both are tagged types, check legality of view conversions
11402 elsif Is_Tagged_Type (Target_Type)
11404 Is_Tagged_Type (Opnd_Type)
11406 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
11408 -- Types derived from the same root type are convertible
11410 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
11413 -- In an instance or an inlined body, there may be inconsistent views of
11414 -- the same type, or of types derived from a common root.
11416 elsif (In_Instance or In_Inlined_Body)
11418 Root_Type (Underlying_Type (Target_Type)) =
11419 Root_Type (Underlying_Type (Opnd_Type))
11423 -- Special check for common access type error case
11425 elsif Ekind (Target_Type) = E_Access_Type
11426 and then Is_Access_Type (Opnd_Type)
11428 Conversion_Error_N ("target type must be general access type!", N);
11429 Conversion_Error_NE -- CODEFIX
11430 ("add ALL to }!", N, Target_Type);
11434 Conversion_Error_NE
11435 ("invalid conversion, not compatible with }", N, Opnd_Type);
11438 end Valid_Conversion;