1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Ghost; use Ghost;
40 with Inline; use Inline;
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 Par_SCO; use Par_SCO;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aggr; use Sem_Aggr;
55 with Sem_Attr; use Sem_Attr;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch4; use Sem_Ch4;
60 with Sem_Ch6; use Sem_Ch6;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Dim; use Sem_Dim;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elab; use Sem_Elab;
67 with Sem_Elim; use Sem_Elim;
68 with Sem_Eval; use Sem_Eval;
69 with Sem_Intr; use Sem_Intr;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Sinfo; use Sinfo;
75 with Sinfo.CN; use Sinfo.CN;
76 with Snames; use Snames;
77 with Stand; use Stand;
78 with Stringt; use Stringt;
79 with Style; use Style;
80 with Targparm; use Targparm;
81 with Tbuild; use Tbuild;
82 with Uintp; use Uintp;
83 with Urealp; use Urealp;
85 package body Sem_Res is
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
91 -- Second pass (top-down) type checking and overload resolution procedures
92 -- Typ is the type required by context. These procedures propagate the
93 -- type information recursively to the descendants of N. If the node is not
94 -- overloaded, its Etype is established in the first pass. If overloaded,
95 -- the Resolve routines set the correct type. For arithmetic operators, the
96 -- Etype is the base type of the context.
98 -- Note that Resolve_Attribute is separated off in Sem_Attr
100 procedure Check_Discriminant_Use (N : Node_Id);
101 -- Enforce the restrictions on the use of discriminants when constraining
102 -- a component of a discriminated type (record or concurrent type).
104 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
105 -- Given a node for an operator associated with type T, check that the
106 -- operator is visible. Operators all of whose operands are universal must
107 -- be checked for visibility during resolution because their type is not
108 -- determinable based on their operands.
110 procedure Check_Fully_Declared_Prefix
113 -- Check that the type of the prefix of a dereference is not incomplete
115 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
116 -- Given a call node, Call, which is known to occur immediately within the
117 -- subprogram being called, determines whether it is a detectable case of
118 -- an infinite recursion, and if so, outputs appropriate messages. Returns
119 -- True if an infinite recursion is detected, and False otherwise.
121 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
122 -- N is the node for a logical operator. If the operator is predefined, and
123 -- the root type of the operands is Standard.Boolean, then a check is made
124 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
125 -- the style check for Style_Check_Boolean_And_Or.
127 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
128 -- N is either an indexed component or a selected component. This function
129 -- returns true if the prefix refers to an object that has an address
130 -- clause (the case in which we may want to issue a warning).
132 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
133 -- Determine whether E is an access type declared by an access declaration,
134 -- and not an (anonymous) allocator type.
136 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
137 -- Utility to check whether the entity for an operator is a predefined
138 -- operator, in which case the expression is left as an operator in the
139 -- tree (else it is rewritten into a call). An instance of an intrinsic
140 -- conversion operation may be given an operator name, but is not treated
141 -- like an operator. Note that an operator that is an imported back-end
142 -- builtin has convention Intrinsic, but is expected to be rewritten into
143 -- a call, so such an operator is not treated as predefined by this
146 procedure Preanalyze_And_Resolve
149 With_Freezing : Boolean);
150 -- Subsidiary of public versions of Preanalyze_And_Resolve.
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_Entity_Name (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
211 function Operator_Kind
213 Is_Binary : Boolean) return Node_Kind;
214 -- Utility to map the name of an operator into the corresponding Node. Used
215 -- by other node rewriting procedures.
217 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
218 -- Resolve actuals of call, and add default expressions for missing ones.
219 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
220 -- called subprogram.
222 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
223 -- Called from Resolve_Call, when the prefix denotes an entry or element
224 -- of entry family. Actuals are resolved as for subprograms, and the node
225 -- is rebuilt as an entry call. Also called for protected operations. Typ
226 -- is the context type, which is used when the operation is a protected
227 -- function with no arguments, and the return value is indexed.
229 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
230 -- A call to a user-defined intrinsic operator is rewritten as a call to
231 -- the corresponding predefined operator, with suitable conversions. Note
232 -- that this applies only for intrinsic operators that denote predefined
233 -- operators, not ones that are intrinsic imports of back-end builtins.
235 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
236 -- Ditto, for arithmetic unary operators
238 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
239 -- If an operator node resolves to a call to a user-defined operator,
240 -- rewrite the node as a function call.
242 procedure Make_Call_Into_Operator
246 -- Inverse transformation: if an operator is given in functional notation,
247 -- then after resolving the node, transform into an operator node, so that
248 -- operands are resolved properly. Recall that predefined operators do not
249 -- have a full signature and special resolution rules apply.
251 procedure Rewrite_Renamed_Operator
255 -- An operator can rename another, e.g. in an instantiation. In that
256 -- case, the proper operator node must be constructed and resolved.
258 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
259 -- The String_Literal_Subtype is built for all strings that are not
260 -- operands of a static concatenation operation. If the argument is not
261 -- a N_String_Literal node, then the call has no effect.
263 procedure Set_Slice_Subtype (N : Node_Id);
264 -- Build subtype of array type, with the range specified by the slice
266 procedure Simplify_Type_Conversion (N : Node_Id);
267 -- Called after N has been resolved and evaluated, but before range checks
268 -- have been applied. This rewrites the conversion into a simpler form.
270 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
271 -- A universal_fixed expression in an universal context is unambiguous if
272 -- there is only one applicable fixed point type. Determining whether there
273 -- is only one requires a search over all visible entities, and happens
274 -- only in very pathological cases (see 6115-006).
276 -------------------------
277 -- Ambiguous_Character --
278 -------------------------
280 procedure Ambiguous_Character (C : Node_Id) is
284 if Nkind (C) = N_Character_Literal then
285 Error_Msg_N ("ambiguous character literal", C);
287 -- First the ones in Standard
289 Error_Msg_N ("\\possible interpretation: Character!", C);
290 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
292 -- Include Wide_Wide_Character in Ada 2005 mode
294 if Ada_Version >= Ada_2005 then
295 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
298 -- Now any other types that match
300 E := Current_Entity (C);
301 while Present (E) loop
302 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
306 end Ambiguous_Character;
308 -------------------------
309 -- Analyze_And_Resolve --
310 -------------------------
312 procedure Analyze_And_Resolve (N : Node_Id) is
316 end Analyze_And_Resolve;
318 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
322 end Analyze_And_Resolve;
324 -- Versions with check(s) suppressed
326 procedure Analyze_And_Resolve
331 Scop : constant Entity_Id := Current_Scope;
334 if Suppress = All_Checks then
336 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
338 Scope_Suppress.Suppress := (others => True);
339 Analyze_And_Resolve (N, Typ);
340 Scope_Suppress.Suppress := Sva;
345 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
347 Scope_Suppress.Suppress (Suppress) := True;
348 Analyze_And_Resolve (N, Typ);
349 Scope_Suppress.Suppress (Suppress) := Svg;
353 if Current_Scope /= Scop
354 and then Scope_Is_Transient
356 -- This can only happen if a transient scope was created for an inner
357 -- expression, which will be removed upon completion of the analysis
358 -- of an enclosing construct. The transient scope must have the
359 -- suppress status of the enclosing environment, not of this Analyze
362 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
365 end Analyze_And_Resolve;
367 procedure Analyze_And_Resolve
371 Scop : constant Entity_Id := Current_Scope;
374 if Suppress = All_Checks then
376 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
378 Scope_Suppress.Suppress := (others => True);
379 Analyze_And_Resolve (N);
380 Scope_Suppress.Suppress := Sva;
385 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
387 Scope_Suppress.Suppress (Suppress) := True;
388 Analyze_And_Resolve (N);
389 Scope_Suppress.Suppress (Suppress) := Svg;
393 if Current_Scope /= Scop and then Scope_Is_Transient then
394 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
397 end Analyze_And_Resolve;
399 ----------------------------
400 -- Check_Discriminant_Use --
401 ----------------------------
403 procedure Check_Discriminant_Use (N : Node_Id) is
404 PN : constant Node_Id := Parent (N);
405 Disc : constant Entity_Id := Entity (N);
410 -- Any use in a spec-expression is legal
412 if In_Spec_Expression then
415 elsif Nkind (PN) = N_Range then
417 -- Discriminant cannot be used to constrain a scalar type
421 if Nkind (P) = N_Range_Constraint
422 and then Nkind (Parent (P)) = N_Subtype_Indication
423 and then Nkind (Parent (Parent (P))) = N_Component_Definition
425 Error_Msg_N ("discriminant cannot constrain scalar type", N);
427 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
429 -- The following check catches the unusual case where a
430 -- discriminant appears within an index constraint that is part
431 -- of a larger expression within a constraint on a component,
432 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
433 -- check case of record components, and note that a similar check
434 -- should also apply in the case of discriminant constraints
437 -- Note that the check for N_Subtype_Declaration below is to
438 -- detect the valid use of discriminants in the constraints of a
439 -- subtype declaration when this subtype declaration appears
440 -- inside the scope of a record type (which is syntactically
441 -- illegal, but which may be created as part of derived type
442 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
445 if Ekind (Current_Scope) = E_Record_Type
446 and then Scope (Disc) = Current_Scope
448 (Nkind (Parent (P)) = N_Subtype_Indication
450 Nkind_In (Parent (Parent (P)), N_Component_Definition,
451 N_Subtype_Declaration)
452 and then Paren_Count (N) = 0)
455 ("discriminant must appear alone in component constraint", N);
459 -- Detect a common error:
461 -- type R (D : Positive := 100) is record
462 -- Name : String (1 .. D);
465 -- The default value causes an object of type R to be allocated
466 -- with room for Positive'Last characters. The RM does not mandate
467 -- the allocation of the maximum size, but that is what GNAT does
468 -- so we should warn the programmer that there is a problem.
470 Check_Large : declare
476 function Large_Storage_Type (T : Entity_Id) return Boolean;
477 -- Return True if type T has a large enough range that any
478 -- array whose index type covered the whole range of the type
479 -- would likely raise Storage_Error.
481 ------------------------
482 -- Large_Storage_Type --
483 ------------------------
485 function Large_Storage_Type (T : Entity_Id) return Boolean is
487 -- The type is considered large if its bounds are known at
488 -- compile time and if it requires at least as many bits as
489 -- a Positive to store the possible values.
491 return Compile_Time_Known_Value (Type_Low_Bound (T))
492 and then Compile_Time_Known_Value (Type_High_Bound (T))
494 Minimum_Size (T, Biased => True) >=
495 RM_Size (Standard_Positive);
496 end Large_Storage_Type;
498 -- Start of processing for Check_Large
501 -- Check that the Disc has a large range
503 if not Large_Storage_Type (Etype (Disc)) then
507 -- If the enclosing type is limited, we allocate only the
508 -- default value, not the maximum, and there is no need for
511 if Is_Limited_Type (Scope (Disc)) then
515 -- Check that it is the high bound
517 if N /= High_Bound (PN)
518 or else No (Discriminant_Default_Value (Disc))
523 -- Check the array allows a large range at this bound. First
528 if Nkind (SI) /= N_Subtype_Indication then
532 T := Entity (Subtype_Mark (SI));
534 if not Is_Array_Type (T) then
538 -- Next, find the dimension
540 TB := First_Index (T);
541 CB := First (Constraints (P));
543 and then Present (TB)
544 and then Present (CB)
555 -- Now, check the dimension has a large range
557 if not Large_Storage_Type (Etype (TB)) then
561 -- Warn about the danger
564 ("??creation of & object may raise Storage_Error!",
573 -- Legal case is in index or discriminant constraint
575 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
576 N_Discriminant_Association)
578 if Paren_Count (N) > 0 then
580 ("discriminant in constraint must appear alone", N);
582 elsif Nkind (N) = N_Expanded_Name
583 and then Comes_From_Source (N)
586 ("discriminant must appear alone as a direct name", N);
591 -- Otherwise, context is an expression. It should not be within (i.e. a
592 -- subexpression of) a constraint for a component.
597 while not Nkind_In (P, N_Component_Declaration,
598 N_Subtype_Indication,
606 -- If the discriminant is used in an expression that is a bound of a
607 -- scalar type, an Itype is created and the bounds are attached to
608 -- its range, not to the original subtype indication. Such use is of
609 -- course a double fault.
611 if (Nkind (P) = N_Subtype_Indication
612 and then Nkind_In (Parent (P), N_Component_Definition,
613 N_Derived_Type_Definition)
614 and then D = Constraint (P))
616 -- The constraint itself may be given by a subtype indication,
617 -- rather than by a more common discrete range.
619 or else (Nkind (P) = N_Subtype_Indication
621 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
622 or else Nkind (P) = N_Entry_Declaration
623 or else Nkind (D) = N_Defining_Identifier
626 ("discriminant in constraint must appear alone", N);
629 end Check_Discriminant_Use;
631 --------------------------------
632 -- Check_For_Visible_Operator --
633 --------------------------------
635 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
637 if Is_Invisible_Operator (N, T) then
638 Error_Msg_NE -- CODEFIX
639 ("operator for} is not directly visible!", N, First_Subtype (T));
640 Error_Msg_N -- CODEFIX
641 ("use clause would make operation legal!", N);
643 end Check_For_Visible_Operator;
645 ----------------------------------
646 -- Check_Fully_Declared_Prefix --
647 ----------------------------------
649 procedure Check_Fully_Declared_Prefix
654 -- Check that the designated type of the prefix of a dereference is
655 -- not an incomplete type. This cannot be done unconditionally, because
656 -- dereferences of private types are legal in default expressions. This
657 -- case is taken care of in Check_Fully_Declared, called below. There
658 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
660 -- This consideration also applies to similar checks for allocators,
661 -- qualified expressions, and type conversions.
663 -- An additional exception concerns other per-object expressions that
664 -- are not directly related to component declarations, in particular
665 -- representation pragmas for tasks. These will be per-object
666 -- expressions if they depend on discriminants or some global entity.
667 -- If the task has access discriminants, the designated type may be
668 -- incomplete at the point the expression is resolved. This resolution
669 -- takes place within the body of the initialization procedure, where
670 -- the discriminant is replaced by its discriminal.
672 if Is_Entity_Name (Pref)
673 and then Ekind (Entity (Pref)) = E_In_Parameter
677 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
678 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
679 -- Analyze_Object_Renaming, and Freeze_Entity.
681 elsif Ada_Version >= Ada_2005
682 and then Is_Entity_Name (Pref)
683 and then Is_Access_Type (Etype (Pref))
684 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
686 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
690 Check_Fully_Declared (Typ, Parent (Pref));
692 end Check_Fully_Declared_Prefix;
694 ------------------------------
695 -- Check_Infinite_Recursion --
696 ------------------------------
698 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
699 function Enclosing_Declaration_Or_Statement (N : Node_Id) return Node_Id;
700 -- Return the nearest enclosing declaration or statement that houses
703 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
704 -- Determine whether call N invokes the related enclosing subprogram
705 -- with actuals that differ from the subprogram's formals.
707 function Is_Conditional_Statement (N : Node_Id) return Boolean;
708 -- Determine whether arbitrary node N denotes a conditional construct
710 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
711 -- Determine whether arbitrary node N denotes a control flow statement
712 -- or a construct that may contains such a statement.
714 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
715 -- Determine whether arbitrary node N appears immediately within the
716 -- statements of an entry or subprogram body.
718 function Is_Raise_Idiom (N : Node_Id) return Boolean;
719 -- Determine whether arbitrary node N appears immediately within the
720 -- body of an entry or subprogram, and is preceded by a single raise
723 function Is_Raise_Statement (N : Node_Id) return Boolean;
724 -- Determine whether arbitrary node N denotes a raise statement
726 function Is_Sole_Statement (N : Node_Id) return Boolean;
727 -- Determine whether arbitrary node N is the sole source statement in
728 -- the body of the enclosing subprogram.
730 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
731 -- Determine whether arbitrary node N is preceded by a control flow
734 function Within_Conditional_Statement (N : Node_Id) return Boolean;
735 -- Determine whether arbitrary node N appears within a conditional
738 ----------------------------------------
739 -- Enclosing_Declaration_Or_Statement --
740 ----------------------------------------
742 function Enclosing_Declaration_Or_Statement
743 (N : Node_Id) return Node_Id
749 while Present (Par) loop
750 if Is_Declaration (Par) or else Is_Statement (Par) then
753 -- Prevent the search from going too far
755 elsif Is_Body_Or_Package_Declaration (Par) then
763 end Enclosing_Declaration_Or_Statement;
765 --------------------------------------
766 -- Invoked_With_Different_Arguments --
767 --------------------------------------
769 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
770 Subp : constant Entity_Id := Entity (Name (N));
776 -- Determine whether the formals of the invoked subprogram are not
777 -- used as actuals in the call.
779 Actual := First_Actual (Call);
780 Formal := First_Formal (Subp);
781 while Present (Actual) and then Present (Formal) loop
783 -- The current actual does not match the current formal
785 if not (Is_Entity_Name (Actual)
786 and then Entity (Actual) = Formal)
791 Next_Actual (Actual);
792 Next_Formal (Formal);
796 end Invoked_With_Different_Arguments;
798 ------------------------------
799 -- Is_Conditional_Statement --
800 ------------------------------
802 function Is_Conditional_Statement (N : Node_Id) return Boolean is
805 Nkind_In (N, N_And_Then,
811 end Is_Conditional_Statement;
813 -------------------------------
814 -- Is_Control_Flow_Statement --
815 -------------------------------
817 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
819 -- It is assumed that all statements may affect the control flow in
820 -- some way. A raise statement may be expanded into a non-statement
823 return Is_Statement (N) or else Is_Raise_Statement (N);
824 end Is_Control_Flow_Statement;
826 --------------------------------
827 -- Is_Immediately_Within_Body --
828 --------------------------------
830 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
831 HSS : constant Node_Id := Parent (N);
835 Nkind (HSS) = N_Handled_Sequence_Of_Statements
836 and then Nkind_In (Parent (HSS), N_Entry_Body, N_Subprogram_Body)
837 and then Is_List_Member (N)
838 and then List_Containing (N) = Statements (HSS);
839 end Is_Immediately_Within_Body;
845 function Is_Raise_Idiom (N : Node_Id) return Boolean is
846 Raise_Stmt : Node_Id;
850 if Is_Immediately_Within_Body (N) then
852 -- Assume that no raise statement has been seen yet
856 -- Examine the statements preceding the input node, skipping
857 -- internally-generated constructs.
860 while Present (Stmt) loop
862 -- Multiple raise statements violate the idiom
864 if Is_Raise_Statement (Stmt) then
865 if Present (Raise_Stmt) then
871 elsif Comes_From_Source (Stmt) then
878 -- At this point the node must be preceded by a raise statement,
879 -- and the raise statement has to be the sole statement within
880 -- the enclosing entry or subprogram body.
883 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
889 ------------------------
890 -- Is_Raise_Statement --
891 ------------------------
893 function Is_Raise_Statement (N : Node_Id) return Boolean is
895 -- A raise statement may be transfomed into a Raise_xxx_Error node
898 Nkind (N) = N_Raise_Statement
899 or else Nkind (N) in N_Raise_xxx_Error;
900 end Is_Raise_Statement;
902 -----------------------
903 -- Is_Sole_Statement --
904 -----------------------
906 function Is_Sole_Statement (N : Node_Id) return Boolean is
910 -- The input node appears within the statements of an entry or
911 -- subprogram body. Examine the statements preceding the node.
913 if Is_Immediately_Within_Body (N) then
916 while Present (Stmt) loop
918 -- The statement is preceded by another statement or a source
919 -- construct. This indicates that the node does not appear by
922 if Is_Control_Flow_Statement (Stmt)
923 or else Comes_From_Source (Stmt)
934 -- The input node is within a construct nested inside the entry or
938 end Is_Sole_Statement;
940 ----------------------------------------
941 -- Preceded_By_Control_Flow_Statement --
942 ----------------------------------------
944 function Preceded_By_Control_Flow_Statement
945 (N : Node_Id) return Boolean
950 if Is_List_Member (N) then
953 -- Examine the statements preceding the input node
955 while Present (Stmt) loop
956 if Is_Control_Flow_Statement (Stmt) then
966 -- Assume that the node is part of some control flow statement
969 end Preceded_By_Control_Flow_Statement;
971 ----------------------------------
972 -- Within_Conditional_Statement --
973 ----------------------------------
975 function Within_Conditional_Statement (N : Node_Id) return Boolean is
980 while Present (Stmt) loop
981 if Is_Conditional_Statement (Stmt) then
984 -- Prevent the search from going too far
986 elsif Is_Body_Or_Package_Declaration (Stmt) then
990 Stmt := Parent (Stmt);
994 end Within_Conditional_Statement;
998 Call_Context : constant Node_Id :=
999 Enclosing_Declaration_Or_Statement (Call);
1001 -- Start of processing for Check_Infinite_Recursion
1004 -- The call is assumed to be safe when the enclosing subprogram is
1005 -- invoked with actuals other than its formals.
1007 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1010 -- Proc (A1, A2, ..., AN);
1014 if Invoked_With_Different_Arguments (Call) then
1017 -- The call is assumed to be safe when the invocation of the enclosing
1018 -- subprogram depends on a conditional statement.
1020 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1023 -- if Some_Condition then
1024 -- Proc (F1, F2, ..., FN);
1029 elsif Within_Conditional_Statement (Call) then
1032 -- The context of the call is assumed to be safe when the invocation of
1033 -- the enclosing subprogram is preceded by some control flow statement.
1035 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1038 -- if Some_Condition then
1042 -- Proc (F1, F2, ..., FN);
1046 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1049 -- Detect an idiom where the context of the call is preceded by a single
1052 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1055 -- Proc (F1, F2, ..., FN);
1058 elsif Is_Raise_Idiom (Call_Context) then
1062 -- At this point it is certain that infinite recursion will take place
1063 -- as long as the call is executed. Detect a case where the context of
1064 -- the call is the sole source statement within the subprogram body.
1066 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1068 -- Proc (F1, F2, ..., FN);
1071 -- Install an explicit raise to prevent the infinite recursion.
1073 if Is_Sole_Statement (Call_Context) then
1074 Error_Msg_Warn := SPARK_Mode /= On;
1075 Error_Msg_N ("!infinite recursion<<", Call);
1076 Error_Msg_N ("\!Storage_Error [<<", Call);
1078 Insert_Action (Call,
1079 Make_Raise_Storage_Error (Sloc (Call),
1080 Reason => SE_Infinite_Recursion));
1082 -- Otherwise infinite recursion could take place, considering other flow
1083 -- control constructs such as gotos, exit statements, etc.
1086 Error_Msg_Warn := SPARK_Mode /= On;
1087 Error_Msg_N ("!possible infinite recursion<<", Call);
1088 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1092 end Check_Infinite_Recursion;
1094 ---------------------------------------
1095 -- Check_No_Direct_Boolean_Operators --
1096 ---------------------------------------
1098 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1100 if Scope (Entity (N)) = Standard_Standard
1101 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1103 -- Restriction only applies to original source code
1105 if Comes_From_Source (N) then
1106 Check_Restriction (No_Direct_Boolean_Operators, N);
1110 -- Do style check (but skip if in instance, error is on template)
1113 if not In_Instance then
1114 Check_Boolean_Operator (N);
1117 end Check_No_Direct_Boolean_Operators;
1119 ------------------------------
1120 -- Check_Parameterless_Call --
1121 ------------------------------
1123 procedure Check_Parameterless_Call (N : Node_Id) is
1126 function Prefix_Is_Access_Subp return Boolean;
1127 -- If the prefix is of an access_to_subprogram type, the node must be
1128 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1129 -- interpretations are access to subprograms.
1131 ---------------------------
1132 -- Prefix_Is_Access_Subp --
1133 ---------------------------
1135 function Prefix_Is_Access_Subp return Boolean is
1140 -- If the context is an attribute reference that can apply to
1141 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1143 if Nkind (Parent (N)) = N_Attribute_Reference
1144 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1151 if not Is_Overloaded (N) then
1153 Ekind (Etype (N)) = E_Subprogram_Type
1154 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1156 Get_First_Interp (N, I, It);
1157 while Present (It.Typ) loop
1158 if Ekind (It.Typ) /= E_Subprogram_Type
1159 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1164 Get_Next_Interp (I, It);
1169 end Prefix_Is_Access_Subp;
1171 -- Start of processing for Check_Parameterless_Call
1174 -- Defend against junk stuff if errors already detected
1176 if Total_Errors_Detected /= 0 then
1177 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1179 elsif Nkind (N) in N_Has_Chars
1180 and then not Is_Valid_Name (Chars (N))
1188 -- If the context expects a value, and the name is a procedure, this is
1189 -- most likely a missing 'Access. Don't try to resolve the parameterless
1190 -- call, error will be caught when the outer call is analyzed.
1192 if Is_Entity_Name (N)
1193 and then Ekind (Entity (N)) = E_Procedure
1194 and then not Is_Overloaded (N)
1196 Nkind_In (Parent (N), N_Parameter_Association,
1198 N_Procedure_Call_Statement)
1203 -- Rewrite as call if overloadable entity that is (or could be, in the
1204 -- overloaded case) a function call. If we know for sure that the entity
1205 -- is an enumeration literal, we do not rewrite it.
1207 -- If the entity is the name of an operator, it cannot be a call because
1208 -- operators cannot have default parameters. In this case, this must be
1209 -- a string whose contents coincide with an operator name. Set the kind
1210 -- of the node appropriately.
1212 if (Is_Entity_Name (N)
1213 and then Nkind (N) /= N_Operator_Symbol
1214 and then Is_Overloadable (Entity (N))
1215 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1216 or else Is_Overloaded (N)))
1218 -- Rewrite as call if it is an explicit dereference of an expression of
1219 -- a subprogram access type, and the subprogram type is not that of a
1220 -- procedure or entry.
1223 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1225 -- Rewrite as call if it is a selected component which is a function,
1226 -- this is the case of a call to a protected function (which may be
1227 -- overloaded with other protected operations).
1230 (Nkind (N) = N_Selected_Component
1231 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1233 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1235 and then Is_Overloaded (Selector_Name (N)))))
1237 -- If one of the above three conditions is met, rewrite as call. Apply
1238 -- the rewriting only once.
1241 if Nkind (Parent (N)) /= N_Function_Call
1242 or else N /= Name (Parent (N))
1245 -- This may be a prefixed call that was not fully analyzed, e.g.
1246 -- an actual in an instance.
1248 if Ada_Version >= Ada_2005
1249 and then Nkind (N) = N_Selected_Component
1250 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1252 Analyze_Selected_Component (N);
1254 if Nkind (N) /= N_Selected_Component then
1259 -- The node is the name of the parameterless call. Preserve its
1260 -- descendants, which may be complex expressions.
1262 Nam := Relocate_Node (N);
1264 -- If overloaded, overload set belongs to new copy
1266 Save_Interps (N, Nam);
1268 -- Change node to parameterless function call (note that the
1269 -- Parameter_Associations associations field is left set to Empty,
1270 -- its normal default value since there are no parameters)
1272 Change_Node (N, N_Function_Call);
1274 Set_Sloc (N, Sloc (Nam));
1278 elsif Nkind (N) = N_Parameter_Association then
1279 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1281 elsif Nkind (N) = N_Operator_Symbol then
1282 Change_Operator_Symbol_To_String_Literal (N);
1283 Set_Is_Overloaded (N, False);
1284 Set_Etype (N, Any_String);
1286 end Check_Parameterless_Call;
1288 --------------------------------
1289 -- Is_Atomic_Ref_With_Address --
1290 --------------------------------
1292 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1293 Pref : constant Node_Id := Prefix (N);
1296 if not Is_Entity_Name (Pref) then
1301 Pent : constant Entity_Id := Entity (Pref);
1302 Ptyp : constant Entity_Id := Etype (Pent);
1304 return not Is_Access_Type (Ptyp)
1305 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1306 and then Present (Address_Clause (Pent));
1309 end Is_Atomic_Ref_With_Address;
1311 -----------------------------
1312 -- Is_Definite_Access_Type --
1313 -----------------------------
1315 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1316 Btyp : constant Entity_Id := Base_Type (E);
1318 return Ekind (Btyp) = E_Access_Type
1319 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1320 and then Comes_From_Source (Btyp));
1321 end Is_Definite_Access_Type;
1323 ----------------------
1324 -- Is_Predefined_Op --
1325 ----------------------
1327 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1329 -- Predefined operators are intrinsic subprograms
1331 if not Is_Intrinsic_Subprogram (Nam) then
1335 -- A call to a back-end builtin is never a predefined operator
1337 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1341 return not Is_Generic_Instance (Nam)
1342 and then Chars (Nam) in Any_Operator_Name
1343 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1344 end Is_Predefined_Op;
1346 -----------------------------
1347 -- Make_Call_Into_Operator --
1348 -----------------------------
1350 procedure Make_Call_Into_Operator
1355 Op_Name : constant Name_Id := Chars (Op_Id);
1356 Act1 : Node_Id := First_Actual (N);
1357 Act2 : Node_Id := Next_Actual (Act1);
1358 Error : Boolean := False;
1359 Func : constant Entity_Id := Entity (Name (N));
1360 Is_Binary : constant Boolean := Present (Act2);
1362 Opnd_Type : Entity_Id := Empty;
1363 Orig_Type : Entity_Id := Empty;
1366 type Kind_Test is access function (E : Entity_Id) return Boolean;
1368 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1369 -- If the operand is not universal, and the operator is given by an
1370 -- expanded name, verify that the operand has an interpretation with a
1371 -- type defined in the given scope of the operator.
1373 function Type_In_P (Test : Kind_Test) return Entity_Id;
1374 -- Find a type of the given class in package Pack that contains the
1377 ---------------------------
1378 -- Operand_Type_In_Scope --
1379 ---------------------------
1381 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1382 Nod : constant Node_Id := Right_Opnd (Op_Node);
1387 if not Is_Overloaded (Nod) then
1388 return Scope (Base_Type (Etype (Nod))) = S;
1391 Get_First_Interp (Nod, I, It);
1392 while Present (It.Typ) loop
1393 if Scope (Base_Type (It.Typ)) = S then
1397 Get_Next_Interp (I, It);
1402 end Operand_Type_In_Scope;
1408 function Type_In_P (Test : Kind_Test) return Entity_Id is
1411 function In_Decl return Boolean;
1412 -- Verify that node is not part of the type declaration for the
1413 -- candidate type, which would otherwise be invisible.
1419 function In_Decl return Boolean is
1420 Decl_Node : constant Node_Id := Parent (E);
1426 if Etype (E) = Any_Type then
1429 elsif No (Decl_Node) then
1434 and then Nkind (N2) /= N_Compilation_Unit
1436 if N2 = Decl_Node then
1447 -- Start of processing for Type_In_P
1450 -- If the context type is declared in the prefix package, this is the
1451 -- desired base type.
1453 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1454 return Base_Type (Typ);
1457 E := First_Entity (Pack);
1458 while Present (E) loop
1459 if Test (E) and then not In_Decl then
1470 -- Start of processing for Make_Call_Into_Operator
1473 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1475 -- Ensure that the corresponding operator has the same parent as the
1476 -- original call. This guarantees that parent traversals performed by
1477 -- the ABE mechanism succeed.
1479 Set_Parent (Op_Node, Parent (N));
1484 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1485 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1486 Save_Interps (Act1, Left_Opnd (Op_Node));
1487 Save_Interps (Act2, Right_Opnd (Op_Node));
1488 Act1 := Left_Opnd (Op_Node);
1489 Act2 := Right_Opnd (Op_Node);
1494 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1495 Save_Interps (Act1, Right_Opnd (Op_Node));
1496 Act1 := Right_Opnd (Op_Node);
1499 -- If the operator is denoted by an expanded name, and the prefix is
1500 -- not Standard, but the operator is a predefined one whose scope is
1501 -- Standard, then this is an implicit_operator, inserted as an
1502 -- interpretation by the procedure of the same name. This procedure
1503 -- overestimates the presence of implicit operators, because it does
1504 -- not examine the type of the operands. Verify now that the operand
1505 -- type appears in the given scope. If right operand is universal,
1506 -- check the other operand. In the case of concatenation, either
1507 -- argument can be the component type, so check the type of the result.
1508 -- If both arguments are literals, look for a type of the right kind
1509 -- defined in the given scope. This elaborate nonsense is brought to
1510 -- you courtesy of b33302a. The type itself must be frozen, so we must
1511 -- find the type of the proper class in the given scope.
1513 -- A final wrinkle is the multiplication operator for fixed point types,
1514 -- which is defined in Standard only, and not in the scope of the
1515 -- fixed point type itself.
1517 if Nkind (Name (N)) = N_Expanded_Name then
1518 Pack := Entity (Prefix (Name (N)));
1520 -- If this is a package renaming, get renamed entity, which will be
1521 -- the scope of the operands if operaton is type-correct.
1523 if Present (Renamed_Entity (Pack)) then
1524 Pack := Renamed_Entity (Pack);
1527 -- If the entity being called is defined in the given package, it is
1528 -- a renaming of a predefined operator, and known to be legal.
1530 if Scope (Entity (Name (N))) = Pack
1531 and then Pack /= Standard_Standard
1535 -- Visibility does not need to be checked in an instance: if the
1536 -- operator was not visible in the generic it has been diagnosed
1537 -- already, else there is an implicit copy of it in the instance.
1539 elsif In_Instance then
1542 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1543 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1544 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1546 if Pack /= Standard_Standard then
1550 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1553 elsif Ada_Version >= Ada_2005
1554 and then Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1555 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1560 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1562 if Op_Name = Name_Op_Concat then
1563 Opnd_Type := Base_Type (Typ);
1565 elsif (Scope (Opnd_Type) = Standard_Standard
1567 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1569 and then not Comes_From_Source (Opnd_Type))
1571 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1574 if Scope (Opnd_Type) = Standard_Standard then
1576 -- Verify that the scope contains a type that corresponds to
1577 -- the given literal. Optimize the case where Pack is Standard.
1579 if Pack /= Standard_Standard then
1580 if Opnd_Type = Universal_Integer then
1581 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1583 elsif Opnd_Type = Universal_Real then
1584 Orig_Type := Type_In_P (Is_Real_Type'Access);
1586 elsif Opnd_Type = Any_String then
1587 Orig_Type := Type_In_P (Is_String_Type'Access);
1589 elsif Opnd_Type = Any_Access then
1590 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1592 elsif Opnd_Type = Any_Composite then
1593 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1595 if Present (Orig_Type) then
1596 if Has_Private_Component (Orig_Type) then
1599 Set_Etype (Act1, Orig_Type);
1602 Set_Etype (Act2, Orig_Type);
1611 Error := No (Orig_Type);
1614 elsif Ekind (Opnd_Type) = E_Allocator_Type
1615 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1619 -- If the type is defined elsewhere, and the operator is not
1620 -- defined in the given scope (by a renaming declaration, e.g.)
1621 -- then this is an error as well. If an extension of System is
1622 -- present, and the type may be defined there, Pack must be
1625 elsif Scope (Opnd_Type) /= Pack
1626 and then Scope (Op_Id) /= Pack
1627 and then (No (System_Aux_Id)
1628 or else Scope (Opnd_Type) /= System_Aux_Id
1629 or else Pack /= Scope (System_Aux_Id))
1631 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1634 Error := not Operand_Type_In_Scope (Pack);
1637 elsif Pack = Standard_Standard
1638 and then not Operand_Type_In_Scope (Standard_Standard)
1645 Error_Msg_Node_2 := Pack;
1647 ("& not declared in&", N, Selector_Name (Name (N)));
1648 Set_Etype (N, Any_Type);
1651 -- Detect a mismatch between the context type and the result type
1652 -- in the named package, which is otherwise not detected if the
1653 -- operands are universal. Check is only needed if source entity is
1654 -- an operator, not a function that renames an operator.
1656 elsif Nkind (Parent (N)) /= N_Type_Conversion
1657 and then Ekind (Entity (Name (N))) = E_Operator
1658 and then Is_Numeric_Type (Typ)
1659 and then not Is_Universal_Numeric_Type (Typ)
1660 and then Scope (Base_Type (Typ)) /= Pack
1661 and then not In_Instance
1663 if Is_Fixed_Point_Type (Typ)
1664 and then Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1666 -- Already checked above
1670 -- Operator may be defined in an extension of System
1672 elsif Present (System_Aux_Id)
1673 and then Present (Opnd_Type)
1674 and then Scope (Opnd_Type) = System_Aux_Id
1679 -- Could we use Wrong_Type here??? (this would require setting
1680 -- Etype (N) to the actual type found where Typ was expected).
1682 Error_Msg_NE ("expect }", N, Typ);
1687 Set_Chars (Op_Node, Op_Name);
1689 if not Is_Private_Type (Etype (N)) then
1690 Set_Etype (Op_Node, Base_Type (Etype (N)));
1692 Set_Etype (Op_Node, Etype (N));
1695 -- If this is a call to a function that renames a predefined equality,
1696 -- the renaming declaration provides a type that must be used to
1697 -- resolve the operands. This must be done now because resolution of
1698 -- the equality node will not resolve any remaining ambiguity, and it
1699 -- assumes that the first operand is not overloaded.
1701 if Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1702 and then Ekind (Func) = E_Function
1703 and then Is_Overloaded (Act1)
1705 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1706 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1709 Set_Entity (Op_Node, Op_Id);
1710 Generate_Reference (Op_Id, N, ' ');
1712 -- Do rewrite setting Comes_From_Source on the result if the original
1713 -- call came from source. Although it is not strictly the case that the
1714 -- operator as such comes from the source, logically it corresponds
1715 -- exactly to the function call in the source, so it should be marked
1716 -- this way (e.g. to make sure that validity checks work fine).
1719 CS : constant Boolean := Comes_From_Source (N);
1721 Rewrite (N, Op_Node);
1722 Set_Comes_From_Source (N, CS);
1725 -- If this is an arithmetic operator and the result type is private,
1726 -- the operands and the result must be wrapped in conversion to
1727 -- expose the underlying numeric type and expand the proper checks,
1728 -- e.g. on division.
1730 if Is_Private_Type (Typ) then
1740 Resolve_Intrinsic_Operator (N, Typ);
1746 Resolve_Intrinsic_Unary_Operator (N, Typ);
1755 -- If in ASIS_Mode, propagate operand types to original actuals of
1756 -- function call, which would otherwise not be fully resolved. If
1757 -- the call has already been constant-folded, nothing to do. We
1758 -- relocate the operand nodes rather than copy them, to preserve
1759 -- original_node pointers, given that the operands themselves may
1760 -- have been rewritten. If the call was itself a rewriting of an
1761 -- operator node, nothing to do.
1764 and then Nkind (N) in N_Op
1765 and then Nkind (Original_Node (N)) = N_Function_Call
1769 R : constant Node_Id := Right_Opnd (N);
1771 Old_First : constant Node_Id :=
1772 First (Parameter_Associations (Original_Node (N)));
1778 Old_Sec := Next (Old_First);
1780 -- If the original call has named associations, replace the
1781 -- explicit actual parameter in the association with the proper
1782 -- resolved operand.
1784 if Nkind (Old_First) = N_Parameter_Association then
1785 if Chars (Selector_Name (Old_First)) =
1786 Chars (First_Entity (Op_Id))
1788 Rewrite (Explicit_Actual_Parameter (Old_First),
1791 Rewrite (Explicit_Actual_Parameter (Old_First),
1796 Rewrite (Old_First, Relocate_Node (L));
1799 if Nkind (Old_Sec) = N_Parameter_Association then
1800 if Chars (Selector_Name (Old_Sec)) =
1801 Chars (First_Entity (Op_Id))
1803 Rewrite (Explicit_Actual_Parameter (Old_Sec),
1806 Rewrite (Explicit_Actual_Parameter (Old_Sec),
1811 Rewrite (Old_Sec, Relocate_Node (R));
1815 if Nkind (Old_First) = N_Parameter_Association then
1816 Rewrite (Explicit_Actual_Parameter (Old_First),
1819 Rewrite (Old_First, Relocate_Node (R));
1824 Set_Parent (Original_Node (N), Parent (N));
1826 end Make_Call_Into_Operator;
1832 function Operator_Kind
1834 Is_Binary : Boolean) return Node_Kind
1839 -- Use CASE statement or array???
1842 if Op_Name = Name_Op_And then
1844 elsif Op_Name = Name_Op_Or then
1846 elsif Op_Name = Name_Op_Xor then
1848 elsif Op_Name = Name_Op_Eq then
1850 elsif Op_Name = Name_Op_Ne then
1852 elsif Op_Name = Name_Op_Lt then
1854 elsif Op_Name = Name_Op_Le then
1856 elsif Op_Name = Name_Op_Gt then
1858 elsif Op_Name = Name_Op_Ge then
1860 elsif Op_Name = Name_Op_Add then
1862 elsif Op_Name = Name_Op_Subtract then
1863 Kind := N_Op_Subtract;
1864 elsif Op_Name = Name_Op_Concat then
1865 Kind := N_Op_Concat;
1866 elsif Op_Name = Name_Op_Multiply then
1867 Kind := N_Op_Multiply;
1868 elsif Op_Name = Name_Op_Divide then
1869 Kind := N_Op_Divide;
1870 elsif Op_Name = Name_Op_Mod then
1872 elsif Op_Name = Name_Op_Rem then
1874 elsif Op_Name = Name_Op_Expon then
1877 raise Program_Error;
1883 if Op_Name = Name_Op_Add then
1885 elsif Op_Name = Name_Op_Subtract then
1887 elsif Op_Name = Name_Op_Abs then
1889 elsif Op_Name = Name_Op_Not then
1892 raise Program_Error;
1899 ----------------------------
1900 -- Preanalyze_And_Resolve --
1901 ----------------------------
1903 procedure Preanalyze_And_Resolve
1906 With_Freezing : Boolean)
1908 Save_Full_Analysis : constant Boolean := Full_Analysis;
1909 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
1910 Save_Preanalysis_Count : constant Nat :=
1911 Inside_Preanalysis_Without_Freezing;
1913 pragma Assert (Nkind (N) in N_Subexpr);
1915 if not With_Freezing then
1916 Set_Must_Not_Freeze (N);
1917 Inside_Preanalysis_Without_Freezing :=
1918 Inside_Preanalysis_Without_Freezing + 1;
1921 Full_Analysis := False;
1922 Expander_Mode_Save_And_Set (False);
1924 -- Normally, we suppress all checks for this preanalysis. There is no
1925 -- point in processing them now, since they will be applied properly
1926 -- and in the proper location when the default expressions reanalyzed
1927 -- and reexpanded later on. We will also have more information at that
1928 -- point for possible suppression of individual checks.
1930 -- However, in SPARK mode, most expansion is suppressed, and this
1931 -- later reanalysis and reexpansion may not occur. SPARK mode does
1932 -- require the setting of checking flags for proof purposes, so we
1933 -- do the SPARK preanalysis without suppressing checks.
1935 -- This special handling for SPARK mode is required for example in the
1936 -- case of Ada 2012 constructs such as quantified expressions, which are
1937 -- expanded in two separate steps.
1939 if GNATprove_Mode then
1940 Analyze_And_Resolve (N, T);
1942 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1945 Expander_Mode_Restore;
1946 Full_Analysis := Save_Full_Analysis;
1947 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
1949 if not With_Freezing then
1950 Inside_Preanalysis_Without_Freezing :=
1951 Inside_Preanalysis_Without_Freezing - 1;
1955 (Inside_Preanalysis_Without_Freezing = Save_Preanalysis_Count);
1956 end Preanalyze_And_Resolve;
1958 ----------------------------
1959 -- Preanalyze_And_Resolve --
1960 ----------------------------
1962 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1964 Preanalyze_And_Resolve (N, T, With_Freezing => False);
1965 end Preanalyze_And_Resolve;
1967 -- Version without context type
1969 procedure Preanalyze_And_Resolve (N : Node_Id) is
1970 Save_Full_Analysis : constant Boolean := Full_Analysis;
1973 Full_Analysis := False;
1974 Expander_Mode_Save_And_Set (False);
1977 Resolve (N, Etype (N), Suppress => All_Checks);
1979 Expander_Mode_Restore;
1980 Full_Analysis := Save_Full_Analysis;
1981 end Preanalyze_And_Resolve;
1983 ------------------------------------------
1984 -- Preanalyze_With_Freezing_And_Resolve --
1985 ------------------------------------------
1987 procedure Preanalyze_With_Freezing_And_Resolve
1992 Preanalyze_And_Resolve (N, T, With_Freezing => True);
1993 end Preanalyze_With_Freezing_And_Resolve;
1995 ----------------------------------
1996 -- Replace_Actual_Discriminants --
1997 ----------------------------------
1999 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
2000 Loc : constant Source_Ptr := Sloc (N);
2001 Tsk : Node_Id := Empty;
2003 function Process_Discr (Nod : Node_Id) return Traverse_Result;
2004 -- Comment needed???
2010 function Process_Discr (Nod : Node_Id) return Traverse_Result is
2014 if Nkind (Nod) = N_Identifier then
2015 Ent := Entity (Nod);
2018 and then Ekind (Ent) = E_Discriminant
2021 Make_Selected_Component (Loc,
2022 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
2023 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
2025 Set_Etype (Nod, Etype (Ent));
2033 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
2035 -- Start of processing for Replace_Actual_Discriminants
2038 if Expander_Active then
2041 -- Allow the replacement of concurrent discriminants in GNATprove even
2042 -- though this is a light expansion activity. Note that generic units
2043 -- are not modified.
2045 elsif GNATprove_Mode and not Inside_A_Generic then
2052 if Nkind (Name (N)) = N_Selected_Component then
2053 Tsk := Prefix (Name (N));
2055 elsif Nkind (Name (N)) = N_Indexed_Component then
2056 Tsk := Prefix (Prefix (Name (N)));
2059 if Present (Tsk) then
2060 Replace_Discrs (Default);
2062 end Replace_Actual_Discriminants;
2068 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2069 Ambiguous : Boolean := False;
2070 Ctx_Type : Entity_Id := Typ;
2071 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2072 Err_Type : Entity_Id := Empty;
2073 Found : Boolean := False;
2076 I1 : Interp_Index := 0; -- prevent junk warning
2079 Seen : Entity_Id := Empty; -- prevent junk warning
2081 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2082 -- Determine whether a node comes from a predefined library unit or
2085 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2086 -- Try and fix up a literal so that it matches its expected type. New
2087 -- literals are manufactured if necessary to avoid cascaded errors.
2089 procedure Report_Ambiguous_Argument;
2090 -- Additional diagnostics when an ambiguous call has an ambiguous
2091 -- argument (typically a controlling actual).
2093 procedure Resolution_Failed;
2094 -- Called when attempt at resolving current expression fails
2096 ------------------------------------
2097 -- Comes_From_Predefined_Lib_Unit --
2098 -------------------------------------
2100 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2103 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2104 end Comes_From_Predefined_Lib_Unit;
2106 --------------------
2107 -- Patch_Up_Value --
2108 --------------------
2110 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2112 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2114 Make_Real_Literal (Sloc (N),
2115 Realval => UR_From_Uint (Intval (N))));
2116 Set_Etype (N, Universal_Real);
2117 Set_Is_Static_Expression (N);
2119 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2121 Make_Integer_Literal (Sloc (N),
2122 Intval => UR_To_Uint (Realval (N))));
2123 Set_Etype (N, Universal_Integer);
2124 Set_Is_Static_Expression (N);
2126 elsif Nkind (N) = N_String_Literal
2127 and then Is_Character_Type (Typ)
2129 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
2131 Make_Character_Literal (Sloc (N),
2133 Char_Literal_Value =>
2134 UI_From_Int (Character'Pos ('A'))));
2135 Set_Etype (N, Any_Character);
2136 Set_Is_Static_Expression (N);
2138 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2140 Make_String_Literal (Sloc (N),
2141 Strval => End_String));
2143 elsif Nkind (N) = N_Range then
2144 Patch_Up_Value (Low_Bound (N), Typ);
2145 Patch_Up_Value (High_Bound (N), Typ);
2149 -------------------------------
2150 -- Report_Ambiguous_Argument --
2151 -------------------------------
2153 procedure Report_Ambiguous_Argument is
2154 Arg : constant Node_Id := First (Parameter_Associations (N));
2159 if Nkind (Arg) = N_Function_Call
2160 and then Is_Entity_Name (Name (Arg))
2161 and then Is_Overloaded (Name (Arg))
2163 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2165 -- Could use comments on what is going on here???
2167 Get_First_Interp (Name (Arg), I, It);
2168 while Present (It.Nam) loop
2169 Error_Msg_Sloc := Sloc (It.Nam);
2171 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
2172 Error_Msg_N ("interpretation (inherited) #!", Arg);
2174 Error_Msg_N ("interpretation #!", Arg);
2177 Get_Next_Interp (I, It);
2180 end Report_Ambiguous_Argument;
2182 -----------------------
2183 -- Resolution_Failed --
2184 -----------------------
2186 procedure Resolution_Failed is
2188 Patch_Up_Value (N, Typ);
2190 -- Set the type to the desired one to minimize cascaded errors. Note
2191 -- that this is an approximation and does not work in all cases.
2195 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2196 Set_Is_Overloaded (N, False);
2198 -- The caller will return without calling the expander, so we need
2199 -- to set the analyzed flag. Note that it is fine to set Analyzed
2200 -- to True even if we are in the middle of a shallow analysis,
2201 -- (see the spec of sem for more details) since this is an error
2202 -- situation anyway, and there is no point in repeating the
2203 -- analysis later (indeed it won't work to repeat it later, since
2204 -- we haven't got a clear resolution of which entity is being
2207 Set_Analyzed (N, True);
2209 end Resolution_Failed;
2211 -- Start of processing for Resolve
2218 -- Access attribute on remote subprogram cannot be used for a non-remote
2219 -- access-to-subprogram type.
2221 if Nkind (N) = N_Attribute_Reference
2222 and then Nam_In (Attribute_Name (N), Name_Access,
2223 Name_Unrestricted_Access,
2224 Name_Unchecked_Access)
2225 and then Comes_From_Source (N)
2226 and then Is_Entity_Name (Prefix (N))
2227 and then Is_Subprogram (Entity (Prefix (N)))
2228 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2229 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2232 ("prefix must statically denote a non-remote subprogram", N);
2235 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2237 -- If the context is a Remote_Access_To_Subprogram, access attributes
2238 -- must be resolved with the corresponding fat pointer. There is no need
2239 -- to check for the attribute name since the return type of an
2240 -- attribute is never a remote type.
2242 if Nkind (N) = N_Attribute_Reference
2243 and then Comes_From_Source (N)
2244 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2247 Attr : constant Attribute_Id :=
2248 Get_Attribute_Id (Attribute_Name (N));
2249 Pref : constant Node_Id := Prefix (N);
2252 Is_Remote : Boolean := True;
2255 -- Check that Typ is a remote access-to-subprogram type
2257 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2259 -- Prefix (N) must statically denote a remote subprogram
2260 -- declared in a package specification.
2262 if Attr = Attribute_Access or else
2263 Attr = Attribute_Unchecked_Access or else
2264 Attr = Attribute_Unrestricted_Access
2266 Decl := Unit_Declaration_Node (Entity (Pref));
2268 if Nkind (Decl) = N_Subprogram_Body then
2269 Spec := Corresponding_Spec (Decl);
2271 if Present (Spec) then
2272 Decl := Unit_Declaration_Node (Spec);
2276 Spec := Parent (Decl);
2278 if not Is_Entity_Name (Prefix (N))
2279 or else Nkind (Spec) /= N_Package_Specification
2281 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2285 ("prefix must statically denote a remote subprogram ",
2289 -- If we are generating code in distributed mode, perform
2290 -- semantic checks against corresponding remote entities.
2293 and then Get_PCS_Name /= Name_No_DSA
2295 Check_Subtype_Conformant
2296 (New_Id => Entity (Prefix (N)),
2297 Old_Id => Designated_Type
2298 (Corresponding_Remote_Type (Typ)),
2302 Process_Remote_AST_Attribute (N, Typ);
2310 Debug_A_Entry ("resolving ", N);
2312 if Debug_Flag_V then
2313 Write_Overloads (N);
2316 if Comes_From_Source (N) then
2317 if Is_Fixed_Point_Type (Typ) then
2318 Check_Restriction (No_Fixed_Point, N);
2320 elsif Is_Floating_Point_Type (Typ)
2321 and then Typ /= Universal_Real
2322 and then Typ /= Any_Real
2324 Check_Restriction (No_Floating_Point, N);
2328 -- Return if already analyzed
2330 if Analyzed (N) then
2331 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2332 Analyze_Dimension (N);
2335 -- Any case of Any_Type as the Etype value means that we had a
2338 elsif Etype (N) = Any_Type then
2339 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2343 Check_Parameterless_Call (N);
2345 -- The resolution of an Expression_With_Actions is determined by
2348 if Nkind (N) = N_Expression_With_Actions then
2349 Resolve (Expression (N), Typ);
2352 Expr_Type := Etype (Expression (N));
2354 -- If not overloaded, then we know the type, and all that needs doing
2355 -- is to check that this type is compatible with the context.
2357 elsif not Is_Overloaded (N) then
2358 Found := Covers (Typ, Etype (N));
2359 Expr_Type := Etype (N);
2361 -- In the overloaded case, we must select the interpretation that
2362 -- is compatible with the context (i.e. the type passed to Resolve)
2365 -- Loop through possible interpretations
2367 Get_First_Interp (N, I, It);
2368 Interp_Loop : while Present (It.Typ) loop
2369 if Debug_Flag_V then
2370 Write_Str ("Interp: ");
2374 -- We are only interested in interpretations that are compatible
2375 -- with the expected type, any other interpretations are ignored.
2377 if not Covers (Typ, It.Typ) then
2378 if Debug_Flag_V then
2379 Write_Str (" interpretation incompatible with context");
2384 -- Skip the current interpretation if it is disabled by an
2385 -- abstract operator. This action is performed only when the
2386 -- type against which we are resolving is the same as the
2387 -- type of the interpretation.
2389 if Ada_Version >= Ada_2005
2390 and then It.Typ = Typ
2391 and then Typ /= Universal_Integer
2392 and then Typ /= Universal_Real
2393 and then Present (It.Abstract_Op)
2395 if Debug_Flag_V then
2396 Write_Line ("Skip.");
2402 -- First matching interpretation
2408 Expr_Type := It.Typ;
2410 -- Matching interpretation that is not the first, maybe an
2411 -- error, but there are some cases where preference rules are
2412 -- used to choose between the two possibilities. These and
2413 -- some more obscure cases are handled in Disambiguate.
2416 -- If the current statement is part of a predefined library
2417 -- unit, then all interpretations which come from user level
2418 -- packages should not be considered. Check previous and
2422 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2425 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2427 -- Previous interpretation must be discarded
2431 Expr_Type := It.Typ;
2432 Set_Entity (N, Seen);
2437 -- Otherwise apply further disambiguation steps
2439 Error_Msg_Sloc := Sloc (Seen);
2440 It1 := Disambiguate (N, I1, I, Typ);
2442 -- Disambiguation has succeeded. Skip the remaining
2445 if It1 /= No_Interp then
2447 Expr_Type := It1.Typ;
2449 while Present (It.Typ) loop
2450 Get_Next_Interp (I, It);
2454 -- Before we issue an ambiguity complaint, check for the
2455 -- case of a subprogram call where at least one of the
2456 -- arguments is Any_Type, and if so suppress the message,
2457 -- since it is a cascaded error. This can also happen for
2458 -- a generalized indexing operation.
2460 if Nkind (N) in N_Subprogram_Call
2461 or else (Nkind (N) = N_Indexed_Component
2462 and then Present (Generalized_Indexing (N)))
2469 if Nkind (N) = N_Indexed_Component then
2470 Rewrite (N, Generalized_Indexing (N));
2473 A := First_Actual (N);
2474 while Present (A) loop
2477 if Nkind (E) = N_Parameter_Association then
2478 E := Explicit_Actual_Parameter (E);
2481 if Etype (E) = Any_Type then
2482 if Debug_Flag_V then
2483 Write_Str ("Any_Type in call");
2494 elsif Nkind (N) in N_Binary_Op
2495 and then (Etype (Left_Opnd (N)) = Any_Type
2496 or else Etype (Right_Opnd (N)) = Any_Type)
2500 elsif Nkind (N) in N_Unary_Op
2501 and then Etype (Right_Opnd (N)) = Any_Type
2506 -- Not that special case, so issue message using the flag
2507 -- Ambiguous to control printing of the header message
2508 -- only at the start of an ambiguous set.
2510 if not Ambiguous then
2511 if Nkind (N) = N_Function_Call
2512 and then Nkind (Name (N)) = N_Explicit_Dereference
2515 ("ambiguous expression (cannot resolve indirect "
2518 Error_Msg_NE -- CODEFIX
2519 ("ambiguous expression (cannot resolve&)!",
2525 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2527 ("\\possible interpretation (inherited)#!", N);
2529 Error_Msg_N -- CODEFIX
2530 ("\\possible interpretation#!", N);
2533 if Nkind (N) in N_Subprogram_Call
2534 and then Present (Parameter_Associations (N))
2536 Report_Ambiguous_Argument;
2540 Error_Msg_Sloc := Sloc (It.Nam);
2542 -- By default, the error message refers to the candidate
2543 -- interpretation. But if it is a predefined operator, it
2544 -- is implicitly declared at the declaration of the type
2545 -- of the operand. Recover the sloc of that declaration
2546 -- for the error message.
2548 if Nkind (N) in N_Op
2549 and then Scope (It.Nam) = Standard_Standard
2550 and then not Is_Overloaded (Right_Opnd (N))
2551 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2554 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2556 if Comes_From_Source (Err_Type)
2557 and then Present (Parent (Err_Type))
2559 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2562 elsif Nkind (N) in N_Binary_Op
2563 and then Scope (It.Nam) = Standard_Standard
2564 and then not Is_Overloaded (Left_Opnd (N))
2565 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2568 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2570 if Comes_From_Source (Err_Type)
2571 and then Present (Parent (Err_Type))
2573 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2576 -- If this is an indirect call, use the subprogram_type
2577 -- in the message, to have a meaningful location. Also
2578 -- indicate if this is an inherited operation, created
2579 -- by a type declaration.
2581 elsif Nkind (N) = N_Function_Call
2582 and then Nkind (Name (N)) = N_Explicit_Dereference
2583 and then Is_Type (It.Nam)
2587 Sloc (Associated_Node_For_Itype (Err_Type));
2592 if Nkind (N) in N_Op
2593 and then Scope (It.Nam) = Standard_Standard
2594 and then Present (Err_Type)
2596 -- Special-case the message for universal_fixed
2597 -- operators, which are not declared with the type
2598 -- of the operand, but appear forever in Standard.
2600 if It.Typ = Universal_Fixed
2601 and then Scope (It.Nam) = Standard_Standard
2604 ("\\possible interpretation as universal_fixed "
2605 & "operation (RM 4.5.5 (19))", N);
2608 ("\\possible interpretation (predefined)#!", N);
2612 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2615 ("\\possible interpretation (inherited)#!", N);
2617 Error_Msg_N -- CODEFIX
2618 ("\\possible interpretation#!", N);
2624 -- We have a matching interpretation, Expr_Type is the type
2625 -- from this interpretation, and Seen is the entity.
2627 -- For an operator, just set the entity name. The type will be
2628 -- set by the specific operator resolution routine.
2630 if Nkind (N) in N_Op then
2631 Set_Entity (N, Seen);
2632 Generate_Reference (Seen, N);
2634 elsif Nkind_In (N, N_Case_Expression,
2635 N_Character_Literal,
2639 Set_Etype (N, Expr_Type);
2641 -- AI05-0139-2: Expression is overloaded because type has
2642 -- implicit dereference. The context may be the one that
2643 -- requires implicit dereferemce.
2645 elsif Has_Implicit_Dereference (Expr_Type) then
2646 Set_Etype (N, Expr_Type);
2647 Set_Is_Overloaded (N, False);
2649 -- If the expression is an entity, generate a reference
2650 -- to it, as this is not done for an overloaded construct
2653 if Is_Entity_Name (N)
2654 and then Comes_From_Source (N)
2656 Generate_Reference (Entity (N), N);
2658 -- Examine access discriminants of entity type,
2659 -- to check whether one of them yields the
2664 First_Discriminant (Etype (Entity (N)));
2667 while Present (Disc) loop
2668 exit when Is_Access_Type (Etype (Disc))
2669 and then Has_Implicit_Dereference (Disc)
2670 and then Designated_Type (Etype (Disc)) = Typ;
2672 Next_Discriminant (Disc);
2675 if Present (Disc) then
2676 Build_Explicit_Dereference (N, Disc);
2683 elsif Is_Overloaded (N)
2684 and then Present (It.Nam)
2685 and then Ekind (It.Nam) = E_Discriminant
2686 and then Has_Implicit_Dereference (It.Nam)
2688 -- If the node is a general indexing, the dereference is
2689 -- is inserted when resolving the rewritten form, else
2692 if Nkind (N) /= N_Indexed_Component
2693 or else No (Generalized_Indexing (N))
2695 Build_Explicit_Dereference (N, It.Nam);
2698 -- For an explicit dereference, attribute reference, range,
2699 -- short-circuit form (which is not an operator node), or call
2700 -- with a name that is an explicit dereference, there is
2701 -- nothing to be done at this point.
2703 elsif Nkind_In (N, N_Attribute_Reference,
2705 N_Explicit_Dereference,
2707 N_Indexed_Component,
2710 N_Selected_Component,
2712 or else Nkind (Name (N)) = N_Explicit_Dereference
2716 -- For procedure or function calls, set the type of the name,
2717 -- and also the entity pointer for the prefix.
2719 elsif Nkind (N) in N_Subprogram_Call
2720 and then Is_Entity_Name (Name (N))
2722 Set_Etype (Name (N), Expr_Type);
2723 Set_Entity (Name (N), Seen);
2724 Generate_Reference (Seen, Name (N));
2726 elsif Nkind (N) = N_Function_Call
2727 and then Nkind (Name (N)) = N_Selected_Component
2729 Set_Etype (Name (N), Expr_Type);
2730 Set_Entity (Selector_Name (Name (N)), Seen);
2731 Generate_Reference (Seen, Selector_Name (Name (N)));
2733 -- For all other cases, just set the type of the Name
2736 Set_Etype (Name (N), Expr_Type);
2743 -- Move to next interpretation
2745 exit Interp_Loop when No (It.Typ);
2747 Get_Next_Interp (I, It);
2748 end loop Interp_Loop;
2751 -- At this stage Found indicates whether or not an acceptable
2752 -- interpretation exists. If not, then we have an error, except that if
2753 -- the context is Any_Type as a result of some other error, then we
2754 -- suppress the error report.
2757 if Typ /= Any_Type then
2759 -- If type we are looking for is Void, then this is the procedure
2760 -- call case, and the error is simply that what we gave is not a
2761 -- procedure name (we think of procedure calls as expressions with
2762 -- types internally, but the user doesn't think of them this way).
2764 if Typ = Standard_Void_Type then
2766 -- Special case message if function used as a procedure
2768 if Nkind (N) = N_Procedure_Call_Statement
2769 and then Is_Entity_Name (Name (N))
2770 and then Ekind (Entity (Name (N))) = E_Function
2773 ("cannot use call to function & as a statement",
2774 Name (N), Entity (Name (N)));
2776 ("\return value of a function call cannot be ignored",
2779 -- Otherwise give general message (not clear what cases this
2780 -- covers, but no harm in providing for them).
2783 Error_Msg_N ("expect procedure name in procedure call", N);
2788 -- Otherwise we do have a subexpression with the wrong type
2790 -- Check for the case of an allocator which uses an access type
2791 -- instead of the designated type. This is a common error and we
2792 -- specialize the message, posting an error on the operand of the
2793 -- allocator, complaining that we expected the designated type of
2796 elsif Nkind (N) = N_Allocator
2797 and then Is_Access_Type (Typ)
2798 and then Is_Access_Type (Etype (N))
2799 and then Designated_Type (Etype (N)) = Typ
2801 Wrong_Type (Expression (N), Designated_Type (Typ));
2804 -- Check for view mismatch on Null in instances, for which the
2805 -- view-swapping mechanism has no identifier.
2807 elsif (In_Instance or else In_Inlined_Body)
2808 and then (Nkind (N) = N_Null)
2809 and then Is_Private_Type (Typ)
2810 and then Is_Access_Type (Full_View (Typ))
2812 Resolve (N, Full_View (Typ));
2816 -- Check for an aggregate. Sometimes we can get bogus aggregates
2817 -- from misuse of parentheses, and we are about to complain about
2818 -- the aggregate without even looking inside it.
2820 -- Instead, if we have an aggregate of type Any_Composite, then
2821 -- analyze and resolve the component fields, and then only issue
2822 -- another message if we get no errors doing this (otherwise
2823 -- assume that the errors in the aggregate caused the problem).
2825 elsif Nkind (N) = N_Aggregate
2826 and then Etype (N) = Any_Composite
2828 -- Disable expansion in any case. If there is a type mismatch
2829 -- it may be fatal to try to expand the aggregate. The flag
2830 -- would otherwise be set to false when the error is posted.
2832 Expander_Active := False;
2835 procedure Check_Aggr (Aggr : Node_Id);
2836 -- Check one aggregate, and set Found to True if we have a
2837 -- definite error in any of its elements
2839 procedure Check_Elmt (Aelmt : Node_Id);
2840 -- Check one element of aggregate and set Found to True if
2841 -- we definitely have an error in the element.
2847 procedure Check_Aggr (Aggr : Node_Id) is
2851 if Present (Expressions (Aggr)) then
2852 Elmt := First (Expressions (Aggr));
2853 while Present (Elmt) loop
2859 if Present (Component_Associations (Aggr)) then
2860 Elmt := First (Component_Associations (Aggr));
2861 while Present (Elmt) loop
2863 -- If this is a default-initialized component, then
2864 -- there is nothing to check. The box will be
2865 -- replaced by the appropriate call during late
2868 if Nkind (Elmt) /= N_Iterated_Component_Association
2869 and then not Box_Present (Elmt)
2871 Check_Elmt (Expression (Elmt));
2883 procedure Check_Elmt (Aelmt : Node_Id) is
2885 -- If we have a nested aggregate, go inside it (to
2886 -- attempt a naked analyze-resolve of the aggregate can
2887 -- cause undesirable cascaded errors). Do not resolve
2888 -- expression if it needs a type from context, as for
2889 -- integer * fixed expression.
2891 if Nkind (Aelmt) = N_Aggregate then
2897 if not Is_Overloaded (Aelmt)
2898 and then Etype (Aelmt) /= Any_Fixed
2903 if Etype (Aelmt) = Any_Type then
2914 -- Looks like we have a type error, but check for special case
2915 -- of Address wanted, integer found, with the configuration pragma
2916 -- Allow_Integer_Address active. If we have this case, introduce
2917 -- an unchecked conversion to allow the integer expression to be
2918 -- treated as an Address. The reverse case of integer wanted,
2919 -- Address found, is treated in an analogous manner.
2921 if Address_Integer_Convert_OK (Typ, Etype (N)) then
2922 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
2923 Analyze_And_Resolve (N, Typ);
2926 -- Under relaxed RM semantics silently replace occurrences of null
2927 -- by System.Address_Null.
2929 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
2930 Replace_Null_By_Null_Address (N);
2931 Analyze_And_Resolve (N, Typ);
2935 -- That special Allow_Integer_Address check did not apply, so we
2936 -- have a real type error. If an error message was issued already,
2937 -- Found got reset to True, so if it's still False, issue standard
2938 -- Wrong_Type message.
2941 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
2943 Subp_Name : Node_Id;
2946 if Is_Entity_Name (Name (N)) then
2947 Subp_Name := Name (N);
2949 elsif Nkind (Name (N)) = N_Selected_Component then
2951 -- Protected operation: retrieve operation name
2953 Subp_Name := Selector_Name (Name (N));
2956 raise Program_Error;
2959 Error_Msg_Node_2 := Typ;
2961 ("no visible interpretation of& matches expected type&",
2965 if All_Errors_Mode then
2967 Index : Interp_Index;
2971 Error_Msg_N ("\\possible interpretations:", N);
2973 Get_First_Interp (Name (N), Index, It);
2974 while Present (It.Nam) loop
2975 Error_Msg_Sloc := Sloc (It.Nam);
2976 Error_Msg_Node_2 := It.Nam;
2978 ("\\ type& for & declared#", N, It.Typ);
2979 Get_Next_Interp (Index, It);
2984 Error_Msg_N ("\use -gnatf for details", N);
2988 Wrong_Type (N, Typ);
2996 -- Test if we have more than one interpretation for the context
2998 elsif Ambiguous then
3002 -- Only one intepretation
3005 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3006 -- the "+" on T is abstract, and the operands are of universal type,
3007 -- the above code will have (incorrectly) resolved the "+" to the
3008 -- universal one in Standard. Therefore check for this case and give
3009 -- an error. We can't do this earlier, because it would cause legal
3010 -- cases to get errors (when some other type has an abstract "+").
3012 if Ada_Version >= Ada_2005
3013 and then Nkind (N) in N_Op
3014 and then Is_Overloaded (N)
3015 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3017 Get_First_Interp (N, I, It);
3018 while Present (It.Typ) loop
3019 if Present (It.Abstract_Op) and then
3020 Etype (It.Abstract_Op) = Typ
3023 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
3027 Get_Next_Interp (I, It);
3031 -- Here we have an acceptable interpretation for the context
3033 -- Propagate type information and normalize tree for various
3034 -- predefined operations. If the context only imposes a class of
3035 -- types, rather than a specific type, propagate the actual type
3038 if Typ = Any_Integer or else
3039 Typ = Any_Boolean or else
3040 Typ = Any_Modular or else
3041 Typ = Any_Real or else
3044 Ctx_Type := Expr_Type;
3046 -- Any_Fixed is legal in a real context only if a specific fixed-
3047 -- point type is imposed. If Norman Cohen can be confused by this,
3048 -- it deserves a separate message.
3051 and then Expr_Type = Any_Fixed
3053 Error_Msg_N ("illegal context for mixed mode operation", N);
3054 Set_Etype (N, Universal_Real);
3055 Ctx_Type := Universal_Real;
3059 -- A user-defined operator is transformed into a function call at
3060 -- this point, so that further processing knows that operators are
3061 -- really operators (i.e. are predefined operators). User-defined
3062 -- operators that are intrinsic are just renamings of the predefined
3063 -- ones, and need not be turned into calls either, but if they rename
3064 -- a different operator, we must transform the node accordingly.
3065 -- Instantiations of Unchecked_Conversion are intrinsic but are
3066 -- treated as functions, even if given an operator designator.
3068 if Nkind (N) in N_Op
3069 and then Present (Entity (N))
3070 and then Ekind (Entity (N)) /= E_Operator
3072 if not Is_Predefined_Op (Entity (N)) then
3073 Rewrite_Operator_As_Call (N, Entity (N));
3075 elsif Present (Alias (Entity (N)))
3077 Nkind (Parent (Parent (Entity (N)))) =
3078 N_Subprogram_Renaming_Declaration
3080 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3082 -- If the node is rewritten, it will be fully resolved in
3083 -- Rewrite_Renamed_Operator.
3085 if Analyzed (N) then
3091 case N_Subexpr'(Nkind (N)) is
3093 Resolve_Aggregate (N, Ctx_Type);
3096 Resolve_Allocator (N, Ctx_Type);
3098 when N_Short_Circuit =>
3099 Resolve_Short_Circuit (N, Ctx_Type);
3101 when N_Attribute_Reference =>
3102 Resolve_Attribute (N, Ctx_Type);
3104 when N_Case_Expression =>
3105 Resolve_Case_Expression (N, Ctx_Type);
3107 when N_Character_Literal =>
3108 Resolve_Character_Literal (N, Ctx_Type);
3110 when N_Delta_Aggregate =>
3111 Resolve_Delta_Aggregate (N, Ctx_Type);
3113 when N_Expanded_Name =>
3114 Resolve_Entity_Name (N, Ctx_Type);
3116 when N_Explicit_Dereference =>
3117 Resolve_Explicit_Dereference (N, Ctx_Type);
3119 when N_Expression_With_Actions =>
3120 Resolve_Expression_With_Actions (N, Ctx_Type);
3122 when N_Extension_Aggregate =>
3123 Resolve_Extension_Aggregate (N, Ctx_Type);
3125 when N_Function_Call =>
3126 Resolve_Call (N, Ctx_Type);
3128 when N_Identifier =>
3129 Resolve_Entity_Name (N, Ctx_Type);
3131 when N_If_Expression =>
3132 Resolve_If_Expression (N, Ctx_Type);
3134 when N_Indexed_Component =>
3135 Resolve_Indexed_Component (N, Ctx_Type);
3137 when N_Integer_Literal =>
3138 Resolve_Integer_Literal (N, Ctx_Type);
3140 when N_Membership_Test =>
3141 Resolve_Membership_Op (N, Ctx_Type);
3144 Resolve_Null (N, Ctx_Type);
3150 Resolve_Logical_Op (N, Ctx_Type);
3155 Resolve_Equality_Op (N, Ctx_Type);
3162 Resolve_Comparison_Op (N, Ctx_Type);
3165 Resolve_Op_Not (N, Ctx_Type);
3174 Resolve_Arithmetic_Op (N, Ctx_Type);
3177 Resolve_Op_Concat (N, Ctx_Type);
3180 Resolve_Op_Expon (N, Ctx_Type);
3186 Resolve_Unary_Op (N, Ctx_Type);
3189 Resolve_Shift (N, Ctx_Type);
3191 when N_Procedure_Call_Statement =>
3192 Resolve_Call (N, Ctx_Type);
3194 when N_Operator_Symbol =>
3195 Resolve_Operator_Symbol (N, Ctx_Type);
3197 when N_Qualified_Expression =>
3198 Resolve_Qualified_Expression (N, Ctx_Type);
3200 -- Why is the following null, needs a comment ???
3202 when N_Quantified_Expression =>
3205 when N_Raise_Expression =>
3206 Resolve_Raise_Expression (N, Ctx_Type);
3208 when N_Raise_xxx_Error =>
3209 Set_Etype (N, Ctx_Type);
3212 Resolve_Range (N, Ctx_Type);
3214 when N_Real_Literal =>
3215 Resolve_Real_Literal (N, Ctx_Type);
3218 Resolve_Reference (N, Ctx_Type);
3220 when N_Selected_Component =>
3221 Resolve_Selected_Component (N, Ctx_Type);
3224 Resolve_Slice (N, Ctx_Type);
3226 when N_String_Literal =>
3227 Resolve_String_Literal (N, Ctx_Type);
3229 when N_Target_Name =>
3230 Resolve_Target_Name (N, Ctx_Type);
3232 when N_Type_Conversion =>
3233 Resolve_Type_Conversion (N, Ctx_Type);
3235 when N_Unchecked_Expression =>
3236 Resolve_Unchecked_Expression (N, Ctx_Type);
3238 when N_Unchecked_Type_Conversion =>
3239 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3242 -- Mark relevant use-type and use-package clauses as effective using
3243 -- the original node because constant folding may have occured and
3244 -- removed references that need to be examined.
3246 if Nkind (Original_Node (N)) in N_Op then
3247 Mark_Use_Clauses (Original_Node (N));
3250 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3251 -- expression of an anonymous access type that occurs in the context
3252 -- of a named general access type, except when the expression is that
3253 -- of a membership test. This ensures proper legality checking in
3254 -- terms of allowed conversions (expressions that would be illegal to
3255 -- convert implicitly are allowed in membership tests).
3257 if Ada_Version >= Ada_2012
3258 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3259 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3260 and then Nkind (Parent (N)) not in N_Membership_Test
3262 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3263 Analyze_And_Resolve (N, Ctx_Type);
3266 -- If the subexpression was replaced by a non-subexpression, then
3267 -- all we do is to expand it. The only legitimate case we know of
3268 -- is converting procedure call statement to entry call statements,
3269 -- but there may be others, so we are making this test general.
3271 if Nkind (N) not in N_Subexpr then
3272 Debug_A_Exit ("resolving ", N, " (done)");
3277 -- The expression is definitely NOT overloaded at this point, so
3278 -- we reset the Is_Overloaded flag to avoid any confusion when
3279 -- reanalyzing the node.
3281 Set_Is_Overloaded (N, False);
3283 -- Freeze expression type, entity if it is a name, and designated
3284 -- type if it is an allocator (RM 13.14(10,11,13)).
3286 -- Now that the resolution of the type of the node is complete, and
3287 -- we did not detect an error, we can expand this node. We skip the
3288 -- expand call if we are in a default expression, see section
3289 -- "Handling of Default Expressions" in Sem spec.
3291 Debug_A_Exit ("resolving ", N, " (done)");
3293 -- We unconditionally freeze the expression, even if we are in
3294 -- default expression mode (the Freeze_Expression routine tests this
3295 -- flag and only freezes static types if it is set).
3297 -- Ada 2012 (AI05-177): The declaration of an expression function
3298 -- does not cause freezing, but we never reach here in that case.
3299 -- Here we are resolving the corresponding expanded body, so we do
3300 -- need to perform normal freezing.
3302 -- As elsewhere we do not emit freeze node within a generic. We make
3303 -- an exception for entities that are expressions, only to detect
3304 -- misuses of deferred constants and preserve the output of various
3307 if not Inside_A_Generic or else Is_Entity_Name (N) then
3308 Freeze_Expression (N);
3311 -- Now we can do the expansion
3321 -- Version with check(s) suppressed
3323 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3325 if Suppress = All_Checks then
3327 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3329 Scope_Suppress.Suppress := (others => True);
3331 Scope_Suppress.Suppress := Sva;
3336 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3338 Scope_Suppress.Suppress (Suppress) := True;
3340 Scope_Suppress.Suppress (Suppress) := Svg;
3349 -- Version with implicit type
3351 procedure Resolve (N : Node_Id) is
3353 Resolve (N, Etype (N));
3356 ---------------------
3357 -- Resolve_Actuals --
3358 ---------------------
3360 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3361 Loc : constant Source_Ptr := Sloc (N);
3364 A_Typ : Entity_Id := Empty; -- init to avoid warning
3367 Prev : Node_Id := Empty;
3369 Real_F : Entity_Id := Empty; -- init to avoid warning
3371 Real_Subp : Entity_Id;
3372 -- If the subprogram being called is an inherited operation for
3373 -- a formal derived type in an instance, Real_Subp is the subprogram
3374 -- that will be called. It may have different formal names than the
3375 -- operation of the formal in the generic, so after actual is resolved
3376 -- the name of the actual in a named association must carry the name
3377 -- of the actual of the subprogram being called.
3379 procedure Check_Aliased_Parameter;
3380 -- Check rules on aliased parameters and related accessibility rules
3381 -- in (RM 3.10.2 (10.2-10.4)).
3383 procedure Check_Argument_Order;
3384 -- Performs a check for the case where the actuals are all simple
3385 -- identifiers that correspond to the formal names, but in the wrong
3386 -- order, which is considered suspicious and cause for a warning.
3388 procedure Check_Prefixed_Call;
3389 -- If the original node is an overloaded call in prefix notation,
3390 -- insert an 'Access or a dereference as needed over the first actual.
3391 -- Try_Object_Operation has already verified that there is a valid
3392 -- interpretation, but the form of the actual can only be determined
3393 -- once the primitive operation is identified.
3395 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id);
3396 -- Emit an error concerning the illegal usage of an effectively volatile
3397 -- object in interfering context (SPARK RM 7.13(12)).
3399 procedure Insert_Default;
3400 -- If the actual is missing in a call, insert in the actuals list
3401 -- an instance of the default expression. The insertion is always
3402 -- a named association.
3404 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3405 -- Check whether T1 and T2, or their full views, are derived from a
3406 -- common type. Used to enforce the restrictions on array conversions
3409 function Static_Concatenation (N : Node_Id) return Boolean;
3410 -- Predicate to determine whether an actual that is a concatenation
3411 -- will be evaluated statically and does not need a transient scope.
3412 -- This must be determined before the actual is resolved and expanded
3413 -- because if needed the transient scope must be introduced earlier.
3415 -----------------------------
3416 -- Check_Aliased_Parameter --
3417 -----------------------------
3419 procedure Check_Aliased_Parameter is
3420 Nominal_Subt : Entity_Id;
3423 if Is_Aliased (F) then
3424 if Is_Tagged_Type (A_Typ) then
3427 elsif Is_Aliased_View (A) then
3428 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3429 Nominal_Subt := Base_Type (A_Typ);
3431 Nominal_Subt := A_Typ;
3434 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3437 -- In a generic body assume the worst for generic formals:
3438 -- they can have a constrained partial view (AI05-041).
3440 elsif Has_Discriminants (F_Typ)
3441 and then not Is_Constrained (F_Typ)
3442 and then not Has_Constrained_Partial_View (F_Typ)
3443 and then not Is_Generic_Type (F_Typ)
3448 Error_Msg_NE ("untagged actual does not match "
3449 & "aliased formal&", A, F);
3453 Error_Msg_NE ("actual for aliased formal& must be "
3454 & "aliased object", A, F);
3457 if Ekind (Nam) = E_Procedure then
3460 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3461 if Nkind (Parent (N)) = N_Type_Conversion
3462 and then Type_Access_Level (Etype (Parent (N))) <
3463 Object_Access_Level (A)
3465 Error_Msg_N ("aliased actual has wrong accessibility", A);
3468 elsif Nkind (Parent (N)) = N_Qualified_Expression
3469 and then Nkind (Parent (Parent (N))) = N_Allocator
3470 and then Type_Access_Level (Etype (Parent (Parent (N)))) <
3471 Object_Access_Level (A)
3474 ("aliased actual in allocator has wrong accessibility", A);
3477 end Check_Aliased_Parameter;
3479 --------------------------
3480 -- Check_Argument_Order --
3481 --------------------------
3483 procedure Check_Argument_Order is
3485 -- Nothing to do if no parameters, or original node is neither a
3486 -- function call nor a procedure call statement (happens in the
3487 -- operator-transformed-to-function call case), or the call is to an
3488 -- operator symbol (which is usually in infix form), or the call does
3489 -- not come from source, or this warning is off.
3491 if not Warn_On_Parameter_Order
3492 or else No (Parameter_Associations (N))
3493 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3494 or else (Nkind (Name (N)) = N_Identifier
3495 and then Present (Entity (Name (N)))
3496 and then Nkind (Entity (Name (N))) =
3497 N_Defining_Operator_Symbol)
3498 or else not Comes_From_Source (N)
3504 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3507 -- Nothing to do if only one parameter
3513 -- Here if at least two arguments
3516 Actuals : array (1 .. Nargs) of Node_Id;
3520 Wrong_Order : Boolean := False;
3521 -- Set True if an out of order case is found
3524 -- Collect identifier names of actuals, fail if any actual is
3525 -- not a simple identifier, and record max length of name.
3527 Actual := First (Parameter_Associations (N));
3528 for J in Actuals'Range loop
3529 if Nkind (Actual) /= N_Identifier then
3532 Actuals (J) := Actual;
3537 -- If we got this far, all actuals are identifiers and the list
3538 -- of their names is stored in the Actuals array.
3540 Formal := First_Formal (Nam);
3541 for J in Actuals'Range loop
3543 -- If we ran out of formals, that's odd, probably an error
3544 -- which will be detected elsewhere, but abandon the search.
3550 -- If name matches and is in order OK
3552 if Chars (Formal) = Chars (Actuals (J)) then
3556 -- If no match, see if it is elsewhere in list and if so
3557 -- flag potential wrong order if type is compatible.
3559 for K in Actuals'Range loop
3560 if Chars (Formal) = Chars (Actuals (K))
3562 Has_Compatible_Type (Actuals (K), Etype (Formal))
3564 Wrong_Order := True;
3574 <<Continue>> Next_Formal (Formal);
3577 -- If Formals left over, also probably an error, skip warning
3579 if Present (Formal) then
3583 -- Here we give the warning if something was out of order
3587 ("?P?actuals for this call may be in wrong order", N);
3591 end Check_Argument_Order;
3593 -------------------------
3594 -- Check_Prefixed_Call --
3595 -------------------------
3597 procedure Check_Prefixed_Call is
3598 Act : constant Node_Id := First_Actual (N);
3599 A_Type : constant Entity_Id := Etype (Act);
3600 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3601 Orig : constant Node_Id := Original_Node (N);
3605 -- Check whether the call is a prefixed call, with or without
3606 -- additional actuals.
3608 if Nkind (Orig) = N_Selected_Component
3610 (Nkind (Orig) = N_Indexed_Component
3611 and then Nkind (Prefix (Orig)) = N_Selected_Component
3612 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3613 and then Is_Entity_Name (Act)
3614 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3616 if Is_Access_Type (A_Type)
3617 and then not Is_Access_Type (F_Type)
3619 -- Introduce dereference on object in prefix
3622 Make_Explicit_Dereference (Sloc (Act),
3623 Prefix => Relocate_Node (Act));
3624 Rewrite (Act, New_A);
3627 elsif Is_Access_Type (F_Type)
3628 and then not Is_Access_Type (A_Type)
3630 -- Introduce an implicit 'Access in prefix
3632 if not Is_Aliased_View (Act) then
3634 ("object in prefixed call to& must be aliased "
3635 & "(RM 4.1.3 (13 1/2))",
3640 Make_Attribute_Reference (Loc,
3641 Attribute_Name => Name_Access,
3642 Prefix => Relocate_Node (Act)));
3647 end Check_Prefixed_Call;
3649 ---------------------------------------
3650 -- Flag_Effectively_Volatile_Objects --
3651 ---------------------------------------
3653 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id) is
3654 function Flag_Object (N : Node_Id) return Traverse_Result;
3655 -- Determine whether arbitrary node N denotes an effectively volatile
3656 -- object and if it does, emit an error.
3662 function Flag_Object (N : Node_Id) return Traverse_Result is
3666 -- Do not consider nested function calls because they have already
3667 -- been processed during their own resolution.
3669 if Nkind (N) = N_Function_Call then
3672 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
3676 and then Is_Effectively_Volatile (Id)
3677 and then (Async_Writers_Enabled (Id)
3678 or else Effective_Reads_Enabled (Id))
3681 ("volatile object cannot appear in this context (SPARK "
3682 & "RM 7.1.3(11))", N);
3690 procedure Flag_Objects is new Traverse_Proc (Flag_Object);
3692 -- Start of processing for Flag_Effectively_Volatile_Objects
3695 Flag_Objects (Expr);
3696 end Flag_Effectively_Volatile_Objects;
3698 --------------------
3699 -- Insert_Default --
3700 --------------------
3702 procedure Insert_Default is
3707 -- Missing argument in call, nothing to insert
3709 if No (Default_Value (F)) then
3713 -- Note that we do a full New_Copy_Tree, so that any associated
3714 -- Itypes are properly copied. This may not be needed any more,
3715 -- but it does no harm as a safety measure. Defaults of a generic
3716 -- formal may be out of bounds of the corresponding actual (see
3717 -- cc1311b) and an additional check may be required.
3722 New_Scope => Current_Scope,
3725 -- Propagate dimension information, if any.
3727 Copy_Dimensions (Default_Value (F), Actval);
3729 if Is_Concurrent_Type (Scope (Nam))
3730 and then Has_Discriminants (Scope (Nam))
3732 Replace_Actual_Discriminants (N, Actval);
3735 if Is_Overloadable (Nam)
3736 and then Present (Alias (Nam))
3738 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3739 and then not Is_Tagged_Type (Etype (F))
3741 -- If default is a real literal, do not introduce a
3742 -- conversion whose effect may depend on the run-time
3743 -- size of universal real.
3745 if Nkind (Actval) = N_Real_Literal then
3746 Set_Etype (Actval, Base_Type (Etype (F)));
3748 Actval := Unchecked_Convert_To (Etype (F), Actval);
3752 if Is_Scalar_Type (Etype (F)) then
3753 Enable_Range_Check (Actval);
3756 Set_Parent (Actval, N);
3758 -- Resolve aggregates with their base type, to avoid scope
3759 -- anomalies: the subtype was first built in the subprogram
3760 -- declaration, and the current call may be nested.
3762 if Nkind (Actval) = N_Aggregate then
3763 Analyze_And_Resolve (Actval, Etype (F));
3765 Analyze_And_Resolve (Actval, Etype (Actval));
3769 Set_Parent (Actval, N);
3771 -- See note above concerning aggregates
3773 if Nkind (Actval) = N_Aggregate
3774 and then Has_Discriminants (Etype (Actval))
3776 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3778 -- Resolve entities with their own type, which may differ from
3779 -- the type of a reference in a generic context (the view
3780 -- swapping mechanism did not anticipate the re-analysis of
3781 -- default values in calls).
3783 elsif Is_Entity_Name (Actval) then
3784 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3787 Analyze_And_Resolve (Actval, Etype (Actval));
3791 -- If default is a tag indeterminate function call, propagate tag
3792 -- to obtain proper dispatching.
3794 if Is_Controlling_Formal (F)
3795 and then Nkind (Default_Value (F)) = N_Function_Call
3797 Set_Is_Controlling_Actual (Actval);
3801 -- If the default expression raises constraint error, then just
3802 -- silently replace it with an N_Raise_Constraint_Error node, since
3803 -- we already gave the warning on the subprogram spec. If node is
3804 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3805 -- the warnings removal machinery.
3807 if Raises_Constraint_Error (Actval)
3808 and then Nkind (Actval) /= N_Raise_Constraint_Error
3811 Make_Raise_Constraint_Error (Loc,
3812 Reason => CE_Range_Check_Failed));
3814 Set_Raises_Constraint_Error (Actval);
3815 Set_Etype (Actval, Etype (F));
3819 Make_Parameter_Association (Loc,
3820 Explicit_Actual_Parameter => Actval,
3821 Selector_Name => Make_Identifier (Loc, Chars (F)));
3823 -- Case of insertion is first named actual
3826 or else Nkind (Parent (Prev)) /= N_Parameter_Association
3828 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3829 Set_First_Named_Actual (N, Actval);
3832 if No (Parameter_Associations (N)) then
3833 Set_Parameter_Associations (N, New_List (Assoc));
3835 Append (Assoc, Parameter_Associations (N));
3839 Insert_After (Prev, Assoc);
3842 -- Case of insertion is not first named actual
3845 Set_Next_Named_Actual
3846 (Assoc, Next_Named_Actual (Parent (Prev)));
3847 Set_Next_Named_Actual (Parent (Prev), Actval);
3848 Append (Assoc, Parameter_Associations (N));
3851 Mark_Rewrite_Insertion (Assoc);
3852 Mark_Rewrite_Insertion (Actval);
3861 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3862 FT1 : Entity_Id := T1;
3863 FT2 : Entity_Id := T2;
3866 if Is_Private_Type (T1)
3867 and then Present (Full_View (T1))
3869 FT1 := Full_View (T1);
3872 if Is_Private_Type (T2)
3873 and then Present (Full_View (T2))
3875 FT2 := Full_View (T2);
3878 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3881 --------------------------
3882 -- Static_Concatenation --
3883 --------------------------
3885 function Static_Concatenation (N : Node_Id) return Boolean is
3888 when N_String_Literal =>
3893 -- Concatenation is static when both operands are static and
3894 -- the concatenation operator is a predefined one.
3896 return Scope (Entity (N)) = Standard_Standard
3898 Static_Concatenation (Left_Opnd (N))
3900 Static_Concatenation (Right_Opnd (N));
3903 if Is_Entity_Name (N) then
3905 Ent : constant Entity_Id := Entity (N);
3907 return Ekind (Ent) = E_Constant
3908 and then Present (Constant_Value (Ent))
3910 Is_OK_Static_Expression (Constant_Value (Ent));
3917 end Static_Concatenation;
3919 -- Start of processing for Resolve_Actuals
3922 Check_Argument_Order;
3924 if Is_Overloadable (Nam)
3925 and then Is_Inherited_Operation (Nam)
3926 and then In_Instance
3927 and then Present (Alias (Nam))
3928 and then Present (Overridden_Operation (Alias (Nam)))
3930 Real_Subp := Alias (Nam);
3935 if Present (First_Actual (N)) then
3936 Check_Prefixed_Call;
3939 A := First_Actual (N);
3940 F := First_Formal (Nam);
3942 if Present (Real_Subp) then
3943 Real_F := First_Formal (Real_Subp);
3946 while Present (F) loop
3947 if No (A) and then Needs_No_Actuals (Nam) then
3950 -- If we have an error in any actual or formal, indicated by a type
3951 -- of Any_Type, then abandon resolution attempt, and set result type
3952 -- to Any_Type. Skip this if the actual is a Raise_Expression, whose
3953 -- type is imposed from context.
3955 elsif (Present (A) and then Etype (A) = Any_Type)
3956 or else Etype (F) = Any_Type
3958 if Nkind (A) /= N_Raise_Expression then
3959 Set_Etype (N, Any_Type);
3964 -- Case where actual is present
3966 -- If the actual is an entity, generate a reference to it now. We
3967 -- do this before the actual is resolved, because a formal of some
3968 -- protected subprogram, or a task discriminant, will be rewritten
3969 -- during expansion, and the source entity reference may be lost.
3972 and then Is_Entity_Name (A)
3973 and then Comes_From_Source (A)
3975 -- Annotate the tree by creating a variable reference marker when
3976 -- the actual denotes a variable reference, in case the reference
3977 -- is folded or optimized away. The variable reference marker is
3978 -- automatically saved for later examination by the ABE Processing
3979 -- phase. The status of the reference is set as follows:
3983 -- write IN OUT, OUT
3985 if Needs_Variable_Reference_Marker
3989 Build_Variable_Reference_Marker
3991 Read => Ekind (F) /= E_Out_Parameter,
3992 Write => Ekind (F) /= E_In_Parameter);
3995 Orig_A := Entity (A);
3997 if Present (Orig_A) then
3998 if Is_Formal (Orig_A)
3999 and then Ekind (F) /= E_In_Parameter
4001 Generate_Reference (Orig_A, A, 'm');
4003 elsif not Is_Overloaded (A) then
4004 if Ekind (F) /= E_Out_Parameter then
4005 Generate_Reference (Orig_A, A);
4007 -- RM 6.4.1(12): For an out parameter that is passed by
4008 -- copy, the formal parameter object is created, and:
4010 -- * For an access type, the formal parameter is initialized
4011 -- from the value of the actual, without checking that the
4012 -- value satisfies any constraint, any predicate, or any
4013 -- exclusion of the null value.
4015 -- * For a scalar type that has the Default_Value aspect
4016 -- specified, the formal parameter is initialized from the
4017 -- value of the actual, without checking that the value
4018 -- satisfies any constraint or any predicate.
4019 -- I do not understand why this case is included??? this is
4020 -- not a case where an OUT parameter is treated as IN OUT.
4022 -- * For a composite type with discriminants or that has
4023 -- implicit initial values for any subcomponents, the
4024 -- behavior is as for an in out parameter passed by copy.
4026 -- Hence for these cases we generate the read reference now
4027 -- (the write reference will be generated later by
4028 -- Note_Possible_Modification).
4030 elsif Is_By_Copy_Type (Etype (F))
4032 (Is_Access_Type (Etype (F))
4034 (Is_Scalar_Type (Etype (F))
4036 Present (Default_Aspect_Value (Etype (F))))
4038 (Is_Composite_Type (Etype (F))
4039 and then (Has_Discriminants (Etype (F))
4040 or else Is_Partially_Initialized_Type
4043 Generate_Reference (Orig_A, A);
4050 and then (Nkind (Parent (A)) /= N_Parameter_Association
4051 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4053 -- If style checking mode on, check match of formal name
4056 if Nkind (Parent (A)) = N_Parameter_Association then
4057 Check_Identifier (Selector_Name (Parent (A)), F);
4061 -- If the formal is Out or In_Out, do not resolve and expand the
4062 -- conversion, because it is subsequently expanded into explicit
4063 -- temporaries and assignments. However, the object of the
4064 -- conversion can be resolved. An exception is the case of tagged
4065 -- type conversion with a class-wide actual. In that case we want
4066 -- the tag check to occur and no temporary will be needed (no
4067 -- representation change can occur) and the parameter is passed by
4068 -- reference, so we go ahead and resolve the type conversion.
4069 -- Another exception is the case of reference to component or
4070 -- subcomponent of a bit-packed array, in which case we want to
4071 -- defer expansion to the point the in and out assignments are
4074 if Ekind (F) /= E_In_Parameter
4075 and then Nkind (A) = N_Type_Conversion
4076 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4077 and then not Is_Interface (Etype (A))
4079 if Ekind (F) = E_In_Out_Parameter
4080 and then Is_Array_Type (Etype (F))
4082 -- In a view conversion, the conversion must be legal in
4083 -- both directions, and thus both component types must be
4084 -- aliased, or neither (4.6 (8)).
4086 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
4087 -- the privacy requirement should not apply to generic
4088 -- types, and should be checked in an instance. ARG query
4091 if Has_Aliased_Components (Etype (Expression (A))) /=
4092 Has_Aliased_Components (Etype (F))
4095 ("both component types in a view conversion must be"
4096 & " aliased, or neither", A);
4098 -- Comment here??? what set of cases???
4101 not Same_Ancestor (Etype (F), Etype (Expression (A)))
4103 -- Check view conv between unrelated by ref array types
4105 if Is_By_Reference_Type (Etype (F))
4106 or else Is_By_Reference_Type (Etype (Expression (A)))
4109 ("view conversion between unrelated by reference "
4110 & "array types not allowed (\'A'I-00246)", A);
4112 -- In Ada 2005 mode, check view conversion component
4113 -- type cannot be private, tagged, or volatile. Note
4114 -- that we only apply this to source conversions. The
4115 -- generated code can contain conversions which are
4116 -- not subject to this test, and we cannot extract the
4117 -- component type in such cases since it is not present.
4119 elsif Comes_From_Source (A)
4120 and then Ada_Version >= Ada_2005
4123 Comp_Type : constant Entity_Id :=
4125 (Etype (Expression (A)));
4127 if (Is_Private_Type (Comp_Type)
4128 and then not Is_Generic_Type (Comp_Type))
4129 or else Is_Tagged_Type (Comp_Type)
4130 or else Is_Volatile (Comp_Type)
4133 ("component type of a view conversion cannot"
4134 & " be private, tagged, or volatile"
4143 -- Resolve expression if conversion is all OK
4145 if (Conversion_OK (A)
4146 or else Valid_Conversion (A, Etype (A), Expression (A)))
4147 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4149 Resolve (Expression (A));
4152 -- If the actual is a function call that returns a limited
4153 -- unconstrained object that needs finalization, create a
4154 -- transient scope for it, so that it can receive the proper
4155 -- finalization list.
4157 elsif Expander_Active
4158 and then Nkind (A) = N_Function_Call
4159 and then Is_Limited_Record (Etype (F))
4160 and then not Is_Constrained (Etype (F))
4161 and then (Needs_Finalization (Etype (F))
4162 or else Has_Task (Etype (F)))
4164 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4165 Resolve (A, Etype (F));
4167 -- A small optimization: if one of the actuals is a concatenation
4168 -- create a block around a procedure call to recover stack space.
4169 -- This alleviates stack usage when several procedure calls in
4170 -- the same statement list use concatenation. We do not perform
4171 -- this wrapping for code statements, where the argument is a
4172 -- static string, and we want to preserve warnings involving
4173 -- sequences of such statements.
4175 elsif Expander_Active
4176 and then Nkind (A) = N_Op_Concat
4177 and then Nkind (N) = N_Procedure_Call_Statement
4178 and then not (Is_Intrinsic_Subprogram (Nam)
4179 and then Chars (Nam) = Name_Asm)
4180 and then not Static_Concatenation (A)
4182 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4183 Resolve (A, Etype (F));
4186 if Nkind (A) = N_Type_Conversion
4187 and then Is_Array_Type (Etype (F))
4188 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4190 (Is_Limited_Type (Etype (F))
4191 or else Is_Limited_Type (Etype (Expression (A))))
4194 ("conversion between unrelated limited array types not "
4195 & "allowed ('A'I-00246)", A);
4197 if Is_Limited_Type (Etype (F)) then
4198 Explain_Limited_Type (Etype (F), A);
4201 if Is_Limited_Type (Etype (Expression (A))) then
4202 Explain_Limited_Type (Etype (Expression (A)), A);
4206 -- (Ada 2005: AI-251): If the actual is an allocator whose
4207 -- directly designated type is a class-wide interface, we build
4208 -- an anonymous access type to use it as the type of the
4209 -- allocator. Later, when the subprogram call is expanded, if
4210 -- the interface has a secondary dispatch table the expander
4211 -- will add a type conversion to force the correct displacement
4214 if Nkind (A) = N_Allocator then
4216 DDT : constant Entity_Id :=
4217 Directly_Designated_Type (Base_Type (Etype (F)));
4220 -- Displace the pointer to the object to reference its
4221 -- secondary dispatch table.
4223 if Is_Class_Wide_Type (DDT)
4224 and then Is_Interface (DDT)
4226 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4227 Analyze_And_Resolve (A, Etype (F),
4228 Suppress => Access_Check);
4231 -- Ada 2005, AI-162:If the actual is an allocator, the
4232 -- innermost enclosing statement is the master of the
4233 -- created object. This needs to be done with expansion
4234 -- enabled only, otherwise the transient scope will not
4235 -- be removed in the expansion of the wrapped construct.
4238 and then (Needs_Finalization (DDT)
4239 or else Has_Task (DDT))
4241 Establish_Transient_Scope
4242 (A, Manage_Sec_Stack => False);
4246 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4247 Check_Restriction (No_Access_Parameter_Allocators, A);
4251 -- (Ada 2005): The call may be to a primitive operation of a
4252 -- tagged synchronized type, declared outside of the type. In
4253 -- this case the controlling actual must be converted to its
4254 -- corresponding record type, which is the formal type. The
4255 -- actual may be a subtype, either because of a constraint or
4256 -- because it is a generic actual, so use base type to locate
4259 F_Typ := Base_Type (Etype (F));
4261 if Is_Tagged_Type (F_Typ)
4262 and then (Is_Concurrent_Type (F_Typ)
4263 or else Is_Concurrent_Record_Type (F_Typ))
4265 -- If the actual is overloaded, look for an interpretation
4266 -- that has a synchronized type.
4268 if not Is_Overloaded (A) then
4269 A_Typ := Base_Type (Etype (A));
4273 Index : Interp_Index;
4277 Get_First_Interp (A, Index, It);
4278 while Present (It.Typ) loop
4279 if Is_Concurrent_Type (It.Typ)
4280 or else Is_Concurrent_Record_Type (It.Typ)
4282 A_Typ := Base_Type (It.Typ);
4286 Get_Next_Interp (Index, It);
4292 Full_A_Typ : Entity_Id;
4295 if Present (Full_View (A_Typ)) then
4296 Full_A_Typ := Base_Type (Full_View (A_Typ));
4298 Full_A_Typ := A_Typ;
4301 -- Tagged synchronized type (case 1): the actual is a
4304 if Is_Concurrent_Type (A_Typ)
4305 and then Corresponding_Record_Type (A_Typ) = F_Typ
4308 Unchecked_Convert_To
4309 (Corresponding_Record_Type (A_Typ), A));
4310 Resolve (A, Etype (F));
4312 -- Tagged synchronized type (case 2): the formal is a
4315 elsif Ekind (Full_A_Typ) = E_Record_Type
4317 (Corresponding_Concurrent_Type (Full_A_Typ))
4318 and then Is_Concurrent_Type (F_Typ)
4319 and then Present (Corresponding_Record_Type (F_Typ))
4320 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4322 Resolve (A, Corresponding_Record_Type (F_Typ));
4327 Resolve (A, Etype (F));
4331 -- Not a synchronized operation
4334 Resolve (A, Etype (F));
4341 -- An actual cannot be an untagged formal incomplete type
4343 if Ekind (A_Typ) = E_Incomplete_Type
4344 and then not Is_Tagged_Type (A_Typ)
4345 and then Is_Generic_Type (A_Typ)
4348 ("invalid use of untagged formal incomplete type", A);
4351 if Comes_From_Source (Original_Node (N))
4352 and then Nkind_In (Original_Node (N), N_Function_Call,
4353 N_Procedure_Call_Statement)
4355 -- In formal mode, check that actual parameters matching
4356 -- formals of tagged types are objects (or ancestor type
4357 -- conversions of objects), not general expressions.
4359 if Is_Actual_Tagged_Parameter (A) then
4360 if Is_SPARK_05_Object_Reference (A) then
4363 elsif Nkind (A) = N_Type_Conversion then
4365 Operand : constant Node_Id := Expression (A);
4366 Operand_Typ : constant Entity_Id := Etype (Operand);
4367 Target_Typ : constant Entity_Id := A_Typ;
4370 if not Is_SPARK_05_Object_Reference (Operand) then
4371 Check_SPARK_05_Restriction
4372 ("object required", Operand);
4374 -- In formal mode, the only view conversions are those
4375 -- involving ancestor conversion of an extended type.
4378 (Is_Tagged_Type (Target_Typ)
4379 and then not Is_Class_Wide_Type (Target_Typ)
4380 and then Is_Tagged_Type (Operand_Typ)
4381 and then not Is_Class_Wide_Type (Operand_Typ)
4382 and then Is_Ancestor (Target_Typ, Operand_Typ))
4385 (F, E_Out_Parameter, E_In_Out_Parameter)
4387 Check_SPARK_05_Restriction
4388 ("ancestor conversion is the only permitted "
4389 & "view conversion", A);
4391 Check_SPARK_05_Restriction
4392 ("ancestor conversion required", A);
4401 Check_SPARK_05_Restriction ("object required", A);
4404 -- In formal mode, the only view conversions are those
4405 -- involving ancestor conversion of an extended type.
4407 elsif Nkind (A) = N_Type_Conversion
4408 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
4410 Check_SPARK_05_Restriction
4411 ("ancestor conversion is the only permitted view "
4416 -- has warnings suppressed, then we reset Never_Set_In_Source for
4417 -- the calling entity. The reason for this is to catch cases like
4418 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4419 -- uses trickery to modify an IN parameter.
4421 if Ekind (F) = E_In_Parameter
4422 and then Is_Entity_Name (A)
4423 and then Present (Entity (A))
4424 and then Ekind (Entity (A)) = E_Variable
4425 and then Has_Warnings_Off (F_Typ)
4427 Set_Never_Set_In_Source (Entity (A), False);
4430 -- Perform error checks for IN and IN OUT parameters
4432 if Ekind (F) /= E_Out_Parameter then
4434 -- Check unset reference. For scalar parameters, it is clearly
4435 -- wrong to pass an uninitialized value as either an IN or
4436 -- IN-OUT parameter. For composites, it is also clearly an
4437 -- error to pass a completely uninitialized value as an IN
4438 -- parameter, but the case of IN OUT is trickier. We prefer
4439 -- not to give a warning here. For example, suppose there is
4440 -- a routine that sets some component of a record to False.
4441 -- It is perfectly reasonable to make this IN-OUT and allow
4442 -- either initialized or uninitialized records to be passed
4445 -- For partially initialized composite values, we also avoid
4446 -- warnings, since it is quite likely that we are passing a
4447 -- partially initialized value and only the initialized fields
4448 -- will in fact be read in the subprogram.
4450 if Is_Scalar_Type (A_Typ)
4451 or else (Ekind (F) = E_In_Parameter
4452 and then not Is_Partially_Initialized_Type (A_Typ))
4454 Check_Unset_Reference (A);
4457 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4458 -- actual to a nested call, since this constitutes a reading of
4459 -- the parameter, which is not allowed.
4461 if Ada_Version = Ada_83
4462 and then Is_Entity_Name (A)
4463 and then Ekind (Entity (A)) = E_Out_Parameter
4465 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4469 -- In -gnatd.q mode, forget that a given array is constant when
4470 -- it is passed as an IN parameter to a foreign-convention
4471 -- subprogram. This is in case the subprogram evilly modifies the
4472 -- object. Of course, correct code would use IN OUT.
4475 and then Ekind (F) = E_In_Parameter
4476 and then Has_Foreign_Convention (Nam)
4477 and then Is_Array_Type (F_Typ)
4478 and then Nkind (A) in N_Has_Entity
4479 and then Present (Entity (A))
4481 Set_Is_True_Constant (Entity (A), False);
4484 -- Case of OUT or IN OUT parameter
4486 if Ekind (F) /= E_In_Parameter then
4488 -- For an Out parameter, check for useless assignment. Note
4489 -- that we can't set Last_Assignment this early, because we may
4490 -- kill current values in Resolve_Call, and that call would
4491 -- clobber the Last_Assignment field.
4493 -- Note: call Warn_On_Useless_Assignment before doing the check
4494 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4495 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4496 -- reflects the last assignment, not this one.
4498 if Ekind (F) = E_Out_Parameter then
4499 if Warn_On_Modified_As_Out_Parameter (F)
4500 and then Is_Entity_Name (A)
4501 and then Present (Entity (A))
4502 and then Comes_From_Source (N)
4504 Warn_On_Useless_Assignment (Entity (A), A);
4508 -- Validate the form of the actual. Note that the call to
4509 -- Is_OK_Variable_For_Out_Formal generates the required
4510 -- reference in this case.
4512 -- A call to an initialization procedure for an aggregate
4513 -- component may initialize a nested component of a constant
4514 -- designated object. In this context the object is variable.
4516 if not Is_OK_Variable_For_Out_Formal (A)
4517 and then not Is_Init_Proc (Nam)
4519 Error_Msg_NE ("actual for& must be a variable", A, F);
4521 if Is_Subprogram (Current_Scope) then
4522 if Is_Invariant_Procedure (Current_Scope)
4523 or else Is_Partial_Invariant_Procedure (Current_Scope)
4526 ("function used in invariant cannot modify its "
4529 elsif Is_Predicate_Function (Current_Scope) then
4531 ("function used in predicate cannot modify its "
4537 -- What's the following about???
4539 if Is_Entity_Name (A) then
4540 Kill_Checks (Entity (A));
4546 if A_Typ = Any_Type then
4547 Set_Etype (N, Any_Type);
4551 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4553 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
4555 -- Apply predicate tests except in certain special cases. Note
4556 -- that it might be more consistent to apply these only when
4557 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4558 -- for the outbound predicate tests ??? In any case indicate
4559 -- the function being called, for better warnings if the call
4560 -- leads to an infinite recursion.
4562 if Predicate_Tests_On_Arguments (Nam) then
4563 Apply_Predicate_Check (A, F_Typ, Nam);
4566 -- Apply required constraint checks
4568 if Is_Scalar_Type (A_Typ) then
4569 Apply_Scalar_Range_Check (A, F_Typ);
4571 elsif Is_Array_Type (A_Typ) then
4572 Apply_Length_Check (A, F_Typ);
4574 elsif Is_Record_Type (F_Typ)
4575 and then Has_Discriminants (F_Typ)
4576 and then Is_Constrained (F_Typ)
4577 and then (not Is_Derived_Type (F_Typ)
4578 or else Comes_From_Source (Nam))
4580 Apply_Discriminant_Check (A, F_Typ);
4582 -- For view conversions of a discriminated object, apply
4583 -- check to object itself, the conversion alreay has the
4586 if Nkind (A) = N_Type_Conversion
4587 and then Is_Constrained (Etype (Expression (A)))
4589 Apply_Discriminant_Check (Expression (A), F_Typ);
4592 elsif Is_Access_Type (F_Typ)
4593 and then Is_Array_Type (Designated_Type (F_Typ))
4594 and then Is_Constrained (Designated_Type (F_Typ))
4596 Apply_Length_Check (A, F_Typ);
4598 elsif Is_Access_Type (F_Typ)
4599 and then Has_Discriminants (Designated_Type (F_Typ))
4600 and then Is_Constrained (Designated_Type (F_Typ))
4602 Apply_Discriminant_Check (A, F_Typ);
4605 Apply_Range_Check (A, F_Typ);
4608 -- Ada 2005 (AI-231): Note that the controlling parameter case
4609 -- already existed in Ada 95, which is partially checked
4610 -- elsewhere (see Checks), and we don't want the warning
4611 -- message to differ.
4613 if Is_Access_Type (F_Typ)
4614 and then Can_Never_Be_Null (F_Typ)
4615 and then Known_Null (A)
4617 if Is_Controlling_Formal (F) then
4618 Apply_Compile_Time_Constraint_Error
4620 Msg => "null value not allowed here??",
4621 Reason => CE_Access_Check_Failed);
4623 elsif Ada_Version >= Ada_2005 then
4624 Apply_Compile_Time_Constraint_Error
4626 Msg => "(Ada 2005) null not allowed in "
4627 & "null-excluding formal??",
4628 Reason => CE_Null_Not_Allowed);
4633 -- Checks for OUT parameters and IN OUT parameters
4635 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
4637 -- If there is a type conversion, make sure the return value
4638 -- meets the constraints of the variable before the conversion.
4640 if Nkind (A) = N_Type_Conversion then
4641 if Is_Scalar_Type (A_Typ) then
4643 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4644 -- which would prevent the check from being generated.
4645 -- This is for Starlet only though, so long obsolete.
4647 if Mechanism (F) = By_Reference
4648 and then Is_Valued_Procedure (Nam)
4652 Apply_Scalar_Range_Check
4653 (Expression (A), Etype (Expression (A)), A_Typ);
4656 -- In addition the return value must meet the constraints
4657 -- of the object type (see the comment below).
4659 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4663 (Expression (A), Etype (Expression (A)), A_Typ);
4666 -- If no conversion, apply scalar range checks and length check
4667 -- based on the subtype of the actual (NOT that of the formal).
4668 -- This indicates that the check takes place on return from the
4669 -- call. During expansion the required constraint checks are
4670 -- inserted. In GNATprove mode, in the absence of expansion,
4671 -- the flag indicates that the returned value is valid.
4674 if Is_Scalar_Type (F_Typ) then
4675 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4677 elsif Is_Array_Type (F_Typ)
4678 and then Ekind (F) = E_Out_Parameter
4680 Apply_Length_Check (A, F_Typ);
4683 Apply_Range_Check (A, A_Typ, F_Typ);
4687 -- Note: we do not apply the predicate checks for the case of
4688 -- OUT and IN OUT parameters. They are instead applied in the
4689 -- Expand_Actuals routine in Exp_Ch6.
4692 -- An actual associated with an access parameter is implicitly
4693 -- converted to the anonymous access type of the formal and must
4694 -- satisfy the legality checks for access conversions.
4696 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4697 if not Valid_Conversion (A, F_Typ, A) then
4699 ("invalid implicit conversion for access parameter", A);
4702 -- If the actual is an access selected component of a variable,
4703 -- the call may modify its designated object. It is reasonable
4704 -- to treat this as a potential modification of the enclosing
4705 -- record, to prevent spurious warnings that it should be
4706 -- declared as a constant, because intuitively programmers
4707 -- regard the designated subcomponent as part of the record.
4709 if Nkind (A) = N_Selected_Component
4710 and then Is_Entity_Name (Prefix (A))
4711 and then not Is_Constant_Object (Entity (Prefix (A)))
4713 Note_Possible_Modification (A, Sure => False);
4717 -- Check illegal cases of atomic/volatile actual (RM C.6(12,13))
4719 if (Is_By_Reference_Type (Etype (F)) or else Is_Aliased (F))
4720 and then Comes_From_Source (N)
4722 if Is_Atomic_Object (A)
4723 and then not Is_Atomic (Etype (F))
4726 ("cannot pass atomic object to nonatomic formal&",
4729 ("\which is passed by reference (RM C.6(12))", A);
4731 elsif Is_Volatile_Object (A)
4732 and then not Is_Volatile (Etype (F))
4735 ("cannot pass volatile object to nonvolatile formal&",
4738 ("\which is passed by reference (RM C.6(12))", A);
4741 if Ada_Version >= Ada_2020
4742 and then Is_Subcomponent_Of_Atomic_Object (A)
4743 and then not Is_Atomic_Object (A)
4746 ("cannot pass nonatomic subcomponent of atomic object",
4749 ("\to formal & which is passed by reference (RM C.6(13))",
4754 -- Check that subprograms don't have improper controlling
4755 -- arguments (RM 3.9.2 (9)).
4757 -- A primitive operation may have an access parameter of an
4758 -- incomplete tagged type, but a dispatching call is illegal
4759 -- if the type is still incomplete.
4761 if Is_Controlling_Formal (F) then
4762 Set_Is_Controlling_Actual (A);
4764 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4766 Desig : constant Entity_Id := Designated_Type (Etype (F));
4768 if Ekind (Desig) = E_Incomplete_Type
4769 and then No (Full_View (Desig))
4770 and then No (Non_Limited_View (Desig))
4773 ("premature use of incomplete type& "
4774 & "in dispatching call", A, Desig);
4779 elsif Nkind (A) = N_Explicit_Dereference then
4780 Validate_Remote_Access_To_Class_Wide_Type (A);
4783 -- Apply legality rule 3.9.2 (9/1)
4785 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4786 and then not Is_Class_Wide_Type (F_Typ)
4787 and then not Is_Controlling_Formal (F)
4788 and then not In_Instance
4790 Error_Msg_N ("class-wide argument not allowed here!", A);
4792 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4793 Error_Msg_Node_2 := F_Typ;
4795 ("& is not a dispatching operation of &!", A, Nam);
4798 -- Apply the checks described in 3.10.2(27): if the context is a
4799 -- specific access-to-object, the actual cannot be class-wide.
4800 -- Use base type to exclude access_to_subprogram cases.
4802 elsif Is_Access_Type (A_Typ)
4803 and then Is_Access_Type (F_Typ)
4804 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4805 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4806 or else (Nkind (A) = N_Attribute_Reference
4808 Is_Class_Wide_Type (Etype (Prefix (A)))))
4809 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4810 and then not Is_Controlling_Formal (F)
4812 -- Disable these checks for call to imported C++ subprograms
4815 (Is_Entity_Name (Name (N))
4816 and then Is_Imported (Entity (Name (N)))
4817 and then Convention (Entity (Name (N))) = Convention_CPP)
4820 ("access to class-wide argument not allowed here!", A);
4822 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4823 Error_Msg_Node_2 := Designated_Type (F_Typ);
4825 ("& is not a dispatching operation of &!", A, Nam);
4829 Check_Aliased_Parameter;
4833 -- If it is a named association, treat the selector_name as a
4834 -- proper identifier, and mark the corresponding entity.
4836 if Nkind (Parent (A)) = N_Parameter_Association
4838 -- Ignore reference in SPARK mode, as it refers to an entity not
4839 -- in scope at the point of reference, so the reference should
4840 -- be ignored for computing effects of subprograms.
4842 and then not GNATprove_Mode
4844 -- If subprogram is overridden, use name of formal that
4847 if Present (Real_Subp) then
4848 Set_Entity (Selector_Name (Parent (A)), Real_F);
4849 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
4852 Set_Entity (Selector_Name (Parent (A)), F);
4853 Generate_Reference (F, Selector_Name (Parent (A)));
4854 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4855 Generate_Reference (F_Typ, N, ' ');
4861 if Ekind (F) /= E_Out_Parameter then
4862 Check_Unset_Reference (A);
4865 -- The following checks are only relevant when SPARK_Mode is on as
4866 -- they are not standard Ada legality rule. Internally generated
4867 -- temporaries are ignored.
4869 if SPARK_Mode = On and then Comes_From_Source (A) then
4871 -- An effectively volatile object may act as an actual when the
4872 -- corresponding formal is of a non-scalar effectively volatile
4873 -- type (SPARK RM 7.1.3(11)).
4875 if not Is_Scalar_Type (Etype (F))
4876 and then Is_Effectively_Volatile (Etype (F))
4880 -- An effectively volatile object may act as an actual in a
4881 -- call to an instance of Unchecked_Conversion.
4882 -- (SPARK RM 7.1.3(11)).
4884 elsif Is_Unchecked_Conversion_Instance (Nam) then
4887 -- The actual denotes an object
4889 elsif Is_Effectively_Volatile_Object (A) then
4891 ("volatile object cannot act as actual in a call (SPARK "
4892 & "RM 7.1.3(11))", A);
4894 -- Otherwise the actual denotes an expression. Inspect the
4895 -- expression and flag each effectively volatile object with
4896 -- enabled property Async_Writers or Effective_Reads as illegal
4897 -- because it apprears within an interfering context. Note that
4898 -- this is usually done in Resolve_Entity_Name, but when the
4899 -- effectively volatile object appears as an actual in a call,
4900 -- the call must be resolved first.
4903 Flag_Effectively_Volatile_Objects (A);
4906 -- An effectively volatile variable cannot act as an actual
4907 -- parameter in a procedure call when the variable has enabled
4908 -- property Effective_Reads and the corresponding formal is of
4909 -- mode IN (SPARK RM 7.1.3(10)).
4911 if Ekind (Nam) = E_Procedure
4912 and then Ekind (F) = E_In_Parameter
4913 and then Is_Entity_Name (A)
4917 if Ekind (A_Id) = E_Variable
4918 and then Is_Effectively_Volatile (Etype (A_Id))
4919 and then Effective_Reads_Enabled (A_Id)
4922 ("effectively volatile variable & cannot appear as "
4923 & "actual in procedure call", A, A_Id);
4925 Error_Msg_Name_1 := Name_Effective_Reads;
4926 Error_Msg_N ("\\variable has enabled property %", A);
4927 Error_Msg_N ("\\corresponding formal has mode IN", A);
4932 -- A formal parameter of a specific tagged type whose related
4933 -- subprogram is subject to pragma Extensions_Visible with value
4934 -- "False" cannot act as an actual in a subprogram with value
4935 -- "True" (SPARK RM 6.1.7(3)).
4937 if Is_EVF_Expression (A)
4938 and then Extensions_Visible_Status (Nam) =
4939 Extensions_Visible_True
4942 ("formal parameter cannot act as actual parameter when "
4943 & "Extensions_Visible is False", A);
4945 ("\subprogram & has Extensions_Visible True", A, Nam);
4948 -- The actual parameter of a Ghost subprogram whose formal is of
4949 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
4951 if Comes_From_Source (Nam)
4952 and then Is_Ghost_Entity (Nam)
4953 and then Ekind_In (F, E_In_Out_Parameter, E_Out_Parameter)
4954 and then Is_Entity_Name (A)
4955 and then Present (Entity (A))
4956 and then not Is_Ghost_Entity (Entity (A))
4959 ("non-ghost variable & cannot appear as actual in call to "
4960 & "ghost procedure", A, Entity (A));
4962 if Ekind (F) = E_In_Out_Parameter then
4963 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
4965 Error_Msg_N ("\corresponding formal has mode OUT", A);
4971 -- Case where actual is not present
4979 if Present (Real_Subp) then
4980 Next_Formal (Real_F);
4983 end Resolve_Actuals;
4985 -----------------------
4986 -- Resolve_Allocator --
4987 -----------------------
4989 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4990 Desig_T : constant Entity_Id := Designated_Type (Typ);
4991 E : constant Node_Id := Expression (N);
4993 Discrim : Entity_Id;
4996 Assoc : Node_Id := Empty;
4999 procedure Check_Allocator_Discrim_Accessibility
5000 (Disc_Exp : Node_Id;
5001 Alloc_Typ : Entity_Id);
5002 -- Check that accessibility level associated with an access discriminant
5003 -- initialized in an allocator by the expression Disc_Exp is not deeper
5004 -- than the level of the allocator type Alloc_Typ. An error message is
5005 -- issued if this condition is violated. Specialized checks are done for
5006 -- the cases of a constraint expression which is an access attribute or
5007 -- an access discriminant.
5009 procedure Check_Allocator_Discrim_Accessibility_Exprs
5010 (Curr_Exp : Node_Id;
5011 Alloc_Typ : Entity_Id);
5012 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5013 -- across all expressions within a given conditional expression.
5015 function In_Dispatching_Context return Boolean;
5016 -- If the allocator is an actual in a call, it is allowed to be class-
5017 -- wide when the context is not because it is a controlling actual.
5019 -------------------------------------------
5020 -- Check_Allocator_Discrim_Accessibility --
5021 -------------------------------------------
5023 procedure Check_Allocator_Discrim_Accessibility
5024 (Disc_Exp : Node_Id;
5025 Alloc_Typ : Entity_Id)
5028 if Type_Access_Level (Etype (Disc_Exp)) >
5029 Deepest_Type_Access_Level (Alloc_Typ)
5032 ("operand type has deeper level than allocator type", Disc_Exp);
5034 -- When the expression is an Access attribute the level of the prefix
5035 -- object must not be deeper than that of the allocator's type.
5037 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5038 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5040 and then Object_Access_Level (Prefix (Disc_Exp)) >
5041 Deepest_Type_Access_Level (Alloc_Typ)
5044 ("prefix of attribute has deeper level than allocator type",
5047 -- When the expression is an access discriminant the check is against
5048 -- the level of the prefix object.
5050 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5051 and then Nkind (Disc_Exp) = N_Selected_Component
5052 and then Object_Access_Level (Prefix (Disc_Exp)) >
5053 Deepest_Type_Access_Level (Alloc_Typ)
5056 ("access discriminant has deeper level than allocator type",
5059 -- All other cases are legal
5064 end Check_Allocator_Discrim_Accessibility;
5066 -------------------------------------------------
5067 -- Check_Allocator_Discrim_Accessibility_Exprs --
5068 -------------------------------------------------
5070 procedure Check_Allocator_Discrim_Accessibility_Exprs
5071 (Curr_Exp : Node_Id;
5072 Alloc_Typ : Entity_Id)
5076 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5078 -- When conditional expressions are constant folded we know at
5079 -- compile time which expression to check - so don't bother with
5080 -- the rest of the cases.
5082 if Nkind (Curr_Exp) = N_Attribute_Reference then
5083 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5085 -- Non-constant-folded if expressions
5087 elsif Nkind (Disc_Exp) = N_If_Expression then
5088 -- Check both expressions if they are still present in the face
5091 Expr := Next (First (Expressions (Disc_Exp)));
5092 if Present (Expr) then
5093 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5094 Expr := Next (Expr);
5095 if Present (Expr) then
5096 Check_Allocator_Discrim_Accessibility_Exprs
5101 -- Non-constant-folded case expressions
5103 elsif Nkind (Disc_Exp) = N_Case_Expression then
5104 -- Check all alternatives
5106 Alt := First (Alternatives (Disc_Exp));
5107 while Present (Alt) loop
5108 Check_Allocator_Discrim_Accessibility_Exprs
5109 (Expression (Alt), Alloc_Typ);
5114 -- Base case, check the accessibility of the original node of the
5118 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5120 end Check_Allocator_Discrim_Accessibility_Exprs;
5122 ----------------------------
5123 -- In_Dispatching_Context --
5124 ----------------------------
5126 function In_Dispatching_Context return Boolean is
5127 Par : constant Node_Id := Parent (N);
5130 return Nkind (Par) in N_Subprogram_Call
5131 and then Is_Entity_Name (Name (Par))
5132 and then Is_Dispatching_Operation (Entity (Name (Par)));
5133 end In_Dispatching_Context;
5135 -- Start of processing for Resolve_Allocator
5138 -- Replace general access with specific type
5140 if Ekind (Etype (N)) = E_Allocator_Type then
5141 Set_Etype (N, Base_Type (Typ));
5144 if Is_Abstract_Type (Typ) then
5145 Error_Msg_N ("type of allocator cannot be abstract", N);
5148 -- For qualified expression, resolve the expression using the given
5149 -- subtype (nothing to do for type mark, subtype indication)
5151 if Nkind (E) = N_Qualified_Expression then
5152 if Is_Class_Wide_Type (Etype (E))
5153 and then not Is_Class_Wide_Type (Desig_T)
5154 and then not In_Dispatching_Context
5157 ("class-wide allocator not allowed for this access type", N);
5160 Resolve (Expression (E), Etype (E));
5161 Check_Non_Static_Context (Expression (E));
5162 Check_Unset_Reference (Expression (E));
5164 -- Allocators generated by the build-in-place expansion mechanism
5165 -- are explicitly marked as coming from source but do not need to be
5166 -- checked for limited initialization. To exclude this case, ensure
5167 -- that the parent of the allocator is a source node.
5168 -- The return statement constructed for an Expression_Function does
5169 -- not come from source but requires a limited check.
5171 if Is_Limited_Type (Etype (E))
5172 and then Comes_From_Source (N)
5174 (Comes_From_Source (Parent (N))
5176 (Ekind (Current_Scope) = E_Function
5177 and then Nkind (Original_Node (Unit_Declaration_Node
5178 (Current_Scope))) = N_Expression_Function))
5179 and then not In_Instance_Body
5181 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5182 if Nkind (Parent (N)) = N_Assignment_Statement then
5184 ("illegal expression for initialized allocator of a "
5185 & "limited type (RM 7.5 (2.7/2))", N);
5188 ("initialization not allowed for limited types", N);
5191 Explain_Limited_Type (Etype (E), N);
5195 -- A qualified expression requires an exact match of the type. Class-
5196 -- wide matching is not allowed.
5198 if (Is_Class_Wide_Type (Etype (Expression (E)))
5199 or else Is_Class_Wide_Type (Etype (E)))
5200 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
5202 Wrong_Type (Expression (E), Etype (E));
5205 -- Calls to build-in-place functions are not currently supported in
5206 -- allocators for access types associated with a simple storage pool.
5207 -- Supporting such allocators may require passing additional implicit
5208 -- parameters to build-in-place functions (or a significant revision
5209 -- of the current b-i-p implementation to unify the handling for
5210 -- multiple kinds of storage pools). ???
5212 if Is_Limited_View (Desig_T)
5213 and then Nkind (Expression (E)) = N_Function_Call
5216 Pool : constant Entity_Id :=
5217 Associated_Storage_Pool (Root_Type (Typ));
5221 Present (Get_Rep_Pragma
5222 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5225 ("limited function calls not yet supported in simple "
5226 & "storage pool allocators", Expression (E));
5231 -- A special accessibility check is needed for allocators that
5232 -- constrain access discriminants. The level of the type of the
5233 -- expression used to constrain an access discriminant cannot be
5234 -- deeper than the type of the allocator (in contrast to access
5235 -- parameters, where the level of the actual can be arbitrary).
5237 -- We can't use Valid_Conversion to perform this check because in
5238 -- general the type of the allocator is unrelated to the type of
5239 -- the access discriminant.
5241 if Ekind (Typ) /= E_Anonymous_Access_Type
5242 or else Is_Local_Anonymous_Access (Typ)
5244 Subtyp := Entity (Subtype_Mark (E));
5246 Aggr := Original_Node (Expression (E));
5248 if Has_Discriminants (Subtyp)
5249 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
5251 Discrim := First_Discriminant (Base_Type (Subtyp));
5253 -- Get the first component expression of the aggregate
5255 if Present (Expressions (Aggr)) then
5256 Disc_Exp := First (Expressions (Aggr));
5258 elsif Present (Component_Associations (Aggr)) then
5259 Assoc := First (Component_Associations (Aggr));
5261 if Present (Assoc) then
5262 Disc_Exp := Expression (Assoc);
5271 while Present (Discrim) and then Present (Disc_Exp) loop
5272 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5273 Check_Allocator_Discrim_Accessibility_Exprs
5277 Next_Discriminant (Discrim);
5279 if Present (Discrim) then
5280 if Present (Assoc) then
5282 Disc_Exp := Expression (Assoc);
5284 elsif Present (Next (Disc_Exp)) then
5288 Assoc := First (Component_Associations (Aggr));
5290 if Present (Assoc) then
5291 Disc_Exp := Expression (Assoc);
5301 -- For a subtype mark or subtype indication, freeze the subtype
5304 Freeze_Expression (E);
5306 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5308 ("initialization required for access-to-constant allocator", N);
5311 -- A special accessibility check is needed for allocators that
5312 -- constrain access discriminants. The level of the type of the
5313 -- expression used to constrain an access discriminant cannot be
5314 -- deeper than the type of the allocator (in contrast to access
5315 -- parameters, where the level of the actual can be arbitrary).
5316 -- We can't use Valid_Conversion to perform this check because
5317 -- in general the type of the allocator is unrelated to the type
5318 -- of the access discriminant.
5320 if Nkind (Original_Node (E)) = N_Subtype_Indication
5321 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5322 or else Is_Local_Anonymous_Access (Typ))
5324 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5326 if Has_Discriminants (Subtyp) then
5327 Discrim := First_Discriminant (Base_Type (Subtyp));
5328 Constr := First (Constraints (Constraint (Original_Node (E))));
5329 while Present (Discrim) and then Present (Constr) loop
5330 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5331 if Nkind (Constr) = N_Discriminant_Association then
5332 Disc_Exp := Expression (Constr);
5337 Check_Allocator_Discrim_Accessibility_Exprs
5341 Next_Discriminant (Discrim);
5348 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5349 -- check that the level of the type of the created object is not deeper
5350 -- than the level of the allocator's access type, since extensions can
5351 -- now occur at deeper levels than their ancestor types. This is a
5352 -- static accessibility level check; a run-time check is also needed in
5353 -- the case of an initialized allocator with a class-wide argument (see
5354 -- Expand_Allocator_Expression).
5356 if Ada_Version >= Ada_2005
5357 and then Is_Class_Wide_Type (Desig_T)
5360 Exp_Typ : Entity_Id;
5363 if Nkind (E) = N_Qualified_Expression then
5364 Exp_Typ := Etype (E);
5365 elsif Nkind (E) = N_Subtype_Indication then
5366 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5368 Exp_Typ := Entity (E);
5371 if Type_Access_Level (Exp_Typ) >
5372 Deepest_Type_Access_Level (Typ)
5374 if In_Instance_Body then
5375 Error_Msg_Warn := SPARK_Mode /= On;
5377 ("type in allocator has deeper level than designated "
5378 & "class-wide type<<", E);
5379 Error_Msg_N ("\Program_Error [<<", E);
5382 Make_Raise_Program_Error (Sloc (N),
5383 Reason => PE_Accessibility_Check_Failed));
5386 -- Do not apply Ada 2005 accessibility checks on a class-wide
5387 -- allocator if the type given in the allocator is a formal
5388 -- type. A run-time check will be performed in the instance.
5390 elsif not Is_Generic_Type (Exp_Typ) then
5392 ("type in allocator has deeper level than designated "
5393 & "class-wide type", E);
5399 -- Check for allocation from an empty storage pool. But do not complain
5400 -- if it's a return statement for a build-in-place function, because the
5401 -- allocator is there just in case the caller uses an allocator. If the
5402 -- caller does use an allocator, it will be caught at the call site.
5404 if No_Pool_Assigned (Typ)
5405 and then not Alloc_For_BIP_Return (N)
5407 Error_Msg_N ("allocation from empty storage pool!", N);
5409 -- If the context is an unchecked conversion, as may happen within an
5410 -- inlined subprogram, the allocator is being resolved with its own
5411 -- anonymous type. In that case, if the target type has a specific
5412 -- storage pool, it must be inherited explicitly by the allocator type.
5414 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5415 and then No (Associated_Storage_Pool (Typ))
5417 Set_Associated_Storage_Pool
5418 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5421 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5422 Check_Restriction (No_Anonymous_Allocators, N);
5425 -- Check that an allocator with task parts isn't for a nested access
5426 -- type when restriction No_Task_Hierarchy applies.
5428 if not Is_Library_Level_Entity (Base_Type (Typ))
5429 and then Has_Task (Base_Type (Desig_T))
5431 Check_Restriction (No_Task_Hierarchy, N);
5434 -- An illegal allocator may be rewritten as a raise Program_Error
5437 if Nkind (N) = N_Allocator then
5439 -- Avoid coextension processing for an allocator that is the
5440 -- expansion of a build-in-place function call.
5442 if Nkind (Original_Node (N)) = N_Allocator
5443 and then Nkind (Expression (Original_Node (N))) =
5444 N_Qualified_Expression
5445 and then Nkind (Expression (Expression (Original_Node (N)))) =
5447 and then Is_Expanded_Build_In_Place_Call
5448 (Expression (Expression (Original_Node (N))))
5450 null; -- b-i-p function call case
5453 -- An anonymous access discriminant is the definition of a
5456 if Ekind (Typ) = E_Anonymous_Access_Type
5457 and then Nkind (Associated_Node_For_Itype (Typ)) =
5458 N_Discriminant_Specification
5461 Discr : constant Entity_Id :=
5462 Defining_Identifier (Associated_Node_For_Itype (Typ));
5465 Check_Restriction (No_Coextensions, N);
5467 -- Ada 2012 AI05-0052: If the designated type of the
5468 -- allocator is limited, then the allocator shall not
5469 -- be used to define the value of an access discriminant
5470 -- unless the discriminated type is immutably limited.
5472 if Ada_Version >= Ada_2012
5473 and then Is_Limited_Type (Desig_T)
5474 and then not Is_Limited_View (Scope (Discr))
5477 ("only immutably limited types can have anonymous "
5478 & "access discriminants designating a limited type",
5483 -- Avoid marking an allocator as a dynamic coextension if it is
5484 -- within a static construct.
5486 if not Is_Static_Coextension (N) then
5487 Set_Is_Dynamic_Coextension (N);
5489 -- Finalization and deallocation of coextensions utilizes an
5490 -- approximate implementation which does not directly adhere
5491 -- to the semantic rules. Warn on potential issues involving
5494 if Is_Controlled (Desig_T) then
5496 ("??coextension will not be finalized when its "
5497 & "associated owner is deallocated or finalized", N);
5500 ("??coextension will not be deallocated when its "
5501 & "associated owner is deallocated", N);
5505 -- Cleanup for potential static coextensions
5508 Set_Is_Dynamic_Coextension (N, False);
5509 Set_Is_Static_Coextension (N, False);
5511 -- Anonymous access-to-controlled objects are not finalized on
5512 -- time because this involves run-time ownership and currently
5513 -- this property is not available. In rare cases the object may
5514 -- not be finalized at all. Warn on potential issues involving
5515 -- anonymous access-to-controlled objects.
5517 if Ekind (Typ) = E_Anonymous_Access_Type
5518 and then Is_Controlled_Active (Desig_T)
5521 ("??object designated by anonymous access object might "
5522 & "not be finalized until its enclosing library unit "
5523 & "goes out of scope", N);
5524 Error_Msg_N ("\use named access type instead", N);
5530 -- Report a simple error: if the designated object is a local task,
5531 -- its body has not been seen yet, and its activation will fail an
5532 -- elaboration check.
5534 if Is_Task_Type (Desig_T)
5535 and then Scope (Base_Type (Desig_T)) = Current_Scope
5536 and then Is_Compilation_Unit (Current_Scope)
5537 and then Ekind (Current_Scope) = E_Package
5538 and then not In_Package_Body (Current_Scope)
5540 Error_Msg_Warn := SPARK_Mode /= On;
5541 Error_Msg_N ("cannot activate task before body seen<<", N);
5542 Error_Msg_N ("\Program_Error [<<", N);
5545 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5546 -- type with a task component on a subpool. This action must raise
5547 -- Program_Error at runtime.
5549 if Ada_Version >= Ada_2012
5550 and then Nkind (N) = N_Allocator
5551 and then Present (Subpool_Handle_Name (N))
5552 and then Has_Task (Desig_T)
5554 Error_Msg_Warn := SPARK_Mode /= On;
5555 Error_Msg_N ("cannot allocate task on subpool<<", N);
5556 Error_Msg_N ("\Program_Error [<<", N);
5559 Make_Raise_Program_Error (Sloc (N),
5560 Reason => PE_Explicit_Raise));
5563 end Resolve_Allocator;
5565 ---------------------------
5566 -- Resolve_Arithmetic_Op --
5567 ---------------------------
5569 -- Used for resolving all arithmetic operators except exponentiation
5571 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5572 L : constant Node_Id := Left_Opnd (N);
5573 R : constant Node_Id := Right_Opnd (N);
5574 TL : constant Entity_Id := Base_Type (Etype (L));
5575 TR : constant Entity_Id := Base_Type (Etype (R));
5579 B_Typ : constant Entity_Id := Base_Type (Typ);
5580 -- We do the resolution using the base type, because intermediate values
5581 -- in expressions always are of the base type, not a subtype of it.
5583 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5584 -- Returns True if N is in a context that expects "any real type"
5586 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5587 -- Return True iff given type is Integer or universal real/integer
5589 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5590 -- Choose type of integer literal in fixed-point operation to conform
5591 -- to available fixed-point type. T is the type of the other operand,
5592 -- which is needed to determine the expected type of N.
5594 procedure Set_Operand_Type (N : Node_Id);
5595 -- Set operand type to T if universal
5597 -------------------------------
5598 -- Expected_Type_Is_Any_Real --
5599 -------------------------------
5601 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5603 -- N is the expression after "delta" in a fixed_point_definition;
5606 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
5607 N_Decimal_Fixed_Point_Definition,
5609 -- N is one of the bounds in a real_range_specification;
5612 N_Real_Range_Specification,
5614 -- N is the expression of a delta_constraint;
5617 N_Delta_Constraint);
5618 end Expected_Type_Is_Any_Real;
5620 -----------------------------
5621 -- Is_Integer_Or_Universal --
5622 -----------------------------
5624 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5626 Index : Interp_Index;
5630 if not Is_Overloaded (N) then
5632 return Base_Type (T) = Base_Type (Standard_Integer)
5633 or else T = Universal_Integer
5634 or else T = Universal_Real;
5636 Get_First_Interp (N, Index, It);
5637 while Present (It.Typ) loop
5638 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5639 or else It.Typ = Universal_Integer
5640 or else It.Typ = Universal_Real
5645 Get_Next_Interp (Index, It);
5650 end Is_Integer_Or_Universal;
5652 ----------------------------
5653 -- Set_Mixed_Mode_Operand --
5654 ----------------------------
5656 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5657 Index : Interp_Index;
5661 if Universal_Interpretation (N) = Universal_Integer then
5663 -- A universal integer literal is resolved as standard integer
5664 -- except in the case of a fixed-point result, where we leave it
5665 -- as universal (to be handled by Exp_Fixd later on)
5667 if Is_Fixed_Point_Type (T) then
5668 Resolve (N, Universal_Integer);
5670 Resolve (N, Standard_Integer);
5673 elsif Universal_Interpretation (N) = Universal_Real
5674 and then (T = Base_Type (Standard_Integer)
5675 or else T = Universal_Integer
5676 or else T = Universal_Real)
5678 -- A universal real can appear in a fixed-type context. We resolve
5679 -- the literal with that context, even though this might raise an
5680 -- exception prematurely (the other operand may be zero).
5684 elsif Etype (N) = Base_Type (Standard_Integer)
5685 and then T = Universal_Real
5686 and then Is_Overloaded (N)
5688 -- Integer arg in mixed-mode operation. Resolve with universal
5689 -- type, in case preference rule must be applied.
5691 Resolve (N, Universal_Integer);
5693 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
5695 -- If the operand is part of a fixed multiplication operation,
5696 -- a conversion will be applied to each operand, so resolve it
5697 -- with its own type.
5699 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
5703 -- Not a mixed-mode operation, resolve with context
5708 elsif Etype (N) = Any_Fixed then
5710 -- N may itself be a mixed-mode operation, so use context type
5714 elsif Is_Fixed_Point_Type (T)
5715 and then B_Typ = Universal_Fixed
5716 and then Is_Overloaded (N)
5718 -- Must be (fixed * fixed) operation, operand must have one
5719 -- compatible interpretation.
5721 Resolve (N, Any_Fixed);
5723 elsif Is_Fixed_Point_Type (B_Typ)
5724 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5725 and then Is_Overloaded (N)
5727 -- C * F(X) in a fixed context, where C is a real literal or a
5728 -- fixed-point expression. F must have either a fixed type
5729 -- interpretation or an integer interpretation, but not both.
5731 Get_First_Interp (N, Index, It);
5732 while Present (It.Typ) loop
5733 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5734 if Analyzed (N) then
5735 Error_Msg_N ("ambiguous operand in fixed operation", N);
5737 Resolve (N, Standard_Integer);
5740 elsif Is_Fixed_Point_Type (It.Typ) then
5741 if Analyzed (N) then
5742 Error_Msg_N ("ambiguous operand in fixed operation", N);
5744 Resolve (N, It.Typ);
5748 Get_Next_Interp (Index, It);
5751 -- Reanalyze the literal with the fixed type of the context. If
5752 -- context is Universal_Fixed, we are within a conversion, leave
5753 -- the literal as a universal real because there is no usable
5754 -- fixed type, and the target of the conversion plays no role in
5768 if B_Typ = Universal_Fixed
5769 and then Nkind (Op2) = N_Real_Literal
5771 T2 := Universal_Real;
5776 Set_Analyzed (Op2, False);
5780 -- A universal real conditional expression can appear in a fixed-type
5781 -- context and must be resolved with that context to facilitate the
5782 -- code generation in the back end. However, If the context is
5783 -- Universal_fixed (i.e. as an operand of a multiplication/division
5784 -- involving a fixed-point operand) the conditional expression must
5785 -- resolve to a unique visible fixed_point type, normally Duration.
5787 elsif Nkind_In (N, N_Case_Expression, N_If_Expression)
5788 and then Etype (N) = Universal_Real
5789 and then Is_Fixed_Point_Type (B_Typ)
5791 if B_Typ = Universal_Fixed then
5792 Resolve (N, Unique_Fixed_Point_Type (N));
5801 end Set_Mixed_Mode_Operand;
5803 ----------------------
5804 -- Set_Operand_Type --
5805 ----------------------
5807 procedure Set_Operand_Type (N : Node_Id) is
5809 if Etype (N) = Universal_Integer
5810 or else Etype (N) = Universal_Real
5814 end Set_Operand_Type;
5816 -- Start of processing for Resolve_Arithmetic_Op
5819 if Comes_From_Source (N)
5820 and then Ekind (Entity (N)) = E_Function
5821 and then Is_Imported (Entity (N))
5822 and then Is_Intrinsic_Subprogram (Entity (N))
5824 Resolve_Intrinsic_Operator (N, Typ);
5827 -- Special-case for mixed-mode universal expressions or fixed point type
5828 -- operation: each argument is resolved separately. The same treatment
5829 -- is required if one of the operands of a fixed point operation is
5830 -- universal real, since in this case we don't do a conversion to a
5831 -- specific fixed-point type (instead the expander handles the case).
5833 -- Set the type of the node to its universal interpretation because
5834 -- legality checks on an exponentiation operand need the context.
5836 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
5837 and then Present (Universal_Interpretation (L))
5838 and then Present (Universal_Interpretation (R))
5840 Set_Etype (N, B_Typ);
5841 Resolve (L, Universal_Interpretation (L));
5842 Resolve (R, Universal_Interpretation (R));
5844 elsif (B_Typ = Universal_Real
5845 or else Etype (N) = Universal_Fixed
5846 or else (Etype (N) = Any_Fixed
5847 and then Is_Fixed_Point_Type (B_Typ))
5848 or else (Is_Fixed_Point_Type (B_Typ)
5849 and then (Is_Integer_Or_Universal (L)
5851 Is_Integer_Or_Universal (R))))
5852 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5854 if TL = Universal_Integer or else TR = Universal_Integer then
5855 Check_For_Visible_Operator (N, B_Typ);
5858 -- If context is a fixed type and one operand is integer, the other
5859 -- is resolved with the type of the context.
5861 if Is_Fixed_Point_Type (B_Typ)
5862 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5863 or else TL = Universal_Integer)
5868 elsif Is_Fixed_Point_Type (B_Typ)
5869 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5870 or else TR = Universal_Integer)
5875 -- If both operands are universal and the context is a floating
5876 -- point type, the operands are resolved to the type of the context.
5878 elsif Is_Floating_Point_Type (B_Typ) then
5883 Set_Mixed_Mode_Operand (L, TR);
5884 Set_Mixed_Mode_Operand (R, TL);
5887 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
5888 -- multiplying operators from being used when the expected type is
5889 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
5890 -- some cases where the expected type is actually Any_Real;
5891 -- Expected_Type_Is_Any_Real takes care of that case.
5893 if Etype (N) = Universal_Fixed
5894 or else Etype (N) = Any_Fixed
5896 if B_Typ = Universal_Fixed
5897 and then not Expected_Type_Is_Any_Real (N)
5898 and then not Nkind_In (Parent (N), N_Type_Conversion,
5899 N_Unchecked_Type_Conversion)
5901 Error_Msg_N ("type cannot be determined from context!", N);
5902 Error_Msg_N ("\explicit conversion to result type required", N);
5904 Set_Etype (L, Any_Type);
5905 Set_Etype (R, Any_Type);
5908 if Ada_Version = Ada_83
5909 and then Etype (N) = Universal_Fixed
5911 Nkind_In (Parent (N), N_Type_Conversion,
5912 N_Unchecked_Type_Conversion)
5915 ("(Ada 83) fixed-point operation needs explicit "
5919 -- The expected type is "any real type" in contexts like
5921 -- type T is delta <universal_fixed-expression> ...
5923 -- in which case we need to set the type to Universal_Real
5924 -- so that static expression evaluation will work properly.
5926 if Expected_Type_Is_Any_Real (N) then
5927 Set_Etype (N, Universal_Real);
5929 Set_Etype (N, B_Typ);
5933 elsif Is_Fixed_Point_Type (B_Typ)
5934 and then (Is_Integer_Or_Universal (L)
5935 or else Nkind (L) = N_Real_Literal
5936 or else Nkind (R) = N_Real_Literal
5937 or else Is_Integer_Or_Universal (R))
5939 Set_Etype (N, B_Typ);
5941 elsif Etype (N) = Any_Fixed then
5943 -- If no previous errors, this is only possible if one operand is
5944 -- overloaded and the context is universal. Resolve as such.
5946 Set_Etype (N, B_Typ);
5950 if (TL = Universal_Integer or else TL = Universal_Real)
5952 (TR = Universal_Integer or else TR = Universal_Real)
5954 Check_For_Visible_Operator (N, B_Typ);
5957 -- If the context is Universal_Fixed and the operands are also
5958 -- universal fixed, this is an error, unless there is only one
5959 -- applicable fixed_point type (usually Duration).
5961 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
5962 T := Unique_Fixed_Point_Type (N);
5964 if T = Any_Type then
5977 -- If one of the arguments was resolved to a non-universal type.
5978 -- label the result of the operation itself with the same type.
5979 -- Do the same for the universal argument, if any.
5981 T := Intersect_Types (L, R);
5982 Set_Etype (N, Base_Type (T));
5983 Set_Operand_Type (L);
5984 Set_Operand_Type (R);
5987 Generate_Operator_Reference (N, Typ);
5988 Analyze_Dimension (N);
5989 Eval_Arithmetic_Op (N);
5991 -- In SPARK, a multiplication or division with operands of fixed point
5992 -- types must be qualified or explicitly converted to identify the
5995 if (Is_Fixed_Point_Type (Etype (L))
5996 or else Is_Fixed_Point_Type (Etype (R)))
5997 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5999 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
6001 Check_SPARK_05_Restriction
6002 ("operation should be qualified or explicitly converted", N);
6005 -- Set overflow and division checking bit
6007 if Nkind (N) in N_Op then
6008 if not Overflow_Checks_Suppressed (Etype (N)) then
6009 Enable_Overflow_Check (N);
6012 -- Give warning if explicit division by zero
6014 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
6015 and then not Division_Checks_Suppressed (Etype (N))
6017 Rop := Right_Opnd (N);
6019 if Compile_Time_Known_Value (Rop)
6020 and then ((Is_Integer_Type (Etype (Rop))
6021 and then Expr_Value (Rop) = Uint_0)
6023 (Is_Real_Type (Etype (Rop))
6024 and then Expr_Value_R (Rop) = Ureal_0))
6026 -- Specialize the warning message according to the operation.
6027 -- When SPARK_Mode is On, force a warning instead of an error
6028 -- in that case, as this likely corresponds to deactivated
6029 -- code. The following warnings are for the case
6034 -- For division, we have two cases, for float division
6035 -- of an unconstrained float type, on a machine where
6036 -- Machine_Overflows is false, we don't get an exception
6037 -- at run-time, but rather an infinity or Nan. The Nan
6038 -- case is pretty obscure, so just warn about infinities.
6040 if Is_Floating_Point_Type (Typ)
6041 and then not Is_Constrained (Typ)
6042 and then not Machine_Overflows_On_Target
6045 ("float division by zero, may generate "
6046 & "'+'/'- infinity??", Right_Opnd (N));
6048 -- For all other cases, we get a Constraint_Error
6051 Apply_Compile_Time_Constraint_Error
6052 (N, "division by zero??", CE_Divide_By_Zero,
6053 Loc => Sloc (Right_Opnd (N)),
6054 Warn => SPARK_Mode = On);
6058 Apply_Compile_Time_Constraint_Error
6059 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6060 Loc => Sloc (Right_Opnd (N)),
6061 Warn => SPARK_Mode = On);
6064 Apply_Compile_Time_Constraint_Error
6065 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6066 Loc => Sloc (Right_Opnd (N)),
6067 Warn => SPARK_Mode = On);
6069 -- Division by zero can only happen with division, rem,
6070 -- and mod operations.
6073 raise Program_Error;
6076 -- In GNATprove mode, we enable the division check so that
6077 -- GNATprove will issue a message if it cannot be proved.
6079 if GNATprove_Mode then
6080 Activate_Division_Check (N);
6083 -- Otherwise just set the flag to check at run time
6086 Activate_Division_Check (N);
6090 -- If Restriction No_Implicit_Conditionals is active, then it is
6091 -- violated if either operand can be negative for mod, or for rem
6092 -- if both operands can be negative.
6094 if Restriction_Check_Required (No_Implicit_Conditionals)
6095 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
6104 -- Set if corresponding operand might be negative
6108 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6109 LNeg := (not OK) or else Lo < 0;
6112 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6113 RNeg := (not OK) or else Lo < 0;
6115 -- Check if we will be generating conditionals. There are two
6116 -- cases where that can happen, first for REM, the only case
6117 -- is largest negative integer mod -1, where the division can
6118 -- overflow, but we still have to give the right result. The
6119 -- front end generates a test for this annoying case. Here we
6120 -- just test if both operands can be negative (that's what the
6121 -- expander does, so we match its logic here).
6123 -- The second case is mod where either operand can be negative.
6124 -- In this case, the back end has to generate additional tests.
6126 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6128 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6130 Check_Restriction (No_Implicit_Conditionals, N);
6136 Check_Unset_Reference (L);
6137 Check_Unset_Reference (R);
6138 end Resolve_Arithmetic_Op;
6144 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6145 function Same_Or_Aliased_Subprograms
6147 E : Entity_Id) return Boolean;
6148 -- Returns True if the subprogram entity S is the same as E or else
6149 -- S is an alias of E.
6151 ---------------------------------
6152 -- Same_Or_Aliased_Subprograms --
6153 ---------------------------------
6155 function Same_Or_Aliased_Subprograms
6157 E : Entity_Id) return Boolean
6159 Subp_Alias : constant Entity_Id := Alias (S);
6161 return S = E or else (Present (Subp_Alias) and then Subp_Alias = E);
6162 end Same_Or_Aliased_Subprograms;
6166 Loc : constant Source_Ptr := Sloc (N);
6167 Subp : constant Node_Id := Name (N);
6168 Body_Id : Entity_Id;
6178 -- Start of processing for Resolve_Call
6181 -- Preserve relevant elaboration-related attributes of the context which
6182 -- are no longer available or very expensive to recompute once analysis,
6183 -- resolution, and expansion are over.
6185 Mark_Elaboration_Attributes
6191 -- The context imposes a unique interpretation with type Typ on a
6192 -- procedure or function call. Find the entity of the subprogram that
6193 -- yields the expected type, and propagate the corresponding formal
6194 -- constraints on the actuals. The caller has established that an
6195 -- interpretation exists, and emitted an error if not unique.
6197 -- First deal with the case of a call to an access-to-subprogram,
6198 -- dereference made explicit in Analyze_Call.
6200 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6201 if not Is_Overloaded (Subp) then
6202 Nam := Etype (Subp);
6205 -- Find the interpretation whose type (a subprogram type) has a
6206 -- return type that is compatible with the context. Analysis of
6207 -- the node has established that one exists.
6211 Get_First_Interp (Subp, I, It);
6212 while Present (It.Typ) loop
6213 if Covers (Typ, Etype (It.Typ)) then
6218 Get_Next_Interp (I, It);
6222 raise Program_Error;
6226 -- If the prefix is not an entity, then resolve it
6228 if not Is_Entity_Name (Subp) then
6229 Resolve (Subp, Nam);
6232 -- For an indirect call, we always invalidate checks, since we do not
6233 -- know whether the subprogram is local or global. Yes we could do
6234 -- better here, e.g. by knowing that there are no local subprograms,
6235 -- but it does not seem worth the effort. Similarly, we kill all
6236 -- knowledge of current constant values.
6238 Kill_Current_Values;
6240 -- If this is a procedure call which is really an entry call, do
6241 -- the conversion of the procedure call to an entry call. Protected
6242 -- operations use the same circuitry because the name in the call
6243 -- can be an arbitrary expression with special resolution rules.
6245 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
6246 or else (Is_Entity_Name (Subp)
6247 and then Ekind_In (Entity (Subp), E_Entry, E_Entry_Family))
6249 Resolve_Entry_Call (N, Typ);
6251 if Legacy_Elaboration_Checks then
6252 Check_Elab_Call (N);
6255 -- Annotate the tree by creating a call marker in case the original
6256 -- call is transformed by expansion. The call marker is automatically
6257 -- saved for later examination by the ABE Processing phase.
6259 Build_Call_Marker (N);
6261 -- Kill checks and constant values, as above for indirect case
6262 -- Who knows what happens when another task is activated?
6264 Kill_Current_Values;
6267 -- Normal subprogram call with name established in Resolve
6269 elsif not (Is_Type (Entity (Subp))) then
6270 Nam := Entity (Subp);
6271 Set_Entity_With_Checks (Subp, Nam);
6273 -- Otherwise we must have the case of an overloaded call
6276 pragma Assert (Is_Overloaded (Subp));
6278 -- Initialize Nam to prevent warning (we know it will be assigned
6279 -- in the loop below, but the compiler does not know that).
6283 Get_First_Interp (Subp, I, It);
6284 while Present (It.Typ) loop
6285 if Covers (Typ, It.Typ) then
6287 Set_Entity_With_Checks (Subp, Nam);
6291 Get_Next_Interp (I, It);
6295 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
6296 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
6297 and then Nkind (Subp) /= N_Explicit_Dereference
6298 and then Present (Parameter_Associations (N))
6300 -- The prefix is a parameterless function call that returns an access
6301 -- to subprogram. If parameters are present in the current call, add
6302 -- add an explicit dereference. We use the base type here because
6303 -- within an instance these may be subtypes.
6305 -- The dereference is added either in Analyze_Call or here. Should
6306 -- be consolidated ???
6308 Set_Is_Overloaded (Subp, False);
6309 Set_Etype (Subp, Etype (Nam));
6310 Insert_Explicit_Dereference (Subp);
6311 Nam := Designated_Type (Etype (Nam));
6312 Resolve (Subp, Nam);
6315 -- Check that a call to Current_Task does not occur in an entry body
6317 if Is_RTE (Nam, RE_Current_Task) then
6326 -- Exclude calls that occur within the default of a formal
6327 -- parameter of the entry, since those are evaluated outside
6330 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6332 if Nkind (P) = N_Entry_Body
6333 or else (Nkind (P) = N_Subprogram_Body
6334 and then Is_Entry_Barrier_Function (P))
6337 Error_Msg_Warn := SPARK_Mode /= On;
6339 ("& should not be used in entry body (RM C.7(17))<<",
6341 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6343 Make_Raise_Program_Error (Loc,
6344 Reason => PE_Current_Task_In_Entry_Body));
6345 Set_Etype (N, Rtype);
6352 -- Check that a procedure call does not occur in the context of the
6353 -- entry call statement of a conditional or timed entry call. Note that
6354 -- the case of a call to a subprogram renaming of an entry will also be
6355 -- rejected. The test for N not being an N_Entry_Call_Statement is
6356 -- defensive, covering the possibility that the processing of entry
6357 -- calls might reach this point due to later modifications of the code
6360 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6361 and then Nkind (N) /= N_Entry_Call_Statement
6362 and then Entry_Call_Statement (Parent (N)) = N
6364 if Ada_Version < Ada_2005 then
6365 Error_Msg_N ("entry call required in select statement", N);
6367 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6368 -- for a procedure_or_entry_call, the procedure_name or
6369 -- procedure_prefix of the procedure_call_statement shall denote
6370 -- an entry renamed by a procedure, or (a view of) a primitive
6371 -- subprogram of a limited interface whose first parameter is
6372 -- a controlling parameter.
6374 elsif Nkind (N) = N_Procedure_Call_Statement
6375 and then not Is_Renamed_Entry (Nam)
6376 and then not Is_Controlling_Limited_Procedure (Nam)
6379 ("entry call or dispatching primitive of interface required", N);
6383 -- If the SPARK_05 restriction is active, we are not allowed
6384 -- to have a call to a subprogram before we see its completion.
6386 if not Has_Completion (Nam)
6387 and then Restriction_Check_Required (SPARK_05)
6389 -- Don't flag strange internal calls
6391 and then Comes_From_Source (N)
6392 and then Comes_From_Source (Nam)
6394 -- Only flag calls in extended main source
6396 and then In_Extended_Main_Source_Unit (Nam)
6397 and then In_Extended_Main_Source_Unit (N)
6399 -- Exclude enumeration literals from this processing
6401 and then Ekind (Nam) /= E_Enumeration_Literal
6403 Check_SPARK_05_Restriction
6404 ("call to subprogram cannot appear before its body", N);
6407 -- Check that this is not a call to a protected procedure or entry from
6408 -- within a protected function.
6410 Check_Internal_Protected_Use (N, Nam);
6412 -- Freeze the subprogram name if not in a spec-expression. Note that
6413 -- we freeze procedure calls as well as function calls. Procedure calls
6414 -- are not frozen according to the rules (RM 13.14(14)) because it is
6415 -- impossible to have a procedure call to a non-frozen procedure in
6416 -- pure Ada, but in the code that we generate in the expander, this
6417 -- rule needs extending because we can generate procedure calls that
6420 -- In Ada 2012, expression functions may be called within pre/post
6421 -- conditions of subsequent functions or expression functions. Such
6422 -- calls do not freeze when they appear within generated bodies,
6423 -- (including the body of another expression function) which would
6424 -- place the freeze node in the wrong scope. An expression function
6425 -- is frozen in the usual fashion, by the appearance of a real body,
6426 -- or at the end of a declarative part. However an implicit call to
6427 -- an expression function may appear when it is part of a default
6428 -- expression in a call to an initialization procedure, and must be
6429 -- frozen now, even if the body is inserted at a later point.
6430 -- Otherwise, the call freezes the expression if expander is active,
6431 -- for example as part of an object declaration.
6433 if Is_Entity_Name (Subp)
6434 and then not In_Spec_Expression
6435 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6437 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6438 or else Expander_Active)
6440 if Is_Expression_Function (Entity (Subp)) then
6442 -- Force freeze of expression function in call
6444 Set_Comes_From_Source (Subp, True);
6445 Set_Must_Not_Freeze (Subp, False);
6448 Freeze_Expression (Subp);
6451 -- For a predefined operator, the type of the result is the type imposed
6452 -- by context, except for a predefined operation on universal fixed.
6453 -- Otherwise the type of the call is the type returned by the subprogram
6456 if Is_Predefined_Op (Nam) then
6457 if Etype (N) /= Universal_Fixed then
6461 -- If the subprogram returns an array type, and the context requires the
6462 -- component type of that array type, the node is really an indexing of
6463 -- the parameterless call. Resolve as such. A pathological case occurs
6464 -- when the type of the component is an access to the array type. In
6465 -- this case the call is truly ambiguous. If the call is to an intrinsic
6466 -- subprogram, it can't be an indexed component. This check is necessary
6467 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6468 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6469 -- pointers to the same array), the compiler gets confused and does an
6470 -- infinite recursion.
6472 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6474 ((Is_Array_Type (Etype (Nam))
6475 and then Covers (Typ, Component_Type (Etype (Nam))))
6477 (Is_Access_Type (Etype (Nam))
6478 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6480 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6481 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6484 Index_Node : Node_Id;
6486 Ret_Type : constant Entity_Id := Etype (Nam);
6489 -- If this is a parameterless call there is no ambiguity and the
6490 -- call has the type of the function.
6492 if No (First_Actual (N)) then
6493 Set_Etype (N, Etype (Nam));
6495 if Present (First_Formal (Nam)) then
6496 Resolve_Actuals (N, Nam);
6499 -- Annotate the tree by creating a call marker in case the
6500 -- original call is transformed by expansion. The call marker
6501 -- is automatically saved for later examination by the ABE
6502 -- Processing phase.
6504 Build_Call_Marker (N);
6506 elsif Is_Access_Type (Ret_Type)
6508 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6511 ("cannot disambiguate function call and indexing", N);
6513 New_Subp := Relocate_Node (Subp);
6515 -- The called entity may be an explicit dereference, in which
6516 -- case there is no entity to set.
6518 if Nkind (New_Subp) /= N_Explicit_Dereference then
6519 Set_Entity (Subp, Nam);
6522 if (Is_Array_Type (Ret_Type)
6523 and then Component_Type (Ret_Type) /= Any_Type)
6525 (Is_Access_Type (Ret_Type)
6527 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6529 if Needs_No_Actuals (Nam) then
6531 -- Indexed call to a parameterless function
6534 Make_Indexed_Component (Loc,
6536 Make_Function_Call (Loc, Name => New_Subp),
6537 Expressions => Parameter_Associations (N));
6539 -- An Ada 2005 prefixed call to a primitive operation
6540 -- whose first parameter is the prefix. This prefix was
6541 -- prepended to the parameter list, which is actually a
6542 -- list of indexes. Remove the prefix in order to build
6543 -- the proper indexed component.
6546 Make_Indexed_Component (Loc,
6548 Make_Function_Call (Loc,
6550 Parameter_Associations =>
6552 (Remove_Head (Parameter_Associations (N)))),
6553 Expressions => Parameter_Associations (N));
6556 -- Preserve the parenthesis count of the node
6558 Set_Paren_Count (Index_Node, Paren_Count (N));
6560 -- Since we are correcting a node classification error made
6561 -- by the parser, we call Replace rather than Rewrite.
6563 Replace (N, Index_Node);
6565 Set_Etype (Prefix (N), Ret_Type);
6567 Resolve_Indexed_Component (N, Typ);
6569 if Legacy_Elaboration_Checks then
6570 Check_Elab_Call (Prefix (N));
6573 -- Annotate the tree by creating a call marker in case
6574 -- the original call is transformed by expansion. The call
6575 -- marker is automatically saved for later examination by
6576 -- the ABE Processing phase.
6578 Build_Call_Marker (Prefix (N));
6586 -- If the called function is not declared in the main unit and it
6587 -- returns the limited view of type then use the available view (as
6588 -- is done in Try_Object_Operation) to prevent back-end confusion;
6589 -- for the function entity itself. The call must appear in a context
6590 -- where the nonlimited view is available. If the function entity is
6591 -- in the extended main unit then no action is needed, because the
6592 -- back end handles this case. In either case the type of the call
6593 -- is the nonlimited view.
6595 if From_Limited_With (Etype (Nam))
6596 and then Present (Available_View (Etype (Nam)))
6598 Set_Etype (N, Available_View (Etype (Nam)));
6600 if not In_Extended_Main_Code_Unit (Nam) then
6601 Set_Etype (Nam, Available_View (Etype (Nam)));
6605 Set_Etype (N, Etype (Nam));
6609 -- In the case where the call is to an overloaded subprogram, Analyze
6610 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6611 -- such a case Normalize_Actuals needs to be called once more to order
6612 -- the actuals correctly. Otherwise the call will have the ordering
6613 -- given by the last overloaded subprogram whether this is the correct
6614 -- one being called or not.
6616 if Is_Overloaded (Subp) then
6617 Normalize_Actuals (N, Nam, False, Norm_OK);
6618 pragma Assert (Norm_OK);
6621 -- In any case, call is fully resolved now. Reset Overload flag, to
6622 -- prevent subsequent overload resolution if node is analyzed again
6624 Set_Is_Overloaded (Subp, False);
6625 Set_Is_Overloaded (N, False);
6627 -- A Ghost entity must appear in a specific context
6629 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6630 Check_Ghost_Context (Nam, N);
6633 -- If we are calling the current subprogram from immediately within its
6634 -- body, then that is the case where we can sometimes detect cases of
6635 -- infinite recursion statically. Do not try this in case restriction
6636 -- No_Recursion is in effect anyway, and do it only for source calls.
6638 if Comes_From_Source (N) then
6639 Scop := Current_Scope;
6641 -- Check violation of SPARK_05 restriction which does not permit
6642 -- a subprogram body to contain a call to the subprogram directly.
6644 if Restriction_Check_Required (SPARK_05)
6645 and then Same_Or_Aliased_Subprograms (Nam, Scop)
6647 Check_SPARK_05_Restriction
6648 ("subprogram may not contain direct call to itself", N);
6651 -- Issue warning for possible infinite recursion in the absence
6652 -- of the No_Recursion restriction.
6654 if Same_Or_Aliased_Subprograms (Nam, Scop)
6655 and then not Restriction_Active (No_Recursion)
6656 and then Check_Infinite_Recursion (N)
6658 -- Here we detected and flagged an infinite recursion, so we do
6659 -- not need to test the case below for further warnings. Also we
6660 -- are all done if we now have a raise SE node.
6662 if Nkind (N) = N_Raise_Storage_Error then
6666 -- If call is to immediately containing subprogram, then check for
6667 -- the case of a possible run-time detectable infinite recursion.
6670 Scope_Loop : while Scop /= Standard_Standard loop
6671 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6673 -- Although in general case, recursion is not statically
6674 -- checkable, the case of calling an immediately containing
6675 -- subprogram is easy to catch.
6677 if not Is_Ignored_Ghost_Entity (Nam) then
6678 Check_Restriction (No_Recursion, N);
6681 -- If the recursive call is to a parameterless subprogram,
6682 -- then even if we can't statically detect infinite
6683 -- recursion, this is pretty suspicious, and we output a
6684 -- warning. Furthermore, we will try later to detect some
6685 -- cases here at run time by expanding checking code (see
6686 -- Detect_Infinite_Recursion in package Exp_Ch6).
6688 -- If the recursive call is within a handler, do not emit a
6689 -- warning, because this is a common idiom: loop until input
6690 -- is correct, catch illegal input in handler and restart.
6692 if No (First_Formal (Nam))
6693 and then Etype (Nam) = Standard_Void_Type
6694 and then not Error_Posted (N)
6695 and then Nkind (Parent (N)) /= N_Exception_Handler
6697 -- For the case of a procedure call. We give the message
6698 -- only if the call is the first statement in a sequence
6699 -- of statements, or if all previous statements are
6700 -- simple assignments. This is simply a heuristic to
6701 -- decrease false positives, without losing too many good
6702 -- warnings. The idea is that these previous statements
6703 -- may affect global variables the procedure depends on.
6704 -- We also exclude raise statements, that may arise from
6705 -- constraint checks and are probably unrelated to the
6706 -- intended control flow.
6708 if Nkind (N) = N_Procedure_Call_Statement
6709 and then Is_List_Member (N)
6715 while Present (P) loop
6716 if not Nkind_In (P, N_Assignment_Statement,
6717 N_Raise_Constraint_Error)
6727 -- Do not give warning if we are in a conditional context
6730 K : constant Node_Kind := Nkind (Parent (N));
6732 if (K = N_Loop_Statement
6733 and then Present (Iteration_Scheme (Parent (N))))
6734 or else K = N_If_Statement
6735 or else K = N_Elsif_Part
6736 or else K = N_Case_Statement_Alternative
6742 -- Here warning is to be issued
6744 Set_Has_Recursive_Call (Nam);
6745 Error_Msg_Warn := SPARK_Mode /= On;
6746 Error_Msg_N ("possible infinite recursion<<!", N);
6747 Error_Msg_N ("\Storage_Error ]<<!", N);
6753 Scop := Scope (Scop);
6754 end loop Scope_Loop;
6758 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6760 Check_Obsolescent_2005_Entity (Nam, Subp);
6762 -- If subprogram name is a predefined operator, it was given in
6763 -- functional notation. Replace call node with operator node, so
6764 -- that actuals can be resolved appropriately.
6766 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
6767 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
6770 elsif Present (Alias (Nam))
6771 and then Is_Predefined_Op (Alias (Nam))
6773 Resolve_Actuals (N, Nam);
6774 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6778 -- Create a transient scope if the resulting type requires it
6780 -- There are several notable exceptions:
6782 -- a) In init procs, the transient scope overhead is not needed, and is
6783 -- even incorrect when the call is a nested initialization call for a
6784 -- component whose expansion may generate adjust calls. However, if the
6785 -- call is some other procedure call within an initialization procedure
6786 -- (for example a call to Create_Task in the init_proc of the task
6787 -- run-time record) a transient scope must be created around this call.
6789 -- b) Enumeration literal pseudo-calls need no transient scope
6791 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
6792 -- functions) do not use the secondary stack even though the return
6793 -- type may be unconstrained.
6795 -- d) Calls to a build-in-place function, since such functions may
6796 -- allocate their result directly in a target object, and cases where
6797 -- the result does get allocated in the secondary stack are checked for
6798 -- within the specialized Exp_Ch6 procedures for expanding those
6799 -- build-in-place calls.
6801 -- e) Calls to inlinable expression functions do not use the secondary
6802 -- stack (since the call will be replaced by its returned object).
6804 -- f) If the subprogram is marked Inline_Always, then even if it returns
6805 -- an unconstrained type the call does not require use of the secondary
6806 -- stack. However, inlining will only take place if the body to inline
6807 -- is already present. It may not be available if e.g. the subprogram is
6808 -- declared in a child instance.
6811 and then Has_Pragma_Inline (Nam)
6812 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
6813 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
6817 elsif Ekind (Nam) = E_Enumeration_Literal
6818 or else Is_Build_In_Place_Function (Nam)
6819 or else Is_Intrinsic_Subprogram (Nam)
6820 or else Is_Inlinable_Expression_Function (Nam)
6824 -- A return statement from an ignored Ghost function does not use the
6825 -- secondary stack (or any other one).
6827 elsif Expander_Active
6828 and then Ekind_In (Nam, E_Function, E_Subprogram_Type)
6829 and then Requires_Transient_Scope (Etype (Nam))
6830 and then not Is_Ignored_Ghost_Entity (Nam)
6832 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
6834 -- If the call appears within the bounds of a loop, it will be
6835 -- rewritten and reanalyzed, nothing left to do here.
6837 if Nkind (N) /= N_Function_Call then
6842 -- A protected function cannot be called within the definition of the
6843 -- enclosing protected type, unless it is part of a pre/postcondition
6844 -- on another protected operation. This may appear in the entry wrapper
6845 -- created for an entry with preconditions.
6847 if Is_Protected_Type (Scope (Nam))
6848 and then In_Open_Scopes (Scope (Nam))
6849 and then not Has_Completion (Scope (Nam))
6850 and then not In_Spec_Expression
6851 and then not Is_Entry_Wrapper (Current_Scope)
6854 ("& cannot be called before end of protected definition", N, Nam);
6857 -- Propagate interpretation to actuals, and add default expressions
6860 if Present (First_Formal (Nam)) then
6861 Resolve_Actuals (N, Nam);
6863 -- Overloaded literals are rewritten as function calls, for purpose of
6864 -- resolution. After resolution, we can replace the call with the
6867 elsif Ekind (Nam) = E_Enumeration_Literal then
6868 Copy_Node (Subp, N);
6869 Resolve_Entity_Name (N, Typ);
6871 -- Avoid validation, since it is a static function call
6873 Generate_Reference (Nam, Subp);
6877 -- If the subprogram is not global, then kill all saved values and
6878 -- checks. This is a bit conservative, since in many cases we could do
6879 -- better, but it is not worth the effort. Similarly, we kill constant
6880 -- values. However we do not need to do this for internal entities
6881 -- (unless they are inherited user-defined subprograms), since they
6882 -- are not in the business of molesting local values.
6884 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6885 -- kill all checks and values for calls to global subprograms. This
6886 -- takes care of the case where an access to a local subprogram is
6887 -- taken, and could be passed directly or indirectly and then called
6888 -- from almost any context.
6890 -- Note: we do not do this step till after resolving the actuals. That
6891 -- way we still take advantage of the current value information while
6892 -- scanning the actuals.
6894 -- We suppress killing values if we are processing the nodes associated
6895 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6896 -- type kills all the values as part of analyzing the code that
6897 -- initializes the dispatch tables.
6899 if Inside_Freezing_Actions = 0
6900 and then (not Is_Library_Level_Entity (Nam)
6901 or else Suppress_Value_Tracking_On_Call
6902 (Nearest_Dynamic_Scope (Current_Scope)))
6903 and then (Comes_From_Source (Nam)
6904 or else (Present (Alias (Nam))
6905 and then Comes_From_Source (Alias (Nam))))
6907 Kill_Current_Values;
6910 -- If we are warning about unread OUT parameters, this is the place to
6911 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
6912 -- after the above call to Kill_Current_Values (since that call clears
6913 -- the Last_Assignment field of all local variables).
6915 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
6916 and then Comes_From_Source (N)
6917 and then In_Extended_Main_Source_Unit (N)
6924 F := First_Formal (Nam);
6925 A := First_Actual (N);
6926 while Present (F) and then Present (A) loop
6927 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
6928 and then Warn_On_Modified_As_Out_Parameter (F)
6929 and then Is_Entity_Name (A)
6930 and then Present (Entity (A))
6931 and then Comes_From_Source (N)
6932 and then Safe_To_Capture_Value (N, Entity (A))
6934 Set_Last_Assignment (Entity (A), A);
6943 -- If the subprogram is a primitive operation, check whether or not
6944 -- it is a correct dispatching call.
6946 if Is_Overloadable (Nam)
6947 and then Is_Dispatching_Operation (Nam)
6949 Check_Dispatching_Call (N);
6951 elsif Ekind (Nam) /= E_Subprogram_Type
6952 and then Is_Abstract_Subprogram (Nam)
6953 and then not In_Instance
6955 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
6958 -- If this is a dispatching call, generate the appropriate reference,
6959 -- for better source navigation in GNAT Studio.
6961 if Is_Overloadable (Nam)
6962 and then Present (Controlling_Argument (N))
6964 Generate_Reference (Nam, Subp, 'R');
6966 -- Normal case, not a dispatching call: generate a call reference
6969 Generate_Reference (Nam, Subp, 's');
6972 if Is_Intrinsic_Subprogram (Nam) then
6973 Check_Intrinsic_Call (N);
6976 -- Check for violation of restriction No_Specific_Termination_Handlers
6977 -- and warn on a potentially blocking call to Abort_Task.
6979 if Restriction_Check_Required (No_Specific_Termination_Handlers)
6980 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
6982 Is_RTE (Nam, RE_Specific_Handler))
6984 Check_Restriction (No_Specific_Termination_Handlers, N);
6986 elsif Is_RTE (Nam, RE_Abort_Task) then
6987 Check_Potentially_Blocking_Operation (N);
6990 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
6991 -- timing event violates restriction No_Relative_Delay (AI-0211). We
6992 -- need to check the second argument to determine whether it is an
6993 -- absolute or relative timing event.
6995 if Restriction_Check_Required (No_Relative_Delay)
6996 and then Is_RTE (Nam, RE_Set_Handler)
6997 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
6999 Check_Restriction (No_Relative_Delay, N);
7002 -- Issue an error for a call to an eliminated subprogram. This routine
7003 -- will not perform the check if the call appears within a default
7006 Check_For_Eliminated_Subprogram (Subp, Nam);
7008 -- In formal mode, the primitive operations of a tagged type or type
7009 -- extension do not include functions that return the tagged type.
7011 if Nkind (N) = N_Function_Call
7012 and then Is_Tagged_Type (Etype (N))
7013 and then Is_Entity_Name (Name (N))
7014 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
7016 Check_SPARK_05_Restriction ("function not inherited", N);
7019 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7020 -- class-wide and the call dispatches on result in a context that does
7021 -- not provide a tag, the call raises Program_Error.
7023 if Nkind (N) = N_Function_Call
7024 and then In_Instance
7025 and then Is_Generic_Actual_Type (Typ)
7026 and then Is_Class_Wide_Type (Typ)
7027 and then Has_Controlling_Result (Nam)
7028 and then Nkind (Parent (N)) = N_Object_Declaration
7030 -- Verify that none of the formals are controlling
7033 Call_OK : Boolean := False;
7037 F := First_Formal (Nam);
7038 while Present (F) loop
7039 if Is_Controlling_Formal (F) then
7048 Error_Msg_Warn := SPARK_Mode /= On;
7049 Error_Msg_N ("!cannot determine tag of result<<", N);
7050 Error_Msg_N ("\Program_Error [<<!", N);
7052 Make_Raise_Program_Error (Sloc (N),
7053 Reason => PE_Explicit_Raise));
7058 -- Check for calling a function with OUT or IN OUT parameter when the
7059 -- calling context (us right now) is not Ada 2012, so does not allow
7060 -- OUT or IN OUT parameters in function calls. Functions declared in
7061 -- a predefined unit are OK, as they may be called indirectly from a
7062 -- user-declared instantiation.
7064 if Ada_Version < Ada_2012
7065 and then Ekind (Nam) = E_Function
7066 and then Has_Out_Or_In_Out_Parameter (Nam)
7067 and then not In_Predefined_Unit (Nam)
7069 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7070 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7073 -- Check the dimensions of the actuals in the call. For function calls,
7074 -- propagate the dimensions from the returned type to N.
7076 Analyze_Dimension_Call (N, Nam);
7078 -- All done, evaluate call and deal with elaboration issues
7082 if Legacy_Elaboration_Checks then
7083 Check_Elab_Call (N);
7086 -- Annotate the tree by creating a call marker in case the original call
7087 -- is transformed by expansion. The call marker is automatically saved
7088 -- for later examination by the ABE Processing phase.
7090 Build_Call_Marker (N);
7092 Mark_Use_Clauses (Subp);
7094 Warn_On_Overlapping_Actuals (Nam, N);
7096 -- In GNATprove mode, expansion is disabled, but we want to inline some
7097 -- subprograms to facilitate formal verification. Indirect calls through
7098 -- a subprogram type or within a generic cannot be inlined. Inlining is
7099 -- performed only for calls subject to SPARK_Mode on.
7102 and then SPARK_Mode = On
7103 and then Is_Overloadable (Nam)
7104 and then not Inside_A_Generic
7106 Nam_UA := Ultimate_Alias (Nam);
7107 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7109 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7110 Body_Id := Corresponding_Body (Nam_Decl);
7112 -- Nothing to do if the subprogram is not eligible for inlining in
7113 -- GNATprove mode, or inlining is disabled with switch -gnatdm
7115 if not Is_Inlined_Always (Nam_UA)
7116 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7117 or else Debug_Flag_M
7121 -- Calls cannot be inlined inside assertions, as GNATprove treats
7122 -- assertions as logic expressions. Only issue a message when the
7123 -- body has been seen, otherwise this leads to spurious messages
7124 -- on expression functions.
7126 elsif In_Assertion_Expr /= 0 then
7127 if Present (Body_Id) then
7129 ("cannot inline & (in assertion expression)?", N, Nam_UA);
7132 -- Calls cannot be inlined inside default expressions
7134 elsif In_Default_Expr then
7136 ("cannot inline & (in default expression)?", N, Nam_UA);
7138 -- Calls cannot be inlined inside quantified expressions, which
7139 -- are left in expression form for GNATprove. Since these
7140 -- expressions are only preanalyzed, we need to detect the failure
7141 -- to inline outside of the case for Full_Analysis below.
7143 elsif In_Quantified_Expression (N) then
7145 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7147 -- Inlining should not be performed during preanalysis
7149 elsif Full_Analysis then
7151 -- Do not inline calls inside expression functions or functions
7152 -- generated by the front end for subtype predicates, as this
7153 -- would prevent interpreting them as logical formulas in
7154 -- GNATprove. Only issue a message when the body has been seen,
7155 -- otherwise this leads to spurious messages on callees that
7156 -- are themselves expression functions.
7158 if Present (Current_Subprogram)
7160 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7161 or else Is_Predicate_Function (Current_Subprogram)
7162 or else Is_Invariant_Procedure (Current_Subprogram)
7163 or else Is_DIC_Procedure (Current_Subprogram))
7165 if Present (Body_Id)
7166 and then Present (Body_To_Inline (Nam_Decl))
7168 if Is_Predicate_Function (Current_Subprogram) then
7170 ("cannot inline & (inside predicate)?",
7173 elsif Is_Invariant_Procedure (Current_Subprogram) then
7175 ("cannot inline & (inside invariant)?",
7178 elsif Is_DIC_Procedure (Current_Subprogram) then
7180 ("cannot inline & (inside Default_Initial_Condition)?",
7185 ("cannot inline & (inside expression function)?",
7190 -- Cannot inline a call inside the definition of a record type,
7191 -- typically inside the constraints of the type. Calls in
7192 -- default expressions are also not inlined, but this is
7193 -- filtered out above when testing In_Default_Expr.
7195 elsif Is_Record_Type (Current_Scope) then
7197 ("cannot inline & (inside record type)?", N, Nam_UA);
7199 -- With the one-pass inlining technique, a call cannot be
7200 -- inlined if the corresponding body has not been seen yet.
7202 elsif No (Body_Id) then
7204 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7206 -- Nothing to do if there is no body to inline, indicating that
7207 -- the subprogram is not suitable for inlining in GNATprove
7210 elsif No (Body_To_Inline (Nam_Decl)) then
7213 -- Calls cannot be inlined inside potentially unevaluated
7214 -- expressions, as this would create complex actions inside
7215 -- expressions, that are not handled by GNATprove.
7217 elsif Is_Potentially_Unevaluated (N) then
7219 ("cannot inline & (in potentially unevaluated context)?",
7222 -- Calls cannot be inlined inside the conditions of while
7223 -- loops, as this would create complex actions inside
7224 -- the condition, that are not handled by GNATprove.
7226 elsif In_While_Loop_Condition (N) then
7228 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7230 -- Do not inline calls which would possibly lead to missing a
7231 -- type conversion check on an input parameter.
7233 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7235 ("cannot inline & (possible check on input parameters)?",
7238 -- Otherwise, inline the call, issuing an info message when
7242 if Debug_Flag_Underscore_F then
7244 ("info: analyzing call to & in context?", N, Nam_UA);
7247 Expand_Inlined_Call (N, Nam_UA, Nam);
7254 -----------------------------
7255 -- Resolve_Case_Expression --
7256 -----------------------------
7258 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7261 Alt_Typ : Entity_Id;
7265 Alt := First (Alternatives (N));
7266 while Present (Alt) loop
7267 Alt_Expr := Expression (Alt);
7269 if Error_Posted (Alt_Expr) then
7273 Resolve (Alt_Expr, Typ);
7274 Alt_Typ := Etype (Alt_Expr);
7276 -- When the expression is of a scalar subtype different from the
7277 -- result subtype, then insert a conversion to ensure the generation
7278 -- of a constraint check.
7280 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7281 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7282 Analyze_And_Resolve (Alt_Expr, Typ);
7288 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7289 -- dynamically tagged must be known statically.
7291 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7292 Alt := First (Alternatives (N));
7293 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7295 while Present (Alt) loop
7296 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7298 ("all or none of the dependent expressions can be "
7299 & "dynamically tagged", N);
7307 Eval_Case_Expression (N);
7308 Analyze_Dimension (N);
7309 end Resolve_Case_Expression;
7311 -------------------------------
7312 -- Resolve_Character_Literal --
7313 -------------------------------
7315 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7316 B_Typ : constant Entity_Id := Base_Type (Typ);
7320 -- Verify that the character does belong to the type of the context
7322 Set_Etype (N, B_Typ);
7323 Eval_Character_Literal (N);
7325 -- Wide_Wide_Character literals must always be defined, since the set
7326 -- of wide wide character literals is complete, i.e. if a character
7327 -- literal is accepted by the parser, then it is OK for wide wide
7328 -- character (out of range character literals are rejected).
7330 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7333 -- Always accept character literal for type Any_Character, which
7334 -- occurs in error situations and in comparisons of literals, both
7335 -- of which should accept all literals.
7337 elsif B_Typ = Any_Character then
7340 -- For Standard.Character or a type derived from it, check that the
7341 -- literal is in range.
7343 elsif Root_Type (B_Typ) = Standard_Character then
7344 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7348 -- For Standard.Wide_Character or a type derived from it, check that the
7349 -- literal is in range.
7351 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7352 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7356 -- If the entity is already set, this has already been resolved in a
7357 -- generic context, or comes from expansion. Nothing else to do.
7359 elsif Present (Entity (N)) then
7362 -- Otherwise we have a user defined character type, and we can use the
7363 -- standard visibility mechanisms to locate the referenced entity.
7366 C := Current_Entity (N);
7367 while Present (C) loop
7368 if Etype (C) = B_Typ then
7369 Set_Entity_With_Checks (N, C);
7370 Generate_Reference (C, N);
7378 -- If we fall through, then the literal does not match any of the
7379 -- entries of the enumeration type. This isn't just a constraint error
7380 -- situation, it is an illegality (see RM 4.2).
7383 ("character not defined for }", N, First_Subtype (B_Typ));
7384 end Resolve_Character_Literal;
7386 ---------------------------
7387 -- Resolve_Comparison_Op --
7388 ---------------------------
7390 -- Context requires a boolean type, and plays no role in resolution.
7391 -- Processing identical to that for equality operators. The result type is
7392 -- the base type, which matters when pathological subtypes of booleans with
7393 -- limited ranges are used.
7395 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7396 L : constant Node_Id := Left_Opnd (N);
7397 R : constant Node_Id := Right_Opnd (N);
7401 -- If this is an intrinsic operation which is not predefined, use the
7402 -- types of its declared arguments to resolve the possibly overloaded
7403 -- operands. Otherwise the operands are unambiguous and specify the
7406 if Scope (Entity (N)) /= Standard_Standard then
7407 T := Etype (First_Entity (Entity (N)));
7410 T := Find_Unique_Type (L, R);
7412 if T = Any_Fixed then
7413 T := Unique_Fixed_Point_Type (L);
7417 Set_Etype (N, Base_Type (Typ));
7418 Generate_Reference (T, N, ' ');
7420 -- Skip remaining processing if already set to Any_Type
7422 if T = Any_Type then
7426 -- Deal with other error cases
7428 if T = Any_String or else
7429 T = Any_Composite or else
7432 if T = Any_Character then
7433 Ambiguous_Character (L);
7435 Error_Msg_N ("ambiguous operands for comparison", N);
7438 Set_Etype (N, Any_Type);
7442 -- Resolve the operands if types OK
7446 Check_Unset_Reference (L);
7447 Check_Unset_Reference (R);
7448 Generate_Operator_Reference (N, T);
7449 Check_Low_Bound_Tested (N);
7451 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
7452 -- types or array types except String.
7454 if Is_Boolean_Type (T) then
7455 Check_SPARK_05_Restriction
7456 ("comparison is not defined on Boolean type", N);
7458 elsif Is_Array_Type (T)
7459 and then Base_Type (T) /= Standard_String
7461 Check_SPARK_05_Restriction
7462 ("comparison is not defined on array types other than String", N);
7465 -- Check comparison on unordered enumeration
7467 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7468 Error_Msg_Sloc := Sloc (Etype (L));
7470 ("comparison on unordered enumeration type& declared#?U?",
7474 Analyze_Dimension (N);
7476 -- Evaluate the relation (note we do this after the above check since
7477 -- this Eval call may change N to True/False. Skip this evaluation
7478 -- inside assertions, in order to keep assertions as written by users
7479 -- for tools that rely on these, e.g. GNATprove for loop invariants.
7480 -- Except evaluation is still performed even inside assertions for
7481 -- comparisons between values of universal type, which are useless
7482 -- for static analysis tools, and not supported even by GNATprove.
7484 if In_Assertion_Expr = 0
7485 or else (Is_Universal_Numeric_Type (Etype (L))
7487 Is_Universal_Numeric_Type (Etype (R)))
7489 Eval_Relational_Op (N);
7491 end Resolve_Comparison_Op;
7493 -----------------------------------------
7494 -- Resolve_Discrete_Subtype_Indication --
7495 -----------------------------------------
7497 procedure Resolve_Discrete_Subtype_Indication
7505 Analyze (Subtype_Mark (N));
7506 S := Entity (Subtype_Mark (N));
7508 if Nkind (Constraint (N)) /= N_Range_Constraint then
7509 Error_Msg_N ("expect range constraint for discrete type", N);
7510 Set_Etype (N, Any_Type);
7513 R := Range_Expression (Constraint (N));
7521 if Base_Type (S) /= Base_Type (Typ) then
7523 ("expect subtype of }", N, First_Subtype (Typ));
7525 -- Rewrite the constraint as a range of Typ
7526 -- to allow compilation to proceed further.
7529 Rewrite (Low_Bound (R),
7530 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7531 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7532 Attribute_Name => Name_First));
7533 Rewrite (High_Bound (R),
7534 Make_Attribute_Reference (Sloc (High_Bound (R)),
7535 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7536 Attribute_Name => Name_First));
7540 Set_Etype (N, Etype (R));
7542 -- Additionally, we must check that the bounds are compatible
7543 -- with the given subtype, which might be different from the
7544 -- type of the context.
7546 Apply_Range_Check (R, S);
7548 -- ??? If the above check statically detects a Constraint_Error
7549 -- it replaces the offending bound(s) of the range R with a
7550 -- Constraint_Error node. When the itype which uses these bounds
7551 -- is frozen the resulting call to Duplicate_Subexpr generates
7552 -- a new temporary for the bounds.
7554 -- Unfortunately there are other itypes that are also made depend
7555 -- on these bounds, so when Duplicate_Subexpr is called they get
7556 -- a forward reference to the newly created temporaries and Gigi
7557 -- aborts on such forward references. This is probably sign of a
7558 -- more fundamental problem somewhere else in either the order of
7559 -- itype freezing or the way certain itypes are constructed.
7561 -- To get around this problem we call Remove_Side_Effects right
7562 -- away if either bounds of R are a Constraint_Error.
7565 L : constant Node_Id := Low_Bound (R);
7566 H : constant Node_Id := High_Bound (R);
7569 if Nkind (L) = N_Raise_Constraint_Error then
7570 Remove_Side_Effects (L);
7573 if Nkind (H) = N_Raise_Constraint_Error then
7574 Remove_Side_Effects (H);
7578 Check_Unset_Reference (Low_Bound (R));
7579 Check_Unset_Reference (High_Bound (R));
7582 end Resolve_Discrete_Subtype_Indication;
7584 -------------------------
7585 -- Resolve_Entity_Name --
7586 -------------------------
7588 -- Used to resolve identifiers and expanded names
7590 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7591 function Is_Assignment_Or_Object_Expression
7593 Expr : Node_Id) return Boolean;
7594 -- Determine whether node Context denotes an assignment statement or an
7595 -- object declaration whose expression is node Expr.
7597 ----------------------------------------
7598 -- Is_Assignment_Or_Object_Expression --
7599 ----------------------------------------
7601 function Is_Assignment_Or_Object_Expression
7603 Expr : Node_Id) return Boolean
7606 if Nkind_In (Context, N_Assignment_Statement,
7607 N_Object_Declaration)
7608 and then Expression (Context) = Expr
7612 -- Check whether a construct that yields a name is the expression of
7613 -- an assignment statement or an object declaration.
7615 elsif (Nkind_In (Context, N_Attribute_Reference,
7616 N_Explicit_Dereference,
7617 N_Indexed_Component,
7618 N_Selected_Component,
7620 and then Prefix (Context) = Expr)
7622 (Nkind_In (Context, N_Type_Conversion,
7623 N_Unchecked_Type_Conversion)
7624 and then Expression (Context) = Expr)
7627 Is_Assignment_Or_Object_Expression
7628 (Context => Parent (Context),
7631 -- Otherwise the context is not an assignment statement or an object
7637 end Is_Assignment_Or_Object_Expression;
7641 E : constant Entity_Id := Entity (N);
7644 -- Start of processing for Resolve_Entity_Name
7647 -- If garbage from errors, set to Any_Type and return
7649 if No (E) and then Total_Errors_Detected /= 0 then
7650 Set_Etype (N, Any_Type);
7654 -- Replace named numbers by corresponding literals. Note that this is
7655 -- the one case where Resolve_Entity_Name must reset the Etype, since
7656 -- it is currently marked as universal.
7658 if Ekind (E) = E_Named_Integer then
7660 Eval_Named_Integer (N);
7662 elsif Ekind (E) = E_Named_Real then
7664 Eval_Named_Real (N);
7666 -- For enumeration literals, we need to make sure that a proper style
7667 -- check is done, since such literals are overloaded, and thus we did
7668 -- not do a style check during the first phase of analysis.
7670 elsif Ekind (E) = E_Enumeration_Literal then
7671 Set_Entity_With_Checks (N, E);
7672 Eval_Entity_Name (N);
7674 -- Case of (sub)type name appearing in a context where an expression
7675 -- is expected. This is legal if occurrence is a current instance.
7676 -- See RM 8.6 (17/3).
7678 elsif Is_Type (E) then
7679 if Is_Current_Instance (N) then
7682 -- Any other use is an error
7686 ("invalid use of subtype mark in expression or call", N);
7689 -- Check discriminant use if entity is discriminant in current scope,
7690 -- i.e. discriminant of record or concurrent type currently being
7691 -- analyzed. Uses in corresponding body are unrestricted.
7693 elsif Ekind (E) = E_Discriminant
7694 and then Scope (E) = Current_Scope
7695 and then not Has_Completion (Current_Scope)
7697 Check_Discriminant_Use (N);
7699 -- A parameterless generic function cannot appear in a context that
7700 -- requires resolution.
7702 elsif Ekind (E) = E_Generic_Function then
7703 Error_Msg_N ("illegal use of generic function", N);
7705 -- In Ada 83 an OUT parameter cannot be read, but attributes of
7706 -- array types (i.e. bounds and length) are legal.
7708 elsif Ekind (E) = E_Out_Parameter
7709 and then (Nkind (Parent (N)) /= N_Attribute_Reference
7710 or else Is_Scalar_Type (Etype (E)))
7712 and then (Nkind (Parent (N)) in N_Op
7713 or else Nkind (Parent (N)) = N_Explicit_Dereference
7714 or else Is_Assignment_Or_Object_Expression
7715 (Context => Parent (N),
7718 if Ada_Version = Ada_83 then
7719 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
7722 -- In all other cases, just do the possible static evaluation
7725 -- A deferred constant that appears in an expression must have a
7726 -- completion, unless it has been removed by in-place expansion of
7727 -- an aggregate. A constant that is a renaming does not need
7730 if Ekind (E) = E_Constant
7731 and then Comes_From_Source (E)
7732 and then No (Constant_Value (E))
7733 and then Is_Frozen (Etype (E))
7734 and then not In_Spec_Expression
7735 and then not Is_Imported (E)
7736 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7738 if No_Initialization (Parent (E))
7739 or else (Present (Full_View (E))
7740 and then No_Initialization (Parent (Full_View (E))))
7745 ("deferred constant is frozen before completion", N);
7749 Eval_Entity_Name (N);
7754 -- When the entity appears in a parameter association, retrieve the
7755 -- related subprogram call.
7757 if Nkind (Par) = N_Parameter_Association then
7758 Par := Parent (Par);
7761 if Comes_From_Source (N) then
7763 -- The following checks are only relevant when SPARK_Mode is on as
7764 -- they are not standard Ada legality rules.
7766 if SPARK_Mode = On then
7768 -- An effectively volatile object subject to enabled properties
7769 -- Async_Writers or Effective_Reads must appear in non-interfering
7770 -- context (SPARK RM 7.1.3(12)).
7773 and then Is_Effectively_Volatile (E)
7774 and then (Async_Writers_Enabled (E)
7775 or else Effective_Reads_Enabled (E))
7776 and then not Is_OK_Volatile_Context (Par, N)
7779 ("volatile object cannot appear in this context "
7780 & "(SPARK RM 7.1.3(12))", N);
7783 -- Check for possible elaboration issues with respect to reads of
7784 -- variables. The act of renaming the variable is not considered a
7785 -- read as it simply establishes an alias.
7787 if Legacy_Elaboration_Checks
7788 and then Ekind (E) = E_Variable
7789 and then Dynamic_Elaboration_Checks
7790 and then Nkind (Par) /= N_Object_Renaming_Declaration
7792 Check_Elab_Call (N);
7796 -- The variable may eventually become a constituent of a single
7797 -- protected/task type. Record the reference now and verify its
7798 -- legality when analyzing the contract of the variable
7801 if Ekind (E) = E_Variable then
7802 Record_Possible_Part_Of_Reference (E, N);
7805 -- A Ghost entity must appear in a specific context
7807 if Is_Ghost_Entity (E) then
7808 Check_Ghost_Context (E, N);
7812 -- We may be resolving an entity within expanded code, so a reference to
7813 -- an entity should be ignored when calculating effective use clauses to
7814 -- avoid inappropriate marking.
7816 if Comes_From_Source (N) then
7817 Mark_Use_Clauses (E);
7819 end Resolve_Entity_Name;
7825 procedure Resolve_Entry (Entry_Name : Node_Id) is
7826 Loc : constant Source_Ptr := Sloc (Entry_Name);
7834 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
7835 -- If the bounds of the entry family being called depend on task
7836 -- discriminants, build a new index subtype where a discriminant is
7837 -- replaced with the value of the discriminant of the target task.
7838 -- The target task is the prefix of the entry name in the call.
7840 -----------------------
7841 -- Actual_Index_Type --
7842 -----------------------
7844 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
7845 Typ : constant Entity_Id := Entry_Index_Type (E);
7846 Tsk : constant Entity_Id := Scope (E);
7847 Lo : constant Node_Id := Type_Low_Bound (Typ);
7848 Hi : constant Node_Id := Type_High_Bound (Typ);
7851 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
7852 -- If the bound is given by a discriminant, replace with a reference
7853 -- to the discriminant of the same name in the target task. If the
7854 -- entry name is the target of a requeue statement and the entry is
7855 -- in the current protected object, the bound to be used is the
7856 -- discriminal of the object (see Apply_Range_Checks for details of
7857 -- the transformation).
7859 -----------------------------
7860 -- Actual_Discriminant_Ref --
7861 -----------------------------
7863 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
7864 Typ : constant Entity_Id := Etype (Bound);
7868 Remove_Side_Effects (Bound);
7870 if not Is_Entity_Name (Bound)
7871 or else Ekind (Entity (Bound)) /= E_Discriminant
7875 elsif Is_Protected_Type (Tsk)
7876 and then In_Open_Scopes (Tsk)
7877 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
7879 -- Note: here Bound denotes a discriminant of the corresponding
7880 -- record type tskV, whose discriminal is a formal of the
7881 -- init-proc tskVIP. What we want is the body discriminal,
7882 -- which is associated to the discriminant of the original
7883 -- concurrent type tsk.
7885 return New_Occurrence_Of
7886 (Find_Body_Discriminal (Entity (Bound)), Loc);
7890 Make_Selected_Component (Loc,
7891 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
7892 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
7897 end Actual_Discriminant_Ref;
7899 -- Start of processing for Actual_Index_Type
7902 if not Has_Discriminants (Tsk)
7903 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
7905 return Entry_Index_Type (E);
7908 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
7909 Set_Etype (New_T, Base_Type (Typ));
7910 Set_Size_Info (New_T, Typ);
7911 Set_RM_Size (New_T, RM_Size (Typ));
7912 Set_Scalar_Range (New_T,
7913 Make_Range (Sloc (Entry_Name),
7914 Low_Bound => Actual_Discriminant_Ref (Lo),
7915 High_Bound => Actual_Discriminant_Ref (Hi)));
7919 end Actual_Index_Type;
7921 -- Start of processing for Resolve_Entry
7924 -- Find name of entry being called, and resolve prefix of name with its
7925 -- own type. The prefix can be overloaded, and the name and signature of
7926 -- the entry must be taken into account.
7928 if Nkind (Entry_Name) = N_Indexed_Component then
7930 -- Case of dealing with entry family within the current tasks
7932 E_Name := Prefix (Entry_Name);
7935 E_Name := Entry_Name;
7938 if Is_Entity_Name (E_Name) then
7940 -- Entry call to an entry (or entry family) in the current task. This
7941 -- is legal even though the task will deadlock. Rewrite as call to
7944 -- This can also be a call to an entry in an enclosing task. If this
7945 -- is a single task, we have to retrieve its name, because the scope
7946 -- of the entry is the task type, not the object. If the enclosing
7947 -- task is a task type, the identity of the task is given by its own
7950 -- Finally this can be a requeue on an entry of the same task or
7951 -- protected object.
7953 S := Scope (Entity (E_Name));
7955 for J in reverse 0 .. Scope_Stack.Last loop
7956 if Is_Task_Type (Scope_Stack.Table (J).Entity)
7957 and then not Comes_From_Source (S)
7959 -- S is an enclosing task or protected object. The concurrent
7960 -- declaration has been converted into a type declaration, and
7961 -- the object itself has an object declaration that follows
7962 -- the type in the same declarative part.
7964 Tsk := Next_Entity (S);
7965 while Etype (Tsk) /= S loop
7972 elsif S = Scope_Stack.Table (J).Entity then
7974 -- Call to current task. Will be transformed into call to Self
7982 Make_Selected_Component (Loc,
7983 Prefix => New_Occurrence_Of (S, Loc),
7985 New_Occurrence_Of (Entity (E_Name), Loc));
7986 Rewrite (E_Name, New_N);
7989 elsif Nkind (Entry_Name) = N_Selected_Component
7990 and then Is_Overloaded (Prefix (Entry_Name))
7992 -- Use the entry name (which must be unique at this point) to find
7993 -- the prefix that returns the corresponding task/protected type.
7996 Pref : constant Node_Id := Prefix (Entry_Name);
7997 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
8002 Get_First_Interp (Pref, I, It);
8003 while Present (It.Typ) loop
8004 if Scope (Ent) = It.Typ then
8005 Set_Etype (Pref, It.Typ);
8009 Get_Next_Interp (I, It);
8014 if Nkind (Entry_Name) = N_Selected_Component then
8015 Resolve (Prefix (Entry_Name));
8017 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8018 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8019 Resolve (Prefix (Prefix (Entry_Name)));
8020 Index := First (Expressions (Entry_Name));
8021 Resolve (Index, Entry_Index_Type (Nam));
8023 -- Generate a reference for the index when it denotes an entity
8025 if Is_Entity_Name (Index) then
8026 Generate_Reference (Entity (Index), Nam);
8029 -- Up to this point the expression could have been the actual in a
8030 -- simple entry call, and be given by a named association.
8032 if Nkind (Index) = N_Parameter_Association then
8033 Error_Msg_N ("expect expression for entry index", Index);
8035 Apply_Range_Check (Index, Actual_Index_Type (Nam));
8040 ------------------------
8041 -- Resolve_Entry_Call --
8042 ------------------------
8044 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8045 Entry_Name : constant Node_Id := Name (N);
8046 Loc : constant Source_Ptr := Sloc (Entry_Name);
8054 -- We kill all checks here, because it does not seem worth the effort to
8055 -- do anything better, an entry call is a big operation.
8059 -- Processing of the name is similar for entry calls and protected
8060 -- operation calls. Once the entity is determined, we can complete
8061 -- the resolution of the actuals.
8063 -- The selector may be overloaded, in the case of a protected object
8064 -- with overloaded functions. The type of the context is used for
8067 if Nkind (Entry_Name) = N_Selected_Component
8068 and then Is_Overloaded (Selector_Name (Entry_Name))
8069 and then Typ /= Standard_Void_Type
8076 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8077 while Present (It.Typ) loop
8078 if Covers (Typ, It.Typ) then
8079 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8080 Set_Etype (Entry_Name, It.Typ);
8082 Generate_Reference (It.Typ, N, ' ');
8085 Get_Next_Interp (I, It);
8090 Resolve_Entry (Entry_Name);
8092 if Nkind (Entry_Name) = N_Selected_Component then
8094 -- Simple entry or protected operation call
8096 Nam := Entity (Selector_Name (Entry_Name));
8097 Obj := Prefix (Entry_Name);
8099 if Is_Subprogram (Nam) then
8100 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8103 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8105 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8107 -- Call to member of entry family
8109 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8110 Obj := Prefix (Prefix (Entry_Name));
8111 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8114 -- We cannot in general check the maximum depth of protected entry calls
8115 -- at compile time. But we can tell that any protected entry call at all
8116 -- violates a specified nesting depth of zero.
8118 if Is_Protected_Type (Scope (Nam)) then
8119 Check_Restriction (Max_Entry_Queue_Length, N);
8122 -- Use context type to disambiguate a protected function that can be
8123 -- called without actuals and that returns an array type, and where the
8124 -- argument list may be an indexing of the returned value.
8126 if Ekind (Nam) = E_Function
8127 and then Needs_No_Actuals (Nam)
8128 and then Present (Parameter_Associations (N))
8130 ((Is_Array_Type (Etype (Nam))
8131 and then Covers (Typ, Component_Type (Etype (Nam))))
8133 or else (Is_Access_Type (Etype (Nam))
8134 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8138 Component_Type (Designated_Type (Etype (Nam))))))
8141 Index_Node : Node_Id;
8145 Make_Indexed_Component (Loc,
8147 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8148 Expressions => Parameter_Associations (N));
8150 -- Since we are correcting a node classification error made by the
8151 -- parser, we call Replace rather than Rewrite.
8153 Replace (N, Index_Node);
8154 Set_Etype (Prefix (N), Etype (Nam));
8156 Resolve_Indexed_Component (N, Typ);
8161 if Ekind_In (Nam, E_Entry, E_Entry_Family)
8162 and then Present (Contract_Wrapper (Nam))
8163 and then Current_Scope /= Contract_Wrapper (Nam)
8165 -- Note the entity being called before rewriting the call, so that
8166 -- it appears used at this point.
8168 Generate_Reference (Nam, Entry_Name, 'r');
8170 -- Rewrite as call to the precondition wrapper, adding the task
8171 -- object to the list of actuals. If the call is to a member of an
8172 -- entry family, include the index as well.
8176 New_Actuals : List_Id;
8179 New_Actuals := New_List (Obj);
8181 if Nkind (Entry_Name) = N_Indexed_Component then
8182 Append_To (New_Actuals,
8183 New_Copy_Tree (First (Expressions (Entry_Name))));
8186 Append_List (Parameter_Associations (N), New_Actuals);
8188 Make_Procedure_Call_Statement (Loc,
8190 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8191 Parameter_Associations => New_Actuals);
8192 Rewrite (N, New_Call);
8194 -- Preanalyze and resolve new call. Current procedure is called
8195 -- from Resolve_Call, after which expansion will take place.
8197 Preanalyze_And_Resolve (N);
8202 -- The operation name may have been overloaded. Order the actuals
8203 -- according to the formals of the resolved entity, and set the return
8204 -- type to that of the operation.
8207 Normalize_Actuals (N, Nam, False, Norm_OK);
8208 pragma Assert (Norm_OK);
8209 Set_Etype (N, Etype (Nam));
8211 -- Reset the Is_Overloaded flag, since resolution is now completed
8213 -- Simple entry call
8215 if Nkind (Entry_Name) = N_Selected_Component then
8216 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8218 -- Call to a member of an entry family
8220 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8221 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8225 Resolve_Actuals (N, Nam);
8226 Check_Internal_Protected_Use (N, Nam);
8228 -- Create a call reference to the entry
8230 Generate_Reference (Nam, Entry_Name, 's');
8232 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
8233 Check_Potentially_Blocking_Operation (N);
8236 -- Verify that a procedure call cannot masquerade as an entry
8237 -- call where an entry call is expected.
8239 if Ekind (Nam) = E_Procedure then
8240 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8241 and then N = Entry_Call_Statement (Parent (N))
8243 Error_Msg_N ("entry call required in select statement", N);
8245 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8246 and then N = Triggering_Statement (Parent (N))
8248 Error_Msg_N ("triggering statement cannot be procedure call", N);
8250 elsif Ekind (Scope (Nam)) = E_Task_Type
8251 and then not In_Open_Scopes (Scope (Nam))
8253 Error_Msg_N ("task has no entry with this name", Entry_Name);
8257 -- After resolution, entry calls and protected procedure calls are
8258 -- changed into entry calls, for expansion. The structure of the node
8259 -- does not change, so it can safely be done in place. Protected
8260 -- function calls must keep their structure because they are
8263 if Ekind (Nam) /= E_Function then
8265 -- A protected operation that is not a function may modify the
8266 -- corresponding object, and cannot apply to a constant. If this
8267 -- is an internal call, the prefix is the type itself.
8269 if Is_Protected_Type (Scope (Nam))
8270 and then not Is_Variable (Obj)
8271 and then (not Is_Entity_Name (Obj)
8272 or else not Is_Type (Entity (Obj)))
8275 ("prefix of protected procedure or entry call must be variable",
8280 Entry_Call : Node_Id;
8284 Make_Entry_Call_Statement (Loc,
8286 Parameter_Associations => Parameter_Associations (N));
8288 -- Inherit relevant attributes from the original call
8290 Set_First_Named_Actual
8291 (Entry_Call, First_Named_Actual (N));
8293 Set_Is_Elaboration_Checks_OK_Node
8294 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8296 Set_Is_Elaboration_Warnings_OK_Node
8297 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8299 Set_Is_SPARK_Mode_On_Node
8300 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8302 Rewrite (N, Entry_Call);
8303 Set_Analyzed (N, True);
8306 -- Protected functions can return on the secondary stack, in which case
8307 -- we must trigger the transient scope mechanism.
8309 elsif Expander_Active
8310 and then Requires_Transient_Scope (Etype (Nam))
8312 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
8314 end Resolve_Entry_Call;
8316 -------------------------
8317 -- Resolve_Equality_Op --
8318 -------------------------
8320 -- Both arguments must have the same type, and the boolean context does
8321 -- not participate in the resolution. The first pass verifies that the
8322 -- interpretation is not ambiguous, and the type of the left argument is
8323 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
8324 -- are strings or aggregates, allocators, or Null, they are ambiguous even
8325 -- though they carry a single (universal) type. Diagnose this case here.
8327 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8328 L : constant Node_Id := Left_Opnd (N);
8329 R : constant Node_Id := Right_Opnd (N);
8330 T : Entity_Id := Find_Unique_Type (L, R);
8332 procedure Check_If_Expression (Cond : Node_Id);
8333 -- The resolution rule for if expressions requires that each such must
8334 -- have a unique type. This means that if several dependent expressions
8335 -- are of a non-null anonymous access type, and the context does not
8336 -- impose an expected type (as can be the case in an equality operation)
8337 -- the expression must be rejected.
8339 procedure Explain_Redundancy (N : Node_Id);
8340 -- Attempt to explain the nature of a redundant comparison with True. If
8341 -- the expression N is too complex, this routine issues a general error
8344 function Find_Unique_Access_Type return Entity_Id;
8345 -- In the case of allocators and access attributes, the context must
8346 -- provide an indication of the specific access type to be used. If
8347 -- one operand is of such a "generic" access type, check whether there
8348 -- is a specific visible access type that has the same designated type.
8349 -- This is semantically dubious, and of no interest to any real code,
8350 -- but c48008a makes it all worthwhile.
8352 -------------------------
8353 -- Check_If_Expression --
8354 -------------------------
8356 procedure Check_If_Expression (Cond : Node_Id) is
8357 Then_Expr : Node_Id;
8358 Else_Expr : Node_Id;
8361 if Nkind (Cond) = N_If_Expression then
8362 Then_Expr := Next (First (Expressions (Cond)));
8363 Else_Expr := Next (Then_Expr);
8365 if Nkind (Then_Expr) /= N_Null
8366 and then Nkind (Else_Expr) /= N_Null
8368 Error_Msg_N ("cannot determine type of if expression", Cond);
8371 end Check_If_Expression;
8373 ------------------------
8374 -- Explain_Redundancy --
8375 ------------------------
8377 procedure Explain_Redundancy (N : Node_Id) is
8385 -- Strip the operand down to an entity
8388 if Nkind (Val) = N_Selected_Component then
8389 Val := Selector_Name (Val);
8395 -- The construct denotes an entity
8397 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8398 Val_Id := Entity (Val);
8400 -- Do not generate an error message when the comparison is done
8401 -- against the enumeration literal Standard.True.
8403 if Ekind (Val_Id) /= E_Enumeration_Literal then
8405 -- Build a customized error message
8408 Add_Str_To_Name_Buffer ("?r?");
8410 if Ekind (Val_Id) = E_Component then
8411 Add_Str_To_Name_Buffer ("component ");
8413 elsif Ekind (Val_Id) = E_Constant then
8414 Add_Str_To_Name_Buffer ("constant ");
8416 elsif Ekind (Val_Id) = E_Discriminant then
8417 Add_Str_To_Name_Buffer ("discriminant ");
8419 elsif Is_Formal (Val_Id) then
8420 Add_Str_To_Name_Buffer ("parameter ");
8422 elsif Ekind (Val_Id) = E_Variable then
8423 Add_Str_To_Name_Buffer ("variable ");
8426 Add_Str_To_Name_Buffer ("& is always True!");
8429 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8432 -- The construct is too complex to disect, issue a general message
8435 Error_Msg_N ("?r?expression is always True!", Val);
8437 end Explain_Redundancy;
8439 -----------------------------
8440 -- Find_Unique_Access_Type --
8441 -----------------------------
8443 function Find_Unique_Access_Type return Entity_Id is
8449 if Ekind_In (Etype (R), E_Allocator_Type,
8450 E_Access_Attribute_Type)
8452 Acc := Designated_Type (Etype (R));
8454 elsif Ekind_In (Etype (L), E_Allocator_Type,
8455 E_Access_Attribute_Type)
8457 Acc := Designated_Type (Etype (L));
8463 while S /= Standard_Standard loop
8464 E := First_Entity (S);
8465 while Present (E) loop
8467 and then Is_Access_Type (E)
8468 and then Ekind (E) /= E_Allocator_Type
8469 and then Designated_Type (E) = Base_Type (Acc)
8481 end Find_Unique_Access_Type;
8483 -- Start of processing for Resolve_Equality_Op
8486 Set_Etype (N, Base_Type (Typ));
8487 Generate_Reference (T, N, ' ');
8489 if T = Any_Fixed then
8490 T := Unique_Fixed_Point_Type (L);
8493 if T /= Any_Type then
8494 if T = Any_String or else
8495 T = Any_Composite or else
8498 if T = Any_Character then
8499 Ambiguous_Character (L);
8501 Error_Msg_N ("ambiguous operands for equality", N);
8504 Set_Etype (N, Any_Type);
8507 elsif T = Any_Access
8508 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
8510 T := Find_Unique_Access_Type;
8513 Error_Msg_N ("ambiguous operands for equality", N);
8514 Set_Etype (N, Any_Type);
8518 -- If expressions must have a single type, and if the context does
8519 -- not impose one the dependent expressions cannot be anonymous
8522 -- Why no similar processing for case expressions???
8524 elsif Ada_Version >= Ada_2012
8525 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
8526 E_Anonymous_Access_Subprogram_Type)
8527 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
8528 E_Anonymous_Access_Subprogram_Type)
8530 Check_If_Expression (L);
8531 Check_If_Expression (R);
8537 -- In SPARK, equality operators = and /= for array types other than
8538 -- String are only defined when, for each index position, the
8539 -- operands have equal static bounds.
8541 if Is_Array_Type (T) then
8543 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8544 -- operation if not needed.
8546 if Restriction_Check_Required (SPARK_05)
8547 and then Base_Type (T) /= Standard_String
8548 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
8549 and then Etype (L) /= Any_Composite -- or else L in error
8550 and then Etype (R) /= Any_Composite -- or else R in error
8551 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
8553 Check_SPARK_05_Restriction
8554 ("array types should have matching static bounds", N);
8558 -- If the unique type is a class-wide type then it will be expanded
8559 -- into a dispatching call to the predefined primitive. Therefore we
8560 -- check here for potential violation of such restriction.
8562 if Is_Class_Wide_Type (T) then
8563 Check_Restriction (No_Dispatching_Calls, N);
8566 -- Only warn for redundant equality comparison to True for objects
8567 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
8568 -- other expressions, it may be a matter of preference to write
8569 -- "Expr = True" or "Expr".
8571 if Warn_On_Redundant_Constructs
8572 and then Comes_From_Source (N)
8573 and then Comes_From_Source (R)
8574 and then Is_Entity_Name (R)
8575 and then Entity (R) = Standard_True
8577 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
8581 Error_Msg_N -- CODEFIX
8582 ("?r?comparison with True is redundant!", N);
8583 Explain_Redundancy (Original_Node (R));
8586 -- If the equality is overloaded and the operands have resolved
8587 -- properly, set the proper equality operator on the node. The
8588 -- current setting is the first one found during analysis, which
8589 -- is not necessarily the one to which the node has resolved.
8591 if Is_Overloaded (N) then
8597 Get_First_Interp (N, I, It);
8599 -- If the equality is user-defined, the type of the operands
8600 -- matches that of the formals. For a predefined operator,
8601 -- it is the scope that matters, given that the predefined
8602 -- equality has Any_Type formals. In either case the result
8603 -- type (most often Boolean) must match the context. The scope
8604 -- is either that of the type, if there is a generated equality
8605 -- (when there is an equality for the component type), or else
8606 -- Standard otherwise.
8608 while Present (It.Typ) loop
8609 if Etype (It.Nam) = Typ
8611 (Etype (First_Entity (It.Nam)) = Etype (L)
8612 or else Scope (It.Nam) = Standard_Standard
8613 or else Scope (It.Nam) = Scope (T))
8615 Set_Entity (N, It.Nam);
8617 Set_Is_Overloaded (N, False);
8621 Get_Next_Interp (I, It);
8624 -- If expansion is active and this is an inherited operation,
8625 -- replace it with its ancestor. This must not be done during
8626 -- preanalysis because the type may not be frozen yet, as when
8627 -- the context is a precondition or postcondition.
8629 if Present (Alias (Entity (N))) and then Expander_Active then
8630 Set_Entity (N, Alias (Entity (N)));
8635 Check_Unset_Reference (L);
8636 Check_Unset_Reference (R);
8637 Generate_Operator_Reference (N, T);
8638 Check_Low_Bound_Tested (N);
8640 -- If this is an inequality, it may be the implicit inequality
8641 -- created for a user-defined operation, in which case the corres-
8642 -- ponding equality operation is not intrinsic, and the operation
8643 -- cannot be constant-folded. Else fold.
8645 if Nkind (N) = N_Op_Eq
8646 or else Comes_From_Source (Entity (N))
8647 or else Ekind (Entity (N)) = E_Operator
8648 or else Is_Intrinsic_Subprogram
8649 (Corresponding_Equality (Entity (N)))
8651 Analyze_Dimension (N);
8652 Eval_Relational_Op (N);
8654 elsif Nkind (N) = N_Op_Ne
8655 and then Is_Abstract_Subprogram (Entity (N))
8657 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
8660 -- Ada 2005: If one operand is an anonymous access type, convert the
8661 -- other operand to it, to ensure that the underlying types match in
8662 -- the back-end. Same for access_to_subprogram, and the conversion
8663 -- verifies that the types are subtype conformant.
8665 -- We apply the same conversion in the case one of the operands is a
8666 -- private subtype of the type of the other.
8668 -- Why the Expander_Active test here ???
8672 (Ekind_In (T, E_Anonymous_Access_Type,
8673 E_Anonymous_Access_Subprogram_Type)
8674 or else Is_Private_Type (T))
8676 if Etype (L) /= T then
8678 Make_Unchecked_Type_Conversion (Sloc (L),
8679 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
8680 Expression => Relocate_Node (L)));
8681 Analyze_And_Resolve (L, T);
8684 if (Etype (R)) /= T then
8686 Make_Unchecked_Type_Conversion (Sloc (R),
8687 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
8688 Expression => Relocate_Node (R)));
8689 Analyze_And_Resolve (R, T);
8693 end Resolve_Equality_Op;
8695 ----------------------------------
8696 -- Resolve_Explicit_Dereference --
8697 ----------------------------------
8699 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
8700 Loc : constant Source_Ptr := Sloc (N);
8702 P : constant Node_Id := Prefix (N);
8705 -- The candidate prefix type, if overloaded
8711 Check_Fully_Declared_Prefix (Typ, P);
8714 -- A useful optimization: check whether the dereference denotes an
8715 -- element of a container, and if so rewrite it as a call to the
8716 -- corresponding Element function.
8718 -- Disabled for now, on advice of ARG. A more restricted form of the
8719 -- predicate might be acceptable ???
8721 -- if Is_Container_Element (N) then
8725 if Is_Overloaded (P) then
8727 -- Use the context type to select the prefix that has the correct
8728 -- designated type. Keep the first match, which will be the inner-
8731 Get_First_Interp (P, I, It);
8733 while Present (It.Typ) loop
8734 if Is_Access_Type (It.Typ)
8735 and then Covers (Typ, Designated_Type (It.Typ))
8741 -- Remove access types that do not match, but preserve access
8742 -- to subprogram interpretations, in case a further dereference
8743 -- is needed (see below).
8745 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
8749 Get_Next_Interp (I, It);
8752 if Present (P_Typ) then
8754 Set_Etype (N, Designated_Type (P_Typ));
8757 -- If no interpretation covers the designated type of the prefix,
8758 -- this is the pathological case where not all implementations of
8759 -- the prefix allow the interpretation of the node as a call. Now
8760 -- that the expected type is known, Remove other interpretations
8761 -- from prefix, rewrite it as a call, and resolve again, so that
8762 -- the proper call node is generated.
8764 Get_First_Interp (P, I, It);
8765 while Present (It.Typ) loop
8766 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
8770 Get_Next_Interp (I, It);
8774 Make_Function_Call (Loc,
8776 Make_Explicit_Dereference (Loc,
8778 Parameter_Associations => New_List);
8780 Save_Interps (N, New_N);
8782 Analyze_And_Resolve (N, Typ);
8786 -- If not overloaded, resolve P with its own type
8792 -- If the prefix might be null, add an access check
8794 if Is_Access_Type (Etype (P))
8795 and then not Can_Never_Be_Null (Etype (P))
8797 Apply_Access_Check (N);
8800 -- If the designated type is a packed unconstrained array type, and the
8801 -- explicit dereference is not in the context of an attribute reference,
8802 -- then we must compute and set the actual subtype, since it is needed
8803 -- by Gigi. The reason we exclude the attribute case is that this is
8804 -- handled fine by Gigi, and in fact we use such attributes to build the
8805 -- actual subtype. We also exclude generated code (which builds actual
8806 -- subtypes directly if they are needed).
8808 if Is_Array_Type (Etype (N))
8809 and then Is_Packed (Etype (N))
8810 and then not Is_Constrained (Etype (N))
8811 and then Nkind (Parent (N)) /= N_Attribute_Reference
8812 and then Comes_From_Source (N)
8814 Set_Etype (N, Get_Actual_Subtype (N));
8817 Analyze_Dimension (N);
8819 -- Note: No Eval processing is required for an explicit dereference,
8820 -- because such a name can never be static.
8822 end Resolve_Explicit_Dereference;
8824 -------------------------------------
8825 -- Resolve_Expression_With_Actions --
8826 -------------------------------------
8828 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
8832 -- If N has no actions, and its expression has been constant folded,
8833 -- then rewrite N as just its expression. Note, we can't do this in
8834 -- the general case of Is_Empty_List (Actions (N)) as this would cause
8835 -- Expression (N) to be expanded again.
8837 if Is_Empty_List (Actions (N))
8838 and then Compile_Time_Known_Value (Expression (N))
8840 Rewrite (N, Expression (N));
8842 end Resolve_Expression_With_Actions;
8844 ----------------------------------
8845 -- Resolve_Generalized_Indexing --
8846 ----------------------------------
8848 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
8849 Indexing : constant Node_Id := Generalized_Indexing (N);
8855 -- In ASIS mode, propagate the information about the indexes back to
8856 -- to the original indexing node. The generalized indexing is either
8857 -- a function call, or a dereference of one. The actuals include the
8858 -- prefix of the original node, which is the container expression.
8861 Resolve (Indexing, Typ);
8862 Set_Etype (N, Etype (Indexing));
8863 Set_Is_Overloaded (N, False);
8866 while Nkind_In (Call, N_Explicit_Dereference, N_Selected_Component)
8868 Call := Prefix (Call);
8871 if Nkind (Call) = N_Function_Call then
8872 Indexes := New_Copy_List (Parameter_Associations (Call));
8873 Pref := Remove_Head (Indexes);
8874 Set_Expressions (N, Indexes);
8876 -- If expression is to be reanalyzed, reset Generalized_Indexing
8877 -- to recreate call node, as is the case when the expression is
8878 -- part of an expression function.
8880 if In_Spec_Expression then
8881 Set_Generalized_Indexing (N, Empty);
8884 Set_Prefix (N, Pref);
8888 Rewrite (N, Indexing);
8891 end Resolve_Generalized_Indexing;
8893 ---------------------------
8894 -- Resolve_If_Expression --
8895 ---------------------------
8897 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
8898 procedure Apply_Check (Expr : Node_Id);
8899 -- When a dependent expression is of a subtype different from
8900 -- the context subtype, then insert a qualification to ensure
8901 -- the generation of a constraint check. This was previously
8902 -- for scalar types. For array types apply a length check, given
8903 -- that the context in general allows sliding, while a qualified
8904 -- expression forces equality of bounds.
8910 procedure Apply_Check (Expr : Node_Id) is
8911 Expr_Typ : constant Entity_Id := Etype (Expr);
8912 Loc : constant Source_Ptr := Sloc (Expr);
8916 or else Is_Tagged_Type (Typ)
8917 or else Is_Access_Type (Typ)
8918 or else not Is_Constrained (Typ)
8919 or else Inside_A_Generic
8923 elsif Is_Array_Type (Typ) then
8924 Apply_Length_Check (Expr, Typ);
8928 Make_Qualified_Expression (Loc,
8929 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
8930 Expression => Relocate_Node (Expr)));
8932 Analyze_And_Resolve (Expr, Typ);
8938 Condition : constant Node_Id := First (Expressions (N));
8939 Else_Expr : Node_Id;
8940 Then_Expr : Node_Id;
8942 -- Start of processing for Resolve_If_Expression
8945 -- Defend against malformed expressions
8947 if No (Condition) then
8951 Then_Expr := Next (Condition);
8953 if No (Then_Expr) then
8957 Else_Expr := Next (Then_Expr);
8959 Resolve (Condition, Any_Boolean);
8960 Resolve (Then_Expr, Typ);
8961 Apply_Check (Then_Expr);
8963 -- If ELSE expression present, just resolve using the determined type
8964 -- If type is universal, resolve to any member of the class.
8966 if Present (Else_Expr) then
8967 if Typ = Universal_Integer then
8968 Resolve (Else_Expr, Any_Integer);
8970 elsif Typ = Universal_Real then
8971 Resolve (Else_Expr, Any_Real);
8974 Resolve (Else_Expr, Typ);
8977 Apply_Check (Else_Expr);
8979 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
8980 -- dynamically tagged must be known statically.
8982 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
8983 if Is_Dynamically_Tagged (Then_Expr) /=
8984 Is_Dynamically_Tagged (Else_Expr)
8986 Error_Msg_N ("all or none of the dependent expressions "
8987 & "can be dynamically tagged", N);
8991 -- If no ELSE expression is present, root type must be Standard.Boolean
8992 -- and we provide a Standard.True result converted to the appropriate
8993 -- Boolean type (in case it is a derived boolean type).
8995 elsif Root_Type (Typ) = Standard_Boolean then
8997 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
8998 Analyze_And_Resolve (Else_Expr, Typ);
8999 Append_To (Expressions (N), Else_Expr);
9002 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9003 Append_To (Expressions (N), Error);
9008 if not Error_Posted (N) then
9009 Eval_If_Expression (N);
9012 Analyze_Dimension (N);
9013 end Resolve_If_Expression;
9015 -------------------------------
9016 -- Resolve_Indexed_Component --
9017 -------------------------------
9019 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9020 Name : constant Node_Id := Prefix (N);
9022 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9026 if Present (Generalized_Indexing (N)) then
9027 Resolve_Generalized_Indexing (N, Typ);
9031 if Is_Overloaded (Name) then
9033 -- Use the context type to select the prefix that yields the correct
9039 I1 : Interp_Index := 0;
9040 P : constant Node_Id := Prefix (N);
9041 Found : Boolean := False;
9044 Get_First_Interp (P, I, It);
9045 while Present (It.Typ) loop
9046 if (Is_Array_Type (It.Typ)
9047 and then Covers (Typ, Component_Type (It.Typ)))
9048 or else (Is_Access_Type (It.Typ)
9049 and then Is_Array_Type (Designated_Type (It.Typ))
9053 Component_Type (Designated_Type (It.Typ))))
9056 It := Disambiguate (P, I1, I, Any_Type);
9058 if It = No_Interp then
9059 Error_Msg_N ("ambiguous prefix for indexing", N);
9065 Array_Type := It.Typ;
9071 Array_Type := It.Typ;
9076 Get_Next_Interp (I, It);
9081 Array_Type := Etype (Name);
9084 Resolve (Name, Array_Type);
9085 Array_Type := Get_Actual_Subtype_If_Available (Name);
9087 -- If prefix is access type, dereference to get real array type.
9088 -- Note: we do not apply an access check because the expander always
9089 -- introduces an explicit dereference, and the check will happen there.
9091 if Is_Access_Type (Array_Type) then
9092 Array_Type := Designated_Type (Array_Type);
9095 -- If name was overloaded, set component type correctly now
9096 -- If a misplaced call to an entry family (which has no index types)
9097 -- return. Error will be diagnosed from calling context.
9099 if Is_Array_Type (Array_Type) then
9100 Set_Etype (N, Component_Type (Array_Type));
9105 Index := First_Index (Array_Type);
9106 Expr := First (Expressions (N));
9108 -- The prefix may have resolved to a string literal, in which case its
9109 -- etype has a special representation. This is only possible currently
9110 -- if the prefix is a static concatenation, written in functional
9113 if Ekind (Array_Type) = E_String_Literal_Subtype then
9114 Resolve (Expr, Standard_Positive);
9117 while Present (Index) and Present (Expr) loop
9118 Resolve (Expr, Etype (Index));
9119 Check_Unset_Reference (Expr);
9121 if Is_Scalar_Type (Etype (Expr)) then
9122 Apply_Scalar_Range_Check (Expr, Etype (Index));
9124 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
9132 Analyze_Dimension (N);
9134 -- Do not generate the warning on suspicious index if we are analyzing
9135 -- package Ada.Tags; otherwise we will report the warning with the
9136 -- Prims_Ptr field of the dispatch table.
9138 if Scope (Etype (Prefix (N))) = Standard_Standard
9140 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
9143 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
9144 Eval_Indexed_Component (N);
9147 -- If the array type is atomic, and the component is not atomic, then
9148 -- this is worth a warning, since we have a situation where the access
9149 -- to the component may cause extra read/writes of the atomic array
9150 -- object, or partial word accesses, which could be unexpected.
9152 if Nkind (N) = N_Indexed_Component
9153 and then Is_Atomic_Ref_With_Address (N)
9154 and then not (Has_Atomic_Components (Array_Type)
9155 or else (Is_Entity_Name (Prefix (N))
9156 and then Has_Atomic_Components
9157 (Entity (Prefix (N)))))
9158 and then not Is_Atomic (Component_Type (Array_Type))
9161 ("??access to non-atomic component of atomic array", Prefix (N));
9163 ("??\may cause unexpected accesses to atomic object", Prefix (N));
9165 end Resolve_Indexed_Component;
9167 -----------------------------
9168 -- Resolve_Integer_Literal --
9169 -----------------------------
9171 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9174 Eval_Integer_Literal (N);
9175 end Resolve_Integer_Literal;
9177 --------------------------------
9178 -- Resolve_Intrinsic_Operator --
9179 --------------------------------
9181 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9182 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9187 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9188 -- If the operand is a literal, it cannot be the expression in a
9189 -- conversion. Use a qualified expression instead.
9191 ---------------------
9192 -- Convert_Operand --
9193 ---------------------
9195 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9196 Loc : constant Source_Ptr := Sloc (Opnd);
9200 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
9202 Make_Qualified_Expression (Loc,
9203 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9204 Expression => Relocate_Node (Opnd));
9208 Res := Unchecked_Convert_To (Btyp, Opnd);
9212 end Convert_Operand;
9214 -- Start of processing for Resolve_Intrinsic_Operator
9217 -- We must preserve the original entity in a generic setting, so that
9218 -- the legality of the operation can be verified in an instance.
9220 if not Expander_Active then
9225 while Scope (Op) /= Standard_Standard loop
9227 pragma Assert (Present (Op));
9231 Set_Is_Overloaded (N, False);
9233 -- If the result or operand types are private, rewrite with unchecked
9234 -- conversions on the operands and the result, to expose the proper
9235 -- underlying numeric type.
9237 if Is_Private_Type (Typ)
9238 or else Is_Private_Type (Etype (Left_Opnd (N)))
9239 or else Is_Private_Type (Etype (Right_Opnd (N)))
9241 Arg1 := Convert_Operand (Left_Opnd (N));
9243 if Nkind (N) = N_Op_Expon then
9244 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9246 Arg2 := Convert_Operand (Right_Opnd (N));
9249 if Nkind (Arg1) = N_Type_Conversion then
9250 Save_Interps (Left_Opnd (N), Expression (Arg1));
9253 if Nkind (Arg2) = N_Type_Conversion then
9254 Save_Interps (Right_Opnd (N), Expression (Arg2));
9257 Set_Left_Opnd (N, Arg1);
9258 Set_Right_Opnd (N, Arg2);
9260 Set_Etype (N, Btyp);
9261 Rewrite (N, Unchecked_Convert_To (Typ, N));
9264 elsif Typ /= Etype (Left_Opnd (N))
9265 or else Typ /= Etype (Right_Opnd (N))
9267 -- Add explicit conversion where needed, and save interpretations in
9268 -- case operands are overloaded.
9270 Arg1 := Convert_To (Typ, Left_Opnd (N));
9271 Arg2 := Convert_To (Typ, Right_Opnd (N));
9273 if Nkind (Arg1) = N_Type_Conversion then
9274 Save_Interps (Left_Opnd (N), Expression (Arg1));
9276 Save_Interps (Left_Opnd (N), Arg1);
9279 if Nkind (Arg2) = N_Type_Conversion then
9280 Save_Interps (Right_Opnd (N), Expression (Arg2));
9282 Save_Interps (Right_Opnd (N), Arg2);
9285 Rewrite (Left_Opnd (N), Arg1);
9286 Rewrite (Right_Opnd (N), Arg2);
9289 Resolve_Arithmetic_Op (N, Typ);
9292 Resolve_Arithmetic_Op (N, Typ);
9294 end Resolve_Intrinsic_Operator;
9296 --------------------------------------
9297 -- Resolve_Intrinsic_Unary_Operator --
9298 --------------------------------------
9300 procedure Resolve_Intrinsic_Unary_Operator
9304 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9310 while Scope (Op) /= Standard_Standard loop
9312 pragma Assert (Present (Op));
9317 if Is_Private_Type (Typ) then
9318 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
9319 Save_Interps (Right_Opnd (N), Expression (Arg2));
9321 Set_Right_Opnd (N, Arg2);
9323 Set_Etype (N, Btyp);
9324 Rewrite (N, Unchecked_Convert_To (Typ, N));
9328 Resolve_Unary_Op (N, Typ);
9330 end Resolve_Intrinsic_Unary_Operator;
9332 ------------------------
9333 -- Resolve_Logical_Op --
9334 ------------------------
9336 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
9340 Check_No_Direct_Boolean_Operators (N);
9342 -- Predefined operations on scalar types yield the base type. On the
9343 -- other hand, logical operations on arrays yield the type of the
9344 -- arguments (and the context).
9346 if Is_Array_Type (Typ) then
9349 B_Typ := Base_Type (Typ);
9352 -- The following test is required because the operands of the operation
9353 -- may be literals, in which case the resulting type appears to be
9354 -- compatible with a signed integer type, when in fact it is compatible
9355 -- only with modular types. If the context itself is universal, the
9356 -- operation is illegal.
9358 if not Valid_Boolean_Arg (Typ) then
9359 Error_Msg_N ("invalid context for logical operation", N);
9360 Set_Etype (N, Any_Type);
9363 elsif Typ = Any_Modular then
9365 ("no modular type available in this context", N);
9366 Set_Etype (N, Any_Type);
9369 elsif Is_Modular_Integer_Type (Typ)
9370 and then Etype (Left_Opnd (N)) = Universal_Integer
9371 and then Etype (Right_Opnd (N)) = Universal_Integer
9373 Check_For_Visible_Operator (N, B_Typ);
9376 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
9377 -- is active and the result type is standard Boolean (do not mess with
9378 -- ops that return a nonstandard Boolean type, because something strange
9381 -- Note: you might expect this replacement to be done during expansion,
9382 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
9383 -- is used, no part of the right operand of an "and" or "or" operator
9384 -- should be executed if the left operand would short-circuit the
9385 -- evaluation of the corresponding "and then" or "or else". If we left
9386 -- the replacement to expansion time, then run-time checks associated
9387 -- with such operands would be evaluated unconditionally, due to being
9388 -- before the condition prior to the rewriting as short-circuit forms
9389 -- during expansion.
9391 if Short_Circuit_And_Or
9392 and then B_Typ = Standard_Boolean
9393 and then Nkind_In (N, N_Op_And, N_Op_Or)
9395 -- Mark the corresponding putative SCO operator as truly a logical
9396 -- (and short-circuit) operator.
9398 if Generate_SCO and then Comes_From_Source (N) then
9399 Set_SCO_Logical_Operator (N);
9402 if Nkind (N) = N_Op_And then
9404 Make_And_Then (Sloc (N),
9405 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9406 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9407 Analyze_And_Resolve (N, B_Typ);
9409 -- Case of OR changed to OR ELSE
9413 Make_Or_Else (Sloc (N),
9414 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9415 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9416 Analyze_And_Resolve (N, B_Typ);
9419 -- Return now, since analysis of the rewritten ops will take care of
9420 -- other reference bookkeeping and expression folding.
9425 Resolve (Left_Opnd (N), B_Typ);
9426 Resolve (Right_Opnd (N), B_Typ);
9428 Check_Unset_Reference (Left_Opnd (N));
9429 Check_Unset_Reference (Right_Opnd (N));
9431 Set_Etype (N, B_Typ);
9432 Generate_Operator_Reference (N, B_Typ);
9433 Eval_Logical_Op (N);
9435 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
9436 -- only when both operands have same static lower and higher bounds. Of
9437 -- course the types have to match, so only check if operands are
9438 -- compatible and the node itself has no errors.
9440 if Is_Array_Type (B_Typ)
9441 and then Nkind (N) in N_Binary_Op
9444 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
9445 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
9448 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9449 -- operation if not needed.
9451 if Restriction_Check_Required (SPARK_05)
9452 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
9453 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
9454 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
9455 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
9457 Check_SPARK_05_Restriction
9458 ("array types should have matching static bounds", N);
9462 end Resolve_Logical_Op;
9464 ---------------------------
9465 -- Resolve_Membership_Op --
9466 ---------------------------
9468 -- The context can only be a boolean type, and does not determine the
9469 -- arguments. Arguments should be unambiguous, but the preference rule for
9470 -- universal types applies.
9472 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
9473 pragma Warnings (Off, Typ);
9475 L : constant Node_Id := Left_Opnd (N);
9476 R : constant Node_Id := Right_Opnd (N);
9479 procedure Resolve_Set_Membership;
9480 -- Analysis has determined a unique type for the left operand. Use it to
9481 -- resolve the disjuncts.
9483 ----------------------------
9484 -- Resolve_Set_Membership --
9485 ----------------------------
9487 procedure Resolve_Set_Membership is
9492 -- If the left operand is overloaded, find type compatible with not
9493 -- overloaded alternative of the right operand.
9495 if Is_Overloaded (L) then
9497 Alt := First (Alternatives (N));
9498 while Present (Alt) loop
9499 if not Is_Overloaded (Alt) then
9500 Ltyp := Intersect_Types (L, Alt);
9507 -- Unclear how to resolve expression if all alternatives are also
9511 Error_Msg_N ("ambiguous expression", N);
9520 Alt := First (Alternatives (N));
9521 while Present (Alt) loop
9523 -- Alternative is an expression, a range
9524 -- or a subtype mark.
9526 if not Is_Entity_Name (Alt)
9527 or else not Is_Type (Entity (Alt))
9529 Resolve (Alt, Ltyp);
9535 -- Check for duplicates for discrete case
9537 if Is_Discrete_Type (Ltyp) then
9544 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
9548 -- Loop checking duplicates. This is quadratic, but giant sets
9549 -- are unlikely in this context so it's a reasonable choice.
9552 Alt := First (Alternatives (N));
9553 while Present (Alt) loop
9554 if Is_OK_Static_Expression (Alt)
9555 and then (Nkind_In (Alt, N_Integer_Literal,
9556 N_Character_Literal)
9557 or else Nkind (Alt) in N_Has_Entity)
9560 Alts (Nalts) := (Alt, Expr_Value (Alt));
9562 for J in 1 .. Nalts - 1 loop
9563 if Alts (J).Val = Alts (Nalts).Val then
9564 Error_Msg_Sloc := Sloc (Alts (J).Alt);
9565 Error_Msg_N ("duplicate of value given#??", Alt);
9575 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
9576 -- limited types, evaluation of a membership test uses the predefined
9577 -- equality for the type. This may be confusing to users, and the
9578 -- following warning appears useful for the most common case.
9580 if Is_Scalar_Type (Ltyp)
9581 and then Present (Get_User_Defined_Eq (Ltyp))
9584 ("membership test on& uses predefined equality?", N, Ltyp);
9586 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
9588 end Resolve_Set_Membership;
9590 -- Start of processing for Resolve_Membership_Op
9593 if L = Error or else R = Error then
9597 if Present (Alternatives (N)) then
9598 Resolve_Set_Membership;
9601 elsif not Is_Overloaded (R)
9603 (Etype (R) = Universal_Integer
9605 Etype (R) = Universal_Real)
9606 and then Is_Overloaded (L)
9610 -- Ada 2005 (AI-251): Support the following case:
9612 -- type I is interface;
9613 -- type T is tagged ...
9615 -- function Test (O : I'Class) is
9617 -- return O in T'Class.
9620 -- In this case we have nothing else to do. The membership test will be
9621 -- done at run time.
9623 elsif Ada_Version >= Ada_2005
9624 and then Is_Class_Wide_Type (Etype (L))
9625 and then Is_Interface (Etype (L))
9626 and then not Is_Interface (Etype (R))
9630 T := Intersect_Types (L, R);
9633 -- If mixed-mode operations are present and operands are all literal,
9634 -- the only interpretation involves Duration, which is probably not
9635 -- the intention of the programmer.
9637 if T = Any_Fixed then
9638 T := Unique_Fixed_Point_Type (N);
9640 if T = Any_Type then
9646 Check_Unset_Reference (L);
9648 if Nkind (R) = N_Range
9649 and then not Is_Scalar_Type (T)
9651 Error_Msg_N ("scalar type required for range", R);
9654 if Is_Entity_Name (R) then
9655 Freeze_Expression (R);
9658 Check_Unset_Reference (R);
9661 -- Here after resolving membership operation
9665 Eval_Membership_Op (N);
9666 end Resolve_Membership_Op;
9672 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
9673 Loc : constant Source_Ptr := Sloc (N);
9676 -- Handle restriction against anonymous null access values This
9677 -- restriction can be turned off using -gnatdj.
9679 -- Ada 2005 (AI-231): Remove restriction
9681 if Ada_Version < Ada_2005
9682 and then not Debug_Flag_J
9683 and then Ekind (Typ) = E_Anonymous_Access_Type
9684 and then Comes_From_Source (N)
9686 -- In the common case of a call which uses an explicitly null value
9687 -- for an access parameter, give specialized error message.
9689 if Nkind (Parent (N)) in N_Subprogram_Call then
9691 ("null is not allowed as argument for an access parameter", N);
9693 -- Standard message for all other cases (are there any?)
9697 ("null cannot be of an anonymous access type", N);
9701 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
9702 -- assignment to a null-excluding object.
9704 if Ada_Version >= Ada_2005
9705 and then Can_Never_Be_Null (Typ)
9706 and then Nkind (Parent (N)) = N_Assignment_Statement
9708 if Inside_Init_Proc then
9710 -- Decide whether to generate an if_statement around our
9711 -- null-excluding check to avoid them on certain internal object
9712 -- declarations by looking at the type the current Init_Proc
9716 -- if T1b_skip_null_excluding_check then
9717 -- [constraint_error "access check failed"]
9720 if Needs_Conditional_Null_Excluding_Check
9721 (Etype (First_Formal (Enclosing_Init_Proc)))
9724 Make_If_Statement (Loc,
9726 Make_Identifier (Loc,
9728 (Chars (Typ), "_skip_null_excluding_check")),
9731 Make_Raise_Constraint_Error (Loc,
9732 Reason => CE_Access_Check_Failed))));
9734 -- Otherwise, simply create the check
9738 Make_Raise_Constraint_Error (Loc,
9739 Reason => CE_Access_Check_Failed));
9743 (Compile_Time_Constraint_Error (N,
9744 "(Ada 2005) null not allowed in null-excluding objects??"),
9745 Make_Raise_Constraint_Error (Loc,
9746 Reason => CE_Access_Check_Failed));
9750 -- In a distributed context, null for a remote access to subprogram may
9751 -- need to be replaced with a special record aggregate. In this case,
9752 -- return after having done the transformation.
9754 if (Ekind (Typ) = E_Record_Type
9755 or else Is_Remote_Access_To_Subprogram_Type (Typ))
9756 and then Remote_AST_Null_Value (N, Typ)
9761 -- The null literal takes its type from the context
9766 -----------------------
9767 -- Resolve_Op_Concat --
9768 -----------------------
9770 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
9772 -- We wish to avoid deep recursion, because concatenations are often
9773 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
9774 -- operands nonrecursively until we find something that is not a simple
9775 -- concatenation (A in this case). We resolve that, and then walk back
9776 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
9777 -- to do the rest of the work at each level. The Parent pointers allow
9778 -- us to avoid recursion, and thus avoid running out of memory. See also
9779 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
9785 -- The following code is equivalent to:
9787 -- Resolve_Op_Concat_First (NN, Typ);
9788 -- Resolve_Op_Concat_Arg (N, ...);
9789 -- Resolve_Op_Concat_Rest (N, Typ);
9791 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
9792 -- operand is a concatenation.
9794 -- Walk down left operands
9797 Resolve_Op_Concat_First (NN, Typ);
9798 Op1 := Left_Opnd (NN);
9799 exit when not (Nkind (Op1) = N_Op_Concat
9800 and then not Is_Array_Type (Component_Type (Typ))
9801 and then Entity (Op1) = Entity (NN));
9805 -- Now (given the above example) NN is A&B and Op1 is A
9807 -- First resolve Op1 ...
9809 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
9811 -- ... then walk NN back up until we reach N (where we started), calling
9812 -- Resolve_Op_Concat_Rest along the way.
9815 Resolve_Op_Concat_Rest (NN, Typ);
9820 if Base_Type (Etype (N)) /= Standard_String then
9821 Check_SPARK_05_Restriction
9822 ("result of concatenation should have type String", N);
9824 end Resolve_Op_Concat;
9826 ---------------------------
9827 -- Resolve_Op_Concat_Arg --
9828 ---------------------------
9830 procedure Resolve_Op_Concat_Arg
9836 Btyp : constant Entity_Id := Base_Type (Typ);
9837 Ctyp : constant Entity_Id := Component_Type (Typ);
9842 or else (not Is_Overloaded (Arg)
9843 and then Etype (Arg) /= Any_Composite
9844 and then Covers (Ctyp, Etype (Arg)))
9846 Resolve (Arg, Ctyp);
9848 Resolve (Arg, Btyp);
9851 -- If both Array & Array and Array & Component are visible, there is a
9852 -- potential ambiguity that must be reported.
9854 elsif Has_Compatible_Type (Arg, Ctyp) then
9855 if Nkind (Arg) = N_Aggregate
9856 and then Is_Composite_Type (Ctyp)
9858 if Is_Private_Type (Ctyp) then
9859 Resolve (Arg, Btyp);
9861 -- If the operation is user-defined and not overloaded use its
9862 -- profile. The operation may be a renaming, in which case it has
9863 -- been rewritten, and we want the original profile.
9865 elsif not Is_Overloaded (N)
9866 and then Comes_From_Source (Entity (Original_Node (N)))
9867 and then Ekind (Entity (Original_Node (N))) = E_Function
9871 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
9874 -- Otherwise an aggregate may match both the array type and the
9878 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
9879 Set_Etype (Arg, Any_Type);
9883 if Is_Overloaded (Arg)
9884 and then Has_Compatible_Type (Arg, Typ)
9885 and then Etype (Arg) /= Any_Type
9893 Get_First_Interp (Arg, I, It);
9895 Get_Next_Interp (I, It);
9897 -- Special-case the error message when the overloading is
9898 -- caused by a function that yields an array and can be
9899 -- called without parameters.
9901 if It.Nam = Func then
9902 Error_Msg_Sloc := Sloc (Func);
9903 Error_Msg_N ("ambiguous call to function#", Arg);
9905 ("\\interpretation as call yields&", Arg, Typ);
9907 ("\\interpretation as indexing of call yields&",
9908 Arg, Component_Type (Typ));
9911 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
9913 Get_First_Interp (Arg, I, It);
9914 while Present (It.Nam) loop
9915 Error_Msg_Sloc := Sloc (It.Nam);
9917 if Base_Type (It.Typ) = Btyp
9919 Base_Type (It.Typ) = Base_Type (Ctyp)
9921 Error_Msg_N -- CODEFIX
9922 ("\\possible interpretation#", Arg);
9925 Get_Next_Interp (I, It);
9931 Resolve (Arg, Component_Type (Typ));
9933 if Nkind (Arg) = N_String_Literal then
9934 Set_Etype (Arg, Component_Type (Typ));
9937 if Arg = Left_Opnd (N) then
9938 Set_Is_Component_Left_Opnd (N);
9940 Set_Is_Component_Right_Opnd (N);
9945 Resolve (Arg, Btyp);
9948 -- Concatenation is restricted in SPARK: each operand must be either a
9949 -- string literal, the name of a string constant, a static character or
9950 -- string expression, or another concatenation. Arg cannot be a
9951 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
9952 -- separately on each final operand, past concatenation operations.
9954 if Is_Character_Type (Etype (Arg)) then
9955 if not Is_OK_Static_Expression (Arg) then
9956 Check_SPARK_05_Restriction
9957 ("character operand for concatenation should be static", Arg);
9960 elsif Is_String_Type (Etype (Arg)) then
9961 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
9962 and then Is_Constant_Object (Entity (Arg)))
9963 and then not Is_OK_Static_Expression (Arg)
9965 Check_SPARK_05_Restriction
9966 ("string operand for concatenation should be static", Arg);
9969 -- Do not issue error on an operand that is neither a character nor a
9970 -- string, as the error is issued in Resolve_Op_Concat.
9976 Check_Unset_Reference (Arg);
9977 end Resolve_Op_Concat_Arg;
9979 -----------------------------
9980 -- Resolve_Op_Concat_First --
9981 -----------------------------
9983 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
9984 Btyp : constant Entity_Id := Base_Type (Typ);
9985 Op1 : constant Node_Id := Left_Opnd (N);
9986 Op2 : constant Node_Id := Right_Opnd (N);
9989 -- The parser folds an enormous sequence of concatenations of string
9990 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
9991 -- in the right operand. If the expression resolves to a predefined "&"
9992 -- operator, all is well. Otherwise, the parser's folding is wrong, so
9993 -- we give an error. See P_Simple_Expression in Par.Ch4.
9995 if Nkind (Op2) = N_String_Literal
9996 and then Is_Folded_In_Parser (Op2)
9997 and then Ekind (Entity (N)) = E_Function
9999 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10000 and then String_Length (Strval (Op1)) = 0);
10001 Error_Msg_N ("too many user-defined concatenations", N);
10005 Set_Etype (N, Btyp);
10007 if Is_Limited_Composite (Btyp) then
10008 Error_Msg_N ("concatenation not available for limited array", N);
10009 Explain_Limited_Type (Btyp, N);
10011 end Resolve_Op_Concat_First;
10013 ----------------------------
10014 -- Resolve_Op_Concat_Rest --
10015 ----------------------------
10017 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10018 Op1 : constant Node_Id := Left_Opnd (N);
10019 Op2 : constant Node_Id := Right_Opnd (N);
10022 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10024 Generate_Operator_Reference (N, Typ);
10026 if Is_String_Type (Typ) then
10027 Eval_Concatenation (N);
10030 -- If this is not a static concatenation, but the result is a string
10031 -- type (and not an array of strings) ensure that static string operands
10032 -- have their subtypes properly constructed.
10034 if Nkind (N) /= N_String_Literal
10035 and then Is_Character_Type (Component_Type (Typ))
10037 Set_String_Literal_Subtype (Op1, Typ);
10038 Set_String_Literal_Subtype (Op2, Typ);
10040 end Resolve_Op_Concat_Rest;
10042 ----------------------
10043 -- Resolve_Op_Expon --
10044 ----------------------
10046 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10047 B_Typ : constant Entity_Id := Base_Type (Typ);
10050 -- Catch attempts to do fixed-point exponentiation with universal
10051 -- operands, which is a case where the illegality is not caught during
10052 -- normal operator analysis. This is not done in preanalysis mode
10053 -- since the tree is not fully decorated during preanalysis.
10055 if Full_Analysis then
10056 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10057 Error_Msg_N ("exponentiation not available for fixed point", N);
10060 elsif Nkind (Parent (N)) in N_Op
10061 and then Present (Etype (Parent (N)))
10062 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10063 and then Etype (N) = Universal_Real
10064 and then Comes_From_Source (N)
10066 Error_Msg_N ("exponentiation not available for fixed point", N);
10071 if Comes_From_Source (N)
10072 and then Ekind (Entity (N)) = E_Function
10073 and then Is_Imported (Entity (N))
10074 and then Is_Intrinsic_Subprogram (Entity (N))
10076 Resolve_Intrinsic_Operator (N, Typ);
10080 if Etype (Left_Opnd (N)) = Universal_Integer
10081 or else Etype (Left_Opnd (N)) = Universal_Real
10083 Check_For_Visible_Operator (N, B_Typ);
10086 -- We do the resolution using the base type, because intermediate values
10087 -- in expressions are always of the base type, not a subtype of it.
10089 Resolve (Left_Opnd (N), B_Typ);
10090 Resolve (Right_Opnd (N), Standard_Integer);
10092 -- For integer types, right argument must be in Natural range
10094 if Is_Integer_Type (Typ) then
10095 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10098 Check_Unset_Reference (Left_Opnd (N));
10099 Check_Unset_Reference (Right_Opnd (N));
10101 Set_Etype (N, B_Typ);
10102 Generate_Operator_Reference (N, B_Typ);
10104 Analyze_Dimension (N);
10106 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10107 -- Evaluate the exponentiation operator for dimensioned type
10109 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10114 -- Set overflow checking bit. Much cleverer code needed here eventually
10115 -- and perhaps the Resolve routines should be separated for the various
10116 -- arithmetic operations, since they will need different processing. ???
10118 if Nkind (N) in N_Op then
10119 if not Overflow_Checks_Suppressed (Etype (N)) then
10120 Enable_Overflow_Check (N);
10123 end Resolve_Op_Expon;
10125 --------------------
10126 -- Resolve_Op_Not --
10127 --------------------
10129 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10132 function Parent_Is_Boolean return Boolean;
10133 -- This function determines if the parent node is a boolean operator or
10134 -- operation (comparison op, membership test, or short circuit form) and
10135 -- the not in question is the left operand of this operation. Note that
10136 -- if the not is in parens, then false is returned.
10138 -----------------------
10139 -- Parent_Is_Boolean --
10140 -----------------------
10142 function Parent_Is_Boolean return Boolean is
10144 if Paren_Count (N) /= 0 then
10148 case Nkind (Parent (N)) is
10163 return Left_Opnd (Parent (N)) = N;
10169 end Parent_Is_Boolean;
10171 -- Start of processing for Resolve_Op_Not
10174 -- Predefined operations on scalar types yield the base type. On the
10175 -- other hand, logical operations on arrays yield the type of the
10176 -- arguments (and the context).
10178 if Is_Array_Type (Typ) then
10181 B_Typ := Base_Type (Typ);
10184 -- Straightforward case of incorrect arguments
10186 if not Valid_Boolean_Arg (Typ) then
10187 Error_Msg_N ("invalid operand type for operator&", N);
10188 Set_Etype (N, Any_Type);
10191 -- Special case of probable missing parens
10193 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10194 if Parent_Is_Boolean then
10196 ("operand of not must be enclosed in parentheses",
10200 ("no modular type available in this context", N);
10203 Set_Etype (N, Any_Type);
10206 -- OK resolution of NOT
10209 -- Warn if non-boolean types involved. This is a case like not a < b
10210 -- where a and b are modular, where we will get (not a) < b and most
10211 -- likely not (a < b) was intended.
10213 if Warn_On_Questionable_Missing_Parens
10214 and then not Is_Boolean_Type (Typ)
10215 and then Parent_Is_Boolean
10217 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10220 -- Warn on double negation if checking redundant constructs
10222 if Warn_On_Redundant_Constructs
10223 and then Comes_From_Source (N)
10224 and then Comes_From_Source (Right_Opnd (N))
10225 and then Root_Type (Typ) = Standard_Boolean
10226 and then Nkind (Right_Opnd (N)) = N_Op_Not
10228 Error_Msg_N ("redundant double negation?r?", N);
10231 -- Complete resolution and evaluation of NOT
10232 -- If argument is an equality and expected type is boolean, that
10233 -- expected type has no effect on resolution, and there are
10234 -- special rules for resolution of Eq, Neq in the presence of
10235 -- overloaded operands, so we directly call its resolution routines.
10238 Opnd : constant Node_Id := Right_Opnd (N);
10242 if B_Typ = Standard_Boolean
10243 and then Nkind_In (Opnd, N_Op_Eq, N_Op_Ne)
10244 and then Is_Overloaded (Opnd)
10246 Resolve_Equality_Op (Opnd, B_Typ);
10247 Op_Id := Entity (Opnd);
10249 if Ekind (Op_Id) = E_Function
10250 and then not Is_Intrinsic_Subprogram (Op_Id)
10252 Rewrite_Operator_As_Call (Opnd, Op_Id);
10255 if not Inside_A_Generic or else Is_Entity_Name (Opnd) then
10256 Freeze_Expression (Opnd);
10262 Resolve (Opnd, B_Typ);
10265 Check_Unset_Reference (Opnd);
10268 Set_Etype (N, B_Typ);
10269 Generate_Operator_Reference (N, B_Typ);
10272 end Resolve_Op_Not;
10274 -----------------------------
10275 -- Resolve_Operator_Symbol --
10276 -----------------------------
10278 -- Nothing to be done, all resolved already
10280 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
10281 pragma Warnings (Off, N);
10282 pragma Warnings (Off, Typ);
10286 end Resolve_Operator_Symbol;
10288 ----------------------------------
10289 -- Resolve_Qualified_Expression --
10290 ----------------------------------
10292 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
10293 pragma Warnings (Off, Typ);
10295 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
10296 Expr : constant Node_Id := Expression (N);
10299 Resolve (Expr, Target_Typ);
10301 -- Protect call to Matching_Static_Array_Bounds to avoid costly
10302 -- operation if not needed.
10304 if Restriction_Check_Required (SPARK_05)
10305 and then Is_Array_Type (Target_Typ)
10306 and then Is_Array_Type (Etype (Expr))
10307 and then Etype (Expr) /= Any_Composite -- or else Expr in error
10308 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
10310 Check_SPARK_05_Restriction
10311 ("array types should have matching static bounds", N);
10314 -- A qualified expression requires an exact match of the type, class-
10315 -- wide matching is not allowed. However, if the qualifying type is
10316 -- specific and the expression has a class-wide type, it may still be
10317 -- okay, since it can be the result of the expansion of a call to a
10318 -- dispatching function, so we also have to check class-wideness of the
10319 -- type of the expression's original node.
10321 if (Is_Class_Wide_Type (Target_Typ)
10323 (Is_Class_Wide_Type (Etype (Expr))
10324 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
10325 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
10327 Wrong_Type (Expr, Target_Typ);
10330 -- If the target type is unconstrained, then we reset the type of the
10331 -- result from the type of the expression. For other cases, the actual
10332 -- subtype of the expression is the target type.
10334 if Is_Composite_Type (Target_Typ)
10335 and then not Is_Constrained (Target_Typ)
10337 Set_Etype (N, Etype (Expr));
10340 Analyze_Dimension (N);
10341 Eval_Qualified_Expression (N);
10343 -- If we still have a qualified expression after the static evaluation,
10344 -- then apply a scalar range check if needed. The reason that we do this
10345 -- after the Eval call is that otherwise, the application of the range
10346 -- check may convert an illegal static expression and result in warning
10347 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
10349 if Nkind (N) = N_Qualified_Expression and then Is_Scalar_Type (Typ) then
10350 Apply_Scalar_Range_Check (Expr, Typ);
10353 -- Finally, check whether a predicate applies to the target type. This
10354 -- comes from AI12-0100. As for type conversions, check the enclosing
10355 -- context to prevent an infinite expansion.
10357 if Has_Predicates (Target_Typ) then
10358 if Nkind (Parent (N)) = N_Function_Call
10359 and then Present (Name (Parent (N)))
10360 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
10362 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
10366 -- In the case of a qualified expression in an allocator, the check
10367 -- is applied when expanding the allocator, so avoid redundant check.
10369 elsif Nkind (N) = N_Qualified_Expression
10370 and then Nkind (Parent (N)) /= N_Allocator
10372 Apply_Predicate_Check (N, Target_Typ);
10375 end Resolve_Qualified_Expression;
10377 ------------------------------
10378 -- Resolve_Raise_Expression --
10379 ------------------------------
10381 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
10383 if Typ = Raise_Type then
10384 Error_Msg_N ("cannot find unique type for raise expression", N);
10385 Set_Etype (N, Any_Type);
10387 Set_Etype (N, Typ);
10389 end Resolve_Raise_Expression;
10391 -------------------
10392 -- Resolve_Range --
10393 -------------------
10395 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
10396 L : constant Node_Id := Low_Bound (N);
10397 H : constant Node_Id := High_Bound (N);
10399 function First_Last_Ref return Boolean;
10400 -- Returns True if N is of the form X'First .. X'Last where X is the
10401 -- same entity for both attributes.
10403 --------------------
10404 -- First_Last_Ref --
10405 --------------------
10407 function First_Last_Ref return Boolean is
10408 Lorig : constant Node_Id := Original_Node (L);
10409 Horig : constant Node_Id := Original_Node (H);
10412 if Nkind (Lorig) = N_Attribute_Reference
10413 and then Nkind (Horig) = N_Attribute_Reference
10414 and then Attribute_Name (Lorig) = Name_First
10415 and then Attribute_Name (Horig) = Name_Last
10418 PL : constant Node_Id := Prefix (Lorig);
10419 PH : constant Node_Id := Prefix (Horig);
10421 if Is_Entity_Name (PL)
10422 and then Is_Entity_Name (PH)
10423 and then Entity (PL) = Entity (PH)
10431 end First_Last_Ref;
10433 -- Start of processing for Resolve_Range
10436 Set_Etype (N, Typ);
10438 -- The lower bound should be in Typ. The higher bound can be in Typ's
10439 -- base type if the range is null. It may still be invalid if it is
10440 -- higher than the lower bound. This is checked later in the context in
10441 -- which the range appears.
10444 Resolve (H, Base_Type (Typ));
10446 -- Reanalyze the lower bound after both bounds have been analyzed, so
10447 -- that the range is known to be static or not by now. This may trigger
10448 -- more compile-time evaluation, which is useful for static analysis
10449 -- with GNATprove. This is not needed for compilation or static analysis
10450 -- with CodePeer, as full expansion does that evaluation then.
10452 if GNATprove_Mode then
10453 Set_Analyzed (L, False);
10457 -- Check for inappropriate range on unordered enumeration type
10459 if Bad_Unordered_Enumeration_Reference (N, Typ)
10461 -- Exclude X'First .. X'Last if X is the same entity for both
10463 and then not First_Last_Ref
10465 Error_Msg_Sloc := Sloc (Typ);
10467 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
10470 Check_Unset_Reference (L);
10471 Check_Unset_Reference (H);
10473 -- We have to check the bounds for being within the base range as
10474 -- required for a non-static context. Normally this is automatic and
10475 -- done as part of evaluating expressions, but the N_Range node is an
10476 -- exception, since in GNAT we consider this node to be a subexpression,
10477 -- even though in Ada it is not. The circuit in Sem_Eval could check for
10478 -- this, but that would put the test on the main evaluation path for
10481 Check_Non_Static_Context (L);
10482 Check_Non_Static_Context (H);
10484 -- Check for an ambiguous range over character literals. This will
10485 -- happen with a membership test involving only literals.
10487 if Typ = Any_Character then
10488 Ambiguous_Character (L);
10489 Set_Etype (N, Any_Type);
10493 -- If bounds are static, constant-fold them, so size computations are
10494 -- identical between front-end and back-end. Do not perform this
10495 -- transformation while analyzing generic units, as type information
10496 -- would be lost when reanalyzing the constant node in the instance.
10498 if Is_Discrete_Type (Typ) and then Expander_Active then
10499 if Is_OK_Static_Expression (L) then
10500 Fold_Uint (L, Expr_Value (L), Is_OK_Static_Expression (L));
10503 if Is_OK_Static_Expression (H) then
10504 Fold_Uint (H, Expr_Value (H), Is_OK_Static_Expression (H));
10509 --------------------------
10510 -- Resolve_Real_Literal --
10511 --------------------------
10513 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
10514 Actual_Typ : constant Entity_Id := Etype (N);
10517 -- Special processing for fixed-point literals to make sure that the
10518 -- value is an exact multiple of small where this is required. We skip
10519 -- this for the universal real case, and also for generic types.
10521 if Is_Fixed_Point_Type (Typ)
10522 and then Typ /= Universal_Fixed
10523 and then Typ /= Any_Fixed
10524 and then not Is_Generic_Type (Typ)
10527 Val : constant Ureal := Realval (N);
10528 Cintr : constant Ureal := Val / Small_Value (Typ);
10529 Cint : constant Uint := UR_Trunc (Cintr);
10530 Den : constant Uint := Norm_Den (Cintr);
10534 -- Case of literal is not an exact multiple of the Small
10538 -- For a source program literal for a decimal fixed-point type,
10539 -- this is statically illegal (RM 4.9(36)).
10541 if Is_Decimal_Fixed_Point_Type (Typ)
10542 and then Actual_Typ = Universal_Real
10543 and then Comes_From_Source (N)
10545 Error_Msg_N ("value has extraneous low order digits", N);
10548 -- Generate a warning if literal from source
10550 if Is_OK_Static_Expression (N)
10551 and then Warn_On_Bad_Fixed_Value
10554 ("?b?static fixed-point value is not a multiple of Small!",
10558 -- Replace literal by a value that is the exact representation
10559 -- of a value of the type, i.e. a multiple of the small value,
10560 -- by truncation, since Machine_Rounds is false for all GNAT
10561 -- fixed-point types (RM 4.9(38)).
10563 Stat := Is_OK_Static_Expression (N);
10565 Make_Real_Literal (Sloc (N),
10566 Realval => Small_Value (Typ) * Cint));
10568 Set_Is_Static_Expression (N, Stat);
10571 -- In all cases, set the corresponding integer field
10573 Set_Corresponding_Integer_Value (N, Cint);
10577 -- Now replace the actual type by the expected type as usual
10579 Set_Etype (N, Typ);
10580 Eval_Real_Literal (N);
10581 end Resolve_Real_Literal;
10583 -----------------------
10584 -- Resolve_Reference --
10585 -----------------------
10587 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
10588 P : constant Node_Id := Prefix (N);
10591 -- Replace general access with specific type
10593 if Ekind (Etype (N)) = E_Allocator_Type then
10594 Set_Etype (N, Base_Type (Typ));
10597 Resolve (P, Designated_Type (Etype (N)));
10599 -- If we are taking the reference of a volatile entity, then treat it as
10600 -- a potential modification of this entity. This is too conservative,
10601 -- but necessary because remove side effects can cause transformations
10602 -- of normal assignments into reference sequences that otherwise fail to
10603 -- notice the modification.
10605 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
10606 Note_Possible_Modification (P, Sure => False);
10608 end Resolve_Reference;
10610 --------------------------------
10611 -- Resolve_Selected_Component --
10612 --------------------------------
10614 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
10616 Comp1 : Entity_Id := Empty; -- prevent junk warning
10617 P : constant Node_Id := Prefix (N);
10618 S : constant Node_Id := Selector_Name (N);
10619 T : Entity_Id := Etype (P);
10621 I1 : Interp_Index := 0; -- prevent junk warning
10626 function Init_Component return Boolean;
10627 -- Check whether this is the initialization of a component within an
10628 -- init proc (by assignment or call to another init proc). If true,
10629 -- there is no need for a discriminant check.
10631 --------------------
10632 -- Init_Component --
10633 --------------------
10635 function Init_Component return Boolean is
10637 return Inside_Init_Proc
10638 and then Nkind (Prefix (N)) = N_Identifier
10639 and then Chars (Prefix (N)) = Name_uInit
10640 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
10641 end Init_Component;
10643 -- Start of processing for Resolve_Selected_Component
10646 if Is_Overloaded (P) then
10648 -- Use the context type to select the prefix that has a selector
10649 -- of the correct name and type.
10652 Get_First_Interp (P, I, It);
10654 Search : while Present (It.Typ) loop
10655 if Is_Access_Type (It.Typ) then
10656 T := Designated_Type (It.Typ);
10661 -- Locate selected component. For a private prefix the selector
10662 -- can denote a discriminant.
10664 if Is_Record_Type (T) or else Is_Private_Type (T) then
10666 -- The visible components of a class-wide type are those of
10669 if Is_Class_Wide_Type (T) then
10673 Comp := First_Entity (T);
10674 while Present (Comp) loop
10675 if Chars (Comp) = Chars (S)
10676 and then Covers (Typ, Etype (Comp))
10685 It := Disambiguate (P, I1, I, Any_Type);
10687 if It = No_Interp then
10689 ("ambiguous prefix for selected component", N);
10690 Set_Etype (N, Typ);
10696 -- There may be an implicit dereference. Retrieve
10697 -- designated record type.
10699 if Is_Access_Type (It1.Typ) then
10700 T := Designated_Type (It1.Typ);
10705 if Scope (Comp1) /= T then
10707 -- Resolution chooses the new interpretation.
10708 -- Find the component with the right name.
10710 Comp1 := First_Entity (T);
10711 while Present (Comp1)
10712 and then Chars (Comp1) /= Chars (S)
10714 Comp1 := Next_Entity (Comp1);
10723 Comp := Next_Entity (Comp);
10727 Get_Next_Interp (I, It);
10730 -- There must be a legal interpretation at this point
10732 pragma Assert (Found);
10733 Resolve (P, It1.Typ);
10735 -- In general the expected type is the type of the context, not the
10736 -- type of the candidate selected component.
10738 Set_Etype (N, Typ);
10739 Set_Entity_With_Checks (S, Comp1);
10741 -- The type of the context and that of the component are
10742 -- compatible and in general identical, but if they are anonymous
10743 -- access-to-subprogram types, the relevant type is that of the
10744 -- component. This matters in Unnest_Subprograms mode, where the
10745 -- relevant context is the one in which the type is declared, not
10746 -- the point of use. This determines what activation record to use.
10748 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
10749 Set_Etype (N, Etype (Comp1));
10751 -- When the type of the component is an access to a class-wide type
10752 -- the relevant type is that of the component (since in such case we
10753 -- may need to generate implicit type conversions or dispatching
10756 elsif Is_Access_Type (Typ)
10757 and then not Is_Class_Wide_Type (Designated_Type (Typ))
10758 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
10760 Set_Etype (N, Etype (Comp1));
10764 -- Resolve prefix with its type
10769 -- Generate cross-reference. We needed to wait until full overloading
10770 -- resolution was complete to do this, since otherwise we can't tell if
10771 -- we are an lvalue or not.
10773 if May_Be_Lvalue (N) then
10774 Generate_Reference (Entity (S), S, 'm');
10776 Generate_Reference (Entity (S), S, 'r');
10779 -- If prefix is an access type, the node will be transformed into an
10780 -- explicit dereference during expansion. The type of the node is the
10781 -- designated type of that of the prefix.
10783 if Is_Access_Type (Etype (P)) then
10784 T := Designated_Type (Etype (P));
10785 Check_Fully_Declared_Prefix (T, P);
10790 -- If the prefix is an entity it may have a deferred reference set
10791 -- during analysis of the selected component. After resolution we
10792 -- can transform it into a proper reference. This prevents spurious
10793 -- warnings on useless assignments when the same selected component
10794 -- is the actual for an out parameter in a subsequent call.
10796 if Is_Entity_Name (P)
10797 and then Has_Deferred_Reference (Entity (P))
10799 if May_Be_Lvalue (N) then
10800 Generate_Reference (Entity (P), P, 'm');
10802 Generate_Reference (Entity (P), P, 'r');
10807 -- Set flag for expander if discriminant check required on a component
10808 -- appearing within a variant.
10810 if Has_Discriminants (T)
10811 and then Ekind (Entity (S)) = E_Component
10812 and then Present (Original_Record_Component (Entity (S)))
10813 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
10815 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
10816 and then not Discriminant_Checks_Suppressed (T)
10817 and then not Init_Component
10819 Set_Do_Discriminant_Check (N);
10822 if Ekind (Entity (S)) = E_Void then
10823 Error_Msg_N ("premature use of component", S);
10826 -- If the prefix is a record conversion, this may be a renamed
10827 -- discriminant whose bounds differ from those of the original
10828 -- one, so we must ensure that a range check is performed.
10830 if Nkind (P) = N_Type_Conversion
10831 and then Ekind (Entity (S)) = E_Discriminant
10832 and then Is_Discrete_Type (Typ)
10834 Set_Etype (N, Base_Type (Typ));
10837 -- Note: No Eval processing is required, because the prefix is of a
10838 -- record type, or protected type, and neither can possibly be static.
10840 -- If the record type is atomic, and the component is non-atomic, then
10841 -- this is worth a warning, since we have a situation where the access
10842 -- to the component may cause extra read/writes of the atomic array
10843 -- object, or partial word accesses, both of which may be unexpected.
10845 if Nkind (N) = N_Selected_Component
10846 and then Is_Atomic_Ref_With_Address (N)
10847 and then not Is_Atomic (Entity (S))
10848 and then not Is_Atomic (Etype (Entity (S)))
10851 ("??access to non-atomic component of atomic record",
10854 ("\??may cause unexpected accesses to atomic object",
10858 Analyze_Dimension (N);
10859 end Resolve_Selected_Component;
10861 -------------------
10862 -- Resolve_Shift --
10863 -------------------
10865 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
10866 B_Typ : constant Entity_Id := Base_Type (Typ);
10867 L : constant Node_Id := Left_Opnd (N);
10868 R : constant Node_Id := Right_Opnd (N);
10871 -- We do the resolution using the base type, because intermediate values
10872 -- in expressions always are of the base type, not a subtype of it.
10874 Resolve (L, B_Typ);
10875 Resolve (R, Standard_Natural);
10877 Check_Unset_Reference (L);
10878 Check_Unset_Reference (R);
10880 Set_Etype (N, B_Typ);
10881 Generate_Operator_Reference (N, B_Typ);
10885 ---------------------------
10886 -- Resolve_Short_Circuit --
10887 ---------------------------
10889 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
10890 B_Typ : constant Entity_Id := Base_Type (Typ);
10891 L : constant Node_Id := Left_Opnd (N);
10892 R : constant Node_Id := Right_Opnd (N);
10895 -- Ensure all actions associated with the left operand (e.g.
10896 -- finalization of transient objects) are fully evaluated locally within
10897 -- an expression with actions. This is particularly helpful for coverage
10898 -- analysis. However this should not happen in generics or if option
10899 -- Minimize_Expression_With_Actions is set.
10901 if Expander_Active and not Minimize_Expression_With_Actions then
10903 Reloc_L : constant Node_Id := Relocate_Node (L);
10905 Save_Interps (Old_N => L, New_N => Reloc_L);
10908 Make_Expression_With_Actions (Sloc (L),
10909 Actions => New_List,
10910 Expression => Reloc_L));
10912 -- Set Comes_From_Source on L to preserve warnings for unset
10915 Set_Comes_From_Source (L, Comes_From_Source (Reloc_L));
10919 Resolve (L, B_Typ);
10920 Resolve (R, B_Typ);
10922 -- Check for issuing warning for always False assert/check, this happens
10923 -- when assertions are turned off, in which case the pragma Assert/Check
10924 -- was transformed into:
10926 -- if False and then <condition> then ...
10928 -- and we detect this pattern
10930 if Warn_On_Assertion_Failure
10931 and then Is_Entity_Name (R)
10932 and then Entity (R) = Standard_False
10933 and then Nkind (Parent (N)) = N_If_Statement
10934 and then Nkind (N) = N_And_Then
10935 and then Is_Entity_Name (L)
10936 and then Entity (L) = Standard_False
10939 Orig : constant Node_Id := Original_Node (Parent (N));
10942 -- Special handling of Asssert pragma
10944 if Nkind (Orig) = N_Pragma
10945 and then Pragma_Name (Orig) = Name_Assert
10948 Expr : constant Node_Id :=
10951 (First (Pragma_Argument_Associations (Orig))));
10954 -- Don't warn if original condition is explicit False,
10955 -- since obviously the failure is expected in this case.
10957 if Is_Entity_Name (Expr)
10958 and then Entity (Expr) = Standard_False
10962 -- Issue warning. We do not want the deletion of the
10963 -- IF/AND-THEN to take this message with it. We achieve this
10964 -- by making sure that the expanded code points to the Sloc
10965 -- of the expression, not the original pragma.
10968 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
10969 -- The source location of the expression is not usually
10970 -- the best choice here. For example, it gets located on
10971 -- the last AND keyword in a chain of boolean expressiond
10972 -- AND'ed together. It is best to put the message on the
10973 -- first character of the assertion, which is the effect
10974 -- of the First_Node call here.
10977 ("?A?assertion would fail at run time!",
10979 (First (Pragma_Argument_Associations (Orig))));
10983 -- Similar processing for Check pragma
10985 elsif Nkind (Orig) = N_Pragma
10986 and then Pragma_Name (Orig) = Name_Check
10988 -- Don't want to warn if original condition is explicit False
10991 Expr : constant Node_Id :=
10994 (Next (First (Pragma_Argument_Associations (Orig)))));
10996 if Is_Entity_Name (Expr)
10997 and then Entity (Expr) = Standard_False
11004 -- Again use Error_Msg_F rather than Error_Msg_N, see
11005 -- comment above for an explanation of why we do this.
11008 ("?A?check would fail at run time!",
11010 (Last (Pragma_Argument_Associations (Orig))));
11017 -- Continue with processing of short circuit
11019 Check_Unset_Reference (L);
11020 Check_Unset_Reference (R);
11022 Set_Etype (N, B_Typ);
11023 Eval_Short_Circuit (N);
11024 end Resolve_Short_Circuit;
11026 -------------------
11027 -- Resolve_Slice --
11028 -------------------
11030 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11031 Drange : constant Node_Id := Discrete_Range (N);
11032 Name : constant Node_Id := Prefix (N);
11033 Array_Type : Entity_Id := Empty;
11034 Dexpr : Node_Id := Empty;
11035 Index_Type : Entity_Id;
11038 if Is_Overloaded (Name) then
11040 -- Use the context type to select the prefix that yields the correct
11045 I1 : Interp_Index := 0;
11047 P : constant Node_Id := Prefix (N);
11048 Found : Boolean := False;
11051 Get_First_Interp (P, I, It);
11052 while Present (It.Typ) loop
11053 if (Is_Array_Type (It.Typ)
11054 and then Covers (Typ, It.Typ))
11055 or else (Is_Access_Type (It.Typ)
11056 and then Is_Array_Type (Designated_Type (It.Typ))
11057 and then Covers (Typ, Designated_Type (It.Typ)))
11060 It := Disambiguate (P, I1, I, Any_Type);
11062 if It = No_Interp then
11063 Error_Msg_N ("ambiguous prefix for slicing", N);
11064 Set_Etype (N, Typ);
11068 Array_Type := It.Typ;
11073 Array_Type := It.Typ;
11078 Get_Next_Interp (I, It);
11083 Array_Type := Etype (Name);
11086 Resolve (Name, Array_Type);
11088 if Is_Access_Type (Array_Type) then
11089 Apply_Access_Check (N);
11090 Array_Type := Designated_Type (Array_Type);
11092 -- If the prefix is an access to an unconstrained array, we must use
11093 -- the actual subtype of the object to perform the index checks. The
11094 -- object denoted by the prefix is implicit in the node, so we build
11095 -- an explicit representation for it in order to compute the actual
11098 if not Is_Constrained (Array_Type) then
11099 Remove_Side_Effects (Prefix (N));
11102 Obj : constant Node_Id :=
11103 Make_Explicit_Dereference (Sloc (N),
11104 Prefix => New_Copy_Tree (Prefix (N)));
11106 Set_Etype (Obj, Array_Type);
11107 Set_Parent (Obj, Parent (N));
11108 Array_Type := Get_Actual_Subtype (Obj);
11112 elsif Is_Entity_Name (Name)
11113 or else Nkind (Name) = N_Explicit_Dereference
11114 or else (Nkind (Name) = N_Function_Call
11115 and then not Is_Constrained (Etype (Name)))
11117 Array_Type := Get_Actual_Subtype (Name);
11119 -- If the name is a selected component that depends on discriminants,
11120 -- build an actual subtype for it. This can happen only when the name
11121 -- itself is overloaded; otherwise the actual subtype is created when
11122 -- the selected component is analyzed.
11124 elsif Nkind (Name) = N_Selected_Component
11125 and then Full_Analysis
11126 and then Depends_On_Discriminant (First_Index (Array_Type))
11129 Act_Decl : constant Node_Id :=
11130 Build_Actual_Subtype_Of_Component (Array_Type, Name);
11132 Insert_Action (N, Act_Decl);
11133 Array_Type := Defining_Identifier (Act_Decl);
11136 -- Maybe this should just be "else", instead of checking for the
11137 -- specific case of slice??? This is needed for the case where the
11138 -- prefix is an Image attribute, which gets expanded to a slice, and so
11139 -- has a constrained subtype which we want to use for the slice range
11140 -- check applied below (the range check won't get done if the
11141 -- unconstrained subtype of the 'Image is used).
11143 elsif Nkind (Name) = N_Slice then
11144 Array_Type := Etype (Name);
11147 -- Obtain the type of the array index
11149 if Ekind (Array_Type) = E_String_Literal_Subtype then
11150 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11152 Index_Type := Etype (First_Index (Array_Type));
11155 -- If name was overloaded, set slice type correctly now
11157 Set_Etype (N, Array_Type);
11159 -- Handle the generation of a range check that compares the array index
11160 -- against the discrete_range. The check is not applied to internally
11161 -- built nodes associated with the expansion of dispatch tables. Check
11162 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11165 if Tagged_Type_Expansion
11166 and then RTU_Loaded (Ada_Tags)
11167 and then Nkind (Prefix (N)) = N_Selected_Component
11168 and then Present (Entity (Selector_Name (Prefix (N))))
11169 and then Entity (Selector_Name (Prefix (N))) =
11170 RTE_Record_Component (RE_Prims_Ptr)
11174 -- The discrete_range is specified by a subtype indication. Create a
11175 -- shallow copy and inherit the type, parent and source location from
11176 -- the discrete_range. This ensures that the range check is inserted
11177 -- relative to the slice and that the runtime exception points to the
11178 -- proper construct.
11180 elsif Is_Entity_Name (Drange) then
11181 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
11183 Set_Etype (Dexpr, Etype (Drange));
11184 Set_Parent (Dexpr, Parent (Drange));
11185 Set_Sloc (Dexpr, Sloc (Drange));
11187 -- The discrete_range is a regular range. Resolve the bounds and remove
11188 -- their side effects.
11191 Resolve (Drange, Base_Type (Index_Type));
11193 if Nkind (Drange) = N_Range then
11194 Force_Evaluation (Low_Bound (Drange));
11195 Force_Evaluation (High_Bound (Drange));
11201 if Present (Dexpr) then
11202 Apply_Range_Check (Dexpr, Index_Type);
11205 Set_Slice_Subtype (N);
11207 -- Check bad use of type with predicates
11213 if Nkind (Drange) = N_Subtype_Indication
11214 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11216 Subt := Entity (Subtype_Mark (Drange));
11218 Subt := Etype (Drange);
11221 if Has_Predicates (Subt) then
11222 Bad_Predicated_Subtype_Use
11223 ("subtype& has predicate, not allowed in slice", Drange, Subt);
11227 -- Otherwise here is where we check suspicious indexes
11229 if Nkind (Drange) = N_Range then
11230 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
11231 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
11234 Analyze_Dimension (N);
11238 ----------------------------
11239 -- Resolve_String_Literal --
11240 ----------------------------
11242 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
11243 C_Typ : constant Entity_Id := Component_Type (Typ);
11244 R_Typ : constant Entity_Id := Root_Type (C_Typ);
11245 Loc : constant Source_Ptr := Sloc (N);
11246 Str : constant String_Id := Strval (N);
11247 Strlen : constant Nat := String_Length (Str);
11248 Subtype_Id : Entity_Id;
11249 Need_Check : Boolean;
11252 -- For a string appearing in a concatenation, defer creation of the
11253 -- string_literal_subtype until the end of the resolution of the
11254 -- concatenation, because the literal may be constant-folded away. This
11255 -- is a useful optimization for long concatenation expressions.
11257 -- If the string is an aggregate built for a single character (which
11258 -- happens in a non-static context) or a is null string to which special
11259 -- checks may apply, we build the subtype. Wide strings must also get a
11260 -- string subtype if they come from a one character aggregate. Strings
11261 -- generated by attributes might be static, but it is often hard to
11262 -- determine whether the enclosing context is static, so we generate
11263 -- subtypes for them as well, thus losing some rarer optimizations ???
11264 -- Same for strings that come from a static conversion.
11267 (Strlen = 0 and then Typ /= Standard_String)
11268 or else Nkind (Parent (N)) /= N_Op_Concat
11269 or else (N /= Left_Opnd (Parent (N))
11270 and then N /= Right_Opnd (Parent (N)))
11271 or else ((Typ = Standard_Wide_String
11272 or else Typ = Standard_Wide_Wide_String)
11273 and then Nkind (Original_Node (N)) /= N_String_Literal);
11275 -- If the resolving type is itself a string literal subtype, we can just
11276 -- reuse it, since there is no point in creating another.
11278 if Ekind (Typ) = E_String_Literal_Subtype then
11281 elsif Nkind (Parent (N)) = N_Op_Concat
11282 and then not Need_Check
11283 and then not Nkind_In (Original_Node (N), N_Character_Literal,
11284 N_Attribute_Reference,
11285 N_Qualified_Expression,
11290 -- Do not generate a string literal subtype for the default expression
11291 -- of a formal parameter in GNATprove mode. This is because the string
11292 -- subtype is associated with the freezing actions of the subprogram,
11293 -- however freezing is disabled in GNATprove mode and as a result the
11294 -- subtype is unavailable.
11296 elsif GNATprove_Mode
11297 and then Nkind (Parent (N)) = N_Parameter_Specification
11301 -- Otherwise we must create a string literal subtype. Note that the
11302 -- whole idea of string literal subtypes is simply to avoid the need
11303 -- for building a full fledged array subtype for each literal.
11306 Set_String_Literal_Subtype (N, Typ);
11307 Subtype_Id := Etype (N);
11310 if Nkind (Parent (N)) /= N_Op_Concat
11313 Set_Etype (N, Subtype_Id);
11314 Eval_String_Literal (N);
11317 if Is_Limited_Composite (Typ)
11318 or else Is_Private_Composite (Typ)
11320 Error_Msg_N ("string literal not available for private array", N);
11321 Set_Etype (N, Any_Type);
11325 -- The validity of a null string has been checked in the call to
11326 -- Eval_String_Literal.
11331 -- Always accept string literal with component type Any_Character, which
11332 -- occurs in error situations and in comparisons of literals, both of
11333 -- which should accept all literals.
11335 elsif R_Typ = Any_Character then
11338 -- If the type is bit-packed, then we always transform the string
11339 -- literal into a full fledged aggregate.
11341 elsif Is_Bit_Packed_Array (Typ) then
11344 -- Deal with cases of Wide_Wide_String, Wide_String, and String
11347 -- For Standard.Wide_Wide_String, or any other type whose component
11348 -- type is Standard.Wide_Wide_Character, we know that all the
11349 -- characters in the string must be acceptable, since the parser
11350 -- accepted the characters as valid character literals.
11352 if R_Typ = Standard_Wide_Wide_Character then
11355 -- For the case of Standard.String, or any other type whose component
11356 -- type is Standard.Character, we must make sure that there are no
11357 -- wide characters in the string, i.e. that it is entirely composed
11358 -- of characters in range of type Character.
11360 -- If the string literal is the result of a static concatenation, the
11361 -- test has already been performed on the components, and need not be
11364 elsif R_Typ = Standard_Character
11365 and then Nkind (Original_Node (N)) /= N_Op_Concat
11367 for J in 1 .. Strlen loop
11368 if not In_Character_Range (Get_String_Char (Str, J)) then
11370 -- If we are out of range, post error. This is one of the
11371 -- very few places that we place the flag in the middle of
11372 -- a token, right under the offending wide character. Not
11373 -- quite clear if this is right wrt wide character encoding
11374 -- sequences, but it's only an error message.
11377 ("literal out of range of type Standard.Character",
11378 Source_Ptr (Int (Loc) + J));
11383 -- For the case of Standard.Wide_String, or any other type whose
11384 -- component type is Standard.Wide_Character, we must make sure that
11385 -- there are no wide characters in the string, i.e. that it is
11386 -- entirely composed of characters in range of type Wide_Character.
11388 -- If the string literal is the result of a static concatenation,
11389 -- the test has already been performed on the components, and need
11390 -- not be repeated.
11392 elsif R_Typ = Standard_Wide_Character
11393 and then Nkind (Original_Node (N)) /= N_Op_Concat
11395 for J in 1 .. Strlen loop
11396 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
11398 -- If we are out of range, post error. This is one of the
11399 -- very few places that we place the flag in the middle of
11400 -- a token, right under the offending wide character.
11402 -- This is not quite right, because characters in general
11403 -- will take more than one character position ???
11406 ("literal out of range of type Standard.Wide_Character",
11407 Source_Ptr (Int (Loc) + J));
11412 -- If the root type is not a standard character, then we will convert
11413 -- the string into an aggregate and will let the aggregate code do
11414 -- the checking. Standard Wide_Wide_Character is also OK here.
11420 -- See if the component type of the array corresponding to the string
11421 -- has compile time known bounds. If yes we can directly check
11422 -- whether the evaluation of the string will raise constraint error.
11423 -- Otherwise we need to transform the string literal into the
11424 -- corresponding character aggregate and let the aggregate code do
11425 -- the checking. We use the same transformation if the component
11426 -- type has a static predicate, which will be applied to each
11427 -- character when the aggregate is resolved.
11429 if Is_Standard_Character_Type (R_Typ) then
11431 -- Check for the case of full range, where we are definitely OK
11433 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
11437 -- Here the range is not the complete base type range, so check
11440 Comp_Typ_Lo : constant Node_Id :=
11441 Type_Low_Bound (Component_Type (Typ));
11442 Comp_Typ_Hi : constant Node_Id :=
11443 Type_High_Bound (Component_Type (Typ));
11448 if Compile_Time_Known_Value (Comp_Typ_Lo)
11449 and then Compile_Time_Known_Value (Comp_Typ_Hi)
11451 for J in 1 .. Strlen loop
11452 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
11454 if Char_Val < Expr_Value (Comp_Typ_Lo)
11455 or else Char_Val > Expr_Value (Comp_Typ_Hi)
11457 Apply_Compile_Time_Constraint_Error
11458 (N, "character out of range??",
11459 CE_Range_Check_Failed,
11460 Loc => Source_Ptr (Int (Loc) + J));
11464 if not Has_Static_Predicate (C_Typ) then
11472 -- If we got here we meed to transform the string literal into the
11473 -- equivalent qualified positional array aggregate. This is rather
11474 -- heavy artillery for this situation, but it is hard work to avoid.
11477 Lits : constant List_Id := New_List;
11478 P : Source_Ptr := Loc + 1;
11482 -- Build the character literals, we give them source locations that
11483 -- correspond to the string positions, which is a bit tricky given
11484 -- the possible presence of wide character escape sequences.
11486 for J in 1 .. Strlen loop
11487 C := Get_String_Char (Str, J);
11488 Set_Character_Literal_Name (C);
11491 Make_Character_Literal (P,
11492 Chars => Name_Find,
11493 Char_Literal_Value => UI_From_CC (C)));
11495 if In_Character_Range (C) then
11498 -- Should we have a call to Skip_Wide here ???
11507 Make_Qualified_Expression (Loc,
11508 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
11510 Make_Aggregate (Loc, Expressions => Lits)));
11512 Analyze_And_Resolve (N, Typ);
11514 end Resolve_String_Literal;
11516 -------------------------
11517 -- Resolve_Target_Name --
11518 -------------------------
11520 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
11522 Set_Etype (N, Typ);
11523 end Resolve_Target_Name;
11525 -----------------------------
11526 -- Resolve_Type_Conversion --
11527 -----------------------------
11529 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
11530 Conv_OK : constant Boolean := Conversion_OK (N);
11531 Operand : constant Node_Id := Expression (N);
11532 Operand_Typ : constant Entity_Id := Etype (Operand);
11533 Target_Typ : constant Entity_Id := Etype (N);
11538 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
11539 -- Set to False to suppress cases where we want to suppress the test
11540 -- for redundancy to avoid possible false positives on this warning.
11544 and then not Valid_Conversion (N, Target_Typ, Operand)
11549 -- If the Operand Etype is Universal_Fixed, then the conversion is
11550 -- never redundant. We need this check because by the time we have
11551 -- finished the rather complex transformation, the conversion looks
11552 -- redundant when it is not.
11554 if Operand_Typ = Universal_Fixed then
11555 Test_Redundant := False;
11557 -- If the operand is marked as Any_Fixed, then special processing is
11558 -- required. This is also a case where we suppress the test for a
11559 -- redundant conversion, since most certainly it is not redundant.
11561 elsif Operand_Typ = Any_Fixed then
11562 Test_Redundant := False;
11564 -- Mixed-mode operation involving a literal. Context must be a fixed
11565 -- type which is applied to the literal subsequently.
11567 -- Multiplication and division involving two fixed type operands must
11568 -- yield a universal real because the result is computed in arbitrary
11571 if Is_Fixed_Point_Type (Typ)
11572 and then Nkind_In (Operand, N_Op_Divide, N_Op_Multiply)
11573 and then Etype (Left_Opnd (Operand)) = Any_Fixed
11574 and then Etype (Right_Opnd (Operand)) = Any_Fixed
11576 Set_Etype (Operand, Universal_Real);
11578 elsif Is_Numeric_Type (Typ)
11579 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
11580 and then (Etype (Right_Opnd (Operand)) = Universal_Real
11582 Etype (Left_Opnd (Operand)) = Universal_Real)
11584 -- Return if expression is ambiguous
11586 if Unique_Fixed_Point_Type (N) = Any_Type then
11589 -- If nothing else, the available fixed type is Duration
11592 Set_Etype (Operand, Standard_Duration);
11595 -- Resolve the real operand with largest available precision
11597 if Etype (Right_Opnd (Operand)) = Universal_Real then
11598 Rop := New_Copy_Tree (Right_Opnd (Operand));
11600 Rop := New_Copy_Tree (Left_Opnd (Operand));
11603 Resolve (Rop, Universal_Real);
11605 -- If the operand is a literal (it could be a non-static and
11606 -- illegal exponentiation) check whether the use of Duration
11607 -- is potentially inaccurate.
11609 if Nkind (Rop) = N_Real_Literal
11610 and then Realval (Rop) /= Ureal_0
11611 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
11614 ("??universal real operand can only "
11615 & "be interpreted as Duration!", Rop);
11617 ("\??precision will be lost in the conversion!", Rop);
11620 elsif Is_Numeric_Type (Typ)
11621 and then Nkind (Operand) in N_Op
11622 and then Unique_Fixed_Point_Type (N) /= Any_Type
11624 Set_Etype (Operand, Standard_Duration);
11627 Error_Msg_N ("invalid context for mixed mode operation", N);
11628 Set_Etype (Operand, Any_Type);
11635 -- In SPARK, a type conversion between array types should be restricted
11636 -- to types which have matching static bounds.
11638 -- Protect call to Matching_Static_Array_Bounds to avoid costly
11639 -- operation if not needed.
11641 if Restriction_Check_Required (SPARK_05)
11642 and then Is_Array_Type (Target_Typ)
11643 and then Is_Array_Type (Operand_Typ)
11644 and then Operand_Typ /= Any_Composite -- or else Operand in error
11645 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
11647 Check_SPARK_05_Restriction
11648 ("array types should have matching static bounds", N);
11651 -- In formal mode, the operand of an ancestor type conversion must be an
11652 -- object (not an expression).
11654 if Is_Tagged_Type (Target_Typ)
11655 and then not Is_Class_Wide_Type (Target_Typ)
11656 and then Is_Tagged_Type (Operand_Typ)
11657 and then not Is_Class_Wide_Type (Operand_Typ)
11658 and then Is_Ancestor (Target_Typ, Operand_Typ)
11659 and then not Is_SPARK_05_Object_Reference (Operand)
11661 Check_SPARK_05_Restriction ("object required", Operand);
11664 Analyze_Dimension (N);
11666 -- Note: we do the Eval_Type_Conversion call before applying the
11667 -- required checks for a subtype conversion. This is important, since
11668 -- both are prepared under certain circumstances to change the type
11669 -- conversion to a constraint error node, but in the case of
11670 -- Eval_Type_Conversion this may reflect an illegality in the static
11671 -- case, and we would miss the illegality (getting only a warning
11672 -- message), if we applied the type conversion checks first.
11674 Eval_Type_Conversion (N);
11676 -- Even when evaluation is not possible, we may be able to simplify the
11677 -- conversion or its expression. This needs to be done before applying
11678 -- checks, since otherwise the checks may use the original expression
11679 -- and defeat the simplifications. This is specifically the case for
11680 -- elimination of the floating-point Truncation attribute in
11681 -- float-to-int conversions.
11683 Simplify_Type_Conversion (N);
11685 -- If after evaluation we still have a type conversion, then we may need
11686 -- to apply checks required for a subtype conversion.
11688 -- Skip these type conversion checks if universal fixed operands
11689 -- operands involved, since range checks are handled separately for
11690 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
11692 if Nkind (N) = N_Type_Conversion
11693 and then not Is_Generic_Type (Root_Type (Target_Typ))
11694 and then Target_Typ /= Universal_Fixed
11695 and then Operand_Typ /= Universal_Fixed
11697 Apply_Type_Conversion_Checks (N);
11700 -- Issue warning for conversion of simple object to its own type. We
11701 -- have to test the original nodes, since they may have been rewritten
11702 -- by various optimizations.
11704 Orig_N := Original_Node (N);
11706 -- Here we test for a redundant conversion if the warning mode is
11707 -- active (and was not locally reset), and we have a type conversion
11708 -- from source not appearing in a generic instance.
11711 and then Nkind (Orig_N) = N_Type_Conversion
11712 and then Comes_From_Source (Orig_N)
11713 and then not In_Instance
11715 Orig_N := Original_Node (Expression (Orig_N));
11716 Orig_T := Target_Typ;
11718 -- If the node is part of a larger expression, the Target_Type
11719 -- may not be the original type of the node if the context is a
11720 -- condition. Recover original type to see if conversion is needed.
11722 if Is_Boolean_Type (Orig_T)
11723 and then Nkind (Parent (N)) in N_Op
11725 Orig_T := Etype (Parent (N));
11728 -- If we have an entity name, then give the warning if the entity
11729 -- is the right type, or if it is a loop parameter covered by the
11730 -- original type (that's needed because loop parameters have an
11731 -- odd subtype coming from the bounds).
11733 if (Is_Entity_Name (Orig_N)
11735 (Etype (Entity (Orig_N)) = Orig_T
11737 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
11738 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
11740 -- If not an entity, then type of expression must match
11742 or else Etype (Orig_N) = Orig_T
11744 -- One more check, do not give warning if the analyzed conversion
11745 -- has an expression with non-static bounds, and the bounds of the
11746 -- target are static. This avoids junk warnings in cases where the
11747 -- conversion is necessary to establish staticness, for example in
11748 -- a case statement.
11750 if not Is_OK_Static_Subtype (Operand_Typ)
11751 and then Is_OK_Static_Subtype (Target_Typ)
11755 -- Finally, if this type conversion occurs in a context requiring
11756 -- a prefix, and the expression is a qualified expression then the
11757 -- type conversion is not redundant, since a qualified expression
11758 -- is not a prefix, whereas a type conversion is. For example, "X
11759 -- := T'(Funx(...)).Y;" is illegal because a selected component
11760 -- requires a prefix, but a type conversion makes it legal: "X :=
11761 -- T(T'(Funx(...))).Y;"
11763 -- In Ada 2012, a qualified expression is a name, so this idiom is
11764 -- no longer needed, but we still suppress the warning because it
11765 -- seems unfriendly for warnings to pop up when you switch to the
11766 -- newer language version.
11768 elsif Nkind (Orig_N) = N_Qualified_Expression
11769 and then Nkind_In (Parent (N), N_Attribute_Reference,
11770 N_Indexed_Component,
11771 N_Selected_Component,
11773 N_Explicit_Dereference)
11777 -- Never warn on conversion to Long_Long_Integer'Base since
11778 -- that is most likely an artifact of the extended overflow
11779 -- checking and comes from complex expanded code.
11781 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
11784 -- Here we give the redundant conversion warning. If it is an
11785 -- entity, give the name of the entity in the message. If not,
11786 -- just mention the expression.
11788 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
11791 if Is_Entity_Name (Orig_N) then
11792 Error_Msg_Node_2 := Orig_T;
11793 Error_Msg_NE -- CODEFIX
11794 ("??redundant conversion, & is of type &!",
11795 N, Entity (Orig_N));
11798 ("??redundant conversion, expression is of type&!",
11805 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
11806 -- No need to perform any interface conversion if the type of the
11807 -- expression coincides with the target type.
11809 if Ada_Version >= Ada_2005
11810 and then Expander_Active
11811 and then Operand_Typ /= Target_Typ
11814 Opnd : Entity_Id := Operand_Typ;
11815 Target : Entity_Id := Target_Typ;
11818 -- If the type of the operand is a limited view, use nonlimited
11819 -- view when available. If it is a class-wide type, recover the
11820 -- class-wide type of the nonlimited view.
11822 if From_Limited_With (Opnd)
11823 and then Has_Non_Limited_View (Opnd)
11825 Opnd := Non_Limited_View (Opnd);
11826 Set_Etype (Expression (N), Opnd);
11829 -- It seems that Non_Limited_View should also be applied for
11830 -- Target when it has a limited view, but that leads to missing
11831 -- error checks on interface conversions further below. ???
11833 if Is_Access_Type (Opnd) then
11834 Opnd := Designated_Type (Opnd);
11836 -- If the type of the operand is a limited view, use nonlimited
11837 -- view when available. If it is a class-wide type, recover the
11838 -- class-wide type of the nonlimited view.
11840 if From_Limited_With (Opnd)
11841 and then Has_Non_Limited_View (Opnd)
11843 Opnd := Non_Limited_View (Opnd);
11847 if Is_Access_Type (Target_Typ) then
11848 Target := Designated_Type (Target);
11850 -- If the target type is a limited view, use nonlimited view
11853 if From_Limited_With (Target)
11854 and then Has_Non_Limited_View (Target)
11856 Target := Non_Limited_View (Target);
11860 if Opnd = Target then
11863 -- Conversion from interface type
11865 -- It seems that it would be better for the error checks below
11866 -- to be performed as part of Validate_Conversion (and maybe some
11867 -- of the error checks above could be moved as well?). ???
11869 elsif Is_Interface (Opnd) then
11871 -- Ada 2005 (AI-217): Handle entities from limited views
11873 if From_Limited_With (Opnd) then
11874 Error_Msg_Qual_Level := 99;
11875 Error_Msg_NE -- CODEFIX
11876 ("missing WITH clause on package &", N,
11877 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
11879 ("type conversions require visibility of the full view",
11882 elsif From_Limited_With (Target)
11884 (Is_Access_Type (Target_Typ)
11885 and then Present (Non_Limited_View (Etype (Target))))
11887 Error_Msg_Qual_Level := 99;
11888 Error_Msg_NE -- CODEFIX
11889 ("missing WITH clause on package &", N,
11890 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
11892 ("type conversions require visibility of the full view",
11896 Expand_Interface_Conversion (N);
11899 -- Conversion to interface type
11901 elsif Is_Interface (Target) then
11905 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
11906 Opnd := Etype (Opnd);
11909 if Is_Class_Wide_Type (Opnd)
11910 or else Interface_Present_In_Ancestor
11914 Expand_Interface_Conversion (N);
11916 Error_Msg_Name_1 := Chars (Etype (Target));
11917 Error_Msg_Name_2 := Chars (Opnd);
11919 ("wrong interface conversion (% is not a progenitor "
11926 -- Ada 2012: once the type conversion is resolved, check whether the
11927 -- operand statisfies the static predicate of the target type.
11929 if Has_Predicates (Target_Typ) then
11930 Check_Expression_Against_Static_Predicate (N, Target_Typ);
11933 -- If at this stage we have a real to integer conversion, make sure that
11934 -- the Do_Range_Check flag is set, because such conversions in general
11935 -- need a range check. We only need this if expansion is off.
11936 -- In GNATprove mode, we only do that when converting from fixed-point
11937 -- (as floating-point to integer conversions are now handled in
11938 -- GNATprove mode).
11940 if Nkind (N) = N_Type_Conversion
11941 and then not Expander_Active
11942 and then Is_Integer_Type (Target_Typ)
11943 and then (Is_Fixed_Point_Type (Operand_Typ)
11944 or else (not GNATprove_Mode
11945 and then Is_Floating_Point_Type (Operand_Typ)))
11946 and then not Range_Checks_Suppressed (Target_Typ)
11947 and then not Range_Checks_Suppressed (Operand_Typ)
11949 Set_Do_Range_Check (Operand);
11952 -- Generating C code a type conversion of an access to constrained
11953 -- array type to access to unconstrained array type involves building
11954 -- a fat pointer which in general cannot be generated on the fly. We
11955 -- remove side effects in order to store the result of the conversion
11956 -- into a temporary.
11958 if Modify_Tree_For_C
11959 and then Nkind (N) = N_Type_Conversion
11960 and then Nkind (Parent (N)) /= N_Object_Declaration
11961 and then Is_Access_Type (Etype (N))
11962 and then Is_Array_Type (Designated_Type (Etype (N)))
11963 and then not Is_Constrained (Designated_Type (Etype (N)))
11964 and then Is_Constrained (Designated_Type (Etype (Expression (N))))
11966 Remove_Side_Effects (N);
11968 end Resolve_Type_Conversion;
11970 ----------------------
11971 -- Resolve_Unary_Op --
11972 ----------------------
11974 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
11975 B_Typ : constant Entity_Id := Base_Type (Typ);
11976 R : constant Node_Id := Right_Opnd (N);
11982 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
11983 Error_Msg_Name_1 := Chars (Typ);
11984 Check_SPARK_05_Restriction
11985 ("unary operator not defined for modular type%", N);
11988 -- Deal with intrinsic unary operators
11990 if Comes_From_Source (N)
11991 and then Ekind (Entity (N)) = E_Function
11992 and then Is_Imported (Entity (N))
11993 and then Is_Intrinsic_Subprogram (Entity (N))
11995 Resolve_Intrinsic_Unary_Operator (N, Typ);
11999 -- Deal with universal cases
12001 if Etype (R) = Universal_Integer
12003 Etype (R) = Universal_Real
12005 Check_For_Visible_Operator (N, B_Typ);
12008 Set_Etype (N, B_Typ);
12009 Resolve (R, B_Typ);
12011 -- Generate warning for expressions like abs (x mod 2)
12013 if Warn_On_Redundant_Constructs
12014 and then Nkind (N) = N_Op_Abs
12016 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12018 if OK and then Hi >= Lo and then Lo >= 0 then
12019 Error_Msg_N -- CODEFIX
12020 ("?r?abs applied to known non-negative value has no effect", N);
12024 -- Deal with reference generation
12026 Check_Unset_Reference (R);
12027 Generate_Operator_Reference (N, B_Typ);
12028 Analyze_Dimension (N);
12031 -- Set overflow checking bit. Much cleverer code needed here eventually
12032 -- and perhaps the Resolve routines should be separated for the various
12033 -- arithmetic operations, since they will need different processing ???
12035 if Nkind (N) in N_Op then
12036 if not Overflow_Checks_Suppressed (Etype (N)) then
12037 Enable_Overflow_Check (N);
12041 -- Generate warning for expressions like -5 mod 3 for integers. No need
12042 -- to worry in the floating-point case, since parens do not affect the
12043 -- result so there is no point in giving in a warning.
12046 Norig : constant Node_Id := Original_Node (N);
12055 if Warn_On_Questionable_Missing_Parens
12056 and then Comes_From_Source (Norig)
12057 and then Is_Integer_Type (Typ)
12058 and then Nkind (Norig) = N_Op_Minus
12060 Rorig := Original_Node (Right_Opnd (Norig));
12062 -- We are looking for cases where the right operand is not
12063 -- parenthesized, and is a binary operator, multiply, divide, or
12064 -- mod. These are the cases where the grouping can affect results.
12066 if Paren_Count (Rorig) = 0
12067 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
12069 -- For mod, we always give the warning, since the value is
12070 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12071 -- -(5 mod 315)). But for the other cases, the only concern is
12072 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12073 -- overflows, but (-2) * 64 does not). So we try to give the
12074 -- message only when overflow is possible.
12076 if Nkind (Rorig) /= N_Op_Mod
12077 and then Compile_Time_Known_Value (R)
12079 Val := Expr_Value (R);
12081 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12082 HB := Expr_Value (Type_High_Bound (Typ));
12084 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12087 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12088 LB := Expr_Value (Type_Low_Bound (Typ));
12090 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12093 -- Note that the test below is deliberately excluding the
12094 -- largest negative number, since that is a potentially
12095 -- troublesome case (e.g. -2 * x, where the result is the
12096 -- largest negative integer has an overflow with 2 * x).
12098 if Val > LB and then Val <= HB then
12103 -- For the multiplication case, the only case we have to worry
12104 -- about is when (-a)*b is exactly the largest negative number
12105 -- so that -(a*b) can cause overflow. This can only happen if
12106 -- a is a power of 2, and more generally if any operand is a
12107 -- constant that is not a power of 2, then the parentheses
12108 -- cannot affect whether overflow occurs. We only bother to
12109 -- test the left most operand
12111 -- Loop looking at left operands for one that has known value
12114 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12115 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12116 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12118 -- Operand value of 0 or 1 skips warning
12123 -- Otherwise check power of 2, if power of 2, warn, if
12124 -- anything else, skip warning.
12127 while Lval /= 2 loop
12128 if Lval mod 2 = 1 then
12139 -- Keep looking at left operands
12141 Opnd := Left_Opnd (Opnd);
12142 end loop Opnd_Loop;
12144 -- For rem or "/" we can only have a problematic situation
12145 -- if the divisor has a value of minus one or one. Otherwise
12146 -- overflow is impossible (divisor > 1) or we have a case of
12147 -- division by zero in any case.
12149 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
12150 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12151 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12156 -- If we fall through warning should be issued
12158 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12161 ("??unary minus expression should be parenthesized here!", N);
12165 end Resolve_Unary_Op;
12167 ----------------------------------
12168 -- Resolve_Unchecked_Expression --
12169 ----------------------------------
12171 procedure Resolve_Unchecked_Expression
12176 Resolve (Expression (N), Typ, Suppress => All_Checks);
12177 Set_Etype (N, Typ);
12178 end Resolve_Unchecked_Expression;
12180 ---------------------------------------
12181 -- Resolve_Unchecked_Type_Conversion --
12182 ---------------------------------------
12184 procedure Resolve_Unchecked_Type_Conversion
12188 pragma Warnings (Off, Typ);
12190 Operand : constant Node_Id := Expression (N);
12191 Opnd_Type : constant Entity_Id := Etype (Operand);
12194 -- Resolve operand using its own type
12196 Resolve (Operand, Opnd_Type);
12198 -- In an inlined context, the unchecked conversion may be applied
12199 -- to a literal, in which case its type is the type of the context.
12200 -- (In other contexts conversions cannot apply to literals).
12203 and then (Opnd_Type = Any_Character or else
12204 Opnd_Type = Any_Integer or else
12205 Opnd_Type = Any_Real)
12207 Set_Etype (Operand, Typ);
12210 Analyze_Dimension (N);
12211 Eval_Unchecked_Conversion (N);
12212 end Resolve_Unchecked_Type_Conversion;
12214 ------------------------------
12215 -- Rewrite_Operator_As_Call --
12216 ------------------------------
12218 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12219 Loc : constant Source_Ptr := Sloc (N);
12220 Actuals : constant List_Id := New_List;
12224 if Nkind (N) in N_Binary_Op then
12225 Append (Left_Opnd (N), Actuals);
12228 Append (Right_Opnd (N), Actuals);
12231 Make_Function_Call (Sloc => Loc,
12232 Name => New_Occurrence_Of (Nam, Loc),
12233 Parameter_Associations => Actuals);
12235 Preserve_Comes_From_Source (New_N, N);
12236 Preserve_Comes_From_Source (Name (New_N), N);
12237 Rewrite (N, New_N);
12238 Set_Etype (N, Etype (Nam));
12239 end Rewrite_Operator_As_Call;
12241 ------------------------------
12242 -- Rewrite_Renamed_Operator --
12243 ------------------------------
12245 procedure Rewrite_Renamed_Operator
12250 Nam : constant Name_Id := Chars (Op);
12251 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12255 -- Do not perform this transformation within a pre/postcondition,
12256 -- because the expression will be reanalyzed, and the transformation
12257 -- might affect the visibility of the operator, e.g. in an instance.
12258 -- Note that fully analyzed and expanded pre/postconditions appear as
12259 -- pragma Check equivalents.
12261 if In_Pre_Post_Condition (N) then
12265 -- Likewise when an expression function is being preanalyzed, since the
12266 -- expression will be reanalyzed as part of the generated body.
12268 if In_Spec_Expression then
12270 S : constant Entity_Id := Current_Scope_No_Loops;
12272 if Ekind (S) = E_Function
12273 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
12274 N_Expression_Function
12281 -- Rewrite the operator node using the real operator, not its renaming.
12282 -- Exclude user-defined intrinsic operations of the same name, which are
12283 -- treated separately and rewritten as calls.
12285 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
12286 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
12287 Set_Chars (Op_Node, Nam);
12288 Set_Etype (Op_Node, Etype (N));
12289 Set_Entity (Op_Node, Op);
12290 Set_Right_Opnd (Op_Node, Right_Opnd (N));
12292 -- Indicate that both the original entity and its renaming are
12293 -- referenced at this point.
12295 Generate_Reference (Entity (N), N);
12296 Generate_Reference (Op, N);
12299 Set_Left_Opnd (Op_Node, Left_Opnd (N));
12302 Rewrite (N, Op_Node);
12304 -- If the context type is private, add the appropriate conversions so
12305 -- that the operator is applied to the full view. This is done in the
12306 -- routines that resolve intrinsic operators.
12308 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
12318 Resolve_Intrinsic_Operator (N, Typ);
12324 Resolve_Intrinsic_Unary_Operator (N, Typ);
12331 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
12333 -- Operator renames a user-defined operator of the same name. Use the
12334 -- original operator in the node, which is the one Gigi knows about.
12336 Set_Entity (N, Op);
12337 Set_Is_Overloaded (N, False);
12339 end Rewrite_Renamed_Operator;
12341 -----------------------
12342 -- Set_Slice_Subtype --
12343 -----------------------
12345 -- Build an implicit subtype declaration to represent the type delivered by
12346 -- the slice. This is an abbreviated version of an array subtype. We define
12347 -- an index subtype for the slice, using either the subtype name or the
12348 -- discrete range of the slice. To be consistent with index usage elsewhere
12349 -- we create a list header to hold the single index. This list is not
12350 -- otherwise attached to the syntax tree.
12352 procedure Set_Slice_Subtype (N : Node_Id) is
12353 Loc : constant Source_Ptr := Sloc (N);
12354 Index_List : constant List_Id := New_List;
12356 Index_Subtype : Entity_Id;
12357 Index_Type : Entity_Id;
12358 Slice_Subtype : Entity_Id;
12359 Drange : constant Node_Id := Discrete_Range (N);
12362 Index_Type := Base_Type (Etype (Drange));
12364 if Is_Entity_Name (Drange) then
12365 Index_Subtype := Entity (Drange);
12368 -- We force the evaluation of a range. This is definitely needed in
12369 -- the renamed case, and seems safer to do unconditionally. Note in
12370 -- any case that since we will create and insert an Itype referring
12371 -- to this range, we must make sure any side effect removal actions
12372 -- are inserted before the Itype definition.
12374 if Nkind (Drange) = N_Range then
12375 Force_Evaluation (Low_Bound (Drange));
12376 Force_Evaluation (High_Bound (Drange));
12378 -- If the discrete range is given by a subtype indication, the
12379 -- type of the slice is the base of the subtype mark.
12381 elsif Nkind (Drange) = N_Subtype_Indication then
12383 R : constant Node_Id := Range_Expression (Constraint (Drange));
12385 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
12386 Force_Evaluation (Low_Bound (R));
12387 Force_Evaluation (High_Bound (R));
12391 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12393 -- Take a new copy of Drange (where bounds have been rewritten to
12394 -- reference side-effect-free names). Using a separate tree ensures
12395 -- that further expansion (e.g. while rewriting a slice assignment
12396 -- into a FOR loop) does not attempt to remove side effects on the
12397 -- bounds again (which would cause the bounds in the index subtype
12398 -- definition to refer to temporaries before they are defined) (the
12399 -- reason is that some names are considered side effect free here
12400 -- for the subtype, but not in the context of a loop iteration
12403 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
12404 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
12405 Set_Etype (Index_Subtype, Index_Type);
12406 Set_Size_Info (Index_Subtype, Index_Type);
12407 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12408 Set_Is_Constrained (Index_Subtype);
12411 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
12413 Index := New_Occurrence_Of (Index_Subtype, Loc);
12414 Set_Etype (Index, Index_Subtype);
12415 Append (Index, Index_List);
12417 Set_First_Index (Slice_Subtype, Index);
12418 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
12419 Set_Is_Constrained (Slice_Subtype, True);
12421 Check_Compile_Time_Size (Slice_Subtype);
12423 -- The Etype of the existing Slice node is reset to this slice subtype.
12424 -- Its bounds are obtained from its first index.
12426 Set_Etype (N, Slice_Subtype);
12428 -- For bit-packed slice subtypes, freeze immediately (except in the case
12429 -- of being in a "spec expression" where we never freeze when we first
12430 -- see the expression).
12432 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
12433 Freeze_Itype (Slice_Subtype, N);
12435 -- For all other cases insert an itype reference in the slice's actions
12436 -- so that the itype is frozen at the proper place in the tree (i.e. at
12437 -- the point where actions for the slice are analyzed). Note that this
12438 -- is different from freezing the itype immediately, which might be
12439 -- premature (e.g. if the slice is within a transient scope). This needs
12440 -- to be done only if expansion is enabled.
12442 elsif Expander_Active then
12443 Ensure_Defined (Typ => Slice_Subtype, N => N);
12445 end Set_Slice_Subtype;
12447 --------------------------------
12448 -- Set_String_Literal_Subtype --
12449 --------------------------------
12451 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
12452 Loc : constant Source_Ptr := Sloc (N);
12453 Low_Bound : constant Node_Id :=
12454 Type_Low_Bound (Etype (First_Index (Typ)));
12455 Subtype_Id : Entity_Id;
12458 if Nkind (N) /= N_String_Literal then
12462 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
12463 Set_String_Literal_Length (Subtype_Id, UI_From_Int
12464 (String_Length (Strval (N))));
12465 Set_Etype (Subtype_Id, Base_Type (Typ));
12466 Set_Is_Constrained (Subtype_Id);
12467 Set_Etype (N, Subtype_Id);
12469 -- The low bound is set from the low bound of the corresponding index
12470 -- type. Note that we do not store the high bound in the string literal
12471 -- subtype, but it can be deduced if necessary from the length and the
12474 if Is_OK_Static_Expression (Low_Bound) then
12475 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
12477 -- If the lower bound is not static we create a range for the string
12478 -- literal, using the index type and the known length of the literal.
12479 -- If the length is 1, then the upper bound is set to a mere copy of
12480 -- the lower bound; or else, if the index type is a signed integer,
12481 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
12482 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
12486 Length : constant Nat := String_Length (Strval (N));
12487 Index_List : constant List_Id := New_List;
12488 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
12489 Array_Subtype : Entity_Id;
12491 High_Bound : Node_Id;
12493 Index_Subtype : Entity_Id;
12497 High_Bound := New_Copy_Tree (Low_Bound);
12499 elsif Is_Signed_Integer_Type (Index_Type) then
12502 Left_Opnd => New_Copy_Tree (Low_Bound),
12503 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
12507 Make_Attribute_Reference (Loc,
12508 Attribute_Name => Name_Val,
12510 New_Occurrence_Of (Index_Type, Loc),
12511 Expressions => New_List (
12514 Make_Attribute_Reference (Loc,
12515 Attribute_Name => Name_Pos,
12517 New_Occurrence_Of (Index_Type, Loc),
12519 New_List (New_Copy_Tree (Low_Bound))),
12521 Make_Integer_Literal (Loc, Length - 1))));
12524 if Is_Integer_Type (Index_Type) then
12525 Set_String_Literal_Low_Bound
12526 (Subtype_Id, Make_Integer_Literal (Loc, 1));
12529 -- If the index type is an enumeration type, build bounds
12530 -- expression with attributes.
12532 Set_String_Literal_Low_Bound
12534 Make_Attribute_Reference (Loc,
12535 Attribute_Name => Name_First,
12537 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
12540 Analyze_And_Resolve
12541 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
12543 -- Build bona fide subtype for the string, and wrap it in an
12544 -- unchecked conversion, because the back end expects the
12545 -- String_Literal_Subtype to have a static lower bound.
12548 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12549 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
12550 Set_Scalar_Range (Index_Subtype, Drange);
12551 Set_Parent (Drange, N);
12552 Analyze_And_Resolve (Drange, Index_Type);
12554 -- In this context, the Index_Type may already have a constraint,
12555 -- so use common base type on string subtype. The base type may
12556 -- be used when generating attributes of the string, for example
12557 -- in the context of a slice assignment.
12559 Set_Etype (Index_Subtype, Base_Type (Index_Type));
12560 Set_Size_Info (Index_Subtype, Index_Type);
12561 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12563 Array_Subtype := Create_Itype (E_Array_Subtype, N);
12565 Index := New_Occurrence_Of (Index_Subtype, Loc);
12566 Set_Etype (Index, Index_Subtype);
12567 Append (Index, Index_List);
12569 Set_First_Index (Array_Subtype, Index);
12570 Set_Etype (Array_Subtype, Base_Type (Typ));
12571 Set_Is_Constrained (Array_Subtype, True);
12574 Make_Unchecked_Type_Conversion (Loc,
12575 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
12576 Expression => Relocate_Node (N)));
12577 Set_Etype (N, Array_Subtype);
12580 end Set_String_Literal_Subtype;
12582 ------------------------------
12583 -- Simplify_Type_Conversion --
12584 ------------------------------
12586 procedure Simplify_Type_Conversion (N : Node_Id) is
12588 if Nkind (N) = N_Type_Conversion then
12590 Operand : constant Node_Id := Expression (N);
12591 Target_Typ : constant Entity_Id := Etype (N);
12592 Opnd_Typ : constant Entity_Id := Etype (Operand);
12595 -- Special processing if the conversion is the expression of a
12596 -- Rounding or Truncation attribute reference. In this case we
12599 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
12605 -- with the Float_Truncate flag set to False or True respectively,
12606 -- which is more efficient. We reuse Rounding for Machine_Rounding
12607 -- as System.Fat_Gen, which is a permissible behavior.
12609 if Is_Floating_Point_Type (Opnd_Typ)
12611 (Is_Integer_Type (Target_Typ)
12612 or else (Is_Fixed_Point_Type (Target_Typ)
12613 and then Conversion_OK (N)))
12614 and then Nkind (Operand) = N_Attribute_Reference
12615 and then Nam_In (Attribute_Name (Operand), Name_Rounding,
12616 Name_Machine_Rounding,
12620 Truncate : constant Boolean :=
12621 Attribute_Name (Operand) = Name_Truncation;
12624 Relocate_Node (First (Expressions (Operand))));
12625 Set_Float_Truncate (N, Truncate);
12628 -- Special processing for the conversion of an integer literal to
12629 -- a dynamic type: we first convert the literal to the root type
12630 -- and then convert the result to the target type, the goal being
12631 -- to avoid doing range checks in universal integer.
12633 elsif Is_Integer_Type (Target_Typ)
12634 and then not Is_Generic_Type (Root_Type (Target_Typ))
12635 and then Nkind (Operand) = N_Integer_Literal
12636 and then Opnd_Typ = Universal_Integer
12638 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
12639 Analyze_And_Resolve (Operand);
12641 -- If the expression is a conversion to universal integer of an
12642 -- an expression with an integer type, then we can eliminate the
12643 -- intermediate conversion to universal integer.
12645 elsif Nkind (Operand) = N_Type_Conversion
12646 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12647 and then Is_Integer_Type (Etype (Expression (Operand)))
12649 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12650 Analyze_And_Resolve (Operand);
12654 end Simplify_Type_Conversion;
12656 -----------------------------
12657 -- Unique_Fixed_Point_Type --
12658 -----------------------------
12660 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
12661 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
12662 -- Give error messages for true ambiguity. Messages are posted on node
12663 -- N, and entities T1, T2 are the possible interpretations.
12665 -----------------------
12666 -- Fixed_Point_Error --
12667 -----------------------
12669 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
12671 Error_Msg_N ("ambiguous universal_fixed_expression", N);
12672 Error_Msg_NE ("\\possible interpretation as}", N, T1);
12673 Error_Msg_NE ("\\possible interpretation as}", N, T2);
12674 end Fixed_Point_Error;
12684 -- Start of processing for Unique_Fixed_Point_Type
12687 -- The operations on Duration are visible, so Duration is always a
12688 -- possible interpretation.
12690 T1 := Standard_Duration;
12692 -- Look for fixed-point types in enclosing scopes
12694 Scop := Current_Scope;
12695 while Scop /= Standard_Standard loop
12696 T2 := First_Entity (Scop);
12697 while Present (T2) loop
12698 if Is_Fixed_Point_Type (T2)
12699 and then Current_Entity (T2) = T2
12700 and then Scope (Base_Type (T2)) = Scop
12702 if Present (T1) then
12703 Fixed_Point_Error (T1, T2);
12713 Scop := Scope (Scop);
12716 -- Look for visible fixed type declarations in the context
12718 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
12719 while Present (Item) loop
12720 if Nkind (Item) = N_With_Clause then
12721 Scop := Entity (Name (Item));
12722 T2 := First_Entity (Scop);
12723 while Present (T2) loop
12724 if Is_Fixed_Point_Type (T2)
12725 and then Scope (Base_Type (T2)) = Scop
12726 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
12728 if Present (T1) then
12729 Fixed_Point_Error (T1, T2);
12743 if Nkind (N) = N_Real_Literal then
12744 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
12747 -- When the context is a type conversion, issue the warning on the
12748 -- expression of the conversion because it is the actual operation.
12750 if Nkind_In (N, N_Type_Conversion, N_Unchecked_Type_Conversion) then
12751 ErrN := Expression (N);
12757 ("??universal_fixed expression interpreted as }!", ErrN, T1);
12761 end Unique_Fixed_Point_Type;
12763 ----------------------
12764 -- Valid_Conversion --
12765 ----------------------
12767 function Valid_Conversion
12769 Target : Entity_Id;
12771 Report_Errs : Boolean := True) return Boolean
12773 Target_Type : constant Entity_Id := Base_Type (Target);
12774 Opnd_Type : Entity_Id := Etype (Operand);
12775 Inc_Ancestor : Entity_Id;
12777 function Conversion_Check
12779 Msg : String) return Boolean;
12780 -- Little routine to post Msg if Valid is False, returns Valid value
12782 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
12783 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
12785 procedure Conversion_Error_NE
12787 N : Node_Or_Entity_Id;
12788 E : Node_Or_Entity_Id);
12789 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
12791 function In_Instance_Code return Boolean;
12792 -- Return True if expression is within an instance but is not in one of
12793 -- the actuals of the instantiation. Type conversions within an instance
12794 -- are not rechecked because type visbility may lead to spurious errors,
12795 -- but conversions in an actual for a formal object must be checked.
12797 function Valid_Tagged_Conversion
12798 (Target_Type : Entity_Id;
12799 Opnd_Type : Entity_Id) return Boolean;
12800 -- Specifically test for validity of tagged conversions
12802 function Valid_Array_Conversion return Boolean;
12803 -- Check index and component conformance, and accessibility levels if
12804 -- the component types are anonymous access types (Ada 2005).
12806 ----------------------
12807 -- Conversion_Check --
12808 ----------------------
12810 function Conversion_Check
12812 Msg : String) return Boolean
12817 -- A generic unit has already been analyzed and we have verified
12818 -- that a particular conversion is OK in that context. Since the
12819 -- instance is reanalyzed without relying on the relationships
12820 -- established during the analysis of the generic, it is possible
12821 -- to end up with inconsistent views of private types. Do not emit
12822 -- the error message in such cases. The rest of the machinery in
12823 -- Valid_Conversion still ensures the proper compatibility of
12824 -- target and operand types.
12826 and then not In_Instance_Code
12828 Conversion_Error_N (Msg, Operand);
12832 end Conversion_Check;
12834 ------------------------
12835 -- Conversion_Error_N --
12836 ------------------------
12838 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
12840 if Report_Errs then
12841 Error_Msg_N (Msg, N);
12843 end Conversion_Error_N;
12845 -------------------------
12846 -- Conversion_Error_NE --
12847 -------------------------
12849 procedure Conversion_Error_NE
12851 N : Node_Or_Entity_Id;
12852 E : Node_Or_Entity_Id)
12855 if Report_Errs then
12856 Error_Msg_NE (Msg, N, E);
12858 end Conversion_Error_NE;
12860 ----------------------
12861 -- In_Instance_Code --
12862 ----------------------
12864 function In_Instance_Code return Boolean is
12868 if not In_Instance then
12873 while Present (Par) loop
12875 -- The expression is part of an actual object if it appears in
12876 -- the generated object declaration in the instance.
12878 if Nkind (Par) = N_Object_Declaration
12879 and then Present (Corresponding_Generic_Association (Par))
12885 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
12886 or else Nkind (Par) in N_Subprogram_Call
12887 or else Nkind (Par) in N_Declaration;
12890 Par := Parent (Par);
12893 -- Otherwise the expression appears within the instantiated unit
12897 end In_Instance_Code;
12899 ----------------------------
12900 -- Valid_Array_Conversion --
12901 ----------------------------
12903 function Valid_Array_Conversion return Boolean is
12904 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
12905 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
12907 Opnd_Index : Node_Id;
12908 Opnd_Index_Type : Entity_Id;
12910 Target_Comp_Type : constant Entity_Id :=
12911 Component_Type (Target_Type);
12912 Target_Comp_Base : constant Entity_Id :=
12913 Base_Type (Target_Comp_Type);
12915 Target_Index : Node_Id;
12916 Target_Index_Type : Entity_Id;
12919 -- Error if wrong number of dimensions
12922 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
12925 ("incompatible number of dimensions for conversion", Operand);
12928 -- Number of dimensions matches
12931 -- Loop through indexes of the two arrays
12933 Target_Index := First_Index (Target_Type);
12934 Opnd_Index := First_Index (Opnd_Type);
12935 while Present (Target_Index) and then Present (Opnd_Index) loop
12936 Target_Index_Type := Etype (Target_Index);
12937 Opnd_Index_Type := Etype (Opnd_Index);
12939 -- Error if index types are incompatible
12941 if not (Is_Integer_Type (Target_Index_Type)
12942 and then Is_Integer_Type (Opnd_Index_Type))
12943 and then (Root_Type (Target_Index_Type)
12944 /= Root_Type (Opnd_Index_Type))
12947 ("incompatible index types for array conversion",
12952 Next_Index (Target_Index);
12953 Next_Index (Opnd_Index);
12956 -- If component types have same base type, all set
12958 if Target_Comp_Base = Opnd_Comp_Base then
12961 -- Here if base types of components are not the same. The only
12962 -- time this is allowed is if we have anonymous access types.
12964 -- The conversion of arrays of anonymous access types can lead
12965 -- to dangling pointers. AI-392 formalizes the accessibility
12966 -- checks that must be applied to such conversions to prevent
12967 -- out-of-scope references.
12970 (Target_Comp_Base, E_Anonymous_Access_Type,
12971 E_Anonymous_Access_Subprogram_Type)
12972 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
12974 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
12976 if Type_Access_Level (Target_Type) <
12977 Deepest_Type_Access_Level (Opnd_Type)
12979 if In_Instance_Body then
12980 Error_Msg_Warn := SPARK_Mode /= On;
12982 ("source array type has deeper accessibility "
12983 & "level than target<<", Operand);
12984 Conversion_Error_N ("\Program_Error [<<", Operand);
12986 Make_Raise_Program_Error (Sloc (N),
12987 Reason => PE_Accessibility_Check_Failed));
12988 Set_Etype (N, Target_Type);
12991 -- Conversion not allowed because of accessibility levels
12995 ("source array type has deeper accessibility "
12996 & "level than target", Operand);
13004 -- All other cases where component base types do not match
13008 ("incompatible component types for array conversion",
13013 -- Check that component subtypes statically match. For numeric
13014 -- types this means that both must be either constrained or
13015 -- unconstrained. For enumeration types the bounds must match.
13016 -- All of this is checked in Subtypes_Statically_Match.
13018 if not Subtypes_Statically_Match
13019 (Target_Comp_Type, Opnd_Comp_Type)
13022 ("component subtypes must statically match", Operand);
13028 end Valid_Array_Conversion;
13030 -----------------------------
13031 -- Valid_Tagged_Conversion --
13032 -----------------------------
13034 function Valid_Tagged_Conversion
13035 (Target_Type : Entity_Id;
13036 Opnd_Type : Entity_Id) return Boolean
13039 -- Upward conversions are allowed (RM 4.6(22))
13041 if Covers (Target_Type, Opnd_Type)
13042 or else Is_Ancestor (Target_Type, Opnd_Type)
13046 -- Downward conversion are allowed if the operand is class-wide
13049 elsif Is_Class_Wide_Type (Opnd_Type)
13050 and then Covers (Opnd_Type, Target_Type)
13054 elsif Covers (Opnd_Type, Target_Type)
13055 or else Is_Ancestor (Opnd_Type, Target_Type)
13058 Conversion_Check (False,
13059 "downward conversion of tagged objects not allowed");
13061 -- Ada 2005 (AI-251): The conversion to/from interface types is
13062 -- always valid. The types involved may be class-wide (sub)types.
13064 elsif Is_Interface (Etype (Base_Type (Target_Type)))
13065 or else Is_Interface (Etype (Base_Type (Opnd_Type)))
13069 -- If the operand is a class-wide type obtained through a limited_
13070 -- with clause, and the context includes the nonlimited view, use
13071 -- it to determine whether the conversion is legal.
13073 elsif Is_Class_Wide_Type (Opnd_Type)
13074 and then From_Limited_With (Opnd_Type)
13075 and then Present (Non_Limited_View (Etype (Opnd_Type)))
13076 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
13080 elsif Is_Access_Type (Opnd_Type)
13081 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
13086 Conversion_Error_NE
13087 ("invalid tagged conversion, not compatible with}",
13088 N, First_Subtype (Opnd_Type));
13091 end Valid_Tagged_Conversion;
13093 -- Start of processing for Valid_Conversion
13096 Check_Parameterless_Call (Operand);
13098 if Is_Overloaded (Operand) then
13108 -- Remove procedure calls, which syntactically cannot appear in
13109 -- this context, but which cannot be removed by type checking,
13110 -- because the context does not impose a type.
13112 -- The node may be labelled overloaded, but still contain only one
13113 -- interpretation because others were discarded earlier. If this
13114 -- is the case, retain the single interpretation if legal.
13116 Get_First_Interp (Operand, I, It);
13117 Opnd_Type := It.Typ;
13118 Get_Next_Interp (I, It);
13120 if Present (It.Typ)
13121 and then Opnd_Type /= Standard_Void_Type
13123 -- More than one candidate interpretation is available
13125 Get_First_Interp (Operand, I, It);
13126 while Present (It.Typ) loop
13127 if It.Typ = Standard_Void_Type then
13131 -- When compiling for a system where Address is of a visible
13132 -- integer type, spurious ambiguities can be produced when
13133 -- arithmetic operations have a literal operand and return
13134 -- System.Address or a descendant of it. These ambiguities
13135 -- are usually resolved by the context, but for conversions
13136 -- there is no context type and the removal of the spurious
13137 -- operations must be done explicitly here.
13139 if not Address_Is_Private
13140 and then Is_Descendant_Of_Address (It.Typ)
13145 Get_Next_Interp (I, It);
13149 Get_First_Interp (Operand, I, It);
13153 if No (It.Typ) then
13154 Conversion_Error_N ("illegal operand in conversion", Operand);
13158 Get_Next_Interp (I, It);
13160 if Present (It.Typ) then
13163 It1 := Disambiguate (Operand, I1, I, Any_Type);
13165 if It1 = No_Interp then
13167 ("ambiguous operand in conversion", Operand);
13169 -- If the interpretation involves a standard operator, use
13170 -- the location of the type, which may be user-defined.
13172 if Sloc (It.Nam) = Standard_Location then
13173 Error_Msg_Sloc := Sloc (It.Typ);
13175 Error_Msg_Sloc := Sloc (It.Nam);
13178 Conversion_Error_N -- CODEFIX
13179 ("\\possible interpretation#!", Operand);
13181 if Sloc (N1) = Standard_Location then
13182 Error_Msg_Sloc := Sloc (T1);
13184 Error_Msg_Sloc := Sloc (N1);
13187 Conversion_Error_N -- CODEFIX
13188 ("\\possible interpretation#!", Operand);
13194 Set_Etype (Operand, It1.Typ);
13195 Opnd_Type := It1.Typ;
13199 -- Deal with conversion of integer type to address if the pragma
13200 -- Allow_Integer_Address is in effect. We convert the conversion to
13201 -- an unchecked conversion in this case and we are all done.
13203 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
13204 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
13205 Analyze_And_Resolve (N, Target_Type);
13209 -- If we are within a child unit, check whether the type of the
13210 -- expression has an ancestor in a parent unit, in which case it
13211 -- belongs to its derivation class even if the ancestor is private.
13212 -- See RM 7.3.1 (5.2/3).
13214 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
13218 if Is_Numeric_Type (Target_Type) then
13220 -- A universal fixed expression can be converted to any numeric type
13222 if Opnd_Type = Universal_Fixed then
13225 -- Also no need to check when in an instance or inlined body, because
13226 -- the legality has been established when the template was analyzed.
13227 -- Furthermore, numeric conversions may occur where only a private
13228 -- view of the operand type is visible at the instantiation point.
13229 -- This results in a spurious error if we check that the operand type
13230 -- is a numeric type.
13232 -- Note: in a previous version of this unit, the following tests were
13233 -- applied only for generated code (Comes_From_Source set to False),
13234 -- but in fact the test is required for source code as well, since
13235 -- this situation can arise in source code.
13237 elsif In_Instance_Code or else In_Inlined_Body then
13240 -- Otherwise we need the conversion check
13243 return Conversion_Check
13244 (Is_Numeric_Type (Opnd_Type)
13246 (Present (Inc_Ancestor)
13247 and then Is_Numeric_Type (Inc_Ancestor)),
13248 "illegal operand for numeric conversion");
13253 elsif Is_Array_Type (Target_Type) then
13254 if not Is_Array_Type (Opnd_Type)
13255 or else Opnd_Type = Any_Composite
13256 or else Opnd_Type = Any_String
13259 ("illegal operand for array conversion", Operand);
13263 return Valid_Array_Conversion;
13266 -- Ada 2005 (AI-251): Internally generated conversions of access to
13267 -- interface types added to force the displacement of the pointer to
13268 -- reference the corresponding dispatch table.
13270 elsif not Comes_From_Source (N)
13271 and then Is_Access_Type (Target_Type)
13272 and then Is_Interface (Designated_Type (Target_Type))
13276 -- Ada 2005 (AI-251): Anonymous access types where target references an
13279 elsif Is_Access_Type (Opnd_Type)
13280 and then Ekind_In (Target_Type, E_General_Access_Type,
13281 E_Anonymous_Access_Type)
13282 and then Is_Interface (Directly_Designated_Type (Target_Type))
13284 -- Check the static accessibility rule of 4.6(17). Note that the
13285 -- check is not enforced when within an instance body, since the
13286 -- RM requires such cases to be caught at run time.
13288 -- If the operand is a rewriting of an allocator no check is needed
13289 -- because there are no accessibility issues.
13291 if Nkind (Original_Node (N)) = N_Allocator then
13294 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
13295 if Type_Access_Level (Opnd_Type) >
13296 Deepest_Type_Access_Level (Target_Type)
13298 -- In an instance, this is a run-time check, but one we know
13299 -- will fail, so generate an appropriate warning. The raise
13300 -- will be generated by Expand_N_Type_Conversion.
13302 if In_Instance_Body then
13303 Error_Msg_Warn := SPARK_Mode /= On;
13305 ("cannot convert local pointer to non-local access type<<",
13307 Conversion_Error_N ("\Program_Error [<<", Operand);
13311 ("cannot convert local pointer to non-local access type",
13316 -- Special accessibility checks are needed in the case of access
13317 -- discriminants declared for a limited type.
13319 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
13320 and then not Is_Local_Anonymous_Access (Opnd_Type)
13322 -- When the operand is a selected access discriminant the check
13323 -- needs to be made against the level of the object denoted by
13324 -- the prefix of the selected name (Object_Access_Level handles
13325 -- checking the prefix of the operand for this case).
13327 if Nkind (Operand) = N_Selected_Component
13328 and then Object_Access_Level (Operand) >
13329 Deepest_Type_Access_Level (Target_Type)
13331 -- In an instance, this is a run-time check, but one we know
13332 -- will fail, so generate an appropriate warning. The raise
13333 -- will be generated by Expand_N_Type_Conversion.
13335 if In_Instance_Body then
13336 Error_Msg_Warn := SPARK_Mode /= On;
13338 ("cannot convert access discriminant to non-local "
13339 & "access type<<", Operand);
13340 Conversion_Error_N ("\Program_Error [<<", Operand);
13342 -- Real error if not in instance body
13346 ("cannot convert access discriminant to non-local "
13347 & "access type", Operand);
13352 -- The case of a reference to an access discriminant from
13353 -- within a limited type declaration (which will appear as
13354 -- a discriminal) is always illegal because the level of the
13355 -- discriminant is considered to be deeper than any (nameable)
13358 if Is_Entity_Name (Operand)
13359 and then not Is_Local_Anonymous_Access (Opnd_Type)
13361 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
13362 and then Present (Discriminal_Link (Entity (Operand)))
13365 ("discriminant has deeper accessibility level than target",
13374 -- General and anonymous access types
13376 elsif Ekind_In (Target_Type, E_General_Access_Type,
13377 E_Anonymous_Access_Type)
13380 (Is_Access_Type (Opnd_Type)
13382 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
13383 E_Access_Protected_Subprogram_Type),
13384 "must be an access-to-object type")
13386 if Is_Access_Constant (Opnd_Type)
13387 and then not Is_Access_Constant (Target_Type)
13390 ("access-to-constant operand type not allowed", Operand);
13394 -- Check the static accessibility rule of 4.6(17). Note that the
13395 -- check is not enforced when within an instance body, since the RM
13396 -- requires such cases to be caught at run time.
13398 if Ekind (Target_Type) /= E_Anonymous_Access_Type
13399 or else Is_Local_Anonymous_Access (Target_Type)
13400 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
13401 N_Object_Declaration
13403 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
13404 -- conversions from an anonymous access type to a named general
13405 -- access type. Such conversions are not allowed in the case of
13406 -- access parameters and stand-alone objects of an anonymous
13407 -- access type. The implicit conversion case is recognized by
13408 -- testing that Comes_From_Source is False and that it's been
13409 -- rewritten. The Comes_From_Source test isn't sufficient because
13410 -- nodes in inlined calls to predefined library routines can have
13411 -- Comes_From_Source set to False. (Is there a better way to test
13412 -- for implicit conversions???)
13414 if Ada_Version >= Ada_2012
13415 and then not Comes_From_Source (N)
13416 and then Is_Rewrite_Substitution (N)
13417 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
13418 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
13420 if Is_Itype (Opnd_Type) then
13422 -- Implicit conversions aren't allowed for objects of an
13423 -- anonymous access type, since such objects have nonstatic
13424 -- levels in Ada 2012.
13426 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
13427 N_Object_Declaration
13430 ("implicit conversion of stand-alone anonymous "
13431 & "access object not allowed", Operand);
13434 -- Implicit conversions aren't allowed for anonymous access
13435 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
13436 -- is done to exclude anonymous access results.
13438 elsif not Is_Local_Anonymous_Access (Opnd_Type)
13439 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
13440 N_Function_Specification,
13441 N_Procedure_Specification)
13444 ("implicit conversion of anonymous access formal "
13445 & "not allowed", Operand);
13448 -- This is a case where there's an enclosing object whose
13449 -- to which the "statically deeper than" relationship does
13450 -- not apply (such as an access discriminant selected from
13451 -- a dereference of an access parameter).
13453 elsif Object_Access_Level (Operand)
13454 = Scope_Depth (Standard_Standard)
13457 ("implicit conversion of anonymous access value "
13458 & "not allowed", Operand);
13461 -- In other cases, the level of the operand's type must be
13462 -- statically less deep than that of the target type, else
13463 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
13465 elsif Type_Access_Level (Opnd_Type) >
13466 Deepest_Type_Access_Level (Target_Type)
13469 ("implicit conversion of anonymous access value "
13470 & "violates accessibility", Operand);
13475 elsif Type_Access_Level (Opnd_Type) >
13476 Deepest_Type_Access_Level (Target_Type)
13478 -- In an instance, this is a run-time check, but one we know
13479 -- will fail, so generate an appropriate warning. The raise
13480 -- will be generated by Expand_N_Type_Conversion.
13482 if In_Instance_Body then
13483 Error_Msg_Warn := SPARK_Mode /= On;
13485 ("cannot convert local pointer to non-local access type<<",
13487 Conversion_Error_N ("\Program_Error [<<", Operand);
13489 -- If not in an instance body, this is a real error
13492 -- Avoid generation of spurious error message
13494 if not Error_Posted (N) then
13496 ("cannot convert local pointer to non-local access type",
13503 -- Special accessibility checks are needed in the case of access
13504 -- discriminants declared for a limited type.
13506 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
13507 and then not Is_Local_Anonymous_Access (Opnd_Type)
13509 -- When the operand is a selected access discriminant the check
13510 -- needs to be made against the level of the object denoted by
13511 -- the prefix of the selected name (Object_Access_Level handles
13512 -- checking the prefix of the operand for this case).
13514 if Nkind (Operand) = N_Selected_Component
13515 and then Object_Access_Level (Operand) >
13516 Deepest_Type_Access_Level (Target_Type)
13518 -- In an instance, this is a run-time check, but one we know
13519 -- will fail, so generate an appropriate warning. The raise
13520 -- will be generated by Expand_N_Type_Conversion.
13522 if In_Instance_Body then
13523 Error_Msg_Warn := SPARK_Mode /= On;
13525 ("cannot convert access discriminant to non-local "
13526 & "access type<<", Operand);
13527 Conversion_Error_N ("\Program_Error [<<", Operand);
13529 -- If not in an instance body, this is a real error
13533 ("cannot convert access discriminant to non-local "
13534 & "access type", Operand);
13539 -- The case of a reference to an access discriminant from
13540 -- within a limited type declaration (which will appear as
13541 -- a discriminal) is always illegal because the level of the
13542 -- discriminant is considered to be deeper than any (nameable)
13545 if Is_Entity_Name (Operand)
13547 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
13548 and then Present (Discriminal_Link (Entity (Operand)))
13551 ("discriminant has deeper accessibility level than target",
13558 -- In the presence of limited_with clauses we have to use nonlimited
13559 -- views, if available.
13561 Check_Limited : declare
13562 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
13563 -- Helper function to handle limited views
13565 --------------------------
13566 -- Full_Designated_Type --
13567 --------------------------
13569 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
13570 Desig : constant Entity_Id := Designated_Type (T);
13573 -- Handle the limited view of a type
13575 if From_Limited_With (Desig)
13576 and then Has_Non_Limited_View (Desig)
13578 return Available_View (Desig);
13582 end Full_Designated_Type;
13584 -- Local Declarations
13586 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
13587 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
13589 Same_Base : constant Boolean :=
13590 Base_Type (Target) = Base_Type (Opnd);
13592 -- Start of processing for Check_Limited
13595 if Is_Tagged_Type (Target) then
13596 return Valid_Tagged_Conversion (Target, Opnd);
13599 if not Same_Base then
13600 Conversion_Error_NE
13601 ("target designated type not compatible with }",
13602 N, Base_Type (Opnd));
13605 -- Ada 2005 AI-384: legality rule is symmetric in both
13606 -- designated types. The conversion is legal (with possible
13607 -- constraint check) if either designated type is
13610 elsif Subtypes_Statically_Match (Target, Opnd)
13612 (Has_Discriminants (Target)
13614 (not Is_Constrained (Opnd)
13615 or else not Is_Constrained (Target)))
13617 -- Special case, if Value_Size has been used to make the
13618 -- sizes different, the conversion is not allowed even
13619 -- though the subtypes statically match.
13621 if Known_Static_RM_Size (Target)
13622 and then Known_Static_RM_Size (Opnd)
13623 and then RM_Size (Target) /= RM_Size (Opnd)
13625 Conversion_Error_NE
13626 ("target designated subtype not compatible with }",
13628 Conversion_Error_NE
13629 ("\because sizes of the two designated subtypes differ",
13633 -- Normal case where conversion is allowed
13641 ("target designated subtype not compatible with }",
13648 -- Access to subprogram types. If the operand is an access parameter,
13649 -- the type has a deeper accessibility that any master, and cannot be
13650 -- assigned. We must make an exception if the conversion is part of an
13651 -- assignment and the target is the return object of an extended return
13652 -- statement, because in that case the accessibility check takes place
13653 -- after the return.
13655 elsif Is_Access_Subprogram_Type (Target_Type)
13657 -- Note: this test of Opnd_Type is there to prevent entering this
13658 -- branch in the case of a remote access to subprogram type, which
13659 -- is internally represented as an E_Record_Type.
13661 and then Is_Access_Type (Opnd_Type)
13663 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
13664 and then Is_Entity_Name (Operand)
13665 and then Ekind (Entity (Operand)) = E_In_Parameter
13667 (Nkind (Parent (N)) /= N_Assignment_Statement
13668 or else not Is_Entity_Name (Name (Parent (N)))
13669 or else not Is_Return_Object (Entity (Name (Parent (N)))))
13672 ("illegal attempt to store anonymous access to subprogram",
13675 ("\value has deeper accessibility than any master "
13676 & "(RM 3.10.2 (13))",
13680 ("\use named access type for& instead of access parameter",
13681 Operand, Entity (Operand));
13684 -- Check that the designated types are subtype conformant
13686 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
13687 Old_Id => Designated_Type (Opnd_Type),
13690 -- Check the static accessibility rule of 4.6(20)
13692 if Type_Access_Level (Opnd_Type) >
13693 Deepest_Type_Access_Level (Target_Type)
13696 ("operand type has deeper accessibility level than target",
13699 -- Check that if the operand type is declared in a generic body,
13700 -- then the target type must be declared within that same body
13701 -- (enforces last sentence of 4.6(20)).
13703 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
13705 O_Gen : constant Node_Id :=
13706 Enclosing_Generic_Body (Opnd_Type);
13711 T_Gen := Enclosing_Generic_Body (Target_Type);
13712 while Present (T_Gen) and then T_Gen /= O_Gen loop
13713 T_Gen := Enclosing_Generic_Body (T_Gen);
13716 if T_Gen /= O_Gen then
13718 ("target type must be declared in same generic body "
13719 & "as operand type", N);
13726 -- Remote access to subprogram types
13728 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
13729 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
13731 -- It is valid to convert from one RAS type to another provided
13732 -- that their specification statically match.
13734 -- Note: at this point, remote access to subprogram types have been
13735 -- expanded to their E_Record_Type representation, and we need to
13736 -- go back to the original access type definition using the
13737 -- Corresponding_Remote_Type attribute in order to check that the
13738 -- designated profiles match.
13740 pragma Assert (Ekind (Target_Type) = E_Record_Type);
13741 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
13743 Check_Subtype_Conformant
13745 Designated_Type (Corresponding_Remote_Type (Target_Type)),
13747 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
13752 -- If it was legal in the generic, it's legal in the instance
13754 elsif In_Instance_Body then
13757 -- If both are tagged types, check legality of view conversions
13759 elsif Is_Tagged_Type (Target_Type)
13761 Is_Tagged_Type (Opnd_Type)
13763 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
13765 -- Types derived from the same root type are convertible
13767 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
13770 -- In an instance or an inlined body, there may be inconsistent views of
13771 -- the same type, or of types derived from a common root.
13773 elsif (In_Instance or In_Inlined_Body)
13775 Root_Type (Underlying_Type (Target_Type)) =
13776 Root_Type (Underlying_Type (Opnd_Type))
13780 -- Special check for common access type error case
13782 elsif Ekind (Target_Type) = E_Access_Type
13783 and then Is_Access_Type (Opnd_Type)
13785 Conversion_Error_N ("target type must be general access type!", N);
13786 Conversion_Error_NE -- CODEFIX
13787 ("add ALL to }!", N, Target_Type);
13790 -- Here we have a real conversion error
13793 -- Check for missing regular with_clause when only a limited view of
13794 -- target is available.
13796 if From_Limited_With (Opnd_Type) and then In_Package_Body then
13797 Conversion_Error_NE
13798 ("invalid conversion, not compatible with limited view of }",
13800 Conversion_Error_NE
13801 ("\add with_clause for& to current unit!", N, Scope (Opnd_Type));
13803 elsif Is_Access_Type (Opnd_Type)
13804 and then From_Limited_With (Designated_Type (Opnd_Type))
13805 and then In_Package_Body
13807 Conversion_Error_NE
13808 ("invalid conversion, not compatible with }", N, Opnd_Type);
13809 Conversion_Error_NE
13810 ("\add with_clause for& to current unit!",
13811 N, Scope (Designated_Type (Opnd_Type)));
13814 Conversion_Error_NE
13815 ("invalid conversion, not compatible with }", N, Opnd_Type);
13820 end Valid_Conversion;