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