1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Debug_A; use Debug_A;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Inline; use Inline;
44 with Itypes; use Itypes;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Nlists; use Nlists;
51 with Output; use Output;
52 with Par_SCO; use Par_SCO;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
57 with Sem_Aggr; use Sem_Aggr;
58 with Sem_Attr; use Sem_Attr;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Cat; use Sem_Cat;
61 with Sem_Ch3; use Sem_Ch3;
62 with Sem_Ch4; use Sem_Ch4;
63 with Sem_Ch6; use Sem_Ch6;
64 with Sem_Ch8; use Sem_Ch8;
65 with Sem_Ch13; use Sem_Ch13;
66 with Sem_Dim; use Sem_Dim;
67 with Sem_Disp; use Sem_Disp;
68 with Sem_Dist; use Sem_Dist;
69 with Sem_Elab; use Sem_Elab;
70 with Sem_Elim; use Sem_Elim;
71 with Sem_Eval; use Sem_Eval;
72 with Sem_Intr; use Sem_Intr;
73 with Sem_Mech; use Sem_Mech;
74 with Sem_Type; use Sem_Type;
75 with Sem_Util; use Sem_Util;
76 with Sem_Warn; use Sem_Warn;
77 with Sinfo; use Sinfo;
78 with Sinfo.Nodes; use Sinfo.Nodes;
79 with Sinfo.Utils; use Sinfo.Utils;
80 with Sinfo.CN; use Sinfo.CN;
81 with Snames; use Snames;
82 with Stand; use Stand;
83 with Stringt; use Stringt;
84 with Style; use Style;
85 with Targparm; use Targparm;
86 with Tbuild; use Tbuild;
87 with Uintp; use Uintp;
88 with Urealp; use Urealp;
90 package body Sem_Res is
92 -----------------------
93 -- Local Subprograms --
94 -----------------------
96 -- Second pass (top-down) type checking and overload resolution procedures
97 -- Typ is the type required by context. These procedures propagate the
98 -- type information recursively to the descendants of N. If the node is not
99 -- overloaded, its Etype is established in the first pass. If overloaded,
100 -- the Resolve routines set the correct type. For arithmetic operators, the
101 -- Etype is the base type of the context.
103 -- Note that Resolve_Attribute is separated off in Sem_Attr
105 procedure Check_Discriminant_Use (N : Node_Id);
106 -- Enforce the restrictions on the use of discriminants when constraining
107 -- a component of a discriminated type (record or concurrent type).
109 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
110 -- Given a node for an operator associated with type T, check that the
111 -- operator is visible. Operators all of whose operands are universal must
112 -- be checked for visibility during resolution because their type is not
113 -- determinable based on their operands.
115 procedure Check_Fully_Declared_Prefix
118 -- Check that the type of the prefix of a dereference is not incomplete
120 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
121 -- Given a call node, Call, which is known to occur immediately within the
122 -- subprogram being called, determines whether it is a detectable case of
123 -- an infinite recursion, and if so, outputs appropriate messages. Returns
124 -- True if an infinite recursion is detected, and False otherwise.
126 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
127 -- N is the node for a logical operator. If the operator is predefined, and
128 -- the root type of the operands is Standard.Boolean, then a check is made
129 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
130 -- the style check for Style_Check_Boolean_And_Or.
132 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
133 -- N is either an indexed component or a selected component. This function
134 -- returns true if the prefix refers to an object that has an address
135 -- clause (the case in which we may want to issue a warning).
137 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
138 -- Determine whether E is an access type declared by an access declaration,
139 -- and not an (anonymous) allocator type.
141 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
142 -- Utility to check whether the entity for an operator is a predefined
143 -- operator, in which case the expression is left as an operator in the
144 -- tree (else it is rewritten into a call). An instance of an intrinsic
145 -- conversion operation may be given an operator name, but is not treated
146 -- like an operator. Note that an operator that is an imported back-end
147 -- builtin has convention Intrinsic, but is expected to be rewritten into
148 -- a call, so such an operator is not treated as predefined by this
151 procedure Preanalyze_And_Resolve
154 With_Freezing : Boolean);
155 -- Subsidiary of public versions of Preanalyze_And_Resolve.
157 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
158 -- If a default expression in entry call N depends on the discriminants
159 -- of the task, it must be replaced with a reference to the discriminant
160 -- of the task being called.
162 procedure Resolve_Op_Concat_Arg
167 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
168 -- concatenation operator. The operand is either of the array type or of
169 -- the component type. If the operand is an aggregate, and the component
170 -- type is composite, this is ambiguous if component type has aggregates.
172 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
173 -- Does the first part of the work of Resolve_Op_Concat
175 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
176 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
177 -- has been resolved. See Resolve_Op_Concat for details.
179 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Declare_Expression (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
210 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
211 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
212 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
213 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
214 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
215 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
217 function Operator_Kind
219 Is_Binary : Boolean) return Node_Kind;
220 -- Utility to map the name of an operator into the corresponding Node. Used
221 -- by other node rewriting procedures.
223 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
224 -- Resolve actuals of call, and add default expressions for missing ones.
225 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
226 -- called subprogram.
228 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
229 -- Called from Resolve_Call, when the prefix denotes an entry or element
230 -- of entry family. Actuals are resolved as for subprograms, and the node
231 -- is rebuilt as an entry call. Also called for protected operations. Typ
232 -- is the context type, which is used when the operation is a protected
233 -- function with no arguments, and the return value is indexed.
235 procedure Resolve_Implicit_Dereference (P : Node_Id);
236 -- Called when P is the prefix of an indexed component, or of a selected
237 -- component, or of a slice. If P is of an access type, we unconditionally
238 -- rewrite it as an explicit dereference. This ensures that the expander
239 -- and the code generator have a fully explicit tree to work with.
241 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
242 -- A call to a user-defined intrinsic operator is rewritten as a call to
243 -- the corresponding predefined operator, with suitable conversions. Note
244 -- that this applies only for intrinsic operators that denote predefined
245 -- operators, not ones that are intrinsic imports of back-end builtins.
247 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
248 -- Ditto, for arithmetic unary operators
250 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
251 -- If an operator node resolves to a call to a user-defined operator,
252 -- rewrite the node as a function call.
254 procedure Make_Call_Into_Operator
258 -- Inverse transformation: if an operator is given in functional notation,
259 -- then after resolving the node, transform into an operator node, so that
260 -- operands are resolved properly. Recall that predefined operators do not
261 -- have a full signature and special resolution rules apply.
263 procedure Rewrite_Renamed_Operator
267 -- An operator can rename another, e.g. in an instantiation. In that
268 -- case, the proper operator node must be constructed and resolved.
270 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
271 -- The String_Literal_Subtype is built for all strings that are not
272 -- operands of a static concatenation operation. If the argument is not
273 -- a N_String_Literal node, then the call has no effect.
275 procedure Set_Slice_Subtype (N : Node_Id);
276 -- Build subtype of array type, with the range specified by the slice
278 procedure Simplify_Type_Conversion (N : Node_Id);
279 -- Called after N has been resolved and evaluated, but before range checks
280 -- have been applied. This rewrites the conversion into a simpler form.
282 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
283 -- A universal_fixed expression in an universal context is unambiguous if
284 -- there is only one applicable fixed point type. Determining whether there
285 -- is only one requires a search over all visible entities, and happens
286 -- only in very pathological cases (see 6115-006).
288 -------------------------
289 -- Ambiguous_Character --
290 -------------------------
292 procedure Ambiguous_Character (C : Node_Id) is
296 if Nkind (C) = N_Character_Literal then
297 Error_Msg_N ("ambiguous character literal", C);
299 -- First the ones in Standard
301 Error_Msg_N ("\\possible interpretation: Character!", C);
302 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
304 -- Include Wide_Wide_Character in Ada 2005 mode
306 if Ada_Version >= Ada_2005 then
307 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
310 -- Now any other types that match
312 E := Current_Entity (C);
313 while Present (E) loop
314 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
318 end Ambiguous_Character;
320 -------------------------
321 -- Analyze_And_Resolve --
322 -------------------------
324 procedure Analyze_And_Resolve (N : Node_Id) is
328 end Analyze_And_Resolve;
330 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
334 end Analyze_And_Resolve;
336 -- Versions with check(s) suppressed
338 procedure Analyze_And_Resolve
343 Scop : constant Entity_Id := Current_Scope;
346 if Suppress = All_Checks then
348 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
350 Scope_Suppress.Suppress := (others => True);
351 Analyze_And_Resolve (N, Typ);
352 Scope_Suppress.Suppress := Sva;
357 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
359 Scope_Suppress.Suppress (Suppress) := True;
360 Analyze_And_Resolve (N, Typ);
361 Scope_Suppress.Suppress (Suppress) := Svg;
365 if Current_Scope /= Scop
366 and then Scope_Is_Transient
368 -- This can only happen if a transient scope was created for an inner
369 -- expression, which will be removed upon completion of the analysis
370 -- of an enclosing construct. The transient scope must have the
371 -- suppress status of the enclosing environment, not of this Analyze
374 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
377 end Analyze_And_Resolve;
379 procedure Analyze_And_Resolve
383 Scop : constant Entity_Id := Current_Scope;
386 if Suppress = All_Checks then
388 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
390 Scope_Suppress.Suppress := (others => True);
391 Analyze_And_Resolve (N);
392 Scope_Suppress.Suppress := Sva;
397 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
399 Scope_Suppress.Suppress (Suppress) := True;
400 Analyze_And_Resolve (N);
401 Scope_Suppress.Suppress (Suppress) := Svg;
405 if Current_Scope /= Scop and then Scope_Is_Transient then
406 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
409 end Analyze_And_Resolve;
411 ----------------------------
412 -- Check_Discriminant_Use --
413 ----------------------------
415 procedure Check_Discriminant_Use (N : Node_Id) is
416 PN : constant Node_Id := Parent (N);
417 Disc : constant Entity_Id := Entity (N);
422 -- Any use in a spec-expression is legal
424 if In_Spec_Expression then
427 elsif Nkind (PN) = N_Range then
429 -- Discriminant cannot be used to constrain a scalar type
433 if Nkind (P) = N_Range_Constraint
434 and then Nkind (Parent (P)) = N_Subtype_Indication
435 and then Nkind (Parent (Parent (P))) = N_Component_Definition
437 Error_Msg_N ("discriminant cannot constrain scalar type", N);
439 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
441 -- The following check catches the unusual case where a
442 -- discriminant appears within an index constraint that is part
443 -- of a larger expression within a constraint on a component,
444 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
445 -- check case of record components, and note that a similar check
446 -- should also apply in the case of discriminant constraints
449 -- Note that the check for N_Subtype_Declaration below is to
450 -- detect the valid use of discriminants in the constraints of a
451 -- subtype declaration when this subtype declaration appears
452 -- inside the scope of a record type (which is syntactically
453 -- illegal, but which may be created as part of derived type
454 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
457 if Ekind (Current_Scope) = E_Record_Type
458 and then Scope (Disc) = Current_Scope
460 (Nkind (Parent (P)) = N_Subtype_Indication
462 Nkind (Parent (Parent (P))) in N_Component_Definition
463 | N_Subtype_Declaration
464 and then Paren_Count (N) = 0)
467 ("discriminant must appear alone in component constraint", N);
471 -- Detect a common error:
473 -- type R (D : Positive := 100) is record
474 -- Name : String (1 .. D);
477 -- The default value causes an object of type R to be allocated
478 -- with room for Positive'Last characters. The RM does not mandate
479 -- the allocation of the maximum size, but that is what GNAT does
480 -- so we should warn the programmer that there is a problem.
482 Check_Large : declare
488 function Large_Storage_Type (T : Entity_Id) return Boolean;
489 -- Return True if type T has a large enough range that any
490 -- array whose index type covered the whole range of the type
491 -- would likely raise Storage_Error.
493 ------------------------
494 -- Large_Storage_Type --
495 ------------------------
497 function Large_Storage_Type (T : Entity_Id) return Boolean is
499 -- The type is considered large if its bounds are known at
500 -- compile time and if it requires at least as many bits as
501 -- a Positive to store the possible values.
503 return Compile_Time_Known_Value (Type_Low_Bound (T))
504 and then Compile_Time_Known_Value (Type_High_Bound (T))
506 Minimum_Size (T, Biased => True) >=
507 RM_Size (Standard_Positive);
508 end Large_Storage_Type;
510 -- Start of processing for Check_Large
513 -- Check that the Disc has a large range
515 if not Large_Storage_Type (Etype (Disc)) then
519 -- If the enclosing type is limited, we allocate only the
520 -- default value, not the maximum, and there is no need for
523 if Is_Limited_Type (Scope (Disc)) then
527 -- Check that it is the high bound
529 if N /= High_Bound (PN)
530 or else No (Discriminant_Default_Value (Disc))
535 -- Check the array allows a large range at this bound. First
540 if Nkind (SI) /= N_Subtype_Indication then
544 T := Entity (Subtype_Mark (SI));
546 if not Is_Array_Type (T) then
550 -- Next, find the dimension
552 TB := First_Index (T);
553 CB := First (Constraints (P));
555 and then Present (TB)
556 and then Present (CB)
567 -- Now, check the dimension has a large range
569 if not Large_Storage_Type (Etype (TB)) then
573 -- Warn about the danger
576 ("??creation of & object may raise Storage_Error!",
585 -- Legal case is in index or discriminant constraint
587 elsif Nkind (PN) in N_Index_Or_Discriminant_Constraint
588 | N_Discriminant_Association
590 if Paren_Count (N) > 0 then
592 ("discriminant in constraint must appear alone", N);
594 elsif Nkind (N) = N_Expanded_Name
595 and then Comes_From_Source (N)
598 ("discriminant must appear alone as a direct name", N);
603 -- Otherwise, context is an expression. It should not be within (i.e. a
604 -- subexpression of) a constraint for a component.
609 while Nkind (P) not in
610 N_Component_Declaration | N_Subtype_Indication | N_Entry_Declaration
617 -- If the discriminant is used in an expression that is a bound of a
618 -- scalar type, an Itype is created and the bounds are attached to
619 -- its range, not to the original subtype indication. Such use is of
620 -- course a double fault.
622 if (Nkind (P) = N_Subtype_Indication
623 and then Nkind (Parent (P)) in N_Component_Definition
624 | N_Derived_Type_Definition
625 and then D = Constraint (P))
627 -- The constraint itself may be given by a subtype indication,
628 -- rather than by a more common discrete range.
630 or else (Nkind (P) = N_Subtype_Indication
632 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
633 or else Nkind (P) = N_Entry_Declaration
634 or else Nkind (D) = N_Defining_Identifier
637 ("discriminant in constraint must appear alone", N);
640 end Check_Discriminant_Use;
642 --------------------------------
643 -- Check_For_Visible_Operator --
644 --------------------------------
646 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
648 if Is_Invisible_Operator (N, T) then
649 Error_Msg_NE -- CODEFIX
650 ("operator for} is not directly visible!", N, First_Subtype (T));
651 Error_Msg_N -- CODEFIX
652 ("use clause would make operation legal!", N);
654 end Check_For_Visible_Operator;
656 ----------------------------------
657 -- Check_Fully_Declared_Prefix --
658 ----------------------------------
660 procedure Check_Fully_Declared_Prefix
665 -- Check that the designated type of the prefix of a dereference is
666 -- not an incomplete type. This cannot be done unconditionally, because
667 -- dereferences of private types are legal in default expressions. This
668 -- case is taken care of in Check_Fully_Declared, called below. There
669 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
671 -- This consideration also applies to similar checks for allocators,
672 -- qualified expressions, and type conversions.
674 -- An additional exception concerns other per-object expressions that
675 -- are not directly related to component declarations, in particular
676 -- representation pragmas for tasks. These will be per-object
677 -- expressions if they depend on discriminants or some global entity.
678 -- If the task has access discriminants, the designated type may be
679 -- incomplete at the point the expression is resolved. This resolution
680 -- takes place within the body of the initialization procedure, where
681 -- the discriminant is replaced by its discriminal.
683 if Is_Entity_Name (Pref)
684 and then Ekind (Entity (Pref)) = E_In_Parameter
688 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
689 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
690 -- Analyze_Object_Renaming, and Freeze_Entity.
692 elsif Ada_Version >= Ada_2005
693 and then Is_Entity_Name (Pref)
694 and then Is_Access_Type (Etype (Pref))
695 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
697 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
701 Check_Fully_Declared (Typ, Parent (Pref));
703 end Check_Fully_Declared_Prefix;
705 ------------------------------
706 -- Check_Infinite_Recursion --
707 ------------------------------
709 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
710 function Enclosing_Declaration_Or_Statement (N : Node_Id) return Node_Id;
711 -- Return the nearest enclosing declaration or statement that houses
714 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
715 -- Determine whether call N invokes the related enclosing subprogram
716 -- with actuals that differ from the subprogram's formals.
718 function Is_Conditional_Statement (N : Node_Id) return Boolean;
719 -- Determine whether arbitrary node N denotes a conditional construct
721 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
722 -- Determine whether arbitrary node N denotes a control flow statement
723 -- or a construct that may contains such a statement.
725 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
726 -- Determine whether arbitrary node N appears immediately within the
727 -- statements of an entry or subprogram body.
729 function Is_Raise_Idiom (N : Node_Id) return Boolean;
730 -- Determine whether arbitrary node N appears immediately within the
731 -- body of an entry or subprogram, and is preceded by a single raise
734 function Is_Raise_Statement (N : Node_Id) return Boolean;
735 -- Determine whether arbitrary node N denotes a raise statement
737 function Is_Sole_Statement (N : Node_Id) return Boolean;
738 -- Determine whether arbitrary node N is the sole source statement in
739 -- the body of the enclosing subprogram.
741 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
742 -- Determine whether arbitrary node N is preceded by a control flow
745 function Within_Conditional_Statement (N : Node_Id) return Boolean;
746 -- Determine whether arbitrary node N appears within a conditional
749 ----------------------------------------
750 -- Enclosing_Declaration_Or_Statement --
751 ----------------------------------------
753 function Enclosing_Declaration_Or_Statement
754 (N : Node_Id) return Node_Id
760 while Present (Par) loop
761 if Is_Declaration (Par) or else Is_Statement (Par) then
764 -- Prevent the search from going too far
766 elsif Is_Body_Or_Package_Declaration (Par) then
774 end Enclosing_Declaration_Or_Statement;
776 --------------------------------------
777 -- Invoked_With_Different_Arguments --
778 --------------------------------------
780 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
781 Subp : constant Entity_Id := Entity (Name (N));
787 -- Determine whether the formals of the invoked subprogram are not
788 -- used as actuals in the call.
790 Actual := First_Actual (Call);
791 Formal := First_Formal (Subp);
792 while Present (Actual) and then Present (Formal) loop
794 -- The current actual does not match the current formal
796 if not (Is_Entity_Name (Actual)
797 and then Entity (Actual) = Formal)
802 Next_Actual (Actual);
803 Next_Formal (Formal);
807 end Invoked_With_Different_Arguments;
809 ------------------------------
810 -- Is_Conditional_Statement --
811 ------------------------------
813 function Is_Conditional_Statement (N : Node_Id) return Boolean is
816 Nkind (N) in N_And_Then
822 end Is_Conditional_Statement;
824 -------------------------------
825 -- Is_Control_Flow_Statement --
826 -------------------------------
828 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
830 -- It is assumed that all statements may affect the control flow in
831 -- some way. A raise statement may be expanded into a non-statement
834 return Is_Statement (N) or else Is_Raise_Statement (N);
835 end Is_Control_Flow_Statement;
837 --------------------------------
838 -- Is_Immediately_Within_Body --
839 --------------------------------
841 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
842 HSS : constant Node_Id := Parent (N);
846 Nkind (HSS) = N_Handled_Sequence_Of_Statements
847 and then Nkind (Parent (HSS)) in N_Entry_Body | N_Subprogram_Body
848 and then Is_List_Member (N)
849 and then List_Containing (N) = Statements (HSS);
850 end Is_Immediately_Within_Body;
856 function Is_Raise_Idiom (N : Node_Id) return Boolean is
857 Raise_Stmt : Node_Id;
861 if Is_Immediately_Within_Body (N) then
863 -- Assume that no raise statement has been seen yet
867 -- Examine the statements preceding the input node, skipping
868 -- internally-generated constructs.
871 while Present (Stmt) loop
873 -- Multiple raise statements violate the idiom
875 if Is_Raise_Statement (Stmt) then
876 if Present (Raise_Stmt) then
882 elsif Comes_From_Source (Stmt) then
889 -- At this point the node must be preceded by a raise statement,
890 -- and the raise statement has to be the sole statement within
891 -- the enclosing entry or subprogram body.
894 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
900 ------------------------
901 -- Is_Raise_Statement --
902 ------------------------
904 function Is_Raise_Statement (N : Node_Id) return Boolean is
906 -- A raise statement may be transfomed into a Raise_xxx_Error node
909 Nkind (N) = N_Raise_Statement
910 or else Nkind (N) in N_Raise_xxx_Error;
911 end Is_Raise_Statement;
913 -----------------------
914 -- Is_Sole_Statement --
915 -----------------------
917 function Is_Sole_Statement (N : Node_Id) return Boolean is
921 -- The input node appears within the statements of an entry or
922 -- subprogram body. Examine the statements preceding the node.
924 if Is_Immediately_Within_Body (N) then
927 while Present (Stmt) loop
929 -- The statement is preceded by another statement or a source
930 -- construct. This indicates that the node does not appear by
933 if Is_Control_Flow_Statement (Stmt)
934 or else Comes_From_Source (Stmt)
945 -- The input node is within a construct nested inside the entry or
949 end Is_Sole_Statement;
951 ----------------------------------------
952 -- Preceded_By_Control_Flow_Statement --
953 ----------------------------------------
955 function Preceded_By_Control_Flow_Statement
956 (N : Node_Id) return Boolean
961 if Is_List_Member (N) then
964 -- Examine the statements preceding the input node
966 while Present (Stmt) loop
967 if Is_Control_Flow_Statement (Stmt) then
977 -- Assume that the node is part of some control flow statement
980 end Preceded_By_Control_Flow_Statement;
982 ----------------------------------
983 -- Within_Conditional_Statement --
984 ----------------------------------
986 function Within_Conditional_Statement (N : Node_Id) return Boolean is
991 while Present (Stmt) loop
992 if Is_Conditional_Statement (Stmt) then
995 -- Prevent the search from going too far
997 elsif Is_Body_Or_Package_Declaration (Stmt) then
1001 Stmt := Parent (Stmt);
1005 end Within_Conditional_Statement;
1009 Call_Context : constant Node_Id :=
1010 Enclosing_Declaration_Or_Statement (Call);
1012 -- Start of processing for Check_Infinite_Recursion
1015 -- The call is assumed to be safe when the enclosing subprogram is
1016 -- invoked with actuals other than its formals.
1018 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1021 -- Proc (A1, A2, ..., AN);
1025 if Invoked_With_Different_Arguments (Call) then
1028 -- The call is assumed to be safe when the invocation of the enclosing
1029 -- subprogram depends on a conditional statement.
1031 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1034 -- if Some_Condition then
1035 -- Proc (F1, F2, ..., FN);
1040 elsif Within_Conditional_Statement (Call) then
1043 -- The context of the call is assumed to be safe when the invocation of
1044 -- the enclosing subprogram is preceded by some control flow statement.
1046 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1049 -- if Some_Condition then
1053 -- Proc (F1, F2, ..., FN);
1057 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1060 -- Detect an idiom where the context of the call is preceded by a single
1063 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1066 -- Proc (F1, F2, ..., FN);
1069 elsif Is_Raise_Idiom (Call_Context) then
1073 -- At this point it is certain that infinite recursion will take place
1074 -- as long as the call is executed. Detect a case where the context of
1075 -- the call is the sole source statement within the subprogram body.
1077 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1079 -- Proc (F1, F2, ..., FN);
1082 -- Install an explicit raise to prevent the infinite recursion.
1084 if Is_Sole_Statement (Call_Context) then
1085 Error_Msg_Warn := SPARK_Mode /= On;
1086 Error_Msg_N ("!infinite recursion<<", Call);
1087 Error_Msg_N ("\!Storage_Error [<<", Call);
1089 Insert_Action (Call,
1090 Make_Raise_Storage_Error (Sloc (Call),
1091 Reason => SE_Infinite_Recursion));
1093 -- Otherwise infinite recursion could take place, considering other flow
1094 -- control constructs such as gotos, exit statements, etc.
1097 Error_Msg_Warn := SPARK_Mode /= On;
1098 Error_Msg_N ("!possible infinite recursion<<", Call);
1099 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1103 end Check_Infinite_Recursion;
1105 ---------------------------------------
1106 -- Check_No_Direct_Boolean_Operators --
1107 ---------------------------------------
1109 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1111 if Scope (Entity (N)) = Standard_Standard
1112 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1114 -- Restriction only applies to original source code
1116 if Comes_From_Source (N) then
1117 Check_Restriction (No_Direct_Boolean_Operators, N);
1121 -- Do style check (but skip if in instance, error is on template)
1124 if not In_Instance then
1125 Check_Boolean_Operator (N);
1128 end Check_No_Direct_Boolean_Operators;
1130 ------------------------------
1131 -- Check_Parameterless_Call --
1132 ------------------------------
1134 procedure Check_Parameterless_Call (N : Node_Id) is
1137 function Prefix_Is_Access_Subp return Boolean;
1138 -- If the prefix is of an access_to_subprogram type, the node must be
1139 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1140 -- interpretations are access to subprograms.
1142 ---------------------------
1143 -- Prefix_Is_Access_Subp --
1144 ---------------------------
1146 function Prefix_Is_Access_Subp return Boolean is
1151 -- If the context is an attribute reference that can apply to
1152 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1154 if Nkind (Parent (N)) = N_Attribute_Reference
1155 and then Attribute_Name (Parent (N))
1156 in Name_Address | Name_Code_Address | Name_Access
1161 if not Is_Overloaded (N) then
1163 Ekind (Etype (N)) = E_Subprogram_Type
1164 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1166 Get_First_Interp (N, I, It);
1167 while Present (It.Typ) loop
1168 if Ekind (It.Typ) /= E_Subprogram_Type
1169 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1174 Get_Next_Interp (I, It);
1179 end Prefix_Is_Access_Subp;
1181 -- Start of processing for Check_Parameterless_Call
1184 -- Defend against junk stuff if errors already detected
1186 if Total_Errors_Detected /= 0 then
1187 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1189 elsif Nkind (N) in N_Has_Chars
1190 and then not Is_Valid_Name (Chars (N))
1198 -- If the context expects a value, and the name is a procedure, this is
1199 -- most likely a missing 'Access. Don't try to resolve the parameterless
1200 -- call, error will be caught when the outer call is analyzed.
1202 if Is_Entity_Name (N)
1203 and then Ekind (Entity (N)) = E_Procedure
1204 and then not Is_Overloaded (N)
1206 Nkind (Parent (N)) in N_Parameter_Association
1208 | N_Procedure_Call_Statement
1213 -- Rewrite as call if overloadable entity that is (or could be, in the
1214 -- overloaded case) a function call. If we know for sure that the entity
1215 -- is an enumeration literal, we do not rewrite it.
1217 -- If the entity is the name of an operator, it cannot be a call because
1218 -- operators cannot have default parameters. In this case, this must be
1219 -- a string whose contents coincide with an operator name. Set the kind
1220 -- of the node appropriately.
1222 if (Is_Entity_Name (N)
1223 and then Nkind (N) /= N_Operator_Symbol
1224 and then Is_Overloadable (Entity (N))
1225 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1226 or else Is_Overloaded (N)))
1228 -- Rewrite as call if it is an explicit dereference of an expression of
1229 -- a subprogram access type, and the subprogram type is not that of a
1230 -- procedure or entry.
1233 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1235 -- Rewrite as call if it is a selected component which is a function,
1236 -- this is the case of a call to a protected function (which may be
1237 -- overloaded with other protected operations).
1240 (Nkind (N) = N_Selected_Component
1241 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1243 (Ekind (Entity (Selector_Name (N))) in
1244 E_Entry | E_Procedure
1245 and then Is_Overloaded (Selector_Name (N)))))
1247 -- If one of the above three conditions is met, rewrite as call. Apply
1248 -- the rewriting only once.
1251 if Nkind (Parent (N)) /= N_Function_Call
1252 or else N /= Name (Parent (N))
1255 -- This may be a prefixed call that was not fully analyzed, e.g.
1256 -- an actual in an instance.
1258 if Ada_Version >= Ada_2005
1259 and then Nkind (N) = N_Selected_Component
1260 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1262 Analyze_Selected_Component (N);
1264 if Nkind (N) /= N_Selected_Component then
1269 -- The node is the name of the parameterless call. Preserve its
1270 -- descendants, which may be complex expressions.
1272 Nam := Relocate_Node (N);
1274 -- If overloaded, overload set belongs to new copy
1276 Save_Interps (N, Nam);
1278 -- Change node to parameterless function call (note that the
1279 -- Parameter_Associations associations field is left set to Empty,
1280 -- its normal default value since there are no parameters)
1282 Change_Node (N, N_Function_Call);
1284 Set_Sloc (N, Sloc (Nam));
1288 elsif Nkind (N) = N_Parameter_Association then
1289 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1291 elsif Nkind (N) = N_Operator_Symbol then
1292 Set_Etype (N, Empty);
1293 Set_Entity (N, Empty);
1294 Set_Is_Overloaded (N, False);
1295 Change_Operator_Symbol_To_String_Literal (N);
1296 Set_Etype (N, Any_String);
1298 end Check_Parameterless_Call;
1300 --------------------------------
1301 -- Is_Atomic_Ref_With_Address --
1302 --------------------------------
1304 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1305 Pref : constant Node_Id := Prefix (N);
1308 if not Is_Entity_Name (Pref) then
1313 Pent : constant Entity_Id := Entity (Pref);
1314 Ptyp : constant Entity_Id := Etype (Pent);
1316 return not Is_Access_Type (Ptyp)
1317 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1318 and then Present (Address_Clause (Pent));
1321 end Is_Atomic_Ref_With_Address;
1323 -----------------------------
1324 -- Is_Definite_Access_Type --
1325 -----------------------------
1327 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1328 Btyp : constant Entity_Id := Base_Type (E);
1330 return Ekind (Btyp) = E_Access_Type
1331 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1332 and then Comes_From_Source (Btyp));
1333 end Is_Definite_Access_Type;
1335 ----------------------
1336 -- Is_Predefined_Op --
1337 ----------------------
1339 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1341 -- Predefined operators are intrinsic subprograms
1343 if not Is_Intrinsic_Subprogram (Nam) then
1347 -- A call to a back-end builtin is never a predefined operator
1349 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1353 return not Is_Generic_Instance (Nam)
1354 and then Chars (Nam) in Any_Operator_Name
1355 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1356 end Is_Predefined_Op;
1358 -----------------------------
1359 -- Make_Call_Into_Operator --
1360 -----------------------------
1362 procedure Make_Call_Into_Operator
1367 Op_Name : constant Name_Id := Chars (Op_Id);
1368 Act1 : Node_Id := First_Actual (N);
1369 Act2 : Node_Id := Next_Actual (Act1);
1370 Error : Boolean := False;
1371 Func : constant Entity_Id := Entity (Name (N));
1372 Is_Binary : constant Boolean := Present (Act2);
1374 Opnd_Type : Entity_Id := Empty;
1375 Orig_Type : Entity_Id := Empty;
1378 type Kind_Test is access function (E : Entity_Id) return Boolean;
1380 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1381 -- If the operand is not universal, and the operator is given by an
1382 -- expanded name, verify that the operand has an interpretation with a
1383 -- type defined in the given scope of the operator.
1385 function Type_In_P (Test : Kind_Test) return Entity_Id;
1386 -- Find a type of the given class in package Pack that contains the
1389 ---------------------------
1390 -- Operand_Type_In_Scope --
1391 ---------------------------
1393 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1394 Nod : constant Node_Id := Right_Opnd (Op_Node);
1399 if not Is_Overloaded (Nod) then
1400 return Scope (Base_Type (Etype (Nod))) = S;
1403 Get_First_Interp (Nod, I, It);
1404 while Present (It.Typ) loop
1405 if Scope (Base_Type (It.Typ)) = S then
1409 Get_Next_Interp (I, It);
1414 end Operand_Type_In_Scope;
1420 function Type_In_P (Test : Kind_Test) return Entity_Id is
1423 function In_Decl return Boolean;
1424 -- Verify that node is not part of the type declaration for the
1425 -- candidate type, which would otherwise be invisible.
1431 function In_Decl return Boolean is
1432 Decl_Node : constant Node_Id := Parent (E);
1438 if Etype (E) = Any_Type then
1441 elsif No (Decl_Node) then
1446 and then Nkind (N2) /= N_Compilation_Unit
1448 if N2 = Decl_Node then
1459 -- Start of processing for Type_In_P
1462 -- If the context type is declared in the prefix package, this is the
1463 -- desired base type.
1465 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1466 return Base_Type (Typ);
1469 E := First_Entity (Pack);
1470 while Present (E) loop
1471 if Test (E) and then not In_Decl then
1482 -- Start of processing for Make_Call_Into_Operator
1485 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1487 -- Ensure that the corresponding operator has the same parent as the
1488 -- original call. This guarantees that parent traversals performed by
1489 -- the ABE mechanism succeed.
1491 Set_Parent (Op_Node, Parent (N));
1496 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1497 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1498 Save_Interps (Act1, Left_Opnd (Op_Node));
1499 Save_Interps (Act2, Right_Opnd (Op_Node));
1500 Act1 := Left_Opnd (Op_Node);
1501 Act2 := Right_Opnd (Op_Node);
1506 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1507 Save_Interps (Act1, Right_Opnd (Op_Node));
1508 Act1 := Right_Opnd (Op_Node);
1511 -- If the operator is denoted by an expanded name, and the prefix is
1512 -- not Standard, but the operator is a predefined one whose scope is
1513 -- Standard, then this is an implicit_operator, inserted as an
1514 -- interpretation by the procedure of the same name. This procedure
1515 -- overestimates the presence of implicit operators, because it does
1516 -- not examine the type of the operands. Verify now that the operand
1517 -- type appears in the given scope. If right operand is universal,
1518 -- check the other operand. In the case of concatenation, either
1519 -- argument can be the component type, so check the type of the result.
1520 -- If both arguments are literals, look for a type of the right kind
1521 -- defined in the given scope. This elaborate nonsense is brought to
1522 -- you courtesy of b33302a. The type itself must be frozen, so we must
1523 -- find the type of the proper class in the given scope.
1525 -- A final wrinkle is the multiplication operator for fixed point types,
1526 -- which is defined in Standard only, and not in the scope of the
1527 -- fixed point type itself.
1529 if Nkind (Name (N)) = N_Expanded_Name then
1530 Pack := Entity (Prefix (Name (N)));
1532 -- If this is a package renaming, get renamed entity, which will be
1533 -- the scope of the operands if operaton is type-correct.
1535 if Present (Renamed_Entity (Pack)) then
1536 Pack := Renamed_Entity (Pack);
1539 -- If the entity being called is defined in the given package, it is
1540 -- a renaming of a predefined operator, and known to be legal.
1542 if Scope (Entity (Name (N))) = Pack
1543 and then Pack /= Standard_Standard
1547 -- Visibility does not need to be checked in an instance: if the
1548 -- operator was not visible in the generic it has been diagnosed
1549 -- already, else there is an implicit copy of it in the instance.
1551 elsif In_Instance then
1554 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide
1555 and then Is_Fixed_Point_Type (Etype (Act1))
1556 and then Is_Fixed_Point_Type (Etype (Act2))
1558 if Pack /= Standard_Standard then
1562 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1565 elsif Ada_Version >= Ada_2005
1566 and then Op_Name in Name_Op_Eq | Name_Op_Ne
1567 and then (Is_Anonymous_Access_Type (Etype (Act1))
1568 or else Is_Anonymous_Access_Type (Etype (Act2)))
1573 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1575 if Op_Name = Name_Op_Concat then
1576 Opnd_Type := Base_Type (Typ);
1578 elsif (Scope (Opnd_Type) = Standard_Standard
1580 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1582 and then not Comes_From_Source (Opnd_Type))
1584 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1587 if Scope (Opnd_Type) = Standard_Standard then
1589 -- Verify that the scope contains a type that corresponds to
1590 -- the given literal. Optimize the case where Pack is Standard.
1592 if Pack /= Standard_Standard then
1593 if Opnd_Type = Universal_Integer then
1594 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1596 elsif Opnd_Type = Universal_Real then
1597 Orig_Type := Type_In_P (Is_Real_Type'Access);
1599 elsif Opnd_Type = Any_String then
1600 Orig_Type := Type_In_P (Is_String_Type'Access);
1602 elsif Opnd_Type = Any_Access then
1603 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1605 elsif Opnd_Type = Any_Composite then
1606 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1608 if Present (Orig_Type) then
1609 if Has_Private_Component (Orig_Type) then
1612 Set_Etype (Act1, Orig_Type);
1615 Set_Etype (Act2, Orig_Type);
1624 Error := No (Orig_Type);
1627 elsif Ekind (Opnd_Type) = E_Allocator_Type
1628 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1632 -- If the type is defined elsewhere, and the operator is not
1633 -- defined in the given scope (by a renaming declaration, e.g.)
1634 -- then this is an error as well. If an extension of System is
1635 -- present, and the type may be defined there, Pack must be
1638 elsif Scope (Opnd_Type) /= Pack
1639 and then Scope (Op_Id) /= Pack
1640 and then (No (System_Aux_Id)
1641 or else Scope (Opnd_Type) /= System_Aux_Id
1642 or else Pack /= Scope (System_Aux_Id))
1644 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1647 Error := not Operand_Type_In_Scope (Pack);
1650 elsif Pack = Standard_Standard
1651 and then not Operand_Type_In_Scope (Standard_Standard)
1658 Error_Msg_Node_2 := Pack;
1660 ("& not declared in&", N, Selector_Name (Name (N)));
1661 Set_Etype (N, Any_Type);
1664 -- Detect a mismatch between the context type and the result type
1665 -- in the named package, which is otherwise not detected if the
1666 -- operands are universal. Check is only needed if source entity is
1667 -- an operator, not a function that renames an operator.
1669 elsif Nkind (Parent (N)) /= N_Type_Conversion
1670 and then Ekind (Entity (Name (N))) = E_Operator
1671 and then Is_Numeric_Type (Typ)
1672 and then not Is_Universal_Numeric_Type (Typ)
1673 and then Scope (Base_Type (Typ)) /= Pack
1674 and then not In_Instance
1676 if Is_Fixed_Point_Type (Typ)
1677 and then Op_Name in Name_Op_Multiply | Name_Op_Divide
1679 -- Already checked above
1683 -- Operator may be defined in an extension of System
1685 elsif Present (System_Aux_Id)
1686 and then Present (Opnd_Type)
1687 and then Scope (Opnd_Type) = System_Aux_Id
1692 -- Could we use Wrong_Type here??? (this would require setting
1693 -- Etype (N) to the actual type found where Typ was expected).
1695 Error_Msg_NE ("expect }", N, Typ);
1700 Set_Chars (Op_Node, Op_Name);
1702 if not Is_Private_Type (Etype (N)) then
1703 Set_Etype (Op_Node, Base_Type (Etype (N)));
1705 Set_Etype (Op_Node, Etype (N));
1708 -- If this is a call to a function that renames a predefined equality,
1709 -- the renaming declaration provides a type that must be used to
1710 -- resolve the operands. This must be done now because resolution of
1711 -- the equality node will not resolve any remaining ambiguity, and it
1712 -- assumes that the first operand is not overloaded.
1714 if Op_Name in Name_Op_Eq | Name_Op_Ne
1715 and then Ekind (Func) = E_Function
1716 and then Is_Overloaded (Act1)
1718 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1719 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1722 Set_Entity (Op_Node, Op_Id);
1723 Generate_Reference (Op_Id, N, ' ');
1725 -- Do rewrite setting Comes_From_Source on the result if the original
1726 -- call came from source. Although it is not strictly the case that the
1727 -- operator as such comes from the source, logically it corresponds
1728 -- exactly to the function call in the source, so it should be marked
1729 -- this way (e.g. to make sure that validity checks work fine).
1732 CS : constant Boolean := Comes_From_Source (N);
1734 Rewrite (N, Op_Node);
1735 Set_Comes_From_Source (N, CS);
1738 -- If this is an arithmetic operator and the result type is private,
1739 -- the operands and the result must be wrapped in conversion to
1740 -- expose the underlying numeric type and expand the proper checks,
1741 -- e.g. on division.
1743 if Is_Private_Type (Typ) then
1753 Resolve_Intrinsic_Operator (N, Typ);
1759 Resolve_Intrinsic_Unary_Operator (N, Typ);
1767 end Make_Call_Into_Operator;
1773 function Operator_Kind
1775 Is_Binary : Boolean) return Node_Kind
1780 -- Use CASE statement or array???
1783 if Op_Name = Name_Op_And then
1785 elsif Op_Name = Name_Op_Or then
1787 elsif Op_Name = Name_Op_Xor then
1789 elsif Op_Name = Name_Op_Eq then
1791 elsif Op_Name = Name_Op_Ne then
1793 elsif Op_Name = Name_Op_Lt then
1795 elsif Op_Name = Name_Op_Le then
1797 elsif Op_Name = Name_Op_Gt then
1799 elsif Op_Name = Name_Op_Ge then
1801 elsif Op_Name = Name_Op_Add then
1803 elsif Op_Name = Name_Op_Subtract then
1804 Kind := N_Op_Subtract;
1805 elsif Op_Name = Name_Op_Concat then
1806 Kind := N_Op_Concat;
1807 elsif Op_Name = Name_Op_Multiply then
1808 Kind := N_Op_Multiply;
1809 elsif Op_Name = Name_Op_Divide then
1810 Kind := N_Op_Divide;
1811 elsif Op_Name = Name_Op_Mod then
1813 elsif Op_Name = Name_Op_Rem then
1815 elsif Op_Name = Name_Op_Expon then
1818 raise Program_Error;
1824 if Op_Name = Name_Op_Add then
1826 elsif Op_Name = Name_Op_Subtract then
1828 elsif Op_Name = Name_Op_Abs then
1830 elsif Op_Name = Name_Op_Not then
1833 raise Program_Error;
1840 ----------------------------
1841 -- Preanalyze_And_Resolve --
1842 ----------------------------
1844 procedure Preanalyze_And_Resolve
1847 With_Freezing : Boolean)
1849 Save_Full_Analysis : constant Boolean := Full_Analysis;
1850 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
1851 Save_Preanalysis_Count : constant Nat :=
1852 Inside_Preanalysis_Without_Freezing;
1854 pragma Assert (Nkind (N) in N_Subexpr);
1856 if not With_Freezing then
1857 Set_Must_Not_Freeze (N);
1858 Inside_Preanalysis_Without_Freezing :=
1859 Inside_Preanalysis_Without_Freezing + 1;
1862 Full_Analysis := False;
1863 Expander_Mode_Save_And_Set (False);
1865 -- Normally, we suppress all checks for this preanalysis. There is no
1866 -- point in processing them now, since they will be applied properly
1867 -- and in the proper location when the default expressions reanalyzed
1868 -- and reexpanded later on. We will also have more information at that
1869 -- point for possible suppression of individual checks.
1871 -- However, in SPARK mode, most expansion is suppressed, and this
1872 -- later reanalysis and reexpansion may not occur. SPARK mode does
1873 -- require the setting of checking flags for proof purposes, so we
1874 -- do the SPARK preanalysis without suppressing checks.
1876 -- This special handling for SPARK mode is required for example in the
1877 -- case of Ada 2012 constructs such as quantified expressions, which are
1878 -- expanded in two separate steps.
1880 if GNATprove_Mode then
1881 Analyze_And_Resolve (N, T);
1883 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1886 Expander_Mode_Restore;
1887 Full_Analysis := Save_Full_Analysis;
1888 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
1890 if not With_Freezing then
1891 Inside_Preanalysis_Without_Freezing :=
1892 Inside_Preanalysis_Without_Freezing - 1;
1896 (Inside_Preanalysis_Without_Freezing = Save_Preanalysis_Count);
1897 end Preanalyze_And_Resolve;
1899 ----------------------------
1900 -- Preanalyze_And_Resolve --
1901 ----------------------------
1903 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1905 Preanalyze_And_Resolve (N, T, With_Freezing => False);
1906 end Preanalyze_And_Resolve;
1908 -- Version without context type
1910 procedure Preanalyze_And_Resolve (N : Node_Id) is
1911 Save_Full_Analysis : constant Boolean := Full_Analysis;
1914 Full_Analysis := False;
1915 Expander_Mode_Save_And_Set (False);
1918 Resolve (N, Etype (N), Suppress => All_Checks);
1920 Expander_Mode_Restore;
1921 Full_Analysis := Save_Full_Analysis;
1922 end Preanalyze_And_Resolve;
1924 ------------------------------------------
1925 -- Preanalyze_With_Freezing_And_Resolve --
1926 ------------------------------------------
1928 procedure Preanalyze_With_Freezing_And_Resolve
1933 Preanalyze_And_Resolve (N, T, With_Freezing => True);
1934 end Preanalyze_With_Freezing_And_Resolve;
1936 ----------------------------------
1937 -- Replace_Actual_Discriminants --
1938 ----------------------------------
1940 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1941 Loc : constant Source_Ptr := Sloc (N);
1942 Tsk : Node_Id := Empty;
1944 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1945 -- Comment needed???
1951 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1955 if Nkind (Nod) = N_Identifier then
1956 Ent := Entity (Nod);
1959 and then Ekind (Ent) = E_Discriminant
1962 Make_Selected_Component (Loc,
1963 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1964 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1966 Set_Etype (Nod, Etype (Ent));
1974 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1976 -- Start of processing for Replace_Actual_Discriminants
1979 if Expander_Active then
1982 -- Allow the replacement of concurrent discriminants in GNATprove even
1983 -- though this is a light expansion activity. Note that generic units
1984 -- are not modified.
1986 elsif GNATprove_Mode and not Inside_A_Generic then
1993 if Nkind (Name (N)) = N_Selected_Component then
1994 Tsk := Prefix (Name (N));
1996 elsif Nkind (Name (N)) = N_Indexed_Component then
1997 Tsk := Prefix (Prefix (Name (N)));
2000 if Present (Tsk) then
2001 Replace_Discrs (Default);
2003 end Replace_Actual_Discriminants;
2009 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2010 Ambiguous : Boolean := False;
2011 Ctx_Type : Entity_Id := Typ;
2012 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2013 Err_Type : Entity_Id := Empty;
2014 Found : Boolean := False;
2017 I1 : Interp_Index := 0; -- prevent junk warning
2020 Seen : Entity_Id := Empty; -- prevent junk warning
2022 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2023 -- Determine whether a node comes from a predefined library unit or
2026 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2027 -- Try and fix up a literal so that it matches its expected type. New
2028 -- literals are manufactured if necessary to avoid cascaded errors.
2030 procedure Report_Ambiguous_Argument;
2031 -- Additional diagnostics when an ambiguous call has an ambiguous
2032 -- argument (typically a controlling actual).
2034 procedure Resolution_Failed;
2035 -- Called when attempt at resolving current expression fails
2037 ------------------------------------
2038 -- Comes_From_Predefined_Lib_Unit --
2039 -------------------------------------
2041 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2044 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2045 end Comes_From_Predefined_Lib_Unit;
2047 --------------------
2048 -- Patch_Up_Value --
2049 --------------------
2051 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2053 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2055 Make_Real_Literal (Sloc (N),
2056 Realval => UR_From_Uint (Intval (N))));
2057 Set_Etype (N, Universal_Real);
2058 Set_Is_Static_Expression (N);
2060 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2062 Make_Integer_Literal (Sloc (N),
2063 Intval => UR_To_Uint (Realval (N))));
2064 Set_Etype (N, Universal_Integer);
2065 Set_Is_Static_Expression (N);
2067 elsif Nkind (N) = N_String_Literal
2068 and then Is_Character_Type (Typ)
2070 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
2072 Make_Character_Literal (Sloc (N),
2074 Char_Literal_Value =>
2075 UI_From_Int (Character'Pos ('A'))));
2076 Set_Etype (N, Any_Character);
2077 Set_Is_Static_Expression (N);
2079 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2081 Make_String_Literal (Sloc (N),
2082 Strval => End_String));
2084 elsif Nkind (N) = N_Range then
2085 Patch_Up_Value (Low_Bound (N), Typ);
2086 Patch_Up_Value (High_Bound (N), Typ);
2090 -------------------------------
2091 -- Report_Ambiguous_Argument --
2092 -------------------------------
2094 procedure Report_Ambiguous_Argument is
2095 Arg : constant Node_Id := First (Parameter_Associations (N));
2100 if Nkind (Arg) = N_Function_Call
2101 and then Is_Entity_Name (Name (Arg))
2102 and then Is_Overloaded (Name (Arg))
2104 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2106 -- Examine possible interpretations, and adapt the message
2107 -- for inherited subprograms declared by a type derivation.
2109 Get_First_Interp (Name (Arg), I, It);
2110 while Present (It.Nam) loop
2111 Error_Msg_Sloc := Sloc (It.Nam);
2113 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
2114 Error_Msg_N ("interpretation (inherited) #!", Arg);
2116 Error_Msg_N ("interpretation #!", Arg);
2119 Get_Next_Interp (I, It);
2123 -- Additional message and hint if the ambiguity involves an Ada2020
2124 -- container aggregate.
2126 Check_Ambiguous_Aggregate (N);
2127 end Report_Ambiguous_Argument;
2129 -----------------------
2130 -- Resolution_Failed --
2131 -----------------------
2133 procedure Resolution_Failed is
2135 Patch_Up_Value (N, Typ);
2137 -- Set the type to the desired one to minimize cascaded errors. Note
2138 -- that this is an approximation and does not work in all cases.
2142 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2143 Set_Is_Overloaded (N, False);
2145 -- The caller will return without calling the expander, so we need
2146 -- to set the analyzed flag. Note that it is fine to set Analyzed
2147 -- to True even if we are in the middle of a shallow analysis,
2148 -- (see the spec of sem for more details) since this is an error
2149 -- situation anyway, and there is no point in repeating the
2150 -- analysis later (indeed it won't work to repeat it later, since
2151 -- we haven't got a clear resolution of which entity is being
2154 Set_Analyzed (N, True);
2156 end Resolution_Failed;
2158 Literal_Aspect_Map :
2159 constant array (N_Numeric_Or_String_Literal) of Aspect_Id :=
2160 (N_Integer_Literal => Aspect_Integer_Literal,
2161 N_Real_Literal => Aspect_Real_Literal,
2162 N_String_Literal => Aspect_String_Literal);
2164 Named_Number_Aspect_Map : constant array (Named_Kind) of Aspect_Id :=
2165 (E_Named_Integer => Aspect_Integer_Literal,
2166 E_Named_Real => Aspect_Real_Literal);
2168 -- Start of processing for Resolve
2175 -- Access attribute on remote subprogram cannot be used for a non-remote
2176 -- access-to-subprogram type.
2178 if Nkind (N) = N_Attribute_Reference
2179 and then Attribute_Name (N) in Name_Access
2180 | Name_Unrestricted_Access
2181 | Name_Unchecked_Access
2182 and then Comes_From_Source (N)
2183 and then Is_Entity_Name (Prefix (N))
2184 and then Is_Subprogram (Entity (Prefix (N)))
2185 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2186 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2189 ("prefix must statically denote a non-remote subprogram", N);
2192 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2194 -- If the context is a Remote_Access_To_Subprogram, access attributes
2195 -- must be resolved with the corresponding fat pointer. There is no need
2196 -- to check for the attribute name since the return type of an
2197 -- attribute is never a remote type.
2199 if Nkind (N) = N_Attribute_Reference
2200 and then Comes_From_Source (N)
2201 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2204 Attr : constant Attribute_Id :=
2205 Get_Attribute_Id (Attribute_Name (N));
2206 Pref : constant Node_Id := Prefix (N);
2209 Is_Remote : Boolean := True;
2212 -- Check that Typ is a remote access-to-subprogram type
2214 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2216 -- Prefix (N) must statically denote a remote subprogram
2217 -- declared in a package specification.
2219 if Attr = Attribute_Access or else
2220 Attr = Attribute_Unchecked_Access or else
2221 Attr = Attribute_Unrestricted_Access
2223 Decl := Unit_Declaration_Node (Entity (Pref));
2225 if Nkind (Decl) = N_Subprogram_Body then
2226 Spec := Corresponding_Spec (Decl);
2228 if Present (Spec) then
2229 Decl := Unit_Declaration_Node (Spec);
2233 Spec := Parent (Decl);
2235 if not Is_Entity_Name (Prefix (N))
2236 or else Nkind (Spec) /= N_Package_Specification
2238 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2242 ("prefix must statically denote a remote subprogram",
2246 -- If we are generating code in distributed mode, perform
2247 -- semantic checks against corresponding remote entities.
2250 and then Get_PCS_Name /= Name_No_DSA
2252 Check_Subtype_Conformant
2253 (New_Id => Entity (Prefix (N)),
2254 Old_Id => Designated_Type
2255 (Corresponding_Remote_Type (Typ)),
2259 Process_Remote_AST_Attribute (N, Typ);
2267 Debug_A_Entry ("resolving ", N);
2269 if Debug_Flag_V then
2270 Write_Overloads (N);
2273 if Comes_From_Source (N) then
2274 if Is_Fixed_Point_Type (Typ) then
2275 Check_Restriction (No_Fixed_Point, N);
2277 elsif Is_Floating_Point_Type (Typ)
2278 and then Typ /= Universal_Real
2279 and then Typ /= Any_Real
2281 Check_Restriction (No_Floating_Point, N);
2285 -- Return if already analyzed
2287 if Analyzed (N) then
2288 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2289 Analyze_Dimension (N);
2292 -- Any case of Any_Type as the Etype value means that we had a
2295 elsif Etype (N) = Any_Type then
2296 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2300 Check_Parameterless_Call (N);
2302 -- The resolution of an Expression_With_Actions is determined by
2303 -- its Expression, but if the node comes from source it is a
2304 -- Declare_Expression and requires scope management.
2306 if Nkind (N) = N_Expression_With_Actions then
2307 if Comes_From_Source (N) and then N = Original_Node (N) then
2308 Resolve_Declare_Expression (N, Typ);
2310 Resolve (Expression (N), Typ);
2314 Expr_Type := Etype (Expression (N));
2316 -- If not overloaded, then we know the type, and all that needs doing
2317 -- is to check that this type is compatible with the context.
2319 elsif not Is_Overloaded (N) then
2320 Found := Covers (Typ, Etype (N));
2321 Expr_Type := Etype (N);
2323 -- In the overloaded case, we must select the interpretation that
2324 -- is compatible with the context (i.e. the type passed to Resolve)
2327 -- Loop through possible interpretations
2329 Get_First_Interp (N, I, It);
2330 Interp_Loop : while Present (It.Typ) loop
2331 if Debug_Flag_V then
2332 Write_Str ("Interp: ");
2336 -- We are only interested in interpretations that are compatible
2337 -- with the expected type, any other interpretations are ignored.
2339 if not Covers (Typ, It.Typ) then
2340 if Debug_Flag_V then
2341 Write_Str (" interpretation incompatible with context");
2346 -- Skip the current interpretation if it is disabled by an
2347 -- abstract operator. This action is performed only when the
2348 -- type against which we are resolving is the same as the
2349 -- type of the interpretation.
2351 if Ada_Version >= Ada_2005
2352 and then It.Typ = Typ
2353 and then not Is_Universal_Numeric_Type (Typ)
2354 and then Present (It.Abstract_Op)
2356 if Debug_Flag_V then
2357 Write_Line ("Skip.");
2363 -- First matching interpretation
2369 Expr_Type := It.Typ;
2371 -- Matching interpretation that is not the first, maybe an
2372 -- error, but there are some cases where preference rules are
2373 -- used to choose between the two possibilities. These and
2374 -- some more obscure cases are handled in Disambiguate.
2377 -- If the current statement is part of a predefined library
2378 -- unit, then all interpretations which come from user level
2379 -- packages should not be considered. Check previous and
2383 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2386 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2388 -- Previous interpretation must be discarded
2392 Expr_Type := It.Typ;
2393 Set_Entity (N, Seen);
2398 -- Otherwise apply further disambiguation steps
2400 Error_Msg_Sloc := Sloc (Seen);
2401 It1 := Disambiguate (N, I1, I, Typ);
2403 -- Disambiguation has succeeded. Skip the remaining
2406 if It1 /= No_Interp then
2408 Expr_Type := It1.Typ;
2410 while Present (It.Typ) loop
2411 Get_Next_Interp (I, It);
2415 -- Before we issue an ambiguity complaint, check for the
2416 -- case of a subprogram call where at least one of the
2417 -- arguments is Any_Type, and if so suppress the message,
2418 -- since it is a cascaded error. This can also happen for
2419 -- a generalized indexing operation.
2421 if Nkind (N) in N_Subprogram_Call
2422 or else (Nkind (N) = N_Indexed_Component
2423 and then Present (Generalized_Indexing (N)))
2430 if Nkind (N) = N_Indexed_Component then
2431 Rewrite (N, Generalized_Indexing (N));
2434 A := First_Actual (N);
2435 while Present (A) loop
2438 if Nkind (E) = N_Parameter_Association then
2439 E := Explicit_Actual_Parameter (E);
2442 if Etype (E) = Any_Type then
2443 if Debug_Flag_V then
2444 Write_Str ("Any_Type in call");
2455 elsif Nkind (N) in N_Binary_Op
2456 and then (Etype (Left_Opnd (N)) = Any_Type
2457 or else Etype (Right_Opnd (N)) = Any_Type)
2461 elsif Nkind (N) in N_Unary_Op
2462 and then Etype (Right_Opnd (N)) = Any_Type
2467 -- Not that special case, so issue message using the flag
2468 -- Ambiguous to control printing of the header message
2469 -- only at the start of an ambiguous set.
2471 if not Ambiguous then
2472 if Nkind (N) = N_Function_Call
2473 and then Nkind (Name (N)) = N_Explicit_Dereference
2476 ("ambiguous expression (cannot resolve indirect "
2479 Error_Msg_NE -- CODEFIX
2480 ("ambiguous expression (cannot resolve&)!",
2486 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2488 ("\\possible interpretation (inherited)#!", N);
2490 Error_Msg_N -- CODEFIX
2491 ("\\possible interpretation#!", N);
2494 if Nkind (N) in N_Subprogram_Call
2495 and then Present (Parameter_Associations (N))
2497 Report_Ambiguous_Argument;
2501 Error_Msg_Sloc := Sloc (It.Nam);
2503 -- By default, the error message refers to the candidate
2504 -- interpretation. But if it is a predefined operator, it
2505 -- is implicitly declared at the declaration of the type
2506 -- of the operand. Recover the sloc of that declaration
2507 -- for the error message.
2509 if Nkind (N) in N_Op
2510 and then Scope (It.Nam) = Standard_Standard
2511 and then not Is_Overloaded (Right_Opnd (N))
2512 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2515 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2517 if Comes_From_Source (Err_Type)
2518 and then Present (Parent (Err_Type))
2520 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2523 elsif Nkind (N) in N_Binary_Op
2524 and then Scope (It.Nam) = Standard_Standard
2525 and then not Is_Overloaded (Left_Opnd (N))
2526 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2529 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2531 if Comes_From_Source (Err_Type)
2532 and then Present (Parent (Err_Type))
2534 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2537 -- If this is an indirect call, use the subprogram_type
2538 -- in the message, to have a meaningful location. Also
2539 -- indicate if this is an inherited operation, created
2540 -- by a type declaration.
2542 elsif Nkind (N) = N_Function_Call
2543 and then Nkind (Name (N)) = N_Explicit_Dereference
2544 and then Is_Type (It.Nam)
2548 Sloc (Associated_Node_For_Itype (Err_Type));
2553 if Nkind (N) in N_Op
2554 and then Scope (It.Nam) = Standard_Standard
2555 and then Present (Err_Type)
2557 -- Special-case the message for universal_fixed
2558 -- operators, which are not declared with the type
2559 -- of the operand, but appear forever in Standard.
2561 if It.Typ = Universal_Fixed
2562 and then Scope (It.Nam) = Standard_Standard
2565 ("\\possible interpretation as universal_fixed "
2566 & "operation (RM 4.5.5 (19))", N);
2569 ("\\possible interpretation (predefined)#!", N);
2573 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2576 ("\\possible interpretation (inherited)#!", N);
2578 Error_Msg_N -- CODEFIX
2579 ("\\possible interpretation#!", N);
2585 -- We have a matching interpretation, Expr_Type is the type
2586 -- from this interpretation, and Seen is the entity.
2588 -- For an operator, just set the entity name. The type will be
2589 -- set by the specific operator resolution routine.
2591 if Nkind (N) in N_Op then
2592 Set_Entity (N, Seen);
2593 Generate_Reference (Seen, N);
2595 elsif Nkind (N) in N_Case_Expression
2596 | N_Character_Literal
2600 Set_Etype (N, Expr_Type);
2602 -- AI05-0139-2: Expression is overloaded because type has
2603 -- implicit dereference. The context may be the one that
2604 -- requires implicit dereferemce.
2606 elsif Has_Implicit_Dereference (Expr_Type) then
2607 Set_Etype (N, Expr_Type);
2608 Set_Is_Overloaded (N, False);
2610 -- If the expression is an entity, generate a reference
2611 -- to it, as this is not done for an overloaded construct
2614 if Is_Entity_Name (N)
2615 and then Comes_From_Source (N)
2617 Generate_Reference (Entity (N), N);
2619 -- Examine access discriminants of entity type,
2620 -- to check whether one of them yields the
2625 First_Discriminant (Etype (Entity (N)));
2628 while Present (Disc) loop
2629 exit when Is_Access_Type (Etype (Disc))
2630 and then Has_Implicit_Dereference (Disc)
2631 and then Designated_Type (Etype (Disc)) = Typ;
2633 Next_Discriminant (Disc);
2636 if Present (Disc) then
2637 Build_Explicit_Dereference (N, Disc);
2644 elsif Is_Overloaded (N)
2645 and then Present (It.Nam)
2646 and then Ekind (It.Nam) = E_Discriminant
2647 and then Has_Implicit_Dereference (It.Nam)
2649 -- If the node is a general indexing, the dereference is
2650 -- is inserted when resolving the rewritten form, else
2653 if Nkind (N) /= N_Indexed_Component
2654 or else No (Generalized_Indexing (N))
2656 Build_Explicit_Dereference (N, It.Nam);
2659 -- For an explicit dereference, attribute reference, range,
2660 -- short-circuit form (which is not an operator node), or call
2661 -- with a name that is an explicit dereference, there is
2662 -- nothing to be done at this point.
2664 elsif Nkind (N) in N_Attribute_Reference
2666 | N_Explicit_Dereference
2668 | N_Indexed_Component
2671 | N_Selected_Component
2673 or else Nkind (Name (N)) = N_Explicit_Dereference
2677 -- For procedure or function calls, set the type of the name,
2678 -- and also the entity pointer for the prefix.
2680 elsif Nkind (N) in N_Subprogram_Call
2681 and then Is_Entity_Name (Name (N))
2683 Set_Etype (Name (N), Expr_Type);
2684 Set_Entity (Name (N), Seen);
2685 Generate_Reference (Seen, Name (N));
2687 elsif Nkind (N) = N_Function_Call
2688 and then Nkind (Name (N)) = N_Selected_Component
2690 Set_Etype (Name (N), Expr_Type);
2691 Set_Entity (Selector_Name (Name (N)), Seen);
2692 Generate_Reference (Seen, Selector_Name (Name (N)));
2694 -- For all other cases, just set the type of the Name
2697 Set_Etype (Name (N), Expr_Type);
2704 -- Move to next interpretation
2706 exit Interp_Loop when No (It.Typ);
2708 Get_Next_Interp (I, It);
2709 end loop Interp_Loop;
2712 -- At this stage Found indicates whether or not an acceptable
2713 -- interpretation exists. If not, then we have an error, except that if
2714 -- the context is Any_Type as a result of some other error, then we
2715 -- suppress the error report.
2718 if Typ /= Any_Type then
2720 -- If type we are looking for is Void, then this is the procedure
2721 -- call case, and the error is simply that what we gave is not a
2722 -- procedure name (we think of procedure calls as expressions with
2723 -- types internally, but the user doesn't think of them this way).
2725 if Typ = Standard_Void_Type then
2727 -- Special case message if function used as a procedure
2729 if Nkind (N) = N_Procedure_Call_Statement
2730 and then Is_Entity_Name (Name (N))
2731 and then Ekind (Entity (Name (N))) = E_Function
2734 ("cannot use call to function & as a statement",
2735 Name (N), Entity (Name (N)));
2737 ("\return value of a function call cannot be ignored",
2740 -- Otherwise give general message (not clear what cases this
2741 -- covers, but no harm in providing for them).
2744 Error_Msg_N ("expect procedure name in procedure call", N);
2749 -- Otherwise we do have a subexpression with the wrong type
2751 -- Check for the case of an allocator which uses an access type
2752 -- instead of the designated type. This is a common error and we
2753 -- specialize the message, posting an error on the operand of the
2754 -- allocator, complaining that we expected the designated type of
2757 elsif Nkind (N) = N_Allocator
2758 and then Is_Access_Type (Typ)
2759 and then Is_Access_Type (Etype (N))
2760 and then Designated_Type (Etype (N)) = Typ
2762 Wrong_Type (Expression (N), Designated_Type (Typ));
2765 -- Check for view mismatch on Null in instances, for which the
2766 -- view-swapping mechanism has no identifier.
2768 elsif (In_Instance or else In_Inlined_Body)
2769 and then (Nkind (N) = N_Null)
2770 and then Is_Private_Type (Typ)
2771 and then Is_Access_Type (Full_View (Typ))
2773 Resolve (N, Full_View (Typ));
2777 -- Check for an aggregate. Sometimes we can get bogus aggregates
2778 -- from misuse of parentheses, and we are about to complain about
2779 -- the aggregate without even looking inside it.
2781 -- Instead, if we have an aggregate of type Any_Composite, then
2782 -- analyze and resolve the component fields, and then only issue
2783 -- another message if we get no errors doing this (otherwise
2784 -- assume that the errors in the aggregate caused the problem).
2786 elsif Nkind (N) = N_Aggregate
2787 and then Etype (N) = Any_Composite
2789 if Ada_Version >= Ada_2020
2790 and then Has_Aspect (Typ, Aspect_Aggregate)
2792 Resolve_Container_Aggregate (N, Typ);
2794 if Expander_Active then
2800 -- Disable expansion in any case. If there is a type mismatch
2801 -- it may be fatal to try to expand the aggregate. The flag
2802 -- would otherwise be set to false when the error is posted.
2804 Expander_Active := False;
2807 procedure Check_Aggr (Aggr : Node_Id);
2808 -- Check one aggregate, and set Found to True if we have a
2809 -- definite error in any of its elements
2811 procedure Check_Elmt (Aelmt : Node_Id);
2812 -- Check one element of aggregate and set Found to True if
2813 -- we definitely have an error in the element.
2819 procedure Check_Aggr (Aggr : Node_Id) is
2823 if Present (Expressions (Aggr)) then
2824 Elmt := First (Expressions (Aggr));
2825 while Present (Elmt) loop
2831 if Present (Component_Associations (Aggr)) then
2832 Elmt := First (Component_Associations (Aggr));
2833 while Present (Elmt) loop
2835 -- If this is a default-initialized component, then
2836 -- there is nothing to check. The box will be
2837 -- replaced by the appropriate call during late
2840 if Nkind (Elmt) /= N_Iterated_Component_Association
2841 and then not Box_Present (Elmt)
2843 Check_Elmt (Expression (Elmt));
2855 procedure Check_Elmt (Aelmt : Node_Id) is
2857 -- If we have a nested aggregate, go inside it (to
2858 -- attempt a naked analyze-resolve of the aggregate can
2859 -- cause undesirable cascaded errors). Do not resolve
2860 -- expression if it needs a type from context, as for
2861 -- integer * fixed expression.
2863 if Nkind (Aelmt) = N_Aggregate then
2869 if not Is_Overloaded (Aelmt)
2870 and then Etype (Aelmt) /= Any_Fixed
2875 if Etype (Aelmt) = Any_Type then
2886 -- Rewrite Literal as a call if the corresponding literal aspect
2889 if (Nkind (N) in N_Numeric_Or_String_Literal
2892 (Find_Aspect (Typ, Literal_Aspect_Map (Nkind (N)))))
2894 (Nkind (N) = N_Identifier
2895 and then Is_Named_Number (Entity (N))
2899 (Typ, Named_Number_Aspect_Map (Ekind (Entity (N))))))
2902 Lit_Aspect : constant Aspect_Id :=
2903 (if Nkind (N) = N_Identifier
2904 then Named_Number_Aspect_Map (Ekind (Entity (N)))
2905 else Literal_Aspect_Map (Nkind (N)));
2907 Loc : constant Source_Ptr := Sloc (N);
2909 Callee : Entity_Id :=
2910 Entity (Expression (Find_Aspect (Typ, Lit_Aspect)));
2912 Name : constant Node_Id :=
2913 Make_Identifier (Loc, Chars (Callee));
2922 if Nkind (N) = N_Identifier then
2923 Expr := Expression (Declaration_Node (Entity (N)));
2925 if Ekind (Entity (N)) = E_Named_Integer then
2926 UI_Image (Expr_Value (Expr), Decimal);
2929 (UI_Image_Buffer (1 .. UI_Image_Length));
2930 Param1 := Make_String_Literal (Loc, End_String);
2931 Params := New_List (Param1);
2934 UI_Image (Norm_Num (Expr_Value_R (Expr)), Decimal);
2937 (UI_Image_Buffer (1 .. UI_Image_Length));
2938 Param1 := Make_String_Literal (Loc, End_String);
2940 -- Note: Set_Etype is called below on Param1
2942 UI_Image (Norm_Den (Expr_Value_R (Expr)), Decimal);
2945 (UI_Image_Buffer (1 .. UI_Image_Length));
2946 Param2 := Make_String_Literal (Loc, End_String);
2947 Set_Etype (Param2, Standard_String);
2949 Params := New_List (Param1, Param2);
2951 if Present (Related_Expression (Callee)) then
2952 Callee := Related_Expression (Callee);
2955 ("cannot resolve & for a named real", N, Callee);
2960 elsif Nkind (N) = N_String_Literal then
2961 Param1 := Make_String_Literal (Loc, Strval (N));
2962 Params := New_List (Param1);
2966 (Loc, String_From_Numeric_Literal (N));
2967 Params := New_List (Param1);
2974 Parameter_Associations => Params);
2976 Set_Entity (Name, Callee);
2977 Set_Is_Overloaded (Name, False);
2979 if Lit_Aspect = Aspect_String_Literal then
2980 Set_Etype (Param1, Standard_Wide_Wide_String);
2982 Set_Etype (Param1, Standard_String);
2985 Set_Etype (Call, Etype (Callee));
2987 -- Conversion needed in case of an inherited aspect
2988 -- of a derived type.
2990 -- ??? Need to do something different here for downward
2991 -- tagged conversion case (which is only possible in the
2992 -- case of a null extension); the current call to
2993 -- Convert_To results in an error message about an illegal
2994 -- downward conversion.
2996 Call := Convert_To (Typ, Call);
3001 Analyze_And_Resolve (N, Typ);
3005 -- Looks like we have a type error, but check for special case
3006 -- of Address wanted, integer found, with the configuration pragma
3007 -- Allow_Integer_Address active. If we have this case, introduce
3008 -- an unchecked conversion to allow the integer expression to be
3009 -- treated as an Address. The reverse case of integer wanted,
3010 -- Address found, is treated in an analogous manner.
3012 if Address_Integer_Convert_OK (Typ, Etype (N)) then
3013 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
3014 Analyze_And_Resolve (N, Typ);
3017 -- Under relaxed RM semantics silently replace occurrences of null
3018 -- by System.Null_Address.
3020 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
3021 Replace_Null_By_Null_Address (N);
3022 Analyze_And_Resolve (N, Typ);
3026 -- That special Allow_Integer_Address check did not apply, so we
3027 -- have a real type error. If an error message was issued already,
3028 -- Found got reset to True, so if it's still False, issue standard
3029 -- Wrong_Type message.
3032 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
3034 Subp_Name : Node_Id;
3037 if Is_Entity_Name (Name (N)) then
3038 Subp_Name := Name (N);
3040 elsif Nkind (Name (N)) = N_Selected_Component then
3042 -- Protected operation: retrieve operation name
3044 Subp_Name := Selector_Name (Name (N));
3047 raise Program_Error;
3050 Error_Msg_Node_2 := Typ;
3052 ("no visible interpretation of& matches expected type&",
3056 if All_Errors_Mode then
3058 Index : Interp_Index;
3062 Error_Msg_N ("\\possible interpretations:", N);
3064 Get_First_Interp (Name (N), Index, It);
3065 while Present (It.Nam) loop
3066 Error_Msg_Sloc := Sloc (It.Nam);
3067 Error_Msg_Node_2 := It.Nam;
3069 ("\\ type& for & declared#", N, It.Typ);
3070 Get_Next_Interp (Index, It);
3075 Error_Msg_N ("\use -gnatf for details", N);
3079 Wrong_Type (N, Typ);
3087 -- Test if we have more than one interpretation for the context
3089 elsif Ambiguous then
3093 -- Only one interpretation
3096 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3097 -- the "+" on T is abstract, and the operands are of universal type,
3098 -- the above code will have (incorrectly) resolved the "+" to the
3099 -- universal one in Standard. Therefore check for this case and give
3100 -- an error. We can't do this earlier, because it would cause legal
3101 -- cases to get errors (when some other type has an abstract "+").
3103 if Ada_Version >= Ada_2005
3104 and then Nkind (N) in N_Op
3105 and then Is_Overloaded (N)
3106 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3108 Get_First_Interp (N, I, It);
3109 while Present (It.Typ) loop
3110 if Present (It.Abstract_Op) and then
3111 Etype (It.Abstract_Op) = Typ
3114 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
3118 Get_Next_Interp (I, It);
3122 -- Here we have an acceptable interpretation for the context
3124 -- Propagate type information and normalize tree for various
3125 -- predefined operations. If the context only imposes a class of
3126 -- types, rather than a specific type, propagate the actual type
3129 if Typ = Any_Integer or else
3130 Typ = Any_Boolean or else
3131 Typ = Any_Modular or else
3132 Typ = Any_Real or else
3135 Ctx_Type := Expr_Type;
3137 -- Any_Fixed is legal in a real context only if a specific fixed-
3138 -- point type is imposed. If Norman Cohen can be confused by this,
3139 -- it deserves a separate message.
3142 and then Expr_Type = Any_Fixed
3144 Error_Msg_N ("illegal context for mixed mode operation", N);
3145 Set_Etype (N, Universal_Real);
3146 Ctx_Type := Universal_Real;
3150 -- A user-defined operator is transformed into a function call at
3151 -- this point, so that further processing knows that operators are
3152 -- really operators (i.e. are predefined operators). User-defined
3153 -- operators that are intrinsic are just renamings of the predefined
3154 -- ones, and need not be turned into calls either, but if they rename
3155 -- a different operator, we must transform the node accordingly.
3156 -- Instantiations of Unchecked_Conversion are intrinsic but are
3157 -- treated as functions, even if given an operator designator.
3159 if Nkind (N) in N_Op
3160 and then Present (Entity (N))
3161 and then Ekind (Entity (N)) /= E_Operator
3163 if not Is_Predefined_Op (Entity (N)) then
3164 Rewrite_Operator_As_Call (N, Entity (N));
3166 elsif Present (Alias (Entity (N)))
3168 Nkind (Parent (Parent (Entity (N)))) =
3169 N_Subprogram_Renaming_Declaration
3171 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3173 -- If the node is rewritten, it will be fully resolved in
3174 -- Rewrite_Renamed_Operator.
3176 if Analyzed (N) then
3182 case N_Subexpr'(Nkind (N)) is
3184 Resolve_Aggregate (N, Ctx_Type);
3187 Resolve_Allocator (N, Ctx_Type);
3189 when N_Short_Circuit =>
3190 Resolve_Short_Circuit (N, Ctx_Type);
3192 when N_Attribute_Reference =>
3193 Resolve_Attribute (N, Ctx_Type);
3195 when N_Case_Expression =>
3196 Resolve_Case_Expression (N, Ctx_Type);
3198 when N_Character_Literal =>
3199 Resolve_Character_Literal (N, Ctx_Type);
3201 when N_Delta_Aggregate =>
3202 Resolve_Delta_Aggregate (N, Ctx_Type);
3204 when N_Expanded_Name =>
3205 Resolve_Entity_Name (N, Ctx_Type);
3207 when N_Explicit_Dereference =>
3208 Resolve_Explicit_Dereference (N, Ctx_Type);
3210 when N_Expression_With_Actions =>
3211 Resolve_Expression_With_Actions (N, Ctx_Type);
3213 when N_Extension_Aggregate =>
3214 Resolve_Extension_Aggregate (N, Ctx_Type);
3216 when N_Function_Call =>
3217 Resolve_Call (N, Ctx_Type);
3219 when N_Identifier =>
3220 Resolve_Entity_Name (N, Ctx_Type);
3222 when N_If_Expression =>
3223 Resolve_If_Expression (N, Ctx_Type);
3225 when N_Indexed_Component =>
3226 Resolve_Indexed_Component (N, Ctx_Type);
3228 when N_Integer_Literal =>
3229 Resolve_Integer_Literal (N, Ctx_Type);
3231 when N_Membership_Test =>
3232 Resolve_Membership_Op (N, Ctx_Type);
3235 Resolve_Null (N, Ctx_Type);
3241 Resolve_Logical_Op (N, Ctx_Type);
3246 Resolve_Equality_Op (N, Ctx_Type);
3253 Resolve_Comparison_Op (N, Ctx_Type);
3256 Resolve_Op_Not (N, Ctx_Type);
3265 Resolve_Arithmetic_Op (N, Ctx_Type);
3268 Resolve_Op_Concat (N, Ctx_Type);
3271 Resolve_Op_Expon (N, Ctx_Type);
3277 Resolve_Unary_Op (N, Ctx_Type);
3280 Resolve_Shift (N, Ctx_Type);
3282 when N_Procedure_Call_Statement =>
3283 Resolve_Call (N, Ctx_Type);
3285 when N_Operator_Symbol =>
3286 Resolve_Operator_Symbol (N, Ctx_Type);
3288 when N_Qualified_Expression =>
3289 Resolve_Qualified_Expression (N, Ctx_Type);
3291 -- Why is the following null, needs a comment ???
3293 when N_Quantified_Expression =>
3296 when N_Raise_Expression =>
3297 Resolve_Raise_Expression (N, Ctx_Type);
3299 when N_Raise_xxx_Error =>
3300 Set_Etype (N, Ctx_Type);
3303 Resolve_Range (N, Ctx_Type);
3305 when N_Real_Literal =>
3306 Resolve_Real_Literal (N, Ctx_Type);
3309 Resolve_Reference (N, Ctx_Type);
3311 when N_Selected_Component =>
3312 Resolve_Selected_Component (N, Ctx_Type);
3315 Resolve_Slice (N, Ctx_Type);
3317 when N_String_Literal =>
3318 Resolve_String_Literal (N, Ctx_Type);
3320 when N_Target_Name =>
3321 Resolve_Target_Name (N, Ctx_Type);
3323 when N_Type_Conversion =>
3324 Resolve_Type_Conversion (N, Ctx_Type);
3326 when N_Unchecked_Expression =>
3327 Resolve_Unchecked_Expression (N, Ctx_Type);
3329 when N_Unchecked_Type_Conversion =>
3330 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3333 -- Mark relevant use-type and use-package clauses as effective using
3334 -- the original node because constant folding may have occured and
3335 -- removed references that need to be examined.
3337 if Nkind (Original_Node (N)) in N_Op then
3338 Mark_Use_Clauses (Original_Node (N));
3341 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3342 -- expression of an anonymous access type that occurs in the context
3343 -- of a named general access type, except when the expression is that
3344 -- of a membership test. This ensures proper legality checking in
3345 -- terms of allowed conversions (expressions that would be illegal to
3346 -- convert implicitly are allowed in membership tests).
3348 if Ada_Version >= Ada_2012
3349 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3350 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3351 and then Nkind (Parent (N)) not in N_Membership_Test
3353 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3354 Analyze_And_Resolve (N, Ctx_Type);
3357 -- If the subexpression was replaced by a non-subexpression, then
3358 -- all we do is to expand it. The only legitimate case we know of
3359 -- is converting procedure call statement to entry call statements,
3360 -- but there may be others, so we are making this test general.
3362 if Nkind (N) not in N_Subexpr then
3363 Debug_A_Exit ("resolving ", N, " (done)");
3368 -- The expression is definitely NOT overloaded at this point, so
3369 -- we reset the Is_Overloaded flag to avoid any confusion when
3370 -- reanalyzing the node.
3372 Set_Is_Overloaded (N, False);
3374 -- Freeze expression type, entity if it is a name, and designated
3375 -- type if it is an allocator (RM 13.14(10,11,13)).
3377 -- Now that the resolution of the type of the node is complete, and
3378 -- we did not detect an error, we can expand this node. We skip the
3379 -- expand call if we are in a default expression, see section
3380 -- "Handling of Default Expressions" in Sem spec.
3382 Debug_A_Exit ("resolving ", N, " (done)");
3384 -- We unconditionally freeze the expression, even if we are in
3385 -- default expression mode (the Freeze_Expression routine tests this
3386 -- flag and only freezes static types if it is set).
3388 -- Ada 2012 (AI05-177): The declaration of an expression function
3389 -- does not cause freezing, but we never reach here in that case.
3390 -- Here we are resolving the corresponding expanded body, so we do
3391 -- need to perform normal freezing.
3393 -- As elsewhere we do not emit freeze node within a generic. We make
3394 -- an exception for entities that are expressions, only to detect
3395 -- misuses of deferred constants and preserve the output of various
3398 if not Inside_A_Generic or else Is_Entity_Name (N) then
3399 Freeze_Expression (N);
3402 -- Now we can do the expansion
3412 -- Version with check(s) suppressed
3414 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3416 if Suppress = All_Checks then
3418 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3420 Scope_Suppress.Suppress := (others => True);
3422 Scope_Suppress.Suppress := Sva;
3427 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3429 Scope_Suppress.Suppress (Suppress) := True;
3431 Scope_Suppress.Suppress (Suppress) := Svg;
3440 -- Version with implicit type
3442 procedure Resolve (N : Node_Id) is
3444 Resolve (N, Etype (N));
3447 ---------------------
3448 -- Resolve_Actuals --
3449 ---------------------
3451 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3452 Loc : constant Source_Ptr := Sloc (N);
3455 A_Typ : Entity_Id := Empty; -- init to avoid warning
3458 Prev : Node_Id := Empty;
3460 Real_F : Entity_Id := Empty; -- init to avoid warning
3462 Real_Subp : Entity_Id;
3463 -- If the subprogram being called is an inherited operation for
3464 -- a formal derived type in an instance, Real_Subp is the subprogram
3465 -- that will be called. It may have different formal names than the
3466 -- operation of the formal in the generic, so after actual is resolved
3467 -- the name of the actual in a named association must carry the name
3468 -- of the actual of the subprogram being called.
3470 procedure Check_Aliased_Parameter;
3471 -- Check rules on aliased parameters and related accessibility rules
3472 -- in (RM 3.10.2 (10.2-10.4)).
3474 procedure Check_Argument_Order;
3475 -- Performs a check for the case where the actuals are all simple
3476 -- identifiers that correspond to the formal names, but in the wrong
3477 -- order, which is considered suspicious and cause for a warning.
3479 procedure Check_Prefixed_Call;
3480 -- If the original node is an overloaded call in prefix notation,
3481 -- insert an 'Access or a dereference as needed over the first actual.
3482 -- Try_Object_Operation has already verified that there is a valid
3483 -- interpretation, but the form of the actual can only be determined
3484 -- once the primitive operation is identified.
3486 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id);
3487 -- Emit an error concerning the illegal usage of an effectively volatile
3488 -- object for reading in interfering context (SPARK RM 7.1.3(10)).
3490 procedure Insert_Default;
3491 -- If the actual is missing in a call, insert in the actuals list
3492 -- an instance of the default expression. The insertion is always
3493 -- a named association.
3495 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3496 -- Check whether T1 and T2, or their full views, are derived from a
3497 -- common type. Used to enforce the restrictions on array conversions
3500 function Static_Concatenation (N : Node_Id) return Boolean;
3501 -- Predicate to determine whether an actual that is a concatenation
3502 -- will be evaluated statically and does not need a transient scope.
3503 -- This must be determined before the actual is resolved and expanded
3504 -- because if needed the transient scope must be introduced earlier.
3506 -----------------------------
3507 -- Check_Aliased_Parameter --
3508 -----------------------------
3510 procedure Check_Aliased_Parameter is
3511 Nominal_Subt : Entity_Id;
3514 if Is_Aliased (F) then
3515 if Is_Tagged_Type (A_Typ) then
3518 elsif Is_Aliased_View (A) then
3519 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3520 Nominal_Subt := Base_Type (A_Typ);
3522 Nominal_Subt := A_Typ;
3525 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3528 -- In a generic body assume the worst for generic formals:
3529 -- they can have a constrained partial view (AI05-041).
3531 elsif Has_Discriminants (F_Typ)
3532 and then not Is_Constrained (F_Typ)
3533 and then not Object_Type_Has_Constrained_Partial_View
3534 (Typ => F_Typ, Scop => Current_Scope)
3539 Error_Msg_NE ("untagged actual does not statically match "
3540 & "aliased formal&", A, F);
3544 Error_Msg_NE ("actual for aliased formal& must be "
3545 & "aliased object", A, F);
3548 if Ekind (Nam) = E_Procedure then
3551 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3552 if Nkind (Parent (N)) = N_Type_Conversion
3553 and then Type_Access_Level (Etype (Parent (N)))
3554 < Static_Accessibility_Level (A, Object_Decl_Level)
3556 Error_Msg_N ("aliased actual has wrong accessibility", A);
3559 elsif Nkind (Parent (N)) = N_Qualified_Expression
3560 and then Nkind (Parent (Parent (N))) = N_Allocator
3561 and then Type_Access_Level (Etype (Parent (Parent (N))))
3562 < Static_Accessibility_Level (A, Object_Decl_Level)
3565 ("aliased actual in allocator has wrong accessibility", A);
3568 end Check_Aliased_Parameter;
3570 --------------------------
3571 -- Check_Argument_Order --
3572 --------------------------
3574 procedure Check_Argument_Order is
3576 -- Nothing to do if no parameters, or original node is neither a
3577 -- function call nor a procedure call statement (happens in the
3578 -- operator-transformed-to-function call case), or the call is to an
3579 -- operator symbol (which is usually in infix form), or the call does
3580 -- not come from source, or this warning is off.
3582 if not Warn_On_Parameter_Order
3583 or else No (Parameter_Associations (N))
3584 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3585 or else (Nkind (Name (N)) = N_Identifier
3586 and then Present (Entity (Name (N)))
3587 and then Nkind (Entity (Name (N))) =
3588 N_Defining_Operator_Symbol)
3589 or else not Comes_From_Source (N)
3595 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3598 -- Nothing to do if only one parameter
3604 -- Here if at least two arguments
3607 Actuals : array (1 .. Nargs) of Node_Id;
3611 Wrong_Order : Boolean := False;
3612 -- Set True if an out of order case is found
3615 -- Collect identifier names of actuals, fail if any actual is
3616 -- not a simple identifier, and record max length of name.
3618 Actual := First (Parameter_Associations (N));
3619 for J in Actuals'Range loop
3620 if Nkind (Actual) /= N_Identifier then
3623 Actuals (J) := Actual;
3628 -- If we got this far, all actuals are identifiers and the list
3629 -- of their names is stored in the Actuals array.
3631 Formal := First_Formal (Nam);
3632 for J in Actuals'Range loop
3634 -- If we ran out of formals, that's odd, probably an error
3635 -- which will be detected elsewhere, but abandon the search.
3641 -- If name matches and is in order OK
3643 if Chars (Formal) = Chars (Actuals (J)) then
3647 -- If no match, see if it is elsewhere in list and if so
3648 -- flag potential wrong order if type is compatible.
3650 for K in Actuals'Range loop
3651 if Chars (Formal) = Chars (Actuals (K))
3653 Has_Compatible_Type (Actuals (K), Etype (Formal))
3655 Wrong_Order := True;
3665 <<Continue>> Next_Formal (Formal);
3668 -- If Formals left over, also probably an error, skip warning
3670 if Present (Formal) then
3674 -- Here we give the warning if something was out of order
3678 ("?P?actuals for this call may be in wrong order", N);
3682 end Check_Argument_Order;
3684 -------------------------
3685 -- Check_Prefixed_Call --
3686 -------------------------
3688 procedure Check_Prefixed_Call is
3689 Act : constant Node_Id := First_Actual (N);
3690 A_Type : constant Entity_Id := Etype (Act);
3691 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3692 Orig : constant Node_Id := Original_Node (N);
3696 -- Check whether the call is a prefixed call, with or without
3697 -- additional actuals.
3699 if Nkind (Orig) = N_Selected_Component
3701 (Nkind (Orig) = N_Indexed_Component
3702 and then Nkind (Prefix (Orig)) = N_Selected_Component
3703 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3704 and then Is_Entity_Name (Act)
3705 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3707 if Is_Access_Type (A_Type)
3708 and then not Is_Access_Type (F_Type)
3710 -- Introduce dereference on object in prefix
3713 Make_Explicit_Dereference (Sloc (Act),
3714 Prefix => Relocate_Node (Act));
3715 Rewrite (Act, New_A);
3718 elsif Is_Access_Type (F_Type)
3719 and then not Is_Access_Type (A_Type)
3721 -- Introduce an implicit 'Access in prefix
3723 if not Is_Aliased_View (Act) then
3725 ("object in prefixed call to& must be aliased "
3726 & "(RM 4.1.3 (13 1/2))",
3731 Make_Attribute_Reference (Loc,
3732 Attribute_Name => Name_Access,
3733 Prefix => Relocate_Node (Act)));
3738 end Check_Prefixed_Call;
3740 ---------------------------------------
3741 -- Flag_Effectively_Volatile_Objects --
3742 ---------------------------------------
3744 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id) is
3745 function Flag_Object (N : Node_Id) return Traverse_Result;
3746 -- Determine whether arbitrary node N denotes an effectively volatile
3747 -- object for reading and if it does, emit an error.
3753 function Flag_Object (N : Node_Id) return Traverse_Result is
3759 -- Do not consider object name appearing in the prefix of
3760 -- attribute Address as a read.
3762 when N_Attribute_Reference =>
3764 -- Prefix of attribute Address denotes an object, program
3765 -- unit, or label; none of them needs to be flagged here.
3767 if Attribute_Name (N) = Name_Address then
3771 -- Do not consider nested function calls because they have
3772 -- already been processed during their own resolution.
3774 when N_Function_Call =>
3777 when N_Identifier | N_Expanded_Name =>
3781 and then Is_Object (Id)
3782 and then Is_Effectively_Volatile_For_Reading (Id)
3785 ("volatile object cannot appear in this context"
3786 & " (SPARK RM 7.1.3(10))", N);
3798 procedure Flag_Objects is new Traverse_Proc (Flag_Object);
3800 -- Start of processing for Flag_Effectively_Volatile_Objects
3803 Flag_Objects (Expr);
3804 end Flag_Effectively_Volatile_Objects;
3806 --------------------
3807 -- Insert_Default --
3808 --------------------
3810 procedure Insert_Default is
3815 -- Missing argument in call, nothing to insert
3817 if No (Default_Value (F)) then
3821 -- Note that we do a full New_Copy_Tree, so that any associated
3822 -- Itypes are properly copied. This may not be needed any more,
3823 -- but it does no harm as a safety measure. Defaults of a generic
3824 -- formal may be out of bounds of the corresponding actual (see
3825 -- cc1311b) and an additional check may be required.
3830 New_Scope => Current_Scope,
3833 -- Propagate dimension information, if any.
3835 Copy_Dimensions (Default_Value (F), Actval);
3837 if Is_Concurrent_Type (Scope (Nam))
3838 and then Has_Discriminants (Scope (Nam))
3840 Replace_Actual_Discriminants (N, Actval);
3843 if Is_Overloadable (Nam)
3844 and then Present (Alias (Nam))
3846 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3847 and then not Is_Tagged_Type (Etype (F))
3849 -- If default is a real literal, do not introduce a
3850 -- conversion whose effect may depend on the run-time
3851 -- size of universal real.
3853 if Nkind (Actval) = N_Real_Literal then
3854 Set_Etype (Actval, Base_Type (Etype (F)));
3856 Actval := Unchecked_Convert_To (Etype (F), Actval);
3860 if Is_Scalar_Type (Etype (F)) then
3861 Enable_Range_Check (Actval);
3864 Set_Parent (Actval, N);
3866 -- Resolve aggregates with their base type, to avoid scope
3867 -- anomalies: the subtype was first built in the subprogram
3868 -- declaration, and the current call may be nested.
3870 if Nkind (Actval) = N_Aggregate then
3871 Analyze_And_Resolve (Actval, Etype (F));
3873 Analyze_And_Resolve (Actval, Etype (Actval));
3877 Set_Parent (Actval, N);
3879 -- See note above concerning aggregates
3881 if Nkind (Actval) = N_Aggregate
3882 and then Has_Discriminants (Etype (Actval))
3884 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3886 -- Resolve entities with their own type, which may differ from
3887 -- the type of a reference in a generic context (the view
3888 -- swapping mechanism did not anticipate the re-analysis of
3889 -- default values in calls).
3891 elsif Is_Entity_Name (Actval) then
3892 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3895 Analyze_And_Resolve (Actval, Etype (Actval));
3899 -- If default is a tag indeterminate function call, propagate tag
3900 -- to obtain proper dispatching.
3902 if Is_Controlling_Formal (F)
3903 and then Nkind (Default_Value (F)) = N_Function_Call
3905 Set_Is_Controlling_Actual (Actval);
3909 -- If the default expression raises constraint error, then just
3910 -- silently replace it with an N_Raise_Constraint_Error node, since
3911 -- we already gave the warning on the subprogram spec. If node is
3912 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3913 -- the warnings removal machinery.
3915 if Raises_Constraint_Error (Actval)
3916 and then Nkind (Actval) /= N_Raise_Constraint_Error
3919 Make_Raise_Constraint_Error (Loc,
3920 Reason => CE_Range_Check_Failed));
3922 Set_Raises_Constraint_Error (Actval);
3923 Set_Etype (Actval, Etype (F));
3927 Make_Parameter_Association (Loc,
3928 Explicit_Actual_Parameter => Actval,
3929 Selector_Name => Make_Identifier (Loc, Chars (F)));
3931 -- Case of insertion is first named actual
3934 or else Nkind (Parent (Prev)) /= N_Parameter_Association
3936 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3937 Set_First_Named_Actual (N, Actval);
3940 if No (Parameter_Associations (N)) then
3941 Set_Parameter_Associations (N, New_List (Assoc));
3943 Append (Assoc, Parameter_Associations (N));
3947 Insert_After (Prev, Assoc);
3950 -- Case of insertion is not first named actual
3953 Set_Next_Named_Actual
3954 (Assoc, Next_Named_Actual (Parent (Prev)));
3955 Set_Next_Named_Actual (Parent (Prev), Actval);
3956 Append (Assoc, Parameter_Associations (N));
3959 Mark_Rewrite_Insertion (Assoc);
3960 Mark_Rewrite_Insertion (Actval);
3969 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3970 FT1 : Entity_Id := T1;
3971 FT2 : Entity_Id := T2;
3974 if Is_Private_Type (T1)
3975 and then Present (Full_View (T1))
3977 FT1 := Full_View (T1);
3980 if Is_Private_Type (T2)
3981 and then Present (Full_View (T2))
3983 FT2 := Full_View (T2);
3986 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3989 --------------------------
3990 -- Static_Concatenation --
3991 --------------------------
3993 function Static_Concatenation (N : Node_Id) return Boolean is
3996 when N_String_Literal =>
4001 -- Concatenation is static when both operands are static and
4002 -- the concatenation operator is a predefined one.
4004 return Scope (Entity (N)) = Standard_Standard
4006 Static_Concatenation (Left_Opnd (N))
4008 Static_Concatenation (Right_Opnd (N));
4011 if Is_Entity_Name (N) then
4013 Ent : constant Entity_Id := Entity (N);
4015 return Ekind (Ent) = E_Constant
4016 and then Present (Constant_Value (Ent))
4018 Is_OK_Static_Expression (Constant_Value (Ent));
4025 end Static_Concatenation;
4027 -- Start of processing for Resolve_Actuals
4030 Check_Argument_Order;
4032 if Is_Overloadable (Nam)
4033 and then Is_Inherited_Operation (Nam)
4034 and then In_Instance
4035 and then Present (Alias (Nam))
4036 and then Present (Overridden_Operation (Alias (Nam)))
4038 Real_Subp := Alias (Nam);
4043 if Present (First_Actual (N)) then
4044 Check_Prefixed_Call;
4047 A := First_Actual (N);
4048 F := First_Formal (Nam);
4050 if Present (Real_Subp) then
4051 Real_F := First_Formal (Real_Subp);
4054 while Present (F) loop
4055 if No (A) and then Needs_No_Actuals (Nam) then
4058 -- If we have an error in any actual or formal, indicated by a type
4059 -- of Any_Type, then abandon resolution attempt, and set result type
4060 -- to Any_Type. Skip this if the actual is a Raise_Expression, whose
4061 -- type is imposed from context.
4063 elsif (Present (A) and then Etype (A) = Any_Type)
4064 or else Etype (F) = Any_Type
4066 if Nkind (A) /= N_Raise_Expression then
4067 Set_Etype (N, Any_Type);
4072 -- Case where actual is present
4074 -- If the actual is an entity, generate a reference to it now. We
4075 -- do this before the actual is resolved, because a formal of some
4076 -- protected subprogram, or a task discriminant, will be rewritten
4077 -- during expansion, and the source entity reference may be lost.
4080 and then Is_Entity_Name (A)
4081 and then Comes_From_Source (A)
4083 -- Annotate the tree by creating a variable reference marker when
4084 -- the actual denotes a variable reference, in case the reference
4085 -- is folded or optimized away. The variable reference marker is
4086 -- automatically saved for later examination by the ABE Processing
4087 -- phase. The status of the reference is set as follows:
4091 -- write IN OUT, OUT
4093 if Needs_Variable_Reference_Marker
4097 Build_Variable_Reference_Marker
4099 Read => Ekind (F) /= E_Out_Parameter,
4100 Write => Ekind (F) /= E_In_Parameter);
4103 Orig_A := Entity (A);
4105 if Present (Orig_A) then
4106 if Is_Formal (Orig_A)
4107 and then Ekind (F) /= E_In_Parameter
4109 Generate_Reference (Orig_A, A, 'm');
4111 elsif not Is_Overloaded (A) then
4112 if Ekind (F) /= E_Out_Parameter then
4113 Generate_Reference (Orig_A, A);
4115 -- RM 6.4.1(12): For an out parameter that is passed by
4116 -- copy, the formal parameter object is created, and:
4118 -- * For an access type, the formal parameter is initialized
4119 -- from the value of the actual, without checking that the
4120 -- value satisfies any constraint, any predicate, or any
4121 -- exclusion of the null value.
4123 -- * For a scalar type that has the Default_Value aspect
4124 -- specified, the formal parameter is initialized from the
4125 -- value of the actual, without checking that the value
4126 -- satisfies any constraint or any predicate.
4127 -- I do not understand why this case is included??? this is
4128 -- not a case where an OUT parameter is treated as IN OUT.
4130 -- * For a composite type with discriminants or that has
4131 -- implicit initial values for any subcomponents, the
4132 -- behavior is as for an in out parameter passed by copy.
4134 -- Hence for these cases we generate the read reference now
4135 -- (the write reference will be generated later by
4136 -- Note_Possible_Modification).
4138 elsif Is_By_Copy_Type (Etype (F))
4140 (Is_Access_Type (Etype (F))
4142 (Is_Scalar_Type (Etype (F))
4144 Present (Default_Aspect_Value (Etype (F))))
4146 (Is_Composite_Type (Etype (F))
4147 and then (Has_Discriminants (Etype (F))
4148 or else Is_Partially_Initialized_Type
4151 Generate_Reference (Orig_A, A);
4158 and then (Nkind (Parent (A)) /= N_Parameter_Association
4159 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4161 -- If style checking mode on, check match of formal name
4164 if Nkind (Parent (A)) = N_Parameter_Association then
4165 Check_Identifier (Selector_Name (Parent (A)), F);
4169 -- If the formal is Out or In_Out, do not resolve and expand the
4170 -- conversion, because it is subsequently expanded into explicit
4171 -- temporaries and assignments. However, the object of the
4172 -- conversion can be resolved. An exception is the case of tagged
4173 -- type conversion with a class-wide actual. In that case we want
4174 -- the tag check to occur and no temporary will be needed (no
4175 -- representation change can occur) and the parameter is passed by
4176 -- reference, so we go ahead and resolve the type conversion.
4177 -- Another exception is the case of reference to component or
4178 -- subcomponent of a bit-packed array, in which case we want to
4179 -- defer expansion to the point the in and out assignments are
4182 if Ekind (F) /= E_In_Parameter
4183 and then Nkind (A) = N_Type_Conversion
4184 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4185 and then not Is_Interface (Etype (A))
4188 Expr_Typ : constant Entity_Id := Etype (Expression (A));
4191 -- Check RM 4.6 (24.2/2)
4193 if Is_Array_Type (Etype (F))
4194 and then Is_View_Conversion (A)
4196 -- In a view conversion, the conversion must be legal in
4197 -- both directions, and thus both component types must be
4198 -- aliased, or neither (4.6 (8)).
4200 -- Check RM 4.6 (24.8/2)
4202 if Has_Aliased_Components (Expr_Typ) /=
4203 Has_Aliased_Components (Etype (F))
4205 -- This normally illegal conversion is legal in an
4206 -- expanded instance body because of RM 12.3(11).
4207 -- At runtime, conversion must create a new object.
4209 if not In_Instance then
4211 ("both component types in a view conversion must"
4212 & " be aliased, or neither", A);
4215 -- Check RM 4.6 (24/3)
4217 elsif not Same_Ancestor (Etype (F), Expr_Typ) then
4218 -- Check view conv between unrelated by ref array
4221 if Is_By_Reference_Type (Etype (F))
4222 or else Is_By_Reference_Type (Expr_Typ)
4225 ("view conversion between unrelated by reference "
4226 & "array types not allowed ('A'I-00246)", A);
4228 -- In Ada 2005 mode, check view conversion component
4229 -- type cannot be private, tagged, or volatile. Note
4230 -- that we only apply this to source conversions. The
4231 -- generated code can contain conversions which are
4232 -- not subject to this test, and we cannot extract the
4233 -- component type in such cases since it is not
4236 elsif Comes_From_Source (A)
4237 and then Ada_Version >= Ada_2005
4240 Comp_Type : constant Entity_Id :=
4241 Component_Type (Expr_Typ);
4243 if (Is_Private_Type (Comp_Type)
4244 and then not Is_Generic_Type (Comp_Type))
4245 or else Is_Tagged_Type (Comp_Type)
4246 or else Is_Volatile (Comp_Type)
4249 ("component type of a view conversion " &
4250 "cannot be private, tagged, or volatile" &
4258 -- AI12-0074 & AI12-0377
4259 -- Check 6.4.1: If the mode is out, the actual parameter is
4260 -- a view conversion, and the type of the formal parameter
4261 -- is a scalar type, then either:
4262 -- - the target and operand type both do not have the
4263 -- Default_Value aspect specified; or
4264 -- - the target and operand type both have the
4265 -- Default_Value aspect specified, and there shall exist
4266 -- a type (other than a root numeric type) that is an
4267 -- ancestor of both the target type and the operand
4270 elsif Ekind (F) = E_Out_Parameter
4271 and then Is_Scalar_Type (Etype (F))
4273 if Has_Default_Aspect (Etype (F)) /=
4274 Has_Default_Aspect (Expr_Typ)
4277 ("view conversion requires Default_Value on both " &
4278 "types (RM 6.4.1)", A);
4279 elsif Has_Default_Aspect (Expr_Typ)
4280 and then not Same_Ancestor (Etype (F), Expr_Typ)
4283 ("view conversion between unrelated types with "
4284 & "Default_Value not allowed (RM 6.4.1)", A);
4289 -- Resolve expression if conversion is all OK
4291 if (Conversion_OK (A)
4292 or else Valid_Conversion (A, Etype (A), Expression (A)))
4293 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4295 Resolve (Expression (A));
4298 -- If the actual is a function call that returns a limited
4299 -- unconstrained object that needs finalization, create a
4300 -- transient scope for it, so that it can receive the proper
4301 -- finalization list.
4303 elsif Expander_Active
4304 and then Nkind (A) = N_Function_Call
4305 and then Is_Limited_Record (Etype (F))
4306 and then not Is_Constrained (Etype (F))
4307 and then (Needs_Finalization (Etype (F))
4308 or else Has_Task (Etype (F)))
4310 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4311 Resolve (A, Etype (F));
4313 -- A small optimization: if one of the actuals is a concatenation
4314 -- create a block around a procedure call to recover stack space.
4315 -- This alleviates stack usage when several procedure calls in
4316 -- the same statement list use concatenation. We do not perform
4317 -- this wrapping for code statements, where the argument is a
4318 -- static string, and we want to preserve warnings involving
4319 -- sequences of such statements.
4321 elsif Expander_Active
4322 and then Nkind (A) = N_Op_Concat
4323 and then Nkind (N) = N_Procedure_Call_Statement
4324 and then not (Is_Intrinsic_Subprogram (Nam)
4325 and then Chars (Nam) = Name_Asm)
4326 and then not Static_Concatenation (A)
4328 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4329 Resolve (A, Etype (F));
4332 if Nkind (A) = N_Type_Conversion
4333 and then Is_Array_Type (Etype (F))
4334 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4336 (Is_Limited_Type (Etype (F))
4337 or else Is_Limited_Type (Etype (Expression (A))))
4340 ("conversion between unrelated limited array types not "
4341 & "allowed ('A'I-00246)", A);
4343 if Is_Limited_Type (Etype (F)) then
4344 Explain_Limited_Type (Etype (F), A);
4347 if Is_Limited_Type (Etype (Expression (A))) then
4348 Explain_Limited_Type (Etype (Expression (A)), A);
4352 -- (Ada 2005: AI-251): If the actual is an allocator whose
4353 -- directly designated type is a class-wide interface, we build
4354 -- an anonymous access type to use it as the type of the
4355 -- allocator. Later, when the subprogram call is expanded, if
4356 -- the interface has a secondary dispatch table the expander
4357 -- will add a type conversion to force the correct displacement
4360 if Nkind (A) = N_Allocator then
4362 DDT : constant Entity_Id :=
4363 Directly_Designated_Type (Base_Type (Etype (F)));
4366 -- Displace the pointer to the object to reference its
4367 -- secondary dispatch table.
4369 if Is_Class_Wide_Type (DDT)
4370 and then Is_Interface (DDT)
4372 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4373 Analyze_And_Resolve (A, Etype (F),
4374 Suppress => Access_Check);
4377 -- Ada 2005, AI-162:If the actual is an allocator, the
4378 -- innermost enclosing statement is the master of the
4379 -- created object. This needs to be done with expansion
4380 -- enabled only, otherwise the transient scope will not
4381 -- be removed in the expansion of the wrapped construct.
4384 and then (Needs_Finalization (DDT)
4385 or else Has_Task (DDT))
4387 Establish_Transient_Scope
4388 (A, Manage_Sec_Stack => False);
4392 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4393 Check_Restriction (No_Access_Parameter_Allocators, A);
4397 -- (Ada 2005): The call may be to a primitive operation of a
4398 -- tagged synchronized type, declared outside of the type. In
4399 -- this case the controlling actual must be converted to its
4400 -- corresponding record type, which is the formal type. The
4401 -- actual may be a subtype, either because of a constraint or
4402 -- because it is a generic actual, so use base type to locate
4405 F_Typ := Base_Type (Etype (F));
4407 if Is_Tagged_Type (F_Typ)
4408 and then (Is_Concurrent_Type (F_Typ)
4409 or else Is_Concurrent_Record_Type (F_Typ))
4411 -- If the actual is overloaded, look for an interpretation
4412 -- that has a synchronized type.
4414 if not Is_Overloaded (A) then
4415 A_Typ := Base_Type (Etype (A));
4419 Index : Interp_Index;
4423 Get_First_Interp (A, Index, It);
4424 while Present (It.Typ) loop
4425 if Is_Concurrent_Type (It.Typ)
4426 or else Is_Concurrent_Record_Type (It.Typ)
4428 A_Typ := Base_Type (It.Typ);
4432 Get_Next_Interp (Index, It);
4438 Full_A_Typ : Entity_Id;
4441 if Present (Full_View (A_Typ)) then
4442 Full_A_Typ := Base_Type (Full_View (A_Typ));
4444 Full_A_Typ := A_Typ;
4447 -- Tagged synchronized type (case 1): the actual is a
4450 if Is_Concurrent_Type (A_Typ)
4451 and then Corresponding_Record_Type (A_Typ) = F_Typ
4454 Unchecked_Convert_To
4455 (Corresponding_Record_Type (A_Typ), A));
4456 Resolve (A, Etype (F));
4458 -- Tagged synchronized type (case 2): the formal is a
4461 elsif Ekind (Full_A_Typ) = E_Record_Type
4463 (Corresponding_Concurrent_Type (Full_A_Typ))
4464 and then Is_Concurrent_Type (F_Typ)
4465 and then Present (Corresponding_Record_Type (F_Typ))
4466 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4468 Resolve (A, Corresponding_Record_Type (F_Typ));
4473 Resolve (A, Etype (F));
4477 -- Not a synchronized operation
4480 Resolve (A, Etype (F));
4487 -- An actual cannot be an untagged formal incomplete type
4489 if Ekind (A_Typ) = E_Incomplete_Type
4490 and then not Is_Tagged_Type (A_Typ)
4491 and then Is_Generic_Type (A_Typ)
4494 ("invalid use of untagged formal incomplete type", A);
4497 -- has warnings suppressed, then we reset Never_Set_In_Source for
4498 -- the calling entity. The reason for this is to catch cases like
4499 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4500 -- uses trickery to modify an IN parameter.
4502 if Ekind (F) = E_In_Parameter
4503 and then Is_Entity_Name (A)
4504 and then Present (Entity (A))
4505 and then Ekind (Entity (A)) = E_Variable
4506 and then Has_Warnings_Off (F_Typ)
4508 Set_Never_Set_In_Source (Entity (A), False);
4511 -- Perform error checks for IN and IN OUT parameters
4513 if Ekind (F) /= E_Out_Parameter then
4515 -- Check unset reference. For scalar parameters, it is clearly
4516 -- wrong to pass an uninitialized value as either an IN or
4517 -- IN-OUT parameter. For composites, it is also clearly an
4518 -- error to pass a completely uninitialized value as an IN
4519 -- parameter, but the case of IN OUT is trickier. We prefer
4520 -- not to give a warning here. For example, suppose there is
4521 -- a routine that sets some component of a record to False.
4522 -- It is perfectly reasonable to make this IN-OUT and allow
4523 -- either initialized or uninitialized records to be passed
4526 -- For partially initialized composite values, we also avoid
4527 -- warnings, since it is quite likely that we are passing a
4528 -- partially initialized value and only the initialized fields
4529 -- will in fact be read in the subprogram.
4531 if Is_Scalar_Type (A_Typ)
4532 or else (Ekind (F) = E_In_Parameter
4533 and then not Is_Partially_Initialized_Type (A_Typ))
4535 Check_Unset_Reference (A);
4538 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4539 -- actual to a nested call, since this constitutes a reading of
4540 -- the parameter, which is not allowed.
4542 if Ada_Version = Ada_83
4543 and then Is_Entity_Name (A)
4544 and then Ekind (Entity (A)) = E_Out_Parameter
4546 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4550 -- In -gnatd.q mode, forget that a given array is constant when
4551 -- it is passed as an IN parameter to a foreign-convention
4552 -- subprogram. This is in case the subprogram evilly modifies the
4553 -- object. Of course, correct code would use IN OUT.
4556 and then Ekind (F) = E_In_Parameter
4557 and then Has_Foreign_Convention (Nam)
4558 and then Is_Array_Type (F_Typ)
4559 and then Nkind (A) in N_Has_Entity
4560 and then Present (Entity (A))
4562 Set_Is_True_Constant (Entity (A), False);
4565 -- Case of OUT or IN OUT parameter
4567 if Ekind (F) /= E_In_Parameter then
4569 -- For an Out parameter, check for useless assignment. Note
4570 -- that we can't set Last_Assignment this early, because we may
4571 -- kill current values in Resolve_Call, and that call would
4572 -- clobber the Last_Assignment field.
4574 -- Note: call Warn_On_Useless_Assignment before doing the check
4575 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4576 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4577 -- reflects the last assignment, not this one.
4579 if Ekind (F) = E_Out_Parameter then
4580 if Warn_On_Modified_As_Out_Parameter (F)
4581 and then Is_Entity_Name (A)
4582 and then Present (Entity (A))
4583 and then Comes_From_Source (N)
4585 Warn_On_Useless_Assignment (Entity (A), A);
4589 -- Validate the form of the actual. Note that the call to
4590 -- Is_OK_Variable_For_Out_Formal generates the required
4591 -- reference in this case.
4593 -- A call to an initialization procedure for an aggregate
4594 -- component may initialize a nested component of a constant
4595 -- designated object. In this context the object is variable.
4597 if not Is_OK_Variable_For_Out_Formal (A)
4598 and then not Is_Init_Proc (Nam)
4600 Error_Msg_NE ("actual for& must be a variable", A, F);
4602 if Is_Subprogram (Current_Scope) then
4603 if Is_Invariant_Procedure (Current_Scope)
4604 or else Is_Partial_Invariant_Procedure (Current_Scope)
4607 ("function used in invariant cannot modify its "
4610 elsif Is_Predicate_Function (Current_Scope) then
4612 ("function used in predicate cannot modify its "
4618 -- What's the following about???
4620 if Is_Entity_Name (A) then
4621 Kill_Checks (Entity (A));
4627 if A_Typ = Any_Type then
4628 Set_Etype (N, Any_Type);
4632 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4634 if Ekind (F) in E_In_Parameter | E_In_Out_Parameter then
4636 -- Apply predicate tests except in certain special cases. Note
4637 -- that it might be more consistent to apply these only when
4638 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4639 -- for the outbound predicate tests ??? In any case indicate
4640 -- the function being called, for better warnings if the call
4641 -- leads to an infinite recursion.
4643 if Predicate_Tests_On_Arguments (Nam) then
4644 Apply_Predicate_Check (A, F_Typ, Nam);
4647 -- Apply required constraint checks
4649 if Is_Scalar_Type (A_Typ) then
4650 Apply_Scalar_Range_Check (A, F_Typ);
4652 elsif Is_Array_Type (A_Typ) then
4653 Apply_Length_Check (A, F_Typ);
4655 elsif Is_Record_Type (F_Typ)
4656 and then Has_Discriminants (F_Typ)
4657 and then Is_Constrained (F_Typ)
4658 and then (not Is_Derived_Type (F_Typ)
4659 or else Comes_From_Source (Nam))
4661 Apply_Discriminant_Check (A, F_Typ);
4663 -- For view conversions of a discriminated object, apply
4664 -- check to object itself, the conversion alreay has the
4667 if Nkind (A) = N_Type_Conversion
4668 and then Is_Constrained (Etype (Expression (A)))
4670 Apply_Discriminant_Check (Expression (A), F_Typ);
4673 elsif Is_Access_Type (F_Typ)
4674 and then Is_Array_Type (Designated_Type (F_Typ))
4675 and then Is_Constrained (Designated_Type (F_Typ))
4677 Apply_Length_Check (A, F_Typ);
4679 elsif Is_Access_Type (F_Typ)
4680 and then Has_Discriminants (Designated_Type (F_Typ))
4681 and then Is_Constrained (Designated_Type (F_Typ))
4683 Apply_Discriminant_Check (A, F_Typ);
4686 Apply_Range_Check (A, F_Typ);
4689 -- Ada 2005 (AI-231): Note that the controlling parameter case
4690 -- already existed in Ada 95, which is partially checked
4691 -- elsewhere (see Checks), and we don't want the warning
4692 -- message to differ.
4694 if Is_Access_Type (F_Typ)
4695 and then Can_Never_Be_Null (F_Typ)
4696 and then Known_Null (A)
4698 if Is_Controlling_Formal (F) then
4699 Apply_Compile_Time_Constraint_Error
4701 Msg => "null value not allowed here??",
4702 Reason => CE_Access_Check_Failed);
4704 elsif Ada_Version >= Ada_2005 then
4705 Apply_Compile_Time_Constraint_Error
4707 Msg => "(Ada 2005) NULL not allowed in "
4708 & "null-excluding formal??",
4709 Reason => CE_Null_Not_Allowed);
4714 -- Checks for OUT parameters and IN OUT parameters
4716 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter then
4718 -- If there is a type conversion, make sure the return value
4719 -- meets the constraints of the variable before the conversion.
4721 if Nkind (A) = N_Type_Conversion then
4722 if Is_Scalar_Type (A_Typ) then
4724 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4725 -- which would prevent the check from being generated.
4726 -- This is for Starlet only though, so long obsolete.
4728 if Mechanism (F) = By_Reference
4729 and then Ekind (Nam) = E_Procedure
4730 and then Is_Valued_Procedure (Nam)
4734 Apply_Scalar_Range_Check
4735 (Expression (A), Etype (Expression (A)), A_Typ);
4738 -- In addition the return value must meet the constraints
4739 -- of the object type (see the comment below).
4741 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4745 (Expression (A), Etype (Expression (A)), A_Typ);
4748 -- If no conversion, apply scalar range checks and length check
4749 -- based on the subtype of the actual (NOT that of the formal).
4750 -- This indicates that the check takes place on return from the
4751 -- call. During expansion the required constraint checks are
4752 -- inserted. In GNATprove mode, in the absence of expansion,
4753 -- the flag indicates that the returned value is valid.
4756 if Is_Scalar_Type (F_Typ) then
4757 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4759 elsif Is_Array_Type (F_Typ)
4760 and then Ekind (F) = E_Out_Parameter
4762 Apply_Length_Check (A, F_Typ);
4765 Apply_Range_Check (A, A_Typ, F_Typ);
4769 -- Note: we do not apply the predicate checks for the case of
4770 -- OUT and IN OUT parameters. They are instead applied in the
4771 -- Expand_Actuals routine in Exp_Ch6.
4774 -- An actual associated with an access parameter is implicitly
4775 -- converted to the anonymous access type of the formal and must
4776 -- satisfy the legality checks for access conversions.
4778 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4779 if not Valid_Conversion (A, F_Typ, A) then
4781 ("invalid implicit conversion for access parameter", A);
4784 -- If the actual is an access selected component of a variable,
4785 -- the call may modify its designated object. It is reasonable
4786 -- to treat this as a potential modification of the enclosing
4787 -- record, to prevent spurious warnings that it should be
4788 -- declared as a constant, because intuitively programmers
4789 -- regard the designated subcomponent as part of the record.
4791 if Nkind (A) = N_Selected_Component
4792 and then Is_Entity_Name (Prefix (A))
4793 and then not Is_Constant_Object (Entity (Prefix (A)))
4795 Note_Possible_Modification (A, Sure => False);
4799 -- Check illegal cases of atomic/volatile/VFA actual (RM C.6(12))
4801 if (Is_By_Reference_Type (Etype (F)) or else Is_Aliased (F))
4802 and then Comes_From_Source (N)
4804 if Is_Atomic_Object (A)
4805 and then not Is_Atomic (Etype (F))
4808 ("cannot pass atomic object to nonatomic formal&",
4811 ("\which is passed by reference (RM C.6(12))", A);
4813 elsif Is_Volatile_Object_Ref (A)
4814 and then not Is_Volatile (Etype (F))
4817 ("cannot pass volatile object to nonvolatile formal&",
4820 ("\which is passed by reference (RM C.6(12))", A);
4822 elsif Is_Volatile_Full_Access_Object_Ref (A)
4823 and then not Is_Volatile_Full_Access (Etype (F))
4826 ("cannot pass full access object to nonfull access "
4829 ("\which is passed by reference (RM C.6(12))", A);
4832 -- Check for nonatomic subcomponent of a full access object
4833 -- in Ada 2020 (RM C.6 (12)).
4835 if Ada_Version >= Ada_2020
4836 and then Is_Subcomponent_Of_Full_Access_Object (A)
4837 and then not Is_Atomic_Object (A)
4840 ("cannot pass nonatomic subcomponent of full access "
4843 ("\to formal & which is passed by reference (RM C.6(12))",
4848 -- Check that subprograms don't have improper controlling
4849 -- arguments (RM 3.9.2 (9)).
4851 -- A primitive operation may have an access parameter of an
4852 -- incomplete tagged type, but a dispatching call is illegal
4853 -- if the type is still incomplete.
4855 if Is_Controlling_Formal (F) then
4856 Set_Is_Controlling_Actual (A);
4858 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4860 Desig : constant Entity_Id := Designated_Type (Etype (F));
4862 if Ekind (Desig) = E_Incomplete_Type
4863 and then No (Full_View (Desig))
4864 and then No (Non_Limited_View (Desig))
4867 ("premature use of incomplete type& "
4868 & "in dispatching call", A, Desig);
4873 elsif Nkind (A) = N_Explicit_Dereference then
4874 Validate_Remote_Access_To_Class_Wide_Type (A);
4877 -- Apply legality rule 3.9.2 (9/1)
4879 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4880 and then not Is_Class_Wide_Type (F_Typ)
4881 and then not Is_Controlling_Formal (F)
4882 and then not In_Instance
4884 Error_Msg_N ("class-wide argument not allowed here!", A);
4886 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4887 Error_Msg_Node_2 := F_Typ;
4889 ("& is not a dispatching operation of &!", A, Nam);
4892 -- Apply the checks described in 3.10.2(27): if the context is a
4893 -- specific access-to-object, the actual cannot be class-wide.
4894 -- Use base type to exclude access_to_subprogram cases.
4896 elsif Is_Access_Type (A_Typ)
4897 and then Is_Access_Type (F_Typ)
4898 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4899 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4900 or else (Nkind (A) = N_Attribute_Reference
4902 Is_Class_Wide_Type (Etype (Prefix (A)))))
4903 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4904 and then not Is_Controlling_Formal (F)
4906 -- Disable these checks for call to imported C++ subprograms
4909 (Is_Entity_Name (Name (N))
4910 and then Is_Imported (Entity (Name (N)))
4911 and then Convention (Entity (Name (N))) = Convention_CPP)
4914 ("access to class-wide argument not allowed here!", A);
4916 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4917 Error_Msg_Node_2 := Designated_Type (F_Typ);
4919 ("& is not a dispatching operation of &!", A, Nam);
4923 Check_Aliased_Parameter;
4927 -- If it is a named association, treat the selector_name as a
4928 -- proper identifier, and mark the corresponding entity.
4930 if Nkind (Parent (A)) = N_Parameter_Association
4932 -- Ignore reference in SPARK mode, as it refers to an entity not
4933 -- in scope at the point of reference, so the reference should
4934 -- be ignored for computing effects of subprograms.
4936 and then not GNATprove_Mode
4938 -- If subprogram is overridden, use name of formal that
4941 if Present (Real_Subp) then
4942 Set_Entity (Selector_Name (Parent (A)), Real_F);
4943 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
4946 Set_Entity (Selector_Name (Parent (A)), F);
4947 Generate_Reference (F, Selector_Name (Parent (A)));
4948 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4949 Generate_Reference (F_Typ, N, ' ');
4955 if Ekind (F) /= E_Out_Parameter then
4956 Check_Unset_Reference (A);
4959 -- The following checks are only relevant when SPARK_Mode is on as
4960 -- they are not standard Ada legality rule. Internally generated
4961 -- temporaries are ignored.
4963 if SPARK_Mode = On and then Comes_From_Source (A) then
4965 -- An effectively volatile object for reading may act as an
4966 -- actual when the corresponding formal is of a non-scalar
4967 -- effectively volatile type for reading (SPARK RM 7.1.3(10)).
4969 if not Is_Scalar_Type (Etype (F))
4970 and then Is_Effectively_Volatile_For_Reading (Etype (F))
4974 -- An effectively volatile object for reading may act as an
4975 -- actual in a call to an instance of Unchecked_Conversion.
4976 -- (SPARK RM 7.1.3(10)).
4978 elsif Is_Unchecked_Conversion_Instance (Nam) then
4981 -- The actual denotes an object
4983 elsif Is_Effectively_Volatile_Object_For_Reading (A) then
4985 ("volatile object cannot act as actual in a call (SPARK "
4986 & "RM 7.1.3(10))", A);
4988 -- Otherwise the actual denotes an expression. Inspect the
4989 -- expression and flag each effectively volatile object
4990 -- for reading as illegal because it apprears within an
4991 -- interfering context. Note that this is usually done in
4992 -- Resolve_Entity_Name, but when the effectively volatile
4993 -- object for reading appears as an actual in a call, the
4994 -- call must be resolved first.
4997 Flag_Effectively_Volatile_Objects (A);
5000 -- An effectively volatile variable cannot act as an actual
5001 -- parameter in a procedure call when the variable has enabled
5002 -- property Effective_Reads and the corresponding formal is of
5003 -- mode IN (SPARK RM 7.1.3(10)).
5005 if Ekind (Nam) = E_Procedure
5006 and then Ekind (F) = E_In_Parameter
5007 and then Is_Entity_Name (A)
5011 if Ekind (A_Id) = E_Variable
5012 and then Is_Effectively_Volatile_For_Reading (Etype (A_Id))
5013 and then Effective_Reads_Enabled (A_Id)
5016 ("effectively volatile variable & cannot appear as "
5017 & "actual in procedure call", A, A_Id);
5019 Error_Msg_Name_1 := Name_Effective_Reads;
5020 Error_Msg_N ("\\variable has enabled property %", A);
5021 Error_Msg_N ("\\corresponding formal has mode IN", A);
5026 -- A formal parameter of a specific tagged type whose related
5027 -- subprogram is subject to pragma Extensions_Visible with value
5028 -- "False" cannot act as an actual in a subprogram with value
5029 -- "True" (SPARK RM 6.1.7(3)).
5031 if Is_EVF_Expression (A)
5032 and then Extensions_Visible_Status (Nam) =
5033 Extensions_Visible_True
5036 ("formal parameter cannot act as actual parameter when "
5037 & "Extensions_Visible is False", A);
5039 ("\subprogram & has Extensions_Visible True", A, Nam);
5042 -- The actual parameter of a Ghost subprogram whose formal is of
5043 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
5045 if Comes_From_Source (Nam)
5046 and then Is_Ghost_Entity (Nam)
5047 and then Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
5048 and then Is_Entity_Name (A)
5049 and then Present (Entity (A))
5050 and then not Is_Ghost_Entity (Entity (A))
5053 ("non-ghost variable & cannot appear as actual in call to "
5054 & "ghost procedure", A, Entity (A));
5056 if Ekind (F) = E_In_Out_Parameter then
5057 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
5059 Error_Msg_N ("\corresponding formal has mode OUT", A);
5063 -- (AI12-0397): The target of a subprogram call that occurs within
5064 -- the expression of an Default_Initial_Condition aspect and has
5065 -- an actual that is the current instance of the type must be
5066 -- either a primitive of the type or a class-wide subprogram,
5067 -- because the type of the current instance in such an aspect is
5068 -- considered to be a notional formal derived type whose only
5069 -- operations correspond to the primitives of the enclosing type.
5070 -- Nonprimitives can be called, but the current instance must be
5071 -- converted rather than passed directly. Note that a current
5072 -- instance of a type with DIC will occur as a reference to an
5073 -- in-mode formal of an enclosing DIC procedure or partial DIC
5074 -- procedure. (It seems that this check should perhaps also apply
5075 -- to calls within Type_Invariant'Class, but not Type_Invariant,
5078 if Nkind (A) = N_Identifier
5079 and then Ekind (Entity (A)) = E_In_Parameter
5081 and then Is_Subprogram (Scope (Entity (A)))
5082 and then Is_DIC_Procedure (Scope (Entity (A)))
5084 -- We check Comes_From_Source to exclude inherited primitives
5085 -- from being flagged, because such subprograms turn out to not
5086 -- always have the Is_Primitive flag set. ???
5088 and then Comes_From_Source (Nam)
5090 and then not Is_Primitive (Nam)
5091 and then not Is_Class_Wide_Type (Etype (F))
5094 ("call to nonprimitive & with current instance not allowed " &
5095 "for aspect", A, Nam);
5100 -- Case where actual is not present
5108 if Present (Real_Subp) then
5109 Next_Formal (Real_F);
5112 end Resolve_Actuals;
5114 -----------------------
5115 -- Resolve_Allocator --
5116 -----------------------
5118 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
5119 Desig_T : constant Entity_Id := Designated_Type (Typ);
5120 E : constant Node_Id := Expression (N);
5122 Discrim : Entity_Id;
5125 Assoc : Node_Id := Empty;
5128 procedure Check_Allocator_Discrim_Accessibility
5129 (Disc_Exp : Node_Id;
5130 Alloc_Typ : Entity_Id);
5131 -- Check that accessibility level associated with an access discriminant
5132 -- initialized in an allocator by the expression Disc_Exp is not deeper
5133 -- than the level of the allocator type Alloc_Typ. An error message is
5134 -- issued if this condition is violated. Specialized checks are done for
5135 -- the cases of a constraint expression which is an access attribute or
5136 -- an access discriminant.
5138 procedure Check_Allocator_Discrim_Accessibility_Exprs
5139 (Curr_Exp : Node_Id;
5140 Alloc_Typ : Entity_Id);
5141 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5142 -- across all expressions within a given conditional expression.
5144 function In_Dispatching_Context return Boolean;
5145 -- If the allocator is an actual in a call, it is allowed to be class-
5146 -- wide when the context is not because it is a controlling actual.
5148 -------------------------------------------
5149 -- Check_Allocator_Discrim_Accessibility --
5150 -------------------------------------------
5152 procedure Check_Allocator_Discrim_Accessibility
5153 (Disc_Exp : Node_Id;
5154 Alloc_Typ : Entity_Id)
5157 if Type_Access_Level (Etype (Disc_Exp)) >
5158 Deepest_Type_Access_Level (Alloc_Typ)
5161 ("operand type has deeper level than allocator type", Disc_Exp);
5163 -- When the expression is an Access attribute the level of the prefix
5164 -- object must not be deeper than that of the allocator's type.
5166 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5167 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5169 and then Static_Accessibility_Level
5170 (Disc_Exp, Zero_On_Dynamic_Level)
5171 > Deepest_Type_Access_Level (Alloc_Typ)
5174 ("prefix of attribute has deeper level than allocator type",
5177 -- When the expression is an access discriminant the check is against
5178 -- the level of the prefix object.
5180 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5181 and then Nkind (Disc_Exp) = N_Selected_Component
5182 and then Static_Accessibility_Level
5183 (Disc_Exp, Zero_On_Dynamic_Level)
5184 > Deepest_Type_Access_Level (Alloc_Typ)
5187 ("access discriminant has deeper level than allocator type",
5190 -- All other cases are legal
5195 end Check_Allocator_Discrim_Accessibility;
5197 -------------------------------------------------
5198 -- Check_Allocator_Discrim_Accessibility_Exprs --
5199 -------------------------------------------------
5201 procedure Check_Allocator_Discrim_Accessibility_Exprs
5202 (Curr_Exp : Node_Id;
5203 Alloc_Typ : Entity_Id)
5207 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5209 -- When conditional expressions are constant folded we know at
5210 -- compile time which expression to check - so don't bother with
5211 -- the rest of the cases.
5213 if Nkind (Curr_Exp) = N_Attribute_Reference then
5214 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5216 -- Non-constant-folded if expressions
5218 elsif Nkind (Disc_Exp) = N_If_Expression then
5219 -- Check both expressions if they are still present in the face
5222 Expr := Next (First (Expressions (Disc_Exp)));
5223 if Present (Expr) then
5224 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5226 if Present (Expr) then
5227 Check_Allocator_Discrim_Accessibility_Exprs
5232 -- Non-constant-folded case expressions
5234 elsif Nkind (Disc_Exp) = N_Case_Expression then
5235 -- Check all alternatives
5237 Alt := First (Alternatives (Disc_Exp));
5238 while Present (Alt) loop
5239 Check_Allocator_Discrim_Accessibility_Exprs
5240 (Expression (Alt), Alloc_Typ);
5245 -- Base case, check the accessibility of the original node of the
5249 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5251 end Check_Allocator_Discrim_Accessibility_Exprs;
5253 ----------------------------
5254 -- In_Dispatching_Context --
5255 ----------------------------
5257 function In_Dispatching_Context return Boolean is
5258 Par : constant Node_Id := Parent (N);
5261 return Nkind (Par) in N_Subprogram_Call
5262 and then Is_Entity_Name (Name (Par))
5263 and then Is_Dispatching_Operation (Entity (Name (Par)));
5264 end In_Dispatching_Context;
5266 -- Start of processing for Resolve_Allocator
5269 -- Replace general access with specific type
5271 if Ekind (Etype (N)) = E_Allocator_Type then
5272 Set_Etype (N, Base_Type (Typ));
5275 if Is_Abstract_Type (Typ) then
5276 Error_Msg_N ("type of allocator cannot be abstract", N);
5279 -- For qualified expression, resolve the expression using the given
5280 -- subtype (nothing to do for type mark, subtype indication)
5282 if Nkind (E) = N_Qualified_Expression then
5283 if Is_Class_Wide_Type (Etype (E))
5284 and then not Is_Class_Wide_Type (Desig_T)
5285 and then not In_Dispatching_Context
5288 ("class-wide allocator not allowed for this access type", N);
5291 -- Do a full resolution to apply constraint and predicate checks
5293 Resolve_Qualified_Expression (E, Etype (E));
5294 Check_Unset_Reference (Expression (E));
5296 -- Allocators generated by the build-in-place expansion mechanism
5297 -- are explicitly marked as coming from source but do not need to be
5298 -- checked for limited initialization. To exclude this case, ensure
5299 -- that the parent of the allocator is a source node.
5300 -- The return statement constructed for an Expression_Function does
5301 -- not come from source but requires a limited check.
5303 if Is_Limited_Type (Etype (E))
5304 and then Comes_From_Source (N)
5306 (Comes_From_Source (Parent (N))
5308 (Ekind (Current_Scope) = E_Function
5309 and then Nkind (Original_Node (Unit_Declaration_Node
5310 (Current_Scope))) = N_Expression_Function))
5311 and then not In_Instance_Body
5313 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5314 if Nkind (Parent (N)) = N_Assignment_Statement then
5316 ("illegal expression for initialized allocator of a "
5317 & "limited type (RM 7.5 (2.7/2))", N);
5320 ("initialization not allowed for limited types", N);
5323 Explain_Limited_Type (Etype (E), N);
5327 -- Calls to build-in-place functions are not currently supported in
5328 -- allocators for access types associated with a simple storage pool.
5329 -- Supporting such allocators may require passing additional implicit
5330 -- parameters to build-in-place functions (or a significant revision
5331 -- of the current b-i-p implementation to unify the handling for
5332 -- multiple kinds of storage pools). ???
5334 if Is_Limited_View (Desig_T)
5335 and then Nkind (Expression (E)) = N_Function_Call
5338 Pool : constant Entity_Id :=
5339 Associated_Storage_Pool (Root_Type (Typ));
5343 Present (Get_Rep_Pragma
5344 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5347 ("limited function calls not yet supported in simple "
5348 & "storage pool allocators", Expression (E));
5353 -- A special accessibility check is needed for allocators that
5354 -- constrain access discriminants. The level of the type of the
5355 -- expression used to constrain an access discriminant cannot be
5356 -- deeper than the type of the allocator (in contrast to access
5357 -- parameters, where the level of the actual can be arbitrary).
5359 -- We can't use Valid_Conversion to perform this check because in
5360 -- general the type of the allocator is unrelated to the type of
5361 -- the access discriminant.
5363 if Ekind (Typ) /= E_Anonymous_Access_Type
5364 or else Is_Local_Anonymous_Access (Typ)
5366 Subtyp := Entity (Subtype_Mark (E));
5368 Aggr := Original_Node (Expression (E));
5370 if Has_Discriminants (Subtyp)
5371 and then Nkind (Aggr) in N_Aggregate | N_Extension_Aggregate
5373 Discrim := First_Discriminant (Base_Type (Subtyp));
5375 -- Get the first component expression of the aggregate
5377 if Present (Expressions (Aggr)) then
5378 Disc_Exp := First (Expressions (Aggr));
5380 elsif Present (Component_Associations (Aggr)) then
5381 Assoc := First (Component_Associations (Aggr));
5383 if Present (Assoc) then
5384 Disc_Exp := Expression (Assoc);
5393 while Present (Discrim) and then Present (Disc_Exp) loop
5394 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5395 Check_Allocator_Discrim_Accessibility_Exprs
5399 Next_Discriminant (Discrim);
5401 if Present (Discrim) then
5402 if Present (Assoc) then
5404 Disc_Exp := Expression (Assoc);
5406 elsif Present (Next (Disc_Exp)) then
5410 Assoc := First (Component_Associations (Aggr));
5412 if Present (Assoc) then
5413 Disc_Exp := Expression (Assoc);
5423 -- For a subtype mark or subtype indication, freeze the subtype
5426 Freeze_Expression (E);
5428 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5430 ("initialization required for access-to-constant allocator", N);
5433 -- A special accessibility check is needed for allocators that
5434 -- constrain access discriminants. The level of the type of the
5435 -- expression used to constrain an access discriminant cannot be
5436 -- deeper than the type of the allocator (in contrast to access
5437 -- parameters, where the level of the actual can be arbitrary).
5438 -- We can't use Valid_Conversion to perform this check because
5439 -- in general the type of the allocator is unrelated to the type
5440 -- of the access discriminant.
5442 if Nkind (Original_Node (E)) = N_Subtype_Indication
5443 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5444 or else Is_Local_Anonymous_Access (Typ))
5446 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5448 if Has_Discriminants (Subtyp) then
5449 Discrim := First_Discriminant (Base_Type (Subtyp));
5450 Constr := First (Constraints (Constraint (Original_Node (E))));
5451 while Present (Discrim) and then Present (Constr) loop
5452 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5453 if Nkind (Constr) = N_Discriminant_Association then
5454 Disc_Exp := Expression (Constr);
5459 Check_Allocator_Discrim_Accessibility_Exprs
5463 Next_Discriminant (Discrim);
5470 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5471 -- check that the level of the type of the created object is not deeper
5472 -- than the level of the allocator's access type, since extensions can
5473 -- now occur at deeper levels than their ancestor types. This is a
5474 -- static accessibility level check; a run-time check is also needed in
5475 -- the case of an initialized allocator with a class-wide argument (see
5476 -- Expand_Allocator_Expression).
5478 if Ada_Version >= Ada_2005
5479 and then Is_Class_Wide_Type (Desig_T)
5482 Exp_Typ : Entity_Id;
5485 if Nkind (E) = N_Qualified_Expression then
5486 Exp_Typ := Etype (E);
5487 elsif Nkind (E) = N_Subtype_Indication then
5488 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5490 Exp_Typ := Entity (E);
5493 if Type_Access_Level (Exp_Typ) >
5494 Deepest_Type_Access_Level (Typ)
5496 if In_Instance_Body then
5497 Error_Msg_Warn := SPARK_Mode /= On;
5499 ("type in allocator has deeper level than designated "
5500 & "class-wide type<<", E);
5501 Error_Msg_N ("\Program_Error [<<", E);
5504 Make_Raise_Program_Error (Sloc (N),
5505 Reason => PE_Accessibility_Check_Failed));
5508 -- Do not apply Ada 2005 accessibility checks on a class-wide
5509 -- allocator if the type given in the allocator is a formal
5510 -- type or within a formal package. A run-time check will be
5511 -- performed in the instance.
5513 elsif not Is_Generic_Type (Exp_Typ)
5514 and then not In_Generic_Formal_Package (Exp_Typ)
5517 ("type in allocator has deeper level than designated "
5518 & "class-wide type", E);
5524 -- Check for allocation from an empty storage pool. But do not complain
5525 -- if it's a return statement for a build-in-place function, because the
5526 -- allocator is there just in case the caller uses an allocator. If the
5527 -- caller does use an allocator, it will be caught at the call site.
5529 if No_Pool_Assigned (Typ)
5530 and then not Alloc_For_BIP_Return (N)
5532 Error_Msg_N ("allocation from empty storage pool!", N);
5534 -- If the context is an unchecked conversion, as may happen within an
5535 -- inlined subprogram, the allocator is being resolved with its own
5536 -- anonymous type. In that case, if the target type has a specific
5537 -- storage pool, it must be inherited explicitly by the allocator type.
5539 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5540 and then No (Associated_Storage_Pool (Typ))
5542 Set_Associated_Storage_Pool
5543 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5546 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5547 Check_Restriction (No_Anonymous_Allocators, N);
5550 -- Check that an allocator with task parts isn't for a nested access
5551 -- type when restriction No_Task_Hierarchy applies.
5553 if not Is_Library_Level_Entity (Base_Type (Typ))
5554 and then Has_Task (Base_Type (Desig_T))
5556 Check_Restriction (No_Task_Hierarchy, N);
5559 -- An illegal allocator may be rewritten as a raise Program_Error
5562 if Nkind (N) = N_Allocator then
5564 -- Avoid coextension processing for an allocator that is the
5565 -- expansion of a build-in-place function call.
5567 if Nkind (Original_Node (N)) = N_Allocator
5568 and then Nkind (Expression (Original_Node (N))) =
5569 N_Qualified_Expression
5570 and then Nkind (Expression (Expression (Original_Node (N)))) =
5572 and then Is_Expanded_Build_In_Place_Call
5573 (Expression (Expression (Original_Node (N))))
5575 null; -- b-i-p function call case
5578 -- An anonymous access discriminant is the definition of a
5581 if Ekind (Typ) = E_Anonymous_Access_Type
5582 and then Nkind (Associated_Node_For_Itype (Typ)) =
5583 N_Discriminant_Specification
5586 Discr : constant Entity_Id :=
5587 Defining_Identifier (Associated_Node_For_Itype (Typ));
5590 Check_Restriction (No_Coextensions, N);
5592 -- Ada 2012 AI05-0052: If the designated type of the
5593 -- allocator is limited, then the allocator shall not
5594 -- be used to define the value of an access discriminant
5595 -- unless the discriminated type is immutably limited.
5597 if Ada_Version >= Ada_2012
5598 and then Is_Limited_Type (Desig_T)
5599 and then not Is_Limited_View (Scope (Discr))
5602 ("only immutably limited types can have anonymous "
5603 & "access discriminants designating a limited type",
5608 -- Avoid marking an allocator as a dynamic coextension if it is
5609 -- within a static construct.
5611 if not Is_Static_Coextension (N) then
5612 Set_Is_Dynamic_Coextension (N);
5614 -- Finalization and deallocation of coextensions utilizes an
5615 -- approximate implementation which does not directly adhere
5616 -- to the semantic rules. Warn on potential issues involving
5619 if Is_Controlled (Desig_T) then
5621 ("??coextension will not be finalized when its "
5622 & "associated owner is deallocated or finalized", N);
5625 ("??coextension will not be deallocated when its "
5626 & "associated owner is deallocated", N);
5630 -- Cleanup for potential static coextensions
5633 Set_Is_Dynamic_Coextension (N, False);
5634 Set_Is_Static_Coextension (N, False);
5636 -- Anonymous access-to-controlled objects are not finalized on
5637 -- time because this involves run-time ownership and currently
5638 -- this property is not available. In rare cases the object may
5639 -- not be finalized at all. Warn on potential issues involving
5640 -- anonymous access-to-controlled objects.
5642 if Ekind (Typ) = E_Anonymous_Access_Type
5643 and then Is_Controlled_Active (Desig_T)
5646 ("??object designated by anonymous access object might "
5647 & "not be finalized until its enclosing library unit "
5648 & "goes out of scope", N);
5649 Error_Msg_N ("\use named access type instead", N);
5655 -- Report a simple error: if the designated object is a local task,
5656 -- its body has not been seen yet, and its activation will fail an
5657 -- elaboration check.
5659 if Is_Task_Type (Desig_T)
5660 and then Scope (Base_Type (Desig_T)) = Current_Scope
5661 and then Is_Compilation_Unit (Current_Scope)
5662 and then Ekind (Current_Scope) = E_Package
5663 and then not In_Package_Body (Current_Scope)
5665 Error_Msg_Warn := SPARK_Mode /= On;
5666 Error_Msg_N ("cannot activate task before body seen<<", N);
5667 Error_Msg_N ("\Program_Error [<<", N);
5670 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5671 -- type with a task component on a subpool. This action must raise
5672 -- Program_Error at runtime.
5674 if Ada_Version >= Ada_2012
5675 and then Nkind (N) = N_Allocator
5676 and then Present (Subpool_Handle_Name (N))
5677 and then Has_Task (Desig_T)
5679 Error_Msg_Warn := SPARK_Mode /= On;
5680 Error_Msg_N ("cannot allocate task on subpool<<", N);
5681 Error_Msg_N ("\Program_Error [<<", N);
5684 Make_Raise_Program_Error (Sloc (N),
5685 Reason => PE_Explicit_Raise));
5688 end Resolve_Allocator;
5690 ---------------------------
5691 -- Resolve_Arithmetic_Op --
5692 ---------------------------
5694 -- Used for resolving all arithmetic operators except exponentiation
5696 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5697 L : constant Node_Id := Left_Opnd (N);
5698 R : constant Node_Id := Right_Opnd (N);
5699 TL : constant Entity_Id := Base_Type (Etype (L));
5700 TR : constant Entity_Id := Base_Type (Etype (R));
5704 B_Typ : constant Entity_Id := Base_Type (Typ);
5705 -- We do the resolution using the base type, because intermediate values
5706 -- in expressions always are of the base type, not a subtype of it.
5708 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5709 -- Returns True if N is in a context that expects "any real type"
5711 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5712 -- Return True iff given type is Integer or universal real/integer
5714 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5715 -- Choose type of integer literal in fixed-point operation to conform
5716 -- to available fixed-point type. T is the type of the other operand,
5717 -- which is needed to determine the expected type of N.
5719 procedure Set_Operand_Type (N : Node_Id);
5720 -- Set operand type to T if universal
5722 -------------------------------
5723 -- Expected_Type_Is_Any_Real --
5724 -------------------------------
5726 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5728 -- N is the expression after "delta" in a fixed_point_definition;
5731 return Nkind (Parent (N)) in N_Ordinary_Fixed_Point_Definition
5732 | N_Decimal_Fixed_Point_Definition
5734 -- N is one of the bounds in a real_range_specification;
5737 | N_Real_Range_Specification
5739 -- N is the expression of a delta_constraint;
5742 | N_Delta_Constraint;
5743 end Expected_Type_Is_Any_Real;
5745 -----------------------------
5746 -- Is_Integer_Or_Universal --
5747 -----------------------------
5749 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5751 Index : Interp_Index;
5755 if not Is_Overloaded (N) then
5757 return Base_Type (T) = Base_Type (Standard_Integer)
5758 or else Is_Universal_Numeric_Type (T);
5760 Get_First_Interp (N, Index, It);
5761 while Present (It.Typ) loop
5762 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5763 or else Is_Universal_Numeric_Type (It.Typ)
5768 Get_Next_Interp (Index, It);
5773 end Is_Integer_Or_Universal;
5775 ----------------------------
5776 -- Set_Mixed_Mode_Operand --
5777 ----------------------------
5779 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5780 Index : Interp_Index;
5784 if Universal_Interpretation (N) = Universal_Integer then
5786 -- A universal integer literal is resolved as standard integer
5787 -- except in the case of a fixed-point result, where we leave it
5788 -- as universal (to be handled by Exp_Fixd later on)
5790 if Is_Fixed_Point_Type (T) then
5791 Resolve (N, Universal_Integer);
5793 Resolve (N, Standard_Integer);
5796 elsif Universal_Interpretation (N) = Universal_Real
5797 and then (T = Base_Type (Standard_Integer)
5798 or else Is_Universal_Numeric_Type (T))
5800 -- A universal real can appear in a fixed-type context. We resolve
5801 -- the literal with that context, even though this might raise an
5802 -- exception prematurely (the other operand may be zero).
5806 elsif Etype (N) = Base_Type (Standard_Integer)
5807 and then T = Universal_Real
5808 and then Is_Overloaded (N)
5810 -- Integer arg in mixed-mode operation. Resolve with universal
5811 -- type, in case preference rule must be applied.
5813 Resolve (N, Universal_Integer);
5815 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
5817 -- If the operand is part of a fixed multiplication operation,
5818 -- a conversion will be applied to each operand, so resolve it
5819 -- with its own type.
5821 if Nkind (Parent (N)) in N_Op_Divide | N_Op_Multiply then
5825 -- Not a mixed-mode operation, resolve with context
5830 elsif Etype (N) = Any_Fixed then
5832 -- N may itself be a mixed-mode operation, so use context type
5836 elsif Is_Fixed_Point_Type (T)
5837 and then B_Typ = Universal_Fixed
5838 and then Is_Overloaded (N)
5840 -- Must be (fixed * fixed) operation, operand must have one
5841 -- compatible interpretation.
5843 Resolve (N, Any_Fixed);
5845 elsif Is_Fixed_Point_Type (B_Typ)
5846 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5847 and then Is_Overloaded (N)
5849 -- C * F(X) in a fixed context, where C is a real literal or a
5850 -- fixed-point expression. F must have either a fixed type
5851 -- interpretation or an integer interpretation, but not both.
5853 Get_First_Interp (N, Index, It);
5854 while Present (It.Typ) loop
5855 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5856 if Analyzed (N) then
5857 Error_Msg_N ("ambiguous operand in fixed operation", N);
5859 Resolve (N, Standard_Integer);
5862 elsif Is_Fixed_Point_Type (It.Typ) then
5863 if Analyzed (N) then
5864 Error_Msg_N ("ambiguous operand in fixed operation", N);
5866 Resolve (N, It.Typ);
5870 Get_Next_Interp (Index, It);
5873 -- Reanalyze the literal with the fixed type of the context. If
5874 -- context is Universal_Fixed, we are within a conversion, leave
5875 -- the literal as a universal real because there is no usable
5876 -- fixed type, and the target of the conversion plays no role in
5890 if B_Typ = Universal_Fixed
5891 and then Nkind (Op2) = N_Real_Literal
5893 T2 := Universal_Real;
5898 Set_Analyzed (Op2, False);
5902 -- A universal real conditional expression can appear in a fixed-type
5903 -- context and must be resolved with that context to facilitate the
5904 -- code generation in the back end. However, If the context is
5905 -- Universal_fixed (i.e. as an operand of a multiplication/division
5906 -- involving a fixed-point operand) the conditional expression must
5907 -- resolve to a unique visible fixed_point type, normally Duration.
5909 elsif Nkind (N) in N_Case_Expression | N_If_Expression
5910 and then Etype (N) = Universal_Real
5911 and then Is_Fixed_Point_Type (B_Typ)
5913 if B_Typ = Universal_Fixed then
5914 Resolve (N, Unique_Fixed_Point_Type (N));
5923 end Set_Mixed_Mode_Operand;
5925 ----------------------
5926 -- Set_Operand_Type --
5927 ----------------------
5929 procedure Set_Operand_Type (N : Node_Id) is
5931 if Is_Universal_Numeric_Type (Etype (N)) then
5934 end Set_Operand_Type;
5936 -- Start of processing for Resolve_Arithmetic_Op
5939 if Comes_From_Source (N)
5940 and then Ekind (Entity (N)) = E_Function
5941 and then Is_Imported (Entity (N))
5942 and then Is_Intrinsic_Subprogram (Entity (N))
5944 Resolve_Intrinsic_Operator (N, Typ);
5947 -- Special-case for mixed-mode universal expressions or fixed point type
5948 -- operation: each argument is resolved separately. The same treatment
5949 -- is required if one of the operands of a fixed point operation is
5950 -- universal real, since in this case we don't do a conversion to a
5951 -- specific fixed-point type (instead the expander handles the case).
5953 -- Set the type of the node to its universal interpretation because
5954 -- legality checks on an exponentiation operand need the context.
5956 elsif Is_Universal_Numeric_Type (B_Typ)
5957 and then Present (Universal_Interpretation (L))
5958 and then Present (Universal_Interpretation (R))
5960 Set_Etype (N, B_Typ);
5961 Resolve (L, Universal_Interpretation (L));
5962 Resolve (R, Universal_Interpretation (R));
5964 elsif (B_Typ = Universal_Real
5965 or else Etype (N) = Universal_Fixed
5966 or else (Etype (N) = Any_Fixed
5967 and then Is_Fixed_Point_Type (B_Typ))
5968 or else (Is_Fixed_Point_Type (B_Typ)
5969 and then (Is_Integer_Or_Universal (L)
5971 Is_Integer_Or_Universal (R))))
5972 and then Nkind (N) in N_Op_Multiply | N_Op_Divide
5974 if TL = Universal_Integer or else TR = Universal_Integer then
5975 Check_For_Visible_Operator (N, B_Typ);
5978 -- If context is a fixed type and one operand is integer, the other
5979 -- is resolved with the type of the context.
5981 if Is_Fixed_Point_Type (B_Typ)
5982 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5983 or else TL = Universal_Integer)
5988 elsif Is_Fixed_Point_Type (B_Typ)
5989 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5990 or else TR = Universal_Integer)
5995 -- If both operands are universal and the context is a floating
5996 -- point type, the operands are resolved to the type of the context.
5998 elsif Is_Floating_Point_Type (B_Typ) then
6003 Set_Mixed_Mode_Operand (L, TR);
6004 Set_Mixed_Mode_Operand (R, TL);
6007 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
6008 -- multiplying operators from being used when the expected type is
6009 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
6010 -- some cases where the expected type is actually Any_Real;
6011 -- Expected_Type_Is_Any_Real takes care of that case.
6013 if Etype (N) = Universal_Fixed
6014 or else Etype (N) = Any_Fixed
6016 if B_Typ = Universal_Fixed
6017 and then not Expected_Type_Is_Any_Real (N)
6018 and then Nkind (Parent (N)) not in
6019 N_Type_Conversion | N_Unchecked_Type_Conversion
6021 Error_Msg_N ("type cannot be determined from context!", N);
6022 Error_Msg_N ("\explicit conversion to result type required", N);
6024 Set_Etype (L, Any_Type);
6025 Set_Etype (R, Any_Type);
6028 if Ada_Version = Ada_83
6029 and then Etype (N) = Universal_Fixed
6030 and then Nkind (Parent (N)) not in
6031 N_Type_Conversion | N_Unchecked_Type_Conversion
6034 ("(Ada 83) fixed-point operation needs explicit "
6038 -- The expected type is "any real type" in contexts like
6040 -- type T is delta <universal_fixed-expression> ...
6042 -- in which case we need to set the type to Universal_Real
6043 -- so that static expression evaluation will work properly.
6045 if Expected_Type_Is_Any_Real (N) then
6046 Set_Etype (N, Universal_Real);
6048 Set_Etype (N, B_Typ);
6052 elsif Is_Fixed_Point_Type (B_Typ)
6053 and then (Is_Integer_Or_Universal (L)
6054 or else Nkind (L) = N_Real_Literal
6055 or else Nkind (R) = N_Real_Literal
6056 or else Is_Integer_Or_Universal (R))
6058 Set_Etype (N, B_Typ);
6060 elsif Etype (N) = Any_Fixed then
6062 -- If no previous errors, this is only possible if one operand is
6063 -- overloaded and the context is universal. Resolve as such.
6065 Set_Etype (N, B_Typ);
6069 if Is_Universal_Numeric_Type (TL)
6071 Is_Universal_Numeric_Type (TR)
6073 Check_For_Visible_Operator (N, B_Typ);
6076 -- If the context is Universal_Fixed and the operands are also
6077 -- universal fixed, this is an error, unless there is only one
6078 -- applicable fixed_point type (usually Duration).
6080 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
6081 T := Unique_Fixed_Point_Type (N);
6083 if T = Any_Type then
6096 -- If one of the arguments was resolved to a non-universal type.
6097 -- label the result of the operation itself with the same type.
6098 -- Do the same for the universal argument, if any.
6100 T := Intersect_Types (L, R);
6101 Set_Etype (N, Base_Type (T));
6102 Set_Operand_Type (L);
6103 Set_Operand_Type (R);
6106 Generate_Operator_Reference (N, Typ);
6107 Analyze_Dimension (N);
6108 Eval_Arithmetic_Op (N);
6110 -- Set overflow and division checking bit
6112 if Nkind (N) in N_Op then
6113 if not Overflow_Checks_Suppressed (Etype (N)) then
6114 Enable_Overflow_Check (N);
6117 -- Give warning if explicit division by zero
6119 if Nkind (N) in N_Op_Divide | N_Op_Rem | N_Op_Mod
6120 and then not Division_Checks_Suppressed (Etype (N))
6122 Rop := Right_Opnd (N);
6124 if Compile_Time_Known_Value (Rop)
6125 and then ((Is_Integer_Type (Etype (Rop))
6126 and then Expr_Value (Rop) = Uint_0)
6128 (Is_Real_Type (Etype (Rop))
6129 and then Expr_Value_R (Rop) = Ureal_0))
6131 -- Specialize the warning message according to the operation.
6132 -- When SPARK_Mode is On, force a warning instead of an error
6133 -- in that case, as this likely corresponds to deactivated
6134 -- code. The following warnings are for the case
6139 -- For division, we have two cases, for float division
6140 -- of an unconstrained float type, on a machine where
6141 -- Machine_Overflows is false, we don't get an exception
6142 -- at run-time, but rather an infinity or Nan. The Nan
6143 -- case is pretty obscure, so just warn about infinities.
6145 if Is_Floating_Point_Type (Typ)
6146 and then not Is_Constrained (Typ)
6147 and then not Machine_Overflows_On_Target
6150 ("float division by zero, may generate "
6151 & "'+'/'- infinity??", Right_Opnd (N));
6153 -- For all other cases, we get a Constraint_Error
6156 Apply_Compile_Time_Constraint_Error
6157 (N, "division by zero??", CE_Divide_By_Zero,
6158 Loc => Sloc (Right_Opnd (N)),
6159 Warn => SPARK_Mode = On);
6163 Apply_Compile_Time_Constraint_Error
6164 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6165 Loc => Sloc (Right_Opnd (N)),
6166 Warn => SPARK_Mode = On);
6169 Apply_Compile_Time_Constraint_Error
6170 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6171 Loc => Sloc (Right_Opnd (N)),
6172 Warn => SPARK_Mode = On);
6174 -- Division by zero can only happen with division, rem,
6175 -- and mod operations.
6178 raise Program_Error;
6181 -- In GNATprove mode, we enable the division check so that
6182 -- GNATprove will issue a message if it cannot be proved.
6184 if GNATprove_Mode then
6185 Activate_Division_Check (N);
6188 -- Otherwise just set the flag to check at run time
6191 Activate_Division_Check (N);
6195 -- If Restriction No_Implicit_Conditionals is active, then it is
6196 -- violated if either operand can be negative for mod, or for rem
6197 -- if both operands can be negative.
6199 if Restriction_Check_Required (No_Implicit_Conditionals)
6200 and then Nkind (N) in N_Op_Rem | N_Op_Mod
6209 -- Set if corresponding operand might be negative
6213 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6214 LNeg := (not OK) or else Lo < 0;
6217 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6218 RNeg := (not OK) or else Lo < 0;
6220 -- Check if we will be generating conditionals. There are two
6221 -- cases where that can happen, first for REM, the only case
6222 -- is largest negative integer mod -1, where the division can
6223 -- overflow, but we still have to give the right result. The
6224 -- front end generates a test for this annoying case. Here we
6225 -- just test if both operands can be negative (that's what the
6226 -- expander does, so we match its logic here).
6228 -- The second case is mod where either operand can be negative.
6229 -- In this case, the back end has to generate additional tests.
6231 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6233 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6235 Check_Restriction (No_Implicit_Conditionals, N);
6241 Check_Unset_Reference (L);
6242 Check_Unset_Reference (R);
6243 end Resolve_Arithmetic_Op;
6249 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6250 Loc : constant Source_Ptr := Sloc (N);
6251 Subp : constant Node_Id := Name (N);
6252 Body_Id : Entity_Id;
6263 -- Preserve relevant elaboration-related attributes of the context which
6264 -- are no longer available or very expensive to recompute once analysis,
6265 -- resolution, and expansion are over.
6267 Mark_Elaboration_Attributes
6273 -- The context imposes a unique interpretation with type Typ on a
6274 -- procedure or function call. Find the entity of the subprogram that
6275 -- yields the expected type, and propagate the corresponding formal
6276 -- constraints on the actuals. The caller has established that an
6277 -- interpretation exists, and emitted an error if not unique.
6279 -- First deal with the case of a call to an access-to-subprogram,
6280 -- dereference made explicit in Analyze_Call.
6282 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6283 if not Is_Overloaded (Subp) then
6284 Nam := Etype (Subp);
6287 -- Find the interpretation whose type (a subprogram type) has a
6288 -- return type that is compatible with the context. Analysis of
6289 -- the node has established that one exists.
6293 Get_First_Interp (Subp, I, It);
6294 while Present (It.Typ) loop
6295 if Covers (Typ, Etype (It.Typ)) then
6300 Get_Next_Interp (I, It);
6304 raise Program_Error;
6308 -- If the prefix is not an entity, then resolve it
6310 if not Is_Entity_Name (Subp) then
6311 Resolve (Subp, Nam);
6314 -- For an indirect call, we always invalidate checks, since we do not
6315 -- know whether the subprogram is local or global. Yes we could do
6316 -- better here, e.g. by knowing that there are no local subprograms,
6317 -- but it does not seem worth the effort. Similarly, we kill all
6318 -- knowledge of current constant values.
6320 Kill_Current_Values;
6322 -- If this is a procedure call which is really an entry call, do
6323 -- the conversion of the procedure call to an entry call. Protected
6324 -- operations use the same circuitry because the name in the call
6325 -- can be an arbitrary expression with special resolution rules.
6327 elsif Nkind (Subp) in N_Selected_Component | N_Indexed_Component
6328 or else (Is_Entity_Name (Subp) and then Is_Entry (Entity (Subp)))
6330 Resolve_Entry_Call (N, Typ);
6332 if Legacy_Elaboration_Checks then
6333 Check_Elab_Call (N);
6336 -- Annotate the tree by creating a call marker in case the original
6337 -- call is transformed by expansion. The call marker is automatically
6338 -- saved for later examination by the ABE Processing phase.
6340 Build_Call_Marker (N);
6342 -- Kill checks and constant values, as above for indirect case
6343 -- Who knows what happens when another task is activated?
6345 Kill_Current_Values;
6348 -- Normal subprogram call with name established in Resolve
6350 elsif not Is_Type (Entity (Subp)) then
6351 Nam := Entity (Subp);
6352 Set_Entity_With_Checks (Subp, Nam);
6354 -- Otherwise we must have the case of an overloaded call
6357 pragma Assert (Is_Overloaded (Subp));
6359 -- Initialize Nam to prevent warning (we know it will be assigned
6360 -- in the loop below, but the compiler does not know that).
6364 Get_First_Interp (Subp, I, It);
6365 while Present (It.Typ) loop
6366 if Covers (Typ, It.Typ) then
6368 Set_Entity_With_Checks (Subp, Nam);
6372 Get_Next_Interp (I, It);
6376 -- Check that a call to Current_Task does not occur in an entry body
6378 if Is_RTE (Nam, RE_Current_Task) then
6387 -- Exclude calls that occur within the default of a formal
6388 -- parameter of the entry, since those are evaluated outside
6391 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6393 if Nkind (P) = N_Entry_Body
6394 or else (Nkind (P) = N_Subprogram_Body
6395 and then Is_Entry_Barrier_Function (P))
6398 Error_Msg_Warn := SPARK_Mode /= On;
6400 ("& should not be used in entry body (RM C.7(17))<<",
6402 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6404 Make_Raise_Program_Error (Loc,
6405 Reason => PE_Current_Task_In_Entry_Body));
6406 Set_Etype (N, Rtype);
6413 -- Check that a procedure call does not occur in the context of the
6414 -- entry call statement of a conditional or timed entry call. Note that
6415 -- the case of a call to a subprogram renaming of an entry will also be
6416 -- rejected. The test for N not being an N_Entry_Call_Statement is
6417 -- defensive, covering the possibility that the processing of entry
6418 -- calls might reach this point due to later modifications of the code
6421 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6422 and then Nkind (N) /= N_Entry_Call_Statement
6423 and then Entry_Call_Statement (Parent (N)) = N
6425 if Ada_Version < Ada_2005 then
6426 Error_Msg_N ("entry call required in select statement", N);
6428 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6429 -- for a procedure_or_entry_call, the procedure_name or
6430 -- procedure_prefix of the procedure_call_statement shall denote
6431 -- an entry renamed by a procedure, or (a view of) a primitive
6432 -- subprogram of a limited interface whose first parameter is
6433 -- a controlling parameter.
6435 elsif Nkind (N) = N_Procedure_Call_Statement
6436 and then not Is_Renamed_Entry (Nam)
6437 and then not Is_Controlling_Limited_Procedure (Nam)
6440 ("entry call or dispatching primitive of interface required", N);
6444 -- Check that this is not a call to a protected procedure or entry from
6445 -- within a protected function.
6447 Check_Internal_Protected_Use (N, Nam);
6449 -- Freeze the subprogram name if not in a spec-expression. Note that
6450 -- we freeze procedure calls as well as function calls. Procedure calls
6451 -- are not frozen according to the rules (RM 13.14(14)) because it is
6452 -- impossible to have a procedure call to a non-frozen procedure in
6453 -- pure Ada, but in the code that we generate in the expander, this
6454 -- rule needs extending because we can generate procedure calls that
6457 -- In Ada 2012, expression functions may be called within pre/post
6458 -- conditions of subsequent functions or expression functions. Such
6459 -- calls do not freeze when they appear within generated bodies,
6460 -- (including the body of another expression function) which would
6461 -- place the freeze node in the wrong scope. An expression function
6462 -- is frozen in the usual fashion, by the appearance of a real body,
6463 -- or at the end of a declarative part. However an implicit call to
6464 -- an expression function may appear when it is part of a default
6465 -- expression in a call to an initialization procedure, and must be
6466 -- frozen now, even if the body is inserted at a later point.
6467 -- Otherwise, the call freezes the expression if expander is active,
6468 -- for example as part of an object declaration.
6470 if Is_Entity_Name (Subp)
6471 and then not In_Spec_Expression
6472 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6474 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6475 or else Expander_Active)
6477 if Is_Expression_Function (Entity (Subp)) then
6479 -- Force freeze of expression function in call
6481 Set_Comes_From_Source (Subp, True);
6482 Set_Must_Not_Freeze (Subp, False);
6485 Freeze_Expression (Subp);
6488 -- For a predefined operator, the type of the result is the type imposed
6489 -- by context, except for a predefined operation on universal fixed.
6490 -- Otherwise the type of the call is the type returned by the subprogram
6493 if Is_Predefined_Op (Nam) then
6494 if Etype (N) /= Universal_Fixed then
6498 -- If the subprogram returns an array type, and the context requires the
6499 -- component type of that array type, the node is really an indexing of
6500 -- the parameterless call. Resolve as such. A pathological case occurs
6501 -- when the type of the component is an access to the array type. In
6502 -- this case the call is truly ambiguous. If the call is to an intrinsic
6503 -- subprogram, it can't be an indexed component. This check is necessary
6504 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6505 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6506 -- pointers to the same array), the compiler gets confused and does an
6507 -- infinite recursion.
6509 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6511 ((Is_Array_Type (Etype (Nam))
6512 and then Covers (Typ, Component_Type (Etype (Nam))))
6514 (Is_Access_Type (Etype (Nam))
6515 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6517 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6518 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6521 Index_Node : Node_Id;
6523 Ret_Type : constant Entity_Id := Etype (Nam);
6526 -- If this is a parameterless call there is no ambiguity and the
6527 -- call has the type of the function.
6529 if No (First_Actual (N)) then
6530 Set_Etype (N, Etype (Nam));
6532 if Present (First_Formal (Nam)) then
6533 Resolve_Actuals (N, Nam);
6536 -- Annotate the tree by creating a call marker in case the
6537 -- original call is transformed by expansion. The call marker
6538 -- is automatically saved for later examination by the ABE
6539 -- Processing phase.
6541 Build_Call_Marker (N);
6543 elsif Is_Access_Type (Ret_Type)
6545 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6548 ("cannot disambiguate function call and indexing", N);
6550 New_Subp := Relocate_Node (Subp);
6552 -- The called entity may be an explicit dereference, in which
6553 -- case there is no entity to set.
6555 if Nkind (New_Subp) /= N_Explicit_Dereference then
6556 Set_Entity (Subp, Nam);
6559 if (Is_Array_Type (Ret_Type)
6560 and then Component_Type (Ret_Type) /= Any_Type)
6562 (Is_Access_Type (Ret_Type)
6564 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6566 if Needs_No_Actuals (Nam) then
6568 -- Indexed call to a parameterless function
6571 Make_Indexed_Component (Loc,
6573 Make_Function_Call (Loc, Name => New_Subp),
6574 Expressions => Parameter_Associations (N));
6576 -- An Ada 2005 prefixed call to a primitive operation
6577 -- whose first parameter is the prefix. This prefix was
6578 -- prepended to the parameter list, which is actually a
6579 -- list of indexes. Remove the prefix in order to build
6580 -- the proper indexed component.
6583 Make_Indexed_Component (Loc,
6585 Make_Function_Call (Loc,
6587 Parameter_Associations =>
6589 (Remove_Head (Parameter_Associations (N)))),
6590 Expressions => Parameter_Associations (N));
6593 -- Preserve the parenthesis count of the node
6595 Set_Paren_Count (Index_Node, Paren_Count (N));
6597 -- Since we are correcting a node classification error made
6598 -- by the parser, we call Replace rather than Rewrite.
6600 Replace (N, Index_Node);
6602 Set_Etype (Prefix (N), Ret_Type);
6605 if Legacy_Elaboration_Checks then
6606 Check_Elab_Call (Prefix (N));
6609 -- Annotate the tree by creating a call marker in case
6610 -- the original call is transformed by expansion. The call
6611 -- marker is automatically saved for later examination by
6612 -- the ABE Processing phase.
6614 Build_Call_Marker (Prefix (N));
6616 Resolve_Indexed_Component (N, Typ);
6624 -- If the called function is not declared in the main unit and it
6625 -- returns the limited view of type then use the available view (as
6626 -- is done in Try_Object_Operation) to prevent back-end confusion;
6627 -- for the function entity itself. The call must appear in a context
6628 -- where the nonlimited view is available. If the function entity is
6629 -- in the extended main unit then no action is needed, because the
6630 -- back end handles this case. In either case the type of the call
6631 -- is the nonlimited view.
6633 if From_Limited_With (Etype (Nam))
6634 and then Present (Available_View (Etype (Nam)))
6636 Set_Etype (N, Available_View (Etype (Nam)));
6638 if not In_Extended_Main_Code_Unit (Nam) then
6639 Set_Etype (Nam, Available_View (Etype (Nam)));
6643 Set_Etype (N, Etype (Nam));
6647 -- In the case where the call is to an overloaded subprogram, Analyze
6648 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6649 -- such a case Normalize_Actuals needs to be called once more to order
6650 -- the actuals correctly. Otherwise the call will have the ordering
6651 -- given by the last overloaded subprogram whether this is the correct
6652 -- one being called or not.
6654 if Is_Overloaded (Subp) then
6655 Normalize_Actuals (N, Nam, False, Norm_OK);
6656 pragma Assert (Norm_OK);
6659 -- In any case, call is fully resolved now. Reset Overload flag, to
6660 -- prevent subsequent overload resolution if node is analyzed again
6662 Set_Is_Overloaded (Subp, False);
6663 Set_Is_Overloaded (N, False);
6665 -- A Ghost entity must appear in a specific context
6667 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6668 Check_Ghost_Context (Nam, N);
6671 -- If we are calling the current subprogram from immediately within its
6672 -- body, then that is the case where we can sometimes detect cases of
6673 -- infinite recursion statically. Do not try this in case restriction
6674 -- No_Recursion is in effect anyway, and do it only for source calls.
6676 if Comes_From_Source (N) then
6677 Scop := Current_Scope;
6679 -- Issue warning for possible infinite recursion in the absence
6680 -- of the No_Recursion restriction.
6682 if Same_Or_Aliased_Subprograms (Nam, Scop)
6683 and then not Restriction_Active (No_Recursion)
6684 and then not Is_Static_Function (Scop)
6685 and then Check_Infinite_Recursion (N)
6687 -- Here we detected and flagged an infinite recursion, so we do
6688 -- not need to test the case below for further warnings. Also we
6689 -- are all done if we now have a raise SE node.
6691 if Nkind (N) = N_Raise_Storage_Error then
6695 -- If call is to immediately containing subprogram, then check for
6696 -- the case of a possible run-time detectable infinite recursion.
6699 Scope_Loop : while Scop /= Standard_Standard loop
6700 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6702 -- Ada 202x (AI12-0075): Static functions are never allowed
6703 -- to make a recursive call, as specified by 6.8(5.4/5).
6705 if Is_Static_Function (Scop) then
6707 ("recursive call not allowed in static expression "
6710 Set_Error_Posted (Scop);
6715 -- Although in general case, recursion is not statically
6716 -- checkable, the case of calling an immediately containing
6717 -- subprogram is easy to catch.
6719 if not Is_Ignored_Ghost_Entity (Nam) then
6720 Check_Restriction (No_Recursion, N);
6723 -- If the recursive call is to a parameterless subprogram,
6724 -- then even if we can't statically detect infinite
6725 -- recursion, this is pretty suspicious, and we output a
6726 -- warning. Furthermore, we will try later to detect some
6727 -- cases here at run time by expanding checking code (see
6728 -- Detect_Infinite_Recursion in package Exp_Ch6).
6730 -- If the recursive call is within a handler, do not emit a
6731 -- warning, because this is a common idiom: loop until input
6732 -- is correct, catch illegal input in handler and restart.
6734 if No (First_Formal (Nam))
6735 and then Etype (Nam) = Standard_Void_Type
6736 and then not Error_Posted (N)
6737 and then Nkind (Parent (N)) /= N_Exception_Handler
6739 -- For the case of a procedure call. We give the message
6740 -- only if the call is the first statement in a sequence
6741 -- of statements, or if all previous statements are
6742 -- simple assignments. This is simply a heuristic to
6743 -- decrease false positives, without losing too many good
6744 -- warnings. The idea is that these previous statements
6745 -- may affect global variables the procedure depends on.
6746 -- We also exclude raise statements, that may arise from
6747 -- constraint checks and are probably unrelated to the
6748 -- intended control flow.
6750 if Nkind (N) = N_Procedure_Call_Statement
6751 and then Is_List_Member (N)
6757 while Present (P) loop
6758 if Nkind (P) not in N_Assignment_Statement
6759 | N_Raise_Constraint_Error
6769 -- Do not give warning if we are in a conditional context
6772 K : constant Node_Kind := Nkind (Parent (N));
6774 if (K = N_Loop_Statement
6775 and then Present (Iteration_Scheme (Parent (N))))
6776 or else K = N_If_Statement
6777 or else K = N_Elsif_Part
6778 or else K = N_Case_Statement_Alternative
6784 -- Here warning is to be issued
6786 Set_Has_Recursive_Call (Nam);
6787 Error_Msg_Warn := SPARK_Mode /= On;
6788 Error_Msg_N ("possible infinite recursion<<!", N);
6789 Error_Msg_N ("\Storage_Error ]<<!", N);
6795 Scop := Scope (Scop);
6796 end loop Scope_Loop;
6800 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6802 Check_Obsolescent_2005_Entity (Nam, Subp);
6804 -- If subprogram name is a predefined operator, it was given in
6805 -- functional notation. Replace call node with operator node, so
6806 -- that actuals can be resolved appropriately.
6808 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
6809 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
6812 elsif Present (Alias (Nam))
6813 and then Is_Predefined_Op (Alias (Nam))
6815 Resolve_Actuals (N, Nam);
6816 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6820 -- Create a transient scope if the resulting type requires it
6822 -- There are several notable exceptions:
6824 -- a) In init procs, the transient scope overhead is not needed, and is
6825 -- even incorrect when the call is a nested initialization call for a
6826 -- component whose expansion may generate adjust calls. However, if the
6827 -- call is some other procedure call within an initialization procedure
6828 -- (for example a call to Create_Task in the init_proc of the task
6829 -- run-time record) a transient scope must be created around this call.
6831 -- b) Enumeration literal pseudo-calls need no transient scope
6833 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
6834 -- functions) do not use the secondary stack even though the return
6835 -- type may be unconstrained.
6837 -- d) Calls to a build-in-place function, since such functions may
6838 -- allocate their result directly in a target object, and cases where
6839 -- the result does get allocated in the secondary stack are checked for
6840 -- within the specialized Exp_Ch6 procedures for expanding those
6841 -- build-in-place calls.
6843 -- e) Calls to inlinable expression functions do not use the secondary
6844 -- stack (since the call will be replaced by its returned object).
6846 -- f) If the subprogram is marked Inline_Always, then even if it returns
6847 -- an unconstrained type the call does not require use of the secondary
6848 -- stack. However, inlining will only take place if the body to inline
6849 -- is already present. It may not be available if e.g. the subprogram is
6850 -- declared in a child instance.
6852 -- g) If the subprogram is a static expression function and the call is
6853 -- a static call (the actuals are all static expressions), then we never
6854 -- want to create a transient scope (this could occur in the case of a
6855 -- static string-returning call).
6858 and then Has_Pragma_Inline (Nam)
6859 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
6860 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
6864 elsif Ekind (Nam) = E_Enumeration_Literal
6865 or else Is_Build_In_Place_Function (Nam)
6866 or else Is_Intrinsic_Subprogram (Nam)
6867 or else Is_Inlinable_Expression_Function (Nam)
6868 or else Is_Static_Function_Call (N)
6872 -- A return statement from an ignored Ghost function does not use the
6873 -- secondary stack (or any other one).
6875 elsif Expander_Active
6876 and then Ekind (Nam) in E_Function | E_Subprogram_Type
6877 and then Requires_Transient_Scope (Etype (Nam))
6878 and then not Is_Ignored_Ghost_Entity (Nam)
6880 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
6882 -- If the call appears within the bounds of a loop, it will be
6883 -- rewritten and reanalyzed, nothing left to do here.
6885 if Nkind (N) /= N_Function_Call then
6890 -- A protected function cannot be called within the definition of the
6891 -- enclosing protected type, unless it is part of a pre/postcondition
6892 -- on another protected operation. This may appear in the entry wrapper
6893 -- created for an entry with preconditions.
6895 if Is_Protected_Type (Scope (Nam))
6896 and then In_Open_Scopes (Scope (Nam))
6897 and then not Has_Completion (Scope (Nam))
6898 and then not In_Spec_Expression
6899 and then not Is_Entry_Wrapper (Current_Scope)
6902 ("& cannot be called before end of protected definition", N, Nam);
6905 -- Propagate interpretation to actuals, and add default expressions
6908 if Present (First_Formal (Nam)) then
6909 Resolve_Actuals (N, Nam);
6911 -- Overloaded literals are rewritten as function calls, for purpose of
6912 -- resolution. After resolution, we can replace the call with the
6915 elsif Ekind (Nam) = E_Enumeration_Literal then
6916 Copy_Node (Subp, N);
6917 Resolve_Entity_Name (N, Typ);
6919 -- Avoid validation, since it is a static function call
6921 Generate_Reference (Nam, Subp);
6925 -- If the subprogram is not global, then kill all saved values and
6926 -- checks. This is a bit conservative, since in many cases we could do
6927 -- better, but it is not worth the effort. Similarly, we kill constant
6928 -- values. However we do not need to do this for internal entities
6929 -- (unless they are inherited user-defined subprograms), since they
6930 -- are not in the business of molesting local values.
6932 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6933 -- kill all checks and values for calls to global subprograms. This
6934 -- takes care of the case where an access to a local subprogram is
6935 -- taken, and could be passed directly or indirectly and then called
6936 -- from almost any context.
6938 -- Note: we do not do this step till after resolving the actuals. That
6939 -- way we still take advantage of the current value information while
6940 -- scanning the actuals.
6942 -- We suppress killing values if we are processing the nodes associated
6943 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6944 -- type kills all the values as part of analyzing the code that
6945 -- initializes the dispatch tables.
6947 if Inside_Freezing_Actions = 0
6948 and then (not Is_Library_Level_Entity (Nam)
6949 or else Suppress_Value_Tracking_On_Call
6950 (Nearest_Dynamic_Scope (Current_Scope)))
6951 and then (Comes_From_Source (Nam)
6952 or else (Present (Alias (Nam))
6953 and then Comes_From_Source (Alias (Nam))))
6955 Kill_Current_Values;
6958 -- If we are warning about unread OUT parameters, this is the place to
6959 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
6960 -- after the above call to Kill_Current_Values (since that call clears
6961 -- the Last_Assignment field of all local variables).
6963 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
6964 and then Comes_From_Source (N)
6965 and then In_Extended_Main_Source_Unit (N)
6972 F := First_Formal (Nam);
6973 A := First_Actual (N);
6974 while Present (F) and then Present (A) loop
6975 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter
6976 and then Warn_On_Modified_As_Out_Parameter (F)
6977 and then Is_Entity_Name (A)
6978 and then Present (Entity (A))
6979 and then Comes_From_Source (N)
6980 and then Safe_To_Capture_Value (N, Entity (A))
6982 Set_Last_Assignment (Entity (A), A);
6991 -- If the subprogram is a primitive operation, check whether or not
6992 -- it is a correct dispatching call.
6994 if Is_Overloadable (Nam)
6995 and then Is_Dispatching_Operation (Nam)
6997 Check_Dispatching_Call (N);
6999 elsif Ekind (Nam) /= E_Subprogram_Type
7000 and then Is_Abstract_Subprogram (Nam)
7001 and then not In_Instance
7003 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
7006 -- If this is a dispatching call, generate the appropriate reference,
7007 -- for better source navigation in GNAT Studio.
7009 if Is_Overloadable (Nam)
7010 and then Present (Controlling_Argument (N))
7012 Generate_Reference (Nam, Subp, 'R');
7014 -- Normal case, not a dispatching call: generate a call reference
7017 Generate_Reference (Nam, Subp, 's');
7020 if Is_Intrinsic_Subprogram (Nam) then
7021 Check_Intrinsic_Call (N);
7024 -- Check for violation of restriction No_Specific_Termination_Handlers
7025 -- and warn on a potentially blocking call to Abort_Task.
7027 if Restriction_Check_Required (No_Specific_Termination_Handlers)
7028 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
7030 Is_RTE (Nam, RE_Specific_Handler))
7032 Check_Restriction (No_Specific_Termination_Handlers, N);
7034 elsif Is_RTE (Nam, RE_Abort_Task) then
7035 Check_Potentially_Blocking_Operation (N);
7038 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
7039 -- timing event violates restriction No_Relative_Delay (AI-0211). We
7040 -- need to check the second argument to determine whether it is an
7041 -- absolute or relative timing event.
7043 if Restriction_Check_Required (No_Relative_Delay)
7044 and then Is_RTE (Nam, RE_Set_Handler)
7045 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
7047 Check_Restriction (No_Relative_Delay, N);
7050 -- Issue an error for a call to an eliminated subprogram. This routine
7051 -- will not perform the check if the call appears within a default
7054 Check_For_Eliminated_Subprogram (Subp, Nam);
7056 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7057 -- class-wide and the call dispatches on result in a context that does
7058 -- not provide a tag, the call raises Program_Error.
7060 if Nkind (N) = N_Function_Call
7061 and then In_Instance
7062 and then Is_Generic_Actual_Type (Typ)
7063 and then Is_Class_Wide_Type (Typ)
7064 and then Has_Controlling_Result (Nam)
7065 and then Nkind (Parent (N)) = N_Object_Declaration
7067 -- Verify that none of the formals are controlling
7070 Call_OK : Boolean := False;
7074 F := First_Formal (Nam);
7075 while Present (F) loop
7076 if Is_Controlling_Formal (F) then
7085 Error_Msg_Warn := SPARK_Mode /= On;
7086 Error_Msg_N ("!cannot determine tag of result<<", N);
7087 Error_Msg_N ("\Program_Error [<<!", N);
7089 Make_Raise_Program_Error (Sloc (N),
7090 Reason => PE_Explicit_Raise));
7095 -- Check for calling a function with OUT or IN OUT parameter when the
7096 -- calling context (us right now) is not Ada 2012, so does not allow
7097 -- OUT or IN OUT parameters in function calls. Functions declared in
7098 -- a predefined unit are OK, as they may be called indirectly from a
7099 -- user-declared instantiation.
7101 if Ada_Version < Ada_2012
7102 and then Ekind (Nam) = E_Function
7103 and then Has_Out_Or_In_Out_Parameter (Nam)
7104 and then not In_Predefined_Unit (Nam)
7106 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7107 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7110 -- Check the dimensions of the actuals in the call. For function calls,
7111 -- propagate the dimensions from the returned type to N.
7113 Analyze_Dimension_Call (N, Nam);
7115 -- All done, evaluate call and deal with elaboration issues
7119 if Legacy_Elaboration_Checks then
7120 Check_Elab_Call (N);
7123 -- Annotate the tree by creating a call marker in case the original call
7124 -- is transformed by expansion. The call marker is automatically saved
7125 -- for later examination by the ABE Processing phase.
7127 Build_Call_Marker (N);
7129 Mark_Use_Clauses (Subp);
7131 Warn_On_Overlapping_Actuals (Nam, N);
7133 -- Ada 202x (AI12-0075): If the call is a static call to a static
7134 -- expression function, then we want to "inline" the call, replacing
7135 -- it with the folded static result. This is not done if the checking
7136 -- for a potentially static expression is enabled or if an error has
7137 -- been posted on the call (which may be due to the check for recursive
7138 -- calls, in which case we don't want to fall into infinite recursion
7139 -- when doing the inlining).
7141 if not Checking_Potentially_Static_Expression
7142 and then Is_Static_Function_Call (N)
7143 and then not Is_Intrinsic_Subprogram (Ultimate_Alias (Nam))
7144 and then not Error_Posted (Ultimate_Alias (Nam))
7146 Inline_Static_Function_Call (N, Ultimate_Alias (Nam));
7148 -- In GNATprove mode, expansion is disabled, but we want to inline some
7149 -- subprograms to facilitate formal verification. Indirect calls through
7150 -- a subprogram type or within a generic cannot be inlined. Inlining is
7151 -- performed only for calls subject to SPARK_Mode on.
7153 elsif GNATprove_Mode
7154 and then SPARK_Mode = On
7155 and then Is_Overloadable (Nam)
7156 and then not Inside_A_Generic
7158 Nam_UA := Ultimate_Alias (Nam);
7159 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7161 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7162 Body_Id := Corresponding_Body (Nam_Decl);
7164 -- Nothing to do if the subprogram is not eligible for inlining in
7165 -- GNATprove mode, or inlining is disabled with switch -gnatdm
7167 if not Is_Inlined_Always (Nam_UA)
7168 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7169 or else Debug_Flag_M
7173 -- Calls cannot be inlined inside assertions, as GNATprove treats
7174 -- assertions as logic expressions. Only issue a message when the
7175 -- body has been seen, otherwise this leads to spurious messages
7176 -- on expression functions.
7178 elsif In_Assertion_Expr /= 0 then
7180 ("cannot inline & (in assertion expression)?", N, Nam_UA,
7181 Suppress_Info => No (Body_Id));
7183 -- Calls cannot be inlined inside default expressions
7185 elsif In_Default_Expr then
7187 ("cannot inline & (in default expression)?", N, Nam_UA);
7189 -- Calls cannot be inlined inside quantified expressions, which
7190 -- are left in expression form for GNATprove. Since these
7191 -- expressions are only preanalyzed, we need to detect the failure
7192 -- to inline outside of the case for Full_Analysis below.
7194 elsif In_Quantified_Expression (N) then
7196 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7198 -- Inlining should not be performed during preanalysis
7200 elsif Full_Analysis then
7202 -- Do not inline calls inside expression functions or functions
7203 -- generated by the front end for subtype predicates, as this
7204 -- would prevent interpreting them as logical formulas in
7205 -- GNATprove. Only issue a message when the body has been seen,
7206 -- otherwise this leads to spurious messages on callees that
7207 -- are themselves expression functions.
7209 if Present (Current_Subprogram)
7211 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7212 or else Is_Predicate_Function (Current_Subprogram)
7213 or else Is_Invariant_Procedure (Current_Subprogram)
7214 or else Is_DIC_Procedure (Current_Subprogram))
7216 if Present (Body_Id)
7217 and then Present (Body_To_Inline (Nam_Decl))
7219 if Is_Predicate_Function (Current_Subprogram) then
7221 ("cannot inline & (inside predicate)?",
7224 elsif Is_Invariant_Procedure (Current_Subprogram) then
7226 ("cannot inline & (inside invariant)?",
7229 elsif Is_DIC_Procedure (Current_Subprogram) then
7231 ("cannot inline & (inside Default_Initial_Condition)?",
7236 ("cannot inline & (inside expression function)?",
7241 -- Cannot inline a call inside the definition of a record type,
7242 -- typically inside the constraints of the type. Calls in
7243 -- default expressions are also not inlined, but this is
7244 -- filtered out above when testing In_Default_Expr.
7246 elsif Is_Record_Type (Current_Scope) then
7248 ("cannot inline & (inside record type)?", N, Nam_UA);
7250 -- With the one-pass inlining technique, a call cannot be
7251 -- inlined if the corresponding body has not been seen yet.
7253 elsif No (Body_Id) then
7255 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7257 -- Nothing to do if there is no body to inline, indicating that
7258 -- the subprogram is not suitable for inlining in GNATprove
7261 elsif No (Body_To_Inline (Nam_Decl)) then
7264 -- Calls cannot be inlined inside potentially unevaluated
7265 -- expressions, as this would create complex actions inside
7266 -- expressions, that are not handled by GNATprove.
7268 elsif Is_Potentially_Unevaluated (N) then
7270 ("cannot inline & (in potentially unevaluated context)?",
7273 -- Calls cannot be inlined inside the conditions of while
7274 -- loops, as this would create complex actions inside
7275 -- the condition, that are not handled by GNATprove.
7277 elsif In_While_Loop_Condition (N) then
7279 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7281 -- Do not inline calls which would possibly lead to missing a
7282 -- type conversion check on an input parameter.
7284 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7286 ("cannot inline & (possible check on input parameters)?",
7289 -- Otherwise, inline the call, issuing an info message when
7293 if Debug_Flag_Underscore_F then
7295 ("info: analyzing call to & in context?", N, Nam_UA);
7298 Expand_Inlined_Call (N, Nam_UA, Nam);
7305 -----------------------------
7306 -- Resolve_Case_Expression --
7307 -----------------------------
7309 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7312 Alt_Typ : Entity_Id;
7316 Alt := First (Alternatives (N));
7317 while Present (Alt) loop
7318 Alt_Expr := Expression (Alt);
7320 if Error_Posted (Alt_Expr) then
7324 Resolve (Alt_Expr, Typ);
7325 Alt_Typ := Etype (Alt_Expr);
7327 -- When the expression is of a scalar subtype different from the
7328 -- result subtype, then insert a conversion to ensure the generation
7329 -- of a constraint check.
7331 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7332 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7333 Analyze_And_Resolve (Alt_Expr, Typ);
7339 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7340 -- dynamically tagged must be known statically.
7342 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7343 Alt := First (Alternatives (N));
7344 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7346 while Present (Alt) loop
7347 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7349 ("all or none of the dependent expressions can be "
7350 & "dynamically tagged", N);
7358 Eval_Case_Expression (N);
7359 Analyze_Dimension (N);
7360 end Resolve_Case_Expression;
7362 -------------------------------
7363 -- Resolve_Character_Literal --
7364 -------------------------------
7366 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7367 B_Typ : constant Entity_Id := Base_Type (Typ);
7371 -- Verify that the character does belong to the type of the context
7373 Set_Etype (N, B_Typ);
7374 Eval_Character_Literal (N);
7376 -- Wide_Wide_Character literals must always be defined, since the set
7377 -- of wide wide character literals is complete, i.e. if a character
7378 -- literal is accepted by the parser, then it is OK for wide wide
7379 -- character (out of range character literals are rejected).
7381 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7384 -- Always accept character literal for type Any_Character, which
7385 -- occurs in error situations and in comparisons of literals, both
7386 -- of which should accept all literals.
7388 elsif B_Typ = Any_Character then
7391 -- For Standard.Character or a type derived from it, check that the
7392 -- literal is in range.
7394 elsif Root_Type (B_Typ) = Standard_Character then
7395 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7399 -- For Standard.Wide_Character or a type derived from it, check that the
7400 -- literal is in range.
7402 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7403 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7407 -- If the entity is already set, this has already been resolved in a
7408 -- generic context, or comes from expansion. Nothing else to do.
7410 elsif Present (Entity (N)) then
7413 -- Otherwise we have a user defined character type, and we can use the
7414 -- standard visibility mechanisms to locate the referenced entity.
7417 C := Current_Entity (N);
7418 while Present (C) loop
7419 if Etype (C) = B_Typ then
7420 Set_Entity_With_Checks (N, C);
7421 Generate_Reference (C, N);
7429 -- If we fall through, then the literal does not match any of the
7430 -- entries of the enumeration type. This isn't just a constraint error
7431 -- situation, it is an illegality (see RM 4.2).
7434 ("character not defined for }", N, First_Subtype (B_Typ));
7435 end Resolve_Character_Literal;
7437 ---------------------------
7438 -- Resolve_Comparison_Op --
7439 ---------------------------
7441 -- Context requires a boolean type, and plays no role in resolution.
7442 -- Processing identical to that for equality operators. The result type is
7443 -- the base type, which matters when pathological subtypes of booleans with
7444 -- limited ranges are used.
7446 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7447 L : constant Node_Id := Left_Opnd (N);
7448 R : constant Node_Id := Right_Opnd (N);
7452 -- If this is an intrinsic operation which is not predefined, use the
7453 -- types of its declared arguments to resolve the possibly overloaded
7454 -- operands. Otherwise the operands are unambiguous and specify the
7457 if Scope (Entity (N)) /= Standard_Standard then
7458 T := Etype (First_Entity (Entity (N)));
7461 T := Find_Unique_Type (L, R);
7463 if T = Any_Fixed then
7464 T := Unique_Fixed_Point_Type (L);
7468 Set_Etype (N, Base_Type (Typ));
7469 Generate_Reference (T, N, ' ');
7471 -- Skip remaining processing if already set to Any_Type
7473 if T = Any_Type then
7477 -- Deal with other error cases
7479 if T = Any_String or else
7480 T = Any_Composite or else
7483 if T = Any_Character then
7484 Ambiguous_Character (L);
7486 Error_Msg_N ("ambiguous operands for comparison", N);
7489 Set_Etype (N, Any_Type);
7493 -- Resolve the operands if types OK
7497 Check_Unset_Reference (L);
7498 Check_Unset_Reference (R);
7499 Generate_Operator_Reference (N, T);
7500 Check_Low_Bound_Tested (N);
7502 -- Check comparison on unordered enumeration
7504 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7505 Error_Msg_Sloc := Sloc (Etype (L));
7507 ("comparison on unordered enumeration type& declared#?U?",
7511 Analyze_Dimension (N);
7513 Eval_Relational_Op (N);
7514 end Resolve_Comparison_Op;
7516 --------------------------------
7517 -- Resolve_Declare_Expression --
7518 --------------------------------
7520 procedure Resolve_Declare_Expression
7525 Need_Transient_Scope : Boolean := False;
7527 -- Install the scope created for local declarations, if
7528 -- any. The syntax allows a Declare_Expression with no
7529 -- declarations, in analogy with block statements.
7530 -- Note that that scope has no explicit declaration, but
7531 -- appears as the scope of all entities declared therein.
7533 Decl := First (Actions (N));
7534 while Present (Decl) loop
7535 exit when Nkind (Decl)
7536 in N_Object_Declaration | N_Object_Renaming_Declaration;
7540 if Present (Decl) then
7542 -- Need to establish a transient scope in case Expression (N)
7543 -- requires actions to be wrapped.
7548 Node := First (Actions (N));
7549 while Present (Node) loop
7550 if Nkind (Node) = N_Object_Declaration
7551 and then Is_Type (Etype (Defining_Identifier (Node)))
7552 and then Requires_Transient_Scope
7553 (Etype (Defining_Identifier (Node)))
7555 Need_Transient_Scope := True;
7563 if Need_Transient_Scope then
7564 Establish_Transient_Scope (Decl, True);
7566 Push_Scope (Scope (Defining_Identifier (Decl)));
7570 E : Entity_Id := First_Entity (Current_Scope);
7572 while Present (E) loop
7573 Set_Current_Entity (E);
7574 Set_Is_Immediately_Visible (E);
7579 Resolve (Expression (N), Typ);
7583 Resolve (Expression (N), Typ);
7585 end Resolve_Declare_Expression;
7587 -----------------------------------------
7588 -- Resolve_Discrete_Subtype_Indication --
7589 -----------------------------------------
7591 procedure Resolve_Discrete_Subtype_Indication
7599 Analyze (Subtype_Mark (N));
7600 S := Entity (Subtype_Mark (N));
7602 if Nkind (Constraint (N)) /= N_Range_Constraint then
7603 Error_Msg_N ("expect range constraint for discrete type", N);
7604 Set_Etype (N, Any_Type);
7607 R := Range_Expression (Constraint (N));
7615 if Base_Type (S) /= Base_Type (Typ) then
7617 ("expect subtype of }", N, First_Subtype (Typ));
7619 -- Rewrite the constraint as a range of Typ
7620 -- to allow compilation to proceed further.
7623 Rewrite (Low_Bound (R),
7624 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7625 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7626 Attribute_Name => Name_First));
7627 Rewrite (High_Bound (R),
7628 Make_Attribute_Reference (Sloc (High_Bound (R)),
7629 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7630 Attribute_Name => Name_First));
7634 Set_Etype (N, Etype (R));
7636 -- Additionally, we must check that the bounds are compatible
7637 -- with the given subtype, which might be different from the
7638 -- type of the context.
7640 Apply_Range_Check (R, S);
7642 -- ??? If the above check statically detects a Constraint_Error
7643 -- it replaces the offending bound(s) of the range R with a
7644 -- Constraint_Error node. When the itype which uses these bounds
7645 -- is frozen the resulting call to Duplicate_Subexpr generates
7646 -- a new temporary for the bounds.
7648 -- Unfortunately there are other itypes that are also made depend
7649 -- on these bounds, so when Duplicate_Subexpr is called they get
7650 -- a forward reference to the newly created temporaries and Gigi
7651 -- aborts on such forward references. This is probably sign of a
7652 -- more fundamental problem somewhere else in either the order of
7653 -- itype freezing or the way certain itypes are constructed.
7655 -- To get around this problem we call Remove_Side_Effects right
7656 -- away if either bounds of R are a Constraint_Error.
7659 L : constant Node_Id := Low_Bound (R);
7660 H : constant Node_Id := High_Bound (R);
7663 if Nkind (L) = N_Raise_Constraint_Error then
7664 Remove_Side_Effects (L);
7667 if Nkind (H) = N_Raise_Constraint_Error then
7668 Remove_Side_Effects (H);
7672 Check_Unset_Reference (Low_Bound (R));
7673 Check_Unset_Reference (High_Bound (R));
7676 end Resolve_Discrete_Subtype_Indication;
7678 -------------------------
7679 -- Resolve_Entity_Name --
7680 -------------------------
7682 -- Used to resolve identifiers and expanded names
7684 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7685 function Is_Assignment_Or_Object_Expression
7687 Expr : Node_Id) return Boolean;
7688 -- Determine whether node Context denotes an assignment statement or an
7689 -- object declaration whose expression is node Expr.
7691 function Is_Attribute_Expression (Expr : Node_Id) return Boolean;
7692 -- Determine whether Expr is part of an N_Attribute_Reference
7695 ----------------------------------------
7696 -- Is_Assignment_Or_Object_Expression --
7697 ----------------------------------------
7699 function Is_Assignment_Or_Object_Expression
7701 Expr : Node_Id) return Boolean
7704 if Nkind (Context) in
7705 N_Assignment_Statement | N_Object_Declaration
7706 and then Expression (Context) = Expr
7710 -- Check whether a construct that yields a name is the expression of
7711 -- an assignment statement or an object declaration.
7713 elsif (Nkind (Context) in N_Attribute_Reference
7714 | N_Explicit_Dereference
7715 | N_Indexed_Component
7716 | N_Selected_Component
7718 and then Prefix (Context) = Expr)
7720 (Nkind (Context) in N_Type_Conversion
7721 | N_Unchecked_Type_Conversion
7722 and then Expression (Context) = Expr)
7725 Is_Assignment_Or_Object_Expression
7726 (Context => Parent (Context),
7729 -- Otherwise the context is not an assignment statement or an object
7735 end Is_Assignment_Or_Object_Expression;
7737 -----------------------------
7738 -- Is_Attribute_Expression --
7739 -----------------------------
7741 function Is_Attribute_Expression (Expr : Node_Id) return Boolean is
7742 N : Node_Id := Expr;
7744 while Present (N) loop
7745 if Nkind (N) = N_Attribute_Reference then
7753 end Is_Attribute_Expression;
7757 E : constant Entity_Id := Entity (N);
7760 -- Start of processing for Resolve_Entity_Name
7763 -- If garbage from errors, set to Any_Type and return
7765 if No (E) and then Total_Errors_Detected /= 0 then
7766 Set_Etype (N, Any_Type);
7770 -- Replace named numbers by corresponding literals. Note that this is
7771 -- the one case where Resolve_Entity_Name must reset the Etype, since
7772 -- it is currently marked as universal.
7774 if Ekind (E) = E_Named_Integer then
7776 Eval_Named_Integer (N);
7778 elsif Ekind (E) = E_Named_Real then
7780 Eval_Named_Real (N);
7782 -- For enumeration literals, we need to make sure that a proper style
7783 -- check is done, since such literals are overloaded, and thus we did
7784 -- not do a style check during the first phase of analysis.
7786 elsif Ekind (E) = E_Enumeration_Literal then
7787 Set_Entity_With_Checks (N, E);
7788 Eval_Entity_Name (N);
7790 -- Case of (sub)type name appearing in a context where an expression
7791 -- is expected. This is legal if occurrence is a current instance.
7792 -- See RM 8.6 (17/3).
7794 elsif Is_Type (E) then
7795 if Is_Current_Instance (N) then
7798 -- Any other use is an error
7802 ("invalid use of subtype mark in expression or call", N);
7805 -- Check discriminant use if entity is discriminant in current scope,
7806 -- i.e. discriminant of record or concurrent type currently being
7807 -- analyzed. Uses in corresponding body are unrestricted.
7809 elsif Ekind (E) = E_Discriminant
7810 and then Scope (E) = Current_Scope
7811 and then not Has_Completion (Current_Scope)
7813 Check_Discriminant_Use (N);
7815 -- A parameterless generic function cannot appear in a context that
7816 -- requires resolution.
7818 elsif Ekind (E) = E_Generic_Function then
7819 Error_Msg_N ("illegal use of generic function", N);
7821 -- In Ada 83 an OUT parameter cannot be read, but attributes of
7822 -- array types (i.e. bounds and length) are legal.
7824 elsif Ekind (E) = E_Out_Parameter
7825 and then (Is_Scalar_Type (Etype (E))
7826 or else not Is_Attribute_Expression (Parent (N)))
7828 and then (Nkind (Parent (N)) in N_Op
7829 or else Nkind (Parent (N)) = N_Explicit_Dereference
7830 or else Is_Assignment_Or_Object_Expression
7831 (Context => Parent (N),
7834 if Ada_Version = Ada_83 then
7835 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
7838 -- In all other cases, just do the possible static evaluation
7841 -- A deferred constant that appears in an expression must have a
7842 -- completion, unless it has been removed by in-place expansion of
7843 -- an aggregate. A constant that is a renaming does not need
7846 if Ekind (E) = E_Constant
7847 and then Comes_From_Source (E)
7848 and then No (Constant_Value (E))
7849 and then Is_Frozen (Etype (E))
7850 and then not In_Spec_Expression
7851 and then not Is_Imported (E)
7852 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7854 if No_Initialization (Parent (E))
7855 or else (Present (Full_View (E))
7856 and then No_Initialization (Parent (Full_View (E))))
7861 ("deferred constant is frozen before completion", N);
7865 Eval_Entity_Name (N);
7870 -- When the entity appears in a parameter association, retrieve the
7871 -- related subprogram call.
7873 if Nkind (Par) = N_Parameter_Association then
7874 Par := Parent (Par);
7877 if Comes_From_Source (N) then
7879 -- The following checks are only relevant when SPARK_Mode is on as
7880 -- they are not standard Ada legality rules.
7882 if SPARK_Mode = On then
7884 -- An effectively volatile object for reading must appear in
7885 -- non-interfering context (SPARK RM 7.1.3(10)).
7888 and then Is_Effectively_Volatile_For_Reading (E)
7889 and then not Is_OK_Volatile_Context (Par, N)
7892 ("volatile object cannot appear in this context "
7893 & "(SPARK RM 7.1.3(10))", N);
7896 -- Check for possible elaboration issues with respect to reads of
7897 -- variables. The act of renaming the variable is not considered a
7898 -- read as it simply establishes an alias.
7900 if Legacy_Elaboration_Checks
7901 and then Ekind (E) = E_Variable
7902 and then Dynamic_Elaboration_Checks
7903 and then Nkind (Par) /= N_Object_Renaming_Declaration
7905 Check_Elab_Call (N);
7909 -- The variable may eventually become a constituent of a single
7910 -- protected/task type. Record the reference now and verify its
7911 -- legality when analyzing the contract of the variable
7914 if Ekind (E) = E_Variable then
7915 Record_Possible_Part_Of_Reference (E, N);
7918 -- A Ghost entity must appear in a specific context
7920 if Is_Ghost_Entity (E) then
7921 Check_Ghost_Context (E, N);
7925 -- We may be resolving an entity within expanded code, so a reference to
7926 -- an entity should be ignored when calculating effective use clauses to
7927 -- avoid inappropriate marking.
7929 if Comes_From_Source (N) then
7930 Mark_Use_Clauses (E);
7932 end Resolve_Entity_Name;
7938 procedure Resolve_Entry (Entry_Name : Node_Id) is
7939 Loc : constant Source_Ptr := Sloc (Entry_Name);
7947 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
7948 -- If the bounds of the entry family being called depend on task
7949 -- discriminants, build a new index subtype where a discriminant is
7950 -- replaced with the value of the discriminant of the target task.
7951 -- The target task is the prefix of the entry name in the call.
7953 -----------------------
7954 -- Actual_Index_Type --
7955 -----------------------
7957 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
7958 Typ : constant Entity_Id := Entry_Index_Type (E);
7959 Tsk : constant Entity_Id := Scope (E);
7960 Lo : constant Node_Id := Type_Low_Bound (Typ);
7961 Hi : constant Node_Id := Type_High_Bound (Typ);
7964 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
7965 -- If the bound is given by a discriminant, replace with a reference
7966 -- to the discriminant of the same name in the target task. If the
7967 -- entry name is the target of a requeue statement and the entry is
7968 -- in the current protected object, the bound to be used is the
7969 -- discriminal of the object (see Apply_Range_Check for details of
7970 -- the transformation).
7972 -----------------------------
7973 -- Actual_Discriminant_Ref --
7974 -----------------------------
7976 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
7977 Typ : constant Entity_Id := Etype (Bound);
7981 Remove_Side_Effects (Bound);
7983 if not Is_Entity_Name (Bound)
7984 or else Ekind (Entity (Bound)) /= E_Discriminant
7988 elsif Is_Protected_Type (Tsk)
7989 and then In_Open_Scopes (Tsk)
7990 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
7992 -- Note: here Bound denotes a discriminant of the corresponding
7993 -- record type tskV, whose discriminal is a formal of the
7994 -- init-proc tskVIP. What we want is the body discriminal,
7995 -- which is associated to the discriminant of the original
7996 -- concurrent type tsk.
7998 return New_Occurrence_Of
7999 (Find_Body_Discriminal (Entity (Bound)), Loc);
8003 Make_Selected_Component (Loc,
8004 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
8005 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
8010 end Actual_Discriminant_Ref;
8012 -- Start of processing for Actual_Index_Type
8015 if not Has_Discriminants (Tsk)
8016 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
8018 return Entry_Index_Type (E);
8021 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
8022 Set_Etype (New_T, Base_Type (Typ));
8023 Set_Size_Info (New_T, Typ);
8024 Set_RM_Size (New_T, RM_Size (Typ));
8025 Set_Scalar_Range (New_T,
8026 Make_Range (Sloc (Entry_Name),
8027 Low_Bound => Actual_Discriminant_Ref (Lo),
8028 High_Bound => Actual_Discriminant_Ref (Hi)));
8032 end Actual_Index_Type;
8034 -- Start of processing for Resolve_Entry
8037 -- Find name of entry being called, and resolve prefix of name with its
8038 -- own type. The prefix can be overloaded, and the name and signature of
8039 -- the entry must be taken into account.
8041 if Nkind (Entry_Name) = N_Indexed_Component then
8043 -- Case of dealing with entry family within the current tasks
8045 E_Name := Prefix (Entry_Name);
8048 E_Name := Entry_Name;
8051 if Is_Entity_Name (E_Name) then
8053 -- Entry call to an entry (or entry family) in the current task. This
8054 -- is legal even though the task will deadlock. Rewrite as call to
8057 -- This can also be a call to an entry in an enclosing task. If this
8058 -- is a single task, we have to retrieve its name, because the scope
8059 -- of the entry is the task type, not the object. If the enclosing
8060 -- task is a task type, the identity of the task is given by its own
8063 -- Finally this can be a requeue on an entry of the same task or
8064 -- protected object.
8066 S := Scope (Entity (E_Name));
8068 for J in reverse 0 .. Scope_Stack.Last loop
8069 if Is_Task_Type (Scope_Stack.Table (J).Entity)
8070 and then not Comes_From_Source (S)
8072 -- S is an enclosing task or protected object. The concurrent
8073 -- declaration has been converted into a type declaration, and
8074 -- the object itself has an object declaration that follows
8075 -- the type in the same declarative part.
8077 Tsk := Next_Entity (S);
8078 while Etype (Tsk) /= S loop
8085 elsif S = Scope_Stack.Table (J).Entity then
8087 -- Call to current task. Will be transformed into call to Self
8095 Make_Selected_Component (Loc,
8096 Prefix => New_Occurrence_Of (S, Loc),
8098 New_Occurrence_Of (Entity (E_Name), Loc));
8099 Rewrite (E_Name, New_N);
8102 elsif Nkind (Entry_Name) = N_Selected_Component
8103 and then Is_Overloaded (Prefix (Entry_Name))
8105 -- Use the entry name (which must be unique at this point) to find
8106 -- the prefix that returns the corresponding task/protected type.
8109 Pref : constant Node_Id := Prefix (Entry_Name);
8110 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
8115 Get_First_Interp (Pref, I, It);
8116 while Present (It.Typ) loop
8117 if Scope (Ent) = It.Typ then
8118 Set_Etype (Pref, It.Typ);
8122 Get_Next_Interp (I, It);
8127 if Nkind (Entry_Name) = N_Selected_Component then
8128 Resolve (Prefix (Entry_Name));
8129 Resolve_Implicit_Dereference (Prefix (Entry_Name));
8131 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8132 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8133 Resolve (Prefix (Prefix (Entry_Name)));
8134 Resolve_Implicit_Dereference (Prefix (Prefix (Entry_Name)));
8136 -- We do not resolve the prefix because an Entry_Family has no type,
8137 -- although it has the semantics of an array since it can be indexed.
8138 -- In order to perform the associated range check, we would need to
8139 -- build an array type on the fly and set it on the prefix, but this
8140 -- would be wasteful since only the index type matters. Therefore we
8141 -- attach this index type directly, so that Actual_Index_Expression
8142 -- can pick it up later in order to generate the range check.
8144 Set_Etype (Prefix (Entry_Name), Actual_Index_Type (Nam));
8146 Index := First (Expressions (Entry_Name));
8147 Resolve (Index, Entry_Index_Type (Nam));
8149 -- Generate a reference for the index when it denotes an entity
8151 if Is_Entity_Name (Index) then
8152 Generate_Reference (Entity (Index), Nam);
8155 -- Up to this point the expression could have been the actual in a
8156 -- simple entry call, and be given by a named association.
8158 if Nkind (Index) = N_Parameter_Association then
8159 Error_Msg_N ("expect expression for entry index", Index);
8161 Apply_Scalar_Range_Check (Index, Etype (Prefix (Entry_Name)));
8166 ------------------------
8167 -- Resolve_Entry_Call --
8168 ------------------------
8170 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8171 Entry_Name : constant Node_Id := Name (N);
8172 Loc : constant Source_Ptr := Sloc (Entry_Name);
8180 -- We kill all checks here, because it does not seem worth the effort to
8181 -- do anything better, an entry call is a big operation.
8185 -- Processing of the name is similar for entry calls and protected
8186 -- operation calls. Once the entity is determined, we can complete
8187 -- the resolution of the actuals.
8189 -- The selector may be overloaded, in the case of a protected object
8190 -- with overloaded functions. The type of the context is used for
8193 if Nkind (Entry_Name) = N_Selected_Component
8194 and then Is_Overloaded (Selector_Name (Entry_Name))
8195 and then Typ /= Standard_Void_Type
8202 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8203 while Present (It.Typ) loop
8204 if Covers (Typ, It.Typ) then
8205 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8206 Set_Etype (Entry_Name, It.Typ);
8208 Generate_Reference (It.Typ, N, ' ');
8211 Get_Next_Interp (I, It);
8216 Resolve_Entry (Entry_Name);
8218 if Nkind (Entry_Name) = N_Selected_Component then
8220 -- Simple entry or protected operation call
8222 Nam := Entity (Selector_Name (Entry_Name));
8223 Obj := Prefix (Entry_Name);
8225 if Is_Subprogram (Nam) then
8226 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8229 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8231 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8233 -- Call to member of entry family
8235 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8236 Obj := Prefix (Prefix (Entry_Name));
8237 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8240 -- We cannot in general check the maximum depth of protected entry calls
8241 -- at compile time. But we can tell that any protected entry call at all
8242 -- violates a specified nesting depth of zero.
8244 if Is_Protected_Type (Scope (Nam)) then
8245 Check_Restriction (Max_Entry_Queue_Length, N);
8248 -- Use context type to disambiguate a protected function that can be
8249 -- called without actuals and that returns an array type, and where the
8250 -- argument list may be an indexing of the returned value.
8252 if Ekind (Nam) = E_Function
8253 and then Needs_No_Actuals (Nam)
8254 and then Present (Parameter_Associations (N))
8256 ((Is_Array_Type (Etype (Nam))
8257 and then Covers (Typ, Component_Type (Etype (Nam))))
8259 or else (Is_Access_Type (Etype (Nam))
8260 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8264 Component_Type (Designated_Type (Etype (Nam))))))
8267 Index_Node : Node_Id;
8271 Make_Indexed_Component (Loc,
8273 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8274 Expressions => Parameter_Associations (N));
8276 -- Since we are correcting a node classification error made by the
8277 -- parser, we call Replace rather than Rewrite.
8279 Replace (N, Index_Node);
8280 Set_Etype (Prefix (N), Etype (Nam));
8282 Resolve_Indexed_Component (N, Typ);
8288 and then Present (Contract_Wrapper (Nam))
8289 and then Current_Scope /= Contract_Wrapper (Nam)
8291 -- Note the entity being called before rewriting the call, so that
8292 -- it appears used at this point.
8294 Generate_Reference (Nam, Entry_Name, 'r');
8296 -- Rewrite as call to the precondition wrapper, adding the task
8297 -- object to the list of actuals. If the call is to a member of an
8298 -- entry family, include the index as well.
8302 New_Actuals : List_Id;
8305 New_Actuals := New_List (Obj);
8307 if Nkind (Entry_Name) = N_Indexed_Component then
8308 Append_To (New_Actuals,
8309 New_Copy_Tree (First (Expressions (Entry_Name))));
8312 Append_List (Parameter_Associations (N), New_Actuals);
8314 Make_Procedure_Call_Statement (Loc,
8316 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8317 Parameter_Associations => New_Actuals);
8318 Rewrite (N, New_Call);
8320 -- Preanalyze and resolve new call. Current procedure is called
8321 -- from Resolve_Call, after which expansion will take place.
8323 Preanalyze_And_Resolve (N);
8328 -- The operation name may have been overloaded. Order the actuals
8329 -- according to the formals of the resolved entity, and set the return
8330 -- type to that of the operation.
8333 Normalize_Actuals (N, Nam, False, Norm_OK);
8334 pragma Assert (Norm_OK);
8335 Set_Etype (N, Etype (Nam));
8337 -- Reset the Is_Overloaded flag, since resolution is now completed
8339 -- Simple entry call
8341 if Nkind (Entry_Name) = N_Selected_Component then
8342 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8344 -- Call to a member of an entry family
8346 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8347 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8351 Resolve_Actuals (N, Nam);
8352 Check_Internal_Protected_Use (N, Nam);
8354 -- Create a call reference to the entry
8356 Generate_Reference (Nam, Entry_Name, 's');
8358 if Is_Entry (Nam) then
8359 Check_Potentially_Blocking_Operation (N);
8362 -- Verify that a procedure call cannot masquerade as an entry
8363 -- call where an entry call is expected.
8365 if Ekind (Nam) = E_Procedure then
8366 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8367 and then N = Entry_Call_Statement (Parent (N))
8369 Error_Msg_N ("entry call required in select statement", N);
8371 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8372 and then N = Triggering_Statement (Parent (N))
8374 Error_Msg_N ("triggering statement cannot be procedure call", N);
8376 elsif Ekind (Scope (Nam)) = E_Task_Type
8377 and then not In_Open_Scopes (Scope (Nam))
8379 Error_Msg_N ("task has no entry with this name", Entry_Name);
8383 -- After resolution, entry calls and protected procedure calls are
8384 -- changed into entry calls, for expansion. The structure of the node
8385 -- does not change, so it can safely be done in place. Protected
8386 -- function calls must keep their structure because they are
8389 if Ekind (Nam) /= E_Function then
8391 -- A protected operation that is not a function may modify the
8392 -- corresponding object, and cannot apply to a constant. If this
8393 -- is an internal call, the prefix is the type itself.
8395 if Is_Protected_Type (Scope (Nam))
8396 and then not Is_Variable (Obj)
8397 and then (not Is_Entity_Name (Obj)
8398 or else not Is_Type (Entity (Obj)))
8401 ("prefix of protected procedure or entry call must be variable",
8406 Entry_Call : Node_Id;
8410 Make_Entry_Call_Statement (Loc,
8412 Parameter_Associations => Parameter_Associations (N));
8414 -- Inherit relevant attributes from the original call
8416 Set_First_Named_Actual
8417 (Entry_Call, First_Named_Actual (N));
8419 Set_Is_Elaboration_Checks_OK_Node
8420 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8422 Set_Is_Elaboration_Warnings_OK_Node
8423 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8425 Set_Is_SPARK_Mode_On_Node
8426 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8428 Rewrite (N, Entry_Call);
8429 Set_Analyzed (N, True);
8432 -- Protected functions can return on the secondary stack, in which case
8433 -- we must trigger the transient scope mechanism.
8435 elsif Expander_Active
8436 and then Requires_Transient_Scope (Etype (Nam))
8438 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
8441 -- Now we know that this is not a call to a function that returns an
8442 -- array type; moreover, we know the name of the called entry. Detect
8443 -- overlapping actuals, just like for a subprogram call.
8445 Warn_On_Overlapping_Actuals (Nam, N);
8447 end Resolve_Entry_Call;
8449 -------------------------
8450 -- Resolve_Equality_Op --
8451 -------------------------
8453 -- Both arguments must have the same type, and the boolean context does
8454 -- not participate in the resolution. The first pass verifies that the
8455 -- interpretation is not ambiguous, and the type of the left argument is
8456 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
8457 -- are strings or aggregates, allocators, or Null, they are ambiguous even
8458 -- though they carry a single (universal) type. Diagnose this case here.
8460 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8461 L : constant Node_Id := Left_Opnd (N);
8462 R : constant Node_Id := Right_Opnd (N);
8463 T : Entity_Id := Find_Unique_Type (L, R);
8465 procedure Check_If_Expression (Cond : Node_Id);
8466 -- The resolution rule for if expressions requires that each such must
8467 -- have a unique type. This means that if several dependent expressions
8468 -- are of a non-null anonymous access type, and the context does not
8469 -- impose an expected type (as can be the case in an equality operation)
8470 -- the expression must be rejected.
8472 procedure Explain_Redundancy (N : Node_Id);
8473 -- Attempt to explain the nature of a redundant comparison with True. If
8474 -- the expression N is too complex, this routine issues a general error
8477 function Find_Unique_Access_Type return Entity_Id;
8478 -- In the case of allocators and access attributes, the context must
8479 -- provide an indication of the specific access type to be used. If
8480 -- one operand is of such a "generic" access type, check whether there
8481 -- is a specific visible access type that has the same designated type.
8482 -- This is semantically dubious, and of no interest to any real code,
8483 -- but c48008a makes it all worthwhile.
8485 function Suspicious_Prio_For_Equality return Boolean;
8486 -- Returns True iff the parent node is a and/or/xor operation that
8487 -- could be the cause of confused priorities. Note that if the not is
8488 -- in parens, then False is returned.
8490 -------------------------
8491 -- Check_If_Expression --
8492 -------------------------
8494 procedure Check_If_Expression (Cond : Node_Id) is
8495 Then_Expr : Node_Id;
8496 Else_Expr : Node_Id;
8499 if Nkind (Cond) = N_If_Expression then
8500 Then_Expr := Next (First (Expressions (Cond)));
8501 Else_Expr := Next (Then_Expr);
8503 if Nkind (Then_Expr) /= N_Null
8504 and then Nkind (Else_Expr) /= N_Null
8506 Error_Msg_N ("cannot determine type of if expression", Cond);
8509 end Check_If_Expression;
8511 ------------------------
8512 -- Explain_Redundancy --
8513 ------------------------
8515 procedure Explain_Redundancy (N : Node_Id) is
8523 -- Strip the operand down to an entity
8526 if Nkind (Val) = N_Selected_Component then
8527 Val := Selector_Name (Val);
8533 -- The construct denotes an entity
8535 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8536 Val_Id := Entity (Val);
8538 -- Do not generate an error message when the comparison is done
8539 -- against the enumeration literal Standard.True.
8541 if Ekind (Val_Id) /= E_Enumeration_Literal then
8543 -- Build a customized error message
8546 Add_Str_To_Name_Buffer ("?r?");
8548 if Ekind (Val_Id) = E_Component then
8549 Add_Str_To_Name_Buffer ("component ");
8551 elsif Ekind (Val_Id) = E_Constant then
8552 Add_Str_To_Name_Buffer ("constant ");
8554 elsif Ekind (Val_Id) = E_Discriminant then
8555 Add_Str_To_Name_Buffer ("discriminant ");
8557 elsif Is_Formal (Val_Id) then
8558 Add_Str_To_Name_Buffer ("parameter ");
8560 elsif Ekind (Val_Id) = E_Variable then
8561 Add_Str_To_Name_Buffer ("variable ");
8564 Add_Str_To_Name_Buffer ("& is always True!");
8567 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8570 -- The construct is too complex to disect, issue a general message
8573 Error_Msg_N ("?r?expression is always True!", Val);
8575 end Explain_Redundancy;
8577 -----------------------------
8578 -- Find_Unique_Access_Type --
8579 -----------------------------
8581 function Find_Unique_Access_Type return Entity_Id is
8587 if Ekind (Etype (R)) in E_Allocator_Type | E_Access_Attribute_Type
8589 Acc := Designated_Type (Etype (R));
8591 elsif Ekind (Etype (L)) in E_Allocator_Type | E_Access_Attribute_Type
8593 Acc := Designated_Type (Etype (L));
8599 while S /= Standard_Standard loop
8600 E := First_Entity (S);
8601 while Present (E) loop
8603 and then Is_Access_Type (E)
8604 and then Ekind (E) /= E_Allocator_Type
8605 and then Designated_Type (E) = Base_Type (Acc)
8617 end Find_Unique_Access_Type;
8619 ----------------------------------
8620 -- Suspicious_Prio_For_Equality --
8621 ----------------------------------
8623 function Suspicious_Prio_For_Equality return Boolean is
8624 Par : constant Node_Id := Parent (N);
8627 -- Check if parent node is one of and/or/xor, not parenthesized
8628 -- explicitly, and its own parent is not of this kind. Otherwise,
8629 -- it's a case of chained Boolean conditions which is likely well
8632 if Nkind (Par) in N_Op_And | N_Op_Or | N_Op_Xor
8633 and then Paren_Count (N) = 0
8634 and then Nkind (Parent (Par)) not in N_Op_And | N_Op_Or | N_Op_Xor
8638 (if Left_Opnd (Par) = N then
8643 -- Compar may have been rewritten, for example from (a /= b)
8644 -- into not (a = b). Use the Original_Node instead.
8646 Compar := Original_Node (Compar);
8648 -- If the other argument of the and/or/xor is also a
8649 -- comparison, or another and/or/xor then most likely
8650 -- the priorities are correctly set.
8652 return Nkind (Compar) not in N_Op_Boolean;
8658 end Suspicious_Prio_For_Equality;
8660 -- Start of processing for Resolve_Equality_Op
8663 Set_Etype (N, Base_Type (Typ));
8664 Generate_Reference (T, N, ' ');
8666 if T = Any_Fixed then
8667 T := Unique_Fixed_Point_Type (L);
8670 if T /= Any_Type then
8671 if T = Any_String or else
8672 T = Any_Composite or else
8675 if T = Any_Character then
8676 Ambiguous_Character (L);
8678 Error_Msg_N ("ambiguous operands for equality", N);
8681 Set_Etype (N, Any_Type);
8684 elsif T = Any_Access
8685 or else Ekind (T) in E_Allocator_Type | E_Access_Attribute_Type
8687 T := Find_Unique_Access_Type;
8690 Error_Msg_N ("ambiguous operands for equality", N);
8691 Set_Etype (N, Any_Type);
8695 -- If expressions must have a single type, and if the context does
8696 -- not impose one the dependent expressions cannot be anonymous
8699 -- Why no similar processing for case expressions???
8701 elsif Ada_Version >= Ada_2012
8702 and then Is_Anonymous_Access_Type (Etype (L))
8703 and then Is_Anonymous_Access_Type (Etype (R))
8705 Check_If_Expression (L);
8706 Check_If_Expression (R);
8712 -- If the unique type is a class-wide type then it will be expanded
8713 -- into a dispatching call to the predefined primitive. Therefore we
8714 -- check here for potential violation of such restriction.
8716 if Is_Class_Wide_Type (T) then
8717 Check_Restriction (No_Dispatching_Calls, N);
8720 -- Only warn for redundant equality comparison to True for objects
8721 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
8722 -- other expressions, it may be a matter of preference to write
8723 -- "Expr = True" or "Expr".
8725 if Warn_On_Redundant_Constructs
8726 and then Comes_From_Source (N)
8727 and then Comes_From_Source (R)
8728 and then Is_Entity_Name (R)
8729 and then Entity (R) = Standard_True
8731 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
8735 Error_Msg_N -- CODEFIX
8736 ("?r?comparison with True is redundant!", N);
8737 Explain_Redundancy (Original_Node (R));
8740 -- Warn on a (in)equality between boolean values which is not
8741 -- parenthesized when the parent expression is one of and/or/xor, as
8742 -- this is interpreted as (a = b) op c where most likely a = (b op c)
8743 -- was intended. Do not generate a warning in generic instances, as
8744 -- the problematic expression may be implicitly parenthesized in
8745 -- the generic itself if one of the operators is a generic formal.
8746 -- Also do not generate a warning for generated equality, for
8747 -- example from rewritting a membership test.
8749 if Warn_On_Questionable_Missing_Parens
8750 and then not In_Instance
8751 and then Comes_From_Source (N)
8752 and then Is_Boolean_Type (T)
8753 and then Suspicious_Prio_For_Equality
8755 Error_Msg_N ("?q?equality should be parenthesized here!", N);
8758 -- If the equality is overloaded and the operands have resolved
8759 -- properly, set the proper equality operator on the node. The
8760 -- current setting is the first one found during analysis, which
8761 -- is not necessarily the one to which the node has resolved.
8763 if Is_Overloaded (N) then
8769 Get_First_Interp (N, I, It);
8771 -- If the equality is user-defined, the type of the operands
8772 -- matches that of the formals. For a predefined operator,
8773 -- it is the scope that matters, given that the predefined
8774 -- equality has Any_Type formals. In either case the result
8775 -- type (most often Boolean) must match the context. The scope
8776 -- is either that of the type, if there is a generated equality
8777 -- (when there is an equality for the component type), or else
8778 -- Standard otherwise.
8780 while Present (It.Typ) loop
8781 if Etype (It.Nam) = Typ
8783 (Etype (First_Entity (It.Nam)) = Etype (L)
8784 or else Scope (It.Nam) = Standard_Standard
8785 or else Scope (It.Nam) = Scope (T))
8787 Set_Entity (N, It.Nam);
8789 Set_Is_Overloaded (N, False);
8793 Get_Next_Interp (I, It);
8796 -- If expansion is active and this is an inherited operation,
8797 -- replace it with its ancestor. This must not be done during
8798 -- preanalysis because the type may not be frozen yet, as when
8799 -- the context is a precondition or postcondition.
8801 if Present (Alias (Entity (N))) and then Expander_Active then
8802 Set_Entity (N, Alias (Entity (N)));
8807 Check_Unset_Reference (L);
8808 Check_Unset_Reference (R);
8809 Generate_Operator_Reference (N, T);
8810 Check_Low_Bound_Tested (N);
8812 -- If this is an inequality, it may be the implicit inequality
8813 -- created for a user-defined operation, in which case the corres-
8814 -- ponding equality operation is not intrinsic, and the operation
8815 -- cannot be constant-folded. Else fold.
8817 if Nkind (N) = N_Op_Eq
8818 or else Comes_From_Source (Entity (N))
8819 or else Ekind (Entity (N)) = E_Operator
8820 or else Is_Intrinsic_Subprogram
8821 (Corresponding_Equality (Entity (N)))
8823 Analyze_Dimension (N);
8824 Eval_Relational_Op (N);
8826 elsif Nkind (N) = N_Op_Ne
8827 and then Is_Abstract_Subprogram (Entity (N))
8829 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
8832 -- Ada 2005: If one operand is an anonymous access type, convert the
8833 -- other operand to it, to ensure that the underlying types match in
8834 -- the back-end. Same for access_to_subprogram, and the conversion
8835 -- verifies that the types are subtype conformant.
8837 -- We apply the same conversion in the case one of the operands is a
8838 -- private subtype of the type of the other.
8840 -- Why the Expander_Active test here ???
8844 (Ekind (T) in E_Anonymous_Access_Type
8845 | E_Anonymous_Access_Subprogram_Type
8846 or else Is_Private_Type (T))
8848 if Etype (L) /= T then
8850 Make_Unchecked_Type_Conversion (Sloc (L),
8851 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
8852 Expression => Relocate_Node (L)));
8853 Analyze_And_Resolve (L, T);
8856 if (Etype (R)) /= T then
8858 Make_Unchecked_Type_Conversion (Sloc (R),
8859 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
8860 Expression => Relocate_Node (R)));
8861 Analyze_And_Resolve (R, T);
8865 end Resolve_Equality_Op;
8867 ----------------------------------
8868 -- Resolve_Explicit_Dereference --
8869 ----------------------------------
8871 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
8872 Loc : constant Source_Ptr := Sloc (N);
8874 P : constant Node_Id := Prefix (N);
8877 -- The candidate prefix type, if overloaded
8883 Check_Fully_Declared_Prefix (Typ, P);
8886 -- A useful optimization: check whether the dereference denotes an
8887 -- element of a container, and if so rewrite it as a call to the
8888 -- corresponding Element function.
8890 -- Disabled for now, on advice of ARG. A more restricted form of the
8891 -- predicate might be acceptable ???
8893 -- if Is_Container_Element (N) then
8897 if Is_Overloaded (P) then
8899 -- Use the context type to select the prefix that has the correct
8900 -- designated type. Keep the first match, which will be the inner-
8903 Get_First_Interp (P, I, It);
8905 while Present (It.Typ) loop
8906 if Is_Access_Type (It.Typ)
8907 and then Covers (Typ, Designated_Type (It.Typ))
8913 -- Remove access types that do not match, but preserve access
8914 -- to subprogram interpretations, in case a further dereference
8915 -- is needed (see below).
8917 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
8921 Get_Next_Interp (I, It);
8924 if Present (P_Typ) then
8926 Set_Etype (N, Designated_Type (P_Typ));
8929 -- If no interpretation covers the designated type of the prefix,
8930 -- this is the pathological case where not all implementations of
8931 -- the prefix allow the interpretation of the node as a call. Now
8932 -- that the expected type is known, Remove other interpretations
8933 -- from prefix, rewrite it as a call, and resolve again, so that
8934 -- the proper call node is generated.
8936 Get_First_Interp (P, I, It);
8937 while Present (It.Typ) loop
8938 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
8942 Get_Next_Interp (I, It);
8946 Make_Function_Call (Loc,
8948 Make_Explicit_Dereference (Loc,
8950 Parameter_Associations => New_List);
8952 Save_Interps (N, New_N);
8954 Analyze_And_Resolve (N, Typ);
8958 -- If not overloaded, resolve P with its own type
8964 -- If the prefix might be null, add an access check
8966 if Is_Access_Type (Etype (P))
8967 and then not Can_Never_Be_Null (Etype (P))
8969 Apply_Access_Check (N);
8972 -- If the designated type is a packed unconstrained array type, and the
8973 -- explicit dereference is not in the context of an attribute reference,
8974 -- then we must compute and set the actual subtype, since it is needed
8975 -- by Gigi. The reason we exclude the attribute case is that this is
8976 -- handled fine by Gigi, and in fact we use such attributes to build the
8977 -- actual subtype. We also exclude generated code (which builds actual
8978 -- subtypes directly if they are needed).
8980 if Is_Packed_Array (Etype (N))
8981 and then not Is_Constrained (Etype (N))
8982 and then Nkind (Parent (N)) /= N_Attribute_Reference
8983 and then Comes_From_Source (N)
8985 Set_Etype (N, Get_Actual_Subtype (N));
8988 Analyze_Dimension (N);
8990 -- Note: No Eval processing is required for an explicit dereference,
8991 -- because such a name can never be static.
8993 end Resolve_Explicit_Dereference;
8995 -------------------------------------
8996 -- Resolve_Expression_With_Actions --
8997 -------------------------------------
8999 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
9001 function OK_For_Static (Act : Node_Id) return Boolean;
9002 -- True if Act is an action of a declare_expression that is allowed in a
9003 -- static declare_expression.
9005 function All_OK_For_Static return Boolean;
9006 -- True if all actions of N are allowed in a static declare_expression.
9008 function Get_Literal (Expr : Node_Id) return Node_Id;
9009 -- Expr is an expression with compile-time-known value. This returns the
9010 -- literal node that reprsents that value.
9012 function OK_For_Static (Act : Node_Id) return Boolean is
9015 when N_Object_Declaration =>
9016 if Constant_Present (Act)
9017 and then Is_Static_Expression (Expression (Act))
9022 when N_Object_Renaming_Declaration =>
9023 if Statically_Names_Object (Name (Act)) then
9028 -- No other declarations, nor even pragmas, are allowed in a
9029 -- declare expression, so if we see something else, it must be
9030 -- an internally generated expression_with_actions.
9037 function All_OK_For_Static return Boolean is
9038 Act : Node_Id := First (Actions (N));
9040 while Present (Act) loop
9041 if not OK_For_Static (Act) then
9049 end All_OK_For_Static;
9051 function Get_Literal (Expr : Node_Id) return Node_Id is
9052 pragma Assert (Compile_Time_Known_Value (Expr));
9055 case Nkind (Expr) is
9056 when N_Has_Entity =>
9057 if Ekind (Entity (Expr)) = E_Enumeration_Literal then
9060 Result := Constant_Value (Entity (Expr));
9062 when N_Numeric_Or_String_Literal =>
9065 raise Program_Error;
9069 (Nkind (Result) in N_Numeric_Or_String_Literal
9070 or else Ekind (Entity (Result)) = E_Enumeration_Literal);
9074 Loc : constant Source_Ptr := Sloc (N);
9079 if Is_Empty_List (Actions (N)) then
9080 pragma Assert (All_OK_For_Static); null;
9083 -- If the value of the expression is known at compile time, and all
9084 -- of the actions (if any) are suitable, then replace the declare
9085 -- expression with its expression. This allows the declare expression
9086 -- as a whole to be static if appropriate. See AI12-0368.
9088 if Compile_Time_Known_Value (Expression (N)) then
9089 if Is_Empty_List (Actions (N)) then
9090 Rewrite (N, Expression (N));
9091 elsif All_OK_For_Static then
9094 (Get_Literal (Expression (N)), New_Sloc => Loc));
9097 end Resolve_Expression_With_Actions;
9099 ----------------------------------
9100 -- Resolve_Generalized_Indexing --
9101 ----------------------------------
9103 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
9104 Indexing : constant Node_Id := Generalized_Indexing (N);
9106 Rewrite (N, Indexing);
9108 end Resolve_Generalized_Indexing;
9110 ---------------------------
9111 -- Resolve_If_Expression --
9112 ---------------------------
9114 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
9115 procedure Apply_Check (Expr : Node_Id);
9116 -- When a dependent expression is of a subtype different from
9117 -- the context subtype, then insert a qualification to ensure
9118 -- the generation of a constraint check. This was previously
9119 -- for scalar types. For array types apply a length check, given
9120 -- that the context in general allows sliding, while a qualified
9121 -- expression forces equality of bounds.
9123 Result_Type : Entity_Id := Typ;
9124 -- So in most cases the type of the If_Expression and of its
9125 -- dependent expressions is that of the context. However, if
9126 -- the expression is the index of an Indexed_Component, we must
9127 -- ensure that a proper index check is applied, rather than a
9128 -- range check on the index type (which might be discriminant
9129 -- dependent). In this case we resolve with the base type of the
9130 -- index type, and the index check is generated in the resolution
9131 -- of the indexed_component above.
9137 procedure Apply_Check (Expr : Node_Id) is
9138 Expr_Typ : constant Entity_Id := Etype (Expr);
9139 Loc : constant Source_Ptr := Sloc (Expr);
9143 or else Is_Tagged_Type (Typ)
9144 or else Is_Access_Type (Typ)
9145 or else not Is_Constrained (Typ)
9146 or else Inside_A_Generic
9150 elsif Is_Array_Type (Typ) then
9151 Apply_Length_Check (Expr, Typ);
9155 Make_Qualified_Expression (Loc,
9156 Subtype_Mark => New_Occurrence_Of (Result_Type, Loc),
9157 Expression => Relocate_Node (Expr)));
9159 Analyze_And_Resolve (Expr, Result_Type);
9165 Condition : constant Node_Id := First (Expressions (N));
9166 Else_Expr : Node_Id;
9167 Then_Expr : Node_Id;
9169 -- Start of processing for Resolve_If_Expression
9172 -- Defend against malformed expressions
9174 if No (Condition) then
9178 if Nkind (Parent (N)) = N_Indexed_Component
9179 or else Nkind (Parent (Parent (N))) = N_Indexed_Component
9181 Result_Type := Base_Type (Typ);
9184 Then_Expr := Next (Condition);
9186 if No (Then_Expr) then
9190 Else_Expr := Next (Then_Expr);
9192 Resolve (Condition, Any_Boolean);
9193 Resolve (Then_Expr, Result_Type);
9194 Apply_Check (Then_Expr);
9196 -- If ELSE expression present, just resolve using the determined type
9197 -- If type is universal, resolve to any member of the class.
9199 if Present (Else_Expr) then
9200 if Typ = Universal_Integer then
9201 Resolve (Else_Expr, Any_Integer);
9203 elsif Typ = Universal_Real then
9204 Resolve (Else_Expr, Any_Real);
9207 Resolve (Else_Expr, Result_Type);
9210 Apply_Check (Else_Expr);
9212 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
9213 -- dynamically tagged must be known statically.
9215 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
9216 if Is_Dynamically_Tagged (Then_Expr) /=
9217 Is_Dynamically_Tagged (Else_Expr)
9219 Error_Msg_N ("all or none of the dependent expressions "
9220 & "can be dynamically tagged", N);
9224 -- If no ELSE expression is present, root type must be Standard.Boolean
9225 -- and we provide a Standard.True result converted to the appropriate
9226 -- Boolean type (in case it is a derived boolean type).
9228 elsif Root_Type (Typ) = Standard_Boolean then
9230 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
9231 Analyze_And_Resolve (Else_Expr, Result_Type);
9232 Append_To (Expressions (N), Else_Expr);
9235 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9236 Append_To (Expressions (N), Error);
9239 Set_Etype (N, Result_Type);
9241 if not Error_Posted (N) then
9242 Eval_If_Expression (N);
9245 Analyze_Dimension (N);
9246 end Resolve_If_Expression;
9248 ----------------------------------
9249 -- Resolve_Implicit_Dereference --
9250 ----------------------------------
9252 procedure Resolve_Implicit_Dereference (P : Node_Id) is
9253 Desig_Typ : Entity_Id;
9256 -- In an instance the proper view may not always be correct for
9257 -- private types, see e.g. Sem_Type.Covers for similar handling.
9259 if Is_Private_Type (Etype (P))
9260 and then Present (Full_View (Etype (P)))
9261 and then Is_Access_Type (Full_View (Etype (P)))
9262 and then In_Instance
9264 Set_Etype (P, Full_View (Etype (P)));
9267 if Is_Access_Type (Etype (P)) then
9268 Desig_Typ := Implicitly_Designated_Type (Etype (P));
9269 Insert_Explicit_Dereference (P);
9270 Analyze_And_Resolve (P, Desig_Typ);
9272 end Resolve_Implicit_Dereference;
9274 -------------------------------
9275 -- Resolve_Indexed_Component --
9276 -------------------------------
9278 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9279 Name : constant Node_Id := Prefix (N);
9281 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9285 if Present (Generalized_Indexing (N)) then
9286 Resolve_Generalized_Indexing (N, Typ);
9290 if Is_Overloaded (Name) then
9292 -- Use the context type to select the prefix that yields the correct
9298 I1 : Interp_Index := 0;
9299 P : constant Node_Id := Prefix (N);
9300 Found : Boolean := False;
9303 Get_First_Interp (P, I, It);
9304 while Present (It.Typ) loop
9305 if (Is_Array_Type (It.Typ)
9306 and then Covers (Typ, Component_Type (It.Typ)))
9307 or else (Is_Access_Type (It.Typ)
9308 and then Is_Array_Type (Designated_Type (It.Typ))
9312 Component_Type (Designated_Type (It.Typ))))
9315 It := Disambiguate (P, I1, I, Any_Type);
9317 if It = No_Interp then
9318 Error_Msg_N ("ambiguous prefix for indexing", N);
9324 Array_Type := It.Typ;
9330 Array_Type := It.Typ;
9335 Get_Next_Interp (I, It);
9340 Array_Type := Etype (Name);
9343 Resolve (Name, Array_Type);
9344 Array_Type := Get_Actual_Subtype_If_Available (Name);
9346 -- If the prefix's type is an access type, get to the real array type.
9347 -- Note: we do not apply an access check because an explicit dereference
9348 -- will be introduced later, and the check will happen there.
9350 if Is_Access_Type (Array_Type) then
9351 Array_Type := Implicitly_Designated_Type (Array_Type);
9354 -- If name was overloaded, set component type correctly now.
9355 -- If a misplaced call to an entry family (which has no index types)
9356 -- return. Error will be diagnosed from calling context.
9358 if Is_Array_Type (Array_Type) then
9359 Set_Etype (N, Component_Type (Array_Type));
9364 Index := First_Index (Array_Type);
9365 Expr := First (Expressions (N));
9367 -- The prefix may have resolved to a string literal, in which case its
9368 -- etype has a special representation. This is only possible currently
9369 -- if the prefix is a static concatenation, written in functional
9372 if Ekind (Array_Type) = E_String_Literal_Subtype then
9373 Resolve (Expr, Standard_Positive);
9376 while Present (Index) and then Present (Expr) loop
9377 Resolve (Expr, Etype (Index));
9378 Check_Unset_Reference (Expr);
9380 Apply_Scalar_Range_Check (Expr, Etype (Index));
9387 Resolve_Implicit_Dereference (Prefix (N));
9388 Analyze_Dimension (N);
9390 -- Do not generate the warning on suspicious index if we are analyzing
9391 -- package Ada.Tags; otherwise we will report the warning with the
9392 -- Prims_Ptr field of the dispatch table.
9394 if Scope (Etype (Prefix (N))) = Standard_Standard
9396 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
9399 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
9400 Eval_Indexed_Component (N);
9403 -- If the array type is atomic and the component is not, then this is
9404 -- worth a warning before Ada 2020, since we have a situation where the
9405 -- access to the component may cause extra read/writes of the atomic
9406 -- object, or partial word accesses, both of which may be unexpected.
9408 if Nkind (N) = N_Indexed_Component
9409 and then Is_Atomic_Ref_With_Address (N)
9410 and then not (Has_Atomic_Components (Array_Type)
9411 or else (Is_Entity_Name (Prefix (N))
9412 and then Has_Atomic_Components
9413 (Entity (Prefix (N)))))
9414 and then not Is_Atomic (Component_Type (Array_Type))
9415 and then Ada_Version < Ada_2020
9418 ("??access to non-atomic component of atomic array", Prefix (N));
9420 ("??\may cause unexpected accesses to atomic object", Prefix (N));
9422 end Resolve_Indexed_Component;
9424 -----------------------------
9425 -- Resolve_Integer_Literal --
9426 -----------------------------
9428 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9431 Eval_Integer_Literal (N);
9432 end Resolve_Integer_Literal;
9434 --------------------------------
9435 -- Resolve_Intrinsic_Operator --
9436 --------------------------------
9438 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9439 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9444 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9445 -- If the operand is a literal, it cannot be the expression in a
9446 -- conversion. Use a qualified expression instead.
9448 ---------------------
9449 -- Convert_Operand --
9450 ---------------------
9452 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9453 Loc : constant Source_Ptr := Sloc (Opnd);
9457 if Nkind (Opnd) in N_Integer_Literal | N_Real_Literal then
9459 Make_Qualified_Expression (Loc,
9460 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9461 Expression => Relocate_Node (Opnd));
9465 Res := Unchecked_Convert_To (Btyp, Opnd);
9469 end Convert_Operand;
9471 -- Start of processing for Resolve_Intrinsic_Operator
9474 -- We must preserve the original entity in a generic setting, so that
9475 -- the legality of the operation can be verified in an instance.
9477 if not Expander_Active then
9482 while Scope (Op) /= Standard_Standard loop
9484 pragma Assert (Present (Op));
9488 Set_Is_Overloaded (N, False);
9490 -- If the result or operand types are private, rewrite with unchecked
9491 -- conversions on the operands and the result, to expose the proper
9492 -- underlying numeric type.
9494 if Is_Private_Type (Typ)
9495 or else Is_Private_Type (Etype (Left_Opnd (N)))
9496 or else Is_Private_Type (Etype (Right_Opnd (N)))
9498 Arg1 := Convert_Operand (Left_Opnd (N));
9500 if Nkind (N) = N_Op_Expon then
9501 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9503 Arg2 := Convert_Operand (Right_Opnd (N));
9506 if Nkind (Arg1) = N_Type_Conversion then
9507 Save_Interps (Left_Opnd (N), Expression (Arg1));
9510 if Nkind (Arg2) = N_Type_Conversion then
9511 Save_Interps (Right_Opnd (N), Expression (Arg2));
9514 Set_Left_Opnd (N, Arg1);
9515 Set_Right_Opnd (N, Arg2);
9517 Set_Etype (N, Btyp);
9518 Rewrite (N, Unchecked_Convert_To (Typ, N));
9521 elsif Typ /= Etype (Left_Opnd (N))
9522 or else Typ /= Etype (Right_Opnd (N))
9524 -- Add explicit conversion where needed, and save interpretations in
9525 -- case operands are overloaded.
9527 Arg1 := Convert_To (Typ, Left_Opnd (N));
9528 Arg2 := Convert_To (Typ, Right_Opnd (N));
9530 if Nkind (Arg1) = N_Type_Conversion then
9531 Save_Interps (Left_Opnd (N), Expression (Arg1));
9533 Save_Interps (Left_Opnd (N), Arg1);
9536 if Nkind (Arg2) = N_Type_Conversion then
9537 Save_Interps (Right_Opnd (N), Expression (Arg2));
9539 Save_Interps (Right_Opnd (N), Arg2);
9542 Rewrite (Left_Opnd (N), Arg1);
9543 Rewrite (Right_Opnd (N), Arg2);
9546 Resolve_Arithmetic_Op (N, Typ);
9549 Resolve_Arithmetic_Op (N, Typ);
9551 end Resolve_Intrinsic_Operator;
9553 --------------------------------------
9554 -- Resolve_Intrinsic_Unary_Operator --
9555 --------------------------------------
9557 procedure Resolve_Intrinsic_Unary_Operator
9561 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9567 while Scope (Op) /= Standard_Standard loop
9569 pragma Assert (Present (Op));
9574 if Is_Private_Type (Typ) then
9575 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
9576 Save_Interps (Right_Opnd (N), Expression (Arg2));
9578 Set_Right_Opnd (N, Arg2);
9580 Set_Etype (N, Btyp);
9581 Rewrite (N, Unchecked_Convert_To (Typ, N));
9585 Resolve_Unary_Op (N, Typ);
9587 end Resolve_Intrinsic_Unary_Operator;
9589 ------------------------
9590 -- Resolve_Logical_Op --
9591 ------------------------
9593 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
9597 Check_No_Direct_Boolean_Operators (N);
9599 -- Predefined operations on scalar types yield the base type. On the
9600 -- other hand, logical operations on arrays yield the type of the
9601 -- arguments (and the context).
9603 if Is_Array_Type (Typ) then
9606 B_Typ := Base_Type (Typ);
9609 -- The following test is required because the operands of the operation
9610 -- may be literals, in which case the resulting type appears to be
9611 -- compatible with a signed integer type, when in fact it is compatible
9612 -- only with modular types. If the context itself is universal, the
9613 -- operation is illegal.
9615 if not Valid_Boolean_Arg (Typ) then
9616 Error_Msg_N ("invalid context for logical operation", N);
9617 Set_Etype (N, Any_Type);
9620 elsif Typ = Any_Modular then
9622 ("no modular type available in this context", N);
9623 Set_Etype (N, Any_Type);
9626 elsif Is_Modular_Integer_Type (Typ)
9627 and then Etype (Left_Opnd (N)) = Universal_Integer
9628 and then Etype (Right_Opnd (N)) = Universal_Integer
9630 Check_For_Visible_Operator (N, B_Typ);
9633 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
9634 -- is active and the result type is standard Boolean (do not mess with
9635 -- ops that return a nonstandard Boolean type, because something strange
9638 -- Note: you might expect this replacement to be done during expansion,
9639 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
9640 -- is used, no part of the right operand of an "and" or "or" operator
9641 -- should be executed if the left operand would short-circuit the
9642 -- evaluation of the corresponding "and then" or "or else". If we left
9643 -- the replacement to expansion time, then run-time checks associated
9644 -- with such operands would be evaluated unconditionally, due to being
9645 -- before the condition prior to the rewriting as short-circuit forms
9646 -- during expansion.
9648 if Short_Circuit_And_Or
9649 and then B_Typ = Standard_Boolean
9650 and then Nkind (N) in N_Op_And | N_Op_Or
9652 -- Mark the corresponding putative SCO operator as truly a logical
9653 -- (and short-circuit) operator.
9655 if Generate_SCO and then Comes_From_Source (N) then
9656 Set_SCO_Logical_Operator (N);
9659 if Nkind (N) = N_Op_And then
9661 Make_And_Then (Sloc (N),
9662 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9663 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9664 Analyze_And_Resolve (N, B_Typ);
9666 -- Case of OR changed to OR ELSE
9670 Make_Or_Else (Sloc (N),
9671 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9672 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9673 Analyze_And_Resolve (N, B_Typ);
9676 -- Return now, since analysis of the rewritten ops will take care of
9677 -- other reference bookkeeping and expression folding.
9682 Resolve (Left_Opnd (N), B_Typ);
9683 Resolve (Right_Opnd (N), B_Typ);
9685 Check_Unset_Reference (Left_Opnd (N));
9686 Check_Unset_Reference (Right_Opnd (N));
9688 Set_Etype (N, B_Typ);
9689 Generate_Operator_Reference (N, B_Typ);
9690 Eval_Logical_Op (N);
9691 end Resolve_Logical_Op;
9693 ---------------------------
9694 -- Resolve_Membership_Op --
9695 ---------------------------
9697 -- The context can only be a boolean type, and does not determine the
9698 -- arguments. Arguments should be unambiguous, but the preference rule for
9699 -- universal types applies.
9701 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
9702 pragma Assert (Is_Boolean_Type (Typ));
9704 L : constant Node_Id := Left_Opnd (N);
9705 R : constant Node_Id := Right_Opnd (N);
9708 procedure Resolve_Set_Membership;
9709 -- Analysis has determined a unique type for the left operand. Use it as
9710 -- the basis to resolve the disjuncts.
9712 ----------------------------
9713 -- Resolve_Set_Membership --
9714 ----------------------------
9716 procedure Resolve_Set_Membership is
9720 -- If the left operand is overloaded, find type compatible with not
9721 -- overloaded alternative of the right operand.
9723 Alt := First (Alternatives (N));
9724 if Is_Overloaded (L) then
9726 while Present (Alt) loop
9727 if not Is_Overloaded (Alt) then
9728 T := Intersect_Types (L, Alt);
9735 -- Unclear how to resolve expression if all alternatives are also
9739 Error_Msg_N ("ambiguous expression", N);
9743 T := Intersect_Types (L, Alt);
9748 Alt := First (Alternatives (N));
9749 while Present (Alt) loop
9751 -- Alternative is an expression, a range
9752 -- or a subtype mark.
9754 if not Is_Entity_Name (Alt)
9755 or else not Is_Type (Entity (Alt))
9763 -- Check for duplicates for discrete case
9765 if Is_Discrete_Type (T) then
9772 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
9776 -- Loop checking duplicates. This is quadratic, but giant sets
9777 -- are unlikely in this context so it's a reasonable choice.
9780 Alt := First (Alternatives (N));
9781 while Present (Alt) loop
9782 if Is_OK_Static_Expression (Alt)
9783 and then Nkind (Alt) in N_Integer_Literal
9784 | N_Character_Literal
9788 Alts (Nalts) := (Alt, Expr_Value (Alt));
9790 for J in 1 .. Nalts - 1 loop
9791 if Alts (J).Val = Alts (Nalts).Val then
9792 Error_Msg_Sloc := Sloc (Alts (J).Alt);
9793 Error_Msg_N ("duplicate of value given#??", Alt);
9803 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
9804 -- limited types, evaluation of a membership test uses the predefined
9805 -- equality for the type. This may be confusing to users, and the
9806 -- following warning appears useful for the most common case.
9808 if Is_Scalar_Type (Etype (L))
9809 and then Present (Get_User_Defined_Eq (Etype (L)))
9812 ("membership test on& uses predefined equality?", N, Etype (L));
9814 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
9816 end Resolve_Set_Membership;
9818 -- Start of processing for Resolve_Membership_Op
9821 if L = Error or else R = Error then
9825 if Present (Alternatives (N)) then
9826 Resolve_Set_Membership;
9829 elsif not Is_Overloaded (R)
9830 and then Is_Universal_Numeric_Type (Etype (R))
9831 and then Is_Overloaded (L)
9835 -- Ada 2005 (AI-251): Support the following case:
9837 -- type I is interface;
9838 -- type T is tagged ...
9840 -- function Test (O : I'Class) is
9842 -- return O in T'Class.
9845 -- In this case we have nothing else to do. The membership test will be
9846 -- done at run time.
9848 elsif Ada_Version >= Ada_2005
9849 and then Is_Class_Wide_Type (Etype (L))
9850 and then Is_Interface (Etype (L))
9851 and then not Is_Interface (Etype (R))
9855 T := Intersect_Types (L, R);
9858 -- If mixed-mode operations are present and operands are all literal,
9859 -- the only interpretation involves Duration, which is probably not
9860 -- the intention of the programmer.
9862 if T = Any_Fixed then
9863 T := Unique_Fixed_Point_Type (N);
9865 if T = Any_Type then
9871 Check_Unset_Reference (L);
9873 if Nkind (R) = N_Range
9874 and then not Is_Scalar_Type (T)
9876 Error_Msg_N ("scalar type required for range", R);
9879 if Is_Entity_Name (R) then
9880 Freeze_Expression (R);
9883 Check_Unset_Reference (R);
9886 -- Here after resolving membership operation
9890 Eval_Membership_Op (N);
9891 end Resolve_Membership_Op;
9897 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
9898 Loc : constant Source_Ptr := Sloc (N);
9901 -- Handle restriction against anonymous null access values This
9902 -- restriction can be turned off using -gnatdj.
9904 -- Ada 2005 (AI-231): Remove restriction
9906 if Ada_Version < Ada_2005
9907 and then not Debug_Flag_J
9908 and then Ekind (Typ) = E_Anonymous_Access_Type
9909 and then Comes_From_Source (N)
9911 -- In the common case of a call which uses an explicitly null value
9912 -- for an access parameter, give specialized error message.
9914 if Nkind (Parent (N)) in N_Subprogram_Call then
9916 ("NULL is not allowed as argument for an access parameter", N);
9918 -- Standard message for all other cases (are there any?)
9922 ("NULL cannot be of an anonymous access type", N);
9926 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
9927 -- assignment to a null-excluding object.
9929 if Ada_Version >= Ada_2005
9930 and then Can_Never_Be_Null (Typ)
9931 and then Nkind (Parent (N)) = N_Assignment_Statement
9933 if Inside_Init_Proc then
9935 -- Decide whether to generate an if_statement around our
9936 -- null-excluding check to avoid them on certain internal object
9937 -- declarations by looking at the type the current Init_Proc
9941 -- if T1b_skip_null_excluding_check then
9942 -- [constraint_error "access check failed"]
9945 if Needs_Conditional_Null_Excluding_Check
9946 (Etype (First_Formal (Enclosing_Init_Proc)))
9949 Make_If_Statement (Loc,
9951 Make_Identifier (Loc,
9953 (Chars (Typ), "_skip_null_excluding_check")),
9956 Make_Raise_Constraint_Error (Loc,
9957 Reason => CE_Access_Check_Failed))));
9959 -- Otherwise, simply create the check
9963 Make_Raise_Constraint_Error (Loc,
9964 Reason => CE_Access_Check_Failed));
9968 (Compile_Time_Constraint_Error (N,
9969 "(Ada 2005) NULL not allowed in null-excluding objects??"),
9970 Make_Raise_Constraint_Error (Loc,
9971 Reason => CE_Access_Check_Failed));
9975 -- In a distributed context, null for a remote access to subprogram may
9976 -- need to be replaced with a special record aggregate. In this case,
9977 -- return after having done the transformation.
9979 if (Ekind (Typ) = E_Record_Type
9980 or else Is_Remote_Access_To_Subprogram_Type (Typ))
9981 and then Remote_AST_Null_Value (N, Typ)
9986 -- The null literal takes its type from the context
9991 -----------------------
9992 -- Resolve_Op_Concat --
9993 -----------------------
9995 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
9997 -- We wish to avoid deep recursion, because concatenations are often
9998 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
9999 -- operands nonrecursively until we find something that is not a simple
10000 -- concatenation (A in this case). We resolve that, and then walk back
10001 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
10002 -- to do the rest of the work at each level. The Parent pointers allow
10003 -- us to avoid recursion, and thus avoid running out of memory. See also
10004 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
10010 -- The following code is equivalent to:
10012 -- Resolve_Op_Concat_First (NN, Typ);
10013 -- Resolve_Op_Concat_Arg (N, ...);
10014 -- Resolve_Op_Concat_Rest (N, Typ);
10016 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
10017 -- operand is a concatenation.
10019 -- Walk down left operands
10022 Resolve_Op_Concat_First (NN, Typ);
10023 Op1 := Left_Opnd (NN);
10024 exit when not (Nkind (Op1) = N_Op_Concat
10025 and then not Is_Array_Type (Component_Type (Typ))
10026 and then Entity (Op1) = Entity (NN));
10030 -- Now (given the above example) NN is A&B and Op1 is A
10032 -- First resolve Op1 ...
10034 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
10036 -- ... then walk NN back up until we reach N (where we started), calling
10037 -- Resolve_Op_Concat_Rest along the way.
10040 Resolve_Op_Concat_Rest (NN, Typ);
10044 end Resolve_Op_Concat;
10046 ---------------------------
10047 -- Resolve_Op_Concat_Arg --
10048 ---------------------------
10050 procedure Resolve_Op_Concat_Arg
10056 Btyp : constant Entity_Id := Base_Type (Typ);
10057 Ctyp : constant Entity_Id := Component_Type (Typ);
10060 if In_Instance then
10062 or else (not Is_Overloaded (Arg)
10063 and then Etype (Arg) /= Any_Composite
10064 and then Covers (Ctyp, Etype (Arg)))
10066 Resolve (Arg, Ctyp);
10068 Resolve (Arg, Btyp);
10071 -- If both Array & Array and Array & Component are visible, there is a
10072 -- potential ambiguity that must be reported.
10074 elsif Has_Compatible_Type (Arg, Ctyp) then
10075 if Nkind (Arg) = N_Aggregate
10076 and then Is_Composite_Type (Ctyp)
10078 if Is_Private_Type (Ctyp) then
10079 Resolve (Arg, Btyp);
10081 -- If the operation is user-defined and not overloaded use its
10082 -- profile. The operation may be a renaming, in which case it has
10083 -- been rewritten, and we want the original profile.
10085 elsif not Is_Overloaded (N)
10086 and then Comes_From_Source (Entity (Original_Node (N)))
10087 and then Ekind (Entity (Original_Node (N))) = E_Function
10091 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
10094 -- Otherwise an aggregate may match both the array type and the
10098 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
10099 Set_Etype (Arg, Any_Type);
10103 if Is_Overloaded (Arg)
10104 and then Has_Compatible_Type (Arg, Typ)
10105 and then Etype (Arg) /= Any_Type
10113 Get_First_Interp (Arg, I, It);
10115 Get_Next_Interp (I, It);
10117 -- Special-case the error message when the overloading is
10118 -- caused by a function that yields an array and can be
10119 -- called without parameters.
10121 if It.Nam = Func then
10122 Error_Msg_Sloc := Sloc (Func);
10123 Error_Msg_N ("ambiguous call to function#", Arg);
10125 ("\\interpretation as call yields&", Arg, Typ);
10127 ("\\interpretation as indexing of call yields&",
10128 Arg, Component_Type (Typ));
10131 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
10133 Get_First_Interp (Arg, I, It);
10134 while Present (It.Nam) loop
10135 Error_Msg_Sloc := Sloc (It.Nam);
10137 if Base_Type (It.Typ) = Btyp
10139 Base_Type (It.Typ) = Base_Type (Ctyp)
10141 Error_Msg_N -- CODEFIX
10142 ("\\possible interpretation#", Arg);
10145 Get_Next_Interp (I, It);
10151 Resolve (Arg, Component_Type (Typ));
10153 if Nkind (Arg) = N_String_Literal then
10154 Set_Etype (Arg, Component_Type (Typ));
10157 if Arg = Left_Opnd (N) then
10158 Set_Is_Component_Left_Opnd (N);
10160 Set_Is_Component_Right_Opnd (N);
10165 Resolve (Arg, Btyp);
10168 Check_Unset_Reference (Arg);
10169 end Resolve_Op_Concat_Arg;
10171 -----------------------------
10172 -- Resolve_Op_Concat_First --
10173 -----------------------------
10175 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
10176 Btyp : constant Entity_Id := Base_Type (Typ);
10177 Op1 : constant Node_Id := Left_Opnd (N);
10178 Op2 : constant Node_Id := Right_Opnd (N);
10181 -- The parser folds an enormous sequence of concatenations of string
10182 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
10183 -- in the right operand. If the expression resolves to a predefined "&"
10184 -- operator, all is well. Otherwise, the parser's folding is wrong, so
10185 -- we give an error. See P_Simple_Expression in Par.Ch4.
10187 if Nkind (Op2) = N_String_Literal
10188 and then Is_Folded_In_Parser (Op2)
10189 and then Ekind (Entity (N)) = E_Function
10191 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10192 and then String_Length (Strval (Op1)) = 0);
10193 Error_Msg_N ("too many user-defined concatenations", N);
10197 Set_Etype (N, Btyp);
10199 if Is_Limited_Composite (Btyp) then
10200 Error_Msg_N ("concatenation not available for limited array", N);
10201 Explain_Limited_Type (Btyp, N);
10203 end Resolve_Op_Concat_First;
10205 ----------------------------
10206 -- Resolve_Op_Concat_Rest --
10207 ----------------------------
10209 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10210 Op1 : constant Node_Id := Left_Opnd (N);
10211 Op2 : constant Node_Id := Right_Opnd (N);
10214 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10216 Generate_Operator_Reference (N, Typ);
10218 if Is_String_Type (Typ) then
10219 Eval_Concatenation (N);
10222 -- If this is not a static concatenation, but the result is a string
10223 -- type (and not an array of strings) ensure that static string operands
10224 -- have their subtypes properly constructed.
10226 if Nkind (N) /= N_String_Literal
10227 and then Is_Character_Type (Component_Type (Typ))
10229 Set_String_Literal_Subtype (Op1, Typ);
10230 Set_String_Literal_Subtype (Op2, Typ);
10232 end Resolve_Op_Concat_Rest;
10234 ----------------------
10235 -- Resolve_Op_Expon --
10236 ----------------------
10238 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10239 B_Typ : constant Entity_Id := Base_Type (Typ);
10242 -- Catch attempts to do fixed-point exponentiation with universal
10243 -- operands, which is a case where the illegality is not caught during
10244 -- normal operator analysis. This is not done in preanalysis mode
10245 -- since the tree is not fully decorated during preanalysis.
10247 if Full_Analysis then
10248 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10249 Error_Msg_N ("exponentiation not available for fixed point", N);
10252 elsif Nkind (Parent (N)) in N_Op
10253 and then Present (Etype (Parent (N)))
10254 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10255 and then Etype (N) = Universal_Real
10256 and then Comes_From_Source (N)
10258 Error_Msg_N ("exponentiation not available for fixed point", N);
10263 if Comes_From_Source (N)
10264 and then Ekind (Entity (N)) = E_Function
10265 and then Is_Imported (Entity (N))
10266 and then Is_Intrinsic_Subprogram (Entity (N))
10268 Resolve_Intrinsic_Operator (N, Typ);
10272 if Is_Universal_Numeric_Type (Etype (Left_Opnd (N))) then
10273 Check_For_Visible_Operator (N, B_Typ);
10276 -- We do the resolution using the base type, because intermediate values
10277 -- in expressions are always of the base type, not a subtype of it.
10279 Resolve (Left_Opnd (N), B_Typ);
10280 Resolve (Right_Opnd (N), Standard_Integer);
10282 -- For integer types, right argument must be in Natural range
10284 if Is_Integer_Type (Typ) then
10285 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10288 Check_Unset_Reference (Left_Opnd (N));
10289 Check_Unset_Reference (Right_Opnd (N));
10291 Set_Etype (N, B_Typ);
10292 Generate_Operator_Reference (N, B_Typ);
10294 Analyze_Dimension (N);
10296 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10297 -- Evaluate the exponentiation operator for dimensioned type
10299 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10304 -- Set overflow checking bit. Much cleverer code needed here eventually
10305 -- and perhaps the Resolve routines should be separated for the various
10306 -- arithmetic operations, since they will need different processing. ???
10308 if Nkind (N) in N_Op then
10309 if not Overflow_Checks_Suppressed (Etype (N)) then
10310 Enable_Overflow_Check (N);
10313 end Resolve_Op_Expon;
10315 --------------------
10316 -- Resolve_Op_Not --
10317 --------------------
10319 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10320 function Parent_Is_Boolean return Boolean;
10321 -- This function determines if the parent node is a boolean operator or
10322 -- operation (comparison op, membership test, or short circuit form) and
10323 -- the not in question is the left operand of this operation. Note that
10324 -- if the not is in parens, then false is returned.
10326 -----------------------
10327 -- Parent_Is_Boolean --
10328 -----------------------
10330 function Parent_Is_Boolean return Boolean is
10332 return Paren_Count (N) = 0
10333 and then Nkind (Parent (N)) in N_Membership_Test
10336 and then Left_Opnd (Parent (N)) = N;
10337 end Parent_Is_Boolean;
10343 -- Start of processing for Resolve_Op_Not
10346 -- Predefined operations on scalar types yield the base type. On the
10347 -- other hand, logical operations on arrays yield the type of the
10348 -- arguments (and the context).
10350 if Is_Array_Type (Typ) then
10353 B_Typ := Base_Type (Typ);
10356 -- Straightforward case of incorrect arguments
10358 if not Valid_Boolean_Arg (Typ) then
10359 Error_Msg_N ("invalid operand type for operator&", N);
10360 Set_Etype (N, Any_Type);
10363 -- Special case of probable missing parens
10365 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10366 if Parent_Is_Boolean then
10368 ("operand of NOT must be enclosed in parentheses",
10372 ("no modular type available in this context", N);
10375 Set_Etype (N, Any_Type);
10378 -- OK resolution of NOT
10381 -- Warn if non-boolean types involved. This is a case like not a < b
10382 -- where a and b are modular, where we will get (not a) < b and most
10383 -- likely not (a < b) was intended.
10385 if Warn_On_Questionable_Missing_Parens
10386 and then not Is_Boolean_Type (Typ)
10387 and then Parent_Is_Boolean
10389 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10392 -- Warn on double negation if checking redundant constructs
10394 if Warn_On_Redundant_Constructs
10395 and then Comes_From_Source (N)
10396 and then Comes_From_Source (Right_Opnd (N))
10397 and then Root_Type (Typ) = Standard_Boolean
10398 and then Nkind (Right_Opnd (N)) = N_Op_Not
10400 Error_Msg_N ("redundant double negation?r?", N);
10403 -- Complete resolution and evaluation of NOT
10404 -- If argument is an equality and expected type is boolean, that
10405 -- expected type has no effect on resolution, and there are
10406 -- special rules for resolution of Eq, Neq in the presence of
10407 -- overloaded operands, so we directly call its resolution routines.
10410 Opnd : constant Node_Id := Right_Opnd (N);
10414 if B_Typ = Standard_Boolean
10415 and then Nkind (Opnd) in N_Op_Eq | N_Op_Ne
10416 and then Is_Overloaded (Opnd)
10418 Resolve_Equality_Op (Opnd, B_Typ);
10419 Op_Id := Entity (Opnd);
10421 if Ekind (Op_Id) = E_Function
10422 and then not Is_Intrinsic_Subprogram (Op_Id)
10424 Rewrite_Operator_As_Call (Opnd, Op_Id);
10427 if not Inside_A_Generic or else Is_Entity_Name (Opnd) then
10428 Freeze_Expression (Opnd);
10434 Resolve (Opnd, B_Typ);
10437 Check_Unset_Reference (Opnd);
10440 Set_Etype (N, B_Typ);
10441 Generate_Operator_Reference (N, B_Typ);
10444 end Resolve_Op_Not;
10446 -----------------------------
10447 -- Resolve_Operator_Symbol --
10448 -----------------------------
10450 -- Nothing to be done, all resolved already
10452 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
10453 pragma Warnings (Off, N);
10454 pragma Warnings (Off, Typ);
10458 end Resolve_Operator_Symbol;
10460 ----------------------------------
10461 -- Resolve_Qualified_Expression --
10462 ----------------------------------
10464 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
10465 pragma Warnings (Off, Typ);
10467 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
10468 Expr : constant Node_Id := Expression (N);
10471 Resolve (Expr, Target_Typ);
10473 -- A qualified expression requires an exact match of the type, class-
10474 -- wide matching is not allowed. However, if the qualifying type is
10475 -- specific and the expression has a class-wide type, it may still be
10476 -- okay, since it can be the result of the expansion of a call to a
10477 -- dispatching function, so we also have to check class-wideness of the
10478 -- type of the expression's original node.
10480 if (Is_Class_Wide_Type (Target_Typ)
10482 (Is_Class_Wide_Type (Etype (Expr))
10483 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
10484 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
10486 Wrong_Type (Expr, Target_Typ);
10489 -- If the target type is unconstrained, then we reset the type of the
10490 -- result from the type of the expression. For other cases, the actual
10491 -- subtype of the expression is the target type. But we avoid doing it
10492 -- for an allocator since this is not needed and might be problematic.
10494 if Is_Composite_Type (Target_Typ)
10495 and then not Is_Constrained (Target_Typ)
10496 and then Nkind (Parent (N)) /= N_Allocator
10498 Set_Etype (N, Etype (Expr));
10501 Analyze_Dimension (N);
10502 Eval_Qualified_Expression (N);
10504 -- If we still have a qualified expression after the static evaluation,
10505 -- then apply a scalar range check if needed. The reason that we do this
10506 -- after the Eval call is that otherwise, the application of the range
10507 -- check may convert an illegal static expression and result in warning
10508 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
10510 if Nkind (N) = N_Qualified_Expression
10511 and then Is_Scalar_Type (Target_Typ)
10513 Apply_Scalar_Range_Check (Expr, Target_Typ);
10516 -- AI12-0100: Once the qualified expression is resolved, check whether
10517 -- operand statisfies a static predicate of the target subtype, if any.
10518 -- In the static expression case, a predicate check failure is an error.
10520 if Has_Predicates (Target_Typ) then
10521 Check_Expression_Against_Static_Predicate
10522 (Expr, Target_Typ, Static_Failure_Is_Error => True);
10524 end Resolve_Qualified_Expression;
10526 ------------------------------
10527 -- Resolve_Raise_Expression --
10528 ------------------------------
10530 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
10532 if Typ = Raise_Type then
10533 Error_Msg_N ("cannot find unique type for raise expression", N);
10534 Set_Etype (N, Any_Type);
10536 Set_Etype (N, Typ);
10538 end Resolve_Raise_Expression;
10540 -------------------
10541 -- Resolve_Range --
10542 -------------------
10544 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
10545 L : constant Node_Id := Low_Bound (N);
10546 H : constant Node_Id := High_Bound (N);
10548 function First_Last_Ref return Boolean;
10549 -- Returns True if N is of the form X'First .. X'Last where X is the
10550 -- same entity for both attributes.
10552 --------------------
10553 -- First_Last_Ref --
10554 --------------------
10556 function First_Last_Ref return Boolean is
10557 Lorig : constant Node_Id := Original_Node (L);
10558 Horig : constant Node_Id := Original_Node (H);
10561 if Nkind (Lorig) = N_Attribute_Reference
10562 and then Nkind (Horig) = N_Attribute_Reference
10563 and then Attribute_Name (Lorig) = Name_First
10564 and then Attribute_Name (Horig) = Name_Last
10567 PL : constant Node_Id := Prefix (Lorig);
10568 PH : constant Node_Id := Prefix (Horig);
10570 return Is_Entity_Name (PL)
10571 and then Is_Entity_Name (PH)
10572 and then Entity (PL) = Entity (PH);
10577 end First_Last_Ref;
10579 -- Start of processing for Resolve_Range
10582 Set_Etype (N, Typ);
10587 -- Reanalyze the lower bound after both bounds have been analyzed, so
10588 -- that the range is known to be static or not by now. This may trigger
10589 -- more compile-time evaluation, which is useful for static analysis
10590 -- with GNATprove. This is not needed for compilation or static analysis
10591 -- with CodePeer, as full expansion does that evaluation then.
10593 if GNATprove_Mode then
10594 Set_Analyzed (L, False);
10598 -- Check for inappropriate range on unordered enumeration type
10600 if Bad_Unordered_Enumeration_Reference (N, Typ)
10602 -- Exclude X'First .. X'Last if X is the same entity for both
10604 and then not First_Last_Ref
10606 Error_Msg_Sloc := Sloc (Typ);
10608 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
10611 Check_Unset_Reference (L);
10612 Check_Unset_Reference (H);
10614 -- We have to check the bounds for being within the base range as
10615 -- required for a non-static context. Normally this is automatic and
10616 -- done as part of evaluating expressions, but the N_Range node is an
10617 -- exception, since in GNAT we consider this node to be a subexpression,
10618 -- even though in Ada it is not. The circuit in Sem_Eval could check for
10619 -- this, but that would put the test on the main evaluation path for
10622 Check_Non_Static_Context (L);
10623 Check_Non_Static_Context (H);
10625 -- Check for an ambiguous range over character literals. This will
10626 -- happen with a membership test involving only literals.
10628 if Typ = Any_Character then
10629 Ambiguous_Character (L);
10630 Set_Etype (N, Any_Type);
10634 -- If bounds are static, constant-fold them, so size computations are
10635 -- identical between front-end and back-end. Do not perform this
10636 -- transformation while analyzing generic units, as type information
10637 -- would be lost when reanalyzing the constant node in the instance.
10639 if Is_Discrete_Type (Typ) and then Expander_Active then
10640 if Is_OK_Static_Expression (L) then
10641 Fold_Uint (L, Expr_Value (L), Static => True);
10644 if Is_OK_Static_Expression (H) then
10645 Fold_Uint (H, Expr_Value (H), Static => True);
10650 --------------------------
10651 -- Resolve_Real_Literal --
10652 --------------------------
10654 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
10655 Actual_Typ : constant Entity_Id := Etype (N);
10658 -- Special processing for fixed-point literals to make sure that the
10659 -- value is an exact multiple of small where this is required. We skip
10660 -- this for the universal real case, and also for generic types.
10662 if Is_Fixed_Point_Type (Typ)
10663 and then Typ /= Universal_Fixed
10664 and then Typ /= Any_Fixed
10665 and then not Is_Generic_Type (Typ)
10668 Val : constant Ureal := Realval (N);
10669 Cintr : constant Ureal := Val / Small_Value (Typ);
10670 Cint : constant Uint := UR_Trunc (Cintr);
10671 Den : constant Uint := Norm_Den (Cintr);
10675 -- Case of literal is not an exact multiple of the Small
10679 -- For a source program literal for a decimal fixed-point type,
10680 -- this is statically illegal (RM 4.9(36)).
10682 if Is_Decimal_Fixed_Point_Type (Typ)
10683 and then Actual_Typ = Universal_Real
10684 and then Comes_From_Source (N)
10686 Error_Msg_N ("value has extraneous low order digits", N);
10689 -- Generate a warning if literal from source
10691 if Is_OK_Static_Expression (N)
10692 and then Warn_On_Bad_Fixed_Value
10695 ("?b?static fixed-point value is not a multiple of Small!",
10699 -- Replace literal by a value that is the exact representation
10700 -- of a value of the type, i.e. a multiple of the small value,
10701 -- by truncation, since Machine_Rounds is false for all GNAT
10702 -- fixed-point types (RM 4.9(38)).
10704 Stat := Is_OK_Static_Expression (N);
10706 Make_Real_Literal (Sloc (N),
10707 Realval => Small_Value (Typ) * Cint));
10709 Set_Is_Static_Expression (N, Stat);
10712 -- In all cases, set the corresponding integer field
10714 Set_Corresponding_Integer_Value (N, Cint);
10718 -- Now replace the actual type by the expected type as usual
10720 Set_Etype (N, Typ);
10721 Eval_Real_Literal (N);
10722 end Resolve_Real_Literal;
10724 -----------------------
10725 -- Resolve_Reference --
10726 -----------------------
10728 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
10729 P : constant Node_Id := Prefix (N);
10732 -- Replace general access with specific type
10734 if Ekind (Etype (N)) = E_Allocator_Type then
10735 Set_Etype (N, Base_Type (Typ));
10738 Resolve (P, Designated_Type (Etype (N)));
10740 -- If we are taking the reference of a volatile entity, then treat it as
10741 -- a potential modification of this entity. This is too conservative,
10742 -- but necessary because remove side effects can cause transformations
10743 -- of normal assignments into reference sequences that otherwise fail to
10744 -- notice the modification.
10746 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
10747 Note_Possible_Modification (P, Sure => False);
10749 end Resolve_Reference;
10751 --------------------------------
10752 -- Resolve_Selected_Component --
10753 --------------------------------
10755 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
10757 Comp1 : Entity_Id := Empty; -- prevent junk warning
10758 P : constant Node_Id := Prefix (N);
10759 S : constant Node_Id := Selector_Name (N);
10760 T : Entity_Id := Etype (P);
10762 I1 : Interp_Index := 0; -- prevent junk warning
10767 function Init_Component return Boolean;
10768 -- Check whether this is the initialization of a component within an
10769 -- init proc (by assignment or call to another init proc). If true,
10770 -- there is no need for a discriminant check.
10772 --------------------
10773 -- Init_Component --
10774 --------------------
10776 function Init_Component return Boolean is
10778 return Inside_Init_Proc
10779 and then Nkind (Prefix (N)) = N_Identifier
10780 and then Chars (Prefix (N)) = Name_uInit
10781 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
10782 end Init_Component;
10784 -- Start of processing for Resolve_Selected_Component
10787 if Is_Overloaded (P) then
10789 -- Use the context type to select the prefix that has a selector
10790 -- of the correct name and type.
10793 Get_First_Interp (P, I, It);
10795 Search : while Present (It.Typ) loop
10796 if Is_Access_Type (It.Typ) then
10797 T := Designated_Type (It.Typ);
10802 -- Locate selected component. For a private prefix the selector
10803 -- can denote a discriminant.
10805 if Is_Record_Type (T) or else Is_Private_Type (T) then
10807 -- The visible components of a class-wide type are those of
10810 if Is_Class_Wide_Type (T) then
10814 Comp := First_Entity (T);
10815 while Present (Comp) loop
10816 if Chars (Comp) = Chars (S)
10817 and then Covers (Typ, Etype (Comp))
10826 It := Disambiguate (P, I1, I, Any_Type);
10828 if It = No_Interp then
10830 ("ambiguous prefix for selected component", N);
10831 Set_Etype (N, Typ);
10837 -- There may be an implicit dereference. Retrieve
10838 -- designated record type.
10840 if Is_Access_Type (It1.Typ) then
10841 T := Designated_Type (It1.Typ);
10846 if Scope (Comp1) /= T then
10848 -- Resolution chooses the new interpretation.
10849 -- Find the component with the right name.
10851 Comp1 := First_Entity (T);
10852 while Present (Comp1)
10853 and then Chars (Comp1) /= Chars (S)
10855 Next_Entity (Comp1);
10864 Next_Entity (Comp);
10868 Get_Next_Interp (I, It);
10871 -- There must be a legal interpretation at this point
10873 pragma Assert (Found);
10874 Resolve (P, It1.Typ);
10876 -- In general the expected type is the type of the context, not the
10877 -- type of the candidate selected component.
10879 Set_Etype (N, Typ);
10880 Set_Entity_With_Checks (S, Comp1);
10882 -- The type of the context and that of the component are
10883 -- compatible and in general identical, but if they are anonymous
10884 -- access-to-subprogram types, the relevant type is that of the
10885 -- component. This matters in Unnest_Subprograms mode, where the
10886 -- relevant context is the one in which the type is declared, not
10887 -- the point of use. This determines what activation record to use.
10889 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
10890 Set_Etype (N, Etype (Comp1));
10892 -- When the type of the component is an access to a class-wide type
10893 -- the relevant type is that of the component (since in such case we
10894 -- may need to generate implicit type conversions or dispatching
10897 elsif Is_Access_Type (Typ)
10898 and then not Is_Class_Wide_Type (Designated_Type (Typ))
10899 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
10901 Set_Etype (N, Etype (Comp1));
10905 -- Resolve prefix with its type
10910 -- Generate cross-reference. We needed to wait until full overloading
10911 -- resolution was complete to do this, since otherwise we can't tell if
10912 -- we are an lvalue or not.
10914 if May_Be_Lvalue (N) then
10915 Generate_Reference (Entity (S), S, 'm');
10917 Generate_Reference (Entity (S), S, 'r');
10920 -- If the prefix's type is an access type, get to the real record type.
10921 -- Note: we do not apply an access check because an explicit dereference
10922 -- will be introduced later, and the check will happen there.
10924 if Is_Access_Type (Etype (P)) then
10925 T := Implicitly_Designated_Type (Etype (P));
10926 Check_Fully_Declared_Prefix (T, P);
10931 -- If the prefix is an entity it may have a deferred reference set
10932 -- during analysis of the selected component. After resolution we
10933 -- can transform it into a proper reference. This prevents spurious
10934 -- warnings on useless assignments when the same selected component
10935 -- is the actual for an out parameter in a subsequent call.
10937 if Is_Entity_Name (P)
10938 and then Has_Deferred_Reference (Entity (P))
10940 if May_Be_Lvalue (N) then
10941 Generate_Reference (Entity (P), P, 'm');
10943 Generate_Reference (Entity (P), P, 'r');
10948 -- Set flag for expander if discriminant check required on a component
10949 -- appearing within a variant.
10951 if Has_Discriminants (T)
10952 and then Ekind (Entity (S)) = E_Component
10953 and then Present (Original_Record_Component (Entity (S)))
10954 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
10956 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
10957 and then not Discriminant_Checks_Suppressed (T)
10958 and then not Init_Component
10960 Set_Do_Discriminant_Check (N);
10963 if Ekind (Entity (S)) = E_Void then
10964 Error_Msg_N ("premature use of component", S);
10967 -- If the prefix is a record conversion, this may be a renamed
10968 -- discriminant whose bounds differ from those of the original
10969 -- one, so we must ensure that a range check is performed.
10971 if Nkind (P) = N_Type_Conversion
10972 and then Ekind (Entity (S)) = E_Discriminant
10973 and then Is_Discrete_Type (Typ)
10975 Set_Etype (N, Base_Type (Typ));
10978 -- Eval_Selected_Component may e.g. fold statically known discriminants.
10980 Eval_Selected_Component (N);
10982 if Nkind (N) = N_Selected_Component then
10984 -- If the record type is atomic and the component is not, then this
10985 -- is worth a warning before Ada 2020, since we have a situation
10986 -- where the access to the component may cause extra read/writes of
10987 -- the atomic object, or partial word accesses, both of which may be
10990 if Is_Atomic_Ref_With_Address (N)
10991 and then not Is_Atomic (Entity (S))
10992 and then not Is_Atomic (Etype (Entity (S)))
10993 and then Ada_Version < Ada_2020
10996 ("??access to non-atomic component of atomic record",
10999 ("\??may cause unexpected accesses to atomic object",
11003 Resolve_Implicit_Dereference (Prefix (N));
11004 Analyze_Dimension (N);
11006 end Resolve_Selected_Component;
11008 -------------------
11009 -- Resolve_Shift --
11010 -------------------
11012 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
11013 B_Typ : constant Entity_Id := Base_Type (Typ);
11014 L : constant Node_Id := Left_Opnd (N);
11015 R : constant Node_Id := Right_Opnd (N);
11018 -- We do the resolution using the base type, because intermediate values
11019 -- in expressions always are of the base type, not a subtype of it.
11021 Resolve (L, B_Typ);
11022 Resolve (R, Standard_Natural);
11024 Check_Unset_Reference (L);
11025 Check_Unset_Reference (R);
11027 Set_Etype (N, B_Typ);
11028 Generate_Operator_Reference (N, B_Typ);
11032 ---------------------------
11033 -- Resolve_Short_Circuit --
11034 ---------------------------
11036 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
11037 B_Typ : constant Entity_Id := Base_Type (Typ);
11038 L : constant Node_Id := Left_Opnd (N);
11039 R : constant Node_Id := Right_Opnd (N);
11042 -- Ensure all actions associated with the left operand (e.g.
11043 -- finalization of transient objects) are fully evaluated locally within
11044 -- an expression with actions. This is particularly helpful for coverage
11045 -- analysis. However this should not happen in generics or if option
11046 -- Minimize_Expression_With_Actions is set.
11048 if Expander_Active and not Minimize_Expression_With_Actions then
11050 Reloc_L : constant Node_Id := Relocate_Node (L);
11052 Save_Interps (Old_N => L, New_N => Reloc_L);
11055 Make_Expression_With_Actions (Sloc (L),
11056 Actions => New_List,
11057 Expression => Reloc_L));
11059 -- Set Comes_From_Source on L to preserve warnings for unset
11062 Preserve_Comes_From_Source (L, Reloc_L);
11066 Resolve (L, B_Typ);
11067 Resolve (R, B_Typ);
11069 -- Check for issuing warning for always False assert/check, this happens
11070 -- when assertions are turned off, in which case the pragma Assert/Check
11071 -- was transformed into:
11073 -- if False and then <condition> then ...
11075 -- and we detect this pattern
11077 if Warn_On_Assertion_Failure
11078 and then Is_Entity_Name (R)
11079 and then Entity (R) = Standard_False
11080 and then Nkind (Parent (N)) = N_If_Statement
11081 and then Nkind (N) = N_And_Then
11082 and then Is_Entity_Name (L)
11083 and then Entity (L) = Standard_False
11086 Orig : constant Node_Id := Original_Node (Parent (N));
11089 -- Special handling of Asssert pragma
11091 if Nkind (Orig) = N_Pragma
11092 and then Pragma_Name (Orig) = Name_Assert
11095 Expr : constant Node_Id :=
11098 (First (Pragma_Argument_Associations (Orig))));
11101 -- Don't warn if original condition is explicit False,
11102 -- since obviously the failure is expected in this case.
11104 if Is_Entity_Name (Expr)
11105 and then Entity (Expr) = Standard_False
11109 -- Issue warning. We do not want the deletion of the
11110 -- IF/AND-THEN to take this message with it. We achieve this
11111 -- by making sure that the expanded code points to the Sloc
11112 -- of the expression, not the original pragma.
11115 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
11116 -- The source location of the expression is not usually
11117 -- the best choice here. For example, it gets located on
11118 -- the last AND keyword in a chain of boolean expressiond
11119 -- AND'ed together. It is best to put the message on the
11120 -- first character of the assertion, which is the effect
11121 -- of the First_Node call here.
11124 ("?A?assertion would fail at run time!",
11126 (First (Pragma_Argument_Associations (Orig))));
11130 -- Similar processing for Check pragma
11132 elsif Nkind (Orig) = N_Pragma
11133 and then Pragma_Name (Orig) = Name_Check
11135 -- Don't want to warn if original condition is explicit False
11138 Expr : constant Node_Id :=
11141 (Next (First (Pragma_Argument_Associations (Orig)))));
11143 if Is_Entity_Name (Expr)
11144 and then Entity (Expr) = Standard_False
11151 -- Again use Error_Msg_F rather than Error_Msg_N, see
11152 -- comment above for an explanation of why we do this.
11155 ("?A?check would fail at run time!",
11157 (Last (Pragma_Argument_Associations (Orig))));
11164 -- Continue with processing of short circuit
11166 Check_Unset_Reference (L);
11167 Check_Unset_Reference (R);
11169 Set_Etype (N, B_Typ);
11170 Eval_Short_Circuit (N);
11171 end Resolve_Short_Circuit;
11173 -------------------
11174 -- Resolve_Slice --
11175 -------------------
11177 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11178 Drange : constant Node_Id := Discrete_Range (N);
11179 Name : constant Node_Id := Prefix (N);
11180 Array_Type : Entity_Id := Empty;
11181 Dexpr : Node_Id := Empty;
11182 Index_Type : Entity_Id;
11185 if Is_Overloaded (Name) then
11187 -- Use the context type to select the prefix that yields the correct
11192 I1 : Interp_Index := 0;
11194 P : constant Node_Id := Prefix (N);
11195 Found : Boolean := False;
11198 Get_First_Interp (P, I, It);
11199 while Present (It.Typ) loop
11200 if (Is_Array_Type (It.Typ)
11201 and then Covers (Typ, It.Typ))
11202 or else (Is_Access_Type (It.Typ)
11203 and then Is_Array_Type (Designated_Type (It.Typ))
11204 and then Covers (Typ, Designated_Type (It.Typ)))
11207 It := Disambiguate (P, I1, I, Any_Type);
11209 if It = No_Interp then
11210 Error_Msg_N ("ambiguous prefix for slicing", N);
11211 Set_Etype (N, Typ);
11215 Array_Type := It.Typ;
11220 Array_Type := It.Typ;
11225 Get_Next_Interp (I, It);
11230 Array_Type := Etype (Name);
11233 Resolve (Name, Array_Type);
11235 -- If the prefix's type is an access type, get to the real array type.
11236 -- Note: we do not apply an access check because an explicit dereference
11237 -- will be introduced later, and the check will happen there.
11239 if Is_Access_Type (Array_Type) then
11240 Array_Type := Implicitly_Designated_Type (Array_Type);
11242 -- If the prefix is an access to an unconstrained array, we must use
11243 -- the actual subtype of the object to perform the index checks. The
11244 -- object denoted by the prefix is implicit in the node, so we build
11245 -- an explicit representation for it in order to compute the actual
11248 if not Is_Constrained (Array_Type) then
11249 Remove_Side_Effects (Prefix (N));
11252 Obj : constant Node_Id :=
11253 Make_Explicit_Dereference (Sloc (N),
11254 Prefix => New_Copy_Tree (Prefix (N)));
11256 Set_Etype (Obj, Array_Type);
11257 Set_Parent (Obj, Parent (N));
11258 Array_Type := Get_Actual_Subtype (Obj);
11262 elsif Is_Entity_Name (Name)
11263 or else Nkind (Name) = N_Explicit_Dereference
11264 or else (Nkind (Name) = N_Function_Call
11265 and then not Is_Constrained (Etype (Name)))
11267 Array_Type := Get_Actual_Subtype (Name);
11269 -- If the name is a selected component that depends on discriminants,
11270 -- build an actual subtype for it. This can happen only when the name
11271 -- itself is overloaded; otherwise the actual subtype is created when
11272 -- the selected component is analyzed.
11274 elsif Nkind (Name) = N_Selected_Component
11275 and then Full_Analysis
11276 and then Depends_On_Discriminant (First_Index (Array_Type))
11279 Act_Decl : constant Node_Id :=
11280 Build_Actual_Subtype_Of_Component (Array_Type, Name);
11282 Insert_Action (N, Act_Decl);
11283 Array_Type := Defining_Identifier (Act_Decl);
11286 -- Maybe this should just be "else", instead of checking for the
11287 -- specific case of slice??? This is needed for the case where the
11288 -- prefix is an Image attribute, which gets expanded to a slice, and so
11289 -- has a constrained subtype which we want to use for the slice range
11290 -- check applied below (the range check won't get done if the
11291 -- unconstrained subtype of the 'Image is used).
11293 elsif Nkind (Name) = N_Slice then
11294 Array_Type := Etype (Name);
11297 -- Obtain the type of the array index
11299 if Ekind (Array_Type) = E_String_Literal_Subtype then
11300 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11302 Index_Type := Etype (First_Index (Array_Type));
11305 -- If name was overloaded, set slice type correctly now
11307 Set_Etype (N, Array_Type);
11309 -- Handle the generation of a range check that compares the array index
11310 -- against the discrete_range. The check is not applied to internally
11311 -- built nodes associated with the expansion of dispatch tables. Check
11312 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11315 if Tagged_Type_Expansion
11316 and then RTU_Loaded (Ada_Tags)
11317 and then Nkind (Prefix (N)) = N_Selected_Component
11318 and then Present (Entity (Selector_Name (Prefix (N))))
11319 and then Entity (Selector_Name (Prefix (N))) =
11320 RTE_Record_Component (RE_Prims_Ptr)
11324 -- The discrete_range is specified by a subtype indication. Create a
11325 -- shallow copy and inherit the type, parent and source location from
11326 -- the discrete_range. This ensures that the range check is inserted
11327 -- relative to the slice and that the runtime exception points to the
11328 -- proper construct.
11330 elsif Is_Entity_Name (Drange) then
11331 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
11333 Set_Etype (Dexpr, Etype (Drange));
11334 Set_Parent (Dexpr, Parent (Drange));
11335 Set_Sloc (Dexpr, Sloc (Drange));
11337 -- The discrete_range is a regular range. Resolve the bounds and remove
11338 -- their side effects.
11341 Resolve (Drange, Base_Type (Index_Type));
11343 if Nkind (Drange) = N_Range then
11344 Force_Evaluation (Low_Bound (Drange));
11345 Force_Evaluation (High_Bound (Drange));
11351 if Present (Dexpr) then
11352 Apply_Range_Check (Dexpr, Index_Type);
11355 Set_Slice_Subtype (N);
11357 -- Check bad use of type with predicates
11363 if Nkind (Drange) = N_Subtype_Indication
11364 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11366 Subt := Entity (Subtype_Mark (Drange));
11368 Subt := Etype (Drange);
11371 if Has_Predicates (Subt) then
11372 Bad_Predicated_Subtype_Use
11373 ("subtype& has predicate, not allowed in slice", Drange, Subt);
11377 -- Otherwise here is where we check suspicious indexes
11379 if Nkind (Drange) = N_Range then
11380 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
11381 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
11384 Resolve_Implicit_Dereference (Prefix (N));
11385 Analyze_Dimension (N);
11389 ----------------------------
11390 -- Resolve_String_Literal --
11391 ----------------------------
11393 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
11394 C_Typ : constant Entity_Id := Component_Type (Typ);
11395 R_Typ : constant Entity_Id := Root_Type (C_Typ);
11396 Loc : constant Source_Ptr := Sloc (N);
11397 Str : constant String_Id := Strval (N);
11398 Strlen : constant Nat := String_Length (Str);
11399 Subtype_Id : Entity_Id;
11400 Need_Check : Boolean;
11403 -- For a string appearing in a concatenation, defer creation of the
11404 -- string_literal_subtype until the end of the resolution of the
11405 -- concatenation, because the literal may be constant-folded away. This
11406 -- is a useful optimization for long concatenation expressions.
11408 -- If the string is an aggregate built for a single character (which
11409 -- happens in a non-static context) or a is null string to which special
11410 -- checks may apply, we build the subtype. Wide strings must also get a
11411 -- string subtype if they come from a one character aggregate. Strings
11412 -- generated by attributes might be static, but it is often hard to
11413 -- determine whether the enclosing context is static, so we generate
11414 -- subtypes for them as well, thus losing some rarer optimizations ???
11415 -- Same for strings that come from a static conversion.
11418 (Strlen = 0 and then Typ /= Standard_String)
11419 or else Nkind (Parent (N)) /= N_Op_Concat
11420 or else (N /= Left_Opnd (Parent (N))
11421 and then N /= Right_Opnd (Parent (N)))
11422 or else ((Typ = Standard_Wide_String
11423 or else Typ = Standard_Wide_Wide_String)
11424 and then Nkind (Original_Node (N)) /= N_String_Literal);
11426 -- If the resolving type is itself a string literal subtype, we can just
11427 -- reuse it, since there is no point in creating another.
11429 if Ekind (Typ) = E_String_Literal_Subtype then
11432 elsif Nkind (Parent (N)) = N_Op_Concat
11433 and then not Need_Check
11434 and then Nkind (Original_Node (N)) not in N_Character_Literal
11435 | N_Attribute_Reference
11436 | N_Qualified_Expression
11437 | N_Type_Conversion
11441 -- Do not generate a string literal subtype for the default expression
11442 -- of a formal parameter in GNATprove mode. This is because the string
11443 -- subtype is associated with the freezing actions of the subprogram,
11444 -- however freezing is disabled in GNATprove mode and as a result the
11445 -- subtype is unavailable.
11447 elsif GNATprove_Mode
11448 and then Nkind (Parent (N)) = N_Parameter_Specification
11452 -- Otherwise we must create a string literal subtype. Note that the
11453 -- whole idea of string literal subtypes is simply to avoid the need
11454 -- for building a full fledged array subtype for each literal.
11457 Set_String_Literal_Subtype (N, Typ);
11458 Subtype_Id := Etype (N);
11461 if Nkind (Parent (N)) /= N_Op_Concat
11464 Set_Etype (N, Subtype_Id);
11465 Eval_String_Literal (N);
11468 if Is_Limited_Composite (Typ)
11469 or else Is_Private_Composite (Typ)
11471 Error_Msg_N ("string literal not available for private array", N);
11472 Set_Etype (N, Any_Type);
11476 -- The validity of a null string has been checked in the call to
11477 -- Eval_String_Literal.
11482 -- Always accept string literal with component type Any_Character, which
11483 -- occurs in error situations and in comparisons of literals, both of
11484 -- which should accept all literals.
11486 elsif R_Typ = Any_Character then
11489 -- If the type is bit-packed, then we always transform the string
11490 -- literal into a full fledged aggregate.
11492 elsif Is_Bit_Packed_Array (Typ) then
11495 -- Deal with cases of Wide_Wide_String, Wide_String, and String
11498 -- For Standard.Wide_Wide_String, or any other type whose component
11499 -- type is Standard.Wide_Wide_Character, we know that all the
11500 -- characters in the string must be acceptable, since the parser
11501 -- accepted the characters as valid character literals.
11503 if R_Typ = Standard_Wide_Wide_Character then
11506 -- For the case of Standard.String, or any other type whose component
11507 -- type is Standard.Character, we must make sure that there are no
11508 -- wide characters in the string, i.e. that it is entirely composed
11509 -- of characters in range of type Character.
11511 -- If the string literal is the result of a static concatenation, the
11512 -- test has already been performed on the components, and need not be
11515 elsif R_Typ = Standard_Character
11516 and then Nkind (Original_Node (N)) /= N_Op_Concat
11518 for J in 1 .. Strlen loop
11519 if not In_Character_Range (Get_String_Char (Str, J)) then
11521 -- If we are out of range, post error. This is one of the
11522 -- very few places that we place the flag in the middle of
11523 -- a token, right under the offending wide character. Not
11524 -- quite clear if this is right wrt wide character encoding
11525 -- sequences, but it's only an error message.
11528 ("literal out of range of type Standard.Character",
11529 Source_Ptr (Int (Loc) + J));
11534 -- For the case of Standard.Wide_String, or any other type whose
11535 -- component type is Standard.Wide_Character, we must make sure that
11536 -- there are no wide characters in the string, i.e. that it is
11537 -- entirely composed of characters in range of type Wide_Character.
11539 -- If the string literal is the result of a static concatenation,
11540 -- the test has already been performed on the components, and need
11541 -- not be repeated.
11543 elsif R_Typ = Standard_Wide_Character
11544 and then Nkind (Original_Node (N)) /= N_Op_Concat
11546 for J in 1 .. Strlen loop
11547 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
11549 -- If we are out of range, post error. This is one of the
11550 -- very few places that we place the flag in the middle of
11551 -- a token, right under the offending wide character.
11553 -- This is not quite right, because characters in general
11554 -- will take more than one character position ???
11557 ("literal out of range of type Standard.Wide_Character",
11558 Source_Ptr (Int (Loc) + J));
11563 -- If the root type is not a standard character, then we will convert
11564 -- the string into an aggregate and will let the aggregate code do
11565 -- the checking. Standard Wide_Wide_Character is also OK here.
11571 -- See if the component type of the array corresponding to the string
11572 -- has compile time known bounds. If yes we can directly check
11573 -- whether the evaluation of the string will raise constraint error.
11574 -- Otherwise we need to transform the string literal into the
11575 -- corresponding character aggregate and let the aggregate code do
11576 -- the checking. We use the same transformation if the component
11577 -- type has a static predicate, which will be applied to each
11578 -- character when the aggregate is resolved.
11580 if Is_Standard_Character_Type (R_Typ) then
11582 -- Check for the case of full range, where we are definitely OK
11584 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
11588 -- Here the range is not the complete base type range, so check
11591 Comp_Typ_Lo : constant Node_Id :=
11592 Type_Low_Bound (Component_Type (Typ));
11593 Comp_Typ_Hi : constant Node_Id :=
11594 Type_High_Bound (Component_Type (Typ));
11599 if Compile_Time_Known_Value (Comp_Typ_Lo)
11600 and then Compile_Time_Known_Value (Comp_Typ_Hi)
11602 for J in 1 .. Strlen loop
11603 Char_Val := Int (Get_String_Char (Str, J));
11605 if Char_Val < Expr_Value (Comp_Typ_Lo)
11606 or else Char_Val > Expr_Value (Comp_Typ_Hi)
11608 Apply_Compile_Time_Constraint_Error
11609 (N, "character out of range??",
11610 CE_Range_Check_Failed,
11611 Loc => Source_Ptr (Int (Loc) + J));
11615 if not Has_Static_Predicate (C_Typ) then
11623 -- If we got here we meed to transform the string literal into the
11624 -- equivalent qualified positional array aggregate. This is rather
11625 -- heavy artillery for this situation, but it is hard work to avoid.
11628 Lits : constant List_Id := New_List;
11629 P : Source_Ptr := Loc + 1;
11633 -- Build the character literals, we give them source locations that
11634 -- correspond to the string positions, which is a bit tricky given
11635 -- the possible presence of wide character escape sequences.
11637 for J in 1 .. Strlen loop
11638 C := Get_String_Char (Str, J);
11639 Set_Character_Literal_Name (C);
11642 Make_Character_Literal (P,
11643 Chars => Name_Find,
11644 Char_Literal_Value => UI_From_CC (C)));
11646 if In_Character_Range (C) then
11649 -- Should we have a call to Skip_Wide here ???
11658 Make_Qualified_Expression (Loc,
11659 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
11661 Make_Aggregate (Loc, Expressions => Lits)));
11663 Analyze_And_Resolve (N, Typ);
11665 end Resolve_String_Literal;
11667 -------------------------
11668 -- Resolve_Target_Name --
11669 -------------------------
11671 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
11673 Set_Etype (N, Typ);
11674 end Resolve_Target_Name;
11676 -----------------------------
11677 -- Resolve_Type_Conversion --
11678 -----------------------------
11680 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
11681 Conv_OK : constant Boolean := Conversion_OK (N);
11682 Operand : constant Node_Id := Expression (N);
11683 Operand_Typ : constant Entity_Id := Etype (Operand);
11684 Target_Typ : constant Entity_Id := Etype (N);
11689 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
11690 -- Set to False to suppress cases where we want to suppress the test
11691 -- for redundancy to avoid possible false positives on this warning.
11695 and then not Valid_Conversion (N, Target_Typ, Operand)
11700 -- If the Operand Etype is Universal_Fixed, then the conversion is
11701 -- never redundant. We need this check because by the time we have
11702 -- finished the rather complex transformation, the conversion looks
11703 -- redundant when it is not.
11705 if Operand_Typ = Universal_Fixed then
11706 Test_Redundant := False;
11708 -- If the operand is marked as Any_Fixed, then special processing is
11709 -- required. This is also a case where we suppress the test for a
11710 -- redundant conversion, since most certainly it is not redundant.
11712 elsif Operand_Typ = Any_Fixed then
11713 Test_Redundant := False;
11715 -- Mixed-mode operation involving a literal. Context must be a fixed
11716 -- type which is applied to the literal subsequently.
11718 -- Multiplication and division involving two fixed type operands must
11719 -- yield a universal real because the result is computed in arbitrary
11722 if Is_Fixed_Point_Type (Typ)
11723 and then Nkind (Operand) in N_Op_Divide | N_Op_Multiply
11724 and then Etype (Left_Opnd (Operand)) = Any_Fixed
11725 and then Etype (Right_Opnd (Operand)) = Any_Fixed
11727 Set_Etype (Operand, Universal_Real);
11729 elsif Is_Numeric_Type (Typ)
11730 and then Nkind (Operand) in N_Op_Multiply | N_Op_Divide
11731 and then (Etype (Right_Opnd (Operand)) = Universal_Real
11733 Etype (Left_Opnd (Operand)) = Universal_Real)
11735 -- Return if expression is ambiguous
11737 if Unique_Fixed_Point_Type (N) = Any_Type then
11740 -- If nothing else, the available fixed type is Duration
11743 Set_Etype (Operand, Standard_Duration);
11746 -- Resolve the real operand with largest available precision
11748 if Etype (Right_Opnd (Operand)) = Universal_Real then
11749 Rop := New_Copy_Tree (Right_Opnd (Operand));
11751 Rop := New_Copy_Tree (Left_Opnd (Operand));
11754 Resolve (Rop, Universal_Real);
11756 -- If the operand is a literal (it could be a non-static and
11757 -- illegal exponentiation) check whether the use of Duration
11758 -- is potentially inaccurate.
11760 if Nkind (Rop) = N_Real_Literal
11761 and then Realval (Rop) /= Ureal_0
11762 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
11765 ("??universal real operand can only "
11766 & "be interpreted as Duration!", Rop);
11768 ("\??precision will be lost in the conversion!", Rop);
11771 elsif Is_Numeric_Type (Typ)
11772 and then Nkind (Operand) in N_Op
11773 and then Unique_Fixed_Point_Type (N) /= Any_Type
11775 Set_Etype (Operand, Standard_Duration);
11778 Error_Msg_N ("invalid context for mixed mode operation", N);
11779 Set_Etype (Operand, Any_Type);
11786 Analyze_Dimension (N);
11788 -- Note: we do the Eval_Type_Conversion call before applying the
11789 -- required checks for a subtype conversion. This is important, since
11790 -- both are prepared under certain circumstances to change the type
11791 -- conversion to a constraint error node, but in the case of
11792 -- Eval_Type_Conversion this may reflect an illegality in the static
11793 -- case, and we would miss the illegality (getting only a warning
11794 -- message), if we applied the type conversion checks first.
11796 Eval_Type_Conversion (N);
11798 -- Even when evaluation is not possible, we may be able to simplify the
11799 -- conversion or its expression. This needs to be done before applying
11800 -- checks, since otherwise the checks may use the original expression
11801 -- and defeat the simplifications. This is specifically the case for
11802 -- elimination of the floating-point Truncation attribute in
11803 -- float-to-int conversions.
11805 Simplify_Type_Conversion (N);
11807 -- If after evaluation we still have a type conversion, then we may need
11808 -- to apply checks required for a subtype conversion. But skip them if
11809 -- universal fixed operands are involved, since range checks are handled
11810 -- separately for these cases, after the expansion done by Exp_Fixd.
11812 if Nkind (N) = N_Type_Conversion
11813 and then not Is_Generic_Type (Root_Type (Target_Typ))
11814 and then Target_Typ /= Universal_Fixed
11815 and then Etype (Operand) /= Universal_Fixed
11817 Apply_Type_Conversion_Checks (N);
11820 -- Issue warning for conversion of simple object to its own type. We
11821 -- have to test the original nodes, since they may have been rewritten
11822 -- by various optimizations.
11824 Orig_N := Original_Node (N);
11826 -- Here we test for a redundant conversion if the warning mode is
11827 -- active (and was not locally reset), and we have a type conversion
11828 -- from source not appearing in a generic instance.
11831 and then Nkind (Orig_N) = N_Type_Conversion
11832 and then Comes_From_Source (Orig_N)
11833 and then not In_Instance
11835 Orig_N := Original_Node (Expression (Orig_N));
11836 Orig_T := Target_Typ;
11838 -- If the node is part of a larger expression, the Target_Type
11839 -- may not be the original type of the node if the context is a
11840 -- condition. Recover original type to see if conversion is needed.
11842 if Is_Boolean_Type (Orig_T)
11843 and then Nkind (Parent (N)) in N_Op
11845 Orig_T := Etype (Parent (N));
11848 -- If we have an entity name, then give the warning if the entity
11849 -- is the right type, or if it is a loop parameter covered by the
11850 -- original type (that's needed because loop parameters have an
11851 -- odd subtype coming from the bounds).
11853 if (Is_Entity_Name (Orig_N)
11854 and then Present (Entity (Orig_N))
11856 (Etype (Entity (Orig_N)) = Orig_T
11858 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
11859 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
11861 -- If not an entity, then type of expression must match
11863 or else Etype (Orig_N) = Orig_T
11865 -- One more check, do not give warning if the analyzed conversion
11866 -- has an expression with non-static bounds, and the bounds of the
11867 -- target are static. This avoids junk warnings in cases where the
11868 -- conversion is necessary to establish staticness, for example in
11869 -- a case statement.
11871 if not Is_OK_Static_Subtype (Operand_Typ)
11872 and then Is_OK_Static_Subtype (Target_Typ)
11876 -- Finally, if this type conversion occurs in a context requiring
11877 -- a prefix, and the expression is a qualified expression then the
11878 -- type conversion is not redundant, since a qualified expression
11879 -- is not a prefix, whereas a type conversion is. For example, "X
11880 -- := T'(Funx(...)).Y;" is illegal because a selected component
11881 -- requires a prefix, but a type conversion makes it legal: "X :=
11882 -- T(T'(Funx(...))).Y;"
11884 -- In Ada 2012, a qualified expression is a name, so this idiom is
11885 -- no longer needed, but we still suppress the warning because it
11886 -- seems unfriendly for warnings to pop up when you switch to the
11887 -- newer language version.
11889 elsif Nkind (Orig_N) = N_Qualified_Expression
11890 and then Nkind (Parent (N)) in N_Attribute_Reference
11891 | N_Indexed_Component
11892 | N_Selected_Component
11894 | N_Explicit_Dereference
11898 -- Never warn on conversion to Long_Long_Integer'Base since
11899 -- that is most likely an artifact of the extended overflow
11900 -- checking and comes from complex expanded code.
11902 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
11905 -- Here we give the redundant conversion warning. If it is an
11906 -- entity, give the name of the entity in the message. If not,
11907 -- just mention the expression.
11910 if Is_Entity_Name (Orig_N) then
11911 Error_Msg_Node_2 := Orig_T;
11912 Error_Msg_NE -- CODEFIX
11913 ("?r?redundant conversion, & is of type &!",
11914 N, Entity (Orig_N));
11917 ("?r?redundant conversion, expression is of type&!",
11924 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
11925 -- No need to perform any interface conversion if the type of the
11926 -- expression coincides with the target type.
11928 if Ada_Version >= Ada_2005
11929 and then Expander_Active
11930 and then Operand_Typ /= Target_Typ
11933 Opnd : Entity_Id := Operand_Typ;
11934 Target : Entity_Id := Target_Typ;
11937 -- If the type of the operand is a limited view, use nonlimited
11938 -- view when available. If it is a class-wide type, recover the
11939 -- class-wide type of the nonlimited view.
11941 if From_Limited_With (Opnd)
11942 and then Has_Non_Limited_View (Opnd)
11944 Opnd := Non_Limited_View (Opnd);
11945 Set_Etype (Expression (N), Opnd);
11948 -- It seems that Non_Limited_View should also be applied for
11949 -- Target when it has a limited view, but that leads to missing
11950 -- error checks on interface conversions further below. ???
11952 if Is_Access_Type (Opnd) then
11953 Opnd := Designated_Type (Opnd);
11955 -- If the type of the operand is a limited view, use nonlimited
11956 -- view when available. If it is a class-wide type, recover the
11957 -- class-wide type of the nonlimited view.
11959 if From_Limited_With (Opnd)
11960 and then Has_Non_Limited_View (Opnd)
11962 Opnd := Non_Limited_View (Opnd);
11966 if Is_Access_Type (Target_Typ) then
11967 Target := Designated_Type (Target);
11969 -- If the target type is a limited view, use nonlimited view
11972 if From_Limited_With (Target)
11973 and then Has_Non_Limited_View (Target)
11975 Target := Non_Limited_View (Target);
11979 if Opnd = Target then
11982 -- Conversion from interface type
11984 -- It seems that it would be better for the error checks below
11985 -- to be performed as part of Validate_Conversion (and maybe some
11986 -- of the error checks above could be moved as well?). ???
11988 elsif Is_Interface (Opnd) then
11990 -- Ada 2005 (AI-217): Handle entities from limited views
11992 if From_Limited_With (Opnd) then
11993 Error_Msg_Qual_Level := 99;
11994 Error_Msg_NE -- CODEFIX
11995 ("missing WITH clause on package &", N,
11996 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
11998 ("type conversions require visibility of the full view",
12001 elsif From_Limited_With (Target)
12003 (Is_Access_Type (Target_Typ)
12004 and then Present (Non_Limited_View (Etype (Target))))
12006 Error_Msg_Qual_Level := 99;
12007 Error_Msg_NE -- CODEFIX
12008 ("missing WITH clause on package &", N,
12009 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
12011 ("type conversions require visibility of the full view",
12015 Expand_Interface_Conversion (N);
12018 -- Conversion to interface type
12020 elsif Is_Interface (Target) then
12024 if Ekind (Opnd) in E_Protected_Subtype | E_Task_Subtype then
12025 Opnd := Etype (Opnd);
12028 if Is_Class_Wide_Type (Opnd)
12029 or else Interface_Present_In_Ancestor
12033 Expand_Interface_Conversion (N);
12035 Error_Msg_Name_1 := Chars (Etype (Target));
12036 Error_Msg_Name_2 := Chars (Opnd);
12038 ("wrong interface conversion (% is not a progenitor "
12045 -- Ada 2012: Once the type conversion is resolved, check whether the
12046 -- operand statisfies a static predicate of the target subtype, if any.
12047 -- In the static expression case, a predicate check failure is an error.
12049 if Has_Predicates (Target_Typ) then
12050 Check_Expression_Against_Static_Predicate
12051 (N, Target_Typ, Static_Failure_Is_Error => True);
12054 -- If at this stage we have a fixed to integer conversion, make sure the
12055 -- Do_Range_Check flag is set, because such conversions in general need
12056 -- a range check. We only need this if expansion is off, see above why.
12058 if Nkind (N) = N_Type_Conversion
12059 and then not Expander_Active
12060 and then Is_Integer_Type (Target_Typ)
12061 and then Is_Fixed_Point_Type (Operand_Typ)
12062 and then not Range_Checks_Suppressed (Target_Typ)
12063 and then not Range_Checks_Suppressed (Operand_Typ)
12065 Set_Do_Range_Check (Operand);
12068 -- Generating C code a type conversion of an access to constrained
12069 -- array type to access to unconstrained array type involves building
12070 -- a fat pointer which in general cannot be generated on the fly. We
12071 -- remove side effects in order to store the result of the conversion
12072 -- into a temporary.
12074 if Modify_Tree_For_C
12075 and then Nkind (N) = N_Type_Conversion
12076 and then Nkind (Parent (N)) /= N_Object_Declaration
12077 and then Is_Access_Type (Etype (N))
12078 and then Is_Array_Type (Designated_Type (Etype (N)))
12079 and then not Is_Constrained (Designated_Type (Etype (N)))
12080 and then Is_Constrained (Designated_Type (Etype (Expression (N))))
12082 Remove_Side_Effects (N);
12084 end Resolve_Type_Conversion;
12086 ----------------------
12087 -- Resolve_Unary_Op --
12088 ----------------------
12090 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
12091 B_Typ : constant Entity_Id := Base_Type (Typ);
12092 R : constant Node_Id := Right_Opnd (N);
12098 -- Deal with intrinsic unary operators
12100 if Comes_From_Source (N)
12101 and then Ekind (Entity (N)) = E_Function
12102 and then Is_Imported (Entity (N))
12103 and then Is_Intrinsic_Subprogram (Entity (N))
12105 Resolve_Intrinsic_Unary_Operator (N, Typ);
12109 -- Deal with universal cases
12111 if Is_Universal_Numeric_Type (Etype (R)) then
12112 Check_For_Visible_Operator (N, B_Typ);
12115 Set_Etype (N, B_Typ);
12116 Resolve (R, B_Typ);
12118 -- Generate warning for negative literal of a modular type, unless it is
12119 -- enclosed directly in a type qualification or a type conversion, as it
12120 -- is likely not what the user intended. We don't issue the warning for
12121 -- the common use of -1 to denote OxFFFF_FFFF...
12123 if Warn_On_Suspicious_Modulus_Value
12124 and then Nkind (N) = N_Op_Minus
12125 and then Nkind (R) = N_Integer_Literal
12126 and then Is_Modular_Integer_Type (B_Typ)
12127 and then Nkind (Parent (N)) not in N_Qualified_Expression
12128 | N_Type_Conversion
12129 and then Expr_Value (R) > Uint_1
12132 ("?M?negative literal of modular type is in fact positive", N);
12133 Error_Msg_Uint_1 := (-Expr_Value (R)) mod Modulus (B_Typ);
12134 Error_Msg_Uint_2 := Expr_Value (R);
12135 Error_Msg_N ("\do you really mean^ when writing -^ '?", N);
12137 ("\if you do, use qualification to avoid this warning", N);
12140 -- Generate warning for expressions like abs (x mod 2)
12142 if Warn_On_Redundant_Constructs
12143 and then Nkind (N) = N_Op_Abs
12145 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12147 if OK and then Hi >= Lo and then Lo >= 0 then
12148 Error_Msg_N -- CODEFIX
12149 ("?r?abs applied to known non-negative value has no effect", N);
12153 -- Deal with reference generation
12155 Check_Unset_Reference (R);
12156 Generate_Operator_Reference (N, B_Typ);
12157 Analyze_Dimension (N);
12160 -- Set overflow checking bit. Much cleverer code needed here eventually
12161 -- and perhaps the Resolve routines should be separated for the various
12162 -- arithmetic operations, since they will need different processing ???
12164 if Nkind (N) in N_Op then
12165 if not Overflow_Checks_Suppressed (Etype (N)) then
12166 Enable_Overflow_Check (N);
12170 -- Generate warning for expressions like -5 mod 3 for integers. No need
12171 -- to worry in the floating-point case, since parens do not affect the
12172 -- result so there is no point in giving in a warning.
12175 Norig : constant Node_Id := Original_Node (N);
12184 if Warn_On_Questionable_Missing_Parens
12185 and then Comes_From_Source (Norig)
12186 and then Is_Integer_Type (Typ)
12187 and then Nkind (Norig) = N_Op_Minus
12189 Rorig := Original_Node (Right_Opnd (Norig));
12191 -- We are looking for cases where the right operand is not
12192 -- parenthesized, and is a binary operator, multiply, divide, or
12193 -- mod. These are the cases where the grouping can affect results.
12195 if Paren_Count (Rorig) = 0
12196 and then Nkind (Rorig) in N_Op_Mod | N_Op_Multiply | N_Op_Divide
12198 -- For mod, we always give the warning, since the value is
12199 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12200 -- -(5 mod 315)). But for the other cases, the only concern is
12201 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12202 -- overflows, but (-2) * 64 does not). So we try to give the
12203 -- message only when overflow is possible.
12205 if Nkind (Rorig) /= N_Op_Mod
12206 and then Compile_Time_Known_Value (R)
12208 Val := Expr_Value (R);
12210 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12211 HB := Expr_Value (Type_High_Bound (Typ));
12213 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12216 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12217 LB := Expr_Value (Type_Low_Bound (Typ));
12219 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12222 -- Note that the test below is deliberately excluding the
12223 -- largest negative number, since that is a potentially
12224 -- troublesome case (e.g. -2 * x, where the result is the
12225 -- largest negative integer has an overflow with 2 * x).
12227 if Val > LB and then Val <= HB then
12232 -- For the multiplication case, the only case we have to worry
12233 -- about is when (-a)*b is exactly the largest negative number
12234 -- so that -(a*b) can cause overflow. This can only happen if
12235 -- a is a power of 2, and more generally if any operand is a
12236 -- constant that is not a power of 2, then the parentheses
12237 -- cannot affect whether overflow occurs. We only bother to
12238 -- test the left most operand
12240 -- Loop looking at left operands for one that has known value
12243 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12244 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12245 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12247 -- Operand value of 0 or 1 skips warning
12252 -- Otherwise check power of 2, if power of 2, warn, if
12253 -- anything else, skip warning.
12256 while Lval /= 2 loop
12257 if Lval mod 2 = 1 then
12268 -- Keep looking at left operands
12270 Opnd := Left_Opnd (Opnd);
12271 end loop Opnd_Loop;
12273 -- For rem or "/" we can only have a problematic situation
12274 -- if the divisor has a value of minus one or one. Otherwise
12275 -- overflow is impossible (divisor > 1) or we have a case of
12276 -- division by zero in any case.
12278 if Nkind (Rorig) in N_Op_Divide | N_Op_Rem
12279 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12280 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12285 -- If we fall through warning should be issued
12287 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12290 ("??unary minus expression should be parenthesized here!", N);
12294 end Resolve_Unary_Op;
12296 ----------------------------------
12297 -- Resolve_Unchecked_Expression --
12298 ----------------------------------
12300 procedure Resolve_Unchecked_Expression
12305 Resolve (Expression (N), Typ, Suppress => All_Checks);
12306 Set_Etype (N, Typ);
12307 end Resolve_Unchecked_Expression;
12309 ---------------------------------------
12310 -- Resolve_Unchecked_Type_Conversion --
12311 ---------------------------------------
12313 procedure Resolve_Unchecked_Type_Conversion
12317 pragma Warnings (Off, Typ);
12319 Operand : constant Node_Id := Expression (N);
12320 Opnd_Type : constant Entity_Id := Etype (Operand);
12323 -- Resolve operand using its own type
12325 Resolve (Operand, Opnd_Type);
12327 -- If the expression is a conversion to universal integer of an
12328 -- an expression with an integer type, then we can eliminate the
12329 -- intermediate conversion to universal integer.
12331 if Nkind (Operand) = N_Type_Conversion
12332 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12333 and then Is_Integer_Type (Etype (Expression (Operand)))
12335 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12336 Analyze_And_Resolve (Operand);
12339 -- In an inlined context, the unchecked conversion may be applied
12340 -- to a literal, in which case its type is the type of the context.
12341 -- (In other contexts conversions cannot apply to literals).
12344 and then (Opnd_Type = Any_Character or else
12345 Opnd_Type = Any_Integer or else
12346 Opnd_Type = Any_Real)
12348 Set_Etype (Operand, Typ);
12351 Analyze_Dimension (N);
12352 Eval_Unchecked_Conversion (N);
12353 end Resolve_Unchecked_Type_Conversion;
12355 ------------------------------
12356 -- Rewrite_Operator_As_Call --
12357 ------------------------------
12359 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12360 Loc : constant Source_Ptr := Sloc (N);
12361 Actuals : constant List_Id := New_List;
12365 if Nkind (N) in N_Binary_Op then
12366 Append (Left_Opnd (N), Actuals);
12369 Append (Right_Opnd (N), Actuals);
12372 Make_Function_Call (Sloc => Loc,
12373 Name => New_Occurrence_Of (Nam, Loc),
12374 Parameter_Associations => Actuals);
12376 Preserve_Comes_From_Source (New_N, N);
12377 Preserve_Comes_From_Source (Name (New_N), N);
12378 Rewrite (N, New_N);
12379 Set_Etype (N, Etype (Nam));
12380 end Rewrite_Operator_As_Call;
12382 ------------------------------
12383 -- Rewrite_Renamed_Operator --
12384 ------------------------------
12386 procedure Rewrite_Renamed_Operator
12391 Nam : constant Name_Id := Chars (Op);
12392 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12396 -- Do not perform this transformation within a pre/postcondition,
12397 -- because the expression will be reanalyzed, and the transformation
12398 -- might affect the visibility of the operator, e.g. in an instance.
12399 -- Note that fully analyzed and expanded pre/postconditions appear as
12400 -- pragma Check equivalents.
12402 if In_Pre_Post_Condition (N) then
12406 -- Likewise when an expression function is being preanalyzed, since the
12407 -- expression will be reanalyzed as part of the generated body.
12409 if In_Spec_Expression then
12411 S : constant Entity_Id := Current_Scope_No_Loops;
12413 if Ekind (S) = E_Function
12414 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
12415 N_Expression_Function
12422 -- Rewrite the operator node using the real operator, not its renaming.
12423 -- Exclude user-defined intrinsic operations of the same name, which are
12424 -- treated separately and rewritten as calls.
12426 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
12427 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
12428 Set_Chars (Op_Node, Nam);
12429 Set_Etype (Op_Node, Etype (N));
12430 Set_Entity (Op_Node, Op);
12431 Set_Right_Opnd (Op_Node, Right_Opnd (N));
12433 -- Indicate that both the original entity and its renaming are
12434 -- referenced at this point.
12436 Generate_Reference (Entity (N), N);
12437 Generate_Reference (Op, N);
12440 Set_Left_Opnd (Op_Node, Left_Opnd (N));
12443 Rewrite (N, Op_Node);
12445 -- If the context type is private, add the appropriate conversions so
12446 -- that the operator is applied to the full view. This is done in the
12447 -- routines that resolve intrinsic operators.
12449 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
12459 Resolve_Intrinsic_Operator (N, Typ);
12465 Resolve_Intrinsic_Unary_Operator (N, Typ);
12472 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
12474 -- Operator renames a user-defined operator of the same name. Use the
12475 -- original operator in the node, which is the one Gigi knows about.
12477 Set_Entity (N, Op);
12478 Set_Is_Overloaded (N, False);
12480 end Rewrite_Renamed_Operator;
12482 -----------------------
12483 -- Set_Slice_Subtype --
12484 -----------------------
12486 -- Build an implicit subtype declaration to represent the type delivered by
12487 -- the slice. This is an abbreviated version of an array subtype. We define
12488 -- an index subtype for the slice, using either the subtype name or the
12489 -- discrete range of the slice. To be consistent with index usage elsewhere
12490 -- we create a list header to hold the single index. This list is not
12491 -- otherwise attached to the syntax tree.
12493 procedure Set_Slice_Subtype (N : Node_Id) is
12494 Loc : constant Source_Ptr := Sloc (N);
12495 Index_List : constant List_Id := New_List;
12497 Index_Subtype : Entity_Id;
12498 Index_Type : Entity_Id;
12499 Slice_Subtype : Entity_Id;
12500 Drange : constant Node_Id := Discrete_Range (N);
12503 Index_Type := Base_Type (Etype (Drange));
12505 if Is_Entity_Name (Drange) then
12506 Index_Subtype := Entity (Drange);
12509 -- We force the evaluation of a range. This is definitely needed in
12510 -- the renamed case, and seems safer to do unconditionally. Note in
12511 -- any case that since we will create and insert an Itype referring
12512 -- to this range, we must make sure any side effect removal actions
12513 -- are inserted before the Itype definition.
12515 if Nkind (Drange) = N_Range then
12516 Force_Evaluation (Low_Bound (Drange));
12517 Force_Evaluation (High_Bound (Drange));
12519 -- If the discrete range is given by a subtype indication, the
12520 -- type of the slice is the base of the subtype mark.
12522 elsif Nkind (Drange) = N_Subtype_Indication then
12524 R : constant Node_Id := Range_Expression (Constraint (Drange));
12526 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
12527 Force_Evaluation (Low_Bound (R));
12528 Force_Evaluation (High_Bound (R));
12532 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12534 -- Take a new copy of Drange (where bounds have been rewritten to
12535 -- reference side-effect-free names). Using a separate tree ensures
12536 -- that further expansion (e.g. while rewriting a slice assignment
12537 -- into a FOR loop) does not attempt to remove side effects on the
12538 -- bounds again (which would cause the bounds in the index subtype
12539 -- definition to refer to temporaries before they are defined) (the
12540 -- reason is that some names are considered side effect free here
12541 -- for the subtype, but not in the context of a loop iteration
12544 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
12545 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
12546 Set_Etype (Index_Subtype, Index_Type);
12547 Set_Size_Info (Index_Subtype, Index_Type);
12548 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12549 Set_Is_Constrained (Index_Subtype);
12552 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
12554 Index := New_Occurrence_Of (Index_Subtype, Loc);
12555 Set_Etype (Index, Index_Subtype);
12556 Append (Index, Index_List);
12558 Set_First_Index (Slice_Subtype, Index);
12559 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
12560 Set_Is_Constrained (Slice_Subtype, True);
12562 Check_Compile_Time_Size (Slice_Subtype);
12564 -- The Etype of the existing Slice node is reset to this slice subtype.
12565 -- Its bounds are obtained from its first index.
12567 Set_Etype (N, Slice_Subtype);
12569 -- For bit-packed slice subtypes, freeze immediately (except in the case
12570 -- of being in a "spec expression" where we never freeze when we first
12571 -- see the expression).
12573 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
12574 Freeze_Itype (Slice_Subtype, N);
12576 -- For all other cases insert an itype reference in the slice's actions
12577 -- so that the itype is frozen at the proper place in the tree (i.e. at
12578 -- the point where actions for the slice are analyzed). Note that this
12579 -- is different from freezing the itype immediately, which might be
12580 -- premature (e.g. if the slice is within a transient scope). This needs
12581 -- to be done only if expansion is enabled, or in GNATprove mode to
12582 -- capture the associated run-time exceptions if any.
12584 elsif Expander_Active or GNATprove_Mode then
12585 Ensure_Defined (Typ => Slice_Subtype, N => N);
12587 end Set_Slice_Subtype;
12589 --------------------------------
12590 -- Set_String_Literal_Subtype --
12591 --------------------------------
12593 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
12594 Loc : constant Source_Ptr := Sloc (N);
12595 Low_Bound : constant Node_Id :=
12596 Type_Low_Bound (Etype (First_Index (Typ)));
12597 Subtype_Id : Entity_Id;
12600 if Nkind (N) /= N_String_Literal then
12604 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
12605 Set_String_Literal_Length (Subtype_Id, UI_From_Int
12606 (String_Length (Strval (N))));
12607 Set_Etype (Subtype_Id, Base_Type (Typ));
12608 Set_Is_Constrained (Subtype_Id);
12609 Set_Etype (N, Subtype_Id);
12611 -- The low bound is set from the low bound of the corresponding index
12612 -- type. Note that we do not store the high bound in the string literal
12613 -- subtype, but it can be deduced if necessary from the length and the
12616 if Is_OK_Static_Expression (Low_Bound) then
12617 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
12619 -- If the lower bound is not static we create a range for the string
12620 -- literal, using the index type and the known length of the literal.
12621 -- If the length is 1, then the upper bound is set to a mere copy of
12622 -- the lower bound; or else, if the index type is a signed integer,
12623 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
12624 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
12628 Length : constant Nat := String_Length (Strval (N));
12629 Index_List : constant List_Id := New_List;
12630 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
12631 Array_Subtype : Entity_Id;
12633 High_Bound : Node_Id;
12635 Index_Subtype : Entity_Id;
12639 High_Bound := New_Copy_Tree (Low_Bound);
12641 elsif Is_Signed_Integer_Type (Index_Type) then
12644 Left_Opnd => New_Copy_Tree (Low_Bound),
12645 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
12649 Make_Attribute_Reference (Loc,
12650 Attribute_Name => Name_Val,
12652 New_Occurrence_Of (Index_Type, Loc),
12653 Expressions => New_List (
12656 Make_Attribute_Reference (Loc,
12657 Attribute_Name => Name_Pos,
12659 New_Occurrence_Of (Index_Type, Loc),
12661 New_List (New_Copy_Tree (Low_Bound))),
12663 Make_Integer_Literal (Loc, Length - 1))));
12666 if Is_Integer_Type (Index_Type) then
12667 Set_String_Literal_Low_Bound
12668 (Subtype_Id, Make_Integer_Literal (Loc, 1));
12671 -- If the index type is an enumeration type, build bounds
12672 -- expression with attributes.
12674 Set_String_Literal_Low_Bound
12676 Make_Attribute_Reference (Loc,
12677 Attribute_Name => Name_First,
12679 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
12682 Analyze_And_Resolve
12683 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
12685 -- Build bona fide subtype for the string, and wrap it in an
12686 -- unchecked conversion, because the back end expects the
12687 -- String_Literal_Subtype to have a static lower bound.
12690 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12691 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
12692 Set_Scalar_Range (Index_Subtype, Drange);
12693 Set_Parent (Drange, N);
12694 Analyze_And_Resolve (Drange, Index_Type);
12696 -- In this context, the Index_Type may already have a constraint,
12697 -- so use common base type on string subtype. The base type may
12698 -- be used when generating attributes of the string, for example
12699 -- in the context of a slice assignment.
12701 Set_Etype (Index_Subtype, Base_Type (Index_Type));
12702 Set_Size_Info (Index_Subtype, Index_Type);
12703 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12705 Array_Subtype := Create_Itype (E_Array_Subtype, N);
12707 Index := New_Occurrence_Of (Index_Subtype, Loc);
12708 Set_Etype (Index, Index_Subtype);
12709 Append (Index, Index_List);
12711 Set_First_Index (Array_Subtype, Index);
12712 Set_Etype (Array_Subtype, Base_Type (Typ));
12713 Set_Is_Constrained (Array_Subtype, True);
12716 Make_Unchecked_Type_Conversion (Loc,
12717 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
12718 Expression => Relocate_Node (N)));
12719 Set_Etype (N, Array_Subtype);
12722 end Set_String_Literal_Subtype;
12724 ------------------------------
12725 -- Simplify_Type_Conversion --
12726 ------------------------------
12728 procedure Simplify_Type_Conversion (N : Node_Id) is
12730 if Nkind (N) = N_Type_Conversion then
12732 Operand : constant Node_Id := Expression (N);
12733 Target_Typ : constant Entity_Id := Etype (N);
12734 Opnd_Typ : constant Entity_Id := Etype (Operand);
12737 -- Special processing if the conversion is the expression of a
12738 -- Rounding or Truncation attribute reference. In this case we
12741 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
12747 -- with the Float_Truncate flag set to False or True respectively,
12748 -- which is more efficient. We reuse Rounding for Machine_Rounding
12749 -- as System.Fat_Gen, which is a permissible behavior.
12751 if Is_Floating_Point_Type (Opnd_Typ)
12753 (Is_Integer_Type (Target_Typ)
12754 or else (Is_Fixed_Point_Type (Target_Typ)
12755 and then Conversion_OK (N)))
12756 and then Nkind (Operand) = N_Attribute_Reference
12757 and then Attribute_Name (Operand) in Name_Rounding
12758 | Name_Machine_Rounding
12762 Truncate : constant Boolean :=
12763 Attribute_Name (Operand) = Name_Truncation;
12766 Relocate_Node (First (Expressions (Operand))));
12767 Set_Float_Truncate (N, Truncate);
12770 -- Special processing for the conversion of an integer literal to
12771 -- a dynamic type: we first convert the literal to the root type
12772 -- and then convert the result to the target type, the goal being
12773 -- to avoid doing range checks in universal integer.
12775 elsif Is_Integer_Type (Target_Typ)
12776 and then not Is_Generic_Type (Root_Type (Target_Typ))
12777 and then Nkind (Operand) = N_Integer_Literal
12778 and then Opnd_Typ = Universal_Integer
12780 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
12781 Analyze_And_Resolve (Operand);
12783 -- If the expression is a conversion to universal integer of an
12784 -- an expression with an integer type, then we can eliminate the
12785 -- intermediate conversion to universal integer.
12787 elsif Nkind (Operand) = N_Type_Conversion
12788 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12789 and then Is_Integer_Type (Etype (Expression (Operand)))
12791 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12792 Analyze_And_Resolve (Operand);
12796 end Simplify_Type_Conversion;
12798 -----------------------------
12799 -- Unique_Fixed_Point_Type --
12800 -----------------------------
12802 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
12803 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
12804 -- Give error messages for true ambiguity. Messages are posted on node
12805 -- N, and entities T1, T2 are the possible interpretations.
12807 -----------------------
12808 -- Fixed_Point_Error --
12809 -----------------------
12811 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
12813 Error_Msg_N ("ambiguous universal_fixed_expression", N);
12814 Error_Msg_NE ("\\possible interpretation as}", N, T1);
12815 Error_Msg_NE ("\\possible interpretation as}", N, T2);
12816 end Fixed_Point_Error;
12826 -- Start of processing for Unique_Fixed_Point_Type
12829 -- The operations on Duration are visible, so Duration is always a
12830 -- possible interpretation.
12832 T1 := Standard_Duration;
12834 -- Look for fixed-point types in enclosing scopes
12836 Scop := Current_Scope;
12837 while Scop /= Standard_Standard loop
12838 T2 := First_Entity (Scop);
12839 while Present (T2) loop
12840 if Is_Fixed_Point_Type (T2)
12841 and then Current_Entity (T2) = T2
12842 and then Scope (Base_Type (T2)) = Scop
12844 if Present (T1) then
12845 Fixed_Point_Error (T1, T2);
12855 Scop := Scope (Scop);
12858 -- Look for visible fixed type declarations in the context
12860 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
12861 while Present (Item) loop
12862 if Nkind (Item) = N_With_Clause then
12863 Scop := Entity (Name (Item));
12864 T2 := First_Entity (Scop);
12865 while Present (T2) loop
12866 if Is_Fixed_Point_Type (T2)
12867 and then Scope (Base_Type (T2)) = Scop
12868 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
12870 if Present (T1) then
12871 Fixed_Point_Error (T1, T2);
12885 if Nkind (N) = N_Real_Literal then
12886 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
12889 -- When the context is a type conversion, issue the warning on the
12890 -- expression of the conversion because it is the actual operation.
12892 if Nkind (N) in N_Type_Conversion | N_Unchecked_Type_Conversion then
12893 ErrN := Expression (N);
12899 ("??universal_fixed expression interpreted as }!", ErrN, T1);
12903 end Unique_Fixed_Point_Type;
12905 ----------------------
12906 -- Valid_Conversion --
12907 ----------------------
12909 function Valid_Conversion
12911 Target : Entity_Id;
12913 Report_Errs : Boolean := True) return Boolean
12915 Target_Type : constant Entity_Id := Base_Type (Target);
12916 Opnd_Type : Entity_Id := Etype (Operand);
12917 Inc_Ancestor : Entity_Id;
12919 function Conversion_Check
12921 Msg : String) return Boolean;
12922 -- Little routine to post Msg if Valid is False, returns Valid value
12924 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
12925 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
12927 procedure Conversion_Error_NE
12929 N : Node_Or_Entity_Id;
12930 E : Node_Or_Entity_Id);
12931 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
12933 function In_Instance_Code return Boolean;
12934 -- Return True if expression is within an instance but is not in one of
12935 -- the actuals of the instantiation. Type conversions within an instance
12936 -- are not rechecked because type visbility may lead to spurious errors,
12937 -- but conversions in an actual for a formal object must be checked.
12939 function Is_Discrim_Of_Bad_Access_Conversion_Argument
12940 (Expr : Node_Id) return Boolean;
12941 -- Implicit anonymous-to-named access type conversions are not allowed
12942 -- if the "statically deeper than" relationship does not apply to the
12943 -- type of the conversion operand. See RM 8.6(28.1) and AARM 8.6(28.d).
12944 -- We deal with most such cases elsewhere so that we can emit more
12945 -- specific error messages (e.g., if the operand is an access parameter
12946 -- or a saooaaat (stand-alone object of an anonymous access type)), but
12947 -- here is where we catch the case where the operand is an access
12948 -- discriminant selected from a dereference of another such "bad"
12949 -- conversion argument.
12951 function Valid_Tagged_Conversion
12952 (Target_Type : Entity_Id;
12953 Opnd_Type : Entity_Id) return Boolean;
12954 -- Specifically test for validity of tagged conversions
12956 function Valid_Array_Conversion return Boolean;
12957 -- Check index and component conformance, and accessibility levels if
12958 -- the component types are anonymous access types (Ada 2005).
12960 ----------------------
12961 -- Conversion_Check --
12962 ----------------------
12964 function Conversion_Check
12966 Msg : String) return Boolean
12971 -- A generic unit has already been analyzed and we have verified
12972 -- that a particular conversion is OK in that context. Since the
12973 -- instance is reanalyzed without relying on the relationships
12974 -- established during the analysis of the generic, it is possible
12975 -- to end up with inconsistent views of private types. Do not emit
12976 -- the error message in such cases. The rest of the machinery in
12977 -- Valid_Conversion still ensures the proper compatibility of
12978 -- target and operand types.
12980 and then not In_Instance_Code
12982 Conversion_Error_N (Msg, Operand);
12986 end Conversion_Check;
12988 ------------------------
12989 -- Conversion_Error_N --
12990 ------------------------
12992 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
12994 if Report_Errs then
12995 Error_Msg_N (Msg, N);
12997 end Conversion_Error_N;
12999 -------------------------
13000 -- Conversion_Error_NE --
13001 -------------------------
13003 procedure Conversion_Error_NE
13005 N : Node_Or_Entity_Id;
13006 E : Node_Or_Entity_Id)
13009 if Report_Errs then
13010 Error_Msg_NE (Msg, N, E);
13012 end Conversion_Error_NE;
13014 ----------------------
13015 -- In_Instance_Code --
13016 ----------------------
13018 function In_Instance_Code return Boolean is
13022 if not In_Instance then
13027 while Present (Par) loop
13029 -- The expression is part of an actual object if it appears in
13030 -- the generated object declaration in the instance.
13032 if Nkind (Par) = N_Object_Declaration
13033 and then Present (Corresponding_Generic_Association (Par))
13039 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
13040 or else Nkind (Par) in N_Subprogram_Call
13041 or else Nkind (Par) in N_Declaration;
13044 Par := Parent (Par);
13047 -- Otherwise the expression appears within the instantiated unit
13051 end In_Instance_Code;
13053 --------------------------------------------------
13054 -- Is_Discrim_Of_Bad_Access_Conversion_Argument --
13055 --------------------------------------------------
13057 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13058 (Expr : Node_Id) return Boolean
13060 Exp_Type : Entity_Id := Base_Type (Etype (Expr));
13061 pragma Assert (Is_Access_Type (Exp_Type));
13063 Associated_Node : Node_Id;
13064 Deref_Prefix : Node_Id;
13066 if not Is_Anonymous_Access_Type (Exp_Type) then
13070 pragma Assert (Is_Itype (Exp_Type));
13071 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13073 if Nkind (Associated_Node) /= N_Discriminant_Specification then
13074 return False; -- not the type of an access discriminant
13077 -- return False if Expr not of form <prefix>.all.Some_Component
13079 if (Nkind (Expr) /= N_Selected_Component)
13080 or else (Nkind (Prefix (Expr)) /= N_Explicit_Dereference)
13082 -- conditional expressions, declare expressions ???
13086 Deref_Prefix := Prefix (Prefix (Expr));
13087 Exp_Type := Base_Type (Etype (Deref_Prefix));
13089 -- The "statically deeper relationship" does not apply
13090 -- to generic formal access types, so a prefix of such
13091 -- a type is a "bad" prefix.
13093 if Is_Generic_Formal (Exp_Type) then
13096 -- The "statically deeper relationship" does apply to
13097 -- any other named access type.
13099 elsif not Is_Anonymous_Access_Type (Exp_Type) then
13103 pragma Assert (Is_Itype (Exp_Type));
13104 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13106 -- The "statically deeper relationship" applies to some
13107 -- anonymous access types and not to others. Return
13108 -- True for the cases where it does not apply. Also check
13109 -- recursively for the
13110 -- <prefix>.all.Access_Discrim.all.Access_Discrim case,
13111 -- where the correct result depends on <prefix>.
13113 return Nkind (Associated_Node) in
13114 N_Procedure_Specification | -- access parameter
13115 N_Function_Specification | -- access parameter
13116 N_Object_Declaration -- saooaaat
13117 or else Is_Discrim_Of_Bad_Access_Conversion_Argument (Deref_Prefix);
13118 end Is_Discrim_Of_Bad_Access_Conversion_Argument;
13120 ----------------------------
13121 -- Valid_Array_Conversion --
13122 ----------------------------
13124 function Valid_Array_Conversion return Boolean is
13125 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
13126 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
13128 Opnd_Index : Node_Id;
13129 Opnd_Index_Type : Entity_Id;
13131 Target_Comp_Type : constant Entity_Id :=
13132 Component_Type (Target_Type);
13133 Target_Comp_Base : constant Entity_Id :=
13134 Base_Type (Target_Comp_Type);
13136 Target_Index : Node_Id;
13137 Target_Index_Type : Entity_Id;
13140 -- Error if wrong number of dimensions
13143 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
13146 ("incompatible number of dimensions for conversion", Operand);
13149 -- Number of dimensions matches
13152 -- Loop through indexes of the two arrays
13154 Target_Index := First_Index (Target_Type);
13155 Opnd_Index := First_Index (Opnd_Type);
13156 while Present (Target_Index) and then Present (Opnd_Index) loop
13157 Target_Index_Type := Etype (Target_Index);
13158 Opnd_Index_Type := Etype (Opnd_Index);
13160 -- Error if index types are incompatible
13162 if not (Is_Integer_Type (Target_Index_Type)
13163 and then Is_Integer_Type (Opnd_Index_Type))
13164 and then (Root_Type (Target_Index_Type)
13165 /= Root_Type (Opnd_Index_Type))
13168 ("incompatible index types for array conversion",
13173 Next_Index (Target_Index);
13174 Next_Index (Opnd_Index);
13177 -- If component types have same base type, all set
13179 if Target_Comp_Base = Opnd_Comp_Base then
13182 -- Here if base types of components are not the same. The only
13183 -- time this is allowed is if we have anonymous access types.
13185 -- The conversion of arrays of anonymous access types can lead
13186 -- to dangling pointers. AI-392 formalizes the accessibility
13187 -- checks that must be applied to such conversions to prevent
13188 -- out-of-scope references.
13190 elsif Ekind (Target_Comp_Base) in
13191 E_Anonymous_Access_Type
13192 | E_Anonymous_Access_Subprogram_Type
13193 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
13195 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
13197 if Type_Access_Level (Target_Type) <
13198 Deepest_Type_Access_Level (Opnd_Type)
13200 if In_Instance_Body then
13201 Error_Msg_Warn := SPARK_Mode /= On;
13203 ("source array type has deeper accessibility "
13204 & "level than target<<", Operand);
13205 Conversion_Error_N ("\Program_Error [<<", Operand);
13207 Make_Raise_Program_Error (Sloc (N),
13208 Reason => PE_Accessibility_Check_Failed));
13209 Set_Etype (N, Target_Type);
13212 -- Conversion not allowed because of accessibility levels
13216 ("source array type has deeper accessibility "
13217 & "level than target", Operand);
13225 -- All other cases where component base types do not match
13229 ("incompatible component types for array conversion",
13234 -- Check that component subtypes statically match. For numeric
13235 -- types this means that both must be either constrained or
13236 -- unconstrained. For enumeration types the bounds must match.
13237 -- All of this is checked in Subtypes_Statically_Match.
13239 if not Subtypes_Statically_Match
13240 (Target_Comp_Type, Opnd_Comp_Type)
13243 ("component subtypes must statically match", Operand);
13249 end Valid_Array_Conversion;
13251 -----------------------------
13252 -- Valid_Tagged_Conversion --
13253 -----------------------------
13255 function Valid_Tagged_Conversion
13256 (Target_Type : Entity_Id;
13257 Opnd_Type : Entity_Id) return Boolean
13260 -- Upward conversions are allowed (RM 4.6(22))
13262 if Covers (Target_Type, Opnd_Type)
13263 or else Is_Ancestor (Target_Type, Opnd_Type)
13267 -- Downward conversion are allowed if the operand is class-wide
13270 elsif Is_Class_Wide_Type (Opnd_Type)
13271 and then Covers (Opnd_Type, Target_Type)
13275 elsif Covers (Opnd_Type, Target_Type)
13276 or else Is_Ancestor (Opnd_Type, Target_Type)
13279 Conversion_Check (False,
13280 "downward conversion of tagged objects not allowed");
13282 -- Ada 2005 (AI-251): The conversion to/from interface types is
13283 -- always valid. The types involved may be class-wide (sub)types.
13285 elsif Is_Interface (Etype (Base_Type (Target_Type)))
13286 or else Is_Interface (Etype (Base_Type (Opnd_Type)))
13290 -- If the operand is a class-wide type obtained through a limited_
13291 -- with clause, and the context includes the nonlimited view, use
13292 -- it to determine whether the conversion is legal.
13294 elsif Is_Class_Wide_Type (Opnd_Type)
13295 and then From_Limited_With (Opnd_Type)
13296 and then Present (Non_Limited_View (Etype (Opnd_Type)))
13297 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
13301 elsif Is_Access_Type (Opnd_Type)
13302 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
13307 Conversion_Error_NE
13308 ("invalid tagged conversion, not compatible with}",
13309 N, First_Subtype (Opnd_Type));
13312 end Valid_Tagged_Conversion;
13314 -- Start of processing for Valid_Conversion
13317 Check_Parameterless_Call (Operand);
13319 if Is_Overloaded (Operand) then
13329 -- Remove procedure calls, which syntactically cannot appear in
13330 -- this context, but which cannot be removed by type checking,
13331 -- because the context does not impose a type.
13333 -- The node may be labelled overloaded, but still contain only one
13334 -- interpretation because others were discarded earlier. If this
13335 -- is the case, retain the single interpretation if legal.
13337 Get_First_Interp (Operand, I, It);
13338 Opnd_Type := It.Typ;
13339 Get_Next_Interp (I, It);
13341 if Present (It.Typ)
13342 and then Opnd_Type /= Standard_Void_Type
13344 -- More than one candidate interpretation is available
13346 Get_First_Interp (Operand, I, It);
13347 while Present (It.Typ) loop
13348 if It.Typ = Standard_Void_Type then
13352 -- When compiling for a system where Address is of a visible
13353 -- integer type, spurious ambiguities can be produced when
13354 -- arithmetic operations have a literal operand and return
13355 -- System.Address or a descendant of it. These ambiguities
13356 -- are usually resolved by the context, but for conversions
13357 -- there is no context type and the removal of the spurious
13358 -- operations must be done explicitly here.
13360 if not Address_Is_Private
13361 and then Is_Descendant_Of_Address (It.Typ)
13366 Get_Next_Interp (I, It);
13370 Get_First_Interp (Operand, I, It);
13374 if No (It.Typ) then
13375 Conversion_Error_N ("illegal operand in conversion", Operand);
13379 Get_Next_Interp (I, It);
13381 if Present (It.Typ) then
13384 It1 := Disambiguate (Operand, I1, I, Any_Type);
13386 if It1 = No_Interp then
13388 ("ambiguous operand in conversion", Operand);
13390 -- If the interpretation involves a standard operator, use
13391 -- the location of the type, which may be user-defined.
13393 if Sloc (It.Nam) = Standard_Location then
13394 Error_Msg_Sloc := Sloc (It.Typ);
13396 Error_Msg_Sloc := Sloc (It.Nam);
13399 Conversion_Error_N -- CODEFIX
13400 ("\\possible interpretation#!", Operand);
13402 if Sloc (N1) = Standard_Location then
13403 Error_Msg_Sloc := Sloc (T1);
13405 Error_Msg_Sloc := Sloc (N1);
13408 Conversion_Error_N -- CODEFIX
13409 ("\\possible interpretation#!", Operand);
13415 Set_Etype (Operand, It1.Typ);
13416 Opnd_Type := It1.Typ;
13420 -- Deal with conversion of integer type to address if the pragma
13421 -- Allow_Integer_Address is in effect. We convert the conversion to
13422 -- an unchecked conversion in this case and we are all done.
13424 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
13425 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
13426 Analyze_And_Resolve (N, Target_Type);
13430 -- If we are within a child unit, check whether the type of the
13431 -- expression has an ancestor in a parent unit, in which case it
13432 -- belongs to its derivation class even if the ancestor is private.
13433 -- See RM 7.3.1 (5.2/3).
13435 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
13439 if Is_Numeric_Type (Target_Type) then
13441 -- A universal fixed expression can be converted to any numeric type
13443 if Opnd_Type = Universal_Fixed then
13446 -- Also no need to check when in an instance or inlined body, because
13447 -- the legality has been established when the template was analyzed.
13448 -- Furthermore, numeric conversions may occur where only a private
13449 -- view of the operand type is visible at the instantiation point.
13450 -- This results in a spurious error if we check that the operand type
13451 -- is a numeric type.
13453 -- Note: in a previous version of this unit, the following tests were
13454 -- applied only for generated code (Comes_From_Source set to False),
13455 -- but in fact the test is required for source code as well, since
13456 -- this situation can arise in source code.
13458 elsif In_Instance_Code or else In_Inlined_Body then
13461 -- Otherwise we need the conversion check
13464 return Conversion_Check
13465 (Is_Numeric_Type (Opnd_Type)
13467 (Present (Inc_Ancestor)
13468 and then Is_Numeric_Type (Inc_Ancestor)),
13469 "illegal operand for numeric conversion");
13474 elsif Is_Array_Type (Target_Type) then
13475 if not Is_Array_Type (Opnd_Type)
13476 or else Opnd_Type = Any_Composite
13477 or else Opnd_Type = Any_String
13480 ("illegal operand for array conversion", Operand);
13484 return Valid_Array_Conversion;
13487 -- Ada 2005 (AI-251): Internally generated conversions of access to
13488 -- interface types added to force the displacement of the pointer to
13489 -- reference the corresponding dispatch table.
13491 elsif not Comes_From_Source (N)
13492 and then Is_Access_Type (Target_Type)
13493 and then Is_Interface (Designated_Type (Target_Type))
13497 -- Ada 2005 (AI-251): Anonymous access types where target references an
13500 elsif Is_Access_Type (Opnd_Type)
13501 and then Ekind (Target_Type) in
13502 E_General_Access_Type | E_Anonymous_Access_Type
13503 and then Is_Interface (Directly_Designated_Type (Target_Type))
13505 -- Check the static accessibility rule of 4.6(17). Note that the
13506 -- check is not enforced when within an instance body, since the
13507 -- RM requires such cases to be caught at run time.
13509 -- If the operand is a rewriting of an allocator no check is needed
13510 -- because there are no accessibility issues.
13512 if Nkind (Original_Node (N)) = N_Allocator then
13515 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
13516 if Type_Access_Level (Opnd_Type) >
13517 Deepest_Type_Access_Level (Target_Type)
13519 -- In an instance, this is a run-time check, but one we know
13520 -- will fail, so generate an appropriate warning. The raise
13521 -- will be generated by Expand_N_Type_Conversion.
13523 if In_Instance_Body then
13524 Error_Msg_Warn := SPARK_Mode /= On;
13526 ("cannot convert local pointer to non-local access type<<",
13528 Conversion_Error_N ("\Program_Error [<<", Operand);
13532 ("cannot convert local pointer to non-local access type",
13537 -- Special accessibility checks are needed in the case of access
13538 -- discriminants declared for a limited type.
13540 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
13541 and then not Is_Local_Anonymous_Access (Opnd_Type)
13543 -- When the operand is a selected access discriminant the check
13544 -- needs to be made against the level of the object denoted by
13545 -- the prefix of the selected name (Accessibility_Level handles
13546 -- checking the prefix of the operand for this case).
13548 if Nkind (Operand) = N_Selected_Component
13549 and then Static_Accessibility_Level
13550 (Operand, Zero_On_Dynamic_Level)
13551 > Deepest_Type_Access_Level (Target_Type)
13553 -- In an instance, this is a run-time check, but one we know
13554 -- will fail, so generate an appropriate warning. The raise
13555 -- will be generated by Expand_N_Type_Conversion.
13557 if In_Instance_Body then
13558 Error_Msg_Warn := SPARK_Mode /= On;
13560 ("cannot convert access discriminant to non-local "
13561 & "access type<<", Operand);
13562 Conversion_Error_N ("\Program_Error [<<", Operand);
13564 -- Real error if not in instance body
13568 ("cannot convert access discriminant to non-local "
13569 & "access type", Operand);
13574 -- The case of a reference to an access discriminant from
13575 -- within a limited type declaration (which will appear as
13576 -- a discriminal) is always illegal because the level of the
13577 -- discriminant is considered to be deeper than any (nameable)
13580 if Is_Entity_Name (Operand)
13581 and then not Is_Local_Anonymous_Access (Opnd_Type)
13583 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
13584 and then Present (Discriminal_Link (Entity (Operand)))
13587 ("discriminant has deeper accessibility level than target",
13596 -- General and anonymous access types
13598 elsif Ekind (Target_Type) in
13599 E_General_Access_Type | E_Anonymous_Access_Type
13602 (Is_Access_Type (Opnd_Type)
13604 Ekind (Opnd_Type) not in
13605 E_Access_Subprogram_Type |
13606 E_Access_Protected_Subprogram_Type,
13607 "must be an access-to-object type")
13609 if Is_Access_Constant (Opnd_Type)
13610 and then not Is_Access_Constant (Target_Type)
13613 ("access-to-constant operand type not allowed", Operand);
13617 -- Check the static accessibility rule of 4.6(17). Note that the
13618 -- check is not enforced when within an instance body, since the RM
13619 -- requires such cases to be caught at run time.
13621 if Ekind (Target_Type) /= E_Anonymous_Access_Type
13622 or else Is_Local_Anonymous_Access (Target_Type)
13623 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
13624 N_Object_Declaration
13626 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
13627 -- conversions from an anonymous access type to a named general
13628 -- access type. Such conversions are not allowed in the case of
13629 -- access parameters and stand-alone objects of an anonymous
13630 -- access type. The implicit conversion case is recognized by
13631 -- testing that Comes_From_Source is False and that it's been
13632 -- rewritten. The Comes_From_Source test isn't sufficient because
13633 -- nodes in inlined calls to predefined library routines can have
13634 -- Comes_From_Source set to False. (Is there a better way to test
13635 -- for implicit conversions???).
13637 -- Do not treat a rewritten 'Old attribute reference like other
13638 -- rewrite substitutions. This makes a difference, for example,
13639 -- in the case where we are generating the expansion of a
13640 -- membership test of the form
13641 -- Saooaaat'Old in Named_Access_Type
13642 -- because in this case Valid_Conversion needs to return True
13643 -- (otherwise the expansion will be False - see the call site
13644 -- in exp_ch4.adb).
13646 if Ada_Version >= Ada_2012
13647 and then not Comes_From_Source (N)
13648 and then Is_Rewrite_Substitution (N)
13649 and then not Is_Attribute_Old (Original_Node (N))
13650 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
13651 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
13653 if Is_Itype (Opnd_Type) then
13655 -- Implicit conversions aren't allowed for objects of an
13656 -- anonymous access type, since such objects have nonstatic
13657 -- levels in Ada 2012.
13659 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
13660 N_Object_Declaration
13663 ("implicit conversion of stand-alone anonymous "
13664 & "access object not allowed", Operand);
13667 -- Implicit conversions aren't allowed for anonymous access
13668 -- parameters. We exclude anonymous access results as well
13669 -- as universal_access "=".
13671 elsif not Is_Local_Anonymous_Access (Opnd_Type)
13672 and then Nkind (Associated_Node_For_Itype (Opnd_Type)) in
13673 N_Function_Specification |
13674 N_Procedure_Specification
13675 and then Nkind (Parent (N)) not in N_Op_Eq | N_Op_Ne
13678 ("implicit conversion of anonymous access parameter "
13679 & "not allowed", Operand);
13682 -- Detect access discriminant values that are illegal
13683 -- implicit anonymous-to-named access conversion operands.
13685 elsif Is_Discrim_Of_Bad_Access_Conversion_Argument (Operand)
13688 ("implicit conversion of anonymous access value "
13689 & "not allowed", Operand);
13692 -- In other cases, the level of the operand's type must be
13693 -- statically less deep than that of the target type, else
13694 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
13696 elsif Type_Access_Level (Opnd_Type) >
13697 Deepest_Type_Access_Level (Target_Type)
13700 ("implicit conversion of anonymous access value "
13701 & "violates accessibility", Operand);
13706 -- Check if the operand is deeper than the target type, taking
13707 -- care to avoid the case where we are converting a result of a
13708 -- function returning an anonymous access type since the "master
13709 -- of the call" would be target type of the conversion unless
13710 -- the target type is anonymous access as well - see RM 3.10.2
13713 elsif Type_Access_Level (Opnd_Type) >
13714 Deepest_Type_Access_Level (Target_Type)
13715 and then (Nkind (Associated_Node_For_Itype (Opnd_Type)) /=
13716 N_Function_Specification
13717 or else Ekind (Target_Type) in
13718 Anonymous_Access_Kind)
13720 -- Check we are not in a return value ???
13722 and then (not In_Return_Value (N)
13724 Nkind (Associated_Node_For_Itype (Target_Type))
13725 = N_Component_Declaration)
13727 -- In an instance, this is a run-time check, but one we know
13728 -- will fail, so generate an appropriate warning. The raise
13729 -- will be generated by Expand_N_Type_Conversion.
13731 if In_Instance_Body then
13732 Error_Msg_Warn := SPARK_Mode /= On;
13734 ("cannot convert local pointer to non-local access type<<",
13736 Conversion_Error_N ("\Program_Error [<<", Operand);
13738 -- If not in an instance body, this is a real error
13741 -- Avoid generation of spurious error message
13743 if not Error_Posted (N) then
13745 ("cannot convert local pointer to non-local access type",
13752 -- Special accessibility checks are needed in the case of access
13753 -- discriminants declared for a limited type.
13755 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
13756 and then not Is_Local_Anonymous_Access (Opnd_Type)
13758 -- When the operand is a selected access discriminant the check
13759 -- needs to be made against the level of the object denoted by
13760 -- the prefix of the selected name (Accessibility_Level handles
13761 -- checking the prefix of the operand for this case).
13763 if Nkind (Operand) = N_Selected_Component
13764 and then Static_Accessibility_Level
13765 (Operand, Zero_On_Dynamic_Level)
13766 > Deepest_Type_Access_Level (Target_Type)
13768 -- In an instance, this is a run-time check, but one we know
13769 -- will fail, so generate an appropriate warning. The raise
13770 -- will be generated by Expand_N_Type_Conversion.
13772 if In_Instance_Body then
13773 Error_Msg_Warn := SPARK_Mode /= On;
13775 ("cannot convert access discriminant to non-local "
13776 & "access type<<", Operand);
13777 Conversion_Error_N ("\Program_Error [<<", Operand);
13779 -- If not in an instance body, this is a real error
13783 ("cannot convert access discriminant to non-local "
13784 & "access type", Operand);
13789 -- The case of a reference to an access discriminant from
13790 -- within a limited type declaration (which will appear as
13791 -- a discriminal) is always illegal because the level of the
13792 -- discriminant is considered to be deeper than any (nameable)
13795 if Is_Entity_Name (Operand)
13797 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
13798 and then Present (Discriminal_Link (Entity (Operand)))
13801 ("discriminant has deeper accessibility level than target",
13808 -- In the presence of limited_with clauses we have to use nonlimited
13809 -- views, if available.
13811 Check_Limited : declare
13812 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
13813 -- Helper function to handle limited views
13815 --------------------------
13816 -- Full_Designated_Type --
13817 --------------------------
13819 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
13820 Desig : constant Entity_Id := Designated_Type (T);
13823 -- Handle the limited view of a type
13825 if From_Limited_With (Desig)
13826 and then Has_Non_Limited_View (Desig)
13828 return Available_View (Desig);
13832 end Full_Designated_Type;
13834 -- Local Declarations
13836 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
13837 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
13839 Same_Base : constant Boolean :=
13840 Base_Type (Target) = Base_Type (Opnd);
13842 -- Start of processing for Check_Limited
13845 if Is_Tagged_Type (Target) then
13846 return Valid_Tagged_Conversion (Target, Opnd);
13849 if not Same_Base then
13850 Conversion_Error_NE
13851 ("target designated type not compatible with }",
13852 N, Base_Type (Opnd));
13855 -- Ada 2005 AI-384: legality rule is symmetric in both
13856 -- designated types. The conversion is legal (with possible
13857 -- constraint check) if either designated type is
13860 elsif Subtypes_Statically_Match (Target, Opnd)
13862 (Has_Discriminants (Target)
13864 (not Is_Constrained (Opnd)
13865 or else not Is_Constrained (Target)))
13867 -- Special case, if Value_Size has been used to make the
13868 -- sizes different, the conversion is not allowed even
13869 -- though the subtypes statically match.
13871 if Known_Static_RM_Size (Target)
13872 and then Known_Static_RM_Size (Opnd)
13873 and then RM_Size (Target) /= RM_Size (Opnd)
13875 Conversion_Error_NE
13876 ("target designated subtype not compatible with }",
13878 Conversion_Error_NE
13879 ("\because sizes of the two designated subtypes differ",
13883 -- Normal case where conversion is allowed
13891 ("target designated subtype not compatible with }",
13898 -- Access to subprogram types. If the operand is an access parameter,
13899 -- the type has a deeper accessibility that any master, and cannot be
13900 -- assigned. We must make an exception if the conversion is part of an
13901 -- assignment and the target is the return object of an extended return
13902 -- statement, because in that case the accessibility check takes place
13903 -- after the return.
13905 elsif Is_Access_Subprogram_Type (Target_Type)
13907 -- Note: this test of Opnd_Type is there to prevent entering this
13908 -- branch in the case of a remote access to subprogram type, which
13909 -- is internally represented as an E_Record_Type.
13911 and then Is_Access_Type (Opnd_Type)
13913 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
13914 and then Is_Entity_Name (Operand)
13915 and then Ekind (Entity (Operand)) = E_In_Parameter
13917 (Nkind (Parent (N)) /= N_Assignment_Statement
13918 or else not Is_Entity_Name (Name (Parent (N)))
13919 or else not Is_Return_Object (Entity (Name (Parent (N)))))
13922 ("illegal attempt to store anonymous access to subprogram",
13925 ("\value has deeper accessibility than any master "
13926 & "(RM 3.10.2 (13))",
13930 ("\use named access type for& instead of access parameter",
13931 Operand, Entity (Operand));
13934 -- Check that the designated types are subtype conformant
13936 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
13937 Old_Id => Designated_Type (Opnd_Type),
13940 -- Check the static accessibility rule of 4.6(20)
13942 if Type_Access_Level (Opnd_Type) >
13943 Deepest_Type_Access_Level (Target_Type)
13946 ("operand type has deeper accessibility level than target",
13949 -- Check that if the operand type is declared in a generic body,
13950 -- then the target type must be declared within that same body
13951 -- (enforces last sentence of 4.6(20)).
13953 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
13955 O_Gen : constant Node_Id :=
13956 Enclosing_Generic_Body (Opnd_Type);
13961 T_Gen := Enclosing_Generic_Body (Target_Type);
13962 while Present (T_Gen) and then T_Gen /= O_Gen loop
13963 T_Gen := Enclosing_Generic_Body (T_Gen);
13966 if T_Gen /= O_Gen then
13968 ("target type must be declared in same generic body "
13969 & "as operand type", N);
13976 -- Remote access to subprogram types
13978 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
13979 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
13981 -- It is valid to convert from one RAS type to another provided
13982 -- that their specification statically match.
13984 -- Note: at this point, remote access to subprogram types have been
13985 -- expanded to their E_Record_Type representation, and we need to
13986 -- go back to the original access type definition using the
13987 -- Corresponding_Remote_Type attribute in order to check that the
13988 -- designated profiles match.
13990 pragma Assert (Ekind (Target_Type) = E_Record_Type);
13991 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
13993 Check_Subtype_Conformant
13995 Designated_Type (Corresponding_Remote_Type (Target_Type)),
13997 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
14002 -- If it was legal in the generic, it's legal in the instance
14004 elsif In_Instance_Body then
14007 -- If both are tagged types, check legality of view conversions
14009 elsif Is_Tagged_Type (Target_Type)
14011 Is_Tagged_Type (Opnd_Type)
14013 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
14015 -- Types derived from the same root type are convertible
14017 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
14020 -- In an instance or an inlined body, there may be inconsistent views of
14021 -- the same type, or of types derived from a common root.
14023 elsif (In_Instance or In_Inlined_Body)
14025 Root_Type (Underlying_Type (Target_Type)) =
14026 Root_Type (Underlying_Type (Opnd_Type))
14030 -- Special check for common access type error case
14032 elsif Ekind (Target_Type) = E_Access_Type
14033 and then Is_Access_Type (Opnd_Type)
14035 Conversion_Error_N ("target type must be general access type!", N);
14036 Conversion_Error_NE -- CODEFIX
14037 ("\add ALL to }!", N, Target_Type);
14040 -- Here we have a real conversion error
14043 -- Check for missing regular with_clause when only a limited view of
14044 -- target is available.
14046 if From_Limited_With (Opnd_Type) and then In_Package_Body then
14047 Conversion_Error_NE
14048 ("invalid conversion, not compatible with limited view of }",
14050 Conversion_Error_NE
14051 ("\add with_clause for& to current unit!", N, Scope (Opnd_Type));
14053 elsif Is_Access_Type (Opnd_Type)
14054 and then From_Limited_With (Designated_Type (Opnd_Type))
14055 and then In_Package_Body
14057 Conversion_Error_NE
14058 ("invalid conversion, not compatible with }", N, Opnd_Type);
14059 Conversion_Error_NE
14060 ("\add with_clause for& to current unit!",
14061 N, Scope (Designated_Type (Opnd_Type)));
14064 Conversion_Error_NE
14065 ("invalid conversion, not compatible with }", N, Opnd_Type);
14070 end Valid_Conversion;