2014-10-31 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / sem_res.adb
blob97f6ea12c3860ce26d1b818a3f6ca73ce08c2710
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 4.1.3 (13 1/2))",
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 -- A formal parameter of a specific tagged type whose related
4422 -- subprogram is subject to pragma Extensions_Visible with value
4423 -- "False" cannot act as an actual in a subprogram with value
4424 -- "True" (SPARK RM 6.1.7(3)).
4426 if Is_EVF_Expression (A)
4427 and then Extensions_Visible_Status (Nam) =
4428 Extensions_Visible_True
4429 then
4430 Error_Msg_N
4431 ("formal parameter with Extensions_Visible False cannot act "
4432 & "as actual parameter", A);
4433 Error_Msg_NE
4434 ("\subprogram & has Extensions_Visible True", A, Nam);
4435 end if;
4437 Next_Actual (A);
4439 -- Case where actual is not present
4441 else
4442 Insert_Default;
4443 end if;
4445 Next_Formal (F);
4446 end loop;
4447 end Resolve_Actuals;
4449 -----------------------
4450 -- Resolve_Allocator --
4451 -----------------------
4453 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4454 Desig_T : constant Entity_Id := Designated_Type (Typ);
4455 E : constant Node_Id := Expression (N);
4456 Subtyp : Entity_Id;
4457 Discrim : Entity_Id;
4458 Constr : Node_Id;
4459 Aggr : Node_Id;
4460 Assoc : Node_Id := Empty;
4461 Disc_Exp : Node_Id;
4463 procedure Check_Allocator_Discrim_Accessibility
4464 (Disc_Exp : Node_Id;
4465 Alloc_Typ : Entity_Id);
4466 -- Check that accessibility level associated with an access discriminant
4467 -- initialized in an allocator by the expression Disc_Exp is not deeper
4468 -- than the level of the allocator type Alloc_Typ. An error message is
4469 -- issued if this condition is violated. Specialized checks are done for
4470 -- the cases of a constraint expression which is an access attribute or
4471 -- an access discriminant.
4473 function In_Dispatching_Context return Boolean;
4474 -- If the allocator is an actual in a call, it is allowed to be class-
4475 -- wide when the context is not because it is a controlling actual.
4477 -------------------------------------------
4478 -- Check_Allocator_Discrim_Accessibility --
4479 -------------------------------------------
4481 procedure Check_Allocator_Discrim_Accessibility
4482 (Disc_Exp : Node_Id;
4483 Alloc_Typ : Entity_Id)
4485 begin
4486 if Type_Access_Level (Etype (Disc_Exp)) >
4487 Deepest_Type_Access_Level (Alloc_Typ)
4488 then
4489 Error_Msg_N
4490 ("operand type has deeper level than allocator type", Disc_Exp);
4492 -- When the expression is an Access attribute the level of the prefix
4493 -- object must not be deeper than that of the allocator's type.
4495 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4496 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4497 Attribute_Access
4498 and then Object_Access_Level (Prefix (Disc_Exp)) >
4499 Deepest_Type_Access_Level (Alloc_Typ)
4500 then
4501 Error_Msg_N
4502 ("prefix of attribute has deeper level than allocator type",
4503 Disc_Exp);
4505 -- When the expression is an access discriminant the check is against
4506 -- the level of the prefix object.
4508 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4509 and then Nkind (Disc_Exp) = N_Selected_Component
4510 and then Object_Access_Level (Prefix (Disc_Exp)) >
4511 Deepest_Type_Access_Level (Alloc_Typ)
4512 then
4513 Error_Msg_N
4514 ("access discriminant has deeper level than allocator type",
4515 Disc_Exp);
4517 -- All other cases are legal
4519 else
4520 null;
4521 end if;
4522 end Check_Allocator_Discrim_Accessibility;
4524 ----------------------------
4525 -- In_Dispatching_Context --
4526 ----------------------------
4528 function In_Dispatching_Context return Boolean is
4529 Par : constant Node_Id := Parent (N);
4531 begin
4532 return Nkind (Par) in N_Subprogram_Call
4533 and then Is_Entity_Name (Name (Par))
4534 and then Is_Dispatching_Operation (Entity (Name (Par)));
4535 end In_Dispatching_Context;
4537 -- Start of processing for Resolve_Allocator
4539 begin
4540 -- Replace general access with specific type
4542 if Ekind (Etype (N)) = E_Allocator_Type then
4543 Set_Etype (N, Base_Type (Typ));
4544 end if;
4546 if Is_Abstract_Type (Typ) then
4547 Error_Msg_N ("type of allocator cannot be abstract", N);
4548 end if;
4550 -- For qualified expression, resolve the expression using the given
4551 -- subtype (nothing to do for type mark, subtype indication)
4553 if Nkind (E) = N_Qualified_Expression then
4554 if Is_Class_Wide_Type (Etype (E))
4555 and then not Is_Class_Wide_Type (Desig_T)
4556 and then not In_Dispatching_Context
4557 then
4558 Error_Msg_N
4559 ("class-wide allocator not allowed for this access type", N);
4560 end if;
4562 Resolve (Expression (E), Etype (E));
4563 Check_Non_Static_Context (Expression (E));
4564 Check_Unset_Reference (Expression (E));
4566 -- A qualified expression requires an exact match of the type.
4567 -- Class-wide matching is not allowed.
4569 if (Is_Class_Wide_Type (Etype (Expression (E)))
4570 or else Is_Class_Wide_Type (Etype (E)))
4571 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4572 then
4573 Wrong_Type (Expression (E), Etype (E));
4574 end if;
4576 -- Calls to build-in-place functions are not currently supported in
4577 -- allocators for access types associated with a simple storage pool.
4578 -- Supporting such allocators may require passing additional implicit
4579 -- parameters to build-in-place functions (or a significant revision
4580 -- of the current b-i-p implementation to unify the handling for
4581 -- multiple kinds of storage pools). ???
4583 if Is_Limited_View (Desig_T)
4584 and then Nkind (Expression (E)) = N_Function_Call
4585 then
4586 declare
4587 Pool : constant Entity_Id :=
4588 Associated_Storage_Pool (Root_Type (Typ));
4589 begin
4590 if Present (Pool)
4591 and then
4592 Present (Get_Rep_Pragma
4593 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4594 then
4595 Error_Msg_N
4596 ("limited function calls not yet supported in simple "
4597 & "storage pool allocators", Expression (E));
4598 end if;
4599 end;
4600 end if;
4602 -- A special accessibility check is needed for allocators that
4603 -- constrain access discriminants. The level of the type of the
4604 -- expression used to constrain an access discriminant cannot be
4605 -- deeper than the type of the allocator (in contrast to access
4606 -- parameters, where the level of the actual can be arbitrary).
4608 -- We can't use Valid_Conversion to perform this check because in
4609 -- general the type of the allocator is unrelated to the type of
4610 -- the access discriminant.
4612 if Ekind (Typ) /= E_Anonymous_Access_Type
4613 or else Is_Local_Anonymous_Access (Typ)
4614 then
4615 Subtyp := Entity (Subtype_Mark (E));
4617 Aggr := Original_Node (Expression (E));
4619 if Has_Discriminants (Subtyp)
4620 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4621 then
4622 Discrim := First_Discriminant (Base_Type (Subtyp));
4624 -- Get the first component expression of the aggregate
4626 if Present (Expressions (Aggr)) then
4627 Disc_Exp := First (Expressions (Aggr));
4629 elsif Present (Component_Associations (Aggr)) then
4630 Assoc := First (Component_Associations (Aggr));
4632 if Present (Assoc) then
4633 Disc_Exp := Expression (Assoc);
4634 else
4635 Disc_Exp := Empty;
4636 end if;
4638 else
4639 Disc_Exp := Empty;
4640 end if;
4642 while Present (Discrim) and then Present (Disc_Exp) loop
4643 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4644 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4645 end if;
4647 Next_Discriminant (Discrim);
4649 if Present (Discrim) then
4650 if Present (Assoc) then
4651 Next (Assoc);
4652 Disc_Exp := Expression (Assoc);
4654 elsif Present (Next (Disc_Exp)) then
4655 Next (Disc_Exp);
4657 else
4658 Assoc := First (Component_Associations (Aggr));
4660 if Present (Assoc) then
4661 Disc_Exp := Expression (Assoc);
4662 else
4663 Disc_Exp := Empty;
4664 end if;
4665 end if;
4666 end if;
4667 end loop;
4668 end if;
4669 end if;
4671 -- For a subtype mark or subtype indication, freeze the subtype
4673 else
4674 Freeze_Expression (E);
4676 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4677 Error_Msg_N
4678 ("initialization required for access-to-constant allocator", N);
4679 end if;
4681 -- A special accessibility check is needed for allocators that
4682 -- constrain access discriminants. The level of the type of the
4683 -- expression used to constrain an access discriminant cannot be
4684 -- deeper than the type of the allocator (in contrast to access
4685 -- parameters, where the level of the actual can be arbitrary).
4686 -- We can't use Valid_Conversion to perform this check because
4687 -- in general the type of the allocator is unrelated to the type
4688 -- of the access discriminant.
4690 if Nkind (Original_Node (E)) = N_Subtype_Indication
4691 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4692 or else Is_Local_Anonymous_Access (Typ))
4693 then
4694 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4696 if Has_Discriminants (Subtyp) then
4697 Discrim := First_Discriminant (Base_Type (Subtyp));
4698 Constr := First (Constraints (Constraint (Original_Node (E))));
4699 while Present (Discrim) and then Present (Constr) loop
4700 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4701 if Nkind (Constr) = N_Discriminant_Association then
4702 Disc_Exp := Original_Node (Expression (Constr));
4703 else
4704 Disc_Exp := Original_Node (Constr);
4705 end if;
4707 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4708 end if;
4710 Next_Discriminant (Discrim);
4711 Next (Constr);
4712 end loop;
4713 end if;
4714 end if;
4715 end if;
4717 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4718 -- check that the level of the type of the created object is not deeper
4719 -- than the level of the allocator's access type, since extensions can
4720 -- now occur at deeper levels than their ancestor types. This is a
4721 -- static accessibility level check; a run-time check is also needed in
4722 -- the case of an initialized allocator with a class-wide argument (see
4723 -- Expand_Allocator_Expression).
4725 if Ada_Version >= Ada_2005
4726 and then Is_Class_Wide_Type (Desig_T)
4727 then
4728 declare
4729 Exp_Typ : Entity_Id;
4731 begin
4732 if Nkind (E) = N_Qualified_Expression then
4733 Exp_Typ := Etype (E);
4734 elsif Nkind (E) = N_Subtype_Indication then
4735 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4736 else
4737 Exp_Typ := Entity (E);
4738 end if;
4740 if Type_Access_Level (Exp_Typ) >
4741 Deepest_Type_Access_Level (Typ)
4742 then
4743 if In_Instance_Body then
4744 Error_Msg_Warn := SPARK_Mode /= On;
4745 Error_Msg_N
4746 ("type in allocator has deeper level than "
4747 & "designated class-wide type<<", E);
4748 Error_Msg_N ("\Program_Error [<<", E);
4749 Rewrite (N,
4750 Make_Raise_Program_Error (Sloc (N),
4751 Reason => PE_Accessibility_Check_Failed));
4752 Set_Etype (N, Typ);
4754 -- Do not apply Ada 2005 accessibility checks on a class-wide
4755 -- allocator if the type given in the allocator is a formal
4756 -- type. A run-time check will be performed in the instance.
4758 elsif not Is_Generic_Type (Exp_Typ) then
4759 Error_Msg_N ("type in allocator has deeper level than "
4760 & "designated class-wide type", E);
4761 end if;
4762 end if;
4763 end;
4764 end if;
4766 -- Check for allocation from an empty storage pool
4768 if No_Pool_Assigned (Typ) then
4769 Error_Msg_N ("allocation from empty storage pool!", N);
4771 -- If the context is an unchecked conversion, as may happen within an
4772 -- inlined subprogram, the allocator is being resolved with its own
4773 -- anonymous type. In that case, if the target type has a specific
4774 -- storage pool, it must be inherited explicitly by the allocator type.
4776 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4777 and then No (Associated_Storage_Pool (Typ))
4778 then
4779 Set_Associated_Storage_Pool
4780 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4781 end if;
4783 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4784 Check_Restriction (No_Anonymous_Allocators, N);
4785 end if;
4787 -- Check that an allocator with task parts isn't for a nested access
4788 -- type when restriction No_Task_Hierarchy applies.
4790 if not Is_Library_Level_Entity (Base_Type (Typ))
4791 and then Has_Task (Base_Type (Desig_T))
4792 then
4793 Check_Restriction (No_Task_Hierarchy, N);
4794 end if;
4796 -- An illegal allocator may be rewritten as a raise Program_Error
4797 -- statement.
4799 if Nkind (N) = N_Allocator then
4801 -- An anonymous access discriminant is the definition of a
4802 -- coextension.
4804 if Ekind (Typ) = E_Anonymous_Access_Type
4805 and then Nkind (Associated_Node_For_Itype (Typ)) =
4806 N_Discriminant_Specification
4807 then
4808 declare
4809 Discr : constant Entity_Id :=
4810 Defining_Identifier (Associated_Node_For_Itype (Typ));
4812 begin
4813 Check_Restriction (No_Coextensions, N);
4815 -- Ada 2012 AI05-0052: If the designated type of the allocator
4816 -- is limited, then the allocator shall not be used to define
4817 -- the value of an access discriminant unless the discriminated
4818 -- type is immutably limited.
4820 if Ada_Version >= Ada_2012
4821 and then Is_Limited_Type (Desig_T)
4822 and then not Is_Limited_View (Scope (Discr))
4823 then
4824 Error_Msg_N
4825 ("only immutably limited types can have anonymous "
4826 & "access discriminants designating a limited type", N);
4827 end if;
4828 end;
4830 -- Avoid marking an allocator as a dynamic coextension if it is
4831 -- within a static construct.
4833 if not Is_Static_Coextension (N) then
4834 Set_Is_Dynamic_Coextension (N);
4835 end if;
4837 -- Cleanup for potential static coextensions
4839 else
4840 Set_Is_Dynamic_Coextension (N, False);
4841 Set_Is_Static_Coextension (N, False);
4842 end if;
4843 end if;
4845 -- Report a simple error: if the designated object is a local task,
4846 -- its body has not been seen yet, and its activation will fail an
4847 -- elaboration check.
4849 if Is_Task_Type (Desig_T)
4850 and then Scope (Base_Type (Desig_T)) = Current_Scope
4851 and then Is_Compilation_Unit (Current_Scope)
4852 and then Ekind (Current_Scope) = E_Package
4853 and then not In_Package_Body (Current_Scope)
4854 then
4855 Error_Msg_Warn := SPARK_Mode /= On;
4856 Error_Msg_N ("cannot activate task before body seen<<", N);
4857 Error_Msg_N ("\Program_Error [<<", N);
4858 end if;
4860 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4861 -- type with a task component on a subpool. This action must raise
4862 -- Program_Error at runtime.
4864 if Ada_Version >= Ada_2012
4865 and then Nkind (N) = N_Allocator
4866 and then Present (Subpool_Handle_Name (N))
4867 and then Has_Task (Desig_T)
4868 then
4869 Error_Msg_Warn := SPARK_Mode /= On;
4870 Error_Msg_N ("cannot allocate task on subpool<<", N);
4871 Error_Msg_N ("\Program_Error [<<", N);
4873 Rewrite (N,
4874 Make_Raise_Program_Error (Sloc (N),
4875 Reason => PE_Explicit_Raise));
4876 Set_Etype (N, Typ);
4877 end if;
4878 end Resolve_Allocator;
4880 ---------------------------
4881 -- Resolve_Arithmetic_Op --
4882 ---------------------------
4884 -- Used for resolving all arithmetic operators except exponentiation
4886 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4887 L : constant Node_Id := Left_Opnd (N);
4888 R : constant Node_Id := Right_Opnd (N);
4889 TL : constant Entity_Id := Base_Type (Etype (L));
4890 TR : constant Entity_Id := Base_Type (Etype (R));
4891 T : Entity_Id;
4892 Rop : Node_Id;
4894 B_Typ : constant Entity_Id := Base_Type (Typ);
4895 -- We do the resolution using the base type, because intermediate values
4896 -- in expressions always are of the base type, not a subtype of it.
4898 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4899 -- Returns True if N is in a context that expects "any real type"
4901 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4902 -- Return True iff given type is Integer or universal real/integer
4904 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4905 -- Choose type of integer literal in fixed-point operation to conform
4906 -- to available fixed-point type. T is the type of the other operand,
4907 -- which is needed to determine the expected type of N.
4909 procedure Set_Operand_Type (N : Node_Id);
4910 -- Set operand type to T if universal
4912 -------------------------------
4913 -- Expected_Type_Is_Any_Real --
4914 -------------------------------
4916 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4917 begin
4918 -- N is the expression after "delta" in a fixed_point_definition;
4919 -- see RM-3.5.9(6):
4921 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4922 N_Decimal_Fixed_Point_Definition,
4924 -- N is one of the bounds in a real_range_specification;
4925 -- see RM-3.5.7(5):
4927 N_Real_Range_Specification,
4929 -- N is the expression of a delta_constraint;
4930 -- see RM-J.3(3):
4932 N_Delta_Constraint);
4933 end Expected_Type_Is_Any_Real;
4935 -----------------------------
4936 -- Is_Integer_Or_Universal --
4937 -----------------------------
4939 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4940 T : Entity_Id;
4941 Index : Interp_Index;
4942 It : Interp;
4944 begin
4945 if not Is_Overloaded (N) then
4946 T := Etype (N);
4947 return Base_Type (T) = Base_Type (Standard_Integer)
4948 or else T = Universal_Integer
4949 or else T = Universal_Real;
4950 else
4951 Get_First_Interp (N, Index, It);
4952 while Present (It.Typ) loop
4953 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4954 or else It.Typ = Universal_Integer
4955 or else It.Typ = Universal_Real
4956 then
4957 return True;
4958 end if;
4960 Get_Next_Interp (Index, It);
4961 end loop;
4962 end if;
4964 return False;
4965 end Is_Integer_Or_Universal;
4967 ----------------------------
4968 -- Set_Mixed_Mode_Operand --
4969 ----------------------------
4971 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4972 Index : Interp_Index;
4973 It : Interp;
4975 begin
4976 if Universal_Interpretation (N) = Universal_Integer then
4978 -- A universal integer literal is resolved as standard integer
4979 -- except in the case of a fixed-point result, where we leave it
4980 -- as universal (to be handled by Exp_Fixd later on)
4982 if Is_Fixed_Point_Type (T) then
4983 Resolve (N, Universal_Integer);
4984 else
4985 Resolve (N, Standard_Integer);
4986 end if;
4988 elsif Universal_Interpretation (N) = Universal_Real
4989 and then (T = Base_Type (Standard_Integer)
4990 or else T = Universal_Integer
4991 or else T = Universal_Real)
4992 then
4993 -- A universal real can appear in a fixed-type context. We resolve
4994 -- the literal with that context, even though this might raise an
4995 -- exception prematurely (the other operand may be zero).
4997 Resolve (N, B_Typ);
4999 elsif Etype (N) = Base_Type (Standard_Integer)
5000 and then T = Universal_Real
5001 and then Is_Overloaded (N)
5002 then
5003 -- Integer arg in mixed-mode operation. Resolve with universal
5004 -- type, in case preference rule must be applied.
5006 Resolve (N, Universal_Integer);
5008 elsif Etype (N) = T
5009 and then B_Typ /= Universal_Fixed
5010 then
5011 -- Not a mixed-mode operation, resolve with context
5013 Resolve (N, B_Typ);
5015 elsif Etype (N) = Any_Fixed then
5017 -- N may itself be a mixed-mode operation, so use context type
5019 Resolve (N, B_Typ);
5021 elsif Is_Fixed_Point_Type (T)
5022 and then B_Typ = Universal_Fixed
5023 and then Is_Overloaded (N)
5024 then
5025 -- Must be (fixed * fixed) operation, operand must have one
5026 -- compatible interpretation.
5028 Resolve (N, Any_Fixed);
5030 elsif Is_Fixed_Point_Type (B_Typ)
5031 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5032 and then Is_Overloaded (N)
5033 then
5034 -- C * F(X) in a fixed context, where C is a real literal or a
5035 -- fixed-point expression. F must have either a fixed type
5036 -- interpretation or an integer interpretation, but not both.
5038 Get_First_Interp (N, Index, It);
5039 while Present (It.Typ) loop
5040 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5041 if Analyzed (N) then
5042 Error_Msg_N ("ambiguous operand in fixed operation", N);
5043 else
5044 Resolve (N, Standard_Integer);
5045 end if;
5047 elsif Is_Fixed_Point_Type (It.Typ) then
5048 if Analyzed (N) then
5049 Error_Msg_N ("ambiguous operand in fixed operation", N);
5050 else
5051 Resolve (N, It.Typ);
5052 end if;
5053 end if;
5055 Get_Next_Interp (Index, It);
5056 end loop;
5058 -- Reanalyze the literal with the fixed type of the context. If
5059 -- context is Universal_Fixed, we are within a conversion, leave
5060 -- the literal as a universal real because there is no usable
5061 -- fixed type, and the target of the conversion plays no role in
5062 -- the resolution.
5064 declare
5065 Op2 : Node_Id;
5066 T2 : Entity_Id;
5068 begin
5069 if N = L then
5070 Op2 := R;
5071 else
5072 Op2 := L;
5073 end if;
5075 if B_Typ = Universal_Fixed
5076 and then Nkind (Op2) = N_Real_Literal
5077 then
5078 T2 := Universal_Real;
5079 else
5080 T2 := B_Typ;
5081 end if;
5083 Set_Analyzed (Op2, False);
5084 Resolve (Op2, T2);
5085 end;
5087 else
5088 Resolve (N);
5089 end if;
5090 end Set_Mixed_Mode_Operand;
5092 ----------------------
5093 -- Set_Operand_Type --
5094 ----------------------
5096 procedure Set_Operand_Type (N : Node_Id) is
5097 begin
5098 if Etype (N) = Universal_Integer
5099 or else Etype (N) = Universal_Real
5100 then
5101 Set_Etype (N, T);
5102 end if;
5103 end Set_Operand_Type;
5105 -- Start of processing for Resolve_Arithmetic_Op
5107 begin
5108 if Comes_From_Source (N)
5109 and then Ekind (Entity (N)) = E_Function
5110 and then Is_Imported (Entity (N))
5111 and then Is_Intrinsic_Subprogram (Entity (N))
5112 then
5113 Resolve_Intrinsic_Operator (N, Typ);
5114 return;
5116 -- Special-case for mixed-mode universal expressions or fixed point type
5117 -- operation: each argument is resolved separately. The same treatment
5118 -- is required if one of the operands of a fixed point operation is
5119 -- universal real, since in this case we don't do a conversion to a
5120 -- specific fixed-point type (instead the expander handles the case).
5122 -- Set the type of the node to its universal interpretation because
5123 -- legality checks on an exponentiation operand need the context.
5125 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
5126 and then Present (Universal_Interpretation (L))
5127 and then Present (Universal_Interpretation (R))
5128 then
5129 Set_Etype (N, B_Typ);
5130 Resolve (L, Universal_Interpretation (L));
5131 Resolve (R, Universal_Interpretation (R));
5133 elsif (B_Typ = Universal_Real
5134 or else Etype (N) = Universal_Fixed
5135 or else (Etype (N) = Any_Fixed
5136 and then Is_Fixed_Point_Type (B_Typ))
5137 or else (Is_Fixed_Point_Type (B_Typ)
5138 and then (Is_Integer_Or_Universal (L)
5139 or else
5140 Is_Integer_Or_Universal (R))))
5141 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5142 then
5143 if TL = Universal_Integer or else TR = Universal_Integer then
5144 Check_For_Visible_Operator (N, B_Typ);
5145 end if;
5147 -- If context is a fixed type and one operand is integer, the other
5148 -- is resolved with the type of the context.
5150 if Is_Fixed_Point_Type (B_Typ)
5151 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5152 or else TL = Universal_Integer)
5153 then
5154 Resolve (R, B_Typ);
5155 Resolve (L, TL);
5157 elsif Is_Fixed_Point_Type (B_Typ)
5158 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5159 or else TR = Universal_Integer)
5160 then
5161 Resolve (L, B_Typ);
5162 Resolve (R, TR);
5164 else
5165 Set_Mixed_Mode_Operand (L, TR);
5166 Set_Mixed_Mode_Operand (R, TL);
5167 end if;
5169 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
5170 -- multiplying operators from being used when the expected type is
5171 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
5172 -- some cases where the expected type is actually Any_Real;
5173 -- Expected_Type_Is_Any_Real takes care of that case.
5175 if Etype (N) = Universal_Fixed
5176 or else Etype (N) = Any_Fixed
5177 then
5178 if B_Typ = Universal_Fixed
5179 and then not Expected_Type_Is_Any_Real (N)
5180 and then not Nkind_In (Parent (N), N_Type_Conversion,
5181 N_Unchecked_Type_Conversion)
5182 then
5183 Error_Msg_N ("type cannot be determined from context!", N);
5184 Error_Msg_N ("\explicit conversion to result type required", N);
5186 Set_Etype (L, Any_Type);
5187 Set_Etype (R, Any_Type);
5189 else
5190 if Ada_Version = Ada_83
5191 and then Etype (N) = Universal_Fixed
5192 and then not
5193 Nkind_In (Parent (N), N_Type_Conversion,
5194 N_Unchecked_Type_Conversion)
5195 then
5196 Error_Msg_N
5197 ("(Ada 83) fixed-point operation "
5198 & "needs explicit conversion", N);
5199 end if;
5201 -- The expected type is "any real type" in contexts like
5203 -- type T is delta <universal_fixed-expression> ...
5205 -- in which case we need to set the type to Universal_Real
5206 -- so that static expression evaluation will work properly.
5208 if Expected_Type_Is_Any_Real (N) then
5209 Set_Etype (N, Universal_Real);
5210 else
5211 Set_Etype (N, B_Typ);
5212 end if;
5213 end if;
5215 elsif Is_Fixed_Point_Type (B_Typ)
5216 and then (Is_Integer_Or_Universal (L)
5217 or else Nkind (L) = N_Real_Literal
5218 or else Nkind (R) = N_Real_Literal
5219 or else Is_Integer_Or_Universal (R))
5220 then
5221 Set_Etype (N, B_Typ);
5223 elsif Etype (N) = Any_Fixed then
5225 -- If no previous errors, this is only possible if one operand is
5226 -- overloaded and the context is universal. Resolve as such.
5228 Set_Etype (N, B_Typ);
5229 end if;
5231 else
5232 if (TL = Universal_Integer or else TL = Universal_Real)
5233 and then
5234 (TR = Universal_Integer or else TR = Universal_Real)
5235 then
5236 Check_For_Visible_Operator (N, B_Typ);
5237 end if;
5239 -- If the context is Universal_Fixed and the operands are also
5240 -- universal fixed, this is an error, unless there is only one
5241 -- applicable fixed_point type (usually Duration).
5243 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
5244 T := Unique_Fixed_Point_Type (N);
5246 if T = Any_Type then
5247 Set_Etype (N, T);
5248 return;
5249 else
5250 Resolve (L, T);
5251 Resolve (R, T);
5252 end if;
5254 else
5255 Resolve (L, B_Typ);
5256 Resolve (R, B_Typ);
5257 end if;
5259 -- If one of the arguments was resolved to a non-universal type.
5260 -- label the result of the operation itself with the same type.
5261 -- Do the same for the universal argument, if any.
5263 T := Intersect_Types (L, R);
5264 Set_Etype (N, Base_Type (T));
5265 Set_Operand_Type (L);
5266 Set_Operand_Type (R);
5267 end if;
5269 Generate_Operator_Reference (N, Typ);
5270 Analyze_Dimension (N);
5271 Eval_Arithmetic_Op (N);
5273 -- In SPARK, a multiplication or division with operands of fixed point
5274 -- types must be qualified or explicitly converted to identify the
5275 -- result type.
5277 if (Is_Fixed_Point_Type (Etype (L))
5278 or else Is_Fixed_Point_Type (Etype (R)))
5279 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5280 and then
5281 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
5282 then
5283 Check_SPARK_05_Restriction
5284 ("operation should be qualified or explicitly converted", N);
5285 end if;
5287 -- Set overflow and division checking bit
5289 if Nkind (N) in N_Op then
5290 if not Overflow_Checks_Suppressed (Etype (N)) then
5291 Enable_Overflow_Check (N);
5292 end if;
5294 -- Give warning if explicit division by zero
5296 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
5297 and then not Division_Checks_Suppressed (Etype (N))
5298 then
5299 Rop := Right_Opnd (N);
5301 if Compile_Time_Known_Value (Rop)
5302 and then ((Is_Integer_Type (Etype (Rop))
5303 and then Expr_Value (Rop) = Uint_0)
5304 or else
5305 (Is_Real_Type (Etype (Rop))
5306 and then Expr_Value_R (Rop) = Ureal_0))
5307 then
5308 -- Specialize the warning message according to the operation.
5309 -- The following warnings are for the case
5311 case Nkind (N) is
5312 when N_Op_Divide =>
5314 -- For division, we have two cases, for float division
5315 -- of an unconstrained float type, on a machine where
5316 -- Machine_Overflows is false, we don't get an exception
5317 -- at run-time, but rather an infinity or Nan. The Nan
5318 -- case is pretty obscure, so just warn about infinities.
5320 if Is_Floating_Point_Type (Typ)
5321 and then not Is_Constrained (Typ)
5322 and then not Machine_Overflows_On_Target
5323 then
5324 Error_Msg_N
5325 ("float division by zero, may generate "
5326 & "'+'/'- infinity??", Right_Opnd (N));
5328 -- For all other cases, we get a Constraint_Error
5330 else
5331 Apply_Compile_Time_Constraint_Error
5332 (N, "division by zero??", CE_Divide_By_Zero,
5333 Loc => Sloc (Right_Opnd (N)));
5334 end if;
5336 when N_Op_Rem =>
5337 Apply_Compile_Time_Constraint_Error
5338 (N, "rem with zero divisor??", CE_Divide_By_Zero,
5339 Loc => Sloc (Right_Opnd (N)));
5341 when N_Op_Mod =>
5342 Apply_Compile_Time_Constraint_Error
5343 (N, "mod with zero divisor??", CE_Divide_By_Zero,
5344 Loc => Sloc (Right_Opnd (N)));
5346 -- Division by zero can only happen with division, rem,
5347 -- and mod operations.
5349 when others =>
5350 raise Program_Error;
5351 end case;
5353 -- Otherwise just set the flag to check at run time
5355 else
5356 Activate_Division_Check (N);
5357 end if;
5358 end if;
5360 -- If Restriction No_Implicit_Conditionals is active, then it is
5361 -- violated if either operand can be negative for mod, or for rem
5362 -- if both operands can be negative.
5364 if Restriction_Check_Required (No_Implicit_Conditionals)
5365 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5366 then
5367 declare
5368 Lo : Uint;
5369 Hi : Uint;
5370 OK : Boolean;
5372 LNeg : Boolean;
5373 RNeg : Boolean;
5374 -- Set if corresponding operand might be negative
5376 begin
5377 Determine_Range
5378 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5379 LNeg := (not OK) or else Lo < 0;
5381 Determine_Range
5382 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5383 RNeg := (not OK) or else Lo < 0;
5385 -- Check if we will be generating conditionals. There are two
5386 -- cases where that can happen, first for REM, the only case
5387 -- is largest negative integer mod -1, where the division can
5388 -- overflow, but we still have to give the right result. The
5389 -- front end generates a test for this annoying case. Here we
5390 -- just test if both operands can be negative (that's what the
5391 -- expander does, so we match its logic here).
5393 -- The second case is mod where either operand can be negative.
5394 -- In this case, the back end has to generate additional tests.
5396 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5397 or else
5398 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5399 then
5400 Check_Restriction (No_Implicit_Conditionals, N);
5401 end if;
5402 end;
5403 end if;
5404 end if;
5406 Check_Unset_Reference (L);
5407 Check_Unset_Reference (R);
5408 Check_Function_Writable_Actuals (N);
5409 end Resolve_Arithmetic_Op;
5411 ------------------
5412 -- Resolve_Call --
5413 ------------------
5415 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5416 function Same_Or_Aliased_Subprograms
5417 (S : Entity_Id;
5418 E : Entity_Id) return Boolean;
5419 -- Returns True if the subprogram entity S is the same as E or else
5420 -- S is an alias of E.
5422 ---------------------------------
5423 -- Same_Or_Aliased_Subprograms --
5424 ---------------------------------
5426 function Same_Or_Aliased_Subprograms
5427 (S : Entity_Id;
5428 E : Entity_Id) return Boolean
5430 Subp_Alias : constant Entity_Id := Alias (S);
5431 begin
5432 return S = E or else (Present (Subp_Alias) and then Subp_Alias = E);
5433 end Same_Or_Aliased_Subprograms;
5435 -- Local variables
5437 Loc : constant Source_Ptr := Sloc (N);
5438 Subp : constant Node_Id := Name (N);
5439 Body_Id : Entity_Id;
5440 I : Interp_Index;
5441 It : Interp;
5442 Nam : Entity_Id;
5443 Nam_Decl : Node_Id;
5444 Nam_UA : Entity_Id;
5445 Norm_OK : Boolean;
5446 Rtype : Entity_Id;
5447 Scop : Entity_Id;
5449 -- Start of processing for Resolve_Call
5451 begin
5452 -- The context imposes a unique interpretation with type Typ on a
5453 -- procedure or function call. Find the entity of the subprogram that
5454 -- yields the expected type, and propagate the corresponding formal
5455 -- constraints on the actuals. The caller has established that an
5456 -- interpretation exists, and emitted an error if not unique.
5458 -- First deal with the case of a call to an access-to-subprogram,
5459 -- dereference made explicit in Analyze_Call.
5461 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5462 if not Is_Overloaded (Subp) then
5463 Nam := Etype (Subp);
5465 else
5466 -- Find the interpretation whose type (a subprogram type) has a
5467 -- return type that is compatible with the context. Analysis of
5468 -- the node has established that one exists.
5470 Nam := Empty;
5472 Get_First_Interp (Subp, I, It);
5473 while Present (It.Typ) loop
5474 if Covers (Typ, Etype (It.Typ)) then
5475 Nam := It.Typ;
5476 exit;
5477 end if;
5479 Get_Next_Interp (I, It);
5480 end loop;
5482 if No (Nam) then
5483 raise Program_Error;
5484 end if;
5485 end if;
5487 -- If the prefix is not an entity, then resolve it
5489 if not Is_Entity_Name (Subp) then
5490 Resolve (Subp, Nam);
5491 end if;
5493 -- For an indirect call, we always invalidate checks, since we do not
5494 -- know whether the subprogram is local or global. Yes we could do
5495 -- better here, e.g. by knowing that there are no local subprograms,
5496 -- but it does not seem worth the effort. Similarly, we kill all
5497 -- knowledge of current constant values.
5499 Kill_Current_Values;
5501 -- If this is a procedure call which is really an entry call, do
5502 -- the conversion of the procedure call to an entry call. Protected
5503 -- operations use the same circuitry because the name in the call
5504 -- can be an arbitrary expression with special resolution rules.
5506 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5507 or else (Is_Entity_Name (Subp)
5508 and then Ekind (Entity (Subp)) = E_Entry)
5509 then
5510 Resolve_Entry_Call (N, Typ);
5511 Check_Elab_Call (N);
5513 -- Kill checks and constant values, as above for indirect case
5514 -- Who knows what happens when another task is activated?
5516 Kill_Current_Values;
5517 return;
5519 -- Normal subprogram call with name established in Resolve
5521 elsif not (Is_Type (Entity (Subp))) then
5522 Nam := Entity (Subp);
5523 Set_Entity_With_Checks (Subp, Nam);
5525 -- Otherwise we must have the case of an overloaded call
5527 else
5528 pragma Assert (Is_Overloaded (Subp));
5530 -- Initialize Nam to prevent warning (we know it will be assigned
5531 -- in the loop below, but the compiler does not know that).
5533 Nam := Empty;
5535 Get_First_Interp (Subp, I, It);
5536 while Present (It.Typ) loop
5537 if Covers (Typ, It.Typ) then
5538 Nam := It.Nam;
5539 Set_Entity_With_Checks (Subp, Nam);
5540 exit;
5541 end if;
5543 Get_Next_Interp (I, It);
5544 end loop;
5545 end if;
5547 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5548 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5549 and then Nkind (Subp) /= N_Explicit_Dereference
5550 and then Present (Parameter_Associations (N))
5551 then
5552 -- The prefix is a parameterless function call that returns an access
5553 -- to subprogram. If parameters are present in the current call, add
5554 -- add an explicit dereference. We use the base type here because
5555 -- within an instance these may be subtypes.
5557 -- The dereference is added either in Analyze_Call or here. Should
5558 -- be consolidated ???
5560 Set_Is_Overloaded (Subp, False);
5561 Set_Etype (Subp, Etype (Nam));
5562 Insert_Explicit_Dereference (Subp);
5563 Nam := Designated_Type (Etype (Nam));
5564 Resolve (Subp, Nam);
5565 end if;
5567 -- Check that a call to Current_Task does not occur in an entry body
5569 if Is_RTE (Nam, RE_Current_Task) then
5570 declare
5571 P : Node_Id;
5573 begin
5574 P := N;
5575 loop
5576 P := Parent (P);
5578 -- Exclude calls that occur within the default of a formal
5579 -- parameter of the entry, since those are evaluated outside
5580 -- of the body.
5582 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5584 if Nkind (P) = N_Entry_Body
5585 or else (Nkind (P) = N_Subprogram_Body
5586 and then Is_Entry_Barrier_Function (P))
5587 then
5588 Rtype := Etype (N);
5589 Error_Msg_Warn := SPARK_Mode /= On;
5590 Error_Msg_NE
5591 ("& should not be used in entry body (RM C.7(17))<<",
5592 N, Nam);
5593 Error_Msg_NE ("\Program_Error [<<", N, Nam);
5594 Rewrite (N,
5595 Make_Raise_Program_Error (Loc,
5596 Reason => PE_Current_Task_In_Entry_Body));
5597 Set_Etype (N, Rtype);
5598 return;
5599 end if;
5600 end loop;
5601 end;
5602 end if;
5604 -- Check that a procedure call does not occur in the context of the
5605 -- entry call statement of a conditional or timed entry call. Note that
5606 -- the case of a call to a subprogram renaming of an entry will also be
5607 -- rejected. The test for N not being an N_Entry_Call_Statement is
5608 -- defensive, covering the possibility that the processing of entry
5609 -- calls might reach this point due to later modifications of the code
5610 -- above.
5612 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5613 and then Nkind (N) /= N_Entry_Call_Statement
5614 and then Entry_Call_Statement (Parent (N)) = N
5615 then
5616 if Ada_Version < Ada_2005 then
5617 Error_Msg_N ("entry call required in select statement", N);
5619 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5620 -- for a procedure_or_entry_call, the procedure_name or
5621 -- procedure_prefix of the procedure_call_statement shall denote
5622 -- an entry renamed by a procedure, or (a view of) a primitive
5623 -- subprogram of a limited interface whose first parameter is
5624 -- a controlling parameter.
5626 elsif Nkind (N) = N_Procedure_Call_Statement
5627 and then not Is_Renamed_Entry (Nam)
5628 and then not Is_Controlling_Limited_Procedure (Nam)
5629 then
5630 Error_Msg_N
5631 ("entry call or dispatching primitive of interface required", N);
5632 end if;
5633 end if;
5635 -- If the SPARK_05 restriction is active, we are not allowed
5636 -- to have a call to a subprogram before we see its completion.
5638 if not Has_Completion (Nam)
5639 and then Restriction_Check_Required (SPARK_05)
5641 -- Don't flag strange internal calls
5643 and then Comes_From_Source (N)
5644 and then Comes_From_Source (Nam)
5646 -- Only flag calls in extended main source
5648 and then In_Extended_Main_Source_Unit (Nam)
5649 and then In_Extended_Main_Source_Unit (N)
5651 -- Exclude enumeration literals from this processing
5653 and then Ekind (Nam) /= E_Enumeration_Literal
5654 then
5655 Check_SPARK_05_Restriction
5656 ("call to subprogram cannot appear before its body", N);
5657 end if;
5659 -- Check that this is not a call to a protected procedure or entry from
5660 -- within a protected function.
5662 Check_Internal_Protected_Use (N, Nam);
5664 -- Freeze the subprogram name if not in a spec-expression. Note that
5665 -- we freeze procedure calls as well as function calls. Procedure calls
5666 -- are not frozen according to the rules (RM 13.14(14)) because it is
5667 -- impossible to have a procedure call to a non-frozen procedure in
5668 -- pure Ada, but in the code that we generate in the expander, this
5669 -- rule needs extending because we can generate procedure calls that
5670 -- need freezing.
5672 -- In Ada 2012, expression functions may be called within pre/post
5673 -- conditions of subsequent functions or expression functions. Such
5674 -- calls do not freeze when they appear within generated bodies,
5675 -- (including the body of another expression function) which would
5676 -- place the freeze node in the wrong scope. An expression function
5677 -- is frozen in the usual fashion, by the appearance of a real body,
5678 -- or at the end of a declarative part.
5680 if Is_Entity_Name (Subp) and then not In_Spec_Expression
5681 and then not Is_Expression_Function (Current_Scope)
5682 and then
5683 (not Is_Expression_Function (Entity (Subp))
5684 or else Scope (Entity (Subp)) = Current_Scope)
5685 then
5686 Freeze_Expression (Subp);
5687 end if;
5689 -- For a predefined operator, the type of the result is the type imposed
5690 -- by context, except for a predefined operation on universal fixed.
5691 -- Otherwise The type of the call is the type returned by the subprogram
5692 -- being called.
5694 if Is_Predefined_Op (Nam) then
5695 if Etype (N) /= Universal_Fixed then
5696 Set_Etype (N, Typ);
5697 end if;
5699 -- If the subprogram returns an array type, and the context requires the
5700 -- component type of that array type, the node is really an indexing of
5701 -- the parameterless call. Resolve as such. A pathological case occurs
5702 -- when the type of the component is an access to the array type. In
5703 -- this case the call is truly ambiguous.
5705 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5706 and then
5707 ((Is_Array_Type (Etype (Nam))
5708 and then Covers (Typ, Component_Type (Etype (Nam))))
5709 or else
5710 (Is_Access_Type (Etype (Nam))
5711 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5712 and then
5713 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))))
5714 then
5715 declare
5716 Index_Node : Node_Id;
5717 New_Subp : Node_Id;
5718 Ret_Type : constant Entity_Id := Etype (Nam);
5720 begin
5721 if Is_Access_Type (Ret_Type)
5722 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5723 then
5724 Error_Msg_N
5725 ("cannot disambiguate function call and indexing", N);
5726 else
5727 New_Subp := Relocate_Node (Subp);
5729 -- The called entity may be an explicit dereference, in which
5730 -- case there is no entity to set.
5732 if Nkind (New_Subp) /= N_Explicit_Dereference then
5733 Set_Entity (Subp, Nam);
5734 end if;
5736 if (Is_Array_Type (Ret_Type)
5737 and then Component_Type (Ret_Type) /= Any_Type)
5738 or else
5739 (Is_Access_Type (Ret_Type)
5740 and then
5741 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5742 then
5743 if Needs_No_Actuals (Nam) then
5745 -- Indexed call to a parameterless function
5747 Index_Node :=
5748 Make_Indexed_Component (Loc,
5749 Prefix =>
5750 Make_Function_Call (Loc, Name => New_Subp),
5751 Expressions => Parameter_Associations (N));
5752 else
5753 -- An Ada 2005 prefixed call to a primitive operation
5754 -- whose first parameter is the prefix. This prefix was
5755 -- prepended to the parameter list, which is actually a
5756 -- list of indexes. Remove the prefix in order to build
5757 -- the proper indexed component.
5759 Index_Node :=
5760 Make_Indexed_Component (Loc,
5761 Prefix =>
5762 Make_Function_Call (Loc,
5763 Name => New_Subp,
5764 Parameter_Associations =>
5765 New_List
5766 (Remove_Head (Parameter_Associations (N)))),
5767 Expressions => Parameter_Associations (N));
5768 end if;
5770 -- Preserve the parenthesis count of the node
5772 Set_Paren_Count (Index_Node, Paren_Count (N));
5774 -- Since we are correcting a node classification error made
5775 -- by the parser, we call Replace rather than Rewrite.
5777 Replace (N, Index_Node);
5779 Set_Etype (Prefix (N), Ret_Type);
5780 Set_Etype (N, Typ);
5781 Resolve_Indexed_Component (N, Typ);
5782 Check_Elab_Call (Prefix (N));
5783 end if;
5784 end if;
5786 return;
5787 end;
5789 else
5790 Set_Etype (N, Etype (Nam));
5791 end if;
5793 -- In the case where the call is to an overloaded subprogram, Analyze
5794 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5795 -- such a case Normalize_Actuals needs to be called once more to order
5796 -- the actuals correctly. Otherwise the call will have the ordering
5797 -- given by the last overloaded subprogram whether this is the correct
5798 -- one being called or not.
5800 if Is_Overloaded (Subp) then
5801 Normalize_Actuals (N, Nam, False, Norm_OK);
5802 pragma Assert (Norm_OK);
5803 end if;
5805 -- In any case, call is fully resolved now. Reset Overload flag, to
5806 -- prevent subsequent overload resolution if node is analyzed again
5808 Set_Is_Overloaded (Subp, False);
5809 Set_Is_Overloaded (N, False);
5811 -- If we are calling the current subprogram from immediately within its
5812 -- body, then that is the case where we can sometimes detect cases of
5813 -- infinite recursion statically. Do not try this in case restriction
5814 -- No_Recursion is in effect anyway, and do it only for source calls.
5816 if Comes_From_Source (N) then
5817 Scop := Current_Scope;
5819 -- Check violation of SPARK_05 restriction which does not permit
5820 -- a subprogram body to contain a call to the subprogram directly.
5822 if Restriction_Check_Required (SPARK_05)
5823 and then Same_Or_Aliased_Subprograms (Nam, Scop)
5824 then
5825 Check_SPARK_05_Restriction
5826 ("subprogram may not contain direct call to itself", N);
5827 end if;
5829 -- Issue warning for possible infinite recursion in the absence
5830 -- of the No_Recursion restriction.
5832 if Same_Or_Aliased_Subprograms (Nam, Scop)
5833 and then not Restriction_Active (No_Recursion)
5834 and then Check_Infinite_Recursion (N)
5835 then
5836 -- Here we detected and flagged an infinite recursion, so we do
5837 -- not need to test the case below for further warnings. Also we
5838 -- are all done if we now have a raise SE node.
5840 if Nkind (N) = N_Raise_Storage_Error then
5841 return;
5842 end if;
5844 -- If call is to immediately containing subprogram, then check for
5845 -- the case of a possible run-time detectable infinite recursion.
5847 else
5848 Scope_Loop : while Scop /= Standard_Standard loop
5849 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5851 -- Although in general case, recursion is not statically
5852 -- checkable, the case of calling an immediately containing
5853 -- subprogram is easy to catch.
5855 Check_Restriction (No_Recursion, N);
5857 -- If the recursive call is to a parameterless subprogram,
5858 -- then even if we can't statically detect infinite
5859 -- recursion, this is pretty suspicious, and we output a
5860 -- warning. Furthermore, we will try later to detect some
5861 -- cases here at run time by expanding checking code (see
5862 -- Detect_Infinite_Recursion in package Exp_Ch6).
5864 -- If the recursive call is within a handler, do not emit a
5865 -- warning, because this is a common idiom: loop until input
5866 -- is correct, catch illegal input in handler and restart.
5868 if No (First_Formal (Nam))
5869 and then Etype (Nam) = Standard_Void_Type
5870 and then not Error_Posted (N)
5871 and then Nkind (Parent (N)) /= N_Exception_Handler
5872 then
5873 -- For the case of a procedure call. We give the message
5874 -- only if the call is the first statement in a sequence
5875 -- of statements, or if all previous statements are
5876 -- simple assignments. This is simply a heuristic to
5877 -- decrease false positives, without losing too many good
5878 -- warnings. The idea is that these previous statements
5879 -- may affect global variables the procedure depends on.
5880 -- We also exclude raise statements, that may arise from
5881 -- constraint checks and are probably unrelated to the
5882 -- intended control flow.
5884 if Nkind (N) = N_Procedure_Call_Statement
5885 and then Is_List_Member (N)
5886 then
5887 declare
5888 P : Node_Id;
5889 begin
5890 P := Prev (N);
5891 while Present (P) loop
5892 if not Nkind_In (P, N_Assignment_Statement,
5893 N_Raise_Constraint_Error)
5894 then
5895 exit Scope_Loop;
5896 end if;
5898 Prev (P);
5899 end loop;
5900 end;
5901 end if;
5903 -- Do not give warning if we are in a conditional context
5905 declare
5906 K : constant Node_Kind := Nkind (Parent (N));
5907 begin
5908 if (K = N_Loop_Statement
5909 and then Present (Iteration_Scheme (Parent (N))))
5910 or else K = N_If_Statement
5911 or else K = N_Elsif_Part
5912 or else K = N_Case_Statement_Alternative
5913 then
5914 exit Scope_Loop;
5915 end if;
5916 end;
5918 -- Here warning is to be issued
5920 Set_Has_Recursive_Call (Nam);
5921 Error_Msg_Warn := SPARK_Mode /= On;
5922 Error_Msg_N ("possible infinite recursion<<!", N);
5923 Error_Msg_N ("\Storage_Error ]<<!", N);
5924 end if;
5926 exit Scope_Loop;
5927 end if;
5929 Scop := Scope (Scop);
5930 end loop Scope_Loop;
5931 end if;
5932 end if;
5934 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5936 Check_Obsolescent_2005_Entity (Nam, Subp);
5938 -- If subprogram name is a predefined operator, it was given in
5939 -- functional notation. Replace call node with operator node, so
5940 -- that actuals can be resolved appropriately.
5942 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5943 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5944 return;
5946 elsif Present (Alias (Nam))
5947 and then Is_Predefined_Op (Alias (Nam))
5948 then
5949 Resolve_Actuals (N, Nam);
5950 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5951 return;
5952 end if;
5954 -- Create a transient scope if the resulting type requires it
5956 -- There are several notable exceptions:
5958 -- a) In init procs, the transient scope overhead is not needed, and is
5959 -- even incorrect when the call is a nested initialization call for a
5960 -- component whose expansion may generate adjust calls. However, if the
5961 -- call is some other procedure call within an initialization procedure
5962 -- (for example a call to Create_Task in the init_proc of the task
5963 -- run-time record) a transient scope must be created around this call.
5965 -- b) Enumeration literal pseudo-calls need no transient scope
5967 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5968 -- functions) do not use the secondary stack even though the return
5969 -- type may be unconstrained.
5971 -- d) Calls to a build-in-place function, since such functions may
5972 -- allocate their result directly in a target object, and cases where
5973 -- the result does get allocated in the secondary stack are checked for
5974 -- within the specialized Exp_Ch6 procedures for expanding those
5975 -- build-in-place calls.
5977 -- e) If the subprogram is marked Inline_Always, then even if it returns
5978 -- an unconstrained type the call does not require use of the secondary
5979 -- stack. However, inlining will only take place if the body to inline
5980 -- is already present. It may not be available if e.g. the subprogram is
5981 -- declared in a child instance.
5983 -- If this is an initialization call for a type whose construction
5984 -- uses the secondary stack, and it is not a nested call to initialize
5985 -- a component, we do need to create a transient scope for it. We
5986 -- check for this by traversing the type in Check_Initialization_Call.
5988 if Is_Inlined (Nam)
5989 and then Has_Pragma_Inline (Nam)
5990 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5991 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5992 then
5993 null;
5995 elsif Ekind (Nam) = E_Enumeration_Literal
5996 or else Is_Build_In_Place_Function (Nam)
5997 or else Is_Intrinsic_Subprogram (Nam)
5998 then
5999 null;
6001 elsif Expander_Active
6002 and then Is_Type (Etype (Nam))
6003 and then Requires_Transient_Scope (Etype (Nam))
6004 and then
6005 (not Within_Init_Proc
6006 or else
6007 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
6008 then
6009 Establish_Transient_Scope (N, Sec_Stack => True);
6011 -- If the call appears within the bounds of a loop, it will
6012 -- be rewritten and reanalyzed, nothing left to do here.
6014 if Nkind (N) /= N_Function_Call then
6015 return;
6016 end if;
6018 elsif Is_Init_Proc (Nam)
6019 and then not Within_Init_Proc
6020 then
6021 Check_Initialization_Call (N, Nam);
6022 end if;
6024 -- A protected function cannot be called within the definition of the
6025 -- enclosing protected type, unless it is part of a pre/postcondition
6026 -- on another protected operation.
6028 if Is_Protected_Type (Scope (Nam))
6029 and then In_Open_Scopes (Scope (Nam))
6030 and then not Has_Completion (Scope (Nam))
6031 and then not In_Spec_Expression
6032 then
6033 Error_Msg_NE
6034 ("& cannot be called before end of protected definition", N, Nam);
6035 end if;
6037 -- Propagate interpretation to actuals, and add default expressions
6038 -- where needed.
6040 if Present (First_Formal (Nam)) then
6041 Resolve_Actuals (N, Nam);
6043 -- Overloaded literals are rewritten as function calls, for purpose of
6044 -- resolution. After resolution, we can replace the call with the
6045 -- literal itself.
6047 elsif Ekind (Nam) = E_Enumeration_Literal then
6048 Copy_Node (Subp, N);
6049 Resolve_Entity_Name (N, Typ);
6051 -- Avoid validation, since it is a static function call
6053 Generate_Reference (Nam, Subp);
6054 return;
6055 end if;
6057 -- If the subprogram is not global, then kill all saved values and
6058 -- checks. This is a bit conservative, since in many cases we could do
6059 -- better, but it is not worth the effort. Similarly, we kill constant
6060 -- values. However we do not need to do this for internal entities
6061 -- (unless they are inherited user-defined subprograms), since they
6062 -- are not in the business of molesting local values.
6064 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6065 -- kill all checks and values for calls to global subprograms. This
6066 -- takes care of the case where an access to a local subprogram is
6067 -- taken, and could be passed directly or indirectly and then called
6068 -- from almost any context.
6070 -- Note: we do not do this step till after resolving the actuals. That
6071 -- way we still take advantage of the current value information while
6072 -- scanning the actuals.
6074 -- We suppress killing values if we are processing the nodes associated
6075 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6076 -- type kills all the values as part of analyzing the code that
6077 -- initializes the dispatch tables.
6079 if Inside_Freezing_Actions = 0
6080 and then (not Is_Library_Level_Entity (Nam)
6081 or else Suppress_Value_Tracking_On_Call
6082 (Nearest_Dynamic_Scope (Current_Scope)))
6083 and then (Comes_From_Source (Nam)
6084 or else (Present (Alias (Nam))
6085 and then Comes_From_Source (Alias (Nam))))
6086 then
6087 Kill_Current_Values;
6088 end if;
6090 -- If we are warning about unread OUT parameters, this is the place to
6091 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
6092 -- after the above call to Kill_Current_Values (since that call clears
6093 -- the Last_Assignment field of all local variables).
6095 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
6096 and then Comes_From_Source (N)
6097 and then In_Extended_Main_Source_Unit (N)
6098 then
6099 declare
6100 F : Entity_Id;
6101 A : Node_Id;
6103 begin
6104 F := First_Formal (Nam);
6105 A := First_Actual (N);
6106 while Present (F) and then Present (A) loop
6107 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
6108 and then Warn_On_Modified_As_Out_Parameter (F)
6109 and then Is_Entity_Name (A)
6110 and then Present (Entity (A))
6111 and then Comes_From_Source (N)
6112 and then Safe_To_Capture_Value (N, Entity (A))
6113 then
6114 Set_Last_Assignment (Entity (A), A);
6115 end if;
6117 Next_Formal (F);
6118 Next_Actual (A);
6119 end loop;
6120 end;
6121 end if;
6123 -- If the subprogram is a primitive operation, check whether or not
6124 -- it is a correct dispatching call.
6126 if Is_Overloadable (Nam)
6127 and then Is_Dispatching_Operation (Nam)
6128 then
6129 Check_Dispatching_Call (N);
6131 elsif Ekind (Nam) /= E_Subprogram_Type
6132 and then Is_Abstract_Subprogram (Nam)
6133 and then not In_Instance
6134 then
6135 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
6136 end if;
6138 -- If this is a dispatching call, generate the appropriate reference,
6139 -- for better source navigation in GPS.
6141 if Is_Overloadable (Nam)
6142 and then Present (Controlling_Argument (N))
6143 then
6144 Generate_Reference (Nam, Subp, 'R');
6146 -- Normal case, not a dispatching call: generate a call reference
6148 else
6149 Generate_Reference (Nam, Subp, 's');
6150 end if;
6152 if Is_Intrinsic_Subprogram (Nam) then
6153 Check_Intrinsic_Call (N);
6154 end if;
6156 -- Check for violation of restriction No_Specific_Termination_Handlers
6157 -- and warn on a potentially blocking call to Abort_Task.
6159 if Restriction_Check_Required (No_Specific_Termination_Handlers)
6160 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
6161 or else
6162 Is_RTE (Nam, RE_Specific_Handler))
6163 then
6164 Check_Restriction (No_Specific_Termination_Handlers, N);
6166 elsif Is_RTE (Nam, RE_Abort_Task) then
6167 Check_Potentially_Blocking_Operation (N);
6168 end if;
6170 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
6171 -- timing event violates restriction No_Relative_Delay (AI-0211). We
6172 -- need to check the second argument to determine whether it is an
6173 -- absolute or relative timing event.
6175 if Restriction_Check_Required (No_Relative_Delay)
6176 and then Is_RTE (Nam, RE_Set_Handler)
6177 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
6178 then
6179 Check_Restriction (No_Relative_Delay, N);
6180 end if;
6182 -- Issue an error for a call to an eliminated subprogram. This routine
6183 -- will not perform the check if the call appears within a default
6184 -- expression.
6186 Check_For_Eliminated_Subprogram (Subp, Nam);
6188 -- In formal mode, the primitive operations of a tagged type or type
6189 -- extension do not include functions that return the tagged type.
6191 if Nkind (N) = N_Function_Call
6192 and then Is_Tagged_Type (Etype (N))
6193 and then Is_Entity_Name (Name (N))
6194 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
6195 then
6196 Check_SPARK_05_Restriction ("function not inherited", N);
6197 end if;
6199 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
6200 -- class-wide and the call dispatches on result in a context that does
6201 -- not provide a tag, the call raises Program_Error.
6203 if Nkind (N) = N_Function_Call
6204 and then In_Instance
6205 and then Is_Generic_Actual_Type (Typ)
6206 and then Is_Class_Wide_Type (Typ)
6207 and then Has_Controlling_Result (Nam)
6208 and then Nkind (Parent (N)) = N_Object_Declaration
6209 then
6210 -- Verify that none of the formals are controlling
6212 declare
6213 Call_OK : Boolean := False;
6214 F : Entity_Id;
6216 begin
6217 F := First_Formal (Nam);
6218 while Present (F) loop
6219 if Is_Controlling_Formal (F) then
6220 Call_OK := True;
6221 exit;
6222 end if;
6224 Next_Formal (F);
6225 end loop;
6227 if not Call_OK then
6228 Error_Msg_Warn := SPARK_Mode /= On;
6229 Error_Msg_N ("!cannot determine tag of result<<", N);
6230 Error_Msg_N ("\Program_Error [<<!", N);
6231 Insert_Action (N,
6232 Make_Raise_Program_Error (Sloc (N),
6233 Reason => PE_Explicit_Raise));
6234 end if;
6235 end;
6236 end if;
6238 -- Check for calling a function with OUT or IN OUT parameter when the
6239 -- calling context (us right now) is not Ada 2012, so does not allow
6240 -- OUT or IN OUT parameters in function calls.
6242 if Ada_Version < Ada_2012
6243 and then Ekind (Nam) = E_Function
6244 and then Has_Out_Or_In_Out_Parameter (Nam)
6245 then
6246 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
6247 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
6248 end if;
6250 -- Check the dimensions of the actuals in the call. For function calls,
6251 -- propagate the dimensions from the returned type to N.
6253 Analyze_Dimension_Call (N, Nam);
6255 -- All done, evaluate call and deal with elaboration issues
6257 Eval_Call (N);
6258 Check_Elab_Call (N);
6260 -- In GNATprove mode, expansion is disabled, but we want to inline some
6261 -- subprograms to facilitate formal verification. Indirect calls through
6262 -- a subprogram type or within a generic cannot be inlined. Inlining is
6263 -- performed only for calls subject to SPARK_Mode on.
6265 if GNATprove_Mode
6266 and then SPARK_Mode = On
6267 and then Is_Overloadable (Nam)
6268 and then not Inside_A_Generic
6269 then
6270 Nam_UA := Ultimate_Alias (Nam);
6271 Nam_Decl := Unit_Declaration_Node (Nam_UA);
6273 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
6274 Body_Id := Corresponding_Body (Nam_Decl);
6276 -- Nothing to do if the subprogram is not eligible for inlining in
6277 -- GNATprove mode.
6279 if not Is_Inlined_Always (Nam_UA)
6280 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
6281 then
6282 null;
6284 -- Calls cannot be inlined inside assertions, as GNATprove treats
6285 -- assertions as logic expressions.
6287 elsif In_Assertion_Expr /= 0 then
6288 Error_Msg_NE ("?no contextual analysis of &", N, Nam);
6289 Error_Msg_N ("\call appears in assertion expression", N);
6290 Set_Is_Inlined_Always (Nam_UA, False);
6292 -- Calls cannot be inlined inside default expressions
6294 elsif In_Default_Expr then
6295 Error_Msg_NE ("?no contextual analysis of &", N, Nam);
6296 Error_Msg_N ("\call appears in default expression", N);
6297 Set_Is_Inlined_Always (Nam_UA, False);
6299 -- Inlining should not be performed during pre-analysis
6301 elsif Full_Analysis then
6303 -- With the one-pass inlining technique, a call cannot be
6304 -- inlined if the corresponding body has not been seen yet.
6306 if No (Body_Id) then
6307 Error_Msg_NE
6308 ("?no contextual analysis of & (body not seen yet)",
6309 N, Nam);
6310 Set_Is_Inlined_Always (Nam_UA, False);
6312 -- Nothing to do if there is no body to inline, indicating that
6313 -- the subprogram is not suitable for inlining in GNATprove
6314 -- mode.
6316 elsif No (Body_To_Inline (Nam_Decl)) then
6317 null;
6319 -- Calls cannot be inlined inside potentially unevaluated
6320 -- expressions, as this would create complex actions inside
6321 -- expressions, that are not handled by GNATprove.
6323 elsif Is_Potentially_Unevaluated (N) then
6324 Error_Msg_NE ("?no contextual analysis of &", N, Nam);
6325 Error_Msg_N
6326 ("\call appears in potentially unevaluated context", N);
6327 Set_Is_Inlined_Always (Nam_UA, False);
6329 -- Otherwise, inline the call
6331 else
6332 Expand_Inlined_Call (N, Nam_UA, Nam);
6333 end if;
6334 end if;
6335 end if;
6336 end if;
6338 Warn_On_Overlapping_Actuals (Nam, N);
6339 end Resolve_Call;
6341 -----------------------------
6342 -- Resolve_Case_Expression --
6343 -----------------------------
6345 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
6346 Alt : Node_Id;
6348 begin
6349 Alt := First (Alternatives (N));
6350 while Present (Alt) loop
6351 Resolve (Expression (Alt), Typ);
6352 Next (Alt);
6353 end loop;
6355 Set_Etype (N, Typ);
6356 Eval_Case_Expression (N);
6357 end Resolve_Case_Expression;
6359 -------------------------------
6360 -- Resolve_Character_Literal --
6361 -------------------------------
6363 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
6364 B_Typ : constant Entity_Id := Base_Type (Typ);
6365 C : Entity_Id;
6367 begin
6368 -- Verify that the character does belong to the type of the context
6370 Set_Etype (N, B_Typ);
6371 Eval_Character_Literal (N);
6373 -- Wide_Wide_Character literals must always be defined, since the set
6374 -- of wide wide character literals is complete, i.e. if a character
6375 -- literal is accepted by the parser, then it is OK for wide wide
6376 -- character (out of range character literals are rejected).
6378 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6379 return;
6381 -- Always accept character literal for type Any_Character, which
6382 -- occurs in error situations and in comparisons of literals, both
6383 -- of which should accept all literals.
6385 elsif B_Typ = Any_Character then
6386 return;
6388 -- For Standard.Character or a type derived from it, check that the
6389 -- literal is in range.
6391 elsif Root_Type (B_Typ) = Standard_Character then
6392 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6393 return;
6394 end if;
6396 -- For Standard.Wide_Character or a type derived from it, check that the
6397 -- literal is in range.
6399 elsif Root_Type (B_Typ) = Standard_Wide_Character then
6400 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6401 return;
6402 end if;
6404 -- For Standard.Wide_Wide_Character or a type derived from it, we
6405 -- know the literal is in range, since the parser checked.
6407 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6408 return;
6410 -- If the entity is already set, this has already been resolved in a
6411 -- generic context, or comes from expansion. Nothing else to do.
6413 elsif Present (Entity (N)) then
6414 return;
6416 -- Otherwise we have a user defined character type, and we can use the
6417 -- standard visibility mechanisms to locate the referenced entity.
6419 else
6420 C := Current_Entity (N);
6421 while Present (C) loop
6422 if Etype (C) = B_Typ then
6423 Set_Entity_With_Checks (N, C);
6424 Generate_Reference (C, N);
6425 return;
6426 end if;
6428 C := Homonym (C);
6429 end loop;
6430 end if;
6432 -- If we fall through, then the literal does not match any of the
6433 -- entries of the enumeration type. This isn't just a constraint error
6434 -- situation, it is an illegality (see RM 4.2).
6436 Error_Msg_NE
6437 ("character not defined for }", N, First_Subtype (B_Typ));
6438 end Resolve_Character_Literal;
6440 ---------------------------
6441 -- Resolve_Comparison_Op --
6442 ---------------------------
6444 -- Context requires a boolean type, and plays no role in resolution.
6445 -- Processing identical to that for equality operators. The result type is
6446 -- the base type, which matters when pathological subtypes of booleans with
6447 -- limited ranges are used.
6449 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6450 L : constant Node_Id := Left_Opnd (N);
6451 R : constant Node_Id := Right_Opnd (N);
6452 T : Entity_Id;
6454 begin
6455 -- If this is an intrinsic operation which is not predefined, use the
6456 -- types of its declared arguments to resolve the possibly overloaded
6457 -- operands. Otherwise the operands are unambiguous and specify the
6458 -- expected type.
6460 if Scope (Entity (N)) /= Standard_Standard then
6461 T := Etype (First_Entity (Entity (N)));
6463 else
6464 T := Find_Unique_Type (L, R);
6466 if T = Any_Fixed then
6467 T := Unique_Fixed_Point_Type (L);
6468 end if;
6469 end if;
6471 Set_Etype (N, Base_Type (Typ));
6472 Generate_Reference (T, N, ' ');
6474 -- Skip remaining processing if already set to Any_Type
6476 if T = Any_Type then
6477 return;
6478 end if;
6480 -- Deal with other error cases
6482 if T = Any_String or else
6483 T = Any_Composite or else
6484 T = Any_Character
6485 then
6486 if T = Any_Character then
6487 Ambiguous_Character (L);
6488 else
6489 Error_Msg_N ("ambiguous operands for comparison", N);
6490 end if;
6492 Set_Etype (N, Any_Type);
6493 return;
6494 end if;
6496 -- Resolve the operands if types OK
6498 Resolve (L, T);
6499 Resolve (R, T);
6500 Check_Unset_Reference (L);
6501 Check_Unset_Reference (R);
6502 Generate_Operator_Reference (N, T);
6503 Check_Low_Bound_Tested (N);
6505 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6506 -- types or array types except String.
6508 if Is_Boolean_Type (T) then
6509 Check_SPARK_05_Restriction
6510 ("comparison is not defined on Boolean type", N);
6512 elsif Is_Array_Type (T)
6513 and then Base_Type (T) /= Standard_String
6514 then
6515 Check_SPARK_05_Restriction
6516 ("comparison is not defined on array types other than String", N);
6517 end if;
6519 -- Check comparison on unordered enumeration
6521 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
6522 Error_Msg_Sloc := Sloc (Etype (L));
6523 Error_Msg_NE
6524 ("comparison on unordered enumeration type& declared#?U?",
6525 N, Etype (L));
6526 end if;
6528 -- Evaluate the relation (note we do this after the above check since
6529 -- this Eval call may change N to True/False.
6531 Analyze_Dimension (N);
6532 Eval_Relational_Op (N);
6533 end Resolve_Comparison_Op;
6535 -----------------------------------------
6536 -- Resolve_Discrete_Subtype_Indication --
6537 -----------------------------------------
6539 procedure Resolve_Discrete_Subtype_Indication
6540 (N : Node_Id;
6541 Typ : Entity_Id)
6543 R : Node_Id;
6544 S : Entity_Id;
6546 begin
6547 Analyze (Subtype_Mark (N));
6548 S := Entity (Subtype_Mark (N));
6550 if Nkind (Constraint (N)) /= N_Range_Constraint then
6551 Error_Msg_N ("expect range constraint for discrete type", N);
6552 Set_Etype (N, Any_Type);
6554 else
6555 R := Range_Expression (Constraint (N));
6557 if R = Error then
6558 return;
6559 end if;
6561 Analyze (R);
6563 if Base_Type (S) /= Base_Type (Typ) then
6564 Error_Msg_NE
6565 ("expect subtype of }", N, First_Subtype (Typ));
6567 -- Rewrite the constraint as a range of Typ
6568 -- to allow compilation to proceed further.
6570 Set_Etype (N, Typ);
6571 Rewrite (Low_Bound (R),
6572 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6573 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6574 Attribute_Name => Name_First));
6575 Rewrite (High_Bound (R),
6576 Make_Attribute_Reference (Sloc (High_Bound (R)),
6577 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6578 Attribute_Name => Name_First));
6580 else
6581 Resolve (R, Typ);
6582 Set_Etype (N, Etype (R));
6584 -- Additionally, we must check that the bounds are compatible
6585 -- with the given subtype, which might be different from the
6586 -- type of the context.
6588 Apply_Range_Check (R, S);
6590 -- ??? If the above check statically detects a Constraint_Error
6591 -- it replaces the offending bound(s) of the range R with a
6592 -- Constraint_Error node. When the itype which uses these bounds
6593 -- is frozen the resulting call to Duplicate_Subexpr generates
6594 -- a new temporary for the bounds.
6596 -- Unfortunately there are other itypes that are also made depend
6597 -- on these bounds, so when Duplicate_Subexpr is called they get
6598 -- a forward reference to the newly created temporaries and Gigi
6599 -- aborts on such forward references. This is probably sign of a
6600 -- more fundamental problem somewhere else in either the order of
6601 -- itype freezing or the way certain itypes are constructed.
6603 -- To get around this problem we call Remove_Side_Effects right
6604 -- away if either bounds of R are a Constraint_Error.
6606 declare
6607 L : constant Node_Id := Low_Bound (R);
6608 H : constant Node_Id := High_Bound (R);
6610 begin
6611 if Nkind (L) = N_Raise_Constraint_Error then
6612 Remove_Side_Effects (L);
6613 end if;
6615 if Nkind (H) = N_Raise_Constraint_Error then
6616 Remove_Side_Effects (H);
6617 end if;
6618 end;
6620 Check_Unset_Reference (Low_Bound (R));
6621 Check_Unset_Reference (High_Bound (R));
6622 end if;
6623 end if;
6624 end Resolve_Discrete_Subtype_Indication;
6626 -------------------------
6627 -- Resolve_Entity_Name --
6628 -------------------------
6630 -- Used to resolve identifiers and expanded names
6632 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6633 function Appears_In_Check (Nod : Node_Id) return Boolean;
6634 -- Denote whether an arbitrary node Nod appears in a check node
6636 function Is_OK_Volatile_Context
6637 (Context : Node_Id;
6638 Obj_Ref : Node_Id) return Boolean;
6639 -- Determine whether node Context denotes a "non-interfering context"
6640 -- (as defined in SPARK RM 7.1.3(13)) where volatile reference Obj_Ref
6641 -- can safely reside.
6643 ----------------------
6644 -- Appears_In_Check --
6645 ----------------------
6647 function Appears_In_Check (Nod : Node_Id) return Boolean is
6648 Par : Node_Id;
6650 begin
6651 -- Climb the parent chain looking for a check node
6653 Par := Nod;
6654 while Present (Par) loop
6655 if Nkind (Par) in N_Raise_xxx_Error then
6656 return True;
6658 -- Prevent the search from going too far
6660 elsif Is_Body_Or_Package_Declaration (Par) then
6661 exit;
6662 end if;
6664 Par := Parent (Par);
6665 end loop;
6667 return False;
6668 end Appears_In_Check;
6670 ----------------------------
6671 -- Is_OK_Volatile_Context --
6672 ----------------------------
6674 function Is_OK_Volatile_Context
6675 (Context : Node_Id;
6676 Obj_Ref : Node_Id) return Boolean
6678 begin
6679 -- The volatile object appears on either side of an assignment
6681 if Nkind (Context) = N_Assignment_Statement then
6682 return True;
6684 -- The volatile object is part of the initialization expression of
6685 -- another object. Ensure that the climb of the parent chain came
6686 -- from the expression side and not from the name side.
6688 elsif Nkind (Context) = N_Object_Declaration
6689 and then Present (Expression (Context))
6690 and then Expression (Context) = Obj_Ref
6691 then
6692 return True;
6694 -- The volatile object appears as an actual parameter in a call to an
6695 -- instance of Unchecked_Conversion whose result is renamed.
6697 elsif Nkind (Context) = N_Function_Call
6698 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
6699 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
6700 then
6701 return True;
6703 -- The volatile object appears as the prefix of a name occurring
6704 -- in a non-interfering context.
6706 elsif Nkind_In (Context, N_Attribute_Reference,
6707 N_Indexed_Component,
6708 N_Selected_Component,
6709 N_Slice)
6710 and then Prefix (Context) = Obj_Ref
6711 and then Is_OK_Volatile_Context
6712 (Context => Parent (Context),
6713 Obj_Ref => Context)
6714 then
6715 return True;
6717 -- The volatile object appears as the expression of a type conversion
6718 -- occurring in a non-interfering context.
6720 elsif Nkind_In (Context, N_Type_Conversion,
6721 N_Unchecked_Type_Conversion)
6722 and then Expression (Context) = Obj_Ref
6723 and then Is_OK_Volatile_Context
6724 (Context => Parent (Context),
6725 Obj_Ref => Context)
6726 then
6727 return True;
6729 -- Allow references to volatile objects in various checks. This is
6730 -- not a direct SPARK 2014 requirement.
6732 elsif Appears_In_Check (Context) then
6733 return True;
6735 else
6736 return False;
6737 end if;
6738 end Is_OK_Volatile_Context;
6740 -- Local variables
6742 E : constant Entity_Id := Entity (N);
6743 Par : Node_Id;
6745 -- Start of processing for Resolve_Entity_Name
6747 begin
6748 -- If garbage from errors, set to Any_Type and return
6750 if No (E) and then Total_Errors_Detected /= 0 then
6751 Set_Etype (N, Any_Type);
6752 return;
6753 end if;
6755 -- Replace named numbers by corresponding literals. Note that this is
6756 -- the one case where Resolve_Entity_Name must reset the Etype, since
6757 -- it is currently marked as universal.
6759 if Ekind (E) = E_Named_Integer then
6760 Set_Etype (N, Typ);
6761 Eval_Named_Integer (N);
6763 elsif Ekind (E) = E_Named_Real then
6764 Set_Etype (N, Typ);
6765 Eval_Named_Real (N);
6767 -- For enumeration literals, we need to make sure that a proper style
6768 -- check is done, since such literals are overloaded, and thus we did
6769 -- not do a style check during the first phase of analysis.
6771 elsif Ekind (E) = E_Enumeration_Literal then
6772 Set_Entity_With_Checks (N, E);
6773 Eval_Entity_Name (N);
6775 -- Case of subtype name appearing as an operand in expression
6777 elsif Is_Type (E) then
6779 -- Allow use of subtype if it is a concurrent type where we are
6780 -- currently inside the body. This will eventually be expanded into a
6781 -- call to Self (for tasks) or _object (for protected objects). Any
6782 -- other use of a subtype is invalid.
6784 if Is_Concurrent_Type (E)
6785 and then In_Open_Scopes (E)
6786 then
6787 null;
6789 -- Any other use is an error
6791 else
6792 Error_Msg_N
6793 ("invalid use of subtype mark in expression or call", N);
6794 end if;
6796 -- Check discriminant use if entity is discriminant in current scope,
6797 -- i.e. discriminant of record or concurrent type currently being
6798 -- analyzed. Uses in corresponding body are unrestricted.
6800 elsif Ekind (E) = E_Discriminant
6801 and then Scope (E) = Current_Scope
6802 and then not Has_Completion (Current_Scope)
6803 then
6804 Check_Discriminant_Use (N);
6806 -- A parameterless generic function cannot appear in a context that
6807 -- requires resolution.
6809 elsif Ekind (E) = E_Generic_Function then
6810 Error_Msg_N ("illegal use of generic function", N);
6812 elsif Ekind (E) = E_Out_Parameter
6813 and then Ada_Version = Ada_83
6814 and then (Nkind (Parent (N)) in N_Op
6815 or else (Nkind (Parent (N)) = N_Assignment_Statement
6816 and then N = Expression (Parent (N)))
6817 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6818 then
6819 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6821 -- In all other cases, just do the possible static evaluation
6823 else
6824 -- A deferred constant that appears in an expression must have a
6825 -- completion, unless it has been removed by in-place expansion of
6826 -- an aggregate.
6828 if Ekind (E) = E_Constant
6829 and then Comes_From_Source (E)
6830 and then No (Constant_Value (E))
6831 and then Is_Frozen (Etype (E))
6832 and then not In_Spec_Expression
6833 and then not Is_Imported (E)
6834 then
6835 if No_Initialization (Parent (E))
6836 or else (Present (Full_View (E))
6837 and then No_Initialization (Parent (Full_View (E))))
6838 then
6839 null;
6840 else
6841 Error_Msg_N (
6842 "deferred constant is frozen before completion", N);
6843 end if;
6844 end if;
6846 Eval_Entity_Name (N);
6847 end if;
6849 Par := Parent (N);
6851 -- When the entity appears in a parameter association, retrieve the
6852 -- related subprogram call.
6854 if Nkind (Par) = N_Parameter_Association then
6855 Par := Parent (Par);
6856 end if;
6858 -- An effectively volatile object subject to enabled properties
6859 -- Async_Writers or Effective_Reads must appear in a specific context.
6860 -- The following checks are only relevant when SPARK_Mode is on as they
6861 -- are not standard Ada legality rules.
6863 if SPARK_Mode = On
6864 and then Is_Object (E)
6865 and then Is_Effectively_Volatile (E)
6866 and then Comes_From_Source (E)
6867 and then
6868 (Async_Writers_Enabled (E) or else Effective_Reads_Enabled (E))
6869 then
6870 -- The effectively volatile objects appears in a "non-interfering
6871 -- context" as defined in SPARK RM 7.1.3(13).
6873 if Is_OK_Volatile_Context (Par, N) then
6874 null;
6876 -- Assume that references to effectively volatile objects that appear
6877 -- as actual parameters in a procedure call are always legal. A full
6878 -- legality check is done when the actuals are resolved.
6880 elsif Nkind (Par) = N_Procedure_Call_Statement then
6881 null;
6883 -- Otherwise the context causes a side effect with respect to the
6884 -- effectively volatile object.
6886 else
6887 Error_Msg_N
6888 ("volatile object cannot appear in this context "
6889 & "(SPARK RM 7.1.3(13))", N);
6890 end if;
6891 end if;
6892 end Resolve_Entity_Name;
6894 -------------------
6895 -- Resolve_Entry --
6896 -------------------
6898 procedure Resolve_Entry (Entry_Name : Node_Id) is
6899 Loc : constant Source_Ptr := Sloc (Entry_Name);
6900 Nam : Entity_Id;
6901 New_N : Node_Id;
6902 S : Entity_Id;
6903 Tsk : Entity_Id;
6904 E_Name : Node_Id;
6905 Index : Node_Id;
6907 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6908 -- If the bounds of the entry family being called depend on task
6909 -- discriminants, build a new index subtype where a discriminant is
6910 -- replaced with the value of the discriminant of the target task.
6911 -- The target task is the prefix of the entry name in the call.
6913 -----------------------
6914 -- Actual_Index_Type --
6915 -----------------------
6917 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6918 Typ : constant Entity_Id := Entry_Index_Type (E);
6919 Tsk : constant Entity_Id := Scope (E);
6920 Lo : constant Node_Id := Type_Low_Bound (Typ);
6921 Hi : constant Node_Id := Type_High_Bound (Typ);
6922 New_T : Entity_Id;
6924 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6925 -- If the bound is given by a discriminant, replace with a reference
6926 -- to the discriminant of the same name in the target task. If the
6927 -- entry name is the target of a requeue statement and the entry is
6928 -- in the current protected object, the bound to be used is the
6929 -- discriminal of the object (see Apply_Range_Checks for details of
6930 -- the transformation).
6932 -----------------------------
6933 -- Actual_Discriminant_Ref --
6934 -----------------------------
6936 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6937 Typ : constant Entity_Id := Etype (Bound);
6938 Ref : Node_Id;
6940 begin
6941 Remove_Side_Effects (Bound);
6943 if not Is_Entity_Name (Bound)
6944 or else Ekind (Entity (Bound)) /= E_Discriminant
6945 then
6946 return Bound;
6948 elsif Is_Protected_Type (Tsk)
6949 and then In_Open_Scopes (Tsk)
6950 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6951 then
6952 -- Note: here Bound denotes a discriminant of the corresponding
6953 -- record type tskV, whose discriminal is a formal of the
6954 -- init-proc tskVIP. What we want is the body discriminal,
6955 -- which is associated to the discriminant of the original
6956 -- concurrent type tsk.
6958 return New_Occurrence_Of
6959 (Find_Body_Discriminal (Entity (Bound)), Loc);
6961 else
6962 Ref :=
6963 Make_Selected_Component (Loc,
6964 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6965 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6966 Analyze (Ref);
6967 Resolve (Ref, Typ);
6968 return Ref;
6969 end if;
6970 end Actual_Discriminant_Ref;
6972 -- Start of processing for Actual_Index_Type
6974 begin
6975 if not Has_Discriminants (Tsk)
6976 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6977 then
6978 return Entry_Index_Type (E);
6980 else
6981 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6982 Set_Etype (New_T, Base_Type (Typ));
6983 Set_Size_Info (New_T, Typ);
6984 Set_RM_Size (New_T, RM_Size (Typ));
6985 Set_Scalar_Range (New_T,
6986 Make_Range (Sloc (Entry_Name),
6987 Low_Bound => Actual_Discriminant_Ref (Lo),
6988 High_Bound => Actual_Discriminant_Ref (Hi)));
6990 return New_T;
6991 end if;
6992 end Actual_Index_Type;
6994 -- Start of processing of Resolve_Entry
6996 begin
6997 -- Find name of entry being called, and resolve prefix of name with its
6998 -- own type. The prefix can be overloaded, and the name and signature of
6999 -- the entry must be taken into account.
7001 if Nkind (Entry_Name) = N_Indexed_Component then
7003 -- Case of dealing with entry family within the current tasks
7005 E_Name := Prefix (Entry_Name);
7007 else
7008 E_Name := Entry_Name;
7009 end if;
7011 if Is_Entity_Name (E_Name) then
7013 -- Entry call to an entry (or entry family) in the current task. This
7014 -- is legal even though the task will deadlock. Rewrite as call to
7015 -- current task.
7017 -- This can also be a call to an entry in an enclosing task. If this
7018 -- is a single task, we have to retrieve its name, because the scope
7019 -- of the entry is the task type, not the object. If the enclosing
7020 -- task is a task type, the identity of the task is given by its own
7021 -- self variable.
7023 -- Finally this can be a requeue on an entry of the same task or
7024 -- protected object.
7026 S := Scope (Entity (E_Name));
7028 for J in reverse 0 .. Scope_Stack.Last loop
7029 if Is_Task_Type (Scope_Stack.Table (J).Entity)
7030 and then not Comes_From_Source (S)
7031 then
7032 -- S is an enclosing task or protected object. The concurrent
7033 -- declaration has been converted into a type declaration, and
7034 -- the object itself has an object declaration that follows
7035 -- the type in the same declarative part.
7037 Tsk := Next_Entity (S);
7038 while Etype (Tsk) /= S loop
7039 Next_Entity (Tsk);
7040 end loop;
7042 S := Tsk;
7043 exit;
7045 elsif S = Scope_Stack.Table (J).Entity then
7047 -- Call to current task. Will be transformed into call to Self
7049 exit;
7051 end if;
7052 end loop;
7054 New_N :=
7055 Make_Selected_Component (Loc,
7056 Prefix => New_Occurrence_Of (S, Loc),
7057 Selector_Name =>
7058 New_Occurrence_Of (Entity (E_Name), Loc));
7059 Rewrite (E_Name, New_N);
7060 Analyze (E_Name);
7062 elsif Nkind (Entry_Name) = N_Selected_Component
7063 and then Is_Overloaded (Prefix (Entry_Name))
7064 then
7065 -- Use the entry name (which must be unique at this point) to find
7066 -- the prefix that returns the corresponding task/protected type.
7068 declare
7069 Pref : constant Node_Id := Prefix (Entry_Name);
7070 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
7071 I : Interp_Index;
7072 It : Interp;
7074 begin
7075 Get_First_Interp (Pref, I, It);
7076 while Present (It.Typ) loop
7077 if Scope (Ent) = It.Typ then
7078 Set_Etype (Pref, It.Typ);
7079 exit;
7080 end if;
7082 Get_Next_Interp (I, It);
7083 end loop;
7084 end;
7085 end if;
7087 if Nkind (Entry_Name) = N_Selected_Component then
7088 Resolve (Prefix (Entry_Name));
7090 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7091 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
7092 Resolve (Prefix (Prefix (Entry_Name)));
7093 Index := First (Expressions (Entry_Name));
7094 Resolve (Index, Entry_Index_Type (Nam));
7096 -- Up to this point the expression could have been the actual in a
7097 -- simple entry call, and be given by a named association.
7099 if Nkind (Index) = N_Parameter_Association then
7100 Error_Msg_N ("expect expression for entry index", Index);
7101 else
7102 Apply_Range_Check (Index, Actual_Index_Type (Nam));
7103 end if;
7104 end if;
7105 end Resolve_Entry;
7107 ------------------------
7108 -- Resolve_Entry_Call --
7109 ------------------------
7111 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
7112 Entry_Name : constant Node_Id := Name (N);
7113 Loc : constant Source_Ptr := Sloc (Entry_Name);
7114 Actuals : List_Id;
7115 First_Named : Node_Id;
7116 Nam : Entity_Id;
7117 Norm_OK : Boolean;
7118 Obj : Node_Id;
7119 Was_Over : Boolean;
7121 begin
7122 -- We kill all checks here, because it does not seem worth the effort to
7123 -- do anything better, an entry call is a big operation.
7125 Kill_All_Checks;
7127 -- Processing of the name is similar for entry calls and protected
7128 -- operation calls. Once the entity is determined, we can complete
7129 -- the resolution of the actuals.
7131 -- The selector may be overloaded, in the case of a protected object
7132 -- with overloaded functions. The type of the context is used for
7133 -- resolution.
7135 if Nkind (Entry_Name) = N_Selected_Component
7136 and then Is_Overloaded (Selector_Name (Entry_Name))
7137 and then Typ /= Standard_Void_Type
7138 then
7139 declare
7140 I : Interp_Index;
7141 It : Interp;
7143 begin
7144 Get_First_Interp (Selector_Name (Entry_Name), I, It);
7145 while Present (It.Typ) loop
7146 if Covers (Typ, It.Typ) then
7147 Set_Entity (Selector_Name (Entry_Name), It.Nam);
7148 Set_Etype (Entry_Name, It.Typ);
7150 Generate_Reference (It.Typ, N, ' ');
7151 end if;
7153 Get_Next_Interp (I, It);
7154 end loop;
7155 end;
7156 end if;
7158 Resolve_Entry (Entry_Name);
7160 if Nkind (Entry_Name) = N_Selected_Component then
7162 -- Simple entry call
7164 Nam := Entity (Selector_Name (Entry_Name));
7165 Obj := Prefix (Entry_Name);
7166 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
7168 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7170 -- Call to member of entry family
7172 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
7173 Obj := Prefix (Prefix (Entry_Name));
7174 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
7175 end if;
7177 -- We cannot in general check the maximum depth of protected entry calls
7178 -- at compile time. But we can tell that any protected entry call at all
7179 -- violates a specified nesting depth of zero.
7181 if Is_Protected_Type (Scope (Nam)) then
7182 Check_Restriction (Max_Entry_Queue_Length, N);
7183 end if;
7185 -- Use context type to disambiguate a protected function that can be
7186 -- called without actuals and that returns an array type, and where the
7187 -- argument list may be an indexing of the returned value.
7189 if Ekind (Nam) = E_Function
7190 and then Needs_No_Actuals (Nam)
7191 and then Present (Parameter_Associations (N))
7192 and then
7193 ((Is_Array_Type (Etype (Nam))
7194 and then Covers (Typ, Component_Type (Etype (Nam))))
7196 or else (Is_Access_Type (Etype (Nam))
7197 and then Is_Array_Type (Designated_Type (Etype (Nam)))
7198 and then
7199 Covers
7200 (Typ,
7201 Component_Type (Designated_Type (Etype (Nam))))))
7202 then
7203 declare
7204 Index_Node : Node_Id;
7206 begin
7207 Index_Node :=
7208 Make_Indexed_Component (Loc,
7209 Prefix =>
7210 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
7211 Expressions => Parameter_Associations (N));
7213 -- Since we are correcting a node classification error made by the
7214 -- parser, we call Replace rather than Rewrite.
7216 Replace (N, Index_Node);
7217 Set_Etype (Prefix (N), Etype (Nam));
7218 Set_Etype (N, Typ);
7219 Resolve_Indexed_Component (N, Typ);
7220 return;
7221 end;
7222 end if;
7224 if Ekind_In (Nam, E_Entry, E_Entry_Family)
7225 and then Present (PPC_Wrapper (Nam))
7226 and then Current_Scope /= PPC_Wrapper (Nam)
7227 then
7228 -- Rewrite as call to the precondition wrapper, adding the task
7229 -- object to the list of actuals. If the call is to a member of an
7230 -- entry family, include the index as well.
7232 declare
7233 New_Call : Node_Id;
7234 New_Actuals : List_Id;
7236 begin
7237 New_Actuals := New_List (Obj);
7239 if Nkind (Entry_Name) = N_Indexed_Component then
7240 Append_To (New_Actuals,
7241 New_Copy_Tree (First (Expressions (Entry_Name))));
7242 end if;
7244 Append_List (Parameter_Associations (N), New_Actuals);
7245 New_Call :=
7246 Make_Procedure_Call_Statement (Loc,
7247 Name =>
7248 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
7249 Parameter_Associations => New_Actuals);
7250 Rewrite (N, New_Call);
7252 -- Preanalyze and resolve new call. Current procedure is called
7253 -- from Resolve_Call, after which expansion will take place.
7255 Preanalyze_And_Resolve (N);
7256 return;
7257 end;
7258 end if;
7260 -- The operation name may have been overloaded. Order the actuals
7261 -- according to the formals of the resolved entity, and set the return
7262 -- type to that of the operation.
7264 if Was_Over then
7265 Normalize_Actuals (N, Nam, False, Norm_OK);
7266 pragma Assert (Norm_OK);
7267 Set_Etype (N, Etype (Nam));
7268 end if;
7270 Resolve_Actuals (N, Nam);
7271 Check_Internal_Protected_Use (N, Nam);
7273 -- Create a call reference to the entry
7275 Generate_Reference (Nam, Entry_Name, 's');
7277 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
7278 Check_Potentially_Blocking_Operation (N);
7279 end if;
7281 -- Verify that a procedure call cannot masquerade as an entry
7282 -- call where an entry call is expected.
7284 if Ekind (Nam) = E_Procedure then
7285 if Nkind (Parent (N)) = N_Entry_Call_Alternative
7286 and then N = Entry_Call_Statement (Parent (N))
7287 then
7288 Error_Msg_N ("entry call required in select statement", N);
7290 elsif Nkind (Parent (N)) = N_Triggering_Alternative
7291 and then N = Triggering_Statement (Parent (N))
7292 then
7293 Error_Msg_N ("triggering statement cannot be procedure call", N);
7295 elsif Ekind (Scope (Nam)) = E_Task_Type
7296 and then not In_Open_Scopes (Scope (Nam))
7297 then
7298 Error_Msg_N ("task has no entry with this name", Entry_Name);
7299 end if;
7300 end if;
7302 -- After resolution, entry calls and protected procedure calls are
7303 -- changed into entry calls, for expansion. The structure of the node
7304 -- does not change, so it can safely be done in place. Protected
7305 -- function calls must keep their structure because they are
7306 -- subexpressions.
7308 if Ekind (Nam) /= E_Function then
7310 -- A protected operation that is not a function may modify the
7311 -- corresponding object, and cannot apply to a constant. If this
7312 -- is an internal call, the prefix is the type itself.
7314 if Is_Protected_Type (Scope (Nam))
7315 and then not Is_Variable (Obj)
7316 and then (not Is_Entity_Name (Obj)
7317 or else not Is_Type (Entity (Obj)))
7318 then
7319 Error_Msg_N
7320 ("prefix of protected procedure or entry call must be variable",
7321 Entry_Name);
7322 end if;
7324 Actuals := Parameter_Associations (N);
7325 First_Named := First_Named_Actual (N);
7327 Rewrite (N,
7328 Make_Entry_Call_Statement (Loc,
7329 Name => Entry_Name,
7330 Parameter_Associations => Actuals));
7332 Set_First_Named_Actual (N, First_Named);
7333 Set_Analyzed (N, True);
7335 -- Protected functions can return on the secondary stack, in which
7336 -- case we must trigger the transient scope mechanism.
7338 elsif Expander_Active
7339 and then Requires_Transient_Scope (Etype (Nam))
7340 then
7341 Establish_Transient_Scope (N, Sec_Stack => True);
7342 end if;
7343 end Resolve_Entry_Call;
7345 -------------------------
7346 -- Resolve_Equality_Op --
7347 -------------------------
7349 -- Both arguments must have the same type, and the boolean context does
7350 -- not participate in the resolution. The first pass verifies that the
7351 -- interpretation is not ambiguous, and the type of the left argument is
7352 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
7353 -- are strings or aggregates, allocators, or Null, they are ambiguous even
7354 -- though they carry a single (universal) type. Diagnose this case here.
7356 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
7357 L : constant Node_Id := Left_Opnd (N);
7358 R : constant Node_Id := Right_Opnd (N);
7359 T : Entity_Id := Find_Unique_Type (L, R);
7361 procedure Check_If_Expression (Cond : Node_Id);
7362 -- The resolution rule for if expressions requires that each such must
7363 -- have a unique type. This means that if several dependent expressions
7364 -- are of a non-null anonymous access type, and the context does not
7365 -- impose an expected type (as can be the case in an equality operation)
7366 -- the expression must be rejected.
7368 procedure Explain_Redundancy (N : Node_Id);
7369 -- Attempt to explain the nature of a redundant comparison with True. If
7370 -- the expression N is too complex, this routine issues a general error
7371 -- message.
7373 function Find_Unique_Access_Type return Entity_Id;
7374 -- In the case of allocators and access attributes, the context must
7375 -- provide an indication of the specific access type to be used. If
7376 -- one operand is of such a "generic" access type, check whether there
7377 -- is a specific visible access type that has the same designated type.
7378 -- This is semantically dubious, and of no interest to any real code,
7379 -- but c48008a makes it all worthwhile.
7381 -------------------------
7382 -- Check_If_Expression --
7383 -------------------------
7385 procedure Check_If_Expression (Cond : Node_Id) is
7386 Then_Expr : Node_Id;
7387 Else_Expr : Node_Id;
7389 begin
7390 if Nkind (Cond) = N_If_Expression then
7391 Then_Expr := Next (First (Expressions (Cond)));
7392 Else_Expr := Next (Then_Expr);
7394 if Nkind (Then_Expr) /= N_Null
7395 and then Nkind (Else_Expr) /= N_Null
7396 then
7397 Error_Msg_N ("cannot determine type of if expression", Cond);
7398 end if;
7399 end if;
7400 end Check_If_Expression;
7402 ------------------------
7403 -- Explain_Redundancy --
7404 ------------------------
7406 procedure Explain_Redundancy (N : Node_Id) is
7407 Error : Name_Id;
7408 Val : Node_Id;
7409 Val_Id : Entity_Id;
7411 begin
7412 Val := N;
7414 -- Strip the operand down to an entity
7416 loop
7417 if Nkind (Val) = N_Selected_Component then
7418 Val := Selector_Name (Val);
7419 else
7420 exit;
7421 end if;
7422 end loop;
7424 -- The construct denotes an entity
7426 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
7427 Val_Id := Entity (Val);
7429 -- Do not generate an error message when the comparison is done
7430 -- against the enumeration literal Standard.True.
7432 if Ekind (Val_Id) /= E_Enumeration_Literal then
7434 -- Build a customized error message
7436 Name_Len := 0;
7437 Add_Str_To_Name_Buffer ("?r?");
7439 if Ekind (Val_Id) = E_Component then
7440 Add_Str_To_Name_Buffer ("component ");
7442 elsif Ekind (Val_Id) = E_Constant then
7443 Add_Str_To_Name_Buffer ("constant ");
7445 elsif Ekind (Val_Id) = E_Discriminant then
7446 Add_Str_To_Name_Buffer ("discriminant ");
7448 elsif Is_Formal (Val_Id) then
7449 Add_Str_To_Name_Buffer ("parameter ");
7451 elsif Ekind (Val_Id) = E_Variable then
7452 Add_Str_To_Name_Buffer ("variable ");
7453 end if;
7455 Add_Str_To_Name_Buffer ("& is always True!");
7456 Error := Name_Find;
7458 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
7459 end if;
7461 -- The construct is too complex to disect, issue a general message
7463 else
7464 Error_Msg_N ("?r?expression is always True!", Val);
7465 end if;
7466 end Explain_Redundancy;
7468 -----------------------------
7469 -- Find_Unique_Access_Type --
7470 -----------------------------
7472 function Find_Unique_Access_Type return Entity_Id is
7473 Acc : Entity_Id;
7474 E : Entity_Id;
7475 S : Entity_Id;
7477 begin
7478 if Ekind_In (Etype (R), E_Allocator_Type,
7479 E_Access_Attribute_Type)
7480 then
7481 Acc := Designated_Type (Etype (R));
7483 elsif Ekind_In (Etype (L), E_Allocator_Type,
7484 E_Access_Attribute_Type)
7485 then
7486 Acc := Designated_Type (Etype (L));
7487 else
7488 return Empty;
7489 end if;
7491 S := Current_Scope;
7492 while S /= Standard_Standard loop
7493 E := First_Entity (S);
7494 while Present (E) loop
7495 if Is_Type (E)
7496 and then Is_Access_Type (E)
7497 and then Ekind (E) /= E_Allocator_Type
7498 and then Designated_Type (E) = Base_Type (Acc)
7499 then
7500 return E;
7501 end if;
7503 Next_Entity (E);
7504 end loop;
7506 S := Scope (S);
7507 end loop;
7509 return Empty;
7510 end Find_Unique_Access_Type;
7512 -- Start of processing for Resolve_Equality_Op
7514 begin
7515 Set_Etype (N, Base_Type (Typ));
7516 Generate_Reference (T, N, ' ');
7518 if T = Any_Fixed then
7519 T := Unique_Fixed_Point_Type (L);
7520 end if;
7522 if T /= Any_Type then
7523 if T = Any_String or else
7524 T = Any_Composite or else
7525 T = Any_Character
7526 then
7527 if T = Any_Character then
7528 Ambiguous_Character (L);
7529 else
7530 Error_Msg_N ("ambiguous operands for equality", N);
7531 end if;
7533 Set_Etype (N, Any_Type);
7534 return;
7536 elsif T = Any_Access
7537 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
7538 then
7539 T := Find_Unique_Access_Type;
7541 if No (T) then
7542 Error_Msg_N ("ambiguous operands for equality", N);
7543 Set_Etype (N, Any_Type);
7544 return;
7545 end if;
7547 -- If expressions must have a single type, and if the context does
7548 -- not impose one the dependent expressions cannot be anonymous
7549 -- access types.
7551 -- Why no similar processing for case expressions???
7553 elsif Ada_Version >= Ada_2012
7554 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
7555 E_Anonymous_Access_Subprogram_Type)
7556 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
7557 E_Anonymous_Access_Subprogram_Type)
7558 then
7559 Check_If_Expression (L);
7560 Check_If_Expression (R);
7561 end if;
7563 Resolve (L, T);
7564 Resolve (R, T);
7566 -- In SPARK, equality operators = and /= for array types other than
7567 -- String are only defined when, for each index position, the
7568 -- operands have equal static bounds.
7570 if Is_Array_Type (T) then
7572 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7573 -- operation if not needed.
7575 if Restriction_Check_Required (SPARK_05)
7576 and then Base_Type (T) /= Standard_String
7577 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7578 and then Etype (L) /= Any_Composite -- or else L in error
7579 and then Etype (R) /= Any_Composite -- or else R in error
7580 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
7581 then
7582 Check_SPARK_05_Restriction
7583 ("array types should have matching static bounds", N);
7584 end if;
7585 end if;
7587 -- If the unique type is a class-wide type then it will be expanded
7588 -- into a dispatching call to the predefined primitive. Therefore we
7589 -- check here for potential violation of such restriction.
7591 if Is_Class_Wide_Type (T) then
7592 Check_Restriction (No_Dispatching_Calls, N);
7593 end if;
7595 if Warn_On_Redundant_Constructs
7596 and then Comes_From_Source (N)
7597 and then Comes_From_Source (R)
7598 and then Is_Entity_Name (R)
7599 and then Entity (R) = Standard_True
7600 then
7601 Error_Msg_N -- CODEFIX
7602 ("?r?comparison with True is redundant!", N);
7603 Explain_Redundancy (Original_Node (R));
7604 end if;
7606 Check_Unset_Reference (L);
7607 Check_Unset_Reference (R);
7608 Generate_Operator_Reference (N, T);
7609 Check_Low_Bound_Tested (N);
7611 -- If this is an inequality, it may be the implicit inequality
7612 -- created for a user-defined operation, in which case the corres-
7613 -- ponding equality operation is not intrinsic, and the operation
7614 -- cannot be constant-folded. Else fold.
7616 if Nkind (N) = N_Op_Eq
7617 or else Comes_From_Source (Entity (N))
7618 or else Ekind (Entity (N)) = E_Operator
7619 or else Is_Intrinsic_Subprogram
7620 (Corresponding_Equality (Entity (N)))
7621 then
7622 Analyze_Dimension (N);
7623 Eval_Relational_Op (N);
7625 elsif Nkind (N) = N_Op_Ne
7626 and then Is_Abstract_Subprogram (Entity (N))
7627 then
7628 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7629 end if;
7631 -- Ada 2005: If one operand is an anonymous access type, convert the
7632 -- other operand to it, to ensure that the underlying types match in
7633 -- the back-end. Same for access_to_subprogram, and the conversion
7634 -- verifies that the types are subtype conformant.
7636 -- We apply the same conversion in the case one of the operands is a
7637 -- private subtype of the type of the other.
7639 -- Why the Expander_Active test here ???
7641 if Expander_Active
7642 and then
7643 (Ekind_In (T, E_Anonymous_Access_Type,
7644 E_Anonymous_Access_Subprogram_Type)
7645 or else Is_Private_Type (T))
7646 then
7647 if Etype (L) /= T then
7648 Rewrite (L,
7649 Make_Unchecked_Type_Conversion (Sloc (L),
7650 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7651 Expression => Relocate_Node (L)));
7652 Analyze_And_Resolve (L, T);
7653 end if;
7655 if (Etype (R)) /= T then
7656 Rewrite (R,
7657 Make_Unchecked_Type_Conversion (Sloc (R),
7658 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7659 Expression => Relocate_Node (R)));
7660 Analyze_And_Resolve (R, T);
7661 end if;
7662 end if;
7663 end if;
7664 end Resolve_Equality_Op;
7666 ----------------------------------
7667 -- Resolve_Explicit_Dereference --
7668 ----------------------------------
7670 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
7671 Loc : constant Source_Ptr := Sloc (N);
7672 New_N : Node_Id;
7673 P : constant Node_Id := Prefix (N);
7675 P_Typ : Entity_Id;
7676 -- The candidate prefix type, if overloaded
7678 I : Interp_Index;
7679 It : Interp;
7681 begin
7682 Check_Fully_Declared_Prefix (Typ, P);
7683 P_Typ := Empty;
7685 -- A useful optimization: check whether the dereference denotes an
7686 -- element of a container, and if so rewrite it as a call to the
7687 -- corresponding Element function.
7689 -- Disabled for now, on advice of ARG. A more restricted form of the
7690 -- predicate might be acceptable ???
7692 -- if Is_Container_Element (N) then
7693 -- return;
7694 -- end if;
7696 if Is_Overloaded (P) then
7698 -- Use the context type to select the prefix that has the correct
7699 -- designated type. Keep the first match, which will be the inner-
7700 -- most.
7702 Get_First_Interp (P, I, It);
7704 while Present (It.Typ) loop
7705 if Is_Access_Type (It.Typ)
7706 and then Covers (Typ, Designated_Type (It.Typ))
7707 then
7708 if No (P_Typ) then
7709 P_Typ := It.Typ;
7710 end if;
7712 -- Remove access types that do not match, but preserve access
7713 -- to subprogram interpretations, in case a further dereference
7714 -- is needed (see below).
7716 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
7717 Remove_Interp (I);
7718 end if;
7720 Get_Next_Interp (I, It);
7721 end loop;
7723 if Present (P_Typ) then
7724 Resolve (P, P_Typ);
7725 Set_Etype (N, Designated_Type (P_Typ));
7727 else
7728 -- If no interpretation covers the designated type of the prefix,
7729 -- this is the pathological case where not all implementations of
7730 -- the prefix allow the interpretation of the node as a call. Now
7731 -- that the expected type is known, Remove other interpretations
7732 -- from prefix, rewrite it as a call, and resolve again, so that
7733 -- the proper call node is generated.
7735 Get_First_Interp (P, I, It);
7736 while Present (It.Typ) loop
7737 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
7738 Remove_Interp (I);
7739 end if;
7741 Get_Next_Interp (I, It);
7742 end loop;
7744 New_N :=
7745 Make_Function_Call (Loc,
7746 Name =>
7747 Make_Explicit_Dereference (Loc,
7748 Prefix => P),
7749 Parameter_Associations => New_List);
7751 Save_Interps (N, New_N);
7752 Rewrite (N, New_N);
7753 Analyze_And_Resolve (N, Typ);
7754 return;
7755 end if;
7757 -- If not overloaded, resolve P with its own type
7759 else
7760 Resolve (P);
7761 end if;
7763 if Is_Access_Type (Etype (P)) then
7764 Apply_Access_Check (N);
7765 end if;
7767 -- If the designated type is a packed unconstrained array type, and the
7768 -- explicit dereference is not in the context of an attribute reference,
7769 -- then we must compute and set the actual subtype, since it is needed
7770 -- by Gigi. The reason we exclude the attribute case is that this is
7771 -- handled fine by Gigi, and in fact we use such attributes to build the
7772 -- actual subtype. We also exclude generated code (which builds actual
7773 -- subtypes directly if they are needed).
7775 if Is_Array_Type (Etype (N))
7776 and then Is_Packed (Etype (N))
7777 and then not Is_Constrained (Etype (N))
7778 and then Nkind (Parent (N)) /= N_Attribute_Reference
7779 and then Comes_From_Source (N)
7780 then
7781 Set_Etype (N, Get_Actual_Subtype (N));
7782 end if;
7784 -- Note: No Eval processing is required for an explicit dereference,
7785 -- because such a name can never be static.
7787 end Resolve_Explicit_Dereference;
7789 -------------------------------------
7790 -- Resolve_Expression_With_Actions --
7791 -------------------------------------
7793 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7794 begin
7795 Set_Etype (N, Typ);
7797 -- If N has no actions, and its expression has been constant folded,
7798 -- then rewrite N as just its expression. Note, we can't do this in
7799 -- the general case of Is_Empty_List (Actions (N)) as this would cause
7800 -- Expression (N) to be expanded again.
7802 if Is_Empty_List (Actions (N))
7803 and then Compile_Time_Known_Value (Expression (N))
7804 then
7805 Rewrite (N, Expression (N));
7806 end if;
7807 end Resolve_Expression_With_Actions;
7809 ----------------------------------
7810 -- Resolve_Generalized_Indexing --
7811 ----------------------------------
7813 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
7814 Indexing : constant Node_Id := Generalized_Indexing (N);
7815 Call : Node_Id;
7816 Indices : List_Id;
7817 Pref : Node_Id;
7819 begin
7820 -- In ASIS mode, propagate the information about the indices back to
7821 -- to the original indexing node. The generalized indexing is either
7822 -- a function call, or a dereference of one. The actuals include the
7823 -- prefix of the original node, which is the container expression.
7825 if ASIS_Mode then
7826 Resolve (Indexing, Typ);
7827 Set_Etype (N, Etype (Indexing));
7828 Set_Is_Overloaded (N, False);
7830 Call := Indexing;
7831 while Nkind_In (Call, N_Explicit_Dereference, N_Selected_Component)
7832 loop
7833 Call := Prefix (Call);
7834 end loop;
7836 if Nkind (Call) = N_Function_Call then
7837 Indices := Parameter_Associations (Call);
7838 Pref := Remove_Head (Indices);
7839 Set_Expressions (N, Indices);
7840 Set_Prefix (N, Pref);
7841 end if;
7843 else
7844 Rewrite (N, Indexing);
7845 Resolve (N, Typ);
7846 end if;
7847 end Resolve_Generalized_Indexing;
7849 ---------------------------
7850 -- Resolve_If_Expression --
7851 ---------------------------
7853 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
7854 Condition : constant Node_Id := First (Expressions (N));
7855 Then_Expr : constant Node_Id := Next (Condition);
7856 Else_Expr : Node_Id := Next (Then_Expr);
7857 Else_Typ : Entity_Id;
7858 Then_Typ : Entity_Id;
7860 begin
7861 Resolve (Condition, Any_Boolean);
7862 Resolve (Then_Expr, Typ);
7863 Then_Typ := Etype (Then_Expr);
7865 -- When the "then" expression is of a scalar subtype different from the
7866 -- result subtype, then insert a conversion to ensure the generation of
7867 -- a constraint check. The same is done for the else part below, again
7868 -- comparing subtypes rather than base types.
7870 if Is_Scalar_Type (Then_Typ)
7871 and then Then_Typ /= Typ
7872 then
7873 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
7874 Analyze_And_Resolve (Then_Expr, Typ);
7875 end if;
7877 -- If ELSE expression present, just resolve using the determined type
7879 if Present (Else_Expr) then
7880 Resolve (Else_Expr, Typ);
7881 Else_Typ := Etype (Else_Expr);
7883 if Is_Scalar_Type (Else_Typ)
7884 and then Else_Typ /= Typ
7885 then
7886 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
7887 Analyze_And_Resolve (Else_Expr, Typ);
7888 end if;
7890 -- If no ELSE expression is present, root type must be Standard.Boolean
7891 -- and we provide a Standard.True result converted to the appropriate
7892 -- Boolean type (in case it is a derived boolean type).
7894 elsif Root_Type (Typ) = Standard_Boolean then
7895 Else_Expr :=
7896 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
7897 Analyze_And_Resolve (Else_Expr, Typ);
7898 Append_To (Expressions (N), Else_Expr);
7900 else
7901 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
7902 Append_To (Expressions (N), Error);
7903 end if;
7905 Set_Etype (N, Typ);
7906 Eval_If_Expression (N);
7907 end Resolve_If_Expression;
7909 -------------------------------
7910 -- Resolve_Indexed_Component --
7911 -------------------------------
7913 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7914 Name : constant Node_Id := Prefix (N);
7915 Expr : Node_Id;
7916 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7917 Index : Node_Id;
7919 begin
7920 if Present (Generalized_Indexing (N)) then
7921 Resolve_Generalized_Indexing (N, Typ);
7922 return;
7923 end if;
7925 if Is_Overloaded (Name) then
7927 -- Use the context type to select the prefix that yields the correct
7928 -- component type.
7930 declare
7931 I : Interp_Index;
7932 It : Interp;
7933 I1 : Interp_Index := 0;
7934 P : constant Node_Id := Prefix (N);
7935 Found : Boolean := False;
7937 begin
7938 Get_First_Interp (P, I, It);
7939 while Present (It.Typ) loop
7940 if (Is_Array_Type (It.Typ)
7941 and then Covers (Typ, Component_Type (It.Typ)))
7942 or else (Is_Access_Type (It.Typ)
7943 and then Is_Array_Type (Designated_Type (It.Typ))
7944 and then
7945 Covers
7946 (Typ,
7947 Component_Type (Designated_Type (It.Typ))))
7948 then
7949 if Found then
7950 It := Disambiguate (P, I1, I, Any_Type);
7952 if It = No_Interp then
7953 Error_Msg_N ("ambiguous prefix for indexing", N);
7954 Set_Etype (N, Typ);
7955 return;
7957 else
7958 Found := True;
7959 Array_Type := It.Typ;
7960 I1 := I;
7961 end if;
7963 else
7964 Found := True;
7965 Array_Type := It.Typ;
7966 I1 := I;
7967 end if;
7968 end if;
7970 Get_Next_Interp (I, It);
7971 end loop;
7972 end;
7974 else
7975 Array_Type := Etype (Name);
7976 end if;
7978 Resolve (Name, Array_Type);
7979 Array_Type := Get_Actual_Subtype_If_Available (Name);
7981 -- If prefix is access type, dereference to get real array type.
7982 -- Note: we do not apply an access check because the expander always
7983 -- introduces an explicit dereference, and the check will happen there.
7985 if Is_Access_Type (Array_Type) then
7986 Array_Type := Designated_Type (Array_Type);
7987 end if;
7989 -- If name was overloaded, set component type correctly now
7990 -- If a misplaced call to an entry family (which has no index types)
7991 -- return. Error will be diagnosed from calling context.
7993 if Is_Array_Type (Array_Type) then
7994 Set_Etype (N, Component_Type (Array_Type));
7995 else
7996 return;
7997 end if;
7999 Index := First_Index (Array_Type);
8000 Expr := First (Expressions (N));
8002 -- The prefix may have resolved to a string literal, in which case its
8003 -- etype has a special representation. This is only possible currently
8004 -- if the prefix is a static concatenation, written in functional
8005 -- notation.
8007 if Ekind (Array_Type) = E_String_Literal_Subtype then
8008 Resolve (Expr, Standard_Positive);
8010 else
8011 while Present (Index) and Present (Expr) loop
8012 Resolve (Expr, Etype (Index));
8013 Check_Unset_Reference (Expr);
8015 if Is_Scalar_Type (Etype (Expr)) then
8016 Apply_Scalar_Range_Check (Expr, Etype (Index));
8017 else
8018 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
8019 end if;
8021 Next_Index (Index);
8022 Next (Expr);
8023 end loop;
8024 end if;
8026 Analyze_Dimension (N);
8028 -- Do not generate the warning on suspicious index if we are analyzing
8029 -- package Ada.Tags; otherwise we will report the warning with the
8030 -- Prims_Ptr field of the dispatch table.
8032 if Scope (Etype (Prefix (N))) = Standard_Standard
8033 or else not
8034 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
8035 Ada_Tags)
8036 then
8037 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
8038 Eval_Indexed_Component (N);
8039 end if;
8041 -- If the array type is atomic, and the component is not atomic, then
8042 -- this is worth a warning, since we have a situation where the access
8043 -- to the component may cause extra read/writes of the atomic array
8044 -- object, or partial word accesses, which could be unexpected.
8046 if Nkind (N) = N_Indexed_Component
8047 and then Is_Atomic_Ref_With_Address (N)
8048 and then not (Has_Atomic_Components (Array_Type)
8049 or else (Is_Entity_Name (Prefix (N))
8050 and then Has_Atomic_Components
8051 (Entity (Prefix (N)))))
8052 and then not Is_Atomic (Component_Type (Array_Type))
8053 then
8054 Error_Msg_N ("??access to non-atomic component of atomic array",
8055 Prefix (N));
8056 Error_Msg_N ("??\may cause unexpected accesses to atomic object",
8057 Prefix (N));
8058 end if;
8059 end Resolve_Indexed_Component;
8061 -----------------------------
8062 -- Resolve_Integer_Literal --
8063 -----------------------------
8065 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
8066 begin
8067 Set_Etype (N, Typ);
8068 Eval_Integer_Literal (N);
8069 end Resolve_Integer_Literal;
8071 --------------------------------
8072 -- Resolve_Intrinsic_Operator --
8073 --------------------------------
8075 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
8076 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8077 Op : Entity_Id;
8078 Arg1 : Node_Id;
8079 Arg2 : Node_Id;
8081 function Convert_Operand (Opnd : Node_Id) return Node_Id;
8082 -- If the operand is a literal, it cannot be the expression in a
8083 -- conversion. Use a qualified expression instead.
8085 function Convert_Operand (Opnd : Node_Id) return Node_Id is
8086 Loc : constant Source_Ptr := Sloc (Opnd);
8087 Res : Node_Id;
8088 begin
8089 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
8090 Res :=
8091 Make_Qualified_Expression (Loc,
8092 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
8093 Expression => Relocate_Node (Opnd));
8094 Analyze (Res);
8096 else
8097 Res := Unchecked_Convert_To (Btyp, Opnd);
8098 end if;
8100 return Res;
8101 end Convert_Operand;
8103 -- Start of processing for Resolve_Intrinsic_Operator
8105 begin
8106 -- We must preserve the original entity in a generic setting, so that
8107 -- the legality of the operation can be verified in an instance.
8109 if not Expander_Active then
8110 return;
8111 end if;
8113 Op := Entity (N);
8114 while Scope (Op) /= Standard_Standard loop
8115 Op := Homonym (Op);
8116 pragma Assert (Present (Op));
8117 end loop;
8119 Set_Entity (N, Op);
8120 Set_Is_Overloaded (N, False);
8122 -- If the result or operand types are private, rewrite with unchecked
8123 -- conversions on the operands and the result, to expose the proper
8124 -- underlying numeric type.
8126 if Is_Private_Type (Typ)
8127 or else Is_Private_Type (Etype (Left_Opnd (N)))
8128 or else Is_Private_Type (Etype (Right_Opnd (N)))
8129 then
8130 Arg1 := Convert_Operand (Left_Opnd (N));
8131 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
8132 -- What on earth is this commented out fragment of code???
8134 if Nkind (N) = N_Op_Expon then
8135 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
8136 else
8137 Arg2 := Convert_Operand (Right_Opnd (N));
8138 end if;
8140 if Nkind (Arg1) = N_Type_Conversion then
8141 Save_Interps (Left_Opnd (N), Expression (Arg1));
8142 end if;
8144 if Nkind (Arg2) = N_Type_Conversion then
8145 Save_Interps (Right_Opnd (N), Expression (Arg2));
8146 end if;
8148 Set_Left_Opnd (N, Arg1);
8149 Set_Right_Opnd (N, Arg2);
8151 Set_Etype (N, Btyp);
8152 Rewrite (N, Unchecked_Convert_To (Typ, N));
8153 Resolve (N, Typ);
8155 elsif Typ /= Etype (Left_Opnd (N))
8156 or else Typ /= Etype (Right_Opnd (N))
8157 then
8158 -- Add explicit conversion where needed, and save interpretations in
8159 -- case operands are overloaded.
8161 Arg1 := Convert_To (Typ, Left_Opnd (N));
8162 Arg2 := Convert_To (Typ, Right_Opnd (N));
8164 if Nkind (Arg1) = N_Type_Conversion then
8165 Save_Interps (Left_Opnd (N), Expression (Arg1));
8166 else
8167 Save_Interps (Left_Opnd (N), Arg1);
8168 end if;
8170 if Nkind (Arg2) = N_Type_Conversion then
8171 Save_Interps (Right_Opnd (N), Expression (Arg2));
8172 else
8173 Save_Interps (Right_Opnd (N), Arg2);
8174 end if;
8176 Rewrite (Left_Opnd (N), Arg1);
8177 Rewrite (Right_Opnd (N), Arg2);
8178 Analyze (Arg1);
8179 Analyze (Arg2);
8180 Resolve_Arithmetic_Op (N, Typ);
8182 else
8183 Resolve_Arithmetic_Op (N, Typ);
8184 end if;
8185 end Resolve_Intrinsic_Operator;
8187 --------------------------------------
8188 -- Resolve_Intrinsic_Unary_Operator --
8189 --------------------------------------
8191 procedure Resolve_Intrinsic_Unary_Operator
8192 (N : Node_Id;
8193 Typ : Entity_Id)
8195 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8196 Op : Entity_Id;
8197 Arg2 : Node_Id;
8199 begin
8200 Op := Entity (N);
8201 while Scope (Op) /= Standard_Standard loop
8202 Op := Homonym (Op);
8203 pragma Assert (Present (Op));
8204 end loop;
8206 Set_Entity (N, Op);
8208 if Is_Private_Type (Typ) then
8209 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
8210 Save_Interps (Right_Opnd (N), Expression (Arg2));
8212 Set_Right_Opnd (N, Arg2);
8214 Set_Etype (N, Btyp);
8215 Rewrite (N, Unchecked_Convert_To (Typ, N));
8216 Resolve (N, Typ);
8218 else
8219 Resolve_Unary_Op (N, Typ);
8220 end if;
8221 end Resolve_Intrinsic_Unary_Operator;
8223 ------------------------
8224 -- Resolve_Logical_Op --
8225 ------------------------
8227 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
8228 B_Typ : Entity_Id;
8230 begin
8231 Check_No_Direct_Boolean_Operators (N);
8233 -- Predefined operations on scalar types yield the base type. On the
8234 -- other hand, logical operations on arrays yield the type of the
8235 -- arguments (and the context).
8237 if Is_Array_Type (Typ) then
8238 B_Typ := Typ;
8239 else
8240 B_Typ := Base_Type (Typ);
8241 end if;
8243 -- The following test is required because the operands of the operation
8244 -- may be literals, in which case the resulting type appears to be
8245 -- compatible with a signed integer type, when in fact it is compatible
8246 -- only with modular types. If the context itself is universal, the
8247 -- operation is illegal.
8249 if not Valid_Boolean_Arg (Typ) then
8250 Error_Msg_N ("invalid context for logical operation", N);
8251 Set_Etype (N, Any_Type);
8252 return;
8254 elsif Typ = Any_Modular then
8255 Error_Msg_N
8256 ("no modular type available in this context", N);
8257 Set_Etype (N, Any_Type);
8258 return;
8260 elsif Is_Modular_Integer_Type (Typ)
8261 and then Etype (Left_Opnd (N)) = Universal_Integer
8262 and then Etype (Right_Opnd (N)) = Universal_Integer
8263 then
8264 Check_For_Visible_Operator (N, B_Typ);
8265 end if;
8267 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
8268 -- is active and the result type is standard Boolean (do not mess with
8269 -- ops that return a nonstandard Boolean type, because something strange
8270 -- is going on).
8272 -- Note: you might expect this replacement to be done during expansion,
8273 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
8274 -- is used, no part of the right operand of an "and" or "or" operator
8275 -- should be executed if the left operand would short-circuit the
8276 -- evaluation of the corresponding "and then" or "or else". If we left
8277 -- the replacement to expansion time, then run-time checks associated
8278 -- with such operands would be evaluated unconditionally, due to being
8279 -- before the condition prior to the rewriting as short-circuit forms
8280 -- during expansion.
8282 if Short_Circuit_And_Or
8283 and then B_Typ = Standard_Boolean
8284 and then Nkind_In (N, N_Op_And, N_Op_Or)
8285 then
8286 if Nkind (N) = N_Op_And then
8287 Rewrite (N,
8288 Make_And_Then (Sloc (N),
8289 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8290 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8291 Analyze_And_Resolve (N, B_Typ);
8293 -- Case of OR changed to OR ELSE
8295 else
8296 Rewrite (N,
8297 Make_Or_Else (Sloc (N),
8298 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8299 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8300 Analyze_And_Resolve (N, B_Typ);
8301 end if;
8303 -- Return now, since analysis of the rewritten ops will take care of
8304 -- other reference bookkeeping and expression folding.
8306 return;
8307 end if;
8309 Resolve (Left_Opnd (N), B_Typ);
8310 Resolve (Right_Opnd (N), B_Typ);
8312 Check_Unset_Reference (Left_Opnd (N));
8313 Check_Unset_Reference (Right_Opnd (N));
8315 Set_Etype (N, B_Typ);
8316 Generate_Operator_Reference (N, B_Typ);
8317 Eval_Logical_Op (N);
8319 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
8320 -- only when both operands have same static lower and higher bounds. Of
8321 -- course the types have to match, so only check if operands are
8322 -- compatible and the node itself has no errors.
8324 if Is_Array_Type (B_Typ)
8325 and then Nkind (N) in N_Binary_Op
8326 then
8327 declare
8328 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
8329 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
8331 begin
8332 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8333 -- operation if not needed.
8335 if Restriction_Check_Required (SPARK_05)
8336 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
8337 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
8338 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
8339 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
8340 then
8341 Check_SPARK_05_Restriction
8342 ("array types should have matching static bounds", N);
8343 end if;
8344 end;
8345 end if;
8347 Check_Function_Writable_Actuals (N);
8348 end Resolve_Logical_Op;
8350 ---------------------------
8351 -- Resolve_Membership_Op --
8352 ---------------------------
8354 -- The context can only be a boolean type, and does not determine the
8355 -- arguments. Arguments should be unambiguous, but the preference rule for
8356 -- universal types applies.
8358 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
8359 pragma Warnings (Off, Typ);
8361 L : constant Node_Id := Left_Opnd (N);
8362 R : constant Node_Id := Right_Opnd (N);
8363 T : Entity_Id;
8365 procedure Resolve_Set_Membership;
8366 -- Analysis has determined a unique type for the left operand. Use it to
8367 -- resolve the disjuncts.
8369 ----------------------------
8370 -- Resolve_Set_Membership --
8371 ----------------------------
8373 procedure Resolve_Set_Membership is
8374 Alt : Node_Id;
8375 Ltyp : constant Entity_Id := Etype (L);
8377 begin
8378 Resolve (L, Ltyp);
8380 Alt := First (Alternatives (N));
8381 while Present (Alt) loop
8383 -- Alternative is an expression, a range
8384 -- or a subtype mark.
8386 if not Is_Entity_Name (Alt)
8387 or else not Is_Type (Entity (Alt))
8388 then
8389 Resolve (Alt, Ltyp);
8390 end if;
8392 Next (Alt);
8393 end loop;
8395 -- Check for duplicates for discrete case
8397 if Is_Discrete_Type (Ltyp) then
8398 declare
8399 type Ent is record
8400 Alt : Node_Id;
8401 Val : Uint;
8402 end record;
8404 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
8405 Nalts : Nat;
8407 begin
8408 -- Loop checking duplicates. This is quadratic, but giant sets
8409 -- are unlikely in this context so it's a reasonable choice.
8411 Nalts := 0;
8412 Alt := First (Alternatives (N));
8413 while Present (Alt) loop
8414 if Is_OK_Static_Expression (Alt)
8415 and then (Nkind_In (Alt, N_Integer_Literal,
8416 N_Character_Literal)
8417 or else Nkind (Alt) in N_Has_Entity)
8418 then
8419 Nalts := Nalts + 1;
8420 Alts (Nalts) := (Alt, Expr_Value (Alt));
8422 for J in 1 .. Nalts - 1 loop
8423 if Alts (J).Val = Alts (Nalts).Val then
8424 Error_Msg_Sloc := Sloc (Alts (J).Alt);
8425 Error_Msg_N ("duplicate of value given#??", Alt);
8426 end if;
8427 end loop;
8428 end if;
8430 Alt := Next (Alt);
8431 end loop;
8432 end;
8433 end if;
8434 end Resolve_Set_Membership;
8436 -- Start of processing for Resolve_Membership_Op
8438 begin
8439 if L = Error or else R = Error then
8440 return;
8441 end if;
8443 if Present (Alternatives (N)) then
8444 Resolve_Set_Membership;
8445 goto SM_Exit;
8447 elsif not Is_Overloaded (R)
8448 and then
8449 (Etype (R) = Universal_Integer
8450 or else
8451 Etype (R) = Universal_Real)
8452 and then Is_Overloaded (L)
8453 then
8454 T := Etype (R);
8456 -- Ada 2005 (AI-251): Support the following case:
8458 -- type I is interface;
8459 -- type T is tagged ...
8461 -- function Test (O : I'Class) is
8462 -- begin
8463 -- return O in T'Class.
8464 -- end Test;
8466 -- In this case we have nothing else to do. The membership test will be
8467 -- done at run time.
8469 elsif Ada_Version >= Ada_2005
8470 and then Is_Class_Wide_Type (Etype (L))
8471 and then Is_Interface (Etype (L))
8472 and then Is_Class_Wide_Type (Etype (R))
8473 and then not Is_Interface (Etype (R))
8474 then
8475 return;
8476 else
8477 T := Intersect_Types (L, R);
8478 end if;
8480 -- If mixed-mode operations are present and operands are all literal,
8481 -- the only interpretation involves Duration, which is probably not
8482 -- the intention of the programmer.
8484 if T = Any_Fixed then
8485 T := Unique_Fixed_Point_Type (N);
8487 if T = Any_Type then
8488 return;
8489 end if;
8490 end if;
8492 Resolve (L, T);
8493 Check_Unset_Reference (L);
8495 if Nkind (R) = N_Range
8496 and then not Is_Scalar_Type (T)
8497 then
8498 Error_Msg_N ("scalar type required for range", R);
8499 end if;
8501 if Is_Entity_Name (R) then
8502 Freeze_Expression (R);
8503 else
8504 Resolve (R, T);
8505 Check_Unset_Reference (R);
8506 end if;
8508 -- Here after resolving membership operation
8510 <<SM_Exit>>
8512 Eval_Membership_Op (N);
8513 Check_Function_Writable_Actuals (N);
8514 end Resolve_Membership_Op;
8516 ------------------
8517 -- Resolve_Null --
8518 ------------------
8520 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
8521 Loc : constant Source_Ptr := Sloc (N);
8523 begin
8524 -- Handle restriction against anonymous null access values This
8525 -- restriction can be turned off using -gnatdj.
8527 -- Ada 2005 (AI-231): Remove restriction
8529 if Ada_Version < Ada_2005
8530 and then not Debug_Flag_J
8531 and then Ekind (Typ) = E_Anonymous_Access_Type
8532 and then Comes_From_Source (N)
8533 then
8534 -- In the common case of a call which uses an explicitly null value
8535 -- for an access parameter, give specialized error message.
8537 if Nkind (Parent (N)) in N_Subprogram_Call then
8538 Error_Msg_N
8539 ("null is not allowed as argument for an access parameter", N);
8541 -- Standard message for all other cases (are there any?)
8543 else
8544 Error_Msg_N
8545 ("null cannot be of an anonymous access type", N);
8546 end if;
8547 end if;
8549 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
8550 -- assignment to a null-excluding object
8552 if Ada_Version >= Ada_2005
8553 and then Can_Never_Be_Null (Typ)
8554 and then Nkind (Parent (N)) = N_Assignment_Statement
8555 then
8556 if not Inside_Init_Proc then
8557 Insert_Action
8558 (Compile_Time_Constraint_Error (N,
8559 "(Ada 2005) null not allowed in null-excluding objects??"),
8560 Make_Raise_Constraint_Error (Loc,
8561 Reason => CE_Access_Check_Failed));
8562 else
8563 Insert_Action (N,
8564 Make_Raise_Constraint_Error (Loc,
8565 Reason => CE_Access_Check_Failed));
8566 end if;
8567 end if;
8569 -- In a distributed context, null for a remote access to subprogram may
8570 -- need to be replaced with a special record aggregate. In this case,
8571 -- return after having done the transformation.
8573 if (Ekind (Typ) = E_Record_Type
8574 or else Is_Remote_Access_To_Subprogram_Type (Typ))
8575 and then Remote_AST_Null_Value (N, Typ)
8576 then
8577 return;
8578 end if;
8580 -- The null literal takes its type from the context
8582 Set_Etype (N, Typ);
8583 end Resolve_Null;
8585 -----------------------
8586 -- Resolve_Op_Concat --
8587 -----------------------
8589 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
8591 -- We wish to avoid deep recursion, because concatenations are often
8592 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
8593 -- operands nonrecursively until we find something that is not a simple
8594 -- concatenation (A in this case). We resolve that, and then walk back
8595 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
8596 -- to do the rest of the work at each level. The Parent pointers allow
8597 -- us to avoid recursion, and thus avoid running out of memory. See also
8598 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
8600 NN : Node_Id := N;
8601 Op1 : Node_Id;
8603 begin
8604 -- The following code is equivalent to:
8606 -- Resolve_Op_Concat_First (NN, Typ);
8607 -- Resolve_Op_Concat_Arg (N, ...);
8608 -- Resolve_Op_Concat_Rest (N, Typ);
8610 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
8611 -- operand is a concatenation.
8613 -- Walk down left operands
8615 loop
8616 Resolve_Op_Concat_First (NN, Typ);
8617 Op1 := Left_Opnd (NN);
8618 exit when not (Nkind (Op1) = N_Op_Concat
8619 and then not Is_Array_Type (Component_Type (Typ))
8620 and then Entity (Op1) = Entity (NN));
8621 NN := Op1;
8622 end loop;
8624 -- Now (given the above example) NN is A&B and Op1 is A
8626 -- First resolve Op1 ...
8628 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
8630 -- ... then walk NN back up until we reach N (where we started), calling
8631 -- Resolve_Op_Concat_Rest along the way.
8633 loop
8634 Resolve_Op_Concat_Rest (NN, Typ);
8635 exit when NN = N;
8636 NN := Parent (NN);
8637 end loop;
8639 if Base_Type (Etype (N)) /= Standard_String then
8640 Check_SPARK_05_Restriction
8641 ("result of concatenation should have type String", N);
8642 end if;
8643 end Resolve_Op_Concat;
8645 ---------------------------
8646 -- Resolve_Op_Concat_Arg --
8647 ---------------------------
8649 procedure Resolve_Op_Concat_Arg
8650 (N : Node_Id;
8651 Arg : Node_Id;
8652 Typ : Entity_Id;
8653 Is_Comp : Boolean)
8655 Btyp : constant Entity_Id := Base_Type (Typ);
8656 Ctyp : constant Entity_Id := Component_Type (Typ);
8658 begin
8659 if In_Instance then
8660 if Is_Comp
8661 or else (not Is_Overloaded (Arg)
8662 and then Etype (Arg) /= Any_Composite
8663 and then Covers (Ctyp, Etype (Arg)))
8664 then
8665 Resolve (Arg, Ctyp);
8666 else
8667 Resolve (Arg, Btyp);
8668 end if;
8670 -- If both Array & Array and Array & Component are visible, there is a
8671 -- potential ambiguity that must be reported.
8673 elsif Has_Compatible_Type (Arg, Ctyp) then
8674 if Nkind (Arg) = N_Aggregate
8675 and then Is_Composite_Type (Ctyp)
8676 then
8677 if Is_Private_Type (Ctyp) then
8678 Resolve (Arg, Btyp);
8680 -- If the operation is user-defined and not overloaded use its
8681 -- profile. The operation may be a renaming, in which case it has
8682 -- been rewritten, and we want the original profile.
8684 elsif not Is_Overloaded (N)
8685 and then Comes_From_Source (Entity (Original_Node (N)))
8686 and then Ekind (Entity (Original_Node (N))) = E_Function
8687 then
8688 Resolve (Arg,
8689 Etype
8690 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
8691 return;
8693 -- Otherwise an aggregate may match both the array type and the
8694 -- component type.
8696 else
8697 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
8698 Set_Etype (Arg, Any_Type);
8699 end if;
8701 else
8702 if Is_Overloaded (Arg)
8703 and then Has_Compatible_Type (Arg, Typ)
8704 and then Etype (Arg) /= Any_Type
8705 then
8706 declare
8707 I : Interp_Index;
8708 It : Interp;
8709 Func : Entity_Id;
8711 begin
8712 Get_First_Interp (Arg, I, It);
8713 Func := It.Nam;
8714 Get_Next_Interp (I, It);
8716 -- Special-case the error message when the overloading is
8717 -- caused by a function that yields an array and can be
8718 -- called without parameters.
8720 if It.Nam = Func then
8721 Error_Msg_Sloc := Sloc (Func);
8722 Error_Msg_N ("ambiguous call to function#", Arg);
8723 Error_Msg_NE
8724 ("\\interpretation as call yields&", Arg, Typ);
8725 Error_Msg_NE
8726 ("\\interpretation as indexing of call yields&",
8727 Arg, Component_Type (Typ));
8729 else
8730 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
8732 Get_First_Interp (Arg, I, It);
8733 while Present (It.Nam) loop
8734 Error_Msg_Sloc := Sloc (It.Nam);
8736 if Base_Type (It.Typ) = Btyp
8737 or else
8738 Base_Type (It.Typ) = Base_Type (Ctyp)
8739 then
8740 Error_Msg_N -- CODEFIX
8741 ("\\possible interpretation#", Arg);
8742 end if;
8744 Get_Next_Interp (I, It);
8745 end loop;
8746 end if;
8747 end;
8748 end if;
8750 Resolve (Arg, Component_Type (Typ));
8752 if Nkind (Arg) = N_String_Literal then
8753 Set_Etype (Arg, Component_Type (Typ));
8754 end if;
8756 if Arg = Left_Opnd (N) then
8757 Set_Is_Component_Left_Opnd (N);
8758 else
8759 Set_Is_Component_Right_Opnd (N);
8760 end if;
8761 end if;
8763 else
8764 Resolve (Arg, Btyp);
8765 end if;
8767 -- Concatenation is restricted in SPARK: each operand must be either a
8768 -- string literal, the name of a string constant, a static character or
8769 -- string expression, or another concatenation. Arg cannot be a
8770 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
8771 -- separately on each final operand, past concatenation operations.
8773 if Is_Character_Type (Etype (Arg)) then
8774 if not Is_OK_Static_Expression (Arg) then
8775 Check_SPARK_05_Restriction
8776 ("character operand for concatenation should be static", Arg);
8777 end if;
8779 elsif Is_String_Type (Etype (Arg)) then
8780 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
8781 and then Is_Constant_Object (Entity (Arg)))
8782 and then not Is_OK_Static_Expression (Arg)
8783 then
8784 Check_SPARK_05_Restriction
8785 ("string operand for concatenation should be static", Arg);
8786 end if;
8788 -- Do not issue error on an operand that is neither a character nor a
8789 -- string, as the error is issued in Resolve_Op_Concat.
8791 else
8792 null;
8793 end if;
8795 Check_Unset_Reference (Arg);
8796 end Resolve_Op_Concat_Arg;
8798 -----------------------------
8799 -- Resolve_Op_Concat_First --
8800 -----------------------------
8802 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
8803 Btyp : constant Entity_Id := Base_Type (Typ);
8804 Op1 : constant Node_Id := Left_Opnd (N);
8805 Op2 : constant Node_Id := Right_Opnd (N);
8807 begin
8808 -- The parser folds an enormous sequence of concatenations of string
8809 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
8810 -- in the right operand. If the expression resolves to a predefined "&"
8811 -- operator, all is well. Otherwise, the parser's folding is wrong, so
8812 -- we give an error. See P_Simple_Expression in Par.Ch4.
8814 if Nkind (Op2) = N_String_Literal
8815 and then Is_Folded_In_Parser (Op2)
8816 and then Ekind (Entity (N)) = E_Function
8817 then
8818 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
8819 and then String_Length (Strval (Op1)) = 0);
8820 Error_Msg_N ("too many user-defined concatenations", N);
8821 return;
8822 end if;
8824 Set_Etype (N, Btyp);
8826 if Is_Limited_Composite (Btyp) then
8827 Error_Msg_N ("concatenation not available for limited array", N);
8828 Explain_Limited_Type (Btyp, N);
8829 end if;
8830 end Resolve_Op_Concat_First;
8832 ----------------------------
8833 -- Resolve_Op_Concat_Rest --
8834 ----------------------------
8836 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
8837 Op1 : constant Node_Id := Left_Opnd (N);
8838 Op2 : constant Node_Id := Right_Opnd (N);
8840 begin
8841 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
8843 Generate_Operator_Reference (N, Typ);
8845 if Is_String_Type (Typ) then
8846 Eval_Concatenation (N);
8847 end if;
8849 -- If this is not a static concatenation, but the result is a string
8850 -- type (and not an array of strings) ensure that static string operands
8851 -- have their subtypes properly constructed.
8853 if Nkind (N) /= N_String_Literal
8854 and then Is_Character_Type (Component_Type (Typ))
8855 then
8856 Set_String_Literal_Subtype (Op1, Typ);
8857 Set_String_Literal_Subtype (Op2, Typ);
8858 end if;
8859 end Resolve_Op_Concat_Rest;
8861 ----------------------
8862 -- Resolve_Op_Expon --
8863 ----------------------
8865 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
8866 B_Typ : constant Entity_Id := Base_Type (Typ);
8868 begin
8869 -- Catch attempts to do fixed-point exponentiation with universal
8870 -- operands, which is a case where the illegality is not caught during
8871 -- normal operator analysis. This is not done in preanalysis mode
8872 -- since the tree is not fully decorated during preanalysis.
8874 if Full_Analysis then
8875 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
8876 Error_Msg_N ("exponentiation not available for fixed point", N);
8877 return;
8879 elsif Nkind (Parent (N)) in N_Op
8880 and then Is_Fixed_Point_Type (Etype (Parent (N)))
8881 and then Etype (N) = Universal_Real
8882 and then Comes_From_Source (N)
8883 then
8884 Error_Msg_N ("exponentiation not available for fixed point", N);
8885 return;
8886 end if;
8887 end if;
8889 if Comes_From_Source (N)
8890 and then Ekind (Entity (N)) = E_Function
8891 and then Is_Imported (Entity (N))
8892 and then Is_Intrinsic_Subprogram (Entity (N))
8893 then
8894 Resolve_Intrinsic_Operator (N, Typ);
8895 return;
8896 end if;
8898 if Etype (Left_Opnd (N)) = Universal_Integer
8899 or else Etype (Left_Opnd (N)) = Universal_Real
8900 then
8901 Check_For_Visible_Operator (N, B_Typ);
8902 end if;
8904 -- We do the resolution using the base type, because intermediate values
8905 -- in expressions are always of the base type, not a subtype of it.
8907 Resolve (Left_Opnd (N), B_Typ);
8908 Resolve (Right_Opnd (N), Standard_Integer);
8910 -- For integer types, right argument must be in Natural range
8912 if Is_Integer_Type (Typ) then
8913 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
8914 end if;
8916 Check_Unset_Reference (Left_Opnd (N));
8917 Check_Unset_Reference (Right_Opnd (N));
8919 Set_Etype (N, B_Typ);
8920 Generate_Operator_Reference (N, B_Typ);
8922 Analyze_Dimension (N);
8924 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
8925 -- Evaluate the exponentiation operator for dimensioned type
8927 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
8928 else
8929 Eval_Op_Expon (N);
8930 end if;
8932 -- Set overflow checking bit. Much cleverer code needed here eventually
8933 -- and perhaps the Resolve routines should be separated for the various
8934 -- arithmetic operations, since they will need different processing. ???
8936 if Nkind (N) in N_Op then
8937 if not Overflow_Checks_Suppressed (Etype (N)) then
8938 Enable_Overflow_Check (N);
8939 end if;
8940 end if;
8941 end Resolve_Op_Expon;
8943 --------------------
8944 -- Resolve_Op_Not --
8945 --------------------
8947 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8948 B_Typ : Entity_Id;
8950 function Parent_Is_Boolean return Boolean;
8951 -- This function determines if the parent node is a boolean operator or
8952 -- operation (comparison op, membership test, or short circuit form) and
8953 -- the not in question is the left operand of this operation. Note that
8954 -- if the not is in parens, then false is returned.
8956 -----------------------
8957 -- Parent_Is_Boolean --
8958 -----------------------
8960 function Parent_Is_Boolean return Boolean is
8961 begin
8962 if Paren_Count (N) /= 0 then
8963 return False;
8965 else
8966 case Nkind (Parent (N)) is
8967 when N_Op_And |
8968 N_Op_Eq |
8969 N_Op_Ge |
8970 N_Op_Gt |
8971 N_Op_Le |
8972 N_Op_Lt |
8973 N_Op_Ne |
8974 N_Op_Or |
8975 N_Op_Xor |
8976 N_In |
8977 N_Not_In |
8978 N_And_Then |
8979 N_Or_Else =>
8981 return Left_Opnd (Parent (N)) = N;
8983 when others =>
8984 return False;
8985 end case;
8986 end if;
8987 end Parent_Is_Boolean;
8989 -- Start of processing for Resolve_Op_Not
8991 begin
8992 -- Predefined operations on scalar types yield the base type. On the
8993 -- other hand, logical operations on arrays yield the type of the
8994 -- arguments (and the context).
8996 if Is_Array_Type (Typ) then
8997 B_Typ := Typ;
8998 else
8999 B_Typ := Base_Type (Typ);
9000 end if;
9002 -- Straightforward case of incorrect arguments
9004 if not Valid_Boolean_Arg (Typ) then
9005 Error_Msg_N ("invalid operand type for operator&", N);
9006 Set_Etype (N, Any_Type);
9007 return;
9009 -- Special case of probable missing parens
9011 elsif Typ = Universal_Integer or else Typ = Any_Modular then
9012 if Parent_Is_Boolean then
9013 Error_Msg_N
9014 ("operand of not must be enclosed in parentheses",
9015 Right_Opnd (N));
9016 else
9017 Error_Msg_N
9018 ("no modular type available in this context", N);
9019 end if;
9021 Set_Etype (N, Any_Type);
9022 return;
9024 -- OK resolution of NOT
9026 else
9027 -- Warn if non-boolean types involved. This is a case like not a < b
9028 -- where a and b are modular, where we will get (not a) < b and most
9029 -- likely not (a < b) was intended.
9031 if Warn_On_Questionable_Missing_Parens
9032 and then not Is_Boolean_Type (Typ)
9033 and then Parent_Is_Boolean
9034 then
9035 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
9036 end if;
9038 -- Warn on double negation if checking redundant constructs
9040 if Warn_On_Redundant_Constructs
9041 and then Comes_From_Source (N)
9042 and then Comes_From_Source (Right_Opnd (N))
9043 and then Root_Type (Typ) = Standard_Boolean
9044 and then Nkind (Right_Opnd (N)) = N_Op_Not
9045 then
9046 Error_Msg_N ("redundant double negation?r?", N);
9047 end if;
9049 -- Complete resolution and evaluation of NOT
9051 Resolve (Right_Opnd (N), B_Typ);
9052 Check_Unset_Reference (Right_Opnd (N));
9053 Set_Etype (N, B_Typ);
9054 Generate_Operator_Reference (N, B_Typ);
9055 Eval_Op_Not (N);
9056 end if;
9057 end Resolve_Op_Not;
9059 -----------------------------
9060 -- Resolve_Operator_Symbol --
9061 -----------------------------
9063 -- Nothing to be done, all resolved already
9065 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
9066 pragma Warnings (Off, N);
9067 pragma Warnings (Off, Typ);
9069 begin
9070 null;
9071 end Resolve_Operator_Symbol;
9073 ----------------------------------
9074 -- Resolve_Qualified_Expression --
9075 ----------------------------------
9077 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
9078 pragma Warnings (Off, Typ);
9080 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
9081 Expr : constant Node_Id := Expression (N);
9083 begin
9084 Resolve (Expr, Target_Typ);
9086 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9087 -- operation if not needed.
9089 if Restriction_Check_Required (SPARK_05)
9090 and then Is_Array_Type (Target_Typ)
9091 and then Is_Array_Type (Etype (Expr))
9092 and then Etype (Expr) /= Any_Composite -- or else Expr in error
9093 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
9094 then
9095 Check_SPARK_05_Restriction
9096 ("array types should have matching static bounds", N);
9097 end if;
9099 -- A qualified expression requires an exact match of the type, class-
9100 -- wide matching is not allowed. However, if the qualifying type is
9101 -- specific and the expression has a class-wide type, it may still be
9102 -- okay, since it can be the result of the expansion of a call to a
9103 -- dispatching function, so we also have to check class-wideness of the
9104 -- type of the expression's original node.
9106 if (Is_Class_Wide_Type (Target_Typ)
9107 or else
9108 (Is_Class_Wide_Type (Etype (Expr))
9109 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
9110 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
9111 then
9112 Wrong_Type (Expr, Target_Typ);
9113 end if;
9115 -- If the target type is unconstrained, then we reset the type of the
9116 -- result from the type of the expression. For other cases, the actual
9117 -- subtype of the expression is the target type.
9119 if Is_Composite_Type (Target_Typ)
9120 and then not Is_Constrained (Target_Typ)
9121 then
9122 Set_Etype (N, Etype (Expr));
9123 end if;
9125 Analyze_Dimension (N);
9126 Eval_Qualified_Expression (N);
9128 -- If we still have a qualified expression after the static evaluation,
9129 -- then apply a scalar range check if needed. The reason that we do this
9130 -- after the Eval call is that otherwise, the application of the range
9131 -- check may convert an illegal static expression and result in warning
9132 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
9134 if Nkind (N) = N_Qualified_Expression and then Is_Scalar_Type (Typ) then
9135 Apply_Scalar_Range_Check (Expr, Typ);
9136 end if;
9137 end Resolve_Qualified_Expression;
9139 ------------------------------
9140 -- Resolve_Raise_Expression --
9141 ------------------------------
9143 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
9144 begin
9145 if Typ = Raise_Type then
9146 Error_Msg_N ("cannot find unique type for raise expression", N);
9147 Set_Etype (N, Any_Type);
9148 else
9149 Set_Etype (N, Typ);
9150 end if;
9151 end Resolve_Raise_Expression;
9153 -------------------
9154 -- Resolve_Range --
9155 -------------------
9157 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
9158 L : constant Node_Id := Low_Bound (N);
9159 H : constant Node_Id := High_Bound (N);
9161 function First_Last_Ref return Boolean;
9162 -- Returns True if N is of the form X'First .. X'Last where X is the
9163 -- same entity for both attributes.
9165 --------------------
9166 -- First_Last_Ref --
9167 --------------------
9169 function First_Last_Ref return Boolean is
9170 Lorig : constant Node_Id := Original_Node (L);
9171 Horig : constant Node_Id := Original_Node (H);
9173 begin
9174 if Nkind (Lorig) = N_Attribute_Reference
9175 and then Nkind (Horig) = N_Attribute_Reference
9176 and then Attribute_Name (Lorig) = Name_First
9177 and then Attribute_Name (Horig) = Name_Last
9178 then
9179 declare
9180 PL : constant Node_Id := Prefix (Lorig);
9181 PH : constant Node_Id := Prefix (Horig);
9182 begin
9183 if Is_Entity_Name (PL)
9184 and then Is_Entity_Name (PH)
9185 and then Entity (PL) = Entity (PH)
9186 then
9187 return True;
9188 end if;
9189 end;
9190 end if;
9192 return False;
9193 end First_Last_Ref;
9195 -- Start of processing for Resolve_Range
9197 begin
9198 Set_Etype (N, Typ);
9199 Resolve (L, Typ);
9200 Resolve (H, Typ);
9202 -- Check for inappropriate range on unordered enumeration type
9204 if Bad_Unordered_Enumeration_Reference (N, Typ)
9206 -- Exclude X'First .. X'Last if X is the same entity for both
9208 and then not First_Last_Ref
9209 then
9210 Error_Msg_Sloc := Sloc (Typ);
9211 Error_Msg_NE
9212 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
9213 end if;
9215 Check_Unset_Reference (L);
9216 Check_Unset_Reference (H);
9218 -- We have to check the bounds for being within the base range as
9219 -- required for a non-static context. Normally this is automatic and
9220 -- done as part of evaluating expressions, but the N_Range node is an
9221 -- exception, since in GNAT we consider this node to be a subexpression,
9222 -- even though in Ada it is not. The circuit in Sem_Eval could check for
9223 -- this, but that would put the test on the main evaluation path for
9224 -- expressions.
9226 Check_Non_Static_Context (L);
9227 Check_Non_Static_Context (H);
9229 -- Check for an ambiguous range over character literals. This will
9230 -- happen with a membership test involving only literals.
9232 if Typ = Any_Character then
9233 Ambiguous_Character (L);
9234 Set_Etype (N, Any_Type);
9235 return;
9236 end if;
9238 -- If bounds are static, constant-fold them, so size computations are
9239 -- identical between front-end and back-end. Do not perform this
9240 -- transformation while analyzing generic units, as type information
9241 -- would be lost when reanalyzing the constant node in the instance.
9243 if Is_Discrete_Type (Typ) and then Expander_Active then
9244 if Is_OK_Static_Expression (L) then
9245 Fold_Uint (L, Expr_Value (L), Is_OK_Static_Expression (L));
9246 end if;
9248 if Is_OK_Static_Expression (H) then
9249 Fold_Uint (H, Expr_Value (H), Is_OK_Static_Expression (H));
9250 end if;
9251 end if;
9252 end Resolve_Range;
9254 --------------------------
9255 -- Resolve_Real_Literal --
9256 --------------------------
9258 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
9259 Actual_Typ : constant Entity_Id := Etype (N);
9261 begin
9262 -- Special processing for fixed-point literals to make sure that the
9263 -- value is an exact multiple of small where this is required. We skip
9264 -- this for the universal real case, and also for generic types.
9266 if Is_Fixed_Point_Type (Typ)
9267 and then Typ /= Universal_Fixed
9268 and then Typ /= Any_Fixed
9269 and then not Is_Generic_Type (Typ)
9270 then
9271 declare
9272 Val : constant Ureal := Realval (N);
9273 Cintr : constant Ureal := Val / Small_Value (Typ);
9274 Cint : constant Uint := UR_Trunc (Cintr);
9275 Den : constant Uint := Norm_Den (Cintr);
9276 Stat : Boolean;
9278 begin
9279 -- Case of literal is not an exact multiple of the Small
9281 if Den /= 1 then
9283 -- For a source program literal for a decimal fixed-point type,
9284 -- this is statically illegal (RM 4.9(36)).
9286 if Is_Decimal_Fixed_Point_Type (Typ)
9287 and then Actual_Typ = Universal_Real
9288 and then Comes_From_Source (N)
9289 then
9290 Error_Msg_N ("value has extraneous low order digits", N);
9291 end if;
9293 -- Generate a warning if literal from source
9295 if Is_OK_Static_Expression (N)
9296 and then Warn_On_Bad_Fixed_Value
9297 then
9298 Error_Msg_N
9299 ("?b?static fixed-point value is not a multiple of Small!",
9301 end if;
9303 -- Replace literal by a value that is the exact representation
9304 -- of a value of the type, i.e. a multiple of the small value,
9305 -- by truncation, since Machine_Rounds is false for all GNAT
9306 -- fixed-point types (RM 4.9(38)).
9308 Stat := Is_OK_Static_Expression (N);
9309 Rewrite (N,
9310 Make_Real_Literal (Sloc (N),
9311 Realval => Small_Value (Typ) * Cint));
9313 Set_Is_Static_Expression (N, Stat);
9314 end if;
9316 -- In all cases, set the corresponding integer field
9318 Set_Corresponding_Integer_Value (N, Cint);
9319 end;
9320 end if;
9322 -- Now replace the actual type by the expected type as usual
9324 Set_Etype (N, Typ);
9325 Eval_Real_Literal (N);
9326 end Resolve_Real_Literal;
9328 -----------------------
9329 -- Resolve_Reference --
9330 -----------------------
9332 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
9333 P : constant Node_Id := Prefix (N);
9335 begin
9336 -- Replace general access with specific type
9338 if Ekind (Etype (N)) = E_Allocator_Type then
9339 Set_Etype (N, Base_Type (Typ));
9340 end if;
9342 Resolve (P, Designated_Type (Etype (N)));
9344 -- If we are taking the reference of a volatile entity, then treat it as
9345 -- a potential modification of this entity. This is too conservative,
9346 -- but necessary because remove side effects can cause transformations
9347 -- of normal assignments into reference sequences that otherwise fail to
9348 -- notice the modification.
9350 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
9351 Note_Possible_Modification (P, Sure => False);
9352 end if;
9353 end Resolve_Reference;
9355 --------------------------------
9356 -- Resolve_Selected_Component --
9357 --------------------------------
9359 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
9360 Comp : Entity_Id;
9361 Comp1 : Entity_Id := Empty; -- prevent junk warning
9362 P : constant Node_Id := Prefix (N);
9363 S : constant Node_Id := Selector_Name (N);
9364 T : Entity_Id := Etype (P);
9365 I : Interp_Index;
9366 I1 : Interp_Index := 0; -- prevent junk warning
9367 It : Interp;
9368 It1 : Interp;
9369 Found : Boolean;
9371 function Init_Component return Boolean;
9372 -- Check whether this is the initialization of a component within an
9373 -- init proc (by assignment or call to another init proc). If true,
9374 -- there is no need for a discriminant check.
9376 --------------------
9377 -- Init_Component --
9378 --------------------
9380 function Init_Component return Boolean is
9381 begin
9382 return Inside_Init_Proc
9383 and then Nkind (Prefix (N)) = N_Identifier
9384 and then Chars (Prefix (N)) = Name_uInit
9385 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
9386 end Init_Component;
9388 -- Start of processing for Resolve_Selected_Component
9390 begin
9391 if Is_Overloaded (P) then
9393 -- Use the context type to select the prefix that has a selector
9394 -- of the correct name and type.
9396 Found := False;
9397 Get_First_Interp (P, I, It);
9399 Search : while Present (It.Typ) loop
9400 if Is_Access_Type (It.Typ) then
9401 T := Designated_Type (It.Typ);
9402 else
9403 T := It.Typ;
9404 end if;
9406 -- Locate selected component. For a private prefix the selector
9407 -- can denote a discriminant.
9409 if Is_Record_Type (T) or else Is_Private_Type (T) then
9411 -- The visible components of a class-wide type are those of
9412 -- the root type.
9414 if Is_Class_Wide_Type (T) then
9415 T := Etype (T);
9416 end if;
9418 Comp := First_Entity (T);
9419 while Present (Comp) loop
9420 if Chars (Comp) = Chars (S)
9421 and then Covers (Typ, Etype (Comp))
9422 then
9423 if not Found then
9424 Found := True;
9425 I1 := I;
9426 It1 := It;
9427 Comp1 := Comp;
9429 else
9430 It := Disambiguate (P, I1, I, Any_Type);
9432 if It = No_Interp then
9433 Error_Msg_N
9434 ("ambiguous prefix for selected component", N);
9435 Set_Etype (N, Typ);
9436 return;
9438 else
9439 It1 := It;
9441 -- There may be an implicit dereference. Retrieve
9442 -- designated record type.
9444 if Is_Access_Type (It1.Typ) then
9445 T := Designated_Type (It1.Typ);
9446 else
9447 T := It1.Typ;
9448 end if;
9450 if Scope (Comp1) /= T then
9452 -- Resolution chooses the new interpretation.
9453 -- Find the component with the right name.
9455 Comp1 := First_Entity (T);
9456 while Present (Comp1)
9457 and then Chars (Comp1) /= Chars (S)
9458 loop
9459 Comp1 := Next_Entity (Comp1);
9460 end loop;
9461 end if;
9463 exit Search;
9464 end if;
9465 end if;
9466 end if;
9468 Comp := Next_Entity (Comp);
9469 end loop;
9470 end if;
9472 Get_Next_Interp (I, It);
9473 end loop Search;
9475 -- There must be a legal interpretation at this point
9477 pragma Assert (Found);
9478 Resolve (P, It1.Typ);
9479 Set_Etype (N, Typ);
9480 Set_Entity_With_Checks (S, Comp1);
9482 else
9483 -- Resolve prefix with its type
9485 Resolve (P, T);
9486 end if;
9488 -- Generate cross-reference. We needed to wait until full overloading
9489 -- resolution was complete to do this, since otherwise we can't tell if
9490 -- we are an lvalue or not.
9492 if May_Be_Lvalue (N) then
9493 Generate_Reference (Entity (S), S, 'm');
9494 else
9495 Generate_Reference (Entity (S), S, 'r');
9496 end if;
9498 -- If prefix is an access type, the node will be transformed into an
9499 -- explicit dereference during expansion. The type of the node is the
9500 -- designated type of that of the prefix.
9502 if Is_Access_Type (Etype (P)) then
9503 T := Designated_Type (Etype (P));
9504 Check_Fully_Declared_Prefix (T, P);
9505 else
9506 T := Etype (P);
9507 end if;
9509 -- Set flag for expander if discriminant check required
9511 if Has_Discriminants (T)
9512 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
9513 and then Present (Original_Record_Component (Entity (S)))
9514 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
9515 and then not Discriminant_Checks_Suppressed (T)
9516 and then not Init_Component
9517 then
9518 Set_Do_Discriminant_Check (N);
9519 end if;
9521 if Ekind (Entity (S)) = E_Void then
9522 Error_Msg_N ("premature use of component", S);
9523 end if;
9525 -- If the prefix is a record conversion, this may be a renamed
9526 -- discriminant whose bounds differ from those of the original
9527 -- one, so we must ensure that a range check is performed.
9529 if Nkind (P) = N_Type_Conversion
9530 and then Ekind (Entity (S)) = E_Discriminant
9531 and then Is_Discrete_Type (Typ)
9532 then
9533 Set_Etype (N, Base_Type (Typ));
9534 end if;
9536 -- Note: No Eval processing is required, because the prefix is of a
9537 -- record type, or protected type, and neither can possibly be static.
9539 -- If the record type is atomic, and the component is non-atomic, then
9540 -- this is worth a warning, since we have a situation where the access
9541 -- to the component may cause extra read/writes of the atomic array
9542 -- object, or partial word accesses, both of which may be unexpected.
9544 if Nkind (N) = N_Selected_Component
9545 and then Is_Atomic_Ref_With_Address (N)
9546 and then not Is_Atomic (Entity (S))
9547 and then not Is_Atomic (Etype (Entity (S)))
9548 then
9549 Error_Msg_N
9550 ("??access to non-atomic component of atomic record",
9551 Prefix (N));
9552 Error_Msg_N
9553 ("\??may cause unexpected accesses to atomic object",
9554 Prefix (N));
9555 end if;
9557 Analyze_Dimension (N);
9558 end Resolve_Selected_Component;
9560 -------------------
9561 -- Resolve_Shift --
9562 -------------------
9564 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
9565 B_Typ : constant Entity_Id := Base_Type (Typ);
9566 L : constant Node_Id := Left_Opnd (N);
9567 R : constant Node_Id := Right_Opnd (N);
9569 begin
9570 -- We do the resolution using the base type, because intermediate values
9571 -- in expressions always are of the base type, not a subtype of it.
9573 Resolve (L, B_Typ);
9574 Resolve (R, Standard_Natural);
9576 Check_Unset_Reference (L);
9577 Check_Unset_Reference (R);
9579 Set_Etype (N, B_Typ);
9580 Generate_Operator_Reference (N, B_Typ);
9581 Eval_Shift (N);
9582 end Resolve_Shift;
9584 ---------------------------
9585 -- Resolve_Short_Circuit --
9586 ---------------------------
9588 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
9589 B_Typ : constant Entity_Id := Base_Type (Typ);
9590 L : constant Node_Id := Left_Opnd (N);
9591 R : constant Node_Id := Right_Opnd (N);
9593 begin
9594 -- Ensure all actions associated with the left operand (e.g.
9595 -- finalization of transient controlled objects) are fully evaluated
9596 -- locally within an expression with actions. This is particularly
9597 -- helpful for coverage analysis. However this should not happen in
9598 -- generics.
9600 if Expander_Active then
9601 declare
9602 Reloc_L : constant Node_Id := Relocate_Node (L);
9603 begin
9604 Save_Interps (Old_N => L, New_N => Reloc_L);
9606 Rewrite (L,
9607 Make_Expression_With_Actions (Sloc (L),
9608 Actions => New_List,
9609 Expression => Reloc_L));
9611 -- Set Comes_From_Source on L to preserve warnings for unset
9612 -- reference.
9614 Set_Comes_From_Source (L, Comes_From_Source (Reloc_L));
9615 end;
9616 end if;
9618 Resolve (L, B_Typ);
9619 Resolve (R, B_Typ);
9621 -- Check for issuing warning for always False assert/check, this happens
9622 -- when assertions are turned off, in which case the pragma Assert/Check
9623 -- was transformed into:
9625 -- if False and then <condition> then ...
9627 -- and we detect this pattern
9629 if Warn_On_Assertion_Failure
9630 and then Is_Entity_Name (R)
9631 and then Entity (R) = Standard_False
9632 and then Nkind (Parent (N)) = N_If_Statement
9633 and then Nkind (N) = N_And_Then
9634 and then Is_Entity_Name (L)
9635 and then Entity (L) = Standard_False
9636 then
9637 declare
9638 Orig : constant Node_Id := Original_Node (Parent (N));
9640 begin
9641 -- Special handling of Asssert pragma
9643 if Nkind (Orig) = N_Pragma
9644 and then Pragma_Name (Orig) = Name_Assert
9645 then
9646 declare
9647 Expr : constant Node_Id :=
9648 Original_Node
9649 (Expression
9650 (First (Pragma_Argument_Associations (Orig))));
9652 begin
9653 -- Don't warn if original condition is explicit False,
9654 -- since obviously the failure is expected in this case.
9656 if Is_Entity_Name (Expr)
9657 and then Entity (Expr) = Standard_False
9658 then
9659 null;
9661 -- Issue warning. We do not want the deletion of the
9662 -- IF/AND-THEN to take this message with it. We achieve this
9663 -- by making sure that the expanded code points to the Sloc
9664 -- of the expression, not the original pragma.
9666 else
9667 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
9668 -- The source location of the expression is not usually
9669 -- the best choice here. For example, it gets located on
9670 -- the last AND keyword in a chain of boolean expressiond
9671 -- AND'ed together. It is best to put the message on the
9672 -- first character of the assertion, which is the effect
9673 -- of the First_Node call here.
9675 Error_Msg_F
9676 ("?A?assertion would fail at run time!",
9677 Expression
9678 (First (Pragma_Argument_Associations (Orig))));
9679 end if;
9680 end;
9682 -- Similar processing for Check pragma
9684 elsif Nkind (Orig) = N_Pragma
9685 and then Pragma_Name (Orig) = Name_Check
9686 then
9687 -- Don't want to warn if original condition is explicit False
9689 declare
9690 Expr : constant Node_Id :=
9691 Original_Node
9692 (Expression
9693 (Next (First (Pragma_Argument_Associations (Orig)))));
9694 begin
9695 if Is_Entity_Name (Expr)
9696 and then Entity (Expr) = Standard_False
9697 then
9698 null;
9700 -- Post warning
9702 else
9703 -- Again use Error_Msg_F rather than Error_Msg_N, see
9704 -- comment above for an explanation of why we do this.
9706 Error_Msg_F
9707 ("?A?check would fail at run time!",
9708 Expression
9709 (Last (Pragma_Argument_Associations (Orig))));
9710 end if;
9711 end;
9712 end if;
9713 end;
9714 end if;
9716 -- Continue with processing of short circuit
9718 Check_Unset_Reference (L);
9719 Check_Unset_Reference (R);
9721 Set_Etype (N, B_Typ);
9722 Eval_Short_Circuit (N);
9723 end Resolve_Short_Circuit;
9725 -------------------
9726 -- Resolve_Slice --
9727 -------------------
9729 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
9730 Drange : constant Node_Id := Discrete_Range (N);
9731 Name : constant Node_Id := Prefix (N);
9732 Array_Type : Entity_Id := Empty;
9733 Dexpr : Node_Id := Empty;
9734 Index_Type : Entity_Id;
9736 begin
9737 if Is_Overloaded (Name) then
9739 -- Use the context type to select the prefix that yields the correct
9740 -- array type.
9742 declare
9743 I : Interp_Index;
9744 I1 : Interp_Index := 0;
9745 It : Interp;
9746 P : constant Node_Id := Prefix (N);
9747 Found : Boolean := False;
9749 begin
9750 Get_First_Interp (P, I, It);
9751 while Present (It.Typ) loop
9752 if (Is_Array_Type (It.Typ)
9753 and then Covers (Typ, It.Typ))
9754 or else (Is_Access_Type (It.Typ)
9755 and then Is_Array_Type (Designated_Type (It.Typ))
9756 and then Covers (Typ, Designated_Type (It.Typ)))
9757 then
9758 if Found then
9759 It := Disambiguate (P, I1, I, Any_Type);
9761 if It = No_Interp then
9762 Error_Msg_N ("ambiguous prefix for slicing", N);
9763 Set_Etype (N, Typ);
9764 return;
9765 else
9766 Found := True;
9767 Array_Type := It.Typ;
9768 I1 := I;
9769 end if;
9770 else
9771 Found := True;
9772 Array_Type := It.Typ;
9773 I1 := I;
9774 end if;
9775 end if;
9777 Get_Next_Interp (I, It);
9778 end loop;
9779 end;
9781 else
9782 Array_Type := Etype (Name);
9783 end if;
9785 Resolve (Name, Array_Type);
9787 if Is_Access_Type (Array_Type) then
9788 Apply_Access_Check (N);
9789 Array_Type := Designated_Type (Array_Type);
9791 -- If the prefix is an access to an unconstrained array, we must use
9792 -- the actual subtype of the object to perform the index checks. The
9793 -- object denoted by the prefix is implicit in the node, so we build
9794 -- an explicit representation for it in order to compute the actual
9795 -- subtype.
9797 if not Is_Constrained (Array_Type) then
9798 Remove_Side_Effects (Prefix (N));
9800 declare
9801 Obj : constant Node_Id :=
9802 Make_Explicit_Dereference (Sloc (N),
9803 Prefix => New_Copy_Tree (Prefix (N)));
9804 begin
9805 Set_Etype (Obj, Array_Type);
9806 Set_Parent (Obj, Parent (N));
9807 Array_Type := Get_Actual_Subtype (Obj);
9808 end;
9809 end if;
9811 elsif Is_Entity_Name (Name)
9812 or else Nkind (Name) = N_Explicit_Dereference
9813 or else (Nkind (Name) = N_Function_Call
9814 and then not Is_Constrained (Etype (Name)))
9815 then
9816 Array_Type := Get_Actual_Subtype (Name);
9818 -- If the name is a selected component that depends on discriminants,
9819 -- build an actual subtype for it. This can happen only when the name
9820 -- itself is overloaded; otherwise the actual subtype is created when
9821 -- the selected component is analyzed.
9823 elsif Nkind (Name) = N_Selected_Component
9824 and then Full_Analysis
9825 and then Depends_On_Discriminant (First_Index (Array_Type))
9826 then
9827 declare
9828 Act_Decl : constant Node_Id :=
9829 Build_Actual_Subtype_Of_Component (Array_Type, Name);
9830 begin
9831 Insert_Action (N, Act_Decl);
9832 Array_Type := Defining_Identifier (Act_Decl);
9833 end;
9835 -- Maybe this should just be "else", instead of checking for the
9836 -- specific case of slice??? This is needed for the case where the
9837 -- prefix is an Image attribute, which gets expanded to a slice, and so
9838 -- has a constrained subtype which we want to use for the slice range
9839 -- check applied below (the range check won't get done if the
9840 -- unconstrained subtype of the 'Image is used).
9842 elsif Nkind (Name) = N_Slice then
9843 Array_Type := Etype (Name);
9844 end if;
9846 -- Obtain the type of the array index
9848 if Ekind (Array_Type) = E_String_Literal_Subtype then
9849 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
9850 else
9851 Index_Type := Etype (First_Index (Array_Type));
9852 end if;
9854 -- If name was overloaded, set slice type correctly now
9856 Set_Etype (N, Array_Type);
9858 -- Handle the generation of a range check that compares the array index
9859 -- against the discrete_range. The check is not applied to internally
9860 -- built nodes associated with the expansion of dispatch tables. Check
9861 -- that Ada.Tags has already been loaded to avoid extra dependencies on
9862 -- the unit.
9864 if Tagged_Type_Expansion
9865 and then RTU_Loaded (Ada_Tags)
9866 and then Nkind (Prefix (N)) = N_Selected_Component
9867 and then Present (Entity (Selector_Name (Prefix (N))))
9868 and then Entity (Selector_Name (Prefix (N))) =
9869 RTE_Record_Component (RE_Prims_Ptr)
9870 then
9871 null;
9873 -- The discrete_range is specified by a subtype indication. Create a
9874 -- shallow copy and inherit the type, parent and source location from
9875 -- the discrete_range. This ensures that the range check is inserted
9876 -- relative to the slice and that the runtime exception points to the
9877 -- proper construct.
9879 elsif Is_Entity_Name (Drange) then
9880 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
9882 Set_Etype (Dexpr, Etype (Drange));
9883 Set_Parent (Dexpr, Parent (Drange));
9884 Set_Sloc (Dexpr, Sloc (Drange));
9886 -- The discrete_range is a regular range. Resolve the bounds and remove
9887 -- their side effects.
9889 else
9890 Resolve (Drange, Base_Type (Index_Type));
9892 if Nkind (Drange) = N_Range then
9893 Force_Evaluation (Low_Bound (Drange));
9894 Force_Evaluation (High_Bound (Drange));
9896 Dexpr := Drange;
9897 end if;
9898 end if;
9900 if Present (Dexpr) then
9901 Apply_Range_Check (Dexpr, Index_Type);
9902 end if;
9904 Set_Slice_Subtype (N);
9906 -- Check bad use of type with predicates
9908 declare
9909 Subt : Entity_Id;
9911 begin
9912 if Nkind (Drange) = N_Subtype_Indication
9913 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
9914 then
9915 Subt := Entity (Subtype_Mark (Drange));
9916 else
9917 Subt := Etype (Drange);
9918 end if;
9920 if Has_Predicates (Subt) then
9921 Bad_Predicated_Subtype_Use
9922 ("subtype& has predicate, not allowed in slice", Drange, Subt);
9923 end if;
9924 end;
9926 -- Otherwise here is where we check suspicious indexes
9928 if Nkind (Drange) = N_Range then
9929 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
9930 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
9931 end if;
9933 Analyze_Dimension (N);
9934 Eval_Slice (N);
9935 end Resolve_Slice;
9937 ----------------------------
9938 -- Resolve_String_Literal --
9939 ----------------------------
9941 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
9942 C_Typ : constant Entity_Id := Component_Type (Typ);
9943 R_Typ : constant Entity_Id := Root_Type (C_Typ);
9944 Loc : constant Source_Ptr := Sloc (N);
9945 Str : constant String_Id := Strval (N);
9946 Strlen : constant Nat := String_Length (Str);
9947 Subtype_Id : Entity_Id;
9948 Need_Check : Boolean;
9950 begin
9951 -- For a string appearing in a concatenation, defer creation of the
9952 -- string_literal_subtype until the end of the resolution of the
9953 -- concatenation, because the literal may be constant-folded away. This
9954 -- is a useful optimization for long concatenation expressions.
9956 -- If the string is an aggregate built for a single character (which
9957 -- happens in a non-static context) or a is null string to which special
9958 -- checks may apply, we build the subtype. Wide strings must also get a
9959 -- string subtype if they come from a one character aggregate. Strings
9960 -- generated by attributes might be static, but it is often hard to
9961 -- determine whether the enclosing context is static, so we generate
9962 -- subtypes for them as well, thus losing some rarer optimizations ???
9963 -- Same for strings that come from a static conversion.
9965 Need_Check :=
9966 (Strlen = 0 and then Typ /= Standard_String)
9967 or else Nkind (Parent (N)) /= N_Op_Concat
9968 or else (N /= Left_Opnd (Parent (N))
9969 and then N /= Right_Opnd (Parent (N)))
9970 or else ((Typ = Standard_Wide_String
9971 or else Typ = Standard_Wide_Wide_String)
9972 and then Nkind (Original_Node (N)) /= N_String_Literal);
9974 -- If the resolving type is itself a string literal subtype, we can just
9975 -- reuse it, since there is no point in creating another.
9977 if Ekind (Typ) = E_String_Literal_Subtype then
9978 Subtype_Id := Typ;
9980 elsif Nkind (Parent (N)) = N_Op_Concat
9981 and then not Need_Check
9982 and then not Nkind_In (Original_Node (N), N_Character_Literal,
9983 N_Attribute_Reference,
9984 N_Qualified_Expression,
9985 N_Type_Conversion)
9986 then
9987 Subtype_Id := Typ;
9989 -- Do not generate a string literal subtype for the default expression
9990 -- of a formal parameter in GNATprove mode. This is because the string
9991 -- subtype is associated with the freezing actions of the subprogram,
9992 -- however freezing is disabled in GNATprove mode and as a result the
9993 -- subtype is unavailable.
9995 elsif GNATprove_Mode
9996 and then Nkind (Parent (N)) = N_Parameter_Specification
9997 then
9998 Subtype_Id := Typ;
10000 -- Otherwise we must create a string literal subtype. Note that the
10001 -- whole idea of string literal subtypes is simply to avoid the need
10002 -- for building a full fledged array subtype for each literal.
10004 else
10005 Set_String_Literal_Subtype (N, Typ);
10006 Subtype_Id := Etype (N);
10007 end if;
10009 if Nkind (Parent (N)) /= N_Op_Concat
10010 or else Need_Check
10011 then
10012 Set_Etype (N, Subtype_Id);
10013 Eval_String_Literal (N);
10014 end if;
10016 if Is_Limited_Composite (Typ)
10017 or else Is_Private_Composite (Typ)
10018 then
10019 Error_Msg_N ("string literal not available for private array", N);
10020 Set_Etype (N, Any_Type);
10021 return;
10022 end if;
10024 -- The validity of a null string has been checked in the call to
10025 -- Eval_String_Literal.
10027 if Strlen = 0 then
10028 return;
10030 -- Always accept string literal with component type Any_Character, which
10031 -- occurs in error situations and in comparisons of literals, both of
10032 -- which should accept all literals.
10034 elsif R_Typ = Any_Character then
10035 return;
10037 -- If the type is bit-packed, then we always transform the string
10038 -- literal into a full fledged aggregate.
10040 elsif Is_Bit_Packed_Array (Typ) then
10041 null;
10043 -- Deal with cases of Wide_Wide_String, Wide_String, and String
10045 else
10046 -- For Standard.Wide_Wide_String, or any other type whose component
10047 -- type is Standard.Wide_Wide_Character, we know that all the
10048 -- characters in the string must be acceptable, since the parser
10049 -- accepted the characters as valid character literals.
10051 if R_Typ = Standard_Wide_Wide_Character then
10052 null;
10054 -- For the case of Standard.String, or any other type whose component
10055 -- type is Standard.Character, we must make sure that there are no
10056 -- wide characters in the string, i.e. that it is entirely composed
10057 -- of characters in range of type Character.
10059 -- If the string literal is the result of a static concatenation, the
10060 -- test has already been performed on the components, and need not be
10061 -- repeated.
10063 elsif R_Typ = Standard_Character
10064 and then Nkind (Original_Node (N)) /= N_Op_Concat
10065 then
10066 for J in 1 .. Strlen loop
10067 if not In_Character_Range (Get_String_Char (Str, J)) then
10069 -- If we are out of range, post error. This is one of the
10070 -- very few places that we place the flag in the middle of
10071 -- a token, right under the offending wide character. Not
10072 -- quite clear if this is right wrt wide character encoding
10073 -- sequences, but it's only an error message.
10075 Error_Msg
10076 ("literal out of range of type Standard.Character",
10077 Source_Ptr (Int (Loc) + J));
10078 return;
10079 end if;
10080 end loop;
10082 -- For the case of Standard.Wide_String, or any other type whose
10083 -- component type is Standard.Wide_Character, we must make sure that
10084 -- there are no wide characters in the string, i.e. that it is
10085 -- entirely composed of characters in range of type Wide_Character.
10087 -- If the string literal is the result of a static concatenation,
10088 -- the test has already been performed on the components, and need
10089 -- not be repeated.
10091 elsif R_Typ = Standard_Wide_Character
10092 and then Nkind (Original_Node (N)) /= N_Op_Concat
10093 then
10094 for J in 1 .. Strlen loop
10095 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
10097 -- If we are out of range, post error. This is one of the
10098 -- very few places that we place the flag in the middle of
10099 -- a token, right under the offending wide character.
10101 -- This is not quite right, because characters in general
10102 -- will take more than one character position ???
10104 Error_Msg
10105 ("literal out of range of type Standard.Wide_Character",
10106 Source_Ptr (Int (Loc) + J));
10107 return;
10108 end if;
10109 end loop;
10111 -- If the root type is not a standard character, then we will convert
10112 -- the string into an aggregate and will let the aggregate code do
10113 -- the checking. Standard Wide_Wide_Character is also OK here.
10115 else
10116 null;
10117 end if;
10119 -- See if the component type of the array corresponding to the string
10120 -- has compile time known bounds. If yes we can directly check
10121 -- whether the evaluation of the string will raise constraint error.
10122 -- Otherwise we need to transform the string literal into the
10123 -- corresponding character aggregate and let the aggregate code do
10124 -- the checking.
10126 if Is_Standard_Character_Type (R_Typ) then
10128 -- Check for the case of full range, where we are definitely OK
10130 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
10131 return;
10132 end if;
10134 -- Here the range is not the complete base type range, so check
10136 declare
10137 Comp_Typ_Lo : constant Node_Id :=
10138 Type_Low_Bound (Component_Type (Typ));
10139 Comp_Typ_Hi : constant Node_Id :=
10140 Type_High_Bound (Component_Type (Typ));
10142 Char_Val : Uint;
10144 begin
10145 if Compile_Time_Known_Value (Comp_Typ_Lo)
10146 and then Compile_Time_Known_Value (Comp_Typ_Hi)
10147 then
10148 for J in 1 .. Strlen loop
10149 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
10151 if Char_Val < Expr_Value (Comp_Typ_Lo)
10152 or else Char_Val > Expr_Value (Comp_Typ_Hi)
10153 then
10154 Apply_Compile_Time_Constraint_Error
10155 (N, "character out of range??",
10156 CE_Range_Check_Failed,
10157 Loc => Source_Ptr (Int (Loc) + J));
10158 end if;
10159 end loop;
10161 return;
10162 end if;
10163 end;
10164 end if;
10165 end if;
10167 -- If we got here we meed to transform the string literal into the
10168 -- equivalent qualified positional array aggregate. This is rather
10169 -- heavy artillery for this situation, but it is hard work to avoid.
10171 declare
10172 Lits : constant List_Id := New_List;
10173 P : Source_Ptr := Loc + 1;
10174 C : Char_Code;
10176 begin
10177 -- Build the character literals, we give them source locations that
10178 -- correspond to the string positions, which is a bit tricky given
10179 -- the possible presence of wide character escape sequences.
10181 for J in 1 .. Strlen loop
10182 C := Get_String_Char (Str, J);
10183 Set_Character_Literal_Name (C);
10185 Append_To (Lits,
10186 Make_Character_Literal (P,
10187 Chars => Name_Find,
10188 Char_Literal_Value => UI_From_CC (C)));
10190 if In_Character_Range (C) then
10191 P := P + 1;
10193 -- Should we have a call to Skip_Wide here ???
10195 -- ??? else
10196 -- Skip_Wide (P);
10198 end if;
10199 end loop;
10201 Rewrite (N,
10202 Make_Qualified_Expression (Loc,
10203 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
10204 Expression =>
10205 Make_Aggregate (Loc, Expressions => Lits)));
10207 Analyze_And_Resolve (N, Typ);
10208 end;
10209 end Resolve_String_Literal;
10211 -----------------------------
10212 -- Resolve_Type_Conversion --
10213 -----------------------------
10215 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
10216 Conv_OK : constant Boolean := Conversion_OK (N);
10217 Operand : constant Node_Id := Expression (N);
10218 Operand_Typ : constant Entity_Id := Etype (Operand);
10219 Target_Typ : constant Entity_Id := Etype (N);
10220 Rop : Node_Id;
10221 Orig_N : Node_Id;
10222 Orig_T : Node_Id;
10224 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
10225 -- Set to False to suppress cases where we want to suppress the test
10226 -- for redundancy to avoid possible false positives on this warning.
10228 begin
10229 if not Conv_OK
10230 and then not Valid_Conversion (N, Target_Typ, Operand)
10231 then
10232 return;
10233 end if;
10235 -- If the Operand Etype is Universal_Fixed, then the conversion is
10236 -- never redundant. We need this check because by the time we have
10237 -- finished the rather complex transformation, the conversion looks
10238 -- redundant when it is not.
10240 if Operand_Typ = Universal_Fixed then
10241 Test_Redundant := False;
10243 -- If the operand is marked as Any_Fixed, then special processing is
10244 -- required. This is also a case where we suppress the test for a
10245 -- redundant conversion, since most certainly it is not redundant.
10247 elsif Operand_Typ = Any_Fixed then
10248 Test_Redundant := False;
10250 -- Mixed-mode operation involving a literal. Context must be a fixed
10251 -- type which is applied to the literal subsequently.
10253 if Is_Fixed_Point_Type (Typ) then
10254 Set_Etype (Operand, Universal_Real);
10256 elsif Is_Numeric_Type (Typ)
10257 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
10258 and then (Etype (Right_Opnd (Operand)) = Universal_Real
10259 or else
10260 Etype (Left_Opnd (Operand)) = Universal_Real)
10261 then
10262 -- Return if expression is ambiguous
10264 if Unique_Fixed_Point_Type (N) = Any_Type then
10265 return;
10267 -- If nothing else, the available fixed type is Duration
10269 else
10270 Set_Etype (Operand, Standard_Duration);
10271 end if;
10273 -- Resolve the real operand with largest available precision
10275 if Etype (Right_Opnd (Operand)) = Universal_Real then
10276 Rop := New_Copy_Tree (Right_Opnd (Operand));
10277 else
10278 Rop := New_Copy_Tree (Left_Opnd (Operand));
10279 end if;
10281 Resolve (Rop, Universal_Real);
10283 -- If the operand is a literal (it could be a non-static and
10284 -- illegal exponentiation) check whether the use of Duration
10285 -- is potentially inaccurate.
10287 if Nkind (Rop) = N_Real_Literal
10288 and then Realval (Rop) /= Ureal_0
10289 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
10290 then
10291 Error_Msg_N
10292 ("??universal real operand can only "
10293 & "be interpreted as Duration!", Rop);
10294 Error_Msg_N
10295 ("\??precision will be lost in the conversion!", Rop);
10296 end if;
10298 elsif Is_Numeric_Type (Typ)
10299 and then Nkind (Operand) in N_Op
10300 and then Unique_Fixed_Point_Type (N) /= Any_Type
10301 then
10302 Set_Etype (Operand, Standard_Duration);
10304 else
10305 Error_Msg_N ("invalid context for mixed mode operation", N);
10306 Set_Etype (Operand, Any_Type);
10307 return;
10308 end if;
10309 end if;
10311 Resolve (Operand);
10313 -- In SPARK, a type conversion between array types should be restricted
10314 -- to types which have matching static bounds.
10316 -- Protect call to Matching_Static_Array_Bounds to avoid costly
10317 -- operation if not needed.
10319 if Restriction_Check_Required (SPARK_05)
10320 and then Is_Array_Type (Target_Typ)
10321 and then Is_Array_Type (Operand_Typ)
10322 and then Operand_Typ /= Any_Composite -- or else Operand in error
10323 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
10324 then
10325 Check_SPARK_05_Restriction
10326 ("array types should have matching static bounds", N);
10327 end if;
10329 -- In formal mode, the operand of an ancestor type conversion must be an
10330 -- object (not an expression).
10332 if Is_Tagged_Type (Target_Typ)
10333 and then not Is_Class_Wide_Type (Target_Typ)
10334 and then Is_Tagged_Type (Operand_Typ)
10335 and then not Is_Class_Wide_Type (Operand_Typ)
10336 and then Is_Ancestor (Target_Typ, Operand_Typ)
10337 and then not Is_SPARK_05_Object_Reference (Operand)
10338 then
10339 Check_SPARK_05_Restriction ("object required", Operand);
10340 end if;
10342 Analyze_Dimension (N);
10344 -- Note: we do the Eval_Type_Conversion call before applying the
10345 -- required checks for a subtype conversion. This is important, since
10346 -- both are prepared under certain circumstances to change the type
10347 -- conversion to a constraint error node, but in the case of
10348 -- Eval_Type_Conversion this may reflect an illegality in the static
10349 -- case, and we would miss the illegality (getting only a warning
10350 -- message), if we applied the type conversion checks first.
10352 Eval_Type_Conversion (N);
10354 -- Even when evaluation is not possible, we may be able to simplify the
10355 -- conversion or its expression. This needs to be done before applying
10356 -- checks, since otherwise the checks may use the original expression
10357 -- and defeat the simplifications. This is specifically the case for
10358 -- elimination of the floating-point Truncation attribute in
10359 -- float-to-int conversions.
10361 Simplify_Type_Conversion (N);
10363 -- If after evaluation we still have a type conversion, then we may need
10364 -- to apply checks required for a subtype conversion.
10366 -- Skip these type conversion checks if universal fixed operands
10367 -- operands involved, since range checks are handled separately for
10368 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
10370 if Nkind (N) = N_Type_Conversion
10371 and then not Is_Generic_Type (Root_Type (Target_Typ))
10372 and then Target_Typ /= Universal_Fixed
10373 and then Operand_Typ /= Universal_Fixed
10374 then
10375 Apply_Type_Conversion_Checks (N);
10376 end if;
10378 -- Issue warning for conversion of simple object to its own type. We
10379 -- have to test the original nodes, since they may have been rewritten
10380 -- by various optimizations.
10382 Orig_N := Original_Node (N);
10384 -- Here we test for a redundant conversion if the warning mode is
10385 -- active (and was not locally reset), and we have a type conversion
10386 -- from source not appearing in a generic instance.
10388 if Test_Redundant
10389 and then Nkind (Orig_N) = N_Type_Conversion
10390 and then Comes_From_Source (Orig_N)
10391 and then not In_Instance
10392 then
10393 Orig_N := Original_Node (Expression (Orig_N));
10394 Orig_T := Target_Typ;
10396 -- If the node is part of a larger expression, the Target_Type
10397 -- may not be the original type of the node if the context is a
10398 -- condition. Recover original type to see if conversion is needed.
10400 if Is_Boolean_Type (Orig_T)
10401 and then Nkind (Parent (N)) in N_Op
10402 then
10403 Orig_T := Etype (Parent (N));
10404 end if;
10406 -- If we have an entity name, then give the warning if the entity
10407 -- is the right type, or if it is a loop parameter covered by the
10408 -- original type (that's needed because loop parameters have an
10409 -- odd subtype coming from the bounds).
10411 if (Is_Entity_Name (Orig_N)
10412 and then
10413 (Etype (Entity (Orig_N)) = Orig_T
10414 or else
10415 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
10416 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
10418 -- If not an entity, then type of expression must match
10420 or else Etype (Orig_N) = Orig_T
10421 then
10422 -- One more check, do not give warning if the analyzed conversion
10423 -- has an expression with non-static bounds, and the bounds of the
10424 -- target are static. This avoids junk warnings in cases where the
10425 -- conversion is necessary to establish staticness, for example in
10426 -- a case statement.
10428 if not Is_OK_Static_Subtype (Operand_Typ)
10429 and then Is_OK_Static_Subtype (Target_Typ)
10430 then
10431 null;
10433 -- Finally, if this type conversion occurs in a context requiring
10434 -- a prefix, and the expression is a qualified expression then the
10435 -- type conversion is not redundant, since a qualified expression
10436 -- is not a prefix, whereas a type conversion is. For example, "X
10437 -- := T'(Funx(...)).Y;" is illegal because a selected component
10438 -- requires a prefix, but a type conversion makes it legal: "X :=
10439 -- T(T'(Funx(...))).Y;"
10441 -- In Ada 2012, a qualified expression is a name, so this idiom is
10442 -- no longer needed, but we still suppress the warning because it
10443 -- seems unfriendly for warnings to pop up when you switch to the
10444 -- newer language version.
10446 elsif Nkind (Orig_N) = N_Qualified_Expression
10447 and then Nkind_In (Parent (N), N_Attribute_Reference,
10448 N_Indexed_Component,
10449 N_Selected_Component,
10450 N_Slice,
10451 N_Explicit_Dereference)
10452 then
10453 null;
10455 -- Never warn on conversion to Long_Long_Integer'Base since
10456 -- that is most likely an artifact of the extended overflow
10457 -- checking and comes from complex expanded code.
10459 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
10460 null;
10462 -- Here we give the redundant conversion warning. If it is an
10463 -- entity, give the name of the entity in the message. If not,
10464 -- just mention the expression.
10466 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
10468 else
10469 if Is_Entity_Name (Orig_N) then
10470 Error_Msg_Node_2 := Orig_T;
10471 Error_Msg_NE -- CODEFIX
10472 ("??redundant conversion, & is of type &!",
10473 N, Entity (Orig_N));
10474 else
10475 Error_Msg_NE
10476 ("??redundant conversion, expression is of type&!",
10477 N, Orig_T);
10478 end if;
10479 end if;
10480 end if;
10481 end if;
10483 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
10484 -- No need to perform any interface conversion if the type of the
10485 -- expression coincides with the target type.
10487 if Ada_Version >= Ada_2005
10488 and then Expander_Active
10489 and then Operand_Typ /= Target_Typ
10490 then
10491 declare
10492 Opnd : Entity_Id := Operand_Typ;
10493 Target : Entity_Id := Target_Typ;
10495 begin
10496 -- If the type of the operand is a limited view, use the non-
10497 -- limited view when available.
10499 if From_Limited_With (Opnd)
10500 and then Ekind (Opnd) in Incomplete_Kind
10501 and then Present (Non_Limited_View (Opnd))
10502 then
10503 Opnd := Non_Limited_View (Opnd);
10504 Set_Etype (Expression (N), Opnd);
10505 end if;
10507 if Is_Access_Type (Opnd) then
10508 Opnd := Designated_Type (Opnd);
10509 end if;
10511 if Is_Access_Type (Target_Typ) then
10512 Target := Designated_Type (Target);
10513 end if;
10515 if Opnd = Target then
10516 null;
10518 -- Conversion from interface type
10520 elsif Is_Interface (Opnd) then
10522 -- Ada 2005 (AI-217): Handle entities from limited views
10524 if From_Limited_With (Opnd) then
10525 Error_Msg_Qual_Level := 99;
10526 Error_Msg_NE -- CODEFIX
10527 ("missing WITH clause on package &", N,
10528 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
10529 Error_Msg_N
10530 ("type conversions require visibility of the full view",
10533 elsif From_Limited_With (Target)
10534 and then not
10535 (Is_Access_Type (Target_Typ)
10536 and then Present (Non_Limited_View (Etype (Target))))
10537 then
10538 Error_Msg_Qual_Level := 99;
10539 Error_Msg_NE -- CODEFIX
10540 ("missing WITH clause on package &", N,
10541 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
10542 Error_Msg_N
10543 ("type conversions require visibility of the full view",
10546 else
10547 Expand_Interface_Conversion (N);
10548 end if;
10550 -- Conversion to interface type
10552 elsif Is_Interface (Target) then
10554 -- Handle subtypes
10556 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
10557 Opnd := Etype (Opnd);
10558 end if;
10560 if Is_Class_Wide_Type (Opnd)
10561 or else Interface_Present_In_Ancestor
10562 (Typ => Opnd,
10563 Iface => Target)
10564 then
10565 Expand_Interface_Conversion (N);
10566 else
10567 Error_Msg_Name_1 := Chars (Etype (Target));
10568 Error_Msg_Name_2 := Chars (Opnd);
10569 Error_Msg_N
10570 ("wrong interface conversion (% is not a progenitor "
10571 & "of %)", N);
10572 end if;
10573 end if;
10574 end;
10575 end if;
10577 -- Ada 2012: if target type has predicates, the result requires a
10578 -- predicate check. If the context is a call to another predicate
10579 -- check we must prevent infinite recursion.
10581 if Has_Predicates (Target_Typ) then
10582 if Nkind (Parent (N)) = N_Function_Call
10583 and then Present (Name (Parent (N)))
10584 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
10585 or else
10586 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
10587 then
10588 null;
10590 else
10591 Apply_Predicate_Check (N, Target_Typ);
10592 end if;
10593 end if;
10595 -- If at this stage we have a real to integer conversion, make sure
10596 -- that the Do_Range_Check flag is set, because such conversions in
10597 -- general need a range check. We only need this if expansion is off
10598 -- or we are in GNATProve mode.
10600 if Nkind (N) = N_Type_Conversion
10601 and then (GNATprove_Mode or not Expander_Active)
10602 and then Is_Integer_Type (Target_Typ)
10603 and then Is_Real_Type (Operand_Typ)
10604 then
10605 Set_Do_Range_Check (Operand);
10606 end if;
10607 end Resolve_Type_Conversion;
10609 ----------------------
10610 -- Resolve_Unary_Op --
10611 ----------------------
10613 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
10614 B_Typ : constant Entity_Id := Base_Type (Typ);
10615 R : constant Node_Id := Right_Opnd (N);
10616 OK : Boolean;
10617 Lo : Uint;
10618 Hi : Uint;
10620 begin
10621 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
10622 Error_Msg_Name_1 := Chars (Typ);
10623 Check_SPARK_05_Restriction
10624 ("unary operator not defined for modular type%", N);
10625 end if;
10627 -- Deal with intrinsic unary operators
10629 if Comes_From_Source (N)
10630 and then Ekind (Entity (N)) = E_Function
10631 and then Is_Imported (Entity (N))
10632 and then Is_Intrinsic_Subprogram (Entity (N))
10633 then
10634 Resolve_Intrinsic_Unary_Operator (N, Typ);
10635 return;
10636 end if;
10638 -- Deal with universal cases
10640 if Etype (R) = Universal_Integer
10641 or else
10642 Etype (R) = Universal_Real
10643 then
10644 Check_For_Visible_Operator (N, B_Typ);
10645 end if;
10647 Set_Etype (N, B_Typ);
10648 Resolve (R, B_Typ);
10650 -- Generate warning for expressions like abs (x mod 2)
10652 if Warn_On_Redundant_Constructs
10653 and then Nkind (N) = N_Op_Abs
10654 then
10655 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
10657 if OK and then Hi >= Lo and then Lo >= 0 then
10658 Error_Msg_N -- CODEFIX
10659 ("?r?abs applied to known non-negative value has no effect", N);
10660 end if;
10661 end if;
10663 -- Deal with reference generation
10665 Check_Unset_Reference (R);
10666 Generate_Operator_Reference (N, B_Typ);
10667 Analyze_Dimension (N);
10668 Eval_Unary_Op (N);
10670 -- Set overflow checking bit. Much cleverer code needed here eventually
10671 -- and perhaps the Resolve routines should be separated for the various
10672 -- arithmetic operations, since they will need different processing ???
10674 if Nkind (N) in N_Op then
10675 if not Overflow_Checks_Suppressed (Etype (N)) then
10676 Enable_Overflow_Check (N);
10677 end if;
10678 end if;
10680 -- Generate warning for expressions like -5 mod 3 for integers. No need
10681 -- to worry in the floating-point case, since parens do not affect the
10682 -- result so there is no point in giving in a warning.
10684 declare
10685 Norig : constant Node_Id := Original_Node (N);
10686 Rorig : Node_Id;
10687 Val : Uint;
10688 HB : Uint;
10689 LB : Uint;
10690 Lval : Uint;
10691 Opnd : Node_Id;
10693 begin
10694 if Warn_On_Questionable_Missing_Parens
10695 and then Comes_From_Source (Norig)
10696 and then Is_Integer_Type (Typ)
10697 and then Nkind (Norig) = N_Op_Minus
10698 then
10699 Rorig := Original_Node (Right_Opnd (Norig));
10701 -- We are looking for cases where the right operand is not
10702 -- parenthesized, and is a binary operator, multiply, divide, or
10703 -- mod. These are the cases where the grouping can affect results.
10705 if Paren_Count (Rorig) = 0
10706 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
10707 then
10708 -- For mod, we always give the warning, since the value is
10709 -- affected by the parenthesization (e.g. (-5) mod 315 /=
10710 -- -(5 mod 315)). But for the other cases, the only concern is
10711 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
10712 -- overflows, but (-2) * 64 does not). So we try to give the
10713 -- message only when overflow is possible.
10715 if Nkind (Rorig) /= N_Op_Mod
10716 and then Compile_Time_Known_Value (R)
10717 then
10718 Val := Expr_Value (R);
10720 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
10721 HB := Expr_Value (Type_High_Bound (Typ));
10722 else
10723 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
10724 end if;
10726 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
10727 LB := Expr_Value (Type_Low_Bound (Typ));
10728 else
10729 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
10730 end if;
10732 -- Note that the test below is deliberately excluding the
10733 -- largest negative number, since that is a potentially
10734 -- troublesome case (e.g. -2 * x, where the result is the
10735 -- largest negative integer has an overflow with 2 * x).
10737 if Val > LB and then Val <= HB then
10738 return;
10739 end if;
10740 end if;
10742 -- For the multiplication case, the only case we have to worry
10743 -- about is when (-a)*b is exactly the largest negative number
10744 -- so that -(a*b) can cause overflow. This can only happen if
10745 -- a is a power of 2, and more generally if any operand is a
10746 -- constant that is not a power of 2, then the parentheses
10747 -- cannot affect whether overflow occurs. We only bother to
10748 -- test the left most operand
10750 -- Loop looking at left operands for one that has known value
10752 Opnd := Rorig;
10753 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
10754 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
10755 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
10757 -- Operand value of 0 or 1 skips warning
10759 if Lval <= 1 then
10760 return;
10762 -- Otherwise check power of 2, if power of 2, warn, if
10763 -- anything else, skip warning.
10765 else
10766 while Lval /= 2 loop
10767 if Lval mod 2 = 1 then
10768 return;
10769 else
10770 Lval := Lval / 2;
10771 end if;
10772 end loop;
10774 exit Opnd_Loop;
10775 end if;
10776 end if;
10778 -- Keep looking at left operands
10780 Opnd := Left_Opnd (Opnd);
10781 end loop Opnd_Loop;
10783 -- For rem or "/" we can only have a problematic situation
10784 -- if the divisor has a value of minus one or one. Otherwise
10785 -- overflow is impossible (divisor > 1) or we have a case of
10786 -- division by zero in any case.
10788 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
10789 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
10790 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
10791 then
10792 return;
10793 end if;
10795 -- If we fall through warning should be issued
10797 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
10799 Error_Msg_N
10800 ("??unary minus expression should be parenthesized here!", N);
10801 end if;
10802 end if;
10803 end;
10804 end Resolve_Unary_Op;
10806 ----------------------------------
10807 -- Resolve_Unchecked_Expression --
10808 ----------------------------------
10810 procedure Resolve_Unchecked_Expression
10811 (N : Node_Id;
10812 Typ : Entity_Id)
10814 begin
10815 Resolve (Expression (N), Typ, Suppress => All_Checks);
10816 Set_Etype (N, Typ);
10817 end Resolve_Unchecked_Expression;
10819 ---------------------------------------
10820 -- Resolve_Unchecked_Type_Conversion --
10821 ---------------------------------------
10823 procedure Resolve_Unchecked_Type_Conversion
10824 (N : Node_Id;
10825 Typ : Entity_Id)
10827 pragma Warnings (Off, Typ);
10829 Operand : constant Node_Id := Expression (N);
10830 Opnd_Type : constant Entity_Id := Etype (Operand);
10832 begin
10833 -- Resolve operand using its own type
10835 Resolve (Operand, Opnd_Type);
10837 -- In an inlined context, the unchecked conversion may be applied
10838 -- to a literal, in which case its type is the type of the context.
10839 -- (In other contexts conversions cannot apply to literals).
10841 if In_Inlined_Body
10842 and then (Opnd_Type = Any_Character or else
10843 Opnd_Type = Any_Integer or else
10844 Opnd_Type = Any_Real)
10845 then
10846 Set_Etype (Operand, Typ);
10847 end if;
10849 Analyze_Dimension (N);
10850 Eval_Unchecked_Conversion (N);
10851 end Resolve_Unchecked_Type_Conversion;
10853 ------------------------------
10854 -- Rewrite_Operator_As_Call --
10855 ------------------------------
10857 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
10858 Loc : constant Source_Ptr := Sloc (N);
10859 Actuals : constant List_Id := New_List;
10860 New_N : Node_Id;
10862 begin
10863 if Nkind (N) in N_Binary_Op then
10864 Append (Left_Opnd (N), Actuals);
10865 end if;
10867 Append (Right_Opnd (N), Actuals);
10869 New_N :=
10870 Make_Function_Call (Sloc => Loc,
10871 Name => New_Occurrence_Of (Nam, Loc),
10872 Parameter_Associations => Actuals);
10874 Preserve_Comes_From_Source (New_N, N);
10875 Preserve_Comes_From_Source (Name (New_N), N);
10876 Rewrite (N, New_N);
10877 Set_Etype (N, Etype (Nam));
10878 end Rewrite_Operator_As_Call;
10880 ------------------------------
10881 -- Rewrite_Renamed_Operator --
10882 ------------------------------
10884 procedure Rewrite_Renamed_Operator
10885 (N : Node_Id;
10886 Op : Entity_Id;
10887 Typ : Entity_Id)
10889 Nam : constant Name_Id := Chars (Op);
10890 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
10891 Op_Node : Node_Id;
10893 begin
10894 -- Do not perform this transformation within a pre/postcondition,
10895 -- because the expression will be re-analyzed, and the transformation
10896 -- might affect the visibility of the operator, e.g. in an instance.
10898 if In_Assertion_Expr > 0 then
10899 return;
10900 end if;
10902 -- Rewrite the operator node using the real operator, not its renaming.
10903 -- Exclude user-defined intrinsic operations of the same name, which are
10904 -- treated separately and rewritten as calls.
10906 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
10907 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
10908 Set_Chars (Op_Node, Nam);
10909 Set_Etype (Op_Node, Etype (N));
10910 Set_Entity (Op_Node, Op);
10911 Set_Right_Opnd (Op_Node, Right_Opnd (N));
10913 -- Indicate that both the original entity and its renaming are
10914 -- referenced at this point.
10916 Generate_Reference (Entity (N), N);
10917 Generate_Reference (Op, N);
10919 if Is_Binary then
10920 Set_Left_Opnd (Op_Node, Left_Opnd (N));
10921 end if;
10923 Rewrite (N, Op_Node);
10925 -- If the context type is private, add the appropriate conversions so
10926 -- that the operator is applied to the full view. This is done in the
10927 -- routines that resolve intrinsic operators.
10929 if Is_Intrinsic_Subprogram (Op)
10930 and then Is_Private_Type (Typ)
10931 then
10932 case Nkind (N) is
10933 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
10934 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
10935 Resolve_Intrinsic_Operator (N, Typ);
10937 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
10938 Resolve_Intrinsic_Unary_Operator (N, Typ);
10940 when others =>
10941 Resolve (N, Typ);
10942 end case;
10943 end if;
10945 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
10947 -- Operator renames a user-defined operator of the same name. Use the
10948 -- original operator in the node, which is the one Gigi knows about.
10950 Set_Entity (N, Op);
10951 Set_Is_Overloaded (N, False);
10952 end if;
10953 end Rewrite_Renamed_Operator;
10955 -----------------------
10956 -- Set_Slice_Subtype --
10957 -----------------------
10959 -- Build an implicit subtype declaration to represent the type delivered by
10960 -- the slice. This is an abbreviated version of an array subtype. We define
10961 -- an index subtype for the slice, using either the subtype name or the
10962 -- discrete range of the slice. To be consistent with index usage elsewhere
10963 -- we create a list header to hold the single index. This list is not
10964 -- otherwise attached to the syntax tree.
10966 procedure Set_Slice_Subtype (N : Node_Id) is
10967 Loc : constant Source_Ptr := Sloc (N);
10968 Index_List : constant List_Id := New_List;
10969 Index : Node_Id;
10970 Index_Subtype : Entity_Id;
10971 Index_Type : Entity_Id;
10972 Slice_Subtype : Entity_Id;
10973 Drange : constant Node_Id := Discrete_Range (N);
10975 begin
10976 Index_Type := Base_Type (Etype (Drange));
10978 if Is_Entity_Name (Drange) then
10979 Index_Subtype := Entity (Drange);
10981 else
10982 -- We force the evaluation of a range. This is definitely needed in
10983 -- the renamed case, and seems safer to do unconditionally. Note in
10984 -- any case that since we will create and insert an Itype referring
10985 -- to this range, we must make sure any side effect removal actions
10986 -- are inserted before the Itype definition.
10988 if Nkind (Drange) = N_Range then
10989 Force_Evaluation (Low_Bound (Drange));
10990 Force_Evaluation (High_Bound (Drange));
10992 -- If the discrete range is given by a subtype indication, the
10993 -- type of the slice is the base of the subtype mark.
10995 elsif Nkind (Drange) = N_Subtype_Indication then
10996 declare
10997 R : constant Node_Id := Range_Expression (Constraint (Drange));
10998 begin
10999 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
11000 Force_Evaluation (Low_Bound (R));
11001 Force_Evaluation (High_Bound (R));
11002 end;
11003 end if;
11005 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
11007 -- Take a new copy of Drange (where bounds have been rewritten to
11008 -- reference side-effect-free names). Using a separate tree ensures
11009 -- that further expansion (e.g. while rewriting a slice assignment
11010 -- into a FOR loop) does not attempt to remove side effects on the
11011 -- bounds again (which would cause the bounds in the index subtype
11012 -- definition to refer to temporaries before they are defined) (the
11013 -- reason is that some names are considered side effect free here
11014 -- for the subtype, but not in the context of a loop iteration
11015 -- scheme).
11017 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
11018 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
11019 Set_Etype (Index_Subtype, Index_Type);
11020 Set_Size_Info (Index_Subtype, Index_Type);
11021 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
11022 end if;
11024 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
11026 Index := New_Occurrence_Of (Index_Subtype, Loc);
11027 Set_Etype (Index, Index_Subtype);
11028 Append (Index, Index_List);
11030 Set_First_Index (Slice_Subtype, Index);
11031 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
11032 Set_Is_Constrained (Slice_Subtype, True);
11034 Check_Compile_Time_Size (Slice_Subtype);
11036 -- The Etype of the existing Slice node is reset to this slice subtype.
11037 -- Its bounds are obtained from its first index.
11039 Set_Etype (N, Slice_Subtype);
11041 -- For packed slice subtypes, freeze immediately (except in the case of
11042 -- being in a "spec expression" where we never freeze when we first see
11043 -- the expression).
11045 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
11046 Freeze_Itype (Slice_Subtype, N);
11048 -- For all other cases insert an itype reference in the slice's actions
11049 -- so that the itype is frozen at the proper place in the tree (i.e. at
11050 -- the point where actions for the slice are analyzed). Note that this
11051 -- is different from freezing the itype immediately, which might be
11052 -- premature (e.g. if the slice is within a transient scope). This needs
11053 -- to be done only if expansion is enabled.
11055 elsif Expander_Active then
11056 Ensure_Defined (Typ => Slice_Subtype, N => N);
11057 end if;
11058 end Set_Slice_Subtype;
11060 --------------------------------
11061 -- Set_String_Literal_Subtype --
11062 --------------------------------
11064 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
11065 Loc : constant Source_Ptr := Sloc (N);
11066 Low_Bound : constant Node_Id :=
11067 Type_Low_Bound (Etype (First_Index (Typ)));
11068 Subtype_Id : Entity_Id;
11070 begin
11071 if Nkind (N) /= N_String_Literal then
11072 return;
11073 end if;
11075 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
11076 Set_String_Literal_Length (Subtype_Id, UI_From_Int
11077 (String_Length (Strval (N))));
11078 Set_Etype (Subtype_Id, Base_Type (Typ));
11079 Set_Is_Constrained (Subtype_Id);
11080 Set_Etype (N, Subtype_Id);
11082 -- The low bound is set from the low bound of the corresponding index
11083 -- type. Note that we do not store the high bound in the string literal
11084 -- subtype, but it can be deduced if necessary from the length and the
11085 -- low bound.
11087 if Is_OK_Static_Expression (Low_Bound) then
11088 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
11090 -- If the lower bound is not static we create a range for the string
11091 -- literal, using the index type and the known length of the literal.
11092 -- The index type is not necessarily Positive, so the upper bound is
11093 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
11095 else
11096 declare
11097 Index_List : constant List_Id := New_List;
11098 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
11099 High_Bound : constant Node_Id :=
11100 Make_Attribute_Reference (Loc,
11101 Attribute_Name => Name_Val,
11102 Prefix =>
11103 New_Occurrence_Of (Index_Type, Loc),
11104 Expressions => New_List (
11105 Make_Op_Add (Loc,
11106 Left_Opnd =>
11107 Make_Attribute_Reference (Loc,
11108 Attribute_Name => Name_Pos,
11109 Prefix =>
11110 New_Occurrence_Of (Index_Type, Loc),
11111 Expressions =>
11112 New_List (New_Copy_Tree (Low_Bound))),
11113 Right_Opnd =>
11114 Make_Integer_Literal (Loc,
11115 String_Length (Strval (N)) - 1))));
11117 Array_Subtype : Entity_Id;
11118 Drange : Node_Id;
11119 Index : Node_Id;
11120 Index_Subtype : Entity_Id;
11122 begin
11123 if Is_Integer_Type (Index_Type) then
11124 Set_String_Literal_Low_Bound
11125 (Subtype_Id, Make_Integer_Literal (Loc, 1));
11127 else
11128 -- If the index type is an enumeration type, build bounds
11129 -- expression with attributes.
11131 Set_String_Literal_Low_Bound
11132 (Subtype_Id,
11133 Make_Attribute_Reference (Loc,
11134 Attribute_Name => Name_First,
11135 Prefix =>
11136 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
11137 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
11138 end if;
11140 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
11142 -- Build bona fide subtype for the string, and wrap it in an
11143 -- unchecked conversion, because the backend expects the
11144 -- String_Literal_Subtype to have a static lower bound.
11146 Index_Subtype :=
11147 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
11148 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
11149 Set_Scalar_Range (Index_Subtype, Drange);
11150 Set_Parent (Drange, N);
11151 Analyze_And_Resolve (Drange, Index_Type);
11153 -- In the context, the Index_Type may already have a constraint,
11154 -- so use common base type on string subtype. The base type may
11155 -- be used when generating attributes of the string, for example
11156 -- in the context of a slice assignment.
11158 Set_Etype (Index_Subtype, Base_Type (Index_Type));
11159 Set_Size_Info (Index_Subtype, Index_Type);
11160 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
11162 Array_Subtype := Create_Itype (E_Array_Subtype, N);
11164 Index := New_Occurrence_Of (Index_Subtype, Loc);
11165 Set_Etype (Index, Index_Subtype);
11166 Append (Index, Index_List);
11168 Set_First_Index (Array_Subtype, Index);
11169 Set_Etype (Array_Subtype, Base_Type (Typ));
11170 Set_Is_Constrained (Array_Subtype, True);
11172 Rewrite (N,
11173 Make_Unchecked_Type_Conversion (Loc,
11174 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
11175 Expression => Relocate_Node (N)));
11176 Set_Etype (N, Array_Subtype);
11177 end;
11178 end if;
11179 end Set_String_Literal_Subtype;
11181 ------------------------------
11182 -- Simplify_Type_Conversion --
11183 ------------------------------
11185 procedure Simplify_Type_Conversion (N : Node_Id) is
11186 begin
11187 if Nkind (N) = N_Type_Conversion then
11188 declare
11189 Operand : constant Node_Id := Expression (N);
11190 Target_Typ : constant Entity_Id := Etype (N);
11191 Opnd_Typ : constant Entity_Id := Etype (Operand);
11193 begin
11194 -- Special processing if the conversion is the expression of a
11195 -- Rounding or Truncation attribute reference. In this case we
11196 -- replace:
11198 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
11200 -- by
11202 -- ityp (x)
11204 -- with the Float_Truncate flag set to False or True respectively,
11205 -- which is more efficient.
11207 if Is_Floating_Point_Type (Opnd_Typ)
11208 and then
11209 (Is_Integer_Type (Target_Typ)
11210 or else (Is_Fixed_Point_Type (Target_Typ)
11211 and then Conversion_OK (N)))
11212 and then Nkind (Operand) = N_Attribute_Reference
11213 and then Nam_In (Attribute_Name (Operand), Name_Rounding,
11214 Name_Truncation)
11215 then
11216 declare
11217 Truncate : constant Boolean :=
11218 Attribute_Name (Operand) = Name_Truncation;
11219 begin
11220 Rewrite (Operand,
11221 Relocate_Node (First (Expressions (Operand))));
11222 Set_Float_Truncate (N, Truncate);
11223 end;
11224 end if;
11225 end;
11226 end if;
11227 end Simplify_Type_Conversion;
11229 -----------------------------
11230 -- Unique_Fixed_Point_Type --
11231 -----------------------------
11233 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
11234 T1 : Entity_Id := Empty;
11235 T2 : Entity_Id;
11236 Item : Node_Id;
11237 Scop : Entity_Id;
11239 procedure Fixed_Point_Error;
11240 -- Give error messages for true ambiguity. Messages are posted on node
11241 -- N, and entities T1, T2 are the possible interpretations.
11243 -----------------------
11244 -- Fixed_Point_Error --
11245 -----------------------
11247 procedure Fixed_Point_Error is
11248 begin
11249 Error_Msg_N ("ambiguous universal_fixed_expression", N);
11250 Error_Msg_NE ("\\possible interpretation as}", N, T1);
11251 Error_Msg_NE ("\\possible interpretation as}", N, T2);
11252 end Fixed_Point_Error;
11254 -- Start of processing for Unique_Fixed_Point_Type
11256 begin
11257 -- The operations on Duration are visible, so Duration is always a
11258 -- possible interpretation.
11260 T1 := Standard_Duration;
11262 -- Look for fixed-point types in enclosing scopes
11264 Scop := Current_Scope;
11265 while Scop /= Standard_Standard loop
11266 T2 := First_Entity (Scop);
11267 while Present (T2) loop
11268 if Is_Fixed_Point_Type (T2)
11269 and then Current_Entity (T2) = T2
11270 and then Scope (Base_Type (T2)) = Scop
11271 then
11272 if Present (T1) then
11273 Fixed_Point_Error;
11274 return Any_Type;
11275 else
11276 T1 := T2;
11277 end if;
11278 end if;
11280 Next_Entity (T2);
11281 end loop;
11283 Scop := Scope (Scop);
11284 end loop;
11286 -- Look for visible fixed type declarations in the context
11288 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
11289 while Present (Item) loop
11290 if Nkind (Item) = N_With_Clause then
11291 Scop := Entity (Name (Item));
11292 T2 := First_Entity (Scop);
11293 while Present (T2) loop
11294 if Is_Fixed_Point_Type (T2)
11295 and then Scope (Base_Type (T2)) = Scop
11296 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
11297 then
11298 if Present (T1) then
11299 Fixed_Point_Error;
11300 return Any_Type;
11301 else
11302 T1 := T2;
11303 end if;
11304 end if;
11306 Next_Entity (T2);
11307 end loop;
11308 end if;
11310 Next (Item);
11311 end loop;
11313 if Nkind (N) = N_Real_Literal then
11314 Error_Msg_NE
11315 ("??real literal interpreted as }!", N, T1);
11316 else
11317 Error_Msg_NE
11318 ("??universal_fixed expression interpreted as }!", N, T1);
11319 end if;
11321 return T1;
11322 end Unique_Fixed_Point_Type;
11324 ----------------------
11325 -- Valid_Conversion --
11326 ----------------------
11328 function Valid_Conversion
11329 (N : Node_Id;
11330 Target : Entity_Id;
11331 Operand : Node_Id;
11332 Report_Errs : Boolean := True) return Boolean
11334 Target_Type : constant Entity_Id := Base_Type (Target);
11335 Opnd_Type : Entity_Id := Etype (Operand);
11336 Inc_Ancestor : Entity_Id;
11338 function Conversion_Check
11339 (Valid : Boolean;
11340 Msg : String) return Boolean;
11341 -- Little routine to post Msg if Valid is False, returns Valid value
11343 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
11344 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
11346 procedure Conversion_Error_NE
11347 (Msg : String;
11348 N : Node_Or_Entity_Id;
11349 E : Node_Or_Entity_Id);
11350 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
11352 function Valid_Tagged_Conversion
11353 (Target_Type : Entity_Id;
11354 Opnd_Type : Entity_Id) return Boolean;
11355 -- Specifically test for validity of tagged conversions
11357 function Valid_Array_Conversion return Boolean;
11358 -- Check index and component conformance, and accessibility levels if
11359 -- the component types are anonymous access types (Ada 2005).
11361 ----------------------
11362 -- Conversion_Check --
11363 ----------------------
11365 function Conversion_Check
11366 (Valid : Boolean;
11367 Msg : String) return Boolean
11369 begin
11370 if not Valid
11372 -- A generic unit has already been analyzed and we have verified
11373 -- that a particular conversion is OK in that context. Since the
11374 -- instance is reanalyzed without relying on the relationships
11375 -- established during the analysis of the generic, it is possible
11376 -- to end up with inconsistent views of private types. Do not emit
11377 -- the error message in such cases. The rest of the machinery in
11378 -- Valid_Conversion still ensures the proper compatibility of
11379 -- target and operand types.
11381 and then not In_Instance
11382 then
11383 Conversion_Error_N (Msg, Operand);
11384 end if;
11386 return Valid;
11387 end Conversion_Check;
11389 ------------------------
11390 -- Conversion_Error_N --
11391 ------------------------
11393 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
11394 begin
11395 if Report_Errs then
11396 Error_Msg_N (Msg, N);
11397 end if;
11398 end Conversion_Error_N;
11400 -------------------------
11401 -- Conversion_Error_NE --
11402 -------------------------
11404 procedure Conversion_Error_NE
11405 (Msg : String;
11406 N : Node_Or_Entity_Id;
11407 E : Node_Or_Entity_Id)
11409 begin
11410 if Report_Errs then
11411 Error_Msg_NE (Msg, N, E);
11412 end if;
11413 end Conversion_Error_NE;
11415 ----------------------------
11416 -- Valid_Array_Conversion --
11417 ----------------------------
11419 function Valid_Array_Conversion return Boolean
11421 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
11422 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
11424 Opnd_Index : Node_Id;
11425 Opnd_Index_Type : Entity_Id;
11427 Target_Comp_Type : constant Entity_Id :=
11428 Component_Type (Target_Type);
11429 Target_Comp_Base : constant Entity_Id :=
11430 Base_Type (Target_Comp_Type);
11432 Target_Index : Node_Id;
11433 Target_Index_Type : Entity_Id;
11435 begin
11436 -- Error if wrong number of dimensions
11439 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
11440 then
11441 Conversion_Error_N
11442 ("incompatible number of dimensions for conversion", Operand);
11443 return False;
11445 -- Number of dimensions matches
11447 else
11448 -- Loop through indexes of the two arrays
11450 Target_Index := First_Index (Target_Type);
11451 Opnd_Index := First_Index (Opnd_Type);
11452 while Present (Target_Index) and then Present (Opnd_Index) loop
11453 Target_Index_Type := Etype (Target_Index);
11454 Opnd_Index_Type := Etype (Opnd_Index);
11456 -- Error if index types are incompatible
11458 if not (Is_Integer_Type (Target_Index_Type)
11459 and then Is_Integer_Type (Opnd_Index_Type))
11460 and then (Root_Type (Target_Index_Type)
11461 /= Root_Type (Opnd_Index_Type))
11462 then
11463 Conversion_Error_N
11464 ("incompatible index types for array conversion",
11465 Operand);
11466 return False;
11467 end if;
11469 Next_Index (Target_Index);
11470 Next_Index (Opnd_Index);
11471 end loop;
11473 -- If component types have same base type, all set
11475 if Target_Comp_Base = Opnd_Comp_Base then
11476 null;
11478 -- Here if base types of components are not the same. The only
11479 -- time this is allowed is if we have anonymous access types.
11481 -- The conversion of arrays of anonymous access types can lead
11482 -- to dangling pointers. AI-392 formalizes the accessibility
11483 -- checks that must be applied to such conversions to prevent
11484 -- out-of-scope references.
11486 elsif Ekind_In
11487 (Target_Comp_Base, E_Anonymous_Access_Type,
11488 E_Anonymous_Access_Subprogram_Type)
11489 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
11490 and then
11491 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
11492 then
11493 if Type_Access_Level (Target_Type) <
11494 Deepest_Type_Access_Level (Opnd_Type)
11495 then
11496 if In_Instance_Body then
11497 Error_Msg_Warn := SPARK_Mode /= On;
11498 Conversion_Error_N
11499 ("source array type has deeper accessibility "
11500 & "level than target<<", Operand);
11501 Conversion_Error_N ("\Program_Error [<<", Operand);
11502 Rewrite (N,
11503 Make_Raise_Program_Error (Sloc (N),
11504 Reason => PE_Accessibility_Check_Failed));
11505 Set_Etype (N, Target_Type);
11506 return False;
11508 -- Conversion not allowed because of accessibility levels
11510 else
11511 Conversion_Error_N
11512 ("source array type has deeper accessibility "
11513 & "level than target", Operand);
11514 return False;
11515 end if;
11517 else
11518 null;
11519 end if;
11521 -- All other cases where component base types do not match
11523 else
11524 Conversion_Error_N
11525 ("incompatible component types for array conversion",
11526 Operand);
11527 return False;
11528 end if;
11530 -- Check that component subtypes statically match. For numeric
11531 -- types this means that both must be either constrained or
11532 -- unconstrained. For enumeration types the bounds must match.
11533 -- All of this is checked in Subtypes_Statically_Match.
11535 if not Subtypes_Statically_Match
11536 (Target_Comp_Type, Opnd_Comp_Type)
11537 then
11538 Conversion_Error_N
11539 ("component subtypes must statically match", Operand);
11540 return False;
11541 end if;
11542 end if;
11544 return True;
11545 end Valid_Array_Conversion;
11547 -----------------------------
11548 -- Valid_Tagged_Conversion --
11549 -----------------------------
11551 function Valid_Tagged_Conversion
11552 (Target_Type : Entity_Id;
11553 Opnd_Type : Entity_Id) return Boolean
11555 begin
11556 -- Upward conversions are allowed (RM 4.6(22))
11558 if Covers (Target_Type, Opnd_Type)
11559 or else Is_Ancestor (Target_Type, Opnd_Type)
11560 then
11561 return True;
11563 -- Downward conversion are allowed if the operand is class-wide
11564 -- (RM 4.6(23)).
11566 elsif Is_Class_Wide_Type (Opnd_Type)
11567 and then Covers (Opnd_Type, Target_Type)
11568 then
11569 return True;
11571 elsif Covers (Opnd_Type, Target_Type)
11572 or else Is_Ancestor (Opnd_Type, Target_Type)
11573 then
11574 return
11575 Conversion_Check (False,
11576 "downward conversion of tagged objects not allowed");
11578 -- Ada 2005 (AI-251): The conversion to/from interface types is
11579 -- always valid
11581 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
11582 return True;
11584 -- If the operand is a class-wide type obtained through a limited_
11585 -- with clause, and the context includes the non-limited view, use
11586 -- it to determine whether the conversion is legal.
11588 elsif Is_Class_Wide_Type (Opnd_Type)
11589 and then From_Limited_With (Opnd_Type)
11590 and then Present (Non_Limited_View (Etype (Opnd_Type)))
11591 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
11592 then
11593 return True;
11595 elsif Is_Access_Type (Opnd_Type)
11596 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
11597 then
11598 return True;
11600 else
11601 Conversion_Error_NE
11602 ("invalid tagged conversion, not compatible with}",
11603 N, First_Subtype (Opnd_Type));
11604 return False;
11605 end if;
11606 end Valid_Tagged_Conversion;
11608 -- Start of processing for Valid_Conversion
11610 begin
11611 Check_Parameterless_Call (Operand);
11613 if Is_Overloaded (Operand) then
11614 declare
11615 I : Interp_Index;
11616 I1 : Interp_Index;
11617 It : Interp;
11618 It1 : Interp;
11619 N1 : Entity_Id;
11620 T1 : Entity_Id;
11622 begin
11623 -- Remove procedure calls, which syntactically cannot appear in
11624 -- this context, but which cannot be removed by type checking,
11625 -- because the context does not impose a type.
11627 -- The node may be labelled overloaded, but still contain only one
11628 -- interpretation because others were discarded earlier. If this
11629 -- is the case, retain the single interpretation if legal.
11631 Get_First_Interp (Operand, I, It);
11632 Opnd_Type := It.Typ;
11633 Get_Next_Interp (I, It);
11635 if Present (It.Typ)
11636 and then Opnd_Type /= Standard_Void_Type
11637 then
11638 -- More than one candidate interpretation is available
11640 Get_First_Interp (Operand, I, It);
11641 while Present (It.Typ) loop
11642 if It.Typ = Standard_Void_Type then
11643 Remove_Interp (I);
11644 end if;
11646 -- When compiling for a system where Address is of a visible
11647 -- integer type, spurious ambiguities can be produced when
11648 -- arithmetic operations have a literal operand and return
11649 -- System.Address or a descendant of it. These ambiguities
11650 -- are usually resolved by the context, but for conversions
11651 -- there is no context type and the removal of the spurious
11652 -- operations must be done explicitly here.
11654 if not Address_Is_Private
11655 and then Is_Descendent_Of_Address (It.Typ)
11656 then
11657 Remove_Interp (I);
11658 end if;
11660 Get_Next_Interp (I, It);
11661 end loop;
11662 end if;
11664 Get_First_Interp (Operand, I, It);
11665 I1 := I;
11666 It1 := It;
11668 if No (It.Typ) then
11669 Conversion_Error_N ("illegal operand in conversion", Operand);
11670 return False;
11671 end if;
11673 Get_Next_Interp (I, It);
11675 if Present (It.Typ) then
11676 N1 := It1.Nam;
11677 T1 := It1.Typ;
11678 It1 := Disambiguate (Operand, I1, I, Any_Type);
11680 if It1 = No_Interp then
11681 Conversion_Error_N
11682 ("ambiguous operand in conversion", Operand);
11684 -- If the interpretation involves a standard operator, use
11685 -- the location of the type, which may be user-defined.
11687 if Sloc (It.Nam) = Standard_Location then
11688 Error_Msg_Sloc := Sloc (It.Typ);
11689 else
11690 Error_Msg_Sloc := Sloc (It.Nam);
11691 end if;
11693 Conversion_Error_N -- CODEFIX
11694 ("\\possible interpretation#!", Operand);
11696 if Sloc (N1) = Standard_Location then
11697 Error_Msg_Sloc := Sloc (T1);
11698 else
11699 Error_Msg_Sloc := Sloc (N1);
11700 end if;
11702 Conversion_Error_N -- CODEFIX
11703 ("\\possible interpretation#!", Operand);
11705 return False;
11706 end if;
11707 end if;
11709 Set_Etype (Operand, It1.Typ);
11710 Opnd_Type := It1.Typ;
11711 end;
11712 end if;
11714 -- Deal with conversion of integer type to address if the pragma
11715 -- Allow_Integer_Address is in effect. We convert the conversion to
11716 -- an unchecked conversion in this case and we are all done.
11718 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
11719 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
11720 Analyze_And_Resolve (N, Target_Type);
11721 return True;
11722 end if;
11724 -- If we are within a child unit, check whether the type of the
11725 -- expression has an ancestor in a parent unit, in which case it
11726 -- belongs to its derivation class even if the ancestor is private.
11727 -- See RM 7.3.1 (5.2/3).
11729 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
11731 -- Numeric types
11733 if Is_Numeric_Type (Target_Type) then
11735 -- A universal fixed expression can be converted to any numeric type
11737 if Opnd_Type = Universal_Fixed then
11738 return True;
11740 -- Also no need to check when in an instance or inlined body, because
11741 -- the legality has been established when the template was analyzed.
11742 -- Furthermore, numeric conversions may occur where only a private
11743 -- view of the operand type is visible at the instantiation point.
11744 -- This results in a spurious error if we check that the operand type
11745 -- is a numeric type.
11747 -- Note: in a previous version of this unit, the following tests were
11748 -- applied only for generated code (Comes_From_Source set to False),
11749 -- but in fact the test is required for source code as well, since
11750 -- this situation can arise in source code.
11752 elsif In_Instance or else In_Inlined_Body then
11753 return True;
11755 -- Otherwise we need the conversion check
11757 else
11758 return Conversion_Check
11759 (Is_Numeric_Type (Opnd_Type)
11760 or else
11761 (Present (Inc_Ancestor)
11762 and then Is_Numeric_Type (Inc_Ancestor)),
11763 "illegal operand for numeric conversion");
11764 end if;
11766 -- Array types
11768 elsif Is_Array_Type (Target_Type) then
11769 if not Is_Array_Type (Opnd_Type)
11770 or else Opnd_Type = Any_Composite
11771 or else Opnd_Type = Any_String
11772 then
11773 Conversion_Error_N
11774 ("illegal operand for array conversion", Operand);
11775 return False;
11777 else
11778 return Valid_Array_Conversion;
11779 end if;
11781 -- Ada 2005 (AI-251): Anonymous access types where target references an
11782 -- interface type.
11784 elsif Ekind_In (Target_Type, E_General_Access_Type,
11785 E_Anonymous_Access_Type)
11786 and then Is_Interface (Directly_Designated_Type (Target_Type))
11787 then
11788 -- Check the static accessibility rule of 4.6(17). Note that the
11789 -- check is not enforced when within an instance body, since the
11790 -- RM requires such cases to be caught at run time.
11792 -- If the operand is a rewriting of an allocator no check is needed
11793 -- because there are no accessibility issues.
11795 if Nkind (Original_Node (N)) = N_Allocator then
11796 null;
11798 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
11799 if Type_Access_Level (Opnd_Type) >
11800 Deepest_Type_Access_Level (Target_Type)
11801 then
11802 -- In an instance, this is a run-time check, but one we know
11803 -- will fail, so generate an appropriate warning. The raise
11804 -- will be generated by Expand_N_Type_Conversion.
11806 if In_Instance_Body then
11807 Error_Msg_Warn := SPARK_Mode /= On;
11808 Conversion_Error_N
11809 ("cannot convert local pointer to non-local access type<<",
11810 Operand);
11811 Conversion_Error_N ("\Program_Error [<<", Operand);
11813 else
11814 Conversion_Error_N
11815 ("cannot convert local pointer to non-local access type",
11816 Operand);
11817 return False;
11818 end if;
11820 -- Special accessibility checks are needed in the case of access
11821 -- discriminants declared for a limited type.
11823 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11824 and then not Is_Local_Anonymous_Access (Opnd_Type)
11825 then
11826 -- When the operand is a selected access discriminant the check
11827 -- needs to be made against the level of the object denoted by
11828 -- the prefix of the selected name (Object_Access_Level handles
11829 -- checking the prefix of the operand for this case).
11831 if Nkind (Operand) = N_Selected_Component
11832 and then Object_Access_Level (Operand) >
11833 Deepest_Type_Access_Level (Target_Type)
11834 then
11835 -- In an instance, this is a run-time check, but one we know
11836 -- will fail, so generate an appropriate warning. The raise
11837 -- will be generated by Expand_N_Type_Conversion.
11839 if In_Instance_Body then
11840 Error_Msg_Warn := SPARK_Mode /= On;
11841 Conversion_Error_N
11842 ("cannot convert access discriminant to non-local "
11843 & "access type<<", Operand);
11844 Conversion_Error_N ("\Program_Error [<<", Operand);
11846 -- Real error if not in instance body
11848 else
11849 Conversion_Error_N
11850 ("cannot convert access discriminant to non-local "
11851 & "access type", Operand);
11852 return False;
11853 end if;
11854 end if;
11856 -- The case of a reference to an access discriminant from
11857 -- within a limited type declaration (which will appear as
11858 -- a discriminal) is always illegal because the level of the
11859 -- discriminant is considered to be deeper than any (nameable)
11860 -- access type.
11862 if Is_Entity_Name (Operand)
11863 and then not Is_Local_Anonymous_Access (Opnd_Type)
11864 and then
11865 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11866 and then Present (Discriminal_Link (Entity (Operand)))
11867 then
11868 Conversion_Error_N
11869 ("discriminant has deeper accessibility level than target",
11870 Operand);
11871 return False;
11872 end if;
11873 end if;
11874 end if;
11876 return True;
11878 -- General and anonymous access types
11880 elsif Ekind_In (Target_Type, E_General_Access_Type,
11881 E_Anonymous_Access_Type)
11882 and then
11883 Conversion_Check
11884 (Is_Access_Type (Opnd_Type)
11885 and then not
11886 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
11887 E_Access_Protected_Subprogram_Type),
11888 "must be an access-to-object type")
11889 then
11890 if Is_Access_Constant (Opnd_Type)
11891 and then not Is_Access_Constant (Target_Type)
11892 then
11893 Conversion_Error_N
11894 ("access-to-constant operand type not allowed", Operand);
11895 return False;
11896 end if;
11898 -- Check the static accessibility rule of 4.6(17). Note that the
11899 -- check is not enforced when within an instance body, since the RM
11900 -- requires such cases to be caught at run time.
11902 if Ekind (Target_Type) /= E_Anonymous_Access_Type
11903 or else Is_Local_Anonymous_Access (Target_Type)
11904 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
11905 N_Object_Declaration
11906 then
11907 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
11908 -- conversions from an anonymous access type to a named general
11909 -- access type. Such conversions are not allowed in the case of
11910 -- access parameters and stand-alone objects of an anonymous
11911 -- access type. The implicit conversion case is recognized by
11912 -- testing that Comes_From_Source is False and that it's been
11913 -- rewritten. The Comes_From_Source test isn't sufficient because
11914 -- nodes in inlined calls to predefined library routines can have
11915 -- Comes_From_Source set to False. (Is there a better way to test
11916 -- for implicit conversions???)
11918 if Ada_Version >= Ada_2012
11919 and then not Comes_From_Source (N)
11920 and then N /= Original_Node (N)
11921 and then Ekind (Target_Type) = E_General_Access_Type
11922 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
11923 then
11924 if Is_Itype (Opnd_Type) then
11926 -- Implicit conversions aren't allowed for objects of an
11927 -- anonymous access type, since such objects have nonstatic
11928 -- levels in Ada 2012.
11930 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
11931 N_Object_Declaration
11932 then
11933 Conversion_Error_N
11934 ("implicit conversion of stand-alone anonymous "
11935 & "access object not allowed", Operand);
11936 return False;
11938 -- Implicit conversions aren't allowed for anonymous access
11939 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
11940 -- is done to exclude anonymous access results.
11942 elsif not Is_Local_Anonymous_Access (Opnd_Type)
11943 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
11944 N_Function_Specification,
11945 N_Procedure_Specification)
11946 then
11947 Conversion_Error_N
11948 ("implicit conversion of anonymous access formal "
11949 & "not allowed", Operand);
11950 return False;
11952 -- This is a case where there's an enclosing object whose
11953 -- to which the "statically deeper than" relationship does
11954 -- not apply (such as an access discriminant selected from
11955 -- a dereference of an access parameter).
11957 elsif Object_Access_Level (Operand)
11958 = Scope_Depth (Standard_Standard)
11959 then
11960 Conversion_Error_N
11961 ("implicit conversion of anonymous access value "
11962 & "not allowed", Operand);
11963 return False;
11965 -- In other cases, the level of the operand's type must be
11966 -- statically less deep than that of the target type, else
11967 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
11969 elsif Type_Access_Level (Opnd_Type) >
11970 Deepest_Type_Access_Level (Target_Type)
11971 then
11972 Conversion_Error_N
11973 ("implicit conversion of anonymous access value "
11974 & "violates accessibility", Operand);
11975 return False;
11976 end if;
11977 end if;
11979 elsif Type_Access_Level (Opnd_Type) >
11980 Deepest_Type_Access_Level (Target_Type)
11981 then
11982 -- In an instance, this is a run-time check, but one we know
11983 -- will fail, so generate an appropriate warning. The raise
11984 -- will be generated by Expand_N_Type_Conversion.
11986 if In_Instance_Body then
11987 Error_Msg_Warn := SPARK_Mode /= On;
11988 Conversion_Error_N
11989 ("cannot convert local pointer to non-local access type<<",
11990 Operand);
11991 Conversion_Error_N ("\Program_Error [<<", Operand);
11993 -- If not in an instance body, this is a real error
11995 else
11996 -- Avoid generation of spurious error message
11998 if not Error_Posted (N) then
11999 Conversion_Error_N
12000 ("cannot convert local pointer to non-local access type",
12001 Operand);
12002 end if;
12004 return False;
12005 end if;
12007 -- Special accessibility checks are needed in the case of access
12008 -- discriminants declared for a limited type.
12010 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
12011 and then not Is_Local_Anonymous_Access (Opnd_Type)
12012 then
12013 -- When the operand is a selected access discriminant the check
12014 -- needs to be made against the level of the object denoted by
12015 -- the prefix of the selected name (Object_Access_Level handles
12016 -- checking the prefix of the operand for this case).
12018 if Nkind (Operand) = N_Selected_Component
12019 and then Object_Access_Level (Operand) >
12020 Deepest_Type_Access_Level (Target_Type)
12021 then
12022 -- In an instance, this is a run-time check, but one we know
12023 -- will fail, so generate an appropriate warning. The raise
12024 -- will be generated by Expand_N_Type_Conversion.
12026 if In_Instance_Body then
12027 Error_Msg_Warn := SPARK_Mode /= On;
12028 Conversion_Error_N
12029 ("cannot convert access discriminant to non-local "
12030 & "access type<<", Operand);
12031 Conversion_Error_N ("\Program_Error [<<", Operand);
12033 -- If not in an instance body, this is a real error
12035 else
12036 Conversion_Error_N
12037 ("cannot convert access discriminant to non-local "
12038 & "access type", Operand);
12039 return False;
12040 end if;
12041 end if;
12043 -- The case of a reference to an access discriminant from
12044 -- within a limited type declaration (which will appear as
12045 -- a discriminal) is always illegal because the level of the
12046 -- discriminant is considered to be deeper than any (nameable)
12047 -- access type.
12049 if Is_Entity_Name (Operand)
12050 and then
12051 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
12052 and then Present (Discriminal_Link (Entity (Operand)))
12053 then
12054 Conversion_Error_N
12055 ("discriminant has deeper accessibility level than target",
12056 Operand);
12057 return False;
12058 end if;
12059 end if;
12060 end if;
12062 -- In the presence of limited_with clauses we have to use non-limited
12063 -- views, if available.
12065 Check_Limited : declare
12066 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
12067 -- Helper function to handle limited views
12069 --------------------------
12070 -- Full_Designated_Type --
12071 --------------------------
12073 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
12074 Desig : constant Entity_Id := Designated_Type (T);
12076 begin
12077 -- Handle the limited view of a type
12079 if Is_Incomplete_Type (Desig)
12080 and then From_Limited_With (Desig)
12081 and then Present (Non_Limited_View (Desig))
12082 then
12083 return Available_View (Desig);
12084 else
12085 return Desig;
12086 end if;
12087 end Full_Designated_Type;
12089 -- Local Declarations
12091 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
12092 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
12094 Same_Base : constant Boolean :=
12095 Base_Type (Target) = Base_Type (Opnd);
12097 -- Start of processing for Check_Limited
12099 begin
12100 if Is_Tagged_Type (Target) then
12101 return Valid_Tagged_Conversion (Target, Opnd);
12103 else
12104 if not Same_Base then
12105 Conversion_Error_NE
12106 ("target designated type not compatible with }",
12107 N, Base_Type (Opnd));
12108 return False;
12110 -- Ada 2005 AI-384: legality rule is symmetric in both
12111 -- designated types. The conversion is legal (with possible
12112 -- constraint check) if either designated type is
12113 -- unconstrained.
12115 elsif Subtypes_Statically_Match (Target, Opnd)
12116 or else
12117 (Has_Discriminants (Target)
12118 and then
12119 (not Is_Constrained (Opnd)
12120 or else not Is_Constrained (Target)))
12121 then
12122 -- Special case, if Value_Size has been used to make the
12123 -- sizes different, the conversion is not allowed even
12124 -- though the subtypes statically match.
12126 if Known_Static_RM_Size (Target)
12127 and then Known_Static_RM_Size (Opnd)
12128 and then RM_Size (Target) /= RM_Size (Opnd)
12129 then
12130 Conversion_Error_NE
12131 ("target designated subtype not compatible with }",
12132 N, Opnd);
12133 Conversion_Error_NE
12134 ("\because sizes of the two designated subtypes differ",
12135 N, Opnd);
12136 return False;
12138 -- Normal case where conversion is allowed
12140 else
12141 return True;
12142 end if;
12144 else
12145 Error_Msg_NE
12146 ("target designated subtype not compatible with }",
12147 N, Opnd);
12148 return False;
12149 end if;
12150 end if;
12151 end Check_Limited;
12153 -- Access to subprogram types. If the operand is an access parameter,
12154 -- the type has a deeper accessibility that any master, and cannot be
12155 -- assigned. We must make an exception if the conversion is part of an
12156 -- assignment and the target is the return object of an extended return
12157 -- statement, because in that case the accessibility check takes place
12158 -- after the return.
12160 elsif Is_Access_Subprogram_Type (Target_Type)
12162 -- Note: this test of Opnd_Type is there to prevent entering this
12163 -- branch in the case of a remote access to subprogram type, which
12164 -- is internally represented as an E_Record_Type.
12166 and then Is_Access_Type (Opnd_Type)
12167 then
12168 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
12169 and then Is_Entity_Name (Operand)
12170 and then Ekind (Entity (Operand)) = E_In_Parameter
12171 and then
12172 (Nkind (Parent (N)) /= N_Assignment_Statement
12173 or else not Is_Entity_Name (Name (Parent (N)))
12174 or else not Is_Return_Object (Entity (Name (Parent (N)))))
12175 then
12176 Conversion_Error_N
12177 ("illegal attempt to store anonymous access to subprogram",
12178 Operand);
12179 Conversion_Error_N
12180 ("\value has deeper accessibility than any master "
12181 & "(RM 3.10.2 (13))",
12182 Operand);
12184 Error_Msg_NE
12185 ("\use named access type for& instead of access parameter",
12186 Operand, Entity (Operand));
12187 end if;
12189 -- Check that the designated types are subtype conformant
12191 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
12192 Old_Id => Designated_Type (Opnd_Type),
12193 Err_Loc => N);
12195 -- Check the static accessibility rule of 4.6(20)
12197 if Type_Access_Level (Opnd_Type) >
12198 Deepest_Type_Access_Level (Target_Type)
12199 then
12200 Conversion_Error_N
12201 ("operand type has deeper accessibility level than target",
12202 Operand);
12204 -- Check that if the operand type is declared in a generic body,
12205 -- then the target type must be declared within that same body
12206 -- (enforces last sentence of 4.6(20)).
12208 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
12209 declare
12210 O_Gen : constant Node_Id :=
12211 Enclosing_Generic_Body (Opnd_Type);
12213 T_Gen : Node_Id;
12215 begin
12216 T_Gen := Enclosing_Generic_Body (Target_Type);
12217 while Present (T_Gen) and then T_Gen /= O_Gen loop
12218 T_Gen := Enclosing_Generic_Body (T_Gen);
12219 end loop;
12221 if T_Gen /= O_Gen then
12222 Conversion_Error_N
12223 ("target type must be declared in same generic body "
12224 & "as operand type", N);
12225 end if;
12226 end;
12227 end if;
12229 return True;
12231 -- Remote access to subprogram types
12233 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
12234 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
12235 then
12236 -- It is valid to convert from one RAS type to another provided
12237 -- that their specification statically match.
12239 -- Note: at this point, remote access to subprogram types have been
12240 -- expanded to their E_Record_Type representation, and we need to
12241 -- go back to the original access type definition using the
12242 -- Corresponding_Remote_Type attribute in order to check that the
12243 -- designated profiles match.
12245 pragma Assert (Ekind (Target_Type) = E_Record_Type);
12246 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
12248 Check_Subtype_Conformant
12249 (New_Id =>
12250 Designated_Type (Corresponding_Remote_Type (Target_Type)),
12251 Old_Id =>
12252 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
12253 Err_Loc =>
12255 return True;
12257 -- If it was legal in the generic, it's legal in the instance
12259 elsif In_Instance_Body then
12260 return True;
12262 -- If both are tagged types, check legality of view conversions
12264 elsif Is_Tagged_Type (Target_Type)
12265 and then
12266 Is_Tagged_Type (Opnd_Type)
12267 then
12268 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
12270 -- Types derived from the same root type are convertible
12272 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
12273 return True;
12275 -- In an instance or an inlined body, there may be inconsistent views of
12276 -- the same type, or of types derived from a common root.
12278 elsif (In_Instance or In_Inlined_Body)
12279 and then
12280 Root_Type (Underlying_Type (Target_Type)) =
12281 Root_Type (Underlying_Type (Opnd_Type))
12282 then
12283 return True;
12285 -- Special check for common access type error case
12287 elsif Ekind (Target_Type) = E_Access_Type
12288 and then Is_Access_Type (Opnd_Type)
12289 then
12290 Conversion_Error_N ("target type must be general access type!", N);
12291 Conversion_Error_NE -- CODEFIX
12292 ("add ALL to }!", N, Target_Type);
12293 return False;
12295 -- Here we have a real conversion error
12297 else
12298 Conversion_Error_NE
12299 ("invalid conversion, not compatible with }", N, Opnd_Type);
12300 return False;
12301 end if;
12302 end Valid_Conversion;
12304 end Sem_Res;