sem_res.adb (Resolve_Entry_Call): When an entry has preconditions...
[official-gcc.git] / gcc / ada / sem_res.adb
blob92c8bfaa2ef6a1c78e501a83bf379b685d0aa671
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ R E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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 Inline; use Inline;
41 with Itypes; use Itypes;
42 with Lib; use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
47 with Opt; use Opt;
48 with Output; use Output;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Aggr; use Sem_Aggr;
55 with Sem_Attr; use Sem_Attr;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch4; use Sem_Ch4;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Elab; use Sem_Elab;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Intr; use Sem_Intr;
68 with Sem_Util; use Sem_Util;
69 with Targparm; use Targparm;
70 with Sem_Type; use Sem_Type;
71 with Sem_Warn; use Sem_Warn;
72 with Sinfo; use Sinfo;
73 with Sinfo.CN; use Sinfo.CN;
74 with Snames; use Snames;
75 with Stand; use Stand;
76 with Stringt; use Stringt;
77 with Style; use Style;
78 with Tbuild; use Tbuild;
79 with Uintp; use Uintp;
80 with Urealp; use Urealp;
82 package body Sem_Res is
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 -- Second pass (top-down) type checking and overload resolution procedures
89 -- Typ is the type required by context. These procedures propagate the type
90 -- information recursively to the descendants of N. If the node is not
91 -- overloaded, its Etype is established in the first pass. If overloaded,
92 -- the Resolve routines set the correct type. For arith. operators, the
93 -- Etype is the base type of the context.
95 -- Note that Resolve_Attribute is separated off in Sem_Attr
97 procedure Check_Discriminant_Use (N : Node_Id);
98 -- Enforce the restrictions on the use of discriminants when constraining
99 -- a component of a discriminated type (record or concurrent type).
101 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
102 -- Given a node for an operator associated with type T, check that
103 -- the operator is visible. Operators all of whose operands are
104 -- universal must be checked for visibility during resolution
105 -- because their type is not determinable based on their operands.
107 procedure Check_Fully_Declared_Prefix
108 (Typ : Entity_Id;
109 Pref : Node_Id);
110 -- Check that the type of the prefix of a dereference is not incomplete
112 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
113 -- Given a call node, N, which is known to occur immediately within the
114 -- subprogram being called, determines whether it is a detectable case of
115 -- an infinite recursion, and if so, outputs appropriate messages. Returns
116 -- True if an infinite recursion is detected, and False otherwise.
118 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
119 -- If the type of the object being initialized uses the secondary stack
120 -- directly or indirectly, create a transient scope for the call to the
121 -- init proc. This is because we do not create transient scopes for the
122 -- initialization of individual components within the init proc itself.
123 -- Could be optimized away perhaps?
125 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
126 -- N is the node for a logical operator. If the operator is predefined, and
127 -- the root type of the operands is Standard.Boolean, then a check is made
128 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
129 -- the style check for Style_Check_Boolean_And_Or.
131 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
132 -- Determine whether E is an access type declared by an access declaration,
133 -- and not an (anonymous) allocator type.
135 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
136 -- Utility to check whether the entity for an operator is a predefined
137 -- operator, in which case the expression is left as an operator in the
138 -- tree (else it is rewritten into a call). An instance of an intrinsic
139 -- conversion operation may be given an operator name, but is not treated
140 -- like an operator. Note that an operator that is an imported back-end
141 -- builtin has convention Intrinsic, but is expected to be rewritten into
142 -- a call, so such an operator is not treated as predefined by this
143 -- predicate.
145 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
146 -- If a default expression in entry call N depends on the discriminants
147 -- of the task, it must be replaced with a reference to the discriminant
148 -- of the task being called.
150 procedure Resolve_Op_Concat_Arg
151 (N : Node_Id;
152 Arg : Node_Id;
153 Typ : Entity_Id;
154 Is_Comp : Boolean);
155 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
156 -- concatenation operator. The operand is either of the array type or of
157 -- the component type. If the operand is an aggregate, and the component
158 -- type is composite, this is ambiguous if component type has aggregates.
160 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
161 -- Does the first part of the work of Resolve_Op_Concat
163 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
164 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
165 -- has been resolved. See Resolve_Op_Concat for details.
167 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
168 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
169 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
170 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
171 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
172 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
173 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
174 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
203 function Operator_Kind
204 (Op_Name : Name_Id;
205 Is_Binary : Boolean) return Node_Kind;
206 -- Utility to map the name of an operator into the corresponding Node. Used
207 -- by other node rewriting procedures.
209 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
210 -- Resolve actuals of call, and add default expressions for missing ones.
211 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
212 -- called subprogram.
214 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
215 -- Called from Resolve_Call, when the prefix denotes an entry or element
216 -- of entry family. Actuals are resolved as for subprograms, and the node
217 -- is rebuilt as an entry call. Also called for protected operations. Typ
218 -- is the context type, which is used when the operation is a protected
219 -- function with no arguments, and the return value is indexed.
221 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
222 -- A call to a user-defined intrinsic operator is rewritten as a call to
223 -- the corresponding predefined operator, with suitable conversions. Note
224 -- that this applies only for intrinsic operators that denote predefined
225 -- operators, not ones that are intrinsic imports of back-end builtins.
227 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
228 -- Ditto, for arithmetic unary operators
230 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
231 -- If an operator node resolves to a call to a user-defined operator,
232 -- rewrite the node as a function call.
234 procedure Make_Call_Into_Operator
235 (N : Node_Id;
236 Typ : Entity_Id;
237 Op_Id : Entity_Id);
238 -- Inverse transformation: if an operator is given in functional notation,
239 -- then after resolving the node, transform into an operator node, so
240 -- that operands are resolved properly. Recall that predefined operators
241 -- do not have a full signature and special resolution rules apply.
243 procedure Rewrite_Renamed_Operator
244 (N : Node_Id;
245 Op : Entity_Id;
246 Typ : Entity_Id);
247 -- An operator can rename another, e.g. in an instantiation. In that
248 -- case, the proper operator node must be constructed and resolved.
250 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
251 -- The String_Literal_Subtype is built for all strings that are not
252 -- operands of a static concatenation operation. If the argument is
253 -- not a N_String_Literal node, then the call has no effect.
255 procedure Set_Slice_Subtype (N : Node_Id);
256 -- Build subtype of array type, with the range specified by the slice
258 procedure Simplify_Type_Conversion (N : Node_Id);
259 -- Called after N has been resolved and evaluated, but before range checks
260 -- have been applied. Currently simplifies a combination of floating-point
261 -- to integer conversion and Rounding or Truncation attribute.
263 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
264 -- A universal_fixed expression in an universal context is unambiguous if
265 -- there is only one applicable fixed point type. Determining whether there
266 -- is only one requires a search over all visible entities, and happens
267 -- only in very pathological cases (see 6115-006).
269 -------------------------
270 -- Ambiguous_Character --
271 -------------------------
273 procedure Ambiguous_Character (C : Node_Id) is
274 E : Entity_Id;
276 begin
277 if Nkind (C) = N_Character_Literal then
278 Error_Msg_N ("ambiguous character literal", C);
280 -- First the ones in Standard
282 Error_Msg_N ("\\possible interpretation: Character!", C);
283 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
285 -- Include Wide_Wide_Character in Ada 2005 mode
287 if Ada_Version >= Ada_2005 then
288 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
289 end if;
291 -- Now any other types that match
293 E := Current_Entity (C);
294 while Present (E) loop
295 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
296 E := Homonym (E);
297 end loop;
298 end if;
299 end Ambiguous_Character;
301 -------------------------
302 -- Analyze_And_Resolve --
303 -------------------------
305 procedure Analyze_And_Resolve (N : Node_Id) is
306 begin
307 Analyze (N);
308 Resolve (N);
309 end Analyze_And_Resolve;
311 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
312 begin
313 Analyze (N);
314 Resolve (N, Typ);
315 end Analyze_And_Resolve;
317 -- Versions with check(s) suppressed
319 procedure Analyze_And_Resolve
320 (N : Node_Id;
321 Typ : Entity_Id;
322 Suppress : Check_Id)
324 Scop : constant Entity_Id := Current_Scope;
326 begin
327 if Suppress = All_Checks then
328 declare
329 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
330 begin
331 Scope_Suppress.Suppress := (others => True);
332 Analyze_And_Resolve (N, Typ);
333 Scope_Suppress.Suppress := Sva;
334 end;
336 else
337 declare
338 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
339 begin
340 Scope_Suppress.Suppress (Suppress) := True;
341 Analyze_And_Resolve (N, Typ);
342 Scope_Suppress.Suppress (Suppress) := Svg;
343 end;
344 end if;
346 if Current_Scope /= Scop
347 and then Scope_Is_Transient
348 then
349 -- This can only happen if a transient scope was created for an inner
350 -- expression, which will be removed upon completion of the analysis
351 -- of an enclosing construct. The transient scope must have the
352 -- suppress status of the enclosing environment, not of this Analyze
353 -- call.
355 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
356 Scope_Suppress;
357 end if;
358 end Analyze_And_Resolve;
360 procedure Analyze_And_Resolve
361 (N : Node_Id;
362 Suppress : Check_Id)
364 Scop : constant Entity_Id := Current_Scope;
366 begin
367 if Suppress = All_Checks then
368 declare
369 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
370 begin
371 Scope_Suppress.Suppress := (others => True);
372 Analyze_And_Resolve (N);
373 Scope_Suppress.Suppress := Sva;
374 end;
376 else
377 declare
378 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
379 begin
380 Scope_Suppress.Suppress (Suppress) := True;
381 Analyze_And_Resolve (N);
382 Scope_Suppress.Suppress (Suppress) := Svg;
383 end;
384 end if;
386 if Current_Scope /= Scop and then Scope_Is_Transient then
387 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
388 Scope_Suppress;
389 end if;
390 end Analyze_And_Resolve;
392 ----------------------------
393 -- Check_Discriminant_Use --
394 ----------------------------
396 procedure Check_Discriminant_Use (N : Node_Id) is
397 PN : constant Node_Id := Parent (N);
398 Disc : constant Entity_Id := Entity (N);
399 P : Node_Id;
400 D : Node_Id;
402 begin
403 -- Any use in a spec-expression is legal
405 if In_Spec_Expression then
406 null;
408 elsif Nkind (PN) = N_Range then
410 -- Discriminant cannot be used to constrain a scalar type
412 P := Parent (PN);
414 if Nkind (P) = N_Range_Constraint
415 and then Nkind (Parent (P)) = N_Subtype_Indication
416 and then Nkind (Parent (Parent (P))) = N_Component_Definition
417 then
418 Error_Msg_N ("discriminant cannot constrain scalar type", N);
420 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
422 -- The following check catches the unusual case where a
423 -- discriminant appears within an index constraint that is part of
424 -- a larger expression within a constraint on a component, e.g. "C
425 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
426 -- of record components, and note that a similar check should also
427 -- apply in the case of discriminant constraints below. ???
429 -- Note that the check for N_Subtype_Declaration below is to
430 -- detect the valid use of discriminants in the constraints of a
431 -- subtype declaration when this subtype declaration appears
432 -- inside the scope of a record type (which is syntactically
433 -- illegal, but which may be created as part of derived type
434 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
435 -- for more info.
437 if Ekind (Current_Scope) = E_Record_Type
438 and then Scope (Disc) = Current_Scope
439 and then not
440 (Nkind (Parent (P)) = N_Subtype_Indication
441 and then
442 Nkind_In (Parent (Parent (P)), N_Component_Definition,
443 N_Subtype_Declaration)
444 and then Paren_Count (N) = 0)
445 then
446 Error_Msg_N
447 ("discriminant must appear alone in component constraint", N);
448 return;
449 end if;
451 -- Detect a common error:
453 -- type R (D : Positive := 100) is record
454 -- Name : String (1 .. D);
455 -- end record;
457 -- The default value causes an object of type R to be allocated
458 -- with room for Positive'Last characters. The RM does not mandate
459 -- the allocation of the maximum size, but that is what GNAT does
460 -- so we should warn the programmer that there is a problem.
462 Check_Large : declare
463 SI : Node_Id;
464 T : Entity_Id;
465 TB : Node_Id;
466 CB : Entity_Id;
468 function Large_Storage_Type (T : Entity_Id) return Boolean;
469 -- Return True if type T has a large enough range that any
470 -- array whose index type covered the whole range of the type
471 -- would likely raise Storage_Error.
473 ------------------------
474 -- Large_Storage_Type --
475 ------------------------
477 function Large_Storage_Type (T : Entity_Id) return Boolean is
478 begin
479 -- The type is considered large if its bounds are known at
480 -- compile time and if it requires at least as many bits as
481 -- a Positive to store the possible values.
483 return Compile_Time_Known_Value (Type_Low_Bound (T))
484 and then Compile_Time_Known_Value (Type_High_Bound (T))
485 and then
486 Minimum_Size (T, Biased => True) >=
487 RM_Size (Standard_Positive);
488 end Large_Storage_Type;
490 -- Start of processing for Check_Large
492 begin
493 -- Check that the Disc has a large range
495 if not Large_Storage_Type (Etype (Disc)) then
496 goto No_Danger;
497 end if;
499 -- If the enclosing type is limited, we allocate only the
500 -- default value, not the maximum, and there is no need for
501 -- a warning.
503 if Is_Limited_Type (Scope (Disc)) then
504 goto No_Danger;
505 end if;
507 -- Check that it is the high bound
509 if N /= High_Bound (PN)
510 or else No (Discriminant_Default_Value (Disc))
511 then
512 goto No_Danger;
513 end if;
515 -- Check the array allows a large range at this bound. First
516 -- find the array
518 SI := Parent (P);
520 if Nkind (SI) /= N_Subtype_Indication then
521 goto No_Danger;
522 end if;
524 T := Entity (Subtype_Mark (SI));
526 if not Is_Array_Type (T) then
527 goto No_Danger;
528 end if;
530 -- Next, find the dimension
532 TB := First_Index (T);
533 CB := First (Constraints (P));
534 while True
535 and then Present (TB)
536 and then Present (CB)
537 and then CB /= PN
538 loop
539 Next_Index (TB);
540 Next (CB);
541 end loop;
543 if CB /= PN then
544 goto No_Danger;
545 end if;
547 -- Now, check the dimension has a large range
549 if not Large_Storage_Type (Etype (TB)) then
550 goto No_Danger;
551 end if;
553 -- Warn about the danger
555 Error_Msg_N
556 ("??creation of & object may raise Storage_Error!",
557 Scope (Disc));
559 <<No_Danger>>
560 null;
562 end Check_Large;
563 end if;
565 -- Legal case is in index or discriminant constraint
567 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
568 N_Discriminant_Association)
569 then
570 if Paren_Count (N) > 0 then
571 Error_Msg_N
572 ("discriminant in constraint must appear alone", N);
574 elsif Nkind (N) = N_Expanded_Name
575 and then Comes_From_Source (N)
576 then
577 Error_Msg_N
578 ("discriminant must appear alone as a direct name", N);
579 end if;
581 return;
583 -- Otherwise, context is an expression. It should not be within (i.e. a
584 -- subexpression of) a constraint for a component.
586 else
587 D := PN;
588 P := Parent (PN);
589 while not Nkind_In (P, N_Component_Declaration,
590 N_Subtype_Indication,
591 N_Entry_Declaration)
592 loop
593 D := P;
594 P := Parent (P);
595 exit when No (P);
596 end loop;
598 -- If the discriminant is used in an expression that is a bound of a
599 -- scalar type, an Itype is created and the bounds are attached to
600 -- its range, not to the original subtype indication. Such use is of
601 -- course a double fault.
603 if (Nkind (P) = N_Subtype_Indication
604 and then Nkind_In (Parent (P), N_Component_Definition,
605 N_Derived_Type_Definition)
606 and then D = Constraint (P))
608 -- The constraint itself may be given by a subtype indication,
609 -- rather than by a more common discrete range.
611 or else (Nkind (P) = N_Subtype_Indication
612 and then
613 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
614 or else Nkind (P) = N_Entry_Declaration
615 or else Nkind (D) = N_Defining_Identifier
616 then
617 Error_Msg_N
618 ("discriminant in constraint must appear alone", N);
619 end if;
620 end if;
621 end Check_Discriminant_Use;
623 --------------------------------
624 -- Check_For_Visible_Operator --
625 --------------------------------
627 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
628 begin
629 if Is_Invisible_Operator (N, T) then
630 Error_Msg_NE -- CODEFIX
631 ("operator for} is not directly visible!", N, First_Subtype (T));
632 Error_Msg_N -- CODEFIX
633 ("use clause would make operation legal!", N);
634 end if;
635 end Check_For_Visible_Operator;
637 ----------------------------------
638 -- Check_Fully_Declared_Prefix --
639 ----------------------------------
641 procedure Check_Fully_Declared_Prefix
642 (Typ : Entity_Id;
643 Pref : Node_Id)
645 begin
646 -- Check that the designated type of the prefix of a dereference is
647 -- not an incomplete type. This cannot be done unconditionally, because
648 -- dereferences of private types are legal in default expressions. This
649 -- case is taken care of in Check_Fully_Declared, called below. There
650 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
652 -- This consideration also applies to similar checks for allocators,
653 -- qualified expressions, and type conversions.
655 -- An additional exception concerns other per-object expressions that
656 -- are not directly related to component declarations, in particular
657 -- representation pragmas for tasks. These will be per-object
658 -- expressions if they depend on discriminants or some global entity.
659 -- If the task has access discriminants, the designated type may be
660 -- incomplete at the point the expression is resolved. This resolution
661 -- takes place within the body of the initialization procedure, where
662 -- the discriminant is replaced by its discriminal.
664 if Is_Entity_Name (Pref)
665 and then Ekind (Entity (Pref)) = E_In_Parameter
666 then
667 null;
669 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
670 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
671 -- Analyze_Object_Renaming, and Freeze_Entity.
673 elsif Ada_Version >= Ada_2005
674 and then Is_Entity_Name (Pref)
675 and then Is_Access_Type (Etype (Pref))
676 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
677 E_Incomplete_Type
678 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
679 then
680 null;
681 else
682 Check_Fully_Declared (Typ, Parent (Pref));
683 end if;
684 end Check_Fully_Declared_Prefix;
686 ------------------------------
687 -- Check_Infinite_Recursion --
688 ------------------------------
690 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
691 P : Node_Id;
692 C : Node_Id;
694 function Same_Argument_List return Boolean;
695 -- Check whether list of actuals is identical to list of formals of
696 -- called function (which is also the enclosing scope).
698 ------------------------
699 -- Same_Argument_List --
700 ------------------------
702 function Same_Argument_List return Boolean is
703 A : Node_Id;
704 F : Entity_Id;
705 Subp : Entity_Id;
707 begin
708 if not Is_Entity_Name (Name (N)) then
709 return False;
710 else
711 Subp := Entity (Name (N));
712 end if;
714 F := First_Formal (Subp);
715 A := First_Actual (N);
716 while Present (F) and then Present (A) loop
717 if not Is_Entity_Name (A)
718 or else Entity (A) /= F
719 then
720 return False;
721 end if;
723 Next_Actual (A);
724 Next_Formal (F);
725 end loop;
727 return True;
728 end Same_Argument_List;
730 -- Start of processing for Check_Infinite_Recursion
732 begin
733 -- Special case, if this is a procedure call and is a call to the
734 -- current procedure with the same argument list, then this is for
735 -- sure an infinite recursion and we insert a call to raise SE.
737 if Is_List_Member (N)
738 and then List_Length (List_Containing (N)) = 1
739 and then Same_Argument_List
740 then
741 declare
742 P : constant Node_Id := Parent (N);
743 begin
744 if Nkind (P) = N_Handled_Sequence_Of_Statements
745 and then Nkind (Parent (P)) = N_Subprogram_Body
746 and then Is_Empty_List (Declarations (Parent (P)))
747 then
748 Error_Msg_Warn := SPARK_Mode /= On;
749 Error_Msg_N ("!infinite recursion<<", N);
750 Error_Msg_N ("\!Storage_Error [<<", N);
751 Insert_Action (N,
752 Make_Raise_Storage_Error (Sloc (N),
753 Reason => SE_Infinite_Recursion));
754 return True;
755 end if;
756 end;
757 end if;
759 -- If not that special case, search up tree, quitting if we reach a
760 -- construct (e.g. a conditional) that tells us that this is not a
761 -- case for an infinite recursion warning.
763 C := N;
764 loop
765 P := Parent (C);
767 -- If no parent, then we were not inside a subprogram, this can for
768 -- example happen when processing certain pragmas in a spec. Just
769 -- return False in this case.
771 if No (P) then
772 return False;
773 end if;
775 -- Done if we get to subprogram body, this is definitely an infinite
776 -- recursion case if we did not find anything to stop us.
778 exit when Nkind (P) = N_Subprogram_Body;
780 -- If appearing in conditional, result is false
782 if Nkind_In (P, N_Or_Else,
783 N_And_Then,
784 N_Case_Expression,
785 N_Case_Statement,
786 N_If_Expression,
787 N_If_Statement)
788 then
789 return False;
791 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
792 and then C /= First (Statements (P))
793 then
794 -- If the call is the expression of a return statement and the
795 -- actuals are identical to the formals, it's worth a warning.
796 -- However, we skip this if there is an immediately preceding
797 -- raise statement, since the call is never executed.
799 -- Furthermore, this corresponds to a common idiom:
801 -- function F (L : Thing) return Boolean is
802 -- begin
803 -- raise Program_Error;
804 -- return F (L);
805 -- end F;
807 -- for generating a stub function
809 if Nkind (Parent (N)) = N_Simple_Return_Statement
810 and then Same_Argument_List
811 then
812 exit when not Is_List_Member (Parent (N));
814 -- OK, return statement is in a statement list, look for raise
816 declare
817 Nod : Node_Id;
819 begin
820 -- Skip past N_Freeze_Entity nodes generated by expansion
822 Nod := Prev (Parent (N));
823 while Present (Nod)
824 and then Nkind (Nod) = N_Freeze_Entity
825 loop
826 Prev (Nod);
827 end loop;
829 -- If no raise statement, give warning. We look at the
830 -- original node, because in the case of "raise ... with
831 -- ...", the node has been transformed into a call.
833 exit when Nkind (Original_Node (Nod)) /= N_Raise_Statement
834 and then
835 (Nkind (Nod) not in N_Raise_xxx_Error
836 or else Present (Condition (Nod)));
837 end;
838 end if;
840 return False;
842 else
843 C := P;
844 end if;
845 end loop;
847 Error_Msg_Warn := SPARK_Mode /= On;
848 Error_Msg_N ("!possible infinite recursion<<", N);
849 Error_Msg_N ("\!??Storage_Error ]<<", N);
851 return True;
852 end Check_Infinite_Recursion;
854 -------------------------------
855 -- Check_Initialization_Call --
856 -------------------------------
858 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
859 Typ : constant Entity_Id := Etype (First_Formal (Nam));
861 function Uses_SS (T : Entity_Id) return Boolean;
862 -- Check whether the creation of an object of the type will involve
863 -- use of the secondary stack. If T is a record type, this is true
864 -- if the expression for some component uses the secondary stack, e.g.
865 -- through a call to a function that returns an unconstrained value.
866 -- False if T is controlled, because cleanups occur elsewhere.
868 -------------
869 -- Uses_SS --
870 -------------
872 function Uses_SS (T : Entity_Id) return Boolean is
873 Comp : Entity_Id;
874 Expr : Node_Id;
875 Full_Type : Entity_Id := Underlying_Type (T);
877 begin
878 -- Normally we want to use the underlying type, but if it's not set
879 -- then continue with T.
881 if not Present (Full_Type) then
882 Full_Type := T;
883 end if;
885 if Is_Controlled (Full_Type) then
886 return False;
888 elsif Is_Array_Type (Full_Type) then
889 return Uses_SS (Component_Type (Full_Type));
891 elsif Is_Record_Type (Full_Type) then
892 Comp := First_Component (Full_Type);
893 while Present (Comp) loop
894 if Ekind (Comp) = E_Component
895 and then Nkind (Parent (Comp)) = N_Component_Declaration
896 then
897 -- The expression for a dynamic component may be rewritten
898 -- as a dereference, so retrieve original node.
900 Expr := Original_Node (Expression (Parent (Comp)));
902 -- Return True if the expression is a call to a function
903 -- (including an attribute function such as Image, or a
904 -- user-defined operator) with a result that requires a
905 -- transient scope.
907 if (Nkind (Expr) = N_Function_Call
908 or else Nkind (Expr) in N_Op
909 or else (Nkind (Expr) = N_Attribute_Reference
910 and then Present (Expressions (Expr))))
911 and then Requires_Transient_Scope (Etype (Expr))
912 then
913 return True;
915 elsif Uses_SS (Etype (Comp)) then
916 return True;
917 end if;
918 end if;
920 Next_Component (Comp);
921 end loop;
923 return False;
925 else
926 return False;
927 end if;
928 end Uses_SS;
930 -- Start of processing for Check_Initialization_Call
932 begin
933 -- Establish a transient scope if the type needs it
935 if Uses_SS (Typ) then
936 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
937 end if;
938 end Check_Initialization_Call;
940 ---------------------------------------
941 -- Check_No_Direct_Boolean_Operators --
942 ---------------------------------------
944 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
945 begin
946 if Scope (Entity (N)) = Standard_Standard
947 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
948 then
949 -- Restriction only applies to original source code
951 if Comes_From_Source (N) then
952 Check_Restriction (No_Direct_Boolean_Operators, N);
953 end if;
954 end if;
956 -- Do style check (but skip if in instance, error is on template)
958 if Style_Check then
959 if not In_Instance then
960 Check_Boolean_Operator (N);
961 end if;
962 end if;
963 end Check_No_Direct_Boolean_Operators;
965 ------------------------------
966 -- Check_Parameterless_Call --
967 ------------------------------
969 procedure Check_Parameterless_Call (N : Node_Id) is
970 Nam : Node_Id;
972 function Prefix_Is_Access_Subp return Boolean;
973 -- If the prefix is of an access_to_subprogram type, the node must be
974 -- rewritten as a call. Ditto if the prefix is overloaded and all its
975 -- interpretations are access to subprograms.
977 ---------------------------
978 -- Prefix_Is_Access_Subp --
979 ---------------------------
981 function Prefix_Is_Access_Subp return Boolean is
982 I : Interp_Index;
983 It : Interp;
985 begin
986 -- If the context is an attribute reference that can apply to
987 -- functions, this is never a parameterless call (RM 4.1.4(6)).
989 if Nkind (Parent (N)) = N_Attribute_Reference
990 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
991 Name_Code_Address,
992 Name_Access)
993 then
994 return False;
995 end if;
997 if not Is_Overloaded (N) then
998 return
999 Ekind (Etype (N)) = E_Subprogram_Type
1000 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1001 else
1002 Get_First_Interp (N, I, It);
1003 while Present (It.Typ) loop
1004 if Ekind (It.Typ) /= E_Subprogram_Type
1005 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1006 then
1007 return False;
1008 end if;
1010 Get_Next_Interp (I, It);
1011 end loop;
1013 return True;
1014 end if;
1015 end Prefix_Is_Access_Subp;
1017 -- Start of processing for Check_Parameterless_Call
1019 begin
1020 -- Defend against junk stuff if errors already detected
1022 if Total_Errors_Detected /= 0 then
1023 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1024 return;
1025 elsif Nkind (N) in N_Has_Chars
1026 and then Chars (N) in Error_Name_Or_No_Name
1027 then
1028 return;
1029 end if;
1031 Require_Entity (N);
1032 end if;
1034 -- If the context expects a value, and the name is a procedure, this is
1035 -- most likely a missing 'Access. Don't try to resolve the parameterless
1036 -- call, error will be caught when the outer call is analyzed.
1038 if Is_Entity_Name (N)
1039 and then Ekind (Entity (N)) = E_Procedure
1040 and then not Is_Overloaded (N)
1041 and then
1042 Nkind_In (Parent (N), N_Parameter_Association,
1043 N_Function_Call,
1044 N_Procedure_Call_Statement)
1045 then
1046 return;
1047 end if;
1049 -- Rewrite as call if overloadable entity that is (or could be, in the
1050 -- overloaded case) a function call. If we know for sure that the entity
1051 -- is an enumeration literal, we do not rewrite it.
1053 -- If the entity is the name of an operator, it cannot be a call because
1054 -- operators cannot have default parameters. In this case, this must be
1055 -- a string whose contents coincide with an operator name. Set the kind
1056 -- of the node appropriately.
1058 if (Is_Entity_Name (N)
1059 and then Nkind (N) /= N_Operator_Symbol
1060 and then Is_Overloadable (Entity (N))
1061 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1062 or else Is_Overloaded (N)))
1064 -- Rewrite as call if it is an explicit dereference of an expression of
1065 -- a subprogram access type, and the subprogram type is not that of a
1066 -- procedure or entry.
1068 or else
1069 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1071 -- Rewrite as call if it is a selected component which is a function,
1072 -- this is the case of a call to a protected function (which may be
1073 -- overloaded with other protected operations).
1075 or else
1076 (Nkind (N) = N_Selected_Component
1077 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1078 or else
1079 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1080 E_Procedure)
1081 and then Is_Overloaded (Selector_Name (N)))))
1083 -- If one of the above three conditions is met, rewrite as call. Apply
1084 -- the rewriting only once.
1086 then
1087 if Nkind (Parent (N)) /= N_Function_Call
1088 or else N /= Name (Parent (N))
1089 then
1091 -- This may be a prefixed call that was not fully analyzed, e.g.
1092 -- an actual in an instance.
1094 if Ada_Version >= Ada_2005
1095 and then Nkind (N) = N_Selected_Component
1096 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1097 then
1098 Analyze_Selected_Component (N);
1100 if Nkind (N) /= N_Selected_Component then
1101 return;
1102 end if;
1103 end if;
1105 -- The node is the name of the parameterless call. Preserve its
1106 -- descendants, which may be complex expressions.
1108 Nam := Relocate_Node (N);
1110 -- If overloaded, overload set belongs to new copy
1112 Save_Interps (N, Nam);
1114 -- Change node to parameterless function call (note that the
1115 -- Parameter_Associations associations field is left set to Empty,
1116 -- its normal default value since there are no parameters)
1118 Change_Node (N, N_Function_Call);
1119 Set_Name (N, Nam);
1120 Set_Sloc (N, Sloc (Nam));
1121 Analyze_Call (N);
1122 end if;
1124 elsif Nkind (N) = N_Parameter_Association then
1125 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1127 elsif Nkind (N) = N_Operator_Symbol then
1128 Change_Operator_Symbol_To_String_Literal (N);
1129 Set_Is_Overloaded (N, False);
1130 Set_Etype (N, Any_String);
1131 end if;
1132 end Check_Parameterless_Call;
1134 -----------------------------
1135 -- Is_Definite_Access_Type --
1136 -----------------------------
1138 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1139 Btyp : constant Entity_Id := Base_Type (E);
1140 begin
1141 return Ekind (Btyp) = E_Access_Type
1142 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1143 and then Comes_From_Source (Btyp));
1144 end Is_Definite_Access_Type;
1146 ----------------------
1147 -- Is_Predefined_Op --
1148 ----------------------
1150 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1151 begin
1152 -- Predefined operators are intrinsic subprograms
1154 if not Is_Intrinsic_Subprogram (Nam) then
1155 return False;
1156 end if;
1158 -- A call to a back-end builtin is never a predefined operator
1160 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1161 return False;
1162 end if;
1164 return not Is_Generic_Instance (Nam)
1165 and then Chars (Nam) in Any_Operator_Name
1166 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1167 end Is_Predefined_Op;
1169 -----------------------------
1170 -- Make_Call_Into_Operator --
1171 -----------------------------
1173 procedure Make_Call_Into_Operator
1174 (N : Node_Id;
1175 Typ : Entity_Id;
1176 Op_Id : Entity_Id)
1178 Op_Name : constant Name_Id := Chars (Op_Id);
1179 Act1 : Node_Id := First_Actual (N);
1180 Act2 : Node_Id := Next_Actual (Act1);
1181 Error : Boolean := False;
1182 Func : constant Entity_Id := Entity (Name (N));
1183 Is_Binary : constant Boolean := Present (Act2);
1184 Op_Node : Node_Id;
1185 Opnd_Type : Entity_Id;
1186 Orig_Type : Entity_Id := Empty;
1187 Pack : Entity_Id;
1189 type Kind_Test is access function (E : Entity_Id) return Boolean;
1191 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1192 -- If the operand is not universal, and the operator is given by an
1193 -- expanded name, verify that the operand has an interpretation with a
1194 -- type defined in the given scope of the operator.
1196 function Type_In_P (Test : Kind_Test) return Entity_Id;
1197 -- Find a type of the given class in package Pack that contains the
1198 -- operator.
1200 ---------------------------
1201 -- Operand_Type_In_Scope --
1202 ---------------------------
1204 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1205 Nod : constant Node_Id := Right_Opnd (Op_Node);
1206 I : Interp_Index;
1207 It : Interp;
1209 begin
1210 if not Is_Overloaded (Nod) then
1211 return Scope (Base_Type (Etype (Nod))) = S;
1213 else
1214 Get_First_Interp (Nod, I, It);
1215 while Present (It.Typ) loop
1216 if Scope (Base_Type (It.Typ)) = S then
1217 return True;
1218 end if;
1220 Get_Next_Interp (I, It);
1221 end loop;
1223 return False;
1224 end if;
1225 end Operand_Type_In_Scope;
1227 ---------------
1228 -- Type_In_P --
1229 ---------------
1231 function Type_In_P (Test : Kind_Test) return Entity_Id is
1232 E : Entity_Id;
1234 function In_Decl return Boolean;
1235 -- Verify that node is not part of the type declaration for the
1236 -- candidate type, which would otherwise be invisible.
1238 -------------
1239 -- In_Decl --
1240 -------------
1242 function In_Decl return Boolean is
1243 Decl_Node : constant Node_Id := Parent (E);
1244 N2 : Node_Id;
1246 begin
1247 N2 := N;
1249 if Etype (E) = Any_Type then
1250 return True;
1252 elsif No (Decl_Node) then
1253 return False;
1255 else
1256 while Present (N2)
1257 and then Nkind (N2) /= N_Compilation_Unit
1258 loop
1259 if N2 = Decl_Node then
1260 return True;
1261 else
1262 N2 := Parent (N2);
1263 end if;
1264 end loop;
1266 return False;
1267 end if;
1268 end In_Decl;
1270 -- Start of processing for Type_In_P
1272 begin
1273 -- If the context type is declared in the prefix package, this is the
1274 -- desired base type.
1276 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1277 return Base_Type (Typ);
1279 else
1280 E := First_Entity (Pack);
1281 while Present (E) loop
1282 if Test (E)
1283 and then not In_Decl
1284 then
1285 return E;
1286 end if;
1288 Next_Entity (E);
1289 end loop;
1291 return Empty;
1292 end if;
1293 end Type_In_P;
1295 -- Start of processing for Make_Call_Into_Operator
1297 begin
1298 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1300 -- Binary operator
1302 if Is_Binary then
1303 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1304 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1305 Save_Interps (Act1, Left_Opnd (Op_Node));
1306 Save_Interps (Act2, Right_Opnd (Op_Node));
1307 Act1 := Left_Opnd (Op_Node);
1308 Act2 := Right_Opnd (Op_Node);
1310 -- Unary operator
1312 else
1313 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1314 Save_Interps (Act1, Right_Opnd (Op_Node));
1315 Act1 := Right_Opnd (Op_Node);
1316 end if;
1318 -- If the operator is denoted by an expanded name, and the prefix is
1319 -- not Standard, but the operator is a predefined one whose scope is
1320 -- Standard, then this is an implicit_operator, inserted as an
1321 -- interpretation by the procedure of the same name. This procedure
1322 -- overestimates the presence of implicit operators, because it does
1323 -- not examine the type of the operands. Verify now that the operand
1324 -- type appears in the given scope. If right operand is universal,
1325 -- check the other operand. In the case of concatenation, either
1326 -- argument can be the component type, so check the type of the result.
1327 -- If both arguments are literals, look for a type of the right kind
1328 -- defined in the given scope. This elaborate nonsense is brought to
1329 -- you courtesy of b33302a. The type itself must be frozen, so we must
1330 -- find the type of the proper class in the given scope.
1332 -- A final wrinkle is the multiplication operator for fixed point types,
1333 -- which is defined in Standard only, and not in the scope of the
1334 -- fixed point type itself.
1336 if Nkind (Name (N)) = N_Expanded_Name then
1337 Pack := Entity (Prefix (Name (N)));
1339 -- If this is a package renaming, get renamed entity, which will be
1340 -- the scope of the operands if operaton is type-correct.
1342 if Present (Renamed_Entity (Pack)) then
1343 Pack := Renamed_Entity (Pack);
1344 end if;
1346 -- If the entity being called is defined in the given package, it is
1347 -- a renaming of a predefined operator, and known to be legal.
1349 if Scope (Entity (Name (N))) = Pack
1350 and then Pack /= Standard_Standard
1351 then
1352 null;
1354 -- Visibility does not need to be checked in an instance: if the
1355 -- operator was not visible in the generic it has been diagnosed
1356 -- already, else there is an implicit copy of it in the instance.
1358 elsif In_Instance then
1359 null;
1361 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1362 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1363 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1364 then
1365 if Pack /= Standard_Standard then
1366 Error := True;
1367 end if;
1369 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1370 -- available.
1372 elsif Ada_Version >= Ada_2005
1373 and then Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1374 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1375 then
1376 null;
1378 else
1379 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1381 if Op_Name = Name_Op_Concat then
1382 Opnd_Type := Base_Type (Typ);
1384 elsif (Scope (Opnd_Type) = Standard_Standard
1385 and then Is_Binary)
1386 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1387 and then Is_Binary
1388 and then not Comes_From_Source (Opnd_Type))
1389 then
1390 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1391 end if;
1393 if Scope (Opnd_Type) = Standard_Standard then
1395 -- Verify that the scope contains a type that corresponds to
1396 -- the given literal. Optimize the case where Pack is Standard.
1398 if Pack /= Standard_Standard then
1400 if Opnd_Type = Universal_Integer then
1401 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1403 elsif Opnd_Type = Universal_Real then
1404 Orig_Type := Type_In_P (Is_Real_Type'Access);
1406 elsif Opnd_Type = Any_String then
1407 Orig_Type := Type_In_P (Is_String_Type'Access);
1409 elsif Opnd_Type = Any_Access then
1410 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1412 elsif Opnd_Type = Any_Composite then
1413 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1415 if Present (Orig_Type) then
1416 if Has_Private_Component (Orig_Type) then
1417 Orig_Type := Empty;
1418 else
1419 Set_Etype (Act1, Orig_Type);
1421 if Is_Binary then
1422 Set_Etype (Act2, Orig_Type);
1423 end if;
1424 end if;
1425 end if;
1427 else
1428 Orig_Type := Empty;
1429 end if;
1431 Error := No (Orig_Type);
1432 end if;
1434 elsif Ekind (Opnd_Type) = E_Allocator_Type
1435 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1436 then
1437 Error := True;
1439 -- If the type is defined elsewhere, and the operator is not
1440 -- defined in the given scope (by a renaming declaration, e.g.)
1441 -- then this is an error as well. If an extension of System is
1442 -- present, and the type may be defined there, Pack must be
1443 -- System itself.
1445 elsif Scope (Opnd_Type) /= Pack
1446 and then Scope (Op_Id) /= Pack
1447 and then (No (System_Aux_Id)
1448 or else Scope (Opnd_Type) /= System_Aux_Id
1449 or else Pack /= Scope (System_Aux_Id))
1450 then
1451 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1452 Error := True;
1453 else
1454 Error := not Operand_Type_In_Scope (Pack);
1455 end if;
1457 elsif Pack = Standard_Standard
1458 and then not Operand_Type_In_Scope (Standard_Standard)
1459 then
1460 Error := True;
1461 end if;
1462 end if;
1464 if Error then
1465 Error_Msg_Node_2 := Pack;
1466 Error_Msg_NE
1467 ("& not declared in&", N, Selector_Name (Name (N)));
1468 Set_Etype (N, Any_Type);
1469 return;
1471 -- Detect a mismatch between the context type and the result type
1472 -- in the named package, which is otherwise not detected if the
1473 -- operands are universal. Check is only needed if source entity is
1474 -- an operator, not a function that renames an operator.
1476 elsif Nkind (Parent (N)) /= N_Type_Conversion
1477 and then Ekind (Entity (Name (N))) = E_Operator
1478 and then Is_Numeric_Type (Typ)
1479 and then not Is_Universal_Numeric_Type (Typ)
1480 and then Scope (Base_Type (Typ)) /= Pack
1481 and then not In_Instance
1482 then
1483 if Is_Fixed_Point_Type (Typ)
1484 and then Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1485 then
1486 -- Already checked above
1488 null;
1490 -- Operator may be defined in an extension of System
1492 elsif Present (System_Aux_Id)
1493 and then Scope (Opnd_Type) = System_Aux_Id
1494 then
1495 null;
1497 else
1498 -- Could we use Wrong_Type here??? (this would require setting
1499 -- Etype (N) to the actual type found where Typ was expected).
1501 Error_Msg_NE ("expect }", N, Typ);
1502 end if;
1503 end if;
1504 end if;
1506 Set_Chars (Op_Node, Op_Name);
1508 if not Is_Private_Type (Etype (N)) then
1509 Set_Etype (Op_Node, Base_Type (Etype (N)));
1510 else
1511 Set_Etype (Op_Node, Etype (N));
1512 end if;
1514 -- If this is a call to a function that renames a predefined equality,
1515 -- the renaming declaration provides a type that must be used to
1516 -- resolve the operands. This must be done now because resolution of
1517 -- the equality node will not resolve any remaining ambiguity, and it
1518 -- assumes that the first operand is not overloaded.
1520 if Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1521 and then Ekind (Func) = E_Function
1522 and then Is_Overloaded (Act1)
1523 then
1524 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1525 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1526 end if;
1528 Set_Entity (Op_Node, Op_Id);
1529 Generate_Reference (Op_Id, N, ' ');
1531 -- Do rewrite setting Comes_From_Source on the result if the original
1532 -- call came from source. Although it is not strictly the case that the
1533 -- operator as such comes from the source, logically it corresponds
1534 -- exactly to the function call in the source, so it should be marked
1535 -- this way (e.g. to make sure that validity checks work fine).
1537 declare
1538 CS : constant Boolean := Comes_From_Source (N);
1539 begin
1540 Rewrite (N, Op_Node);
1541 Set_Comes_From_Source (N, CS);
1542 end;
1544 -- If this is an arithmetic operator and the result type is private,
1545 -- the operands and the result must be wrapped in conversion to
1546 -- expose the underlying numeric type and expand the proper checks,
1547 -- e.g. on division.
1549 if Is_Private_Type (Typ) then
1550 case Nkind (N) is
1551 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1552 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1553 Resolve_Intrinsic_Operator (N, Typ);
1555 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1556 Resolve_Intrinsic_Unary_Operator (N, Typ);
1558 when others =>
1559 Resolve (N, Typ);
1560 end case;
1561 else
1562 Resolve (N, Typ);
1563 end if;
1565 -- If in ASIS_Mode, propagate operand types to original actuals of
1566 -- function call, which would otherwise not be fully resolved. If
1567 -- the call has already been constant-folded, nothing to do. We
1568 -- relocate the operand nodes rather than copy them, to preserve
1569 -- original_node pointers, given that the operands themselves may
1570 -- have been rewritten. If the call was itself a rewriting of an
1571 -- operator node, nothing to do.
1573 if ASIS_Mode
1574 and then Nkind (N) in N_Op
1575 and then Nkind (Original_Node (N)) = N_Function_Call
1576 then
1577 if Is_Binary then
1578 Rewrite (First (Parameter_Associations (Original_Node (N))),
1579 Relocate_Node (Left_Opnd (N)));
1580 Rewrite (Next (First (Parameter_Associations (Original_Node (N)))),
1581 Relocate_Node (Right_Opnd (N)));
1582 else
1583 Rewrite (First (Parameter_Associations (Original_Node (N))),
1584 Relocate_Node (Right_Opnd (N)));
1585 end if;
1587 Set_Parent (Original_Node (N), Parent (N));
1588 end if;
1589 end Make_Call_Into_Operator;
1591 -------------------
1592 -- Operator_Kind --
1593 -------------------
1595 function Operator_Kind
1596 (Op_Name : Name_Id;
1597 Is_Binary : Boolean) return Node_Kind
1599 Kind : Node_Kind;
1601 begin
1602 -- Use CASE statement or array???
1604 if Is_Binary then
1605 if Op_Name = Name_Op_And then
1606 Kind := N_Op_And;
1607 elsif Op_Name = Name_Op_Or then
1608 Kind := N_Op_Or;
1609 elsif Op_Name = Name_Op_Xor then
1610 Kind := N_Op_Xor;
1611 elsif Op_Name = Name_Op_Eq then
1612 Kind := N_Op_Eq;
1613 elsif Op_Name = Name_Op_Ne then
1614 Kind := N_Op_Ne;
1615 elsif Op_Name = Name_Op_Lt then
1616 Kind := N_Op_Lt;
1617 elsif Op_Name = Name_Op_Le then
1618 Kind := N_Op_Le;
1619 elsif Op_Name = Name_Op_Gt then
1620 Kind := N_Op_Gt;
1621 elsif Op_Name = Name_Op_Ge then
1622 Kind := N_Op_Ge;
1623 elsif Op_Name = Name_Op_Add then
1624 Kind := N_Op_Add;
1625 elsif Op_Name = Name_Op_Subtract then
1626 Kind := N_Op_Subtract;
1627 elsif Op_Name = Name_Op_Concat then
1628 Kind := N_Op_Concat;
1629 elsif Op_Name = Name_Op_Multiply then
1630 Kind := N_Op_Multiply;
1631 elsif Op_Name = Name_Op_Divide then
1632 Kind := N_Op_Divide;
1633 elsif Op_Name = Name_Op_Mod then
1634 Kind := N_Op_Mod;
1635 elsif Op_Name = Name_Op_Rem then
1636 Kind := N_Op_Rem;
1637 elsif Op_Name = Name_Op_Expon then
1638 Kind := N_Op_Expon;
1639 else
1640 raise Program_Error;
1641 end if;
1643 -- Unary operators
1645 else
1646 if Op_Name = Name_Op_Add then
1647 Kind := N_Op_Plus;
1648 elsif Op_Name = Name_Op_Subtract then
1649 Kind := N_Op_Minus;
1650 elsif Op_Name = Name_Op_Abs then
1651 Kind := N_Op_Abs;
1652 elsif Op_Name = Name_Op_Not then
1653 Kind := N_Op_Not;
1654 else
1655 raise Program_Error;
1656 end if;
1657 end if;
1659 return Kind;
1660 end Operator_Kind;
1662 ----------------------------
1663 -- Preanalyze_And_Resolve --
1664 ----------------------------
1666 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1667 Save_Full_Analysis : constant Boolean := Full_Analysis;
1669 begin
1670 Full_Analysis := False;
1671 Expander_Mode_Save_And_Set (False);
1673 -- Normally, we suppress all checks for this preanalysis. There is no
1674 -- point in processing them now, since they will be applied properly
1675 -- and in the proper location when the default expressions reanalyzed
1676 -- and reexpanded later on. We will also have more information at that
1677 -- point for possible suppression of individual checks.
1679 -- However, in SPARK mode, most expansion is suppressed, and this
1680 -- later reanalysis and reexpansion may not occur. SPARK mode does
1681 -- require the setting of checking flags for proof purposes, so we
1682 -- do the SPARK preanalysis without suppressing checks.
1684 -- This special handling for SPARK mode is required for example in the
1685 -- case of Ada 2012 constructs such as quantified expressions, which are
1686 -- expanded in two separate steps.
1688 if GNATprove_Mode then
1689 Analyze_And_Resolve (N, T);
1690 else
1691 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1692 end if;
1694 Expander_Mode_Restore;
1695 Full_Analysis := Save_Full_Analysis;
1696 end Preanalyze_And_Resolve;
1698 -- Version without context type
1700 procedure Preanalyze_And_Resolve (N : Node_Id) is
1701 Save_Full_Analysis : constant Boolean := Full_Analysis;
1703 begin
1704 Full_Analysis := False;
1705 Expander_Mode_Save_And_Set (False);
1707 Analyze (N);
1708 Resolve (N, Etype (N), Suppress => All_Checks);
1710 Expander_Mode_Restore;
1711 Full_Analysis := Save_Full_Analysis;
1712 end Preanalyze_And_Resolve;
1714 ----------------------------------
1715 -- Replace_Actual_Discriminants --
1716 ----------------------------------
1718 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1719 Loc : constant Source_Ptr := Sloc (N);
1720 Tsk : Node_Id := Empty;
1722 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1723 -- Comment needed???
1725 -------------------
1726 -- Process_Discr --
1727 -------------------
1729 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1730 Ent : Entity_Id;
1732 begin
1733 if Nkind (Nod) = N_Identifier then
1734 Ent := Entity (Nod);
1736 if Present (Ent)
1737 and then Ekind (Ent) = E_Discriminant
1738 then
1739 Rewrite (Nod,
1740 Make_Selected_Component (Loc,
1741 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1742 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1744 Set_Etype (Nod, Etype (Ent));
1745 end if;
1747 end if;
1749 return OK;
1750 end Process_Discr;
1752 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1754 -- Start of processing for Replace_Actual_Discriminants
1756 begin
1757 if not Expander_Active then
1758 return;
1759 end if;
1761 if Nkind (Name (N)) = N_Selected_Component then
1762 Tsk := Prefix (Name (N));
1764 elsif Nkind (Name (N)) = N_Indexed_Component then
1765 Tsk := Prefix (Prefix (Name (N)));
1766 end if;
1768 if No (Tsk) then
1769 return;
1770 else
1771 Replace_Discrs (Default);
1772 end if;
1773 end Replace_Actual_Discriminants;
1775 -------------
1776 -- Resolve --
1777 -------------
1779 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1780 Ambiguous : Boolean := False;
1781 Ctx_Type : Entity_Id := Typ;
1782 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1783 Err_Type : Entity_Id := Empty;
1784 Found : Boolean := False;
1785 From_Lib : Boolean;
1786 I : Interp_Index;
1787 I1 : Interp_Index := 0; -- prevent junk warning
1788 It : Interp;
1789 It1 : Interp;
1790 Seen : Entity_Id := Empty; -- prevent junk warning
1792 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1793 -- Determine whether a node comes from a predefined library unit or
1794 -- Standard.
1796 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1797 -- Try and fix up a literal so that it matches its expected type. New
1798 -- literals are manufactured if necessary to avoid cascaded errors.
1800 procedure Report_Ambiguous_Argument;
1801 -- Additional diagnostics when an ambiguous call has an ambiguous
1802 -- argument (typically a controlling actual).
1804 procedure Resolution_Failed;
1805 -- Called when attempt at resolving current expression fails
1807 ------------------------------------
1808 -- Comes_From_Predefined_Lib_Unit --
1809 -------------------------------------
1811 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1812 begin
1813 return
1814 Sloc (Nod) = Standard_Location
1815 or else Is_Predefined_File_Name
1816 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1817 end Comes_From_Predefined_Lib_Unit;
1819 --------------------
1820 -- Patch_Up_Value --
1821 --------------------
1823 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1824 begin
1825 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1826 Rewrite (N,
1827 Make_Real_Literal (Sloc (N),
1828 Realval => UR_From_Uint (Intval (N))));
1829 Set_Etype (N, Universal_Real);
1830 Set_Is_Static_Expression (N);
1832 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1833 Rewrite (N,
1834 Make_Integer_Literal (Sloc (N),
1835 Intval => UR_To_Uint (Realval (N))));
1836 Set_Etype (N, Universal_Integer);
1837 Set_Is_Static_Expression (N);
1839 elsif Nkind (N) = N_String_Literal
1840 and then Is_Character_Type (Typ)
1841 then
1842 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1843 Rewrite (N,
1844 Make_Character_Literal (Sloc (N),
1845 Chars => Name_Find,
1846 Char_Literal_Value =>
1847 UI_From_Int (Character'Pos ('A'))));
1848 Set_Etype (N, Any_Character);
1849 Set_Is_Static_Expression (N);
1851 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1852 Rewrite (N,
1853 Make_String_Literal (Sloc (N),
1854 Strval => End_String));
1856 elsif Nkind (N) = N_Range then
1857 Patch_Up_Value (Low_Bound (N), Typ);
1858 Patch_Up_Value (High_Bound (N), Typ);
1859 end if;
1860 end Patch_Up_Value;
1862 -------------------------------
1863 -- Report_Ambiguous_Argument --
1864 -------------------------------
1866 procedure Report_Ambiguous_Argument is
1867 Arg : constant Node_Id := First (Parameter_Associations (N));
1868 I : Interp_Index;
1869 It : Interp;
1871 begin
1872 if Nkind (Arg) = N_Function_Call
1873 and then Is_Entity_Name (Name (Arg))
1874 and then Is_Overloaded (Name (Arg))
1875 then
1876 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1878 -- Could use comments on what is going on here???
1880 Get_First_Interp (Name (Arg), I, It);
1881 while Present (It.Nam) loop
1882 Error_Msg_Sloc := Sloc (It.Nam);
1884 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1885 Error_Msg_N ("interpretation (inherited) #!", Arg);
1886 else
1887 Error_Msg_N ("interpretation #!", Arg);
1888 end if;
1890 Get_Next_Interp (I, It);
1891 end loop;
1892 end if;
1893 end Report_Ambiguous_Argument;
1895 -----------------------
1896 -- Resolution_Failed --
1897 -----------------------
1899 procedure Resolution_Failed is
1900 begin
1901 Patch_Up_Value (N, Typ);
1902 Set_Etype (N, Typ);
1903 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1904 Set_Is_Overloaded (N, False);
1906 -- The caller will return without calling the expander, so we need
1907 -- to set the analyzed flag. Note that it is fine to set Analyzed
1908 -- to True even if we are in the middle of a shallow analysis,
1909 -- (see the spec of sem for more details) since this is an error
1910 -- situation anyway, and there is no point in repeating the
1911 -- analysis later (indeed it won't work to repeat it later, since
1912 -- we haven't got a clear resolution of which entity is being
1913 -- referenced.)
1915 Set_Analyzed (N, True);
1916 return;
1917 end Resolution_Failed;
1919 -- Start of processing for Resolve
1921 begin
1922 if N = Error then
1923 return;
1924 end if;
1926 -- Access attribute on remote subprogram cannot be used for a non-remote
1927 -- access-to-subprogram type.
1929 if Nkind (N) = N_Attribute_Reference
1930 and then Nam_In (Attribute_Name (N), Name_Access,
1931 Name_Unrestricted_Access,
1932 Name_Unchecked_Access)
1933 and then Comes_From_Source (N)
1934 and then Is_Entity_Name (Prefix (N))
1935 and then Is_Subprogram (Entity (Prefix (N)))
1936 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1937 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1938 then
1939 Error_Msg_N
1940 ("prefix must statically denote a non-remote subprogram", N);
1941 end if;
1943 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1945 -- If the context is a Remote_Access_To_Subprogram, access attributes
1946 -- must be resolved with the corresponding fat pointer. There is no need
1947 -- to check for the attribute name since the return type of an
1948 -- attribute is never a remote type.
1950 if Nkind (N) = N_Attribute_Reference
1951 and then Comes_From_Source (N)
1952 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1953 then
1954 declare
1955 Attr : constant Attribute_Id :=
1956 Get_Attribute_Id (Attribute_Name (N));
1957 Pref : constant Node_Id := Prefix (N);
1958 Decl : Node_Id;
1959 Spec : Node_Id;
1960 Is_Remote : Boolean := True;
1962 begin
1963 -- Check that Typ is a remote access-to-subprogram type
1965 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1967 -- Prefix (N) must statically denote a remote subprogram
1968 -- declared in a package specification.
1970 if Attr = Attribute_Access or else
1971 Attr = Attribute_Unchecked_Access or else
1972 Attr = Attribute_Unrestricted_Access
1973 then
1974 Decl := Unit_Declaration_Node (Entity (Pref));
1976 if Nkind (Decl) = N_Subprogram_Body then
1977 Spec := Corresponding_Spec (Decl);
1979 if Present (Spec) then
1980 Decl := Unit_Declaration_Node (Spec);
1981 end if;
1982 end if;
1984 Spec := Parent (Decl);
1986 if not Is_Entity_Name (Prefix (N))
1987 or else Nkind (Spec) /= N_Package_Specification
1988 or else
1989 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1990 then
1991 Is_Remote := False;
1992 Error_Msg_N
1993 ("prefix must statically denote a remote subprogram ",
1995 end if;
1997 -- If we are generating code in distributed mode, perform
1998 -- semantic checks against corresponding remote entities.
2000 if Expander_Active
2001 and then Get_PCS_Name /= Name_No_DSA
2002 then
2003 Check_Subtype_Conformant
2004 (New_Id => Entity (Prefix (N)),
2005 Old_Id => Designated_Type
2006 (Corresponding_Remote_Type (Typ)),
2007 Err_Loc => N);
2009 if Is_Remote then
2010 Process_Remote_AST_Attribute (N, Typ);
2011 end if;
2012 end if;
2013 end if;
2014 end if;
2015 end;
2016 end if;
2018 Debug_A_Entry ("resolving ", N);
2020 if Debug_Flag_V then
2021 Write_Overloads (N);
2022 end if;
2024 if Comes_From_Source (N) then
2025 if Is_Fixed_Point_Type (Typ) then
2026 Check_Restriction (No_Fixed_Point, N);
2028 elsif Is_Floating_Point_Type (Typ)
2029 and then Typ /= Universal_Real
2030 and then Typ /= Any_Real
2031 then
2032 Check_Restriction (No_Floating_Point, N);
2033 end if;
2034 end if;
2036 -- Return if already analyzed
2038 if Analyzed (N) then
2039 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2040 Analyze_Dimension (N);
2041 return;
2043 -- Any case of Any_Type as the Etype value means that we had a
2044 -- previous error.
2046 elsif Etype (N) = Any_Type then
2047 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2048 return;
2049 end if;
2051 Check_Parameterless_Call (N);
2053 -- The resolution of an Expression_With_Actions is determined by
2054 -- its Expression.
2056 if Nkind (N) = N_Expression_With_Actions then
2057 Resolve (Expression (N), Typ);
2059 Found := True;
2060 Expr_Type := Etype (Expression (N));
2062 -- If not overloaded, then we know the type, and all that needs doing
2063 -- is to check that this type is compatible with the context.
2065 elsif not Is_Overloaded (N) then
2066 Found := Covers (Typ, Etype (N));
2067 Expr_Type := Etype (N);
2069 -- In the overloaded case, we must select the interpretation that
2070 -- is compatible with the context (i.e. the type passed to Resolve)
2072 else
2073 -- Loop through possible interpretations
2075 Get_First_Interp (N, I, It);
2076 Interp_Loop : while Present (It.Typ) loop
2078 if Debug_Flag_V then
2079 Write_Str ("Interp: ");
2080 Write_Interp (It);
2081 end if;
2083 -- We are only interested in interpretations that are compatible
2084 -- with the expected type, any other interpretations are ignored.
2086 if not Covers (Typ, It.Typ) then
2087 if Debug_Flag_V then
2088 Write_Str (" interpretation incompatible with context");
2089 Write_Eol;
2090 end if;
2092 else
2093 -- Skip the current interpretation if it is disabled by an
2094 -- abstract operator. This action is performed only when the
2095 -- type against which we are resolving is the same as the
2096 -- type of the interpretation.
2098 if Ada_Version >= Ada_2005
2099 and then It.Typ = Typ
2100 and then Typ /= Universal_Integer
2101 and then Typ /= Universal_Real
2102 and then Present (It.Abstract_Op)
2103 then
2104 if Debug_Flag_V then
2105 Write_Line ("Skip.");
2106 end if;
2108 goto Continue;
2109 end if;
2111 -- First matching interpretation
2113 if not Found then
2114 Found := True;
2115 I1 := I;
2116 Seen := It.Nam;
2117 Expr_Type := It.Typ;
2119 -- Matching interpretation that is not the first, maybe an
2120 -- error, but there are some cases where preference rules are
2121 -- used to choose between the two possibilities. These and
2122 -- some more obscure cases are handled in Disambiguate.
2124 else
2125 -- If the current statement is part of a predefined library
2126 -- unit, then all interpretations which come from user level
2127 -- packages should not be considered. Check previous and
2128 -- current one.
2130 if From_Lib then
2131 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2132 goto Continue;
2134 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2136 -- Previous interpretation must be discarded
2138 I1 := I;
2139 Seen := It.Nam;
2140 Expr_Type := It.Typ;
2141 Set_Entity (N, Seen);
2142 goto Continue;
2143 end if;
2144 end if;
2146 -- Otherwise apply further disambiguation steps
2148 Error_Msg_Sloc := Sloc (Seen);
2149 It1 := Disambiguate (N, I1, I, Typ);
2151 -- Disambiguation has succeeded. Skip the remaining
2152 -- interpretations.
2154 if It1 /= No_Interp then
2155 Seen := It1.Nam;
2156 Expr_Type := It1.Typ;
2158 while Present (It.Typ) loop
2159 Get_Next_Interp (I, It);
2160 end loop;
2162 else
2163 -- Before we issue an ambiguity complaint, check for
2164 -- the case of a subprogram call where at least one
2165 -- of the arguments is Any_Type, and if so, suppress
2166 -- the message, since it is a cascaded error.
2168 if Nkind (N) in N_Subprogram_Call then
2169 declare
2170 A : Node_Id;
2171 E : Node_Id;
2173 begin
2174 A := First_Actual (N);
2175 while Present (A) loop
2176 E := A;
2178 if Nkind (E) = N_Parameter_Association then
2179 E := Explicit_Actual_Parameter (E);
2180 end if;
2182 if Etype (E) = Any_Type then
2183 if Debug_Flag_V then
2184 Write_Str ("Any_Type in call");
2185 Write_Eol;
2186 end if;
2188 exit Interp_Loop;
2189 end if;
2191 Next_Actual (A);
2192 end loop;
2193 end;
2195 elsif Nkind (N) in N_Binary_Op
2196 and then (Etype (Left_Opnd (N)) = Any_Type
2197 or else Etype (Right_Opnd (N)) = Any_Type)
2198 then
2199 exit Interp_Loop;
2201 elsif Nkind (N) in N_Unary_Op
2202 and then Etype (Right_Opnd (N)) = Any_Type
2203 then
2204 exit Interp_Loop;
2205 end if;
2207 -- Not that special case, so issue message using the
2208 -- flag Ambiguous to control printing of the header
2209 -- message only at the start of an ambiguous set.
2211 if not Ambiguous then
2212 if Nkind (N) = N_Function_Call
2213 and then Nkind (Name (N)) = N_Explicit_Dereference
2214 then
2215 Error_Msg_N
2216 ("ambiguous expression "
2217 & "(cannot resolve indirect call)!", N);
2218 else
2219 Error_Msg_NE -- CODEFIX
2220 ("ambiguous expression (cannot resolve&)!",
2221 N, It.Nam);
2222 end if;
2224 Ambiguous := True;
2226 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2227 Error_Msg_N
2228 ("\\possible interpretation (inherited)#!", N);
2229 else
2230 Error_Msg_N -- CODEFIX
2231 ("\\possible interpretation#!", N);
2232 end if;
2234 if Nkind (N) in N_Subprogram_Call
2235 and then Present (Parameter_Associations (N))
2236 then
2237 Report_Ambiguous_Argument;
2238 end if;
2239 end if;
2241 Error_Msg_Sloc := Sloc (It.Nam);
2243 -- By default, the error message refers to the candidate
2244 -- interpretation. But if it is a predefined operator, it
2245 -- is implicitly declared at the declaration of the type
2246 -- of the operand. Recover the sloc of that declaration
2247 -- for the error message.
2249 if Nkind (N) in N_Op
2250 and then Scope (It.Nam) = Standard_Standard
2251 and then not Is_Overloaded (Right_Opnd (N))
2252 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2253 Standard_Standard
2254 then
2255 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2257 if Comes_From_Source (Err_Type)
2258 and then Present (Parent (Err_Type))
2259 then
2260 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2261 end if;
2263 elsif Nkind (N) in N_Binary_Op
2264 and then Scope (It.Nam) = Standard_Standard
2265 and then not Is_Overloaded (Left_Opnd (N))
2266 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2267 Standard_Standard
2268 then
2269 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2271 if Comes_From_Source (Err_Type)
2272 and then Present (Parent (Err_Type))
2273 then
2274 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2275 end if;
2277 -- If this is an indirect call, use the subprogram_type
2278 -- in the message, to have a meaningful location. Also
2279 -- indicate if this is an inherited operation, created
2280 -- by a type declaration.
2282 elsif Nkind (N) = N_Function_Call
2283 and then Nkind (Name (N)) = N_Explicit_Dereference
2284 and then Is_Type (It.Nam)
2285 then
2286 Err_Type := It.Nam;
2287 Error_Msg_Sloc :=
2288 Sloc (Associated_Node_For_Itype (Err_Type));
2289 else
2290 Err_Type := Empty;
2291 end if;
2293 if Nkind (N) in N_Op
2294 and then Scope (It.Nam) = Standard_Standard
2295 and then Present (Err_Type)
2296 then
2297 -- Special-case the message for universal_fixed
2298 -- operators, which are not declared with the type
2299 -- of the operand, but appear forever in Standard.
2301 if It.Typ = Universal_Fixed
2302 and then Scope (It.Nam) = Standard_Standard
2303 then
2304 Error_Msg_N
2305 ("\\possible interpretation as universal_fixed "
2306 & "operation (RM 4.5.5 (19))", N);
2307 else
2308 Error_Msg_N
2309 ("\\possible interpretation (predefined)#!", N);
2310 end if;
2312 elsif
2313 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2314 then
2315 Error_Msg_N
2316 ("\\possible interpretation (inherited)#!", N);
2317 else
2318 Error_Msg_N -- CODEFIX
2319 ("\\possible interpretation#!", N);
2320 end if;
2322 end if;
2323 end if;
2325 -- We have a matching interpretation, Expr_Type is the type
2326 -- from this interpretation, and Seen is the entity.
2328 -- For an operator, just set the entity name. The type will be
2329 -- set by the specific operator resolution routine.
2331 if Nkind (N) in N_Op then
2332 Set_Entity (N, Seen);
2333 Generate_Reference (Seen, N);
2335 elsif Nkind (N) = N_Case_Expression then
2336 Set_Etype (N, Expr_Type);
2338 elsif Nkind (N) = N_Character_Literal then
2339 Set_Etype (N, Expr_Type);
2341 elsif Nkind (N) = N_If_Expression then
2342 Set_Etype (N, Expr_Type);
2344 -- AI05-0139-2: Expression is overloaded because type has
2345 -- implicit dereference. If type matches context, no implicit
2346 -- dereference is involved.
2348 elsif Has_Implicit_Dereference (Expr_Type) then
2349 Set_Etype (N, Expr_Type);
2350 Set_Is_Overloaded (N, False);
2351 exit Interp_Loop;
2353 elsif Is_Overloaded (N)
2354 and then Present (It.Nam)
2355 and then Ekind (It.Nam) = E_Discriminant
2356 and then Has_Implicit_Dereference (It.Nam)
2357 then
2358 -- If the node is a general indexing, the dereference is
2359 -- is inserted when resolving the rewritten form, else
2360 -- insert it now.
2362 if Nkind (N) /= N_Indexed_Component
2363 or else No (Generalized_Indexing (N))
2364 then
2365 Build_Explicit_Dereference (N, It.Nam);
2366 end if;
2368 -- For an explicit dereference, attribute reference, range,
2369 -- short-circuit form (which is not an operator node), or call
2370 -- with a name that is an explicit dereference, there is
2371 -- nothing to be done at this point.
2373 elsif Nkind_In (N, N_Explicit_Dereference,
2374 N_Attribute_Reference,
2375 N_And_Then,
2376 N_Indexed_Component,
2377 N_Or_Else,
2378 N_Range,
2379 N_Selected_Component,
2380 N_Slice)
2381 or else Nkind (Name (N)) = N_Explicit_Dereference
2382 then
2383 null;
2385 -- For procedure or function calls, set the type of the name,
2386 -- and also the entity pointer for the prefix.
2388 elsif Nkind (N) in N_Subprogram_Call
2389 and then Is_Entity_Name (Name (N))
2390 then
2391 Set_Etype (Name (N), Expr_Type);
2392 Set_Entity (Name (N), Seen);
2393 Generate_Reference (Seen, Name (N));
2395 elsif Nkind (N) = N_Function_Call
2396 and then Nkind (Name (N)) = N_Selected_Component
2397 then
2398 Set_Etype (Name (N), Expr_Type);
2399 Set_Entity (Selector_Name (Name (N)), Seen);
2400 Generate_Reference (Seen, Selector_Name (Name (N)));
2402 -- For all other cases, just set the type of the Name
2404 else
2405 Set_Etype (Name (N), Expr_Type);
2406 end if;
2408 end if;
2410 <<Continue>>
2412 -- Move to next interpretation
2414 exit Interp_Loop when No (It.Typ);
2416 Get_Next_Interp (I, It);
2417 end loop Interp_Loop;
2418 end if;
2420 -- At this stage Found indicates whether or not an acceptable
2421 -- interpretation exists. If not, then we have an error, except that if
2422 -- the context is Any_Type as a result of some other error, then we
2423 -- suppress the error report.
2425 if not Found then
2426 if Typ /= Any_Type then
2428 -- If type we are looking for is Void, then this is the procedure
2429 -- call case, and the error is simply that what we gave is not a
2430 -- procedure name (we think of procedure calls as expressions with
2431 -- types internally, but the user doesn't think of them this way).
2433 if Typ = Standard_Void_Type then
2435 -- Special case message if function used as a procedure
2437 if Nkind (N) = N_Procedure_Call_Statement
2438 and then Is_Entity_Name (Name (N))
2439 and then Ekind (Entity (Name (N))) = E_Function
2440 then
2441 Error_Msg_NE
2442 ("cannot use function & in a procedure call",
2443 Name (N), Entity (Name (N)));
2445 -- Otherwise give general message (not clear what cases this
2446 -- covers, but no harm in providing for them).
2448 else
2449 Error_Msg_N ("expect procedure name in procedure call", N);
2450 end if;
2452 Found := True;
2454 -- Otherwise we do have a subexpression with the wrong type
2456 -- Check for the case of an allocator which uses an access type
2457 -- instead of the designated type. This is a common error and we
2458 -- specialize the message, posting an error on the operand of the
2459 -- allocator, complaining that we expected the designated type of
2460 -- the allocator.
2462 elsif Nkind (N) = N_Allocator
2463 and then Is_Access_Type (Typ)
2464 and then Is_Access_Type (Etype (N))
2465 and then Designated_Type (Etype (N)) = Typ
2466 then
2467 Wrong_Type (Expression (N), Designated_Type (Typ));
2468 Found := True;
2470 -- Check for view mismatch on Null in instances, for which the
2471 -- view-swapping mechanism has no identifier.
2473 elsif (In_Instance or else In_Inlined_Body)
2474 and then (Nkind (N) = N_Null)
2475 and then Is_Private_Type (Typ)
2476 and then Is_Access_Type (Full_View (Typ))
2477 then
2478 Resolve (N, Full_View (Typ));
2479 Set_Etype (N, Typ);
2480 return;
2482 -- Check for an aggregate. Sometimes we can get bogus aggregates
2483 -- from misuse of parentheses, and we are about to complain about
2484 -- the aggregate without even looking inside it.
2486 -- Instead, if we have an aggregate of type Any_Composite, then
2487 -- analyze and resolve the component fields, and then only issue
2488 -- another message if we get no errors doing this (otherwise
2489 -- assume that the errors in the aggregate caused the problem).
2491 elsif Nkind (N) = N_Aggregate
2492 and then Etype (N) = Any_Composite
2493 then
2494 -- Disable expansion in any case. If there is a type mismatch
2495 -- it may be fatal to try to expand the aggregate. The flag
2496 -- would otherwise be set to false when the error is posted.
2498 Expander_Active := False;
2500 declare
2501 procedure Check_Aggr (Aggr : Node_Id);
2502 -- Check one aggregate, and set Found to True if we have a
2503 -- definite error in any of its elements
2505 procedure Check_Elmt (Aelmt : Node_Id);
2506 -- Check one element of aggregate and set Found to True if
2507 -- we definitely have an error in the element.
2509 ----------------
2510 -- Check_Aggr --
2511 ----------------
2513 procedure Check_Aggr (Aggr : Node_Id) is
2514 Elmt : Node_Id;
2516 begin
2517 if Present (Expressions (Aggr)) then
2518 Elmt := First (Expressions (Aggr));
2519 while Present (Elmt) loop
2520 Check_Elmt (Elmt);
2521 Next (Elmt);
2522 end loop;
2523 end if;
2525 if Present (Component_Associations (Aggr)) then
2526 Elmt := First (Component_Associations (Aggr));
2527 while Present (Elmt) loop
2529 -- If this is a default-initialized component, then
2530 -- there is nothing to check. The box will be
2531 -- replaced by the appropriate call during late
2532 -- expansion.
2534 if not Box_Present (Elmt) then
2535 Check_Elmt (Expression (Elmt));
2536 end if;
2538 Next (Elmt);
2539 end loop;
2540 end if;
2541 end Check_Aggr;
2543 ----------------
2544 -- Check_Elmt --
2545 ----------------
2547 procedure Check_Elmt (Aelmt : Node_Id) is
2548 begin
2549 -- If we have a nested aggregate, go inside it (to
2550 -- attempt a naked analyze-resolve of the aggregate can
2551 -- cause undesirable cascaded errors). Do not resolve
2552 -- expression if it needs a type from context, as for
2553 -- integer * fixed expression.
2555 if Nkind (Aelmt) = N_Aggregate then
2556 Check_Aggr (Aelmt);
2558 else
2559 Analyze (Aelmt);
2561 if not Is_Overloaded (Aelmt)
2562 and then Etype (Aelmt) /= Any_Fixed
2563 then
2564 Resolve (Aelmt);
2565 end if;
2567 if Etype (Aelmt) = Any_Type then
2568 Found := True;
2569 end if;
2570 end if;
2571 end Check_Elmt;
2573 begin
2574 Check_Aggr (N);
2575 end;
2576 end if;
2578 -- Looks like we have a type error, but check for special case
2579 -- of Address wanted, integer found, with the configuration pragma
2580 -- Allow_Integer_Address active. If we have this case, introduce
2581 -- an unchecked conversion to allow the integer expression to be
2582 -- treated as an Address. The reverse case of integer wanted,
2583 -- Address found, is treated in an analogous manner.
2585 if Address_Integer_Convert_OK (Typ, Etype (N)) then
2586 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
2587 Analyze_And_Resolve (N, Typ);
2588 return;
2589 end if;
2591 -- That special Allow_Integer_Address check did not appply, so we
2592 -- have a real type error. If an error message was issued already,
2593 -- Found got reset to True, so if it's still False, issue standard
2594 -- Wrong_Type message.
2596 if not Found then
2597 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
2598 declare
2599 Subp_Name : Node_Id;
2601 begin
2602 if Is_Entity_Name (Name (N)) then
2603 Subp_Name := Name (N);
2605 elsif Nkind (Name (N)) = N_Selected_Component then
2607 -- Protected operation: retrieve operation name
2609 Subp_Name := Selector_Name (Name (N));
2611 else
2612 raise Program_Error;
2613 end if;
2615 Error_Msg_Node_2 := Typ;
2616 Error_Msg_NE
2617 ("no visible interpretation of& "
2618 & "matches expected type&", N, Subp_Name);
2619 end;
2621 if All_Errors_Mode then
2622 declare
2623 Index : Interp_Index;
2624 It : Interp;
2626 begin
2627 Error_Msg_N ("\\possible interpretations:", N);
2629 Get_First_Interp (Name (N), Index, It);
2630 while Present (It.Nam) loop
2631 Error_Msg_Sloc := Sloc (It.Nam);
2632 Error_Msg_Node_2 := It.Nam;
2633 Error_Msg_NE
2634 ("\\ type& for & declared#", N, It.Typ);
2635 Get_Next_Interp (Index, It);
2636 end loop;
2637 end;
2639 else
2640 Error_Msg_N ("\use -gnatf for details", N);
2641 end if;
2643 else
2644 Wrong_Type (N, Typ);
2645 end if;
2646 end if;
2647 end if;
2649 Resolution_Failed;
2650 return;
2652 -- Test if we have more than one interpretation for the context
2654 elsif Ambiguous then
2655 Resolution_Failed;
2656 return;
2658 -- Only one intepretation
2660 else
2661 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2662 -- the "+" on T is abstract, and the operands are of universal type,
2663 -- the above code will have (incorrectly) resolved the "+" to the
2664 -- universal one in Standard. Therefore check for this case and give
2665 -- an error. We can't do this earlier, because it would cause legal
2666 -- cases to get errors (when some other type has an abstract "+").
2668 if Ada_Version >= Ada_2005
2669 and then Nkind (N) in N_Op
2670 and then Is_Overloaded (N)
2671 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
2672 then
2673 Get_First_Interp (N, I, It);
2674 while Present (It.Typ) loop
2675 if Present (It.Abstract_Op) and then
2676 Etype (It.Abstract_Op) = Typ
2677 then
2678 Error_Msg_NE
2679 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2680 return;
2681 end if;
2683 Get_Next_Interp (I, It);
2684 end loop;
2685 end if;
2687 -- Here we have an acceptable interpretation for the context
2689 -- Propagate type information and normalize tree for various
2690 -- predefined operations. If the context only imposes a class of
2691 -- types, rather than a specific type, propagate the actual type
2692 -- downward.
2694 if Typ = Any_Integer or else
2695 Typ = Any_Boolean or else
2696 Typ = Any_Modular or else
2697 Typ = Any_Real or else
2698 Typ = Any_Discrete
2699 then
2700 Ctx_Type := Expr_Type;
2702 -- Any_Fixed is legal in a real context only if a specific fixed-
2703 -- point type is imposed. If Norman Cohen can be confused by this,
2704 -- it deserves a separate message.
2706 if Typ = Any_Real
2707 and then Expr_Type = Any_Fixed
2708 then
2709 Error_Msg_N ("illegal context for mixed mode operation", N);
2710 Set_Etype (N, Universal_Real);
2711 Ctx_Type := Universal_Real;
2712 end if;
2713 end if;
2715 -- A user-defined operator is transformed into a function call at
2716 -- this point, so that further processing knows that operators are
2717 -- really operators (i.e. are predefined operators). User-defined
2718 -- operators that are intrinsic are just renamings of the predefined
2719 -- ones, and need not be turned into calls either, but if they rename
2720 -- a different operator, we must transform the node accordingly.
2721 -- Instantiations of Unchecked_Conversion are intrinsic but are
2722 -- treated as functions, even if given an operator designator.
2724 if Nkind (N) in N_Op
2725 and then Present (Entity (N))
2726 and then Ekind (Entity (N)) /= E_Operator
2727 then
2729 if not Is_Predefined_Op (Entity (N)) then
2730 Rewrite_Operator_As_Call (N, Entity (N));
2732 elsif Present (Alias (Entity (N)))
2733 and then
2734 Nkind (Parent (Parent (Entity (N)))) =
2735 N_Subprogram_Renaming_Declaration
2736 then
2737 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2739 -- If the node is rewritten, it will be fully resolved in
2740 -- Rewrite_Renamed_Operator.
2742 if Analyzed (N) then
2743 return;
2744 end if;
2745 end if;
2746 end if;
2748 case N_Subexpr'(Nkind (N)) is
2750 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2752 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2754 when N_Short_Circuit
2755 => Resolve_Short_Circuit (N, Ctx_Type);
2757 when N_Attribute_Reference
2758 => Resolve_Attribute (N, Ctx_Type);
2760 when N_Case_Expression
2761 => Resolve_Case_Expression (N, Ctx_Type);
2763 when N_Character_Literal
2764 => Resolve_Character_Literal (N, Ctx_Type);
2766 when N_Expanded_Name
2767 => Resolve_Entity_Name (N, Ctx_Type);
2769 when N_Explicit_Dereference
2770 => Resolve_Explicit_Dereference (N, Ctx_Type);
2772 when N_Expression_With_Actions
2773 => Resolve_Expression_With_Actions (N, Ctx_Type);
2775 when N_Extension_Aggregate
2776 => Resolve_Extension_Aggregate (N, Ctx_Type);
2778 when N_Function_Call
2779 => Resolve_Call (N, Ctx_Type);
2781 when N_Identifier
2782 => Resolve_Entity_Name (N, Ctx_Type);
2784 when N_If_Expression
2785 => Resolve_If_Expression (N, Ctx_Type);
2787 when N_Indexed_Component
2788 => Resolve_Indexed_Component (N, Ctx_Type);
2790 when N_Integer_Literal
2791 => Resolve_Integer_Literal (N, Ctx_Type);
2793 when N_Membership_Test
2794 => Resolve_Membership_Op (N, Ctx_Type);
2796 when N_Null => Resolve_Null (N, Ctx_Type);
2798 when N_Op_And | N_Op_Or | N_Op_Xor
2799 => Resolve_Logical_Op (N, Ctx_Type);
2801 when N_Op_Eq | N_Op_Ne
2802 => Resolve_Equality_Op (N, Ctx_Type);
2804 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2805 => Resolve_Comparison_Op (N, Ctx_Type);
2807 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2809 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2810 N_Op_Divide | N_Op_Mod | N_Op_Rem
2812 => Resolve_Arithmetic_Op (N, Ctx_Type);
2814 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2816 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2818 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2819 => Resolve_Unary_Op (N, Ctx_Type);
2821 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2823 when N_Procedure_Call_Statement
2824 => Resolve_Call (N, Ctx_Type);
2826 when N_Operator_Symbol
2827 => Resolve_Operator_Symbol (N, Ctx_Type);
2829 when N_Qualified_Expression
2830 => Resolve_Qualified_Expression (N, Ctx_Type);
2832 -- Why is the following null, needs a comment ???
2834 when N_Quantified_Expression
2835 => null;
2837 when N_Raise_Expression
2838 => Resolve_Raise_Expression (N, Ctx_Type);
2840 when N_Raise_xxx_Error
2841 => Set_Etype (N, Ctx_Type);
2843 when N_Range => Resolve_Range (N, Ctx_Type);
2845 when N_Real_Literal
2846 => Resolve_Real_Literal (N, Ctx_Type);
2848 when N_Reference => Resolve_Reference (N, Ctx_Type);
2850 when N_Selected_Component
2851 => Resolve_Selected_Component (N, Ctx_Type);
2853 when N_Slice => Resolve_Slice (N, Ctx_Type);
2855 when N_String_Literal
2856 => Resolve_String_Literal (N, Ctx_Type);
2858 when N_Type_Conversion
2859 => Resolve_Type_Conversion (N, Ctx_Type);
2861 when N_Unchecked_Expression =>
2862 Resolve_Unchecked_Expression (N, Ctx_Type);
2864 when N_Unchecked_Type_Conversion =>
2865 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2866 end case;
2868 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2869 -- expression of an anonymous access type that occurs in the context
2870 -- of a named general access type, except when the expression is that
2871 -- of a membership test. This ensures proper legality checking in
2872 -- terms of allowed conversions (expressions that would be illegal to
2873 -- convert implicitly are allowed in membership tests).
2875 if Ada_Version >= Ada_2012
2876 and then Ekind (Ctx_Type) = E_General_Access_Type
2877 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2878 and then Nkind (Parent (N)) not in N_Membership_Test
2879 then
2880 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2881 Analyze_And_Resolve (N, Ctx_Type);
2882 end if;
2884 -- If the subexpression was replaced by a non-subexpression, then
2885 -- all we do is to expand it. The only legitimate case we know of
2886 -- is converting procedure call statement to entry call statements,
2887 -- but there may be others, so we are making this test general.
2889 if Nkind (N) not in N_Subexpr then
2890 Debug_A_Exit ("resolving ", N, " (done)");
2891 Expand (N);
2892 return;
2893 end if;
2895 -- The expression is definitely NOT overloaded at this point, so
2896 -- we reset the Is_Overloaded flag to avoid any confusion when
2897 -- reanalyzing the node.
2899 Set_Is_Overloaded (N, False);
2901 -- Freeze expression type, entity if it is a name, and designated
2902 -- type if it is an allocator (RM 13.14(10,11,13)).
2904 -- Now that the resolution of the type of the node is complete, and
2905 -- we did not detect an error, we can expand this node. We skip the
2906 -- expand call if we are in a default expression, see section
2907 -- "Handling of Default Expressions" in Sem spec.
2909 Debug_A_Exit ("resolving ", N, " (done)");
2911 -- We unconditionally freeze the expression, even if we are in
2912 -- default expression mode (the Freeze_Expression routine tests this
2913 -- flag and only freezes static types if it is set).
2915 -- Ada 2012 (AI05-177): The declaration of an expression function
2916 -- does not cause freezing, but we never reach here in that case.
2917 -- Here we are resolving the corresponding expanded body, so we do
2918 -- need to perform normal freezing.
2920 Freeze_Expression (N);
2922 -- Now we can do the expansion
2924 Expand (N);
2925 end if;
2926 end Resolve;
2928 -------------
2929 -- Resolve --
2930 -------------
2932 -- Version with check(s) suppressed
2934 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2935 begin
2936 if Suppress = All_Checks then
2937 declare
2938 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
2939 begin
2940 Scope_Suppress.Suppress := (others => True);
2941 Resolve (N, Typ);
2942 Scope_Suppress.Suppress := Sva;
2943 end;
2945 else
2946 declare
2947 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
2948 begin
2949 Scope_Suppress.Suppress (Suppress) := True;
2950 Resolve (N, Typ);
2951 Scope_Suppress.Suppress (Suppress) := Svg;
2952 end;
2953 end if;
2954 end Resolve;
2956 -------------
2957 -- Resolve --
2958 -------------
2960 -- Version with implicit type
2962 procedure Resolve (N : Node_Id) is
2963 begin
2964 Resolve (N, Etype (N));
2965 end Resolve;
2967 ---------------------
2968 -- Resolve_Actuals --
2969 ---------------------
2971 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2972 Loc : constant Source_Ptr := Sloc (N);
2973 A : Node_Id;
2974 A_Id : Entity_Id;
2975 A_Typ : Entity_Id;
2976 F : Entity_Id;
2977 F_Typ : Entity_Id;
2978 Prev : Node_Id := Empty;
2979 Orig_A : Node_Id;
2981 procedure Check_Aliased_Parameter;
2982 -- Check rules on aliased parameters and related accessibility rules
2983 -- in (RM 3.10.2 (10.2-10.4)).
2985 procedure Check_Argument_Order;
2986 -- Performs a check for the case where the actuals are all simple
2987 -- identifiers that correspond to the formal names, but in the wrong
2988 -- order, which is considered suspicious and cause for a warning.
2990 procedure Check_Prefixed_Call;
2991 -- If the original node is an overloaded call in prefix notation,
2992 -- insert an 'Access or a dereference as needed over the first actual.
2993 -- Try_Object_Operation has already verified that there is a valid
2994 -- interpretation, but the form of the actual can only be determined
2995 -- once the primitive operation is identified.
2997 procedure Insert_Default;
2998 -- If the actual is missing in a call, insert in the actuals list
2999 -- an instance of the default expression. The insertion is always
3000 -- a named association.
3002 procedure Property_Error
3003 (Var : Node_Id;
3004 Var_Id : Entity_Id;
3005 Prop_Nam : Name_Id);
3006 -- Emit an error concerning variable Var with entity Var_Id that has
3007 -- enabled property Prop_Nam when it acts as an actual parameter in a
3008 -- call and the corresponding formal parameter is of mode IN.
3010 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3011 -- Check whether T1 and T2, or their full views, are derived from a
3012 -- common type. Used to enforce the restrictions on array conversions
3013 -- of AI95-00246.
3015 function Static_Concatenation (N : Node_Id) return Boolean;
3016 -- Predicate to determine whether an actual that is a concatenation
3017 -- will be evaluated statically and does not need a transient scope.
3018 -- This must be determined before the actual is resolved and expanded
3019 -- because if needed the transient scope must be introduced earlier.
3021 ------------------------------
3022 -- Check_Aliased_Parameter --
3023 ------------------------------
3025 procedure Check_Aliased_Parameter is
3026 Nominal_Subt : Entity_Id;
3028 begin
3029 if Is_Aliased (F) then
3030 if Is_Tagged_Type (A_Typ) then
3031 null;
3033 elsif Is_Aliased_View (A) then
3034 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3035 Nominal_Subt := Base_Type (A_Typ);
3036 else
3037 Nominal_Subt := A_Typ;
3038 end if;
3040 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3041 null;
3043 -- In a generic body assume the worst for generic formals:
3044 -- they can have a constrained partial view (AI05-041).
3046 elsif Has_Discriminants (F_Typ)
3047 and then not Is_Constrained (F_Typ)
3048 and then not Has_Constrained_Partial_View (F_Typ)
3049 and then not Is_Generic_Type (F_Typ)
3050 then
3051 null;
3053 else
3054 Error_Msg_NE ("untagged actual does not match "
3055 & "aliased formal&", A, F);
3056 end if;
3058 else
3059 Error_Msg_NE ("actual for aliased formal& must be "
3060 & "aliased object", A, F);
3061 end if;
3063 if Ekind (Nam) = E_Procedure then
3064 null;
3066 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3067 if Nkind (Parent (N)) = N_Type_Conversion
3068 and then Type_Access_Level (Etype (Parent (N))) <
3069 Object_Access_Level (A)
3070 then
3071 Error_Msg_N ("aliased actual has wrong accessibility", A);
3072 end if;
3074 elsif Nkind (Parent (N)) = N_Qualified_Expression
3075 and then Nkind (Parent (Parent (N))) = N_Allocator
3076 and then Type_Access_Level (Etype (Parent (Parent (N)))) <
3077 Object_Access_Level (A)
3078 then
3079 Error_Msg_N
3080 ("aliased actual in allocator has wrong accessibility", A);
3081 end if;
3082 end if;
3083 end Check_Aliased_Parameter;
3085 --------------------------
3086 -- Check_Argument_Order --
3087 --------------------------
3089 procedure Check_Argument_Order is
3090 begin
3091 -- Nothing to do if no parameters, or original node is neither a
3092 -- function call nor a procedure call statement (happens in the
3093 -- operator-transformed-to-function call case), or the call does
3094 -- not come from source, or this warning is off.
3096 if not Warn_On_Parameter_Order
3097 or else No (Parameter_Associations (N))
3098 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3099 or else not Comes_From_Source (N)
3100 then
3101 return;
3102 end if;
3104 declare
3105 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3107 begin
3108 -- Nothing to do if only one parameter
3110 if Nargs < 2 then
3111 return;
3112 end if;
3114 -- Here if at least two arguments
3116 declare
3117 Actuals : array (1 .. Nargs) of Node_Id;
3118 Actual : Node_Id;
3119 Formal : Node_Id;
3121 Wrong_Order : Boolean := False;
3122 -- Set True if an out of order case is found
3124 begin
3125 -- Collect identifier names of actuals, fail if any actual is
3126 -- not a simple identifier, and record max length of name.
3128 Actual := First (Parameter_Associations (N));
3129 for J in Actuals'Range loop
3130 if Nkind (Actual) /= N_Identifier then
3131 return;
3132 else
3133 Actuals (J) := Actual;
3134 Next (Actual);
3135 end if;
3136 end loop;
3138 -- If we got this far, all actuals are identifiers and the list
3139 -- of their names is stored in the Actuals array.
3141 Formal := First_Formal (Nam);
3142 for J in Actuals'Range loop
3144 -- If we ran out of formals, that's odd, probably an error
3145 -- which will be detected elsewhere, but abandon the search.
3147 if No (Formal) then
3148 return;
3149 end if;
3151 -- If name matches and is in order OK
3153 if Chars (Formal) = Chars (Actuals (J)) then
3154 null;
3156 else
3157 -- If no match, see if it is elsewhere in list and if so
3158 -- flag potential wrong order if type is compatible.
3160 for K in Actuals'Range loop
3161 if Chars (Formal) = Chars (Actuals (K))
3162 and then
3163 Has_Compatible_Type (Actuals (K), Etype (Formal))
3164 then
3165 Wrong_Order := True;
3166 goto Continue;
3167 end if;
3168 end loop;
3170 -- No match
3172 return;
3173 end if;
3175 <<Continue>> Next_Formal (Formal);
3176 end loop;
3178 -- If Formals left over, also probably an error, skip warning
3180 if Present (Formal) then
3181 return;
3182 end if;
3184 -- Here we give the warning if something was out of order
3186 if Wrong_Order then
3187 Error_Msg_N
3188 ("?P?actuals for this call may be in wrong order", N);
3189 end if;
3190 end;
3191 end;
3192 end Check_Argument_Order;
3194 -------------------------
3195 -- Check_Prefixed_Call --
3196 -------------------------
3198 procedure Check_Prefixed_Call is
3199 Act : constant Node_Id := First_Actual (N);
3200 A_Type : constant Entity_Id := Etype (Act);
3201 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3202 Orig : constant Node_Id := Original_Node (N);
3203 New_A : Node_Id;
3205 begin
3206 -- Check whether the call is a prefixed call, with or without
3207 -- additional actuals.
3209 if Nkind (Orig) = N_Selected_Component
3210 or else
3211 (Nkind (Orig) = N_Indexed_Component
3212 and then Nkind (Prefix (Orig)) = N_Selected_Component
3213 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3214 and then Is_Entity_Name (Act)
3215 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3216 then
3217 if Is_Access_Type (A_Type)
3218 and then not Is_Access_Type (F_Type)
3219 then
3220 -- Introduce dereference on object in prefix
3222 New_A :=
3223 Make_Explicit_Dereference (Sloc (Act),
3224 Prefix => Relocate_Node (Act));
3225 Rewrite (Act, New_A);
3226 Analyze (Act);
3228 elsif Is_Access_Type (F_Type)
3229 and then not Is_Access_Type (A_Type)
3230 then
3231 -- Introduce an implicit 'Access in prefix
3233 if not Is_Aliased_View (Act) then
3234 Error_Msg_NE
3235 ("object in prefixed call to& must be aliased"
3236 & " (RM-2005 4.3.1 (13))",
3237 Prefix (Act), Nam);
3238 end if;
3240 Rewrite (Act,
3241 Make_Attribute_Reference (Loc,
3242 Attribute_Name => Name_Access,
3243 Prefix => Relocate_Node (Act)));
3244 end if;
3246 Analyze (Act);
3247 end if;
3248 end Check_Prefixed_Call;
3250 --------------------
3251 -- Insert_Default --
3252 --------------------
3254 procedure Insert_Default is
3255 Actval : Node_Id;
3256 Assoc : Node_Id;
3258 begin
3259 -- Missing argument in call, nothing to insert
3261 if No (Default_Value (F)) then
3262 return;
3264 else
3265 -- Note that we do a full New_Copy_Tree, so that any associated
3266 -- Itypes are properly copied. This may not be needed any more,
3267 -- but it does no harm as a safety measure. Defaults of a generic
3268 -- formal may be out of bounds of the corresponding actual (see
3269 -- cc1311b) and an additional check may be required.
3271 Actval :=
3272 New_Copy_Tree
3273 (Default_Value (F),
3274 New_Scope => Current_Scope,
3275 New_Sloc => Loc);
3277 if Is_Concurrent_Type (Scope (Nam))
3278 and then Has_Discriminants (Scope (Nam))
3279 then
3280 Replace_Actual_Discriminants (N, Actval);
3281 end if;
3283 if Is_Overloadable (Nam)
3284 and then Present (Alias (Nam))
3285 then
3286 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3287 and then not Is_Tagged_Type (Etype (F))
3288 then
3289 -- If default is a real literal, do not introduce a
3290 -- conversion whose effect may depend on the run-time
3291 -- size of universal real.
3293 if Nkind (Actval) = N_Real_Literal then
3294 Set_Etype (Actval, Base_Type (Etype (F)));
3295 else
3296 Actval := Unchecked_Convert_To (Etype (F), Actval);
3297 end if;
3298 end if;
3300 if Is_Scalar_Type (Etype (F)) then
3301 Enable_Range_Check (Actval);
3302 end if;
3304 Set_Parent (Actval, N);
3306 -- Resolve aggregates with their base type, to avoid scope
3307 -- anomalies: the subtype was first built in the subprogram
3308 -- declaration, and the current call may be nested.
3310 if Nkind (Actval) = N_Aggregate then
3311 Analyze_And_Resolve (Actval, Etype (F));
3312 else
3313 Analyze_And_Resolve (Actval, Etype (Actval));
3314 end if;
3316 else
3317 Set_Parent (Actval, N);
3319 -- See note above concerning aggregates
3321 if Nkind (Actval) = N_Aggregate
3322 and then Has_Discriminants (Etype (Actval))
3323 then
3324 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3326 -- Resolve entities with their own type, which may differ from
3327 -- the type of a reference in a generic context (the view
3328 -- swapping mechanism did not anticipate the re-analysis of
3329 -- default values in calls).
3331 elsif Is_Entity_Name (Actval) then
3332 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3334 else
3335 Analyze_And_Resolve (Actval, Etype (Actval));
3336 end if;
3337 end if;
3339 -- If default is a tag indeterminate function call, propagate tag
3340 -- to obtain proper dispatching.
3342 if Is_Controlling_Formal (F)
3343 and then Nkind (Default_Value (F)) = N_Function_Call
3344 then
3345 Set_Is_Controlling_Actual (Actval);
3346 end if;
3348 end if;
3350 -- If the default expression raises constraint error, then just
3351 -- silently replace it with an N_Raise_Constraint_Error node, since
3352 -- we already gave the warning on the subprogram spec. If node is
3353 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3354 -- the warnings removal machinery.
3356 if Raises_Constraint_Error (Actval)
3357 and then Nkind (Actval) /= N_Raise_Constraint_Error
3358 then
3359 Rewrite (Actval,
3360 Make_Raise_Constraint_Error (Loc,
3361 Reason => CE_Range_Check_Failed));
3362 Set_Raises_Constraint_Error (Actval);
3363 Set_Etype (Actval, Etype (F));
3364 end if;
3366 Assoc :=
3367 Make_Parameter_Association (Loc,
3368 Explicit_Actual_Parameter => Actval,
3369 Selector_Name => Make_Identifier (Loc, Chars (F)));
3371 -- Case of insertion is first named actual
3373 if No (Prev) or else
3374 Nkind (Parent (Prev)) /= N_Parameter_Association
3375 then
3376 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3377 Set_First_Named_Actual (N, Actval);
3379 if No (Prev) then
3380 if No (Parameter_Associations (N)) then
3381 Set_Parameter_Associations (N, New_List (Assoc));
3382 else
3383 Append (Assoc, Parameter_Associations (N));
3384 end if;
3386 else
3387 Insert_After (Prev, Assoc);
3388 end if;
3390 -- Case of insertion is not first named actual
3392 else
3393 Set_Next_Named_Actual
3394 (Assoc, Next_Named_Actual (Parent (Prev)));
3395 Set_Next_Named_Actual (Parent (Prev), Actval);
3396 Append (Assoc, Parameter_Associations (N));
3397 end if;
3399 Mark_Rewrite_Insertion (Assoc);
3400 Mark_Rewrite_Insertion (Actval);
3402 Prev := Actval;
3403 end Insert_Default;
3405 --------------------
3406 -- Property_Error --
3407 --------------------
3409 procedure Property_Error
3410 (Var : Node_Id;
3411 Var_Id : Entity_Id;
3412 Prop_Nam : Name_Id)
3414 begin
3415 Error_Msg_Name_1 := Prop_Nam;
3416 Error_Msg_NE
3417 ("external variable & with enabled property % cannot appear as "
3418 & "actual in procedure call (SPARK RM 7.1.3(11))", Var, Var_Id);
3419 Error_Msg_N ("\\corresponding formal parameter has mode In", Var);
3420 end Property_Error;
3422 -------------------
3423 -- Same_Ancestor --
3424 -------------------
3426 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3427 FT1 : Entity_Id := T1;
3428 FT2 : Entity_Id := T2;
3430 begin
3431 if Is_Private_Type (T1)
3432 and then Present (Full_View (T1))
3433 then
3434 FT1 := Full_View (T1);
3435 end if;
3437 if Is_Private_Type (T2)
3438 and then Present (Full_View (T2))
3439 then
3440 FT2 := Full_View (T2);
3441 end if;
3443 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3444 end Same_Ancestor;
3446 --------------------------
3447 -- Static_Concatenation --
3448 --------------------------
3450 function Static_Concatenation (N : Node_Id) return Boolean is
3451 begin
3452 case Nkind (N) is
3453 when N_String_Literal =>
3454 return True;
3456 when N_Op_Concat =>
3458 -- Concatenation is static when both operands are static and
3459 -- the concatenation operator is a predefined one.
3461 return Scope (Entity (N)) = Standard_Standard
3462 and then
3463 Static_Concatenation (Left_Opnd (N))
3464 and then
3465 Static_Concatenation (Right_Opnd (N));
3467 when others =>
3468 if Is_Entity_Name (N) then
3469 declare
3470 Ent : constant Entity_Id := Entity (N);
3471 begin
3472 return Ekind (Ent) = E_Constant
3473 and then Present (Constant_Value (Ent))
3474 and then
3475 Is_OK_Static_Expression (Constant_Value (Ent));
3476 end;
3478 else
3479 return False;
3480 end if;
3481 end case;
3482 end Static_Concatenation;
3484 -- Start of processing for Resolve_Actuals
3486 begin
3487 Check_Argument_Order;
3488 Check_Function_Writable_Actuals (N);
3490 if Present (First_Actual (N)) then
3491 Check_Prefixed_Call;
3492 end if;
3494 A := First_Actual (N);
3495 F := First_Formal (Nam);
3496 while Present (F) loop
3497 if No (A) and then Needs_No_Actuals (Nam) then
3498 null;
3500 -- If we have an error in any actual or formal, indicated by a type
3501 -- of Any_Type, then abandon resolution attempt, and set result type
3502 -- to Any_Type. Skip this if the actual is a Raise_Expression, whose
3503 -- type is imposed from context.
3505 elsif (Present (A) and then Etype (A) = Any_Type)
3506 or else Etype (F) = Any_Type
3507 then
3508 if Nkind (A) /= N_Raise_Expression then
3509 Set_Etype (N, Any_Type);
3510 return;
3511 end if;
3512 end if;
3514 -- Case where actual is present
3516 -- If the actual is an entity, generate a reference to it now. We
3517 -- do this before the actual is resolved, because a formal of some
3518 -- protected subprogram, or a task discriminant, will be rewritten
3519 -- during expansion, and the source entity reference may be lost.
3521 if Present (A)
3522 and then Is_Entity_Name (A)
3523 and then Comes_From_Source (N)
3524 then
3525 Orig_A := Entity (A);
3527 if Present (Orig_A) then
3528 if Is_Formal (Orig_A)
3529 and then Ekind (F) /= E_In_Parameter
3530 then
3531 Generate_Reference (Orig_A, A, 'm');
3533 elsif not Is_Overloaded (A) then
3534 if Ekind (F) /= E_Out_Parameter then
3535 Generate_Reference (Orig_A, A);
3537 -- RM 6.4.1(12): For an out parameter that is passed by
3538 -- copy, the formal parameter object is created, and:
3540 -- * For an access type, the formal parameter is initialized
3541 -- from the value of the actual, without checking that the
3542 -- value satisfies any constraint, any predicate, or any
3543 -- exclusion of the null value.
3545 -- * For a scalar type that has the Default_Value aspect
3546 -- specified, the formal parameter is initialized from the
3547 -- value of the actual, without checking that the value
3548 -- satisfies any constraint or any predicate.
3549 -- I do not understand why this case is included??? this is
3550 -- not a case where an OUT parameter is treated as IN OUT.
3552 -- * For a composite type with discriminants or that has
3553 -- implicit initial values for any subcomponents, the
3554 -- behavior is as for an in out parameter passed by copy.
3556 -- Hence for these cases we generate the read reference now
3557 -- (the write reference will be generated later by
3558 -- Note_Possible_Modification).
3560 elsif Is_By_Copy_Type (Etype (F))
3561 and then
3562 (Is_Access_Type (Etype (F))
3563 or else
3564 (Is_Scalar_Type (Etype (F))
3565 and then
3566 Present (Default_Aspect_Value (Etype (F))))
3567 or else
3568 (Is_Composite_Type (Etype (F))
3569 and then (Has_Discriminants (Etype (F))
3570 or else Is_Partially_Initialized_Type
3571 (Etype (F)))))
3572 then
3573 Generate_Reference (Orig_A, A);
3574 end if;
3575 end if;
3576 end if;
3577 end if;
3579 if Present (A)
3580 and then (Nkind (Parent (A)) /= N_Parameter_Association
3581 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3582 then
3583 -- If style checking mode on, check match of formal name
3585 if Style_Check then
3586 if Nkind (Parent (A)) = N_Parameter_Association then
3587 Check_Identifier (Selector_Name (Parent (A)), F);
3588 end if;
3589 end if;
3591 -- If the formal is Out or In_Out, do not resolve and expand the
3592 -- conversion, because it is subsequently expanded into explicit
3593 -- temporaries and assignments. However, the object of the
3594 -- conversion can be resolved. An exception is the case of tagged
3595 -- type conversion with a class-wide actual. In that case we want
3596 -- the tag check to occur and no temporary will be needed (no
3597 -- representation change can occur) and the parameter is passed by
3598 -- reference, so we go ahead and resolve the type conversion.
3599 -- Another exception is the case of reference to component or
3600 -- subcomponent of a bit-packed array, in which case we want to
3601 -- defer expansion to the point the in and out assignments are
3602 -- performed.
3604 if Ekind (F) /= E_In_Parameter
3605 and then Nkind (A) = N_Type_Conversion
3606 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3607 then
3608 if Ekind (F) = E_In_Out_Parameter
3609 and then Is_Array_Type (Etype (F))
3610 then
3611 -- In a view conversion, the conversion must be legal in
3612 -- both directions, and thus both component types must be
3613 -- aliased, or neither (4.6 (8)).
3615 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3616 -- the privacy requirement should not apply to generic
3617 -- types, and should be checked in an instance. ARG query
3618 -- is in order ???
3620 if Has_Aliased_Components (Etype (Expression (A))) /=
3621 Has_Aliased_Components (Etype (F))
3622 then
3623 Error_Msg_N
3624 ("both component types in a view conversion must be"
3625 & " aliased, or neither", A);
3627 -- Comment here??? what set of cases???
3629 elsif
3630 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3631 then
3632 -- Check view conv between unrelated by ref array types
3634 if Is_By_Reference_Type (Etype (F))
3635 or else Is_By_Reference_Type (Etype (Expression (A)))
3636 then
3637 Error_Msg_N
3638 ("view conversion between unrelated by reference "
3639 & "array types not allowed (\'A'I-00246)", A);
3641 -- In Ada 2005 mode, check view conversion component
3642 -- type cannot be private, tagged, or volatile. Note
3643 -- that we only apply this to source conversions. The
3644 -- generated code can contain conversions which are
3645 -- not subject to this test, and we cannot extract the
3646 -- component type in such cases since it is not present.
3648 elsif Comes_From_Source (A)
3649 and then Ada_Version >= Ada_2005
3650 then
3651 declare
3652 Comp_Type : constant Entity_Id :=
3653 Component_Type
3654 (Etype (Expression (A)));
3655 begin
3656 if (Is_Private_Type (Comp_Type)
3657 and then not Is_Generic_Type (Comp_Type))
3658 or else Is_Tagged_Type (Comp_Type)
3659 or else Is_Volatile (Comp_Type)
3660 then
3661 Error_Msg_N
3662 ("component type of a view conversion cannot"
3663 & " be private, tagged, or volatile"
3664 & " (RM 4.6 (24))",
3665 Expression (A));
3666 end if;
3667 end;
3668 end if;
3669 end if;
3670 end if;
3672 -- Resolve expression if conversion is all OK
3674 if (Conversion_OK (A)
3675 or else Valid_Conversion (A, Etype (A), Expression (A)))
3676 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3677 then
3678 Resolve (Expression (A));
3679 end if;
3681 -- If the actual is a function call that returns a limited
3682 -- unconstrained object that needs finalization, create a
3683 -- transient scope for it, so that it can receive the proper
3684 -- finalization list.
3686 elsif Nkind (A) = N_Function_Call
3687 and then Is_Limited_Record (Etype (F))
3688 and then not Is_Constrained (Etype (F))
3689 and then Expander_Active
3690 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3691 then
3692 Establish_Transient_Scope (A, Sec_Stack => False);
3693 Resolve (A, Etype (F));
3695 -- A small optimization: if one of the actuals is a concatenation
3696 -- create a block around a procedure call to recover stack space.
3697 -- This alleviates stack usage when several procedure calls in
3698 -- the same statement list use concatenation. We do not perform
3699 -- this wrapping for code statements, where the argument is a
3700 -- static string, and we want to preserve warnings involving
3701 -- sequences of such statements.
3703 elsif Nkind (A) = N_Op_Concat
3704 and then Nkind (N) = N_Procedure_Call_Statement
3705 and then Expander_Active
3706 and then
3707 not (Is_Intrinsic_Subprogram (Nam)
3708 and then Chars (Nam) = Name_Asm)
3709 and then not Static_Concatenation (A)
3710 then
3711 Establish_Transient_Scope (A, Sec_Stack => False);
3712 Resolve (A, Etype (F));
3714 else
3715 if Nkind (A) = N_Type_Conversion
3716 and then Is_Array_Type (Etype (F))
3717 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3718 and then
3719 (Is_Limited_Type (Etype (F))
3720 or else Is_Limited_Type (Etype (Expression (A))))
3721 then
3722 Error_Msg_N
3723 ("conversion between unrelated limited array types "
3724 & "not allowed ('A'I-00246)", A);
3726 if Is_Limited_Type (Etype (F)) then
3727 Explain_Limited_Type (Etype (F), A);
3728 end if;
3730 if Is_Limited_Type (Etype (Expression (A))) then
3731 Explain_Limited_Type (Etype (Expression (A)), A);
3732 end if;
3733 end if;
3735 -- (Ada 2005: AI-251): If the actual is an allocator whose
3736 -- directly designated type is a class-wide interface, we build
3737 -- an anonymous access type to use it as the type of the
3738 -- allocator. Later, when the subprogram call is expanded, if
3739 -- the interface has a secondary dispatch table the expander
3740 -- will add a type conversion to force the correct displacement
3741 -- of the pointer.
3743 if Nkind (A) = N_Allocator then
3744 declare
3745 DDT : constant Entity_Id :=
3746 Directly_Designated_Type (Base_Type (Etype (F)));
3748 New_Itype : Entity_Id;
3750 begin
3751 if Is_Class_Wide_Type (DDT)
3752 and then Is_Interface (DDT)
3753 then
3754 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3755 Set_Etype (New_Itype, Etype (A));
3756 Set_Directly_Designated_Type
3757 (New_Itype, Directly_Designated_Type (Etype (A)));
3758 Set_Etype (A, New_Itype);
3759 end if;
3761 -- Ada 2005, AI-162:If the actual is an allocator, the
3762 -- innermost enclosing statement is the master of the
3763 -- created object. This needs to be done with expansion
3764 -- enabled only, otherwise the transient scope will not
3765 -- be removed in the expansion of the wrapped construct.
3767 if (Is_Controlled (DDT) or else Has_Task (DDT))
3768 and then Expander_Active
3769 then
3770 Establish_Transient_Scope (A, Sec_Stack => False);
3771 end if;
3772 end;
3774 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3775 Check_Restriction (No_Access_Parameter_Allocators, A);
3776 end if;
3777 end if;
3779 -- (Ada 2005): The call may be to a primitive operation of a
3780 -- tagged synchronized type, declared outside of the type. In
3781 -- this case the controlling actual must be converted to its
3782 -- corresponding record type, which is the formal type. The
3783 -- actual may be a subtype, either because of a constraint or
3784 -- because it is a generic actual, so use base type to locate
3785 -- concurrent type.
3787 F_Typ := Base_Type (Etype (F));
3789 if Is_Tagged_Type (F_Typ)
3790 and then (Is_Concurrent_Type (F_Typ)
3791 or else Is_Concurrent_Record_Type (F_Typ))
3792 then
3793 -- If the actual is overloaded, look for an interpretation
3794 -- that has a synchronized type.
3796 if not Is_Overloaded (A) then
3797 A_Typ := Base_Type (Etype (A));
3799 else
3800 declare
3801 Index : Interp_Index;
3802 It : Interp;
3804 begin
3805 Get_First_Interp (A, Index, It);
3806 while Present (It.Typ) loop
3807 if Is_Concurrent_Type (It.Typ)
3808 or else Is_Concurrent_Record_Type (It.Typ)
3809 then
3810 A_Typ := Base_Type (It.Typ);
3811 exit;
3812 end if;
3814 Get_Next_Interp (Index, It);
3815 end loop;
3816 end;
3817 end if;
3819 declare
3820 Full_A_Typ : Entity_Id;
3822 begin
3823 if Present (Full_View (A_Typ)) then
3824 Full_A_Typ := Base_Type (Full_View (A_Typ));
3825 else
3826 Full_A_Typ := A_Typ;
3827 end if;
3829 -- Tagged synchronized type (case 1): the actual is a
3830 -- concurrent type.
3832 if Is_Concurrent_Type (A_Typ)
3833 and then Corresponding_Record_Type (A_Typ) = F_Typ
3834 then
3835 Rewrite (A,
3836 Unchecked_Convert_To
3837 (Corresponding_Record_Type (A_Typ), A));
3838 Resolve (A, Etype (F));
3840 -- Tagged synchronized type (case 2): the formal is a
3841 -- concurrent type.
3843 elsif Ekind (Full_A_Typ) = E_Record_Type
3844 and then Present
3845 (Corresponding_Concurrent_Type (Full_A_Typ))
3846 and then Is_Concurrent_Type (F_Typ)
3847 and then Present (Corresponding_Record_Type (F_Typ))
3848 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3849 then
3850 Resolve (A, Corresponding_Record_Type (F_Typ));
3852 -- Common case
3854 else
3855 Resolve (A, Etype (F));
3856 end if;
3857 end;
3859 -- Not a synchronized operation
3861 else
3862 Resolve (A, Etype (F));
3863 end if;
3864 end if;
3866 A_Typ := Etype (A);
3867 F_Typ := Etype (F);
3869 -- An actual cannot be an untagged formal incomplete type
3871 if Ekind (A_Typ) = E_Incomplete_Type
3872 and then not Is_Tagged_Type (A_Typ)
3873 and then Is_Generic_Type (A_Typ)
3874 then
3875 Error_Msg_N
3876 ("invalid use of untagged formal incomplete type", A);
3877 end if;
3879 if Comes_From_Source (Original_Node (N))
3880 and then Nkind_In (Original_Node (N), N_Function_Call,
3881 N_Procedure_Call_Statement)
3882 then
3883 -- In formal mode, check that actual parameters matching
3884 -- formals of tagged types are objects (or ancestor type
3885 -- conversions of objects), not general expressions.
3887 if Is_Actual_Tagged_Parameter (A) then
3888 if Is_SPARK_Object_Reference (A) then
3889 null;
3891 elsif Nkind (A) = N_Type_Conversion then
3892 declare
3893 Operand : constant Node_Id := Expression (A);
3894 Operand_Typ : constant Entity_Id := Etype (Operand);
3895 Target_Typ : constant Entity_Id := A_Typ;
3897 begin
3898 if not Is_SPARK_Object_Reference (Operand) then
3899 Check_SPARK_Restriction
3900 ("object required", Operand);
3902 -- In formal mode, the only view conversions are those
3903 -- involving ancestor conversion of an extended type.
3905 elsif not
3906 (Is_Tagged_Type (Target_Typ)
3907 and then not Is_Class_Wide_Type (Target_Typ)
3908 and then Is_Tagged_Type (Operand_Typ)
3909 and then not Is_Class_Wide_Type (Operand_Typ)
3910 and then Is_Ancestor (Target_Typ, Operand_Typ))
3911 then
3912 if Ekind_In
3913 (F, E_Out_Parameter, E_In_Out_Parameter)
3914 then
3915 Check_SPARK_Restriction
3916 ("ancestor conversion is the only permitted "
3917 & "view conversion", A);
3918 else
3919 Check_SPARK_Restriction
3920 ("ancestor conversion required", A);
3921 end if;
3923 else
3924 null;
3925 end if;
3926 end;
3928 else
3929 Check_SPARK_Restriction ("object required", A);
3930 end if;
3932 -- In formal mode, the only view conversions are those
3933 -- involving ancestor conversion of an extended type.
3935 elsif Nkind (A) = N_Type_Conversion
3936 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3937 then
3938 Check_SPARK_Restriction
3939 ("ancestor conversion is the only permitted view "
3940 & "conversion", A);
3941 end if;
3942 end if;
3944 -- has warnings suppressed, then we reset Never_Set_In_Source for
3945 -- the calling entity. The reason for this is to catch cases like
3946 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3947 -- uses trickery to modify an IN parameter.
3949 if Ekind (F) = E_In_Parameter
3950 and then Is_Entity_Name (A)
3951 and then Present (Entity (A))
3952 and then Ekind (Entity (A)) = E_Variable
3953 and then Has_Warnings_Off (F_Typ)
3954 then
3955 Set_Never_Set_In_Source (Entity (A), False);
3956 end if;
3958 -- Perform error checks for IN and IN OUT parameters
3960 if Ekind (F) /= E_Out_Parameter then
3962 -- Check unset reference. For scalar parameters, it is clearly
3963 -- wrong to pass an uninitialized value as either an IN or
3964 -- IN-OUT parameter. For composites, it is also clearly an
3965 -- error to pass a completely uninitialized value as an IN
3966 -- parameter, but the case of IN OUT is trickier. We prefer
3967 -- not to give a warning here. For example, suppose there is
3968 -- a routine that sets some component of a record to False.
3969 -- It is perfectly reasonable to make this IN-OUT and allow
3970 -- either initialized or uninitialized records to be passed
3971 -- in this case.
3973 -- For partially initialized composite values, we also avoid
3974 -- warnings, since it is quite likely that we are passing a
3975 -- partially initialized value and only the initialized fields
3976 -- will in fact be read in the subprogram.
3978 if Is_Scalar_Type (A_Typ)
3979 or else (Ekind (F) = E_In_Parameter
3980 and then not Is_Partially_Initialized_Type (A_Typ))
3981 then
3982 Check_Unset_Reference (A);
3983 end if;
3985 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3986 -- actual to a nested call, since this is case of reading an
3987 -- out parameter, which is not allowed.
3989 if Ada_Version = Ada_83
3990 and then Is_Entity_Name (A)
3991 and then Ekind (Entity (A)) = E_Out_Parameter
3992 then
3993 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3994 end if;
3995 end if;
3997 -- Case of OUT or IN OUT parameter
3999 if Ekind (F) /= E_In_Parameter then
4001 -- For an Out parameter, check for useless assignment. Note
4002 -- that we can't set Last_Assignment this early, because we may
4003 -- kill current values in Resolve_Call, and that call would
4004 -- clobber the Last_Assignment field.
4006 -- Note: call Warn_On_Useless_Assignment before doing the check
4007 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4008 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4009 -- reflects the last assignment, not this one.
4011 if Ekind (F) = E_Out_Parameter then
4012 if Warn_On_Modified_As_Out_Parameter (F)
4013 and then Is_Entity_Name (A)
4014 and then Present (Entity (A))
4015 and then Comes_From_Source (N)
4016 then
4017 Warn_On_Useless_Assignment (Entity (A), A);
4018 end if;
4019 end if;
4021 -- Validate the form of the actual. Note that the call to
4022 -- Is_OK_Variable_For_Out_Formal generates the required
4023 -- reference in this case.
4025 -- A call to an initialization procedure for an aggregate
4026 -- component may initialize a nested component of a constant
4027 -- designated object. In this context the object is variable.
4029 if not Is_OK_Variable_For_Out_Formal (A)
4030 and then not Is_Init_Proc (Nam)
4031 then
4032 Error_Msg_NE ("actual for& must be a variable", A, F);
4034 if Is_Subprogram (Current_Scope)
4035 and then
4036 (Is_Invariant_Procedure (Current_Scope)
4037 or else Is_Predicate_Function (Current_Scope))
4038 then
4039 Error_Msg_N
4040 ("function used in predicate cannot "
4041 & "modify its argument", F);
4042 end if;
4043 end if;
4045 -- What's the following about???
4047 if Is_Entity_Name (A) then
4048 Kill_Checks (Entity (A));
4049 else
4050 Kill_All_Checks;
4051 end if;
4052 end if;
4054 if Etype (A) = Any_Type then
4055 Set_Etype (N, Any_Type);
4056 return;
4057 end if;
4059 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4061 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
4063 -- Apply predicate tests except in certain special cases. Note
4064 -- that it might be more consistent to apply these only when
4065 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4066 -- for the outbound predicate tests ???
4068 if Predicate_Tests_On_Arguments (Nam) then
4069 Apply_Predicate_Check (A, F_Typ);
4070 end if;
4072 -- Apply required constraint checks
4074 -- Gigi looks at the check flag and uses the appropriate types.
4075 -- For now since one flag is used there is an optimization
4076 -- which might not be done in the IN OUT case since Gigi does
4077 -- not do any analysis. More thought required about this ???
4079 -- In fact is this comment obsolete??? doesn't the expander now
4080 -- generate all these tests anyway???
4082 if Is_Scalar_Type (Etype (A)) then
4083 Apply_Scalar_Range_Check (A, F_Typ);
4085 elsif Is_Array_Type (Etype (A)) then
4086 Apply_Length_Check (A, F_Typ);
4088 elsif Is_Record_Type (F_Typ)
4089 and then Has_Discriminants (F_Typ)
4090 and then Is_Constrained (F_Typ)
4091 and then (not Is_Derived_Type (F_Typ)
4092 or else Comes_From_Source (Nam))
4093 then
4094 Apply_Discriminant_Check (A, F_Typ);
4096 -- For view conversions of a discriminated object, apply
4097 -- check to object itself, the conversion alreay has the
4098 -- proper type.
4100 if Nkind (A) = N_Type_Conversion
4101 and then Is_Constrained (Etype (Expression (A)))
4102 then
4103 Apply_Discriminant_Check (Expression (A), F_Typ);
4104 end if;
4106 elsif Is_Access_Type (F_Typ)
4107 and then Is_Array_Type (Designated_Type (F_Typ))
4108 and then Is_Constrained (Designated_Type (F_Typ))
4109 then
4110 Apply_Length_Check (A, F_Typ);
4112 elsif Is_Access_Type (F_Typ)
4113 and then Has_Discriminants (Designated_Type (F_Typ))
4114 and then Is_Constrained (Designated_Type (F_Typ))
4115 then
4116 Apply_Discriminant_Check (A, F_Typ);
4118 else
4119 Apply_Range_Check (A, F_Typ);
4120 end if;
4122 -- Ada 2005 (AI-231): Note that the controlling parameter case
4123 -- already existed in Ada 95, which is partially checked
4124 -- elsewhere (see Checks), and we don't want the warning
4125 -- message to differ.
4127 if Is_Access_Type (F_Typ)
4128 and then Can_Never_Be_Null (F_Typ)
4129 and then Known_Null (A)
4130 then
4131 if Is_Controlling_Formal (F) then
4132 Apply_Compile_Time_Constraint_Error
4133 (N => A,
4134 Msg => "null value not allowed here??",
4135 Reason => CE_Access_Check_Failed);
4137 elsif Ada_Version >= Ada_2005 then
4138 Apply_Compile_Time_Constraint_Error
4139 (N => A,
4140 Msg => "(Ada 2005) null not allowed in "
4141 & "null-excluding formal??",
4142 Reason => CE_Null_Not_Allowed);
4143 end if;
4144 end if;
4145 end if;
4147 -- Checks for OUT parameters and IN OUT parameters
4149 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
4151 -- If there is a type conversion, to make sure the return value
4152 -- meets the constraints of the variable before the conversion.
4154 if Nkind (A) = N_Type_Conversion then
4155 if Is_Scalar_Type (A_Typ) then
4156 Apply_Scalar_Range_Check
4157 (Expression (A), Etype (Expression (A)), A_Typ);
4158 else
4159 Apply_Range_Check
4160 (Expression (A), Etype (Expression (A)), A_Typ);
4161 end if;
4163 -- If no conversion apply scalar range checks and length checks
4164 -- base on the subtype of the actual (NOT that of the formal).
4166 else
4167 if Is_Scalar_Type (F_Typ) then
4168 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4169 elsif Is_Array_Type (F_Typ)
4170 and then Ekind (F) = E_Out_Parameter
4171 then
4172 Apply_Length_Check (A, F_Typ);
4173 else
4174 Apply_Range_Check (A, A_Typ, F_Typ);
4175 end if;
4176 end if;
4178 -- Note: we do not apply the predicate checks for the case of
4179 -- OUT and IN OUT parameters. They are instead applied in the
4180 -- Expand_Actuals routine in Exp_Ch6.
4181 end if;
4183 -- An actual associated with an access parameter is implicitly
4184 -- converted to the anonymous access type of the formal and must
4185 -- satisfy the legality checks for access conversions.
4187 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4188 if not Valid_Conversion (A, F_Typ, A) then
4189 Error_Msg_N
4190 ("invalid implicit conversion for access parameter", A);
4191 end if;
4193 -- If the actual is an access selected component of a variable,
4194 -- the call may modify its designated object. It is reasonable
4195 -- to treat this as a potential modification of the enclosing
4196 -- record, to prevent spurious warnings that it should be
4197 -- declared as a constant, because intuitively programmers
4198 -- regard the designated subcomponent as part of the record.
4200 if Nkind (A) = N_Selected_Component
4201 and then Is_Entity_Name (Prefix (A))
4202 and then not Is_Constant_Object (Entity (Prefix (A)))
4203 then
4204 Note_Possible_Modification (A, Sure => False);
4205 end if;
4206 end if;
4208 -- Check bad case of atomic/volatile argument (RM C.6(12))
4210 if Is_By_Reference_Type (Etype (F))
4211 and then Comes_From_Source (N)
4212 then
4213 if Is_Atomic_Object (A)
4214 and then not Is_Atomic (Etype (F))
4215 then
4216 Error_Msg_NE
4217 ("cannot pass atomic argument to non-atomic formal&",
4218 A, F);
4220 elsif Is_Volatile_Object (A)
4221 and then not Is_Volatile (Etype (F))
4222 then
4223 Error_Msg_NE
4224 ("cannot pass volatile argument to non-volatile formal&",
4225 A, F);
4226 end if;
4227 end if;
4229 -- Check that subprograms don't have improper controlling
4230 -- arguments (RM 3.9.2 (9)).
4232 -- A primitive operation may have an access parameter of an
4233 -- incomplete tagged type, but a dispatching call is illegal
4234 -- if the type is still incomplete.
4236 if Is_Controlling_Formal (F) then
4237 Set_Is_Controlling_Actual (A);
4239 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4240 declare
4241 Desig : constant Entity_Id := Designated_Type (Etype (F));
4242 begin
4243 if Ekind (Desig) = E_Incomplete_Type
4244 and then No (Full_View (Desig))
4245 and then No (Non_Limited_View (Desig))
4246 then
4247 Error_Msg_NE
4248 ("premature use of incomplete type& "
4249 & "in dispatching call", A, Desig);
4250 end if;
4251 end;
4252 end if;
4254 elsif Nkind (A) = N_Explicit_Dereference then
4255 Validate_Remote_Access_To_Class_Wide_Type (A);
4256 end if;
4258 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4259 and then not Is_Class_Wide_Type (F_Typ)
4260 and then not Is_Controlling_Formal (F)
4261 then
4262 Error_Msg_N ("class-wide argument not allowed here!", A);
4264 if Is_Subprogram (Nam)
4265 and then Comes_From_Source (Nam)
4266 then
4267 Error_Msg_Node_2 := F_Typ;
4268 Error_Msg_NE
4269 ("& is not a dispatching operation of &!", A, Nam);
4270 end if;
4272 -- Apply the checks described in 3.10.2(27): if the context is a
4273 -- specific access-to-object, the actual cannot be class-wide.
4274 -- Use base type to exclude access_to_subprogram cases.
4276 elsif Is_Access_Type (A_Typ)
4277 and then Is_Access_Type (F_Typ)
4278 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4279 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4280 or else (Nkind (A) = N_Attribute_Reference
4281 and then
4282 Is_Class_Wide_Type (Etype (Prefix (A)))))
4283 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4284 and then not Is_Controlling_Formal (F)
4286 -- Disable these checks for call to imported C++ subprograms
4288 and then not
4289 (Is_Entity_Name (Name (N))
4290 and then Is_Imported (Entity (Name (N)))
4291 and then Convention (Entity (Name (N))) = Convention_CPP)
4292 then
4293 Error_Msg_N
4294 ("access to class-wide argument not allowed here!", A);
4296 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4297 Error_Msg_Node_2 := Designated_Type (F_Typ);
4298 Error_Msg_NE
4299 ("& is not a dispatching operation of &!", A, Nam);
4300 end if;
4301 end if;
4303 Check_Aliased_Parameter;
4305 Eval_Actual (A);
4307 -- If it is a named association, treat the selector_name as a
4308 -- proper identifier, and mark the corresponding entity.
4310 if Nkind (Parent (A)) = N_Parameter_Association
4312 -- Ignore reference in SPARK mode, as it refers to an entity not
4313 -- in scope at the point of reference, so the reference should
4314 -- be ignored for computing effects of subprograms.
4316 and then not GNATprove_Mode
4317 then
4318 Set_Entity (Selector_Name (Parent (A)), F);
4319 Generate_Reference (F, Selector_Name (Parent (A)));
4320 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4321 Generate_Reference (F_Typ, N, ' ');
4322 end if;
4324 Prev := A;
4326 if Ekind (F) /= E_Out_Parameter then
4327 Check_Unset_Reference (A);
4328 end if;
4330 -- The following checks are only relevant when SPARK_Mode is on as
4331 -- they are not standard Ada legality rule. Internally generated
4332 -- temporaries are ignored.
4334 if SPARK_Mode = On
4335 and then Is_Effectively_Volatile_Object (A)
4336 and then Comes_From_Source (A)
4337 then
4338 -- An effectively volatile object may act as an actual
4339 -- parameter when the corresponding formal is of a non-scalar
4340 -- volatile type.
4342 if Is_Volatile (Etype (F))
4343 and then not Is_Scalar_Type (Etype (F))
4344 then
4345 null;
4347 -- An effectively volatile object may act as an actual
4348 -- parameter in a call to an instance of Unchecked_Conversion.
4350 elsif Is_Unchecked_Conversion_Instance (Nam) then
4351 null;
4353 else
4354 Error_Msg_N
4355 ("volatile object cannot act as actual in a call (SPARK "
4356 & "RM 7.1.3(12))", A);
4357 end if;
4359 -- Detect an external variable with an enabled property that
4360 -- does not match the mode of the corresponding formal in a
4361 -- procedure call. Functions are not considered because they
4362 -- cannot have effectively volatile formal parameters in the
4363 -- first place.
4365 if Ekind (Nam) = E_Procedure
4366 and then Is_Entity_Name (A)
4367 and then Present (Entity (A))
4368 and then Ekind (Entity (A)) = E_Variable
4369 then
4370 A_Id := Entity (A);
4372 if Ekind (F) = E_In_Parameter then
4373 if Async_Readers_Enabled (A_Id) then
4374 Property_Error (A, A_Id, Name_Async_Readers);
4375 elsif Effective_Reads_Enabled (A_Id) then
4376 Property_Error (A, A_Id, Name_Effective_Reads);
4377 elsif Effective_Writes_Enabled (A_Id) then
4378 Property_Error (A, A_Id, Name_Effective_Writes);
4379 end if;
4381 elsif Ekind (F) = E_Out_Parameter
4382 and then Async_Writers_Enabled (A_Id)
4383 then
4384 Error_Msg_Name_1 := Name_Async_Writers;
4385 Error_Msg_NE
4386 ("external variable & with enabled property % cannot "
4387 & "appear as actual in procedure call "
4388 & "(SPARK RM 7.1.3(11))", A, A_Id);
4389 Error_Msg_N
4390 ("\\corresponding formal parameter has mode Out", A);
4391 end if;
4392 end if;
4393 end if;
4395 Next_Actual (A);
4397 -- Case where actual is not present
4399 else
4400 Insert_Default;
4401 end if;
4403 Next_Formal (F);
4404 end loop;
4405 end Resolve_Actuals;
4407 -----------------------
4408 -- Resolve_Allocator --
4409 -----------------------
4411 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4412 Desig_T : constant Entity_Id := Designated_Type (Typ);
4413 E : constant Node_Id := Expression (N);
4414 Subtyp : Entity_Id;
4415 Discrim : Entity_Id;
4416 Constr : Node_Id;
4417 Aggr : Node_Id;
4418 Assoc : Node_Id := Empty;
4419 Disc_Exp : Node_Id;
4421 procedure Check_Allocator_Discrim_Accessibility
4422 (Disc_Exp : Node_Id;
4423 Alloc_Typ : Entity_Id);
4424 -- Check that accessibility level associated with an access discriminant
4425 -- initialized in an allocator by the expression Disc_Exp is not deeper
4426 -- than the level of the allocator type Alloc_Typ. An error message is
4427 -- issued if this condition is violated. Specialized checks are done for
4428 -- the cases of a constraint expression which is an access attribute or
4429 -- an access discriminant.
4431 function In_Dispatching_Context return Boolean;
4432 -- If the allocator is an actual in a call, it is allowed to be class-
4433 -- wide when the context is not because it is a controlling actual.
4435 -------------------------------------------
4436 -- Check_Allocator_Discrim_Accessibility --
4437 -------------------------------------------
4439 procedure Check_Allocator_Discrim_Accessibility
4440 (Disc_Exp : Node_Id;
4441 Alloc_Typ : Entity_Id)
4443 begin
4444 if Type_Access_Level (Etype (Disc_Exp)) >
4445 Deepest_Type_Access_Level (Alloc_Typ)
4446 then
4447 Error_Msg_N
4448 ("operand type has deeper level than allocator type", Disc_Exp);
4450 -- When the expression is an Access attribute the level of the prefix
4451 -- object must not be deeper than that of the allocator's type.
4453 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4454 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4455 Attribute_Access
4456 and then Object_Access_Level (Prefix (Disc_Exp)) >
4457 Deepest_Type_Access_Level (Alloc_Typ)
4458 then
4459 Error_Msg_N
4460 ("prefix of attribute has deeper level than allocator type",
4461 Disc_Exp);
4463 -- When the expression is an access discriminant the check is against
4464 -- the level of the prefix object.
4466 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4467 and then Nkind (Disc_Exp) = N_Selected_Component
4468 and then Object_Access_Level (Prefix (Disc_Exp)) >
4469 Deepest_Type_Access_Level (Alloc_Typ)
4470 then
4471 Error_Msg_N
4472 ("access discriminant has deeper level than allocator type",
4473 Disc_Exp);
4475 -- All other cases are legal
4477 else
4478 null;
4479 end if;
4480 end Check_Allocator_Discrim_Accessibility;
4482 ----------------------------
4483 -- In_Dispatching_Context --
4484 ----------------------------
4486 function In_Dispatching_Context return Boolean is
4487 Par : constant Node_Id := Parent (N);
4489 begin
4490 return Nkind (Par) in N_Subprogram_Call
4491 and then Is_Entity_Name (Name (Par))
4492 and then Is_Dispatching_Operation (Entity (Name (Par)));
4493 end In_Dispatching_Context;
4495 -- Start of processing for Resolve_Allocator
4497 begin
4498 -- Replace general access with specific type
4500 if Ekind (Etype (N)) = E_Allocator_Type then
4501 Set_Etype (N, Base_Type (Typ));
4502 end if;
4504 if Is_Abstract_Type (Typ) then
4505 Error_Msg_N ("type of allocator cannot be abstract", N);
4506 end if;
4508 -- For qualified expression, resolve the expression using the given
4509 -- subtype (nothing to do for type mark, subtype indication)
4511 if Nkind (E) = N_Qualified_Expression then
4512 if Is_Class_Wide_Type (Etype (E))
4513 and then not Is_Class_Wide_Type (Desig_T)
4514 and then not In_Dispatching_Context
4515 then
4516 Error_Msg_N
4517 ("class-wide allocator not allowed for this access type", N);
4518 end if;
4520 Resolve (Expression (E), Etype (E));
4521 Check_Non_Static_Context (Expression (E));
4522 Check_Unset_Reference (Expression (E));
4524 -- A qualified expression requires an exact match of the type.
4525 -- Class-wide matching is not allowed.
4527 if (Is_Class_Wide_Type (Etype (Expression (E)))
4528 or else Is_Class_Wide_Type (Etype (E)))
4529 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4530 then
4531 Wrong_Type (Expression (E), Etype (E));
4532 end if;
4534 -- Calls to build-in-place functions are not currently supported in
4535 -- allocators for access types associated with a simple storage pool.
4536 -- Supporting such allocators may require passing additional implicit
4537 -- parameters to build-in-place functions (or a significant revision
4538 -- of the current b-i-p implementation to unify the handling for
4539 -- multiple kinds of storage pools). ???
4541 if Is_Limited_View (Desig_T)
4542 and then Nkind (Expression (E)) = N_Function_Call
4543 then
4544 declare
4545 Pool : constant Entity_Id :=
4546 Associated_Storage_Pool (Root_Type (Typ));
4547 begin
4548 if Present (Pool)
4549 and then
4550 Present (Get_Rep_Pragma
4551 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4552 then
4553 Error_Msg_N
4554 ("limited function calls not yet supported in simple "
4555 & "storage pool allocators", Expression (E));
4556 end if;
4557 end;
4558 end if;
4560 -- A special accessibility check is needed for allocators that
4561 -- constrain access discriminants. The level of the type of the
4562 -- expression used to constrain an access discriminant cannot be
4563 -- deeper than the type of the allocator (in contrast to access
4564 -- parameters, where the level of the actual can be arbitrary).
4566 -- We can't use Valid_Conversion to perform this check because in
4567 -- general the type of the allocator is unrelated to the type of
4568 -- the access discriminant.
4570 if Ekind (Typ) /= E_Anonymous_Access_Type
4571 or else Is_Local_Anonymous_Access (Typ)
4572 then
4573 Subtyp := Entity (Subtype_Mark (E));
4575 Aggr := Original_Node (Expression (E));
4577 if Has_Discriminants (Subtyp)
4578 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4579 then
4580 Discrim := First_Discriminant (Base_Type (Subtyp));
4582 -- Get the first component expression of the aggregate
4584 if Present (Expressions (Aggr)) then
4585 Disc_Exp := First (Expressions (Aggr));
4587 elsif Present (Component_Associations (Aggr)) then
4588 Assoc := First (Component_Associations (Aggr));
4590 if Present (Assoc) then
4591 Disc_Exp := Expression (Assoc);
4592 else
4593 Disc_Exp := Empty;
4594 end if;
4596 else
4597 Disc_Exp := Empty;
4598 end if;
4600 while Present (Discrim) and then Present (Disc_Exp) loop
4601 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4602 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4603 end if;
4605 Next_Discriminant (Discrim);
4607 if Present (Discrim) then
4608 if Present (Assoc) then
4609 Next (Assoc);
4610 Disc_Exp := Expression (Assoc);
4612 elsif Present (Next (Disc_Exp)) then
4613 Next (Disc_Exp);
4615 else
4616 Assoc := First (Component_Associations (Aggr));
4618 if Present (Assoc) then
4619 Disc_Exp := Expression (Assoc);
4620 else
4621 Disc_Exp := Empty;
4622 end if;
4623 end if;
4624 end if;
4625 end loop;
4626 end if;
4627 end if;
4629 -- For a subtype mark or subtype indication, freeze the subtype
4631 else
4632 Freeze_Expression (E);
4634 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4635 Error_Msg_N
4636 ("initialization required for access-to-constant allocator", N);
4637 end if;
4639 -- A special accessibility check is needed for allocators that
4640 -- constrain access discriminants. The level of the type of the
4641 -- expression used to constrain an access discriminant cannot be
4642 -- deeper than the type of the allocator (in contrast to access
4643 -- parameters, where the level of the actual can be arbitrary).
4644 -- We can't use Valid_Conversion to perform this check because
4645 -- in general the type of the allocator is unrelated to the type
4646 -- of the access discriminant.
4648 if Nkind (Original_Node (E)) = N_Subtype_Indication
4649 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4650 or else Is_Local_Anonymous_Access (Typ))
4651 then
4652 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4654 if Has_Discriminants (Subtyp) then
4655 Discrim := First_Discriminant (Base_Type (Subtyp));
4656 Constr := First (Constraints (Constraint (Original_Node (E))));
4657 while Present (Discrim) and then Present (Constr) loop
4658 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4659 if Nkind (Constr) = N_Discriminant_Association then
4660 Disc_Exp := Original_Node (Expression (Constr));
4661 else
4662 Disc_Exp := Original_Node (Constr);
4663 end if;
4665 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4666 end if;
4668 Next_Discriminant (Discrim);
4669 Next (Constr);
4670 end loop;
4671 end if;
4672 end if;
4673 end if;
4675 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4676 -- check that the level of the type of the created object is not deeper
4677 -- than the level of the allocator's access type, since extensions can
4678 -- now occur at deeper levels than their ancestor types. This is a
4679 -- static accessibility level check; a run-time check is also needed in
4680 -- the case of an initialized allocator with a class-wide argument (see
4681 -- Expand_Allocator_Expression).
4683 if Ada_Version >= Ada_2005
4684 and then Is_Class_Wide_Type (Desig_T)
4685 then
4686 declare
4687 Exp_Typ : Entity_Id;
4689 begin
4690 if Nkind (E) = N_Qualified_Expression then
4691 Exp_Typ := Etype (E);
4692 elsif Nkind (E) = N_Subtype_Indication then
4693 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4694 else
4695 Exp_Typ := Entity (E);
4696 end if;
4698 if Type_Access_Level (Exp_Typ) >
4699 Deepest_Type_Access_Level (Typ)
4700 then
4701 if In_Instance_Body then
4702 Error_Msg_Warn := SPARK_Mode /= On;
4703 Error_Msg_N
4704 ("type in allocator has deeper level than "
4705 & "designated class-wide type<<", E);
4706 Error_Msg_N ("\Program_Error [<<", E);
4707 Rewrite (N,
4708 Make_Raise_Program_Error (Sloc (N),
4709 Reason => PE_Accessibility_Check_Failed));
4710 Set_Etype (N, Typ);
4712 -- Do not apply Ada 2005 accessibility checks on a class-wide
4713 -- allocator if the type given in the allocator is a formal
4714 -- type. A run-time check will be performed in the instance.
4716 elsif not Is_Generic_Type (Exp_Typ) then
4717 Error_Msg_N ("type in allocator has deeper level than "
4718 & "designated class-wide type", E);
4719 end if;
4720 end if;
4721 end;
4722 end if;
4724 -- Check for allocation from an empty storage pool
4726 if No_Pool_Assigned (Typ) then
4727 Error_Msg_N ("allocation from empty storage pool!", N);
4729 -- If the context is an unchecked conversion, as may happen within an
4730 -- inlined subprogram, the allocator is being resolved with its own
4731 -- anonymous type. In that case, if the target type has a specific
4732 -- storage pool, it must be inherited explicitly by the allocator type.
4734 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4735 and then No (Associated_Storage_Pool (Typ))
4736 then
4737 Set_Associated_Storage_Pool
4738 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4739 end if;
4741 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4742 Check_Restriction (No_Anonymous_Allocators, N);
4743 end if;
4745 -- Check that an allocator with task parts isn't for a nested access
4746 -- type when restriction No_Task_Hierarchy applies.
4748 if not Is_Library_Level_Entity (Base_Type (Typ))
4749 and then Has_Task (Base_Type (Desig_T))
4750 then
4751 Check_Restriction (No_Task_Hierarchy, N);
4752 end if;
4754 -- An illegal allocator may be rewritten as a raise Program_Error
4755 -- statement.
4757 if Nkind (N) = N_Allocator then
4759 -- An anonymous access discriminant is the definition of a
4760 -- coextension.
4762 if Ekind (Typ) = E_Anonymous_Access_Type
4763 and then Nkind (Associated_Node_For_Itype (Typ)) =
4764 N_Discriminant_Specification
4765 then
4766 declare
4767 Discr : constant Entity_Id :=
4768 Defining_Identifier (Associated_Node_For_Itype (Typ));
4770 begin
4771 Check_Restriction (No_Coextensions, N);
4773 -- Ada 2012 AI05-0052: If the designated type of the allocator
4774 -- is limited, then the allocator shall not be used to define
4775 -- the value of an access discriminant unless the discriminated
4776 -- type is immutably limited.
4778 if Ada_Version >= Ada_2012
4779 and then Is_Limited_Type (Desig_T)
4780 and then not Is_Limited_View (Scope (Discr))
4781 then
4782 Error_Msg_N
4783 ("only immutably limited types can have anonymous "
4784 & "access discriminants designating a limited type", N);
4785 end if;
4786 end;
4788 -- Avoid marking an allocator as a dynamic coextension if it is
4789 -- within a static construct.
4791 if not Is_Static_Coextension (N) then
4792 Set_Is_Dynamic_Coextension (N);
4793 end if;
4795 -- Cleanup for potential static coextensions
4797 else
4798 Set_Is_Dynamic_Coextension (N, False);
4799 Set_Is_Static_Coextension (N, False);
4800 end if;
4801 end if;
4803 -- Report a simple error: if the designated object is a local task,
4804 -- its body has not been seen yet, and its activation will fail an
4805 -- elaboration check.
4807 if Is_Task_Type (Desig_T)
4808 and then Scope (Base_Type (Desig_T)) = Current_Scope
4809 and then Is_Compilation_Unit (Current_Scope)
4810 and then Ekind (Current_Scope) = E_Package
4811 and then not In_Package_Body (Current_Scope)
4812 then
4813 Error_Msg_Warn := SPARK_Mode /= On;
4814 Error_Msg_N ("cannot activate task before body seen<<", N);
4815 Error_Msg_N ("\Program_Error [<<", N);
4816 end if;
4818 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4819 -- type with a task component on a subpool. This action must raise
4820 -- Program_Error at runtime.
4822 if Ada_Version >= Ada_2012
4823 and then Nkind (N) = N_Allocator
4824 and then Present (Subpool_Handle_Name (N))
4825 and then Has_Task (Desig_T)
4826 then
4827 Error_Msg_Warn := SPARK_Mode /= On;
4828 Error_Msg_N ("cannot allocate task on subpool<<", N);
4829 Error_Msg_N ("\Program_Error [<<", N);
4831 Rewrite (N,
4832 Make_Raise_Program_Error (Sloc (N),
4833 Reason => PE_Explicit_Raise));
4834 Set_Etype (N, Typ);
4835 end if;
4836 end Resolve_Allocator;
4838 ---------------------------
4839 -- Resolve_Arithmetic_Op --
4840 ---------------------------
4842 -- Used for resolving all arithmetic operators except exponentiation
4844 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4845 L : constant Node_Id := Left_Opnd (N);
4846 R : constant Node_Id := Right_Opnd (N);
4847 TL : constant Entity_Id := Base_Type (Etype (L));
4848 TR : constant Entity_Id := Base_Type (Etype (R));
4849 T : Entity_Id;
4850 Rop : Node_Id;
4852 B_Typ : constant Entity_Id := Base_Type (Typ);
4853 -- We do the resolution using the base type, because intermediate values
4854 -- in expressions always are of the base type, not a subtype of it.
4856 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4857 -- Returns True if N is in a context that expects "any real type"
4859 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4860 -- Return True iff given type is Integer or universal real/integer
4862 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4863 -- Choose type of integer literal in fixed-point operation to conform
4864 -- to available fixed-point type. T is the type of the other operand,
4865 -- which is needed to determine the expected type of N.
4867 procedure Set_Operand_Type (N : Node_Id);
4868 -- Set operand type to T if universal
4870 -------------------------------
4871 -- Expected_Type_Is_Any_Real --
4872 -------------------------------
4874 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4875 begin
4876 -- N is the expression after "delta" in a fixed_point_definition;
4877 -- see RM-3.5.9(6):
4879 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4880 N_Decimal_Fixed_Point_Definition,
4882 -- N is one of the bounds in a real_range_specification;
4883 -- see RM-3.5.7(5):
4885 N_Real_Range_Specification,
4887 -- N is the expression of a delta_constraint;
4888 -- see RM-J.3(3):
4890 N_Delta_Constraint);
4891 end Expected_Type_Is_Any_Real;
4893 -----------------------------
4894 -- Is_Integer_Or_Universal --
4895 -----------------------------
4897 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4898 T : Entity_Id;
4899 Index : Interp_Index;
4900 It : Interp;
4902 begin
4903 if not Is_Overloaded (N) then
4904 T := Etype (N);
4905 return Base_Type (T) = Base_Type (Standard_Integer)
4906 or else T = Universal_Integer
4907 or else T = Universal_Real;
4908 else
4909 Get_First_Interp (N, Index, It);
4910 while Present (It.Typ) loop
4911 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4912 or else It.Typ = Universal_Integer
4913 or else It.Typ = Universal_Real
4914 then
4915 return True;
4916 end if;
4918 Get_Next_Interp (Index, It);
4919 end loop;
4920 end if;
4922 return False;
4923 end Is_Integer_Or_Universal;
4925 ----------------------------
4926 -- Set_Mixed_Mode_Operand --
4927 ----------------------------
4929 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4930 Index : Interp_Index;
4931 It : Interp;
4933 begin
4934 if Universal_Interpretation (N) = Universal_Integer then
4936 -- A universal integer literal is resolved as standard integer
4937 -- except in the case of a fixed-point result, where we leave it
4938 -- as universal (to be handled by Exp_Fixd later on)
4940 if Is_Fixed_Point_Type (T) then
4941 Resolve (N, Universal_Integer);
4942 else
4943 Resolve (N, Standard_Integer);
4944 end if;
4946 elsif Universal_Interpretation (N) = Universal_Real
4947 and then (T = Base_Type (Standard_Integer)
4948 or else T = Universal_Integer
4949 or else T = Universal_Real)
4950 then
4951 -- A universal real can appear in a fixed-type context. We resolve
4952 -- the literal with that context, even though this might raise an
4953 -- exception prematurely (the other operand may be zero).
4955 Resolve (N, B_Typ);
4957 elsif Etype (N) = Base_Type (Standard_Integer)
4958 and then T = Universal_Real
4959 and then Is_Overloaded (N)
4960 then
4961 -- Integer arg in mixed-mode operation. Resolve with universal
4962 -- type, in case preference rule must be applied.
4964 Resolve (N, Universal_Integer);
4966 elsif Etype (N) = T
4967 and then B_Typ /= Universal_Fixed
4968 then
4969 -- Not a mixed-mode operation, resolve with context
4971 Resolve (N, B_Typ);
4973 elsif Etype (N) = Any_Fixed then
4975 -- N may itself be a mixed-mode operation, so use context type
4977 Resolve (N, B_Typ);
4979 elsif Is_Fixed_Point_Type (T)
4980 and then B_Typ = Universal_Fixed
4981 and then Is_Overloaded (N)
4982 then
4983 -- Must be (fixed * fixed) operation, operand must have one
4984 -- compatible interpretation.
4986 Resolve (N, Any_Fixed);
4988 elsif Is_Fixed_Point_Type (B_Typ)
4989 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
4990 and then Is_Overloaded (N)
4991 then
4992 -- C * F(X) in a fixed context, where C is a real literal or a
4993 -- fixed-point expression. F must have either a fixed type
4994 -- interpretation or an integer interpretation, but not both.
4996 Get_First_Interp (N, Index, It);
4997 while Present (It.Typ) loop
4998 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4999 if Analyzed (N) then
5000 Error_Msg_N ("ambiguous operand in fixed operation", N);
5001 else
5002 Resolve (N, Standard_Integer);
5003 end if;
5005 elsif Is_Fixed_Point_Type (It.Typ) then
5006 if Analyzed (N) then
5007 Error_Msg_N ("ambiguous operand in fixed operation", N);
5008 else
5009 Resolve (N, It.Typ);
5010 end if;
5011 end if;
5013 Get_Next_Interp (Index, It);
5014 end loop;
5016 -- Reanalyze the literal with the fixed type of the context. If
5017 -- context is Universal_Fixed, we are within a conversion, leave
5018 -- the literal as a universal real because there is no usable
5019 -- fixed type, and the target of the conversion plays no role in
5020 -- the resolution.
5022 declare
5023 Op2 : Node_Id;
5024 T2 : Entity_Id;
5026 begin
5027 if N = L then
5028 Op2 := R;
5029 else
5030 Op2 := L;
5031 end if;
5033 if B_Typ = Universal_Fixed
5034 and then Nkind (Op2) = N_Real_Literal
5035 then
5036 T2 := Universal_Real;
5037 else
5038 T2 := B_Typ;
5039 end if;
5041 Set_Analyzed (Op2, False);
5042 Resolve (Op2, T2);
5043 end;
5045 else
5046 Resolve (N);
5047 end if;
5048 end Set_Mixed_Mode_Operand;
5050 ----------------------
5051 -- Set_Operand_Type --
5052 ----------------------
5054 procedure Set_Operand_Type (N : Node_Id) is
5055 begin
5056 if Etype (N) = Universal_Integer
5057 or else Etype (N) = Universal_Real
5058 then
5059 Set_Etype (N, T);
5060 end if;
5061 end Set_Operand_Type;
5063 -- Start of processing for Resolve_Arithmetic_Op
5065 begin
5066 if Comes_From_Source (N)
5067 and then Ekind (Entity (N)) = E_Function
5068 and then Is_Imported (Entity (N))
5069 and then Is_Intrinsic_Subprogram (Entity (N))
5070 then
5071 Resolve_Intrinsic_Operator (N, Typ);
5072 return;
5074 -- Special-case for mixed-mode universal expressions or fixed point type
5075 -- operation: each argument is resolved separately. The same treatment
5076 -- is required if one of the operands of a fixed point operation is
5077 -- universal real, since in this case we don't do a conversion to a
5078 -- specific fixed-point type (instead the expander handles the case).
5080 -- Set the type of the node to its universal interpretation because
5081 -- legality checks on an exponentiation operand need the context.
5083 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
5084 and then Present (Universal_Interpretation (L))
5085 and then Present (Universal_Interpretation (R))
5086 then
5087 Set_Etype (N, B_Typ);
5088 Resolve (L, Universal_Interpretation (L));
5089 Resolve (R, Universal_Interpretation (R));
5091 elsif (B_Typ = Universal_Real
5092 or else Etype (N) = Universal_Fixed
5093 or else (Etype (N) = Any_Fixed
5094 and then Is_Fixed_Point_Type (B_Typ))
5095 or else (Is_Fixed_Point_Type (B_Typ)
5096 and then (Is_Integer_Or_Universal (L)
5097 or else
5098 Is_Integer_Or_Universal (R))))
5099 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5100 then
5101 if TL = Universal_Integer or else TR = Universal_Integer then
5102 Check_For_Visible_Operator (N, B_Typ);
5103 end if;
5105 -- If context is a fixed type and one operand is integer, the other
5106 -- is resolved with the type of the context.
5108 if Is_Fixed_Point_Type (B_Typ)
5109 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5110 or else TL = Universal_Integer)
5111 then
5112 Resolve (R, B_Typ);
5113 Resolve (L, TL);
5115 elsif Is_Fixed_Point_Type (B_Typ)
5116 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5117 or else TR = Universal_Integer)
5118 then
5119 Resolve (L, B_Typ);
5120 Resolve (R, TR);
5122 else
5123 Set_Mixed_Mode_Operand (L, TR);
5124 Set_Mixed_Mode_Operand (R, TL);
5125 end if;
5127 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
5128 -- multiplying operators from being used when the expected type is
5129 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
5130 -- some cases where the expected type is actually Any_Real;
5131 -- Expected_Type_Is_Any_Real takes care of that case.
5133 if Etype (N) = Universal_Fixed
5134 or else Etype (N) = Any_Fixed
5135 then
5136 if B_Typ = Universal_Fixed
5137 and then not Expected_Type_Is_Any_Real (N)
5138 and then not Nkind_In (Parent (N), N_Type_Conversion,
5139 N_Unchecked_Type_Conversion)
5140 then
5141 Error_Msg_N ("type cannot be determined from context!", N);
5142 Error_Msg_N ("\explicit conversion to result type required", N);
5144 Set_Etype (L, Any_Type);
5145 Set_Etype (R, Any_Type);
5147 else
5148 if Ada_Version = Ada_83
5149 and then Etype (N) = Universal_Fixed
5150 and then not
5151 Nkind_In (Parent (N), N_Type_Conversion,
5152 N_Unchecked_Type_Conversion)
5153 then
5154 Error_Msg_N
5155 ("(Ada 83) fixed-point operation "
5156 & "needs explicit conversion", N);
5157 end if;
5159 -- The expected type is "any real type" in contexts like
5161 -- type T is delta <universal_fixed-expression> ...
5163 -- in which case we need to set the type to Universal_Real
5164 -- so that static expression evaluation will work properly.
5166 if Expected_Type_Is_Any_Real (N) then
5167 Set_Etype (N, Universal_Real);
5168 else
5169 Set_Etype (N, B_Typ);
5170 end if;
5171 end if;
5173 elsif Is_Fixed_Point_Type (B_Typ)
5174 and then (Is_Integer_Or_Universal (L)
5175 or else Nkind (L) = N_Real_Literal
5176 or else Nkind (R) = N_Real_Literal
5177 or else Is_Integer_Or_Universal (R))
5178 then
5179 Set_Etype (N, B_Typ);
5181 elsif Etype (N) = Any_Fixed then
5183 -- If no previous errors, this is only possible if one operand is
5184 -- overloaded and the context is universal. Resolve as such.
5186 Set_Etype (N, B_Typ);
5187 end if;
5189 else
5190 if (TL = Universal_Integer or else TL = Universal_Real)
5191 and then
5192 (TR = Universal_Integer or else TR = Universal_Real)
5193 then
5194 Check_For_Visible_Operator (N, B_Typ);
5195 end if;
5197 -- If the context is Universal_Fixed and the operands are also
5198 -- universal fixed, this is an error, unless there is only one
5199 -- applicable fixed_point type (usually Duration).
5201 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
5202 T := Unique_Fixed_Point_Type (N);
5204 if T = Any_Type then
5205 Set_Etype (N, T);
5206 return;
5207 else
5208 Resolve (L, T);
5209 Resolve (R, T);
5210 end if;
5212 else
5213 Resolve (L, B_Typ);
5214 Resolve (R, B_Typ);
5215 end if;
5217 -- If one of the arguments was resolved to a non-universal type.
5218 -- label the result of the operation itself with the same type.
5219 -- Do the same for the universal argument, if any.
5221 T := Intersect_Types (L, R);
5222 Set_Etype (N, Base_Type (T));
5223 Set_Operand_Type (L);
5224 Set_Operand_Type (R);
5225 end if;
5227 Generate_Operator_Reference (N, Typ);
5228 Analyze_Dimension (N);
5229 Eval_Arithmetic_Op (N);
5231 -- In SPARK, a multiplication or division with operands of fixed point
5232 -- types must be qualified or explicitly converted to identify the
5233 -- result type.
5235 if (Is_Fixed_Point_Type (Etype (L))
5236 or else Is_Fixed_Point_Type (Etype (R)))
5237 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5238 and then
5239 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
5240 then
5241 Check_SPARK_Restriction
5242 ("operation should be qualified or explicitly converted", N);
5243 end if;
5245 -- Set overflow and division checking bit
5247 if Nkind (N) in N_Op then
5248 if not Overflow_Checks_Suppressed (Etype (N)) then
5249 Enable_Overflow_Check (N);
5250 end if;
5252 -- Give warning if explicit division by zero
5254 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
5255 and then not Division_Checks_Suppressed (Etype (N))
5256 then
5257 Rop := Right_Opnd (N);
5259 if Compile_Time_Known_Value (Rop)
5260 and then ((Is_Integer_Type (Etype (Rop))
5261 and then Expr_Value (Rop) = Uint_0)
5262 or else
5263 (Is_Real_Type (Etype (Rop))
5264 and then Expr_Value_R (Rop) = Ureal_0))
5265 then
5266 -- Specialize the warning message according to the operation.
5267 -- The following warnings are for the case
5269 case Nkind (N) is
5270 when N_Op_Divide =>
5272 -- For division, we have two cases, for float division
5273 -- of an unconstrained float type, on a machine where
5274 -- Machine_Overflows is false, we don't get an exception
5275 -- at run-time, but rather an infinity or Nan. The Nan
5276 -- case is pretty obscure, so just warn about infinities.
5278 if Is_Floating_Point_Type (Typ)
5279 and then not Is_Constrained (Typ)
5280 and then not Machine_Overflows_On_Target
5281 then
5282 Error_Msg_N
5283 ("float division by zero, may generate "
5284 & "'+'/'- infinity??", Right_Opnd (N));
5286 -- For all other cases, we get a Constraint_Error
5288 else
5289 Apply_Compile_Time_Constraint_Error
5290 (N, "division by zero??", CE_Divide_By_Zero,
5291 Loc => Sloc (Right_Opnd (N)));
5292 end if;
5294 when N_Op_Rem =>
5295 Apply_Compile_Time_Constraint_Error
5296 (N, "rem with zero divisor??", CE_Divide_By_Zero,
5297 Loc => Sloc (Right_Opnd (N)));
5299 when N_Op_Mod =>
5300 Apply_Compile_Time_Constraint_Error
5301 (N, "mod with zero divisor??", CE_Divide_By_Zero,
5302 Loc => Sloc (Right_Opnd (N)));
5304 -- Division by zero can only happen with division, rem,
5305 -- and mod operations.
5307 when others =>
5308 raise Program_Error;
5309 end case;
5311 -- Otherwise just set the flag to check at run time
5313 else
5314 Activate_Division_Check (N);
5315 end if;
5316 end if;
5318 -- If Restriction No_Implicit_Conditionals is active, then it is
5319 -- violated if either operand can be negative for mod, or for rem
5320 -- if both operands can be negative.
5322 if Restriction_Check_Required (No_Implicit_Conditionals)
5323 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5324 then
5325 declare
5326 Lo : Uint;
5327 Hi : Uint;
5328 OK : Boolean;
5330 LNeg : Boolean;
5331 RNeg : Boolean;
5332 -- Set if corresponding operand might be negative
5334 begin
5335 Determine_Range
5336 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5337 LNeg := (not OK) or else Lo < 0;
5339 Determine_Range
5340 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5341 RNeg := (not OK) or else Lo < 0;
5343 -- Check if we will be generating conditionals. There are two
5344 -- cases where that can happen, first for REM, the only case
5345 -- is largest negative integer mod -1, where the division can
5346 -- overflow, but we still have to give the right result. The
5347 -- front end generates a test for this annoying case. Here we
5348 -- just test if both operands can be negative (that's what the
5349 -- expander does, so we match its logic here).
5351 -- The second case is mod where either operand can be negative.
5352 -- In this case, the back end has to generate additional tests.
5354 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5355 or else
5356 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5357 then
5358 Check_Restriction (No_Implicit_Conditionals, N);
5359 end if;
5360 end;
5361 end if;
5362 end if;
5364 Check_Unset_Reference (L);
5365 Check_Unset_Reference (R);
5366 Check_Function_Writable_Actuals (N);
5367 end Resolve_Arithmetic_Op;
5369 ------------------
5370 -- Resolve_Call --
5371 ------------------
5373 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5374 Loc : constant Source_Ptr := Sloc (N);
5375 Subp : constant Node_Id := Name (N);
5376 Nam : Entity_Id;
5377 I : Interp_Index;
5378 It : Interp;
5379 Norm_OK : Boolean;
5380 Scop : Entity_Id;
5381 Rtype : Entity_Id;
5383 function Same_Or_Aliased_Subprograms
5384 (S : Entity_Id;
5385 E : Entity_Id) return Boolean;
5386 -- Returns True if the subprogram entity S is the same as E or else
5387 -- S is an alias of E.
5389 ---------------------------------
5390 -- Same_Or_Aliased_Subprograms --
5391 ---------------------------------
5393 function Same_Or_Aliased_Subprograms
5394 (S : Entity_Id;
5395 E : Entity_Id) return Boolean
5397 Subp_Alias : constant Entity_Id := Alias (S);
5398 begin
5399 return S = E or else (Present (Subp_Alias) and then Subp_Alias = E);
5400 end Same_Or_Aliased_Subprograms;
5402 -- Start of processing for Resolve_Call
5404 begin
5405 -- The context imposes a unique interpretation with type Typ on a
5406 -- procedure or function call. Find the entity of the subprogram that
5407 -- yields the expected type, and propagate the corresponding formal
5408 -- constraints on the actuals. The caller has established that an
5409 -- interpretation exists, and emitted an error if not unique.
5411 -- First deal with the case of a call to an access-to-subprogram,
5412 -- dereference made explicit in Analyze_Call.
5414 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5415 if not Is_Overloaded (Subp) then
5416 Nam := Etype (Subp);
5418 else
5419 -- Find the interpretation whose type (a subprogram type) has a
5420 -- return type that is compatible with the context. Analysis of
5421 -- the node has established that one exists.
5423 Nam := Empty;
5425 Get_First_Interp (Subp, I, It);
5426 while Present (It.Typ) loop
5427 if Covers (Typ, Etype (It.Typ)) then
5428 Nam := It.Typ;
5429 exit;
5430 end if;
5432 Get_Next_Interp (I, It);
5433 end loop;
5435 if No (Nam) then
5436 raise Program_Error;
5437 end if;
5438 end if;
5440 -- If the prefix is not an entity, then resolve it
5442 if not Is_Entity_Name (Subp) then
5443 Resolve (Subp, Nam);
5444 end if;
5446 -- For an indirect call, we always invalidate checks, since we do not
5447 -- know whether the subprogram is local or global. Yes we could do
5448 -- better here, e.g. by knowing that there are no local subprograms,
5449 -- but it does not seem worth the effort. Similarly, we kill all
5450 -- knowledge of current constant values.
5452 Kill_Current_Values;
5454 -- If this is a procedure call which is really an entry call, do
5455 -- the conversion of the procedure call to an entry call. Protected
5456 -- operations use the same circuitry because the name in the call
5457 -- can be an arbitrary expression with special resolution rules.
5459 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5460 or else (Is_Entity_Name (Subp)
5461 and then Ekind (Entity (Subp)) = E_Entry)
5462 then
5463 Resolve_Entry_Call (N, Typ);
5464 Check_Elab_Call (N);
5466 -- Kill checks and constant values, as above for indirect case
5467 -- Who knows what happens when another task is activated?
5469 Kill_Current_Values;
5470 return;
5472 -- Normal subprogram call with name established in Resolve
5474 elsif not (Is_Type (Entity (Subp))) then
5475 Nam := Entity (Subp);
5476 Set_Entity_With_Checks (Subp, Nam);
5478 -- Otherwise we must have the case of an overloaded call
5480 else
5481 pragma Assert (Is_Overloaded (Subp));
5483 -- Initialize Nam to prevent warning (we know it will be assigned
5484 -- in the loop below, but the compiler does not know that).
5486 Nam := Empty;
5488 Get_First_Interp (Subp, I, It);
5489 while Present (It.Typ) loop
5490 if Covers (Typ, It.Typ) then
5491 Nam := It.Nam;
5492 Set_Entity_With_Checks (Subp, Nam);
5493 exit;
5494 end if;
5496 Get_Next_Interp (I, It);
5497 end loop;
5498 end if;
5500 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5501 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5502 and then Nkind (Subp) /= N_Explicit_Dereference
5503 and then Present (Parameter_Associations (N))
5504 then
5505 -- The prefix is a parameterless function call that returns an access
5506 -- to subprogram. If parameters are present in the current call, add
5507 -- add an explicit dereference. We use the base type here because
5508 -- within an instance these may be subtypes.
5510 -- The dereference is added either in Analyze_Call or here. Should
5511 -- be consolidated ???
5513 Set_Is_Overloaded (Subp, False);
5514 Set_Etype (Subp, Etype (Nam));
5515 Insert_Explicit_Dereference (Subp);
5516 Nam := Designated_Type (Etype (Nam));
5517 Resolve (Subp, Nam);
5518 end if;
5520 -- Check that a call to Current_Task does not occur in an entry body
5522 if Is_RTE (Nam, RE_Current_Task) then
5523 declare
5524 P : Node_Id;
5526 begin
5527 P := N;
5528 loop
5529 P := Parent (P);
5531 -- Exclude calls that occur within the default of a formal
5532 -- parameter of the entry, since those are evaluated outside
5533 -- of the body.
5535 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5537 if Nkind (P) = N_Entry_Body
5538 or else (Nkind (P) = N_Subprogram_Body
5539 and then Is_Entry_Barrier_Function (P))
5540 then
5541 Rtype := Etype (N);
5542 Error_Msg_Warn := SPARK_Mode /= On;
5543 Error_Msg_NE
5544 ("& should not be used in entry body (RM C.7(17))<<",
5545 N, Nam);
5546 Error_Msg_NE ("\Program_Error [<<", N, Nam);
5547 Rewrite (N,
5548 Make_Raise_Program_Error (Loc,
5549 Reason => PE_Current_Task_In_Entry_Body));
5550 Set_Etype (N, Rtype);
5551 return;
5552 end if;
5553 end loop;
5554 end;
5555 end if;
5557 -- Check that a procedure call does not occur in the context of the
5558 -- entry call statement of a conditional or timed entry call. Note that
5559 -- the case of a call to a subprogram renaming of an entry will also be
5560 -- rejected. The test for N not being an N_Entry_Call_Statement is
5561 -- defensive, covering the possibility that the processing of entry
5562 -- calls might reach this point due to later modifications of the code
5563 -- above.
5565 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5566 and then Nkind (N) /= N_Entry_Call_Statement
5567 and then Entry_Call_Statement (Parent (N)) = N
5568 then
5569 if Ada_Version < Ada_2005 then
5570 Error_Msg_N ("entry call required in select statement", N);
5572 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5573 -- for a procedure_or_entry_call, the procedure_name or
5574 -- procedure_prefix of the procedure_call_statement shall denote
5575 -- an entry renamed by a procedure, or (a view of) a primitive
5576 -- subprogram of a limited interface whose first parameter is
5577 -- a controlling parameter.
5579 elsif Nkind (N) = N_Procedure_Call_Statement
5580 and then not Is_Renamed_Entry (Nam)
5581 and then not Is_Controlling_Limited_Procedure (Nam)
5582 then
5583 Error_Msg_N
5584 ("entry call or dispatching primitive of interface required", N);
5585 end if;
5586 end if;
5588 -- If the SPARK_05 restriction is active, we are not allowed
5589 -- to have a call to a subprogram before we see its completion.
5591 if not Has_Completion (Nam)
5592 and then Restriction_Check_Required (SPARK_05)
5594 -- Don't flag strange internal calls
5596 and then Comes_From_Source (N)
5597 and then Comes_From_Source (Nam)
5599 -- Only flag calls in extended main source
5601 and then In_Extended_Main_Source_Unit (Nam)
5602 and then In_Extended_Main_Source_Unit (N)
5604 -- Exclude enumeration literals from this processing
5606 and then Ekind (Nam) /= E_Enumeration_Literal
5607 then
5608 Check_SPARK_Restriction
5609 ("call to subprogram cannot appear before its body", N);
5610 end if;
5612 -- Check that this is not a call to a protected procedure or entry from
5613 -- within a protected function.
5615 Check_Internal_Protected_Use (N, Nam);
5617 -- Freeze the subprogram name if not in a spec-expression. Note that
5618 -- we freeze procedure calls as well as function calls. Procedure calls
5619 -- are not frozen according to the rules (RM 13.14(14)) because it is
5620 -- impossible to have a procedure call to a non-frozen procedure in
5621 -- pure Ada, but in the code that we generate in the expander, this
5622 -- rule needs extending because we can generate procedure calls that
5623 -- need freezing.
5625 -- In Ada 2012, expression functions may be called within pre/post
5626 -- conditions of subsequent functions or expression functions. Such
5627 -- calls do not freeze when they appear within generated bodies,
5628 -- (including the body of another expression function) which would
5629 -- place the freeze node in the wrong scope. An expression function
5630 -- is frozen in the usual fashion, by the appearance of a real body,
5631 -- or at the end of a declarative part.
5633 if Is_Entity_Name (Subp) and then not In_Spec_Expression
5634 and then not Is_Expression_Function (Current_Scope)
5635 and then
5636 (not Is_Expression_Function (Entity (Subp))
5637 or else Scope (Entity (Subp)) = Current_Scope)
5638 then
5639 Freeze_Expression (Subp);
5640 end if;
5642 -- For a predefined operator, the type of the result is the type imposed
5643 -- by context, except for a predefined operation on universal fixed.
5644 -- Otherwise The type of the call is the type returned by the subprogram
5645 -- being called.
5647 if Is_Predefined_Op (Nam) then
5648 if Etype (N) /= Universal_Fixed then
5649 Set_Etype (N, Typ);
5650 end if;
5652 -- If the subprogram returns an array type, and the context requires the
5653 -- component type of that array type, the node is really an indexing of
5654 -- the parameterless call. Resolve as such. A pathological case occurs
5655 -- when the type of the component is an access to the array type. In
5656 -- this case the call is truly ambiguous.
5658 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5659 and then
5660 ((Is_Array_Type (Etype (Nam))
5661 and then Covers (Typ, Component_Type (Etype (Nam))))
5662 or else
5663 (Is_Access_Type (Etype (Nam))
5664 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5665 and then
5666 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))))
5667 then
5668 declare
5669 Index_Node : Node_Id;
5670 New_Subp : Node_Id;
5671 Ret_Type : constant Entity_Id := Etype (Nam);
5673 begin
5674 if Is_Access_Type (Ret_Type)
5675 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5676 then
5677 Error_Msg_N
5678 ("cannot disambiguate function call and indexing", N);
5679 else
5680 New_Subp := Relocate_Node (Subp);
5682 -- The called entity may be an explicit dereference, in which
5683 -- case there is no entity to set.
5685 if Nkind (New_Subp) /= N_Explicit_Dereference then
5686 Set_Entity (Subp, Nam);
5687 end if;
5689 if (Is_Array_Type (Ret_Type)
5690 and then Component_Type (Ret_Type) /= Any_Type)
5691 or else
5692 (Is_Access_Type (Ret_Type)
5693 and then
5694 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5695 then
5696 if Needs_No_Actuals (Nam) then
5698 -- Indexed call to a parameterless function
5700 Index_Node :=
5701 Make_Indexed_Component (Loc,
5702 Prefix =>
5703 Make_Function_Call (Loc, Name => New_Subp),
5704 Expressions => Parameter_Associations (N));
5705 else
5706 -- An Ada 2005 prefixed call to a primitive operation
5707 -- whose first parameter is the prefix. This prefix was
5708 -- prepended to the parameter list, which is actually a
5709 -- list of indexes. Remove the prefix in order to build
5710 -- the proper indexed component.
5712 Index_Node :=
5713 Make_Indexed_Component (Loc,
5714 Prefix =>
5715 Make_Function_Call (Loc,
5716 Name => New_Subp,
5717 Parameter_Associations =>
5718 New_List
5719 (Remove_Head (Parameter_Associations (N)))),
5720 Expressions => Parameter_Associations (N));
5721 end if;
5723 -- Preserve the parenthesis count of the node
5725 Set_Paren_Count (Index_Node, Paren_Count (N));
5727 -- Since we are correcting a node classification error made
5728 -- by the parser, we call Replace rather than Rewrite.
5730 Replace (N, Index_Node);
5732 Set_Etype (Prefix (N), Ret_Type);
5733 Set_Etype (N, Typ);
5734 Resolve_Indexed_Component (N, Typ);
5735 Check_Elab_Call (Prefix (N));
5736 end if;
5737 end if;
5739 return;
5740 end;
5742 else
5743 Set_Etype (N, Etype (Nam));
5744 end if;
5746 -- In the case where the call is to an overloaded subprogram, Analyze
5747 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5748 -- such a case Normalize_Actuals needs to be called once more to order
5749 -- the actuals correctly. Otherwise the call will have the ordering
5750 -- given by the last overloaded subprogram whether this is the correct
5751 -- one being called or not.
5753 if Is_Overloaded (Subp) then
5754 Normalize_Actuals (N, Nam, False, Norm_OK);
5755 pragma Assert (Norm_OK);
5756 end if;
5758 -- In any case, call is fully resolved now. Reset Overload flag, to
5759 -- prevent subsequent overload resolution if node is analyzed again
5761 Set_Is_Overloaded (Subp, False);
5762 Set_Is_Overloaded (N, False);
5764 -- If we are calling the current subprogram from immediately within its
5765 -- body, then that is the case where we can sometimes detect cases of
5766 -- infinite recursion statically. Do not try this in case restriction
5767 -- No_Recursion is in effect anyway, and do it only for source calls.
5769 if Comes_From_Source (N) then
5770 Scop := Current_Scope;
5772 -- Check violation of SPARK_05 restriction which does not permit
5773 -- a subprogram body to contain a call to the subprogram directly.
5775 if Restriction_Check_Required (SPARK_05)
5776 and then Same_Or_Aliased_Subprograms (Nam, Scop)
5777 then
5778 Check_SPARK_Restriction
5779 ("subprogram may not contain direct call to itself", N);
5780 end if;
5782 -- Issue warning for possible infinite recursion in the absence
5783 -- of the No_Recursion restriction.
5785 if Same_Or_Aliased_Subprograms (Nam, Scop)
5786 and then not Restriction_Active (No_Recursion)
5787 and then Check_Infinite_Recursion (N)
5788 then
5789 -- Here we detected and flagged an infinite recursion, so we do
5790 -- not need to test the case below for further warnings. Also we
5791 -- are all done if we now have a raise SE node.
5793 if Nkind (N) = N_Raise_Storage_Error then
5794 return;
5795 end if;
5797 -- If call is to immediately containing subprogram, then check for
5798 -- the case of a possible run-time detectable infinite recursion.
5800 else
5801 Scope_Loop : while Scop /= Standard_Standard loop
5802 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5804 -- Although in general case, recursion is not statically
5805 -- checkable, the case of calling an immediately containing
5806 -- subprogram is easy to catch.
5808 Check_Restriction (No_Recursion, N);
5810 -- If the recursive call is to a parameterless subprogram,
5811 -- then even if we can't statically detect infinite
5812 -- recursion, this is pretty suspicious, and we output a
5813 -- warning. Furthermore, we will try later to detect some
5814 -- cases here at run time by expanding checking code (see
5815 -- Detect_Infinite_Recursion in package Exp_Ch6).
5817 -- If the recursive call is within a handler, do not emit a
5818 -- warning, because this is a common idiom: loop until input
5819 -- is correct, catch illegal input in handler and restart.
5821 if No (First_Formal (Nam))
5822 and then Etype (Nam) = Standard_Void_Type
5823 and then not Error_Posted (N)
5824 and then Nkind (Parent (N)) /= N_Exception_Handler
5825 then
5826 -- For the case of a procedure call. We give the message
5827 -- only if the call is the first statement in a sequence
5828 -- of statements, or if all previous statements are
5829 -- simple assignments. This is simply a heuristic to
5830 -- decrease false positives, without losing too many good
5831 -- warnings. The idea is that these previous statements
5832 -- may affect global variables the procedure depends on.
5833 -- We also exclude raise statements, that may arise from
5834 -- constraint checks and are probably unrelated to the
5835 -- intended control flow.
5837 if Nkind (N) = N_Procedure_Call_Statement
5838 and then Is_List_Member (N)
5839 then
5840 declare
5841 P : Node_Id;
5842 begin
5843 P := Prev (N);
5844 while Present (P) loop
5845 if not Nkind_In (P, N_Assignment_Statement,
5846 N_Raise_Constraint_Error)
5847 then
5848 exit Scope_Loop;
5849 end if;
5851 Prev (P);
5852 end loop;
5853 end;
5854 end if;
5856 -- Do not give warning if we are in a conditional context
5858 declare
5859 K : constant Node_Kind := Nkind (Parent (N));
5860 begin
5861 if (K = N_Loop_Statement
5862 and then Present (Iteration_Scheme (Parent (N))))
5863 or else K = N_If_Statement
5864 or else K = N_Elsif_Part
5865 or else K = N_Case_Statement_Alternative
5866 then
5867 exit Scope_Loop;
5868 end if;
5869 end;
5871 -- Here warning is to be issued
5873 Set_Has_Recursive_Call (Nam);
5874 Error_Msg_Warn := SPARK_Mode /= On;
5875 Error_Msg_N ("possible infinite recursion<<!", N);
5876 Error_Msg_N ("\Storage_Error ]<<!", N);
5877 end if;
5879 exit Scope_Loop;
5880 end if;
5882 Scop := Scope (Scop);
5883 end loop Scope_Loop;
5884 end if;
5885 end if;
5887 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5889 Check_Obsolescent_2005_Entity (Nam, Subp);
5891 -- If subprogram name is a predefined operator, it was given in
5892 -- functional notation. Replace call node with operator node, so
5893 -- that actuals can be resolved appropriately.
5895 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5896 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5897 return;
5899 elsif Present (Alias (Nam))
5900 and then Is_Predefined_Op (Alias (Nam))
5901 then
5902 Resolve_Actuals (N, Nam);
5903 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5904 return;
5905 end if;
5907 -- Create a transient scope if the resulting type requires it
5909 -- There are several notable exceptions:
5911 -- a) In init procs, the transient scope overhead is not needed, and is
5912 -- even incorrect when the call is a nested initialization call for a
5913 -- component whose expansion may generate adjust calls. However, if the
5914 -- call is some other procedure call within an initialization procedure
5915 -- (for example a call to Create_Task in the init_proc of the task
5916 -- run-time record) a transient scope must be created around this call.
5918 -- b) Enumeration literal pseudo-calls need no transient scope
5920 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5921 -- functions) do not use the secondary stack even though the return
5922 -- type may be unconstrained.
5924 -- d) Calls to a build-in-place function, since such functions may
5925 -- allocate their result directly in a target object, and cases where
5926 -- the result does get allocated in the secondary stack are checked for
5927 -- within the specialized Exp_Ch6 procedures for expanding those
5928 -- build-in-place calls.
5930 -- e) If the subprogram is marked Inline_Always, then even if it returns
5931 -- an unconstrained type the call does not require use of the secondary
5932 -- stack. However, inlining will only take place if the body to inline
5933 -- is already present. It may not be available if e.g. the subprogram is
5934 -- declared in a child instance.
5936 -- If this is an initialization call for a type whose construction
5937 -- uses the secondary stack, and it is not a nested call to initialize
5938 -- a component, we do need to create a transient scope for it. We
5939 -- check for this by traversing the type in Check_Initialization_Call.
5941 if Is_Inlined (Nam)
5942 and then Has_Pragma_Inline (Nam)
5943 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5944 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5945 then
5946 null;
5948 elsif Ekind (Nam) = E_Enumeration_Literal
5949 or else Is_Build_In_Place_Function (Nam)
5950 or else Is_Intrinsic_Subprogram (Nam)
5951 then
5952 null;
5954 elsif Expander_Active
5955 and then Is_Type (Etype (Nam))
5956 and then Requires_Transient_Scope (Etype (Nam))
5957 and then
5958 (not Within_Init_Proc
5959 or else
5960 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5961 then
5962 Establish_Transient_Scope (N, Sec_Stack => True);
5964 -- If the call appears within the bounds of a loop, it will
5965 -- be rewritten and reanalyzed, nothing left to do here.
5967 if Nkind (N) /= N_Function_Call then
5968 return;
5969 end if;
5971 elsif Is_Init_Proc (Nam)
5972 and then not Within_Init_Proc
5973 then
5974 Check_Initialization_Call (N, Nam);
5975 end if;
5977 -- A protected function cannot be called within the definition of the
5978 -- enclosing protected type.
5980 if Is_Protected_Type (Scope (Nam))
5981 and then In_Open_Scopes (Scope (Nam))
5982 and then not Has_Completion (Scope (Nam))
5983 then
5984 Error_Msg_NE
5985 ("& cannot be called before end of protected definition", N, Nam);
5986 end if;
5988 -- Propagate interpretation to actuals, and add default expressions
5989 -- where needed.
5991 if Present (First_Formal (Nam)) then
5992 Resolve_Actuals (N, Nam);
5994 -- Overloaded literals are rewritten as function calls, for purpose of
5995 -- resolution. After resolution, we can replace the call with the
5996 -- literal itself.
5998 elsif Ekind (Nam) = E_Enumeration_Literal then
5999 Copy_Node (Subp, N);
6000 Resolve_Entity_Name (N, Typ);
6002 -- Avoid validation, since it is a static function call
6004 Generate_Reference (Nam, Subp);
6005 return;
6006 end if;
6008 -- If the subprogram is not global, then kill all saved values and
6009 -- checks. This is a bit conservative, since in many cases we could do
6010 -- better, but it is not worth the effort. Similarly, we kill constant
6011 -- values. However we do not need to do this for internal entities
6012 -- (unless they are inherited user-defined subprograms), since they
6013 -- are not in the business of molesting local values.
6015 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6016 -- kill all checks and values for calls to global subprograms. This
6017 -- takes care of the case where an access to a local subprogram is
6018 -- taken, and could be passed directly or indirectly and then called
6019 -- from almost any context.
6021 -- Note: we do not do this step till after resolving the actuals. That
6022 -- way we still take advantage of the current value information while
6023 -- scanning the actuals.
6025 -- We suppress killing values if we are processing the nodes associated
6026 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6027 -- type kills all the values as part of analyzing the code that
6028 -- initializes the dispatch tables.
6030 if Inside_Freezing_Actions = 0
6031 and then (not Is_Library_Level_Entity (Nam)
6032 or else Suppress_Value_Tracking_On_Call
6033 (Nearest_Dynamic_Scope (Current_Scope)))
6034 and then (Comes_From_Source (Nam)
6035 or else (Present (Alias (Nam))
6036 and then Comes_From_Source (Alias (Nam))))
6037 then
6038 Kill_Current_Values;
6039 end if;
6041 -- If we are warning about unread OUT parameters, this is the place to
6042 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
6043 -- after the above call to Kill_Current_Values (since that call clears
6044 -- the Last_Assignment field of all local variables).
6046 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
6047 and then Comes_From_Source (N)
6048 and then In_Extended_Main_Source_Unit (N)
6049 then
6050 declare
6051 F : Entity_Id;
6052 A : Node_Id;
6054 begin
6055 F := First_Formal (Nam);
6056 A := First_Actual (N);
6057 while Present (F) and then Present (A) loop
6058 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
6059 and then Warn_On_Modified_As_Out_Parameter (F)
6060 and then Is_Entity_Name (A)
6061 and then Present (Entity (A))
6062 and then Comes_From_Source (N)
6063 and then Safe_To_Capture_Value (N, Entity (A))
6064 then
6065 Set_Last_Assignment (Entity (A), A);
6066 end if;
6068 Next_Formal (F);
6069 Next_Actual (A);
6070 end loop;
6071 end;
6072 end if;
6074 -- If the subprogram is a primitive operation, check whether or not
6075 -- it is a correct dispatching call.
6077 if Is_Overloadable (Nam)
6078 and then Is_Dispatching_Operation (Nam)
6079 then
6080 Check_Dispatching_Call (N);
6082 elsif Ekind (Nam) /= E_Subprogram_Type
6083 and then Is_Abstract_Subprogram (Nam)
6084 and then not In_Instance
6085 then
6086 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
6087 end if;
6089 -- If this is a dispatching call, generate the appropriate reference,
6090 -- for better source navigation in GPS.
6092 if Is_Overloadable (Nam)
6093 and then Present (Controlling_Argument (N))
6094 then
6095 Generate_Reference (Nam, Subp, 'R');
6097 -- Normal case, not a dispatching call: generate a call reference
6099 else
6100 Generate_Reference (Nam, Subp, 's');
6101 end if;
6103 if Is_Intrinsic_Subprogram (Nam) then
6104 Check_Intrinsic_Call (N);
6105 end if;
6107 -- Check for violation of restriction No_Specific_Termination_Handlers
6108 -- and warn on a potentially blocking call to Abort_Task.
6110 if Restriction_Check_Required (No_Specific_Termination_Handlers)
6111 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
6112 or else
6113 Is_RTE (Nam, RE_Specific_Handler))
6114 then
6115 Check_Restriction (No_Specific_Termination_Handlers, N);
6117 elsif Is_RTE (Nam, RE_Abort_Task) then
6118 Check_Potentially_Blocking_Operation (N);
6119 end if;
6121 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
6122 -- timing event violates restriction No_Relative_Delay (AI-0211). We
6123 -- need to check the second argument to determine whether it is an
6124 -- absolute or relative timing event.
6126 if Restriction_Check_Required (No_Relative_Delay)
6127 and then Is_RTE (Nam, RE_Set_Handler)
6128 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
6129 then
6130 Check_Restriction (No_Relative_Delay, N);
6131 end if;
6133 -- Issue an error for a call to an eliminated subprogram. This routine
6134 -- will not perform the check if the call appears within a default
6135 -- expression.
6137 Check_For_Eliminated_Subprogram (Subp, Nam);
6139 -- In formal mode, the primitive operations of a tagged type or type
6140 -- extension do not include functions that return the tagged type.
6142 if Nkind (N) = N_Function_Call
6143 and then Is_Tagged_Type (Etype (N))
6144 and then Is_Entity_Name (Name (N))
6145 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
6146 then
6147 Check_SPARK_Restriction ("function not inherited", N);
6148 end if;
6150 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
6151 -- class-wide and the call dispatches on result in a context that does
6152 -- not provide a tag, the call raises Program_Error.
6154 if Nkind (N) = N_Function_Call
6155 and then In_Instance
6156 and then Is_Generic_Actual_Type (Typ)
6157 and then Is_Class_Wide_Type (Typ)
6158 and then Has_Controlling_Result (Nam)
6159 and then Nkind (Parent (N)) = N_Object_Declaration
6160 then
6161 -- Verify that none of the formals are controlling
6163 declare
6164 Call_OK : Boolean := False;
6165 F : Entity_Id;
6167 begin
6168 F := First_Formal (Nam);
6169 while Present (F) loop
6170 if Is_Controlling_Formal (F) then
6171 Call_OK := True;
6172 exit;
6173 end if;
6175 Next_Formal (F);
6176 end loop;
6178 if not Call_OK then
6179 Error_Msg_Warn := SPARK_Mode /= On;
6180 Error_Msg_N ("!cannot determine tag of result<<", N);
6181 Error_Msg_N ("\Program_Error [<<!", N);
6182 Insert_Action (N,
6183 Make_Raise_Program_Error (Sloc (N),
6184 Reason => PE_Explicit_Raise));
6185 end if;
6186 end;
6187 end if;
6189 -- Check for calling a function with OUT or IN OUT parameter when the
6190 -- calling context (us right now) is not Ada 2012, so does not allow
6191 -- OUT or IN OUT parameters in function calls.
6193 if Ada_Version < Ada_2012
6194 and then Ekind (Nam) = E_Function
6195 and then Has_Out_Or_In_Out_Parameter (Nam)
6196 then
6197 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
6198 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
6199 end if;
6201 -- Check the dimensions of the actuals in the call. For function calls,
6202 -- propagate the dimensions from the returned type to N.
6204 Analyze_Dimension_Call (N, Nam);
6206 -- All done, evaluate call and deal with elaboration issues
6208 Eval_Call (N);
6209 Check_Elab_Call (N);
6211 -- In GNATprove mode, expansion is disabled, but we want to inline some
6212 -- subprograms to facilitate formal verification. Indirect calls through
6213 -- a subprogram type or within a generic cannot be inlined. Inlining is
6214 -- performed only for calls subject to SPARK_Mode on.
6216 if GNATprove_Mode
6217 and then SPARK_Mode = On
6218 and then Is_Overloadable (Nam)
6219 and then not Inside_A_Generic
6220 then
6221 -- Retrieve the body to inline from the ultimate alias of Nam, if
6222 -- there is one, otherwise calls that should be inlined end up not
6223 -- being inlined.
6225 declare
6226 Nam_UA : constant Entity_Id := Ultimate_Alias (Nam);
6227 Decl : constant Node_Id := Unit_Declaration_Node (Nam_UA);
6228 Body_Id : constant Entity_Id := Corresponding_Body (Decl);
6230 begin
6231 -- If the subprogram is not eligible for inlining in GNATprove
6232 -- mode, do nothing.
6234 if Nkind (Decl) /= N_Subprogram_Declaration
6235 or else not Is_Inlined_Always (Nam_UA)
6236 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
6237 then
6238 null;
6240 -- Calls cannot be inlined inside assertions, as GNATprove treats
6241 -- assertions as logic expressions.
6243 elsif In_Assertion_Expr /= 0 then
6244 Error_Msg_NE ("?no contextual analysis of &", N, Nam);
6245 Error_Msg_N ("\call appears in assertion expression", N);
6246 Set_Is_Inlined_Always (Nam_UA, False);
6248 -- Inlining should not be performed during pre-analysis
6250 elsif Full_Analysis then
6252 -- With the one-pass inlining technique, a call cannot be
6253 -- inlined if the corresponding body has not been seen yet.
6255 if No (Body_Id) then
6256 Error_Msg_NE
6257 ("?no contextual analysis of & (body not seen yet)",
6258 N, Nam);
6259 Set_Is_Inlined_Always (Nam_UA, False);
6261 -- Nothing to do if there is no body to inline, indicating that
6262 -- the subprogram is not suitable for inlining in GNATprove
6263 -- mode.
6265 elsif No (Body_To_Inline (Decl)) then
6266 null;
6268 -- Calls cannot be inlined inside potentially unevaluated
6269 -- expressions, as this would create complex actions inside
6270 -- expressions, that are not handled by GNATprove.
6272 elsif Is_Potentially_Unevaluated (N) then
6273 Error_Msg_NE ("?no contextual analysis of &", N, Nam);
6274 Error_Msg_N
6275 ("\call appears in potentially unevaluated context", N);
6276 Set_Is_Inlined_Always (Nam_UA, False);
6278 -- Otherwise, inline the call
6280 else
6281 Expand_Inlined_Call (N, Nam_UA, Nam);
6282 end if;
6283 end if;
6284 end;
6285 end if;
6287 Warn_On_Overlapping_Actuals (Nam, N);
6288 end Resolve_Call;
6290 -----------------------------
6291 -- Resolve_Case_Expression --
6292 -----------------------------
6294 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
6295 Alt : Node_Id;
6297 begin
6298 Alt := First (Alternatives (N));
6299 while Present (Alt) loop
6300 Resolve (Expression (Alt), Typ);
6301 Next (Alt);
6302 end loop;
6304 Set_Etype (N, Typ);
6305 Eval_Case_Expression (N);
6306 end Resolve_Case_Expression;
6308 -------------------------------
6309 -- Resolve_Character_Literal --
6310 -------------------------------
6312 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
6313 B_Typ : constant Entity_Id := Base_Type (Typ);
6314 C : Entity_Id;
6316 begin
6317 -- Verify that the character does belong to the type of the context
6319 Set_Etype (N, B_Typ);
6320 Eval_Character_Literal (N);
6322 -- Wide_Wide_Character literals must always be defined, since the set
6323 -- of wide wide character literals is complete, i.e. if a character
6324 -- literal is accepted by the parser, then it is OK for wide wide
6325 -- character (out of range character literals are rejected).
6327 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6328 return;
6330 -- Always accept character literal for type Any_Character, which
6331 -- occurs in error situations and in comparisons of literals, both
6332 -- of which should accept all literals.
6334 elsif B_Typ = Any_Character then
6335 return;
6337 -- For Standard.Character or a type derived from it, check that the
6338 -- literal is in range.
6340 elsif Root_Type (B_Typ) = Standard_Character then
6341 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6342 return;
6343 end if;
6345 -- For Standard.Wide_Character or a type derived from it, check that the
6346 -- literal is in range.
6348 elsif Root_Type (B_Typ) = Standard_Wide_Character then
6349 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6350 return;
6351 end if;
6353 -- For Standard.Wide_Wide_Character or a type derived from it, we
6354 -- know the literal is in range, since the parser checked.
6356 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6357 return;
6359 -- If the entity is already set, this has already been resolved in a
6360 -- generic context, or comes from expansion. Nothing else to do.
6362 elsif Present (Entity (N)) then
6363 return;
6365 -- Otherwise we have a user defined character type, and we can use the
6366 -- standard visibility mechanisms to locate the referenced entity.
6368 else
6369 C := Current_Entity (N);
6370 while Present (C) loop
6371 if Etype (C) = B_Typ then
6372 Set_Entity_With_Checks (N, C);
6373 Generate_Reference (C, N);
6374 return;
6375 end if;
6377 C := Homonym (C);
6378 end loop;
6379 end if;
6381 -- If we fall through, then the literal does not match any of the
6382 -- entries of the enumeration type. This isn't just a constraint error
6383 -- situation, it is an illegality (see RM 4.2).
6385 Error_Msg_NE
6386 ("character not defined for }", N, First_Subtype (B_Typ));
6387 end Resolve_Character_Literal;
6389 ---------------------------
6390 -- Resolve_Comparison_Op --
6391 ---------------------------
6393 -- Context requires a boolean type, and plays no role in resolution.
6394 -- Processing identical to that for equality operators. The result type is
6395 -- the base type, which matters when pathological subtypes of booleans with
6396 -- limited ranges are used.
6398 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6399 L : constant Node_Id := Left_Opnd (N);
6400 R : constant Node_Id := Right_Opnd (N);
6401 T : Entity_Id;
6403 begin
6404 -- If this is an intrinsic operation which is not predefined, use the
6405 -- types of its declared arguments to resolve the possibly overloaded
6406 -- operands. Otherwise the operands are unambiguous and specify the
6407 -- expected type.
6409 if Scope (Entity (N)) /= Standard_Standard then
6410 T := Etype (First_Entity (Entity (N)));
6412 else
6413 T := Find_Unique_Type (L, R);
6415 if T = Any_Fixed then
6416 T := Unique_Fixed_Point_Type (L);
6417 end if;
6418 end if;
6420 Set_Etype (N, Base_Type (Typ));
6421 Generate_Reference (T, N, ' ');
6423 -- Skip remaining processing if already set to Any_Type
6425 if T = Any_Type then
6426 return;
6427 end if;
6429 -- Deal with other error cases
6431 if T = Any_String or else
6432 T = Any_Composite or else
6433 T = Any_Character
6434 then
6435 if T = Any_Character then
6436 Ambiguous_Character (L);
6437 else
6438 Error_Msg_N ("ambiguous operands for comparison", N);
6439 end if;
6441 Set_Etype (N, Any_Type);
6442 return;
6443 end if;
6445 -- Resolve the operands if types OK
6447 Resolve (L, T);
6448 Resolve (R, T);
6449 Check_Unset_Reference (L);
6450 Check_Unset_Reference (R);
6451 Generate_Operator_Reference (N, T);
6452 Check_Low_Bound_Tested (N);
6454 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6455 -- types or array types except String.
6457 if Is_Boolean_Type (T) then
6458 Check_SPARK_Restriction
6459 ("comparison is not defined on Boolean type", N);
6461 elsif Is_Array_Type (T)
6462 and then Base_Type (T) /= Standard_String
6463 then
6464 Check_SPARK_Restriction
6465 ("comparison is not defined on array types other than String", N);
6466 end if;
6468 -- Check comparison on unordered enumeration
6470 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
6471 Error_Msg_Sloc := Sloc (Etype (L));
6472 Error_Msg_NE
6473 ("comparison on unordered enumeration type& declared#?U?",
6474 N, Etype (L));
6475 end if;
6477 -- Evaluate the relation (note we do this after the above check since
6478 -- this Eval call may change N to True/False.
6480 Analyze_Dimension (N);
6481 Eval_Relational_Op (N);
6482 end Resolve_Comparison_Op;
6484 -----------------------------------------
6485 -- Resolve_Discrete_Subtype_Indication --
6486 -----------------------------------------
6488 procedure Resolve_Discrete_Subtype_Indication
6489 (N : Node_Id;
6490 Typ : Entity_Id)
6492 R : Node_Id;
6493 S : Entity_Id;
6495 begin
6496 Analyze (Subtype_Mark (N));
6497 S := Entity (Subtype_Mark (N));
6499 if Nkind (Constraint (N)) /= N_Range_Constraint then
6500 Error_Msg_N ("expect range constraint for discrete type", N);
6501 Set_Etype (N, Any_Type);
6503 else
6504 R := Range_Expression (Constraint (N));
6506 if R = Error then
6507 return;
6508 end if;
6510 Analyze (R);
6512 if Base_Type (S) /= Base_Type (Typ) then
6513 Error_Msg_NE
6514 ("expect subtype of }", N, First_Subtype (Typ));
6516 -- Rewrite the constraint as a range of Typ
6517 -- to allow compilation to proceed further.
6519 Set_Etype (N, Typ);
6520 Rewrite (Low_Bound (R),
6521 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6522 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6523 Attribute_Name => Name_First));
6524 Rewrite (High_Bound (R),
6525 Make_Attribute_Reference (Sloc (High_Bound (R)),
6526 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6527 Attribute_Name => Name_First));
6529 else
6530 Resolve (R, Typ);
6531 Set_Etype (N, Etype (R));
6533 -- Additionally, we must check that the bounds are compatible
6534 -- with the given subtype, which might be different from the
6535 -- type of the context.
6537 Apply_Range_Check (R, S);
6539 -- ??? If the above check statically detects a Constraint_Error
6540 -- it replaces the offending bound(s) of the range R with a
6541 -- Constraint_Error node. When the itype which uses these bounds
6542 -- is frozen the resulting call to Duplicate_Subexpr generates
6543 -- a new temporary for the bounds.
6545 -- Unfortunately there are other itypes that are also made depend
6546 -- on these bounds, so when Duplicate_Subexpr is called they get
6547 -- a forward reference to the newly created temporaries and Gigi
6548 -- aborts on such forward references. This is probably sign of a
6549 -- more fundamental problem somewhere else in either the order of
6550 -- itype freezing or the way certain itypes are constructed.
6552 -- To get around this problem we call Remove_Side_Effects right
6553 -- away if either bounds of R are a Constraint_Error.
6555 declare
6556 L : constant Node_Id := Low_Bound (R);
6557 H : constant Node_Id := High_Bound (R);
6559 begin
6560 if Nkind (L) = N_Raise_Constraint_Error then
6561 Remove_Side_Effects (L);
6562 end if;
6564 if Nkind (H) = N_Raise_Constraint_Error then
6565 Remove_Side_Effects (H);
6566 end if;
6567 end;
6569 Check_Unset_Reference (Low_Bound (R));
6570 Check_Unset_Reference (High_Bound (R));
6571 end if;
6572 end if;
6573 end Resolve_Discrete_Subtype_Indication;
6575 -------------------------
6576 -- Resolve_Entity_Name --
6577 -------------------------
6579 -- Used to resolve identifiers and expanded names
6581 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6582 function Appears_In_Check (Nod : Node_Id) return Boolean;
6583 -- Denote whether an arbitrary node Nod appears in a check node
6585 function Is_OK_Volatile_Context
6586 (Context : Node_Id;
6587 Obj_Ref : Node_Id) return Boolean;
6588 -- Determine whether node Context denotes a "non-interfering context"
6589 -- (as defined in SPARK RM 7.1.3(13)) where volatile reference Obj_Ref
6590 -- can safely reside.
6592 ----------------------
6593 -- Appears_In_Check --
6594 ----------------------
6596 function Appears_In_Check (Nod : Node_Id) return Boolean is
6597 Par : Node_Id;
6599 begin
6600 -- Climb the parent chain looking for a check node
6602 Par := Nod;
6603 while Present (Par) loop
6604 if Nkind (Par) in N_Raise_xxx_Error then
6605 return True;
6607 -- Prevent the search from going too far
6609 elsif Is_Body_Or_Package_Declaration (Par) then
6610 exit;
6611 end if;
6613 Par := Parent (Par);
6614 end loop;
6616 return False;
6617 end Appears_In_Check;
6619 ----------------------------
6620 -- Is_OK_Volatile_Context --
6621 ----------------------------
6623 function Is_OK_Volatile_Context
6624 (Context : Node_Id;
6625 Obj_Ref : Node_Id) return Boolean
6627 begin
6628 -- The volatile object appears on either side of an assignment
6630 if Nkind (Context) = N_Assignment_Statement then
6631 return True;
6633 -- The volatile object is part of the initialization expression of
6634 -- another object. Ensure that the climb of the parent chain came
6635 -- from the expression side and not from the name side.
6637 elsif Nkind (Context) = N_Object_Declaration
6638 and then Present (Expression (Context))
6639 and then Expression (Context) = Obj_Ref
6640 then
6641 return True;
6643 -- The volatile object appears as an actual parameter in a call to an
6644 -- instance of Unchecked_Conversion whose result is renamed.
6646 elsif Nkind (Context) = N_Function_Call
6647 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
6648 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
6649 then
6650 return True;
6652 -- The volatile object appears as the prefix of a name occurring
6653 -- in a non-interfering context.
6655 elsif Nkind_In (Context, N_Attribute_Reference,
6656 N_Indexed_Component,
6657 N_Selected_Component,
6658 N_Slice)
6659 and then Prefix (Context) = Obj_Ref
6660 and then Is_OK_Volatile_Context
6661 (Context => Parent (Context),
6662 Obj_Ref => Context)
6663 then
6664 return True;
6666 -- Allow references to volatile objects in various checks. This is
6667 -- not a direct SPARK 2014 requirement.
6669 elsif Appears_In_Check (Context) then
6670 return True;
6672 else
6673 return False;
6674 end if;
6675 end Is_OK_Volatile_Context;
6677 -- Local variables
6679 E : constant Entity_Id := Entity (N);
6680 Par : constant Node_Id := Parent (N);
6682 -- Start of processing for Resolve_Entity_Name
6684 begin
6685 -- If garbage from errors, set to Any_Type and return
6687 if No (E) and then Total_Errors_Detected /= 0 then
6688 Set_Etype (N, Any_Type);
6689 return;
6690 end if;
6692 -- Replace named numbers by corresponding literals. Note that this is
6693 -- the one case where Resolve_Entity_Name must reset the Etype, since
6694 -- it is currently marked as universal.
6696 if Ekind (E) = E_Named_Integer then
6697 Set_Etype (N, Typ);
6698 Eval_Named_Integer (N);
6700 elsif Ekind (E) = E_Named_Real then
6701 Set_Etype (N, Typ);
6702 Eval_Named_Real (N);
6704 -- For enumeration literals, we need to make sure that a proper style
6705 -- check is done, since such literals are overloaded, and thus we did
6706 -- not do a style check during the first phase of analysis.
6708 elsif Ekind (E) = E_Enumeration_Literal then
6709 Set_Entity_With_Checks (N, E);
6710 Eval_Entity_Name (N);
6712 -- Case of subtype name appearing as an operand in expression
6714 elsif Is_Type (E) then
6716 -- Allow use of subtype if it is a concurrent type where we are
6717 -- currently inside the body. This will eventually be expanded into a
6718 -- call to Self (for tasks) or _object (for protected objects). Any
6719 -- other use of a subtype is invalid.
6721 if Is_Concurrent_Type (E)
6722 and then In_Open_Scopes (E)
6723 then
6724 null;
6726 -- Any other use is an error
6728 else
6729 Error_Msg_N
6730 ("invalid use of subtype mark in expression or call", N);
6731 end if;
6733 -- Check discriminant use if entity is discriminant in current scope,
6734 -- i.e. discriminant of record or concurrent type currently being
6735 -- analyzed. Uses in corresponding body are unrestricted.
6737 elsif Ekind (E) = E_Discriminant
6738 and then Scope (E) = Current_Scope
6739 and then not Has_Completion (Current_Scope)
6740 then
6741 Check_Discriminant_Use (N);
6743 -- A parameterless generic function cannot appear in a context that
6744 -- requires resolution.
6746 elsif Ekind (E) = E_Generic_Function then
6747 Error_Msg_N ("illegal use of generic function", N);
6749 elsif Ekind (E) = E_Out_Parameter
6750 and then Ada_Version = Ada_83
6751 and then (Nkind (Parent (N)) in N_Op
6752 or else (Nkind (Parent (N)) = N_Assignment_Statement
6753 and then N = Expression (Parent (N)))
6754 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6755 then
6756 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6758 -- In all other cases, just do the possible static evaluation
6760 else
6761 -- A deferred constant that appears in an expression must have a
6762 -- completion, unless it has been removed by in-place expansion of
6763 -- an aggregate.
6765 if Ekind (E) = E_Constant
6766 and then Comes_From_Source (E)
6767 and then No (Constant_Value (E))
6768 and then Is_Frozen (Etype (E))
6769 and then not In_Spec_Expression
6770 and then not Is_Imported (E)
6771 then
6772 if No_Initialization (Parent (E))
6773 or else (Present (Full_View (E))
6774 and then No_Initialization (Parent (Full_View (E))))
6775 then
6776 null;
6777 else
6778 Error_Msg_N (
6779 "deferred constant is frozen before completion", N);
6780 end if;
6781 end if;
6783 Eval_Entity_Name (N);
6784 end if;
6786 -- An effectively volatile object subject to enabled properties
6787 -- Async_Writers or Effective_Reads must appear in a specific context.
6788 -- The following checks are only relevant when SPARK_Mode is on as they
6789 -- are not standard Ada legality rules.
6791 if SPARK_Mode = On
6792 and then Is_Object (E)
6793 and then Is_Effectively_Volatile (E)
6794 and then Comes_From_Source (E)
6795 and then
6796 (Async_Writers_Enabled (E) or else Effective_Reads_Enabled (E))
6797 then
6798 -- The effectively volatile objects appears in a "non-interfering
6799 -- context" as defined in SPARK RM 7.1.3(13).
6801 if Is_OK_Volatile_Context (Par, N) then
6802 null;
6804 -- Assume that references to effectively volatile objects that appear
6805 -- as actual parameters in a procedure call are always legal. The
6806 -- full legality check is done when the actuals are resolved.
6808 elsif Nkind (Par) = N_Procedure_Call_Statement then
6809 null;
6811 -- Otherwise the context causes a side effect with respect to the
6812 -- effectively volatile object.
6814 else
6815 Error_Msg_N
6816 ("volatile object cannot appear in this context "
6817 & "(SPARK RM 7.1.3(13))", N);
6818 end if;
6819 end if;
6820 end Resolve_Entity_Name;
6822 -------------------
6823 -- Resolve_Entry --
6824 -------------------
6826 procedure Resolve_Entry (Entry_Name : Node_Id) is
6827 Loc : constant Source_Ptr := Sloc (Entry_Name);
6828 Nam : Entity_Id;
6829 New_N : Node_Id;
6830 S : Entity_Id;
6831 Tsk : Entity_Id;
6832 E_Name : Node_Id;
6833 Index : Node_Id;
6835 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6836 -- If the bounds of the entry family being called depend on task
6837 -- discriminants, build a new index subtype where a discriminant is
6838 -- replaced with the value of the discriminant of the target task.
6839 -- The target task is the prefix of the entry name in the call.
6841 -----------------------
6842 -- Actual_Index_Type --
6843 -----------------------
6845 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6846 Typ : constant Entity_Id := Entry_Index_Type (E);
6847 Tsk : constant Entity_Id := Scope (E);
6848 Lo : constant Node_Id := Type_Low_Bound (Typ);
6849 Hi : constant Node_Id := Type_High_Bound (Typ);
6850 New_T : Entity_Id;
6852 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6853 -- If the bound is given by a discriminant, replace with a reference
6854 -- to the discriminant of the same name in the target task. If the
6855 -- entry name is the target of a requeue statement and the entry is
6856 -- in the current protected object, the bound to be used is the
6857 -- discriminal of the object (see Apply_Range_Checks for details of
6858 -- the transformation).
6860 -----------------------------
6861 -- Actual_Discriminant_Ref --
6862 -----------------------------
6864 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6865 Typ : constant Entity_Id := Etype (Bound);
6866 Ref : Node_Id;
6868 begin
6869 Remove_Side_Effects (Bound);
6871 if not Is_Entity_Name (Bound)
6872 or else Ekind (Entity (Bound)) /= E_Discriminant
6873 then
6874 return Bound;
6876 elsif Is_Protected_Type (Tsk)
6877 and then In_Open_Scopes (Tsk)
6878 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6879 then
6880 -- Note: here Bound denotes a discriminant of the corresponding
6881 -- record type tskV, whose discriminal is a formal of the
6882 -- init-proc tskVIP. What we want is the body discriminal,
6883 -- which is associated to the discriminant of the original
6884 -- concurrent type tsk.
6886 return New_Occurrence_Of
6887 (Find_Body_Discriminal (Entity (Bound)), Loc);
6889 else
6890 Ref :=
6891 Make_Selected_Component (Loc,
6892 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6893 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6894 Analyze (Ref);
6895 Resolve (Ref, Typ);
6896 return Ref;
6897 end if;
6898 end Actual_Discriminant_Ref;
6900 -- Start of processing for Actual_Index_Type
6902 begin
6903 if not Has_Discriminants (Tsk)
6904 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6905 then
6906 return Entry_Index_Type (E);
6908 else
6909 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6910 Set_Etype (New_T, Base_Type (Typ));
6911 Set_Size_Info (New_T, Typ);
6912 Set_RM_Size (New_T, RM_Size (Typ));
6913 Set_Scalar_Range (New_T,
6914 Make_Range (Sloc (Entry_Name),
6915 Low_Bound => Actual_Discriminant_Ref (Lo),
6916 High_Bound => Actual_Discriminant_Ref (Hi)));
6918 return New_T;
6919 end if;
6920 end Actual_Index_Type;
6922 -- Start of processing of Resolve_Entry
6924 begin
6925 -- Find name of entry being called, and resolve prefix of name with its
6926 -- own type. The prefix can be overloaded, and the name and signature of
6927 -- the entry must be taken into account.
6929 if Nkind (Entry_Name) = N_Indexed_Component then
6931 -- Case of dealing with entry family within the current tasks
6933 E_Name := Prefix (Entry_Name);
6935 else
6936 E_Name := Entry_Name;
6937 end if;
6939 if Is_Entity_Name (E_Name) then
6941 -- Entry call to an entry (or entry family) in the current task. This
6942 -- is legal even though the task will deadlock. Rewrite as call to
6943 -- current task.
6945 -- This can also be a call to an entry in an enclosing task. If this
6946 -- is a single task, we have to retrieve its name, because the scope
6947 -- of the entry is the task type, not the object. If the enclosing
6948 -- task is a task type, the identity of the task is given by its own
6949 -- self variable.
6951 -- Finally this can be a requeue on an entry of the same task or
6952 -- protected object.
6954 S := Scope (Entity (E_Name));
6956 for J in reverse 0 .. Scope_Stack.Last loop
6957 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6958 and then not Comes_From_Source (S)
6959 then
6960 -- S is an enclosing task or protected object. The concurrent
6961 -- declaration has been converted into a type declaration, and
6962 -- the object itself has an object declaration that follows
6963 -- the type in the same declarative part.
6965 Tsk := Next_Entity (S);
6966 while Etype (Tsk) /= S loop
6967 Next_Entity (Tsk);
6968 end loop;
6970 S := Tsk;
6971 exit;
6973 elsif S = Scope_Stack.Table (J).Entity then
6975 -- Call to current task. Will be transformed into call to Self
6977 exit;
6979 end if;
6980 end loop;
6982 New_N :=
6983 Make_Selected_Component (Loc,
6984 Prefix => New_Occurrence_Of (S, Loc),
6985 Selector_Name =>
6986 New_Occurrence_Of (Entity (E_Name), Loc));
6987 Rewrite (E_Name, New_N);
6988 Analyze (E_Name);
6990 elsif Nkind (Entry_Name) = N_Selected_Component
6991 and then Is_Overloaded (Prefix (Entry_Name))
6992 then
6993 -- Use the entry name (which must be unique at this point) to find
6994 -- the prefix that returns the corresponding task/protected type.
6996 declare
6997 Pref : constant Node_Id := Prefix (Entry_Name);
6998 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6999 I : Interp_Index;
7000 It : Interp;
7002 begin
7003 Get_First_Interp (Pref, I, It);
7004 while Present (It.Typ) loop
7005 if Scope (Ent) = It.Typ then
7006 Set_Etype (Pref, It.Typ);
7007 exit;
7008 end if;
7010 Get_Next_Interp (I, It);
7011 end loop;
7012 end;
7013 end if;
7015 if Nkind (Entry_Name) = N_Selected_Component then
7016 Resolve (Prefix (Entry_Name));
7018 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7019 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
7020 Resolve (Prefix (Prefix (Entry_Name)));
7021 Index := First (Expressions (Entry_Name));
7022 Resolve (Index, Entry_Index_Type (Nam));
7024 -- Up to this point the expression could have been the actual in a
7025 -- simple entry call, and be given by a named association.
7027 if Nkind (Index) = N_Parameter_Association then
7028 Error_Msg_N ("expect expression for entry index", Index);
7029 else
7030 Apply_Range_Check (Index, Actual_Index_Type (Nam));
7031 end if;
7032 end if;
7033 end Resolve_Entry;
7035 ------------------------
7036 -- Resolve_Entry_Call --
7037 ------------------------
7039 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
7040 Entry_Name : constant Node_Id := Name (N);
7041 Loc : constant Source_Ptr := Sloc (Entry_Name);
7042 Actuals : List_Id;
7043 First_Named : Node_Id;
7044 Nam : Entity_Id;
7045 Norm_OK : Boolean;
7046 Obj : Node_Id;
7047 Was_Over : Boolean;
7049 begin
7050 -- We kill all checks here, because it does not seem worth the effort to
7051 -- do anything better, an entry call is a big operation.
7053 Kill_All_Checks;
7055 -- Processing of the name is similar for entry calls and protected
7056 -- operation calls. Once the entity is determined, we can complete
7057 -- the resolution of the actuals.
7059 -- The selector may be overloaded, in the case of a protected object
7060 -- with overloaded functions. The type of the context is used for
7061 -- resolution.
7063 if Nkind (Entry_Name) = N_Selected_Component
7064 and then Is_Overloaded (Selector_Name (Entry_Name))
7065 and then Typ /= Standard_Void_Type
7066 then
7067 declare
7068 I : Interp_Index;
7069 It : Interp;
7071 begin
7072 Get_First_Interp (Selector_Name (Entry_Name), I, It);
7073 while Present (It.Typ) loop
7074 if Covers (Typ, It.Typ) then
7075 Set_Entity (Selector_Name (Entry_Name), It.Nam);
7076 Set_Etype (Entry_Name, It.Typ);
7078 Generate_Reference (It.Typ, N, ' ');
7079 end if;
7081 Get_Next_Interp (I, It);
7082 end loop;
7083 end;
7084 end if;
7086 Resolve_Entry (Entry_Name);
7088 if Nkind (Entry_Name) = N_Selected_Component then
7090 -- Simple entry call
7092 Nam := Entity (Selector_Name (Entry_Name));
7093 Obj := Prefix (Entry_Name);
7094 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
7096 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7098 -- Call to member of entry family
7100 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
7101 Obj := Prefix (Prefix (Entry_Name));
7102 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
7103 end if;
7105 -- We cannot in general check the maximum depth of protected entry calls
7106 -- at compile time. But we can tell that any protected entry call at all
7107 -- violates a specified nesting depth of zero.
7109 if Is_Protected_Type (Scope (Nam)) then
7110 Check_Restriction (Max_Entry_Queue_Length, N);
7111 end if;
7113 -- Use context type to disambiguate a protected function that can be
7114 -- called without actuals and that returns an array type, and where the
7115 -- argument list may be an indexing of the returned value.
7117 if Ekind (Nam) = E_Function
7118 and then Needs_No_Actuals (Nam)
7119 and then Present (Parameter_Associations (N))
7120 and then
7121 ((Is_Array_Type (Etype (Nam))
7122 and then Covers (Typ, Component_Type (Etype (Nam))))
7124 or else (Is_Access_Type (Etype (Nam))
7125 and then Is_Array_Type (Designated_Type (Etype (Nam)))
7126 and then
7127 Covers
7128 (Typ,
7129 Component_Type (Designated_Type (Etype (Nam))))))
7130 then
7131 declare
7132 Index_Node : Node_Id;
7134 begin
7135 Index_Node :=
7136 Make_Indexed_Component (Loc,
7137 Prefix =>
7138 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
7139 Expressions => Parameter_Associations (N));
7141 -- Since we are correcting a node classification error made by the
7142 -- parser, we call Replace rather than Rewrite.
7144 Replace (N, Index_Node);
7145 Set_Etype (Prefix (N), Etype (Nam));
7146 Set_Etype (N, Typ);
7147 Resolve_Indexed_Component (N, Typ);
7148 return;
7149 end;
7150 end if;
7152 if Ekind_In (Nam, E_Entry, E_Entry_Family)
7153 and then Present (PPC_Wrapper (Nam))
7154 and then Current_Scope /= PPC_Wrapper (Nam)
7155 then
7156 -- Rewrite as call to the precondition wrapper, adding the task
7157 -- object to the list of actuals. If the call is to a member of an
7158 -- entry family, include the index as well.
7160 declare
7161 New_Call : Node_Id;
7162 New_Actuals : List_Id;
7164 begin
7165 New_Actuals := New_List (Obj);
7167 if Nkind (Entry_Name) = N_Indexed_Component then
7168 Append_To (New_Actuals,
7169 New_Copy_Tree (First (Expressions (Entry_Name))));
7170 end if;
7172 Append_List (Parameter_Associations (N), New_Actuals);
7173 New_Call :=
7174 Make_Procedure_Call_Statement (Loc,
7175 Name =>
7176 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
7177 Parameter_Associations => New_Actuals);
7178 Rewrite (N, New_Call);
7180 -- Preanalyze and resolve new call. Current procedure is called
7181 -- from Resolve_Call, after which expansion will take place.
7183 Preanalyze_And_Resolve (N);
7184 return;
7185 end;
7186 end if;
7188 -- The operation name may have been overloaded. Order the actuals
7189 -- according to the formals of the resolved entity, and set the return
7190 -- type to that of the operation.
7192 if Was_Over then
7193 Normalize_Actuals (N, Nam, False, Norm_OK);
7194 pragma Assert (Norm_OK);
7195 Set_Etype (N, Etype (Nam));
7196 end if;
7198 Resolve_Actuals (N, Nam);
7199 Check_Internal_Protected_Use (N, Nam);
7201 -- Create a call reference to the entry
7203 Generate_Reference (Nam, Entry_Name, 's');
7205 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
7206 Check_Potentially_Blocking_Operation (N);
7207 end if;
7209 -- Verify that a procedure call cannot masquerade as an entry
7210 -- call where an entry call is expected.
7212 if Ekind (Nam) = E_Procedure then
7213 if Nkind (Parent (N)) = N_Entry_Call_Alternative
7214 and then N = Entry_Call_Statement (Parent (N))
7215 then
7216 Error_Msg_N ("entry call required in select statement", N);
7218 elsif Nkind (Parent (N)) = N_Triggering_Alternative
7219 and then N = Triggering_Statement (Parent (N))
7220 then
7221 Error_Msg_N ("triggering statement cannot be procedure call", N);
7223 elsif Ekind (Scope (Nam)) = E_Task_Type
7224 and then not In_Open_Scopes (Scope (Nam))
7225 then
7226 Error_Msg_N ("task has no entry with this name", Entry_Name);
7227 end if;
7228 end if;
7230 -- After resolution, entry calls and protected procedure calls are
7231 -- changed into entry calls, for expansion. The structure of the node
7232 -- does not change, so it can safely be done in place. Protected
7233 -- function calls must keep their structure because they are
7234 -- subexpressions.
7236 if Ekind (Nam) /= E_Function then
7238 -- A protected operation that is not a function may modify the
7239 -- corresponding object, and cannot apply to a constant. If this
7240 -- is an internal call, the prefix is the type itself.
7242 if Is_Protected_Type (Scope (Nam))
7243 and then not Is_Variable (Obj)
7244 and then (not Is_Entity_Name (Obj)
7245 or else not Is_Type (Entity (Obj)))
7246 then
7247 Error_Msg_N
7248 ("prefix of protected procedure or entry call must be variable",
7249 Entry_Name);
7250 end if;
7252 Actuals := Parameter_Associations (N);
7253 First_Named := First_Named_Actual (N);
7255 Rewrite (N,
7256 Make_Entry_Call_Statement (Loc,
7257 Name => Entry_Name,
7258 Parameter_Associations => Actuals));
7260 Set_First_Named_Actual (N, First_Named);
7261 Set_Analyzed (N, True);
7263 -- Protected functions can return on the secondary stack, in which
7264 -- case we must trigger the transient scope mechanism.
7266 elsif Expander_Active
7267 and then Requires_Transient_Scope (Etype (Nam))
7268 then
7269 Establish_Transient_Scope (N, Sec_Stack => True);
7270 end if;
7271 end Resolve_Entry_Call;
7273 -------------------------
7274 -- Resolve_Equality_Op --
7275 -------------------------
7277 -- Both arguments must have the same type, and the boolean context does
7278 -- not participate in the resolution. The first pass verifies that the
7279 -- interpretation is not ambiguous, and the type of the left argument is
7280 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
7281 -- are strings or aggregates, allocators, or Null, they are ambiguous even
7282 -- though they carry a single (universal) type. Diagnose this case here.
7284 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
7285 L : constant Node_Id := Left_Opnd (N);
7286 R : constant Node_Id := Right_Opnd (N);
7287 T : Entity_Id := Find_Unique_Type (L, R);
7289 procedure Check_If_Expression (Cond : Node_Id);
7290 -- The resolution rule for if expressions requires that each such must
7291 -- have a unique type. This means that if several dependent expressions
7292 -- are of a non-null anonymous access type, and the context does not
7293 -- impose an expected type (as can be the case in an equality operation)
7294 -- the expression must be rejected.
7296 procedure Explain_Redundancy (N : Node_Id);
7297 -- Attempt to explain the nature of a redundant comparison with True. If
7298 -- the expression N is too complex, this routine issues a general error
7299 -- message.
7301 function Find_Unique_Access_Type return Entity_Id;
7302 -- In the case of allocators and access attributes, the context must
7303 -- provide an indication of the specific access type to be used. If
7304 -- one operand is of such a "generic" access type, check whether there
7305 -- is a specific visible access type that has the same designated type.
7306 -- This is semantically dubious, and of no interest to any real code,
7307 -- but c48008a makes it all worthwhile.
7309 -------------------------
7310 -- Check_If_Expression --
7311 -------------------------
7313 procedure Check_If_Expression (Cond : Node_Id) is
7314 Then_Expr : Node_Id;
7315 Else_Expr : Node_Id;
7317 begin
7318 if Nkind (Cond) = N_If_Expression then
7319 Then_Expr := Next (First (Expressions (Cond)));
7320 Else_Expr := Next (Then_Expr);
7322 if Nkind (Then_Expr) /= N_Null
7323 and then Nkind (Else_Expr) /= N_Null
7324 then
7325 Error_Msg_N ("cannot determine type of if expression", Cond);
7326 end if;
7327 end if;
7328 end Check_If_Expression;
7330 ------------------------
7331 -- Explain_Redundancy --
7332 ------------------------
7334 procedure Explain_Redundancy (N : Node_Id) is
7335 Error : Name_Id;
7336 Val : Node_Id;
7337 Val_Id : Entity_Id;
7339 begin
7340 Val := N;
7342 -- Strip the operand down to an entity
7344 loop
7345 if Nkind (Val) = N_Selected_Component then
7346 Val := Selector_Name (Val);
7347 else
7348 exit;
7349 end if;
7350 end loop;
7352 -- The construct denotes an entity
7354 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
7355 Val_Id := Entity (Val);
7357 -- Do not generate an error message when the comparison is done
7358 -- against the enumeration literal Standard.True.
7360 if Ekind (Val_Id) /= E_Enumeration_Literal then
7362 -- Build a customized error message
7364 Name_Len := 0;
7365 Add_Str_To_Name_Buffer ("?r?");
7367 if Ekind (Val_Id) = E_Component then
7368 Add_Str_To_Name_Buffer ("component ");
7370 elsif Ekind (Val_Id) = E_Constant then
7371 Add_Str_To_Name_Buffer ("constant ");
7373 elsif Ekind (Val_Id) = E_Discriminant then
7374 Add_Str_To_Name_Buffer ("discriminant ");
7376 elsif Is_Formal (Val_Id) then
7377 Add_Str_To_Name_Buffer ("parameter ");
7379 elsif Ekind (Val_Id) = E_Variable then
7380 Add_Str_To_Name_Buffer ("variable ");
7381 end if;
7383 Add_Str_To_Name_Buffer ("& is always True!");
7384 Error := Name_Find;
7386 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
7387 end if;
7389 -- The construct is too complex to disect, issue a general message
7391 else
7392 Error_Msg_N ("?r?expression is always True!", Val);
7393 end if;
7394 end Explain_Redundancy;
7396 -----------------------------
7397 -- Find_Unique_Access_Type --
7398 -----------------------------
7400 function Find_Unique_Access_Type return Entity_Id is
7401 Acc : Entity_Id;
7402 E : Entity_Id;
7403 S : Entity_Id;
7405 begin
7406 if Ekind_In (Etype (R), E_Allocator_Type,
7407 E_Access_Attribute_Type)
7408 then
7409 Acc := Designated_Type (Etype (R));
7411 elsif Ekind_In (Etype (L), E_Allocator_Type,
7412 E_Access_Attribute_Type)
7413 then
7414 Acc := Designated_Type (Etype (L));
7415 else
7416 return Empty;
7417 end if;
7419 S := Current_Scope;
7420 while S /= Standard_Standard loop
7421 E := First_Entity (S);
7422 while Present (E) loop
7423 if Is_Type (E)
7424 and then Is_Access_Type (E)
7425 and then Ekind (E) /= E_Allocator_Type
7426 and then Designated_Type (E) = Base_Type (Acc)
7427 then
7428 return E;
7429 end if;
7431 Next_Entity (E);
7432 end loop;
7434 S := Scope (S);
7435 end loop;
7437 return Empty;
7438 end Find_Unique_Access_Type;
7440 -- Start of processing for Resolve_Equality_Op
7442 begin
7443 Set_Etype (N, Base_Type (Typ));
7444 Generate_Reference (T, N, ' ');
7446 if T = Any_Fixed then
7447 T := Unique_Fixed_Point_Type (L);
7448 end if;
7450 if T /= Any_Type then
7451 if T = Any_String or else
7452 T = Any_Composite or else
7453 T = Any_Character
7454 then
7455 if T = Any_Character then
7456 Ambiguous_Character (L);
7457 else
7458 Error_Msg_N ("ambiguous operands for equality", N);
7459 end if;
7461 Set_Etype (N, Any_Type);
7462 return;
7464 elsif T = Any_Access
7465 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
7466 then
7467 T := Find_Unique_Access_Type;
7469 if No (T) then
7470 Error_Msg_N ("ambiguous operands for equality", N);
7471 Set_Etype (N, Any_Type);
7472 return;
7473 end if;
7475 -- If expressions must have a single type, and if the context does
7476 -- not impose one the dependent expressions cannot be anonymous
7477 -- access types.
7479 -- Why no similar processing for case expressions???
7481 elsif Ada_Version >= Ada_2012
7482 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
7483 E_Anonymous_Access_Subprogram_Type)
7484 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
7485 E_Anonymous_Access_Subprogram_Type)
7486 then
7487 Check_If_Expression (L);
7488 Check_If_Expression (R);
7489 end if;
7491 Resolve (L, T);
7492 Resolve (R, T);
7494 -- In SPARK, equality operators = and /= for array types other than
7495 -- String are only defined when, for each index position, the
7496 -- operands have equal static bounds.
7498 if Is_Array_Type (T) then
7500 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7501 -- operation if not needed.
7503 if Restriction_Check_Required (SPARK_05)
7504 and then Base_Type (T) /= Standard_String
7505 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7506 and then Etype (L) /= Any_Composite -- or else L in error
7507 and then Etype (R) /= Any_Composite -- or else R in error
7508 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
7509 then
7510 Check_SPARK_Restriction
7511 ("array types should have matching static bounds", N);
7512 end if;
7513 end if;
7515 -- If the unique type is a class-wide type then it will be expanded
7516 -- into a dispatching call to the predefined primitive. Therefore we
7517 -- check here for potential violation of such restriction.
7519 if Is_Class_Wide_Type (T) then
7520 Check_Restriction (No_Dispatching_Calls, N);
7521 end if;
7523 if Warn_On_Redundant_Constructs
7524 and then Comes_From_Source (N)
7525 and then Comes_From_Source (R)
7526 and then Is_Entity_Name (R)
7527 and then Entity (R) = Standard_True
7528 then
7529 Error_Msg_N -- CODEFIX
7530 ("?r?comparison with True is redundant!", N);
7531 Explain_Redundancy (Original_Node (R));
7532 end if;
7534 Check_Unset_Reference (L);
7535 Check_Unset_Reference (R);
7536 Generate_Operator_Reference (N, T);
7537 Check_Low_Bound_Tested (N);
7539 -- If this is an inequality, it may be the implicit inequality
7540 -- created for a user-defined operation, in which case the corres-
7541 -- ponding equality operation is not intrinsic, and the operation
7542 -- cannot be constant-folded. Else fold.
7544 if Nkind (N) = N_Op_Eq
7545 or else Comes_From_Source (Entity (N))
7546 or else Ekind (Entity (N)) = E_Operator
7547 or else Is_Intrinsic_Subprogram
7548 (Corresponding_Equality (Entity (N)))
7549 then
7550 Analyze_Dimension (N);
7551 Eval_Relational_Op (N);
7553 elsif Nkind (N) = N_Op_Ne
7554 and then Is_Abstract_Subprogram (Entity (N))
7555 then
7556 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7557 end if;
7559 -- Ada 2005: If one operand is an anonymous access type, convert the
7560 -- other operand to it, to ensure that the underlying types match in
7561 -- the back-end. Same for access_to_subprogram, and the conversion
7562 -- verifies that the types are subtype conformant.
7564 -- We apply the same conversion in the case one of the operands is a
7565 -- private subtype of the type of the other.
7567 -- Why the Expander_Active test here ???
7569 if Expander_Active
7570 and then
7571 (Ekind_In (T, E_Anonymous_Access_Type,
7572 E_Anonymous_Access_Subprogram_Type)
7573 or else Is_Private_Type (T))
7574 then
7575 if Etype (L) /= T then
7576 Rewrite (L,
7577 Make_Unchecked_Type_Conversion (Sloc (L),
7578 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7579 Expression => Relocate_Node (L)));
7580 Analyze_And_Resolve (L, T);
7581 end if;
7583 if (Etype (R)) /= T then
7584 Rewrite (R,
7585 Make_Unchecked_Type_Conversion (Sloc (R),
7586 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7587 Expression => Relocate_Node (R)));
7588 Analyze_And_Resolve (R, T);
7589 end if;
7590 end if;
7591 end if;
7592 end Resolve_Equality_Op;
7594 ----------------------------------
7595 -- Resolve_Explicit_Dereference --
7596 ----------------------------------
7598 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
7599 Loc : constant Source_Ptr := Sloc (N);
7600 New_N : Node_Id;
7601 P : constant Node_Id := Prefix (N);
7603 P_Typ : Entity_Id;
7604 -- The candidate prefix type, if overloaded
7606 I : Interp_Index;
7607 It : Interp;
7609 begin
7610 Check_Fully_Declared_Prefix (Typ, P);
7611 P_Typ := Empty;
7613 -- A useful optimization: check whether the dereference denotes an
7614 -- element of a container, and if so rewrite it as a call to the
7615 -- corresponding Element function.
7617 -- Disabled for now, on advice of ARG. A more restricted form of the
7618 -- predicate might be acceptable ???
7620 -- if Is_Container_Element (N) then
7621 -- return;
7622 -- end if;
7624 if Is_Overloaded (P) then
7626 -- Use the context type to select the prefix that has the correct
7627 -- designated type. Keep the first match, which will be the inner-
7628 -- most.
7630 Get_First_Interp (P, I, It);
7632 while Present (It.Typ) loop
7633 if Is_Access_Type (It.Typ)
7634 and then Covers (Typ, Designated_Type (It.Typ))
7635 then
7636 if No (P_Typ) then
7637 P_Typ := It.Typ;
7638 end if;
7640 -- Remove access types that do not match, but preserve access
7641 -- to subprogram interpretations, in case a further dereference
7642 -- is needed (see below).
7644 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
7645 Remove_Interp (I);
7646 end if;
7648 Get_Next_Interp (I, It);
7649 end loop;
7651 if Present (P_Typ) then
7652 Resolve (P, P_Typ);
7653 Set_Etype (N, Designated_Type (P_Typ));
7655 else
7656 -- If no interpretation covers the designated type of the prefix,
7657 -- this is the pathological case where not all implementations of
7658 -- the prefix allow the interpretation of the node as a call. Now
7659 -- that the expected type is known, Remove other interpretations
7660 -- from prefix, rewrite it as a call, and resolve again, so that
7661 -- the proper call node is generated.
7663 Get_First_Interp (P, I, It);
7664 while Present (It.Typ) loop
7665 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
7666 Remove_Interp (I);
7667 end if;
7669 Get_Next_Interp (I, It);
7670 end loop;
7672 New_N :=
7673 Make_Function_Call (Loc,
7674 Name =>
7675 Make_Explicit_Dereference (Loc,
7676 Prefix => P),
7677 Parameter_Associations => New_List);
7679 Save_Interps (N, New_N);
7680 Rewrite (N, New_N);
7681 Analyze_And_Resolve (N, Typ);
7682 return;
7683 end if;
7685 -- If not overloaded, resolve P with its own type
7687 else
7688 Resolve (P);
7689 end if;
7691 if Is_Access_Type (Etype (P)) then
7692 Apply_Access_Check (N);
7693 end if;
7695 -- If the designated type is a packed unconstrained array type, and the
7696 -- explicit dereference is not in the context of an attribute reference,
7697 -- then we must compute and set the actual subtype, since it is needed
7698 -- by Gigi. The reason we exclude the attribute case is that this is
7699 -- handled fine by Gigi, and in fact we use such attributes to build the
7700 -- actual subtype. We also exclude generated code (which builds actual
7701 -- subtypes directly if they are needed).
7703 if Is_Array_Type (Etype (N))
7704 and then Is_Packed (Etype (N))
7705 and then not Is_Constrained (Etype (N))
7706 and then Nkind (Parent (N)) /= N_Attribute_Reference
7707 and then Comes_From_Source (N)
7708 then
7709 Set_Etype (N, Get_Actual_Subtype (N));
7710 end if;
7712 -- Note: No Eval processing is required for an explicit dereference,
7713 -- because such a name can never be static.
7715 end Resolve_Explicit_Dereference;
7717 -------------------------------------
7718 -- Resolve_Expression_With_Actions --
7719 -------------------------------------
7721 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7722 begin
7723 Set_Etype (N, Typ);
7725 -- If N has no actions, and its expression has been constant folded,
7726 -- then rewrite N as just its expression. Note, we can't do this in
7727 -- the general case of Is_Empty_List (Actions (N)) as this would cause
7728 -- Expression (N) to be expanded again.
7730 if Is_Empty_List (Actions (N))
7731 and then Compile_Time_Known_Value (Expression (N))
7732 then
7733 Rewrite (N, Expression (N));
7734 end if;
7735 end Resolve_Expression_With_Actions;
7737 ----------------------------------
7738 -- Resolve_Generalized_Indexing --
7739 ----------------------------------
7741 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
7742 Indexing : constant Node_Id := Generalized_Indexing (N);
7743 Call : Node_Id;
7744 Indices : List_Id;
7745 Pref : Node_Id;
7747 begin
7748 -- In ASIS mode, propagate the information about the indices back to
7749 -- to the original indexing node. The generalized indexing is either
7750 -- a function call, or a dereference of one. The actuals include the
7751 -- prefix of the original node, which is the container expression.
7753 if ASIS_Mode then
7754 Resolve (Indexing, Typ);
7755 Set_Etype (N, Etype (Indexing));
7756 Set_Is_Overloaded (N, False);
7758 Call := Indexing;
7759 while Nkind_In (Call, N_Explicit_Dereference, N_Selected_Component)
7760 loop
7761 Call := Prefix (Call);
7762 end loop;
7764 if Nkind (Call) = N_Function_Call then
7765 Indices := Parameter_Associations (Call);
7766 Pref := Remove_Head (Indices);
7767 Set_Expressions (N, Indices);
7768 Set_Prefix (N, Pref);
7769 end if;
7771 else
7772 Rewrite (N, Indexing);
7773 Resolve (N, Typ);
7774 end if;
7775 end Resolve_Generalized_Indexing;
7777 ---------------------------
7778 -- Resolve_If_Expression --
7779 ---------------------------
7781 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
7782 Condition : constant Node_Id := First (Expressions (N));
7783 Then_Expr : constant Node_Id := Next (Condition);
7784 Else_Expr : Node_Id := Next (Then_Expr);
7785 Else_Typ : Entity_Id;
7786 Then_Typ : Entity_Id;
7788 begin
7789 Resolve (Condition, Any_Boolean);
7790 Resolve (Then_Expr, Typ);
7791 Then_Typ := Etype (Then_Expr);
7793 -- When the "then" expression is of a scalar subtype different from the
7794 -- result subtype, then insert a conversion to ensure the generation of
7795 -- a constraint check. The same is done for the else part below, again
7796 -- comparing subtypes rather than base types.
7798 if Is_Scalar_Type (Then_Typ)
7799 and then Then_Typ /= Typ
7800 then
7801 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
7802 Analyze_And_Resolve (Then_Expr, Typ);
7803 end if;
7805 -- If ELSE expression present, just resolve using the determined type
7807 if Present (Else_Expr) then
7808 Resolve (Else_Expr, Typ);
7809 Else_Typ := Etype (Else_Expr);
7811 if Is_Scalar_Type (Else_Typ)
7812 and then Else_Typ /= Typ
7813 then
7814 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
7815 Analyze_And_Resolve (Else_Expr, Typ);
7816 end if;
7818 -- If no ELSE expression is present, root type must be Standard.Boolean
7819 -- and we provide a Standard.True result converted to the appropriate
7820 -- Boolean type (in case it is a derived boolean type).
7822 elsif Root_Type (Typ) = Standard_Boolean then
7823 Else_Expr :=
7824 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
7825 Analyze_And_Resolve (Else_Expr, Typ);
7826 Append_To (Expressions (N), Else_Expr);
7828 else
7829 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
7830 Append_To (Expressions (N), Error);
7831 end if;
7833 Set_Etype (N, Typ);
7834 Eval_If_Expression (N);
7835 end Resolve_If_Expression;
7837 -------------------------------
7838 -- Resolve_Indexed_Component --
7839 -------------------------------
7841 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7842 Name : constant Node_Id := Prefix (N);
7843 Expr : Node_Id;
7844 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7845 Index : Node_Id;
7847 begin
7848 if Present (Generalized_Indexing (N)) then
7849 Resolve_Generalized_Indexing (N, Typ);
7850 return;
7851 end if;
7853 if Is_Overloaded (Name) then
7855 -- Use the context type to select the prefix that yields the correct
7856 -- component type.
7858 declare
7859 I : Interp_Index;
7860 It : Interp;
7861 I1 : Interp_Index := 0;
7862 P : constant Node_Id := Prefix (N);
7863 Found : Boolean := False;
7865 begin
7866 Get_First_Interp (P, I, It);
7867 while Present (It.Typ) loop
7868 if (Is_Array_Type (It.Typ)
7869 and then Covers (Typ, Component_Type (It.Typ)))
7870 or else (Is_Access_Type (It.Typ)
7871 and then Is_Array_Type (Designated_Type (It.Typ))
7872 and then
7873 Covers
7874 (Typ,
7875 Component_Type (Designated_Type (It.Typ))))
7876 then
7877 if Found then
7878 It := Disambiguate (P, I1, I, Any_Type);
7880 if It = No_Interp then
7881 Error_Msg_N ("ambiguous prefix for indexing", N);
7882 Set_Etype (N, Typ);
7883 return;
7885 else
7886 Found := True;
7887 Array_Type := It.Typ;
7888 I1 := I;
7889 end if;
7891 else
7892 Found := True;
7893 Array_Type := It.Typ;
7894 I1 := I;
7895 end if;
7896 end if;
7898 Get_Next_Interp (I, It);
7899 end loop;
7900 end;
7902 else
7903 Array_Type := Etype (Name);
7904 end if;
7906 Resolve (Name, Array_Type);
7907 Array_Type := Get_Actual_Subtype_If_Available (Name);
7909 -- If prefix is access type, dereference to get real array type.
7910 -- Note: we do not apply an access check because the expander always
7911 -- introduces an explicit dereference, and the check will happen there.
7913 if Is_Access_Type (Array_Type) then
7914 Array_Type := Designated_Type (Array_Type);
7915 end if;
7917 -- If name was overloaded, set component type correctly now
7918 -- If a misplaced call to an entry family (which has no index types)
7919 -- return. Error will be diagnosed from calling context.
7921 if Is_Array_Type (Array_Type) then
7922 Set_Etype (N, Component_Type (Array_Type));
7923 else
7924 return;
7925 end if;
7927 Index := First_Index (Array_Type);
7928 Expr := First (Expressions (N));
7930 -- The prefix may have resolved to a string literal, in which case its
7931 -- etype has a special representation. This is only possible currently
7932 -- if the prefix is a static concatenation, written in functional
7933 -- notation.
7935 if Ekind (Array_Type) = E_String_Literal_Subtype then
7936 Resolve (Expr, Standard_Positive);
7938 else
7939 while Present (Index) and Present (Expr) loop
7940 Resolve (Expr, Etype (Index));
7941 Check_Unset_Reference (Expr);
7943 if Is_Scalar_Type (Etype (Expr)) then
7944 Apply_Scalar_Range_Check (Expr, Etype (Index));
7945 else
7946 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7947 end if;
7949 Next_Index (Index);
7950 Next (Expr);
7951 end loop;
7952 end if;
7954 Analyze_Dimension (N);
7956 -- Do not generate the warning on suspicious index if we are analyzing
7957 -- package Ada.Tags; otherwise we will report the warning with the
7958 -- Prims_Ptr field of the dispatch table.
7960 if Scope (Etype (Prefix (N))) = Standard_Standard
7961 or else not
7962 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7963 Ada_Tags)
7964 then
7965 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7966 Eval_Indexed_Component (N);
7967 end if;
7969 -- If the array type is atomic, and is packed, and we are in a left side
7970 -- context, then this is worth a warning, since we have a situation
7971 -- where the access to the component may cause extra read/writes of
7972 -- the atomic array object, which could be considered unexpected.
7974 if Nkind (N) = N_Indexed_Component
7975 and then (Is_Atomic (Array_Type)
7976 or else (Is_Entity_Name (Prefix (N))
7977 and then Is_Atomic (Entity (Prefix (N)))))
7978 and then Is_Bit_Packed_Array (Array_Type)
7979 and then Is_LHS (N) = Yes
7980 then
7981 Error_Msg_N ("??assignment to component of packed atomic array",
7982 Prefix (N));
7983 Error_Msg_N ("??\may cause unexpected accesses to atomic object",
7984 Prefix (N));
7985 end if;
7986 end Resolve_Indexed_Component;
7988 -----------------------------
7989 -- Resolve_Integer_Literal --
7990 -----------------------------
7992 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7993 begin
7994 Set_Etype (N, Typ);
7995 Eval_Integer_Literal (N);
7996 end Resolve_Integer_Literal;
7998 --------------------------------
7999 -- Resolve_Intrinsic_Operator --
8000 --------------------------------
8002 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
8003 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8004 Op : Entity_Id;
8005 Arg1 : Node_Id;
8006 Arg2 : Node_Id;
8008 function Convert_Operand (Opnd : Node_Id) return Node_Id;
8009 -- If the operand is a literal, it cannot be the expression in a
8010 -- conversion. Use a qualified expression instead.
8012 function Convert_Operand (Opnd : Node_Id) return Node_Id is
8013 Loc : constant Source_Ptr := Sloc (Opnd);
8014 Res : Node_Id;
8015 begin
8016 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
8017 Res :=
8018 Make_Qualified_Expression (Loc,
8019 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
8020 Expression => Relocate_Node (Opnd));
8021 Analyze (Res);
8023 else
8024 Res := Unchecked_Convert_To (Btyp, Opnd);
8025 end if;
8027 return Res;
8028 end Convert_Operand;
8030 -- Start of processing for Resolve_Intrinsic_Operator
8032 begin
8033 -- We must preserve the original entity in a generic setting, so that
8034 -- the legality of the operation can be verified in an instance.
8036 if not Expander_Active then
8037 return;
8038 end if;
8040 Op := Entity (N);
8041 while Scope (Op) /= Standard_Standard loop
8042 Op := Homonym (Op);
8043 pragma Assert (Present (Op));
8044 end loop;
8046 Set_Entity (N, Op);
8047 Set_Is_Overloaded (N, False);
8049 -- If the result or operand types are private, rewrite with unchecked
8050 -- conversions on the operands and the result, to expose the proper
8051 -- underlying numeric type.
8053 if Is_Private_Type (Typ)
8054 or else Is_Private_Type (Etype (Left_Opnd (N)))
8055 or else Is_Private_Type (Etype (Right_Opnd (N)))
8056 then
8057 Arg1 := Convert_Operand (Left_Opnd (N));
8058 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
8059 -- What on earth is this commented out fragment of code???
8061 if Nkind (N) = N_Op_Expon then
8062 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
8063 else
8064 Arg2 := Convert_Operand (Right_Opnd (N));
8065 end if;
8067 if Nkind (Arg1) = N_Type_Conversion then
8068 Save_Interps (Left_Opnd (N), Expression (Arg1));
8069 end if;
8071 if Nkind (Arg2) = N_Type_Conversion then
8072 Save_Interps (Right_Opnd (N), Expression (Arg2));
8073 end if;
8075 Set_Left_Opnd (N, Arg1);
8076 Set_Right_Opnd (N, Arg2);
8078 Set_Etype (N, Btyp);
8079 Rewrite (N, Unchecked_Convert_To (Typ, N));
8080 Resolve (N, Typ);
8082 elsif Typ /= Etype (Left_Opnd (N))
8083 or else Typ /= Etype (Right_Opnd (N))
8084 then
8085 -- Add explicit conversion where needed, and save interpretations in
8086 -- case operands are overloaded.
8088 Arg1 := Convert_To (Typ, Left_Opnd (N));
8089 Arg2 := Convert_To (Typ, Right_Opnd (N));
8091 if Nkind (Arg1) = N_Type_Conversion then
8092 Save_Interps (Left_Opnd (N), Expression (Arg1));
8093 else
8094 Save_Interps (Left_Opnd (N), Arg1);
8095 end if;
8097 if Nkind (Arg2) = N_Type_Conversion then
8098 Save_Interps (Right_Opnd (N), Expression (Arg2));
8099 else
8100 Save_Interps (Right_Opnd (N), Arg2);
8101 end if;
8103 Rewrite (Left_Opnd (N), Arg1);
8104 Rewrite (Right_Opnd (N), Arg2);
8105 Analyze (Arg1);
8106 Analyze (Arg2);
8107 Resolve_Arithmetic_Op (N, Typ);
8109 else
8110 Resolve_Arithmetic_Op (N, Typ);
8111 end if;
8112 end Resolve_Intrinsic_Operator;
8114 --------------------------------------
8115 -- Resolve_Intrinsic_Unary_Operator --
8116 --------------------------------------
8118 procedure Resolve_Intrinsic_Unary_Operator
8119 (N : Node_Id;
8120 Typ : Entity_Id)
8122 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8123 Op : Entity_Id;
8124 Arg2 : Node_Id;
8126 begin
8127 Op := Entity (N);
8128 while Scope (Op) /= Standard_Standard loop
8129 Op := Homonym (Op);
8130 pragma Assert (Present (Op));
8131 end loop;
8133 Set_Entity (N, Op);
8135 if Is_Private_Type (Typ) then
8136 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
8137 Save_Interps (Right_Opnd (N), Expression (Arg2));
8139 Set_Right_Opnd (N, Arg2);
8141 Set_Etype (N, Btyp);
8142 Rewrite (N, Unchecked_Convert_To (Typ, N));
8143 Resolve (N, Typ);
8145 else
8146 Resolve_Unary_Op (N, Typ);
8147 end if;
8148 end Resolve_Intrinsic_Unary_Operator;
8150 ------------------------
8151 -- Resolve_Logical_Op --
8152 ------------------------
8154 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
8155 B_Typ : Entity_Id;
8157 begin
8158 Check_No_Direct_Boolean_Operators (N);
8160 -- Predefined operations on scalar types yield the base type. On the
8161 -- other hand, logical operations on arrays yield the type of the
8162 -- arguments (and the context).
8164 if Is_Array_Type (Typ) then
8165 B_Typ := Typ;
8166 else
8167 B_Typ := Base_Type (Typ);
8168 end if;
8170 -- The following test is required because the operands of the operation
8171 -- may be literals, in which case the resulting type appears to be
8172 -- compatible with a signed integer type, when in fact it is compatible
8173 -- only with modular types. If the context itself is universal, the
8174 -- operation is illegal.
8176 if not Valid_Boolean_Arg (Typ) then
8177 Error_Msg_N ("invalid context for logical operation", N);
8178 Set_Etype (N, Any_Type);
8179 return;
8181 elsif Typ = Any_Modular then
8182 Error_Msg_N
8183 ("no modular type available in this context", N);
8184 Set_Etype (N, Any_Type);
8185 return;
8187 elsif Is_Modular_Integer_Type (Typ)
8188 and then Etype (Left_Opnd (N)) = Universal_Integer
8189 and then Etype (Right_Opnd (N)) = Universal_Integer
8190 then
8191 Check_For_Visible_Operator (N, B_Typ);
8192 end if;
8194 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
8195 -- is active and the result type is standard Boolean (do not mess with
8196 -- ops that return a nonstandard Boolean type, because something strange
8197 -- is going on).
8199 -- Note: you might expect this replacement to be done during expansion,
8200 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
8201 -- is used, no part of the right operand of an "and" or "or" operator
8202 -- should be executed if the left operand would short-circuit the
8203 -- evaluation of the corresponding "and then" or "or else". If we left
8204 -- the replacement to expansion time, then run-time checks associated
8205 -- with such operands would be evaluated unconditionally, due to being
8206 -- before the condition prior to the rewriting as short-circuit forms
8207 -- during expansion.
8209 if Short_Circuit_And_Or
8210 and then B_Typ = Standard_Boolean
8211 and then Nkind_In (N, N_Op_And, N_Op_Or)
8212 then
8213 if Nkind (N) = N_Op_And then
8214 Rewrite (N,
8215 Make_And_Then (Sloc (N),
8216 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8217 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8218 Analyze_And_Resolve (N, B_Typ);
8220 -- Case of OR changed to OR ELSE
8222 else
8223 Rewrite (N,
8224 Make_Or_Else (Sloc (N),
8225 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8226 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8227 Analyze_And_Resolve (N, B_Typ);
8228 end if;
8230 -- Return now, since analysis of the rewritten ops will take care of
8231 -- other reference bookkeeping and expression folding.
8233 return;
8234 end if;
8236 Resolve (Left_Opnd (N), B_Typ);
8237 Resolve (Right_Opnd (N), B_Typ);
8239 Check_Unset_Reference (Left_Opnd (N));
8240 Check_Unset_Reference (Right_Opnd (N));
8242 Set_Etype (N, B_Typ);
8243 Generate_Operator_Reference (N, B_Typ);
8244 Eval_Logical_Op (N);
8246 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
8247 -- only when both operands have same static lower and higher bounds. Of
8248 -- course the types have to match, so only check if operands are
8249 -- compatible and the node itself has no errors.
8251 if Is_Array_Type (B_Typ)
8252 and then Nkind (N) in N_Binary_Op
8253 then
8254 declare
8255 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
8256 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
8258 begin
8259 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8260 -- operation if not needed.
8262 if Restriction_Check_Required (SPARK_05)
8263 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
8264 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
8265 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
8266 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
8267 then
8268 Check_SPARK_Restriction
8269 ("array types should have matching static bounds", N);
8270 end if;
8271 end;
8272 end if;
8274 Check_Function_Writable_Actuals (N);
8275 end Resolve_Logical_Op;
8277 ---------------------------
8278 -- Resolve_Membership_Op --
8279 ---------------------------
8281 -- The context can only be a boolean type, and does not determine the
8282 -- arguments. Arguments should be unambiguous, but the preference rule for
8283 -- universal types applies.
8285 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
8286 pragma Warnings (Off, Typ);
8288 L : constant Node_Id := Left_Opnd (N);
8289 R : constant Node_Id := Right_Opnd (N);
8290 T : Entity_Id;
8292 procedure Resolve_Set_Membership;
8293 -- Analysis has determined a unique type for the left operand. Use it to
8294 -- resolve the disjuncts.
8296 ----------------------------
8297 -- Resolve_Set_Membership --
8298 ----------------------------
8300 procedure Resolve_Set_Membership is
8301 Alt : Node_Id;
8302 Ltyp : constant Entity_Id := Etype (L);
8304 begin
8305 Resolve (L, Ltyp);
8307 Alt := First (Alternatives (N));
8308 while Present (Alt) loop
8310 -- Alternative is an expression, a range
8311 -- or a subtype mark.
8313 if not Is_Entity_Name (Alt)
8314 or else not Is_Type (Entity (Alt))
8315 then
8316 Resolve (Alt, Ltyp);
8317 end if;
8319 Next (Alt);
8320 end loop;
8322 -- Check for duplicates for discrete case
8324 if Is_Discrete_Type (Ltyp) then
8325 declare
8326 type Ent is record
8327 Alt : Node_Id;
8328 Val : Uint;
8329 end record;
8331 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
8332 Nalts : Nat;
8334 begin
8335 -- Loop checking duplicates. This is quadratic, but giant sets
8336 -- are unlikely in this context so it's a reasonable choice.
8338 Nalts := 0;
8339 Alt := First (Alternatives (N));
8340 while Present (Alt) loop
8341 if Is_OK_Static_Expression (Alt)
8342 and then (Nkind_In (Alt, N_Integer_Literal,
8343 N_Character_Literal)
8344 or else Nkind (Alt) in N_Has_Entity)
8345 then
8346 Nalts := Nalts + 1;
8347 Alts (Nalts) := (Alt, Expr_Value (Alt));
8349 for J in 1 .. Nalts - 1 loop
8350 if Alts (J).Val = Alts (Nalts).Val then
8351 Error_Msg_Sloc := Sloc (Alts (J).Alt);
8352 Error_Msg_N ("duplicate of value given#??", Alt);
8353 end if;
8354 end loop;
8355 end if;
8357 Alt := Next (Alt);
8358 end loop;
8359 end;
8360 end if;
8361 end Resolve_Set_Membership;
8363 -- Start of processing for Resolve_Membership_Op
8365 begin
8366 if L = Error or else R = Error then
8367 return;
8368 end if;
8370 if Present (Alternatives (N)) then
8371 Resolve_Set_Membership;
8372 goto SM_Exit;
8374 elsif not Is_Overloaded (R)
8375 and then
8376 (Etype (R) = Universal_Integer
8377 or else
8378 Etype (R) = Universal_Real)
8379 and then Is_Overloaded (L)
8380 then
8381 T := Etype (R);
8383 -- Ada 2005 (AI-251): Support the following case:
8385 -- type I is interface;
8386 -- type T is tagged ...
8388 -- function Test (O : I'Class) is
8389 -- begin
8390 -- return O in T'Class.
8391 -- end Test;
8393 -- In this case we have nothing else to do. The membership test will be
8394 -- done at run time.
8396 elsif Ada_Version >= Ada_2005
8397 and then Is_Class_Wide_Type (Etype (L))
8398 and then Is_Interface (Etype (L))
8399 and then Is_Class_Wide_Type (Etype (R))
8400 and then not Is_Interface (Etype (R))
8401 then
8402 return;
8403 else
8404 T := Intersect_Types (L, R);
8405 end if;
8407 -- If mixed-mode operations are present and operands are all literal,
8408 -- the only interpretation involves Duration, which is probably not
8409 -- the intention of the programmer.
8411 if T = Any_Fixed then
8412 T := Unique_Fixed_Point_Type (N);
8414 if T = Any_Type then
8415 return;
8416 end if;
8417 end if;
8419 Resolve (L, T);
8420 Check_Unset_Reference (L);
8422 if Nkind (R) = N_Range
8423 and then not Is_Scalar_Type (T)
8424 then
8425 Error_Msg_N ("scalar type required for range", R);
8426 end if;
8428 if Is_Entity_Name (R) then
8429 Freeze_Expression (R);
8430 else
8431 Resolve (R, T);
8432 Check_Unset_Reference (R);
8433 end if;
8435 -- Here after resolving membership operation
8437 <<SM_Exit>>
8439 Eval_Membership_Op (N);
8440 Check_Function_Writable_Actuals (N);
8441 end Resolve_Membership_Op;
8443 ------------------
8444 -- Resolve_Null --
8445 ------------------
8447 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
8448 Loc : constant Source_Ptr := Sloc (N);
8450 begin
8451 -- Handle restriction against anonymous null access values This
8452 -- restriction can be turned off using -gnatdj.
8454 -- Ada 2005 (AI-231): Remove restriction
8456 if Ada_Version < Ada_2005
8457 and then not Debug_Flag_J
8458 and then Ekind (Typ) = E_Anonymous_Access_Type
8459 and then Comes_From_Source (N)
8460 then
8461 -- In the common case of a call which uses an explicitly null value
8462 -- for an access parameter, give specialized error message.
8464 if Nkind (Parent (N)) in N_Subprogram_Call then
8465 Error_Msg_N
8466 ("null is not allowed as argument for an access parameter", N);
8468 -- Standard message for all other cases (are there any?)
8470 else
8471 Error_Msg_N
8472 ("null cannot be of an anonymous access type", N);
8473 end if;
8474 end if;
8476 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
8477 -- assignment to a null-excluding object
8479 if Ada_Version >= Ada_2005
8480 and then Can_Never_Be_Null (Typ)
8481 and then Nkind (Parent (N)) = N_Assignment_Statement
8482 then
8483 if not Inside_Init_Proc then
8484 Insert_Action
8485 (Compile_Time_Constraint_Error (N,
8486 "(Ada 2005) null not allowed in null-excluding objects??"),
8487 Make_Raise_Constraint_Error (Loc,
8488 Reason => CE_Access_Check_Failed));
8489 else
8490 Insert_Action (N,
8491 Make_Raise_Constraint_Error (Loc,
8492 Reason => CE_Access_Check_Failed));
8493 end if;
8494 end if;
8496 -- In a distributed context, null for a remote access to subprogram may
8497 -- need to be replaced with a special record aggregate. In this case,
8498 -- return after having done the transformation.
8500 if (Ekind (Typ) = E_Record_Type
8501 or else Is_Remote_Access_To_Subprogram_Type (Typ))
8502 and then Remote_AST_Null_Value (N, Typ)
8503 then
8504 return;
8505 end if;
8507 -- The null literal takes its type from the context
8509 Set_Etype (N, Typ);
8510 end Resolve_Null;
8512 -----------------------
8513 -- Resolve_Op_Concat --
8514 -----------------------
8516 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
8518 -- We wish to avoid deep recursion, because concatenations are often
8519 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
8520 -- operands nonrecursively until we find something that is not a simple
8521 -- concatenation (A in this case). We resolve that, and then walk back
8522 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
8523 -- to do the rest of the work at each level. The Parent pointers allow
8524 -- us to avoid recursion, and thus avoid running out of memory. See also
8525 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
8527 NN : Node_Id := N;
8528 Op1 : Node_Id;
8530 begin
8531 -- The following code is equivalent to:
8533 -- Resolve_Op_Concat_First (NN, Typ);
8534 -- Resolve_Op_Concat_Arg (N, ...);
8535 -- Resolve_Op_Concat_Rest (N, Typ);
8537 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
8538 -- operand is a concatenation.
8540 -- Walk down left operands
8542 loop
8543 Resolve_Op_Concat_First (NN, Typ);
8544 Op1 := Left_Opnd (NN);
8545 exit when not (Nkind (Op1) = N_Op_Concat
8546 and then not Is_Array_Type (Component_Type (Typ))
8547 and then Entity (Op1) = Entity (NN));
8548 NN := Op1;
8549 end loop;
8551 -- Now (given the above example) NN is A&B and Op1 is A
8553 -- First resolve Op1 ...
8555 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
8557 -- ... then walk NN back up until we reach N (where we started), calling
8558 -- Resolve_Op_Concat_Rest along the way.
8560 loop
8561 Resolve_Op_Concat_Rest (NN, Typ);
8562 exit when NN = N;
8563 NN := Parent (NN);
8564 end loop;
8566 if Base_Type (Etype (N)) /= Standard_String then
8567 Check_SPARK_Restriction
8568 ("result of concatenation should have type String", N);
8569 end if;
8570 end Resolve_Op_Concat;
8572 ---------------------------
8573 -- Resolve_Op_Concat_Arg --
8574 ---------------------------
8576 procedure Resolve_Op_Concat_Arg
8577 (N : Node_Id;
8578 Arg : Node_Id;
8579 Typ : Entity_Id;
8580 Is_Comp : Boolean)
8582 Btyp : constant Entity_Id := Base_Type (Typ);
8583 Ctyp : constant Entity_Id := Component_Type (Typ);
8585 begin
8586 if In_Instance then
8587 if Is_Comp
8588 or else (not Is_Overloaded (Arg)
8589 and then Etype (Arg) /= Any_Composite
8590 and then Covers (Ctyp, Etype (Arg)))
8591 then
8592 Resolve (Arg, Ctyp);
8593 else
8594 Resolve (Arg, Btyp);
8595 end if;
8597 -- If both Array & Array and Array & Component are visible, there is a
8598 -- potential ambiguity that must be reported.
8600 elsif Has_Compatible_Type (Arg, Ctyp) then
8601 if Nkind (Arg) = N_Aggregate
8602 and then Is_Composite_Type (Ctyp)
8603 then
8604 if Is_Private_Type (Ctyp) then
8605 Resolve (Arg, Btyp);
8607 -- If the operation is user-defined and not overloaded use its
8608 -- profile. The operation may be a renaming, in which case it has
8609 -- been rewritten, and we want the original profile.
8611 elsif not Is_Overloaded (N)
8612 and then Comes_From_Source (Entity (Original_Node (N)))
8613 and then Ekind (Entity (Original_Node (N))) = E_Function
8614 then
8615 Resolve (Arg,
8616 Etype
8617 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
8618 return;
8620 -- Otherwise an aggregate may match both the array type and the
8621 -- component type.
8623 else
8624 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
8625 Set_Etype (Arg, Any_Type);
8626 end if;
8628 else
8629 if Is_Overloaded (Arg)
8630 and then Has_Compatible_Type (Arg, Typ)
8631 and then Etype (Arg) /= Any_Type
8632 then
8633 declare
8634 I : Interp_Index;
8635 It : Interp;
8636 Func : Entity_Id;
8638 begin
8639 Get_First_Interp (Arg, I, It);
8640 Func := It.Nam;
8641 Get_Next_Interp (I, It);
8643 -- Special-case the error message when the overloading is
8644 -- caused by a function that yields an array and can be
8645 -- called without parameters.
8647 if It.Nam = Func then
8648 Error_Msg_Sloc := Sloc (Func);
8649 Error_Msg_N ("ambiguous call to function#", Arg);
8650 Error_Msg_NE
8651 ("\\interpretation as call yields&", Arg, Typ);
8652 Error_Msg_NE
8653 ("\\interpretation as indexing of call yields&",
8654 Arg, Component_Type (Typ));
8656 else
8657 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
8659 Get_First_Interp (Arg, I, It);
8660 while Present (It.Nam) loop
8661 Error_Msg_Sloc := Sloc (It.Nam);
8663 if Base_Type (It.Typ) = Btyp
8664 or else
8665 Base_Type (It.Typ) = Base_Type (Ctyp)
8666 then
8667 Error_Msg_N -- CODEFIX
8668 ("\\possible interpretation#", Arg);
8669 end if;
8671 Get_Next_Interp (I, It);
8672 end loop;
8673 end if;
8674 end;
8675 end if;
8677 Resolve (Arg, Component_Type (Typ));
8679 if Nkind (Arg) = N_String_Literal then
8680 Set_Etype (Arg, Component_Type (Typ));
8681 end if;
8683 if Arg = Left_Opnd (N) then
8684 Set_Is_Component_Left_Opnd (N);
8685 else
8686 Set_Is_Component_Right_Opnd (N);
8687 end if;
8688 end if;
8690 else
8691 Resolve (Arg, Btyp);
8692 end if;
8694 -- Concatenation is restricted in SPARK: each operand must be either a
8695 -- string literal, the name of a string constant, a static character or
8696 -- string expression, or another concatenation. Arg cannot be a
8697 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
8698 -- separately on each final operand, past concatenation operations.
8700 if Is_Character_Type (Etype (Arg)) then
8701 if not Is_OK_Static_Expression (Arg) then
8702 Check_SPARK_Restriction
8703 ("character operand for concatenation should be static", Arg);
8704 end if;
8706 elsif Is_String_Type (Etype (Arg)) then
8707 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
8708 and then Is_Constant_Object (Entity (Arg)))
8709 and then not Is_OK_Static_Expression (Arg)
8710 then
8711 Check_SPARK_Restriction
8712 ("string operand for concatenation should be static", Arg);
8713 end if;
8715 -- Do not issue error on an operand that is neither a character nor a
8716 -- string, as the error is issued in Resolve_Op_Concat.
8718 else
8719 null;
8720 end if;
8722 Check_Unset_Reference (Arg);
8723 end Resolve_Op_Concat_Arg;
8725 -----------------------------
8726 -- Resolve_Op_Concat_First --
8727 -----------------------------
8729 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
8730 Btyp : constant Entity_Id := Base_Type (Typ);
8731 Op1 : constant Node_Id := Left_Opnd (N);
8732 Op2 : constant Node_Id := Right_Opnd (N);
8734 begin
8735 -- The parser folds an enormous sequence of concatenations of string
8736 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
8737 -- in the right operand. If the expression resolves to a predefined "&"
8738 -- operator, all is well. Otherwise, the parser's folding is wrong, so
8739 -- we give an error. See P_Simple_Expression in Par.Ch4.
8741 if Nkind (Op2) = N_String_Literal
8742 and then Is_Folded_In_Parser (Op2)
8743 and then Ekind (Entity (N)) = E_Function
8744 then
8745 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
8746 and then String_Length (Strval (Op1)) = 0);
8747 Error_Msg_N ("too many user-defined concatenations", N);
8748 return;
8749 end if;
8751 Set_Etype (N, Btyp);
8753 if Is_Limited_Composite (Btyp) then
8754 Error_Msg_N ("concatenation not available for limited array", N);
8755 Explain_Limited_Type (Btyp, N);
8756 end if;
8757 end Resolve_Op_Concat_First;
8759 ----------------------------
8760 -- Resolve_Op_Concat_Rest --
8761 ----------------------------
8763 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
8764 Op1 : constant Node_Id := Left_Opnd (N);
8765 Op2 : constant Node_Id := Right_Opnd (N);
8767 begin
8768 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
8770 Generate_Operator_Reference (N, Typ);
8772 if Is_String_Type (Typ) then
8773 Eval_Concatenation (N);
8774 end if;
8776 -- If this is not a static concatenation, but the result is a string
8777 -- type (and not an array of strings) ensure that static string operands
8778 -- have their subtypes properly constructed.
8780 if Nkind (N) /= N_String_Literal
8781 and then Is_Character_Type (Component_Type (Typ))
8782 then
8783 Set_String_Literal_Subtype (Op1, Typ);
8784 Set_String_Literal_Subtype (Op2, Typ);
8785 end if;
8786 end Resolve_Op_Concat_Rest;
8788 ----------------------
8789 -- Resolve_Op_Expon --
8790 ----------------------
8792 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
8793 B_Typ : constant Entity_Id := Base_Type (Typ);
8795 begin
8796 -- Catch attempts to do fixed-point exponentiation with universal
8797 -- operands, which is a case where the illegality is not caught during
8798 -- normal operator analysis. This is not done in preanalysis mode
8799 -- since the tree is not fully decorated during preanalysis.
8801 if Full_Analysis then
8802 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
8803 Error_Msg_N ("exponentiation not available for fixed point", N);
8804 return;
8806 elsif Nkind (Parent (N)) in N_Op
8807 and then Is_Fixed_Point_Type (Etype (Parent (N)))
8808 and then Etype (N) = Universal_Real
8809 and then Comes_From_Source (N)
8810 then
8811 Error_Msg_N ("exponentiation not available for fixed point", N);
8812 return;
8813 end if;
8814 end if;
8816 if Comes_From_Source (N)
8817 and then Ekind (Entity (N)) = E_Function
8818 and then Is_Imported (Entity (N))
8819 and then Is_Intrinsic_Subprogram (Entity (N))
8820 then
8821 Resolve_Intrinsic_Operator (N, Typ);
8822 return;
8823 end if;
8825 if Etype (Left_Opnd (N)) = Universal_Integer
8826 or else Etype (Left_Opnd (N)) = Universal_Real
8827 then
8828 Check_For_Visible_Operator (N, B_Typ);
8829 end if;
8831 -- We do the resolution using the base type, because intermediate values
8832 -- in expressions are always of the base type, not a subtype of it.
8834 Resolve (Left_Opnd (N), B_Typ);
8835 Resolve (Right_Opnd (N), Standard_Integer);
8837 -- For integer types, right argument must be in Natural range
8839 if Is_Integer_Type (Typ) then
8840 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
8841 end if;
8843 Check_Unset_Reference (Left_Opnd (N));
8844 Check_Unset_Reference (Right_Opnd (N));
8846 Set_Etype (N, B_Typ);
8847 Generate_Operator_Reference (N, B_Typ);
8849 Analyze_Dimension (N);
8851 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
8852 -- Evaluate the exponentiation operator for dimensioned type
8854 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
8855 else
8856 Eval_Op_Expon (N);
8857 end if;
8859 -- Set overflow checking bit. Much cleverer code needed here eventually
8860 -- and perhaps the Resolve routines should be separated for the various
8861 -- arithmetic operations, since they will need different processing. ???
8863 if Nkind (N) in N_Op then
8864 if not Overflow_Checks_Suppressed (Etype (N)) then
8865 Enable_Overflow_Check (N);
8866 end if;
8867 end if;
8868 end Resolve_Op_Expon;
8870 --------------------
8871 -- Resolve_Op_Not --
8872 --------------------
8874 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8875 B_Typ : Entity_Id;
8877 function Parent_Is_Boolean return Boolean;
8878 -- This function determines if the parent node is a boolean operator or
8879 -- operation (comparison op, membership test, or short circuit form) and
8880 -- the not in question is the left operand of this operation. Note that
8881 -- if the not is in parens, then false is returned.
8883 -----------------------
8884 -- Parent_Is_Boolean --
8885 -----------------------
8887 function Parent_Is_Boolean return Boolean is
8888 begin
8889 if Paren_Count (N) /= 0 then
8890 return False;
8892 else
8893 case Nkind (Parent (N)) is
8894 when N_Op_And |
8895 N_Op_Eq |
8896 N_Op_Ge |
8897 N_Op_Gt |
8898 N_Op_Le |
8899 N_Op_Lt |
8900 N_Op_Ne |
8901 N_Op_Or |
8902 N_Op_Xor |
8903 N_In |
8904 N_Not_In |
8905 N_And_Then |
8906 N_Or_Else =>
8908 return Left_Opnd (Parent (N)) = N;
8910 when others =>
8911 return False;
8912 end case;
8913 end if;
8914 end Parent_Is_Boolean;
8916 -- Start of processing for Resolve_Op_Not
8918 begin
8919 -- Predefined operations on scalar types yield the base type. On the
8920 -- other hand, logical operations on arrays yield the type of the
8921 -- arguments (and the context).
8923 if Is_Array_Type (Typ) then
8924 B_Typ := Typ;
8925 else
8926 B_Typ := Base_Type (Typ);
8927 end if;
8929 -- Straightforward case of incorrect arguments
8931 if not Valid_Boolean_Arg (Typ) then
8932 Error_Msg_N ("invalid operand type for operator&", N);
8933 Set_Etype (N, Any_Type);
8934 return;
8936 -- Special case of probable missing parens
8938 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8939 if Parent_Is_Boolean then
8940 Error_Msg_N
8941 ("operand of not must be enclosed in parentheses",
8942 Right_Opnd (N));
8943 else
8944 Error_Msg_N
8945 ("no modular type available in this context", N);
8946 end if;
8948 Set_Etype (N, Any_Type);
8949 return;
8951 -- OK resolution of NOT
8953 else
8954 -- Warn if non-boolean types involved. This is a case like not a < b
8955 -- where a and b are modular, where we will get (not a) < b and most
8956 -- likely not (a < b) was intended.
8958 if Warn_On_Questionable_Missing_Parens
8959 and then not Is_Boolean_Type (Typ)
8960 and then Parent_Is_Boolean
8961 then
8962 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
8963 end if;
8965 -- Warn on double negation if checking redundant constructs
8967 if Warn_On_Redundant_Constructs
8968 and then Comes_From_Source (N)
8969 and then Comes_From_Source (Right_Opnd (N))
8970 and then Root_Type (Typ) = Standard_Boolean
8971 and then Nkind (Right_Opnd (N)) = N_Op_Not
8972 then
8973 Error_Msg_N ("redundant double negation?r?", N);
8974 end if;
8976 -- Complete resolution and evaluation of NOT
8978 Resolve (Right_Opnd (N), B_Typ);
8979 Check_Unset_Reference (Right_Opnd (N));
8980 Set_Etype (N, B_Typ);
8981 Generate_Operator_Reference (N, B_Typ);
8982 Eval_Op_Not (N);
8983 end if;
8984 end Resolve_Op_Not;
8986 -----------------------------
8987 -- Resolve_Operator_Symbol --
8988 -----------------------------
8990 -- Nothing to be done, all resolved already
8992 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8993 pragma Warnings (Off, N);
8994 pragma Warnings (Off, Typ);
8996 begin
8997 null;
8998 end Resolve_Operator_Symbol;
9000 ----------------------------------
9001 -- Resolve_Qualified_Expression --
9002 ----------------------------------
9004 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
9005 pragma Warnings (Off, Typ);
9007 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
9008 Expr : constant Node_Id := Expression (N);
9010 begin
9011 Resolve (Expr, Target_Typ);
9013 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9014 -- operation if not needed.
9016 if Restriction_Check_Required (SPARK_05)
9017 and then Is_Array_Type (Target_Typ)
9018 and then Is_Array_Type (Etype (Expr))
9019 and then Etype (Expr) /= Any_Composite -- or else Expr in error
9020 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
9021 then
9022 Check_SPARK_Restriction
9023 ("array types should have matching static bounds", N);
9024 end if;
9026 -- A qualified expression requires an exact match of the type, class-
9027 -- wide matching is not allowed. However, if the qualifying type is
9028 -- specific and the expression has a class-wide type, it may still be
9029 -- okay, since it can be the result of the expansion of a call to a
9030 -- dispatching function, so we also have to check class-wideness of the
9031 -- type of the expression's original node.
9033 if (Is_Class_Wide_Type (Target_Typ)
9034 or else
9035 (Is_Class_Wide_Type (Etype (Expr))
9036 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
9037 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
9038 then
9039 Wrong_Type (Expr, Target_Typ);
9040 end if;
9042 -- If the target type is unconstrained, then we reset the type of the
9043 -- result from the type of the expression. For other cases, the actual
9044 -- subtype of the expression is the target type.
9046 if Is_Composite_Type (Target_Typ)
9047 and then not Is_Constrained (Target_Typ)
9048 then
9049 Set_Etype (N, Etype (Expr));
9050 end if;
9052 Analyze_Dimension (N);
9053 Eval_Qualified_Expression (N);
9054 end Resolve_Qualified_Expression;
9056 ------------------------------
9057 -- Resolve_Raise_Expression --
9058 ------------------------------
9060 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
9061 begin
9062 if Typ = Raise_Type then
9063 Error_Msg_N ("cannot find unique type for raise expression", N);
9064 Set_Etype (N, Any_Type);
9065 else
9066 Set_Etype (N, Typ);
9067 end if;
9068 end Resolve_Raise_Expression;
9070 -------------------
9071 -- Resolve_Range --
9072 -------------------
9074 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
9075 L : constant Node_Id := Low_Bound (N);
9076 H : constant Node_Id := High_Bound (N);
9078 function First_Last_Ref return Boolean;
9079 -- Returns True if N is of the form X'First .. X'Last where X is the
9080 -- same entity for both attributes.
9082 --------------------
9083 -- First_Last_Ref --
9084 --------------------
9086 function First_Last_Ref return Boolean is
9087 Lorig : constant Node_Id := Original_Node (L);
9088 Horig : constant Node_Id := Original_Node (H);
9090 begin
9091 if Nkind (Lorig) = N_Attribute_Reference
9092 and then Nkind (Horig) = N_Attribute_Reference
9093 and then Attribute_Name (Lorig) = Name_First
9094 and then Attribute_Name (Horig) = Name_Last
9095 then
9096 declare
9097 PL : constant Node_Id := Prefix (Lorig);
9098 PH : constant Node_Id := Prefix (Horig);
9099 begin
9100 if Is_Entity_Name (PL)
9101 and then Is_Entity_Name (PH)
9102 and then Entity (PL) = Entity (PH)
9103 then
9104 return True;
9105 end if;
9106 end;
9107 end if;
9109 return False;
9110 end First_Last_Ref;
9112 -- Start of processing for Resolve_Range
9114 begin
9115 Set_Etype (N, Typ);
9116 Resolve (L, Typ);
9117 Resolve (H, Typ);
9119 -- Check for inappropriate range on unordered enumeration type
9121 if Bad_Unordered_Enumeration_Reference (N, Typ)
9123 -- Exclude X'First .. X'Last if X is the same entity for both
9125 and then not First_Last_Ref
9126 then
9127 Error_Msg_Sloc := Sloc (Typ);
9128 Error_Msg_NE
9129 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
9130 end if;
9132 Check_Unset_Reference (L);
9133 Check_Unset_Reference (H);
9135 -- We have to check the bounds for being within the base range as
9136 -- required for a non-static context. Normally this is automatic and
9137 -- done as part of evaluating expressions, but the N_Range node is an
9138 -- exception, since in GNAT we consider this node to be a subexpression,
9139 -- even though in Ada it is not. The circuit in Sem_Eval could check for
9140 -- this, but that would put the test on the main evaluation path for
9141 -- expressions.
9143 Check_Non_Static_Context (L);
9144 Check_Non_Static_Context (H);
9146 -- Check for an ambiguous range over character literals. This will
9147 -- happen with a membership test involving only literals.
9149 if Typ = Any_Character then
9150 Ambiguous_Character (L);
9151 Set_Etype (N, Any_Type);
9152 return;
9153 end if;
9155 -- If bounds are static, constant-fold them, so size computations are
9156 -- identical between front-end and back-end. Do not perform this
9157 -- transformation while analyzing generic units, as type information
9158 -- would be lost when reanalyzing the constant node in the instance.
9160 if Is_Discrete_Type (Typ) and then Expander_Active then
9161 if Is_OK_Static_Expression (L) then
9162 Fold_Uint (L, Expr_Value (L), Is_OK_Static_Expression (L));
9163 end if;
9165 if Is_OK_Static_Expression (H) then
9166 Fold_Uint (H, Expr_Value (H), Is_OK_Static_Expression (H));
9167 end if;
9168 end if;
9169 end Resolve_Range;
9171 --------------------------
9172 -- Resolve_Real_Literal --
9173 --------------------------
9175 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
9176 Actual_Typ : constant Entity_Id := Etype (N);
9178 begin
9179 -- Special processing for fixed-point literals to make sure that the
9180 -- value is an exact multiple of small where this is required. We skip
9181 -- this for the universal real case, and also for generic types.
9183 if Is_Fixed_Point_Type (Typ)
9184 and then Typ /= Universal_Fixed
9185 and then Typ /= Any_Fixed
9186 and then not Is_Generic_Type (Typ)
9187 then
9188 declare
9189 Val : constant Ureal := Realval (N);
9190 Cintr : constant Ureal := Val / Small_Value (Typ);
9191 Cint : constant Uint := UR_Trunc (Cintr);
9192 Den : constant Uint := Norm_Den (Cintr);
9193 Stat : Boolean;
9195 begin
9196 -- Case of literal is not an exact multiple of the Small
9198 if Den /= 1 then
9200 -- For a source program literal for a decimal fixed-point type,
9201 -- this is statically illegal (RM 4.9(36)).
9203 if Is_Decimal_Fixed_Point_Type (Typ)
9204 and then Actual_Typ = Universal_Real
9205 and then Comes_From_Source (N)
9206 then
9207 Error_Msg_N ("value has extraneous low order digits", N);
9208 end if;
9210 -- Generate a warning if literal from source
9212 if Is_OK_Static_Expression (N)
9213 and then Warn_On_Bad_Fixed_Value
9214 then
9215 Error_Msg_N
9216 ("?b?static fixed-point value is not a multiple of Small!",
9218 end if;
9220 -- Replace literal by a value that is the exact representation
9221 -- of a value of the type, i.e. a multiple of the small value,
9222 -- by truncation, since Machine_Rounds is false for all GNAT
9223 -- fixed-point types (RM 4.9(38)).
9225 Stat := Is_OK_Static_Expression (N);
9226 Rewrite (N,
9227 Make_Real_Literal (Sloc (N),
9228 Realval => Small_Value (Typ) * Cint));
9230 Set_Is_Static_Expression (N, Stat);
9231 end if;
9233 -- In all cases, set the corresponding integer field
9235 Set_Corresponding_Integer_Value (N, Cint);
9236 end;
9237 end if;
9239 -- Now replace the actual type by the expected type as usual
9241 Set_Etype (N, Typ);
9242 Eval_Real_Literal (N);
9243 end Resolve_Real_Literal;
9245 -----------------------
9246 -- Resolve_Reference --
9247 -----------------------
9249 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
9250 P : constant Node_Id := Prefix (N);
9252 begin
9253 -- Replace general access with specific type
9255 if Ekind (Etype (N)) = E_Allocator_Type then
9256 Set_Etype (N, Base_Type (Typ));
9257 end if;
9259 Resolve (P, Designated_Type (Etype (N)));
9261 -- If we are taking the reference of a volatile entity, then treat it as
9262 -- a potential modification of this entity. This is too conservative,
9263 -- but necessary because remove side effects can cause transformations
9264 -- of normal assignments into reference sequences that otherwise fail to
9265 -- notice the modification.
9267 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
9268 Note_Possible_Modification (P, Sure => False);
9269 end if;
9270 end Resolve_Reference;
9272 --------------------------------
9273 -- Resolve_Selected_Component --
9274 --------------------------------
9276 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
9277 Comp : Entity_Id;
9278 Comp1 : Entity_Id := Empty; -- prevent junk warning
9279 P : constant Node_Id := Prefix (N);
9280 S : constant Node_Id := Selector_Name (N);
9281 T : Entity_Id := Etype (P);
9282 I : Interp_Index;
9283 I1 : Interp_Index := 0; -- prevent junk warning
9284 It : Interp;
9285 It1 : Interp;
9286 Found : Boolean;
9288 function Init_Component return Boolean;
9289 -- Check whether this is the initialization of a component within an
9290 -- init proc (by assignment or call to another init proc). If true,
9291 -- there is no need for a discriminant check.
9293 --------------------
9294 -- Init_Component --
9295 --------------------
9297 function Init_Component return Boolean is
9298 begin
9299 return Inside_Init_Proc
9300 and then Nkind (Prefix (N)) = N_Identifier
9301 and then Chars (Prefix (N)) = Name_uInit
9302 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
9303 end Init_Component;
9305 -- Start of processing for Resolve_Selected_Component
9307 begin
9308 if Is_Overloaded (P) then
9310 -- Use the context type to select the prefix that has a selector
9311 -- of the correct name and type.
9313 Found := False;
9314 Get_First_Interp (P, I, It);
9316 Search : while Present (It.Typ) loop
9317 if Is_Access_Type (It.Typ) then
9318 T := Designated_Type (It.Typ);
9319 else
9320 T := It.Typ;
9321 end if;
9323 -- Locate selected component. For a private prefix the selector
9324 -- can denote a discriminant.
9326 if Is_Record_Type (T) or else Is_Private_Type (T) then
9328 -- The visible components of a class-wide type are those of
9329 -- the root type.
9331 if Is_Class_Wide_Type (T) then
9332 T := Etype (T);
9333 end if;
9335 Comp := First_Entity (T);
9336 while Present (Comp) loop
9337 if Chars (Comp) = Chars (S)
9338 and then Covers (Typ, Etype (Comp))
9339 then
9340 if not Found then
9341 Found := True;
9342 I1 := I;
9343 It1 := It;
9344 Comp1 := Comp;
9346 else
9347 It := Disambiguate (P, I1, I, Any_Type);
9349 if It = No_Interp then
9350 Error_Msg_N
9351 ("ambiguous prefix for selected component", N);
9352 Set_Etype (N, Typ);
9353 return;
9355 else
9356 It1 := It;
9358 -- There may be an implicit dereference. Retrieve
9359 -- designated record type.
9361 if Is_Access_Type (It1.Typ) then
9362 T := Designated_Type (It1.Typ);
9363 else
9364 T := It1.Typ;
9365 end if;
9367 if Scope (Comp1) /= T then
9369 -- Resolution chooses the new interpretation.
9370 -- Find the component with the right name.
9372 Comp1 := First_Entity (T);
9373 while Present (Comp1)
9374 and then Chars (Comp1) /= Chars (S)
9375 loop
9376 Comp1 := Next_Entity (Comp1);
9377 end loop;
9378 end if;
9380 exit Search;
9381 end if;
9382 end if;
9383 end if;
9385 Comp := Next_Entity (Comp);
9386 end loop;
9387 end if;
9389 Get_Next_Interp (I, It);
9390 end loop Search;
9392 -- There must be a legal interpretation at this point
9394 pragma Assert (Found);
9395 Resolve (P, It1.Typ);
9396 Set_Etype (N, Typ);
9397 Set_Entity_With_Checks (S, Comp1);
9399 else
9400 -- Resolve prefix with its type
9402 Resolve (P, T);
9403 end if;
9405 -- Generate cross-reference. We needed to wait until full overloading
9406 -- resolution was complete to do this, since otherwise we can't tell if
9407 -- we are an lvalue or not.
9409 if May_Be_Lvalue (N) then
9410 Generate_Reference (Entity (S), S, 'm');
9411 else
9412 Generate_Reference (Entity (S), S, 'r');
9413 end if;
9415 -- If prefix is an access type, the node will be transformed into an
9416 -- explicit dereference during expansion. The type of the node is the
9417 -- designated type of that of the prefix.
9419 if Is_Access_Type (Etype (P)) then
9420 T := Designated_Type (Etype (P));
9421 Check_Fully_Declared_Prefix (T, P);
9422 else
9423 T := Etype (P);
9424 end if;
9426 -- Set flag for expander if discriminant check required
9428 if Has_Discriminants (T)
9429 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
9430 and then Present (Original_Record_Component (Entity (S)))
9431 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
9432 and then not Discriminant_Checks_Suppressed (T)
9433 and then not Init_Component
9434 then
9435 Set_Do_Discriminant_Check (N);
9436 end if;
9438 if Ekind (Entity (S)) = E_Void then
9439 Error_Msg_N ("premature use of component", S);
9440 end if;
9442 -- If the prefix is a record conversion, this may be a renamed
9443 -- discriminant whose bounds differ from those of the original
9444 -- one, so we must ensure that a range check is performed.
9446 if Nkind (P) = N_Type_Conversion
9447 and then Ekind (Entity (S)) = E_Discriminant
9448 and then Is_Discrete_Type (Typ)
9449 then
9450 Set_Etype (N, Base_Type (Typ));
9451 end if;
9453 -- Note: No Eval processing is required, because the prefix is of a
9454 -- record type, or protected type, and neither can possibly be static.
9456 -- If the array type is atomic, and is packed, and we are in a left side
9457 -- context, then this is worth a warning, since we have a situation
9458 -- where the access to the component may cause extra read/writes of the
9459 -- atomic array object, which could be considered unexpected.
9461 if Nkind (N) = N_Selected_Component
9462 and then (Is_Atomic (T)
9463 or else (Is_Entity_Name (Prefix (N))
9464 and then Is_Atomic (Entity (Prefix (N)))))
9465 and then Is_Packed (T)
9466 and then Is_LHS (N) = Yes
9467 then
9468 Error_Msg_N
9469 ("??assignment to component of packed atomic record", Prefix (N));
9470 Error_Msg_N
9471 ("\??may cause unexpected accesses to atomic object", Prefix (N));
9472 end if;
9474 Analyze_Dimension (N);
9475 end Resolve_Selected_Component;
9477 -------------------
9478 -- Resolve_Shift --
9479 -------------------
9481 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
9482 B_Typ : constant Entity_Id := Base_Type (Typ);
9483 L : constant Node_Id := Left_Opnd (N);
9484 R : constant Node_Id := Right_Opnd (N);
9486 begin
9487 -- We do the resolution using the base type, because intermediate values
9488 -- in expressions always are of the base type, not a subtype of it.
9490 Resolve (L, B_Typ);
9491 Resolve (R, Standard_Natural);
9493 Check_Unset_Reference (L);
9494 Check_Unset_Reference (R);
9496 Set_Etype (N, B_Typ);
9497 Generate_Operator_Reference (N, B_Typ);
9498 Eval_Shift (N);
9499 end Resolve_Shift;
9501 ---------------------------
9502 -- Resolve_Short_Circuit --
9503 ---------------------------
9505 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
9506 B_Typ : constant Entity_Id := Base_Type (Typ);
9507 L : constant Node_Id := Left_Opnd (N);
9508 R : constant Node_Id := Right_Opnd (N);
9510 begin
9511 -- Ensure all actions associated with the left operand (e.g.
9512 -- finalization of transient controlled objects) are fully evaluated
9513 -- locally within an expression with actions. This is particularly
9514 -- helpful for coverage analysis. However this should not happen in
9515 -- generics.
9517 if Expander_Active then
9518 declare
9519 Reloc_L : constant Node_Id := Relocate_Node (L);
9520 begin
9521 Save_Interps (Old_N => L, New_N => Reloc_L);
9523 Rewrite (L,
9524 Make_Expression_With_Actions (Sloc (L),
9525 Actions => New_List,
9526 Expression => Reloc_L));
9528 -- Set Comes_From_Source on L to preserve warnings for unset
9529 -- reference.
9531 Set_Comes_From_Source (L, Comes_From_Source (Reloc_L));
9532 end;
9533 end if;
9535 Resolve (L, B_Typ);
9536 Resolve (R, B_Typ);
9538 -- Check for issuing warning for always False assert/check, this happens
9539 -- when assertions are turned off, in which case the pragma Assert/Check
9540 -- was transformed into:
9542 -- if False and then <condition> then ...
9544 -- and we detect this pattern
9546 if Warn_On_Assertion_Failure
9547 and then Is_Entity_Name (R)
9548 and then Entity (R) = Standard_False
9549 and then Nkind (Parent (N)) = N_If_Statement
9550 and then Nkind (N) = N_And_Then
9551 and then Is_Entity_Name (L)
9552 and then Entity (L) = Standard_False
9553 then
9554 declare
9555 Orig : constant Node_Id := Original_Node (Parent (N));
9557 begin
9558 -- Special handling of Asssert pragma
9560 if Nkind (Orig) = N_Pragma
9561 and then Pragma_Name (Orig) = Name_Assert
9562 then
9563 declare
9564 Expr : constant Node_Id :=
9565 Original_Node
9566 (Expression
9567 (First (Pragma_Argument_Associations (Orig))));
9569 begin
9570 -- Don't warn if original condition is explicit False,
9571 -- since obviously the failure is expected in this case.
9573 if Is_Entity_Name (Expr)
9574 and then Entity (Expr) = Standard_False
9575 then
9576 null;
9578 -- Issue warning. We do not want the deletion of the
9579 -- IF/AND-THEN to take this message with it. We achieve this
9580 -- by making sure that the expanded code points to the Sloc
9581 -- of the expression, not the original pragma.
9583 else
9584 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
9585 -- The source location of the expression is not usually
9586 -- the best choice here. For example, it gets located on
9587 -- the last AND keyword in a chain of boolean expressiond
9588 -- AND'ed together. It is best to put the message on the
9589 -- first character of the assertion, which is the effect
9590 -- of the First_Node call here.
9592 Error_Msg_F
9593 ("?A?assertion would fail at run time!",
9594 Expression
9595 (First (Pragma_Argument_Associations (Orig))));
9596 end if;
9597 end;
9599 -- Similar processing for Check pragma
9601 elsif Nkind (Orig) = N_Pragma
9602 and then Pragma_Name (Orig) = Name_Check
9603 then
9604 -- Don't want to warn if original condition is explicit False
9606 declare
9607 Expr : constant Node_Id :=
9608 Original_Node
9609 (Expression
9610 (Next (First (Pragma_Argument_Associations (Orig)))));
9611 begin
9612 if Is_Entity_Name (Expr)
9613 and then Entity (Expr) = Standard_False
9614 then
9615 null;
9617 -- Post warning
9619 else
9620 -- Again use Error_Msg_F rather than Error_Msg_N, see
9621 -- comment above for an explanation of why we do this.
9623 Error_Msg_F
9624 ("?A?check would fail at run time!",
9625 Expression
9626 (Last (Pragma_Argument_Associations (Orig))));
9627 end if;
9628 end;
9629 end if;
9630 end;
9631 end if;
9633 -- Continue with processing of short circuit
9635 Check_Unset_Reference (L);
9636 Check_Unset_Reference (R);
9638 Set_Etype (N, B_Typ);
9639 Eval_Short_Circuit (N);
9640 end Resolve_Short_Circuit;
9642 -------------------
9643 -- Resolve_Slice --
9644 -------------------
9646 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
9647 Drange : constant Node_Id := Discrete_Range (N);
9648 Name : constant Node_Id := Prefix (N);
9649 Array_Type : Entity_Id := Empty;
9650 Dexpr : Node_Id := Empty;
9651 Index_Type : Entity_Id;
9653 begin
9654 if Is_Overloaded (Name) then
9656 -- Use the context type to select the prefix that yields the correct
9657 -- array type.
9659 declare
9660 I : Interp_Index;
9661 I1 : Interp_Index := 0;
9662 It : Interp;
9663 P : constant Node_Id := Prefix (N);
9664 Found : Boolean := False;
9666 begin
9667 Get_First_Interp (P, I, It);
9668 while Present (It.Typ) loop
9669 if (Is_Array_Type (It.Typ)
9670 and then Covers (Typ, It.Typ))
9671 or else (Is_Access_Type (It.Typ)
9672 and then Is_Array_Type (Designated_Type (It.Typ))
9673 and then Covers (Typ, Designated_Type (It.Typ)))
9674 then
9675 if Found then
9676 It := Disambiguate (P, I1, I, Any_Type);
9678 if It = No_Interp then
9679 Error_Msg_N ("ambiguous prefix for slicing", N);
9680 Set_Etype (N, Typ);
9681 return;
9682 else
9683 Found := True;
9684 Array_Type := It.Typ;
9685 I1 := I;
9686 end if;
9687 else
9688 Found := True;
9689 Array_Type := It.Typ;
9690 I1 := I;
9691 end if;
9692 end if;
9694 Get_Next_Interp (I, It);
9695 end loop;
9696 end;
9698 else
9699 Array_Type := Etype (Name);
9700 end if;
9702 Resolve (Name, Array_Type);
9704 if Is_Access_Type (Array_Type) then
9705 Apply_Access_Check (N);
9706 Array_Type := Designated_Type (Array_Type);
9708 -- If the prefix is an access to an unconstrained array, we must use
9709 -- the actual subtype of the object to perform the index checks. The
9710 -- object denoted by the prefix is implicit in the node, so we build
9711 -- an explicit representation for it in order to compute the actual
9712 -- subtype.
9714 if not Is_Constrained (Array_Type) then
9715 Remove_Side_Effects (Prefix (N));
9717 declare
9718 Obj : constant Node_Id :=
9719 Make_Explicit_Dereference (Sloc (N),
9720 Prefix => New_Copy_Tree (Prefix (N)));
9721 begin
9722 Set_Etype (Obj, Array_Type);
9723 Set_Parent (Obj, Parent (N));
9724 Array_Type := Get_Actual_Subtype (Obj);
9725 end;
9726 end if;
9728 elsif Is_Entity_Name (Name)
9729 or else Nkind (Name) = N_Explicit_Dereference
9730 or else (Nkind (Name) = N_Function_Call
9731 and then not Is_Constrained (Etype (Name)))
9732 then
9733 Array_Type := Get_Actual_Subtype (Name);
9735 -- If the name is a selected component that depends on discriminants,
9736 -- build an actual subtype for it. This can happen only when the name
9737 -- itself is overloaded; otherwise the actual subtype is created when
9738 -- the selected component is analyzed.
9740 elsif Nkind (Name) = N_Selected_Component
9741 and then Full_Analysis
9742 and then Depends_On_Discriminant (First_Index (Array_Type))
9743 then
9744 declare
9745 Act_Decl : constant Node_Id :=
9746 Build_Actual_Subtype_Of_Component (Array_Type, Name);
9747 begin
9748 Insert_Action (N, Act_Decl);
9749 Array_Type := Defining_Identifier (Act_Decl);
9750 end;
9752 -- Maybe this should just be "else", instead of checking for the
9753 -- specific case of slice??? This is needed for the case where the
9754 -- prefix is an Image attribute, which gets expanded to a slice, and so
9755 -- has a constrained subtype which we want to use for the slice range
9756 -- check applied below (the range check won't get done if the
9757 -- unconstrained subtype of the 'Image is used).
9759 elsif Nkind (Name) = N_Slice then
9760 Array_Type := Etype (Name);
9761 end if;
9763 -- Obtain the type of the array index
9765 if Ekind (Array_Type) = E_String_Literal_Subtype then
9766 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
9767 else
9768 Index_Type := Etype (First_Index (Array_Type));
9769 end if;
9771 -- If name was overloaded, set slice type correctly now
9773 Set_Etype (N, Array_Type);
9775 -- Handle the generation of a range check that compares the array index
9776 -- against the discrete_range. The check is not applied to internally
9777 -- built nodes associated with the expansion of dispatch tables. Check
9778 -- that Ada.Tags has already been loaded to avoid extra dependencies on
9779 -- the unit.
9781 if Tagged_Type_Expansion
9782 and then RTU_Loaded (Ada_Tags)
9783 and then Nkind (Prefix (N)) = N_Selected_Component
9784 and then Present (Entity (Selector_Name (Prefix (N))))
9785 and then Entity (Selector_Name (Prefix (N))) =
9786 RTE_Record_Component (RE_Prims_Ptr)
9787 then
9788 null;
9790 -- The discrete_range is specified by a subtype indication. Create a
9791 -- shallow copy and inherit the type, parent and source location from
9792 -- the discrete_range. This ensures that the range check is inserted
9793 -- relative to the slice and that the runtime exception points to the
9794 -- proper construct.
9796 elsif Is_Entity_Name (Drange) then
9797 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
9799 Set_Etype (Dexpr, Etype (Drange));
9800 Set_Parent (Dexpr, Parent (Drange));
9801 Set_Sloc (Dexpr, Sloc (Drange));
9803 -- The discrete_range is a regular range. Resolve the bounds and remove
9804 -- their side effects.
9806 else
9807 Resolve (Drange, Base_Type (Index_Type));
9809 if Nkind (Drange) = N_Range then
9810 Force_Evaluation (Low_Bound (Drange));
9811 Force_Evaluation (High_Bound (Drange));
9813 Dexpr := Drange;
9814 end if;
9815 end if;
9817 if Present (Dexpr) then
9818 Apply_Range_Check (Dexpr, Index_Type);
9819 end if;
9821 Set_Slice_Subtype (N);
9823 -- Check bad use of type with predicates
9825 declare
9826 Subt : Entity_Id;
9828 begin
9829 if Nkind (Drange) = N_Subtype_Indication
9830 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
9831 then
9832 Subt := Entity (Subtype_Mark (Drange));
9833 else
9834 Subt := Etype (Drange);
9835 end if;
9837 if Has_Predicates (Subt) then
9838 Bad_Predicated_Subtype_Use
9839 ("subtype& has predicate, not allowed in slice", Drange, Subt);
9840 end if;
9841 end;
9843 -- Otherwise here is where we check suspicious indexes
9845 if Nkind (Drange) = N_Range then
9846 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
9847 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
9848 end if;
9850 Analyze_Dimension (N);
9851 Eval_Slice (N);
9852 end Resolve_Slice;
9854 ----------------------------
9855 -- Resolve_String_Literal --
9856 ----------------------------
9858 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
9859 C_Typ : constant Entity_Id := Component_Type (Typ);
9860 R_Typ : constant Entity_Id := Root_Type (C_Typ);
9861 Loc : constant Source_Ptr := Sloc (N);
9862 Str : constant String_Id := Strval (N);
9863 Strlen : constant Nat := String_Length (Str);
9864 Subtype_Id : Entity_Id;
9865 Need_Check : Boolean;
9867 begin
9868 -- For a string appearing in a concatenation, defer creation of the
9869 -- string_literal_subtype until the end of the resolution of the
9870 -- concatenation, because the literal may be constant-folded away. This
9871 -- is a useful optimization for long concatenation expressions.
9873 -- If the string is an aggregate built for a single character (which
9874 -- happens in a non-static context) or a is null string to which special
9875 -- checks may apply, we build the subtype. Wide strings must also get a
9876 -- string subtype if they come from a one character aggregate. Strings
9877 -- generated by attributes might be static, but it is often hard to
9878 -- determine whether the enclosing context is static, so we generate
9879 -- subtypes for them as well, thus losing some rarer optimizations ???
9880 -- Same for strings that come from a static conversion.
9882 Need_Check :=
9883 (Strlen = 0 and then Typ /= Standard_String)
9884 or else Nkind (Parent (N)) /= N_Op_Concat
9885 or else (N /= Left_Opnd (Parent (N))
9886 and then N /= Right_Opnd (Parent (N)))
9887 or else ((Typ = Standard_Wide_String
9888 or else Typ = Standard_Wide_Wide_String)
9889 and then Nkind (Original_Node (N)) /= N_String_Literal);
9891 -- If the resolving type is itself a string literal subtype, we can just
9892 -- reuse it, since there is no point in creating another.
9894 if Ekind (Typ) = E_String_Literal_Subtype then
9895 Subtype_Id := Typ;
9897 elsif Nkind (Parent (N)) = N_Op_Concat
9898 and then not Need_Check
9899 and then not Nkind_In (Original_Node (N), N_Character_Literal,
9900 N_Attribute_Reference,
9901 N_Qualified_Expression,
9902 N_Type_Conversion)
9903 then
9904 Subtype_Id := Typ;
9906 -- Otherwise we must create a string literal subtype. Note that the
9907 -- whole idea of string literal subtypes is simply to avoid the need
9908 -- for building a full fledged array subtype for each literal.
9910 else
9911 Set_String_Literal_Subtype (N, Typ);
9912 Subtype_Id := Etype (N);
9913 end if;
9915 if Nkind (Parent (N)) /= N_Op_Concat
9916 or else Need_Check
9917 then
9918 Set_Etype (N, Subtype_Id);
9919 Eval_String_Literal (N);
9920 end if;
9922 if Is_Limited_Composite (Typ)
9923 or else Is_Private_Composite (Typ)
9924 then
9925 Error_Msg_N ("string literal not available for private array", N);
9926 Set_Etype (N, Any_Type);
9927 return;
9928 end if;
9930 -- The validity of a null string has been checked in the call to
9931 -- Eval_String_Literal.
9933 if Strlen = 0 then
9934 return;
9936 -- Always accept string literal with component type Any_Character, which
9937 -- occurs in error situations and in comparisons of literals, both of
9938 -- which should accept all literals.
9940 elsif R_Typ = Any_Character then
9941 return;
9943 -- If the type is bit-packed, then we always transform the string
9944 -- literal into a full fledged aggregate.
9946 elsif Is_Bit_Packed_Array (Typ) then
9947 null;
9949 -- Deal with cases of Wide_Wide_String, Wide_String, and String
9951 else
9952 -- For Standard.Wide_Wide_String, or any other type whose component
9953 -- type is Standard.Wide_Wide_Character, we know that all the
9954 -- characters in the string must be acceptable, since the parser
9955 -- accepted the characters as valid character literals.
9957 if R_Typ = Standard_Wide_Wide_Character then
9958 null;
9960 -- For the case of Standard.String, or any other type whose component
9961 -- type is Standard.Character, we must make sure that there are no
9962 -- wide characters in the string, i.e. that it is entirely composed
9963 -- of characters in range of type Character.
9965 -- If the string literal is the result of a static concatenation, the
9966 -- test has already been performed on the components, and need not be
9967 -- repeated.
9969 elsif R_Typ = Standard_Character
9970 and then Nkind (Original_Node (N)) /= N_Op_Concat
9971 then
9972 for J in 1 .. Strlen loop
9973 if not In_Character_Range (Get_String_Char (Str, J)) then
9975 -- If we are out of range, post error. This is one of the
9976 -- very few places that we place the flag in the middle of
9977 -- a token, right under the offending wide character. Not
9978 -- quite clear if this is right wrt wide character encoding
9979 -- sequences, but it's only an error message.
9981 Error_Msg
9982 ("literal out of range of type Standard.Character",
9983 Source_Ptr (Int (Loc) + J));
9984 return;
9985 end if;
9986 end loop;
9988 -- For the case of Standard.Wide_String, or any other type whose
9989 -- component type is Standard.Wide_Character, we must make sure that
9990 -- there are no wide characters in the string, i.e. that it is
9991 -- entirely composed of characters in range of type Wide_Character.
9993 -- If the string literal is the result of a static concatenation,
9994 -- the test has already been performed on the components, and need
9995 -- not be repeated.
9997 elsif R_Typ = Standard_Wide_Character
9998 and then Nkind (Original_Node (N)) /= N_Op_Concat
9999 then
10000 for J in 1 .. Strlen loop
10001 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
10003 -- If we are out of range, post error. This is one of the
10004 -- very few places that we place the flag in the middle of
10005 -- a token, right under the offending wide character.
10007 -- This is not quite right, because characters in general
10008 -- will take more than one character position ???
10010 Error_Msg
10011 ("literal out of range of type Standard.Wide_Character",
10012 Source_Ptr (Int (Loc) + J));
10013 return;
10014 end if;
10015 end loop;
10017 -- If the root type is not a standard character, then we will convert
10018 -- the string into an aggregate and will let the aggregate code do
10019 -- the checking. Standard Wide_Wide_Character is also OK here.
10021 else
10022 null;
10023 end if;
10025 -- See if the component type of the array corresponding to the string
10026 -- has compile time known bounds. If yes we can directly check
10027 -- whether the evaluation of the string will raise constraint error.
10028 -- Otherwise we need to transform the string literal into the
10029 -- corresponding character aggregate and let the aggregate code do
10030 -- the checking.
10032 if Is_Standard_Character_Type (R_Typ) then
10034 -- Check for the case of full range, where we are definitely OK
10036 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
10037 return;
10038 end if;
10040 -- Here the range is not the complete base type range, so check
10042 declare
10043 Comp_Typ_Lo : constant Node_Id :=
10044 Type_Low_Bound (Component_Type (Typ));
10045 Comp_Typ_Hi : constant Node_Id :=
10046 Type_High_Bound (Component_Type (Typ));
10048 Char_Val : Uint;
10050 begin
10051 if Compile_Time_Known_Value (Comp_Typ_Lo)
10052 and then Compile_Time_Known_Value (Comp_Typ_Hi)
10053 then
10054 for J in 1 .. Strlen loop
10055 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
10057 if Char_Val < Expr_Value (Comp_Typ_Lo)
10058 or else Char_Val > Expr_Value (Comp_Typ_Hi)
10059 then
10060 Apply_Compile_Time_Constraint_Error
10061 (N, "character out of range??",
10062 CE_Range_Check_Failed,
10063 Loc => Source_Ptr (Int (Loc) + J));
10064 end if;
10065 end loop;
10067 return;
10068 end if;
10069 end;
10070 end if;
10071 end if;
10073 -- If we got here we meed to transform the string literal into the
10074 -- equivalent qualified positional array aggregate. This is rather
10075 -- heavy artillery for this situation, but it is hard work to avoid.
10077 declare
10078 Lits : constant List_Id := New_List;
10079 P : Source_Ptr := Loc + 1;
10080 C : Char_Code;
10082 begin
10083 -- Build the character literals, we give them source locations that
10084 -- correspond to the string positions, which is a bit tricky given
10085 -- the possible presence of wide character escape sequences.
10087 for J in 1 .. Strlen loop
10088 C := Get_String_Char (Str, J);
10089 Set_Character_Literal_Name (C);
10091 Append_To (Lits,
10092 Make_Character_Literal (P,
10093 Chars => Name_Find,
10094 Char_Literal_Value => UI_From_CC (C)));
10096 if In_Character_Range (C) then
10097 P := P + 1;
10099 -- Should we have a call to Skip_Wide here ???
10101 -- ??? else
10102 -- Skip_Wide (P);
10104 end if;
10105 end loop;
10107 Rewrite (N,
10108 Make_Qualified_Expression (Loc,
10109 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
10110 Expression =>
10111 Make_Aggregate (Loc, Expressions => Lits)));
10113 Analyze_And_Resolve (N, Typ);
10114 end;
10115 end Resolve_String_Literal;
10117 -----------------------------
10118 -- Resolve_Type_Conversion --
10119 -----------------------------
10121 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
10122 Conv_OK : constant Boolean := Conversion_OK (N);
10123 Operand : constant Node_Id := Expression (N);
10124 Operand_Typ : constant Entity_Id := Etype (Operand);
10125 Target_Typ : constant Entity_Id := Etype (N);
10126 Rop : Node_Id;
10127 Orig_N : Node_Id;
10128 Orig_T : Node_Id;
10130 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
10131 -- Set to False to suppress cases where we want to suppress the test
10132 -- for redundancy to avoid possible false positives on this warning.
10134 begin
10135 if not Conv_OK
10136 and then not Valid_Conversion (N, Target_Typ, Operand)
10137 then
10138 return;
10139 end if;
10141 -- If the Operand Etype is Universal_Fixed, then the conversion is
10142 -- never redundant. We need this check because by the time we have
10143 -- finished the rather complex transformation, the conversion looks
10144 -- redundant when it is not.
10146 if Operand_Typ = Universal_Fixed then
10147 Test_Redundant := False;
10149 -- If the operand is marked as Any_Fixed, then special processing is
10150 -- required. This is also a case where we suppress the test for a
10151 -- redundant conversion, since most certainly it is not redundant.
10153 elsif Operand_Typ = Any_Fixed then
10154 Test_Redundant := False;
10156 -- Mixed-mode operation involving a literal. Context must be a fixed
10157 -- type which is applied to the literal subsequently.
10159 if Is_Fixed_Point_Type (Typ) then
10160 Set_Etype (Operand, Universal_Real);
10162 elsif Is_Numeric_Type (Typ)
10163 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
10164 and then (Etype (Right_Opnd (Operand)) = Universal_Real
10165 or else
10166 Etype (Left_Opnd (Operand)) = Universal_Real)
10167 then
10168 -- Return if expression is ambiguous
10170 if Unique_Fixed_Point_Type (N) = Any_Type then
10171 return;
10173 -- If nothing else, the available fixed type is Duration
10175 else
10176 Set_Etype (Operand, Standard_Duration);
10177 end if;
10179 -- Resolve the real operand with largest available precision
10181 if Etype (Right_Opnd (Operand)) = Universal_Real then
10182 Rop := New_Copy_Tree (Right_Opnd (Operand));
10183 else
10184 Rop := New_Copy_Tree (Left_Opnd (Operand));
10185 end if;
10187 Resolve (Rop, Universal_Real);
10189 -- If the operand is a literal (it could be a non-static and
10190 -- illegal exponentiation) check whether the use of Duration
10191 -- is potentially inaccurate.
10193 if Nkind (Rop) = N_Real_Literal
10194 and then Realval (Rop) /= Ureal_0
10195 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
10196 then
10197 Error_Msg_N
10198 ("??universal real operand can only "
10199 & "be interpreted as Duration!", Rop);
10200 Error_Msg_N
10201 ("\??precision will be lost in the conversion!", Rop);
10202 end if;
10204 elsif Is_Numeric_Type (Typ)
10205 and then Nkind (Operand) in N_Op
10206 and then Unique_Fixed_Point_Type (N) /= Any_Type
10207 then
10208 Set_Etype (Operand, Standard_Duration);
10210 else
10211 Error_Msg_N ("invalid context for mixed mode operation", N);
10212 Set_Etype (Operand, Any_Type);
10213 return;
10214 end if;
10215 end if;
10217 Resolve (Operand);
10219 -- In SPARK, a type conversion between array types should be restricted
10220 -- to types which have matching static bounds.
10222 -- Protect call to Matching_Static_Array_Bounds to avoid costly
10223 -- operation if not needed.
10225 if Restriction_Check_Required (SPARK_05)
10226 and then Is_Array_Type (Target_Typ)
10227 and then Is_Array_Type (Operand_Typ)
10228 and then Operand_Typ /= Any_Composite -- or else Operand in error
10229 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
10230 then
10231 Check_SPARK_Restriction
10232 ("array types should have matching static bounds", N);
10233 end if;
10235 -- In formal mode, the operand of an ancestor type conversion must be an
10236 -- object (not an expression).
10238 if Is_Tagged_Type (Target_Typ)
10239 and then not Is_Class_Wide_Type (Target_Typ)
10240 and then Is_Tagged_Type (Operand_Typ)
10241 and then not Is_Class_Wide_Type (Operand_Typ)
10242 and then Is_Ancestor (Target_Typ, Operand_Typ)
10243 and then not Is_SPARK_Object_Reference (Operand)
10244 then
10245 Check_SPARK_Restriction ("object required", Operand);
10246 end if;
10248 Analyze_Dimension (N);
10250 -- Note: we do the Eval_Type_Conversion call before applying the
10251 -- required checks for a subtype conversion. This is important, since
10252 -- both are prepared under certain circumstances to change the type
10253 -- conversion to a constraint error node, but in the case of
10254 -- Eval_Type_Conversion this may reflect an illegality in the static
10255 -- case, and we would miss the illegality (getting only a warning
10256 -- message), if we applied the type conversion checks first.
10258 Eval_Type_Conversion (N);
10260 -- Even when evaluation is not possible, we may be able to simplify the
10261 -- conversion or its expression. This needs to be done before applying
10262 -- checks, since otherwise the checks may use the original expression
10263 -- and defeat the simplifications. This is specifically the case for
10264 -- elimination of the floating-point Truncation attribute in
10265 -- float-to-int conversions.
10267 Simplify_Type_Conversion (N);
10269 -- If after evaluation we still have a type conversion, then we may need
10270 -- to apply checks required for a subtype conversion.
10272 -- Skip these type conversion checks if universal fixed operands
10273 -- operands involved, since range checks are handled separately for
10274 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
10276 if Nkind (N) = N_Type_Conversion
10277 and then not Is_Generic_Type (Root_Type (Target_Typ))
10278 and then Target_Typ /= Universal_Fixed
10279 and then Operand_Typ /= Universal_Fixed
10280 then
10281 Apply_Type_Conversion_Checks (N);
10282 end if;
10284 -- Issue warning for conversion of simple object to its own type. We
10285 -- have to test the original nodes, since they may have been rewritten
10286 -- by various optimizations.
10288 Orig_N := Original_Node (N);
10290 -- Here we test for a redundant conversion if the warning mode is
10291 -- active (and was not locally reset), and we have a type conversion
10292 -- from source not appearing in a generic instance.
10294 if Test_Redundant
10295 and then Nkind (Orig_N) = N_Type_Conversion
10296 and then Comes_From_Source (Orig_N)
10297 and then not In_Instance
10298 then
10299 Orig_N := Original_Node (Expression (Orig_N));
10300 Orig_T := Target_Typ;
10302 -- If the node is part of a larger expression, the Target_Type
10303 -- may not be the original type of the node if the context is a
10304 -- condition. Recover original type to see if conversion is needed.
10306 if Is_Boolean_Type (Orig_T)
10307 and then Nkind (Parent (N)) in N_Op
10308 then
10309 Orig_T := Etype (Parent (N));
10310 end if;
10312 -- If we have an entity name, then give the warning if the entity
10313 -- is the right type, or if it is a loop parameter covered by the
10314 -- original type (that's needed because loop parameters have an
10315 -- odd subtype coming from the bounds).
10317 if (Is_Entity_Name (Orig_N)
10318 and then
10319 (Etype (Entity (Orig_N)) = Orig_T
10320 or else
10321 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
10322 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
10324 -- If not an entity, then type of expression must match
10326 or else Etype (Orig_N) = Orig_T
10327 then
10328 -- One more check, do not give warning if the analyzed conversion
10329 -- has an expression with non-static bounds, and the bounds of the
10330 -- target are static. This avoids junk warnings in cases where the
10331 -- conversion is necessary to establish staticness, for example in
10332 -- a case statement.
10334 if not Is_OK_Static_Subtype (Operand_Typ)
10335 and then Is_OK_Static_Subtype (Target_Typ)
10336 then
10337 null;
10339 -- Finally, if this type conversion occurs in a context requiring
10340 -- a prefix, and the expression is a qualified expression then the
10341 -- type conversion is not redundant, since a qualified expression
10342 -- is not a prefix, whereas a type conversion is. For example, "X
10343 -- := T'(Funx(...)).Y;" is illegal because a selected component
10344 -- requires a prefix, but a type conversion makes it legal: "X :=
10345 -- T(T'(Funx(...))).Y;"
10347 -- In Ada 2012, a qualified expression is a name, so this idiom is
10348 -- no longer needed, but we still suppress the warning because it
10349 -- seems unfriendly for warnings to pop up when you switch to the
10350 -- newer language version.
10352 elsif Nkind (Orig_N) = N_Qualified_Expression
10353 and then Nkind_In (Parent (N), N_Attribute_Reference,
10354 N_Indexed_Component,
10355 N_Selected_Component,
10356 N_Slice,
10357 N_Explicit_Dereference)
10358 then
10359 null;
10361 -- Never warn on conversion to Long_Long_Integer'Base since
10362 -- that is most likely an artifact of the extended overflow
10363 -- checking and comes from complex expanded code.
10365 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
10366 null;
10368 -- Here we give the redundant conversion warning. If it is an
10369 -- entity, give the name of the entity in the message. If not,
10370 -- just mention the expression.
10372 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
10374 else
10375 if Is_Entity_Name (Orig_N) then
10376 Error_Msg_Node_2 := Orig_T;
10377 Error_Msg_NE -- CODEFIX
10378 ("??redundant conversion, & is of type &!",
10379 N, Entity (Orig_N));
10380 else
10381 Error_Msg_NE
10382 ("??redundant conversion, expression is of type&!",
10383 N, Orig_T);
10384 end if;
10385 end if;
10386 end if;
10387 end if;
10389 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
10390 -- No need to perform any interface conversion if the type of the
10391 -- expression coincides with the target type.
10393 if Ada_Version >= Ada_2005
10394 and then Expander_Active
10395 and then Operand_Typ /= Target_Typ
10396 then
10397 declare
10398 Opnd : Entity_Id := Operand_Typ;
10399 Target : Entity_Id := Target_Typ;
10401 begin
10402 -- If the type of the operand is a limited view, use the non-
10403 -- limited view when available.
10405 if From_Limited_With (Opnd)
10406 and then Ekind (Opnd) in Incomplete_Kind
10407 and then Present (Non_Limited_View (Opnd))
10408 then
10409 Opnd := Non_Limited_View (Opnd);
10410 Set_Etype (Expression (N), Opnd);
10411 end if;
10413 if Is_Access_Type (Opnd) then
10414 Opnd := Designated_Type (Opnd);
10415 end if;
10417 if Is_Access_Type (Target_Typ) then
10418 Target := Designated_Type (Target);
10419 end if;
10421 if Opnd = Target then
10422 null;
10424 -- Conversion from interface type
10426 elsif Is_Interface (Opnd) then
10428 -- Ada 2005 (AI-217): Handle entities from limited views
10430 if From_Limited_With (Opnd) then
10431 Error_Msg_Qual_Level := 99;
10432 Error_Msg_NE -- CODEFIX
10433 ("missing WITH clause on package &", N,
10434 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
10435 Error_Msg_N
10436 ("type conversions require visibility of the full view",
10439 elsif From_Limited_With (Target)
10440 and then not
10441 (Is_Access_Type (Target_Typ)
10442 and then Present (Non_Limited_View (Etype (Target))))
10443 then
10444 Error_Msg_Qual_Level := 99;
10445 Error_Msg_NE -- CODEFIX
10446 ("missing WITH clause on package &", N,
10447 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
10448 Error_Msg_N
10449 ("type conversions require visibility of the full view",
10452 else
10453 Expand_Interface_Conversion (N);
10454 end if;
10456 -- Conversion to interface type
10458 elsif Is_Interface (Target) then
10460 -- Handle subtypes
10462 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
10463 Opnd := Etype (Opnd);
10464 end if;
10466 if Is_Class_Wide_Type (Opnd)
10467 or else Interface_Present_In_Ancestor
10468 (Typ => Opnd,
10469 Iface => Target)
10470 then
10471 Expand_Interface_Conversion (N);
10472 else
10473 Error_Msg_Name_1 := Chars (Etype (Target));
10474 Error_Msg_Name_2 := Chars (Opnd);
10475 Error_Msg_N
10476 ("wrong interface conversion (% is not a progenitor "
10477 & "of %)", N);
10478 end if;
10479 end if;
10480 end;
10481 end if;
10483 -- Ada 2012: if target type has predicates, the result requires a
10484 -- predicate check. If the context is a call to another predicate
10485 -- check we must prevent infinite recursion.
10487 if Has_Predicates (Target_Typ) then
10488 if Nkind (Parent (N)) = N_Function_Call
10489 and then Present (Name (Parent (N)))
10490 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
10491 or else
10492 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
10493 then
10494 null;
10496 else
10497 Apply_Predicate_Check (N, Target_Typ);
10498 end if;
10499 end if;
10500 end Resolve_Type_Conversion;
10502 ----------------------
10503 -- Resolve_Unary_Op --
10504 ----------------------
10506 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
10507 B_Typ : constant Entity_Id := Base_Type (Typ);
10508 R : constant Node_Id := Right_Opnd (N);
10509 OK : Boolean;
10510 Lo : Uint;
10511 Hi : Uint;
10513 begin
10514 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
10515 Error_Msg_Name_1 := Chars (Typ);
10516 Check_SPARK_Restriction
10517 ("unary operator not defined for modular type%", N);
10518 end if;
10520 -- Deal with intrinsic unary operators
10522 if Comes_From_Source (N)
10523 and then Ekind (Entity (N)) = E_Function
10524 and then Is_Imported (Entity (N))
10525 and then Is_Intrinsic_Subprogram (Entity (N))
10526 then
10527 Resolve_Intrinsic_Unary_Operator (N, Typ);
10528 return;
10529 end if;
10531 -- Deal with universal cases
10533 if Etype (R) = Universal_Integer
10534 or else
10535 Etype (R) = Universal_Real
10536 then
10537 Check_For_Visible_Operator (N, B_Typ);
10538 end if;
10540 Set_Etype (N, B_Typ);
10541 Resolve (R, B_Typ);
10543 -- Generate warning for expressions like abs (x mod 2)
10545 if Warn_On_Redundant_Constructs
10546 and then Nkind (N) = N_Op_Abs
10547 then
10548 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
10550 if OK and then Hi >= Lo and then Lo >= 0 then
10551 Error_Msg_N -- CODEFIX
10552 ("?r?abs applied to known non-negative value has no effect", N);
10553 end if;
10554 end if;
10556 -- Deal with reference generation
10558 Check_Unset_Reference (R);
10559 Generate_Operator_Reference (N, B_Typ);
10560 Analyze_Dimension (N);
10561 Eval_Unary_Op (N);
10563 -- Set overflow checking bit. Much cleverer code needed here eventually
10564 -- and perhaps the Resolve routines should be separated for the various
10565 -- arithmetic operations, since they will need different processing ???
10567 if Nkind (N) in N_Op then
10568 if not Overflow_Checks_Suppressed (Etype (N)) then
10569 Enable_Overflow_Check (N);
10570 end if;
10571 end if;
10573 -- Generate warning for expressions like -5 mod 3 for integers. No need
10574 -- to worry in the floating-point case, since parens do not affect the
10575 -- result so there is no point in giving in a warning.
10577 declare
10578 Norig : constant Node_Id := Original_Node (N);
10579 Rorig : Node_Id;
10580 Val : Uint;
10581 HB : Uint;
10582 LB : Uint;
10583 Lval : Uint;
10584 Opnd : Node_Id;
10586 begin
10587 if Warn_On_Questionable_Missing_Parens
10588 and then Comes_From_Source (Norig)
10589 and then Is_Integer_Type (Typ)
10590 and then Nkind (Norig) = N_Op_Minus
10591 then
10592 Rorig := Original_Node (Right_Opnd (Norig));
10594 -- We are looking for cases where the right operand is not
10595 -- parenthesized, and is a binary operator, multiply, divide, or
10596 -- mod. These are the cases where the grouping can affect results.
10598 if Paren_Count (Rorig) = 0
10599 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
10600 then
10601 -- For mod, we always give the warning, since the value is
10602 -- affected by the parenthesization (e.g. (-5) mod 315 /=
10603 -- -(5 mod 315)). But for the other cases, the only concern is
10604 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
10605 -- overflows, but (-2) * 64 does not). So we try to give the
10606 -- message only when overflow is possible.
10608 if Nkind (Rorig) /= N_Op_Mod
10609 and then Compile_Time_Known_Value (R)
10610 then
10611 Val := Expr_Value (R);
10613 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
10614 HB := Expr_Value (Type_High_Bound (Typ));
10615 else
10616 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
10617 end if;
10619 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
10620 LB := Expr_Value (Type_Low_Bound (Typ));
10621 else
10622 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
10623 end if;
10625 -- Note that the test below is deliberately excluding the
10626 -- largest negative number, since that is a potentially
10627 -- troublesome case (e.g. -2 * x, where the result is the
10628 -- largest negative integer has an overflow with 2 * x).
10630 if Val > LB and then Val <= HB then
10631 return;
10632 end if;
10633 end if;
10635 -- For the multiplication case, the only case we have to worry
10636 -- about is when (-a)*b is exactly the largest negative number
10637 -- so that -(a*b) can cause overflow. This can only happen if
10638 -- a is a power of 2, and more generally if any operand is a
10639 -- constant that is not a power of 2, then the parentheses
10640 -- cannot affect whether overflow occurs. We only bother to
10641 -- test the left most operand
10643 -- Loop looking at left operands for one that has known value
10645 Opnd := Rorig;
10646 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
10647 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
10648 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
10650 -- Operand value of 0 or 1 skips warning
10652 if Lval <= 1 then
10653 return;
10655 -- Otherwise check power of 2, if power of 2, warn, if
10656 -- anything else, skip warning.
10658 else
10659 while Lval /= 2 loop
10660 if Lval mod 2 = 1 then
10661 return;
10662 else
10663 Lval := Lval / 2;
10664 end if;
10665 end loop;
10667 exit Opnd_Loop;
10668 end if;
10669 end if;
10671 -- Keep looking at left operands
10673 Opnd := Left_Opnd (Opnd);
10674 end loop Opnd_Loop;
10676 -- For rem or "/" we can only have a problematic situation
10677 -- if the divisor has a value of minus one or one. Otherwise
10678 -- overflow is impossible (divisor > 1) or we have a case of
10679 -- division by zero in any case.
10681 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
10682 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
10683 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
10684 then
10685 return;
10686 end if;
10688 -- If we fall through warning should be issued
10690 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
10692 Error_Msg_N
10693 ("??unary minus expression should be parenthesized here!", N);
10694 end if;
10695 end if;
10696 end;
10697 end Resolve_Unary_Op;
10699 ----------------------------------
10700 -- Resolve_Unchecked_Expression --
10701 ----------------------------------
10703 procedure Resolve_Unchecked_Expression
10704 (N : Node_Id;
10705 Typ : Entity_Id)
10707 begin
10708 Resolve (Expression (N), Typ, Suppress => All_Checks);
10709 Set_Etype (N, Typ);
10710 end Resolve_Unchecked_Expression;
10712 ---------------------------------------
10713 -- Resolve_Unchecked_Type_Conversion --
10714 ---------------------------------------
10716 procedure Resolve_Unchecked_Type_Conversion
10717 (N : Node_Id;
10718 Typ : Entity_Id)
10720 pragma Warnings (Off, Typ);
10722 Operand : constant Node_Id := Expression (N);
10723 Opnd_Type : constant Entity_Id := Etype (Operand);
10725 begin
10726 -- Resolve operand using its own type
10728 Resolve (Operand, Opnd_Type);
10730 -- In an inlined context, the unchecked conversion may be applied
10731 -- to a literal, in which case its type is the type of the context.
10732 -- (In other contexts conversions cannot apply to literals).
10734 if In_Inlined_Body
10735 and then (Opnd_Type = Any_Character or else
10736 Opnd_Type = Any_Integer or else
10737 Opnd_Type = Any_Real)
10738 then
10739 Set_Etype (Operand, Typ);
10740 end if;
10742 Analyze_Dimension (N);
10743 Eval_Unchecked_Conversion (N);
10744 end Resolve_Unchecked_Type_Conversion;
10746 ------------------------------
10747 -- Rewrite_Operator_As_Call --
10748 ------------------------------
10750 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
10751 Loc : constant Source_Ptr := Sloc (N);
10752 Actuals : constant List_Id := New_List;
10753 New_N : Node_Id;
10755 begin
10756 if Nkind (N) in N_Binary_Op then
10757 Append (Left_Opnd (N), Actuals);
10758 end if;
10760 Append (Right_Opnd (N), Actuals);
10762 New_N :=
10763 Make_Function_Call (Sloc => Loc,
10764 Name => New_Occurrence_Of (Nam, Loc),
10765 Parameter_Associations => Actuals);
10767 Preserve_Comes_From_Source (New_N, N);
10768 Preserve_Comes_From_Source (Name (New_N), N);
10769 Rewrite (N, New_N);
10770 Set_Etype (N, Etype (Nam));
10771 end Rewrite_Operator_As_Call;
10773 ------------------------------
10774 -- Rewrite_Renamed_Operator --
10775 ------------------------------
10777 procedure Rewrite_Renamed_Operator
10778 (N : Node_Id;
10779 Op : Entity_Id;
10780 Typ : Entity_Id)
10782 Nam : constant Name_Id := Chars (Op);
10783 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
10784 Op_Node : Node_Id;
10786 begin
10787 -- Do not perform this transformation within a pre/postcondition,
10788 -- because the expression will be re-analyzed, and the transformation
10789 -- might affect the visibility of the operator, e.g. in an instance.
10791 if In_Assertion_Expr > 0 then
10792 return;
10793 end if;
10795 -- Rewrite the operator node using the real operator, not its renaming.
10796 -- Exclude user-defined intrinsic operations of the same name, which are
10797 -- treated separately and rewritten as calls.
10799 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
10800 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
10801 Set_Chars (Op_Node, Nam);
10802 Set_Etype (Op_Node, Etype (N));
10803 Set_Entity (Op_Node, Op);
10804 Set_Right_Opnd (Op_Node, Right_Opnd (N));
10806 -- Indicate that both the original entity and its renaming are
10807 -- referenced at this point.
10809 Generate_Reference (Entity (N), N);
10810 Generate_Reference (Op, N);
10812 if Is_Binary then
10813 Set_Left_Opnd (Op_Node, Left_Opnd (N));
10814 end if;
10816 Rewrite (N, Op_Node);
10818 -- If the context type is private, add the appropriate conversions so
10819 -- that the operator is applied to the full view. This is done in the
10820 -- routines that resolve intrinsic operators.
10822 if Is_Intrinsic_Subprogram (Op)
10823 and then Is_Private_Type (Typ)
10824 then
10825 case Nkind (N) is
10826 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
10827 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
10828 Resolve_Intrinsic_Operator (N, Typ);
10830 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
10831 Resolve_Intrinsic_Unary_Operator (N, Typ);
10833 when others =>
10834 Resolve (N, Typ);
10835 end case;
10836 end if;
10838 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
10840 -- Operator renames a user-defined operator of the same name. Use the
10841 -- original operator in the node, which is the one Gigi knows about.
10843 Set_Entity (N, Op);
10844 Set_Is_Overloaded (N, False);
10845 end if;
10846 end Rewrite_Renamed_Operator;
10848 -----------------------
10849 -- Set_Slice_Subtype --
10850 -----------------------
10852 -- Build an implicit subtype declaration to represent the type delivered by
10853 -- the slice. This is an abbreviated version of an array subtype. We define
10854 -- an index subtype for the slice, using either the subtype name or the
10855 -- discrete range of the slice. To be consistent with index usage elsewhere
10856 -- we create a list header to hold the single index. This list is not
10857 -- otherwise attached to the syntax tree.
10859 procedure Set_Slice_Subtype (N : Node_Id) is
10860 Loc : constant Source_Ptr := Sloc (N);
10861 Index_List : constant List_Id := New_List;
10862 Index : Node_Id;
10863 Index_Subtype : Entity_Id;
10864 Index_Type : Entity_Id;
10865 Slice_Subtype : Entity_Id;
10866 Drange : constant Node_Id := Discrete_Range (N);
10868 begin
10869 Index_Type := Base_Type (Etype (Drange));
10871 if Is_Entity_Name (Drange) then
10872 Index_Subtype := Entity (Drange);
10874 else
10875 -- We force the evaluation of a range. This is definitely needed in
10876 -- the renamed case, and seems safer to do unconditionally. Note in
10877 -- any case that since we will create and insert an Itype referring
10878 -- to this range, we must make sure any side effect removal actions
10879 -- are inserted before the Itype definition.
10881 if Nkind (Drange) = N_Range then
10882 Force_Evaluation (Low_Bound (Drange));
10883 Force_Evaluation (High_Bound (Drange));
10885 -- If the discrete range is given by a subtype indication, the
10886 -- type of the slice is the base of the subtype mark.
10888 elsif Nkind (Drange) = N_Subtype_Indication then
10889 declare
10890 R : constant Node_Id := Range_Expression (Constraint (Drange));
10891 begin
10892 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
10893 Force_Evaluation (Low_Bound (R));
10894 Force_Evaluation (High_Bound (R));
10895 end;
10896 end if;
10898 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10900 -- Take a new copy of Drange (where bounds have been rewritten to
10901 -- reference side-effect-free names). Using a separate tree ensures
10902 -- that further expansion (e.g. while rewriting a slice assignment
10903 -- into a FOR loop) does not attempt to remove side effects on the
10904 -- bounds again (which would cause the bounds in the index subtype
10905 -- definition to refer to temporaries before they are defined) (the
10906 -- reason is that some names are considered side effect free here
10907 -- for the subtype, but not in the context of a loop iteration
10908 -- scheme).
10910 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
10911 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
10912 Set_Etype (Index_Subtype, Index_Type);
10913 Set_Size_Info (Index_Subtype, Index_Type);
10914 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10915 end if;
10917 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
10919 Index := New_Occurrence_Of (Index_Subtype, Loc);
10920 Set_Etype (Index, Index_Subtype);
10921 Append (Index, Index_List);
10923 Set_First_Index (Slice_Subtype, Index);
10924 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
10925 Set_Is_Constrained (Slice_Subtype, True);
10927 Check_Compile_Time_Size (Slice_Subtype);
10929 -- The Etype of the existing Slice node is reset to this slice subtype.
10930 -- Its bounds are obtained from its first index.
10932 Set_Etype (N, Slice_Subtype);
10934 -- For packed slice subtypes, freeze immediately (except in the case of
10935 -- being in a "spec expression" where we never freeze when we first see
10936 -- the expression).
10938 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
10939 Freeze_Itype (Slice_Subtype, N);
10941 -- For all other cases insert an itype reference in the slice's actions
10942 -- so that the itype is frozen at the proper place in the tree (i.e. at
10943 -- the point where actions for the slice are analyzed). Note that this
10944 -- is different from freezing the itype immediately, which might be
10945 -- premature (e.g. if the slice is within a transient scope). This needs
10946 -- to be done only if expansion is enabled.
10948 elsif Expander_Active then
10949 Ensure_Defined (Typ => Slice_Subtype, N => N);
10950 end if;
10951 end Set_Slice_Subtype;
10953 --------------------------------
10954 -- Set_String_Literal_Subtype --
10955 --------------------------------
10957 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
10958 Loc : constant Source_Ptr := Sloc (N);
10959 Low_Bound : constant Node_Id :=
10960 Type_Low_Bound (Etype (First_Index (Typ)));
10961 Subtype_Id : Entity_Id;
10963 begin
10964 if Nkind (N) /= N_String_Literal then
10965 return;
10966 end if;
10968 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
10969 Set_String_Literal_Length (Subtype_Id, UI_From_Int
10970 (String_Length (Strval (N))));
10971 Set_Etype (Subtype_Id, Base_Type (Typ));
10972 Set_Is_Constrained (Subtype_Id);
10973 Set_Etype (N, Subtype_Id);
10975 -- The low bound is set from the low bound of the corresponding index
10976 -- type. Note that we do not store the high bound in the string literal
10977 -- subtype, but it can be deduced if necessary from the length and the
10978 -- low bound.
10980 if Is_OK_Static_Expression (Low_Bound) then
10981 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
10983 -- If the lower bound is not static we create a range for the string
10984 -- literal, using the index type and the known length of the literal.
10985 -- The index type is not necessarily Positive, so the upper bound is
10986 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
10988 else
10989 declare
10990 Index_List : constant List_Id := New_List;
10991 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
10992 High_Bound : constant Node_Id :=
10993 Make_Attribute_Reference (Loc,
10994 Attribute_Name => Name_Val,
10995 Prefix =>
10996 New_Occurrence_Of (Index_Type, Loc),
10997 Expressions => New_List (
10998 Make_Op_Add (Loc,
10999 Left_Opnd =>
11000 Make_Attribute_Reference (Loc,
11001 Attribute_Name => Name_Pos,
11002 Prefix =>
11003 New_Occurrence_Of (Index_Type, Loc),
11004 Expressions =>
11005 New_List (New_Copy_Tree (Low_Bound))),
11006 Right_Opnd =>
11007 Make_Integer_Literal (Loc,
11008 String_Length (Strval (N)) - 1))));
11010 Array_Subtype : Entity_Id;
11011 Drange : Node_Id;
11012 Index : Node_Id;
11013 Index_Subtype : Entity_Id;
11015 begin
11016 if Is_Integer_Type (Index_Type) then
11017 Set_String_Literal_Low_Bound
11018 (Subtype_Id, Make_Integer_Literal (Loc, 1));
11020 else
11021 -- If the index type is an enumeration type, build bounds
11022 -- expression with attributes.
11024 Set_String_Literal_Low_Bound
11025 (Subtype_Id,
11026 Make_Attribute_Reference (Loc,
11027 Attribute_Name => Name_First,
11028 Prefix =>
11029 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
11030 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
11031 end if;
11033 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
11035 -- Build bona fide subtype for the string, and wrap it in an
11036 -- unchecked conversion, because the backend expects the
11037 -- String_Literal_Subtype to have a static lower bound.
11039 Index_Subtype :=
11040 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
11041 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
11042 Set_Scalar_Range (Index_Subtype, Drange);
11043 Set_Parent (Drange, N);
11044 Analyze_And_Resolve (Drange, Index_Type);
11046 -- In the context, the Index_Type may already have a constraint,
11047 -- so use common base type on string subtype. The base type may
11048 -- be used when generating attributes of the string, for example
11049 -- in the context of a slice assignment.
11051 Set_Etype (Index_Subtype, Base_Type (Index_Type));
11052 Set_Size_Info (Index_Subtype, Index_Type);
11053 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
11055 Array_Subtype := Create_Itype (E_Array_Subtype, N);
11057 Index := New_Occurrence_Of (Index_Subtype, Loc);
11058 Set_Etype (Index, Index_Subtype);
11059 Append (Index, Index_List);
11061 Set_First_Index (Array_Subtype, Index);
11062 Set_Etype (Array_Subtype, Base_Type (Typ));
11063 Set_Is_Constrained (Array_Subtype, True);
11065 Rewrite (N,
11066 Make_Unchecked_Type_Conversion (Loc,
11067 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
11068 Expression => Relocate_Node (N)));
11069 Set_Etype (N, Array_Subtype);
11070 end;
11071 end if;
11072 end Set_String_Literal_Subtype;
11074 ------------------------------
11075 -- Simplify_Type_Conversion --
11076 ------------------------------
11078 procedure Simplify_Type_Conversion (N : Node_Id) is
11079 begin
11080 if Nkind (N) = N_Type_Conversion then
11081 declare
11082 Operand : constant Node_Id := Expression (N);
11083 Target_Typ : constant Entity_Id := Etype (N);
11084 Opnd_Typ : constant Entity_Id := Etype (Operand);
11086 begin
11087 -- Special processing if the conversion is the expression of a
11088 -- Rounding or Truncation attribute reference. In this case we
11089 -- replace:
11091 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
11093 -- by
11095 -- ityp (x)
11097 -- with the Float_Truncate flag set to False or True respectively,
11098 -- which is more efficient.
11100 if Is_Floating_Point_Type (Opnd_Typ)
11101 and then
11102 (Is_Integer_Type (Target_Typ)
11103 or else (Is_Fixed_Point_Type (Target_Typ)
11104 and then Conversion_OK (N)))
11105 and then Nkind (Operand) = N_Attribute_Reference
11106 and then Nam_In (Attribute_Name (Operand), Name_Rounding,
11107 Name_Truncation)
11108 then
11109 declare
11110 Truncate : constant Boolean :=
11111 Attribute_Name (Operand) = Name_Truncation;
11112 begin
11113 Rewrite (Operand,
11114 Relocate_Node (First (Expressions (Operand))));
11115 Set_Float_Truncate (N, Truncate);
11116 end;
11117 end if;
11118 end;
11119 end if;
11120 end Simplify_Type_Conversion;
11122 -----------------------------
11123 -- Unique_Fixed_Point_Type --
11124 -----------------------------
11126 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
11127 T1 : Entity_Id := Empty;
11128 T2 : Entity_Id;
11129 Item : Node_Id;
11130 Scop : Entity_Id;
11132 procedure Fixed_Point_Error;
11133 -- Give error messages for true ambiguity. Messages are posted on node
11134 -- N, and entities T1, T2 are the possible interpretations.
11136 -----------------------
11137 -- Fixed_Point_Error --
11138 -----------------------
11140 procedure Fixed_Point_Error is
11141 begin
11142 Error_Msg_N ("ambiguous universal_fixed_expression", N);
11143 Error_Msg_NE ("\\possible interpretation as}", N, T1);
11144 Error_Msg_NE ("\\possible interpretation as}", N, T2);
11145 end Fixed_Point_Error;
11147 -- Start of processing for Unique_Fixed_Point_Type
11149 begin
11150 -- The operations on Duration are visible, so Duration is always a
11151 -- possible interpretation.
11153 T1 := Standard_Duration;
11155 -- Look for fixed-point types in enclosing scopes
11157 Scop := Current_Scope;
11158 while Scop /= Standard_Standard loop
11159 T2 := First_Entity (Scop);
11160 while Present (T2) loop
11161 if Is_Fixed_Point_Type (T2)
11162 and then Current_Entity (T2) = T2
11163 and then Scope (Base_Type (T2)) = Scop
11164 then
11165 if Present (T1) then
11166 Fixed_Point_Error;
11167 return Any_Type;
11168 else
11169 T1 := T2;
11170 end if;
11171 end if;
11173 Next_Entity (T2);
11174 end loop;
11176 Scop := Scope (Scop);
11177 end loop;
11179 -- Look for visible fixed type declarations in the context
11181 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
11182 while Present (Item) loop
11183 if Nkind (Item) = N_With_Clause then
11184 Scop := Entity (Name (Item));
11185 T2 := First_Entity (Scop);
11186 while Present (T2) loop
11187 if Is_Fixed_Point_Type (T2)
11188 and then Scope (Base_Type (T2)) = Scop
11189 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
11190 then
11191 if Present (T1) then
11192 Fixed_Point_Error;
11193 return Any_Type;
11194 else
11195 T1 := T2;
11196 end if;
11197 end if;
11199 Next_Entity (T2);
11200 end loop;
11201 end if;
11203 Next (Item);
11204 end loop;
11206 if Nkind (N) = N_Real_Literal then
11207 Error_Msg_NE
11208 ("??real literal interpreted as }!", N, T1);
11209 else
11210 Error_Msg_NE
11211 ("??universal_fixed expression interpreted as }!", N, T1);
11212 end if;
11214 return T1;
11215 end Unique_Fixed_Point_Type;
11217 ----------------------
11218 -- Valid_Conversion --
11219 ----------------------
11221 function Valid_Conversion
11222 (N : Node_Id;
11223 Target : Entity_Id;
11224 Operand : Node_Id;
11225 Report_Errs : Boolean := True) return Boolean
11227 Target_Type : constant Entity_Id := Base_Type (Target);
11228 Opnd_Type : Entity_Id := Etype (Operand);
11229 Inc_Ancestor : Entity_Id;
11231 function Conversion_Check
11232 (Valid : Boolean;
11233 Msg : String) return Boolean;
11234 -- Little routine to post Msg if Valid is False, returns Valid value
11236 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
11237 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
11239 procedure Conversion_Error_NE
11240 (Msg : String;
11241 N : Node_Or_Entity_Id;
11242 E : Node_Or_Entity_Id);
11243 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
11245 function Valid_Tagged_Conversion
11246 (Target_Type : Entity_Id;
11247 Opnd_Type : Entity_Id) return Boolean;
11248 -- Specifically test for validity of tagged conversions
11250 function Valid_Array_Conversion return Boolean;
11251 -- Check index and component conformance, and accessibility levels if
11252 -- the component types are anonymous access types (Ada 2005).
11254 ----------------------
11255 -- Conversion_Check --
11256 ----------------------
11258 function Conversion_Check
11259 (Valid : Boolean;
11260 Msg : String) return Boolean
11262 begin
11263 if not Valid
11265 -- A generic unit has already been analyzed and we have verified
11266 -- that a particular conversion is OK in that context. Since the
11267 -- instance is reanalyzed without relying on the relationships
11268 -- established during the analysis of the generic, it is possible
11269 -- to end up with inconsistent views of private types. Do not emit
11270 -- the error message in such cases. The rest of the machinery in
11271 -- Valid_Conversion still ensures the proper compatibility of
11272 -- target and operand types.
11274 and then not In_Instance
11275 then
11276 Conversion_Error_N (Msg, Operand);
11277 end if;
11279 return Valid;
11280 end Conversion_Check;
11282 ------------------------
11283 -- Conversion_Error_N --
11284 ------------------------
11286 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
11287 begin
11288 if Report_Errs then
11289 Error_Msg_N (Msg, N);
11290 end if;
11291 end Conversion_Error_N;
11293 -------------------------
11294 -- Conversion_Error_NE --
11295 -------------------------
11297 procedure Conversion_Error_NE
11298 (Msg : String;
11299 N : Node_Or_Entity_Id;
11300 E : Node_Or_Entity_Id)
11302 begin
11303 if Report_Errs then
11304 Error_Msg_NE (Msg, N, E);
11305 end if;
11306 end Conversion_Error_NE;
11308 ----------------------------
11309 -- Valid_Array_Conversion --
11310 ----------------------------
11312 function Valid_Array_Conversion return Boolean
11314 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
11315 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
11317 Opnd_Index : Node_Id;
11318 Opnd_Index_Type : Entity_Id;
11320 Target_Comp_Type : constant Entity_Id :=
11321 Component_Type (Target_Type);
11322 Target_Comp_Base : constant Entity_Id :=
11323 Base_Type (Target_Comp_Type);
11325 Target_Index : Node_Id;
11326 Target_Index_Type : Entity_Id;
11328 begin
11329 -- Error if wrong number of dimensions
11332 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
11333 then
11334 Conversion_Error_N
11335 ("incompatible number of dimensions for conversion", Operand);
11336 return False;
11338 -- Number of dimensions matches
11340 else
11341 -- Loop through indexes of the two arrays
11343 Target_Index := First_Index (Target_Type);
11344 Opnd_Index := First_Index (Opnd_Type);
11345 while Present (Target_Index) and then Present (Opnd_Index) loop
11346 Target_Index_Type := Etype (Target_Index);
11347 Opnd_Index_Type := Etype (Opnd_Index);
11349 -- Error if index types are incompatible
11351 if not (Is_Integer_Type (Target_Index_Type)
11352 and then Is_Integer_Type (Opnd_Index_Type))
11353 and then (Root_Type (Target_Index_Type)
11354 /= Root_Type (Opnd_Index_Type))
11355 then
11356 Conversion_Error_N
11357 ("incompatible index types for array conversion",
11358 Operand);
11359 return False;
11360 end if;
11362 Next_Index (Target_Index);
11363 Next_Index (Opnd_Index);
11364 end loop;
11366 -- If component types have same base type, all set
11368 if Target_Comp_Base = Opnd_Comp_Base then
11369 null;
11371 -- Here if base types of components are not the same. The only
11372 -- time this is allowed is if we have anonymous access types.
11374 -- The conversion of arrays of anonymous access types can lead
11375 -- to dangling pointers. AI-392 formalizes the accessibility
11376 -- checks that must be applied to such conversions to prevent
11377 -- out-of-scope references.
11379 elsif Ekind_In
11380 (Target_Comp_Base, E_Anonymous_Access_Type,
11381 E_Anonymous_Access_Subprogram_Type)
11382 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
11383 and then
11384 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
11385 then
11386 if Type_Access_Level (Target_Type) <
11387 Deepest_Type_Access_Level (Opnd_Type)
11388 then
11389 if In_Instance_Body then
11390 Error_Msg_Warn := SPARK_Mode /= On;
11391 Conversion_Error_N
11392 ("source array type has deeper accessibility "
11393 & "level than target<<", Operand);
11394 Conversion_Error_N ("\Program_Error [<<", Operand);
11395 Rewrite (N,
11396 Make_Raise_Program_Error (Sloc (N),
11397 Reason => PE_Accessibility_Check_Failed));
11398 Set_Etype (N, Target_Type);
11399 return False;
11401 -- Conversion not allowed because of accessibility levels
11403 else
11404 Conversion_Error_N
11405 ("source array type has deeper accessibility "
11406 & "level than target", Operand);
11407 return False;
11408 end if;
11410 else
11411 null;
11412 end if;
11414 -- All other cases where component base types do not match
11416 else
11417 Conversion_Error_N
11418 ("incompatible component types for array conversion",
11419 Operand);
11420 return False;
11421 end if;
11423 -- Check that component subtypes statically match. For numeric
11424 -- types this means that both must be either constrained or
11425 -- unconstrained. For enumeration types the bounds must match.
11426 -- All of this is checked in Subtypes_Statically_Match.
11428 if not Subtypes_Statically_Match
11429 (Target_Comp_Type, Opnd_Comp_Type)
11430 then
11431 Conversion_Error_N
11432 ("component subtypes must statically match", Operand);
11433 return False;
11434 end if;
11435 end if;
11437 return True;
11438 end Valid_Array_Conversion;
11440 -----------------------------
11441 -- Valid_Tagged_Conversion --
11442 -----------------------------
11444 function Valid_Tagged_Conversion
11445 (Target_Type : Entity_Id;
11446 Opnd_Type : Entity_Id) return Boolean
11448 begin
11449 -- Upward conversions are allowed (RM 4.6(22))
11451 if Covers (Target_Type, Opnd_Type)
11452 or else Is_Ancestor (Target_Type, Opnd_Type)
11453 then
11454 return True;
11456 -- Downward conversion are allowed if the operand is class-wide
11457 -- (RM 4.6(23)).
11459 elsif Is_Class_Wide_Type (Opnd_Type)
11460 and then Covers (Opnd_Type, Target_Type)
11461 then
11462 return True;
11464 elsif Covers (Opnd_Type, Target_Type)
11465 or else Is_Ancestor (Opnd_Type, Target_Type)
11466 then
11467 return
11468 Conversion_Check (False,
11469 "downward conversion of tagged objects not allowed");
11471 -- Ada 2005 (AI-251): The conversion to/from interface types is
11472 -- always valid
11474 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
11475 return True;
11477 -- If the operand is a class-wide type obtained through a limited_
11478 -- with clause, and the context includes the non-limited view, use
11479 -- it to determine whether the conversion is legal.
11481 elsif Is_Class_Wide_Type (Opnd_Type)
11482 and then From_Limited_With (Opnd_Type)
11483 and then Present (Non_Limited_View (Etype (Opnd_Type)))
11484 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
11485 then
11486 return True;
11488 elsif Is_Access_Type (Opnd_Type)
11489 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
11490 then
11491 return True;
11493 else
11494 Conversion_Error_NE
11495 ("invalid tagged conversion, not compatible with}",
11496 N, First_Subtype (Opnd_Type));
11497 return False;
11498 end if;
11499 end Valid_Tagged_Conversion;
11501 -- Start of processing for Valid_Conversion
11503 begin
11504 Check_Parameterless_Call (Operand);
11506 if Is_Overloaded (Operand) then
11507 declare
11508 I : Interp_Index;
11509 I1 : Interp_Index;
11510 It : Interp;
11511 It1 : Interp;
11512 N1 : Entity_Id;
11513 T1 : Entity_Id;
11515 begin
11516 -- Remove procedure calls, which syntactically cannot appear in
11517 -- this context, but which cannot be removed by type checking,
11518 -- because the context does not impose a type.
11520 -- The node may be labelled overloaded, but still contain only one
11521 -- interpretation because others were discarded earlier. If this
11522 -- is the case, retain the single interpretation if legal.
11524 Get_First_Interp (Operand, I, It);
11525 Opnd_Type := It.Typ;
11526 Get_Next_Interp (I, It);
11528 if Present (It.Typ)
11529 and then Opnd_Type /= Standard_Void_Type
11530 then
11531 -- More than one candidate interpretation is available
11533 Get_First_Interp (Operand, I, It);
11534 while Present (It.Typ) loop
11535 if It.Typ = Standard_Void_Type then
11536 Remove_Interp (I);
11537 end if;
11539 -- When compiling for a system where Address is of a visible
11540 -- integer type, spurious ambiguities can be produced when
11541 -- arithmetic operations have a literal operand and return
11542 -- System.Address or a descendant of it. These ambiguities
11543 -- are usually resolved by the context, but for conversions
11544 -- there is no context type and the removal of the spurious
11545 -- operations must be done explicitly here.
11547 if not Address_Is_Private
11548 and then Is_Descendent_Of_Address (It.Typ)
11549 then
11550 Remove_Interp (I);
11551 end if;
11553 Get_Next_Interp (I, It);
11554 end loop;
11555 end if;
11557 Get_First_Interp (Operand, I, It);
11558 I1 := I;
11559 It1 := It;
11561 if No (It.Typ) then
11562 Conversion_Error_N ("illegal operand in conversion", Operand);
11563 return False;
11564 end if;
11566 Get_Next_Interp (I, It);
11568 if Present (It.Typ) then
11569 N1 := It1.Nam;
11570 T1 := It1.Typ;
11571 It1 := Disambiguate (Operand, I1, I, Any_Type);
11573 if It1 = No_Interp then
11574 Conversion_Error_N
11575 ("ambiguous operand in conversion", Operand);
11577 -- If the interpretation involves a standard operator, use
11578 -- the location of the type, which may be user-defined.
11580 if Sloc (It.Nam) = Standard_Location then
11581 Error_Msg_Sloc := Sloc (It.Typ);
11582 else
11583 Error_Msg_Sloc := Sloc (It.Nam);
11584 end if;
11586 Conversion_Error_N -- CODEFIX
11587 ("\\possible interpretation#!", Operand);
11589 if Sloc (N1) = Standard_Location then
11590 Error_Msg_Sloc := Sloc (T1);
11591 else
11592 Error_Msg_Sloc := Sloc (N1);
11593 end if;
11595 Conversion_Error_N -- CODEFIX
11596 ("\\possible interpretation#!", Operand);
11598 return False;
11599 end if;
11600 end if;
11602 Set_Etype (Operand, It1.Typ);
11603 Opnd_Type := It1.Typ;
11604 end;
11605 end if;
11607 -- Deal with conversion of integer type to address if the pragma
11608 -- Allow_Integer_Address is in effect. We convert the conversion to
11609 -- an unchecked conversion in this case and we are all done.
11611 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
11612 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
11613 Analyze_And_Resolve (N, Target_Type);
11614 return True;
11615 end if;
11617 -- If we are within a child unit, check whether the type of the
11618 -- expression has an ancestor in a parent unit, in which case it
11619 -- belongs to its derivation class even if the ancestor is private.
11620 -- See RM 7.3.1 (5.2/3).
11622 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
11624 -- Numeric types
11626 if Is_Numeric_Type (Target_Type) then
11628 -- A universal fixed expression can be converted to any numeric type
11630 if Opnd_Type = Universal_Fixed then
11631 return True;
11633 -- Also no need to check when in an instance or inlined body, because
11634 -- the legality has been established when the template was analyzed.
11635 -- Furthermore, numeric conversions may occur where only a private
11636 -- view of the operand type is visible at the instantiation point.
11637 -- This results in a spurious error if we check that the operand type
11638 -- is a numeric type.
11640 -- Note: in a previous version of this unit, the following tests were
11641 -- applied only for generated code (Comes_From_Source set to False),
11642 -- but in fact the test is required for source code as well, since
11643 -- this situation can arise in source code.
11645 elsif In_Instance or else In_Inlined_Body then
11646 return True;
11648 -- Otherwise we need the conversion check
11650 else
11651 return Conversion_Check
11652 (Is_Numeric_Type (Opnd_Type)
11653 or else
11654 (Present (Inc_Ancestor)
11655 and then Is_Numeric_Type (Inc_Ancestor)),
11656 "illegal operand for numeric conversion");
11657 end if;
11659 -- Array types
11661 elsif Is_Array_Type (Target_Type) then
11662 if not Is_Array_Type (Opnd_Type)
11663 or else Opnd_Type = Any_Composite
11664 or else Opnd_Type = Any_String
11665 then
11666 Conversion_Error_N
11667 ("illegal operand for array conversion", Operand);
11668 return False;
11670 else
11671 return Valid_Array_Conversion;
11672 end if;
11674 -- Ada 2005 (AI-251): Anonymous access types where target references an
11675 -- interface type.
11677 elsif Ekind_In (Target_Type, E_General_Access_Type,
11678 E_Anonymous_Access_Type)
11679 and then Is_Interface (Directly_Designated_Type (Target_Type))
11680 then
11681 -- Check the static accessibility rule of 4.6(17). Note that the
11682 -- check is not enforced when within an instance body, since the
11683 -- RM requires such cases to be caught at run time.
11685 -- If the operand is a rewriting of an allocator no check is needed
11686 -- because there are no accessibility issues.
11688 if Nkind (Original_Node (N)) = N_Allocator then
11689 null;
11691 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
11692 if Type_Access_Level (Opnd_Type) >
11693 Deepest_Type_Access_Level (Target_Type)
11694 then
11695 -- In an instance, this is a run-time check, but one we know
11696 -- will fail, so generate an appropriate warning. The raise
11697 -- will be generated by Expand_N_Type_Conversion.
11699 if In_Instance_Body then
11700 Error_Msg_Warn := SPARK_Mode /= On;
11701 Conversion_Error_N
11702 ("cannot convert local pointer to non-local access type<<",
11703 Operand);
11704 Conversion_Error_N ("\Program_Error [<<", Operand);
11706 else
11707 Conversion_Error_N
11708 ("cannot convert local pointer to non-local access type",
11709 Operand);
11710 return False;
11711 end if;
11713 -- Special accessibility checks are needed in the case of access
11714 -- discriminants declared for a limited type.
11716 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11717 and then not Is_Local_Anonymous_Access (Opnd_Type)
11718 then
11719 -- When the operand is a selected access discriminant the check
11720 -- needs to be made against the level of the object denoted by
11721 -- the prefix of the selected name (Object_Access_Level handles
11722 -- checking the prefix of the operand for this case).
11724 if Nkind (Operand) = N_Selected_Component
11725 and then Object_Access_Level (Operand) >
11726 Deepest_Type_Access_Level (Target_Type)
11727 then
11728 -- In an instance, this is a run-time check, but one we know
11729 -- will fail, so generate an appropriate warning. The raise
11730 -- will be generated by Expand_N_Type_Conversion.
11732 if In_Instance_Body then
11733 Error_Msg_Warn := SPARK_Mode /= On;
11734 Conversion_Error_N
11735 ("cannot convert access discriminant to non-local "
11736 & "access type<<", Operand);
11737 Conversion_Error_N ("\Program_Error [<<", Operand);
11739 -- Real error if not in instance body
11741 else
11742 Conversion_Error_N
11743 ("cannot convert access discriminant to non-local "
11744 & "access type", Operand);
11745 return False;
11746 end if;
11747 end if;
11749 -- The case of a reference to an access discriminant from
11750 -- within a limited type declaration (which will appear as
11751 -- a discriminal) is always illegal because the level of the
11752 -- discriminant is considered to be deeper than any (nameable)
11753 -- access type.
11755 if Is_Entity_Name (Operand)
11756 and then not Is_Local_Anonymous_Access (Opnd_Type)
11757 and then
11758 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11759 and then Present (Discriminal_Link (Entity (Operand)))
11760 then
11761 Conversion_Error_N
11762 ("discriminant has deeper accessibility level than target",
11763 Operand);
11764 return False;
11765 end if;
11766 end if;
11767 end if;
11769 return True;
11771 -- General and anonymous access types
11773 elsif Ekind_In (Target_Type, E_General_Access_Type,
11774 E_Anonymous_Access_Type)
11775 and then
11776 Conversion_Check
11777 (Is_Access_Type (Opnd_Type)
11778 and then not
11779 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
11780 E_Access_Protected_Subprogram_Type),
11781 "must be an access-to-object type")
11782 then
11783 if Is_Access_Constant (Opnd_Type)
11784 and then not Is_Access_Constant (Target_Type)
11785 then
11786 Conversion_Error_N
11787 ("access-to-constant operand type not allowed", Operand);
11788 return False;
11789 end if;
11791 -- Check the static accessibility rule of 4.6(17). Note that the
11792 -- check is not enforced when within an instance body, since the RM
11793 -- requires such cases to be caught at run time.
11795 if Ekind (Target_Type) /= E_Anonymous_Access_Type
11796 or else Is_Local_Anonymous_Access (Target_Type)
11797 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
11798 N_Object_Declaration
11799 then
11800 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
11801 -- conversions from an anonymous access type to a named general
11802 -- access type. Such conversions are not allowed in the case of
11803 -- access parameters and stand-alone objects of an anonymous
11804 -- access type. The implicit conversion case is recognized by
11805 -- testing that Comes_From_Source is False and that it's been
11806 -- rewritten. The Comes_From_Source test isn't sufficient because
11807 -- nodes in inlined calls to predefined library routines can have
11808 -- Comes_From_Source set to False. (Is there a better way to test
11809 -- for implicit conversions???)
11811 if Ada_Version >= Ada_2012
11812 and then not Comes_From_Source (N)
11813 and then N /= Original_Node (N)
11814 and then Ekind (Target_Type) = E_General_Access_Type
11815 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
11816 then
11817 if Is_Itype (Opnd_Type) then
11819 -- Implicit conversions aren't allowed for objects of an
11820 -- anonymous access type, since such objects have nonstatic
11821 -- levels in Ada 2012.
11823 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
11824 N_Object_Declaration
11825 then
11826 Conversion_Error_N
11827 ("implicit conversion of stand-alone anonymous "
11828 & "access object not allowed", Operand);
11829 return False;
11831 -- Implicit conversions aren't allowed for anonymous access
11832 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
11833 -- is done to exclude anonymous access results.
11835 elsif not Is_Local_Anonymous_Access (Opnd_Type)
11836 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
11837 N_Function_Specification,
11838 N_Procedure_Specification)
11839 then
11840 Conversion_Error_N
11841 ("implicit conversion of anonymous access formal "
11842 & "not allowed", Operand);
11843 return False;
11845 -- This is a case where there's an enclosing object whose
11846 -- to which the "statically deeper than" relationship does
11847 -- not apply (such as an access discriminant selected from
11848 -- a dereference of an access parameter).
11850 elsif Object_Access_Level (Operand)
11851 = Scope_Depth (Standard_Standard)
11852 then
11853 Conversion_Error_N
11854 ("implicit conversion of anonymous access value "
11855 & "not allowed", Operand);
11856 return False;
11858 -- In other cases, the level of the operand's type must be
11859 -- statically less deep than that of the target type, else
11860 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
11862 elsif Type_Access_Level (Opnd_Type) >
11863 Deepest_Type_Access_Level (Target_Type)
11864 then
11865 Conversion_Error_N
11866 ("implicit conversion of anonymous access value "
11867 & "violates accessibility", Operand);
11868 return False;
11869 end if;
11870 end if;
11872 elsif Type_Access_Level (Opnd_Type) >
11873 Deepest_Type_Access_Level (Target_Type)
11874 then
11875 -- In an instance, this is a run-time check, but one we know
11876 -- will fail, so generate an appropriate warning. The raise
11877 -- will be generated by Expand_N_Type_Conversion.
11879 if In_Instance_Body then
11880 Error_Msg_Warn := SPARK_Mode /= On;
11881 Conversion_Error_N
11882 ("cannot convert local pointer to non-local access type<<",
11883 Operand);
11884 Conversion_Error_N ("\Program_Error [<<", Operand);
11886 -- If not in an instance body, this is a real error
11888 else
11889 -- Avoid generation of spurious error message
11891 if not Error_Posted (N) then
11892 Conversion_Error_N
11893 ("cannot convert local pointer to non-local access type",
11894 Operand);
11895 end if;
11897 return False;
11898 end if;
11900 -- Special accessibility checks are needed in the case of access
11901 -- discriminants declared for a limited type.
11903 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11904 and then not Is_Local_Anonymous_Access (Opnd_Type)
11905 then
11906 -- When the operand is a selected access discriminant the check
11907 -- needs to be made against the level of the object denoted by
11908 -- the prefix of the selected name (Object_Access_Level handles
11909 -- checking the prefix of the operand for this case).
11911 if Nkind (Operand) = N_Selected_Component
11912 and then Object_Access_Level (Operand) >
11913 Deepest_Type_Access_Level (Target_Type)
11914 then
11915 -- In an instance, this is a run-time check, but one we know
11916 -- will fail, so generate an appropriate warning. The raise
11917 -- will be generated by Expand_N_Type_Conversion.
11919 if In_Instance_Body then
11920 Error_Msg_Warn := SPARK_Mode /= On;
11921 Conversion_Error_N
11922 ("cannot convert access discriminant to non-local "
11923 & "access type<<", Operand);
11924 Conversion_Error_N ("\Program_Error [<<", Operand);
11926 -- If not in an instance body, this is a real error
11928 else
11929 Conversion_Error_N
11930 ("cannot convert access discriminant to non-local "
11931 & "access type", Operand);
11932 return False;
11933 end if;
11934 end if;
11936 -- The case of a reference to an access discriminant from
11937 -- within a limited type declaration (which will appear as
11938 -- a discriminal) is always illegal because the level of the
11939 -- discriminant is considered to be deeper than any (nameable)
11940 -- access type.
11942 if Is_Entity_Name (Operand)
11943 and then
11944 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11945 and then Present (Discriminal_Link (Entity (Operand)))
11946 then
11947 Conversion_Error_N
11948 ("discriminant has deeper accessibility level than target",
11949 Operand);
11950 return False;
11951 end if;
11952 end if;
11953 end if;
11955 -- In the presence of limited_with clauses we have to use non-limited
11956 -- views, if available.
11958 Check_Limited : declare
11959 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
11960 -- Helper function to handle limited views
11962 --------------------------
11963 -- Full_Designated_Type --
11964 --------------------------
11966 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
11967 Desig : constant Entity_Id := Designated_Type (T);
11969 begin
11970 -- Handle the limited view of a type
11972 if Is_Incomplete_Type (Desig)
11973 and then From_Limited_With (Desig)
11974 and then Present (Non_Limited_View (Desig))
11975 then
11976 return Available_View (Desig);
11977 else
11978 return Desig;
11979 end if;
11980 end Full_Designated_Type;
11982 -- Local Declarations
11984 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
11985 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
11987 Same_Base : constant Boolean :=
11988 Base_Type (Target) = Base_Type (Opnd);
11990 -- Start of processing for Check_Limited
11992 begin
11993 if Is_Tagged_Type (Target) then
11994 return Valid_Tagged_Conversion (Target, Opnd);
11996 else
11997 if not Same_Base then
11998 Conversion_Error_NE
11999 ("target designated type not compatible with }",
12000 N, Base_Type (Opnd));
12001 return False;
12003 -- Ada 2005 AI-384: legality rule is symmetric in both
12004 -- designated types. The conversion is legal (with possible
12005 -- constraint check) if either designated type is
12006 -- unconstrained.
12008 elsif Subtypes_Statically_Match (Target, Opnd)
12009 or else
12010 (Has_Discriminants (Target)
12011 and then
12012 (not Is_Constrained (Opnd)
12013 or else not Is_Constrained (Target)))
12014 then
12015 -- Special case, if Value_Size has been used to make the
12016 -- sizes different, the conversion is not allowed even
12017 -- though the subtypes statically match.
12019 if Known_Static_RM_Size (Target)
12020 and then Known_Static_RM_Size (Opnd)
12021 and then RM_Size (Target) /= RM_Size (Opnd)
12022 then
12023 Conversion_Error_NE
12024 ("target designated subtype not compatible with }",
12025 N, Opnd);
12026 Conversion_Error_NE
12027 ("\because sizes of the two designated subtypes differ",
12028 N, Opnd);
12029 return False;
12031 -- Normal case where conversion is allowed
12033 else
12034 return True;
12035 end if;
12037 else
12038 Error_Msg_NE
12039 ("target designated subtype not compatible with }",
12040 N, Opnd);
12041 return False;
12042 end if;
12043 end if;
12044 end Check_Limited;
12046 -- Access to subprogram types. If the operand is an access parameter,
12047 -- the type has a deeper accessibility that any master, and cannot be
12048 -- assigned. We must make an exception if the conversion is part of an
12049 -- assignment and the target is the return object of an extended return
12050 -- statement, because in that case the accessibility check takes place
12051 -- after the return.
12053 elsif Is_Access_Subprogram_Type (Target_Type)
12055 -- Note: this test of Opnd_Type is there to prevent entering this
12056 -- branch in the case of a remote access to subprogram type, which
12057 -- is internally represented as an E_Record_Type.
12059 and then Is_Access_Type (Opnd_Type)
12060 then
12061 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
12062 and then Is_Entity_Name (Operand)
12063 and then Ekind (Entity (Operand)) = E_In_Parameter
12064 and then
12065 (Nkind (Parent (N)) /= N_Assignment_Statement
12066 or else not Is_Entity_Name (Name (Parent (N)))
12067 or else not Is_Return_Object (Entity (Name (Parent (N)))))
12068 then
12069 Conversion_Error_N
12070 ("illegal attempt to store anonymous access to subprogram",
12071 Operand);
12072 Conversion_Error_N
12073 ("\value has deeper accessibility than any master "
12074 & "(RM 3.10.2 (13))",
12075 Operand);
12077 Error_Msg_NE
12078 ("\use named access type for& instead of access parameter",
12079 Operand, Entity (Operand));
12080 end if;
12082 -- Check that the designated types are subtype conformant
12084 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
12085 Old_Id => Designated_Type (Opnd_Type),
12086 Err_Loc => N);
12088 -- Check the static accessibility rule of 4.6(20)
12090 if Type_Access_Level (Opnd_Type) >
12091 Deepest_Type_Access_Level (Target_Type)
12092 then
12093 Conversion_Error_N
12094 ("operand type has deeper accessibility level than target",
12095 Operand);
12097 -- Check that if the operand type is declared in a generic body,
12098 -- then the target type must be declared within that same body
12099 -- (enforces last sentence of 4.6(20)).
12101 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
12102 declare
12103 O_Gen : constant Node_Id :=
12104 Enclosing_Generic_Body (Opnd_Type);
12106 T_Gen : Node_Id;
12108 begin
12109 T_Gen := Enclosing_Generic_Body (Target_Type);
12110 while Present (T_Gen) and then T_Gen /= O_Gen loop
12111 T_Gen := Enclosing_Generic_Body (T_Gen);
12112 end loop;
12114 if T_Gen /= O_Gen then
12115 Conversion_Error_N
12116 ("target type must be declared in same generic body "
12117 & "as operand type", N);
12118 end if;
12119 end;
12120 end if;
12122 return True;
12124 -- Remote access to subprogram types
12126 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
12127 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
12128 then
12129 -- It is valid to convert from one RAS type to another provided
12130 -- that their specification statically match.
12132 -- Note: at this point, remote access to subprogram types have been
12133 -- expanded to their E_Record_Type representation, and we need to
12134 -- go back to the original access type definition using the
12135 -- Corresponding_Remote_Type attribute in order to check that the
12136 -- designated profiles match.
12138 pragma Assert (Ekind (Target_Type) = E_Record_Type);
12139 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
12141 Check_Subtype_Conformant
12142 (New_Id =>
12143 Designated_Type (Corresponding_Remote_Type (Target_Type)),
12144 Old_Id =>
12145 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
12146 Err_Loc =>
12148 return True;
12150 -- If it was legal in the generic, it's legal in the instance
12152 elsif In_Instance_Body then
12153 return True;
12155 -- If both are tagged types, check legality of view conversions
12157 elsif Is_Tagged_Type (Target_Type)
12158 and then
12159 Is_Tagged_Type (Opnd_Type)
12160 then
12161 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
12163 -- Types derived from the same root type are convertible
12165 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
12166 return True;
12168 -- In an instance or an inlined body, there may be inconsistent views of
12169 -- the same type, or of types derived from a common root.
12171 elsif (In_Instance or In_Inlined_Body)
12172 and then
12173 Root_Type (Underlying_Type (Target_Type)) =
12174 Root_Type (Underlying_Type (Opnd_Type))
12175 then
12176 return True;
12178 -- Special check for common access type error case
12180 elsif Ekind (Target_Type) = E_Access_Type
12181 and then Is_Access_Type (Opnd_Type)
12182 then
12183 Conversion_Error_N ("target type must be general access type!", N);
12184 Conversion_Error_NE -- CODEFIX
12185 ("add ALL to }!", N, Target_Type);
12186 return False;
12188 -- Here we have a real conversion error
12190 else
12191 Conversion_Error_NE
12192 ("invalid conversion, not compatible with }", N, Opnd_Type);
12193 return False;
12194 end if;
12195 end Valid_Conversion;
12197 end Sem_Res;