2016-06-16 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / sem_res.adb
blob1d73bf462a1e18bbb8ff5f0706730c1be9dd79ea
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-2016, 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 Ghost; use Ghost;
41 with Inline; use Inline;
42 with Itypes; use Itypes;
43 with Lib; use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
47 with Nlists; use Nlists;
48 with Opt; use Opt;
49 with Output; use Output;
50 with Par_SCO; use Par_SCO;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Aggr; use Sem_Aggr;
57 with Sem_Attr; use Sem_Attr;
58 with Sem_Cat; use Sem_Cat;
59 with Sem_Ch4; use Sem_Ch4;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch6; use Sem_Ch6;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch13; use Sem_Ch13;
64 with Sem_Dim; use Sem_Dim;
65 with Sem_Disp; use Sem_Disp;
66 with Sem_Dist; use Sem_Dist;
67 with Sem_Elim; use Sem_Elim;
68 with Sem_Elab; use Sem_Elab;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Intr; use Sem_Intr;
71 with Sem_Util; use Sem_Util;
72 with Targparm; use Targparm;
73 with Sem_Type; use Sem_Type;
74 with Sem_Warn; use Sem_Warn;
75 with Sinfo; use Sinfo;
76 with Sinfo.CN; use Sinfo.CN;
77 with Snames; use Snames;
78 with Stand; use Stand;
79 with Stringt; use Stringt;
80 with Style; use Style;
81 with Tbuild; use Tbuild;
82 with Uintp; use Uintp;
83 with Urealp; use Urealp;
85 package body Sem_Res is
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
91 -- Second pass (top-down) type checking and overload resolution procedures
92 -- Typ is the type required by context. These procedures propagate the
93 -- type information recursively to the descendants of N. If the node is not
94 -- overloaded, its Etype is established in the first pass. If overloaded,
95 -- the Resolve routines set the correct type. For arithmetic operators, the
96 -- Etype is the base type of the context.
98 -- Note that Resolve_Attribute is separated off in Sem_Attr
100 procedure Check_Discriminant_Use (N : Node_Id);
101 -- Enforce the restrictions on the use of discriminants when constraining
102 -- a component of a discriminated type (record or concurrent type).
104 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
105 -- Given a node for an operator associated with type T, check that the
106 -- operator is visible. Operators all of whose operands are universal must
107 -- be checked for visibility during resolution because their type is not
108 -- determinable based on their operands.
110 procedure Check_Fully_Declared_Prefix
111 (Typ : Entity_Id;
112 Pref : Node_Id);
113 -- Check that the type of the prefix of a dereference is not incomplete
115 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
116 -- Given a call node, N, which is known to occur immediately within the
117 -- subprogram being called, determines whether it is a detectable case of
118 -- an infinite recursion, and if so, outputs appropriate messages. Returns
119 -- True if an infinite recursion is detected, and False otherwise.
121 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
122 -- If the type of the object being initialized uses the secondary stack
123 -- directly or indirectly, create a transient scope for the call to the
124 -- init proc. This is because we do not create transient scopes for the
125 -- initialization of individual components within the init proc itself.
126 -- Could be optimized away perhaps?
128 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
129 -- N is the node for a logical operator. If the operator is predefined, and
130 -- the root type of the operands is Standard.Boolean, then a check is made
131 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
132 -- the style check for Style_Check_Boolean_And_Or.
134 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
135 -- N is either an indexed component or a selected component. This function
136 -- returns true if the prefix refers to an object that has an address
137 -- clause (the case in which we may want to issue a warning).
139 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
140 -- Determine whether E is an access type declared by an access declaration,
141 -- and not an (anonymous) allocator type.
143 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
144 -- Utility to check whether the entity for an operator is a predefined
145 -- operator, in which case the expression is left as an operator in the
146 -- tree (else it is rewritten into a call). An instance of an intrinsic
147 -- conversion operation may be given an operator name, but is not treated
148 -- like an operator. Note that an operator that is an imported back-end
149 -- builtin has convention Intrinsic, but is expected to be rewritten into
150 -- a call, so such an operator is not treated as predefined by this
151 -- predicate.
153 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
154 -- If a default expression in entry call N depends on the discriminants
155 -- of the task, it must be replaced with a reference to the discriminant
156 -- of the task being called.
158 procedure Resolve_Op_Concat_Arg
159 (N : Node_Id;
160 Arg : Node_Id;
161 Typ : Entity_Id;
162 Is_Comp : Boolean);
163 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
164 -- concatenation operator. The operand is either of the array type or of
165 -- the component type. If the operand is an aggregate, and the component
166 -- type is composite, this is ambiguous if component type has aggregates.
168 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
169 -- Does the first part of the work of Resolve_Op_Concat
171 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
172 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
173 -- has been resolved. See Resolve_Op_Concat for details.
175 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
211 function Operator_Kind
212 (Op_Name : Name_Id;
213 Is_Binary : Boolean) return Node_Kind;
214 -- Utility to map the name of an operator into the corresponding Node. Used
215 -- by other node rewriting procedures.
217 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
218 -- Resolve actuals of call, and add default expressions for missing ones.
219 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
220 -- called subprogram.
222 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
223 -- Called from Resolve_Call, when the prefix denotes an entry or element
224 -- of entry family. Actuals are resolved as for subprograms, and the node
225 -- is rebuilt as an entry call. Also called for protected operations. Typ
226 -- is the context type, which is used when the operation is a protected
227 -- function with no arguments, and the return value is indexed.
229 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
230 -- A call to a user-defined intrinsic operator is rewritten as a call to
231 -- the corresponding predefined operator, with suitable conversions. Note
232 -- that this applies only for intrinsic operators that denote predefined
233 -- operators, not ones that are intrinsic imports of back-end builtins.
235 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
236 -- Ditto, for arithmetic unary operators
238 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
239 -- If an operator node resolves to a call to a user-defined operator,
240 -- rewrite the node as a function call.
242 procedure Make_Call_Into_Operator
243 (N : Node_Id;
244 Typ : Entity_Id;
245 Op_Id : Entity_Id);
246 -- Inverse transformation: if an operator is given in functional notation,
247 -- then after resolving the node, transform into an operator node, so that
248 -- operands are resolved properly. Recall that predefined operators do not
249 -- have a full signature and special resolution rules apply.
251 procedure Rewrite_Renamed_Operator
252 (N : Node_Id;
253 Op : Entity_Id;
254 Typ : Entity_Id);
255 -- An operator can rename another, e.g. in an instantiation. In that
256 -- case, the proper operator node must be constructed and resolved.
258 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
259 -- The String_Literal_Subtype is built for all strings that are not
260 -- operands of a static concatenation operation. If the argument is not
261 -- a N_String_Literal node, then the call has no effect.
263 procedure Set_Slice_Subtype (N : Node_Id);
264 -- Build subtype of array type, with the range specified by the slice
266 procedure Simplify_Type_Conversion (N : Node_Id);
267 -- Called after N has been resolved and evaluated, but before range checks
268 -- have been applied. Currently simplifies a combination of floating-point
269 -- to integer conversion and Rounding or Truncation attribute.
271 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
272 -- A universal_fixed expression in an universal context is unambiguous if
273 -- there is only one applicable fixed point type. Determining whether there
274 -- is only one requires a search over all visible entities, and happens
275 -- only in very pathological cases (see 6115-006).
277 -------------------------
278 -- Ambiguous_Character --
279 -------------------------
281 procedure Ambiguous_Character (C : Node_Id) is
282 E : Entity_Id;
284 begin
285 if Nkind (C) = N_Character_Literal then
286 Error_Msg_N ("ambiguous character literal", C);
288 -- First the ones in Standard
290 Error_Msg_N ("\\possible interpretation: Character!", C);
291 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
293 -- Include Wide_Wide_Character in Ada 2005 mode
295 if Ada_Version >= Ada_2005 then
296 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
297 end if;
299 -- Now any other types that match
301 E := Current_Entity (C);
302 while Present (E) loop
303 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
304 E := Homonym (E);
305 end loop;
306 end if;
307 end Ambiguous_Character;
309 -------------------------
310 -- Analyze_And_Resolve --
311 -------------------------
313 procedure Analyze_And_Resolve (N : Node_Id) is
314 begin
315 Analyze (N);
316 Resolve (N);
317 end Analyze_And_Resolve;
319 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
320 begin
321 Analyze (N);
322 Resolve (N, Typ);
323 end Analyze_And_Resolve;
325 -- Versions with check(s) suppressed
327 procedure Analyze_And_Resolve
328 (N : Node_Id;
329 Typ : Entity_Id;
330 Suppress : Check_Id)
332 Scop : constant Entity_Id := Current_Scope;
334 begin
335 if Suppress = All_Checks then
336 declare
337 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
338 begin
339 Scope_Suppress.Suppress := (others => True);
340 Analyze_And_Resolve (N, Typ);
341 Scope_Suppress.Suppress := Sva;
342 end;
344 else
345 declare
346 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
347 begin
348 Scope_Suppress.Suppress (Suppress) := True;
349 Analyze_And_Resolve (N, Typ);
350 Scope_Suppress.Suppress (Suppress) := Svg;
351 end;
352 end if;
354 if Current_Scope /= Scop
355 and then Scope_Is_Transient
356 then
357 -- This can only happen if a transient scope was created for an inner
358 -- expression, which will be removed upon completion of the analysis
359 -- of an enclosing construct. The transient scope must have the
360 -- suppress status of the enclosing environment, not of this Analyze
361 -- call.
363 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
364 Scope_Suppress;
365 end if;
366 end Analyze_And_Resolve;
368 procedure Analyze_And_Resolve
369 (N : Node_Id;
370 Suppress : Check_Id)
372 Scop : constant Entity_Id := Current_Scope;
374 begin
375 if Suppress = All_Checks then
376 declare
377 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
378 begin
379 Scope_Suppress.Suppress := (others => True);
380 Analyze_And_Resolve (N);
381 Scope_Suppress.Suppress := Sva;
382 end;
384 else
385 declare
386 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
387 begin
388 Scope_Suppress.Suppress (Suppress) := True;
389 Analyze_And_Resolve (N);
390 Scope_Suppress.Suppress (Suppress) := Svg;
391 end;
392 end if;
394 if Current_Scope /= Scop and then Scope_Is_Transient then
395 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
396 Scope_Suppress;
397 end if;
398 end Analyze_And_Resolve;
400 ----------------------------
401 -- Check_Discriminant_Use --
402 ----------------------------
404 procedure Check_Discriminant_Use (N : Node_Id) is
405 PN : constant Node_Id := Parent (N);
406 Disc : constant Entity_Id := Entity (N);
407 P : Node_Id;
408 D : Node_Id;
410 begin
411 -- Any use in a spec-expression is legal
413 if In_Spec_Expression then
414 null;
416 elsif Nkind (PN) = N_Range then
418 -- Discriminant cannot be used to constrain a scalar type
420 P := Parent (PN);
422 if Nkind (P) = N_Range_Constraint
423 and then Nkind (Parent (P)) = N_Subtype_Indication
424 and then Nkind (Parent (Parent (P))) = N_Component_Definition
425 then
426 Error_Msg_N ("discriminant cannot constrain scalar type", N);
428 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
430 -- The following check catches the unusual case where a
431 -- discriminant appears within an index constraint that is part
432 -- of a larger expression within a constraint on a component,
433 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
434 -- check case of record components, and note that a similar check
435 -- should also apply in the case of discriminant constraints
436 -- below. ???
438 -- Note that the check for N_Subtype_Declaration below is to
439 -- detect the valid use of discriminants in the constraints of a
440 -- subtype declaration when this subtype declaration appears
441 -- inside the scope of a record type (which is syntactically
442 -- illegal, but which may be created as part of derived type
443 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
444 -- for more info.
446 if Ekind (Current_Scope) = E_Record_Type
447 and then Scope (Disc) = Current_Scope
448 and then not
449 (Nkind (Parent (P)) = N_Subtype_Indication
450 and then
451 Nkind_In (Parent (Parent (P)), N_Component_Definition,
452 N_Subtype_Declaration)
453 and then Paren_Count (N) = 0)
454 then
455 Error_Msg_N
456 ("discriminant must appear alone in component constraint", N);
457 return;
458 end if;
460 -- Detect a common error:
462 -- type R (D : Positive := 100) is record
463 -- Name : String (1 .. D);
464 -- end record;
466 -- The default value causes an object of type R to be allocated
467 -- with room for Positive'Last characters. The RM does not mandate
468 -- the allocation of the maximum size, but that is what GNAT does
469 -- so we should warn the programmer that there is a problem.
471 Check_Large : declare
472 SI : Node_Id;
473 T : Entity_Id;
474 TB : Node_Id;
475 CB : Entity_Id;
477 function Large_Storage_Type (T : Entity_Id) return Boolean;
478 -- Return True if type T has a large enough range that any
479 -- array whose index type covered the whole range of the type
480 -- would likely raise Storage_Error.
482 ------------------------
483 -- Large_Storage_Type --
484 ------------------------
486 function Large_Storage_Type (T : Entity_Id) return Boolean is
487 begin
488 -- The type is considered large if its bounds are known at
489 -- compile time and if it requires at least as many bits as
490 -- a Positive to store the possible values.
492 return Compile_Time_Known_Value (Type_Low_Bound (T))
493 and then Compile_Time_Known_Value (Type_High_Bound (T))
494 and then
495 Minimum_Size (T, Biased => True) >=
496 RM_Size (Standard_Positive);
497 end Large_Storage_Type;
499 -- Start of processing for Check_Large
501 begin
502 -- Check that the Disc has a large range
504 if not Large_Storage_Type (Etype (Disc)) then
505 goto No_Danger;
506 end if;
508 -- If the enclosing type is limited, we allocate only the
509 -- default value, not the maximum, and there is no need for
510 -- a warning.
512 if Is_Limited_Type (Scope (Disc)) then
513 goto No_Danger;
514 end if;
516 -- Check that it is the high bound
518 if N /= High_Bound (PN)
519 or else No (Discriminant_Default_Value (Disc))
520 then
521 goto No_Danger;
522 end if;
524 -- Check the array allows a large range at this bound. First
525 -- find the array
527 SI := Parent (P);
529 if Nkind (SI) /= N_Subtype_Indication then
530 goto No_Danger;
531 end if;
533 T := Entity (Subtype_Mark (SI));
535 if not Is_Array_Type (T) then
536 goto No_Danger;
537 end if;
539 -- Next, find the dimension
541 TB := First_Index (T);
542 CB := First (Constraints (P));
543 while True
544 and then Present (TB)
545 and then Present (CB)
546 and then CB /= PN
547 loop
548 Next_Index (TB);
549 Next (CB);
550 end loop;
552 if CB /= PN then
553 goto No_Danger;
554 end if;
556 -- Now, check the dimension has a large range
558 if not Large_Storage_Type (Etype (TB)) then
559 goto No_Danger;
560 end if;
562 -- Warn about the danger
564 Error_Msg_N
565 ("??creation of & object may raise Storage_Error!",
566 Scope (Disc));
568 <<No_Danger>>
569 null;
571 end Check_Large;
572 end if;
574 -- Legal case is in index or discriminant constraint
576 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
577 N_Discriminant_Association)
578 then
579 if Paren_Count (N) > 0 then
580 Error_Msg_N
581 ("discriminant in constraint must appear alone", N);
583 elsif Nkind (N) = N_Expanded_Name
584 and then Comes_From_Source (N)
585 then
586 Error_Msg_N
587 ("discriminant must appear alone as a direct name", N);
588 end if;
590 return;
592 -- Otherwise, context is an expression. It should not be within (i.e. a
593 -- subexpression of) a constraint for a component.
595 else
596 D := PN;
597 P := Parent (PN);
598 while not Nkind_In (P, N_Component_Declaration,
599 N_Subtype_Indication,
600 N_Entry_Declaration)
601 loop
602 D := P;
603 P := Parent (P);
604 exit when No (P);
605 end loop;
607 -- If the discriminant is used in an expression that is a bound of a
608 -- scalar type, an Itype is created and the bounds are attached to
609 -- its range, not to the original subtype indication. Such use is of
610 -- course a double fault.
612 if (Nkind (P) = N_Subtype_Indication
613 and then Nkind_In (Parent (P), N_Component_Definition,
614 N_Derived_Type_Definition)
615 and then D = Constraint (P))
617 -- The constraint itself may be given by a subtype indication,
618 -- rather than by a more common discrete range.
620 or else (Nkind (P) = N_Subtype_Indication
621 and then
622 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
623 or else Nkind (P) = N_Entry_Declaration
624 or else Nkind (D) = N_Defining_Identifier
625 then
626 Error_Msg_N
627 ("discriminant in constraint must appear alone", N);
628 end if;
629 end if;
630 end Check_Discriminant_Use;
632 --------------------------------
633 -- Check_For_Visible_Operator --
634 --------------------------------
636 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
637 begin
638 if Is_Invisible_Operator (N, T) then
639 Error_Msg_NE -- CODEFIX
640 ("operator for} is not directly visible!", N, First_Subtype (T));
641 Error_Msg_N -- CODEFIX
642 ("use clause would make operation legal!", N);
643 end if;
644 end Check_For_Visible_Operator;
646 ----------------------------------
647 -- Check_Fully_Declared_Prefix --
648 ----------------------------------
650 procedure Check_Fully_Declared_Prefix
651 (Typ : Entity_Id;
652 Pref : Node_Id)
654 begin
655 -- Check that the designated type of the prefix of a dereference is
656 -- not an incomplete type. This cannot be done unconditionally, because
657 -- dereferences of private types are legal in default expressions. This
658 -- case is taken care of in Check_Fully_Declared, called below. There
659 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
661 -- This consideration also applies to similar checks for allocators,
662 -- qualified expressions, and type conversions.
664 -- An additional exception concerns other per-object expressions that
665 -- are not directly related to component declarations, in particular
666 -- representation pragmas for tasks. These will be per-object
667 -- expressions if they depend on discriminants or some global entity.
668 -- If the task has access discriminants, the designated type may be
669 -- incomplete at the point the expression is resolved. This resolution
670 -- takes place within the body of the initialization procedure, where
671 -- the discriminant is replaced by its discriminal.
673 if Is_Entity_Name (Pref)
674 and then Ekind (Entity (Pref)) = E_In_Parameter
675 then
676 null;
678 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
679 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
680 -- Analyze_Object_Renaming, and Freeze_Entity.
682 elsif Ada_Version >= Ada_2005
683 and then Is_Entity_Name (Pref)
684 and then Is_Access_Type (Etype (Pref))
685 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
686 E_Incomplete_Type
687 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
688 then
689 null;
690 else
691 Check_Fully_Declared (Typ, Parent (Pref));
692 end if;
693 end Check_Fully_Declared_Prefix;
695 ------------------------------
696 -- Check_Infinite_Recursion --
697 ------------------------------
699 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
700 P : Node_Id;
701 C : Node_Id;
703 function Same_Argument_List return Boolean;
704 -- Check whether list of actuals is identical to list of formals of
705 -- called function (which is also the enclosing scope).
707 ------------------------
708 -- Same_Argument_List --
709 ------------------------
711 function Same_Argument_List return Boolean is
712 A : Node_Id;
713 F : Entity_Id;
714 Subp : Entity_Id;
716 begin
717 if not Is_Entity_Name (Name (N)) then
718 return False;
719 else
720 Subp := Entity (Name (N));
721 end if;
723 F := First_Formal (Subp);
724 A := First_Actual (N);
725 while Present (F) and then Present (A) loop
726 if not Is_Entity_Name (A) or else Entity (A) /= F then
727 return False;
728 end if;
730 Next_Actual (A);
731 Next_Formal (F);
732 end loop;
734 return True;
735 end Same_Argument_List;
737 -- Start of processing for Check_Infinite_Recursion
739 begin
740 -- Special case, if this is a procedure call and is a call to the
741 -- current procedure with the same argument list, then this is for
742 -- sure an infinite recursion and we insert a call to raise SE.
744 if Is_List_Member (N)
745 and then List_Length (List_Containing (N)) = 1
746 and then Same_Argument_List
747 then
748 declare
749 P : constant Node_Id := Parent (N);
750 begin
751 if Nkind (P) = N_Handled_Sequence_Of_Statements
752 and then Nkind (Parent (P)) = N_Subprogram_Body
753 and then Is_Empty_List (Declarations (Parent (P)))
754 then
755 Error_Msg_Warn := SPARK_Mode /= On;
756 Error_Msg_N ("!infinite recursion<<", N);
757 Error_Msg_N ("\!Storage_Error [<<", N);
758 Insert_Action (N,
759 Make_Raise_Storage_Error (Sloc (N),
760 Reason => SE_Infinite_Recursion));
761 return True;
762 end if;
763 end;
764 end if;
766 -- If not that special case, search up tree, quitting if we reach a
767 -- construct (e.g. a conditional) that tells us that this is not a
768 -- case for an infinite recursion warning.
770 C := N;
771 loop
772 P := Parent (C);
774 -- If no parent, then we were not inside a subprogram, this can for
775 -- example happen when processing certain pragmas in a spec. Just
776 -- return False in this case.
778 if No (P) then
779 return False;
780 end if;
782 -- Done if we get to subprogram body, this is definitely an infinite
783 -- recursion case if we did not find anything to stop us.
785 exit when Nkind (P) = N_Subprogram_Body;
787 -- If appearing in conditional, result is false
789 if Nkind_In (P, N_Or_Else,
790 N_And_Then,
791 N_Case_Expression,
792 N_Case_Statement,
793 N_If_Expression,
794 N_If_Statement)
795 then
796 return False;
798 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
799 and then C /= First (Statements (P))
800 then
801 -- If the call is the expression of a return statement and the
802 -- actuals are identical to the formals, it's worth a warning.
803 -- However, we skip this if there is an immediately preceding
804 -- raise statement, since the call is never executed.
806 -- Furthermore, this corresponds to a common idiom:
808 -- function F (L : Thing) return Boolean is
809 -- begin
810 -- raise Program_Error;
811 -- return F (L);
812 -- end F;
814 -- for generating a stub function
816 if Nkind (Parent (N)) = N_Simple_Return_Statement
817 and then Same_Argument_List
818 then
819 exit when not Is_List_Member (Parent (N));
821 -- OK, return statement is in a statement list, look for raise
823 declare
824 Nod : Node_Id;
826 begin
827 -- Skip past N_Freeze_Entity nodes generated by expansion
829 Nod := Prev (Parent (N));
830 while Present (Nod)
831 and then Nkind (Nod) = N_Freeze_Entity
832 loop
833 Prev (Nod);
834 end loop;
836 -- If no raise statement, give warning. We look at the
837 -- original node, because in the case of "raise ... with
838 -- ...", the node has been transformed into a call.
840 exit when Nkind (Original_Node (Nod)) /= N_Raise_Statement
841 and then
842 (Nkind (Nod) not in N_Raise_xxx_Error
843 or else Present (Condition (Nod)));
844 end;
845 end if;
847 return False;
849 else
850 C := P;
851 end if;
852 end loop;
854 Error_Msg_Warn := SPARK_Mode /= On;
855 Error_Msg_N ("!possible infinite recursion<<", N);
856 Error_Msg_N ("\!??Storage_Error ]<<", N);
858 return True;
859 end Check_Infinite_Recursion;
861 -------------------------------
862 -- Check_Initialization_Call --
863 -------------------------------
865 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
866 Typ : constant Entity_Id := Etype (First_Formal (Nam));
868 function Uses_SS (T : Entity_Id) return Boolean;
869 -- Check whether the creation of an object of the type will involve
870 -- use of the secondary stack. If T is a record type, this is true
871 -- if the expression for some component uses the secondary stack, e.g.
872 -- through a call to a function that returns an unconstrained value.
873 -- False if T is controlled, because cleanups occur elsewhere.
875 -------------
876 -- Uses_SS --
877 -------------
879 function Uses_SS (T : Entity_Id) return Boolean is
880 Comp : Entity_Id;
881 Expr : Node_Id;
882 Full_Type : Entity_Id := Underlying_Type (T);
884 begin
885 -- Normally we want to use the underlying type, but if it's not set
886 -- then continue with T.
888 if not Present (Full_Type) then
889 Full_Type := T;
890 end if;
892 if Is_Controlled (Full_Type) then
893 return False;
895 elsif Is_Array_Type (Full_Type) then
896 return Uses_SS (Component_Type (Full_Type));
898 elsif Is_Record_Type (Full_Type) then
899 Comp := First_Component (Full_Type);
900 while Present (Comp) loop
901 if Ekind (Comp) = E_Component
902 and then Nkind (Parent (Comp)) = N_Component_Declaration
903 then
904 -- The expression for a dynamic component may be rewritten
905 -- as a dereference, so retrieve original node.
907 Expr := Original_Node (Expression (Parent (Comp)));
909 -- Return True if the expression is a call to a function
910 -- (including an attribute function such as Image, or a
911 -- user-defined operator) with a result that requires a
912 -- transient scope.
914 if (Nkind (Expr) = N_Function_Call
915 or else Nkind (Expr) in N_Op
916 or else (Nkind (Expr) = N_Attribute_Reference
917 and then Present (Expressions (Expr))))
918 and then Requires_Transient_Scope (Etype (Expr))
919 then
920 return True;
922 elsif Uses_SS (Etype (Comp)) then
923 return True;
924 end if;
925 end if;
927 Next_Component (Comp);
928 end loop;
930 return False;
932 else
933 return False;
934 end if;
935 end Uses_SS;
937 -- Start of processing for Check_Initialization_Call
939 begin
940 -- Establish a transient scope if the type needs it
942 if Uses_SS (Typ) then
943 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
944 end if;
945 end Check_Initialization_Call;
947 ---------------------------------------
948 -- Check_No_Direct_Boolean_Operators --
949 ---------------------------------------
951 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
952 begin
953 if Scope (Entity (N)) = Standard_Standard
954 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
955 then
956 -- Restriction only applies to original source code
958 if Comes_From_Source (N) then
959 Check_Restriction (No_Direct_Boolean_Operators, N);
960 end if;
961 end if;
963 -- Do style check (but skip if in instance, error is on template)
965 if Style_Check then
966 if not In_Instance then
967 Check_Boolean_Operator (N);
968 end if;
969 end if;
970 end Check_No_Direct_Boolean_Operators;
972 ------------------------------
973 -- Check_Parameterless_Call --
974 ------------------------------
976 procedure Check_Parameterless_Call (N : Node_Id) is
977 Nam : Node_Id;
979 function Prefix_Is_Access_Subp return Boolean;
980 -- If the prefix is of an access_to_subprogram type, the node must be
981 -- rewritten as a call. Ditto if the prefix is overloaded and all its
982 -- interpretations are access to subprograms.
984 ---------------------------
985 -- Prefix_Is_Access_Subp --
986 ---------------------------
988 function Prefix_Is_Access_Subp return Boolean is
989 I : Interp_Index;
990 It : Interp;
992 begin
993 -- If the context is an attribute reference that can apply to
994 -- functions, this is never a parameterless call (RM 4.1.4(6)).
996 if Nkind (Parent (N)) = N_Attribute_Reference
997 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
998 Name_Code_Address,
999 Name_Access)
1000 then
1001 return False;
1002 end if;
1004 if not Is_Overloaded (N) then
1005 return
1006 Ekind (Etype (N)) = E_Subprogram_Type
1007 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1008 else
1009 Get_First_Interp (N, I, It);
1010 while Present (It.Typ) loop
1011 if Ekind (It.Typ) /= E_Subprogram_Type
1012 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1013 then
1014 return False;
1015 end if;
1017 Get_Next_Interp (I, It);
1018 end loop;
1020 return True;
1021 end if;
1022 end Prefix_Is_Access_Subp;
1024 -- Start of processing for Check_Parameterless_Call
1026 begin
1027 -- Defend against junk stuff if errors already detected
1029 if Total_Errors_Detected /= 0 then
1030 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1031 return;
1032 elsif Nkind (N) in N_Has_Chars
1033 and then Chars (N) in Error_Name_Or_No_Name
1034 then
1035 return;
1036 end if;
1038 Require_Entity (N);
1039 end if;
1041 -- If the context expects a value, and the name is a procedure, this is
1042 -- most likely a missing 'Access. Don't try to resolve the parameterless
1043 -- call, error will be caught when the outer call is analyzed.
1045 if Is_Entity_Name (N)
1046 and then Ekind (Entity (N)) = E_Procedure
1047 and then not Is_Overloaded (N)
1048 and then
1049 Nkind_In (Parent (N), N_Parameter_Association,
1050 N_Function_Call,
1051 N_Procedure_Call_Statement)
1052 then
1053 return;
1054 end if;
1056 -- Rewrite as call if overloadable entity that is (or could be, in the
1057 -- overloaded case) a function call. If we know for sure that the entity
1058 -- is an enumeration literal, we do not rewrite it.
1060 -- If the entity is the name of an operator, it cannot be a call because
1061 -- operators cannot have default parameters. In this case, this must be
1062 -- a string whose contents coincide with an operator name. Set the kind
1063 -- of the node appropriately.
1065 if (Is_Entity_Name (N)
1066 and then Nkind (N) /= N_Operator_Symbol
1067 and then Is_Overloadable (Entity (N))
1068 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1069 or else Is_Overloaded (N)))
1071 -- Rewrite as call if it is an explicit dereference of an expression of
1072 -- a subprogram access type, and the subprogram type is not that of a
1073 -- procedure or entry.
1075 or else
1076 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1078 -- Rewrite as call if it is a selected component which is a function,
1079 -- this is the case of a call to a protected function (which may be
1080 -- overloaded with other protected operations).
1082 or else
1083 (Nkind (N) = N_Selected_Component
1084 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1085 or else
1086 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1087 E_Procedure)
1088 and then Is_Overloaded (Selector_Name (N)))))
1090 -- If one of the above three conditions is met, rewrite as call. Apply
1091 -- the rewriting only once.
1093 then
1094 if Nkind (Parent (N)) /= N_Function_Call
1095 or else N /= Name (Parent (N))
1096 then
1098 -- This may be a prefixed call that was not fully analyzed, e.g.
1099 -- an actual in an instance.
1101 if Ada_Version >= Ada_2005
1102 and then Nkind (N) = N_Selected_Component
1103 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1104 then
1105 Analyze_Selected_Component (N);
1107 if Nkind (N) /= N_Selected_Component then
1108 return;
1109 end if;
1110 end if;
1112 -- The node is the name of the parameterless call. Preserve its
1113 -- descendants, which may be complex expressions.
1115 Nam := Relocate_Node (N);
1117 -- If overloaded, overload set belongs to new copy
1119 Save_Interps (N, Nam);
1121 -- Change node to parameterless function call (note that the
1122 -- Parameter_Associations associations field is left set to Empty,
1123 -- its normal default value since there are no parameters)
1125 Change_Node (N, N_Function_Call);
1126 Set_Name (N, Nam);
1127 Set_Sloc (N, Sloc (Nam));
1128 Analyze_Call (N);
1129 end if;
1131 elsif Nkind (N) = N_Parameter_Association then
1132 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1134 elsif Nkind (N) = N_Operator_Symbol then
1135 Change_Operator_Symbol_To_String_Literal (N);
1136 Set_Is_Overloaded (N, False);
1137 Set_Etype (N, Any_String);
1138 end if;
1139 end Check_Parameterless_Call;
1141 --------------------------------
1142 -- Is_Atomic_Ref_With_Address --
1143 --------------------------------
1145 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1146 Pref : constant Node_Id := Prefix (N);
1148 begin
1149 if not Is_Entity_Name (Pref) then
1150 return False;
1152 else
1153 declare
1154 Pent : constant Entity_Id := Entity (Pref);
1155 Ptyp : constant Entity_Id := Etype (Pent);
1156 begin
1157 return not Is_Access_Type (Ptyp)
1158 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1159 and then Present (Address_Clause (Pent));
1160 end;
1161 end if;
1162 end Is_Atomic_Ref_With_Address;
1164 -----------------------------
1165 -- Is_Definite_Access_Type --
1166 -----------------------------
1168 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1169 Btyp : constant Entity_Id := Base_Type (E);
1170 begin
1171 return Ekind (Btyp) = E_Access_Type
1172 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1173 and then Comes_From_Source (Btyp));
1174 end Is_Definite_Access_Type;
1176 ----------------------
1177 -- Is_Predefined_Op --
1178 ----------------------
1180 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1181 begin
1182 -- Predefined operators are intrinsic subprograms
1184 if not Is_Intrinsic_Subprogram (Nam) then
1185 return False;
1186 end if;
1188 -- A call to a back-end builtin is never a predefined operator
1190 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1191 return False;
1192 end if;
1194 return not Is_Generic_Instance (Nam)
1195 and then Chars (Nam) in Any_Operator_Name
1196 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1197 end Is_Predefined_Op;
1199 -----------------------------
1200 -- Make_Call_Into_Operator --
1201 -----------------------------
1203 procedure Make_Call_Into_Operator
1204 (N : Node_Id;
1205 Typ : Entity_Id;
1206 Op_Id : Entity_Id)
1208 Op_Name : constant Name_Id := Chars (Op_Id);
1209 Act1 : Node_Id := First_Actual (N);
1210 Act2 : Node_Id := Next_Actual (Act1);
1211 Error : Boolean := False;
1212 Func : constant Entity_Id := Entity (Name (N));
1213 Is_Binary : constant Boolean := Present (Act2);
1214 Op_Node : Node_Id;
1215 Opnd_Type : Entity_Id;
1216 Orig_Type : Entity_Id := Empty;
1217 Pack : Entity_Id;
1219 type Kind_Test is access function (E : Entity_Id) return Boolean;
1221 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1222 -- If the operand is not universal, and the operator is given by an
1223 -- expanded name, verify that the operand has an interpretation with a
1224 -- type defined in the given scope of the operator.
1226 function Type_In_P (Test : Kind_Test) return Entity_Id;
1227 -- Find a type of the given class in package Pack that contains the
1228 -- operator.
1230 ---------------------------
1231 -- Operand_Type_In_Scope --
1232 ---------------------------
1234 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1235 Nod : constant Node_Id := Right_Opnd (Op_Node);
1236 I : Interp_Index;
1237 It : Interp;
1239 begin
1240 if not Is_Overloaded (Nod) then
1241 return Scope (Base_Type (Etype (Nod))) = S;
1243 else
1244 Get_First_Interp (Nod, I, It);
1245 while Present (It.Typ) loop
1246 if Scope (Base_Type (It.Typ)) = S then
1247 return True;
1248 end if;
1250 Get_Next_Interp (I, It);
1251 end loop;
1253 return False;
1254 end if;
1255 end Operand_Type_In_Scope;
1257 ---------------
1258 -- Type_In_P --
1259 ---------------
1261 function Type_In_P (Test : Kind_Test) return Entity_Id is
1262 E : Entity_Id;
1264 function In_Decl return Boolean;
1265 -- Verify that node is not part of the type declaration for the
1266 -- candidate type, which would otherwise be invisible.
1268 -------------
1269 -- In_Decl --
1270 -------------
1272 function In_Decl return Boolean is
1273 Decl_Node : constant Node_Id := Parent (E);
1274 N2 : Node_Id;
1276 begin
1277 N2 := N;
1279 if Etype (E) = Any_Type then
1280 return True;
1282 elsif No (Decl_Node) then
1283 return False;
1285 else
1286 while Present (N2)
1287 and then Nkind (N2) /= N_Compilation_Unit
1288 loop
1289 if N2 = Decl_Node then
1290 return True;
1291 else
1292 N2 := Parent (N2);
1293 end if;
1294 end loop;
1296 return False;
1297 end if;
1298 end In_Decl;
1300 -- Start of processing for Type_In_P
1302 begin
1303 -- If the context type is declared in the prefix package, this is the
1304 -- desired base type.
1306 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1307 return Base_Type (Typ);
1309 else
1310 E := First_Entity (Pack);
1311 while Present (E) loop
1312 if Test (E) and then not In_Decl 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 declare
1606 L : Node_Id;
1607 R : constant Node_Id := Right_Opnd (N);
1609 Old_First : constant Node_Id :=
1610 First (Parameter_Associations (Original_Node (N)));
1611 Old_Sec : Node_Id;
1613 begin
1614 if Is_Binary then
1615 L := Left_Opnd (N);
1616 Old_Sec := Next (Old_First);
1618 -- If the original call has named associations, replace the
1619 -- explicit actual parameter in the association with the proper
1620 -- resolved operand.
1622 if Nkind (Old_First) = N_Parameter_Association then
1623 if Chars (Selector_Name (Old_First)) =
1624 Chars (First_Entity (Op_Id))
1625 then
1626 Rewrite (Explicit_Actual_Parameter (Old_First),
1627 Relocate_Node (L));
1628 else
1629 Rewrite (Explicit_Actual_Parameter (Old_First),
1630 Relocate_Node (R));
1631 end if;
1633 else
1634 Rewrite (Old_First, Relocate_Node (L));
1635 end if;
1637 if Nkind (Old_Sec) = N_Parameter_Association then
1638 if Chars (Selector_Name (Old_Sec)) =
1639 Chars (First_Entity (Op_Id))
1640 then
1641 Rewrite (Explicit_Actual_Parameter (Old_Sec),
1642 Relocate_Node (L));
1643 else
1644 Rewrite (Explicit_Actual_Parameter (Old_Sec),
1645 Relocate_Node (R));
1646 end if;
1648 else
1649 Rewrite (Old_Sec, Relocate_Node (R));
1650 end if;
1652 else
1653 if Nkind (Old_First) = N_Parameter_Association then
1654 Rewrite (Explicit_Actual_Parameter (Old_First),
1655 Relocate_Node (R));
1656 else
1657 Rewrite (Old_First, Relocate_Node (R));
1658 end if;
1659 end if;
1660 end;
1662 Set_Parent (Original_Node (N), Parent (N));
1663 end if;
1664 end Make_Call_Into_Operator;
1666 -------------------
1667 -- Operator_Kind --
1668 -------------------
1670 function Operator_Kind
1671 (Op_Name : Name_Id;
1672 Is_Binary : Boolean) return Node_Kind
1674 Kind : Node_Kind;
1676 begin
1677 -- Use CASE statement or array???
1679 if Is_Binary then
1680 if Op_Name = Name_Op_And then
1681 Kind := N_Op_And;
1682 elsif Op_Name = Name_Op_Or then
1683 Kind := N_Op_Or;
1684 elsif Op_Name = Name_Op_Xor then
1685 Kind := N_Op_Xor;
1686 elsif Op_Name = Name_Op_Eq then
1687 Kind := N_Op_Eq;
1688 elsif Op_Name = Name_Op_Ne then
1689 Kind := N_Op_Ne;
1690 elsif Op_Name = Name_Op_Lt then
1691 Kind := N_Op_Lt;
1692 elsif Op_Name = Name_Op_Le then
1693 Kind := N_Op_Le;
1694 elsif Op_Name = Name_Op_Gt then
1695 Kind := N_Op_Gt;
1696 elsif Op_Name = Name_Op_Ge then
1697 Kind := N_Op_Ge;
1698 elsif Op_Name = Name_Op_Add then
1699 Kind := N_Op_Add;
1700 elsif Op_Name = Name_Op_Subtract then
1701 Kind := N_Op_Subtract;
1702 elsif Op_Name = Name_Op_Concat then
1703 Kind := N_Op_Concat;
1704 elsif Op_Name = Name_Op_Multiply then
1705 Kind := N_Op_Multiply;
1706 elsif Op_Name = Name_Op_Divide then
1707 Kind := N_Op_Divide;
1708 elsif Op_Name = Name_Op_Mod then
1709 Kind := N_Op_Mod;
1710 elsif Op_Name = Name_Op_Rem then
1711 Kind := N_Op_Rem;
1712 elsif Op_Name = Name_Op_Expon then
1713 Kind := N_Op_Expon;
1714 else
1715 raise Program_Error;
1716 end if;
1718 -- Unary operators
1720 else
1721 if Op_Name = Name_Op_Add then
1722 Kind := N_Op_Plus;
1723 elsif Op_Name = Name_Op_Subtract then
1724 Kind := N_Op_Minus;
1725 elsif Op_Name = Name_Op_Abs then
1726 Kind := N_Op_Abs;
1727 elsif Op_Name = Name_Op_Not then
1728 Kind := N_Op_Not;
1729 else
1730 raise Program_Error;
1731 end if;
1732 end if;
1734 return Kind;
1735 end Operator_Kind;
1737 ----------------------------
1738 -- Preanalyze_And_Resolve --
1739 ----------------------------
1741 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1742 Save_Full_Analysis : constant Boolean := Full_Analysis;
1744 begin
1745 Full_Analysis := False;
1746 Expander_Mode_Save_And_Set (False);
1748 -- Normally, we suppress all checks for this preanalysis. There is no
1749 -- point in processing them now, since they will be applied properly
1750 -- and in the proper location when the default expressions reanalyzed
1751 -- and reexpanded later on. We will also have more information at that
1752 -- point for possible suppression of individual checks.
1754 -- However, in SPARK mode, most expansion is suppressed, and this
1755 -- later reanalysis and reexpansion may not occur. SPARK mode does
1756 -- require the setting of checking flags for proof purposes, so we
1757 -- do the SPARK preanalysis without suppressing checks.
1759 -- This special handling for SPARK mode is required for example in the
1760 -- case of Ada 2012 constructs such as quantified expressions, which are
1761 -- expanded in two separate steps.
1763 if GNATprove_Mode then
1764 Analyze_And_Resolve (N, T);
1765 else
1766 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1767 end if;
1769 Expander_Mode_Restore;
1770 Full_Analysis := Save_Full_Analysis;
1771 end Preanalyze_And_Resolve;
1773 -- Version without context type
1775 procedure Preanalyze_And_Resolve (N : Node_Id) is
1776 Save_Full_Analysis : constant Boolean := Full_Analysis;
1778 begin
1779 Full_Analysis := False;
1780 Expander_Mode_Save_And_Set (False);
1782 Analyze (N);
1783 Resolve (N, Etype (N), Suppress => All_Checks);
1785 Expander_Mode_Restore;
1786 Full_Analysis := Save_Full_Analysis;
1787 end Preanalyze_And_Resolve;
1789 ----------------------------------
1790 -- Replace_Actual_Discriminants --
1791 ----------------------------------
1793 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1794 Loc : constant Source_Ptr := Sloc (N);
1795 Tsk : Node_Id := Empty;
1797 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1798 -- Comment needed???
1800 -------------------
1801 -- Process_Discr --
1802 -------------------
1804 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1805 Ent : Entity_Id;
1807 begin
1808 if Nkind (Nod) = N_Identifier then
1809 Ent := Entity (Nod);
1811 if Present (Ent)
1812 and then Ekind (Ent) = E_Discriminant
1813 then
1814 Rewrite (Nod,
1815 Make_Selected_Component (Loc,
1816 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1817 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1819 Set_Etype (Nod, Etype (Ent));
1820 end if;
1822 end if;
1824 return OK;
1825 end Process_Discr;
1827 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1829 -- Start of processing for Replace_Actual_Discriminants
1831 begin
1832 if not Expander_Active then
1833 return;
1834 end if;
1836 if Nkind (Name (N)) = N_Selected_Component then
1837 Tsk := Prefix (Name (N));
1839 elsif Nkind (Name (N)) = N_Indexed_Component then
1840 Tsk := Prefix (Prefix (Name (N)));
1841 end if;
1843 if No (Tsk) then
1844 return;
1845 else
1846 Replace_Discrs (Default);
1847 end if;
1848 end Replace_Actual_Discriminants;
1850 -------------
1851 -- Resolve --
1852 -------------
1854 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1855 Ambiguous : Boolean := False;
1856 Ctx_Type : Entity_Id := Typ;
1857 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1858 Err_Type : Entity_Id := Empty;
1859 Found : Boolean := False;
1860 From_Lib : Boolean;
1861 I : Interp_Index;
1862 I1 : Interp_Index := 0; -- prevent junk warning
1863 It : Interp;
1864 It1 : Interp;
1865 Seen : Entity_Id := Empty; -- prevent junk warning
1867 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1868 -- Determine whether a node comes from a predefined library unit or
1869 -- Standard.
1871 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1872 -- Try and fix up a literal so that it matches its expected type. New
1873 -- literals are manufactured if necessary to avoid cascaded errors.
1875 procedure Report_Ambiguous_Argument;
1876 -- Additional diagnostics when an ambiguous call has an ambiguous
1877 -- argument (typically a controlling actual).
1879 procedure Resolution_Failed;
1880 -- Called when attempt at resolving current expression fails
1882 ------------------------------------
1883 -- Comes_From_Predefined_Lib_Unit --
1884 -------------------------------------
1886 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1887 begin
1888 return
1889 Sloc (Nod) = Standard_Location
1890 or else Is_Predefined_File_Name
1891 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1892 end Comes_From_Predefined_Lib_Unit;
1894 --------------------
1895 -- Patch_Up_Value --
1896 --------------------
1898 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1899 begin
1900 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1901 Rewrite (N,
1902 Make_Real_Literal (Sloc (N),
1903 Realval => UR_From_Uint (Intval (N))));
1904 Set_Etype (N, Universal_Real);
1905 Set_Is_Static_Expression (N);
1907 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1908 Rewrite (N,
1909 Make_Integer_Literal (Sloc (N),
1910 Intval => UR_To_Uint (Realval (N))));
1911 Set_Etype (N, Universal_Integer);
1912 Set_Is_Static_Expression (N);
1914 elsif Nkind (N) = N_String_Literal
1915 and then Is_Character_Type (Typ)
1916 then
1917 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1918 Rewrite (N,
1919 Make_Character_Literal (Sloc (N),
1920 Chars => Name_Find,
1921 Char_Literal_Value =>
1922 UI_From_Int (Character'Pos ('A'))));
1923 Set_Etype (N, Any_Character);
1924 Set_Is_Static_Expression (N);
1926 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1927 Rewrite (N,
1928 Make_String_Literal (Sloc (N),
1929 Strval => End_String));
1931 elsif Nkind (N) = N_Range then
1932 Patch_Up_Value (Low_Bound (N), Typ);
1933 Patch_Up_Value (High_Bound (N), Typ);
1934 end if;
1935 end Patch_Up_Value;
1937 -------------------------------
1938 -- Report_Ambiguous_Argument --
1939 -------------------------------
1941 procedure Report_Ambiguous_Argument is
1942 Arg : constant Node_Id := First (Parameter_Associations (N));
1943 I : Interp_Index;
1944 It : Interp;
1946 begin
1947 if Nkind (Arg) = N_Function_Call
1948 and then Is_Entity_Name (Name (Arg))
1949 and then Is_Overloaded (Name (Arg))
1950 then
1951 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1953 -- Could use comments on what is going on here???
1955 Get_First_Interp (Name (Arg), I, It);
1956 while Present (It.Nam) loop
1957 Error_Msg_Sloc := Sloc (It.Nam);
1959 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1960 Error_Msg_N ("interpretation (inherited) #!", Arg);
1961 else
1962 Error_Msg_N ("interpretation #!", Arg);
1963 end if;
1965 Get_Next_Interp (I, It);
1966 end loop;
1967 end if;
1968 end Report_Ambiguous_Argument;
1970 -----------------------
1971 -- Resolution_Failed --
1972 -----------------------
1974 procedure Resolution_Failed is
1975 begin
1976 Patch_Up_Value (N, Typ);
1977 Set_Etype (N, Typ);
1978 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1979 Set_Is_Overloaded (N, False);
1981 -- The caller will return without calling the expander, so we need
1982 -- to set the analyzed flag. Note that it is fine to set Analyzed
1983 -- to True even if we are in the middle of a shallow analysis,
1984 -- (see the spec of sem for more details) since this is an error
1985 -- situation anyway, and there is no point in repeating the
1986 -- analysis later (indeed it won't work to repeat it later, since
1987 -- we haven't got a clear resolution of which entity is being
1988 -- referenced.)
1990 Set_Analyzed (N, True);
1991 return;
1992 end Resolution_Failed;
1994 -- Local variables
1996 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
1998 -- Start of processing for Resolve
2000 begin
2001 if N = Error then
2002 return;
2003 end if;
2005 -- A declaration may be subject to pragma Ghost. Set the mode now to
2006 -- ensure that any nodes generated during analysis and expansion are
2007 -- marked as Ghost.
2009 if Is_Declaration (N) then
2010 Set_Ghost_Mode (N);
2011 end if;
2013 -- Access attribute on remote subprogram cannot be used for a non-remote
2014 -- access-to-subprogram type.
2016 if Nkind (N) = N_Attribute_Reference
2017 and then Nam_In (Attribute_Name (N), Name_Access,
2018 Name_Unrestricted_Access,
2019 Name_Unchecked_Access)
2020 and then Comes_From_Source (N)
2021 and then Is_Entity_Name (Prefix (N))
2022 and then Is_Subprogram (Entity (Prefix (N)))
2023 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2024 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2025 then
2026 Error_Msg_N
2027 ("prefix must statically denote a non-remote subprogram", N);
2028 end if;
2030 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2032 -- If the context is a Remote_Access_To_Subprogram, access attributes
2033 -- must be resolved with the corresponding fat pointer. There is no need
2034 -- to check for the attribute name since the return type of an
2035 -- attribute is never a remote type.
2037 if Nkind (N) = N_Attribute_Reference
2038 and then Comes_From_Source (N)
2039 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2040 then
2041 declare
2042 Attr : constant Attribute_Id :=
2043 Get_Attribute_Id (Attribute_Name (N));
2044 Pref : constant Node_Id := Prefix (N);
2045 Decl : Node_Id;
2046 Spec : Node_Id;
2047 Is_Remote : Boolean := True;
2049 begin
2050 -- Check that Typ is a remote access-to-subprogram type
2052 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2054 -- Prefix (N) must statically denote a remote subprogram
2055 -- declared in a package specification.
2057 if Attr = Attribute_Access or else
2058 Attr = Attribute_Unchecked_Access or else
2059 Attr = Attribute_Unrestricted_Access
2060 then
2061 Decl := Unit_Declaration_Node (Entity (Pref));
2063 if Nkind (Decl) = N_Subprogram_Body then
2064 Spec := Corresponding_Spec (Decl);
2066 if Present (Spec) then
2067 Decl := Unit_Declaration_Node (Spec);
2068 end if;
2069 end if;
2071 Spec := Parent (Decl);
2073 if not Is_Entity_Name (Prefix (N))
2074 or else Nkind (Spec) /= N_Package_Specification
2075 or else
2076 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2077 then
2078 Is_Remote := False;
2079 Error_Msg_N
2080 ("prefix must statically denote a remote subprogram ",
2082 end if;
2084 -- If we are generating code in distributed mode, perform
2085 -- semantic checks against corresponding remote entities.
2087 if Expander_Active
2088 and then Get_PCS_Name /= Name_No_DSA
2089 then
2090 Check_Subtype_Conformant
2091 (New_Id => Entity (Prefix (N)),
2092 Old_Id => Designated_Type
2093 (Corresponding_Remote_Type (Typ)),
2094 Err_Loc => N);
2096 if Is_Remote then
2097 Process_Remote_AST_Attribute (N, Typ);
2098 end if;
2099 end if;
2100 end if;
2101 end if;
2102 end;
2103 end if;
2105 Debug_A_Entry ("resolving ", N);
2107 if Debug_Flag_V then
2108 Write_Overloads (N);
2109 end if;
2111 if Comes_From_Source (N) then
2112 if Is_Fixed_Point_Type (Typ) then
2113 Check_Restriction (No_Fixed_Point, N);
2115 elsif Is_Floating_Point_Type (Typ)
2116 and then Typ /= Universal_Real
2117 and then Typ /= Any_Real
2118 then
2119 Check_Restriction (No_Floating_Point, N);
2120 end if;
2121 end if;
2123 -- Return if already analyzed
2125 if Analyzed (N) then
2126 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2127 Analyze_Dimension (N);
2128 Ghost_Mode := Save_Ghost_Mode;
2129 return;
2131 -- Any case of Any_Type as the Etype value means that we had a
2132 -- previous error.
2134 elsif Etype (N) = Any_Type then
2135 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2136 Ghost_Mode := Save_Ghost_Mode;
2137 return;
2138 end if;
2140 Check_Parameterless_Call (N);
2142 -- The resolution of an Expression_With_Actions is determined by
2143 -- its Expression.
2145 if Nkind (N) = N_Expression_With_Actions then
2146 Resolve (Expression (N), Typ);
2148 Found := True;
2149 Expr_Type := Etype (Expression (N));
2151 -- If not overloaded, then we know the type, and all that needs doing
2152 -- is to check that this type is compatible with the context.
2154 elsif not Is_Overloaded (N) then
2155 Found := Covers (Typ, Etype (N));
2156 Expr_Type := Etype (N);
2158 -- In the overloaded case, we must select the interpretation that
2159 -- is compatible with the context (i.e. the type passed to Resolve)
2161 else
2162 -- Loop through possible interpretations
2164 Get_First_Interp (N, I, It);
2165 Interp_Loop : while Present (It.Typ) loop
2166 if Debug_Flag_V then
2167 Write_Str ("Interp: ");
2168 Write_Interp (It);
2169 end if;
2171 -- We are only interested in interpretations that are compatible
2172 -- with the expected type, any other interpretations are ignored.
2174 if not Covers (Typ, It.Typ) then
2175 if Debug_Flag_V then
2176 Write_Str (" interpretation incompatible with context");
2177 Write_Eol;
2178 end if;
2180 else
2181 -- Skip the current interpretation if it is disabled by an
2182 -- abstract operator. This action is performed only when the
2183 -- type against which we are resolving is the same as the
2184 -- type of the interpretation.
2186 if Ada_Version >= Ada_2005
2187 and then It.Typ = Typ
2188 and then Typ /= Universal_Integer
2189 and then Typ /= Universal_Real
2190 and then Present (It.Abstract_Op)
2191 then
2192 if Debug_Flag_V then
2193 Write_Line ("Skip.");
2194 end if;
2196 goto Continue;
2197 end if;
2199 -- First matching interpretation
2201 if not Found then
2202 Found := True;
2203 I1 := I;
2204 Seen := It.Nam;
2205 Expr_Type := It.Typ;
2207 -- Matching interpretation that is not the first, maybe an
2208 -- error, but there are some cases where preference rules are
2209 -- used to choose between the two possibilities. These and
2210 -- some more obscure cases are handled in Disambiguate.
2212 else
2213 -- If the current statement is part of a predefined library
2214 -- unit, then all interpretations which come from user level
2215 -- packages should not be considered. Check previous and
2216 -- current one.
2218 if From_Lib then
2219 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2220 goto Continue;
2222 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2224 -- Previous interpretation must be discarded
2226 I1 := I;
2227 Seen := It.Nam;
2228 Expr_Type := It.Typ;
2229 Set_Entity (N, Seen);
2230 goto Continue;
2231 end if;
2232 end if;
2234 -- Otherwise apply further disambiguation steps
2236 Error_Msg_Sloc := Sloc (Seen);
2237 It1 := Disambiguate (N, I1, I, Typ);
2239 -- Disambiguation has succeeded. Skip the remaining
2240 -- interpretations.
2242 if It1 /= No_Interp then
2243 Seen := It1.Nam;
2244 Expr_Type := It1.Typ;
2246 while Present (It.Typ) loop
2247 Get_Next_Interp (I, It);
2248 end loop;
2250 else
2251 -- Before we issue an ambiguity complaint, check for the
2252 -- case of a subprogram call where at least one of the
2253 -- arguments is Any_Type, and if so suppress the message,
2254 -- since it is a cascaded error. This can also happen for
2255 -- a generalized indexing operation.
2257 if Nkind (N) in N_Subprogram_Call
2258 or else (Nkind (N) = N_Indexed_Component
2259 and then Present (Generalized_Indexing (N)))
2260 then
2261 declare
2262 A : Node_Id;
2263 E : Node_Id;
2265 begin
2266 if Nkind (N) = N_Indexed_Component then
2267 Rewrite (N, Generalized_Indexing (N));
2268 end if;
2270 A := First_Actual (N);
2271 while Present (A) loop
2272 E := A;
2274 if Nkind (E) = N_Parameter_Association then
2275 E := Explicit_Actual_Parameter (E);
2276 end if;
2278 if Etype (E) = Any_Type then
2279 if Debug_Flag_V then
2280 Write_Str ("Any_Type in call");
2281 Write_Eol;
2282 end if;
2284 exit Interp_Loop;
2285 end if;
2287 Next_Actual (A);
2288 end loop;
2289 end;
2291 elsif Nkind (N) in N_Binary_Op
2292 and then (Etype (Left_Opnd (N)) = Any_Type
2293 or else Etype (Right_Opnd (N)) = Any_Type)
2294 then
2295 exit Interp_Loop;
2297 elsif Nkind (N) in N_Unary_Op
2298 and then Etype (Right_Opnd (N)) = Any_Type
2299 then
2300 exit Interp_Loop;
2301 end if;
2303 -- Not that special case, so issue message using the flag
2304 -- Ambiguous to control printing of the header message
2305 -- only at the start of an ambiguous set.
2307 if not Ambiguous then
2308 if Nkind (N) = N_Function_Call
2309 and then Nkind (Name (N)) = N_Explicit_Dereference
2310 then
2311 Error_Msg_N
2312 ("ambiguous expression (cannot resolve indirect "
2313 & "call)!", N);
2314 else
2315 Error_Msg_NE -- CODEFIX
2316 ("ambiguous expression (cannot resolve&)!",
2317 N, It.Nam);
2318 end if;
2320 Ambiguous := True;
2322 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2323 Error_Msg_N
2324 ("\\possible interpretation (inherited)#!", N);
2325 else
2326 Error_Msg_N -- CODEFIX
2327 ("\\possible interpretation#!", N);
2328 end if;
2330 if Nkind (N) in N_Subprogram_Call
2331 and then Present (Parameter_Associations (N))
2332 then
2333 Report_Ambiguous_Argument;
2334 end if;
2335 end if;
2337 Error_Msg_Sloc := Sloc (It.Nam);
2339 -- By default, the error message refers to the candidate
2340 -- interpretation. But if it is a predefined operator, it
2341 -- is implicitly declared at the declaration of the type
2342 -- of the operand. Recover the sloc of that declaration
2343 -- for the error message.
2345 if Nkind (N) in N_Op
2346 and then Scope (It.Nam) = Standard_Standard
2347 and then not Is_Overloaded (Right_Opnd (N))
2348 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2349 Standard_Standard
2350 then
2351 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2353 if Comes_From_Source (Err_Type)
2354 and then Present (Parent (Err_Type))
2355 then
2356 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2357 end if;
2359 elsif Nkind (N) in N_Binary_Op
2360 and then Scope (It.Nam) = Standard_Standard
2361 and then not Is_Overloaded (Left_Opnd (N))
2362 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2363 Standard_Standard
2364 then
2365 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2367 if Comes_From_Source (Err_Type)
2368 and then Present (Parent (Err_Type))
2369 then
2370 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2371 end if;
2373 -- If this is an indirect call, use the subprogram_type
2374 -- in the message, to have a meaningful location. Also
2375 -- indicate if this is an inherited operation, created
2376 -- by a type declaration.
2378 elsif Nkind (N) = N_Function_Call
2379 and then Nkind (Name (N)) = N_Explicit_Dereference
2380 and then Is_Type (It.Nam)
2381 then
2382 Err_Type := It.Nam;
2383 Error_Msg_Sloc :=
2384 Sloc (Associated_Node_For_Itype (Err_Type));
2385 else
2386 Err_Type := Empty;
2387 end if;
2389 if Nkind (N) in N_Op
2390 and then Scope (It.Nam) = Standard_Standard
2391 and then Present (Err_Type)
2392 then
2393 -- Special-case the message for universal_fixed
2394 -- operators, which are not declared with the type
2395 -- of the operand, but appear forever in Standard.
2397 if It.Typ = Universal_Fixed
2398 and then Scope (It.Nam) = Standard_Standard
2399 then
2400 Error_Msg_N
2401 ("\\possible interpretation as universal_fixed "
2402 & "operation (RM 4.5.5 (19))", N);
2403 else
2404 Error_Msg_N
2405 ("\\possible interpretation (predefined)#!", N);
2406 end if;
2408 elsif
2409 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2410 then
2411 Error_Msg_N
2412 ("\\possible interpretation (inherited)#!", N);
2413 else
2414 Error_Msg_N -- CODEFIX
2415 ("\\possible interpretation#!", N);
2416 end if;
2418 end if;
2419 end if;
2421 -- We have a matching interpretation, Expr_Type is the type
2422 -- from this interpretation, and Seen is the entity.
2424 -- For an operator, just set the entity name. The type will be
2425 -- set by the specific operator resolution routine.
2427 if Nkind (N) in N_Op then
2428 Set_Entity (N, Seen);
2429 Generate_Reference (Seen, N);
2431 elsif Nkind (N) = N_Case_Expression then
2432 Set_Etype (N, Expr_Type);
2434 elsif Nkind (N) = N_Character_Literal then
2435 Set_Etype (N, Expr_Type);
2437 elsif Nkind (N) = N_If_Expression then
2438 Set_Etype (N, Expr_Type);
2440 -- AI05-0139-2: Expression is overloaded because type has
2441 -- implicit dereference. If type matches context, no implicit
2442 -- dereference is involved.
2444 elsif Has_Implicit_Dereference (Expr_Type) then
2445 Set_Etype (N, Expr_Type);
2446 Set_Is_Overloaded (N, False);
2447 exit Interp_Loop;
2449 elsif Is_Overloaded (N)
2450 and then Present (It.Nam)
2451 and then Ekind (It.Nam) = E_Discriminant
2452 and then Has_Implicit_Dereference (It.Nam)
2453 then
2454 -- If the node is a general indexing, the dereference is
2455 -- is inserted when resolving the rewritten form, else
2456 -- insert it now.
2458 if Nkind (N) /= N_Indexed_Component
2459 or else No (Generalized_Indexing (N))
2460 then
2461 Build_Explicit_Dereference (N, It.Nam);
2462 end if;
2464 -- For an explicit dereference, attribute reference, range,
2465 -- short-circuit form (which is not an operator node), or call
2466 -- with a name that is an explicit dereference, there is
2467 -- nothing to be done at this point.
2469 elsif Nkind_In (N, N_Explicit_Dereference,
2470 N_Attribute_Reference,
2471 N_And_Then,
2472 N_Indexed_Component,
2473 N_Or_Else,
2474 N_Range,
2475 N_Selected_Component,
2476 N_Slice)
2477 or else Nkind (Name (N)) = N_Explicit_Dereference
2478 then
2479 null;
2481 -- For procedure or function calls, set the type of the name,
2482 -- and also the entity pointer for the prefix.
2484 elsif Nkind (N) in N_Subprogram_Call
2485 and then Is_Entity_Name (Name (N))
2486 then
2487 Set_Etype (Name (N), Expr_Type);
2488 Set_Entity (Name (N), Seen);
2489 Generate_Reference (Seen, Name (N));
2491 elsif Nkind (N) = N_Function_Call
2492 and then Nkind (Name (N)) = N_Selected_Component
2493 then
2494 Set_Etype (Name (N), Expr_Type);
2495 Set_Entity (Selector_Name (Name (N)), Seen);
2496 Generate_Reference (Seen, Selector_Name (Name (N)));
2498 -- For all other cases, just set the type of the Name
2500 else
2501 Set_Etype (Name (N), Expr_Type);
2502 end if;
2504 end if;
2506 <<Continue>>
2508 -- Move to next interpretation
2510 exit Interp_Loop when No (It.Typ);
2512 Get_Next_Interp (I, It);
2513 end loop Interp_Loop;
2514 end if;
2516 -- At this stage Found indicates whether or not an acceptable
2517 -- interpretation exists. If not, then we have an error, except that if
2518 -- the context is Any_Type as a result of some other error, then we
2519 -- suppress the error report.
2521 if not Found then
2522 if Typ /= Any_Type then
2524 -- If type we are looking for is Void, then this is the procedure
2525 -- call case, and the error is simply that what we gave is not a
2526 -- procedure name (we think of procedure calls as expressions with
2527 -- types internally, but the user doesn't think of them this way).
2529 if Typ = Standard_Void_Type then
2531 -- Special case message if function used as a procedure
2533 if Nkind (N) = N_Procedure_Call_Statement
2534 and then Is_Entity_Name (Name (N))
2535 and then Ekind (Entity (Name (N))) = E_Function
2536 then
2537 Error_Msg_NE
2538 ("cannot use function & in a procedure call",
2539 Name (N), Entity (Name (N)));
2541 -- Otherwise give general message (not clear what cases this
2542 -- covers, but no harm in providing for them).
2544 else
2545 Error_Msg_N ("expect procedure name in procedure call", N);
2546 end if;
2548 Found := True;
2550 -- Otherwise we do have a subexpression with the wrong type
2552 -- Check for the case of an allocator which uses an access type
2553 -- instead of the designated type. This is a common error and we
2554 -- specialize the message, posting an error on the operand of the
2555 -- allocator, complaining that we expected the designated type of
2556 -- the allocator.
2558 elsif Nkind (N) = N_Allocator
2559 and then Is_Access_Type (Typ)
2560 and then Is_Access_Type (Etype (N))
2561 and then Designated_Type (Etype (N)) = Typ
2562 then
2563 Wrong_Type (Expression (N), Designated_Type (Typ));
2564 Found := True;
2566 -- Check for view mismatch on Null in instances, for which the
2567 -- view-swapping mechanism has no identifier.
2569 elsif (In_Instance or else In_Inlined_Body)
2570 and then (Nkind (N) = N_Null)
2571 and then Is_Private_Type (Typ)
2572 and then Is_Access_Type (Full_View (Typ))
2573 then
2574 Resolve (N, Full_View (Typ));
2575 Set_Etype (N, Typ);
2576 Ghost_Mode := Save_Ghost_Mode;
2577 return;
2579 -- Check for an aggregate. Sometimes we can get bogus aggregates
2580 -- from misuse of parentheses, and we are about to complain about
2581 -- the aggregate without even looking inside it.
2583 -- Instead, if we have an aggregate of type Any_Composite, then
2584 -- analyze and resolve the component fields, and then only issue
2585 -- another message if we get no errors doing this (otherwise
2586 -- assume that the errors in the aggregate caused the problem).
2588 elsif Nkind (N) = N_Aggregate
2589 and then Etype (N) = Any_Composite
2590 then
2591 -- Disable expansion in any case. If there is a type mismatch
2592 -- it may be fatal to try to expand the aggregate. The flag
2593 -- would otherwise be set to false when the error is posted.
2595 Expander_Active := False;
2597 declare
2598 procedure Check_Aggr (Aggr : Node_Id);
2599 -- Check one aggregate, and set Found to True if we have a
2600 -- definite error in any of its elements
2602 procedure Check_Elmt (Aelmt : Node_Id);
2603 -- Check one element of aggregate and set Found to True if
2604 -- we definitely have an error in the element.
2606 ----------------
2607 -- Check_Aggr --
2608 ----------------
2610 procedure Check_Aggr (Aggr : Node_Id) is
2611 Elmt : Node_Id;
2613 begin
2614 if Present (Expressions (Aggr)) then
2615 Elmt := First (Expressions (Aggr));
2616 while Present (Elmt) loop
2617 Check_Elmt (Elmt);
2618 Next (Elmt);
2619 end loop;
2620 end if;
2622 if Present (Component_Associations (Aggr)) then
2623 Elmt := First (Component_Associations (Aggr));
2624 while Present (Elmt) loop
2626 -- If this is a default-initialized component, then
2627 -- there is nothing to check. The box will be
2628 -- replaced by the appropriate call during late
2629 -- expansion.
2631 if not Box_Present (Elmt) then
2632 Check_Elmt (Expression (Elmt));
2633 end if;
2635 Next (Elmt);
2636 end loop;
2637 end if;
2638 end Check_Aggr;
2640 ----------------
2641 -- Check_Elmt --
2642 ----------------
2644 procedure Check_Elmt (Aelmt : Node_Id) is
2645 begin
2646 -- If we have a nested aggregate, go inside it (to
2647 -- attempt a naked analyze-resolve of the aggregate can
2648 -- cause undesirable cascaded errors). Do not resolve
2649 -- expression if it needs a type from context, as for
2650 -- integer * fixed expression.
2652 if Nkind (Aelmt) = N_Aggregate then
2653 Check_Aggr (Aelmt);
2655 else
2656 Analyze (Aelmt);
2658 if not Is_Overloaded (Aelmt)
2659 and then Etype (Aelmt) /= Any_Fixed
2660 then
2661 Resolve (Aelmt);
2662 end if;
2664 if Etype (Aelmt) = Any_Type then
2665 Found := True;
2666 end if;
2667 end if;
2668 end Check_Elmt;
2670 begin
2671 Check_Aggr (N);
2672 end;
2673 end if;
2675 -- Looks like we have a type error, but check for special case
2676 -- of Address wanted, integer found, with the configuration pragma
2677 -- Allow_Integer_Address active. If we have this case, introduce
2678 -- an unchecked conversion to allow the integer expression to be
2679 -- treated as an Address. The reverse case of integer wanted,
2680 -- Address found, is treated in an analogous manner.
2682 if Address_Integer_Convert_OK (Typ, Etype (N)) then
2683 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
2684 Analyze_And_Resolve (N, Typ);
2685 Ghost_Mode := Save_Ghost_Mode;
2686 return;
2687 end if;
2689 -- That special Allow_Integer_Address check did not appply, so we
2690 -- have a real type error. If an error message was issued already,
2691 -- Found got reset to True, so if it's still False, issue standard
2692 -- Wrong_Type message.
2694 if not Found then
2695 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
2696 declare
2697 Subp_Name : Node_Id;
2699 begin
2700 if Is_Entity_Name (Name (N)) then
2701 Subp_Name := Name (N);
2703 elsif Nkind (Name (N)) = N_Selected_Component then
2705 -- Protected operation: retrieve operation name
2707 Subp_Name := Selector_Name (Name (N));
2709 else
2710 raise Program_Error;
2711 end if;
2713 Error_Msg_Node_2 := Typ;
2714 Error_Msg_NE
2715 ("no visible interpretation of& "
2716 & "matches expected type&", N, Subp_Name);
2717 end;
2719 if All_Errors_Mode then
2720 declare
2721 Index : Interp_Index;
2722 It : Interp;
2724 begin
2725 Error_Msg_N ("\\possible interpretations:", N);
2727 Get_First_Interp (Name (N), Index, It);
2728 while Present (It.Nam) loop
2729 Error_Msg_Sloc := Sloc (It.Nam);
2730 Error_Msg_Node_2 := It.Nam;
2731 Error_Msg_NE
2732 ("\\ type& for & declared#", N, It.Typ);
2733 Get_Next_Interp (Index, It);
2734 end loop;
2735 end;
2737 else
2738 Error_Msg_N ("\use -gnatf for details", N);
2739 end if;
2741 else
2742 Wrong_Type (N, Typ);
2743 end if;
2744 end if;
2745 end if;
2747 Resolution_Failed;
2748 Ghost_Mode := Save_Ghost_Mode;
2749 return;
2751 -- Test if we have more than one interpretation for the context
2753 elsif Ambiguous then
2754 Resolution_Failed;
2755 Ghost_Mode := Save_Ghost_Mode;
2756 return;
2758 -- Only one intepretation
2760 else
2761 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2762 -- the "+" on T is abstract, and the operands are of universal type,
2763 -- the above code will have (incorrectly) resolved the "+" to the
2764 -- universal one in Standard. Therefore check for this case and give
2765 -- an error. We can't do this earlier, because it would cause legal
2766 -- cases to get errors (when some other type has an abstract "+").
2768 if Ada_Version >= Ada_2005
2769 and then Nkind (N) in N_Op
2770 and then Is_Overloaded (N)
2771 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
2772 then
2773 Get_First_Interp (N, I, It);
2774 while Present (It.Typ) loop
2775 if Present (It.Abstract_Op) and then
2776 Etype (It.Abstract_Op) = Typ
2777 then
2778 Error_Msg_NE
2779 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2780 return;
2781 end if;
2783 Get_Next_Interp (I, It);
2784 end loop;
2785 end if;
2787 -- Here we have an acceptable interpretation for the context
2789 -- Propagate type information and normalize tree for various
2790 -- predefined operations. If the context only imposes a class of
2791 -- types, rather than a specific type, propagate the actual type
2792 -- downward.
2794 if Typ = Any_Integer or else
2795 Typ = Any_Boolean or else
2796 Typ = Any_Modular or else
2797 Typ = Any_Real or else
2798 Typ = Any_Discrete
2799 then
2800 Ctx_Type := Expr_Type;
2802 -- Any_Fixed is legal in a real context only if a specific fixed-
2803 -- point type is imposed. If Norman Cohen can be confused by this,
2804 -- it deserves a separate message.
2806 if Typ = Any_Real
2807 and then Expr_Type = Any_Fixed
2808 then
2809 Error_Msg_N ("illegal context for mixed mode operation", N);
2810 Set_Etype (N, Universal_Real);
2811 Ctx_Type := Universal_Real;
2812 end if;
2813 end if;
2815 -- A user-defined operator is transformed into a function call at
2816 -- this point, so that further processing knows that operators are
2817 -- really operators (i.e. are predefined operators). User-defined
2818 -- operators that are intrinsic are just renamings of the predefined
2819 -- ones, and need not be turned into calls either, but if they rename
2820 -- a different operator, we must transform the node accordingly.
2821 -- Instantiations of Unchecked_Conversion are intrinsic but are
2822 -- treated as functions, even if given an operator designator.
2824 if Nkind (N) in N_Op
2825 and then Present (Entity (N))
2826 and then Ekind (Entity (N)) /= E_Operator
2827 then
2829 if not Is_Predefined_Op (Entity (N)) then
2830 Rewrite_Operator_As_Call (N, Entity (N));
2832 elsif Present (Alias (Entity (N)))
2833 and then
2834 Nkind (Parent (Parent (Entity (N)))) =
2835 N_Subprogram_Renaming_Declaration
2836 then
2837 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2839 -- If the node is rewritten, it will be fully resolved in
2840 -- Rewrite_Renamed_Operator.
2842 if Analyzed (N) then
2843 Ghost_Mode := Save_Ghost_Mode;
2844 return;
2845 end if;
2846 end if;
2847 end if;
2849 case N_Subexpr'(Nkind (N)) is
2851 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2853 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2855 when N_Short_Circuit
2856 => Resolve_Short_Circuit (N, Ctx_Type);
2858 when N_Attribute_Reference
2859 => Resolve_Attribute (N, Ctx_Type);
2861 when N_Case_Expression
2862 => Resolve_Case_Expression (N, Ctx_Type);
2864 when N_Character_Literal
2865 => Resolve_Character_Literal (N, Ctx_Type);
2867 when N_Expanded_Name
2868 => Resolve_Entity_Name (N, Ctx_Type);
2870 when N_Explicit_Dereference
2871 => Resolve_Explicit_Dereference (N, Ctx_Type);
2873 when N_Expression_With_Actions
2874 => Resolve_Expression_With_Actions (N, Ctx_Type);
2876 when N_Extension_Aggregate
2877 => Resolve_Extension_Aggregate (N, Ctx_Type);
2879 when N_Function_Call
2880 => Resolve_Call (N, Ctx_Type);
2882 when N_Identifier
2883 => Resolve_Entity_Name (N, Ctx_Type);
2885 when N_If_Expression
2886 => Resolve_If_Expression (N, Ctx_Type);
2888 when N_Indexed_Component
2889 => Resolve_Indexed_Component (N, Ctx_Type);
2891 when N_Integer_Literal
2892 => Resolve_Integer_Literal (N, Ctx_Type);
2894 when N_Membership_Test
2895 => Resolve_Membership_Op (N, Ctx_Type);
2897 when N_Null => Resolve_Null (N, Ctx_Type);
2899 when N_Op_And | N_Op_Or | N_Op_Xor
2900 => Resolve_Logical_Op (N, Ctx_Type);
2902 when N_Op_Eq | N_Op_Ne
2903 => Resolve_Equality_Op (N, Ctx_Type);
2905 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2906 => Resolve_Comparison_Op (N, Ctx_Type);
2908 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2910 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2911 N_Op_Divide | N_Op_Mod | N_Op_Rem
2913 => Resolve_Arithmetic_Op (N, Ctx_Type);
2915 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2917 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2919 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2920 => Resolve_Unary_Op (N, Ctx_Type);
2922 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2924 when N_Procedure_Call_Statement
2925 => Resolve_Call (N, Ctx_Type);
2927 when N_Operator_Symbol
2928 => Resolve_Operator_Symbol (N, Ctx_Type);
2930 when N_Qualified_Expression
2931 => Resolve_Qualified_Expression (N, Ctx_Type);
2933 -- Why is the following null, needs a comment ???
2935 when N_Quantified_Expression
2936 => null;
2938 when N_Raise_Expression
2939 => Resolve_Raise_Expression (N, Ctx_Type);
2941 when N_Raise_xxx_Error
2942 => Set_Etype (N, Ctx_Type);
2944 when N_Range => Resolve_Range (N, Ctx_Type);
2946 when N_Real_Literal
2947 => Resolve_Real_Literal (N, Ctx_Type);
2949 when N_Reference => Resolve_Reference (N, Ctx_Type);
2951 when N_Selected_Component
2952 => Resolve_Selected_Component (N, Ctx_Type);
2954 when N_Slice => Resolve_Slice (N, Ctx_Type);
2956 when N_String_Literal
2957 => Resolve_String_Literal (N, Ctx_Type);
2959 when N_Type_Conversion
2960 => Resolve_Type_Conversion (N, Ctx_Type);
2962 when N_Unchecked_Expression =>
2963 Resolve_Unchecked_Expression (N, Ctx_Type);
2965 when N_Unchecked_Type_Conversion =>
2966 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2967 end case;
2969 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2970 -- expression of an anonymous access type that occurs in the context
2971 -- of a named general access type, except when the expression is that
2972 -- of a membership test. This ensures proper legality checking in
2973 -- terms of allowed conversions (expressions that would be illegal to
2974 -- convert implicitly are allowed in membership tests).
2976 if Ada_Version >= Ada_2012
2977 and then Ekind (Ctx_Type) = E_General_Access_Type
2978 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2979 and then Nkind (Parent (N)) not in N_Membership_Test
2980 then
2981 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2982 Analyze_And_Resolve (N, Ctx_Type);
2983 end if;
2985 -- If the subexpression was replaced by a non-subexpression, then
2986 -- all we do is to expand it. The only legitimate case we know of
2987 -- is converting procedure call statement to entry call statements,
2988 -- but there may be others, so we are making this test general.
2990 if Nkind (N) not in N_Subexpr then
2991 Debug_A_Exit ("resolving ", N, " (done)");
2992 Expand (N);
2993 Ghost_Mode := Save_Ghost_Mode;
2994 return;
2995 end if;
2997 -- The expression is definitely NOT overloaded at this point, so
2998 -- we reset the Is_Overloaded flag to avoid any confusion when
2999 -- reanalyzing the node.
3001 Set_Is_Overloaded (N, False);
3003 -- Freeze expression type, entity if it is a name, and designated
3004 -- type if it is an allocator (RM 13.14(10,11,13)).
3006 -- Now that the resolution of the type of the node is complete, and
3007 -- we did not detect an error, we can expand this node. We skip the
3008 -- expand call if we are in a default expression, see section
3009 -- "Handling of Default Expressions" in Sem spec.
3011 Debug_A_Exit ("resolving ", N, " (done)");
3013 -- We unconditionally freeze the expression, even if we are in
3014 -- default expression mode (the Freeze_Expression routine tests this
3015 -- flag and only freezes static types if it is set).
3017 -- Ada 2012 (AI05-177): The declaration of an expression function
3018 -- does not cause freezing, but we never reach here in that case.
3019 -- Here we are resolving the corresponding expanded body, so we do
3020 -- need to perform normal freezing.
3022 Freeze_Expression (N);
3024 -- Now we can do the expansion
3026 Expand (N);
3027 end if;
3029 Ghost_Mode := Save_Ghost_Mode;
3030 end Resolve;
3032 -------------
3033 -- Resolve --
3034 -------------
3036 -- Version with check(s) suppressed
3038 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3039 begin
3040 if Suppress = All_Checks then
3041 declare
3042 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3043 begin
3044 Scope_Suppress.Suppress := (others => True);
3045 Resolve (N, Typ);
3046 Scope_Suppress.Suppress := Sva;
3047 end;
3049 else
3050 declare
3051 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3052 begin
3053 Scope_Suppress.Suppress (Suppress) := True;
3054 Resolve (N, Typ);
3055 Scope_Suppress.Suppress (Suppress) := Svg;
3056 end;
3057 end if;
3058 end Resolve;
3060 -------------
3061 -- Resolve --
3062 -------------
3064 -- Version with implicit type
3066 procedure Resolve (N : Node_Id) is
3067 begin
3068 Resolve (N, Etype (N));
3069 end Resolve;
3071 ---------------------
3072 -- Resolve_Actuals --
3073 ---------------------
3075 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3076 Loc : constant Source_Ptr := Sloc (N);
3077 A : Node_Id;
3078 A_Id : Entity_Id;
3079 A_Typ : Entity_Id;
3080 F : Entity_Id;
3081 F_Typ : Entity_Id;
3082 Prev : Node_Id := Empty;
3083 Orig_A : Node_Id;
3084 Real_F : Entity_Id;
3086 Real_Subp : Entity_Id;
3087 -- If the subprogram being called is an inherited operation for
3088 -- a formal derived type in an instance, Real_Subp is the subprogram
3089 -- that will be called. It may have different formal names than the
3090 -- operation of the formal in the generic, so after actual is resolved
3091 -- the name of the actual in a named association must carry the name
3092 -- of the actual of the subprogram being called.
3094 procedure Check_Aliased_Parameter;
3095 -- Check rules on aliased parameters and related accessibility rules
3096 -- in (RM 3.10.2 (10.2-10.4)).
3098 procedure Check_Argument_Order;
3099 -- Performs a check for the case where the actuals are all simple
3100 -- identifiers that correspond to the formal names, but in the wrong
3101 -- order, which is considered suspicious and cause for a warning.
3103 procedure Check_Prefixed_Call;
3104 -- If the original node is an overloaded call in prefix notation,
3105 -- insert an 'Access or a dereference as needed over the first actual.
3106 -- Try_Object_Operation has already verified that there is a valid
3107 -- interpretation, but the form of the actual can only be determined
3108 -- once the primitive operation is identified.
3110 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id);
3111 -- Emit an error concerning the illegal usage of an effectively volatile
3112 -- object in interfering context (SPARK RM 7.13(12)).
3114 procedure Insert_Default;
3115 -- If the actual is missing in a call, insert in the actuals list
3116 -- an instance of the default expression. The insertion is always
3117 -- a named association.
3119 procedure Property_Error
3120 (Var : Node_Id;
3121 Var_Id : Entity_Id;
3122 Prop_Nam : Name_Id);
3123 -- Emit an error concerning variable Var with entity Var_Id that has
3124 -- enabled property Prop_Nam when it acts as an actual parameter in a
3125 -- call and the corresponding formal parameter is of mode IN.
3127 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3128 -- Check whether T1 and T2, or their full views, are derived from a
3129 -- common type. Used to enforce the restrictions on array conversions
3130 -- of AI95-00246.
3132 function Static_Concatenation (N : Node_Id) return Boolean;
3133 -- Predicate to determine whether an actual that is a concatenation
3134 -- will be evaluated statically and does not need a transient scope.
3135 -- This must be determined before the actual is resolved and expanded
3136 -- because if needed the transient scope must be introduced earlier.
3138 -----------------------------
3139 -- Check_Aliased_Parameter --
3140 -----------------------------
3142 procedure Check_Aliased_Parameter is
3143 Nominal_Subt : Entity_Id;
3145 begin
3146 if Is_Aliased (F) then
3147 if Is_Tagged_Type (A_Typ) then
3148 null;
3150 elsif Is_Aliased_View (A) then
3151 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3152 Nominal_Subt := Base_Type (A_Typ);
3153 else
3154 Nominal_Subt := A_Typ;
3155 end if;
3157 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3158 null;
3160 -- In a generic body assume the worst for generic formals:
3161 -- they can have a constrained partial view (AI05-041).
3163 elsif Has_Discriminants (F_Typ)
3164 and then not Is_Constrained (F_Typ)
3165 and then not Has_Constrained_Partial_View (F_Typ)
3166 and then not Is_Generic_Type (F_Typ)
3167 then
3168 null;
3170 else
3171 Error_Msg_NE ("untagged actual does not match "
3172 & "aliased formal&", A, F);
3173 end if;
3175 else
3176 Error_Msg_NE ("actual for aliased formal& must be "
3177 & "aliased object", A, F);
3178 end if;
3180 if Ekind (Nam) = E_Procedure then
3181 null;
3183 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3184 if Nkind (Parent (N)) = N_Type_Conversion
3185 and then Type_Access_Level (Etype (Parent (N))) <
3186 Object_Access_Level (A)
3187 then
3188 Error_Msg_N ("aliased actual has wrong accessibility", A);
3189 end if;
3191 elsif Nkind (Parent (N)) = N_Qualified_Expression
3192 and then Nkind (Parent (Parent (N))) = N_Allocator
3193 and then Type_Access_Level (Etype (Parent (Parent (N)))) <
3194 Object_Access_Level (A)
3195 then
3196 Error_Msg_N
3197 ("aliased actual in allocator has wrong accessibility", A);
3198 end if;
3199 end if;
3200 end Check_Aliased_Parameter;
3202 --------------------------
3203 -- Check_Argument_Order --
3204 --------------------------
3206 procedure Check_Argument_Order is
3207 begin
3208 -- Nothing to do if no parameters, or original node is neither a
3209 -- function call nor a procedure call statement (happens in the
3210 -- operator-transformed-to-function call case), or the call does
3211 -- not come from source, or this warning is off.
3213 if not Warn_On_Parameter_Order
3214 or else No (Parameter_Associations (N))
3215 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3216 or else not Comes_From_Source (N)
3217 then
3218 return;
3219 end if;
3221 declare
3222 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3224 begin
3225 -- Nothing to do if only one parameter
3227 if Nargs < 2 then
3228 return;
3229 end if;
3231 -- Here if at least two arguments
3233 declare
3234 Actuals : array (1 .. Nargs) of Node_Id;
3235 Actual : Node_Id;
3236 Formal : Node_Id;
3238 Wrong_Order : Boolean := False;
3239 -- Set True if an out of order case is found
3241 begin
3242 -- Collect identifier names of actuals, fail if any actual is
3243 -- not a simple identifier, and record max length of name.
3245 Actual := First (Parameter_Associations (N));
3246 for J in Actuals'Range loop
3247 if Nkind (Actual) /= N_Identifier then
3248 return;
3249 else
3250 Actuals (J) := Actual;
3251 Next (Actual);
3252 end if;
3253 end loop;
3255 -- If we got this far, all actuals are identifiers and the list
3256 -- of their names is stored in the Actuals array.
3258 Formal := First_Formal (Nam);
3259 for J in Actuals'Range loop
3261 -- If we ran out of formals, that's odd, probably an error
3262 -- which will be detected elsewhere, but abandon the search.
3264 if No (Formal) then
3265 return;
3266 end if;
3268 -- If name matches and is in order OK
3270 if Chars (Formal) = Chars (Actuals (J)) then
3271 null;
3273 else
3274 -- If no match, see if it is elsewhere in list and if so
3275 -- flag potential wrong order if type is compatible.
3277 for K in Actuals'Range loop
3278 if Chars (Formal) = Chars (Actuals (K))
3279 and then
3280 Has_Compatible_Type (Actuals (K), Etype (Formal))
3281 then
3282 Wrong_Order := True;
3283 goto Continue;
3284 end if;
3285 end loop;
3287 -- No match
3289 return;
3290 end if;
3292 <<Continue>> Next_Formal (Formal);
3293 end loop;
3295 -- If Formals left over, also probably an error, skip warning
3297 if Present (Formal) then
3298 return;
3299 end if;
3301 -- Here we give the warning if something was out of order
3303 if Wrong_Order then
3304 Error_Msg_N
3305 ("?P?actuals for this call may be in wrong order", N);
3306 end if;
3307 end;
3308 end;
3309 end Check_Argument_Order;
3311 -------------------------
3312 -- Check_Prefixed_Call --
3313 -------------------------
3315 procedure Check_Prefixed_Call is
3316 Act : constant Node_Id := First_Actual (N);
3317 A_Type : constant Entity_Id := Etype (Act);
3318 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3319 Orig : constant Node_Id := Original_Node (N);
3320 New_A : Node_Id;
3322 begin
3323 -- Check whether the call is a prefixed call, with or without
3324 -- additional actuals.
3326 if Nkind (Orig) = N_Selected_Component
3327 or else
3328 (Nkind (Orig) = N_Indexed_Component
3329 and then Nkind (Prefix (Orig)) = N_Selected_Component
3330 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3331 and then Is_Entity_Name (Act)
3332 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3333 then
3334 if Is_Access_Type (A_Type)
3335 and then not Is_Access_Type (F_Type)
3336 then
3337 -- Introduce dereference on object in prefix
3339 New_A :=
3340 Make_Explicit_Dereference (Sloc (Act),
3341 Prefix => Relocate_Node (Act));
3342 Rewrite (Act, New_A);
3343 Analyze (Act);
3345 elsif Is_Access_Type (F_Type)
3346 and then not Is_Access_Type (A_Type)
3347 then
3348 -- Introduce an implicit 'Access in prefix
3350 if not Is_Aliased_View (Act) then
3351 Error_Msg_NE
3352 ("object in prefixed call to& must be aliased "
3353 & "(RM 4.1.3 (13 1/2))",
3354 Prefix (Act), Nam);
3355 end if;
3357 Rewrite (Act,
3358 Make_Attribute_Reference (Loc,
3359 Attribute_Name => Name_Access,
3360 Prefix => Relocate_Node (Act)));
3361 end if;
3363 Analyze (Act);
3364 end if;
3365 end Check_Prefixed_Call;
3367 ---------------------------------------
3368 -- Flag_Effectively_Volatile_Objects --
3369 ---------------------------------------
3371 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id) is
3372 function Flag_Object (N : Node_Id) return Traverse_Result;
3373 -- Determine whether arbitrary node N denotes an effectively volatile
3374 -- object and if it does, emit an error.
3376 -----------------
3377 -- Flag_Object --
3378 -----------------
3380 function Flag_Object (N : Node_Id) return Traverse_Result is
3381 Id : Entity_Id;
3383 begin
3384 -- Do not consider nested function calls because they have already
3385 -- been processed during their own resolution.
3387 if Nkind (N) = N_Function_Call then
3388 return Skip;
3390 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
3391 Id := Entity (N);
3393 if Is_Object (Id)
3394 and then Is_Effectively_Volatile (Id)
3395 and then (Async_Writers_Enabled (Id)
3396 or else Effective_Reads_Enabled (Id))
3397 then
3398 Error_Msg_N
3399 ("volatile object cannot appear in this context (SPARK "
3400 & "RM 7.1.3(11))", N);
3401 return Skip;
3402 end if;
3403 end if;
3405 return OK;
3406 end Flag_Object;
3408 procedure Flag_Objects is new Traverse_Proc (Flag_Object);
3410 -- Start of processing for Flag_Effectively_Volatile_Objects
3412 begin
3413 Flag_Objects (Expr);
3414 end Flag_Effectively_Volatile_Objects;
3416 --------------------
3417 -- Insert_Default --
3418 --------------------
3420 procedure Insert_Default is
3421 Actval : Node_Id;
3422 Assoc : Node_Id;
3424 begin
3425 -- Missing argument in call, nothing to insert
3427 if No (Default_Value (F)) then
3428 return;
3430 else
3431 -- Note that we do a full New_Copy_Tree, so that any associated
3432 -- Itypes are properly copied. This may not be needed any more,
3433 -- but it does no harm as a safety measure. Defaults of a generic
3434 -- formal may be out of bounds of the corresponding actual (see
3435 -- cc1311b) and an additional check may be required.
3437 Actval :=
3438 New_Copy_Tree
3439 (Default_Value (F),
3440 New_Scope => Current_Scope,
3441 New_Sloc => Loc);
3443 -- Propagate dimension information, if any.
3445 Copy_Dimensions (Default_Value (F), Actval);
3447 if Is_Concurrent_Type (Scope (Nam))
3448 and then Has_Discriminants (Scope (Nam))
3449 then
3450 Replace_Actual_Discriminants (N, Actval);
3451 end if;
3453 if Is_Overloadable (Nam)
3454 and then Present (Alias (Nam))
3455 then
3456 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3457 and then not Is_Tagged_Type (Etype (F))
3458 then
3459 -- If default is a real literal, do not introduce a
3460 -- conversion whose effect may depend on the run-time
3461 -- size of universal real.
3463 if Nkind (Actval) = N_Real_Literal then
3464 Set_Etype (Actval, Base_Type (Etype (F)));
3465 else
3466 Actval := Unchecked_Convert_To (Etype (F), Actval);
3467 end if;
3468 end if;
3470 if Is_Scalar_Type (Etype (F)) then
3471 Enable_Range_Check (Actval);
3472 end if;
3474 Set_Parent (Actval, N);
3476 -- Resolve aggregates with their base type, to avoid scope
3477 -- anomalies: the subtype was first built in the subprogram
3478 -- declaration, and the current call may be nested.
3480 if Nkind (Actval) = N_Aggregate then
3481 Analyze_And_Resolve (Actval, Etype (F));
3482 else
3483 Analyze_And_Resolve (Actval, Etype (Actval));
3484 end if;
3486 else
3487 Set_Parent (Actval, N);
3489 -- See note above concerning aggregates
3491 if Nkind (Actval) = N_Aggregate
3492 and then Has_Discriminants (Etype (Actval))
3493 then
3494 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3496 -- Resolve entities with their own type, which may differ from
3497 -- the type of a reference in a generic context (the view
3498 -- swapping mechanism did not anticipate the re-analysis of
3499 -- default values in calls).
3501 elsif Is_Entity_Name (Actval) then
3502 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3504 else
3505 Analyze_And_Resolve (Actval, Etype (Actval));
3506 end if;
3507 end if;
3509 -- If default is a tag indeterminate function call, propagate tag
3510 -- to obtain proper dispatching.
3512 if Is_Controlling_Formal (F)
3513 and then Nkind (Default_Value (F)) = N_Function_Call
3514 then
3515 Set_Is_Controlling_Actual (Actval);
3516 end if;
3517 end if;
3519 -- If the default expression raises constraint error, then just
3520 -- silently replace it with an N_Raise_Constraint_Error node, since
3521 -- we already gave the warning on the subprogram spec. If node is
3522 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3523 -- the warnings removal machinery.
3525 if Raises_Constraint_Error (Actval)
3526 and then Nkind (Actval) /= N_Raise_Constraint_Error
3527 then
3528 Rewrite (Actval,
3529 Make_Raise_Constraint_Error (Loc,
3530 Reason => CE_Range_Check_Failed));
3531 Set_Raises_Constraint_Error (Actval);
3532 Set_Etype (Actval, Etype (F));
3533 end if;
3535 Assoc :=
3536 Make_Parameter_Association (Loc,
3537 Explicit_Actual_Parameter => Actval,
3538 Selector_Name => Make_Identifier (Loc, Chars (F)));
3540 -- Case of insertion is first named actual
3542 if No (Prev) or else
3543 Nkind (Parent (Prev)) /= N_Parameter_Association
3544 then
3545 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3546 Set_First_Named_Actual (N, Actval);
3548 if No (Prev) then
3549 if No (Parameter_Associations (N)) then
3550 Set_Parameter_Associations (N, New_List (Assoc));
3551 else
3552 Append (Assoc, Parameter_Associations (N));
3553 end if;
3555 else
3556 Insert_After (Prev, Assoc);
3557 end if;
3559 -- Case of insertion is not first named actual
3561 else
3562 Set_Next_Named_Actual
3563 (Assoc, Next_Named_Actual (Parent (Prev)));
3564 Set_Next_Named_Actual (Parent (Prev), Actval);
3565 Append (Assoc, Parameter_Associations (N));
3566 end if;
3568 Mark_Rewrite_Insertion (Assoc);
3569 Mark_Rewrite_Insertion (Actval);
3571 Prev := Actval;
3572 end Insert_Default;
3574 --------------------
3575 -- Property_Error --
3576 --------------------
3578 procedure Property_Error
3579 (Var : Node_Id;
3580 Var_Id : Entity_Id;
3581 Prop_Nam : Name_Id)
3583 begin
3584 Error_Msg_Name_1 := Prop_Nam;
3585 Error_Msg_NE
3586 ("external variable & with enabled property % cannot appear as "
3587 & "actual in procedure call (SPARK RM 7.1.3(10))", Var, Var_Id);
3588 Error_Msg_N ("\\corresponding formal parameter has mode In", Var);
3589 end Property_Error;
3591 -------------------
3592 -- Same_Ancestor --
3593 -------------------
3595 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3596 FT1 : Entity_Id := T1;
3597 FT2 : Entity_Id := T2;
3599 begin
3600 if Is_Private_Type (T1)
3601 and then Present (Full_View (T1))
3602 then
3603 FT1 := Full_View (T1);
3604 end if;
3606 if Is_Private_Type (T2)
3607 and then Present (Full_View (T2))
3608 then
3609 FT2 := Full_View (T2);
3610 end if;
3612 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3613 end Same_Ancestor;
3615 --------------------------
3616 -- Static_Concatenation --
3617 --------------------------
3619 function Static_Concatenation (N : Node_Id) return Boolean is
3620 begin
3621 case Nkind (N) is
3622 when N_String_Literal =>
3623 return True;
3625 when N_Op_Concat =>
3627 -- Concatenation is static when both operands are static and
3628 -- the concatenation operator is a predefined one.
3630 return Scope (Entity (N)) = Standard_Standard
3631 and then
3632 Static_Concatenation (Left_Opnd (N))
3633 and then
3634 Static_Concatenation (Right_Opnd (N));
3636 when others =>
3637 if Is_Entity_Name (N) then
3638 declare
3639 Ent : constant Entity_Id := Entity (N);
3640 begin
3641 return Ekind (Ent) = E_Constant
3642 and then Present (Constant_Value (Ent))
3643 and then
3644 Is_OK_Static_Expression (Constant_Value (Ent));
3645 end;
3647 else
3648 return False;
3649 end if;
3650 end case;
3651 end Static_Concatenation;
3653 -- Start of processing for Resolve_Actuals
3655 begin
3656 Check_Argument_Order;
3658 if Is_Overloadable (Nam)
3659 and then Is_Inherited_Operation (Nam)
3660 and then In_Instance
3661 and then Present (Alias (Nam))
3662 and then Present (Overridden_Operation (Alias (Nam)))
3663 then
3664 Real_Subp := Alias (Nam);
3665 else
3666 Real_Subp := Empty;
3667 end if;
3669 if Present (First_Actual (N)) then
3670 Check_Prefixed_Call;
3671 end if;
3673 A := First_Actual (N);
3674 F := First_Formal (Nam);
3676 if Present (Real_Subp) then
3677 Real_F := First_Formal (Real_Subp);
3678 end if;
3680 while Present (F) loop
3681 if No (A) and then Needs_No_Actuals (Nam) then
3682 null;
3684 -- If we have an error in any actual or formal, indicated by a type
3685 -- of Any_Type, then abandon resolution attempt, and set result type
3686 -- to Any_Type. Skip this if the actual is a Raise_Expression, whose
3687 -- type is imposed from context.
3689 elsif (Present (A) and then Etype (A) = Any_Type)
3690 or else Etype (F) = Any_Type
3691 then
3692 if Nkind (A) /= N_Raise_Expression then
3693 Set_Etype (N, Any_Type);
3694 return;
3695 end if;
3696 end if;
3698 -- Case where actual is present
3700 -- If the actual is an entity, generate a reference to it now. We
3701 -- do this before the actual is resolved, because a formal of some
3702 -- protected subprogram, or a task discriminant, will be rewritten
3703 -- during expansion, and the source entity reference may be lost.
3705 if Present (A)
3706 and then Is_Entity_Name (A)
3707 and then Comes_From_Source (A)
3708 then
3709 Orig_A := Entity (A);
3711 if Present (Orig_A) then
3712 if Is_Formal (Orig_A)
3713 and then Ekind (F) /= E_In_Parameter
3714 then
3715 Generate_Reference (Orig_A, A, 'm');
3717 elsif not Is_Overloaded (A) then
3718 if Ekind (F) /= E_Out_Parameter then
3719 Generate_Reference (Orig_A, A);
3721 -- RM 6.4.1(12): For an out parameter that is passed by
3722 -- copy, the formal parameter object is created, and:
3724 -- * For an access type, the formal parameter is initialized
3725 -- from the value of the actual, without checking that the
3726 -- value satisfies any constraint, any predicate, or any
3727 -- exclusion of the null value.
3729 -- * For a scalar type that has the Default_Value aspect
3730 -- specified, the formal parameter is initialized from the
3731 -- value of the actual, without checking that the value
3732 -- satisfies any constraint or any predicate.
3733 -- I do not understand why this case is included??? this is
3734 -- not a case where an OUT parameter is treated as IN OUT.
3736 -- * For a composite type with discriminants or that has
3737 -- implicit initial values for any subcomponents, the
3738 -- behavior is as for an in out parameter passed by copy.
3740 -- Hence for these cases we generate the read reference now
3741 -- (the write reference will be generated later by
3742 -- Note_Possible_Modification).
3744 elsif Is_By_Copy_Type (Etype (F))
3745 and then
3746 (Is_Access_Type (Etype (F))
3747 or else
3748 (Is_Scalar_Type (Etype (F))
3749 and then
3750 Present (Default_Aspect_Value (Etype (F))))
3751 or else
3752 (Is_Composite_Type (Etype (F))
3753 and then (Has_Discriminants (Etype (F))
3754 or else Is_Partially_Initialized_Type
3755 (Etype (F)))))
3756 then
3757 Generate_Reference (Orig_A, A);
3758 end if;
3759 end if;
3760 end if;
3761 end if;
3763 if Present (A)
3764 and then (Nkind (Parent (A)) /= N_Parameter_Association
3765 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3766 then
3767 -- If style checking mode on, check match of formal name
3769 if Style_Check then
3770 if Nkind (Parent (A)) = N_Parameter_Association then
3771 Check_Identifier (Selector_Name (Parent (A)), F);
3772 end if;
3773 end if;
3775 -- If the formal is Out or In_Out, do not resolve and expand the
3776 -- conversion, because it is subsequently expanded into explicit
3777 -- temporaries and assignments. However, the object of the
3778 -- conversion can be resolved. An exception is the case of tagged
3779 -- type conversion with a class-wide actual. In that case we want
3780 -- the tag check to occur and no temporary will be needed (no
3781 -- representation change can occur) and the parameter is passed by
3782 -- reference, so we go ahead and resolve the type conversion.
3783 -- Another exception is the case of reference to component or
3784 -- subcomponent of a bit-packed array, in which case we want to
3785 -- defer expansion to the point the in and out assignments are
3786 -- performed.
3788 if Ekind (F) /= E_In_Parameter
3789 and then Nkind (A) = N_Type_Conversion
3790 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3791 then
3792 if Ekind (F) = E_In_Out_Parameter
3793 and then Is_Array_Type (Etype (F))
3794 then
3795 -- In a view conversion, the conversion must be legal in
3796 -- both directions, and thus both component types must be
3797 -- aliased, or neither (4.6 (8)).
3799 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3800 -- the privacy requirement should not apply to generic
3801 -- types, and should be checked in an instance. ARG query
3802 -- is in order ???
3804 if Has_Aliased_Components (Etype (Expression (A))) /=
3805 Has_Aliased_Components (Etype (F))
3806 then
3807 Error_Msg_N
3808 ("both component types in a view conversion must be"
3809 & " aliased, or neither", A);
3811 -- Comment here??? what set of cases???
3813 elsif
3814 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3815 then
3816 -- Check view conv between unrelated by ref array types
3818 if Is_By_Reference_Type (Etype (F))
3819 or else Is_By_Reference_Type (Etype (Expression (A)))
3820 then
3821 Error_Msg_N
3822 ("view conversion between unrelated by reference "
3823 & "array types not allowed (\'A'I-00246)", A);
3825 -- In Ada 2005 mode, check view conversion component
3826 -- type cannot be private, tagged, or volatile. Note
3827 -- that we only apply this to source conversions. The
3828 -- generated code can contain conversions which are
3829 -- not subject to this test, and we cannot extract the
3830 -- component type in such cases since it is not present.
3832 elsif Comes_From_Source (A)
3833 and then Ada_Version >= Ada_2005
3834 then
3835 declare
3836 Comp_Type : constant Entity_Id :=
3837 Component_Type
3838 (Etype (Expression (A)));
3839 begin
3840 if (Is_Private_Type (Comp_Type)
3841 and then not Is_Generic_Type (Comp_Type))
3842 or else Is_Tagged_Type (Comp_Type)
3843 or else Is_Volatile (Comp_Type)
3844 then
3845 Error_Msg_N
3846 ("component type of a view conversion cannot"
3847 & " be private, tagged, or volatile"
3848 & " (RM 4.6 (24))",
3849 Expression (A));
3850 end if;
3851 end;
3852 end if;
3853 end if;
3854 end if;
3856 -- Resolve expression if conversion is all OK
3858 if (Conversion_OK (A)
3859 or else Valid_Conversion (A, Etype (A), Expression (A)))
3860 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3861 then
3862 Resolve (Expression (A));
3863 end if;
3865 -- If the actual is a function call that returns a limited
3866 -- unconstrained object that needs finalization, create a
3867 -- transient scope for it, so that it can receive the proper
3868 -- finalization list.
3870 elsif Nkind (A) = N_Function_Call
3871 and then Is_Limited_Record (Etype (F))
3872 and then not Is_Constrained (Etype (F))
3873 and then Expander_Active
3874 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3875 then
3876 Establish_Transient_Scope (A, Sec_Stack => False);
3877 Resolve (A, Etype (F));
3879 -- A small optimization: if one of the actuals is a concatenation
3880 -- create a block around a procedure call to recover stack space.
3881 -- This alleviates stack usage when several procedure calls in
3882 -- the same statement list use concatenation. We do not perform
3883 -- this wrapping for code statements, where the argument is a
3884 -- static string, and we want to preserve warnings involving
3885 -- sequences of such statements.
3887 elsif Nkind (A) = N_Op_Concat
3888 and then Nkind (N) = N_Procedure_Call_Statement
3889 and then Expander_Active
3890 and then
3891 not (Is_Intrinsic_Subprogram (Nam)
3892 and then Chars (Nam) = Name_Asm)
3893 and then not Static_Concatenation (A)
3894 then
3895 Establish_Transient_Scope (A, Sec_Stack => False);
3896 Resolve (A, Etype (F));
3898 else
3899 if Nkind (A) = N_Type_Conversion
3900 and then Is_Array_Type (Etype (F))
3901 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3902 and then
3903 (Is_Limited_Type (Etype (F))
3904 or else Is_Limited_Type (Etype (Expression (A))))
3905 then
3906 Error_Msg_N
3907 ("conversion between unrelated limited array types "
3908 & "not allowed ('A'I-00246)", A);
3910 if Is_Limited_Type (Etype (F)) then
3911 Explain_Limited_Type (Etype (F), A);
3912 end if;
3914 if Is_Limited_Type (Etype (Expression (A))) then
3915 Explain_Limited_Type (Etype (Expression (A)), A);
3916 end if;
3917 end if;
3919 -- (Ada 2005: AI-251): If the actual is an allocator whose
3920 -- directly designated type is a class-wide interface, we build
3921 -- an anonymous access type to use it as the type of the
3922 -- allocator. Later, when the subprogram call is expanded, if
3923 -- the interface has a secondary dispatch table the expander
3924 -- will add a type conversion to force the correct displacement
3925 -- of the pointer.
3927 if Nkind (A) = N_Allocator then
3928 declare
3929 DDT : constant Entity_Id :=
3930 Directly_Designated_Type (Base_Type (Etype (F)));
3932 New_Itype : Entity_Id;
3934 begin
3935 if Is_Class_Wide_Type (DDT)
3936 and then Is_Interface (DDT)
3937 then
3938 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3939 Set_Etype (New_Itype, Etype (A));
3940 Set_Directly_Designated_Type
3941 (New_Itype, Directly_Designated_Type (Etype (A)));
3942 Set_Etype (A, New_Itype);
3943 end if;
3945 -- Ada 2005, AI-162:If the actual is an allocator, the
3946 -- innermost enclosing statement is the master of the
3947 -- created object. This needs to be done with expansion
3948 -- enabled only, otherwise the transient scope will not
3949 -- be removed in the expansion of the wrapped construct.
3951 if (Is_Controlled (DDT) or else Has_Task (DDT))
3952 and then Expander_Active
3953 then
3954 Establish_Transient_Scope (A, Sec_Stack => False);
3955 end if;
3956 end;
3958 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3959 Check_Restriction (No_Access_Parameter_Allocators, A);
3960 end if;
3961 end if;
3963 -- (Ada 2005): The call may be to a primitive operation of a
3964 -- tagged synchronized type, declared outside of the type. In
3965 -- this case the controlling actual must be converted to its
3966 -- corresponding record type, which is the formal type. The
3967 -- actual may be a subtype, either because of a constraint or
3968 -- because it is a generic actual, so use base type to locate
3969 -- concurrent type.
3971 F_Typ := Base_Type (Etype (F));
3973 if Is_Tagged_Type (F_Typ)
3974 and then (Is_Concurrent_Type (F_Typ)
3975 or else Is_Concurrent_Record_Type (F_Typ))
3976 then
3977 -- If the actual is overloaded, look for an interpretation
3978 -- that has a synchronized type.
3980 if not Is_Overloaded (A) then
3981 A_Typ := Base_Type (Etype (A));
3983 else
3984 declare
3985 Index : Interp_Index;
3986 It : Interp;
3988 begin
3989 Get_First_Interp (A, Index, It);
3990 while Present (It.Typ) loop
3991 if Is_Concurrent_Type (It.Typ)
3992 or else Is_Concurrent_Record_Type (It.Typ)
3993 then
3994 A_Typ := Base_Type (It.Typ);
3995 exit;
3996 end if;
3998 Get_Next_Interp (Index, It);
3999 end loop;
4000 end;
4001 end if;
4003 declare
4004 Full_A_Typ : Entity_Id;
4006 begin
4007 if Present (Full_View (A_Typ)) then
4008 Full_A_Typ := Base_Type (Full_View (A_Typ));
4009 else
4010 Full_A_Typ := A_Typ;
4011 end if;
4013 -- Tagged synchronized type (case 1): the actual is a
4014 -- concurrent type.
4016 if Is_Concurrent_Type (A_Typ)
4017 and then Corresponding_Record_Type (A_Typ) = F_Typ
4018 then
4019 Rewrite (A,
4020 Unchecked_Convert_To
4021 (Corresponding_Record_Type (A_Typ), A));
4022 Resolve (A, Etype (F));
4024 -- Tagged synchronized type (case 2): the formal is a
4025 -- concurrent type.
4027 elsif Ekind (Full_A_Typ) = E_Record_Type
4028 and then Present
4029 (Corresponding_Concurrent_Type (Full_A_Typ))
4030 and then Is_Concurrent_Type (F_Typ)
4031 and then Present (Corresponding_Record_Type (F_Typ))
4032 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4033 then
4034 Resolve (A, Corresponding_Record_Type (F_Typ));
4036 -- Common case
4038 else
4039 Resolve (A, Etype (F));
4040 end if;
4041 end;
4043 -- Not a synchronized operation
4045 else
4046 Resolve (A, Etype (F));
4047 end if;
4048 end if;
4050 A_Typ := Etype (A);
4051 F_Typ := Etype (F);
4053 -- An actual cannot be an untagged formal incomplete type
4055 if Ekind (A_Typ) = E_Incomplete_Type
4056 and then not Is_Tagged_Type (A_Typ)
4057 and then Is_Generic_Type (A_Typ)
4058 then
4059 Error_Msg_N
4060 ("invalid use of untagged formal incomplete type", A);
4061 end if;
4063 if Comes_From_Source (Original_Node (N))
4064 and then Nkind_In (Original_Node (N), N_Function_Call,
4065 N_Procedure_Call_Statement)
4066 then
4067 -- In formal mode, check that actual parameters matching
4068 -- formals of tagged types are objects (or ancestor type
4069 -- conversions of objects), not general expressions.
4071 if Is_Actual_Tagged_Parameter (A) then
4072 if Is_SPARK_05_Object_Reference (A) then
4073 null;
4075 elsif Nkind (A) = N_Type_Conversion then
4076 declare
4077 Operand : constant Node_Id := Expression (A);
4078 Operand_Typ : constant Entity_Id := Etype (Operand);
4079 Target_Typ : constant Entity_Id := A_Typ;
4081 begin
4082 if not Is_SPARK_05_Object_Reference (Operand) then
4083 Check_SPARK_05_Restriction
4084 ("object required", Operand);
4086 -- In formal mode, the only view conversions are those
4087 -- involving ancestor conversion of an extended type.
4089 elsif not
4090 (Is_Tagged_Type (Target_Typ)
4091 and then not Is_Class_Wide_Type (Target_Typ)
4092 and then Is_Tagged_Type (Operand_Typ)
4093 and then not Is_Class_Wide_Type (Operand_Typ)
4094 and then Is_Ancestor (Target_Typ, Operand_Typ))
4095 then
4096 if Ekind_In
4097 (F, E_Out_Parameter, E_In_Out_Parameter)
4098 then
4099 Check_SPARK_05_Restriction
4100 ("ancestor conversion is the only permitted "
4101 & "view conversion", A);
4102 else
4103 Check_SPARK_05_Restriction
4104 ("ancestor conversion required", A);
4105 end if;
4107 else
4108 null;
4109 end if;
4110 end;
4112 else
4113 Check_SPARK_05_Restriction ("object required", A);
4114 end if;
4116 -- In formal mode, the only view conversions are those
4117 -- involving ancestor conversion of an extended type.
4119 elsif Nkind (A) = N_Type_Conversion
4120 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
4121 then
4122 Check_SPARK_05_Restriction
4123 ("ancestor conversion is the only permitted view "
4124 & "conversion", A);
4125 end if;
4126 end if;
4128 -- has warnings suppressed, then we reset Never_Set_In_Source for
4129 -- the calling entity. The reason for this is to catch cases like
4130 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4131 -- uses trickery to modify an IN parameter.
4133 if Ekind (F) = E_In_Parameter
4134 and then Is_Entity_Name (A)
4135 and then Present (Entity (A))
4136 and then Ekind (Entity (A)) = E_Variable
4137 and then Has_Warnings_Off (F_Typ)
4138 then
4139 Set_Never_Set_In_Source (Entity (A), False);
4140 end if;
4142 -- Perform error checks for IN and IN OUT parameters
4144 if Ekind (F) /= E_Out_Parameter then
4146 -- Check unset reference. For scalar parameters, it is clearly
4147 -- wrong to pass an uninitialized value as either an IN or
4148 -- IN-OUT parameter. For composites, it is also clearly an
4149 -- error to pass a completely uninitialized value as an IN
4150 -- parameter, but the case of IN OUT is trickier. We prefer
4151 -- not to give a warning here. For example, suppose there is
4152 -- a routine that sets some component of a record to False.
4153 -- It is perfectly reasonable to make this IN-OUT and allow
4154 -- either initialized or uninitialized records to be passed
4155 -- in this case.
4157 -- For partially initialized composite values, we also avoid
4158 -- warnings, since it is quite likely that we are passing a
4159 -- partially initialized value and only the initialized fields
4160 -- will in fact be read in the subprogram.
4162 if Is_Scalar_Type (A_Typ)
4163 or else (Ekind (F) = E_In_Parameter
4164 and then not Is_Partially_Initialized_Type (A_Typ))
4165 then
4166 Check_Unset_Reference (A);
4167 end if;
4169 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4170 -- actual to a nested call, since this constitutes a reading of
4171 -- the parameter, which is not allowed.
4173 if Ada_Version = Ada_83
4174 and then Is_Entity_Name (A)
4175 and then Ekind (Entity (A)) = E_Out_Parameter
4176 then
4177 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4178 end if;
4179 end if;
4181 -- Case of OUT or IN OUT parameter
4183 if Ekind (F) /= E_In_Parameter then
4185 -- For an Out parameter, check for useless assignment. Note
4186 -- that we can't set Last_Assignment this early, because we may
4187 -- kill current values in Resolve_Call, and that call would
4188 -- clobber the Last_Assignment field.
4190 -- Note: call Warn_On_Useless_Assignment before doing the check
4191 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4192 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4193 -- reflects the last assignment, not this one.
4195 if Ekind (F) = E_Out_Parameter then
4196 if Warn_On_Modified_As_Out_Parameter (F)
4197 and then Is_Entity_Name (A)
4198 and then Present (Entity (A))
4199 and then Comes_From_Source (N)
4200 then
4201 Warn_On_Useless_Assignment (Entity (A), A);
4202 end if;
4203 end if;
4205 -- Validate the form of the actual. Note that the call to
4206 -- Is_OK_Variable_For_Out_Formal generates the required
4207 -- reference in this case.
4209 -- A call to an initialization procedure for an aggregate
4210 -- component may initialize a nested component of a constant
4211 -- designated object. In this context the object is variable.
4213 if not Is_OK_Variable_For_Out_Formal (A)
4214 and then not Is_Init_Proc (Nam)
4215 then
4216 Error_Msg_NE ("actual for& must be a variable", A, F);
4218 if Is_Subprogram (Current_Scope)
4219 and then
4220 (Is_Invariant_Procedure (Current_Scope)
4221 or else Is_Predicate_Function (Current_Scope))
4222 then
4223 Error_Msg_N
4224 ("function used in predicate cannot "
4225 & "modify its argument", F);
4226 end if;
4227 end if;
4229 -- What's the following about???
4231 if Is_Entity_Name (A) then
4232 Kill_Checks (Entity (A));
4233 else
4234 Kill_All_Checks;
4235 end if;
4236 end if;
4238 if Etype (A) = Any_Type then
4239 Set_Etype (N, Any_Type);
4240 return;
4241 end if;
4243 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4245 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
4247 -- Apply predicate tests except in certain special cases. Note
4248 -- that it might be more consistent to apply these only when
4249 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4250 -- for the outbound predicate tests ???
4252 if Predicate_Tests_On_Arguments (Nam) then
4253 Apply_Predicate_Check (A, F_Typ);
4254 end if;
4256 -- Apply required constraint checks
4258 -- Gigi looks at the check flag and uses the appropriate types.
4259 -- For now since one flag is used there is an optimization
4260 -- which might not be done in the IN OUT case since Gigi does
4261 -- not do any analysis. More thought required about this ???
4263 -- In fact is this comment obsolete??? doesn't the expander now
4264 -- generate all these tests anyway???
4266 if Is_Scalar_Type (Etype (A)) then
4267 Apply_Scalar_Range_Check (A, F_Typ);
4269 elsif Is_Array_Type (Etype (A)) then
4270 Apply_Length_Check (A, F_Typ);
4272 elsif Is_Record_Type (F_Typ)
4273 and then Has_Discriminants (F_Typ)
4274 and then Is_Constrained (F_Typ)
4275 and then (not Is_Derived_Type (F_Typ)
4276 or else Comes_From_Source (Nam))
4277 then
4278 Apply_Discriminant_Check (A, F_Typ);
4280 -- For view conversions of a discriminated object, apply
4281 -- check to object itself, the conversion alreay has the
4282 -- proper type.
4284 if Nkind (A) = N_Type_Conversion
4285 and then Is_Constrained (Etype (Expression (A)))
4286 then
4287 Apply_Discriminant_Check (Expression (A), F_Typ);
4288 end if;
4290 elsif Is_Access_Type (F_Typ)
4291 and then Is_Array_Type (Designated_Type (F_Typ))
4292 and then Is_Constrained (Designated_Type (F_Typ))
4293 then
4294 Apply_Length_Check (A, F_Typ);
4296 elsif Is_Access_Type (F_Typ)
4297 and then Has_Discriminants (Designated_Type (F_Typ))
4298 and then Is_Constrained (Designated_Type (F_Typ))
4299 then
4300 Apply_Discriminant_Check (A, F_Typ);
4302 else
4303 Apply_Range_Check (A, F_Typ);
4304 end if;
4306 -- Ada 2005 (AI-231): Note that the controlling parameter case
4307 -- already existed in Ada 95, which is partially checked
4308 -- elsewhere (see Checks), and we don't want the warning
4309 -- message to differ.
4311 if Is_Access_Type (F_Typ)
4312 and then Can_Never_Be_Null (F_Typ)
4313 and then Known_Null (A)
4314 then
4315 if Is_Controlling_Formal (F) then
4316 Apply_Compile_Time_Constraint_Error
4317 (N => A,
4318 Msg => "null value not allowed here??",
4319 Reason => CE_Access_Check_Failed);
4321 elsif Ada_Version >= Ada_2005 then
4322 Apply_Compile_Time_Constraint_Error
4323 (N => A,
4324 Msg => "(Ada 2005) null not allowed in "
4325 & "null-excluding formal??",
4326 Reason => CE_Null_Not_Allowed);
4327 end if;
4328 end if;
4329 end if;
4331 -- Checks for OUT parameters and IN OUT parameters
4333 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
4335 -- If there is a type conversion, to make sure the return value
4336 -- meets the constraints of the variable before the conversion.
4338 if Nkind (A) = N_Type_Conversion then
4339 if Is_Scalar_Type (A_Typ) then
4340 Apply_Scalar_Range_Check
4341 (Expression (A), Etype (Expression (A)), A_Typ);
4342 else
4343 Apply_Range_Check
4344 (Expression (A), Etype (Expression (A)), A_Typ);
4345 end if;
4347 -- If no conversion apply scalar range checks and length checks
4348 -- base on the subtype of the actual (NOT that of the formal).
4350 else
4351 if Is_Scalar_Type (F_Typ) then
4352 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4353 elsif Is_Array_Type (F_Typ)
4354 and then Ekind (F) = E_Out_Parameter
4355 then
4356 Apply_Length_Check (A, F_Typ);
4357 else
4358 Apply_Range_Check (A, A_Typ, F_Typ);
4359 end if;
4360 end if;
4362 -- Note: we do not apply the predicate checks for the case of
4363 -- OUT and IN OUT parameters. They are instead applied in the
4364 -- Expand_Actuals routine in Exp_Ch6.
4365 end if;
4367 -- An actual associated with an access parameter is implicitly
4368 -- converted to the anonymous access type of the formal and must
4369 -- satisfy the legality checks for access conversions.
4371 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4372 if not Valid_Conversion (A, F_Typ, A) then
4373 Error_Msg_N
4374 ("invalid implicit conversion for access parameter", A);
4375 end if;
4377 -- If the actual is an access selected component of a variable,
4378 -- the call may modify its designated object. It is reasonable
4379 -- to treat this as a potential modification of the enclosing
4380 -- record, to prevent spurious warnings that it should be
4381 -- declared as a constant, because intuitively programmers
4382 -- regard the designated subcomponent as part of the record.
4384 if Nkind (A) = N_Selected_Component
4385 and then Is_Entity_Name (Prefix (A))
4386 and then not Is_Constant_Object (Entity (Prefix (A)))
4387 then
4388 Note_Possible_Modification (A, Sure => False);
4389 end if;
4390 end if;
4392 -- Check bad case of atomic/volatile argument (RM C.6(12))
4394 if Is_By_Reference_Type (Etype (F))
4395 and then Comes_From_Source (N)
4396 then
4397 if Is_Atomic_Object (A)
4398 and then not Is_Atomic (Etype (F))
4399 then
4400 Error_Msg_NE
4401 ("cannot pass atomic argument to non-atomic formal&",
4402 A, F);
4404 elsif Is_Volatile_Object (A)
4405 and then not Is_Volatile (Etype (F))
4406 then
4407 Error_Msg_NE
4408 ("cannot pass volatile argument to non-volatile formal&",
4409 A, F);
4410 end if;
4411 end if;
4413 -- Check that subprograms don't have improper controlling
4414 -- arguments (RM 3.9.2 (9)).
4416 -- A primitive operation may have an access parameter of an
4417 -- incomplete tagged type, but a dispatching call is illegal
4418 -- if the type is still incomplete.
4420 if Is_Controlling_Formal (F) then
4421 Set_Is_Controlling_Actual (A);
4423 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4424 declare
4425 Desig : constant Entity_Id := Designated_Type (Etype (F));
4426 begin
4427 if Ekind (Desig) = E_Incomplete_Type
4428 and then No (Full_View (Desig))
4429 and then No (Non_Limited_View (Desig))
4430 then
4431 Error_Msg_NE
4432 ("premature use of incomplete type& "
4433 & "in dispatching call", A, Desig);
4434 end if;
4435 end;
4436 end if;
4438 elsif Nkind (A) = N_Explicit_Dereference then
4439 Validate_Remote_Access_To_Class_Wide_Type (A);
4440 end if;
4442 -- Apply legality rule 3.9.2 (9/1)
4444 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4445 and then not Is_Class_Wide_Type (F_Typ)
4446 and then not Is_Controlling_Formal (F)
4447 and then not In_Instance
4448 then
4449 Error_Msg_N ("class-wide argument not allowed here!", A);
4451 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4452 Error_Msg_Node_2 := F_Typ;
4453 Error_Msg_NE
4454 ("& is not a dispatching operation of &!", A, Nam);
4455 end if;
4457 -- Apply the checks described in 3.10.2(27): if the context is a
4458 -- specific access-to-object, the actual cannot be class-wide.
4459 -- Use base type to exclude access_to_subprogram cases.
4461 elsif Is_Access_Type (A_Typ)
4462 and then Is_Access_Type (F_Typ)
4463 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4464 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4465 or else (Nkind (A) = N_Attribute_Reference
4466 and then
4467 Is_Class_Wide_Type (Etype (Prefix (A)))))
4468 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4469 and then not Is_Controlling_Formal (F)
4471 -- Disable these checks for call to imported C++ subprograms
4473 and then not
4474 (Is_Entity_Name (Name (N))
4475 and then Is_Imported (Entity (Name (N)))
4476 and then Convention (Entity (Name (N))) = Convention_CPP)
4477 then
4478 Error_Msg_N
4479 ("access to class-wide argument not allowed here!", A);
4481 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4482 Error_Msg_Node_2 := Designated_Type (F_Typ);
4483 Error_Msg_NE
4484 ("& is not a dispatching operation of &!", A, Nam);
4485 end if;
4486 end if;
4488 Check_Aliased_Parameter;
4490 Eval_Actual (A);
4492 -- If it is a named association, treat the selector_name as a
4493 -- proper identifier, and mark the corresponding entity.
4495 if Nkind (Parent (A)) = N_Parameter_Association
4497 -- Ignore reference in SPARK mode, as it refers to an entity not
4498 -- in scope at the point of reference, so the reference should
4499 -- be ignored for computing effects of subprograms.
4501 and then not GNATprove_Mode
4502 then
4503 -- If subprogram is overridden, use name of formal that
4504 -- is being called.
4506 if Present (Real_Subp) then
4507 Set_Entity (Selector_Name (Parent (A)), Real_F);
4508 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
4510 else
4511 Set_Entity (Selector_Name (Parent (A)), F);
4512 Generate_Reference (F, Selector_Name (Parent (A)));
4513 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4514 Generate_Reference (F_Typ, N, ' ');
4515 end if;
4516 end if;
4518 Prev := A;
4520 if Ekind (F) /= E_Out_Parameter then
4521 Check_Unset_Reference (A);
4522 end if;
4524 -- The following checks are only relevant when SPARK_Mode is on as
4525 -- they are not standard Ada legality rule. Internally generated
4526 -- temporaries are ignored.
4528 if SPARK_Mode = On and then Comes_From_Source (A) then
4530 -- An effectively volatile object may act as an actual when the
4531 -- corresponding formal is of a non-scalar effectively volatile
4532 -- type (SPARK RM 7.1.3(11)).
4534 if not Is_Scalar_Type (Etype (F))
4535 and then Is_Effectively_Volatile (Etype (F))
4536 then
4537 null;
4539 -- An effectively volatile object may act as an actual in a
4540 -- call to an instance of Unchecked_Conversion.
4541 -- (SPARK RM 7.1.3(11)).
4543 elsif Is_Unchecked_Conversion_Instance (Nam) then
4544 null;
4546 -- The actual denotes an object
4548 elsif Is_Effectively_Volatile_Object (A) then
4549 Error_Msg_N
4550 ("volatile object cannot act as actual in a call (SPARK "
4551 & "RM 7.1.3(11))", A);
4553 -- Otherwise the actual denotes an expression. Inspect the
4554 -- expression and flag each effectively volatile object with
4555 -- enabled property Async_Writers or Effective_Reads as illegal
4556 -- because it apprears within an interfering context. Note that
4557 -- this is usually done in Resolve_Entity_Name, but when the
4558 -- effectively volatile object appears as an actual in a call,
4559 -- the call must be resolved first.
4561 else
4562 Flag_Effectively_Volatile_Objects (A);
4563 end if;
4565 -- Detect an external variable with an enabled property that
4566 -- does not match the mode of the corresponding formal in a
4567 -- procedure call. Functions are not considered because they
4568 -- cannot have effectively volatile formal parameters in the
4569 -- first place.
4571 if Ekind (Nam) = E_Procedure
4572 and then Ekind (F) = E_In_Parameter
4573 and then Is_Entity_Name (A)
4574 and then Present (Entity (A))
4575 and then Ekind (Entity (A)) = E_Variable
4576 then
4577 A_Id := Entity (A);
4579 if Async_Readers_Enabled (A_Id) then
4580 Property_Error (A, A_Id, Name_Async_Readers);
4581 elsif Effective_Reads_Enabled (A_Id) then
4582 Property_Error (A, A_Id, Name_Effective_Reads);
4583 elsif Effective_Writes_Enabled (A_Id) then
4584 Property_Error (A, A_Id, Name_Effective_Writes);
4585 end if;
4586 end if;
4587 end if;
4589 -- A formal parameter of a specific tagged type whose related
4590 -- subprogram is subject to pragma Extensions_Visible with value
4591 -- "False" cannot act as an actual in a subprogram with value
4592 -- "True" (SPARK RM 6.1.7(3)).
4594 if Is_EVF_Expression (A)
4595 and then Extensions_Visible_Status (Nam) =
4596 Extensions_Visible_True
4597 then
4598 Error_Msg_N
4599 ("formal parameter cannot act as actual parameter when "
4600 & "Extensions_Visible is False", A);
4601 Error_Msg_NE
4602 ("\subprogram & has Extensions_Visible True", A, Nam);
4603 end if;
4605 -- The actual parameter of a Ghost subprogram whose formal is of
4606 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
4608 if Comes_From_Source (Nam)
4609 and then Is_Ghost_Entity (Nam)
4610 and then Ekind_In (F, E_In_Out_Parameter, E_Out_Parameter)
4611 and then Is_Entity_Name (A)
4612 and then Present (Entity (A))
4613 and then not Is_Ghost_Entity (Entity (A))
4614 then
4615 Error_Msg_NE
4616 ("non-ghost variable & cannot appear as actual in call to "
4617 & "ghost procedure", A, Entity (A));
4619 if Ekind (F) = E_In_Out_Parameter then
4620 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
4621 else
4622 Error_Msg_N ("\corresponding formal has mode OUT", A);
4623 end if;
4624 end if;
4626 Next_Actual (A);
4628 -- Case where actual is not present
4630 else
4631 Insert_Default;
4632 end if;
4634 Next_Formal (F);
4636 if Present (Real_Subp) then
4637 Next_Formal (Real_F);
4638 end if;
4639 end loop;
4640 end Resolve_Actuals;
4642 -----------------------
4643 -- Resolve_Allocator --
4644 -----------------------
4646 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4647 Desig_T : constant Entity_Id := Designated_Type (Typ);
4648 E : constant Node_Id := Expression (N);
4649 Subtyp : Entity_Id;
4650 Discrim : Entity_Id;
4651 Constr : Node_Id;
4652 Aggr : Node_Id;
4653 Assoc : Node_Id := Empty;
4654 Disc_Exp : Node_Id;
4656 procedure Check_Allocator_Discrim_Accessibility
4657 (Disc_Exp : Node_Id;
4658 Alloc_Typ : Entity_Id);
4659 -- Check that accessibility level associated with an access discriminant
4660 -- initialized in an allocator by the expression Disc_Exp is not deeper
4661 -- than the level of the allocator type Alloc_Typ. An error message is
4662 -- issued if this condition is violated. Specialized checks are done for
4663 -- the cases of a constraint expression which is an access attribute or
4664 -- an access discriminant.
4666 function In_Dispatching_Context return Boolean;
4667 -- If the allocator is an actual in a call, it is allowed to be class-
4668 -- wide when the context is not because it is a controlling actual.
4670 -------------------------------------------
4671 -- Check_Allocator_Discrim_Accessibility --
4672 -------------------------------------------
4674 procedure Check_Allocator_Discrim_Accessibility
4675 (Disc_Exp : Node_Id;
4676 Alloc_Typ : Entity_Id)
4678 begin
4679 if Type_Access_Level (Etype (Disc_Exp)) >
4680 Deepest_Type_Access_Level (Alloc_Typ)
4681 then
4682 Error_Msg_N
4683 ("operand type has deeper level than allocator type", Disc_Exp);
4685 -- When the expression is an Access attribute the level of the prefix
4686 -- object must not be deeper than that of the allocator's type.
4688 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4689 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4690 Attribute_Access
4691 and then Object_Access_Level (Prefix (Disc_Exp)) >
4692 Deepest_Type_Access_Level (Alloc_Typ)
4693 then
4694 Error_Msg_N
4695 ("prefix of attribute has deeper level than allocator type",
4696 Disc_Exp);
4698 -- When the expression is an access discriminant the check is against
4699 -- the level of the prefix object.
4701 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4702 and then Nkind (Disc_Exp) = N_Selected_Component
4703 and then Object_Access_Level (Prefix (Disc_Exp)) >
4704 Deepest_Type_Access_Level (Alloc_Typ)
4705 then
4706 Error_Msg_N
4707 ("access discriminant has deeper level than allocator type",
4708 Disc_Exp);
4710 -- All other cases are legal
4712 else
4713 null;
4714 end if;
4715 end Check_Allocator_Discrim_Accessibility;
4717 ----------------------------
4718 -- In_Dispatching_Context --
4719 ----------------------------
4721 function In_Dispatching_Context return Boolean is
4722 Par : constant Node_Id := Parent (N);
4724 begin
4725 return Nkind (Par) in N_Subprogram_Call
4726 and then Is_Entity_Name (Name (Par))
4727 and then Is_Dispatching_Operation (Entity (Name (Par)));
4728 end In_Dispatching_Context;
4730 -- Start of processing for Resolve_Allocator
4732 begin
4733 -- Replace general access with specific type
4735 if Ekind (Etype (N)) = E_Allocator_Type then
4736 Set_Etype (N, Base_Type (Typ));
4737 end if;
4739 if Is_Abstract_Type (Typ) then
4740 Error_Msg_N ("type of allocator cannot be abstract", N);
4741 end if;
4743 -- For qualified expression, resolve the expression using the given
4744 -- subtype (nothing to do for type mark, subtype indication)
4746 if Nkind (E) = N_Qualified_Expression then
4747 if Is_Class_Wide_Type (Etype (E))
4748 and then not Is_Class_Wide_Type (Desig_T)
4749 and then not In_Dispatching_Context
4750 then
4751 Error_Msg_N
4752 ("class-wide allocator not allowed for this access type", N);
4753 end if;
4755 Resolve (Expression (E), Etype (E));
4756 Check_Non_Static_Context (Expression (E));
4757 Check_Unset_Reference (Expression (E));
4759 -- Allocators generated by the build-in-place expansion mechanism
4760 -- are explicitly marked as coming from source but do not need to be
4761 -- checked for limited initialization. To exclude this case, ensure
4762 -- that the parent of the allocator is a source node.
4764 if Is_Limited_Type (Etype (E))
4765 and then Comes_From_Source (N)
4766 and then Comes_From_Source (Parent (N))
4767 and then not In_Instance_Body
4768 then
4769 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
4770 if Nkind (Parent (N)) = N_Assignment_Statement then
4771 Error_Msg_N
4772 ("illegal expression for initialized allocator of a "
4773 & "limited type (RM 7.5 (2.7/2))", N);
4774 else
4775 Error_Msg_N
4776 ("initialization not allowed for limited types", N);
4777 end if;
4779 Explain_Limited_Type (Etype (E), N);
4780 end if;
4781 end if;
4783 -- A qualified expression requires an exact match of the type. Class-
4784 -- wide matching is not allowed.
4786 if (Is_Class_Wide_Type (Etype (Expression (E)))
4787 or else Is_Class_Wide_Type (Etype (E)))
4788 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4789 then
4790 Wrong_Type (Expression (E), Etype (E));
4791 end if;
4793 -- Calls to build-in-place functions are not currently supported in
4794 -- allocators for access types associated with a simple storage pool.
4795 -- Supporting such allocators may require passing additional implicit
4796 -- parameters to build-in-place functions (or a significant revision
4797 -- of the current b-i-p implementation to unify the handling for
4798 -- multiple kinds of storage pools). ???
4800 if Is_Limited_View (Desig_T)
4801 and then Nkind (Expression (E)) = N_Function_Call
4802 then
4803 declare
4804 Pool : constant Entity_Id :=
4805 Associated_Storage_Pool (Root_Type (Typ));
4806 begin
4807 if Present (Pool)
4808 and then
4809 Present (Get_Rep_Pragma
4810 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4811 then
4812 Error_Msg_N
4813 ("limited function calls not yet supported in simple "
4814 & "storage pool allocators", Expression (E));
4815 end if;
4816 end;
4817 end if;
4819 -- A special accessibility check is needed for allocators that
4820 -- constrain access discriminants. The level of the type of the
4821 -- expression used to constrain an access discriminant cannot be
4822 -- deeper than the type of the allocator (in contrast to access
4823 -- parameters, where the level of the actual can be arbitrary).
4825 -- We can't use Valid_Conversion to perform this check because in
4826 -- general the type of the allocator is unrelated to the type of
4827 -- the access discriminant.
4829 if Ekind (Typ) /= E_Anonymous_Access_Type
4830 or else Is_Local_Anonymous_Access (Typ)
4831 then
4832 Subtyp := Entity (Subtype_Mark (E));
4834 Aggr := Original_Node (Expression (E));
4836 if Has_Discriminants (Subtyp)
4837 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4838 then
4839 Discrim := First_Discriminant (Base_Type (Subtyp));
4841 -- Get the first component expression of the aggregate
4843 if Present (Expressions (Aggr)) then
4844 Disc_Exp := First (Expressions (Aggr));
4846 elsif Present (Component_Associations (Aggr)) then
4847 Assoc := First (Component_Associations (Aggr));
4849 if Present (Assoc) then
4850 Disc_Exp := Expression (Assoc);
4851 else
4852 Disc_Exp := Empty;
4853 end if;
4855 else
4856 Disc_Exp := Empty;
4857 end if;
4859 while Present (Discrim) and then Present (Disc_Exp) loop
4860 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4861 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4862 end if;
4864 Next_Discriminant (Discrim);
4866 if Present (Discrim) then
4867 if Present (Assoc) then
4868 Next (Assoc);
4869 Disc_Exp := Expression (Assoc);
4871 elsif Present (Next (Disc_Exp)) then
4872 Next (Disc_Exp);
4874 else
4875 Assoc := First (Component_Associations (Aggr));
4877 if Present (Assoc) then
4878 Disc_Exp := Expression (Assoc);
4879 else
4880 Disc_Exp := Empty;
4881 end if;
4882 end if;
4883 end if;
4884 end loop;
4885 end if;
4886 end if;
4888 -- For a subtype mark or subtype indication, freeze the subtype
4890 else
4891 Freeze_Expression (E);
4893 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4894 Error_Msg_N
4895 ("initialization required for access-to-constant allocator", N);
4896 end if;
4898 -- A special accessibility check is needed for allocators that
4899 -- constrain access discriminants. The level of the type of the
4900 -- expression used to constrain an access discriminant cannot be
4901 -- deeper than the type of the allocator (in contrast to access
4902 -- parameters, where the level of the actual can be arbitrary).
4903 -- We can't use Valid_Conversion to perform this check because
4904 -- in general the type of the allocator is unrelated to the type
4905 -- of the access discriminant.
4907 if Nkind (Original_Node (E)) = N_Subtype_Indication
4908 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4909 or else Is_Local_Anonymous_Access (Typ))
4910 then
4911 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4913 if Has_Discriminants (Subtyp) then
4914 Discrim := First_Discriminant (Base_Type (Subtyp));
4915 Constr := First (Constraints (Constraint (Original_Node (E))));
4916 while Present (Discrim) and then Present (Constr) loop
4917 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4918 if Nkind (Constr) = N_Discriminant_Association then
4919 Disc_Exp := Original_Node (Expression (Constr));
4920 else
4921 Disc_Exp := Original_Node (Constr);
4922 end if;
4924 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4925 end if;
4927 Next_Discriminant (Discrim);
4928 Next (Constr);
4929 end loop;
4930 end if;
4931 end if;
4932 end if;
4934 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4935 -- check that the level of the type of the created object is not deeper
4936 -- than the level of the allocator's access type, since extensions can
4937 -- now occur at deeper levels than their ancestor types. This is a
4938 -- static accessibility level check; a run-time check is also needed in
4939 -- the case of an initialized allocator with a class-wide argument (see
4940 -- Expand_Allocator_Expression).
4942 if Ada_Version >= Ada_2005
4943 and then Is_Class_Wide_Type (Desig_T)
4944 then
4945 declare
4946 Exp_Typ : Entity_Id;
4948 begin
4949 if Nkind (E) = N_Qualified_Expression then
4950 Exp_Typ := Etype (E);
4951 elsif Nkind (E) = N_Subtype_Indication then
4952 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4953 else
4954 Exp_Typ := Entity (E);
4955 end if;
4957 if Type_Access_Level (Exp_Typ) >
4958 Deepest_Type_Access_Level (Typ)
4959 then
4960 if In_Instance_Body then
4961 Error_Msg_Warn := SPARK_Mode /= On;
4962 Error_Msg_N
4963 ("type in allocator has deeper level than "
4964 & "designated class-wide type<<", E);
4965 Error_Msg_N ("\Program_Error [<<", E);
4966 Rewrite (N,
4967 Make_Raise_Program_Error (Sloc (N),
4968 Reason => PE_Accessibility_Check_Failed));
4969 Set_Etype (N, Typ);
4971 -- Do not apply Ada 2005 accessibility checks on a class-wide
4972 -- allocator if the type given in the allocator is a formal
4973 -- type. A run-time check will be performed in the instance.
4975 elsif not Is_Generic_Type (Exp_Typ) then
4976 Error_Msg_N ("type in allocator has deeper level than "
4977 & "designated class-wide type", E);
4978 end if;
4979 end if;
4980 end;
4981 end if;
4983 -- Check for allocation from an empty storage pool
4985 if No_Pool_Assigned (Typ) then
4986 Error_Msg_N ("allocation from empty storage pool!", N);
4988 -- If the context is an unchecked conversion, as may happen within an
4989 -- inlined subprogram, the allocator is being resolved with its own
4990 -- anonymous type. In that case, if the target type has a specific
4991 -- storage pool, it must be inherited explicitly by the allocator type.
4993 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4994 and then No (Associated_Storage_Pool (Typ))
4995 then
4996 Set_Associated_Storage_Pool
4997 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4998 end if;
5000 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5001 Check_Restriction (No_Anonymous_Allocators, N);
5002 end if;
5004 -- Check that an allocator with task parts isn't for a nested access
5005 -- type when restriction No_Task_Hierarchy applies.
5007 if not Is_Library_Level_Entity (Base_Type (Typ))
5008 and then Has_Task (Base_Type (Desig_T))
5009 then
5010 Check_Restriction (No_Task_Hierarchy, N);
5011 end if;
5013 -- An illegal allocator may be rewritten as a raise Program_Error
5014 -- statement.
5016 if Nkind (N) = N_Allocator then
5018 -- An anonymous access discriminant is the definition of a
5019 -- coextension.
5021 if Ekind (Typ) = E_Anonymous_Access_Type
5022 and then Nkind (Associated_Node_For_Itype (Typ)) =
5023 N_Discriminant_Specification
5024 then
5025 declare
5026 Discr : constant Entity_Id :=
5027 Defining_Identifier (Associated_Node_For_Itype (Typ));
5029 begin
5030 Check_Restriction (No_Coextensions, N);
5032 -- Ada 2012 AI05-0052: If the designated type of the allocator
5033 -- is limited, then the allocator shall not be used to define
5034 -- the value of an access discriminant unless the discriminated
5035 -- type is immutably limited.
5037 if Ada_Version >= Ada_2012
5038 and then Is_Limited_Type (Desig_T)
5039 and then not Is_Limited_View (Scope (Discr))
5040 then
5041 Error_Msg_N
5042 ("only immutably limited types can have anonymous "
5043 & "access discriminants designating a limited type", N);
5044 end if;
5045 end;
5047 -- Avoid marking an allocator as a dynamic coextension if it is
5048 -- within a static construct.
5050 if not Is_Static_Coextension (N) then
5051 Set_Is_Dynamic_Coextension (N);
5052 end if;
5054 -- Cleanup for potential static coextensions
5056 else
5057 Set_Is_Dynamic_Coextension (N, False);
5058 Set_Is_Static_Coextension (N, False);
5059 end if;
5060 end if;
5062 -- Report a simple error: if the designated object is a local task,
5063 -- its body has not been seen yet, and its activation will fail an
5064 -- elaboration check.
5066 if Is_Task_Type (Desig_T)
5067 and then Scope (Base_Type (Desig_T)) = Current_Scope
5068 and then Is_Compilation_Unit (Current_Scope)
5069 and then Ekind (Current_Scope) = E_Package
5070 and then not In_Package_Body (Current_Scope)
5071 then
5072 Error_Msg_Warn := SPARK_Mode /= On;
5073 Error_Msg_N ("cannot activate task before body seen<<", N);
5074 Error_Msg_N ("\Program_Error [<<", N);
5075 end if;
5077 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5078 -- type with a task component on a subpool. This action must raise
5079 -- Program_Error at runtime.
5081 if Ada_Version >= Ada_2012
5082 and then Nkind (N) = N_Allocator
5083 and then Present (Subpool_Handle_Name (N))
5084 and then Has_Task (Desig_T)
5085 then
5086 Error_Msg_Warn := SPARK_Mode /= On;
5087 Error_Msg_N ("cannot allocate task on subpool<<", N);
5088 Error_Msg_N ("\Program_Error [<<", N);
5090 Rewrite (N,
5091 Make_Raise_Program_Error (Sloc (N),
5092 Reason => PE_Explicit_Raise));
5093 Set_Etype (N, Typ);
5094 end if;
5095 end Resolve_Allocator;
5097 ---------------------------
5098 -- Resolve_Arithmetic_Op --
5099 ---------------------------
5101 -- Used for resolving all arithmetic operators except exponentiation
5103 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5104 L : constant Node_Id := Left_Opnd (N);
5105 R : constant Node_Id := Right_Opnd (N);
5106 TL : constant Entity_Id := Base_Type (Etype (L));
5107 TR : constant Entity_Id := Base_Type (Etype (R));
5108 T : Entity_Id;
5109 Rop : Node_Id;
5111 B_Typ : constant Entity_Id := Base_Type (Typ);
5112 -- We do the resolution using the base type, because intermediate values
5113 -- in expressions always are of the base type, not a subtype of it.
5115 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5116 -- Returns True if N is in a context that expects "any real type"
5118 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5119 -- Return True iff given type is Integer or universal real/integer
5121 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5122 -- Choose type of integer literal in fixed-point operation to conform
5123 -- to available fixed-point type. T is the type of the other operand,
5124 -- which is needed to determine the expected type of N.
5126 procedure Set_Operand_Type (N : Node_Id);
5127 -- Set operand type to T if universal
5129 -------------------------------
5130 -- Expected_Type_Is_Any_Real --
5131 -------------------------------
5133 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5134 begin
5135 -- N is the expression after "delta" in a fixed_point_definition;
5136 -- see RM-3.5.9(6):
5138 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
5139 N_Decimal_Fixed_Point_Definition,
5141 -- N is one of the bounds in a real_range_specification;
5142 -- see RM-3.5.7(5):
5144 N_Real_Range_Specification,
5146 -- N is the expression of a delta_constraint;
5147 -- see RM-J.3(3):
5149 N_Delta_Constraint);
5150 end Expected_Type_Is_Any_Real;
5152 -----------------------------
5153 -- Is_Integer_Or_Universal --
5154 -----------------------------
5156 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5157 T : Entity_Id;
5158 Index : Interp_Index;
5159 It : Interp;
5161 begin
5162 if not Is_Overloaded (N) then
5163 T := Etype (N);
5164 return Base_Type (T) = Base_Type (Standard_Integer)
5165 or else T = Universal_Integer
5166 or else T = Universal_Real;
5167 else
5168 Get_First_Interp (N, Index, It);
5169 while Present (It.Typ) loop
5170 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5171 or else It.Typ = Universal_Integer
5172 or else It.Typ = Universal_Real
5173 then
5174 return True;
5175 end if;
5177 Get_Next_Interp (Index, It);
5178 end loop;
5179 end if;
5181 return False;
5182 end Is_Integer_Or_Universal;
5184 ----------------------------
5185 -- Set_Mixed_Mode_Operand --
5186 ----------------------------
5188 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5189 Index : Interp_Index;
5190 It : Interp;
5192 begin
5193 if Universal_Interpretation (N) = Universal_Integer then
5195 -- A universal integer literal is resolved as standard integer
5196 -- except in the case of a fixed-point result, where we leave it
5197 -- as universal (to be handled by Exp_Fixd later on)
5199 if Is_Fixed_Point_Type (T) then
5200 Resolve (N, Universal_Integer);
5201 else
5202 Resolve (N, Standard_Integer);
5203 end if;
5205 elsif Universal_Interpretation (N) = Universal_Real
5206 and then (T = Base_Type (Standard_Integer)
5207 or else T = Universal_Integer
5208 or else T = Universal_Real)
5209 then
5210 -- A universal real can appear in a fixed-type context. We resolve
5211 -- the literal with that context, even though this might raise an
5212 -- exception prematurely (the other operand may be zero).
5214 Resolve (N, B_Typ);
5216 elsif Etype (N) = Base_Type (Standard_Integer)
5217 and then T = Universal_Real
5218 and then Is_Overloaded (N)
5219 then
5220 -- Integer arg in mixed-mode operation. Resolve with universal
5221 -- type, in case preference rule must be applied.
5223 Resolve (N, Universal_Integer);
5225 elsif Etype (N) = T
5226 and then B_Typ /= Universal_Fixed
5227 then
5228 -- Not a mixed-mode operation, resolve with context
5230 Resolve (N, B_Typ);
5232 elsif Etype (N) = Any_Fixed then
5234 -- N may itself be a mixed-mode operation, so use context type
5236 Resolve (N, B_Typ);
5238 elsif Is_Fixed_Point_Type (T)
5239 and then B_Typ = Universal_Fixed
5240 and then Is_Overloaded (N)
5241 then
5242 -- Must be (fixed * fixed) operation, operand must have one
5243 -- compatible interpretation.
5245 Resolve (N, Any_Fixed);
5247 elsif Is_Fixed_Point_Type (B_Typ)
5248 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5249 and then Is_Overloaded (N)
5250 then
5251 -- C * F(X) in a fixed context, where C is a real literal or a
5252 -- fixed-point expression. F must have either a fixed type
5253 -- interpretation or an integer interpretation, but not both.
5255 Get_First_Interp (N, Index, It);
5256 while Present (It.Typ) loop
5257 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5258 if Analyzed (N) then
5259 Error_Msg_N ("ambiguous operand in fixed operation", N);
5260 else
5261 Resolve (N, Standard_Integer);
5262 end if;
5264 elsif Is_Fixed_Point_Type (It.Typ) then
5265 if Analyzed (N) then
5266 Error_Msg_N ("ambiguous operand in fixed operation", N);
5267 else
5268 Resolve (N, It.Typ);
5269 end if;
5270 end if;
5272 Get_Next_Interp (Index, It);
5273 end loop;
5275 -- Reanalyze the literal with the fixed type of the context. If
5276 -- context is Universal_Fixed, we are within a conversion, leave
5277 -- the literal as a universal real because there is no usable
5278 -- fixed type, and the target of the conversion plays no role in
5279 -- the resolution.
5281 declare
5282 Op2 : Node_Id;
5283 T2 : Entity_Id;
5285 begin
5286 if N = L then
5287 Op2 := R;
5288 else
5289 Op2 := L;
5290 end if;
5292 if B_Typ = Universal_Fixed
5293 and then Nkind (Op2) = N_Real_Literal
5294 then
5295 T2 := Universal_Real;
5296 else
5297 T2 := B_Typ;
5298 end if;
5300 Set_Analyzed (Op2, False);
5301 Resolve (Op2, T2);
5302 end;
5304 else
5305 Resolve (N);
5306 end if;
5307 end Set_Mixed_Mode_Operand;
5309 ----------------------
5310 -- Set_Operand_Type --
5311 ----------------------
5313 procedure Set_Operand_Type (N : Node_Id) is
5314 begin
5315 if Etype (N) = Universal_Integer
5316 or else Etype (N) = Universal_Real
5317 then
5318 Set_Etype (N, T);
5319 end if;
5320 end Set_Operand_Type;
5322 -- Start of processing for Resolve_Arithmetic_Op
5324 begin
5325 if Comes_From_Source (N)
5326 and then Ekind (Entity (N)) = E_Function
5327 and then Is_Imported (Entity (N))
5328 and then Is_Intrinsic_Subprogram (Entity (N))
5329 then
5330 Resolve_Intrinsic_Operator (N, Typ);
5331 return;
5333 -- Special-case for mixed-mode universal expressions or fixed point type
5334 -- operation: each argument is resolved separately. The same treatment
5335 -- is required if one of the operands of a fixed point operation is
5336 -- universal real, since in this case we don't do a conversion to a
5337 -- specific fixed-point type (instead the expander handles the case).
5339 -- Set the type of the node to its universal interpretation because
5340 -- legality checks on an exponentiation operand need the context.
5342 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
5343 and then Present (Universal_Interpretation (L))
5344 and then Present (Universal_Interpretation (R))
5345 then
5346 Set_Etype (N, B_Typ);
5347 Resolve (L, Universal_Interpretation (L));
5348 Resolve (R, Universal_Interpretation (R));
5350 elsif (B_Typ = Universal_Real
5351 or else Etype (N) = Universal_Fixed
5352 or else (Etype (N) = Any_Fixed
5353 and then Is_Fixed_Point_Type (B_Typ))
5354 or else (Is_Fixed_Point_Type (B_Typ)
5355 and then (Is_Integer_Or_Universal (L)
5356 or else
5357 Is_Integer_Or_Universal (R))))
5358 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5359 then
5360 if TL = Universal_Integer or else TR = Universal_Integer then
5361 Check_For_Visible_Operator (N, B_Typ);
5362 end if;
5364 -- If context is a fixed type and one operand is integer, the other
5365 -- is resolved with the type of the context.
5367 if Is_Fixed_Point_Type (B_Typ)
5368 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5369 or else TL = Universal_Integer)
5370 then
5371 Resolve (R, B_Typ);
5372 Resolve (L, TL);
5374 elsif Is_Fixed_Point_Type (B_Typ)
5375 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5376 or else TR = Universal_Integer)
5377 then
5378 Resolve (L, B_Typ);
5379 Resolve (R, TR);
5381 else
5382 Set_Mixed_Mode_Operand (L, TR);
5383 Set_Mixed_Mode_Operand (R, TL);
5384 end if;
5386 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
5387 -- multiplying operators from being used when the expected type is
5388 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
5389 -- some cases where the expected type is actually Any_Real;
5390 -- Expected_Type_Is_Any_Real takes care of that case.
5392 if Etype (N) = Universal_Fixed
5393 or else Etype (N) = Any_Fixed
5394 then
5395 if B_Typ = Universal_Fixed
5396 and then not Expected_Type_Is_Any_Real (N)
5397 and then not Nkind_In (Parent (N), N_Type_Conversion,
5398 N_Unchecked_Type_Conversion)
5399 then
5400 Error_Msg_N ("type cannot be determined from context!", N);
5401 Error_Msg_N ("\explicit conversion to result type required", N);
5403 Set_Etype (L, Any_Type);
5404 Set_Etype (R, Any_Type);
5406 else
5407 if Ada_Version = Ada_83
5408 and then Etype (N) = Universal_Fixed
5409 and then not
5410 Nkind_In (Parent (N), N_Type_Conversion,
5411 N_Unchecked_Type_Conversion)
5412 then
5413 Error_Msg_N
5414 ("(Ada 83) fixed-point operation needs explicit "
5415 & "conversion", N);
5416 end if;
5418 -- The expected type is "any real type" in contexts like
5420 -- type T is delta <universal_fixed-expression> ...
5422 -- in which case we need to set the type to Universal_Real
5423 -- so that static expression evaluation will work properly.
5425 if Expected_Type_Is_Any_Real (N) then
5426 Set_Etype (N, Universal_Real);
5427 else
5428 Set_Etype (N, B_Typ);
5429 end if;
5430 end if;
5432 elsif Is_Fixed_Point_Type (B_Typ)
5433 and then (Is_Integer_Or_Universal (L)
5434 or else Nkind (L) = N_Real_Literal
5435 or else Nkind (R) = N_Real_Literal
5436 or else Is_Integer_Or_Universal (R))
5437 then
5438 Set_Etype (N, B_Typ);
5440 elsif Etype (N) = Any_Fixed then
5442 -- If no previous errors, this is only possible if one operand is
5443 -- overloaded and the context is universal. Resolve as such.
5445 Set_Etype (N, B_Typ);
5446 end if;
5448 else
5449 if (TL = Universal_Integer or else TL = Universal_Real)
5450 and then
5451 (TR = Universal_Integer or else TR = Universal_Real)
5452 then
5453 Check_For_Visible_Operator (N, B_Typ);
5454 end if;
5456 -- If the context is Universal_Fixed and the operands are also
5457 -- universal fixed, this is an error, unless there is only one
5458 -- applicable fixed_point type (usually Duration).
5460 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
5461 T := Unique_Fixed_Point_Type (N);
5463 if T = Any_Type then
5464 Set_Etype (N, T);
5465 return;
5466 else
5467 Resolve (L, T);
5468 Resolve (R, T);
5469 end if;
5471 else
5472 Resolve (L, B_Typ);
5473 Resolve (R, B_Typ);
5474 end if;
5476 -- If one of the arguments was resolved to a non-universal type.
5477 -- label the result of the operation itself with the same type.
5478 -- Do the same for the universal argument, if any.
5480 T := Intersect_Types (L, R);
5481 Set_Etype (N, Base_Type (T));
5482 Set_Operand_Type (L);
5483 Set_Operand_Type (R);
5484 end if;
5486 Generate_Operator_Reference (N, Typ);
5487 Analyze_Dimension (N);
5488 Eval_Arithmetic_Op (N);
5490 -- In SPARK, a multiplication or division with operands of fixed point
5491 -- types must be qualified or explicitly converted to identify the
5492 -- result type.
5494 if (Is_Fixed_Point_Type (Etype (L))
5495 or else Is_Fixed_Point_Type (Etype (R)))
5496 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5497 and then
5498 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
5499 then
5500 Check_SPARK_05_Restriction
5501 ("operation should be qualified or explicitly converted", N);
5502 end if;
5504 -- Set overflow and division checking bit
5506 if Nkind (N) in N_Op then
5507 if not Overflow_Checks_Suppressed (Etype (N)) then
5508 Enable_Overflow_Check (N);
5509 end if;
5511 -- Give warning if explicit division by zero
5513 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
5514 and then not Division_Checks_Suppressed (Etype (N))
5515 then
5516 Rop := Right_Opnd (N);
5518 if Compile_Time_Known_Value (Rop)
5519 and then ((Is_Integer_Type (Etype (Rop))
5520 and then Expr_Value (Rop) = Uint_0)
5521 or else
5522 (Is_Real_Type (Etype (Rop))
5523 and then Expr_Value_R (Rop) = Ureal_0))
5524 then
5525 -- Specialize the warning message according to the operation.
5526 -- When SPARK_Mode is On, force a warning instead of an error
5527 -- in that case, as this likely corresponds to deactivated
5528 -- code. The following warnings are for the case
5530 case Nkind (N) is
5531 when N_Op_Divide =>
5533 -- For division, we have two cases, for float division
5534 -- of an unconstrained float type, on a machine where
5535 -- Machine_Overflows is false, we don't get an exception
5536 -- at run-time, but rather an infinity or Nan. The Nan
5537 -- case is pretty obscure, so just warn about infinities.
5539 if Is_Floating_Point_Type (Typ)
5540 and then not Is_Constrained (Typ)
5541 and then not Machine_Overflows_On_Target
5542 then
5543 Error_Msg_N
5544 ("float division by zero, may generate "
5545 & "'+'/'- infinity??", Right_Opnd (N));
5547 -- For all other cases, we get a Constraint_Error
5549 else
5550 Apply_Compile_Time_Constraint_Error
5551 (N, "division by zero??", CE_Divide_By_Zero,
5552 Loc => Sloc (Right_Opnd (N)),
5553 Warn => SPARK_Mode = On);
5554 end if;
5556 when N_Op_Rem =>
5557 Apply_Compile_Time_Constraint_Error
5558 (N, "rem with zero divisor??", CE_Divide_By_Zero,
5559 Loc => Sloc (Right_Opnd (N)),
5560 Warn => SPARK_Mode = On);
5562 when N_Op_Mod =>
5563 Apply_Compile_Time_Constraint_Error
5564 (N, "mod with zero divisor??", CE_Divide_By_Zero,
5565 Loc => Sloc (Right_Opnd (N)),
5566 Warn => SPARK_Mode = On);
5568 -- Division by zero can only happen with division, rem,
5569 -- and mod operations.
5571 when others =>
5572 raise Program_Error;
5573 end case;
5575 -- In GNATprove mode, we enable the division check so that
5576 -- GNATprove will issue a message if it cannot be proved.
5578 if GNATprove_Mode then
5579 Activate_Division_Check (N);
5580 end if;
5582 -- Otherwise just set the flag to check at run time
5584 else
5585 Activate_Division_Check (N);
5586 end if;
5587 end if;
5589 -- If Restriction No_Implicit_Conditionals is active, then it is
5590 -- violated if either operand can be negative for mod, or for rem
5591 -- if both operands can be negative.
5593 if Restriction_Check_Required (No_Implicit_Conditionals)
5594 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5595 then
5596 declare
5597 Lo : Uint;
5598 Hi : Uint;
5599 OK : Boolean;
5601 LNeg : Boolean;
5602 RNeg : Boolean;
5603 -- Set if corresponding operand might be negative
5605 begin
5606 Determine_Range
5607 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5608 LNeg := (not OK) or else Lo < 0;
5610 Determine_Range
5611 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5612 RNeg := (not OK) or else Lo < 0;
5614 -- Check if we will be generating conditionals. There are two
5615 -- cases where that can happen, first for REM, the only case
5616 -- is largest negative integer mod -1, where the division can
5617 -- overflow, but we still have to give the right result. The
5618 -- front end generates a test for this annoying case. Here we
5619 -- just test if both operands can be negative (that's what the
5620 -- expander does, so we match its logic here).
5622 -- The second case is mod where either operand can be negative.
5623 -- In this case, the back end has to generate additional tests.
5625 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5626 or else
5627 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5628 then
5629 Check_Restriction (No_Implicit_Conditionals, N);
5630 end if;
5631 end;
5632 end if;
5633 end if;
5635 Check_Unset_Reference (L);
5636 Check_Unset_Reference (R);
5637 end Resolve_Arithmetic_Op;
5639 ------------------
5640 -- Resolve_Call --
5641 ------------------
5643 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5644 function Same_Or_Aliased_Subprograms
5645 (S : Entity_Id;
5646 E : Entity_Id) return Boolean;
5647 -- Returns True if the subprogram entity S is the same as E or else
5648 -- S is an alias of E.
5650 ---------------------------------
5651 -- Same_Or_Aliased_Subprograms --
5652 ---------------------------------
5654 function Same_Or_Aliased_Subprograms
5655 (S : Entity_Id;
5656 E : Entity_Id) return Boolean
5658 Subp_Alias : constant Entity_Id := Alias (S);
5659 begin
5660 return S = E or else (Present (Subp_Alias) and then Subp_Alias = E);
5661 end Same_Or_Aliased_Subprograms;
5663 -- Local variables
5665 Loc : constant Source_Ptr := Sloc (N);
5666 Subp : constant Node_Id := Name (N);
5667 Body_Id : Entity_Id;
5668 I : Interp_Index;
5669 It : Interp;
5670 Nam : Entity_Id;
5671 Nam_Decl : Node_Id;
5672 Nam_UA : Entity_Id;
5673 Norm_OK : Boolean;
5674 Rtype : Entity_Id;
5675 Scop : Entity_Id;
5677 -- Start of processing for Resolve_Call
5679 begin
5680 -- The context imposes a unique interpretation with type Typ on a
5681 -- procedure or function call. Find the entity of the subprogram that
5682 -- yields the expected type, and propagate the corresponding formal
5683 -- constraints on the actuals. The caller has established that an
5684 -- interpretation exists, and emitted an error if not unique.
5686 -- First deal with the case of a call to an access-to-subprogram,
5687 -- dereference made explicit in Analyze_Call.
5689 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5690 if not Is_Overloaded (Subp) then
5691 Nam := Etype (Subp);
5693 else
5694 -- Find the interpretation whose type (a subprogram type) has a
5695 -- return type that is compatible with the context. Analysis of
5696 -- the node has established that one exists.
5698 Nam := Empty;
5700 Get_First_Interp (Subp, I, It);
5701 while Present (It.Typ) loop
5702 if Covers (Typ, Etype (It.Typ)) then
5703 Nam := It.Typ;
5704 exit;
5705 end if;
5707 Get_Next_Interp (I, It);
5708 end loop;
5710 if No (Nam) then
5711 raise Program_Error;
5712 end if;
5713 end if;
5715 -- If the prefix is not an entity, then resolve it
5717 if not Is_Entity_Name (Subp) then
5718 Resolve (Subp, Nam);
5719 end if;
5721 -- For an indirect call, we always invalidate checks, since we do not
5722 -- know whether the subprogram is local or global. Yes we could do
5723 -- better here, e.g. by knowing that there are no local subprograms,
5724 -- but it does not seem worth the effort. Similarly, we kill all
5725 -- knowledge of current constant values.
5727 Kill_Current_Values;
5729 -- If this is a procedure call which is really an entry call, do
5730 -- the conversion of the procedure call to an entry call. Protected
5731 -- operations use the same circuitry because the name in the call
5732 -- can be an arbitrary expression with special resolution rules.
5734 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5735 or else (Is_Entity_Name (Subp)
5736 and then Ekind (Entity (Subp)) = E_Entry)
5737 then
5738 Resolve_Entry_Call (N, Typ);
5739 Check_Elab_Call (N);
5741 -- Kill checks and constant values, as above for indirect case
5742 -- Who knows what happens when another task is activated?
5744 Kill_Current_Values;
5745 return;
5747 -- Normal subprogram call with name established in Resolve
5749 elsif not (Is_Type (Entity (Subp))) then
5750 Nam := Entity (Subp);
5751 Set_Entity_With_Checks (Subp, Nam);
5753 -- Otherwise we must have the case of an overloaded call
5755 else
5756 pragma Assert (Is_Overloaded (Subp));
5758 -- Initialize Nam to prevent warning (we know it will be assigned
5759 -- in the loop below, but the compiler does not know that).
5761 Nam := Empty;
5763 Get_First_Interp (Subp, I, It);
5764 while Present (It.Typ) loop
5765 if Covers (Typ, It.Typ) then
5766 Nam := It.Nam;
5767 Set_Entity_With_Checks (Subp, Nam);
5768 exit;
5769 end if;
5771 Get_Next_Interp (I, It);
5772 end loop;
5773 end if;
5775 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5776 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5777 and then Nkind (Subp) /= N_Explicit_Dereference
5778 and then Present (Parameter_Associations (N))
5779 then
5780 -- The prefix is a parameterless function call that returns an access
5781 -- to subprogram. If parameters are present in the current call, add
5782 -- add an explicit dereference. We use the base type here because
5783 -- within an instance these may be subtypes.
5785 -- The dereference is added either in Analyze_Call or here. Should
5786 -- be consolidated ???
5788 Set_Is_Overloaded (Subp, False);
5789 Set_Etype (Subp, Etype (Nam));
5790 Insert_Explicit_Dereference (Subp);
5791 Nam := Designated_Type (Etype (Nam));
5792 Resolve (Subp, Nam);
5793 end if;
5795 -- Check that a call to Current_Task does not occur in an entry body
5797 if Is_RTE (Nam, RE_Current_Task) then
5798 declare
5799 P : Node_Id;
5801 begin
5802 P := N;
5803 loop
5804 P := Parent (P);
5806 -- Exclude calls that occur within the default of a formal
5807 -- parameter of the entry, since those are evaluated outside
5808 -- of the body.
5810 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5812 if Nkind (P) = N_Entry_Body
5813 or else (Nkind (P) = N_Subprogram_Body
5814 and then Is_Entry_Barrier_Function (P))
5815 then
5816 Rtype := Etype (N);
5817 Error_Msg_Warn := SPARK_Mode /= On;
5818 Error_Msg_NE
5819 ("& should not be used in entry body (RM C.7(17))<<",
5820 N, Nam);
5821 Error_Msg_NE ("\Program_Error [<<", N, Nam);
5822 Rewrite (N,
5823 Make_Raise_Program_Error (Loc,
5824 Reason => PE_Current_Task_In_Entry_Body));
5825 Set_Etype (N, Rtype);
5826 return;
5827 end if;
5828 end loop;
5829 end;
5830 end if;
5832 -- Check that a procedure call does not occur in the context of the
5833 -- entry call statement of a conditional or timed entry call. Note that
5834 -- the case of a call to a subprogram renaming of an entry will also be
5835 -- rejected. The test for N not being an N_Entry_Call_Statement is
5836 -- defensive, covering the possibility that the processing of entry
5837 -- calls might reach this point due to later modifications of the code
5838 -- above.
5840 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5841 and then Nkind (N) /= N_Entry_Call_Statement
5842 and then Entry_Call_Statement (Parent (N)) = N
5843 then
5844 if Ada_Version < Ada_2005 then
5845 Error_Msg_N ("entry call required in select statement", N);
5847 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5848 -- for a procedure_or_entry_call, the procedure_name or
5849 -- procedure_prefix of the procedure_call_statement shall denote
5850 -- an entry renamed by a procedure, or (a view of) a primitive
5851 -- subprogram of a limited interface whose first parameter is
5852 -- a controlling parameter.
5854 elsif Nkind (N) = N_Procedure_Call_Statement
5855 and then not Is_Renamed_Entry (Nam)
5856 and then not Is_Controlling_Limited_Procedure (Nam)
5857 then
5858 Error_Msg_N
5859 ("entry call or dispatching primitive of interface required", N);
5860 end if;
5861 end if;
5863 -- If the SPARK_05 restriction is active, we are not allowed
5864 -- to have a call to a subprogram before we see its completion.
5866 if not Has_Completion (Nam)
5867 and then Restriction_Check_Required (SPARK_05)
5869 -- Don't flag strange internal calls
5871 and then Comes_From_Source (N)
5872 and then Comes_From_Source (Nam)
5874 -- Only flag calls in extended main source
5876 and then In_Extended_Main_Source_Unit (Nam)
5877 and then In_Extended_Main_Source_Unit (N)
5879 -- Exclude enumeration literals from this processing
5881 and then Ekind (Nam) /= E_Enumeration_Literal
5882 then
5883 Check_SPARK_05_Restriction
5884 ("call to subprogram cannot appear before its body", N);
5885 end if;
5887 -- Check that this is not a call to a protected procedure or entry from
5888 -- within a protected function.
5890 Check_Internal_Protected_Use (N, Nam);
5892 -- Freeze the subprogram name if not in a spec-expression. Note that
5893 -- we freeze procedure calls as well as function calls. Procedure calls
5894 -- are not frozen according to the rules (RM 13.14(14)) because it is
5895 -- impossible to have a procedure call to a non-frozen procedure in
5896 -- pure Ada, but in the code that we generate in the expander, this
5897 -- rule needs extending because we can generate procedure calls that
5898 -- need freezing.
5900 -- In Ada 2012, expression functions may be called within pre/post
5901 -- conditions of subsequent functions or expression functions. Such
5902 -- calls do not freeze when they appear within generated bodies,
5903 -- (including the body of another expression function) which would
5904 -- place the freeze node in the wrong scope. An expression function
5905 -- is frozen in the usual fashion, by the appearance of a real body,
5906 -- or at the end of a declarative part.
5908 if Is_Entity_Name (Subp)
5909 and then not In_Spec_Expression
5910 and then not Is_Expression_Function_Or_Completion (Current_Scope)
5911 and then
5912 (not Is_Expression_Function_Or_Completion (Entity (Subp))
5913 or else Scope (Entity (Subp)) = Current_Scope)
5914 then
5915 Freeze_Expression (Subp);
5916 end if;
5918 -- For a predefined operator, the type of the result is the type imposed
5919 -- by context, except for a predefined operation on universal fixed.
5920 -- Otherwise The type of the call is the type returned by the subprogram
5921 -- being called.
5923 if Is_Predefined_Op (Nam) then
5924 if Etype (N) /= Universal_Fixed then
5925 Set_Etype (N, Typ);
5926 end if;
5928 -- If the subprogram returns an array type, and the context requires the
5929 -- component type of that array type, the node is really an indexing of
5930 -- the parameterless call. Resolve as such. A pathological case occurs
5931 -- when the type of the component is an access to the array type. In
5932 -- this case the call is truly ambiguous.
5934 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5935 and then
5936 ((Is_Array_Type (Etype (Nam))
5937 and then Covers (Typ, Component_Type (Etype (Nam))))
5938 or else
5939 (Is_Access_Type (Etype (Nam))
5940 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5941 and then
5942 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))))
5943 then
5944 declare
5945 Index_Node : Node_Id;
5946 New_Subp : Node_Id;
5947 Ret_Type : constant Entity_Id := Etype (Nam);
5949 begin
5950 if Is_Access_Type (Ret_Type)
5951 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5952 then
5953 Error_Msg_N
5954 ("cannot disambiguate function call and indexing", N);
5955 else
5956 New_Subp := Relocate_Node (Subp);
5958 -- The called entity may be an explicit dereference, in which
5959 -- case there is no entity to set.
5961 if Nkind (New_Subp) /= N_Explicit_Dereference then
5962 Set_Entity (Subp, Nam);
5963 end if;
5965 if (Is_Array_Type (Ret_Type)
5966 and then Component_Type (Ret_Type) /= Any_Type)
5967 or else
5968 (Is_Access_Type (Ret_Type)
5969 and then
5970 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5971 then
5972 if Needs_No_Actuals (Nam) then
5974 -- Indexed call to a parameterless function
5976 Index_Node :=
5977 Make_Indexed_Component (Loc,
5978 Prefix =>
5979 Make_Function_Call (Loc, Name => New_Subp),
5980 Expressions => Parameter_Associations (N));
5981 else
5982 -- An Ada 2005 prefixed call to a primitive operation
5983 -- whose first parameter is the prefix. This prefix was
5984 -- prepended to the parameter list, which is actually a
5985 -- list of indexes. Remove the prefix in order to build
5986 -- the proper indexed component.
5988 Index_Node :=
5989 Make_Indexed_Component (Loc,
5990 Prefix =>
5991 Make_Function_Call (Loc,
5992 Name => New_Subp,
5993 Parameter_Associations =>
5994 New_List
5995 (Remove_Head (Parameter_Associations (N)))),
5996 Expressions => Parameter_Associations (N));
5997 end if;
5999 -- Preserve the parenthesis count of the node
6001 Set_Paren_Count (Index_Node, Paren_Count (N));
6003 -- Since we are correcting a node classification error made
6004 -- by the parser, we call Replace rather than Rewrite.
6006 Replace (N, Index_Node);
6008 Set_Etype (Prefix (N), Ret_Type);
6009 Set_Etype (N, Typ);
6010 Resolve_Indexed_Component (N, Typ);
6011 Check_Elab_Call (Prefix (N));
6012 end if;
6013 end if;
6015 return;
6016 end;
6018 else
6019 Set_Etype (N, Etype (Nam));
6020 end if;
6022 -- In the case where the call is to an overloaded subprogram, Analyze
6023 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6024 -- such a case Normalize_Actuals needs to be called once more to order
6025 -- the actuals correctly. Otherwise the call will have the ordering
6026 -- given by the last overloaded subprogram whether this is the correct
6027 -- one being called or not.
6029 if Is_Overloaded (Subp) then
6030 Normalize_Actuals (N, Nam, False, Norm_OK);
6031 pragma Assert (Norm_OK);
6032 end if;
6034 -- In any case, call is fully resolved now. Reset Overload flag, to
6035 -- prevent subsequent overload resolution if node is analyzed again
6037 Set_Is_Overloaded (Subp, False);
6038 Set_Is_Overloaded (N, False);
6040 -- A Ghost entity must appear in a specific context
6042 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6043 Check_Ghost_Context (Nam, N);
6044 end if;
6046 -- If we are calling the current subprogram from immediately within its
6047 -- body, then that is the case where we can sometimes detect cases of
6048 -- infinite recursion statically. Do not try this in case restriction
6049 -- No_Recursion is in effect anyway, and do it only for source calls.
6051 if Comes_From_Source (N) then
6052 Scop := Current_Scope;
6054 -- Check violation of SPARK_05 restriction which does not permit
6055 -- a subprogram body to contain a call to the subprogram directly.
6057 if Restriction_Check_Required (SPARK_05)
6058 and then Same_Or_Aliased_Subprograms (Nam, Scop)
6059 then
6060 Check_SPARK_05_Restriction
6061 ("subprogram may not contain direct call to itself", N);
6062 end if;
6064 -- Issue warning for possible infinite recursion in the absence
6065 -- of the No_Recursion restriction.
6067 if Same_Or_Aliased_Subprograms (Nam, Scop)
6068 and then not Restriction_Active (No_Recursion)
6069 and then Check_Infinite_Recursion (N)
6070 then
6071 -- Here we detected and flagged an infinite recursion, so we do
6072 -- not need to test the case below for further warnings. Also we
6073 -- are all done if we now have a raise SE node.
6075 if Nkind (N) = N_Raise_Storage_Error then
6076 return;
6077 end if;
6079 -- If call is to immediately containing subprogram, then check for
6080 -- the case of a possible run-time detectable infinite recursion.
6082 else
6083 Scope_Loop : while Scop /= Standard_Standard loop
6084 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6086 -- Although in general case, recursion is not statically
6087 -- checkable, the case of calling an immediately containing
6088 -- subprogram is easy to catch.
6090 Check_Restriction (No_Recursion, N);
6092 -- If the recursive call is to a parameterless subprogram,
6093 -- then even if we can't statically detect infinite
6094 -- recursion, this is pretty suspicious, and we output a
6095 -- warning. Furthermore, we will try later to detect some
6096 -- cases here at run time by expanding checking code (see
6097 -- Detect_Infinite_Recursion in package Exp_Ch6).
6099 -- If the recursive call is within a handler, do not emit a
6100 -- warning, because this is a common idiom: loop until input
6101 -- is correct, catch illegal input in handler and restart.
6103 if No (First_Formal (Nam))
6104 and then Etype (Nam) = Standard_Void_Type
6105 and then not Error_Posted (N)
6106 and then Nkind (Parent (N)) /= N_Exception_Handler
6107 then
6108 -- For the case of a procedure call. We give the message
6109 -- only if the call is the first statement in a sequence
6110 -- of statements, or if all previous statements are
6111 -- simple assignments. This is simply a heuristic to
6112 -- decrease false positives, without losing too many good
6113 -- warnings. The idea is that these previous statements
6114 -- may affect global variables the procedure depends on.
6115 -- We also exclude raise statements, that may arise from
6116 -- constraint checks and are probably unrelated to the
6117 -- intended control flow.
6119 if Nkind (N) = N_Procedure_Call_Statement
6120 and then Is_List_Member (N)
6121 then
6122 declare
6123 P : Node_Id;
6124 begin
6125 P := Prev (N);
6126 while Present (P) loop
6127 if not Nkind_In (P, N_Assignment_Statement,
6128 N_Raise_Constraint_Error)
6129 then
6130 exit Scope_Loop;
6131 end if;
6133 Prev (P);
6134 end loop;
6135 end;
6136 end if;
6138 -- Do not give warning if we are in a conditional context
6140 declare
6141 K : constant Node_Kind := Nkind (Parent (N));
6142 begin
6143 if (K = N_Loop_Statement
6144 and then Present (Iteration_Scheme (Parent (N))))
6145 or else K = N_If_Statement
6146 or else K = N_Elsif_Part
6147 or else K = N_Case_Statement_Alternative
6148 then
6149 exit Scope_Loop;
6150 end if;
6151 end;
6153 -- Here warning is to be issued
6155 Set_Has_Recursive_Call (Nam);
6156 Error_Msg_Warn := SPARK_Mode /= On;
6157 Error_Msg_N ("possible infinite recursion<<!", N);
6158 Error_Msg_N ("\Storage_Error ]<<!", N);
6159 end if;
6161 exit Scope_Loop;
6162 end if;
6164 Scop := Scope (Scop);
6165 end loop Scope_Loop;
6166 end if;
6167 end if;
6169 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6171 Check_Obsolescent_2005_Entity (Nam, Subp);
6173 -- If subprogram name is a predefined operator, it was given in
6174 -- functional notation. Replace call node with operator node, so
6175 -- that actuals can be resolved appropriately.
6177 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
6178 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
6179 return;
6181 elsif Present (Alias (Nam))
6182 and then Is_Predefined_Op (Alias (Nam))
6183 then
6184 Resolve_Actuals (N, Nam);
6185 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6186 return;
6187 end if;
6189 -- Create a transient scope if the resulting type requires it
6191 -- There are several notable exceptions:
6193 -- a) In init procs, the transient scope overhead is not needed, and is
6194 -- even incorrect when the call is a nested initialization call for a
6195 -- component whose expansion may generate adjust calls. However, if the
6196 -- call is some other procedure call within an initialization procedure
6197 -- (for example a call to Create_Task in the init_proc of the task
6198 -- run-time record) a transient scope must be created around this call.
6200 -- b) Enumeration literal pseudo-calls need no transient scope
6202 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
6203 -- functions) do not use the secondary stack even though the return
6204 -- type may be unconstrained.
6206 -- d) Calls to a build-in-place function, since such functions may
6207 -- allocate their result directly in a target object, and cases where
6208 -- the result does get allocated in the secondary stack are checked for
6209 -- within the specialized Exp_Ch6 procedures for expanding those
6210 -- build-in-place calls.
6212 -- e) If the subprogram is marked Inline_Always, then even if it returns
6213 -- an unconstrained type the call does not require use of the secondary
6214 -- stack. However, inlining will only take place if the body to inline
6215 -- is already present. It may not be available if e.g. the subprogram is
6216 -- declared in a child instance.
6218 -- If this is an initialization call for a type whose construction
6219 -- uses the secondary stack, and it is not a nested call to initialize
6220 -- a component, we do need to create a transient scope for it. We
6221 -- check for this by traversing the type in Check_Initialization_Call.
6223 if Is_Inlined (Nam)
6224 and then Has_Pragma_Inline (Nam)
6225 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
6226 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
6227 then
6228 null;
6230 elsif Ekind (Nam) = E_Enumeration_Literal
6231 or else Is_Build_In_Place_Function (Nam)
6232 or else Is_Intrinsic_Subprogram (Nam)
6233 then
6234 null;
6236 elsif Expander_Active
6237 and then Is_Type (Etype (Nam))
6238 and then Requires_Transient_Scope (Etype (Nam))
6239 and then
6240 (not Within_Init_Proc
6241 or else
6242 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
6243 then
6244 Establish_Transient_Scope (N, Sec_Stack => True);
6246 -- If the call appears within the bounds of a loop, it will
6247 -- be rewritten and reanalyzed, nothing left to do here.
6249 if Nkind (N) /= N_Function_Call then
6250 return;
6251 end if;
6253 elsif Is_Init_Proc (Nam)
6254 and then not Within_Init_Proc
6255 then
6256 Check_Initialization_Call (N, Nam);
6257 end if;
6259 -- A protected function cannot be called within the definition of the
6260 -- enclosing protected type, unless it is part of a pre/postcondition
6261 -- on another protected operation.
6263 if Is_Protected_Type (Scope (Nam))
6264 and then In_Open_Scopes (Scope (Nam))
6265 and then not Has_Completion (Scope (Nam))
6266 and then not In_Spec_Expression
6267 then
6268 Error_Msg_NE
6269 ("& cannot be called before end of protected definition", N, Nam);
6270 end if;
6272 -- Propagate interpretation to actuals, and add default expressions
6273 -- where needed.
6275 if Present (First_Formal (Nam)) then
6276 Resolve_Actuals (N, Nam);
6278 -- Overloaded literals are rewritten as function calls, for purpose of
6279 -- resolution. After resolution, we can replace the call with the
6280 -- literal itself.
6282 elsif Ekind (Nam) = E_Enumeration_Literal then
6283 Copy_Node (Subp, N);
6284 Resolve_Entity_Name (N, Typ);
6286 -- Avoid validation, since it is a static function call
6288 Generate_Reference (Nam, Subp);
6289 return;
6290 end if;
6292 -- If the subprogram is not global, then kill all saved values and
6293 -- checks. This is a bit conservative, since in many cases we could do
6294 -- better, but it is not worth the effort. Similarly, we kill constant
6295 -- values. However we do not need to do this for internal entities
6296 -- (unless they are inherited user-defined subprograms), since they
6297 -- are not in the business of molesting local values.
6299 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6300 -- kill all checks and values for calls to global subprograms. This
6301 -- takes care of the case where an access to a local subprogram is
6302 -- taken, and could be passed directly or indirectly and then called
6303 -- from almost any context.
6305 -- Note: we do not do this step till after resolving the actuals. That
6306 -- way we still take advantage of the current value information while
6307 -- scanning the actuals.
6309 -- We suppress killing values if we are processing the nodes associated
6310 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6311 -- type kills all the values as part of analyzing the code that
6312 -- initializes the dispatch tables.
6314 if Inside_Freezing_Actions = 0
6315 and then (not Is_Library_Level_Entity (Nam)
6316 or else Suppress_Value_Tracking_On_Call
6317 (Nearest_Dynamic_Scope (Current_Scope)))
6318 and then (Comes_From_Source (Nam)
6319 or else (Present (Alias (Nam))
6320 and then Comes_From_Source (Alias (Nam))))
6321 then
6322 Kill_Current_Values;
6323 end if;
6325 -- If we are warning about unread OUT parameters, this is the place to
6326 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
6327 -- after the above call to Kill_Current_Values (since that call clears
6328 -- the Last_Assignment field of all local variables).
6330 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
6331 and then Comes_From_Source (N)
6332 and then In_Extended_Main_Source_Unit (N)
6333 then
6334 declare
6335 F : Entity_Id;
6336 A : Node_Id;
6338 begin
6339 F := First_Formal (Nam);
6340 A := First_Actual (N);
6341 while Present (F) and then Present (A) loop
6342 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
6343 and then Warn_On_Modified_As_Out_Parameter (F)
6344 and then Is_Entity_Name (A)
6345 and then Present (Entity (A))
6346 and then Comes_From_Source (N)
6347 and then Safe_To_Capture_Value (N, Entity (A))
6348 then
6349 Set_Last_Assignment (Entity (A), A);
6350 end if;
6352 Next_Formal (F);
6353 Next_Actual (A);
6354 end loop;
6355 end;
6356 end if;
6358 -- If the subprogram is a primitive operation, check whether or not
6359 -- it is a correct dispatching call.
6361 if Is_Overloadable (Nam)
6362 and then Is_Dispatching_Operation (Nam)
6363 then
6364 Check_Dispatching_Call (N);
6366 elsif Ekind (Nam) /= E_Subprogram_Type
6367 and then Is_Abstract_Subprogram (Nam)
6368 and then not In_Instance
6369 then
6370 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
6371 end if;
6373 -- If this is a dispatching call, generate the appropriate reference,
6374 -- for better source navigation in GPS.
6376 if Is_Overloadable (Nam)
6377 and then Present (Controlling_Argument (N))
6378 then
6379 Generate_Reference (Nam, Subp, 'R');
6381 -- Normal case, not a dispatching call: generate a call reference
6383 else
6384 Generate_Reference (Nam, Subp, 's');
6385 end if;
6387 if Is_Intrinsic_Subprogram (Nam) then
6388 Check_Intrinsic_Call (N);
6389 end if;
6391 -- Check for violation of restriction No_Specific_Termination_Handlers
6392 -- and warn on a potentially blocking call to Abort_Task.
6394 if Restriction_Check_Required (No_Specific_Termination_Handlers)
6395 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
6396 or else
6397 Is_RTE (Nam, RE_Specific_Handler))
6398 then
6399 Check_Restriction (No_Specific_Termination_Handlers, N);
6401 elsif Is_RTE (Nam, RE_Abort_Task) then
6402 Check_Potentially_Blocking_Operation (N);
6403 end if;
6405 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
6406 -- timing event violates restriction No_Relative_Delay (AI-0211). We
6407 -- need to check the second argument to determine whether it is an
6408 -- absolute or relative timing event.
6410 if Restriction_Check_Required (No_Relative_Delay)
6411 and then Is_RTE (Nam, RE_Set_Handler)
6412 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
6413 then
6414 Check_Restriction (No_Relative_Delay, N);
6415 end if;
6417 -- Issue an error for a call to an eliminated subprogram. This routine
6418 -- will not perform the check if the call appears within a default
6419 -- expression.
6421 Check_For_Eliminated_Subprogram (Subp, Nam);
6423 -- In formal mode, the primitive operations of a tagged type or type
6424 -- extension do not include functions that return the tagged type.
6426 if Nkind (N) = N_Function_Call
6427 and then Is_Tagged_Type (Etype (N))
6428 and then Is_Entity_Name (Name (N))
6429 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
6430 then
6431 Check_SPARK_05_Restriction ("function not inherited", N);
6432 end if;
6434 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
6435 -- class-wide and the call dispatches on result in a context that does
6436 -- not provide a tag, the call raises Program_Error.
6438 if Nkind (N) = N_Function_Call
6439 and then In_Instance
6440 and then Is_Generic_Actual_Type (Typ)
6441 and then Is_Class_Wide_Type (Typ)
6442 and then Has_Controlling_Result (Nam)
6443 and then Nkind (Parent (N)) = N_Object_Declaration
6444 then
6445 -- Verify that none of the formals are controlling
6447 declare
6448 Call_OK : Boolean := False;
6449 F : Entity_Id;
6451 begin
6452 F := First_Formal (Nam);
6453 while Present (F) loop
6454 if Is_Controlling_Formal (F) then
6455 Call_OK := True;
6456 exit;
6457 end if;
6459 Next_Formal (F);
6460 end loop;
6462 if not Call_OK then
6463 Error_Msg_Warn := SPARK_Mode /= On;
6464 Error_Msg_N ("!cannot determine tag of result<<", N);
6465 Error_Msg_N ("\Program_Error [<<!", N);
6466 Insert_Action (N,
6467 Make_Raise_Program_Error (Sloc (N),
6468 Reason => PE_Explicit_Raise));
6469 end if;
6470 end;
6471 end if;
6473 -- Check for calling a function with OUT or IN OUT parameter when the
6474 -- calling context (us right now) is not Ada 2012, so does not allow
6475 -- OUT or IN OUT parameters in function calls. Functions declared in
6476 -- a predefined unit are OK, as they may be called indirectly from a
6477 -- user-declared instantiation.
6479 if Ada_Version < Ada_2012
6480 and then Ekind (Nam) = E_Function
6481 and then Has_Out_Or_In_Out_Parameter (Nam)
6482 and then not In_Predefined_Unit (Nam)
6483 then
6484 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
6485 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
6486 end if;
6488 -- Check the dimensions of the actuals in the call. For function calls,
6489 -- propagate the dimensions from the returned type to N.
6491 Analyze_Dimension_Call (N, Nam);
6493 -- All done, evaluate call and deal with elaboration issues
6495 Eval_Call (N);
6496 Check_Elab_Call (N);
6498 -- In GNATprove mode, expansion is disabled, but we want to inline some
6499 -- subprograms to facilitate formal verification. Indirect calls through
6500 -- a subprogram type or within a generic cannot be inlined. Inlining is
6501 -- performed only for calls subject to SPARK_Mode on.
6503 if GNATprove_Mode
6504 and then SPARK_Mode = On
6505 and then Is_Overloadable (Nam)
6506 and then not Inside_A_Generic
6507 then
6508 Nam_UA := Ultimate_Alias (Nam);
6509 Nam_Decl := Unit_Declaration_Node (Nam_UA);
6511 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
6512 Body_Id := Corresponding_Body (Nam_Decl);
6514 -- Nothing to do if the subprogram is not eligible for inlining in
6515 -- GNATprove mode.
6517 if not Is_Inlined_Always (Nam_UA)
6518 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
6519 then
6520 null;
6522 -- Calls cannot be inlined inside assertions, as GNATprove treats
6523 -- assertions as logic expressions.
6525 elsif In_Assertion_Expr /= 0 then
6526 Cannot_Inline
6527 ("cannot inline & (in assertion expression)?", N, Nam_UA);
6529 -- Calls cannot be inlined inside default expressions
6531 elsif In_Default_Expr then
6532 Cannot_Inline
6533 ("cannot inline & (in default expression)?", N, Nam_UA);
6535 -- Inlining should not be performed during pre-analysis
6537 elsif Full_Analysis then
6539 -- With the one-pass inlining technique, a call cannot be
6540 -- inlined if the corresponding body has not been seen yet.
6542 if No (Body_Id) then
6543 Cannot_Inline
6544 ("cannot inline & (body not seen yet)?", N, Nam_UA);
6546 -- Nothing to do if there is no body to inline, indicating that
6547 -- the subprogram is not suitable for inlining in GNATprove
6548 -- mode.
6550 elsif No (Body_To_Inline (Nam_Decl)) then
6551 null;
6553 -- Do not inline calls inside expression functions, as this
6554 -- would prevent interpreting them as logical formulas in
6555 -- GNATprove.
6557 elsif Present (Current_Subprogram)
6558 and then
6559 Is_Expression_Function_Or_Completion (Current_Subprogram)
6560 then
6561 Cannot_Inline
6562 ("cannot inline & (inside expression function)?",
6563 N, Nam_UA);
6565 -- Calls cannot be inlined inside potentially unevaluated
6566 -- expressions, as this would create complex actions inside
6567 -- expressions, that are not handled by GNATprove.
6569 elsif Is_Potentially_Unevaluated (N) then
6570 Cannot_Inline
6571 ("cannot inline & (in potentially unevaluated context)?",
6572 N, Nam_UA);
6574 -- Otherwise, inline the call
6576 else
6577 Expand_Inlined_Call (N, Nam_UA, Nam);
6578 end if;
6579 end if;
6580 end if;
6581 end if;
6583 Warn_On_Overlapping_Actuals (Nam, N);
6584 end Resolve_Call;
6586 -----------------------------
6587 -- Resolve_Case_Expression --
6588 -----------------------------
6590 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
6591 Alt : Node_Id;
6592 Alt_Expr : Node_Id;
6593 Alt_Typ : Entity_Id;
6594 Is_Dyn : Boolean;
6596 begin
6597 Alt := First (Alternatives (N));
6598 while Present (Alt) loop
6599 Alt_Expr := Expression (Alt);
6600 Resolve (Alt_Expr, Typ);
6601 Alt_Typ := Etype (Alt_Expr);
6603 -- When the expression is of a scalar subtype different from the
6604 -- result subtype, then insert a conversion to ensure the generation
6605 -- of a constraint check.
6607 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
6608 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
6609 Analyze_And_Resolve (Alt_Expr, Typ);
6610 end if;
6612 Next (Alt);
6613 end loop;
6615 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
6616 -- dynamically tagged must be known statically.
6618 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
6619 Alt := First (Alternatives (N));
6620 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
6622 while Present (Alt) loop
6623 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
6624 Error_Msg_N
6625 ("all or none of the dependent expressions can be "
6626 & "dynamically tagged", N);
6627 end if;
6629 Next (Alt);
6630 end loop;
6631 end if;
6633 Set_Etype (N, Typ);
6634 Eval_Case_Expression (N);
6635 end Resolve_Case_Expression;
6637 -------------------------------
6638 -- Resolve_Character_Literal --
6639 -------------------------------
6641 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
6642 B_Typ : constant Entity_Id := Base_Type (Typ);
6643 C : Entity_Id;
6645 begin
6646 -- Verify that the character does belong to the type of the context
6648 Set_Etype (N, B_Typ);
6649 Eval_Character_Literal (N);
6651 -- Wide_Wide_Character literals must always be defined, since the set
6652 -- of wide wide character literals is complete, i.e. if a character
6653 -- literal is accepted by the parser, then it is OK for wide wide
6654 -- character (out of range character literals are rejected).
6656 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6657 return;
6659 -- Always accept character literal for type Any_Character, which
6660 -- occurs in error situations and in comparisons of literals, both
6661 -- of which should accept all literals.
6663 elsif B_Typ = Any_Character then
6664 return;
6666 -- For Standard.Character or a type derived from it, check that the
6667 -- literal is in range.
6669 elsif Root_Type (B_Typ) = Standard_Character then
6670 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6671 return;
6672 end if;
6674 -- For Standard.Wide_Character or a type derived from it, check that the
6675 -- literal is in range.
6677 elsif Root_Type (B_Typ) = Standard_Wide_Character then
6678 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6679 return;
6680 end if;
6682 -- For Standard.Wide_Wide_Character or a type derived from it, we
6683 -- know the literal is in range, since the parser checked.
6685 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6686 return;
6688 -- If the entity is already set, this has already been resolved in a
6689 -- generic context, or comes from expansion. Nothing else to do.
6691 elsif Present (Entity (N)) then
6692 return;
6694 -- Otherwise we have a user defined character type, and we can use the
6695 -- standard visibility mechanisms to locate the referenced entity.
6697 else
6698 C := Current_Entity (N);
6699 while Present (C) loop
6700 if Etype (C) = B_Typ then
6701 Set_Entity_With_Checks (N, C);
6702 Generate_Reference (C, N);
6703 return;
6704 end if;
6706 C := Homonym (C);
6707 end loop;
6708 end if;
6710 -- If we fall through, then the literal does not match any of the
6711 -- entries of the enumeration type. This isn't just a constraint error
6712 -- situation, it is an illegality (see RM 4.2).
6714 Error_Msg_NE
6715 ("character not defined for }", N, First_Subtype (B_Typ));
6716 end Resolve_Character_Literal;
6718 ---------------------------
6719 -- Resolve_Comparison_Op --
6720 ---------------------------
6722 -- Context requires a boolean type, and plays no role in resolution.
6723 -- Processing identical to that for equality operators. The result type is
6724 -- the base type, which matters when pathological subtypes of booleans with
6725 -- limited ranges are used.
6727 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6728 L : constant Node_Id := Left_Opnd (N);
6729 R : constant Node_Id := Right_Opnd (N);
6730 T : Entity_Id;
6732 begin
6733 -- If this is an intrinsic operation which is not predefined, use the
6734 -- types of its declared arguments to resolve the possibly overloaded
6735 -- operands. Otherwise the operands are unambiguous and specify the
6736 -- expected type.
6738 if Scope (Entity (N)) /= Standard_Standard then
6739 T := Etype (First_Entity (Entity (N)));
6741 else
6742 T := Find_Unique_Type (L, R);
6744 if T = Any_Fixed then
6745 T := Unique_Fixed_Point_Type (L);
6746 end if;
6747 end if;
6749 Set_Etype (N, Base_Type (Typ));
6750 Generate_Reference (T, N, ' ');
6752 -- Skip remaining processing if already set to Any_Type
6754 if T = Any_Type then
6755 return;
6756 end if;
6758 -- Deal with other error cases
6760 if T = Any_String or else
6761 T = Any_Composite or else
6762 T = Any_Character
6763 then
6764 if T = Any_Character then
6765 Ambiguous_Character (L);
6766 else
6767 Error_Msg_N ("ambiguous operands for comparison", N);
6768 end if;
6770 Set_Etype (N, Any_Type);
6771 return;
6772 end if;
6774 -- Resolve the operands if types OK
6776 Resolve (L, T);
6777 Resolve (R, T);
6778 Check_Unset_Reference (L);
6779 Check_Unset_Reference (R);
6780 Generate_Operator_Reference (N, T);
6781 Check_Low_Bound_Tested (N);
6783 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6784 -- types or array types except String.
6786 if Is_Boolean_Type (T) then
6787 Check_SPARK_05_Restriction
6788 ("comparison is not defined on Boolean type", N);
6790 elsif Is_Array_Type (T)
6791 and then Base_Type (T) /= Standard_String
6792 then
6793 Check_SPARK_05_Restriction
6794 ("comparison is not defined on array types other than String", N);
6795 end if;
6797 -- Check comparison on unordered enumeration
6799 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
6800 Error_Msg_Sloc := Sloc (Etype (L));
6801 Error_Msg_NE
6802 ("comparison on unordered enumeration type& declared#?U?",
6803 N, Etype (L));
6804 end if;
6806 -- Evaluate the relation (note we do this after the above check since
6807 -- this Eval call may change N to True/False.
6809 Analyze_Dimension (N);
6810 Eval_Relational_Op (N);
6811 end Resolve_Comparison_Op;
6813 -----------------------------------------
6814 -- Resolve_Discrete_Subtype_Indication --
6815 -----------------------------------------
6817 procedure Resolve_Discrete_Subtype_Indication
6818 (N : Node_Id;
6819 Typ : Entity_Id)
6821 R : Node_Id;
6822 S : Entity_Id;
6824 begin
6825 Analyze (Subtype_Mark (N));
6826 S := Entity (Subtype_Mark (N));
6828 if Nkind (Constraint (N)) /= N_Range_Constraint then
6829 Error_Msg_N ("expect range constraint for discrete type", N);
6830 Set_Etype (N, Any_Type);
6832 else
6833 R := Range_Expression (Constraint (N));
6835 if R = Error then
6836 return;
6837 end if;
6839 Analyze (R);
6841 if Base_Type (S) /= Base_Type (Typ) then
6842 Error_Msg_NE
6843 ("expect subtype of }", N, First_Subtype (Typ));
6845 -- Rewrite the constraint as a range of Typ
6846 -- to allow compilation to proceed further.
6848 Set_Etype (N, Typ);
6849 Rewrite (Low_Bound (R),
6850 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6851 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6852 Attribute_Name => Name_First));
6853 Rewrite (High_Bound (R),
6854 Make_Attribute_Reference (Sloc (High_Bound (R)),
6855 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6856 Attribute_Name => Name_First));
6858 else
6859 Resolve (R, Typ);
6860 Set_Etype (N, Etype (R));
6862 -- Additionally, we must check that the bounds are compatible
6863 -- with the given subtype, which might be different from the
6864 -- type of the context.
6866 Apply_Range_Check (R, S);
6868 -- ??? If the above check statically detects a Constraint_Error
6869 -- it replaces the offending bound(s) of the range R with a
6870 -- Constraint_Error node. When the itype which uses these bounds
6871 -- is frozen the resulting call to Duplicate_Subexpr generates
6872 -- a new temporary for the bounds.
6874 -- Unfortunately there are other itypes that are also made depend
6875 -- on these bounds, so when Duplicate_Subexpr is called they get
6876 -- a forward reference to the newly created temporaries and Gigi
6877 -- aborts on such forward references. This is probably sign of a
6878 -- more fundamental problem somewhere else in either the order of
6879 -- itype freezing or the way certain itypes are constructed.
6881 -- To get around this problem we call Remove_Side_Effects right
6882 -- away if either bounds of R are a Constraint_Error.
6884 declare
6885 L : constant Node_Id := Low_Bound (R);
6886 H : constant Node_Id := High_Bound (R);
6888 begin
6889 if Nkind (L) = N_Raise_Constraint_Error then
6890 Remove_Side_Effects (L);
6891 end if;
6893 if Nkind (H) = N_Raise_Constraint_Error then
6894 Remove_Side_Effects (H);
6895 end if;
6896 end;
6898 Check_Unset_Reference (Low_Bound (R));
6899 Check_Unset_Reference (High_Bound (R));
6900 end if;
6901 end if;
6902 end Resolve_Discrete_Subtype_Indication;
6904 -------------------------
6905 -- Resolve_Entity_Name --
6906 -------------------------
6908 -- Used to resolve identifiers and expanded names
6910 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6911 function Is_Assignment_Or_Object_Expression
6912 (Context : Node_Id;
6913 Expr : Node_Id) return Boolean;
6914 -- Determine whether node Context denotes an assignment statement or an
6915 -- object declaration whose expression is node Expr.
6917 ----------------------------------------
6918 -- Is_Assignment_Or_Object_Expression --
6919 ----------------------------------------
6921 function Is_Assignment_Or_Object_Expression
6922 (Context : Node_Id;
6923 Expr : Node_Id) return Boolean
6925 begin
6926 if Nkind_In (Context, N_Assignment_Statement,
6927 N_Object_Declaration)
6928 and then Expression (Context) = Expr
6929 then
6930 return True;
6932 -- Check whether a construct that yields a name is the expression of
6933 -- an assignment statement or an object declaration.
6935 elsif (Nkind_In (Context, N_Attribute_Reference,
6936 N_Explicit_Dereference,
6937 N_Indexed_Component,
6938 N_Selected_Component,
6939 N_Slice)
6940 and then Prefix (Context) = Expr)
6941 or else
6942 (Nkind_In (Context, N_Type_Conversion,
6943 N_Unchecked_Type_Conversion)
6944 and then Expression (Context) = Expr)
6945 then
6946 return
6947 Is_Assignment_Or_Object_Expression
6948 (Context => Parent (Context),
6949 Expr => Context);
6951 -- Otherwise the context is not an assignment statement or an object
6952 -- declaration.
6954 else
6955 return False;
6956 end if;
6957 end Is_Assignment_Or_Object_Expression;
6959 -- Local variables
6961 E : constant Entity_Id := Entity (N);
6962 Par : Node_Id;
6964 -- Start of processing for Resolve_Entity_Name
6966 begin
6967 -- If garbage from errors, set to Any_Type and return
6969 if No (E) and then Total_Errors_Detected /= 0 then
6970 Set_Etype (N, Any_Type);
6971 return;
6972 end if;
6974 -- Replace named numbers by corresponding literals. Note that this is
6975 -- the one case where Resolve_Entity_Name must reset the Etype, since
6976 -- it is currently marked as universal.
6978 if Ekind (E) = E_Named_Integer then
6979 Set_Etype (N, Typ);
6980 Eval_Named_Integer (N);
6982 elsif Ekind (E) = E_Named_Real then
6983 Set_Etype (N, Typ);
6984 Eval_Named_Real (N);
6986 -- For enumeration literals, we need to make sure that a proper style
6987 -- check is done, since such literals are overloaded, and thus we did
6988 -- not do a style check during the first phase of analysis.
6990 elsif Ekind (E) = E_Enumeration_Literal then
6991 Set_Entity_With_Checks (N, E);
6992 Eval_Entity_Name (N);
6994 -- Case of (sub)type name appearing in a context where an expression
6995 -- is expected. This is legal if occurrence is a current instance.
6996 -- See RM 8.6 (17/3).
6998 elsif Is_Type (E) then
6999 if Is_Current_Instance (N) then
7000 null;
7002 -- Any other use is an error
7004 else
7005 Error_Msg_N
7006 ("invalid use of subtype mark in expression or call", N);
7007 end if;
7009 -- Check discriminant use if entity is discriminant in current scope,
7010 -- i.e. discriminant of record or concurrent type currently being
7011 -- analyzed. Uses in corresponding body are unrestricted.
7013 elsif Ekind (E) = E_Discriminant
7014 and then Scope (E) = Current_Scope
7015 and then not Has_Completion (Current_Scope)
7016 then
7017 Check_Discriminant_Use (N);
7019 -- A parameterless generic function cannot appear in a context that
7020 -- requires resolution.
7022 elsif Ekind (E) = E_Generic_Function then
7023 Error_Msg_N ("illegal use of generic function", N);
7025 -- In Ada 83 an OUT parameter cannot be read
7027 elsif Ekind (E) = E_Out_Parameter
7028 and then (Nkind (Parent (N)) in N_Op
7029 or else Nkind (Parent (N)) = N_Explicit_Dereference
7030 or else Is_Assignment_Or_Object_Expression
7031 (Context => Parent (N),
7032 Expr => N))
7033 then
7034 if Ada_Version = Ada_83 then
7035 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
7036 end if;
7038 -- In all other cases, just do the possible static evaluation
7040 else
7041 -- A deferred constant that appears in an expression must have a
7042 -- completion, unless it has been removed by in-place expansion of
7043 -- an aggregate. A constant that is a renaming does not need
7044 -- initialization.
7046 if Ekind (E) = E_Constant
7047 and then Comes_From_Source (E)
7048 and then No (Constant_Value (E))
7049 and then Is_Frozen (Etype (E))
7050 and then not In_Spec_Expression
7051 and then not Is_Imported (E)
7052 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7053 then
7054 if No_Initialization (Parent (E))
7055 or else (Present (Full_View (E))
7056 and then No_Initialization (Parent (Full_View (E))))
7057 then
7058 null;
7059 else
7060 Error_Msg_N
7061 ("deferred constant is frozen before completion", N);
7062 end if;
7063 end if;
7065 Eval_Entity_Name (N);
7066 end if;
7068 Par := Parent (N);
7070 -- When the entity appears in a parameter association, retrieve the
7071 -- related subprogram call.
7073 if Nkind (Par) = N_Parameter_Association then
7074 Par := Parent (Par);
7075 end if;
7077 if Comes_From_Source (N) then
7079 -- The following checks are only relevant when SPARK_Mode is on as
7080 -- they are not standard Ada legality rules.
7082 if SPARK_Mode = On then
7084 -- An effectively volatile object subject to enabled properties
7085 -- Async_Writers or Effective_Reads must appear in non-interfering
7086 -- context (SPARK RM 7.1.3(12)).
7088 if Is_Object (E)
7089 and then Is_Effectively_Volatile (E)
7090 and then (Async_Writers_Enabled (E)
7091 or else Effective_Reads_Enabled (E))
7092 and then not Is_OK_Volatile_Context (Par, N)
7093 then
7094 SPARK_Msg_N
7095 ("volatile object cannot appear in this context "
7096 & "(SPARK RM 7.1.3(12))", N);
7097 end if;
7099 -- Check for possible elaboration issues with respect to reads of
7100 -- variables. The act of renaming the variable is not considered a
7101 -- read as it simply establishes an alias.
7103 if Ekind (E) = E_Variable
7104 and then Nkind (Par) /= N_Object_Renaming_Declaration
7105 then
7106 Check_Elab_Call (N);
7107 end if;
7109 -- The variable may eventually become a constituent of a single
7110 -- protected/task type. Record the reference now and verify its
7111 -- legality when analyzing the contract of the variable
7112 -- (SPARK RM 9.3).
7114 if Ekind (E) = E_Variable then
7115 Record_Possible_Part_Of_Reference (E, N);
7116 end if;
7117 end if;
7119 -- A Ghost entity must appear in a specific context
7121 if Is_Ghost_Entity (E) then
7122 Check_Ghost_Context (E, N);
7123 end if;
7124 end if;
7125 end Resolve_Entity_Name;
7127 -------------------
7128 -- Resolve_Entry --
7129 -------------------
7131 procedure Resolve_Entry (Entry_Name : Node_Id) is
7132 Loc : constant Source_Ptr := Sloc (Entry_Name);
7133 Nam : Entity_Id;
7134 New_N : Node_Id;
7135 S : Entity_Id;
7136 Tsk : Entity_Id;
7137 E_Name : Node_Id;
7138 Index : Node_Id;
7140 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
7141 -- If the bounds of the entry family being called depend on task
7142 -- discriminants, build a new index subtype where a discriminant is
7143 -- replaced with the value of the discriminant of the target task.
7144 -- The target task is the prefix of the entry name in the call.
7146 -----------------------
7147 -- Actual_Index_Type --
7148 -----------------------
7150 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
7151 Typ : constant Entity_Id := Entry_Index_Type (E);
7152 Tsk : constant Entity_Id := Scope (E);
7153 Lo : constant Node_Id := Type_Low_Bound (Typ);
7154 Hi : constant Node_Id := Type_High_Bound (Typ);
7155 New_T : Entity_Id;
7157 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
7158 -- If the bound is given by a discriminant, replace with a reference
7159 -- to the discriminant of the same name in the target task. If the
7160 -- entry name is the target of a requeue statement and the entry is
7161 -- in the current protected object, the bound to be used is the
7162 -- discriminal of the object (see Apply_Range_Checks for details of
7163 -- the transformation).
7165 -----------------------------
7166 -- Actual_Discriminant_Ref --
7167 -----------------------------
7169 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
7170 Typ : constant Entity_Id := Etype (Bound);
7171 Ref : Node_Id;
7173 begin
7174 Remove_Side_Effects (Bound);
7176 if not Is_Entity_Name (Bound)
7177 or else Ekind (Entity (Bound)) /= E_Discriminant
7178 then
7179 return Bound;
7181 elsif Is_Protected_Type (Tsk)
7182 and then In_Open_Scopes (Tsk)
7183 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
7184 then
7185 -- Note: here Bound denotes a discriminant of the corresponding
7186 -- record type tskV, whose discriminal is a formal of the
7187 -- init-proc tskVIP. What we want is the body discriminal,
7188 -- which is associated to the discriminant of the original
7189 -- concurrent type tsk.
7191 return New_Occurrence_Of
7192 (Find_Body_Discriminal (Entity (Bound)), Loc);
7194 else
7195 Ref :=
7196 Make_Selected_Component (Loc,
7197 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
7198 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
7199 Analyze (Ref);
7200 Resolve (Ref, Typ);
7201 return Ref;
7202 end if;
7203 end Actual_Discriminant_Ref;
7205 -- Start of processing for Actual_Index_Type
7207 begin
7208 if not Has_Discriminants (Tsk)
7209 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
7210 then
7211 return Entry_Index_Type (E);
7213 else
7214 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
7215 Set_Etype (New_T, Base_Type (Typ));
7216 Set_Size_Info (New_T, Typ);
7217 Set_RM_Size (New_T, RM_Size (Typ));
7218 Set_Scalar_Range (New_T,
7219 Make_Range (Sloc (Entry_Name),
7220 Low_Bound => Actual_Discriminant_Ref (Lo),
7221 High_Bound => Actual_Discriminant_Ref (Hi)));
7223 return New_T;
7224 end if;
7225 end Actual_Index_Type;
7227 -- Start of processing for Resolve_Entry
7229 begin
7230 -- Find name of entry being called, and resolve prefix of name with its
7231 -- own type. The prefix can be overloaded, and the name and signature of
7232 -- the entry must be taken into account.
7234 if Nkind (Entry_Name) = N_Indexed_Component then
7236 -- Case of dealing with entry family within the current tasks
7238 E_Name := Prefix (Entry_Name);
7240 else
7241 E_Name := Entry_Name;
7242 end if;
7244 if Is_Entity_Name (E_Name) then
7246 -- Entry call to an entry (or entry family) in the current task. This
7247 -- is legal even though the task will deadlock. Rewrite as call to
7248 -- current task.
7250 -- This can also be a call to an entry in an enclosing task. If this
7251 -- is a single task, we have to retrieve its name, because the scope
7252 -- of the entry is the task type, not the object. If the enclosing
7253 -- task is a task type, the identity of the task is given by its own
7254 -- self variable.
7256 -- Finally this can be a requeue on an entry of the same task or
7257 -- protected object.
7259 S := Scope (Entity (E_Name));
7261 for J in reverse 0 .. Scope_Stack.Last loop
7262 if Is_Task_Type (Scope_Stack.Table (J).Entity)
7263 and then not Comes_From_Source (S)
7264 then
7265 -- S is an enclosing task or protected object. The concurrent
7266 -- declaration has been converted into a type declaration, and
7267 -- the object itself has an object declaration that follows
7268 -- the type in the same declarative part.
7270 Tsk := Next_Entity (S);
7271 while Etype (Tsk) /= S loop
7272 Next_Entity (Tsk);
7273 end loop;
7275 S := Tsk;
7276 exit;
7278 elsif S = Scope_Stack.Table (J).Entity then
7280 -- Call to current task. Will be transformed into call to Self
7282 exit;
7284 end if;
7285 end loop;
7287 New_N :=
7288 Make_Selected_Component (Loc,
7289 Prefix => New_Occurrence_Of (S, Loc),
7290 Selector_Name =>
7291 New_Occurrence_Of (Entity (E_Name), Loc));
7292 Rewrite (E_Name, New_N);
7293 Analyze (E_Name);
7295 elsif Nkind (Entry_Name) = N_Selected_Component
7296 and then Is_Overloaded (Prefix (Entry_Name))
7297 then
7298 -- Use the entry name (which must be unique at this point) to find
7299 -- the prefix that returns the corresponding task/protected type.
7301 declare
7302 Pref : constant Node_Id := Prefix (Entry_Name);
7303 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
7304 I : Interp_Index;
7305 It : Interp;
7307 begin
7308 Get_First_Interp (Pref, I, It);
7309 while Present (It.Typ) loop
7310 if Scope (Ent) = It.Typ then
7311 Set_Etype (Pref, It.Typ);
7312 exit;
7313 end if;
7315 Get_Next_Interp (I, It);
7316 end loop;
7317 end;
7318 end if;
7320 if Nkind (Entry_Name) = N_Selected_Component then
7321 Resolve (Prefix (Entry_Name));
7323 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7324 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
7325 Resolve (Prefix (Prefix (Entry_Name)));
7326 Index := First (Expressions (Entry_Name));
7327 Resolve (Index, Entry_Index_Type (Nam));
7329 -- Up to this point the expression could have been the actual in a
7330 -- simple entry call, and be given by a named association.
7332 if Nkind (Index) = N_Parameter_Association then
7333 Error_Msg_N ("expect expression for entry index", Index);
7334 else
7335 Apply_Range_Check (Index, Actual_Index_Type (Nam));
7336 end if;
7337 end if;
7338 end Resolve_Entry;
7340 ------------------------
7341 -- Resolve_Entry_Call --
7342 ------------------------
7344 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
7345 Entry_Name : constant Node_Id := Name (N);
7346 Loc : constant Source_Ptr := Sloc (Entry_Name);
7347 Actuals : List_Id;
7348 First_Named : Node_Id;
7349 Nam : Entity_Id;
7350 Norm_OK : Boolean;
7351 Obj : Node_Id;
7352 Was_Over : Boolean;
7354 begin
7355 -- We kill all checks here, because it does not seem worth the effort to
7356 -- do anything better, an entry call is a big operation.
7358 Kill_All_Checks;
7360 -- Processing of the name is similar for entry calls and protected
7361 -- operation calls. Once the entity is determined, we can complete
7362 -- the resolution of the actuals.
7364 -- The selector may be overloaded, in the case of a protected object
7365 -- with overloaded functions. The type of the context is used for
7366 -- resolution.
7368 if Nkind (Entry_Name) = N_Selected_Component
7369 and then Is_Overloaded (Selector_Name (Entry_Name))
7370 and then Typ /= Standard_Void_Type
7371 then
7372 declare
7373 I : Interp_Index;
7374 It : Interp;
7376 begin
7377 Get_First_Interp (Selector_Name (Entry_Name), I, It);
7378 while Present (It.Typ) loop
7379 if Covers (Typ, It.Typ) then
7380 Set_Entity (Selector_Name (Entry_Name), It.Nam);
7381 Set_Etype (Entry_Name, It.Typ);
7383 Generate_Reference (It.Typ, N, ' ');
7384 end if;
7386 Get_Next_Interp (I, It);
7387 end loop;
7388 end;
7389 end if;
7391 Resolve_Entry (Entry_Name);
7393 if Nkind (Entry_Name) = N_Selected_Component then
7395 -- Simple entry call
7397 Nam := Entity (Selector_Name (Entry_Name));
7398 Obj := Prefix (Entry_Name);
7399 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
7401 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7403 -- Call to member of entry family
7405 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
7406 Obj := Prefix (Prefix (Entry_Name));
7407 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
7408 end if;
7410 -- We cannot in general check the maximum depth of protected entry calls
7411 -- at compile time. But we can tell that any protected entry call at all
7412 -- violates a specified nesting depth of zero.
7414 if Is_Protected_Type (Scope (Nam)) then
7415 Check_Restriction (Max_Entry_Queue_Length, N);
7416 end if;
7418 -- Use context type to disambiguate a protected function that can be
7419 -- called without actuals and that returns an array type, and where the
7420 -- argument list may be an indexing of the returned value.
7422 if Ekind (Nam) = E_Function
7423 and then Needs_No_Actuals (Nam)
7424 and then Present (Parameter_Associations (N))
7425 and then
7426 ((Is_Array_Type (Etype (Nam))
7427 and then Covers (Typ, Component_Type (Etype (Nam))))
7429 or else (Is_Access_Type (Etype (Nam))
7430 and then Is_Array_Type (Designated_Type (Etype (Nam)))
7431 and then
7432 Covers
7433 (Typ,
7434 Component_Type (Designated_Type (Etype (Nam))))))
7435 then
7436 declare
7437 Index_Node : Node_Id;
7439 begin
7440 Index_Node :=
7441 Make_Indexed_Component (Loc,
7442 Prefix =>
7443 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
7444 Expressions => Parameter_Associations (N));
7446 -- Since we are correcting a node classification error made by the
7447 -- parser, we call Replace rather than Rewrite.
7449 Replace (N, Index_Node);
7450 Set_Etype (Prefix (N), Etype (Nam));
7451 Set_Etype (N, Typ);
7452 Resolve_Indexed_Component (N, Typ);
7453 return;
7454 end;
7455 end if;
7457 if Ekind_In (Nam, E_Entry, E_Entry_Family)
7458 and then Present (Contract_Wrapper (Nam))
7459 and then Current_Scope /= Contract_Wrapper (Nam)
7460 then
7462 -- Note the entity being called before rewriting the call, so that
7463 -- it appears used at this point.
7465 Generate_Reference (Nam, Entry_Name, 'r');
7467 -- Rewrite as call to the precondition wrapper, adding the task
7468 -- object to the list of actuals. If the call is to a member of an
7469 -- entry family, include the index as well.
7471 declare
7472 New_Call : Node_Id;
7473 New_Actuals : List_Id;
7475 begin
7476 New_Actuals := New_List (Obj);
7478 if Nkind (Entry_Name) = N_Indexed_Component then
7479 Append_To (New_Actuals,
7480 New_Copy_Tree (First (Expressions (Entry_Name))));
7481 end if;
7483 Append_List (Parameter_Associations (N), New_Actuals);
7484 New_Call :=
7485 Make_Procedure_Call_Statement (Loc,
7486 Name =>
7487 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
7488 Parameter_Associations => New_Actuals);
7489 Rewrite (N, New_Call);
7491 -- Preanalyze and resolve new call. Current procedure is called
7492 -- from Resolve_Call, after which expansion will take place.
7494 Preanalyze_And_Resolve (N);
7495 return;
7496 end;
7497 end if;
7499 -- The operation name may have been overloaded. Order the actuals
7500 -- according to the formals of the resolved entity, and set the return
7501 -- type to that of the operation.
7503 if Was_Over then
7504 Normalize_Actuals (N, Nam, False, Norm_OK);
7505 pragma Assert (Norm_OK);
7506 Set_Etype (N, Etype (Nam));
7508 -- Reset the Is_Overloaded flag, since resolution is now completed
7510 -- Simple entry call
7512 if Nkind (Entry_Name) = N_Selected_Component then
7513 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
7515 -- Call to a member of an entry family
7517 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7518 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
7519 end if;
7520 end if;
7522 Resolve_Actuals (N, Nam);
7523 Check_Internal_Protected_Use (N, Nam);
7525 -- Create a call reference to the entry
7527 Generate_Reference (Nam, Entry_Name, 's');
7529 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
7530 Check_Potentially_Blocking_Operation (N);
7531 end if;
7533 -- Verify that a procedure call cannot masquerade as an entry
7534 -- call where an entry call is expected.
7536 if Ekind (Nam) = E_Procedure then
7537 if Nkind (Parent (N)) = N_Entry_Call_Alternative
7538 and then N = Entry_Call_Statement (Parent (N))
7539 then
7540 Error_Msg_N ("entry call required in select statement", N);
7542 elsif Nkind (Parent (N)) = N_Triggering_Alternative
7543 and then N = Triggering_Statement (Parent (N))
7544 then
7545 Error_Msg_N ("triggering statement cannot be procedure call", N);
7547 elsif Ekind (Scope (Nam)) = E_Task_Type
7548 and then not In_Open_Scopes (Scope (Nam))
7549 then
7550 Error_Msg_N ("task has no entry with this name", Entry_Name);
7551 end if;
7552 end if;
7554 -- After resolution, entry calls and protected procedure calls are
7555 -- changed into entry calls, for expansion. The structure of the node
7556 -- does not change, so it can safely be done in place. Protected
7557 -- function calls must keep their structure because they are
7558 -- subexpressions.
7560 if Ekind (Nam) /= E_Function then
7562 -- A protected operation that is not a function may modify the
7563 -- corresponding object, and cannot apply to a constant. If this
7564 -- is an internal call, the prefix is the type itself.
7566 if Is_Protected_Type (Scope (Nam))
7567 and then not Is_Variable (Obj)
7568 and then (not Is_Entity_Name (Obj)
7569 or else not Is_Type (Entity (Obj)))
7570 then
7571 Error_Msg_N
7572 ("prefix of protected procedure or entry call must be variable",
7573 Entry_Name);
7574 end if;
7576 Actuals := Parameter_Associations (N);
7577 First_Named := First_Named_Actual (N);
7579 Rewrite (N,
7580 Make_Entry_Call_Statement (Loc,
7581 Name => Entry_Name,
7582 Parameter_Associations => Actuals));
7584 Set_First_Named_Actual (N, First_Named);
7585 Set_Analyzed (N, True);
7587 -- Protected functions can return on the secondary stack, in which
7588 -- case we must trigger the transient scope mechanism.
7590 elsif Expander_Active
7591 and then Requires_Transient_Scope (Etype (Nam))
7592 then
7593 Establish_Transient_Scope (N, Sec_Stack => True);
7594 end if;
7595 end Resolve_Entry_Call;
7597 -------------------------
7598 -- Resolve_Equality_Op --
7599 -------------------------
7601 -- Both arguments must have the same type, and the boolean context does
7602 -- not participate in the resolution. The first pass verifies that the
7603 -- interpretation is not ambiguous, and the type of the left argument is
7604 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
7605 -- are strings or aggregates, allocators, or Null, they are ambiguous even
7606 -- though they carry a single (universal) type. Diagnose this case here.
7608 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
7609 L : constant Node_Id := Left_Opnd (N);
7610 R : constant Node_Id := Right_Opnd (N);
7611 T : Entity_Id := Find_Unique_Type (L, R);
7613 procedure Check_If_Expression (Cond : Node_Id);
7614 -- The resolution rule for if expressions requires that each such must
7615 -- have a unique type. This means that if several dependent expressions
7616 -- are of a non-null anonymous access type, and the context does not
7617 -- impose an expected type (as can be the case in an equality operation)
7618 -- the expression must be rejected.
7620 procedure Explain_Redundancy (N : Node_Id);
7621 -- Attempt to explain the nature of a redundant comparison with True. If
7622 -- the expression N is too complex, this routine issues a general error
7623 -- message.
7625 function Find_Unique_Access_Type return Entity_Id;
7626 -- In the case of allocators and access attributes, the context must
7627 -- provide an indication of the specific access type to be used. If
7628 -- one operand is of such a "generic" access type, check whether there
7629 -- is a specific visible access type that has the same designated type.
7630 -- This is semantically dubious, and of no interest to any real code,
7631 -- but c48008a makes it all worthwhile.
7633 -------------------------
7634 -- Check_If_Expression --
7635 -------------------------
7637 procedure Check_If_Expression (Cond : Node_Id) is
7638 Then_Expr : Node_Id;
7639 Else_Expr : Node_Id;
7641 begin
7642 if Nkind (Cond) = N_If_Expression then
7643 Then_Expr := Next (First (Expressions (Cond)));
7644 Else_Expr := Next (Then_Expr);
7646 if Nkind (Then_Expr) /= N_Null
7647 and then Nkind (Else_Expr) /= N_Null
7648 then
7649 Error_Msg_N ("cannot determine type of if expression", Cond);
7650 end if;
7651 end if;
7652 end Check_If_Expression;
7654 ------------------------
7655 -- Explain_Redundancy --
7656 ------------------------
7658 procedure Explain_Redundancy (N : Node_Id) is
7659 Error : Name_Id;
7660 Val : Node_Id;
7661 Val_Id : Entity_Id;
7663 begin
7664 Val := N;
7666 -- Strip the operand down to an entity
7668 loop
7669 if Nkind (Val) = N_Selected_Component then
7670 Val := Selector_Name (Val);
7671 else
7672 exit;
7673 end if;
7674 end loop;
7676 -- The construct denotes an entity
7678 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
7679 Val_Id := Entity (Val);
7681 -- Do not generate an error message when the comparison is done
7682 -- against the enumeration literal Standard.True.
7684 if Ekind (Val_Id) /= E_Enumeration_Literal then
7686 -- Build a customized error message
7688 Name_Len := 0;
7689 Add_Str_To_Name_Buffer ("?r?");
7691 if Ekind (Val_Id) = E_Component then
7692 Add_Str_To_Name_Buffer ("component ");
7694 elsif Ekind (Val_Id) = E_Constant then
7695 Add_Str_To_Name_Buffer ("constant ");
7697 elsif Ekind (Val_Id) = E_Discriminant then
7698 Add_Str_To_Name_Buffer ("discriminant ");
7700 elsif Is_Formal (Val_Id) then
7701 Add_Str_To_Name_Buffer ("parameter ");
7703 elsif Ekind (Val_Id) = E_Variable then
7704 Add_Str_To_Name_Buffer ("variable ");
7705 end if;
7707 Add_Str_To_Name_Buffer ("& is always True!");
7708 Error := Name_Find;
7710 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
7711 end if;
7713 -- The construct is too complex to disect, issue a general message
7715 else
7716 Error_Msg_N ("?r?expression is always True!", Val);
7717 end if;
7718 end Explain_Redundancy;
7720 -----------------------------
7721 -- Find_Unique_Access_Type --
7722 -----------------------------
7724 function Find_Unique_Access_Type return Entity_Id is
7725 Acc : Entity_Id;
7726 E : Entity_Id;
7727 S : Entity_Id;
7729 begin
7730 if Ekind_In (Etype (R), E_Allocator_Type,
7731 E_Access_Attribute_Type)
7732 then
7733 Acc := Designated_Type (Etype (R));
7735 elsif Ekind_In (Etype (L), E_Allocator_Type,
7736 E_Access_Attribute_Type)
7737 then
7738 Acc := Designated_Type (Etype (L));
7739 else
7740 return Empty;
7741 end if;
7743 S := Current_Scope;
7744 while S /= Standard_Standard loop
7745 E := First_Entity (S);
7746 while Present (E) loop
7747 if Is_Type (E)
7748 and then Is_Access_Type (E)
7749 and then Ekind (E) /= E_Allocator_Type
7750 and then Designated_Type (E) = Base_Type (Acc)
7751 then
7752 return E;
7753 end if;
7755 Next_Entity (E);
7756 end loop;
7758 S := Scope (S);
7759 end loop;
7761 return Empty;
7762 end Find_Unique_Access_Type;
7764 -- Start of processing for Resolve_Equality_Op
7766 begin
7767 Set_Etype (N, Base_Type (Typ));
7768 Generate_Reference (T, N, ' ');
7770 if T = Any_Fixed then
7771 T := Unique_Fixed_Point_Type (L);
7772 end if;
7774 if T /= Any_Type then
7775 if T = Any_String or else
7776 T = Any_Composite or else
7777 T = Any_Character
7778 then
7779 if T = Any_Character then
7780 Ambiguous_Character (L);
7781 else
7782 Error_Msg_N ("ambiguous operands for equality", N);
7783 end if;
7785 Set_Etype (N, Any_Type);
7786 return;
7788 elsif T = Any_Access
7789 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
7790 then
7791 T := Find_Unique_Access_Type;
7793 if No (T) then
7794 Error_Msg_N ("ambiguous operands for equality", N);
7795 Set_Etype (N, Any_Type);
7796 return;
7797 end if;
7799 -- If expressions must have a single type, and if the context does
7800 -- not impose one the dependent expressions cannot be anonymous
7801 -- access types.
7803 -- Why no similar processing for case expressions???
7805 elsif Ada_Version >= Ada_2012
7806 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
7807 E_Anonymous_Access_Subprogram_Type)
7808 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
7809 E_Anonymous_Access_Subprogram_Type)
7810 then
7811 Check_If_Expression (L);
7812 Check_If_Expression (R);
7813 end if;
7815 Resolve (L, T);
7816 Resolve (R, T);
7818 -- In SPARK, equality operators = and /= for array types other than
7819 -- String are only defined when, for each index position, the
7820 -- operands have equal static bounds.
7822 if Is_Array_Type (T) then
7824 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7825 -- operation if not needed.
7827 if Restriction_Check_Required (SPARK_05)
7828 and then Base_Type (T) /= Standard_String
7829 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7830 and then Etype (L) /= Any_Composite -- or else L in error
7831 and then Etype (R) /= Any_Composite -- or else R in error
7832 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
7833 then
7834 Check_SPARK_05_Restriction
7835 ("array types should have matching static bounds", N);
7836 end if;
7837 end if;
7839 -- If the unique type is a class-wide type then it will be expanded
7840 -- into a dispatching call to the predefined primitive. Therefore we
7841 -- check here for potential violation of such restriction.
7843 if Is_Class_Wide_Type (T) then
7844 Check_Restriction (No_Dispatching_Calls, N);
7845 end if;
7847 if Warn_On_Redundant_Constructs
7848 and then Comes_From_Source (N)
7849 and then Comes_From_Source (R)
7850 and then Is_Entity_Name (R)
7851 and then Entity (R) = Standard_True
7852 then
7853 Error_Msg_N -- CODEFIX
7854 ("?r?comparison with True is redundant!", N);
7855 Explain_Redundancy (Original_Node (R));
7856 end if;
7858 Check_Unset_Reference (L);
7859 Check_Unset_Reference (R);
7860 Generate_Operator_Reference (N, T);
7861 Check_Low_Bound_Tested (N);
7863 -- If this is an inequality, it may be the implicit inequality
7864 -- created for a user-defined operation, in which case the corres-
7865 -- ponding equality operation is not intrinsic, and the operation
7866 -- cannot be constant-folded. Else fold.
7868 if Nkind (N) = N_Op_Eq
7869 or else Comes_From_Source (Entity (N))
7870 or else Ekind (Entity (N)) = E_Operator
7871 or else Is_Intrinsic_Subprogram
7872 (Corresponding_Equality (Entity (N)))
7873 then
7874 Analyze_Dimension (N);
7875 Eval_Relational_Op (N);
7877 elsif Nkind (N) = N_Op_Ne
7878 and then Is_Abstract_Subprogram (Entity (N))
7879 then
7880 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7881 end if;
7883 -- Ada 2005: If one operand is an anonymous access type, convert the
7884 -- other operand to it, to ensure that the underlying types match in
7885 -- the back-end. Same for access_to_subprogram, and the conversion
7886 -- verifies that the types are subtype conformant.
7888 -- We apply the same conversion in the case one of the operands is a
7889 -- private subtype of the type of the other.
7891 -- Why the Expander_Active test here ???
7893 if Expander_Active
7894 and then
7895 (Ekind_In (T, E_Anonymous_Access_Type,
7896 E_Anonymous_Access_Subprogram_Type)
7897 or else Is_Private_Type (T))
7898 then
7899 if Etype (L) /= T then
7900 Rewrite (L,
7901 Make_Unchecked_Type_Conversion (Sloc (L),
7902 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7903 Expression => Relocate_Node (L)));
7904 Analyze_And_Resolve (L, T);
7905 end if;
7907 if (Etype (R)) /= T then
7908 Rewrite (R,
7909 Make_Unchecked_Type_Conversion (Sloc (R),
7910 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7911 Expression => Relocate_Node (R)));
7912 Analyze_And_Resolve (R, T);
7913 end if;
7914 end if;
7915 end if;
7916 end Resolve_Equality_Op;
7918 ----------------------------------
7919 -- Resolve_Explicit_Dereference --
7920 ----------------------------------
7922 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
7923 Loc : constant Source_Ptr := Sloc (N);
7924 New_N : Node_Id;
7925 P : constant Node_Id := Prefix (N);
7927 P_Typ : Entity_Id;
7928 -- The candidate prefix type, if overloaded
7930 I : Interp_Index;
7931 It : Interp;
7933 begin
7934 Check_Fully_Declared_Prefix (Typ, P);
7935 P_Typ := Empty;
7937 -- A useful optimization: check whether the dereference denotes an
7938 -- element of a container, and if so rewrite it as a call to the
7939 -- corresponding Element function.
7941 -- Disabled for now, on advice of ARG. A more restricted form of the
7942 -- predicate might be acceptable ???
7944 -- if Is_Container_Element (N) then
7945 -- return;
7946 -- end if;
7948 if Is_Overloaded (P) then
7950 -- Use the context type to select the prefix that has the correct
7951 -- designated type. Keep the first match, which will be the inner-
7952 -- most.
7954 Get_First_Interp (P, I, It);
7956 while Present (It.Typ) loop
7957 if Is_Access_Type (It.Typ)
7958 and then Covers (Typ, Designated_Type (It.Typ))
7959 then
7960 if No (P_Typ) then
7961 P_Typ := It.Typ;
7962 end if;
7964 -- Remove access types that do not match, but preserve access
7965 -- to subprogram interpretations, in case a further dereference
7966 -- is needed (see below).
7968 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
7969 Remove_Interp (I);
7970 end if;
7972 Get_Next_Interp (I, It);
7973 end loop;
7975 if Present (P_Typ) then
7976 Resolve (P, P_Typ);
7977 Set_Etype (N, Designated_Type (P_Typ));
7979 else
7980 -- If no interpretation covers the designated type of the prefix,
7981 -- this is the pathological case where not all implementations of
7982 -- the prefix allow the interpretation of the node as a call. Now
7983 -- that the expected type is known, Remove other interpretations
7984 -- from prefix, rewrite it as a call, and resolve again, so that
7985 -- the proper call node is generated.
7987 Get_First_Interp (P, I, It);
7988 while Present (It.Typ) loop
7989 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
7990 Remove_Interp (I);
7991 end if;
7993 Get_Next_Interp (I, It);
7994 end loop;
7996 New_N :=
7997 Make_Function_Call (Loc,
7998 Name =>
7999 Make_Explicit_Dereference (Loc,
8000 Prefix => P),
8001 Parameter_Associations => New_List);
8003 Save_Interps (N, New_N);
8004 Rewrite (N, New_N);
8005 Analyze_And_Resolve (N, Typ);
8006 return;
8007 end if;
8009 -- If not overloaded, resolve P with its own type
8011 else
8012 Resolve (P);
8013 end if;
8015 -- If the prefix might be null, add an access check
8017 if Is_Access_Type (Etype (P))
8018 and then not Can_Never_Be_Null (Etype (P))
8019 then
8020 Apply_Access_Check (N);
8021 end if;
8023 -- If the designated type is a packed unconstrained array type, and the
8024 -- explicit dereference is not in the context of an attribute reference,
8025 -- then we must compute and set the actual subtype, since it is needed
8026 -- by Gigi. The reason we exclude the attribute case is that this is
8027 -- handled fine by Gigi, and in fact we use such attributes to build the
8028 -- actual subtype. We also exclude generated code (which builds actual
8029 -- subtypes directly if they are needed).
8031 if Is_Array_Type (Etype (N))
8032 and then Is_Packed (Etype (N))
8033 and then not Is_Constrained (Etype (N))
8034 and then Nkind (Parent (N)) /= N_Attribute_Reference
8035 and then Comes_From_Source (N)
8036 then
8037 Set_Etype (N, Get_Actual_Subtype (N));
8038 end if;
8040 Analyze_Dimension (N);
8042 -- Note: No Eval processing is required for an explicit dereference,
8043 -- because such a name can never be static.
8045 end Resolve_Explicit_Dereference;
8047 -------------------------------------
8048 -- Resolve_Expression_With_Actions --
8049 -------------------------------------
8051 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
8052 begin
8053 Set_Etype (N, Typ);
8055 -- If N has no actions, and its expression has been constant folded,
8056 -- then rewrite N as just its expression. Note, we can't do this in
8057 -- the general case of Is_Empty_List (Actions (N)) as this would cause
8058 -- Expression (N) to be expanded again.
8060 if Is_Empty_List (Actions (N))
8061 and then Compile_Time_Known_Value (Expression (N))
8062 then
8063 Rewrite (N, Expression (N));
8064 end if;
8065 end Resolve_Expression_With_Actions;
8067 ----------------------------------
8068 -- Resolve_Generalized_Indexing --
8069 ----------------------------------
8071 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
8072 Indexing : constant Node_Id := Generalized_Indexing (N);
8073 Call : Node_Id;
8074 Indexes : List_Id;
8075 Pref : Node_Id;
8077 begin
8078 -- In ASIS mode, propagate the information about the indexes back to
8079 -- to the original indexing node. The generalized indexing is either
8080 -- a function call, or a dereference of one. The actuals include the
8081 -- prefix of the original node, which is the container expression.
8083 if ASIS_Mode then
8084 Resolve (Indexing, Typ);
8085 Set_Etype (N, Etype (Indexing));
8086 Set_Is_Overloaded (N, False);
8088 Call := Indexing;
8089 while Nkind_In (Call, N_Explicit_Dereference, N_Selected_Component)
8090 loop
8091 Call := Prefix (Call);
8092 end loop;
8094 if Nkind (Call) = N_Function_Call then
8095 Indexes := Parameter_Associations (Call);
8096 Pref := Remove_Head (Indexes);
8097 Set_Expressions (N, Indexes);
8099 -- If expression is to be reanalyzed, reset Generalized_Indexing
8100 -- to recreate call node, as is the case when the expression is
8101 -- part of an expression function.
8103 if In_Spec_Expression then
8104 Set_Generalized_Indexing (N, Empty);
8105 end if;
8107 Set_Prefix (N, Pref);
8108 end if;
8110 else
8111 Rewrite (N, Indexing);
8112 Resolve (N, Typ);
8113 end if;
8114 end Resolve_Generalized_Indexing;
8116 ---------------------------
8117 -- Resolve_If_Expression --
8118 ---------------------------
8120 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
8121 Condition : constant Node_Id := First (Expressions (N));
8122 Then_Expr : constant Node_Id := Next (Condition);
8123 Else_Expr : Node_Id := Next (Then_Expr);
8124 Else_Typ : Entity_Id;
8125 Then_Typ : Entity_Id;
8127 begin
8128 Resolve (Condition, Any_Boolean);
8129 Resolve (Then_Expr, Typ);
8130 Then_Typ := Etype (Then_Expr);
8132 -- When the "then" expression is of a scalar subtype different from the
8133 -- result subtype, then insert a conversion to ensure the generation of
8134 -- a constraint check. The same is done for the else part below, again
8135 -- comparing subtypes rather than base types.
8137 if Is_Scalar_Type (Then_Typ)
8138 and then Then_Typ /= Typ
8139 then
8140 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
8141 Analyze_And_Resolve (Then_Expr, Typ);
8142 end if;
8144 -- If ELSE expression present, just resolve using the determined type
8145 -- If type is universal, resolve to any member of the class.
8147 if Present (Else_Expr) then
8148 if Typ = Universal_Integer then
8149 Resolve (Else_Expr, Any_Integer);
8151 elsif Typ = Universal_Real then
8152 Resolve (Else_Expr, Any_Real);
8154 else
8155 Resolve (Else_Expr, Typ);
8156 end if;
8158 Else_Typ := Etype (Else_Expr);
8160 if Is_Scalar_Type (Else_Typ) and then Else_Typ /= Typ then
8161 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
8162 Analyze_And_Resolve (Else_Expr, Typ);
8164 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
8165 -- dynamically tagged must be known statically.
8167 elsif Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
8168 if Is_Dynamically_Tagged (Then_Expr) /=
8169 Is_Dynamically_Tagged (Else_Expr)
8170 then
8171 Error_Msg_N ("all or none of the dependent expressions "
8172 & "can be dynamically tagged", N);
8173 end if;
8174 end if;
8176 -- If no ELSE expression is present, root type must be Standard.Boolean
8177 -- and we provide a Standard.True result converted to the appropriate
8178 -- Boolean type (in case it is a derived boolean type).
8180 elsif Root_Type (Typ) = Standard_Boolean then
8181 Else_Expr :=
8182 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
8183 Analyze_And_Resolve (Else_Expr, Typ);
8184 Append_To (Expressions (N), Else_Expr);
8186 else
8187 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
8188 Append_To (Expressions (N), Error);
8189 end if;
8191 Set_Etype (N, Typ);
8192 Eval_If_Expression (N);
8193 end Resolve_If_Expression;
8195 -------------------------------
8196 -- Resolve_Indexed_Component --
8197 -------------------------------
8199 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
8200 Name : constant Node_Id := Prefix (N);
8201 Expr : Node_Id;
8202 Array_Type : Entity_Id := Empty; -- to prevent junk warning
8203 Index : Node_Id;
8205 begin
8206 if Present (Generalized_Indexing (N)) then
8207 Resolve_Generalized_Indexing (N, Typ);
8208 return;
8209 end if;
8211 if Is_Overloaded (Name) then
8213 -- Use the context type to select the prefix that yields the correct
8214 -- component type.
8216 declare
8217 I : Interp_Index;
8218 It : Interp;
8219 I1 : Interp_Index := 0;
8220 P : constant Node_Id := Prefix (N);
8221 Found : Boolean := False;
8223 begin
8224 Get_First_Interp (P, I, It);
8225 while Present (It.Typ) loop
8226 if (Is_Array_Type (It.Typ)
8227 and then Covers (Typ, Component_Type (It.Typ)))
8228 or else (Is_Access_Type (It.Typ)
8229 and then Is_Array_Type (Designated_Type (It.Typ))
8230 and then
8231 Covers
8232 (Typ,
8233 Component_Type (Designated_Type (It.Typ))))
8234 then
8235 if Found then
8236 It := Disambiguate (P, I1, I, Any_Type);
8238 if It = No_Interp then
8239 Error_Msg_N ("ambiguous prefix for indexing", N);
8240 Set_Etype (N, Typ);
8241 return;
8243 else
8244 Found := True;
8245 Array_Type := It.Typ;
8246 I1 := I;
8247 end if;
8249 else
8250 Found := True;
8251 Array_Type := It.Typ;
8252 I1 := I;
8253 end if;
8254 end if;
8256 Get_Next_Interp (I, It);
8257 end loop;
8258 end;
8260 else
8261 Array_Type := Etype (Name);
8262 end if;
8264 Resolve (Name, Array_Type);
8265 Array_Type := Get_Actual_Subtype_If_Available (Name);
8267 -- If prefix is access type, dereference to get real array type.
8268 -- Note: we do not apply an access check because the expander always
8269 -- introduces an explicit dereference, and the check will happen there.
8271 if Is_Access_Type (Array_Type) then
8272 Array_Type := Designated_Type (Array_Type);
8273 end if;
8275 -- If name was overloaded, set component type correctly now
8276 -- If a misplaced call to an entry family (which has no index types)
8277 -- return. Error will be diagnosed from calling context.
8279 if Is_Array_Type (Array_Type) then
8280 Set_Etype (N, Component_Type (Array_Type));
8281 else
8282 return;
8283 end if;
8285 Index := First_Index (Array_Type);
8286 Expr := First (Expressions (N));
8288 -- The prefix may have resolved to a string literal, in which case its
8289 -- etype has a special representation. This is only possible currently
8290 -- if the prefix is a static concatenation, written in functional
8291 -- notation.
8293 if Ekind (Array_Type) = E_String_Literal_Subtype then
8294 Resolve (Expr, Standard_Positive);
8296 else
8297 while Present (Index) and Present (Expr) loop
8298 Resolve (Expr, Etype (Index));
8299 Check_Unset_Reference (Expr);
8301 if Is_Scalar_Type (Etype (Expr)) then
8302 Apply_Scalar_Range_Check (Expr, Etype (Index));
8303 else
8304 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
8305 end if;
8307 Next_Index (Index);
8308 Next (Expr);
8309 end loop;
8310 end if;
8312 Analyze_Dimension (N);
8314 -- Do not generate the warning on suspicious index if we are analyzing
8315 -- package Ada.Tags; otherwise we will report the warning with the
8316 -- Prims_Ptr field of the dispatch table.
8318 if Scope (Etype (Prefix (N))) = Standard_Standard
8319 or else not
8320 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
8321 Ada_Tags)
8322 then
8323 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
8324 Eval_Indexed_Component (N);
8325 end if;
8327 -- If the array type is atomic, and the component is not atomic, then
8328 -- this is worth a warning, since we have a situation where the access
8329 -- to the component may cause extra read/writes of the atomic array
8330 -- object, or partial word accesses, which could be unexpected.
8332 if Nkind (N) = N_Indexed_Component
8333 and then Is_Atomic_Ref_With_Address (N)
8334 and then not (Has_Atomic_Components (Array_Type)
8335 or else (Is_Entity_Name (Prefix (N))
8336 and then Has_Atomic_Components
8337 (Entity (Prefix (N)))))
8338 and then not Is_Atomic (Component_Type (Array_Type))
8339 then
8340 Error_Msg_N
8341 ("??access to non-atomic component of atomic array", Prefix (N));
8342 Error_Msg_N
8343 ("??\may cause unexpected accesses to atomic object", Prefix (N));
8344 end if;
8345 end Resolve_Indexed_Component;
8347 -----------------------------
8348 -- Resolve_Integer_Literal --
8349 -----------------------------
8351 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
8352 begin
8353 Set_Etype (N, Typ);
8354 Eval_Integer_Literal (N);
8355 end Resolve_Integer_Literal;
8357 --------------------------------
8358 -- Resolve_Intrinsic_Operator --
8359 --------------------------------
8361 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
8362 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8363 Op : Entity_Id;
8364 Arg1 : Node_Id;
8365 Arg2 : Node_Id;
8367 function Convert_Operand (Opnd : Node_Id) return Node_Id;
8368 -- If the operand is a literal, it cannot be the expression in a
8369 -- conversion. Use a qualified expression instead.
8371 ---------------------
8372 -- Convert_Operand --
8373 ---------------------
8375 function Convert_Operand (Opnd : Node_Id) return Node_Id is
8376 Loc : constant Source_Ptr := Sloc (Opnd);
8377 Res : Node_Id;
8379 begin
8380 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
8381 Res :=
8382 Make_Qualified_Expression (Loc,
8383 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
8384 Expression => Relocate_Node (Opnd));
8385 Analyze (Res);
8387 else
8388 Res := Unchecked_Convert_To (Btyp, Opnd);
8389 end if;
8391 return Res;
8392 end Convert_Operand;
8394 -- Start of processing for Resolve_Intrinsic_Operator
8396 begin
8397 -- We must preserve the original entity in a generic setting, so that
8398 -- the legality of the operation can be verified in an instance.
8400 if not Expander_Active then
8401 return;
8402 end if;
8404 Op := Entity (N);
8405 while Scope (Op) /= Standard_Standard loop
8406 Op := Homonym (Op);
8407 pragma Assert (Present (Op));
8408 end loop;
8410 Set_Entity (N, Op);
8411 Set_Is_Overloaded (N, False);
8413 -- If the result or operand types are private, rewrite with unchecked
8414 -- conversions on the operands and the result, to expose the proper
8415 -- underlying numeric type.
8417 if Is_Private_Type (Typ)
8418 or else Is_Private_Type (Etype (Left_Opnd (N)))
8419 or else Is_Private_Type (Etype (Right_Opnd (N)))
8420 then
8421 Arg1 := Convert_Operand (Left_Opnd (N));
8423 if Nkind (N) = N_Op_Expon then
8424 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
8425 else
8426 Arg2 := Convert_Operand (Right_Opnd (N));
8427 end if;
8429 if Nkind (Arg1) = N_Type_Conversion then
8430 Save_Interps (Left_Opnd (N), Expression (Arg1));
8431 end if;
8433 if Nkind (Arg2) = N_Type_Conversion then
8434 Save_Interps (Right_Opnd (N), Expression (Arg2));
8435 end if;
8437 Set_Left_Opnd (N, Arg1);
8438 Set_Right_Opnd (N, Arg2);
8440 Set_Etype (N, Btyp);
8441 Rewrite (N, Unchecked_Convert_To (Typ, N));
8442 Resolve (N, Typ);
8444 elsif Typ /= Etype (Left_Opnd (N))
8445 or else Typ /= Etype (Right_Opnd (N))
8446 then
8447 -- Add explicit conversion where needed, and save interpretations in
8448 -- case operands are overloaded.
8450 Arg1 := Convert_To (Typ, Left_Opnd (N));
8451 Arg2 := Convert_To (Typ, Right_Opnd (N));
8453 if Nkind (Arg1) = N_Type_Conversion then
8454 Save_Interps (Left_Opnd (N), Expression (Arg1));
8455 else
8456 Save_Interps (Left_Opnd (N), Arg1);
8457 end if;
8459 if Nkind (Arg2) = N_Type_Conversion then
8460 Save_Interps (Right_Opnd (N), Expression (Arg2));
8461 else
8462 Save_Interps (Right_Opnd (N), Arg2);
8463 end if;
8465 Rewrite (Left_Opnd (N), Arg1);
8466 Rewrite (Right_Opnd (N), Arg2);
8467 Analyze (Arg1);
8468 Analyze (Arg2);
8469 Resolve_Arithmetic_Op (N, Typ);
8471 else
8472 Resolve_Arithmetic_Op (N, Typ);
8473 end if;
8474 end Resolve_Intrinsic_Operator;
8476 --------------------------------------
8477 -- Resolve_Intrinsic_Unary_Operator --
8478 --------------------------------------
8480 procedure Resolve_Intrinsic_Unary_Operator
8481 (N : Node_Id;
8482 Typ : Entity_Id)
8484 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8485 Op : Entity_Id;
8486 Arg2 : Node_Id;
8488 begin
8489 Op := Entity (N);
8490 while Scope (Op) /= Standard_Standard loop
8491 Op := Homonym (Op);
8492 pragma Assert (Present (Op));
8493 end loop;
8495 Set_Entity (N, Op);
8497 if Is_Private_Type (Typ) then
8498 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
8499 Save_Interps (Right_Opnd (N), Expression (Arg2));
8501 Set_Right_Opnd (N, Arg2);
8503 Set_Etype (N, Btyp);
8504 Rewrite (N, Unchecked_Convert_To (Typ, N));
8505 Resolve (N, Typ);
8507 else
8508 Resolve_Unary_Op (N, Typ);
8509 end if;
8510 end Resolve_Intrinsic_Unary_Operator;
8512 ------------------------
8513 -- Resolve_Logical_Op --
8514 ------------------------
8516 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
8517 B_Typ : Entity_Id;
8519 begin
8520 Check_No_Direct_Boolean_Operators (N);
8522 -- Predefined operations on scalar types yield the base type. On the
8523 -- other hand, logical operations on arrays yield the type of the
8524 -- arguments (and the context).
8526 if Is_Array_Type (Typ) then
8527 B_Typ := Typ;
8528 else
8529 B_Typ := Base_Type (Typ);
8530 end if;
8532 -- The following test is required because the operands of the operation
8533 -- may be literals, in which case the resulting type appears to be
8534 -- compatible with a signed integer type, when in fact it is compatible
8535 -- only with modular types. If the context itself is universal, the
8536 -- operation is illegal.
8538 if not Valid_Boolean_Arg (Typ) then
8539 Error_Msg_N ("invalid context for logical operation", N);
8540 Set_Etype (N, Any_Type);
8541 return;
8543 elsif Typ = Any_Modular then
8544 Error_Msg_N
8545 ("no modular type available in this context", N);
8546 Set_Etype (N, Any_Type);
8547 return;
8549 elsif Is_Modular_Integer_Type (Typ)
8550 and then Etype (Left_Opnd (N)) = Universal_Integer
8551 and then Etype (Right_Opnd (N)) = Universal_Integer
8552 then
8553 Check_For_Visible_Operator (N, B_Typ);
8554 end if;
8556 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
8557 -- is active and the result type is standard Boolean (do not mess with
8558 -- ops that return a nonstandard Boolean type, because something strange
8559 -- is going on).
8561 -- Note: you might expect this replacement to be done during expansion,
8562 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
8563 -- is used, no part of the right operand of an "and" or "or" operator
8564 -- should be executed if the left operand would short-circuit the
8565 -- evaluation of the corresponding "and then" or "or else". If we left
8566 -- the replacement to expansion time, then run-time checks associated
8567 -- with such operands would be evaluated unconditionally, due to being
8568 -- before the condition prior to the rewriting as short-circuit forms
8569 -- during expansion.
8571 if Short_Circuit_And_Or
8572 and then B_Typ = Standard_Boolean
8573 and then Nkind_In (N, N_Op_And, N_Op_Or)
8574 then
8575 -- Mark the corresponding putative SCO operator as truly a logical
8576 -- (and short-circuit) operator.
8578 if Generate_SCO and then Comes_From_Source (N) then
8579 Set_SCO_Logical_Operator (N);
8580 end if;
8582 if Nkind (N) = N_Op_And then
8583 Rewrite (N,
8584 Make_And_Then (Sloc (N),
8585 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8586 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8587 Analyze_And_Resolve (N, B_Typ);
8589 -- Case of OR changed to OR ELSE
8591 else
8592 Rewrite (N,
8593 Make_Or_Else (Sloc (N),
8594 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8595 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8596 Analyze_And_Resolve (N, B_Typ);
8597 end if;
8599 -- Return now, since analysis of the rewritten ops will take care of
8600 -- other reference bookkeeping and expression folding.
8602 return;
8603 end if;
8605 Resolve (Left_Opnd (N), B_Typ);
8606 Resolve (Right_Opnd (N), B_Typ);
8608 Check_Unset_Reference (Left_Opnd (N));
8609 Check_Unset_Reference (Right_Opnd (N));
8611 Set_Etype (N, B_Typ);
8612 Generate_Operator_Reference (N, B_Typ);
8613 Eval_Logical_Op (N);
8615 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
8616 -- only when both operands have same static lower and higher bounds. Of
8617 -- course the types have to match, so only check if operands are
8618 -- compatible and the node itself has no errors.
8620 if Is_Array_Type (B_Typ)
8621 and then Nkind (N) in N_Binary_Op
8622 then
8623 declare
8624 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
8625 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
8627 begin
8628 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8629 -- operation if not needed.
8631 if Restriction_Check_Required (SPARK_05)
8632 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
8633 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
8634 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
8635 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
8636 then
8637 Check_SPARK_05_Restriction
8638 ("array types should have matching static bounds", N);
8639 end if;
8640 end;
8641 end if;
8642 end Resolve_Logical_Op;
8644 ---------------------------
8645 -- Resolve_Membership_Op --
8646 ---------------------------
8648 -- The context can only be a boolean type, and does not determine the
8649 -- arguments. Arguments should be unambiguous, but the preference rule for
8650 -- universal types applies.
8652 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
8653 pragma Warnings (Off, Typ);
8655 L : constant Node_Id := Left_Opnd (N);
8656 R : constant Node_Id := Right_Opnd (N);
8657 T : Entity_Id;
8659 procedure Resolve_Set_Membership;
8660 -- Analysis has determined a unique type for the left operand. Use it to
8661 -- resolve the disjuncts.
8663 ----------------------------
8664 -- Resolve_Set_Membership --
8665 ----------------------------
8667 procedure Resolve_Set_Membership is
8668 Alt : Node_Id;
8669 Ltyp : Entity_Id;
8671 begin
8672 -- If the left operand is overloaded, find type compatible with not
8673 -- overloaded alternative of the right operand.
8675 if Is_Overloaded (L) then
8676 Ltyp := Empty;
8677 Alt := First (Alternatives (N));
8678 while Present (Alt) loop
8679 if not Is_Overloaded (Alt) then
8680 Ltyp := Intersect_Types (L, Alt);
8681 exit;
8682 else
8683 Next (Alt);
8684 end if;
8685 end loop;
8687 -- Unclear how to resolve expression if all alternatives are also
8688 -- overloaded.
8690 if No (Ltyp) then
8691 Error_Msg_N ("ambiguous expression", N);
8692 end if;
8694 else
8695 Ltyp := Etype (L);
8696 end if;
8698 Resolve (L, Ltyp);
8700 Alt := First (Alternatives (N));
8701 while Present (Alt) loop
8703 -- Alternative is an expression, a range
8704 -- or a subtype mark.
8706 if not Is_Entity_Name (Alt)
8707 or else not Is_Type (Entity (Alt))
8708 then
8709 Resolve (Alt, Ltyp);
8710 end if;
8712 Next (Alt);
8713 end loop;
8715 -- Check for duplicates for discrete case
8717 if Is_Discrete_Type (Ltyp) then
8718 declare
8719 type Ent is record
8720 Alt : Node_Id;
8721 Val : Uint;
8722 end record;
8724 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
8725 Nalts : Nat;
8727 begin
8728 -- Loop checking duplicates. This is quadratic, but giant sets
8729 -- are unlikely in this context so it's a reasonable choice.
8731 Nalts := 0;
8732 Alt := First (Alternatives (N));
8733 while Present (Alt) loop
8734 if Is_OK_Static_Expression (Alt)
8735 and then (Nkind_In (Alt, N_Integer_Literal,
8736 N_Character_Literal)
8737 or else Nkind (Alt) in N_Has_Entity)
8738 then
8739 Nalts := Nalts + 1;
8740 Alts (Nalts) := (Alt, Expr_Value (Alt));
8742 for J in 1 .. Nalts - 1 loop
8743 if Alts (J).Val = Alts (Nalts).Val then
8744 Error_Msg_Sloc := Sloc (Alts (J).Alt);
8745 Error_Msg_N ("duplicate of value given#??", Alt);
8746 end if;
8747 end loop;
8748 end if;
8750 Alt := Next (Alt);
8751 end loop;
8752 end;
8753 end if;
8754 end Resolve_Set_Membership;
8756 -- Start of processing for Resolve_Membership_Op
8758 begin
8759 if L = Error or else R = Error then
8760 return;
8761 end if;
8763 if Present (Alternatives (N)) then
8764 Resolve_Set_Membership;
8765 goto SM_Exit;
8767 elsif not Is_Overloaded (R)
8768 and then
8769 (Etype (R) = Universal_Integer
8770 or else
8771 Etype (R) = Universal_Real)
8772 and then Is_Overloaded (L)
8773 then
8774 T := Etype (R);
8776 -- Ada 2005 (AI-251): Support the following case:
8778 -- type I is interface;
8779 -- type T is tagged ...
8781 -- function Test (O : I'Class) is
8782 -- begin
8783 -- return O in T'Class.
8784 -- end Test;
8786 -- In this case we have nothing else to do. The membership test will be
8787 -- done at run time.
8789 elsif Ada_Version >= Ada_2005
8790 and then Is_Class_Wide_Type (Etype (L))
8791 and then Is_Interface (Etype (L))
8792 and then Is_Class_Wide_Type (Etype (R))
8793 and then not Is_Interface (Etype (R))
8794 then
8795 return;
8796 else
8797 T := Intersect_Types (L, R);
8798 end if;
8800 -- If mixed-mode operations are present and operands are all literal,
8801 -- the only interpretation involves Duration, which is probably not
8802 -- the intention of the programmer.
8804 if T = Any_Fixed then
8805 T := Unique_Fixed_Point_Type (N);
8807 if T = Any_Type then
8808 return;
8809 end if;
8810 end if;
8812 Resolve (L, T);
8813 Check_Unset_Reference (L);
8815 if Nkind (R) = N_Range
8816 and then not Is_Scalar_Type (T)
8817 then
8818 Error_Msg_N ("scalar type required for range", R);
8819 end if;
8821 if Is_Entity_Name (R) then
8822 Freeze_Expression (R);
8823 else
8824 Resolve (R, T);
8825 Check_Unset_Reference (R);
8826 end if;
8828 -- Here after resolving membership operation
8830 <<SM_Exit>>
8832 Eval_Membership_Op (N);
8833 end Resolve_Membership_Op;
8835 ------------------
8836 -- Resolve_Null --
8837 ------------------
8839 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
8840 Loc : constant Source_Ptr := Sloc (N);
8842 begin
8843 -- Handle restriction against anonymous null access values This
8844 -- restriction can be turned off using -gnatdj.
8846 -- Ada 2005 (AI-231): Remove restriction
8848 if Ada_Version < Ada_2005
8849 and then not Debug_Flag_J
8850 and then Ekind (Typ) = E_Anonymous_Access_Type
8851 and then Comes_From_Source (N)
8852 then
8853 -- In the common case of a call which uses an explicitly null value
8854 -- for an access parameter, give specialized error message.
8856 if Nkind (Parent (N)) in N_Subprogram_Call then
8857 Error_Msg_N
8858 ("null is not allowed as argument for an access parameter", N);
8860 -- Standard message for all other cases (are there any?)
8862 else
8863 Error_Msg_N
8864 ("null cannot be of an anonymous access type", N);
8865 end if;
8866 end if;
8868 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
8869 -- assignment to a null-excluding object
8871 if Ada_Version >= Ada_2005
8872 and then Can_Never_Be_Null (Typ)
8873 and then Nkind (Parent (N)) = N_Assignment_Statement
8874 then
8875 if not Inside_Init_Proc then
8876 Insert_Action
8877 (Compile_Time_Constraint_Error (N,
8878 "(Ada 2005) null not allowed in null-excluding objects??"),
8879 Make_Raise_Constraint_Error (Loc,
8880 Reason => CE_Access_Check_Failed));
8881 else
8882 Insert_Action (N,
8883 Make_Raise_Constraint_Error (Loc,
8884 Reason => CE_Access_Check_Failed));
8885 end if;
8886 end if;
8888 -- In a distributed context, null for a remote access to subprogram may
8889 -- need to be replaced with a special record aggregate. In this case,
8890 -- return after having done the transformation.
8892 if (Ekind (Typ) = E_Record_Type
8893 or else Is_Remote_Access_To_Subprogram_Type (Typ))
8894 and then Remote_AST_Null_Value (N, Typ)
8895 then
8896 return;
8897 end if;
8899 -- The null literal takes its type from the context
8901 Set_Etype (N, Typ);
8902 end Resolve_Null;
8904 -----------------------
8905 -- Resolve_Op_Concat --
8906 -----------------------
8908 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
8910 -- We wish to avoid deep recursion, because concatenations are often
8911 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
8912 -- operands nonrecursively until we find something that is not a simple
8913 -- concatenation (A in this case). We resolve that, and then walk back
8914 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
8915 -- to do the rest of the work at each level. The Parent pointers allow
8916 -- us to avoid recursion, and thus avoid running out of memory. See also
8917 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
8919 NN : Node_Id := N;
8920 Op1 : Node_Id;
8922 begin
8923 -- The following code is equivalent to:
8925 -- Resolve_Op_Concat_First (NN, Typ);
8926 -- Resolve_Op_Concat_Arg (N, ...);
8927 -- Resolve_Op_Concat_Rest (N, Typ);
8929 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
8930 -- operand is a concatenation.
8932 -- Walk down left operands
8934 loop
8935 Resolve_Op_Concat_First (NN, Typ);
8936 Op1 := Left_Opnd (NN);
8937 exit when not (Nkind (Op1) = N_Op_Concat
8938 and then not Is_Array_Type (Component_Type (Typ))
8939 and then Entity (Op1) = Entity (NN));
8940 NN := Op1;
8941 end loop;
8943 -- Now (given the above example) NN is A&B and Op1 is A
8945 -- First resolve Op1 ...
8947 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
8949 -- ... then walk NN back up until we reach N (where we started), calling
8950 -- Resolve_Op_Concat_Rest along the way.
8952 loop
8953 Resolve_Op_Concat_Rest (NN, Typ);
8954 exit when NN = N;
8955 NN := Parent (NN);
8956 end loop;
8958 if Base_Type (Etype (N)) /= Standard_String then
8959 Check_SPARK_05_Restriction
8960 ("result of concatenation should have type String", N);
8961 end if;
8962 end Resolve_Op_Concat;
8964 ---------------------------
8965 -- Resolve_Op_Concat_Arg --
8966 ---------------------------
8968 procedure Resolve_Op_Concat_Arg
8969 (N : Node_Id;
8970 Arg : Node_Id;
8971 Typ : Entity_Id;
8972 Is_Comp : Boolean)
8974 Btyp : constant Entity_Id := Base_Type (Typ);
8975 Ctyp : constant Entity_Id := Component_Type (Typ);
8977 begin
8978 if In_Instance then
8979 if Is_Comp
8980 or else (not Is_Overloaded (Arg)
8981 and then Etype (Arg) /= Any_Composite
8982 and then Covers (Ctyp, Etype (Arg)))
8983 then
8984 Resolve (Arg, Ctyp);
8985 else
8986 Resolve (Arg, Btyp);
8987 end if;
8989 -- If both Array & Array and Array & Component are visible, there is a
8990 -- potential ambiguity that must be reported.
8992 elsif Has_Compatible_Type (Arg, Ctyp) then
8993 if Nkind (Arg) = N_Aggregate
8994 and then Is_Composite_Type (Ctyp)
8995 then
8996 if Is_Private_Type (Ctyp) then
8997 Resolve (Arg, Btyp);
8999 -- If the operation is user-defined and not overloaded use its
9000 -- profile. The operation may be a renaming, in which case it has
9001 -- been rewritten, and we want the original profile.
9003 elsif not Is_Overloaded (N)
9004 and then Comes_From_Source (Entity (Original_Node (N)))
9005 and then Ekind (Entity (Original_Node (N))) = E_Function
9006 then
9007 Resolve (Arg,
9008 Etype
9009 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
9010 return;
9012 -- Otherwise an aggregate may match both the array type and the
9013 -- component type.
9015 else
9016 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
9017 Set_Etype (Arg, Any_Type);
9018 end if;
9020 else
9021 if Is_Overloaded (Arg)
9022 and then Has_Compatible_Type (Arg, Typ)
9023 and then Etype (Arg) /= Any_Type
9024 then
9025 declare
9026 I : Interp_Index;
9027 It : Interp;
9028 Func : Entity_Id;
9030 begin
9031 Get_First_Interp (Arg, I, It);
9032 Func := It.Nam;
9033 Get_Next_Interp (I, It);
9035 -- Special-case the error message when the overloading is
9036 -- caused by a function that yields an array and can be
9037 -- called without parameters.
9039 if It.Nam = Func then
9040 Error_Msg_Sloc := Sloc (Func);
9041 Error_Msg_N ("ambiguous call to function#", Arg);
9042 Error_Msg_NE
9043 ("\\interpretation as call yields&", Arg, Typ);
9044 Error_Msg_NE
9045 ("\\interpretation as indexing of call yields&",
9046 Arg, Component_Type (Typ));
9048 else
9049 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
9051 Get_First_Interp (Arg, I, It);
9052 while Present (It.Nam) loop
9053 Error_Msg_Sloc := Sloc (It.Nam);
9055 if Base_Type (It.Typ) = Btyp
9056 or else
9057 Base_Type (It.Typ) = Base_Type (Ctyp)
9058 then
9059 Error_Msg_N -- CODEFIX
9060 ("\\possible interpretation#", Arg);
9061 end if;
9063 Get_Next_Interp (I, It);
9064 end loop;
9065 end if;
9066 end;
9067 end if;
9069 Resolve (Arg, Component_Type (Typ));
9071 if Nkind (Arg) = N_String_Literal then
9072 Set_Etype (Arg, Component_Type (Typ));
9073 end if;
9075 if Arg = Left_Opnd (N) then
9076 Set_Is_Component_Left_Opnd (N);
9077 else
9078 Set_Is_Component_Right_Opnd (N);
9079 end if;
9080 end if;
9082 else
9083 Resolve (Arg, Btyp);
9084 end if;
9086 -- Concatenation is restricted in SPARK: each operand must be either a
9087 -- string literal, the name of a string constant, a static character or
9088 -- string expression, or another concatenation. Arg cannot be a
9089 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
9090 -- separately on each final operand, past concatenation operations.
9092 if Is_Character_Type (Etype (Arg)) then
9093 if not Is_OK_Static_Expression (Arg) then
9094 Check_SPARK_05_Restriction
9095 ("character operand for concatenation should be static", Arg);
9096 end if;
9098 elsif Is_String_Type (Etype (Arg)) then
9099 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
9100 and then Is_Constant_Object (Entity (Arg)))
9101 and then not Is_OK_Static_Expression (Arg)
9102 then
9103 Check_SPARK_05_Restriction
9104 ("string operand for concatenation should be static", Arg);
9105 end if;
9107 -- Do not issue error on an operand that is neither a character nor a
9108 -- string, as the error is issued in Resolve_Op_Concat.
9110 else
9111 null;
9112 end if;
9114 Check_Unset_Reference (Arg);
9115 end Resolve_Op_Concat_Arg;
9117 -----------------------------
9118 -- Resolve_Op_Concat_First --
9119 -----------------------------
9121 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
9122 Btyp : constant Entity_Id := Base_Type (Typ);
9123 Op1 : constant Node_Id := Left_Opnd (N);
9124 Op2 : constant Node_Id := Right_Opnd (N);
9126 begin
9127 -- The parser folds an enormous sequence of concatenations of string
9128 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
9129 -- in the right operand. If the expression resolves to a predefined "&"
9130 -- operator, all is well. Otherwise, the parser's folding is wrong, so
9131 -- we give an error. See P_Simple_Expression in Par.Ch4.
9133 if Nkind (Op2) = N_String_Literal
9134 and then Is_Folded_In_Parser (Op2)
9135 and then Ekind (Entity (N)) = E_Function
9136 then
9137 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
9138 and then String_Length (Strval (Op1)) = 0);
9139 Error_Msg_N ("too many user-defined concatenations", N);
9140 return;
9141 end if;
9143 Set_Etype (N, Btyp);
9145 if Is_Limited_Composite (Btyp) then
9146 Error_Msg_N ("concatenation not available for limited array", N);
9147 Explain_Limited_Type (Btyp, N);
9148 end if;
9149 end Resolve_Op_Concat_First;
9151 ----------------------------
9152 -- Resolve_Op_Concat_Rest --
9153 ----------------------------
9155 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
9156 Op1 : constant Node_Id := Left_Opnd (N);
9157 Op2 : constant Node_Id := Right_Opnd (N);
9159 begin
9160 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
9162 Generate_Operator_Reference (N, Typ);
9164 if Is_String_Type (Typ) then
9165 Eval_Concatenation (N);
9166 end if;
9168 -- If this is not a static concatenation, but the result is a string
9169 -- type (and not an array of strings) ensure that static string operands
9170 -- have their subtypes properly constructed.
9172 if Nkind (N) /= N_String_Literal
9173 and then Is_Character_Type (Component_Type (Typ))
9174 then
9175 Set_String_Literal_Subtype (Op1, Typ);
9176 Set_String_Literal_Subtype (Op2, Typ);
9177 end if;
9178 end Resolve_Op_Concat_Rest;
9180 ----------------------
9181 -- Resolve_Op_Expon --
9182 ----------------------
9184 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
9185 B_Typ : constant Entity_Id := Base_Type (Typ);
9187 begin
9188 -- Catch attempts to do fixed-point exponentiation with universal
9189 -- operands, which is a case where the illegality is not caught during
9190 -- normal operator analysis. This is not done in preanalysis mode
9191 -- since the tree is not fully decorated during preanalysis.
9193 if Full_Analysis then
9194 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
9195 Error_Msg_N ("exponentiation not available for fixed point", N);
9196 return;
9198 elsif Nkind (Parent (N)) in N_Op
9199 and then Is_Fixed_Point_Type (Etype (Parent (N)))
9200 and then Etype (N) = Universal_Real
9201 and then Comes_From_Source (N)
9202 then
9203 Error_Msg_N ("exponentiation not available for fixed point", N);
9204 return;
9205 end if;
9206 end if;
9208 if Comes_From_Source (N)
9209 and then Ekind (Entity (N)) = E_Function
9210 and then Is_Imported (Entity (N))
9211 and then Is_Intrinsic_Subprogram (Entity (N))
9212 then
9213 Resolve_Intrinsic_Operator (N, Typ);
9214 return;
9215 end if;
9217 if Etype (Left_Opnd (N)) = Universal_Integer
9218 or else Etype (Left_Opnd (N)) = Universal_Real
9219 then
9220 Check_For_Visible_Operator (N, B_Typ);
9221 end if;
9223 -- We do the resolution using the base type, because intermediate values
9224 -- in expressions are always of the base type, not a subtype of it.
9226 Resolve (Left_Opnd (N), B_Typ);
9227 Resolve (Right_Opnd (N), Standard_Integer);
9229 -- For integer types, right argument must be in Natural range
9231 if Is_Integer_Type (Typ) then
9232 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
9233 end if;
9235 Check_Unset_Reference (Left_Opnd (N));
9236 Check_Unset_Reference (Right_Opnd (N));
9238 Set_Etype (N, B_Typ);
9239 Generate_Operator_Reference (N, B_Typ);
9241 Analyze_Dimension (N);
9243 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
9244 -- Evaluate the exponentiation operator for dimensioned type
9246 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
9247 else
9248 Eval_Op_Expon (N);
9249 end if;
9251 -- Set overflow checking bit. Much cleverer code needed here eventually
9252 -- and perhaps the Resolve routines should be separated for the various
9253 -- arithmetic operations, since they will need different processing. ???
9255 if Nkind (N) in N_Op then
9256 if not Overflow_Checks_Suppressed (Etype (N)) then
9257 Enable_Overflow_Check (N);
9258 end if;
9259 end if;
9260 end Resolve_Op_Expon;
9262 --------------------
9263 -- Resolve_Op_Not --
9264 --------------------
9266 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
9267 B_Typ : Entity_Id;
9269 function Parent_Is_Boolean return Boolean;
9270 -- This function determines if the parent node is a boolean operator or
9271 -- operation (comparison op, membership test, or short circuit form) and
9272 -- the not in question is the left operand of this operation. Note that
9273 -- if the not is in parens, then false is returned.
9275 -----------------------
9276 -- Parent_Is_Boolean --
9277 -----------------------
9279 function Parent_Is_Boolean return Boolean is
9280 begin
9281 if Paren_Count (N) /= 0 then
9282 return False;
9284 else
9285 case Nkind (Parent (N)) is
9286 when N_Op_And |
9287 N_Op_Eq |
9288 N_Op_Ge |
9289 N_Op_Gt |
9290 N_Op_Le |
9291 N_Op_Lt |
9292 N_Op_Ne |
9293 N_Op_Or |
9294 N_Op_Xor |
9295 N_In |
9296 N_Not_In |
9297 N_And_Then |
9298 N_Or_Else =>
9300 return Left_Opnd (Parent (N)) = N;
9302 when others =>
9303 return False;
9304 end case;
9305 end if;
9306 end Parent_Is_Boolean;
9308 -- Start of processing for Resolve_Op_Not
9310 begin
9311 -- Predefined operations on scalar types yield the base type. On the
9312 -- other hand, logical operations on arrays yield the type of the
9313 -- arguments (and the context).
9315 if Is_Array_Type (Typ) then
9316 B_Typ := Typ;
9317 else
9318 B_Typ := Base_Type (Typ);
9319 end if;
9321 -- Straightforward case of incorrect arguments
9323 if not Valid_Boolean_Arg (Typ) then
9324 Error_Msg_N ("invalid operand type for operator&", N);
9325 Set_Etype (N, Any_Type);
9326 return;
9328 -- Special case of probable missing parens
9330 elsif Typ = Universal_Integer or else Typ = Any_Modular then
9331 if Parent_Is_Boolean then
9332 Error_Msg_N
9333 ("operand of not must be enclosed in parentheses",
9334 Right_Opnd (N));
9335 else
9336 Error_Msg_N
9337 ("no modular type available in this context", N);
9338 end if;
9340 Set_Etype (N, Any_Type);
9341 return;
9343 -- OK resolution of NOT
9345 else
9346 -- Warn if non-boolean types involved. This is a case like not a < b
9347 -- where a and b are modular, where we will get (not a) < b and most
9348 -- likely not (a < b) was intended.
9350 if Warn_On_Questionable_Missing_Parens
9351 and then not Is_Boolean_Type (Typ)
9352 and then Parent_Is_Boolean
9353 then
9354 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
9355 end if;
9357 -- Warn on double negation if checking redundant constructs
9359 if Warn_On_Redundant_Constructs
9360 and then Comes_From_Source (N)
9361 and then Comes_From_Source (Right_Opnd (N))
9362 and then Root_Type (Typ) = Standard_Boolean
9363 and then Nkind (Right_Opnd (N)) = N_Op_Not
9364 then
9365 Error_Msg_N ("redundant double negation?r?", N);
9366 end if;
9368 -- Complete resolution and evaluation of NOT
9370 Resolve (Right_Opnd (N), B_Typ);
9371 Check_Unset_Reference (Right_Opnd (N));
9372 Set_Etype (N, B_Typ);
9373 Generate_Operator_Reference (N, B_Typ);
9374 Eval_Op_Not (N);
9375 end if;
9376 end Resolve_Op_Not;
9378 -----------------------------
9379 -- Resolve_Operator_Symbol --
9380 -----------------------------
9382 -- Nothing to be done, all resolved already
9384 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
9385 pragma Warnings (Off, N);
9386 pragma Warnings (Off, Typ);
9388 begin
9389 null;
9390 end Resolve_Operator_Symbol;
9392 ----------------------------------
9393 -- Resolve_Qualified_Expression --
9394 ----------------------------------
9396 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
9397 pragma Warnings (Off, Typ);
9399 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
9400 Expr : constant Node_Id := Expression (N);
9402 begin
9403 Resolve (Expr, Target_Typ);
9405 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9406 -- operation if not needed.
9408 if Restriction_Check_Required (SPARK_05)
9409 and then Is_Array_Type (Target_Typ)
9410 and then Is_Array_Type (Etype (Expr))
9411 and then Etype (Expr) /= Any_Composite -- or else Expr in error
9412 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
9413 then
9414 Check_SPARK_05_Restriction
9415 ("array types should have matching static bounds", N);
9416 end if;
9418 -- A qualified expression requires an exact match of the type, class-
9419 -- wide matching is not allowed. However, if the qualifying type is
9420 -- specific and the expression has a class-wide type, it may still be
9421 -- okay, since it can be the result of the expansion of a call to a
9422 -- dispatching function, so we also have to check class-wideness of the
9423 -- type of the expression's original node.
9425 if (Is_Class_Wide_Type (Target_Typ)
9426 or else
9427 (Is_Class_Wide_Type (Etype (Expr))
9428 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
9429 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
9430 then
9431 Wrong_Type (Expr, Target_Typ);
9432 end if;
9434 -- If the target type is unconstrained, then we reset the type of the
9435 -- result from the type of the expression. For other cases, the actual
9436 -- subtype of the expression is the target type.
9438 if Is_Composite_Type (Target_Typ)
9439 and then not Is_Constrained (Target_Typ)
9440 then
9441 Set_Etype (N, Etype (Expr));
9442 end if;
9444 Analyze_Dimension (N);
9445 Eval_Qualified_Expression (N);
9447 -- If we still have a qualified expression after the static evaluation,
9448 -- then apply a scalar range check if needed. The reason that we do this
9449 -- after the Eval call is that otherwise, the application of the range
9450 -- check may convert an illegal static expression and result in warning
9451 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
9453 if Nkind (N) = N_Qualified_Expression and then Is_Scalar_Type (Typ) then
9454 Apply_Scalar_Range_Check (Expr, Typ);
9455 end if;
9457 -- Finally, check whether a predicate applies to the target type. This
9458 -- comes from AI12-0100. As for type conversions, check the enclosing
9459 -- context to prevent an infinite expansion.
9461 if Has_Predicates (Target_Typ) then
9462 if Nkind (Parent (N)) = N_Function_Call
9463 and then Present (Name (Parent (N)))
9464 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
9465 or else
9466 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
9467 then
9468 null;
9470 elsif Nkind (N) = N_Qualified_Expression then
9471 Apply_Predicate_Check (N, Target_Typ);
9472 end if;
9473 end if;
9474 end Resolve_Qualified_Expression;
9476 ------------------------------
9477 -- Resolve_Raise_Expression --
9478 ------------------------------
9480 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
9481 begin
9482 if Typ = Raise_Type then
9483 Error_Msg_N ("cannot find unique type for raise expression", N);
9484 Set_Etype (N, Any_Type);
9485 else
9486 Set_Etype (N, Typ);
9487 end if;
9488 end Resolve_Raise_Expression;
9490 -------------------
9491 -- Resolve_Range --
9492 -------------------
9494 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
9495 L : constant Node_Id := Low_Bound (N);
9496 H : constant Node_Id := High_Bound (N);
9498 function First_Last_Ref return Boolean;
9499 -- Returns True if N is of the form X'First .. X'Last where X is the
9500 -- same entity for both attributes.
9502 --------------------
9503 -- First_Last_Ref --
9504 --------------------
9506 function First_Last_Ref return Boolean is
9507 Lorig : constant Node_Id := Original_Node (L);
9508 Horig : constant Node_Id := Original_Node (H);
9510 begin
9511 if Nkind (Lorig) = N_Attribute_Reference
9512 and then Nkind (Horig) = N_Attribute_Reference
9513 and then Attribute_Name (Lorig) = Name_First
9514 and then Attribute_Name (Horig) = Name_Last
9515 then
9516 declare
9517 PL : constant Node_Id := Prefix (Lorig);
9518 PH : constant Node_Id := Prefix (Horig);
9519 begin
9520 if Is_Entity_Name (PL)
9521 and then Is_Entity_Name (PH)
9522 and then Entity (PL) = Entity (PH)
9523 then
9524 return True;
9525 end if;
9526 end;
9527 end if;
9529 return False;
9530 end First_Last_Ref;
9532 -- Start of processing for Resolve_Range
9534 begin
9535 Set_Etype (N, Typ);
9536 Resolve (L, Typ);
9537 Resolve (H, Typ);
9539 -- Check for inappropriate range on unordered enumeration type
9541 if Bad_Unordered_Enumeration_Reference (N, Typ)
9543 -- Exclude X'First .. X'Last if X is the same entity for both
9545 and then not First_Last_Ref
9546 then
9547 Error_Msg_Sloc := Sloc (Typ);
9548 Error_Msg_NE
9549 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
9550 end if;
9552 Check_Unset_Reference (L);
9553 Check_Unset_Reference (H);
9555 -- We have to check the bounds for being within the base range as
9556 -- required for a non-static context. Normally this is automatic and
9557 -- done as part of evaluating expressions, but the N_Range node is an
9558 -- exception, since in GNAT we consider this node to be a subexpression,
9559 -- even though in Ada it is not. The circuit in Sem_Eval could check for
9560 -- this, but that would put the test on the main evaluation path for
9561 -- expressions.
9563 Check_Non_Static_Context (L);
9564 Check_Non_Static_Context (H);
9566 -- Check for an ambiguous range over character literals. This will
9567 -- happen with a membership test involving only literals.
9569 if Typ = Any_Character then
9570 Ambiguous_Character (L);
9571 Set_Etype (N, Any_Type);
9572 return;
9573 end if;
9575 -- If bounds are static, constant-fold them, so size computations are
9576 -- identical between front-end and back-end. Do not perform this
9577 -- transformation while analyzing generic units, as type information
9578 -- would be lost when reanalyzing the constant node in the instance.
9580 if Is_Discrete_Type (Typ) and then Expander_Active then
9581 if Is_OK_Static_Expression (L) then
9582 Fold_Uint (L, Expr_Value (L), Is_OK_Static_Expression (L));
9583 end if;
9585 if Is_OK_Static_Expression (H) then
9586 Fold_Uint (H, Expr_Value (H), Is_OK_Static_Expression (H));
9587 end if;
9588 end if;
9589 end Resolve_Range;
9591 --------------------------
9592 -- Resolve_Real_Literal --
9593 --------------------------
9595 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
9596 Actual_Typ : constant Entity_Id := Etype (N);
9598 begin
9599 -- Special processing for fixed-point literals to make sure that the
9600 -- value is an exact multiple of small where this is required. We skip
9601 -- this for the universal real case, and also for generic types.
9603 if Is_Fixed_Point_Type (Typ)
9604 and then Typ /= Universal_Fixed
9605 and then Typ /= Any_Fixed
9606 and then not Is_Generic_Type (Typ)
9607 then
9608 declare
9609 Val : constant Ureal := Realval (N);
9610 Cintr : constant Ureal := Val / Small_Value (Typ);
9611 Cint : constant Uint := UR_Trunc (Cintr);
9612 Den : constant Uint := Norm_Den (Cintr);
9613 Stat : Boolean;
9615 begin
9616 -- Case of literal is not an exact multiple of the Small
9618 if Den /= 1 then
9620 -- For a source program literal for a decimal fixed-point type,
9621 -- this is statically illegal (RM 4.9(36)).
9623 if Is_Decimal_Fixed_Point_Type (Typ)
9624 and then Actual_Typ = Universal_Real
9625 and then Comes_From_Source (N)
9626 then
9627 Error_Msg_N ("value has extraneous low order digits", N);
9628 end if;
9630 -- Generate a warning if literal from source
9632 if Is_OK_Static_Expression (N)
9633 and then Warn_On_Bad_Fixed_Value
9634 then
9635 Error_Msg_N
9636 ("?b?static fixed-point value is not a multiple of Small!",
9638 end if;
9640 -- Replace literal by a value that is the exact representation
9641 -- of a value of the type, i.e. a multiple of the small value,
9642 -- by truncation, since Machine_Rounds is false for all GNAT
9643 -- fixed-point types (RM 4.9(38)).
9645 Stat := Is_OK_Static_Expression (N);
9646 Rewrite (N,
9647 Make_Real_Literal (Sloc (N),
9648 Realval => Small_Value (Typ) * Cint));
9650 Set_Is_Static_Expression (N, Stat);
9651 end if;
9653 -- In all cases, set the corresponding integer field
9655 Set_Corresponding_Integer_Value (N, Cint);
9656 end;
9657 end if;
9659 -- Now replace the actual type by the expected type as usual
9661 Set_Etype (N, Typ);
9662 Eval_Real_Literal (N);
9663 end Resolve_Real_Literal;
9665 -----------------------
9666 -- Resolve_Reference --
9667 -----------------------
9669 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
9670 P : constant Node_Id := Prefix (N);
9672 begin
9673 -- Replace general access with specific type
9675 if Ekind (Etype (N)) = E_Allocator_Type then
9676 Set_Etype (N, Base_Type (Typ));
9677 end if;
9679 Resolve (P, Designated_Type (Etype (N)));
9681 -- If we are taking the reference of a volatile entity, then treat it as
9682 -- a potential modification of this entity. This is too conservative,
9683 -- but necessary because remove side effects can cause transformations
9684 -- of normal assignments into reference sequences that otherwise fail to
9685 -- notice the modification.
9687 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
9688 Note_Possible_Modification (P, Sure => False);
9689 end if;
9690 end Resolve_Reference;
9692 --------------------------------
9693 -- Resolve_Selected_Component --
9694 --------------------------------
9696 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
9697 Comp : Entity_Id;
9698 Comp1 : Entity_Id := Empty; -- prevent junk warning
9699 P : constant Node_Id := Prefix (N);
9700 S : constant Node_Id := Selector_Name (N);
9701 T : Entity_Id := Etype (P);
9702 I : Interp_Index;
9703 I1 : Interp_Index := 0; -- prevent junk warning
9704 It : Interp;
9705 It1 : Interp;
9706 Found : Boolean;
9708 function Init_Component return Boolean;
9709 -- Check whether this is the initialization of a component within an
9710 -- init proc (by assignment or call to another init proc). If true,
9711 -- there is no need for a discriminant check.
9713 --------------------
9714 -- Init_Component --
9715 --------------------
9717 function Init_Component return Boolean is
9718 begin
9719 return Inside_Init_Proc
9720 and then Nkind (Prefix (N)) = N_Identifier
9721 and then Chars (Prefix (N)) = Name_uInit
9722 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
9723 end Init_Component;
9725 -- Start of processing for Resolve_Selected_Component
9727 begin
9728 if Is_Overloaded (P) then
9730 -- Use the context type to select the prefix that has a selector
9731 -- of the correct name and type.
9733 Found := False;
9734 Get_First_Interp (P, I, It);
9736 Search : while Present (It.Typ) loop
9737 if Is_Access_Type (It.Typ) then
9738 T := Designated_Type (It.Typ);
9739 else
9740 T := It.Typ;
9741 end if;
9743 -- Locate selected component. For a private prefix the selector
9744 -- can denote a discriminant.
9746 if Is_Record_Type (T) or else Is_Private_Type (T) then
9748 -- The visible components of a class-wide type are those of
9749 -- the root type.
9751 if Is_Class_Wide_Type (T) then
9752 T := Etype (T);
9753 end if;
9755 Comp := First_Entity (T);
9756 while Present (Comp) loop
9757 if Chars (Comp) = Chars (S)
9758 and then Covers (Typ, Etype (Comp))
9759 then
9760 if not Found then
9761 Found := True;
9762 I1 := I;
9763 It1 := It;
9764 Comp1 := Comp;
9766 else
9767 It := Disambiguate (P, I1, I, Any_Type);
9769 if It = No_Interp then
9770 Error_Msg_N
9771 ("ambiguous prefix for selected component", N);
9772 Set_Etype (N, Typ);
9773 return;
9775 else
9776 It1 := It;
9778 -- There may be an implicit dereference. Retrieve
9779 -- designated record type.
9781 if Is_Access_Type (It1.Typ) then
9782 T := Designated_Type (It1.Typ);
9783 else
9784 T := It1.Typ;
9785 end if;
9787 if Scope (Comp1) /= T then
9789 -- Resolution chooses the new interpretation.
9790 -- Find the component with the right name.
9792 Comp1 := First_Entity (T);
9793 while Present (Comp1)
9794 and then Chars (Comp1) /= Chars (S)
9795 loop
9796 Comp1 := Next_Entity (Comp1);
9797 end loop;
9798 end if;
9800 exit Search;
9801 end if;
9802 end if;
9803 end if;
9805 Comp := Next_Entity (Comp);
9806 end loop;
9807 end if;
9809 Get_Next_Interp (I, It);
9810 end loop Search;
9812 -- There must be a legal interpretation at this point
9814 pragma Assert (Found);
9815 Resolve (P, It1.Typ);
9816 Set_Etype (N, Typ);
9817 Set_Entity_With_Checks (S, Comp1);
9819 else
9820 -- Resolve prefix with its type
9822 Resolve (P, T);
9823 end if;
9825 -- Generate cross-reference. We needed to wait until full overloading
9826 -- resolution was complete to do this, since otherwise we can't tell if
9827 -- we are an lvalue or not.
9829 if May_Be_Lvalue (N) then
9830 Generate_Reference (Entity (S), S, 'm');
9831 else
9832 Generate_Reference (Entity (S), S, 'r');
9833 end if;
9835 -- If prefix is an access type, the node will be transformed into an
9836 -- explicit dereference during expansion. The type of the node is the
9837 -- designated type of that of the prefix.
9839 if Is_Access_Type (Etype (P)) then
9840 T := Designated_Type (Etype (P));
9841 Check_Fully_Declared_Prefix (T, P);
9842 else
9843 T := Etype (P);
9844 end if;
9846 -- Set flag for expander if discriminant check required on a component
9847 -- appearing within a variant.
9849 if Has_Discriminants (T)
9850 and then Ekind (Entity (S)) = E_Component
9851 and then Present (Original_Record_Component (Entity (S)))
9852 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
9853 and then
9854 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
9855 and then not Discriminant_Checks_Suppressed (T)
9856 and then not Init_Component
9857 then
9858 Set_Do_Discriminant_Check (N);
9859 end if;
9861 if Ekind (Entity (S)) = E_Void then
9862 Error_Msg_N ("premature use of component", S);
9863 end if;
9865 -- If the prefix is a record conversion, this may be a renamed
9866 -- discriminant whose bounds differ from those of the original
9867 -- one, so we must ensure that a range check is performed.
9869 if Nkind (P) = N_Type_Conversion
9870 and then Ekind (Entity (S)) = E_Discriminant
9871 and then Is_Discrete_Type (Typ)
9872 then
9873 Set_Etype (N, Base_Type (Typ));
9874 end if;
9876 -- Note: No Eval processing is required, because the prefix is of a
9877 -- record type, or protected type, and neither can possibly be static.
9879 -- If the record type is atomic, and the component is non-atomic, then
9880 -- this is worth a warning, since we have a situation where the access
9881 -- to the component may cause extra read/writes of the atomic array
9882 -- object, or partial word accesses, both of which may be unexpected.
9884 if Nkind (N) = N_Selected_Component
9885 and then Is_Atomic_Ref_With_Address (N)
9886 and then not Is_Atomic (Entity (S))
9887 and then not Is_Atomic (Etype (Entity (S)))
9888 then
9889 Error_Msg_N
9890 ("??access to non-atomic component of atomic record",
9891 Prefix (N));
9892 Error_Msg_N
9893 ("\??may cause unexpected accesses to atomic object",
9894 Prefix (N));
9895 end if;
9897 Analyze_Dimension (N);
9898 end Resolve_Selected_Component;
9900 -------------------
9901 -- Resolve_Shift --
9902 -------------------
9904 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
9905 B_Typ : constant Entity_Id := Base_Type (Typ);
9906 L : constant Node_Id := Left_Opnd (N);
9907 R : constant Node_Id := Right_Opnd (N);
9909 begin
9910 -- We do the resolution using the base type, because intermediate values
9911 -- in expressions always are of the base type, not a subtype of it.
9913 Resolve (L, B_Typ);
9914 Resolve (R, Standard_Natural);
9916 Check_Unset_Reference (L);
9917 Check_Unset_Reference (R);
9919 Set_Etype (N, B_Typ);
9920 Generate_Operator_Reference (N, B_Typ);
9921 Eval_Shift (N);
9922 end Resolve_Shift;
9924 ---------------------------
9925 -- Resolve_Short_Circuit --
9926 ---------------------------
9928 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
9929 B_Typ : constant Entity_Id := Base_Type (Typ);
9930 L : constant Node_Id := Left_Opnd (N);
9931 R : constant Node_Id := Right_Opnd (N);
9933 begin
9934 -- Ensure all actions associated with the left operand (e.g.
9935 -- finalization of transient controlled objects) are fully evaluated
9936 -- locally within an expression with actions. This is particularly
9937 -- helpful for coverage analysis. However this should not happen in
9938 -- generics or if Minimize_Expression_With_Actions is set.
9940 if Expander_Active and not Minimize_Expression_With_Actions then
9941 declare
9942 Reloc_L : constant Node_Id := Relocate_Node (L);
9943 begin
9944 Save_Interps (Old_N => L, New_N => Reloc_L);
9946 Rewrite (L,
9947 Make_Expression_With_Actions (Sloc (L),
9948 Actions => New_List,
9949 Expression => Reloc_L));
9951 -- Set Comes_From_Source on L to preserve warnings for unset
9952 -- reference.
9954 Set_Comes_From_Source (L, Comes_From_Source (Reloc_L));
9955 end;
9956 end if;
9958 Resolve (L, B_Typ);
9959 Resolve (R, B_Typ);
9961 -- Check for issuing warning for always False assert/check, this happens
9962 -- when assertions are turned off, in which case the pragma Assert/Check
9963 -- was transformed into:
9965 -- if False and then <condition> then ...
9967 -- and we detect this pattern
9969 if Warn_On_Assertion_Failure
9970 and then Is_Entity_Name (R)
9971 and then Entity (R) = Standard_False
9972 and then Nkind (Parent (N)) = N_If_Statement
9973 and then Nkind (N) = N_And_Then
9974 and then Is_Entity_Name (L)
9975 and then Entity (L) = Standard_False
9976 then
9977 declare
9978 Orig : constant Node_Id := Original_Node (Parent (N));
9980 begin
9981 -- Special handling of Asssert pragma
9983 if Nkind (Orig) = N_Pragma
9984 and then Pragma_Name (Orig) = Name_Assert
9985 then
9986 declare
9987 Expr : constant Node_Id :=
9988 Original_Node
9989 (Expression
9990 (First (Pragma_Argument_Associations (Orig))));
9992 begin
9993 -- Don't warn if original condition is explicit False,
9994 -- since obviously the failure is expected in this case.
9996 if Is_Entity_Name (Expr)
9997 and then Entity (Expr) = Standard_False
9998 then
9999 null;
10001 -- Issue warning. We do not want the deletion of the
10002 -- IF/AND-THEN to take this message with it. We achieve this
10003 -- by making sure that the expanded code points to the Sloc
10004 -- of the expression, not the original pragma.
10006 else
10007 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
10008 -- The source location of the expression is not usually
10009 -- the best choice here. For example, it gets located on
10010 -- the last AND keyword in a chain of boolean expressiond
10011 -- AND'ed together. It is best to put the message on the
10012 -- first character of the assertion, which is the effect
10013 -- of the First_Node call here.
10015 Error_Msg_F
10016 ("?A?assertion would fail at run time!",
10017 Expression
10018 (First (Pragma_Argument_Associations (Orig))));
10019 end if;
10020 end;
10022 -- Similar processing for Check pragma
10024 elsif Nkind (Orig) = N_Pragma
10025 and then Pragma_Name (Orig) = Name_Check
10026 then
10027 -- Don't want to warn if original condition is explicit False
10029 declare
10030 Expr : constant Node_Id :=
10031 Original_Node
10032 (Expression
10033 (Next (First (Pragma_Argument_Associations (Orig)))));
10034 begin
10035 if Is_Entity_Name (Expr)
10036 and then Entity (Expr) = Standard_False
10037 then
10038 null;
10040 -- Post warning
10042 else
10043 -- Again use Error_Msg_F rather than Error_Msg_N, see
10044 -- comment above for an explanation of why we do this.
10046 Error_Msg_F
10047 ("?A?check would fail at run time!",
10048 Expression
10049 (Last (Pragma_Argument_Associations (Orig))));
10050 end if;
10051 end;
10052 end if;
10053 end;
10054 end if;
10056 -- Continue with processing of short circuit
10058 Check_Unset_Reference (L);
10059 Check_Unset_Reference (R);
10061 Set_Etype (N, B_Typ);
10062 Eval_Short_Circuit (N);
10063 end Resolve_Short_Circuit;
10065 -------------------
10066 -- Resolve_Slice --
10067 -------------------
10069 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
10070 Drange : constant Node_Id := Discrete_Range (N);
10071 Name : constant Node_Id := Prefix (N);
10072 Array_Type : Entity_Id := Empty;
10073 Dexpr : Node_Id := Empty;
10074 Index_Type : Entity_Id;
10076 begin
10077 if Is_Overloaded (Name) then
10079 -- Use the context type to select the prefix that yields the correct
10080 -- array type.
10082 declare
10083 I : Interp_Index;
10084 I1 : Interp_Index := 0;
10085 It : Interp;
10086 P : constant Node_Id := Prefix (N);
10087 Found : Boolean := False;
10089 begin
10090 Get_First_Interp (P, I, It);
10091 while Present (It.Typ) loop
10092 if (Is_Array_Type (It.Typ)
10093 and then Covers (Typ, It.Typ))
10094 or else (Is_Access_Type (It.Typ)
10095 and then Is_Array_Type (Designated_Type (It.Typ))
10096 and then Covers (Typ, Designated_Type (It.Typ)))
10097 then
10098 if Found then
10099 It := Disambiguate (P, I1, I, Any_Type);
10101 if It = No_Interp then
10102 Error_Msg_N ("ambiguous prefix for slicing", N);
10103 Set_Etype (N, Typ);
10104 return;
10105 else
10106 Found := True;
10107 Array_Type := It.Typ;
10108 I1 := I;
10109 end if;
10110 else
10111 Found := True;
10112 Array_Type := It.Typ;
10113 I1 := I;
10114 end if;
10115 end if;
10117 Get_Next_Interp (I, It);
10118 end loop;
10119 end;
10121 else
10122 Array_Type := Etype (Name);
10123 end if;
10125 Resolve (Name, Array_Type);
10127 if Is_Access_Type (Array_Type) then
10128 Apply_Access_Check (N);
10129 Array_Type := Designated_Type (Array_Type);
10131 -- If the prefix is an access to an unconstrained array, we must use
10132 -- the actual subtype of the object to perform the index checks. The
10133 -- object denoted by the prefix is implicit in the node, so we build
10134 -- an explicit representation for it in order to compute the actual
10135 -- subtype.
10137 if not Is_Constrained (Array_Type) then
10138 Remove_Side_Effects (Prefix (N));
10140 declare
10141 Obj : constant Node_Id :=
10142 Make_Explicit_Dereference (Sloc (N),
10143 Prefix => New_Copy_Tree (Prefix (N)));
10144 begin
10145 Set_Etype (Obj, Array_Type);
10146 Set_Parent (Obj, Parent (N));
10147 Array_Type := Get_Actual_Subtype (Obj);
10148 end;
10149 end if;
10151 elsif Is_Entity_Name (Name)
10152 or else Nkind (Name) = N_Explicit_Dereference
10153 or else (Nkind (Name) = N_Function_Call
10154 and then not Is_Constrained (Etype (Name)))
10155 then
10156 Array_Type := Get_Actual_Subtype (Name);
10158 -- If the name is a selected component that depends on discriminants,
10159 -- build an actual subtype for it. This can happen only when the name
10160 -- itself is overloaded; otherwise the actual subtype is created when
10161 -- the selected component is analyzed.
10163 elsif Nkind (Name) = N_Selected_Component
10164 and then Full_Analysis
10165 and then Depends_On_Discriminant (First_Index (Array_Type))
10166 then
10167 declare
10168 Act_Decl : constant Node_Id :=
10169 Build_Actual_Subtype_Of_Component (Array_Type, Name);
10170 begin
10171 Insert_Action (N, Act_Decl);
10172 Array_Type := Defining_Identifier (Act_Decl);
10173 end;
10175 -- Maybe this should just be "else", instead of checking for the
10176 -- specific case of slice??? This is needed for the case where the
10177 -- prefix is an Image attribute, which gets expanded to a slice, and so
10178 -- has a constrained subtype which we want to use for the slice range
10179 -- check applied below (the range check won't get done if the
10180 -- unconstrained subtype of the 'Image is used).
10182 elsif Nkind (Name) = N_Slice then
10183 Array_Type := Etype (Name);
10184 end if;
10186 -- Obtain the type of the array index
10188 if Ekind (Array_Type) = E_String_Literal_Subtype then
10189 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
10190 else
10191 Index_Type := Etype (First_Index (Array_Type));
10192 end if;
10194 -- If name was overloaded, set slice type correctly now
10196 Set_Etype (N, Array_Type);
10198 -- Handle the generation of a range check that compares the array index
10199 -- against the discrete_range. The check is not applied to internally
10200 -- built nodes associated with the expansion of dispatch tables. Check
10201 -- that Ada.Tags has already been loaded to avoid extra dependencies on
10202 -- the unit.
10204 if Tagged_Type_Expansion
10205 and then RTU_Loaded (Ada_Tags)
10206 and then Nkind (Prefix (N)) = N_Selected_Component
10207 and then Present (Entity (Selector_Name (Prefix (N))))
10208 and then Entity (Selector_Name (Prefix (N))) =
10209 RTE_Record_Component (RE_Prims_Ptr)
10210 then
10211 null;
10213 -- The discrete_range is specified by a subtype indication. Create a
10214 -- shallow copy and inherit the type, parent and source location from
10215 -- the discrete_range. This ensures that the range check is inserted
10216 -- relative to the slice and that the runtime exception points to the
10217 -- proper construct.
10219 elsif Is_Entity_Name (Drange) then
10220 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
10222 Set_Etype (Dexpr, Etype (Drange));
10223 Set_Parent (Dexpr, Parent (Drange));
10224 Set_Sloc (Dexpr, Sloc (Drange));
10226 -- The discrete_range is a regular range. Resolve the bounds and remove
10227 -- their side effects.
10229 else
10230 Resolve (Drange, Base_Type (Index_Type));
10232 if Nkind (Drange) = N_Range then
10233 Force_Evaluation (Low_Bound (Drange));
10234 Force_Evaluation (High_Bound (Drange));
10236 Dexpr := Drange;
10237 end if;
10238 end if;
10240 if Present (Dexpr) then
10241 Apply_Range_Check (Dexpr, Index_Type);
10242 end if;
10244 Set_Slice_Subtype (N);
10246 -- Check bad use of type with predicates
10248 declare
10249 Subt : Entity_Id;
10251 begin
10252 if Nkind (Drange) = N_Subtype_Indication
10253 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
10254 then
10255 Subt := Entity (Subtype_Mark (Drange));
10256 else
10257 Subt := Etype (Drange);
10258 end if;
10260 if Has_Predicates (Subt) then
10261 Bad_Predicated_Subtype_Use
10262 ("subtype& has predicate, not allowed in slice", Drange, Subt);
10263 end if;
10264 end;
10266 -- Otherwise here is where we check suspicious indexes
10268 if Nkind (Drange) = N_Range then
10269 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
10270 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
10271 end if;
10273 Analyze_Dimension (N);
10274 Eval_Slice (N);
10275 end Resolve_Slice;
10277 ----------------------------
10278 -- Resolve_String_Literal --
10279 ----------------------------
10281 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
10282 C_Typ : constant Entity_Id := Component_Type (Typ);
10283 R_Typ : constant Entity_Id := Root_Type (C_Typ);
10284 Loc : constant Source_Ptr := Sloc (N);
10285 Str : constant String_Id := Strval (N);
10286 Strlen : constant Nat := String_Length (Str);
10287 Subtype_Id : Entity_Id;
10288 Need_Check : Boolean;
10290 begin
10291 -- For a string appearing in a concatenation, defer creation of the
10292 -- string_literal_subtype until the end of the resolution of the
10293 -- concatenation, because the literal may be constant-folded away. This
10294 -- is a useful optimization for long concatenation expressions.
10296 -- If the string is an aggregate built for a single character (which
10297 -- happens in a non-static context) or a is null string to which special
10298 -- checks may apply, we build the subtype. Wide strings must also get a
10299 -- string subtype if they come from a one character aggregate. Strings
10300 -- generated by attributes might be static, but it is often hard to
10301 -- determine whether the enclosing context is static, so we generate
10302 -- subtypes for them as well, thus losing some rarer optimizations ???
10303 -- Same for strings that come from a static conversion.
10305 Need_Check :=
10306 (Strlen = 0 and then Typ /= Standard_String)
10307 or else Nkind (Parent (N)) /= N_Op_Concat
10308 or else (N /= Left_Opnd (Parent (N))
10309 and then N /= Right_Opnd (Parent (N)))
10310 or else ((Typ = Standard_Wide_String
10311 or else Typ = Standard_Wide_Wide_String)
10312 and then Nkind (Original_Node (N)) /= N_String_Literal);
10314 -- If the resolving type is itself a string literal subtype, we can just
10315 -- reuse it, since there is no point in creating another.
10317 if Ekind (Typ) = E_String_Literal_Subtype then
10318 Subtype_Id := Typ;
10320 elsif Nkind (Parent (N)) = N_Op_Concat
10321 and then not Need_Check
10322 and then not Nkind_In (Original_Node (N), N_Character_Literal,
10323 N_Attribute_Reference,
10324 N_Qualified_Expression,
10325 N_Type_Conversion)
10326 then
10327 Subtype_Id := Typ;
10329 -- Do not generate a string literal subtype for the default expression
10330 -- of a formal parameter in GNATprove mode. This is because the string
10331 -- subtype is associated with the freezing actions of the subprogram,
10332 -- however freezing is disabled in GNATprove mode and as a result the
10333 -- subtype is unavailable.
10335 elsif GNATprove_Mode
10336 and then Nkind (Parent (N)) = N_Parameter_Specification
10337 then
10338 Subtype_Id := Typ;
10340 -- Otherwise we must create a string literal subtype. Note that the
10341 -- whole idea of string literal subtypes is simply to avoid the need
10342 -- for building a full fledged array subtype for each literal.
10344 else
10345 Set_String_Literal_Subtype (N, Typ);
10346 Subtype_Id := Etype (N);
10347 end if;
10349 if Nkind (Parent (N)) /= N_Op_Concat
10350 or else Need_Check
10351 then
10352 Set_Etype (N, Subtype_Id);
10353 Eval_String_Literal (N);
10354 end if;
10356 if Is_Limited_Composite (Typ)
10357 or else Is_Private_Composite (Typ)
10358 then
10359 Error_Msg_N ("string literal not available for private array", N);
10360 Set_Etype (N, Any_Type);
10361 return;
10362 end if;
10364 -- The validity of a null string has been checked in the call to
10365 -- Eval_String_Literal.
10367 if Strlen = 0 then
10368 return;
10370 -- Always accept string literal with component type Any_Character, which
10371 -- occurs in error situations and in comparisons of literals, both of
10372 -- which should accept all literals.
10374 elsif R_Typ = Any_Character then
10375 return;
10377 -- If the type is bit-packed, then we always transform the string
10378 -- literal into a full fledged aggregate.
10380 elsif Is_Bit_Packed_Array (Typ) then
10381 null;
10383 -- Deal with cases of Wide_Wide_String, Wide_String, and String
10385 else
10386 -- For Standard.Wide_Wide_String, or any other type whose component
10387 -- type is Standard.Wide_Wide_Character, we know that all the
10388 -- characters in the string must be acceptable, since the parser
10389 -- accepted the characters as valid character literals.
10391 if R_Typ = Standard_Wide_Wide_Character then
10392 null;
10394 -- For the case of Standard.String, or any other type whose component
10395 -- type is Standard.Character, we must make sure that there are no
10396 -- wide characters in the string, i.e. that it is entirely composed
10397 -- of characters in range of type Character.
10399 -- If the string literal is the result of a static concatenation, the
10400 -- test has already been performed on the components, and need not be
10401 -- repeated.
10403 elsif R_Typ = Standard_Character
10404 and then Nkind (Original_Node (N)) /= N_Op_Concat
10405 then
10406 for J in 1 .. Strlen loop
10407 if not In_Character_Range (Get_String_Char (Str, J)) then
10409 -- If we are out of range, post error. This is one of the
10410 -- very few places that we place the flag in the middle of
10411 -- a token, right under the offending wide character. Not
10412 -- quite clear if this is right wrt wide character encoding
10413 -- sequences, but it's only an error message.
10415 Error_Msg
10416 ("literal out of range of type Standard.Character",
10417 Source_Ptr (Int (Loc) + J));
10418 return;
10419 end if;
10420 end loop;
10422 -- For the case of Standard.Wide_String, or any other type whose
10423 -- component type is Standard.Wide_Character, we must make sure that
10424 -- there are no wide characters in the string, i.e. that it is
10425 -- entirely composed of characters in range of type Wide_Character.
10427 -- If the string literal is the result of a static concatenation,
10428 -- the test has already been performed on the components, and need
10429 -- not be repeated.
10431 elsif R_Typ = Standard_Wide_Character
10432 and then Nkind (Original_Node (N)) /= N_Op_Concat
10433 then
10434 for J in 1 .. Strlen loop
10435 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
10437 -- If we are out of range, post error. This is one of the
10438 -- very few places that we place the flag in the middle of
10439 -- a token, right under the offending wide character.
10441 -- This is not quite right, because characters in general
10442 -- will take more than one character position ???
10444 Error_Msg
10445 ("literal out of range of type Standard.Wide_Character",
10446 Source_Ptr (Int (Loc) + J));
10447 return;
10448 end if;
10449 end loop;
10451 -- If the root type is not a standard character, then we will convert
10452 -- the string into an aggregate and will let the aggregate code do
10453 -- the checking. Standard Wide_Wide_Character is also OK here.
10455 else
10456 null;
10457 end if;
10459 -- See if the component type of the array corresponding to the string
10460 -- has compile time known bounds. If yes we can directly check
10461 -- whether the evaluation of the string will raise constraint error.
10462 -- Otherwise we need to transform the string literal into the
10463 -- corresponding character aggregate and let the aggregate code do
10464 -- the checking.
10466 if Is_Standard_Character_Type (R_Typ) then
10468 -- Check for the case of full range, where we are definitely OK
10470 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
10471 return;
10472 end if;
10474 -- Here the range is not the complete base type range, so check
10476 declare
10477 Comp_Typ_Lo : constant Node_Id :=
10478 Type_Low_Bound (Component_Type (Typ));
10479 Comp_Typ_Hi : constant Node_Id :=
10480 Type_High_Bound (Component_Type (Typ));
10482 Char_Val : Uint;
10484 begin
10485 if Compile_Time_Known_Value (Comp_Typ_Lo)
10486 and then Compile_Time_Known_Value (Comp_Typ_Hi)
10487 then
10488 for J in 1 .. Strlen loop
10489 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
10491 if Char_Val < Expr_Value (Comp_Typ_Lo)
10492 or else Char_Val > Expr_Value (Comp_Typ_Hi)
10493 then
10494 Apply_Compile_Time_Constraint_Error
10495 (N, "character out of range??",
10496 CE_Range_Check_Failed,
10497 Loc => Source_Ptr (Int (Loc) + J));
10498 end if;
10499 end loop;
10501 return;
10502 end if;
10503 end;
10504 end if;
10505 end if;
10507 -- If we got here we meed to transform the string literal into the
10508 -- equivalent qualified positional array aggregate. This is rather
10509 -- heavy artillery for this situation, but it is hard work to avoid.
10511 declare
10512 Lits : constant List_Id := New_List;
10513 P : Source_Ptr := Loc + 1;
10514 C : Char_Code;
10516 begin
10517 -- Build the character literals, we give them source locations that
10518 -- correspond to the string positions, which is a bit tricky given
10519 -- the possible presence of wide character escape sequences.
10521 for J in 1 .. Strlen loop
10522 C := Get_String_Char (Str, J);
10523 Set_Character_Literal_Name (C);
10525 Append_To (Lits,
10526 Make_Character_Literal (P,
10527 Chars => Name_Find,
10528 Char_Literal_Value => UI_From_CC (C)));
10530 if In_Character_Range (C) then
10531 P := P + 1;
10533 -- Should we have a call to Skip_Wide here ???
10535 -- ??? else
10536 -- Skip_Wide (P);
10538 end if;
10539 end loop;
10541 Rewrite (N,
10542 Make_Qualified_Expression (Loc,
10543 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
10544 Expression =>
10545 Make_Aggregate (Loc, Expressions => Lits)));
10547 Analyze_And_Resolve (N, Typ);
10548 end;
10549 end Resolve_String_Literal;
10551 -----------------------------
10552 -- Resolve_Type_Conversion --
10553 -----------------------------
10555 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
10556 Conv_OK : constant Boolean := Conversion_OK (N);
10557 Operand : constant Node_Id := Expression (N);
10558 Operand_Typ : constant Entity_Id := Etype (Operand);
10559 Target_Typ : constant Entity_Id := Etype (N);
10560 Rop : Node_Id;
10561 Orig_N : Node_Id;
10562 Orig_T : Node_Id;
10564 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
10565 -- Set to False to suppress cases where we want to suppress the test
10566 -- for redundancy to avoid possible false positives on this warning.
10568 begin
10569 if not Conv_OK
10570 and then not Valid_Conversion (N, Target_Typ, Operand)
10571 then
10572 return;
10573 end if;
10575 -- If the Operand Etype is Universal_Fixed, then the conversion is
10576 -- never redundant. We need this check because by the time we have
10577 -- finished the rather complex transformation, the conversion looks
10578 -- redundant when it is not.
10580 if Operand_Typ = Universal_Fixed then
10581 Test_Redundant := False;
10583 -- If the operand is marked as Any_Fixed, then special processing is
10584 -- required. This is also a case where we suppress the test for a
10585 -- redundant conversion, since most certainly it is not redundant.
10587 elsif Operand_Typ = Any_Fixed then
10588 Test_Redundant := False;
10590 -- Mixed-mode operation involving a literal. Context must be a fixed
10591 -- type which is applied to the literal subsequently.
10593 if Is_Fixed_Point_Type (Typ) then
10594 Set_Etype (Operand, Universal_Real);
10596 elsif Is_Numeric_Type (Typ)
10597 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
10598 and then (Etype (Right_Opnd (Operand)) = Universal_Real
10599 or else
10600 Etype (Left_Opnd (Operand)) = Universal_Real)
10601 then
10602 -- Return if expression is ambiguous
10604 if Unique_Fixed_Point_Type (N) = Any_Type then
10605 return;
10607 -- If nothing else, the available fixed type is Duration
10609 else
10610 Set_Etype (Operand, Standard_Duration);
10611 end if;
10613 -- Resolve the real operand with largest available precision
10615 if Etype (Right_Opnd (Operand)) = Universal_Real then
10616 Rop := New_Copy_Tree (Right_Opnd (Operand));
10617 else
10618 Rop := New_Copy_Tree (Left_Opnd (Operand));
10619 end if;
10621 Resolve (Rop, Universal_Real);
10623 -- If the operand is a literal (it could be a non-static and
10624 -- illegal exponentiation) check whether the use of Duration
10625 -- is potentially inaccurate.
10627 if Nkind (Rop) = N_Real_Literal
10628 and then Realval (Rop) /= Ureal_0
10629 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
10630 then
10631 Error_Msg_N
10632 ("??universal real operand can only "
10633 & "be interpreted as Duration!", Rop);
10634 Error_Msg_N
10635 ("\??precision will be lost in the conversion!", Rop);
10636 end if;
10638 elsif Is_Numeric_Type (Typ)
10639 and then Nkind (Operand) in N_Op
10640 and then Unique_Fixed_Point_Type (N) /= Any_Type
10641 then
10642 Set_Etype (Operand, Standard_Duration);
10644 else
10645 Error_Msg_N ("invalid context for mixed mode operation", N);
10646 Set_Etype (Operand, Any_Type);
10647 return;
10648 end if;
10649 end if;
10651 Resolve (Operand);
10653 -- In SPARK, a type conversion between array types should be restricted
10654 -- to types which have matching static bounds.
10656 -- Protect call to Matching_Static_Array_Bounds to avoid costly
10657 -- operation if not needed.
10659 if Restriction_Check_Required (SPARK_05)
10660 and then Is_Array_Type (Target_Typ)
10661 and then Is_Array_Type (Operand_Typ)
10662 and then Operand_Typ /= Any_Composite -- or else Operand in error
10663 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
10664 then
10665 Check_SPARK_05_Restriction
10666 ("array types should have matching static bounds", N);
10667 end if;
10669 -- In formal mode, the operand of an ancestor type conversion must be an
10670 -- object (not an expression).
10672 if Is_Tagged_Type (Target_Typ)
10673 and then not Is_Class_Wide_Type (Target_Typ)
10674 and then Is_Tagged_Type (Operand_Typ)
10675 and then not Is_Class_Wide_Type (Operand_Typ)
10676 and then Is_Ancestor (Target_Typ, Operand_Typ)
10677 and then not Is_SPARK_05_Object_Reference (Operand)
10678 then
10679 Check_SPARK_05_Restriction ("object required", Operand);
10680 end if;
10682 Analyze_Dimension (N);
10684 -- Note: we do the Eval_Type_Conversion call before applying the
10685 -- required checks for a subtype conversion. This is important, since
10686 -- both are prepared under certain circumstances to change the type
10687 -- conversion to a constraint error node, but in the case of
10688 -- Eval_Type_Conversion this may reflect an illegality in the static
10689 -- case, and we would miss the illegality (getting only a warning
10690 -- message), if we applied the type conversion checks first.
10692 Eval_Type_Conversion (N);
10694 -- Even when evaluation is not possible, we may be able to simplify the
10695 -- conversion or its expression. This needs to be done before applying
10696 -- checks, since otherwise the checks may use the original expression
10697 -- and defeat the simplifications. This is specifically the case for
10698 -- elimination of the floating-point Truncation attribute in
10699 -- float-to-int conversions.
10701 Simplify_Type_Conversion (N);
10703 -- If after evaluation we still have a type conversion, then we may need
10704 -- to apply checks required for a subtype conversion.
10706 -- Skip these type conversion checks if universal fixed operands
10707 -- operands involved, since range checks are handled separately for
10708 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
10710 if Nkind (N) = N_Type_Conversion
10711 and then not Is_Generic_Type (Root_Type (Target_Typ))
10712 and then Target_Typ /= Universal_Fixed
10713 and then Operand_Typ /= Universal_Fixed
10714 then
10715 Apply_Type_Conversion_Checks (N);
10716 end if;
10718 -- Issue warning for conversion of simple object to its own type. We
10719 -- have to test the original nodes, since they may have been rewritten
10720 -- by various optimizations.
10722 Orig_N := Original_Node (N);
10724 -- Here we test for a redundant conversion if the warning mode is
10725 -- active (and was not locally reset), and we have a type conversion
10726 -- from source not appearing in a generic instance.
10728 if Test_Redundant
10729 and then Nkind (Orig_N) = N_Type_Conversion
10730 and then Comes_From_Source (Orig_N)
10731 and then not In_Instance
10732 then
10733 Orig_N := Original_Node (Expression (Orig_N));
10734 Orig_T := Target_Typ;
10736 -- If the node is part of a larger expression, the Target_Type
10737 -- may not be the original type of the node if the context is a
10738 -- condition. Recover original type to see if conversion is needed.
10740 if Is_Boolean_Type (Orig_T)
10741 and then Nkind (Parent (N)) in N_Op
10742 then
10743 Orig_T := Etype (Parent (N));
10744 end if;
10746 -- If we have an entity name, then give the warning if the entity
10747 -- is the right type, or if it is a loop parameter covered by the
10748 -- original type (that's needed because loop parameters have an
10749 -- odd subtype coming from the bounds).
10751 if (Is_Entity_Name (Orig_N)
10752 and then
10753 (Etype (Entity (Orig_N)) = Orig_T
10754 or else
10755 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
10756 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
10758 -- If not an entity, then type of expression must match
10760 or else Etype (Orig_N) = Orig_T
10761 then
10762 -- One more check, do not give warning if the analyzed conversion
10763 -- has an expression with non-static bounds, and the bounds of the
10764 -- target are static. This avoids junk warnings in cases where the
10765 -- conversion is necessary to establish staticness, for example in
10766 -- a case statement.
10768 if not Is_OK_Static_Subtype (Operand_Typ)
10769 and then Is_OK_Static_Subtype (Target_Typ)
10770 then
10771 null;
10773 -- Finally, if this type conversion occurs in a context requiring
10774 -- a prefix, and the expression is a qualified expression then the
10775 -- type conversion is not redundant, since a qualified expression
10776 -- is not a prefix, whereas a type conversion is. For example, "X
10777 -- := T'(Funx(...)).Y;" is illegal because a selected component
10778 -- requires a prefix, but a type conversion makes it legal: "X :=
10779 -- T(T'(Funx(...))).Y;"
10781 -- In Ada 2012, a qualified expression is a name, so this idiom is
10782 -- no longer needed, but we still suppress the warning because it
10783 -- seems unfriendly for warnings to pop up when you switch to the
10784 -- newer language version.
10786 elsif Nkind (Orig_N) = N_Qualified_Expression
10787 and then Nkind_In (Parent (N), N_Attribute_Reference,
10788 N_Indexed_Component,
10789 N_Selected_Component,
10790 N_Slice,
10791 N_Explicit_Dereference)
10792 then
10793 null;
10795 -- Never warn on conversion to Long_Long_Integer'Base since
10796 -- that is most likely an artifact of the extended overflow
10797 -- checking and comes from complex expanded code.
10799 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
10800 null;
10802 -- Here we give the redundant conversion warning. If it is an
10803 -- entity, give the name of the entity in the message. If not,
10804 -- just mention the expression.
10806 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
10808 else
10809 if Is_Entity_Name (Orig_N) then
10810 Error_Msg_Node_2 := Orig_T;
10811 Error_Msg_NE -- CODEFIX
10812 ("??redundant conversion, & is of type &!",
10813 N, Entity (Orig_N));
10814 else
10815 Error_Msg_NE
10816 ("??redundant conversion, expression is of type&!",
10817 N, Orig_T);
10818 end if;
10819 end if;
10820 end if;
10821 end if;
10823 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
10824 -- No need to perform any interface conversion if the type of the
10825 -- expression coincides with the target type.
10827 if Ada_Version >= Ada_2005
10828 and then Expander_Active
10829 and then Operand_Typ /= Target_Typ
10830 then
10831 declare
10832 Opnd : Entity_Id := Operand_Typ;
10833 Target : Entity_Id := Target_Typ;
10835 begin
10836 -- If the type of the operand is a limited view, use nonlimited
10837 -- view when available. If it is a class-wide type, recover the
10838 -- class-wide type of the nonlimited view.
10840 if From_Limited_With (Opnd)
10841 and then Has_Non_Limited_View (Opnd)
10842 then
10843 Opnd := Non_Limited_View (Opnd);
10844 Set_Etype (Expression (N), Opnd);
10845 end if;
10847 if Is_Access_Type (Opnd) then
10848 Opnd := Designated_Type (Opnd);
10849 end if;
10851 if Is_Access_Type (Target_Typ) then
10852 Target := Designated_Type (Target);
10853 end if;
10855 if Opnd = Target then
10856 null;
10858 -- Conversion from interface type
10860 elsif Is_Interface (Opnd) then
10862 -- Ada 2005 (AI-217): Handle entities from limited views
10864 if From_Limited_With (Opnd) then
10865 Error_Msg_Qual_Level := 99;
10866 Error_Msg_NE -- CODEFIX
10867 ("missing WITH clause on package &", N,
10868 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
10869 Error_Msg_N
10870 ("type conversions require visibility of the full view",
10873 elsif From_Limited_With (Target)
10874 and then not
10875 (Is_Access_Type (Target_Typ)
10876 and then Present (Non_Limited_View (Etype (Target))))
10877 then
10878 Error_Msg_Qual_Level := 99;
10879 Error_Msg_NE -- CODEFIX
10880 ("missing WITH clause on package &", N,
10881 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
10882 Error_Msg_N
10883 ("type conversions require visibility of the full view",
10886 else
10887 Expand_Interface_Conversion (N);
10888 end if;
10890 -- Conversion to interface type
10892 elsif Is_Interface (Target) then
10894 -- Handle subtypes
10896 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
10897 Opnd := Etype (Opnd);
10898 end if;
10900 if Is_Class_Wide_Type (Opnd)
10901 or else Interface_Present_In_Ancestor
10902 (Typ => Opnd,
10903 Iface => Target)
10904 then
10905 Expand_Interface_Conversion (N);
10906 else
10907 Error_Msg_Name_1 := Chars (Etype (Target));
10908 Error_Msg_Name_2 := Chars (Opnd);
10909 Error_Msg_N
10910 ("wrong interface conversion (% is not a progenitor "
10911 & "of %)", N);
10912 end if;
10913 end if;
10914 end;
10915 end if;
10917 -- Ada 2012: if target type has predicates, the result requires a
10918 -- predicate check. If the context is a call to another predicate
10919 -- check we must prevent infinite recursion.
10921 if Has_Predicates (Target_Typ) then
10922 if Nkind (Parent (N)) = N_Function_Call
10923 and then Present (Name (Parent (N)))
10924 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
10925 or else
10926 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
10927 then
10928 null;
10930 else
10931 Apply_Predicate_Check (N, Target_Typ);
10932 end if;
10933 end if;
10935 -- If at this stage we have a real to integer conversion, make sure
10936 -- that the Do_Range_Check flag is set, because such conversions in
10937 -- general need a range check. We only need this if expansion is off
10938 -- or we are in GNATProve mode.
10940 if Nkind (N) = N_Type_Conversion
10941 and then (GNATprove_Mode or not Expander_Active)
10942 and then Is_Integer_Type (Target_Typ)
10943 and then Is_Real_Type (Operand_Typ)
10944 then
10945 Set_Do_Range_Check (Operand);
10946 end if;
10948 -- Generating C code a type conversion of an access to constrained
10949 -- array type to access to unconstrained array type involves building
10950 -- a fat pointer which in general cannot be generated on the fly. We
10951 -- remove side effects in order to store the result of the conversion
10952 -- into a temporary.
10954 if Generate_C_Code
10955 and then Nkind (N) = N_Type_Conversion
10956 and then Nkind (Parent (N)) /= N_Object_Declaration
10957 and then Is_Access_Type (Etype (N))
10958 and then Is_Array_Type (Designated_Type (Etype (N)))
10959 and then not Is_Constrained (Designated_Type (Etype (N)))
10960 and then Is_Constrained (Designated_Type (Etype (Expression (N))))
10961 then
10962 Remove_Side_Effects (N);
10963 end if;
10964 end Resolve_Type_Conversion;
10966 ----------------------
10967 -- Resolve_Unary_Op --
10968 ----------------------
10970 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
10971 B_Typ : constant Entity_Id := Base_Type (Typ);
10972 R : constant Node_Id := Right_Opnd (N);
10973 OK : Boolean;
10974 Lo : Uint;
10975 Hi : Uint;
10977 begin
10978 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
10979 Error_Msg_Name_1 := Chars (Typ);
10980 Check_SPARK_05_Restriction
10981 ("unary operator not defined for modular type%", N);
10982 end if;
10984 -- Deal with intrinsic unary operators
10986 if Comes_From_Source (N)
10987 and then Ekind (Entity (N)) = E_Function
10988 and then Is_Imported (Entity (N))
10989 and then Is_Intrinsic_Subprogram (Entity (N))
10990 then
10991 Resolve_Intrinsic_Unary_Operator (N, Typ);
10992 return;
10993 end if;
10995 -- Deal with universal cases
10997 if Etype (R) = Universal_Integer
10998 or else
10999 Etype (R) = Universal_Real
11000 then
11001 Check_For_Visible_Operator (N, B_Typ);
11002 end if;
11004 Set_Etype (N, B_Typ);
11005 Resolve (R, B_Typ);
11007 -- Generate warning for expressions like abs (x mod 2)
11009 if Warn_On_Redundant_Constructs
11010 and then Nkind (N) = N_Op_Abs
11011 then
11012 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
11014 if OK and then Hi >= Lo and then Lo >= 0 then
11015 Error_Msg_N -- CODEFIX
11016 ("?r?abs applied to known non-negative value has no effect", N);
11017 end if;
11018 end if;
11020 -- Deal with reference generation
11022 Check_Unset_Reference (R);
11023 Generate_Operator_Reference (N, B_Typ);
11024 Analyze_Dimension (N);
11025 Eval_Unary_Op (N);
11027 -- Set overflow checking bit. Much cleverer code needed here eventually
11028 -- and perhaps the Resolve routines should be separated for the various
11029 -- arithmetic operations, since they will need different processing ???
11031 if Nkind (N) in N_Op then
11032 if not Overflow_Checks_Suppressed (Etype (N)) then
11033 Enable_Overflow_Check (N);
11034 end if;
11035 end if;
11037 -- Generate warning for expressions like -5 mod 3 for integers. No need
11038 -- to worry in the floating-point case, since parens do not affect the
11039 -- result so there is no point in giving in a warning.
11041 declare
11042 Norig : constant Node_Id := Original_Node (N);
11043 Rorig : Node_Id;
11044 Val : Uint;
11045 HB : Uint;
11046 LB : Uint;
11047 Lval : Uint;
11048 Opnd : Node_Id;
11050 begin
11051 if Warn_On_Questionable_Missing_Parens
11052 and then Comes_From_Source (Norig)
11053 and then Is_Integer_Type (Typ)
11054 and then Nkind (Norig) = N_Op_Minus
11055 then
11056 Rorig := Original_Node (Right_Opnd (Norig));
11058 -- We are looking for cases where the right operand is not
11059 -- parenthesized, and is a binary operator, multiply, divide, or
11060 -- mod. These are the cases where the grouping can affect results.
11062 if Paren_Count (Rorig) = 0
11063 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
11064 then
11065 -- For mod, we always give the warning, since the value is
11066 -- affected by the parenthesization (e.g. (-5) mod 315 /=
11067 -- -(5 mod 315)). But for the other cases, the only concern is
11068 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
11069 -- overflows, but (-2) * 64 does not). So we try to give the
11070 -- message only when overflow is possible.
11072 if Nkind (Rorig) /= N_Op_Mod
11073 and then Compile_Time_Known_Value (R)
11074 then
11075 Val := Expr_Value (R);
11077 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
11078 HB := Expr_Value (Type_High_Bound (Typ));
11079 else
11080 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
11081 end if;
11083 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
11084 LB := Expr_Value (Type_Low_Bound (Typ));
11085 else
11086 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
11087 end if;
11089 -- Note that the test below is deliberately excluding the
11090 -- largest negative number, since that is a potentially
11091 -- troublesome case (e.g. -2 * x, where the result is the
11092 -- largest negative integer has an overflow with 2 * x).
11094 if Val > LB and then Val <= HB then
11095 return;
11096 end if;
11097 end if;
11099 -- For the multiplication case, the only case we have to worry
11100 -- about is when (-a)*b is exactly the largest negative number
11101 -- so that -(a*b) can cause overflow. This can only happen if
11102 -- a is a power of 2, and more generally if any operand is a
11103 -- constant that is not a power of 2, then the parentheses
11104 -- cannot affect whether overflow occurs. We only bother to
11105 -- test the left most operand
11107 -- Loop looking at left operands for one that has known value
11109 Opnd := Rorig;
11110 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
11111 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
11112 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
11114 -- Operand value of 0 or 1 skips warning
11116 if Lval <= 1 then
11117 return;
11119 -- Otherwise check power of 2, if power of 2, warn, if
11120 -- anything else, skip warning.
11122 else
11123 while Lval /= 2 loop
11124 if Lval mod 2 = 1 then
11125 return;
11126 else
11127 Lval := Lval / 2;
11128 end if;
11129 end loop;
11131 exit Opnd_Loop;
11132 end if;
11133 end if;
11135 -- Keep looking at left operands
11137 Opnd := Left_Opnd (Opnd);
11138 end loop Opnd_Loop;
11140 -- For rem or "/" we can only have a problematic situation
11141 -- if the divisor has a value of minus one or one. Otherwise
11142 -- overflow is impossible (divisor > 1) or we have a case of
11143 -- division by zero in any case.
11145 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
11146 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
11147 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
11148 then
11149 return;
11150 end if;
11152 -- If we fall through warning should be issued
11154 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
11156 Error_Msg_N
11157 ("??unary minus expression should be parenthesized here!", N);
11158 end if;
11159 end if;
11160 end;
11161 end Resolve_Unary_Op;
11163 ----------------------------------
11164 -- Resolve_Unchecked_Expression --
11165 ----------------------------------
11167 procedure Resolve_Unchecked_Expression
11168 (N : Node_Id;
11169 Typ : Entity_Id)
11171 begin
11172 Resolve (Expression (N), Typ, Suppress => All_Checks);
11173 Set_Etype (N, Typ);
11174 end Resolve_Unchecked_Expression;
11176 ---------------------------------------
11177 -- Resolve_Unchecked_Type_Conversion --
11178 ---------------------------------------
11180 procedure Resolve_Unchecked_Type_Conversion
11181 (N : Node_Id;
11182 Typ : Entity_Id)
11184 pragma Warnings (Off, Typ);
11186 Operand : constant Node_Id := Expression (N);
11187 Opnd_Type : constant Entity_Id := Etype (Operand);
11189 begin
11190 -- Resolve operand using its own type
11192 Resolve (Operand, Opnd_Type);
11194 -- In an inlined context, the unchecked conversion may be applied
11195 -- to a literal, in which case its type is the type of the context.
11196 -- (In other contexts conversions cannot apply to literals).
11198 if In_Inlined_Body
11199 and then (Opnd_Type = Any_Character or else
11200 Opnd_Type = Any_Integer or else
11201 Opnd_Type = Any_Real)
11202 then
11203 Set_Etype (Operand, Typ);
11204 end if;
11206 Analyze_Dimension (N);
11207 Eval_Unchecked_Conversion (N);
11208 end Resolve_Unchecked_Type_Conversion;
11210 ------------------------------
11211 -- Rewrite_Operator_As_Call --
11212 ------------------------------
11214 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
11215 Loc : constant Source_Ptr := Sloc (N);
11216 Actuals : constant List_Id := New_List;
11217 New_N : Node_Id;
11219 begin
11220 if Nkind (N) in N_Binary_Op then
11221 Append (Left_Opnd (N), Actuals);
11222 end if;
11224 Append (Right_Opnd (N), Actuals);
11226 New_N :=
11227 Make_Function_Call (Sloc => Loc,
11228 Name => New_Occurrence_Of (Nam, Loc),
11229 Parameter_Associations => Actuals);
11231 Preserve_Comes_From_Source (New_N, N);
11232 Preserve_Comes_From_Source (Name (New_N), N);
11233 Rewrite (N, New_N);
11234 Set_Etype (N, Etype (Nam));
11235 end Rewrite_Operator_As_Call;
11237 ------------------------------
11238 -- Rewrite_Renamed_Operator --
11239 ------------------------------
11241 procedure Rewrite_Renamed_Operator
11242 (N : Node_Id;
11243 Op : Entity_Id;
11244 Typ : Entity_Id)
11246 Nam : constant Name_Id := Chars (Op);
11247 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
11248 Op_Node : Node_Id;
11250 begin
11251 -- Do not perform this transformation within a pre/postcondition,
11252 -- because the expression will be re-analyzed, and the transformation
11253 -- might affect the visibility of the operator, e.g. in an instance.
11254 -- Note that fully analyzed and expanded pre/postconditions appear as
11255 -- pragma Check equivalents.
11257 if In_Pre_Post_Condition (N) then
11258 return;
11259 end if;
11261 -- Rewrite the operator node using the real operator, not its renaming.
11262 -- Exclude user-defined intrinsic operations of the same name, which are
11263 -- treated separately and rewritten as calls.
11265 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
11266 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
11267 Set_Chars (Op_Node, Nam);
11268 Set_Etype (Op_Node, Etype (N));
11269 Set_Entity (Op_Node, Op);
11270 Set_Right_Opnd (Op_Node, Right_Opnd (N));
11272 -- Indicate that both the original entity and its renaming are
11273 -- referenced at this point.
11275 Generate_Reference (Entity (N), N);
11276 Generate_Reference (Op, N);
11278 if Is_Binary then
11279 Set_Left_Opnd (Op_Node, Left_Opnd (N));
11280 end if;
11282 Rewrite (N, Op_Node);
11284 -- If the context type is private, add the appropriate conversions so
11285 -- that the operator is applied to the full view. This is done in the
11286 -- routines that resolve intrinsic operators.
11288 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
11289 case Nkind (N) is
11290 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
11291 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
11292 Resolve_Intrinsic_Operator (N, Typ);
11294 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
11295 Resolve_Intrinsic_Unary_Operator (N, Typ);
11297 when others =>
11298 Resolve (N, Typ);
11299 end case;
11300 end if;
11302 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
11304 -- Operator renames a user-defined operator of the same name. Use the
11305 -- original operator in the node, which is the one Gigi knows about.
11307 Set_Entity (N, Op);
11308 Set_Is_Overloaded (N, False);
11309 end if;
11310 end Rewrite_Renamed_Operator;
11312 -----------------------
11313 -- Set_Slice_Subtype --
11314 -----------------------
11316 -- Build an implicit subtype declaration to represent the type delivered by
11317 -- the slice. This is an abbreviated version of an array subtype. We define
11318 -- an index subtype for the slice, using either the subtype name or the
11319 -- discrete range of the slice. To be consistent with index usage elsewhere
11320 -- we create a list header to hold the single index. This list is not
11321 -- otherwise attached to the syntax tree.
11323 procedure Set_Slice_Subtype (N : Node_Id) is
11324 Loc : constant Source_Ptr := Sloc (N);
11325 Index_List : constant List_Id := New_List;
11326 Index : Node_Id;
11327 Index_Subtype : Entity_Id;
11328 Index_Type : Entity_Id;
11329 Slice_Subtype : Entity_Id;
11330 Drange : constant Node_Id := Discrete_Range (N);
11332 begin
11333 Index_Type := Base_Type (Etype (Drange));
11335 if Is_Entity_Name (Drange) then
11336 Index_Subtype := Entity (Drange);
11338 else
11339 -- We force the evaluation of a range. This is definitely needed in
11340 -- the renamed case, and seems safer to do unconditionally. Note in
11341 -- any case that since we will create and insert an Itype referring
11342 -- to this range, we must make sure any side effect removal actions
11343 -- are inserted before the Itype definition.
11345 if Nkind (Drange) = N_Range then
11346 Force_Evaluation (Low_Bound (Drange));
11347 Force_Evaluation (High_Bound (Drange));
11349 -- If the discrete range is given by a subtype indication, the
11350 -- type of the slice is the base of the subtype mark.
11352 elsif Nkind (Drange) = N_Subtype_Indication then
11353 declare
11354 R : constant Node_Id := Range_Expression (Constraint (Drange));
11355 begin
11356 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
11357 Force_Evaluation (Low_Bound (R));
11358 Force_Evaluation (High_Bound (R));
11359 end;
11360 end if;
11362 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
11364 -- Take a new copy of Drange (where bounds have been rewritten to
11365 -- reference side-effect-free names). Using a separate tree ensures
11366 -- that further expansion (e.g. while rewriting a slice assignment
11367 -- into a FOR loop) does not attempt to remove side effects on the
11368 -- bounds again (which would cause the bounds in the index subtype
11369 -- definition to refer to temporaries before they are defined) (the
11370 -- reason is that some names are considered side effect free here
11371 -- for the subtype, but not in the context of a loop iteration
11372 -- scheme).
11374 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
11375 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
11376 Set_Etype (Index_Subtype, Index_Type);
11377 Set_Size_Info (Index_Subtype, Index_Type);
11378 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
11379 end if;
11381 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
11383 Index := New_Occurrence_Of (Index_Subtype, Loc);
11384 Set_Etype (Index, Index_Subtype);
11385 Append (Index, Index_List);
11387 Set_First_Index (Slice_Subtype, Index);
11388 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
11389 Set_Is_Constrained (Slice_Subtype, True);
11391 Check_Compile_Time_Size (Slice_Subtype);
11393 -- The Etype of the existing Slice node is reset to this slice subtype.
11394 -- Its bounds are obtained from its first index.
11396 Set_Etype (N, Slice_Subtype);
11398 -- For packed slice subtypes, freeze immediately (except in the case of
11399 -- being in a "spec expression" where we never freeze when we first see
11400 -- the expression).
11402 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
11403 Freeze_Itype (Slice_Subtype, N);
11405 -- For all other cases insert an itype reference in the slice's actions
11406 -- so that the itype is frozen at the proper place in the tree (i.e. at
11407 -- the point where actions for the slice are analyzed). Note that this
11408 -- is different from freezing the itype immediately, which might be
11409 -- premature (e.g. if the slice is within a transient scope). This needs
11410 -- to be done only if expansion is enabled.
11412 elsif Expander_Active then
11413 Ensure_Defined (Typ => Slice_Subtype, N => N);
11414 end if;
11415 end Set_Slice_Subtype;
11417 --------------------------------
11418 -- Set_String_Literal_Subtype --
11419 --------------------------------
11421 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
11422 Loc : constant Source_Ptr := Sloc (N);
11423 Low_Bound : constant Node_Id :=
11424 Type_Low_Bound (Etype (First_Index (Typ)));
11425 Subtype_Id : Entity_Id;
11427 begin
11428 if Nkind (N) /= N_String_Literal then
11429 return;
11430 end if;
11432 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
11433 Set_String_Literal_Length (Subtype_Id, UI_From_Int
11434 (String_Length (Strval (N))));
11435 Set_Etype (Subtype_Id, Base_Type (Typ));
11436 Set_Is_Constrained (Subtype_Id);
11437 Set_Etype (N, Subtype_Id);
11439 -- The low bound is set from the low bound of the corresponding index
11440 -- type. Note that we do not store the high bound in the string literal
11441 -- subtype, but it can be deduced if necessary from the length and the
11442 -- low bound.
11444 if Is_OK_Static_Expression (Low_Bound) then
11445 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
11447 -- If the lower bound is not static we create a range for the string
11448 -- literal, using the index type and the known length of the literal.
11449 -- The index type is not necessarily Positive, so the upper bound is
11450 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
11452 else
11453 declare
11454 Index_List : constant List_Id := New_List;
11455 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
11456 High_Bound : constant Node_Id :=
11457 Make_Attribute_Reference (Loc,
11458 Attribute_Name => Name_Val,
11459 Prefix =>
11460 New_Occurrence_Of (Index_Type, Loc),
11461 Expressions => New_List (
11462 Make_Op_Add (Loc,
11463 Left_Opnd =>
11464 Make_Attribute_Reference (Loc,
11465 Attribute_Name => Name_Pos,
11466 Prefix =>
11467 New_Occurrence_Of (Index_Type, Loc),
11468 Expressions =>
11469 New_List (New_Copy_Tree (Low_Bound))),
11470 Right_Opnd =>
11471 Make_Integer_Literal (Loc,
11472 String_Length (Strval (N)) - 1))));
11474 Array_Subtype : Entity_Id;
11475 Drange : Node_Id;
11476 Index : Node_Id;
11477 Index_Subtype : Entity_Id;
11479 begin
11480 if Is_Integer_Type (Index_Type) then
11481 Set_String_Literal_Low_Bound
11482 (Subtype_Id, Make_Integer_Literal (Loc, 1));
11484 else
11485 -- If the index type is an enumeration type, build bounds
11486 -- expression with attributes.
11488 Set_String_Literal_Low_Bound
11489 (Subtype_Id,
11490 Make_Attribute_Reference (Loc,
11491 Attribute_Name => Name_First,
11492 Prefix =>
11493 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
11494 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
11495 end if;
11497 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
11499 -- Build bona fide subtype for the string, and wrap it in an
11500 -- unchecked conversion, because the backend expects the
11501 -- String_Literal_Subtype to have a static lower bound.
11503 Index_Subtype :=
11504 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
11505 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
11506 Set_Scalar_Range (Index_Subtype, Drange);
11507 Set_Parent (Drange, N);
11508 Analyze_And_Resolve (Drange, Index_Type);
11510 -- In the context, the Index_Type may already have a constraint,
11511 -- so use common base type on string subtype. The base type may
11512 -- be used when generating attributes of the string, for example
11513 -- in the context of a slice assignment.
11515 Set_Etype (Index_Subtype, Base_Type (Index_Type));
11516 Set_Size_Info (Index_Subtype, Index_Type);
11517 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
11519 Array_Subtype := Create_Itype (E_Array_Subtype, N);
11521 Index := New_Occurrence_Of (Index_Subtype, Loc);
11522 Set_Etype (Index, Index_Subtype);
11523 Append (Index, Index_List);
11525 Set_First_Index (Array_Subtype, Index);
11526 Set_Etype (Array_Subtype, Base_Type (Typ));
11527 Set_Is_Constrained (Array_Subtype, True);
11529 Rewrite (N,
11530 Make_Unchecked_Type_Conversion (Loc,
11531 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
11532 Expression => Relocate_Node (N)));
11533 Set_Etype (N, Array_Subtype);
11534 end;
11535 end if;
11536 end Set_String_Literal_Subtype;
11538 ------------------------------
11539 -- Simplify_Type_Conversion --
11540 ------------------------------
11542 procedure Simplify_Type_Conversion (N : Node_Id) is
11543 begin
11544 if Nkind (N) = N_Type_Conversion then
11545 declare
11546 Operand : constant Node_Id := Expression (N);
11547 Target_Typ : constant Entity_Id := Etype (N);
11548 Opnd_Typ : constant Entity_Id := Etype (Operand);
11550 begin
11551 -- Special processing if the conversion is the expression of a
11552 -- Rounding or Truncation attribute reference. In this case we
11553 -- replace:
11555 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
11557 -- by
11559 -- ityp (x)
11561 -- with the Float_Truncate flag set to False or True respectively,
11562 -- which is more efficient.
11564 if Is_Floating_Point_Type (Opnd_Typ)
11565 and then
11566 (Is_Integer_Type (Target_Typ)
11567 or else (Is_Fixed_Point_Type (Target_Typ)
11568 and then Conversion_OK (N)))
11569 and then Nkind (Operand) = N_Attribute_Reference
11570 and then Nam_In (Attribute_Name (Operand), Name_Rounding,
11571 Name_Truncation)
11572 then
11573 declare
11574 Truncate : constant Boolean :=
11575 Attribute_Name (Operand) = Name_Truncation;
11576 begin
11577 Rewrite (Operand,
11578 Relocate_Node (First (Expressions (Operand))));
11579 Set_Float_Truncate (N, Truncate);
11580 end;
11581 end if;
11582 end;
11583 end if;
11584 end Simplify_Type_Conversion;
11586 -----------------------------
11587 -- Unique_Fixed_Point_Type --
11588 -----------------------------
11590 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
11591 T1 : Entity_Id := Empty;
11592 T2 : Entity_Id;
11593 Item : Node_Id;
11594 Scop : Entity_Id;
11596 procedure Fixed_Point_Error;
11597 -- Give error messages for true ambiguity. Messages are posted on node
11598 -- N, and entities T1, T2 are the possible interpretations.
11600 -----------------------
11601 -- Fixed_Point_Error --
11602 -----------------------
11604 procedure Fixed_Point_Error is
11605 begin
11606 Error_Msg_N ("ambiguous universal_fixed_expression", N);
11607 Error_Msg_NE ("\\possible interpretation as}", N, T1);
11608 Error_Msg_NE ("\\possible interpretation as}", N, T2);
11609 end Fixed_Point_Error;
11611 -- Start of processing for Unique_Fixed_Point_Type
11613 begin
11614 -- The operations on Duration are visible, so Duration is always a
11615 -- possible interpretation.
11617 T1 := Standard_Duration;
11619 -- Look for fixed-point types in enclosing scopes
11621 Scop := Current_Scope;
11622 while Scop /= Standard_Standard loop
11623 T2 := First_Entity (Scop);
11624 while Present (T2) loop
11625 if Is_Fixed_Point_Type (T2)
11626 and then Current_Entity (T2) = T2
11627 and then Scope (Base_Type (T2)) = Scop
11628 then
11629 if Present (T1) then
11630 Fixed_Point_Error;
11631 return Any_Type;
11632 else
11633 T1 := T2;
11634 end if;
11635 end if;
11637 Next_Entity (T2);
11638 end loop;
11640 Scop := Scope (Scop);
11641 end loop;
11643 -- Look for visible fixed type declarations in the context
11645 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
11646 while Present (Item) loop
11647 if Nkind (Item) = N_With_Clause then
11648 Scop := Entity (Name (Item));
11649 T2 := First_Entity (Scop);
11650 while Present (T2) loop
11651 if Is_Fixed_Point_Type (T2)
11652 and then Scope (Base_Type (T2)) = Scop
11653 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
11654 then
11655 if Present (T1) then
11656 Fixed_Point_Error;
11657 return Any_Type;
11658 else
11659 T1 := T2;
11660 end if;
11661 end if;
11663 Next_Entity (T2);
11664 end loop;
11665 end if;
11667 Next (Item);
11668 end loop;
11670 if Nkind (N) = N_Real_Literal then
11671 Error_Msg_NE
11672 ("??real literal interpreted as }!", N, T1);
11673 else
11674 Error_Msg_NE
11675 ("??universal_fixed expression interpreted as }!", N, T1);
11676 end if;
11678 return T1;
11679 end Unique_Fixed_Point_Type;
11681 ----------------------
11682 -- Valid_Conversion --
11683 ----------------------
11685 function Valid_Conversion
11686 (N : Node_Id;
11687 Target : Entity_Id;
11688 Operand : Node_Id;
11689 Report_Errs : Boolean := True) return Boolean
11691 Target_Type : constant Entity_Id := Base_Type (Target);
11692 Opnd_Type : Entity_Id := Etype (Operand);
11693 Inc_Ancestor : Entity_Id;
11695 function Conversion_Check
11696 (Valid : Boolean;
11697 Msg : String) return Boolean;
11698 -- Little routine to post Msg if Valid is False, returns Valid value
11700 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
11701 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
11703 procedure Conversion_Error_NE
11704 (Msg : String;
11705 N : Node_Or_Entity_Id;
11706 E : Node_Or_Entity_Id);
11707 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
11709 function Valid_Tagged_Conversion
11710 (Target_Type : Entity_Id;
11711 Opnd_Type : Entity_Id) return Boolean;
11712 -- Specifically test for validity of tagged conversions
11714 function Valid_Array_Conversion return Boolean;
11715 -- Check index and component conformance, and accessibility levels if
11716 -- the component types are anonymous access types (Ada 2005).
11718 ----------------------
11719 -- Conversion_Check --
11720 ----------------------
11722 function Conversion_Check
11723 (Valid : Boolean;
11724 Msg : String) return Boolean
11726 begin
11727 if not Valid
11729 -- A generic unit has already been analyzed and we have verified
11730 -- that a particular conversion is OK in that context. Since the
11731 -- instance is reanalyzed without relying on the relationships
11732 -- established during the analysis of the generic, it is possible
11733 -- to end up with inconsistent views of private types. Do not emit
11734 -- the error message in such cases. The rest of the machinery in
11735 -- Valid_Conversion still ensures the proper compatibility of
11736 -- target and operand types.
11738 and then not In_Instance
11739 then
11740 Conversion_Error_N (Msg, Operand);
11741 end if;
11743 return Valid;
11744 end Conversion_Check;
11746 ------------------------
11747 -- Conversion_Error_N --
11748 ------------------------
11750 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
11751 begin
11752 if Report_Errs then
11753 Error_Msg_N (Msg, N);
11754 end if;
11755 end Conversion_Error_N;
11757 -------------------------
11758 -- Conversion_Error_NE --
11759 -------------------------
11761 procedure Conversion_Error_NE
11762 (Msg : String;
11763 N : Node_Or_Entity_Id;
11764 E : Node_Or_Entity_Id)
11766 begin
11767 if Report_Errs then
11768 Error_Msg_NE (Msg, N, E);
11769 end if;
11770 end Conversion_Error_NE;
11772 ----------------------------
11773 -- Valid_Array_Conversion --
11774 ----------------------------
11776 function Valid_Array_Conversion return Boolean
11778 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
11779 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
11781 Opnd_Index : Node_Id;
11782 Opnd_Index_Type : Entity_Id;
11784 Target_Comp_Type : constant Entity_Id :=
11785 Component_Type (Target_Type);
11786 Target_Comp_Base : constant Entity_Id :=
11787 Base_Type (Target_Comp_Type);
11789 Target_Index : Node_Id;
11790 Target_Index_Type : Entity_Id;
11792 begin
11793 -- Error if wrong number of dimensions
11796 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
11797 then
11798 Conversion_Error_N
11799 ("incompatible number of dimensions for conversion", Operand);
11800 return False;
11802 -- Number of dimensions matches
11804 else
11805 -- Loop through indexes of the two arrays
11807 Target_Index := First_Index (Target_Type);
11808 Opnd_Index := First_Index (Opnd_Type);
11809 while Present (Target_Index) and then Present (Opnd_Index) loop
11810 Target_Index_Type := Etype (Target_Index);
11811 Opnd_Index_Type := Etype (Opnd_Index);
11813 -- Error if index types are incompatible
11815 if not (Is_Integer_Type (Target_Index_Type)
11816 and then Is_Integer_Type (Opnd_Index_Type))
11817 and then (Root_Type (Target_Index_Type)
11818 /= Root_Type (Opnd_Index_Type))
11819 then
11820 Conversion_Error_N
11821 ("incompatible index types for array conversion",
11822 Operand);
11823 return False;
11824 end if;
11826 Next_Index (Target_Index);
11827 Next_Index (Opnd_Index);
11828 end loop;
11830 -- If component types have same base type, all set
11832 if Target_Comp_Base = Opnd_Comp_Base then
11833 null;
11835 -- Here if base types of components are not the same. The only
11836 -- time this is allowed is if we have anonymous access types.
11838 -- The conversion of arrays of anonymous access types can lead
11839 -- to dangling pointers. AI-392 formalizes the accessibility
11840 -- checks that must be applied to such conversions to prevent
11841 -- out-of-scope references.
11843 elsif Ekind_In
11844 (Target_Comp_Base, E_Anonymous_Access_Type,
11845 E_Anonymous_Access_Subprogram_Type)
11846 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
11847 and then
11848 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
11849 then
11850 if Type_Access_Level (Target_Type) <
11851 Deepest_Type_Access_Level (Opnd_Type)
11852 then
11853 if In_Instance_Body then
11854 Error_Msg_Warn := SPARK_Mode /= On;
11855 Conversion_Error_N
11856 ("source array type has deeper accessibility "
11857 & "level than target<<", Operand);
11858 Conversion_Error_N ("\Program_Error [<<", Operand);
11859 Rewrite (N,
11860 Make_Raise_Program_Error (Sloc (N),
11861 Reason => PE_Accessibility_Check_Failed));
11862 Set_Etype (N, Target_Type);
11863 return False;
11865 -- Conversion not allowed because of accessibility levels
11867 else
11868 Conversion_Error_N
11869 ("source array type has deeper accessibility "
11870 & "level than target", Operand);
11871 return False;
11872 end if;
11874 else
11875 null;
11876 end if;
11878 -- All other cases where component base types do not match
11880 else
11881 Conversion_Error_N
11882 ("incompatible component types for array conversion",
11883 Operand);
11884 return False;
11885 end if;
11887 -- Check that component subtypes statically match. For numeric
11888 -- types this means that both must be either constrained or
11889 -- unconstrained. For enumeration types the bounds must match.
11890 -- All of this is checked in Subtypes_Statically_Match.
11892 if not Subtypes_Statically_Match
11893 (Target_Comp_Type, Opnd_Comp_Type)
11894 then
11895 Conversion_Error_N
11896 ("component subtypes must statically match", Operand);
11897 return False;
11898 end if;
11899 end if;
11901 return True;
11902 end Valid_Array_Conversion;
11904 -----------------------------
11905 -- Valid_Tagged_Conversion --
11906 -----------------------------
11908 function Valid_Tagged_Conversion
11909 (Target_Type : Entity_Id;
11910 Opnd_Type : Entity_Id) return Boolean
11912 begin
11913 -- Upward conversions are allowed (RM 4.6(22))
11915 if Covers (Target_Type, Opnd_Type)
11916 or else Is_Ancestor (Target_Type, Opnd_Type)
11917 then
11918 return True;
11920 -- Downward conversion are allowed if the operand is class-wide
11921 -- (RM 4.6(23)).
11923 elsif Is_Class_Wide_Type (Opnd_Type)
11924 and then Covers (Opnd_Type, Target_Type)
11925 then
11926 return True;
11928 elsif Covers (Opnd_Type, Target_Type)
11929 or else Is_Ancestor (Opnd_Type, Target_Type)
11930 then
11931 return
11932 Conversion_Check (False,
11933 "downward conversion of tagged objects not allowed");
11935 -- Ada 2005 (AI-251): The conversion to/from interface types is
11936 -- always valid. The types involved may be class-wide (sub)types.
11938 elsif Is_Interface (Etype (Base_Type (Target_Type)))
11939 or else Is_Interface (Etype (Base_Type (Opnd_Type)))
11940 then
11941 return True;
11943 -- If the operand is a class-wide type obtained through a limited_
11944 -- with clause, and the context includes the nonlimited view, use
11945 -- it to determine whether the conversion is legal.
11947 elsif Is_Class_Wide_Type (Opnd_Type)
11948 and then From_Limited_With (Opnd_Type)
11949 and then Present (Non_Limited_View (Etype (Opnd_Type)))
11950 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
11951 then
11952 return True;
11954 elsif Is_Access_Type (Opnd_Type)
11955 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
11956 then
11957 return True;
11959 else
11960 Conversion_Error_NE
11961 ("invalid tagged conversion, not compatible with}",
11962 N, First_Subtype (Opnd_Type));
11963 return False;
11964 end if;
11965 end Valid_Tagged_Conversion;
11967 -- Start of processing for Valid_Conversion
11969 begin
11970 Check_Parameterless_Call (Operand);
11972 if Is_Overloaded (Operand) then
11973 declare
11974 I : Interp_Index;
11975 I1 : Interp_Index;
11976 It : Interp;
11977 It1 : Interp;
11978 N1 : Entity_Id;
11979 T1 : Entity_Id;
11981 begin
11982 -- Remove procedure calls, which syntactically cannot appear in
11983 -- this context, but which cannot be removed by type checking,
11984 -- because the context does not impose a type.
11986 -- The node may be labelled overloaded, but still contain only one
11987 -- interpretation because others were discarded earlier. If this
11988 -- is the case, retain the single interpretation if legal.
11990 Get_First_Interp (Operand, I, It);
11991 Opnd_Type := It.Typ;
11992 Get_Next_Interp (I, It);
11994 if Present (It.Typ)
11995 and then Opnd_Type /= Standard_Void_Type
11996 then
11997 -- More than one candidate interpretation is available
11999 Get_First_Interp (Operand, I, It);
12000 while Present (It.Typ) loop
12001 if It.Typ = Standard_Void_Type then
12002 Remove_Interp (I);
12003 end if;
12005 -- When compiling for a system where Address is of a visible
12006 -- integer type, spurious ambiguities can be produced when
12007 -- arithmetic operations have a literal operand and return
12008 -- System.Address or a descendant of it. These ambiguities
12009 -- are usually resolved by the context, but for conversions
12010 -- there is no context type and the removal of the spurious
12011 -- operations must be done explicitly here.
12013 if not Address_Is_Private
12014 and then Is_Descendant_Of_Address (It.Typ)
12015 then
12016 Remove_Interp (I);
12017 end if;
12019 Get_Next_Interp (I, It);
12020 end loop;
12021 end if;
12023 Get_First_Interp (Operand, I, It);
12024 I1 := I;
12025 It1 := It;
12027 if No (It.Typ) then
12028 Conversion_Error_N ("illegal operand in conversion", Operand);
12029 return False;
12030 end if;
12032 Get_Next_Interp (I, It);
12034 if Present (It.Typ) then
12035 N1 := It1.Nam;
12036 T1 := It1.Typ;
12037 It1 := Disambiguate (Operand, I1, I, Any_Type);
12039 if It1 = No_Interp then
12040 Conversion_Error_N
12041 ("ambiguous operand in conversion", Operand);
12043 -- If the interpretation involves a standard operator, use
12044 -- the location of the type, which may be user-defined.
12046 if Sloc (It.Nam) = Standard_Location then
12047 Error_Msg_Sloc := Sloc (It.Typ);
12048 else
12049 Error_Msg_Sloc := Sloc (It.Nam);
12050 end if;
12052 Conversion_Error_N -- CODEFIX
12053 ("\\possible interpretation#!", Operand);
12055 if Sloc (N1) = Standard_Location then
12056 Error_Msg_Sloc := Sloc (T1);
12057 else
12058 Error_Msg_Sloc := Sloc (N1);
12059 end if;
12061 Conversion_Error_N -- CODEFIX
12062 ("\\possible interpretation#!", Operand);
12064 return False;
12065 end if;
12066 end if;
12068 Set_Etype (Operand, It1.Typ);
12069 Opnd_Type := It1.Typ;
12070 end;
12071 end if;
12073 -- Deal with conversion of integer type to address if the pragma
12074 -- Allow_Integer_Address is in effect. We convert the conversion to
12075 -- an unchecked conversion in this case and we are all done.
12077 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
12078 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
12079 Analyze_And_Resolve (N, Target_Type);
12080 return True;
12081 end if;
12083 -- If we are within a child unit, check whether the type of the
12084 -- expression has an ancestor in a parent unit, in which case it
12085 -- belongs to its derivation class even if the ancestor is private.
12086 -- See RM 7.3.1 (5.2/3).
12088 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
12090 -- Numeric types
12092 if Is_Numeric_Type (Target_Type) then
12094 -- A universal fixed expression can be converted to any numeric type
12096 if Opnd_Type = Universal_Fixed then
12097 return True;
12099 -- Also no need to check when in an instance or inlined body, because
12100 -- the legality has been established when the template was analyzed.
12101 -- Furthermore, numeric conversions may occur where only a private
12102 -- view of the operand type is visible at the instantiation point.
12103 -- This results in a spurious error if we check that the operand type
12104 -- is a numeric type.
12106 -- Note: in a previous version of this unit, the following tests were
12107 -- applied only for generated code (Comes_From_Source set to False),
12108 -- but in fact the test is required for source code as well, since
12109 -- this situation can arise in source code.
12111 elsif In_Instance or else In_Inlined_Body then
12112 return True;
12114 -- Otherwise we need the conversion check
12116 else
12117 return Conversion_Check
12118 (Is_Numeric_Type (Opnd_Type)
12119 or else
12120 (Present (Inc_Ancestor)
12121 and then Is_Numeric_Type (Inc_Ancestor)),
12122 "illegal operand for numeric conversion");
12123 end if;
12125 -- Array types
12127 elsif Is_Array_Type (Target_Type) then
12128 if not Is_Array_Type (Opnd_Type)
12129 or else Opnd_Type = Any_Composite
12130 or else Opnd_Type = Any_String
12131 then
12132 Conversion_Error_N
12133 ("illegal operand for array conversion", Operand);
12134 return False;
12136 else
12137 return Valid_Array_Conversion;
12138 end if;
12140 -- Ada 2005 (AI-251): Internally generated conversions of access to
12141 -- interface types added to force the displacement of the pointer to
12142 -- reference the corresponding dispatch table.
12144 elsif not Comes_From_Source (N)
12145 and then Is_Access_Type (Target_Type)
12146 and then Is_Interface (Designated_Type (Target_Type))
12147 then
12148 return True;
12150 -- Ada 2005 (AI-251): Anonymous access types where target references an
12151 -- interface type.
12153 elsif Is_Access_Type (Opnd_Type)
12154 and then Ekind_In (Target_Type, E_General_Access_Type,
12155 E_Anonymous_Access_Type)
12156 and then Is_Interface (Directly_Designated_Type (Target_Type))
12157 then
12158 -- Check the static accessibility rule of 4.6(17). Note that the
12159 -- check is not enforced when within an instance body, since the
12160 -- RM requires such cases to be caught at run time.
12162 -- If the operand is a rewriting of an allocator no check is needed
12163 -- because there are no accessibility issues.
12165 if Nkind (Original_Node (N)) = N_Allocator then
12166 null;
12168 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
12169 if Type_Access_Level (Opnd_Type) >
12170 Deepest_Type_Access_Level (Target_Type)
12171 then
12172 -- In an instance, this is a run-time check, but one we know
12173 -- will fail, so generate an appropriate warning. The raise
12174 -- will be generated by Expand_N_Type_Conversion.
12176 if In_Instance_Body then
12177 Error_Msg_Warn := SPARK_Mode /= On;
12178 Conversion_Error_N
12179 ("cannot convert local pointer to non-local access type<<",
12180 Operand);
12181 Conversion_Error_N ("\Program_Error [<<", Operand);
12183 else
12184 Conversion_Error_N
12185 ("cannot convert local pointer to non-local access type",
12186 Operand);
12187 return False;
12188 end if;
12190 -- Special accessibility checks are needed in the case of access
12191 -- discriminants declared for a limited type.
12193 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
12194 and then not Is_Local_Anonymous_Access (Opnd_Type)
12195 then
12196 -- When the operand is a selected access discriminant the check
12197 -- needs to be made against the level of the object denoted by
12198 -- the prefix of the selected name (Object_Access_Level handles
12199 -- checking the prefix of the operand for this case).
12201 if Nkind (Operand) = N_Selected_Component
12202 and then Object_Access_Level (Operand) >
12203 Deepest_Type_Access_Level (Target_Type)
12204 then
12205 -- In an instance, this is a run-time check, but one we know
12206 -- will fail, so generate an appropriate warning. The raise
12207 -- will be generated by Expand_N_Type_Conversion.
12209 if In_Instance_Body then
12210 Error_Msg_Warn := SPARK_Mode /= On;
12211 Conversion_Error_N
12212 ("cannot convert access discriminant to non-local "
12213 & "access type<<", Operand);
12214 Conversion_Error_N ("\Program_Error [<<", Operand);
12216 -- Real error if not in instance body
12218 else
12219 Conversion_Error_N
12220 ("cannot convert access discriminant to non-local "
12221 & "access type", Operand);
12222 return False;
12223 end if;
12224 end if;
12226 -- The case of a reference to an access discriminant from
12227 -- within a limited type declaration (which will appear as
12228 -- a discriminal) is always illegal because the level of the
12229 -- discriminant is considered to be deeper than any (nameable)
12230 -- access type.
12232 if Is_Entity_Name (Operand)
12233 and then not Is_Local_Anonymous_Access (Opnd_Type)
12234 and then
12235 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
12236 and then Present (Discriminal_Link (Entity (Operand)))
12237 then
12238 Conversion_Error_N
12239 ("discriminant has deeper accessibility level than target",
12240 Operand);
12241 return False;
12242 end if;
12243 end if;
12244 end if;
12246 return True;
12248 -- General and anonymous access types
12250 elsif Ekind_In (Target_Type, E_General_Access_Type,
12251 E_Anonymous_Access_Type)
12252 and then
12253 Conversion_Check
12254 (Is_Access_Type (Opnd_Type)
12255 and then not
12256 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
12257 E_Access_Protected_Subprogram_Type),
12258 "must be an access-to-object type")
12259 then
12260 if Is_Access_Constant (Opnd_Type)
12261 and then not Is_Access_Constant (Target_Type)
12262 then
12263 Conversion_Error_N
12264 ("access-to-constant operand type not allowed", Operand);
12265 return False;
12266 end if;
12268 -- Check the static accessibility rule of 4.6(17). Note that the
12269 -- check is not enforced when within an instance body, since the RM
12270 -- requires such cases to be caught at run time.
12272 if Ekind (Target_Type) /= E_Anonymous_Access_Type
12273 or else Is_Local_Anonymous_Access (Target_Type)
12274 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
12275 N_Object_Declaration
12276 then
12277 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
12278 -- conversions from an anonymous access type to a named general
12279 -- access type. Such conversions are not allowed in the case of
12280 -- access parameters and stand-alone objects of an anonymous
12281 -- access type. The implicit conversion case is recognized by
12282 -- testing that Comes_From_Source is False and that it's been
12283 -- rewritten. The Comes_From_Source test isn't sufficient because
12284 -- nodes in inlined calls to predefined library routines can have
12285 -- Comes_From_Source set to False. (Is there a better way to test
12286 -- for implicit conversions???)
12288 if Ada_Version >= Ada_2012
12289 and then not Comes_From_Source (N)
12290 and then N /= Original_Node (N)
12291 and then Ekind (Target_Type) = E_General_Access_Type
12292 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
12293 then
12294 if Is_Itype (Opnd_Type) then
12296 -- Implicit conversions aren't allowed for objects of an
12297 -- anonymous access type, since such objects have nonstatic
12298 -- levels in Ada 2012.
12300 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
12301 N_Object_Declaration
12302 then
12303 Conversion_Error_N
12304 ("implicit conversion of stand-alone anonymous "
12305 & "access object not allowed", Operand);
12306 return False;
12308 -- Implicit conversions aren't allowed for anonymous access
12309 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
12310 -- is done to exclude anonymous access results.
12312 elsif not Is_Local_Anonymous_Access (Opnd_Type)
12313 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
12314 N_Function_Specification,
12315 N_Procedure_Specification)
12316 then
12317 Conversion_Error_N
12318 ("implicit conversion of anonymous access formal "
12319 & "not allowed", Operand);
12320 return False;
12322 -- This is a case where there's an enclosing object whose
12323 -- to which the "statically deeper than" relationship does
12324 -- not apply (such as an access discriminant selected from
12325 -- a dereference of an access parameter).
12327 elsif Object_Access_Level (Operand)
12328 = Scope_Depth (Standard_Standard)
12329 then
12330 Conversion_Error_N
12331 ("implicit conversion of anonymous access value "
12332 & "not allowed", Operand);
12333 return False;
12335 -- In other cases, the level of the operand's type must be
12336 -- statically less deep than that of the target type, else
12337 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
12339 elsif Type_Access_Level (Opnd_Type) >
12340 Deepest_Type_Access_Level (Target_Type)
12341 then
12342 Conversion_Error_N
12343 ("implicit conversion of anonymous access value "
12344 & "violates accessibility", Operand);
12345 return False;
12346 end if;
12347 end if;
12349 elsif Type_Access_Level (Opnd_Type) >
12350 Deepest_Type_Access_Level (Target_Type)
12351 then
12352 -- In an instance, this is a run-time check, but one we know
12353 -- will fail, so generate an appropriate warning. The raise
12354 -- will be generated by Expand_N_Type_Conversion.
12356 if In_Instance_Body then
12357 Error_Msg_Warn := SPARK_Mode /= On;
12358 Conversion_Error_N
12359 ("cannot convert local pointer to non-local access type<<",
12360 Operand);
12361 Conversion_Error_N ("\Program_Error [<<", Operand);
12363 -- If not in an instance body, this is a real error
12365 else
12366 -- Avoid generation of spurious error message
12368 if not Error_Posted (N) then
12369 Conversion_Error_N
12370 ("cannot convert local pointer to non-local access type",
12371 Operand);
12372 end if;
12374 return False;
12375 end if;
12377 -- Special accessibility checks are needed in the case of access
12378 -- discriminants declared for a limited type.
12380 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
12381 and then not Is_Local_Anonymous_Access (Opnd_Type)
12382 then
12383 -- When the operand is a selected access discriminant the check
12384 -- needs to be made against the level of the object denoted by
12385 -- the prefix of the selected name (Object_Access_Level handles
12386 -- checking the prefix of the operand for this case).
12388 if Nkind (Operand) = N_Selected_Component
12389 and then Object_Access_Level (Operand) >
12390 Deepest_Type_Access_Level (Target_Type)
12391 then
12392 -- In an instance, this is a run-time check, but one we know
12393 -- will fail, so generate an appropriate warning. The raise
12394 -- will be generated by Expand_N_Type_Conversion.
12396 if In_Instance_Body then
12397 Error_Msg_Warn := SPARK_Mode /= On;
12398 Conversion_Error_N
12399 ("cannot convert access discriminant to non-local "
12400 & "access type<<", Operand);
12401 Conversion_Error_N ("\Program_Error [<<", Operand);
12403 -- If not in an instance body, this is a real error
12405 else
12406 Conversion_Error_N
12407 ("cannot convert access discriminant to non-local "
12408 & "access type", Operand);
12409 return False;
12410 end if;
12411 end if;
12413 -- The case of a reference to an access discriminant from
12414 -- within a limited type declaration (which will appear as
12415 -- a discriminal) is always illegal because the level of the
12416 -- discriminant is considered to be deeper than any (nameable)
12417 -- access type.
12419 if Is_Entity_Name (Operand)
12420 and then
12421 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
12422 and then Present (Discriminal_Link (Entity (Operand)))
12423 then
12424 Conversion_Error_N
12425 ("discriminant has deeper accessibility level than target",
12426 Operand);
12427 return False;
12428 end if;
12429 end if;
12430 end if;
12432 -- In the presence of limited_with clauses we have to use nonlimited
12433 -- views, if available.
12435 Check_Limited : declare
12436 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
12437 -- Helper function to handle limited views
12439 --------------------------
12440 -- Full_Designated_Type --
12441 --------------------------
12443 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
12444 Desig : constant Entity_Id := Designated_Type (T);
12446 begin
12447 -- Handle the limited view of a type
12449 if From_Limited_With (Desig)
12450 and then Has_Non_Limited_View (Desig)
12451 then
12452 return Available_View (Desig);
12453 else
12454 return Desig;
12455 end if;
12456 end Full_Designated_Type;
12458 -- Local Declarations
12460 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
12461 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
12463 Same_Base : constant Boolean :=
12464 Base_Type (Target) = Base_Type (Opnd);
12466 -- Start of processing for Check_Limited
12468 begin
12469 if Is_Tagged_Type (Target) then
12470 return Valid_Tagged_Conversion (Target, Opnd);
12472 else
12473 if not Same_Base then
12474 Conversion_Error_NE
12475 ("target designated type not compatible with }",
12476 N, Base_Type (Opnd));
12477 return False;
12479 -- Ada 2005 AI-384: legality rule is symmetric in both
12480 -- designated types. The conversion is legal (with possible
12481 -- constraint check) if either designated type is
12482 -- unconstrained.
12484 elsif Subtypes_Statically_Match (Target, Opnd)
12485 or else
12486 (Has_Discriminants (Target)
12487 and then
12488 (not Is_Constrained (Opnd)
12489 or else not Is_Constrained (Target)))
12490 then
12491 -- Special case, if Value_Size has been used to make the
12492 -- sizes different, the conversion is not allowed even
12493 -- though the subtypes statically match.
12495 if Known_Static_RM_Size (Target)
12496 and then Known_Static_RM_Size (Opnd)
12497 and then RM_Size (Target) /= RM_Size (Opnd)
12498 then
12499 Conversion_Error_NE
12500 ("target designated subtype not compatible with }",
12501 N, Opnd);
12502 Conversion_Error_NE
12503 ("\because sizes of the two designated subtypes differ",
12504 N, Opnd);
12505 return False;
12507 -- Normal case where conversion is allowed
12509 else
12510 return True;
12511 end if;
12513 else
12514 Error_Msg_NE
12515 ("target designated subtype not compatible with }",
12516 N, Opnd);
12517 return False;
12518 end if;
12519 end if;
12520 end Check_Limited;
12522 -- Access to subprogram types. If the operand is an access parameter,
12523 -- the type has a deeper accessibility that any master, and cannot be
12524 -- assigned. We must make an exception if the conversion is part of an
12525 -- assignment and the target is the return object of an extended return
12526 -- statement, because in that case the accessibility check takes place
12527 -- after the return.
12529 elsif Is_Access_Subprogram_Type (Target_Type)
12531 -- Note: this test of Opnd_Type is there to prevent entering this
12532 -- branch in the case of a remote access to subprogram type, which
12533 -- is internally represented as an E_Record_Type.
12535 and then Is_Access_Type (Opnd_Type)
12536 then
12537 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
12538 and then Is_Entity_Name (Operand)
12539 and then Ekind (Entity (Operand)) = E_In_Parameter
12540 and then
12541 (Nkind (Parent (N)) /= N_Assignment_Statement
12542 or else not Is_Entity_Name (Name (Parent (N)))
12543 or else not Is_Return_Object (Entity (Name (Parent (N)))))
12544 then
12545 Conversion_Error_N
12546 ("illegal attempt to store anonymous access to subprogram",
12547 Operand);
12548 Conversion_Error_N
12549 ("\value has deeper accessibility than any master "
12550 & "(RM 3.10.2 (13))",
12551 Operand);
12553 Error_Msg_NE
12554 ("\use named access type for& instead of access parameter",
12555 Operand, Entity (Operand));
12556 end if;
12558 -- Check that the designated types are subtype conformant
12560 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
12561 Old_Id => Designated_Type (Opnd_Type),
12562 Err_Loc => N);
12564 -- Check the static accessibility rule of 4.6(20)
12566 if Type_Access_Level (Opnd_Type) >
12567 Deepest_Type_Access_Level (Target_Type)
12568 then
12569 Conversion_Error_N
12570 ("operand type has deeper accessibility level than target",
12571 Operand);
12573 -- Check that if the operand type is declared in a generic body,
12574 -- then the target type must be declared within that same body
12575 -- (enforces last sentence of 4.6(20)).
12577 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
12578 declare
12579 O_Gen : constant Node_Id :=
12580 Enclosing_Generic_Body (Opnd_Type);
12582 T_Gen : Node_Id;
12584 begin
12585 T_Gen := Enclosing_Generic_Body (Target_Type);
12586 while Present (T_Gen) and then T_Gen /= O_Gen loop
12587 T_Gen := Enclosing_Generic_Body (T_Gen);
12588 end loop;
12590 if T_Gen /= O_Gen then
12591 Conversion_Error_N
12592 ("target type must be declared in same generic body "
12593 & "as operand type", N);
12594 end if;
12595 end;
12596 end if;
12598 return True;
12600 -- Remote access to subprogram types
12602 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
12603 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
12604 then
12605 -- It is valid to convert from one RAS type to another provided
12606 -- that their specification statically match.
12608 -- Note: at this point, remote access to subprogram types have been
12609 -- expanded to their E_Record_Type representation, and we need to
12610 -- go back to the original access type definition using the
12611 -- Corresponding_Remote_Type attribute in order to check that the
12612 -- designated profiles match.
12614 pragma Assert (Ekind (Target_Type) = E_Record_Type);
12615 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
12617 Check_Subtype_Conformant
12618 (New_Id =>
12619 Designated_Type (Corresponding_Remote_Type (Target_Type)),
12620 Old_Id =>
12621 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
12622 Err_Loc =>
12624 return True;
12626 -- If it was legal in the generic, it's legal in the instance
12628 elsif In_Instance_Body then
12629 return True;
12631 -- If both are tagged types, check legality of view conversions
12633 elsif Is_Tagged_Type (Target_Type)
12634 and then
12635 Is_Tagged_Type (Opnd_Type)
12636 then
12637 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
12639 -- Types derived from the same root type are convertible
12641 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
12642 return True;
12644 -- In an instance or an inlined body, there may be inconsistent views of
12645 -- the same type, or of types derived from a common root.
12647 elsif (In_Instance or In_Inlined_Body)
12648 and then
12649 Root_Type (Underlying_Type (Target_Type)) =
12650 Root_Type (Underlying_Type (Opnd_Type))
12651 then
12652 return True;
12654 -- Special check for common access type error case
12656 elsif Ekind (Target_Type) = E_Access_Type
12657 and then Is_Access_Type (Opnd_Type)
12658 then
12659 Conversion_Error_N ("target type must be general access type!", N);
12660 Conversion_Error_NE -- CODEFIX
12661 ("add ALL to }!", N, Target_Type);
12662 return False;
12664 -- Here we have a real conversion error
12666 else
12667 Conversion_Error_NE
12668 ("invalid conversion, not compatible with }", N, Opnd_Type);
12669 return False;
12670 end if;
12671 end Valid_Conversion;
12673 end Sem_Res;