1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Output; use Output;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Elab; use Sem_Elab;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Intr; use Sem_Intr;
66 with Sem_Util; use Sem_Util;
67 with Sem_Type; use Sem_Type;
68 with Sem_Warn; use Sem_Warn;
69 with Sinfo; use Sinfo;
70 with Sinfo.CN; use Sinfo.CN;
71 with Snames; use Snames;
72 with Stand; use Stand;
73 with Stringt; use Stringt;
74 with Style; use Style;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
79 package body Sem_Res is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 -- Second pass (top-down) type checking and overload resolution procedures
86 -- Typ is the type required by context. These procedures propagate the type
87 -- information recursively to the descendants of N. If the node is not
88 -- overloaded, its Etype is established in the first pass. If overloaded,
89 -- the Resolve routines set the correct type. For arith. operators, the
90 -- Etype is the base type of the context.
92 -- Note that Resolve_Attribute is separated off in Sem_Attr
94 function Bad_Unordered_Enumeration_Reference
96 T : Entity_Id) return Boolean;
97 -- Node N contains a potentially dubious reference to type T, either an
98 -- explicit comparison, or an explicit range. This function returns True
99 -- if the type T is an enumeration type for which No pragma Order has been
100 -- given, and the reference N is not in the same extended source unit as
101 -- the declaration of T.
103 procedure Check_Discriminant_Use (N : Node_Id);
104 -- Enforce the restrictions on the use of discriminants when constraining
105 -- a component of a discriminated type (record or concurrent type).
107 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
108 -- Given a node for an operator associated with type T, check that
109 -- the operator is visible. Operators all of whose operands are
110 -- universal must be checked for visibility during resolution
111 -- because their type is not determinable based on their operands.
113 procedure Check_Fully_Declared_Prefix
116 -- Check that the type of the prefix of a dereference is not incomplete
118 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
119 -- Given a call node, N, which is known to occur immediately within the
120 -- subprogram being called, determines whether it is a detectable case of
121 -- an infinite recursion, and if so, outputs appropriate messages. Returns
122 -- True if an infinite recursion is detected, and False otherwise.
124 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
125 -- If the type of the object being initialized uses the secondary stack
126 -- directly or indirectly, create a transient scope for the call to the
127 -- init proc. This is because we do not create transient scopes for the
128 -- initialization of individual components within the init proc itself.
129 -- Could be optimized away perhaps?
131 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
132 -- N is the node for a logical operator. If the operator is predefined, and
133 -- the root type of the operands is Standard.Boolean, then a check is made
134 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
135 -- the style check for Style_Check_Boolean_And_Or.
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 Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
152 -- If a default expression in entry call N depends on the discriminants
153 -- of the task, it must be replaced with a reference to the discriminant
154 -- of the task being called.
156 procedure Resolve_Op_Concat_Arg
161 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
162 -- concatenation operator. The operand is either of the array type or of
163 -- the component type. If the operand is an aggregate, and the component
164 -- type is composite, this is ambiguous if component type has aggregates.
166 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
167 -- Does the first part of the work of Resolve_Op_Concat
169 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
170 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
171 -- has been resolved. See Resolve_Op_Concat for details.
173 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
174 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
209 function Operator_Kind
211 Is_Binary : Boolean) return Node_Kind;
212 -- Utility to map the name of an operator into the corresponding Node. Used
213 -- by other node rewriting procedures.
215 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
216 -- Resolve actuals of call, and add default expressions for missing ones.
217 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
218 -- called subprogram.
220 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
221 -- Called from Resolve_Call, when the prefix denotes an entry or element
222 -- of entry family. Actuals are resolved as for subprograms, and the node
223 -- is rebuilt as an entry call. Also called for protected operations. Typ
224 -- is the context type, which is used when the operation is a protected
225 -- function with no arguments, and the return value is indexed.
227 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
228 -- A call to a user-defined intrinsic operator is rewritten as a call to
229 -- the corresponding predefined operator, with suitable conversions. Note
230 -- that this applies only for intrinsic operators that denote predefined
231 -- operators, not ones that are intrinsic imports of back-end builtins.
233 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
234 -- Ditto, for unary operators (arithmetic ones and "not" on signed
235 -- integer types for VMS).
237 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
238 -- If an operator node resolves to a call to a user-defined operator,
239 -- rewrite the node as a function call.
241 procedure Make_Call_Into_Operator
245 -- Inverse transformation: if an operator is given in functional notation,
246 -- then after resolving the node, transform into an operator node, so
247 -- that operands are resolved properly. Recall that predefined operators
248 -- do not have a full signature and special resolution rules apply.
250 procedure Rewrite_Renamed_Operator
254 -- An operator can rename another, e.g. in an instantiation. In that
255 -- case, the proper operator node must be constructed and resolved.
257 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
258 -- The String_Literal_Subtype is built for all strings that are not
259 -- operands of a static concatenation operation. If the argument is
260 -- not a N_String_Literal node, then the call has no effect.
262 procedure Set_Slice_Subtype (N : Node_Id);
263 -- Build subtype of array type, with the range specified by the slice
265 procedure Simplify_Type_Conversion (N : Node_Id);
266 -- Called after N has been resolved and evaluated, but before range checks
267 -- have been applied. Currently simplifies a combination of floating-point
268 -- to integer conversion and Truncation attribute.
270 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
271 -- A universal_fixed expression in an universal context is unambiguous if
272 -- there is only one applicable fixed point type. Determining whether there
273 -- is only one requires a search over all visible entities, and happens
274 -- only in very pathological cases (see 6115-006).
276 -------------------------
277 -- Ambiguous_Character --
278 -------------------------
280 procedure Ambiguous_Character (C : Node_Id) is
284 if Nkind (C) = N_Character_Literal then
285 Error_Msg_N ("ambiguous character literal", C);
287 -- First the ones in Standard
289 Error_Msg_N ("\\possible interpretation: Character!", C);
290 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
292 -- Include Wide_Wide_Character in Ada 2005 mode
294 if Ada_Version >= Ada_2005 then
295 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
298 -- Now any other types that match
300 E := Current_Entity (C);
301 while Present (E) loop
302 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
306 end Ambiguous_Character;
308 -------------------------
309 -- Analyze_And_Resolve --
310 -------------------------
312 procedure Analyze_And_Resolve (N : Node_Id) is
316 end Analyze_And_Resolve;
318 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
322 end Analyze_And_Resolve;
324 -- Version withs check(s) suppressed
326 procedure Analyze_And_Resolve
331 Scop : constant Entity_Id := Current_Scope;
334 if Suppress = All_Checks then
336 Svg : constant Suppress_Array := Scope_Suppress;
338 Scope_Suppress := (others => True);
339 Analyze_And_Resolve (N, Typ);
340 Scope_Suppress := Svg;
345 Svg : constant Boolean := Scope_Suppress (Suppress);
348 Scope_Suppress (Suppress) := True;
349 Analyze_And_Resolve (N, Typ);
350 Scope_Suppress (Suppress) := Svg;
354 if Current_Scope /= Scop
355 and then Scope_Is_Transient
357 -- This can only happen if a transient scope was created for an inner
358 -- expression, which will be removed upon completion of the analysis
359 -- of an enclosing construct. The transient scope must have the
360 -- suppress status of the enclosing environment, not of this Analyze
363 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
366 end Analyze_And_Resolve;
368 procedure Analyze_And_Resolve
372 Scop : constant Entity_Id := Current_Scope;
375 if Suppress = All_Checks then
377 Svg : constant Suppress_Array := Scope_Suppress;
379 Scope_Suppress := (others => True);
380 Analyze_And_Resolve (N);
381 Scope_Suppress := Svg;
386 Svg : constant Boolean := Scope_Suppress (Suppress);
389 Scope_Suppress (Suppress) := True;
390 Analyze_And_Resolve (N);
391 Scope_Suppress (Suppress) := Svg;
395 if Current_Scope /= Scop
396 and then Scope_Is_Transient
398 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
401 end Analyze_And_Resolve;
403 ----------------------------------------
404 -- Bad_Unordered_Enumeration_Reference --
405 ----------------------------------------
407 function Bad_Unordered_Enumeration_Reference
409 T : Entity_Id) return Boolean
412 return Is_Enumeration_Type (T)
413 and then Comes_From_Source (N)
414 and then Warn_On_Unordered_Enumeration_Type
415 and then not Has_Pragma_Ordered (T)
416 and then not In_Same_Extended_Unit (N, T);
417 end Bad_Unordered_Enumeration_Reference;
419 ----------------------------
420 -- Check_Discriminant_Use --
421 ----------------------------
423 procedure Check_Discriminant_Use (N : Node_Id) is
424 PN : constant Node_Id := Parent (N);
425 Disc : constant Entity_Id := Entity (N);
430 -- Any use in a spec-expression is legal
432 if In_Spec_Expression then
435 elsif Nkind (PN) = N_Range then
437 -- Discriminant cannot be used to constrain a scalar type
441 if Nkind (P) = N_Range_Constraint
442 and then Nkind (Parent (P)) = N_Subtype_Indication
443 and then Nkind (Parent (Parent (P))) = N_Component_Definition
445 Error_Msg_N ("discriminant cannot constrain scalar type", N);
447 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
449 -- The following check catches the unusual case where a
450 -- discriminant appears within an index constraint that is part of
451 -- a larger expression within a constraint on a component, e.g. "C
452 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
453 -- of record components, and note that a similar check should also
454 -- apply in the case of discriminant constraints below. ???
456 -- Note that the check for N_Subtype_Declaration below is to
457 -- detect the valid use of discriminants in the constraints of a
458 -- subtype declaration when this subtype declaration appears
459 -- inside the scope of a record type (which is syntactically
460 -- illegal, but which may be created as part of derived type
461 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
464 if Ekind (Current_Scope) = E_Record_Type
465 and then Scope (Disc) = Current_Scope
467 (Nkind (Parent (P)) = N_Subtype_Indication
469 Nkind_In (Parent (Parent (P)), N_Component_Definition,
470 N_Subtype_Declaration)
471 and then Paren_Count (N) = 0)
474 ("discriminant must appear alone in component constraint", N);
478 -- Detect a common error:
480 -- type R (D : Positive := 100) is record
481 -- Name : String (1 .. D);
484 -- The default value causes an object of type R to be allocated
485 -- with room for Positive'Last characters. The RM does not mandate
486 -- the allocation of the maximum size, but that is what GNAT does
487 -- so we should warn the programmer that there is a problem.
489 Check_Large : declare
495 function Large_Storage_Type (T : Entity_Id) return Boolean;
496 -- Return True if type T has a large enough range that any
497 -- array whose index type covered the whole range of the type
498 -- would likely raise Storage_Error.
500 ------------------------
501 -- Large_Storage_Type --
502 ------------------------
504 function Large_Storage_Type (T : Entity_Id) return Boolean is
506 -- The type is considered large if its bounds are known at
507 -- compile time and if it requires at least as many bits as
508 -- a Positive to store the possible values.
510 return Compile_Time_Known_Value (Type_Low_Bound (T))
511 and then Compile_Time_Known_Value (Type_High_Bound (T))
513 Minimum_Size (T, Biased => True) >=
514 RM_Size (Standard_Positive);
515 end Large_Storage_Type;
517 -- Start of processing for Check_Large
520 -- Check that the Disc has a large range
522 if not Large_Storage_Type (Etype (Disc)) then
526 -- If the enclosing type is limited, we allocate only the
527 -- default value, not the maximum, and there is no need for
530 if Is_Limited_Type (Scope (Disc)) then
534 -- Check that it is the high bound
536 if N /= High_Bound (PN)
537 or else No (Discriminant_Default_Value (Disc))
542 -- Check the array allows a large range at this bound. First
547 if Nkind (SI) /= N_Subtype_Indication then
551 T := Entity (Subtype_Mark (SI));
553 if not Is_Array_Type (T) then
557 -- Next, find the dimension
559 TB := First_Index (T);
560 CB := First (Constraints (P));
562 and then Present (TB)
563 and then Present (CB)
574 -- Now, check the dimension has a large range
576 if not Large_Storage_Type (Etype (TB)) then
580 -- Warn about the danger
583 ("?creation of & object may raise Storage_Error!",
592 -- Legal case is in index or discriminant constraint
594 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
595 N_Discriminant_Association)
597 if Paren_Count (N) > 0 then
599 ("discriminant in constraint must appear alone", N);
601 elsif Nkind (N) = N_Expanded_Name
602 and then Comes_From_Source (N)
605 ("discriminant must appear alone as a direct name", N);
610 -- Otherwise, context is an expression. It should not be within (i.e. a
611 -- subexpression of) a constraint for a component.
616 while not Nkind_In (P, N_Component_Declaration,
617 N_Subtype_Indication,
625 -- If the discriminant is used in an expression that is a bound of a
626 -- scalar type, an Itype is created and the bounds are attached to
627 -- its range, not to the original subtype indication. Such use is of
628 -- course a double fault.
630 if (Nkind (P) = N_Subtype_Indication
631 and then Nkind_In (Parent (P), N_Component_Definition,
632 N_Derived_Type_Definition)
633 and then D = Constraint (P))
635 -- The constraint itself may be given by a subtype indication,
636 -- rather than by a more common discrete range.
638 or else (Nkind (P) = N_Subtype_Indication
640 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
641 or else Nkind (P) = N_Entry_Declaration
642 or else Nkind (D) = N_Defining_Identifier
645 ("discriminant in constraint must appear alone", N);
648 end Check_Discriminant_Use;
650 --------------------------------
651 -- Check_For_Visible_Operator --
652 --------------------------------
654 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
656 if Is_Invisible_Operator (N, T) then
657 Error_Msg_NE -- CODEFIX
658 ("operator for} is not directly visible!", N, First_Subtype (T));
659 Error_Msg_N -- CODEFIX
660 ("use clause would make operation legal!", N);
662 end Check_For_Visible_Operator;
664 ----------------------------------
665 -- Check_Fully_Declared_Prefix --
666 ----------------------------------
668 procedure Check_Fully_Declared_Prefix
673 -- Check that the designated type of the prefix of a dereference is
674 -- not an incomplete type. This cannot be done unconditionally, because
675 -- dereferences of private types are legal in default expressions. This
676 -- case is taken care of in Check_Fully_Declared, called below. There
677 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
679 -- This consideration also applies to similar checks for allocators,
680 -- qualified expressions, and type conversions.
682 -- An additional exception concerns other per-object expressions that
683 -- are not directly related to component declarations, in particular
684 -- representation pragmas for tasks. These will be per-object
685 -- expressions if they depend on discriminants or some global entity.
686 -- If the task has access discriminants, the designated type may be
687 -- incomplete at the point the expression is resolved. This resolution
688 -- takes place within the body of the initialization procedure, where
689 -- the discriminant is replaced by its discriminal.
691 if Is_Entity_Name (Pref)
692 and then Ekind (Entity (Pref)) = E_In_Parameter
696 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
697 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
698 -- Analyze_Object_Renaming, and Freeze_Entity.
700 elsif Ada_Version >= Ada_2005
701 and then Is_Entity_Name (Pref)
702 and then Is_Access_Type (Etype (Pref))
703 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
705 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
709 Check_Fully_Declared (Typ, Parent (Pref));
711 end Check_Fully_Declared_Prefix;
713 ------------------------------
714 -- Check_Infinite_Recursion --
715 ------------------------------
717 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
721 function Same_Argument_List return Boolean;
722 -- Check whether list of actuals is identical to list of formals of
723 -- called function (which is also the enclosing scope).
725 ------------------------
726 -- Same_Argument_List --
727 ------------------------
729 function Same_Argument_List return Boolean is
735 if not Is_Entity_Name (Name (N)) then
738 Subp := Entity (Name (N));
741 F := First_Formal (Subp);
742 A := First_Actual (N);
743 while Present (F) and then Present (A) loop
744 if not Is_Entity_Name (A)
745 or else Entity (A) /= F
755 end Same_Argument_List;
757 -- Start of processing for Check_Infinite_Recursion
760 -- Special case, if this is a procedure call and is a call to the
761 -- current procedure with the same argument list, then this is for
762 -- sure an infinite recursion and we insert a call to raise SE.
764 if Is_List_Member (N)
765 and then List_Length (List_Containing (N)) = 1
766 and then Same_Argument_List
769 P : constant Node_Id := Parent (N);
771 if Nkind (P) = N_Handled_Sequence_Of_Statements
772 and then Nkind (Parent (P)) = N_Subprogram_Body
773 and then Is_Empty_List (Declarations (Parent (P)))
775 Error_Msg_N ("!?infinite recursion", N);
776 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
778 Make_Raise_Storage_Error (Sloc (N),
779 Reason => SE_Infinite_Recursion));
785 -- If not that special case, search up tree, quitting if we reach a
786 -- construct (e.g. a conditional) that tells us that this is not a
787 -- case for an infinite recursion warning.
793 -- If no parent, then we were not inside a subprogram, this can for
794 -- example happen when processing certain pragmas in a spec. Just
795 -- return False in this case.
801 -- Done if we get to subprogram body, this is definitely an infinite
802 -- recursion case if we did not find anything to stop us.
804 exit when Nkind (P) = N_Subprogram_Body;
806 -- If appearing in conditional, result is false
808 if Nkind_In (P, N_Or_Else,
812 N_Conditional_Expression,
817 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
818 and then C /= First (Statements (P))
820 -- If the call is the expression of a return statement and the
821 -- actuals are identical to the formals, it's worth a warning.
822 -- However, we skip this if there is an immediately preceding
823 -- raise statement, since the call is never executed.
825 -- Furthermore, this corresponds to a common idiom:
827 -- function F (L : Thing) return Boolean is
829 -- raise Program_Error;
833 -- for generating a stub function
835 if Nkind (Parent (N)) = N_Simple_Return_Statement
836 and then Same_Argument_List
838 exit when not Is_List_Member (Parent (N));
840 -- OK, return statement is in a statement list, look for raise
846 -- Skip past N_Freeze_Entity nodes generated by expansion
848 Nod := Prev (Parent (N));
850 and then Nkind (Nod) = N_Freeze_Entity
855 -- If no raise statement, give warning
857 exit when Nkind (Nod) /= N_Raise_Statement
859 (Nkind (Nod) not in N_Raise_xxx_Error
860 or else Present (Condition (Nod)));
871 Error_Msg_N ("!?possible infinite recursion", N);
872 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
875 end Check_Infinite_Recursion;
877 -------------------------------
878 -- Check_Initialization_Call --
879 -------------------------------
881 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
882 Typ : constant Entity_Id := Etype (First_Formal (Nam));
884 function Uses_SS (T : Entity_Id) return Boolean;
885 -- Check whether the creation of an object of the type will involve
886 -- use of the secondary stack. If T is a record type, this is true
887 -- if the expression for some component uses the secondary stack, e.g.
888 -- through a call to a function that returns an unconstrained value.
889 -- False if T is controlled, because cleanups occur elsewhere.
895 function Uses_SS (T : Entity_Id) return Boolean is
898 Full_Type : Entity_Id := Underlying_Type (T);
901 -- Normally we want to use the underlying type, but if it's not set
902 -- then continue with T.
904 if not Present (Full_Type) then
908 if Is_Controlled (Full_Type) then
911 elsif Is_Array_Type (Full_Type) then
912 return Uses_SS (Component_Type (Full_Type));
914 elsif Is_Record_Type (Full_Type) then
915 Comp := First_Component (Full_Type);
916 while Present (Comp) loop
917 if Ekind (Comp) = E_Component
918 and then Nkind (Parent (Comp)) = N_Component_Declaration
920 -- The expression for a dynamic component may be rewritten
921 -- as a dereference, so retrieve original node.
923 Expr := Original_Node (Expression (Parent (Comp)));
925 -- Return True if the expression is a call to a function
926 -- (including an attribute function such as Image, or a
927 -- user-defined operator) with a result that requires a
930 if (Nkind (Expr) = N_Function_Call
931 or else Nkind (Expr) in N_Op
932 or else (Nkind (Expr) = N_Attribute_Reference
933 and then Present (Expressions (Expr))))
934 and then Requires_Transient_Scope (Etype (Expr))
938 elsif Uses_SS (Etype (Comp)) then
943 Next_Component (Comp);
953 -- Start of processing for Check_Initialization_Call
956 -- Establish a transient scope if the type needs it
958 if Uses_SS (Typ) then
959 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
961 end Check_Initialization_Call;
963 ---------------------------------------
964 -- Check_No_Direct_Boolean_Operators --
965 ---------------------------------------
967 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
969 if Scope (Entity (N)) = Standard_Standard
970 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
972 -- Restriction only applies to original source code
974 if Comes_From_Source (N) then
975 Check_Restriction (No_Direct_Boolean_Operators, N);
980 Check_Boolean_Operator (N);
982 end Check_No_Direct_Boolean_Operators;
984 ------------------------------
985 -- Check_Parameterless_Call --
986 ------------------------------
988 procedure Check_Parameterless_Call (N : Node_Id) is
991 function Prefix_Is_Access_Subp return Boolean;
992 -- If the prefix is of an access_to_subprogram type, the node must be
993 -- rewritten as a call. Ditto if the prefix is overloaded and all its
994 -- interpretations are access to subprograms.
996 ---------------------------
997 -- Prefix_Is_Access_Subp --
998 ---------------------------
1000 function Prefix_Is_Access_Subp return Boolean is
1005 -- If the context is an attribute reference that can apply to
1006 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1008 if Nkind (Parent (N)) = N_Attribute_Reference
1009 and then (Attribute_Name (Parent (N)) = Name_Address or else
1010 Attribute_Name (Parent (N)) = Name_Code_Address or else
1011 Attribute_Name (Parent (N)) = Name_Access)
1016 if not Is_Overloaded (N) then
1018 Ekind (Etype (N)) = E_Subprogram_Type
1019 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1021 Get_First_Interp (N, I, It);
1022 while Present (It.Typ) loop
1023 if Ekind (It.Typ) /= E_Subprogram_Type
1024 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1029 Get_Next_Interp (I, It);
1034 end Prefix_Is_Access_Subp;
1036 -- Start of processing for Check_Parameterless_Call
1039 -- Defend against junk stuff if errors already detected
1041 if Total_Errors_Detected /= 0 then
1042 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1044 elsif Nkind (N) in N_Has_Chars
1045 and then Chars (N) in Error_Name_Or_No_Name
1053 -- If the context expects a value, and the name is a procedure, this is
1054 -- most likely a missing 'Access. Don't try to resolve the parameterless
1055 -- call, error will be caught when the outer call is analyzed.
1057 if Is_Entity_Name (N)
1058 and then Ekind (Entity (N)) = E_Procedure
1059 and then not Is_Overloaded (N)
1061 Nkind_In (Parent (N), N_Parameter_Association,
1063 N_Procedure_Call_Statement)
1068 -- Rewrite as call if overloadable entity that is (or could be, in the
1069 -- overloaded case) a function call. If we know for sure that the entity
1070 -- is an enumeration literal, we do not rewrite it.
1072 -- If the entity is the name of an operator, it cannot be a call because
1073 -- operators cannot have default parameters. In this case, this must be
1074 -- a string whose contents coincide with an operator name. Set the kind
1075 -- of the node appropriately.
1077 if (Is_Entity_Name (N)
1078 and then Nkind (N) /= N_Operator_Symbol
1079 and then Is_Overloadable (Entity (N))
1080 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1081 or else Is_Overloaded (N)))
1083 -- Rewrite as call if it is an explicit dereference of an expression of
1084 -- a subprogram access type, and the subprogram type is not that of a
1085 -- procedure or entry.
1088 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1090 -- Rewrite as call if it is a selected component which is a function,
1091 -- this is the case of a call to a protected function (which may be
1092 -- overloaded with other protected operations).
1095 (Nkind (N) = N_Selected_Component
1096 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1098 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1100 and then Is_Overloaded (Selector_Name (N)))))
1102 -- If one of the above three conditions is met, rewrite as call. Apply
1103 -- the rewriting only once.
1106 if Nkind (Parent (N)) /= N_Function_Call
1107 or else N /= Name (Parent (N))
1110 -- This may be a prefixed call that was not fully analyzed, e.g.
1111 -- an actual in an instance.
1113 if Ada_Version >= Ada_2005
1114 and then Nkind (N) = N_Selected_Component
1115 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1117 Analyze_Selected_Component (N);
1118 if Nkind (N) /= N_Selected_Component then
1123 Nam := New_Copy (N);
1125 -- If overloaded, overload set belongs to new copy
1127 Save_Interps (N, Nam);
1129 -- Change node to parameterless function call (note that the
1130 -- Parameter_Associations associations field is left set to Empty,
1131 -- its normal default value since there are no parameters)
1133 Change_Node (N, N_Function_Call);
1135 Set_Sloc (N, Sloc (Nam));
1139 elsif Nkind (N) = N_Parameter_Association then
1140 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1142 elsif Nkind (N) = N_Operator_Symbol then
1143 Change_Operator_Symbol_To_String_Literal (N);
1144 Set_Is_Overloaded (N, False);
1145 Set_Etype (N, Any_String);
1147 end Check_Parameterless_Call;
1149 -----------------------------
1150 -- Is_Definite_Access_Type --
1151 -----------------------------
1153 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1154 Btyp : constant Entity_Id := Base_Type (E);
1156 return Ekind (Btyp) = E_Access_Type
1157 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1158 and then Comes_From_Source (Btyp));
1159 end Is_Definite_Access_Type;
1161 ----------------------
1162 -- Is_Predefined_Op --
1163 ----------------------
1165 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1167 -- Predefined operators are intrinsic subprograms
1169 if not Is_Intrinsic_Subprogram (Nam) then
1173 -- A call to a back-end builtin is never a predefined operator
1175 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1179 return not Is_Generic_Instance (Nam)
1180 and then Chars (Nam) in Any_Operator_Name
1181 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1182 end Is_Predefined_Op;
1184 -----------------------------
1185 -- Make_Call_Into_Operator --
1186 -----------------------------
1188 procedure Make_Call_Into_Operator
1193 Op_Name : constant Name_Id := Chars (Op_Id);
1194 Act1 : Node_Id := First_Actual (N);
1195 Act2 : Node_Id := Next_Actual (Act1);
1196 Error : Boolean := False;
1197 Func : constant Entity_Id := Entity (Name (N));
1198 Is_Binary : constant Boolean := Present (Act2);
1200 Opnd_Type : Entity_Id;
1201 Orig_Type : Entity_Id := Empty;
1204 type Kind_Test is access function (E : Entity_Id) return Boolean;
1206 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1207 -- If the operand is not universal, and the operator is given by an
1208 -- expanded name, verify that the operand has an interpretation with a
1209 -- type defined in the given scope of the operator.
1211 function Type_In_P (Test : Kind_Test) return Entity_Id;
1212 -- Find a type of the given class in package Pack that contains the
1215 ---------------------------
1216 -- Operand_Type_In_Scope --
1217 ---------------------------
1219 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1220 Nod : constant Node_Id := Right_Opnd (Op_Node);
1225 if not Is_Overloaded (Nod) then
1226 return Scope (Base_Type (Etype (Nod))) = S;
1229 Get_First_Interp (Nod, I, It);
1230 while Present (It.Typ) loop
1231 if Scope (Base_Type (It.Typ)) = S then
1235 Get_Next_Interp (I, It);
1240 end Operand_Type_In_Scope;
1246 function Type_In_P (Test : Kind_Test) return Entity_Id is
1249 function In_Decl return Boolean;
1250 -- Verify that node is not part of the type declaration for the
1251 -- candidate type, which would otherwise be invisible.
1257 function In_Decl return Boolean is
1258 Decl_Node : constant Node_Id := Parent (E);
1264 if Etype (E) = Any_Type then
1267 elsif No (Decl_Node) then
1272 and then Nkind (N2) /= N_Compilation_Unit
1274 if N2 = Decl_Node then
1285 -- Start of processing for Type_In_P
1288 -- If the context type is declared in the prefix package, this is the
1289 -- desired base type.
1291 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1292 return Base_Type (Typ);
1295 E := First_Entity (Pack);
1296 while Present (E) loop
1298 and then not In_Decl
1310 -- Start of processing for Make_Call_Into_Operator
1313 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1318 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1319 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1320 Save_Interps (Act1, Left_Opnd (Op_Node));
1321 Save_Interps (Act2, Right_Opnd (Op_Node));
1322 Act1 := Left_Opnd (Op_Node);
1323 Act2 := Right_Opnd (Op_Node);
1328 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1329 Save_Interps (Act1, Right_Opnd (Op_Node));
1330 Act1 := Right_Opnd (Op_Node);
1333 -- If the operator is denoted by an expanded name, and the prefix is
1334 -- not Standard, but the operator is a predefined one whose scope is
1335 -- Standard, then this is an implicit_operator, inserted as an
1336 -- interpretation by the procedure of the same name. This procedure
1337 -- overestimates the presence of implicit operators, because it does
1338 -- not examine the type of the operands. Verify now that the operand
1339 -- type appears in the given scope. If right operand is universal,
1340 -- check the other operand. In the case of concatenation, either
1341 -- argument can be the component type, so check the type of the result.
1342 -- If both arguments are literals, look for a type of the right kind
1343 -- defined in the given scope. This elaborate nonsense is brought to
1344 -- you courtesy of b33302a. The type itself must be frozen, so we must
1345 -- find the type of the proper class in the given scope.
1347 -- A final wrinkle is the multiplication operator for fixed point types,
1348 -- which is defined in Standard only, and not in the scope of the
1349 -- fixed point type itself.
1351 if Nkind (Name (N)) = N_Expanded_Name then
1352 Pack := Entity (Prefix (Name (N)));
1354 -- If the entity being called is defined in the given package, it is
1355 -- a renaming of a predefined operator, and known to be legal.
1357 if Scope (Entity (Name (N))) = Pack
1358 and then Pack /= Standard_Standard
1362 -- Visibility does not need to be checked in an instance: if the
1363 -- operator was not visible in the generic it has been diagnosed
1364 -- already, else there is an implicit copy of it in the instance.
1366 elsif In_Instance then
1369 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1370 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1371 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1373 if Pack /= Standard_Standard then
1377 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1380 elsif Ada_Version >= Ada_2005
1381 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1382 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1387 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1389 if Op_Name = Name_Op_Concat then
1390 Opnd_Type := Base_Type (Typ);
1392 elsif (Scope (Opnd_Type) = Standard_Standard
1394 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1396 and then not Comes_From_Source (Opnd_Type))
1398 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1401 if Scope (Opnd_Type) = Standard_Standard then
1403 -- Verify that the scope contains a type that corresponds to
1404 -- the given literal. Optimize the case where Pack is Standard.
1406 if Pack /= Standard_Standard then
1408 if Opnd_Type = Universal_Integer then
1409 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1411 elsif Opnd_Type = Universal_Real then
1412 Orig_Type := Type_In_P (Is_Real_Type'Access);
1414 elsif Opnd_Type = Any_String then
1415 Orig_Type := Type_In_P (Is_String_Type'Access);
1417 elsif Opnd_Type = Any_Access then
1418 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1420 elsif Opnd_Type = Any_Composite then
1421 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1423 if Present (Orig_Type) then
1424 if Has_Private_Component (Orig_Type) then
1427 Set_Etype (Act1, Orig_Type);
1430 Set_Etype (Act2, Orig_Type);
1439 Error := No (Orig_Type);
1442 elsif Ekind (Opnd_Type) = E_Allocator_Type
1443 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1447 -- If the type is defined elsewhere, and the operator is not
1448 -- defined in the given scope (by a renaming declaration, e.g.)
1449 -- then this is an error as well. If an extension of System is
1450 -- present, and the type may be defined there, Pack must be
1453 elsif Scope (Opnd_Type) /= Pack
1454 and then Scope (Op_Id) /= Pack
1455 and then (No (System_Aux_Id)
1456 or else Scope (Opnd_Type) /= System_Aux_Id
1457 or else Pack /= Scope (System_Aux_Id))
1459 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1462 Error := not Operand_Type_In_Scope (Pack);
1465 elsif Pack = Standard_Standard
1466 and then not Operand_Type_In_Scope (Standard_Standard)
1473 Error_Msg_Node_2 := Pack;
1475 ("& not declared in&", N, Selector_Name (Name (N)));
1476 Set_Etype (N, Any_Type);
1479 -- Detect a mismatch between the context type and the result type
1480 -- in the named package, which is otherwise not detected if the
1481 -- operands are universal. Check is only needed if source entity is
1482 -- an operator, not a function that renames an operator.
1484 elsif Nkind (Parent (N)) /= N_Type_Conversion
1485 and then Ekind (Entity (Name (N))) = E_Operator
1486 and then Is_Numeric_Type (Typ)
1487 and then not Is_Universal_Numeric_Type (Typ)
1488 and then Scope (Base_Type (Typ)) /= Pack
1489 and then not In_Instance
1491 if Is_Fixed_Point_Type (Typ)
1492 and then (Op_Name = Name_Op_Multiply
1494 Op_Name = Name_Op_Divide)
1496 -- Already checked above
1500 -- Operator may be defined in an extension of System
1502 elsif Present (System_Aux_Id)
1503 and then Scope (Opnd_Type) = System_Aux_Id
1508 -- Could we use Wrong_Type here??? (this would require setting
1509 -- Etype (N) to the actual type found where Typ was expected).
1511 Error_Msg_NE ("expect }", N, Typ);
1516 Set_Chars (Op_Node, Op_Name);
1518 if not Is_Private_Type (Etype (N)) then
1519 Set_Etype (Op_Node, Base_Type (Etype (N)));
1521 Set_Etype (Op_Node, Etype (N));
1524 -- If this is a call to a function that renames a predefined equality,
1525 -- the renaming declaration provides a type that must be used to
1526 -- resolve the operands. This must be done now because resolution of
1527 -- the equality node will not resolve any remaining ambiguity, and it
1528 -- assumes that the first operand is not overloaded.
1530 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1531 and then Ekind (Func) = E_Function
1532 and then Is_Overloaded (Act1)
1534 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1535 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1538 Set_Entity (Op_Node, Op_Id);
1539 Generate_Reference (Op_Id, N, ' ');
1541 -- Do rewrite setting Comes_From_Source on the result if the original
1542 -- call came from source. Although it is not strictly the case that the
1543 -- operator as such comes from the source, logically it corresponds
1544 -- exactly to the function call in the source, so it should be marked
1545 -- this way (e.g. to make sure that validity checks work fine).
1548 CS : constant Boolean := Comes_From_Source (N);
1550 Rewrite (N, Op_Node);
1551 Set_Comes_From_Source (N, CS);
1554 -- If this is an arithmetic operator and the result type is private,
1555 -- the operands and the result must be wrapped in conversion to
1556 -- expose the underlying numeric type and expand the proper checks,
1557 -- e.g. on division.
1559 if Is_Private_Type (Typ) then
1561 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1562 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1563 Resolve_Intrinsic_Operator (N, Typ);
1565 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1566 Resolve_Intrinsic_Unary_Operator (N, Typ);
1574 end Make_Call_Into_Operator;
1580 function Operator_Kind
1582 Is_Binary : Boolean) return Node_Kind
1587 -- Use CASE statement or array???
1590 if Op_Name = Name_Op_And then
1592 elsif Op_Name = Name_Op_Or then
1594 elsif Op_Name = Name_Op_Xor then
1596 elsif Op_Name = Name_Op_Eq then
1598 elsif Op_Name = Name_Op_Ne then
1600 elsif Op_Name = Name_Op_Lt then
1602 elsif Op_Name = Name_Op_Le then
1604 elsif Op_Name = Name_Op_Gt then
1606 elsif Op_Name = Name_Op_Ge then
1608 elsif Op_Name = Name_Op_Add then
1610 elsif Op_Name = Name_Op_Subtract then
1611 Kind := N_Op_Subtract;
1612 elsif Op_Name = Name_Op_Concat then
1613 Kind := N_Op_Concat;
1614 elsif Op_Name = Name_Op_Multiply then
1615 Kind := N_Op_Multiply;
1616 elsif Op_Name = Name_Op_Divide then
1617 Kind := N_Op_Divide;
1618 elsif Op_Name = Name_Op_Mod then
1620 elsif Op_Name = Name_Op_Rem then
1622 elsif Op_Name = Name_Op_Expon then
1625 raise Program_Error;
1631 if Op_Name = Name_Op_Add then
1633 elsif Op_Name = Name_Op_Subtract then
1635 elsif Op_Name = Name_Op_Abs then
1637 elsif Op_Name = Name_Op_Not then
1640 raise Program_Error;
1647 ----------------------------
1648 -- Preanalyze_And_Resolve --
1649 ----------------------------
1651 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1652 Save_Full_Analysis : constant Boolean := Full_Analysis;
1655 Full_Analysis := False;
1656 Expander_Mode_Save_And_Set (False);
1658 -- We suppress all checks for this analysis, since the checks will
1659 -- be applied properly, and in the right location, when the default
1660 -- expression is reanalyzed and reexpanded later on.
1662 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1664 Expander_Mode_Restore;
1665 Full_Analysis := Save_Full_Analysis;
1666 end Preanalyze_And_Resolve;
1668 -- Version without context type
1670 procedure Preanalyze_And_Resolve (N : Node_Id) is
1671 Save_Full_Analysis : constant Boolean := Full_Analysis;
1674 Full_Analysis := False;
1675 Expander_Mode_Save_And_Set (False);
1678 Resolve (N, Etype (N), Suppress => All_Checks);
1680 Expander_Mode_Restore;
1681 Full_Analysis := Save_Full_Analysis;
1682 end Preanalyze_And_Resolve;
1684 ----------------------------------
1685 -- Replace_Actual_Discriminants --
1686 ----------------------------------
1688 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1689 Loc : constant Source_Ptr := Sloc (N);
1690 Tsk : Node_Id := Empty;
1692 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1693 -- Comment needed???
1699 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1703 if Nkind (Nod) = N_Identifier then
1704 Ent := Entity (Nod);
1707 and then Ekind (Ent) = E_Discriminant
1710 Make_Selected_Component (Loc,
1711 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1712 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1714 Set_Etype (Nod, Etype (Ent));
1722 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1724 -- Start of processing for Replace_Actual_Discriminants
1727 if not Expander_Active then
1731 if Nkind (Name (N)) = N_Selected_Component then
1732 Tsk := Prefix (Name (N));
1734 elsif Nkind (Name (N)) = N_Indexed_Component then
1735 Tsk := Prefix (Prefix (Name (N)));
1741 Replace_Discrs (Default);
1743 end Replace_Actual_Discriminants;
1749 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1750 Ambiguous : Boolean := False;
1751 Ctx_Type : Entity_Id := Typ;
1752 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1753 Err_Type : Entity_Id := Empty;
1754 Found : Boolean := False;
1757 I1 : Interp_Index := 0; -- prevent junk warning
1760 Seen : Entity_Id := Empty; -- prevent junk warning
1762 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1763 -- Determine whether a node comes from a predefined library unit or
1766 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1767 -- Try and fix up a literal so that it matches its expected type. New
1768 -- literals are manufactured if necessary to avoid cascaded errors.
1770 procedure Report_Ambiguous_Argument;
1771 -- Additional diagnostics when an ambiguous call has an ambiguous
1772 -- argument (typically a controlling actual).
1774 procedure Resolution_Failed;
1775 -- Called when attempt at resolving current expression fails
1777 ------------------------------------
1778 -- Comes_From_Predefined_Lib_Unit --
1779 -------------------------------------
1781 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1784 Sloc (Nod) = Standard_Location
1785 or else Is_Predefined_File_Name
1786 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1787 end Comes_From_Predefined_Lib_Unit;
1789 --------------------
1790 -- Patch_Up_Value --
1791 --------------------
1793 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1795 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1797 Make_Real_Literal (Sloc (N),
1798 Realval => UR_From_Uint (Intval (N))));
1799 Set_Etype (N, Universal_Real);
1800 Set_Is_Static_Expression (N);
1802 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1804 Make_Integer_Literal (Sloc (N),
1805 Intval => UR_To_Uint (Realval (N))));
1806 Set_Etype (N, Universal_Integer);
1807 Set_Is_Static_Expression (N);
1809 elsif Nkind (N) = N_String_Literal
1810 and then Is_Character_Type (Typ)
1812 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1814 Make_Character_Literal (Sloc (N),
1816 Char_Literal_Value =>
1817 UI_From_Int (Character'Pos ('A'))));
1818 Set_Etype (N, Any_Character);
1819 Set_Is_Static_Expression (N);
1821 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1823 Make_String_Literal (Sloc (N),
1824 Strval => End_String));
1826 elsif Nkind (N) = N_Range then
1827 Patch_Up_Value (Low_Bound (N), Typ);
1828 Patch_Up_Value (High_Bound (N), Typ);
1832 -------------------------------
1833 -- Report_Ambiguous_Argument --
1834 -------------------------------
1836 procedure Report_Ambiguous_Argument is
1837 Arg : constant Node_Id := First (Parameter_Associations (N));
1842 if Nkind (Arg) = N_Function_Call
1843 and then Is_Entity_Name (Name (Arg))
1844 and then Is_Overloaded (Name (Arg))
1846 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1848 -- Could use comments on what is going on here???
1850 Get_First_Interp (Name (Arg), I, It);
1851 while Present (It.Nam) loop
1852 Error_Msg_Sloc := Sloc (It.Nam);
1854 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1855 Error_Msg_N ("interpretation (inherited) #!", Arg);
1857 Error_Msg_N ("interpretation #!", Arg);
1860 Get_Next_Interp (I, It);
1863 end Report_Ambiguous_Argument;
1865 -----------------------
1866 -- Resolution_Failed --
1867 -----------------------
1869 procedure Resolution_Failed is
1871 Patch_Up_Value (N, Typ);
1873 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1874 Set_Is_Overloaded (N, False);
1876 -- The caller will return without calling the expander, so we need
1877 -- to set the analyzed flag. Note that it is fine to set Analyzed
1878 -- to True even if we are in the middle of a shallow analysis,
1879 -- (see the spec of sem for more details) since this is an error
1880 -- situation anyway, and there is no point in repeating the
1881 -- analysis later (indeed it won't work to repeat it later, since
1882 -- we haven't got a clear resolution of which entity is being
1885 Set_Analyzed (N, True);
1887 end Resolution_Failed;
1889 -- Start of processing for Resolve
1896 -- Access attribute on remote subprogram cannot be used for a non-remote
1897 -- access-to-subprogram type.
1899 if Nkind (N) = N_Attribute_Reference
1900 and then (Attribute_Name (N) = Name_Access or else
1901 Attribute_Name (N) = Name_Unrestricted_Access or else
1902 Attribute_Name (N) = Name_Unchecked_Access)
1903 and then Comes_From_Source (N)
1904 and then Is_Entity_Name (Prefix (N))
1905 and then Is_Subprogram (Entity (Prefix (N)))
1906 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1907 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1910 ("prefix must statically denote a non-remote subprogram", N);
1913 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1915 -- If the context is a Remote_Access_To_Subprogram, access attributes
1916 -- must be resolved with the corresponding fat pointer. There is no need
1917 -- to check for the attribute name since the return type of an
1918 -- attribute is never a remote type.
1920 if Nkind (N) = N_Attribute_Reference
1921 and then Comes_From_Source (N)
1922 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1925 Attr : constant Attribute_Id :=
1926 Get_Attribute_Id (Attribute_Name (N));
1927 Pref : constant Node_Id := Prefix (N);
1930 Is_Remote : Boolean := True;
1933 -- Check that Typ is a remote access-to-subprogram type
1935 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1937 -- Prefix (N) must statically denote a remote subprogram
1938 -- declared in a package specification.
1940 if Attr = Attribute_Access then
1941 Decl := Unit_Declaration_Node (Entity (Pref));
1943 if Nkind (Decl) = N_Subprogram_Body then
1944 Spec := Corresponding_Spec (Decl);
1946 if not No (Spec) then
1947 Decl := Unit_Declaration_Node (Spec);
1951 Spec := Parent (Decl);
1953 if not Is_Entity_Name (Prefix (N))
1954 or else Nkind (Spec) /= N_Package_Specification
1956 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1960 ("prefix must statically denote a remote subprogram ",
1965 -- If we are generating code for a distributed program.
1966 -- perform semantic checks against the corresponding
1969 if (Attr = Attribute_Access or else
1970 Attr = Attribute_Unchecked_Access or else
1971 Attr = Attribute_Unrestricted_Access)
1972 and then Expander_Active
1973 and then Get_PCS_Name /= Name_No_DSA
1975 Check_Subtype_Conformant
1976 (New_Id => Entity (Prefix (N)),
1977 Old_Id => Designated_Type
1978 (Corresponding_Remote_Type (Typ)),
1982 Process_Remote_AST_Attribute (N, Typ);
1989 Debug_A_Entry ("resolving ", N);
1991 if Comes_From_Source (N) then
1992 if Is_Fixed_Point_Type (Typ) then
1993 Check_Restriction (No_Fixed_Point, N);
1995 elsif Is_Floating_Point_Type (Typ)
1996 and then Typ /= Universal_Real
1997 and then Typ /= Any_Real
1999 Check_Restriction (No_Floating_Point, N);
2003 -- Return if already analyzed
2005 if Analyzed (N) then
2006 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2009 -- Return if type = Any_Type (previous error encountered)
2011 elsif Etype (N) = Any_Type then
2012 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2016 Check_Parameterless_Call (N);
2018 -- If not overloaded, then we know the type, and all that needs doing
2019 -- is to check that this type is compatible with the context.
2021 if not Is_Overloaded (N) then
2022 Found := Covers (Typ, Etype (N));
2023 Expr_Type := Etype (N);
2025 -- In the overloaded case, we must select the interpretation that
2026 -- is compatible with the context (i.e. the type passed to Resolve)
2029 -- Loop through possible interpretations
2031 Get_First_Interp (N, I, It);
2032 Interp_Loop : while Present (It.Typ) loop
2034 -- We are only interested in interpretations that are compatible
2035 -- with the expected type, any other interpretations are ignored.
2037 if not Covers (Typ, It.Typ) then
2038 if Debug_Flag_V then
2039 Write_Str (" interpretation incompatible with context");
2044 -- Skip the current interpretation if it is disabled by an
2045 -- abstract operator. This action is performed only when the
2046 -- type against which we are resolving is the same as the
2047 -- type of the interpretation.
2049 if Ada_Version >= Ada_2005
2050 and then It.Typ = Typ
2051 and then Typ /= Universal_Integer
2052 and then Typ /= Universal_Real
2053 and then Present (It.Abstract_Op)
2058 -- First matching interpretation
2064 Expr_Type := It.Typ;
2066 -- Matching interpretation that is not the first, maybe an
2067 -- error, but there are some cases where preference rules are
2068 -- used to choose between the two possibilities. These and
2069 -- some more obscure cases are handled in Disambiguate.
2072 -- If the current statement is part of a predefined library
2073 -- unit, then all interpretations which come from user level
2074 -- packages should not be considered.
2077 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2082 Error_Msg_Sloc := Sloc (Seen);
2083 It1 := Disambiguate (N, I1, I, Typ);
2085 -- Disambiguation has succeeded. Skip the remaining
2088 if It1 /= No_Interp then
2090 Expr_Type := It1.Typ;
2092 while Present (It.Typ) loop
2093 Get_Next_Interp (I, It);
2097 -- Before we issue an ambiguity complaint, check for
2098 -- the case of a subprogram call where at least one
2099 -- of the arguments is Any_Type, and if so, suppress
2100 -- the message, since it is a cascaded error.
2102 if Nkind_In (N, N_Function_Call,
2103 N_Procedure_Call_Statement)
2110 A := First_Actual (N);
2111 while Present (A) loop
2114 if Nkind (E) = N_Parameter_Association then
2115 E := Explicit_Actual_Parameter (E);
2118 if Etype (E) = Any_Type then
2119 if Debug_Flag_V then
2120 Write_Str ("Any_Type in call");
2131 elsif Nkind (N) in N_Binary_Op
2132 and then (Etype (Left_Opnd (N)) = Any_Type
2133 or else Etype (Right_Opnd (N)) = Any_Type)
2137 elsif Nkind (N) in N_Unary_Op
2138 and then Etype (Right_Opnd (N)) = Any_Type
2143 -- Not that special case, so issue message using the
2144 -- flag Ambiguous to control printing of the header
2145 -- message only at the start of an ambiguous set.
2147 if not Ambiguous then
2148 if Nkind (N) = N_Function_Call
2149 and then Nkind (Name (N)) = N_Explicit_Dereference
2152 ("ambiguous expression "
2153 & "(cannot resolve indirect call)!", N);
2155 Error_Msg_NE -- CODEFIX
2156 ("ambiguous expression (cannot resolve&)!",
2162 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2164 ("\\possible interpretation (inherited)#!", N);
2166 Error_Msg_N -- CODEFIX
2167 ("\\possible interpretation#!", N);
2171 (N, N_Procedure_Call_Statement, N_Function_Call)
2172 and then Present (Parameter_Associations (N))
2174 Report_Ambiguous_Argument;
2178 Error_Msg_Sloc := Sloc (It.Nam);
2180 -- By default, the error message refers to the candidate
2181 -- interpretation. But if it is a predefined operator, it
2182 -- is implicitly declared at the declaration of the type
2183 -- of the operand. Recover the sloc of that declaration
2184 -- for the error message.
2186 if Nkind (N) in N_Op
2187 and then Scope (It.Nam) = Standard_Standard
2188 and then not Is_Overloaded (Right_Opnd (N))
2189 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2192 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2194 if Comes_From_Source (Err_Type)
2195 and then Present (Parent (Err_Type))
2197 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2200 elsif Nkind (N) in N_Binary_Op
2201 and then Scope (It.Nam) = Standard_Standard
2202 and then not Is_Overloaded (Left_Opnd (N))
2203 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2206 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2208 if Comes_From_Source (Err_Type)
2209 and then Present (Parent (Err_Type))
2211 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2214 -- If this is an indirect call, use the subprogram_type
2215 -- in the message, to have a meaningful location. Also
2216 -- indicate if this is an inherited operation, created
2217 -- by a type declaration.
2219 elsif Nkind (N) = N_Function_Call
2220 and then Nkind (Name (N)) = N_Explicit_Dereference
2221 and then Is_Type (It.Nam)
2225 Sloc (Associated_Node_For_Itype (Err_Type));
2230 if Nkind (N) in N_Op
2231 and then Scope (It.Nam) = Standard_Standard
2232 and then Present (Err_Type)
2234 -- Special-case the message for universal_fixed
2235 -- operators, which are not declared with the type
2236 -- of the operand, but appear forever in Standard.
2238 if It.Typ = Universal_Fixed
2239 and then Scope (It.Nam) = Standard_Standard
2242 ("\\possible interpretation as " &
2243 "universal_fixed operation " &
2244 "(RM 4.5.5 (19))", N);
2247 ("\\possible interpretation (predefined)#!", N);
2251 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2254 ("\\possible interpretation (inherited)#!", N);
2256 Error_Msg_N -- CODEFIX
2257 ("\\possible interpretation#!", N);
2263 -- We have a matching interpretation, Expr_Type is the type
2264 -- from this interpretation, and Seen is the entity.
2266 -- For an operator, just set the entity name. The type will be
2267 -- set by the specific operator resolution routine.
2269 if Nkind (N) in N_Op then
2270 Set_Entity (N, Seen);
2271 Generate_Reference (Seen, N);
2273 elsif Nkind (N) = N_Case_Expression then
2274 Set_Etype (N, Expr_Type);
2276 elsif Nkind (N) = N_Character_Literal then
2277 Set_Etype (N, Expr_Type);
2279 elsif Nkind (N) = N_Conditional_Expression then
2280 Set_Etype (N, Expr_Type);
2282 -- AI05-0139-2: Expression is overloaded because type has
2283 -- implicit dereference. If type matches context, no implicit
2284 -- dereference is involved.
2286 elsif Has_Implicit_Dereference (Expr_Type) then
2287 Set_Etype (N, Expr_Type);
2288 Set_Is_Overloaded (N, False);
2291 elsif Is_Overloaded (N)
2292 and then Present (It.Nam)
2293 and then Ekind (It.Nam) = E_Discriminant
2294 and then Has_Implicit_Dereference (It.Nam)
2296 Build_Explicit_Dereference (N, It.Nam);
2298 -- For an explicit dereference, attribute reference, range,
2299 -- short-circuit form (which is not an operator node), or call
2300 -- with a name that is an explicit dereference, there is
2301 -- nothing to be done at this point.
2303 elsif Nkind_In (N, N_Explicit_Dereference,
2304 N_Attribute_Reference,
2306 N_Indexed_Component,
2309 N_Selected_Component,
2311 or else Nkind (Name (N)) = N_Explicit_Dereference
2315 -- For procedure or function calls, set the type of the name,
2316 -- and also the entity pointer for the prefix.
2318 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2319 and then Is_Entity_Name (Name (N))
2321 Set_Etype (Name (N), Expr_Type);
2322 Set_Entity (Name (N), Seen);
2323 Generate_Reference (Seen, Name (N));
2325 elsif Nkind (N) = N_Function_Call
2326 and then Nkind (Name (N)) = N_Selected_Component
2328 Set_Etype (Name (N), Expr_Type);
2329 Set_Entity (Selector_Name (Name (N)), Seen);
2330 Generate_Reference (Seen, Selector_Name (Name (N)));
2332 -- For all other cases, just set the type of the Name
2335 Set_Etype (Name (N), Expr_Type);
2342 -- Move to next interpretation
2344 exit Interp_Loop when No (It.Typ);
2346 Get_Next_Interp (I, It);
2347 end loop Interp_Loop;
2350 -- At this stage Found indicates whether or not an acceptable
2351 -- interpretation exists. If not, then we have an error, except that if
2352 -- the context is Any_Type as a result of some other error, then we
2353 -- suppress the error report.
2356 if Typ /= Any_Type then
2358 -- If type we are looking for is Void, then this is the procedure
2359 -- call case, and the error is simply that what we gave is not a
2360 -- procedure name (we think of procedure calls as expressions with
2361 -- types internally, but the user doesn't think of them this way!)
2363 if Typ = Standard_Void_Type then
2365 -- Special case message if function used as a procedure
2367 if Nkind (N) = N_Procedure_Call_Statement
2368 and then Is_Entity_Name (Name (N))
2369 and then Ekind (Entity (Name (N))) = E_Function
2372 ("cannot use function & in a procedure call",
2373 Name (N), Entity (Name (N)));
2375 -- Otherwise give general message (not clear what cases this
2376 -- covers, but no harm in providing for them!)
2379 Error_Msg_N ("expect procedure name in procedure call", N);
2384 -- Otherwise we do have a subexpression with the wrong type
2386 -- Check for the case of an allocator which uses an access type
2387 -- instead of the designated type. This is a common error and we
2388 -- specialize the message, posting an error on the operand of the
2389 -- allocator, complaining that we expected the designated type of
2392 elsif Nkind (N) = N_Allocator
2393 and then Ekind (Typ) in Access_Kind
2394 and then Ekind (Etype (N)) in Access_Kind
2395 and then Designated_Type (Etype (N)) = Typ
2397 Wrong_Type (Expression (N), Designated_Type (Typ));
2400 -- Check for view mismatch on Null in instances, for which the
2401 -- view-swapping mechanism has no identifier.
2403 elsif (In_Instance or else In_Inlined_Body)
2404 and then (Nkind (N) = N_Null)
2405 and then Is_Private_Type (Typ)
2406 and then Is_Access_Type (Full_View (Typ))
2408 Resolve (N, Full_View (Typ));
2412 -- Check for an aggregate. Sometimes we can get bogus aggregates
2413 -- from misuse of parentheses, and we are about to complain about
2414 -- the aggregate without even looking inside it.
2416 -- Instead, if we have an aggregate of type Any_Composite, then
2417 -- analyze and resolve the component fields, and then only issue
2418 -- another message if we get no errors doing this (otherwise
2419 -- assume that the errors in the aggregate caused the problem).
2421 elsif Nkind (N) = N_Aggregate
2422 and then Etype (N) = Any_Composite
2424 -- Disable expansion in any case. If there is a type mismatch
2425 -- it may be fatal to try to expand the aggregate. The flag
2426 -- would otherwise be set to false when the error is posted.
2428 Expander_Active := False;
2431 procedure Check_Aggr (Aggr : Node_Id);
2432 -- Check one aggregate, and set Found to True if we have a
2433 -- definite error in any of its elements
2435 procedure Check_Elmt (Aelmt : Node_Id);
2436 -- Check one element of aggregate and set Found to True if
2437 -- we definitely have an error in the element.
2443 procedure Check_Aggr (Aggr : Node_Id) is
2447 if Present (Expressions (Aggr)) then
2448 Elmt := First (Expressions (Aggr));
2449 while Present (Elmt) loop
2455 if Present (Component_Associations (Aggr)) then
2456 Elmt := First (Component_Associations (Aggr));
2457 while Present (Elmt) loop
2459 -- If this is a default-initialized component, then
2460 -- there is nothing to check. The box will be
2461 -- replaced by the appropriate call during late
2464 if not Box_Present (Elmt) then
2465 Check_Elmt (Expression (Elmt));
2477 procedure Check_Elmt (Aelmt : Node_Id) is
2479 -- If we have a nested aggregate, go inside it (to
2480 -- attempt a naked analyze-resolve of the aggregate can
2481 -- cause undesirable cascaded errors). Do not resolve
2482 -- expression if it needs a type from context, as for
2483 -- integer * fixed expression.
2485 if Nkind (Aelmt) = N_Aggregate then
2491 if not Is_Overloaded (Aelmt)
2492 and then Etype (Aelmt) /= Any_Fixed
2497 if Etype (Aelmt) = Any_Type then
2508 -- If an error message was issued already, Found got reset to
2509 -- True, so if it is still False, issue standard Wrong_Type msg.
2512 if Is_Overloaded (N)
2513 and then Nkind (N) = N_Function_Call
2516 Subp_Name : Node_Id;
2518 if Is_Entity_Name (Name (N)) then
2519 Subp_Name := Name (N);
2521 elsif Nkind (Name (N)) = N_Selected_Component then
2523 -- Protected operation: retrieve operation name
2525 Subp_Name := Selector_Name (Name (N));
2528 raise Program_Error;
2531 Error_Msg_Node_2 := Typ;
2532 Error_Msg_NE ("no visible interpretation of&" &
2533 " matches expected type&", N, Subp_Name);
2536 if All_Errors_Mode then
2538 Index : Interp_Index;
2542 Error_Msg_N ("\\possible interpretations:", N);
2544 Get_First_Interp (Name (N), Index, It);
2545 while Present (It.Nam) loop
2546 Error_Msg_Sloc := Sloc (It.Nam);
2547 Error_Msg_Node_2 := It.Nam;
2549 ("\\ type& for & declared#", N, It.Typ);
2550 Get_Next_Interp (Index, It);
2555 Error_Msg_N ("\use -gnatf for details", N);
2559 Wrong_Type (N, Typ);
2567 -- Test if we have more than one interpretation for the context
2569 elsif Ambiguous then
2573 -- Here we have an acceptable interpretation for the context
2576 -- Propagate type information and normalize tree for various
2577 -- predefined operations. If the context only imposes a class of
2578 -- types, rather than a specific type, propagate the actual type
2581 if Typ = Any_Integer or else
2582 Typ = Any_Boolean or else
2583 Typ = Any_Modular or else
2584 Typ = Any_Real or else
2587 Ctx_Type := Expr_Type;
2589 -- Any_Fixed is legal in a real context only if a specific fixed-
2590 -- point type is imposed. If Norman Cohen can be confused by this,
2591 -- it deserves a separate message.
2594 and then Expr_Type = Any_Fixed
2596 Error_Msg_N ("illegal context for mixed mode operation", N);
2597 Set_Etype (N, Universal_Real);
2598 Ctx_Type := Universal_Real;
2602 -- A user-defined operator is transformed into a function call at
2603 -- this point, so that further processing knows that operators are
2604 -- really operators (i.e. are predefined operators). User-defined
2605 -- operators that are intrinsic are just renamings of the predefined
2606 -- ones, and need not be turned into calls either, but if they rename
2607 -- a different operator, we must transform the node accordingly.
2608 -- Instantiations of Unchecked_Conversion are intrinsic but are
2609 -- treated as functions, even if given an operator designator.
2611 if Nkind (N) in N_Op
2612 and then Present (Entity (N))
2613 and then Ekind (Entity (N)) /= E_Operator
2616 if not Is_Predefined_Op (Entity (N)) then
2617 Rewrite_Operator_As_Call (N, Entity (N));
2619 elsif Present (Alias (Entity (N)))
2621 Nkind (Parent (Parent (Entity (N)))) =
2622 N_Subprogram_Renaming_Declaration
2624 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2626 -- If the node is rewritten, it will be fully resolved in
2627 -- Rewrite_Renamed_Operator.
2629 if Analyzed (N) then
2635 case N_Subexpr'(Nkind (N)) is
2637 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2639 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2641 when N_Short_Circuit
2642 => Resolve_Short_Circuit (N, Ctx_Type);
2644 when N_Attribute_Reference
2645 => Resolve_Attribute (N, Ctx_Type);
2647 when N_Case_Expression
2648 => Resolve_Case_Expression (N, Ctx_Type);
2650 when N_Character_Literal
2651 => Resolve_Character_Literal (N, Ctx_Type);
2653 when N_Conditional_Expression
2654 => Resolve_Conditional_Expression (N, Ctx_Type);
2656 when N_Expanded_Name
2657 => Resolve_Entity_Name (N, Ctx_Type);
2659 when N_Explicit_Dereference
2660 => Resolve_Explicit_Dereference (N, Ctx_Type);
2662 when N_Expression_With_Actions
2663 => Resolve_Expression_With_Actions (N, Ctx_Type);
2665 when N_Extension_Aggregate
2666 => Resolve_Extension_Aggregate (N, Ctx_Type);
2668 when N_Function_Call
2669 => Resolve_Call (N, Ctx_Type);
2672 => Resolve_Entity_Name (N, Ctx_Type);
2674 when N_Indexed_Component
2675 => Resolve_Indexed_Component (N, Ctx_Type);
2677 when N_Integer_Literal
2678 => Resolve_Integer_Literal (N, Ctx_Type);
2680 when N_Membership_Test
2681 => Resolve_Membership_Op (N, Ctx_Type);
2683 when N_Null => Resolve_Null (N, Ctx_Type);
2685 when N_Op_And | N_Op_Or | N_Op_Xor
2686 => Resolve_Logical_Op (N, Ctx_Type);
2688 when N_Op_Eq | N_Op_Ne
2689 => Resolve_Equality_Op (N, Ctx_Type);
2691 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2692 => Resolve_Comparison_Op (N, Ctx_Type);
2694 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2696 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2697 N_Op_Divide | N_Op_Mod | N_Op_Rem
2699 => Resolve_Arithmetic_Op (N, Ctx_Type);
2701 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2703 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2705 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2706 => Resolve_Unary_Op (N, Ctx_Type);
2708 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2710 when N_Procedure_Call_Statement
2711 => Resolve_Call (N, Ctx_Type);
2713 when N_Operator_Symbol
2714 => Resolve_Operator_Symbol (N, Ctx_Type);
2716 when N_Qualified_Expression
2717 => Resolve_Qualified_Expression (N, Ctx_Type);
2719 when N_Quantified_Expression
2720 => Resolve_Quantified_Expression (N, Ctx_Type);
2722 when N_Raise_xxx_Error
2723 => Set_Etype (N, Ctx_Type);
2725 when N_Range => Resolve_Range (N, Ctx_Type);
2728 => Resolve_Real_Literal (N, Ctx_Type);
2730 when N_Reference => Resolve_Reference (N, Ctx_Type);
2732 when N_Selected_Component
2733 => Resolve_Selected_Component (N, Ctx_Type);
2735 when N_Slice => Resolve_Slice (N, Ctx_Type);
2737 when N_String_Literal
2738 => Resolve_String_Literal (N, Ctx_Type);
2740 when N_Subprogram_Info
2741 => Resolve_Subprogram_Info (N, Ctx_Type);
2743 when N_Type_Conversion
2744 => Resolve_Type_Conversion (N, Ctx_Type);
2746 when N_Unchecked_Expression =>
2747 Resolve_Unchecked_Expression (N, Ctx_Type);
2749 when N_Unchecked_Type_Conversion =>
2750 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2753 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2754 -- expression of an anonymous access type that occurs in the context
2755 -- of a named general access type, except when the expression is that
2756 -- of a membership test. This ensures proper legality checking in
2757 -- terms of allowed conversions (expressions that would be illegal to
2758 -- convert implicitly are allowed in membership tests).
2760 if Ada_Version >= Ada_2012
2761 and then Ekind (Ctx_Type) = E_General_Access_Type
2762 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2763 and then Nkind (Parent (N)) not in N_Membership_Test
2765 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2766 Analyze_And_Resolve (N, Ctx_Type);
2769 -- If the subexpression was replaced by a non-subexpression, then
2770 -- all we do is to expand it. The only legitimate case we know of
2771 -- is converting procedure call statement to entry call statements,
2772 -- but there may be others, so we are making this test general.
2774 if Nkind (N) not in N_Subexpr then
2775 Debug_A_Exit ("resolving ", N, " (done)");
2780 -- AI05-144-2: Check dangerous order dependence within an expression
2781 -- that is not a subexpression. Exclude RHS of an assignment, because
2782 -- both sides may have side-effects and the check must be performed
2783 -- over the statement.
2785 if Nkind (Parent (N)) not in N_Subexpr
2786 and then Nkind (Parent (N)) /= N_Assignment_Statement
2787 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2789 Check_Order_Dependence;
2792 -- The expression is definitely NOT overloaded at this point, so
2793 -- we reset the Is_Overloaded flag to avoid any confusion when
2794 -- reanalyzing the node.
2796 Set_Is_Overloaded (N, False);
2798 -- Freeze expression type, entity if it is a name, and designated
2799 -- type if it is an allocator (RM 13.14(10,11,13)).
2801 -- Now that the resolution of the type of the node is complete, and
2802 -- we did not detect an error, we can expand this node. We skip the
2803 -- expand call if we are in a default expression, see section
2804 -- "Handling of Default Expressions" in Sem spec.
2806 Debug_A_Exit ("resolving ", N, " (done)");
2808 -- We unconditionally freeze the expression, even if we are in
2809 -- default expression mode (the Freeze_Expression routine tests this
2810 -- flag and only freezes static types if it is set).
2812 Freeze_Expression (N);
2814 -- Now we can do the expansion
2824 -- Version with check(s) suppressed
2826 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2828 if Suppress = All_Checks then
2830 Svg : constant Suppress_Array := Scope_Suppress;
2832 Scope_Suppress := (others => True);
2834 Scope_Suppress := Svg;
2839 Svg : constant Boolean := Scope_Suppress (Suppress);
2841 Scope_Suppress (Suppress) := True;
2843 Scope_Suppress (Suppress) := Svg;
2852 -- Version with implicit type
2854 procedure Resolve (N : Node_Id) is
2856 Resolve (N, Etype (N));
2859 ---------------------
2860 -- Resolve_Actuals --
2861 ---------------------
2863 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2864 Loc : constant Source_Ptr := Sloc (N);
2869 Prev : Node_Id := Empty;
2872 procedure Check_Argument_Order;
2873 -- Performs a check for the case where the actuals are all simple
2874 -- identifiers that correspond to the formal names, but in the wrong
2875 -- order, which is considered suspicious and cause for a warning.
2877 procedure Check_Prefixed_Call;
2878 -- If the original node is an overloaded call in prefix notation,
2879 -- insert an 'Access or a dereference as needed over the first actual.
2880 -- Try_Object_Operation has already verified that there is a valid
2881 -- interpretation, but the form of the actual can only be determined
2882 -- once the primitive operation is identified.
2884 procedure Insert_Default;
2885 -- If the actual is missing in a call, insert in the actuals list
2886 -- an instance of the default expression. The insertion is always
2887 -- a named association.
2889 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2890 -- Check whether T1 and T2, or their full views, are derived from a
2891 -- common type. Used to enforce the restrictions on array conversions
2894 function Static_Concatenation (N : Node_Id) return Boolean;
2895 -- Predicate to determine whether an actual that is a concatenation
2896 -- will be evaluated statically and does not need a transient scope.
2897 -- This must be determined before the actual is resolved and expanded
2898 -- because if needed the transient scope must be introduced earlier.
2900 --------------------------
2901 -- Check_Argument_Order --
2902 --------------------------
2904 procedure Check_Argument_Order is
2906 -- Nothing to do if no parameters, or original node is neither a
2907 -- function call nor a procedure call statement (happens in the
2908 -- operator-transformed-to-function call case), or the call does
2909 -- not come from source, or this warning is off.
2911 if not Warn_On_Parameter_Order
2912 or else No (Parameter_Associations (N))
2913 or else not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2915 or else not Comes_From_Source (N)
2921 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2924 -- Nothing to do if only one parameter
2930 -- Here if at least two arguments
2933 Actuals : array (1 .. Nargs) of Node_Id;
2937 Wrong_Order : Boolean := False;
2938 -- Set True if an out of order case is found
2941 -- Collect identifier names of actuals, fail if any actual is
2942 -- not a simple identifier, and record max length of name.
2944 Actual := First (Parameter_Associations (N));
2945 for J in Actuals'Range loop
2946 if Nkind (Actual) /= N_Identifier then
2949 Actuals (J) := Actual;
2954 -- If we got this far, all actuals are identifiers and the list
2955 -- of their names is stored in the Actuals array.
2957 Formal := First_Formal (Nam);
2958 for J in Actuals'Range loop
2960 -- If we ran out of formals, that's odd, probably an error
2961 -- which will be detected elsewhere, but abandon the search.
2967 -- If name matches and is in order OK
2969 if Chars (Formal) = Chars (Actuals (J)) then
2973 -- If no match, see if it is elsewhere in list and if so
2974 -- flag potential wrong order if type is compatible.
2976 for K in Actuals'Range loop
2977 if Chars (Formal) = Chars (Actuals (K))
2979 Has_Compatible_Type (Actuals (K), Etype (Formal))
2981 Wrong_Order := True;
2991 <<Continue>> Next_Formal (Formal);
2994 -- If Formals left over, also probably an error, skip warning
2996 if Present (Formal) then
3000 -- Here we give the warning if something was out of order
3004 ("actuals for this call may be in wrong order?", N);
3008 end Check_Argument_Order;
3010 -------------------------
3011 -- Check_Prefixed_Call --
3012 -------------------------
3014 procedure Check_Prefixed_Call is
3015 Act : constant Node_Id := First_Actual (N);
3016 A_Type : constant Entity_Id := Etype (Act);
3017 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3018 Orig : constant Node_Id := Original_Node (N);
3022 -- Check whether the call is a prefixed call, with or without
3023 -- additional actuals.
3025 if Nkind (Orig) = N_Selected_Component
3027 (Nkind (Orig) = N_Indexed_Component
3028 and then Nkind (Prefix (Orig)) = N_Selected_Component
3029 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3030 and then Is_Entity_Name (Act)
3031 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3033 if Is_Access_Type (A_Type)
3034 and then not Is_Access_Type (F_Type)
3036 -- Introduce dereference on object in prefix
3039 Make_Explicit_Dereference (Sloc (Act),
3040 Prefix => Relocate_Node (Act));
3041 Rewrite (Act, New_A);
3044 elsif Is_Access_Type (F_Type)
3045 and then not Is_Access_Type (A_Type)
3047 -- Introduce an implicit 'Access in prefix
3049 if not Is_Aliased_View (Act) then
3051 ("object in prefixed call to& must be aliased"
3052 & " (RM-2005 4.3.1 (13))",
3057 Make_Attribute_Reference (Loc,
3058 Attribute_Name => Name_Access,
3059 Prefix => Relocate_Node (Act)));
3064 end Check_Prefixed_Call;
3066 --------------------
3067 -- Insert_Default --
3068 --------------------
3070 procedure Insert_Default is
3075 -- Missing argument in call, nothing to insert
3077 if No (Default_Value (F)) then
3081 -- Note that we do a full New_Copy_Tree, so that any associated
3082 -- Itypes are properly copied. This may not be needed any more,
3083 -- but it does no harm as a safety measure! Defaults of a generic
3084 -- formal may be out of bounds of the corresponding actual (see
3085 -- cc1311b) and an additional check may be required.
3090 New_Scope => Current_Scope,
3093 if Is_Concurrent_Type (Scope (Nam))
3094 and then Has_Discriminants (Scope (Nam))
3096 Replace_Actual_Discriminants (N, Actval);
3099 if Is_Overloadable (Nam)
3100 and then Present (Alias (Nam))
3102 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3103 and then not Is_Tagged_Type (Etype (F))
3105 -- If default is a real literal, do not introduce a
3106 -- conversion whose effect may depend on the run-time
3107 -- size of universal real.
3109 if Nkind (Actval) = N_Real_Literal then
3110 Set_Etype (Actval, Base_Type (Etype (F)));
3112 Actval := Unchecked_Convert_To (Etype (F), Actval);
3116 if Is_Scalar_Type (Etype (F)) then
3117 Enable_Range_Check (Actval);
3120 Set_Parent (Actval, N);
3122 -- Resolve aggregates with their base type, to avoid scope
3123 -- anomalies: the subtype was first built in the subprogram
3124 -- declaration, and the current call may be nested.
3126 if Nkind (Actval) = N_Aggregate then
3127 Analyze_And_Resolve (Actval, Etype (F));
3129 Analyze_And_Resolve (Actval, Etype (Actval));
3133 Set_Parent (Actval, N);
3135 -- See note above concerning aggregates
3137 if Nkind (Actval) = N_Aggregate
3138 and then Has_Discriminants (Etype (Actval))
3140 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3142 -- Resolve entities with their own type, which may differ from
3143 -- the type of a reference in a generic context (the view
3144 -- swapping mechanism did not anticipate the re-analysis of
3145 -- default values in calls).
3147 elsif Is_Entity_Name (Actval) then
3148 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3151 Analyze_And_Resolve (Actval, Etype (Actval));
3155 -- If default is a tag indeterminate function call, propagate tag
3156 -- to obtain proper dispatching.
3158 if Is_Controlling_Formal (F)
3159 and then Nkind (Default_Value (F)) = N_Function_Call
3161 Set_Is_Controlling_Actual (Actval);
3166 -- If the default expression raises constraint error, then just
3167 -- silently replace it with an N_Raise_Constraint_Error node, since
3168 -- we already gave the warning on the subprogram spec. If node is
3169 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3170 -- the warnings removal machinery.
3172 if Raises_Constraint_Error (Actval)
3173 and then Nkind (Actval) /= N_Raise_Constraint_Error
3176 Make_Raise_Constraint_Error (Loc,
3177 Reason => CE_Range_Check_Failed));
3178 Set_Raises_Constraint_Error (Actval);
3179 Set_Etype (Actval, Etype (F));
3183 Make_Parameter_Association (Loc,
3184 Explicit_Actual_Parameter => Actval,
3185 Selector_Name => Make_Identifier (Loc, Chars (F)));
3187 -- Case of insertion is first named actual
3189 if No (Prev) or else
3190 Nkind (Parent (Prev)) /= N_Parameter_Association
3192 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3193 Set_First_Named_Actual (N, Actval);
3196 if No (Parameter_Associations (N)) then
3197 Set_Parameter_Associations (N, New_List (Assoc));
3199 Append (Assoc, Parameter_Associations (N));
3203 Insert_After (Prev, Assoc);
3206 -- Case of insertion is not first named actual
3209 Set_Next_Named_Actual
3210 (Assoc, Next_Named_Actual (Parent (Prev)));
3211 Set_Next_Named_Actual (Parent (Prev), Actval);
3212 Append (Assoc, Parameter_Associations (N));
3215 Mark_Rewrite_Insertion (Assoc);
3216 Mark_Rewrite_Insertion (Actval);
3225 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3226 FT1 : Entity_Id := T1;
3227 FT2 : Entity_Id := T2;
3230 if Is_Private_Type (T1)
3231 and then Present (Full_View (T1))
3233 FT1 := Full_View (T1);
3236 if Is_Private_Type (T2)
3237 and then Present (Full_View (T2))
3239 FT2 := Full_View (T2);
3242 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3245 --------------------------
3246 -- Static_Concatenation --
3247 --------------------------
3249 function Static_Concatenation (N : Node_Id) return Boolean is
3252 when N_String_Literal =>
3257 -- Concatenation is static when both operands are static and
3258 -- the concatenation operator is a predefined one.
3260 return Scope (Entity (N)) = Standard_Standard
3262 Static_Concatenation (Left_Opnd (N))
3264 Static_Concatenation (Right_Opnd (N));
3267 if Is_Entity_Name (N) then
3269 Ent : constant Entity_Id := Entity (N);
3271 return Ekind (Ent) = E_Constant
3272 and then Present (Constant_Value (Ent))
3274 Is_Static_Expression (Constant_Value (Ent));
3281 end Static_Concatenation;
3283 -- Start of processing for Resolve_Actuals
3286 Check_Argument_Order;
3288 if Present (First_Actual (N)) then
3289 Check_Prefixed_Call;
3292 A := First_Actual (N);
3293 F := First_Formal (Nam);
3294 while Present (F) loop
3295 if No (A) and then Needs_No_Actuals (Nam) then
3298 -- If we have an error in any actual or formal, indicated by a type
3299 -- of Any_Type, then abandon resolution attempt, and set result type
3302 elsif (Present (A) and then Etype (A) = Any_Type)
3303 or else Etype (F) = Any_Type
3305 Set_Etype (N, Any_Type);
3309 -- Case where actual is present
3311 -- If the actual is an entity, generate a reference to it now. We
3312 -- do this before the actual is resolved, because a formal of some
3313 -- protected subprogram, or a task discriminant, will be rewritten
3314 -- during expansion, and the source entity reference may be lost.
3317 and then Is_Entity_Name (A)
3318 and then Comes_From_Source (N)
3320 Orig_A := Entity (A);
3322 if Present (Orig_A) then
3323 if Is_Formal (Orig_A)
3324 and then Ekind (F) /= E_In_Parameter
3326 Generate_Reference (Orig_A, A, 'm');
3328 elsif not Is_Overloaded (A) then
3329 Generate_Reference (Orig_A, A);
3335 and then (Nkind (Parent (A)) /= N_Parameter_Association
3336 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3338 -- If style checking mode on, check match of formal name
3341 if Nkind (Parent (A)) = N_Parameter_Association then
3342 Check_Identifier (Selector_Name (Parent (A)), F);
3346 -- If the formal is Out or In_Out, do not resolve and expand the
3347 -- conversion, because it is subsequently expanded into explicit
3348 -- temporaries and assignments. However, the object of the
3349 -- conversion can be resolved. An exception is the case of tagged
3350 -- type conversion with a class-wide actual. In that case we want
3351 -- the tag check to occur and no temporary will be needed (no
3352 -- representation change can occur) and the parameter is passed by
3353 -- reference, so we go ahead and resolve the type conversion.
3354 -- Another exception is the case of reference to component or
3355 -- subcomponent of a bit-packed array, in which case we want to
3356 -- defer expansion to the point the in and out assignments are
3359 if Ekind (F) /= E_In_Parameter
3360 and then Nkind (A) = N_Type_Conversion
3361 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3363 if Ekind (F) = E_In_Out_Parameter
3364 and then Is_Array_Type (Etype (F))
3366 -- In a view conversion, the conversion must be legal in
3367 -- both directions, and thus both component types must be
3368 -- aliased, or neither (4.6 (8)).
3370 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3371 -- the privacy requirement should not apply to generic
3372 -- types, and should be checked in an instance. ARG query
3375 if Has_Aliased_Components (Etype (Expression (A))) /=
3376 Has_Aliased_Components (Etype (F))
3379 ("both component types in a view conversion must be"
3380 & " aliased, or neither", A);
3382 -- Comment here??? what set of cases???
3385 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3387 -- Check view conv between unrelated by ref array types
3389 if Is_By_Reference_Type (Etype (F))
3390 or else Is_By_Reference_Type (Etype (Expression (A)))
3393 ("view conversion between unrelated by reference " &
3394 "array types not allowed (\'A'I-00246)", A);
3396 -- In Ada 2005 mode, check view conversion component
3397 -- type cannot be private, tagged, or volatile. Note
3398 -- that we only apply this to source conversions. The
3399 -- generated code can contain conversions which are
3400 -- not subject to this test, and we cannot extract the
3401 -- component type in such cases since it is not present.
3403 elsif Comes_From_Source (A)
3404 and then Ada_Version >= Ada_2005
3407 Comp_Type : constant Entity_Id :=
3409 (Etype (Expression (A)));
3411 if (Is_Private_Type (Comp_Type)
3412 and then not Is_Generic_Type (Comp_Type))
3413 or else Is_Tagged_Type (Comp_Type)
3414 or else Is_Volatile (Comp_Type)
3417 ("component type of a view conversion cannot"
3418 & " be private, tagged, or volatile"
3427 -- Resolve expression if conversion is all OK
3429 if (Conversion_OK (A)
3430 or else Valid_Conversion (A, Etype (A), Expression (A)))
3431 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3433 Resolve (Expression (A));
3436 -- If the actual is a function call that returns a limited
3437 -- unconstrained object that needs finalization, create a
3438 -- transient scope for it, so that it can receive the proper
3439 -- finalization list.
3441 elsif Nkind (A) = N_Function_Call
3442 and then Is_Limited_Record (Etype (F))
3443 and then not Is_Constrained (Etype (F))
3444 and then Expander_Active
3445 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3447 Establish_Transient_Scope (A, False);
3449 -- A small optimization: if one of the actuals is a concatenation
3450 -- create a block around a procedure call to recover stack space.
3451 -- This alleviates stack usage when several procedure calls in
3452 -- the same statement list use concatenation. We do not perform
3453 -- this wrapping for code statements, where the argument is a
3454 -- static string, and we want to preserve warnings involving
3455 -- sequences of such statements.
3457 elsif Nkind (A) = N_Op_Concat
3458 and then Nkind (N) = N_Procedure_Call_Statement
3459 and then Expander_Active
3461 not (Is_Intrinsic_Subprogram (Nam)
3462 and then Chars (Nam) = Name_Asm)
3463 and then not Static_Concatenation (A)
3465 Establish_Transient_Scope (A, False);
3466 Resolve (A, Etype (F));
3469 if Nkind (A) = N_Type_Conversion
3470 and then Is_Array_Type (Etype (F))
3471 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3473 (Is_Limited_Type (Etype (F))
3474 or else Is_Limited_Type (Etype (Expression (A))))
3477 ("conversion between unrelated limited array types " &
3478 "not allowed (\A\I-00246)", A);
3480 if Is_Limited_Type (Etype (F)) then
3481 Explain_Limited_Type (Etype (F), A);
3484 if Is_Limited_Type (Etype (Expression (A))) then
3485 Explain_Limited_Type (Etype (Expression (A)), A);
3489 -- (Ada 2005: AI-251): If the actual is an allocator whose
3490 -- directly designated type is a class-wide interface, we build
3491 -- an anonymous access type to use it as the type of the
3492 -- allocator. Later, when the subprogram call is expanded, if
3493 -- the interface has a secondary dispatch table the expander
3494 -- will add a type conversion to force the correct displacement
3497 if Nkind (A) = N_Allocator then
3499 DDT : constant Entity_Id :=
3500 Directly_Designated_Type (Base_Type (Etype (F)));
3502 New_Itype : Entity_Id;
3505 if Is_Class_Wide_Type (DDT)
3506 and then Is_Interface (DDT)
3508 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3509 Set_Etype (New_Itype, Etype (A));
3510 Set_Directly_Designated_Type (New_Itype,
3511 Directly_Designated_Type (Etype (A)));
3512 Set_Etype (A, New_Itype);
3515 -- Ada 2005, AI-162:If the actual is an allocator, the
3516 -- innermost enclosing statement is the master of the
3517 -- created object. This needs to be done with expansion
3518 -- enabled only, otherwise the transient scope will not
3519 -- be removed in the expansion of the wrapped construct.
3521 if (Is_Controlled (DDT) or else Has_Task (DDT))
3522 and then Expander_Active
3524 Establish_Transient_Scope (A, False);
3529 -- (Ada 2005): The call may be to a primitive operation of
3530 -- a tagged synchronized type, declared outside of the type.
3531 -- In this case the controlling actual must be converted to
3532 -- its corresponding record type, which is the formal type.
3533 -- The actual may be a subtype, either because of a constraint
3534 -- or because it is a generic actual, so use base type to
3535 -- locate concurrent type.
3537 F_Typ := Base_Type (Etype (F));
3539 if Is_Tagged_Type (F_Typ)
3540 and then (Is_Concurrent_Type (F_Typ)
3541 or else Is_Concurrent_Record_Type (F_Typ))
3543 -- If the actual is overloaded, look for an interpretation
3544 -- that has a synchronized type.
3546 if not Is_Overloaded (A) then
3547 A_Typ := Base_Type (Etype (A));
3551 Index : Interp_Index;
3555 Get_First_Interp (A, Index, It);
3556 while Present (It.Typ) loop
3557 if Is_Concurrent_Type (It.Typ)
3558 or else Is_Concurrent_Record_Type (It.Typ)
3560 A_Typ := Base_Type (It.Typ);
3564 Get_Next_Interp (Index, It);
3570 Full_A_Typ : Entity_Id;
3573 if Present (Full_View (A_Typ)) then
3574 Full_A_Typ := Base_Type (Full_View (A_Typ));
3576 Full_A_Typ := A_Typ;
3579 -- Tagged synchronized type (case 1): the actual is a
3582 if Is_Concurrent_Type (A_Typ)
3583 and then Corresponding_Record_Type (A_Typ) = F_Typ
3586 Unchecked_Convert_To
3587 (Corresponding_Record_Type (A_Typ), A));
3588 Resolve (A, Etype (F));
3590 -- Tagged synchronized type (case 2): the formal is a
3593 elsif Ekind (Full_A_Typ) = E_Record_Type
3595 (Corresponding_Concurrent_Type (Full_A_Typ))
3596 and then Is_Concurrent_Type (F_Typ)
3597 and then Present (Corresponding_Record_Type (F_Typ))
3598 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3600 Resolve (A, Corresponding_Record_Type (F_Typ));
3605 Resolve (A, Etype (F));
3610 -- not a synchronized operation.
3612 Resolve (A, Etype (F));
3619 if Comes_From_Source (Original_Node (N))
3620 and then Nkind_In (Original_Node (N), N_Function_Call,
3621 N_Procedure_Call_Statement)
3623 -- In formal mode, check that actual parameters matching
3624 -- formals of tagged types are objects (or ancestor type
3625 -- conversions of objects), not general expressions.
3627 if Is_Actual_Tagged_Parameter (A) then
3628 if Is_SPARK_Object_Reference (A) then
3631 elsif Nkind (A) = N_Type_Conversion then
3633 Operand : constant Node_Id := Expression (A);
3634 Operand_Typ : constant Entity_Id := Etype (Operand);
3635 Target_Typ : constant Entity_Id := A_Typ;
3638 if not Is_SPARK_Object_Reference (Operand) then
3639 Check_SPARK_Restriction
3640 ("object required", Operand);
3642 -- In formal mode, the only view conversions are those
3643 -- involving ancestor conversion of an extended type.
3646 (Is_Tagged_Type (Target_Typ)
3647 and then not Is_Class_Wide_Type (Target_Typ)
3648 and then Is_Tagged_Type (Operand_Typ)
3649 and then not Is_Class_Wide_Type (Operand_Typ)
3650 and then Is_Ancestor (Target_Typ, Operand_Typ))
3653 (F, E_Out_Parameter, E_In_Out_Parameter)
3655 Check_SPARK_Restriction
3656 ("ancestor conversion is the only permitted "
3657 & "view conversion", A);
3659 Check_SPARK_Restriction
3660 ("ancestor conversion required", A);
3669 Check_SPARK_Restriction ("object required", A);
3672 -- In formal mode, the only view conversions are those
3673 -- involving ancestor conversion of an extended type.
3675 elsif Nkind (A) = N_Type_Conversion
3676 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3678 Check_SPARK_Restriction
3679 ("ancestor conversion is the only permitted view "
3684 -- Save actual for subsequent check on order dependence, and
3685 -- indicate whether actual is modifiable. For AI05-0144-2.
3687 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3689 -- For mode IN, if actual is an entity, and the type of the formal
3690 -- has warnings suppressed, then we reset Never_Set_In_Source for
3691 -- the calling entity. The reason for this is to catch cases like
3692 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3693 -- uses trickery to modify an IN parameter.
3695 if Ekind (F) = E_In_Parameter
3696 and then Is_Entity_Name (A)
3697 and then Present (Entity (A))
3698 and then Ekind (Entity (A)) = E_Variable
3699 and then Has_Warnings_Off (F_Typ)
3701 Set_Never_Set_In_Source (Entity (A), False);
3704 -- Perform error checks for IN and IN OUT parameters
3706 if Ekind (F) /= E_Out_Parameter then
3708 -- Check unset reference. For scalar parameters, it is clearly
3709 -- wrong to pass an uninitialized value as either an IN or
3710 -- IN-OUT parameter. For composites, it is also clearly an
3711 -- error to pass a completely uninitialized value as an IN
3712 -- parameter, but the case of IN OUT is trickier. We prefer
3713 -- not to give a warning here. For example, suppose there is
3714 -- a routine that sets some component of a record to False.
3715 -- It is perfectly reasonable to make this IN-OUT and allow
3716 -- either initialized or uninitialized records to be passed
3719 -- For partially initialized composite values, we also avoid
3720 -- warnings, since it is quite likely that we are passing a
3721 -- partially initialized value and only the initialized fields
3722 -- will in fact be read in the subprogram.
3724 if Is_Scalar_Type (A_Typ)
3725 or else (Ekind (F) = E_In_Parameter
3726 and then not Is_Partially_Initialized_Type (A_Typ))
3728 Check_Unset_Reference (A);
3731 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3732 -- actual to a nested call, since this is case of reading an
3733 -- out parameter, which is not allowed.
3735 if Ada_Version = Ada_83
3736 and then Is_Entity_Name (A)
3737 and then Ekind (Entity (A)) = E_Out_Parameter
3739 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3743 -- Case of OUT or IN OUT parameter
3745 if Ekind (F) /= E_In_Parameter then
3747 -- For an Out parameter, check for useless assignment. Note
3748 -- that we can't set Last_Assignment this early, because we may
3749 -- kill current values in Resolve_Call, and that call would
3750 -- clobber the Last_Assignment field.
3752 -- Note: call Warn_On_Useless_Assignment before doing the check
3753 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3754 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3755 -- reflects the last assignment, not this one!
3757 if Ekind (F) = E_Out_Parameter then
3758 if Warn_On_Modified_As_Out_Parameter (F)
3759 and then Is_Entity_Name (A)
3760 and then Present (Entity (A))
3761 and then Comes_From_Source (N)
3763 Warn_On_Useless_Assignment (Entity (A), A);
3767 -- Validate the form of the actual. Note that the call to
3768 -- Is_OK_Variable_For_Out_Formal generates the required
3769 -- reference in this case.
3771 -- A call to an initialization procedure for an aggregate
3772 -- component may initialize a nested component of a constant
3773 -- designated object. In this context the object is variable.
3775 if not Is_OK_Variable_For_Out_Formal (A)
3776 and then not Is_Init_Proc (Nam)
3778 Error_Msg_NE ("actual for& must be a variable", A, F);
3781 -- What's the following about???
3783 if Is_Entity_Name (A) then
3784 Kill_Checks (Entity (A));
3790 if Etype (A) = Any_Type then
3791 Set_Etype (N, Any_Type);
3795 -- Apply appropriate range checks for in, out, and in-out
3796 -- parameters. Out and in-out parameters also need a separate
3797 -- check, if there is a type conversion, to make sure the return
3798 -- value meets the constraints of the variable before the
3801 -- Gigi looks at the check flag and uses the appropriate types.
3802 -- For now since one flag is used there is an optimization which
3803 -- might not be done in the In Out case since Gigi does not do
3804 -- any analysis. More thought required about this ???
3806 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3808 -- Apply predicate checks, unless this is a call to the
3809 -- predicate check function itself, which would cause an
3810 -- infinite recursion.
3812 if not (Ekind (Nam) = E_Function
3813 and then Has_Predicates (Nam))
3815 Apply_Predicate_Check (A, F_Typ);
3818 -- Apply required constraint checks
3820 if Is_Scalar_Type (Etype (A)) then
3821 Apply_Scalar_Range_Check (A, F_Typ);
3823 elsif Is_Array_Type (Etype (A)) then
3824 Apply_Length_Check (A, F_Typ);
3826 elsif Is_Record_Type (F_Typ)
3827 and then Has_Discriminants (F_Typ)
3828 and then Is_Constrained (F_Typ)
3829 and then (not Is_Derived_Type (F_Typ)
3830 or else Comes_From_Source (Nam))
3832 Apply_Discriminant_Check (A, F_Typ);
3834 elsif Is_Access_Type (F_Typ)
3835 and then Is_Array_Type (Designated_Type (F_Typ))
3836 and then Is_Constrained (Designated_Type (F_Typ))
3838 Apply_Length_Check (A, F_Typ);
3840 elsif Is_Access_Type (F_Typ)
3841 and then Has_Discriminants (Designated_Type (F_Typ))
3842 and then Is_Constrained (Designated_Type (F_Typ))
3844 Apply_Discriminant_Check (A, F_Typ);
3847 Apply_Range_Check (A, F_Typ);
3850 -- Ada 2005 (AI-231): Note that the controlling parameter case
3851 -- already existed in Ada 95, which is partially checked
3852 -- elsewhere (see Checks), and we don't want the warning
3853 -- message to differ.
3855 if Is_Access_Type (F_Typ)
3856 and then Can_Never_Be_Null (F_Typ)
3857 and then Known_Null (A)
3859 if Is_Controlling_Formal (F) then
3860 Apply_Compile_Time_Constraint_Error
3862 Msg => "null value not allowed here?",
3863 Reason => CE_Access_Check_Failed);
3865 elsif Ada_Version >= Ada_2005 then
3866 Apply_Compile_Time_Constraint_Error
3868 Msg => "(Ada 2005) null not allowed in "
3869 & "null-excluding formal?",
3870 Reason => CE_Null_Not_Allowed);
3875 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3876 if Nkind (A) = N_Type_Conversion then
3877 if Is_Scalar_Type (A_Typ) then
3878 Apply_Scalar_Range_Check
3879 (Expression (A), Etype (Expression (A)), A_Typ);
3882 (Expression (A), Etype (Expression (A)), A_Typ);
3886 if Is_Scalar_Type (F_Typ) then
3887 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3888 elsif Is_Array_Type (F_Typ)
3889 and then Ekind (F) = E_Out_Parameter
3891 Apply_Length_Check (A, F_Typ);
3893 Apply_Range_Check (A, A_Typ, F_Typ);
3898 -- An actual associated with an access parameter is implicitly
3899 -- converted to the anonymous access type of the formal and must
3900 -- satisfy the legality checks for access conversions.
3902 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3903 if not Valid_Conversion (A, F_Typ, A) then
3905 ("invalid implicit conversion for access parameter", A);
3909 -- Check bad case of atomic/volatile argument (RM C.6(12))
3911 if Is_By_Reference_Type (Etype (F))
3912 and then Comes_From_Source (N)
3914 if Is_Atomic_Object (A)
3915 and then not Is_Atomic (Etype (F))
3918 ("cannot pass atomic argument to non-atomic formal",
3921 elsif Is_Volatile_Object (A)
3922 and then not Is_Volatile (Etype (F))
3925 ("cannot pass volatile argument to non-volatile formal",
3930 -- Check that subprograms don't have improper controlling
3931 -- arguments (RM 3.9.2 (9)).
3933 -- A primitive operation may have an access parameter of an
3934 -- incomplete tagged type, but a dispatching call is illegal
3935 -- if the type is still incomplete.
3937 if Is_Controlling_Formal (F) then
3938 Set_Is_Controlling_Actual (A);
3940 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3942 Desig : constant Entity_Id := Designated_Type (Etype (F));
3944 if Ekind (Desig) = E_Incomplete_Type
3945 and then No (Full_View (Desig))
3946 and then No (Non_Limited_View (Desig))
3949 ("premature use of incomplete type& " &
3950 "in dispatching call", A, Desig);
3955 elsif Nkind (A) = N_Explicit_Dereference then
3956 Validate_Remote_Access_To_Class_Wide_Type (A);
3959 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3960 and then not Is_Class_Wide_Type (F_Typ)
3961 and then not Is_Controlling_Formal (F)
3963 Error_Msg_N ("class-wide argument not allowed here!", A);
3965 if Is_Subprogram (Nam)
3966 and then Comes_From_Source (Nam)
3968 Error_Msg_Node_2 := F_Typ;
3970 ("& is not a dispatching operation of &!", A, Nam);
3973 -- Apply the checks described in 3.10.2(27): if the context is a
3974 -- specific access-to-object, the actual cannot be class-wide.
3975 -- Use base type to exclude access_to_subprogram cases.
3977 elsif Is_Access_Type (A_Typ)
3978 and then Is_Access_Type (F_Typ)
3979 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
3980 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3981 or else (Nkind (A) = N_Attribute_Reference
3983 Is_Class_Wide_Type (Etype (Prefix (A)))))
3984 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3985 and then not Is_Controlling_Formal (F)
3987 -- Disable these checks for call to imported C++ subprograms
3990 (Is_Entity_Name (Name (N))
3991 and then Is_Imported (Entity (Name (N)))
3992 and then Convention (Entity (Name (N))) = Convention_CPP)
3995 ("access to class-wide argument not allowed here!", A);
3997 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
3998 Error_Msg_Node_2 := Designated_Type (F_Typ);
4000 ("& is not a dispatching operation of &!", A, Nam);
4006 -- If it is a named association, treat the selector_name as a
4007 -- proper identifier, and mark the corresponding entity. Ignore
4008 -- this reference in ALFA mode, as it refers to an entity not in
4009 -- scope at the point of reference, so the reference should be
4010 -- ignored for computing effects of subprograms.
4012 if Nkind (Parent (A)) = N_Parameter_Association
4013 and then not ALFA_Mode
4015 Set_Entity (Selector_Name (Parent (A)), F);
4016 Generate_Reference (F, Selector_Name (Parent (A)));
4017 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4018 Generate_Reference (F_Typ, N, ' ');
4023 if Ekind (F) /= E_Out_Parameter then
4024 Check_Unset_Reference (A);
4029 -- Case where actual is not present
4037 end Resolve_Actuals;
4039 -----------------------
4040 -- Resolve_Allocator --
4041 -----------------------
4043 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4044 Desig_T : constant Entity_Id := Designated_Type (Typ);
4045 E : constant Node_Id := Expression (N);
4047 Discrim : Entity_Id;
4050 Assoc : Node_Id := Empty;
4053 procedure Check_Allocator_Discrim_Accessibility
4054 (Disc_Exp : Node_Id;
4055 Alloc_Typ : Entity_Id);
4056 -- Check that accessibility level associated with an access discriminant
4057 -- initialized in an allocator by the expression Disc_Exp is not deeper
4058 -- than the level of the allocator type Alloc_Typ. An error message is
4059 -- issued if this condition is violated. Specialized checks are done for
4060 -- the cases of a constraint expression which is an access attribute or
4061 -- an access discriminant.
4063 function In_Dispatching_Context return Boolean;
4064 -- If the allocator is an actual in a call, it is allowed to be class-
4065 -- wide when the context is not because it is a controlling actual.
4067 -------------------------------------------
4068 -- Check_Allocator_Discrim_Accessibility --
4069 -------------------------------------------
4071 procedure Check_Allocator_Discrim_Accessibility
4072 (Disc_Exp : Node_Id;
4073 Alloc_Typ : Entity_Id)
4076 if Type_Access_Level (Etype (Disc_Exp)) >
4077 Type_Access_Level (Alloc_Typ)
4080 ("operand type has deeper level than allocator type", Disc_Exp);
4082 -- When the expression is an Access attribute the level of the prefix
4083 -- object must not be deeper than that of the allocator's type.
4085 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4086 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
4088 and then Object_Access_Level (Prefix (Disc_Exp))
4089 > Type_Access_Level (Alloc_Typ)
4092 ("prefix of attribute has deeper level than allocator type",
4095 -- When the expression is an access discriminant the check is against
4096 -- the level of the prefix object.
4098 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4099 and then Nkind (Disc_Exp) = N_Selected_Component
4100 and then Object_Access_Level (Prefix (Disc_Exp))
4101 > Type_Access_Level (Alloc_Typ)
4104 ("access discriminant has deeper level than allocator type",
4107 -- All other cases are legal
4112 end Check_Allocator_Discrim_Accessibility;
4114 ----------------------------
4115 -- In_Dispatching_Context --
4116 ----------------------------
4118 function In_Dispatching_Context return Boolean is
4119 Par : constant Node_Id := Parent (N);
4123 Nkind_In (Par, N_Function_Call,
4124 N_Procedure_Call_Statement)
4125 and then Is_Entity_Name (Name (Par))
4126 and then Is_Dispatching_Operation (Entity (Name (Par)));
4127 end In_Dispatching_Context;
4129 -- Start of processing for Resolve_Allocator
4132 -- Replace general access with specific type
4134 if Ekind (Etype (N)) = E_Allocator_Type then
4135 Set_Etype (N, Base_Type (Typ));
4138 if Is_Abstract_Type (Typ) then
4139 Error_Msg_N ("type of allocator cannot be abstract", N);
4142 -- For qualified expression, resolve the expression using the
4143 -- given subtype (nothing to do for type mark, subtype indication)
4145 if Nkind (E) = N_Qualified_Expression then
4146 if Is_Class_Wide_Type (Etype (E))
4147 and then not Is_Class_Wide_Type (Desig_T)
4148 and then not In_Dispatching_Context
4151 ("class-wide allocator not allowed for this access type", N);
4154 Resolve (Expression (E), Etype (E));
4155 Check_Unset_Reference (Expression (E));
4157 -- A qualified expression requires an exact match of the type,
4158 -- class-wide matching is not allowed.
4160 if (Is_Class_Wide_Type (Etype (Expression (E)))
4161 or else Is_Class_Wide_Type (Etype (E)))
4162 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4164 Wrong_Type (Expression (E), Etype (E));
4167 -- A special accessibility check is needed for allocators that
4168 -- constrain access discriminants. The level of the type of the
4169 -- expression used to constrain an access discriminant cannot be
4170 -- deeper than the type of the allocator (in contrast to access
4171 -- parameters, where the level of the actual can be arbitrary).
4173 -- We can't use Valid_Conversion to perform this check because
4174 -- in general the type of the allocator is unrelated to the type
4175 -- of the access discriminant.
4177 if Ekind (Typ) /= E_Anonymous_Access_Type
4178 or else Is_Local_Anonymous_Access (Typ)
4180 Subtyp := Entity (Subtype_Mark (E));
4182 Aggr := Original_Node (Expression (E));
4184 if Has_Discriminants (Subtyp)
4185 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4187 Discrim := First_Discriminant (Base_Type (Subtyp));
4189 -- Get the first component expression of the aggregate
4191 if Present (Expressions (Aggr)) then
4192 Disc_Exp := First (Expressions (Aggr));
4194 elsif Present (Component_Associations (Aggr)) then
4195 Assoc := First (Component_Associations (Aggr));
4197 if Present (Assoc) then
4198 Disc_Exp := Expression (Assoc);
4207 while Present (Discrim) and then Present (Disc_Exp) loop
4208 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4209 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4212 Next_Discriminant (Discrim);
4214 if Present (Discrim) then
4215 if Present (Assoc) then
4217 Disc_Exp := Expression (Assoc);
4219 elsif Present (Next (Disc_Exp)) then
4223 Assoc := First (Component_Associations (Aggr));
4225 if Present (Assoc) then
4226 Disc_Exp := Expression (Assoc);
4236 -- For a subtype mark or subtype indication, freeze the subtype
4239 Freeze_Expression (E);
4241 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4243 ("initialization required for access-to-constant allocator", N);
4246 -- A special accessibility check is needed for allocators that
4247 -- constrain access discriminants. The level of the type of the
4248 -- expression used to constrain an access discriminant cannot be
4249 -- deeper than the type of the allocator (in contrast to access
4250 -- parameters, where the level of the actual can be arbitrary).
4251 -- We can't use Valid_Conversion to perform this check because
4252 -- in general the type of the allocator is unrelated to the type
4253 -- of the access discriminant.
4255 if Nkind (Original_Node (E)) = N_Subtype_Indication
4256 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4257 or else Is_Local_Anonymous_Access (Typ))
4259 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4261 if Has_Discriminants (Subtyp) then
4262 Discrim := First_Discriminant (Base_Type (Subtyp));
4263 Constr := First (Constraints (Constraint (Original_Node (E))));
4264 while Present (Discrim) and then Present (Constr) loop
4265 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4266 if Nkind (Constr) = N_Discriminant_Association then
4267 Disc_Exp := Original_Node (Expression (Constr));
4269 Disc_Exp := Original_Node (Constr);
4272 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4275 Next_Discriminant (Discrim);
4282 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4283 -- check that the level of the type of the created object is not deeper
4284 -- than the level of the allocator's access type, since extensions can
4285 -- now occur at deeper levels than their ancestor types. This is a
4286 -- static accessibility level check; a run-time check is also needed in
4287 -- the case of an initialized allocator with a class-wide argument (see
4288 -- Expand_Allocator_Expression).
4290 if Ada_Version >= Ada_2005
4291 and then Is_Class_Wide_Type (Desig_T)
4294 Exp_Typ : Entity_Id;
4297 if Nkind (E) = N_Qualified_Expression then
4298 Exp_Typ := Etype (E);
4299 elsif Nkind (E) = N_Subtype_Indication then
4300 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4302 Exp_Typ := Entity (E);
4305 if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
4306 if In_Instance_Body then
4307 Error_Msg_N ("?type in allocator has deeper level than" &
4308 " designated class-wide type", E);
4309 Error_Msg_N ("\?Program_Error will be raised at run time",
4312 Make_Raise_Program_Error (Sloc (N),
4313 Reason => PE_Accessibility_Check_Failed));
4316 -- Do not apply Ada 2005 accessibility checks on a class-wide
4317 -- allocator if the type given in the allocator is a formal
4318 -- type. A run-time check will be performed in the instance.
4320 elsif not Is_Generic_Type (Exp_Typ) then
4321 Error_Msg_N ("type in allocator has deeper level than" &
4322 " designated class-wide type", E);
4328 -- Check for allocation from an empty storage pool
4330 if No_Pool_Assigned (Typ) then
4331 Error_Msg_N ("allocation from empty storage pool!", N);
4333 -- If the context is an unchecked conversion, as may happen within an
4334 -- inlined subprogram, the allocator is being resolved with its own
4335 -- anonymous type. In that case, if the target type has a specific
4336 -- storage pool, it must be inherited explicitly by the allocator type.
4338 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4339 and then No (Associated_Storage_Pool (Typ))
4341 Set_Associated_Storage_Pool
4342 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4345 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4346 Check_Restriction (No_Anonymous_Allocators, N);
4349 -- Check that an allocator with task parts isn't for a nested access
4350 -- type when restriction No_Task_Hierarchy applies.
4352 if not Is_Library_Level_Entity (Base_Type (Typ))
4353 and then Has_Task (Base_Type (Desig_T))
4355 Check_Restriction (No_Task_Hierarchy, N);
4358 -- An erroneous allocator may be rewritten as a raise Program_Error
4361 if Nkind (N) = N_Allocator then
4363 -- An anonymous access discriminant is the definition of a
4366 if Ekind (Typ) = E_Anonymous_Access_Type
4367 and then Nkind (Associated_Node_For_Itype (Typ)) =
4368 N_Discriminant_Specification
4371 Discr : constant Entity_Id :=
4372 Defining_Identifier (Associated_Node_For_Itype (Typ));
4375 -- Ada 2012 AI05-0052: If the designated type of the allocator
4376 -- is limited, then the allocator shall not be used to define
4377 -- the value of an access discriminant unless the discriminated
4378 -- type is immutably limited.
4380 if Ada_Version >= Ada_2012
4381 and then Is_Limited_Type (Desig_T)
4382 and then not Is_Immutably_Limited_Type (Scope (Discr))
4385 ("only immutably limited types can have anonymous "
4386 & "access discriminants designating a limited type", N);
4390 -- Avoid marking an allocator as a dynamic coextension if it is
4391 -- within a static construct.
4393 if not Is_Static_Coextension (N) then
4394 Set_Is_Dynamic_Coextension (N);
4397 -- Cleanup for potential static coextensions
4400 Set_Is_Dynamic_Coextension (N, False);
4401 Set_Is_Static_Coextension (N, False);
4405 -- Report a simple error: if the designated object is a local task,
4406 -- its body has not been seen yet, and its activation will fail an
4407 -- elaboration check.
4409 if Is_Task_Type (Desig_T)
4410 and then Scope (Base_Type (Desig_T)) = Current_Scope
4411 and then Is_Compilation_Unit (Current_Scope)
4412 and then Ekind (Current_Scope) = E_Package
4413 and then not In_Package_Body (Current_Scope)
4415 Error_Msg_N ("cannot activate task before body seen?", N);
4416 Error_Msg_N ("\Program_Error will be raised at run time?", N);
4419 -- Ada 2012 (AI05-0111-3): Issue a warning whenever allocating a task
4420 -- or a type containing tasks on a subpool since the deallocation of
4421 -- the subpool may lead to undefined task behavior. Perform the check
4422 -- only when the allocator has not been converted into a Program_Error
4423 -- due to a previous error.
4425 if Ada_Version >= Ada_2012
4426 and then Nkind (N) = N_Allocator
4427 and then Present (Subpool_Handle_Name (N))
4428 and then Has_Task (Desig_T)
4430 Error_Msg_N ("?allocation of task on subpool may lead to " &
4431 "undefined behavior", N);
4433 end Resolve_Allocator;
4435 ---------------------------
4436 -- Resolve_Arithmetic_Op --
4437 ---------------------------
4439 -- Used for resolving all arithmetic operators except exponentiation
4441 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4442 L : constant Node_Id := Left_Opnd (N);
4443 R : constant Node_Id := Right_Opnd (N);
4444 TL : constant Entity_Id := Base_Type (Etype (L));
4445 TR : constant Entity_Id := Base_Type (Etype (R));
4449 B_Typ : constant Entity_Id := Base_Type (Typ);
4450 -- We do the resolution using the base type, because intermediate values
4451 -- in expressions always are of the base type, not a subtype of it.
4453 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4454 -- Returns True if N is in a context that expects "any real type"
4456 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4457 -- Return True iff given type is Integer or universal real/integer
4459 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4460 -- Choose type of integer literal in fixed-point operation to conform
4461 -- to available fixed-point type. T is the type of the other operand,
4462 -- which is needed to determine the expected type of N.
4464 procedure Set_Operand_Type (N : Node_Id);
4465 -- Set operand type to T if universal
4467 -------------------------------
4468 -- Expected_Type_Is_Any_Real --
4469 -------------------------------
4471 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4473 -- N is the expression after "delta" in a fixed_point_definition;
4476 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4477 N_Decimal_Fixed_Point_Definition,
4479 -- N is one of the bounds in a real_range_specification;
4482 N_Real_Range_Specification,
4484 -- N is the expression of a delta_constraint;
4487 N_Delta_Constraint);
4488 end Expected_Type_Is_Any_Real;
4490 -----------------------------
4491 -- Is_Integer_Or_Universal --
4492 -----------------------------
4494 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4496 Index : Interp_Index;
4500 if not Is_Overloaded (N) then
4502 return Base_Type (T) = Base_Type (Standard_Integer)
4503 or else T = Universal_Integer
4504 or else T = Universal_Real;
4506 Get_First_Interp (N, Index, It);
4507 while Present (It.Typ) loop
4508 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4509 or else It.Typ = Universal_Integer
4510 or else It.Typ = Universal_Real
4515 Get_Next_Interp (Index, It);
4520 end Is_Integer_Or_Universal;
4522 ----------------------------
4523 -- Set_Mixed_Mode_Operand --
4524 ----------------------------
4526 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4527 Index : Interp_Index;
4531 if Universal_Interpretation (N) = Universal_Integer then
4533 -- A universal integer literal is resolved as standard integer
4534 -- except in the case of a fixed-point result, where we leave it
4535 -- as universal (to be handled by Exp_Fixd later on)
4537 if Is_Fixed_Point_Type (T) then
4538 Resolve (N, Universal_Integer);
4540 Resolve (N, Standard_Integer);
4543 elsif Universal_Interpretation (N) = Universal_Real
4544 and then (T = Base_Type (Standard_Integer)
4545 or else T = Universal_Integer
4546 or else T = Universal_Real)
4548 -- A universal real can appear in a fixed-type context. We resolve
4549 -- the literal with that context, even though this might raise an
4550 -- exception prematurely (the other operand may be zero).
4554 elsif Etype (N) = Base_Type (Standard_Integer)
4555 and then T = Universal_Real
4556 and then Is_Overloaded (N)
4558 -- Integer arg in mixed-mode operation. Resolve with universal
4559 -- type, in case preference rule must be applied.
4561 Resolve (N, Universal_Integer);
4564 and then B_Typ /= Universal_Fixed
4566 -- Not a mixed-mode operation, resolve with context
4570 elsif Etype (N) = Any_Fixed then
4572 -- N may itself be a mixed-mode operation, so use context type
4576 elsif Is_Fixed_Point_Type (T)
4577 and then B_Typ = Universal_Fixed
4578 and then Is_Overloaded (N)
4580 -- Must be (fixed * fixed) operation, operand must have one
4581 -- compatible interpretation.
4583 Resolve (N, Any_Fixed);
4585 elsif Is_Fixed_Point_Type (B_Typ)
4586 and then (T = Universal_Real
4587 or else Is_Fixed_Point_Type (T))
4588 and then Is_Overloaded (N)
4590 -- C * F(X) in a fixed context, where C is a real literal or a
4591 -- fixed-point expression. F must have either a fixed type
4592 -- interpretation or an integer interpretation, but not both.
4594 Get_First_Interp (N, Index, It);
4595 while Present (It.Typ) loop
4596 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4597 if Analyzed (N) then
4598 Error_Msg_N ("ambiguous operand in fixed operation", N);
4600 Resolve (N, Standard_Integer);
4603 elsif Is_Fixed_Point_Type (It.Typ) then
4604 if Analyzed (N) then
4605 Error_Msg_N ("ambiguous operand in fixed operation", N);
4607 Resolve (N, It.Typ);
4611 Get_Next_Interp (Index, It);
4614 -- Reanalyze the literal with the fixed type of the context. If
4615 -- context is Universal_Fixed, we are within a conversion, leave
4616 -- the literal as a universal real because there is no usable
4617 -- fixed type, and the target of the conversion plays no role in
4631 if B_Typ = Universal_Fixed
4632 and then Nkind (Op2) = N_Real_Literal
4634 T2 := Universal_Real;
4639 Set_Analyzed (Op2, False);
4646 end Set_Mixed_Mode_Operand;
4648 ----------------------
4649 -- Set_Operand_Type --
4650 ----------------------
4652 procedure Set_Operand_Type (N : Node_Id) is
4654 if Etype (N) = Universal_Integer
4655 or else Etype (N) = Universal_Real
4659 end Set_Operand_Type;
4661 -- Start of processing for Resolve_Arithmetic_Op
4664 if Comes_From_Source (N)
4665 and then Ekind (Entity (N)) = E_Function
4666 and then Is_Imported (Entity (N))
4667 and then Is_Intrinsic_Subprogram (Entity (N))
4669 Resolve_Intrinsic_Operator (N, Typ);
4672 -- Special-case for mixed-mode universal expressions or fixed point type
4673 -- operation: each argument is resolved separately. The same treatment
4674 -- is required if one of the operands of a fixed point operation is
4675 -- universal real, since in this case we don't do a conversion to a
4676 -- specific fixed-point type (instead the expander handles the case).
4678 -- Set the type of the node to its universal interpretation because
4679 -- legality checks on an exponentiation operand need the context.
4681 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4682 and then Present (Universal_Interpretation (L))
4683 and then Present (Universal_Interpretation (R))
4685 Set_Etype (N, B_Typ);
4686 Resolve (L, Universal_Interpretation (L));
4687 Resolve (R, Universal_Interpretation (R));
4689 elsif (B_Typ = Universal_Real
4690 or else Etype (N) = Universal_Fixed
4691 or else (Etype (N) = Any_Fixed
4692 and then Is_Fixed_Point_Type (B_Typ))
4693 or else (Is_Fixed_Point_Type (B_Typ)
4694 and then (Is_Integer_Or_Universal (L)
4696 Is_Integer_Or_Universal (R))))
4697 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4699 if TL = Universal_Integer or else TR = Universal_Integer then
4700 Check_For_Visible_Operator (N, B_Typ);
4703 -- If context is a fixed type and one operand is integer, the other
4704 -- is resolved with the type of the context.
4706 if Is_Fixed_Point_Type (B_Typ)
4707 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4708 or else TL = Universal_Integer)
4713 elsif Is_Fixed_Point_Type (B_Typ)
4714 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4715 or else TR = Universal_Integer)
4721 Set_Mixed_Mode_Operand (L, TR);
4722 Set_Mixed_Mode_Operand (R, TL);
4725 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4726 -- multiplying operators from being used when the expected type is
4727 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4728 -- some cases where the expected type is actually Any_Real;
4729 -- Expected_Type_Is_Any_Real takes care of that case.
4731 if Etype (N) = Universal_Fixed
4732 or else Etype (N) = Any_Fixed
4734 if B_Typ = Universal_Fixed
4735 and then not Expected_Type_Is_Any_Real (N)
4736 and then not Nkind_In (Parent (N), N_Type_Conversion,
4737 N_Unchecked_Type_Conversion)
4739 Error_Msg_N ("type cannot be determined from context!", N);
4740 Error_Msg_N ("\explicit conversion to result type required", N);
4742 Set_Etype (L, Any_Type);
4743 Set_Etype (R, Any_Type);
4746 if Ada_Version = Ada_83
4747 and then Etype (N) = Universal_Fixed
4749 Nkind_In (Parent (N), N_Type_Conversion,
4750 N_Unchecked_Type_Conversion)
4753 ("(Ada 83) fixed-point operation "
4754 & "needs explicit conversion", N);
4757 -- The expected type is "any real type" in contexts like
4759 -- type T is delta <universal_fixed-expression> ...
4761 -- in which case we need to set the type to Universal_Real
4762 -- so that static expression evaluation will work properly.
4764 if Expected_Type_Is_Any_Real (N) then
4765 Set_Etype (N, Universal_Real);
4767 Set_Etype (N, B_Typ);
4771 elsif Is_Fixed_Point_Type (B_Typ)
4772 and then (Is_Integer_Or_Universal (L)
4773 or else Nkind (L) = N_Real_Literal
4774 or else Nkind (R) = N_Real_Literal
4775 or else Is_Integer_Or_Universal (R))
4777 Set_Etype (N, B_Typ);
4779 elsif Etype (N) = Any_Fixed then
4781 -- If no previous errors, this is only possible if one operand is
4782 -- overloaded and the context is universal. Resolve as such.
4784 Set_Etype (N, B_Typ);
4788 if (TL = Universal_Integer or else TL = Universal_Real)
4790 (TR = Universal_Integer or else TR = Universal_Real)
4792 Check_For_Visible_Operator (N, B_Typ);
4795 -- If the context is Universal_Fixed and the operands are also
4796 -- universal fixed, this is an error, unless there is only one
4797 -- applicable fixed_point type (usually Duration).
4799 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4800 T := Unique_Fixed_Point_Type (N);
4802 if T = Any_Type then
4815 -- If one of the arguments was resolved to a non-universal type.
4816 -- label the result of the operation itself with the same type.
4817 -- Do the same for the universal argument, if any.
4819 T := Intersect_Types (L, R);
4820 Set_Etype (N, Base_Type (T));
4821 Set_Operand_Type (L);
4822 Set_Operand_Type (R);
4825 Generate_Operator_Reference (N, Typ);
4826 Eval_Arithmetic_Op (N);
4828 -- In SPARK, a multiplication or division with operands of fixed point
4829 -- types shall be qualified or explicitly converted to identify the
4832 if (Is_Fixed_Point_Type (Etype (L))
4833 or else Is_Fixed_Point_Type (Etype (R)))
4834 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4836 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4838 Check_SPARK_Restriction
4839 ("operation should be qualified or explicitly converted", N);
4842 -- Set overflow and division checking bit. Much cleverer code needed
4843 -- here eventually and perhaps the Resolve routines should be separated
4844 -- for the various arithmetic operations, since they will need
4845 -- different processing. ???
4847 if Nkind (N) in N_Op then
4848 if not Overflow_Checks_Suppressed (Etype (N)) then
4849 Enable_Overflow_Check (N);
4852 -- Give warning if explicit division by zero
4854 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4855 and then not Division_Checks_Suppressed (Etype (N))
4857 Rop := Right_Opnd (N);
4859 if Compile_Time_Known_Value (Rop)
4860 and then ((Is_Integer_Type (Etype (Rop))
4861 and then Expr_Value (Rop) = Uint_0)
4863 (Is_Real_Type (Etype (Rop))
4864 and then Expr_Value_R (Rop) = Ureal_0))
4866 -- Specialize the warning message according to the operation
4870 Apply_Compile_Time_Constraint_Error
4871 (N, "division by zero?", CE_Divide_By_Zero,
4872 Loc => Sloc (Right_Opnd (N)));
4875 Apply_Compile_Time_Constraint_Error
4876 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4877 Loc => Sloc (Right_Opnd (N)));
4880 Apply_Compile_Time_Constraint_Error
4881 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4882 Loc => Sloc (Right_Opnd (N)));
4884 -- Division by zero can only happen with division, rem,
4885 -- and mod operations.
4888 raise Program_Error;
4891 -- Otherwise just set the flag to check at run time
4894 Activate_Division_Check (N);
4898 -- If Restriction No_Implicit_Conditionals is active, then it is
4899 -- violated if either operand can be negative for mod, or for rem
4900 -- if both operands can be negative.
4902 if Restriction_Check_Required (No_Implicit_Conditionals)
4903 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4912 -- Set if corresponding operand might be negative
4916 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4917 LNeg := (not OK) or else Lo < 0;
4920 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4921 RNeg := (not OK) or else Lo < 0;
4923 -- Check if we will be generating conditionals. There are two
4924 -- cases where that can happen, first for REM, the only case
4925 -- is largest negative integer mod -1, where the division can
4926 -- overflow, but we still have to give the right result. The
4927 -- front end generates a test for this annoying case. Here we
4928 -- just test if both operands can be negative (that's what the
4929 -- expander does, so we match its logic here).
4931 -- The second case is mod where either operand can be negative.
4932 -- In this case, the back end has to generate additional tests.
4934 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4936 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4938 Check_Restriction (No_Implicit_Conditionals, N);
4944 Check_Unset_Reference (L);
4945 Check_Unset_Reference (R);
4946 end Resolve_Arithmetic_Op;
4952 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4953 Loc : constant Source_Ptr := Sloc (N);
4954 Subp : constant Node_Id := Name (N);
4962 function Same_Or_Aliased_Subprograms
4964 E : Entity_Id) return Boolean;
4965 -- Returns True if the subprogram entity S is the same as E or else
4966 -- S is an alias of E.
4968 ---------------------------------
4969 -- Same_Or_Aliased_Subprograms --
4970 ---------------------------------
4972 function Same_Or_Aliased_Subprograms
4974 E : Entity_Id) return Boolean
4976 Subp_Alias : constant Entity_Id := Alias (S);
4979 or else (Present (Subp_Alias) and then Subp_Alias = E);
4980 end Same_Or_Aliased_Subprograms;
4982 -- Start of processing for Resolve_Call
4985 -- The context imposes a unique interpretation with type Typ on a
4986 -- procedure or function call. Find the entity of the subprogram that
4987 -- yields the expected type, and propagate the corresponding formal
4988 -- constraints on the actuals. The caller has established that an
4989 -- interpretation exists, and emitted an error if not unique.
4991 -- First deal with the case of a call to an access-to-subprogram,
4992 -- dereference made explicit in Analyze_Call.
4994 if Ekind (Etype (Subp)) = E_Subprogram_Type then
4995 if not Is_Overloaded (Subp) then
4996 Nam := Etype (Subp);
4999 -- Find the interpretation whose type (a subprogram type) has a
5000 -- return type that is compatible with the context. Analysis of
5001 -- the node has established that one exists.
5005 Get_First_Interp (Subp, I, It);
5006 while Present (It.Typ) loop
5007 if Covers (Typ, Etype (It.Typ)) then
5012 Get_Next_Interp (I, It);
5016 raise Program_Error;
5020 -- If the prefix is not an entity, then resolve it
5022 if not Is_Entity_Name (Subp) then
5023 Resolve (Subp, Nam);
5026 -- For an indirect call, we always invalidate checks, since we do not
5027 -- know whether the subprogram is local or global. Yes we could do
5028 -- better here, e.g. by knowing that there are no local subprograms,
5029 -- but it does not seem worth the effort. Similarly, we kill all
5030 -- knowledge of current constant values.
5032 Kill_Current_Values;
5034 -- If this is a procedure call which is really an entry call, do
5035 -- the conversion of the procedure call to an entry call. Protected
5036 -- operations use the same circuitry because the name in the call
5037 -- can be an arbitrary expression with special resolution rules.
5039 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5040 or else (Is_Entity_Name (Subp)
5041 and then Ekind (Entity (Subp)) = E_Entry)
5043 Resolve_Entry_Call (N, Typ);
5044 Check_Elab_Call (N);
5046 -- Kill checks and constant values, as above for indirect case
5047 -- Who knows what happens when another task is activated?
5049 Kill_Current_Values;
5052 -- Normal subprogram call with name established in Resolve
5054 elsif not (Is_Type (Entity (Subp))) then
5055 Nam := Entity (Subp);
5056 Set_Entity_With_Style_Check (Subp, Nam);
5058 -- Otherwise we must have the case of an overloaded call
5061 pragma Assert (Is_Overloaded (Subp));
5063 -- Initialize Nam to prevent warning (we know it will be assigned
5064 -- in the loop below, but the compiler does not know that).
5068 Get_First_Interp (Subp, I, It);
5069 while Present (It.Typ) loop
5070 if Covers (Typ, It.Typ) then
5072 Set_Entity_With_Style_Check (Subp, Nam);
5076 Get_Next_Interp (I, It);
5080 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5081 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5082 and then Nkind (Subp) /= N_Explicit_Dereference
5083 and then Present (Parameter_Associations (N))
5085 -- The prefix is a parameterless function call that returns an access
5086 -- to subprogram. If parameters are present in the current call, add
5087 -- add an explicit dereference. We use the base type here because
5088 -- within an instance these may be subtypes.
5090 -- The dereference is added either in Analyze_Call or here. Should
5091 -- be consolidated ???
5093 Set_Is_Overloaded (Subp, False);
5094 Set_Etype (Subp, Etype (Nam));
5095 Insert_Explicit_Dereference (Subp);
5096 Nam := Designated_Type (Etype (Nam));
5097 Resolve (Subp, Nam);
5100 -- Check that a call to Current_Task does not occur in an entry body
5102 if Is_RTE (Nam, RE_Current_Task) then
5111 -- Exclude calls that occur within the default of a formal
5112 -- parameter of the entry, since those are evaluated outside
5115 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5117 if Nkind (P) = N_Entry_Body
5118 or else (Nkind (P) = N_Subprogram_Body
5119 and then Is_Entry_Barrier_Function (P))
5123 ("?& should not be used in entry body (RM C.7(17))",
5126 ("\Program_Error will be raised at run time?", N, Nam);
5128 Make_Raise_Program_Error (Loc,
5129 Reason => PE_Current_Task_In_Entry_Body));
5130 Set_Etype (N, Rtype);
5137 -- Check that a procedure call does not occur in the context of the
5138 -- entry call statement of a conditional or timed entry call. Note that
5139 -- the case of a call to a subprogram renaming of an entry will also be
5140 -- rejected. The test for N not being an N_Entry_Call_Statement is
5141 -- defensive, covering the possibility that the processing of entry
5142 -- calls might reach this point due to later modifications of the code
5145 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5146 and then Nkind (N) /= N_Entry_Call_Statement
5147 and then Entry_Call_Statement (Parent (N)) = N
5149 if Ada_Version < Ada_2005 then
5150 Error_Msg_N ("entry call required in select statement", N);
5152 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5153 -- for a procedure_or_entry_call, the procedure_name or
5154 -- procedure_prefix of the procedure_call_statement shall denote
5155 -- an entry renamed by a procedure, or (a view of) a primitive
5156 -- subprogram of a limited interface whose first parameter is
5157 -- a controlling parameter.
5159 elsif Nkind (N) = N_Procedure_Call_Statement
5160 and then not Is_Renamed_Entry (Nam)
5161 and then not Is_Controlling_Limited_Procedure (Nam)
5164 ("entry call or dispatching primitive of interface required", N);
5168 -- Check that this is not a call to a protected procedure or entry from
5169 -- within a protected function.
5171 if Ekind (Current_Scope) = E_Function
5172 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5173 and then Ekind (Nam) /= E_Function
5174 and then Scope (Nam) = Scope (Current_Scope)
5176 Error_Msg_N ("within protected function, protected " &
5177 "object is constant", N);
5178 Error_Msg_N ("\cannot call operation that may modify it", N);
5181 -- Freeze the subprogram name if not in a spec-expression. Note that we
5182 -- freeze procedure calls as well as function calls. Procedure calls are
5183 -- not frozen according to the rules (RM 13.14(14)) because it is
5184 -- impossible to have a procedure call to a non-frozen procedure in pure
5185 -- Ada, but in the code that we generate in the expander, this rule
5186 -- needs extending because we can generate procedure calls that need
5189 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5190 Freeze_Expression (Subp);
5193 -- For a predefined operator, the type of the result is the type imposed
5194 -- by context, except for a predefined operation on universal fixed.
5195 -- Otherwise The type of the call is the type returned by the subprogram
5198 if Is_Predefined_Op (Nam) then
5199 if Etype (N) /= Universal_Fixed then
5203 -- If the subprogram returns an array type, and the context requires the
5204 -- component type of that array type, the node is really an indexing of
5205 -- the parameterless call. Resolve as such. A pathological case occurs
5206 -- when the type of the component is an access to the array type. In
5207 -- this case the call is truly ambiguous.
5209 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5211 ((Is_Array_Type (Etype (Nam))
5212 and then Covers (Typ, Component_Type (Etype (Nam))))
5213 or else (Is_Access_Type (Etype (Nam))
5214 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5218 Component_Type (Designated_Type (Etype (Nam))))))
5221 Index_Node : Node_Id;
5223 Ret_Type : constant Entity_Id := Etype (Nam);
5226 if Is_Access_Type (Ret_Type)
5227 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5230 ("cannot disambiguate function call and indexing", N);
5232 New_Subp := Relocate_Node (Subp);
5233 Set_Entity (Subp, Nam);
5235 if (Is_Array_Type (Ret_Type)
5236 and then Component_Type (Ret_Type) /= Any_Type)
5238 (Is_Access_Type (Ret_Type)
5240 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5242 if Needs_No_Actuals (Nam) then
5244 -- Indexed call to a parameterless function
5247 Make_Indexed_Component (Loc,
5249 Make_Function_Call (Loc,
5251 Expressions => Parameter_Associations (N));
5253 -- An Ada 2005 prefixed call to a primitive operation
5254 -- whose first parameter is the prefix. This prefix was
5255 -- prepended to the parameter list, which is actually a
5256 -- list of indexes. Remove the prefix in order to build
5257 -- the proper indexed component.
5260 Make_Indexed_Component (Loc,
5262 Make_Function_Call (Loc,
5264 Parameter_Associations =>
5266 (Remove_Head (Parameter_Associations (N)))),
5267 Expressions => Parameter_Associations (N));
5270 -- Preserve the parenthesis count of the node
5272 Set_Paren_Count (Index_Node, Paren_Count (N));
5274 -- Since we are correcting a node classification error made
5275 -- by the parser, we call Replace rather than Rewrite.
5277 Replace (N, Index_Node);
5279 Set_Etype (Prefix (N), Ret_Type);
5281 Resolve_Indexed_Component (N, Typ);
5282 Check_Elab_Call (Prefix (N));
5290 Set_Etype (N, Etype (Nam));
5293 -- In the case where the call is to an overloaded subprogram, Analyze
5294 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5295 -- such a case Normalize_Actuals needs to be called once more to order
5296 -- the actuals correctly. Otherwise the call will have the ordering
5297 -- given by the last overloaded subprogram whether this is the correct
5298 -- one being called or not.
5300 if Is_Overloaded (Subp) then
5301 Normalize_Actuals (N, Nam, False, Norm_OK);
5302 pragma Assert (Norm_OK);
5305 -- In any case, call is fully resolved now. Reset Overload flag, to
5306 -- prevent subsequent overload resolution if node is analyzed again
5308 Set_Is_Overloaded (Subp, False);
5309 Set_Is_Overloaded (N, False);
5311 -- If we are calling the current subprogram from immediately within its
5312 -- body, then that is the case where we can sometimes detect cases of
5313 -- infinite recursion statically. Do not try this in case restriction
5314 -- No_Recursion is in effect anyway, and do it only for source calls.
5316 if Comes_From_Source (N) then
5317 Scop := Current_Scope;
5319 -- Issue warning for possible infinite recursion in the absence
5320 -- of the No_Recursion restriction.
5322 if Same_Or_Aliased_Subprograms (Nam, Scop)
5323 and then not Restriction_Active (No_Recursion)
5324 and then Check_Infinite_Recursion (N)
5326 -- Here we detected and flagged an infinite recursion, so we do
5327 -- not need to test the case below for further warnings. Also we
5328 -- are all done if we now have a raise SE node.
5330 if Nkind (N) = N_Raise_Storage_Error then
5334 -- If call is to immediately containing subprogram, then check for
5335 -- the case of a possible run-time detectable infinite recursion.
5338 Scope_Loop : while Scop /= Standard_Standard loop
5339 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5341 -- Although in general case, recursion is not statically
5342 -- checkable, the case of calling an immediately containing
5343 -- subprogram is easy to catch.
5345 Check_Restriction (No_Recursion, N);
5347 -- If the recursive call is to a parameterless subprogram,
5348 -- then even if we can't statically detect infinite
5349 -- recursion, this is pretty suspicious, and we output a
5350 -- warning. Furthermore, we will try later to detect some
5351 -- cases here at run time by expanding checking code (see
5352 -- Detect_Infinite_Recursion in package Exp_Ch6).
5354 -- If the recursive call is within a handler, do not emit a
5355 -- warning, because this is a common idiom: loop until input
5356 -- is correct, catch illegal input in handler and restart.
5358 if No (First_Formal (Nam))
5359 and then Etype (Nam) = Standard_Void_Type
5360 and then not Error_Posted (N)
5361 and then Nkind (Parent (N)) /= N_Exception_Handler
5363 -- For the case of a procedure call. We give the message
5364 -- only if the call is the first statement in a sequence
5365 -- of statements, or if all previous statements are
5366 -- simple assignments. This is simply a heuristic to
5367 -- decrease false positives, without losing too many good
5368 -- warnings. The idea is that these previous statements
5369 -- may affect global variables the procedure depends on.
5370 -- We also exclude raise statements, that may arise from
5371 -- constraint checks and are probably unrelated to the
5372 -- intended control flow.
5374 if Nkind (N) = N_Procedure_Call_Statement
5375 and then Is_List_Member (N)
5381 while Present (P) loop
5383 N_Assignment_Statement,
5384 N_Raise_Constraint_Error)
5394 -- Do not give warning if we are in a conditional context
5397 K : constant Node_Kind := Nkind (Parent (N));
5399 if (K = N_Loop_Statement
5400 and then Present (Iteration_Scheme (Parent (N))))
5401 or else K = N_If_Statement
5402 or else K = N_Elsif_Part
5403 or else K = N_Case_Statement_Alternative
5409 -- Here warning is to be issued
5411 Set_Has_Recursive_Call (Nam);
5413 ("?possible infinite recursion!", N);
5415 ("\?Storage_Error may be raised at run time!", N);
5421 Scop := Scope (Scop);
5422 end loop Scope_Loop;
5426 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5428 Check_Obsolescent_2005_Entity (Nam, Subp);
5430 -- If subprogram name is a predefined operator, it was given in
5431 -- functional notation. Replace call node with operator node, so
5432 -- that actuals can be resolved appropriately.
5434 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5435 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5438 elsif Present (Alias (Nam))
5439 and then Is_Predefined_Op (Alias (Nam))
5441 Resolve_Actuals (N, Nam);
5442 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5446 -- Create a transient scope if the resulting type requires it
5448 -- There are several notable exceptions:
5450 -- a) In init procs, the transient scope overhead is not needed, and is
5451 -- even incorrect when the call is a nested initialization call for a
5452 -- component whose expansion may generate adjust calls. However, if the
5453 -- call is some other procedure call within an initialization procedure
5454 -- (for example a call to Create_Task in the init_proc of the task
5455 -- run-time record) a transient scope must be created around this call.
5457 -- b) Enumeration literal pseudo-calls need no transient scope
5459 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5460 -- functions) do not use the secondary stack even though the return
5461 -- type may be unconstrained.
5463 -- d) Calls to a build-in-place function, since such functions may
5464 -- allocate their result directly in a target object, and cases where
5465 -- the result does get allocated in the secondary stack are checked for
5466 -- within the specialized Exp_Ch6 procedures for expanding those
5467 -- build-in-place calls.
5469 -- e) If the subprogram is marked Inline_Always, then even if it returns
5470 -- an unconstrained type the call does not require use of the secondary
5471 -- stack. However, inlining will only take place if the body to inline
5472 -- is already present. It may not be available if e.g. the subprogram is
5473 -- declared in a child instance.
5475 -- If this is an initialization call for a type whose construction
5476 -- uses the secondary stack, and it is not a nested call to initialize
5477 -- a component, we do need to create a transient scope for it. We
5478 -- check for this by traversing the type in Check_Initialization_Call.
5481 and then Has_Pragma_Inline_Always (Nam)
5482 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5483 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5487 elsif Ekind (Nam) = E_Enumeration_Literal
5488 or else Is_Build_In_Place_Function (Nam)
5489 or else Is_Intrinsic_Subprogram (Nam)
5493 elsif Expander_Active
5494 and then Is_Type (Etype (Nam))
5495 and then Requires_Transient_Scope (Etype (Nam))
5497 (not Within_Init_Proc
5499 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5501 Establish_Transient_Scope (N, Sec_Stack => True);
5503 -- If the call appears within the bounds of a loop, it will
5504 -- be rewritten and reanalyzed, nothing left to do here.
5506 if Nkind (N) /= N_Function_Call then
5510 elsif Is_Init_Proc (Nam)
5511 and then not Within_Init_Proc
5513 Check_Initialization_Call (N, Nam);
5516 -- A protected function cannot be called within the definition of the
5517 -- enclosing protected type.
5519 if Is_Protected_Type (Scope (Nam))
5520 and then In_Open_Scopes (Scope (Nam))
5521 and then not Has_Completion (Scope (Nam))
5524 ("& cannot be called before end of protected definition", N, Nam);
5527 -- Propagate interpretation to actuals, and add default expressions
5530 if Present (First_Formal (Nam)) then
5531 Resolve_Actuals (N, Nam);
5533 -- Overloaded literals are rewritten as function calls, for purpose of
5534 -- resolution. After resolution, we can replace the call with the
5537 elsif Ekind (Nam) = E_Enumeration_Literal then
5538 Copy_Node (Subp, N);
5539 Resolve_Entity_Name (N, Typ);
5541 -- Avoid validation, since it is a static function call
5543 Generate_Reference (Nam, Subp);
5547 -- If the subprogram is not global, then kill all saved values and
5548 -- checks. This is a bit conservative, since in many cases we could do
5549 -- better, but it is not worth the effort. Similarly, we kill constant
5550 -- values. However we do not need to do this for internal entities
5551 -- (unless they are inherited user-defined subprograms), since they
5552 -- are not in the business of molesting local values.
5554 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5555 -- kill all checks and values for calls to global subprograms. This
5556 -- takes care of the case where an access to a local subprogram is
5557 -- taken, and could be passed directly or indirectly and then called
5558 -- from almost any context.
5560 -- Note: we do not do this step till after resolving the actuals. That
5561 -- way we still take advantage of the current value information while
5562 -- scanning the actuals.
5564 -- We suppress killing values if we are processing the nodes associated
5565 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5566 -- type kills all the values as part of analyzing the code that
5567 -- initializes the dispatch tables.
5569 if Inside_Freezing_Actions = 0
5570 and then (not Is_Library_Level_Entity (Nam)
5571 or else Suppress_Value_Tracking_On_Call
5572 (Nearest_Dynamic_Scope (Current_Scope)))
5573 and then (Comes_From_Source (Nam)
5574 or else (Present (Alias (Nam))
5575 and then Comes_From_Source (Alias (Nam))))
5577 Kill_Current_Values;
5580 -- If we are warning about unread OUT parameters, this is the place to
5581 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5582 -- after the above call to Kill_Current_Values (since that call clears
5583 -- the Last_Assignment field of all local variables).
5585 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5586 and then Comes_From_Source (N)
5587 and then In_Extended_Main_Source_Unit (N)
5594 F := First_Formal (Nam);
5595 A := First_Actual (N);
5596 while Present (F) and then Present (A) loop
5597 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5598 and then Warn_On_Modified_As_Out_Parameter (F)
5599 and then Is_Entity_Name (A)
5600 and then Present (Entity (A))
5601 and then Comes_From_Source (N)
5602 and then Safe_To_Capture_Value (N, Entity (A))
5604 Set_Last_Assignment (Entity (A), A);
5613 -- If the subprogram is a primitive operation, check whether or not
5614 -- it is a correct dispatching call.
5616 if Is_Overloadable (Nam)
5617 and then Is_Dispatching_Operation (Nam)
5619 Check_Dispatching_Call (N);
5621 elsif Ekind (Nam) /= E_Subprogram_Type
5622 and then Is_Abstract_Subprogram (Nam)
5623 and then not In_Instance
5625 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5628 -- If this is a dispatching call, generate the appropriate reference,
5629 -- for better source navigation in GPS.
5631 if Is_Overloadable (Nam)
5632 and then Present (Controlling_Argument (N))
5634 Generate_Reference (Nam, Subp, 'R');
5636 -- Normal case, not a dispatching call: generate a call reference
5639 Generate_Reference (Nam, Subp, 's');
5642 if Is_Intrinsic_Subprogram (Nam) then
5643 Check_Intrinsic_Call (N);
5646 -- Check for violation of restriction No_Specific_Termination_Handlers
5647 -- and warn on a potentially blocking call to Abort_Task.
5649 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5650 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5652 Is_RTE (Nam, RE_Specific_Handler))
5654 Check_Restriction (No_Specific_Termination_Handlers, N);
5656 elsif Is_RTE (Nam, RE_Abort_Task) then
5657 Check_Potentially_Blocking_Operation (N);
5660 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5661 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5662 -- need to check the second argument to determine whether it is an
5663 -- absolute or relative timing event.
5665 if Restriction_Check_Required (No_Relative_Delay)
5666 and then Is_RTE (Nam, RE_Set_Handler)
5667 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5669 Check_Restriction (No_Relative_Delay, N);
5672 -- Issue an error for a call to an eliminated subprogram. We skip this
5673 -- in a spec expression, e.g. a call in a default parameter value, since
5674 -- we are not really doing a call at this time. That's important because
5675 -- the spec expression may itself belong to an eliminated subprogram.
5677 if not In_Spec_Expression then
5678 Check_For_Eliminated_Subprogram (Subp, Nam);
5681 -- In formal mode, the primitive operations of a tagged type or type
5682 -- extension do not include functions that return the tagged type.
5684 -- Commented out as the call to Is_Inherited_Operation_For_Type may
5685 -- cause an error because the type entity of the parent node of
5686 -- Entity (Name (N) may not be set. ???
5687 -- So why not just add a guard ???
5689 -- if Nkind (N) = N_Function_Call
5690 -- and then Is_Tagged_Type (Etype (N))
5691 -- and then Is_Entity_Name (Name (N))
5692 -- and then Is_Inherited_Operation_For_Type
5693 -- (Entity (Name (N)), Etype (N))
5695 -- Check_SPARK_Restriction ("function not inherited", N);
5698 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5699 -- class-wide and the call dispatches on result in a context that does
5700 -- not provide a tag, the call raises Program_Error.
5702 if Nkind (N) = N_Function_Call
5703 and then In_Instance
5704 and then Is_Generic_Actual_Type (Typ)
5705 and then Is_Class_Wide_Type (Typ)
5706 and then Has_Controlling_Result (Nam)
5707 and then Nkind (Parent (N)) = N_Object_Declaration
5709 -- Verify that none of the formals are controlling
5712 Call_OK : Boolean := False;
5716 F := First_Formal (Nam);
5717 while Present (F) loop
5718 if Is_Controlling_Formal (F) then
5727 Error_Msg_N ("!? cannot determine tag of result", N);
5728 Error_Msg_N ("!? Program_Error will be raised", N);
5730 Make_Raise_Program_Error (Sloc (N),
5731 Reason => PE_Explicit_Raise));
5736 -- All done, evaluate call and deal with elaboration issues
5739 Check_Elab_Call (N);
5740 Warn_On_Overlapping_Actuals (Nam, N);
5743 -----------------------------
5744 -- Resolve_Case_Expression --
5745 -----------------------------
5747 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5751 Alt := First (Alternatives (N));
5752 while Present (Alt) loop
5753 Resolve (Expression (Alt), Typ);
5758 Eval_Case_Expression (N);
5759 end Resolve_Case_Expression;
5761 -------------------------------
5762 -- Resolve_Character_Literal --
5763 -------------------------------
5765 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5766 B_Typ : constant Entity_Id := Base_Type (Typ);
5770 -- Verify that the character does belong to the type of the context
5772 Set_Etype (N, B_Typ);
5773 Eval_Character_Literal (N);
5775 -- Wide_Wide_Character literals must always be defined, since the set
5776 -- of wide wide character literals is complete, i.e. if a character
5777 -- literal is accepted by the parser, then it is OK for wide wide
5778 -- character (out of range character literals are rejected).
5780 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5783 -- Always accept character literal for type Any_Character, which
5784 -- occurs in error situations and in comparisons of literals, both
5785 -- of which should accept all literals.
5787 elsif B_Typ = Any_Character then
5790 -- For Standard.Character or a type derived from it, check that the
5791 -- literal is in range.
5793 elsif Root_Type (B_Typ) = Standard_Character then
5794 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5798 -- For Standard.Wide_Character or a type derived from it, check that the
5799 -- literal is in range.
5801 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5802 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5806 -- For Standard.Wide_Wide_Character or a type derived from it, we
5807 -- know the literal is in range, since the parser checked!
5809 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5812 -- If the entity is already set, this has already been resolved in a
5813 -- generic context, or comes from expansion. Nothing else to do.
5815 elsif Present (Entity (N)) then
5818 -- Otherwise we have a user defined character type, and we can use the
5819 -- standard visibility mechanisms to locate the referenced entity.
5822 C := Current_Entity (N);
5823 while Present (C) loop
5824 if Etype (C) = B_Typ then
5825 Set_Entity_With_Style_Check (N, C);
5826 Generate_Reference (C, N);
5834 -- If we fall through, then the literal does not match any of the
5835 -- entries of the enumeration type. This isn't just a constraint error
5836 -- situation, it is an illegality (see RM 4.2).
5839 ("character not defined for }", N, First_Subtype (B_Typ));
5840 end Resolve_Character_Literal;
5842 ---------------------------
5843 -- Resolve_Comparison_Op --
5844 ---------------------------
5846 -- Context requires a boolean type, and plays no role in resolution.
5847 -- Processing identical to that for equality operators. The result type is
5848 -- the base type, which matters when pathological subtypes of booleans with
5849 -- limited ranges are used.
5851 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5852 L : constant Node_Id := Left_Opnd (N);
5853 R : constant Node_Id := Right_Opnd (N);
5857 -- If this is an intrinsic operation which is not predefined, use the
5858 -- types of its declared arguments to resolve the possibly overloaded
5859 -- operands. Otherwise the operands are unambiguous and specify the
5862 if Scope (Entity (N)) /= Standard_Standard then
5863 T := Etype (First_Entity (Entity (N)));
5866 T := Find_Unique_Type (L, R);
5868 if T = Any_Fixed then
5869 T := Unique_Fixed_Point_Type (L);
5873 Set_Etype (N, Base_Type (Typ));
5874 Generate_Reference (T, N, ' ');
5876 -- Skip remaining processing if already set to Any_Type
5878 if T = Any_Type then
5882 -- Deal with other error cases
5884 if T = Any_String or else
5885 T = Any_Composite or else
5888 if T = Any_Character then
5889 Ambiguous_Character (L);
5891 Error_Msg_N ("ambiguous operands for comparison", N);
5894 Set_Etype (N, Any_Type);
5898 -- Resolve the operands if types OK
5902 Check_Unset_Reference (L);
5903 Check_Unset_Reference (R);
5904 Generate_Operator_Reference (N, T);
5905 Check_Low_Bound_Tested (N);
5907 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
5908 -- types or array types except String.
5910 if Is_Boolean_Type (T) then
5911 Check_SPARK_Restriction
5912 ("comparison is not defined on Boolean type", N);
5914 elsif Is_Array_Type (T)
5915 and then Base_Type (T) /= Standard_String
5917 Check_SPARK_Restriction
5918 ("comparison is not defined on array types other than String", N);
5921 -- Check comparison on unordered enumeration
5923 if Comes_From_Source (N)
5924 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
5926 Error_Msg_N ("comparison on unordered enumeration type?", N);
5929 -- Evaluate the relation (note we do this after the above check since
5930 -- this Eval call may change N to True/False.
5932 Eval_Relational_Op (N);
5933 end Resolve_Comparison_Op;
5935 ------------------------------------
5936 -- Resolve_Conditional_Expression --
5937 ------------------------------------
5939 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5940 Condition : constant Node_Id := First (Expressions (N));
5941 Then_Expr : constant Node_Id := Next (Condition);
5942 Else_Expr : Node_Id := Next (Then_Expr);
5945 Resolve (Condition, Any_Boolean);
5946 Resolve (Then_Expr, Typ);
5948 -- If ELSE expression present, just resolve using the determined type
5950 if Present (Else_Expr) then
5951 Resolve (Else_Expr, Typ);
5953 -- If no ELSE expression is present, root type must be Standard.Boolean
5954 -- and we provide a Standard.True result converted to the appropriate
5955 -- Boolean type (in case it is a derived boolean type).
5957 elsif Root_Type (Typ) = Standard_Boolean then
5959 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
5960 Analyze_And_Resolve (Else_Expr, Typ);
5961 Append_To (Expressions (N), Else_Expr);
5964 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
5965 Append_To (Expressions (N), Error);
5969 Eval_Conditional_Expression (N);
5970 end Resolve_Conditional_Expression;
5972 -----------------------------------------
5973 -- Resolve_Discrete_Subtype_Indication --
5974 -----------------------------------------
5976 procedure Resolve_Discrete_Subtype_Indication
5984 Analyze (Subtype_Mark (N));
5985 S := Entity (Subtype_Mark (N));
5987 if Nkind (Constraint (N)) /= N_Range_Constraint then
5988 Error_Msg_N ("expect range constraint for discrete type", N);
5989 Set_Etype (N, Any_Type);
5992 R := Range_Expression (Constraint (N));
6000 if Base_Type (S) /= Base_Type (Typ) then
6002 ("expect subtype of }", N, First_Subtype (Typ));
6004 -- Rewrite the constraint as a range of Typ
6005 -- to allow compilation to proceed further.
6008 Rewrite (Low_Bound (R),
6009 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6010 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6011 Attribute_Name => Name_First));
6012 Rewrite (High_Bound (R),
6013 Make_Attribute_Reference (Sloc (High_Bound (R)),
6014 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6015 Attribute_Name => Name_First));
6019 Set_Etype (N, Etype (R));
6021 -- Additionally, we must check that the bounds are compatible
6022 -- with the given subtype, which might be different from the
6023 -- type of the context.
6025 Apply_Range_Check (R, S);
6027 -- ??? If the above check statically detects a Constraint_Error
6028 -- it replaces the offending bound(s) of the range R with a
6029 -- Constraint_Error node. When the itype which uses these bounds
6030 -- is frozen the resulting call to Duplicate_Subexpr generates
6031 -- a new temporary for the bounds.
6033 -- Unfortunately there are other itypes that are also made depend
6034 -- on these bounds, so when Duplicate_Subexpr is called they get
6035 -- a forward reference to the newly created temporaries and Gigi
6036 -- aborts on such forward references. This is probably sign of a
6037 -- more fundamental problem somewhere else in either the order of
6038 -- itype freezing or the way certain itypes are constructed.
6040 -- To get around this problem we call Remove_Side_Effects right
6041 -- away if either bounds of R are a Constraint_Error.
6044 L : constant Node_Id := Low_Bound (R);
6045 H : constant Node_Id := High_Bound (R);
6048 if Nkind (L) = N_Raise_Constraint_Error then
6049 Remove_Side_Effects (L);
6052 if Nkind (H) = N_Raise_Constraint_Error then
6053 Remove_Side_Effects (H);
6057 Check_Unset_Reference (Low_Bound (R));
6058 Check_Unset_Reference (High_Bound (R));
6061 end Resolve_Discrete_Subtype_Indication;
6063 -------------------------
6064 -- Resolve_Entity_Name --
6065 -------------------------
6067 -- Used to resolve identifiers and expanded names
6069 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6070 E : constant Entity_Id := Entity (N);
6073 -- If garbage from errors, set to Any_Type and return
6075 if No (E) and then Total_Errors_Detected /= 0 then
6076 Set_Etype (N, Any_Type);
6080 -- Replace named numbers by corresponding literals. Note that this is
6081 -- the one case where Resolve_Entity_Name must reset the Etype, since
6082 -- it is currently marked as universal.
6084 if Ekind (E) = E_Named_Integer then
6086 Eval_Named_Integer (N);
6088 elsif Ekind (E) = E_Named_Real then
6090 Eval_Named_Real (N);
6092 -- For enumeration literals, we need to make sure that a proper style
6093 -- check is done, since such literals are overloaded, and thus we did
6094 -- not do a style check during the first phase of analysis.
6096 elsif Ekind (E) = E_Enumeration_Literal then
6097 Set_Entity_With_Style_Check (N, E);
6098 Eval_Entity_Name (N);
6100 -- Case of subtype name appearing as an operand in expression
6102 elsif Is_Type (E) then
6104 -- Allow use of subtype if it is a concurrent type where we are
6105 -- currently inside the body. This will eventually be expanded into a
6106 -- call to Self (for tasks) or _object (for protected objects). Any
6107 -- other use of a subtype is invalid.
6109 if Is_Concurrent_Type (E)
6110 and then In_Open_Scopes (E)
6114 -- Any other use is an error
6118 ("invalid use of subtype mark in expression or call", N);
6121 -- Check discriminant use if entity is discriminant in current scope,
6122 -- i.e. discriminant of record or concurrent type currently being
6123 -- analyzed. Uses in corresponding body are unrestricted.
6125 elsif Ekind (E) = E_Discriminant
6126 and then Scope (E) = Current_Scope
6127 and then not Has_Completion (Current_Scope)
6129 Check_Discriminant_Use (N);
6131 -- A parameterless generic function cannot appear in a context that
6132 -- requires resolution.
6134 elsif Ekind (E) = E_Generic_Function then
6135 Error_Msg_N ("illegal use of generic function", N);
6137 elsif Ekind (E) = E_Out_Parameter
6138 and then Ada_Version = Ada_83
6139 and then (Nkind (Parent (N)) in N_Op
6140 or else (Nkind (Parent (N)) = N_Assignment_Statement
6141 and then N = Expression (Parent (N)))
6142 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6144 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6146 -- In all other cases, just do the possible static evaluation
6149 -- A deferred constant that appears in an expression must have a
6150 -- completion, unless it has been removed by in-place expansion of
6153 if Ekind (E) = E_Constant
6154 and then Comes_From_Source (E)
6155 and then No (Constant_Value (E))
6156 and then Is_Frozen (Etype (E))
6157 and then not In_Spec_Expression
6158 and then not Is_Imported (E)
6160 if No_Initialization (Parent (E))
6161 or else (Present (Full_View (E))
6162 and then No_Initialization (Parent (Full_View (E))))
6167 "deferred constant is frozen before completion", N);
6171 Eval_Entity_Name (N);
6173 end Resolve_Entity_Name;
6179 procedure Resolve_Entry (Entry_Name : Node_Id) is
6180 Loc : constant Source_Ptr := Sloc (Entry_Name);
6188 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6189 -- If the bounds of the entry family being called depend on task
6190 -- discriminants, build a new index subtype where a discriminant is
6191 -- replaced with the value of the discriminant of the target task.
6192 -- The target task is the prefix of the entry name in the call.
6194 -----------------------
6195 -- Actual_Index_Type --
6196 -----------------------
6198 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6199 Typ : constant Entity_Id := Entry_Index_Type (E);
6200 Tsk : constant Entity_Id := Scope (E);
6201 Lo : constant Node_Id := Type_Low_Bound (Typ);
6202 Hi : constant Node_Id := Type_High_Bound (Typ);
6205 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6206 -- If the bound is given by a discriminant, replace with a reference
6207 -- to the discriminant of the same name in the target task. If the
6208 -- entry name is the target of a requeue statement and the entry is
6209 -- in the current protected object, the bound to be used is the
6210 -- discriminal of the object (see Apply_Range_Checks for details of
6211 -- the transformation).
6213 -----------------------------
6214 -- Actual_Discriminant_Ref --
6215 -----------------------------
6217 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6218 Typ : constant Entity_Id := Etype (Bound);
6222 Remove_Side_Effects (Bound);
6224 if not Is_Entity_Name (Bound)
6225 or else Ekind (Entity (Bound)) /= E_Discriminant
6229 elsif Is_Protected_Type (Tsk)
6230 and then In_Open_Scopes (Tsk)
6231 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6233 -- Note: here Bound denotes a discriminant of the corresponding
6234 -- record type tskV, whose discriminal is a formal of the
6235 -- init-proc tskVIP. What we want is the body discriminal,
6236 -- which is associated to the discriminant of the original
6237 -- concurrent type tsk.
6239 return New_Occurrence_Of
6240 (Find_Body_Discriminal (Entity (Bound)), Loc);
6244 Make_Selected_Component (Loc,
6245 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6246 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6251 end Actual_Discriminant_Ref;
6253 -- Start of processing for Actual_Index_Type
6256 if not Has_Discriminants (Tsk)
6257 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6259 return Entry_Index_Type (E);
6262 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6263 Set_Etype (New_T, Base_Type (Typ));
6264 Set_Size_Info (New_T, Typ);
6265 Set_RM_Size (New_T, RM_Size (Typ));
6266 Set_Scalar_Range (New_T,
6267 Make_Range (Sloc (Entry_Name),
6268 Low_Bound => Actual_Discriminant_Ref (Lo),
6269 High_Bound => Actual_Discriminant_Ref (Hi)));
6273 end Actual_Index_Type;
6275 -- Start of processing of Resolve_Entry
6278 -- Find name of entry being called, and resolve prefix of name with its
6279 -- own type. The prefix can be overloaded, and the name and signature of
6280 -- the entry must be taken into account.
6282 if Nkind (Entry_Name) = N_Indexed_Component then
6284 -- Case of dealing with entry family within the current tasks
6286 E_Name := Prefix (Entry_Name);
6289 E_Name := Entry_Name;
6292 if Is_Entity_Name (E_Name) then
6294 -- Entry call to an entry (or entry family) in the current task. This
6295 -- is legal even though the task will deadlock. Rewrite as call to
6298 -- This can also be a call to an entry in an enclosing task. If this
6299 -- is a single task, we have to retrieve its name, because the scope
6300 -- of the entry is the task type, not the object. If the enclosing
6301 -- task is a task type, the identity of the task is given by its own
6304 -- Finally this can be a requeue on an entry of the same task or
6305 -- protected object.
6307 S := Scope (Entity (E_Name));
6309 for J in reverse 0 .. Scope_Stack.Last loop
6310 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6311 and then not Comes_From_Source (S)
6313 -- S is an enclosing task or protected object. The concurrent
6314 -- declaration has been converted into a type declaration, and
6315 -- the object itself has an object declaration that follows
6316 -- the type in the same declarative part.
6318 Tsk := Next_Entity (S);
6319 while Etype (Tsk) /= S loop
6326 elsif S = Scope_Stack.Table (J).Entity then
6328 -- Call to current task. Will be transformed into call to Self
6336 Make_Selected_Component (Loc,
6337 Prefix => New_Occurrence_Of (S, Loc),
6339 New_Occurrence_Of (Entity (E_Name), Loc));
6340 Rewrite (E_Name, New_N);
6343 elsif Nkind (Entry_Name) = N_Selected_Component
6344 and then Is_Overloaded (Prefix (Entry_Name))
6346 -- Use the entry name (which must be unique at this point) to find
6347 -- the prefix that returns the corresponding task/protected type.
6350 Pref : constant Node_Id := Prefix (Entry_Name);
6351 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6356 Get_First_Interp (Pref, I, It);
6357 while Present (It.Typ) loop
6358 if Scope (Ent) = It.Typ then
6359 Set_Etype (Pref, It.Typ);
6363 Get_Next_Interp (I, It);
6368 if Nkind (Entry_Name) = N_Selected_Component then
6369 Resolve (Prefix (Entry_Name));
6371 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6372 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6373 Resolve (Prefix (Prefix (Entry_Name)));
6374 Index := First (Expressions (Entry_Name));
6375 Resolve (Index, Entry_Index_Type (Nam));
6377 -- Up to this point the expression could have been the actual in a
6378 -- simple entry call, and be given by a named association.
6380 if Nkind (Index) = N_Parameter_Association then
6381 Error_Msg_N ("expect expression for entry index", Index);
6383 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6388 ------------------------
6389 -- Resolve_Entry_Call --
6390 ------------------------
6392 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6393 Entry_Name : constant Node_Id := Name (N);
6394 Loc : constant Source_Ptr := Sloc (Entry_Name);
6396 First_Named : Node_Id;
6403 -- We kill all checks here, because it does not seem worth the effort to
6404 -- do anything better, an entry call is a big operation.
6408 -- Processing of the name is similar for entry calls and protected
6409 -- operation calls. Once the entity is determined, we can complete
6410 -- the resolution of the actuals.
6412 -- The selector may be overloaded, in the case of a protected object
6413 -- with overloaded functions. The type of the context is used for
6416 if Nkind (Entry_Name) = N_Selected_Component
6417 and then Is_Overloaded (Selector_Name (Entry_Name))
6418 and then Typ /= Standard_Void_Type
6425 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6426 while Present (It.Typ) loop
6427 if Covers (Typ, It.Typ) then
6428 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6429 Set_Etype (Entry_Name, It.Typ);
6431 Generate_Reference (It.Typ, N, ' ');
6434 Get_Next_Interp (I, It);
6439 Resolve_Entry (Entry_Name);
6441 if Nkind (Entry_Name) = N_Selected_Component then
6443 -- Simple entry call
6445 Nam := Entity (Selector_Name (Entry_Name));
6446 Obj := Prefix (Entry_Name);
6447 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6449 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6451 -- Call to member of entry family
6453 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6454 Obj := Prefix (Prefix (Entry_Name));
6455 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6458 -- We cannot in general check the maximum depth of protected entry calls
6459 -- at compile time. But we can tell that any protected entry call at all
6460 -- violates a specified nesting depth of zero.
6462 if Is_Protected_Type (Scope (Nam)) then
6463 Check_Restriction (Max_Entry_Queue_Length, N);
6466 -- Use context type to disambiguate a protected function that can be
6467 -- called without actuals and that returns an array type, and where the
6468 -- argument list may be an indexing of the returned value.
6470 if Ekind (Nam) = E_Function
6471 and then Needs_No_Actuals (Nam)
6472 and then Present (Parameter_Associations (N))
6474 ((Is_Array_Type (Etype (Nam))
6475 and then Covers (Typ, Component_Type (Etype (Nam))))
6477 or else (Is_Access_Type (Etype (Nam))
6478 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6482 Component_Type (Designated_Type (Etype (Nam))))))
6485 Index_Node : Node_Id;
6489 Make_Indexed_Component (Loc,
6491 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6492 Expressions => Parameter_Associations (N));
6494 -- Since we are correcting a node classification error made by the
6495 -- parser, we call Replace rather than Rewrite.
6497 Replace (N, Index_Node);
6498 Set_Etype (Prefix (N), Etype (Nam));
6500 Resolve_Indexed_Component (N, Typ);
6505 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6506 and then Present (PPC_Wrapper (Nam))
6507 and then Current_Scope /= PPC_Wrapper (Nam)
6509 -- Rewrite as call to the precondition wrapper, adding the task
6510 -- object to the list of actuals. If the call is to a member of an
6511 -- entry family, include the index as well.
6515 New_Actuals : List_Id;
6518 New_Actuals := New_List (Obj);
6520 if Nkind (Entry_Name) = N_Indexed_Component then
6521 Append_To (New_Actuals,
6522 New_Copy_Tree (First (Expressions (Entry_Name))));
6525 Append_List (Parameter_Associations (N), New_Actuals);
6527 Make_Procedure_Call_Statement (Loc,
6529 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6530 Parameter_Associations => New_Actuals);
6531 Rewrite (N, New_Call);
6532 Analyze_And_Resolve (N);
6537 -- The operation name may have been overloaded. Order the actuals
6538 -- according to the formals of the resolved entity, and set the return
6539 -- type to that of the operation.
6542 Normalize_Actuals (N, Nam, False, Norm_OK);
6543 pragma Assert (Norm_OK);
6544 Set_Etype (N, Etype (Nam));
6547 Resolve_Actuals (N, Nam);
6549 -- Create a call reference to the entry
6551 Generate_Reference (Nam, Entry_Name, 's');
6553 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6554 Check_Potentially_Blocking_Operation (N);
6557 -- Verify that a procedure call cannot masquerade as an entry
6558 -- call where an entry call is expected.
6560 if Ekind (Nam) = E_Procedure then
6561 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6562 and then N = Entry_Call_Statement (Parent (N))
6564 Error_Msg_N ("entry call required in select statement", N);
6566 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6567 and then N = Triggering_Statement (Parent (N))
6569 Error_Msg_N ("triggering statement cannot be procedure call", N);
6571 elsif Ekind (Scope (Nam)) = E_Task_Type
6572 and then not In_Open_Scopes (Scope (Nam))
6574 Error_Msg_N ("task has no entry with this name", Entry_Name);
6578 -- After resolution, entry calls and protected procedure calls are
6579 -- changed into entry calls, for expansion. The structure of the node
6580 -- does not change, so it can safely be done in place. Protected
6581 -- function calls must keep their structure because they are
6584 if Ekind (Nam) /= E_Function then
6586 -- A protected operation that is not a function may modify the
6587 -- corresponding object, and cannot apply to a constant. If this
6588 -- is an internal call, the prefix is the type itself.
6590 if Is_Protected_Type (Scope (Nam))
6591 and then not Is_Variable (Obj)
6592 and then (not Is_Entity_Name (Obj)
6593 or else not Is_Type (Entity (Obj)))
6596 ("prefix of protected procedure or entry call must be variable",
6600 Actuals := Parameter_Associations (N);
6601 First_Named := First_Named_Actual (N);
6604 Make_Entry_Call_Statement (Loc,
6606 Parameter_Associations => Actuals));
6608 Set_First_Named_Actual (N, First_Named);
6609 Set_Analyzed (N, True);
6611 -- Protected functions can return on the secondary stack, in which
6612 -- case we must trigger the transient scope mechanism.
6614 elsif Expander_Active
6615 and then Requires_Transient_Scope (Etype (Nam))
6617 Establish_Transient_Scope (N, Sec_Stack => True);
6619 end Resolve_Entry_Call;
6621 -------------------------
6622 -- Resolve_Equality_Op --
6623 -------------------------
6625 -- Both arguments must have the same type, and the boolean context does
6626 -- not participate in the resolution. The first pass verifies that the
6627 -- interpretation is not ambiguous, and the type of the left argument is
6628 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6629 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6630 -- though they carry a single (universal) type. Diagnose this case here.
6632 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6633 L : constant Node_Id := Left_Opnd (N);
6634 R : constant Node_Id := Right_Opnd (N);
6635 T : Entity_Id := Find_Unique_Type (L, R);
6637 procedure Check_Conditional_Expression (Cond : Node_Id);
6638 -- The resolution rule for conditional expressions requires that each
6639 -- such must have a unique type. This means that if several dependent
6640 -- expressions are of a non-null anonymous access type, and the context
6641 -- does not impose an expected type (as can be the case in an equality
6642 -- operation) the expression must be rejected.
6644 function Find_Unique_Access_Type return Entity_Id;
6645 -- In the case of allocators, make a last-ditch attempt to find a single
6646 -- access type with the right designated type. This is semantically
6647 -- dubious, and of no interest to any real code, but c48008a makes it
6650 ----------------------------------
6651 -- Check_Conditional_Expression --
6652 ----------------------------------
6654 procedure Check_Conditional_Expression (Cond : Node_Id) is
6655 Then_Expr : Node_Id;
6656 Else_Expr : Node_Id;
6659 if Nkind (Cond) = N_Conditional_Expression then
6660 Then_Expr := Next (First (Expressions (Cond)));
6661 Else_Expr := Next (Then_Expr);
6663 if Nkind (Then_Expr) /= N_Null
6664 and then Nkind (Else_Expr) /= N_Null
6667 ("cannot determine type of conditional expression", Cond);
6670 end Check_Conditional_Expression;
6672 -----------------------------
6673 -- Find_Unique_Access_Type --
6674 -----------------------------
6676 function Find_Unique_Access_Type return Entity_Id is
6682 if Ekind (Etype (R)) = E_Allocator_Type then
6683 Acc := Designated_Type (Etype (R));
6684 elsif Ekind (Etype (L)) = E_Allocator_Type then
6685 Acc := Designated_Type (Etype (L));
6691 while S /= Standard_Standard loop
6692 E := First_Entity (S);
6693 while Present (E) loop
6695 and then Is_Access_Type (E)
6696 and then Ekind (E) /= E_Allocator_Type
6697 and then Designated_Type (E) = Base_Type (Acc)
6709 end Find_Unique_Access_Type;
6711 -- Start of processing for Resolve_Equality_Op
6714 Set_Etype (N, Base_Type (Typ));
6715 Generate_Reference (T, N, ' ');
6717 if T = Any_Fixed then
6718 T := Unique_Fixed_Point_Type (L);
6721 if T /= Any_Type then
6722 if T = Any_String or else
6723 T = Any_Composite or else
6726 if T = Any_Character then
6727 Ambiguous_Character (L);
6729 Error_Msg_N ("ambiguous operands for equality", N);
6732 Set_Etype (N, Any_Type);
6735 elsif T = Any_Access
6736 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6738 T := Find_Unique_Access_Type;
6741 Error_Msg_N ("ambiguous operands for equality", N);
6742 Set_Etype (N, Any_Type);
6746 -- Conditional expressions must have a single type, and if the
6747 -- context does not impose one the dependent expressions cannot
6748 -- be anonymous access types.
6750 elsif Ada_Version >= Ada_2012
6751 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6752 E_Anonymous_Access_Subprogram_Type)
6753 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6754 E_Anonymous_Access_Subprogram_Type)
6756 Check_Conditional_Expression (L);
6757 Check_Conditional_Expression (R);
6763 -- In SPARK, equality operators = and /= for array types other than
6764 -- String are only defined when, for each index position, the
6765 -- operands have equal static bounds.
6767 if Is_Array_Type (T) then
6768 -- Protect call to Matching_Static_Array_Bounds to avoid costly
6769 -- operation if not needed.
6771 if Restriction_Check_Required (SPARK)
6772 and then Base_Type (T) /= Standard_String
6773 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6774 and then Etype (L) /= Any_Composite -- or else L in error
6775 and then Etype (R) /= Any_Composite -- or else R in error
6776 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6778 Check_SPARK_Restriction
6779 ("array types should have matching static bounds", N);
6783 -- If the unique type is a class-wide type then it will be expanded
6784 -- into a dispatching call to the predefined primitive. Therefore we
6785 -- check here for potential violation of such restriction.
6787 if Is_Class_Wide_Type (T) then
6788 Check_Restriction (No_Dispatching_Calls, N);
6791 if Warn_On_Redundant_Constructs
6792 and then Comes_From_Source (N)
6793 and then Is_Entity_Name (R)
6794 and then Entity (R) = Standard_True
6795 and then Comes_From_Source (R)
6797 Error_Msg_N -- CODEFIX
6798 ("?comparison with True is redundant!", R);
6801 Check_Unset_Reference (L);
6802 Check_Unset_Reference (R);
6803 Generate_Operator_Reference (N, T);
6804 Check_Low_Bound_Tested (N);
6806 -- If this is an inequality, it may be the implicit inequality
6807 -- created for a user-defined operation, in which case the corres-
6808 -- ponding equality operation is not intrinsic, and the operation
6809 -- cannot be constant-folded. Else fold.
6811 if Nkind (N) = N_Op_Eq
6812 or else Comes_From_Source (Entity (N))
6813 or else Ekind (Entity (N)) = E_Operator
6814 or else Is_Intrinsic_Subprogram
6815 (Corresponding_Equality (Entity (N)))
6817 Eval_Relational_Op (N);
6819 elsif Nkind (N) = N_Op_Ne
6820 and then Is_Abstract_Subprogram (Entity (N))
6822 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6825 -- Ada 2005: If one operand is an anonymous access type, convert the
6826 -- other operand to it, to ensure that the underlying types match in
6827 -- the back-end. Same for access_to_subprogram, and the conversion
6828 -- verifies that the types are subtype conformant.
6830 -- We apply the same conversion in the case one of the operands is a
6831 -- private subtype of the type of the other.
6833 -- Why the Expander_Active test here ???
6837 (Ekind_In (T, E_Anonymous_Access_Type,
6838 E_Anonymous_Access_Subprogram_Type)
6839 or else Is_Private_Type (T))
6841 if Etype (L) /= T then
6843 Make_Unchecked_Type_Conversion (Sloc (L),
6844 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6845 Expression => Relocate_Node (L)));
6846 Analyze_And_Resolve (L, T);
6849 if (Etype (R)) /= T then
6851 Make_Unchecked_Type_Conversion (Sloc (R),
6852 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6853 Expression => Relocate_Node (R)));
6854 Analyze_And_Resolve (R, T);
6858 end Resolve_Equality_Op;
6860 ----------------------------------
6861 -- Resolve_Explicit_Dereference --
6862 ----------------------------------
6864 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6865 Loc : constant Source_Ptr := Sloc (N);
6867 P : constant Node_Id := Prefix (N);
6872 Check_Fully_Declared_Prefix (Typ, P);
6874 if Is_Overloaded (P) then
6876 -- Use the context type to select the prefix that has the correct
6879 Get_First_Interp (P, I, It);
6880 while Present (It.Typ) loop
6881 exit when Is_Access_Type (It.Typ)
6882 and then Covers (Typ, Designated_Type (It.Typ));
6883 Get_Next_Interp (I, It);
6886 if Present (It.Typ) then
6887 Resolve (P, It.Typ);
6889 -- If no interpretation covers the designated type of the prefix,
6890 -- this is the pathological case where not all implementations of
6891 -- the prefix allow the interpretation of the node as a call. Now
6892 -- that the expected type is known, Remove other interpretations
6893 -- from prefix, rewrite it as a call, and resolve again, so that
6894 -- the proper call node is generated.
6896 Get_First_Interp (P, I, It);
6897 while Present (It.Typ) loop
6898 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6902 Get_Next_Interp (I, It);
6906 Make_Function_Call (Loc,
6908 Make_Explicit_Dereference (Loc,
6910 Parameter_Associations => New_List);
6912 Save_Interps (N, New_N);
6914 Analyze_And_Resolve (N, Typ);
6918 Set_Etype (N, Designated_Type (It.Typ));
6924 if Is_Access_Type (Etype (P)) then
6925 Apply_Access_Check (N);
6928 -- If the designated type is a packed unconstrained array type, and the
6929 -- explicit dereference is not in the context of an attribute reference,
6930 -- then we must compute and set the actual subtype, since it is needed
6931 -- by Gigi. The reason we exclude the attribute case is that this is
6932 -- handled fine by Gigi, and in fact we use such attributes to build the
6933 -- actual subtype. We also exclude generated code (which builds actual
6934 -- subtypes directly if they are needed).
6936 if Is_Array_Type (Etype (N))
6937 and then Is_Packed (Etype (N))
6938 and then not Is_Constrained (Etype (N))
6939 and then Nkind (Parent (N)) /= N_Attribute_Reference
6940 and then Comes_From_Source (N)
6942 Set_Etype (N, Get_Actual_Subtype (N));
6945 -- Note: No Eval processing is required for an explicit dereference,
6946 -- because such a name can never be static.
6948 end Resolve_Explicit_Dereference;
6950 -------------------------------------
6951 -- Resolve_Expression_With_Actions --
6952 -------------------------------------
6954 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
6957 end Resolve_Expression_With_Actions;
6959 -------------------------------
6960 -- Resolve_Indexed_Component --
6961 -------------------------------
6963 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6964 Name : constant Node_Id := Prefix (N);
6966 Array_Type : Entity_Id := Empty; -- to prevent junk warning
6970 if Is_Overloaded (Name) then
6972 -- Use the context type to select the prefix that yields the correct
6978 I1 : Interp_Index := 0;
6979 P : constant Node_Id := Prefix (N);
6980 Found : Boolean := False;
6983 Get_First_Interp (P, I, It);
6984 while Present (It.Typ) loop
6985 if (Is_Array_Type (It.Typ)
6986 and then Covers (Typ, Component_Type (It.Typ)))
6987 or else (Is_Access_Type (It.Typ)
6988 and then Is_Array_Type (Designated_Type (It.Typ))
6992 Component_Type (Designated_Type (It.Typ))))
6995 It := Disambiguate (P, I1, I, Any_Type);
6997 if It = No_Interp then
6998 Error_Msg_N ("ambiguous prefix for indexing", N);
7004 Array_Type := It.Typ;
7010 Array_Type := It.Typ;
7015 Get_Next_Interp (I, It);
7020 Array_Type := Etype (Name);
7023 Resolve (Name, Array_Type);
7024 Array_Type := Get_Actual_Subtype_If_Available (Name);
7026 -- If prefix is access type, dereference to get real array type.
7027 -- Note: we do not apply an access check because the expander always
7028 -- introduces an explicit dereference, and the check will happen there.
7030 if Is_Access_Type (Array_Type) then
7031 Array_Type := Designated_Type (Array_Type);
7034 -- If name was overloaded, set component type correctly now
7035 -- If a misplaced call to an entry family (which has no index types)
7036 -- return. Error will be diagnosed from calling context.
7038 if Is_Array_Type (Array_Type) then
7039 Set_Etype (N, Component_Type (Array_Type));
7044 Index := First_Index (Array_Type);
7045 Expr := First (Expressions (N));
7047 -- The prefix may have resolved to a string literal, in which case its
7048 -- etype has a special representation. This is only possible currently
7049 -- if the prefix is a static concatenation, written in functional
7052 if Ekind (Array_Type) = E_String_Literal_Subtype then
7053 Resolve (Expr, Standard_Positive);
7056 while Present (Index) and Present (Expr) loop
7057 Resolve (Expr, Etype (Index));
7058 Check_Unset_Reference (Expr);
7060 if Is_Scalar_Type (Etype (Expr)) then
7061 Apply_Scalar_Range_Check (Expr, Etype (Index));
7063 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7071 -- Do not generate the warning on suspicious index if we are analyzing
7072 -- package Ada.Tags; otherwise we will report the warning with the
7073 -- Prims_Ptr field of the dispatch table.
7075 if Scope (Etype (Prefix (N))) = Standard_Standard
7077 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7080 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7081 Eval_Indexed_Component (N);
7084 -- If the array type is atomic, and is packed, and we are in a left side
7085 -- context, then this is worth a warning, since we have a situation
7086 -- where the access to the component may cause extra read/writes of
7087 -- the atomic array object, which could be considered unexpected.
7089 if Nkind (N) = N_Indexed_Component
7090 and then (Is_Atomic (Array_Type)
7091 or else (Is_Entity_Name (Prefix (N))
7092 and then Is_Atomic (Entity (Prefix (N)))))
7093 and then Is_Bit_Packed_Array (Array_Type)
7096 Error_Msg_N ("?assignment to component of packed atomic array",
7098 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7101 end Resolve_Indexed_Component;
7103 -----------------------------
7104 -- Resolve_Integer_Literal --
7105 -----------------------------
7107 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7110 Eval_Integer_Literal (N);
7111 end Resolve_Integer_Literal;
7113 --------------------------------
7114 -- Resolve_Intrinsic_Operator --
7115 --------------------------------
7117 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7118 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7120 Orig_Op : constant Entity_Id := Entity (N);
7124 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7125 -- If the operand is a literal, it cannot be the expression in a
7126 -- conversion. Use a qualified expression instead.
7128 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7129 Loc : constant Source_Ptr := Sloc (Opnd);
7132 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7134 Make_Qualified_Expression (Loc,
7135 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7136 Expression => Relocate_Node (Opnd));
7140 Res := Unchecked_Convert_To (Btyp, Opnd);
7144 end Convert_Operand;
7147 -- We must preserve the original entity in a generic setting, so that
7148 -- the legality of the operation can be verified in an instance.
7150 if not Expander_Active then
7155 while Scope (Op) /= Standard_Standard loop
7157 pragma Assert (Present (Op));
7161 Set_Is_Overloaded (N, False);
7163 -- If the operand type is private, rewrite with suitable conversions on
7164 -- the operands and the result, to expose the proper underlying numeric
7167 if Is_Private_Type (Typ) then
7168 Arg1 := Convert_Operand (Left_Opnd (N));
7169 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7171 if Nkind (N) = N_Op_Expon then
7172 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7174 Arg2 := Convert_Operand (Right_Opnd (N));
7177 if Nkind (Arg1) = N_Type_Conversion then
7178 Save_Interps (Left_Opnd (N), Expression (Arg1));
7181 if Nkind (Arg2) = N_Type_Conversion then
7182 Save_Interps (Right_Opnd (N), Expression (Arg2));
7185 Set_Left_Opnd (N, Arg1);
7186 Set_Right_Opnd (N, Arg2);
7188 Set_Etype (N, Btyp);
7189 Rewrite (N, Unchecked_Convert_To (Typ, N));
7192 elsif Typ /= Etype (Left_Opnd (N))
7193 or else Typ /= Etype (Right_Opnd (N))
7195 -- Add explicit conversion where needed, and save interpretations in
7196 -- case operands are overloaded. If the context is a VMS operation,
7197 -- assert that the conversion is legal (the operands have the proper
7198 -- types to select the VMS intrinsic). Note that in rare cases the
7199 -- VMS operators may be visible, but the default System is being used
7200 -- and Address is a private type.
7202 Arg1 := Convert_To (Typ, Left_Opnd (N));
7203 Arg2 := Convert_To (Typ, Right_Opnd (N));
7205 if Nkind (Arg1) = N_Type_Conversion then
7206 Save_Interps (Left_Opnd (N), Expression (Arg1));
7208 if Is_VMS_Operator (Orig_Op) then
7209 Set_Conversion_OK (Arg1);
7212 Save_Interps (Left_Opnd (N), Arg1);
7215 if Nkind (Arg2) = N_Type_Conversion then
7216 Save_Interps (Right_Opnd (N), Expression (Arg2));
7218 if Is_VMS_Operator (Orig_Op) then
7219 Set_Conversion_OK (Arg2);
7222 Save_Interps (Right_Opnd (N), Arg2);
7225 Rewrite (Left_Opnd (N), Arg1);
7226 Rewrite (Right_Opnd (N), Arg2);
7229 Resolve_Arithmetic_Op (N, Typ);
7232 Resolve_Arithmetic_Op (N, Typ);
7234 end Resolve_Intrinsic_Operator;
7236 --------------------------------------
7237 -- Resolve_Intrinsic_Unary_Operator --
7238 --------------------------------------
7240 procedure Resolve_Intrinsic_Unary_Operator
7244 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7250 while Scope (Op) /= Standard_Standard loop
7252 pragma Assert (Present (Op));
7257 if Is_Private_Type (Typ) then
7258 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7259 Save_Interps (Right_Opnd (N), Expression (Arg2));
7261 Set_Right_Opnd (N, Arg2);
7263 Set_Etype (N, Btyp);
7264 Rewrite (N, Unchecked_Convert_To (Typ, N));
7268 Resolve_Unary_Op (N, Typ);
7270 end Resolve_Intrinsic_Unary_Operator;
7272 ------------------------
7273 -- Resolve_Logical_Op --
7274 ------------------------
7276 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7280 Check_No_Direct_Boolean_Operators (N);
7282 -- Predefined operations on scalar types yield the base type. On the
7283 -- other hand, logical operations on arrays yield the type of the
7284 -- arguments (and the context).
7286 if Is_Array_Type (Typ) then
7289 B_Typ := Base_Type (Typ);
7292 -- OK if this is a VMS-specific intrinsic operation
7294 if Is_VMS_Operator (Entity (N)) then
7297 -- The following test is required because the operands of the operation
7298 -- may be literals, in which case the resulting type appears to be
7299 -- compatible with a signed integer type, when in fact it is compatible
7300 -- only with modular types. If the context itself is universal, the
7301 -- operation is illegal.
7303 elsif not Valid_Boolean_Arg (Typ) then
7304 Error_Msg_N ("invalid context for logical operation", N);
7305 Set_Etype (N, Any_Type);
7308 elsif Typ = Any_Modular then
7310 ("no modular type available in this context", N);
7311 Set_Etype (N, Any_Type);
7314 elsif Is_Modular_Integer_Type (Typ)
7315 and then Etype (Left_Opnd (N)) = Universal_Integer
7316 and then Etype (Right_Opnd (N)) = Universal_Integer
7318 Check_For_Visible_Operator (N, B_Typ);
7321 Resolve (Left_Opnd (N), B_Typ);
7322 Resolve (Right_Opnd (N), B_Typ);
7324 Check_Unset_Reference (Left_Opnd (N));
7325 Check_Unset_Reference (Right_Opnd (N));
7327 Set_Etype (N, B_Typ);
7328 Generate_Operator_Reference (N, B_Typ);
7329 Eval_Logical_Op (N);
7331 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7332 -- only when both operands have same static lower and higher bounds. Of
7333 -- course the types have to match, so only check if operands are
7334 -- compatible and the node itself has no errors.
7336 if Is_Array_Type (B_Typ)
7337 and then Nkind (N) in N_Binary_Op
7340 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7341 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7344 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7345 -- operation if not needed.
7347 if Restriction_Check_Required (SPARK)
7348 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7349 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7350 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7351 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7353 Check_SPARK_Restriction
7354 ("array types should have matching static bounds", N);
7358 end Resolve_Logical_Op;
7360 ---------------------------
7361 -- Resolve_Membership_Op --
7362 ---------------------------
7364 -- The context can only be a boolean type, and does not determine the
7365 -- arguments. Arguments should be unambiguous, but the preference rule for
7366 -- universal types applies.
7368 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7369 pragma Warnings (Off, Typ);
7371 L : constant Node_Id := Left_Opnd (N);
7372 R : constant Node_Id := Right_Opnd (N);
7375 procedure Resolve_Set_Membership;
7376 -- Analysis has determined a unique type for the left operand. Use it to
7377 -- resolve the disjuncts.
7379 ----------------------------
7380 -- Resolve_Set_Membership --
7381 ----------------------------
7383 procedure Resolve_Set_Membership is
7387 Resolve (L, Etype (L));
7389 Alt := First (Alternatives (N));
7390 while Present (Alt) loop
7392 -- Alternative is an expression, a range
7393 -- or a subtype mark.
7395 if not Is_Entity_Name (Alt)
7396 or else not Is_Type (Entity (Alt))
7398 Resolve (Alt, Etype (L));
7403 end Resolve_Set_Membership;
7405 -- Start of processing for Resolve_Membership_Op
7408 if L = Error or else R = Error then
7412 if Present (Alternatives (N)) then
7413 Resolve_Set_Membership;
7416 elsif not Is_Overloaded (R)
7418 (Etype (R) = Universal_Integer
7420 Etype (R) = Universal_Real)
7421 and then Is_Overloaded (L)
7425 -- Ada 2005 (AI-251): Support the following case:
7427 -- type I is interface;
7428 -- type T is tagged ...
7430 -- function Test (O : I'Class) is
7432 -- return O in T'Class.
7435 -- In this case we have nothing else to do. The membership test will be
7436 -- done at run time.
7438 elsif Ada_Version >= Ada_2005
7439 and then Is_Class_Wide_Type (Etype (L))
7440 and then Is_Interface (Etype (L))
7441 and then Is_Class_Wide_Type (Etype (R))
7442 and then not Is_Interface (Etype (R))
7446 T := Intersect_Types (L, R);
7449 -- If mixed-mode operations are present and operands are all literal,
7450 -- the only interpretation involves Duration, which is probably not
7451 -- the intention of the programmer.
7453 if T = Any_Fixed then
7454 T := Unique_Fixed_Point_Type (N);
7456 if T = Any_Type then
7462 Check_Unset_Reference (L);
7464 if Nkind (R) = N_Range
7465 and then not Is_Scalar_Type (T)
7467 Error_Msg_N ("scalar type required for range", R);
7470 if Is_Entity_Name (R) then
7471 Freeze_Expression (R);
7474 Check_Unset_Reference (R);
7477 Eval_Membership_Op (N);
7478 end Resolve_Membership_Op;
7484 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7485 Loc : constant Source_Ptr := Sloc (N);
7488 -- Handle restriction against anonymous null access values This
7489 -- restriction can be turned off using -gnatdj.
7491 -- Ada 2005 (AI-231): Remove restriction
7493 if Ada_Version < Ada_2005
7494 and then not Debug_Flag_J
7495 and then Ekind (Typ) = E_Anonymous_Access_Type
7496 and then Comes_From_Source (N)
7498 -- In the common case of a call which uses an explicitly null value
7499 -- for an access parameter, give specialized error message.
7501 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7505 ("null is not allowed as argument for an access parameter", N);
7507 -- Standard message for all other cases (are there any?)
7511 ("null cannot be of an anonymous access type", N);
7515 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7516 -- assignment to a null-excluding object
7518 if Ada_Version >= Ada_2005
7519 and then Can_Never_Be_Null (Typ)
7520 and then Nkind (Parent (N)) = N_Assignment_Statement
7522 if not Inside_Init_Proc then
7524 (Compile_Time_Constraint_Error (N,
7525 "(Ada 2005) null not allowed in null-excluding objects?"),
7526 Make_Raise_Constraint_Error (Loc,
7527 Reason => CE_Access_Check_Failed));
7530 Make_Raise_Constraint_Error (Loc,
7531 Reason => CE_Access_Check_Failed));
7535 -- In a distributed context, null for a remote access to subprogram may
7536 -- need to be replaced with a special record aggregate. In this case,
7537 -- return after having done the transformation.
7539 if (Ekind (Typ) = E_Record_Type
7540 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7541 and then Remote_AST_Null_Value (N, Typ)
7546 -- The null literal takes its type from the context
7551 -----------------------
7552 -- Resolve_Op_Concat --
7553 -----------------------
7555 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7557 -- We wish to avoid deep recursion, because concatenations are often
7558 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7559 -- operands nonrecursively until we find something that is not a simple
7560 -- concatenation (A in this case). We resolve that, and then walk back
7561 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7562 -- to do the rest of the work at each level. The Parent pointers allow
7563 -- us to avoid recursion, and thus avoid running out of memory. See also
7564 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7570 -- The following code is equivalent to:
7572 -- Resolve_Op_Concat_First (NN, Typ);
7573 -- Resolve_Op_Concat_Arg (N, ...);
7574 -- Resolve_Op_Concat_Rest (N, Typ);
7576 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7577 -- operand is a concatenation.
7579 -- Walk down left operands
7582 Resolve_Op_Concat_First (NN, Typ);
7583 Op1 := Left_Opnd (NN);
7584 exit when not (Nkind (Op1) = N_Op_Concat
7585 and then not Is_Array_Type (Component_Type (Typ))
7586 and then Entity (Op1) = Entity (NN));
7590 -- Now (given the above example) NN is A&B and Op1 is A
7592 -- First resolve Op1 ...
7594 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7596 -- ... then walk NN back up until we reach N (where we started), calling
7597 -- Resolve_Op_Concat_Rest along the way.
7600 Resolve_Op_Concat_Rest (NN, Typ);
7605 if Base_Type (Etype (N)) /= Standard_String then
7606 Check_SPARK_Restriction
7607 ("result of concatenation should have type String", N);
7609 end Resolve_Op_Concat;
7611 ---------------------------
7612 -- Resolve_Op_Concat_Arg --
7613 ---------------------------
7615 procedure Resolve_Op_Concat_Arg
7621 Btyp : constant Entity_Id := Base_Type (Typ);
7622 Ctyp : constant Entity_Id := Component_Type (Typ);
7627 or else (not Is_Overloaded (Arg)
7628 and then Etype (Arg) /= Any_Composite
7629 and then Covers (Ctyp, Etype (Arg)))
7631 Resolve (Arg, Ctyp);
7633 Resolve (Arg, Btyp);
7636 -- If both Array & Array and Array & Component are visible, there is a
7637 -- potential ambiguity that must be reported.
7639 elsif Has_Compatible_Type (Arg, Ctyp) then
7640 if Nkind (Arg) = N_Aggregate
7641 and then Is_Composite_Type (Ctyp)
7643 if Is_Private_Type (Ctyp) then
7644 Resolve (Arg, Btyp);
7646 -- If the operation is user-defined and not overloaded use its
7647 -- profile. The operation may be a renaming, in which case it has
7648 -- been rewritten, and we want the original profile.
7650 elsif not Is_Overloaded (N)
7651 and then Comes_From_Source (Entity (Original_Node (N)))
7652 and then Ekind (Entity (Original_Node (N))) = E_Function
7656 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
7659 -- Otherwise an aggregate may match both the array type and the
7663 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7664 Set_Etype (Arg, Any_Type);
7668 if Is_Overloaded (Arg)
7669 and then Has_Compatible_Type (Arg, Typ)
7670 and then Etype (Arg) /= Any_Type
7678 Get_First_Interp (Arg, I, It);
7680 Get_Next_Interp (I, It);
7682 -- Special-case the error message when the overloading is
7683 -- caused by a function that yields an array and can be
7684 -- called without parameters.
7686 if It.Nam = Func then
7687 Error_Msg_Sloc := Sloc (Func);
7688 Error_Msg_N ("ambiguous call to function#", Arg);
7690 ("\\interpretation as call yields&", Arg, Typ);
7692 ("\\interpretation as indexing of call yields&",
7693 Arg, Component_Type (Typ));
7696 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
7698 Get_First_Interp (Arg, I, It);
7699 while Present (It.Nam) loop
7700 Error_Msg_Sloc := Sloc (It.Nam);
7702 if Base_Type (It.Typ) = Btyp
7704 Base_Type (It.Typ) = Base_Type (Ctyp)
7706 Error_Msg_N -- CODEFIX
7707 ("\\possible interpretation#", Arg);
7710 Get_Next_Interp (I, It);
7716 Resolve (Arg, Component_Type (Typ));
7718 if Nkind (Arg) = N_String_Literal then
7719 Set_Etype (Arg, Component_Type (Typ));
7722 if Arg = Left_Opnd (N) then
7723 Set_Is_Component_Left_Opnd (N);
7725 Set_Is_Component_Right_Opnd (N);
7730 Resolve (Arg, Btyp);
7733 -- Concatenation is restricted in SPARK: each operand must be either a
7734 -- string literal, the name of a string constant, a static character or
7735 -- string expression, or another concatenation. Arg cannot be a
7736 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
7737 -- separately on each final operand, past concatenation operations.
7739 if Is_Character_Type (Etype (Arg)) then
7740 if not Is_Static_Expression (Arg) then
7741 Check_SPARK_Restriction
7742 ("character operand for concatenation should be static", N);
7745 elsif Is_String_Type (Etype (Arg)) then
7746 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
7747 and then Is_Constant_Object (Entity (Arg)))
7748 and then not Is_Static_Expression (Arg)
7750 Check_SPARK_Restriction
7751 ("string operand for concatenation should be static", N);
7754 -- Do not issue error on an operand that is neither a character nor a
7755 -- string, as the error is issued in Resolve_Op_Concat.
7761 Check_Unset_Reference (Arg);
7762 end Resolve_Op_Concat_Arg;
7764 -----------------------------
7765 -- Resolve_Op_Concat_First --
7766 -----------------------------
7768 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7769 Btyp : constant Entity_Id := Base_Type (Typ);
7770 Op1 : constant Node_Id := Left_Opnd (N);
7771 Op2 : constant Node_Id := Right_Opnd (N);
7774 -- The parser folds an enormous sequence of concatenations of string
7775 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7776 -- in the right operand. If the expression resolves to a predefined "&"
7777 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7778 -- we give an error. See P_Simple_Expression in Par.Ch4.
7780 if Nkind (Op2) = N_String_Literal
7781 and then Is_Folded_In_Parser (Op2)
7782 and then Ekind (Entity (N)) = E_Function
7784 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7785 and then String_Length (Strval (Op1)) = 0);
7786 Error_Msg_N ("too many user-defined concatenations", N);
7790 Set_Etype (N, Btyp);
7792 if Is_Limited_Composite (Btyp) then
7793 Error_Msg_N ("concatenation not available for limited array", N);
7794 Explain_Limited_Type (Btyp, N);
7796 end Resolve_Op_Concat_First;
7798 ----------------------------
7799 -- Resolve_Op_Concat_Rest --
7800 ----------------------------
7802 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7803 Op1 : constant Node_Id := Left_Opnd (N);
7804 Op2 : constant Node_Id := Right_Opnd (N);
7807 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7809 Generate_Operator_Reference (N, Typ);
7811 if Is_String_Type (Typ) then
7812 Eval_Concatenation (N);
7815 -- If this is not a static concatenation, but the result is a string
7816 -- type (and not an array of strings) ensure that static string operands
7817 -- have their subtypes properly constructed.
7819 if Nkind (N) /= N_String_Literal
7820 and then Is_Character_Type (Component_Type (Typ))
7822 Set_String_Literal_Subtype (Op1, Typ);
7823 Set_String_Literal_Subtype (Op2, Typ);
7825 end Resolve_Op_Concat_Rest;
7827 ----------------------
7828 -- Resolve_Op_Expon --
7829 ----------------------
7831 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7832 B_Typ : constant Entity_Id := Base_Type (Typ);
7835 -- Catch attempts to do fixed-point exponentiation with universal
7836 -- operands, which is a case where the illegality is not caught during
7837 -- normal operator analysis.
7839 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7840 Error_Msg_N ("exponentiation not available for fixed point", N);
7843 elsif Nkind (Parent (N)) in N_Op
7844 and then Is_Fixed_Point_Type (Etype (Parent (N)))
7845 and then Etype (N) = Universal_Real
7846 and then Comes_From_Source (N)
7848 Error_Msg_N ("exponentiation not available for fixed point", N);
7852 if Comes_From_Source (N)
7853 and then Ekind (Entity (N)) = E_Function
7854 and then Is_Imported (Entity (N))
7855 and then Is_Intrinsic_Subprogram (Entity (N))
7857 Resolve_Intrinsic_Operator (N, Typ);
7861 if Etype (Left_Opnd (N)) = Universal_Integer
7862 or else Etype (Left_Opnd (N)) = Universal_Real
7864 Check_For_Visible_Operator (N, B_Typ);
7867 -- We do the resolution using the base type, because intermediate values
7868 -- in expressions always are of the base type, not a subtype of it.
7870 Resolve (Left_Opnd (N), B_Typ);
7871 Resolve (Right_Opnd (N), Standard_Integer);
7873 Check_Unset_Reference (Left_Opnd (N));
7874 Check_Unset_Reference (Right_Opnd (N));
7876 Set_Etype (N, B_Typ);
7877 Generate_Operator_Reference (N, B_Typ);
7880 -- Set overflow checking bit. Much cleverer code needed here eventually
7881 -- and perhaps the Resolve routines should be separated for the various
7882 -- arithmetic operations, since they will need different processing. ???
7884 if Nkind (N) in N_Op then
7885 if not Overflow_Checks_Suppressed (Etype (N)) then
7886 Enable_Overflow_Check (N);
7889 end Resolve_Op_Expon;
7891 --------------------
7892 -- Resolve_Op_Not --
7893 --------------------
7895 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7898 function Parent_Is_Boolean return Boolean;
7899 -- This function determines if the parent node is a boolean operator or
7900 -- operation (comparison op, membership test, or short circuit form) and
7901 -- the not in question is the left operand of this operation. Note that
7902 -- if the not is in parens, then false is returned.
7904 -----------------------
7905 -- Parent_Is_Boolean --
7906 -----------------------
7908 function Parent_Is_Boolean return Boolean is
7910 if Paren_Count (N) /= 0 then
7914 case Nkind (Parent (N)) is
7929 return Left_Opnd (Parent (N)) = N;
7935 end Parent_Is_Boolean;
7937 -- Start of processing for Resolve_Op_Not
7940 -- Predefined operations on scalar types yield the base type. On the
7941 -- other hand, logical operations on arrays yield the type of the
7942 -- arguments (and the context).
7944 if Is_Array_Type (Typ) then
7947 B_Typ := Base_Type (Typ);
7950 if Is_VMS_Operator (Entity (N)) then
7953 -- Straightforward case of incorrect arguments
7955 elsif not Valid_Boolean_Arg (Typ) then
7956 Error_Msg_N ("invalid operand type for operator&", N);
7957 Set_Etype (N, Any_Type);
7960 -- Special case of probable missing parens
7962 elsif Typ = Universal_Integer or else Typ = Any_Modular then
7963 if Parent_Is_Boolean then
7965 ("operand of not must be enclosed in parentheses",
7969 ("no modular type available in this context", N);
7972 Set_Etype (N, Any_Type);
7975 -- OK resolution of NOT
7978 -- Warn if non-boolean types involved. This is a case like not a < b
7979 -- where a and b are modular, where we will get (not a) < b and most
7980 -- likely not (a < b) was intended.
7982 if Warn_On_Questionable_Missing_Parens
7983 and then not Is_Boolean_Type (Typ)
7984 and then Parent_Is_Boolean
7986 Error_Msg_N ("?not expression should be parenthesized here!", N);
7989 -- Warn on double negation if checking redundant constructs
7991 if Warn_On_Redundant_Constructs
7992 and then Comes_From_Source (N)
7993 and then Comes_From_Source (Right_Opnd (N))
7994 and then Root_Type (Typ) = Standard_Boolean
7995 and then Nkind (Right_Opnd (N)) = N_Op_Not
7997 Error_Msg_N ("redundant double negation?", N);
8000 -- Complete resolution and evaluation of NOT
8002 Resolve (Right_Opnd (N), B_Typ);
8003 Check_Unset_Reference (Right_Opnd (N));
8004 Set_Etype (N, B_Typ);
8005 Generate_Operator_Reference (N, B_Typ);
8010 -----------------------------
8011 -- Resolve_Operator_Symbol --
8012 -----------------------------
8014 -- Nothing to be done, all resolved already
8016 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8017 pragma Warnings (Off, N);
8018 pragma Warnings (Off, Typ);
8022 end Resolve_Operator_Symbol;
8024 ----------------------------------
8025 -- Resolve_Qualified_Expression --
8026 ----------------------------------
8028 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8029 pragma Warnings (Off, Typ);
8031 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8032 Expr : constant Node_Id := Expression (N);
8035 Resolve (Expr, Target_Typ);
8037 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8038 -- operation if not needed.
8040 if Restriction_Check_Required (SPARK)
8041 and then Is_Array_Type (Target_Typ)
8042 and then Is_Array_Type (Etype (Expr))
8043 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8044 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8046 Check_SPARK_Restriction
8047 ("array types should have matching static bounds", N);
8050 -- A qualified expression requires an exact match of the type, class-
8051 -- wide matching is not allowed. However, if the qualifying type is
8052 -- specific and the expression has a class-wide type, it may still be
8053 -- okay, since it can be the result of the expansion of a call to a
8054 -- dispatching function, so we also have to check class-wideness of the
8055 -- type of the expression's original node.
8057 if (Is_Class_Wide_Type (Target_Typ)
8059 (Is_Class_Wide_Type (Etype (Expr))
8060 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8061 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8063 Wrong_Type (Expr, Target_Typ);
8066 -- If the target type is unconstrained, then we reset the type of the
8067 -- result from the type of the expression. For other cases, the actual
8068 -- subtype of the expression is the target type.
8070 if Is_Composite_Type (Target_Typ)
8071 and then not Is_Constrained (Target_Typ)
8073 Set_Etype (N, Etype (Expr));
8076 Eval_Qualified_Expression (N);
8077 end Resolve_Qualified_Expression;
8079 -----------------------------------
8080 -- Resolve_Quantified_Expression --
8081 -----------------------------------
8083 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
8085 -- Normal mode (not ALFA)
8087 if not ALFA_Mode then
8089 -- The loop structure is already resolved during its analysis, only
8090 -- the resolution of the condition needs to be done. Expansion is
8091 -- disabled so that checks and other generated code are inserted in
8092 -- the tree after expression has been rewritten as a loop.
8094 Expander_Mode_Save_And_Set (False);
8095 Resolve (Condition (N), Typ);
8096 Expander_Mode_Restore;
8098 -- In ALFA_Mode, no magic needed, we just resolve the underlying nodes
8101 Resolve (Condition (N), Typ);
8103 end Resolve_Quantified_Expression;
8109 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8110 L : constant Node_Id := Low_Bound (N);
8111 H : constant Node_Id := High_Bound (N);
8113 function First_Last_Ref return Boolean;
8114 -- Returns True if N is of the form X'First .. X'Last where X is the
8115 -- same entity for both attributes.
8117 --------------------
8118 -- First_Last_Ref --
8119 --------------------
8121 function First_Last_Ref return Boolean is
8122 Lorig : constant Node_Id := Original_Node (L);
8123 Horig : constant Node_Id := Original_Node (H);
8126 if Nkind (Lorig) = N_Attribute_Reference
8127 and then Nkind (Horig) = N_Attribute_Reference
8128 and then Attribute_Name (Lorig) = Name_First
8129 and then Attribute_Name (Horig) = Name_Last
8132 PL : constant Node_Id := Prefix (Lorig);
8133 PH : constant Node_Id := Prefix (Horig);
8135 if Is_Entity_Name (PL)
8136 and then Is_Entity_Name (PH)
8137 and then Entity (PL) = Entity (PH)
8147 -- Start of processing for Resolve_Range
8154 -- Check for inappropriate range on unordered enumeration type
8156 if Bad_Unordered_Enumeration_Reference (N, Typ)
8158 -- Exclude X'First .. X'Last if X is the same entity for both
8160 and then not First_Last_Ref
8162 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
8165 Check_Unset_Reference (L);
8166 Check_Unset_Reference (H);
8168 -- We have to check the bounds for being within the base range as
8169 -- required for a non-static context. Normally this is automatic and
8170 -- done as part of evaluating expressions, but the N_Range node is an
8171 -- exception, since in GNAT we consider this node to be a subexpression,
8172 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8173 -- this, but that would put the test on the main evaluation path for
8176 Check_Non_Static_Context (L);
8177 Check_Non_Static_Context (H);
8179 -- Check for an ambiguous range over character literals. This will
8180 -- happen with a membership test involving only literals.
8182 if Typ = Any_Character then
8183 Ambiguous_Character (L);
8184 Set_Etype (N, Any_Type);
8188 -- If bounds are static, constant-fold them, so size computations are
8189 -- identical between front-end and back-end. Do not perform this
8190 -- transformation while analyzing generic units, as type information
8191 -- would be lost when reanalyzing the constant node in the instance.
8193 if Is_Discrete_Type (Typ) and then Expander_Active then
8194 if Is_OK_Static_Expression (L) then
8195 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8198 if Is_OK_Static_Expression (H) then
8199 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8204 --------------------------
8205 -- Resolve_Real_Literal --
8206 --------------------------
8208 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8209 Actual_Typ : constant Entity_Id := Etype (N);
8212 -- Special processing for fixed-point literals to make sure that the
8213 -- value is an exact multiple of small where this is required. We skip
8214 -- this for the universal real case, and also for generic types.
8216 if Is_Fixed_Point_Type (Typ)
8217 and then Typ /= Universal_Fixed
8218 and then Typ /= Any_Fixed
8219 and then not Is_Generic_Type (Typ)
8222 Val : constant Ureal := Realval (N);
8223 Cintr : constant Ureal := Val / Small_Value (Typ);
8224 Cint : constant Uint := UR_Trunc (Cintr);
8225 Den : constant Uint := Norm_Den (Cintr);
8229 -- Case of literal is not an exact multiple of the Small
8233 -- For a source program literal for a decimal fixed-point type,
8234 -- this is statically illegal (RM 4.9(36)).
8236 if Is_Decimal_Fixed_Point_Type (Typ)
8237 and then Actual_Typ = Universal_Real
8238 and then Comes_From_Source (N)
8240 Error_Msg_N ("value has extraneous low order digits", N);
8243 -- Generate a warning if literal from source
8245 if Is_Static_Expression (N)
8246 and then Warn_On_Bad_Fixed_Value
8249 ("?static fixed-point value is not a multiple of Small!",
8253 -- Replace literal by a value that is the exact representation
8254 -- of a value of the type, i.e. a multiple of the small value,
8255 -- by truncation, since Machine_Rounds is false for all GNAT
8256 -- fixed-point types (RM 4.9(38)).
8258 Stat := Is_Static_Expression (N);
8260 Make_Real_Literal (Sloc (N),
8261 Realval => Small_Value (Typ) * Cint));
8263 Set_Is_Static_Expression (N, Stat);
8266 -- In all cases, set the corresponding integer field
8268 Set_Corresponding_Integer_Value (N, Cint);
8272 -- Now replace the actual type by the expected type as usual
8275 Eval_Real_Literal (N);
8276 end Resolve_Real_Literal;
8278 -----------------------
8279 -- Resolve_Reference --
8280 -----------------------
8282 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8283 P : constant Node_Id := Prefix (N);
8286 -- Replace general access with specific type
8288 if Ekind (Etype (N)) = E_Allocator_Type then
8289 Set_Etype (N, Base_Type (Typ));
8292 Resolve (P, Designated_Type (Etype (N)));
8294 -- If we are taking the reference of a volatile entity, then treat it as
8295 -- a potential modification of this entity. This is too conservative,
8296 -- but necessary because remove side effects can cause transformations
8297 -- of normal assignments into reference sequences that otherwise fail to
8298 -- notice the modification.
8300 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8301 Note_Possible_Modification (P, Sure => False);
8303 end Resolve_Reference;
8305 --------------------------------
8306 -- Resolve_Selected_Component --
8307 --------------------------------
8309 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8311 Comp1 : Entity_Id := Empty; -- prevent junk warning
8312 P : constant Node_Id := Prefix (N);
8313 S : constant Node_Id := Selector_Name (N);
8314 T : Entity_Id := Etype (P);
8316 I1 : Interp_Index := 0; -- prevent junk warning
8321 function Init_Component return Boolean;
8322 -- Check whether this is the initialization of a component within an
8323 -- init proc (by assignment or call to another init proc). If true,
8324 -- there is no need for a discriminant check.
8326 --------------------
8327 -- Init_Component --
8328 --------------------
8330 function Init_Component return Boolean is
8332 return Inside_Init_Proc
8333 and then Nkind (Prefix (N)) = N_Identifier
8334 and then Chars (Prefix (N)) = Name_uInit
8335 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8338 -- Start of processing for Resolve_Selected_Component
8341 if Is_Overloaded (P) then
8343 -- Use the context type to select the prefix that has a selector
8344 -- of the correct name and type.
8347 Get_First_Interp (P, I, It);
8349 Search : while Present (It.Typ) loop
8350 if Is_Access_Type (It.Typ) then
8351 T := Designated_Type (It.Typ);
8356 -- Locate selected component. For a private prefix the selector
8357 -- can denote a discriminant.
8359 if Is_Record_Type (T) or else Is_Private_Type (T) then
8361 -- The visible components of a class-wide type are those of
8364 if Is_Class_Wide_Type (T) then
8368 Comp := First_Entity (T);
8369 while Present (Comp) loop
8370 if Chars (Comp) = Chars (S)
8371 and then Covers (Etype (Comp), Typ)
8380 It := Disambiguate (P, I1, I, Any_Type);
8382 if It = No_Interp then
8384 ("ambiguous prefix for selected component", N);
8391 -- There may be an implicit dereference. Retrieve
8392 -- designated record type.
8394 if Is_Access_Type (It1.Typ) then
8395 T := Designated_Type (It1.Typ);
8400 if Scope (Comp1) /= T then
8402 -- Resolution chooses the new interpretation.
8403 -- Find the component with the right name.
8405 Comp1 := First_Entity (T);
8406 while Present (Comp1)
8407 and then Chars (Comp1) /= Chars (S)
8409 Comp1 := Next_Entity (Comp1);
8418 Comp := Next_Entity (Comp);
8422 Get_Next_Interp (I, It);
8425 Resolve (P, It1.Typ);
8427 Set_Entity_With_Style_Check (S, Comp1);
8430 -- Resolve prefix with its type
8435 -- Generate cross-reference. We needed to wait until full overloading
8436 -- resolution was complete to do this, since otherwise we can't tell if
8437 -- we are an lvalue or not.
8439 if May_Be_Lvalue (N) then
8440 Generate_Reference (Entity (S), S, 'm');
8442 Generate_Reference (Entity (S), S, 'r');
8445 -- If prefix is an access type, the node will be transformed into an
8446 -- explicit dereference during expansion. The type of the node is the
8447 -- designated type of that of the prefix.
8449 if Is_Access_Type (Etype (P)) then
8450 T := Designated_Type (Etype (P));
8451 Check_Fully_Declared_Prefix (T, P);
8456 if Has_Discriminants (T)
8457 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8458 and then Present (Original_Record_Component (Entity (S)))
8459 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8460 and then Present (Discriminant_Checking_Func
8461 (Original_Record_Component (Entity (S))))
8462 and then not Discriminant_Checks_Suppressed (T)
8463 and then not Init_Component
8465 Set_Do_Discriminant_Check (N);
8468 if Ekind (Entity (S)) = E_Void then
8469 Error_Msg_N ("premature use of component", S);
8472 -- If the prefix is a record conversion, this may be a renamed
8473 -- discriminant whose bounds differ from those of the original
8474 -- one, so we must ensure that a range check is performed.
8476 if Nkind (P) = N_Type_Conversion
8477 and then Ekind (Entity (S)) = E_Discriminant
8478 and then Is_Discrete_Type (Typ)
8480 Set_Etype (N, Base_Type (Typ));
8483 -- Note: No Eval processing is required, because the prefix is of a
8484 -- record type, or protected type, and neither can possibly be static.
8486 -- If the array type is atomic, and is packed, and we are in a left side
8487 -- context, then this is worth a warning, since we have a situation
8488 -- where the access to the component may cause extra read/writes of the
8489 -- atomic array object, which could be considered unexpected.
8491 if Nkind (N) = N_Selected_Component
8492 and then (Is_Atomic (T)
8493 or else (Is_Entity_Name (Prefix (N))
8494 and then Is_Atomic (Entity (Prefix (N)))))
8495 and then Is_Packed (T)
8498 Error_Msg_N ("?assignment to component of packed atomic record",
8500 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8503 end Resolve_Selected_Component;
8509 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8510 B_Typ : constant Entity_Id := Base_Type (Typ);
8511 L : constant Node_Id := Left_Opnd (N);
8512 R : constant Node_Id := Right_Opnd (N);
8515 -- We do the resolution using the base type, because intermediate values
8516 -- in expressions always are of the base type, not a subtype of it.
8519 Resolve (R, Standard_Natural);
8521 Check_Unset_Reference (L);
8522 Check_Unset_Reference (R);
8524 Set_Etype (N, B_Typ);
8525 Generate_Operator_Reference (N, B_Typ);
8529 ---------------------------
8530 -- Resolve_Short_Circuit --
8531 ---------------------------
8533 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8534 B_Typ : constant Entity_Id := Base_Type (Typ);
8535 L : constant Node_Id := Left_Opnd (N);
8536 R : constant Node_Id := Right_Opnd (N);
8542 -- Check for issuing warning for always False assert/check, this happens
8543 -- when assertions are turned off, in which case the pragma Assert/Check
8544 -- was transformed into:
8546 -- if False and then <condition> then ...
8548 -- and we detect this pattern
8550 if Warn_On_Assertion_Failure
8551 and then Is_Entity_Name (R)
8552 and then Entity (R) = Standard_False
8553 and then Nkind (Parent (N)) = N_If_Statement
8554 and then Nkind (N) = N_And_Then
8555 and then Is_Entity_Name (L)
8556 and then Entity (L) = Standard_False
8559 Orig : constant Node_Id := Original_Node (Parent (N));
8562 if Nkind (Orig) = N_Pragma
8563 and then Pragma_Name (Orig) = Name_Assert
8565 -- Don't want to warn if original condition is explicit False
8568 Expr : constant Node_Id :=
8571 (First (Pragma_Argument_Associations (Orig))));
8573 if Is_Entity_Name (Expr)
8574 and then Entity (Expr) = Standard_False
8578 -- Issue warning. We do not want the deletion of the
8579 -- IF/AND-THEN to take this message with it. We achieve
8580 -- this by making sure that the expanded code points to
8581 -- the Sloc of the expression, not the original pragma.
8584 ("?assertion would fail at run time!",
8586 (First (Pragma_Argument_Associations (Orig))));
8590 -- Similar processing for Check pragma
8592 elsif Nkind (Orig) = N_Pragma
8593 and then Pragma_Name (Orig) = Name_Check
8595 -- Don't want to warn if original condition is explicit False
8598 Expr : constant Node_Id :=
8602 (Pragma_Argument_Associations (Orig)))));
8604 if Is_Entity_Name (Expr)
8605 and then Entity (Expr) = Standard_False
8610 ("?check would fail at run time!",
8612 (Last (Pragma_Argument_Associations (Orig))));
8619 -- Continue with processing of short circuit
8621 Check_Unset_Reference (L);
8622 Check_Unset_Reference (R);
8624 Set_Etype (N, B_Typ);
8625 Eval_Short_Circuit (N);
8626 end Resolve_Short_Circuit;
8632 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8633 Name : constant Node_Id := Prefix (N);
8634 Drange : constant Node_Id := Discrete_Range (N);
8635 Array_Type : Entity_Id := Empty;
8639 if Is_Overloaded (Name) then
8641 -- Use the context type to select the prefix that yields the correct
8646 I1 : Interp_Index := 0;
8648 P : constant Node_Id := Prefix (N);
8649 Found : Boolean := False;
8652 Get_First_Interp (P, I, It);
8653 while Present (It.Typ) loop
8654 if (Is_Array_Type (It.Typ)
8655 and then Covers (Typ, It.Typ))
8656 or else (Is_Access_Type (It.Typ)
8657 and then Is_Array_Type (Designated_Type (It.Typ))
8658 and then Covers (Typ, Designated_Type (It.Typ)))
8661 It := Disambiguate (P, I1, I, Any_Type);
8663 if It = No_Interp then
8664 Error_Msg_N ("ambiguous prefix for slicing", N);
8669 Array_Type := It.Typ;
8674 Array_Type := It.Typ;
8679 Get_Next_Interp (I, It);
8684 Array_Type := Etype (Name);
8687 Resolve (Name, Array_Type);
8689 if Is_Access_Type (Array_Type) then
8690 Apply_Access_Check (N);
8691 Array_Type := Designated_Type (Array_Type);
8693 -- If the prefix is an access to an unconstrained array, we must use
8694 -- the actual subtype of the object to perform the index checks. The
8695 -- object denoted by the prefix is implicit in the node, so we build
8696 -- an explicit representation for it in order to compute the actual
8699 if not Is_Constrained (Array_Type) then
8700 Remove_Side_Effects (Prefix (N));
8703 Obj : constant Node_Id :=
8704 Make_Explicit_Dereference (Sloc (N),
8705 Prefix => New_Copy_Tree (Prefix (N)));
8707 Set_Etype (Obj, Array_Type);
8708 Set_Parent (Obj, Parent (N));
8709 Array_Type := Get_Actual_Subtype (Obj);
8713 elsif Is_Entity_Name (Name)
8714 or else Nkind (Name) = N_Explicit_Dereference
8715 or else (Nkind (Name) = N_Function_Call
8716 and then not Is_Constrained (Etype (Name)))
8718 Array_Type := Get_Actual_Subtype (Name);
8720 -- If the name is a selected component that depends on discriminants,
8721 -- build an actual subtype for it. This can happen only when the name
8722 -- itself is overloaded; otherwise the actual subtype is created when
8723 -- the selected component is analyzed.
8725 elsif Nkind (Name) = N_Selected_Component
8726 and then Full_Analysis
8727 and then Depends_On_Discriminant (First_Index (Array_Type))
8730 Act_Decl : constant Node_Id :=
8731 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8733 Insert_Action (N, Act_Decl);
8734 Array_Type := Defining_Identifier (Act_Decl);
8737 -- Maybe this should just be "else", instead of checking for the
8738 -- specific case of slice??? This is needed for the case where the
8739 -- prefix is an Image attribute, which gets expanded to a slice, and so
8740 -- has a constrained subtype which we want to use for the slice range
8741 -- check applied below (the range check won't get done if the
8742 -- unconstrained subtype of the 'Image is used).
8744 elsif Nkind (Name) = N_Slice then
8745 Array_Type := Etype (Name);
8748 -- If name was overloaded, set slice type correctly now
8750 Set_Etype (N, Array_Type);
8752 -- If the range is specified by a subtype mark, no resolution is
8753 -- necessary. Else resolve the bounds, and apply needed checks.
8755 if not Is_Entity_Name (Drange) then
8756 Index := First_Index (Array_Type);
8757 Resolve (Drange, Base_Type (Etype (Index)));
8759 if Nkind (Drange) = N_Range then
8761 -- Ensure that side effects in the bounds are properly handled
8763 Force_Evaluation (Low_Bound (Drange));
8764 Force_Evaluation (High_Bound (Drange));
8766 -- Do not apply the range check to nodes associated with the
8767 -- frontend expansion of the dispatch table. We first check
8768 -- if Ada.Tags is already loaded to avoid the addition of an
8769 -- undesired dependence on such run-time unit.
8771 if not Tagged_Type_Expansion
8773 (RTU_Loaded (Ada_Tags)
8774 and then Nkind (Prefix (N)) = N_Selected_Component
8775 and then Present (Entity (Selector_Name (Prefix (N))))
8776 and then Entity (Selector_Name (Prefix (N))) =
8777 RTE_Record_Component (RE_Prims_Ptr))
8779 Apply_Range_Check (Drange, Etype (Index));
8784 Set_Slice_Subtype (N);
8786 -- Check bad use of type with predicates
8788 if Has_Predicates (Etype (Drange)) then
8789 Bad_Predicated_Subtype_Use
8790 ("subtype& has predicate, not allowed in slice",
8791 Drange, Etype (Drange));
8793 -- Otherwise here is where we check suspicious indexes
8795 elsif Nkind (Drange) = N_Range then
8796 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8797 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8803 ----------------------------
8804 -- Resolve_String_Literal --
8805 ----------------------------
8807 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8808 C_Typ : constant Entity_Id := Component_Type (Typ);
8809 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8810 Loc : constant Source_Ptr := Sloc (N);
8811 Str : constant String_Id := Strval (N);
8812 Strlen : constant Nat := String_Length (Str);
8813 Subtype_Id : Entity_Id;
8814 Need_Check : Boolean;
8817 -- For a string appearing in a concatenation, defer creation of the
8818 -- string_literal_subtype until the end of the resolution of the
8819 -- concatenation, because the literal may be constant-folded away. This
8820 -- is a useful optimization for long concatenation expressions.
8822 -- If the string is an aggregate built for a single character (which
8823 -- happens in a non-static context) or a is null string to which special
8824 -- checks may apply, we build the subtype. Wide strings must also get a
8825 -- string subtype if they come from a one character aggregate. Strings
8826 -- generated by attributes might be static, but it is often hard to
8827 -- determine whether the enclosing context is static, so we generate
8828 -- subtypes for them as well, thus losing some rarer optimizations ???
8829 -- Same for strings that come from a static conversion.
8832 (Strlen = 0 and then Typ /= Standard_String)
8833 or else Nkind (Parent (N)) /= N_Op_Concat
8834 or else (N /= Left_Opnd (Parent (N))
8835 and then N /= Right_Opnd (Parent (N)))
8836 or else ((Typ = Standard_Wide_String
8837 or else Typ = Standard_Wide_Wide_String)
8838 and then Nkind (Original_Node (N)) /= N_String_Literal);
8840 -- If the resolving type is itself a string literal subtype, we can just
8841 -- reuse it, since there is no point in creating another.
8843 if Ekind (Typ) = E_String_Literal_Subtype then
8846 elsif Nkind (Parent (N)) = N_Op_Concat
8847 and then not Need_Check
8848 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8849 N_Attribute_Reference,
8850 N_Qualified_Expression,
8855 -- Otherwise we must create a string literal subtype. Note that the
8856 -- whole idea of string literal subtypes is simply to avoid the need
8857 -- for building a full fledged array subtype for each literal.
8860 Set_String_Literal_Subtype (N, Typ);
8861 Subtype_Id := Etype (N);
8864 if Nkind (Parent (N)) /= N_Op_Concat
8867 Set_Etype (N, Subtype_Id);
8868 Eval_String_Literal (N);
8871 if Is_Limited_Composite (Typ)
8872 or else Is_Private_Composite (Typ)
8874 Error_Msg_N ("string literal not available for private array", N);
8875 Set_Etype (N, Any_Type);
8879 -- The validity of a null string has been checked in the call to
8880 -- Eval_String_Literal.
8885 -- Always accept string literal with component type Any_Character, which
8886 -- occurs in error situations and in comparisons of literals, both of
8887 -- which should accept all literals.
8889 elsif R_Typ = Any_Character then
8892 -- If the type is bit-packed, then we always transform the string
8893 -- literal into a full fledged aggregate.
8895 elsif Is_Bit_Packed_Array (Typ) then
8898 -- Deal with cases of Wide_Wide_String, Wide_String, and String
8901 -- For Standard.Wide_Wide_String, or any other type whose component
8902 -- type is Standard.Wide_Wide_Character, we know that all the
8903 -- characters in the string must be acceptable, since the parser
8904 -- accepted the characters as valid character literals.
8906 if R_Typ = Standard_Wide_Wide_Character then
8909 -- For the case of Standard.String, or any other type whose component
8910 -- type is Standard.Character, we must make sure that there are no
8911 -- wide characters in the string, i.e. that it is entirely composed
8912 -- of characters in range of type Character.
8914 -- If the string literal is the result of a static concatenation, the
8915 -- test has already been performed on the components, and need not be
8918 elsif R_Typ = Standard_Character
8919 and then Nkind (Original_Node (N)) /= N_Op_Concat
8921 for J in 1 .. Strlen loop
8922 if not In_Character_Range (Get_String_Char (Str, J)) then
8924 -- If we are out of range, post error. This is one of the
8925 -- very few places that we place the flag in the middle of
8926 -- a token, right under the offending wide character. Not
8927 -- quite clear if this is right wrt wide character encoding
8928 -- sequences, but it's only an error message!
8931 ("literal out of range of type Standard.Character",
8932 Source_Ptr (Int (Loc) + J));
8937 -- For the case of Standard.Wide_String, or any other type whose
8938 -- component type is Standard.Wide_Character, we must make sure that
8939 -- there are no wide characters in the string, i.e. that it is
8940 -- entirely composed of characters in range of type Wide_Character.
8942 -- If the string literal is the result of a static concatenation,
8943 -- the test has already been performed on the components, and need
8946 elsif R_Typ = Standard_Wide_Character
8947 and then Nkind (Original_Node (N)) /= N_Op_Concat
8949 for J in 1 .. Strlen loop
8950 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8952 -- If we are out of range, post error. This is one of the
8953 -- very few places that we place the flag in the middle of
8954 -- a token, right under the offending wide character.
8956 -- This is not quite right, because characters in general
8957 -- will take more than one character position ???
8960 ("literal out of range of type Standard.Wide_Character",
8961 Source_Ptr (Int (Loc) + J));
8966 -- If the root type is not a standard character, then we will convert
8967 -- the string into an aggregate and will let the aggregate code do
8968 -- the checking. Standard Wide_Wide_Character is also OK here.
8974 -- See if the component type of the array corresponding to the string
8975 -- has compile time known bounds. If yes we can directly check
8976 -- whether the evaluation of the string will raise constraint error.
8977 -- Otherwise we need to transform the string literal into the
8978 -- corresponding character aggregate and let the aggregate code do
8981 if Is_Standard_Character_Type (R_Typ) then
8983 -- Check for the case of full range, where we are definitely OK
8985 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8989 -- Here the range is not the complete base type range, so check
8992 Comp_Typ_Lo : constant Node_Id :=
8993 Type_Low_Bound (Component_Type (Typ));
8994 Comp_Typ_Hi : constant Node_Id :=
8995 Type_High_Bound (Component_Type (Typ));
9000 if Compile_Time_Known_Value (Comp_Typ_Lo)
9001 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9003 for J in 1 .. Strlen loop
9004 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9006 if Char_Val < Expr_Value (Comp_Typ_Lo)
9007 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9009 Apply_Compile_Time_Constraint_Error
9010 (N, "character out of range?", CE_Range_Check_Failed,
9011 Loc => Source_Ptr (Int (Loc) + J));
9021 -- If we got here we meed to transform the string literal into the
9022 -- equivalent qualified positional array aggregate. This is rather
9023 -- heavy artillery for this situation, but it is hard work to avoid.
9026 Lits : constant List_Id := New_List;
9027 P : Source_Ptr := Loc + 1;
9031 -- Build the character literals, we give them source locations that
9032 -- correspond to the string positions, which is a bit tricky given
9033 -- the possible presence of wide character escape sequences.
9035 for J in 1 .. Strlen loop
9036 C := Get_String_Char (Str, J);
9037 Set_Character_Literal_Name (C);
9040 Make_Character_Literal (P,
9042 Char_Literal_Value => UI_From_CC (C)));
9044 if In_Character_Range (C) then
9047 -- Should we have a call to Skip_Wide here ???
9056 Make_Qualified_Expression (Loc,
9057 Subtype_Mark => New_Reference_To (Typ, Loc),
9059 Make_Aggregate (Loc, Expressions => Lits)));
9061 Analyze_And_Resolve (N, Typ);
9063 end Resolve_String_Literal;
9065 -----------------------------
9066 -- Resolve_Subprogram_Info --
9067 -----------------------------
9069 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9072 end Resolve_Subprogram_Info;
9074 -----------------------------
9075 -- Resolve_Type_Conversion --
9076 -----------------------------
9078 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9079 Conv_OK : constant Boolean := Conversion_OK (N);
9080 Operand : constant Node_Id := Expression (N);
9081 Operand_Typ : constant Entity_Id := Etype (Operand);
9082 Target_Typ : constant Entity_Id := Etype (N);
9087 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9088 -- Set to False to suppress cases where we want to suppress the test
9089 -- for redundancy to avoid possible false positives on this warning.
9093 and then not Valid_Conversion (N, Target_Typ, Operand)
9098 -- If the Operand Etype is Universal_Fixed, then the conversion is
9099 -- never redundant. We need this check because by the time we have
9100 -- finished the rather complex transformation, the conversion looks
9101 -- redundant when it is not.
9103 if Operand_Typ = Universal_Fixed then
9104 Test_Redundant := False;
9106 -- If the operand is marked as Any_Fixed, then special processing is
9107 -- required. This is also a case where we suppress the test for a
9108 -- redundant conversion, since most certainly it is not redundant.
9110 elsif Operand_Typ = Any_Fixed then
9111 Test_Redundant := False;
9113 -- Mixed-mode operation involving a literal. Context must be a fixed
9114 -- type which is applied to the literal subsequently.
9116 if Is_Fixed_Point_Type (Typ) then
9117 Set_Etype (Operand, Universal_Real);
9119 elsif Is_Numeric_Type (Typ)
9120 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9121 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9123 Etype (Left_Opnd (Operand)) = Universal_Real)
9125 -- Return if expression is ambiguous
9127 if Unique_Fixed_Point_Type (N) = Any_Type then
9130 -- If nothing else, the available fixed type is Duration
9133 Set_Etype (Operand, Standard_Duration);
9136 -- Resolve the real operand with largest available precision
9138 if Etype (Right_Opnd (Operand)) = Universal_Real then
9139 Rop := New_Copy_Tree (Right_Opnd (Operand));
9141 Rop := New_Copy_Tree (Left_Opnd (Operand));
9144 Resolve (Rop, Universal_Real);
9146 -- If the operand is a literal (it could be a non-static and
9147 -- illegal exponentiation) check whether the use of Duration
9148 -- is potentially inaccurate.
9150 if Nkind (Rop) = N_Real_Literal
9151 and then Realval (Rop) /= Ureal_0
9152 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9155 ("?universal real operand can only " &
9156 "be interpreted as Duration!",
9159 ("\?precision will be lost in the conversion!", Rop);
9162 elsif Is_Numeric_Type (Typ)
9163 and then Nkind (Operand) in N_Op
9164 and then Unique_Fixed_Point_Type (N) /= Any_Type
9166 Set_Etype (Operand, Standard_Duration);
9169 Error_Msg_N ("invalid context for mixed mode operation", N);
9170 Set_Etype (Operand, Any_Type);
9177 -- In SPARK, a type conversion between array types should be restricted
9178 -- to types which have matching static bounds.
9180 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9181 -- operation if not needed.
9183 if Restriction_Check_Required (SPARK)
9184 and then Is_Array_Type (Target_Typ)
9185 and then Is_Array_Type (Operand_Typ)
9186 and then Operand_Typ /= Any_Composite -- or else Operand in error
9187 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9189 Check_SPARK_Restriction
9190 ("array types should have matching static bounds", N);
9193 -- In formal mode, the operand of an ancestor type conversion must be an
9194 -- object (not an expression).
9196 if Is_Tagged_Type (Target_Typ)
9197 and then not Is_Class_Wide_Type (Target_Typ)
9198 and then Is_Tagged_Type (Operand_Typ)
9199 and then not Is_Class_Wide_Type (Operand_Typ)
9200 and then Is_Ancestor (Target_Typ, Operand_Typ)
9201 and then not Is_SPARK_Object_Reference (Operand)
9203 Check_SPARK_Restriction ("object required", Operand);
9206 -- Note: we do the Eval_Type_Conversion call before applying the
9207 -- required checks for a subtype conversion. This is important, since
9208 -- both are prepared under certain circumstances to change the type
9209 -- conversion to a constraint error node, but in the case of
9210 -- Eval_Type_Conversion this may reflect an illegality in the static
9211 -- case, and we would miss the illegality (getting only a warning
9212 -- message), if we applied the type conversion checks first.
9214 Eval_Type_Conversion (N);
9216 -- Even when evaluation is not possible, we may be able to simplify the
9217 -- conversion or its expression. This needs to be done before applying
9218 -- checks, since otherwise the checks may use the original expression
9219 -- and defeat the simplifications. This is specifically the case for
9220 -- elimination of the floating-point Truncation attribute in
9221 -- float-to-int conversions.
9223 Simplify_Type_Conversion (N);
9225 -- If after evaluation we still have a type conversion, then we may need
9226 -- to apply checks required for a subtype conversion.
9228 -- Skip these type conversion checks if universal fixed operands
9229 -- operands involved, since range checks are handled separately for
9230 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9232 if Nkind (N) = N_Type_Conversion
9233 and then not Is_Generic_Type (Root_Type (Target_Typ))
9234 and then Target_Typ /= Universal_Fixed
9235 and then Operand_Typ /= Universal_Fixed
9237 Apply_Type_Conversion_Checks (N);
9240 -- Issue warning for conversion of simple object to its own type. We
9241 -- have to test the original nodes, since they may have been rewritten
9242 -- by various optimizations.
9244 Orig_N := Original_Node (N);
9246 -- Here we test for a redundant conversion if the warning mode is
9247 -- active (and was not locally reset), and we have a type conversion
9248 -- from source not appearing in a generic instance.
9251 and then Nkind (Orig_N) = N_Type_Conversion
9252 and then Comes_From_Source (Orig_N)
9253 and then not In_Instance
9255 Orig_N := Original_Node (Expression (Orig_N));
9256 Orig_T := Target_Typ;
9258 -- If the node is part of a larger expression, the Target_Type
9259 -- may not be the original type of the node if the context is a
9260 -- condition. Recover original type to see if conversion is needed.
9262 if Is_Boolean_Type (Orig_T)
9263 and then Nkind (Parent (N)) in N_Op
9265 Orig_T := Etype (Parent (N));
9268 -- If we have an entity name, then give the warning if the entity
9269 -- is the right type, or if it is a loop parameter covered by the
9270 -- original type (that's needed because loop parameters have an
9271 -- odd subtype coming from the bounds).
9273 if (Is_Entity_Name (Orig_N)
9275 (Etype (Entity (Orig_N)) = Orig_T
9277 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9278 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9280 -- If not an entity, then type of expression must match
9282 or else Etype (Orig_N) = Orig_T
9284 -- One more check, do not give warning if the analyzed conversion
9285 -- has an expression with non-static bounds, and the bounds of the
9286 -- target are static. This avoids junk warnings in cases where the
9287 -- conversion is necessary to establish staticness, for example in
9288 -- a case statement.
9290 if not Is_OK_Static_Subtype (Operand_Typ)
9291 and then Is_OK_Static_Subtype (Target_Typ)
9295 -- Finally, if this type conversion occurs in a context requiring
9296 -- a prefix, and the expression is a qualified expression then the
9297 -- type conversion is not redundant, since a qualified expression
9298 -- is not a prefix, whereas a type conversion is. For example, "X
9299 -- := T'(Funx(...)).Y;" is illegal because a selected component
9300 -- requires a prefix, but a type conversion makes it legal: "X :=
9301 -- T(T'(Funx(...))).Y;"
9303 -- In Ada 2012, a qualified expression is a name, so this idiom is
9304 -- no longer needed, but we still suppress the warning because it
9305 -- seems unfriendly for warnings to pop up when you switch to the
9306 -- newer language version.
9308 elsif Nkind (Orig_N) = N_Qualified_Expression
9309 and then Nkind_In (Parent (N), N_Attribute_Reference,
9310 N_Indexed_Component,
9311 N_Selected_Component,
9313 N_Explicit_Dereference)
9317 -- Here we give the redundant conversion warning. If it is an
9318 -- entity, give the name of the entity in the message. If not,
9319 -- just mention the expression.
9322 if Is_Entity_Name (Orig_N) then
9323 Error_Msg_Node_2 := Orig_T;
9324 Error_Msg_NE -- CODEFIX
9325 ("?redundant conversion, & is of type &!",
9326 N, Entity (Orig_N));
9329 ("?redundant conversion, expression is of type&!",
9336 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9337 -- No need to perform any interface conversion if the type of the
9338 -- expression coincides with the target type.
9340 if Ada_Version >= Ada_2005
9341 and then Expander_Active
9342 and then Operand_Typ /= Target_Typ
9345 Opnd : Entity_Id := Operand_Typ;
9346 Target : Entity_Id := Target_Typ;
9349 if Is_Access_Type (Opnd) then
9350 Opnd := Designated_Type (Opnd);
9353 if Is_Access_Type (Target_Typ) then
9354 Target := Designated_Type (Target);
9357 if Opnd = Target then
9360 -- Conversion from interface type
9362 elsif Is_Interface (Opnd) then
9364 -- Ada 2005 (AI-217): Handle entities from limited views
9366 if From_With_Type (Opnd) then
9367 Error_Msg_Qual_Level := 99;
9368 Error_Msg_NE -- CODEFIX
9369 ("missing WITH clause on package &", N,
9370 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9372 ("type conversions require visibility of the full view",
9375 elsif From_With_Type (Target)
9377 (Is_Access_Type (Target_Typ)
9378 and then Present (Non_Limited_View (Etype (Target))))
9380 Error_Msg_Qual_Level := 99;
9381 Error_Msg_NE -- CODEFIX
9382 ("missing WITH clause on package &", N,
9383 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9385 ("type conversions require visibility of the full view",
9389 Expand_Interface_Conversion (N, Is_Static => False);
9392 -- Conversion to interface type
9394 elsif Is_Interface (Target) then
9398 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9399 Opnd := Etype (Opnd);
9402 if not Interface_Present_In_Ancestor
9406 if Is_Class_Wide_Type (Opnd) then
9408 -- The static analysis is not enough to know if the
9409 -- interface is implemented or not. Hence we must pass
9410 -- the work to the expander to generate code to evaluate
9411 -- the conversion at run time.
9413 Expand_Interface_Conversion (N, Is_Static => False);
9416 Error_Msg_Name_1 := Chars (Etype (Target));
9417 Error_Msg_Name_2 := Chars (Opnd);
9419 ("wrong interface conversion (% is not a progenitor " &
9424 Expand_Interface_Conversion (N);
9429 end Resolve_Type_Conversion;
9431 ----------------------
9432 -- Resolve_Unary_Op --
9433 ----------------------
9435 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9436 B_Typ : constant Entity_Id := Base_Type (Typ);
9437 R : constant Node_Id := Right_Opnd (N);
9443 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9444 Error_Msg_Name_1 := Chars (Typ);
9445 Check_SPARK_Restriction
9446 ("unary operator not defined for modular type%", N);
9449 -- Deal with intrinsic unary operators
9451 if Comes_From_Source (N)
9452 and then Ekind (Entity (N)) = E_Function
9453 and then Is_Imported (Entity (N))
9454 and then Is_Intrinsic_Subprogram (Entity (N))
9456 Resolve_Intrinsic_Unary_Operator (N, Typ);
9460 -- Deal with universal cases
9462 if Etype (R) = Universal_Integer
9464 Etype (R) = Universal_Real
9466 Check_For_Visible_Operator (N, B_Typ);
9469 Set_Etype (N, B_Typ);
9472 -- Generate warning for expressions like abs (x mod 2)
9474 if Warn_On_Redundant_Constructs
9475 and then Nkind (N) = N_Op_Abs
9477 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9479 if OK and then Hi >= Lo and then Lo >= 0 then
9480 Error_Msg_N -- CODEFIX
9481 ("?abs applied to known non-negative value has no effect", N);
9485 -- Deal with reference generation
9487 Check_Unset_Reference (R);
9488 Generate_Operator_Reference (N, B_Typ);
9491 -- Set overflow checking bit. Much cleverer code needed here eventually
9492 -- and perhaps the Resolve routines should be separated for the various
9493 -- arithmetic operations, since they will need different processing ???
9495 if Nkind (N) in N_Op then
9496 if not Overflow_Checks_Suppressed (Etype (N)) then
9497 Enable_Overflow_Check (N);
9501 -- Generate warning for expressions like -5 mod 3 for integers. No need
9502 -- to worry in the floating-point case, since parens do not affect the
9503 -- result so there is no point in giving in a warning.
9506 Norig : constant Node_Id := Original_Node (N);
9515 if Warn_On_Questionable_Missing_Parens
9516 and then Comes_From_Source (Norig)
9517 and then Is_Integer_Type (Typ)
9518 and then Nkind (Norig) = N_Op_Minus
9520 Rorig := Original_Node (Right_Opnd (Norig));
9522 -- We are looking for cases where the right operand is not
9523 -- parenthesized, and is a binary operator, multiply, divide, or
9524 -- mod. These are the cases where the grouping can affect results.
9526 if Paren_Count (Rorig) = 0
9527 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9529 -- For mod, we always give the warning, since the value is
9530 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9531 -- -(5 mod 315)). But for the other cases, the only concern is
9532 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9533 -- overflows, but (-2) * 64 does not). So we try to give the
9534 -- message only when overflow is possible.
9536 if Nkind (Rorig) /= N_Op_Mod
9537 and then Compile_Time_Known_Value (R)
9539 Val := Expr_Value (R);
9541 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9542 HB := Expr_Value (Type_High_Bound (Typ));
9544 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9547 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9548 LB := Expr_Value (Type_Low_Bound (Typ));
9550 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9553 -- Note that the test below is deliberately excluding the
9554 -- largest negative number, since that is a potentially
9555 -- troublesome case (e.g. -2 * x, where the result is the
9556 -- largest negative integer has an overflow with 2 * x).
9558 if Val > LB and then Val <= HB then
9563 -- For the multiplication case, the only case we have to worry
9564 -- about is when (-a)*b is exactly the largest negative number
9565 -- so that -(a*b) can cause overflow. This can only happen if
9566 -- a is a power of 2, and more generally if any operand is a
9567 -- constant that is not a power of 2, then the parentheses
9568 -- cannot affect whether overflow occurs. We only bother to
9569 -- test the left most operand
9571 -- Loop looking at left operands for one that has known value
9574 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9575 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9576 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9578 -- Operand value of 0 or 1 skips warning
9583 -- Otherwise check power of 2, if power of 2, warn, if
9584 -- anything else, skip warning.
9587 while Lval /= 2 loop
9588 if Lval mod 2 = 1 then
9599 -- Keep looking at left operands
9601 Opnd := Left_Opnd (Opnd);
9604 -- For rem or "/" we can only have a problematic situation
9605 -- if the divisor has a value of minus one or one. Otherwise
9606 -- overflow is impossible (divisor > 1) or we have a case of
9607 -- division by zero in any case.
9609 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9610 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9611 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9616 -- If we fall through warning should be issued
9619 ("?unary minus expression should be parenthesized here!", N);
9623 end Resolve_Unary_Op;
9625 ----------------------------------
9626 -- Resolve_Unchecked_Expression --
9627 ----------------------------------
9629 procedure Resolve_Unchecked_Expression
9634 Resolve (Expression (N), Typ, Suppress => All_Checks);
9636 end Resolve_Unchecked_Expression;
9638 ---------------------------------------
9639 -- Resolve_Unchecked_Type_Conversion --
9640 ---------------------------------------
9642 procedure Resolve_Unchecked_Type_Conversion
9646 pragma Warnings (Off, Typ);
9648 Operand : constant Node_Id := Expression (N);
9649 Opnd_Type : constant Entity_Id := Etype (Operand);
9652 -- Resolve operand using its own type
9654 Resolve (Operand, Opnd_Type);
9655 Eval_Unchecked_Conversion (N);
9656 end Resolve_Unchecked_Type_Conversion;
9658 ------------------------------
9659 -- Rewrite_Operator_As_Call --
9660 ------------------------------
9662 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9663 Loc : constant Source_Ptr := Sloc (N);
9664 Actuals : constant List_Id := New_List;
9668 if Nkind (N) in N_Binary_Op then
9669 Append (Left_Opnd (N), Actuals);
9672 Append (Right_Opnd (N), Actuals);
9675 Make_Function_Call (Sloc => Loc,
9676 Name => New_Occurrence_Of (Nam, Loc),
9677 Parameter_Associations => Actuals);
9679 Preserve_Comes_From_Source (New_N, N);
9680 Preserve_Comes_From_Source (Name (New_N), N);
9682 Set_Etype (N, Etype (Nam));
9683 end Rewrite_Operator_As_Call;
9685 ------------------------------
9686 -- Rewrite_Renamed_Operator --
9687 ------------------------------
9689 procedure Rewrite_Renamed_Operator
9694 Nam : constant Name_Id := Chars (Op);
9695 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9699 -- Rewrite the operator node using the real operator, not its renaming.
9700 -- Exclude user-defined intrinsic operations of the same name, which are
9701 -- treated separately and rewritten as calls.
9703 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9704 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9705 Set_Chars (Op_Node, Nam);
9706 Set_Etype (Op_Node, Etype (N));
9707 Set_Entity (Op_Node, Op);
9708 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9710 -- Indicate that both the original entity and its renaming are
9711 -- referenced at this point.
9713 Generate_Reference (Entity (N), N);
9714 Generate_Reference (Op, N);
9717 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9720 Rewrite (N, Op_Node);
9722 -- If the context type is private, add the appropriate conversions so
9723 -- that the operator is applied to the full view. This is done in the
9724 -- routines that resolve intrinsic operators.
9726 if Is_Intrinsic_Subprogram (Op)
9727 and then Is_Private_Type (Typ)
9730 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9731 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9732 Resolve_Intrinsic_Operator (N, Typ);
9734 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9735 Resolve_Intrinsic_Unary_Operator (N, Typ);
9742 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9744 -- Operator renames a user-defined operator of the same name. Use the
9745 -- original operator in the node, which is the one Gigi knows about.
9748 Set_Is_Overloaded (N, False);
9750 end Rewrite_Renamed_Operator;
9752 -----------------------
9753 -- Set_Slice_Subtype --
9754 -----------------------
9756 -- Build an implicit subtype declaration to represent the type delivered by
9757 -- the slice. This is an abbreviated version of an array subtype. We define
9758 -- an index subtype for the slice, using either the subtype name or the
9759 -- discrete range of the slice. To be consistent with index usage elsewhere
9760 -- we create a list header to hold the single index. This list is not
9761 -- otherwise attached to the syntax tree.
9763 procedure Set_Slice_Subtype (N : Node_Id) is
9764 Loc : constant Source_Ptr := Sloc (N);
9765 Index_List : constant List_Id := New_List;
9767 Index_Subtype : Entity_Id;
9768 Index_Type : Entity_Id;
9769 Slice_Subtype : Entity_Id;
9770 Drange : constant Node_Id := Discrete_Range (N);
9773 if Is_Entity_Name (Drange) then
9774 Index_Subtype := Entity (Drange);
9777 -- We force the evaluation of a range. This is definitely needed in
9778 -- the renamed case, and seems safer to do unconditionally. Note in
9779 -- any case that since we will create and insert an Itype referring
9780 -- to this range, we must make sure any side effect removal actions
9781 -- are inserted before the Itype definition.
9783 if Nkind (Drange) = N_Range then
9784 Force_Evaluation (Low_Bound (Drange));
9785 Force_Evaluation (High_Bound (Drange));
9788 Index_Type := Base_Type (Etype (Drange));
9790 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9792 -- Take a new copy of Drange (where bounds have been rewritten to
9793 -- reference side-effect-free names). Using a separate tree ensures
9794 -- that further expansion (e.g. while rewriting a slice assignment
9795 -- into a FOR loop) does not attempt to remove side effects on the
9796 -- bounds again (which would cause the bounds in the index subtype
9797 -- definition to refer to temporaries before they are defined) (the
9798 -- reason is that some names are considered side effect free here
9799 -- for the subtype, but not in the context of a loop iteration
9802 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9803 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
9804 Set_Etype (Index_Subtype, Index_Type);
9805 Set_Size_Info (Index_Subtype, Index_Type);
9806 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9809 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9811 Index := New_Occurrence_Of (Index_Subtype, Loc);
9812 Set_Etype (Index, Index_Subtype);
9813 Append (Index, Index_List);
9815 Set_First_Index (Slice_Subtype, Index);
9816 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9817 Set_Is_Constrained (Slice_Subtype, True);
9819 Check_Compile_Time_Size (Slice_Subtype);
9821 -- The Etype of the existing Slice node is reset to this slice subtype.
9822 -- Its bounds are obtained from its first index.
9824 Set_Etype (N, Slice_Subtype);
9826 -- For packed slice subtypes, freeze immediately (except in the case of
9827 -- being in a "spec expression" where we never freeze when we first see
9830 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9831 Freeze_Itype (Slice_Subtype, N);
9833 -- For all other cases insert an itype reference in the slice's actions
9834 -- so that the itype is frozen at the proper place in the tree (i.e. at
9835 -- the point where actions for the slice are analyzed). Note that this
9836 -- is different from freezing the itype immediately, which might be
9837 -- premature (e.g. if the slice is within a transient scope). This needs
9838 -- to be done only if expansion is enabled.
9840 elsif Expander_Active then
9841 Ensure_Defined (Typ => Slice_Subtype, N => N);
9843 end Set_Slice_Subtype;
9845 --------------------------------
9846 -- Set_String_Literal_Subtype --
9847 --------------------------------
9849 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9850 Loc : constant Source_Ptr := Sloc (N);
9851 Low_Bound : constant Node_Id :=
9852 Type_Low_Bound (Etype (First_Index (Typ)));
9853 Subtype_Id : Entity_Id;
9856 if Nkind (N) /= N_String_Literal then
9860 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
9861 Set_String_Literal_Length (Subtype_Id, UI_From_Int
9862 (String_Length (Strval (N))));
9863 Set_Etype (Subtype_Id, Base_Type (Typ));
9864 Set_Is_Constrained (Subtype_Id);
9865 Set_Etype (N, Subtype_Id);
9867 if Is_OK_Static_Expression (Low_Bound) then
9869 -- The low bound is set from the low bound of the corresponding index
9870 -- type. Note that we do not store the high bound in the string literal
9871 -- subtype, but it can be deduced if necessary from the length and the
9874 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
9877 -- If the lower bound is not static we create a range for the string
9878 -- literal, using the index type and the known length of the literal.
9879 -- The index type is not necessarily Positive, so the upper bound is
9880 -- computed as T'Val (T'Pos (Low_Bound) + L - 1)
9883 Index_List : constant List_Id := New_List;
9884 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
9886 High_Bound : constant Node_Id :=
9887 Make_Attribute_Reference (Loc,
9888 Attribute_Name => Name_Val,
9890 New_Occurrence_Of (Index_Type, Loc),
9891 Expressions => New_List (
9894 Make_Attribute_Reference (Loc,
9895 Attribute_Name => Name_Pos,
9897 New_Occurrence_Of (Index_Type, Loc),
9899 New_List (New_Copy_Tree (Low_Bound))),
9901 Make_Integer_Literal (Loc,
9902 String_Length (Strval (N)) - 1))));
9904 Array_Subtype : Entity_Id;
9905 Index_Subtype : Entity_Id;
9910 if Is_Integer_Type (Index_Type) then
9911 Set_String_Literal_Low_Bound
9912 (Subtype_Id, Make_Integer_Literal (Loc, 1));
9915 -- If the index type is an enumeration type, build bounds
9916 -- expression with attributes.
9918 Set_String_Literal_Low_Bound
9920 Make_Attribute_Reference (Loc,
9921 Attribute_Name => Name_First,
9923 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
9924 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
9927 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
9929 -- Build bona fide subtype for the string, and wrap it in an
9930 -- unchecked conversion, because the backend expects the
9931 -- String_Literal_Subtype to have a static lower bound.
9934 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9935 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
9936 Set_Scalar_Range (Index_Subtype, Drange);
9937 Set_Parent (Drange, N);
9938 Analyze_And_Resolve (Drange, Index_Type);
9940 -- In the context, the Index_Type may already have a constraint,
9941 -- so use common base type on string subtype. The base type may
9942 -- be used when generating attributes of the string, for example
9943 -- in the context of a slice assignment.
9945 Set_Etype (Index_Subtype, Base_Type (Index_Type));
9946 Set_Size_Info (Index_Subtype, Index_Type);
9947 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9949 Array_Subtype := Create_Itype (E_Array_Subtype, N);
9951 Index := New_Occurrence_Of (Index_Subtype, Loc);
9952 Set_Etype (Index, Index_Subtype);
9953 Append (Index, Index_List);
9955 Set_First_Index (Array_Subtype, Index);
9956 Set_Etype (Array_Subtype, Base_Type (Typ));
9957 Set_Is_Constrained (Array_Subtype, True);
9960 Make_Unchecked_Type_Conversion (Loc,
9961 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9962 Expression => Relocate_Node (N)));
9963 Set_Etype (N, Array_Subtype);
9966 end Set_String_Literal_Subtype;
9968 ------------------------------
9969 -- Simplify_Type_Conversion --
9970 ------------------------------
9972 procedure Simplify_Type_Conversion (N : Node_Id) is
9974 if Nkind (N) = N_Type_Conversion then
9976 Operand : constant Node_Id := Expression (N);
9977 Target_Typ : constant Entity_Id := Etype (N);
9978 Opnd_Typ : constant Entity_Id := Etype (Operand);
9981 if Is_Floating_Point_Type (Opnd_Typ)
9983 (Is_Integer_Type (Target_Typ)
9984 or else (Is_Fixed_Point_Type (Target_Typ)
9985 and then Conversion_OK (N)))
9986 and then Nkind (Operand) = N_Attribute_Reference
9987 and then Attribute_Name (Operand) = Name_Truncation
9989 -- Special processing required if the conversion is the expression
9990 -- of a Truncation attribute reference. In this case we replace:
9992 -- ityp (ftyp'Truncation (x))
9998 -- with the Float_Truncate flag set, which is more efficient.
10002 Relocate_Node (First (Expressions (Operand))));
10003 Set_Float_Truncate (N, True);
10007 end Simplify_Type_Conversion;
10009 -----------------------------
10010 -- Unique_Fixed_Point_Type --
10011 -----------------------------
10013 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10014 T1 : Entity_Id := Empty;
10019 procedure Fixed_Point_Error;
10020 -- Give error messages for true ambiguity. Messages are posted on node
10021 -- N, and entities T1, T2 are the possible interpretations.
10023 -----------------------
10024 -- Fixed_Point_Error --
10025 -----------------------
10027 procedure Fixed_Point_Error is
10029 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10030 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10031 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10032 end Fixed_Point_Error;
10034 -- Start of processing for Unique_Fixed_Point_Type
10037 -- The operations on Duration are visible, so Duration is always a
10038 -- possible interpretation.
10040 T1 := Standard_Duration;
10042 -- Look for fixed-point types in enclosing scopes
10044 Scop := Current_Scope;
10045 while Scop /= Standard_Standard loop
10046 T2 := First_Entity (Scop);
10047 while Present (T2) loop
10048 if Is_Fixed_Point_Type (T2)
10049 and then Current_Entity (T2) = T2
10050 and then Scope (Base_Type (T2)) = Scop
10052 if Present (T1) then
10063 Scop := Scope (Scop);
10066 -- Look for visible fixed type declarations in the context
10068 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10069 while Present (Item) loop
10070 if Nkind (Item) = N_With_Clause then
10071 Scop := Entity (Name (Item));
10072 T2 := First_Entity (Scop);
10073 while Present (T2) loop
10074 if Is_Fixed_Point_Type (T2)
10075 and then Scope (Base_Type (T2)) = Scop
10076 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10078 if Present (T1) then
10093 if Nkind (N) = N_Real_Literal then
10094 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
10096 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
10100 end Unique_Fixed_Point_Type;
10102 ----------------------
10103 -- Valid_Conversion --
10104 ----------------------
10106 function Valid_Conversion
10108 Target : Entity_Id;
10110 Report_Errs : Boolean := True) return Boolean
10112 Target_Type : constant Entity_Id := Base_Type (Target);
10113 Opnd_Type : Entity_Id := Etype (Operand);
10115 function Conversion_Check
10117 Msg : String) return Boolean;
10118 -- Little routine to post Msg if Valid is False, returns Valid value
10120 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id);
10121 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10123 procedure Error_Msg_NE
10125 N : Node_Or_Entity_Id;
10126 E : Node_Or_Entity_Id);
10127 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10129 function Valid_Tagged_Conversion
10130 (Target_Type : Entity_Id;
10131 Opnd_Type : Entity_Id) return Boolean;
10132 -- Specifically test for validity of tagged conversions
10134 function Valid_Array_Conversion return Boolean;
10135 -- Check index and component conformance, and accessibility levels if
10136 -- the component types are anonymous access types (Ada 2005).
10138 ----------------------
10139 -- Conversion_Check --
10140 ----------------------
10142 function Conversion_Check
10144 Msg : String) return Boolean
10148 Error_Msg_N (Msg, Operand);
10152 end Conversion_Check;
10158 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id) is
10160 if Report_Errs then
10161 Errout.Error_Msg_N (Msg, N);
10169 procedure Error_Msg_NE
10171 N : Node_Or_Entity_Id;
10172 E : Node_Or_Entity_Id)
10175 if Report_Errs then
10176 Errout.Error_Msg_NE (Msg, N, E);
10180 ----------------------------
10181 -- Valid_Array_Conversion --
10182 ----------------------------
10184 function Valid_Array_Conversion return Boolean
10186 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10187 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10189 Opnd_Index : Node_Id;
10190 Opnd_Index_Type : Entity_Id;
10192 Target_Comp_Type : constant Entity_Id :=
10193 Component_Type (Target_Type);
10194 Target_Comp_Base : constant Entity_Id :=
10195 Base_Type (Target_Comp_Type);
10197 Target_Index : Node_Id;
10198 Target_Index_Type : Entity_Id;
10201 -- Error if wrong number of dimensions
10204 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10207 ("incompatible number of dimensions for conversion", Operand);
10210 -- Number of dimensions matches
10213 -- Loop through indexes of the two arrays
10215 Target_Index := First_Index (Target_Type);
10216 Opnd_Index := First_Index (Opnd_Type);
10217 while Present (Target_Index) and then Present (Opnd_Index) loop
10218 Target_Index_Type := Etype (Target_Index);
10219 Opnd_Index_Type := Etype (Opnd_Index);
10221 -- Error if index types are incompatible
10223 if not (Is_Integer_Type (Target_Index_Type)
10224 and then Is_Integer_Type (Opnd_Index_Type))
10225 and then (Root_Type (Target_Index_Type)
10226 /= Root_Type (Opnd_Index_Type))
10229 ("incompatible index types for array conversion",
10234 Next_Index (Target_Index);
10235 Next_Index (Opnd_Index);
10238 -- If component types have same base type, all set
10240 if Target_Comp_Base = Opnd_Comp_Base then
10243 -- Here if base types of components are not the same. The only
10244 -- time this is allowed is if we have anonymous access types.
10246 -- The conversion of arrays of anonymous access types can lead
10247 -- to dangling pointers. AI-392 formalizes the accessibility
10248 -- checks that must be applied to such conversions to prevent
10249 -- out-of-scope references.
10252 (Target_Comp_Base, E_Anonymous_Access_Type,
10253 E_Anonymous_Access_Subprogram_Type)
10254 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10256 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10258 if Type_Access_Level (Target_Type) <
10259 Type_Access_Level (Opnd_Type)
10261 if In_Instance_Body then
10262 Error_Msg_N ("?source array type " &
10263 "has deeper accessibility level than target", Operand);
10264 Error_Msg_N ("\?Program_Error will be raised at run time",
10267 Make_Raise_Program_Error (Sloc (N),
10268 Reason => PE_Accessibility_Check_Failed));
10269 Set_Etype (N, Target_Type);
10272 -- Conversion not allowed because of accessibility levels
10275 Error_Msg_N ("source array type " &
10276 "has deeper accessibility level than target", Operand);
10284 -- All other cases where component base types do not match
10288 ("incompatible component types for array conversion",
10293 -- Check that component subtypes statically match. For numeric
10294 -- types this means that both must be either constrained or
10295 -- unconstrained. For enumeration types the bounds must match.
10296 -- All of this is checked in Subtypes_Statically_Match.
10298 if not Subtypes_Statically_Match
10299 (Target_Comp_Type, Opnd_Comp_Type)
10302 ("component subtypes must statically match", Operand);
10308 end Valid_Array_Conversion;
10310 -----------------------------
10311 -- Valid_Tagged_Conversion --
10312 -----------------------------
10314 function Valid_Tagged_Conversion
10315 (Target_Type : Entity_Id;
10316 Opnd_Type : Entity_Id) return Boolean
10319 -- Upward conversions are allowed (RM 4.6(22))
10321 if Covers (Target_Type, Opnd_Type)
10322 or else Is_Ancestor (Target_Type, Opnd_Type)
10326 -- Downward conversion are allowed if the operand is class-wide
10329 elsif Is_Class_Wide_Type (Opnd_Type)
10330 and then Covers (Opnd_Type, Target_Type)
10334 elsif Covers (Opnd_Type, Target_Type)
10335 or else Is_Ancestor (Opnd_Type, Target_Type)
10338 Conversion_Check (False,
10339 "downward conversion of tagged objects not allowed");
10341 -- Ada 2005 (AI-251): The conversion to/from interface types is
10344 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10347 -- If the operand is a class-wide type obtained through a limited_
10348 -- with clause, and the context includes the non-limited view, use
10349 -- it to determine whether the conversion is legal.
10351 elsif Is_Class_Wide_Type (Opnd_Type)
10352 and then From_With_Type (Opnd_Type)
10353 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10354 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10358 elsif Is_Access_Type (Opnd_Type)
10359 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10365 ("invalid tagged conversion, not compatible with}",
10366 N, First_Subtype (Opnd_Type));
10369 end Valid_Tagged_Conversion;
10371 -- Start of processing for Valid_Conversion
10374 Check_Parameterless_Call (Operand);
10376 if Is_Overloaded (Operand) then
10386 -- Remove procedure calls, which syntactically cannot appear in
10387 -- this context, but which cannot be removed by type checking,
10388 -- because the context does not impose a type.
10390 -- When compiling for VMS, spurious ambiguities can be produced
10391 -- when arithmetic operations have a literal operand and return
10392 -- System.Address or a descendant of it. These ambiguities are
10393 -- otherwise resolved by the context, but for conversions there
10394 -- is no context type and the removal of the spurious operations
10395 -- must be done explicitly here.
10397 -- The node may be labelled overloaded, but still contain only one
10398 -- interpretation because others were discarded earlier. If this
10399 -- is the case, retain the single interpretation if legal.
10401 Get_First_Interp (Operand, I, It);
10402 Opnd_Type := It.Typ;
10403 Get_Next_Interp (I, It);
10405 if Present (It.Typ)
10406 and then Opnd_Type /= Standard_Void_Type
10408 -- More than one candidate interpretation is available
10410 Get_First_Interp (Operand, I, It);
10411 while Present (It.Typ) loop
10412 if It.Typ = Standard_Void_Type then
10416 if Present (System_Aux_Id)
10417 and then Is_Descendent_Of_Address (It.Typ)
10422 Get_Next_Interp (I, It);
10426 Get_First_Interp (Operand, I, It);
10430 if No (It.Typ) then
10431 Error_Msg_N ("illegal operand in conversion", Operand);
10435 Get_Next_Interp (I, It);
10437 if Present (It.Typ) then
10440 It1 := Disambiguate (Operand, I1, I, Any_Type);
10442 if It1 = No_Interp then
10443 Error_Msg_N ("ambiguous operand in conversion", Operand);
10445 -- If the interpretation involves a standard operator, use
10446 -- the location of the type, which may be user-defined.
10448 if Sloc (It.Nam) = Standard_Location then
10449 Error_Msg_Sloc := Sloc (It.Typ);
10451 Error_Msg_Sloc := Sloc (It.Nam);
10454 Error_Msg_N -- CODEFIX
10455 ("\\possible interpretation#!", Operand);
10457 if Sloc (N1) = Standard_Location then
10458 Error_Msg_Sloc := Sloc (T1);
10460 Error_Msg_Sloc := Sloc (N1);
10463 Error_Msg_N -- CODEFIX
10464 ("\\possible interpretation#!", Operand);
10470 Set_Etype (Operand, It1.Typ);
10471 Opnd_Type := It1.Typ;
10477 if Is_Numeric_Type (Target_Type) then
10479 -- A universal fixed expression can be converted to any numeric type
10481 if Opnd_Type = Universal_Fixed then
10484 -- Also no need to check when in an instance or inlined body, because
10485 -- the legality has been established when the template was analyzed.
10486 -- Furthermore, numeric conversions may occur where only a private
10487 -- view of the operand type is visible at the instantiation point.
10488 -- This results in a spurious error if we check that the operand type
10489 -- is a numeric type.
10491 -- Note: in a previous version of this unit, the following tests were
10492 -- applied only for generated code (Comes_From_Source set to False),
10493 -- but in fact the test is required for source code as well, since
10494 -- this situation can arise in source code.
10496 elsif In_Instance or else In_Inlined_Body then
10499 -- Otherwise we need the conversion check
10502 return Conversion_Check
10503 (Is_Numeric_Type (Opnd_Type),
10504 "illegal operand for numeric conversion");
10509 elsif Is_Array_Type (Target_Type) then
10510 if not Is_Array_Type (Opnd_Type)
10511 or else Opnd_Type = Any_Composite
10512 or else Opnd_Type = Any_String
10514 Error_Msg_N ("illegal operand for array conversion", Operand);
10517 return Valid_Array_Conversion;
10520 -- Ada 2005 (AI-251): Anonymous access types where target references an
10523 elsif Ekind_In (Target_Type, E_General_Access_Type,
10524 E_Anonymous_Access_Type)
10525 and then Is_Interface (Directly_Designated_Type (Target_Type))
10527 -- Check the static accessibility rule of 4.6(17). Note that the
10528 -- check is not enforced when within an instance body, since the
10529 -- RM requires such cases to be caught at run time.
10531 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10532 if Type_Access_Level (Opnd_Type) >
10533 Type_Access_Level (Target_Type)
10535 -- In an instance, this is a run-time check, but one we know
10536 -- will fail, so generate an appropriate warning. The raise
10537 -- will be generated by Expand_N_Type_Conversion.
10539 if In_Instance_Body then
10541 ("?cannot convert local pointer to non-local access type",
10544 ("\?Program_Error will be raised at run time", Operand);
10547 ("cannot convert local pointer to non-local access type",
10552 -- Special accessibility checks are needed in the case of access
10553 -- discriminants declared for a limited type.
10555 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10556 and then not Is_Local_Anonymous_Access (Opnd_Type)
10558 -- When the operand is a selected access discriminant the check
10559 -- needs to be made against the level of the object denoted by
10560 -- the prefix of the selected name (Object_Access_Level handles
10561 -- checking the prefix of the operand for this case).
10563 if Nkind (Operand) = N_Selected_Component
10564 and then Object_Access_Level (Operand) >
10565 Type_Access_Level (Target_Type)
10567 -- In an instance, this is a run-time check, but one we know
10568 -- will fail, so generate an appropriate warning. The raise
10569 -- will be generated by Expand_N_Type_Conversion.
10571 if In_Instance_Body then
10573 ("?cannot convert access discriminant to non-local" &
10574 " access type", Operand);
10576 ("\?Program_Error will be raised at run time", Operand);
10579 ("cannot convert access discriminant to non-local" &
10580 " access type", Operand);
10585 -- The case of a reference to an access discriminant from
10586 -- within a limited type declaration (which will appear as
10587 -- a discriminal) is always illegal because the level of the
10588 -- discriminant is considered to be deeper than any (nameable)
10591 if Is_Entity_Name (Operand)
10592 and then not Is_Local_Anonymous_Access (Opnd_Type)
10594 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10595 and then Present (Discriminal_Link (Entity (Operand)))
10598 ("discriminant has deeper accessibility level than target",
10607 -- General and anonymous access types
10609 elsif Ekind_In (Target_Type, E_General_Access_Type,
10610 E_Anonymous_Access_Type)
10613 (Is_Access_Type (Opnd_Type)
10615 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10616 E_Access_Protected_Subprogram_Type),
10617 "must be an access-to-object type")
10619 if Is_Access_Constant (Opnd_Type)
10620 and then not Is_Access_Constant (Target_Type)
10623 ("access-to-constant operand type not allowed", Operand);
10627 -- Check the static accessibility rule of 4.6(17). Note that the
10628 -- check is not enforced when within an instance body, since the RM
10629 -- requires such cases to be caught at run time.
10631 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10632 or else Is_Local_Anonymous_Access (Target_Type)
10634 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
10635 -- conversions from an anonymous access type to a named general
10636 -- access type. Such conversions are not allowed in the case of
10637 -- access parameters and stand-alone objects of an anonymous
10640 if Ada_Version >= Ada_2012
10641 and then not Comes_From_Source (N)
10642 and then Ekind (Target_Type) = E_General_Access_Type
10643 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
10645 if Is_Itype (Opnd_Type) then
10647 -- Implicit conversions aren't allowed for objects of an
10648 -- anonymous access type, since such objects have nonstatic
10649 -- levels in Ada 2012.
10651 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
10652 N_Object_Declaration
10655 ("implicit conversion of stand-alone anonymous " &
10656 "access object not allowed", Operand);
10659 -- Implicit conversions aren't allowed for anonymous access
10660 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
10661 -- is done to exclude anonymous access results.
10663 elsif not Is_Local_Anonymous_Access (Opnd_Type)
10664 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
10665 N_Function_Specification,
10666 N_Procedure_Specification)
10669 ("implicit conversion of anonymous access formal " &
10670 "not allowed", Operand);
10673 -- This is a case where there's an enclosing object whose
10674 -- to which the "statically deeper than" relationship does
10675 -- not apply (such as an access discriminant selected from
10676 -- a dereference of an access parameter).
10678 elsif Object_Access_Level (Operand)
10679 = Scope_Depth (Standard_Standard)
10682 ("implicit conversion of anonymous access value " &
10683 "not allowed", Operand);
10686 -- In other cases, the level of the operand's type must be
10687 -- statically less deep than that of the target type, else
10688 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
10690 elsif Type_Access_Level (Opnd_Type)
10691 > Type_Access_Level (Target_Type)
10694 ("implicit conversion of anonymous access value " &
10695 "violates accessibility", Operand);
10700 elsif Type_Access_Level (Opnd_Type)
10701 > Type_Access_Level (Target_Type)
10704 -- In an instance, this is a run-time check, but one we know
10705 -- will fail, so generate an appropriate warning. The raise
10706 -- will be generated by Expand_N_Type_Conversion.
10708 if In_Instance_Body then
10710 ("?cannot convert local pointer to non-local access type",
10713 ("\?Program_Error will be raised at run time", Operand);
10716 -- Avoid generation of spurious error message
10718 if not Error_Posted (N) then
10720 ("cannot convert local pointer to non-local access type",
10727 -- Special accessibility checks are needed in the case of access
10728 -- discriminants declared for a limited type.
10730 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10731 and then not Is_Local_Anonymous_Access (Opnd_Type)
10733 -- When the operand is a selected access discriminant the check
10734 -- needs to be made against the level of the object denoted by
10735 -- the prefix of the selected name (Object_Access_Level handles
10736 -- checking the prefix of the operand for this case).
10738 if Nkind (Operand) = N_Selected_Component
10739 and then Object_Access_Level (Operand) >
10740 Type_Access_Level (Target_Type)
10742 -- In an instance, this is a run-time check, but one we know
10743 -- will fail, so generate an appropriate warning. The raise
10744 -- will be generated by Expand_N_Type_Conversion.
10746 if In_Instance_Body then
10748 ("?cannot convert access discriminant to non-local" &
10749 " access type", Operand);
10751 ("\?Program_Error will be raised at run time",
10756 ("cannot convert access discriminant to non-local" &
10757 " access type", Operand);
10762 -- The case of a reference to an access discriminant from
10763 -- within a limited type declaration (which will appear as
10764 -- a discriminal) is always illegal because the level of the
10765 -- discriminant is considered to be deeper than any (nameable)
10768 if Is_Entity_Name (Operand)
10770 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10771 and then Present (Discriminal_Link (Entity (Operand)))
10774 ("discriminant has deeper accessibility level than target",
10781 -- In the presence of limited_with clauses we have to use non-limited
10782 -- views, if available.
10784 Check_Limited : declare
10785 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10786 -- Helper function to handle limited views
10788 --------------------------
10789 -- Full_Designated_Type --
10790 --------------------------
10792 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10793 Desig : constant Entity_Id := Designated_Type (T);
10796 -- Handle the limited view of a type
10798 if Is_Incomplete_Type (Desig)
10799 and then From_With_Type (Desig)
10800 and then Present (Non_Limited_View (Desig))
10802 return Available_View (Desig);
10806 end Full_Designated_Type;
10808 -- Local Declarations
10810 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10811 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10813 Same_Base : constant Boolean :=
10814 Base_Type (Target) = Base_Type (Opnd);
10816 -- Start of processing for Check_Limited
10819 if Is_Tagged_Type (Target) then
10820 return Valid_Tagged_Conversion (Target, Opnd);
10823 if not Same_Base then
10825 ("target designated type not compatible with }",
10826 N, Base_Type (Opnd));
10829 -- Ada 2005 AI-384: legality rule is symmetric in both
10830 -- designated types. The conversion is legal (with possible
10831 -- constraint check) if either designated type is
10834 elsif Subtypes_Statically_Match (Target, Opnd)
10836 (Has_Discriminants (Target)
10838 (not Is_Constrained (Opnd)
10839 or else not Is_Constrained (Target)))
10841 -- Special case, if Value_Size has been used to make the
10842 -- sizes different, the conversion is not allowed even
10843 -- though the subtypes statically match.
10845 if Known_Static_RM_Size (Target)
10846 and then Known_Static_RM_Size (Opnd)
10847 and then RM_Size (Target) /= RM_Size (Opnd)
10850 ("target designated subtype not compatible with }",
10853 ("\because sizes of the two designated subtypes differ",
10857 -- Normal case where conversion is allowed
10865 ("target designated subtype not compatible with }",
10872 -- Access to subprogram types. If the operand is an access parameter,
10873 -- the type has a deeper accessibility that any master, and cannot be
10874 -- assigned. We must make an exception if the conversion is part of an
10875 -- assignment and the target is the return object of an extended return
10876 -- statement, because in that case the accessibility check takes place
10877 -- after the return.
10879 elsif Is_Access_Subprogram_Type (Target_Type)
10880 and then No (Corresponding_Remote_Type (Opnd_Type))
10882 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10883 and then Is_Entity_Name (Operand)
10884 and then Ekind (Entity (Operand)) = E_In_Parameter
10886 (Nkind (Parent (N)) /= N_Assignment_Statement
10887 or else not Is_Entity_Name (Name (Parent (N)))
10888 or else not Is_Return_Object (Entity (Name (Parent (N)))))
10891 ("illegal attempt to store anonymous access to subprogram",
10894 ("\value has deeper accessibility than any master " &
10895 "(RM 3.10.2 (13))",
10899 ("\use named access type for& instead of access parameter",
10900 Operand, Entity (Operand));
10903 -- Check that the designated types are subtype conformant
10905 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
10906 Old_Id => Designated_Type (Opnd_Type),
10909 -- Check the static accessibility rule of 4.6(20)
10911 if Type_Access_Level (Opnd_Type) >
10912 Type_Access_Level (Target_Type)
10915 ("operand type has deeper accessibility level than target",
10918 -- Check that if the operand type is declared in a generic body,
10919 -- then the target type must be declared within that same body
10920 -- (enforces last sentence of 4.6(20)).
10922 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
10924 O_Gen : constant Node_Id :=
10925 Enclosing_Generic_Body (Opnd_Type);
10930 T_Gen := Enclosing_Generic_Body (Target_Type);
10931 while Present (T_Gen) and then T_Gen /= O_Gen loop
10932 T_Gen := Enclosing_Generic_Body (T_Gen);
10935 if T_Gen /= O_Gen then
10937 ("target type must be declared in same generic body"
10938 & " as operand type", N);
10945 -- Remote subprogram access types
10947 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
10948 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
10950 -- It is valid to convert from one RAS type to another provided
10951 -- that their specification statically match.
10953 Check_Subtype_Conformant
10955 Designated_Type (Corresponding_Remote_Type (Target_Type)),
10957 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
10962 -- If both are tagged types, check legality of view conversions
10964 elsif Is_Tagged_Type (Target_Type)
10966 Is_Tagged_Type (Opnd_Type)
10968 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
10970 -- Types derived from the same root type are convertible
10972 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
10975 -- In an instance or an inlined body, there may be inconsistent views of
10976 -- the same type, or of types derived from a common root.
10978 elsif (In_Instance or In_Inlined_Body)
10980 Root_Type (Underlying_Type (Target_Type)) =
10981 Root_Type (Underlying_Type (Opnd_Type))
10985 -- Special check for common access type error case
10987 elsif Ekind (Target_Type) = E_Access_Type
10988 and then Is_Access_Type (Opnd_Type)
10990 Error_Msg_N ("target type must be general access type!", N);
10991 Error_Msg_NE -- CODEFIX
10992 ("add ALL to }!", N, Target_Type);
10996 Error_Msg_NE ("invalid conversion, not compatible with }",
11000 end Valid_Conversion;