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