gcc/ada/
[official-gcc.git] / gcc / ada / sem_res.adb
blobf300e7099b39e76b2c38ba079a90592f40e2ac50
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_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
132 -- N is either an indexed component or a selected component. This function
133 -- returns true if the prefix refers to an object that has an address
134 -- clause (the case in which we may want to issue a warning).
136 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
137 -- Determine whether E is an access type declared by an access declaration,
138 -- and not an (anonymous) allocator type.
140 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
141 -- Utility to check whether the entity for an operator is a predefined
142 -- operator, in which case the expression is left as an operator in the
143 -- tree (else it is rewritten into a call). An instance of an intrinsic
144 -- conversion operation may be given an operator name, but is not treated
145 -- like an operator. Note that an operator that is an imported back-end
146 -- builtin has convention Intrinsic, but is expected to be rewritten into
147 -- a call, so such an operator is not treated as predefined by this
148 -- predicate.
150 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
151 -- If a default expression in entry call N depends on the discriminants
152 -- of the task, it must be replaced with a reference to the discriminant
153 -- of the task being called.
155 procedure Resolve_Op_Concat_Arg
156 (N : Node_Id;
157 Arg : Node_Id;
158 Typ : Entity_Id;
159 Is_Comp : Boolean);
160 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
161 -- concatenation operator. The operand is either of the array type or of
162 -- the component type. If the operand is an aggregate, and the component
163 -- type is composite, this is ambiguous if component type has aggregates.
165 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
166 -- Does the first part of the work of Resolve_Op_Concat
168 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
169 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
170 -- has been resolved. See Resolve_Op_Concat for details.
172 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
173 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
174 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
208 function Operator_Kind
209 (Op_Name : Name_Id;
210 Is_Binary : Boolean) return Node_Kind;
211 -- Utility to map the name of an operator into the corresponding Node. Used
212 -- by other node rewriting procedures.
214 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
215 -- Resolve actuals of call, and add default expressions for missing ones.
216 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
217 -- called subprogram.
219 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
220 -- Called from Resolve_Call, when the prefix denotes an entry or element
221 -- of entry family. Actuals are resolved as for subprograms, and the node
222 -- is rebuilt as an entry call. Also called for protected operations. Typ
223 -- is the context type, which is used when the operation is a protected
224 -- function with no arguments, and the return value is indexed.
226 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
227 -- A call to a user-defined intrinsic operator is rewritten as a call to
228 -- the corresponding predefined operator, with suitable conversions. Note
229 -- that this applies only for intrinsic operators that denote predefined
230 -- operators, not ones that are intrinsic imports of back-end builtins.
232 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
233 -- Ditto, for arithmetic unary operators
235 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
236 -- If an operator node resolves to a call to a user-defined operator,
237 -- rewrite the node as a function call.
239 procedure Make_Call_Into_Operator
240 (N : Node_Id;
241 Typ : Entity_Id;
242 Op_Id : Entity_Id);
243 -- Inverse transformation: if an operator is given in functional notation,
244 -- then after resolving the node, transform into an operator node, so
245 -- that operands are resolved properly. Recall that predefined operators
246 -- do not have a full signature and special resolution rules apply.
248 procedure Rewrite_Renamed_Operator
249 (N : Node_Id;
250 Op : Entity_Id;
251 Typ : Entity_Id);
252 -- An operator can rename another, e.g. in an instantiation. In that
253 -- case, the proper operator node must be constructed and resolved.
255 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
256 -- The String_Literal_Subtype is built for all strings that are not
257 -- operands of a static concatenation operation. If the argument is
258 -- not a N_String_Literal node, then the call has no effect.
260 procedure Set_Slice_Subtype (N : Node_Id);
261 -- Build subtype of array type, with the range specified by the slice
263 procedure Simplify_Type_Conversion (N : Node_Id);
264 -- Called after N has been resolved and evaluated, but before range checks
265 -- have been applied. Currently simplifies a combination of floating-point
266 -- to integer conversion and Rounding or Truncation attribute.
268 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
269 -- A universal_fixed expression in an universal context is unambiguous if
270 -- there is only one applicable fixed point type. Determining whether there
271 -- is only one requires a search over all visible entities, and happens
272 -- only in very pathological cases (see 6115-006).
274 -------------------------
275 -- Ambiguous_Character --
276 -------------------------
278 procedure Ambiguous_Character (C : Node_Id) is
279 E : Entity_Id;
281 begin
282 if Nkind (C) = N_Character_Literal then
283 Error_Msg_N ("ambiguous character literal", C);
285 -- First the ones in Standard
287 Error_Msg_N ("\\possible interpretation: Character!", C);
288 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
290 -- Include Wide_Wide_Character in Ada 2005 mode
292 if Ada_Version >= Ada_2005 then
293 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
294 end if;
296 -- Now any other types that match
298 E := Current_Entity (C);
299 while Present (E) loop
300 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
301 E := Homonym (E);
302 end loop;
303 end if;
304 end Ambiguous_Character;
306 -------------------------
307 -- Analyze_And_Resolve --
308 -------------------------
310 procedure Analyze_And_Resolve (N : Node_Id) is
311 begin
312 Analyze (N);
313 Resolve (N);
314 end Analyze_And_Resolve;
316 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
317 begin
318 Analyze (N);
319 Resolve (N, Typ);
320 end Analyze_And_Resolve;
322 -- Versions with check(s) suppressed
324 procedure Analyze_And_Resolve
325 (N : Node_Id;
326 Typ : Entity_Id;
327 Suppress : Check_Id)
329 Scop : constant Entity_Id := Current_Scope;
331 begin
332 if Suppress = All_Checks then
333 declare
334 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
335 begin
336 Scope_Suppress.Suppress := (others => True);
337 Analyze_And_Resolve (N, Typ);
338 Scope_Suppress.Suppress := Sva;
339 end;
341 else
342 declare
343 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
344 begin
345 Scope_Suppress.Suppress (Suppress) := True;
346 Analyze_And_Resolve (N, Typ);
347 Scope_Suppress.Suppress (Suppress) := Svg;
348 end;
349 end if;
351 if Current_Scope /= Scop
352 and then Scope_Is_Transient
353 then
354 -- This can only happen if a transient scope was created for an inner
355 -- expression, which will be removed upon completion of the analysis
356 -- of an enclosing construct. The transient scope must have the
357 -- suppress status of the enclosing environment, not of this Analyze
358 -- call.
360 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
361 Scope_Suppress;
362 end if;
363 end Analyze_And_Resolve;
365 procedure Analyze_And_Resolve
366 (N : Node_Id;
367 Suppress : Check_Id)
369 Scop : constant Entity_Id := Current_Scope;
371 begin
372 if Suppress = All_Checks then
373 declare
374 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
375 begin
376 Scope_Suppress.Suppress := (others => True);
377 Analyze_And_Resolve (N);
378 Scope_Suppress.Suppress := Sva;
379 end;
381 else
382 declare
383 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
384 begin
385 Scope_Suppress.Suppress (Suppress) := True;
386 Analyze_And_Resolve (N);
387 Scope_Suppress.Suppress (Suppress) := Svg;
388 end;
389 end if;
391 if Current_Scope /= Scop and then Scope_Is_Transient then
392 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
393 Scope_Suppress;
394 end if;
395 end Analyze_And_Resolve;
397 ----------------------------
398 -- Check_Discriminant_Use --
399 ----------------------------
401 procedure Check_Discriminant_Use (N : Node_Id) is
402 PN : constant Node_Id := Parent (N);
403 Disc : constant Entity_Id := Entity (N);
404 P : Node_Id;
405 D : Node_Id;
407 begin
408 -- Any use in a spec-expression is legal
410 if In_Spec_Expression then
411 null;
413 elsif Nkind (PN) = N_Range then
415 -- Discriminant cannot be used to constrain a scalar type
417 P := Parent (PN);
419 if Nkind (P) = N_Range_Constraint
420 and then Nkind (Parent (P)) = N_Subtype_Indication
421 and then Nkind (Parent (Parent (P))) = N_Component_Definition
422 then
423 Error_Msg_N ("discriminant cannot constrain scalar type", N);
425 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
427 -- The following check catches the unusual case where a
428 -- discriminant appears within an index constraint that is part of
429 -- a larger expression within a constraint on a component, e.g. "C
430 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
431 -- of record components, and note that a similar check should also
432 -- apply in the case of discriminant constraints below. ???
434 -- Note that the check for N_Subtype_Declaration below is to
435 -- detect the valid use of discriminants in the constraints of a
436 -- subtype declaration when this subtype declaration appears
437 -- inside the scope of a record type (which is syntactically
438 -- illegal, but which may be created as part of derived type
439 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
440 -- for more info.
442 if Ekind (Current_Scope) = E_Record_Type
443 and then Scope (Disc) = Current_Scope
444 and then not
445 (Nkind (Parent (P)) = N_Subtype_Indication
446 and then
447 Nkind_In (Parent (Parent (P)), N_Component_Definition,
448 N_Subtype_Declaration)
449 and then Paren_Count (N) = 0)
450 then
451 Error_Msg_N
452 ("discriminant must appear alone in component constraint", N);
453 return;
454 end if;
456 -- Detect a common error:
458 -- type R (D : Positive := 100) is record
459 -- Name : String (1 .. D);
460 -- end record;
462 -- The default value causes an object of type R to be allocated
463 -- with room for Positive'Last characters. The RM does not mandate
464 -- the allocation of the maximum size, but that is what GNAT does
465 -- so we should warn the programmer that there is a problem.
467 Check_Large : declare
468 SI : Node_Id;
469 T : Entity_Id;
470 TB : Node_Id;
471 CB : Entity_Id;
473 function Large_Storage_Type (T : Entity_Id) return Boolean;
474 -- Return True if type T has a large enough range that any
475 -- array whose index type covered the whole range of the type
476 -- would likely raise Storage_Error.
478 ------------------------
479 -- Large_Storage_Type --
480 ------------------------
482 function Large_Storage_Type (T : Entity_Id) return Boolean is
483 begin
484 -- The type is considered large if its bounds are known at
485 -- compile time and if it requires at least as many bits as
486 -- a Positive to store the possible values.
488 return Compile_Time_Known_Value (Type_Low_Bound (T))
489 and then Compile_Time_Known_Value (Type_High_Bound (T))
490 and then
491 Minimum_Size (T, Biased => True) >=
492 RM_Size (Standard_Positive);
493 end Large_Storage_Type;
495 -- Start of processing for Check_Large
497 begin
498 -- Check that the Disc has a large range
500 if not Large_Storage_Type (Etype (Disc)) then
501 goto No_Danger;
502 end if;
504 -- If the enclosing type is limited, we allocate only the
505 -- default value, not the maximum, and there is no need for
506 -- a warning.
508 if Is_Limited_Type (Scope (Disc)) then
509 goto No_Danger;
510 end if;
512 -- Check that it is the high bound
514 if N /= High_Bound (PN)
515 or else No (Discriminant_Default_Value (Disc))
516 then
517 goto No_Danger;
518 end if;
520 -- Check the array allows a large range at this bound. First
521 -- find the array
523 SI := Parent (P);
525 if Nkind (SI) /= N_Subtype_Indication then
526 goto No_Danger;
527 end if;
529 T := Entity (Subtype_Mark (SI));
531 if not Is_Array_Type (T) then
532 goto No_Danger;
533 end if;
535 -- Next, find the dimension
537 TB := First_Index (T);
538 CB := First (Constraints (P));
539 while True
540 and then Present (TB)
541 and then Present (CB)
542 and then CB /= PN
543 loop
544 Next_Index (TB);
545 Next (CB);
546 end loop;
548 if CB /= PN then
549 goto No_Danger;
550 end if;
552 -- Now, check the dimension has a large range
554 if not Large_Storage_Type (Etype (TB)) then
555 goto No_Danger;
556 end if;
558 -- Warn about the danger
560 Error_Msg_N
561 ("??creation of & object may raise Storage_Error!",
562 Scope (Disc));
564 <<No_Danger>>
565 null;
567 end Check_Large;
568 end if;
570 -- Legal case is in index or discriminant constraint
572 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
573 N_Discriminant_Association)
574 then
575 if Paren_Count (N) > 0 then
576 Error_Msg_N
577 ("discriminant in constraint must appear alone", N);
579 elsif Nkind (N) = N_Expanded_Name
580 and then Comes_From_Source (N)
581 then
582 Error_Msg_N
583 ("discriminant must appear alone as a direct name", N);
584 end if;
586 return;
588 -- Otherwise, context is an expression. It should not be within (i.e. a
589 -- subexpression of) a constraint for a component.
591 else
592 D := PN;
593 P := Parent (PN);
594 while not Nkind_In (P, N_Component_Declaration,
595 N_Subtype_Indication,
596 N_Entry_Declaration)
597 loop
598 D := P;
599 P := Parent (P);
600 exit when No (P);
601 end loop;
603 -- If the discriminant is used in an expression that is a bound of a
604 -- scalar type, an Itype is created and the bounds are attached to
605 -- its range, not to the original subtype indication. Such use is of
606 -- course a double fault.
608 if (Nkind (P) = N_Subtype_Indication
609 and then Nkind_In (Parent (P), N_Component_Definition,
610 N_Derived_Type_Definition)
611 and then D = Constraint (P))
613 -- The constraint itself may be given by a subtype indication,
614 -- rather than by a more common discrete range.
616 or else (Nkind (P) = N_Subtype_Indication
617 and then
618 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
619 or else Nkind (P) = N_Entry_Declaration
620 or else Nkind (D) = N_Defining_Identifier
621 then
622 Error_Msg_N
623 ("discriminant in constraint must appear alone", N);
624 end if;
625 end if;
626 end Check_Discriminant_Use;
628 --------------------------------
629 -- Check_For_Visible_Operator --
630 --------------------------------
632 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
633 begin
634 if Is_Invisible_Operator (N, T) then
635 Error_Msg_NE -- CODEFIX
636 ("operator for} is not directly visible!", N, First_Subtype (T));
637 Error_Msg_N -- CODEFIX
638 ("use clause would make operation legal!", N);
639 end if;
640 end Check_For_Visible_Operator;
642 ----------------------------------
643 -- Check_Fully_Declared_Prefix --
644 ----------------------------------
646 procedure Check_Fully_Declared_Prefix
647 (Typ : Entity_Id;
648 Pref : Node_Id)
650 begin
651 -- Check that the designated type of the prefix of a dereference is
652 -- not an incomplete type. This cannot be done unconditionally, because
653 -- dereferences of private types are legal in default expressions. This
654 -- case is taken care of in Check_Fully_Declared, called below. There
655 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
657 -- This consideration also applies to similar checks for allocators,
658 -- qualified expressions, and type conversions.
660 -- An additional exception concerns other per-object expressions that
661 -- are not directly related to component declarations, in particular
662 -- representation pragmas for tasks. These will be per-object
663 -- expressions if they depend on discriminants or some global entity.
664 -- If the task has access discriminants, the designated type may be
665 -- incomplete at the point the expression is resolved. This resolution
666 -- takes place within the body of the initialization procedure, where
667 -- the discriminant is replaced by its discriminal.
669 if Is_Entity_Name (Pref)
670 and then Ekind (Entity (Pref)) = E_In_Parameter
671 then
672 null;
674 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
675 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
676 -- Analyze_Object_Renaming, and Freeze_Entity.
678 elsif Ada_Version >= Ada_2005
679 and then Is_Entity_Name (Pref)
680 and then Is_Access_Type (Etype (Pref))
681 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
682 E_Incomplete_Type
683 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
684 then
685 null;
686 else
687 Check_Fully_Declared (Typ, Parent (Pref));
688 end if;
689 end Check_Fully_Declared_Prefix;
691 ------------------------------
692 -- Check_Infinite_Recursion --
693 ------------------------------
695 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
696 P : Node_Id;
697 C : Node_Id;
699 function Same_Argument_List return Boolean;
700 -- Check whether list of actuals is identical to list of formals of
701 -- called function (which is also the enclosing scope).
703 ------------------------
704 -- Same_Argument_List --
705 ------------------------
707 function Same_Argument_List return Boolean is
708 A : Node_Id;
709 F : Entity_Id;
710 Subp : Entity_Id;
712 begin
713 if not Is_Entity_Name (Name (N)) then
714 return False;
715 else
716 Subp := Entity (Name (N));
717 end if;
719 F := First_Formal (Subp);
720 A := First_Actual (N);
721 while Present (F) and then Present (A) loop
722 if not Is_Entity_Name (A)
723 or else Entity (A) /= F
724 then
725 return False;
726 end if;
728 Next_Actual (A);
729 Next_Formal (F);
730 end loop;
732 return True;
733 end Same_Argument_List;
735 -- Start of processing for Check_Infinite_Recursion
737 begin
738 -- Special case, if this is a procedure call and is a call to the
739 -- current procedure with the same argument list, then this is for
740 -- sure an infinite recursion and we insert a call to raise SE.
742 if Is_List_Member (N)
743 and then List_Length (List_Containing (N)) = 1
744 and then Same_Argument_List
745 then
746 declare
747 P : constant Node_Id := Parent (N);
748 begin
749 if Nkind (P) = N_Handled_Sequence_Of_Statements
750 and then Nkind (Parent (P)) = N_Subprogram_Body
751 and then Is_Empty_List (Declarations (Parent (P)))
752 then
753 Error_Msg_Warn := SPARK_Mode /= On;
754 Error_Msg_N ("!infinite recursion<<", N);
755 Error_Msg_N ("\!Storage_Error [<<", N);
756 Insert_Action (N,
757 Make_Raise_Storage_Error (Sloc (N),
758 Reason => SE_Infinite_Recursion));
759 return True;
760 end if;
761 end;
762 end if;
764 -- If not that special case, search up tree, quitting if we reach a
765 -- construct (e.g. a conditional) that tells us that this is not a
766 -- case for an infinite recursion warning.
768 C := N;
769 loop
770 P := Parent (C);
772 -- If no parent, then we were not inside a subprogram, this can for
773 -- example happen when processing certain pragmas in a spec. Just
774 -- return False in this case.
776 if No (P) then
777 return False;
778 end if;
780 -- Done if we get to subprogram body, this is definitely an infinite
781 -- recursion case if we did not find anything to stop us.
783 exit when Nkind (P) = N_Subprogram_Body;
785 -- If appearing in conditional, result is false
787 if Nkind_In (P, N_Or_Else,
788 N_And_Then,
789 N_Case_Expression,
790 N_Case_Statement,
791 N_If_Expression,
792 N_If_Statement)
793 then
794 return False;
796 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
797 and then C /= First (Statements (P))
798 then
799 -- If the call is the expression of a return statement and the
800 -- actuals are identical to the formals, it's worth a warning.
801 -- However, we skip this if there is an immediately preceding
802 -- raise statement, since the call is never executed.
804 -- Furthermore, this corresponds to a common idiom:
806 -- function F (L : Thing) return Boolean is
807 -- begin
808 -- raise Program_Error;
809 -- return F (L);
810 -- end F;
812 -- for generating a stub function
814 if Nkind (Parent (N)) = N_Simple_Return_Statement
815 and then Same_Argument_List
816 then
817 exit when not Is_List_Member (Parent (N));
819 -- OK, return statement is in a statement list, look for raise
821 declare
822 Nod : Node_Id;
824 begin
825 -- Skip past N_Freeze_Entity nodes generated by expansion
827 Nod := Prev (Parent (N));
828 while Present (Nod)
829 and then Nkind (Nod) = N_Freeze_Entity
830 loop
831 Prev (Nod);
832 end loop;
834 -- If no raise statement, give warning. We look at the
835 -- original node, because in the case of "raise ... with
836 -- ...", the node has been transformed into a call.
838 exit when Nkind (Original_Node (Nod)) /= N_Raise_Statement
839 and then
840 (Nkind (Nod) not in N_Raise_xxx_Error
841 or else Present (Condition (Nod)));
842 end;
843 end if;
845 return False;
847 else
848 C := P;
849 end if;
850 end loop;
852 Error_Msg_Warn := SPARK_Mode /= On;
853 Error_Msg_N ("!possible infinite recursion<<", N);
854 Error_Msg_N ("\!??Storage_Error ]<<", N);
856 return True;
857 end Check_Infinite_Recursion;
859 -------------------------------
860 -- Check_Initialization_Call --
861 -------------------------------
863 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
864 Typ : constant Entity_Id := Etype (First_Formal (Nam));
866 function Uses_SS (T : Entity_Id) return Boolean;
867 -- Check whether the creation of an object of the type will involve
868 -- use of the secondary stack. If T is a record type, this is true
869 -- if the expression for some component uses the secondary stack, e.g.
870 -- through a call to a function that returns an unconstrained value.
871 -- False if T is controlled, because cleanups occur elsewhere.
873 -------------
874 -- Uses_SS --
875 -------------
877 function Uses_SS (T : Entity_Id) return Boolean is
878 Comp : Entity_Id;
879 Expr : Node_Id;
880 Full_Type : Entity_Id := Underlying_Type (T);
882 begin
883 -- Normally we want to use the underlying type, but if it's not set
884 -- then continue with T.
886 if not Present (Full_Type) then
887 Full_Type := T;
888 end if;
890 if Is_Controlled (Full_Type) then
891 return False;
893 elsif Is_Array_Type (Full_Type) then
894 return Uses_SS (Component_Type (Full_Type));
896 elsif Is_Record_Type (Full_Type) then
897 Comp := First_Component (Full_Type);
898 while Present (Comp) loop
899 if Ekind (Comp) = E_Component
900 and then Nkind (Parent (Comp)) = N_Component_Declaration
901 then
902 -- The expression for a dynamic component may be rewritten
903 -- as a dereference, so retrieve original node.
905 Expr := Original_Node (Expression (Parent (Comp)));
907 -- Return True if the expression is a call to a function
908 -- (including an attribute function such as Image, or a
909 -- user-defined operator) with a result that requires a
910 -- transient scope.
912 if (Nkind (Expr) = N_Function_Call
913 or else Nkind (Expr) in N_Op
914 or else (Nkind (Expr) = N_Attribute_Reference
915 and then Present (Expressions (Expr))))
916 and then Requires_Transient_Scope (Etype (Expr))
917 then
918 return True;
920 elsif Uses_SS (Etype (Comp)) then
921 return True;
922 end if;
923 end if;
925 Next_Component (Comp);
926 end loop;
928 return False;
930 else
931 return False;
932 end if;
933 end Uses_SS;
935 -- Start of processing for Check_Initialization_Call
937 begin
938 -- Establish a transient scope if the type needs it
940 if Uses_SS (Typ) then
941 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
942 end if;
943 end Check_Initialization_Call;
945 ---------------------------------------
946 -- Check_No_Direct_Boolean_Operators --
947 ---------------------------------------
949 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
950 begin
951 if Scope (Entity (N)) = Standard_Standard
952 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
953 then
954 -- Restriction only applies to original source code
956 if Comes_From_Source (N) then
957 Check_Restriction (No_Direct_Boolean_Operators, N);
958 end if;
959 end if;
961 -- Do style check (but skip if in instance, error is on template)
963 if Style_Check then
964 if not In_Instance then
965 Check_Boolean_Operator (N);
966 end if;
967 end if;
968 end Check_No_Direct_Boolean_Operators;
970 ------------------------------
971 -- Check_Parameterless_Call --
972 ------------------------------
974 procedure Check_Parameterless_Call (N : Node_Id) is
975 Nam : Node_Id;
977 function Prefix_Is_Access_Subp return Boolean;
978 -- If the prefix is of an access_to_subprogram type, the node must be
979 -- rewritten as a call. Ditto if the prefix is overloaded and all its
980 -- interpretations are access to subprograms.
982 ---------------------------
983 -- Prefix_Is_Access_Subp --
984 ---------------------------
986 function Prefix_Is_Access_Subp return Boolean is
987 I : Interp_Index;
988 It : Interp;
990 begin
991 -- If the context is an attribute reference that can apply to
992 -- functions, this is never a parameterless call (RM 4.1.4(6)).
994 if Nkind (Parent (N)) = N_Attribute_Reference
995 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
996 Name_Code_Address,
997 Name_Access)
998 then
999 return False;
1000 end if;
1002 if not Is_Overloaded (N) then
1003 return
1004 Ekind (Etype (N)) = E_Subprogram_Type
1005 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1006 else
1007 Get_First_Interp (N, I, It);
1008 while Present (It.Typ) loop
1009 if Ekind (It.Typ) /= E_Subprogram_Type
1010 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1011 then
1012 return False;
1013 end if;
1015 Get_Next_Interp (I, It);
1016 end loop;
1018 return True;
1019 end if;
1020 end Prefix_Is_Access_Subp;
1022 -- Start of processing for Check_Parameterless_Call
1024 begin
1025 -- Defend against junk stuff if errors already detected
1027 if Total_Errors_Detected /= 0 then
1028 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1029 return;
1030 elsif Nkind (N) in N_Has_Chars
1031 and then Chars (N) in Error_Name_Or_No_Name
1032 then
1033 return;
1034 end if;
1036 Require_Entity (N);
1037 end if;
1039 -- If the context expects a value, and the name is a procedure, this is
1040 -- most likely a missing 'Access. Don't try to resolve the parameterless
1041 -- call, error will be caught when the outer call is analyzed.
1043 if Is_Entity_Name (N)
1044 and then Ekind (Entity (N)) = E_Procedure
1045 and then not Is_Overloaded (N)
1046 and then
1047 Nkind_In (Parent (N), N_Parameter_Association,
1048 N_Function_Call,
1049 N_Procedure_Call_Statement)
1050 then
1051 return;
1052 end if;
1054 -- Rewrite as call if overloadable entity that is (or could be, in the
1055 -- overloaded case) a function call. If we know for sure that the entity
1056 -- is an enumeration literal, we do not rewrite it.
1058 -- If the entity is the name of an operator, it cannot be a call because
1059 -- operators cannot have default parameters. In this case, this must be
1060 -- a string whose contents coincide with an operator name. Set the kind
1061 -- of the node appropriately.
1063 if (Is_Entity_Name (N)
1064 and then Nkind (N) /= N_Operator_Symbol
1065 and then Is_Overloadable (Entity (N))
1066 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1067 or else Is_Overloaded (N)))
1069 -- Rewrite as call if it is an explicit dereference of an expression of
1070 -- a subprogram access type, and the subprogram type is not that of a
1071 -- procedure or entry.
1073 or else
1074 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1076 -- Rewrite as call if it is a selected component which is a function,
1077 -- this is the case of a call to a protected function (which may be
1078 -- overloaded with other protected operations).
1080 or else
1081 (Nkind (N) = N_Selected_Component
1082 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1083 or else
1084 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1085 E_Procedure)
1086 and then Is_Overloaded (Selector_Name (N)))))
1088 -- If one of the above three conditions is met, rewrite as call. Apply
1089 -- the rewriting only once.
1091 then
1092 if Nkind (Parent (N)) /= N_Function_Call
1093 or else N /= Name (Parent (N))
1094 then
1096 -- This may be a prefixed call that was not fully analyzed, e.g.
1097 -- an actual in an instance.
1099 if Ada_Version >= Ada_2005
1100 and then Nkind (N) = N_Selected_Component
1101 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1102 then
1103 Analyze_Selected_Component (N);
1105 if Nkind (N) /= N_Selected_Component then
1106 return;
1107 end if;
1108 end if;
1110 -- The node is the name of the parameterless call. Preserve its
1111 -- descendants, which may be complex expressions.
1113 Nam := Relocate_Node (N);
1115 -- If overloaded, overload set belongs to new copy
1117 Save_Interps (N, Nam);
1119 -- Change node to parameterless function call (note that the
1120 -- Parameter_Associations associations field is left set to Empty,
1121 -- its normal default value since there are no parameters)
1123 Change_Node (N, N_Function_Call);
1124 Set_Name (N, Nam);
1125 Set_Sloc (N, Sloc (Nam));
1126 Analyze_Call (N);
1127 end if;
1129 elsif Nkind (N) = N_Parameter_Association then
1130 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1132 elsif Nkind (N) = N_Operator_Symbol then
1133 Change_Operator_Symbol_To_String_Literal (N);
1134 Set_Is_Overloaded (N, False);
1135 Set_Etype (N, Any_String);
1136 end if;
1137 end Check_Parameterless_Call;
1139 --------------------------------
1140 -- Is_Atomic_Ref_With_Address --
1141 --------------------------------
1143 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1144 Pref : constant Node_Id := Prefix (N);
1146 begin
1147 if not Is_Entity_Name (Pref) then
1148 return False;
1150 else
1151 declare
1152 Pent : constant Entity_Id := Entity (Pref);
1153 Ptyp : constant Entity_Id := Etype (Pent);
1154 begin
1155 return not Is_Access_Type (Ptyp)
1156 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1157 and then Present (Address_Clause (Pent));
1158 end;
1159 end if;
1160 end Is_Atomic_Ref_With_Address;
1162 -----------------------------
1163 -- Is_Definite_Access_Type --
1164 -----------------------------
1166 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1167 Btyp : constant Entity_Id := Base_Type (E);
1168 begin
1169 return Ekind (Btyp) = E_Access_Type
1170 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1171 and then Comes_From_Source (Btyp));
1172 end Is_Definite_Access_Type;
1174 ----------------------
1175 -- Is_Predefined_Op --
1176 ----------------------
1178 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1179 begin
1180 -- Predefined operators are intrinsic subprograms
1182 if not Is_Intrinsic_Subprogram (Nam) then
1183 return False;
1184 end if;
1186 -- A call to a back-end builtin is never a predefined operator
1188 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1189 return False;
1190 end if;
1192 return not Is_Generic_Instance (Nam)
1193 and then Chars (Nam) in Any_Operator_Name
1194 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1195 end Is_Predefined_Op;
1197 -----------------------------
1198 -- Make_Call_Into_Operator --
1199 -----------------------------
1201 procedure Make_Call_Into_Operator
1202 (N : Node_Id;
1203 Typ : Entity_Id;
1204 Op_Id : Entity_Id)
1206 Op_Name : constant Name_Id := Chars (Op_Id);
1207 Act1 : Node_Id := First_Actual (N);
1208 Act2 : Node_Id := Next_Actual (Act1);
1209 Error : Boolean := False;
1210 Func : constant Entity_Id := Entity (Name (N));
1211 Is_Binary : constant Boolean := Present (Act2);
1212 Op_Node : Node_Id;
1213 Opnd_Type : Entity_Id;
1214 Orig_Type : Entity_Id := Empty;
1215 Pack : Entity_Id;
1217 type Kind_Test is access function (E : Entity_Id) return Boolean;
1219 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1220 -- If the operand is not universal, and the operator is given by an
1221 -- expanded name, verify that the operand has an interpretation with a
1222 -- type defined in the given scope of the operator.
1224 function Type_In_P (Test : Kind_Test) return Entity_Id;
1225 -- Find a type of the given class in package Pack that contains the
1226 -- operator.
1228 ---------------------------
1229 -- Operand_Type_In_Scope --
1230 ---------------------------
1232 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1233 Nod : constant Node_Id := Right_Opnd (Op_Node);
1234 I : Interp_Index;
1235 It : Interp;
1237 begin
1238 if not Is_Overloaded (Nod) then
1239 return Scope (Base_Type (Etype (Nod))) = S;
1241 else
1242 Get_First_Interp (Nod, I, It);
1243 while Present (It.Typ) loop
1244 if Scope (Base_Type (It.Typ)) = S then
1245 return True;
1246 end if;
1248 Get_Next_Interp (I, It);
1249 end loop;
1251 return False;
1252 end if;
1253 end Operand_Type_In_Scope;
1255 ---------------
1256 -- Type_In_P --
1257 ---------------
1259 function Type_In_P (Test : Kind_Test) return Entity_Id is
1260 E : Entity_Id;
1262 function In_Decl return Boolean;
1263 -- Verify that node is not part of the type declaration for the
1264 -- candidate type, which would otherwise be invisible.
1266 -------------
1267 -- In_Decl --
1268 -------------
1270 function In_Decl return Boolean is
1271 Decl_Node : constant Node_Id := Parent (E);
1272 N2 : Node_Id;
1274 begin
1275 N2 := N;
1277 if Etype (E) = Any_Type then
1278 return True;
1280 elsif No (Decl_Node) then
1281 return False;
1283 else
1284 while Present (N2)
1285 and then Nkind (N2) /= N_Compilation_Unit
1286 loop
1287 if N2 = Decl_Node then
1288 return True;
1289 else
1290 N2 := Parent (N2);
1291 end if;
1292 end loop;
1294 return False;
1295 end if;
1296 end In_Decl;
1298 -- Start of processing for Type_In_P
1300 begin
1301 -- If the context type is declared in the prefix package, this is the
1302 -- desired base type.
1304 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1305 return Base_Type (Typ);
1307 else
1308 E := First_Entity (Pack);
1309 while Present (E) loop
1310 if Test (E)
1311 and then not In_Decl
1312 then
1313 return E;
1314 end if;
1316 Next_Entity (E);
1317 end loop;
1319 return Empty;
1320 end if;
1321 end Type_In_P;
1323 -- Start of processing for Make_Call_Into_Operator
1325 begin
1326 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1328 -- Binary operator
1330 if Is_Binary then
1331 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1332 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1333 Save_Interps (Act1, Left_Opnd (Op_Node));
1334 Save_Interps (Act2, Right_Opnd (Op_Node));
1335 Act1 := Left_Opnd (Op_Node);
1336 Act2 := Right_Opnd (Op_Node);
1338 -- Unary operator
1340 else
1341 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1342 Save_Interps (Act1, Right_Opnd (Op_Node));
1343 Act1 := Right_Opnd (Op_Node);
1344 end if;
1346 -- If the operator is denoted by an expanded name, and the prefix is
1347 -- not Standard, but the operator is a predefined one whose scope is
1348 -- Standard, then this is an implicit_operator, inserted as an
1349 -- interpretation by the procedure of the same name. This procedure
1350 -- overestimates the presence of implicit operators, because it does
1351 -- not examine the type of the operands. Verify now that the operand
1352 -- type appears in the given scope. If right operand is universal,
1353 -- check the other operand. In the case of concatenation, either
1354 -- argument can be the component type, so check the type of the result.
1355 -- If both arguments are literals, look for a type of the right kind
1356 -- defined in the given scope. This elaborate nonsense is brought to
1357 -- you courtesy of b33302a. The type itself must be frozen, so we must
1358 -- find the type of the proper class in the given scope.
1360 -- A final wrinkle is the multiplication operator for fixed point types,
1361 -- which is defined in Standard only, and not in the scope of the
1362 -- fixed point type itself.
1364 if Nkind (Name (N)) = N_Expanded_Name then
1365 Pack := Entity (Prefix (Name (N)));
1367 -- If this is a package renaming, get renamed entity, which will be
1368 -- the scope of the operands if operaton is type-correct.
1370 if Present (Renamed_Entity (Pack)) then
1371 Pack := Renamed_Entity (Pack);
1372 end if;
1374 -- If the entity being called is defined in the given package, it is
1375 -- a renaming of a predefined operator, and known to be legal.
1377 if Scope (Entity (Name (N))) = Pack
1378 and then Pack /= Standard_Standard
1379 then
1380 null;
1382 -- Visibility does not need to be checked in an instance: if the
1383 -- operator was not visible in the generic it has been diagnosed
1384 -- already, else there is an implicit copy of it in the instance.
1386 elsif In_Instance then
1387 null;
1389 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1390 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1391 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1392 then
1393 if Pack /= Standard_Standard then
1394 Error := True;
1395 end if;
1397 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1398 -- available.
1400 elsif Ada_Version >= Ada_2005
1401 and then Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1402 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1403 then
1404 null;
1406 else
1407 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1409 if Op_Name = Name_Op_Concat then
1410 Opnd_Type := Base_Type (Typ);
1412 elsif (Scope (Opnd_Type) = Standard_Standard
1413 and then Is_Binary)
1414 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1415 and then Is_Binary
1416 and then not Comes_From_Source (Opnd_Type))
1417 then
1418 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1419 end if;
1421 if Scope (Opnd_Type) = Standard_Standard then
1423 -- Verify that the scope contains a type that corresponds to
1424 -- the given literal. Optimize the case where Pack is Standard.
1426 if Pack /= Standard_Standard then
1428 if Opnd_Type = Universal_Integer then
1429 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1431 elsif Opnd_Type = Universal_Real then
1432 Orig_Type := Type_In_P (Is_Real_Type'Access);
1434 elsif Opnd_Type = Any_String then
1435 Orig_Type := Type_In_P (Is_String_Type'Access);
1437 elsif Opnd_Type = Any_Access then
1438 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1440 elsif Opnd_Type = Any_Composite then
1441 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1443 if Present (Orig_Type) then
1444 if Has_Private_Component (Orig_Type) then
1445 Orig_Type := Empty;
1446 else
1447 Set_Etype (Act1, Orig_Type);
1449 if Is_Binary then
1450 Set_Etype (Act2, Orig_Type);
1451 end if;
1452 end if;
1453 end if;
1455 else
1456 Orig_Type := Empty;
1457 end if;
1459 Error := No (Orig_Type);
1460 end if;
1462 elsif Ekind (Opnd_Type) = E_Allocator_Type
1463 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1464 then
1465 Error := True;
1467 -- If the type is defined elsewhere, and the operator is not
1468 -- defined in the given scope (by a renaming declaration, e.g.)
1469 -- then this is an error as well. If an extension of System is
1470 -- present, and the type may be defined there, Pack must be
1471 -- System itself.
1473 elsif Scope (Opnd_Type) /= Pack
1474 and then Scope (Op_Id) /= Pack
1475 and then (No (System_Aux_Id)
1476 or else Scope (Opnd_Type) /= System_Aux_Id
1477 or else Pack /= Scope (System_Aux_Id))
1478 then
1479 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1480 Error := True;
1481 else
1482 Error := not Operand_Type_In_Scope (Pack);
1483 end if;
1485 elsif Pack = Standard_Standard
1486 and then not Operand_Type_In_Scope (Standard_Standard)
1487 then
1488 Error := True;
1489 end if;
1490 end if;
1492 if Error then
1493 Error_Msg_Node_2 := Pack;
1494 Error_Msg_NE
1495 ("& not declared in&", N, Selector_Name (Name (N)));
1496 Set_Etype (N, Any_Type);
1497 return;
1499 -- Detect a mismatch between the context type and the result type
1500 -- in the named package, which is otherwise not detected if the
1501 -- operands are universal. Check is only needed if source entity is
1502 -- an operator, not a function that renames an operator.
1504 elsif Nkind (Parent (N)) /= N_Type_Conversion
1505 and then Ekind (Entity (Name (N))) = E_Operator
1506 and then Is_Numeric_Type (Typ)
1507 and then not Is_Universal_Numeric_Type (Typ)
1508 and then Scope (Base_Type (Typ)) /= Pack
1509 and then not In_Instance
1510 then
1511 if Is_Fixed_Point_Type (Typ)
1512 and then Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1513 then
1514 -- Already checked above
1516 null;
1518 -- Operator may be defined in an extension of System
1520 elsif Present (System_Aux_Id)
1521 and then Scope (Opnd_Type) = System_Aux_Id
1522 then
1523 null;
1525 else
1526 -- Could we use Wrong_Type here??? (this would require setting
1527 -- Etype (N) to the actual type found where Typ was expected).
1529 Error_Msg_NE ("expect }", N, Typ);
1530 end if;
1531 end if;
1532 end if;
1534 Set_Chars (Op_Node, Op_Name);
1536 if not Is_Private_Type (Etype (N)) then
1537 Set_Etype (Op_Node, Base_Type (Etype (N)));
1538 else
1539 Set_Etype (Op_Node, Etype (N));
1540 end if;
1542 -- If this is a call to a function that renames a predefined equality,
1543 -- the renaming declaration provides a type that must be used to
1544 -- resolve the operands. This must be done now because resolution of
1545 -- the equality node will not resolve any remaining ambiguity, and it
1546 -- assumes that the first operand is not overloaded.
1548 if Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1549 and then Ekind (Func) = E_Function
1550 and then Is_Overloaded (Act1)
1551 then
1552 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1553 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1554 end if;
1556 Set_Entity (Op_Node, Op_Id);
1557 Generate_Reference (Op_Id, N, ' ');
1559 -- Do rewrite setting Comes_From_Source on the result if the original
1560 -- call came from source. Although it is not strictly the case that the
1561 -- operator as such comes from the source, logically it corresponds
1562 -- exactly to the function call in the source, so it should be marked
1563 -- this way (e.g. to make sure that validity checks work fine).
1565 declare
1566 CS : constant Boolean := Comes_From_Source (N);
1567 begin
1568 Rewrite (N, Op_Node);
1569 Set_Comes_From_Source (N, CS);
1570 end;
1572 -- If this is an arithmetic operator and the result type is private,
1573 -- the operands and the result must be wrapped in conversion to
1574 -- expose the underlying numeric type and expand the proper checks,
1575 -- e.g. on division.
1577 if Is_Private_Type (Typ) then
1578 case Nkind (N) is
1579 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1580 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1581 Resolve_Intrinsic_Operator (N, Typ);
1583 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1584 Resolve_Intrinsic_Unary_Operator (N, Typ);
1586 when others =>
1587 Resolve (N, Typ);
1588 end case;
1589 else
1590 Resolve (N, Typ);
1591 end if;
1593 -- If in ASIS_Mode, propagate operand types to original actuals of
1594 -- function call, which would otherwise not be fully resolved. If
1595 -- the call has already been constant-folded, nothing to do. We
1596 -- relocate the operand nodes rather than copy them, to preserve
1597 -- original_node pointers, given that the operands themselves may
1598 -- have been rewritten. If the call was itself a rewriting of an
1599 -- operator node, nothing to do.
1601 if ASIS_Mode
1602 and then Nkind (N) in N_Op
1603 and then Nkind (Original_Node (N)) = N_Function_Call
1604 then
1605 if Is_Binary then
1606 Rewrite (First (Parameter_Associations (Original_Node (N))),
1607 Relocate_Node (Left_Opnd (N)));
1608 Rewrite (Next (First (Parameter_Associations (Original_Node (N)))),
1609 Relocate_Node (Right_Opnd (N)));
1610 else
1611 Rewrite (First (Parameter_Associations (Original_Node (N))),
1612 Relocate_Node (Right_Opnd (N)));
1613 end if;
1615 Set_Parent (Original_Node (N), Parent (N));
1616 end if;
1617 end Make_Call_Into_Operator;
1619 -------------------
1620 -- Operator_Kind --
1621 -------------------
1623 function Operator_Kind
1624 (Op_Name : Name_Id;
1625 Is_Binary : Boolean) return Node_Kind
1627 Kind : Node_Kind;
1629 begin
1630 -- Use CASE statement or array???
1632 if Is_Binary then
1633 if Op_Name = Name_Op_And then
1634 Kind := N_Op_And;
1635 elsif Op_Name = Name_Op_Or then
1636 Kind := N_Op_Or;
1637 elsif Op_Name = Name_Op_Xor then
1638 Kind := N_Op_Xor;
1639 elsif Op_Name = Name_Op_Eq then
1640 Kind := N_Op_Eq;
1641 elsif Op_Name = Name_Op_Ne then
1642 Kind := N_Op_Ne;
1643 elsif Op_Name = Name_Op_Lt then
1644 Kind := N_Op_Lt;
1645 elsif Op_Name = Name_Op_Le then
1646 Kind := N_Op_Le;
1647 elsif Op_Name = Name_Op_Gt then
1648 Kind := N_Op_Gt;
1649 elsif Op_Name = Name_Op_Ge then
1650 Kind := N_Op_Ge;
1651 elsif Op_Name = Name_Op_Add then
1652 Kind := N_Op_Add;
1653 elsif Op_Name = Name_Op_Subtract then
1654 Kind := N_Op_Subtract;
1655 elsif Op_Name = Name_Op_Concat then
1656 Kind := N_Op_Concat;
1657 elsif Op_Name = Name_Op_Multiply then
1658 Kind := N_Op_Multiply;
1659 elsif Op_Name = Name_Op_Divide then
1660 Kind := N_Op_Divide;
1661 elsif Op_Name = Name_Op_Mod then
1662 Kind := N_Op_Mod;
1663 elsif Op_Name = Name_Op_Rem then
1664 Kind := N_Op_Rem;
1665 elsif Op_Name = Name_Op_Expon then
1666 Kind := N_Op_Expon;
1667 else
1668 raise Program_Error;
1669 end if;
1671 -- Unary operators
1673 else
1674 if Op_Name = Name_Op_Add then
1675 Kind := N_Op_Plus;
1676 elsif Op_Name = Name_Op_Subtract then
1677 Kind := N_Op_Minus;
1678 elsif Op_Name = Name_Op_Abs then
1679 Kind := N_Op_Abs;
1680 elsif Op_Name = Name_Op_Not then
1681 Kind := N_Op_Not;
1682 else
1683 raise Program_Error;
1684 end if;
1685 end if;
1687 return Kind;
1688 end Operator_Kind;
1690 ----------------------------
1691 -- Preanalyze_And_Resolve --
1692 ----------------------------
1694 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1695 Save_Full_Analysis : constant Boolean := Full_Analysis;
1697 begin
1698 Full_Analysis := False;
1699 Expander_Mode_Save_And_Set (False);
1701 -- Normally, we suppress all checks for this preanalysis. There is no
1702 -- point in processing them now, since they will be applied properly
1703 -- and in the proper location when the default expressions reanalyzed
1704 -- and reexpanded later on. We will also have more information at that
1705 -- point for possible suppression of individual checks.
1707 -- However, in SPARK mode, most expansion is suppressed, and this
1708 -- later reanalysis and reexpansion may not occur. SPARK mode does
1709 -- require the setting of checking flags for proof purposes, so we
1710 -- do the SPARK preanalysis without suppressing checks.
1712 -- This special handling for SPARK mode is required for example in the
1713 -- case of Ada 2012 constructs such as quantified expressions, which are
1714 -- expanded in two separate steps.
1716 if GNATprove_Mode then
1717 Analyze_And_Resolve (N, T);
1718 else
1719 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1720 end if;
1722 Expander_Mode_Restore;
1723 Full_Analysis := Save_Full_Analysis;
1724 end Preanalyze_And_Resolve;
1726 -- Version without context type
1728 procedure Preanalyze_And_Resolve (N : Node_Id) is
1729 Save_Full_Analysis : constant Boolean := Full_Analysis;
1731 begin
1732 Full_Analysis := False;
1733 Expander_Mode_Save_And_Set (False);
1735 Analyze (N);
1736 Resolve (N, Etype (N), Suppress => All_Checks);
1738 Expander_Mode_Restore;
1739 Full_Analysis := Save_Full_Analysis;
1740 end Preanalyze_And_Resolve;
1742 ----------------------------------
1743 -- Replace_Actual_Discriminants --
1744 ----------------------------------
1746 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1747 Loc : constant Source_Ptr := Sloc (N);
1748 Tsk : Node_Id := Empty;
1750 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1751 -- Comment needed???
1753 -------------------
1754 -- Process_Discr --
1755 -------------------
1757 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1758 Ent : Entity_Id;
1760 begin
1761 if Nkind (Nod) = N_Identifier then
1762 Ent := Entity (Nod);
1764 if Present (Ent)
1765 and then Ekind (Ent) = E_Discriminant
1766 then
1767 Rewrite (Nod,
1768 Make_Selected_Component (Loc,
1769 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1770 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1772 Set_Etype (Nod, Etype (Ent));
1773 end if;
1775 end if;
1777 return OK;
1778 end Process_Discr;
1780 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1782 -- Start of processing for Replace_Actual_Discriminants
1784 begin
1785 if not Expander_Active then
1786 return;
1787 end if;
1789 if Nkind (Name (N)) = N_Selected_Component then
1790 Tsk := Prefix (Name (N));
1792 elsif Nkind (Name (N)) = N_Indexed_Component then
1793 Tsk := Prefix (Prefix (Name (N)));
1794 end if;
1796 if No (Tsk) then
1797 return;
1798 else
1799 Replace_Discrs (Default);
1800 end if;
1801 end Replace_Actual_Discriminants;
1803 -------------
1804 -- Resolve --
1805 -------------
1807 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1808 Ambiguous : Boolean := False;
1809 Ctx_Type : Entity_Id := Typ;
1810 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1811 Err_Type : Entity_Id := Empty;
1812 Found : Boolean := False;
1813 From_Lib : Boolean;
1814 I : Interp_Index;
1815 I1 : Interp_Index := 0; -- prevent junk warning
1816 It : Interp;
1817 It1 : Interp;
1818 Seen : Entity_Id := Empty; -- prevent junk warning
1820 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1821 -- Determine whether a node comes from a predefined library unit or
1822 -- Standard.
1824 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1825 -- Try and fix up a literal so that it matches its expected type. New
1826 -- literals are manufactured if necessary to avoid cascaded errors.
1828 procedure Report_Ambiguous_Argument;
1829 -- Additional diagnostics when an ambiguous call has an ambiguous
1830 -- argument (typically a controlling actual).
1832 procedure Resolution_Failed;
1833 -- Called when attempt at resolving current expression fails
1835 ------------------------------------
1836 -- Comes_From_Predefined_Lib_Unit --
1837 -------------------------------------
1839 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1840 begin
1841 return
1842 Sloc (Nod) = Standard_Location
1843 or else Is_Predefined_File_Name
1844 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1845 end Comes_From_Predefined_Lib_Unit;
1847 --------------------
1848 -- Patch_Up_Value --
1849 --------------------
1851 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1852 begin
1853 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1854 Rewrite (N,
1855 Make_Real_Literal (Sloc (N),
1856 Realval => UR_From_Uint (Intval (N))));
1857 Set_Etype (N, Universal_Real);
1858 Set_Is_Static_Expression (N);
1860 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1861 Rewrite (N,
1862 Make_Integer_Literal (Sloc (N),
1863 Intval => UR_To_Uint (Realval (N))));
1864 Set_Etype (N, Universal_Integer);
1865 Set_Is_Static_Expression (N);
1867 elsif Nkind (N) = N_String_Literal
1868 and then Is_Character_Type (Typ)
1869 then
1870 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1871 Rewrite (N,
1872 Make_Character_Literal (Sloc (N),
1873 Chars => Name_Find,
1874 Char_Literal_Value =>
1875 UI_From_Int (Character'Pos ('A'))));
1876 Set_Etype (N, Any_Character);
1877 Set_Is_Static_Expression (N);
1879 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1880 Rewrite (N,
1881 Make_String_Literal (Sloc (N),
1882 Strval => End_String));
1884 elsif Nkind (N) = N_Range then
1885 Patch_Up_Value (Low_Bound (N), Typ);
1886 Patch_Up_Value (High_Bound (N), Typ);
1887 end if;
1888 end Patch_Up_Value;
1890 -------------------------------
1891 -- Report_Ambiguous_Argument --
1892 -------------------------------
1894 procedure Report_Ambiguous_Argument is
1895 Arg : constant Node_Id := First (Parameter_Associations (N));
1896 I : Interp_Index;
1897 It : Interp;
1899 begin
1900 if Nkind (Arg) = N_Function_Call
1901 and then Is_Entity_Name (Name (Arg))
1902 and then Is_Overloaded (Name (Arg))
1903 then
1904 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1906 -- Could use comments on what is going on here???
1908 Get_First_Interp (Name (Arg), I, It);
1909 while Present (It.Nam) loop
1910 Error_Msg_Sloc := Sloc (It.Nam);
1912 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1913 Error_Msg_N ("interpretation (inherited) #!", Arg);
1914 else
1915 Error_Msg_N ("interpretation #!", Arg);
1916 end if;
1918 Get_Next_Interp (I, It);
1919 end loop;
1920 end if;
1921 end Report_Ambiguous_Argument;
1923 -----------------------
1924 -- Resolution_Failed --
1925 -----------------------
1927 procedure Resolution_Failed is
1928 begin
1929 Patch_Up_Value (N, Typ);
1930 Set_Etype (N, Typ);
1931 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1932 Set_Is_Overloaded (N, False);
1934 -- The caller will return without calling the expander, so we need
1935 -- to set the analyzed flag. Note that it is fine to set Analyzed
1936 -- to True even if we are in the middle of a shallow analysis,
1937 -- (see the spec of sem for more details) since this is an error
1938 -- situation anyway, and there is no point in repeating the
1939 -- analysis later (indeed it won't work to repeat it later, since
1940 -- we haven't got a clear resolution of which entity is being
1941 -- referenced.)
1943 Set_Analyzed (N, True);
1944 return;
1945 end Resolution_Failed;
1947 -- Start of processing for Resolve
1949 begin
1950 if N = Error then
1951 return;
1952 end if;
1954 -- Access attribute on remote subprogram cannot be used for a non-remote
1955 -- access-to-subprogram type.
1957 if Nkind (N) = N_Attribute_Reference
1958 and then Nam_In (Attribute_Name (N), Name_Access,
1959 Name_Unrestricted_Access,
1960 Name_Unchecked_Access)
1961 and then Comes_From_Source (N)
1962 and then Is_Entity_Name (Prefix (N))
1963 and then Is_Subprogram (Entity (Prefix (N)))
1964 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1965 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1966 then
1967 Error_Msg_N
1968 ("prefix must statically denote a non-remote subprogram", N);
1969 end if;
1971 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1973 -- If the context is a Remote_Access_To_Subprogram, access attributes
1974 -- must be resolved with the corresponding fat pointer. There is no need
1975 -- to check for the attribute name since the return type of an
1976 -- attribute is never a remote type.
1978 if Nkind (N) = N_Attribute_Reference
1979 and then Comes_From_Source (N)
1980 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1981 then
1982 declare
1983 Attr : constant Attribute_Id :=
1984 Get_Attribute_Id (Attribute_Name (N));
1985 Pref : constant Node_Id := Prefix (N);
1986 Decl : Node_Id;
1987 Spec : Node_Id;
1988 Is_Remote : Boolean := True;
1990 begin
1991 -- Check that Typ is a remote access-to-subprogram type
1993 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1995 -- Prefix (N) must statically denote a remote subprogram
1996 -- declared in a package specification.
1998 if Attr = Attribute_Access or else
1999 Attr = Attribute_Unchecked_Access or else
2000 Attr = Attribute_Unrestricted_Access
2001 then
2002 Decl := Unit_Declaration_Node (Entity (Pref));
2004 if Nkind (Decl) = N_Subprogram_Body then
2005 Spec := Corresponding_Spec (Decl);
2007 if Present (Spec) then
2008 Decl := Unit_Declaration_Node (Spec);
2009 end if;
2010 end if;
2012 Spec := Parent (Decl);
2014 if not Is_Entity_Name (Prefix (N))
2015 or else Nkind (Spec) /= N_Package_Specification
2016 or else
2017 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2018 then
2019 Is_Remote := False;
2020 Error_Msg_N
2021 ("prefix must statically denote a remote subprogram ",
2023 end if;
2025 -- If we are generating code in distributed mode, perform
2026 -- semantic checks against corresponding remote entities.
2028 if Expander_Active
2029 and then Get_PCS_Name /= Name_No_DSA
2030 then
2031 Check_Subtype_Conformant
2032 (New_Id => Entity (Prefix (N)),
2033 Old_Id => Designated_Type
2034 (Corresponding_Remote_Type (Typ)),
2035 Err_Loc => N);
2037 if Is_Remote then
2038 Process_Remote_AST_Attribute (N, Typ);
2039 end if;
2040 end if;
2041 end if;
2042 end if;
2043 end;
2044 end if;
2046 Debug_A_Entry ("resolving ", N);
2048 if Debug_Flag_V then
2049 Write_Overloads (N);
2050 end if;
2052 if Comes_From_Source (N) then
2053 if Is_Fixed_Point_Type (Typ) then
2054 Check_Restriction (No_Fixed_Point, N);
2056 elsif Is_Floating_Point_Type (Typ)
2057 and then Typ /= Universal_Real
2058 and then Typ /= Any_Real
2059 then
2060 Check_Restriction (No_Floating_Point, N);
2061 end if;
2062 end if;
2064 -- Return if already analyzed
2066 if Analyzed (N) then
2067 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2068 Analyze_Dimension (N);
2069 return;
2071 -- Any case of Any_Type as the Etype value means that we had a
2072 -- previous error.
2074 elsif Etype (N) = Any_Type then
2075 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2076 return;
2077 end if;
2079 Check_Parameterless_Call (N);
2081 -- The resolution of an Expression_With_Actions is determined by
2082 -- its Expression.
2084 if Nkind (N) = N_Expression_With_Actions then
2085 Resolve (Expression (N), Typ);
2087 Found := True;
2088 Expr_Type := Etype (Expression (N));
2090 -- If not overloaded, then we know the type, and all that needs doing
2091 -- is to check that this type is compatible with the context.
2093 elsif not Is_Overloaded (N) then
2094 Found := Covers (Typ, Etype (N));
2095 Expr_Type := Etype (N);
2097 -- In the overloaded case, we must select the interpretation that
2098 -- is compatible with the context (i.e. the type passed to Resolve)
2100 else
2101 -- Loop through possible interpretations
2103 Get_First_Interp (N, I, It);
2104 Interp_Loop : while Present (It.Typ) loop
2106 if Debug_Flag_V then
2107 Write_Str ("Interp: ");
2108 Write_Interp (It);
2109 end if;
2111 -- We are only interested in interpretations that are compatible
2112 -- with the expected type, any other interpretations are ignored.
2114 if not Covers (Typ, It.Typ) then
2115 if Debug_Flag_V then
2116 Write_Str (" interpretation incompatible with context");
2117 Write_Eol;
2118 end if;
2120 else
2121 -- Skip the current interpretation if it is disabled by an
2122 -- abstract operator. This action is performed only when the
2123 -- type against which we are resolving is the same as the
2124 -- type of the interpretation.
2126 if Ada_Version >= Ada_2005
2127 and then It.Typ = Typ
2128 and then Typ /= Universal_Integer
2129 and then Typ /= Universal_Real
2130 and then Present (It.Abstract_Op)
2131 then
2132 if Debug_Flag_V then
2133 Write_Line ("Skip.");
2134 end if;
2136 goto Continue;
2137 end if;
2139 -- First matching interpretation
2141 if not Found then
2142 Found := True;
2143 I1 := I;
2144 Seen := It.Nam;
2145 Expr_Type := It.Typ;
2147 -- Matching interpretation that is not the first, maybe an
2148 -- error, but there are some cases where preference rules are
2149 -- used to choose between the two possibilities. These and
2150 -- some more obscure cases are handled in Disambiguate.
2152 else
2153 -- If the current statement is part of a predefined library
2154 -- unit, then all interpretations which come from user level
2155 -- packages should not be considered. Check previous and
2156 -- current one.
2158 if From_Lib then
2159 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2160 goto Continue;
2162 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2164 -- Previous interpretation must be discarded
2166 I1 := I;
2167 Seen := It.Nam;
2168 Expr_Type := It.Typ;
2169 Set_Entity (N, Seen);
2170 goto Continue;
2171 end if;
2172 end if;
2174 -- Otherwise apply further disambiguation steps
2176 Error_Msg_Sloc := Sloc (Seen);
2177 It1 := Disambiguate (N, I1, I, Typ);
2179 -- Disambiguation has succeeded. Skip the remaining
2180 -- interpretations.
2182 if It1 /= No_Interp then
2183 Seen := It1.Nam;
2184 Expr_Type := It1.Typ;
2186 while Present (It.Typ) loop
2187 Get_Next_Interp (I, It);
2188 end loop;
2190 else
2191 -- Before we issue an ambiguity complaint, check for
2192 -- the case of a subprogram call where at least one
2193 -- of the arguments is Any_Type, and if so, suppress
2194 -- the message, since it is a cascaded error.
2196 if Nkind (N) in N_Subprogram_Call then
2197 declare
2198 A : Node_Id;
2199 E : Node_Id;
2201 begin
2202 A := First_Actual (N);
2203 while Present (A) loop
2204 E := A;
2206 if Nkind (E) = N_Parameter_Association then
2207 E := Explicit_Actual_Parameter (E);
2208 end if;
2210 if Etype (E) = Any_Type then
2211 if Debug_Flag_V then
2212 Write_Str ("Any_Type in call");
2213 Write_Eol;
2214 end if;
2216 exit Interp_Loop;
2217 end if;
2219 Next_Actual (A);
2220 end loop;
2221 end;
2223 elsif Nkind (N) in N_Binary_Op
2224 and then (Etype (Left_Opnd (N)) = Any_Type
2225 or else Etype (Right_Opnd (N)) = Any_Type)
2226 then
2227 exit Interp_Loop;
2229 elsif Nkind (N) in N_Unary_Op
2230 and then Etype (Right_Opnd (N)) = Any_Type
2231 then
2232 exit Interp_Loop;
2233 end if;
2235 -- Not that special case, so issue message using the
2236 -- flag Ambiguous to control printing of the header
2237 -- message only at the start of an ambiguous set.
2239 if not Ambiguous then
2240 if Nkind (N) = N_Function_Call
2241 and then Nkind (Name (N)) = N_Explicit_Dereference
2242 then
2243 Error_Msg_N
2244 ("ambiguous expression "
2245 & "(cannot resolve indirect call)!", N);
2246 else
2247 Error_Msg_NE -- CODEFIX
2248 ("ambiguous expression (cannot resolve&)!",
2249 N, It.Nam);
2250 end if;
2252 Ambiguous := True;
2254 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2255 Error_Msg_N
2256 ("\\possible interpretation (inherited)#!", N);
2257 else
2258 Error_Msg_N -- CODEFIX
2259 ("\\possible interpretation#!", N);
2260 end if;
2262 if Nkind (N) in N_Subprogram_Call
2263 and then Present (Parameter_Associations (N))
2264 then
2265 Report_Ambiguous_Argument;
2266 end if;
2267 end if;
2269 Error_Msg_Sloc := Sloc (It.Nam);
2271 -- By default, the error message refers to the candidate
2272 -- interpretation. But if it is a predefined operator, it
2273 -- is implicitly declared at the declaration of the type
2274 -- of the operand. Recover the sloc of that declaration
2275 -- for the error message.
2277 if Nkind (N) in N_Op
2278 and then Scope (It.Nam) = Standard_Standard
2279 and then not Is_Overloaded (Right_Opnd (N))
2280 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2281 Standard_Standard
2282 then
2283 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2285 if Comes_From_Source (Err_Type)
2286 and then Present (Parent (Err_Type))
2287 then
2288 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2289 end if;
2291 elsif Nkind (N) in N_Binary_Op
2292 and then Scope (It.Nam) = Standard_Standard
2293 and then not Is_Overloaded (Left_Opnd (N))
2294 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2295 Standard_Standard
2296 then
2297 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2299 if Comes_From_Source (Err_Type)
2300 and then Present (Parent (Err_Type))
2301 then
2302 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2303 end if;
2305 -- If this is an indirect call, use the subprogram_type
2306 -- in the message, to have a meaningful location. Also
2307 -- indicate if this is an inherited operation, created
2308 -- by a type declaration.
2310 elsif Nkind (N) = N_Function_Call
2311 and then Nkind (Name (N)) = N_Explicit_Dereference
2312 and then Is_Type (It.Nam)
2313 then
2314 Err_Type := It.Nam;
2315 Error_Msg_Sloc :=
2316 Sloc (Associated_Node_For_Itype (Err_Type));
2317 else
2318 Err_Type := Empty;
2319 end if;
2321 if Nkind (N) in N_Op
2322 and then Scope (It.Nam) = Standard_Standard
2323 and then Present (Err_Type)
2324 then
2325 -- Special-case the message for universal_fixed
2326 -- operators, which are not declared with the type
2327 -- of the operand, but appear forever in Standard.
2329 if It.Typ = Universal_Fixed
2330 and then Scope (It.Nam) = Standard_Standard
2331 then
2332 Error_Msg_N
2333 ("\\possible interpretation as universal_fixed "
2334 & "operation (RM 4.5.5 (19))", N);
2335 else
2336 Error_Msg_N
2337 ("\\possible interpretation (predefined)#!", N);
2338 end if;
2340 elsif
2341 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2342 then
2343 Error_Msg_N
2344 ("\\possible interpretation (inherited)#!", N);
2345 else
2346 Error_Msg_N -- CODEFIX
2347 ("\\possible interpretation#!", N);
2348 end if;
2350 end if;
2351 end if;
2353 -- We have a matching interpretation, Expr_Type is the type
2354 -- from this interpretation, and Seen is the entity.
2356 -- For an operator, just set the entity name. The type will be
2357 -- set by the specific operator resolution routine.
2359 if Nkind (N) in N_Op then
2360 Set_Entity (N, Seen);
2361 Generate_Reference (Seen, N);
2363 elsif Nkind (N) = N_Case_Expression then
2364 Set_Etype (N, Expr_Type);
2366 elsif Nkind (N) = N_Character_Literal then
2367 Set_Etype (N, Expr_Type);
2369 elsif Nkind (N) = N_If_Expression then
2370 Set_Etype (N, Expr_Type);
2372 -- AI05-0139-2: Expression is overloaded because type has
2373 -- implicit dereference. If type matches context, no implicit
2374 -- dereference is involved.
2376 elsif Has_Implicit_Dereference (Expr_Type) then
2377 Set_Etype (N, Expr_Type);
2378 Set_Is_Overloaded (N, False);
2379 exit Interp_Loop;
2381 elsif Is_Overloaded (N)
2382 and then Present (It.Nam)
2383 and then Ekind (It.Nam) = E_Discriminant
2384 and then Has_Implicit_Dereference (It.Nam)
2385 then
2386 -- If the node is a general indexing, the dereference is
2387 -- is inserted when resolving the rewritten form, else
2388 -- insert it now.
2390 if Nkind (N) /= N_Indexed_Component
2391 or else No (Generalized_Indexing (N))
2392 then
2393 Build_Explicit_Dereference (N, It.Nam);
2394 end if;
2396 -- For an explicit dereference, attribute reference, range,
2397 -- short-circuit form (which is not an operator node), or call
2398 -- with a name that is an explicit dereference, there is
2399 -- nothing to be done at this point.
2401 elsif Nkind_In (N, N_Explicit_Dereference,
2402 N_Attribute_Reference,
2403 N_And_Then,
2404 N_Indexed_Component,
2405 N_Or_Else,
2406 N_Range,
2407 N_Selected_Component,
2408 N_Slice)
2409 or else Nkind (Name (N)) = N_Explicit_Dereference
2410 then
2411 null;
2413 -- For procedure or function calls, set the type of the name,
2414 -- and also the entity pointer for the prefix.
2416 elsif Nkind (N) in N_Subprogram_Call
2417 and then Is_Entity_Name (Name (N))
2418 then
2419 Set_Etype (Name (N), Expr_Type);
2420 Set_Entity (Name (N), Seen);
2421 Generate_Reference (Seen, Name (N));
2423 elsif Nkind (N) = N_Function_Call
2424 and then Nkind (Name (N)) = N_Selected_Component
2425 then
2426 Set_Etype (Name (N), Expr_Type);
2427 Set_Entity (Selector_Name (Name (N)), Seen);
2428 Generate_Reference (Seen, Selector_Name (Name (N)));
2430 -- For all other cases, just set the type of the Name
2432 else
2433 Set_Etype (Name (N), Expr_Type);
2434 end if;
2436 end if;
2438 <<Continue>>
2440 -- Move to next interpretation
2442 exit Interp_Loop when No (It.Typ);
2444 Get_Next_Interp (I, It);
2445 end loop Interp_Loop;
2446 end if;
2448 -- At this stage Found indicates whether or not an acceptable
2449 -- interpretation exists. If not, then we have an error, except that if
2450 -- the context is Any_Type as a result of some other error, then we
2451 -- suppress the error report.
2453 if not Found then
2454 if Typ /= Any_Type then
2456 -- If type we are looking for is Void, then this is the procedure
2457 -- call case, and the error is simply that what we gave is not a
2458 -- procedure name (we think of procedure calls as expressions with
2459 -- types internally, but the user doesn't think of them this way).
2461 if Typ = Standard_Void_Type then
2463 -- Special case message if function used as a procedure
2465 if Nkind (N) = N_Procedure_Call_Statement
2466 and then Is_Entity_Name (Name (N))
2467 and then Ekind (Entity (Name (N))) = E_Function
2468 then
2469 Error_Msg_NE
2470 ("cannot use function & in a procedure call",
2471 Name (N), Entity (Name (N)));
2473 -- Otherwise give general message (not clear what cases this
2474 -- covers, but no harm in providing for them).
2476 else
2477 Error_Msg_N ("expect procedure name in procedure call", N);
2478 end if;
2480 Found := True;
2482 -- Otherwise we do have a subexpression with the wrong type
2484 -- Check for the case of an allocator which uses an access type
2485 -- instead of the designated type. This is a common error and we
2486 -- specialize the message, posting an error on the operand of the
2487 -- allocator, complaining that we expected the designated type of
2488 -- the allocator.
2490 elsif Nkind (N) = N_Allocator
2491 and then Is_Access_Type (Typ)
2492 and then Is_Access_Type (Etype (N))
2493 and then Designated_Type (Etype (N)) = Typ
2494 then
2495 Wrong_Type (Expression (N), Designated_Type (Typ));
2496 Found := True;
2498 -- Check for view mismatch on Null in instances, for which the
2499 -- view-swapping mechanism has no identifier.
2501 elsif (In_Instance or else In_Inlined_Body)
2502 and then (Nkind (N) = N_Null)
2503 and then Is_Private_Type (Typ)
2504 and then Is_Access_Type (Full_View (Typ))
2505 then
2506 Resolve (N, Full_View (Typ));
2507 Set_Etype (N, Typ);
2508 return;
2510 -- Check for an aggregate. Sometimes we can get bogus aggregates
2511 -- from misuse of parentheses, and we are about to complain about
2512 -- the aggregate without even looking inside it.
2514 -- Instead, if we have an aggregate of type Any_Composite, then
2515 -- analyze and resolve the component fields, and then only issue
2516 -- another message if we get no errors doing this (otherwise
2517 -- assume that the errors in the aggregate caused the problem).
2519 elsif Nkind (N) = N_Aggregate
2520 and then Etype (N) = Any_Composite
2521 then
2522 -- Disable expansion in any case. If there is a type mismatch
2523 -- it may be fatal to try to expand the aggregate. The flag
2524 -- would otherwise be set to false when the error is posted.
2526 Expander_Active := False;
2528 declare
2529 procedure Check_Aggr (Aggr : Node_Id);
2530 -- Check one aggregate, and set Found to True if we have a
2531 -- definite error in any of its elements
2533 procedure Check_Elmt (Aelmt : Node_Id);
2534 -- Check one element of aggregate and set Found to True if
2535 -- we definitely have an error in the element.
2537 ----------------
2538 -- Check_Aggr --
2539 ----------------
2541 procedure Check_Aggr (Aggr : Node_Id) is
2542 Elmt : Node_Id;
2544 begin
2545 if Present (Expressions (Aggr)) then
2546 Elmt := First (Expressions (Aggr));
2547 while Present (Elmt) loop
2548 Check_Elmt (Elmt);
2549 Next (Elmt);
2550 end loop;
2551 end if;
2553 if Present (Component_Associations (Aggr)) then
2554 Elmt := First (Component_Associations (Aggr));
2555 while Present (Elmt) loop
2557 -- If this is a default-initialized component, then
2558 -- there is nothing to check. The box will be
2559 -- replaced by the appropriate call during late
2560 -- expansion.
2562 if not Box_Present (Elmt) then
2563 Check_Elmt (Expression (Elmt));
2564 end if;
2566 Next (Elmt);
2567 end loop;
2568 end if;
2569 end Check_Aggr;
2571 ----------------
2572 -- Check_Elmt --
2573 ----------------
2575 procedure Check_Elmt (Aelmt : Node_Id) is
2576 begin
2577 -- If we have a nested aggregate, go inside it (to
2578 -- attempt a naked analyze-resolve of the aggregate can
2579 -- cause undesirable cascaded errors). Do not resolve
2580 -- expression if it needs a type from context, as for
2581 -- integer * fixed expression.
2583 if Nkind (Aelmt) = N_Aggregate then
2584 Check_Aggr (Aelmt);
2586 else
2587 Analyze (Aelmt);
2589 if not Is_Overloaded (Aelmt)
2590 and then Etype (Aelmt) /= Any_Fixed
2591 then
2592 Resolve (Aelmt);
2593 end if;
2595 if Etype (Aelmt) = Any_Type then
2596 Found := True;
2597 end if;
2598 end if;
2599 end Check_Elmt;
2601 begin
2602 Check_Aggr (N);
2603 end;
2604 end if;
2606 -- Looks like we have a type error, but check for special case
2607 -- of Address wanted, integer found, with the configuration pragma
2608 -- Allow_Integer_Address active. If we have this case, introduce
2609 -- an unchecked conversion to allow the integer expression to be
2610 -- treated as an Address. The reverse case of integer wanted,
2611 -- Address found, is treated in an analogous manner.
2613 if Address_Integer_Convert_OK (Typ, Etype (N)) then
2614 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
2615 Analyze_And_Resolve (N, Typ);
2616 return;
2617 end if;
2619 -- That special Allow_Integer_Address check did not appply, so we
2620 -- have a real type error. If an error message was issued already,
2621 -- Found got reset to True, so if it's still False, issue standard
2622 -- Wrong_Type message.
2624 if not Found then
2625 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
2626 declare
2627 Subp_Name : Node_Id;
2629 begin
2630 if Is_Entity_Name (Name (N)) then
2631 Subp_Name := Name (N);
2633 elsif Nkind (Name (N)) = N_Selected_Component then
2635 -- Protected operation: retrieve operation name
2637 Subp_Name := Selector_Name (Name (N));
2639 else
2640 raise Program_Error;
2641 end if;
2643 Error_Msg_Node_2 := Typ;
2644 Error_Msg_NE
2645 ("no visible interpretation of& "
2646 & "matches expected type&", N, Subp_Name);
2647 end;
2649 if All_Errors_Mode then
2650 declare
2651 Index : Interp_Index;
2652 It : Interp;
2654 begin
2655 Error_Msg_N ("\\possible interpretations:", N);
2657 Get_First_Interp (Name (N), Index, It);
2658 while Present (It.Nam) loop
2659 Error_Msg_Sloc := Sloc (It.Nam);
2660 Error_Msg_Node_2 := It.Nam;
2661 Error_Msg_NE
2662 ("\\ type& for & declared#", N, It.Typ);
2663 Get_Next_Interp (Index, It);
2664 end loop;
2665 end;
2667 else
2668 Error_Msg_N ("\use -gnatf for details", N);
2669 end if;
2671 else
2672 Wrong_Type (N, Typ);
2673 end if;
2674 end if;
2675 end if;
2677 Resolution_Failed;
2678 return;
2680 -- Test if we have more than one interpretation for the context
2682 elsif Ambiguous then
2683 Resolution_Failed;
2684 return;
2686 -- Only one intepretation
2688 else
2689 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2690 -- the "+" on T is abstract, and the operands are of universal type,
2691 -- the above code will have (incorrectly) resolved the "+" to the
2692 -- universal one in Standard. Therefore check for this case and give
2693 -- an error. We can't do this earlier, because it would cause legal
2694 -- cases to get errors (when some other type has an abstract "+").
2696 if Ada_Version >= Ada_2005
2697 and then Nkind (N) in N_Op
2698 and then Is_Overloaded (N)
2699 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
2700 then
2701 Get_First_Interp (N, I, It);
2702 while Present (It.Typ) loop
2703 if Present (It.Abstract_Op) and then
2704 Etype (It.Abstract_Op) = Typ
2705 then
2706 Error_Msg_NE
2707 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2708 return;
2709 end if;
2711 Get_Next_Interp (I, It);
2712 end loop;
2713 end if;
2715 -- Here we have an acceptable interpretation for the context
2717 -- Propagate type information and normalize tree for various
2718 -- predefined operations. If the context only imposes a class of
2719 -- types, rather than a specific type, propagate the actual type
2720 -- downward.
2722 if Typ = Any_Integer or else
2723 Typ = Any_Boolean or else
2724 Typ = Any_Modular or else
2725 Typ = Any_Real or else
2726 Typ = Any_Discrete
2727 then
2728 Ctx_Type := Expr_Type;
2730 -- Any_Fixed is legal in a real context only if a specific fixed-
2731 -- point type is imposed. If Norman Cohen can be confused by this,
2732 -- it deserves a separate message.
2734 if Typ = Any_Real
2735 and then Expr_Type = Any_Fixed
2736 then
2737 Error_Msg_N ("illegal context for mixed mode operation", N);
2738 Set_Etype (N, Universal_Real);
2739 Ctx_Type := Universal_Real;
2740 end if;
2741 end if;
2743 -- A user-defined operator is transformed into a function call at
2744 -- this point, so that further processing knows that operators are
2745 -- really operators (i.e. are predefined operators). User-defined
2746 -- operators that are intrinsic are just renamings of the predefined
2747 -- ones, and need not be turned into calls either, but if they rename
2748 -- a different operator, we must transform the node accordingly.
2749 -- Instantiations of Unchecked_Conversion are intrinsic but are
2750 -- treated as functions, even if given an operator designator.
2752 if Nkind (N) in N_Op
2753 and then Present (Entity (N))
2754 and then Ekind (Entity (N)) /= E_Operator
2755 then
2757 if not Is_Predefined_Op (Entity (N)) then
2758 Rewrite_Operator_As_Call (N, Entity (N));
2760 elsif Present (Alias (Entity (N)))
2761 and then
2762 Nkind (Parent (Parent (Entity (N)))) =
2763 N_Subprogram_Renaming_Declaration
2764 then
2765 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2767 -- If the node is rewritten, it will be fully resolved in
2768 -- Rewrite_Renamed_Operator.
2770 if Analyzed (N) then
2771 return;
2772 end if;
2773 end if;
2774 end if;
2776 case N_Subexpr'(Nkind (N)) is
2778 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2780 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2782 when N_Short_Circuit
2783 => Resolve_Short_Circuit (N, Ctx_Type);
2785 when N_Attribute_Reference
2786 => Resolve_Attribute (N, Ctx_Type);
2788 when N_Case_Expression
2789 => Resolve_Case_Expression (N, Ctx_Type);
2791 when N_Character_Literal
2792 => Resolve_Character_Literal (N, Ctx_Type);
2794 when N_Expanded_Name
2795 => Resolve_Entity_Name (N, Ctx_Type);
2797 when N_Explicit_Dereference
2798 => Resolve_Explicit_Dereference (N, Ctx_Type);
2800 when N_Expression_With_Actions
2801 => Resolve_Expression_With_Actions (N, Ctx_Type);
2803 when N_Extension_Aggregate
2804 => Resolve_Extension_Aggregate (N, Ctx_Type);
2806 when N_Function_Call
2807 => Resolve_Call (N, Ctx_Type);
2809 when N_Identifier
2810 => Resolve_Entity_Name (N, Ctx_Type);
2812 when N_If_Expression
2813 => Resolve_If_Expression (N, Ctx_Type);
2815 when N_Indexed_Component
2816 => Resolve_Indexed_Component (N, Ctx_Type);
2818 when N_Integer_Literal
2819 => Resolve_Integer_Literal (N, Ctx_Type);
2821 when N_Membership_Test
2822 => Resolve_Membership_Op (N, Ctx_Type);
2824 when N_Null => Resolve_Null (N, Ctx_Type);
2826 when N_Op_And | N_Op_Or | N_Op_Xor
2827 => Resolve_Logical_Op (N, Ctx_Type);
2829 when N_Op_Eq | N_Op_Ne
2830 => Resolve_Equality_Op (N, Ctx_Type);
2832 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2833 => Resolve_Comparison_Op (N, Ctx_Type);
2835 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2837 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2838 N_Op_Divide | N_Op_Mod | N_Op_Rem
2840 => Resolve_Arithmetic_Op (N, Ctx_Type);
2842 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2844 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2846 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2847 => Resolve_Unary_Op (N, Ctx_Type);
2849 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2851 when N_Procedure_Call_Statement
2852 => Resolve_Call (N, Ctx_Type);
2854 when N_Operator_Symbol
2855 => Resolve_Operator_Symbol (N, Ctx_Type);
2857 when N_Qualified_Expression
2858 => Resolve_Qualified_Expression (N, Ctx_Type);
2860 -- Why is the following null, needs a comment ???
2862 when N_Quantified_Expression
2863 => null;
2865 when N_Raise_Expression
2866 => Resolve_Raise_Expression (N, Ctx_Type);
2868 when N_Raise_xxx_Error
2869 => Set_Etype (N, Ctx_Type);
2871 when N_Range => Resolve_Range (N, Ctx_Type);
2873 when N_Real_Literal
2874 => Resolve_Real_Literal (N, Ctx_Type);
2876 when N_Reference => Resolve_Reference (N, Ctx_Type);
2878 when N_Selected_Component
2879 => Resolve_Selected_Component (N, Ctx_Type);
2881 when N_Slice => Resolve_Slice (N, Ctx_Type);
2883 when N_String_Literal
2884 => Resolve_String_Literal (N, Ctx_Type);
2886 when N_Type_Conversion
2887 => Resolve_Type_Conversion (N, Ctx_Type);
2889 when N_Unchecked_Expression =>
2890 Resolve_Unchecked_Expression (N, Ctx_Type);
2892 when N_Unchecked_Type_Conversion =>
2893 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2894 end case;
2896 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2897 -- expression of an anonymous access type that occurs in the context
2898 -- of a named general access type, except when the expression is that
2899 -- of a membership test. This ensures proper legality checking in
2900 -- terms of allowed conversions (expressions that would be illegal to
2901 -- convert implicitly are allowed in membership tests).
2903 if Ada_Version >= Ada_2012
2904 and then Ekind (Ctx_Type) = E_General_Access_Type
2905 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2906 and then Nkind (Parent (N)) not in N_Membership_Test
2907 then
2908 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2909 Analyze_And_Resolve (N, Ctx_Type);
2910 end if;
2912 -- If the subexpression was replaced by a non-subexpression, then
2913 -- all we do is to expand it. The only legitimate case we know of
2914 -- is converting procedure call statement to entry call statements,
2915 -- but there may be others, so we are making this test general.
2917 if Nkind (N) not in N_Subexpr then
2918 Debug_A_Exit ("resolving ", N, " (done)");
2919 Expand (N);
2920 return;
2921 end if;
2923 -- The expression is definitely NOT overloaded at this point, so
2924 -- we reset the Is_Overloaded flag to avoid any confusion when
2925 -- reanalyzing the node.
2927 Set_Is_Overloaded (N, False);
2929 -- Freeze expression type, entity if it is a name, and designated
2930 -- type if it is an allocator (RM 13.14(10,11,13)).
2932 -- Now that the resolution of the type of the node is complete, and
2933 -- we did not detect an error, we can expand this node. We skip the
2934 -- expand call if we are in a default expression, see section
2935 -- "Handling of Default Expressions" in Sem spec.
2937 Debug_A_Exit ("resolving ", N, " (done)");
2939 -- We unconditionally freeze the expression, even if we are in
2940 -- default expression mode (the Freeze_Expression routine tests this
2941 -- flag and only freezes static types if it is set).
2943 -- Ada 2012 (AI05-177): The declaration of an expression function
2944 -- does not cause freezing, but we never reach here in that case.
2945 -- Here we are resolving the corresponding expanded body, so we do
2946 -- need to perform normal freezing.
2948 Freeze_Expression (N);
2950 -- Now we can do the expansion
2952 Expand (N);
2953 end if;
2954 end Resolve;
2956 -------------
2957 -- Resolve --
2958 -------------
2960 -- Version with check(s) suppressed
2962 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2963 begin
2964 if Suppress = All_Checks then
2965 declare
2966 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
2967 begin
2968 Scope_Suppress.Suppress := (others => True);
2969 Resolve (N, Typ);
2970 Scope_Suppress.Suppress := Sva;
2971 end;
2973 else
2974 declare
2975 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
2976 begin
2977 Scope_Suppress.Suppress (Suppress) := True;
2978 Resolve (N, Typ);
2979 Scope_Suppress.Suppress (Suppress) := Svg;
2980 end;
2981 end if;
2982 end Resolve;
2984 -------------
2985 -- Resolve --
2986 -------------
2988 -- Version with implicit type
2990 procedure Resolve (N : Node_Id) is
2991 begin
2992 Resolve (N, Etype (N));
2993 end Resolve;
2995 ---------------------
2996 -- Resolve_Actuals --
2997 ---------------------
2999 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3000 Loc : constant Source_Ptr := Sloc (N);
3001 A : Node_Id;
3002 A_Id : Entity_Id;
3003 A_Typ : Entity_Id;
3004 F : Entity_Id;
3005 F_Typ : Entity_Id;
3006 Prev : Node_Id := Empty;
3007 Orig_A : Node_Id;
3009 procedure Check_Aliased_Parameter;
3010 -- Check rules on aliased parameters and related accessibility rules
3011 -- in (RM 3.10.2 (10.2-10.4)).
3013 procedure Check_Argument_Order;
3014 -- Performs a check for the case where the actuals are all simple
3015 -- identifiers that correspond to the formal names, but in the wrong
3016 -- order, which is considered suspicious and cause for a warning.
3018 procedure Check_Prefixed_Call;
3019 -- If the original node is an overloaded call in prefix notation,
3020 -- insert an 'Access or a dereference as needed over the first actual.
3021 -- Try_Object_Operation has already verified that there is a valid
3022 -- interpretation, but the form of the actual can only be determined
3023 -- once the primitive operation is identified.
3025 procedure Insert_Default;
3026 -- If the actual is missing in a call, insert in the actuals list
3027 -- an instance of the default expression. The insertion is always
3028 -- a named association.
3030 procedure Property_Error
3031 (Var : Node_Id;
3032 Var_Id : Entity_Id;
3033 Prop_Nam : Name_Id);
3034 -- Emit an error concerning variable Var with entity Var_Id that has
3035 -- enabled property Prop_Nam when it acts as an actual parameter in a
3036 -- call and the corresponding formal parameter is of mode IN.
3038 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3039 -- Check whether T1 and T2, or their full views, are derived from a
3040 -- common type. Used to enforce the restrictions on array conversions
3041 -- of AI95-00246.
3043 function Static_Concatenation (N : Node_Id) return Boolean;
3044 -- Predicate to determine whether an actual that is a concatenation
3045 -- will be evaluated statically and does not need a transient scope.
3046 -- This must be determined before the actual is resolved and expanded
3047 -- because if needed the transient scope must be introduced earlier.
3049 ------------------------------
3050 -- Check_Aliased_Parameter --
3051 ------------------------------
3053 procedure Check_Aliased_Parameter is
3054 Nominal_Subt : Entity_Id;
3056 begin
3057 if Is_Aliased (F) then
3058 if Is_Tagged_Type (A_Typ) then
3059 null;
3061 elsif Is_Aliased_View (A) then
3062 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3063 Nominal_Subt := Base_Type (A_Typ);
3064 else
3065 Nominal_Subt := A_Typ;
3066 end if;
3068 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3069 null;
3071 -- In a generic body assume the worst for generic formals:
3072 -- they can have a constrained partial view (AI05-041).
3074 elsif Has_Discriminants (F_Typ)
3075 and then not Is_Constrained (F_Typ)
3076 and then not Has_Constrained_Partial_View (F_Typ)
3077 and then not Is_Generic_Type (F_Typ)
3078 then
3079 null;
3081 else
3082 Error_Msg_NE ("untagged actual does not match "
3083 & "aliased formal&", A, F);
3084 end if;
3086 else
3087 Error_Msg_NE ("actual for aliased formal& must be "
3088 & "aliased object", A, F);
3089 end if;
3091 if Ekind (Nam) = E_Procedure then
3092 null;
3094 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3095 if Nkind (Parent (N)) = N_Type_Conversion
3096 and then Type_Access_Level (Etype (Parent (N))) <
3097 Object_Access_Level (A)
3098 then
3099 Error_Msg_N ("aliased actual has wrong accessibility", A);
3100 end if;
3102 elsif Nkind (Parent (N)) = N_Qualified_Expression
3103 and then Nkind (Parent (Parent (N))) = N_Allocator
3104 and then Type_Access_Level (Etype (Parent (Parent (N)))) <
3105 Object_Access_Level (A)
3106 then
3107 Error_Msg_N
3108 ("aliased actual in allocator has wrong accessibility", A);
3109 end if;
3110 end if;
3111 end Check_Aliased_Parameter;
3113 --------------------------
3114 -- Check_Argument_Order --
3115 --------------------------
3117 procedure Check_Argument_Order is
3118 begin
3119 -- Nothing to do if no parameters, or original node is neither a
3120 -- function call nor a procedure call statement (happens in the
3121 -- operator-transformed-to-function call case), or the call does
3122 -- not come from source, or this warning is off.
3124 if not Warn_On_Parameter_Order
3125 or else No (Parameter_Associations (N))
3126 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3127 or else not Comes_From_Source (N)
3128 then
3129 return;
3130 end if;
3132 declare
3133 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3135 begin
3136 -- Nothing to do if only one parameter
3138 if Nargs < 2 then
3139 return;
3140 end if;
3142 -- Here if at least two arguments
3144 declare
3145 Actuals : array (1 .. Nargs) of Node_Id;
3146 Actual : Node_Id;
3147 Formal : Node_Id;
3149 Wrong_Order : Boolean := False;
3150 -- Set True if an out of order case is found
3152 begin
3153 -- Collect identifier names of actuals, fail if any actual is
3154 -- not a simple identifier, and record max length of name.
3156 Actual := First (Parameter_Associations (N));
3157 for J in Actuals'Range loop
3158 if Nkind (Actual) /= N_Identifier then
3159 return;
3160 else
3161 Actuals (J) := Actual;
3162 Next (Actual);
3163 end if;
3164 end loop;
3166 -- If we got this far, all actuals are identifiers and the list
3167 -- of their names is stored in the Actuals array.
3169 Formal := First_Formal (Nam);
3170 for J in Actuals'Range loop
3172 -- If we ran out of formals, that's odd, probably an error
3173 -- which will be detected elsewhere, but abandon the search.
3175 if No (Formal) then
3176 return;
3177 end if;
3179 -- If name matches and is in order OK
3181 if Chars (Formal) = Chars (Actuals (J)) then
3182 null;
3184 else
3185 -- If no match, see if it is elsewhere in list and if so
3186 -- flag potential wrong order if type is compatible.
3188 for K in Actuals'Range loop
3189 if Chars (Formal) = Chars (Actuals (K))
3190 and then
3191 Has_Compatible_Type (Actuals (K), Etype (Formal))
3192 then
3193 Wrong_Order := True;
3194 goto Continue;
3195 end if;
3196 end loop;
3198 -- No match
3200 return;
3201 end if;
3203 <<Continue>> Next_Formal (Formal);
3204 end loop;
3206 -- If Formals left over, also probably an error, skip warning
3208 if Present (Formal) then
3209 return;
3210 end if;
3212 -- Here we give the warning if something was out of order
3214 if Wrong_Order then
3215 Error_Msg_N
3216 ("?P?actuals for this call may be in wrong order", N);
3217 end if;
3218 end;
3219 end;
3220 end Check_Argument_Order;
3222 -------------------------
3223 -- Check_Prefixed_Call --
3224 -------------------------
3226 procedure Check_Prefixed_Call is
3227 Act : constant Node_Id := First_Actual (N);
3228 A_Type : constant Entity_Id := Etype (Act);
3229 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3230 Orig : constant Node_Id := Original_Node (N);
3231 New_A : Node_Id;
3233 begin
3234 -- Check whether the call is a prefixed call, with or without
3235 -- additional actuals.
3237 if Nkind (Orig) = N_Selected_Component
3238 or else
3239 (Nkind (Orig) = N_Indexed_Component
3240 and then Nkind (Prefix (Orig)) = N_Selected_Component
3241 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3242 and then Is_Entity_Name (Act)
3243 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3244 then
3245 if Is_Access_Type (A_Type)
3246 and then not Is_Access_Type (F_Type)
3247 then
3248 -- Introduce dereference on object in prefix
3250 New_A :=
3251 Make_Explicit_Dereference (Sloc (Act),
3252 Prefix => Relocate_Node (Act));
3253 Rewrite (Act, New_A);
3254 Analyze (Act);
3256 elsif Is_Access_Type (F_Type)
3257 and then not Is_Access_Type (A_Type)
3258 then
3259 -- Introduce an implicit 'Access in prefix
3261 if not Is_Aliased_View (Act) then
3262 Error_Msg_NE
3263 ("object in prefixed call to& must be aliased"
3264 & " (RM-2005 4.3.1 (13))",
3265 Prefix (Act), Nam);
3266 end if;
3268 Rewrite (Act,
3269 Make_Attribute_Reference (Loc,
3270 Attribute_Name => Name_Access,
3271 Prefix => Relocate_Node (Act)));
3272 end if;
3274 Analyze (Act);
3275 end if;
3276 end Check_Prefixed_Call;
3278 --------------------
3279 -- Insert_Default --
3280 --------------------
3282 procedure Insert_Default is
3283 Actval : Node_Id;
3284 Assoc : Node_Id;
3286 begin
3287 -- Missing argument in call, nothing to insert
3289 if No (Default_Value (F)) then
3290 return;
3292 else
3293 -- Note that we do a full New_Copy_Tree, so that any associated
3294 -- Itypes are properly copied. This may not be needed any more,
3295 -- but it does no harm as a safety measure. Defaults of a generic
3296 -- formal may be out of bounds of the corresponding actual (see
3297 -- cc1311b) and an additional check may be required.
3299 Actval :=
3300 New_Copy_Tree
3301 (Default_Value (F),
3302 New_Scope => Current_Scope,
3303 New_Sloc => Loc);
3305 if Is_Concurrent_Type (Scope (Nam))
3306 and then Has_Discriminants (Scope (Nam))
3307 then
3308 Replace_Actual_Discriminants (N, Actval);
3309 end if;
3311 if Is_Overloadable (Nam)
3312 and then Present (Alias (Nam))
3313 then
3314 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3315 and then not Is_Tagged_Type (Etype (F))
3316 then
3317 -- If default is a real literal, do not introduce a
3318 -- conversion whose effect may depend on the run-time
3319 -- size of universal real.
3321 if Nkind (Actval) = N_Real_Literal then
3322 Set_Etype (Actval, Base_Type (Etype (F)));
3323 else
3324 Actval := Unchecked_Convert_To (Etype (F), Actval);
3325 end if;
3326 end if;
3328 if Is_Scalar_Type (Etype (F)) then
3329 Enable_Range_Check (Actval);
3330 end if;
3332 Set_Parent (Actval, N);
3334 -- Resolve aggregates with their base type, to avoid scope
3335 -- anomalies: the subtype was first built in the subprogram
3336 -- declaration, and the current call may be nested.
3338 if Nkind (Actval) = N_Aggregate then
3339 Analyze_And_Resolve (Actval, Etype (F));
3340 else
3341 Analyze_And_Resolve (Actval, Etype (Actval));
3342 end if;
3344 else
3345 Set_Parent (Actval, N);
3347 -- See note above concerning aggregates
3349 if Nkind (Actval) = N_Aggregate
3350 and then Has_Discriminants (Etype (Actval))
3351 then
3352 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3354 -- Resolve entities with their own type, which may differ from
3355 -- the type of a reference in a generic context (the view
3356 -- swapping mechanism did not anticipate the re-analysis of
3357 -- default values in calls).
3359 elsif Is_Entity_Name (Actval) then
3360 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3362 else
3363 Analyze_And_Resolve (Actval, Etype (Actval));
3364 end if;
3365 end if;
3367 -- If default is a tag indeterminate function call, propagate tag
3368 -- to obtain proper dispatching.
3370 if Is_Controlling_Formal (F)
3371 and then Nkind (Default_Value (F)) = N_Function_Call
3372 then
3373 Set_Is_Controlling_Actual (Actval);
3374 end if;
3376 end if;
3378 -- If the default expression raises constraint error, then just
3379 -- silently replace it with an N_Raise_Constraint_Error node, since
3380 -- we already gave the warning on the subprogram spec. If node is
3381 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3382 -- the warnings removal machinery.
3384 if Raises_Constraint_Error (Actval)
3385 and then Nkind (Actval) /= N_Raise_Constraint_Error
3386 then
3387 Rewrite (Actval,
3388 Make_Raise_Constraint_Error (Loc,
3389 Reason => CE_Range_Check_Failed));
3390 Set_Raises_Constraint_Error (Actval);
3391 Set_Etype (Actval, Etype (F));
3392 end if;
3394 Assoc :=
3395 Make_Parameter_Association (Loc,
3396 Explicit_Actual_Parameter => Actval,
3397 Selector_Name => Make_Identifier (Loc, Chars (F)));
3399 -- Case of insertion is first named actual
3401 if No (Prev) or else
3402 Nkind (Parent (Prev)) /= N_Parameter_Association
3403 then
3404 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3405 Set_First_Named_Actual (N, Actval);
3407 if No (Prev) then
3408 if No (Parameter_Associations (N)) then
3409 Set_Parameter_Associations (N, New_List (Assoc));
3410 else
3411 Append (Assoc, Parameter_Associations (N));
3412 end if;
3414 else
3415 Insert_After (Prev, Assoc);
3416 end if;
3418 -- Case of insertion is not first named actual
3420 else
3421 Set_Next_Named_Actual
3422 (Assoc, Next_Named_Actual (Parent (Prev)));
3423 Set_Next_Named_Actual (Parent (Prev), Actval);
3424 Append (Assoc, Parameter_Associations (N));
3425 end if;
3427 Mark_Rewrite_Insertion (Assoc);
3428 Mark_Rewrite_Insertion (Actval);
3430 Prev := Actval;
3431 end Insert_Default;
3433 --------------------
3434 -- Property_Error --
3435 --------------------
3437 procedure Property_Error
3438 (Var : Node_Id;
3439 Var_Id : Entity_Id;
3440 Prop_Nam : Name_Id)
3442 begin
3443 Error_Msg_Name_1 := Prop_Nam;
3444 Error_Msg_NE
3445 ("external variable & with enabled property % cannot appear as "
3446 & "actual in procedure call (SPARK RM 7.1.3(11))", Var, Var_Id);
3447 Error_Msg_N ("\\corresponding formal parameter has mode In", Var);
3448 end Property_Error;
3450 -------------------
3451 -- Same_Ancestor --
3452 -------------------
3454 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3455 FT1 : Entity_Id := T1;
3456 FT2 : Entity_Id := T2;
3458 begin
3459 if Is_Private_Type (T1)
3460 and then Present (Full_View (T1))
3461 then
3462 FT1 := Full_View (T1);
3463 end if;
3465 if Is_Private_Type (T2)
3466 and then Present (Full_View (T2))
3467 then
3468 FT2 := Full_View (T2);
3469 end if;
3471 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3472 end Same_Ancestor;
3474 --------------------------
3475 -- Static_Concatenation --
3476 --------------------------
3478 function Static_Concatenation (N : Node_Id) return Boolean is
3479 begin
3480 case Nkind (N) is
3481 when N_String_Literal =>
3482 return True;
3484 when N_Op_Concat =>
3486 -- Concatenation is static when both operands are static and
3487 -- the concatenation operator is a predefined one.
3489 return Scope (Entity (N)) = Standard_Standard
3490 and then
3491 Static_Concatenation (Left_Opnd (N))
3492 and then
3493 Static_Concatenation (Right_Opnd (N));
3495 when others =>
3496 if Is_Entity_Name (N) then
3497 declare
3498 Ent : constant Entity_Id := Entity (N);
3499 begin
3500 return Ekind (Ent) = E_Constant
3501 and then Present (Constant_Value (Ent))
3502 and then
3503 Is_OK_Static_Expression (Constant_Value (Ent));
3504 end;
3506 else
3507 return False;
3508 end if;
3509 end case;
3510 end Static_Concatenation;
3512 -- Start of processing for Resolve_Actuals
3514 begin
3515 Check_Argument_Order;
3516 Check_Function_Writable_Actuals (N);
3518 if Present (First_Actual (N)) then
3519 Check_Prefixed_Call;
3520 end if;
3522 A := First_Actual (N);
3523 F := First_Formal (Nam);
3524 while Present (F) loop
3525 if No (A) and then Needs_No_Actuals (Nam) then
3526 null;
3528 -- If we have an error in any actual or formal, indicated by a type
3529 -- of Any_Type, then abandon resolution attempt, and set result type
3530 -- to Any_Type. Skip this if the actual is a Raise_Expression, whose
3531 -- type is imposed from context.
3533 elsif (Present (A) and then Etype (A) = Any_Type)
3534 or else Etype (F) = Any_Type
3535 then
3536 if Nkind (A) /= N_Raise_Expression then
3537 Set_Etype (N, Any_Type);
3538 return;
3539 end if;
3540 end if;
3542 -- Case where actual is present
3544 -- If the actual is an entity, generate a reference to it now. We
3545 -- do this before the actual is resolved, because a formal of some
3546 -- protected subprogram, or a task discriminant, will be rewritten
3547 -- during expansion, and the source entity reference may be lost.
3549 if Present (A)
3550 and then Is_Entity_Name (A)
3551 and then Comes_From_Source (N)
3552 then
3553 Orig_A := Entity (A);
3555 if Present (Orig_A) then
3556 if Is_Formal (Orig_A)
3557 and then Ekind (F) /= E_In_Parameter
3558 then
3559 Generate_Reference (Orig_A, A, 'm');
3561 elsif not Is_Overloaded (A) then
3562 if Ekind (F) /= E_Out_Parameter then
3563 Generate_Reference (Orig_A, A);
3565 -- RM 6.4.1(12): For an out parameter that is passed by
3566 -- copy, the formal parameter object is created, and:
3568 -- * For an access type, the formal parameter is initialized
3569 -- from the value of the actual, without checking that the
3570 -- value satisfies any constraint, any predicate, or any
3571 -- exclusion of the null value.
3573 -- * For a scalar type that has the Default_Value aspect
3574 -- specified, the formal parameter is initialized from the
3575 -- value of the actual, without checking that the value
3576 -- satisfies any constraint or any predicate.
3577 -- I do not understand why this case is included??? this is
3578 -- not a case where an OUT parameter is treated as IN OUT.
3580 -- * For a composite type with discriminants or that has
3581 -- implicit initial values for any subcomponents, the
3582 -- behavior is as for an in out parameter passed by copy.
3584 -- Hence for these cases we generate the read reference now
3585 -- (the write reference will be generated later by
3586 -- Note_Possible_Modification).
3588 elsif Is_By_Copy_Type (Etype (F))
3589 and then
3590 (Is_Access_Type (Etype (F))
3591 or else
3592 (Is_Scalar_Type (Etype (F))
3593 and then
3594 Present (Default_Aspect_Value (Etype (F))))
3595 or else
3596 (Is_Composite_Type (Etype (F))
3597 and then (Has_Discriminants (Etype (F))
3598 or else Is_Partially_Initialized_Type
3599 (Etype (F)))))
3600 then
3601 Generate_Reference (Orig_A, A);
3602 end if;
3603 end if;
3604 end if;
3605 end if;
3607 if Present (A)
3608 and then (Nkind (Parent (A)) /= N_Parameter_Association
3609 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3610 then
3611 -- If style checking mode on, check match of formal name
3613 if Style_Check then
3614 if Nkind (Parent (A)) = N_Parameter_Association then
3615 Check_Identifier (Selector_Name (Parent (A)), F);
3616 end if;
3617 end if;
3619 -- If the formal is Out or In_Out, do not resolve and expand the
3620 -- conversion, because it is subsequently expanded into explicit
3621 -- temporaries and assignments. However, the object of the
3622 -- conversion can be resolved. An exception is the case of tagged
3623 -- type conversion with a class-wide actual. In that case we want
3624 -- the tag check to occur and no temporary will be needed (no
3625 -- representation change can occur) and the parameter is passed by
3626 -- reference, so we go ahead and resolve the type conversion.
3627 -- Another exception is the case of reference to component or
3628 -- subcomponent of a bit-packed array, in which case we want to
3629 -- defer expansion to the point the in and out assignments are
3630 -- performed.
3632 if Ekind (F) /= E_In_Parameter
3633 and then Nkind (A) = N_Type_Conversion
3634 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3635 then
3636 if Ekind (F) = E_In_Out_Parameter
3637 and then Is_Array_Type (Etype (F))
3638 then
3639 -- In a view conversion, the conversion must be legal in
3640 -- both directions, and thus both component types must be
3641 -- aliased, or neither (4.6 (8)).
3643 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3644 -- the privacy requirement should not apply to generic
3645 -- types, and should be checked in an instance. ARG query
3646 -- is in order ???
3648 if Has_Aliased_Components (Etype (Expression (A))) /=
3649 Has_Aliased_Components (Etype (F))
3650 then
3651 Error_Msg_N
3652 ("both component types in a view conversion must be"
3653 & " aliased, or neither", A);
3655 -- Comment here??? what set of cases???
3657 elsif
3658 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3659 then
3660 -- Check view conv between unrelated by ref array types
3662 if Is_By_Reference_Type (Etype (F))
3663 or else Is_By_Reference_Type (Etype (Expression (A)))
3664 then
3665 Error_Msg_N
3666 ("view conversion between unrelated by reference "
3667 & "array types not allowed (\'A'I-00246)", A);
3669 -- In Ada 2005 mode, check view conversion component
3670 -- type cannot be private, tagged, or volatile. Note
3671 -- that we only apply this to source conversions. The
3672 -- generated code can contain conversions which are
3673 -- not subject to this test, and we cannot extract the
3674 -- component type in such cases since it is not present.
3676 elsif Comes_From_Source (A)
3677 and then Ada_Version >= Ada_2005
3678 then
3679 declare
3680 Comp_Type : constant Entity_Id :=
3681 Component_Type
3682 (Etype (Expression (A)));
3683 begin
3684 if (Is_Private_Type (Comp_Type)
3685 and then not Is_Generic_Type (Comp_Type))
3686 or else Is_Tagged_Type (Comp_Type)
3687 or else Is_Volatile (Comp_Type)
3688 then
3689 Error_Msg_N
3690 ("component type of a view conversion cannot"
3691 & " be private, tagged, or volatile"
3692 & " (RM 4.6 (24))",
3693 Expression (A));
3694 end if;
3695 end;
3696 end if;
3697 end if;
3698 end if;
3700 -- Resolve expression if conversion is all OK
3702 if (Conversion_OK (A)
3703 or else Valid_Conversion (A, Etype (A), Expression (A)))
3704 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3705 then
3706 Resolve (Expression (A));
3707 end if;
3709 -- If the actual is a function call that returns a limited
3710 -- unconstrained object that needs finalization, create a
3711 -- transient scope for it, so that it can receive the proper
3712 -- finalization list.
3714 elsif Nkind (A) = N_Function_Call
3715 and then Is_Limited_Record (Etype (F))
3716 and then not Is_Constrained (Etype (F))
3717 and then Expander_Active
3718 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3719 then
3720 Establish_Transient_Scope (A, Sec_Stack => False);
3721 Resolve (A, Etype (F));
3723 -- A small optimization: if one of the actuals is a concatenation
3724 -- create a block around a procedure call to recover stack space.
3725 -- This alleviates stack usage when several procedure calls in
3726 -- the same statement list use concatenation. We do not perform
3727 -- this wrapping for code statements, where the argument is a
3728 -- static string, and we want to preserve warnings involving
3729 -- sequences of such statements.
3731 elsif Nkind (A) = N_Op_Concat
3732 and then Nkind (N) = N_Procedure_Call_Statement
3733 and then Expander_Active
3734 and then
3735 not (Is_Intrinsic_Subprogram (Nam)
3736 and then Chars (Nam) = Name_Asm)
3737 and then not Static_Concatenation (A)
3738 then
3739 Establish_Transient_Scope (A, Sec_Stack => False);
3740 Resolve (A, Etype (F));
3742 else
3743 if Nkind (A) = N_Type_Conversion
3744 and then Is_Array_Type (Etype (F))
3745 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3746 and then
3747 (Is_Limited_Type (Etype (F))
3748 or else Is_Limited_Type (Etype (Expression (A))))
3749 then
3750 Error_Msg_N
3751 ("conversion between unrelated limited array types "
3752 & "not allowed ('A'I-00246)", A);
3754 if Is_Limited_Type (Etype (F)) then
3755 Explain_Limited_Type (Etype (F), A);
3756 end if;
3758 if Is_Limited_Type (Etype (Expression (A))) then
3759 Explain_Limited_Type (Etype (Expression (A)), A);
3760 end if;
3761 end if;
3763 -- (Ada 2005: AI-251): If the actual is an allocator whose
3764 -- directly designated type is a class-wide interface, we build
3765 -- an anonymous access type to use it as the type of the
3766 -- allocator. Later, when the subprogram call is expanded, if
3767 -- the interface has a secondary dispatch table the expander
3768 -- will add a type conversion to force the correct displacement
3769 -- of the pointer.
3771 if Nkind (A) = N_Allocator then
3772 declare
3773 DDT : constant Entity_Id :=
3774 Directly_Designated_Type (Base_Type (Etype (F)));
3776 New_Itype : Entity_Id;
3778 begin
3779 if Is_Class_Wide_Type (DDT)
3780 and then Is_Interface (DDT)
3781 then
3782 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3783 Set_Etype (New_Itype, Etype (A));
3784 Set_Directly_Designated_Type
3785 (New_Itype, Directly_Designated_Type (Etype (A)));
3786 Set_Etype (A, New_Itype);
3787 end if;
3789 -- Ada 2005, AI-162:If the actual is an allocator, the
3790 -- innermost enclosing statement is the master of the
3791 -- created object. This needs to be done with expansion
3792 -- enabled only, otherwise the transient scope will not
3793 -- be removed in the expansion of the wrapped construct.
3795 if (Is_Controlled (DDT) or else Has_Task (DDT))
3796 and then Expander_Active
3797 then
3798 Establish_Transient_Scope (A, Sec_Stack => False);
3799 end if;
3800 end;
3802 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3803 Check_Restriction (No_Access_Parameter_Allocators, A);
3804 end if;
3805 end if;
3807 -- (Ada 2005): The call may be to a primitive operation of a
3808 -- tagged synchronized type, declared outside of the type. In
3809 -- this case the controlling actual must be converted to its
3810 -- corresponding record type, which is the formal type. The
3811 -- actual may be a subtype, either because of a constraint or
3812 -- because it is a generic actual, so use base type to locate
3813 -- concurrent type.
3815 F_Typ := Base_Type (Etype (F));
3817 if Is_Tagged_Type (F_Typ)
3818 and then (Is_Concurrent_Type (F_Typ)
3819 or else Is_Concurrent_Record_Type (F_Typ))
3820 then
3821 -- If the actual is overloaded, look for an interpretation
3822 -- that has a synchronized type.
3824 if not Is_Overloaded (A) then
3825 A_Typ := Base_Type (Etype (A));
3827 else
3828 declare
3829 Index : Interp_Index;
3830 It : Interp;
3832 begin
3833 Get_First_Interp (A, Index, It);
3834 while Present (It.Typ) loop
3835 if Is_Concurrent_Type (It.Typ)
3836 or else Is_Concurrent_Record_Type (It.Typ)
3837 then
3838 A_Typ := Base_Type (It.Typ);
3839 exit;
3840 end if;
3842 Get_Next_Interp (Index, It);
3843 end loop;
3844 end;
3845 end if;
3847 declare
3848 Full_A_Typ : Entity_Id;
3850 begin
3851 if Present (Full_View (A_Typ)) then
3852 Full_A_Typ := Base_Type (Full_View (A_Typ));
3853 else
3854 Full_A_Typ := A_Typ;
3855 end if;
3857 -- Tagged synchronized type (case 1): the actual is a
3858 -- concurrent type.
3860 if Is_Concurrent_Type (A_Typ)
3861 and then Corresponding_Record_Type (A_Typ) = F_Typ
3862 then
3863 Rewrite (A,
3864 Unchecked_Convert_To
3865 (Corresponding_Record_Type (A_Typ), A));
3866 Resolve (A, Etype (F));
3868 -- Tagged synchronized type (case 2): the formal is a
3869 -- concurrent type.
3871 elsif Ekind (Full_A_Typ) = E_Record_Type
3872 and then Present
3873 (Corresponding_Concurrent_Type (Full_A_Typ))
3874 and then Is_Concurrent_Type (F_Typ)
3875 and then Present (Corresponding_Record_Type (F_Typ))
3876 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3877 then
3878 Resolve (A, Corresponding_Record_Type (F_Typ));
3880 -- Common case
3882 else
3883 Resolve (A, Etype (F));
3884 end if;
3885 end;
3887 -- Not a synchronized operation
3889 else
3890 Resolve (A, Etype (F));
3891 end if;
3892 end if;
3894 A_Typ := Etype (A);
3895 F_Typ := Etype (F);
3897 -- An actual cannot be an untagged formal incomplete type
3899 if Ekind (A_Typ) = E_Incomplete_Type
3900 and then not Is_Tagged_Type (A_Typ)
3901 and then Is_Generic_Type (A_Typ)
3902 then
3903 Error_Msg_N
3904 ("invalid use of untagged formal incomplete type", A);
3905 end if;
3907 if Comes_From_Source (Original_Node (N))
3908 and then Nkind_In (Original_Node (N), N_Function_Call,
3909 N_Procedure_Call_Statement)
3910 then
3911 -- In formal mode, check that actual parameters matching
3912 -- formals of tagged types are objects (or ancestor type
3913 -- conversions of objects), not general expressions.
3915 if Is_Actual_Tagged_Parameter (A) then
3916 if Is_SPARK_05_Object_Reference (A) then
3917 null;
3919 elsif Nkind (A) = N_Type_Conversion then
3920 declare
3921 Operand : constant Node_Id := Expression (A);
3922 Operand_Typ : constant Entity_Id := Etype (Operand);
3923 Target_Typ : constant Entity_Id := A_Typ;
3925 begin
3926 if not Is_SPARK_05_Object_Reference (Operand) then
3927 Check_SPARK_05_Restriction
3928 ("object required", Operand);
3930 -- In formal mode, the only view conversions are those
3931 -- involving ancestor conversion of an extended type.
3933 elsif not
3934 (Is_Tagged_Type (Target_Typ)
3935 and then not Is_Class_Wide_Type (Target_Typ)
3936 and then Is_Tagged_Type (Operand_Typ)
3937 and then not Is_Class_Wide_Type (Operand_Typ)
3938 and then Is_Ancestor (Target_Typ, Operand_Typ))
3939 then
3940 if Ekind_In
3941 (F, E_Out_Parameter, E_In_Out_Parameter)
3942 then
3943 Check_SPARK_05_Restriction
3944 ("ancestor conversion is the only permitted "
3945 & "view conversion", A);
3946 else
3947 Check_SPARK_05_Restriction
3948 ("ancestor conversion required", A);
3949 end if;
3951 else
3952 null;
3953 end if;
3954 end;
3956 else
3957 Check_SPARK_05_Restriction ("object required", A);
3958 end if;
3960 -- In formal mode, the only view conversions are those
3961 -- involving ancestor conversion of an extended type.
3963 elsif Nkind (A) = N_Type_Conversion
3964 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3965 then
3966 Check_SPARK_05_Restriction
3967 ("ancestor conversion is the only permitted view "
3968 & "conversion", A);
3969 end if;
3970 end if;
3972 -- has warnings suppressed, then we reset Never_Set_In_Source for
3973 -- the calling entity. The reason for this is to catch cases like
3974 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3975 -- uses trickery to modify an IN parameter.
3977 if Ekind (F) = E_In_Parameter
3978 and then Is_Entity_Name (A)
3979 and then Present (Entity (A))
3980 and then Ekind (Entity (A)) = E_Variable
3981 and then Has_Warnings_Off (F_Typ)
3982 then
3983 Set_Never_Set_In_Source (Entity (A), False);
3984 end if;
3986 -- Perform error checks for IN and IN OUT parameters
3988 if Ekind (F) /= E_Out_Parameter then
3990 -- Check unset reference. For scalar parameters, it is clearly
3991 -- wrong to pass an uninitialized value as either an IN or
3992 -- IN-OUT parameter. For composites, it is also clearly an
3993 -- error to pass a completely uninitialized value as an IN
3994 -- parameter, but the case of IN OUT is trickier. We prefer
3995 -- not to give a warning here. For example, suppose there is
3996 -- a routine that sets some component of a record to False.
3997 -- It is perfectly reasonable to make this IN-OUT and allow
3998 -- either initialized or uninitialized records to be passed
3999 -- in this case.
4001 -- For partially initialized composite values, we also avoid
4002 -- warnings, since it is quite likely that we are passing a
4003 -- partially initialized value and only the initialized fields
4004 -- will in fact be read in the subprogram.
4006 if Is_Scalar_Type (A_Typ)
4007 or else (Ekind (F) = E_In_Parameter
4008 and then not Is_Partially_Initialized_Type (A_Typ))
4009 then
4010 Check_Unset_Reference (A);
4011 end if;
4013 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4014 -- actual to a nested call, since this is case of reading an
4015 -- out parameter, which is not allowed.
4017 if Ada_Version = Ada_83
4018 and then Is_Entity_Name (A)
4019 and then Ekind (Entity (A)) = E_Out_Parameter
4020 then
4021 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4022 end if;
4023 end if;
4025 -- Case of OUT or IN OUT parameter
4027 if Ekind (F) /= E_In_Parameter then
4029 -- For an Out parameter, check for useless assignment. Note
4030 -- that we can't set Last_Assignment this early, because we may
4031 -- kill current values in Resolve_Call, and that call would
4032 -- clobber the Last_Assignment field.
4034 -- Note: call Warn_On_Useless_Assignment before doing the check
4035 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4036 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4037 -- reflects the last assignment, not this one.
4039 if Ekind (F) = E_Out_Parameter then
4040 if Warn_On_Modified_As_Out_Parameter (F)
4041 and then Is_Entity_Name (A)
4042 and then Present (Entity (A))
4043 and then Comes_From_Source (N)
4044 then
4045 Warn_On_Useless_Assignment (Entity (A), A);
4046 end if;
4047 end if;
4049 -- Validate the form of the actual. Note that the call to
4050 -- Is_OK_Variable_For_Out_Formal generates the required
4051 -- reference in this case.
4053 -- A call to an initialization procedure for an aggregate
4054 -- component may initialize a nested component of a constant
4055 -- designated object. In this context the object is variable.
4057 if not Is_OK_Variable_For_Out_Formal (A)
4058 and then not Is_Init_Proc (Nam)
4059 then
4060 Error_Msg_NE ("actual for& must be a variable", A, F);
4062 if Is_Subprogram (Current_Scope)
4063 and then
4064 (Is_Invariant_Procedure (Current_Scope)
4065 or else Is_Predicate_Function (Current_Scope))
4066 then
4067 Error_Msg_N
4068 ("function used in predicate cannot "
4069 & "modify its argument", F);
4070 end if;
4071 end if;
4073 -- What's the following about???
4075 if Is_Entity_Name (A) then
4076 Kill_Checks (Entity (A));
4077 else
4078 Kill_All_Checks;
4079 end if;
4080 end if;
4082 if Etype (A) = Any_Type then
4083 Set_Etype (N, Any_Type);
4084 return;
4085 end if;
4087 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4089 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
4091 -- Apply predicate tests except in certain special cases. Note
4092 -- that it might be more consistent to apply these only when
4093 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4094 -- for the outbound predicate tests ???
4096 if Predicate_Tests_On_Arguments (Nam) then
4097 Apply_Predicate_Check (A, F_Typ);
4098 end if;
4100 -- Apply required constraint checks
4102 -- Gigi looks at the check flag and uses the appropriate types.
4103 -- For now since one flag is used there is an optimization
4104 -- which might not be done in the IN OUT case since Gigi does
4105 -- not do any analysis. More thought required about this ???
4107 -- In fact is this comment obsolete??? doesn't the expander now
4108 -- generate all these tests anyway???
4110 if Is_Scalar_Type (Etype (A)) then
4111 Apply_Scalar_Range_Check (A, F_Typ);
4113 elsif Is_Array_Type (Etype (A)) then
4114 Apply_Length_Check (A, F_Typ);
4116 elsif Is_Record_Type (F_Typ)
4117 and then Has_Discriminants (F_Typ)
4118 and then Is_Constrained (F_Typ)
4119 and then (not Is_Derived_Type (F_Typ)
4120 or else Comes_From_Source (Nam))
4121 then
4122 Apply_Discriminant_Check (A, F_Typ);
4124 -- For view conversions of a discriminated object, apply
4125 -- check to object itself, the conversion alreay has the
4126 -- proper type.
4128 if Nkind (A) = N_Type_Conversion
4129 and then Is_Constrained (Etype (Expression (A)))
4130 then
4131 Apply_Discriminant_Check (Expression (A), F_Typ);
4132 end if;
4134 elsif Is_Access_Type (F_Typ)
4135 and then Is_Array_Type (Designated_Type (F_Typ))
4136 and then Is_Constrained (Designated_Type (F_Typ))
4137 then
4138 Apply_Length_Check (A, F_Typ);
4140 elsif Is_Access_Type (F_Typ)
4141 and then Has_Discriminants (Designated_Type (F_Typ))
4142 and then Is_Constrained (Designated_Type (F_Typ))
4143 then
4144 Apply_Discriminant_Check (A, F_Typ);
4146 else
4147 Apply_Range_Check (A, F_Typ);
4148 end if;
4150 -- Ada 2005 (AI-231): Note that the controlling parameter case
4151 -- already existed in Ada 95, which is partially checked
4152 -- elsewhere (see Checks), and we don't want the warning
4153 -- message to differ.
4155 if Is_Access_Type (F_Typ)
4156 and then Can_Never_Be_Null (F_Typ)
4157 and then Known_Null (A)
4158 then
4159 if Is_Controlling_Formal (F) then
4160 Apply_Compile_Time_Constraint_Error
4161 (N => A,
4162 Msg => "null value not allowed here??",
4163 Reason => CE_Access_Check_Failed);
4165 elsif Ada_Version >= Ada_2005 then
4166 Apply_Compile_Time_Constraint_Error
4167 (N => A,
4168 Msg => "(Ada 2005) null not allowed in "
4169 & "null-excluding formal??",
4170 Reason => CE_Null_Not_Allowed);
4171 end if;
4172 end if;
4173 end if;
4175 -- Checks for OUT parameters and IN OUT parameters
4177 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
4179 -- If there is a type conversion, to make sure the return value
4180 -- meets the constraints of the variable before the conversion.
4182 if Nkind (A) = N_Type_Conversion then
4183 if Is_Scalar_Type (A_Typ) then
4184 Apply_Scalar_Range_Check
4185 (Expression (A), Etype (Expression (A)), A_Typ);
4186 else
4187 Apply_Range_Check
4188 (Expression (A), Etype (Expression (A)), A_Typ);
4189 end if;
4191 -- If no conversion apply scalar range checks and length checks
4192 -- base on the subtype of the actual (NOT that of the formal).
4194 else
4195 if Is_Scalar_Type (F_Typ) then
4196 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4197 elsif Is_Array_Type (F_Typ)
4198 and then Ekind (F) = E_Out_Parameter
4199 then
4200 Apply_Length_Check (A, F_Typ);
4201 else
4202 Apply_Range_Check (A, A_Typ, F_Typ);
4203 end if;
4204 end if;
4206 -- Note: we do not apply the predicate checks for the case of
4207 -- OUT and IN OUT parameters. They are instead applied in the
4208 -- Expand_Actuals routine in Exp_Ch6.
4209 end if;
4211 -- An actual associated with an access parameter is implicitly
4212 -- converted to the anonymous access type of the formal and must
4213 -- satisfy the legality checks for access conversions.
4215 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4216 if not Valid_Conversion (A, F_Typ, A) then
4217 Error_Msg_N
4218 ("invalid implicit conversion for access parameter", A);
4219 end if;
4221 -- If the actual is an access selected component of a variable,
4222 -- the call may modify its designated object. It is reasonable
4223 -- to treat this as a potential modification of the enclosing
4224 -- record, to prevent spurious warnings that it should be
4225 -- declared as a constant, because intuitively programmers
4226 -- regard the designated subcomponent as part of the record.
4228 if Nkind (A) = N_Selected_Component
4229 and then Is_Entity_Name (Prefix (A))
4230 and then not Is_Constant_Object (Entity (Prefix (A)))
4231 then
4232 Note_Possible_Modification (A, Sure => False);
4233 end if;
4234 end if;
4236 -- Check bad case of atomic/volatile argument (RM C.6(12))
4238 if Is_By_Reference_Type (Etype (F))
4239 and then Comes_From_Source (N)
4240 then
4241 if Is_Atomic_Object (A)
4242 and then not Is_Atomic (Etype (F))
4243 then
4244 Error_Msg_NE
4245 ("cannot pass atomic argument to non-atomic formal&",
4246 A, F);
4248 elsif Is_Volatile_Object (A)
4249 and then not Is_Volatile (Etype (F))
4250 then
4251 Error_Msg_NE
4252 ("cannot pass volatile argument to non-volatile formal&",
4253 A, F);
4254 end if;
4255 end if;
4257 -- Check that subprograms don't have improper controlling
4258 -- arguments (RM 3.9.2 (9)).
4260 -- A primitive operation may have an access parameter of an
4261 -- incomplete tagged type, but a dispatching call is illegal
4262 -- if the type is still incomplete.
4264 if Is_Controlling_Formal (F) then
4265 Set_Is_Controlling_Actual (A);
4267 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4268 declare
4269 Desig : constant Entity_Id := Designated_Type (Etype (F));
4270 begin
4271 if Ekind (Desig) = E_Incomplete_Type
4272 and then No (Full_View (Desig))
4273 and then No (Non_Limited_View (Desig))
4274 then
4275 Error_Msg_NE
4276 ("premature use of incomplete type& "
4277 & "in dispatching call", A, Desig);
4278 end if;
4279 end;
4280 end if;
4282 elsif Nkind (A) = N_Explicit_Dereference then
4283 Validate_Remote_Access_To_Class_Wide_Type (A);
4284 end if;
4286 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4287 and then not Is_Class_Wide_Type (F_Typ)
4288 and then not Is_Controlling_Formal (F)
4289 then
4290 Error_Msg_N ("class-wide argument not allowed here!", A);
4292 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4293 Error_Msg_Node_2 := F_Typ;
4294 Error_Msg_NE
4295 ("& is not a dispatching operation of &!", A, Nam);
4296 end if;
4298 -- Apply the checks described in 3.10.2(27): if the context is a
4299 -- specific access-to-object, the actual cannot be class-wide.
4300 -- Use base type to exclude access_to_subprogram cases.
4302 elsif Is_Access_Type (A_Typ)
4303 and then Is_Access_Type (F_Typ)
4304 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4305 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4306 or else (Nkind (A) = N_Attribute_Reference
4307 and then
4308 Is_Class_Wide_Type (Etype (Prefix (A)))))
4309 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4310 and then not Is_Controlling_Formal (F)
4312 -- Disable these checks for call to imported C++ subprograms
4314 and then not
4315 (Is_Entity_Name (Name (N))
4316 and then Is_Imported (Entity (Name (N)))
4317 and then Convention (Entity (Name (N))) = Convention_CPP)
4318 then
4319 Error_Msg_N
4320 ("access to class-wide argument not allowed here!", A);
4322 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4323 Error_Msg_Node_2 := Designated_Type (F_Typ);
4324 Error_Msg_NE
4325 ("& is not a dispatching operation of &!", A, Nam);
4326 end if;
4327 end if;
4329 Check_Aliased_Parameter;
4331 Eval_Actual (A);
4333 -- If it is a named association, treat the selector_name as a
4334 -- proper identifier, and mark the corresponding entity.
4336 if Nkind (Parent (A)) = N_Parameter_Association
4338 -- Ignore reference in SPARK mode, as it refers to an entity not
4339 -- in scope at the point of reference, so the reference should
4340 -- be ignored for computing effects of subprograms.
4342 and then not GNATprove_Mode
4343 then
4344 Set_Entity (Selector_Name (Parent (A)), F);
4345 Generate_Reference (F, Selector_Name (Parent (A)));
4346 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4347 Generate_Reference (F_Typ, N, ' ');
4348 end if;
4350 Prev := A;
4352 if Ekind (F) /= E_Out_Parameter then
4353 Check_Unset_Reference (A);
4354 end if;
4356 -- The following checks are only relevant when SPARK_Mode is on as
4357 -- they are not standard Ada legality rule. Internally generated
4358 -- temporaries are ignored.
4360 if SPARK_Mode = On
4361 and then Is_Effectively_Volatile_Object (A)
4362 and then Comes_From_Source (A)
4363 then
4364 -- An effectively volatile object may act as an actual
4365 -- parameter when the corresponding formal is of a non-scalar
4366 -- volatile type.
4368 if Is_Volatile (Etype (F))
4369 and then not Is_Scalar_Type (Etype (F))
4370 then
4371 null;
4373 -- An effectively volatile object may act as an actual
4374 -- parameter in a call to an instance of Unchecked_Conversion.
4376 elsif Is_Unchecked_Conversion_Instance (Nam) then
4377 null;
4379 else
4380 Error_Msg_N
4381 ("volatile object cannot act as actual in a call (SPARK "
4382 & "RM 7.1.3(12))", A);
4383 end if;
4385 -- Detect an external variable with an enabled property that
4386 -- does not match the mode of the corresponding formal in a
4387 -- procedure call. Functions are not considered because they
4388 -- cannot have effectively volatile formal parameters in the
4389 -- first place.
4391 if Ekind (Nam) = E_Procedure
4392 and then Is_Entity_Name (A)
4393 and then Present (Entity (A))
4394 and then Ekind (Entity (A)) = E_Variable
4395 then
4396 A_Id := Entity (A);
4398 if Ekind (F) = E_In_Parameter then
4399 if Async_Readers_Enabled (A_Id) then
4400 Property_Error (A, A_Id, Name_Async_Readers);
4401 elsif Effective_Reads_Enabled (A_Id) then
4402 Property_Error (A, A_Id, Name_Effective_Reads);
4403 elsif Effective_Writes_Enabled (A_Id) then
4404 Property_Error (A, A_Id, Name_Effective_Writes);
4405 end if;
4407 elsif Ekind (F) = E_Out_Parameter
4408 and then Async_Writers_Enabled (A_Id)
4409 then
4410 Error_Msg_Name_1 := Name_Async_Writers;
4411 Error_Msg_NE
4412 ("external variable & with enabled property % cannot "
4413 & "appear as actual in procedure call "
4414 & "(SPARK RM 7.1.3(11))", A, A_Id);
4415 Error_Msg_N
4416 ("\\corresponding formal parameter has mode Out", A);
4417 end if;
4418 end if;
4419 end if;
4421 Next_Actual (A);
4423 -- Case where actual is not present
4425 else
4426 Insert_Default;
4427 end if;
4429 Next_Formal (F);
4430 end loop;
4431 end Resolve_Actuals;
4433 -----------------------
4434 -- Resolve_Allocator --
4435 -----------------------
4437 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4438 Desig_T : constant Entity_Id := Designated_Type (Typ);
4439 E : constant Node_Id := Expression (N);
4440 Subtyp : Entity_Id;
4441 Discrim : Entity_Id;
4442 Constr : Node_Id;
4443 Aggr : Node_Id;
4444 Assoc : Node_Id := Empty;
4445 Disc_Exp : Node_Id;
4447 procedure Check_Allocator_Discrim_Accessibility
4448 (Disc_Exp : Node_Id;
4449 Alloc_Typ : Entity_Id);
4450 -- Check that accessibility level associated with an access discriminant
4451 -- initialized in an allocator by the expression Disc_Exp is not deeper
4452 -- than the level of the allocator type Alloc_Typ. An error message is
4453 -- issued if this condition is violated. Specialized checks are done for
4454 -- the cases of a constraint expression which is an access attribute or
4455 -- an access discriminant.
4457 function In_Dispatching_Context return Boolean;
4458 -- If the allocator is an actual in a call, it is allowed to be class-
4459 -- wide when the context is not because it is a controlling actual.
4461 -------------------------------------------
4462 -- Check_Allocator_Discrim_Accessibility --
4463 -------------------------------------------
4465 procedure Check_Allocator_Discrim_Accessibility
4466 (Disc_Exp : Node_Id;
4467 Alloc_Typ : Entity_Id)
4469 begin
4470 if Type_Access_Level (Etype (Disc_Exp)) >
4471 Deepest_Type_Access_Level (Alloc_Typ)
4472 then
4473 Error_Msg_N
4474 ("operand type has deeper level than allocator type", Disc_Exp);
4476 -- When the expression is an Access attribute the level of the prefix
4477 -- object must not be deeper than that of the allocator's type.
4479 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4480 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4481 Attribute_Access
4482 and then Object_Access_Level (Prefix (Disc_Exp)) >
4483 Deepest_Type_Access_Level (Alloc_Typ)
4484 then
4485 Error_Msg_N
4486 ("prefix of attribute has deeper level than allocator type",
4487 Disc_Exp);
4489 -- When the expression is an access discriminant the check is against
4490 -- the level of the prefix object.
4492 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4493 and then Nkind (Disc_Exp) = N_Selected_Component
4494 and then Object_Access_Level (Prefix (Disc_Exp)) >
4495 Deepest_Type_Access_Level (Alloc_Typ)
4496 then
4497 Error_Msg_N
4498 ("access discriminant has deeper level than allocator type",
4499 Disc_Exp);
4501 -- All other cases are legal
4503 else
4504 null;
4505 end if;
4506 end Check_Allocator_Discrim_Accessibility;
4508 ----------------------------
4509 -- In_Dispatching_Context --
4510 ----------------------------
4512 function In_Dispatching_Context return Boolean is
4513 Par : constant Node_Id := Parent (N);
4515 begin
4516 return Nkind (Par) in N_Subprogram_Call
4517 and then Is_Entity_Name (Name (Par))
4518 and then Is_Dispatching_Operation (Entity (Name (Par)));
4519 end In_Dispatching_Context;
4521 -- Start of processing for Resolve_Allocator
4523 begin
4524 -- Replace general access with specific type
4526 if Ekind (Etype (N)) = E_Allocator_Type then
4527 Set_Etype (N, Base_Type (Typ));
4528 end if;
4530 if Is_Abstract_Type (Typ) then
4531 Error_Msg_N ("type of allocator cannot be abstract", N);
4532 end if;
4534 -- For qualified expression, resolve the expression using the given
4535 -- subtype (nothing to do for type mark, subtype indication)
4537 if Nkind (E) = N_Qualified_Expression then
4538 if Is_Class_Wide_Type (Etype (E))
4539 and then not Is_Class_Wide_Type (Desig_T)
4540 and then not In_Dispatching_Context
4541 then
4542 Error_Msg_N
4543 ("class-wide allocator not allowed for this access type", N);
4544 end if;
4546 Resolve (Expression (E), Etype (E));
4547 Check_Non_Static_Context (Expression (E));
4548 Check_Unset_Reference (Expression (E));
4550 -- A qualified expression requires an exact match of the type.
4551 -- Class-wide matching is not allowed.
4553 if (Is_Class_Wide_Type (Etype (Expression (E)))
4554 or else Is_Class_Wide_Type (Etype (E)))
4555 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4556 then
4557 Wrong_Type (Expression (E), Etype (E));
4558 end if;
4560 -- Calls to build-in-place functions are not currently supported in
4561 -- allocators for access types associated with a simple storage pool.
4562 -- Supporting such allocators may require passing additional implicit
4563 -- parameters to build-in-place functions (or a significant revision
4564 -- of the current b-i-p implementation to unify the handling for
4565 -- multiple kinds of storage pools). ???
4567 if Is_Limited_View (Desig_T)
4568 and then Nkind (Expression (E)) = N_Function_Call
4569 then
4570 declare
4571 Pool : constant Entity_Id :=
4572 Associated_Storage_Pool (Root_Type (Typ));
4573 begin
4574 if Present (Pool)
4575 and then
4576 Present (Get_Rep_Pragma
4577 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4578 then
4579 Error_Msg_N
4580 ("limited function calls not yet supported in simple "
4581 & "storage pool allocators", Expression (E));
4582 end if;
4583 end;
4584 end if;
4586 -- A special accessibility check is needed for allocators that
4587 -- constrain access discriminants. The level of the type of the
4588 -- expression used to constrain an access discriminant cannot be
4589 -- deeper than the type of the allocator (in contrast to access
4590 -- parameters, where the level of the actual can be arbitrary).
4592 -- We can't use Valid_Conversion to perform this check because in
4593 -- general the type of the allocator is unrelated to the type of
4594 -- the access discriminant.
4596 if Ekind (Typ) /= E_Anonymous_Access_Type
4597 or else Is_Local_Anonymous_Access (Typ)
4598 then
4599 Subtyp := Entity (Subtype_Mark (E));
4601 Aggr := Original_Node (Expression (E));
4603 if Has_Discriminants (Subtyp)
4604 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4605 then
4606 Discrim := First_Discriminant (Base_Type (Subtyp));
4608 -- Get the first component expression of the aggregate
4610 if Present (Expressions (Aggr)) then
4611 Disc_Exp := First (Expressions (Aggr));
4613 elsif Present (Component_Associations (Aggr)) then
4614 Assoc := First (Component_Associations (Aggr));
4616 if Present (Assoc) then
4617 Disc_Exp := Expression (Assoc);
4618 else
4619 Disc_Exp := Empty;
4620 end if;
4622 else
4623 Disc_Exp := Empty;
4624 end if;
4626 while Present (Discrim) and then Present (Disc_Exp) loop
4627 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4628 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4629 end if;
4631 Next_Discriminant (Discrim);
4633 if Present (Discrim) then
4634 if Present (Assoc) then
4635 Next (Assoc);
4636 Disc_Exp := Expression (Assoc);
4638 elsif Present (Next (Disc_Exp)) then
4639 Next (Disc_Exp);
4641 else
4642 Assoc := First (Component_Associations (Aggr));
4644 if Present (Assoc) then
4645 Disc_Exp := Expression (Assoc);
4646 else
4647 Disc_Exp := Empty;
4648 end if;
4649 end if;
4650 end if;
4651 end loop;
4652 end if;
4653 end if;
4655 -- For a subtype mark or subtype indication, freeze the subtype
4657 else
4658 Freeze_Expression (E);
4660 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4661 Error_Msg_N
4662 ("initialization required for access-to-constant allocator", N);
4663 end if;
4665 -- A special accessibility check is needed for allocators that
4666 -- constrain access discriminants. The level of the type of the
4667 -- expression used to constrain an access discriminant cannot be
4668 -- deeper than the type of the allocator (in contrast to access
4669 -- parameters, where the level of the actual can be arbitrary).
4670 -- We can't use Valid_Conversion to perform this check because
4671 -- in general the type of the allocator is unrelated to the type
4672 -- of the access discriminant.
4674 if Nkind (Original_Node (E)) = N_Subtype_Indication
4675 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4676 or else Is_Local_Anonymous_Access (Typ))
4677 then
4678 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4680 if Has_Discriminants (Subtyp) then
4681 Discrim := First_Discriminant (Base_Type (Subtyp));
4682 Constr := First (Constraints (Constraint (Original_Node (E))));
4683 while Present (Discrim) and then Present (Constr) loop
4684 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4685 if Nkind (Constr) = N_Discriminant_Association then
4686 Disc_Exp := Original_Node (Expression (Constr));
4687 else
4688 Disc_Exp := Original_Node (Constr);
4689 end if;
4691 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4692 end if;
4694 Next_Discriminant (Discrim);
4695 Next (Constr);
4696 end loop;
4697 end if;
4698 end if;
4699 end if;
4701 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4702 -- check that the level of the type of the created object is not deeper
4703 -- than the level of the allocator's access type, since extensions can
4704 -- now occur at deeper levels than their ancestor types. This is a
4705 -- static accessibility level check; a run-time check is also needed in
4706 -- the case of an initialized allocator with a class-wide argument (see
4707 -- Expand_Allocator_Expression).
4709 if Ada_Version >= Ada_2005
4710 and then Is_Class_Wide_Type (Desig_T)
4711 then
4712 declare
4713 Exp_Typ : Entity_Id;
4715 begin
4716 if Nkind (E) = N_Qualified_Expression then
4717 Exp_Typ := Etype (E);
4718 elsif Nkind (E) = N_Subtype_Indication then
4719 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4720 else
4721 Exp_Typ := Entity (E);
4722 end if;
4724 if Type_Access_Level (Exp_Typ) >
4725 Deepest_Type_Access_Level (Typ)
4726 then
4727 if In_Instance_Body then
4728 Error_Msg_Warn := SPARK_Mode /= On;
4729 Error_Msg_N
4730 ("type in allocator has deeper level than "
4731 & "designated class-wide type<<", E);
4732 Error_Msg_N ("\Program_Error [<<", E);
4733 Rewrite (N,
4734 Make_Raise_Program_Error (Sloc (N),
4735 Reason => PE_Accessibility_Check_Failed));
4736 Set_Etype (N, Typ);
4738 -- Do not apply Ada 2005 accessibility checks on a class-wide
4739 -- allocator if the type given in the allocator is a formal
4740 -- type. A run-time check will be performed in the instance.
4742 elsif not Is_Generic_Type (Exp_Typ) then
4743 Error_Msg_N ("type in allocator has deeper level than "
4744 & "designated class-wide type", E);
4745 end if;
4746 end if;
4747 end;
4748 end if;
4750 -- Check for allocation from an empty storage pool
4752 if No_Pool_Assigned (Typ) then
4753 Error_Msg_N ("allocation from empty storage pool!", N);
4755 -- If the context is an unchecked conversion, as may happen within an
4756 -- inlined subprogram, the allocator is being resolved with its own
4757 -- anonymous type. In that case, if the target type has a specific
4758 -- storage pool, it must be inherited explicitly by the allocator type.
4760 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4761 and then No (Associated_Storage_Pool (Typ))
4762 then
4763 Set_Associated_Storage_Pool
4764 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4765 end if;
4767 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4768 Check_Restriction (No_Anonymous_Allocators, N);
4769 end if;
4771 -- Check that an allocator with task parts isn't for a nested access
4772 -- type when restriction No_Task_Hierarchy applies.
4774 if not Is_Library_Level_Entity (Base_Type (Typ))
4775 and then Has_Task (Base_Type (Desig_T))
4776 then
4777 Check_Restriction (No_Task_Hierarchy, N);
4778 end if;
4780 -- An illegal allocator may be rewritten as a raise Program_Error
4781 -- statement.
4783 if Nkind (N) = N_Allocator then
4785 -- An anonymous access discriminant is the definition of a
4786 -- coextension.
4788 if Ekind (Typ) = E_Anonymous_Access_Type
4789 and then Nkind (Associated_Node_For_Itype (Typ)) =
4790 N_Discriminant_Specification
4791 then
4792 declare
4793 Discr : constant Entity_Id :=
4794 Defining_Identifier (Associated_Node_For_Itype (Typ));
4796 begin
4797 Check_Restriction (No_Coextensions, N);
4799 -- Ada 2012 AI05-0052: If the designated type of the allocator
4800 -- is limited, then the allocator shall not be used to define
4801 -- the value of an access discriminant unless the discriminated
4802 -- type is immutably limited.
4804 if Ada_Version >= Ada_2012
4805 and then Is_Limited_Type (Desig_T)
4806 and then not Is_Limited_View (Scope (Discr))
4807 then
4808 Error_Msg_N
4809 ("only immutably limited types can have anonymous "
4810 & "access discriminants designating a limited type", N);
4811 end if;
4812 end;
4814 -- Avoid marking an allocator as a dynamic coextension if it is
4815 -- within a static construct.
4817 if not Is_Static_Coextension (N) then
4818 Set_Is_Dynamic_Coextension (N);
4819 end if;
4821 -- Cleanup for potential static coextensions
4823 else
4824 Set_Is_Dynamic_Coextension (N, False);
4825 Set_Is_Static_Coextension (N, False);
4826 end if;
4827 end if;
4829 -- Report a simple error: if the designated object is a local task,
4830 -- its body has not been seen yet, and its activation will fail an
4831 -- elaboration check.
4833 if Is_Task_Type (Desig_T)
4834 and then Scope (Base_Type (Desig_T)) = Current_Scope
4835 and then Is_Compilation_Unit (Current_Scope)
4836 and then Ekind (Current_Scope) = E_Package
4837 and then not In_Package_Body (Current_Scope)
4838 then
4839 Error_Msg_Warn := SPARK_Mode /= On;
4840 Error_Msg_N ("cannot activate task before body seen<<", N);
4841 Error_Msg_N ("\Program_Error [<<", N);
4842 end if;
4844 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4845 -- type with a task component on a subpool. This action must raise
4846 -- Program_Error at runtime.
4848 if Ada_Version >= Ada_2012
4849 and then Nkind (N) = N_Allocator
4850 and then Present (Subpool_Handle_Name (N))
4851 and then Has_Task (Desig_T)
4852 then
4853 Error_Msg_Warn := SPARK_Mode /= On;
4854 Error_Msg_N ("cannot allocate task on subpool<<", N);
4855 Error_Msg_N ("\Program_Error [<<", N);
4857 Rewrite (N,
4858 Make_Raise_Program_Error (Sloc (N),
4859 Reason => PE_Explicit_Raise));
4860 Set_Etype (N, Typ);
4861 end if;
4862 end Resolve_Allocator;
4864 ---------------------------
4865 -- Resolve_Arithmetic_Op --
4866 ---------------------------
4868 -- Used for resolving all arithmetic operators except exponentiation
4870 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4871 L : constant Node_Id := Left_Opnd (N);
4872 R : constant Node_Id := Right_Opnd (N);
4873 TL : constant Entity_Id := Base_Type (Etype (L));
4874 TR : constant Entity_Id := Base_Type (Etype (R));
4875 T : Entity_Id;
4876 Rop : Node_Id;
4878 B_Typ : constant Entity_Id := Base_Type (Typ);
4879 -- We do the resolution using the base type, because intermediate values
4880 -- in expressions always are of the base type, not a subtype of it.
4882 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4883 -- Returns True if N is in a context that expects "any real type"
4885 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4886 -- Return True iff given type is Integer or universal real/integer
4888 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4889 -- Choose type of integer literal in fixed-point operation to conform
4890 -- to available fixed-point type. T is the type of the other operand,
4891 -- which is needed to determine the expected type of N.
4893 procedure Set_Operand_Type (N : Node_Id);
4894 -- Set operand type to T if universal
4896 -------------------------------
4897 -- Expected_Type_Is_Any_Real --
4898 -------------------------------
4900 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4901 begin
4902 -- N is the expression after "delta" in a fixed_point_definition;
4903 -- see RM-3.5.9(6):
4905 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4906 N_Decimal_Fixed_Point_Definition,
4908 -- N is one of the bounds in a real_range_specification;
4909 -- see RM-3.5.7(5):
4911 N_Real_Range_Specification,
4913 -- N is the expression of a delta_constraint;
4914 -- see RM-J.3(3):
4916 N_Delta_Constraint);
4917 end Expected_Type_Is_Any_Real;
4919 -----------------------------
4920 -- Is_Integer_Or_Universal --
4921 -----------------------------
4923 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4924 T : Entity_Id;
4925 Index : Interp_Index;
4926 It : Interp;
4928 begin
4929 if not Is_Overloaded (N) then
4930 T := Etype (N);
4931 return Base_Type (T) = Base_Type (Standard_Integer)
4932 or else T = Universal_Integer
4933 or else T = Universal_Real;
4934 else
4935 Get_First_Interp (N, Index, It);
4936 while Present (It.Typ) loop
4937 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4938 or else It.Typ = Universal_Integer
4939 or else It.Typ = Universal_Real
4940 then
4941 return True;
4942 end if;
4944 Get_Next_Interp (Index, It);
4945 end loop;
4946 end if;
4948 return False;
4949 end Is_Integer_Or_Universal;
4951 ----------------------------
4952 -- Set_Mixed_Mode_Operand --
4953 ----------------------------
4955 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4956 Index : Interp_Index;
4957 It : Interp;
4959 begin
4960 if Universal_Interpretation (N) = Universal_Integer then
4962 -- A universal integer literal is resolved as standard integer
4963 -- except in the case of a fixed-point result, where we leave it
4964 -- as universal (to be handled by Exp_Fixd later on)
4966 if Is_Fixed_Point_Type (T) then
4967 Resolve (N, Universal_Integer);
4968 else
4969 Resolve (N, Standard_Integer);
4970 end if;
4972 elsif Universal_Interpretation (N) = Universal_Real
4973 and then (T = Base_Type (Standard_Integer)
4974 or else T = Universal_Integer
4975 or else T = Universal_Real)
4976 then
4977 -- A universal real can appear in a fixed-type context. We resolve
4978 -- the literal with that context, even though this might raise an
4979 -- exception prematurely (the other operand may be zero).
4981 Resolve (N, B_Typ);
4983 elsif Etype (N) = Base_Type (Standard_Integer)
4984 and then T = Universal_Real
4985 and then Is_Overloaded (N)
4986 then
4987 -- Integer arg in mixed-mode operation. Resolve with universal
4988 -- type, in case preference rule must be applied.
4990 Resolve (N, Universal_Integer);
4992 elsif Etype (N) = T
4993 and then B_Typ /= Universal_Fixed
4994 then
4995 -- Not a mixed-mode operation, resolve with context
4997 Resolve (N, B_Typ);
4999 elsif Etype (N) = Any_Fixed then
5001 -- N may itself be a mixed-mode operation, so use context type
5003 Resolve (N, B_Typ);
5005 elsif Is_Fixed_Point_Type (T)
5006 and then B_Typ = Universal_Fixed
5007 and then Is_Overloaded (N)
5008 then
5009 -- Must be (fixed * fixed) operation, operand must have one
5010 -- compatible interpretation.
5012 Resolve (N, Any_Fixed);
5014 elsif Is_Fixed_Point_Type (B_Typ)
5015 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5016 and then Is_Overloaded (N)
5017 then
5018 -- C * F(X) in a fixed context, where C is a real literal or a
5019 -- fixed-point expression. F must have either a fixed type
5020 -- interpretation or an integer interpretation, but not both.
5022 Get_First_Interp (N, Index, It);
5023 while Present (It.Typ) loop
5024 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5025 if Analyzed (N) then
5026 Error_Msg_N ("ambiguous operand in fixed operation", N);
5027 else
5028 Resolve (N, Standard_Integer);
5029 end if;
5031 elsif Is_Fixed_Point_Type (It.Typ) then
5032 if Analyzed (N) then
5033 Error_Msg_N ("ambiguous operand in fixed operation", N);
5034 else
5035 Resolve (N, It.Typ);
5036 end if;
5037 end if;
5039 Get_Next_Interp (Index, It);
5040 end loop;
5042 -- Reanalyze the literal with the fixed type of the context. If
5043 -- context is Universal_Fixed, we are within a conversion, leave
5044 -- the literal as a universal real because there is no usable
5045 -- fixed type, and the target of the conversion plays no role in
5046 -- the resolution.
5048 declare
5049 Op2 : Node_Id;
5050 T2 : Entity_Id;
5052 begin
5053 if N = L then
5054 Op2 := R;
5055 else
5056 Op2 := L;
5057 end if;
5059 if B_Typ = Universal_Fixed
5060 and then Nkind (Op2) = N_Real_Literal
5061 then
5062 T2 := Universal_Real;
5063 else
5064 T2 := B_Typ;
5065 end if;
5067 Set_Analyzed (Op2, False);
5068 Resolve (Op2, T2);
5069 end;
5071 else
5072 Resolve (N);
5073 end if;
5074 end Set_Mixed_Mode_Operand;
5076 ----------------------
5077 -- Set_Operand_Type --
5078 ----------------------
5080 procedure Set_Operand_Type (N : Node_Id) is
5081 begin
5082 if Etype (N) = Universal_Integer
5083 or else Etype (N) = Universal_Real
5084 then
5085 Set_Etype (N, T);
5086 end if;
5087 end Set_Operand_Type;
5089 -- Start of processing for Resolve_Arithmetic_Op
5091 begin
5092 if Comes_From_Source (N)
5093 and then Ekind (Entity (N)) = E_Function
5094 and then Is_Imported (Entity (N))
5095 and then Is_Intrinsic_Subprogram (Entity (N))
5096 then
5097 Resolve_Intrinsic_Operator (N, Typ);
5098 return;
5100 -- Special-case for mixed-mode universal expressions or fixed point type
5101 -- operation: each argument is resolved separately. The same treatment
5102 -- is required if one of the operands of a fixed point operation is
5103 -- universal real, since in this case we don't do a conversion to a
5104 -- specific fixed-point type (instead the expander handles the case).
5106 -- Set the type of the node to its universal interpretation because
5107 -- legality checks on an exponentiation operand need the context.
5109 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
5110 and then Present (Universal_Interpretation (L))
5111 and then Present (Universal_Interpretation (R))
5112 then
5113 Set_Etype (N, B_Typ);
5114 Resolve (L, Universal_Interpretation (L));
5115 Resolve (R, Universal_Interpretation (R));
5117 elsif (B_Typ = Universal_Real
5118 or else Etype (N) = Universal_Fixed
5119 or else (Etype (N) = Any_Fixed
5120 and then Is_Fixed_Point_Type (B_Typ))
5121 or else (Is_Fixed_Point_Type (B_Typ)
5122 and then (Is_Integer_Or_Universal (L)
5123 or else
5124 Is_Integer_Or_Universal (R))))
5125 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5126 then
5127 if TL = Universal_Integer or else TR = Universal_Integer then
5128 Check_For_Visible_Operator (N, B_Typ);
5129 end if;
5131 -- If context is a fixed type and one operand is integer, the other
5132 -- is resolved with the type of the context.
5134 if Is_Fixed_Point_Type (B_Typ)
5135 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5136 or else TL = Universal_Integer)
5137 then
5138 Resolve (R, B_Typ);
5139 Resolve (L, TL);
5141 elsif Is_Fixed_Point_Type (B_Typ)
5142 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5143 or else TR = Universal_Integer)
5144 then
5145 Resolve (L, B_Typ);
5146 Resolve (R, TR);
5148 else
5149 Set_Mixed_Mode_Operand (L, TR);
5150 Set_Mixed_Mode_Operand (R, TL);
5151 end if;
5153 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
5154 -- multiplying operators from being used when the expected type is
5155 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
5156 -- some cases where the expected type is actually Any_Real;
5157 -- Expected_Type_Is_Any_Real takes care of that case.
5159 if Etype (N) = Universal_Fixed
5160 or else Etype (N) = Any_Fixed
5161 then
5162 if B_Typ = Universal_Fixed
5163 and then not Expected_Type_Is_Any_Real (N)
5164 and then not Nkind_In (Parent (N), N_Type_Conversion,
5165 N_Unchecked_Type_Conversion)
5166 then
5167 Error_Msg_N ("type cannot be determined from context!", N);
5168 Error_Msg_N ("\explicit conversion to result type required", N);
5170 Set_Etype (L, Any_Type);
5171 Set_Etype (R, Any_Type);
5173 else
5174 if Ada_Version = Ada_83
5175 and then Etype (N) = Universal_Fixed
5176 and then not
5177 Nkind_In (Parent (N), N_Type_Conversion,
5178 N_Unchecked_Type_Conversion)
5179 then
5180 Error_Msg_N
5181 ("(Ada 83) fixed-point operation "
5182 & "needs explicit conversion", N);
5183 end if;
5185 -- The expected type is "any real type" in contexts like
5187 -- type T is delta <universal_fixed-expression> ...
5189 -- in which case we need to set the type to Universal_Real
5190 -- so that static expression evaluation will work properly.
5192 if Expected_Type_Is_Any_Real (N) then
5193 Set_Etype (N, Universal_Real);
5194 else
5195 Set_Etype (N, B_Typ);
5196 end if;
5197 end if;
5199 elsif Is_Fixed_Point_Type (B_Typ)
5200 and then (Is_Integer_Or_Universal (L)
5201 or else Nkind (L) = N_Real_Literal
5202 or else Nkind (R) = N_Real_Literal
5203 or else Is_Integer_Or_Universal (R))
5204 then
5205 Set_Etype (N, B_Typ);
5207 elsif Etype (N) = Any_Fixed then
5209 -- If no previous errors, this is only possible if one operand is
5210 -- overloaded and the context is universal. Resolve as such.
5212 Set_Etype (N, B_Typ);
5213 end if;
5215 else
5216 if (TL = Universal_Integer or else TL = Universal_Real)
5217 and then
5218 (TR = Universal_Integer or else TR = Universal_Real)
5219 then
5220 Check_For_Visible_Operator (N, B_Typ);
5221 end if;
5223 -- If the context is Universal_Fixed and the operands are also
5224 -- universal fixed, this is an error, unless there is only one
5225 -- applicable fixed_point type (usually Duration).
5227 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
5228 T := Unique_Fixed_Point_Type (N);
5230 if T = Any_Type then
5231 Set_Etype (N, T);
5232 return;
5233 else
5234 Resolve (L, T);
5235 Resolve (R, T);
5236 end if;
5238 else
5239 Resolve (L, B_Typ);
5240 Resolve (R, B_Typ);
5241 end if;
5243 -- If one of the arguments was resolved to a non-universal type.
5244 -- label the result of the operation itself with the same type.
5245 -- Do the same for the universal argument, if any.
5247 T := Intersect_Types (L, R);
5248 Set_Etype (N, Base_Type (T));
5249 Set_Operand_Type (L);
5250 Set_Operand_Type (R);
5251 end if;
5253 Generate_Operator_Reference (N, Typ);
5254 Analyze_Dimension (N);
5255 Eval_Arithmetic_Op (N);
5257 -- In SPARK, a multiplication or division with operands of fixed point
5258 -- types must be qualified or explicitly converted to identify the
5259 -- result type.
5261 if (Is_Fixed_Point_Type (Etype (L))
5262 or else Is_Fixed_Point_Type (Etype (R)))
5263 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5264 and then
5265 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
5266 then
5267 Check_SPARK_05_Restriction
5268 ("operation should be qualified or explicitly converted", N);
5269 end if;
5271 -- Set overflow and division checking bit
5273 if Nkind (N) in N_Op then
5274 if not Overflow_Checks_Suppressed (Etype (N)) then
5275 Enable_Overflow_Check (N);
5276 end if;
5278 -- Give warning if explicit division by zero
5280 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
5281 and then not Division_Checks_Suppressed (Etype (N))
5282 then
5283 Rop := Right_Opnd (N);
5285 if Compile_Time_Known_Value (Rop)
5286 and then ((Is_Integer_Type (Etype (Rop))
5287 and then Expr_Value (Rop) = Uint_0)
5288 or else
5289 (Is_Real_Type (Etype (Rop))
5290 and then Expr_Value_R (Rop) = Ureal_0))
5291 then
5292 -- Specialize the warning message according to the operation.
5293 -- The following warnings are for the case
5295 case Nkind (N) is
5296 when N_Op_Divide =>
5298 -- For division, we have two cases, for float division
5299 -- of an unconstrained float type, on a machine where
5300 -- Machine_Overflows is false, we don't get an exception
5301 -- at run-time, but rather an infinity or Nan. The Nan
5302 -- case is pretty obscure, so just warn about infinities.
5304 if Is_Floating_Point_Type (Typ)
5305 and then not Is_Constrained (Typ)
5306 and then not Machine_Overflows_On_Target
5307 then
5308 Error_Msg_N
5309 ("float division by zero, may generate "
5310 & "'+'/'- infinity??", Right_Opnd (N));
5312 -- For all other cases, we get a Constraint_Error
5314 else
5315 Apply_Compile_Time_Constraint_Error
5316 (N, "division by zero??", CE_Divide_By_Zero,
5317 Loc => Sloc (Right_Opnd (N)));
5318 end if;
5320 when N_Op_Rem =>
5321 Apply_Compile_Time_Constraint_Error
5322 (N, "rem with zero divisor??", CE_Divide_By_Zero,
5323 Loc => Sloc (Right_Opnd (N)));
5325 when N_Op_Mod =>
5326 Apply_Compile_Time_Constraint_Error
5327 (N, "mod with zero divisor??", CE_Divide_By_Zero,
5328 Loc => Sloc (Right_Opnd (N)));
5330 -- Division by zero can only happen with division, rem,
5331 -- and mod operations.
5333 when others =>
5334 raise Program_Error;
5335 end case;
5337 -- Otherwise just set the flag to check at run time
5339 else
5340 Activate_Division_Check (N);
5341 end if;
5342 end if;
5344 -- If Restriction No_Implicit_Conditionals is active, then it is
5345 -- violated if either operand can be negative for mod, or for rem
5346 -- if both operands can be negative.
5348 if Restriction_Check_Required (No_Implicit_Conditionals)
5349 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5350 then
5351 declare
5352 Lo : Uint;
5353 Hi : Uint;
5354 OK : Boolean;
5356 LNeg : Boolean;
5357 RNeg : Boolean;
5358 -- Set if corresponding operand might be negative
5360 begin
5361 Determine_Range
5362 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5363 LNeg := (not OK) or else Lo < 0;
5365 Determine_Range
5366 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5367 RNeg := (not OK) or else Lo < 0;
5369 -- Check if we will be generating conditionals. There are two
5370 -- cases where that can happen, first for REM, the only case
5371 -- is largest negative integer mod -1, where the division can
5372 -- overflow, but we still have to give the right result. The
5373 -- front end generates a test for this annoying case. Here we
5374 -- just test if both operands can be negative (that's what the
5375 -- expander does, so we match its logic here).
5377 -- The second case is mod where either operand can be negative.
5378 -- In this case, the back end has to generate additional tests.
5380 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5381 or else
5382 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5383 then
5384 Check_Restriction (No_Implicit_Conditionals, N);
5385 end if;
5386 end;
5387 end if;
5388 end if;
5390 Check_Unset_Reference (L);
5391 Check_Unset_Reference (R);
5392 Check_Function_Writable_Actuals (N);
5393 end Resolve_Arithmetic_Op;
5395 ------------------
5396 -- Resolve_Call --
5397 ------------------
5399 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5400 function Same_Or_Aliased_Subprograms
5401 (S : Entity_Id;
5402 E : Entity_Id) return Boolean;
5403 -- Returns True if the subprogram entity S is the same as E or else
5404 -- S is an alias of E.
5406 ---------------------------------
5407 -- Same_Or_Aliased_Subprograms --
5408 ---------------------------------
5410 function Same_Or_Aliased_Subprograms
5411 (S : Entity_Id;
5412 E : Entity_Id) return Boolean
5414 Subp_Alias : constant Entity_Id := Alias (S);
5415 begin
5416 return S = E or else (Present (Subp_Alias) and then Subp_Alias = E);
5417 end Same_Or_Aliased_Subprograms;
5419 -- Local variables
5421 Loc : constant Source_Ptr := Sloc (N);
5422 Subp : constant Node_Id := Name (N);
5423 Body_Id : Entity_Id;
5424 I : Interp_Index;
5425 It : Interp;
5426 Nam : Entity_Id;
5427 Nam_Decl : Node_Id;
5428 Nam_UA : Entity_Id;
5429 Norm_OK : Boolean;
5430 Rtype : Entity_Id;
5431 Scop : Entity_Id;
5433 -- Start of processing for Resolve_Call
5435 begin
5436 -- The context imposes a unique interpretation with type Typ on a
5437 -- procedure or function call. Find the entity of the subprogram that
5438 -- yields the expected type, and propagate the corresponding formal
5439 -- constraints on the actuals. The caller has established that an
5440 -- interpretation exists, and emitted an error if not unique.
5442 -- First deal with the case of a call to an access-to-subprogram,
5443 -- dereference made explicit in Analyze_Call.
5445 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5446 if not Is_Overloaded (Subp) then
5447 Nam := Etype (Subp);
5449 else
5450 -- Find the interpretation whose type (a subprogram type) has a
5451 -- return type that is compatible with the context. Analysis of
5452 -- the node has established that one exists.
5454 Nam := Empty;
5456 Get_First_Interp (Subp, I, It);
5457 while Present (It.Typ) loop
5458 if Covers (Typ, Etype (It.Typ)) then
5459 Nam := It.Typ;
5460 exit;
5461 end if;
5463 Get_Next_Interp (I, It);
5464 end loop;
5466 if No (Nam) then
5467 raise Program_Error;
5468 end if;
5469 end if;
5471 -- If the prefix is not an entity, then resolve it
5473 if not Is_Entity_Name (Subp) then
5474 Resolve (Subp, Nam);
5475 end if;
5477 -- For an indirect call, we always invalidate checks, since we do not
5478 -- know whether the subprogram is local or global. Yes we could do
5479 -- better here, e.g. by knowing that there are no local subprograms,
5480 -- but it does not seem worth the effort. Similarly, we kill all
5481 -- knowledge of current constant values.
5483 Kill_Current_Values;
5485 -- If this is a procedure call which is really an entry call, do
5486 -- the conversion of the procedure call to an entry call. Protected
5487 -- operations use the same circuitry because the name in the call
5488 -- can be an arbitrary expression with special resolution rules.
5490 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5491 or else (Is_Entity_Name (Subp)
5492 and then Ekind (Entity (Subp)) = E_Entry)
5493 then
5494 Resolve_Entry_Call (N, Typ);
5495 Check_Elab_Call (N);
5497 -- Kill checks and constant values, as above for indirect case
5498 -- Who knows what happens when another task is activated?
5500 Kill_Current_Values;
5501 return;
5503 -- Normal subprogram call with name established in Resolve
5505 elsif not (Is_Type (Entity (Subp))) then
5506 Nam := Entity (Subp);
5507 Set_Entity_With_Checks (Subp, Nam);
5509 -- Otherwise we must have the case of an overloaded call
5511 else
5512 pragma Assert (Is_Overloaded (Subp));
5514 -- Initialize Nam to prevent warning (we know it will be assigned
5515 -- in the loop below, but the compiler does not know that).
5517 Nam := Empty;
5519 Get_First_Interp (Subp, I, It);
5520 while Present (It.Typ) loop
5521 if Covers (Typ, It.Typ) then
5522 Nam := It.Nam;
5523 Set_Entity_With_Checks (Subp, Nam);
5524 exit;
5525 end if;
5527 Get_Next_Interp (I, It);
5528 end loop;
5529 end if;
5531 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5532 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5533 and then Nkind (Subp) /= N_Explicit_Dereference
5534 and then Present (Parameter_Associations (N))
5535 then
5536 -- The prefix is a parameterless function call that returns an access
5537 -- to subprogram. If parameters are present in the current call, add
5538 -- add an explicit dereference. We use the base type here because
5539 -- within an instance these may be subtypes.
5541 -- The dereference is added either in Analyze_Call or here. Should
5542 -- be consolidated ???
5544 Set_Is_Overloaded (Subp, False);
5545 Set_Etype (Subp, Etype (Nam));
5546 Insert_Explicit_Dereference (Subp);
5547 Nam := Designated_Type (Etype (Nam));
5548 Resolve (Subp, Nam);
5549 end if;
5551 -- Check that a call to Current_Task does not occur in an entry body
5553 if Is_RTE (Nam, RE_Current_Task) then
5554 declare
5555 P : Node_Id;
5557 begin
5558 P := N;
5559 loop
5560 P := Parent (P);
5562 -- Exclude calls that occur within the default of a formal
5563 -- parameter of the entry, since those are evaluated outside
5564 -- of the body.
5566 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5568 if Nkind (P) = N_Entry_Body
5569 or else (Nkind (P) = N_Subprogram_Body
5570 and then Is_Entry_Barrier_Function (P))
5571 then
5572 Rtype := Etype (N);
5573 Error_Msg_Warn := SPARK_Mode /= On;
5574 Error_Msg_NE
5575 ("& should not be used in entry body (RM C.7(17))<<",
5576 N, Nam);
5577 Error_Msg_NE ("\Program_Error [<<", N, Nam);
5578 Rewrite (N,
5579 Make_Raise_Program_Error (Loc,
5580 Reason => PE_Current_Task_In_Entry_Body));
5581 Set_Etype (N, Rtype);
5582 return;
5583 end if;
5584 end loop;
5585 end;
5586 end if;
5588 -- Check that a procedure call does not occur in the context of the
5589 -- entry call statement of a conditional or timed entry call. Note that
5590 -- the case of a call to a subprogram renaming of an entry will also be
5591 -- rejected. The test for N not being an N_Entry_Call_Statement is
5592 -- defensive, covering the possibility that the processing of entry
5593 -- calls might reach this point due to later modifications of the code
5594 -- above.
5596 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5597 and then Nkind (N) /= N_Entry_Call_Statement
5598 and then Entry_Call_Statement (Parent (N)) = N
5599 then
5600 if Ada_Version < Ada_2005 then
5601 Error_Msg_N ("entry call required in select statement", N);
5603 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5604 -- for a procedure_or_entry_call, the procedure_name or
5605 -- procedure_prefix of the procedure_call_statement shall denote
5606 -- an entry renamed by a procedure, or (a view of) a primitive
5607 -- subprogram of a limited interface whose first parameter is
5608 -- a controlling parameter.
5610 elsif Nkind (N) = N_Procedure_Call_Statement
5611 and then not Is_Renamed_Entry (Nam)
5612 and then not Is_Controlling_Limited_Procedure (Nam)
5613 then
5614 Error_Msg_N
5615 ("entry call or dispatching primitive of interface required", N);
5616 end if;
5617 end if;
5619 -- If the SPARK_05 restriction is active, we are not allowed
5620 -- to have a call to a subprogram before we see its completion.
5622 if not Has_Completion (Nam)
5623 and then Restriction_Check_Required (SPARK_05)
5625 -- Don't flag strange internal calls
5627 and then Comes_From_Source (N)
5628 and then Comes_From_Source (Nam)
5630 -- Only flag calls in extended main source
5632 and then In_Extended_Main_Source_Unit (Nam)
5633 and then In_Extended_Main_Source_Unit (N)
5635 -- Exclude enumeration literals from this processing
5637 and then Ekind (Nam) /= E_Enumeration_Literal
5638 then
5639 Check_SPARK_05_Restriction
5640 ("call to subprogram cannot appear before its body", N);
5641 end if;
5643 -- Check that this is not a call to a protected procedure or entry from
5644 -- within a protected function.
5646 Check_Internal_Protected_Use (N, Nam);
5648 -- Freeze the subprogram name if not in a spec-expression. Note that
5649 -- we freeze procedure calls as well as function calls. Procedure calls
5650 -- are not frozen according to the rules (RM 13.14(14)) because it is
5651 -- impossible to have a procedure call to a non-frozen procedure in
5652 -- pure Ada, but in the code that we generate in the expander, this
5653 -- rule needs extending because we can generate procedure calls that
5654 -- need freezing.
5656 -- In Ada 2012, expression functions may be called within pre/post
5657 -- conditions of subsequent functions or expression functions. Such
5658 -- calls do not freeze when they appear within generated bodies,
5659 -- (including the body of another expression function) which would
5660 -- place the freeze node in the wrong scope. An expression function
5661 -- is frozen in the usual fashion, by the appearance of a real body,
5662 -- or at the end of a declarative part.
5664 if Is_Entity_Name (Subp) and then not In_Spec_Expression
5665 and then not Is_Expression_Function (Current_Scope)
5666 and then
5667 (not Is_Expression_Function (Entity (Subp))
5668 or else Scope (Entity (Subp)) = Current_Scope)
5669 then
5670 Freeze_Expression (Subp);
5671 end if;
5673 -- For a predefined operator, the type of the result is the type imposed
5674 -- by context, except for a predefined operation on universal fixed.
5675 -- Otherwise The type of the call is the type returned by the subprogram
5676 -- being called.
5678 if Is_Predefined_Op (Nam) then
5679 if Etype (N) /= Universal_Fixed then
5680 Set_Etype (N, Typ);
5681 end if;
5683 -- If the subprogram returns an array type, and the context requires the
5684 -- component type of that array type, the node is really an indexing of
5685 -- the parameterless call. Resolve as such. A pathological case occurs
5686 -- when the type of the component is an access to the array type. In
5687 -- this case the call is truly ambiguous.
5689 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5690 and then
5691 ((Is_Array_Type (Etype (Nam))
5692 and then Covers (Typ, Component_Type (Etype (Nam))))
5693 or else
5694 (Is_Access_Type (Etype (Nam))
5695 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5696 and then
5697 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))))
5698 then
5699 declare
5700 Index_Node : Node_Id;
5701 New_Subp : Node_Id;
5702 Ret_Type : constant Entity_Id := Etype (Nam);
5704 begin
5705 if Is_Access_Type (Ret_Type)
5706 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5707 then
5708 Error_Msg_N
5709 ("cannot disambiguate function call and indexing", N);
5710 else
5711 New_Subp := Relocate_Node (Subp);
5713 -- The called entity may be an explicit dereference, in which
5714 -- case there is no entity to set.
5716 if Nkind (New_Subp) /= N_Explicit_Dereference then
5717 Set_Entity (Subp, Nam);
5718 end if;
5720 if (Is_Array_Type (Ret_Type)
5721 and then Component_Type (Ret_Type) /= Any_Type)
5722 or else
5723 (Is_Access_Type (Ret_Type)
5724 and then
5725 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5726 then
5727 if Needs_No_Actuals (Nam) then
5729 -- Indexed call to a parameterless function
5731 Index_Node :=
5732 Make_Indexed_Component (Loc,
5733 Prefix =>
5734 Make_Function_Call (Loc, Name => New_Subp),
5735 Expressions => Parameter_Associations (N));
5736 else
5737 -- An Ada 2005 prefixed call to a primitive operation
5738 -- whose first parameter is the prefix. This prefix was
5739 -- prepended to the parameter list, which is actually a
5740 -- list of indexes. Remove the prefix in order to build
5741 -- the proper indexed component.
5743 Index_Node :=
5744 Make_Indexed_Component (Loc,
5745 Prefix =>
5746 Make_Function_Call (Loc,
5747 Name => New_Subp,
5748 Parameter_Associations =>
5749 New_List
5750 (Remove_Head (Parameter_Associations (N)))),
5751 Expressions => Parameter_Associations (N));
5752 end if;
5754 -- Preserve the parenthesis count of the node
5756 Set_Paren_Count (Index_Node, Paren_Count (N));
5758 -- Since we are correcting a node classification error made
5759 -- by the parser, we call Replace rather than Rewrite.
5761 Replace (N, Index_Node);
5763 Set_Etype (Prefix (N), Ret_Type);
5764 Set_Etype (N, Typ);
5765 Resolve_Indexed_Component (N, Typ);
5766 Check_Elab_Call (Prefix (N));
5767 end if;
5768 end if;
5770 return;
5771 end;
5773 else
5774 Set_Etype (N, Etype (Nam));
5775 end if;
5777 -- In the case where the call is to an overloaded subprogram, Analyze
5778 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5779 -- such a case Normalize_Actuals needs to be called once more to order
5780 -- the actuals correctly. Otherwise the call will have the ordering
5781 -- given by the last overloaded subprogram whether this is the correct
5782 -- one being called or not.
5784 if Is_Overloaded (Subp) then
5785 Normalize_Actuals (N, Nam, False, Norm_OK);
5786 pragma Assert (Norm_OK);
5787 end if;
5789 -- In any case, call is fully resolved now. Reset Overload flag, to
5790 -- prevent subsequent overload resolution if node is analyzed again
5792 Set_Is_Overloaded (Subp, False);
5793 Set_Is_Overloaded (N, False);
5795 -- If we are calling the current subprogram from immediately within its
5796 -- body, then that is the case where we can sometimes detect cases of
5797 -- infinite recursion statically. Do not try this in case restriction
5798 -- No_Recursion is in effect anyway, and do it only for source calls.
5800 if Comes_From_Source (N) then
5801 Scop := Current_Scope;
5803 -- Check violation of SPARK_05 restriction which does not permit
5804 -- a subprogram body to contain a call to the subprogram directly.
5806 if Restriction_Check_Required (SPARK_05)
5807 and then Same_Or_Aliased_Subprograms (Nam, Scop)
5808 then
5809 Check_SPARK_05_Restriction
5810 ("subprogram may not contain direct call to itself", N);
5811 end if;
5813 -- Issue warning for possible infinite recursion in the absence
5814 -- of the No_Recursion restriction.
5816 if Same_Or_Aliased_Subprograms (Nam, Scop)
5817 and then not Restriction_Active (No_Recursion)
5818 and then Check_Infinite_Recursion (N)
5819 then
5820 -- Here we detected and flagged an infinite recursion, so we do
5821 -- not need to test the case below for further warnings. Also we
5822 -- are all done if we now have a raise SE node.
5824 if Nkind (N) = N_Raise_Storage_Error then
5825 return;
5826 end if;
5828 -- If call is to immediately containing subprogram, then check for
5829 -- the case of a possible run-time detectable infinite recursion.
5831 else
5832 Scope_Loop : while Scop /= Standard_Standard loop
5833 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5835 -- Although in general case, recursion is not statically
5836 -- checkable, the case of calling an immediately containing
5837 -- subprogram is easy to catch.
5839 Check_Restriction (No_Recursion, N);
5841 -- If the recursive call is to a parameterless subprogram,
5842 -- then even if we can't statically detect infinite
5843 -- recursion, this is pretty suspicious, and we output a
5844 -- warning. Furthermore, we will try later to detect some
5845 -- cases here at run time by expanding checking code (see
5846 -- Detect_Infinite_Recursion in package Exp_Ch6).
5848 -- If the recursive call is within a handler, do not emit a
5849 -- warning, because this is a common idiom: loop until input
5850 -- is correct, catch illegal input in handler and restart.
5852 if No (First_Formal (Nam))
5853 and then Etype (Nam) = Standard_Void_Type
5854 and then not Error_Posted (N)
5855 and then Nkind (Parent (N)) /= N_Exception_Handler
5856 then
5857 -- For the case of a procedure call. We give the message
5858 -- only if the call is the first statement in a sequence
5859 -- of statements, or if all previous statements are
5860 -- simple assignments. This is simply a heuristic to
5861 -- decrease false positives, without losing too many good
5862 -- warnings. The idea is that these previous statements
5863 -- may affect global variables the procedure depends on.
5864 -- We also exclude raise statements, that may arise from
5865 -- constraint checks and are probably unrelated to the
5866 -- intended control flow.
5868 if Nkind (N) = N_Procedure_Call_Statement
5869 and then Is_List_Member (N)
5870 then
5871 declare
5872 P : Node_Id;
5873 begin
5874 P := Prev (N);
5875 while Present (P) loop
5876 if not Nkind_In (P, N_Assignment_Statement,
5877 N_Raise_Constraint_Error)
5878 then
5879 exit Scope_Loop;
5880 end if;
5882 Prev (P);
5883 end loop;
5884 end;
5885 end if;
5887 -- Do not give warning if we are in a conditional context
5889 declare
5890 K : constant Node_Kind := Nkind (Parent (N));
5891 begin
5892 if (K = N_Loop_Statement
5893 and then Present (Iteration_Scheme (Parent (N))))
5894 or else K = N_If_Statement
5895 or else K = N_Elsif_Part
5896 or else K = N_Case_Statement_Alternative
5897 then
5898 exit Scope_Loop;
5899 end if;
5900 end;
5902 -- Here warning is to be issued
5904 Set_Has_Recursive_Call (Nam);
5905 Error_Msg_Warn := SPARK_Mode /= On;
5906 Error_Msg_N ("possible infinite recursion<<!", N);
5907 Error_Msg_N ("\Storage_Error ]<<!", N);
5908 end if;
5910 exit Scope_Loop;
5911 end if;
5913 Scop := Scope (Scop);
5914 end loop Scope_Loop;
5915 end if;
5916 end if;
5918 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5920 Check_Obsolescent_2005_Entity (Nam, Subp);
5922 -- If subprogram name is a predefined operator, it was given in
5923 -- functional notation. Replace call node with operator node, so
5924 -- that actuals can be resolved appropriately.
5926 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5927 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5928 return;
5930 elsif Present (Alias (Nam))
5931 and then Is_Predefined_Op (Alias (Nam))
5932 then
5933 Resolve_Actuals (N, Nam);
5934 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5935 return;
5936 end if;
5938 -- Create a transient scope if the resulting type requires it
5940 -- There are several notable exceptions:
5942 -- a) In init procs, the transient scope overhead is not needed, and is
5943 -- even incorrect when the call is a nested initialization call for a
5944 -- component whose expansion may generate adjust calls. However, if the
5945 -- call is some other procedure call within an initialization procedure
5946 -- (for example a call to Create_Task in the init_proc of the task
5947 -- run-time record) a transient scope must be created around this call.
5949 -- b) Enumeration literal pseudo-calls need no transient scope
5951 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5952 -- functions) do not use the secondary stack even though the return
5953 -- type may be unconstrained.
5955 -- d) Calls to a build-in-place function, since such functions may
5956 -- allocate their result directly in a target object, and cases where
5957 -- the result does get allocated in the secondary stack are checked for
5958 -- within the specialized Exp_Ch6 procedures for expanding those
5959 -- build-in-place calls.
5961 -- e) If the subprogram is marked Inline_Always, then even if it returns
5962 -- an unconstrained type the call does not require use of the secondary
5963 -- stack. However, inlining will only take place if the body to inline
5964 -- is already present. It may not be available if e.g. the subprogram is
5965 -- declared in a child instance.
5967 -- If this is an initialization call for a type whose construction
5968 -- uses the secondary stack, and it is not a nested call to initialize
5969 -- a component, we do need to create a transient scope for it. We
5970 -- check for this by traversing the type in Check_Initialization_Call.
5972 if Is_Inlined (Nam)
5973 and then Has_Pragma_Inline (Nam)
5974 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5975 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5976 then
5977 null;
5979 elsif Ekind (Nam) = E_Enumeration_Literal
5980 or else Is_Build_In_Place_Function (Nam)
5981 or else Is_Intrinsic_Subprogram (Nam)
5982 then
5983 null;
5985 elsif Expander_Active
5986 and then Is_Type (Etype (Nam))
5987 and then Requires_Transient_Scope (Etype (Nam))
5988 and then
5989 (not Within_Init_Proc
5990 or else
5991 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5992 then
5993 Establish_Transient_Scope (N, Sec_Stack => True);
5995 -- If the call appears within the bounds of a loop, it will
5996 -- be rewritten and reanalyzed, nothing left to do here.
5998 if Nkind (N) /= N_Function_Call then
5999 return;
6000 end if;
6002 elsif Is_Init_Proc (Nam)
6003 and then not Within_Init_Proc
6004 then
6005 Check_Initialization_Call (N, Nam);
6006 end if;
6008 -- A protected function cannot be called within the definition of the
6009 -- enclosing protected type.
6011 if Is_Protected_Type (Scope (Nam))
6012 and then In_Open_Scopes (Scope (Nam))
6013 and then not Has_Completion (Scope (Nam))
6014 then
6015 Error_Msg_NE
6016 ("& cannot be called before end of protected definition", N, Nam);
6017 end if;
6019 -- Propagate interpretation to actuals, and add default expressions
6020 -- where needed.
6022 if Present (First_Formal (Nam)) then
6023 Resolve_Actuals (N, Nam);
6025 -- Overloaded literals are rewritten as function calls, for purpose of
6026 -- resolution. After resolution, we can replace the call with the
6027 -- literal itself.
6029 elsif Ekind (Nam) = E_Enumeration_Literal then
6030 Copy_Node (Subp, N);
6031 Resolve_Entity_Name (N, Typ);
6033 -- Avoid validation, since it is a static function call
6035 Generate_Reference (Nam, Subp);
6036 return;
6037 end if;
6039 -- If the subprogram is not global, then kill all saved values and
6040 -- checks. This is a bit conservative, since in many cases we could do
6041 -- better, but it is not worth the effort. Similarly, we kill constant
6042 -- values. However we do not need to do this for internal entities
6043 -- (unless they are inherited user-defined subprograms), since they
6044 -- are not in the business of molesting local values.
6046 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6047 -- kill all checks and values for calls to global subprograms. This
6048 -- takes care of the case where an access to a local subprogram is
6049 -- taken, and could be passed directly or indirectly and then called
6050 -- from almost any context.
6052 -- Note: we do not do this step till after resolving the actuals. That
6053 -- way we still take advantage of the current value information while
6054 -- scanning the actuals.
6056 -- We suppress killing values if we are processing the nodes associated
6057 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6058 -- type kills all the values as part of analyzing the code that
6059 -- initializes the dispatch tables.
6061 if Inside_Freezing_Actions = 0
6062 and then (not Is_Library_Level_Entity (Nam)
6063 or else Suppress_Value_Tracking_On_Call
6064 (Nearest_Dynamic_Scope (Current_Scope)))
6065 and then (Comes_From_Source (Nam)
6066 or else (Present (Alias (Nam))
6067 and then Comes_From_Source (Alias (Nam))))
6068 then
6069 Kill_Current_Values;
6070 end if;
6072 -- If we are warning about unread OUT parameters, this is the place to
6073 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
6074 -- after the above call to Kill_Current_Values (since that call clears
6075 -- the Last_Assignment field of all local variables).
6077 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
6078 and then Comes_From_Source (N)
6079 and then In_Extended_Main_Source_Unit (N)
6080 then
6081 declare
6082 F : Entity_Id;
6083 A : Node_Id;
6085 begin
6086 F := First_Formal (Nam);
6087 A := First_Actual (N);
6088 while Present (F) and then Present (A) loop
6089 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
6090 and then Warn_On_Modified_As_Out_Parameter (F)
6091 and then Is_Entity_Name (A)
6092 and then Present (Entity (A))
6093 and then Comes_From_Source (N)
6094 and then Safe_To_Capture_Value (N, Entity (A))
6095 then
6096 Set_Last_Assignment (Entity (A), A);
6097 end if;
6099 Next_Formal (F);
6100 Next_Actual (A);
6101 end loop;
6102 end;
6103 end if;
6105 -- If the subprogram is a primitive operation, check whether or not
6106 -- it is a correct dispatching call.
6108 if Is_Overloadable (Nam)
6109 and then Is_Dispatching_Operation (Nam)
6110 then
6111 Check_Dispatching_Call (N);
6113 elsif Ekind (Nam) /= E_Subprogram_Type
6114 and then Is_Abstract_Subprogram (Nam)
6115 and then not In_Instance
6116 then
6117 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
6118 end if;
6120 -- If this is a dispatching call, generate the appropriate reference,
6121 -- for better source navigation in GPS.
6123 if Is_Overloadable (Nam)
6124 and then Present (Controlling_Argument (N))
6125 then
6126 Generate_Reference (Nam, Subp, 'R');
6128 -- Normal case, not a dispatching call: generate a call reference
6130 else
6131 Generate_Reference (Nam, Subp, 's');
6132 end if;
6134 if Is_Intrinsic_Subprogram (Nam) then
6135 Check_Intrinsic_Call (N);
6136 end if;
6138 -- Check for violation of restriction No_Specific_Termination_Handlers
6139 -- and warn on a potentially blocking call to Abort_Task.
6141 if Restriction_Check_Required (No_Specific_Termination_Handlers)
6142 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
6143 or else
6144 Is_RTE (Nam, RE_Specific_Handler))
6145 then
6146 Check_Restriction (No_Specific_Termination_Handlers, N);
6148 elsif Is_RTE (Nam, RE_Abort_Task) then
6149 Check_Potentially_Blocking_Operation (N);
6150 end if;
6152 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
6153 -- timing event violates restriction No_Relative_Delay (AI-0211). We
6154 -- need to check the second argument to determine whether it is an
6155 -- absolute or relative timing event.
6157 if Restriction_Check_Required (No_Relative_Delay)
6158 and then Is_RTE (Nam, RE_Set_Handler)
6159 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
6160 then
6161 Check_Restriction (No_Relative_Delay, N);
6162 end if;
6164 -- Issue an error for a call to an eliminated subprogram. This routine
6165 -- will not perform the check if the call appears within a default
6166 -- expression.
6168 Check_For_Eliminated_Subprogram (Subp, Nam);
6170 -- In formal mode, the primitive operations of a tagged type or type
6171 -- extension do not include functions that return the tagged type.
6173 if Nkind (N) = N_Function_Call
6174 and then Is_Tagged_Type (Etype (N))
6175 and then Is_Entity_Name (Name (N))
6176 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
6177 then
6178 Check_SPARK_05_Restriction ("function not inherited", N);
6179 end if;
6181 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
6182 -- class-wide and the call dispatches on result in a context that does
6183 -- not provide a tag, the call raises Program_Error.
6185 if Nkind (N) = N_Function_Call
6186 and then In_Instance
6187 and then Is_Generic_Actual_Type (Typ)
6188 and then Is_Class_Wide_Type (Typ)
6189 and then Has_Controlling_Result (Nam)
6190 and then Nkind (Parent (N)) = N_Object_Declaration
6191 then
6192 -- Verify that none of the formals are controlling
6194 declare
6195 Call_OK : Boolean := False;
6196 F : Entity_Id;
6198 begin
6199 F := First_Formal (Nam);
6200 while Present (F) loop
6201 if Is_Controlling_Formal (F) then
6202 Call_OK := True;
6203 exit;
6204 end if;
6206 Next_Formal (F);
6207 end loop;
6209 if not Call_OK then
6210 Error_Msg_Warn := SPARK_Mode /= On;
6211 Error_Msg_N ("!cannot determine tag of result<<", N);
6212 Error_Msg_N ("\Program_Error [<<!", N);
6213 Insert_Action (N,
6214 Make_Raise_Program_Error (Sloc (N),
6215 Reason => PE_Explicit_Raise));
6216 end if;
6217 end;
6218 end if;
6220 -- Check for calling a function with OUT or IN OUT parameter when the
6221 -- calling context (us right now) is not Ada 2012, so does not allow
6222 -- OUT or IN OUT parameters in function calls.
6224 if Ada_Version < Ada_2012
6225 and then Ekind (Nam) = E_Function
6226 and then Has_Out_Or_In_Out_Parameter (Nam)
6227 then
6228 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
6229 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
6230 end if;
6232 -- Check the dimensions of the actuals in the call. For function calls,
6233 -- propagate the dimensions from the returned type to N.
6235 Analyze_Dimension_Call (N, Nam);
6237 -- All done, evaluate call and deal with elaboration issues
6239 Eval_Call (N);
6240 Check_Elab_Call (N);
6242 -- In GNATprove mode, expansion is disabled, but we want to inline some
6243 -- subprograms to facilitate formal verification. Indirect calls through
6244 -- a subprogram type or within a generic cannot be inlined. Inlining is
6245 -- performed only for calls subject to SPARK_Mode on.
6247 if GNATprove_Mode
6248 and then SPARK_Mode = On
6249 and then Is_Overloadable (Nam)
6250 and then not Inside_A_Generic
6251 then
6252 Nam_UA := Ultimate_Alias (Nam);
6253 Nam_Decl := Unit_Declaration_Node (Nam_UA);
6255 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
6256 Body_Id := Corresponding_Body (Nam_Decl);
6258 -- Nothing to do if the subprogram is not eligible for inlining in
6259 -- GNATprove mode.
6261 if not Is_Inlined_Always (Nam_UA)
6262 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
6263 then
6264 null;
6266 -- Calls cannot be inlined inside assertions, as GNATprove treats
6267 -- assertions as logic expressions.
6269 elsif In_Assertion_Expr /= 0 then
6270 Error_Msg_NE ("?no contextual analysis of &", N, Nam);
6271 Error_Msg_N ("\call appears in assertion expression", N);
6272 Set_Is_Inlined_Always (Nam_UA, False);
6274 -- Calls cannot be inlined inside default expressions
6276 elsif In_Default_Expr then
6277 Error_Msg_NE ("?no contextual analysis of &", N, Nam);
6278 Error_Msg_N ("\call appears in default expression", N);
6279 Set_Is_Inlined_Always (Nam_UA, False);
6281 -- Inlining should not be performed during pre-analysis
6283 elsif Full_Analysis then
6285 -- With the one-pass inlining technique, a call cannot be
6286 -- inlined if the corresponding body has not been seen yet.
6288 if No (Body_Id) then
6289 Error_Msg_NE
6290 ("?no contextual analysis of & (body not seen yet)",
6291 N, Nam);
6292 Set_Is_Inlined_Always (Nam_UA, False);
6294 -- Nothing to do if there is no body to inline, indicating that
6295 -- the subprogram is not suitable for inlining in GNATprove
6296 -- mode.
6298 elsif No (Body_To_Inline (Nam_Decl)) then
6299 null;
6301 -- Calls cannot be inlined inside potentially unevaluated
6302 -- expressions, as this would create complex actions inside
6303 -- expressions, that are not handled by GNATprove.
6305 elsif Is_Potentially_Unevaluated (N) then
6306 Error_Msg_NE ("?no contextual analysis of &", N, Nam);
6307 Error_Msg_N
6308 ("\call appears in potentially unevaluated context", N);
6309 Set_Is_Inlined_Always (Nam_UA, False);
6311 -- Otherwise, inline the call
6313 else
6314 Expand_Inlined_Call (N, Nam_UA, Nam);
6315 end if;
6316 end if;
6317 end if;
6318 end if;
6320 Warn_On_Overlapping_Actuals (Nam, N);
6321 end Resolve_Call;
6323 -----------------------------
6324 -- Resolve_Case_Expression --
6325 -----------------------------
6327 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
6328 Alt : Node_Id;
6330 begin
6331 Alt := First (Alternatives (N));
6332 while Present (Alt) loop
6333 Resolve (Expression (Alt), Typ);
6334 Next (Alt);
6335 end loop;
6337 Set_Etype (N, Typ);
6338 Eval_Case_Expression (N);
6339 end Resolve_Case_Expression;
6341 -------------------------------
6342 -- Resolve_Character_Literal --
6343 -------------------------------
6345 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
6346 B_Typ : constant Entity_Id := Base_Type (Typ);
6347 C : Entity_Id;
6349 begin
6350 -- Verify that the character does belong to the type of the context
6352 Set_Etype (N, B_Typ);
6353 Eval_Character_Literal (N);
6355 -- Wide_Wide_Character literals must always be defined, since the set
6356 -- of wide wide character literals is complete, i.e. if a character
6357 -- literal is accepted by the parser, then it is OK for wide wide
6358 -- character (out of range character literals are rejected).
6360 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6361 return;
6363 -- Always accept character literal for type Any_Character, which
6364 -- occurs in error situations and in comparisons of literals, both
6365 -- of which should accept all literals.
6367 elsif B_Typ = Any_Character then
6368 return;
6370 -- For Standard.Character or a type derived from it, check that the
6371 -- literal is in range.
6373 elsif Root_Type (B_Typ) = Standard_Character then
6374 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6375 return;
6376 end if;
6378 -- For Standard.Wide_Character or a type derived from it, check that the
6379 -- literal is in range.
6381 elsif Root_Type (B_Typ) = Standard_Wide_Character then
6382 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6383 return;
6384 end if;
6386 -- For Standard.Wide_Wide_Character or a type derived from it, we
6387 -- know the literal is in range, since the parser checked.
6389 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6390 return;
6392 -- If the entity is already set, this has already been resolved in a
6393 -- generic context, or comes from expansion. Nothing else to do.
6395 elsif Present (Entity (N)) then
6396 return;
6398 -- Otherwise we have a user defined character type, and we can use the
6399 -- standard visibility mechanisms to locate the referenced entity.
6401 else
6402 C := Current_Entity (N);
6403 while Present (C) loop
6404 if Etype (C) = B_Typ then
6405 Set_Entity_With_Checks (N, C);
6406 Generate_Reference (C, N);
6407 return;
6408 end if;
6410 C := Homonym (C);
6411 end loop;
6412 end if;
6414 -- If we fall through, then the literal does not match any of the
6415 -- entries of the enumeration type. This isn't just a constraint error
6416 -- situation, it is an illegality (see RM 4.2).
6418 Error_Msg_NE
6419 ("character not defined for }", N, First_Subtype (B_Typ));
6420 end Resolve_Character_Literal;
6422 ---------------------------
6423 -- Resolve_Comparison_Op --
6424 ---------------------------
6426 -- Context requires a boolean type, and plays no role in resolution.
6427 -- Processing identical to that for equality operators. The result type is
6428 -- the base type, which matters when pathological subtypes of booleans with
6429 -- limited ranges are used.
6431 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6432 L : constant Node_Id := Left_Opnd (N);
6433 R : constant Node_Id := Right_Opnd (N);
6434 T : Entity_Id;
6436 begin
6437 -- If this is an intrinsic operation which is not predefined, use the
6438 -- types of its declared arguments to resolve the possibly overloaded
6439 -- operands. Otherwise the operands are unambiguous and specify the
6440 -- expected type.
6442 if Scope (Entity (N)) /= Standard_Standard then
6443 T := Etype (First_Entity (Entity (N)));
6445 else
6446 T := Find_Unique_Type (L, R);
6448 if T = Any_Fixed then
6449 T := Unique_Fixed_Point_Type (L);
6450 end if;
6451 end if;
6453 Set_Etype (N, Base_Type (Typ));
6454 Generate_Reference (T, N, ' ');
6456 -- Skip remaining processing if already set to Any_Type
6458 if T = Any_Type then
6459 return;
6460 end if;
6462 -- Deal with other error cases
6464 if T = Any_String or else
6465 T = Any_Composite or else
6466 T = Any_Character
6467 then
6468 if T = Any_Character then
6469 Ambiguous_Character (L);
6470 else
6471 Error_Msg_N ("ambiguous operands for comparison", N);
6472 end if;
6474 Set_Etype (N, Any_Type);
6475 return;
6476 end if;
6478 -- Resolve the operands if types OK
6480 Resolve (L, T);
6481 Resolve (R, T);
6482 Check_Unset_Reference (L);
6483 Check_Unset_Reference (R);
6484 Generate_Operator_Reference (N, T);
6485 Check_Low_Bound_Tested (N);
6487 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6488 -- types or array types except String.
6490 if Is_Boolean_Type (T) then
6491 Check_SPARK_05_Restriction
6492 ("comparison is not defined on Boolean type", N);
6494 elsif Is_Array_Type (T)
6495 and then Base_Type (T) /= Standard_String
6496 then
6497 Check_SPARK_05_Restriction
6498 ("comparison is not defined on array types other than String", N);
6499 end if;
6501 -- Check comparison on unordered enumeration
6503 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
6504 Error_Msg_Sloc := Sloc (Etype (L));
6505 Error_Msg_NE
6506 ("comparison on unordered enumeration type& declared#?U?",
6507 N, Etype (L));
6508 end if;
6510 -- Evaluate the relation (note we do this after the above check since
6511 -- this Eval call may change N to True/False.
6513 Analyze_Dimension (N);
6514 Eval_Relational_Op (N);
6515 end Resolve_Comparison_Op;
6517 -----------------------------------------
6518 -- Resolve_Discrete_Subtype_Indication --
6519 -----------------------------------------
6521 procedure Resolve_Discrete_Subtype_Indication
6522 (N : Node_Id;
6523 Typ : Entity_Id)
6525 R : Node_Id;
6526 S : Entity_Id;
6528 begin
6529 Analyze (Subtype_Mark (N));
6530 S := Entity (Subtype_Mark (N));
6532 if Nkind (Constraint (N)) /= N_Range_Constraint then
6533 Error_Msg_N ("expect range constraint for discrete type", N);
6534 Set_Etype (N, Any_Type);
6536 else
6537 R := Range_Expression (Constraint (N));
6539 if R = Error then
6540 return;
6541 end if;
6543 Analyze (R);
6545 if Base_Type (S) /= Base_Type (Typ) then
6546 Error_Msg_NE
6547 ("expect subtype of }", N, First_Subtype (Typ));
6549 -- Rewrite the constraint as a range of Typ
6550 -- to allow compilation to proceed further.
6552 Set_Etype (N, Typ);
6553 Rewrite (Low_Bound (R),
6554 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6555 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6556 Attribute_Name => Name_First));
6557 Rewrite (High_Bound (R),
6558 Make_Attribute_Reference (Sloc (High_Bound (R)),
6559 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6560 Attribute_Name => Name_First));
6562 else
6563 Resolve (R, Typ);
6564 Set_Etype (N, Etype (R));
6566 -- Additionally, we must check that the bounds are compatible
6567 -- with the given subtype, which might be different from the
6568 -- type of the context.
6570 Apply_Range_Check (R, S);
6572 -- ??? If the above check statically detects a Constraint_Error
6573 -- it replaces the offending bound(s) of the range R with a
6574 -- Constraint_Error node. When the itype which uses these bounds
6575 -- is frozen the resulting call to Duplicate_Subexpr generates
6576 -- a new temporary for the bounds.
6578 -- Unfortunately there are other itypes that are also made depend
6579 -- on these bounds, so when Duplicate_Subexpr is called they get
6580 -- a forward reference to the newly created temporaries and Gigi
6581 -- aborts on such forward references. This is probably sign of a
6582 -- more fundamental problem somewhere else in either the order of
6583 -- itype freezing or the way certain itypes are constructed.
6585 -- To get around this problem we call Remove_Side_Effects right
6586 -- away if either bounds of R are a Constraint_Error.
6588 declare
6589 L : constant Node_Id := Low_Bound (R);
6590 H : constant Node_Id := High_Bound (R);
6592 begin
6593 if Nkind (L) = N_Raise_Constraint_Error then
6594 Remove_Side_Effects (L);
6595 end if;
6597 if Nkind (H) = N_Raise_Constraint_Error then
6598 Remove_Side_Effects (H);
6599 end if;
6600 end;
6602 Check_Unset_Reference (Low_Bound (R));
6603 Check_Unset_Reference (High_Bound (R));
6604 end if;
6605 end if;
6606 end Resolve_Discrete_Subtype_Indication;
6608 -------------------------
6609 -- Resolve_Entity_Name --
6610 -------------------------
6612 -- Used to resolve identifiers and expanded names
6614 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6615 function Appears_In_Check (Nod : Node_Id) return Boolean;
6616 -- Denote whether an arbitrary node Nod appears in a check node
6618 function Is_OK_Volatile_Context
6619 (Context : Node_Id;
6620 Obj_Ref : Node_Id) return Boolean;
6621 -- Determine whether node Context denotes a "non-interfering context"
6622 -- (as defined in SPARK RM 7.1.3(13)) where volatile reference Obj_Ref
6623 -- can safely reside.
6625 ----------------------
6626 -- Appears_In_Check --
6627 ----------------------
6629 function Appears_In_Check (Nod : Node_Id) return Boolean is
6630 Par : Node_Id;
6632 begin
6633 -- Climb the parent chain looking for a check node
6635 Par := Nod;
6636 while Present (Par) loop
6637 if Nkind (Par) in N_Raise_xxx_Error then
6638 return True;
6640 -- Prevent the search from going too far
6642 elsif Is_Body_Or_Package_Declaration (Par) then
6643 exit;
6644 end if;
6646 Par := Parent (Par);
6647 end loop;
6649 return False;
6650 end Appears_In_Check;
6652 ----------------------------
6653 -- Is_OK_Volatile_Context --
6654 ----------------------------
6656 function Is_OK_Volatile_Context
6657 (Context : Node_Id;
6658 Obj_Ref : Node_Id) return Boolean
6660 begin
6661 -- The volatile object appears on either side of an assignment
6663 if Nkind (Context) = N_Assignment_Statement then
6664 return True;
6666 -- The volatile object is part of the initialization expression of
6667 -- another object. Ensure that the climb of the parent chain came
6668 -- from the expression side and not from the name side.
6670 elsif Nkind (Context) = N_Object_Declaration
6671 and then Present (Expression (Context))
6672 and then Expression (Context) = Obj_Ref
6673 then
6674 return True;
6676 -- The volatile object appears as an actual parameter in a call to an
6677 -- instance of Unchecked_Conversion whose result is renamed.
6679 elsif Nkind (Context) = N_Function_Call
6680 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
6681 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
6682 then
6683 return True;
6685 -- The volatile object appears as the prefix of a name occurring
6686 -- in a non-interfering context.
6688 elsif Nkind_In (Context, N_Attribute_Reference,
6689 N_Indexed_Component,
6690 N_Selected_Component,
6691 N_Slice)
6692 and then Prefix (Context) = Obj_Ref
6693 and then Is_OK_Volatile_Context
6694 (Context => Parent (Context),
6695 Obj_Ref => Context)
6696 then
6697 return True;
6699 -- The volatile object appears as the expression of a type conversion
6700 -- occurring in a non-interfering context.
6702 elsif Nkind_In (Context, N_Type_Conversion,
6703 N_Unchecked_Type_Conversion)
6704 and then Expression (Context) = Obj_Ref
6705 and then Is_OK_Volatile_Context
6706 (Context => Parent (Context),
6707 Obj_Ref => Context)
6708 then
6709 return True;
6711 -- Allow references to volatile objects in various checks. This is
6712 -- not a direct SPARK 2014 requirement.
6714 elsif Appears_In_Check (Context) then
6715 return True;
6717 else
6718 return False;
6719 end if;
6720 end Is_OK_Volatile_Context;
6722 -- Local variables
6724 E : constant Entity_Id := Entity (N);
6725 Par : constant Node_Id := Parent (N);
6727 -- Start of processing for Resolve_Entity_Name
6729 begin
6730 -- If garbage from errors, set to Any_Type and return
6732 if No (E) and then Total_Errors_Detected /= 0 then
6733 Set_Etype (N, Any_Type);
6734 return;
6735 end if;
6737 -- Replace named numbers by corresponding literals. Note that this is
6738 -- the one case where Resolve_Entity_Name must reset the Etype, since
6739 -- it is currently marked as universal.
6741 if Ekind (E) = E_Named_Integer then
6742 Set_Etype (N, Typ);
6743 Eval_Named_Integer (N);
6745 elsif Ekind (E) = E_Named_Real then
6746 Set_Etype (N, Typ);
6747 Eval_Named_Real (N);
6749 -- For enumeration literals, we need to make sure that a proper style
6750 -- check is done, since such literals are overloaded, and thus we did
6751 -- not do a style check during the first phase of analysis.
6753 elsif Ekind (E) = E_Enumeration_Literal then
6754 Set_Entity_With_Checks (N, E);
6755 Eval_Entity_Name (N);
6757 -- Case of subtype name appearing as an operand in expression
6759 elsif Is_Type (E) then
6761 -- Allow use of subtype if it is a concurrent type where we are
6762 -- currently inside the body. This will eventually be expanded into a
6763 -- call to Self (for tasks) or _object (for protected objects). Any
6764 -- other use of a subtype is invalid.
6766 if Is_Concurrent_Type (E)
6767 and then In_Open_Scopes (E)
6768 then
6769 null;
6771 -- Any other use is an error
6773 else
6774 Error_Msg_N
6775 ("invalid use of subtype mark in expression or call", N);
6776 end if;
6778 -- Check discriminant use if entity is discriminant in current scope,
6779 -- i.e. discriminant of record or concurrent type currently being
6780 -- analyzed. Uses in corresponding body are unrestricted.
6782 elsif Ekind (E) = E_Discriminant
6783 and then Scope (E) = Current_Scope
6784 and then not Has_Completion (Current_Scope)
6785 then
6786 Check_Discriminant_Use (N);
6788 -- A parameterless generic function cannot appear in a context that
6789 -- requires resolution.
6791 elsif Ekind (E) = E_Generic_Function then
6792 Error_Msg_N ("illegal use of generic function", N);
6794 elsif Ekind (E) = E_Out_Parameter
6795 and then Ada_Version = Ada_83
6796 and then (Nkind (Parent (N)) in N_Op
6797 or else (Nkind (Parent (N)) = N_Assignment_Statement
6798 and then N = Expression (Parent (N)))
6799 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6800 then
6801 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6803 -- In all other cases, just do the possible static evaluation
6805 else
6806 -- A deferred constant that appears in an expression must have a
6807 -- completion, unless it has been removed by in-place expansion of
6808 -- an aggregate.
6810 if Ekind (E) = E_Constant
6811 and then Comes_From_Source (E)
6812 and then No (Constant_Value (E))
6813 and then Is_Frozen (Etype (E))
6814 and then not In_Spec_Expression
6815 and then not Is_Imported (E)
6816 then
6817 if No_Initialization (Parent (E))
6818 or else (Present (Full_View (E))
6819 and then No_Initialization (Parent (Full_View (E))))
6820 then
6821 null;
6822 else
6823 Error_Msg_N (
6824 "deferred constant is frozen before completion", N);
6825 end if;
6826 end if;
6828 Eval_Entity_Name (N);
6829 end if;
6831 -- An effectively volatile object subject to enabled properties
6832 -- Async_Writers or Effective_Reads must appear in a specific context.
6833 -- The following checks are only relevant when SPARK_Mode is on as they
6834 -- are not standard Ada legality rules.
6836 if SPARK_Mode = On
6837 and then Is_Object (E)
6838 and then Is_Effectively_Volatile (E)
6839 and then Comes_From_Source (E)
6840 and then
6841 (Async_Writers_Enabled (E) or else Effective_Reads_Enabled (E))
6842 then
6843 -- The effectively volatile objects appears in a "non-interfering
6844 -- context" as defined in SPARK RM 7.1.3(13).
6846 if Is_OK_Volatile_Context (Par, N) then
6847 null;
6849 -- Assume that references to effectively volatile objects that appear
6850 -- as actual parameters in a procedure call are always legal. The
6851 -- full legality check is done when the actuals are resolved.
6853 elsif Nkind (Par) = N_Procedure_Call_Statement then
6854 null;
6856 -- Otherwise the context causes a side effect with respect to the
6857 -- effectively volatile object.
6859 else
6860 Error_Msg_N
6861 ("volatile object cannot appear in this context "
6862 & "(SPARK RM 7.1.3(13))", N);
6863 end if;
6864 end if;
6865 end Resolve_Entity_Name;
6867 -------------------
6868 -- Resolve_Entry --
6869 -------------------
6871 procedure Resolve_Entry (Entry_Name : Node_Id) is
6872 Loc : constant Source_Ptr := Sloc (Entry_Name);
6873 Nam : Entity_Id;
6874 New_N : Node_Id;
6875 S : Entity_Id;
6876 Tsk : Entity_Id;
6877 E_Name : Node_Id;
6878 Index : Node_Id;
6880 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6881 -- If the bounds of the entry family being called depend on task
6882 -- discriminants, build a new index subtype where a discriminant is
6883 -- replaced with the value of the discriminant of the target task.
6884 -- The target task is the prefix of the entry name in the call.
6886 -----------------------
6887 -- Actual_Index_Type --
6888 -----------------------
6890 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6891 Typ : constant Entity_Id := Entry_Index_Type (E);
6892 Tsk : constant Entity_Id := Scope (E);
6893 Lo : constant Node_Id := Type_Low_Bound (Typ);
6894 Hi : constant Node_Id := Type_High_Bound (Typ);
6895 New_T : Entity_Id;
6897 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6898 -- If the bound is given by a discriminant, replace with a reference
6899 -- to the discriminant of the same name in the target task. If the
6900 -- entry name is the target of a requeue statement and the entry is
6901 -- in the current protected object, the bound to be used is the
6902 -- discriminal of the object (see Apply_Range_Checks for details of
6903 -- the transformation).
6905 -----------------------------
6906 -- Actual_Discriminant_Ref --
6907 -----------------------------
6909 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6910 Typ : constant Entity_Id := Etype (Bound);
6911 Ref : Node_Id;
6913 begin
6914 Remove_Side_Effects (Bound);
6916 if not Is_Entity_Name (Bound)
6917 or else Ekind (Entity (Bound)) /= E_Discriminant
6918 then
6919 return Bound;
6921 elsif Is_Protected_Type (Tsk)
6922 and then In_Open_Scopes (Tsk)
6923 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6924 then
6925 -- Note: here Bound denotes a discriminant of the corresponding
6926 -- record type tskV, whose discriminal is a formal of the
6927 -- init-proc tskVIP. What we want is the body discriminal,
6928 -- which is associated to the discriminant of the original
6929 -- concurrent type tsk.
6931 return New_Occurrence_Of
6932 (Find_Body_Discriminal (Entity (Bound)), Loc);
6934 else
6935 Ref :=
6936 Make_Selected_Component (Loc,
6937 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6938 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6939 Analyze (Ref);
6940 Resolve (Ref, Typ);
6941 return Ref;
6942 end if;
6943 end Actual_Discriminant_Ref;
6945 -- Start of processing for Actual_Index_Type
6947 begin
6948 if not Has_Discriminants (Tsk)
6949 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6950 then
6951 return Entry_Index_Type (E);
6953 else
6954 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6955 Set_Etype (New_T, Base_Type (Typ));
6956 Set_Size_Info (New_T, Typ);
6957 Set_RM_Size (New_T, RM_Size (Typ));
6958 Set_Scalar_Range (New_T,
6959 Make_Range (Sloc (Entry_Name),
6960 Low_Bound => Actual_Discriminant_Ref (Lo),
6961 High_Bound => Actual_Discriminant_Ref (Hi)));
6963 return New_T;
6964 end if;
6965 end Actual_Index_Type;
6967 -- Start of processing of Resolve_Entry
6969 begin
6970 -- Find name of entry being called, and resolve prefix of name with its
6971 -- own type. The prefix can be overloaded, and the name and signature of
6972 -- the entry must be taken into account.
6974 if Nkind (Entry_Name) = N_Indexed_Component then
6976 -- Case of dealing with entry family within the current tasks
6978 E_Name := Prefix (Entry_Name);
6980 else
6981 E_Name := Entry_Name;
6982 end if;
6984 if Is_Entity_Name (E_Name) then
6986 -- Entry call to an entry (or entry family) in the current task. This
6987 -- is legal even though the task will deadlock. Rewrite as call to
6988 -- current task.
6990 -- This can also be a call to an entry in an enclosing task. If this
6991 -- is a single task, we have to retrieve its name, because the scope
6992 -- of the entry is the task type, not the object. If the enclosing
6993 -- task is a task type, the identity of the task is given by its own
6994 -- self variable.
6996 -- Finally this can be a requeue on an entry of the same task or
6997 -- protected object.
6999 S := Scope (Entity (E_Name));
7001 for J in reverse 0 .. Scope_Stack.Last loop
7002 if Is_Task_Type (Scope_Stack.Table (J).Entity)
7003 and then not Comes_From_Source (S)
7004 then
7005 -- S is an enclosing task or protected object. The concurrent
7006 -- declaration has been converted into a type declaration, and
7007 -- the object itself has an object declaration that follows
7008 -- the type in the same declarative part.
7010 Tsk := Next_Entity (S);
7011 while Etype (Tsk) /= S loop
7012 Next_Entity (Tsk);
7013 end loop;
7015 S := Tsk;
7016 exit;
7018 elsif S = Scope_Stack.Table (J).Entity then
7020 -- Call to current task. Will be transformed into call to Self
7022 exit;
7024 end if;
7025 end loop;
7027 New_N :=
7028 Make_Selected_Component (Loc,
7029 Prefix => New_Occurrence_Of (S, Loc),
7030 Selector_Name =>
7031 New_Occurrence_Of (Entity (E_Name), Loc));
7032 Rewrite (E_Name, New_N);
7033 Analyze (E_Name);
7035 elsif Nkind (Entry_Name) = N_Selected_Component
7036 and then Is_Overloaded (Prefix (Entry_Name))
7037 then
7038 -- Use the entry name (which must be unique at this point) to find
7039 -- the prefix that returns the corresponding task/protected type.
7041 declare
7042 Pref : constant Node_Id := Prefix (Entry_Name);
7043 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
7044 I : Interp_Index;
7045 It : Interp;
7047 begin
7048 Get_First_Interp (Pref, I, It);
7049 while Present (It.Typ) loop
7050 if Scope (Ent) = It.Typ then
7051 Set_Etype (Pref, It.Typ);
7052 exit;
7053 end if;
7055 Get_Next_Interp (I, It);
7056 end loop;
7057 end;
7058 end if;
7060 if Nkind (Entry_Name) = N_Selected_Component then
7061 Resolve (Prefix (Entry_Name));
7063 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7064 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
7065 Resolve (Prefix (Prefix (Entry_Name)));
7066 Index := First (Expressions (Entry_Name));
7067 Resolve (Index, Entry_Index_Type (Nam));
7069 -- Up to this point the expression could have been the actual in a
7070 -- simple entry call, and be given by a named association.
7072 if Nkind (Index) = N_Parameter_Association then
7073 Error_Msg_N ("expect expression for entry index", Index);
7074 else
7075 Apply_Range_Check (Index, Actual_Index_Type (Nam));
7076 end if;
7077 end if;
7078 end Resolve_Entry;
7080 ------------------------
7081 -- Resolve_Entry_Call --
7082 ------------------------
7084 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
7085 Entry_Name : constant Node_Id := Name (N);
7086 Loc : constant Source_Ptr := Sloc (Entry_Name);
7087 Actuals : List_Id;
7088 First_Named : Node_Id;
7089 Nam : Entity_Id;
7090 Norm_OK : Boolean;
7091 Obj : Node_Id;
7092 Was_Over : Boolean;
7094 begin
7095 -- We kill all checks here, because it does not seem worth the effort to
7096 -- do anything better, an entry call is a big operation.
7098 Kill_All_Checks;
7100 -- Processing of the name is similar for entry calls and protected
7101 -- operation calls. Once the entity is determined, we can complete
7102 -- the resolution of the actuals.
7104 -- The selector may be overloaded, in the case of a protected object
7105 -- with overloaded functions. The type of the context is used for
7106 -- resolution.
7108 if Nkind (Entry_Name) = N_Selected_Component
7109 and then Is_Overloaded (Selector_Name (Entry_Name))
7110 and then Typ /= Standard_Void_Type
7111 then
7112 declare
7113 I : Interp_Index;
7114 It : Interp;
7116 begin
7117 Get_First_Interp (Selector_Name (Entry_Name), I, It);
7118 while Present (It.Typ) loop
7119 if Covers (Typ, It.Typ) then
7120 Set_Entity (Selector_Name (Entry_Name), It.Nam);
7121 Set_Etype (Entry_Name, It.Typ);
7123 Generate_Reference (It.Typ, N, ' ');
7124 end if;
7126 Get_Next_Interp (I, It);
7127 end loop;
7128 end;
7129 end if;
7131 Resolve_Entry (Entry_Name);
7133 if Nkind (Entry_Name) = N_Selected_Component then
7135 -- Simple entry call
7137 Nam := Entity (Selector_Name (Entry_Name));
7138 Obj := Prefix (Entry_Name);
7139 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
7141 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7143 -- Call to member of entry family
7145 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
7146 Obj := Prefix (Prefix (Entry_Name));
7147 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
7148 end if;
7150 -- We cannot in general check the maximum depth of protected entry calls
7151 -- at compile time. But we can tell that any protected entry call at all
7152 -- violates a specified nesting depth of zero.
7154 if Is_Protected_Type (Scope (Nam)) then
7155 Check_Restriction (Max_Entry_Queue_Length, N);
7156 end if;
7158 -- Use context type to disambiguate a protected function that can be
7159 -- called without actuals and that returns an array type, and where the
7160 -- argument list may be an indexing of the returned value.
7162 if Ekind (Nam) = E_Function
7163 and then Needs_No_Actuals (Nam)
7164 and then Present (Parameter_Associations (N))
7165 and then
7166 ((Is_Array_Type (Etype (Nam))
7167 and then Covers (Typ, Component_Type (Etype (Nam))))
7169 or else (Is_Access_Type (Etype (Nam))
7170 and then Is_Array_Type (Designated_Type (Etype (Nam)))
7171 and then
7172 Covers
7173 (Typ,
7174 Component_Type (Designated_Type (Etype (Nam))))))
7175 then
7176 declare
7177 Index_Node : Node_Id;
7179 begin
7180 Index_Node :=
7181 Make_Indexed_Component (Loc,
7182 Prefix =>
7183 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
7184 Expressions => Parameter_Associations (N));
7186 -- Since we are correcting a node classification error made by the
7187 -- parser, we call Replace rather than Rewrite.
7189 Replace (N, Index_Node);
7190 Set_Etype (Prefix (N), Etype (Nam));
7191 Set_Etype (N, Typ);
7192 Resolve_Indexed_Component (N, Typ);
7193 return;
7194 end;
7195 end if;
7197 if Ekind_In (Nam, E_Entry, E_Entry_Family)
7198 and then Present (PPC_Wrapper (Nam))
7199 and then Current_Scope /= PPC_Wrapper (Nam)
7200 then
7201 -- Rewrite as call to the precondition wrapper, adding the task
7202 -- object to the list of actuals. If the call is to a member of an
7203 -- entry family, include the index as well.
7205 declare
7206 New_Call : Node_Id;
7207 New_Actuals : List_Id;
7209 begin
7210 New_Actuals := New_List (Obj);
7212 if Nkind (Entry_Name) = N_Indexed_Component then
7213 Append_To (New_Actuals,
7214 New_Copy_Tree (First (Expressions (Entry_Name))));
7215 end if;
7217 Append_List (Parameter_Associations (N), New_Actuals);
7218 New_Call :=
7219 Make_Procedure_Call_Statement (Loc,
7220 Name =>
7221 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
7222 Parameter_Associations => New_Actuals);
7223 Rewrite (N, New_Call);
7225 -- Preanalyze and resolve new call. Current procedure is called
7226 -- from Resolve_Call, after which expansion will take place.
7228 Preanalyze_And_Resolve (N);
7229 return;
7230 end;
7231 end if;
7233 -- The operation name may have been overloaded. Order the actuals
7234 -- according to the formals of the resolved entity, and set the return
7235 -- type to that of the operation.
7237 if Was_Over then
7238 Normalize_Actuals (N, Nam, False, Norm_OK);
7239 pragma Assert (Norm_OK);
7240 Set_Etype (N, Etype (Nam));
7241 end if;
7243 Resolve_Actuals (N, Nam);
7244 Check_Internal_Protected_Use (N, Nam);
7246 -- Create a call reference to the entry
7248 Generate_Reference (Nam, Entry_Name, 's');
7250 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
7251 Check_Potentially_Blocking_Operation (N);
7252 end if;
7254 -- Verify that a procedure call cannot masquerade as an entry
7255 -- call where an entry call is expected.
7257 if Ekind (Nam) = E_Procedure then
7258 if Nkind (Parent (N)) = N_Entry_Call_Alternative
7259 and then N = Entry_Call_Statement (Parent (N))
7260 then
7261 Error_Msg_N ("entry call required in select statement", N);
7263 elsif Nkind (Parent (N)) = N_Triggering_Alternative
7264 and then N = Triggering_Statement (Parent (N))
7265 then
7266 Error_Msg_N ("triggering statement cannot be procedure call", N);
7268 elsif Ekind (Scope (Nam)) = E_Task_Type
7269 and then not In_Open_Scopes (Scope (Nam))
7270 then
7271 Error_Msg_N ("task has no entry with this name", Entry_Name);
7272 end if;
7273 end if;
7275 -- After resolution, entry calls and protected procedure calls are
7276 -- changed into entry calls, for expansion. The structure of the node
7277 -- does not change, so it can safely be done in place. Protected
7278 -- function calls must keep their structure because they are
7279 -- subexpressions.
7281 if Ekind (Nam) /= E_Function then
7283 -- A protected operation that is not a function may modify the
7284 -- corresponding object, and cannot apply to a constant. If this
7285 -- is an internal call, the prefix is the type itself.
7287 if Is_Protected_Type (Scope (Nam))
7288 and then not Is_Variable (Obj)
7289 and then (not Is_Entity_Name (Obj)
7290 or else not Is_Type (Entity (Obj)))
7291 then
7292 Error_Msg_N
7293 ("prefix of protected procedure or entry call must be variable",
7294 Entry_Name);
7295 end if;
7297 Actuals := Parameter_Associations (N);
7298 First_Named := First_Named_Actual (N);
7300 Rewrite (N,
7301 Make_Entry_Call_Statement (Loc,
7302 Name => Entry_Name,
7303 Parameter_Associations => Actuals));
7305 Set_First_Named_Actual (N, First_Named);
7306 Set_Analyzed (N, True);
7308 -- Protected functions can return on the secondary stack, in which
7309 -- case we must trigger the transient scope mechanism.
7311 elsif Expander_Active
7312 and then Requires_Transient_Scope (Etype (Nam))
7313 then
7314 Establish_Transient_Scope (N, Sec_Stack => True);
7315 end if;
7316 end Resolve_Entry_Call;
7318 -------------------------
7319 -- Resolve_Equality_Op --
7320 -------------------------
7322 -- Both arguments must have the same type, and the boolean context does
7323 -- not participate in the resolution. The first pass verifies that the
7324 -- interpretation is not ambiguous, and the type of the left argument is
7325 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
7326 -- are strings or aggregates, allocators, or Null, they are ambiguous even
7327 -- though they carry a single (universal) type. Diagnose this case here.
7329 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
7330 L : constant Node_Id := Left_Opnd (N);
7331 R : constant Node_Id := Right_Opnd (N);
7332 T : Entity_Id := Find_Unique_Type (L, R);
7334 procedure Check_If_Expression (Cond : Node_Id);
7335 -- The resolution rule for if expressions requires that each such must
7336 -- have a unique type. This means that if several dependent expressions
7337 -- are of a non-null anonymous access type, and the context does not
7338 -- impose an expected type (as can be the case in an equality operation)
7339 -- the expression must be rejected.
7341 procedure Explain_Redundancy (N : Node_Id);
7342 -- Attempt to explain the nature of a redundant comparison with True. If
7343 -- the expression N is too complex, this routine issues a general error
7344 -- message.
7346 function Find_Unique_Access_Type return Entity_Id;
7347 -- In the case of allocators and access attributes, the context must
7348 -- provide an indication of the specific access type to be used. If
7349 -- one operand is of such a "generic" access type, check whether there
7350 -- is a specific visible access type that has the same designated type.
7351 -- This is semantically dubious, and of no interest to any real code,
7352 -- but c48008a makes it all worthwhile.
7354 -------------------------
7355 -- Check_If_Expression --
7356 -------------------------
7358 procedure Check_If_Expression (Cond : Node_Id) is
7359 Then_Expr : Node_Id;
7360 Else_Expr : Node_Id;
7362 begin
7363 if Nkind (Cond) = N_If_Expression then
7364 Then_Expr := Next (First (Expressions (Cond)));
7365 Else_Expr := Next (Then_Expr);
7367 if Nkind (Then_Expr) /= N_Null
7368 and then Nkind (Else_Expr) /= N_Null
7369 then
7370 Error_Msg_N ("cannot determine type of if expression", Cond);
7371 end if;
7372 end if;
7373 end Check_If_Expression;
7375 ------------------------
7376 -- Explain_Redundancy --
7377 ------------------------
7379 procedure Explain_Redundancy (N : Node_Id) is
7380 Error : Name_Id;
7381 Val : Node_Id;
7382 Val_Id : Entity_Id;
7384 begin
7385 Val := N;
7387 -- Strip the operand down to an entity
7389 loop
7390 if Nkind (Val) = N_Selected_Component then
7391 Val := Selector_Name (Val);
7392 else
7393 exit;
7394 end if;
7395 end loop;
7397 -- The construct denotes an entity
7399 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
7400 Val_Id := Entity (Val);
7402 -- Do not generate an error message when the comparison is done
7403 -- against the enumeration literal Standard.True.
7405 if Ekind (Val_Id) /= E_Enumeration_Literal then
7407 -- Build a customized error message
7409 Name_Len := 0;
7410 Add_Str_To_Name_Buffer ("?r?");
7412 if Ekind (Val_Id) = E_Component then
7413 Add_Str_To_Name_Buffer ("component ");
7415 elsif Ekind (Val_Id) = E_Constant then
7416 Add_Str_To_Name_Buffer ("constant ");
7418 elsif Ekind (Val_Id) = E_Discriminant then
7419 Add_Str_To_Name_Buffer ("discriminant ");
7421 elsif Is_Formal (Val_Id) then
7422 Add_Str_To_Name_Buffer ("parameter ");
7424 elsif Ekind (Val_Id) = E_Variable then
7425 Add_Str_To_Name_Buffer ("variable ");
7426 end if;
7428 Add_Str_To_Name_Buffer ("& is always True!");
7429 Error := Name_Find;
7431 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
7432 end if;
7434 -- The construct is too complex to disect, issue a general message
7436 else
7437 Error_Msg_N ("?r?expression is always True!", Val);
7438 end if;
7439 end Explain_Redundancy;
7441 -----------------------------
7442 -- Find_Unique_Access_Type --
7443 -----------------------------
7445 function Find_Unique_Access_Type return Entity_Id is
7446 Acc : Entity_Id;
7447 E : Entity_Id;
7448 S : Entity_Id;
7450 begin
7451 if Ekind_In (Etype (R), E_Allocator_Type,
7452 E_Access_Attribute_Type)
7453 then
7454 Acc := Designated_Type (Etype (R));
7456 elsif Ekind_In (Etype (L), E_Allocator_Type,
7457 E_Access_Attribute_Type)
7458 then
7459 Acc := Designated_Type (Etype (L));
7460 else
7461 return Empty;
7462 end if;
7464 S := Current_Scope;
7465 while S /= Standard_Standard loop
7466 E := First_Entity (S);
7467 while Present (E) loop
7468 if Is_Type (E)
7469 and then Is_Access_Type (E)
7470 and then Ekind (E) /= E_Allocator_Type
7471 and then Designated_Type (E) = Base_Type (Acc)
7472 then
7473 return E;
7474 end if;
7476 Next_Entity (E);
7477 end loop;
7479 S := Scope (S);
7480 end loop;
7482 return Empty;
7483 end Find_Unique_Access_Type;
7485 -- Start of processing for Resolve_Equality_Op
7487 begin
7488 Set_Etype (N, Base_Type (Typ));
7489 Generate_Reference (T, N, ' ');
7491 if T = Any_Fixed then
7492 T := Unique_Fixed_Point_Type (L);
7493 end if;
7495 if T /= Any_Type then
7496 if T = Any_String or else
7497 T = Any_Composite or else
7498 T = Any_Character
7499 then
7500 if T = Any_Character then
7501 Ambiguous_Character (L);
7502 else
7503 Error_Msg_N ("ambiguous operands for equality", N);
7504 end if;
7506 Set_Etype (N, Any_Type);
7507 return;
7509 elsif T = Any_Access
7510 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
7511 then
7512 T := Find_Unique_Access_Type;
7514 if No (T) then
7515 Error_Msg_N ("ambiguous operands for equality", N);
7516 Set_Etype (N, Any_Type);
7517 return;
7518 end if;
7520 -- If expressions must have a single type, and if the context does
7521 -- not impose one the dependent expressions cannot be anonymous
7522 -- access types.
7524 -- Why no similar processing for case expressions???
7526 elsif Ada_Version >= Ada_2012
7527 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
7528 E_Anonymous_Access_Subprogram_Type)
7529 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
7530 E_Anonymous_Access_Subprogram_Type)
7531 then
7532 Check_If_Expression (L);
7533 Check_If_Expression (R);
7534 end if;
7536 Resolve (L, T);
7537 Resolve (R, T);
7539 -- In SPARK, equality operators = and /= for array types other than
7540 -- String are only defined when, for each index position, the
7541 -- operands have equal static bounds.
7543 if Is_Array_Type (T) then
7545 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7546 -- operation if not needed.
7548 if Restriction_Check_Required (SPARK_05)
7549 and then Base_Type (T) /= Standard_String
7550 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7551 and then Etype (L) /= Any_Composite -- or else L in error
7552 and then Etype (R) /= Any_Composite -- or else R in error
7553 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
7554 then
7555 Check_SPARK_05_Restriction
7556 ("array types should have matching static bounds", N);
7557 end if;
7558 end if;
7560 -- If the unique type is a class-wide type then it will be expanded
7561 -- into a dispatching call to the predefined primitive. Therefore we
7562 -- check here for potential violation of such restriction.
7564 if Is_Class_Wide_Type (T) then
7565 Check_Restriction (No_Dispatching_Calls, N);
7566 end if;
7568 if Warn_On_Redundant_Constructs
7569 and then Comes_From_Source (N)
7570 and then Comes_From_Source (R)
7571 and then Is_Entity_Name (R)
7572 and then Entity (R) = Standard_True
7573 then
7574 Error_Msg_N -- CODEFIX
7575 ("?r?comparison with True is redundant!", N);
7576 Explain_Redundancy (Original_Node (R));
7577 end if;
7579 Check_Unset_Reference (L);
7580 Check_Unset_Reference (R);
7581 Generate_Operator_Reference (N, T);
7582 Check_Low_Bound_Tested (N);
7584 -- If this is an inequality, it may be the implicit inequality
7585 -- created for a user-defined operation, in which case the corres-
7586 -- ponding equality operation is not intrinsic, and the operation
7587 -- cannot be constant-folded. Else fold.
7589 if Nkind (N) = N_Op_Eq
7590 or else Comes_From_Source (Entity (N))
7591 or else Ekind (Entity (N)) = E_Operator
7592 or else Is_Intrinsic_Subprogram
7593 (Corresponding_Equality (Entity (N)))
7594 then
7595 Analyze_Dimension (N);
7596 Eval_Relational_Op (N);
7598 elsif Nkind (N) = N_Op_Ne
7599 and then Is_Abstract_Subprogram (Entity (N))
7600 then
7601 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7602 end if;
7604 -- Ada 2005: If one operand is an anonymous access type, convert the
7605 -- other operand to it, to ensure that the underlying types match in
7606 -- the back-end. Same for access_to_subprogram, and the conversion
7607 -- verifies that the types are subtype conformant.
7609 -- We apply the same conversion in the case one of the operands is a
7610 -- private subtype of the type of the other.
7612 -- Why the Expander_Active test here ???
7614 if Expander_Active
7615 and then
7616 (Ekind_In (T, E_Anonymous_Access_Type,
7617 E_Anonymous_Access_Subprogram_Type)
7618 or else Is_Private_Type (T))
7619 then
7620 if Etype (L) /= T then
7621 Rewrite (L,
7622 Make_Unchecked_Type_Conversion (Sloc (L),
7623 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7624 Expression => Relocate_Node (L)));
7625 Analyze_And_Resolve (L, T);
7626 end if;
7628 if (Etype (R)) /= T then
7629 Rewrite (R,
7630 Make_Unchecked_Type_Conversion (Sloc (R),
7631 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7632 Expression => Relocate_Node (R)));
7633 Analyze_And_Resolve (R, T);
7634 end if;
7635 end if;
7636 end if;
7637 end Resolve_Equality_Op;
7639 ----------------------------------
7640 -- Resolve_Explicit_Dereference --
7641 ----------------------------------
7643 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
7644 Loc : constant Source_Ptr := Sloc (N);
7645 New_N : Node_Id;
7646 P : constant Node_Id := Prefix (N);
7648 P_Typ : Entity_Id;
7649 -- The candidate prefix type, if overloaded
7651 I : Interp_Index;
7652 It : Interp;
7654 begin
7655 Check_Fully_Declared_Prefix (Typ, P);
7656 P_Typ := Empty;
7658 -- A useful optimization: check whether the dereference denotes an
7659 -- element of a container, and if so rewrite it as a call to the
7660 -- corresponding Element function.
7662 -- Disabled for now, on advice of ARG. A more restricted form of the
7663 -- predicate might be acceptable ???
7665 -- if Is_Container_Element (N) then
7666 -- return;
7667 -- end if;
7669 if Is_Overloaded (P) then
7671 -- Use the context type to select the prefix that has the correct
7672 -- designated type. Keep the first match, which will be the inner-
7673 -- most.
7675 Get_First_Interp (P, I, It);
7677 while Present (It.Typ) loop
7678 if Is_Access_Type (It.Typ)
7679 and then Covers (Typ, Designated_Type (It.Typ))
7680 then
7681 if No (P_Typ) then
7682 P_Typ := It.Typ;
7683 end if;
7685 -- Remove access types that do not match, but preserve access
7686 -- to subprogram interpretations, in case a further dereference
7687 -- is needed (see below).
7689 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
7690 Remove_Interp (I);
7691 end if;
7693 Get_Next_Interp (I, It);
7694 end loop;
7696 if Present (P_Typ) then
7697 Resolve (P, P_Typ);
7698 Set_Etype (N, Designated_Type (P_Typ));
7700 else
7701 -- If no interpretation covers the designated type of the prefix,
7702 -- this is the pathological case where not all implementations of
7703 -- the prefix allow the interpretation of the node as a call. Now
7704 -- that the expected type is known, Remove other interpretations
7705 -- from prefix, rewrite it as a call, and resolve again, so that
7706 -- the proper call node is generated.
7708 Get_First_Interp (P, I, It);
7709 while Present (It.Typ) loop
7710 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
7711 Remove_Interp (I);
7712 end if;
7714 Get_Next_Interp (I, It);
7715 end loop;
7717 New_N :=
7718 Make_Function_Call (Loc,
7719 Name =>
7720 Make_Explicit_Dereference (Loc,
7721 Prefix => P),
7722 Parameter_Associations => New_List);
7724 Save_Interps (N, New_N);
7725 Rewrite (N, New_N);
7726 Analyze_And_Resolve (N, Typ);
7727 return;
7728 end if;
7730 -- If not overloaded, resolve P with its own type
7732 else
7733 Resolve (P);
7734 end if;
7736 if Is_Access_Type (Etype (P)) then
7737 Apply_Access_Check (N);
7738 end if;
7740 -- If the designated type is a packed unconstrained array type, and the
7741 -- explicit dereference is not in the context of an attribute reference,
7742 -- then we must compute and set the actual subtype, since it is needed
7743 -- by Gigi. The reason we exclude the attribute case is that this is
7744 -- handled fine by Gigi, and in fact we use such attributes to build the
7745 -- actual subtype. We also exclude generated code (which builds actual
7746 -- subtypes directly if they are needed).
7748 if Is_Array_Type (Etype (N))
7749 and then Is_Packed (Etype (N))
7750 and then not Is_Constrained (Etype (N))
7751 and then Nkind (Parent (N)) /= N_Attribute_Reference
7752 and then Comes_From_Source (N)
7753 then
7754 Set_Etype (N, Get_Actual_Subtype (N));
7755 end if;
7757 -- Note: No Eval processing is required for an explicit dereference,
7758 -- because such a name can never be static.
7760 end Resolve_Explicit_Dereference;
7762 -------------------------------------
7763 -- Resolve_Expression_With_Actions --
7764 -------------------------------------
7766 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7767 begin
7768 Set_Etype (N, Typ);
7770 -- If N has no actions, and its expression has been constant folded,
7771 -- then rewrite N as just its expression. Note, we can't do this in
7772 -- the general case of Is_Empty_List (Actions (N)) as this would cause
7773 -- Expression (N) to be expanded again.
7775 if Is_Empty_List (Actions (N))
7776 and then Compile_Time_Known_Value (Expression (N))
7777 then
7778 Rewrite (N, Expression (N));
7779 end if;
7780 end Resolve_Expression_With_Actions;
7782 ----------------------------------
7783 -- Resolve_Generalized_Indexing --
7784 ----------------------------------
7786 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
7787 Indexing : constant Node_Id := Generalized_Indexing (N);
7788 Call : Node_Id;
7789 Indices : List_Id;
7790 Pref : Node_Id;
7792 begin
7793 -- In ASIS mode, propagate the information about the indices back to
7794 -- to the original indexing node. The generalized indexing is either
7795 -- a function call, or a dereference of one. The actuals include the
7796 -- prefix of the original node, which is the container expression.
7798 if ASIS_Mode then
7799 Resolve (Indexing, Typ);
7800 Set_Etype (N, Etype (Indexing));
7801 Set_Is_Overloaded (N, False);
7803 Call := Indexing;
7804 while Nkind_In (Call, N_Explicit_Dereference, N_Selected_Component)
7805 loop
7806 Call := Prefix (Call);
7807 end loop;
7809 if Nkind (Call) = N_Function_Call then
7810 Indices := Parameter_Associations (Call);
7811 Pref := Remove_Head (Indices);
7812 Set_Expressions (N, Indices);
7813 Set_Prefix (N, Pref);
7814 end if;
7816 else
7817 Rewrite (N, Indexing);
7818 Resolve (N, Typ);
7819 end if;
7820 end Resolve_Generalized_Indexing;
7822 ---------------------------
7823 -- Resolve_If_Expression --
7824 ---------------------------
7826 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
7827 Condition : constant Node_Id := First (Expressions (N));
7828 Then_Expr : constant Node_Id := Next (Condition);
7829 Else_Expr : Node_Id := Next (Then_Expr);
7830 Else_Typ : Entity_Id;
7831 Then_Typ : Entity_Id;
7833 begin
7834 Resolve (Condition, Any_Boolean);
7835 Resolve (Then_Expr, Typ);
7836 Then_Typ := Etype (Then_Expr);
7838 -- When the "then" expression is of a scalar subtype different from the
7839 -- result subtype, then insert a conversion to ensure the generation of
7840 -- a constraint check. The same is done for the else part below, again
7841 -- comparing subtypes rather than base types.
7843 if Is_Scalar_Type (Then_Typ)
7844 and then Then_Typ /= Typ
7845 then
7846 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
7847 Analyze_And_Resolve (Then_Expr, Typ);
7848 end if;
7850 -- If ELSE expression present, just resolve using the determined type
7852 if Present (Else_Expr) then
7853 Resolve (Else_Expr, Typ);
7854 Else_Typ := Etype (Else_Expr);
7856 if Is_Scalar_Type (Else_Typ)
7857 and then Else_Typ /= Typ
7858 then
7859 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
7860 Analyze_And_Resolve (Else_Expr, Typ);
7861 end if;
7863 -- If no ELSE expression is present, root type must be Standard.Boolean
7864 -- and we provide a Standard.True result converted to the appropriate
7865 -- Boolean type (in case it is a derived boolean type).
7867 elsif Root_Type (Typ) = Standard_Boolean then
7868 Else_Expr :=
7869 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
7870 Analyze_And_Resolve (Else_Expr, Typ);
7871 Append_To (Expressions (N), Else_Expr);
7873 else
7874 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
7875 Append_To (Expressions (N), Error);
7876 end if;
7878 Set_Etype (N, Typ);
7879 Eval_If_Expression (N);
7880 end Resolve_If_Expression;
7882 -------------------------------
7883 -- Resolve_Indexed_Component --
7884 -------------------------------
7886 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7887 Name : constant Node_Id := Prefix (N);
7888 Expr : Node_Id;
7889 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7890 Index : Node_Id;
7892 begin
7893 if Present (Generalized_Indexing (N)) then
7894 Resolve_Generalized_Indexing (N, Typ);
7895 return;
7896 end if;
7898 if Is_Overloaded (Name) then
7900 -- Use the context type to select the prefix that yields the correct
7901 -- component type.
7903 declare
7904 I : Interp_Index;
7905 It : Interp;
7906 I1 : Interp_Index := 0;
7907 P : constant Node_Id := Prefix (N);
7908 Found : Boolean := False;
7910 begin
7911 Get_First_Interp (P, I, It);
7912 while Present (It.Typ) loop
7913 if (Is_Array_Type (It.Typ)
7914 and then Covers (Typ, Component_Type (It.Typ)))
7915 or else (Is_Access_Type (It.Typ)
7916 and then Is_Array_Type (Designated_Type (It.Typ))
7917 and then
7918 Covers
7919 (Typ,
7920 Component_Type (Designated_Type (It.Typ))))
7921 then
7922 if Found then
7923 It := Disambiguate (P, I1, I, Any_Type);
7925 if It = No_Interp then
7926 Error_Msg_N ("ambiguous prefix for indexing", N);
7927 Set_Etype (N, Typ);
7928 return;
7930 else
7931 Found := True;
7932 Array_Type := It.Typ;
7933 I1 := I;
7934 end if;
7936 else
7937 Found := True;
7938 Array_Type := It.Typ;
7939 I1 := I;
7940 end if;
7941 end if;
7943 Get_Next_Interp (I, It);
7944 end loop;
7945 end;
7947 else
7948 Array_Type := Etype (Name);
7949 end if;
7951 Resolve (Name, Array_Type);
7952 Array_Type := Get_Actual_Subtype_If_Available (Name);
7954 -- If prefix is access type, dereference to get real array type.
7955 -- Note: we do not apply an access check because the expander always
7956 -- introduces an explicit dereference, and the check will happen there.
7958 if Is_Access_Type (Array_Type) then
7959 Array_Type := Designated_Type (Array_Type);
7960 end if;
7962 -- If name was overloaded, set component type correctly now
7963 -- If a misplaced call to an entry family (which has no index types)
7964 -- return. Error will be diagnosed from calling context.
7966 if Is_Array_Type (Array_Type) then
7967 Set_Etype (N, Component_Type (Array_Type));
7968 else
7969 return;
7970 end if;
7972 Index := First_Index (Array_Type);
7973 Expr := First (Expressions (N));
7975 -- The prefix may have resolved to a string literal, in which case its
7976 -- etype has a special representation. This is only possible currently
7977 -- if the prefix is a static concatenation, written in functional
7978 -- notation.
7980 if Ekind (Array_Type) = E_String_Literal_Subtype then
7981 Resolve (Expr, Standard_Positive);
7983 else
7984 while Present (Index) and Present (Expr) loop
7985 Resolve (Expr, Etype (Index));
7986 Check_Unset_Reference (Expr);
7988 if Is_Scalar_Type (Etype (Expr)) then
7989 Apply_Scalar_Range_Check (Expr, Etype (Index));
7990 else
7991 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7992 end if;
7994 Next_Index (Index);
7995 Next (Expr);
7996 end loop;
7997 end if;
7999 Analyze_Dimension (N);
8001 -- Do not generate the warning on suspicious index if we are analyzing
8002 -- package Ada.Tags; otherwise we will report the warning with the
8003 -- Prims_Ptr field of the dispatch table.
8005 if Scope (Etype (Prefix (N))) = Standard_Standard
8006 or else not
8007 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
8008 Ada_Tags)
8009 then
8010 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
8011 Eval_Indexed_Component (N);
8012 end if;
8014 -- If the array type is atomic, and the component is not atomic, then
8015 -- this is worth a warning, since we have a situation where the access
8016 -- to the component may cause extra read/writes of the atomic array
8017 -- object, or partial word accesses, which could be unexpected.
8019 if Nkind (N) = N_Indexed_Component
8020 and then Is_Atomic_Ref_With_Address (N)
8021 and then not (Has_Atomic_Components (Array_Type)
8022 or else (Is_Entity_Name (Prefix (N))
8023 and then Has_Atomic_Components
8024 (Entity (Prefix (N)))))
8025 and then not Is_Atomic (Component_Type (Array_Type))
8026 then
8027 Error_Msg_N ("??access to non-atomic component of atomic array",
8028 Prefix (N));
8029 Error_Msg_N ("??\may cause unexpected accesses to atomic object",
8030 Prefix (N));
8031 end if;
8032 end Resolve_Indexed_Component;
8034 -----------------------------
8035 -- Resolve_Integer_Literal --
8036 -----------------------------
8038 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
8039 begin
8040 Set_Etype (N, Typ);
8041 Eval_Integer_Literal (N);
8042 end Resolve_Integer_Literal;
8044 --------------------------------
8045 -- Resolve_Intrinsic_Operator --
8046 --------------------------------
8048 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
8049 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8050 Op : Entity_Id;
8051 Arg1 : Node_Id;
8052 Arg2 : Node_Id;
8054 function Convert_Operand (Opnd : Node_Id) return Node_Id;
8055 -- If the operand is a literal, it cannot be the expression in a
8056 -- conversion. Use a qualified expression instead.
8058 function Convert_Operand (Opnd : Node_Id) return Node_Id is
8059 Loc : constant Source_Ptr := Sloc (Opnd);
8060 Res : Node_Id;
8061 begin
8062 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
8063 Res :=
8064 Make_Qualified_Expression (Loc,
8065 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
8066 Expression => Relocate_Node (Opnd));
8067 Analyze (Res);
8069 else
8070 Res := Unchecked_Convert_To (Btyp, Opnd);
8071 end if;
8073 return Res;
8074 end Convert_Operand;
8076 -- Start of processing for Resolve_Intrinsic_Operator
8078 begin
8079 -- We must preserve the original entity in a generic setting, so that
8080 -- the legality of the operation can be verified in an instance.
8082 if not Expander_Active then
8083 return;
8084 end if;
8086 Op := Entity (N);
8087 while Scope (Op) /= Standard_Standard loop
8088 Op := Homonym (Op);
8089 pragma Assert (Present (Op));
8090 end loop;
8092 Set_Entity (N, Op);
8093 Set_Is_Overloaded (N, False);
8095 -- If the result or operand types are private, rewrite with unchecked
8096 -- conversions on the operands and the result, to expose the proper
8097 -- underlying numeric type.
8099 if Is_Private_Type (Typ)
8100 or else Is_Private_Type (Etype (Left_Opnd (N)))
8101 or else Is_Private_Type (Etype (Right_Opnd (N)))
8102 then
8103 Arg1 := Convert_Operand (Left_Opnd (N));
8104 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
8105 -- What on earth is this commented out fragment of code???
8107 if Nkind (N) = N_Op_Expon then
8108 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
8109 else
8110 Arg2 := Convert_Operand (Right_Opnd (N));
8111 end if;
8113 if Nkind (Arg1) = N_Type_Conversion then
8114 Save_Interps (Left_Opnd (N), Expression (Arg1));
8115 end if;
8117 if Nkind (Arg2) = N_Type_Conversion then
8118 Save_Interps (Right_Opnd (N), Expression (Arg2));
8119 end if;
8121 Set_Left_Opnd (N, Arg1);
8122 Set_Right_Opnd (N, Arg2);
8124 Set_Etype (N, Btyp);
8125 Rewrite (N, Unchecked_Convert_To (Typ, N));
8126 Resolve (N, Typ);
8128 elsif Typ /= Etype (Left_Opnd (N))
8129 or else Typ /= Etype (Right_Opnd (N))
8130 then
8131 -- Add explicit conversion where needed, and save interpretations in
8132 -- case operands are overloaded.
8134 Arg1 := Convert_To (Typ, Left_Opnd (N));
8135 Arg2 := Convert_To (Typ, Right_Opnd (N));
8137 if Nkind (Arg1) = N_Type_Conversion then
8138 Save_Interps (Left_Opnd (N), Expression (Arg1));
8139 else
8140 Save_Interps (Left_Opnd (N), Arg1);
8141 end if;
8143 if Nkind (Arg2) = N_Type_Conversion then
8144 Save_Interps (Right_Opnd (N), Expression (Arg2));
8145 else
8146 Save_Interps (Right_Opnd (N), Arg2);
8147 end if;
8149 Rewrite (Left_Opnd (N), Arg1);
8150 Rewrite (Right_Opnd (N), Arg2);
8151 Analyze (Arg1);
8152 Analyze (Arg2);
8153 Resolve_Arithmetic_Op (N, Typ);
8155 else
8156 Resolve_Arithmetic_Op (N, Typ);
8157 end if;
8158 end Resolve_Intrinsic_Operator;
8160 --------------------------------------
8161 -- Resolve_Intrinsic_Unary_Operator --
8162 --------------------------------------
8164 procedure Resolve_Intrinsic_Unary_Operator
8165 (N : Node_Id;
8166 Typ : Entity_Id)
8168 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8169 Op : Entity_Id;
8170 Arg2 : Node_Id;
8172 begin
8173 Op := Entity (N);
8174 while Scope (Op) /= Standard_Standard loop
8175 Op := Homonym (Op);
8176 pragma Assert (Present (Op));
8177 end loop;
8179 Set_Entity (N, Op);
8181 if Is_Private_Type (Typ) then
8182 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
8183 Save_Interps (Right_Opnd (N), Expression (Arg2));
8185 Set_Right_Opnd (N, Arg2);
8187 Set_Etype (N, Btyp);
8188 Rewrite (N, Unchecked_Convert_To (Typ, N));
8189 Resolve (N, Typ);
8191 else
8192 Resolve_Unary_Op (N, Typ);
8193 end if;
8194 end Resolve_Intrinsic_Unary_Operator;
8196 ------------------------
8197 -- Resolve_Logical_Op --
8198 ------------------------
8200 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
8201 B_Typ : Entity_Id;
8203 begin
8204 Check_No_Direct_Boolean_Operators (N);
8206 -- Predefined operations on scalar types yield the base type. On the
8207 -- other hand, logical operations on arrays yield the type of the
8208 -- arguments (and the context).
8210 if Is_Array_Type (Typ) then
8211 B_Typ := Typ;
8212 else
8213 B_Typ := Base_Type (Typ);
8214 end if;
8216 -- The following test is required because the operands of the operation
8217 -- may be literals, in which case the resulting type appears to be
8218 -- compatible with a signed integer type, when in fact it is compatible
8219 -- only with modular types. If the context itself is universal, the
8220 -- operation is illegal.
8222 if not Valid_Boolean_Arg (Typ) then
8223 Error_Msg_N ("invalid context for logical operation", N);
8224 Set_Etype (N, Any_Type);
8225 return;
8227 elsif Typ = Any_Modular then
8228 Error_Msg_N
8229 ("no modular type available in this context", N);
8230 Set_Etype (N, Any_Type);
8231 return;
8233 elsif Is_Modular_Integer_Type (Typ)
8234 and then Etype (Left_Opnd (N)) = Universal_Integer
8235 and then Etype (Right_Opnd (N)) = Universal_Integer
8236 then
8237 Check_For_Visible_Operator (N, B_Typ);
8238 end if;
8240 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
8241 -- is active and the result type is standard Boolean (do not mess with
8242 -- ops that return a nonstandard Boolean type, because something strange
8243 -- is going on).
8245 -- Note: you might expect this replacement to be done during expansion,
8246 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
8247 -- is used, no part of the right operand of an "and" or "or" operator
8248 -- should be executed if the left operand would short-circuit the
8249 -- evaluation of the corresponding "and then" or "or else". If we left
8250 -- the replacement to expansion time, then run-time checks associated
8251 -- with such operands would be evaluated unconditionally, due to being
8252 -- before the condition prior to the rewriting as short-circuit forms
8253 -- during expansion.
8255 if Short_Circuit_And_Or
8256 and then B_Typ = Standard_Boolean
8257 and then Nkind_In (N, N_Op_And, N_Op_Or)
8258 then
8259 if Nkind (N) = N_Op_And then
8260 Rewrite (N,
8261 Make_And_Then (Sloc (N),
8262 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8263 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8264 Analyze_And_Resolve (N, B_Typ);
8266 -- Case of OR changed to OR ELSE
8268 else
8269 Rewrite (N,
8270 Make_Or_Else (Sloc (N),
8271 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8272 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8273 Analyze_And_Resolve (N, B_Typ);
8274 end if;
8276 -- Return now, since analysis of the rewritten ops will take care of
8277 -- other reference bookkeeping and expression folding.
8279 return;
8280 end if;
8282 Resolve (Left_Opnd (N), B_Typ);
8283 Resolve (Right_Opnd (N), B_Typ);
8285 Check_Unset_Reference (Left_Opnd (N));
8286 Check_Unset_Reference (Right_Opnd (N));
8288 Set_Etype (N, B_Typ);
8289 Generate_Operator_Reference (N, B_Typ);
8290 Eval_Logical_Op (N);
8292 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
8293 -- only when both operands have same static lower and higher bounds. Of
8294 -- course the types have to match, so only check if operands are
8295 -- compatible and the node itself has no errors.
8297 if Is_Array_Type (B_Typ)
8298 and then Nkind (N) in N_Binary_Op
8299 then
8300 declare
8301 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
8302 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
8304 begin
8305 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8306 -- operation if not needed.
8308 if Restriction_Check_Required (SPARK_05)
8309 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
8310 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
8311 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
8312 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
8313 then
8314 Check_SPARK_05_Restriction
8315 ("array types should have matching static bounds", N);
8316 end if;
8317 end;
8318 end if;
8320 Check_Function_Writable_Actuals (N);
8321 end Resolve_Logical_Op;
8323 ---------------------------
8324 -- Resolve_Membership_Op --
8325 ---------------------------
8327 -- The context can only be a boolean type, and does not determine the
8328 -- arguments. Arguments should be unambiguous, but the preference rule for
8329 -- universal types applies.
8331 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
8332 pragma Warnings (Off, Typ);
8334 L : constant Node_Id := Left_Opnd (N);
8335 R : constant Node_Id := Right_Opnd (N);
8336 T : Entity_Id;
8338 procedure Resolve_Set_Membership;
8339 -- Analysis has determined a unique type for the left operand. Use it to
8340 -- resolve the disjuncts.
8342 ----------------------------
8343 -- Resolve_Set_Membership --
8344 ----------------------------
8346 procedure Resolve_Set_Membership is
8347 Alt : Node_Id;
8348 Ltyp : constant Entity_Id := Etype (L);
8350 begin
8351 Resolve (L, Ltyp);
8353 Alt := First (Alternatives (N));
8354 while Present (Alt) loop
8356 -- Alternative is an expression, a range
8357 -- or a subtype mark.
8359 if not Is_Entity_Name (Alt)
8360 or else not Is_Type (Entity (Alt))
8361 then
8362 Resolve (Alt, Ltyp);
8363 end if;
8365 Next (Alt);
8366 end loop;
8368 -- Check for duplicates for discrete case
8370 if Is_Discrete_Type (Ltyp) then
8371 declare
8372 type Ent is record
8373 Alt : Node_Id;
8374 Val : Uint;
8375 end record;
8377 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
8378 Nalts : Nat;
8380 begin
8381 -- Loop checking duplicates. This is quadratic, but giant sets
8382 -- are unlikely in this context so it's a reasonable choice.
8384 Nalts := 0;
8385 Alt := First (Alternatives (N));
8386 while Present (Alt) loop
8387 if Is_OK_Static_Expression (Alt)
8388 and then (Nkind_In (Alt, N_Integer_Literal,
8389 N_Character_Literal)
8390 or else Nkind (Alt) in N_Has_Entity)
8391 then
8392 Nalts := Nalts + 1;
8393 Alts (Nalts) := (Alt, Expr_Value (Alt));
8395 for J in 1 .. Nalts - 1 loop
8396 if Alts (J).Val = Alts (Nalts).Val then
8397 Error_Msg_Sloc := Sloc (Alts (J).Alt);
8398 Error_Msg_N ("duplicate of value given#??", Alt);
8399 end if;
8400 end loop;
8401 end if;
8403 Alt := Next (Alt);
8404 end loop;
8405 end;
8406 end if;
8407 end Resolve_Set_Membership;
8409 -- Start of processing for Resolve_Membership_Op
8411 begin
8412 if L = Error or else R = Error then
8413 return;
8414 end if;
8416 if Present (Alternatives (N)) then
8417 Resolve_Set_Membership;
8418 goto SM_Exit;
8420 elsif not Is_Overloaded (R)
8421 and then
8422 (Etype (R) = Universal_Integer
8423 or else
8424 Etype (R) = Universal_Real)
8425 and then Is_Overloaded (L)
8426 then
8427 T := Etype (R);
8429 -- Ada 2005 (AI-251): Support the following case:
8431 -- type I is interface;
8432 -- type T is tagged ...
8434 -- function Test (O : I'Class) is
8435 -- begin
8436 -- return O in T'Class.
8437 -- end Test;
8439 -- In this case we have nothing else to do. The membership test will be
8440 -- done at run time.
8442 elsif Ada_Version >= Ada_2005
8443 and then Is_Class_Wide_Type (Etype (L))
8444 and then Is_Interface (Etype (L))
8445 and then Is_Class_Wide_Type (Etype (R))
8446 and then not Is_Interface (Etype (R))
8447 then
8448 return;
8449 else
8450 T := Intersect_Types (L, R);
8451 end if;
8453 -- If mixed-mode operations are present and operands are all literal,
8454 -- the only interpretation involves Duration, which is probably not
8455 -- the intention of the programmer.
8457 if T = Any_Fixed then
8458 T := Unique_Fixed_Point_Type (N);
8460 if T = Any_Type then
8461 return;
8462 end if;
8463 end if;
8465 Resolve (L, T);
8466 Check_Unset_Reference (L);
8468 if Nkind (R) = N_Range
8469 and then not Is_Scalar_Type (T)
8470 then
8471 Error_Msg_N ("scalar type required for range", R);
8472 end if;
8474 if Is_Entity_Name (R) then
8475 Freeze_Expression (R);
8476 else
8477 Resolve (R, T);
8478 Check_Unset_Reference (R);
8479 end if;
8481 -- Here after resolving membership operation
8483 <<SM_Exit>>
8485 Eval_Membership_Op (N);
8486 Check_Function_Writable_Actuals (N);
8487 end Resolve_Membership_Op;
8489 ------------------
8490 -- Resolve_Null --
8491 ------------------
8493 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
8494 Loc : constant Source_Ptr := Sloc (N);
8496 begin
8497 -- Handle restriction against anonymous null access values This
8498 -- restriction can be turned off using -gnatdj.
8500 -- Ada 2005 (AI-231): Remove restriction
8502 if Ada_Version < Ada_2005
8503 and then not Debug_Flag_J
8504 and then Ekind (Typ) = E_Anonymous_Access_Type
8505 and then Comes_From_Source (N)
8506 then
8507 -- In the common case of a call which uses an explicitly null value
8508 -- for an access parameter, give specialized error message.
8510 if Nkind (Parent (N)) in N_Subprogram_Call then
8511 Error_Msg_N
8512 ("null is not allowed as argument for an access parameter", N);
8514 -- Standard message for all other cases (are there any?)
8516 else
8517 Error_Msg_N
8518 ("null cannot be of an anonymous access type", N);
8519 end if;
8520 end if;
8522 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
8523 -- assignment to a null-excluding object
8525 if Ada_Version >= Ada_2005
8526 and then Can_Never_Be_Null (Typ)
8527 and then Nkind (Parent (N)) = N_Assignment_Statement
8528 then
8529 if not Inside_Init_Proc then
8530 Insert_Action
8531 (Compile_Time_Constraint_Error (N,
8532 "(Ada 2005) null not allowed in null-excluding objects??"),
8533 Make_Raise_Constraint_Error (Loc,
8534 Reason => CE_Access_Check_Failed));
8535 else
8536 Insert_Action (N,
8537 Make_Raise_Constraint_Error (Loc,
8538 Reason => CE_Access_Check_Failed));
8539 end if;
8540 end if;
8542 -- In a distributed context, null for a remote access to subprogram may
8543 -- need to be replaced with a special record aggregate. In this case,
8544 -- return after having done the transformation.
8546 if (Ekind (Typ) = E_Record_Type
8547 or else Is_Remote_Access_To_Subprogram_Type (Typ))
8548 and then Remote_AST_Null_Value (N, Typ)
8549 then
8550 return;
8551 end if;
8553 -- The null literal takes its type from the context
8555 Set_Etype (N, Typ);
8556 end Resolve_Null;
8558 -----------------------
8559 -- Resolve_Op_Concat --
8560 -----------------------
8562 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
8564 -- We wish to avoid deep recursion, because concatenations are often
8565 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
8566 -- operands nonrecursively until we find something that is not a simple
8567 -- concatenation (A in this case). We resolve that, and then walk back
8568 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
8569 -- to do the rest of the work at each level. The Parent pointers allow
8570 -- us to avoid recursion, and thus avoid running out of memory. See also
8571 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
8573 NN : Node_Id := N;
8574 Op1 : Node_Id;
8576 begin
8577 -- The following code is equivalent to:
8579 -- Resolve_Op_Concat_First (NN, Typ);
8580 -- Resolve_Op_Concat_Arg (N, ...);
8581 -- Resolve_Op_Concat_Rest (N, Typ);
8583 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
8584 -- operand is a concatenation.
8586 -- Walk down left operands
8588 loop
8589 Resolve_Op_Concat_First (NN, Typ);
8590 Op1 := Left_Opnd (NN);
8591 exit when not (Nkind (Op1) = N_Op_Concat
8592 and then not Is_Array_Type (Component_Type (Typ))
8593 and then Entity (Op1) = Entity (NN));
8594 NN := Op1;
8595 end loop;
8597 -- Now (given the above example) NN is A&B and Op1 is A
8599 -- First resolve Op1 ...
8601 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
8603 -- ... then walk NN back up until we reach N (where we started), calling
8604 -- Resolve_Op_Concat_Rest along the way.
8606 loop
8607 Resolve_Op_Concat_Rest (NN, Typ);
8608 exit when NN = N;
8609 NN := Parent (NN);
8610 end loop;
8612 if Base_Type (Etype (N)) /= Standard_String then
8613 Check_SPARK_05_Restriction
8614 ("result of concatenation should have type String", N);
8615 end if;
8616 end Resolve_Op_Concat;
8618 ---------------------------
8619 -- Resolve_Op_Concat_Arg --
8620 ---------------------------
8622 procedure Resolve_Op_Concat_Arg
8623 (N : Node_Id;
8624 Arg : Node_Id;
8625 Typ : Entity_Id;
8626 Is_Comp : Boolean)
8628 Btyp : constant Entity_Id := Base_Type (Typ);
8629 Ctyp : constant Entity_Id := Component_Type (Typ);
8631 begin
8632 if In_Instance then
8633 if Is_Comp
8634 or else (not Is_Overloaded (Arg)
8635 and then Etype (Arg) /= Any_Composite
8636 and then Covers (Ctyp, Etype (Arg)))
8637 then
8638 Resolve (Arg, Ctyp);
8639 else
8640 Resolve (Arg, Btyp);
8641 end if;
8643 -- If both Array & Array and Array & Component are visible, there is a
8644 -- potential ambiguity that must be reported.
8646 elsif Has_Compatible_Type (Arg, Ctyp) then
8647 if Nkind (Arg) = N_Aggregate
8648 and then Is_Composite_Type (Ctyp)
8649 then
8650 if Is_Private_Type (Ctyp) then
8651 Resolve (Arg, Btyp);
8653 -- If the operation is user-defined and not overloaded use its
8654 -- profile. The operation may be a renaming, in which case it has
8655 -- been rewritten, and we want the original profile.
8657 elsif not Is_Overloaded (N)
8658 and then Comes_From_Source (Entity (Original_Node (N)))
8659 and then Ekind (Entity (Original_Node (N))) = E_Function
8660 then
8661 Resolve (Arg,
8662 Etype
8663 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
8664 return;
8666 -- Otherwise an aggregate may match both the array type and the
8667 -- component type.
8669 else
8670 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
8671 Set_Etype (Arg, Any_Type);
8672 end if;
8674 else
8675 if Is_Overloaded (Arg)
8676 and then Has_Compatible_Type (Arg, Typ)
8677 and then Etype (Arg) /= Any_Type
8678 then
8679 declare
8680 I : Interp_Index;
8681 It : Interp;
8682 Func : Entity_Id;
8684 begin
8685 Get_First_Interp (Arg, I, It);
8686 Func := It.Nam;
8687 Get_Next_Interp (I, It);
8689 -- Special-case the error message when the overloading is
8690 -- caused by a function that yields an array and can be
8691 -- called without parameters.
8693 if It.Nam = Func then
8694 Error_Msg_Sloc := Sloc (Func);
8695 Error_Msg_N ("ambiguous call to function#", Arg);
8696 Error_Msg_NE
8697 ("\\interpretation as call yields&", Arg, Typ);
8698 Error_Msg_NE
8699 ("\\interpretation as indexing of call yields&",
8700 Arg, Component_Type (Typ));
8702 else
8703 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
8705 Get_First_Interp (Arg, I, It);
8706 while Present (It.Nam) loop
8707 Error_Msg_Sloc := Sloc (It.Nam);
8709 if Base_Type (It.Typ) = Btyp
8710 or else
8711 Base_Type (It.Typ) = Base_Type (Ctyp)
8712 then
8713 Error_Msg_N -- CODEFIX
8714 ("\\possible interpretation#", Arg);
8715 end if;
8717 Get_Next_Interp (I, It);
8718 end loop;
8719 end if;
8720 end;
8721 end if;
8723 Resolve (Arg, Component_Type (Typ));
8725 if Nkind (Arg) = N_String_Literal then
8726 Set_Etype (Arg, Component_Type (Typ));
8727 end if;
8729 if Arg = Left_Opnd (N) then
8730 Set_Is_Component_Left_Opnd (N);
8731 else
8732 Set_Is_Component_Right_Opnd (N);
8733 end if;
8734 end if;
8736 else
8737 Resolve (Arg, Btyp);
8738 end if;
8740 -- Concatenation is restricted in SPARK: each operand must be either a
8741 -- string literal, the name of a string constant, a static character or
8742 -- string expression, or another concatenation. Arg cannot be a
8743 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
8744 -- separately on each final operand, past concatenation operations.
8746 if Is_Character_Type (Etype (Arg)) then
8747 if not Is_OK_Static_Expression (Arg) then
8748 Check_SPARK_05_Restriction
8749 ("character operand for concatenation should be static", Arg);
8750 end if;
8752 elsif Is_String_Type (Etype (Arg)) then
8753 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
8754 and then Is_Constant_Object (Entity (Arg)))
8755 and then not Is_OK_Static_Expression (Arg)
8756 then
8757 Check_SPARK_05_Restriction
8758 ("string operand for concatenation should be static", Arg);
8759 end if;
8761 -- Do not issue error on an operand that is neither a character nor a
8762 -- string, as the error is issued in Resolve_Op_Concat.
8764 else
8765 null;
8766 end if;
8768 Check_Unset_Reference (Arg);
8769 end Resolve_Op_Concat_Arg;
8771 -----------------------------
8772 -- Resolve_Op_Concat_First --
8773 -----------------------------
8775 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
8776 Btyp : constant Entity_Id := Base_Type (Typ);
8777 Op1 : constant Node_Id := Left_Opnd (N);
8778 Op2 : constant Node_Id := Right_Opnd (N);
8780 begin
8781 -- The parser folds an enormous sequence of concatenations of string
8782 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
8783 -- in the right operand. If the expression resolves to a predefined "&"
8784 -- operator, all is well. Otherwise, the parser's folding is wrong, so
8785 -- we give an error. See P_Simple_Expression in Par.Ch4.
8787 if Nkind (Op2) = N_String_Literal
8788 and then Is_Folded_In_Parser (Op2)
8789 and then Ekind (Entity (N)) = E_Function
8790 then
8791 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
8792 and then String_Length (Strval (Op1)) = 0);
8793 Error_Msg_N ("too many user-defined concatenations", N);
8794 return;
8795 end if;
8797 Set_Etype (N, Btyp);
8799 if Is_Limited_Composite (Btyp) then
8800 Error_Msg_N ("concatenation not available for limited array", N);
8801 Explain_Limited_Type (Btyp, N);
8802 end if;
8803 end Resolve_Op_Concat_First;
8805 ----------------------------
8806 -- Resolve_Op_Concat_Rest --
8807 ----------------------------
8809 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
8810 Op1 : constant Node_Id := Left_Opnd (N);
8811 Op2 : constant Node_Id := Right_Opnd (N);
8813 begin
8814 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
8816 Generate_Operator_Reference (N, Typ);
8818 if Is_String_Type (Typ) then
8819 Eval_Concatenation (N);
8820 end if;
8822 -- If this is not a static concatenation, but the result is a string
8823 -- type (and not an array of strings) ensure that static string operands
8824 -- have their subtypes properly constructed.
8826 if Nkind (N) /= N_String_Literal
8827 and then Is_Character_Type (Component_Type (Typ))
8828 then
8829 Set_String_Literal_Subtype (Op1, Typ);
8830 Set_String_Literal_Subtype (Op2, Typ);
8831 end if;
8832 end Resolve_Op_Concat_Rest;
8834 ----------------------
8835 -- Resolve_Op_Expon --
8836 ----------------------
8838 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
8839 B_Typ : constant Entity_Id := Base_Type (Typ);
8841 begin
8842 -- Catch attempts to do fixed-point exponentiation with universal
8843 -- operands, which is a case where the illegality is not caught during
8844 -- normal operator analysis. This is not done in preanalysis mode
8845 -- since the tree is not fully decorated during preanalysis.
8847 if Full_Analysis then
8848 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
8849 Error_Msg_N ("exponentiation not available for fixed point", N);
8850 return;
8852 elsif Nkind (Parent (N)) in N_Op
8853 and then Is_Fixed_Point_Type (Etype (Parent (N)))
8854 and then Etype (N) = Universal_Real
8855 and then Comes_From_Source (N)
8856 then
8857 Error_Msg_N ("exponentiation not available for fixed point", N);
8858 return;
8859 end if;
8860 end if;
8862 if Comes_From_Source (N)
8863 and then Ekind (Entity (N)) = E_Function
8864 and then Is_Imported (Entity (N))
8865 and then Is_Intrinsic_Subprogram (Entity (N))
8866 then
8867 Resolve_Intrinsic_Operator (N, Typ);
8868 return;
8869 end if;
8871 if Etype (Left_Opnd (N)) = Universal_Integer
8872 or else Etype (Left_Opnd (N)) = Universal_Real
8873 then
8874 Check_For_Visible_Operator (N, B_Typ);
8875 end if;
8877 -- We do the resolution using the base type, because intermediate values
8878 -- in expressions are always of the base type, not a subtype of it.
8880 Resolve (Left_Opnd (N), B_Typ);
8881 Resolve (Right_Opnd (N), Standard_Integer);
8883 -- For integer types, right argument must be in Natural range
8885 if Is_Integer_Type (Typ) then
8886 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
8887 end if;
8889 Check_Unset_Reference (Left_Opnd (N));
8890 Check_Unset_Reference (Right_Opnd (N));
8892 Set_Etype (N, B_Typ);
8893 Generate_Operator_Reference (N, B_Typ);
8895 Analyze_Dimension (N);
8897 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
8898 -- Evaluate the exponentiation operator for dimensioned type
8900 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
8901 else
8902 Eval_Op_Expon (N);
8903 end if;
8905 -- Set overflow checking bit. Much cleverer code needed here eventually
8906 -- and perhaps the Resolve routines should be separated for the various
8907 -- arithmetic operations, since they will need different processing. ???
8909 if Nkind (N) in N_Op then
8910 if not Overflow_Checks_Suppressed (Etype (N)) then
8911 Enable_Overflow_Check (N);
8912 end if;
8913 end if;
8914 end Resolve_Op_Expon;
8916 --------------------
8917 -- Resolve_Op_Not --
8918 --------------------
8920 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8921 B_Typ : Entity_Id;
8923 function Parent_Is_Boolean return Boolean;
8924 -- This function determines if the parent node is a boolean operator or
8925 -- operation (comparison op, membership test, or short circuit form) and
8926 -- the not in question is the left operand of this operation. Note that
8927 -- if the not is in parens, then false is returned.
8929 -----------------------
8930 -- Parent_Is_Boolean --
8931 -----------------------
8933 function Parent_Is_Boolean return Boolean is
8934 begin
8935 if Paren_Count (N) /= 0 then
8936 return False;
8938 else
8939 case Nkind (Parent (N)) is
8940 when N_Op_And |
8941 N_Op_Eq |
8942 N_Op_Ge |
8943 N_Op_Gt |
8944 N_Op_Le |
8945 N_Op_Lt |
8946 N_Op_Ne |
8947 N_Op_Or |
8948 N_Op_Xor |
8949 N_In |
8950 N_Not_In |
8951 N_And_Then |
8952 N_Or_Else =>
8954 return Left_Opnd (Parent (N)) = N;
8956 when others =>
8957 return False;
8958 end case;
8959 end if;
8960 end Parent_Is_Boolean;
8962 -- Start of processing for Resolve_Op_Not
8964 begin
8965 -- Predefined operations on scalar types yield the base type. On the
8966 -- other hand, logical operations on arrays yield the type of the
8967 -- arguments (and the context).
8969 if Is_Array_Type (Typ) then
8970 B_Typ := Typ;
8971 else
8972 B_Typ := Base_Type (Typ);
8973 end if;
8975 -- Straightforward case of incorrect arguments
8977 if not Valid_Boolean_Arg (Typ) then
8978 Error_Msg_N ("invalid operand type for operator&", N);
8979 Set_Etype (N, Any_Type);
8980 return;
8982 -- Special case of probable missing parens
8984 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8985 if Parent_Is_Boolean then
8986 Error_Msg_N
8987 ("operand of not must be enclosed in parentheses",
8988 Right_Opnd (N));
8989 else
8990 Error_Msg_N
8991 ("no modular type available in this context", N);
8992 end if;
8994 Set_Etype (N, Any_Type);
8995 return;
8997 -- OK resolution of NOT
8999 else
9000 -- Warn if non-boolean types involved. This is a case like not a < b
9001 -- where a and b are modular, where we will get (not a) < b and most
9002 -- likely not (a < b) was intended.
9004 if Warn_On_Questionable_Missing_Parens
9005 and then not Is_Boolean_Type (Typ)
9006 and then Parent_Is_Boolean
9007 then
9008 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
9009 end if;
9011 -- Warn on double negation if checking redundant constructs
9013 if Warn_On_Redundant_Constructs
9014 and then Comes_From_Source (N)
9015 and then Comes_From_Source (Right_Opnd (N))
9016 and then Root_Type (Typ) = Standard_Boolean
9017 and then Nkind (Right_Opnd (N)) = N_Op_Not
9018 then
9019 Error_Msg_N ("redundant double negation?r?", N);
9020 end if;
9022 -- Complete resolution and evaluation of NOT
9024 Resolve (Right_Opnd (N), B_Typ);
9025 Check_Unset_Reference (Right_Opnd (N));
9026 Set_Etype (N, B_Typ);
9027 Generate_Operator_Reference (N, B_Typ);
9028 Eval_Op_Not (N);
9029 end if;
9030 end Resolve_Op_Not;
9032 -----------------------------
9033 -- Resolve_Operator_Symbol --
9034 -----------------------------
9036 -- Nothing to be done, all resolved already
9038 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
9039 pragma Warnings (Off, N);
9040 pragma Warnings (Off, Typ);
9042 begin
9043 null;
9044 end Resolve_Operator_Symbol;
9046 ----------------------------------
9047 -- Resolve_Qualified_Expression --
9048 ----------------------------------
9050 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
9051 pragma Warnings (Off, Typ);
9053 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
9054 Expr : constant Node_Id := Expression (N);
9056 begin
9057 Resolve (Expr, Target_Typ);
9059 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9060 -- operation if not needed.
9062 if Restriction_Check_Required (SPARK_05)
9063 and then Is_Array_Type (Target_Typ)
9064 and then Is_Array_Type (Etype (Expr))
9065 and then Etype (Expr) /= Any_Composite -- or else Expr in error
9066 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
9067 then
9068 Check_SPARK_05_Restriction
9069 ("array types should have matching static bounds", N);
9070 end if;
9072 -- A qualified expression requires an exact match of the type, class-
9073 -- wide matching is not allowed. However, if the qualifying type is
9074 -- specific and the expression has a class-wide type, it may still be
9075 -- okay, since it can be the result of the expansion of a call to a
9076 -- dispatching function, so we also have to check class-wideness of the
9077 -- type of the expression's original node.
9079 if (Is_Class_Wide_Type (Target_Typ)
9080 or else
9081 (Is_Class_Wide_Type (Etype (Expr))
9082 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
9083 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
9084 then
9085 Wrong_Type (Expr, Target_Typ);
9086 end if;
9088 -- If the target type is unconstrained, then we reset the type of the
9089 -- result from the type of the expression. For other cases, the actual
9090 -- subtype of the expression is the target type.
9092 if Is_Composite_Type (Target_Typ)
9093 and then not Is_Constrained (Target_Typ)
9094 then
9095 Set_Etype (N, Etype (Expr));
9096 end if;
9098 Analyze_Dimension (N);
9099 Eval_Qualified_Expression (N);
9101 -- If we still have a qualified expression after the static evaluation,
9102 -- then apply a scalar range check if needed. The reason that we do this
9103 -- after the Eval call is that otherwise, the application of the range
9104 -- check may convert an illegal static expression and result in warning
9105 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
9107 if Nkind (N) = N_Qualified_Expression and then Is_Scalar_Type (Typ) then
9108 Apply_Scalar_Range_Check (Expr, Typ);
9109 end if;
9110 end Resolve_Qualified_Expression;
9112 ------------------------------
9113 -- Resolve_Raise_Expression --
9114 ------------------------------
9116 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
9117 begin
9118 if Typ = Raise_Type then
9119 Error_Msg_N ("cannot find unique type for raise expression", N);
9120 Set_Etype (N, Any_Type);
9121 else
9122 Set_Etype (N, Typ);
9123 end if;
9124 end Resolve_Raise_Expression;
9126 -------------------
9127 -- Resolve_Range --
9128 -------------------
9130 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
9131 L : constant Node_Id := Low_Bound (N);
9132 H : constant Node_Id := High_Bound (N);
9134 function First_Last_Ref return Boolean;
9135 -- Returns True if N is of the form X'First .. X'Last where X is the
9136 -- same entity for both attributes.
9138 --------------------
9139 -- First_Last_Ref --
9140 --------------------
9142 function First_Last_Ref return Boolean is
9143 Lorig : constant Node_Id := Original_Node (L);
9144 Horig : constant Node_Id := Original_Node (H);
9146 begin
9147 if Nkind (Lorig) = N_Attribute_Reference
9148 and then Nkind (Horig) = N_Attribute_Reference
9149 and then Attribute_Name (Lorig) = Name_First
9150 and then Attribute_Name (Horig) = Name_Last
9151 then
9152 declare
9153 PL : constant Node_Id := Prefix (Lorig);
9154 PH : constant Node_Id := Prefix (Horig);
9155 begin
9156 if Is_Entity_Name (PL)
9157 and then Is_Entity_Name (PH)
9158 and then Entity (PL) = Entity (PH)
9159 then
9160 return True;
9161 end if;
9162 end;
9163 end if;
9165 return False;
9166 end First_Last_Ref;
9168 -- Start of processing for Resolve_Range
9170 begin
9171 Set_Etype (N, Typ);
9172 Resolve (L, Typ);
9173 Resolve (H, Typ);
9175 -- Check for inappropriate range on unordered enumeration type
9177 if Bad_Unordered_Enumeration_Reference (N, Typ)
9179 -- Exclude X'First .. X'Last if X is the same entity for both
9181 and then not First_Last_Ref
9182 then
9183 Error_Msg_Sloc := Sloc (Typ);
9184 Error_Msg_NE
9185 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
9186 end if;
9188 Check_Unset_Reference (L);
9189 Check_Unset_Reference (H);
9191 -- We have to check the bounds for being within the base range as
9192 -- required for a non-static context. Normally this is automatic and
9193 -- done as part of evaluating expressions, but the N_Range node is an
9194 -- exception, since in GNAT we consider this node to be a subexpression,
9195 -- even though in Ada it is not. The circuit in Sem_Eval could check for
9196 -- this, but that would put the test on the main evaluation path for
9197 -- expressions.
9199 Check_Non_Static_Context (L);
9200 Check_Non_Static_Context (H);
9202 -- Check for an ambiguous range over character literals. This will
9203 -- happen with a membership test involving only literals.
9205 if Typ = Any_Character then
9206 Ambiguous_Character (L);
9207 Set_Etype (N, Any_Type);
9208 return;
9209 end if;
9211 -- If bounds are static, constant-fold them, so size computations are
9212 -- identical between front-end and back-end. Do not perform this
9213 -- transformation while analyzing generic units, as type information
9214 -- would be lost when reanalyzing the constant node in the instance.
9216 if Is_Discrete_Type (Typ) and then Expander_Active then
9217 if Is_OK_Static_Expression (L) then
9218 Fold_Uint (L, Expr_Value (L), Is_OK_Static_Expression (L));
9219 end if;
9221 if Is_OK_Static_Expression (H) then
9222 Fold_Uint (H, Expr_Value (H), Is_OK_Static_Expression (H));
9223 end if;
9224 end if;
9225 end Resolve_Range;
9227 --------------------------
9228 -- Resolve_Real_Literal --
9229 --------------------------
9231 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
9232 Actual_Typ : constant Entity_Id := Etype (N);
9234 begin
9235 -- Special processing for fixed-point literals to make sure that the
9236 -- value is an exact multiple of small where this is required. We skip
9237 -- this for the universal real case, and also for generic types.
9239 if Is_Fixed_Point_Type (Typ)
9240 and then Typ /= Universal_Fixed
9241 and then Typ /= Any_Fixed
9242 and then not Is_Generic_Type (Typ)
9243 then
9244 declare
9245 Val : constant Ureal := Realval (N);
9246 Cintr : constant Ureal := Val / Small_Value (Typ);
9247 Cint : constant Uint := UR_Trunc (Cintr);
9248 Den : constant Uint := Norm_Den (Cintr);
9249 Stat : Boolean;
9251 begin
9252 -- Case of literal is not an exact multiple of the Small
9254 if Den /= 1 then
9256 -- For a source program literal for a decimal fixed-point type,
9257 -- this is statically illegal (RM 4.9(36)).
9259 if Is_Decimal_Fixed_Point_Type (Typ)
9260 and then Actual_Typ = Universal_Real
9261 and then Comes_From_Source (N)
9262 then
9263 Error_Msg_N ("value has extraneous low order digits", N);
9264 end if;
9266 -- Generate a warning if literal from source
9268 if Is_OK_Static_Expression (N)
9269 and then Warn_On_Bad_Fixed_Value
9270 then
9271 Error_Msg_N
9272 ("?b?static fixed-point value is not a multiple of Small!",
9274 end if;
9276 -- Replace literal by a value that is the exact representation
9277 -- of a value of the type, i.e. a multiple of the small value,
9278 -- by truncation, since Machine_Rounds is false for all GNAT
9279 -- fixed-point types (RM 4.9(38)).
9281 Stat := Is_OK_Static_Expression (N);
9282 Rewrite (N,
9283 Make_Real_Literal (Sloc (N),
9284 Realval => Small_Value (Typ) * Cint));
9286 Set_Is_Static_Expression (N, Stat);
9287 end if;
9289 -- In all cases, set the corresponding integer field
9291 Set_Corresponding_Integer_Value (N, Cint);
9292 end;
9293 end if;
9295 -- Now replace the actual type by the expected type as usual
9297 Set_Etype (N, Typ);
9298 Eval_Real_Literal (N);
9299 end Resolve_Real_Literal;
9301 -----------------------
9302 -- Resolve_Reference --
9303 -----------------------
9305 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
9306 P : constant Node_Id := Prefix (N);
9308 begin
9309 -- Replace general access with specific type
9311 if Ekind (Etype (N)) = E_Allocator_Type then
9312 Set_Etype (N, Base_Type (Typ));
9313 end if;
9315 Resolve (P, Designated_Type (Etype (N)));
9317 -- If we are taking the reference of a volatile entity, then treat it as
9318 -- a potential modification of this entity. This is too conservative,
9319 -- but necessary because remove side effects can cause transformations
9320 -- of normal assignments into reference sequences that otherwise fail to
9321 -- notice the modification.
9323 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
9324 Note_Possible_Modification (P, Sure => False);
9325 end if;
9326 end Resolve_Reference;
9328 --------------------------------
9329 -- Resolve_Selected_Component --
9330 --------------------------------
9332 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
9333 Comp : Entity_Id;
9334 Comp1 : Entity_Id := Empty; -- prevent junk warning
9335 P : constant Node_Id := Prefix (N);
9336 S : constant Node_Id := Selector_Name (N);
9337 T : Entity_Id := Etype (P);
9338 I : Interp_Index;
9339 I1 : Interp_Index := 0; -- prevent junk warning
9340 It : Interp;
9341 It1 : Interp;
9342 Found : Boolean;
9344 function Init_Component return Boolean;
9345 -- Check whether this is the initialization of a component within an
9346 -- init proc (by assignment or call to another init proc). If true,
9347 -- there is no need for a discriminant check.
9349 --------------------
9350 -- Init_Component --
9351 --------------------
9353 function Init_Component return Boolean is
9354 begin
9355 return Inside_Init_Proc
9356 and then Nkind (Prefix (N)) = N_Identifier
9357 and then Chars (Prefix (N)) = Name_uInit
9358 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
9359 end Init_Component;
9361 -- Start of processing for Resolve_Selected_Component
9363 begin
9364 if Is_Overloaded (P) then
9366 -- Use the context type to select the prefix that has a selector
9367 -- of the correct name and type.
9369 Found := False;
9370 Get_First_Interp (P, I, It);
9372 Search : while Present (It.Typ) loop
9373 if Is_Access_Type (It.Typ) then
9374 T := Designated_Type (It.Typ);
9375 else
9376 T := It.Typ;
9377 end if;
9379 -- Locate selected component. For a private prefix the selector
9380 -- can denote a discriminant.
9382 if Is_Record_Type (T) or else Is_Private_Type (T) then
9384 -- The visible components of a class-wide type are those of
9385 -- the root type.
9387 if Is_Class_Wide_Type (T) then
9388 T := Etype (T);
9389 end if;
9391 Comp := First_Entity (T);
9392 while Present (Comp) loop
9393 if Chars (Comp) = Chars (S)
9394 and then Covers (Typ, Etype (Comp))
9395 then
9396 if not Found then
9397 Found := True;
9398 I1 := I;
9399 It1 := It;
9400 Comp1 := Comp;
9402 else
9403 It := Disambiguate (P, I1, I, Any_Type);
9405 if It = No_Interp then
9406 Error_Msg_N
9407 ("ambiguous prefix for selected component", N);
9408 Set_Etype (N, Typ);
9409 return;
9411 else
9412 It1 := It;
9414 -- There may be an implicit dereference. Retrieve
9415 -- designated record type.
9417 if Is_Access_Type (It1.Typ) then
9418 T := Designated_Type (It1.Typ);
9419 else
9420 T := It1.Typ;
9421 end if;
9423 if Scope (Comp1) /= T then
9425 -- Resolution chooses the new interpretation.
9426 -- Find the component with the right name.
9428 Comp1 := First_Entity (T);
9429 while Present (Comp1)
9430 and then Chars (Comp1) /= Chars (S)
9431 loop
9432 Comp1 := Next_Entity (Comp1);
9433 end loop;
9434 end if;
9436 exit Search;
9437 end if;
9438 end if;
9439 end if;
9441 Comp := Next_Entity (Comp);
9442 end loop;
9443 end if;
9445 Get_Next_Interp (I, It);
9446 end loop Search;
9448 -- There must be a legal interpretation at this point
9450 pragma Assert (Found);
9451 Resolve (P, It1.Typ);
9452 Set_Etype (N, Typ);
9453 Set_Entity_With_Checks (S, Comp1);
9455 else
9456 -- Resolve prefix with its type
9458 Resolve (P, T);
9459 end if;
9461 -- Generate cross-reference. We needed to wait until full overloading
9462 -- resolution was complete to do this, since otherwise we can't tell if
9463 -- we are an lvalue or not.
9465 if May_Be_Lvalue (N) then
9466 Generate_Reference (Entity (S), S, 'm');
9467 else
9468 Generate_Reference (Entity (S), S, 'r');
9469 end if;
9471 -- If prefix is an access type, the node will be transformed into an
9472 -- explicit dereference during expansion. The type of the node is the
9473 -- designated type of that of the prefix.
9475 if Is_Access_Type (Etype (P)) then
9476 T := Designated_Type (Etype (P));
9477 Check_Fully_Declared_Prefix (T, P);
9478 else
9479 T := Etype (P);
9480 end if;
9482 -- Set flag for expander if discriminant check required
9484 if Has_Discriminants (T)
9485 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
9486 and then Present (Original_Record_Component (Entity (S)))
9487 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
9488 and then not Discriminant_Checks_Suppressed (T)
9489 and then not Init_Component
9490 then
9491 Set_Do_Discriminant_Check (N);
9492 end if;
9494 if Ekind (Entity (S)) = E_Void then
9495 Error_Msg_N ("premature use of component", S);
9496 end if;
9498 -- If the prefix is a record conversion, this may be a renamed
9499 -- discriminant whose bounds differ from those of the original
9500 -- one, so we must ensure that a range check is performed.
9502 if Nkind (P) = N_Type_Conversion
9503 and then Ekind (Entity (S)) = E_Discriminant
9504 and then Is_Discrete_Type (Typ)
9505 then
9506 Set_Etype (N, Base_Type (Typ));
9507 end if;
9509 -- Note: No Eval processing is required, because the prefix is of a
9510 -- record type, or protected type, and neither can possibly be static.
9512 -- If the record type is atomic, and the component is non-atomic, then
9513 -- this is worth a warning, since we have a situation where the access
9514 -- to the component may cause extra read/writes of the atomic array
9515 -- object, or partial word accesses, both of which may be unexpected.
9517 if Nkind (N) = N_Selected_Component
9518 and then Is_Atomic_Ref_With_Address (N)
9519 and then not Is_Atomic (Entity (S))
9520 and then not Is_Atomic (Etype (Entity (S)))
9521 then
9522 Error_Msg_N
9523 ("??access to non-atomic component of atomic record",
9524 Prefix (N));
9525 Error_Msg_N
9526 ("\??may cause unexpected accesses to atomic object",
9527 Prefix (N));
9528 end if;
9530 Analyze_Dimension (N);
9531 end Resolve_Selected_Component;
9533 -------------------
9534 -- Resolve_Shift --
9535 -------------------
9537 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
9538 B_Typ : constant Entity_Id := Base_Type (Typ);
9539 L : constant Node_Id := Left_Opnd (N);
9540 R : constant Node_Id := Right_Opnd (N);
9542 begin
9543 -- We do the resolution using the base type, because intermediate values
9544 -- in expressions always are of the base type, not a subtype of it.
9546 Resolve (L, B_Typ);
9547 Resolve (R, Standard_Natural);
9549 Check_Unset_Reference (L);
9550 Check_Unset_Reference (R);
9552 Set_Etype (N, B_Typ);
9553 Generate_Operator_Reference (N, B_Typ);
9554 Eval_Shift (N);
9555 end Resolve_Shift;
9557 ---------------------------
9558 -- Resolve_Short_Circuit --
9559 ---------------------------
9561 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
9562 B_Typ : constant Entity_Id := Base_Type (Typ);
9563 L : constant Node_Id := Left_Opnd (N);
9564 R : constant Node_Id := Right_Opnd (N);
9566 begin
9567 -- Ensure all actions associated with the left operand (e.g.
9568 -- finalization of transient controlled objects) are fully evaluated
9569 -- locally within an expression with actions. This is particularly
9570 -- helpful for coverage analysis. However this should not happen in
9571 -- generics.
9573 if Expander_Active then
9574 declare
9575 Reloc_L : constant Node_Id := Relocate_Node (L);
9576 begin
9577 Save_Interps (Old_N => L, New_N => Reloc_L);
9579 Rewrite (L,
9580 Make_Expression_With_Actions (Sloc (L),
9581 Actions => New_List,
9582 Expression => Reloc_L));
9584 -- Set Comes_From_Source on L to preserve warnings for unset
9585 -- reference.
9587 Set_Comes_From_Source (L, Comes_From_Source (Reloc_L));
9588 end;
9589 end if;
9591 Resolve (L, B_Typ);
9592 Resolve (R, B_Typ);
9594 -- Check for issuing warning for always False assert/check, this happens
9595 -- when assertions are turned off, in which case the pragma Assert/Check
9596 -- was transformed into:
9598 -- if False and then <condition> then ...
9600 -- and we detect this pattern
9602 if Warn_On_Assertion_Failure
9603 and then Is_Entity_Name (R)
9604 and then Entity (R) = Standard_False
9605 and then Nkind (Parent (N)) = N_If_Statement
9606 and then Nkind (N) = N_And_Then
9607 and then Is_Entity_Name (L)
9608 and then Entity (L) = Standard_False
9609 then
9610 declare
9611 Orig : constant Node_Id := Original_Node (Parent (N));
9613 begin
9614 -- Special handling of Asssert pragma
9616 if Nkind (Orig) = N_Pragma
9617 and then Pragma_Name (Orig) = Name_Assert
9618 then
9619 declare
9620 Expr : constant Node_Id :=
9621 Original_Node
9622 (Expression
9623 (First (Pragma_Argument_Associations (Orig))));
9625 begin
9626 -- Don't warn if original condition is explicit False,
9627 -- since obviously the failure is expected in this case.
9629 if Is_Entity_Name (Expr)
9630 and then Entity (Expr) = Standard_False
9631 then
9632 null;
9634 -- Issue warning. We do not want the deletion of the
9635 -- IF/AND-THEN to take this message with it. We achieve this
9636 -- by making sure that the expanded code points to the Sloc
9637 -- of the expression, not the original pragma.
9639 else
9640 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
9641 -- The source location of the expression is not usually
9642 -- the best choice here. For example, it gets located on
9643 -- the last AND keyword in a chain of boolean expressiond
9644 -- AND'ed together. It is best to put the message on the
9645 -- first character of the assertion, which is the effect
9646 -- of the First_Node call here.
9648 Error_Msg_F
9649 ("?A?assertion would fail at run time!",
9650 Expression
9651 (First (Pragma_Argument_Associations (Orig))));
9652 end if;
9653 end;
9655 -- Similar processing for Check pragma
9657 elsif Nkind (Orig) = N_Pragma
9658 and then Pragma_Name (Orig) = Name_Check
9659 then
9660 -- Don't want to warn if original condition is explicit False
9662 declare
9663 Expr : constant Node_Id :=
9664 Original_Node
9665 (Expression
9666 (Next (First (Pragma_Argument_Associations (Orig)))));
9667 begin
9668 if Is_Entity_Name (Expr)
9669 and then Entity (Expr) = Standard_False
9670 then
9671 null;
9673 -- Post warning
9675 else
9676 -- Again use Error_Msg_F rather than Error_Msg_N, see
9677 -- comment above for an explanation of why we do this.
9679 Error_Msg_F
9680 ("?A?check would fail at run time!",
9681 Expression
9682 (Last (Pragma_Argument_Associations (Orig))));
9683 end if;
9684 end;
9685 end if;
9686 end;
9687 end if;
9689 -- Continue with processing of short circuit
9691 Check_Unset_Reference (L);
9692 Check_Unset_Reference (R);
9694 Set_Etype (N, B_Typ);
9695 Eval_Short_Circuit (N);
9696 end Resolve_Short_Circuit;
9698 -------------------
9699 -- Resolve_Slice --
9700 -------------------
9702 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
9703 Drange : constant Node_Id := Discrete_Range (N);
9704 Name : constant Node_Id := Prefix (N);
9705 Array_Type : Entity_Id := Empty;
9706 Dexpr : Node_Id := Empty;
9707 Index_Type : Entity_Id;
9709 begin
9710 if Is_Overloaded (Name) then
9712 -- Use the context type to select the prefix that yields the correct
9713 -- array type.
9715 declare
9716 I : Interp_Index;
9717 I1 : Interp_Index := 0;
9718 It : Interp;
9719 P : constant Node_Id := Prefix (N);
9720 Found : Boolean := False;
9722 begin
9723 Get_First_Interp (P, I, It);
9724 while Present (It.Typ) loop
9725 if (Is_Array_Type (It.Typ)
9726 and then Covers (Typ, It.Typ))
9727 or else (Is_Access_Type (It.Typ)
9728 and then Is_Array_Type (Designated_Type (It.Typ))
9729 and then Covers (Typ, Designated_Type (It.Typ)))
9730 then
9731 if Found then
9732 It := Disambiguate (P, I1, I, Any_Type);
9734 if It = No_Interp then
9735 Error_Msg_N ("ambiguous prefix for slicing", N);
9736 Set_Etype (N, Typ);
9737 return;
9738 else
9739 Found := True;
9740 Array_Type := It.Typ;
9741 I1 := I;
9742 end if;
9743 else
9744 Found := True;
9745 Array_Type := It.Typ;
9746 I1 := I;
9747 end if;
9748 end if;
9750 Get_Next_Interp (I, It);
9751 end loop;
9752 end;
9754 else
9755 Array_Type := Etype (Name);
9756 end if;
9758 Resolve (Name, Array_Type);
9760 if Is_Access_Type (Array_Type) then
9761 Apply_Access_Check (N);
9762 Array_Type := Designated_Type (Array_Type);
9764 -- If the prefix is an access to an unconstrained array, we must use
9765 -- the actual subtype of the object to perform the index checks. The
9766 -- object denoted by the prefix is implicit in the node, so we build
9767 -- an explicit representation for it in order to compute the actual
9768 -- subtype.
9770 if not Is_Constrained (Array_Type) then
9771 Remove_Side_Effects (Prefix (N));
9773 declare
9774 Obj : constant Node_Id :=
9775 Make_Explicit_Dereference (Sloc (N),
9776 Prefix => New_Copy_Tree (Prefix (N)));
9777 begin
9778 Set_Etype (Obj, Array_Type);
9779 Set_Parent (Obj, Parent (N));
9780 Array_Type := Get_Actual_Subtype (Obj);
9781 end;
9782 end if;
9784 elsif Is_Entity_Name (Name)
9785 or else Nkind (Name) = N_Explicit_Dereference
9786 or else (Nkind (Name) = N_Function_Call
9787 and then not Is_Constrained (Etype (Name)))
9788 then
9789 Array_Type := Get_Actual_Subtype (Name);
9791 -- If the name is a selected component that depends on discriminants,
9792 -- build an actual subtype for it. This can happen only when the name
9793 -- itself is overloaded; otherwise the actual subtype is created when
9794 -- the selected component is analyzed.
9796 elsif Nkind (Name) = N_Selected_Component
9797 and then Full_Analysis
9798 and then Depends_On_Discriminant (First_Index (Array_Type))
9799 then
9800 declare
9801 Act_Decl : constant Node_Id :=
9802 Build_Actual_Subtype_Of_Component (Array_Type, Name);
9803 begin
9804 Insert_Action (N, Act_Decl);
9805 Array_Type := Defining_Identifier (Act_Decl);
9806 end;
9808 -- Maybe this should just be "else", instead of checking for the
9809 -- specific case of slice??? This is needed for the case where the
9810 -- prefix is an Image attribute, which gets expanded to a slice, and so
9811 -- has a constrained subtype which we want to use for the slice range
9812 -- check applied below (the range check won't get done if the
9813 -- unconstrained subtype of the 'Image is used).
9815 elsif Nkind (Name) = N_Slice then
9816 Array_Type := Etype (Name);
9817 end if;
9819 -- Obtain the type of the array index
9821 if Ekind (Array_Type) = E_String_Literal_Subtype then
9822 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
9823 else
9824 Index_Type := Etype (First_Index (Array_Type));
9825 end if;
9827 -- If name was overloaded, set slice type correctly now
9829 Set_Etype (N, Array_Type);
9831 -- Handle the generation of a range check that compares the array index
9832 -- against the discrete_range. The check is not applied to internally
9833 -- built nodes associated with the expansion of dispatch tables. Check
9834 -- that Ada.Tags has already been loaded to avoid extra dependencies on
9835 -- the unit.
9837 if Tagged_Type_Expansion
9838 and then RTU_Loaded (Ada_Tags)
9839 and then Nkind (Prefix (N)) = N_Selected_Component
9840 and then Present (Entity (Selector_Name (Prefix (N))))
9841 and then Entity (Selector_Name (Prefix (N))) =
9842 RTE_Record_Component (RE_Prims_Ptr)
9843 then
9844 null;
9846 -- The discrete_range is specified by a subtype indication. Create a
9847 -- shallow copy and inherit the type, parent and source location from
9848 -- the discrete_range. This ensures that the range check is inserted
9849 -- relative to the slice and that the runtime exception points to the
9850 -- proper construct.
9852 elsif Is_Entity_Name (Drange) then
9853 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
9855 Set_Etype (Dexpr, Etype (Drange));
9856 Set_Parent (Dexpr, Parent (Drange));
9857 Set_Sloc (Dexpr, Sloc (Drange));
9859 -- The discrete_range is a regular range. Resolve the bounds and remove
9860 -- their side effects.
9862 else
9863 Resolve (Drange, Base_Type (Index_Type));
9865 if Nkind (Drange) = N_Range then
9866 Force_Evaluation (Low_Bound (Drange));
9867 Force_Evaluation (High_Bound (Drange));
9869 Dexpr := Drange;
9870 end if;
9871 end if;
9873 if Present (Dexpr) then
9874 Apply_Range_Check (Dexpr, Index_Type);
9875 end if;
9877 Set_Slice_Subtype (N);
9879 -- Check bad use of type with predicates
9881 declare
9882 Subt : Entity_Id;
9884 begin
9885 if Nkind (Drange) = N_Subtype_Indication
9886 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
9887 then
9888 Subt := Entity (Subtype_Mark (Drange));
9889 else
9890 Subt := Etype (Drange);
9891 end if;
9893 if Has_Predicates (Subt) then
9894 Bad_Predicated_Subtype_Use
9895 ("subtype& has predicate, not allowed in slice", Drange, Subt);
9896 end if;
9897 end;
9899 -- Otherwise here is where we check suspicious indexes
9901 if Nkind (Drange) = N_Range then
9902 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
9903 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
9904 end if;
9906 Analyze_Dimension (N);
9907 Eval_Slice (N);
9908 end Resolve_Slice;
9910 ----------------------------
9911 -- Resolve_String_Literal --
9912 ----------------------------
9914 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
9915 C_Typ : constant Entity_Id := Component_Type (Typ);
9916 R_Typ : constant Entity_Id := Root_Type (C_Typ);
9917 Loc : constant Source_Ptr := Sloc (N);
9918 Str : constant String_Id := Strval (N);
9919 Strlen : constant Nat := String_Length (Str);
9920 Subtype_Id : Entity_Id;
9921 Need_Check : Boolean;
9923 begin
9924 -- For a string appearing in a concatenation, defer creation of the
9925 -- string_literal_subtype until the end of the resolution of the
9926 -- concatenation, because the literal may be constant-folded away. This
9927 -- is a useful optimization for long concatenation expressions.
9929 -- If the string is an aggregate built for a single character (which
9930 -- happens in a non-static context) or a is null string to which special
9931 -- checks may apply, we build the subtype. Wide strings must also get a
9932 -- string subtype if they come from a one character aggregate. Strings
9933 -- generated by attributes might be static, but it is often hard to
9934 -- determine whether the enclosing context is static, so we generate
9935 -- subtypes for them as well, thus losing some rarer optimizations ???
9936 -- Same for strings that come from a static conversion.
9938 Need_Check :=
9939 (Strlen = 0 and then Typ /= Standard_String)
9940 or else Nkind (Parent (N)) /= N_Op_Concat
9941 or else (N /= Left_Opnd (Parent (N))
9942 and then N /= Right_Opnd (Parent (N)))
9943 or else ((Typ = Standard_Wide_String
9944 or else Typ = Standard_Wide_Wide_String)
9945 and then Nkind (Original_Node (N)) /= N_String_Literal);
9947 -- If the resolving type is itself a string literal subtype, we can just
9948 -- reuse it, since there is no point in creating another.
9950 if Ekind (Typ) = E_String_Literal_Subtype then
9951 Subtype_Id := Typ;
9953 elsif Nkind (Parent (N)) = N_Op_Concat
9954 and then not Need_Check
9955 and then not Nkind_In (Original_Node (N), N_Character_Literal,
9956 N_Attribute_Reference,
9957 N_Qualified_Expression,
9958 N_Type_Conversion)
9959 then
9960 Subtype_Id := Typ;
9962 -- Do not generate a string literal subtype for the default expression
9963 -- of a formal parameter in GNATprove mode. This is because the string
9964 -- subtype is associated with the freezing actions of the subprogram,
9965 -- however freezing is disabled in GNATprove mode and as a result the
9966 -- subtype is unavailable.
9968 elsif GNATprove_Mode
9969 and then Nkind (Parent (N)) = N_Parameter_Specification
9970 then
9971 Subtype_Id := Typ;
9973 -- Otherwise we must create a string literal subtype. Note that the
9974 -- whole idea of string literal subtypes is simply to avoid the need
9975 -- for building a full fledged array subtype for each literal.
9977 else
9978 Set_String_Literal_Subtype (N, Typ);
9979 Subtype_Id := Etype (N);
9980 end if;
9982 if Nkind (Parent (N)) /= N_Op_Concat
9983 or else Need_Check
9984 then
9985 Set_Etype (N, Subtype_Id);
9986 Eval_String_Literal (N);
9987 end if;
9989 if Is_Limited_Composite (Typ)
9990 or else Is_Private_Composite (Typ)
9991 then
9992 Error_Msg_N ("string literal not available for private array", N);
9993 Set_Etype (N, Any_Type);
9994 return;
9995 end if;
9997 -- The validity of a null string has been checked in the call to
9998 -- Eval_String_Literal.
10000 if Strlen = 0 then
10001 return;
10003 -- Always accept string literal with component type Any_Character, which
10004 -- occurs in error situations and in comparisons of literals, both of
10005 -- which should accept all literals.
10007 elsif R_Typ = Any_Character then
10008 return;
10010 -- If the type is bit-packed, then we always transform the string
10011 -- literal into a full fledged aggregate.
10013 elsif Is_Bit_Packed_Array (Typ) then
10014 null;
10016 -- Deal with cases of Wide_Wide_String, Wide_String, and String
10018 else
10019 -- For Standard.Wide_Wide_String, or any other type whose component
10020 -- type is Standard.Wide_Wide_Character, we know that all the
10021 -- characters in the string must be acceptable, since the parser
10022 -- accepted the characters as valid character literals.
10024 if R_Typ = Standard_Wide_Wide_Character then
10025 null;
10027 -- For the case of Standard.String, or any other type whose component
10028 -- type is Standard.Character, we must make sure that there are no
10029 -- wide characters in the string, i.e. that it is entirely composed
10030 -- of characters in range of type Character.
10032 -- If the string literal is the result of a static concatenation, the
10033 -- test has already been performed on the components, and need not be
10034 -- repeated.
10036 elsif R_Typ = Standard_Character
10037 and then Nkind (Original_Node (N)) /= N_Op_Concat
10038 then
10039 for J in 1 .. Strlen loop
10040 if not In_Character_Range (Get_String_Char (Str, J)) then
10042 -- If we are out of range, post error. This is one of the
10043 -- very few places that we place the flag in the middle of
10044 -- a token, right under the offending wide character. Not
10045 -- quite clear if this is right wrt wide character encoding
10046 -- sequences, but it's only an error message.
10048 Error_Msg
10049 ("literal out of range of type Standard.Character",
10050 Source_Ptr (Int (Loc) + J));
10051 return;
10052 end if;
10053 end loop;
10055 -- For the case of Standard.Wide_String, or any other type whose
10056 -- component type is Standard.Wide_Character, we must make sure that
10057 -- there are no wide characters in the string, i.e. that it is
10058 -- entirely composed of characters in range of type Wide_Character.
10060 -- If the string literal is the result of a static concatenation,
10061 -- the test has already been performed on the components, and need
10062 -- not be repeated.
10064 elsif R_Typ = Standard_Wide_Character
10065 and then Nkind (Original_Node (N)) /= N_Op_Concat
10066 then
10067 for J in 1 .. Strlen loop
10068 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
10070 -- If we are out of range, post error. This is one of the
10071 -- very few places that we place the flag in the middle of
10072 -- a token, right under the offending wide character.
10074 -- This is not quite right, because characters in general
10075 -- will take more than one character position ???
10077 Error_Msg
10078 ("literal out of range of type Standard.Wide_Character",
10079 Source_Ptr (Int (Loc) + J));
10080 return;
10081 end if;
10082 end loop;
10084 -- If the root type is not a standard character, then we will convert
10085 -- the string into an aggregate and will let the aggregate code do
10086 -- the checking. Standard Wide_Wide_Character is also OK here.
10088 else
10089 null;
10090 end if;
10092 -- See if the component type of the array corresponding to the string
10093 -- has compile time known bounds. If yes we can directly check
10094 -- whether the evaluation of the string will raise constraint error.
10095 -- Otherwise we need to transform the string literal into the
10096 -- corresponding character aggregate and let the aggregate code do
10097 -- the checking.
10099 if Is_Standard_Character_Type (R_Typ) then
10101 -- Check for the case of full range, where we are definitely OK
10103 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
10104 return;
10105 end if;
10107 -- Here the range is not the complete base type range, so check
10109 declare
10110 Comp_Typ_Lo : constant Node_Id :=
10111 Type_Low_Bound (Component_Type (Typ));
10112 Comp_Typ_Hi : constant Node_Id :=
10113 Type_High_Bound (Component_Type (Typ));
10115 Char_Val : Uint;
10117 begin
10118 if Compile_Time_Known_Value (Comp_Typ_Lo)
10119 and then Compile_Time_Known_Value (Comp_Typ_Hi)
10120 then
10121 for J in 1 .. Strlen loop
10122 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
10124 if Char_Val < Expr_Value (Comp_Typ_Lo)
10125 or else Char_Val > Expr_Value (Comp_Typ_Hi)
10126 then
10127 Apply_Compile_Time_Constraint_Error
10128 (N, "character out of range??",
10129 CE_Range_Check_Failed,
10130 Loc => Source_Ptr (Int (Loc) + J));
10131 end if;
10132 end loop;
10134 return;
10135 end if;
10136 end;
10137 end if;
10138 end if;
10140 -- If we got here we meed to transform the string literal into the
10141 -- equivalent qualified positional array aggregate. This is rather
10142 -- heavy artillery for this situation, but it is hard work to avoid.
10144 declare
10145 Lits : constant List_Id := New_List;
10146 P : Source_Ptr := Loc + 1;
10147 C : Char_Code;
10149 begin
10150 -- Build the character literals, we give them source locations that
10151 -- correspond to the string positions, which is a bit tricky given
10152 -- the possible presence of wide character escape sequences.
10154 for J in 1 .. Strlen loop
10155 C := Get_String_Char (Str, J);
10156 Set_Character_Literal_Name (C);
10158 Append_To (Lits,
10159 Make_Character_Literal (P,
10160 Chars => Name_Find,
10161 Char_Literal_Value => UI_From_CC (C)));
10163 if In_Character_Range (C) then
10164 P := P + 1;
10166 -- Should we have a call to Skip_Wide here ???
10168 -- ??? else
10169 -- Skip_Wide (P);
10171 end if;
10172 end loop;
10174 Rewrite (N,
10175 Make_Qualified_Expression (Loc,
10176 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
10177 Expression =>
10178 Make_Aggregate (Loc, Expressions => Lits)));
10180 Analyze_And_Resolve (N, Typ);
10181 end;
10182 end Resolve_String_Literal;
10184 -----------------------------
10185 -- Resolve_Type_Conversion --
10186 -----------------------------
10188 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
10189 Conv_OK : constant Boolean := Conversion_OK (N);
10190 Operand : constant Node_Id := Expression (N);
10191 Operand_Typ : constant Entity_Id := Etype (Operand);
10192 Target_Typ : constant Entity_Id := Etype (N);
10193 Rop : Node_Id;
10194 Orig_N : Node_Id;
10195 Orig_T : Node_Id;
10197 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
10198 -- Set to False to suppress cases where we want to suppress the test
10199 -- for redundancy to avoid possible false positives on this warning.
10201 begin
10202 if not Conv_OK
10203 and then not Valid_Conversion (N, Target_Typ, Operand)
10204 then
10205 return;
10206 end if;
10208 -- If the Operand Etype is Universal_Fixed, then the conversion is
10209 -- never redundant. We need this check because by the time we have
10210 -- finished the rather complex transformation, the conversion looks
10211 -- redundant when it is not.
10213 if Operand_Typ = Universal_Fixed then
10214 Test_Redundant := False;
10216 -- If the operand is marked as Any_Fixed, then special processing is
10217 -- required. This is also a case where we suppress the test for a
10218 -- redundant conversion, since most certainly it is not redundant.
10220 elsif Operand_Typ = Any_Fixed then
10221 Test_Redundant := False;
10223 -- Mixed-mode operation involving a literal. Context must be a fixed
10224 -- type which is applied to the literal subsequently.
10226 if Is_Fixed_Point_Type (Typ) then
10227 Set_Etype (Operand, Universal_Real);
10229 elsif Is_Numeric_Type (Typ)
10230 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
10231 and then (Etype (Right_Opnd (Operand)) = Universal_Real
10232 or else
10233 Etype (Left_Opnd (Operand)) = Universal_Real)
10234 then
10235 -- Return if expression is ambiguous
10237 if Unique_Fixed_Point_Type (N) = Any_Type then
10238 return;
10240 -- If nothing else, the available fixed type is Duration
10242 else
10243 Set_Etype (Operand, Standard_Duration);
10244 end if;
10246 -- Resolve the real operand with largest available precision
10248 if Etype (Right_Opnd (Operand)) = Universal_Real then
10249 Rop := New_Copy_Tree (Right_Opnd (Operand));
10250 else
10251 Rop := New_Copy_Tree (Left_Opnd (Operand));
10252 end if;
10254 Resolve (Rop, Universal_Real);
10256 -- If the operand is a literal (it could be a non-static and
10257 -- illegal exponentiation) check whether the use of Duration
10258 -- is potentially inaccurate.
10260 if Nkind (Rop) = N_Real_Literal
10261 and then Realval (Rop) /= Ureal_0
10262 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
10263 then
10264 Error_Msg_N
10265 ("??universal real operand can only "
10266 & "be interpreted as Duration!", Rop);
10267 Error_Msg_N
10268 ("\??precision will be lost in the conversion!", Rop);
10269 end if;
10271 elsif Is_Numeric_Type (Typ)
10272 and then Nkind (Operand) in N_Op
10273 and then Unique_Fixed_Point_Type (N) /= Any_Type
10274 then
10275 Set_Etype (Operand, Standard_Duration);
10277 else
10278 Error_Msg_N ("invalid context for mixed mode operation", N);
10279 Set_Etype (Operand, Any_Type);
10280 return;
10281 end if;
10282 end if;
10284 Resolve (Operand);
10286 -- In SPARK, a type conversion between array types should be restricted
10287 -- to types which have matching static bounds.
10289 -- Protect call to Matching_Static_Array_Bounds to avoid costly
10290 -- operation if not needed.
10292 if Restriction_Check_Required (SPARK_05)
10293 and then Is_Array_Type (Target_Typ)
10294 and then Is_Array_Type (Operand_Typ)
10295 and then Operand_Typ /= Any_Composite -- or else Operand in error
10296 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
10297 then
10298 Check_SPARK_05_Restriction
10299 ("array types should have matching static bounds", N);
10300 end if;
10302 -- In formal mode, the operand of an ancestor type conversion must be an
10303 -- object (not an expression).
10305 if Is_Tagged_Type (Target_Typ)
10306 and then not Is_Class_Wide_Type (Target_Typ)
10307 and then Is_Tagged_Type (Operand_Typ)
10308 and then not Is_Class_Wide_Type (Operand_Typ)
10309 and then Is_Ancestor (Target_Typ, Operand_Typ)
10310 and then not Is_SPARK_05_Object_Reference (Operand)
10311 then
10312 Check_SPARK_05_Restriction ("object required", Operand);
10313 end if;
10315 Analyze_Dimension (N);
10317 -- Note: we do the Eval_Type_Conversion call before applying the
10318 -- required checks for a subtype conversion. This is important, since
10319 -- both are prepared under certain circumstances to change the type
10320 -- conversion to a constraint error node, but in the case of
10321 -- Eval_Type_Conversion this may reflect an illegality in the static
10322 -- case, and we would miss the illegality (getting only a warning
10323 -- message), if we applied the type conversion checks first.
10325 Eval_Type_Conversion (N);
10327 -- Even when evaluation is not possible, we may be able to simplify the
10328 -- conversion or its expression. This needs to be done before applying
10329 -- checks, since otherwise the checks may use the original expression
10330 -- and defeat the simplifications. This is specifically the case for
10331 -- elimination of the floating-point Truncation attribute in
10332 -- float-to-int conversions.
10334 Simplify_Type_Conversion (N);
10336 -- If after evaluation we still have a type conversion, then we may need
10337 -- to apply checks required for a subtype conversion.
10339 -- Skip these type conversion checks if universal fixed operands
10340 -- operands involved, since range checks are handled separately for
10341 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
10343 if Nkind (N) = N_Type_Conversion
10344 and then not Is_Generic_Type (Root_Type (Target_Typ))
10345 and then Target_Typ /= Universal_Fixed
10346 and then Operand_Typ /= Universal_Fixed
10347 then
10348 Apply_Type_Conversion_Checks (N);
10349 end if;
10351 -- Issue warning for conversion of simple object to its own type. We
10352 -- have to test the original nodes, since they may have been rewritten
10353 -- by various optimizations.
10355 Orig_N := Original_Node (N);
10357 -- Here we test for a redundant conversion if the warning mode is
10358 -- active (and was not locally reset), and we have a type conversion
10359 -- from source not appearing in a generic instance.
10361 if Test_Redundant
10362 and then Nkind (Orig_N) = N_Type_Conversion
10363 and then Comes_From_Source (Orig_N)
10364 and then not In_Instance
10365 then
10366 Orig_N := Original_Node (Expression (Orig_N));
10367 Orig_T := Target_Typ;
10369 -- If the node is part of a larger expression, the Target_Type
10370 -- may not be the original type of the node if the context is a
10371 -- condition. Recover original type to see if conversion is needed.
10373 if Is_Boolean_Type (Orig_T)
10374 and then Nkind (Parent (N)) in N_Op
10375 then
10376 Orig_T := Etype (Parent (N));
10377 end if;
10379 -- If we have an entity name, then give the warning if the entity
10380 -- is the right type, or if it is a loop parameter covered by the
10381 -- original type (that's needed because loop parameters have an
10382 -- odd subtype coming from the bounds).
10384 if (Is_Entity_Name (Orig_N)
10385 and then
10386 (Etype (Entity (Orig_N)) = Orig_T
10387 or else
10388 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
10389 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
10391 -- If not an entity, then type of expression must match
10393 or else Etype (Orig_N) = Orig_T
10394 then
10395 -- One more check, do not give warning if the analyzed conversion
10396 -- has an expression with non-static bounds, and the bounds of the
10397 -- target are static. This avoids junk warnings in cases where the
10398 -- conversion is necessary to establish staticness, for example in
10399 -- a case statement.
10401 if not Is_OK_Static_Subtype (Operand_Typ)
10402 and then Is_OK_Static_Subtype (Target_Typ)
10403 then
10404 null;
10406 -- Finally, if this type conversion occurs in a context requiring
10407 -- a prefix, and the expression is a qualified expression then the
10408 -- type conversion is not redundant, since a qualified expression
10409 -- is not a prefix, whereas a type conversion is. For example, "X
10410 -- := T'(Funx(...)).Y;" is illegal because a selected component
10411 -- requires a prefix, but a type conversion makes it legal: "X :=
10412 -- T(T'(Funx(...))).Y;"
10414 -- In Ada 2012, a qualified expression is a name, so this idiom is
10415 -- no longer needed, but we still suppress the warning because it
10416 -- seems unfriendly for warnings to pop up when you switch to the
10417 -- newer language version.
10419 elsif Nkind (Orig_N) = N_Qualified_Expression
10420 and then Nkind_In (Parent (N), N_Attribute_Reference,
10421 N_Indexed_Component,
10422 N_Selected_Component,
10423 N_Slice,
10424 N_Explicit_Dereference)
10425 then
10426 null;
10428 -- Never warn on conversion to Long_Long_Integer'Base since
10429 -- that is most likely an artifact of the extended overflow
10430 -- checking and comes from complex expanded code.
10432 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
10433 null;
10435 -- Here we give the redundant conversion warning. If it is an
10436 -- entity, give the name of the entity in the message. If not,
10437 -- just mention the expression.
10439 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
10441 else
10442 if Is_Entity_Name (Orig_N) then
10443 Error_Msg_Node_2 := Orig_T;
10444 Error_Msg_NE -- CODEFIX
10445 ("??redundant conversion, & is of type &!",
10446 N, Entity (Orig_N));
10447 else
10448 Error_Msg_NE
10449 ("??redundant conversion, expression is of type&!",
10450 N, Orig_T);
10451 end if;
10452 end if;
10453 end if;
10454 end if;
10456 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
10457 -- No need to perform any interface conversion if the type of the
10458 -- expression coincides with the target type.
10460 if Ada_Version >= Ada_2005
10461 and then Expander_Active
10462 and then Operand_Typ /= Target_Typ
10463 then
10464 declare
10465 Opnd : Entity_Id := Operand_Typ;
10466 Target : Entity_Id := Target_Typ;
10468 begin
10469 -- If the type of the operand is a limited view, use the non-
10470 -- limited view when available.
10472 if From_Limited_With (Opnd)
10473 and then Ekind (Opnd) in Incomplete_Kind
10474 and then Present (Non_Limited_View (Opnd))
10475 then
10476 Opnd := Non_Limited_View (Opnd);
10477 Set_Etype (Expression (N), Opnd);
10478 end if;
10480 if Is_Access_Type (Opnd) then
10481 Opnd := Designated_Type (Opnd);
10482 end if;
10484 if Is_Access_Type (Target_Typ) then
10485 Target := Designated_Type (Target);
10486 end if;
10488 if Opnd = Target then
10489 null;
10491 -- Conversion from interface type
10493 elsif Is_Interface (Opnd) then
10495 -- Ada 2005 (AI-217): Handle entities from limited views
10497 if From_Limited_With (Opnd) then
10498 Error_Msg_Qual_Level := 99;
10499 Error_Msg_NE -- CODEFIX
10500 ("missing WITH clause on package &", N,
10501 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
10502 Error_Msg_N
10503 ("type conversions require visibility of the full view",
10506 elsif From_Limited_With (Target)
10507 and then not
10508 (Is_Access_Type (Target_Typ)
10509 and then Present (Non_Limited_View (Etype (Target))))
10510 then
10511 Error_Msg_Qual_Level := 99;
10512 Error_Msg_NE -- CODEFIX
10513 ("missing WITH clause on package &", N,
10514 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
10515 Error_Msg_N
10516 ("type conversions require visibility of the full view",
10519 else
10520 Expand_Interface_Conversion (N);
10521 end if;
10523 -- Conversion to interface type
10525 elsif Is_Interface (Target) then
10527 -- Handle subtypes
10529 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
10530 Opnd := Etype (Opnd);
10531 end if;
10533 if Is_Class_Wide_Type (Opnd)
10534 or else Interface_Present_In_Ancestor
10535 (Typ => Opnd,
10536 Iface => Target)
10537 then
10538 Expand_Interface_Conversion (N);
10539 else
10540 Error_Msg_Name_1 := Chars (Etype (Target));
10541 Error_Msg_Name_2 := Chars (Opnd);
10542 Error_Msg_N
10543 ("wrong interface conversion (% is not a progenitor "
10544 & "of %)", N);
10545 end if;
10546 end if;
10547 end;
10548 end if;
10550 -- Ada 2012: if target type has predicates, the result requires a
10551 -- predicate check. If the context is a call to another predicate
10552 -- check we must prevent infinite recursion.
10554 if Has_Predicates (Target_Typ) then
10555 if Nkind (Parent (N)) = N_Function_Call
10556 and then Present (Name (Parent (N)))
10557 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
10558 or else
10559 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
10560 then
10561 null;
10563 else
10564 Apply_Predicate_Check (N, Target_Typ);
10565 end if;
10566 end if;
10568 -- If at this stage we have a real to integer conversion, make sure
10569 -- that the Do_Range_Check flag is set, because such conversions in
10570 -- general need a range check. We only need this if expansion is off
10571 -- or we are in GNATProve mode.
10573 if Nkind (N) = N_Type_Conversion
10574 and then (GNATprove_Mode or not Expander_Active)
10575 and then Is_Integer_Type (Target_Typ)
10576 and then Is_Real_Type (Operand_Typ)
10577 then
10578 Set_Do_Range_Check (Operand);
10579 end if;
10580 end Resolve_Type_Conversion;
10582 ----------------------
10583 -- Resolve_Unary_Op --
10584 ----------------------
10586 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
10587 B_Typ : constant Entity_Id := Base_Type (Typ);
10588 R : constant Node_Id := Right_Opnd (N);
10589 OK : Boolean;
10590 Lo : Uint;
10591 Hi : Uint;
10593 begin
10594 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
10595 Error_Msg_Name_1 := Chars (Typ);
10596 Check_SPARK_05_Restriction
10597 ("unary operator not defined for modular type%", N);
10598 end if;
10600 -- Deal with intrinsic unary operators
10602 if Comes_From_Source (N)
10603 and then Ekind (Entity (N)) = E_Function
10604 and then Is_Imported (Entity (N))
10605 and then Is_Intrinsic_Subprogram (Entity (N))
10606 then
10607 Resolve_Intrinsic_Unary_Operator (N, Typ);
10608 return;
10609 end if;
10611 -- Deal with universal cases
10613 if Etype (R) = Universal_Integer
10614 or else
10615 Etype (R) = Universal_Real
10616 then
10617 Check_For_Visible_Operator (N, B_Typ);
10618 end if;
10620 Set_Etype (N, B_Typ);
10621 Resolve (R, B_Typ);
10623 -- Generate warning for expressions like abs (x mod 2)
10625 if Warn_On_Redundant_Constructs
10626 and then Nkind (N) = N_Op_Abs
10627 then
10628 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
10630 if OK and then Hi >= Lo and then Lo >= 0 then
10631 Error_Msg_N -- CODEFIX
10632 ("?r?abs applied to known non-negative value has no effect", N);
10633 end if;
10634 end if;
10636 -- Deal with reference generation
10638 Check_Unset_Reference (R);
10639 Generate_Operator_Reference (N, B_Typ);
10640 Analyze_Dimension (N);
10641 Eval_Unary_Op (N);
10643 -- Set overflow checking bit. Much cleverer code needed here eventually
10644 -- and perhaps the Resolve routines should be separated for the various
10645 -- arithmetic operations, since they will need different processing ???
10647 if Nkind (N) in N_Op then
10648 if not Overflow_Checks_Suppressed (Etype (N)) then
10649 Enable_Overflow_Check (N);
10650 end if;
10651 end if;
10653 -- Generate warning for expressions like -5 mod 3 for integers. No need
10654 -- to worry in the floating-point case, since parens do not affect the
10655 -- result so there is no point in giving in a warning.
10657 declare
10658 Norig : constant Node_Id := Original_Node (N);
10659 Rorig : Node_Id;
10660 Val : Uint;
10661 HB : Uint;
10662 LB : Uint;
10663 Lval : Uint;
10664 Opnd : Node_Id;
10666 begin
10667 if Warn_On_Questionable_Missing_Parens
10668 and then Comes_From_Source (Norig)
10669 and then Is_Integer_Type (Typ)
10670 and then Nkind (Norig) = N_Op_Minus
10671 then
10672 Rorig := Original_Node (Right_Opnd (Norig));
10674 -- We are looking for cases where the right operand is not
10675 -- parenthesized, and is a binary operator, multiply, divide, or
10676 -- mod. These are the cases where the grouping can affect results.
10678 if Paren_Count (Rorig) = 0
10679 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
10680 then
10681 -- For mod, we always give the warning, since the value is
10682 -- affected by the parenthesization (e.g. (-5) mod 315 /=
10683 -- -(5 mod 315)). But for the other cases, the only concern is
10684 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
10685 -- overflows, but (-2) * 64 does not). So we try to give the
10686 -- message only when overflow is possible.
10688 if Nkind (Rorig) /= N_Op_Mod
10689 and then Compile_Time_Known_Value (R)
10690 then
10691 Val := Expr_Value (R);
10693 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
10694 HB := Expr_Value (Type_High_Bound (Typ));
10695 else
10696 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
10697 end if;
10699 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
10700 LB := Expr_Value (Type_Low_Bound (Typ));
10701 else
10702 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
10703 end if;
10705 -- Note that the test below is deliberately excluding the
10706 -- largest negative number, since that is a potentially
10707 -- troublesome case (e.g. -2 * x, where the result is the
10708 -- largest negative integer has an overflow with 2 * x).
10710 if Val > LB and then Val <= HB then
10711 return;
10712 end if;
10713 end if;
10715 -- For the multiplication case, the only case we have to worry
10716 -- about is when (-a)*b is exactly the largest negative number
10717 -- so that -(a*b) can cause overflow. This can only happen if
10718 -- a is a power of 2, and more generally if any operand is a
10719 -- constant that is not a power of 2, then the parentheses
10720 -- cannot affect whether overflow occurs. We only bother to
10721 -- test the left most operand
10723 -- Loop looking at left operands for one that has known value
10725 Opnd := Rorig;
10726 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
10727 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
10728 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
10730 -- Operand value of 0 or 1 skips warning
10732 if Lval <= 1 then
10733 return;
10735 -- Otherwise check power of 2, if power of 2, warn, if
10736 -- anything else, skip warning.
10738 else
10739 while Lval /= 2 loop
10740 if Lval mod 2 = 1 then
10741 return;
10742 else
10743 Lval := Lval / 2;
10744 end if;
10745 end loop;
10747 exit Opnd_Loop;
10748 end if;
10749 end if;
10751 -- Keep looking at left operands
10753 Opnd := Left_Opnd (Opnd);
10754 end loop Opnd_Loop;
10756 -- For rem or "/" we can only have a problematic situation
10757 -- if the divisor has a value of minus one or one. Otherwise
10758 -- overflow is impossible (divisor > 1) or we have a case of
10759 -- division by zero in any case.
10761 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
10762 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
10763 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
10764 then
10765 return;
10766 end if;
10768 -- If we fall through warning should be issued
10770 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
10772 Error_Msg_N
10773 ("??unary minus expression should be parenthesized here!", N);
10774 end if;
10775 end if;
10776 end;
10777 end Resolve_Unary_Op;
10779 ----------------------------------
10780 -- Resolve_Unchecked_Expression --
10781 ----------------------------------
10783 procedure Resolve_Unchecked_Expression
10784 (N : Node_Id;
10785 Typ : Entity_Id)
10787 begin
10788 Resolve (Expression (N), Typ, Suppress => All_Checks);
10789 Set_Etype (N, Typ);
10790 end Resolve_Unchecked_Expression;
10792 ---------------------------------------
10793 -- Resolve_Unchecked_Type_Conversion --
10794 ---------------------------------------
10796 procedure Resolve_Unchecked_Type_Conversion
10797 (N : Node_Id;
10798 Typ : Entity_Id)
10800 pragma Warnings (Off, Typ);
10802 Operand : constant Node_Id := Expression (N);
10803 Opnd_Type : constant Entity_Id := Etype (Operand);
10805 begin
10806 -- Resolve operand using its own type
10808 Resolve (Operand, Opnd_Type);
10810 -- In an inlined context, the unchecked conversion may be applied
10811 -- to a literal, in which case its type is the type of the context.
10812 -- (In other contexts conversions cannot apply to literals).
10814 if In_Inlined_Body
10815 and then (Opnd_Type = Any_Character or else
10816 Opnd_Type = Any_Integer or else
10817 Opnd_Type = Any_Real)
10818 then
10819 Set_Etype (Operand, Typ);
10820 end if;
10822 Analyze_Dimension (N);
10823 Eval_Unchecked_Conversion (N);
10824 end Resolve_Unchecked_Type_Conversion;
10826 ------------------------------
10827 -- Rewrite_Operator_As_Call --
10828 ------------------------------
10830 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
10831 Loc : constant Source_Ptr := Sloc (N);
10832 Actuals : constant List_Id := New_List;
10833 New_N : Node_Id;
10835 begin
10836 if Nkind (N) in N_Binary_Op then
10837 Append (Left_Opnd (N), Actuals);
10838 end if;
10840 Append (Right_Opnd (N), Actuals);
10842 New_N :=
10843 Make_Function_Call (Sloc => Loc,
10844 Name => New_Occurrence_Of (Nam, Loc),
10845 Parameter_Associations => Actuals);
10847 Preserve_Comes_From_Source (New_N, N);
10848 Preserve_Comes_From_Source (Name (New_N), N);
10849 Rewrite (N, New_N);
10850 Set_Etype (N, Etype (Nam));
10851 end Rewrite_Operator_As_Call;
10853 ------------------------------
10854 -- Rewrite_Renamed_Operator --
10855 ------------------------------
10857 procedure Rewrite_Renamed_Operator
10858 (N : Node_Id;
10859 Op : Entity_Id;
10860 Typ : Entity_Id)
10862 Nam : constant Name_Id := Chars (Op);
10863 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
10864 Op_Node : Node_Id;
10866 begin
10867 -- Do not perform this transformation within a pre/postcondition,
10868 -- because the expression will be re-analyzed, and the transformation
10869 -- might affect the visibility of the operator, e.g. in an instance.
10871 if In_Assertion_Expr > 0 then
10872 return;
10873 end if;
10875 -- Rewrite the operator node using the real operator, not its renaming.
10876 -- Exclude user-defined intrinsic operations of the same name, which are
10877 -- treated separately and rewritten as calls.
10879 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
10880 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
10881 Set_Chars (Op_Node, Nam);
10882 Set_Etype (Op_Node, Etype (N));
10883 Set_Entity (Op_Node, Op);
10884 Set_Right_Opnd (Op_Node, Right_Opnd (N));
10886 -- Indicate that both the original entity and its renaming are
10887 -- referenced at this point.
10889 Generate_Reference (Entity (N), N);
10890 Generate_Reference (Op, N);
10892 if Is_Binary then
10893 Set_Left_Opnd (Op_Node, Left_Opnd (N));
10894 end if;
10896 Rewrite (N, Op_Node);
10898 -- If the context type is private, add the appropriate conversions so
10899 -- that the operator is applied to the full view. This is done in the
10900 -- routines that resolve intrinsic operators.
10902 if Is_Intrinsic_Subprogram (Op)
10903 and then Is_Private_Type (Typ)
10904 then
10905 case Nkind (N) is
10906 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
10907 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
10908 Resolve_Intrinsic_Operator (N, Typ);
10910 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
10911 Resolve_Intrinsic_Unary_Operator (N, Typ);
10913 when others =>
10914 Resolve (N, Typ);
10915 end case;
10916 end if;
10918 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
10920 -- Operator renames a user-defined operator of the same name. Use the
10921 -- original operator in the node, which is the one Gigi knows about.
10923 Set_Entity (N, Op);
10924 Set_Is_Overloaded (N, False);
10925 end if;
10926 end Rewrite_Renamed_Operator;
10928 -----------------------
10929 -- Set_Slice_Subtype --
10930 -----------------------
10932 -- Build an implicit subtype declaration to represent the type delivered by
10933 -- the slice. This is an abbreviated version of an array subtype. We define
10934 -- an index subtype for the slice, using either the subtype name or the
10935 -- discrete range of the slice. To be consistent with index usage elsewhere
10936 -- we create a list header to hold the single index. This list is not
10937 -- otherwise attached to the syntax tree.
10939 procedure Set_Slice_Subtype (N : Node_Id) is
10940 Loc : constant Source_Ptr := Sloc (N);
10941 Index_List : constant List_Id := New_List;
10942 Index : Node_Id;
10943 Index_Subtype : Entity_Id;
10944 Index_Type : Entity_Id;
10945 Slice_Subtype : Entity_Id;
10946 Drange : constant Node_Id := Discrete_Range (N);
10948 begin
10949 Index_Type := Base_Type (Etype (Drange));
10951 if Is_Entity_Name (Drange) then
10952 Index_Subtype := Entity (Drange);
10954 else
10955 -- We force the evaluation of a range. This is definitely needed in
10956 -- the renamed case, and seems safer to do unconditionally. Note in
10957 -- any case that since we will create and insert an Itype referring
10958 -- to this range, we must make sure any side effect removal actions
10959 -- are inserted before the Itype definition.
10961 if Nkind (Drange) = N_Range then
10962 Force_Evaluation (Low_Bound (Drange));
10963 Force_Evaluation (High_Bound (Drange));
10965 -- If the discrete range is given by a subtype indication, the
10966 -- type of the slice is the base of the subtype mark.
10968 elsif Nkind (Drange) = N_Subtype_Indication then
10969 declare
10970 R : constant Node_Id := Range_Expression (Constraint (Drange));
10971 begin
10972 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
10973 Force_Evaluation (Low_Bound (R));
10974 Force_Evaluation (High_Bound (R));
10975 end;
10976 end if;
10978 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10980 -- Take a new copy of Drange (where bounds have been rewritten to
10981 -- reference side-effect-free names). Using a separate tree ensures
10982 -- that further expansion (e.g. while rewriting a slice assignment
10983 -- into a FOR loop) does not attempt to remove side effects on the
10984 -- bounds again (which would cause the bounds in the index subtype
10985 -- definition to refer to temporaries before they are defined) (the
10986 -- reason is that some names are considered side effect free here
10987 -- for the subtype, but not in the context of a loop iteration
10988 -- scheme).
10990 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
10991 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
10992 Set_Etype (Index_Subtype, Index_Type);
10993 Set_Size_Info (Index_Subtype, Index_Type);
10994 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10995 end if;
10997 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
10999 Index := New_Occurrence_Of (Index_Subtype, Loc);
11000 Set_Etype (Index, Index_Subtype);
11001 Append (Index, Index_List);
11003 Set_First_Index (Slice_Subtype, Index);
11004 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
11005 Set_Is_Constrained (Slice_Subtype, True);
11007 Check_Compile_Time_Size (Slice_Subtype);
11009 -- The Etype of the existing Slice node is reset to this slice subtype.
11010 -- Its bounds are obtained from its first index.
11012 Set_Etype (N, Slice_Subtype);
11014 -- For packed slice subtypes, freeze immediately (except in the case of
11015 -- being in a "spec expression" where we never freeze when we first see
11016 -- the expression).
11018 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
11019 Freeze_Itype (Slice_Subtype, N);
11021 -- For all other cases insert an itype reference in the slice's actions
11022 -- so that the itype is frozen at the proper place in the tree (i.e. at
11023 -- the point where actions for the slice are analyzed). Note that this
11024 -- is different from freezing the itype immediately, which might be
11025 -- premature (e.g. if the slice is within a transient scope). This needs
11026 -- to be done only if expansion is enabled.
11028 elsif Expander_Active then
11029 Ensure_Defined (Typ => Slice_Subtype, N => N);
11030 end if;
11031 end Set_Slice_Subtype;
11033 --------------------------------
11034 -- Set_String_Literal_Subtype --
11035 --------------------------------
11037 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
11038 Loc : constant Source_Ptr := Sloc (N);
11039 Low_Bound : constant Node_Id :=
11040 Type_Low_Bound (Etype (First_Index (Typ)));
11041 Subtype_Id : Entity_Id;
11043 begin
11044 if Nkind (N) /= N_String_Literal then
11045 return;
11046 end if;
11048 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
11049 Set_String_Literal_Length (Subtype_Id, UI_From_Int
11050 (String_Length (Strval (N))));
11051 Set_Etype (Subtype_Id, Base_Type (Typ));
11052 Set_Is_Constrained (Subtype_Id);
11053 Set_Etype (N, Subtype_Id);
11055 -- The low bound is set from the low bound of the corresponding index
11056 -- type. Note that we do not store the high bound in the string literal
11057 -- subtype, but it can be deduced if necessary from the length and the
11058 -- low bound.
11060 if Is_OK_Static_Expression (Low_Bound) then
11061 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
11063 -- If the lower bound is not static we create a range for the string
11064 -- literal, using the index type and the known length of the literal.
11065 -- The index type is not necessarily Positive, so the upper bound is
11066 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
11068 else
11069 declare
11070 Index_List : constant List_Id := New_List;
11071 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
11072 High_Bound : constant Node_Id :=
11073 Make_Attribute_Reference (Loc,
11074 Attribute_Name => Name_Val,
11075 Prefix =>
11076 New_Occurrence_Of (Index_Type, Loc),
11077 Expressions => New_List (
11078 Make_Op_Add (Loc,
11079 Left_Opnd =>
11080 Make_Attribute_Reference (Loc,
11081 Attribute_Name => Name_Pos,
11082 Prefix =>
11083 New_Occurrence_Of (Index_Type, Loc),
11084 Expressions =>
11085 New_List (New_Copy_Tree (Low_Bound))),
11086 Right_Opnd =>
11087 Make_Integer_Literal (Loc,
11088 String_Length (Strval (N)) - 1))));
11090 Array_Subtype : Entity_Id;
11091 Drange : Node_Id;
11092 Index : Node_Id;
11093 Index_Subtype : Entity_Id;
11095 begin
11096 if Is_Integer_Type (Index_Type) then
11097 Set_String_Literal_Low_Bound
11098 (Subtype_Id, Make_Integer_Literal (Loc, 1));
11100 else
11101 -- If the index type is an enumeration type, build bounds
11102 -- expression with attributes.
11104 Set_String_Literal_Low_Bound
11105 (Subtype_Id,
11106 Make_Attribute_Reference (Loc,
11107 Attribute_Name => Name_First,
11108 Prefix =>
11109 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
11110 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
11111 end if;
11113 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
11115 -- Build bona fide subtype for the string, and wrap it in an
11116 -- unchecked conversion, because the backend expects the
11117 -- String_Literal_Subtype to have a static lower bound.
11119 Index_Subtype :=
11120 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
11121 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
11122 Set_Scalar_Range (Index_Subtype, Drange);
11123 Set_Parent (Drange, N);
11124 Analyze_And_Resolve (Drange, Index_Type);
11126 -- In the context, the Index_Type may already have a constraint,
11127 -- so use common base type on string subtype. The base type may
11128 -- be used when generating attributes of the string, for example
11129 -- in the context of a slice assignment.
11131 Set_Etype (Index_Subtype, Base_Type (Index_Type));
11132 Set_Size_Info (Index_Subtype, Index_Type);
11133 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
11135 Array_Subtype := Create_Itype (E_Array_Subtype, N);
11137 Index := New_Occurrence_Of (Index_Subtype, Loc);
11138 Set_Etype (Index, Index_Subtype);
11139 Append (Index, Index_List);
11141 Set_First_Index (Array_Subtype, Index);
11142 Set_Etype (Array_Subtype, Base_Type (Typ));
11143 Set_Is_Constrained (Array_Subtype, True);
11145 Rewrite (N,
11146 Make_Unchecked_Type_Conversion (Loc,
11147 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
11148 Expression => Relocate_Node (N)));
11149 Set_Etype (N, Array_Subtype);
11150 end;
11151 end if;
11152 end Set_String_Literal_Subtype;
11154 ------------------------------
11155 -- Simplify_Type_Conversion --
11156 ------------------------------
11158 procedure Simplify_Type_Conversion (N : Node_Id) is
11159 begin
11160 if Nkind (N) = N_Type_Conversion then
11161 declare
11162 Operand : constant Node_Id := Expression (N);
11163 Target_Typ : constant Entity_Id := Etype (N);
11164 Opnd_Typ : constant Entity_Id := Etype (Operand);
11166 begin
11167 -- Special processing if the conversion is the expression of a
11168 -- Rounding or Truncation attribute reference. In this case we
11169 -- replace:
11171 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
11173 -- by
11175 -- ityp (x)
11177 -- with the Float_Truncate flag set to False or True respectively,
11178 -- which is more efficient.
11180 if Is_Floating_Point_Type (Opnd_Typ)
11181 and then
11182 (Is_Integer_Type (Target_Typ)
11183 or else (Is_Fixed_Point_Type (Target_Typ)
11184 and then Conversion_OK (N)))
11185 and then Nkind (Operand) = N_Attribute_Reference
11186 and then Nam_In (Attribute_Name (Operand), Name_Rounding,
11187 Name_Truncation)
11188 then
11189 declare
11190 Truncate : constant Boolean :=
11191 Attribute_Name (Operand) = Name_Truncation;
11192 begin
11193 Rewrite (Operand,
11194 Relocate_Node (First (Expressions (Operand))));
11195 Set_Float_Truncate (N, Truncate);
11196 end;
11197 end if;
11198 end;
11199 end if;
11200 end Simplify_Type_Conversion;
11202 -----------------------------
11203 -- Unique_Fixed_Point_Type --
11204 -----------------------------
11206 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
11207 T1 : Entity_Id := Empty;
11208 T2 : Entity_Id;
11209 Item : Node_Id;
11210 Scop : Entity_Id;
11212 procedure Fixed_Point_Error;
11213 -- Give error messages for true ambiguity. Messages are posted on node
11214 -- N, and entities T1, T2 are the possible interpretations.
11216 -----------------------
11217 -- Fixed_Point_Error --
11218 -----------------------
11220 procedure Fixed_Point_Error is
11221 begin
11222 Error_Msg_N ("ambiguous universal_fixed_expression", N);
11223 Error_Msg_NE ("\\possible interpretation as}", N, T1);
11224 Error_Msg_NE ("\\possible interpretation as}", N, T2);
11225 end Fixed_Point_Error;
11227 -- Start of processing for Unique_Fixed_Point_Type
11229 begin
11230 -- The operations on Duration are visible, so Duration is always a
11231 -- possible interpretation.
11233 T1 := Standard_Duration;
11235 -- Look for fixed-point types in enclosing scopes
11237 Scop := Current_Scope;
11238 while Scop /= Standard_Standard loop
11239 T2 := First_Entity (Scop);
11240 while Present (T2) loop
11241 if Is_Fixed_Point_Type (T2)
11242 and then Current_Entity (T2) = T2
11243 and then Scope (Base_Type (T2)) = Scop
11244 then
11245 if Present (T1) then
11246 Fixed_Point_Error;
11247 return Any_Type;
11248 else
11249 T1 := T2;
11250 end if;
11251 end if;
11253 Next_Entity (T2);
11254 end loop;
11256 Scop := Scope (Scop);
11257 end loop;
11259 -- Look for visible fixed type declarations in the context
11261 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
11262 while Present (Item) loop
11263 if Nkind (Item) = N_With_Clause then
11264 Scop := Entity (Name (Item));
11265 T2 := First_Entity (Scop);
11266 while Present (T2) loop
11267 if Is_Fixed_Point_Type (T2)
11268 and then Scope (Base_Type (T2)) = Scop
11269 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
11270 then
11271 if Present (T1) then
11272 Fixed_Point_Error;
11273 return Any_Type;
11274 else
11275 T1 := T2;
11276 end if;
11277 end if;
11279 Next_Entity (T2);
11280 end loop;
11281 end if;
11283 Next (Item);
11284 end loop;
11286 if Nkind (N) = N_Real_Literal then
11287 Error_Msg_NE
11288 ("??real literal interpreted as }!", N, T1);
11289 else
11290 Error_Msg_NE
11291 ("??universal_fixed expression interpreted as }!", N, T1);
11292 end if;
11294 return T1;
11295 end Unique_Fixed_Point_Type;
11297 ----------------------
11298 -- Valid_Conversion --
11299 ----------------------
11301 function Valid_Conversion
11302 (N : Node_Id;
11303 Target : Entity_Id;
11304 Operand : Node_Id;
11305 Report_Errs : Boolean := True) return Boolean
11307 Target_Type : constant Entity_Id := Base_Type (Target);
11308 Opnd_Type : Entity_Id := Etype (Operand);
11309 Inc_Ancestor : Entity_Id;
11311 function Conversion_Check
11312 (Valid : Boolean;
11313 Msg : String) return Boolean;
11314 -- Little routine to post Msg if Valid is False, returns Valid value
11316 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
11317 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
11319 procedure Conversion_Error_NE
11320 (Msg : String;
11321 N : Node_Or_Entity_Id;
11322 E : Node_Or_Entity_Id);
11323 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
11325 function Valid_Tagged_Conversion
11326 (Target_Type : Entity_Id;
11327 Opnd_Type : Entity_Id) return Boolean;
11328 -- Specifically test for validity of tagged conversions
11330 function Valid_Array_Conversion return Boolean;
11331 -- Check index and component conformance, and accessibility levels if
11332 -- the component types are anonymous access types (Ada 2005).
11334 ----------------------
11335 -- Conversion_Check --
11336 ----------------------
11338 function Conversion_Check
11339 (Valid : Boolean;
11340 Msg : String) return Boolean
11342 begin
11343 if not Valid
11345 -- A generic unit has already been analyzed and we have verified
11346 -- that a particular conversion is OK in that context. Since the
11347 -- instance is reanalyzed without relying on the relationships
11348 -- established during the analysis of the generic, it is possible
11349 -- to end up with inconsistent views of private types. Do not emit
11350 -- the error message in such cases. The rest of the machinery in
11351 -- Valid_Conversion still ensures the proper compatibility of
11352 -- target and operand types.
11354 and then not In_Instance
11355 then
11356 Conversion_Error_N (Msg, Operand);
11357 end if;
11359 return Valid;
11360 end Conversion_Check;
11362 ------------------------
11363 -- Conversion_Error_N --
11364 ------------------------
11366 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
11367 begin
11368 if Report_Errs then
11369 Error_Msg_N (Msg, N);
11370 end if;
11371 end Conversion_Error_N;
11373 -------------------------
11374 -- Conversion_Error_NE --
11375 -------------------------
11377 procedure Conversion_Error_NE
11378 (Msg : String;
11379 N : Node_Or_Entity_Id;
11380 E : Node_Or_Entity_Id)
11382 begin
11383 if Report_Errs then
11384 Error_Msg_NE (Msg, N, E);
11385 end if;
11386 end Conversion_Error_NE;
11388 ----------------------------
11389 -- Valid_Array_Conversion --
11390 ----------------------------
11392 function Valid_Array_Conversion return Boolean
11394 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
11395 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
11397 Opnd_Index : Node_Id;
11398 Opnd_Index_Type : Entity_Id;
11400 Target_Comp_Type : constant Entity_Id :=
11401 Component_Type (Target_Type);
11402 Target_Comp_Base : constant Entity_Id :=
11403 Base_Type (Target_Comp_Type);
11405 Target_Index : Node_Id;
11406 Target_Index_Type : Entity_Id;
11408 begin
11409 -- Error if wrong number of dimensions
11412 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
11413 then
11414 Conversion_Error_N
11415 ("incompatible number of dimensions for conversion", Operand);
11416 return False;
11418 -- Number of dimensions matches
11420 else
11421 -- Loop through indexes of the two arrays
11423 Target_Index := First_Index (Target_Type);
11424 Opnd_Index := First_Index (Opnd_Type);
11425 while Present (Target_Index) and then Present (Opnd_Index) loop
11426 Target_Index_Type := Etype (Target_Index);
11427 Opnd_Index_Type := Etype (Opnd_Index);
11429 -- Error if index types are incompatible
11431 if not (Is_Integer_Type (Target_Index_Type)
11432 and then Is_Integer_Type (Opnd_Index_Type))
11433 and then (Root_Type (Target_Index_Type)
11434 /= Root_Type (Opnd_Index_Type))
11435 then
11436 Conversion_Error_N
11437 ("incompatible index types for array conversion",
11438 Operand);
11439 return False;
11440 end if;
11442 Next_Index (Target_Index);
11443 Next_Index (Opnd_Index);
11444 end loop;
11446 -- If component types have same base type, all set
11448 if Target_Comp_Base = Opnd_Comp_Base then
11449 null;
11451 -- Here if base types of components are not the same. The only
11452 -- time this is allowed is if we have anonymous access types.
11454 -- The conversion of arrays of anonymous access types can lead
11455 -- to dangling pointers. AI-392 formalizes the accessibility
11456 -- checks that must be applied to such conversions to prevent
11457 -- out-of-scope references.
11459 elsif Ekind_In
11460 (Target_Comp_Base, E_Anonymous_Access_Type,
11461 E_Anonymous_Access_Subprogram_Type)
11462 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
11463 and then
11464 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
11465 then
11466 if Type_Access_Level (Target_Type) <
11467 Deepest_Type_Access_Level (Opnd_Type)
11468 then
11469 if In_Instance_Body then
11470 Error_Msg_Warn := SPARK_Mode /= On;
11471 Conversion_Error_N
11472 ("source array type has deeper accessibility "
11473 & "level than target<<", Operand);
11474 Conversion_Error_N ("\Program_Error [<<", Operand);
11475 Rewrite (N,
11476 Make_Raise_Program_Error (Sloc (N),
11477 Reason => PE_Accessibility_Check_Failed));
11478 Set_Etype (N, Target_Type);
11479 return False;
11481 -- Conversion not allowed because of accessibility levels
11483 else
11484 Conversion_Error_N
11485 ("source array type has deeper accessibility "
11486 & "level than target", Operand);
11487 return False;
11488 end if;
11490 else
11491 null;
11492 end if;
11494 -- All other cases where component base types do not match
11496 else
11497 Conversion_Error_N
11498 ("incompatible component types for array conversion",
11499 Operand);
11500 return False;
11501 end if;
11503 -- Check that component subtypes statically match. For numeric
11504 -- types this means that both must be either constrained or
11505 -- unconstrained. For enumeration types the bounds must match.
11506 -- All of this is checked in Subtypes_Statically_Match.
11508 if not Subtypes_Statically_Match
11509 (Target_Comp_Type, Opnd_Comp_Type)
11510 then
11511 Conversion_Error_N
11512 ("component subtypes must statically match", Operand);
11513 return False;
11514 end if;
11515 end if;
11517 return True;
11518 end Valid_Array_Conversion;
11520 -----------------------------
11521 -- Valid_Tagged_Conversion --
11522 -----------------------------
11524 function Valid_Tagged_Conversion
11525 (Target_Type : Entity_Id;
11526 Opnd_Type : Entity_Id) return Boolean
11528 begin
11529 -- Upward conversions are allowed (RM 4.6(22))
11531 if Covers (Target_Type, Opnd_Type)
11532 or else Is_Ancestor (Target_Type, Opnd_Type)
11533 then
11534 return True;
11536 -- Downward conversion are allowed if the operand is class-wide
11537 -- (RM 4.6(23)).
11539 elsif Is_Class_Wide_Type (Opnd_Type)
11540 and then Covers (Opnd_Type, Target_Type)
11541 then
11542 return True;
11544 elsif Covers (Opnd_Type, Target_Type)
11545 or else Is_Ancestor (Opnd_Type, Target_Type)
11546 then
11547 return
11548 Conversion_Check (False,
11549 "downward conversion of tagged objects not allowed");
11551 -- Ada 2005 (AI-251): The conversion to/from interface types is
11552 -- always valid
11554 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
11555 return True;
11557 -- If the operand is a class-wide type obtained through a limited_
11558 -- with clause, and the context includes the non-limited view, use
11559 -- it to determine whether the conversion is legal.
11561 elsif Is_Class_Wide_Type (Opnd_Type)
11562 and then From_Limited_With (Opnd_Type)
11563 and then Present (Non_Limited_View (Etype (Opnd_Type)))
11564 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
11565 then
11566 return True;
11568 elsif Is_Access_Type (Opnd_Type)
11569 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
11570 then
11571 return True;
11573 else
11574 Conversion_Error_NE
11575 ("invalid tagged conversion, not compatible with}",
11576 N, First_Subtype (Opnd_Type));
11577 return False;
11578 end if;
11579 end Valid_Tagged_Conversion;
11581 -- Start of processing for Valid_Conversion
11583 begin
11584 Check_Parameterless_Call (Operand);
11586 if Is_Overloaded (Operand) then
11587 declare
11588 I : Interp_Index;
11589 I1 : Interp_Index;
11590 It : Interp;
11591 It1 : Interp;
11592 N1 : Entity_Id;
11593 T1 : Entity_Id;
11595 begin
11596 -- Remove procedure calls, which syntactically cannot appear in
11597 -- this context, but which cannot be removed by type checking,
11598 -- because the context does not impose a type.
11600 -- The node may be labelled overloaded, but still contain only one
11601 -- interpretation because others were discarded earlier. If this
11602 -- is the case, retain the single interpretation if legal.
11604 Get_First_Interp (Operand, I, It);
11605 Opnd_Type := It.Typ;
11606 Get_Next_Interp (I, It);
11608 if Present (It.Typ)
11609 and then Opnd_Type /= Standard_Void_Type
11610 then
11611 -- More than one candidate interpretation is available
11613 Get_First_Interp (Operand, I, It);
11614 while Present (It.Typ) loop
11615 if It.Typ = Standard_Void_Type then
11616 Remove_Interp (I);
11617 end if;
11619 -- When compiling for a system where Address is of a visible
11620 -- integer type, spurious ambiguities can be produced when
11621 -- arithmetic operations have a literal operand and return
11622 -- System.Address or a descendant of it. These ambiguities
11623 -- are usually resolved by the context, but for conversions
11624 -- there is no context type and the removal of the spurious
11625 -- operations must be done explicitly here.
11627 if not Address_Is_Private
11628 and then Is_Descendent_Of_Address (It.Typ)
11629 then
11630 Remove_Interp (I);
11631 end if;
11633 Get_Next_Interp (I, It);
11634 end loop;
11635 end if;
11637 Get_First_Interp (Operand, I, It);
11638 I1 := I;
11639 It1 := It;
11641 if No (It.Typ) then
11642 Conversion_Error_N ("illegal operand in conversion", Operand);
11643 return False;
11644 end if;
11646 Get_Next_Interp (I, It);
11648 if Present (It.Typ) then
11649 N1 := It1.Nam;
11650 T1 := It1.Typ;
11651 It1 := Disambiguate (Operand, I1, I, Any_Type);
11653 if It1 = No_Interp then
11654 Conversion_Error_N
11655 ("ambiguous operand in conversion", Operand);
11657 -- If the interpretation involves a standard operator, use
11658 -- the location of the type, which may be user-defined.
11660 if Sloc (It.Nam) = Standard_Location then
11661 Error_Msg_Sloc := Sloc (It.Typ);
11662 else
11663 Error_Msg_Sloc := Sloc (It.Nam);
11664 end if;
11666 Conversion_Error_N -- CODEFIX
11667 ("\\possible interpretation#!", Operand);
11669 if Sloc (N1) = Standard_Location then
11670 Error_Msg_Sloc := Sloc (T1);
11671 else
11672 Error_Msg_Sloc := Sloc (N1);
11673 end if;
11675 Conversion_Error_N -- CODEFIX
11676 ("\\possible interpretation#!", Operand);
11678 return False;
11679 end if;
11680 end if;
11682 Set_Etype (Operand, It1.Typ);
11683 Opnd_Type := It1.Typ;
11684 end;
11685 end if;
11687 -- Deal with conversion of integer type to address if the pragma
11688 -- Allow_Integer_Address is in effect. We convert the conversion to
11689 -- an unchecked conversion in this case and we are all done.
11691 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
11692 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
11693 Analyze_And_Resolve (N, Target_Type);
11694 return True;
11695 end if;
11697 -- If we are within a child unit, check whether the type of the
11698 -- expression has an ancestor in a parent unit, in which case it
11699 -- belongs to its derivation class even if the ancestor is private.
11700 -- See RM 7.3.1 (5.2/3).
11702 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
11704 -- Numeric types
11706 if Is_Numeric_Type (Target_Type) then
11708 -- A universal fixed expression can be converted to any numeric type
11710 if Opnd_Type = Universal_Fixed then
11711 return True;
11713 -- Also no need to check when in an instance or inlined body, because
11714 -- the legality has been established when the template was analyzed.
11715 -- Furthermore, numeric conversions may occur where only a private
11716 -- view of the operand type is visible at the instantiation point.
11717 -- This results in a spurious error if we check that the operand type
11718 -- is a numeric type.
11720 -- Note: in a previous version of this unit, the following tests were
11721 -- applied only for generated code (Comes_From_Source set to False),
11722 -- but in fact the test is required for source code as well, since
11723 -- this situation can arise in source code.
11725 elsif In_Instance or else In_Inlined_Body then
11726 return True;
11728 -- Otherwise we need the conversion check
11730 else
11731 return Conversion_Check
11732 (Is_Numeric_Type (Opnd_Type)
11733 or else
11734 (Present (Inc_Ancestor)
11735 and then Is_Numeric_Type (Inc_Ancestor)),
11736 "illegal operand for numeric conversion");
11737 end if;
11739 -- Array types
11741 elsif Is_Array_Type (Target_Type) then
11742 if not Is_Array_Type (Opnd_Type)
11743 or else Opnd_Type = Any_Composite
11744 or else Opnd_Type = Any_String
11745 then
11746 Conversion_Error_N
11747 ("illegal operand for array conversion", Operand);
11748 return False;
11750 else
11751 return Valid_Array_Conversion;
11752 end if;
11754 -- Ada 2005 (AI-251): Anonymous access types where target references an
11755 -- interface type.
11757 elsif Ekind_In (Target_Type, E_General_Access_Type,
11758 E_Anonymous_Access_Type)
11759 and then Is_Interface (Directly_Designated_Type (Target_Type))
11760 then
11761 -- Check the static accessibility rule of 4.6(17). Note that the
11762 -- check is not enforced when within an instance body, since the
11763 -- RM requires such cases to be caught at run time.
11765 -- If the operand is a rewriting of an allocator no check is needed
11766 -- because there are no accessibility issues.
11768 if Nkind (Original_Node (N)) = N_Allocator then
11769 null;
11771 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
11772 if Type_Access_Level (Opnd_Type) >
11773 Deepest_Type_Access_Level (Target_Type)
11774 then
11775 -- In an instance, this is a run-time check, but one we know
11776 -- will fail, so generate an appropriate warning. The raise
11777 -- will be generated by Expand_N_Type_Conversion.
11779 if In_Instance_Body then
11780 Error_Msg_Warn := SPARK_Mode /= On;
11781 Conversion_Error_N
11782 ("cannot convert local pointer to non-local access type<<",
11783 Operand);
11784 Conversion_Error_N ("\Program_Error [<<", Operand);
11786 else
11787 Conversion_Error_N
11788 ("cannot convert local pointer to non-local access type",
11789 Operand);
11790 return False;
11791 end if;
11793 -- Special accessibility checks are needed in the case of access
11794 -- discriminants declared for a limited type.
11796 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11797 and then not Is_Local_Anonymous_Access (Opnd_Type)
11798 then
11799 -- When the operand is a selected access discriminant the check
11800 -- needs to be made against the level of the object denoted by
11801 -- the prefix of the selected name (Object_Access_Level handles
11802 -- checking the prefix of the operand for this case).
11804 if Nkind (Operand) = N_Selected_Component
11805 and then Object_Access_Level (Operand) >
11806 Deepest_Type_Access_Level (Target_Type)
11807 then
11808 -- In an instance, this is a run-time check, but one we know
11809 -- will fail, so generate an appropriate warning. The raise
11810 -- will be generated by Expand_N_Type_Conversion.
11812 if In_Instance_Body then
11813 Error_Msg_Warn := SPARK_Mode /= On;
11814 Conversion_Error_N
11815 ("cannot convert access discriminant to non-local "
11816 & "access type<<", Operand);
11817 Conversion_Error_N ("\Program_Error [<<", Operand);
11819 -- Real error if not in instance body
11821 else
11822 Conversion_Error_N
11823 ("cannot convert access discriminant to non-local "
11824 & "access type", Operand);
11825 return False;
11826 end if;
11827 end if;
11829 -- The case of a reference to an access discriminant from
11830 -- within a limited type declaration (which will appear as
11831 -- a discriminal) is always illegal because the level of the
11832 -- discriminant is considered to be deeper than any (nameable)
11833 -- access type.
11835 if Is_Entity_Name (Operand)
11836 and then not Is_Local_Anonymous_Access (Opnd_Type)
11837 and then
11838 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11839 and then Present (Discriminal_Link (Entity (Operand)))
11840 then
11841 Conversion_Error_N
11842 ("discriminant has deeper accessibility level than target",
11843 Operand);
11844 return False;
11845 end if;
11846 end if;
11847 end if;
11849 return True;
11851 -- General and anonymous access types
11853 elsif Ekind_In (Target_Type, E_General_Access_Type,
11854 E_Anonymous_Access_Type)
11855 and then
11856 Conversion_Check
11857 (Is_Access_Type (Opnd_Type)
11858 and then not
11859 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
11860 E_Access_Protected_Subprogram_Type),
11861 "must be an access-to-object type")
11862 then
11863 if Is_Access_Constant (Opnd_Type)
11864 and then not Is_Access_Constant (Target_Type)
11865 then
11866 Conversion_Error_N
11867 ("access-to-constant operand type not allowed", Operand);
11868 return False;
11869 end if;
11871 -- Check the static accessibility rule of 4.6(17). Note that the
11872 -- check is not enforced when within an instance body, since the RM
11873 -- requires such cases to be caught at run time.
11875 if Ekind (Target_Type) /= E_Anonymous_Access_Type
11876 or else Is_Local_Anonymous_Access (Target_Type)
11877 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
11878 N_Object_Declaration
11879 then
11880 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
11881 -- conversions from an anonymous access type to a named general
11882 -- access type. Such conversions are not allowed in the case of
11883 -- access parameters and stand-alone objects of an anonymous
11884 -- access type. The implicit conversion case is recognized by
11885 -- testing that Comes_From_Source is False and that it's been
11886 -- rewritten. The Comes_From_Source test isn't sufficient because
11887 -- nodes in inlined calls to predefined library routines can have
11888 -- Comes_From_Source set to False. (Is there a better way to test
11889 -- for implicit conversions???)
11891 if Ada_Version >= Ada_2012
11892 and then not Comes_From_Source (N)
11893 and then N /= Original_Node (N)
11894 and then Ekind (Target_Type) = E_General_Access_Type
11895 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
11896 then
11897 if Is_Itype (Opnd_Type) then
11899 -- Implicit conversions aren't allowed for objects of an
11900 -- anonymous access type, since such objects have nonstatic
11901 -- levels in Ada 2012.
11903 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
11904 N_Object_Declaration
11905 then
11906 Conversion_Error_N
11907 ("implicit conversion of stand-alone anonymous "
11908 & "access object not allowed", Operand);
11909 return False;
11911 -- Implicit conversions aren't allowed for anonymous access
11912 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
11913 -- is done to exclude anonymous access results.
11915 elsif not Is_Local_Anonymous_Access (Opnd_Type)
11916 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
11917 N_Function_Specification,
11918 N_Procedure_Specification)
11919 then
11920 Conversion_Error_N
11921 ("implicit conversion of anonymous access formal "
11922 & "not allowed", Operand);
11923 return False;
11925 -- This is a case where there's an enclosing object whose
11926 -- to which the "statically deeper than" relationship does
11927 -- not apply (such as an access discriminant selected from
11928 -- a dereference of an access parameter).
11930 elsif Object_Access_Level (Operand)
11931 = Scope_Depth (Standard_Standard)
11932 then
11933 Conversion_Error_N
11934 ("implicit conversion of anonymous access value "
11935 & "not allowed", Operand);
11936 return False;
11938 -- In other cases, the level of the operand's type must be
11939 -- statically less deep than that of the target type, else
11940 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
11942 elsif Type_Access_Level (Opnd_Type) >
11943 Deepest_Type_Access_Level (Target_Type)
11944 then
11945 Conversion_Error_N
11946 ("implicit conversion of anonymous access value "
11947 & "violates accessibility", Operand);
11948 return False;
11949 end if;
11950 end if;
11952 elsif Type_Access_Level (Opnd_Type) >
11953 Deepest_Type_Access_Level (Target_Type)
11954 then
11955 -- In an instance, this is a run-time check, but one we know
11956 -- will fail, so generate an appropriate warning. The raise
11957 -- will be generated by Expand_N_Type_Conversion.
11959 if In_Instance_Body then
11960 Error_Msg_Warn := SPARK_Mode /= On;
11961 Conversion_Error_N
11962 ("cannot convert local pointer to non-local access type<<",
11963 Operand);
11964 Conversion_Error_N ("\Program_Error [<<", Operand);
11966 -- If not in an instance body, this is a real error
11968 else
11969 -- Avoid generation of spurious error message
11971 if not Error_Posted (N) then
11972 Conversion_Error_N
11973 ("cannot convert local pointer to non-local access type",
11974 Operand);
11975 end if;
11977 return False;
11978 end if;
11980 -- Special accessibility checks are needed in the case of access
11981 -- discriminants declared for a limited type.
11983 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11984 and then not Is_Local_Anonymous_Access (Opnd_Type)
11985 then
11986 -- When the operand is a selected access discriminant the check
11987 -- needs to be made against the level of the object denoted by
11988 -- the prefix of the selected name (Object_Access_Level handles
11989 -- checking the prefix of the operand for this case).
11991 if Nkind (Operand) = N_Selected_Component
11992 and then Object_Access_Level (Operand) >
11993 Deepest_Type_Access_Level (Target_Type)
11994 then
11995 -- In an instance, this is a run-time check, but one we know
11996 -- will fail, so generate an appropriate warning. The raise
11997 -- will be generated by Expand_N_Type_Conversion.
11999 if In_Instance_Body then
12000 Error_Msg_Warn := SPARK_Mode /= On;
12001 Conversion_Error_N
12002 ("cannot convert access discriminant to non-local "
12003 & "access type<<", Operand);
12004 Conversion_Error_N ("\Program_Error [<<", Operand);
12006 -- If not in an instance body, this is a real error
12008 else
12009 Conversion_Error_N
12010 ("cannot convert access discriminant to non-local "
12011 & "access type", Operand);
12012 return False;
12013 end if;
12014 end if;
12016 -- The case of a reference to an access discriminant from
12017 -- within a limited type declaration (which will appear as
12018 -- a discriminal) is always illegal because the level of the
12019 -- discriminant is considered to be deeper than any (nameable)
12020 -- access type.
12022 if Is_Entity_Name (Operand)
12023 and then
12024 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
12025 and then Present (Discriminal_Link (Entity (Operand)))
12026 then
12027 Conversion_Error_N
12028 ("discriminant has deeper accessibility level than target",
12029 Operand);
12030 return False;
12031 end if;
12032 end if;
12033 end if;
12035 -- In the presence of limited_with clauses we have to use non-limited
12036 -- views, if available.
12038 Check_Limited : declare
12039 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
12040 -- Helper function to handle limited views
12042 --------------------------
12043 -- Full_Designated_Type --
12044 --------------------------
12046 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
12047 Desig : constant Entity_Id := Designated_Type (T);
12049 begin
12050 -- Handle the limited view of a type
12052 if Is_Incomplete_Type (Desig)
12053 and then From_Limited_With (Desig)
12054 and then Present (Non_Limited_View (Desig))
12055 then
12056 return Available_View (Desig);
12057 else
12058 return Desig;
12059 end if;
12060 end Full_Designated_Type;
12062 -- Local Declarations
12064 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
12065 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
12067 Same_Base : constant Boolean :=
12068 Base_Type (Target) = Base_Type (Opnd);
12070 -- Start of processing for Check_Limited
12072 begin
12073 if Is_Tagged_Type (Target) then
12074 return Valid_Tagged_Conversion (Target, Opnd);
12076 else
12077 if not Same_Base then
12078 Conversion_Error_NE
12079 ("target designated type not compatible with }",
12080 N, Base_Type (Opnd));
12081 return False;
12083 -- Ada 2005 AI-384: legality rule is symmetric in both
12084 -- designated types. The conversion is legal (with possible
12085 -- constraint check) if either designated type is
12086 -- unconstrained.
12088 elsif Subtypes_Statically_Match (Target, Opnd)
12089 or else
12090 (Has_Discriminants (Target)
12091 and then
12092 (not Is_Constrained (Opnd)
12093 or else not Is_Constrained (Target)))
12094 then
12095 -- Special case, if Value_Size has been used to make the
12096 -- sizes different, the conversion is not allowed even
12097 -- though the subtypes statically match.
12099 if Known_Static_RM_Size (Target)
12100 and then Known_Static_RM_Size (Opnd)
12101 and then RM_Size (Target) /= RM_Size (Opnd)
12102 then
12103 Conversion_Error_NE
12104 ("target designated subtype not compatible with }",
12105 N, Opnd);
12106 Conversion_Error_NE
12107 ("\because sizes of the two designated subtypes differ",
12108 N, Opnd);
12109 return False;
12111 -- Normal case where conversion is allowed
12113 else
12114 return True;
12115 end if;
12117 else
12118 Error_Msg_NE
12119 ("target designated subtype not compatible with }",
12120 N, Opnd);
12121 return False;
12122 end if;
12123 end if;
12124 end Check_Limited;
12126 -- Access to subprogram types. If the operand is an access parameter,
12127 -- the type has a deeper accessibility that any master, and cannot be
12128 -- assigned. We must make an exception if the conversion is part of an
12129 -- assignment and the target is the return object of an extended return
12130 -- statement, because in that case the accessibility check takes place
12131 -- after the return.
12133 elsif Is_Access_Subprogram_Type (Target_Type)
12135 -- Note: this test of Opnd_Type is there to prevent entering this
12136 -- branch in the case of a remote access to subprogram type, which
12137 -- is internally represented as an E_Record_Type.
12139 and then Is_Access_Type (Opnd_Type)
12140 then
12141 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
12142 and then Is_Entity_Name (Operand)
12143 and then Ekind (Entity (Operand)) = E_In_Parameter
12144 and then
12145 (Nkind (Parent (N)) /= N_Assignment_Statement
12146 or else not Is_Entity_Name (Name (Parent (N)))
12147 or else not Is_Return_Object (Entity (Name (Parent (N)))))
12148 then
12149 Conversion_Error_N
12150 ("illegal attempt to store anonymous access to subprogram",
12151 Operand);
12152 Conversion_Error_N
12153 ("\value has deeper accessibility than any master "
12154 & "(RM 3.10.2 (13))",
12155 Operand);
12157 Error_Msg_NE
12158 ("\use named access type for& instead of access parameter",
12159 Operand, Entity (Operand));
12160 end if;
12162 -- Check that the designated types are subtype conformant
12164 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
12165 Old_Id => Designated_Type (Opnd_Type),
12166 Err_Loc => N);
12168 -- Check the static accessibility rule of 4.6(20)
12170 if Type_Access_Level (Opnd_Type) >
12171 Deepest_Type_Access_Level (Target_Type)
12172 then
12173 Conversion_Error_N
12174 ("operand type has deeper accessibility level than target",
12175 Operand);
12177 -- Check that if the operand type is declared in a generic body,
12178 -- then the target type must be declared within that same body
12179 -- (enforces last sentence of 4.6(20)).
12181 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
12182 declare
12183 O_Gen : constant Node_Id :=
12184 Enclosing_Generic_Body (Opnd_Type);
12186 T_Gen : Node_Id;
12188 begin
12189 T_Gen := Enclosing_Generic_Body (Target_Type);
12190 while Present (T_Gen) and then T_Gen /= O_Gen loop
12191 T_Gen := Enclosing_Generic_Body (T_Gen);
12192 end loop;
12194 if T_Gen /= O_Gen then
12195 Conversion_Error_N
12196 ("target type must be declared in same generic body "
12197 & "as operand type", N);
12198 end if;
12199 end;
12200 end if;
12202 return True;
12204 -- Remote access to subprogram types
12206 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
12207 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
12208 then
12209 -- It is valid to convert from one RAS type to another provided
12210 -- that their specification statically match.
12212 -- Note: at this point, remote access to subprogram types have been
12213 -- expanded to their E_Record_Type representation, and we need to
12214 -- go back to the original access type definition using the
12215 -- Corresponding_Remote_Type attribute in order to check that the
12216 -- designated profiles match.
12218 pragma Assert (Ekind (Target_Type) = E_Record_Type);
12219 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
12221 Check_Subtype_Conformant
12222 (New_Id =>
12223 Designated_Type (Corresponding_Remote_Type (Target_Type)),
12224 Old_Id =>
12225 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
12226 Err_Loc =>
12228 return True;
12230 -- If it was legal in the generic, it's legal in the instance
12232 elsif In_Instance_Body then
12233 return True;
12235 -- If both are tagged types, check legality of view conversions
12237 elsif Is_Tagged_Type (Target_Type)
12238 and then
12239 Is_Tagged_Type (Opnd_Type)
12240 then
12241 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
12243 -- Types derived from the same root type are convertible
12245 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
12246 return True;
12248 -- In an instance or an inlined body, there may be inconsistent views of
12249 -- the same type, or of types derived from a common root.
12251 elsif (In_Instance or In_Inlined_Body)
12252 and then
12253 Root_Type (Underlying_Type (Target_Type)) =
12254 Root_Type (Underlying_Type (Opnd_Type))
12255 then
12256 return True;
12258 -- Special check for common access type error case
12260 elsif Ekind (Target_Type) = E_Access_Type
12261 and then Is_Access_Type (Opnd_Type)
12262 then
12263 Conversion_Error_N ("target type must be general access type!", N);
12264 Conversion_Error_NE -- CODEFIX
12265 ("add ALL to }!", N, Target_Type);
12266 return False;
12268 -- Here we have a real conversion error
12270 else
12271 Conversion_Error_NE
12272 ("invalid conversion, not compatible with }", N, Opnd_Type);
12273 return False;
12274 end if;
12275 end Valid_Conversion;
12277 end Sem_Res;