1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
48 with Output; use Output;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Aggr; use Sem_Aggr;
55 with Sem_Attr; use Sem_Attr;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch4; use Sem_Ch4;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Elim; use Sem_Elim;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Intr; use Sem_Intr;
67 with Sem_Util; use Sem_Util;
68 with Sem_Type; use Sem_Type;
69 with Sem_Warn; use Sem_Warn;
70 with Sinfo; use Sinfo;
71 with Sinfo.CN; use Sinfo.CN;
72 with Snames; use Snames;
73 with Stand; use Stand;
74 with Stringt; use Stringt;
75 with Style; use Style;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
80 package body Sem_Res is
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
86 -- Second pass (top-down) type checking and overload resolution procedures
87 -- Typ is the type required by context. These procedures propagate the
88 -- type information recursively to the descendants of N. If the node
89 -- is not overloaded, its Etype is established in the first pass. If
90 -- overloaded, the Resolve routines set the correct type. For arith.
91 -- operators, the Etype is the base type of the context.
93 -- Note that Resolve_Attribute is separated off in Sem_Attr
95 function Bad_Unordered_Enumeration_Reference
97 T : Entity_Id) return Boolean;
98 -- Node N contains a potentially dubious reference to type T, either an
99 -- explicit comparison, or an explicit range. This function returns True
100 -- if the type T is an enumeration type for which No pragma Order has been
101 -- given, and the reference N is not in the same extended source unit as
102 -- the declaration of T.
104 procedure Check_Discriminant_Use (N : Node_Id);
105 -- Enforce the restrictions on the use of discriminants when constraining
106 -- a component of a discriminated type (record or concurrent type).
108 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
109 -- Given a node for an operator associated with type T, check that
110 -- the operator is visible. Operators all of whose operands are
111 -- universal must be checked for visibility during resolution
112 -- because their type is not determinable based on their operands.
114 procedure Check_Fully_Declared_Prefix
117 -- Check that the type of the prefix of a dereference is not incomplete
119 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
120 -- Given a call node, N, which is known to occur immediately within the
121 -- subprogram being called, determines whether it is a detectable case of
122 -- an infinite recursion, and if so, outputs appropriate messages. Returns
123 -- True if an infinite recursion is detected, and False otherwise.
125 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
126 -- If the type of the object being initialized uses the secondary stack
127 -- directly or indirectly, create a transient scope for the call to the
128 -- init proc. This is because we do not create transient scopes for the
129 -- initialization of individual components within the init proc itself.
130 -- Could be optimized away perhaps?
132 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
133 -- N is the node for a logical operator. If the operator is predefined, and
134 -- the root type of the operands is Standard.Boolean, then a check is made
135 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
136 -- the style check for Style_Check_Boolean_And_Or.
138 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
139 -- Determine whether E is an access type declared by an access
140 -- declaration, and not an (anonymous) allocator type.
142 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
143 -- Utility to check whether the entity for an operator is a predefined
144 -- operator, in which case the expression is left as an operator in the
145 -- tree (else it is rewritten into a call). An instance of an intrinsic
146 -- conversion operation may be given an operator name, but is not treated
147 -- like an operator. Note that an operator that is an imported back-end
148 -- builtin has convention Intrinsic, but is expected to be rewritten into
149 -- a call, so such an operator is not treated as predefined by this
152 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
153 -- If a default expression in entry call N depends on the discriminants
154 -- of the task, it must be replaced with a reference to the discriminant
155 -- of the task being called.
157 procedure Resolve_Op_Concat_Arg
162 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
163 -- concatenation operator. The operand is either of the array type or of
164 -- the component type. If the operand is an aggregate, and the component
165 -- type is composite, this is ambiguous if component type has aggregates.
167 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
168 -- Does the first part of the work of Resolve_Op_Concat
170 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
171 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
172 -- has been resolved. See Resolve_Op_Concat for details.
174 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
210 function Operator_Kind
212 Is_Binary : Boolean) return Node_Kind;
213 -- Utility to map the name of an operator into the corresponding Node. Used
214 -- by other node rewriting procedures.
216 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
217 -- Resolve actuals of call, and add default expressions for missing ones.
218 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
219 -- called subprogram.
221 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
222 -- Called from Resolve_Call, when the prefix denotes an entry or element
223 -- of entry family. Actuals are resolved as for subprograms, and the node
224 -- is rebuilt as an entry call. Also called for protected operations. Typ
225 -- is the context type, which is used when the operation is a protected
226 -- function with no arguments, and the return value is indexed.
228 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
229 -- A call to a user-defined intrinsic operator is rewritten as a call
230 -- to the corresponding predefined operator, with suitable conversions.
231 -- Note that this applies only for intrinsic operators that denote
232 -- predefined operators, not operators that are intrinsic imports of
233 -- back-end builtins.
235 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
236 -- Ditto, for unary operators (arithmetic ones and "not" on signed
237 -- integer types for VMS).
239 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
240 -- If an operator node resolves to a call to a user-defined operator,
241 -- rewrite the node as a function call.
243 procedure Make_Call_Into_Operator
247 -- Inverse transformation: if an operator is given in functional notation,
248 -- then after resolving the node, transform into an operator node, so
249 -- that operands are resolved properly. Recall that predefined operators
250 -- do not have a full signature and special resolution rules apply.
252 procedure Rewrite_Renamed_Operator
256 -- An operator can rename another, e.g. in an instantiation. In that
257 -- case, the proper operator node must be constructed and resolved.
259 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
260 -- The String_Literal_Subtype is built for all strings that are not
261 -- operands of a static concatenation operation. If the argument is
262 -- not a N_String_Literal node, then the call has no effect.
264 procedure Set_Slice_Subtype (N : Node_Id);
265 -- Build subtype of array type, with the range specified by the slice
267 procedure Simplify_Type_Conversion (N : Node_Id);
268 -- Called after N has been resolved and evaluated, but before range checks
269 -- have been applied. Currently simplifies a combination of floating-point
270 -- to integer conversion and Truncation attribute.
272 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
273 -- A universal_fixed expression in an universal context is unambiguous
274 -- if there is only one applicable fixed point type. Determining whether
275 -- there is only one requires a search over all visible entities, and
276 -- happens only in very pathological cases (see 6115-006).
278 function Valid_Conversion
281 Operand : Node_Id) return Boolean;
282 -- Verify legality rules given in 4.6 (8-23). Target is the target
283 -- type of the conversion, which may be an implicit conversion of
284 -- an actual parameter to an anonymous access type (in which case
285 -- N denotes the actual parameter and N = Operand).
287 -------------------------
288 -- Ambiguous_Character --
289 -------------------------
291 procedure Ambiguous_Character (C : Node_Id) is
295 if Nkind (C) = N_Character_Literal then
296 Error_Msg_N ("ambiguous character literal", C);
298 -- First the ones in Standard
300 Error_Msg_N ("\\possible interpretation: Character!", C);
301 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
303 -- Include Wide_Wide_Character in Ada 2005 mode
305 if Ada_Version >= Ada_2005 then
306 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
309 -- Now any other types that match
311 E := Current_Entity (C);
312 while Present (E) loop
313 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
317 end Ambiguous_Character;
319 -------------------------
320 -- Analyze_And_Resolve --
321 -------------------------
323 procedure Analyze_And_Resolve (N : Node_Id) is
327 end Analyze_And_Resolve;
329 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
333 end Analyze_And_Resolve;
335 -- Version withs check(s) suppressed
337 procedure Analyze_And_Resolve
342 Scop : constant Entity_Id := Current_Scope;
345 if Suppress = All_Checks then
347 Svg : constant Suppress_Array := Scope_Suppress;
349 Scope_Suppress := (others => True);
350 Analyze_And_Resolve (N, Typ);
351 Scope_Suppress := Svg;
356 Svg : constant Boolean := Scope_Suppress (Suppress);
359 Scope_Suppress (Suppress) := True;
360 Analyze_And_Resolve (N, Typ);
361 Scope_Suppress (Suppress) := Svg;
365 if Current_Scope /= Scop
366 and then Scope_Is_Transient
368 -- This can only happen if a transient scope was created
369 -- for an inner expression, which will be removed upon
370 -- completion of the analysis of an enclosing construct.
371 -- The transient scope must have the suppress status of
372 -- the enclosing environment, not of this Analyze call.
374 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
377 end Analyze_And_Resolve;
379 procedure Analyze_And_Resolve
383 Scop : constant Entity_Id := Current_Scope;
386 if Suppress = All_Checks then
388 Svg : constant Suppress_Array := Scope_Suppress;
390 Scope_Suppress := (others => True);
391 Analyze_And_Resolve (N);
392 Scope_Suppress := Svg;
397 Svg : constant Boolean := Scope_Suppress (Suppress);
400 Scope_Suppress (Suppress) := True;
401 Analyze_And_Resolve (N);
402 Scope_Suppress (Suppress) := Svg;
406 if Current_Scope /= Scop
407 and then Scope_Is_Transient
409 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
412 end Analyze_And_Resolve;
414 ----------------------------------------
415 -- Bad_Unordered_Enumeration_Reference --
416 ----------------------------------------
418 function Bad_Unordered_Enumeration_Reference
420 T : Entity_Id) return Boolean
423 return Is_Enumeration_Type (T)
424 and then Comes_From_Source (N)
425 and then Warn_On_Unordered_Enumeration_Type
426 and then not Has_Pragma_Ordered (T)
427 and then not In_Same_Extended_Unit (N, T);
428 end Bad_Unordered_Enumeration_Reference;
430 ----------------------------
431 -- Check_Discriminant_Use --
432 ----------------------------
434 procedure Check_Discriminant_Use (N : Node_Id) is
435 PN : constant Node_Id := Parent (N);
436 Disc : constant Entity_Id := Entity (N);
441 -- Any use in a spec-expression is legal
443 if In_Spec_Expression then
446 elsif Nkind (PN) = N_Range then
448 -- Discriminant cannot be used to constrain a scalar type
452 if Nkind (P) = N_Range_Constraint
453 and then Nkind (Parent (P)) = N_Subtype_Indication
454 and then Nkind (Parent (Parent (P))) = N_Component_Definition
456 Error_Msg_N ("discriminant cannot constrain scalar type", N);
458 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
460 -- The following check catches the unusual case where
461 -- a discriminant appears within an index constraint
462 -- that is part of a larger expression within a constraint
463 -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
464 -- For now we only check case of record components, and
465 -- note that a similar check should also apply in the
466 -- case of discriminant constraints below. ???
468 -- Note that the check for N_Subtype_Declaration below is to
469 -- detect the valid use of discriminants in the constraints of a
470 -- subtype declaration when this subtype declaration appears
471 -- inside the scope of a record type (which is syntactically
472 -- illegal, but which may be created as part of derived type
473 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
476 if Ekind (Current_Scope) = E_Record_Type
477 and then Scope (Disc) = Current_Scope
479 (Nkind (Parent (P)) = N_Subtype_Indication
481 Nkind_In (Parent (Parent (P)), N_Component_Definition,
482 N_Subtype_Declaration)
483 and then Paren_Count (N) = 0)
486 ("discriminant must appear alone in component constraint", N);
490 -- Detect a common error:
492 -- type R (D : Positive := 100) is record
493 -- Name : String (1 .. D);
496 -- The default value causes an object of type R to be allocated
497 -- with room for Positive'Last characters. The RM does not mandate
498 -- the allocation of the maximum size, but that is what GNAT does
499 -- so we should warn the programmer that there is a problem.
501 Check_Large : declare
507 function Large_Storage_Type (T : Entity_Id) return Boolean;
508 -- Return True if type T has a large enough range that
509 -- any array whose index type covered the whole range of
510 -- the type would likely raise Storage_Error.
512 ------------------------
513 -- Large_Storage_Type --
514 ------------------------
516 function Large_Storage_Type (T : Entity_Id) return Boolean is
518 -- The type is considered large if its bounds are known at
519 -- compile time and if it requires at least as many bits as
520 -- a Positive to store the possible values.
522 return Compile_Time_Known_Value (Type_Low_Bound (T))
523 and then Compile_Time_Known_Value (Type_High_Bound (T))
525 Minimum_Size (T, Biased => True) >=
526 RM_Size (Standard_Positive);
527 end Large_Storage_Type;
529 -- Start of processing for Check_Large
532 -- Check that the Disc has a large range
534 if not Large_Storage_Type (Etype (Disc)) then
538 -- If the enclosing type is limited, we allocate only the
539 -- default value, not the maximum, and there is no need for
542 if Is_Limited_Type (Scope (Disc)) then
546 -- Check that it is the high bound
548 if N /= High_Bound (PN)
549 or else No (Discriminant_Default_Value (Disc))
554 -- Check the array allows a large range at this bound.
555 -- First find the array
559 if Nkind (SI) /= N_Subtype_Indication then
563 T := Entity (Subtype_Mark (SI));
565 if not Is_Array_Type (T) then
569 -- Next, find the dimension
571 TB := First_Index (T);
572 CB := First (Constraints (P));
574 and then Present (TB)
575 and then Present (CB)
586 -- Now, check the dimension has a large range
588 if not Large_Storage_Type (Etype (TB)) then
592 -- Warn about the danger
595 ("?creation of & object may raise Storage_Error!",
604 -- Legal case is in index or discriminant constraint
606 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
607 N_Discriminant_Association)
609 if Paren_Count (N) > 0 then
611 ("discriminant in constraint must appear alone", N);
613 elsif Nkind (N) = N_Expanded_Name
614 and then Comes_From_Source (N)
617 ("discriminant must appear alone as a direct name", N);
622 -- Otherwise, context is an expression. It should not be within
623 -- (i.e. a subexpression of) a constraint for a component.
628 while not Nkind_In (P, N_Component_Declaration,
629 N_Subtype_Indication,
637 -- If the discriminant is used in an expression that is a bound
638 -- of a scalar type, an Itype is created and the bounds are attached
639 -- to its range, not to the original subtype indication. Such use
640 -- is of course a double fault.
642 if (Nkind (P) = N_Subtype_Indication
643 and then Nkind_In (Parent (P), N_Component_Definition,
644 N_Derived_Type_Definition)
645 and then D = Constraint (P))
647 -- The constraint itself may be given by a subtype indication,
648 -- rather than by a more common discrete range.
650 or else (Nkind (P) = N_Subtype_Indication
652 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
653 or else Nkind (P) = N_Entry_Declaration
654 or else Nkind (D) = N_Defining_Identifier
657 ("discriminant in constraint must appear alone", N);
660 end Check_Discriminant_Use;
662 --------------------------------
663 -- Check_For_Visible_Operator --
664 --------------------------------
666 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
668 if Is_Invisible_Operator (N, T) then
669 Error_Msg_NE -- CODEFIX
670 ("operator for} is not directly visible!", N, First_Subtype (T));
671 Error_Msg_N -- CODEFIX
672 ("use clause would make operation legal!", N);
674 end Check_For_Visible_Operator;
676 ----------------------------------
677 -- Check_Fully_Declared_Prefix --
678 ----------------------------------
680 procedure Check_Fully_Declared_Prefix
685 -- Check that the designated type of the prefix of a dereference is
686 -- not an incomplete type. This cannot be done unconditionally, because
687 -- dereferences of private types are legal in default expressions. This
688 -- case is taken care of in Check_Fully_Declared, called below. There
689 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
691 -- This consideration also applies to similar checks for allocators,
692 -- qualified expressions, and type conversions.
694 -- An additional exception concerns other per-object expressions that
695 -- are not directly related to component declarations, in particular
696 -- representation pragmas for tasks. These will be per-object
697 -- expressions if they depend on discriminants or some global entity.
698 -- If the task has access discriminants, the designated type may be
699 -- incomplete at the point the expression is resolved. This resolution
700 -- takes place within the body of the initialization procedure, where
701 -- the discriminant is replaced by its discriminal.
703 if Is_Entity_Name (Pref)
704 and then Ekind (Entity (Pref)) = E_In_Parameter
708 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
709 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
710 -- Analyze_Object_Renaming, and Freeze_Entity.
712 elsif Ada_Version >= Ada_2005
713 and then Is_Entity_Name (Pref)
714 and then Is_Access_Type (Etype (Pref))
715 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
717 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
721 Check_Fully_Declared (Typ, Parent (Pref));
723 end Check_Fully_Declared_Prefix;
725 ------------------------------
726 -- Check_Infinite_Recursion --
727 ------------------------------
729 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
733 function Same_Argument_List return Boolean;
734 -- Check whether list of actuals is identical to list of formals
735 -- of called function (which is also the enclosing scope).
737 ------------------------
738 -- Same_Argument_List --
739 ------------------------
741 function Same_Argument_List return Boolean is
747 if not Is_Entity_Name (Name (N)) then
750 Subp := Entity (Name (N));
753 F := First_Formal (Subp);
754 A := First_Actual (N);
755 while Present (F) and then Present (A) loop
756 if not Is_Entity_Name (A)
757 or else Entity (A) /= F
767 end Same_Argument_List;
769 -- Start of processing for Check_Infinite_Recursion
772 -- Special case, if this is a procedure call and is a call to the
773 -- current procedure with the same argument list, then this is for
774 -- sure an infinite recursion and we insert a call to raise SE.
776 if Is_List_Member (N)
777 and then List_Length (List_Containing (N)) = 1
778 and then Same_Argument_List
781 P : constant Node_Id := Parent (N);
783 if Nkind (P) = N_Handled_Sequence_Of_Statements
784 and then Nkind (Parent (P)) = N_Subprogram_Body
785 and then Is_Empty_List (Declarations (Parent (P)))
787 Error_Msg_N ("!?infinite recursion", N);
788 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
790 Make_Raise_Storage_Error (Sloc (N),
791 Reason => SE_Infinite_Recursion));
797 -- If not that special case, search up tree, quitting if we reach a
798 -- construct (e.g. a conditional) that tells us that this is not a
799 -- case for an infinite recursion warning.
805 -- If no parent, then we were not inside a subprogram, this can for
806 -- example happen when processing certain pragmas in a spec. Just
807 -- return False in this case.
813 -- Done if we get to subprogram body, this is definitely an infinite
814 -- recursion case if we did not find anything to stop us.
816 exit when Nkind (P) = N_Subprogram_Body;
818 -- If appearing in conditional, result is false
820 if Nkind_In (P, N_Or_Else,
827 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
828 and then C /= First (Statements (P))
830 -- If the call is the expression of a return statement and the
831 -- actuals are identical to the formals, it's worth a warning.
832 -- However, we skip this if there is an immediately preceding
833 -- raise statement, since the call is never executed.
835 -- Furthermore, this corresponds to a common idiom:
837 -- function F (L : Thing) return Boolean is
839 -- raise Program_Error;
843 -- for generating a stub function
845 if Nkind (Parent (N)) = N_Simple_Return_Statement
846 and then Same_Argument_List
848 exit when not Is_List_Member (Parent (N));
850 -- OK, return statement is in a statement list, look for raise
856 -- Skip past N_Freeze_Entity nodes generated by expansion
858 Nod := Prev (Parent (N));
860 and then Nkind (Nod) = N_Freeze_Entity
865 -- If no raise statement, give warning
867 exit when Nkind (Nod) /= N_Raise_Statement
869 (Nkind (Nod) not in N_Raise_xxx_Error
870 or else Present (Condition (Nod)));
881 Error_Msg_N ("!?possible infinite recursion", N);
882 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
885 end Check_Infinite_Recursion;
887 -------------------------------
888 -- Check_Initialization_Call --
889 -------------------------------
891 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
892 Typ : constant Entity_Id := Etype (First_Formal (Nam));
894 function Uses_SS (T : Entity_Id) return Boolean;
895 -- Check whether the creation of an object of the type will involve
896 -- use of the secondary stack. If T is a record type, this is true
897 -- if the expression for some component uses the secondary stack, e.g.
898 -- through a call to a function that returns an unconstrained value.
899 -- False if T is controlled, because cleanups occur elsewhere.
905 function Uses_SS (T : Entity_Id) return Boolean is
908 Full_Type : Entity_Id := Underlying_Type (T);
911 -- Normally we want to use the underlying type, but if it's not set
912 -- then continue with T.
914 if not Present (Full_Type) then
918 if Is_Controlled (Full_Type) then
921 elsif Is_Array_Type (Full_Type) then
922 return Uses_SS (Component_Type (Full_Type));
924 elsif Is_Record_Type (Full_Type) then
925 Comp := First_Component (Full_Type);
926 while Present (Comp) loop
927 if Ekind (Comp) = E_Component
928 and then Nkind (Parent (Comp)) = N_Component_Declaration
930 -- The expression for a dynamic component may be rewritten
931 -- as a dereference, so retrieve original node.
933 Expr := Original_Node (Expression (Parent (Comp)));
935 -- Return True if the expression is a call to a function
936 -- (including an attribute function such as Image, or a
937 -- user-defined operator) with a result that requires a
940 if (Nkind (Expr) = N_Function_Call
941 or else Nkind (Expr) in N_Op
942 or else (Nkind (Expr) = N_Attribute_Reference
943 and then Present (Expressions (Expr))))
944 and then Requires_Transient_Scope (Etype (Expr))
948 elsif Uses_SS (Etype (Comp)) then
953 Next_Component (Comp);
963 -- Start of processing for Check_Initialization_Call
966 -- Establish a transient scope if the type needs it
968 if Uses_SS (Typ) then
969 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
971 end Check_Initialization_Call;
973 ---------------------------------------
974 -- Check_No_Direct_Boolean_Operators --
975 ---------------------------------------
977 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
979 if Scope (Entity (N)) = Standard_Standard
980 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
982 -- Restriction only applies to original source code
984 if Comes_From_Source (N) then
985 Check_Restriction (No_Direct_Boolean_Operators, N);
990 Check_Boolean_Operator (N);
992 end Check_No_Direct_Boolean_Operators;
994 ------------------------------
995 -- Check_Parameterless_Call --
996 ------------------------------
998 procedure Check_Parameterless_Call (N : Node_Id) is
1001 function Prefix_Is_Access_Subp return Boolean;
1002 -- If the prefix is of an access_to_subprogram type, the node must be
1003 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1004 -- interpretations are access to subprograms.
1006 ---------------------------
1007 -- Prefix_Is_Access_Subp --
1008 ---------------------------
1010 function Prefix_Is_Access_Subp return Boolean is
1015 -- If the context is an attribute reference that can apply to
1016 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1018 if Nkind (Parent (N)) = N_Attribute_Reference
1019 and then (Attribute_Name (Parent (N)) = Name_Address
1020 or else Attribute_Name (Parent (N)) = Name_Code_Address
1021 or else Attribute_Name (Parent (N)) = Name_Access)
1026 if not Is_Overloaded (N) then
1028 Ekind (Etype (N)) = E_Subprogram_Type
1029 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1031 Get_First_Interp (N, I, It);
1032 while Present (It.Typ) loop
1033 if Ekind (It.Typ) /= E_Subprogram_Type
1034 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1039 Get_Next_Interp (I, It);
1044 end Prefix_Is_Access_Subp;
1046 -- Start of processing for Check_Parameterless_Call
1049 -- Defend against junk stuff if errors already detected
1051 if Total_Errors_Detected /= 0 then
1052 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1054 elsif Nkind (N) in N_Has_Chars
1055 and then Chars (N) in Error_Name_Or_No_Name
1063 -- If the context expects a value, and the name is a procedure, this is
1064 -- most likely a missing 'Access. Don't try to resolve the parameterless
1065 -- call, error will be caught when the outer call is analyzed.
1067 if Is_Entity_Name (N)
1068 and then Ekind (Entity (N)) = E_Procedure
1069 and then not Is_Overloaded (N)
1071 Nkind_In (Parent (N), N_Parameter_Association,
1073 N_Procedure_Call_Statement)
1078 -- Rewrite as call if overloadable entity that is (or could be, in the
1079 -- overloaded case) a function call. If we know for sure that the entity
1080 -- is an enumeration literal, we do not rewrite it.
1082 -- If the entity is the name of an operator, it cannot be a call because
1083 -- operators cannot have default parameters. In this case, this must be
1084 -- a string whose contents coincide with an operator name. Set the kind
1085 -- of the node appropriately.
1087 if (Is_Entity_Name (N)
1088 and then Nkind (N) /= N_Operator_Symbol
1089 and then Is_Overloadable (Entity (N))
1090 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1091 or else Is_Overloaded (N)))
1093 -- Rewrite as call if it is an explicit dereference of an expression of
1094 -- a subprogram access type, and the subprogram type is not that of a
1095 -- procedure or entry.
1098 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1100 -- Rewrite as call if it is a selected component which is a function,
1101 -- this is the case of a call to a protected function (which may be
1102 -- overloaded with other protected operations).
1105 (Nkind (N) = N_Selected_Component
1106 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1108 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1110 and then Is_Overloaded (Selector_Name (N)))))
1112 -- If one of the above three conditions is met, rewrite as call.
1113 -- Apply the rewriting only once.
1116 if Nkind (Parent (N)) /= N_Function_Call
1117 or else N /= Name (Parent (N))
1119 Nam := New_Copy (N);
1121 -- If overloaded, overload set belongs to new copy
1123 Save_Interps (N, Nam);
1125 -- Change node to parameterless function call (note that the
1126 -- Parameter_Associations associations field is left set to Empty,
1127 -- its normal default value since there are no parameters)
1129 Change_Node (N, N_Function_Call);
1131 Set_Sloc (N, Sloc (Nam));
1135 elsif Nkind (N) = N_Parameter_Association then
1136 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1138 elsif Nkind (N) = N_Operator_Symbol then
1139 Change_Operator_Symbol_To_String_Literal (N);
1140 Set_Is_Overloaded (N, False);
1141 Set_Etype (N, Any_String);
1143 end Check_Parameterless_Call;
1145 -----------------------------
1146 -- Is_Definite_Access_Type --
1147 -----------------------------
1149 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1150 Btyp : constant Entity_Id := Base_Type (E);
1152 return Ekind (Btyp) = E_Access_Type
1153 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1154 and then Comes_From_Source (Btyp));
1155 end Is_Definite_Access_Type;
1157 ----------------------
1158 -- Is_Predefined_Op --
1159 ----------------------
1161 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1163 -- Predefined operators are intrinsic subprograms
1165 if not Is_Intrinsic_Subprogram (Nam) then
1169 -- A call to a back-end builtin is never a predefined operator
1171 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1175 return not Is_Generic_Instance (Nam)
1176 and then Chars (Nam) in Any_Operator_Name
1177 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1178 end Is_Predefined_Op;
1180 -----------------------------
1181 -- Make_Call_Into_Operator --
1182 -----------------------------
1184 procedure Make_Call_Into_Operator
1189 Op_Name : constant Name_Id := Chars (Op_Id);
1190 Act1 : Node_Id := First_Actual (N);
1191 Act2 : Node_Id := Next_Actual (Act1);
1192 Error : Boolean := False;
1193 Func : constant Entity_Id := Entity (Name (N));
1194 Is_Binary : constant Boolean := Present (Act2);
1196 Opnd_Type : Entity_Id;
1197 Orig_Type : Entity_Id := Empty;
1200 type Kind_Test is access function (E : Entity_Id) return Boolean;
1202 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1203 -- If the operand is not universal, and the operator is given by an
1204 -- expanded name, verify that the operand has an interpretation with a
1205 -- type defined in the given scope of the operator.
1207 function Type_In_P (Test : Kind_Test) return Entity_Id;
1208 -- Find a type of the given class in package Pack that contains the
1211 ---------------------------
1212 -- Operand_Type_In_Scope --
1213 ---------------------------
1215 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1216 Nod : constant Node_Id := Right_Opnd (Op_Node);
1221 if not Is_Overloaded (Nod) then
1222 return Scope (Base_Type (Etype (Nod))) = S;
1225 Get_First_Interp (Nod, I, It);
1226 while Present (It.Typ) loop
1227 if Scope (Base_Type (It.Typ)) = S then
1231 Get_Next_Interp (I, It);
1236 end Operand_Type_In_Scope;
1242 function Type_In_P (Test : Kind_Test) return Entity_Id is
1245 function In_Decl return Boolean;
1246 -- Verify that node is not part of the type declaration for the
1247 -- candidate type, which would otherwise be invisible.
1253 function In_Decl return Boolean is
1254 Decl_Node : constant Node_Id := Parent (E);
1260 if Etype (E) = Any_Type then
1263 elsif No (Decl_Node) then
1268 and then Nkind (N2) /= N_Compilation_Unit
1270 if N2 = Decl_Node then
1281 -- Start of processing for Type_In_P
1284 -- If the context type is declared in the prefix package, this is the
1285 -- desired base type.
1287 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1288 return Base_Type (Typ);
1291 E := First_Entity (Pack);
1292 while Present (E) loop
1294 and then not In_Decl
1306 -- Start of processing for Make_Call_Into_Operator
1309 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1314 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1315 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1316 Save_Interps (Act1, Left_Opnd (Op_Node));
1317 Save_Interps (Act2, Right_Opnd (Op_Node));
1318 Act1 := Left_Opnd (Op_Node);
1319 Act2 := Right_Opnd (Op_Node);
1324 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1325 Save_Interps (Act1, Right_Opnd (Op_Node));
1326 Act1 := Right_Opnd (Op_Node);
1329 -- If the operator is denoted by an expanded name, and the prefix is
1330 -- not Standard, but the operator is a predefined one whose scope is
1331 -- Standard, then this is an implicit_operator, inserted as an
1332 -- interpretation by the procedure of the same name. This procedure
1333 -- overestimates the presence of implicit operators, because it does
1334 -- not examine the type of the operands. Verify now that the operand
1335 -- type appears in the given scope. If right operand is universal,
1336 -- check the other operand. In the case of concatenation, either
1337 -- argument can be the component type, so check the type of the result.
1338 -- If both arguments are literals, look for a type of the right kind
1339 -- defined in the given scope. This elaborate nonsense is brought to
1340 -- you courtesy of b33302a. The type itself must be frozen, so we must
1341 -- find the type of the proper class in the given scope.
1343 -- A final wrinkle is the multiplication operator for fixed point types,
1344 -- which is defined in Standard only, and not in the scope of the
1345 -- fixed point type itself.
1347 if Nkind (Name (N)) = N_Expanded_Name then
1348 Pack := Entity (Prefix (Name (N)));
1350 -- If the entity being called is defined in the given package, it is
1351 -- a renaming of a predefined operator, and known to be legal.
1353 if Scope (Entity (Name (N))) = Pack
1354 and then Pack /= Standard_Standard
1358 -- Visibility does not need to be checked in an instance: if the
1359 -- operator was not visible in the generic it has been diagnosed
1360 -- already, else there is an implicit copy of it in the instance.
1362 elsif In_Instance then
1365 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1366 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1367 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1369 if Pack /= Standard_Standard then
1373 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1376 elsif Ada_Version >= Ada_2005
1377 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1378 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1383 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1385 if Op_Name = Name_Op_Concat then
1386 Opnd_Type := Base_Type (Typ);
1388 elsif (Scope (Opnd_Type) = Standard_Standard
1390 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1392 and then not Comes_From_Source (Opnd_Type))
1394 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1397 if Scope (Opnd_Type) = Standard_Standard then
1399 -- Verify that the scope contains a type that corresponds to
1400 -- the given literal. Optimize the case where Pack is Standard.
1402 if Pack /= Standard_Standard then
1404 if Opnd_Type = Universal_Integer then
1405 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1407 elsif Opnd_Type = Universal_Real then
1408 Orig_Type := Type_In_P (Is_Real_Type'Access);
1410 elsif Opnd_Type = Any_String then
1411 Orig_Type := Type_In_P (Is_String_Type'Access);
1413 elsif Opnd_Type = Any_Access then
1414 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1416 elsif Opnd_Type = Any_Composite then
1417 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1419 if Present (Orig_Type) then
1420 if Has_Private_Component (Orig_Type) then
1423 Set_Etype (Act1, Orig_Type);
1426 Set_Etype (Act2, Orig_Type);
1435 Error := No (Orig_Type);
1438 elsif Ekind (Opnd_Type) = E_Allocator_Type
1439 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1443 -- If the type is defined elsewhere, and the operator is not
1444 -- defined in the given scope (by a renaming declaration, e.g.)
1445 -- then this is an error as well. If an extension of System is
1446 -- present, and the type may be defined there, Pack must be
1449 elsif Scope (Opnd_Type) /= Pack
1450 and then Scope (Op_Id) /= Pack
1451 and then (No (System_Aux_Id)
1452 or else Scope (Opnd_Type) /= System_Aux_Id
1453 or else Pack /= Scope (System_Aux_Id))
1455 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1458 Error := not Operand_Type_In_Scope (Pack);
1461 elsif Pack = Standard_Standard
1462 and then not Operand_Type_In_Scope (Standard_Standard)
1469 Error_Msg_Node_2 := Pack;
1471 ("& not declared in&", N, Selector_Name (Name (N)));
1472 Set_Etype (N, Any_Type);
1475 -- Detect a mismatch between the context type and the result type
1476 -- in the named package, which is otherwise not detected if the
1477 -- operands are universal. Check is only needed if source entity is
1478 -- an operator, not a function that renames an operator.
1480 elsif Nkind (Parent (N)) /= N_Type_Conversion
1481 and then Ekind (Entity (Name (N))) = E_Operator
1482 and then Is_Numeric_Type (Typ)
1483 and then not Is_Universal_Numeric_Type (Typ)
1484 and then Scope (Base_Type (Typ)) /= Pack
1485 and then not In_Instance
1487 if Is_Fixed_Point_Type (Typ)
1488 and then (Op_Name = Name_Op_Multiply
1490 Op_Name = Name_Op_Divide)
1492 -- Already checked above
1496 -- Operator may be defined in an extension of System
1498 elsif Present (System_Aux_Id)
1499 and then Scope (Opnd_Type) = System_Aux_Id
1504 -- Could we use Wrong_Type here??? (this would require setting
1505 -- Etype (N) to the actual type found where Typ was expected).
1507 Error_Msg_NE ("expect }", N, Typ);
1512 Set_Chars (Op_Node, Op_Name);
1514 if not Is_Private_Type (Etype (N)) then
1515 Set_Etype (Op_Node, Base_Type (Etype (N)));
1517 Set_Etype (Op_Node, Etype (N));
1520 -- If this is a call to a function that renames a predefined equality,
1521 -- the renaming declaration provides a type that must be used to
1522 -- resolve the operands. This must be done now because resolution of
1523 -- the equality node will not resolve any remaining ambiguity, and it
1524 -- assumes that the first operand is not overloaded.
1526 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1527 and then Ekind (Func) = E_Function
1528 and then Is_Overloaded (Act1)
1530 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1531 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1534 Set_Entity (Op_Node, Op_Id);
1535 Generate_Reference (Op_Id, N, ' ');
1537 -- Do rewrite setting Comes_From_Source on the result if the original
1538 -- call came from source. Although it is not strictly the case that the
1539 -- operator as such comes from the source, logically it corresponds
1540 -- exactly to the function call in the source, so it should be marked
1541 -- this way (e.g. to make sure that validity checks work fine).
1544 CS : constant Boolean := Comes_From_Source (N);
1546 Rewrite (N, Op_Node);
1547 Set_Comes_From_Source (N, CS);
1550 -- If this is an arithmetic operator and the result type is private,
1551 -- the operands and the result must be wrapped in conversion to
1552 -- expose the underlying numeric type and expand the proper checks,
1553 -- e.g. on division.
1555 if Is_Private_Type (Typ) then
1557 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1558 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1559 Resolve_Intrinsic_Operator (N, Typ);
1561 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1562 Resolve_Intrinsic_Unary_Operator (N, Typ);
1570 end Make_Call_Into_Operator;
1576 function Operator_Kind
1578 Is_Binary : Boolean) return Node_Kind
1584 if Op_Name = Name_Op_And then
1586 elsif Op_Name = Name_Op_Or then
1588 elsif Op_Name = Name_Op_Xor then
1590 elsif Op_Name = Name_Op_Eq then
1592 elsif Op_Name = Name_Op_Ne then
1594 elsif Op_Name = Name_Op_Lt then
1596 elsif Op_Name = Name_Op_Le then
1598 elsif Op_Name = Name_Op_Gt then
1600 elsif Op_Name = Name_Op_Ge then
1602 elsif Op_Name = Name_Op_Add then
1604 elsif Op_Name = Name_Op_Subtract then
1605 Kind := N_Op_Subtract;
1606 elsif Op_Name = Name_Op_Concat then
1607 Kind := N_Op_Concat;
1608 elsif Op_Name = Name_Op_Multiply then
1609 Kind := N_Op_Multiply;
1610 elsif Op_Name = Name_Op_Divide then
1611 Kind := N_Op_Divide;
1612 elsif Op_Name = Name_Op_Mod then
1614 elsif Op_Name = Name_Op_Rem then
1616 elsif Op_Name = Name_Op_Expon then
1619 raise Program_Error;
1625 if Op_Name = Name_Op_Add then
1627 elsif Op_Name = Name_Op_Subtract then
1629 elsif Op_Name = Name_Op_Abs then
1631 elsif Op_Name = Name_Op_Not then
1634 raise Program_Error;
1641 ----------------------------
1642 -- Preanalyze_And_Resolve --
1643 ----------------------------
1645 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1646 Save_Full_Analysis : constant Boolean := Full_Analysis;
1649 Full_Analysis := False;
1650 Expander_Mode_Save_And_Set (False);
1652 -- We suppress all checks for this analysis, since the checks will
1653 -- be applied properly, and in the right location, when the default
1654 -- expression is reanalyzed and reexpanded later on.
1656 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1658 Expander_Mode_Restore;
1659 Full_Analysis := Save_Full_Analysis;
1660 end Preanalyze_And_Resolve;
1662 -- Version without context type
1664 procedure Preanalyze_And_Resolve (N : Node_Id) is
1665 Save_Full_Analysis : constant Boolean := Full_Analysis;
1668 Full_Analysis := False;
1669 Expander_Mode_Save_And_Set (False);
1672 Resolve (N, Etype (N), Suppress => All_Checks);
1674 Expander_Mode_Restore;
1675 Full_Analysis := Save_Full_Analysis;
1676 end Preanalyze_And_Resolve;
1678 ----------------------------------
1679 -- Replace_Actual_Discriminants --
1680 ----------------------------------
1682 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1683 Loc : constant Source_Ptr := Sloc (N);
1684 Tsk : Node_Id := Empty;
1686 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1692 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1696 if Nkind (Nod) = N_Identifier then
1697 Ent := Entity (Nod);
1700 and then Ekind (Ent) = E_Discriminant
1703 Make_Selected_Component (Loc,
1704 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1705 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1707 Set_Etype (Nod, Etype (Ent));
1715 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1717 -- Start of processing for Replace_Actual_Discriminants
1720 if not Expander_Active then
1724 if Nkind (Name (N)) = N_Selected_Component then
1725 Tsk := Prefix (Name (N));
1727 elsif Nkind (Name (N)) = N_Indexed_Component then
1728 Tsk := Prefix (Prefix (Name (N)));
1734 Replace_Discrs (Default);
1736 end Replace_Actual_Discriminants;
1742 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1743 Ambiguous : Boolean := False;
1744 Ctx_Type : Entity_Id := Typ;
1745 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1746 Err_Type : Entity_Id := Empty;
1747 Found : Boolean := False;
1750 I1 : Interp_Index := 0; -- prevent junk warning
1753 Seen : Entity_Id := Empty; -- prevent junk warning
1755 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1756 -- Determine whether a node comes from a predefined library unit or
1759 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1760 -- Try and fix up a literal so that it matches its expected type. New
1761 -- literals are manufactured if necessary to avoid cascaded errors.
1763 procedure Report_Ambiguous_Argument;
1764 -- Additional diagnostics when an ambiguous call has an ambiguous
1765 -- argument (typically a controlling actual).
1767 procedure Resolution_Failed;
1768 -- Called when attempt at resolving current expression fails
1770 ------------------------------------
1771 -- Comes_From_Predefined_Lib_Unit --
1772 -------------------------------------
1774 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1777 Sloc (Nod) = Standard_Location
1778 or else Is_Predefined_File_Name (Unit_File_Name (
1779 Get_Source_Unit (Sloc (Nod))));
1780 end Comes_From_Predefined_Lib_Unit;
1782 --------------------
1783 -- Patch_Up_Value --
1784 --------------------
1786 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1788 if Nkind (N) = N_Integer_Literal
1789 and then Is_Real_Type (Typ)
1792 Make_Real_Literal (Sloc (N),
1793 Realval => UR_From_Uint (Intval (N))));
1794 Set_Etype (N, Universal_Real);
1795 Set_Is_Static_Expression (N);
1797 elsif Nkind (N) = N_Real_Literal
1798 and then Is_Integer_Type (Typ)
1801 Make_Integer_Literal (Sloc (N),
1802 Intval => UR_To_Uint (Realval (N))));
1803 Set_Etype (N, Universal_Integer);
1804 Set_Is_Static_Expression (N);
1806 elsif Nkind (N) = N_String_Literal
1807 and then Is_Character_Type (Typ)
1809 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1811 Make_Character_Literal (Sloc (N),
1813 Char_Literal_Value =>
1814 UI_From_Int (Character'Pos ('A'))));
1815 Set_Etype (N, Any_Character);
1816 Set_Is_Static_Expression (N);
1818 elsif Nkind (N) /= N_String_Literal
1819 and then Is_String_Type (Typ)
1822 Make_String_Literal (Sloc (N),
1823 Strval => End_String));
1825 elsif Nkind (N) = N_Range then
1826 Patch_Up_Value (Low_Bound (N), Typ);
1827 Patch_Up_Value (High_Bound (N), Typ);
1831 -------------------------------
1832 -- Report_Ambiguous_Argument --
1833 -------------------------------
1835 procedure Report_Ambiguous_Argument is
1836 Arg : constant Node_Id := First (Parameter_Associations (N));
1841 if Nkind (Arg) = N_Function_Call
1842 and then Is_Entity_Name (Name (Arg))
1843 and then Is_Overloaded (Name (Arg))
1845 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1847 -- Could use comments on what is going on here ???
1849 Get_First_Interp (Name (Arg), I, It);
1850 while Present (It.Nam) loop
1851 Error_Msg_Sloc := Sloc (It.Nam);
1853 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1854 Error_Msg_N ("interpretation (inherited) #!", Arg);
1856 Error_Msg_N ("interpretation #!", Arg);
1859 Get_Next_Interp (I, It);
1862 end Report_Ambiguous_Argument;
1864 -----------------------
1865 -- Resolution_Failed --
1866 -----------------------
1868 procedure Resolution_Failed is
1870 Patch_Up_Value (N, Typ);
1872 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1873 Set_Is_Overloaded (N, False);
1875 -- The caller will return without calling the expander, so we need
1876 -- to set the analyzed flag. Note that it is fine to set Analyzed
1877 -- to True even if we are in the middle of a shallow analysis,
1878 -- (see the spec of sem for more details) since this is an error
1879 -- situation anyway, and there is no point in repeating the
1880 -- analysis later (indeed it won't work to repeat it later, since
1881 -- we haven't got a clear resolution of which entity is being
1884 Set_Analyzed (N, True);
1886 end Resolution_Failed;
1888 -- Start of processing for Resolve
1895 -- Access attribute on remote subprogram cannot be used for
1896 -- a non-remote access-to-subprogram type.
1898 if Nkind (N) = N_Attribute_Reference
1899 and then (Attribute_Name (N) = Name_Access
1900 or else Attribute_Name (N) = Name_Unrestricted_Access
1901 or else Attribute_Name (N) = Name_Unchecked_Access)
1902 and then Comes_From_Source (N)
1903 and then Is_Entity_Name (Prefix (N))
1904 and then Is_Subprogram (Entity (Prefix (N)))
1905 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1906 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1909 ("prefix must statically denote a non-remote subprogram", N);
1912 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1914 -- If the context is a Remote_Access_To_Subprogram, access attributes
1915 -- must be resolved with the corresponding fat pointer. There is no need
1916 -- to check for the attribute name since the return type of an
1917 -- attribute is never a remote type.
1919 if Nkind (N) = N_Attribute_Reference
1920 and then Comes_From_Source (N)
1921 and then (Is_Remote_Call_Interface (Typ)
1922 or else Is_Remote_Types (Typ))
1925 Attr : constant Attribute_Id :=
1926 Get_Attribute_Id (Attribute_Name (N));
1927 Pref : constant Node_Id := Prefix (N);
1930 Is_Remote : Boolean := True;
1933 -- Check that Typ is a remote access-to-subprogram type
1935 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1937 -- Prefix (N) must statically denote a remote subprogram
1938 -- declared in a package specification.
1940 if Attr = Attribute_Access then
1941 Decl := Unit_Declaration_Node (Entity (Pref));
1943 if Nkind (Decl) = N_Subprogram_Body then
1944 Spec := Corresponding_Spec (Decl);
1946 if not No (Spec) then
1947 Decl := Unit_Declaration_Node (Spec);
1951 Spec := Parent (Decl);
1953 if not Is_Entity_Name (Prefix (N))
1954 or else Nkind (Spec) /= N_Package_Specification
1956 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1960 ("prefix must statically denote a remote subprogram ",
1965 -- If we are generating code for a distributed program.
1966 -- perform semantic checks against the corresponding
1969 if (Attr = Attribute_Access
1970 or else Attr = Attribute_Unchecked_Access
1971 or else Attr = Attribute_Unrestricted_Access)
1972 and then Expander_Active
1973 and then Get_PCS_Name /= Name_No_DSA
1975 Check_Subtype_Conformant
1976 (New_Id => Entity (Prefix (N)),
1977 Old_Id => Designated_Type
1978 (Corresponding_Remote_Type (Typ)),
1982 Process_Remote_AST_Attribute (N, Typ);
1989 Debug_A_Entry ("resolving ", N);
1991 if Comes_From_Source (N) then
1992 if Is_Fixed_Point_Type (Typ) then
1993 Check_Restriction (No_Fixed_Point, N);
1995 elsif Is_Floating_Point_Type (Typ)
1996 and then Typ /= Universal_Real
1997 and then Typ /= Any_Real
1999 Check_Restriction (No_Floating_Point, N);
2003 -- Return if already analyzed
2005 if Analyzed (N) then
2006 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2009 -- Return if type = Any_Type (previous error encountered)
2011 elsif Etype (N) = Any_Type then
2012 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2016 Check_Parameterless_Call (N);
2018 -- If not overloaded, then we know the type, and all that needs doing
2019 -- is to check that this type is compatible with the context.
2021 if not Is_Overloaded (N) then
2022 Found := Covers (Typ, Etype (N));
2023 Expr_Type := Etype (N);
2025 -- In the overloaded case, we must select the interpretation that
2026 -- is compatible with the context (i.e. the type passed to Resolve)
2029 -- Loop through possible interpretations
2031 Get_First_Interp (N, I, It);
2032 Interp_Loop : while Present (It.Typ) loop
2034 -- We are only interested in interpretations that are compatible
2035 -- with the expected type, any other interpretations are ignored.
2037 if not Covers (Typ, It.Typ) then
2038 if Debug_Flag_V then
2039 Write_Str (" interpretation incompatible with context");
2044 -- Skip the current interpretation if it is disabled by an
2045 -- abstract operator. This action is performed only when the
2046 -- type against which we are resolving is the same as the
2047 -- type of the interpretation.
2049 if Ada_Version >= Ada_2005
2050 and then It.Typ = Typ
2051 and then Typ /= Universal_Integer
2052 and then Typ /= Universal_Real
2053 and then Present (It.Abstract_Op)
2058 -- First matching interpretation
2064 Expr_Type := It.Typ;
2066 -- Matching interpretation that is not the first, maybe an
2067 -- error, but there are some cases where preference rules are
2068 -- used to choose between the two possibilities. These and
2069 -- some more obscure cases are handled in Disambiguate.
2072 -- If the current statement is part of a predefined library
2073 -- unit, then all interpretations which come from user level
2074 -- packages should not be considered.
2077 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2082 Error_Msg_Sloc := Sloc (Seen);
2083 It1 := Disambiguate (N, I1, I, Typ);
2085 -- Disambiguation has succeeded. Skip the remaining
2088 if It1 /= No_Interp then
2090 Expr_Type := It1.Typ;
2092 while Present (It.Typ) loop
2093 Get_Next_Interp (I, It);
2097 -- Before we issue an ambiguity complaint, check for
2098 -- the case of a subprogram call where at least one
2099 -- of the arguments is Any_Type, and if so, suppress
2100 -- the message, since it is a cascaded error.
2102 if Nkind_In (N, N_Function_Call,
2103 N_Procedure_Call_Statement)
2110 A := First_Actual (N);
2111 while Present (A) loop
2114 if Nkind (E) = N_Parameter_Association then
2115 E := Explicit_Actual_Parameter (E);
2118 if Etype (E) = Any_Type then
2119 if Debug_Flag_V then
2120 Write_Str ("Any_Type in call");
2131 elsif Nkind (N) in N_Binary_Op
2132 and then (Etype (Left_Opnd (N)) = Any_Type
2133 or else Etype (Right_Opnd (N)) = Any_Type)
2137 elsif Nkind (N) in N_Unary_Op
2138 and then Etype (Right_Opnd (N)) = Any_Type
2143 -- Not that special case, so issue message using the
2144 -- flag Ambiguous to control printing of the header
2145 -- message only at the start of an ambiguous set.
2147 if not Ambiguous then
2148 if Nkind (N) = N_Function_Call
2149 and then Nkind (Name (N)) = N_Explicit_Dereference
2152 ("ambiguous expression "
2153 & "(cannot resolve indirect call)!", N);
2155 Error_Msg_NE -- CODEFIX
2156 ("ambiguous expression (cannot resolve&)!",
2162 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2164 ("\\possible interpretation (inherited)#!", N);
2166 Error_Msg_N -- CODEFIX
2167 ("\\possible interpretation#!", N);
2171 (N, N_Procedure_Call_Statement, N_Function_Call)
2172 and then Present (Parameter_Associations (N))
2174 Report_Ambiguous_Argument;
2178 Error_Msg_Sloc := Sloc (It.Nam);
2180 -- By default, the error message refers to the candidate
2181 -- interpretation. But if it is a predefined operator, it
2182 -- is implicitly declared at the declaration of the type
2183 -- of the operand. Recover the sloc of that declaration
2184 -- for the error message.
2186 if Nkind (N) in N_Op
2187 and then Scope (It.Nam) = Standard_Standard
2188 and then not Is_Overloaded (Right_Opnd (N))
2189 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2192 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2194 if Comes_From_Source (Err_Type)
2195 and then Present (Parent (Err_Type))
2197 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2200 elsif Nkind (N) in N_Binary_Op
2201 and then Scope (It.Nam) = Standard_Standard
2202 and then not Is_Overloaded (Left_Opnd (N))
2203 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2206 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2208 if Comes_From_Source (Err_Type)
2209 and then Present (Parent (Err_Type))
2211 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2214 -- If this is an indirect call, use the subprogram_type
2215 -- in the message, to have a meaningful location.
2216 -- Also indicate if this is an inherited operation,
2217 -- created by a type declaration.
2219 elsif Nkind (N) = N_Function_Call
2220 and then Nkind (Name (N)) = N_Explicit_Dereference
2221 and then Is_Type (It.Nam)
2225 Sloc (Associated_Node_For_Itype (Err_Type));
2230 if Nkind (N) in N_Op
2231 and then Scope (It.Nam) = Standard_Standard
2232 and then Present (Err_Type)
2234 -- Special-case the message for universal_fixed
2235 -- operators, which are not declared with the type
2236 -- of the operand, but appear forever in Standard.
2238 if It.Typ = Universal_Fixed
2239 and then Scope (It.Nam) = Standard_Standard
2242 ("\\possible interpretation as " &
2243 "universal_fixed operation " &
2244 "(RM 4.5.5 (19))", N);
2247 ("\\possible interpretation (predefined)#!", N);
2251 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2254 ("\\possible interpretation (inherited)#!", N);
2256 Error_Msg_N -- CODEFIX
2257 ("\\possible interpretation#!", N);
2263 -- We have a matching interpretation, Expr_Type is the type
2264 -- from this interpretation, and Seen is the entity.
2266 -- For an operator, just set the entity name. The type will be
2267 -- set by the specific operator resolution routine.
2269 if Nkind (N) in N_Op then
2270 Set_Entity (N, Seen);
2271 Generate_Reference (Seen, N);
2273 elsif Nkind (N) = N_Case_Expression then
2274 Set_Etype (N, Expr_Type);
2276 elsif Nkind (N) = N_Character_Literal then
2277 Set_Etype (N, Expr_Type);
2279 elsif Nkind (N) = N_Conditional_Expression then
2280 Set_Etype (N, Expr_Type);
2282 -- For an explicit dereference, attribute reference, range,
2283 -- short-circuit form (which is not an operator node), or call
2284 -- with a name that is an explicit dereference, there is
2285 -- nothing to be done at this point.
2287 elsif Nkind_In (N, N_Explicit_Dereference,
2288 N_Attribute_Reference,
2290 N_Indexed_Component,
2293 N_Selected_Component,
2295 or else Nkind (Name (N)) = N_Explicit_Dereference
2299 -- For procedure or function calls, set the type of the name,
2300 -- and also the entity pointer for the prefix.
2302 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2303 and then Is_Entity_Name (Name (N))
2305 Set_Etype (Name (N), Expr_Type);
2306 Set_Entity (Name (N), Seen);
2307 Generate_Reference (Seen, Name (N));
2309 elsif Nkind (N) = N_Function_Call
2310 and then Nkind (Name (N)) = N_Selected_Component
2312 Set_Etype (Name (N), Expr_Type);
2313 Set_Entity (Selector_Name (Name (N)), Seen);
2314 Generate_Reference (Seen, Selector_Name (Name (N)));
2316 -- For all other cases, just set the type of the Name
2319 Set_Etype (Name (N), Expr_Type);
2326 -- Move to next interpretation
2328 exit Interp_Loop when No (It.Typ);
2330 Get_Next_Interp (I, It);
2331 end loop Interp_Loop;
2334 -- At this stage Found indicates whether or not an acceptable
2335 -- interpretation exists. If not, then we have an error, except that if
2336 -- the context is Any_Type as a result of some other error, then we
2337 -- suppress the error report.
2340 if Typ /= Any_Type then
2342 -- If type we are looking for is Void, then this is the procedure
2343 -- call case, and the error is simply that what we gave is not a
2344 -- procedure name (we think of procedure calls as expressions with
2345 -- types internally, but the user doesn't think of them this way!)
2347 if Typ = Standard_Void_Type then
2349 -- Special case message if function used as a procedure
2351 if Nkind (N) = N_Procedure_Call_Statement
2352 and then Is_Entity_Name (Name (N))
2353 and then Ekind (Entity (Name (N))) = E_Function
2356 ("cannot use function & in a procedure call",
2357 Name (N), Entity (Name (N)));
2359 -- Otherwise give general message (not clear what cases this
2360 -- covers, but no harm in providing for them!)
2363 Error_Msg_N ("expect procedure name in procedure call", N);
2368 -- Otherwise we do have a subexpression with the wrong type
2370 -- Check for the case of an allocator which uses an access type
2371 -- instead of the designated type. This is a common error and we
2372 -- specialize the message, posting an error on the operand of the
2373 -- allocator, complaining that we expected the designated type of
2376 elsif Nkind (N) = N_Allocator
2377 and then Ekind (Typ) in Access_Kind
2378 and then Ekind (Etype (N)) in Access_Kind
2379 and then Designated_Type (Etype (N)) = Typ
2381 Wrong_Type (Expression (N), Designated_Type (Typ));
2384 -- Check for view mismatch on Null in instances, for which the
2385 -- view-swapping mechanism has no identifier.
2387 elsif (In_Instance or else In_Inlined_Body)
2388 and then (Nkind (N) = N_Null)
2389 and then Is_Private_Type (Typ)
2390 and then Is_Access_Type (Full_View (Typ))
2392 Resolve (N, Full_View (Typ));
2396 -- Check for an aggregate. Sometimes we can get bogus aggregates
2397 -- from misuse of parentheses, and we are about to complain about
2398 -- the aggregate without even looking inside it.
2400 -- Instead, if we have an aggregate of type Any_Composite, then
2401 -- analyze and resolve the component fields, and then only issue
2402 -- another message if we get no errors doing this (otherwise
2403 -- assume that the errors in the aggregate caused the problem).
2405 elsif Nkind (N) = N_Aggregate
2406 and then Etype (N) = Any_Composite
2408 -- Disable expansion in any case. If there is a type mismatch
2409 -- it may be fatal to try to expand the aggregate. The flag
2410 -- would otherwise be set to false when the error is posted.
2412 Expander_Active := False;
2415 procedure Check_Aggr (Aggr : Node_Id);
2416 -- Check one aggregate, and set Found to True if we have a
2417 -- definite error in any of its elements
2419 procedure Check_Elmt (Aelmt : Node_Id);
2420 -- Check one element of aggregate and set Found to True if
2421 -- we definitely have an error in the element.
2427 procedure Check_Aggr (Aggr : Node_Id) is
2431 if Present (Expressions (Aggr)) then
2432 Elmt := First (Expressions (Aggr));
2433 while Present (Elmt) loop
2439 if Present (Component_Associations (Aggr)) then
2440 Elmt := First (Component_Associations (Aggr));
2441 while Present (Elmt) loop
2443 -- If this is a default-initialized component, then
2444 -- there is nothing to check. The box will be
2445 -- replaced by the appropriate call during late
2448 if not Box_Present (Elmt) then
2449 Check_Elmt (Expression (Elmt));
2461 procedure Check_Elmt (Aelmt : Node_Id) is
2463 -- If we have a nested aggregate, go inside it (to
2464 -- attempt a naked analyze-resolve of the aggregate
2465 -- can cause undesirable cascaded errors). Do not
2466 -- resolve expression if it needs a type from context,
2467 -- as for integer * fixed expression.
2469 if Nkind (Aelmt) = N_Aggregate then
2475 if not Is_Overloaded (Aelmt)
2476 and then Etype (Aelmt) /= Any_Fixed
2481 if Etype (Aelmt) = Any_Type then
2492 -- If an error message was issued already, Found got reset
2493 -- to True, so if it is still False, issue the standard
2494 -- Wrong_Type message.
2497 if Is_Overloaded (N)
2498 and then Nkind (N) = N_Function_Call
2501 Subp_Name : Node_Id;
2503 if Is_Entity_Name (Name (N)) then
2504 Subp_Name := Name (N);
2506 elsif Nkind (Name (N)) = N_Selected_Component then
2508 -- Protected operation: retrieve operation name
2510 Subp_Name := Selector_Name (Name (N));
2512 raise Program_Error;
2515 Error_Msg_Node_2 := Typ;
2516 Error_Msg_NE ("no visible interpretation of&" &
2517 " matches expected type&", N, Subp_Name);
2520 if All_Errors_Mode then
2522 Index : Interp_Index;
2526 Error_Msg_N ("\\possible interpretations:", N);
2528 Get_First_Interp (Name (N), Index, It);
2529 while Present (It.Nam) loop
2530 Error_Msg_Sloc := Sloc (It.Nam);
2531 Error_Msg_Node_2 := It.Nam;
2533 ("\\ type& for & declared#", N, It.Typ);
2534 Get_Next_Interp (Index, It);
2539 Error_Msg_N ("\use -gnatf for details", N);
2542 Wrong_Type (N, Typ);
2550 -- Test if we have more than one interpretation for the context
2552 elsif Ambiguous then
2556 -- Here we have an acceptable interpretation for the context
2559 -- Propagate type information and normalize tree for various
2560 -- predefined operations. If the context only imposes a class of
2561 -- types, rather than a specific type, propagate the actual type
2564 if Typ = Any_Integer
2565 or else Typ = Any_Boolean
2566 or else Typ = Any_Modular
2567 or else Typ = Any_Real
2568 or else Typ = Any_Discrete
2570 Ctx_Type := Expr_Type;
2572 -- Any_Fixed is legal in a real context only if a specific
2573 -- fixed point type is imposed. If Norman Cohen can be
2574 -- confused by this, it deserves a separate message.
2577 and then Expr_Type = Any_Fixed
2579 Error_Msg_N ("illegal context for mixed mode operation", N);
2580 Set_Etype (N, Universal_Real);
2581 Ctx_Type := Universal_Real;
2585 -- A user-defined operator is transformed into a function call at
2586 -- this point, so that further processing knows that operators are
2587 -- really operators (i.e. are predefined operators). User-defined
2588 -- operators that are intrinsic are just renamings of the predefined
2589 -- ones, and need not be turned into calls either, but if they rename
2590 -- a different operator, we must transform the node accordingly.
2591 -- Instantiations of Unchecked_Conversion are intrinsic but are
2592 -- treated as functions, even if given an operator designator.
2594 if Nkind (N) in N_Op
2595 and then Present (Entity (N))
2596 and then Ekind (Entity (N)) /= E_Operator
2599 if not Is_Predefined_Op (Entity (N)) then
2600 Rewrite_Operator_As_Call (N, Entity (N));
2602 elsif Present (Alias (Entity (N)))
2604 Nkind (Parent (Parent (Entity (N)))) =
2605 N_Subprogram_Renaming_Declaration
2607 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2609 -- If the node is rewritten, it will be fully resolved in
2610 -- Rewrite_Renamed_Operator.
2612 if Analyzed (N) then
2618 case N_Subexpr'(Nkind (N)) is
2620 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2622 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2624 when N_Short_Circuit
2625 => Resolve_Short_Circuit (N, Ctx_Type);
2627 when N_Attribute_Reference
2628 => Resolve_Attribute (N, Ctx_Type);
2630 when N_Case_Expression
2631 => Resolve_Case_Expression (N, Ctx_Type);
2633 when N_Character_Literal
2634 => Resolve_Character_Literal (N, Ctx_Type);
2636 when N_Conditional_Expression
2637 => Resolve_Conditional_Expression (N, Ctx_Type);
2639 when N_Expanded_Name
2640 => Resolve_Entity_Name (N, Ctx_Type);
2642 when N_Explicit_Dereference
2643 => Resolve_Explicit_Dereference (N, Ctx_Type);
2645 when N_Expression_With_Actions
2646 => Resolve_Expression_With_Actions (N, Ctx_Type);
2648 when N_Extension_Aggregate
2649 => Resolve_Extension_Aggregate (N, Ctx_Type);
2651 when N_Function_Call
2652 => Resolve_Call (N, Ctx_Type);
2655 => Resolve_Entity_Name (N, Ctx_Type);
2657 when N_Indexed_Component
2658 => Resolve_Indexed_Component (N, Ctx_Type);
2660 when N_Integer_Literal
2661 => Resolve_Integer_Literal (N, Ctx_Type);
2663 when N_Membership_Test
2664 => Resolve_Membership_Op (N, Ctx_Type);
2666 when N_Null => Resolve_Null (N, Ctx_Type);
2668 when N_Op_And | N_Op_Or | N_Op_Xor
2669 => Resolve_Logical_Op (N, Ctx_Type);
2671 when N_Op_Eq | N_Op_Ne
2672 => Resolve_Equality_Op (N, Ctx_Type);
2674 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2675 => Resolve_Comparison_Op (N, Ctx_Type);
2677 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2679 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2680 N_Op_Divide | N_Op_Mod | N_Op_Rem
2682 => Resolve_Arithmetic_Op (N, Ctx_Type);
2684 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2686 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2688 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2689 => Resolve_Unary_Op (N, Ctx_Type);
2691 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2693 when N_Procedure_Call_Statement
2694 => Resolve_Call (N, Ctx_Type);
2696 when N_Operator_Symbol
2697 => Resolve_Operator_Symbol (N, Ctx_Type);
2699 when N_Qualified_Expression
2700 => Resolve_Qualified_Expression (N, Ctx_Type);
2702 when N_Quantified_Expression
2703 => Resolve_Quantified_Expression (N, Ctx_Type);
2705 when N_Raise_xxx_Error
2706 => Set_Etype (N, Ctx_Type);
2708 when N_Range => Resolve_Range (N, Ctx_Type);
2711 => Resolve_Real_Literal (N, Ctx_Type);
2713 when N_Reference => Resolve_Reference (N, Ctx_Type);
2715 when N_Selected_Component
2716 => Resolve_Selected_Component (N, Ctx_Type);
2718 when N_Slice => Resolve_Slice (N, Ctx_Type);
2720 when N_String_Literal
2721 => Resolve_String_Literal (N, Ctx_Type);
2723 when N_Subprogram_Info
2724 => Resolve_Subprogram_Info (N, Ctx_Type);
2726 when N_Type_Conversion
2727 => Resolve_Type_Conversion (N, Ctx_Type);
2729 when N_Unchecked_Expression =>
2730 Resolve_Unchecked_Expression (N, Ctx_Type);
2732 when N_Unchecked_Type_Conversion =>
2733 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2736 -- If the subexpression was replaced by a non-subexpression, then
2737 -- all we do is to expand it. The only legitimate case we know of
2738 -- is converting procedure call statement to entry call statements,
2739 -- but there may be others, so we are making this test general.
2741 if Nkind (N) not in N_Subexpr then
2742 Debug_A_Exit ("resolving ", N, " (done)");
2747 -- The expression is definitely NOT overloaded at this point, so
2748 -- we reset the Is_Overloaded flag to avoid any confusion when
2749 -- reanalyzing the node.
2751 Set_Is_Overloaded (N, False);
2753 -- Freeze expression type, entity if it is a name, and designated
2754 -- type if it is an allocator (RM 13.14(10,11,13)).
2756 -- Now that the resolution of the type of the node is complete,
2757 -- and we did not detect an error, we can expand this node. We
2758 -- skip the expand call if we are in a default expression, see
2759 -- section "Handling of Default Expressions" in Sem spec.
2761 Debug_A_Exit ("resolving ", N, " (done)");
2763 -- We unconditionally freeze the expression, even if we are in
2764 -- default expression mode (the Freeze_Expression routine tests
2765 -- this flag and only freezes static types if it is set).
2767 Freeze_Expression (N);
2769 -- Now we can do the expansion
2779 -- Version with check(s) suppressed
2781 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2783 if Suppress = All_Checks then
2785 Svg : constant Suppress_Array := Scope_Suppress;
2787 Scope_Suppress := (others => True);
2789 Scope_Suppress := Svg;
2794 Svg : constant Boolean := Scope_Suppress (Suppress);
2796 Scope_Suppress (Suppress) := True;
2798 Scope_Suppress (Suppress) := Svg;
2807 -- Version with implicit type
2809 procedure Resolve (N : Node_Id) is
2811 Resolve (N, Etype (N));
2814 ---------------------
2815 -- Resolve_Actuals --
2816 ---------------------
2818 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2819 Loc : constant Source_Ptr := Sloc (N);
2824 Prev : Node_Id := Empty;
2827 procedure Check_Argument_Order;
2828 -- Performs a check for the case where the actuals are all simple
2829 -- identifiers that correspond to the formal names, but in the wrong
2830 -- order, which is considered suspicious and cause for a warning.
2832 procedure Check_Prefixed_Call;
2833 -- If the original node is an overloaded call in prefix notation,
2834 -- insert an 'Access or a dereference as needed over the first actual.
2835 -- Try_Object_Operation has already verified that there is a valid
2836 -- interpretation, but the form of the actual can only be determined
2837 -- once the primitive operation is identified.
2839 procedure Insert_Default;
2840 -- If the actual is missing in a call, insert in the actuals list
2841 -- an instance of the default expression. The insertion is always
2842 -- a named association.
2844 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2845 -- Check whether T1 and T2, or their full views, are derived from a
2846 -- common type. Used to enforce the restrictions on array conversions
2849 function Static_Concatenation (N : Node_Id) return Boolean;
2850 -- Predicate to determine whether an actual that is a concatenation
2851 -- will be evaluated statically and does not need a transient scope.
2852 -- This must be determined before the actual is resolved and expanded
2853 -- because if needed the transient scope must be introduced earlier.
2855 --------------------------
2856 -- Check_Argument_Order --
2857 --------------------------
2859 procedure Check_Argument_Order is
2861 -- Nothing to do if no parameters, or original node is neither a
2862 -- function call nor a procedure call statement (happens in the
2863 -- operator-transformed-to-function call case), or the call does
2864 -- not come from source, or this warning is off.
2866 if not Warn_On_Parameter_Order
2868 No (Parameter_Associations (N))
2870 not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2873 not Comes_From_Source (N)
2879 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2882 -- Nothing to do if only one parameter
2888 -- Here if at least two arguments
2891 Actuals : array (1 .. Nargs) of Node_Id;
2895 Wrong_Order : Boolean := False;
2896 -- Set True if an out of order case is found
2899 -- Collect identifier names of actuals, fail if any actual is
2900 -- not a simple identifier, and record max length of name.
2902 Actual := First (Parameter_Associations (N));
2903 for J in Actuals'Range loop
2904 if Nkind (Actual) /= N_Identifier then
2907 Actuals (J) := Actual;
2912 -- If we got this far, all actuals are identifiers and the list
2913 -- of their names is stored in the Actuals array.
2915 Formal := First_Formal (Nam);
2916 for J in Actuals'Range loop
2918 -- If we ran out of formals, that's odd, probably an error
2919 -- which will be detected elsewhere, but abandon the search.
2925 -- If name matches and is in order OK
2927 if Chars (Formal) = Chars (Actuals (J)) then
2931 -- If no match, see if it is elsewhere in list and if so
2932 -- flag potential wrong order if type is compatible.
2934 for K in Actuals'Range loop
2935 if Chars (Formal) = Chars (Actuals (K))
2937 Has_Compatible_Type (Actuals (K), Etype (Formal))
2939 Wrong_Order := True;
2949 <<Continue>> Next_Formal (Formal);
2952 -- If Formals left over, also probably an error, skip warning
2954 if Present (Formal) then
2958 -- Here we give the warning if something was out of order
2962 ("actuals for this call may be in wrong order?", N);
2966 end Check_Argument_Order;
2968 -------------------------
2969 -- Check_Prefixed_Call --
2970 -------------------------
2972 procedure Check_Prefixed_Call is
2973 Act : constant Node_Id := First_Actual (N);
2974 A_Type : constant Entity_Id := Etype (Act);
2975 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2976 Orig : constant Node_Id := Original_Node (N);
2980 -- Check whether the call is a prefixed call, with or without
2981 -- additional actuals.
2983 if Nkind (Orig) = N_Selected_Component
2985 (Nkind (Orig) = N_Indexed_Component
2986 and then Nkind (Prefix (Orig)) = N_Selected_Component
2987 and then Is_Entity_Name (Prefix (Prefix (Orig)))
2988 and then Is_Entity_Name (Act)
2989 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
2991 if Is_Access_Type (A_Type)
2992 and then not Is_Access_Type (F_Type)
2994 -- Introduce dereference on object in prefix
2997 Make_Explicit_Dereference (Sloc (Act),
2998 Prefix => Relocate_Node (Act));
2999 Rewrite (Act, New_A);
3002 elsif Is_Access_Type (F_Type)
3003 and then not Is_Access_Type (A_Type)
3005 -- Introduce an implicit 'Access in prefix
3007 if not Is_Aliased_View (Act) then
3009 ("object in prefixed call to& must be aliased"
3010 & " (RM-2005 4.3.1 (13))",
3015 Make_Attribute_Reference (Loc,
3016 Attribute_Name => Name_Access,
3017 Prefix => Relocate_Node (Act)));
3022 end Check_Prefixed_Call;
3024 --------------------
3025 -- Insert_Default --
3026 --------------------
3028 procedure Insert_Default is
3033 -- Missing argument in call, nothing to insert
3035 if No (Default_Value (F)) then
3039 -- Note that we do a full New_Copy_Tree, so that any associated
3040 -- Itypes are properly copied. This may not be needed any more,
3041 -- but it does no harm as a safety measure! Defaults of a generic
3042 -- formal may be out of bounds of the corresponding actual (see
3043 -- cc1311b) and an additional check may be required.
3048 New_Scope => Current_Scope,
3051 if Is_Concurrent_Type (Scope (Nam))
3052 and then Has_Discriminants (Scope (Nam))
3054 Replace_Actual_Discriminants (N, Actval);
3057 if Is_Overloadable (Nam)
3058 and then Present (Alias (Nam))
3060 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3061 and then not Is_Tagged_Type (Etype (F))
3063 -- If default is a real literal, do not introduce a
3064 -- conversion whose effect may depend on the run-time
3065 -- size of universal real.
3067 if Nkind (Actval) = N_Real_Literal then
3068 Set_Etype (Actval, Base_Type (Etype (F)));
3070 Actval := Unchecked_Convert_To (Etype (F), Actval);
3074 if Is_Scalar_Type (Etype (F)) then
3075 Enable_Range_Check (Actval);
3078 Set_Parent (Actval, N);
3080 -- Resolve aggregates with their base type, to avoid scope
3081 -- anomalies: the subtype was first built in the subprogram
3082 -- declaration, and the current call may be nested.
3084 if Nkind (Actval) = N_Aggregate then
3085 Analyze_And_Resolve (Actval, Etype (F));
3087 Analyze_And_Resolve (Actval, Etype (Actval));
3091 Set_Parent (Actval, N);
3093 -- See note above concerning aggregates
3095 if Nkind (Actval) = N_Aggregate
3096 and then Has_Discriminants (Etype (Actval))
3098 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3100 -- Resolve entities with their own type, which may differ
3101 -- from the type of a reference in a generic context (the
3102 -- view swapping mechanism did not anticipate the re-analysis
3103 -- of default values in calls).
3105 elsif Is_Entity_Name (Actval) then
3106 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3109 Analyze_And_Resolve (Actval, Etype (Actval));
3113 -- If default is a tag indeterminate function call, propagate
3114 -- tag to obtain proper dispatching.
3116 if Is_Controlling_Formal (F)
3117 and then Nkind (Default_Value (F)) = N_Function_Call
3119 Set_Is_Controlling_Actual (Actval);
3124 -- If the default expression raises constraint error, then just
3125 -- silently replace it with an N_Raise_Constraint_Error node,
3126 -- since we already gave the warning on the subprogram spec.
3127 -- If node is already a Raise_Constraint_Error leave as is, to
3128 -- prevent loops in the warnings removal machinery.
3130 if Raises_Constraint_Error (Actval)
3131 and then Nkind (Actval) /= N_Raise_Constraint_Error
3134 Make_Raise_Constraint_Error (Loc,
3135 Reason => CE_Range_Check_Failed));
3136 Set_Raises_Constraint_Error (Actval);
3137 Set_Etype (Actval, Etype (F));
3141 Make_Parameter_Association (Loc,
3142 Explicit_Actual_Parameter => Actval,
3143 Selector_Name => Make_Identifier (Loc, Chars (F)));
3145 -- Case of insertion is first named actual
3147 if No (Prev) or else
3148 Nkind (Parent (Prev)) /= N_Parameter_Association
3150 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3151 Set_First_Named_Actual (N, Actval);
3154 if No (Parameter_Associations (N)) then
3155 Set_Parameter_Associations (N, New_List (Assoc));
3157 Append (Assoc, Parameter_Associations (N));
3161 Insert_After (Prev, Assoc);
3164 -- Case of insertion is not first named actual
3167 Set_Next_Named_Actual
3168 (Assoc, Next_Named_Actual (Parent (Prev)));
3169 Set_Next_Named_Actual (Parent (Prev), Actval);
3170 Append (Assoc, Parameter_Associations (N));
3173 Mark_Rewrite_Insertion (Assoc);
3174 Mark_Rewrite_Insertion (Actval);
3183 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3184 FT1 : Entity_Id := T1;
3185 FT2 : Entity_Id := T2;
3188 if Is_Private_Type (T1)
3189 and then Present (Full_View (T1))
3191 FT1 := Full_View (T1);
3194 if Is_Private_Type (T2)
3195 and then Present (Full_View (T2))
3197 FT2 := Full_View (T2);
3200 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3203 --------------------------
3204 -- Static_Concatenation --
3205 --------------------------
3207 function Static_Concatenation (N : Node_Id) return Boolean is
3210 when N_String_Literal =>
3215 -- Concatenation is static when both operands are static
3216 -- and the concatenation operator is a predefined one.
3218 return Scope (Entity (N)) = Standard_Standard
3220 Static_Concatenation (Left_Opnd (N))
3222 Static_Concatenation (Right_Opnd (N));
3225 if Is_Entity_Name (N) then
3227 Ent : constant Entity_Id := Entity (N);
3229 return Ekind (Ent) = E_Constant
3230 and then Present (Constant_Value (Ent))
3232 Is_Static_Expression (Constant_Value (Ent));
3239 end Static_Concatenation;
3241 -- Start of processing for Resolve_Actuals
3244 Check_Argument_Order;
3246 if Present (First_Actual (N)) then
3247 Check_Prefixed_Call;
3250 A := First_Actual (N);
3251 F := First_Formal (Nam);
3252 while Present (F) loop
3253 if No (A) and then Needs_No_Actuals (Nam) then
3256 -- If we have an error in any actual or formal, indicated by a type
3257 -- of Any_Type, then abandon resolution attempt, and set result type
3260 elsif (Present (A) and then Etype (A) = Any_Type)
3261 or else Etype (F) = Any_Type
3263 Set_Etype (N, Any_Type);
3267 -- Case where actual is present
3269 -- If the actual is an entity, generate a reference to it now. We
3270 -- do this before the actual is resolved, because a formal of some
3271 -- protected subprogram, or a task discriminant, will be rewritten
3272 -- during expansion, and the reference to the source entity may
3276 and then Is_Entity_Name (A)
3277 and then Comes_From_Source (N)
3279 Orig_A := Entity (A);
3281 if Present (Orig_A) then
3282 if Is_Formal (Orig_A)
3283 and then Ekind (F) /= E_In_Parameter
3285 Generate_Reference (Orig_A, A, 'm');
3286 elsif not Is_Overloaded (A) then
3287 Generate_Reference (Orig_A, A);
3293 and then (Nkind (Parent (A)) /= N_Parameter_Association
3295 Chars (Selector_Name (Parent (A))) = Chars (F))
3297 -- If style checking mode on, check match of formal name
3300 if Nkind (Parent (A)) = N_Parameter_Association then
3301 Check_Identifier (Selector_Name (Parent (A)), F);
3305 -- If the formal is Out or In_Out, do not resolve and expand the
3306 -- conversion, because it is subsequently expanded into explicit
3307 -- temporaries and assignments. However, the object of the
3308 -- conversion can be resolved. An exception is the case of tagged
3309 -- type conversion with a class-wide actual. In that case we want
3310 -- the tag check to occur and no temporary will be needed (no
3311 -- representation change can occur) and the parameter is passed by
3312 -- reference, so we go ahead and resolve the type conversion.
3313 -- Another exception is the case of reference to component or
3314 -- subcomponent of a bit-packed array, in which case we want to
3315 -- defer expansion to the point the in and out assignments are
3318 if Ekind (F) /= E_In_Parameter
3319 and then Nkind (A) = N_Type_Conversion
3320 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3322 if Ekind (F) = E_In_Out_Parameter
3323 and then Is_Array_Type (Etype (F))
3325 if Has_Aliased_Components (Etype (Expression (A)))
3326 /= Has_Aliased_Components (Etype (F))
3329 -- In a view conversion, the conversion must be legal in
3330 -- both directions, and thus both component types must be
3331 -- aliased, or neither (4.6 (8)).
3333 -- The additional rule 4.6 (24.9.2) seems unduly
3334 -- restrictive: the privacy requirement should not apply
3335 -- to generic types, and should be checked in an
3336 -- instance. ARG query is in order ???
3339 ("both component types in a view conversion must be"
3340 & " aliased, or neither", A);
3343 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3345 if Is_By_Reference_Type (Etype (F))
3346 or else Is_By_Reference_Type (Etype (Expression (A)))
3349 ("view conversion between unrelated by reference " &
3350 "array types not allowed (\'A'I-00246)", A);
3353 Comp_Type : constant Entity_Id :=
3355 (Etype (Expression (A)));
3357 if Comes_From_Source (A)
3358 and then Ada_Version >= Ada_2005
3360 ((Is_Private_Type (Comp_Type)
3361 and then not Is_Generic_Type (Comp_Type))
3362 or else Is_Tagged_Type (Comp_Type)
3363 or else Is_Volatile (Comp_Type))
3366 ("component type of a view conversion cannot"
3367 & " be private, tagged, or volatile"
3376 if (Conversion_OK (A)
3377 or else Valid_Conversion (A, Etype (A), Expression (A)))
3378 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3380 Resolve (Expression (A));
3383 -- If the actual is a function call that returns a limited
3384 -- unconstrained object that needs finalization, create a
3385 -- transient scope for it, so that it can receive the proper
3386 -- finalization list.
3388 elsif Nkind (A) = N_Function_Call
3389 and then Is_Limited_Record (Etype (F))
3390 and then not Is_Constrained (Etype (F))
3391 and then Expander_Active
3393 (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3395 Establish_Transient_Scope (A, False);
3397 -- A small optimization: if one of the actuals is a concatenation
3398 -- create a block around a procedure call to recover stack space.
3399 -- This alleviates stack usage when several procedure calls in
3400 -- the same statement list use concatenation. We do not perform
3401 -- this wrapping for code statements, where the argument is a
3402 -- static string, and we want to preserve warnings involving
3403 -- sequences of such statements.
3405 elsif Nkind (A) = N_Op_Concat
3406 and then Nkind (N) = N_Procedure_Call_Statement
3407 and then Expander_Active
3409 not (Is_Intrinsic_Subprogram (Nam)
3410 and then Chars (Nam) = Name_Asm)
3411 and then not Static_Concatenation (A)
3413 Establish_Transient_Scope (A, False);
3414 Resolve (A, Etype (F));
3417 if Nkind (A) = N_Type_Conversion
3418 and then Is_Array_Type (Etype (F))
3419 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3421 (Is_Limited_Type (Etype (F))
3422 or else Is_Limited_Type (Etype (Expression (A))))
3425 ("conversion between unrelated limited array types " &
3426 "not allowed (\A\I-00246)", A);
3428 if Is_Limited_Type (Etype (F)) then
3429 Explain_Limited_Type (Etype (F), A);
3432 if Is_Limited_Type (Etype (Expression (A))) then
3433 Explain_Limited_Type (Etype (Expression (A)), A);
3437 -- (Ada 2005: AI-251): If the actual is an allocator whose
3438 -- directly designated type is a class-wide interface, we build
3439 -- an anonymous access type to use it as the type of the
3440 -- allocator. Later, when the subprogram call is expanded, if
3441 -- the interface has a secondary dispatch table the expander
3442 -- will add a type conversion to force the correct displacement
3445 if Nkind (A) = N_Allocator then
3447 DDT : constant Entity_Id :=
3448 Directly_Designated_Type (Base_Type (Etype (F)));
3450 New_Itype : Entity_Id;
3453 if Is_Class_Wide_Type (DDT)
3454 and then Is_Interface (DDT)
3456 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3457 Set_Etype (New_Itype, Etype (A));
3458 Set_Directly_Designated_Type (New_Itype,
3459 Directly_Designated_Type (Etype (A)));
3460 Set_Etype (A, New_Itype);
3463 -- Ada 2005, AI-162:If the actual is an allocator, the
3464 -- innermost enclosing statement is the master of the
3465 -- created object. This needs to be done with expansion
3466 -- enabled only, otherwise the transient scope will not
3467 -- be removed in the expansion of the wrapped construct.
3469 if (Is_Controlled (DDT) or else Has_Task (DDT))
3470 and then Expander_Active
3472 Establish_Transient_Scope (A, False);
3477 -- (Ada 2005): The call may be to a primitive operation of
3478 -- a tagged synchronized type, declared outside of the type.
3479 -- In this case the controlling actual must be converted to
3480 -- its corresponding record type, which is the formal type.
3481 -- The actual may be a subtype, either because of a constraint
3482 -- or because it is a generic actual, so use base type to
3483 -- locate concurrent type.
3485 A_Typ := Base_Type (Etype (A));
3486 F_Typ := Base_Type (Etype (F));
3489 Full_A_Typ : Entity_Id;
3492 if Present (Full_View (A_Typ)) then
3493 Full_A_Typ := Base_Type (Full_View (A_Typ));
3495 Full_A_Typ := A_Typ;
3498 -- Tagged synchronized type (case 1): the actual is a
3501 if Is_Concurrent_Type (A_Typ)
3502 and then Corresponding_Record_Type (A_Typ) = F_Typ
3505 Unchecked_Convert_To
3506 (Corresponding_Record_Type (A_Typ), A));
3507 Resolve (A, Etype (F));
3509 -- Tagged synchronized type (case 2): the formal is a
3512 elsif Ekind (Full_A_Typ) = E_Record_Type
3514 (Corresponding_Concurrent_Type (Full_A_Typ))
3515 and then Is_Concurrent_Type (F_Typ)
3516 and then Present (Corresponding_Record_Type (F_Typ))
3517 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3519 Resolve (A, Corresponding_Record_Type (F_Typ));
3524 Resolve (A, Etype (F));
3532 -- Save actual for subsequent check on order dependence,
3533 -- and indicate whether actual is modifiable. For AI05-0144
3536 -- Ekind (F) /= E_In_Parameter or else Is_Access_Type (F_Typ));
3537 -- Why is this code commented out ???
3539 -- For mode IN, if actual is an entity, and the type of the formal
3540 -- has warnings suppressed, then we reset Never_Set_In_Source for
3541 -- the calling entity. The reason for this is to catch cases like
3542 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3543 -- uses trickery to modify an IN parameter.
3545 if Ekind (F) = E_In_Parameter
3546 and then Is_Entity_Name (A)
3547 and then Present (Entity (A))
3548 and then Ekind (Entity (A)) = E_Variable
3549 and then Has_Warnings_Off (F_Typ)
3551 Set_Never_Set_In_Source (Entity (A), False);
3554 -- Perform error checks for IN and IN OUT parameters
3556 if Ekind (F) /= E_Out_Parameter then
3558 -- Check unset reference. For scalar parameters, it is clearly
3559 -- wrong to pass an uninitialized value as either an IN or
3560 -- IN-OUT parameter. For composites, it is also clearly an
3561 -- error to pass a completely uninitialized value as an IN
3562 -- parameter, but the case of IN OUT is trickier. We prefer
3563 -- not to give a warning here. For example, suppose there is
3564 -- a routine that sets some component of a record to False.
3565 -- It is perfectly reasonable to make this IN-OUT and allow
3566 -- either initialized or uninitialized records to be passed
3569 -- For partially initialized composite values, we also avoid
3570 -- warnings, since it is quite likely that we are passing a
3571 -- partially initialized value and only the initialized fields
3572 -- will in fact be read in the subprogram.
3574 if Is_Scalar_Type (A_Typ)
3575 or else (Ekind (F) = E_In_Parameter
3576 and then not Is_Partially_Initialized_Type (A_Typ))
3578 Check_Unset_Reference (A);
3581 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3582 -- actual to a nested call, since this is case of reading an
3583 -- out parameter, which is not allowed.
3585 if Ada_Version = Ada_83
3586 and then Is_Entity_Name (A)
3587 and then Ekind (Entity (A)) = E_Out_Parameter
3589 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3593 -- Case of OUT or IN OUT parameter
3595 if Ekind (F) /= E_In_Parameter then
3597 -- For an Out parameter, check for useless assignment. Note
3598 -- that we can't set Last_Assignment this early, because we may
3599 -- kill current values in Resolve_Call, and that call would
3600 -- clobber the Last_Assignment field.
3602 -- Note: call Warn_On_Useless_Assignment before doing the check
3603 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3604 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3605 -- reflects the last assignment, not this one!
3607 if Ekind (F) = E_Out_Parameter then
3608 if Warn_On_Modified_As_Out_Parameter (F)
3609 and then Is_Entity_Name (A)
3610 and then Present (Entity (A))
3611 and then Comes_From_Source (N)
3613 Warn_On_Useless_Assignment (Entity (A), A);
3617 -- Validate the form of the actual. Note that the call to
3618 -- Is_OK_Variable_For_Out_Formal generates the required
3619 -- reference in this case.
3621 if not Is_OK_Variable_For_Out_Formal (A) then
3622 Error_Msg_NE ("actual for& must be a variable", A, F);
3625 -- What's the following about???
3627 if Is_Entity_Name (A) then
3628 Kill_Checks (Entity (A));
3634 if Etype (A) = Any_Type then
3635 Set_Etype (N, Any_Type);
3639 -- Apply appropriate range checks for in, out, and in-out
3640 -- parameters. Out and in-out parameters also need a separate
3641 -- check, if there is a type conversion, to make sure the return
3642 -- value meets the constraints of the variable before the
3645 -- Gigi looks at the check flag and uses the appropriate types.
3646 -- For now since one flag is used there is an optimization which
3647 -- might not be done in the In Out case since Gigi does not do
3648 -- any analysis. More thought required about this ???
3650 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3652 -- Apply predicate checks, unless this is a call to the
3653 -- predicate check function itself, which would cause an
3654 -- infinite recursion.
3656 if not (Ekind (Nam) = E_Function
3657 and then Has_Predicates (Nam))
3659 Apply_Predicate_Check (A, F_Typ);
3662 -- Apply required constraint checks
3664 if Is_Scalar_Type (Etype (A)) then
3665 Apply_Scalar_Range_Check (A, F_Typ);
3667 elsif Is_Array_Type (Etype (A)) then
3668 Apply_Length_Check (A, F_Typ);
3670 elsif Is_Record_Type (F_Typ)
3671 and then Has_Discriminants (F_Typ)
3672 and then Is_Constrained (F_Typ)
3673 and then (not Is_Derived_Type (F_Typ)
3674 or else Comes_From_Source (Nam))
3676 Apply_Discriminant_Check (A, F_Typ);
3678 elsif Is_Access_Type (F_Typ)
3679 and then Is_Array_Type (Designated_Type (F_Typ))
3680 and then Is_Constrained (Designated_Type (F_Typ))
3682 Apply_Length_Check (A, F_Typ);
3684 elsif Is_Access_Type (F_Typ)
3685 and then Has_Discriminants (Designated_Type (F_Typ))
3686 and then Is_Constrained (Designated_Type (F_Typ))
3688 Apply_Discriminant_Check (A, F_Typ);
3691 Apply_Range_Check (A, F_Typ);
3694 -- Ada 2005 (AI-231): Note that the controlling parameter case
3695 -- already existed in Ada 95, which is partially checked
3696 -- elsewhere (see Checks), and we don't want the warning
3697 -- message to differ.
3699 if Is_Access_Type (F_Typ)
3700 and then Can_Never_Be_Null (F_Typ)
3701 and then Known_Null (A)
3703 if Is_Controlling_Formal (F) then
3704 Apply_Compile_Time_Constraint_Error
3706 Msg => "null value not allowed here?",
3707 Reason => CE_Access_Check_Failed);
3709 elsif Ada_Version >= Ada_2005 then
3710 Apply_Compile_Time_Constraint_Error
3712 Msg => "(Ada 2005) null not allowed in "
3713 & "null-excluding formal?",
3714 Reason => CE_Null_Not_Allowed);
3719 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3720 if Nkind (A) = N_Type_Conversion then
3721 if Is_Scalar_Type (A_Typ) then
3722 Apply_Scalar_Range_Check
3723 (Expression (A), Etype (Expression (A)), A_Typ);
3726 (Expression (A), Etype (Expression (A)), A_Typ);
3730 if Is_Scalar_Type (F_Typ) then
3731 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3733 elsif Is_Array_Type (F_Typ)
3734 and then Ekind (F) = E_Out_Parameter
3736 Apply_Length_Check (A, F_Typ);
3739 Apply_Range_Check (A, A_Typ, F_Typ);
3744 -- An actual associated with an access parameter is implicitly
3745 -- converted to the anonymous access type of the formal and must
3746 -- satisfy the legality checks for access conversions.
3748 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3749 if not Valid_Conversion (A, F_Typ, A) then
3751 ("invalid implicit conversion for access parameter", A);
3755 -- Check bad case of atomic/volatile argument (RM C.6(12))
3757 if Is_By_Reference_Type (Etype (F))
3758 and then Comes_From_Source (N)
3760 if Is_Atomic_Object (A)
3761 and then not Is_Atomic (Etype (F))
3764 ("cannot pass atomic argument to non-atomic formal",
3767 elsif Is_Volatile_Object (A)
3768 and then not Is_Volatile (Etype (F))
3771 ("cannot pass volatile argument to non-volatile formal",
3776 -- Check that subprograms don't have improper controlling
3777 -- arguments (RM 3.9.2 (9)).
3779 -- A primitive operation may have an access parameter of an
3780 -- incomplete tagged type, but a dispatching call is illegal
3781 -- if the type is still incomplete.
3783 if Is_Controlling_Formal (F) then
3784 Set_Is_Controlling_Actual (A);
3786 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3788 Desig : constant Entity_Id := Designated_Type (Etype (F));
3790 if Ekind (Desig) = E_Incomplete_Type
3791 and then No (Full_View (Desig))
3792 and then No (Non_Limited_View (Desig))
3795 ("premature use of incomplete type& " &
3796 "in dispatching call", A, Desig);
3801 elsif Nkind (A) = N_Explicit_Dereference then
3802 Validate_Remote_Access_To_Class_Wide_Type (A);
3805 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3806 and then not Is_Class_Wide_Type (F_Typ)
3807 and then not Is_Controlling_Formal (F)
3809 Error_Msg_N ("class-wide argument not allowed here!", A);
3811 if Is_Subprogram (Nam)
3812 and then Comes_From_Source (Nam)
3814 Error_Msg_Node_2 := F_Typ;
3816 ("& is not a dispatching operation of &!", A, Nam);
3819 elsif Is_Access_Type (A_Typ)
3820 and then Is_Access_Type (F_Typ)
3821 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3822 and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3823 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3824 or else (Nkind (A) = N_Attribute_Reference
3826 Is_Class_Wide_Type (Etype (Prefix (A)))))
3827 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3828 and then not Is_Controlling_Formal (F)
3830 -- Disable these checks for call to imported C++ subprograms
3833 (Is_Entity_Name (Name (N))
3834 and then Is_Imported (Entity (Name (N)))
3835 and then Convention (Entity (Name (N))) = Convention_CPP)
3838 ("access to class-wide argument not allowed here!", A);
3840 if Is_Subprogram (Nam)
3841 and then Comes_From_Source (Nam)
3843 Error_Msg_Node_2 := Designated_Type (F_Typ);
3845 ("& is not a dispatching operation of &!", A, Nam);
3851 -- If it is a named association, treat the selector_name as a
3852 -- proper identifier, and mark the corresponding entity.
3854 if Nkind (Parent (A)) = N_Parameter_Association then
3855 Set_Entity (Selector_Name (Parent (A)), F);
3856 Generate_Reference (F, Selector_Name (Parent (A)));
3857 Set_Etype (Selector_Name (Parent (A)), F_Typ);
3858 Generate_Reference (F_Typ, N, ' ');
3863 if Ekind (F) /= E_Out_Parameter then
3864 Check_Unset_Reference (A);
3869 -- Case where actual is not present
3877 end Resolve_Actuals;
3879 -----------------------
3880 -- Resolve_Allocator --
3881 -----------------------
3883 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
3884 E : constant Node_Id := Expression (N);
3886 Discrim : Entity_Id;
3889 Assoc : Node_Id := Empty;
3892 procedure Check_Allocator_Discrim_Accessibility
3893 (Disc_Exp : Node_Id;
3894 Alloc_Typ : Entity_Id);
3895 -- Check that accessibility level associated with an access discriminant
3896 -- initialized in an allocator by the expression Disc_Exp is not deeper
3897 -- than the level of the allocator type Alloc_Typ. An error message is
3898 -- issued if this condition is violated. Specialized checks are done for
3899 -- the cases of a constraint expression which is an access attribute or
3900 -- an access discriminant.
3902 function In_Dispatching_Context return Boolean;
3903 -- If the allocator is an actual in a call, it is allowed to be class-
3904 -- wide when the context is not because it is a controlling actual.
3906 procedure Propagate_Coextensions (Root : Node_Id);
3907 -- Propagate all nested coextensions which are located one nesting
3908 -- level down the tree to the node Root. Example:
3911 -- Level_1_Coextension
3912 -- Level_2_Coextension
3914 -- The algorithm is paired with delay actions done by the Expander. In
3915 -- the above example, assume all coextensions are controlled types.
3916 -- The cycle of analysis, resolution and expansion will yield:
3918 -- 1) Analyze Top_Record
3919 -- 2) Analyze Level_1_Coextension
3920 -- 3) Analyze Level_2_Coextension
3921 -- 4) Resolve Level_2_Coextension. The allocator is marked as a
3923 -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is
3924 -- generated to capture the allocated object. Temp_1 is attached
3925 -- to the coextension chain of Level_2_Coextension.
3926 -- 6) Resolve Level_1_Coextension. The allocator is marked as a
3927 -- coextension. A forward tree traversal is performed which finds
3928 -- Level_2_Coextension's list and copies its contents into its
3930 -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is
3931 -- generated to capture the allocated object. Temp_2 is attached
3932 -- to the coextension chain of Level_1_Coextension. Currently, the
3933 -- contents of the list are [Temp_2, Temp_1].
3934 -- 8) Resolve Top_Record. A forward tree traversal is performed which
3935 -- finds Level_1_Coextension's list and copies its contents into
3937 -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and
3938 -- Temp_2 and attach them to Top_Record's finalization list.
3940 -------------------------------------------
3941 -- Check_Allocator_Discrim_Accessibility --
3942 -------------------------------------------
3944 procedure Check_Allocator_Discrim_Accessibility
3945 (Disc_Exp : Node_Id;
3946 Alloc_Typ : Entity_Id)
3949 if Type_Access_Level (Etype (Disc_Exp)) >
3950 Type_Access_Level (Alloc_Typ)
3953 ("operand type has deeper level than allocator type", Disc_Exp);
3955 -- When the expression is an Access attribute the level of the prefix
3956 -- object must not be deeper than that of the allocator's type.
3958 elsif Nkind (Disc_Exp) = N_Attribute_Reference
3959 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
3961 and then Object_Access_Level (Prefix (Disc_Exp))
3962 > Type_Access_Level (Alloc_Typ)
3965 ("prefix of attribute has deeper level than allocator type",
3968 -- When the expression is an access discriminant the check is against
3969 -- the level of the prefix object.
3971 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
3972 and then Nkind (Disc_Exp) = N_Selected_Component
3973 and then Object_Access_Level (Prefix (Disc_Exp))
3974 > Type_Access_Level (Alloc_Typ)
3977 ("access discriminant has deeper level than allocator type",
3980 -- All other cases are legal
3985 end Check_Allocator_Discrim_Accessibility;
3987 ----------------------------
3988 -- In_Dispatching_Context --
3989 ----------------------------
3991 function In_Dispatching_Context return Boolean is
3992 Par : constant Node_Id := Parent (N);
3994 return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
3995 and then Is_Entity_Name (Name (Par))
3996 and then Is_Dispatching_Operation (Entity (Name (Par)));
3997 end In_Dispatching_Context;
3999 ----------------------------
4000 -- Propagate_Coextensions --
4001 ----------------------------
4003 procedure Propagate_Coextensions (Root : Node_Id) is
4005 procedure Copy_List (From : Elist_Id; To : Elist_Id);
4006 -- Copy the contents of list From into list To, preserving the
4007 -- order of elements.
4009 function Process_Allocator (Nod : Node_Id) return Traverse_Result;
4010 -- Recognize an allocator or a rewritten allocator node and add it
4011 -- along with its nested coextensions to the list of Root.
4017 procedure Copy_List (From : Elist_Id; To : Elist_Id) is
4018 From_Elmt : Elmt_Id;
4020 From_Elmt := First_Elmt (From);
4021 while Present (From_Elmt) loop
4022 Append_Elmt (Node (From_Elmt), To);
4023 Next_Elmt (From_Elmt);
4027 -----------------------
4028 -- Process_Allocator --
4029 -----------------------
4031 function Process_Allocator (Nod : Node_Id) return Traverse_Result is
4032 Orig_Nod : Node_Id := Nod;
4035 -- This is a possible rewritten subtype indication allocator. Any
4036 -- nested coextensions will appear as discriminant constraints.
4038 if Nkind (Nod) = N_Identifier
4039 and then Present (Original_Node (Nod))
4040 and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
4044 Discr_Elmt : Elmt_Id;
4047 if Is_Record_Type (Entity (Nod)) then
4049 First_Elmt (Discriminant_Constraint (Entity (Nod)));
4050 while Present (Discr_Elmt) loop
4051 Discr := Node (Discr_Elmt);
4053 if Nkind (Discr) = N_Identifier
4054 and then Present (Original_Node (Discr))
4055 and then Nkind (Original_Node (Discr)) = N_Allocator
4056 and then Present (Coextensions (
4057 Original_Node (Discr)))
4059 if No (Coextensions (Root)) then
4060 Set_Coextensions (Root, New_Elmt_List);
4064 (From => Coextensions (Original_Node (Discr)),
4065 To => Coextensions (Root));
4068 Next_Elmt (Discr_Elmt);
4071 -- There is no need to continue the traversal of this
4072 -- subtree since all the information has already been
4079 -- Case of either a stand alone allocator or a rewritten allocator
4080 -- with an aggregate.
4083 if Present (Original_Node (Nod)) then
4084 Orig_Nod := Original_Node (Nod);
4087 if Nkind (Orig_Nod) = N_Allocator then
4089 -- Propagate the list of nested coextensions to the Root
4090 -- allocator. This is done through list copy since a single
4091 -- allocator may have multiple coextensions. Do not touch
4092 -- coextensions roots.
4094 if not Is_Coextension_Root (Orig_Nod)
4095 and then Present (Coextensions (Orig_Nod))
4097 if No (Coextensions (Root)) then
4098 Set_Coextensions (Root, New_Elmt_List);
4102 (From => Coextensions (Orig_Nod),
4103 To => Coextensions (Root));
4106 -- There is no need to continue the traversal of this
4107 -- subtree since all the information has already been
4114 -- Keep on traversing, looking for the next allocator
4117 end Process_Allocator;
4119 procedure Process_Allocators is
4120 new Traverse_Proc (Process_Allocator);
4122 -- Start of processing for Propagate_Coextensions
4125 Process_Allocators (Expression (Root));
4126 end Propagate_Coextensions;
4128 -- Start of processing for Resolve_Allocator
4131 -- Replace general access with specific type
4133 if Ekind (Etype (N)) = E_Allocator_Type then
4134 Set_Etype (N, Base_Type (Typ));
4137 if Is_Abstract_Type (Typ) then
4138 Error_Msg_N ("type of allocator cannot be abstract", N);
4141 -- For qualified expression, resolve the expression using the
4142 -- given subtype (nothing to do for type mark, subtype indication)
4144 if Nkind (E) = N_Qualified_Expression then
4145 if Is_Class_Wide_Type (Etype (E))
4146 and then not Is_Class_Wide_Type (Designated_Type (Typ))
4147 and then not In_Dispatching_Context
4150 ("class-wide allocator not allowed for this access type", N);
4153 Resolve (Expression (E), Etype (E));
4154 Check_Unset_Reference (Expression (E));
4156 -- A qualified expression requires an exact match of the type,
4157 -- class-wide matching is not allowed.
4159 if (Is_Class_Wide_Type (Etype (Expression (E)))
4160 or else Is_Class_Wide_Type (Etype (E)))
4161 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4163 Wrong_Type (Expression (E), Etype (E));
4166 -- A special accessibility check is needed for allocators that
4167 -- constrain access discriminants. The level of the type of the
4168 -- expression used to constrain an access discriminant cannot be
4169 -- deeper than the type of the allocator (in contrast to access
4170 -- parameters, where the level of the actual can be arbitrary).
4172 -- We can't use Valid_Conversion to perform this check because
4173 -- in general the type of the allocator is unrelated to the type
4174 -- of the access discriminant.
4176 if Ekind (Typ) /= E_Anonymous_Access_Type
4177 or else Is_Local_Anonymous_Access (Typ)
4179 Subtyp := Entity (Subtype_Mark (E));
4181 Aggr := Original_Node (Expression (E));
4183 if Has_Discriminants (Subtyp)
4184 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4186 Discrim := First_Discriminant (Base_Type (Subtyp));
4188 -- Get the first component expression of the aggregate
4190 if Present (Expressions (Aggr)) then
4191 Disc_Exp := First (Expressions (Aggr));
4193 elsif Present (Component_Associations (Aggr)) then
4194 Assoc := First (Component_Associations (Aggr));
4196 if Present (Assoc) then
4197 Disc_Exp := Expression (Assoc);
4206 while Present (Discrim) and then Present (Disc_Exp) loop
4207 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4208 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4211 Next_Discriminant (Discrim);
4213 if Present (Discrim) then
4214 if Present (Assoc) then
4216 Disc_Exp := Expression (Assoc);
4218 elsif Present (Next (Disc_Exp)) then
4222 Assoc := First (Component_Associations (Aggr));
4224 if Present (Assoc) then
4225 Disc_Exp := Expression (Assoc);
4235 -- For a subtype mark or subtype indication, freeze the subtype
4238 Freeze_Expression (E);
4240 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4242 ("initialization required for access-to-constant allocator", N);
4245 -- A special accessibility check is needed for allocators that
4246 -- constrain access discriminants. The level of the type of the
4247 -- expression used to constrain an access discriminant cannot be
4248 -- deeper than the type of the allocator (in contrast to access
4249 -- parameters, where the level of the actual can be arbitrary).
4250 -- We can't use Valid_Conversion to perform this check because
4251 -- in general the type of the allocator is unrelated to the type
4252 -- of the access discriminant.
4254 if Nkind (Original_Node (E)) = N_Subtype_Indication
4255 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4256 or else Is_Local_Anonymous_Access (Typ))
4258 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4260 if Has_Discriminants (Subtyp) then
4261 Discrim := First_Discriminant (Base_Type (Subtyp));
4262 Constr := First (Constraints (Constraint (Original_Node (E))));
4263 while Present (Discrim) and then Present (Constr) loop
4264 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4265 if Nkind (Constr) = N_Discriminant_Association then
4266 Disc_Exp := Original_Node (Expression (Constr));
4268 Disc_Exp := Original_Node (Constr);
4271 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4274 Next_Discriminant (Discrim);
4281 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4282 -- check that the level of the type of the created object is not deeper
4283 -- than the level of the allocator's access type, since extensions can
4284 -- now occur at deeper levels than their ancestor types. This is a
4285 -- static accessibility level check; a run-time check is also needed in
4286 -- the case of an initialized allocator with a class-wide argument (see
4287 -- Expand_Allocator_Expression).
4289 if Ada_Version >= Ada_2005
4290 and then Is_Class_Wide_Type (Designated_Type (Typ))
4293 Exp_Typ : Entity_Id;
4296 if Nkind (E) = N_Qualified_Expression then
4297 Exp_Typ := Etype (E);
4298 elsif Nkind (E) = N_Subtype_Indication then
4299 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4301 Exp_Typ := Entity (E);
4304 if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
4305 if In_Instance_Body then
4306 Error_Msg_N ("?type in allocator has deeper level than" &
4307 " designated class-wide type", E);
4308 Error_Msg_N ("\?Program_Error will be raised at run time",
4311 Make_Raise_Program_Error (Sloc (N),
4312 Reason => PE_Accessibility_Check_Failed));
4315 -- Do not apply Ada 2005 accessibility checks on a class-wide
4316 -- allocator if the type given in the allocator is a formal
4317 -- type. A run-time check will be performed in the instance.
4319 elsif not Is_Generic_Type (Exp_Typ) then
4320 Error_Msg_N ("type in allocator has deeper level than" &
4321 " designated class-wide type", E);
4327 -- Check for allocation from an empty storage pool
4329 if No_Pool_Assigned (Typ) then
4330 Error_Msg_N ("allocation from empty storage pool!", N);
4332 -- If the context is an unchecked conversion, as may happen within
4333 -- an inlined subprogram, the allocator is being resolved with its
4334 -- own anonymous type. In that case, if the target type has a specific
4335 -- storage pool, it must be inherited explicitly by the allocator type.
4337 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4338 and then No (Associated_Storage_Pool (Typ))
4340 Set_Associated_Storage_Pool
4341 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4344 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4345 Check_Restriction (No_Anonymous_Allocators, N);
4348 -- An erroneous allocator may be rewritten as a raise Program_Error
4351 if Nkind (N) = N_Allocator then
4353 -- An anonymous access discriminant is the definition of a
4356 if Ekind (Typ) = E_Anonymous_Access_Type
4357 and then Nkind (Associated_Node_For_Itype (Typ)) =
4358 N_Discriminant_Specification
4360 -- Avoid marking an allocator as a dynamic coextension if it is
4361 -- within a static construct.
4363 if not Is_Static_Coextension (N) then
4364 Set_Is_Dynamic_Coextension (N);
4367 -- Cleanup for potential static coextensions
4370 Set_Is_Dynamic_Coextension (N, False);
4371 Set_Is_Static_Coextension (N, False);
4374 -- There is no need to propagate any nested coextensions if they
4375 -- are marked as static since they will be rewritten on the spot.
4377 if not Is_Static_Coextension (N) then
4378 Propagate_Coextensions (N);
4381 end Resolve_Allocator;
4383 ---------------------------
4384 -- Resolve_Arithmetic_Op --
4385 ---------------------------
4387 -- Used for resolving all arithmetic operators except exponentiation
4389 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4390 L : constant Node_Id := Left_Opnd (N);
4391 R : constant Node_Id := Right_Opnd (N);
4392 TL : constant Entity_Id := Base_Type (Etype (L));
4393 TR : constant Entity_Id := Base_Type (Etype (R));
4397 B_Typ : constant Entity_Id := Base_Type (Typ);
4398 -- We do the resolution using the base type, because intermediate values
4399 -- in expressions always are of the base type, not a subtype of it.
4401 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4402 -- Returns True if N is in a context that expects "any real type"
4404 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4405 -- Return True iff given type is Integer or universal real/integer
4407 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4408 -- Choose type of integer literal in fixed-point operation to conform
4409 -- to available fixed-point type. T is the type of the other operand,
4410 -- which is needed to determine the expected type of N.
4412 procedure Set_Operand_Type (N : Node_Id);
4413 -- Set operand type to T if universal
4415 -------------------------------
4416 -- Expected_Type_Is_Any_Real --
4417 -------------------------------
4419 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4421 -- N is the expression after "delta" in a fixed_point_definition;
4424 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4425 N_Decimal_Fixed_Point_Definition,
4427 -- N is one of the bounds in a real_range_specification;
4430 N_Real_Range_Specification,
4432 -- N is the expression of a delta_constraint;
4435 N_Delta_Constraint);
4436 end Expected_Type_Is_Any_Real;
4438 -----------------------------
4439 -- Is_Integer_Or_Universal --
4440 -----------------------------
4442 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4444 Index : Interp_Index;
4448 if not Is_Overloaded (N) then
4450 return Base_Type (T) = Base_Type (Standard_Integer)
4451 or else T = Universal_Integer
4452 or else T = Universal_Real;
4454 Get_First_Interp (N, Index, It);
4455 while Present (It.Typ) loop
4456 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4457 or else It.Typ = Universal_Integer
4458 or else It.Typ = Universal_Real
4463 Get_Next_Interp (Index, It);
4468 end Is_Integer_Or_Universal;
4470 ----------------------------
4471 -- Set_Mixed_Mode_Operand --
4472 ----------------------------
4474 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4475 Index : Interp_Index;
4479 if Universal_Interpretation (N) = Universal_Integer then
4481 -- A universal integer literal is resolved as standard integer
4482 -- except in the case of a fixed-point result, where we leave it
4483 -- as universal (to be handled by Exp_Fixd later on)
4485 if Is_Fixed_Point_Type (T) then
4486 Resolve (N, Universal_Integer);
4488 Resolve (N, Standard_Integer);
4491 elsif Universal_Interpretation (N) = Universal_Real
4492 and then (T = Base_Type (Standard_Integer)
4493 or else T = Universal_Integer
4494 or else T = Universal_Real)
4496 -- A universal real can appear in a fixed-type context. We resolve
4497 -- the literal with that context, even though this might raise an
4498 -- exception prematurely (the other operand may be zero).
4502 elsif Etype (N) = Base_Type (Standard_Integer)
4503 and then T = Universal_Real
4504 and then Is_Overloaded (N)
4506 -- Integer arg in mixed-mode operation. Resolve with universal
4507 -- type, in case preference rule must be applied.
4509 Resolve (N, Universal_Integer);
4512 and then B_Typ /= Universal_Fixed
4514 -- Not a mixed-mode operation, resolve with context
4518 elsif Etype (N) = Any_Fixed then
4520 -- N may itself be a mixed-mode operation, so use context type
4524 elsif Is_Fixed_Point_Type (T)
4525 and then B_Typ = Universal_Fixed
4526 and then Is_Overloaded (N)
4528 -- Must be (fixed * fixed) operation, operand must have one
4529 -- compatible interpretation.
4531 Resolve (N, Any_Fixed);
4533 elsif Is_Fixed_Point_Type (B_Typ)
4534 and then (T = Universal_Real
4535 or else Is_Fixed_Point_Type (T))
4536 and then Is_Overloaded (N)
4538 -- C * F(X) in a fixed context, where C is a real literal or a
4539 -- fixed-point expression. F must have either a fixed type
4540 -- interpretation or an integer interpretation, but not both.
4542 Get_First_Interp (N, Index, It);
4543 while Present (It.Typ) loop
4544 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4546 if Analyzed (N) then
4547 Error_Msg_N ("ambiguous operand in fixed operation", N);
4549 Resolve (N, Standard_Integer);
4552 elsif Is_Fixed_Point_Type (It.Typ) then
4554 if Analyzed (N) then
4555 Error_Msg_N ("ambiguous operand in fixed operation", N);
4557 Resolve (N, It.Typ);
4561 Get_Next_Interp (Index, It);
4564 -- Reanalyze the literal with the fixed type of the context. If
4565 -- context is Universal_Fixed, we are within a conversion, leave
4566 -- the literal as a universal real because there is no usable
4567 -- fixed type, and the target of the conversion plays no role in
4581 if B_Typ = Universal_Fixed
4582 and then Nkind (Op2) = N_Real_Literal
4584 T2 := Universal_Real;
4589 Set_Analyzed (Op2, False);
4596 end Set_Mixed_Mode_Operand;
4598 ----------------------
4599 -- Set_Operand_Type --
4600 ----------------------
4602 procedure Set_Operand_Type (N : Node_Id) is
4604 if Etype (N) = Universal_Integer
4605 or else Etype (N) = Universal_Real
4609 end Set_Operand_Type;
4611 -- Start of processing for Resolve_Arithmetic_Op
4614 if Comes_From_Source (N)
4615 and then Ekind (Entity (N)) = E_Function
4616 and then Is_Imported (Entity (N))
4617 and then Is_Intrinsic_Subprogram (Entity (N))
4619 Resolve_Intrinsic_Operator (N, Typ);
4622 -- Special-case for mixed-mode universal expressions or fixed point
4623 -- type operation: each argument is resolved separately. The same
4624 -- treatment is required if one of the operands of a fixed point
4625 -- operation is universal real, since in this case we don't do a
4626 -- conversion to a specific fixed-point type (instead the expander
4627 -- takes care of the case).
4629 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4630 and then Present (Universal_Interpretation (L))
4631 and then Present (Universal_Interpretation (R))
4633 Resolve (L, Universal_Interpretation (L));
4634 Resolve (R, Universal_Interpretation (R));
4635 Set_Etype (N, B_Typ);
4637 elsif (B_Typ = Universal_Real
4638 or else Etype (N) = Universal_Fixed
4639 or else (Etype (N) = Any_Fixed
4640 and then Is_Fixed_Point_Type (B_Typ))
4641 or else (Is_Fixed_Point_Type (B_Typ)
4642 and then (Is_Integer_Or_Universal (L)
4644 Is_Integer_Or_Universal (R))))
4645 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4647 if TL = Universal_Integer or else TR = Universal_Integer then
4648 Check_For_Visible_Operator (N, B_Typ);
4651 -- If context is a fixed type and one operand is integer, the
4652 -- other is resolved with the type of the context.
4654 if Is_Fixed_Point_Type (B_Typ)
4655 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4656 or else TL = Universal_Integer)
4661 elsif Is_Fixed_Point_Type (B_Typ)
4662 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4663 or else TR = Universal_Integer)
4669 Set_Mixed_Mode_Operand (L, TR);
4670 Set_Mixed_Mode_Operand (R, TL);
4673 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4674 -- multiplying operators from being used when the expected type is
4675 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4676 -- some cases where the expected type is actually Any_Real;
4677 -- Expected_Type_Is_Any_Real takes care of that case.
4679 if Etype (N) = Universal_Fixed
4680 or else Etype (N) = Any_Fixed
4682 if B_Typ = Universal_Fixed
4683 and then not Expected_Type_Is_Any_Real (N)
4684 and then not Nkind_In (Parent (N), N_Type_Conversion,
4685 N_Unchecked_Type_Conversion)
4687 Error_Msg_N ("type cannot be determined from context!", N);
4688 Error_Msg_N ("\explicit conversion to result type required", N);
4690 Set_Etype (L, Any_Type);
4691 Set_Etype (R, Any_Type);
4694 if Ada_Version = Ada_83
4695 and then Etype (N) = Universal_Fixed
4697 Nkind_In (Parent (N), N_Type_Conversion,
4698 N_Unchecked_Type_Conversion)
4701 ("(Ada 83) fixed-point operation "
4702 & "needs explicit conversion", N);
4705 -- The expected type is "any real type" in contexts like
4706 -- type T is delta <universal_fixed-expression> ...
4707 -- in which case we need to set the type to Universal_Real
4708 -- so that static expression evaluation will work properly.
4710 if Expected_Type_Is_Any_Real (N) then
4711 Set_Etype (N, Universal_Real);
4713 Set_Etype (N, B_Typ);
4717 elsif Is_Fixed_Point_Type (B_Typ)
4718 and then (Is_Integer_Or_Universal (L)
4719 or else Nkind (L) = N_Real_Literal
4720 or else Nkind (R) = N_Real_Literal
4721 or else Is_Integer_Or_Universal (R))
4723 Set_Etype (N, B_Typ);
4725 elsif Etype (N) = Any_Fixed then
4727 -- If no previous errors, this is only possible if one operand
4728 -- is overloaded and the context is universal. Resolve as such.
4730 Set_Etype (N, B_Typ);
4734 if (TL = Universal_Integer or else TL = Universal_Real)
4736 (TR = Universal_Integer or else TR = Universal_Real)
4738 Check_For_Visible_Operator (N, B_Typ);
4741 -- If the context is Universal_Fixed and the operands are also
4742 -- universal fixed, this is an error, unless there is only one
4743 -- applicable fixed_point type (usually Duration).
4745 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4746 T := Unique_Fixed_Point_Type (N);
4748 if T = Any_Type then
4761 -- If one of the arguments was resolved to a non-universal type.
4762 -- label the result of the operation itself with the same type.
4763 -- Do the same for the universal argument, if any.
4765 T := Intersect_Types (L, R);
4766 Set_Etype (N, Base_Type (T));
4767 Set_Operand_Type (L);
4768 Set_Operand_Type (R);
4771 Generate_Operator_Reference (N, Typ);
4772 Eval_Arithmetic_Op (N);
4774 -- Set overflow and division checking bit. Much cleverer code needed
4775 -- here eventually and perhaps the Resolve routines should be separated
4776 -- for the various arithmetic operations, since they will need
4777 -- different processing. ???
4779 if Nkind (N) in N_Op then
4780 if not Overflow_Checks_Suppressed (Etype (N)) then
4781 Enable_Overflow_Check (N);
4784 -- Give warning if explicit division by zero
4786 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4787 and then not Division_Checks_Suppressed (Etype (N))
4789 Rop := Right_Opnd (N);
4791 if Compile_Time_Known_Value (Rop)
4792 and then ((Is_Integer_Type (Etype (Rop))
4793 and then Expr_Value (Rop) = Uint_0)
4795 (Is_Real_Type (Etype (Rop))
4796 and then Expr_Value_R (Rop) = Ureal_0))
4798 -- Specialize the warning message according to the operation
4802 Apply_Compile_Time_Constraint_Error
4803 (N, "division by zero?", CE_Divide_By_Zero,
4804 Loc => Sloc (Right_Opnd (N)));
4807 Apply_Compile_Time_Constraint_Error
4808 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4809 Loc => Sloc (Right_Opnd (N)));
4812 Apply_Compile_Time_Constraint_Error
4813 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4814 Loc => Sloc (Right_Opnd (N)));
4816 -- Division by zero can only happen with division, rem,
4817 -- and mod operations.
4820 raise Program_Error;
4823 -- Otherwise just set the flag to check at run time
4826 Activate_Division_Check (N);
4830 -- If Restriction No_Implicit_Conditionals is active, then it is
4831 -- violated if either operand can be negative for mod, or for rem
4832 -- if both operands can be negative.
4834 if Restriction_Check_Required (No_Implicit_Conditionals)
4835 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4844 -- Set if corresponding operand might be negative
4848 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4849 LNeg := (not OK) or else Lo < 0;
4852 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4853 RNeg := (not OK) or else Lo < 0;
4855 -- Check if we will be generating conditionals. There are two
4856 -- cases where that can happen, first for REM, the only case
4857 -- is largest negative integer mod -1, where the division can
4858 -- overflow, but we still have to give the right result. The
4859 -- front end generates a test for this annoying case. Here we
4860 -- just test if both operands can be negative (that's what the
4861 -- expander does, so we match its logic here).
4863 -- The second case is mod where either operand can be negative.
4864 -- In this case, the back end has to generate additonal tests.
4866 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4868 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4870 Check_Restriction (No_Implicit_Conditionals, N);
4876 Check_Unset_Reference (L);
4877 Check_Unset_Reference (R);
4878 end Resolve_Arithmetic_Op;
4884 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4885 Loc : constant Source_Ptr := Sloc (N);
4886 Subp : constant Node_Id := Name (N);
4894 function Same_Or_Aliased_Subprograms
4896 E : Entity_Id) return Boolean;
4897 -- Returns True if the subprogram entity S is the same as E or else
4898 -- S is an alias of E.
4900 ---------------------------------
4901 -- Same_Or_Aliased_Subprograms --
4902 ---------------------------------
4904 function Same_Or_Aliased_Subprograms
4906 E : Entity_Id) return Boolean
4908 Subp_Alias : constant Entity_Id := Alias (S);
4911 or else (Present (Subp_Alias) and then Subp_Alias = E);
4912 end Same_Or_Aliased_Subprograms;
4914 -- Start of processing for Resolve_Call
4917 -- The context imposes a unique interpretation with type Typ on a
4918 -- procedure or function call. Find the entity of the subprogram that
4919 -- yields the expected type, and propagate the corresponding formal
4920 -- constraints on the actuals. The caller has established that an
4921 -- interpretation exists, and emitted an error if not unique.
4923 -- First deal with the case of a call to an access-to-subprogram,
4924 -- dereference made explicit in Analyze_Call.
4926 if Ekind (Etype (Subp)) = E_Subprogram_Type then
4927 if not Is_Overloaded (Subp) then
4928 Nam := Etype (Subp);
4931 -- Find the interpretation whose type (a subprogram type) has a
4932 -- return type that is compatible with the context. Analysis of
4933 -- the node has established that one exists.
4937 Get_First_Interp (Subp, I, It);
4938 while Present (It.Typ) loop
4939 if Covers (Typ, Etype (It.Typ)) then
4944 Get_Next_Interp (I, It);
4948 raise Program_Error;
4952 -- If the prefix is not an entity, then resolve it
4954 if not Is_Entity_Name (Subp) then
4955 Resolve (Subp, Nam);
4958 -- For an indirect call, we always invalidate checks, since we do not
4959 -- know whether the subprogram is local or global. Yes we could do
4960 -- better here, e.g. by knowing that there are no local subprograms,
4961 -- but it does not seem worth the effort. Similarly, we kill all
4962 -- knowledge of current constant values.
4964 Kill_Current_Values;
4966 -- If this is a procedure call which is really an entry call, do
4967 -- the conversion of the procedure call to an entry call. Protected
4968 -- operations use the same circuitry because the name in the call
4969 -- can be an arbitrary expression with special resolution rules.
4971 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
4972 or else (Is_Entity_Name (Subp)
4973 and then Ekind (Entity (Subp)) = E_Entry)
4975 Resolve_Entry_Call (N, Typ);
4976 Check_Elab_Call (N);
4978 -- Kill checks and constant values, as above for indirect case
4979 -- Who knows what happens when another task is activated?
4981 Kill_Current_Values;
4984 -- Normal subprogram call with name established in Resolve
4986 elsif not (Is_Type (Entity (Subp))) then
4987 Nam := Entity (Subp);
4988 Set_Entity_With_Style_Check (Subp, Nam);
4990 -- Otherwise we must have the case of an overloaded call
4993 pragma Assert (Is_Overloaded (Subp));
4995 -- Initialize Nam to prevent warning (we know it will be assigned
4996 -- in the loop below, but the compiler does not know that).
5000 Get_First_Interp (Subp, I, It);
5001 while Present (It.Typ) loop
5002 if Covers (Typ, It.Typ) then
5004 Set_Entity_With_Style_Check (Subp, Nam);
5008 Get_Next_Interp (I, It);
5012 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5013 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5014 and then Nkind (Subp) /= N_Explicit_Dereference
5015 and then Present (Parameter_Associations (N))
5017 -- The prefix is a parameterless function call that returns an access
5018 -- to subprogram. If parameters are present in the current call, add
5019 -- add an explicit dereference. We use the base type here because
5020 -- within an instance these may be subtypes.
5022 -- The dereference is added either in Analyze_Call or here. Should
5023 -- be consolidated ???
5025 Set_Is_Overloaded (Subp, False);
5026 Set_Etype (Subp, Etype (Nam));
5027 Insert_Explicit_Dereference (Subp);
5028 Nam := Designated_Type (Etype (Nam));
5029 Resolve (Subp, Nam);
5032 -- Check that a call to Current_Task does not occur in an entry body
5034 if Is_RTE (Nam, RE_Current_Task) then
5043 -- Exclude calls that occur within the default of a formal
5044 -- parameter of the entry, since those are evaluated outside
5047 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5049 if Nkind (P) = N_Entry_Body
5050 or else (Nkind (P) = N_Subprogram_Body
5051 and then Is_Entry_Barrier_Function (P))
5055 ("?& should not be used in entry body (RM C.7(17))",
5058 ("\Program_Error will be raised at run time?", N, Nam);
5060 Make_Raise_Program_Error (Loc,
5061 Reason => PE_Current_Task_In_Entry_Body));
5062 Set_Etype (N, Rtype);
5069 -- Check that a procedure call does not occur in the context of the
5070 -- entry call statement of a conditional or timed entry call. Note that
5071 -- the case of a call to a subprogram renaming of an entry will also be
5072 -- rejected. The test for N not being an N_Entry_Call_Statement is
5073 -- defensive, covering the possibility that the processing of entry
5074 -- calls might reach this point due to later modifications of the code
5077 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5078 and then Nkind (N) /= N_Entry_Call_Statement
5079 and then Entry_Call_Statement (Parent (N)) = N
5081 if Ada_Version < Ada_2005 then
5082 Error_Msg_N ("entry call required in select statement", N);
5084 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5085 -- for a procedure_or_entry_call, the procedure_name or
5086 -- procedure_prefix of the procedure_call_statement shall denote
5087 -- an entry renamed by a procedure, or (a view of) a primitive
5088 -- subprogram of a limited interface whose first parameter is
5089 -- a controlling parameter.
5091 elsif Nkind (N) = N_Procedure_Call_Statement
5092 and then not Is_Renamed_Entry (Nam)
5093 and then not Is_Controlling_Limited_Procedure (Nam)
5096 ("entry call or dispatching primitive of interface required", N);
5100 -- Check that this is not a call to a protected procedure or entry from
5101 -- within a protected function.
5103 if Ekind (Current_Scope) = E_Function
5104 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5105 and then Ekind (Nam) /= E_Function
5106 and then Scope (Nam) = Scope (Current_Scope)
5108 Error_Msg_N ("within protected function, protected " &
5109 "object is constant", N);
5110 Error_Msg_N ("\cannot call operation that may modify it", N);
5113 -- Freeze the subprogram name if not in a spec-expression. Note that we
5114 -- freeze procedure calls as well as function calls. Procedure calls are
5115 -- not frozen according to the rules (RM 13.14(14)) because it is
5116 -- impossible to have a procedure call to a non-frozen procedure in pure
5117 -- Ada, but in the code that we generate in the expander, this rule
5118 -- needs extending because we can generate procedure calls that need
5121 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5122 Freeze_Expression (Subp);
5125 -- For a predefined operator, the type of the result is the type imposed
5126 -- by context, except for a predefined operation on universal fixed.
5127 -- Otherwise The type of the call is the type returned by the subprogram
5130 if Is_Predefined_Op (Nam) then
5131 if Etype (N) /= Universal_Fixed then
5135 -- If the subprogram returns an array type, and the context requires the
5136 -- component type of that array type, the node is really an indexing of
5137 -- the parameterless call. Resolve as such. A pathological case occurs
5138 -- when the type of the component is an access to the array type. In
5139 -- this case the call is truly ambiguous.
5141 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5143 ((Is_Array_Type (Etype (Nam))
5144 and then Covers (Typ, Component_Type (Etype (Nam))))
5145 or else (Is_Access_Type (Etype (Nam))
5146 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5149 Component_Type (Designated_Type (Etype (Nam))))))
5152 Index_Node : Node_Id;
5154 Ret_Type : constant Entity_Id := Etype (Nam);
5157 if Is_Access_Type (Ret_Type)
5158 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5161 ("cannot disambiguate function call and indexing", N);
5163 New_Subp := Relocate_Node (Subp);
5164 Set_Entity (Subp, Nam);
5166 if (Is_Array_Type (Ret_Type)
5167 and then Component_Type (Ret_Type) /= Any_Type)
5169 (Is_Access_Type (Ret_Type)
5171 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5173 if Needs_No_Actuals (Nam) then
5175 -- Indexed call to a parameterless function
5178 Make_Indexed_Component (Loc,
5180 Make_Function_Call (Loc,
5182 Expressions => Parameter_Associations (N));
5184 -- An Ada 2005 prefixed call to a primitive operation
5185 -- whose first parameter is the prefix. This prefix was
5186 -- prepended to the parameter list, which is actually a
5187 -- list of indexes. Remove the prefix in order to build
5188 -- the proper indexed component.
5191 Make_Indexed_Component (Loc,
5193 Make_Function_Call (Loc,
5195 Parameter_Associations =>
5197 (Remove_Head (Parameter_Associations (N)))),
5198 Expressions => Parameter_Associations (N));
5201 -- Preserve the parenthesis count of the node
5203 Set_Paren_Count (Index_Node, Paren_Count (N));
5205 -- Since we are correcting a node classification error made
5206 -- by the parser, we call Replace rather than Rewrite.
5208 Replace (N, Index_Node);
5210 Set_Etype (Prefix (N), Ret_Type);
5212 Resolve_Indexed_Component (N, Typ);
5213 Check_Elab_Call (Prefix (N));
5221 Set_Etype (N, Etype (Nam));
5224 -- In the case where the call is to an overloaded subprogram, Analyze
5225 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5226 -- such a case Normalize_Actuals needs to be called once more to order
5227 -- the actuals correctly. Otherwise the call will have the ordering
5228 -- given by the last overloaded subprogram whether this is the correct
5229 -- one being called or not.
5231 if Is_Overloaded (Subp) then
5232 Normalize_Actuals (N, Nam, False, Norm_OK);
5233 pragma Assert (Norm_OK);
5236 -- In any case, call is fully resolved now. Reset Overload flag, to
5237 -- prevent subsequent overload resolution if node is analyzed again
5239 Set_Is_Overloaded (Subp, False);
5240 Set_Is_Overloaded (N, False);
5242 -- If we are calling the current subprogram from immediately within its
5243 -- body, then that is the case where we can sometimes detect cases of
5244 -- infinite recursion statically. Do not try this in case restriction
5245 -- No_Recursion is in effect anyway, and do it only for source calls.
5247 if Comes_From_Source (N) then
5248 Scop := Current_Scope;
5250 -- Issue warning for possible infinite recursion in the absence
5251 -- of the No_Recursion restriction.
5253 if Same_Or_Aliased_Subprograms (Nam, Scop)
5254 and then not Restriction_Active (No_Recursion)
5255 and then Check_Infinite_Recursion (N)
5257 -- Here we detected and flagged an infinite recursion, so we do
5258 -- not need to test the case below for further warnings. Also if
5259 -- we now have a raise SE node, we are all done.
5261 if Nkind (N) = N_Raise_Storage_Error then
5265 -- If call is to immediately containing subprogram, then check for
5266 -- the case of a possible run-time detectable infinite recursion.
5269 Scope_Loop : while Scop /= Standard_Standard loop
5270 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5272 -- Although in general case, recursion is not statically
5273 -- checkable, the case of calling an immediately containing
5274 -- subprogram is easy to catch.
5276 Check_Restriction (No_Recursion, N);
5278 -- If the recursive call is to a parameterless subprogram,
5279 -- then even if we can't statically detect infinite
5280 -- recursion, this is pretty suspicious, and we output a
5281 -- warning. Furthermore, we will try later to detect some
5282 -- cases here at run time by expanding checking code (see
5283 -- Detect_Infinite_Recursion in package Exp_Ch6).
5285 -- If the recursive call is within a handler, do not emit a
5286 -- warning, because this is a common idiom: loop until input
5287 -- is correct, catch illegal input in handler and restart.
5289 if No (First_Formal (Nam))
5290 and then Etype (Nam) = Standard_Void_Type
5291 and then not Error_Posted (N)
5292 and then Nkind (Parent (N)) /= N_Exception_Handler
5294 -- For the case of a procedure call. We give the message
5295 -- only if the call is the first statement in a sequence
5296 -- of statements, or if all previous statements are
5297 -- simple assignments. This is simply a heuristic to
5298 -- decrease false positives, without losing too many good
5299 -- warnings. The idea is that these previous statements
5300 -- may affect global variables the procedure depends on.
5302 if Nkind (N) = N_Procedure_Call_Statement
5303 and then Is_List_Member (N)
5309 while Present (P) loop
5310 if Nkind (P) /= N_Assignment_Statement then
5319 -- Do not give warning if we are in a conditional context
5322 K : constant Node_Kind := Nkind (Parent (N));
5324 if (K = N_Loop_Statement
5325 and then Present (Iteration_Scheme (Parent (N))))
5326 or else K = N_If_Statement
5327 or else K = N_Elsif_Part
5328 or else K = N_Case_Statement_Alternative
5334 -- Here warning is to be issued
5336 Set_Has_Recursive_Call (Nam);
5338 ("?possible infinite recursion!", N);
5340 ("\?Storage_Error may be raised at run time!", N);
5346 Scop := Scope (Scop);
5347 end loop Scope_Loop;
5351 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5353 Check_Obsolescent_2005_Entity (Nam, Subp);
5355 -- If subprogram name is a predefined operator, it was given in
5356 -- functional notation. Replace call node with operator node, so
5357 -- that actuals can be resolved appropriately.
5359 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5360 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5363 elsif Present (Alias (Nam))
5364 and then Is_Predefined_Op (Alias (Nam))
5366 Resolve_Actuals (N, Nam);
5367 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5371 -- Create a transient scope if the resulting type requires it
5373 -- There are several notable exceptions:
5375 -- a) In init procs, the transient scope overhead is not needed, and is
5376 -- even incorrect when the call is a nested initialization call for a
5377 -- component whose expansion may generate adjust calls. However, if the
5378 -- call is some other procedure call within an initialization procedure
5379 -- (for example a call to Create_Task in the init_proc of the task
5380 -- run-time record) a transient scope must be created around this call.
5382 -- b) Enumeration literal pseudo-calls need no transient scope
5384 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5385 -- functions) do not use the secondary stack even though the return
5386 -- type may be unconstrained.
5388 -- d) Calls to a build-in-place function, since such functions may
5389 -- allocate their result directly in a target object, and cases where
5390 -- the result does get allocated in the secondary stack are checked for
5391 -- within the specialized Exp_Ch6 procedures for expanding those
5392 -- build-in-place calls.
5394 -- e) If the subprogram is marked Inline_Always, then even if it returns
5395 -- an unconstrained type the call does not require use of the secondary
5396 -- stack. However, inlining will only take place if the body to inline
5397 -- is already present. It may not be available if e.g. the subprogram is
5398 -- declared in a child instance.
5400 -- If this is an initialization call for a type whose construction
5401 -- uses the secondary stack, and it is not a nested call to initialize
5402 -- a component, we do need to create a transient scope for it. We
5403 -- check for this by traversing the type in Check_Initialization_Call.
5406 and then Has_Pragma_Inline_Always (Nam)
5407 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5408 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5412 elsif Ekind (Nam) = E_Enumeration_Literal
5413 or else Is_Build_In_Place_Function (Nam)
5414 or else Is_Intrinsic_Subprogram (Nam)
5418 elsif Expander_Active
5419 and then Is_Type (Etype (Nam))
5420 and then Requires_Transient_Scope (Etype (Nam))
5422 (not Within_Init_Proc
5424 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5426 Establish_Transient_Scope (N, Sec_Stack => True);
5428 -- If the call appears within the bounds of a loop, it will
5429 -- be rewritten and reanalyzed, nothing left to do here.
5431 if Nkind (N) /= N_Function_Call then
5435 elsif Is_Init_Proc (Nam)
5436 and then not Within_Init_Proc
5438 Check_Initialization_Call (N, Nam);
5441 -- A protected function cannot be called within the definition of the
5442 -- enclosing protected type.
5444 if Is_Protected_Type (Scope (Nam))
5445 and then In_Open_Scopes (Scope (Nam))
5446 and then not Has_Completion (Scope (Nam))
5449 ("& cannot be called before end of protected definition", N, Nam);
5452 -- Propagate interpretation to actuals, and add default expressions
5455 if Present (First_Formal (Nam)) then
5456 Resolve_Actuals (N, Nam);
5458 -- Overloaded literals are rewritten as function calls, for purpose of
5459 -- resolution. After resolution, we can replace the call with the
5462 elsif Ekind (Nam) = E_Enumeration_Literal then
5463 Copy_Node (Subp, N);
5464 Resolve_Entity_Name (N, Typ);
5466 -- Avoid validation, since it is a static function call
5468 Generate_Reference (Nam, Subp);
5472 -- If the subprogram is not global, then kill all saved values and
5473 -- checks. This is a bit conservative, since in many cases we could do
5474 -- better, but it is not worth the effort. Similarly, we kill constant
5475 -- values. However we do not need to do this for internal entities
5476 -- (unless they are inherited user-defined subprograms), since they
5477 -- are not in the business of molesting local values.
5479 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5480 -- kill all checks and values for calls to global subprograms. This
5481 -- takes care of the case where an access to a local subprogram is
5482 -- taken, and could be passed directly or indirectly and then called
5483 -- from almost any context.
5485 -- Note: we do not do this step till after resolving the actuals. That
5486 -- way we still take advantage of the current value information while
5487 -- scanning the actuals.
5489 -- We suppress killing values if we are processing the nodes associated
5490 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5491 -- type kills all the values as part of analyzing the code that
5492 -- initializes the dispatch tables.
5494 if Inside_Freezing_Actions = 0
5495 and then (not Is_Library_Level_Entity (Nam)
5496 or else Suppress_Value_Tracking_On_Call
5497 (Nearest_Dynamic_Scope (Current_Scope)))
5498 and then (Comes_From_Source (Nam)
5499 or else (Present (Alias (Nam))
5500 and then Comes_From_Source (Alias (Nam))))
5502 Kill_Current_Values;
5505 -- If we are warning about unread OUT parameters, this is the place to
5506 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5507 -- after the above call to Kill_Current_Values (since that call clears
5508 -- the Last_Assignment field of all local variables).
5510 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5511 and then Comes_From_Source (N)
5512 and then In_Extended_Main_Source_Unit (N)
5519 F := First_Formal (Nam);
5520 A := First_Actual (N);
5521 while Present (F) and then Present (A) loop
5522 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5523 and then Warn_On_Modified_As_Out_Parameter (F)
5524 and then Is_Entity_Name (A)
5525 and then Present (Entity (A))
5526 and then Comes_From_Source (N)
5527 and then Safe_To_Capture_Value (N, Entity (A))
5529 Set_Last_Assignment (Entity (A), A);
5538 -- If the subprogram is a primitive operation, check whether or not
5539 -- it is a correct dispatching call.
5541 if Is_Overloadable (Nam)
5542 and then Is_Dispatching_Operation (Nam)
5544 Check_Dispatching_Call (N);
5546 elsif Ekind (Nam) /= E_Subprogram_Type
5547 and then Is_Abstract_Subprogram (Nam)
5548 and then not In_Instance
5550 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5553 -- If this is a dispatching call, generate the appropriate reference,
5554 -- for better source navigation in GPS.
5556 if Is_Overloadable (Nam)
5557 and then Present (Controlling_Argument (N))
5559 Generate_Reference (Nam, Subp, 'R');
5561 -- Normal case, not a dispatching call. Generate a call reference.
5564 Generate_Reference (Nam, Subp, 's');
5567 if Is_Intrinsic_Subprogram (Nam) then
5568 Check_Intrinsic_Call (N);
5571 -- Check for violation of restriction No_Specific_Termination_Handlers
5572 -- and warn on a potentially blocking call to Abort_Task.
5574 if Is_RTE (Nam, RE_Set_Specific_Handler)
5576 Is_RTE (Nam, RE_Specific_Handler)
5578 Check_Restriction (No_Specific_Termination_Handlers, N);
5580 elsif Is_RTE (Nam, RE_Abort_Task) then
5581 Check_Potentially_Blocking_Operation (N);
5584 -- A call to Ada.Real_Time.Timing_Events.Set_Handler violates
5585 -- restriction No_Relative_Delay (AI-0211).
5587 if Is_RTE (Nam, RE_Set_Handler) then
5588 Check_Restriction (No_Relative_Delay, N);
5591 -- Issue an error for a call to an eliminated subprogram. We skip this
5592 -- in a spec expression, e.g. a call in a default parameter value, since
5593 -- we are not really doing a call at this time. That's important because
5594 -- the spec expression may itself belong to an eliminated subprogram.
5596 if not In_Spec_Expression then
5597 Check_For_Eliminated_Subprogram (Subp, Nam);
5600 -- All done, evaluate call and deal with elaboration issues
5603 Check_Elab_Call (N);
5604 Warn_On_Overlapping_Actuals (Nam, N);
5607 -----------------------------
5608 -- Resolve_Case_Expression --
5609 -----------------------------
5611 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5615 Alt := First (Alternatives (N));
5616 while Present (Alt) loop
5617 Resolve (Expression (Alt), Typ);
5622 Eval_Case_Expression (N);
5623 end Resolve_Case_Expression;
5625 -------------------------------
5626 -- Resolve_Character_Literal --
5627 -------------------------------
5629 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5630 B_Typ : constant Entity_Id := Base_Type (Typ);
5634 -- Verify that the character does belong to the type of the context
5636 Set_Etype (N, B_Typ);
5637 Eval_Character_Literal (N);
5639 -- Wide_Wide_Character literals must always be defined, since the set
5640 -- of wide wide character literals is complete, i.e. if a character
5641 -- literal is accepted by the parser, then it is OK for wide wide
5642 -- character (out of range character literals are rejected).
5644 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5647 -- Always accept character literal for type Any_Character, which
5648 -- occurs in error situations and in comparisons of literals, both
5649 -- of which should accept all literals.
5651 elsif B_Typ = Any_Character then
5654 -- For Standard.Character or a type derived from it, check that
5655 -- the literal is in range
5657 elsif Root_Type (B_Typ) = Standard_Character then
5658 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5662 -- For Standard.Wide_Character or a type derived from it, check
5663 -- that the literal is in range
5665 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5666 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5670 -- For Standard.Wide_Wide_Character or a type derived from it, we
5671 -- know the literal is in range, since the parser checked!
5673 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5676 -- If the entity is already set, this has already been resolved in a
5677 -- generic context, or comes from expansion. Nothing else to do.
5679 elsif Present (Entity (N)) then
5682 -- Otherwise we have a user defined character type, and we can use the
5683 -- standard visibility mechanisms to locate the referenced entity.
5686 C := Current_Entity (N);
5687 while Present (C) loop
5688 if Etype (C) = B_Typ then
5689 Set_Entity_With_Style_Check (N, C);
5690 Generate_Reference (C, N);
5698 -- If we fall through, then the literal does not match any of the
5699 -- entries of the enumeration type. This isn't just a constraint
5700 -- error situation, it is an illegality (see RM 4.2).
5703 ("character not defined for }", N, First_Subtype (B_Typ));
5704 end Resolve_Character_Literal;
5706 ---------------------------
5707 -- Resolve_Comparison_Op --
5708 ---------------------------
5710 -- Context requires a boolean type, and plays no role in resolution.
5711 -- Processing identical to that for equality operators. The result
5712 -- type is the base type, which matters when pathological subtypes of
5713 -- booleans with limited ranges are used.
5715 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5716 L : constant Node_Id := Left_Opnd (N);
5717 R : constant Node_Id := Right_Opnd (N);
5721 -- If this is an intrinsic operation which is not predefined, use the
5722 -- types of its declared arguments to resolve the possibly overloaded
5723 -- operands. Otherwise the operands are unambiguous and specify the
5726 if Scope (Entity (N)) /= Standard_Standard then
5727 T := Etype (First_Entity (Entity (N)));
5730 T := Find_Unique_Type (L, R);
5732 if T = Any_Fixed then
5733 T := Unique_Fixed_Point_Type (L);
5737 Set_Etype (N, Base_Type (Typ));
5738 Generate_Reference (T, N, ' ');
5740 -- Skip remaining processing if already set to Any_Type
5742 if T = Any_Type then
5746 -- Deal with other error cases
5748 if T = Any_String or else
5749 T = Any_Composite or else
5752 if T = Any_Character then
5753 Ambiguous_Character (L);
5755 Error_Msg_N ("ambiguous operands for comparison", N);
5758 Set_Etype (N, Any_Type);
5762 -- Resolve the operands if types OK
5766 Check_Unset_Reference (L);
5767 Check_Unset_Reference (R);
5768 Generate_Operator_Reference (N, T);
5769 Check_Low_Bound_Tested (N);
5771 -- Check comparison on unordered enumeration
5773 if Comes_From_Source (N)
5774 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
5776 Error_Msg_N ("comparison on unordered enumeration type?", N);
5779 -- Evaluate the relation (note we do this after the above check
5780 -- since this Eval call may change N to True/False.
5782 Eval_Relational_Op (N);
5783 end Resolve_Comparison_Op;
5785 ------------------------------------
5786 -- Resolve_Conditional_Expression --
5787 ------------------------------------
5789 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5790 Condition : constant Node_Id := First (Expressions (N));
5791 Then_Expr : constant Node_Id := Next (Condition);
5792 Else_Expr : Node_Id := Next (Then_Expr);
5795 Resolve (Condition, Any_Boolean);
5796 Resolve (Then_Expr, Typ);
5798 -- If ELSE expression present, just resolve using the determined type
5800 if Present (Else_Expr) then
5801 Resolve (Else_Expr, Typ);
5803 -- If no ELSE expression is present, root type must be Standard.Boolean
5804 -- and we provide a Standard.True result converted to the appropriate
5805 -- Boolean type (in case it is a derived boolean type).
5807 elsif Root_Type (Typ) = Standard_Boolean then
5809 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
5810 Analyze_And_Resolve (Else_Expr, Typ);
5811 Append_To (Expressions (N), Else_Expr);
5814 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
5815 Append_To (Expressions (N), Error);
5819 Eval_Conditional_Expression (N);
5820 end Resolve_Conditional_Expression;
5822 -----------------------------------------
5823 -- Resolve_Discrete_Subtype_Indication --
5824 -----------------------------------------
5826 procedure Resolve_Discrete_Subtype_Indication
5834 Analyze (Subtype_Mark (N));
5835 S := Entity (Subtype_Mark (N));
5837 if Nkind (Constraint (N)) /= N_Range_Constraint then
5838 Error_Msg_N ("expect range constraint for discrete type", N);
5839 Set_Etype (N, Any_Type);
5842 R := Range_Expression (Constraint (N));
5850 if Base_Type (S) /= Base_Type (Typ) then
5852 ("expect subtype of }", N, First_Subtype (Typ));
5854 -- Rewrite the constraint as a range of Typ
5855 -- to allow compilation to proceed further.
5858 Rewrite (Low_Bound (R),
5859 Make_Attribute_Reference (Sloc (Low_Bound (R)),
5860 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5861 Attribute_Name => Name_First));
5862 Rewrite (High_Bound (R),
5863 Make_Attribute_Reference (Sloc (High_Bound (R)),
5864 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5865 Attribute_Name => Name_First));
5869 Set_Etype (N, Etype (R));
5871 -- Additionally, we must check that the bounds are compatible
5872 -- with the given subtype, which might be different from the
5873 -- type of the context.
5875 Apply_Range_Check (R, S);
5877 -- ??? If the above check statically detects a Constraint_Error
5878 -- it replaces the offending bound(s) of the range R with a
5879 -- Constraint_Error node. When the itype which uses these bounds
5880 -- is frozen the resulting call to Duplicate_Subexpr generates
5881 -- a new temporary for the bounds.
5883 -- Unfortunately there are other itypes that are also made depend
5884 -- on these bounds, so when Duplicate_Subexpr is called they get
5885 -- a forward reference to the newly created temporaries and Gigi
5886 -- aborts on such forward references. This is probably sign of a
5887 -- more fundamental problem somewhere else in either the order of
5888 -- itype freezing or the way certain itypes are constructed.
5890 -- To get around this problem we call Remove_Side_Effects right
5891 -- away if either bounds of R are a Constraint_Error.
5894 L : constant Node_Id := Low_Bound (R);
5895 H : constant Node_Id := High_Bound (R);
5898 if Nkind (L) = N_Raise_Constraint_Error then
5899 Remove_Side_Effects (L);
5902 if Nkind (H) = N_Raise_Constraint_Error then
5903 Remove_Side_Effects (H);
5907 Check_Unset_Reference (Low_Bound (R));
5908 Check_Unset_Reference (High_Bound (R));
5911 end Resolve_Discrete_Subtype_Indication;
5913 -------------------------
5914 -- Resolve_Entity_Name --
5915 -------------------------
5917 -- Used to resolve identifiers and expanded names
5919 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
5920 E : constant Entity_Id := Entity (N);
5923 -- If garbage from errors, set to Any_Type and return
5925 if No (E) and then Total_Errors_Detected /= 0 then
5926 Set_Etype (N, Any_Type);
5930 -- Replace named numbers by corresponding literals. Note that this is
5931 -- the one case where Resolve_Entity_Name must reset the Etype, since
5932 -- it is currently marked as universal.
5934 if Ekind (E) = E_Named_Integer then
5936 Eval_Named_Integer (N);
5938 elsif Ekind (E) = E_Named_Real then
5940 Eval_Named_Real (N);
5942 -- For enumeration literals, we need to make sure that a proper style
5943 -- check is done, since such literals are overloaded, and thus we did
5944 -- not do a style check during the first phase of analysis.
5946 elsif Ekind (E) = E_Enumeration_Literal then
5947 Set_Entity_With_Style_Check (N, E);
5948 Eval_Entity_Name (N);
5950 -- Case of subtype name appearing as an operand in expression
5952 elsif Is_Type (E) then
5954 -- Allow use of subtype if it is a concurrent type where we are
5955 -- currently inside the body. This will eventually be expanded into a
5956 -- call to Self (for tasks) or _object (for protected objects). Any
5957 -- other use of a subtype is invalid.
5959 if Is_Concurrent_Type (E)
5960 and then In_Open_Scopes (E)
5964 -- Allow reference to type specifically marked as being OK in this
5965 -- context (this is used for example for type names in invariants).
5967 elsif OK_To_Reference (E) then
5970 -- Any other use is an eror
5974 ("invalid use of subtype mark in expression or call", N);
5977 -- Check discriminant use if entity is discriminant in current scope,
5978 -- i.e. discriminant of record or concurrent type currently being
5979 -- analyzed. Uses in corresponding body are unrestricted.
5981 elsif Ekind (E) = E_Discriminant
5982 and then Scope (E) = Current_Scope
5983 and then not Has_Completion (Current_Scope)
5985 Check_Discriminant_Use (N);
5987 -- A parameterless generic function cannot appear in a context that
5988 -- requires resolution.
5990 elsif Ekind (E) = E_Generic_Function then
5991 Error_Msg_N ("illegal use of generic function", N);
5993 elsif Ekind (E) = E_Out_Parameter
5994 and then Ada_Version = Ada_83
5995 and then (Nkind (Parent (N)) in N_Op
5996 or else (Nkind (Parent (N)) = N_Assignment_Statement
5997 and then N = Expression (Parent (N)))
5998 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6000 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6002 -- In all other cases, just do the possible static evaluation
6005 -- A deferred constant that appears in an expression must have a
6006 -- completion, unless it has been removed by in-place expansion of
6009 if Ekind (E) = E_Constant
6010 and then Comes_From_Source (E)
6011 and then No (Constant_Value (E))
6012 and then Is_Frozen (Etype (E))
6013 and then not In_Spec_Expression
6014 and then not Is_Imported (E)
6016 if No_Initialization (Parent (E))
6017 or else (Present (Full_View (E))
6018 and then No_Initialization (Parent (Full_View (E))))
6023 "deferred constant is frozen before completion", N);
6027 Eval_Entity_Name (N);
6029 end Resolve_Entity_Name;
6035 procedure Resolve_Entry (Entry_Name : Node_Id) is
6036 Loc : constant Source_Ptr := Sloc (Entry_Name);
6044 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6045 -- If the bounds of the entry family being called depend on task
6046 -- discriminants, build a new index subtype where a discriminant is
6047 -- replaced with the value of the discriminant of the target task.
6048 -- The target task is the prefix of the entry name in the call.
6050 -----------------------
6051 -- Actual_Index_Type --
6052 -----------------------
6054 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6055 Typ : constant Entity_Id := Entry_Index_Type (E);
6056 Tsk : constant Entity_Id := Scope (E);
6057 Lo : constant Node_Id := Type_Low_Bound (Typ);
6058 Hi : constant Node_Id := Type_High_Bound (Typ);
6061 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6062 -- If the bound is given by a discriminant, replace with a reference
6063 -- to the discriminant of the same name in the target task. If the
6064 -- entry name is the target of a requeue statement and the entry is
6065 -- in the current protected object, the bound to be used is the
6066 -- discriminal of the object (see Apply_Range_Checks for details of
6067 -- the transformation).
6069 -----------------------------
6070 -- Actual_Discriminant_Ref --
6071 -----------------------------
6073 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6074 Typ : constant Entity_Id := Etype (Bound);
6078 Remove_Side_Effects (Bound);
6080 if not Is_Entity_Name (Bound)
6081 or else Ekind (Entity (Bound)) /= E_Discriminant
6085 elsif Is_Protected_Type (Tsk)
6086 and then In_Open_Scopes (Tsk)
6087 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6089 -- Note: here Bound denotes a discriminant of the corresponding
6090 -- record type tskV, whose discriminal is a formal of the
6091 -- init-proc tskVIP. What we want is the body discriminal,
6092 -- which is associated to the discriminant of the original
6093 -- concurrent type tsk.
6095 return New_Occurrence_Of
6096 (Find_Body_Discriminal (Entity (Bound)), Loc);
6100 Make_Selected_Component (Loc,
6101 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6102 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6107 end Actual_Discriminant_Ref;
6109 -- Start of processing for Actual_Index_Type
6112 if not Has_Discriminants (Tsk)
6113 or else (not Is_Entity_Name (Lo)
6115 not Is_Entity_Name (Hi))
6117 return Entry_Index_Type (E);
6120 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6121 Set_Etype (New_T, Base_Type (Typ));
6122 Set_Size_Info (New_T, Typ);
6123 Set_RM_Size (New_T, RM_Size (Typ));
6124 Set_Scalar_Range (New_T,
6125 Make_Range (Sloc (Entry_Name),
6126 Low_Bound => Actual_Discriminant_Ref (Lo),
6127 High_Bound => Actual_Discriminant_Ref (Hi)));
6131 end Actual_Index_Type;
6133 -- Start of processing of Resolve_Entry
6136 -- Find name of entry being called, and resolve prefix of name
6137 -- with its own type. The prefix can be overloaded, and the name
6138 -- and signature of the entry must be taken into account.
6140 if Nkind (Entry_Name) = N_Indexed_Component then
6142 -- Case of dealing with entry family within the current tasks
6144 E_Name := Prefix (Entry_Name);
6147 E_Name := Entry_Name;
6150 if Is_Entity_Name (E_Name) then
6152 -- Entry call to an entry (or entry family) in the current task. This
6153 -- is legal even though the task will deadlock. Rewrite as call to
6156 -- This can also be a call to an entry in an enclosing task. If this
6157 -- is a single task, we have to retrieve its name, because the scope
6158 -- of the entry is the task type, not the object. If the enclosing
6159 -- task is a task type, the identity of the task is given by its own
6162 -- Finally this can be a requeue on an entry of the same task or
6163 -- protected object.
6165 S := Scope (Entity (E_Name));
6167 for J in reverse 0 .. Scope_Stack.Last loop
6168 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6169 and then not Comes_From_Source (S)
6171 -- S is an enclosing task or protected object. The concurrent
6172 -- declaration has been converted into a type declaration, and
6173 -- the object itself has an object declaration that follows
6174 -- the type in the same declarative part.
6176 Tsk := Next_Entity (S);
6177 while Etype (Tsk) /= S loop
6184 elsif S = Scope_Stack.Table (J).Entity then
6186 -- Call to current task. Will be transformed into call to Self
6194 Make_Selected_Component (Loc,
6195 Prefix => New_Occurrence_Of (S, Loc),
6197 New_Occurrence_Of (Entity (E_Name), Loc));
6198 Rewrite (E_Name, New_N);
6201 elsif Nkind (Entry_Name) = N_Selected_Component
6202 and then Is_Overloaded (Prefix (Entry_Name))
6204 -- Use the entry name (which must be unique at this point) to find
6205 -- the prefix that returns the corresponding task type or protected
6209 Pref : constant Node_Id := Prefix (Entry_Name);
6210 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6215 Get_First_Interp (Pref, I, It);
6216 while Present (It.Typ) loop
6217 if Scope (Ent) = It.Typ then
6218 Set_Etype (Pref, It.Typ);
6222 Get_Next_Interp (I, It);
6227 if Nkind (Entry_Name) = N_Selected_Component then
6228 Resolve (Prefix (Entry_Name));
6230 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6231 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6232 Resolve (Prefix (Prefix (Entry_Name)));
6233 Index := First (Expressions (Entry_Name));
6234 Resolve (Index, Entry_Index_Type (Nam));
6236 -- Up to this point the expression could have been the actual in a
6237 -- simple entry call, and be given by a named association.
6239 if Nkind (Index) = N_Parameter_Association then
6240 Error_Msg_N ("expect expression for entry index", Index);
6242 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6247 ------------------------
6248 -- Resolve_Entry_Call --
6249 ------------------------
6251 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6252 Entry_Name : constant Node_Id := Name (N);
6253 Loc : constant Source_Ptr := Sloc (Entry_Name);
6255 First_Named : Node_Id;
6262 -- We kill all checks here, because it does not seem worth the effort to
6263 -- do anything better, an entry call is a big operation.
6267 -- Processing of the name is similar for entry calls and protected
6268 -- operation calls. Once the entity is determined, we can complete
6269 -- the resolution of the actuals.
6271 -- The selector may be overloaded, in the case of a protected object
6272 -- with overloaded functions. The type of the context is used for
6275 if Nkind (Entry_Name) = N_Selected_Component
6276 and then Is_Overloaded (Selector_Name (Entry_Name))
6277 and then Typ /= Standard_Void_Type
6284 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6285 while Present (It.Typ) loop
6286 if Covers (Typ, It.Typ) then
6287 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6288 Set_Etype (Entry_Name, It.Typ);
6290 Generate_Reference (It.Typ, N, ' ');
6293 Get_Next_Interp (I, It);
6298 Resolve_Entry (Entry_Name);
6300 if Nkind (Entry_Name) = N_Selected_Component then
6302 -- Simple entry call
6304 Nam := Entity (Selector_Name (Entry_Name));
6305 Obj := Prefix (Entry_Name);
6306 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6308 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6310 -- Call to member of entry family
6312 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6313 Obj := Prefix (Prefix (Entry_Name));
6314 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6317 -- We cannot in general check the maximum depth of protected entry
6318 -- calls at compile time. But we can tell that any protected entry
6319 -- call at all violates a specified nesting depth of zero.
6321 if Is_Protected_Type (Scope (Nam)) then
6322 Check_Restriction (Max_Entry_Queue_Length, N);
6325 -- Use context type to disambiguate a protected function that can be
6326 -- called without actuals and that returns an array type, and where
6327 -- the argument list may be an indexing of the returned value.
6329 if Ekind (Nam) = E_Function
6330 and then Needs_No_Actuals (Nam)
6331 and then Present (Parameter_Associations (N))
6333 ((Is_Array_Type (Etype (Nam))
6334 and then Covers (Typ, Component_Type (Etype (Nam))))
6336 or else (Is_Access_Type (Etype (Nam))
6337 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6338 and then Covers (Typ,
6339 Component_Type (Designated_Type (Etype (Nam))))))
6342 Index_Node : Node_Id;
6346 Make_Indexed_Component (Loc,
6348 Make_Function_Call (Loc,
6349 Name => Relocate_Node (Entry_Name)),
6350 Expressions => Parameter_Associations (N));
6352 -- Since we are correcting a node classification error made by
6353 -- the parser, we call Replace rather than Rewrite.
6355 Replace (N, Index_Node);
6356 Set_Etype (Prefix (N), Etype (Nam));
6358 Resolve_Indexed_Component (N, Typ);
6363 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6364 and then Present (PPC_Wrapper (Nam))
6365 and then Current_Scope /= PPC_Wrapper (Nam)
6367 -- Rewrite as call to the precondition wrapper, adding the task
6368 -- object to the list of actuals. If the call is to a member of
6369 -- an entry family, include the index as well.
6373 New_Actuals : List_Id;
6375 New_Actuals := New_List (Obj);
6377 if Nkind (Entry_Name) = N_Indexed_Component then
6378 Append_To (New_Actuals,
6379 New_Copy_Tree (First (Expressions (Entry_Name))));
6382 Append_List (Parameter_Associations (N), New_Actuals);
6384 Make_Procedure_Call_Statement (Loc,
6386 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6387 Parameter_Associations => New_Actuals);
6388 Rewrite (N, New_Call);
6389 Analyze_And_Resolve (N);
6394 -- The operation name may have been overloaded. Order the actuals
6395 -- according to the formals of the resolved entity, and set the
6396 -- return type to that of the operation.
6399 Normalize_Actuals (N, Nam, False, Norm_OK);
6400 pragma Assert (Norm_OK);
6401 Set_Etype (N, Etype (Nam));
6404 Resolve_Actuals (N, Nam);
6406 -- Create a call reference to the entry
6408 Generate_Reference (Nam, Entry_Name, 's');
6410 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6411 Check_Potentially_Blocking_Operation (N);
6414 -- Verify that a procedure call cannot masquerade as an entry
6415 -- call where an entry call is expected.
6417 if Ekind (Nam) = E_Procedure then
6418 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6419 and then N = Entry_Call_Statement (Parent (N))
6421 Error_Msg_N ("entry call required in select statement", N);
6423 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6424 and then N = Triggering_Statement (Parent (N))
6426 Error_Msg_N ("triggering statement cannot be procedure call", N);
6428 elsif Ekind (Scope (Nam)) = E_Task_Type
6429 and then not In_Open_Scopes (Scope (Nam))
6431 Error_Msg_N ("task has no entry with this name", Entry_Name);
6435 -- After resolution, entry calls and protected procedure calls are
6436 -- changed into entry calls, for expansion. The structure of the node
6437 -- does not change, so it can safely be done in place. Protected
6438 -- function calls must keep their structure because they are
6441 if Ekind (Nam) /= E_Function then
6443 -- A protected operation that is not a function may modify the
6444 -- corresponding object, and cannot apply to a constant. If this
6445 -- is an internal call, the prefix is the type itself.
6447 if Is_Protected_Type (Scope (Nam))
6448 and then not Is_Variable (Obj)
6449 and then (not Is_Entity_Name (Obj)
6450 or else not Is_Type (Entity (Obj)))
6453 ("prefix of protected procedure or entry call must be variable",
6457 Actuals := Parameter_Associations (N);
6458 First_Named := First_Named_Actual (N);
6461 Make_Entry_Call_Statement (Loc,
6463 Parameter_Associations => Actuals));
6465 Set_First_Named_Actual (N, First_Named);
6466 Set_Analyzed (N, True);
6468 -- Protected functions can return on the secondary stack, in which
6469 -- case we must trigger the transient scope mechanism.
6471 elsif Expander_Active
6472 and then Requires_Transient_Scope (Etype (Nam))
6474 Establish_Transient_Scope (N, Sec_Stack => True);
6476 end Resolve_Entry_Call;
6478 -------------------------
6479 -- Resolve_Equality_Op --
6480 -------------------------
6482 -- Both arguments must have the same type, and the boolean context does
6483 -- not participate in the resolution. The first pass verifies that the
6484 -- interpretation is not ambiguous, and the type of the left argument is
6485 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6486 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6487 -- though they carry a single (universal) type. Diagnose this case here.
6489 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6490 L : constant Node_Id := Left_Opnd (N);
6491 R : constant Node_Id := Right_Opnd (N);
6492 T : Entity_Id := Find_Unique_Type (L, R);
6494 procedure Check_Conditional_Expression (Cond : Node_Id);
6495 -- The resolution rule for conditional expressions requires that each
6496 -- such must have a unique type. This means that if several dependent
6497 -- expressions are of a non-null anonymous access type, and the context
6498 -- does not impose an expected type (as can be the case in an equality
6499 -- operation) the expression must be rejected.
6501 function Find_Unique_Access_Type return Entity_Id;
6502 -- In the case of allocators, make a last-ditch attempt to find a single
6503 -- access type with the right designated type. This is semantically
6504 -- dubious, and of no interest to any real code, but c48008a makes it
6507 ----------------------------------
6508 -- Check_Conditional_Expression --
6509 ----------------------------------
6511 procedure Check_Conditional_Expression (Cond : Node_Id) is
6512 Then_Expr : Node_Id;
6513 Else_Expr : Node_Id;
6516 if Nkind (Cond) = N_Conditional_Expression then
6517 Then_Expr := Next (First (Expressions (Cond)));
6518 Else_Expr := Next (Then_Expr);
6520 if Nkind (Then_Expr) /= N_Null
6521 and then Nkind (Else_Expr) /= N_Null
6524 ("cannot determine type of conditional expression", Cond);
6527 end Check_Conditional_Expression;
6529 -----------------------------
6530 -- Find_Unique_Access_Type --
6531 -----------------------------
6533 function Find_Unique_Access_Type return Entity_Id is
6539 if Ekind (Etype (R)) = E_Allocator_Type then
6540 Acc := Designated_Type (Etype (R));
6541 elsif Ekind (Etype (L)) = E_Allocator_Type then
6542 Acc := Designated_Type (Etype (L));
6548 while S /= Standard_Standard loop
6549 E := First_Entity (S);
6550 while Present (E) loop
6552 and then Is_Access_Type (E)
6553 and then Ekind (E) /= E_Allocator_Type
6554 and then Designated_Type (E) = Base_Type (Acc)
6566 end Find_Unique_Access_Type;
6568 -- Start of processing for Resolve_Equality_Op
6571 Set_Etype (N, Base_Type (Typ));
6572 Generate_Reference (T, N, ' ');
6574 if T = Any_Fixed then
6575 T := Unique_Fixed_Point_Type (L);
6578 if T /= Any_Type then
6580 or else T = Any_Composite
6581 or else T = Any_Character
6583 if T = Any_Character then
6584 Ambiguous_Character (L);
6586 Error_Msg_N ("ambiguous operands for equality", N);
6589 Set_Etype (N, Any_Type);
6592 elsif T = Any_Access
6593 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6595 T := Find_Unique_Access_Type;
6598 Error_Msg_N ("ambiguous operands for equality", N);
6599 Set_Etype (N, Any_Type);
6603 -- Conditional expressions must have a single type, and if the
6604 -- context does not impose one the dependent expressions cannot
6605 -- be anonymous access types.
6607 elsif Ada_Version >= Ada_2012
6608 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6609 E_Anonymous_Access_Subprogram_Type)
6610 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6611 E_Anonymous_Access_Subprogram_Type)
6613 Check_Conditional_Expression (L);
6614 Check_Conditional_Expression (R);
6620 -- If the unique type is a class-wide type then it will be expanded
6621 -- into a dispatching call to the predefined primitive. Therefore we
6622 -- check here for potential violation of such restriction.
6624 if Is_Class_Wide_Type (T) then
6625 Check_Restriction (No_Dispatching_Calls, N);
6628 if Warn_On_Redundant_Constructs
6629 and then Comes_From_Source (N)
6630 and then Is_Entity_Name (R)
6631 and then Entity (R) = Standard_True
6632 and then Comes_From_Source (R)
6634 Error_Msg_N -- CODEFIX
6635 ("?comparison with True is redundant!", R);
6638 Check_Unset_Reference (L);
6639 Check_Unset_Reference (R);
6640 Generate_Operator_Reference (N, T);
6641 Check_Low_Bound_Tested (N);
6643 -- If this is an inequality, it may be the implicit inequality
6644 -- created for a user-defined operation, in which case the corres-
6645 -- ponding equality operation is not intrinsic, and the operation
6646 -- cannot be constant-folded. Else fold.
6648 if Nkind (N) = N_Op_Eq
6649 or else Comes_From_Source (Entity (N))
6650 or else Ekind (Entity (N)) = E_Operator
6651 or else Is_Intrinsic_Subprogram
6652 (Corresponding_Equality (Entity (N)))
6654 Eval_Relational_Op (N);
6656 elsif Nkind (N) = N_Op_Ne
6657 and then Is_Abstract_Subprogram (Entity (N))
6659 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6662 -- Ada 2005: If one operand is an anonymous access type, convert the
6663 -- other operand to it, to ensure that the underlying types match in
6664 -- the back-end. Same for access_to_subprogram, and the conversion
6665 -- verifies that the types are subtype conformant.
6667 -- We apply the same conversion in the case one of the operands is a
6668 -- private subtype of the type of the other.
6670 -- Why the Expander_Active test here ???
6674 (Ekind_In (T, E_Anonymous_Access_Type,
6675 E_Anonymous_Access_Subprogram_Type)
6676 or else Is_Private_Type (T))
6678 if Etype (L) /= T then
6680 Make_Unchecked_Type_Conversion (Sloc (L),
6681 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6682 Expression => Relocate_Node (L)));
6683 Analyze_And_Resolve (L, T);
6686 if (Etype (R)) /= T then
6688 Make_Unchecked_Type_Conversion (Sloc (R),
6689 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6690 Expression => Relocate_Node (R)));
6691 Analyze_And_Resolve (R, T);
6695 end Resolve_Equality_Op;
6697 ----------------------------------
6698 -- Resolve_Explicit_Dereference --
6699 ----------------------------------
6701 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6702 Loc : constant Source_Ptr := Sloc (N);
6704 P : constant Node_Id := Prefix (N);
6709 Check_Fully_Declared_Prefix (Typ, P);
6711 if Is_Overloaded (P) then
6713 -- Use the context type to select the prefix that has the correct
6716 Get_First_Interp (P, I, It);
6717 while Present (It.Typ) loop
6718 exit when Is_Access_Type (It.Typ)
6719 and then Covers (Typ, Designated_Type (It.Typ));
6720 Get_Next_Interp (I, It);
6723 if Present (It.Typ) then
6724 Resolve (P, It.Typ);
6726 -- If no interpretation covers the designated type of the prefix,
6727 -- this is the pathological case where not all implementations of
6728 -- the prefix allow the interpretation of the node as a call. Now
6729 -- that the expected type is known, Remove other interpretations
6730 -- from prefix, rewrite it as a call, and resolve again, so that
6731 -- the proper call node is generated.
6733 Get_First_Interp (P, I, It);
6734 while Present (It.Typ) loop
6735 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6739 Get_Next_Interp (I, It);
6743 Make_Function_Call (Loc,
6745 Make_Explicit_Dereference (Loc,
6747 Parameter_Associations => New_List);
6749 Save_Interps (N, New_N);
6751 Analyze_And_Resolve (N, Typ);
6755 Set_Etype (N, Designated_Type (It.Typ));
6761 if Is_Access_Type (Etype (P)) then
6762 Apply_Access_Check (N);
6765 -- If the designated type is a packed unconstrained array type, and the
6766 -- explicit dereference is not in the context of an attribute reference,
6767 -- then we must compute and set the actual subtype, since it is needed
6768 -- by Gigi. The reason we exclude the attribute case is that this is
6769 -- handled fine by Gigi, and in fact we use such attributes to build the
6770 -- actual subtype. We also exclude generated code (which builds actual
6771 -- subtypes directly if they are needed).
6773 if Is_Array_Type (Etype (N))
6774 and then Is_Packed (Etype (N))
6775 and then not Is_Constrained (Etype (N))
6776 and then Nkind (Parent (N)) /= N_Attribute_Reference
6777 and then Comes_From_Source (N)
6779 Set_Etype (N, Get_Actual_Subtype (N));
6782 -- Note: No Eval processing is required for an explicit dereference,
6783 -- because such a name can never be static.
6785 end Resolve_Explicit_Dereference;
6787 -------------------------------------
6788 -- Resolve_Expression_With_Actions --
6789 -------------------------------------
6791 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
6794 end Resolve_Expression_With_Actions;
6796 -------------------------------
6797 -- Resolve_Indexed_Component --
6798 -------------------------------
6800 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6801 Name : constant Node_Id := Prefix (N);
6803 Array_Type : Entity_Id := Empty; -- to prevent junk warning
6807 if Is_Overloaded (Name) then
6809 -- Use the context type to select the prefix that yields the correct
6815 I1 : Interp_Index := 0;
6816 P : constant Node_Id := Prefix (N);
6817 Found : Boolean := False;
6820 Get_First_Interp (P, I, It);
6821 while Present (It.Typ) loop
6822 if (Is_Array_Type (It.Typ)
6823 and then Covers (Typ, Component_Type (It.Typ)))
6824 or else (Is_Access_Type (It.Typ)
6825 and then Is_Array_Type (Designated_Type (It.Typ))
6827 (Typ, Component_Type (Designated_Type (It.Typ))))
6830 It := Disambiguate (P, I1, I, Any_Type);
6832 if It = No_Interp then
6833 Error_Msg_N ("ambiguous prefix for indexing", N);
6839 Array_Type := It.Typ;
6845 Array_Type := It.Typ;
6850 Get_Next_Interp (I, It);
6855 Array_Type := Etype (Name);
6858 Resolve (Name, Array_Type);
6859 Array_Type := Get_Actual_Subtype_If_Available (Name);
6861 -- If prefix is access type, dereference to get real array type.
6862 -- Note: we do not apply an access check because the expander always
6863 -- introduces an explicit dereference, and the check will happen there.
6865 if Is_Access_Type (Array_Type) then
6866 Array_Type := Designated_Type (Array_Type);
6869 -- If name was overloaded, set component type correctly now
6870 -- If a misplaced call to an entry family (which has no index types)
6871 -- return. Error will be diagnosed from calling context.
6873 if Is_Array_Type (Array_Type) then
6874 Set_Etype (N, Component_Type (Array_Type));
6879 Index := First_Index (Array_Type);
6880 Expr := First (Expressions (N));
6882 -- The prefix may have resolved to a string literal, in which case its
6883 -- etype has a special representation. This is only possible currently
6884 -- if the prefix is a static concatenation, written in functional
6887 if Ekind (Array_Type) = E_String_Literal_Subtype then
6888 Resolve (Expr, Standard_Positive);
6891 while Present (Index) and Present (Expr) loop
6892 Resolve (Expr, Etype (Index));
6893 Check_Unset_Reference (Expr);
6895 if Is_Scalar_Type (Etype (Expr)) then
6896 Apply_Scalar_Range_Check (Expr, Etype (Index));
6898 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
6906 -- Do not generate the warning on suspicious index if we are analyzing
6907 -- package Ada.Tags; otherwise we will report the warning with the
6908 -- Prims_Ptr field of the dispatch table.
6910 if Scope (Etype (Prefix (N))) = Standard_Standard
6912 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
6915 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
6916 Eval_Indexed_Component (N);
6919 -- If the array type is atomic, and is packed, and we are in a left side
6920 -- context, then this is worth a warning, since we have a situation
6921 -- where the access to the component may cause extra read/writes of
6922 -- the atomic array object, which could be considered unexpected.
6924 if Nkind (N) = N_Indexed_Component
6925 and then (Is_Atomic (Array_Type)
6926 or else (Is_Entity_Name (Prefix (N))
6927 and then Is_Atomic (Entity (Prefix (N)))))
6928 and then Is_Bit_Packed_Array (Array_Type)
6931 Error_Msg_N ("?assignment to component of packed atomic array",
6933 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
6936 end Resolve_Indexed_Component;
6938 -----------------------------
6939 -- Resolve_Integer_Literal --
6940 -----------------------------
6942 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
6945 Eval_Integer_Literal (N);
6946 end Resolve_Integer_Literal;
6948 --------------------------------
6949 -- Resolve_Intrinsic_Operator --
6950 --------------------------------
6952 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
6953 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6955 Orig_Op : constant Entity_Id := Entity (N);
6960 -- We must preserve the original entity in a generic setting, so that
6961 -- the legality of the operation can be verified in an instance.
6963 if not Expander_Active then
6968 while Scope (Op) /= Standard_Standard loop
6970 pragma Assert (Present (Op));
6974 Set_Is_Overloaded (N, False);
6976 -- If the operand type is private, rewrite with suitable conversions on
6977 -- the operands and the result, to expose the proper underlying numeric
6980 if Is_Private_Type (Typ) then
6981 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
6983 if Nkind (N) = N_Op_Expon then
6984 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
6986 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6989 if Nkind (Arg1) = N_Type_Conversion then
6990 Save_Interps (Left_Opnd (N), Expression (Arg1));
6993 if Nkind (Arg2) = N_Type_Conversion then
6994 Save_Interps (Right_Opnd (N), Expression (Arg2));
6997 Set_Left_Opnd (N, Arg1);
6998 Set_Right_Opnd (N, Arg2);
7000 Set_Etype (N, Btyp);
7001 Rewrite (N, Unchecked_Convert_To (Typ, N));
7004 elsif Typ /= Etype (Left_Opnd (N))
7005 or else Typ /= Etype (Right_Opnd (N))
7007 -- Add explicit conversion where needed, and save interpretations in
7008 -- case operands are overloaded. If the context is a VMS operation,
7009 -- assert that the conversion is legal (the operands have the proper
7010 -- types to select the VMS intrinsic). Note that in rare cases the
7011 -- VMS operators may be visible, but the default System is being used
7012 -- and Address is a private type.
7014 Arg1 := Convert_To (Typ, Left_Opnd (N));
7015 Arg2 := Convert_To (Typ, Right_Opnd (N));
7017 if Nkind (Arg1) = N_Type_Conversion then
7018 Save_Interps (Left_Opnd (N), Expression (Arg1));
7020 if Is_VMS_Operator (Orig_Op) then
7021 Set_Conversion_OK (Arg1);
7024 Save_Interps (Left_Opnd (N), Arg1);
7027 if Nkind (Arg2) = N_Type_Conversion then
7028 Save_Interps (Right_Opnd (N), Expression (Arg2));
7030 if Is_VMS_Operator (Orig_Op) then
7031 Set_Conversion_OK (Arg2);
7034 Save_Interps (Right_Opnd (N), Arg2);
7037 Rewrite (Left_Opnd (N), Arg1);
7038 Rewrite (Right_Opnd (N), Arg2);
7041 Resolve_Arithmetic_Op (N, Typ);
7044 Resolve_Arithmetic_Op (N, Typ);
7046 end Resolve_Intrinsic_Operator;
7048 --------------------------------------
7049 -- Resolve_Intrinsic_Unary_Operator --
7050 --------------------------------------
7052 procedure Resolve_Intrinsic_Unary_Operator
7056 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7062 while Scope (Op) /= Standard_Standard loop
7064 pragma Assert (Present (Op));
7069 if Is_Private_Type (Typ) then
7070 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7071 Save_Interps (Right_Opnd (N), Expression (Arg2));
7073 Set_Right_Opnd (N, Arg2);
7075 Set_Etype (N, Btyp);
7076 Rewrite (N, Unchecked_Convert_To (Typ, N));
7080 Resolve_Unary_Op (N, Typ);
7082 end Resolve_Intrinsic_Unary_Operator;
7084 ------------------------
7085 -- Resolve_Logical_Op --
7086 ------------------------
7088 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7092 Check_No_Direct_Boolean_Operators (N);
7094 -- Predefined operations on scalar types yield the base type. On the
7095 -- other hand, logical operations on arrays yield the type of the
7096 -- arguments (and the context).
7098 if Is_Array_Type (Typ) then
7101 B_Typ := Base_Type (Typ);
7104 -- OK if this is a VMS-specific intrinsic operation
7106 if Is_VMS_Operator (Entity (N)) then
7109 -- The following test is required because the operands of the operation
7110 -- may be literals, in which case the resulting type appears to be
7111 -- compatible with a signed integer type, when in fact it is compatible
7112 -- only with modular types. If the context itself is universal, the
7113 -- operation is illegal.
7115 elsif not Valid_Boolean_Arg (Typ) then
7116 Error_Msg_N ("invalid context for logical operation", N);
7117 Set_Etype (N, Any_Type);
7120 elsif Typ = Any_Modular then
7122 ("no modular type available in this context", N);
7123 Set_Etype (N, Any_Type);
7125 elsif Is_Modular_Integer_Type (Typ)
7126 and then Etype (Left_Opnd (N)) = Universal_Integer
7127 and then Etype (Right_Opnd (N)) = Universal_Integer
7129 Check_For_Visible_Operator (N, B_Typ);
7132 Resolve (Left_Opnd (N), B_Typ);
7133 Resolve (Right_Opnd (N), B_Typ);
7135 Check_Unset_Reference (Left_Opnd (N));
7136 Check_Unset_Reference (Right_Opnd (N));
7138 Set_Etype (N, B_Typ);
7139 Generate_Operator_Reference (N, B_Typ);
7140 Eval_Logical_Op (N);
7141 end Resolve_Logical_Op;
7143 ---------------------------
7144 -- Resolve_Membership_Op --
7145 ---------------------------
7147 -- The context can only be a boolean type, and does not determine
7148 -- the arguments. Arguments should be unambiguous, but the preference
7149 -- rule for universal types applies.
7151 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7152 pragma Warnings (Off, Typ);
7154 L : constant Node_Id := Left_Opnd (N);
7155 R : constant Node_Id := Right_Opnd (N);
7158 procedure Resolve_Set_Membership;
7159 -- Analysis has determined a unique type for the left operand.
7160 -- Use it to resolve the disjuncts.
7162 ----------------------------
7163 -- Resolve_Set_Membership --
7164 ----------------------------
7166 procedure Resolve_Set_Membership is
7170 Resolve (L, Etype (L));
7172 Alt := First (Alternatives (N));
7173 while Present (Alt) loop
7175 -- Alternative is an expression, a range
7176 -- or a subtype mark.
7178 if not Is_Entity_Name (Alt)
7179 or else not Is_Type (Entity (Alt))
7181 Resolve (Alt, Etype (L));
7186 end Resolve_Set_Membership;
7188 -- Start of processing for Resolve_Membership_Op
7191 if L = Error or else R = Error then
7195 if Present (Alternatives (N)) then
7196 Resolve_Set_Membership;
7199 elsif not Is_Overloaded (R)
7201 (Etype (R) = Universal_Integer or else
7202 Etype (R) = Universal_Real)
7203 and then Is_Overloaded (L)
7207 -- Ada 2005 (AI-251): Support the following case:
7209 -- type I is interface;
7210 -- type T is tagged ...
7212 -- function Test (O : I'Class) is
7214 -- return O in T'Class.
7217 -- In this case we have nothing else to do. The membership test will be
7218 -- done at run time.
7220 elsif Ada_Version >= Ada_2005
7221 and then Is_Class_Wide_Type (Etype (L))
7222 and then Is_Interface (Etype (L))
7223 and then Is_Class_Wide_Type (Etype (R))
7224 and then not Is_Interface (Etype (R))
7229 T := Intersect_Types (L, R);
7232 -- If mixed-mode operations are present and operands are all literal,
7233 -- the only interpretation involves Duration, which is probably not
7234 -- the intention of the programmer.
7236 if T = Any_Fixed then
7237 T := Unique_Fixed_Point_Type (N);
7239 if T = Any_Type then
7245 Check_Unset_Reference (L);
7247 if Nkind (R) = N_Range
7248 and then not Is_Scalar_Type (T)
7250 Error_Msg_N ("scalar type required for range", R);
7253 if Is_Entity_Name (R) then
7254 Freeze_Expression (R);
7257 Check_Unset_Reference (R);
7260 Eval_Membership_Op (N);
7261 end Resolve_Membership_Op;
7267 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7268 Loc : constant Source_Ptr := Sloc (N);
7271 -- Handle restriction against anonymous null access values This
7272 -- restriction can be turned off using -gnatdj.
7274 -- Ada 2005 (AI-231): Remove restriction
7276 if Ada_Version < Ada_2005
7277 and then not Debug_Flag_J
7278 and then Ekind (Typ) = E_Anonymous_Access_Type
7279 and then Comes_From_Source (N)
7281 -- In the common case of a call which uses an explicitly null value
7282 -- for an access parameter, give specialized error message.
7284 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7288 ("null is not allowed as argument for an access parameter", N);
7290 -- Standard message for all other cases (are there any?)
7294 ("null cannot be of an anonymous access type", N);
7298 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7299 -- assignment to a null-excluding object
7301 if Ada_Version >= Ada_2005
7302 and then Can_Never_Be_Null (Typ)
7303 and then Nkind (Parent (N)) = N_Assignment_Statement
7305 if not Inside_Init_Proc then
7307 (Compile_Time_Constraint_Error (N,
7308 "(Ada 2005) null not allowed in null-excluding objects?"),
7309 Make_Raise_Constraint_Error (Loc,
7310 Reason => CE_Access_Check_Failed));
7313 Make_Raise_Constraint_Error (Loc,
7314 Reason => CE_Access_Check_Failed));
7318 -- In a distributed context, null for a remote access to subprogram may
7319 -- need to be replaced with a special record aggregate. In this case,
7320 -- return after having done the transformation.
7322 if (Ekind (Typ) = E_Record_Type
7323 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7324 and then Remote_AST_Null_Value (N, Typ)
7329 -- The null literal takes its type from the context
7334 -----------------------
7335 -- Resolve_Op_Concat --
7336 -----------------------
7338 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7340 -- We wish to avoid deep recursion, because concatenations are often
7341 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7342 -- operands nonrecursively until we find something that is not a simple
7343 -- concatenation (A in this case). We resolve that, and then walk back
7344 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7345 -- to do the rest of the work at each level. The Parent pointers allow
7346 -- us to avoid recursion, and thus avoid running out of memory. See also
7347 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7353 -- The following code is equivalent to:
7355 -- Resolve_Op_Concat_First (NN, Typ);
7356 -- Resolve_Op_Concat_Arg (N, ...);
7357 -- Resolve_Op_Concat_Rest (N, Typ);
7359 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7360 -- operand is a concatenation.
7362 -- Walk down left operands
7365 Resolve_Op_Concat_First (NN, Typ);
7366 Op1 := Left_Opnd (NN);
7367 exit when not (Nkind (Op1) = N_Op_Concat
7368 and then not Is_Array_Type (Component_Type (Typ))
7369 and then Entity (Op1) = Entity (NN));
7373 -- Now (given the above example) NN is A&B and Op1 is A
7375 -- First resolve Op1 ...
7377 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7379 -- ... then walk NN back up until we reach N (where we started), calling
7380 -- Resolve_Op_Concat_Rest along the way.
7383 Resolve_Op_Concat_Rest (NN, Typ);
7387 end Resolve_Op_Concat;
7389 ---------------------------
7390 -- Resolve_Op_Concat_Arg --
7391 ---------------------------
7393 procedure Resolve_Op_Concat_Arg
7399 Btyp : constant Entity_Id := Base_Type (Typ);
7404 or else (not Is_Overloaded (Arg)
7405 and then Etype (Arg) /= Any_Composite
7406 and then Covers (Component_Type (Typ), Etype (Arg)))
7408 Resolve (Arg, Component_Type (Typ));
7410 Resolve (Arg, Btyp);
7413 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
7414 if Nkind (Arg) = N_Aggregate
7415 and then Is_Composite_Type (Component_Type (Typ))
7417 if Is_Private_Type (Component_Type (Typ)) then
7418 Resolve (Arg, Btyp);
7420 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7421 Set_Etype (Arg, Any_Type);
7425 if Is_Overloaded (Arg)
7426 and then Has_Compatible_Type (Arg, Typ)
7427 and then Etype (Arg) /= Any_Type
7435 Get_First_Interp (Arg, I, It);
7437 Get_Next_Interp (I, It);
7439 -- Special-case the error message when the overloading is
7440 -- caused by a function that yields an array and can be
7441 -- called without parameters.
7443 if It.Nam = Func then
7444 Error_Msg_Sloc := Sloc (Func);
7445 Error_Msg_N ("ambiguous call to function#", Arg);
7447 ("\\interpretation as call yields&", Arg, Typ);
7449 ("\\interpretation as indexing of call yields&",
7450 Arg, Component_Type (Typ));
7454 ("ambiguous operand for concatenation!", Arg);
7455 Get_First_Interp (Arg, I, It);
7456 while Present (It.Nam) loop
7457 Error_Msg_Sloc := Sloc (It.Nam);
7459 if Base_Type (It.Typ) = Base_Type (Typ)
7460 or else Base_Type (It.Typ) =
7461 Base_Type (Component_Type (Typ))
7463 Error_Msg_N -- CODEFIX
7464 ("\\possible interpretation#", Arg);
7467 Get_Next_Interp (I, It);
7473 Resolve (Arg, Component_Type (Typ));
7475 if Nkind (Arg) = N_String_Literal then
7476 Set_Etype (Arg, Component_Type (Typ));
7479 if Arg = Left_Opnd (N) then
7480 Set_Is_Component_Left_Opnd (N);
7482 Set_Is_Component_Right_Opnd (N);
7487 Resolve (Arg, Btyp);
7490 Check_Unset_Reference (Arg);
7491 end Resolve_Op_Concat_Arg;
7493 -----------------------------
7494 -- Resolve_Op_Concat_First --
7495 -----------------------------
7497 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7498 Btyp : constant Entity_Id := Base_Type (Typ);
7499 Op1 : constant Node_Id := Left_Opnd (N);
7500 Op2 : constant Node_Id := Right_Opnd (N);
7503 -- The parser folds an enormous sequence of concatenations of string
7504 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7505 -- in the right operand. If the expression resolves to a predefined "&"
7506 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7507 -- we give an error. See P_Simple_Expression in Par.Ch4.
7509 if Nkind (Op2) = N_String_Literal
7510 and then Is_Folded_In_Parser (Op2)
7511 and then Ekind (Entity (N)) = E_Function
7513 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7514 and then String_Length (Strval (Op1)) = 0);
7515 Error_Msg_N ("too many user-defined concatenations", N);
7519 Set_Etype (N, Btyp);
7521 if Is_Limited_Composite (Btyp) then
7522 Error_Msg_N ("concatenation not available for limited array", N);
7523 Explain_Limited_Type (Btyp, N);
7525 end Resolve_Op_Concat_First;
7527 ----------------------------
7528 -- Resolve_Op_Concat_Rest --
7529 ----------------------------
7531 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7532 Op1 : constant Node_Id := Left_Opnd (N);
7533 Op2 : constant Node_Id := Right_Opnd (N);
7536 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7538 Generate_Operator_Reference (N, Typ);
7540 if Is_String_Type (Typ) then
7541 Eval_Concatenation (N);
7544 -- If this is not a static concatenation, but the result is a string
7545 -- type (and not an array of strings) ensure that static string operands
7546 -- have their subtypes properly constructed.
7548 if Nkind (N) /= N_String_Literal
7549 and then Is_Character_Type (Component_Type (Typ))
7551 Set_String_Literal_Subtype (Op1, Typ);
7552 Set_String_Literal_Subtype (Op2, Typ);
7554 end Resolve_Op_Concat_Rest;
7556 ----------------------
7557 -- Resolve_Op_Expon --
7558 ----------------------
7560 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7561 B_Typ : constant Entity_Id := Base_Type (Typ);
7564 -- Catch attempts to do fixed-point exponentiation with universal
7565 -- operands, which is a case where the illegality is not caught during
7566 -- normal operator analysis.
7568 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7569 Error_Msg_N ("exponentiation not available for fixed point", N);
7573 if Comes_From_Source (N)
7574 and then Ekind (Entity (N)) = E_Function
7575 and then Is_Imported (Entity (N))
7576 and then Is_Intrinsic_Subprogram (Entity (N))
7578 Resolve_Intrinsic_Operator (N, Typ);
7582 if Etype (Left_Opnd (N)) = Universal_Integer
7583 or else Etype (Left_Opnd (N)) = Universal_Real
7585 Check_For_Visible_Operator (N, B_Typ);
7588 -- We do the resolution using the base type, because intermediate values
7589 -- in expressions always are of the base type, not a subtype of it.
7591 Resolve (Left_Opnd (N), B_Typ);
7592 Resolve (Right_Opnd (N), Standard_Integer);
7594 Check_Unset_Reference (Left_Opnd (N));
7595 Check_Unset_Reference (Right_Opnd (N));
7597 Set_Etype (N, B_Typ);
7598 Generate_Operator_Reference (N, B_Typ);
7601 -- Set overflow checking bit. Much cleverer code needed here eventually
7602 -- and perhaps the Resolve routines should be separated for the various
7603 -- arithmetic operations, since they will need different processing. ???
7605 if Nkind (N) in N_Op then
7606 if not Overflow_Checks_Suppressed (Etype (N)) then
7607 Enable_Overflow_Check (N);
7610 end Resolve_Op_Expon;
7612 --------------------
7613 -- Resolve_Op_Not --
7614 --------------------
7616 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7619 function Parent_Is_Boolean return Boolean;
7620 -- This function determines if the parent node is a boolean operator
7621 -- or operation (comparison op, membership test, or short circuit form)
7622 -- and the not in question is the left operand of this operation.
7623 -- Note that if the not is in parens, then false is returned.
7625 -----------------------
7626 -- Parent_Is_Boolean --
7627 -----------------------
7629 function Parent_Is_Boolean return Boolean is
7631 if Paren_Count (N) /= 0 then
7635 case Nkind (Parent (N)) is
7650 return Left_Opnd (Parent (N)) = N;
7656 end Parent_Is_Boolean;
7658 -- Start of processing for Resolve_Op_Not
7661 -- Predefined operations on scalar types yield the base type. On the
7662 -- other hand, logical operations on arrays yield the type of the
7663 -- arguments (and the context).
7665 if Is_Array_Type (Typ) then
7668 B_Typ := Base_Type (Typ);
7671 if Is_VMS_Operator (Entity (N)) then
7674 -- Straightforward case of incorrect arguments
7676 elsif not Valid_Boolean_Arg (Typ) then
7677 Error_Msg_N ("invalid operand type for operator&", N);
7678 Set_Etype (N, Any_Type);
7681 -- Special case of probable missing parens
7683 elsif Typ = Universal_Integer or else Typ = Any_Modular then
7684 if Parent_Is_Boolean then
7686 ("operand of not must be enclosed in parentheses",
7690 ("no modular type available in this context", N);
7693 Set_Etype (N, Any_Type);
7696 -- OK resolution of not
7699 -- Warn if non-boolean types involved. This is a case like not a < b
7700 -- where a and b are modular, where we will get (not a) < b and most
7701 -- likely not (a < b) was intended.
7703 if Warn_On_Questionable_Missing_Parens
7704 and then not Is_Boolean_Type (Typ)
7705 and then Parent_Is_Boolean
7707 Error_Msg_N ("?not expression should be parenthesized here!", N);
7710 -- Warn on double negation if checking redundant constructs
7712 if Warn_On_Redundant_Constructs
7713 and then Comes_From_Source (N)
7714 and then Comes_From_Source (Right_Opnd (N))
7715 and then Root_Type (Typ) = Standard_Boolean
7716 and then Nkind (Right_Opnd (N)) = N_Op_Not
7718 Error_Msg_N ("redundant double negation?", N);
7721 -- Complete resolution and evaluation of NOT
7723 Resolve (Right_Opnd (N), B_Typ);
7724 Check_Unset_Reference (Right_Opnd (N));
7725 Set_Etype (N, B_Typ);
7726 Generate_Operator_Reference (N, B_Typ);
7731 -----------------------------
7732 -- Resolve_Operator_Symbol --
7733 -----------------------------
7735 -- Nothing to be done, all resolved already
7737 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
7738 pragma Warnings (Off, N);
7739 pragma Warnings (Off, Typ);
7743 end Resolve_Operator_Symbol;
7745 ----------------------------------
7746 -- Resolve_Qualified_Expression --
7747 ----------------------------------
7749 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
7750 pragma Warnings (Off, Typ);
7752 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7753 Expr : constant Node_Id := Expression (N);
7756 Resolve (Expr, Target_Typ);
7758 -- A qualified expression requires an exact match of the type,
7759 -- class-wide matching is not allowed. However, if the qualifying
7760 -- type is specific and the expression has a class-wide type, it
7761 -- may still be okay, since it can be the result of the expansion
7762 -- of a call to a dispatching function, so we also have to check
7763 -- class-wideness of the type of the expression's original node.
7765 if (Is_Class_Wide_Type (Target_Typ)
7767 (Is_Class_Wide_Type (Etype (Expr))
7768 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
7769 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
7771 Wrong_Type (Expr, Target_Typ);
7774 -- If the target type is unconstrained, then we reset the type of the
7775 -- result from the type of the expression. For other cases, the actual
7776 -- subtype of the expression is the target type.
7778 if Is_Composite_Type (Target_Typ)
7779 and then not Is_Constrained (Target_Typ)
7781 Set_Etype (N, Etype (Expr));
7784 Eval_Qualified_Expression (N);
7785 end Resolve_Qualified_Expression;
7787 -----------------------------------
7788 -- Resolve_Quantified_Expression --
7789 -----------------------------------
7791 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
7793 -- The loop structure is already resolved during its analysis, only the
7794 -- resolution of the condition needs to be done.
7796 Resolve (Condition (N), Typ);
7797 end Resolve_Quantified_Expression;
7803 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
7804 L : constant Node_Id := Low_Bound (N);
7805 H : constant Node_Id := High_Bound (N);
7807 function First_Last_Ref return Boolean;
7808 -- Returns True if N is of the form X'First .. X'Last where X is the
7809 -- same entity for both attributes.
7811 --------------------
7812 -- First_Last_Ref --
7813 --------------------
7815 function First_Last_Ref return Boolean is
7816 Lorig : constant Node_Id := Original_Node (L);
7817 Horig : constant Node_Id := Original_Node (H);
7820 if Nkind (Lorig) = N_Attribute_Reference
7821 and then Nkind (Horig) = N_Attribute_Reference
7822 and then Attribute_Name (Lorig) = Name_First
7823 and then Attribute_Name (Horig) = Name_Last
7826 PL : constant Node_Id := Prefix (Lorig);
7827 PH : constant Node_Id := Prefix (Horig);
7829 if Is_Entity_Name (PL)
7830 and then Is_Entity_Name (PH)
7831 and then Entity (PL) = Entity (PH)
7841 -- Start of processing for Resolve_Range
7848 -- Check for inappropriate range on unordered enumeration type
7850 if Bad_Unordered_Enumeration_Reference (N, Typ)
7852 -- Exclude X'First .. X'Last if X is the same entity for both
7854 and then not First_Last_Ref
7856 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
7859 Check_Unset_Reference (L);
7860 Check_Unset_Reference (H);
7862 -- We have to check the bounds for being within the base range as
7863 -- required for a non-static context. Normally this is automatic and
7864 -- done as part of evaluating expressions, but the N_Range node is an
7865 -- exception, since in GNAT we consider this node to be a subexpression,
7866 -- even though in Ada it is not. The circuit in Sem_Eval could check for
7867 -- this, but that would put the test on the main evaluation path for
7870 Check_Non_Static_Context (L);
7871 Check_Non_Static_Context (H);
7873 -- Check for an ambiguous range over character literals. This will
7874 -- happen with a membership test involving only literals.
7876 if Typ = Any_Character then
7877 Ambiguous_Character (L);
7878 Set_Etype (N, Any_Type);
7882 -- If bounds are static, constant-fold them, so size computations
7883 -- are identical between front-end and back-end. Do not perform this
7884 -- transformation while analyzing generic units, as type information
7885 -- would then be lost when reanalyzing the constant node in the
7888 if Is_Discrete_Type (Typ) and then Expander_Active then
7889 if Is_OK_Static_Expression (L) then
7890 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
7893 if Is_OK_Static_Expression (H) then
7894 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
7899 --------------------------
7900 -- Resolve_Real_Literal --
7901 --------------------------
7903 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
7904 Actual_Typ : constant Entity_Id := Etype (N);
7907 -- Special processing for fixed-point literals to make sure that the
7908 -- value is an exact multiple of small where this is required. We
7909 -- skip this for the universal real case, and also for generic types.
7911 if Is_Fixed_Point_Type (Typ)
7912 and then Typ /= Universal_Fixed
7913 and then Typ /= Any_Fixed
7914 and then not Is_Generic_Type (Typ)
7917 Val : constant Ureal := Realval (N);
7918 Cintr : constant Ureal := Val / Small_Value (Typ);
7919 Cint : constant Uint := UR_Trunc (Cintr);
7920 Den : constant Uint := Norm_Den (Cintr);
7924 -- Case of literal is not an exact multiple of the Small
7928 -- For a source program literal for a decimal fixed-point
7929 -- type, this is statically illegal (RM 4.9(36)).
7931 if Is_Decimal_Fixed_Point_Type (Typ)
7932 and then Actual_Typ = Universal_Real
7933 and then Comes_From_Source (N)
7935 Error_Msg_N ("value has extraneous low order digits", N);
7938 -- Generate a warning if literal from source
7940 if Is_Static_Expression (N)
7941 and then Warn_On_Bad_Fixed_Value
7944 ("?static fixed-point value is not a multiple of Small!",
7948 -- Replace literal by a value that is the exact representation
7949 -- of a value of the type, i.e. a multiple of the small value,
7950 -- by truncation, since Machine_Rounds is false for all GNAT
7951 -- fixed-point types (RM 4.9(38)).
7953 Stat := Is_Static_Expression (N);
7955 Make_Real_Literal (Sloc (N),
7956 Realval => Small_Value (Typ) * Cint));
7958 Set_Is_Static_Expression (N, Stat);
7961 -- In all cases, set the corresponding integer field
7963 Set_Corresponding_Integer_Value (N, Cint);
7967 -- Now replace the actual type by the expected type as usual
7970 Eval_Real_Literal (N);
7971 end Resolve_Real_Literal;
7973 -----------------------
7974 -- Resolve_Reference --
7975 -----------------------
7977 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
7978 P : constant Node_Id := Prefix (N);
7981 -- Replace general access with specific type
7983 if Ekind (Etype (N)) = E_Allocator_Type then
7984 Set_Etype (N, Base_Type (Typ));
7987 Resolve (P, Designated_Type (Etype (N)));
7989 -- If we are taking the reference of a volatile entity, then treat
7990 -- it as a potential modification of this entity. This is much too
7991 -- conservative, but is necessary because remove side effects can
7992 -- result in transformations of normal assignments into reference
7993 -- sequences that otherwise fail to notice the modification.
7995 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
7996 Note_Possible_Modification (P, Sure => False);
7998 end Resolve_Reference;
8000 --------------------------------
8001 -- Resolve_Selected_Component --
8002 --------------------------------
8004 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8006 Comp1 : Entity_Id := Empty; -- prevent junk warning
8007 P : constant Node_Id := Prefix (N);
8008 S : constant Node_Id := Selector_Name (N);
8009 T : Entity_Id := Etype (P);
8011 I1 : Interp_Index := 0; -- prevent junk warning
8016 function Init_Component return Boolean;
8017 -- Check whether this is the initialization of a component within an
8018 -- init proc (by assignment or call to another init proc). If true,
8019 -- there is no need for a discriminant check.
8021 --------------------
8022 -- Init_Component --
8023 --------------------
8025 function Init_Component return Boolean is
8027 return Inside_Init_Proc
8028 and then Nkind (Prefix (N)) = N_Identifier
8029 and then Chars (Prefix (N)) = Name_uInit
8030 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8033 -- Start of processing for Resolve_Selected_Component
8036 if Is_Overloaded (P) then
8038 -- Use the context type to select the prefix that has a selector
8039 -- of the correct name and type.
8042 Get_First_Interp (P, I, It);
8044 Search : while Present (It.Typ) loop
8045 if Is_Access_Type (It.Typ) then
8046 T := Designated_Type (It.Typ);
8051 if Is_Record_Type (T) then
8053 -- The visible components of a class-wide type are those of
8056 if Is_Class_Wide_Type (T) then
8060 Comp := First_Entity (T);
8061 while Present (Comp) loop
8062 if Chars (Comp) = Chars (S)
8063 and then Covers (Etype (Comp), Typ)
8072 It := Disambiguate (P, I1, I, Any_Type);
8074 if It = No_Interp then
8076 ("ambiguous prefix for selected component", N);
8083 -- There may be an implicit dereference. Retrieve
8084 -- designated record type.
8086 if Is_Access_Type (It1.Typ) then
8087 T := Designated_Type (It1.Typ);
8092 if Scope (Comp1) /= T then
8094 -- Resolution chooses the new interpretation.
8095 -- Find the component with the right name.
8097 Comp1 := First_Entity (T);
8098 while Present (Comp1)
8099 and then Chars (Comp1) /= Chars (S)
8101 Comp1 := Next_Entity (Comp1);
8110 Comp := Next_Entity (Comp);
8114 Get_Next_Interp (I, It);
8117 Resolve (P, It1.Typ);
8119 Set_Entity_With_Style_Check (S, Comp1);
8122 -- Resolve prefix with its type
8127 -- Generate cross-reference. We needed to wait until full overloading
8128 -- resolution was complete to do this, since otherwise we can't tell if
8129 -- we are an lvalue or not.
8131 if May_Be_Lvalue (N) then
8132 Generate_Reference (Entity (S), S, 'm');
8134 Generate_Reference (Entity (S), S, 'r');
8137 -- If prefix is an access type, the node will be transformed into an
8138 -- explicit dereference during expansion. The type of the node is the
8139 -- designated type of that of the prefix.
8141 if Is_Access_Type (Etype (P)) then
8142 T := Designated_Type (Etype (P));
8143 Check_Fully_Declared_Prefix (T, P);
8148 if Has_Discriminants (T)
8149 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8150 and then Present (Original_Record_Component (Entity (S)))
8151 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8152 and then Present (Discriminant_Checking_Func
8153 (Original_Record_Component (Entity (S))))
8154 and then not Discriminant_Checks_Suppressed (T)
8155 and then not Init_Component
8157 Set_Do_Discriminant_Check (N);
8160 if Ekind (Entity (S)) = E_Void then
8161 Error_Msg_N ("premature use of component", S);
8164 -- If the prefix is a record conversion, this may be a renamed
8165 -- discriminant whose bounds differ from those of the original
8166 -- one, so we must ensure that a range check is performed.
8168 if Nkind (P) = N_Type_Conversion
8169 and then Ekind (Entity (S)) = E_Discriminant
8170 and then Is_Discrete_Type (Typ)
8172 Set_Etype (N, Base_Type (Typ));
8175 -- Note: No Eval processing is required, because the prefix is of a
8176 -- record type, or protected type, and neither can possibly be static.
8178 -- If the array type is atomic, and is packed, and we are in a left side
8179 -- context, then this is worth a warning, since we have a situation
8180 -- where the access to the component may cause extra read/writes of
8181 -- the atomic array object, which could be considered unexpected.
8183 if Nkind (N) = N_Selected_Component
8184 and then (Is_Atomic (T)
8185 or else (Is_Entity_Name (Prefix (N))
8186 and then Is_Atomic (Entity (Prefix (N)))))
8187 and then Is_Packed (T)
8190 Error_Msg_N ("?assignment to component of packed atomic record",
8192 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8195 end Resolve_Selected_Component;
8201 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8202 B_Typ : constant Entity_Id := Base_Type (Typ);
8203 L : constant Node_Id := Left_Opnd (N);
8204 R : constant Node_Id := Right_Opnd (N);
8207 -- We do the resolution using the base type, because intermediate values
8208 -- in expressions always are of the base type, not a subtype of it.
8211 Resolve (R, Standard_Natural);
8213 Check_Unset_Reference (L);
8214 Check_Unset_Reference (R);
8216 Set_Etype (N, B_Typ);
8217 Generate_Operator_Reference (N, B_Typ);
8221 ---------------------------
8222 -- Resolve_Short_Circuit --
8223 ---------------------------
8225 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8226 B_Typ : constant Entity_Id := Base_Type (Typ);
8227 L : constant Node_Id := Left_Opnd (N);
8228 R : constant Node_Id := Right_Opnd (N);
8231 -- Why are the calls to Check_Order_Dependence commented out ???
8233 -- Check_Order_Dependence; -- For AI05-0144
8235 -- Check_Order_Dependence; -- For AI05-0144
8237 -- Check for issuing warning for always False assert/check, this happens
8238 -- when assertions are turned off, in which case the pragma Assert/Check
8239 -- was transformed into:
8241 -- if False and then <condition> then ...
8243 -- and we detect this pattern
8245 if Warn_On_Assertion_Failure
8246 and then Is_Entity_Name (R)
8247 and then Entity (R) = Standard_False
8248 and then Nkind (Parent (N)) = N_If_Statement
8249 and then Nkind (N) = N_And_Then
8250 and then Is_Entity_Name (L)
8251 and then Entity (L) = Standard_False
8254 Orig : constant Node_Id := Original_Node (Parent (N));
8257 if Nkind (Orig) = N_Pragma
8258 and then Pragma_Name (Orig) = Name_Assert
8260 -- Don't want to warn if original condition is explicit False
8263 Expr : constant Node_Id :=
8266 (First (Pragma_Argument_Associations (Orig))));
8268 if Is_Entity_Name (Expr)
8269 and then Entity (Expr) = Standard_False
8273 -- Issue warning. We do not want the deletion of the
8274 -- IF/AND-THEN to take this message with it. We achieve
8275 -- this by making sure that the expanded code points to
8276 -- the Sloc of the expression, not the original pragma.
8279 ("?assertion would fail at run time!",
8281 (First (Pragma_Argument_Associations (Orig))));
8285 -- Similar processing for Check pragma
8287 elsif Nkind (Orig) = N_Pragma
8288 and then Pragma_Name (Orig) = Name_Check
8290 -- Don't want to warn if original condition is explicit False
8293 Expr : constant Node_Id :=
8297 (Pragma_Argument_Associations (Orig)))));
8299 if Is_Entity_Name (Expr)
8300 and then Entity (Expr) = Standard_False
8305 ("?check would fail at run time!",
8307 (Last (Pragma_Argument_Associations (Orig))));
8314 -- Continue with processing of short circuit
8316 Check_Unset_Reference (L);
8317 Check_Unset_Reference (R);
8319 Set_Etype (N, B_Typ);
8320 Eval_Short_Circuit (N);
8321 end Resolve_Short_Circuit;
8327 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8328 Name : constant Node_Id := Prefix (N);
8329 Drange : constant Node_Id := Discrete_Range (N);
8330 Array_Type : Entity_Id := Empty;
8334 if Is_Overloaded (Name) then
8336 -- Use the context type to select the prefix that yields the correct
8341 I1 : Interp_Index := 0;
8343 P : constant Node_Id := Prefix (N);
8344 Found : Boolean := False;
8347 Get_First_Interp (P, I, It);
8348 while Present (It.Typ) loop
8349 if (Is_Array_Type (It.Typ)
8350 and then Covers (Typ, It.Typ))
8351 or else (Is_Access_Type (It.Typ)
8352 and then Is_Array_Type (Designated_Type (It.Typ))
8353 and then Covers (Typ, Designated_Type (It.Typ)))
8356 It := Disambiguate (P, I1, I, Any_Type);
8358 if It = No_Interp then
8359 Error_Msg_N ("ambiguous prefix for slicing", N);
8364 Array_Type := It.Typ;
8369 Array_Type := It.Typ;
8374 Get_Next_Interp (I, It);
8379 Array_Type := Etype (Name);
8382 Resolve (Name, Array_Type);
8384 if Is_Access_Type (Array_Type) then
8385 Apply_Access_Check (N);
8386 Array_Type := Designated_Type (Array_Type);
8388 -- If the prefix is an access to an unconstrained array, we must use
8389 -- the actual subtype of the object to perform the index checks. The
8390 -- object denoted by the prefix is implicit in the node, so we build
8391 -- an explicit representation for it in order to compute the actual
8394 if not Is_Constrained (Array_Type) then
8395 Remove_Side_Effects (Prefix (N));
8398 Obj : constant Node_Id :=
8399 Make_Explicit_Dereference (Sloc (N),
8400 Prefix => New_Copy_Tree (Prefix (N)));
8402 Set_Etype (Obj, Array_Type);
8403 Set_Parent (Obj, Parent (N));
8404 Array_Type := Get_Actual_Subtype (Obj);
8408 elsif Is_Entity_Name (Name)
8409 or else Nkind (Name) = N_Explicit_Dereference
8410 or else (Nkind (Name) = N_Function_Call
8411 and then not Is_Constrained (Etype (Name)))
8413 Array_Type := Get_Actual_Subtype (Name);
8415 -- If the name is a selected component that depends on discriminants,
8416 -- build an actual subtype for it. This can happen only when the name
8417 -- itself is overloaded; otherwise the actual subtype is created when
8418 -- the selected component is analyzed.
8420 elsif Nkind (Name) = N_Selected_Component
8421 and then Full_Analysis
8422 and then Depends_On_Discriminant (First_Index (Array_Type))
8425 Act_Decl : constant Node_Id :=
8426 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8428 Insert_Action (N, Act_Decl);
8429 Array_Type := Defining_Identifier (Act_Decl);
8432 -- Maybe this should just be "else", instead of checking for the
8433 -- specific case of slice??? This is needed for the case where
8434 -- the prefix is an Image attribute, which gets expanded to a
8435 -- slice, and so has a constrained subtype which we want to use
8436 -- for the slice range check applied below (the range check won't
8437 -- get done if the unconstrained subtype of the 'Image is used).
8439 elsif Nkind (Name) = N_Slice then
8440 Array_Type := Etype (Name);
8443 -- If name was overloaded, set slice type correctly now
8445 Set_Etype (N, Array_Type);
8447 -- If the range is specified by a subtype mark, no resolution is
8448 -- necessary. Else resolve the bounds, and apply needed checks.
8450 if not Is_Entity_Name (Drange) then
8451 Index := First_Index (Array_Type);
8452 Resolve (Drange, Base_Type (Etype (Index)));
8454 if Nkind (Drange) = N_Range then
8456 -- Ensure that side effects in the bounds are properly handled
8458 Remove_Side_Effects (Low_Bound (Drange), Variable_Ref => True);
8459 Remove_Side_Effects (High_Bound (Drange), Variable_Ref => True);
8461 -- Do not apply the range check to nodes associated with the
8462 -- frontend expansion of the dispatch table. We first check
8463 -- if Ada.Tags is already loaded to avoid the addition of an
8464 -- undesired dependence on such run-time unit.
8466 if not Tagged_Type_Expansion
8468 (RTU_Loaded (Ada_Tags)
8469 and then Nkind (Prefix (N)) = N_Selected_Component
8470 and then Present (Entity (Selector_Name (Prefix (N))))
8471 and then Entity (Selector_Name (Prefix (N))) =
8472 RTE_Record_Component (RE_Prims_Ptr))
8474 Apply_Range_Check (Drange, Etype (Index));
8479 Set_Slice_Subtype (N);
8481 -- Check bad use of type with predicates
8483 if Has_Predicates (Etype (Drange)) then
8485 ("subtype& has predicate, not allowed in slice",
8486 Drange, Etype (Drange));
8488 -- Otherwise here is where we check suspicious indexes
8490 elsif Nkind (Drange) = N_Range then
8491 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8492 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8498 ----------------------------
8499 -- Resolve_String_Literal --
8500 ----------------------------
8502 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8503 C_Typ : constant Entity_Id := Component_Type (Typ);
8504 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8505 Loc : constant Source_Ptr := Sloc (N);
8506 Str : constant String_Id := Strval (N);
8507 Strlen : constant Nat := String_Length (Str);
8508 Subtype_Id : Entity_Id;
8509 Need_Check : Boolean;
8512 -- For a string appearing in a concatenation, defer creation of the
8513 -- string_literal_subtype until the end of the resolution of the
8514 -- concatenation, because the literal may be constant-folded away. This
8515 -- is a useful optimization for long concatenation expressions.
8517 -- If the string is an aggregate built for a single character (which
8518 -- happens in a non-static context) or a is null string to which special
8519 -- checks may apply, we build the subtype. Wide strings must also get a
8520 -- string subtype if they come from a one character aggregate. Strings
8521 -- generated by attributes might be static, but it is often hard to
8522 -- determine whether the enclosing context is static, so we generate
8523 -- subtypes for them as well, thus losing some rarer optimizations ???
8524 -- Same for strings that come from a static conversion.
8527 (Strlen = 0 and then Typ /= Standard_String)
8528 or else Nkind (Parent (N)) /= N_Op_Concat
8529 or else (N /= Left_Opnd (Parent (N))
8530 and then N /= Right_Opnd (Parent (N)))
8531 or else ((Typ = Standard_Wide_String
8532 or else Typ = Standard_Wide_Wide_String)
8533 and then Nkind (Original_Node (N)) /= N_String_Literal);
8535 -- If the resolving type is itself a string literal subtype, we can just
8536 -- reuse it, since there is no point in creating another.
8538 if Ekind (Typ) = E_String_Literal_Subtype then
8541 elsif Nkind (Parent (N)) = N_Op_Concat
8542 and then not Need_Check
8543 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8544 N_Attribute_Reference,
8545 N_Qualified_Expression,
8550 -- Otherwise we must create a string literal subtype. Note that the
8551 -- whole idea of string literal subtypes is simply to avoid the need
8552 -- for building a full fledged array subtype for each literal.
8555 Set_String_Literal_Subtype (N, Typ);
8556 Subtype_Id := Etype (N);
8559 if Nkind (Parent (N)) /= N_Op_Concat
8562 Set_Etype (N, Subtype_Id);
8563 Eval_String_Literal (N);
8566 if Is_Limited_Composite (Typ)
8567 or else Is_Private_Composite (Typ)
8569 Error_Msg_N ("string literal not available for private array", N);
8570 Set_Etype (N, Any_Type);
8574 -- The validity of a null string has been checked in the call to
8575 -- Eval_String_Literal.
8580 -- Always accept string literal with component type Any_Character, which
8581 -- occurs in error situations and in comparisons of literals, both of
8582 -- which should accept all literals.
8584 elsif R_Typ = Any_Character then
8587 -- If the type is bit-packed, then we always transform the string
8588 -- literal into a full fledged aggregate.
8590 elsif Is_Bit_Packed_Array (Typ) then
8593 -- Deal with cases of Wide_Wide_String, Wide_String, and String
8596 -- For Standard.Wide_Wide_String, or any other type whose component
8597 -- type is Standard.Wide_Wide_Character, we know that all the
8598 -- characters in the string must be acceptable, since the parser
8599 -- accepted the characters as valid character literals.
8601 if R_Typ = Standard_Wide_Wide_Character then
8604 -- For the case of Standard.String, or any other type whose component
8605 -- type is Standard.Character, we must make sure that there are no
8606 -- wide characters in the string, i.e. that it is entirely composed
8607 -- of characters in range of type Character.
8609 -- If the string literal is the result of a static concatenation, the
8610 -- test has already been performed on the components, and need not be
8613 elsif R_Typ = Standard_Character
8614 and then Nkind (Original_Node (N)) /= N_Op_Concat
8616 for J in 1 .. Strlen loop
8617 if not In_Character_Range (Get_String_Char (Str, J)) then
8619 -- If we are out of range, post error. This is one of the
8620 -- very few places that we place the flag in the middle of
8621 -- a token, right under the offending wide character. Not
8622 -- quite clear if this is right wrt wide character encoding
8623 -- sequences, but it's only an error message!
8626 ("literal out of range of type Standard.Character",
8627 Source_Ptr (Int (Loc) + J));
8632 -- For the case of Standard.Wide_String, or any other type whose
8633 -- component type is Standard.Wide_Character, we must make sure that
8634 -- there are no wide characters in the string, i.e. that it is
8635 -- entirely composed of characters in range of type Wide_Character.
8637 -- If the string literal is the result of a static concatenation,
8638 -- the test has already been performed on the components, and need
8641 elsif R_Typ = Standard_Wide_Character
8642 and then Nkind (Original_Node (N)) /= N_Op_Concat
8644 for J in 1 .. Strlen loop
8645 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8647 -- If we are out of range, post error. This is one of the
8648 -- very few places that we place the flag in the middle of
8649 -- a token, right under the offending wide character.
8651 -- This is not quite right, because characters in general
8652 -- will take more than one character position ???
8655 ("literal out of range of type Standard.Wide_Character",
8656 Source_Ptr (Int (Loc) + J));
8661 -- If the root type is not a standard character, then we will convert
8662 -- the string into an aggregate and will let the aggregate code do
8663 -- the checking. Standard Wide_Wide_Character is also OK here.
8669 -- See if the component type of the array corresponding to the string
8670 -- has compile time known bounds. If yes we can directly check
8671 -- whether the evaluation of the string will raise constraint error.
8672 -- Otherwise we need to transform the string literal into the
8673 -- corresponding character aggregate and let the aggregate
8674 -- code do the checking.
8676 if Is_Standard_Character_Type (R_Typ) then
8678 -- Check for the case of full range, where we are definitely OK
8680 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8684 -- Here the range is not the complete base type range, so check
8687 Comp_Typ_Lo : constant Node_Id :=
8688 Type_Low_Bound (Component_Type (Typ));
8689 Comp_Typ_Hi : constant Node_Id :=
8690 Type_High_Bound (Component_Type (Typ));
8695 if Compile_Time_Known_Value (Comp_Typ_Lo)
8696 and then Compile_Time_Known_Value (Comp_Typ_Hi)
8698 for J in 1 .. Strlen loop
8699 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
8701 if Char_Val < Expr_Value (Comp_Typ_Lo)
8702 or else Char_Val > Expr_Value (Comp_Typ_Hi)
8704 Apply_Compile_Time_Constraint_Error
8705 (N, "character out of range?", CE_Range_Check_Failed,
8706 Loc => Source_Ptr (Int (Loc) + J));
8716 -- If we got here we meed to transform the string literal into the
8717 -- equivalent qualified positional array aggregate. This is rather
8718 -- heavy artillery for this situation, but it is hard work to avoid.
8721 Lits : constant List_Id := New_List;
8722 P : Source_Ptr := Loc + 1;
8726 -- Build the character literals, we give them source locations that
8727 -- correspond to the string positions, which is a bit tricky given
8728 -- the possible presence of wide character escape sequences.
8730 for J in 1 .. Strlen loop
8731 C := Get_String_Char (Str, J);
8732 Set_Character_Literal_Name (C);
8735 Make_Character_Literal (P,
8737 Char_Literal_Value => UI_From_CC (C)));
8739 if In_Character_Range (C) then
8742 -- Should we have a call to Skip_Wide here ???
8750 Make_Qualified_Expression (Loc,
8751 Subtype_Mark => New_Reference_To (Typ, Loc),
8753 Make_Aggregate (Loc, Expressions => Lits)));
8755 Analyze_And_Resolve (N, Typ);
8757 end Resolve_String_Literal;
8759 -----------------------------
8760 -- Resolve_Subprogram_Info --
8761 -----------------------------
8763 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
8766 end Resolve_Subprogram_Info;
8768 -----------------------------
8769 -- Resolve_Type_Conversion --
8770 -----------------------------
8772 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
8773 Conv_OK : constant Boolean := Conversion_OK (N);
8774 Operand : constant Node_Id := Expression (N);
8775 Operand_Typ : constant Entity_Id := Etype (Operand);
8776 Target_Typ : constant Entity_Id := Etype (N);
8781 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
8782 -- Set to False to suppress cases where we want to suppress the test
8783 -- for redundancy to avoid possible false positives on this warning.
8787 and then not Valid_Conversion (N, Target_Typ, Operand)
8792 -- If the Operand Etype is Universal_Fixed, then the conversion is
8793 -- never redundant. We need this check because by the time we have
8794 -- finished the rather complex transformation, the conversion looks
8795 -- redundant when it is not.
8797 if Operand_Typ = Universal_Fixed then
8798 Test_Redundant := False;
8800 -- If the operand is marked as Any_Fixed, then special processing is
8801 -- required. This is also a case where we suppress the test for a
8802 -- redundant conversion, since most certainly it is not redundant.
8804 elsif Operand_Typ = Any_Fixed then
8805 Test_Redundant := False;
8807 -- Mixed-mode operation involving a literal. Context must be a fixed
8808 -- type which is applied to the literal subsequently.
8810 if Is_Fixed_Point_Type (Typ) then
8811 Set_Etype (Operand, Universal_Real);
8813 elsif Is_Numeric_Type (Typ)
8814 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
8815 and then (Etype (Right_Opnd (Operand)) = Universal_Real
8817 Etype (Left_Opnd (Operand)) = Universal_Real)
8819 -- Return if expression is ambiguous
8821 if Unique_Fixed_Point_Type (N) = Any_Type then
8824 -- If nothing else, the available fixed type is Duration
8827 Set_Etype (Operand, Standard_Duration);
8830 -- Resolve the real operand with largest available precision
8832 if Etype (Right_Opnd (Operand)) = Universal_Real then
8833 Rop := New_Copy_Tree (Right_Opnd (Operand));
8835 Rop := New_Copy_Tree (Left_Opnd (Operand));
8838 Resolve (Rop, Universal_Real);
8840 -- If the operand is a literal (it could be a non-static and
8841 -- illegal exponentiation) check whether the use of Duration
8842 -- is potentially inaccurate.
8844 if Nkind (Rop) = N_Real_Literal
8845 and then Realval (Rop) /= Ureal_0
8846 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
8849 ("?universal real operand can only " &
8850 "be interpreted as Duration!",
8853 ("\?precision will be lost in the conversion!", Rop);
8856 elsif Is_Numeric_Type (Typ)
8857 and then Nkind (Operand) in N_Op
8858 and then Unique_Fixed_Point_Type (N) /= Any_Type
8860 Set_Etype (Operand, Standard_Duration);
8863 Error_Msg_N ("invalid context for mixed mode operation", N);
8864 Set_Etype (Operand, Any_Type);
8871 -- Note: we do the Eval_Type_Conversion call before applying the
8872 -- required checks for a subtype conversion. This is important, since
8873 -- both are prepared under certain circumstances to change the type
8874 -- conversion to a constraint error node, but in the case of
8875 -- Eval_Type_Conversion this may reflect an illegality in the static
8876 -- case, and we would miss the illegality (getting only a warning
8877 -- message), if we applied the type conversion checks first.
8879 Eval_Type_Conversion (N);
8881 -- Even when evaluation is not possible, we may be able to simplify the
8882 -- conversion or its expression. This needs to be done before applying
8883 -- checks, since otherwise the checks may use the original expression
8884 -- and defeat the simplifications. This is specifically the case for
8885 -- elimination of the floating-point Truncation attribute in
8886 -- float-to-int conversions.
8888 Simplify_Type_Conversion (N);
8890 -- If after evaluation we still have a type conversion, then we may need
8891 -- to apply checks required for a subtype conversion.
8893 -- Skip these type conversion checks if universal fixed operands
8894 -- operands involved, since range checks are handled separately for
8895 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
8897 if Nkind (N) = N_Type_Conversion
8898 and then not Is_Generic_Type (Root_Type (Target_Typ))
8899 and then Target_Typ /= Universal_Fixed
8900 and then Operand_Typ /= Universal_Fixed
8902 Apply_Type_Conversion_Checks (N);
8905 -- Issue warning for conversion of simple object to its own type. We
8906 -- have to test the original nodes, since they may have been rewritten
8907 -- by various optimizations.
8909 Orig_N := Original_Node (N);
8911 -- Here we test for a redundant conversion if the warning mode is
8912 -- active (and was not locally reset), and we have a type conversion
8913 -- from source not appearing in a generic instance.
8916 and then Nkind (Orig_N) = N_Type_Conversion
8917 and then Comes_From_Source (Orig_N)
8918 and then not In_Instance
8920 Orig_N := Original_Node (Expression (Orig_N));
8921 Orig_T := Target_Typ;
8923 -- If the node is part of a larger expression, the Target_Type
8924 -- may not be the original type of the node if the context is a
8925 -- condition. Recover original type to see if conversion is needed.
8927 if Is_Boolean_Type (Orig_T)
8928 and then Nkind (Parent (N)) in N_Op
8930 Orig_T := Etype (Parent (N));
8933 -- If we have an entity name, then give the warning if the entity
8934 -- is the right type, or if it is a loop parameter covered by the
8935 -- original type (that's needed because loop parameters have an
8936 -- odd subtype coming from the bounds).
8938 if (Is_Entity_Name (Orig_N)
8940 (Etype (Entity (Orig_N)) = Orig_T
8942 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
8943 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
8945 -- If not an entity, then type of expression must match
8947 or else Etype (Orig_N) = Orig_T
8949 -- One more check, do not give warning if the analyzed conversion
8950 -- has an expression with non-static bounds, and the bounds of the
8951 -- target are static. This avoids junk warnings in cases where the
8952 -- conversion is necessary to establish staticness, for example in
8953 -- a case statement.
8955 if not Is_OK_Static_Subtype (Operand_Typ)
8956 and then Is_OK_Static_Subtype (Target_Typ)
8960 -- Finally, if this type conversion occurs in a context that
8961 -- requires a prefix, and the expression is a qualified expression
8962 -- then the type conversion is not redundant, because a qualified
8963 -- expression is not a prefix, whereas a type conversion is. For
8964 -- example, "X := T'(Funx(...)).Y;" is illegal because a selected
8965 -- component requires a prefix, but a type conversion makes it
8966 -- legal: "X := T(T'(Funx(...))).Y;"
8968 -- In Ada 2012, a qualified expression is a name, so this idiom is
8969 -- no longer needed, but we still suppress the warning because it
8970 -- seems unfriendly for warnings to pop up when you switch to the
8971 -- newer language version.
8973 elsif Nkind (Orig_N) = N_Qualified_Expression
8974 and then Nkind_In (Parent (N), N_Attribute_Reference,
8975 N_Indexed_Component,
8976 N_Selected_Component,
8978 N_Explicit_Dereference)
8982 -- Here we give the redundant conversion warning. If it is an
8983 -- entity, give the name of the entity in the message. If not,
8984 -- just mention the expression.
8987 if Is_Entity_Name (Orig_N) then
8988 Error_Msg_Node_2 := Orig_T;
8989 Error_Msg_NE -- CODEFIX
8990 ("?redundant conversion, & is of type &!",
8991 N, Entity (Orig_N));
8994 ("?redundant conversion, expression is of type&!",
9001 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9002 -- No need to perform any interface conversion if the type of the
9003 -- expression coincides with the target type.
9005 if Ada_Version >= Ada_2005
9006 and then Expander_Active
9007 and then Operand_Typ /= Target_Typ
9010 Opnd : Entity_Id := Operand_Typ;
9011 Target : Entity_Id := Target_Typ;
9014 if Is_Access_Type (Opnd) then
9015 Opnd := Designated_Type (Opnd);
9018 if Is_Access_Type (Target_Typ) then
9019 Target := Designated_Type (Target);
9022 if Opnd = Target then
9025 -- Conversion from interface type
9027 elsif Is_Interface (Opnd) then
9029 -- Ada 2005 (AI-217): Handle entities from limited views
9031 if From_With_Type (Opnd) then
9032 Error_Msg_Qual_Level := 99;
9033 Error_Msg_NE -- CODEFIX
9034 ("missing WITH clause on package &", N,
9035 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9037 ("type conversions require visibility of the full view",
9040 elsif From_With_Type (Target)
9042 (Is_Access_Type (Target_Typ)
9043 and then Present (Non_Limited_View (Etype (Target))))
9045 Error_Msg_Qual_Level := 99;
9046 Error_Msg_NE -- CODEFIX
9047 ("missing WITH clause on package &", N,
9048 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9050 ("type conversions require visibility of the full view",
9054 Expand_Interface_Conversion (N, Is_Static => False);
9057 -- Conversion to interface type
9059 elsif Is_Interface (Target) then
9063 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9064 Opnd := Etype (Opnd);
9067 if not Interface_Present_In_Ancestor
9071 if Is_Class_Wide_Type (Opnd) then
9073 -- The static analysis is not enough to know if the
9074 -- interface is implemented or not. Hence we must pass
9075 -- the work to the expander to generate code to evaluate
9076 -- the conversion at run time.
9078 Expand_Interface_Conversion (N, Is_Static => False);
9081 Error_Msg_Name_1 := Chars (Etype (Target));
9082 Error_Msg_Name_2 := Chars (Opnd);
9084 ("wrong interface conversion (% is not a progenitor " &
9089 Expand_Interface_Conversion (N);
9094 end Resolve_Type_Conversion;
9096 ----------------------
9097 -- Resolve_Unary_Op --
9098 ----------------------
9100 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9101 B_Typ : constant Entity_Id := Base_Type (Typ);
9102 R : constant Node_Id := Right_Opnd (N);
9108 -- Deal with intrinsic unary operators
9110 if Comes_From_Source (N)
9111 and then Ekind (Entity (N)) = E_Function
9112 and then Is_Imported (Entity (N))
9113 and then Is_Intrinsic_Subprogram (Entity (N))
9115 Resolve_Intrinsic_Unary_Operator (N, Typ);
9119 -- Deal with universal cases
9121 if Etype (R) = Universal_Integer
9123 Etype (R) = Universal_Real
9125 Check_For_Visible_Operator (N, B_Typ);
9128 Set_Etype (N, B_Typ);
9131 -- Generate warning for expressions like abs (x mod 2)
9133 if Warn_On_Redundant_Constructs
9134 and then Nkind (N) = N_Op_Abs
9136 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9138 if OK and then Hi >= Lo and then Lo >= 0 then
9139 Error_Msg_N -- CODEFIX
9140 ("?abs applied to known non-negative value has no effect", N);
9144 -- Deal with reference generation
9146 Check_Unset_Reference (R);
9147 Generate_Operator_Reference (N, B_Typ);
9150 -- Set overflow checking bit. Much cleverer code needed here eventually
9151 -- and perhaps the Resolve routines should be separated for the various
9152 -- arithmetic operations, since they will need different processing ???
9154 if Nkind (N) in N_Op then
9155 if not Overflow_Checks_Suppressed (Etype (N)) then
9156 Enable_Overflow_Check (N);
9160 -- Generate warning for expressions like -5 mod 3 for integers. No need
9161 -- to worry in the floating-point case, since parens do not affect the
9162 -- result so there is no point in giving in a warning.
9165 Norig : constant Node_Id := Original_Node (N);
9174 if Warn_On_Questionable_Missing_Parens
9175 and then Comes_From_Source (Norig)
9176 and then Is_Integer_Type (Typ)
9177 and then Nkind (Norig) = N_Op_Minus
9179 Rorig := Original_Node (Right_Opnd (Norig));
9181 -- We are looking for cases where the right operand is not
9182 -- parenthesized, and is a binary operator, multiply, divide, or
9183 -- mod. These are the cases where the grouping can affect results.
9185 if Paren_Count (Rorig) = 0
9186 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9188 -- For mod, we always give the warning, since the value is
9189 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9190 -- -(5 mod 315)). But for the other cases, the only concern is
9191 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9192 -- overflows, but (-2) * 64 does not). So we try to give the
9193 -- message only when overflow is possible.
9195 if Nkind (Rorig) /= N_Op_Mod
9196 and then Compile_Time_Known_Value (R)
9198 Val := Expr_Value (R);
9200 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9201 HB := Expr_Value (Type_High_Bound (Typ));
9203 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9206 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9207 LB := Expr_Value (Type_Low_Bound (Typ));
9209 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9212 -- Note that the test below is deliberately excluding the
9213 -- largest negative number, since that is a potentially
9214 -- troublesome case (e.g. -2 * x, where the result is the
9215 -- largest negative integer has an overflow with 2 * x).
9217 if Val > LB and then Val <= HB then
9222 -- For the multiplication case, the only case we have to worry
9223 -- about is when (-a)*b is exactly the largest negative number
9224 -- so that -(a*b) can cause overflow. This can only happen if
9225 -- a is a power of 2, and more generally if any operand is a
9226 -- constant that is not a power of 2, then the parentheses
9227 -- cannot affect whether overflow occurs. We only bother to
9228 -- test the left most operand
9230 -- Loop looking at left operands for one that has known value
9233 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9234 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9235 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9237 -- Operand value of 0 or 1 skips warning
9242 -- Otherwise check power of 2, if power of 2, warn, if
9243 -- anything else, skip warning.
9246 while Lval /= 2 loop
9247 if Lval mod 2 = 1 then
9258 -- Keep looking at left operands
9260 Opnd := Left_Opnd (Opnd);
9263 -- For rem or "/" we can only have a problematic situation
9264 -- if the divisor has a value of minus one or one. Otherwise
9265 -- overflow is impossible (divisor > 1) or we have a case of
9266 -- division by zero in any case.
9268 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9269 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9270 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9275 -- If we fall through warning should be issued
9278 ("?unary minus expression should be parenthesized here!", N);
9282 end Resolve_Unary_Op;
9284 ----------------------------------
9285 -- Resolve_Unchecked_Expression --
9286 ----------------------------------
9288 procedure Resolve_Unchecked_Expression
9293 Resolve (Expression (N), Typ, Suppress => All_Checks);
9295 end Resolve_Unchecked_Expression;
9297 ---------------------------------------
9298 -- Resolve_Unchecked_Type_Conversion --
9299 ---------------------------------------
9301 procedure Resolve_Unchecked_Type_Conversion
9305 pragma Warnings (Off, Typ);
9307 Operand : constant Node_Id := Expression (N);
9308 Opnd_Type : constant Entity_Id := Etype (Operand);
9311 -- Resolve operand using its own type
9313 Resolve (Operand, Opnd_Type);
9314 Eval_Unchecked_Conversion (N);
9315 end Resolve_Unchecked_Type_Conversion;
9317 ------------------------------
9318 -- Rewrite_Operator_As_Call --
9319 ------------------------------
9321 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9322 Loc : constant Source_Ptr := Sloc (N);
9323 Actuals : constant List_Id := New_List;
9327 if Nkind (N) in N_Binary_Op then
9328 Append (Left_Opnd (N), Actuals);
9331 Append (Right_Opnd (N), Actuals);
9334 Make_Function_Call (Sloc => Loc,
9335 Name => New_Occurrence_Of (Nam, Loc),
9336 Parameter_Associations => Actuals);
9338 Preserve_Comes_From_Source (New_N, N);
9339 Preserve_Comes_From_Source (Name (New_N), N);
9341 Set_Etype (N, Etype (Nam));
9342 end Rewrite_Operator_As_Call;
9344 ------------------------------
9345 -- Rewrite_Renamed_Operator --
9346 ------------------------------
9348 procedure Rewrite_Renamed_Operator
9353 Nam : constant Name_Id := Chars (Op);
9354 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9358 -- Rewrite the operator node using the real operator, not its renaming.
9359 -- Exclude user-defined intrinsic operations of the same name, which are
9360 -- treated separately and rewritten as calls.
9362 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9363 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9364 Set_Chars (Op_Node, Nam);
9365 Set_Etype (Op_Node, Etype (N));
9366 Set_Entity (Op_Node, Op);
9367 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9369 -- Indicate that both the original entity and its renaming are
9370 -- referenced at this point.
9372 Generate_Reference (Entity (N), N);
9373 Generate_Reference (Op, N);
9376 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9379 Rewrite (N, Op_Node);
9381 -- If the context type is private, add the appropriate conversions so
9382 -- that the operator is applied to the full view. This is done in the
9383 -- routines that resolve intrinsic operators.
9385 if Is_Intrinsic_Subprogram (Op)
9386 and then Is_Private_Type (Typ)
9389 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9390 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9391 Resolve_Intrinsic_Operator (N, Typ);
9393 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9394 Resolve_Intrinsic_Unary_Operator (N, Typ);
9401 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9403 -- Operator renames a user-defined operator of the same name. Use the
9404 -- original operator in the node, which is the one Gigi knows about.
9407 Set_Is_Overloaded (N, False);
9409 end Rewrite_Renamed_Operator;
9411 -----------------------
9412 -- Set_Slice_Subtype --
9413 -----------------------
9415 -- Build an implicit subtype declaration to represent the type delivered by
9416 -- the slice. This is an abbreviated version of an array subtype. We define
9417 -- an index subtype for the slice, using either the subtype name or the
9418 -- discrete range of the slice. To be consistent with index usage elsewhere
9419 -- we create a list header to hold the single index. This list is not
9420 -- otherwise attached to the syntax tree.
9422 procedure Set_Slice_Subtype (N : Node_Id) is
9423 Loc : constant Source_Ptr := Sloc (N);
9424 Index_List : constant List_Id := New_List;
9426 Index_Subtype : Entity_Id;
9427 Index_Type : Entity_Id;
9428 Slice_Subtype : Entity_Id;
9429 Drange : constant Node_Id := Discrete_Range (N);
9432 if Is_Entity_Name (Drange) then
9433 Index_Subtype := Entity (Drange);
9436 -- We force the evaluation of a range. This is definitely needed in
9437 -- the renamed case, and seems safer to do unconditionally. Note in
9438 -- any case that since we will create and insert an Itype referring
9439 -- to this range, we must make sure any side effect removal actions
9440 -- are inserted before the Itype definition.
9442 if Nkind (Drange) = N_Range then
9443 Force_Evaluation (Low_Bound (Drange));
9444 Force_Evaluation (High_Bound (Drange));
9447 Index_Type := Base_Type (Etype (Drange));
9449 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9451 -- Take a new copy of Drange (where bounds have been rewritten to
9452 -- reference side-effect-free names). Using a separate tree ensures
9453 -- that further expansion (e.g. while rewriting a slice assignment
9454 -- into a FOR loop) does not attempt to remove side effects on the
9455 -- bounds again (which would cause the bounds in the index subtype
9456 -- definition to refer to temporaries before they are defined) (the
9457 -- reason is that some names are considered side effect free here
9458 -- for the subtype, but not in the context of a loop iteration
9461 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9462 Set_Etype (Index_Subtype, Index_Type);
9463 Set_Size_Info (Index_Subtype, Index_Type);
9464 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9467 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9469 Index := New_Occurrence_Of (Index_Subtype, Loc);
9470 Set_Etype (Index, Index_Subtype);
9471 Append (Index, Index_List);
9473 Set_First_Index (Slice_Subtype, Index);
9474 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9475 Set_Is_Constrained (Slice_Subtype, True);
9477 Check_Compile_Time_Size (Slice_Subtype);
9479 -- The Etype of the existing Slice node is reset to this slice subtype.
9480 -- Its bounds are obtained from its first index.
9482 Set_Etype (N, Slice_Subtype);
9484 -- For packed slice subtypes, freeze immediately (except in the
9485 -- case of being in a "spec expression" where we never freeze
9486 -- when we first see the expression).
9488 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9489 Freeze_Itype (Slice_Subtype, N);
9491 -- For all other cases insert an itype reference in the slice's actions
9492 -- so that the itype is frozen at the proper place in the tree (i.e. at
9493 -- the point where actions for the slice are analyzed). Note that this
9494 -- is different from freezing the itype immediately, which might be
9495 -- premature (e.g. if the slice is within a transient scope).
9498 Ensure_Defined (Typ => Slice_Subtype, N => N);
9500 end Set_Slice_Subtype;
9502 --------------------------------
9503 -- Set_String_Literal_Subtype --
9504 --------------------------------
9506 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9507 Loc : constant Source_Ptr := Sloc (N);
9508 Low_Bound : constant Node_Id :=
9509 Type_Low_Bound (Etype (First_Index (Typ)));
9510 Subtype_Id : Entity_Id;
9513 if Nkind (N) /= N_String_Literal then
9517 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
9518 Set_String_Literal_Length (Subtype_Id, UI_From_Int
9519 (String_Length (Strval (N))));
9520 Set_Etype (Subtype_Id, Base_Type (Typ));
9521 Set_Is_Constrained (Subtype_Id);
9522 Set_Etype (N, Subtype_Id);
9524 if Is_OK_Static_Expression (Low_Bound) then
9526 -- The low bound is set from the low bound of the corresponding index
9527 -- type. Note that we do not store the high bound in the string literal
9528 -- subtype, but it can be deduced if necessary from the length and the
9531 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
9534 Set_String_Literal_Low_Bound
9535 (Subtype_Id, Make_Integer_Literal (Loc, 1));
9536 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
9538 -- Build bona fide subtype for the string, and wrap it in an
9539 -- unchecked conversion, because the backend expects the
9540 -- String_Literal_Subtype to have a static lower bound.
9543 Index_List : constant List_Id := New_List;
9544 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
9545 High_Bound : constant Node_Id :=
9547 Left_Opnd => New_Copy_Tree (Low_Bound),
9549 Make_Integer_Literal (Loc,
9550 String_Length (Strval (N)) - 1));
9551 Array_Subtype : Entity_Id;
9552 Index_Subtype : Entity_Id;
9558 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9559 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
9560 Set_Scalar_Range (Index_Subtype, Drange);
9561 Set_Parent (Drange, N);
9562 Analyze_And_Resolve (Drange, Index_Type);
9564 -- In the context, the Index_Type may already have a constraint,
9565 -- so use common base type on string subtype. The base type may
9566 -- be used when generating attributes of the string, for example
9567 -- in the context of a slice assignment.
9569 Set_Etype (Index_Subtype, Base_Type (Index_Type));
9570 Set_Size_Info (Index_Subtype, Index_Type);
9571 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9573 Array_Subtype := Create_Itype (E_Array_Subtype, N);
9575 Index := New_Occurrence_Of (Index_Subtype, Loc);
9576 Set_Etype (Index, Index_Subtype);
9577 Append (Index, Index_List);
9579 Set_First_Index (Array_Subtype, Index);
9580 Set_Etype (Array_Subtype, Base_Type (Typ));
9581 Set_Is_Constrained (Array_Subtype, True);
9584 Make_Unchecked_Type_Conversion (Loc,
9585 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9586 Expression => Relocate_Node (N)));
9587 Set_Etype (N, Array_Subtype);
9590 end Set_String_Literal_Subtype;
9592 ------------------------------
9593 -- Simplify_Type_Conversion --
9594 ------------------------------
9596 procedure Simplify_Type_Conversion (N : Node_Id) is
9598 if Nkind (N) = N_Type_Conversion then
9600 Operand : constant Node_Id := Expression (N);
9601 Target_Typ : constant Entity_Id := Etype (N);
9602 Opnd_Typ : constant Entity_Id := Etype (Operand);
9605 if Is_Floating_Point_Type (Opnd_Typ)
9607 (Is_Integer_Type (Target_Typ)
9608 or else (Is_Fixed_Point_Type (Target_Typ)
9609 and then Conversion_OK (N)))
9610 and then Nkind (Operand) = N_Attribute_Reference
9611 and then Attribute_Name (Operand) = Name_Truncation
9613 -- Special processing required if the conversion is the expression
9614 -- of a Truncation attribute reference. In this case we replace:
9616 -- ityp (ftyp'Truncation (x))
9622 -- with the Float_Truncate flag set, which is more efficient.
9626 Relocate_Node (First (Expressions (Operand))));
9627 Set_Float_Truncate (N, True);
9631 end Simplify_Type_Conversion;
9633 -----------------------------
9634 -- Unique_Fixed_Point_Type --
9635 -----------------------------
9637 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
9638 T1 : Entity_Id := Empty;
9643 procedure Fixed_Point_Error;
9644 -- Give error messages for true ambiguity. Messages are posted on node
9645 -- N, and entities T1, T2 are the possible interpretations.
9647 -----------------------
9648 -- Fixed_Point_Error --
9649 -----------------------
9651 procedure Fixed_Point_Error is
9653 Error_Msg_N ("ambiguous universal_fixed_expression", N);
9654 Error_Msg_NE ("\\possible interpretation as}", N, T1);
9655 Error_Msg_NE ("\\possible interpretation as}", N, T2);
9656 end Fixed_Point_Error;
9658 -- Start of processing for Unique_Fixed_Point_Type
9661 -- The operations on Duration are visible, so Duration is always a
9662 -- possible interpretation.
9664 T1 := Standard_Duration;
9666 -- Look for fixed-point types in enclosing scopes
9668 Scop := Current_Scope;
9669 while Scop /= Standard_Standard loop
9670 T2 := First_Entity (Scop);
9671 while Present (T2) loop
9672 if Is_Fixed_Point_Type (T2)
9673 and then Current_Entity (T2) = T2
9674 and then Scope (Base_Type (T2)) = Scop
9676 if Present (T1) then
9687 Scop := Scope (Scop);
9690 -- Look for visible fixed type declarations in the context
9692 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
9693 while Present (Item) loop
9694 if Nkind (Item) = N_With_Clause then
9695 Scop := Entity (Name (Item));
9696 T2 := First_Entity (Scop);
9697 while Present (T2) loop
9698 if Is_Fixed_Point_Type (T2)
9699 and then Scope (Base_Type (T2)) = Scop
9700 and then (Is_Potentially_Use_Visible (T2)
9701 or else In_Use (T2))
9703 if Present (T1) then
9718 if Nkind (N) = N_Real_Literal then
9719 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
9721 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
9725 end Unique_Fixed_Point_Type;
9727 ----------------------
9728 -- Valid_Conversion --
9729 ----------------------
9731 function Valid_Conversion
9734 Operand : Node_Id) return Boolean
9736 Target_Type : constant Entity_Id := Base_Type (Target);
9737 Opnd_Type : Entity_Id := Etype (Operand);
9739 function Conversion_Check
9741 Msg : String) return Boolean;
9742 -- Little routine to post Msg if Valid is False, returns Valid value
9744 function Valid_Tagged_Conversion
9745 (Target_Type : Entity_Id;
9746 Opnd_Type : Entity_Id) return Boolean;
9747 -- Specifically test for validity of tagged conversions
9749 function Valid_Array_Conversion return Boolean;
9750 -- Check index and component conformance, and accessibility levels if
9751 -- the component types are anonymous access types (Ada 2005).
9753 ----------------------
9754 -- Conversion_Check --
9755 ----------------------
9757 function Conversion_Check
9759 Msg : String) return Boolean
9763 Error_Msg_N (Msg, Operand);
9767 end Conversion_Check;
9769 ----------------------------
9770 -- Valid_Array_Conversion --
9771 ----------------------------
9773 function Valid_Array_Conversion return Boolean
9775 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
9776 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
9778 Opnd_Index : Node_Id;
9779 Opnd_Index_Type : Entity_Id;
9781 Target_Comp_Type : constant Entity_Id :=
9782 Component_Type (Target_Type);
9783 Target_Comp_Base : constant Entity_Id :=
9784 Base_Type (Target_Comp_Type);
9786 Target_Index : Node_Id;
9787 Target_Index_Type : Entity_Id;
9790 -- Error if wrong number of dimensions
9793 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
9796 ("incompatible number of dimensions for conversion", Operand);
9799 -- Number of dimensions matches
9802 -- Loop through indexes of the two arrays
9804 Target_Index := First_Index (Target_Type);
9805 Opnd_Index := First_Index (Opnd_Type);
9806 while Present (Target_Index) and then Present (Opnd_Index) loop
9807 Target_Index_Type := Etype (Target_Index);
9808 Opnd_Index_Type := Etype (Opnd_Index);
9810 -- Error if index types are incompatible
9812 if not (Is_Integer_Type (Target_Index_Type)
9813 and then Is_Integer_Type (Opnd_Index_Type))
9814 and then (Root_Type (Target_Index_Type)
9815 /= Root_Type (Opnd_Index_Type))
9818 ("incompatible index types for array conversion",
9823 Next_Index (Target_Index);
9824 Next_Index (Opnd_Index);
9827 -- If component types have same base type, all set
9829 if Target_Comp_Base = Opnd_Comp_Base then
9832 -- Here if base types of components are not the same. The only
9833 -- time this is allowed is if we have anonymous access types.
9835 -- The conversion of arrays of anonymous access types can lead
9836 -- to dangling pointers. AI-392 formalizes the accessibility
9837 -- checks that must be applied to such conversions to prevent
9838 -- out-of-scope references.
9841 Ekind_In (Target_Comp_Base, E_Anonymous_Access_Type,
9842 E_Anonymous_Access_Subprogram_Type)
9843 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
9845 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
9847 if Type_Access_Level (Target_Type) <
9848 Type_Access_Level (Opnd_Type)
9850 if In_Instance_Body then
9851 Error_Msg_N ("?source array type " &
9852 "has deeper accessibility level than target", Operand);
9853 Error_Msg_N ("\?Program_Error will be raised at run time",
9856 Make_Raise_Program_Error (Sloc (N),
9857 Reason => PE_Accessibility_Check_Failed));
9858 Set_Etype (N, Target_Type);
9861 -- Conversion not allowed because of accessibility levels
9864 Error_Msg_N ("source array type " &
9865 "has deeper accessibility level than target", Operand);
9872 -- All other cases where component base types do not match
9876 ("incompatible component types for array conversion",
9881 -- Check that component subtypes statically match. For numeric
9882 -- types this means that both must be either constrained or
9883 -- unconstrained. For enumeration types the bounds must match.
9884 -- All of this is checked in Subtypes_Statically_Match.
9886 if not Subtypes_Statically_Match
9887 (Target_Comp_Type, Opnd_Comp_Type)
9890 ("component subtypes must statically match", Operand);
9896 end Valid_Array_Conversion;
9898 -----------------------------
9899 -- Valid_Tagged_Conversion --
9900 -----------------------------
9902 function Valid_Tagged_Conversion
9903 (Target_Type : Entity_Id;
9904 Opnd_Type : Entity_Id) return Boolean
9907 -- Upward conversions are allowed (RM 4.6(22))
9909 if Covers (Target_Type, Opnd_Type)
9910 or else Is_Ancestor (Target_Type, Opnd_Type)
9914 -- Downward conversion are allowed if the operand is class-wide
9917 elsif Is_Class_Wide_Type (Opnd_Type)
9918 and then Covers (Opnd_Type, Target_Type)
9922 elsif Covers (Opnd_Type, Target_Type)
9923 or else Is_Ancestor (Opnd_Type, Target_Type)
9926 Conversion_Check (False,
9927 "downward conversion of tagged objects not allowed");
9929 -- Ada 2005 (AI-251): The conversion to/from interface types is
9932 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
9935 -- If the operand is a class-wide type obtained through a limited_
9936 -- with clause, and the context includes the non-limited view, use
9937 -- it to determine whether the conversion is legal.
9939 elsif Is_Class_Wide_Type (Opnd_Type)
9940 and then From_With_Type (Opnd_Type)
9941 and then Present (Non_Limited_View (Etype (Opnd_Type)))
9942 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
9946 elsif Is_Access_Type (Opnd_Type)
9947 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
9953 ("invalid tagged conversion, not compatible with}",
9954 N, First_Subtype (Opnd_Type));
9957 end Valid_Tagged_Conversion;
9959 -- Start of processing for Valid_Conversion
9962 Check_Parameterless_Call (Operand);
9964 if Is_Overloaded (Operand) then
9974 -- Remove procedure calls, which syntactically cannot appear in
9975 -- this context, but which cannot be removed by type checking,
9976 -- because the context does not impose a type.
9978 -- When compiling for VMS, spurious ambiguities can be produced
9979 -- when arithmetic operations have a literal operand and return
9980 -- System.Address or a descendant of it. These ambiguities are
9981 -- otherwise resolved by the context, but for conversions there
9982 -- is no context type and the removal of the spurious operations
9983 -- must be done explicitly here.
9985 -- The node may be labelled overloaded, but still contain only one
9986 -- interpretation because others were discarded earlier. If this
9987 -- is the case, retain the single interpretation if legal.
9989 Get_First_Interp (Operand, I, It);
9990 Opnd_Type := It.Typ;
9991 Get_Next_Interp (I, It);
9994 and then Opnd_Type /= Standard_Void_Type
9996 -- More than one candidate interpretation is available
9998 Get_First_Interp (Operand, I, It);
9999 while Present (It.Typ) loop
10000 if It.Typ = Standard_Void_Type then
10004 if Present (System_Aux_Id)
10005 and then Is_Descendent_Of_Address (It.Typ)
10010 Get_Next_Interp (I, It);
10014 Get_First_Interp (Operand, I, It);
10018 if No (It.Typ) then
10019 Error_Msg_N ("illegal operand in conversion", Operand);
10023 Get_Next_Interp (I, It);
10025 if Present (It.Typ) then
10028 It1 := Disambiguate (Operand, I1, I, Any_Type);
10030 if It1 = No_Interp then
10031 Error_Msg_N ("ambiguous operand in conversion", Operand);
10033 -- If the interpretation involves a standard operator, use
10034 -- the location of the type, which may be user-defined.
10036 if Sloc (It.Nam) = Standard_Location then
10037 Error_Msg_Sloc := Sloc (It.Typ);
10039 Error_Msg_Sloc := Sloc (It.Nam);
10042 Error_Msg_N -- CODEFIX
10043 ("\\possible interpretation#!", Operand);
10045 if Sloc (N1) = Standard_Location then
10046 Error_Msg_Sloc := Sloc (T1);
10048 Error_Msg_Sloc := Sloc (N1);
10051 Error_Msg_N -- CODEFIX
10052 ("\\possible interpretation#!", Operand);
10058 Set_Etype (Operand, It1.Typ);
10059 Opnd_Type := It1.Typ;
10065 if Is_Numeric_Type (Target_Type) then
10067 -- A universal fixed expression can be converted to any numeric type
10069 if Opnd_Type = Universal_Fixed then
10072 -- Also no need to check when in an instance or inlined body, because
10073 -- the legality has been established when the template was analyzed.
10074 -- Furthermore, numeric conversions may occur where only a private
10075 -- view of the operand type is visible at the instantiation point.
10076 -- This results in a spurious error if we check that the operand type
10077 -- is a numeric type.
10079 -- Note: in a previous version of this unit, the following tests were
10080 -- applied only for generated code (Comes_From_Source set to False),
10081 -- but in fact the test is required for source code as well, since
10082 -- this situation can arise in source code.
10084 elsif In_Instance or else In_Inlined_Body then
10087 -- Otherwise we need the conversion check
10090 return Conversion_Check
10091 (Is_Numeric_Type (Opnd_Type),
10092 "illegal operand for numeric conversion");
10097 elsif Is_Array_Type (Target_Type) then
10098 if not Is_Array_Type (Opnd_Type)
10099 or else Opnd_Type = Any_Composite
10100 or else Opnd_Type = Any_String
10102 Error_Msg_N ("illegal operand for array conversion", Operand);
10105 return Valid_Array_Conversion;
10108 -- Ada 2005 (AI-251): Anonymous access types where target references an
10111 elsif Ekind_In (Target_Type, E_General_Access_Type,
10112 E_Anonymous_Access_Type)
10113 and then Is_Interface (Directly_Designated_Type (Target_Type))
10115 -- Check the static accessibility rule of 4.6(17). Note that the
10116 -- check is not enforced when within an instance body, since the
10117 -- RM requires such cases to be caught at run time.
10119 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10120 if Type_Access_Level (Opnd_Type) >
10121 Type_Access_Level (Target_Type)
10123 -- In an instance, this is a run-time check, but one we know
10124 -- will fail, so generate an appropriate warning. The raise
10125 -- will be generated by Expand_N_Type_Conversion.
10127 if In_Instance_Body then
10129 ("?cannot convert local pointer to non-local access type",
10132 ("\?Program_Error will be raised at run time", Operand);
10135 ("cannot convert local pointer to non-local access type",
10140 -- Special accessibility checks are needed in the case of access
10141 -- discriminants declared for a limited type.
10143 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10144 and then not Is_Local_Anonymous_Access (Opnd_Type)
10146 -- When the operand is a selected access discriminant the check
10147 -- needs to be made against the level of the object denoted by
10148 -- the prefix of the selected name (Object_Access_Level handles
10149 -- checking the prefix of the operand for this case).
10151 if Nkind (Operand) = N_Selected_Component
10152 and then Object_Access_Level (Operand) >
10153 Type_Access_Level (Target_Type)
10155 -- In an instance, this is a run-time check, but one we know
10156 -- will fail, so generate an appropriate warning. The raise
10157 -- will be generated by Expand_N_Type_Conversion.
10159 if In_Instance_Body then
10161 ("?cannot convert access discriminant to non-local" &
10162 " access type", Operand);
10164 ("\?Program_Error will be raised at run time", Operand);
10167 ("cannot convert access discriminant to non-local" &
10168 " access type", Operand);
10173 -- The case of a reference to an access discriminant from
10174 -- within a limited type declaration (which will appear as
10175 -- a discriminal) is always illegal because the level of the
10176 -- discriminant is considered to be deeper than any (nameable)
10179 if Is_Entity_Name (Operand)
10180 and then not Is_Local_Anonymous_Access (Opnd_Type)
10182 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10183 and then Present (Discriminal_Link (Entity (Operand)))
10186 ("discriminant has deeper accessibility level than target",
10195 -- General and anonymous access types
10197 elsif Ekind_In (Target_Type, E_General_Access_Type,
10198 E_Anonymous_Access_Type)
10201 (Is_Access_Type (Opnd_Type)
10203 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10204 E_Access_Protected_Subprogram_Type),
10205 "must be an access-to-object type")
10207 if Is_Access_Constant (Opnd_Type)
10208 and then not Is_Access_Constant (Target_Type)
10211 ("access-to-constant operand type not allowed", Operand);
10215 -- Check the static accessibility rule of 4.6(17). Note that the
10216 -- check is not enforced when within an instance body, since the RM
10217 -- requires such cases to be caught at run time.
10219 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10220 or else Is_Local_Anonymous_Access (Target_Type)
10222 if Type_Access_Level (Opnd_Type)
10223 > Type_Access_Level (Target_Type)
10225 -- In an instance, this is a run-time check, but one we know
10226 -- will fail, so generate an appropriate warning. The raise
10227 -- will be generated by Expand_N_Type_Conversion.
10229 if In_Instance_Body then
10231 ("?cannot convert local pointer to non-local access type",
10234 ("\?Program_Error will be raised at run time", Operand);
10237 -- Avoid generation of spurious error message
10239 if not Error_Posted (N) then
10241 ("cannot convert local pointer to non-local access type",
10248 -- Special accessibility checks are needed in the case of access
10249 -- discriminants declared for a limited type.
10251 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10252 and then not Is_Local_Anonymous_Access (Opnd_Type)
10254 -- When the operand is a selected access discriminant the check
10255 -- needs to be made against the level of the object denoted by
10256 -- the prefix of the selected name (Object_Access_Level handles
10257 -- checking the prefix of the operand for this case).
10259 if Nkind (Operand) = N_Selected_Component
10260 and then Object_Access_Level (Operand) >
10261 Type_Access_Level (Target_Type)
10263 -- In an instance, this is a run-time check, but one we know
10264 -- will fail, so generate an appropriate warning. The raise
10265 -- will be generated by Expand_N_Type_Conversion.
10267 if In_Instance_Body then
10269 ("?cannot convert access discriminant to non-local" &
10270 " access type", Operand);
10272 ("\?Program_Error will be raised at run time",
10277 ("cannot convert access discriminant to non-local" &
10278 " access type", Operand);
10283 -- The case of a reference to an access discriminant from
10284 -- within a limited type declaration (which will appear as
10285 -- a discriminal) is always illegal because the level of the
10286 -- discriminant is considered to be deeper than any (nameable)
10289 if Is_Entity_Name (Operand)
10291 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10292 and then Present (Discriminal_Link (Entity (Operand)))
10295 ("discriminant has deeper accessibility level than target",
10302 -- In the presence of limited_with clauses we have to use non-limited
10303 -- views, if available.
10305 Check_Limited : declare
10306 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10307 -- Helper function to handle limited views
10309 --------------------------
10310 -- Full_Designated_Type --
10311 --------------------------
10313 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10314 Desig : constant Entity_Id := Designated_Type (T);
10317 -- Handle the limited view of a type
10319 if Is_Incomplete_Type (Desig)
10320 and then From_With_Type (Desig)
10321 and then Present (Non_Limited_View (Desig))
10323 return Available_View (Desig);
10327 end Full_Designated_Type;
10329 -- Local Declarations
10331 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10332 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10334 Same_Base : constant Boolean :=
10335 Base_Type (Target) = Base_Type (Opnd);
10337 -- Start of processing for Check_Limited
10340 if Is_Tagged_Type (Target) then
10341 return Valid_Tagged_Conversion (Target, Opnd);
10344 if not Same_Base then
10346 ("target designated type not compatible with }",
10347 N, Base_Type (Opnd));
10350 -- Ada 2005 AI-384: legality rule is symmetric in both
10351 -- designated types. The conversion is legal (with possible
10352 -- constraint check) if either designated type is
10355 elsif Subtypes_Statically_Match (Target, Opnd)
10357 (Has_Discriminants (Target)
10359 (not Is_Constrained (Opnd)
10360 or else not Is_Constrained (Target)))
10362 -- Special case, if Value_Size has been used to make the
10363 -- sizes different, the conversion is not allowed even
10364 -- though the subtypes statically match.
10366 if Known_Static_RM_Size (Target)
10367 and then Known_Static_RM_Size (Opnd)
10368 and then RM_Size (Target) /= RM_Size (Opnd)
10371 ("target designated subtype not compatible with }",
10374 ("\because sizes of the two designated subtypes differ",
10378 -- Normal case where conversion is allowed
10386 ("target designated subtype not compatible with }",
10393 -- Access to subprogram types. If the operand is an access parameter,
10394 -- the type has a deeper accessibility that any master, and cannot be
10395 -- assigned. We must make an exception if the conversion is part of an
10396 -- assignment and the target is the return object of an extended return
10397 -- statement, because in that case the accessibility check takes place
10398 -- after the return.
10400 elsif Is_Access_Subprogram_Type (Target_Type)
10401 and then No (Corresponding_Remote_Type (Opnd_Type))
10403 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10404 and then Is_Entity_Name (Operand)
10405 and then Ekind (Entity (Operand)) = E_In_Parameter
10407 (Nkind (Parent (N)) /= N_Assignment_Statement
10408 or else not Is_Entity_Name (Name (Parent (N)))
10409 or else not Is_Return_Object (Entity (Name (Parent (N)))))
10412 ("illegal attempt to store anonymous access to subprogram",
10415 ("\value has deeper accessibility than any master " &
10416 "(RM 3.10.2 (13))",
10420 ("\use named access type for& instead of access parameter",
10421 Operand, Entity (Operand));
10424 -- Check that the designated types are subtype conformant
10426 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
10427 Old_Id => Designated_Type (Opnd_Type),
10430 -- Check the static accessibility rule of 4.6(20)
10432 if Type_Access_Level (Opnd_Type) >
10433 Type_Access_Level (Target_Type)
10436 ("operand type has deeper accessibility level than target",
10439 -- Check that if the operand type is declared in a generic body,
10440 -- then the target type must be declared within that same body
10441 -- (enforces last sentence of 4.6(20)).
10443 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
10445 O_Gen : constant Node_Id :=
10446 Enclosing_Generic_Body (Opnd_Type);
10451 T_Gen := Enclosing_Generic_Body (Target_Type);
10452 while Present (T_Gen) and then T_Gen /= O_Gen loop
10453 T_Gen := Enclosing_Generic_Body (T_Gen);
10456 if T_Gen /= O_Gen then
10458 ("target type must be declared in same generic body"
10459 & " as operand type", N);
10466 -- Remote subprogram access types
10468 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
10469 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
10471 -- It is valid to convert from one RAS type to another provided
10472 -- that their specification statically match.
10474 Check_Subtype_Conformant
10476 Designated_Type (Corresponding_Remote_Type (Target_Type)),
10478 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
10483 -- If both are tagged types, check legality of view conversions
10485 elsif Is_Tagged_Type (Target_Type)
10487 Is_Tagged_Type (Opnd_Type)
10489 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
10491 -- Types derived from the same root type are convertible
10493 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
10496 -- In an instance or an inlined body, there may be inconsistent views of
10497 -- the same type, or of types derived from a common root.
10499 elsif (In_Instance or In_Inlined_Body)
10501 Root_Type (Underlying_Type (Target_Type)) =
10502 Root_Type (Underlying_Type (Opnd_Type))
10506 -- Special check for common access type error case
10508 elsif Ekind (Target_Type) = E_Access_Type
10509 and then Is_Access_Type (Opnd_Type)
10511 Error_Msg_N ("target type must be general access type!", N);
10512 Error_Msg_NE -- CODEFIX
10513 ("add ALL to }!", N, Target_Type);
10517 Error_Msg_NE ("invalid conversion, not compatible with }",
10521 end Valid_Conversion;