* arm.c (FL_WBUF): Define.
[official-gcc.git] / gcc / ada / sem_res.adb
blobcc55d26d2d511fdd710b5b1a4644382699aa1181
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-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Debug_A; use Debug_A;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Tss; use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Freeze; use Freeze;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Lib.Xref; use Lib.Xref;
41 with Namet; use Namet;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aggr; use Sem_Aggr;
51 with Sem_Attr; use Sem_Attr;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch4; use Sem_Ch4;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Intr; use Sem_Intr;
61 with Sem_Util; use Sem_Util;
62 with Sem_Type; use Sem_Type;
63 with Sem_Warn; use Sem_Warn;
64 with Sinfo; use Sinfo;
65 with Snames; use Snames;
66 with Stand; use Stand;
67 with Stringt; use Stringt;
68 with Targparm; use Targparm;
69 with Tbuild; use Tbuild;
70 with Uintp; use Uintp;
71 with Urealp; use Urealp;
73 package body Sem_Res is
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 -- Second pass (top-down) type checking and overload resolution procedures
80 -- Typ is the type required by context. These procedures propagate the
81 -- type information recursively to the descendants of N. If the node
82 -- is not overloaded, its Etype is established in the first pass. If
83 -- overloaded, the Resolve routines set the correct type. For arith.
84 -- operators, the Etype is the base type of the context.
86 -- Note that Resolve_Attribute is separated off in Sem_Attr
88 procedure Ambiguous_Character (C : Node_Id);
89 -- Give list of candidate interpretations when a character literal cannot
90 -- be resolved.
92 procedure Check_Direct_Boolean_Op (N : Node_Id);
93 -- N is a binary operator node which may possibly operate on Boolean
94 -- operands. If the operator does have Boolean operands, then a call is
95 -- made to check the restriction No_Direct_Boolean_Operators.
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 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
108 -- Given a call node, N, which is known to occur immediately within the
109 -- subprogram being called, determines whether it is a detectable case of
110 -- an infinite recursion, and if so, outputs appropriate messages. Returns
111 -- True if an infinite recursion is detected, and False otherwise.
113 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
114 -- If the type of the object being initialized uses the secondary stack
115 -- directly or indirectly, create a transient scope for the call to the
116 -- init proc. This is because we do not create transient scopes for the
117 -- initialization of individual components within the init proc itself.
118 -- Could be optimized away perhaps?
120 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
121 -- Utility to check whether the name in the call is a predefined
122 -- operator, in which case the call is made into an operator node.
123 -- An instance of an intrinsic conversion operation may be given
124 -- an operator name, but is not treated like an operator.
126 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
127 -- If a default expression in entry call N depends on the discriminants
128 -- of the task, it must be replaced with a reference to the discriminant
129 -- of the task being called.
131 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
132 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
133 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
134 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
135 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
136 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
137 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
138 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
139 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
140 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
141 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
142 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
143 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
144 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
145 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
146 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
147 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
148 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
149 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
150 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
151 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
152 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
153 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
154 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
155 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
156 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
157 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
158 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
159 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
160 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
161 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
162 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
164 function Operator_Kind
165 (Op_Name : Name_Id;
166 Is_Binary : Boolean) return Node_Kind;
167 -- Utility to map the name of an operator into the corresponding Node. Used
168 -- by other node rewriting procedures.
170 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
171 -- Resolve actuals of call, and add default expressions for missing ones.
172 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
173 -- called subprogram.
175 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
176 -- Called from Resolve_Call, when the prefix denotes an entry or element
177 -- of entry family. Actuals are resolved as for subprograms, and the node
178 -- is rebuilt as an entry call. Also called for protected operations. Typ
179 -- is the context type, which is used when the operation is a protected
180 -- function with no arguments, and the return value is indexed.
182 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
183 -- A call to a user-defined intrinsic operator is rewritten as a call
184 -- to the corresponding predefined operator, with suitable conversions.
186 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
187 -- Ditto, for unary operators (only arithmetic ones)
189 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
190 -- If an operator node resolves to a call to a user-defined operator,
191 -- rewrite the node as a function call.
193 procedure Make_Call_Into_Operator
194 (N : Node_Id;
195 Typ : Entity_Id;
196 Op_Id : Entity_Id);
197 -- Inverse transformation: if an operator is given in functional notation,
198 -- then after resolving the node, transform into an operator node, so
199 -- that operands are resolved properly. Recall that predefined operators
200 -- do not have a full signature and special resolution rules apply.
202 procedure Rewrite_Renamed_Operator
203 (N : Node_Id;
204 Op : Entity_Id;
205 Typ : Entity_Id);
206 -- An operator can rename another, e.g. in an instantiation. In that
207 -- case, the proper operator node must be constructed and resolved.
209 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
210 -- The String_Literal_Subtype is built for all strings that are not
211 -- operands of a static concatenation operation. If the argument is
212 -- not a N_String_Literal node, then the call has no effect.
214 procedure Set_Slice_Subtype (N : Node_Id);
215 -- Build subtype of array type, with the range specified by the slice
217 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
218 -- A universal_fixed expression in an universal context is unambiguous
219 -- if there is only one applicable fixed point type. Determining whether
220 -- there is only one requires a search over all visible entities, and
221 -- happens only in very pathological cases (see 6115-006).
223 function Valid_Conversion
224 (N : Node_Id;
225 Target : Entity_Id;
226 Operand : Node_Id) return Boolean;
227 -- Verify legality rules given in 4.6 (8-23). Target is the target
228 -- type of the conversion, which may be an implicit conversion of
229 -- an actual parameter to an anonymous access type (in which case
230 -- N denotes the actual parameter and N = Operand).
232 -------------------------
233 -- Ambiguous_Character --
234 -------------------------
236 procedure Ambiguous_Character (C : Node_Id) is
237 E : Entity_Id;
239 begin
240 if Nkind (C) = N_Character_Literal then
241 Error_Msg_N ("ambiguous character literal", C);
242 Error_Msg_N
243 ("\possible interpretations: Character, Wide_Character!", C);
245 E := Current_Entity (C);
247 if Present (E) then
249 while Present (E) loop
250 Error_Msg_NE ("\possible interpretation:}!", C, Etype (E));
251 E := Homonym (E);
252 end loop;
253 end if;
254 end if;
255 end Ambiguous_Character;
257 -------------------------
258 -- Analyze_And_Resolve --
259 -------------------------
261 procedure Analyze_And_Resolve (N : Node_Id) is
262 begin
263 Analyze (N);
264 Resolve (N);
265 end Analyze_And_Resolve;
267 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
268 begin
269 Analyze (N);
270 Resolve (N, Typ);
271 end Analyze_And_Resolve;
273 -- Version withs check(s) suppressed
275 procedure Analyze_And_Resolve
276 (N : Node_Id;
277 Typ : Entity_Id;
278 Suppress : Check_Id)
280 Scop : constant Entity_Id := Current_Scope;
282 begin
283 if Suppress = All_Checks then
284 declare
285 Svg : constant Suppress_Array := Scope_Suppress;
287 begin
288 Scope_Suppress := (others => True);
289 Analyze_And_Resolve (N, Typ);
290 Scope_Suppress := Svg;
291 end;
293 else
294 declare
295 Svg : constant Boolean := Scope_Suppress (Suppress);
297 begin
298 Scope_Suppress (Suppress) := True;
299 Analyze_And_Resolve (N, Typ);
300 Scope_Suppress (Suppress) := Svg;
301 end;
302 end if;
304 if Current_Scope /= Scop
305 and then Scope_Is_Transient
306 then
307 -- This can only happen if a transient scope was created
308 -- for an inner expression, which will be removed upon
309 -- completion of the analysis of an enclosing construct.
310 -- The transient scope must have the suppress status of
311 -- the enclosing environment, not of this Analyze call.
313 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
314 Scope_Suppress;
315 end if;
316 end Analyze_And_Resolve;
318 procedure Analyze_And_Resolve
319 (N : Node_Id;
320 Suppress : Check_Id)
322 Scop : constant Entity_Id := Current_Scope;
324 begin
325 if Suppress = All_Checks then
326 declare
327 Svg : constant Suppress_Array := Scope_Suppress;
329 begin
330 Scope_Suppress := (others => True);
331 Analyze_And_Resolve (N);
332 Scope_Suppress := Svg;
333 end;
335 else
336 declare
337 Svg : constant Boolean := Scope_Suppress (Suppress);
339 begin
340 Scope_Suppress (Suppress) := True;
341 Analyze_And_Resolve (N);
342 Scope_Suppress (Suppress) := Svg;
343 end;
344 end if;
346 if Current_Scope /= Scop
347 and then Scope_Is_Transient
348 then
349 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
350 Scope_Suppress;
351 end if;
352 end Analyze_And_Resolve;
354 -----------------------------
355 -- Check_Direct_Boolean_Op --
356 -----------------------------
358 procedure Check_Direct_Boolean_Op (N : Node_Id) is
359 begin
360 if Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean then
361 Check_Restriction (No_Direct_Boolean_Operators, N);
362 end if;
363 end Check_Direct_Boolean_Op;
365 ----------------------------
366 -- Check_Discriminant_Use --
367 ----------------------------
369 procedure Check_Discriminant_Use (N : Node_Id) is
370 PN : constant Node_Id := Parent (N);
371 Disc : constant Entity_Id := Entity (N);
372 P : Node_Id;
373 D : Node_Id;
375 begin
376 -- Any use in a default expression is legal
378 if In_Default_Expression then
379 null;
381 elsif Nkind (PN) = N_Range then
383 -- Discriminant cannot be used to constrain a scalar type
385 P := Parent (PN);
387 if Nkind (P) = N_Range_Constraint
388 and then Nkind (Parent (P)) = N_Subtype_Indication
389 and then Nkind (Parent (Parent (P))) = N_Component_Definition
390 then
391 Error_Msg_N ("discriminant cannot constrain scalar type", N);
393 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
395 -- The following check catches the unusual case where
396 -- a discriminant appears within an index constraint
397 -- that is part of a larger expression within a constraint
398 -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
399 -- For now we only check case of record components, and
400 -- note that a similar check should also apply in the
401 -- case of discriminant constraints below. ???
403 -- Note that the check for N_Subtype_Declaration below is to
404 -- detect the valid use of discriminants in the constraints of a
405 -- subtype declaration when this subtype declaration appears
406 -- inside the scope of a record type (which is syntactically
407 -- illegal, but which may be created as part of derived type
408 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
409 -- for more info.
411 if Ekind (Current_Scope) = E_Record_Type
412 and then Scope (Disc) = Current_Scope
413 and then not
414 (Nkind (Parent (P)) = N_Subtype_Indication
415 and then
416 (Nkind (Parent (Parent (P))) = N_Component_Definition
417 or else
418 Nkind (Parent (Parent (P))) = N_Subtype_Declaration)
419 and then Paren_Count (N) = 0)
420 then
421 Error_Msg_N
422 ("discriminant must appear alone in component constraint", N);
423 return;
424 end if;
426 -- Detect a common beginner error:
428 -- type R (D : Positive := 100) is record
429 -- Name : String (1 .. D);
430 -- end record;
432 -- The default value causes an object of type R to be
433 -- allocated with room for Positive'Last characters.
435 declare
436 SI : Node_Id;
437 T : Entity_Id;
438 TB : Node_Id;
439 CB : Entity_Id;
441 function Large_Storage_Type (T : Entity_Id) return Boolean;
442 -- Return True if type T has a large enough range that
443 -- any array whose index type covered the whole range of
444 -- the type would likely raise Storage_Error.
446 ------------------------
447 -- Large_Storage_Type --
448 ------------------------
450 function Large_Storage_Type (T : Entity_Id) return Boolean is
451 begin
452 return
453 T = Standard_Integer
454 or else
455 T = Standard_Positive
456 or else
457 T = Standard_Natural;
458 end Large_Storage_Type;
460 begin
461 -- Check that the Disc has a large range
463 if not Large_Storage_Type (Etype (Disc)) then
464 goto No_Danger;
465 end if;
467 -- If the enclosing type is limited, we allocate only the
468 -- default value, not the maximum, and there is no need for
469 -- a warning.
471 if Is_Limited_Type (Scope (Disc)) then
472 goto No_Danger;
473 end if;
475 -- Check that it is the high bound
477 if N /= High_Bound (PN)
478 or else not Present (Discriminant_Default_Value (Disc))
479 then
480 goto No_Danger;
481 end if;
483 -- Check the array allows a large range at this bound.
484 -- First find the array
486 SI := Parent (P);
488 if Nkind (SI) /= N_Subtype_Indication then
489 goto No_Danger;
490 end if;
492 T := Entity (Subtype_Mark (SI));
494 if not Is_Array_Type (T) then
495 goto No_Danger;
496 end if;
498 -- Next, find the dimension
500 TB := First_Index (T);
501 CB := First (Constraints (P));
502 while True
503 and then Present (TB)
504 and then Present (CB)
505 and then CB /= PN
506 loop
507 Next_Index (TB);
508 Next (CB);
509 end loop;
511 if CB /= PN then
512 goto No_Danger;
513 end if;
515 -- Now, check the dimension has a large range
517 if not Large_Storage_Type (Etype (TB)) then
518 goto No_Danger;
519 end if;
521 -- Warn about the danger
523 Error_Msg_N
524 ("creation of & object may raise Storage_Error?",
525 Scope (Disc));
527 <<No_Danger>>
528 null;
530 end;
531 end if;
533 -- Legal case is in index or discriminant constraint
535 elsif Nkind (PN) = N_Index_Or_Discriminant_Constraint
536 or else Nkind (PN) = N_Discriminant_Association
537 then
538 if Paren_Count (N) > 0 then
539 Error_Msg_N
540 ("discriminant in constraint must appear alone", N);
541 end if;
543 return;
545 -- Otherwise, context is an expression. It should not be within
546 -- (i.e. a subexpression of) a constraint for a component.
548 else
549 D := PN;
550 P := Parent (PN);
552 while Nkind (P) /= N_Component_Declaration
553 and then Nkind (P) /= N_Subtype_Indication
554 and then Nkind (P) /= N_Entry_Declaration
555 loop
556 D := P;
557 P := Parent (P);
558 exit when No (P);
559 end loop;
561 -- If the discriminant is used in an expression that is a bound
562 -- of a scalar type, an Itype is created and the bounds are attached
563 -- to its range, not to the original subtype indication. Such use
564 -- is of course a double fault.
566 if (Nkind (P) = N_Subtype_Indication
567 and then
568 (Nkind (Parent (P)) = N_Component_Definition
569 or else
570 Nkind (Parent (P)) = N_Derived_Type_Definition)
571 and then D = Constraint (P))
573 -- The constraint itself may be given by a subtype indication,
574 -- rather than by a more common discrete range.
576 or else (Nkind (P) = N_Subtype_Indication
577 and then
578 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
579 or else Nkind (P) = N_Entry_Declaration
580 or else Nkind (D) = N_Defining_Identifier
581 then
582 Error_Msg_N
583 ("discriminant in constraint must appear alone", N);
584 end if;
585 end if;
586 end Check_Discriminant_Use;
588 --------------------------------
589 -- Check_For_Visible_Operator --
590 --------------------------------
592 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
593 begin
594 if Is_Invisible_Operator (N, T) then
595 Error_Msg_NE
596 ("operator for} is not directly visible!", N, First_Subtype (T));
597 Error_Msg_N ("use clause would make operation legal!", N);
598 end if;
599 end Check_For_Visible_Operator;
601 ------------------------------
602 -- Check_Infinite_Recursion --
603 ------------------------------
605 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
606 P : Node_Id;
607 C : Node_Id;
609 function Same_Argument_List return Boolean;
610 -- Check whether list of actuals is identical to list of formals
611 -- of called function (which is also the enclosing scope).
613 ------------------------
614 -- Same_Argument_List --
615 ------------------------
617 function Same_Argument_List return Boolean is
618 A : Node_Id;
619 F : Entity_Id;
620 Subp : Entity_Id;
622 begin
623 if not Is_Entity_Name (Name (N)) then
624 return False;
625 else
626 Subp := Entity (Name (N));
627 end if;
629 F := First_Formal (Subp);
630 A := First_Actual (N);
631 while Present (F) and then Present (A) loop
632 if not Is_Entity_Name (A)
633 or else Entity (A) /= F
634 then
635 return False;
636 end if;
638 Next_Actual (A);
639 Next_Formal (F);
640 end loop;
642 return True;
643 end Same_Argument_List;
645 -- Start of processing for Check_Infinite_Recursion
647 begin
648 -- Loop moving up tree, quitting if something tells us we are
649 -- definitely not in an infinite recursion situation.
651 C := N;
652 loop
653 P := Parent (C);
654 exit when Nkind (P) = N_Subprogram_Body;
656 if Nkind (P) = N_Or_Else or else
657 Nkind (P) = N_And_Then or else
658 Nkind (P) = N_If_Statement or else
659 Nkind (P) = N_Case_Statement
660 then
661 return False;
663 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
664 and then C /= First (Statements (P))
665 then
666 -- If the call is the expression of a return statement and
667 -- the actuals are identical to the formals, it's worth a
668 -- warning. However, we skip this if there is an immediately
669 -- preceding raise statement, since the call is never executed.
671 -- Furthermore, this corresponds to a common idiom:
673 -- function F (L : Thing) return Boolean is
674 -- begin
675 -- raise Program_Error;
676 -- return F (L);
677 -- end F;
679 -- for generating a stub function
681 if Nkind (Parent (N)) = N_Return_Statement
682 and then Same_Argument_List
683 then
684 exit when not Is_List_Member (Parent (N))
685 or else (Nkind (Prev (Parent (N))) /= N_Raise_Statement
686 and then
687 (Nkind (Prev (Parent (N))) not in N_Raise_xxx_Error
688 or else
689 Present (Condition (Prev (Parent (N))))));
690 end if;
692 return False;
694 else
695 C := P;
696 end if;
697 end loop;
699 Error_Msg_N ("possible infinite recursion?", N);
700 Error_Msg_N ("\Storage_Error may be raised at run time?", N);
702 return True;
703 end Check_Infinite_Recursion;
705 -------------------------------
706 -- Check_Initialization_Call --
707 -------------------------------
709 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
710 Typ : constant Entity_Id := Etype (First_Formal (Nam));
712 function Uses_SS (T : Entity_Id) return Boolean;
713 -- Check whether the creation of an object of the type will involve
714 -- use of the secondary stack. If T is a record type, this is true
715 -- if the expression for some component uses the secondary stack, eg.
716 -- through a call to a function that returns an unconstrained value.
717 -- False if T is controlled, because cleanups occur elsewhere.
719 -------------
720 -- Uses_SS --
721 -------------
723 function Uses_SS (T : Entity_Id) return Boolean is
724 Comp : Entity_Id;
725 Expr : Node_Id;
727 begin
728 if Is_Controlled (T) then
729 return False;
731 elsif Is_Array_Type (T) then
732 return Uses_SS (Component_Type (T));
734 elsif Is_Record_Type (T) then
735 Comp := First_Component (T);
737 while Present (Comp) loop
739 if Ekind (Comp) = E_Component
740 and then Nkind (Parent (Comp)) = N_Component_Declaration
741 then
742 Expr := Expression (Parent (Comp));
744 -- The expression for a dynamic component may be
745 -- rewritten as a dereference. Retrieve original
746 -- call.
748 if Nkind (Original_Node (Expr)) = N_Function_Call
749 and then Requires_Transient_Scope (Etype (Expr))
750 then
751 return True;
753 elsif Uses_SS (Etype (Comp)) then
754 return True;
755 end if;
756 end if;
758 Next_Component (Comp);
759 end loop;
761 return False;
763 else
764 return False;
765 end if;
766 end Uses_SS;
768 -- Start of processing for Check_Initialization_Call
770 begin
771 -- Nothing to do if functions do not use the secondary stack for
772 -- returns (i.e. they use a depressed stack pointer instead).
774 if Functions_Return_By_DSP_On_Target then
775 return;
777 -- Otherwise establish a transient scope if the type needs it
779 elsif Uses_SS (Typ) then
780 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
781 end if;
782 end Check_Initialization_Call;
784 ------------------------------
785 -- Check_Parameterless_Call --
786 ------------------------------
788 procedure Check_Parameterless_Call (N : Node_Id) is
789 Nam : Node_Id;
791 function Prefix_Is_Access_Subp return Boolean;
792 -- If the prefix is of an access_to_subprogram type, the node must be
793 -- rewritten as a call. Ditto if the prefix is overloaded and all its
794 -- interpretations are access to subprograms.
796 ---------------------------
797 -- Prefix_Is_Access_Subp --
798 ---------------------------
800 function Prefix_Is_Access_Subp return Boolean is
801 I : Interp_Index;
802 It : Interp;
804 begin
805 if not Is_Overloaded (N) then
806 return
807 Ekind (Etype (N)) = E_Subprogram_Type
808 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
809 else
810 Get_First_Interp (N, I, It);
811 while Present (It.Typ) loop
812 if Ekind (It.Typ) /= E_Subprogram_Type
813 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
814 then
815 return False;
816 end if;
818 Get_Next_Interp (I, It);
819 end loop;
821 return True;
822 end if;
823 end Prefix_Is_Access_Subp;
825 -- Start of processing for Check_Parameterless_Call
827 begin
828 -- Defend against junk stuff if errors already detected
830 if Total_Errors_Detected /= 0 then
831 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
832 return;
833 elsif Nkind (N) in N_Has_Chars
834 and then Chars (N) in Error_Name_Or_No_Name
835 then
836 return;
837 end if;
839 Require_Entity (N);
840 end if;
842 -- If the context expects a value, and the name is a procedure,
843 -- this is most likely a missing 'Access. Do not try to resolve
844 -- the parameterless call, error will be caught when the outer
845 -- call is analyzed.
847 if Is_Entity_Name (N)
848 and then Ekind (Entity (N)) = E_Procedure
849 and then not Is_Overloaded (N)
850 and then
851 (Nkind (Parent (N)) = N_Parameter_Association
852 or else Nkind (Parent (N)) = N_Function_Call
853 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
854 then
855 return;
856 end if;
858 -- Rewrite as call if overloadable entity that is (or could be, in
859 -- the overloaded case) a function call. If we know for sure that
860 -- the entity is an enumeration literal, we do not rewrite it.
862 if (Is_Entity_Name (N)
863 and then Is_Overloadable (Entity (N))
864 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
865 or else Is_Overloaded (N)))
867 -- Rewrite as call if it is an explicit deference of an expression of
868 -- a subprogram access type, and the suprogram type is not that of a
869 -- procedure or entry.
871 or else
872 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
874 -- Rewrite as call if it is a selected component which is a function,
875 -- this is the case of a call to a protected function (which may be
876 -- overloaded with other protected operations).
878 or else
879 (Nkind (N) = N_Selected_Component
880 and then (Ekind (Entity (Selector_Name (N))) = E_Function
881 or else
882 ((Ekind (Entity (Selector_Name (N))) = E_Entry
883 or else
884 Ekind (Entity (Selector_Name (N))) = E_Procedure)
885 and then Is_Overloaded (Selector_Name (N)))))
887 -- If one of the above three conditions is met, rewrite as call.
888 -- Apply the rewriting only once.
890 then
891 if Nkind (Parent (N)) /= N_Function_Call
892 or else N /= Name (Parent (N))
893 then
894 Nam := New_Copy (N);
896 -- If overloaded, overload set belongs to new copy
898 Save_Interps (N, Nam);
900 -- Change node to parameterless function call (note that the
901 -- Parameter_Associations associations field is left set to Empty,
902 -- its normal default value since there are no parameters)
904 Change_Node (N, N_Function_Call);
905 Set_Name (N, Nam);
906 Set_Sloc (N, Sloc (Nam));
907 Analyze_Call (N);
908 end if;
910 elsif Nkind (N) = N_Parameter_Association then
911 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
912 end if;
913 end Check_Parameterless_Call;
915 ----------------------
916 -- Is_Predefined_Op --
917 ----------------------
919 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
920 begin
921 return Is_Intrinsic_Subprogram (Nam)
922 and then not Is_Generic_Instance (Nam)
923 and then Chars (Nam) in Any_Operator_Name
924 and then (No (Alias (Nam))
925 or else Is_Predefined_Op (Alias (Nam)));
926 end Is_Predefined_Op;
928 -----------------------------
929 -- Make_Call_Into_Operator --
930 -----------------------------
932 procedure Make_Call_Into_Operator
933 (N : Node_Id;
934 Typ : Entity_Id;
935 Op_Id : Entity_Id)
937 Op_Name : constant Name_Id := Chars (Op_Id);
938 Act1 : Node_Id := First_Actual (N);
939 Act2 : Node_Id := Next_Actual (Act1);
940 Error : Boolean := False;
941 Func : constant Entity_Id := Entity (Name (N));
942 Is_Binary : constant Boolean := Present (Act2);
943 Op_Node : Node_Id;
944 Opnd_Type : Entity_Id;
945 Orig_Type : Entity_Id := Empty;
946 Pack : Entity_Id;
948 type Kind_Test is access function (E : Entity_Id) return Boolean;
950 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
951 -- Determine whether E is an access type declared by an access decla-
952 -- ration, and not an (anonymous) allocator type.
954 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
955 -- If the operand is not universal, and the operator is given by a
956 -- expanded name, verify that the operand has an interpretation with
957 -- a type defined in the given scope of the operator.
959 function Type_In_P (Test : Kind_Test) return Entity_Id;
960 -- Find a type of the given class in the package Pack that contains
961 -- the operator.
963 -----------------------------
964 -- Is_Definite_Access_Type --
965 -----------------------------
967 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
968 Btyp : constant Entity_Id := Base_Type (E);
969 begin
970 return Ekind (Btyp) = E_Access_Type
971 or else (Ekind (Btyp) = E_Access_Subprogram_Type
972 and then Comes_From_Source (Btyp));
973 end Is_Definite_Access_Type;
975 ---------------------------
976 -- Operand_Type_In_Scope --
977 ---------------------------
979 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
980 Nod : constant Node_Id := Right_Opnd (Op_Node);
981 I : Interp_Index;
982 It : Interp;
984 begin
985 if not Is_Overloaded (Nod) then
986 return Scope (Base_Type (Etype (Nod))) = S;
988 else
989 Get_First_Interp (Nod, I, It);
991 while Present (It.Typ) loop
993 if Scope (Base_Type (It.Typ)) = S then
994 return True;
995 end if;
997 Get_Next_Interp (I, It);
998 end loop;
1000 return False;
1001 end if;
1002 end Operand_Type_In_Scope;
1004 ---------------
1005 -- Type_In_P --
1006 ---------------
1008 function Type_In_P (Test : Kind_Test) return Entity_Id is
1009 E : Entity_Id;
1011 function In_Decl return Boolean;
1012 -- Verify that node is not part of the type declaration for the
1013 -- candidate type, which would otherwise be invisible.
1015 -------------
1016 -- In_Decl --
1017 -------------
1019 function In_Decl return Boolean is
1020 Decl_Node : constant Node_Id := Parent (E);
1021 N2 : Node_Id;
1023 begin
1024 N2 := N;
1026 if Etype (E) = Any_Type then
1027 return True;
1029 elsif No (Decl_Node) then
1030 return False;
1032 else
1033 while Present (N2)
1034 and then Nkind (N2) /= N_Compilation_Unit
1035 loop
1036 if N2 = Decl_Node then
1037 return True;
1038 else
1039 N2 := Parent (N2);
1040 end if;
1041 end loop;
1043 return False;
1044 end if;
1045 end In_Decl;
1047 -- Start of processing for Type_In_P
1049 begin
1050 -- If the context type is declared in the prefix package, this
1051 -- is the desired base type.
1053 if Scope (Base_Type (Typ)) = Pack
1054 and then Test (Typ)
1055 then
1056 return Base_Type (Typ);
1058 else
1059 E := First_Entity (Pack);
1061 while Present (E) loop
1063 if Test (E)
1064 and then not In_Decl
1065 then
1066 return E;
1067 end if;
1069 Next_Entity (E);
1070 end loop;
1072 return Empty;
1073 end if;
1074 end Type_In_P;
1076 -- Start of processing for Make_Call_Into_Operator
1078 begin
1079 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1081 -- Binary operator
1083 if Is_Binary then
1084 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1085 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1086 Save_Interps (Act1, Left_Opnd (Op_Node));
1087 Save_Interps (Act2, Right_Opnd (Op_Node));
1088 Act1 := Left_Opnd (Op_Node);
1089 Act2 := Right_Opnd (Op_Node);
1091 -- Unary operator
1093 else
1094 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1095 Save_Interps (Act1, Right_Opnd (Op_Node));
1096 Act1 := Right_Opnd (Op_Node);
1097 end if;
1099 -- If the operator is denoted by an expanded name, and the prefix is
1100 -- not Standard, but the operator is a predefined one whose scope is
1101 -- Standard, then this is an implicit_operator, inserted as an
1102 -- interpretation by the procedure of the same name. This procedure
1103 -- overestimates the presence of implicit operators, because it does
1104 -- not examine the type of the operands. Verify now that the operand
1105 -- type appears in the given scope. If right operand is universal,
1106 -- check the other operand. In the case of concatenation, either
1107 -- argument can be the component type, so check the type of the result.
1108 -- If both arguments are literals, look for a type of the right kind
1109 -- defined in the given scope. This elaborate nonsense is brought to
1110 -- you courtesy of b33302a. The type itself must be frozen, so we must
1111 -- find the type of the proper class in the given scope.
1113 -- A final wrinkle is the multiplication operator for fixed point
1114 -- types, which is defined in Standard only, and not in the scope of
1115 -- the fixed_point type itself.
1117 if Nkind (Name (N)) = N_Expanded_Name then
1118 Pack := Entity (Prefix (Name (N)));
1120 -- If the entity being called is defined in the given package,
1121 -- it is a renaming of a predefined operator, and known to be
1122 -- legal.
1124 if Scope (Entity (Name (N))) = Pack
1125 and then Pack /= Standard_Standard
1126 then
1127 null;
1129 elsif (Op_Name = Name_Op_Multiply
1130 or else Op_Name = Name_Op_Divide)
1131 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1132 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1133 then
1134 if Pack /= Standard_Standard then
1135 Error := True;
1136 end if;
1138 else
1139 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1141 if Op_Name = Name_Op_Concat then
1142 Opnd_Type := Base_Type (Typ);
1144 elsif (Scope (Opnd_Type) = Standard_Standard
1145 and then Is_Binary)
1146 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1147 and then Is_Binary
1148 and then not Comes_From_Source (Opnd_Type))
1149 then
1150 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1151 end if;
1153 if Scope (Opnd_Type) = Standard_Standard then
1155 -- Verify that the scope contains a type that corresponds to
1156 -- the given literal. Optimize the case where Pack is Standard.
1158 if Pack /= Standard_Standard then
1160 if Opnd_Type = Universal_Integer then
1161 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1163 elsif Opnd_Type = Universal_Real then
1164 Orig_Type := Type_In_P (Is_Real_Type'Access);
1166 elsif Opnd_Type = Any_String then
1167 Orig_Type := Type_In_P (Is_String_Type'Access);
1169 elsif Opnd_Type = Any_Access then
1170 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1172 elsif Opnd_Type = Any_Composite then
1173 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1175 if Present (Orig_Type) then
1176 if Has_Private_Component (Orig_Type) then
1177 Orig_Type := Empty;
1178 else
1179 Set_Etype (Act1, Orig_Type);
1181 if Is_Binary then
1182 Set_Etype (Act2, Orig_Type);
1183 end if;
1184 end if;
1185 end if;
1187 else
1188 Orig_Type := Empty;
1189 end if;
1191 Error := No (Orig_Type);
1192 end if;
1194 elsif Ekind (Opnd_Type) = E_Allocator_Type
1195 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1196 then
1197 Error := True;
1199 -- If the type is defined elsewhere, and the operator is not
1200 -- defined in the given scope (by a renaming declaration, e.g.)
1201 -- then this is an error as well. If an extension of System is
1202 -- present, and the type may be defined there, Pack must be
1203 -- System itself.
1205 elsif Scope (Opnd_Type) /= Pack
1206 and then Scope (Op_Id) /= Pack
1207 and then (No (System_Aux_Id)
1208 or else Scope (Opnd_Type) /= System_Aux_Id
1209 or else Pack /= Scope (System_Aux_Id))
1210 then
1211 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1212 Error := True;
1213 else
1214 Error := not Operand_Type_In_Scope (Pack);
1215 end if;
1217 elsif Pack = Standard_Standard
1218 and then not Operand_Type_In_Scope (Standard_Standard)
1219 then
1220 Error := True;
1221 end if;
1222 end if;
1224 if Error then
1225 Error_Msg_Node_2 := Pack;
1226 Error_Msg_NE
1227 ("& not declared in&", N, Selector_Name (Name (N)));
1228 Set_Etype (N, Any_Type);
1229 return;
1230 end if;
1231 end if;
1233 Set_Chars (Op_Node, Op_Name);
1235 if not Is_Private_Type (Etype (N)) then
1236 Set_Etype (Op_Node, Base_Type (Etype (N)));
1237 else
1238 Set_Etype (Op_Node, Etype (N));
1239 end if;
1241 -- If this is a call to a function that renames a predefined equality,
1242 -- the renaming declaration provides a type that must be used to
1243 -- resolve the operands. This must be done now because resolution of
1244 -- the equality node will not resolve any remaining ambiguity, and it
1245 -- assumes that the first operand is not overloaded.
1247 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1248 and then Ekind (Func) = E_Function
1249 and then Is_Overloaded (Act1)
1250 then
1251 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1252 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1253 end if;
1255 Set_Entity (Op_Node, Op_Id);
1256 Generate_Reference (Op_Id, N, ' ');
1257 Rewrite (N, Op_Node);
1259 -- If this is an arithmetic operator and the result type is private,
1260 -- the operands and the result must be wrapped in conversion to
1261 -- expose the underlying numeric type and expand the proper checks,
1262 -- e.g. on division.
1264 if Is_Private_Type (Typ) then
1265 case Nkind (N) is
1266 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1267 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1268 Resolve_Intrinsic_Operator (N, Typ);
1270 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1271 Resolve_Intrinsic_Unary_Operator (N, Typ);
1273 when others =>
1274 Resolve (N, Typ);
1275 end case;
1276 else
1277 Resolve (N, Typ);
1278 end if;
1280 -- For predefined operators on literals, the operation freezes
1281 -- their type.
1283 if Present (Orig_Type) then
1284 Set_Etype (Act1, Orig_Type);
1285 Freeze_Expression (Act1);
1286 end if;
1287 end Make_Call_Into_Operator;
1289 -------------------
1290 -- Operator_Kind --
1291 -------------------
1293 function Operator_Kind
1294 (Op_Name : Name_Id;
1295 Is_Binary : Boolean) return Node_Kind
1297 Kind : Node_Kind;
1299 begin
1300 if Is_Binary then
1301 if Op_Name = Name_Op_And then Kind := N_Op_And;
1302 elsif Op_Name = Name_Op_Or then Kind := N_Op_Or;
1303 elsif Op_Name = Name_Op_Xor then Kind := N_Op_Xor;
1304 elsif Op_Name = Name_Op_Eq then Kind := N_Op_Eq;
1305 elsif Op_Name = Name_Op_Ne then Kind := N_Op_Ne;
1306 elsif Op_Name = Name_Op_Lt then Kind := N_Op_Lt;
1307 elsif Op_Name = Name_Op_Le then Kind := N_Op_Le;
1308 elsif Op_Name = Name_Op_Gt then Kind := N_Op_Gt;
1309 elsif Op_Name = Name_Op_Ge then Kind := N_Op_Ge;
1310 elsif Op_Name = Name_Op_Add then Kind := N_Op_Add;
1311 elsif Op_Name = Name_Op_Subtract then Kind := N_Op_Subtract;
1312 elsif Op_Name = Name_Op_Concat then Kind := N_Op_Concat;
1313 elsif Op_Name = Name_Op_Multiply then Kind := N_Op_Multiply;
1314 elsif Op_Name = Name_Op_Divide then Kind := N_Op_Divide;
1315 elsif Op_Name = Name_Op_Mod then Kind := N_Op_Mod;
1316 elsif Op_Name = Name_Op_Rem then Kind := N_Op_Rem;
1317 elsif Op_Name = Name_Op_Expon then Kind := N_Op_Expon;
1318 else
1319 raise Program_Error;
1320 end if;
1322 -- Unary operators
1324 else
1325 if Op_Name = Name_Op_Add then Kind := N_Op_Plus;
1326 elsif Op_Name = Name_Op_Subtract then Kind := N_Op_Minus;
1327 elsif Op_Name = Name_Op_Abs then Kind := N_Op_Abs;
1328 elsif Op_Name = Name_Op_Not then Kind := N_Op_Not;
1329 else
1330 raise Program_Error;
1331 end if;
1332 end if;
1334 return Kind;
1335 end Operator_Kind;
1337 -----------------------------
1338 -- Pre_Analyze_And_Resolve --
1339 -----------------------------
1341 procedure Pre_Analyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1342 Save_Full_Analysis : constant Boolean := Full_Analysis;
1344 begin
1345 Full_Analysis := False;
1346 Expander_Mode_Save_And_Set (False);
1348 -- We suppress all checks for this analysis, since the checks will
1349 -- be applied properly, and in the right location, when the default
1350 -- expression is reanalyzed and reexpanded later on.
1352 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1354 Expander_Mode_Restore;
1355 Full_Analysis := Save_Full_Analysis;
1356 end Pre_Analyze_And_Resolve;
1358 -- Version without context type
1360 procedure Pre_Analyze_And_Resolve (N : Node_Id) is
1361 Save_Full_Analysis : constant Boolean := Full_Analysis;
1363 begin
1364 Full_Analysis := False;
1365 Expander_Mode_Save_And_Set (False);
1367 Analyze (N);
1368 Resolve (N, Etype (N), Suppress => All_Checks);
1370 Expander_Mode_Restore;
1371 Full_Analysis := Save_Full_Analysis;
1372 end Pre_Analyze_And_Resolve;
1374 ----------------------------------
1375 -- Replace_Actual_Discriminants --
1376 ----------------------------------
1378 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1379 Loc : constant Source_Ptr := Sloc (N);
1380 Tsk : Node_Id := Empty;
1382 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1384 -------------------
1385 -- Process_Discr --
1386 -------------------
1388 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1389 Ent : Entity_Id;
1391 begin
1392 if Nkind (Nod) = N_Identifier then
1393 Ent := Entity (Nod);
1395 if Present (Ent)
1396 and then Ekind (Ent) = E_Discriminant
1397 then
1398 Rewrite (Nod,
1399 Make_Selected_Component (Loc,
1400 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1401 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1403 Set_Etype (Nod, Etype (Ent));
1404 end if;
1406 end if;
1408 return OK;
1409 end Process_Discr;
1411 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1413 -- Start of processing for Replace_Actual_Discriminants
1415 begin
1416 if not Expander_Active then
1417 return;
1418 end if;
1420 if Nkind (Name (N)) = N_Selected_Component then
1421 Tsk := Prefix (Name (N));
1423 elsif Nkind (Name (N)) = N_Indexed_Component then
1424 Tsk := Prefix (Prefix (Name (N)));
1425 end if;
1427 if No (Tsk) then
1428 return;
1429 else
1430 Replace_Discrs (Default);
1431 end if;
1432 end Replace_Actual_Discriminants;
1434 -------------
1435 -- Resolve --
1436 -------------
1438 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1439 I : Interp_Index;
1440 I1 : Interp_Index := 0; -- prevent junk warning
1441 It : Interp;
1442 It1 : Interp;
1443 Found : Boolean := False;
1444 Seen : Entity_Id := Empty; -- prevent junk warning
1445 Ctx_Type : Entity_Id := Typ;
1446 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1447 Err_Type : Entity_Id := Empty;
1448 Ambiguous : Boolean := False;
1450 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1451 -- Try and fix up a literal so that it matches its expected type. New
1452 -- literals are manufactured if necessary to avoid cascaded errors.
1454 procedure Resolution_Failed;
1455 -- Called when attempt at resolving current expression fails
1457 --------------------
1458 -- Patch_Up_Value --
1459 --------------------
1461 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1462 begin
1463 if Nkind (N) = N_Integer_Literal
1464 and then Is_Real_Type (Typ)
1465 then
1466 Rewrite (N,
1467 Make_Real_Literal (Sloc (N),
1468 Realval => UR_From_Uint (Intval (N))));
1469 Set_Etype (N, Universal_Real);
1470 Set_Is_Static_Expression (N);
1472 elsif Nkind (N) = N_Real_Literal
1473 and then Is_Integer_Type (Typ)
1474 then
1475 Rewrite (N,
1476 Make_Integer_Literal (Sloc (N),
1477 Intval => UR_To_Uint (Realval (N))));
1478 Set_Etype (N, Universal_Integer);
1479 Set_Is_Static_Expression (N);
1480 elsif Nkind (N) = N_String_Literal
1481 and then Is_Character_Type (Typ)
1482 then
1483 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1484 Rewrite (N,
1485 Make_Character_Literal (Sloc (N),
1486 Chars => Name_Find,
1487 Char_Literal_Value =>
1488 UI_From_Int (Character'Pos ('A'))));
1489 Set_Etype (N, Any_Character);
1490 Set_Is_Static_Expression (N);
1492 elsif Nkind (N) /= N_String_Literal
1493 and then Is_String_Type (Typ)
1494 then
1495 Rewrite (N,
1496 Make_String_Literal (Sloc (N),
1497 Strval => End_String));
1499 elsif Nkind (N) = N_Range then
1500 Patch_Up_Value (Low_Bound (N), Typ);
1501 Patch_Up_Value (High_Bound (N), Typ);
1502 end if;
1503 end Patch_Up_Value;
1505 -----------------------
1506 -- Resolution_Failed --
1507 -----------------------
1509 procedure Resolution_Failed is
1510 begin
1511 Patch_Up_Value (N, Typ);
1512 Set_Etype (N, Typ);
1513 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1514 Set_Is_Overloaded (N, False);
1516 -- The caller will return without calling the expander, so we need
1517 -- to set the analyzed flag. Note that it is fine to set Analyzed
1518 -- to True even if we are in the middle of a shallow analysis,
1519 -- (see the spec of sem for more details) since this is an error
1520 -- situation anyway, and there is no point in repeating the
1521 -- analysis later (indeed it won't work to repeat it later, since
1522 -- we haven't got a clear resolution of which entity is being
1523 -- referenced.)
1525 Set_Analyzed (N, True);
1526 return;
1527 end Resolution_Failed;
1529 -- Start of processing for Resolve
1531 begin
1532 if N = Error then
1533 return;
1534 end if;
1536 -- Access attribute on remote subprogram cannot be used for
1537 -- a non-remote access-to-subprogram type.
1539 if Nkind (N) = N_Attribute_Reference
1540 and then (Attribute_Name (N) = Name_Access
1541 or else Attribute_Name (N) = Name_Unrestricted_Access
1542 or else Attribute_Name (N) = Name_Unchecked_Access)
1543 and then Comes_From_Source (N)
1544 and then Is_Entity_Name (Prefix (N))
1545 and then Is_Subprogram (Entity (Prefix (N)))
1546 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1547 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1548 then
1549 Error_Msg_N
1550 ("prefix must statically denote a non-remote subprogram", N);
1551 end if;
1553 -- If the context is a Remote_Access_To_Subprogram, access attributes
1554 -- must be resolved with the corresponding fat pointer. There is no need
1555 -- to check for the attribute name since the return type of an
1556 -- attribute is never a remote type.
1558 if Nkind (N) = N_Attribute_Reference
1559 and then Comes_From_Source (N)
1560 and then (Is_Remote_Call_Interface (Typ)
1561 or else Is_Remote_Types (Typ))
1562 then
1563 declare
1564 Attr : constant Attribute_Id :=
1565 Get_Attribute_Id (Attribute_Name (N));
1566 Pref : constant Node_Id := Prefix (N);
1567 Decl : Node_Id;
1568 Spec : Node_Id;
1569 Is_Remote : Boolean := True;
1571 begin
1572 -- Check that Typ is a remote access-to-subprogram type
1574 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1575 -- Prefix (N) must statically denote a remote subprogram
1576 -- declared in a package specification.
1578 if Attr = Attribute_Access then
1579 Decl := Unit_Declaration_Node (Entity (Pref));
1581 if Nkind (Decl) = N_Subprogram_Body then
1582 Spec := Corresponding_Spec (Decl);
1584 if not No (Spec) then
1585 Decl := Unit_Declaration_Node (Spec);
1586 end if;
1587 end if;
1589 Spec := Parent (Decl);
1591 if not Is_Entity_Name (Prefix (N))
1592 or else Nkind (Spec) /= N_Package_Specification
1593 or else
1594 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1595 then
1596 Is_Remote := False;
1597 Error_Msg_N
1598 ("prefix must statically denote a remote subprogram ",
1600 end if;
1601 end if;
1603 -- If we are generating code for a distributed program.
1604 -- perform semantic checks against the corresponding
1605 -- remote entities.
1607 if (Attr = Attribute_Access
1608 or else Attr = Attribute_Unchecked_Access
1609 or else Attr = Attribute_Unrestricted_Access)
1610 and then Expander_Active
1611 and then Get_PCS_Name /= Name_No_DSA
1612 then
1613 Check_Subtype_Conformant
1614 (New_Id => Entity (Prefix (N)),
1615 Old_Id => Designated_Type
1616 (Corresponding_Remote_Type (Typ)),
1617 Err_Loc => N);
1618 if Is_Remote then
1619 Process_Remote_AST_Attribute (N, Typ);
1620 end if;
1621 end if;
1622 end if;
1623 end;
1624 end if;
1626 Debug_A_Entry ("resolving ", N);
1628 if Comes_From_Source (N) then
1629 if Is_Fixed_Point_Type (Typ) then
1630 Check_Restriction (No_Fixed_Point, N);
1632 elsif Is_Floating_Point_Type (Typ)
1633 and then Typ /= Universal_Real
1634 and then Typ /= Any_Real
1635 then
1636 Check_Restriction (No_Floating_Point, N);
1637 end if;
1638 end if;
1640 -- Return if already analyzed
1642 if Analyzed (N) then
1643 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
1644 return;
1646 -- Return if type = Any_Type (previous error encountered)
1648 elsif Etype (N) = Any_Type then
1649 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
1650 return;
1651 end if;
1653 Check_Parameterless_Call (N);
1655 -- If not overloaded, then we know the type, and all that needs doing
1656 -- is to check that this type is compatible with the context.
1658 if not Is_Overloaded (N) then
1659 Found := Covers (Typ, Etype (N));
1660 Expr_Type := Etype (N);
1662 -- In the overloaded case, we must select the interpretation that
1663 -- is compatible with the context (i.e. the type passed to Resolve)
1665 else
1666 Get_First_Interp (N, I, It);
1668 -- Loop through possible interpretations
1670 Interp_Loop : while Present (It.Typ) loop
1672 -- We are only interested in interpretations that are compatible
1673 -- with the expected type, any other interpretations are ignored
1675 if not Covers (Typ, It.Typ) then
1676 if Debug_Flag_V then
1677 Write_Str (" interpretation incompatible with context");
1678 Write_Eol;
1679 end if;
1681 else
1682 -- First matching interpretation
1684 if not Found then
1685 Found := True;
1686 I1 := I;
1687 Seen := It.Nam;
1688 Expr_Type := It.Typ;
1690 -- Matching interpretation that is not the first, maybe an
1691 -- error, but there are some cases where preference rules are
1692 -- used to choose between the two possibilities. These and
1693 -- some more obscure cases are handled in Disambiguate.
1695 else
1696 Error_Msg_Sloc := Sloc (Seen);
1697 It1 := Disambiguate (N, I1, I, Typ);
1699 -- Disambiguation has succeeded. Skip the remaining
1700 -- interpretations.
1702 if It1 /= No_Interp then
1703 Seen := It1.Nam;
1704 Expr_Type := It1.Typ;
1706 while Present (It.Typ) loop
1707 Get_Next_Interp (I, It);
1708 end loop;
1710 else
1711 -- Before we issue an ambiguity complaint, check for
1712 -- the case of a subprogram call where at least one
1713 -- of the arguments is Any_Type, and if so, suppress
1714 -- the message, since it is a cascaded error.
1716 if Nkind (N) = N_Function_Call
1717 or else Nkind (N) = N_Procedure_Call_Statement
1718 then
1719 declare
1720 A : Node_Id := First_Actual (N);
1721 E : Node_Id;
1723 begin
1724 while Present (A) loop
1725 E := A;
1727 if Nkind (E) = N_Parameter_Association then
1728 E := Explicit_Actual_Parameter (E);
1729 end if;
1731 if Etype (E) = Any_Type then
1732 if Debug_Flag_V then
1733 Write_Str ("Any_Type in call");
1734 Write_Eol;
1735 end if;
1737 exit Interp_Loop;
1738 end if;
1740 Next_Actual (A);
1741 end loop;
1742 end;
1744 elsif Nkind (N) in N_Binary_Op
1745 and then (Etype (Left_Opnd (N)) = Any_Type
1746 or else Etype (Right_Opnd (N)) = Any_Type)
1747 then
1748 exit Interp_Loop;
1750 elsif Nkind (N) in N_Unary_Op
1751 and then Etype (Right_Opnd (N)) = Any_Type
1752 then
1753 exit Interp_Loop;
1754 end if;
1756 -- Not that special case, so issue message using the
1757 -- flag Ambiguous to control printing of the header
1758 -- message only at the start of an ambiguous set.
1760 if not Ambiguous then
1761 Error_Msg_NE
1762 ("ambiguous expression (cannot resolve&)!",
1763 N, It.Nam);
1765 Error_Msg_N
1766 ("possible interpretation#!", N);
1767 Ambiguous := True;
1768 end if;
1770 Error_Msg_Sloc := Sloc (It.Nam);
1772 -- By default, the error message refers to the candidate
1773 -- interpretation. But if it is a predefined operator,
1774 -- it is implicitly declared at the declaration of
1775 -- the type of the operand. Recover the sloc of that
1776 -- declaration for the error message.
1778 if Nkind (N) in N_Op
1779 and then Scope (It.Nam) = Standard_Standard
1780 and then not Is_Overloaded (Right_Opnd (N))
1781 and then Scope (Base_Type (Etype (Right_Opnd (N))))
1782 /= Standard_Standard
1783 then
1784 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
1786 if Comes_From_Source (Err_Type)
1787 and then Present (Parent (Err_Type))
1788 then
1789 Error_Msg_Sloc := Sloc (Parent (Err_Type));
1790 end if;
1792 elsif Nkind (N) in N_Binary_Op
1793 and then Scope (It.Nam) = Standard_Standard
1794 and then not Is_Overloaded (Left_Opnd (N))
1795 and then Scope (Base_Type (Etype (Left_Opnd (N))))
1796 /= Standard_Standard
1797 then
1798 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
1800 if Comes_From_Source (Err_Type)
1801 and then Present (Parent (Err_Type))
1802 then
1803 Error_Msg_Sloc := Sloc (Parent (Err_Type));
1804 end if;
1805 else
1806 Err_Type := Empty;
1807 end if;
1809 if Nkind (N) in N_Op
1810 and then Scope (It.Nam) = Standard_Standard
1811 and then Present (Err_Type)
1812 then
1813 Error_Msg_N
1814 ("possible interpretation (predefined)#!", N);
1815 else
1816 Error_Msg_N ("possible interpretation#!", N);
1817 end if;
1819 end if;
1820 end if;
1822 -- We have a matching interpretation, Expr_Type is the
1823 -- type from this interpretation, and Seen is the entity.
1825 -- For an operator, just set the entity name. The type will
1826 -- be set by the specific operator resolution routine.
1828 if Nkind (N) in N_Op then
1829 Set_Entity (N, Seen);
1830 Generate_Reference (Seen, N);
1832 elsif Nkind (N) = N_Character_Literal then
1833 Set_Etype (N, Expr_Type);
1835 -- For an explicit dereference, attribute reference, range,
1836 -- short-circuit form (which is not an operator node),
1837 -- or a call with a name that is an explicit dereference,
1838 -- there is nothing to be done at this point.
1840 elsif Nkind (N) = N_Explicit_Dereference
1841 or else Nkind (N) = N_Attribute_Reference
1842 or else Nkind (N) = N_And_Then
1843 or else Nkind (N) = N_Indexed_Component
1844 or else Nkind (N) = N_Or_Else
1845 or else Nkind (N) = N_Range
1846 or else Nkind (N) = N_Selected_Component
1847 or else Nkind (N) = N_Slice
1848 or else Nkind (Name (N)) = N_Explicit_Dereference
1849 then
1850 null;
1852 -- For procedure or function calls, set the type of the
1853 -- name, and also the entity pointer for the prefix
1855 elsif (Nkind (N) = N_Procedure_Call_Statement
1856 or else Nkind (N) = N_Function_Call)
1857 and then (Is_Entity_Name (Name (N))
1858 or else Nkind (Name (N)) = N_Operator_Symbol)
1859 then
1860 Set_Etype (Name (N), Expr_Type);
1861 Set_Entity (Name (N), Seen);
1862 Generate_Reference (Seen, Name (N));
1864 elsif Nkind (N) = N_Function_Call
1865 and then Nkind (Name (N)) = N_Selected_Component
1866 then
1867 Set_Etype (Name (N), Expr_Type);
1868 Set_Entity (Selector_Name (Name (N)), Seen);
1869 Generate_Reference (Seen, Selector_Name (Name (N)));
1871 -- For all other cases, just set the type of the Name
1873 else
1874 Set_Etype (Name (N), Expr_Type);
1875 end if;
1877 end if;
1879 -- Move to next interpretation
1881 exit Interp_Loop when not Present (It.Typ);
1883 Get_Next_Interp (I, It);
1884 end loop Interp_Loop;
1885 end if;
1887 -- At this stage Found indicates whether or not an acceptable
1888 -- interpretation exists. If not, then we have an error, except
1889 -- that if the context is Any_Type as a result of some other error,
1890 -- then we suppress the error report.
1892 if not Found then
1893 if Typ /= Any_Type then
1895 -- If type we are looking for is Void, then this is the
1896 -- procedure call case, and the error is simply that what
1897 -- we gave is not a procedure name (we think of procedure
1898 -- calls as expressions with types internally, but the user
1899 -- doesn't think of them this way!)
1901 if Typ = Standard_Void_Type then
1903 -- Special case message if function used as a procedure
1905 if Nkind (N) = N_Procedure_Call_Statement
1906 and then Is_Entity_Name (Name (N))
1907 and then Ekind (Entity (Name (N))) = E_Function
1908 then
1909 Error_Msg_NE
1910 ("cannot use function & in a procedure call",
1911 Name (N), Entity (Name (N)));
1913 -- Otherwise give general message (not clear what cases
1914 -- this covers, but no harm in providing for them!)
1916 else
1917 Error_Msg_N ("expect procedure name in procedure call", N);
1918 end if;
1920 Found := True;
1922 -- Otherwise we do have a subexpression with the wrong type
1924 -- Check for the case of an allocator which uses an access
1925 -- type instead of the designated type. This is a common
1926 -- error and we specialize the message, posting an error
1927 -- on the operand of the allocator, complaining that we
1928 -- expected the designated type of the allocator.
1930 elsif Nkind (N) = N_Allocator
1931 and then Ekind (Typ) in Access_Kind
1932 and then Ekind (Etype (N)) in Access_Kind
1933 and then Designated_Type (Etype (N)) = Typ
1934 then
1935 Wrong_Type (Expression (N), Designated_Type (Typ));
1936 Found := True;
1938 -- Check for view mismatch on Null in instances, for
1939 -- which the view-swapping mechanism has no identifier.
1941 elsif (In_Instance or else In_Inlined_Body)
1942 and then (Nkind (N) = N_Null)
1943 and then Is_Private_Type (Typ)
1944 and then Is_Access_Type (Full_View (Typ))
1945 then
1946 Resolve (N, Full_View (Typ));
1947 Set_Etype (N, Typ);
1948 return;
1950 -- Check for an aggregate. Sometimes we can get bogus
1951 -- aggregates from misuse of parentheses, and we are
1952 -- about to complain about the aggregate without even
1953 -- looking inside it.
1955 -- Instead, if we have an aggregate of type Any_Composite,
1956 -- then analyze and resolve the component fields, and then
1957 -- only issue another message if we get no errors doing
1958 -- this (otherwise assume that the errors in the aggregate
1959 -- caused the problem).
1961 elsif Nkind (N) = N_Aggregate
1962 and then Etype (N) = Any_Composite
1963 then
1964 -- Disable expansion in any case. If there is a type mismatch
1965 -- it may be fatal to try to expand the aggregate. The flag
1966 -- would otherwise be set to false when the error is posted.
1968 Expander_Active := False;
1970 declare
1971 procedure Check_Aggr (Aggr : Node_Id);
1972 -- Check one aggregate, and set Found to True if we
1973 -- have a definite error in any of its elements
1975 procedure Check_Elmt (Aelmt : Node_Id);
1976 -- Check one element of aggregate and set Found to
1977 -- True if we definitely have an error in the element.
1979 procedure Check_Aggr (Aggr : Node_Id) is
1980 Elmt : Node_Id;
1982 begin
1983 if Present (Expressions (Aggr)) then
1984 Elmt := First (Expressions (Aggr));
1985 while Present (Elmt) loop
1986 Check_Elmt (Elmt);
1987 Next (Elmt);
1988 end loop;
1989 end if;
1991 if Present (Component_Associations (Aggr)) then
1992 Elmt := First (Component_Associations (Aggr));
1993 while Present (Elmt) loop
1994 Check_Elmt (Expression (Elmt));
1995 Next (Elmt);
1996 end loop;
1997 end if;
1998 end Check_Aggr;
2000 ----------------
2001 -- Check_Elmt --
2002 ----------------
2004 procedure Check_Elmt (Aelmt : Node_Id) is
2005 begin
2006 -- If we have a nested aggregate, go inside it (to
2007 -- attempt a naked analyze-resolve of the aggregate
2008 -- can cause undesirable cascaded errors). Do not
2009 -- resolve expression if it needs a type from context,
2010 -- as for integer * fixed expression.
2012 if Nkind (Aelmt) = N_Aggregate then
2013 Check_Aggr (Aelmt);
2015 else
2016 Analyze (Aelmt);
2018 if not Is_Overloaded (Aelmt)
2019 and then Etype (Aelmt) /= Any_Fixed
2020 then
2021 Resolve (Aelmt);
2022 end if;
2024 if Etype (Aelmt) = Any_Type then
2025 Found := True;
2026 end if;
2027 end if;
2028 end Check_Elmt;
2030 begin
2031 Check_Aggr (N);
2032 end;
2033 end if;
2035 -- If an error message was issued already, Found got reset
2036 -- to True, so if it is still False, issue the standard
2037 -- Wrong_Type message.
2039 if not Found then
2040 if Is_Overloaded (N)
2041 and then Nkind (N) = N_Function_Call
2042 then
2043 declare
2044 Subp_Name : Node_Id;
2045 begin
2046 if Is_Entity_Name (Name (N)) then
2047 Subp_Name := Name (N);
2049 elsif Nkind (Name (N)) = N_Selected_Component then
2051 -- Protected operation: retrieve operation name
2053 Subp_Name := Selector_Name (Name (N));
2054 else
2055 raise Program_Error;
2056 end if;
2058 Error_Msg_Node_2 := Typ;
2059 Error_Msg_NE ("no visible interpretation of&" &
2060 " matches expected type&", N, Subp_Name);
2061 end;
2063 if All_Errors_Mode then
2064 declare
2065 Index : Interp_Index;
2066 It : Interp;
2068 begin
2069 Error_Msg_N ("\possible interpretations:", N);
2070 Get_First_Interp (Name (N), Index, It);
2072 while Present (It.Nam) loop
2074 Error_Msg_Sloc := Sloc (It.Nam);
2075 Error_Msg_Node_2 := It.Typ;
2076 Error_Msg_NE ("\& declared#, type&",
2077 N, It.Nam);
2079 Get_Next_Interp (Index, It);
2080 end loop;
2081 end;
2082 else
2083 Error_Msg_N ("\use -gnatf for details", N);
2084 end if;
2085 else
2086 Wrong_Type (N, Typ);
2087 end if;
2088 end if;
2089 end if;
2091 Resolution_Failed;
2092 return;
2094 -- Test if we have more than one interpretation for the context
2096 elsif Ambiguous then
2097 Resolution_Failed;
2098 return;
2100 -- Here we have an acceptable interpretation for the context
2102 else
2103 -- Propagate type information and normalize tree for various
2104 -- predefined operations. If the context only imposes a class of
2105 -- types, rather than a specific type, propagate the actual type
2106 -- downward.
2108 if Typ = Any_Integer
2109 or else Typ = Any_Boolean
2110 or else Typ = Any_Modular
2111 or else Typ = Any_Real
2112 or else Typ = Any_Discrete
2113 then
2114 Ctx_Type := Expr_Type;
2116 -- Any_Fixed is legal in a real context only if a specific
2117 -- fixed point type is imposed. If Norman Cohen can be
2118 -- confused by this, it deserves a separate message.
2120 if Typ = Any_Real
2121 and then Expr_Type = Any_Fixed
2122 then
2123 Error_Msg_N ("Illegal context for mixed mode operation", N);
2124 Set_Etype (N, Universal_Real);
2125 Ctx_Type := Universal_Real;
2126 end if;
2127 end if;
2129 -- A user-defined operator is tranformed into a function call at
2130 -- this point, so that further processing knows that operators are
2131 -- really operators (i.e. are predefined operators). User-defined
2132 -- operators that are intrinsic are just renamings of the predefined
2133 -- ones, and need not be turned into calls either, but if they rename
2134 -- a different operator, we must transform the node accordingly.
2135 -- Instantiations of Unchecked_Conversion are intrinsic but are
2136 -- treated as functions, even if given an operator designator.
2138 if Nkind (N) in N_Op
2139 and then Present (Entity (N))
2140 and then Ekind (Entity (N)) /= E_Operator
2141 then
2143 if not Is_Predefined_Op (Entity (N)) then
2144 Rewrite_Operator_As_Call (N, Entity (N));
2146 elsif Present (Alias (Entity (N)))
2147 and then
2148 Nkind (Parent (Parent (Entity (N))))
2149 = N_Subprogram_Renaming_Declaration
2150 then
2151 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2153 -- If the node is rewritten, it will be fully resolved in
2154 -- Rewrite_Renamed_Operator.
2156 if Analyzed (N) then
2157 return;
2158 end if;
2159 end if;
2160 end if;
2162 case N_Subexpr'(Nkind (N)) is
2164 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2166 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2168 when N_And_Then | N_Or_Else
2169 => Resolve_Short_Circuit (N, Ctx_Type);
2171 when N_Attribute_Reference
2172 => Resolve_Attribute (N, Ctx_Type);
2174 when N_Character_Literal
2175 => Resolve_Character_Literal (N, Ctx_Type);
2177 when N_Conditional_Expression
2178 => Resolve_Conditional_Expression (N, Ctx_Type);
2180 when N_Expanded_Name
2181 => Resolve_Entity_Name (N, Ctx_Type);
2183 when N_Extension_Aggregate
2184 => Resolve_Extension_Aggregate (N, Ctx_Type);
2186 when N_Explicit_Dereference
2187 => Resolve_Explicit_Dereference (N, Ctx_Type);
2189 when N_Function_Call
2190 => Resolve_Call (N, Ctx_Type);
2192 when N_Identifier
2193 => Resolve_Entity_Name (N, Ctx_Type);
2195 when N_In | N_Not_In
2196 => Resolve_Membership_Op (N, Ctx_Type);
2198 when N_Indexed_Component
2199 => Resolve_Indexed_Component (N, Ctx_Type);
2201 when N_Integer_Literal
2202 => Resolve_Integer_Literal (N, Ctx_Type);
2204 when N_Null => Resolve_Null (N, Ctx_Type);
2206 when N_Op_And | N_Op_Or | N_Op_Xor
2207 => Resolve_Logical_Op (N, Ctx_Type);
2209 when N_Op_Eq | N_Op_Ne
2210 => Resolve_Equality_Op (N, Ctx_Type);
2212 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2213 => Resolve_Comparison_Op (N, Ctx_Type);
2215 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2217 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2218 N_Op_Divide | N_Op_Mod | N_Op_Rem
2220 => Resolve_Arithmetic_Op (N, Ctx_Type);
2222 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2224 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2226 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2227 => Resolve_Unary_Op (N, Ctx_Type);
2229 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2231 when N_Procedure_Call_Statement
2232 => Resolve_Call (N, Ctx_Type);
2234 when N_Operator_Symbol
2235 => Resolve_Operator_Symbol (N, Ctx_Type);
2237 when N_Qualified_Expression
2238 => Resolve_Qualified_Expression (N, Ctx_Type);
2240 when N_Raise_xxx_Error
2241 => Set_Etype (N, Ctx_Type);
2243 when N_Range => Resolve_Range (N, Ctx_Type);
2245 when N_Real_Literal
2246 => Resolve_Real_Literal (N, Ctx_Type);
2248 when N_Reference => Resolve_Reference (N, Ctx_Type);
2250 when N_Selected_Component
2251 => Resolve_Selected_Component (N, Ctx_Type);
2253 when N_Slice => Resolve_Slice (N, Ctx_Type);
2255 when N_String_Literal
2256 => Resolve_String_Literal (N, Ctx_Type);
2258 when N_Subprogram_Info
2259 => Resolve_Subprogram_Info (N, Ctx_Type);
2261 when N_Type_Conversion
2262 => Resolve_Type_Conversion (N, Ctx_Type);
2264 when N_Unchecked_Expression =>
2265 Resolve_Unchecked_Expression (N, Ctx_Type);
2267 when N_Unchecked_Type_Conversion =>
2268 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2270 end case;
2272 -- If the subexpression was replaced by a non-subexpression, then
2273 -- all we do is to expand it. The only legitimate case we know of
2274 -- is converting procedure call statement to entry call statements,
2275 -- but there may be others, so we are making this test general.
2277 if Nkind (N) not in N_Subexpr then
2278 Debug_A_Exit ("resolving ", N, " (done)");
2279 Expand (N);
2280 return;
2281 end if;
2283 -- The expression is definitely NOT overloaded at this point, so
2284 -- we reset the Is_Overloaded flag to avoid any confusion when
2285 -- reanalyzing the node.
2287 Set_Is_Overloaded (N, False);
2289 -- Freeze expression type, entity if it is a name, and designated
2290 -- type if it is an allocator (RM 13.14(10,11,13)).
2292 -- Now that the resolution of the type of the node is complete,
2293 -- and we did not detect an error, we can expand this node. We
2294 -- skip the expand call if we are in a default expression, see
2295 -- section "Handling of Default Expressions" in Sem spec.
2297 Debug_A_Exit ("resolving ", N, " (done)");
2299 -- We unconditionally freeze the expression, even if we are in
2300 -- default expression mode (the Freeze_Expression routine tests
2301 -- this flag and only freezes static types if it is set).
2303 Freeze_Expression (N);
2305 -- Now we can do the expansion
2307 Expand (N);
2308 end if;
2309 end Resolve;
2311 -------------
2312 -- Resolve --
2313 -------------
2315 -- Version with check(s) suppressed
2317 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2318 begin
2319 if Suppress = All_Checks then
2320 declare
2321 Svg : constant Suppress_Array := Scope_Suppress;
2323 begin
2324 Scope_Suppress := (others => True);
2325 Resolve (N, Typ);
2326 Scope_Suppress := Svg;
2327 end;
2329 else
2330 declare
2331 Svg : constant Boolean := Scope_Suppress (Suppress);
2333 begin
2334 Scope_Suppress (Suppress) := True;
2335 Resolve (N, Typ);
2336 Scope_Suppress (Suppress) := Svg;
2337 end;
2338 end if;
2339 end Resolve;
2341 -------------
2342 -- Resolve --
2343 -------------
2345 -- Version with implicit type
2347 procedure Resolve (N : Node_Id) is
2348 begin
2349 Resolve (N, Etype (N));
2350 end Resolve;
2352 ---------------------
2353 -- Resolve_Actuals --
2354 ---------------------
2356 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2357 Loc : constant Source_Ptr := Sloc (N);
2358 A : Node_Id;
2359 F : Entity_Id;
2360 A_Typ : Entity_Id;
2361 F_Typ : Entity_Id;
2362 Prev : Node_Id := Empty;
2364 procedure Insert_Default;
2365 -- If the actual is missing in a call, insert in the actuals list
2366 -- an instance of the default expression. The insertion is always
2367 -- a named association.
2369 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2370 -- Check whether T1 and T2, or their full views, are derived from a
2371 -- common type. Used to enforce the restrictions on array conversions
2372 -- of AI95-00246.
2374 --------------------
2375 -- Insert_Default --
2376 --------------------
2378 procedure Insert_Default is
2379 Actval : Node_Id;
2380 Assoc : Node_Id;
2382 begin
2383 -- Missing argument in call, nothing to insert
2385 if No (Default_Value (F)) then
2386 return;
2388 else
2389 -- Note that we do a full New_Copy_Tree, so that any associated
2390 -- Itypes are properly copied. This may not be needed any more,
2391 -- but it does no harm as a safety measure! Defaults of a generic
2392 -- formal may be out of bounds of the corresponding actual (see
2393 -- cc1311b) and an additional check may be required.
2395 Actval := New_Copy_Tree (Default_Value (F),
2396 New_Scope => Current_Scope, New_Sloc => Loc);
2398 if Is_Concurrent_Type (Scope (Nam))
2399 and then Has_Discriminants (Scope (Nam))
2400 then
2401 Replace_Actual_Discriminants (N, Actval);
2402 end if;
2404 if Is_Overloadable (Nam)
2405 and then Present (Alias (Nam))
2406 then
2407 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
2408 and then not Is_Tagged_Type (Etype (F))
2409 then
2410 -- If default is a real literal, do not introduce a
2411 -- conversion whose effect may depend on the run-time
2412 -- size of universal real.
2414 if Nkind (Actval) = N_Real_Literal then
2415 Set_Etype (Actval, Base_Type (Etype (F)));
2416 else
2417 Actval := Unchecked_Convert_To (Etype (F), Actval);
2418 end if;
2419 end if;
2421 if Is_Scalar_Type (Etype (F)) then
2422 Enable_Range_Check (Actval);
2423 end if;
2425 Set_Parent (Actval, N);
2427 -- Resolve aggregates with their base type, to avoid scope
2428 -- anomalies: the subtype was first built in the suprogram
2429 -- declaration, and the current call may be nested.
2431 if Nkind (Actval) = N_Aggregate
2432 and then Has_Discriminants (Etype (Actval))
2433 then
2434 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2435 else
2436 Analyze_And_Resolve (Actval, Etype (Actval));
2437 end if;
2439 else
2440 Set_Parent (Actval, N);
2442 -- See note above concerning aggregates
2444 if Nkind (Actval) = N_Aggregate
2445 and then Has_Discriminants (Etype (Actval))
2446 then
2447 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2449 -- Resolve entities with their own type, which may differ
2450 -- from the type of a reference in a generic context (the
2451 -- view swapping mechanism did not anticipate the re-analysis
2452 -- of default values in calls).
2454 elsif Is_Entity_Name (Actval) then
2455 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
2457 else
2458 Analyze_And_Resolve (Actval, Etype (Actval));
2459 end if;
2460 end if;
2462 -- If default is a tag indeterminate function call, propagate
2463 -- tag to obtain proper dispatching.
2465 if Is_Controlling_Formal (F)
2466 and then Nkind (Default_Value (F)) = N_Function_Call
2467 then
2468 Set_Is_Controlling_Actual (Actval);
2469 end if;
2471 end if;
2473 -- If the default expression raises constraint error, then just
2474 -- silently replace it with an N_Raise_Constraint_Error node,
2475 -- since we already gave the warning on the subprogram spec.
2477 if Raises_Constraint_Error (Actval) then
2478 Rewrite (Actval,
2479 Make_Raise_Constraint_Error (Loc,
2480 Reason => CE_Range_Check_Failed));
2481 Set_Raises_Constraint_Error (Actval);
2482 Set_Etype (Actval, Etype (F));
2483 end if;
2485 Assoc :=
2486 Make_Parameter_Association (Loc,
2487 Explicit_Actual_Parameter => Actval,
2488 Selector_Name => Make_Identifier (Loc, Chars (F)));
2490 -- Case of insertion is first named actual
2492 if No (Prev) or else
2493 Nkind (Parent (Prev)) /= N_Parameter_Association
2494 then
2495 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
2496 Set_First_Named_Actual (N, Actval);
2498 if No (Prev) then
2499 if not Present (Parameter_Associations (N)) then
2500 Set_Parameter_Associations (N, New_List (Assoc));
2501 else
2502 Append (Assoc, Parameter_Associations (N));
2503 end if;
2505 else
2506 Insert_After (Prev, Assoc);
2507 end if;
2509 -- Case of insertion is not first named actual
2511 else
2512 Set_Next_Named_Actual
2513 (Assoc, Next_Named_Actual (Parent (Prev)));
2514 Set_Next_Named_Actual (Parent (Prev), Actval);
2515 Append (Assoc, Parameter_Associations (N));
2516 end if;
2518 Mark_Rewrite_Insertion (Assoc);
2519 Mark_Rewrite_Insertion (Actval);
2521 Prev := Actval;
2522 end Insert_Default;
2524 -------------------
2525 -- Same_Ancestor --
2526 -------------------
2528 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
2529 FT1 : Entity_Id := T1;
2530 FT2 : Entity_Id := T2;
2532 begin
2533 if Is_Private_Type (T1)
2534 and then Present (Full_View (T1))
2535 then
2536 FT1 := Full_View (T1);
2537 end if;
2539 if Is_Private_Type (T2)
2540 and then Present (Full_View (T2))
2541 then
2542 FT2 := Full_View (T2);
2543 end if;
2545 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
2546 end Same_Ancestor;
2548 -- Start of processing for Resolve_Actuals
2550 begin
2551 A := First_Actual (N);
2552 F := First_Formal (Nam);
2553 while Present (F) loop
2554 if No (A) and then Needs_No_Actuals (Nam) then
2555 null;
2557 -- If we have an error in any actual or formal, indicated by
2558 -- a type of Any_Type, then abandon resolution attempt, and
2559 -- set result type to Any_Type.
2561 elsif (Present (A) and then Etype (A) = Any_Type)
2562 or else Etype (F) = Any_Type
2563 then
2564 Set_Etype (N, Any_Type);
2565 return;
2566 end if;
2568 if Present (A)
2569 and then (Nkind (Parent (A)) /= N_Parameter_Association
2570 or else
2571 Chars (Selector_Name (Parent (A))) = Chars (F))
2572 then
2573 -- If the formal is Out or In_Out, do not resolve and expand the
2574 -- conversion, because it is subsequently expanded into explicit
2575 -- temporaries and assignments. However, the object of the
2576 -- conversion can be resolved. An exception is the case of
2577 -- a tagged type conversion with a class-wide actual. In that
2578 -- case we want the tag check to occur and no temporary will
2579 -- will be needed (no representation change can occur) and
2580 -- the parameter is passed by reference, so we go ahead and
2581 -- resolve the type conversion.
2583 if Ekind (F) /= E_In_Parameter
2584 and then Nkind (A) = N_Type_Conversion
2585 and then not Is_Class_Wide_Type (Etype (Expression (A)))
2586 then
2587 if Ekind (F) = E_In_Out_Parameter
2588 and then Is_Array_Type (Etype (F))
2589 then
2590 if Has_Aliased_Components (Etype (Expression (A)))
2591 /= Has_Aliased_Components (Etype (F))
2592 then
2593 Error_Msg_N
2594 ("both component types in a view conversion must be"
2595 & " aliased, or neither", A);
2597 elsif not Same_Ancestor (Etype (F), Etype (Expression (A)))
2598 and then
2599 (Is_By_Reference_Type (Etype (F))
2600 or else Is_By_Reference_Type (Etype (Expression (A))))
2601 then
2602 Error_Msg_N
2603 ("view conversion between unrelated by_reference "
2604 & "array types not allowed (\A\I-00246)?", A);
2605 end if;
2606 end if;
2608 if Conversion_OK (A)
2609 or else Valid_Conversion (A, Etype (A), Expression (A))
2610 then
2611 Resolve (Expression (A));
2612 end if;
2614 else
2615 if Nkind (A) = N_Type_Conversion
2616 and then Is_Array_Type (Etype (F))
2617 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
2618 and then
2619 (Is_Limited_Type (Etype (F))
2620 or else Is_Limited_Type (Etype (Expression (A))))
2621 then
2622 Error_Msg_N
2623 ("Conversion between unrelated limited array types "
2624 & "not allowed (\A\I-00246)?", A);
2626 -- Disable explanation (which produces additional errors)
2627 -- until AI is approved and warning becomes an error.
2629 -- if Is_Limited_Type (Etype (F)) then
2630 -- Explain_Limited_Type (Etype (F), A);
2631 -- end if;
2633 -- if Is_Limited_Type (Etype (Expression (A))) then
2634 -- Explain_Limited_Type (Etype (Expression (A)), A);
2635 -- end if;
2636 end if;
2638 Resolve (A, Etype (F));
2639 end if;
2641 A_Typ := Etype (A);
2642 F_Typ := Etype (F);
2644 -- Perform error checks for IN and IN OUT parameters
2646 if Ekind (F) /= E_Out_Parameter then
2648 -- Check unset reference. For scalar parameters, it is clearly
2649 -- wrong to pass an uninitialized value as either an IN or
2650 -- IN-OUT parameter. For composites, it is also clearly an
2651 -- error to pass a completely uninitialized value as an IN
2652 -- parameter, but the case of IN OUT is trickier. We prefer
2653 -- not to give a warning here. For example, suppose there is
2654 -- a routine that sets some component of a record to False.
2655 -- It is perfectly reasonable to make this IN-OUT and allow
2656 -- either initialized or uninitialized records to be passed
2657 -- in this case.
2659 -- For partially initialized composite values, we also avoid
2660 -- warnings, since it is quite likely that we are passing a
2661 -- partially initialized value and only the initialized fields
2662 -- will in fact be read in the subprogram.
2664 if Is_Scalar_Type (A_Typ)
2665 or else (Ekind (F) = E_In_Parameter
2666 and then not Is_Partially_Initialized_Type (A_Typ))
2667 then
2668 Check_Unset_Reference (A);
2669 end if;
2671 -- In Ada 83 we cannot pass an OUT parameter as an IN
2672 -- or IN OUT actual to a nested call, since this is a
2673 -- case of reading an out parameter, which is not allowed.
2675 if Ada_Version = Ada_83
2676 and then Is_Entity_Name (A)
2677 and then Ekind (Entity (A)) = E_Out_Parameter
2678 then
2679 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
2680 end if;
2681 end if;
2683 if Ekind (F) /= E_In_Parameter
2684 and then not Is_OK_Variable_For_Out_Formal (A)
2685 then
2686 Error_Msg_NE ("actual for& must be a variable", A, F);
2688 if Is_Entity_Name (A) then
2689 Kill_Checks (Entity (A));
2690 else
2691 Kill_All_Checks;
2692 end if;
2693 end if;
2695 if Etype (A) = Any_Type then
2696 Set_Etype (N, Any_Type);
2697 return;
2698 end if;
2700 -- Apply appropriate range checks for in, out, and in-out
2701 -- parameters. Out and in-out parameters also need a separate
2702 -- check, if there is a type conversion, to make sure the return
2703 -- value meets the constraints of the variable before the
2704 -- conversion.
2706 -- Gigi looks at the check flag and uses the appropriate types.
2707 -- For now since one flag is used there is an optimization which
2708 -- might not be done in the In Out case since Gigi does not do
2709 -- any analysis. More thought required about this ???
2711 if Ekind (F) = E_In_Parameter
2712 or else Ekind (F) = E_In_Out_Parameter
2713 then
2714 if Is_Scalar_Type (Etype (A)) then
2715 Apply_Scalar_Range_Check (A, F_Typ);
2717 elsif Is_Array_Type (Etype (A)) then
2718 Apply_Length_Check (A, F_Typ);
2720 elsif Is_Record_Type (F_Typ)
2721 and then Has_Discriminants (F_Typ)
2722 and then Is_Constrained (F_Typ)
2723 and then (not Is_Derived_Type (F_Typ)
2724 or else Comes_From_Source (Nam))
2725 then
2726 Apply_Discriminant_Check (A, F_Typ);
2728 elsif Is_Access_Type (F_Typ)
2729 and then Is_Array_Type (Designated_Type (F_Typ))
2730 and then Is_Constrained (Designated_Type (F_Typ))
2731 then
2732 Apply_Length_Check (A, F_Typ);
2734 elsif Is_Access_Type (F_Typ)
2735 and then Has_Discriminants (Designated_Type (F_Typ))
2736 and then Is_Constrained (Designated_Type (F_Typ))
2737 then
2738 Apply_Discriminant_Check (A, F_Typ);
2740 else
2741 Apply_Range_Check (A, F_Typ);
2742 end if;
2744 -- Ada 2005 (AI-231)
2746 if Ada_Version >= Ada_05
2747 and then Is_Access_Type (F_Typ)
2748 and then (Can_Never_Be_Null (F)
2749 or else Can_Never_Be_Null (F_Typ))
2750 then
2751 if Nkind (A) = N_Null then
2752 Apply_Compile_Time_Constraint_Error
2753 (N => A,
2754 Msg => "(Ada 2005) NULL not allowed in "
2755 & "null-excluding formal?",
2756 Reason => CE_Null_Not_Allowed);
2757 end if;
2758 end if;
2759 end if;
2761 if Ekind (F) = E_Out_Parameter
2762 or else Ekind (F) = E_In_Out_Parameter
2763 then
2764 if Nkind (A) = N_Type_Conversion then
2765 if Is_Scalar_Type (A_Typ) then
2766 Apply_Scalar_Range_Check
2767 (Expression (A), Etype (Expression (A)), A_Typ);
2768 else
2769 Apply_Range_Check
2770 (Expression (A), Etype (Expression (A)), A_Typ);
2771 end if;
2773 else
2774 if Is_Scalar_Type (F_Typ) then
2775 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
2777 elsif Is_Array_Type (F_Typ)
2778 and then Ekind (F) = E_Out_Parameter
2779 then
2780 Apply_Length_Check (A, F_Typ);
2782 else
2783 Apply_Range_Check (A, A_Typ, F_Typ);
2784 end if;
2785 end if;
2786 end if;
2788 -- An actual associated with an access parameter is implicitly
2789 -- converted to the anonymous access type of the formal and
2790 -- must satisfy the legality checks for access conversions.
2792 if Ekind (F_Typ) = E_Anonymous_Access_Type then
2793 if not Valid_Conversion (A, F_Typ, A) then
2794 Error_Msg_N
2795 ("invalid implicit conversion for access parameter", A);
2796 end if;
2797 end if;
2799 -- Check bad case of atomic/volatile argument (RM C.6(12))
2801 if Is_By_Reference_Type (Etype (F))
2802 and then Comes_From_Source (N)
2803 then
2804 if Is_Atomic_Object (A)
2805 and then not Is_Atomic (Etype (F))
2806 then
2807 Error_Msg_N
2808 ("cannot pass atomic argument to non-atomic formal",
2811 elsif Is_Volatile_Object (A)
2812 and then not Is_Volatile (Etype (F))
2813 then
2814 Error_Msg_N
2815 ("cannot pass volatile argument to non-volatile formal",
2817 end if;
2818 end if;
2820 -- Check that subprograms don't have improper controlling
2821 -- arguments (RM 3.9.2 (9))
2823 if Is_Controlling_Formal (F) then
2824 Set_Is_Controlling_Actual (A);
2825 elsif Nkind (A) = N_Explicit_Dereference then
2826 Validate_Remote_Access_To_Class_Wide_Type (A);
2827 end if;
2829 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
2830 and then not Is_Class_Wide_Type (F_Typ)
2831 and then not Is_Controlling_Formal (F)
2832 then
2833 Error_Msg_N ("class-wide argument not allowed here!", A);
2835 if Is_Subprogram (Nam)
2836 and then Comes_From_Source (Nam)
2837 then
2838 Error_Msg_Node_2 := F_Typ;
2839 Error_Msg_NE
2840 ("& is not a dispatching operation of &!", A, Nam);
2841 end if;
2843 elsif Is_Access_Type (A_Typ)
2844 and then Is_Access_Type (F_Typ)
2845 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
2846 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
2847 or else (Nkind (A) = N_Attribute_Reference
2848 and then
2849 Is_Class_Wide_Type (Etype (Prefix (A)))))
2850 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
2851 and then not Is_Controlling_Formal (F)
2852 then
2853 Error_Msg_N
2854 ("access to class-wide argument not allowed here!", A);
2856 if Is_Subprogram (Nam)
2857 and then Comes_From_Source (Nam)
2858 then
2859 Error_Msg_Node_2 := Designated_Type (F_Typ);
2860 Error_Msg_NE
2861 ("& is not a dispatching operation of &!", A, Nam);
2862 end if;
2863 end if;
2865 Eval_Actual (A);
2867 -- If it is a named association, treat the selector_name as
2868 -- a proper identifier, and mark the corresponding entity.
2870 if Nkind (Parent (A)) = N_Parameter_Association then
2871 Set_Entity (Selector_Name (Parent (A)), F);
2872 Generate_Reference (F, Selector_Name (Parent (A)));
2873 Set_Etype (Selector_Name (Parent (A)), F_Typ);
2874 Generate_Reference (F_Typ, N, ' ');
2875 end if;
2877 Prev := A;
2879 if Ekind (F) /= E_Out_Parameter then
2880 Check_Unset_Reference (A);
2881 end if;
2883 Next_Actual (A);
2885 -- Case where actual is not present
2887 else
2888 Insert_Default;
2889 end if;
2891 Next_Formal (F);
2892 end loop;
2893 end Resolve_Actuals;
2895 -----------------------
2896 -- Resolve_Allocator --
2897 -----------------------
2899 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
2900 E : constant Node_Id := Expression (N);
2901 Subtyp : Entity_Id;
2902 Discrim : Entity_Id;
2903 Constr : Node_Id;
2904 Disc_Exp : Node_Id;
2906 function In_Dispatching_Context return Boolean;
2907 -- If the allocator is an actual in a call, it is allowed to be
2908 -- class-wide when the context is not because it is a controlling
2909 -- actual.
2911 ----------------------------
2912 -- In_Dispatching_Context --
2913 ----------------------------
2915 function In_Dispatching_Context return Boolean is
2916 Par : constant Node_Id := Parent (N);
2918 begin
2919 return (Nkind (Par) = N_Function_Call
2920 or else Nkind (Par) = N_Procedure_Call_Statement)
2921 and then Is_Entity_Name (Name (Par))
2922 and then Is_Dispatching_Operation (Entity (Name (Par)));
2923 end In_Dispatching_Context;
2925 -- Start of processing for Resolve_Allocator
2927 begin
2928 -- Replace general access with specific type
2930 if Ekind (Etype (N)) = E_Allocator_Type then
2931 Set_Etype (N, Base_Type (Typ));
2932 end if;
2934 if Is_Abstract (Typ) then
2935 Error_Msg_N ("type of allocator cannot be abstract", N);
2936 end if;
2938 -- For qualified expression, resolve the expression using the
2939 -- given subtype (nothing to do for type mark, subtype indication)
2941 if Nkind (E) = N_Qualified_Expression then
2942 if Is_Class_Wide_Type (Etype (E))
2943 and then not Is_Class_Wide_Type (Designated_Type (Typ))
2944 and then not In_Dispatching_Context
2945 then
2946 Error_Msg_N
2947 ("class-wide allocator not allowed for this access type", N);
2948 end if;
2950 Resolve (Expression (E), Etype (E));
2951 Check_Unset_Reference (Expression (E));
2953 -- A qualified expression requires an exact match of the type,
2954 -- class-wide matching is not allowed.
2956 if (Is_Class_Wide_Type (Etype (Expression (E)))
2957 or else Is_Class_Wide_Type (Etype (E)))
2958 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
2959 then
2960 Wrong_Type (Expression (E), Etype (E));
2961 end if;
2963 -- For a subtype mark or subtype indication, freeze the subtype
2965 else
2966 Freeze_Expression (E);
2968 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
2969 Error_Msg_N
2970 ("initialization required for access-to-constant allocator", N);
2971 end if;
2973 -- A special accessibility check is needed for allocators that
2974 -- constrain access discriminants. The level of the type of the
2975 -- expression used to contrain an access discriminant cannot be
2976 -- deeper than the type of the allocator (in constrast to access
2977 -- parameters, where the level of the actual can be arbitrary).
2978 -- We can't use Valid_Conversion to perform this check because
2979 -- in general the type of the allocator is unrelated to the type
2980 -- of the access discriminant. Note that specialized checks are
2981 -- needed for the cases of a constraint expression which is an
2982 -- access attribute or an access discriminant.
2984 if Nkind (Original_Node (E)) = N_Subtype_Indication
2985 and then Ekind (Typ) /= E_Anonymous_Access_Type
2986 then
2987 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
2989 if Has_Discriminants (Subtyp) then
2990 Discrim := First_Discriminant (Base_Type (Subtyp));
2991 Constr := First (Constraints (Constraint (Original_Node (E))));
2993 while Present (Discrim) and then Present (Constr) loop
2994 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
2995 if Nkind (Constr) = N_Discriminant_Association then
2996 Disc_Exp := Original_Node (Expression (Constr));
2997 else
2998 Disc_Exp := Original_Node (Constr);
2999 end if;
3001 if Type_Access_Level (Etype (Disc_Exp))
3002 > Type_Access_Level (Typ)
3003 then
3004 Error_Msg_N
3005 ("operand type has deeper level than allocator type",
3006 Disc_Exp);
3008 elsif Nkind (Disc_Exp) = N_Attribute_Reference
3009 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
3010 = Attribute_Access
3011 and then Object_Access_Level (Prefix (Disc_Exp))
3012 > Type_Access_Level (Typ)
3013 then
3014 Error_Msg_N
3015 ("prefix of attribute has deeper level than"
3016 & " allocator type", Disc_Exp);
3018 -- When the operand is an access discriminant the check
3019 -- is against the level of the prefix object.
3021 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
3022 and then Nkind (Disc_Exp) = N_Selected_Component
3023 and then Object_Access_Level (Prefix (Disc_Exp))
3024 > Type_Access_Level (Typ)
3025 then
3026 Error_Msg_N
3027 ("access discriminant has deeper level than"
3028 & " allocator type", Disc_Exp);
3029 end if;
3030 end if;
3031 Next_Discriminant (Discrim);
3032 Next (Constr);
3033 end loop;
3034 end if;
3035 end if;
3036 end if;
3038 -- Check for allocation from an empty storage pool
3040 if No_Pool_Assigned (Typ) then
3041 declare
3042 Loc : constant Source_Ptr := Sloc (N);
3044 begin
3045 Error_Msg_N ("?allocation from empty storage pool!", N);
3046 Error_Msg_N ("?Storage_Error will be raised at run time!", N);
3047 Insert_Action (N,
3048 Make_Raise_Storage_Error (Loc,
3049 Reason => SE_Empty_Storage_Pool));
3050 end;
3051 end if;
3052 end Resolve_Allocator;
3054 ---------------------------
3055 -- Resolve_Arithmetic_Op --
3056 ---------------------------
3058 -- Used for resolving all arithmetic operators except exponentiation
3060 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
3061 L : constant Node_Id := Left_Opnd (N);
3062 R : constant Node_Id := Right_Opnd (N);
3063 TL : constant Entity_Id := Base_Type (Etype (L));
3064 TR : constant Entity_Id := Base_Type (Etype (R));
3065 T : Entity_Id;
3066 Rop : Node_Id;
3068 B_Typ : constant Entity_Id := Base_Type (Typ);
3069 -- We do the resolution using the base type, because intermediate values
3070 -- in expressions always are of the base type, not a subtype of it.
3072 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
3073 -- Return True iff given type is Integer or universal real/integer
3075 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
3076 -- Choose type of integer literal in fixed-point operation to conform
3077 -- to available fixed-point type. T is the type of the other operand,
3078 -- which is needed to determine the expected type of N.
3080 procedure Set_Operand_Type (N : Node_Id);
3081 -- Set operand type to T if universal
3083 -----------------------------
3084 -- Is_Integer_Or_Universal --
3085 -----------------------------
3087 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
3088 T : Entity_Id;
3089 Index : Interp_Index;
3090 It : Interp;
3092 begin
3093 if not Is_Overloaded (N) then
3094 T := Etype (N);
3095 return Base_Type (T) = Base_Type (Standard_Integer)
3096 or else T = Universal_Integer
3097 or else T = Universal_Real;
3098 else
3099 Get_First_Interp (N, Index, It);
3101 while Present (It.Typ) loop
3103 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
3104 or else It.Typ = Universal_Integer
3105 or else It.Typ = Universal_Real
3106 then
3107 return True;
3108 end if;
3110 Get_Next_Interp (Index, It);
3111 end loop;
3112 end if;
3114 return False;
3115 end Is_Integer_Or_Universal;
3117 ----------------------------
3118 -- Set_Mixed_Mode_Operand --
3119 ----------------------------
3121 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
3122 Index : Interp_Index;
3123 It : Interp;
3125 begin
3126 if Universal_Interpretation (N) = Universal_Integer then
3128 -- A universal integer literal is resolved as standard integer
3129 -- except in the case of a fixed-point result, where we leave
3130 -- it as universal (to be handled by Exp_Fixd later on)
3132 if Is_Fixed_Point_Type (T) then
3133 Resolve (N, Universal_Integer);
3134 else
3135 Resolve (N, Standard_Integer);
3136 end if;
3138 elsif Universal_Interpretation (N) = Universal_Real
3139 and then (T = Base_Type (Standard_Integer)
3140 or else T = Universal_Integer
3141 or else T = Universal_Real)
3142 then
3143 -- A universal real can appear in a fixed-type context. We resolve
3144 -- the literal with that context, even though this might raise an
3145 -- exception prematurely (the other operand may be zero).
3147 Resolve (N, B_Typ);
3149 elsif Etype (N) = Base_Type (Standard_Integer)
3150 and then T = Universal_Real
3151 and then Is_Overloaded (N)
3152 then
3153 -- Integer arg in mixed-mode operation. Resolve with universal
3154 -- type, in case preference rule must be applied.
3156 Resolve (N, Universal_Integer);
3158 elsif Etype (N) = T
3159 and then B_Typ /= Universal_Fixed
3160 then
3161 -- Not a mixed-mode operation, resolve with context
3163 Resolve (N, B_Typ);
3165 elsif Etype (N) = Any_Fixed then
3167 -- N may itself be a mixed-mode operation, so use context type
3169 Resolve (N, B_Typ);
3171 elsif Is_Fixed_Point_Type (T)
3172 and then B_Typ = Universal_Fixed
3173 and then Is_Overloaded (N)
3174 then
3175 -- Must be (fixed * fixed) operation, operand must have one
3176 -- compatible interpretation.
3178 Resolve (N, Any_Fixed);
3180 elsif Is_Fixed_Point_Type (B_Typ)
3181 and then (T = Universal_Real
3182 or else Is_Fixed_Point_Type (T))
3183 and then Is_Overloaded (N)
3184 then
3185 -- C * F(X) in a fixed context, where C is a real literal or a
3186 -- fixed-point expression. F must have either a fixed type
3187 -- interpretation or an integer interpretation, but not both.
3189 Get_First_Interp (N, Index, It);
3191 while Present (It.Typ) loop
3192 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
3194 if Analyzed (N) then
3195 Error_Msg_N ("ambiguous operand in fixed operation", N);
3196 else
3197 Resolve (N, Standard_Integer);
3198 end if;
3200 elsif Is_Fixed_Point_Type (It.Typ) then
3202 if Analyzed (N) then
3203 Error_Msg_N ("ambiguous operand in fixed operation", N);
3204 else
3205 Resolve (N, It.Typ);
3206 end if;
3207 end if;
3209 Get_Next_Interp (Index, It);
3210 end loop;
3212 -- Reanalyze the literal with the fixed type of the context.
3213 -- If context is Universal_Fixed, we are within a conversion,
3214 -- leave the literal as a universal real because there is no
3215 -- usable fixed type, and the target of the conversion plays
3216 -- no role in the resolution.
3218 declare
3219 Op2 : Node_Id;
3220 T2 : Entity_Id;
3222 begin
3223 if N = L then
3224 Op2 := R;
3225 else
3226 Op2 := L;
3227 end if;
3229 if B_Typ = Universal_Fixed
3230 and then Nkind (Op2) = N_Real_Literal
3231 then
3232 T2 := Universal_Real;
3233 else
3234 T2 := B_Typ;
3235 end if;
3237 Set_Analyzed (Op2, False);
3238 Resolve (Op2, T2);
3239 end;
3241 else
3242 Resolve (N);
3243 end if;
3244 end Set_Mixed_Mode_Operand;
3246 ----------------------
3247 -- Set_Operand_Type --
3248 ----------------------
3250 procedure Set_Operand_Type (N : Node_Id) is
3251 begin
3252 if Etype (N) = Universal_Integer
3253 or else Etype (N) = Universal_Real
3254 then
3255 Set_Etype (N, T);
3256 end if;
3257 end Set_Operand_Type;
3259 -- Start of processing for Resolve_Arithmetic_Op
3261 begin
3262 if Comes_From_Source (N)
3263 and then Ekind (Entity (N)) = E_Function
3264 and then Is_Imported (Entity (N))
3265 and then Is_Intrinsic_Subprogram (Entity (N))
3266 then
3267 Resolve_Intrinsic_Operator (N, Typ);
3268 return;
3270 -- Special-case for mixed-mode universal expressions or fixed point
3271 -- type operation: each argument is resolved separately. The same
3272 -- treatment is required if one of the operands of a fixed point
3273 -- operation is universal real, since in this case we don't do a
3274 -- conversion to a specific fixed-point type (instead the expander
3275 -- takes care of the case).
3277 elsif (B_Typ = Universal_Integer
3278 or else B_Typ = Universal_Real)
3279 and then Present (Universal_Interpretation (L))
3280 and then Present (Universal_Interpretation (R))
3281 then
3282 Resolve (L, Universal_Interpretation (L));
3283 Resolve (R, Universal_Interpretation (R));
3284 Set_Etype (N, B_Typ);
3286 elsif (B_Typ = Universal_Real
3287 or else Etype (N) = Universal_Fixed
3288 or else (Etype (N) = Any_Fixed
3289 and then Is_Fixed_Point_Type (B_Typ))
3290 or else (Is_Fixed_Point_Type (B_Typ)
3291 and then (Is_Integer_Or_Universal (L)
3292 or else
3293 Is_Integer_Or_Universal (R))))
3294 and then (Nkind (N) = N_Op_Multiply or else
3295 Nkind (N) = N_Op_Divide)
3296 then
3297 if TL = Universal_Integer or else TR = Universal_Integer then
3298 Check_For_Visible_Operator (N, B_Typ);
3299 end if;
3301 -- If context is a fixed type and one operand is integer, the
3302 -- other is resolved with the type of the context.
3304 if Is_Fixed_Point_Type (B_Typ)
3305 and then (Base_Type (TL) = Base_Type (Standard_Integer)
3306 or else TL = Universal_Integer)
3307 then
3308 Resolve (R, B_Typ);
3309 Resolve (L, TL);
3311 elsif Is_Fixed_Point_Type (B_Typ)
3312 and then (Base_Type (TR) = Base_Type (Standard_Integer)
3313 or else TR = Universal_Integer)
3314 then
3315 Resolve (L, B_Typ);
3316 Resolve (R, TR);
3318 else
3319 Set_Mixed_Mode_Operand (L, TR);
3320 Set_Mixed_Mode_Operand (R, TL);
3321 end if;
3323 if Etype (N) = Universal_Fixed
3324 or else Etype (N) = Any_Fixed
3325 then
3326 if B_Typ = Universal_Fixed
3327 and then Nkind (Parent (N)) /= N_Type_Conversion
3328 and then Nkind (Parent (N)) /= N_Unchecked_Type_Conversion
3329 then
3330 Error_Msg_N
3331 ("type cannot be determined from context!", N);
3332 Error_Msg_N
3333 ("\explicit conversion to result type required", N);
3335 Set_Etype (L, Any_Type);
3336 Set_Etype (R, Any_Type);
3338 else
3339 if Ada_Version = Ada_83
3340 and then Etype (N) = Universal_Fixed
3341 and then Nkind (Parent (N)) /= N_Type_Conversion
3342 and then Nkind (Parent (N)) /= N_Unchecked_Type_Conversion
3343 then
3344 Error_Msg_N
3345 ("(Ada 83) fixed-point operation " &
3346 "needs explicit conversion",
3348 end if;
3350 Set_Etype (N, B_Typ);
3351 end if;
3353 elsif Is_Fixed_Point_Type (B_Typ)
3354 and then (Is_Integer_Or_Universal (L)
3355 or else Nkind (L) = N_Real_Literal
3356 or else Nkind (R) = N_Real_Literal
3357 or else
3358 Is_Integer_Or_Universal (R))
3359 then
3360 Set_Etype (N, B_Typ);
3362 elsif Etype (N) = Any_Fixed then
3364 -- If no previous errors, this is only possible if one operand
3365 -- is overloaded and the context is universal. Resolve as such.
3367 Set_Etype (N, B_Typ);
3368 end if;
3370 else
3371 if (TL = Universal_Integer or else TL = Universal_Real)
3372 and then (TR = Universal_Integer or else TR = Universal_Real)
3373 then
3374 Check_For_Visible_Operator (N, B_Typ);
3375 end if;
3377 -- If the context is Universal_Fixed and the operands are also
3378 -- universal fixed, this is an error, unless there is only one
3379 -- applicable fixed_point type (usually duration).
3381 if B_Typ = Universal_Fixed
3382 and then Etype (L) = Universal_Fixed
3383 then
3384 T := Unique_Fixed_Point_Type (N);
3386 if T = Any_Type then
3387 Set_Etype (N, T);
3388 return;
3389 else
3390 Resolve (L, T);
3391 Resolve (R, T);
3392 end if;
3394 else
3395 Resolve (L, B_Typ);
3396 Resolve (R, B_Typ);
3397 end if;
3399 -- If one of the arguments was resolved to a non-universal type.
3400 -- label the result of the operation itself with the same type.
3401 -- Do the same for the universal argument, if any.
3403 T := Intersect_Types (L, R);
3404 Set_Etype (N, Base_Type (T));
3405 Set_Operand_Type (L);
3406 Set_Operand_Type (R);
3407 end if;
3409 Generate_Operator_Reference (N, Typ);
3410 Eval_Arithmetic_Op (N);
3412 -- Set overflow and division checking bit. Much cleverer code needed
3413 -- here eventually and perhaps the Resolve routines should be separated
3414 -- for the various arithmetic operations, since they will need
3415 -- different processing. ???
3417 if Nkind (N) in N_Op then
3418 if not Overflow_Checks_Suppressed (Etype (N)) then
3419 Enable_Overflow_Check (N);
3420 end if;
3422 -- Give warning if explicit division by zero
3424 if (Nkind (N) = N_Op_Divide
3425 or else Nkind (N) = N_Op_Rem
3426 or else Nkind (N) = N_Op_Mod)
3427 and then not Division_Checks_Suppressed (Etype (N))
3428 then
3429 Rop := Right_Opnd (N);
3431 if Compile_Time_Known_Value (Rop)
3432 and then ((Is_Integer_Type (Etype (Rop))
3433 and then Expr_Value (Rop) = Uint_0)
3434 or else
3435 (Is_Real_Type (Etype (Rop))
3436 and then Expr_Value_R (Rop) = Ureal_0))
3437 then
3438 Apply_Compile_Time_Constraint_Error
3439 (N, "division by zero?", CE_Divide_By_Zero,
3440 Loc => Sloc (Right_Opnd (N)));
3442 -- Otherwise just set the flag to check at run time
3444 else
3445 Set_Do_Division_Check (N);
3446 end if;
3447 end if;
3448 end if;
3450 Check_Unset_Reference (L);
3451 Check_Unset_Reference (R);
3452 end Resolve_Arithmetic_Op;
3454 ------------------
3455 -- Resolve_Call --
3456 ------------------
3458 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
3459 Loc : constant Source_Ptr := Sloc (N);
3460 Subp : constant Node_Id := Name (N);
3461 Nam : Entity_Id;
3462 I : Interp_Index;
3463 It : Interp;
3464 Norm_OK : Boolean;
3465 Scop : Entity_Id;
3466 W : Node_Id;
3468 begin
3469 -- The context imposes a unique interpretation with type Typ on
3470 -- a procedure or function call. Find the entity of the subprogram
3471 -- that yields the expected type, and propagate the corresponding
3472 -- formal constraints on the actuals. The caller has established
3473 -- that an interpretation exists, and emitted an error if not unique.
3475 -- First deal with the case of a call to an access-to-subprogram,
3476 -- dereference made explicit in Analyze_Call.
3478 if Ekind (Etype (Subp)) = E_Subprogram_Type then
3479 if not Is_Overloaded (Subp) then
3480 Nam := Etype (Subp);
3482 else
3483 -- Find the interpretation whose type (a subprogram type)
3484 -- has a return type that is compatible with the context.
3485 -- Analysis of the node has established that one exists.
3487 Get_First_Interp (Subp, I, It);
3488 Nam := Empty;
3490 while Present (It.Typ) loop
3491 if Covers (Typ, Etype (It.Typ)) then
3492 Nam := It.Typ;
3493 exit;
3494 end if;
3496 Get_Next_Interp (I, It);
3497 end loop;
3499 if No (Nam) then
3500 raise Program_Error;
3501 end if;
3502 end if;
3504 -- If the prefix is not an entity, then resolve it
3506 if not Is_Entity_Name (Subp) then
3507 Resolve (Subp, Nam);
3508 end if;
3510 -- For an indirect call, we always invalidate checks, since we
3511 -- do not know whether the subprogram is local or global. Yes
3512 -- we could do better here, e.g. by knowing that there are no
3513 -- local subprograms, but it does not seem worth the effort.
3514 -- Similarly, we kill al knowledge of current constant values.
3516 Kill_Current_Values;
3518 -- If this is a procedure call which is really an entry call, do
3519 -- the conversion of the procedure call to an entry call. Protected
3520 -- operations use the same circuitry because the name in the call
3521 -- can be an arbitrary expression with special resolution rules.
3523 elsif Nkind (Subp) = N_Selected_Component
3524 or else Nkind (Subp) = N_Indexed_Component
3525 or else (Is_Entity_Name (Subp)
3526 and then Ekind (Entity (Subp)) = E_Entry)
3527 then
3528 Resolve_Entry_Call (N, Typ);
3529 Check_Elab_Call (N);
3531 -- Kill checks and constant values, as above for indirect case
3532 -- Who knows what happens when another task is activated?
3534 Kill_Current_Values;
3535 return;
3537 -- Normal subprogram call with name established in Resolve
3539 elsif not (Is_Type (Entity (Subp))) then
3540 Nam := Entity (Subp);
3541 Set_Entity_With_Style_Check (Subp, Nam);
3542 Generate_Reference (Nam, Subp);
3544 -- Otherwise we must have the case of an overloaded call
3546 else
3547 pragma Assert (Is_Overloaded (Subp));
3548 Nam := Empty; -- We know that it will be assigned in loop below.
3550 Get_First_Interp (Subp, I, It);
3552 while Present (It.Typ) loop
3553 if Covers (Typ, It.Typ) then
3554 Nam := It.Nam;
3555 Set_Entity_With_Style_Check (Subp, Nam);
3556 Generate_Reference (Nam, Subp);
3557 exit;
3558 end if;
3560 Get_Next_Interp (I, It);
3561 end loop;
3562 end if;
3564 -- Check that a call to Current_Task does not occur in an entry body
3566 if Is_RTE (Nam, RE_Current_Task) then
3567 declare
3568 P : Node_Id;
3570 begin
3571 P := N;
3572 loop
3573 P := Parent (P);
3574 exit when No (P);
3576 if Nkind (P) = N_Entry_Body then
3577 Error_Msg_NE
3578 ("& should not be used in entry body ('R'M C.7(17))",
3579 N, Nam);
3580 exit;
3581 end if;
3582 end loop;
3583 end;
3584 end if;
3586 -- Cannot call thread body directly
3588 if Is_Thread_Body (Nam) then
3589 Error_Msg_N ("cannot call thread body directly", N);
3590 end if;
3592 -- If the subprogram is not global, then kill all checks. This is
3593 -- a bit conservative, since in many cases we could do better, but
3594 -- it is not worth the effort. Similarly, we kill constant values.
3595 -- However we do not need to do this for internal entities (unless
3596 -- they are inherited user-defined subprograms), since they are not
3597 -- in the business of molesting global values.
3599 if not Is_Library_Level_Entity (Nam)
3600 and then (Comes_From_Source (Nam)
3601 or else (Present (Alias (Nam))
3602 and then Comes_From_Source (Alias (Nam))))
3603 then
3604 Kill_Current_Values;
3605 end if;
3607 -- Check for call to obsolescent subprogram
3609 if Warn_On_Obsolescent_Feature
3610 and then Is_Subprogram (Nam)
3611 and then Is_Obsolescent (Nam)
3612 then
3613 Error_Msg_NE ("call to obsolescent subprogram&?", N, Nam);
3615 -- Output additional warning if present
3617 W := Obsolescent_Warning (Nam);
3619 if Present (W) then
3620 Name_Buffer (1) := '|';
3621 Name_Buffer (2) := '?';
3622 Name_Len := 2;
3624 -- Add characters to message, protecting all of them
3626 for J in 1 .. String_Length (Strval (W)) loop
3627 Add_Char_To_Name_Buffer (''');
3628 Add_Char_To_Name_Buffer
3629 (Get_Character (Get_String_Char (Strval (W), J)));
3630 end loop;
3632 Error_Msg_N (Name_Buffer (1 .. Name_Len), N);
3633 end if;
3634 end if;
3636 -- Check that a procedure call does not occur in the context
3637 -- of the entry call statement of a conditional or timed
3638 -- entry call. Note that the case of a call to a subprogram
3639 -- renaming of an entry will also be rejected. The test
3640 -- for N not being an N_Entry_Call_Statement is defensive,
3641 -- covering the possibility that the processing of entry
3642 -- calls might reach this point due to later modifications
3643 -- of the code above.
3645 if Nkind (Parent (N)) = N_Entry_Call_Alternative
3646 and then Nkind (N) /= N_Entry_Call_Statement
3647 and then Entry_Call_Statement (Parent (N)) = N
3648 then
3649 Error_Msg_N ("entry call required in select statement", N);
3650 end if;
3652 -- Check that this is not a call to a protected procedure or
3653 -- entry from within a protected function.
3655 if Ekind (Current_Scope) = E_Function
3656 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
3657 and then Ekind (Nam) /= E_Function
3658 and then Scope (Nam) = Scope (Current_Scope)
3659 then
3660 Error_Msg_N ("within protected function, protected " &
3661 "object is constant", N);
3662 Error_Msg_N ("\cannot call operation that may modify it", N);
3663 end if;
3665 -- Freeze the subprogram name if not in default expression. Note
3666 -- that we freeze procedure calls as well as function calls.
3667 -- Procedure calls are not frozen according to the rules (RM
3668 -- 13.14(14)) because it is impossible to have a procedure call to
3669 -- a non-frozen procedure in pure Ada, but in the code that we
3670 -- generate in the expander, this rule needs extending because we
3671 -- can generate procedure calls that need freezing.
3673 if Is_Entity_Name (Subp) and then not In_Default_Expression then
3674 Freeze_Expression (Subp);
3675 end if;
3677 -- For a predefined operator, the type of the result is the type
3678 -- imposed by context, except for a predefined operation on universal
3679 -- fixed. Otherwise The type of the call is the type returned by the
3680 -- subprogram being called.
3682 if Is_Predefined_Op (Nam) then
3683 if Etype (N) /= Universal_Fixed then
3684 Set_Etype (N, Typ);
3685 end if;
3687 -- If the subprogram returns an array type, and the context
3688 -- requires the component type of that array type, the node is
3689 -- really an indexing of the parameterless call. Resolve as such.
3690 -- A pathological case occurs when the type of the component is
3691 -- an access to the array type. In this case the call is truly
3692 -- ambiguous.
3694 elsif Needs_No_Actuals (Nam)
3695 and then
3696 ((Is_Array_Type (Etype (Nam))
3697 and then Covers (Typ, Component_Type (Etype (Nam))))
3698 or else (Is_Access_Type (Etype (Nam))
3699 and then Is_Array_Type (Designated_Type (Etype (Nam)))
3700 and then
3701 Covers (Typ,
3702 Component_Type (Designated_Type (Etype (Nam))))))
3703 then
3704 declare
3705 Index_Node : Node_Id;
3706 New_Subp : Node_Id;
3707 Ret_Type : constant Entity_Id := Etype (Nam);
3709 begin
3710 if Is_Access_Type (Ret_Type)
3711 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
3712 then
3713 Error_Msg_N
3714 ("cannot disambiguate function call and indexing", N);
3715 else
3716 New_Subp := Relocate_Node (Subp);
3717 Set_Entity (Subp, Nam);
3719 if Component_Type (Ret_Type) /= Any_Type then
3720 Index_Node :=
3721 Make_Indexed_Component (Loc,
3722 Prefix =>
3723 Make_Function_Call (Loc,
3724 Name => New_Subp),
3725 Expressions => Parameter_Associations (N));
3727 -- Since we are correcting a node classification error made
3728 -- by the parser, we call Replace rather than Rewrite.
3730 Replace (N, Index_Node);
3731 Set_Etype (Prefix (N), Ret_Type);
3732 Set_Etype (N, Typ);
3733 Resolve_Indexed_Component (N, Typ);
3734 Check_Elab_Call (Prefix (N));
3735 end if;
3736 end if;
3738 return;
3739 end;
3741 else
3742 Set_Etype (N, Etype (Nam));
3743 end if;
3745 -- In the case where the call is to an overloaded subprogram, Analyze
3746 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
3747 -- such a case Normalize_Actuals needs to be called once more to order
3748 -- the actuals correctly. Otherwise the call will have the ordering
3749 -- given by the last overloaded subprogram whether this is the correct
3750 -- one being called or not.
3752 if Is_Overloaded (Subp) then
3753 Normalize_Actuals (N, Nam, False, Norm_OK);
3754 pragma Assert (Norm_OK);
3755 end if;
3757 -- In any case, call is fully resolved now. Reset Overload flag, to
3758 -- prevent subsequent overload resolution if node is analyzed again
3760 Set_Is_Overloaded (Subp, False);
3761 Set_Is_Overloaded (N, False);
3763 -- If we are calling the current subprogram from immediately within
3764 -- its body, then that is the case where we can sometimes detect
3765 -- cases of infinite recursion statically. Do not try this in case
3766 -- restriction No_Recursion is in effect anyway.
3768 Scop := Current_Scope;
3770 if Nam = Scop
3771 and then not Restriction_Active (No_Recursion)
3772 and then Check_Infinite_Recursion (N)
3773 then
3774 -- Here we detected and flagged an infinite recursion, so we do
3775 -- not need to test the case below for further warnings.
3777 null;
3779 -- If call is to immediately containing subprogram, then check for
3780 -- the case of a possible run-time detectable infinite recursion.
3782 else
3783 while Scop /= Standard_Standard loop
3784 if Nam = Scop then
3785 -- Although in general recursion is not statically checkable,
3786 -- the case of calling an immediately containing subprogram
3787 -- is easy to catch.
3789 Check_Restriction (No_Recursion, N);
3791 -- If the recursive call is to a parameterless procedure, then
3792 -- even if we can't statically detect infinite recursion, this
3793 -- is pretty suspicious, and we output a warning. Furthermore,
3794 -- we will try later to detect some cases here at run time by
3795 -- expanding checking code (see Detect_Infinite_Recursion in
3796 -- package Exp_Ch6).
3798 -- If the recursive call is within a handler we do not emit a
3799 -- warning, because this is a common idiom: loop until input
3800 -- is correct, catch illegal input in handler and restart.
3802 if No (First_Formal (Nam))
3803 and then Etype (Nam) = Standard_Void_Type
3804 and then not Error_Posted (N)
3805 and then Nkind (Parent (N)) /= N_Exception_Handler
3806 then
3807 Set_Has_Recursive_Call (Nam);
3808 Error_Msg_N ("possible infinite recursion?", N);
3809 Error_Msg_N ("Storage_Error may be raised at run time?", N);
3810 end if;
3812 exit;
3813 end if;
3815 Scop := Scope (Scop);
3816 end loop;
3817 end if;
3819 -- If subprogram name is a predefined operator, it was given in
3820 -- functional notation. Replace call node with operator node, so
3821 -- that actuals can be resolved appropriately.
3823 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
3824 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
3825 return;
3827 elsif Present (Alias (Nam))
3828 and then Is_Predefined_Op (Alias (Nam))
3829 then
3830 Resolve_Actuals (N, Nam);
3831 Make_Call_Into_Operator (N, Typ, Alias (Nam));
3832 return;
3833 end if;
3835 -- Create a transient scope if the resulting type requires it
3837 -- There are 3 notable exceptions: in init procs, the transient scope
3838 -- overhead is not needed and even incorrect due to the actual expansion
3839 -- of adjust calls; the second case is enumeration literal pseudo calls,
3840 -- the other case is intrinsic subprograms (Unchecked_Conversion and
3841 -- source information functions) that do not use the secondary stack
3842 -- even though the return type is unconstrained.
3844 -- If this is an initialization call for a type whose initialization
3845 -- uses the secondary stack, we also need to create a transient scope
3846 -- for it, precisely because we will not do it within the init proc
3847 -- itself.
3849 if Expander_Active
3850 and then Is_Type (Etype (Nam))
3851 and then Requires_Transient_Scope (Etype (Nam))
3852 and then Ekind (Nam) /= E_Enumeration_Literal
3853 and then not Within_Init_Proc
3854 and then not Is_Intrinsic_Subprogram (Nam)
3855 then
3856 Establish_Transient_Scope
3857 (N, Sec_Stack => not Functions_Return_By_DSP_On_Target);
3859 -- If the call appears within the bounds of a loop, it will
3860 -- be rewritten and reanalyzed, nothing left to do here.
3862 if Nkind (N) /= N_Function_Call then
3863 return;
3864 end if;
3866 elsif Is_Init_Proc (Nam)
3867 and then not Within_Init_Proc
3868 then
3869 Check_Initialization_Call (N, Nam);
3870 end if;
3872 -- A protected function cannot be called within the definition of the
3873 -- enclosing protected type.
3875 if Is_Protected_Type (Scope (Nam))
3876 and then In_Open_Scopes (Scope (Nam))
3877 and then not Has_Completion (Scope (Nam))
3878 then
3879 Error_Msg_NE
3880 ("& cannot be called before end of protected definition", N, Nam);
3881 end if;
3883 -- Propagate interpretation to actuals, and add default expressions
3884 -- where needed.
3886 if Present (First_Formal (Nam)) then
3887 Resolve_Actuals (N, Nam);
3889 -- Overloaded literals are rewritten as function calls, for
3890 -- purpose of resolution. After resolution, we can replace
3891 -- the call with the literal itself.
3893 elsif Ekind (Nam) = E_Enumeration_Literal then
3894 Copy_Node (Subp, N);
3895 Resolve_Entity_Name (N, Typ);
3897 -- Avoid validation, since it is a static function call
3899 return;
3900 end if;
3902 -- If the subprogram is a primitive operation, check whether or not
3903 -- it is a correct dispatching call.
3905 if Is_Overloadable (Nam)
3906 and then Is_Dispatching_Operation (Nam)
3907 then
3908 Check_Dispatching_Call (N);
3910 elsif Is_Abstract (Nam)
3911 and then not In_Instance
3912 then
3913 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
3914 end if;
3916 if Is_Intrinsic_Subprogram (Nam) then
3917 Check_Intrinsic_Call (N);
3918 end if;
3920 Eval_Call (N);
3921 Check_Elab_Call (N);
3922 end Resolve_Call;
3924 -------------------------------
3925 -- Resolve_Character_Literal --
3926 -------------------------------
3928 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
3929 B_Typ : constant Entity_Id := Base_Type (Typ);
3930 C : Entity_Id;
3932 begin
3933 -- Verify that the character does belong to the type of the context
3935 Set_Etype (N, B_Typ);
3936 Eval_Character_Literal (N);
3938 -- Wide_Wide_Character literals must always be defined, since the set
3939 -- of wide wide character literals is complete, i.e. if a character
3940 -- literal is accepted by the parser, then it is OK for wide wide
3941 -- character (out of range character literals are rejected).
3943 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
3944 return;
3946 -- Always accept character literal for type Any_Character, which
3947 -- occurs in error situations and in comparisons of literals, both
3948 -- of which should accept all literals.
3950 elsif B_Typ = Any_Character then
3951 return;
3953 -- For Standard.Character or a type derived from it, check that
3954 -- the literal is in range
3956 elsif Root_Type (B_Typ) = Standard_Character then
3957 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
3958 return;
3959 end if;
3961 -- For Standard.Wide_Character or a type derived from it, check
3962 -- that the literal is in range
3964 elsif Root_Type (B_Typ) = Standard_Wide_Character then
3965 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
3966 return;
3967 end if;
3969 -- For Standard.Wide_Wide_Character or a type derived from it, we
3970 -- know the literal is in range, since the parser checked!
3972 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
3973 return;
3975 -- If the entity is already set, this has already been resolved in
3976 -- a generic context, or comes from expansion. Nothing else to do.
3978 elsif Present (Entity (N)) then
3979 return;
3981 -- Otherwise we have a user defined character type, and we can use
3982 -- the standard visibility mechanisms to locate the referenced entity
3984 else
3985 C := Current_Entity (N);
3987 while Present (C) loop
3988 if Etype (C) = B_Typ then
3989 Set_Entity_With_Style_Check (N, C);
3990 Generate_Reference (C, N);
3991 return;
3992 end if;
3994 C := Homonym (C);
3995 end loop;
3996 end if;
3998 -- If we fall through, then the literal does not match any of the
3999 -- entries of the enumeration type. This isn't just a constraint
4000 -- error situation, it is an illegality (see RM 4.2).
4002 Error_Msg_NE
4003 ("character not defined for }", N, First_Subtype (B_Typ));
4004 end Resolve_Character_Literal;
4006 ---------------------------
4007 -- Resolve_Comparison_Op --
4008 ---------------------------
4010 -- Context requires a boolean type, and plays no role in resolution.
4011 -- Processing identical to that for equality operators. The result
4012 -- type is the base type, which matters when pathological subtypes of
4013 -- booleans with limited ranges are used.
4015 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
4016 L : constant Node_Id := Left_Opnd (N);
4017 R : constant Node_Id := Right_Opnd (N);
4018 T : Entity_Id;
4020 begin
4021 Check_Direct_Boolean_Op (N);
4023 -- If this is an intrinsic operation which is not predefined, use
4024 -- the types of its declared arguments to resolve the possibly
4025 -- overloaded operands. Otherwise the operands are unambiguous and
4026 -- specify the expected type.
4028 if Scope (Entity (N)) /= Standard_Standard then
4029 T := Etype (First_Entity (Entity (N)));
4030 else
4031 T := Find_Unique_Type (L, R);
4033 if T = Any_Fixed then
4034 T := Unique_Fixed_Point_Type (L);
4035 end if;
4036 end if;
4038 Set_Etype (N, Base_Type (Typ));
4039 Generate_Reference (T, N, ' ');
4041 if T /= Any_Type then
4042 if T = Any_String
4043 or else T = Any_Composite
4044 or else T = Any_Character
4045 then
4046 if T = Any_Character then
4047 Ambiguous_Character (L);
4048 else
4049 Error_Msg_N ("ambiguous operands for comparison", N);
4050 end if;
4052 Set_Etype (N, Any_Type);
4053 return;
4055 else
4056 Resolve (L, T);
4057 Resolve (R, T);
4058 Check_Unset_Reference (L);
4059 Check_Unset_Reference (R);
4060 Generate_Operator_Reference (N, T);
4061 Eval_Relational_Op (N);
4062 end if;
4063 end if;
4064 end Resolve_Comparison_Op;
4066 ------------------------------------
4067 -- Resolve_Conditional_Expression --
4068 ------------------------------------
4070 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
4071 Condition : constant Node_Id := First (Expressions (N));
4072 Then_Expr : constant Node_Id := Next (Condition);
4073 Else_Expr : constant Node_Id := Next (Then_Expr);
4075 begin
4076 Resolve (Condition, Standard_Boolean);
4077 Resolve (Then_Expr, Typ);
4078 Resolve (Else_Expr, Typ);
4080 Set_Etype (N, Typ);
4081 Eval_Conditional_Expression (N);
4082 end Resolve_Conditional_Expression;
4084 -----------------------------------------
4085 -- Resolve_Discrete_Subtype_Indication --
4086 -----------------------------------------
4088 procedure Resolve_Discrete_Subtype_Indication
4089 (N : Node_Id;
4090 Typ : Entity_Id)
4092 R : Node_Id;
4093 S : Entity_Id;
4095 begin
4096 Analyze (Subtype_Mark (N));
4097 S := Entity (Subtype_Mark (N));
4099 if Nkind (Constraint (N)) /= N_Range_Constraint then
4100 Error_Msg_N ("expect range constraint for discrete type", N);
4101 Set_Etype (N, Any_Type);
4103 else
4104 R := Range_Expression (Constraint (N));
4106 if R = Error then
4107 return;
4108 end if;
4110 Analyze (R);
4112 if Base_Type (S) /= Base_Type (Typ) then
4113 Error_Msg_NE
4114 ("expect subtype of }", N, First_Subtype (Typ));
4116 -- Rewrite the constraint as a range of Typ
4117 -- to allow compilation to proceed further.
4119 Set_Etype (N, Typ);
4120 Rewrite (Low_Bound (R),
4121 Make_Attribute_Reference (Sloc (Low_Bound (R)),
4122 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
4123 Attribute_Name => Name_First));
4124 Rewrite (High_Bound (R),
4125 Make_Attribute_Reference (Sloc (High_Bound (R)),
4126 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
4127 Attribute_Name => Name_First));
4129 else
4130 Resolve (R, Typ);
4131 Set_Etype (N, Etype (R));
4133 -- Additionally, we must check that the bounds are compatible
4134 -- with the given subtype, which might be different from the
4135 -- type of the context.
4137 Apply_Range_Check (R, S);
4139 -- ??? If the above check statically detects a Constraint_Error
4140 -- it replaces the offending bound(s) of the range R with a
4141 -- Constraint_Error node. When the itype which uses these bounds
4142 -- is frozen the resulting call to Duplicate_Subexpr generates
4143 -- a new temporary for the bounds.
4145 -- Unfortunately there are other itypes that are also made depend
4146 -- on these bounds, so when Duplicate_Subexpr is called they get
4147 -- a forward reference to the newly created temporaries and Gigi
4148 -- aborts on such forward references. This is probably sign of a
4149 -- more fundamental problem somewhere else in either the order of
4150 -- itype freezing or the way certain itypes are constructed.
4152 -- To get around this problem we call Remove_Side_Effects right
4153 -- away if either bounds of R are a Constraint_Error.
4155 declare
4156 L : constant Node_Id := Low_Bound (R);
4157 H : constant Node_Id := High_Bound (R);
4159 begin
4160 if Nkind (L) = N_Raise_Constraint_Error then
4161 Remove_Side_Effects (L);
4162 end if;
4164 if Nkind (H) = N_Raise_Constraint_Error then
4165 Remove_Side_Effects (H);
4166 end if;
4167 end;
4169 Check_Unset_Reference (Low_Bound (R));
4170 Check_Unset_Reference (High_Bound (R));
4171 end if;
4172 end if;
4173 end Resolve_Discrete_Subtype_Indication;
4175 -------------------------
4176 -- Resolve_Entity_Name --
4177 -------------------------
4179 -- Used to resolve identifiers and expanded names
4181 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
4182 E : constant Entity_Id := Entity (N);
4184 begin
4185 -- If garbage from errors, set to Any_Type and return
4187 if No (E) and then Total_Errors_Detected /= 0 then
4188 Set_Etype (N, Any_Type);
4189 return;
4190 end if;
4192 -- Replace named numbers by corresponding literals. Note that this is
4193 -- the one case where Resolve_Entity_Name must reset the Etype, since
4194 -- it is currently marked as universal.
4196 if Ekind (E) = E_Named_Integer then
4197 Set_Etype (N, Typ);
4198 Eval_Named_Integer (N);
4200 elsif Ekind (E) = E_Named_Real then
4201 Set_Etype (N, Typ);
4202 Eval_Named_Real (N);
4204 -- Allow use of subtype only if it is a concurrent type where we are
4205 -- currently inside the body. This will eventually be expanded
4206 -- into a call to Self (for tasks) or _object (for protected
4207 -- objects). Any other use of a subtype is invalid.
4209 elsif Is_Type (E) then
4210 if Is_Concurrent_Type (E)
4211 and then In_Open_Scopes (E)
4212 then
4213 null;
4214 else
4215 Error_Msg_N
4216 ("Invalid use of subtype mark in expression or call", N);
4217 end if;
4219 -- Check discriminant use if entity is discriminant in current scope,
4220 -- i.e. discriminant of record or concurrent type currently being
4221 -- analyzed. Uses in corresponding body are unrestricted.
4223 elsif Ekind (E) = E_Discriminant
4224 and then Scope (E) = Current_Scope
4225 and then not Has_Completion (Current_Scope)
4226 then
4227 Check_Discriminant_Use (N);
4229 -- A parameterless generic function cannot appear in a context that
4230 -- requires resolution.
4232 elsif Ekind (E) = E_Generic_Function then
4233 Error_Msg_N ("illegal use of generic function", N);
4235 elsif Ekind (E) = E_Out_Parameter
4236 and then Ada_Version = Ada_83
4237 and then (Nkind (Parent (N)) in N_Op
4238 or else (Nkind (Parent (N)) = N_Assignment_Statement
4239 and then N = Expression (Parent (N)))
4240 or else Nkind (Parent (N)) = N_Explicit_Dereference)
4241 then
4242 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
4244 -- In all other cases, just do the possible static evaluation
4246 else
4247 -- A deferred constant that appears in an expression must have
4248 -- a completion, unless it has been removed by in-place expansion
4249 -- of an aggregate.
4251 if Ekind (E) = E_Constant
4252 and then Comes_From_Source (E)
4253 and then No (Constant_Value (E))
4254 and then Is_Frozen (Etype (E))
4255 and then not In_Default_Expression
4256 and then not Is_Imported (E)
4257 then
4259 if No_Initialization (Parent (E))
4260 or else (Present (Full_View (E))
4261 and then No_Initialization (Parent (Full_View (E))))
4262 then
4263 null;
4264 else
4265 Error_Msg_N (
4266 "deferred constant is frozen before completion", N);
4267 end if;
4268 end if;
4270 Eval_Entity_Name (N);
4271 end if;
4272 end Resolve_Entity_Name;
4274 -------------------
4275 -- Resolve_Entry --
4276 -------------------
4278 procedure Resolve_Entry (Entry_Name : Node_Id) is
4279 Loc : constant Source_Ptr := Sloc (Entry_Name);
4280 Nam : Entity_Id;
4281 New_N : Node_Id;
4282 S : Entity_Id;
4283 Tsk : Entity_Id;
4284 E_Name : Node_Id;
4285 Index : Node_Id;
4287 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
4288 -- If the bounds of the entry family being called depend on task
4289 -- discriminants, build a new index subtype where a discriminant is
4290 -- replaced with the value of the discriminant of the target task.
4291 -- The target task is the prefix of the entry name in the call.
4293 -----------------------
4294 -- Actual_Index_Type --
4295 -----------------------
4297 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
4298 Typ : constant Entity_Id := Entry_Index_Type (E);
4299 Tsk : constant Entity_Id := Scope (E);
4300 Lo : constant Node_Id := Type_Low_Bound (Typ);
4301 Hi : constant Node_Id := Type_High_Bound (Typ);
4302 New_T : Entity_Id;
4304 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
4305 -- If the bound is given by a discriminant, replace with a reference
4306 -- to the discriminant of the same name in the target task.
4307 -- If the entry name is the target of a requeue statement and the
4308 -- entry is in the current protected object, the bound to be used
4309 -- is the discriminal of the object (see apply_range_checks for
4310 -- details of the transformation).
4312 -----------------------------
4313 -- Actual_Discriminant_Ref --
4314 -----------------------------
4316 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
4317 Typ : constant Entity_Id := Etype (Bound);
4318 Ref : Node_Id;
4320 begin
4321 Remove_Side_Effects (Bound);
4323 if not Is_Entity_Name (Bound)
4324 or else Ekind (Entity (Bound)) /= E_Discriminant
4325 then
4326 return Bound;
4328 elsif Is_Protected_Type (Tsk)
4329 and then In_Open_Scopes (Tsk)
4330 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
4331 then
4332 return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
4334 else
4335 Ref :=
4336 Make_Selected_Component (Loc,
4337 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
4338 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
4339 Analyze (Ref);
4340 Resolve (Ref, Typ);
4341 return Ref;
4342 end if;
4343 end Actual_Discriminant_Ref;
4345 -- Start of processing for Actual_Index_Type
4347 begin
4348 if not Has_Discriminants (Tsk)
4349 or else (not Is_Entity_Name (Lo)
4350 and then not Is_Entity_Name (Hi))
4351 then
4352 return Entry_Index_Type (E);
4354 else
4355 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
4356 Set_Etype (New_T, Base_Type (Typ));
4357 Set_Size_Info (New_T, Typ);
4358 Set_RM_Size (New_T, RM_Size (Typ));
4359 Set_Scalar_Range (New_T,
4360 Make_Range (Sloc (Entry_Name),
4361 Low_Bound => Actual_Discriminant_Ref (Lo),
4362 High_Bound => Actual_Discriminant_Ref (Hi)));
4364 return New_T;
4365 end if;
4366 end Actual_Index_Type;
4368 -- Start of processing of Resolve_Entry
4370 begin
4371 -- Find name of entry being called, and resolve prefix of name
4372 -- with its own type. The prefix can be overloaded, and the name
4373 -- and signature of the entry must be taken into account.
4375 if Nkind (Entry_Name) = N_Indexed_Component then
4377 -- Case of dealing with entry family within the current tasks
4379 E_Name := Prefix (Entry_Name);
4381 else
4382 E_Name := Entry_Name;
4383 end if;
4385 if Is_Entity_Name (E_Name) then
4386 -- Entry call to an entry (or entry family) in the current task.
4387 -- This is legal even though the task will deadlock. Rewrite as
4388 -- call to current task.
4390 -- This can also be a call to an entry in an enclosing task.
4391 -- If this is a single task, we have to retrieve its name,
4392 -- because the scope of the entry is the task type, not the
4393 -- object. If the enclosing task is a task type, the identity
4394 -- of the task is given by its own self variable.
4396 -- Finally this can be a requeue on an entry of the same task
4397 -- or protected object.
4399 S := Scope (Entity (E_Name));
4401 for J in reverse 0 .. Scope_Stack.Last loop
4403 if Is_Task_Type (Scope_Stack.Table (J).Entity)
4404 and then not Comes_From_Source (S)
4405 then
4406 -- S is an enclosing task or protected object. The concurrent
4407 -- declaration has been converted into a type declaration, and
4408 -- the object itself has an object declaration that follows
4409 -- the type in the same declarative part.
4411 Tsk := Next_Entity (S);
4413 while Etype (Tsk) /= S loop
4414 Next_Entity (Tsk);
4415 end loop;
4417 S := Tsk;
4418 exit;
4420 elsif S = Scope_Stack.Table (J).Entity then
4422 -- Call to current task. Will be transformed into call to Self
4424 exit;
4426 end if;
4427 end loop;
4429 New_N :=
4430 Make_Selected_Component (Loc,
4431 Prefix => New_Occurrence_Of (S, Loc),
4432 Selector_Name =>
4433 New_Occurrence_Of (Entity (E_Name), Loc));
4434 Rewrite (E_Name, New_N);
4435 Analyze (E_Name);
4437 elsif Nkind (Entry_Name) = N_Selected_Component
4438 and then Is_Overloaded (Prefix (Entry_Name))
4439 then
4440 -- Use the entry name (which must be unique at this point) to
4441 -- find the prefix that returns the corresponding task type or
4442 -- protected type.
4444 declare
4445 Pref : constant Node_Id := Prefix (Entry_Name);
4446 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
4447 I : Interp_Index;
4448 It : Interp;
4450 begin
4451 Get_First_Interp (Pref, I, It);
4453 while Present (It.Typ) loop
4455 if Scope (Ent) = It.Typ then
4456 Set_Etype (Pref, It.Typ);
4457 exit;
4458 end if;
4460 Get_Next_Interp (I, It);
4461 end loop;
4462 end;
4463 end if;
4465 if Nkind (Entry_Name) = N_Selected_Component then
4466 Resolve (Prefix (Entry_Name));
4468 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
4469 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
4470 Resolve (Prefix (Prefix (Entry_Name)));
4471 Index := First (Expressions (Entry_Name));
4472 Resolve (Index, Entry_Index_Type (Nam));
4474 -- Up to this point the expression could have been the actual
4475 -- in a simple entry call, and be given by a named association.
4477 if Nkind (Index) = N_Parameter_Association then
4478 Error_Msg_N ("expect expression for entry index", Index);
4479 else
4480 Apply_Range_Check (Index, Actual_Index_Type (Nam));
4481 end if;
4482 end if;
4483 end Resolve_Entry;
4485 ------------------------
4486 -- Resolve_Entry_Call --
4487 ------------------------
4489 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
4490 Entry_Name : constant Node_Id := Name (N);
4491 Loc : constant Source_Ptr := Sloc (Entry_Name);
4492 Actuals : List_Id;
4493 First_Named : Node_Id;
4494 Nam : Entity_Id;
4495 Norm_OK : Boolean;
4496 Obj : Node_Id;
4497 Was_Over : Boolean;
4499 begin
4500 -- We kill all checks here, because it does not seem worth the
4501 -- effort to do anything better, an entry call is a big operation.
4503 Kill_All_Checks;
4505 -- Processing of the name is similar for entry calls and protected
4506 -- operation calls. Once the entity is determined, we can complete
4507 -- the resolution of the actuals.
4509 -- The selector may be overloaded, in the case of a protected object
4510 -- with overloaded functions. The type of the context is used for
4511 -- resolution.
4513 if Nkind (Entry_Name) = N_Selected_Component
4514 and then Is_Overloaded (Selector_Name (Entry_Name))
4515 and then Typ /= Standard_Void_Type
4516 then
4517 declare
4518 I : Interp_Index;
4519 It : Interp;
4521 begin
4522 Get_First_Interp (Selector_Name (Entry_Name), I, It);
4524 while Present (It.Typ) loop
4526 if Covers (Typ, It.Typ) then
4527 Set_Entity (Selector_Name (Entry_Name), It.Nam);
4528 Set_Etype (Entry_Name, It.Typ);
4530 Generate_Reference (It.Typ, N, ' ');
4531 end if;
4533 Get_Next_Interp (I, It);
4534 end loop;
4535 end;
4536 end if;
4538 Resolve_Entry (Entry_Name);
4540 if Nkind (Entry_Name) = N_Selected_Component then
4542 -- Simple entry call
4544 Nam := Entity (Selector_Name (Entry_Name));
4545 Obj := Prefix (Entry_Name);
4546 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
4548 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
4550 -- Call to member of entry family
4552 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
4553 Obj := Prefix (Prefix (Entry_Name));
4554 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
4555 end if;
4557 -- We cannot in general check the maximum depth of protected entry
4558 -- calls at compile time. But we can tell that any protected entry
4559 -- call at all violates a specified nesting depth of zero.
4561 if Is_Protected_Type (Scope (Nam)) then
4562 Check_Restriction (Max_Entry_Queue_Length, N);
4563 end if;
4565 -- Use context type to disambiguate a protected function that can be
4566 -- called without actuals and that returns an array type, and where
4567 -- the argument list may be an indexing of the returned value.
4569 if Ekind (Nam) = E_Function
4570 and then Needs_No_Actuals (Nam)
4571 and then Present (Parameter_Associations (N))
4572 and then
4573 ((Is_Array_Type (Etype (Nam))
4574 and then Covers (Typ, Component_Type (Etype (Nam))))
4576 or else (Is_Access_Type (Etype (Nam))
4577 and then Is_Array_Type (Designated_Type (Etype (Nam)))
4578 and then Covers (Typ,
4579 Component_Type (Designated_Type (Etype (Nam))))))
4580 then
4581 declare
4582 Index_Node : Node_Id;
4584 begin
4585 Index_Node :=
4586 Make_Indexed_Component (Loc,
4587 Prefix =>
4588 Make_Function_Call (Loc,
4589 Name => Relocate_Node (Entry_Name)),
4590 Expressions => Parameter_Associations (N));
4592 -- Since we are correcting a node classification error made by
4593 -- the parser, we call Replace rather than Rewrite.
4595 Replace (N, Index_Node);
4596 Set_Etype (Prefix (N), Etype (Nam));
4597 Set_Etype (N, Typ);
4598 Resolve_Indexed_Component (N, Typ);
4599 return;
4600 end;
4601 end if;
4603 -- The operation name may have been overloaded. Order the actuals
4604 -- according to the formals of the resolved entity, and set the
4605 -- return type to that of the operation.
4607 if Was_Over then
4608 Normalize_Actuals (N, Nam, False, Norm_OK);
4609 pragma Assert (Norm_OK);
4610 Set_Etype (N, Etype (Nam));
4611 end if;
4613 Resolve_Actuals (N, Nam);
4614 Generate_Reference (Nam, Entry_Name);
4616 if Ekind (Nam) = E_Entry
4617 or else Ekind (Nam) = E_Entry_Family
4618 then
4619 Check_Potentially_Blocking_Operation (N);
4620 end if;
4622 -- Verify that a procedure call cannot masquerade as an entry
4623 -- call where an entry call is expected.
4625 if Ekind (Nam) = E_Procedure then
4626 if Nkind (Parent (N)) = N_Entry_Call_Alternative
4627 and then N = Entry_Call_Statement (Parent (N))
4628 then
4629 Error_Msg_N ("entry call required in select statement", N);
4631 elsif Nkind (Parent (N)) = N_Triggering_Alternative
4632 and then N = Triggering_Statement (Parent (N))
4633 then
4634 Error_Msg_N ("triggering statement cannot be procedure call", N);
4636 elsif Ekind (Scope (Nam)) = E_Task_Type
4637 and then not In_Open_Scopes (Scope (Nam))
4638 then
4639 Error_Msg_N ("Task has no entry with this name", Entry_Name);
4640 end if;
4641 end if;
4643 -- After resolution, entry calls and protected procedure calls
4644 -- are changed into entry calls, for expansion. The structure
4645 -- of the node does not change, so it can safely be done in place.
4646 -- Protected function calls must keep their structure because they
4647 -- are subexpressions.
4649 if Ekind (Nam) /= E_Function then
4651 -- A protected operation that is not a function may modify the
4652 -- corresponding object, and cannot apply to a constant.
4653 -- If this is an internal call, the prefix is the type itself.
4655 if Is_Protected_Type (Scope (Nam))
4656 and then not Is_Variable (Obj)
4657 and then (not Is_Entity_Name (Obj)
4658 or else not Is_Type (Entity (Obj)))
4659 then
4660 Error_Msg_N
4661 ("prefix of protected procedure or entry call must be variable",
4662 Entry_Name);
4663 end if;
4665 Actuals := Parameter_Associations (N);
4666 First_Named := First_Named_Actual (N);
4668 Rewrite (N,
4669 Make_Entry_Call_Statement (Loc,
4670 Name => Entry_Name,
4671 Parameter_Associations => Actuals));
4673 Set_First_Named_Actual (N, First_Named);
4674 Set_Analyzed (N, True);
4676 -- Protected functions can return on the secondary stack, in which
4677 -- case we must trigger the transient scope mechanism
4679 elsif Expander_Active
4680 and then Requires_Transient_Scope (Etype (Nam))
4681 then
4682 Establish_Transient_Scope (N,
4683 Sec_Stack => not Functions_Return_By_DSP_On_Target);
4684 end if;
4685 end Resolve_Entry_Call;
4687 -------------------------
4688 -- Resolve_Equality_Op --
4689 -------------------------
4691 -- Both arguments must have the same type, and the boolean context
4692 -- does not participate in the resolution. The first pass verifies
4693 -- that the interpretation is not ambiguous, and the type of the left
4694 -- argument is correctly set, or is Any_Type in case of ambiguity.
4695 -- If both arguments are strings or aggregates, allocators, or Null,
4696 -- they are ambiguous even though they carry a single (universal) type.
4697 -- Diagnose this case here.
4699 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
4700 L : constant Node_Id := Left_Opnd (N);
4701 R : constant Node_Id := Right_Opnd (N);
4702 T : Entity_Id := Find_Unique_Type (L, R);
4704 function Find_Unique_Access_Type return Entity_Id;
4705 -- In the case of allocators, make a last-ditch attempt to find a single
4706 -- access type with the right designated type. This is semantically
4707 -- dubious, and of no interest to any real code, but c48008a makes it
4708 -- all worthwhile.
4710 -----------------------------
4711 -- Find_Unique_Access_Type --
4712 -----------------------------
4714 function Find_Unique_Access_Type return Entity_Id is
4715 Acc : Entity_Id;
4716 E : Entity_Id;
4717 S : Entity_Id := Current_Scope;
4719 begin
4720 if Ekind (Etype (R)) = E_Allocator_Type then
4721 Acc := Designated_Type (Etype (R));
4723 elsif Ekind (Etype (L)) = E_Allocator_Type then
4724 Acc := Designated_Type (Etype (L));
4726 else
4727 return Empty;
4728 end if;
4730 while S /= Standard_Standard loop
4731 E := First_Entity (S);
4733 while Present (E) loop
4735 if Is_Type (E)
4736 and then Is_Access_Type (E)
4737 and then Ekind (E) /= E_Allocator_Type
4738 and then Designated_Type (E) = Base_Type (Acc)
4739 then
4740 return E;
4741 end if;
4743 Next_Entity (E);
4744 end loop;
4746 S := Scope (S);
4747 end loop;
4749 return Empty;
4750 end Find_Unique_Access_Type;
4752 -- Start of processing for Resolve_Equality_Op
4754 begin
4755 Check_Direct_Boolean_Op (N);
4757 Set_Etype (N, Base_Type (Typ));
4758 Generate_Reference (T, N, ' ');
4760 if T = Any_Fixed then
4761 T := Unique_Fixed_Point_Type (L);
4762 end if;
4764 if T /= Any_Type then
4766 if T = Any_String
4767 or else T = Any_Composite
4768 or else T = Any_Character
4769 then
4771 if T = Any_Character then
4772 Ambiguous_Character (L);
4773 else
4774 Error_Msg_N ("ambiguous operands for equality", N);
4775 end if;
4777 Set_Etype (N, Any_Type);
4778 return;
4780 elsif T = Any_Access
4781 or else Ekind (T) = E_Allocator_Type
4782 then
4783 T := Find_Unique_Access_Type;
4785 if No (T) then
4786 Error_Msg_N ("ambiguous operands for equality", N);
4787 Set_Etype (N, Any_Type);
4788 return;
4789 end if;
4790 end if;
4792 Resolve (L, T);
4793 Resolve (R, T);
4795 if Warn_On_Redundant_Constructs
4796 and then Comes_From_Source (N)
4797 and then Is_Entity_Name (R)
4798 and then Entity (R) = Standard_True
4799 and then Comes_From_Source (R)
4800 then
4801 Error_Msg_N ("comparison with True is redundant?", R);
4802 end if;
4804 Check_Unset_Reference (L);
4805 Check_Unset_Reference (R);
4806 Generate_Operator_Reference (N, T);
4808 -- If this is an inequality, it may be the implicit inequality
4809 -- created for a user-defined operation, in which case the corres-
4810 -- ponding equality operation is not intrinsic, and the operation
4811 -- cannot be constant-folded. Else fold.
4813 if Nkind (N) = N_Op_Eq
4814 or else Comes_From_Source (Entity (N))
4815 or else Ekind (Entity (N)) = E_Operator
4816 or else Is_Intrinsic_Subprogram
4817 (Corresponding_Equality (Entity (N)))
4818 then
4819 Eval_Relational_Op (N);
4820 elsif Nkind (N) = N_Op_Ne
4821 and then Is_Abstract (Entity (N))
4822 then
4823 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
4824 end if;
4825 end if;
4826 end Resolve_Equality_Op;
4828 ----------------------------------
4829 -- Resolve_Explicit_Dereference --
4830 ----------------------------------
4832 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
4833 Loc : constant Source_Ptr := Sloc (N);
4834 New_N : Node_Id;
4835 P : constant Node_Id := Prefix (N);
4836 I : Interp_Index;
4837 It : Interp;
4839 begin
4840 -- Now that we know the type, check that this is not a
4841 -- dereference of an uncompleted type. Note that this
4842 -- is not entirely correct, because dereferences of
4843 -- private types are legal in default expressions.
4844 -- This consideration also applies to similar checks
4845 -- for allocators, qualified expressions, and type
4846 -- conversions. ???
4848 Check_Fully_Declared (Typ, N);
4850 if Is_Overloaded (P) then
4852 -- Use the context type to select the prefix that has the
4853 -- correct designated type.
4855 Get_First_Interp (P, I, It);
4856 while Present (It.Typ) loop
4857 exit when Is_Access_Type (It.Typ)
4858 and then Covers (Typ, Designated_Type (It.Typ));
4860 Get_Next_Interp (I, It);
4861 end loop;
4863 if Present (It.Typ) then
4864 Resolve (P, It.Typ);
4865 else
4866 -- If no interpretation covers the designated type of the
4867 -- prefix, this is the pathological case where not all
4868 -- implementations of the prefix allow the interpretation
4869 -- of the node as a call. Now that the expected type is known,
4870 -- Remove other interpretations from prefix, rewrite it as
4871 -- a call, and resolve again, so that the proper call node
4872 -- is generated.
4874 Get_First_Interp (P, I, It);
4875 while Present (It.Typ) loop
4876 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
4877 Remove_Interp (I);
4878 end if;
4880 Get_Next_Interp (I, It);
4881 end loop;
4883 New_N :=
4884 Make_Function_Call (Loc,
4885 Name =>
4886 Make_Explicit_Dereference (Loc,
4887 Prefix => P),
4888 Parameter_Associations => New_List);
4890 Save_Interps (N, New_N);
4891 Rewrite (N, New_N);
4892 Analyze_And_Resolve (N, Typ);
4893 return;
4894 end if;
4896 Set_Etype (N, Designated_Type (It.Typ));
4898 else
4899 Resolve (P);
4900 end if;
4902 if Is_Access_Type (Etype (P)) then
4903 Apply_Access_Check (N);
4904 end if;
4906 -- If the designated type is a packed unconstrained array type,
4907 -- and the explicit dereference is not in the context of an
4908 -- attribute reference, then we must compute and set the actual
4909 -- subtype, since it is needed by Gigi. The reason we exclude
4910 -- the attribute case is that this is handled fine by Gigi, and
4911 -- in fact we use such attributes to build the actual subtype.
4912 -- We also exclude generated code (which builds actual subtypes
4913 -- directly if they are needed).
4915 if Is_Array_Type (Etype (N))
4916 and then Is_Packed (Etype (N))
4917 and then not Is_Constrained (Etype (N))
4918 and then Nkind (Parent (N)) /= N_Attribute_Reference
4919 and then Comes_From_Source (N)
4920 then
4921 Set_Etype (N, Get_Actual_Subtype (N));
4922 end if;
4924 -- Note: there is no Eval processing required for an explicit
4925 -- deference, because the type is known to be an allocators, and
4926 -- allocator expressions can never be static.
4928 end Resolve_Explicit_Dereference;
4930 -------------------------------
4931 -- Resolve_Indexed_Component --
4932 -------------------------------
4934 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
4935 Name : constant Node_Id := Prefix (N);
4936 Expr : Node_Id;
4937 Array_Type : Entity_Id := Empty; -- to prevent junk warning
4938 Index : Node_Id;
4940 begin
4941 if Is_Overloaded (Name) then
4943 -- Use the context type to select the prefix that yields the
4944 -- correct component type.
4946 declare
4947 I : Interp_Index;
4948 It : Interp;
4949 I1 : Interp_Index := 0;
4950 P : constant Node_Id := Prefix (N);
4951 Found : Boolean := False;
4953 begin
4954 Get_First_Interp (P, I, It);
4956 while Present (It.Typ) loop
4958 if (Is_Array_Type (It.Typ)
4959 and then Covers (Typ, Component_Type (It.Typ)))
4960 or else (Is_Access_Type (It.Typ)
4961 and then Is_Array_Type (Designated_Type (It.Typ))
4962 and then Covers
4963 (Typ, Component_Type (Designated_Type (It.Typ))))
4964 then
4965 if Found then
4966 It := Disambiguate (P, I1, I, Any_Type);
4968 if It = No_Interp then
4969 Error_Msg_N ("ambiguous prefix for indexing", N);
4970 Set_Etype (N, Typ);
4971 return;
4973 else
4974 Found := True;
4975 Array_Type := It.Typ;
4976 I1 := I;
4977 end if;
4979 else
4980 Found := True;
4981 Array_Type := It.Typ;
4982 I1 := I;
4983 end if;
4984 end if;
4986 Get_Next_Interp (I, It);
4987 end loop;
4988 end;
4990 else
4991 Array_Type := Etype (Name);
4992 end if;
4994 Resolve (Name, Array_Type);
4995 Array_Type := Get_Actual_Subtype_If_Available (Name);
4997 -- If prefix is access type, dereference to get real array type.
4998 -- Note: we do not apply an access check because the expander always
4999 -- introduces an explicit dereference, and the check will happen there.
5001 if Is_Access_Type (Array_Type) then
5002 Array_Type := Designated_Type (Array_Type);
5003 end if;
5005 -- If name was overloaded, set component type correctly now
5007 Set_Etype (N, Component_Type (Array_Type));
5009 Index := First_Index (Array_Type);
5010 Expr := First (Expressions (N));
5012 -- The prefix may have resolved to a string literal, in which case
5013 -- its etype has a special representation. This is only possible
5014 -- currently if the prefix is a static concatenation, written in
5015 -- functional notation.
5017 if Ekind (Array_Type) = E_String_Literal_Subtype then
5018 Resolve (Expr, Standard_Positive);
5020 else
5021 while Present (Index) and Present (Expr) loop
5022 Resolve (Expr, Etype (Index));
5023 Check_Unset_Reference (Expr);
5025 if Is_Scalar_Type (Etype (Expr)) then
5026 Apply_Scalar_Range_Check (Expr, Etype (Index));
5027 else
5028 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
5029 end if;
5031 Next_Index (Index);
5032 Next (Expr);
5033 end loop;
5034 end if;
5036 Eval_Indexed_Component (N);
5037 end Resolve_Indexed_Component;
5039 -----------------------------
5040 -- Resolve_Integer_Literal --
5041 -----------------------------
5043 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
5044 begin
5045 Set_Etype (N, Typ);
5046 Eval_Integer_Literal (N);
5047 end Resolve_Integer_Literal;
5049 --------------------------------
5050 -- Resolve_Intrinsic_Operator --
5051 --------------------------------
5053 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
5054 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
5055 Op : Entity_Id;
5056 Arg1 : Node_Id;
5057 Arg2 : Node_Id;
5059 begin
5060 Op := Entity (N);
5062 while Scope (Op) /= Standard_Standard loop
5063 Op := Homonym (Op);
5064 pragma Assert (Present (Op));
5065 end loop;
5067 Set_Entity (N, Op);
5068 Set_Is_Overloaded (N, False);
5070 -- If the operand type is private, rewrite with suitable
5071 -- conversions on the operands and the result, to expose
5072 -- the proper underlying numeric type.
5074 if Is_Private_Type (Typ) then
5075 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
5077 if Nkind (N) = N_Op_Expon then
5078 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
5079 else
5080 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
5081 end if;
5083 Save_Interps (Left_Opnd (N), Expression (Arg1));
5084 Save_Interps (Right_Opnd (N), Expression (Arg2));
5086 Set_Left_Opnd (N, Arg1);
5087 Set_Right_Opnd (N, Arg2);
5089 Set_Etype (N, Btyp);
5090 Rewrite (N, Unchecked_Convert_To (Typ, N));
5091 Resolve (N, Typ);
5093 elsif Typ /= Etype (Left_Opnd (N))
5094 or else Typ /= Etype (Right_Opnd (N))
5095 then
5096 -- Add explicit conversion where needed, and save interpretations
5097 -- in case operands are overloaded.
5099 Arg1 := Convert_To (Typ, Left_Opnd (N));
5100 Arg2 := Convert_To (Typ, Right_Opnd (N));
5102 if Nkind (Arg1) = N_Type_Conversion then
5103 Save_Interps (Left_Opnd (N), Expression (Arg1));
5104 else
5105 Save_Interps (Left_Opnd (N), Arg1);
5106 end if;
5108 if Nkind (Arg2) = N_Type_Conversion then
5109 Save_Interps (Right_Opnd (N), Expression (Arg2));
5110 else
5111 Save_Interps (Right_Opnd (N), Arg2);
5112 end if;
5114 Rewrite (Left_Opnd (N), Arg1);
5115 Rewrite (Right_Opnd (N), Arg2);
5116 Analyze (Arg1);
5117 Analyze (Arg2);
5118 Resolve_Arithmetic_Op (N, Typ);
5120 else
5121 Resolve_Arithmetic_Op (N, Typ);
5122 end if;
5123 end Resolve_Intrinsic_Operator;
5125 --------------------------------------
5126 -- Resolve_Intrinsic_Unary_Operator --
5127 --------------------------------------
5129 procedure Resolve_Intrinsic_Unary_Operator
5130 (N : Node_Id;
5131 Typ : Entity_Id)
5133 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
5134 Op : Entity_Id;
5135 Arg2 : Node_Id;
5137 begin
5138 Op := Entity (N);
5140 while Scope (Op) /= Standard_Standard loop
5141 Op := Homonym (Op);
5142 pragma Assert (Present (Op));
5143 end loop;
5145 Set_Entity (N, Op);
5147 if Is_Private_Type (Typ) then
5148 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
5149 Save_Interps (Right_Opnd (N), Expression (Arg2));
5151 Set_Right_Opnd (N, Arg2);
5153 Set_Etype (N, Btyp);
5154 Rewrite (N, Unchecked_Convert_To (Typ, N));
5155 Resolve (N, Typ);
5157 else
5158 Resolve_Unary_Op (N, Typ);
5159 end if;
5160 end Resolve_Intrinsic_Unary_Operator;
5162 ------------------------
5163 -- Resolve_Logical_Op --
5164 ------------------------
5166 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
5167 B_Typ : Entity_Id;
5169 begin
5170 Check_Direct_Boolean_Op (N);
5172 -- Predefined operations on scalar types yield the base type. On
5173 -- the other hand, logical operations on arrays yield the type of
5174 -- the arguments (and the context).
5176 if Is_Array_Type (Typ) then
5177 B_Typ := Typ;
5178 else
5179 B_Typ := Base_Type (Typ);
5180 end if;
5182 -- The following test is required because the operands of the operation
5183 -- may be literals, in which case the resulting type appears to be
5184 -- compatible with a signed integer type, when in fact it is compatible
5185 -- only with modular types. If the context itself is universal, the
5186 -- operation is illegal.
5188 if not Valid_Boolean_Arg (Typ) then
5189 Error_Msg_N ("invalid context for logical operation", N);
5190 Set_Etype (N, Any_Type);
5191 return;
5193 elsif Typ = Any_Modular then
5194 Error_Msg_N
5195 ("no modular type available in this context", N);
5196 Set_Etype (N, Any_Type);
5197 return;
5198 elsif Is_Modular_Integer_Type (Typ)
5199 and then Etype (Left_Opnd (N)) = Universal_Integer
5200 and then Etype (Right_Opnd (N)) = Universal_Integer
5201 then
5202 Check_For_Visible_Operator (N, B_Typ);
5203 end if;
5205 Resolve (Left_Opnd (N), B_Typ);
5206 Resolve (Right_Opnd (N), B_Typ);
5208 Check_Unset_Reference (Left_Opnd (N));
5209 Check_Unset_Reference (Right_Opnd (N));
5211 Set_Etype (N, B_Typ);
5212 Generate_Operator_Reference (N, B_Typ);
5213 Eval_Logical_Op (N);
5214 end Resolve_Logical_Op;
5216 ---------------------------
5217 -- Resolve_Membership_Op --
5218 ---------------------------
5220 -- The context can only be a boolean type, and does not determine
5221 -- the arguments. Arguments should be unambiguous, but the preference
5222 -- rule for universal types applies.
5224 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
5225 pragma Warnings (Off, Typ);
5227 L : constant Node_Id := Left_Opnd (N);
5228 R : constant Node_Id := Right_Opnd (N);
5229 T : Entity_Id;
5231 begin
5232 if L = Error or else R = Error then
5233 return;
5234 end if;
5236 if not Is_Overloaded (R)
5237 and then
5238 (Etype (R) = Universal_Integer or else
5239 Etype (R) = Universal_Real)
5240 and then Is_Overloaded (L)
5241 then
5242 T := Etype (R);
5243 else
5244 T := Intersect_Types (L, R);
5245 end if;
5247 Resolve (L, T);
5248 Check_Unset_Reference (L);
5250 if Nkind (R) = N_Range
5251 and then not Is_Scalar_Type (T)
5252 then
5253 Error_Msg_N ("scalar type required for range", R);
5254 end if;
5256 if Is_Entity_Name (R) then
5257 Freeze_Expression (R);
5258 else
5259 Resolve (R, T);
5260 Check_Unset_Reference (R);
5261 end if;
5263 Eval_Membership_Op (N);
5264 end Resolve_Membership_Op;
5266 ------------------
5267 -- Resolve_Null --
5268 ------------------
5270 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
5271 begin
5272 -- Handle restriction against anonymous null access values
5273 -- This restriction can be turned off using -gnatdh.
5275 -- Ada 2005 (AI-231): Remove restriction
5277 if Ada_Version < Ada_05
5278 and then not Debug_Flag_J
5279 and then Ekind (Typ) = E_Anonymous_Access_Type
5280 and then Comes_From_Source (N)
5281 then
5282 -- In the common case of a call which uses an explicitly null
5283 -- value for an access parameter, give specialized error msg
5285 if Nkind (Parent (N)) = N_Procedure_Call_Statement
5286 or else
5287 Nkind (Parent (N)) = N_Function_Call
5288 then
5289 Error_Msg_N
5290 ("null is not allowed as argument for an access parameter", N);
5292 -- Standard message for all other cases (are there any?)
5294 else
5295 Error_Msg_N
5296 ("null cannot be of an anonymous access type", N);
5297 end if;
5298 end if;
5300 -- In a distributed context, null for a remote access to subprogram
5301 -- may need to be replaced with a special record aggregate. In this
5302 -- case, return after having done the transformation.
5304 if (Ekind (Typ) = E_Record_Type
5305 or else Is_Remote_Access_To_Subprogram_Type (Typ))
5306 and then Remote_AST_Null_Value (N, Typ)
5307 then
5308 return;
5309 end if;
5311 -- The null literal takes its type from the context
5313 Set_Etype (N, Typ);
5314 end Resolve_Null;
5316 -----------------------
5317 -- Resolve_Op_Concat --
5318 -----------------------
5320 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
5321 Btyp : constant Entity_Id := Base_Type (Typ);
5322 Op1 : constant Node_Id := Left_Opnd (N);
5323 Op2 : constant Node_Id := Right_Opnd (N);
5325 procedure Resolve_Concatenation_Arg (Arg : Node_Id; Is_Comp : Boolean);
5326 -- Internal procedure to resolve one operand of concatenation operator.
5327 -- The operand is either of the array type or of the component type.
5328 -- If the operand is an aggregate, and the component type is composite,
5329 -- this is ambiguous if component type has aggregates.
5331 -------------------------------
5332 -- Resolve_Concatenation_Arg --
5333 -------------------------------
5335 procedure Resolve_Concatenation_Arg (Arg : Node_Id; Is_Comp : Boolean) is
5336 begin
5337 if In_Instance then
5338 if Is_Comp
5339 or else (not Is_Overloaded (Arg)
5340 and then Etype (Arg) /= Any_Composite
5341 and then Covers (Component_Type (Typ), Etype (Arg)))
5342 then
5343 Resolve (Arg, Component_Type (Typ));
5344 else
5345 Resolve (Arg, Btyp);
5346 end if;
5348 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
5350 if Nkind (Arg) = N_Aggregate
5351 and then Is_Composite_Type (Component_Type (Typ))
5352 then
5353 if Is_Private_Type (Component_Type (Typ)) then
5354 Resolve (Arg, Btyp);
5356 else
5357 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
5358 Set_Etype (Arg, Any_Type);
5359 end if;
5361 else
5362 if Is_Overloaded (Arg)
5363 and then Has_Compatible_Type (Arg, Typ)
5364 and then Etype (Arg) /= Any_Type
5365 then
5366 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
5368 declare
5369 I : Interp_Index;
5370 It : Interp;
5372 begin
5373 Get_First_Interp (Arg, I, It);
5375 while Present (It.Nam) loop
5377 if Base_Type (Etype (It.Nam)) = Base_Type (Typ)
5378 or else Base_Type (Etype (It.Nam)) =
5379 Base_Type (Component_Type (Typ))
5380 then
5381 Error_Msg_Sloc := Sloc (It.Nam);
5382 Error_Msg_N ("\possible interpretation#", Arg);
5383 end if;
5385 Get_Next_Interp (I, It);
5386 end loop;
5387 end;
5388 end if;
5390 Resolve (Arg, Component_Type (Typ));
5392 if Nkind (Arg) = N_String_Literal then
5393 Set_Etype (Arg, Component_Type (Typ));
5394 end if;
5396 if Arg = Left_Opnd (N) then
5397 Set_Is_Component_Left_Opnd (N);
5398 else
5399 Set_Is_Component_Right_Opnd (N);
5400 end if;
5401 end if;
5403 else
5404 Resolve (Arg, Btyp);
5405 end if;
5407 Check_Unset_Reference (Arg);
5408 end Resolve_Concatenation_Arg;
5410 -- Start of processing for Resolve_Op_Concat
5412 begin
5413 Set_Etype (N, Btyp);
5415 if Is_Limited_Composite (Btyp) then
5416 Error_Msg_N ("concatenation not available for limited array", N);
5417 Explain_Limited_Type (Btyp, N);
5418 end if;
5420 -- If the operands are themselves concatenations, resolve them as
5421 -- such directly. This removes several layers of recursion and allows
5422 -- GNAT to handle larger multiple concatenations.
5424 if Nkind (Op1) = N_Op_Concat
5425 and then not Is_Array_Type (Component_Type (Typ))
5426 and then Entity (Op1) = Entity (N)
5427 then
5428 Resolve_Op_Concat (Op1, Typ);
5429 else
5430 Resolve_Concatenation_Arg
5431 (Op1, Is_Component_Left_Opnd (N));
5432 end if;
5434 if Nkind (Op2) = N_Op_Concat
5435 and then not Is_Array_Type (Component_Type (Typ))
5436 and then Entity (Op2) = Entity (N)
5437 then
5438 Resolve_Op_Concat (Op2, Typ);
5439 else
5440 Resolve_Concatenation_Arg
5441 (Op2, Is_Component_Right_Opnd (N));
5442 end if;
5444 Generate_Operator_Reference (N, Typ);
5446 if Is_String_Type (Typ) then
5447 Eval_Concatenation (N);
5448 end if;
5450 -- If this is not a static concatenation, but the result is a
5451 -- string type (and not an array of strings) insure that static
5452 -- string operands have their subtypes properly constructed.
5454 if Nkind (N) /= N_String_Literal
5455 and then Is_Character_Type (Component_Type (Typ))
5456 then
5457 Set_String_Literal_Subtype (Op1, Typ);
5458 Set_String_Literal_Subtype (Op2, Typ);
5459 end if;
5460 end Resolve_Op_Concat;
5462 ----------------------
5463 -- Resolve_Op_Expon --
5464 ----------------------
5466 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
5467 B_Typ : constant Entity_Id := Base_Type (Typ);
5469 begin
5470 -- Catch attempts to do fixed-point exponentation with universal
5471 -- operands, which is a case where the illegality is not caught
5472 -- during normal operator analysis.
5474 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
5475 Error_Msg_N ("exponentiation not available for fixed point", N);
5476 return;
5477 end if;
5479 if Comes_From_Source (N)
5480 and then Ekind (Entity (N)) = E_Function
5481 and then Is_Imported (Entity (N))
5482 and then Is_Intrinsic_Subprogram (Entity (N))
5483 then
5484 Resolve_Intrinsic_Operator (N, Typ);
5485 return;
5486 end if;
5488 if Etype (Left_Opnd (N)) = Universal_Integer
5489 or else Etype (Left_Opnd (N)) = Universal_Real
5490 then
5491 Check_For_Visible_Operator (N, B_Typ);
5492 end if;
5494 -- We do the resolution using the base type, because intermediate values
5495 -- in expressions always are of the base type, not a subtype of it.
5497 Resolve (Left_Opnd (N), B_Typ);
5498 Resolve (Right_Opnd (N), Standard_Integer);
5500 Check_Unset_Reference (Left_Opnd (N));
5501 Check_Unset_Reference (Right_Opnd (N));
5503 Set_Etype (N, B_Typ);
5504 Generate_Operator_Reference (N, B_Typ);
5505 Eval_Op_Expon (N);
5507 -- Set overflow checking bit. Much cleverer code needed here eventually
5508 -- and perhaps the Resolve routines should be separated for the various
5509 -- arithmetic operations, since they will need different processing. ???
5511 if Nkind (N) in N_Op then
5512 if not Overflow_Checks_Suppressed (Etype (N)) then
5513 Enable_Overflow_Check (N);
5514 end if;
5515 end if;
5516 end Resolve_Op_Expon;
5518 --------------------
5519 -- Resolve_Op_Not --
5520 --------------------
5522 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
5523 B_Typ : Entity_Id;
5525 function Parent_Is_Boolean return Boolean;
5526 -- This function determines if the parent node is a boolean operator
5527 -- or operation (comparison op, membership test, or short circuit form)
5528 -- and the not in question is the left operand of this operation.
5529 -- Note that if the not is in parens, then false is returned.
5531 function Parent_Is_Boolean return Boolean is
5532 begin
5533 if Paren_Count (N) /= 0 then
5534 return False;
5536 else
5537 case Nkind (Parent (N)) is
5538 when N_Op_And |
5539 N_Op_Eq |
5540 N_Op_Ge |
5541 N_Op_Gt |
5542 N_Op_Le |
5543 N_Op_Lt |
5544 N_Op_Ne |
5545 N_Op_Or |
5546 N_Op_Xor |
5547 N_In |
5548 N_Not_In |
5549 N_And_Then |
5550 N_Or_Else =>
5552 return Left_Opnd (Parent (N)) = N;
5554 when others =>
5555 return False;
5556 end case;
5557 end if;
5558 end Parent_Is_Boolean;
5560 -- Start of processing for Resolve_Op_Not
5562 begin
5563 -- Predefined operations on scalar types yield the base type. On
5564 -- the other hand, logical operations on arrays yield the type of
5565 -- the arguments (and the context).
5567 if Is_Array_Type (Typ) then
5568 B_Typ := Typ;
5569 else
5570 B_Typ := Base_Type (Typ);
5571 end if;
5573 if not Valid_Boolean_Arg (Typ) then
5574 Error_Msg_N ("invalid operand type for operator&", N);
5575 Set_Etype (N, Any_Type);
5576 return;
5578 elsif Typ = Universal_Integer or else Typ = Any_Modular then
5579 if Parent_Is_Boolean then
5580 Error_Msg_N
5581 ("operand of not must be enclosed in parentheses",
5582 Right_Opnd (N));
5583 else
5584 Error_Msg_N
5585 ("no modular type available in this context", N);
5586 end if;
5588 Set_Etype (N, Any_Type);
5589 return;
5591 else
5592 if not Is_Boolean_Type (Typ)
5593 and then Parent_Is_Boolean
5594 then
5595 Error_Msg_N ("?not expression should be parenthesized here", N);
5596 end if;
5598 Resolve (Right_Opnd (N), B_Typ);
5599 Check_Unset_Reference (Right_Opnd (N));
5600 Set_Etype (N, B_Typ);
5601 Generate_Operator_Reference (N, B_Typ);
5602 Eval_Op_Not (N);
5603 end if;
5604 end Resolve_Op_Not;
5606 -----------------------------
5607 -- Resolve_Operator_Symbol --
5608 -----------------------------
5610 -- Nothing to be done, all resolved already
5612 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
5613 pragma Warnings (Off, N);
5614 pragma Warnings (Off, Typ);
5616 begin
5617 null;
5618 end Resolve_Operator_Symbol;
5620 ----------------------------------
5621 -- Resolve_Qualified_Expression --
5622 ----------------------------------
5624 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
5625 pragma Warnings (Off, Typ);
5627 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
5628 Expr : constant Node_Id := Expression (N);
5630 begin
5631 Resolve (Expr, Target_Typ);
5633 -- A qualified expression requires an exact match of the type,
5634 -- class-wide matching is not allowed.
5636 if Is_Class_Wide_Type (Target_Typ)
5637 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
5638 then
5639 Wrong_Type (Expr, Target_Typ);
5640 end if;
5642 -- If the target type is unconstrained, then we reset the type of
5643 -- the result from the type of the expression. For other cases, the
5644 -- actual subtype of the expression is the target type.
5646 if Is_Composite_Type (Target_Typ)
5647 and then not Is_Constrained (Target_Typ)
5648 then
5649 Set_Etype (N, Etype (Expr));
5650 end if;
5652 Eval_Qualified_Expression (N);
5653 end Resolve_Qualified_Expression;
5655 -------------------
5656 -- Resolve_Range --
5657 -------------------
5659 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
5660 L : constant Node_Id := Low_Bound (N);
5661 H : constant Node_Id := High_Bound (N);
5663 begin
5664 Set_Etype (N, Typ);
5665 Resolve (L, Typ);
5666 Resolve (H, Typ);
5668 Check_Unset_Reference (L);
5669 Check_Unset_Reference (H);
5671 -- We have to check the bounds for being within the base range as
5672 -- required for a non-static context. Normally this is automatic
5673 -- and done as part of evaluating expressions, but the N_Range
5674 -- node is an exception, since in GNAT we consider this node to
5675 -- be a subexpression, even though in Ada it is not. The circuit
5676 -- in Sem_Eval could check for this, but that would put the test
5677 -- on the main evaluation path for expressions.
5679 Check_Non_Static_Context (L);
5680 Check_Non_Static_Context (H);
5682 -- If bounds are static, constant-fold them, so size computations
5683 -- are identical between front-end and back-end. Do not perform this
5684 -- transformation while analyzing generic units, as type information
5685 -- would then be lost when reanalyzing the constant node in the
5686 -- instance.
5688 if Is_Discrete_Type (Typ) and then Expander_Active then
5689 if Is_OK_Static_Expression (L) then
5690 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
5691 end if;
5693 if Is_OK_Static_Expression (H) then
5694 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
5695 end if;
5696 end if;
5697 end Resolve_Range;
5699 --------------------------
5700 -- Resolve_Real_Literal --
5701 --------------------------
5703 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
5704 Actual_Typ : constant Entity_Id := Etype (N);
5706 begin
5707 -- Special processing for fixed-point literals to make sure that the
5708 -- value is an exact multiple of small where this is required. We
5709 -- skip this for the universal real case, and also for generic types.
5711 if Is_Fixed_Point_Type (Typ)
5712 and then Typ /= Universal_Fixed
5713 and then Typ /= Any_Fixed
5714 and then not Is_Generic_Type (Typ)
5715 then
5716 declare
5717 Val : constant Ureal := Realval (N);
5718 Cintr : constant Ureal := Val / Small_Value (Typ);
5719 Cint : constant Uint := UR_Trunc (Cintr);
5720 Den : constant Uint := Norm_Den (Cintr);
5721 Stat : Boolean;
5723 begin
5724 -- Case of literal is not an exact multiple of the Small
5726 if Den /= 1 then
5728 -- For a source program literal for a decimal fixed-point
5729 -- type, this is statically illegal (RM 4.9(36)).
5731 if Is_Decimal_Fixed_Point_Type (Typ)
5732 and then Actual_Typ = Universal_Real
5733 and then Comes_From_Source (N)
5734 then
5735 Error_Msg_N ("value has extraneous low order digits", N);
5736 end if;
5738 -- Generate a warning if literal from source
5740 if Is_Static_Expression (N)
5741 and then Warn_On_Bad_Fixed_Value
5742 then
5743 Error_Msg_N
5744 ("static fixed-point value is not a multiple of Small?",
5746 end if;
5748 -- Replace literal by a value that is the exact representation
5749 -- of a value of the type, i.e. a multiple of the small value,
5750 -- by truncation, since Machine_Rounds is false for all GNAT
5751 -- fixed-point types (RM 4.9(38)).
5753 Stat := Is_Static_Expression (N);
5754 Rewrite (N,
5755 Make_Real_Literal (Sloc (N),
5756 Realval => Small_Value (Typ) * Cint));
5758 Set_Is_Static_Expression (N, Stat);
5761 end if;
5763 -- In all cases, set the corresponding integer field
5765 Set_Corresponding_Integer_Value (N, Cint);
5766 end;
5767 end if;
5769 -- Now replace the actual type by the expected type as usual
5771 Set_Etype (N, Typ);
5772 Eval_Real_Literal (N);
5773 end Resolve_Real_Literal;
5775 -----------------------
5776 -- Resolve_Reference --
5777 -----------------------
5779 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
5780 P : constant Node_Id := Prefix (N);
5782 begin
5783 -- Replace general access with specific type
5785 if Ekind (Etype (N)) = E_Allocator_Type then
5786 Set_Etype (N, Base_Type (Typ));
5787 end if;
5789 Resolve (P, Designated_Type (Etype (N)));
5791 -- If we are taking the reference of a volatile entity, then treat
5792 -- it as a potential modification of this entity. This is much too
5793 -- conservative, but is necessary because remove side effects can
5794 -- result in transformations of normal assignments into reference
5795 -- sequences that otherwise fail to notice the modification.
5797 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
5798 Note_Possible_Modification (P);
5799 end if;
5800 end Resolve_Reference;
5802 --------------------------------
5803 -- Resolve_Selected_Component --
5804 --------------------------------
5806 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
5807 Comp : Entity_Id;
5808 Comp1 : Entity_Id := Empty; -- prevent junk warning
5809 P : constant Node_Id := Prefix (N);
5810 S : constant Node_Id := Selector_Name (N);
5811 T : Entity_Id := Etype (P);
5812 I : Interp_Index;
5813 I1 : Interp_Index := 0; -- prevent junk warning
5814 It : Interp;
5815 It1 : Interp;
5816 Found : Boolean;
5818 function Init_Component return Boolean;
5819 -- Check whether this is the initialization of a component within an
5820 -- init proc (by assignment or call to another init proc). If true,
5821 -- there is no need for a discriminant check.
5823 --------------------
5824 -- Init_Component --
5825 --------------------
5827 function Init_Component return Boolean is
5828 begin
5829 return Inside_Init_Proc
5830 and then Nkind (Prefix (N)) = N_Identifier
5831 and then Chars (Prefix (N)) = Name_uInit
5832 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
5833 end Init_Component;
5835 -- Start of processing for Resolve_Selected_Component
5837 begin
5838 if Is_Overloaded (P) then
5840 -- Use the context type to select the prefix that has a selector
5841 -- of the correct name and type.
5843 Found := False;
5844 Get_First_Interp (P, I, It);
5846 Search : while Present (It.Typ) loop
5847 if Is_Access_Type (It.Typ) then
5848 T := Designated_Type (It.Typ);
5849 else
5850 T := It.Typ;
5851 end if;
5853 if Is_Record_Type (T) then
5854 Comp := First_Entity (T);
5856 while Present (Comp) loop
5858 if Chars (Comp) = Chars (S)
5859 and then Covers (Etype (Comp), Typ)
5860 then
5861 if not Found then
5862 Found := True;
5863 I1 := I;
5864 It1 := It;
5865 Comp1 := Comp;
5867 else
5868 It := Disambiguate (P, I1, I, Any_Type);
5870 if It = No_Interp then
5871 Error_Msg_N
5872 ("ambiguous prefix for selected component", N);
5873 Set_Etype (N, Typ);
5874 return;
5876 else
5877 It1 := It;
5879 if Scope (Comp1) /= It1.Typ then
5881 -- Resolution chooses the new interpretation.
5882 -- Find the component with the right name.
5884 Comp1 := First_Entity (It1.Typ);
5886 while Present (Comp1)
5887 and then Chars (Comp1) /= Chars (S)
5888 loop
5889 Comp1 := Next_Entity (Comp1);
5890 end loop;
5891 end if;
5893 exit Search;
5894 end if;
5895 end if;
5896 end if;
5898 Comp := Next_Entity (Comp);
5899 end loop;
5901 end if;
5903 Get_Next_Interp (I, It);
5904 end loop Search;
5906 Resolve (P, It1.Typ);
5907 Set_Etype (N, Typ);
5908 Set_Entity (S, Comp1);
5910 else
5911 -- Resolve prefix with its type
5913 Resolve (P, T);
5914 end if;
5916 -- If prefix is an access type, the node will be transformed into
5917 -- an explicit dereference during expansion. The type of the node
5918 -- is the designated type of that of the prefix.
5920 if Is_Access_Type (Etype (P)) then
5921 T := Designated_Type (Etype (P));
5922 else
5923 T := Etype (P);
5924 end if;
5926 if Has_Discriminants (T)
5927 and then (Ekind (Entity (S)) = E_Component
5928 or else
5929 Ekind (Entity (S)) = E_Discriminant)
5930 and then Present (Original_Record_Component (Entity (S)))
5931 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
5932 and then Present (Discriminant_Checking_Func
5933 (Original_Record_Component (Entity (S))))
5934 and then not Discriminant_Checks_Suppressed (T)
5935 and then not Init_Component
5936 then
5937 Set_Do_Discriminant_Check (N);
5938 end if;
5940 if Ekind (Entity (S)) = E_Void then
5941 Error_Msg_N ("premature use of component", S);
5942 end if;
5944 -- If the prefix is a record conversion, this may be a renamed
5945 -- discriminant whose bounds differ from those of the original
5946 -- one, so we must ensure that a range check is performed.
5948 if Nkind (P) = N_Type_Conversion
5949 and then Ekind (Entity (S)) = E_Discriminant
5950 and then Is_Discrete_Type (Typ)
5951 then
5952 Set_Etype (N, Base_Type (Typ));
5953 end if;
5955 -- Note: No Eval processing is required, because the prefix is of a
5956 -- record type, or protected type, and neither can possibly be static.
5958 end Resolve_Selected_Component;
5960 -------------------
5961 -- Resolve_Shift --
5962 -------------------
5964 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
5965 B_Typ : constant Entity_Id := Base_Type (Typ);
5966 L : constant Node_Id := Left_Opnd (N);
5967 R : constant Node_Id := Right_Opnd (N);
5969 begin
5970 -- We do the resolution using the base type, because intermediate values
5971 -- in expressions always are of the base type, not a subtype of it.
5973 Resolve (L, B_Typ);
5974 Resolve (R, Standard_Natural);
5976 Check_Unset_Reference (L);
5977 Check_Unset_Reference (R);
5979 Set_Etype (N, B_Typ);
5980 Generate_Operator_Reference (N, B_Typ);
5981 Eval_Shift (N);
5982 end Resolve_Shift;
5984 ---------------------------
5985 -- Resolve_Short_Circuit --
5986 ---------------------------
5988 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
5989 B_Typ : constant Entity_Id := Base_Type (Typ);
5990 L : constant Node_Id := Left_Opnd (N);
5991 R : constant Node_Id := Right_Opnd (N);
5993 begin
5994 Resolve (L, B_Typ);
5995 Resolve (R, B_Typ);
5997 Check_Unset_Reference (L);
5998 Check_Unset_Reference (R);
6000 Set_Etype (N, B_Typ);
6001 Eval_Short_Circuit (N);
6002 end Resolve_Short_Circuit;
6004 -------------------
6005 -- Resolve_Slice --
6006 -------------------
6008 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
6009 Name : constant Node_Id := Prefix (N);
6010 Drange : constant Node_Id := Discrete_Range (N);
6011 Array_Type : Entity_Id := Empty;
6012 Index : Node_Id;
6014 begin
6015 if Is_Overloaded (Name) then
6017 -- Use the context type to select the prefix that yields the
6018 -- correct array type.
6020 declare
6021 I : Interp_Index;
6022 I1 : Interp_Index := 0;
6023 It : Interp;
6024 P : constant Node_Id := Prefix (N);
6025 Found : Boolean := False;
6027 begin
6028 Get_First_Interp (P, I, It);
6030 while Present (It.Typ) loop
6032 if (Is_Array_Type (It.Typ)
6033 and then Covers (Typ, It.Typ))
6034 or else (Is_Access_Type (It.Typ)
6035 and then Is_Array_Type (Designated_Type (It.Typ))
6036 and then Covers (Typ, Designated_Type (It.Typ)))
6037 then
6038 if Found then
6039 It := Disambiguate (P, I1, I, Any_Type);
6041 if It = No_Interp then
6042 Error_Msg_N ("ambiguous prefix for slicing", N);
6043 Set_Etype (N, Typ);
6044 return;
6045 else
6046 Found := True;
6047 Array_Type := It.Typ;
6048 I1 := I;
6049 end if;
6050 else
6051 Found := True;
6052 Array_Type := It.Typ;
6053 I1 := I;
6054 end if;
6055 end if;
6057 Get_Next_Interp (I, It);
6058 end loop;
6059 end;
6061 else
6062 Array_Type := Etype (Name);
6063 end if;
6065 Resolve (Name, Array_Type);
6067 if Is_Access_Type (Array_Type) then
6068 Apply_Access_Check (N);
6069 Array_Type := Designated_Type (Array_Type);
6071 -- If the prefix is an access to an unconstrained array, we must
6072 -- use the actual subtype of the object to perform the index checks.
6073 -- The object denoted by the prefix is implicit in the node, so we
6074 -- build an explicit representation for it in order to compute the
6075 -- actual subtype.
6077 if not Is_Constrained (Array_Type) then
6078 Remove_Side_Effects (Prefix (N));
6080 declare
6081 Obj : constant Node_Id :=
6082 Make_Explicit_Dereference (Sloc (N),
6083 Prefix => New_Copy_Tree (Prefix (N)));
6084 begin
6085 Set_Etype (Obj, Array_Type);
6086 Set_Parent (Obj, Parent (N));
6087 Array_Type := Get_Actual_Subtype (Obj);
6088 end;
6089 end if;
6091 elsif Is_Entity_Name (Name)
6092 or else (Nkind (Name) = N_Function_Call
6093 and then not Is_Constrained (Etype (Name)))
6094 then
6095 Array_Type := Get_Actual_Subtype (Name);
6096 end if;
6098 -- If name was overloaded, set slice type correctly now
6100 Set_Etype (N, Array_Type);
6102 -- If the range is specified by a subtype mark, no resolution
6103 -- is necessary. Else resolve the bounds, and apply needed checks.
6105 if not Is_Entity_Name (Drange) then
6106 Index := First_Index (Array_Type);
6107 Resolve (Drange, Base_Type (Etype (Index)));
6109 if Nkind (Drange) = N_Range then
6110 Apply_Range_Check (Drange, Etype (Index));
6111 end if;
6112 end if;
6114 Set_Slice_Subtype (N);
6115 Eval_Slice (N);
6116 end Resolve_Slice;
6118 ----------------------------
6119 -- Resolve_String_Literal --
6120 ----------------------------
6122 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
6123 C_Typ : constant Entity_Id := Component_Type (Typ);
6124 R_Typ : constant Entity_Id := Root_Type (C_Typ);
6125 Loc : constant Source_Ptr := Sloc (N);
6126 Str : constant String_Id := Strval (N);
6127 Strlen : constant Nat := String_Length (Str);
6128 Subtype_Id : Entity_Id;
6129 Need_Check : Boolean;
6131 begin
6132 -- For a string appearing in a concatenation, defer creation of the
6133 -- string_literal_subtype until the end of the resolution of the
6134 -- concatenation, because the literal may be constant-folded away.
6135 -- This is a useful optimization for long concatenation expressions.
6137 -- If the string is an aggregate built for a single character (which
6138 -- happens in a non-static context) or a is null string to which special
6139 -- checks may apply, we build the subtype. Wide strings must also get
6140 -- a string subtype if they come from a one character aggregate. Strings
6141 -- generated by attributes might be static, but it is often hard to
6142 -- determine whether the enclosing context is static, so we generate
6143 -- subtypes for them as well, thus losing some rarer optimizations ???
6144 -- Same for strings that come from a static conversion.
6146 Need_Check :=
6147 (Strlen = 0 and then Typ /= Standard_String)
6148 or else Nkind (Parent (N)) /= N_Op_Concat
6149 or else (N /= Left_Opnd (Parent (N))
6150 and then N /= Right_Opnd (Parent (N)))
6151 or else ((Typ = Standard_Wide_String
6152 or else Typ = Standard_Wide_Wide_String)
6153 and then Nkind (Original_Node (N)) /= N_String_Literal);
6155 -- If the resolving type is itself a string literal subtype, we
6156 -- can just reuse it, since there is no point in creating another.
6158 if Ekind (Typ) = E_String_Literal_Subtype then
6159 Subtype_Id := Typ;
6161 elsif Nkind (Parent (N)) = N_Op_Concat
6162 and then not Need_Check
6163 and then Nkind (Original_Node (N)) /= N_Character_Literal
6164 and then Nkind (Original_Node (N)) /= N_Attribute_Reference
6165 and then Nkind (Original_Node (N)) /= N_Qualified_Expression
6166 and then Nkind (Original_Node (N)) /= N_Type_Conversion
6167 then
6168 Subtype_Id := Typ;
6170 -- Otherwise we must create a string literal subtype. Note that the
6171 -- whole idea of string literal subtypes is simply to avoid the need
6172 -- for building a full fledged array subtype for each literal.
6173 else
6174 Set_String_Literal_Subtype (N, Typ);
6175 Subtype_Id := Etype (N);
6176 end if;
6178 if Nkind (Parent (N)) /= N_Op_Concat
6179 or else Need_Check
6180 then
6181 Set_Etype (N, Subtype_Id);
6182 Eval_String_Literal (N);
6183 end if;
6185 if Is_Limited_Composite (Typ)
6186 or else Is_Private_Composite (Typ)
6187 then
6188 Error_Msg_N ("string literal not available for private array", N);
6189 Set_Etype (N, Any_Type);
6190 return;
6191 end if;
6193 -- The validity of a null string has been checked in the
6194 -- call to Eval_String_Literal.
6196 if Strlen = 0 then
6197 return;
6199 -- Always accept string literal with component type Any_Character,
6200 -- which occurs in error situations and in comparisons of literals,
6201 -- both of which should accept all literals.
6203 elsif R_Typ = Any_Character then
6204 return;
6206 -- If the type is bit-packed, then we always tranform the string
6207 -- literal into a full fledged aggregate.
6209 elsif Is_Bit_Packed_Array (Typ) then
6210 null;
6212 -- Deal with cases of Wide_Wide_String, Wide_String, and String
6214 else
6215 -- For Standard.Wide_Wide_String, or any other type whose component
6216 -- type is Standard.Wide_Wide_Character, we know that all the
6217 -- characters in the string must be acceptable, since the parser
6218 -- accepted the characters as valid character literals.
6220 if R_Typ = Standard_Wide_Wide_Character then
6221 null;
6223 -- For the case of Standard.String, or any other type whose
6224 -- component type is Standard.Character, we must make sure that
6225 -- there are no wide characters in the string, i.e. that it is
6226 -- entirely composed of characters in range of type Character.
6228 -- If the string literal is the result of a static concatenation,
6229 -- the test has already been performed on the components, and need
6230 -- not be repeated.
6232 elsif R_Typ = Standard_Character
6233 and then Nkind (Original_Node (N)) /= N_Op_Concat
6234 then
6235 for J in 1 .. Strlen loop
6236 if not In_Character_Range (Get_String_Char (Str, J)) then
6238 -- If we are out of range, post error. This is one of the
6239 -- very few places that we place the flag in the middle of
6240 -- a token, right under the offending wide character.
6242 Error_Msg
6243 ("literal out of range of type Standard.Character",
6244 Source_Ptr (Int (Loc) + J));
6245 return;
6246 end if;
6247 end loop;
6249 -- For the case of Standard.Wide_String, or any other type whose
6250 -- component type is Standard.Wide_Character, we must make sure that
6251 -- there are no wide characters in the string, i.e. that it is
6252 -- entirely composed of characters in range of type Wide_Character.
6254 -- If the string literal is the result of a static concatenation,
6255 -- the test has already been performed on the components, and need
6256 -- not be repeated.
6258 elsif R_Typ = Standard_Wide_Character
6259 and then Nkind (Original_Node (N)) /= N_Op_Concat
6260 then
6261 for J in 1 .. Strlen loop
6262 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
6264 -- If we are out of range, post error. This is one of the
6265 -- very few places that we place the flag in the middle of
6266 -- a token, right under the offending wide character.
6268 -- This is not quite right, because characters in general
6269 -- will take more than one character position ???
6271 Error_Msg
6272 ("literal out of range of type Standard.Wide_Character",
6273 Source_Ptr (Int (Loc) + J));
6274 return;
6275 end if;
6276 end loop;
6278 -- If the root type is not a standard character, then we will convert
6279 -- the string into an aggregate and will let the aggregate code do
6280 -- the checking. Standard Wide_Wide_Character is also OK here.
6282 else
6283 null;
6284 end if;
6286 -- See if the component type of the array corresponding to the
6287 -- string has compile time known bounds. If yes we can directly
6288 -- check whether the evaluation of the string will raise constraint
6289 -- error. Otherwise we need to transform the string literal into
6290 -- the corresponding character aggregate and let the aggregate
6291 -- code do the checking.
6293 if R_Typ = Standard_Character
6294 or else R_Typ = Standard_Wide_Character
6295 or else R_Typ = Standard_Wide_Wide_Character
6296 then
6297 -- Check for the case of full range, where we are definitely OK
6299 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
6300 return;
6301 end if;
6303 -- Here the range is not the complete base type range, so check
6305 declare
6306 Comp_Typ_Lo : constant Node_Id :=
6307 Type_Low_Bound (Component_Type (Typ));
6308 Comp_Typ_Hi : constant Node_Id :=
6309 Type_High_Bound (Component_Type (Typ));
6311 Char_Val : Uint;
6313 begin
6314 if Compile_Time_Known_Value (Comp_Typ_Lo)
6315 and then Compile_Time_Known_Value (Comp_Typ_Hi)
6316 then
6317 for J in 1 .. Strlen loop
6318 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
6320 if Char_Val < Expr_Value (Comp_Typ_Lo)
6321 or else Char_Val > Expr_Value (Comp_Typ_Hi)
6322 then
6323 Apply_Compile_Time_Constraint_Error
6324 (N, "character out of range?", CE_Range_Check_Failed,
6325 Loc => Source_Ptr (Int (Loc) + J));
6326 end if;
6327 end loop;
6329 return;
6330 end if;
6331 end;
6332 end if;
6333 end if;
6335 -- If we got here we meed to transform the string literal into the
6336 -- equivalent qualified positional array aggregate. This is rather
6337 -- heavy artillery for this situation, but it is hard work to avoid.
6339 declare
6340 Lits : constant List_Id := New_List;
6341 P : Source_Ptr := Loc + 1;
6342 C : Char_Code;
6344 begin
6345 -- Build the character literals, we give them source locations
6346 -- that correspond to the string positions, which is a bit tricky
6347 -- given the possible presence of wide character escape sequences.
6349 for J in 1 .. Strlen loop
6350 C := Get_String_Char (Str, J);
6351 Set_Character_Literal_Name (C);
6353 Append_To (Lits,
6354 Make_Character_Literal (P,
6355 Chars => Name_Find,
6356 Char_Literal_Value => UI_From_CC (C)));
6358 if In_Character_Range (C) then
6359 P := P + 1;
6361 -- Should we have a call to Skip_Wide here ???
6362 -- ??? else
6363 -- Skip_Wide (P);
6365 end if;
6366 end loop;
6368 Rewrite (N,
6369 Make_Qualified_Expression (Loc,
6370 Subtype_Mark => New_Reference_To (Typ, Loc),
6371 Expression =>
6372 Make_Aggregate (Loc, Expressions => Lits)));
6374 Analyze_And_Resolve (N, Typ);
6375 end;
6376 end Resolve_String_Literal;
6378 -----------------------------
6379 -- Resolve_Subprogram_Info --
6380 -----------------------------
6382 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
6383 begin
6384 Set_Etype (N, Typ);
6385 end Resolve_Subprogram_Info;
6387 -----------------------------
6388 -- Resolve_Type_Conversion --
6389 -----------------------------
6391 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
6392 Target_Type : constant Entity_Id := Etype (N);
6393 Conv_OK : constant Boolean := Conversion_OK (N);
6394 Operand : Node_Id;
6395 Opnd_Type : Entity_Id;
6396 Rop : Node_Id;
6397 Orig_N : Node_Id;
6398 Orig_T : Node_Id;
6400 begin
6401 Operand := Expression (N);
6403 if not Conv_OK
6404 and then not Valid_Conversion (N, Target_Type, Operand)
6405 then
6406 return;
6407 end if;
6409 if Etype (Operand) = Any_Fixed then
6411 -- Mixed-mode operation involving a literal. Context must be a fixed
6412 -- type which is applied to the literal subsequently.
6414 if Is_Fixed_Point_Type (Typ) then
6415 Set_Etype (Operand, Universal_Real);
6417 elsif Is_Numeric_Type (Typ)
6418 and then (Nkind (Operand) = N_Op_Multiply
6419 or else Nkind (Operand) = N_Op_Divide)
6420 and then (Etype (Right_Opnd (Operand)) = Universal_Real
6421 or else Etype (Left_Opnd (Operand)) = Universal_Real)
6422 then
6423 -- Return if expression is ambiguous
6425 if Unique_Fixed_Point_Type (N) = Any_Type then
6426 return;
6428 -- If nothing else, the available fixed type is Duration
6430 else
6431 Set_Etype (Operand, Standard_Duration);
6432 end if;
6434 -- Resolve the real operand with largest available precision
6435 if Etype (Right_Opnd (Operand)) = Universal_Real then
6436 Rop := New_Copy_Tree (Right_Opnd (Operand));
6437 else
6438 Rop := New_Copy_Tree (Left_Opnd (Operand));
6439 end if;
6441 Resolve (Rop, Standard_Long_Long_Float);
6443 -- If the operand is a literal (it could be a non-static and
6444 -- illegal exponentiation) check whether the use of Duration
6445 -- is potentially inaccurate.
6447 if Nkind (Rop) = N_Real_Literal
6448 and then Realval (Rop) /= Ureal_0
6449 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
6450 then
6451 Error_Msg_N ("universal real operand can only be interpreted?",
6452 Rop);
6453 Error_Msg_N ("\as Duration, and will lose precision?", Rop);
6454 end if;
6456 elsif Is_Numeric_Type (Typ)
6457 and then Nkind (Operand) in N_Op
6458 and then Unique_Fixed_Point_Type (N) /= Any_Type
6459 then
6460 Set_Etype (Operand, Standard_Duration);
6462 else
6463 Error_Msg_N ("invalid context for mixed mode operation", N);
6464 Set_Etype (Operand, Any_Type);
6465 return;
6466 end if;
6467 end if;
6469 Opnd_Type := Etype (Operand);
6470 Resolve (Operand);
6472 -- Note: we do the Eval_Type_Conversion call before applying the
6473 -- required checks for a subtype conversion. This is important,
6474 -- since both are prepared under certain circumstances to change
6475 -- the type conversion to a constraint error node, but in the case
6476 -- of Eval_Type_Conversion this may reflect an illegality in the
6477 -- static case, and we would miss the illegality (getting only a
6478 -- warning message), if we applied the type conversion checks first.
6480 Eval_Type_Conversion (N);
6482 -- If after evaluation, we still have a type conversion, then we
6483 -- may need to apply checks required for a subtype conversion.
6485 -- Skip these type conversion checks if universal fixed operands
6486 -- operands involved, since range checks are handled separately for
6487 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
6489 if Nkind (N) = N_Type_Conversion
6490 and then not Is_Generic_Type (Root_Type (Target_Type))
6491 and then Target_Type /= Universal_Fixed
6492 and then Opnd_Type /= Universal_Fixed
6493 then
6494 Apply_Type_Conversion_Checks (N);
6495 end if;
6497 -- Issue warning for conversion of simple object to its own type
6498 -- We have to test the original nodes, since they may have been
6499 -- rewritten by various optimizations.
6501 Orig_N := Original_Node (N);
6503 if Warn_On_Redundant_Constructs
6504 and then Comes_From_Source (Orig_N)
6505 and then Nkind (Orig_N) = N_Type_Conversion
6506 and then not In_Instance
6507 then
6508 Orig_N := Original_Node (Expression (Orig_N));
6509 Orig_T := Target_Type;
6511 -- If the node is part of a larger expression, the Target_Type
6512 -- may not be the original type of the node if the context is a
6513 -- condition. Recover original type to see if conversion is needed.
6515 if Is_Boolean_Type (Orig_T)
6516 and then Nkind (Parent (N)) in N_Op
6517 then
6518 Orig_T := Etype (Parent (N));
6519 end if;
6521 if Is_Entity_Name (Orig_N)
6522 and then Etype (Entity (Orig_N)) = Orig_T
6523 then
6524 Error_Msg_NE
6525 ("?useless conversion, & has this type", N, Entity (Orig_N));
6526 end if;
6527 end if;
6528 end Resolve_Type_Conversion;
6530 ----------------------
6531 -- Resolve_Unary_Op --
6532 ----------------------
6534 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
6535 B_Typ : constant Entity_Id := Base_Type (Typ);
6536 R : constant Node_Id := Right_Opnd (N);
6537 OK : Boolean;
6538 Lo : Uint;
6539 Hi : Uint;
6541 begin
6542 -- Generate warning for expressions like abs (x mod 2)
6544 if Warn_On_Redundant_Constructs
6545 and then Nkind (N) = N_Op_Abs
6546 then
6547 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
6549 if OK and then Hi >= Lo and then Lo >= 0 then
6550 Error_Msg_N
6551 ("?abs applied to known non-negative value has no effect", N);
6552 end if;
6553 end if;
6555 -- Generate warning for expressions like -5 mod 3
6557 if Paren_Count (N) = 0
6558 and then Nkind (N) = N_Op_Minus
6559 and then Nkind (Right_Opnd (N)) = N_Op_Mod
6560 and then Comes_From_Source (N)
6561 then
6562 Error_Msg_N
6563 ("?unary minus expression should be parenthesized here", N);
6564 end if;
6566 if Comes_From_Source (N)
6567 and then Ekind (Entity (N)) = E_Function
6568 and then Is_Imported (Entity (N))
6569 and then Is_Intrinsic_Subprogram (Entity (N))
6570 then
6571 Resolve_Intrinsic_Unary_Operator (N, Typ);
6572 return;
6573 end if;
6575 if Etype (R) = Universal_Integer
6576 or else Etype (R) = Universal_Real
6577 then
6578 Check_For_Visible_Operator (N, B_Typ);
6579 end if;
6581 Set_Etype (N, B_Typ);
6582 Resolve (R, B_Typ);
6584 Check_Unset_Reference (R);
6585 Generate_Operator_Reference (N, B_Typ);
6586 Eval_Unary_Op (N);
6588 -- Set overflow checking bit. Much cleverer code needed here eventually
6589 -- and perhaps the Resolve routines should be separated for the various
6590 -- arithmetic operations, since they will need different processing ???
6592 if Nkind (N) in N_Op then
6593 if not Overflow_Checks_Suppressed (Etype (N)) then
6594 Enable_Overflow_Check (N);
6595 end if;
6596 end if;
6597 end Resolve_Unary_Op;
6599 ----------------------------------
6600 -- Resolve_Unchecked_Expression --
6601 ----------------------------------
6603 procedure Resolve_Unchecked_Expression
6604 (N : Node_Id;
6605 Typ : Entity_Id)
6607 begin
6608 Resolve (Expression (N), Typ, Suppress => All_Checks);
6609 Set_Etype (N, Typ);
6610 end Resolve_Unchecked_Expression;
6612 ---------------------------------------
6613 -- Resolve_Unchecked_Type_Conversion --
6614 ---------------------------------------
6616 procedure Resolve_Unchecked_Type_Conversion
6617 (N : Node_Id;
6618 Typ : Entity_Id)
6620 pragma Warnings (Off, Typ);
6622 Operand : constant Node_Id := Expression (N);
6623 Opnd_Type : constant Entity_Id := Etype (Operand);
6625 begin
6626 -- Resolve operand using its own type
6628 Resolve (Operand, Opnd_Type);
6629 Eval_Unchecked_Conversion (N);
6631 end Resolve_Unchecked_Type_Conversion;
6633 ------------------------------
6634 -- Rewrite_Operator_As_Call --
6635 ------------------------------
6637 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
6638 Loc : constant Source_Ptr := Sloc (N);
6639 Actuals : constant List_Id := New_List;
6640 New_N : Node_Id;
6642 begin
6643 if Nkind (N) in N_Binary_Op then
6644 Append (Left_Opnd (N), Actuals);
6645 end if;
6647 Append (Right_Opnd (N), Actuals);
6649 New_N :=
6650 Make_Function_Call (Sloc => Loc,
6651 Name => New_Occurrence_Of (Nam, Loc),
6652 Parameter_Associations => Actuals);
6654 Preserve_Comes_From_Source (New_N, N);
6655 Preserve_Comes_From_Source (Name (New_N), N);
6656 Rewrite (N, New_N);
6657 Set_Etype (N, Etype (Nam));
6658 end Rewrite_Operator_As_Call;
6660 ------------------------------
6661 -- Rewrite_Renamed_Operator --
6662 ------------------------------
6664 procedure Rewrite_Renamed_Operator
6665 (N : Node_Id;
6666 Op : Entity_Id;
6667 Typ : Entity_Id)
6669 Nam : constant Name_Id := Chars (Op);
6670 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
6671 Op_Node : Node_Id;
6673 begin
6674 -- Rewrite the operator node using the real operator, not its
6675 -- renaming. Exclude user-defined intrinsic operations of the same
6676 -- name, which are treated separately and rewritten as calls.
6678 if Ekind (Op) /= E_Function
6679 or else Chars (N) /= Nam
6680 then
6681 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
6682 Set_Chars (Op_Node, Nam);
6683 Set_Etype (Op_Node, Etype (N));
6684 Set_Entity (Op_Node, Op);
6685 Set_Right_Opnd (Op_Node, Right_Opnd (N));
6687 -- Indicate that both the original entity and its renaming
6688 -- are referenced at this point.
6690 Generate_Reference (Entity (N), N);
6691 Generate_Reference (Op, N);
6693 if Is_Binary then
6694 Set_Left_Opnd (Op_Node, Left_Opnd (N));
6695 end if;
6697 Rewrite (N, Op_Node);
6699 -- If the context type is private, add the appropriate conversions
6700 -- so that the operator is applied to the full view. This is done
6701 -- in the routines that resolve intrinsic operators,
6703 if Is_Intrinsic_Subprogram (Op)
6704 and then Is_Private_Type (Typ)
6705 then
6706 case Nkind (N) is
6707 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
6708 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
6709 Resolve_Intrinsic_Operator (N, Typ);
6711 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
6712 Resolve_Intrinsic_Unary_Operator (N, Typ);
6714 when others =>
6715 Resolve (N, Typ);
6716 end case;
6717 end if;
6719 elsif Ekind (Op) = E_Function
6720 and then Is_Intrinsic_Subprogram (Op)
6721 then
6722 -- Operator renames a user-defined operator of the same name. Use
6723 -- the original operator in the node, which is the one that gigi
6724 -- knows about.
6726 Set_Entity (N, Op);
6727 Set_Is_Overloaded (N, False);
6728 end if;
6729 end Rewrite_Renamed_Operator;
6731 -----------------------
6732 -- Set_Slice_Subtype --
6733 -----------------------
6735 -- Build an implicit subtype declaration to represent the type delivered
6736 -- by the slice. This is an abbreviated version of an array subtype. We
6737 -- define an index subtype for the slice, using either the subtype name
6738 -- or the discrete range of the slice. To be consistent with index usage
6739 -- elsewhere, we create a list header to hold the single index. This list
6740 -- is not otherwise attached to the syntax tree.
6742 procedure Set_Slice_Subtype (N : Node_Id) is
6743 Loc : constant Source_Ptr := Sloc (N);
6744 Index_List : constant List_Id := New_List;
6745 Index : Node_Id;
6746 Index_Subtype : Entity_Id;
6747 Index_Type : Entity_Id;
6748 Slice_Subtype : Entity_Id;
6749 Drange : constant Node_Id := Discrete_Range (N);
6751 begin
6752 if Is_Entity_Name (Drange) then
6753 Index_Subtype := Entity (Drange);
6755 else
6756 -- We force the evaluation of a range. This is definitely needed in
6757 -- the renamed case, and seems safer to do unconditionally. Note in
6758 -- any case that since we will create and insert an Itype referring
6759 -- to this range, we must make sure any side effect removal actions
6760 -- are inserted before the Itype definition.
6762 if Nkind (Drange) = N_Range then
6763 Force_Evaluation (Low_Bound (Drange));
6764 Force_Evaluation (High_Bound (Drange));
6765 end if;
6767 Index_Type := Base_Type (Etype (Drange));
6769 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
6771 Set_Scalar_Range (Index_Subtype, Drange);
6772 Set_Etype (Index_Subtype, Index_Type);
6773 Set_Size_Info (Index_Subtype, Index_Type);
6774 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
6775 end if;
6777 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
6779 Index := New_Occurrence_Of (Index_Subtype, Loc);
6780 Set_Etype (Index, Index_Subtype);
6781 Append (Index, Index_List);
6783 Set_First_Index (Slice_Subtype, Index);
6784 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
6785 Set_Is_Constrained (Slice_Subtype, True);
6786 Init_Size_Align (Slice_Subtype);
6788 Check_Compile_Time_Size (Slice_Subtype);
6790 -- The Etype of the existing Slice node is reset to this slice
6791 -- subtype. Its bounds are obtained from its first index.
6793 Set_Etype (N, Slice_Subtype);
6795 -- In the packed case, this must be immediately frozen
6797 -- Couldn't we always freeze here??? and if we did, then the above
6798 -- call to Check_Compile_Time_Size could be eliminated, which would
6799 -- be nice, because then that routine could be made private to Freeze.
6801 if Is_Packed (Slice_Subtype) and not In_Default_Expression then
6802 Freeze_Itype (Slice_Subtype, N);
6803 end if;
6805 end Set_Slice_Subtype;
6807 --------------------------------
6808 -- Set_String_Literal_Subtype --
6809 --------------------------------
6811 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
6812 Subtype_Id : Entity_Id;
6814 begin
6815 if Nkind (N) /= N_String_Literal then
6816 return;
6817 else
6818 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
6819 end if;
6821 Set_String_Literal_Length (Subtype_Id, UI_From_Int
6822 (String_Length (Strval (N))));
6823 Set_Etype (Subtype_Id, Base_Type (Typ));
6824 Set_Is_Constrained (Subtype_Id);
6826 -- The low bound is set from the low bound of the corresponding
6827 -- index type. Note that we do not store the high bound in the
6828 -- string literal subtype, but it can be deduced if necssary
6829 -- from the length and the low bound.
6831 Set_String_Literal_Low_Bound
6832 (Subtype_Id, Type_Low_Bound (Etype (First_Index (Typ))));
6834 Set_Etype (N, Subtype_Id);
6835 end Set_String_Literal_Subtype;
6837 -----------------------------
6838 -- Unique_Fixed_Point_Type --
6839 -----------------------------
6841 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
6842 T1 : Entity_Id := Empty;
6843 T2 : Entity_Id;
6844 Item : Node_Id;
6845 Scop : Entity_Id;
6847 procedure Fixed_Point_Error;
6848 -- If true ambiguity, give details
6850 -----------------------
6851 -- Fixed_Point_Error --
6852 -----------------------
6854 procedure Fixed_Point_Error is
6855 begin
6856 Error_Msg_N ("ambiguous universal_fixed_expression", N);
6857 Error_Msg_NE ("\possible interpretation as}", N, T1);
6858 Error_Msg_NE ("\possible interpretation as}", N, T2);
6859 end Fixed_Point_Error;
6861 -- Start of processing for Unique_Fixed_Point_Type
6863 begin
6864 -- The operations on Duration are visible, so Duration is always a
6865 -- possible interpretation.
6867 T1 := Standard_Duration;
6869 -- Look for fixed-point types in enclosing scopes
6871 Scop := Current_Scope;
6872 while Scop /= Standard_Standard loop
6873 T2 := First_Entity (Scop);
6875 while Present (T2) loop
6876 if Is_Fixed_Point_Type (T2)
6877 and then Current_Entity (T2) = T2
6878 and then Scope (Base_Type (T2)) = Scop
6879 then
6880 if Present (T1) then
6881 Fixed_Point_Error;
6882 return Any_Type;
6883 else
6884 T1 := T2;
6885 end if;
6886 end if;
6888 Next_Entity (T2);
6889 end loop;
6891 Scop := Scope (Scop);
6892 end loop;
6894 -- Look for visible fixed type declarations in the context
6896 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
6897 while Present (Item) loop
6898 if Nkind (Item) = N_With_Clause then
6899 Scop := Entity (Name (Item));
6900 T2 := First_Entity (Scop);
6902 while Present (T2) loop
6903 if Is_Fixed_Point_Type (T2)
6904 and then Scope (Base_Type (T2)) = Scop
6905 and then (Is_Potentially_Use_Visible (T2)
6906 or else In_Use (T2))
6907 then
6908 if Present (T1) then
6909 Fixed_Point_Error;
6910 return Any_Type;
6911 else
6912 T1 := T2;
6913 end if;
6914 end if;
6916 Next_Entity (T2);
6917 end loop;
6918 end if;
6920 Next (Item);
6921 end loop;
6923 if Nkind (N) = N_Real_Literal then
6924 Error_Msg_NE ("real literal interpreted as }?", N, T1);
6926 else
6927 Error_Msg_NE ("universal_fixed expression interpreted as }?", N, T1);
6928 end if;
6930 return T1;
6931 end Unique_Fixed_Point_Type;
6933 ----------------------
6934 -- Valid_Conversion --
6935 ----------------------
6937 function Valid_Conversion
6938 (N : Node_Id;
6939 Target : Entity_Id;
6940 Operand : Node_Id) return Boolean
6942 Target_Type : constant Entity_Id := Base_Type (Target);
6943 Opnd_Type : Entity_Id := Etype (Operand);
6945 function Conversion_Check
6946 (Valid : Boolean;
6947 Msg : String) return Boolean;
6948 -- Little routine to post Msg if Valid is False, returns Valid value
6950 function Valid_Tagged_Conversion
6951 (Target_Type : Entity_Id;
6952 Opnd_Type : Entity_Id) return Boolean;
6953 -- Specifically test for validity of tagged conversions
6955 ----------------------
6956 -- Conversion_Check --
6957 ----------------------
6959 function Conversion_Check
6960 (Valid : Boolean;
6961 Msg : String) return Boolean
6963 begin
6964 if not Valid then
6965 Error_Msg_N (Msg, Operand);
6966 end if;
6968 return Valid;
6969 end Conversion_Check;
6971 -----------------------------
6972 -- Valid_Tagged_Conversion --
6973 -----------------------------
6975 function Valid_Tagged_Conversion
6976 (Target_Type : Entity_Id;
6977 Opnd_Type : Entity_Id) return Boolean
6979 begin
6980 -- Upward conversions are allowed (RM 4.6(22))
6982 if Covers (Target_Type, Opnd_Type)
6983 or else Is_Ancestor (Target_Type, Opnd_Type)
6984 then
6985 return True;
6987 -- Downward conversion are allowed if the operand is class-wide
6988 -- (RM 4.6(23)).
6990 elsif Is_Class_Wide_Type (Opnd_Type)
6991 and then Covers (Opnd_Type, Target_Type)
6992 then
6993 return True;
6995 elsif Covers (Opnd_Type, Target_Type)
6996 or else Is_Ancestor (Opnd_Type, Target_Type)
6997 then
6998 return
6999 Conversion_Check (False,
7000 "downward conversion of tagged objects not allowed");
7001 else
7002 Error_Msg_NE
7003 ("invalid tagged conversion, not compatible with}",
7004 N, First_Subtype (Opnd_Type));
7005 return False;
7006 end if;
7007 end Valid_Tagged_Conversion;
7009 -- Start of processing for Valid_Conversion
7011 begin
7012 Check_Parameterless_Call (Operand);
7014 if Is_Overloaded (Operand) then
7015 declare
7016 I : Interp_Index;
7017 I1 : Interp_Index;
7018 It : Interp;
7019 It1 : Interp;
7020 N1 : Entity_Id;
7022 begin
7023 -- Remove procedure calls, which syntactically cannot appear
7024 -- in this context, but which cannot be removed by type checking,
7025 -- because the context does not impose a type.
7027 Get_First_Interp (Operand, I, It);
7029 while Present (It.Typ) loop
7031 if It.Typ = Standard_Void_Type then
7032 Remove_Interp (I);
7033 end if;
7035 Get_Next_Interp (I, It);
7036 end loop;
7038 Get_First_Interp (Operand, I, It);
7039 I1 := I;
7040 It1 := It;
7042 if No (It.Typ) then
7043 Error_Msg_N ("illegal operand in conversion", Operand);
7044 return False;
7045 end if;
7047 Get_Next_Interp (I, It);
7049 if Present (It.Typ) then
7050 N1 := It1.Nam;
7051 It1 := Disambiguate (Operand, I1, I, Any_Type);
7053 if It1 = No_Interp then
7054 Error_Msg_N ("ambiguous operand in conversion", Operand);
7056 Error_Msg_Sloc := Sloc (It.Nam);
7057 Error_Msg_N ("possible interpretation#!", Operand);
7059 Error_Msg_Sloc := Sloc (N1);
7060 Error_Msg_N ("possible interpretation#!", Operand);
7062 return False;
7063 end if;
7064 end if;
7066 Set_Etype (Operand, It1.Typ);
7067 Opnd_Type := It1.Typ;
7068 end;
7069 end if;
7071 if Chars (Current_Scope) = Name_Unchecked_Conversion then
7073 -- This check is dubious, what if there were a user defined
7074 -- scope whose name was Unchecked_Conversion ???
7076 return True;
7078 elsif Is_Numeric_Type (Target_Type) then
7079 if Opnd_Type = Universal_Fixed then
7080 return True;
7082 elsif (In_Instance or else In_Inlined_Body)
7083 and then not Comes_From_Source (N)
7084 then
7085 return True;
7087 else
7088 return Conversion_Check (Is_Numeric_Type (Opnd_Type),
7089 "illegal operand for numeric conversion");
7090 end if;
7092 elsif Is_Array_Type (Target_Type) then
7093 if not Is_Array_Type (Opnd_Type)
7094 or else Opnd_Type = Any_Composite
7095 or else Opnd_Type = Any_String
7096 then
7097 Error_Msg_N
7098 ("illegal operand for array conversion", Operand);
7099 return False;
7101 elsif Number_Dimensions (Target_Type) /=
7102 Number_Dimensions (Opnd_Type)
7103 then
7104 Error_Msg_N
7105 ("incompatible number of dimensions for conversion", Operand);
7106 return False;
7108 else
7109 declare
7110 Target_Index : Node_Id := First_Index (Target_Type);
7111 Opnd_Index : Node_Id := First_Index (Opnd_Type);
7113 Target_Index_Type : Entity_Id;
7114 Opnd_Index_Type : Entity_Id;
7116 Target_Comp_Type : constant Entity_Id :=
7117 Component_Type (Target_Type);
7118 Opnd_Comp_Type : constant Entity_Id :=
7119 Component_Type (Opnd_Type);
7121 begin
7122 while Present (Target_Index) and then Present (Opnd_Index) loop
7123 Target_Index_Type := Etype (Target_Index);
7124 Opnd_Index_Type := Etype (Opnd_Index);
7126 if not (Is_Integer_Type (Target_Index_Type)
7127 and then Is_Integer_Type (Opnd_Index_Type))
7128 and then (Root_Type (Target_Index_Type)
7129 /= Root_Type (Opnd_Index_Type))
7130 then
7131 Error_Msg_N
7132 ("incompatible index types for array conversion",
7133 Operand);
7134 return False;
7135 end if;
7137 Next_Index (Target_Index);
7138 Next_Index (Opnd_Index);
7139 end loop;
7141 if Base_Type (Target_Comp_Type) /=
7142 Base_Type (Opnd_Comp_Type)
7143 then
7144 Error_Msg_N
7145 ("incompatible component types for array conversion",
7146 Operand);
7147 return False;
7149 elsif
7150 Is_Constrained (Target_Comp_Type)
7151 /= Is_Constrained (Opnd_Comp_Type)
7152 or else not Subtypes_Statically_Match
7153 (Target_Comp_Type, Opnd_Comp_Type)
7154 then
7155 Error_Msg_N
7156 ("component subtypes must statically match", Operand);
7157 return False;
7159 end if;
7160 end;
7161 end if;
7163 return True;
7165 elsif (Ekind (Target_Type) = E_General_Access_Type
7166 or else Ekind (Target_Type) = E_Anonymous_Access_Type)
7167 and then
7168 Conversion_Check
7169 (Is_Access_Type (Opnd_Type)
7170 and then Ekind (Opnd_Type) /=
7171 E_Access_Subprogram_Type
7172 and then Ekind (Opnd_Type) /=
7173 E_Access_Protected_Subprogram_Type,
7174 "must be an access-to-object type")
7175 then
7176 if Is_Access_Constant (Opnd_Type)
7177 and then not Is_Access_Constant (Target_Type)
7178 then
7179 Error_Msg_N
7180 ("access-to-constant operand type not allowed", Operand);
7181 return False;
7182 end if;
7184 -- Check the static accessibility rule of 4.6(17). Note that
7185 -- the check is not enforced when within an instance body, since
7186 -- the RM requires such cases to be caught at run time.
7188 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
7189 if Type_Access_Level (Opnd_Type)
7190 > Type_Access_Level (Target_Type)
7191 then
7192 -- In an instance, this is a run-time check, but one we
7193 -- know will fail, so generate an appropriate warning.
7194 -- The raise will be generated by Expand_N_Type_Conversion.
7196 if In_Instance_Body then
7197 Error_Msg_N
7198 ("?cannot convert local pointer to non-local access type",
7199 Operand);
7200 Error_Msg_N
7201 ("?Program_Error will be raised at run time", Operand);
7203 else
7204 Error_Msg_N
7205 ("cannot convert local pointer to non-local access type",
7206 Operand);
7207 return False;
7208 end if;
7210 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type then
7212 -- When the operand is a selected access discriminant
7213 -- the check needs to be made against the level of the
7214 -- object denoted by the prefix of the selected name.
7215 -- (Object_Access_Level handles checking the prefix
7216 -- of the operand for this case.)
7218 if Nkind (Operand) = N_Selected_Component
7219 and then Object_Access_Level (Operand)
7220 > Type_Access_Level (Target_Type)
7221 then
7222 -- In an instance, this is a run-time check, but one we
7223 -- know will fail, so generate an appropriate warning.
7224 -- The raise will be generated by Expand_N_Type_Conversion.
7226 if In_Instance_Body then
7227 Error_Msg_N
7228 ("?cannot convert access discriminant to non-local" &
7229 " access type", Operand);
7230 Error_Msg_N
7231 ("?Program_Error will be raised at run time", Operand);
7233 else
7234 Error_Msg_N
7235 ("cannot convert access discriminant to non-local" &
7236 " access type", Operand);
7237 return False;
7238 end if;
7239 end if;
7241 -- The case of a reference to an access discriminant
7242 -- from within a type declaration (which will appear
7243 -- as a discriminal) is always illegal because the
7244 -- level of the discriminant is considered to be
7245 -- deeper than any (namable) access type.
7247 if Is_Entity_Name (Operand)
7248 and then (Ekind (Entity (Operand)) = E_In_Parameter
7249 or else Ekind (Entity (Operand)) = E_Constant)
7250 and then Present (Discriminal_Link (Entity (Operand)))
7251 then
7252 Error_Msg_N
7253 ("discriminant has deeper accessibility level than target",
7254 Operand);
7255 return False;
7256 end if;
7257 end if;
7258 end if;
7260 declare
7261 Target : constant Entity_Id := Designated_Type (Target_Type);
7262 Opnd : constant Entity_Id := Designated_Type (Opnd_Type);
7264 begin
7265 if Is_Tagged_Type (Target) then
7266 return Valid_Tagged_Conversion (Target, Opnd);
7268 else
7269 if Base_Type (Target) /= Base_Type (Opnd) then
7270 Error_Msg_NE
7271 ("target designated type not compatible with }",
7272 N, Base_Type (Opnd));
7273 return False;
7275 -- Ada 2005 AI-384: legality rule is symmetric in both
7276 -- designated types. The conversion is legal (with possible
7277 -- constraint check) if either designated type is
7278 -- unconstrained.
7280 elsif Subtypes_Statically_Match (Target, Opnd)
7281 or else
7282 (Has_Discriminants (Target)
7283 and then
7284 (not Is_Constrained (Opnd)
7285 or else not Is_Constrained (Target)))
7286 then
7287 return True;
7289 else
7290 Error_Msg_NE
7291 ("target designated subtype not compatible with }",
7292 N, Opnd);
7293 return False;
7294 end if;
7295 end if;
7296 end;
7298 elsif (Ekind (Target_Type) = E_Access_Subprogram_Type
7299 or else
7300 Ekind (Target_Type) = E_Anonymous_Access_Subprogram_Type)
7301 and then No (Corresponding_Remote_Type (Opnd_Type))
7302 and then Conversion_Check
7303 (Ekind (Base_Type (Opnd_Type)) = E_Access_Subprogram_Type,
7304 "illegal operand for access subprogram conversion")
7305 then
7306 -- Check that the designated types are subtype conformant
7308 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
7309 Old_Id => Designated_Type (Opnd_Type),
7310 Err_Loc => N);
7312 -- Check the static accessibility rule of 4.6(20)
7314 if Type_Access_Level (Opnd_Type) >
7315 Type_Access_Level (Target_Type)
7316 then
7317 Error_Msg_N
7318 ("operand type has deeper accessibility level than target",
7319 Operand);
7321 -- Check that if the operand type is declared in a generic body,
7322 -- then the target type must be declared within that same body
7323 -- (enforces last sentence of 4.6(20)).
7325 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
7326 declare
7327 O_Gen : constant Node_Id :=
7328 Enclosing_Generic_Body (Opnd_Type);
7330 T_Gen : Node_Id :=
7331 Enclosing_Generic_Body (Target_Type);
7333 begin
7334 while Present (T_Gen) and then T_Gen /= O_Gen loop
7335 T_Gen := Enclosing_Generic_Body (T_Gen);
7336 end loop;
7338 if T_Gen /= O_Gen then
7339 Error_Msg_N
7340 ("target type must be declared in same generic body"
7341 & " as operand type", N);
7342 end if;
7343 end;
7344 end if;
7346 return True;
7348 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
7349 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
7350 then
7351 -- It is valid to convert from one RAS type to another provided
7352 -- that their specification statically match.
7354 Check_Subtype_Conformant
7355 (New_Id =>
7356 Designated_Type (Corresponding_Remote_Type (Target_Type)),
7357 Old_Id =>
7358 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
7359 Err_Loc =>
7361 return True;
7363 elsif Is_Tagged_Type (Target_Type) then
7364 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
7366 -- Types derived from the same root type are convertible
7368 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
7369 return True;
7371 -- In an instance, there may be inconsistent views of the same
7372 -- type, or types derived from the same type.
7374 elsif In_Instance
7375 and then Underlying_Type (Target_Type) = Underlying_Type (Opnd_Type)
7376 then
7377 return True;
7379 -- Special check for common access type error case
7381 elsif Ekind (Target_Type) = E_Access_Type
7382 and then Is_Access_Type (Opnd_Type)
7383 then
7384 Error_Msg_N ("target type must be general access type!", N);
7385 Error_Msg_NE ("add ALL to }!", N, Target_Type);
7387 return False;
7389 else
7390 Error_Msg_NE ("invalid conversion, not compatible with }",
7391 N, Opnd_Type);
7393 return False;
7394 end if;
7395 end Valid_Conversion;
7397 end Sem_Res;