7a767a391794fc33f32c42eb94f0af0601d54511
[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-2008, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Lib; use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
47 with Opt; use Opt;
48 with Output; use Output;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elab; use Sem_Elab;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Intr; use Sem_Intr;
65 with Sem_Util; use Sem_Util;
66 with Sem_Type; use Sem_Type;
67 with Sem_Warn; use Sem_Warn;
68 with Sinfo; use Sinfo;
69 with Snames; use Snames;
70 with Stand; use Stand;
71 with Stringt; use Stringt;
72 with Style; use Style;
73 with Targparm; use Targparm;
74 with Tbuild; use Tbuild;
75 with Uintp; use Uintp;
76 with Urealp; use Urealp;
77
78 package body Sem_Res is
79
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
83
84 -- Second pass (top-down) type checking and overload resolution procedures
85 -- Typ is the type required by context. These procedures propagate the
86 -- type information recursively to the descendants of N. If the node
87 -- is not overloaded, its Etype is established in the first pass. If
88 -- overloaded, the Resolve routines set the correct type. For arith.
89 -- operators, the Etype is the base type of the context.
90
91 -- Note that Resolve_Attribute is separated off in Sem_Attr
92
93 procedure Check_Discriminant_Use (N : Node_Id);
94 -- Enforce the restrictions on the use of discriminants when constraining
95 -- a component of a discriminated type (record or concurrent type).
96
97 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
98 -- Given a node for an operator associated with type T, check that
99 -- the operator is visible. Operators all of whose operands are
100 -- universal must be checked for visibility during resolution
101 -- because their type is not determinable based on their operands.
102
103 procedure Check_Fully_Declared_Prefix
104 (Typ : Entity_Id;
105 Pref : Node_Id);
106 -- Check that the type of the prefix of a dereference is not incomplete
107
108 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
109 -- Given a call node, N, which is known to occur immediately within the
110 -- subprogram being called, determines whether it is a detectable case of
111 -- an infinite recursion, and if so, outputs appropriate messages. Returns
112 -- True if an infinite recursion is detected, and False otherwise.
113
114 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
115 -- If the type of the object being initialized uses the secondary stack
116 -- directly or indirectly, create a transient scope for the call to the
117 -- init proc. This is because we do not create transient scopes for the
118 -- initialization of individual components within the init proc itself.
119 -- Could be optimized away perhaps?
120
121 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
122 -- Determine whether E is an access type declared by an access
123 -- declaration, and not an (anonymous) allocator type.
124
125 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
126 -- Utility to check whether the name in the call is a predefined
127 -- operator, in which case the call is made into an operator node.
128 -- An instance of an intrinsic conversion operation may be given
129 -- an operator name, but is not treated like an operator.
130
131 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
132 -- If a default expression in entry call N depends on the discriminants
133 -- of the task, it must be replaced with a reference to the discriminant
134 -- of the task being called.
135
136 procedure Resolve_Op_Concat_Arg
137 (N : Node_Id;
138 Arg : Node_Id;
139 Typ : Entity_Id;
140 Is_Comp : Boolean);
141 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
142 -- concatenation operator. The operand is either of the array type or of
143 -- the component type. If the operand is an aggregate, and the component
144 -- type is composite, this is ambiguous if component type has aggregates.
145
146 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
147 -- Does the first part of the work of Resolve_Op_Concat
148
149 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
150 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
151 -- has been resolved. See Resolve_Op_Concat for details.
152
153 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
154 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
155 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
156 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
157 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
158 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
159 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
160 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
161 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
162 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
163 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
164 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
165 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
166 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
167 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
168 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
169 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
170 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
171 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
172 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
173 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
174 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
185
186 function Operator_Kind
187 (Op_Name : Name_Id;
188 Is_Binary : Boolean) return Node_Kind;
189 -- Utility to map the name of an operator into the corresponding Node. Used
190 -- by other node rewriting procedures.
191
192 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
193 -- Resolve actuals of call, and add default expressions for missing ones.
194 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
195 -- called subprogram.
196
197 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
198 -- Called from Resolve_Call, when the prefix denotes an entry or element
199 -- of entry family. Actuals are resolved as for subprograms, and the node
200 -- is rebuilt as an entry call. Also called for protected operations. Typ
201 -- is the context type, which is used when the operation is a protected
202 -- function with no arguments, and the return value is indexed.
203
204 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
205 -- A call to a user-defined intrinsic operator is rewritten as a call
206 -- to the corresponding predefined operator, with suitable conversions.
207
208 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
209 -- Ditto, for unary operators (only arithmetic ones)
210
211 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
212 -- If an operator node resolves to a call to a user-defined operator,
213 -- rewrite the node as a function call.
214
215 procedure Make_Call_Into_Operator
216 (N : Node_Id;
217 Typ : Entity_Id;
218 Op_Id : Entity_Id);
219 -- Inverse transformation: if an operator is given in functional notation,
220 -- then after resolving the node, transform into an operator node, so
221 -- that operands are resolved properly. Recall that predefined operators
222 -- do not have a full signature and special resolution rules apply.
223
224 procedure Rewrite_Renamed_Operator
225 (N : Node_Id;
226 Op : Entity_Id;
227 Typ : Entity_Id);
228 -- An operator can rename another, e.g. in an instantiation. In that
229 -- case, the proper operator node must be constructed and resolved.
230
231 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
232 -- The String_Literal_Subtype is built for all strings that are not
233 -- operands of a static concatenation operation. If the argument is
234 -- not a N_String_Literal node, then the call has no effect.
235
236 procedure Set_Slice_Subtype (N : Node_Id);
237 -- Build subtype of array type, with the range specified by the slice
238
239 procedure Simplify_Type_Conversion (N : Node_Id);
240 -- Called after N has been resolved and evaluated, but before range checks
241 -- have been applied. Currently simplifies a combination of floating-point
242 -- to integer conversion and Truncation attribute.
243
244 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
245 -- A universal_fixed expression in an universal context is unambiguous
246 -- if there is only one applicable fixed point type. Determining whether
247 -- there is only one requires a search over all visible entities, and
248 -- happens only in very pathological cases (see 6115-006).
249
250 function Valid_Conversion
251 (N : Node_Id;
252 Target : Entity_Id;
253 Operand : Node_Id) return Boolean;
254 -- Verify legality rules given in 4.6 (8-23). Target is the target
255 -- type of the conversion, which may be an implicit conversion of
256 -- an actual parameter to an anonymous access type (in which case
257 -- N denotes the actual parameter and N = Operand).
258
259 -------------------------
260 -- Ambiguous_Character --
261 -------------------------
262
263 procedure Ambiguous_Character (C : Node_Id) is
264 E : Entity_Id;
265
266 begin
267 if Nkind (C) = N_Character_Literal then
268 Error_Msg_N ("ambiguous character literal", C);
269
270 -- First the ones in Standard
271
272 Error_Msg_N
273 ("\\possible interpretation: Character!", C);
274 Error_Msg_N
275 ("\\possible interpretation: Wide_Character!", C);
276
277 -- Include Wide_Wide_Character in Ada 2005 mode
278
279 if Ada_Version >= Ada_05 then
280 Error_Msg_N
281 ("\\possible interpretation: Wide_Wide_Character!", C);
282 end if;
283
284 -- Now any other types that match
285
286 E := Current_Entity (C);
287 while Present (E) loop
288 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
289 E := Homonym (E);
290 end loop;
291 end if;
292 end Ambiguous_Character;
293
294 -------------------------
295 -- Analyze_And_Resolve --
296 -------------------------
297
298 procedure Analyze_And_Resolve (N : Node_Id) is
299 begin
300 Analyze (N);
301 Resolve (N);
302 end Analyze_And_Resolve;
303
304 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
305 begin
306 Analyze (N);
307 Resolve (N, Typ);
308 end Analyze_And_Resolve;
309
310 -- Version withs check(s) suppressed
311
312 procedure Analyze_And_Resolve
313 (N : Node_Id;
314 Typ : Entity_Id;
315 Suppress : Check_Id)
316 is
317 Scop : constant Entity_Id := Current_Scope;
318
319 begin
320 if Suppress = All_Checks then
321 declare
322 Svg : constant Suppress_Array := Scope_Suppress;
323 begin
324 Scope_Suppress := (others => True);
325 Analyze_And_Resolve (N, Typ);
326 Scope_Suppress := Svg;
327 end;
328
329 else
330 declare
331 Svg : constant Boolean := Scope_Suppress (Suppress);
332
333 begin
334 Scope_Suppress (Suppress) := True;
335 Analyze_And_Resolve (N, Typ);
336 Scope_Suppress (Suppress) := Svg;
337 end;
338 end if;
339
340 if Current_Scope /= Scop
341 and then Scope_Is_Transient
342 then
343 -- This can only happen if a transient scope was created
344 -- for an inner expression, which will be removed upon
345 -- completion of the analysis of an enclosing construct.
346 -- The transient scope must have the suppress status of
347 -- the enclosing environment, not of this Analyze call.
348
349 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
350 Scope_Suppress;
351 end if;
352 end Analyze_And_Resolve;
353
354 procedure Analyze_And_Resolve
355 (N : Node_Id;
356 Suppress : Check_Id)
357 is
358 Scop : constant Entity_Id := Current_Scope;
359
360 begin
361 if Suppress = All_Checks then
362 declare
363 Svg : constant Suppress_Array := Scope_Suppress;
364 begin
365 Scope_Suppress := (others => True);
366 Analyze_And_Resolve (N);
367 Scope_Suppress := Svg;
368 end;
369
370 else
371 declare
372 Svg : constant Boolean := Scope_Suppress (Suppress);
373
374 begin
375 Scope_Suppress (Suppress) := True;
376 Analyze_And_Resolve (N);
377 Scope_Suppress (Suppress) := Svg;
378 end;
379 end if;
380
381 if Current_Scope /= Scop
382 and then Scope_Is_Transient
383 then
384 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
385 Scope_Suppress;
386 end if;
387 end Analyze_And_Resolve;
388
389 ----------------------------
390 -- Check_Discriminant_Use --
391 ----------------------------
392
393 procedure Check_Discriminant_Use (N : Node_Id) is
394 PN : constant Node_Id := Parent (N);
395 Disc : constant Entity_Id := Entity (N);
396 P : Node_Id;
397 D : Node_Id;
398
399 begin
400 -- Any use in a spec-expression is legal
401
402 if In_Spec_Expression then
403 null;
404
405 elsif Nkind (PN) = N_Range then
406
407 -- Discriminant cannot be used to constrain a scalar type
408
409 P := Parent (PN);
410
411 if Nkind (P) = N_Range_Constraint
412 and then Nkind (Parent (P)) = N_Subtype_Indication
413 and then Nkind (Parent (Parent (P))) = N_Component_Definition
414 then
415 Error_Msg_N ("discriminant cannot constrain scalar type", N);
416
417 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
418
419 -- The following check catches the unusual case where
420 -- a discriminant appears within an index constraint
421 -- that is part of a larger expression within a constraint
422 -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
423 -- For now we only check case of record components, and
424 -- note that a similar check should also apply in the
425 -- case of discriminant constraints below. ???
426
427 -- Note that the check for N_Subtype_Declaration below is to
428 -- detect the valid use of discriminants in the constraints of a
429 -- subtype declaration when this subtype declaration appears
430 -- inside the scope of a record type (which is syntactically
431 -- illegal, but which may be created as part of derived type
432 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
433 -- for more info.
434
435 if Ekind (Current_Scope) = E_Record_Type
436 and then Scope (Disc) = Current_Scope
437 and then not
438 (Nkind (Parent (P)) = N_Subtype_Indication
439 and then
440 Nkind_In (Parent (Parent (P)), N_Component_Definition,
441 N_Subtype_Declaration)
442 and then Paren_Count (N) = 0)
443 then
444 Error_Msg_N
445 ("discriminant must appear alone in component constraint", N);
446 return;
447 end if;
448
449 -- Detect a common beginner error:
450
451 -- type R (D : Positive := 100) is record
452 -- Name : String (1 .. D);
453 -- end record;
454
455 -- The default value causes an object of type R to be
456 -- allocated with room for Positive'Last characters.
457
458 declare
459 SI : Node_Id;
460 T : Entity_Id;
461 TB : Node_Id;
462 CB : Entity_Id;
463
464 function Large_Storage_Type (T : Entity_Id) return Boolean;
465 -- Return True if type T has a large enough range that
466 -- any array whose index type covered the whole range of
467 -- the type would likely raise Storage_Error.
468
469 ------------------------
470 -- Large_Storage_Type --
471 ------------------------
472
473 function Large_Storage_Type (T : Entity_Id) return Boolean is
474 begin
475 -- The type is considered large if its bounds are known at
476 -- compile time and if it requires at least as many bits as
477 -- a Positive to store the possible values.
478
479 return Compile_Time_Known_Value (Type_Low_Bound (T))
480 and then Compile_Time_Known_Value (Type_High_Bound (T))
481 and then
482 Minimum_Size (T, Biased => True) >=
483 Esize (Standard_Integer) - 1;
484 end Large_Storage_Type;
485
486 begin
487 -- Check that the Disc has a large range
488
489 if not Large_Storage_Type (Etype (Disc)) then
490 goto No_Danger;
491 end if;
492
493 -- If the enclosing type is limited, we allocate only the
494 -- default value, not the maximum, and there is no need for
495 -- a warning.
496
497 if Is_Limited_Type (Scope (Disc)) then
498 goto No_Danger;
499 end if;
500
501 -- Check that it is the high bound
502
503 if N /= High_Bound (PN)
504 or else No (Discriminant_Default_Value (Disc))
505 then
506 goto No_Danger;
507 end if;
508
509 -- Check the array allows a large range at this bound.
510 -- First find the array
511
512 SI := Parent (P);
513
514 if Nkind (SI) /= N_Subtype_Indication then
515 goto No_Danger;
516 end if;
517
518 T := Entity (Subtype_Mark (SI));
519
520 if not Is_Array_Type (T) then
521 goto No_Danger;
522 end if;
523
524 -- Next, find the dimension
525
526 TB := First_Index (T);
527 CB := First (Constraints (P));
528 while True
529 and then Present (TB)
530 and then Present (CB)
531 and then CB /= PN
532 loop
533 Next_Index (TB);
534 Next (CB);
535 end loop;
536
537 if CB /= PN then
538 goto No_Danger;
539 end if;
540
541 -- Now, check the dimension has a large range
542
543 if not Large_Storage_Type (Etype (TB)) then
544 goto No_Danger;
545 end if;
546
547 -- Warn about the danger
548
549 Error_Msg_N
550 ("?creation of & object may raise Storage_Error!",
551 Scope (Disc));
552
553 <<No_Danger>>
554 null;
555
556 end;
557 end if;
558
559 -- Legal case is in index or discriminant constraint
560
561 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
562 N_Discriminant_Association)
563 then
564 if Paren_Count (N) > 0 then
565 Error_Msg_N
566 ("discriminant in constraint must appear alone", N);
567
568 elsif Nkind (N) = N_Expanded_Name
569 and then Comes_From_Source (N)
570 then
571 Error_Msg_N
572 ("discriminant must appear alone as a direct name", N);
573 end if;
574
575 return;
576
577 -- Otherwise, context is an expression. It should not be within
578 -- (i.e. a subexpression of) a constraint for a component.
579
580 else
581 D := PN;
582 P := Parent (PN);
583 while not Nkind_In (P, N_Component_Declaration,
584 N_Subtype_Indication,
585 N_Entry_Declaration)
586 loop
587 D := P;
588 P := Parent (P);
589 exit when No (P);
590 end loop;
591
592 -- If the discriminant is used in an expression that is a bound
593 -- of a scalar type, an Itype is created and the bounds are attached
594 -- to its range, not to the original subtype indication. Such use
595 -- is of course a double fault.
596
597 if (Nkind (P) = N_Subtype_Indication
598 and then Nkind_In (Parent (P), N_Component_Definition,
599 N_Derived_Type_Definition)
600 and then D = Constraint (P))
601
602 -- The constraint itself may be given by a subtype indication,
603 -- rather than by a more common discrete range.
604
605 or else (Nkind (P) = N_Subtype_Indication
606 and then
607 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
608 or else Nkind (P) = N_Entry_Declaration
609 or else Nkind (D) = N_Defining_Identifier
610 then
611 Error_Msg_N
612 ("discriminant in constraint must appear alone", N);
613 end if;
614 end if;
615 end Check_Discriminant_Use;
616
617 --------------------------------
618 -- Check_For_Visible_Operator --
619 --------------------------------
620
621 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
622 begin
623 if Is_Invisible_Operator (N, T) then
624 Error_Msg_NE
625 ("operator for} is not directly visible!", N, First_Subtype (T));
626 Error_Msg_N ("use clause would make operation legal!", N);
627 end if;
628 end Check_For_Visible_Operator;
629
630 ----------------------------------
631 -- Check_Fully_Declared_Prefix --
632 ----------------------------------
633
634 procedure Check_Fully_Declared_Prefix
635 (Typ : Entity_Id;
636 Pref : Node_Id)
637 is
638 begin
639 -- Check that the designated type of the prefix of a dereference is
640 -- not an incomplete type. This cannot be done unconditionally, because
641 -- dereferences of private types are legal in default expressions. This
642 -- case is taken care of in Check_Fully_Declared, called below. There
643 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
644
645 -- This consideration also applies to similar checks for allocators,
646 -- qualified expressions, and type conversions.
647
648 -- An additional exception concerns other per-object expressions that
649 -- are not directly related to component declarations, in particular
650 -- representation pragmas for tasks. These will be per-object
651 -- expressions if they depend on discriminants or some global entity.
652 -- If the task has access discriminants, the designated type may be
653 -- incomplete at the point the expression is resolved. This resolution
654 -- takes place within the body of the initialization procedure, where
655 -- the discriminant is replaced by its discriminal.
656
657 if Is_Entity_Name (Pref)
658 and then Ekind (Entity (Pref)) = E_In_Parameter
659 then
660 null;
661
662 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
663 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
664 -- Analyze_Object_Renaming, and Freeze_Entity.
665
666 elsif Ada_Version >= Ada_05
667 and then Is_Entity_Name (Pref)
668 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
669 E_Incomplete_Type
670 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
671 then
672 null;
673 else
674 Check_Fully_Declared (Typ, Parent (Pref));
675 end if;
676 end Check_Fully_Declared_Prefix;
677
678 ------------------------------
679 -- Check_Infinite_Recursion --
680 ------------------------------
681
682 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
683 P : Node_Id;
684 C : Node_Id;
685
686 function Same_Argument_List return Boolean;
687 -- Check whether list of actuals is identical to list of formals
688 -- of called function (which is also the enclosing scope).
689
690 ------------------------
691 -- Same_Argument_List --
692 ------------------------
693
694 function Same_Argument_List return Boolean is
695 A : Node_Id;
696 F : Entity_Id;
697 Subp : Entity_Id;
698
699 begin
700 if not Is_Entity_Name (Name (N)) then
701 return False;
702 else
703 Subp := Entity (Name (N));
704 end if;
705
706 F := First_Formal (Subp);
707 A := First_Actual (N);
708 while Present (F) and then Present (A) loop
709 if not Is_Entity_Name (A)
710 or else Entity (A) /= F
711 then
712 return False;
713 end if;
714
715 Next_Actual (A);
716 Next_Formal (F);
717 end loop;
718
719 return True;
720 end Same_Argument_List;
721
722 -- Start of processing for Check_Infinite_Recursion
723
724 begin
725 -- Special case, if this is a procedure call and is a call to the
726 -- current procedure with the same argument list, then this is for
727 -- sure an infinite recursion and we insert a call to raise SE.
728
729 if Is_List_Member (N)
730 and then List_Length (List_Containing (N)) = 1
731 and then Same_Argument_List
732 then
733 declare
734 P : constant Node_Id := Parent (N);
735 begin
736 if Nkind (P) = N_Handled_Sequence_Of_Statements
737 and then Nkind (Parent (P)) = N_Subprogram_Body
738 and then Is_Empty_List (Declarations (Parent (P)))
739 then
740 Error_Msg_N ("!?infinite recursion", N);
741 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
742 Insert_Action (N,
743 Make_Raise_Storage_Error (Sloc (N),
744 Reason => SE_Infinite_Recursion));
745 return True;
746 end if;
747 end;
748 end if;
749
750 -- If not that special case, search up tree, quitting if we reach a
751 -- construct (e.g. a conditional) that tells us that this is not a
752 -- case for an infinite recursion warning.
753
754 C := N;
755 loop
756 P := Parent (C);
757
758 -- If no parent, then we were not inside a subprogram, this can for
759 -- example happen when processing certain pragmas in a spec. Just
760 -- return False in this case.
761
762 if No (P) then
763 return False;
764 end if;
765
766 -- Done if we get to subprogram body, this is definitely an infinite
767 -- recursion case if we did not find anything to stop us.
768
769 exit when Nkind (P) = N_Subprogram_Body;
770
771 -- If appearing in conditional, result is false
772
773 if Nkind_In (P, N_Or_Else,
774 N_And_Then,
775 N_If_Statement,
776 N_Case_Statement)
777 then
778 return False;
779
780 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
781 and then C /= First (Statements (P))
782 then
783 -- If the call is the expression of a return statement and the
784 -- actuals are identical to the formals, it's worth a warning.
785 -- However, we skip this if there is an immediately preceding
786 -- raise statement, since the call is never executed.
787
788 -- Furthermore, this corresponds to a common idiom:
789
790 -- function F (L : Thing) return Boolean is
791 -- begin
792 -- raise Program_Error;
793 -- return F (L);
794 -- end F;
795
796 -- for generating a stub function
797
798 if Nkind (Parent (N)) = N_Simple_Return_Statement
799 and then Same_Argument_List
800 then
801 exit when not Is_List_Member (Parent (N));
802
803 -- OK, return statement is in a statement list, look for raise
804
805 declare
806 Nod : Node_Id;
807
808 begin
809 -- Skip past N_Freeze_Entity nodes generated by expansion
810
811 Nod := Prev (Parent (N));
812 while Present (Nod)
813 and then Nkind (Nod) = N_Freeze_Entity
814 loop
815 Prev (Nod);
816 end loop;
817
818 -- If no raise statement, give warning
819
820 exit when Nkind (Nod) /= N_Raise_Statement
821 and then
822 (Nkind (Nod) not in N_Raise_xxx_Error
823 or else Present (Condition (Nod)));
824 end;
825 end if;
826
827 return False;
828
829 else
830 C := P;
831 end if;
832 end loop;
833
834 Error_Msg_N ("!?possible infinite recursion", N);
835 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
836
837 return True;
838 end Check_Infinite_Recursion;
839
840 -------------------------------
841 -- Check_Initialization_Call --
842 -------------------------------
843
844 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
845 Typ : constant Entity_Id := Etype (First_Formal (Nam));
846
847 function Uses_SS (T : Entity_Id) return Boolean;
848 -- Check whether the creation of an object of the type will involve
849 -- use of the secondary stack. If T is a record type, this is true
850 -- if the expression for some component uses the secondary stack, e.g.
851 -- through a call to a function that returns an unconstrained value.
852 -- False if T is controlled, because cleanups occur elsewhere.
853
854 -------------
855 -- Uses_SS --
856 -------------
857
858 function Uses_SS (T : Entity_Id) return Boolean is
859 Comp : Entity_Id;
860 Expr : Node_Id;
861 Full_Type : Entity_Id := Underlying_Type (T);
862
863 begin
864 -- Normally we want to use the underlying type, but if it's not set
865 -- then continue with T.
866
867 if not Present (Full_Type) then
868 Full_Type := T;
869 end if;
870
871 if Is_Controlled (Full_Type) then
872 return False;
873
874 elsif Is_Array_Type (Full_Type) then
875 return Uses_SS (Component_Type (Full_Type));
876
877 elsif Is_Record_Type (Full_Type) then
878 Comp := First_Component (Full_Type);
879 while Present (Comp) loop
880 if Ekind (Comp) = E_Component
881 and then Nkind (Parent (Comp)) = N_Component_Declaration
882 then
883 -- The expression for a dynamic component may be rewritten
884 -- as a dereference, so retrieve original node.
885
886 Expr := Original_Node (Expression (Parent (Comp)));
887
888 -- Return True if the expression is a call to a function
889 -- (including an attribute function such as Image) with
890 -- a result that requires a transient scope.
891
892 if (Nkind (Expr) = N_Function_Call
893 or else (Nkind (Expr) = N_Attribute_Reference
894 and then Present (Expressions (Expr))))
895 and then Requires_Transient_Scope (Etype (Expr))
896 then
897 return True;
898
899 elsif Uses_SS (Etype (Comp)) then
900 return True;
901 end if;
902 end if;
903
904 Next_Component (Comp);
905 end loop;
906
907 return False;
908
909 else
910 return False;
911 end if;
912 end Uses_SS;
913
914 -- Start of processing for Check_Initialization_Call
915
916 begin
917 -- Establish a transient scope if the type needs it
918
919 if Uses_SS (Typ) then
920 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
921 end if;
922 end Check_Initialization_Call;
923
924 ------------------------------
925 -- Check_Parameterless_Call --
926 ------------------------------
927
928 procedure Check_Parameterless_Call (N : Node_Id) is
929 Nam : Node_Id;
930
931 function Prefix_Is_Access_Subp return Boolean;
932 -- If the prefix is of an access_to_subprogram type, the node must be
933 -- rewritten as a call. Ditto if the prefix is overloaded and all its
934 -- interpretations are access to subprograms.
935
936 ---------------------------
937 -- Prefix_Is_Access_Subp --
938 ---------------------------
939
940 function Prefix_Is_Access_Subp return Boolean is
941 I : Interp_Index;
942 It : Interp;
943
944 begin
945 if not Is_Overloaded (N) then
946 return
947 Ekind (Etype (N)) = E_Subprogram_Type
948 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
949 else
950 Get_First_Interp (N, I, It);
951 while Present (It.Typ) loop
952 if Ekind (It.Typ) /= E_Subprogram_Type
953 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
954 then
955 return False;
956 end if;
957
958 Get_Next_Interp (I, It);
959 end loop;
960
961 return True;
962 end if;
963 end Prefix_Is_Access_Subp;
964
965 -- Start of processing for Check_Parameterless_Call
966
967 begin
968 -- Defend against junk stuff if errors already detected
969
970 if Total_Errors_Detected /= 0 then
971 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
972 return;
973 elsif Nkind (N) in N_Has_Chars
974 and then Chars (N) in Error_Name_Or_No_Name
975 then
976 return;
977 end if;
978
979 Require_Entity (N);
980 end if;
981
982 -- If the context expects a value, and the name is a procedure, this is
983 -- most likely a missing 'Access. Don't try to resolve the parameterless
984 -- call, error will be caught when the outer call is analyzed.
985
986 if Is_Entity_Name (N)
987 and then Ekind (Entity (N)) = E_Procedure
988 and then not Is_Overloaded (N)
989 and then
990 Nkind_In (Parent (N), N_Parameter_Association,
991 N_Function_Call,
992 N_Procedure_Call_Statement)
993 then
994 return;
995 end if;
996
997 -- Rewrite as call if overloadable entity that is (or could be, in the
998 -- overloaded case) a function call. If we know for sure that the entity
999 -- is an enumeration literal, we do not rewrite it.
1000
1001 if (Is_Entity_Name (N)
1002 and then Is_Overloadable (Entity (N))
1003 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1004 or else Is_Overloaded (N)))
1005
1006 -- Rewrite as call if it is an explicit deference of an expression of
1007 -- a subprogram access type, and the subprogram type is not that of a
1008 -- procedure or entry.
1009
1010 or else
1011 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1012
1013 -- Rewrite as call if it is a selected component which is a function,
1014 -- this is the case of a call to a protected function (which may be
1015 -- overloaded with other protected operations).
1016
1017 or else
1018 (Nkind (N) = N_Selected_Component
1019 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1020 or else
1021 ((Ekind (Entity (Selector_Name (N))) = E_Entry
1022 or else
1023 Ekind (Entity (Selector_Name (N))) = E_Procedure)
1024 and then Is_Overloaded (Selector_Name (N)))))
1025
1026 -- If one of the above three conditions is met, rewrite as call.
1027 -- Apply the rewriting only once.
1028
1029 then
1030 if Nkind (Parent (N)) /= N_Function_Call
1031 or else N /= Name (Parent (N))
1032 then
1033 Nam := New_Copy (N);
1034
1035 -- If overloaded, overload set belongs to new copy
1036
1037 Save_Interps (N, Nam);
1038
1039 -- Change node to parameterless function call (note that the
1040 -- Parameter_Associations associations field is left set to Empty,
1041 -- its normal default value since there are no parameters)
1042
1043 Change_Node (N, N_Function_Call);
1044 Set_Name (N, Nam);
1045 Set_Sloc (N, Sloc (Nam));
1046 Analyze_Call (N);
1047 end if;
1048
1049 elsif Nkind (N) = N_Parameter_Association then
1050 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1051 end if;
1052 end Check_Parameterless_Call;
1053
1054 -----------------------------
1055 -- Is_Definite_Access_Type --
1056 -----------------------------
1057
1058 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1059 Btyp : constant Entity_Id := Base_Type (E);
1060 begin
1061 return Ekind (Btyp) = E_Access_Type
1062 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1063 and then Comes_From_Source (Btyp));
1064 end Is_Definite_Access_Type;
1065
1066 ----------------------
1067 -- Is_Predefined_Op --
1068 ----------------------
1069
1070 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1071 begin
1072 return Is_Intrinsic_Subprogram (Nam)
1073 and then not Is_Generic_Instance (Nam)
1074 and then Chars (Nam) in Any_Operator_Name
1075 and then (No (Alias (Nam))
1076 or else Is_Predefined_Op (Alias (Nam)));
1077 end Is_Predefined_Op;
1078
1079 -----------------------------
1080 -- Make_Call_Into_Operator --
1081 -----------------------------
1082
1083 procedure Make_Call_Into_Operator
1084 (N : Node_Id;
1085 Typ : Entity_Id;
1086 Op_Id : Entity_Id)
1087 is
1088 Op_Name : constant Name_Id := Chars (Op_Id);
1089 Act1 : Node_Id := First_Actual (N);
1090 Act2 : Node_Id := Next_Actual (Act1);
1091 Error : Boolean := False;
1092 Func : constant Entity_Id := Entity (Name (N));
1093 Is_Binary : constant Boolean := Present (Act2);
1094 Op_Node : Node_Id;
1095 Opnd_Type : Entity_Id;
1096 Orig_Type : Entity_Id := Empty;
1097 Pack : Entity_Id;
1098
1099 type Kind_Test is access function (E : Entity_Id) return Boolean;
1100
1101 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1102 -- If the operand is not universal, and the operator is given by a
1103 -- expanded name, verify that the operand has an interpretation with
1104 -- a type defined in the given scope of the operator.
1105
1106 function Type_In_P (Test : Kind_Test) return Entity_Id;
1107 -- Find a type of the given class in the package Pack that contains
1108 -- the operator.
1109
1110 ---------------------------
1111 -- Operand_Type_In_Scope --
1112 ---------------------------
1113
1114 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1115 Nod : constant Node_Id := Right_Opnd (Op_Node);
1116 I : Interp_Index;
1117 It : Interp;
1118
1119 begin
1120 if not Is_Overloaded (Nod) then
1121 return Scope (Base_Type (Etype (Nod))) = S;
1122
1123 else
1124 Get_First_Interp (Nod, I, It);
1125 while Present (It.Typ) loop
1126 if Scope (Base_Type (It.Typ)) = S then
1127 return True;
1128 end if;
1129
1130 Get_Next_Interp (I, It);
1131 end loop;
1132
1133 return False;
1134 end if;
1135 end Operand_Type_In_Scope;
1136
1137 ---------------
1138 -- Type_In_P --
1139 ---------------
1140
1141 function Type_In_P (Test : Kind_Test) return Entity_Id is
1142 E : Entity_Id;
1143
1144 function In_Decl return Boolean;
1145 -- Verify that node is not part of the type declaration for the
1146 -- candidate type, which would otherwise be invisible.
1147
1148 -------------
1149 -- In_Decl --
1150 -------------
1151
1152 function In_Decl return Boolean is
1153 Decl_Node : constant Node_Id := Parent (E);
1154 N2 : Node_Id;
1155
1156 begin
1157 N2 := N;
1158
1159 if Etype (E) = Any_Type then
1160 return True;
1161
1162 elsif No (Decl_Node) then
1163 return False;
1164
1165 else
1166 while Present (N2)
1167 and then Nkind (N2) /= N_Compilation_Unit
1168 loop
1169 if N2 = Decl_Node then
1170 return True;
1171 else
1172 N2 := Parent (N2);
1173 end if;
1174 end loop;
1175
1176 return False;
1177 end if;
1178 end In_Decl;
1179
1180 -- Start of processing for Type_In_P
1181
1182 begin
1183 -- If the context type is declared in the prefix package, this
1184 -- is the desired base type.
1185
1186 if Scope (Base_Type (Typ)) = Pack
1187 and then Test (Typ)
1188 then
1189 return Base_Type (Typ);
1190
1191 else
1192 E := First_Entity (Pack);
1193 while Present (E) loop
1194 if Test (E)
1195 and then not In_Decl
1196 then
1197 return E;
1198 end if;
1199
1200 Next_Entity (E);
1201 end loop;
1202
1203 return Empty;
1204 end if;
1205 end Type_In_P;
1206
1207 -- Start of processing for Make_Call_Into_Operator
1208
1209 begin
1210 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1211
1212 -- Binary operator
1213
1214 if Is_Binary then
1215 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1216 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1217 Save_Interps (Act1, Left_Opnd (Op_Node));
1218 Save_Interps (Act2, Right_Opnd (Op_Node));
1219 Act1 := Left_Opnd (Op_Node);
1220 Act2 := Right_Opnd (Op_Node);
1221
1222 -- Unary operator
1223
1224 else
1225 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1226 Save_Interps (Act1, Right_Opnd (Op_Node));
1227 Act1 := Right_Opnd (Op_Node);
1228 end if;
1229
1230 -- If the operator is denoted by an expanded name, and the prefix is
1231 -- not Standard, but the operator is a predefined one whose scope is
1232 -- Standard, then this is an implicit_operator, inserted as an
1233 -- interpretation by the procedure of the same name. This procedure
1234 -- overestimates the presence of implicit operators, because it does
1235 -- not examine the type of the operands. Verify now that the operand
1236 -- type appears in the given scope. If right operand is universal,
1237 -- check the other operand. In the case of concatenation, either
1238 -- argument can be the component type, so check the type of the result.
1239 -- If both arguments are literals, look for a type of the right kind
1240 -- defined in the given scope. This elaborate nonsense is brought to
1241 -- you courtesy of b33302a. The type itself must be frozen, so we must
1242 -- find the type of the proper class in the given scope.
1243
1244 -- A final wrinkle is the multiplication operator for fixed point
1245 -- types, which is defined in Standard only, and not in the scope of
1246 -- the fixed_point type itself.
1247
1248 if Nkind (Name (N)) = N_Expanded_Name then
1249 Pack := Entity (Prefix (Name (N)));
1250
1251 -- If the entity being called is defined in the given package,
1252 -- it is a renaming of a predefined operator, and known to be
1253 -- legal.
1254
1255 if Scope (Entity (Name (N))) = Pack
1256 and then Pack /= Standard_Standard
1257 then
1258 null;
1259
1260 -- Visibility does not need to be checked in an instance: if the
1261 -- operator was not visible in the generic it has been diagnosed
1262 -- already, else there is an implicit copy of it in the instance.
1263
1264 elsif In_Instance then
1265 null;
1266
1267 elsif (Op_Name = Name_Op_Multiply
1268 or else Op_Name = Name_Op_Divide)
1269 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1270 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1271 then
1272 if Pack /= Standard_Standard then
1273 Error := True;
1274 end if;
1275
1276 -- Ada 2005, AI-420: Predefined equality on Universal_Access
1277 -- is available.
1278
1279 elsif Ada_Version >= Ada_05
1280 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1281 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1282 then
1283 null;
1284
1285 else
1286 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1287
1288 if Op_Name = Name_Op_Concat then
1289 Opnd_Type := Base_Type (Typ);
1290
1291 elsif (Scope (Opnd_Type) = Standard_Standard
1292 and then Is_Binary)
1293 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1294 and then Is_Binary
1295 and then not Comes_From_Source (Opnd_Type))
1296 then
1297 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1298 end if;
1299
1300 if Scope (Opnd_Type) = Standard_Standard then
1301
1302 -- Verify that the scope contains a type that corresponds to
1303 -- the given literal. Optimize the case where Pack is Standard.
1304
1305 if Pack /= Standard_Standard then
1306
1307 if Opnd_Type = Universal_Integer then
1308 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1309
1310 elsif Opnd_Type = Universal_Real then
1311 Orig_Type := Type_In_P (Is_Real_Type'Access);
1312
1313 elsif Opnd_Type = Any_String then
1314 Orig_Type := Type_In_P (Is_String_Type'Access);
1315
1316 elsif Opnd_Type = Any_Access then
1317 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1318
1319 elsif Opnd_Type = Any_Composite then
1320 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1321
1322 if Present (Orig_Type) then
1323 if Has_Private_Component (Orig_Type) then
1324 Orig_Type := Empty;
1325 else
1326 Set_Etype (Act1, Orig_Type);
1327
1328 if Is_Binary then
1329 Set_Etype (Act2, Orig_Type);
1330 end if;
1331 end if;
1332 end if;
1333
1334 else
1335 Orig_Type := Empty;
1336 end if;
1337
1338 Error := No (Orig_Type);
1339 end if;
1340
1341 elsif Ekind (Opnd_Type) = E_Allocator_Type
1342 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1343 then
1344 Error := True;
1345
1346 -- If the type is defined elsewhere, and the operator is not
1347 -- defined in the given scope (by a renaming declaration, e.g.)
1348 -- then this is an error as well. If an extension of System is
1349 -- present, and the type may be defined there, Pack must be
1350 -- System itself.
1351
1352 elsif Scope (Opnd_Type) /= Pack
1353 and then Scope (Op_Id) /= Pack
1354 and then (No (System_Aux_Id)
1355 or else Scope (Opnd_Type) /= System_Aux_Id
1356 or else Pack /= Scope (System_Aux_Id))
1357 then
1358 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1359 Error := True;
1360 else
1361 Error := not Operand_Type_In_Scope (Pack);
1362 end if;
1363
1364 elsif Pack = Standard_Standard
1365 and then not Operand_Type_In_Scope (Standard_Standard)
1366 then
1367 Error := True;
1368 end if;
1369 end if;
1370
1371 if Error then
1372 Error_Msg_Node_2 := Pack;
1373 Error_Msg_NE
1374 ("& not declared in&", N, Selector_Name (Name (N)));
1375 Set_Etype (N, Any_Type);
1376 return;
1377 end if;
1378 end if;
1379
1380 Set_Chars (Op_Node, Op_Name);
1381
1382 if not Is_Private_Type (Etype (N)) then
1383 Set_Etype (Op_Node, Base_Type (Etype (N)));
1384 else
1385 Set_Etype (Op_Node, Etype (N));
1386 end if;
1387
1388 -- If this is a call to a function that renames a predefined equality,
1389 -- the renaming declaration provides a type that must be used to
1390 -- resolve the operands. This must be done now because resolution of
1391 -- the equality node will not resolve any remaining ambiguity, and it
1392 -- assumes that the first operand is not overloaded.
1393
1394 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1395 and then Ekind (Func) = E_Function
1396 and then Is_Overloaded (Act1)
1397 then
1398 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1399 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1400 end if;
1401
1402 Set_Entity (Op_Node, Op_Id);
1403 Generate_Reference (Op_Id, N, ' ');
1404
1405 -- Do rewrite setting Comes_From_Source on the result if the original
1406 -- call came from source. Although it is not strictly the case that the
1407 -- operator as such comes from the source, logically it corresponds
1408 -- exactly to the function call in the source, so it should be marked
1409 -- this way (e.g. to make sure that validity checks work fine).
1410
1411 declare
1412 CS : constant Boolean := Comes_From_Source (N);
1413 begin
1414 Rewrite (N, Op_Node);
1415 Set_Comes_From_Source (N, CS);
1416 end;
1417
1418 -- If this is an arithmetic operator and the result type is private,
1419 -- the operands and the result must be wrapped in conversion to
1420 -- expose the underlying numeric type and expand the proper checks,
1421 -- e.g. on division.
1422
1423 if Is_Private_Type (Typ) then
1424 case Nkind (N) is
1425 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1426 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1427 Resolve_Intrinsic_Operator (N, Typ);
1428
1429 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1430 Resolve_Intrinsic_Unary_Operator (N, Typ);
1431
1432 when others =>
1433 Resolve (N, Typ);
1434 end case;
1435 else
1436 Resolve (N, Typ);
1437 end if;
1438
1439 -- For predefined operators on literals, the operation freezes
1440 -- their type.
1441
1442 if Present (Orig_Type) then
1443 Set_Etype (Act1, Orig_Type);
1444 Freeze_Expression (Act1);
1445 end if;
1446 end Make_Call_Into_Operator;
1447
1448 -------------------
1449 -- Operator_Kind --
1450 -------------------
1451
1452 function Operator_Kind
1453 (Op_Name : Name_Id;
1454 Is_Binary : Boolean) return Node_Kind
1455 is
1456 Kind : Node_Kind;
1457
1458 begin
1459 if Is_Binary then
1460 if Op_Name = Name_Op_And then
1461 Kind := N_Op_And;
1462 elsif Op_Name = Name_Op_Or then
1463 Kind := N_Op_Or;
1464 elsif Op_Name = Name_Op_Xor then
1465 Kind := N_Op_Xor;
1466 elsif Op_Name = Name_Op_Eq then
1467 Kind := N_Op_Eq;
1468 elsif Op_Name = Name_Op_Ne then
1469 Kind := N_Op_Ne;
1470 elsif Op_Name = Name_Op_Lt then
1471 Kind := N_Op_Lt;
1472 elsif Op_Name = Name_Op_Le then
1473 Kind := N_Op_Le;
1474 elsif Op_Name = Name_Op_Gt then
1475 Kind := N_Op_Gt;
1476 elsif Op_Name = Name_Op_Ge then
1477 Kind := N_Op_Ge;
1478 elsif Op_Name = Name_Op_Add then
1479 Kind := N_Op_Add;
1480 elsif Op_Name = Name_Op_Subtract then
1481 Kind := N_Op_Subtract;
1482 elsif Op_Name = Name_Op_Concat then
1483 Kind := N_Op_Concat;
1484 elsif Op_Name = Name_Op_Multiply then
1485 Kind := N_Op_Multiply;
1486 elsif Op_Name = Name_Op_Divide then
1487 Kind := N_Op_Divide;
1488 elsif Op_Name = Name_Op_Mod then
1489 Kind := N_Op_Mod;
1490 elsif Op_Name = Name_Op_Rem then
1491 Kind := N_Op_Rem;
1492 elsif Op_Name = Name_Op_Expon then
1493 Kind := N_Op_Expon;
1494 else
1495 raise Program_Error;
1496 end if;
1497
1498 -- Unary operators
1499
1500 else
1501 if Op_Name = Name_Op_Add then
1502 Kind := N_Op_Plus;
1503 elsif Op_Name = Name_Op_Subtract then
1504 Kind := N_Op_Minus;
1505 elsif Op_Name = Name_Op_Abs then
1506 Kind := N_Op_Abs;
1507 elsif Op_Name = Name_Op_Not then
1508 Kind := N_Op_Not;
1509 else
1510 raise Program_Error;
1511 end if;
1512 end if;
1513
1514 return Kind;
1515 end Operator_Kind;
1516
1517 ----------------------------
1518 -- Preanalyze_And_Resolve --
1519 ----------------------------
1520
1521 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1522 Save_Full_Analysis : constant Boolean := Full_Analysis;
1523
1524 begin
1525 Full_Analysis := False;
1526 Expander_Mode_Save_And_Set (False);
1527
1528 -- We suppress all checks for this analysis, since the checks will
1529 -- be applied properly, and in the right location, when the default
1530 -- expression is reanalyzed and reexpanded later on.
1531
1532 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1533
1534 Expander_Mode_Restore;
1535 Full_Analysis := Save_Full_Analysis;
1536 end Preanalyze_And_Resolve;
1537
1538 -- Version without context type
1539
1540 procedure Preanalyze_And_Resolve (N : Node_Id) is
1541 Save_Full_Analysis : constant Boolean := Full_Analysis;
1542
1543 begin
1544 Full_Analysis := False;
1545 Expander_Mode_Save_And_Set (False);
1546
1547 Analyze (N);
1548 Resolve (N, Etype (N), Suppress => All_Checks);
1549
1550 Expander_Mode_Restore;
1551 Full_Analysis := Save_Full_Analysis;
1552 end Preanalyze_And_Resolve;
1553
1554 ----------------------------------
1555 -- Replace_Actual_Discriminants --
1556 ----------------------------------
1557
1558 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1559 Loc : constant Source_Ptr := Sloc (N);
1560 Tsk : Node_Id := Empty;
1561
1562 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1563
1564 -------------------
1565 -- Process_Discr --
1566 -------------------
1567
1568 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1569 Ent : Entity_Id;
1570
1571 begin
1572 if Nkind (Nod) = N_Identifier then
1573 Ent := Entity (Nod);
1574
1575 if Present (Ent)
1576 and then Ekind (Ent) = E_Discriminant
1577 then
1578 Rewrite (Nod,
1579 Make_Selected_Component (Loc,
1580 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1581 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1582
1583 Set_Etype (Nod, Etype (Ent));
1584 end if;
1585
1586 end if;
1587
1588 return OK;
1589 end Process_Discr;
1590
1591 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1592
1593 -- Start of processing for Replace_Actual_Discriminants
1594
1595 begin
1596 if not Expander_Active then
1597 return;
1598 end if;
1599
1600 if Nkind (Name (N)) = N_Selected_Component then
1601 Tsk := Prefix (Name (N));
1602
1603 elsif Nkind (Name (N)) = N_Indexed_Component then
1604 Tsk := Prefix (Prefix (Name (N)));
1605 end if;
1606
1607 if No (Tsk) then
1608 return;
1609 else
1610 Replace_Discrs (Default);
1611 end if;
1612 end Replace_Actual_Discriminants;
1613
1614 -------------
1615 -- Resolve --
1616 -------------
1617
1618 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1619 Ambiguous : Boolean := False;
1620 Ctx_Type : Entity_Id := Typ;
1621 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1622 Err_Type : Entity_Id := Empty;
1623 Found : Boolean := False;
1624 From_Lib : Boolean;
1625 I : Interp_Index;
1626 I1 : Interp_Index := 0; -- prevent junk warning
1627 It : Interp;
1628 It1 : Interp;
1629 Seen : Entity_Id := Empty; -- prevent junk warning
1630
1631 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1632 -- Determine whether a node comes from a predefined library unit or
1633 -- Standard.
1634
1635 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1636 -- Try and fix up a literal so that it matches its expected type. New
1637 -- literals are manufactured if necessary to avoid cascaded errors.
1638
1639 procedure Resolution_Failed;
1640 -- Called when attempt at resolving current expression fails
1641
1642 ------------------------------------
1643 -- Comes_From_Predefined_Lib_Unit --
1644 -------------------------------------
1645
1646 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1647 begin
1648 return
1649 Sloc (Nod) = Standard_Location
1650 or else Is_Predefined_File_Name (Unit_File_Name (
1651 Get_Source_Unit (Sloc (Nod))));
1652 end Comes_From_Predefined_Lib_Unit;
1653
1654 --------------------
1655 -- Patch_Up_Value --
1656 --------------------
1657
1658 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1659 begin
1660 if Nkind (N) = N_Integer_Literal
1661 and then Is_Real_Type (Typ)
1662 then
1663 Rewrite (N,
1664 Make_Real_Literal (Sloc (N),
1665 Realval => UR_From_Uint (Intval (N))));
1666 Set_Etype (N, Universal_Real);
1667 Set_Is_Static_Expression (N);
1668
1669 elsif Nkind (N) = N_Real_Literal
1670 and then Is_Integer_Type (Typ)
1671 then
1672 Rewrite (N,
1673 Make_Integer_Literal (Sloc (N),
1674 Intval => UR_To_Uint (Realval (N))));
1675 Set_Etype (N, Universal_Integer);
1676 Set_Is_Static_Expression (N);
1677
1678 elsif Nkind (N) = N_String_Literal
1679 and then Is_Character_Type (Typ)
1680 then
1681 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1682 Rewrite (N,
1683 Make_Character_Literal (Sloc (N),
1684 Chars => Name_Find,
1685 Char_Literal_Value =>
1686 UI_From_Int (Character'Pos ('A'))));
1687 Set_Etype (N, Any_Character);
1688 Set_Is_Static_Expression (N);
1689
1690 elsif Nkind (N) /= N_String_Literal
1691 and then Is_String_Type (Typ)
1692 then
1693 Rewrite (N,
1694 Make_String_Literal (Sloc (N),
1695 Strval => End_String));
1696
1697 elsif Nkind (N) = N_Range then
1698 Patch_Up_Value (Low_Bound (N), Typ);
1699 Patch_Up_Value (High_Bound (N), Typ);
1700 end if;
1701 end Patch_Up_Value;
1702
1703 -----------------------
1704 -- Resolution_Failed --
1705 -----------------------
1706
1707 procedure Resolution_Failed is
1708 begin
1709 Patch_Up_Value (N, Typ);
1710 Set_Etype (N, Typ);
1711 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1712 Set_Is_Overloaded (N, False);
1713
1714 -- The caller will return without calling the expander, so we need
1715 -- to set the analyzed flag. Note that it is fine to set Analyzed
1716 -- to True even if we are in the middle of a shallow analysis,
1717 -- (see the spec of sem for more details) since this is an error
1718 -- situation anyway, and there is no point in repeating the
1719 -- analysis later (indeed it won't work to repeat it later, since
1720 -- we haven't got a clear resolution of which entity is being
1721 -- referenced.)
1722
1723 Set_Analyzed (N, True);
1724 return;
1725 end Resolution_Failed;
1726
1727 -- Start of processing for Resolve
1728
1729 begin
1730 if N = Error then
1731 return;
1732 end if;
1733
1734 -- Access attribute on remote subprogram cannot be used for
1735 -- a non-remote access-to-subprogram type.
1736
1737 if Nkind (N) = N_Attribute_Reference
1738 and then (Attribute_Name (N) = Name_Access
1739 or else Attribute_Name (N) = Name_Unrestricted_Access
1740 or else Attribute_Name (N) = Name_Unchecked_Access)
1741 and then Comes_From_Source (N)
1742 and then Is_Entity_Name (Prefix (N))
1743 and then Is_Subprogram (Entity (Prefix (N)))
1744 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1745 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1746 then
1747 Error_Msg_N
1748 ("prefix must statically denote a non-remote subprogram", N);
1749 end if;
1750
1751 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1752
1753 -- If the context is a Remote_Access_To_Subprogram, access attributes
1754 -- must be resolved with the corresponding fat pointer. There is no need
1755 -- to check for the attribute name since the return type of an
1756 -- attribute is never a remote type.
1757
1758 if Nkind (N) = N_Attribute_Reference
1759 and then Comes_From_Source (N)
1760 and then (Is_Remote_Call_Interface (Typ)
1761 or else Is_Remote_Types (Typ))
1762 then
1763 declare
1764 Attr : constant Attribute_Id :=
1765 Get_Attribute_Id (Attribute_Name (N));
1766 Pref : constant Node_Id := Prefix (N);
1767 Decl : Node_Id;
1768 Spec : Node_Id;
1769 Is_Remote : Boolean := True;
1770
1771 begin
1772 -- Check that Typ is a remote access-to-subprogram type
1773
1774 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1775 -- Prefix (N) must statically denote a remote subprogram
1776 -- declared in a package specification.
1777
1778 if Attr = Attribute_Access then
1779 Decl := Unit_Declaration_Node (Entity (Pref));
1780
1781 if Nkind (Decl) = N_Subprogram_Body then
1782 Spec := Corresponding_Spec (Decl);
1783
1784 if not No (Spec) then
1785 Decl := Unit_Declaration_Node (Spec);
1786 end if;
1787 end if;
1788
1789 Spec := Parent (Decl);
1790
1791 if not Is_Entity_Name (Prefix (N))
1792 or else Nkind (Spec) /= N_Package_Specification
1793 or else
1794 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1795 then
1796 Is_Remote := False;
1797 Error_Msg_N
1798 ("prefix must statically denote a remote subprogram ",
1799 N);
1800 end if;
1801 end if;
1802
1803 -- If we are generating code for a distributed program.
1804 -- perform semantic checks against the corresponding
1805 -- remote entities.
1806
1807 if (Attr = Attribute_Access
1808 or else Attr = Attribute_Unchecked_Access
1809 or else Attr = Attribute_Unrestricted_Access)
1810 and then Expander_Active
1811 and then Get_PCS_Name /= Name_No_DSA
1812 then
1813 Check_Subtype_Conformant
1814 (New_Id => Entity (Prefix (N)),
1815 Old_Id => Designated_Type
1816 (Corresponding_Remote_Type (Typ)),
1817 Err_Loc => N);
1818
1819 if Is_Remote then
1820 Process_Remote_AST_Attribute (N, Typ);
1821 end if;
1822 end if;
1823 end if;
1824 end;
1825 end if;
1826
1827 Debug_A_Entry ("resolving ", N);
1828
1829 if Comes_From_Source (N) then
1830 if Is_Fixed_Point_Type (Typ) then
1831 Check_Restriction (No_Fixed_Point, N);
1832
1833 elsif Is_Floating_Point_Type (Typ)
1834 and then Typ /= Universal_Real
1835 and then Typ /= Any_Real
1836 then
1837 Check_Restriction (No_Floating_Point, N);
1838 end if;
1839 end if;
1840
1841 -- Return if already analyzed
1842
1843 if Analyzed (N) then
1844 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
1845 return;
1846
1847 -- Return if type = Any_Type (previous error encountered)
1848
1849 elsif Etype (N) = Any_Type then
1850 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
1851 return;
1852 end if;
1853
1854 Check_Parameterless_Call (N);
1855
1856 -- If not overloaded, then we know the type, and all that needs doing
1857 -- is to check that this type is compatible with the context.
1858
1859 if not Is_Overloaded (N) then
1860 Found := Covers (Typ, Etype (N));
1861 Expr_Type := Etype (N);
1862
1863 -- In the overloaded case, we must select the interpretation that
1864 -- is compatible with the context (i.e. the type passed to Resolve)
1865
1866 else
1867 -- Loop through possible interpretations
1868
1869 Get_First_Interp (N, I, It);
1870 Interp_Loop : while Present (It.Typ) loop
1871
1872 -- We are only interested in interpretations that are compatible
1873 -- with the expected type, any other interpretations are ignored.
1874
1875 if not Covers (Typ, It.Typ) then
1876 if Debug_Flag_V then
1877 Write_Str (" interpretation incompatible with context");
1878 Write_Eol;
1879 end if;
1880
1881 else
1882 -- Skip the current interpretation if it is disabled by an
1883 -- abstract operator. This action is performed only when the
1884 -- type against which we are resolving is the same as the
1885 -- type of the interpretation.
1886
1887 if Ada_Version >= Ada_05
1888 and then It.Typ = Typ
1889 and then Typ /= Universal_Integer
1890 and then Typ /= Universal_Real
1891 and then Present (It.Abstract_Op)
1892 then
1893 goto Continue;
1894 end if;
1895
1896 -- First matching interpretation
1897
1898 if not Found then
1899 Found := True;
1900 I1 := I;
1901 Seen := It.Nam;
1902 Expr_Type := It.Typ;
1903
1904 -- Matching interpretation that is not the first, maybe an
1905 -- error, but there are some cases where preference rules are
1906 -- used to choose between the two possibilities. These and
1907 -- some more obscure cases are handled in Disambiguate.
1908
1909 else
1910 -- If the current statement is part of a predefined library
1911 -- unit, then all interpretations which come from user level
1912 -- packages should not be considered.
1913
1914 if From_Lib
1915 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
1916 then
1917 goto Continue;
1918 end if;
1919
1920 Error_Msg_Sloc := Sloc (Seen);
1921 It1 := Disambiguate (N, I1, I, Typ);
1922
1923 -- Disambiguation has succeeded. Skip the remaining
1924 -- interpretations.
1925
1926 if It1 /= No_Interp then
1927 Seen := It1.Nam;
1928 Expr_Type := It1.Typ;
1929
1930 while Present (It.Typ) loop
1931 Get_Next_Interp (I, It);
1932 end loop;
1933
1934 else
1935 -- Before we issue an ambiguity complaint, check for
1936 -- the case of a subprogram call where at least one
1937 -- of the arguments is Any_Type, and if so, suppress
1938 -- the message, since it is a cascaded error.
1939
1940 if Nkind_In (N, N_Function_Call,
1941 N_Procedure_Call_Statement)
1942 then
1943 declare
1944 A : Node_Id;
1945 E : Node_Id;
1946
1947 begin
1948 A := First_Actual (N);
1949 while Present (A) loop
1950 E := A;
1951
1952 if Nkind (E) = N_Parameter_Association then
1953 E := Explicit_Actual_Parameter (E);
1954 end if;
1955
1956 if Etype (E) = Any_Type then
1957 if Debug_Flag_V then
1958 Write_Str ("Any_Type in call");
1959 Write_Eol;
1960 end if;
1961
1962 exit Interp_Loop;
1963 end if;
1964
1965 Next_Actual (A);
1966 end loop;
1967 end;
1968
1969 elsif Nkind (N) in N_Binary_Op
1970 and then (Etype (Left_Opnd (N)) = Any_Type
1971 or else Etype (Right_Opnd (N)) = Any_Type)
1972 then
1973 exit Interp_Loop;
1974
1975 elsif Nkind (N) in N_Unary_Op
1976 and then Etype (Right_Opnd (N)) = Any_Type
1977 then
1978 exit Interp_Loop;
1979 end if;
1980
1981 -- Not that special case, so issue message using the
1982 -- flag Ambiguous to control printing of the header
1983 -- message only at the start of an ambiguous set.
1984
1985 if not Ambiguous then
1986 if Nkind (N) = N_Function_Call
1987 and then Nkind (Name (N)) = N_Explicit_Dereference
1988 then
1989 Error_Msg_N
1990 ("ambiguous expression "
1991 & "(cannot resolve indirect call)!", N);
1992 else
1993 Error_Msg_NE
1994 ("ambiguous expression (cannot resolve&)!",
1995 N, It.Nam);
1996 end if;
1997
1998 Ambiguous := True;
1999
2000 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2001 Error_Msg_N
2002 ("\\possible interpretation (inherited)#!", N);
2003 else
2004 Error_Msg_N ("\\possible interpretation#!", N);
2005 end if;
2006 end if;
2007
2008 Error_Msg_Sloc := Sloc (It.Nam);
2009
2010 -- By default, the error message refers to the candidate
2011 -- interpretation. But if it is a predefined operator, it
2012 -- is implicitly declared at the declaration of the type
2013 -- of the operand. Recover the sloc of that declaration
2014 -- for the error message.
2015
2016 if Nkind (N) in N_Op
2017 and then Scope (It.Nam) = Standard_Standard
2018 and then not Is_Overloaded (Right_Opnd (N))
2019 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2020 Standard_Standard
2021 then
2022 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2023
2024 if Comes_From_Source (Err_Type)
2025 and then Present (Parent (Err_Type))
2026 then
2027 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2028 end if;
2029
2030 elsif Nkind (N) in N_Binary_Op
2031 and then Scope (It.Nam) = Standard_Standard
2032 and then not Is_Overloaded (Left_Opnd (N))
2033 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2034 Standard_Standard
2035 then
2036 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2037
2038 if Comes_From_Source (Err_Type)
2039 and then Present (Parent (Err_Type))
2040 then
2041 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2042 end if;
2043
2044 -- If this is an indirect call, use the subprogram_type
2045 -- in the message, to have a meaningful location.
2046 -- Indicate as well if this is an inherited operation,
2047 -- created by a type declaration.
2048
2049 elsif Nkind (N) = N_Function_Call
2050 and then Nkind (Name (N)) = N_Explicit_Dereference
2051 and then Is_Type (It.Nam)
2052 then
2053 Err_Type := It.Nam;
2054 Error_Msg_Sloc :=
2055 Sloc (Associated_Node_For_Itype (Err_Type));
2056 else
2057 Err_Type := Empty;
2058 end if;
2059
2060 if Nkind (N) in N_Op
2061 and then Scope (It.Nam) = Standard_Standard
2062 and then Present (Err_Type)
2063 then
2064 -- Special-case the message for universal_fixed
2065 -- operators, which are not declared with the type
2066 -- of the operand, but appear forever in Standard.
2067
2068 if It.Typ = Universal_Fixed
2069 and then Scope (It.Nam) = Standard_Standard
2070 then
2071 Error_Msg_N
2072 ("\\possible interpretation as " &
2073 "universal_fixed operation " &
2074 "(RM 4.5.5 (19))", N);
2075 else
2076 Error_Msg_N
2077 ("\\possible interpretation (predefined)#!", N);
2078 end if;
2079
2080 elsif
2081 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2082 then
2083 Error_Msg_N
2084 ("\\possible interpretation (inherited)#!", N);
2085 else
2086 Error_Msg_N ("\\possible interpretation#!", N);
2087 end if;
2088
2089 end if;
2090 end if;
2091
2092 -- We have a matching interpretation, Expr_Type is the type
2093 -- from this interpretation, and Seen is the entity.
2094
2095 -- For an operator, just set the entity name. The type will be
2096 -- set by the specific operator resolution routine.
2097
2098 if Nkind (N) in N_Op then
2099 Set_Entity (N, Seen);
2100 Generate_Reference (Seen, N);
2101
2102 elsif Nkind (N) = N_Character_Literal then
2103 Set_Etype (N, Expr_Type);
2104
2105 -- For an explicit dereference, attribute reference, range,
2106 -- short-circuit form (which is not an operator node), or call
2107 -- with a name that is an explicit dereference, there is
2108 -- nothing to be done at this point.
2109
2110 elsif Nkind_In (N, N_Explicit_Dereference,
2111 N_Attribute_Reference,
2112 N_And_Then,
2113 N_Indexed_Component,
2114 N_Or_Else,
2115 N_Range,
2116 N_Selected_Component,
2117 N_Slice)
2118 or else Nkind (Name (N)) = N_Explicit_Dereference
2119 then
2120 null;
2121
2122 -- For procedure or function calls, set the type of the name,
2123 -- and also the entity pointer for the prefix
2124
2125 elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2126 and then (Is_Entity_Name (Name (N))
2127 or else Nkind (Name (N)) = N_Operator_Symbol)
2128 then
2129 Set_Etype (Name (N), Expr_Type);
2130 Set_Entity (Name (N), Seen);
2131 Generate_Reference (Seen, Name (N));
2132
2133 elsif Nkind (N) = N_Function_Call
2134 and then Nkind (Name (N)) = N_Selected_Component
2135 then
2136 Set_Etype (Name (N), Expr_Type);
2137 Set_Entity (Selector_Name (Name (N)), Seen);
2138 Generate_Reference (Seen, Selector_Name (Name (N)));
2139
2140 -- For all other cases, just set the type of the Name
2141
2142 else
2143 Set_Etype (Name (N), Expr_Type);
2144 end if;
2145
2146 end if;
2147
2148 <<Continue>>
2149
2150 -- Move to next interpretation
2151
2152 exit Interp_Loop when No (It.Typ);
2153
2154 Get_Next_Interp (I, It);
2155 end loop Interp_Loop;
2156 end if;
2157
2158 -- At this stage Found indicates whether or not an acceptable
2159 -- interpretation exists. If not, then we have an error, except
2160 -- that if the context is Any_Type as a result of some other error,
2161 -- then we suppress the error report.
2162
2163 if not Found then
2164 if Typ /= Any_Type then
2165
2166 -- If type we are looking for is Void, then this is the procedure
2167 -- call case, and the error is simply that what we gave is not a
2168 -- procedure name (we think of procedure calls as expressions with
2169 -- types internally, but the user doesn't think of them this way!)
2170
2171 if Typ = Standard_Void_Type then
2172
2173 -- Special case message if function used as a procedure
2174
2175 if Nkind (N) = N_Procedure_Call_Statement
2176 and then Is_Entity_Name (Name (N))
2177 and then Ekind (Entity (Name (N))) = E_Function
2178 then
2179 Error_Msg_NE
2180 ("cannot use function & in a procedure call",
2181 Name (N), Entity (Name (N)));
2182
2183 -- Otherwise give general message (not clear what cases this
2184 -- covers, but no harm in providing for them!)
2185
2186 else
2187 Error_Msg_N ("expect procedure name in procedure call", N);
2188 end if;
2189
2190 Found := True;
2191
2192 -- Otherwise we do have a subexpression with the wrong type
2193
2194 -- Check for the case of an allocator which uses an access type
2195 -- instead of the designated type. This is a common error and we
2196 -- specialize the message, posting an error on the operand of the
2197 -- allocator, complaining that we expected the designated type of
2198 -- the allocator.
2199
2200 elsif Nkind (N) = N_Allocator
2201 and then Ekind (Typ) in Access_Kind
2202 and then Ekind (Etype (N)) in Access_Kind
2203 and then Designated_Type (Etype (N)) = Typ
2204 then
2205 Wrong_Type (Expression (N), Designated_Type (Typ));
2206 Found := True;
2207
2208 -- Check for view mismatch on Null in instances, for which the
2209 -- view-swapping mechanism has no identifier.
2210
2211 elsif (In_Instance or else In_Inlined_Body)
2212 and then (Nkind (N) = N_Null)
2213 and then Is_Private_Type (Typ)
2214 and then Is_Access_Type (Full_View (Typ))
2215 then
2216 Resolve (N, Full_View (Typ));
2217 Set_Etype (N, Typ);
2218 return;
2219
2220 -- Check for an aggregate. Sometimes we can get bogus aggregates
2221 -- from misuse of parentheses, and we are about to complain about
2222 -- the aggregate without even looking inside it.
2223
2224 -- Instead, if we have an aggregate of type Any_Composite, then
2225 -- analyze and resolve the component fields, and then only issue
2226 -- another message if we get no errors doing this (otherwise
2227 -- assume that the errors in the aggregate caused the problem).
2228
2229 elsif Nkind (N) = N_Aggregate
2230 and then Etype (N) = Any_Composite
2231 then
2232 -- Disable expansion in any case. If there is a type mismatch
2233 -- it may be fatal to try to expand the aggregate. The flag
2234 -- would otherwise be set to false when the error is posted.
2235
2236 Expander_Active := False;
2237
2238 declare
2239 procedure Check_Aggr (Aggr : Node_Id);
2240 -- Check one aggregate, and set Found to True if we have a
2241 -- definite error in any of its elements
2242
2243 procedure Check_Elmt (Aelmt : Node_Id);
2244 -- Check one element of aggregate and set Found to True if
2245 -- we definitely have an error in the element.
2246
2247 ----------------
2248 -- Check_Aggr --
2249 ----------------
2250
2251 procedure Check_Aggr (Aggr : Node_Id) is
2252 Elmt : Node_Id;
2253
2254 begin
2255 if Present (Expressions (Aggr)) then
2256 Elmt := First (Expressions (Aggr));
2257 while Present (Elmt) loop
2258 Check_Elmt (Elmt);
2259 Next (Elmt);
2260 end loop;
2261 end if;
2262
2263 if Present (Component_Associations (Aggr)) then
2264 Elmt := First (Component_Associations (Aggr));
2265 while Present (Elmt) loop
2266
2267 -- If this is a default-initialized component, then
2268 -- there is nothing to check. The box will be
2269 -- replaced by the appropriate call during late
2270 -- expansion.
2271
2272 if not Box_Present (Elmt) then
2273 Check_Elmt (Expression (Elmt));
2274 end if;
2275
2276 Next (Elmt);
2277 end loop;
2278 end if;
2279 end Check_Aggr;
2280
2281 ----------------
2282 -- Check_Elmt --
2283 ----------------
2284
2285 procedure Check_Elmt (Aelmt : Node_Id) is
2286 begin
2287 -- If we have a nested aggregate, go inside it (to
2288 -- attempt a naked analyze-resolve of the aggregate
2289 -- can cause undesirable cascaded errors). Do not
2290 -- resolve expression if it needs a type from context,
2291 -- as for integer * fixed expression.
2292
2293 if Nkind (Aelmt) = N_Aggregate then
2294 Check_Aggr (Aelmt);
2295
2296 else
2297 Analyze (Aelmt);
2298
2299 if not Is_Overloaded (Aelmt)
2300 and then Etype (Aelmt) /= Any_Fixed
2301 then
2302 Resolve (Aelmt);
2303 end if;
2304
2305 if Etype (Aelmt) = Any_Type then
2306 Found := True;
2307 end if;
2308 end if;
2309 end Check_Elmt;
2310
2311 begin
2312 Check_Aggr (N);
2313 end;
2314 end if;
2315
2316 -- If an error message was issued already, Found got reset
2317 -- to True, so if it is still False, issue the standard
2318 -- Wrong_Type message.
2319
2320 if not Found then
2321 if Is_Overloaded (N)
2322 and then Nkind (N) = N_Function_Call
2323 then
2324 declare
2325 Subp_Name : Node_Id;
2326 begin
2327 if Is_Entity_Name (Name (N)) then
2328 Subp_Name := Name (N);
2329
2330 elsif Nkind (Name (N)) = N_Selected_Component then
2331
2332 -- Protected operation: retrieve operation name
2333
2334 Subp_Name := Selector_Name (Name (N));
2335 else
2336 raise Program_Error;
2337 end if;
2338
2339 Error_Msg_Node_2 := Typ;
2340 Error_Msg_NE ("no visible interpretation of&" &
2341 " matches expected type&", N, Subp_Name);
2342 end;
2343
2344 if All_Errors_Mode then
2345 declare
2346 Index : Interp_Index;
2347 It : Interp;
2348
2349 begin
2350 Error_Msg_N ("\\possible interpretations:", N);
2351
2352 Get_First_Interp (Name (N), Index, It);
2353 while Present (It.Nam) loop
2354 Error_Msg_Sloc := Sloc (It.Nam);
2355 Error_Msg_Node_2 := It.Nam;
2356 Error_Msg_NE
2357 ("\\ type& for & declared#", N, It.Typ);
2358 Get_Next_Interp (Index, It);
2359 end loop;
2360 end;
2361
2362 else
2363 Error_Msg_N ("\use -gnatf for details", N);
2364 end if;
2365 else
2366 Wrong_Type (N, Typ);
2367 end if;
2368 end if;
2369 end if;
2370
2371 Resolution_Failed;
2372 return;
2373
2374 -- Test if we have more than one interpretation for the context
2375
2376 elsif Ambiguous then
2377 Resolution_Failed;
2378 return;
2379
2380 -- Here we have an acceptable interpretation for the context
2381
2382 else
2383 -- Propagate type information and normalize tree for various
2384 -- predefined operations. If the context only imposes a class of
2385 -- types, rather than a specific type, propagate the actual type
2386 -- downward.
2387
2388 if Typ = Any_Integer
2389 or else Typ = Any_Boolean
2390 or else Typ = Any_Modular
2391 or else Typ = Any_Real
2392 or else Typ = Any_Discrete
2393 then
2394 Ctx_Type := Expr_Type;
2395
2396 -- Any_Fixed is legal in a real context only if a specific
2397 -- fixed point type is imposed. If Norman Cohen can be
2398 -- confused by this, it deserves a separate message.
2399
2400 if Typ = Any_Real
2401 and then Expr_Type = Any_Fixed
2402 then
2403 Error_Msg_N ("illegal context for mixed mode operation", N);
2404 Set_Etype (N, Universal_Real);
2405 Ctx_Type := Universal_Real;
2406 end if;
2407 end if;
2408
2409 -- A user-defined operator is transformed into a function call at
2410 -- this point, so that further processing knows that operators are
2411 -- really operators (i.e. are predefined operators). User-defined
2412 -- operators that are intrinsic are just renamings of the predefined
2413 -- ones, and need not be turned into calls either, but if they rename
2414 -- a different operator, we must transform the node accordingly.
2415 -- Instantiations of Unchecked_Conversion are intrinsic but are
2416 -- treated as functions, even if given an operator designator.
2417
2418 if Nkind (N) in N_Op
2419 and then Present (Entity (N))
2420 and then Ekind (Entity (N)) /= E_Operator
2421 then
2422
2423 if not Is_Predefined_Op (Entity (N)) then
2424 Rewrite_Operator_As_Call (N, Entity (N));
2425
2426 elsif Present (Alias (Entity (N)))
2427 and then
2428 Nkind (Parent (Parent (Entity (N)))) =
2429 N_Subprogram_Renaming_Declaration
2430 then
2431 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2432
2433 -- If the node is rewritten, it will be fully resolved in
2434 -- Rewrite_Renamed_Operator.
2435
2436 if Analyzed (N) then
2437 return;
2438 end if;
2439 end if;
2440 end if;
2441
2442 case N_Subexpr'(Nkind (N)) is
2443
2444 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2445
2446 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2447
2448 when N_And_Then | N_Or_Else
2449 => Resolve_Short_Circuit (N, Ctx_Type);
2450
2451 when N_Attribute_Reference
2452 => Resolve_Attribute (N, Ctx_Type);
2453
2454 when N_Character_Literal
2455 => Resolve_Character_Literal (N, Ctx_Type);
2456
2457 when N_Conditional_Expression
2458 => Resolve_Conditional_Expression (N, Ctx_Type);
2459
2460 when N_Expanded_Name
2461 => Resolve_Entity_Name (N, Ctx_Type);
2462
2463 when N_Extension_Aggregate
2464 => Resolve_Extension_Aggregate (N, Ctx_Type);
2465
2466 when N_Explicit_Dereference
2467 => Resolve_Explicit_Dereference (N, Ctx_Type);
2468
2469 when N_Function_Call
2470 => Resolve_Call (N, Ctx_Type);
2471
2472 when N_Identifier
2473 => Resolve_Entity_Name (N, Ctx_Type);
2474
2475 when N_Indexed_Component
2476 => Resolve_Indexed_Component (N, Ctx_Type);
2477
2478 when N_Integer_Literal
2479 => Resolve_Integer_Literal (N, Ctx_Type);
2480
2481 when N_Membership_Test
2482 => Resolve_Membership_Op (N, Ctx_Type);
2483
2484 when N_Null => Resolve_Null (N, Ctx_Type);
2485
2486 when N_Op_And | N_Op_Or | N_Op_Xor
2487 => Resolve_Logical_Op (N, Ctx_Type);
2488
2489 when N_Op_Eq | N_Op_Ne
2490 => Resolve_Equality_Op (N, Ctx_Type);
2491
2492 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2493 => Resolve_Comparison_Op (N, Ctx_Type);
2494
2495 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2496
2497 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2498 N_Op_Divide | N_Op_Mod | N_Op_Rem
2499
2500 => Resolve_Arithmetic_Op (N, Ctx_Type);
2501
2502 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2503
2504 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2505
2506 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2507 => Resolve_Unary_Op (N, Ctx_Type);
2508
2509 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2510
2511 when N_Procedure_Call_Statement
2512 => Resolve_Call (N, Ctx_Type);
2513
2514 when N_Operator_Symbol
2515 => Resolve_Operator_Symbol (N, Ctx_Type);
2516
2517 when N_Qualified_Expression
2518 => Resolve_Qualified_Expression (N, Ctx_Type);
2519
2520 when N_Raise_xxx_Error
2521 => Set_Etype (N, Ctx_Type);
2522
2523 when N_Range => Resolve_Range (N, Ctx_Type);
2524
2525 when N_Real_Literal
2526 => Resolve_Real_Literal (N, Ctx_Type);
2527
2528 when N_Reference => Resolve_Reference (N, Ctx_Type);
2529
2530 when N_Selected_Component
2531 => Resolve_Selected_Component (N, Ctx_Type);
2532
2533 when N_Slice => Resolve_Slice (N, Ctx_Type);
2534
2535 when N_String_Literal
2536 => Resolve_String_Literal (N, Ctx_Type);
2537
2538 when N_Subprogram_Info
2539 => Resolve_Subprogram_Info (N, Ctx_Type);
2540
2541 when N_Type_Conversion
2542 => Resolve_Type_Conversion (N, Ctx_Type);
2543
2544 when N_Unchecked_Expression =>
2545 Resolve_Unchecked_Expression (N, Ctx_Type);
2546
2547 when N_Unchecked_Type_Conversion =>
2548 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2549
2550 end case;
2551
2552 -- If the subexpression was replaced by a non-subexpression, then
2553 -- all we do is to expand it. The only legitimate case we know of
2554 -- is converting procedure call statement to entry call statements,
2555 -- but there may be others, so we are making this test general.
2556
2557 if Nkind (N) not in N_Subexpr then
2558 Debug_A_Exit ("resolving ", N, " (done)");
2559 Expand (N);
2560 return;
2561 end if;
2562
2563 -- The expression is definitely NOT overloaded at this point, so
2564 -- we reset the Is_Overloaded flag to avoid any confusion when
2565 -- reanalyzing the node.
2566
2567 Set_Is_Overloaded (N, False);
2568
2569 -- Freeze expression type, entity if it is a name, and designated
2570 -- type if it is an allocator (RM 13.14(10,11,13)).
2571
2572 -- Now that the resolution of the type of the node is complete,
2573 -- and we did not detect an error, we can expand this node. We
2574 -- skip the expand call if we are in a default expression, see
2575 -- section "Handling of Default Expressions" in Sem spec.
2576
2577 Debug_A_Exit ("resolving ", N, " (done)");
2578
2579 -- We unconditionally freeze the expression, even if we are in
2580 -- default expression mode (the Freeze_Expression routine tests
2581 -- this flag and only freezes static types if it is set).
2582
2583 Freeze_Expression (N);
2584
2585 -- Now we can do the expansion
2586
2587 Expand (N);
2588 end if;
2589 end Resolve;
2590
2591 -------------
2592 -- Resolve --
2593 -------------
2594
2595 -- Version with check(s) suppressed
2596
2597 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2598 begin
2599 if Suppress = All_Checks then
2600 declare
2601 Svg : constant Suppress_Array := Scope_Suppress;
2602 begin
2603 Scope_Suppress := (others => True);
2604 Resolve (N, Typ);
2605 Scope_Suppress := Svg;
2606 end;
2607
2608 else
2609 declare
2610 Svg : constant Boolean := Scope_Suppress (Suppress);
2611 begin
2612 Scope_Suppress (Suppress) := True;
2613 Resolve (N, Typ);
2614 Scope_Suppress (Suppress) := Svg;
2615 end;
2616 end if;
2617 end Resolve;
2618
2619 -------------
2620 -- Resolve --
2621 -------------
2622
2623 -- Version with implicit type
2624
2625 procedure Resolve (N : Node_Id) is
2626 begin
2627 Resolve (N, Etype (N));
2628 end Resolve;
2629
2630 ---------------------
2631 -- Resolve_Actuals --
2632 ---------------------
2633
2634 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2635 Loc : constant Source_Ptr := Sloc (N);
2636 A : Node_Id;
2637 F : Entity_Id;
2638 A_Typ : Entity_Id;
2639 F_Typ : Entity_Id;
2640 Prev : Node_Id := Empty;
2641 Orig_A : Node_Id;
2642
2643 procedure Check_Argument_Order;
2644 -- Performs a check for the case where the actuals are all simple
2645 -- identifiers that correspond to the formal names, but in the wrong
2646 -- order, which is considered suspicious and cause for a warning.
2647
2648 procedure Check_Prefixed_Call;
2649 -- If the original node is an overloaded call in prefix notation,
2650 -- insert an 'Access or a dereference as needed over the first actual.
2651 -- Try_Object_Operation has already verified that there is a valid
2652 -- interpretation, but the form of the actual can only be determined
2653 -- once the primitive operation is identified.
2654
2655 procedure Insert_Default;
2656 -- If the actual is missing in a call, insert in the actuals list
2657 -- an instance of the default expression. The insertion is always
2658 -- a named association.
2659
2660 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2661 -- Check whether T1 and T2, or their full views, are derived from a
2662 -- common type. Used to enforce the restrictions on array conversions
2663 -- of AI95-00246.
2664
2665 --------------------------
2666 -- Check_Argument_Order --
2667 --------------------------
2668
2669 procedure Check_Argument_Order is
2670 begin
2671 -- Nothing to do if no parameters, or original node is neither a
2672 -- function call nor a procedure call statement (happens in the
2673 -- operator-transformed-to-function call case), or the call does
2674 -- not come from source, or this warning is off.
2675
2676 if not Warn_On_Parameter_Order
2677 or else
2678 No (Parameter_Associations (N))
2679 or else
2680 not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2681 N_Function_Call)
2682 or else
2683 not Comes_From_Source (N)
2684 then
2685 return;
2686 end if;
2687
2688 declare
2689 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2690
2691 begin
2692 -- Nothing to do if only one parameter
2693
2694 if Nargs < 2 then
2695 return;
2696 end if;
2697
2698 -- Here if at least two arguments
2699
2700 declare
2701 Actuals : array (1 .. Nargs) of Node_Id;
2702 Actual : Node_Id;
2703 Formal : Node_Id;
2704
2705 Wrong_Order : Boolean := False;
2706 -- Set True if an out of order case is found
2707
2708 begin
2709 -- Collect identifier names of actuals, fail if any actual is
2710 -- not a simple identifier, and record max length of name.
2711
2712 Actual := First (Parameter_Associations (N));
2713 for J in Actuals'Range loop
2714 if Nkind (Actual) /= N_Identifier then
2715 return;
2716 else
2717 Actuals (J) := Actual;
2718 Next (Actual);
2719 end if;
2720 end loop;
2721
2722 -- If we got this far, all actuals are identifiers and the list
2723 -- of their names is stored in the Actuals array.
2724
2725 Formal := First_Formal (Nam);
2726 for J in Actuals'Range loop
2727
2728 -- If we ran out of formals, that's odd, probably an error
2729 -- which will be detected elsewhere, but abandon the search.
2730
2731 if No (Formal) then
2732 return;
2733 end if;
2734
2735 -- If name matches and is in order OK
2736
2737 if Chars (Formal) = Chars (Actuals (J)) then
2738 null;
2739
2740 else
2741 -- If no match, see if it is elsewhere in list and if so
2742 -- flag potential wrong order if type is compatible.
2743
2744 for K in Actuals'Range loop
2745 if Chars (Formal) = Chars (Actuals (K))
2746 and then
2747 Has_Compatible_Type (Actuals (K), Etype (Formal))
2748 then
2749 Wrong_Order := True;
2750 goto Continue;
2751 end if;
2752 end loop;
2753
2754 -- No match
2755
2756 return;
2757 end if;
2758
2759 <<Continue>> Next_Formal (Formal);
2760 end loop;
2761
2762 -- If Formals left over, also probably an error, skip warning
2763
2764 if Present (Formal) then
2765 return;
2766 end if;
2767
2768 -- Here we give the warning if something was out of order
2769
2770 if Wrong_Order then
2771 Error_Msg_N
2772 ("actuals for this call may be in wrong order?", N);
2773 end if;
2774 end;
2775 end;
2776 end Check_Argument_Order;
2777
2778 -------------------------
2779 -- Check_Prefixed_Call --
2780 -------------------------
2781
2782 procedure Check_Prefixed_Call is
2783 Act : constant Node_Id := First_Actual (N);
2784 A_Type : constant Entity_Id := Etype (Act);
2785 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2786 Orig : constant Node_Id := Original_Node (N);
2787 New_A : Node_Id;
2788
2789 begin
2790 -- Check whether the call is a prefixed call, with or without
2791 -- additional actuals.
2792
2793 if Nkind (Orig) = N_Selected_Component
2794 or else
2795 (Nkind (Orig) = N_Indexed_Component
2796 and then Nkind (Prefix (Orig)) = N_Selected_Component
2797 and then Is_Entity_Name (Prefix (Prefix (Orig)))
2798 and then Is_Entity_Name (Act)
2799 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
2800 then
2801 if Is_Access_Type (A_Type)
2802 and then not Is_Access_Type (F_Type)
2803 then
2804 -- Introduce dereference on object in prefix
2805
2806 New_A :=
2807 Make_Explicit_Dereference (Sloc (Act),
2808 Prefix => Relocate_Node (Act));
2809 Rewrite (Act, New_A);
2810 Analyze (Act);
2811
2812 elsif Is_Access_Type (F_Type)
2813 and then not Is_Access_Type (A_Type)
2814 then
2815 -- Introduce an implicit 'Access in prefix
2816
2817 if not Is_Aliased_View (Act) then
2818 Error_Msg_NE
2819 ("object in prefixed call to& must be aliased"
2820 & " (RM-2005 4.3.1 (13))",
2821 Prefix (Act), Nam);
2822 end if;
2823
2824 Rewrite (Act,
2825 Make_Attribute_Reference (Loc,
2826 Attribute_Name => Name_Access,
2827 Prefix => Relocate_Node (Act)));
2828 end if;
2829
2830 Analyze (Act);
2831 end if;
2832 end Check_Prefixed_Call;
2833
2834 --------------------
2835 -- Insert_Default --
2836 --------------------
2837
2838 procedure Insert_Default is
2839 Actval : Node_Id;
2840 Assoc : Node_Id;
2841
2842 begin
2843 -- Missing argument in call, nothing to insert
2844
2845 if No (Default_Value (F)) then
2846 return;
2847
2848 else
2849 -- Note that we do a full New_Copy_Tree, so that any associated
2850 -- Itypes are properly copied. This may not be needed any more,
2851 -- but it does no harm as a safety measure! Defaults of a generic
2852 -- formal may be out of bounds of the corresponding actual (see
2853 -- cc1311b) and an additional check may be required.
2854
2855 Actval :=
2856 New_Copy_Tree
2857 (Default_Value (F),
2858 New_Scope => Current_Scope,
2859 New_Sloc => Loc);
2860
2861 if Is_Concurrent_Type (Scope (Nam))
2862 and then Has_Discriminants (Scope (Nam))
2863 then
2864 Replace_Actual_Discriminants (N, Actval);
2865 end if;
2866
2867 if Is_Overloadable (Nam)
2868 and then Present (Alias (Nam))
2869 then
2870 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
2871 and then not Is_Tagged_Type (Etype (F))
2872 then
2873 -- If default is a real literal, do not introduce a
2874 -- conversion whose effect may depend on the run-time
2875 -- size of universal real.
2876
2877 if Nkind (Actval) = N_Real_Literal then
2878 Set_Etype (Actval, Base_Type (Etype (F)));
2879 else
2880 Actval := Unchecked_Convert_To (Etype (F), Actval);
2881 end if;
2882 end if;
2883
2884 if Is_Scalar_Type (Etype (F)) then
2885 Enable_Range_Check (Actval);
2886 end if;
2887
2888 Set_Parent (Actval, N);
2889
2890 -- Resolve aggregates with their base type, to avoid scope
2891 -- anomalies: the subtype was first built in the subprogram
2892 -- declaration, and the current call may be nested.
2893
2894 if Nkind (Actval) = N_Aggregate
2895 and then Has_Discriminants (Etype (Actval))
2896 then
2897 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2898 else
2899 Analyze_And_Resolve (Actval, Etype (Actval));
2900 end if;
2901
2902 else
2903 Set_Parent (Actval, N);
2904
2905 -- See note above concerning aggregates
2906
2907 if Nkind (Actval) = N_Aggregate
2908 and then Has_Discriminants (Etype (Actval))
2909 then
2910 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2911
2912 -- Resolve entities with their own type, which may differ
2913 -- from the type of a reference in a generic context (the
2914 -- view swapping mechanism did not anticipate the re-analysis
2915 -- of default values in calls).
2916
2917 elsif Is_Entity_Name (Actval) then
2918 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
2919
2920 else
2921 Analyze_And_Resolve (Actval, Etype (Actval));
2922 end if;
2923 end if;
2924
2925 -- If default is a tag indeterminate function call, propagate
2926 -- tag to obtain proper dispatching.
2927
2928 if Is_Controlling_Formal (F)
2929 and then Nkind (Default_Value (F)) = N_Function_Call
2930 then
2931 Set_Is_Controlling_Actual (Actval);
2932 end if;
2933
2934 end if;
2935
2936 -- If the default expression raises constraint error, then just
2937 -- silently replace it with an N_Raise_Constraint_Error node,
2938 -- since we already gave the warning on the subprogram spec.
2939
2940 if Raises_Constraint_Error (Actval) then
2941 Rewrite (Actval,
2942 Make_Raise_Constraint_Error (Loc,
2943 Reason => CE_Range_Check_Failed));
2944 Set_Raises_Constraint_Error (Actval);
2945 Set_Etype (Actval, Etype (F));
2946 end if;
2947
2948 Assoc :=
2949 Make_Parameter_Association (Loc,
2950 Explicit_Actual_Parameter => Actval,
2951 Selector_Name => Make_Identifier (Loc, Chars (F)));
2952
2953 -- Case of insertion is first named actual
2954
2955 if No (Prev) or else
2956 Nkind (Parent (Prev)) /= N_Parameter_Association
2957 then
2958 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
2959 Set_First_Named_Actual (N, Actval);
2960
2961 if No (Prev) then
2962 if No (Parameter_Associations (N)) then
2963 Set_Parameter_Associations (N, New_List (Assoc));
2964 else
2965 Append (Assoc, Parameter_Associations (N));
2966 end if;
2967
2968 else
2969 Insert_After (Prev, Assoc);
2970 end if;
2971
2972 -- Case of insertion is not first named actual
2973
2974 else
2975 Set_Next_Named_Actual
2976 (Assoc, Next_Named_Actual (Parent (Prev)));
2977 Set_Next_Named_Actual (Parent (Prev), Actval);
2978 Append (Assoc, Parameter_Associations (N));
2979 end if;
2980
2981 Mark_Rewrite_Insertion (Assoc);
2982 Mark_Rewrite_Insertion (Actval);
2983
2984 Prev := Actval;
2985 end Insert_Default;
2986
2987 -------------------
2988 -- Same_Ancestor --
2989 -------------------
2990
2991 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
2992 FT1 : Entity_Id := T1;
2993 FT2 : Entity_Id := T2;
2994
2995 begin
2996 if Is_Private_Type (T1)
2997 and then Present (Full_View (T1))
2998 then
2999 FT1 := Full_View (T1);
3000 end if;
3001
3002 if Is_Private_Type (T2)
3003 and then Present (Full_View (T2))
3004 then
3005 FT2 := Full_View (T2);
3006 end if;
3007
3008 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3009 end Same_Ancestor;
3010
3011 -- Start of processing for Resolve_Actuals
3012
3013 begin
3014 Check_Argument_Order;
3015
3016 if Present (First_Actual (N)) then
3017 Check_Prefixed_Call;
3018 end if;
3019
3020 A := First_Actual (N);
3021 F := First_Formal (Nam);
3022 while Present (F) loop
3023 if No (A) and then Needs_No_Actuals (Nam) then
3024 null;
3025
3026 -- If we have an error in any actual or formal, indicated by
3027 -- a type of Any_Type, then abandon resolution attempt, and
3028 -- set result type to Any_Type.
3029
3030 elsif (Present (A) and then Etype (A) = Any_Type)
3031 or else Etype (F) = Any_Type
3032 then
3033 Set_Etype (N, Any_Type);
3034 return;
3035 end if;
3036
3037 -- Case where actual is present
3038
3039 -- If the actual is an entity, generate a reference to it now. We
3040 -- do this before the actual is resolved, because a formal of some
3041 -- protected subprogram, or a task discriminant, will be rewritten
3042 -- during expansion, and the reference to the source entity may
3043 -- be lost.
3044
3045 if Present (A)
3046 and then Is_Entity_Name (A)
3047 and then Comes_From_Source (N)
3048 then
3049 Orig_A := Entity (A);
3050
3051 if Present (Orig_A) then
3052 if Is_Formal (Orig_A)
3053 and then Ekind (F) /= E_In_Parameter
3054 then
3055 Generate_Reference (Orig_A, A, 'm');
3056 elsif not Is_Overloaded (A) then
3057 Generate_Reference (Orig_A, A);
3058 end if;
3059 end if;
3060 end if;
3061
3062 if Present (A)
3063 and then (Nkind (Parent (A)) /= N_Parameter_Association
3064 or else
3065 Chars (Selector_Name (Parent (A))) = Chars (F))
3066 then
3067 -- If style checking mode on, check match of formal name
3068
3069 if Style_Check then
3070 if Nkind (Parent (A)) = N_Parameter_Association then
3071 Check_Identifier (Selector_Name (Parent (A)), F);
3072 end if;
3073 end if;
3074
3075 -- If the formal is Out or In_Out, do not resolve and expand the
3076 -- conversion, because it is subsequently expanded into explicit
3077 -- temporaries and assignments. However, the object of the
3078 -- conversion can be resolved. An exception is the case of tagged
3079 -- type conversion with a class-wide actual. In that case we want
3080 -- the tag check to occur and no temporary will be needed (no
3081 -- representation change can occur) and the parameter is passed by
3082 -- reference, so we go ahead and resolve the type conversion.
3083 -- Another exception is the case of reference to component or
3084 -- subcomponent of a bit-packed array, in which case we want to
3085 -- defer expansion to the point the in and out assignments are
3086 -- performed.
3087
3088 if Ekind (F) /= E_In_Parameter
3089 and then Nkind (A) = N_Type_Conversion
3090 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3091 then
3092 if Ekind (F) = E_In_Out_Parameter
3093 and then Is_Array_Type (Etype (F))
3094 then
3095 if Has_Aliased_Components (Etype (Expression (A)))
3096 /= Has_Aliased_Components (Etype (F))
3097 then
3098
3099 -- In a view conversion, the conversion must be legal in
3100 -- both directions, and thus both component types must be
3101 -- aliased, or neither (4.6 (8)).
3102
3103 -- The additional rule 4.6 (24.9.2) seems unduly
3104 -- restrictive: the privacy requirement should not
3105 -- apply to generic types, and should be checked in
3106 -- an instance. ARG query is in order.
3107
3108 Error_Msg_N
3109 ("both component types in a view conversion must be"
3110 & " aliased, or neither", A);
3111
3112 elsif
3113 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3114 then
3115 if Is_By_Reference_Type (Etype (F))
3116 or else Is_By_Reference_Type (Etype (Expression (A)))
3117 then
3118 Error_Msg_N
3119 ("view conversion between unrelated by reference " &
3120 "array types not allowed (\'A'I-00246)", A);
3121 else
3122 declare
3123 Comp_Type : constant Entity_Id :=
3124 Component_Type
3125 (Etype (Expression (A)));
3126 begin
3127 if Comes_From_Source (A)
3128 and then Ada_Version >= Ada_05
3129 and then
3130 ((Is_Private_Type (Comp_Type)
3131 and then not Is_Generic_Type (Comp_Type))
3132 or else Is_Tagged_Type (Comp_Type)
3133 or else Is_Volatile (Comp_Type))
3134 then
3135 Error_Msg_N
3136 ("component type of a view conversion cannot"
3137 & " be private, tagged, or volatile"
3138 & " (RM 4.6 (24))",
3139 Expression (A));
3140 end if;
3141 end;
3142 end if;
3143 end if;
3144 end if;
3145
3146 if (Conversion_OK (A)
3147 or else Valid_Conversion (A, Etype (A), Expression (A)))
3148 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3149 then
3150 Resolve (Expression (A));
3151 end if;
3152
3153 -- If the actual is a function call that returns a limited
3154 -- unconstrained object that needs finalization, create a
3155 -- transient scope for it, so that it can receive the proper
3156 -- finalization list.
3157
3158 elsif Nkind (A) = N_Function_Call
3159 and then Is_Limited_Record (Etype (F))
3160 and then not Is_Constrained (Etype (F))
3161 and then Expander_Active
3162 and then
3163 (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3164 then
3165 Establish_Transient_Scope (A, False);
3166
3167 else
3168 if Nkind (A) = N_Type_Conversion
3169 and then Is_Array_Type (Etype (F))
3170 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3171 and then
3172 (Is_Limited_Type (Etype (F))
3173 or else Is_Limited_Type (Etype (Expression (A))))
3174 then
3175 Error_Msg_N
3176 ("conversion between unrelated limited array types " &
3177 "not allowed (\A\I-00246)", A);
3178
3179 if Is_Limited_Type (Etype (F)) then
3180 Explain_Limited_Type (Etype (F), A);
3181 end if;
3182
3183 if Is_Limited_Type (Etype (Expression (A))) then
3184 Explain_Limited_Type (Etype (Expression (A)), A);
3185 end if;
3186 end if;
3187
3188 -- (Ada 2005: AI-251): If the actual is an allocator whose
3189 -- directly designated type is a class-wide interface, we build
3190 -- an anonymous access type to use it as the type of the
3191 -- allocator. Later, when the subprogram call is expanded, if
3192 -- the interface has a secondary dispatch table the expander
3193 -- will add a type conversion to force the correct displacement
3194 -- of the pointer.
3195
3196 if Nkind (A) = N_Allocator then
3197 declare
3198 DDT : constant Entity_Id :=
3199 Directly_Designated_Type (Base_Type (Etype (F)));
3200
3201 New_Itype : Entity_Id;
3202
3203 begin
3204 if Is_Class_Wide_Type (DDT)
3205 and then Is_Interface (DDT)
3206 then
3207 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3208 Set_Etype (New_Itype, Etype (A));
3209 Set_Directly_Designated_Type (New_Itype,
3210 Directly_Designated_Type (Etype (A)));
3211 Set_Etype (A, New_Itype);
3212 end if;
3213
3214 -- Ada 2005, AI-162:If the actual is an allocator, the
3215 -- innermost enclosing statement is the master of the
3216 -- created object. This needs to be done with expansion
3217 -- enabled only, otherwise the transient scope will not
3218 -- be removed in the expansion of the wrapped construct.
3219
3220 if (Is_Controlled (DDT) or else Has_Task (DDT))
3221 and then Expander_Active
3222 then
3223 Establish_Transient_Scope (A, False);
3224 end if;
3225 end;
3226 end if;
3227
3228 -- (Ada 2005): The call may be to a primitive operation of
3229 -- a tagged synchronized type, declared outside of the type.
3230 -- In this case the controlling actual must be converted to
3231 -- its corresponding record type, which is the formal type.
3232 -- The actual may be a subtype, either because of a constraint
3233 -- or because it is a generic actual, so use base type to
3234 -- locate concurrent type.
3235
3236 A_Typ := Base_Type (Etype (A));
3237 F_Typ := Base_Type (Etype (F));
3238
3239 declare
3240 Full_A_Typ : Entity_Id;
3241
3242 begin
3243 if Present (Full_View (A_Typ)) then
3244 Full_A_Typ := Base_Type (Full_View (A_Typ));
3245 else
3246 Full_A_Typ := A_Typ;
3247 end if;
3248
3249 -- Tagged synchronized type (case 1): the actual is a
3250 -- concurrent type
3251
3252 if Is_Concurrent_Type (A_Typ)
3253 and then Corresponding_Record_Type (A_Typ) = F_Typ
3254 then
3255 Rewrite (A,
3256 Unchecked_Convert_To
3257 (Corresponding_Record_Type (A_Typ), A));
3258 Resolve (A, Etype (F));
3259
3260 -- Tagged synchronized type (case 2): the formal is a
3261 -- concurrent type
3262
3263 elsif Ekind (Full_A_Typ) = E_Record_Type
3264 and then Present
3265 (Corresponding_Concurrent_Type (Full_A_Typ))
3266 and then Is_Concurrent_Type (F_Typ)
3267 and then Present (Corresponding_Record_Type (F_Typ))
3268 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3269 then
3270 Resolve (A, Corresponding_Record_Type (F_Typ));
3271
3272 -- Common case
3273
3274 else
3275 Resolve (A, Etype (F));
3276 end if;
3277 end;
3278 end if;
3279
3280 A_Typ := Etype (A);
3281 F_Typ := Etype (F);
3282
3283 -- For mode IN, if actual is an entity, and the type of the formal
3284 -- has warnings suppressed, then we reset Never_Set_In_Source for
3285 -- the calling entity. The reason for this is to catch cases like
3286 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3287 -- uses trickery to modify an IN parameter.
3288
3289 if Ekind (F) = E_In_Parameter
3290 and then Is_Entity_Name (A)
3291 and then Present (Entity (A))
3292 and then Ekind (Entity (A)) = E_Variable
3293 and then Has_Warnings_Off (F_Typ)
3294 then
3295 Set_Never_Set_In_Source (Entity (A), False);
3296 end if;
3297
3298 -- Perform error checks for IN and IN OUT parameters
3299
3300 if Ekind (F) /= E_Out_Parameter then
3301
3302 -- Check unset reference. For scalar parameters, it is clearly
3303 -- wrong to pass an uninitialized value as either an IN or
3304 -- IN-OUT parameter. For composites, it is also clearly an
3305 -- error to pass a completely uninitialized value as an IN
3306 -- parameter, but the case of IN OUT is trickier. We prefer
3307 -- not to give a warning here. For example, suppose there is
3308 -- a routine that sets some component of a record to False.
3309 -- It is perfectly reasonable to make this IN-OUT and allow
3310 -- either initialized or uninitialized records to be passed
3311 -- in this case.
3312
3313 -- For partially initialized composite values, we also avoid
3314 -- warnings, since it is quite likely that we are passing a
3315 -- partially initialized value and only the initialized fields
3316 -- will in fact be read in the subprogram.
3317
3318 if Is_Scalar_Type (A_Typ)
3319 or else (Ekind (F) = E_In_Parameter
3320 and then not Is_Partially_Initialized_Type (A_Typ))
3321 then
3322 Check_Unset_Reference (A);
3323 end if;
3324
3325 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3326 -- actual to a nested call, since this is case of reading an
3327 -- out parameter, which is not allowed.
3328
3329 if Ada_Version = Ada_83
3330 and then Is_Entity_Name (A)
3331 and then Ekind (Entity (A)) = E_Out_Parameter
3332 then
3333 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3334 end if;
3335 end if;
3336
3337 -- Case of OUT or IN OUT parameter
3338
3339 if Ekind (F) /= E_In_Parameter then
3340
3341 -- For an Out parameter, check for useless assignment. Note
3342 -- that we can't set Last_Assignment this early, because we may
3343 -- kill current values in Resolve_Call, and that call would
3344 -- clobber the Last_Assignment field.
3345
3346 -- Note: call Warn_On_Useless_Assignment before doing the check
3347 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3348 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3349 -- reflects the last assignment, not this one!
3350
3351 if Ekind (F) = E_Out_Parameter then
3352 if Warn_On_Modified_As_Out_Parameter (F)
3353 and then Is_Entity_Name (A)
3354 and then Present (Entity (A))
3355 and then Comes_From_Source (N)
3356 then
3357 Warn_On_Useless_Assignment (Entity (A), A);
3358 end if;
3359 end if;
3360
3361 -- Validate the form of the actual. Note that the call to
3362 -- Is_OK_Variable_For_Out_Formal generates the required
3363 -- reference in this case.
3364
3365 if not Is_OK_Variable_For_Out_Formal (A) then
3366 Error_Msg_NE ("actual for& must be a variable", A, F);
3367 end if;
3368
3369 -- What's the following about???
3370
3371 if Is_Entity_Name (A) then
3372 Kill_Checks (Entity (A));
3373 else
3374 Kill_All_Checks;
3375 end if;
3376 end if;
3377
3378 if Etype (A) = Any_Type then
3379 Set_Etype (N, Any_Type);
3380 return;
3381 end if;
3382
3383 -- Apply appropriate range checks for in, out, and in-out
3384 -- parameters. Out and in-out parameters also need a separate
3385 -- check, if there is a type conversion, to make sure the return
3386 -- value meets the constraints of the variable before the
3387 -- conversion.
3388
3389 -- Gigi looks at the check flag and uses the appropriate types.
3390 -- For now since one flag is used there is an optimization which
3391 -- might not be done in the In Out case since Gigi does not do
3392 -- any analysis. More thought required about this ???
3393
3394 if Ekind (F) = E_In_Parameter
3395 or else Ekind (F) = E_In_Out_Parameter
3396 then
3397 if Is_Scalar_Type (Etype (A)) then
3398 Apply_Scalar_Range_Check (A, F_Typ);
3399
3400 elsif Is_Array_Type (Etype (A)) then
3401 Apply_Length_Check (A, F_Typ);
3402
3403 elsif Is_Record_Type (F_Typ)
3404 and then Has_Discriminants (F_Typ)
3405 and then Is_Constrained (F_Typ)
3406 and then (not Is_Derived_Type (F_Typ)
3407 or else Comes_From_Source (Nam))
3408 then
3409 Apply_Discriminant_Check (A, F_Typ);
3410
3411 elsif Is_Access_Type (F_Typ)
3412 and then Is_Array_Type (Designated_Type (F_Typ))
3413 and then Is_Constrained (Designated_Type (F_Typ))
3414 then
3415 Apply_Length_Check (A, F_Typ);
3416
3417 elsif Is_Access_Type (F_Typ)
3418 and then Has_Discriminants (Designated_Type (F_Typ))
3419 and then Is_Constrained (Designated_Type (F_Typ))
3420 then
3421 Apply_Discriminant_Check (A, F_Typ);
3422
3423 else
3424 Apply_Range_Check (A, F_Typ);
3425 end if;
3426
3427 -- Ada 2005 (AI-231)
3428
3429 if Ada_Version >= Ada_05
3430 and then Is_Access_Type (F_Typ)
3431 and then Can_Never_Be_Null (F_Typ)
3432 and then Known_Null (A)
3433 then
3434 Apply_Compile_Time_Constraint_Error
3435 (N => A,
3436 Msg => "(Ada 2005) null not allowed in "
3437 & "null-excluding formal?",
3438 Reason => CE_Null_Not_Allowed);
3439 end if;
3440 end if;
3441
3442 if Ekind (F) = E_Out_Parameter
3443 or else Ekind (F) = E_In_Out_Parameter
3444 then
3445 if Nkind (A) = N_Type_Conversion then
3446 if Is_Scalar_Type (A_Typ) then
3447 Apply_Scalar_Range_Check
3448 (Expression (A), Etype (Expression (A)), A_Typ);
3449 else
3450 Apply_Range_Check
3451 (Expression (A), Etype (Expression (A)), A_Typ);
3452 end if;
3453
3454 else
3455 if Is_Scalar_Type (F_Typ) then
3456 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3457
3458 elsif Is_Array_Type (F_Typ)
3459 and then Ekind (F) = E_Out_Parameter
3460 then
3461 Apply_Length_Check (A, F_Typ);
3462
3463 else
3464 Apply_Range_Check (A, A_Typ, F_Typ);
3465 end if;
3466 end if;
3467 end if;
3468
3469 -- An actual associated with an access parameter is implicitly
3470 -- converted to the anonymous access type of the formal and must
3471 -- satisfy the legality checks for access conversions.
3472
3473 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3474 if not Valid_Conversion (A, F_Typ, A) then
3475 Error_Msg_N
3476 ("invalid implicit conversion for access parameter", A);
3477 end if;
3478 end if;
3479
3480 -- Check bad case of atomic/volatile argument (RM C.6(12))
3481
3482 if Is_By_Reference_Type (Etype (F))
3483 and then Comes_From_Source (N)
3484 then
3485 if Is_Atomic_Object (A)
3486 and then not Is_Atomic (Etype (F))
3487 then
3488 Error_Msg_N
3489 ("cannot pass atomic argument to non-atomic formal",
3490 N);
3491
3492 elsif Is_Volatile_Object (A)
3493 and then not Is_Volatile (Etype (F))
3494 then
3495 Error_Msg_N
3496 ("cannot pass volatile argument to non-volatile formal",
3497 N);
3498 end if;
3499 end if;
3500
3501 -- Check that subprograms don't have improper controlling
3502 -- arguments (RM 3.9.2 (9))
3503
3504 -- A primitive operation may have an access parameter of an
3505 -- incomplete tagged type, but a dispatching call is illegal
3506 -- if the type is still incomplete.
3507
3508 if Is_Controlling_Formal (F) then
3509 Set_Is_Controlling_Actual (A);
3510
3511 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3512 declare
3513 Desig : constant Entity_Id := Designated_Type (Etype (F));
3514 begin
3515 if Ekind (Desig) = E_Incomplete_Type
3516 and then No (Full_View (Desig))
3517 and then No (Non_Limited_View (Desig))
3518 then
3519 Error_Msg_NE
3520 ("premature use of incomplete type& " &
3521 "in dispatching call", A, Desig);
3522 end if;
3523 end;
3524 end if;
3525
3526 elsif Nkind (A) = N_Explicit_Dereference then
3527 Validate_Remote_Access_To_Class_Wide_Type (A);
3528 end if;
3529
3530 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3531 and then not Is_Class_Wide_Type (F_Typ)
3532 and then not Is_Controlling_Formal (F)
3533 then
3534 Error_Msg_N ("class-wide argument not allowed here!", A);
3535
3536 if Is_Subprogram (Nam)
3537 and then Comes_From_Source (Nam)
3538 then
3539 Error_Msg_Node_2 := F_Typ;
3540 Error_Msg_NE
3541 ("& is not a dispatching operation of &!", A, Nam);
3542 end if;
3543
3544 elsif Is_Access_Type (A_Typ)
3545 and then Is_Access_Type (F_Typ)
3546 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3547 and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3548 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3549 or else (Nkind (A) = N_Attribute_Reference
3550 and then
3551 Is_Class_Wide_Type (Etype (Prefix (A)))))
3552 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3553 and then not Is_Controlling_Formal (F)
3554 then
3555 Error_Msg_N
3556 ("access to class-wide argument not allowed here!", A);
3557
3558 if Is_Subprogram (Nam)
3559 and then Comes_From_Source (Nam)
3560 then
3561 Error_Msg_Node_2 := Designated_Type (F_Typ);
3562 Error_Msg_NE
3563 ("& is not a dispatching operation of &!", A, Nam);
3564 end if;
3565 end if;
3566
3567 Eval_Actual (A);
3568
3569 -- If it is a named association, treat the selector_name as
3570 -- a proper identifier, and mark the corresponding entity.
3571
3572 if Nkind (Parent (A)) = N_Parameter_Association then
3573 Set_Entity (Selector_Name (Parent (A)), F);
3574 Generate_Reference (F, Selector_Name (Parent (A)));
3575 Set_Etype (Selector_Name (Parent (A)), F_Typ);
3576 Generate_Reference (F_Typ, N, ' ');
3577 end if;
3578
3579 Prev := A;
3580
3581 if Ekind (F) /= E_Out_Parameter then
3582 Check_Unset_Reference (A);
3583 end if;
3584
3585 Next_Actual (A);
3586
3587 -- Case where actual is not present
3588
3589 else
3590 Insert_Default;
3591 end if;
3592
3593 Next_Formal (F);
3594 end loop;
3595 end Resolve_Actuals;
3596
3597 -----------------------
3598 -- Resolve_Allocator --
3599 -----------------------
3600
3601 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
3602 E : constant Node_Id := Expression (N);
3603 Subtyp : Entity_Id;
3604 Discrim : Entity_Id;
3605 Constr : Node_Id;
3606 Aggr : Node_Id;
3607 Assoc : Node_Id := Empty;
3608 Disc_Exp : Node_Id;
3609
3610 procedure Check_Allocator_Discrim_Accessibility
3611 (Disc_Exp : Node_Id;
3612 Alloc_Typ : Entity_Id);
3613 -- Check that accessibility level associated with an access discriminant
3614 -- initialized in an allocator by the expression Disc_Exp is not deeper
3615 -- than the level of the allocator type Alloc_Typ. An error message is
3616 -- issued if this condition is violated. Specialized checks are done for
3617 -- the cases of a constraint expression which is an access attribute or
3618 -- an access discriminant.
3619
3620 function In_Dispatching_Context return Boolean;
3621 -- If the allocator is an actual in a call, it is allowed to be class-
3622 -- wide when the context is not because it is a controlling actual.
3623
3624 procedure Propagate_Coextensions (Root : Node_Id);
3625 -- Propagate all nested coextensions which are located one nesting
3626 -- level down the tree to the node Root. Example:
3627 --
3628 -- Top_Record
3629 -- Level_1_Coextension
3630 -- Level_2_Coextension
3631 --
3632 -- The algorithm is paired with delay actions done by the Expander. In
3633 -- the above example, assume all coextensions are controlled types.
3634 -- The cycle of analysis, resolution and expansion will yield:
3635 --
3636 -- 1) Analyze Top_Record
3637 -- 2) Analyze Level_1_Coextension
3638 -- 3) Analyze Level_2_Coextension
3639 -- 4) Resolve Level_2_Coextension. The allocator is marked as a
3640 -- coextension.
3641 -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is
3642 -- generated to capture the allocated object. Temp_1 is attached
3643 -- to the coextension chain of Level_2_Coextension.
3644 -- 6) Resolve Level_1_Coextension. The allocator is marked as a
3645 -- coextension. A forward tree traversal is performed which finds
3646 -- Level_2_Coextension's list and copies its contents into its
3647 -- own list.
3648 -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is
3649 -- generated to capture the allocated object. Temp_2 is attached
3650 -- to the coextension chain of Level_1_Coextension. Currently, the
3651 -- contents of the list are [Temp_2, Temp_1].
3652 -- 8) Resolve Top_Record. A forward tree traversal is performed which
3653 -- finds Level_1_Coextension's list and copies its contents into
3654 -- its own list.
3655 -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and
3656 -- Temp_2 and attach them to Top_Record's finalization list.
3657
3658 -------------------------------------------
3659 -- Check_Allocator_Discrim_Accessibility --
3660 -------------------------------------------
3661
3662 procedure Check_Allocator_Discrim_Accessibility
3663 (Disc_Exp : Node_Id;
3664 Alloc_Typ : Entity_Id)
3665 is
3666 begin
3667 if Type_Access_Level (Etype (Disc_Exp)) >
3668 Type_Access_Level (Alloc_Typ)
3669 then
3670 Error_Msg_N
3671 ("operand type has deeper level than allocator type", Disc_Exp);
3672
3673 -- When the expression is an Access attribute the level of the prefix
3674 -- object must not be deeper than that of the allocator's type.
3675
3676 elsif Nkind (Disc_Exp) = N_Attribute_Reference
3677 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
3678 = Attribute_Access
3679 and then Object_Access_Level (Prefix (Disc_Exp))
3680 > Type_Access_Level (Alloc_Typ)
3681 then
3682 Error_Msg_N
3683 ("prefix of attribute has deeper level than allocator type",
3684 Disc_Exp);
3685
3686 -- When the expression is an access discriminant the check is against
3687 -- the level of the prefix object.
3688
3689 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
3690 and then Nkind (Disc_Exp) = N_Selected_Component
3691 and then Object_Access_Level (Prefix (Disc_Exp))
3692 > Type_Access_Level (Alloc_Typ)
3693 then
3694 Error_Msg_N
3695 ("access discriminant has deeper level than allocator type",
3696 Disc_Exp);
3697
3698 -- All other cases are legal
3699
3700 else
3701 null;
3702 end if;
3703 end Check_Allocator_Discrim_Accessibility;
3704
3705 ----------------------------
3706 -- In_Dispatching_Context --
3707 ----------------------------
3708
3709 function In_Dispatching_Context return Boolean is
3710 Par : constant Node_Id := Parent (N);
3711 begin
3712 return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
3713 and then Is_Entity_Name (Name (Par))
3714 and then Is_Dispatching_Operation (Entity (Name (Par)));
3715 end In_Dispatching_Context;
3716
3717 ----------------------------
3718 -- Propagate_Coextensions --
3719 ----------------------------
3720
3721 procedure Propagate_Coextensions (Root : Node_Id) is
3722
3723 procedure Copy_List (From : Elist_Id; To : Elist_Id);
3724 -- Copy the contents of list From into list To, preserving the
3725 -- order of elements.
3726
3727 function Process_Allocator (Nod : Node_Id) return Traverse_Result;
3728 -- Recognize an allocator or a rewritten allocator node and add it
3729 -- along with its nested coextensions to the list of Root.
3730
3731 ---------------
3732 -- Copy_List --
3733 ---------------
3734
3735 procedure Copy_List (From : Elist_Id; To : Elist_Id) is
3736 From_Elmt : Elmt_Id;
3737 begin
3738 From_Elmt := First_Elmt (From);
3739 while Present (From_Elmt) loop
3740 Append_Elmt (Node (From_Elmt), To);
3741 Next_Elmt (From_Elmt);
3742 end loop;
3743 end Copy_List;
3744
3745 -----------------------
3746 -- Process_Allocator --
3747 -----------------------
3748
3749 function Process_Allocator (Nod : Node_Id) return Traverse_Result is
3750 Orig_Nod : Node_Id := Nod;
3751
3752 begin
3753 -- This is a possible rewritten subtype indication allocator. Any
3754 -- nested coextensions will appear as discriminant constraints.
3755
3756 if Nkind (Nod) = N_Identifier
3757 and then Present (Original_Node (Nod))
3758 and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
3759 then
3760 declare
3761 Discr : Node_Id;
3762 Discr_Elmt : Elmt_Id;
3763
3764 begin
3765 if Is_Record_Type (Entity (Nod)) then
3766 Discr_Elmt :=
3767 First_Elmt (Discriminant_Constraint (Entity (Nod)));
3768 while Present (Discr_Elmt) loop
3769 Discr := Node (Discr_Elmt);
3770
3771 if Nkind (Discr) = N_Identifier
3772 and then Present (Original_Node (Discr))
3773 and then Nkind (Original_Node (Discr)) = N_Allocator
3774 and then Present (Coextensions (
3775 Original_Node (Discr)))
3776 then
3777 if No (Coextensions (Root)) then
3778 Set_Coextensions (Root, New_Elmt_List);
3779 end if;
3780
3781 Copy_List
3782 (From => Coextensions (Original_Node (Discr)),
3783 To => Coextensions (Root));
3784 end if;
3785
3786 Next_Elmt (Discr_Elmt);
3787 end loop;
3788
3789 -- There is no need to continue the traversal of this
3790 -- subtree since all the information has already been
3791 -- propagated.
3792
3793 return Skip;
3794 end if;
3795 end;
3796
3797 -- Case of either a stand alone allocator or a rewritten allocator
3798 -- with an aggregate.
3799
3800 else
3801 if Present (Original_Node (Nod)) then
3802 Orig_Nod := Original_Node (Nod);
3803 end if;
3804
3805 if Nkind (Orig_Nod) = N_Allocator then
3806
3807 -- Propagate the list of nested coextensions to the Root
3808 -- allocator. This is done through list copy since a single
3809 -- allocator may have multiple coextensions. Do not touch
3810 -- coextensions roots.
3811
3812 if not Is_Coextension_Root (Orig_Nod)
3813 and then Present (Coextensions (Orig_Nod))
3814 then
3815 if No (Coextensions (Root)) then
3816 Set_Coextensions (Root, New_Elmt_List);
3817 end if;
3818
3819 Copy_List
3820 (From => Coextensions (Orig_Nod),
3821 To => Coextensions (Root));
3822 end if;
3823
3824 -- There is no need to continue the traversal of this
3825 -- subtree since all the information has already been
3826 -- propagated.
3827
3828 return Skip;
3829 end if;
3830 end if;
3831
3832 -- Keep on traversing, looking for the next allocator
3833
3834 return OK;
3835 end Process_Allocator;
3836
3837 procedure Process_Allocators is
3838 new Traverse_Proc (Process_Allocator);
3839
3840 -- Start of processing for Propagate_Coextensions
3841
3842 begin
3843 Process_Allocators (Expression (Root));
3844 end Propagate_Coextensions;
3845
3846 -- Start of processing for Resolve_Allocator
3847
3848 begin
3849 -- Replace general access with specific type
3850
3851 if Ekind (Etype (N)) = E_Allocator_Type then
3852 Set_Etype (N, Base_Type (Typ));
3853 end if;
3854
3855 if Is_Abstract_Type (Typ) then
3856 Error_Msg_N ("type of allocator cannot be abstract", N);
3857 end if;
3858
3859 -- For qualified expression, resolve the expression using the
3860 -- given subtype (nothing to do for type mark, subtype indication)
3861
3862 if Nkind (E) = N_Qualified_Expression then
3863 if Is_Class_Wide_Type (Etype (E))
3864 and then not Is_Class_Wide_Type (Designated_Type (Typ))
3865 and then not In_Dispatching_Context
3866 then
3867 Error_Msg_N
3868 ("class-wide allocator not allowed for this access type", N);
3869 end if;
3870
3871 Resolve (Expression (E), Etype (E));
3872 Check_Unset_Reference (Expression (E));
3873
3874 -- A qualified expression requires an exact match of the type,
3875 -- class-wide matching is not allowed.
3876
3877 if (Is_Class_Wide_Type (Etype (Expression (E)))
3878 or else Is_Class_Wide_Type (Etype (E)))
3879 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
3880 then
3881 Wrong_Type (Expression (E), Etype (E));
3882 end if;
3883
3884 -- A special accessibility check is needed for allocators that
3885 -- constrain access discriminants. The level of the type of the
3886 -- expression used to constrain an access discriminant cannot be
3887 -- deeper than the type of the allocator (in contrast to access
3888 -- parameters, where the level of the actual can be arbitrary).
3889
3890 -- We can't use Valid_Conversion to perform this check because
3891 -- in general the type of the allocator is unrelated to the type
3892 -- of the access discriminant.
3893
3894 if Ekind (Typ) /= E_Anonymous_Access_Type
3895 or else Is_Local_Anonymous_Access (Typ)
3896 then
3897 Subtyp := Entity (Subtype_Mark (E));
3898
3899 Aggr := Original_Node (Expression (E));
3900
3901 if Has_Discriminants (Subtyp)
3902 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
3903 then
3904 Discrim := First_Discriminant (Base_Type (Subtyp));
3905
3906 -- Get the first component expression of the aggregate
3907
3908 if Present (Expressions (Aggr)) then
3909 Disc_Exp := First (Expressions (Aggr));
3910
3911 elsif Present (Component_Associations (Aggr)) then
3912 Assoc := First (Component_Associations (Aggr));
3913
3914 if Present (Assoc) then
3915 Disc_Exp := Expression (Assoc);
3916 else
3917 Disc_Exp := Empty;
3918 end if;
3919
3920 else
3921 Disc_Exp := Empty;
3922 end if;
3923
3924 while Present (Discrim) and then Present (Disc_Exp) loop
3925 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
3926 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
3927 end if;
3928
3929 Next_Discriminant (Discrim);
3930
3931 if Present (Discrim) then
3932 if Present (Assoc) then
3933 Next (Assoc);
3934 Disc_Exp := Expression (Assoc);
3935
3936 elsif Present (Next (Disc_Exp)) then
3937 Next (Disc_Exp);
3938
3939 else
3940 Assoc := First (Component_Associations (Aggr));
3941
3942 if Present (Assoc) then
3943 Disc_Exp := Expression (Assoc);
3944 else
3945 Disc_Exp := Empty;
3946 end if;
3947 end if;
3948 end if;
3949 end loop;
3950 end if;
3951 end if;
3952
3953 -- For a subtype mark or subtype indication, freeze the subtype
3954
3955 else
3956 Freeze_Expression (E);
3957
3958 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
3959 Error_Msg_N
3960 ("initialization required for access-to-constant allocator", N);
3961 end if;
3962
3963 -- A special accessibility check is needed for allocators that
3964 -- constrain access discriminants. The level of the type of the
3965 -- expression used to constrain an access discriminant cannot be
3966 -- deeper than the type of the allocator (in contrast to access
3967 -- parameters, where the level of the actual can be arbitrary).
3968 -- We can't use Valid_Conversion to perform this check because
3969 -- in general the type of the allocator is unrelated to the type
3970 -- of the access discriminant.
3971
3972 if Nkind (Original_Node (E)) = N_Subtype_Indication
3973 and then (Ekind (Typ) /= E_Anonymous_Access_Type
3974 or else Is_Local_Anonymous_Access (Typ))
3975 then
3976 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
3977
3978 if Has_Discriminants (Subtyp) then
3979 Discrim := First_Discriminant (Base_Type (Subtyp));
3980 Constr := First (Constraints (Constraint (Original_Node (E))));
3981 while Present (Discrim) and then Present (Constr) loop
3982 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
3983 if Nkind (Constr) = N_Discriminant_Association then
3984 Disc_Exp := Original_Node (Expression (Constr));
3985 else
3986 Disc_Exp := Original_Node (Constr);
3987 end if;
3988
3989 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
3990 end if;
3991
3992 Next_Discriminant (Discrim);
3993 Next (Constr);
3994 end loop;
3995 end if;
3996 end if;
3997 end if;
3998
3999 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4000 -- check that the level of the type of the created object is not deeper
4001 -- than the level of the allocator's access type, since extensions can
4002 -- now occur at deeper levels than their ancestor types. This is a
4003 -- static accessibility level check; a run-time check is also needed in
4004 -- the case of an initialized allocator with a class-wide argument (see
4005 -- Expand_Allocator_Expression).
4006
4007 if Ada_Version >= Ada_05
4008 and then Is_Class_Wide_Type (Designated_Type (Typ))
4009 then
4010 declare
4011 Exp_Typ : Entity_Id;
4012
4013 begin
4014 if Nkind (E) = N_Qualified_Expression then
4015 Exp_Typ := Etype (E);
4016 elsif Nkind (E) = N_Subtype_Indication then
4017 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4018 else
4019 Exp_Typ := Entity (E);
4020 end if;
4021
4022 if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
4023 if In_Instance_Body then
4024 Error_Msg_N ("?type in allocator has deeper level than" &
4025 " designated class-wide type", E);
4026 Error_Msg_N ("\?Program_Error will be raised at run time",
4027 E);
4028 Rewrite (N,
4029 Make_Raise_Program_Error (Sloc (N),
4030 Reason => PE_Accessibility_Check_Failed));
4031 Set_Etype (N, Typ);
4032
4033 -- Do not apply Ada 2005 accessibility checks on a class-wide
4034 -- allocator if the type given in the allocator is a formal
4035 -- type. A run-time check will be performed in the instance.
4036
4037 elsif not Is_Generic_Type (Exp_Typ) then
4038 Error_Msg_N ("type in allocator has deeper level than" &
4039 " designated class-wide type", E);
4040 end if;
4041 end if;
4042 end;
4043 end if;
4044
4045 -- Check for allocation from an empty storage pool
4046
4047 if No_Pool_Assigned (Typ) then
4048 declare
4049 Loc : constant Source_Ptr := Sloc (N);
4050 begin
4051 Error_Msg_N ("?allocation from empty storage pool!", N);
4052 Error_Msg_N ("\?Storage_Error will be raised at run time!", N);
4053 Insert_Action (N,
4054 Make_Raise_Storage_Error (Loc,
4055 Reason => SE_Empty_Storage_Pool));
4056 end;
4057
4058 -- If the context is an unchecked conversion, as may happen within
4059 -- an inlined subprogram, the allocator is being resolved with its
4060 -- own anonymous type. In that case, if the target type has a specific
4061 -- storage pool, it must be inherited explicitly by the allocator type.
4062
4063 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4064 and then No (Associated_Storage_Pool (Typ))
4065 then
4066 Set_Associated_Storage_Pool
4067 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4068 end if;
4069
4070 -- An erroneous allocator may be rewritten as a raise Program_Error
4071 -- statement.
4072
4073 if Nkind (N) = N_Allocator then
4074
4075 -- An anonymous access discriminant is the definition of a
4076 -- coextension.
4077
4078 if Ekind (Typ) = E_Anonymous_Access_Type
4079 and then Nkind (Associated_Node_For_Itype (Typ)) =
4080 N_Discriminant_Specification
4081 then
4082 -- Avoid marking an allocator as a dynamic coextension if it is
4083 -- within a static construct.
4084
4085 if not Is_Static_Coextension (N) then
4086 Set_Is_Dynamic_Coextension (N);
4087 end if;
4088
4089 -- Cleanup for potential static coextensions
4090
4091 else
4092 Set_Is_Dynamic_Coextension (N, False);
4093 Set_Is_Static_Coextension (N, False);
4094 end if;
4095
4096 -- There is no need to propagate any nested coextensions if they
4097 -- are marked as static since they will be rewritten on the spot.
4098
4099 if not Is_Static_Coextension (N) then
4100 Propagate_Coextensions (N);
4101 end if;
4102 end if;
4103 end Resolve_Allocator;
4104
4105 ---------------------------
4106 -- Resolve_Arithmetic_Op --
4107 ---------------------------
4108
4109 -- Used for resolving all arithmetic operators except exponentiation
4110
4111 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4112 L : constant Node_Id := Left_Opnd (N);
4113 R : constant Node_Id := Right_Opnd (N);
4114 TL : constant Entity_Id := Base_Type (Etype (L));
4115 TR : constant Entity_Id := Base_Type (Etype (R));
4116 T : Entity_Id;
4117 Rop : Node_Id;
4118
4119 B_Typ : constant Entity_Id := Base_Type (Typ);
4120 -- We do the resolution using the base type, because intermediate values
4121 -- in expressions always are of the base type, not a subtype of it.
4122
4123 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4124 -- Returns True if N is in a context that expects "any real type"
4125
4126 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4127 -- Return True iff given type is Integer or universal real/integer
4128
4129 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4130 -- Choose type of integer literal in fixed-point operation to conform
4131 -- to available fixed-point type. T is the type of the other operand,
4132 -- which is needed to determine the expected type of N.
4133
4134 procedure Set_Operand_Type (N : Node_Id);
4135 -- Set operand type to T if universal
4136
4137 -------------------------------
4138 -- Expected_Type_Is_Any_Real --
4139 -------------------------------
4140
4141 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4142 begin
4143 -- N is the expression after "delta" in a fixed_point_definition;
4144 -- see RM-3.5.9(6):
4145
4146 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4147 N_Decimal_Fixed_Point_Definition,
4148
4149 -- N is one of the bounds in a real_range_specification;
4150 -- see RM-3.5.7(5):
4151
4152 N_Real_Range_Specification,
4153
4154 -- N is the expression of a delta_constraint;
4155 -- see RM-J.3(3):
4156
4157 N_Delta_Constraint);
4158 end Expected_Type_Is_Any_Real;
4159
4160 -----------------------------
4161 -- Is_Integer_Or_Universal --
4162 -----------------------------
4163
4164 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4165 T : Entity_Id;
4166 Index : Interp_Index;
4167 It : Interp;
4168
4169 begin
4170 if not Is_Overloaded (N) then
4171 T := Etype (N);
4172 return Base_Type (T) = Base_Type (Standard_Integer)
4173 or else T = Universal_Integer
4174 or else T = Universal_Real;
4175 else
4176 Get_First_Interp (N, Index, It);
4177 while Present (It.Typ) loop
4178 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4179 or else It.Typ = Universal_Integer
4180 or else It.Typ = Universal_Real
4181 then
4182 return True;
4183 end if;
4184
4185 Get_Next_Interp (Index, It);
4186 end loop;
4187 end if;
4188
4189 return False;
4190 end Is_Integer_Or_Universal;
4191
4192 ----------------------------
4193 -- Set_Mixed_Mode_Operand --
4194 ----------------------------
4195
4196 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4197 Index : Interp_Index;
4198 It : Interp;
4199
4200 begin
4201 if Universal_Interpretation (N) = Universal_Integer then
4202
4203 -- A universal integer literal is resolved as standard integer
4204 -- except in the case of a fixed-point result, where we leave it
4205 -- as universal (to be handled by Exp_Fixd later on)
4206
4207 if Is_Fixed_Point_Type (T) then
4208 Resolve (N, Universal_Integer);
4209 else
4210 Resolve (N, Standard_Integer);
4211 end if;
4212
4213 elsif Universal_Interpretation (N) = Universal_Real
4214 and then (T = Base_Type (Standard_Integer)
4215 or else T = Universal_Integer
4216 or else T = Universal_Real)
4217 then
4218 -- A universal real can appear in a fixed-type context. We resolve
4219 -- the literal with that context, even though this might raise an
4220 -- exception prematurely (the other operand may be zero).
4221
4222 Resolve (N, B_Typ);
4223
4224 elsif Etype (N) = Base_Type (Standard_Integer)
4225 and then T = Universal_Real
4226 and then Is_Overloaded (N)
4227 then
4228 -- Integer arg in mixed-mode operation. Resolve with universal
4229 -- type, in case preference rule must be applied.
4230
4231 Resolve (N, Universal_Integer);
4232
4233 elsif Etype (N) = T
4234 and then B_Typ /= Universal_Fixed
4235 then
4236 -- Not a mixed-mode operation, resolve with context
4237
4238 Resolve (N, B_Typ);
4239
4240 elsif Etype (N) = Any_Fixed then
4241
4242 -- N may itself be a mixed-mode operation, so use context type
4243
4244 Resolve (N, B_Typ);
4245
4246 elsif Is_Fixed_Point_Type (T)
4247 and then B_Typ = Universal_Fixed
4248 and then Is_Overloaded (N)
4249 then
4250 -- Must be (fixed * fixed) operation, operand must have one
4251 -- compatible interpretation.
4252
4253 Resolve (N, Any_Fixed);
4254
4255 elsif Is_Fixed_Point_Type (B_Typ)
4256 and then (T = Universal_Real
4257 or else Is_Fixed_Point_Type (T))
4258 and then Is_Overloaded (N)
4259 then
4260 -- C * F(X) in a fixed context, where C is a real literal or a
4261 -- fixed-point expression. F must have either a fixed type
4262 -- interpretation or an integer interpretation, but not both.
4263
4264 Get_First_Interp (N, Index, It);
4265 while Present (It.Typ) loop
4266 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4267
4268 if Analyzed (N) then
4269 Error_Msg_N ("ambiguous operand in fixed operation", N);
4270 else
4271 Resolve (N, Standard_Integer);
4272 end if;
4273
4274 elsif Is_Fixed_Point_Type (It.Typ) then
4275
4276 if Analyzed (N) then
4277 Error_Msg_N ("ambiguous operand in fixed operation", N);
4278 else
4279 Resolve (N, It.Typ);
4280 end if;
4281 end if;
4282
4283 Get_Next_Interp (Index, It);
4284 end loop;
4285
4286 -- Reanalyze the literal with the fixed type of the context. If
4287 -- context is Universal_Fixed, we are within a conversion, leave
4288 -- the literal as a universal real because there is no usable
4289 -- fixed type, and the target of the conversion plays no role in
4290 -- the resolution.
4291
4292 declare
4293 Op2 : Node_Id;
4294 T2 : Entity_Id;
4295
4296 begin
4297 if N = L then
4298 Op2 := R;
4299 else
4300 Op2 := L;
4301 end if;
4302
4303 if B_Typ = Universal_Fixed
4304 and then Nkind (Op2) = N_Real_Literal
4305 then
4306 T2 := Universal_Real;
4307 else
4308 T2 := B_Typ;
4309 end if;
4310
4311 Set_Analyzed (Op2, False);
4312 Resolve (Op2, T2);
4313 end;
4314
4315 else
4316 Resolve (N);
4317 end if;
4318 end Set_Mixed_Mode_Operand;
4319
4320 ----------------------
4321 -- Set_Operand_Type --
4322 ----------------------
4323
4324 procedure Set_Operand_Type (N : Node_Id) is
4325 begin
4326 if Etype (N) = Universal_Integer
4327 or else Etype (N) = Universal_Real
4328 then
4329 Set_Etype (N, T);
4330 end if;
4331 end Set_Operand_Type;
4332
4333 -- Start of processing for Resolve_Arithmetic_Op
4334
4335 begin
4336 if Comes_From_Source (N)
4337 and then Ekind (Entity (N)) = E_Function
4338 and then Is_Imported (Entity (N))
4339 and then Is_Intrinsic_Subprogram (Entity (N))
4340 then
4341 Resolve_Intrinsic_Operator (N, Typ);
4342 return;
4343
4344 -- Special-case for mixed-mode universal expressions or fixed point
4345 -- type operation: each argument is resolved separately. The same
4346 -- treatment is required if one of the operands of a fixed point
4347 -- operation is universal real, since in this case we don't do a
4348 -- conversion to a specific fixed-point type (instead the expander
4349 -- takes care of the case).
4350
4351 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4352 and then Present (Universal_Interpretation (L))
4353 and then Present (Universal_Interpretation (R))
4354 then
4355 Resolve (L, Universal_Interpretation (L));
4356 Resolve (R, Universal_Interpretation (R));
4357 Set_Etype (N, B_Typ);
4358
4359 elsif (B_Typ = Universal_Real
4360 or else Etype (N) = Universal_Fixed
4361 or else (Etype (N) = Any_Fixed
4362 and then Is_Fixed_Point_Type (B_Typ))
4363 or else (Is_Fixed_Point_Type (B_Typ)
4364 and then (Is_Integer_Or_Universal (L)
4365 or else
4366 Is_Integer_Or_Universal (R))))
4367 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4368 then
4369 if TL = Universal_Integer or else TR = Universal_Integer then
4370 Check_For_Visible_Operator (N, B_Typ);
4371 end if;
4372
4373 -- If context is a fixed type and one operand is integer, the
4374 -- other is resolved with the type of the context.
4375
4376 if Is_Fixed_Point_Type (B_Typ)
4377 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4378 or else TL = Universal_Integer)
4379 then
4380 Resolve (R, B_Typ);
4381 Resolve (L, TL);
4382
4383 elsif Is_Fixed_Point_Type (B_Typ)
4384 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4385 or else TR = Universal_Integer)
4386 then
4387 Resolve (L, B_Typ);
4388 Resolve (R, TR);
4389
4390 else
4391 Set_Mixed_Mode_Operand (L, TR);
4392 Set_Mixed_Mode_Operand (R, TL);
4393 end if;
4394
4395 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4396 -- multiplying operators from being used when the expected type is
4397 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4398 -- some cases where the expected type is actually Any_Real;
4399 -- Expected_Type_Is_Any_Real takes care of that case.
4400
4401 if Etype (N) = Universal_Fixed
4402 or else Etype (N) = Any_Fixed
4403 then
4404 if B_Typ = Universal_Fixed
4405 and then not Expected_Type_Is_Any_Real (N)
4406 and then not Nkind_In (Parent (N), N_Type_Conversion,
4407 N_Unchecked_Type_Conversion)
4408 then
4409 Error_Msg_N ("type cannot be determined from context!", N);
4410 Error_Msg_N ("\explicit conversion to result type required", N);
4411
4412 Set_Etype (L, Any_Type);
4413 Set_Etype (R, Any_Type);
4414
4415 else
4416 if Ada_Version = Ada_83
4417 and then Etype (N) = Universal_Fixed
4418 and then not
4419 Nkind_In (Parent (N), N_Type_Conversion,
4420 N_Unchecked_Type_Conversion)
4421 then
4422 Error_Msg_N
4423 ("(Ada 83) fixed-point operation "
4424 & "needs explicit conversion", N);
4425 end if;
4426
4427 -- The expected type is "any real type" in contexts like
4428 -- type T is delta <universal_fixed-expression> ...
4429 -- in which case we need to set the type to Universal_Real
4430 -- so that static expression evaluation will work properly.
4431
4432 if Expected_Type_Is_Any_Real (N) then
4433 Set_Etype (N, Universal_Real);
4434 else
4435 Set_Etype (N, B_Typ);
4436 end if;
4437 end if;
4438
4439 elsif Is_Fixed_Point_Type (B_Typ)
4440 and then (Is_Integer_Or_Universal (L)
4441 or else Nkind (L) = N_Real_Literal
4442 or else Nkind (R) = N_Real_Literal
4443 or else Is_Integer_Or_Universal (R))
4444 then
4445 Set_Etype (N, B_Typ);
4446
4447 elsif Etype (N) = Any_Fixed then
4448
4449 -- If no previous errors, this is only possible if one operand
4450 -- is overloaded and the context is universal. Resolve as such.
4451
4452 Set_Etype (N, B_Typ);
4453 end if;
4454
4455 else
4456 if (TL = Universal_Integer or else TL = Universal_Real)
4457 and then
4458 (TR = Universal_Integer or else TR = Universal_Real)
4459 then
4460 Check_For_Visible_Operator (N, B_Typ);
4461 end if;
4462
4463 -- If the context is Universal_Fixed and the operands are also
4464 -- universal fixed, this is an error, unless there is only one
4465 -- applicable fixed_point type (usually duration).
4466
4467 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4468 T := Unique_Fixed_Point_Type (N);
4469
4470 if T = Any_Type then
4471 Set_Etype (N, T);
4472 return;
4473 else
4474 Resolve (L, T);
4475 Resolve (R, T);
4476 end if;
4477
4478 else
4479 Resolve (L, B_Typ);
4480 Resolve (R, B_Typ);
4481 end if;
4482
4483 -- If one of the arguments was resolved to a non-universal type.
4484 -- label the result of the operation itself with the same type.
4485 -- Do the same for the universal argument, if any.
4486
4487 T := Intersect_Types (L, R);
4488 Set_Etype (N, Base_Type (T));
4489 Set_Operand_Type (L);
4490 Set_Operand_Type (R);
4491 end if;
4492
4493 Generate_Operator_Reference (N, Typ);
4494 Eval_Arithmetic_Op (N);
4495
4496 -- Set overflow and division checking bit. Much cleverer code needed
4497 -- here eventually and perhaps the Resolve routines should be separated
4498 -- for the various arithmetic operations, since they will need
4499 -- different processing. ???
4500
4501 if Nkind (N) in N_Op then
4502 if not Overflow_Checks_Suppressed (Etype (N)) then
4503 Enable_Overflow_Check (N);
4504 end if;
4505
4506 -- Give warning if explicit division by zero
4507
4508 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4509 and then not Division_Checks_Suppressed (Etype (N))
4510 then
4511 Rop := Right_Opnd (N);
4512
4513 if Compile_Time_Known_Value (Rop)
4514 and then ((Is_Integer_Type (Etype (Rop))
4515 and then Expr_Value (Rop) = Uint_0)
4516 or else
4517 (Is_Real_Type (Etype (Rop))
4518 and then Expr_Value_R (Rop) = Ureal_0))
4519 then
4520 -- Specialize the warning message according to the operation
4521
4522 case Nkind (N) is
4523 when N_Op_Divide =>
4524 Apply_Compile_Time_Constraint_Error
4525 (N, "division by zero?", CE_Divide_By_Zero,
4526 Loc => Sloc (Right_Opnd (N)));
4527
4528 when N_Op_Rem =>
4529 Apply_Compile_Time_Constraint_Error
4530 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4531 Loc => Sloc (Right_Opnd (N)));
4532
4533 when N_Op_Mod =>
4534 Apply_Compile_Time_Constraint_Error
4535 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4536 Loc => Sloc (Right_Opnd (N)));
4537
4538 -- Division by zero can only happen with division, rem,
4539 -- and mod operations.
4540
4541 when others =>
4542 raise Program_Error;
4543 end case;
4544
4545 -- Otherwise just set the flag to check at run time
4546
4547 else
4548 Activate_Division_Check (N);
4549 end if;
4550 end if;
4551
4552 -- If Restriction No_Implicit_Conditionals is active, then it is
4553 -- violated if either operand can be negative for mod, or for rem
4554 -- if both operands can be negative.
4555
4556 if Restrictions.Set (No_Implicit_Conditionals)
4557 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4558 then
4559 declare
4560 Lo : Uint;
4561 Hi : Uint;
4562 OK : Boolean;
4563
4564 LNeg : Boolean;
4565 RNeg : Boolean;
4566 -- Set if corresponding operand might be negative
4567
4568 begin
4569 Determine_Range (Left_Opnd (N), OK, Lo, Hi);
4570 LNeg := (not OK) or else Lo < 0;
4571
4572 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
4573 RNeg := (not OK) or else Lo < 0;
4574
4575 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4576 or else
4577 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4578 then
4579 Check_Restriction (No_Implicit_Conditionals, N);
4580 end if;
4581 end;
4582 end if;
4583 end if;
4584
4585 Check_Unset_Reference (L);
4586 Check_Unset_Reference (R);
4587 end Resolve_Arithmetic_Op;
4588
4589 ------------------
4590 -- Resolve_Call --
4591 ------------------
4592
4593 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4594 Loc : constant Source_Ptr := Sloc (N);
4595 Subp : constant Node_Id := Name (N);
4596 Nam : Entity_Id;
4597 I : Interp_Index;
4598 It : Interp;
4599 Norm_OK : Boolean;
4600 Scop : Entity_Id;
4601 Rtype : Entity_Id;
4602
4603 begin
4604 -- The context imposes a unique interpretation with type Typ on a
4605 -- procedure or function call. Find the entity of the subprogram that
4606 -- yields the expected type, and propagate the corresponding formal
4607 -- constraints on the actuals. The caller has established that an
4608 -- interpretation exists, and emitted an error if not unique.
4609
4610 -- First deal with the case of a call to an access-to-subprogram,
4611 -- dereference made explicit in Analyze_Call.
4612
4613 if Ekind (Etype (Subp)) = E_Subprogram_Type then
4614 if not Is_Overloaded (Subp) then
4615 Nam := Etype (Subp);
4616
4617 else
4618 -- Find the interpretation whose type (a subprogram type) has a
4619 -- return type that is compatible with the context. Analysis of
4620 -- the node has established that one exists.
4621
4622 Nam := Empty;
4623
4624 Get_First_Interp (Subp, I, It);
4625 while Present (It.Typ) loop
4626 if Covers (Typ, Etype (It.Typ)) then
4627 Nam := It.Typ;
4628 exit;
4629 end if;
4630
4631 Get_Next_Interp (I, It);
4632 end loop;
4633
4634 if No (Nam) then
4635 raise Program_Error;
4636 end if;
4637 end if;
4638
4639 -- If the prefix is not an entity, then resolve it
4640
4641 if not Is_Entity_Name (Subp) then
4642 Resolve (Subp, Nam);
4643 end if;
4644
4645 -- For an indirect call, we always invalidate checks, since we do not
4646 -- know whether the subprogram is local or global. Yes we could do
4647 -- better here, e.g. by knowing that there are no local subprograms,
4648 -- but it does not seem worth the effort. Similarly, we kill all
4649 -- knowledge of current constant values.
4650
4651 Kill_Current_Values;
4652
4653 -- If this is a procedure call which is really an entry call, do
4654 -- the conversion of the procedure call to an entry call. Protected
4655 -- operations use the same circuitry because the name in the call
4656 -- can be an arbitrary expression with special resolution rules.
4657
4658 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
4659 or else (Is_Entity_Name (Subp)
4660 and then Ekind (Entity (Subp)) = E_Entry)
4661 then
4662 Resolve_Entry_Call (N, Typ);
4663 Check_Elab_Call (N);
4664
4665 -- Kill checks and constant values, as above for indirect case
4666 -- Who knows what happens when another task is activated?
4667
4668 Kill_Current_Values;
4669 return;
4670
4671 -- Normal subprogram call with name established in Resolve
4672
4673 elsif not (Is_Type (Entity (Subp))) then
4674 Nam := Entity (Subp);
4675 Set_Entity_With_Style_Check (Subp, Nam);
4676
4677 -- Otherwise we must have the case of an overloaded call
4678
4679 else
4680 pragma Assert (Is_Overloaded (Subp));
4681 Nam := Empty; -- We know that it will be assigned in loop below
4682
4683 Get_First_Interp (Subp, I, It);
4684 while Present (It.Typ) loop
4685 if Covers (Typ, It.Typ) then
4686 Nam := It.Nam;
4687 Set_Entity_With_Style_Check (Subp, Nam);
4688 exit;
4689 end if;
4690
4691 Get_Next_Interp (I, It);
4692 end loop;
4693 end if;
4694
4695 -- Check that a call to Current_Task does not occur in an entry body
4696
4697 if Is_RTE (Nam, RE_Current_Task) then
4698 declare
4699 P : Node_Id;
4700
4701 begin
4702 P := N;
4703 loop
4704 P := Parent (P);
4705
4706 -- Exclude calls that occur within the default of a formal
4707 -- parameter of the entry, since those are evaluated outside
4708 -- of the body.
4709
4710 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
4711
4712 if Nkind (P) = N_Entry_Body
4713 or else (Nkind (P) = N_Subprogram_Body
4714 and then Is_Entry_Barrier_Function (P))
4715 then
4716 Rtype := Etype (N);
4717 Error_Msg_NE
4718 ("?& should not be used in entry body (RM C.7(17))",
4719 N, Nam);
4720 Error_Msg_NE
4721 ("\Program_Error will be raised at run time?", N, Nam);
4722 Rewrite (N,
4723 Make_Raise_Program_Error (Loc,
4724 Reason => PE_Current_Task_In_Entry_Body));
4725 Set_Etype (N, Rtype);
4726 return;
4727 end if;
4728 end loop;
4729 end;
4730 end if;
4731
4732 -- Check that a procedure call does not occur in the context of the
4733 -- entry call statement of a conditional or timed entry call. Note that
4734 -- the case of a call to a subprogram renaming of an entry will also be
4735 -- rejected. The test for N not being an N_Entry_Call_Statement is
4736 -- defensive, covering the possibility that the processing of entry
4737 -- calls might reach this point due to later modifications of the code
4738 -- above.
4739
4740 if Nkind (Parent (N)) = N_Entry_Call_Alternative
4741 and then Nkind (N) /= N_Entry_Call_Statement
4742 and then Entry_Call_Statement (Parent (N)) = N
4743 then
4744 if Ada_Version < Ada_05 then
4745 Error_Msg_N ("entry call required in select statement", N);
4746
4747 -- Ada 2005 (AI-345): If a procedure_call_statement is used
4748 -- for a procedure_or_entry_call, the procedure_name or pro-
4749 -- cedure_prefix of the procedure_call_statement shall denote
4750 -- an entry renamed by a procedure, or (a view of) a primitive
4751 -- subprogram of a limited interface whose first parameter is
4752 -- a controlling parameter.
4753
4754 elsif Nkind (N) = N_Procedure_Call_Statement
4755 and then not Is_Renamed_Entry (Nam)
4756 and then not Is_Controlling_Limited_Procedure (Nam)
4757 then
4758 Error_Msg_N
4759 ("entry call or dispatching primitive of interface required", N);
4760 end if;
4761 end if;
4762
4763 -- Check that this is not a call to a protected procedure or
4764 -- entry from within a protected function.
4765
4766 if Ekind (Current_Scope) = E_Function
4767 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
4768 and then Ekind (Nam) /= E_Function
4769 and then Scope (Nam) = Scope (Current_Scope)
4770 then
4771 Error_Msg_N ("within protected function, protected " &
4772 "object is constant", N);
4773 Error_Msg_N ("\cannot call operation that may modify it", N);
4774 end if;
4775
4776 -- Freeze the subprogram name if not in a spec-expression. Note that we
4777 -- freeze procedure calls as well as function calls. Procedure calls are
4778 -- not frozen according to the rules (RM 13.14(14)) because it is
4779 -- impossible to have a procedure call to a non-frozen procedure in pure
4780 -- Ada, but in the code that we generate in the expander, this rule
4781 -- needs extending because we can generate procedure calls that need
4782 -- freezing.
4783
4784 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
4785 Freeze_Expression (Subp);
4786 end if;
4787
4788 -- For a predefined operator, the type of the result is the type imposed
4789 -- by context, except for a predefined operation on universal fixed.
4790 -- Otherwise The type of the call is the type returned by the subprogram
4791 -- being called.
4792
4793 if Is_Predefined_Op (Nam) then
4794 if Etype (N) /= Universal_Fixed then
4795 Set_Etype (N, Typ);
4796 end if;
4797
4798 -- If the subprogram returns an array type, and the context requires the
4799 -- component type of that array type, the node is really an indexing of
4800 -- the parameterless call. Resolve as such. A pathological case occurs
4801 -- when the type of the component is an access to the array type. In
4802 -- this case the call is truly ambiguous.
4803
4804 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
4805 and then
4806 ((Is_Array_Type (Etype (Nam))
4807 and then Covers (Typ, Component_Type (Etype (Nam))))
4808 or else (Is_Access_Type (Etype (Nam))
4809 and then Is_Array_Type (Designated_Type (Etype (Nam)))
4810 and then
4811 Covers (Typ,
4812 Component_Type (Designated_Type (Etype (Nam))))))
4813 then
4814 declare
4815 Index_Node : Node_Id;
4816 New_Subp : Node_Id;
4817 Ret_Type : constant Entity_Id := Etype (Nam);
4818
4819 begin
4820 if Is_Access_Type (Ret_Type)
4821 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
4822 then
4823 Error_Msg_N
4824 ("cannot disambiguate function call and indexing", N);
4825 else
4826 New_Subp := Relocate_Node (Subp);
4827 Set_Entity (Subp, Nam);
4828
4829 if Component_Type (Ret_Type) /= Any_Type then
4830 if Needs_No_Actuals (Nam) then
4831
4832 -- Indexed call to a parameterless function
4833
4834 Index_Node :=
4835 Make_Indexed_Component (Loc,
4836 Prefix =>
4837 Make_Function_Call (Loc,
4838 Name => New_Subp),
4839 Expressions => Parameter_Associations (N));
4840 else
4841 -- An Ada 2005 prefixed call to a primitive operation
4842 -- whose first parameter is the prefix. This prefix was
4843 -- prepended to the parameter list, which is actually a
4844 -- list of indices. Remove the prefix in order to build
4845 -- the proper indexed component.
4846
4847 Index_Node :=
4848 Make_Indexed_Component (Loc,
4849 Prefix =>
4850 Make_Function_Call (Loc,
4851 Name => New_Subp,
4852 Parameter_Associations =>
4853 New_List
4854 (Remove_Head (Parameter_Associations (N)))),
4855 Expressions => Parameter_Associations (N));
4856 end if;
4857
4858 -- Since we are correcting a node classification error made
4859 -- by the parser, we call Replace rather than Rewrite.
4860
4861 Replace (N, Index_Node);
4862 Set_Etype (Prefix (N), Ret_Type);
4863 Set_Etype (N, Typ);
4864 Resolve_Indexed_Component (N, Typ);
4865 Check_Elab_Call (Prefix (N));
4866 end if;
4867 end if;
4868
4869 return;
4870 end;
4871
4872 else
4873 Set_Etype (N, Etype (Nam));
4874 end if;
4875
4876 -- In the case where the call is to an overloaded subprogram, Analyze
4877 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
4878 -- such a case Normalize_Actuals needs to be called once more to order
4879 -- the actuals correctly. Otherwise the call will have the ordering
4880 -- given by the last overloaded subprogram whether this is the correct
4881 -- one being called or not.
4882
4883 if Is_Overloaded (Subp) then
4884 Normalize_Actuals (N, Nam, False, Norm_OK);
4885 pragma Assert (Norm_OK);
4886 end if;
4887
4888 -- In any case, call is fully resolved now. Reset Overload flag, to
4889 -- prevent subsequent overload resolution if node is analyzed again
4890
4891 Set_Is_Overloaded (Subp, False);
4892 Set_Is_Overloaded (N, False);
4893
4894 -- If we are calling the current subprogram from immediately within its
4895 -- body, then that is the case where we can sometimes detect cases of
4896 -- infinite recursion statically. Do not try this in case restriction
4897 -- No_Recursion is in effect anyway, and do it only for source calls.
4898
4899 if Comes_From_Source (N) then
4900 Scop := Current_Scope;
4901
4902 -- Issue warning for possible infinite recursion in the absence
4903 -- of the No_Recursion restriction.
4904
4905 if Nam = Scop
4906 and then not Restriction_Active (No_Recursion)
4907 and then Check_Infinite_Recursion (N)
4908 then
4909 -- Here we detected and flagged an infinite recursion, so we do
4910 -- not need to test the case below for further warnings. Also if
4911 -- we now have a raise SE node, we are all done.
4912
4913 if Nkind (N) = N_Raise_Storage_Error then
4914 return;
4915 end if;
4916
4917 -- If call is to immediately containing subprogram, then check for
4918 -- the case of a possible run-time detectable infinite recursion.
4919
4920 else
4921 Scope_Loop : while Scop /= Standard_Standard loop
4922 if Nam = Scop then
4923
4924 -- Although in general case, recursion is not statically
4925 -- checkable, the case of calling an immediately containing
4926 -- subprogram is easy to catch.
4927
4928 Check_Restriction (No_Recursion, N);
4929
4930 -- If the recursive call is to a parameterless subprogram,
4931 -- then even if we can't statically detect infinite
4932 -- recursion, this is pretty suspicious, and we output a
4933 -- warning. Furthermore, we will try later to detect some
4934 -- cases here at run time by expanding checking code (see
4935 -- Detect_Infinite_Recursion in package Exp_Ch6).
4936
4937 -- If the recursive call is within a handler, do not emit a
4938 -- warning, because this is a common idiom: loop until input
4939 -- is correct, catch illegal input in handler and restart.
4940
4941 if No (First_Formal (Nam))
4942 and then Etype (Nam) = Standard_Void_Type
4943 and then not Error_Posted (N)
4944 and then Nkind (Parent (N)) /= N_Exception_Handler
4945 then
4946 -- For the case of a procedure call. We give the message
4947 -- only if the call is the first statement in a sequence
4948 -- of statements, or if all previous statements are
4949 -- simple assignments. This is simply a heuristic to
4950 -- decrease false positives, without losing too many good
4951 -- warnings. The idea is that these previous statements
4952 -- may affect global variables the procedure depends on.
4953
4954 if Nkind (N) = N_Procedure_Call_Statement
4955 and then Is_List_Member (N)
4956 then
4957 declare
4958 P : Node_Id;
4959 begin
4960 P := Prev (N);
4961 while Present (P) loop
4962 if Nkind (P) /= N_Assignment_Statement then
4963 exit Scope_Loop;
4964 end if;
4965
4966 Prev (P);
4967 end loop;
4968 end;
4969 end if;
4970
4971 -- Do not give warning if we are in a conditional context
4972
4973 declare
4974 K : constant Node_Kind := Nkind (Parent (N));
4975 begin
4976 if (K = N_Loop_Statement
4977 and then Present (Iteration_Scheme (Parent (N))))
4978 or else K = N_If_Statement
4979 or else K = N_Elsif_Part
4980 or else K = N_Case_Statement_Alternative
4981 then
4982 exit Scope_Loop;
4983 end if;
4984 end;
4985
4986 -- Here warning is to be issued
4987
4988 Set_Has_Recursive_Call (Nam);
4989 Error_Msg_N
4990 ("?possible infinite recursion!", N);
4991 Error_Msg_N
4992 ("\?Storage_Error may be raised at run time!", N);
4993 end if;
4994
4995 exit Scope_Loop;
4996 end if;
4997
4998 Scop := Scope (Scop);
4999 end loop Scope_Loop;
5000 end if;
5001 end if;
5002
5003 -- If subprogram name is a predefined operator, it was given in
5004 -- functional notation. Replace call node with operator node, so
5005 -- that actuals can be resolved appropriately.
5006
5007 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5008 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5009 return;
5010
5011 elsif Present (Alias (Nam))
5012 and then Is_Predefined_Op (Alias (Nam))
5013 then
5014 Resolve_Actuals (N, Nam);
5015 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5016 return;
5017 end if;
5018
5019 -- Create a transient scope if the resulting type requires it
5020
5021 -- There are 4 notable exceptions: in init procs, the transient scope
5022 -- overhead is not needed and even incorrect due to the actual expansion
5023 -- of adjust calls; the second case is enumeration literal pseudo calls;
5024 -- the third case is intrinsic subprograms (Unchecked_Conversion and
5025 -- source information functions) that do not use the secondary stack
5026 -- even though the return type is unconstrained; the fourth case is a
5027 -- call to a build-in-place function, since such functions may allocate
5028 -- their result directly in a target object, and cases where the result
5029 -- does get allocated in the secondary stack are checked for within the
5030 -- specialized Exp_Ch6 procedures for expanding build-in-place calls.
5031
5032 -- If this is an initialization call for a type whose initialization
5033 -- uses the secondary stack, we also need to create a transient scope
5034 -- for it, precisely because we will not do it within the init proc
5035 -- itself.
5036
5037 -- If the subprogram is marked Inline_Always, then even if it returns
5038 -- an unconstrained type the call does not require use of the secondary
5039 -- stack. However, inlining will only take place if the body to inline
5040 -- is already present. It may not be available if e.g. the subprogram is
5041 -- declared in a child instance.
5042
5043 if Is_Inlined (Nam)
5044 and then Has_Pragma_Inline_Always (Nam)
5045 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5046 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5047 then
5048 null;
5049
5050 elsif Expander_Active
5051 and then Is_Type (Etype (Nam))
5052 and then Requires_Transient_Scope (Etype (Nam))
5053 and then not Is_Build_In_Place_Function (Nam)
5054 and then Ekind (Nam) /= E_Enumeration_Literal
5055 and then not Within_Init_Proc
5056 and then not Is_Intrinsic_Subprogram (Nam)
5057 then
5058 Establish_Transient_Scope (N, Sec_Stack => True);
5059
5060 -- If the call appears within the bounds of a loop, it will
5061 -- be rewritten and reanalyzed, nothing left to do here.
5062
5063 if Nkind (N) /= N_Function_Call then
5064 return;
5065 end if;
5066
5067 elsif Is_Init_Proc (Nam)
5068 and then not Within_Init_Proc
5069 then
5070 Check_Initialization_Call (N, Nam);
5071 end if;
5072
5073 -- A protected function cannot be called within the definition of the
5074 -- enclosing protected type.
5075
5076 if Is_Protected_Type (Scope (Nam))
5077 and then In_Open_Scopes (Scope (Nam))
5078 and then not Has_Completion (Scope (Nam))
5079 then
5080 Error_Msg_NE
5081 ("& cannot be called before end of protected definition", N, Nam);
5082 end if;
5083
5084 -- Propagate interpretation to actuals, and add default expressions
5085 -- where needed.
5086
5087 if Present (First_Formal (Nam)) then
5088 Resolve_Actuals (N, Nam);
5089
5090 -- Overloaded literals are rewritten as function calls, for
5091 -- purpose of resolution. After resolution, we can replace
5092 -- the call with the literal itself.
5093
5094 elsif Ekind (Nam) = E_Enumeration_Literal then
5095 Copy_Node (Subp, N);
5096 Resolve_Entity_Name (N, Typ);
5097
5098 -- Avoid validation, since it is a static function call
5099
5100 Generate_Reference (Nam, Subp);
5101 return;
5102 end if;
5103
5104 -- If the subprogram is not global, then kill all saved values and
5105 -- checks. This is a bit conservative, since in many cases we could do
5106 -- better, but it is not worth the effort. Similarly, we kill constant
5107 -- values. However we do not need to do this for internal entities
5108 -- (unless they are inherited user-defined subprograms), since they
5109 -- are not in the business of molesting local values.
5110
5111 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5112 -- kill all checks and values for calls to global subprograms. This
5113 -- takes care of the case where an access to a local subprogram is
5114 -- taken, and could be passed directly or indirectly and then called
5115 -- from almost any context.
5116
5117 -- Note: we do not do this step till after resolving the actuals. That
5118 -- way we still take advantage of the current value information while
5119 -- scanning the actuals.
5120
5121 -- We suppress killing values if we are processing the nodes associated
5122 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5123 -- type kills all the values as part of analyzing the code that
5124 -- initializes the dispatch tables.
5125
5126 if Inside_Freezing_Actions = 0
5127 and then (not Is_Library_Level_Entity (Nam)
5128 or else Suppress_Value_Tracking_On_Call (Current_Scope))
5129 and then (Comes_From_Source (Nam)
5130 or else (Present (Alias (Nam))
5131 and then Comes_From_Source (Alias (Nam))))
5132 then
5133 Kill_Current_Values;
5134 end if;
5135
5136 -- If we are warning about unread OUT parameters, this is the place to
5137 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5138 -- after the above call to Kill_Current_Values (since that call clears
5139 -- the Last_Assignment field of all local variables).
5140
5141 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5142 and then Comes_From_Source (N)
5143 and then In_Extended_Main_Source_Unit (N)
5144 then
5145 declare
5146 F : Entity_Id;
5147 A : Node_Id;
5148
5149 begin
5150 F := First_Formal (Nam);
5151 A := First_Actual (N);
5152 while Present (F) and then Present (A) loop
5153 if (Ekind (F) = E_Out_Parameter
5154 or else Ekind (F) = E_In_Out_Parameter)
5155 and then Warn_On_Modified_As_Out_Parameter (F)
5156 and then Is_Entity_Name (A)
5157 and then Present (Entity (A))
5158 and then Comes_From_Source (N)
5159 and then Safe_To_Capture_Value (N, Entity (A))
5160 then
5161 Set_Last_Assignment (Entity (A), A);
5162 end if;
5163
5164 Next_Formal (F);
5165 Next_Actual (A);
5166 end loop;
5167 end;
5168 end if;
5169
5170 -- If the subprogram is a primitive operation, check whether or not
5171 -- it is a correct dispatching call.
5172
5173 if Is_Overloadable (Nam)
5174 and then Is_Dispatching_Operation (Nam)
5175 then
5176 Check_Dispatching_Call (N);
5177
5178 elsif Ekind (Nam) /= E_Subprogram_Type
5179 and then Is_Abstract_Subprogram (Nam)
5180 and then not In_Instance
5181 then
5182 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5183 end if;
5184
5185 -- If this is a dispatching call, generate the appropriate reference,
5186 -- for better source navigation in GPS.
5187
5188 if Is_Overloadable (Nam)
5189 and then Present (Controlling_Argument (N))
5190 then
5191 Generate_Reference (Nam, Subp, 'R');
5192 else
5193 Generate_Reference (Nam, Subp);
5194 end if;
5195
5196 if Is_Intrinsic_Subprogram (Nam) then
5197 Check_Intrinsic_Call (N);
5198 end if;
5199
5200 -- Check for violation of restriction No_Specific_Termination_Handlers
5201
5202 if Is_RTE (Nam, RE_Set_Specific_Handler)
5203 or else
5204 Is_RTE (Nam, RE_Specific_Handler)
5205 then
5206 Check_Restriction (No_Specific_Termination_Handlers, N);
5207 end if;
5208
5209 -- All done, evaluate call and deal with elaboration issues
5210
5211 Eval_Call (N);
5212 Check_Elab_Call (N);
5213 end Resolve_Call;
5214
5215 -------------------------------
5216 -- Resolve_Character_Literal --
5217 -------------------------------
5218
5219 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5220 B_Typ : constant Entity_Id := Base_Type (Typ);
5221 C : Entity_Id;
5222
5223 begin
5224 -- Verify that the character does belong to the type of the context
5225
5226 Set_Etype (N, B_Typ);
5227 Eval_Character_Literal (N);
5228
5229 -- Wide_Wide_Character literals must always be defined, since the set
5230 -- of wide wide character literals is complete, i.e. if a character
5231 -- literal is accepted by the parser, then it is OK for wide wide
5232 -- character (out of range character literals are rejected).
5233
5234 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5235 return;
5236
5237 -- Always accept character literal for type Any_Character, which
5238 -- occurs in error situations and in comparisons of literals, both
5239 -- of which should accept all literals.
5240
5241 elsif B_Typ = Any_Character then
5242 return;
5243
5244 -- For Standard.Character or a type derived from it, check that
5245 -- the literal is in range
5246
5247 elsif Root_Type (B_Typ) = Standard_Character then
5248 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5249 return;
5250 end if;
5251
5252 -- For Standard.Wide_Character or a type derived from it, check
5253 -- that the literal is in range
5254
5255 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5256 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5257 return;
5258 end if;
5259
5260 -- For Standard.Wide_Wide_Character or a type derived from it, we
5261 -- know the literal is in range, since the parser checked!
5262
5263 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5264 return;
5265
5266 -- If the entity is already set, this has already been resolved in
5267 -- a generic context, or comes from expansion. Nothing else to do.
5268
5269 elsif Present (Entity (N)) then
5270 return;
5271
5272 -- Otherwise we have a user defined character type, and we can use
5273 -- the standard visibility mechanisms to locate the referenced entity
5274
5275 else
5276 C := Current_Entity (N);
5277 while Present (C) loop
5278 if Etype (C) = B_Typ then
5279 Set_Entity_With_Style_Check (N, C);
5280 Generate_Reference (C, N);
5281 return;
5282 end if;
5283
5284 C := Homonym (C);
5285 end loop;
5286 end if;
5287
5288 -- If we fall through, then the literal does not match any of the
5289 -- entries of the enumeration type. This isn't just a constraint
5290 -- error situation, it is an illegality (see RM 4.2).
5291
5292 Error_Msg_NE
5293 ("character not defined for }", N, First_Subtype (B_Typ));
5294 end Resolve_Character_Literal;
5295
5296 ---------------------------
5297 -- Resolve_Comparison_Op --
5298 ---------------------------
5299
5300 -- Context requires a boolean type, and plays no role in resolution.
5301 -- Processing identical to that for equality operators. The result
5302 -- type is the base type, which matters when pathological subtypes of
5303 -- booleans with limited ranges are used.
5304
5305 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5306 L : constant Node_Id := Left_Opnd (N);
5307 R : constant Node_Id := Right_Opnd (N);
5308 T : Entity_Id;
5309
5310 begin
5311 -- If this is an intrinsic operation which is not predefined, use
5312 -- the types of its declared arguments to resolve the possibly
5313 -- overloaded operands. Otherwise the operands are unambiguous and
5314 -- specify the expected type.
5315
5316 if Scope (Entity (N)) /= Standard_Standard then
5317 T := Etype (First_Entity (Entity (N)));
5318
5319 else
5320 T := Find_Unique_Type (L, R);
5321
5322 if T = Any_Fixed then
5323 T := Unique_Fixed_Point_Type (L);
5324 end if;
5325 end if;
5326
5327 Set_Etype (N, Base_Type (Typ));
5328 Generate_Reference (T, N, ' ');
5329
5330 if T /= Any_Type then
5331 if T = Any_String
5332 or else T = Any_Composite
5333 or else T = Any_Character
5334 then
5335 if T = Any_Character then
5336 Ambiguous_Character (L);
5337 else
5338 Error_Msg_N ("ambiguous operands for comparison", N);
5339 end if;
5340
5341 Set_Etype (N, Any_Type);
5342 return;
5343
5344 else
5345 Resolve (L, T);
5346 Resolve (R, T);
5347 Check_Unset_Reference (L);
5348 Check_Unset_Reference (R);
5349 Generate_Operator_Reference (N, T);
5350 Eval_Relational_Op (N);
5351 end if;
5352 end if;
5353 end Resolve_Comparison_Op;
5354
5355 ------------------------------------
5356 -- Resolve_Conditional_Expression --
5357 ------------------------------------
5358
5359 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5360 Condition : constant Node_Id := First (Expressions (N));
5361 Then_Expr : constant Node_Id := Next (Condition);
5362 Else_Expr : constant Node_Id := Next (Then_Expr);
5363
5364 begin
5365 Resolve (Condition, Standard_Boolean);
5366 Resolve (Then_Expr, Typ);
5367 Resolve (Else_Expr, Typ);
5368
5369 Set_Etype (N, Typ);
5370 Eval_Conditional_Expression (N);
5371 end Resolve_Conditional_Expression;
5372
5373 -----------------------------------------
5374 -- Resolve_Discrete_Subtype_Indication --
5375 -----------------------------------------
5376
5377 procedure Resolve_Discrete_Subtype_Indication
5378 (N : Node_Id;
5379 Typ : Entity_Id)
5380 is
5381 R : Node_Id;
5382 S : Entity_Id;
5383
5384 begin
5385 Analyze (Subtype_Mark (N));
5386 S := Entity (Subtype_Mark (N));
5387
5388 if Nkind (Constraint (N)) /= N_Range_Constraint then
5389 Error_Msg_N ("expect range constraint for discrete type", N);
5390 Set_Etype (N, Any_Type);
5391
5392 else
5393 R := Range_Expression (Constraint (N));
5394
5395 if R = Error then
5396 return;
5397 end if;
5398
5399 Analyze (R);
5400
5401 if Base_Type (S) /= Base_Type (Typ) then
5402 Error_Msg_NE
5403 ("expect subtype of }", N, First_Subtype (Typ));
5404
5405 -- Rewrite the constraint as a range of Typ
5406 -- to allow compilation to proceed further.
5407
5408 Set_Etype (N, Typ);
5409 Rewrite (Low_Bound (R),
5410 Make_Attribute_Reference (Sloc (Low_Bound (R)),
5411 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5412 Attribute_Name => Name_First));
5413 Rewrite (High_Bound (R),
5414 Make_Attribute_Reference (Sloc (High_Bound (R)),
5415 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5416 Attribute_Name => Name_First));
5417
5418 else
5419 Resolve (R, Typ);
5420 Set_Etype (N, Etype (R));
5421
5422 -- Additionally, we must check that the bounds are compatible
5423 -- with the given subtype, which might be different from the
5424 -- type of the context.
5425
5426 Apply_Range_Check (R, S);
5427
5428 -- ??? If the above check statically detects a Constraint_Error
5429 -- it replaces the offending bound(s) of the range R with a
5430 -- Constraint_Error node. When the itype which uses these bounds
5431 -- is frozen the resulting call to Duplicate_Subexpr generates
5432 -- a new temporary for the bounds.
5433
5434 -- Unfortunately there are other itypes that are also made depend
5435 -- on these bounds, so when Duplicate_Subexpr is called they get
5436 -- a forward reference to the newly created temporaries and Gigi
5437 -- aborts on such forward references. This is probably sign of a
5438 -- more fundamental problem somewhere else in either the order of
5439 -- itype freezing or the way certain itypes are constructed.
5440
5441 -- To get around this problem we call Remove_Side_Effects right
5442 -- away if either bounds of R are a Constraint_Error.
5443
5444 declare
5445 L : constant Node_Id := Low_Bound (R);
5446 H : constant Node_Id := High_Bound (R);
5447
5448 begin
5449 if Nkind (L) = N_Raise_Constraint_Error then
5450 Remove_Side_Effects (L);
5451 end if;
5452
5453 if Nkind (H) = N_Raise_Constraint_Error then
5454 Remove_Side_Effects (H);
5455 end if;
5456 end;
5457
5458 Check_Unset_Reference (Low_Bound (R));
5459 Check_Unset_Reference (High_Bound (R));
5460 end if;
5461 end if;
5462 end Resolve_Discrete_Subtype_Indication;
5463
5464 -------------------------
5465 -- Resolve_Entity_Name --
5466 -------------------------
5467
5468 -- Used to resolve identifiers and expanded names
5469
5470 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
5471 E : constant Entity_Id := Entity (N);
5472
5473 begin
5474 -- If garbage from errors, set to Any_Type and return
5475
5476 if No (E) and then Total_Errors_Detected /= 0 then
5477 Set_Etype (N, Any_Type);
5478 return;
5479 end if;
5480
5481 -- Replace named numbers by corresponding literals. Note that this is
5482 -- the one case where Resolve_Entity_Name must reset the Etype, since
5483 -- it is currently marked as universal.
5484
5485 if Ekind (E) = E_Named_Integer then
5486 Set_Etype (N, Typ);
5487 Eval_Named_Integer (N);
5488
5489 elsif Ekind (E) = E_Named_Real then
5490 Set_Etype (N, Typ);
5491 Eval_Named_Real (N);
5492
5493 -- Allow use of subtype only if it is a concurrent type where we are
5494 -- currently inside the body. This will eventually be expanded
5495 -- into a call to Self (for tasks) or _object (for protected
5496 -- objects). Any other use of a subtype is invalid.
5497
5498 elsif Is_Type (E) then
5499 if Is_Concurrent_Type (E)
5500 and then In_Open_Scopes (E)
5501 then
5502 null;
5503 else
5504 Error_Msg_N
5505 ("invalid use of subtype mark in expression or call", N);
5506 end if;
5507
5508 -- Check discriminant use if entity is discriminant in current scope,
5509 -- i.e. discriminant of record or concurrent type currently being
5510 -- analyzed. Uses in corresponding body are unrestricted.
5511
5512 elsif Ekind (E) = E_Discriminant
5513 and then Scope (E) = Current_Scope
5514 and then not Has_Completion (Current_Scope)
5515 then
5516 Check_Discriminant_Use (N);
5517
5518 -- A parameterless generic function cannot appear in a context that
5519 -- requires resolution.
5520
5521 elsif Ekind (E) = E_Generic_Function then
5522 Error_Msg_N ("illegal use of generic function", N);
5523
5524 elsif Ekind (E) = E_Out_Parameter
5525 and then Ada_Version = Ada_83
5526 and then (Nkind (Parent (N)) in N_Op
5527 or else (Nkind (Parent (N)) = N_Assignment_Statement
5528 and then N = Expression (Parent (N)))
5529 or else Nkind (Parent (N)) = N_Explicit_Dereference)
5530 then
5531 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
5532
5533 -- In all other cases, just do the possible static evaluation
5534
5535 else
5536 -- A deferred constant that appears in an expression must have
5537 -- a completion, unless it has been removed by in-place expansion
5538 -- of an aggregate.
5539
5540 if Ekind (E) = E_Constant
5541 and then Comes_From_Source (E)
5542 and then No (Constant_Value (E))
5543 and then Is_Frozen (Etype (E))
5544 and then not In_Spec_Expression
5545 and then not Is_Imported (E)
5546 then
5547
5548 if No_Initialization (Parent (E))
5549 or else (Present (Full_View (E))
5550 and then No_Initialization (Parent (Full_View (E))))
5551 then
5552 null;
5553 else
5554 Error_Msg_N (
5555 "deferred constant is frozen before completion", N);
5556 end if;
5557 end if;
5558
5559 Eval_Entity_Name (N);
5560 end if;
5561 end Resolve_Entity_Name;
5562
5563 -------------------
5564 -- Resolve_Entry --
5565 -------------------
5566
5567 procedure Resolve_Entry (Entry_Name : Node_Id) is
5568 Loc : constant Source_Ptr := Sloc (Entry_Name);
5569 Nam : Entity_Id;
5570 New_N : Node_Id;
5571 S : Entity_Id;
5572 Tsk : Entity_Id;
5573 E_Name : Node_Id;
5574 Index : Node_Id;
5575
5576 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
5577 -- If the bounds of the entry family being called depend on task
5578 -- discriminants, build a new index subtype where a discriminant is
5579 -- replaced with the value of the discriminant of the target task.
5580 -- The target task is the prefix of the entry name in the call.
5581
5582 -----------------------
5583 -- Actual_Index_Type --
5584 -----------------------
5585
5586 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
5587 Typ : constant Entity_Id := Entry_Index_Type (E);
5588 Tsk : constant Entity_Id := Scope (E);
5589 Lo : constant Node_Id := Type_Low_Bound (Typ);
5590 Hi : constant Node_Id := Type_High_Bound (Typ);
5591 New_T : Entity_Id;
5592
5593 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
5594 -- If the bound is given by a discriminant, replace with a reference
5595 -- to the discriminant of the same name in the target task.
5596 -- If the entry name is the target of a requeue statement and the
5597 -- entry is in the current protected object, the bound to be used
5598 -- is the discriminal of the object (see apply_range_checks for
5599 -- details of the transformation).
5600
5601 -----------------------------
5602 -- Actual_Discriminant_Ref --
5603 -----------------------------
5604
5605 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
5606 Typ : constant Entity_Id := Etype (Bound);
5607 Ref : Node_Id;
5608
5609 begin
5610 Remove_Side_Effects (Bound);
5611
5612 if not Is_Entity_Name (Bound)
5613 or else Ekind (Entity (Bound)) /= E_Discriminant
5614 then
5615 return Bound;
5616
5617 elsif Is_Protected_Type (Tsk)
5618 and then In_Open_Scopes (Tsk)
5619 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
5620 then
5621 return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
5622
5623 else
5624 Ref :=
5625 Make_Selected_Component (Loc,
5626 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
5627 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
5628 Analyze (Ref);
5629 Resolve (Ref, Typ);
5630 return Ref;
5631 end if;
5632 end Actual_Discriminant_Ref;
5633
5634 -- Start of processing for Actual_Index_Type
5635
5636 begin
5637 if not Has_Discriminants (Tsk)
5638 or else (not Is_Entity_Name (Lo)
5639 and then not Is_Entity_Name (Hi))
5640 then
5641 return Entry_Index_Type (E);
5642
5643 else
5644 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
5645 Set_Etype (New_T, Base_Type (Typ));
5646 Set_Size_Info (New_T, Typ);
5647 Set_RM_Size (New_T, RM_Size (Typ));
5648 Set_Scalar_Range (New_T,
5649 Make_Range (Sloc (Entry_Name),
5650 Low_Bound => Actual_Discriminant_Ref (Lo),
5651 High_Bound => Actual_Discriminant_Ref (Hi)));
5652
5653 return New_T;
5654 end if;
5655 end Actual_Index_Type;
5656
5657 -- Start of processing of Resolve_Entry
5658
5659 begin
5660 -- Find name of entry being called, and resolve prefix of name
5661 -- with its own type. The prefix can be overloaded, and the name
5662 -- and signature of the entry must be taken into account.
5663
5664 if Nkind (Entry_Name) = N_Indexed_Component then
5665
5666 -- Case of dealing with entry family within the current tasks
5667
5668 E_Name := Prefix (Entry_Name);
5669
5670 else
5671 E_Name := Entry_Name;
5672 end if;
5673
5674 if Is_Entity_Name (E_Name) then
5675 -- Entry call to an entry (or entry family) in the current task.
5676 -- This is legal even though the task will deadlock. Rewrite as
5677 -- call to current task.
5678
5679 -- This can also be a call to an entry in an enclosing task.
5680 -- If this is a single task, we have to retrieve its name,
5681 -- because the scope of the entry is the task type, not the
5682 -- object. If the enclosing task is a task type, the identity
5683 -- of the task is given by its own self variable.
5684
5685 -- Finally this can be a requeue on an entry of the same task
5686 -- or protected object.
5687
5688 S := Scope (Entity (E_Name));
5689
5690 for J in reverse 0 .. Scope_Stack.Last loop
5691
5692 if Is_Task_Type (Scope_Stack.Table (J).Entity)
5693 and then not Comes_From_Source (S)
5694 then
5695 -- S is an enclosing task or protected object. The concurrent
5696 -- declaration has been converted into a type declaration, and
5697 -- the object itself has an object declaration that follows
5698 -- the type in the same declarative part.
5699
5700 Tsk := Next_Entity (S);
5701 while Etype (Tsk) /= S loop
5702 Next_Entity (Tsk);
5703 end loop;
5704
5705 S := Tsk;
5706 exit;
5707
5708 elsif S = Scope_Stack.Table (J).Entity then
5709
5710 -- Call to current task. Will be transformed into call to Self
5711
5712 exit;
5713
5714 end if;
5715 end loop;
5716
5717 New_N :=
5718 Make_Selected_Component (Loc,
5719 Prefix => New_Occurrence_Of (S, Loc),
5720 Selector_Name =>
5721 New_Occurrence_Of (Entity (E_Name), Loc));
5722 Rewrite (E_Name, New_N);
5723 Analyze (E_Name);
5724
5725 elsif Nkind (Entry_Name) = N_Selected_Component
5726 and then Is_Overloaded (Prefix (Entry_Name))
5727 then
5728 -- Use the entry name (which must be unique at this point) to
5729 -- find the prefix that returns the corresponding task type or
5730 -- protected type.
5731
5732 declare
5733 Pref : constant Node_Id := Prefix (Entry_Name);
5734 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
5735 I : Interp_Index;
5736 It : Interp;
5737
5738 begin
5739 Get_First_Interp (Pref, I, It);
5740 while Present (It.Typ) loop
5741 if Scope (Ent) = It.Typ then
5742 Set_Etype (Pref, It.Typ);
5743 exit;
5744 end if;
5745
5746 Get_Next_Interp (I, It);
5747 end loop;
5748 end;
5749 end if;
5750
5751 if Nkind (Entry_Name) = N_Selected_Component then
5752 Resolve (Prefix (Entry_Name));
5753
5754 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
5755 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
5756 Resolve (Prefix (Prefix (Entry_Name)));
5757 Index := First (Expressions (Entry_Name));
5758 Resolve (Index, Entry_Index_Type (Nam));
5759
5760 -- Up to this point the expression could have been the actual
5761 -- in a simple entry call, and be given by a named association.
5762
5763 if Nkind (Index) = N_Parameter_Association then
5764 Error_Msg_N ("expect expression for entry index", Index);
5765 else
5766 Apply_Range_Check (Index, Actual_Index_Type (Nam));
5767 end if;
5768 end if;
5769 end Resolve_Entry;
5770
5771 ------------------------
5772 -- Resolve_Entry_Call --
5773 ------------------------
5774
5775 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
5776 Entry_Name : constant Node_Id := Name (N);
5777 Loc : constant Source_Ptr := Sloc (Entry_Name);
5778 Actuals : List_Id;
5779 First_Named : Node_Id;
5780 Nam : Entity_Id;
5781 Norm_OK : Boolean;
5782 Obj : Node_Id;
5783 Was_Over : Boolean;
5784
5785 begin
5786 -- We kill all checks here, because it does not seem worth the
5787 -- effort to do anything better, an entry call is a big operation.
5788
5789 Kill_All_Checks;
5790
5791 -- Processing of the name is similar for entry calls and protected
5792 -- operation calls. Once the entity is determined, we can complete
5793 -- the resolution of the actuals.
5794
5795 -- The selector may be overloaded, in the case of a protected object
5796 -- with overloaded functions. The type of the context is used for
5797 -- resolution.
5798
5799 if Nkind (Entry_Name) = N_Selected_Component
5800 and then Is_Overloaded (Selector_Name (Entry_Name))
5801 and then Typ /= Standard_Void_Type
5802 then
5803 declare
5804 I : Interp_Index;
5805 It : Interp;
5806
5807 begin
5808 Get_First_Interp (Selector_Name (Entry_Name), I, It);
5809 while Present (It.Typ) loop
5810 if Covers (Typ, It.Typ) then
5811 Set_Entity (Selector_Name (Entry_Name), It.Nam);
5812 Set_Etype (Entry_Name, It.Typ);
5813
5814 Generate_Reference (It.Typ, N, ' ');
5815 end if;
5816
5817 Get_Next_Interp (I, It);
5818 end loop;
5819 end;
5820 end if;
5821
5822 Resolve_Entry (Entry_Name);
5823
5824 if Nkind (Entry_Name) = N_Selected_Component then
5825
5826 -- Simple entry call
5827
5828 Nam := Entity (Selector_Name (Entry_Name));
5829 Obj := Prefix (Entry_Name);
5830 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
5831
5832 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
5833
5834 -- Call to member of entry family
5835
5836 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
5837 Obj := Prefix (Prefix (Entry_Name));
5838 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
5839 end if;
5840
5841 -- We cannot in general check the maximum depth of protected entry
5842 -- calls at compile time. But we can tell that any protected entry
5843 -- call at all violates a specified nesting depth of zero.
5844
5845 if Is_Protected_Type (Scope (Nam)) then
5846 Check_Restriction (Max_Entry_Queue_Length, N);
5847 end if;
5848
5849 -- Use context type to disambiguate a protected function that can be
5850 -- called without actuals and that returns an array type, and where
5851 -- the argument list may be an indexing of the returned value.
5852
5853 if Ekind (Nam) = E_Function
5854 and then Needs_No_Actuals (Nam)
5855 and then Present (Parameter_Associations (N))
5856 and then
5857 ((Is_Array_Type (Etype (Nam))
5858 and then Covers (Typ, Component_Type (Etype (Nam))))
5859
5860 or else (Is_Access_Type (Etype (Nam))
5861 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5862 and then Covers (Typ,
5863 Component_Type (Designated_Type (Etype (Nam))))))
5864 then
5865 declare
5866 Index_Node : Node_Id;
5867
5868 begin
5869 Index_Node :=
5870 Make_Indexed_Component (Loc,
5871 Prefix =>
5872 Make_Function_Call (Loc,
5873 Name => Relocate_Node (Entry_Name)),
5874 Expressions => Parameter_Associations (N));
5875
5876 -- Since we are correcting a node classification error made by
5877 -- the parser, we call Replace rather than Rewrite.
5878
5879 Replace (N, Index_Node);
5880 Set_Etype (Prefix (N), Etype (Nam));
5881 Set_Etype (N, Typ);
5882 Resolve_Indexed_Component (N, Typ);
5883 return;
5884 end;
5885 end if;
5886
5887 -- The operation name may have been overloaded. Order the actuals
5888 -- according to the formals of the resolved entity, and set the
5889 -- return type to that of the operation.
5890
5891 if Was_Over then
5892 Normalize_Actuals (N, Nam, False, Norm_OK);
5893 pragma Assert (Norm_OK);
5894 Set_Etype (N, Etype (Nam));
5895 end if;
5896
5897 Resolve_Actuals (N, Nam);
5898 Generate_Reference (Nam, Entry_Name);
5899
5900 if Ekind (Nam) = E_Entry
5901 or else Ekind (Nam) = E_Entry_Family
5902 then
5903 Check_Potentially_Blocking_Operation (N);
5904 end if;
5905
5906 -- Verify that a procedure call cannot masquerade as an entry
5907 -- call where an entry call is expected.
5908
5909 if Ekind (Nam) = E_Procedure then
5910 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5911 and then N = Entry_Call_Statement (Parent (N))
5912 then
5913 Error_Msg_N ("entry call required in select statement", N);
5914
5915 elsif Nkind (Parent (N)) = N_Triggering_Alternative
5916 and then N = Triggering_Statement (Parent (N))
5917 then
5918 Error_Msg_N ("triggering statement cannot be procedure call", N);
5919
5920 elsif Ekind (Scope (Nam)) = E_Task_Type
5921 and then not In_Open_Scopes (Scope (Nam))
5922 then
5923 Error_Msg_N ("task has no entry with this name", Entry_Name);
5924 end if;
5925 end if;
5926
5927 -- After resolution, entry calls and protected procedure calls
5928 -- are changed into entry calls, for expansion. The structure
5929 -- of the node does not change, so it can safely be done in place.
5930 -- Protected function calls must keep their structure because they
5931 -- are subexpressions.
5932
5933 if Ekind (Nam) /= E_Function then
5934
5935 -- A protected operation that is not a function may modify the
5936 -- corresponding object, and cannot apply to a constant.
5937 -- If this is an internal call, the prefix is the type itself.
5938
5939 if Is_Protected_Type (Scope (Nam))
5940 and then not Is_Variable (Obj)
5941 and then (not Is_Entity_Name (Obj)
5942 or else not Is_Type (Entity (Obj)))
5943 then
5944 Error_Msg_N
5945 ("prefix of protected procedure or entry call must be variable",
5946 Entry_Name);
5947 end if;
5948
5949 Actuals := Parameter_Associations (N);
5950 First_Named := First_Named_Actual (N);
5951
5952 Rewrite (N,
5953 Make_Entry_Call_Statement (Loc,
5954 Name => Entry_Name,
5955 Parameter_Associations => Actuals));
5956
5957 Set_First_Named_Actual (N, First_Named);
5958 Set_Analyzed (N, True);
5959
5960 -- Protected functions can return on the secondary stack, in which
5961 -- case we must trigger the transient scope mechanism.
5962
5963 elsif Expander_Active
5964 and then Requires_Transient_Scope (Etype (Nam))
5965 then
5966 Establish_Transient_Scope (N, Sec_Stack => True);
5967 end if;
5968 end Resolve_Entry_Call;
5969
5970 -------------------------
5971 -- Resolve_Equality_Op --
5972 -------------------------
5973
5974 -- Both arguments must have the same type, and the boolean context
5975 -- does not participate in the resolution. The first pass verifies
5976 -- that the interpretation is not ambiguous, and the type of the left
5977 -- argument is correctly set, or is Any_Type in case of ambiguity.
5978 -- If both arguments are strings or aggregates, allocators, or Null,
5979 -- they are ambiguous even though they carry a single (universal) type.
5980 -- Diagnose this case here.
5981
5982 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
5983 L : constant Node_Id := Left_Opnd (N);
5984 R : constant Node_Id := Right_Opnd (N);
5985 T : Entity_Id := Find_Unique_Type (L, R);
5986
5987 function Find_Unique_Access_Type return Entity_Id;
5988 -- In the case of allocators, make a last-ditch attempt to find a single
5989 -- access type with the right designated type. This is semantically
5990 -- dubious, and of no interest to any real code, but c48008a makes it
5991 -- all worthwhile.
5992
5993 -----------------------------
5994 -- Find_Unique_Access_Type --
5995 -----------------------------
5996
5997 function Find_Unique_Access_Type return Entity_Id is
5998 Acc : Entity_Id;
5999 E : Entity_Id;
6000 S : Entity_Id;
6001
6002 begin
6003 if Ekind (Etype (R)) = E_Allocator_Type then
6004 Acc := Designated_Type (Etype (R));
6005 elsif Ekind (Etype (L)) = E_Allocator_Type then
6006 Acc := Designated_Type (Etype (L));
6007 else
6008 return Empty;
6009 end if;
6010
6011 S := Current_Scope;
6012 while S /= Standard_Standard loop
6013 E := First_Entity (S);
6014 while Present (E) loop
6015 if Is_Type (E)
6016 and then Is_Access_Type (E)
6017 and then Ekind (E) /= E_Allocator_Type
6018 and then Designated_Type (E) = Base_Type (Acc)
6019 then
6020 return E;
6021 end if;
6022
6023 Next_Entity (E);
6024 end loop;
6025
6026 S := Scope (S);
6027 end loop;
6028
6029 return Empty;
6030 end Find_Unique_Access_Type;
6031
6032 -- Start of processing for Resolve_Equality_Op
6033
6034 begin
6035 Set_Etype (N, Base_Type (Typ));
6036 Generate_Reference (T, N, ' ');
6037
6038 if T = Any_Fixed then
6039 T := Unique_Fixed_Point_Type (L);
6040 end if;
6041
6042 if T /= Any_Type then
6043 if T = Any_String
6044 or else T = Any_Composite
6045 or else T = Any_Character
6046 then
6047 if T = Any_Character then
6048 Ambiguous_Character (L);
6049 else
6050 Error_Msg_N ("ambiguous operands for equality", N);
6051 end if;
6052
6053 Set_Etype (N, Any_Type);
6054 return;
6055
6056 elsif T = Any_Access
6057 or else Ekind (T) = E_Allocator_Type
6058 or else Ekind (T) = E_Access_Attribute_Type
6059 then
6060 T := Find_Unique_Access_Type;
6061
6062 if No (T) then
6063 Error_Msg_N ("ambiguous operands for equality", N);
6064 Set_Etype (N, Any_Type);
6065 return;
6066 end if;
6067 end if;
6068
6069 Resolve (L, T);
6070 Resolve (R, T);
6071
6072 -- If the unique type is a class-wide type then it will be expanded
6073 -- into a dispatching call to the predefined primitive. Therefore we
6074 -- check here for potential violation of such restriction.
6075
6076 if Is_Class_Wide_Type (T) then
6077 Check_Restriction (No_Dispatching_Calls, N);
6078 end if;
6079
6080 if Warn_On_Redundant_Constructs
6081 and then Comes_From_Source (N)
6082 and then Is_Entity_Name (R)
6083 and then Entity (R) = Standard_True
6084 and then Comes_From_Source (R)
6085 then
6086 Error_Msg_N ("?comparison with True is redundant!", R);
6087 end if;
6088
6089 Check_Unset_Reference (L);
6090 Check_Unset_Reference (R);
6091 Generate_Operator_Reference (N, T);
6092
6093 -- If this is an inequality, it may be the implicit inequality
6094 -- created for a user-defined operation, in which case the corres-
6095 -- ponding equality operation is not intrinsic, and the operation
6096 -- cannot be constant-folded. Else fold.
6097
6098 if Nkind (N) = N_Op_Eq
6099 or else Comes_From_Source (Entity (N))
6100 or else Ekind (Entity (N)) = E_Operator
6101 or else Is_Intrinsic_Subprogram
6102 (Corresponding_Equality (Entity (N)))
6103 then
6104 Eval_Relational_Op (N);
6105
6106 elsif Nkind (N) = N_Op_Ne
6107 and then Is_Abstract_Subprogram (Entity (N))
6108 then
6109 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6110 end if;
6111
6112 -- Ada 2005: If one operand is an anonymous access type, convert
6113 -- the other operand to it, to ensure that the underlying types
6114 -- match in the back-end. Same for access_to_subprogram, and the
6115 -- conversion verifies that the types are subtype conformant.
6116
6117 -- We apply the same conversion in the case one of the operands is
6118 -- a private subtype of the type of the other.
6119
6120 -- Why the Expander_Active test here ???
6121
6122 if Expander_Active
6123 and then
6124 (Ekind (T) = E_Anonymous_Access_Type
6125 or else Ekind (T) = E_Anonymous_Access_Subprogram_Type
6126 or else Is_Private_Type (T))
6127 then
6128 if Etype (L) /= T then
6129 Rewrite (L,
6130 Make_Unchecked_Type_Conversion (Sloc (L),
6131 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6132 Expression => Relocate_Node (L)));
6133 Analyze_And_Resolve (L, T);
6134 end if;
6135
6136 if (Etype (R)) /= T then
6137 Rewrite (R,
6138 Make_Unchecked_Type_Conversion (Sloc (R),
6139 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6140 Expression => Relocate_Node (R)));
6141 Analyze_And_Resolve (R, T);
6142 end if;
6143 end if;
6144 end if;
6145 end Resolve_Equality_Op;
6146
6147 ----------------------------------
6148 -- Resolve_Explicit_Dereference --
6149 ----------------------------------
6150
6151 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6152 Loc : constant Source_Ptr := Sloc (N);
6153 New_N : Node_Id;
6154 P : constant Node_Id := Prefix (N);
6155 I : Interp_Index;
6156 It : Interp;
6157
6158 begin
6159 Check_Fully_Declared_Prefix (Typ, P);
6160
6161 if Is_Overloaded (P) then
6162
6163 -- Use the context type to select the prefix that has the correct
6164 -- designated type.
6165
6166 Get_First_Interp (P, I, It);
6167 while Present (It.Typ) loop
6168 exit when Is_Access_Type (It.Typ)
6169 and then Covers (Typ, Designated_Type (It.Typ));
6170 Get_Next_Interp (I, It);
6171 end loop;
6172
6173 if Present (It.Typ) then
6174 Resolve (P, It.Typ);
6175 else
6176 -- If no interpretation covers the designated type of the prefix,
6177 -- this is the pathological case where not all implementations of
6178 -- the prefix allow the interpretation of the node as a call. Now
6179 -- that the expected type is known, Remove other interpretations
6180 -- from prefix, rewrite it as a call, and resolve again, so that
6181 -- the proper call node is generated.
6182
6183 Get_First_Interp (P, I, It);
6184 while Present (It.Typ) loop
6185 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6186 Remove_Interp (I);
6187 end if;
6188
6189 Get_Next_Interp (I, It);
6190 end loop;
6191
6192 New_N :=
6193 Make_Function_Call (Loc,
6194 Name =>
6195 Make_Explicit_Dereference (Loc,
6196 Prefix => P),
6197 Parameter_Associations => New_List);
6198
6199 Save_Interps (N, New_N);
6200 Rewrite (N, New_N);
6201 Analyze_And_Resolve (N, Typ);
6202 return;
6203 end if;
6204
6205 Set_Etype (N, Designated_Type (It.Typ));
6206
6207 else
6208 Resolve (P);
6209 end if;
6210
6211 if Is_Access_Type (Etype (P)) then
6212 Apply_Access_Check (N);
6213 end if;
6214
6215 -- If the designated type is a packed unconstrained array type, and the
6216 -- explicit dereference is not in the context of an attribute reference,
6217 -- then we must compute and set the actual subtype, since it is needed
6218 -- by Gigi. The reason we exclude the attribute case is that this is
6219 -- handled fine by Gigi, and in fact we use such attributes to build the
6220 -- actual subtype. We also exclude generated code (which builds actual
6221 -- subtypes directly if they are needed).
6222
6223 if Is_Array_Type (Etype (N))
6224 and then Is_Packed (Etype (N))
6225 and then not Is_Constrained (Etype (N))
6226 and then Nkind (Parent (N)) /= N_Attribute_Reference
6227 and then Comes_From_Source (N)
6228 then
6229 Set_Etype (N, Get_Actual_Subtype (N));
6230 end if;
6231
6232 -- Note: there is no Eval processing required for an explicit deference,
6233 -- because the type is known to be an allocators, and allocator
6234 -- expressions can never be static.
6235
6236 end Resolve_Explicit_Dereference;
6237
6238 -------------------------------
6239 -- Resolve_Indexed_Component --
6240 -------------------------------
6241
6242 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6243 Name : constant Node_Id := Prefix (N);
6244 Expr : Node_Id;
6245 Array_Type : Entity_Id := Empty; -- to prevent junk warning
6246 Index : Node_Id;
6247
6248 begin
6249 if Is_Overloaded (Name) then
6250
6251 -- Use the context type to select the prefix that yields the correct
6252 -- component type.
6253
6254 declare
6255 I : Interp_Index;
6256 It : Interp;
6257 I1 : Interp_Index := 0;
6258 P : constant Node_Id := Prefix (N);
6259 Found : Boolean := False;
6260
6261 begin
6262 Get_First_Interp (P, I, It);
6263 while Present (It.Typ) loop
6264 if (Is_Array_Type (It.Typ)
6265 and then Covers (Typ, Component_Type (It.Typ)))
6266 or else (Is_Access_Type (It.Typ)
6267 and then Is_Array_Type (Designated_Type (It.Typ))
6268 and then Covers
6269 (Typ, Component_Type (Designated_Type (It.Typ))))
6270 then
6271 if Found then
6272 It := Disambiguate (P, I1, I, Any_Type);
6273
6274 if It = No_Interp then
6275 Error_Msg_N ("ambiguous prefix for indexing", N);
6276 Set_Etype (N, Typ);
6277 return;
6278
6279 else
6280 Found := True;
6281 Array_Type := It.Typ;
6282 I1 := I;
6283 end if;
6284
6285 else
6286 Found := True;
6287 Array_Type := It.Typ;
6288 I1 := I;
6289 end if;
6290 end if;
6291
6292 Get_Next_Interp (I, It);
6293 end loop;
6294 end;
6295
6296 else
6297 Array_Type := Etype (Name);
6298 end if;
6299
6300 Resolve (Name, Array_Type);
6301 Array_Type := Get_Actual_Subtype_If_Available (Name);
6302
6303 -- If prefix is access type, dereference to get real array type.
6304 -- Note: we do not apply an access check because the expander always
6305 -- introduces an explicit dereference, and the check will happen there.
6306
6307 if Is_Access_Type (Array_Type) then
6308 Array_Type := Designated_Type (Array_Type);
6309 end if;
6310
6311 -- If name was overloaded, set component type correctly now
6312 -- If a misplaced call to an entry family (which has no index types)
6313 -- return. Error will be diagnosed from calling context.
6314
6315 if Is_Array_Type (Array_Type) then
6316 Set_Etype (N, Component_Type (Array_Type));
6317 else
6318 return;
6319 end if;
6320
6321 Index := First_Index (Array_Type);
6322 Expr := First (Expressions (N));
6323
6324 -- The prefix may have resolved to a string literal, in which case its
6325 -- etype has a special representation. This is only possible currently
6326 -- if the prefix is a static concatenation, written in functional
6327 -- notation.
6328
6329 if Ekind (Array_Type) = E_String_Literal_Subtype then
6330 Resolve (Expr, Standard_Positive);
6331
6332 else
6333 while Present (Index) and Present (Expr) loop
6334 Resolve (Expr, Etype (Index));
6335 Check_Unset_Reference (Expr);
6336
6337 if Is_Scalar_Type (Etype (Expr)) then
6338 Apply_Scalar_Range_Check (Expr, Etype (Index));
6339 else
6340 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
6341 end if;
6342
6343 Next_Index (Index);
6344 Next (Expr);
6345 end loop;
6346 end if;
6347
6348 -- Do not generate the warning on suspicious index if we are analyzing
6349 -- package Ada.Tags; otherwise we will report the warning with the
6350 -- Prims_Ptr field of the dispatch table.
6351
6352 if Scope (Etype (Prefix (N))) = Standard_Standard
6353 or else not
6354 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
6355 Ada_Tags)
6356 then
6357 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
6358 Eval_Indexed_Component (N);
6359 end if;
6360 end Resolve_Indexed_Component;
6361
6362 -----------------------------
6363 -- Resolve_Integer_Literal --
6364 -----------------------------
6365
6366 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
6367 begin
6368 Set_Etype (N, Typ);
6369 Eval_Integer_Literal (N);
6370 end Resolve_Integer_Literal;
6371
6372 --------------------------------
6373 -- Resolve_Intrinsic_Operator --
6374 --------------------------------
6375
6376 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
6377 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6378 Op : Entity_Id;
6379 Arg1 : Node_Id;
6380 Arg2 : Node_Id;
6381
6382 begin
6383 Op := Entity (N);
6384 while Scope (Op) /= Standard_Standard loop
6385 Op := Homonym (Op);
6386 pragma Assert (Present (Op));
6387 end loop;
6388
6389 Set_Entity (N, Op);
6390 Set_Is_Overloaded (N, False);
6391
6392 -- If the operand type is private, rewrite with suitable conversions on
6393 -- the operands and the result, to expose the proper underlying numeric
6394 -- type.
6395
6396 if Is_Private_Type (Typ) then
6397 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
6398
6399 if Nkind (N) = N_Op_Expon then
6400 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
6401 else
6402 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6403 end if;
6404
6405 Save_Interps (Left_Opnd (N), Expression (Arg1));
6406 Save_Interps (Right_Opnd (N), Expression (Arg2));
6407
6408 Set_Left_Opnd (N, Arg1);
6409 Set_Right_Opnd (N, Arg2);
6410
6411 Set_Etype (N, Btyp);
6412 Rewrite (N, Unchecked_Convert_To (Typ, N));
6413 Resolve (N, Typ);
6414
6415 elsif Typ /= Etype (Left_Opnd (N))
6416 or else Typ /= Etype (Right_Opnd (N))
6417 then
6418 -- Add explicit conversion where needed, and save interpretations
6419 -- in case operands are overloaded.
6420
6421 Arg1 := Convert_To (Typ, Left_Opnd (N));
6422 Arg2 := Convert_To (Typ, Right_Opnd (N));
6423
6424 if Nkind (Arg1) = N_Type_Conversion then
6425 Save_Interps (Left_Opnd (N), Expression (Arg1));
6426 else
6427 Save_Interps (Left_Opnd (N), Arg1);
6428 end if;
6429
6430 if Nkind (Arg2) = N_Type_Conversion then
6431 Save_Interps (Right_Opnd (N), Expression (Arg2));
6432 else
6433 Save_Interps (Right_Opnd (N), Arg2);
6434 end if;
6435
6436 Rewrite (Left_Opnd (N), Arg1);
6437 Rewrite (Right_Opnd (N), Arg2);
6438 Analyze (Arg1);
6439 Analyze (Arg2);
6440 Resolve_Arithmetic_Op (N, Typ);
6441
6442 else
6443 Resolve_Arithmetic_Op (N, Typ);
6444 end if;
6445 end Resolve_Intrinsic_Operator;
6446
6447 --------------------------------------
6448 -- Resolve_Intrinsic_Unary_Operator --
6449 --------------------------------------
6450
6451 procedure Resolve_Intrinsic_Unary_Operator
6452 (N : Node_Id;
6453 Typ : Entity_Id)
6454 is
6455 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6456 Op : Entity_Id;
6457 Arg2 : Node_Id;
6458
6459 begin
6460 Op := Entity (N);
6461 while Scope (Op) /= Standard_Standard loop
6462 Op := Homonym (Op);
6463 pragma Assert (Present (Op));
6464 end loop;
6465
6466 Set_Entity (N, Op);
6467
6468 if Is_Private_Type (Typ) then
6469 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6470 Save_Interps (Right_Opnd (N), Expression (Arg2));
6471
6472 Set_Right_Opnd (N, Arg2);
6473
6474 Set_Etype (N, Btyp);
6475 Rewrite (N, Unchecked_Convert_To (Typ, N));
6476 Resolve (N, Typ);
6477
6478 else
6479 Resolve_Unary_Op (N, Typ);
6480 end if;
6481 end Resolve_Intrinsic_Unary_Operator;
6482
6483 ------------------------
6484 -- Resolve_Logical_Op --
6485 ------------------------
6486
6487 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
6488 B_Typ : Entity_Id;
6489 N_Opr : constant Node_Kind := Nkind (N);
6490
6491 begin
6492 -- Predefined operations on scalar types yield the base type. On the
6493 -- other hand, logical operations on arrays yield the type of the
6494 -- arguments (and the context).
6495
6496 if Is_Array_Type (Typ) then
6497 B_Typ := Typ;
6498 else
6499 B_Typ := Base_Type (Typ);
6500 end if;
6501
6502 -- The following test is required because the operands of the operation
6503 -- may be literals, in which case the resulting type appears to be
6504 -- compatible with a signed integer type, when in fact it is compatible
6505 -- only with modular types. If the context itself is universal, the
6506 -- operation is illegal.
6507
6508 if not Valid_Boolean_Arg (Typ) then
6509 Error_Msg_N ("invalid context for logical operation", N);
6510 Set_Etype (N, Any_Type);
6511 return;
6512
6513 elsif Typ = Any_Modular then
6514 Error_Msg_N
6515 ("no modular type available in this context", N);
6516 Set_Etype (N, Any_Type);
6517 return;
6518 elsif Is_Modular_Integer_Type (Typ)
6519 and then Etype (Left_Opnd (N)) = Universal_Integer
6520 and then Etype (Right_Opnd (N)) = Universal_Integer
6521 then
6522 Check_For_Visible_Operator (N, B_Typ);
6523 end if;
6524
6525 Resolve (Left_Opnd (N), B_Typ);
6526 Resolve (Right_Opnd (N), B_Typ);
6527
6528 Check_Unset_Reference (Left_Opnd (N));
6529 Check_Unset_Reference (Right_Opnd (N));
6530
6531 Set_Etype (N, B_Typ);
6532 Generate_Operator_Reference (N, B_Typ);
6533 Eval_Logical_Op (N);
6534
6535 -- Check for violation of restriction No_Direct_Boolean_Operators
6536 -- if the operator was not eliminated by the Eval_Logical_Op call.
6537
6538 if Nkind (N) = N_Opr
6539 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
6540 then
6541 Check_Restriction (No_Direct_Boolean_Operators, N);
6542 end if;
6543 end Resolve_Logical_Op;
6544
6545 ---------------------------
6546 -- Resolve_Membership_Op --
6547 ---------------------------
6548
6549 -- The context can only be a boolean type, and does not determine
6550 -- the arguments. Arguments should be unambiguous, but the preference
6551 -- rule for universal types applies.
6552
6553 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
6554 pragma Warnings (Off, Typ);
6555
6556 L : constant Node_Id := Left_Opnd (N);
6557 R : constant Node_Id := Right_Opnd (N);
6558 T : Entity_Id;
6559
6560 begin
6561 if L = Error or else R = Error then
6562 return;
6563 end if;
6564
6565 if not Is_Overloaded (R)
6566 and then
6567 (Etype (R) = Universal_Integer or else
6568 Etype (R) = Universal_Real)
6569 and then Is_Overloaded (L)
6570 then
6571 T := Etype (R);
6572
6573 -- Ada 2005 (AI-251): Give support to the following case:
6574
6575 -- type I is interface;
6576 -- type T is tagged ...
6577
6578 -- function Test (O : I'Class) is
6579 -- begin
6580 -- return O in T'Class.
6581 -- end Test;
6582
6583 -- In this case we have nothing else to do; the membership test will be
6584 -- done at run-time.
6585
6586 elsif Ada_Version >= Ada_05
6587 and then Is_Class_Wide_Type (Etype (L))
6588 and then Is_Interface (Etype (L))
6589 and then Is_Class_Wide_Type (Etype (R))
6590 and then not Is_Interface (Etype (R))
6591 then
6592 return;
6593
6594 else
6595 T := Intersect_Types (L, R);
6596 end if;
6597
6598 Resolve (L, T);
6599 Check_Unset_Reference (L);
6600
6601 if Nkind (R) = N_Range
6602 and then not Is_Scalar_Type (T)
6603 then
6604 Error_Msg_N ("scalar type required for range", R);
6605 end if;
6606
6607 if Is_Entity_Name (R) then
6608 Freeze_Expression (R);
6609 else
6610 Resolve (R, T);
6611 Check_Unset_Reference (R);
6612 end if;
6613
6614 Eval_Membership_Op (N);
6615 end Resolve_Membership_Op;
6616
6617 ------------------
6618 -- Resolve_Null --
6619 ------------------
6620
6621 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
6622 begin
6623 -- Handle restriction against anonymous null access values This
6624 -- restriction can be turned off using -gnatdj.
6625
6626 -- Ada 2005 (AI-231): Remove restriction
6627
6628 if Ada_Version < Ada_05
6629 and then not Debug_Flag_J
6630 and then Ekind (Typ) = E_Anonymous_Access_Type
6631 and then Comes_From_Source (N)
6632 then
6633 -- In the common case of a call which uses an explicitly null
6634 -- value for an access parameter, give specialized error message.
6635
6636 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
6637 N_Function_Call)
6638 then
6639 Error_Msg_N
6640 ("null is not allowed as argument for an access parameter", N);
6641
6642 -- Standard message for all other cases (are there any?)
6643
6644 else
6645 Error_Msg_N
6646 ("null cannot be of an anonymous access type", N);
6647 end if;
6648 end if;
6649
6650 -- In a distributed context, null for a remote access to subprogram
6651 -- may need to be replaced with a special record aggregate. In this
6652 -- case, return after having done the transformation.
6653
6654 if (Ekind (Typ) = E_Record_Type
6655 or else Is_Remote_Access_To_Subprogram_Type (Typ))
6656 and then Remote_AST_Null_Value (N, Typ)
6657 then
6658 return;
6659 end if;
6660
6661 -- The null literal takes its type from the context
6662
6663 Set_Etype (N, Typ);
6664 end Resolve_Null;
6665
6666 -----------------------
6667 -- Resolve_Op_Concat --
6668 -----------------------
6669
6670 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
6671
6672 -- We wish to avoid deep recursion, because concatenations are often
6673 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
6674 -- operands nonrecursively until we find something that is not a simple
6675 -- concatenation (A in this case). We resolve that, and then walk back
6676 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
6677 -- to do the rest of the work at each level. The Parent pointers allow
6678 -- us to avoid recursion, and thus avoid running out of memory. See also
6679 -- Sem_Ch4.Analyze_Concatenation, where a similar hack is used.
6680
6681 NN : Node_Id := N;
6682 Op1 : Node_Id;
6683
6684 begin
6685 -- The following code is equivalent to:
6686
6687 -- Resolve_Op_Concat_First (NN, Typ);
6688 -- Resolve_Op_Concat_Arg (N, ...);
6689 -- Resolve_Op_Concat_Rest (N, Typ);
6690
6691 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
6692 -- operand is a concatenation.
6693
6694 -- Walk down left operands
6695
6696 loop
6697 Resolve_Op_Concat_First (NN, Typ);
6698 Op1 := Left_Opnd (NN);
6699 exit when not (Nkind (Op1) = N_Op_Concat
6700 and then not Is_Array_Type (Component_Type (Typ))
6701 and then Entity (Op1) = Entity (NN));
6702 NN := Op1;
6703 end loop;
6704
6705 -- Now (given the above example) NN is A&B and Op1 is A
6706
6707 -- First resolve Op1 ...
6708
6709 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
6710
6711 -- ... then walk NN back up until we reach N (where we started), calling
6712 -- Resolve_Op_Concat_Rest along the way.
6713
6714 loop
6715 Resolve_Op_Concat_Rest (NN, Typ);
6716 exit when NN = N;
6717 NN := Parent (NN);
6718 end loop;
6719 end Resolve_Op_Concat;
6720
6721 ---------------------------
6722 -- Resolve_Op_Concat_Arg --
6723 ---------------------------
6724
6725 procedure Resolve_Op_Concat_Arg
6726 (N : Node_Id;
6727 Arg : Node_Id;
6728 Typ : Entity_Id;
6729 Is_Comp : Boolean)
6730 is
6731 Btyp : constant Entity_Id := Base_Type (Typ);
6732
6733 begin
6734 if In_Instance then
6735 if Is_Comp
6736 or else (not Is_Overloaded (Arg)
6737 and then Etype (Arg) /= Any_Composite
6738 and then Covers (Component_Type (Typ), Etype (Arg)))
6739 then
6740 Resolve (Arg, Component_Type (Typ));
6741 else
6742 Resolve (Arg, Btyp);
6743 end if;
6744
6745 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
6746 if Nkind (Arg) = N_Aggregate
6747 and then Is_Composite_Type (Component_Type (Typ))
6748 then
6749 if Is_Private_Type (Component_Type (Typ)) then
6750 Resolve (Arg, Btyp);
6751 else
6752 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
6753 Set_Etype (Arg, Any_Type);
6754 end if;
6755
6756 else
6757 if Is_Overloaded (Arg)
6758 and then Has_Compatible_Type (Arg, Typ)
6759 and then Etype (Arg) /= Any_Type
6760 then
6761 declare
6762 I : Interp_Index;
6763 It : Interp;
6764 Func : Entity_Id;
6765
6766 begin
6767 Get_First_Interp (Arg, I, It);
6768 Func := It.Nam;
6769 Get_Next_Interp (I, It);
6770
6771 -- Special-case the error message when the overloading is
6772 -- caused by a function that yields an array and can be
6773 -- called without parameters.
6774
6775 if It.Nam = Func then
6776 Error_Msg_Sloc := Sloc (Func);
6777 Error_Msg_N ("ambiguous call to function#", Arg);
6778 Error_Msg_NE
6779 ("\\interpretation as call yields&", Arg, Typ);
6780 Error_Msg_NE
6781 ("\\interpretation as indexing of call yields&",
6782 Arg, Component_Type (Typ));
6783
6784 else
6785 Error_Msg_N
6786 ("ambiguous operand for concatenation!", Arg);
6787 Get_First_Interp (Arg, I, It);
6788 while Present (It.Nam) loop
6789 Error_Msg_Sloc := Sloc (It.Nam);
6790
6791 if Base_Type (It.Typ) = Base_Type (Typ)
6792 or else Base_Type (It.Typ) =
6793 Base_Type (Component_Type (Typ))
6794 then
6795 Error_Msg_N ("\\possible interpretation#", Arg);
6796 end if;
6797
6798 Get_Next_Interp (I, It);
6799 end loop;
6800 end if;
6801 end;
6802 end if;
6803
6804 Resolve (Arg, Component_Type (Typ));
6805
6806 if Nkind (Arg) = N_String_Literal then
6807 Set_Etype (Arg, Component_Type (Typ));
6808 end if;
6809
6810 if Arg = Left_Opnd (N) then
6811 Set_Is_Component_Left_Opnd (N);
6812 else
6813 Set_Is_Component_Right_Opnd (N);
6814 end if;
6815 end if;
6816
6817 else
6818 Resolve (Arg, Btyp);
6819 end if;
6820
6821 Check_Unset_Reference (Arg);
6822 end Resolve_Op_Concat_Arg;
6823
6824 -----------------------------
6825 -- Resolve_Op_Concat_First --
6826 -----------------------------
6827
6828 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
6829 Btyp : constant Entity_Id := Base_Type (Typ);
6830 Op1 : constant Node_Id := Left_Opnd (N);
6831 Op2 : constant Node_Id := Right_Opnd (N);
6832
6833 begin
6834 -- The parser folds an enormous sequence of concatenations of string
6835 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
6836 -- in the right. If the expression resolves to a predefined "&"
6837 -- operator, all is well. Otherwise, the parser's folding is wrong, so
6838 -- we give an error. See P_Simple_Expression in Par.Ch4.
6839
6840 if Nkind (Op2) = N_String_Literal
6841 and then Is_Folded_In_Parser (Op2)
6842 and then Ekind (Entity (N)) = E_Function
6843 then
6844 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
6845 and then String_Length (Strval (Op1)) = 0);
6846 Error_Msg_N ("too many user-defined concatenations", N);
6847 return;
6848 end if;
6849
6850 Set_Etype (N, Btyp);
6851
6852 if Is_Limited_Composite (Btyp) then
6853 Error_Msg_N ("concatenation not available for limited array", N);
6854 Explain_Limited_Type (Btyp, N);
6855 end if;
6856 end Resolve_Op_Concat_First;
6857
6858 ----------------------------
6859 -- Resolve_Op_Concat_Rest --
6860 ----------------------------
6861
6862 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
6863 Op1 : constant Node_Id := Left_Opnd (N);
6864 Op2 : constant Node_Id := Right_Opnd (N);
6865
6866 begin
6867 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
6868
6869 Generate_Operator_Reference (N, Typ);
6870
6871 if Is_String_Type (Typ) then
6872 Eval_Concatenation (N);
6873 end if;
6874
6875 -- If this is not a static concatenation, but the result is a
6876 -- string type (and not an array of strings) ensure that static
6877 -- string operands have their subtypes properly constructed.
6878
6879 if Nkind (N) /= N_String_Literal
6880 and then Is_Character_Type (Component_Type (Typ))
6881 then
6882 Set_String_Literal_Subtype (Op1, Typ);
6883 Set_String_Literal_Subtype (Op2, Typ);
6884 end if;
6885 end Resolve_Op_Concat_Rest;
6886
6887 ----------------------
6888 -- Resolve_Op_Expon --
6889 ----------------------
6890
6891 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
6892 B_Typ : constant Entity_Id := Base_Type (Typ);
6893
6894 begin
6895 -- Catch attempts to do fixed-point exponentiation with universal
6896 -- operands, which is a case where the illegality is not caught during
6897 -- normal operator analysis.
6898
6899 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
6900 Error_Msg_N ("exponentiation not available for fixed point", N);
6901 return;
6902 end if;
6903
6904 if Comes_From_Source (N)
6905 and then Ekind (Entity (N)) = E_Function
6906 and then Is_Imported (Entity (N))
6907 and then Is_Intrinsic_Subprogram (Entity (N))
6908 then
6909 Resolve_Intrinsic_Operator (N, Typ);
6910 return;
6911 end if;
6912
6913 if Etype (Left_Opnd (N)) = Universal_Integer
6914 or else Etype (Left_Opnd (N)) = Universal_Real
6915 then
6916 Check_For_Visible_Operator (N, B_Typ);
6917 end if;
6918
6919 -- We do the resolution using the base type, because intermediate values
6920 -- in expressions always are of the base type, not a subtype of it.
6921
6922 Resolve (Left_Opnd (N), B_Typ);
6923 Resolve (Right_Opnd (N), Standard_Integer);
6924
6925 Check_Unset_Reference (Left_Opnd (N));
6926 Check_Unset_Reference (Right_Opnd (N));
6927
6928 Set_Etype (N, B_Typ);
6929 Generate_Operator_Reference (N, B_Typ);
6930 Eval_Op_Expon (N);
6931
6932 -- Set overflow checking bit. Much cleverer code needed here eventually
6933 -- and perhaps the Resolve routines should be separated for the various
6934 -- arithmetic operations, since they will need different processing. ???
6935
6936 if Nkind (N) in N_Op then
6937 if not Overflow_Checks_Suppressed (Etype (N)) then
6938 Enable_Overflow_Check (N);
6939 end if;
6940 end if;
6941 end Resolve_Op_Expon;
6942
6943 --------------------
6944 -- Resolve_Op_Not --
6945 --------------------
6946
6947 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
6948 B_Typ : Entity_Id;
6949
6950 function Parent_Is_Boolean return Boolean;
6951 -- This function determines if the parent node is a boolean operator
6952 -- or operation (comparison op, membership test, or short circuit form)
6953 -- and the not in question is the left operand of this operation.
6954 -- Note that if the not is in parens, then false is returned.
6955
6956 -----------------------
6957 -- Parent_Is_Boolean --
6958 -----------------------
6959
6960 function Parent_Is_Boolean return Boolean is
6961 begin
6962 if Paren_Count (N) /= 0 then
6963 return False;
6964
6965 else
6966 case Nkind (Parent (N)) is
6967 when N_Op_And |
6968 N_Op_Eq |
6969 N_Op_Ge |
6970 N_Op_Gt |
6971 N_Op_Le |
6972 N_Op_Lt |
6973 N_Op_Ne |
6974 N_Op_Or |
6975 N_Op_Xor |
6976 N_In |
6977 N_Not_In |
6978 N_And_Then |
6979 N_Or_Else =>
6980
6981 return Left_Opnd (Parent (N)) = N;
6982
6983 when others =>
6984 return False;
6985 end case;
6986 end if;
6987 end Parent_Is_Boolean;
6988
6989 -- Start of processing for Resolve_Op_Not
6990
6991 begin
6992 -- Predefined operations on scalar types yield the base type. On the
6993 -- other hand, logical operations on arrays yield the type of the
6994 -- arguments (and the context).
6995
6996 if Is_Array_Type (Typ) then
6997 B_Typ := Typ;
6998 else
6999 B_Typ := Base_Type (Typ);
7000 end if;
7001
7002 -- Straightforward case of incorrect arguments
7003
7004 if not Valid_Boolean_Arg (Typ) then
7005 Error_Msg_N ("invalid operand type for operator&", N);
7006 Set_Etype (N, Any_Type);
7007 return;
7008
7009 -- Special case of probable missing parens
7010
7011 elsif Typ = Universal_Integer or else Typ = Any_Modular then
7012 if Parent_Is_Boolean then
7013 Error_Msg_N
7014 ("operand of not must be enclosed in parentheses",
7015 Right_Opnd (N));
7016 else
7017 Error_Msg_N
7018 ("no modular type available in this context", N);
7019 end if;
7020
7021 Set_Etype (N, Any_Type);
7022 return;
7023
7024 -- OK resolution of not
7025
7026 else
7027 -- Warn if non-boolean types involved. This is a case like not a < b
7028 -- where a and b are modular, where we will get (not a) < b and most
7029 -- likely not (a < b) was intended.
7030
7031 if Warn_On_Questionable_Missing_Parens
7032 and then not Is_Boolean_Type (Typ)
7033 and then Parent_Is_Boolean
7034 then
7035 Error_Msg_N ("?not expression should be parenthesized here!", N);
7036 end if;
7037
7038 -- Warn on double negation if checking redundant constructs
7039
7040 if Warn_On_Redundant_Constructs
7041 and then Comes_From_Source (N)
7042 and then Comes_From_Source (Right_Opnd (N))
7043 and then Root_Type (Typ) = Standard_Boolean
7044 and then Nkind (Right_Opnd (N)) = N_Op_Not
7045 then
7046 Error_Msg_N ("redundant double negation?", N);
7047 end if;
7048
7049 -- Complete resolution and evaluation of NOT
7050
7051 Resolve (Right_Opnd (N), B_Typ);
7052 Check_Unset_Reference (Right_Opnd (N));
7053 Set_Etype (N, B_Typ);
7054 Generate_Operator_Reference (N, B_Typ);
7055 Eval_Op_Not (N);
7056 end if;
7057 end Resolve_Op_Not;
7058
7059 -----------------------------
7060 -- Resolve_Operator_Symbol --
7061 -----------------------------
7062
7063 -- Nothing to be done, all resolved already
7064
7065 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
7066 pragma Warnings (Off, N);
7067 pragma Warnings (Off, Typ);
7068
7069 begin
7070 null;
7071 end Resolve_Operator_Symbol;
7072
7073 ----------------------------------
7074 -- Resolve_Qualified_Expression --
7075 ----------------------------------
7076
7077 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
7078 pragma Warnings (Off, Typ);
7079
7080 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7081 Expr : constant Node_Id := Expression (N);
7082
7083 begin
7084 Resolve (Expr, Target_Typ);
7085
7086 -- A qualified expression requires an exact match of the type,
7087 -- class-wide matching is not allowed. However, if the qualifying
7088 -- type is specific and the expression has a class-wide type, it
7089 -- may still be okay, since it can be the result of the expansion
7090 -- of a call to a dispatching function, so we also have to check
7091 -- class-wideness of the type of the expression's original node.
7092
7093 if (Is_Class_Wide_Type (Target_Typ)
7094 or else
7095 (Is_Class_Wide_Type (Etype (Expr))
7096 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
7097 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
7098 then
7099 Wrong_Type (Expr, Target_Typ);
7100 end if;
7101
7102 -- If the target type is unconstrained, then we reset the type of
7103 -- the result from the type of the expression. For other cases, the
7104 -- actual subtype of the expression is the target type.
7105
7106 if Is_Composite_Type (Target_Typ)
7107 and then not Is_Constrained (Target_Typ)
7108 then
7109 Set_Etype (N, Etype (Expr));
7110 end if;
7111
7112 Eval_Qualified_Expression (N);
7113 end Resolve_Qualified_Expression;
7114
7115 -------------------
7116 -- Resolve_Range --
7117 -------------------
7118
7119 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
7120 L : constant Node_Id := Low_Bound (N);
7121 H : constant Node_Id := High_Bound (N);
7122
7123 begin
7124 Set_Etype (N, Typ);
7125 Resolve (L, Typ);
7126 Resolve (H, Typ);
7127
7128 Check_Unset_Reference (L);
7129 Check_Unset_Reference (H);
7130
7131 -- We have to check the bounds for being within the base range as
7132 -- required for a non-static context. Normally this is automatic and
7133 -- done as part of evaluating expressions, but the N_Range node is an
7134 -- exception, since in GNAT we consider this node to be a subexpression,
7135 -- even though in Ada it is not. The circuit in Sem_Eval could check for
7136 -- this, but that would put the test on the main evaluation path for
7137 -- expressions.
7138
7139 Check_Non_Static_Context (L);
7140 Check_Non_Static_Context (H);
7141
7142 -- Check for an ambiguous range over character literals. This will
7143 -- happen with a membership test involving only literals.
7144
7145 if Typ = Any_Character then
7146 Ambiguous_Character (L);
7147 Set_Etype (N, Any_Type);
7148 return;
7149 end if;
7150
7151 -- If bounds are static, constant-fold them, so size computations
7152 -- are identical between front-end and back-end. Do not perform this
7153 -- transformation while analyzing generic units, as type information
7154 -- would then be lost when reanalyzing the constant node in the
7155 -- instance.
7156
7157 if Is_Discrete_Type (Typ) and then Expander_Active then
7158 if Is_OK_Static_Expression (L) then
7159 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
7160 end if;
7161
7162 if Is_OK_Static_Expression (H) then
7163 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
7164 end if;
7165 end if;
7166 end Resolve_Range;
7167
7168 --------------------------
7169 -- Resolve_Real_Literal --
7170 --------------------------
7171
7172 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
7173 Actual_Typ : constant Entity_Id := Etype (N);
7174
7175 begin
7176 -- Special processing for fixed-point literals to make sure that the
7177 -- value is an exact multiple of small where this is required. We
7178 -- skip this for the universal real case, and also for generic types.
7179
7180 if Is_Fixed_Point_Type (Typ)
7181 and then Typ /= Universal_Fixed
7182 and then Typ /= Any_Fixed
7183 and then not Is_Generic_Type (Typ)
7184 then
7185 declare
7186 Val : constant Ureal := Realval (N);
7187 Cintr : constant Ureal := Val / Small_Value (Typ);
7188 Cint : constant Uint := UR_Trunc (Cintr);
7189 Den : constant Uint := Norm_Den (Cintr);
7190 Stat : Boolean;
7191
7192 begin
7193 -- Case of literal is not an exact multiple of the Small
7194
7195 if Den /= 1 then
7196
7197 -- For a source program literal for a decimal fixed-point
7198 -- type, this is statically illegal (RM 4.9(36)).
7199
7200 if Is_Decimal_Fixed_Point_Type (Typ)
7201 and then Actual_Typ = Universal_Real
7202 and then Comes_From_Source (N)
7203 then
7204 Error_Msg_N ("value has extraneous low order digits", N);
7205 end if;
7206
7207 -- Generate a warning if literal from source
7208
7209 if Is_Static_Expression (N)
7210 and then Warn_On_Bad_Fixed_Value
7211 then
7212 Error_Msg_N
7213 ("?static fixed-point value is not a multiple of Small!",
7214 N);
7215 end if;
7216
7217 -- Replace literal by a value that is the exact representation
7218 -- of a value of the type, i.e. a multiple of the small value,
7219 -- by truncation, since Machine_Rounds is false for all GNAT
7220 -- fixed-point types (RM 4.9(38)).
7221
7222 Stat := Is_Static_Expression (N);
7223 Rewrite (N,
7224 Make_Real_Literal (Sloc (N),
7225 Realval => Small_Value (Typ) * Cint));
7226
7227 Set_Is_Static_Expression (N, Stat);
7228 end if;
7229
7230 -- In all cases, set the corresponding integer field
7231
7232 Set_Corresponding_Integer_Value (N, Cint);
7233 end;
7234 end if;
7235
7236 -- Now replace the actual type by the expected type as usual
7237
7238 Set_Etype (N, Typ);
7239 Eval_Real_Literal (N);
7240 end Resolve_Real_Literal;
7241
7242 -----------------------
7243 -- Resolve_Reference --
7244 -----------------------
7245
7246 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
7247 P : constant Node_Id := Prefix (N);
7248
7249 begin
7250 -- Replace general access with specific type
7251
7252 if Ekind (Etype (N)) = E_Allocator_Type then
7253 Set_Etype (N, Base_Type (Typ));
7254 end if;
7255
7256 Resolve (P, Designated_Type (Etype (N)));
7257
7258 -- If we are taking the reference of a volatile entity, then treat
7259 -- it as a potential modification of this entity. This is much too
7260 -- conservative, but is necessary because remove side effects can
7261 -- result in transformations of normal assignments into reference
7262 -- sequences that otherwise fail to notice the modification.
7263
7264 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
7265 Note_Possible_Modification (P, Sure => False);
7266 end if;
7267 end Resolve_Reference;
7268
7269 --------------------------------
7270 -- Resolve_Selected_Component --
7271 --------------------------------
7272
7273 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
7274 Comp : Entity_Id;
7275 Comp1 : Entity_Id := Empty; -- prevent junk warning
7276 P : constant Node_Id := Prefix (N);
7277 S : constant Node_Id := Selector_Name (N);
7278 T : Entity_Id := Etype (P);
7279 I : Interp_Index;
7280 I1 : Interp_Index := 0; -- prevent junk warning
7281 It : Interp;
7282 It1 : Interp;
7283 Found : Boolean;
7284
7285 function Init_Component return Boolean;
7286 -- Check whether this is the initialization of a component within an
7287 -- init proc (by assignment or call to another init proc). If true,
7288 -- there is no need for a discriminant check.
7289
7290 --------------------
7291 -- Init_Component --
7292 --------------------
7293
7294 function Init_Component return Boolean is
7295 begin
7296 return Inside_Init_Proc
7297 and then Nkind (Prefix (N)) = N_Identifier
7298 and then Chars (Prefix (N)) = Name_uInit
7299 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
7300 end Init_Component;
7301
7302 -- Start of processing for Resolve_Selected_Component
7303
7304 begin
7305 if Is_Overloaded (P) then
7306
7307 -- Use the context type to select the prefix that has a selector
7308 -- of the correct name and type.
7309
7310 Found := False;
7311 Get_First_Interp (P, I, It);
7312
7313 Search : while Present (It.Typ) loop
7314 if Is_Access_Type (It.Typ) then
7315 T := Designated_Type (It.Typ);
7316 else
7317 T := It.Typ;
7318 end if;
7319
7320 if Is_Record_Type (T) then
7321
7322 -- The visible components of a class-wide type are those of
7323 -- the root type.
7324
7325 if Is_Class_Wide_Type (T) then
7326 T := Etype (T);
7327 end if;
7328
7329 Comp := First_Entity (T);
7330 while Present (Comp) loop
7331 if Chars (Comp) = Chars (S)
7332 and then Covers (Etype (Comp), Typ)
7333 then
7334 if not Found then
7335 Found := True;
7336 I1 := I;
7337 It1 := It;
7338 Comp1 := Comp;
7339
7340 else
7341 It := Disambiguate (P, I1, I, Any_Type);
7342
7343 if It = No_Interp then
7344 Error_Msg_N
7345 ("ambiguous prefix for selected component", N);
7346 Set_Etype (N, Typ);
7347 return;
7348
7349 else
7350 It1 := It;
7351
7352 -- There may be an implicit dereference. Retrieve
7353 -- designated record type.
7354
7355 if Is_Access_Type (It1.Typ) then
7356 T := Designated_Type (It1.Typ);
7357 else
7358 T := It1.Typ;
7359 end if;
7360
7361 if Scope (Comp1) /= T then
7362
7363 -- Resolution chooses the new interpretation.
7364 -- Find the component with the right name.
7365
7366 Comp1 := First_Entity (T);
7367 while Present (Comp1)
7368 and then Chars (Comp1) /= Chars (S)
7369 loop
7370 Comp1 := Next_Entity (Comp1);
7371 end loop;
7372 end if;
7373
7374 exit Search;
7375 end if;
7376 end if;
7377 end if;
7378
7379 Comp := Next_Entity (Comp);
7380 end loop;
7381
7382 end if;
7383
7384 Get_Next_Interp (I, It);
7385 end loop Search;
7386
7387 Resolve (P, It1.Typ);
7388 Set_Etype (N, Typ);
7389 Set_Entity_With_Style_Check (S, Comp1);
7390
7391 else
7392 -- Resolve prefix with its type
7393
7394 Resolve (P, T);
7395 end if;
7396
7397 -- Generate cross-reference. We needed to wait until full overloading
7398 -- resolution was complete to do this, since otherwise we can't tell if
7399 -- we are an Lvalue of not.
7400
7401 if May_Be_Lvalue (N) then
7402 Generate_Reference (Entity (S), S, 'm');
7403 else
7404 Generate_Reference (Entity (S), S, 'r');
7405 end if;
7406
7407 -- If prefix is an access type, the node will be transformed into an
7408 -- explicit dereference during expansion. The type of the node is the
7409 -- designated type of that of the prefix.
7410
7411 if Is_Access_Type (Etype (P)) then
7412 T := Designated_Type (Etype (P));
7413 Check_Fully_Declared_Prefix (T, P);
7414 else
7415 T := Etype (P);
7416 end if;
7417
7418 if Has_Discriminants (T)
7419 and then (Ekind (Entity (S)) = E_Component
7420 or else
7421 Ekind (Entity (S)) = E_Discriminant)
7422 and then Present (Original_Record_Component (Entity (S)))
7423 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
7424 and then Present (Discriminant_Checking_Func
7425 (Original_Record_Component (Entity (S))))
7426 and then not Discriminant_Checks_Suppressed (T)
7427 and then not Init_Component
7428 then
7429 Set_Do_Discriminant_Check (N);
7430 end if;
7431
7432 if Ekind (Entity (S)) = E_Void then
7433 Error_Msg_N ("premature use of component", S);
7434 end if;
7435
7436 -- If the prefix is a record conversion, this may be a renamed
7437 -- discriminant whose bounds differ from those of the original
7438 -- one, so we must ensure that a range check is performed.
7439
7440 if Nkind (P) = N_Type_Conversion
7441 and then Ekind (Entity (S)) = E_Discriminant
7442 and then Is_Discrete_Type (Typ)
7443 then
7444 Set_Etype (N, Base_Type (Typ));
7445 end if;
7446
7447 -- Note: No Eval processing is required, because the prefix is of a
7448 -- record type, or protected type, and neither can possibly be static.
7449
7450 end Resolve_Selected_Component;
7451
7452 -------------------
7453 -- Resolve_Shift --
7454 -------------------
7455
7456 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
7457 B_Typ : constant Entity_Id := Base_Type (Typ);
7458 L : constant Node_Id := Left_Opnd (N);
7459 R : constant Node_Id := Right_Opnd (N);
7460
7461 begin
7462 -- We do the resolution using the base type, because intermediate values
7463 -- in expressions always are of the base type, not a subtype of it.
7464
7465 Resolve (L, B_Typ);
7466 Resolve (R, Standard_Natural);
7467
7468 Check_Unset_Reference (L);
7469 Check_Unset_Reference (R);
7470
7471 Set_Etype (N, B_Typ);
7472 Generate_Operator_Reference (N, B_Typ);
7473 Eval_Shift (N);
7474 end Resolve_Shift;
7475
7476 ---------------------------
7477 -- Resolve_Short_Circuit --
7478 ---------------------------
7479
7480 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
7481 B_Typ : constant Entity_Id := Base_Type (Typ);
7482 L : constant Node_Id := Left_Opnd (N);
7483 R : constant Node_Id := Right_Opnd (N);
7484
7485 begin
7486 Resolve (L, B_Typ);
7487 Resolve (R, B_Typ);
7488
7489 -- Check for issuing warning for always False assert/check, this happens
7490 -- when assertions are turned off, in which case the pragma Assert/Check
7491 -- was transformed into:
7492
7493 -- if False and then <condition> then ...
7494
7495 -- and we detect this pattern
7496
7497 if Warn_On_Assertion_Failure
7498 and then Is_Entity_Name (R)
7499 and then Entity (R) = Standard_False
7500 and then Nkind (Parent (N)) = N_If_Statement
7501 and then Nkind (N) = N_And_Then
7502 and then Is_Entity_Name (L)
7503 and then Entity (L) = Standard_False
7504 then
7505 declare
7506 Orig : constant Node_Id := Original_Node (Parent (N));
7507
7508 begin
7509 if Nkind (Orig) = N_Pragma
7510 and then Pragma_Name (Orig) = Name_Assert
7511 then
7512 -- Don't want to warn if original condition is explicit False
7513
7514 declare
7515 Expr : constant Node_Id :=
7516 Original_Node
7517 (Expression
7518 (First (Pragma_Argument_Associations (Orig))));
7519 begin
7520 if Is_Entity_Name (Expr)
7521 and then Entity (Expr) = Standard_False
7522 then
7523 null;
7524 else
7525 -- Issue warning. Note that we don't want to make this
7526 -- an unconditional warning, because if the assert is
7527 -- within deleted code we do not want the warning. But
7528 -- we do not want the deletion of the IF/AND-THEN to
7529 -- take this message with it. We achieve this by making
7530 -- sure that the expanded code points to the Sloc of
7531 -- the expression, not the original pragma.
7532
7533 Error_Msg_N ("?assertion would fail at run-time", Orig);
7534 end if;
7535 end;
7536
7537 -- Similar processing for Check pragma
7538
7539 elsif Nkind (Orig) = N_Pragma
7540 and then Pragma_Name (Orig) = Name_Check
7541 then
7542 -- Don't want to warn if original condition is explicit False
7543
7544 declare
7545 Expr : constant Node_Id :=
7546 Original_Node
7547 (Expression
7548 (Next (First
7549 (Pragma_Argument_Associations (Orig)))));
7550 begin
7551 if Is_Entity_Name (Expr)
7552 and then Entity (Expr) = Standard_False
7553 then
7554 null;
7555 else
7556 Error_Msg_N ("?check would fail at run-time", Orig);
7557 end if;
7558 end;
7559 end if;
7560 end;
7561 end if;
7562
7563 -- Continue with processing of short circuit
7564
7565 Check_Unset_Reference (L);
7566 Check_Unset_Reference (R);
7567
7568 Set_Etype (N, B_Typ);
7569 Eval_Short_Circuit (N);
7570 end Resolve_Short_Circuit;
7571
7572 -------------------
7573 -- Resolve_Slice --
7574 -------------------
7575
7576 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
7577 Name : constant Node_Id := Prefix (N);
7578 Drange : constant Node_Id := Discrete_Range (N);
7579 Array_Type : Entity_Id := Empty;
7580 Index : Node_Id;
7581
7582 begin
7583 if Is_Overloaded (Name) then
7584
7585 -- Use the context type to select the prefix that yields the
7586 -- correct array type.
7587
7588 declare
7589 I : Interp_Index;
7590 I1 : Interp_Index := 0;
7591 It : Interp;
7592 P : constant Node_Id := Prefix (N);
7593 Found : Boolean := False;
7594
7595 begin
7596 Get_First_Interp (P, I, It);
7597 while Present (It.Typ) loop
7598 if (Is_Array_Type (It.Typ)
7599 and then Covers (Typ, It.Typ))
7600 or else (Is_Access_Type (It.Typ)
7601 and then Is_Array_Type (Designated_Type (It.Typ))
7602 and then Covers (Typ, Designated_Type (It.Typ)))
7603 then
7604 if Found then
7605 It := Disambiguate (P, I1, I, Any_Type);
7606
7607 if It = No_Interp then
7608 Error_Msg_N ("ambiguous prefix for slicing", N);
7609 Set_Etype (N, Typ);
7610 return;
7611 else
7612 Found := True;
7613 Array_Type := It.Typ;
7614 I1 := I;
7615 end if;
7616 else
7617 Found := True;
7618 Array_Type := It.Typ;
7619 I1 := I;
7620 end if;
7621 end if;
7622
7623 Get_Next_Interp (I, It);
7624 end loop;
7625 end;
7626
7627 else
7628 Array_Type := Etype (Name);
7629 end if;
7630
7631 Resolve (Name, Array_Type);
7632
7633 if Is_Access_Type (Array_Type) then
7634 Apply_Access_Check (N);
7635 Array_Type := Designated_Type (Array_Type);
7636
7637 -- If the prefix is an access to an unconstrained array, we must use
7638 -- the actual subtype of the object to perform the index checks. The
7639 -- object denoted by the prefix is implicit in the node, so we build
7640 -- an explicit representation for it in order to compute the actual
7641 -- subtype.
7642
7643 if not Is_Constrained (Array_Type) then
7644 Remove_Side_Effects (Prefix (N));
7645
7646 declare
7647 Obj : constant Node_Id :=
7648 Make_Explicit_Dereference (Sloc (N),
7649 Prefix => New_Copy_Tree (Prefix (N)));
7650 begin
7651 Set_Etype (Obj, Array_Type);
7652 Set_Parent (Obj, Parent (N));
7653 Array_Type := Get_Actual_Subtype (Obj);
7654 end;
7655 end if;
7656
7657 elsif Is_Entity_Name (Name)
7658 or else (Nkind (Name) = N_Function_Call
7659 and then not Is_Constrained (Etype (Name)))
7660 then
7661 Array_Type := Get_Actual_Subtype (Name);
7662
7663 -- If the name is a selected component that depends on discriminants,
7664 -- build an actual subtype for it. This can happen only when the name
7665 -- itself is overloaded; otherwise the actual subtype is created when
7666 -- the selected component is analyzed.
7667
7668 elsif Nkind (Name) = N_Selected_Component
7669 and then Full_Analysis
7670 and then Depends_On_Discriminant (First_Index (Array_Type))
7671 then
7672 declare
7673 Act_Decl : constant Node_Id :=
7674 Build_Actual_Subtype_Of_Component (Array_Type, Name);
7675 begin
7676 Insert_Action (N, Act_Decl);
7677 Array_Type := Defining_Identifier (Act_Decl);
7678 end;
7679 end if;
7680
7681 -- If name was overloaded, set slice type correctly now
7682
7683 Set_Etype (N, Array_Type);
7684
7685 -- If the range is specified by a subtype mark, no resolution is
7686 -- necessary. Else resolve the bounds, and apply needed checks.
7687
7688 if not Is_Entity_Name (Drange) then
7689 Index := First_Index (Array_Type);
7690 Resolve (Drange, Base_Type (Etype (Index)));
7691
7692 if Nkind (Drange) = N_Range
7693
7694 -- Do not apply the range check to nodes associated with the
7695 -- frontend expansion of the dispatch table. We first check
7696 -- if Ada.Tags is already loaded to void the addition of an
7697 -- undesired dependence on such run-time unit.
7698
7699 and then
7700 (VM_Target /= No_VM
7701 or else not
7702 (RTU_Loaded (Ada_Tags)
7703 and then Nkind (Prefix (N)) = N_Selected_Component
7704 and then Present (Entity (Selector_Name (Prefix (N))))
7705 and then Entity (Selector_Name (Prefix (N))) =
7706 RTE_Record_Component (RE_Prims_Ptr)))
7707 then
7708 Apply_Range_Check (Drange, Etype (Index));
7709 end if;
7710 end if;
7711
7712 Set_Slice_Subtype (N);
7713
7714 if Nkind (Drange) = N_Range then
7715 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
7716 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
7717 end if;
7718
7719 Eval_Slice (N);
7720 end Resolve_Slice;
7721
7722 ----------------------------
7723 -- Resolve_String_Literal --
7724 ----------------------------
7725
7726 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
7727 C_Typ : constant Entity_Id := Component_Type (Typ);
7728 R_Typ : constant Entity_Id := Root_Type (C_Typ);
7729 Loc : constant Source_Ptr := Sloc (N);
7730 Str : constant String_Id := Strval (N);
7731 Strlen : constant Nat := String_Length (Str);
7732 Subtype_Id : Entity_Id;
7733 Need_Check : Boolean;
7734
7735 begin
7736 -- For a string appearing in a concatenation, defer creation of the
7737 -- string_literal_subtype until the end of the resolution of the
7738 -- concatenation, because the literal may be constant-folded away. This
7739 -- is a useful optimization for long concatenation expressions.
7740
7741 -- If the string is an aggregate built for a single character (which
7742 -- happens in a non-static context) or a is null string to which special
7743 -- checks may apply, we build the subtype. Wide strings must also get a
7744 -- string subtype if they come from a one character aggregate. Strings
7745 -- generated by attributes might be static, but it is often hard to
7746 -- determine whether the enclosing context is static, so we generate
7747 -- subtypes for them as well, thus losing some rarer optimizations ???
7748 -- Same for strings that come from a static conversion.
7749
7750 Need_Check :=
7751 (Strlen = 0 and then Typ /= Standard_String)
7752 or else Nkind (Parent (N)) /= N_Op_Concat
7753 or else (N /= Left_Opnd (Parent (N))
7754 and then N /= Right_Opnd (Parent (N)))
7755 or else ((Typ = Standard_Wide_String
7756 or else Typ = Standard_Wide_Wide_String)
7757 and then Nkind (Original_Node (N)) /= N_String_Literal);
7758
7759 -- If the resolving type is itself a string literal subtype, we
7760 -- can just reuse it, since there is no point in creating another.
7761
7762 if Ekind (Typ) = E_String_Literal_Subtype then
7763 Subtype_Id := Typ;
7764
7765 elsif Nkind (Parent (N)) = N_Op_Concat
7766 and then not Need_Check
7767 and then not Nkind_In (Original_Node (N), N_Character_Literal,
7768 N_Attribute_Reference,
7769 N_Qualified_Expression,
7770 N_Type_Conversion)
7771 then
7772 Subtype_Id := Typ;
7773
7774 -- Otherwise we must create a string literal subtype. Note that the
7775 -- whole idea of string literal subtypes is simply to avoid the need
7776 -- for building a full fledged array subtype for each literal.
7777
7778 else
7779 Set_String_Literal_Subtype (N, Typ);
7780 Subtype_Id := Etype (N);
7781 end if;
7782
7783 if Nkind (Parent (N)) /= N_Op_Concat
7784 or else Need_Check
7785 then
7786 Set_Etype (N, Subtype_Id);
7787 Eval_String_Literal (N);
7788 end if;
7789
7790 if Is_Limited_Composite (Typ)
7791 or else Is_Private_Composite (Typ)
7792 then
7793 Error_Msg_N ("string literal not available for private array", N);
7794 Set_Etype (N, Any_Type);
7795 return;
7796 end if;
7797
7798 -- The validity of a null string has been checked in the
7799 -- call to Eval_String_Literal.
7800
7801 if Strlen = 0 then
7802 return;
7803
7804 -- Always accept string literal with component type Any_Character, which
7805 -- occurs in error situations and in comparisons of literals, both of
7806 -- which should accept all literals.
7807
7808 elsif R_Typ = Any_Character then
7809 return;
7810
7811 -- If the type is bit-packed, then we always transform the string
7812 -- literal into a full fledged aggregate.
7813
7814 elsif Is_Bit_Packed_Array (Typ) then
7815 null;
7816
7817 -- Deal with cases of Wide_Wide_String, Wide_String, and String
7818
7819 else
7820 -- For Standard.Wide_Wide_String, or any other type whose component
7821 -- type is Standard.Wide_Wide_Character, we know that all the
7822 -- characters in the string must be acceptable, since the parser
7823 -- accepted the characters as valid character literals.
7824
7825 if R_Typ = Standard_Wide_Wide_Character then
7826 null;
7827
7828 -- For the case of Standard.String, or any other type whose component
7829 -- type is Standard.Character, we must make sure that there are no
7830 -- wide characters in the string, i.e. that it is entirely composed
7831 -- of characters in range of type Character.
7832
7833 -- If the string literal is the result of a static concatenation, the
7834 -- test has already been performed on the components, and need not be
7835 -- repeated.
7836
7837 elsif R_Typ = Standard_Character
7838 and then Nkind (Original_Node (N)) /= N_Op_Concat
7839 then
7840 for J in 1 .. Strlen loop
7841 if not In_Character_Range (Get_String_Char (Str, J)) then
7842
7843 -- If we are out of range, post error. This is one of the
7844 -- very few places that we place the flag in the middle of
7845 -- a token, right under the offending wide character.
7846
7847 Error_Msg
7848 ("literal out of range of type Standard.Character",
7849 Source_Ptr (Int (Loc) + J));
7850 return;
7851 end if;
7852 end loop;
7853
7854 -- For the case of Standard.Wide_String, or any other type whose
7855 -- component type is Standard.Wide_Character, we must make sure that
7856 -- there are no wide characters in the string, i.e. that it is
7857 -- entirely composed of characters in range of type Wide_Character.
7858
7859 -- If the string literal is the result of a static concatenation,
7860 -- the test has already been performed on the components, and need
7861 -- not be repeated.
7862
7863 elsif R_Typ = Standard_Wide_Character
7864 and then Nkind (Original_Node (N)) /= N_Op_Concat
7865 then
7866 for J in 1 .. Strlen loop
7867 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
7868
7869 -- If we are out of range, post error. This is one of the
7870 -- very few places that we place the flag in the middle of
7871 -- a token, right under the offending wide character.
7872
7873 -- This is not quite right, because characters in general
7874 -- will take more than one character position ???
7875
7876 Error_Msg
7877 ("literal out of range of type Standard.Wide_Character",
7878 Source_Ptr (Int (Loc) + J));
7879 return;
7880 end if;
7881 end loop;
7882
7883 -- If the root type is not a standard character, then we will convert
7884 -- the string into an aggregate and will let the aggregate code do
7885 -- the checking. Standard Wide_Wide_Character is also OK here.
7886
7887 else
7888 null;
7889 end if;
7890
7891 -- See if the component type of the array corresponding to the string
7892 -- has compile time known bounds. If yes we can directly check
7893 -- whether the evaluation of the string will raise constraint error.
7894 -- Otherwise we need to transform the string literal into the
7895 -- corresponding character aggregate and let the aggregate
7896 -- code do the checking.
7897
7898 if Is_Standard_Character_Type (R_Typ) then
7899
7900 -- Check for the case of full range, where we are definitely OK
7901
7902 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
7903 return;
7904 end if;
7905
7906 -- Here the range is not the complete base type range, so check
7907
7908 declare
7909 Comp_Typ_Lo : constant Node_Id :=
7910 Type_Low_Bound (Component_Type (Typ));
7911 Comp_Typ_Hi : constant Node_Id :=
7912 Type_High_Bound (Component_Type (Typ));
7913
7914 Char_Val : Uint;
7915
7916 begin
7917 if Compile_Time_Known_Value (Comp_Typ_Lo)
7918 and then Compile_Time_Known_Value (Comp_Typ_Hi)
7919 then
7920 for J in 1 .. Strlen loop
7921 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
7922
7923 if Char_Val < Expr_Value (Comp_Typ_Lo)
7924 or else Char_Val > Expr_Value (Comp_Typ_Hi)
7925 then
7926 Apply_Compile_Time_Constraint_Error
7927 (N, "character out of range?", CE_Range_Check_Failed,
7928 Loc => Source_Ptr (Int (Loc) + J));
7929 end if;
7930 end loop;
7931
7932 return;
7933 end if;
7934 end;
7935 end if;
7936 end if;
7937
7938 -- If we got here we meed to transform the string literal into the
7939 -- equivalent qualified positional array aggregate. This is rather
7940 -- heavy artillery for this situation, but it is hard work to avoid.
7941
7942 declare
7943 Lits : constant List_Id := New_List;
7944 P : Source_Ptr := Loc + 1;
7945 C : Char_Code;
7946
7947 begin
7948 -- Build the character literals, we give them source locations that
7949 -- correspond to the string positions, which is a bit tricky given
7950 -- the possible presence of wide character escape sequences.
7951
7952 for J in 1 .. Strlen loop
7953 C := Get_String_Char (Str, J);
7954 Set_Character_Literal_Name (C);
7955
7956 Append_To (Lits,
7957 Make_Character_Literal (P,
7958 Chars => Name_Find,
7959 Char_Literal_Value => UI_From_CC (C)));
7960
7961 if In_Character_Range (C) then
7962 P := P + 1;
7963
7964 -- Should we have a call to Skip_Wide here ???
7965 -- ??? else
7966 -- Skip_Wide (P);
7967
7968 end if;
7969 end loop;
7970
7971 Rewrite (N,
7972 Make_Qualified_Expression (Loc,
7973 Subtype_Mark => New_Reference_To (Typ, Loc),
7974 Expression =>
7975 Make_Aggregate (Loc, Expressions => Lits)));
7976
7977 Analyze_And_Resolve (N, Typ);
7978 end;
7979 end Resolve_String_Literal;
7980
7981 -----------------------------
7982 -- Resolve_Subprogram_Info --
7983 -----------------------------
7984
7985 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
7986 begin
7987 Set_Etype (N, Typ);
7988 end Resolve_Subprogram_Info;
7989
7990 -----------------------------
7991 -- Resolve_Type_Conversion --
7992 -----------------------------
7993
7994 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
7995 Conv_OK : constant Boolean := Conversion_OK (N);
7996 Operand : constant Node_Id := Expression (N);
7997 Operand_Typ : constant Entity_Id := Etype (Operand);
7998 Target_Typ : constant Entity_Id := Etype (N);
7999 Rop : Node_Id;
8000 Orig_N : Node_Id;
8001 Orig_T : Node_Id;
8002
8003 begin
8004 if not Conv_OK
8005 and then not Valid_Conversion (N, Target_Typ, Operand)
8006 then
8007 return;
8008 end if;
8009
8010 if Etype (Operand) = Any_Fixed then
8011
8012 -- Mixed-mode operation involving a literal. Context must be a fixed
8013 -- type which is applied to the literal subsequently.
8014
8015 if Is_Fixed_Point_Type (Typ) then
8016 Set_Etype (Operand, Universal_Real);
8017
8018 elsif Is_Numeric_Type (Typ)
8019 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
8020 and then (Etype (Right_Opnd (Operand)) = Universal_Real
8021 or else
8022 Etype (Left_Opnd (Operand)) = Universal_Real)
8023 then
8024 -- Return if expression is ambiguous
8025
8026 if Unique_Fixed_Point_Type (N) = Any_Type then
8027 return;
8028
8029 -- If nothing else, the available fixed type is Duration
8030
8031 else
8032 Set_Etype (Operand, Standard_Duration);
8033 end if;
8034
8035 -- Resolve the real operand with largest available precision
8036
8037 if Etype (Right_Opnd (Operand)) = Universal_Real then
8038 Rop := New_Copy_Tree (Right_Opnd (Operand));
8039 else
8040 Rop := New_Copy_Tree (Left_Opnd (Operand));
8041 end if;
8042
8043 Resolve (Rop, Universal_Real);
8044
8045 -- If the operand is a literal (it could be a non-static and
8046 -- illegal exponentiation) check whether the use of Duration
8047 -- is potentially inaccurate.
8048
8049 if Nkind (Rop) = N_Real_Literal
8050 and then Realval (Rop) /= Ureal_0
8051 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
8052 then
8053 Error_Msg_N
8054 ("?universal real operand can only " &
8055 "be interpreted as Duration!",
8056 Rop);
8057 Error_Msg_N
8058 ("\?precision will be lost in the conversion!", Rop);
8059 end if;
8060
8061 elsif Is_Numeric_Type (Typ)
8062 and then Nkind (Operand) in N_Op
8063 and then Unique_Fixed_Point_Type (N) /= Any_Type
8064 then
8065 Set_Etype (Operand, Standard_Duration);
8066
8067 else
8068 Error_Msg_N ("invalid context for mixed mode operation", N);
8069 Set_Etype (Operand, Any_Type);
8070 return;
8071 end if;
8072 end if;
8073
8074 Resolve (Operand);
8075
8076 -- Note: we do the Eval_Type_Conversion call before applying the
8077 -- required checks for a subtype conversion. This is important,
8078 -- since both are prepared under certain circumstances to change
8079 -- the type conversion to a constraint error node, but in the case
8080 -- of Eval_Type_Conversion this may reflect an illegality in the
8081 -- static case, and we would miss the illegality (getting only a
8082 -- warning message), if we applied the type conversion checks first.
8083
8084 Eval_Type_Conversion (N);
8085
8086 -- Even when evaluation is not possible, we may be able to simplify
8087 -- the conversion or its expression. This needs to be done before
8088 -- applying checks, since otherwise the checks may use the original
8089 -- expression and defeat the simplifications. This is specifically
8090 -- the case for elimination of the floating-point Truncation
8091 -- attribute in float-to-int conversions.
8092
8093 Simplify_Type_Conversion (N);
8094
8095 -- If after evaluation we still have a type conversion, then we
8096 -- may need to apply checks required for a subtype conversion.
8097
8098 -- Skip these type conversion checks if universal fixed operands
8099 -- operands involved, since range checks are handled separately for
8100 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
8101
8102 if Nkind (N) = N_Type_Conversion
8103 and then not Is_Generic_Type (Root_Type (Target_Typ))
8104 and then Target_Typ /= Universal_Fixed
8105 and then Operand_Typ /= Universal_Fixed
8106 then
8107 Apply_Type_Conversion_Checks (N);
8108 end if;
8109
8110 -- Issue warning for conversion of simple object to its own type
8111 -- We have to test the original nodes, since they may have been
8112 -- rewritten by various optimizations.
8113
8114 Orig_N := Original_Node (N);
8115
8116 if Warn_On_Redundant_Constructs
8117 and then Comes_From_Source (Orig_N)
8118 and then Nkind (Orig_N) = N_Type_Conversion
8119 and then not In_Instance
8120 then
8121 Orig_N := Original_Node (Expression (Orig_N));
8122 Orig_T := Target_Typ;
8123
8124 -- If the node is part of a larger expression, the Target_Type
8125 -- may not be the original type of the node if the context is a
8126 -- condition. Recover original type to see if conversion is needed.
8127
8128 if Is_Boolean_Type (Orig_T)
8129 and then Nkind (Parent (N)) in N_Op
8130 then
8131 Orig_T := Etype (Parent (N));
8132 end if;
8133
8134 if Is_Entity_Name (Orig_N)
8135 and then
8136 (Etype (Entity (Orig_N)) = Orig_T
8137 or else
8138 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
8139 and then Covers (Orig_T, Etype (Entity (Orig_N)))))
8140 then
8141 Error_Msg_Node_2 := Orig_T;
8142 Error_Msg_NE
8143 ("?redundant conversion, & is of type &!", N, Entity (Orig_N));
8144 end if;
8145 end if;
8146
8147 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
8148 -- No need to perform any interface conversion if the type of the
8149 -- expression coincides with the target type.
8150
8151 if Ada_Version >= Ada_05
8152 and then Expander_Active
8153 and then Operand_Typ /= Target_Typ
8154 then
8155 declare
8156 Opnd : Entity_Id := Operand_Typ;
8157 Target : Entity_Id := Target_Typ;
8158
8159 begin
8160 if Is_Access_Type (Opnd) then
8161 Opnd := Directly_Designated_Type (Opnd);
8162 end if;
8163
8164 if Is_Access_Type (Target_Typ) then
8165 Target := Directly_Designated_Type (Target);
8166 end if;
8167
8168 if Opnd = Target then
8169 null;
8170
8171 -- Conversion from interface type
8172
8173 elsif Is_Interface (Opnd) then
8174
8175 -- Ada 2005 (AI-217): Handle entities from limited views
8176
8177 if From_With_Type (Opnd) then
8178 Error_Msg_Qual_Level := 99;
8179 Error_Msg_NE ("missing with-clause on package &", N,
8180 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
8181 Error_Msg_N
8182 ("type conversions require visibility of the full view",
8183 N);
8184
8185 elsif From_With_Type (Target)
8186 and then not
8187 (Is_Access_Type (Target_Typ)
8188 and then Present (Non_Limited_View (Etype (Target))))
8189 then
8190 Error_Msg_Qual_Level := 99;
8191 Error_Msg_NE ("missing with-clause on package &", N,
8192 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
8193 Error_Msg_N
8194 ("type conversions require visibility of the full view",
8195 N);
8196
8197 else
8198 Expand_Interface_Conversion (N, Is_Static => False);
8199 end if;
8200
8201 -- Conversion to interface type
8202
8203 elsif Is_Interface (Target) then
8204
8205 -- Handle subtypes
8206
8207 if Ekind (Opnd) = E_Protected_Subtype
8208 or else Ekind (Opnd) = E_Task_Subtype
8209 then
8210 Opnd := Etype (Opnd);
8211 end if;
8212
8213 if not Interface_Present_In_Ancestor
8214 (Typ => Opnd,
8215 Iface => Target)
8216 then
8217 if Is_Class_Wide_Type (Opnd) then
8218
8219 -- The static analysis is not enough to know if the
8220 -- interface is implemented or not. Hence we must pass
8221 -- the work to the expander to generate code to evaluate
8222 -- the conversion at run-time.
8223
8224 Expand_Interface_Conversion (N, Is_Static => False);
8225
8226 else
8227 Error_Msg_Name_1 := Chars (Etype (Target));
8228 Error_Msg_Name_2 := Chars (Opnd);
8229 Error_Msg_N
8230 ("wrong interface conversion (% is not a progenitor " &
8231 "of %)", N);
8232 end if;
8233
8234 else
8235 Expand_Interface_Conversion (N);
8236 end if;
8237 end if;
8238 end;
8239 end if;
8240 end Resolve_Type_Conversion;
8241
8242 ----------------------
8243 -- Resolve_Unary_Op --
8244 ----------------------
8245
8246 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
8247 B_Typ : constant Entity_Id := Base_Type (Typ);
8248 R : constant Node_Id := Right_Opnd (N);
8249 OK : Boolean;
8250 Lo : Uint;
8251 Hi : Uint;
8252
8253 begin
8254 -- Deal with intrinsic unary operators
8255
8256 if Comes_From_Source (N)
8257 and then Ekind (Entity (N)) = E_Function
8258 and then Is_Imported (Entity (N))
8259 and then Is_Intrinsic_Subprogram (Entity (N))
8260 then
8261 Resolve_Intrinsic_Unary_Operator (N, Typ);
8262 return;
8263 end if;
8264
8265 -- Deal with universal cases
8266
8267 if Etype (R) = Universal_Integer
8268 or else
8269 Etype (R) = Universal_Real
8270 then
8271 Check_For_Visible_Operator (N, B_Typ);
8272 end if;
8273
8274 Set_Etype (N, B_Typ);
8275 Resolve (R, B_Typ);
8276
8277 -- Generate warning for expressions like abs (x mod 2)
8278
8279 if Warn_On_Redundant_Constructs
8280 and then Nkind (N) = N_Op_Abs
8281 then
8282 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
8283
8284 if OK and then Hi >= Lo and then Lo >= 0 then
8285 Error_Msg_N
8286 ("?abs applied to known non-negative value has no effect", N);
8287 end if;
8288 end if;
8289
8290 -- Deal with reference generation
8291
8292 Check_Unset_Reference (R);
8293 Generate_Operator_Reference (N, B_Typ);
8294 Eval_Unary_Op (N);
8295
8296 -- Set overflow checking bit. Much cleverer code needed here eventually
8297 -- and perhaps the Resolve routines should be separated for the various
8298 -- arithmetic operations, since they will need different processing ???
8299
8300 if Nkind (N) in N_Op then
8301 if not Overflow_Checks_Suppressed (Etype (N)) then
8302 Enable_Overflow_Check (N);
8303 end if;
8304 end if;
8305
8306 -- Generate warning for expressions like -5 mod 3 for integers. No
8307 -- need to worry in the floating-point case, since parens do not affect
8308 -- the result so there is no point in giving in a warning.
8309
8310 declare
8311 Norig : constant Node_Id := Original_Node (N);
8312 Rorig : Node_Id;
8313 Val : Uint;
8314 HB : Uint;
8315 LB : Uint;
8316 Lval : Uint;
8317 Opnd : Node_Id;
8318
8319 begin
8320 if Warn_On_Questionable_Missing_Parens
8321 and then Comes_From_Source (Norig)
8322 and then Is_Integer_Type (Typ)
8323 and then Nkind (Norig) = N_Op_Minus
8324 then
8325 Rorig := Original_Node (Right_Opnd (Norig));
8326
8327 -- We are looking for cases where the right operand is not
8328 -- parenthesized, and is a binary operator, multiply, divide, or
8329 -- mod. These are the cases where the grouping can affect results.
8330
8331 if Paren_Count (Rorig) = 0
8332 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
8333 then
8334 -- For mod, we always give the warning, since the value is
8335 -- affected by the parenthesization (e.g. (-5) mod 315 /=
8336 -- (5 mod 315)). But for the other cases, the only concern is
8337 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
8338 -- overflows, but (-2) * 64 does not). So we try to give the
8339 -- message only when overflow is possible.
8340
8341 if Nkind (Rorig) /= N_Op_Mod
8342 and then Compile_Time_Known_Value (R)
8343 then
8344 Val := Expr_Value (R);
8345
8346 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
8347 HB := Expr_Value (Type_High_Bound (Typ));
8348 else
8349 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
8350 end if;
8351
8352 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
8353 LB := Expr_Value (Type_Low_Bound (Typ));
8354 else
8355 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
8356 end if;
8357
8358 -- Note that the test below is deliberately excluding
8359 -- the largest negative number, since that is a potentially
8360 -- troublesome case (e.g. -2 * x, where the result is the
8361 -- largest negative integer has an overflow with 2 * x).
8362
8363 if Val > LB and then Val <= HB then
8364 return;
8365 end if;
8366 end if;
8367
8368 -- For the multiplication case, the only case we have to worry
8369 -- about is when (-a)*b is exactly the largest negative number
8370 -- so that -(a*b) can cause overflow. This can only happen if
8371 -- a is a power of 2, and more generally if any operand is a
8372 -- constant that is not a power of 2, then the parentheses
8373 -- cannot affect whether overflow occurs. We only bother to
8374 -- test the left most operand
8375
8376 -- Loop looking at left operands for one that has known value
8377
8378 Opnd := Rorig;
8379 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
8380 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
8381 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
8382
8383 -- Operand value of 0 or 1 skips warning
8384
8385 if Lval <= 1 then
8386 return;
8387
8388 -- Otherwise check power of 2, if power of 2, warn, if
8389 -- anything else, skip warning.
8390
8391 else
8392 while Lval /= 2 loop
8393 if Lval mod 2 = 1 then
8394 return;
8395 else
8396 Lval := Lval / 2;
8397 end if;
8398 end loop;
8399
8400 exit Opnd_Loop;
8401 end if;
8402 end if;
8403
8404 -- Keep looking at left operands
8405
8406 Opnd := Left_Opnd (Opnd);
8407 end loop Opnd_Loop;
8408
8409 -- For rem or "/" we can only have a problematic situation
8410 -- if the divisor has a value of minus one or one. Otherwise
8411 -- overflow is impossible (divisor > 1) or we have a case of
8412 -- division by zero in any case.
8413
8414 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
8415 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
8416 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
8417 then
8418 return;
8419 end if;
8420
8421 -- If we fall through warning should be issued
8422
8423 Error_Msg_N
8424 ("?unary minus expression should be parenthesized here!", N);
8425 end if;
8426 end if;
8427 end;
8428 end Resolve_Unary_Op;
8429
8430 ----------------------------------
8431 -- Resolve_Unchecked_Expression --
8432 ----------------------------------
8433
8434 procedure Resolve_Unchecked_Expression
8435 (N : Node_Id;
8436 Typ : Entity_Id)
8437 is
8438 begin
8439 Resolve (Expression (N), Typ, Suppress => All_Checks);
8440 Set_Etype (N, Typ);
8441 end Resolve_Unchecked_Expression;
8442
8443 ---------------------------------------
8444 -- Resolve_Unchecked_Type_Conversion --
8445 ---------------------------------------
8446
8447 procedure Resolve_Unchecked_Type_Conversion
8448 (N : Node_Id;
8449 Typ : Entity_Id)
8450 is
8451 pragma Warnings (Off, Typ);
8452
8453 Operand : constant Node_Id := Expression (N);
8454 Opnd_Type : constant Entity_Id := Etype (Operand);
8455
8456 begin
8457 -- Resolve operand using its own type
8458
8459 Resolve (Operand, Opnd_Type);
8460 Eval_Unchecked_Conversion (N);
8461
8462 end Resolve_Unchecked_Type_Conversion;
8463
8464 ------------------------------
8465 -- Rewrite_Operator_As_Call --
8466 ------------------------------
8467
8468 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
8469 Loc : constant Source_Ptr := Sloc (N);
8470 Actuals : constant List_Id := New_List;
8471 New_N : Node_Id;
8472
8473 begin
8474 if Nkind (N) in N_Binary_Op then
8475 Append (Left_Opnd (N), Actuals);
8476 end if;
8477
8478 Append (Right_Opnd (N), Actuals);
8479
8480 New_N :=
8481 Make_Function_Call (Sloc => Loc,
8482 Name => New_Occurrence_Of (Nam, Loc),
8483 Parameter_Associations => Actuals);
8484
8485 Preserve_Comes_From_Source (New_N, N);
8486 Preserve_Comes_From_Source (Name (New_N), N);
8487 Rewrite (N, New_N);
8488 Set_Etype (N, Etype (Nam));
8489 end Rewrite_Operator_As_Call;
8490
8491 ------------------------------
8492 -- Rewrite_Renamed_Operator --
8493 ------------------------------
8494
8495 procedure Rewrite_Renamed_Operator
8496 (N : Node_Id;
8497 Op : Entity_Id;
8498 Typ : Entity_Id)
8499 is
8500 Nam : constant Name_Id := Chars (Op);
8501 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
8502 Op_Node : Node_Id;
8503
8504 begin
8505 -- Rewrite the operator node using the real operator, not its
8506 -- renaming. Exclude user-defined intrinsic operations of the same
8507 -- name, which are treated separately and rewritten as calls.
8508
8509 if Ekind (Op) /= E_Function
8510 or else Chars (N) /= Nam
8511 then
8512 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
8513 Set_Chars (Op_Node, Nam);
8514 Set_Etype (Op_Node, Etype (N));
8515 Set_Entity (Op_Node, Op);
8516 Set_Right_Opnd (Op_Node, Right_Opnd (N));
8517
8518 -- Indicate that both the original entity and its renaming are
8519 -- referenced at this point.
8520
8521 Generate_Reference (Entity (N), N);
8522 Generate_Reference (Op, N);
8523
8524 if Is_Binary then
8525 Set_Left_Opnd (Op_Node, Left_Opnd (N));
8526 end if;
8527
8528 Rewrite (N, Op_Node);
8529
8530 -- If the context type is private, add the appropriate conversions
8531 -- so that the operator is applied to the full view. This is done
8532 -- in the routines that resolve intrinsic operators,
8533
8534 if Is_Intrinsic_Subprogram (Op)
8535 and then Is_Private_Type (Typ)
8536 then
8537 case Nkind (N) is
8538 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
8539 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
8540 Resolve_Intrinsic_Operator (N, Typ);
8541
8542 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
8543 Resolve_Intrinsic_Unary_Operator (N, Typ);
8544
8545 when others =>
8546 Resolve (N, Typ);
8547 end case;
8548 end if;
8549
8550 elsif Ekind (Op) = E_Function
8551 and then Is_Intrinsic_Subprogram (Op)
8552 then
8553 -- Operator renames a user-defined operator of the same name. Use
8554 -- the original operator in the node, which is the one that Gigi
8555 -- knows about.
8556
8557 Set_Entity (N, Op);
8558 Set_Is_Overloaded (N, False);
8559 end if;
8560 end Rewrite_Renamed_Operator;
8561
8562 -----------------------
8563 -- Set_Slice_Subtype --
8564 -----------------------
8565
8566 -- Build an implicit subtype declaration to represent the type delivered
8567 -- by the slice. This is an abbreviated version of an array subtype. We
8568 -- define an index subtype for the slice, using either the subtype name
8569 -- or the discrete range of the slice. To be consistent with index usage
8570 -- elsewhere, we create a list header to hold the single index. This list
8571 -- is not otherwise attached to the syntax tree.
8572
8573 procedure Set_Slice_Subtype (N : Node_Id) is
8574 Loc : constant Source_Ptr := Sloc (N);
8575 Index_List : constant List_Id := New_List;
8576 Index : Node_Id;
8577 Index_Subtype : Entity_Id;
8578 Index_Type : Entity_Id;
8579 Slice_Subtype : Entity_Id;
8580 Drange : constant Node_Id := Discrete_Range (N);
8581
8582 begin
8583 if Is_Entity_Name (Drange) then
8584 Index_Subtype := Entity (Drange);
8585
8586 else
8587 -- We force the evaluation of a range. This is definitely needed in
8588 -- the renamed case, and seems safer to do unconditionally. Note in
8589 -- any case that since we will create and insert an Itype referring
8590 -- to this range, we must make sure any side effect removal actions
8591 -- are inserted before the Itype definition.
8592
8593 if Nkind (Drange) = N_Range then
8594 Force_Evaluation (Low_Bound (Drange));
8595 Force_Evaluation (High_Bound (Drange));
8596 end if;
8597
8598 Index_Type := Base_Type (Etype (Drange));
8599
8600 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8601
8602 Set_Scalar_Range (Index_Subtype, Drange);
8603 Set_Etype (Index_Subtype, Index_Type);
8604 Set_Size_Info (Index_Subtype, Index_Type);
8605 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
8606 end if;
8607
8608 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
8609
8610 Index := New_Occurrence_Of (Index_Subtype, Loc);
8611 Set_Etype (Index, Index_Subtype);
8612 Append (Index, Index_List);
8613
8614 Set_First_Index (Slice_Subtype, Index);
8615 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
8616 Set_Is_Constrained (Slice_Subtype, True);
8617
8618 Check_Compile_Time_Size (Slice_Subtype);
8619
8620 -- The Etype of the existing Slice node is reset to this slice subtype.
8621 -- Its bounds are obtained from its first index.
8622
8623 Set_Etype (N, Slice_Subtype);
8624
8625 -- In the packed case, this must be immediately frozen
8626
8627 -- Couldn't we always freeze here??? and if we did, then the above
8628 -- call to Check_Compile_Time_Size could be eliminated, which would
8629 -- be nice, because then that routine could be made private to Freeze.
8630
8631 -- Why the test for In_Spec_Expression here ???
8632
8633 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
8634 Freeze_Itype (Slice_Subtype, N);
8635 end if;
8636
8637 end Set_Slice_Subtype;
8638
8639 --------------------------------
8640 -- Set_String_Literal_Subtype --
8641 --------------------------------
8642
8643 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
8644 Loc : constant Source_Ptr := Sloc (N);
8645 Low_Bound : constant Node_Id :=
8646 Type_Low_Bound (Etype (First_Index (Typ)));
8647 Subtype_Id : Entity_Id;
8648
8649 begin
8650 if Nkind (N) /= N_String_Literal then
8651 return;
8652 end if;
8653
8654 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
8655 Set_String_Literal_Length (Subtype_Id, UI_From_Int
8656 (String_Length (Strval (N))));
8657 Set_Etype (Subtype_Id, Base_Type (Typ));
8658 Set_Is_Constrained (Subtype_Id);
8659 Set_Etype (N, Subtype_Id);
8660
8661 if Is_OK_Static_Expression (Low_Bound) then
8662
8663 -- The low bound is set from the low bound of the corresponding
8664 -- index type. Note that we do not store the high bound in the
8665 -- string literal subtype, but it can be deduced if necessary
8666 -- from the length and the low bound.
8667
8668 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
8669
8670 else
8671 Set_String_Literal_Low_Bound
8672 (Subtype_Id, Make_Integer_Literal (Loc, 1));
8673 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
8674
8675 -- Build bona fide subtype for the string, and wrap it in an
8676 -- unchecked conversion, because the backend expects the
8677 -- String_Literal_Subtype to have a static lower bound.
8678
8679 declare
8680 Index_List : constant List_Id := New_List;
8681 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
8682 High_Bound : constant Node_Id :=
8683 Make_Op_Add (Loc,
8684 Left_Opnd => New_Copy_Tree (Low_Bound),
8685 Right_Opnd =>
8686 Make_Integer_Literal (Loc,
8687 String_Length (Strval (N)) - 1));
8688 Array_Subtype : Entity_Id;
8689 Index_Subtype : Entity_Id;
8690 Drange : Node_Id;
8691 Index : Node_Id;
8692
8693 begin
8694 Index_Subtype :=
8695 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8696 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
8697 Set_Scalar_Range (Index_Subtype, Drange);
8698 Set_Parent (Drange, N);
8699 Analyze_And_Resolve (Drange, Index_Type);
8700
8701 -- In the context, the Index_Type may already have a constraint,
8702 -- so use common base type on string subtype. The base type may
8703 -- be used when generating attributes of the string, for example
8704 -- in the context of a slice assignment.
8705
8706 Set_Etype (Index_Subtype, Base_Type (Index_Type));
8707 Set_Size_Info (Index_Subtype, Index_Type);
8708 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
8709
8710 Array_Subtype := Create_Itype (E_Array_Subtype, N);
8711
8712 Index := New_Occurrence_Of (Index_Subtype, Loc);
8713 Set_Etype (Index, Index_Subtype);
8714 Append (Index, Index_List);
8715
8716 Set_First_Index (Array_Subtype, Index);
8717 Set_Etype (Array_Subtype, Base_Type (Typ));
8718 Set_Is_Constrained (Array_Subtype, True);
8719
8720 Rewrite (N,
8721 Make_Unchecked_Type_Conversion (Loc,
8722 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
8723 Expression => Relocate_Node (N)));
8724 Set_Etype (N, Array_Subtype);
8725 end;
8726 end if;
8727 end Set_String_Literal_Subtype;
8728
8729 ------------------------------
8730 -- Simplify_Type_Conversion --
8731 ------------------------------
8732
8733 procedure Simplify_Type_Conversion (N : Node_Id) is
8734 begin
8735 if Nkind (N) = N_Type_Conversion then
8736 declare
8737 Operand : constant Node_Id := Expression (N);
8738 Target_Typ : constant Entity_Id := Etype (N);
8739 Opnd_Typ : constant Entity_Id := Etype (Operand);
8740
8741 begin
8742 if Is_Floating_Point_Type (Opnd_Typ)
8743 and then
8744 (Is_Integer_Type (Target_Typ)
8745 or else (Is_Fixed_Point_Type (Target_Typ)
8746 and then Conversion_OK (N)))
8747 and then Nkind (Operand) = N_Attribute_Reference
8748 and then Attribute_Name (Operand) = Name_Truncation
8749
8750 -- Special processing required if the conversion is the expression
8751 -- of a Truncation attribute reference. In this case we replace:
8752
8753 -- ityp (ftyp'Truncation (x))
8754
8755 -- by
8756
8757 -- ityp (x)
8758
8759 -- with the Float_Truncate flag set, which is more efficient
8760
8761 then
8762 Rewrite (Operand,
8763 Relocate_Node (First (Expressions (Operand))));
8764 Set_Float_Truncate (N, True);
8765 end if;
8766 end;
8767 end if;
8768 end Simplify_Type_Conversion;
8769
8770 -----------------------------
8771 -- Unique_Fixed_Point_Type --
8772 -----------------------------
8773
8774 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
8775 T1 : Entity_Id := Empty;
8776 T2 : Entity_Id;
8777 Item : Node_Id;
8778 Scop : Entity_Id;
8779
8780 procedure Fixed_Point_Error;
8781 -- If true ambiguity, give details
8782
8783 -----------------------
8784 -- Fixed_Point_Error --
8785 -----------------------
8786
8787 procedure Fixed_Point_Error is
8788 begin
8789 Error_Msg_N ("ambiguous universal_fixed_expression", N);
8790 Error_Msg_NE ("\\possible interpretation as}", N, T1);
8791 Error_Msg_NE ("\\possible interpretation as}", N, T2);
8792 end Fixed_Point_Error;
8793
8794 -- Start of processing for Unique_Fixed_Point_Type
8795
8796 begin
8797 -- The operations on Duration are visible, so Duration is always a
8798 -- possible interpretation.
8799
8800 T1 := Standard_Duration;
8801
8802 -- Look for fixed-point types in enclosing scopes
8803
8804 Scop := Current_Scope;
8805 while Scop /= Standard_Standard loop
8806 T2 := First_Entity (Scop);
8807 while Present (T2) loop
8808 if Is_Fixed_Point_Type (T2)
8809 and then Current_Entity (T2) = T2
8810 and then Scope (Base_Type (T2)) = Scop
8811 then
8812 if Present (T1) then
8813 Fixed_Point_Error;
8814 return Any_Type;
8815 else
8816 T1 := T2;
8817 end if;
8818 end if;
8819
8820 Next_Entity (T2);
8821 end loop;
8822
8823 Scop := Scope (Scop);
8824 end loop;
8825
8826 -- Look for visible fixed type declarations in the context
8827
8828 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
8829 while Present (Item) loop
8830 if Nkind (Item) = N_With_Clause then
8831 Scop := Entity (Name (Item));
8832 T2 := First_Entity (Scop);
8833 while Present (T2) loop
8834 if Is_Fixed_Point_Type (T2)
8835 and then Scope (Base_Type (T2)) = Scop
8836 and then (Is_Potentially_Use_Visible (T2)
8837 or else In_Use (T2))
8838 then
8839 if Present (T1) then
8840 Fixed_Point_Error;
8841 return Any_Type;
8842 else
8843 T1 := T2;
8844 end if;
8845 end if;
8846
8847 Next_Entity (T2);
8848 end loop;
8849 end if;
8850
8851 Next (Item);
8852 end loop;
8853
8854 if Nkind (N) = N_Real_Literal then
8855 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
8856 else
8857 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
8858 end if;
8859
8860 return T1;
8861 end Unique_Fixed_Point_Type;
8862
8863 ----------------------
8864 -- Valid_Conversion --
8865 ----------------------
8866
8867 function Valid_Conversion
8868 (N : Node_Id;
8869 Target : Entity_Id;
8870 Operand : Node_Id) return Boolean
8871 is
8872 Target_Type : constant Entity_Id := Base_Type (Target);
8873 Opnd_Type : Entity_Id := Etype (Operand);
8874
8875 function Conversion_Check
8876 (Valid : Boolean;
8877 Msg : String) return Boolean;
8878 -- Little routine to post Msg if Valid is False, returns Valid value
8879
8880 function Valid_Tagged_Conversion
8881 (Target_Type : Entity_Id;
8882 Opnd_Type : Entity_Id) return Boolean;
8883 -- Specifically test for validity of tagged conversions
8884
8885 function Valid_Array_Conversion return Boolean;
8886 -- Check index and component conformance, and accessibility levels
8887 -- if the component types are anonymous access types (Ada 2005)
8888
8889 ----------------------
8890 -- Conversion_Check --
8891 ----------------------
8892
8893 function Conversion_Check
8894 (Valid : Boolean;
8895 Msg : String) return Boolean
8896 is
8897 begin
8898 if not Valid then
8899 Error_Msg_N (Msg, Operand);
8900 end if;
8901
8902 return Valid;
8903 end Conversion_Check;
8904
8905 ----------------------------
8906 -- Valid_Array_Conversion --
8907 ----------------------------
8908
8909 function Valid_Array_Conversion return Boolean
8910 is
8911 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
8912 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
8913
8914 Opnd_Index : Node_Id;
8915 Opnd_Index_Type : Entity_Id;
8916
8917 Target_Comp_Type : constant Entity_Id :=
8918 Component_Type (Target_Type);
8919 Target_Comp_Base : constant Entity_Id :=
8920 Base_Type (Target_Comp_Type);
8921
8922 Target_Index : Node_Id;
8923 Target_Index_Type : Entity_Id;
8924
8925 begin
8926 -- Error if wrong number of dimensions
8927
8928 if
8929 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
8930 then
8931 Error_Msg_N
8932 ("incompatible number of dimensions for conversion", Operand);
8933 return False;
8934
8935 -- Number of dimensions matches
8936
8937 else
8938 -- Loop through indexes of the two arrays
8939
8940 Target_Index := First_Index (Target_Type);
8941 Opnd_Index := First_Index (Opnd_Type);
8942 while Present (Target_Index) and then Present (Opnd_Index) loop
8943 Target_Index_Type := Etype (Target_Index);
8944 Opnd_Index_Type := Etype (Opnd_Index);
8945
8946 -- Error if index types are incompatible
8947
8948 if not (Is_Integer_Type (Target_Index_Type)
8949 and then Is_Integer_Type (Opnd_Index_Type))
8950 and then (Root_Type (Target_Index_Type)
8951 /= Root_Type (Opnd_Index_Type))
8952 then
8953 Error_Msg_N
8954 ("incompatible index types for array conversion",
8955 Operand);
8956 return False;
8957 end if;
8958
8959 Next_Index (Target_Index);
8960 Next_Index (Opnd_Index);
8961 end loop;
8962
8963 -- If component types have same base type, all set
8964
8965 if Target_Comp_Base = Opnd_Comp_Base then
8966 null;
8967
8968 -- Here if base types of components are not the same. The only
8969 -- time this is allowed is if we have anonymous access types.
8970
8971 -- The conversion of arrays of anonymous access types can lead
8972 -- to dangling pointers. AI-392 formalizes the accessibility
8973 -- checks that must be applied to such conversions to prevent
8974 -- out-of-scope references.
8975
8976 elsif
8977 (Ekind (Target_Comp_Base) = E_Anonymous_Access_Type
8978 or else
8979 Ekind (Target_Comp_Base) = E_Anonymous_Access_Subprogram_Type)
8980 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
8981 and then
8982 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
8983 then
8984 if Type_Access_Level (Target_Type) <
8985 Type_Access_Level (Opnd_Type)
8986 then
8987 if In_Instance_Body then
8988 Error_Msg_N ("?source array type " &
8989 "has deeper accessibility level than target", Operand);
8990 Error_Msg_N ("\?Program_Error will be raised at run time",
8991 Operand);
8992 Rewrite (N,
8993 Make_Raise_Program_Error (Sloc (N),
8994 Reason => PE_Accessibility_Check_Failed));
8995 Set_Etype (N, Target_Type);
8996 return False;
8997
8998 -- Conversion not allowed because of accessibility levels
8999
9000 else
9001 Error_Msg_N ("source array type " &
9002 "has deeper accessibility level than target", Operand);
9003 return False;
9004 end if;
9005 else
9006 null;
9007 end if;
9008
9009 -- All other cases where component base types do not match
9010
9011 else
9012 Error_Msg_N
9013 ("incompatible component types for array conversion",
9014 Operand);
9015 return False;
9016 end if;
9017
9018 -- Check that component subtypes statically match. For numeric
9019 -- types this means that both must be either constrained or
9020 -- unconstrained. For enumeration types the bounds must match.
9021 -- All of this is checked in Subtypes_Statically_Match.
9022
9023 if not Subtypes_Statically_Match
9024 (Target_Comp_Type, Opnd_Comp_Type)
9025 then
9026 Error_Msg_N
9027 ("component subtypes must statically match", Operand);
9028 return False;
9029 end if;
9030 end if;
9031
9032 return True;
9033 end Valid_Array_Conversion;
9034
9035 -----------------------------
9036 -- Valid_Tagged_Conversion --
9037 -----------------------------
9038
9039 function Valid_Tagged_Conversion
9040 (Target_Type : Entity_Id;
9041 Opnd_Type : Entity_Id) return Boolean
9042 is
9043 begin
9044 -- Upward conversions are allowed (RM 4.6(22))
9045
9046 if Covers (Target_Type, Opnd_Type)
9047 or else Is_Ancestor (Target_Type, Opnd_Type)
9048 then
9049 return True;
9050
9051 -- Downward conversion are allowed if the operand is class-wide
9052 -- (RM 4.6(23)).
9053
9054 elsif Is_Class_Wide_Type (Opnd_Type)
9055 and then Covers (Opnd_Type, Target_Type)
9056 then
9057 return True;
9058
9059 elsif Covers (Opnd_Type, Target_Type)
9060 or else Is_Ancestor (Opnd_Type, Target_Type)
9061 then
9062 return
9063 Conversion_Check (False,
9064 "downward conversion of tagged objects not allowed");
9065
9066 -- Ada 2005 (AI-251): The conversion to/from interface types is
9067 -- always valid
9068
9069 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
9070 return True;
9071
9072 -- If the operand is a class-wide type obtained through a limited_
9073 -- with clause, and the context includes the non-limited view, use
9074 -- it to determine whether the conversion is legal.
9075
9076 elsif Is_Class_Wide_Type (Opnd_Type)
9077 and then From_With_Type (Opnd_Type)
9078 and then Present (Non_Limited_View (Etype (Opnd_Type)))
9079 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
9080 then
9081 return True;
9082
9083 elsif Is_Access_Type (Opnd_Type)
9084 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
9085 then
9086 return True;
9087
9088 else
9089 Error_Msg_NE
9090 ("invalid tagged conversion, not compatible with}",
9091 N, First_Subtype (Opnd_Type));
9092 return False;
9093 end if;
9094 end Valid_Tagged_Conversion;
9095
9096 -- Start of processing for Valid_Conversion
9097
9098 begin
9099 Check_Parameterless_Call (Operand);
9100
9101 if Is_Overloaded (Operand) then
9102 declare
9103 I : Interp_Index;
9104 I1 : Interp_Index;
9105 It : Interp;
9106 It1 : Interp;
9107 N1 : Entity_Id;
9108
9109 begin
9110 -- Remove procedure calls, which syntactically cannot appear
9111 -- in this context, but which cannot be removed by type checking,
9112 -- because the context does not impose a type.
9113
9114 -- When compiling for VMS, spurious ambiguities can be produced
9115 -- when arithmetic operations have a literal operand and return
9116 -- System.Address or a descendant of it. These ambiguities are
9117 -- otherwise resolved by the context, but for conversions there
9118 -- is no context type and the removal of the spurious operations
9119 -- must be done explicitly here.
9120
9121 -- The node may be labelled overloaded, but still contain only
9122 -- one interpretation because others were discarded in previous
9123 -- filters. If this is the case, retain the single interpretation
9124 -- if legal.
9125
9126 Get_First_Interp (Operand, I, It);
9127 Opnd_Type := It.Typ;
9128 Get_Next_Interp (I, It);
9129
9130 if Present (It.Typ)
9131 and then Opnd_Type /= Standard_Void_Type
9132 then
9133 -- More than one candidate interpretation is available
9134
9135 Get_First_Interp (Operand, I, It);
9136 while Present (It.Typ) loop
9137 if It.Typ = Standard_Void_Type then
9138 Remove_Interp (I);
9139 end if;
9140
9141 if Present (System_Aux_Id)
9142 and then Is_Descendent_Of_Address (It.Typ)
9143 then
9144 Remove_Interp (I);
9145 end if;
9146
9147 Get_Next_Interp (I, It);
9148 end loop;
9149 end if;
9150
9151 Get_First_Interp (Operand, I, It);
9152 I1 := I;
9153 It1 := It;
9154
9155 if No (It.Typ) then
9156 Error_Msg_N ("illegal operand in conversion", Operand);
9157 return False;
9158 end if;
9159
9160 Get_Next_Interp (I, It);
9161
9162 if Present (It.Typ) then
9163 N1 := It1.Nam;
9164 It1 := Disambiguate (Operand, I1, I, Any_Type);
9165
9166 if It1 = No_Interp then
9167 Error_Msg_N ("ambiguous operand in conversion", Operand);
9168
9169 Error_Msg_Sloc := Sloc (It.Nam);
9170 Error_Msg_N ("\\possible interpretation#!", Operand);
9171
9172 Error_Msg_Sloc := Sloc (N1);
9173 Error_Msg_N ("\\possible interpretation#!", Operand);
9174
9175 return False;
9176 end if;
9177 end if;
9178
9179 Set_Etype (Operand, It1.Typ);
9180 Opnd_Type := It1.Typ;
9181 end;
9182 end if;
9183
9184 -- Numeric types
9185
9186 if Is_Numeric_Type (Target_Type) then
9187
9188 -- A universal fixed expression can be converted to any numeric type
9189
9190 if Opnd_Type = Universal_Fixed then
9191 return True;
9192
9193 -- Also no need to check when in an instance or inlined body, because
9194 -- the legality has been established when the template was analyzed.
9195 -- Furthermore, numeric conversions may occur where only a private
9196 -- view of the operand type is visible at the instantiation point.
9197 -- This results in a spurious error if we check that the operand type
9198 -- is a numeric type.
9199
9200 -- Note: in a previous version of this unit, the following tests were
9201 -- applied only for generated code (Comes_From_Source set to False),
9202 -- but in fact the test is required for source code as well, since
9203 -- this situation can arise in source code.
9204
9205 elsif In_Instance or else In_Inlined_Body then
9206 return True;
9207
9208 -- Otherwise we need the conversion check
9209
9210 else
9211 return Conversion_Check
9212 (Is_Numeric_Type (Opnd_Type),
9213 "illegal operand for numeric conversion");
9214 end if;
9215
9216 -- Array types
9217
9218 elsif Is_Array_Type (Target_Type) then
9219 if not Is_Array_Type (Opnd_Type)
9220 or else Opnd_Type = Any_Composite
9221 or else Opnd_Type = Any_String
9222 then
9223 Error_Msg_N
9224 ("illegal operand for array conversion", Operand);
9225 return False;
9226 else
9227 return Valid_Array_Conversion;
9228 end if;
9229
9230 -- Ada 2005 (AI-251): Anonymous access types where target references an
9231 -- interface type.
9232
9233 elsif (Ekind (Target_Type) = E_General_Access_Type
9234 or else
9235 Ekind (Target_Type) = E_Anonymous_Access_Type)
9236 and then Is_Interface (Directly_Designated_Type (Target_Type))
9237 then
9238 -- Check the static accessibility rule of 4.6(17). Note that the
9239 -- check is not enforced when within an instance body, since the RM
9240 -- requires such cases to be caught at run time.
9241
9242 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
9243 if Type_Access_Level (Opnd_Type) >
9244 Type_Access_Level (Target_Type)
9245 then
9246 -- In an instance, this is a run-time check, but one we know
9247 -- will fail, so generate an appropriate warning. The raise
9248 -- will be generated by Expand_N_Type_Conversion.
9249
9250 if In_Instance_Body then
9251 Error_Msg_N
9252 ("?cannot convert local pointer to non-local access type",
9253 Operand);
9254 Error_Msg_N
9255 ("\?Program_Error will be raised at run time", Operand);
9256 else
9257 Error_Msg_N
9258 ("cannot convert local pointer to non-local access type",
9259 Operand);
9260 return False;
9261 end if;
9262
9263 -- Special accessibility checks are needed in the case of access
9264 -- discriminants declared for a limited type.
9265
9266 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
9267 and then not Is_Local_Anonymous_Access (Opnd_Type)
9268 then
9269 -- When the operand is a selected access discriminant the check
9270 -- needs to be made against the level of the object denoted by
9271 -- the prefix of the selected name. (Object_Access_Level
9272 -- handles checking the prefix of the operand for this case.)
9273
9274 if Nkind (Operand) = N_Selected_Component
9275 and then Object_Access_Level (Operand) >
9276 Type_Access_Level (Target_Type)
9277 then
9278 -- In an instance, this is a run-time check, but one we
9279 -- know will fail, so generate an appropriate warning.
9280 -- The raise will be generated by Expand_N_Type_Conversion.
9281
9282 if In_Instance_Body then
9283 Error_Msg_N
9284 ("?cannot convert access discriminant to non-local" &
9285 " access type", Operand);
9286 Error_Msg_N
9287 ("\?Program_Error will be raised at run time", Operand);
9288 else
9289 Error_Msg_N
9290 ("cannot convert access discriminant to non-local" &
9291 " access type", Operand);
9292 return False;
9293 end if;
9294 end if;
9295
9296 -- The case of a reference to an access discriminant from
9297 -- within a limited type declaration (which will appear as
9298 -- a discriminal) is always illegal because the level of the
9299 -- discriminant is considered to be deeper than any (nameable)
9300 -- access type.
9301
9302 if Is_Entity_Name (Operand)
9303 and then not Is_Local_Anonymous_Access (Opnd_Type)
9304 and then (Ekind (Entity (Operand)) = E_In_Parameter
9305 or else Ekind (Entity (Operand)) = E_Constant)
9306 and then Present (Discriminal_Link (Entity (Operand)))
9307 then
9308 Error_Msg_N
9309 ("discriminant has deeper accessibility level than target",
9310 Operand);
9311 return False;
9312 end if;
9313 end if;
9314 end if;
9315
9316 return True;
9317
9318 -- General and anonymous access types
9319
9320 elsif (Ekind (Target_Type) = E_General_Access_Type
9321 or else Ekind (Target_Type) = E_Anonymous_Access_Type)
9322 and then
9323 Conversion_Check
9324 (Is_Access_Type (Opnd_Type)
9325 and then Ekind (Opnd_Type) /=
9326 E_Access_Subprogram_Type
9327 and then Ekind (Opnd_Type) /=
9328 E_Access_Protected_Subprogram_Type,
9329 "must be an access-to-object type")
9330 then
9331 if Is_Access_Constant (Opnd_Type)
9332 and then not Is_Access_Constant (Target_Type)
9333 then
9334 Error_Msg_N
9335 ("access-to-constant operand type not allowed", Operand);
9336 return False;
9337 end if;
9338
9339 -- Check the static accessibility rule of 4.6(17). Note that the
9340 -- check is not enforced when within an instance body, since the RM
9341 -- requires such cases to be caught at run time.
9342
9343 if Ekind (Target_Type) /= E_Anonymous_Access_Type
9344 or else Is_Local_Anonymous_Access (Target_Type)
9345 then
9346 if Type_Access_Level (Opnd_Type)
9347 > Type_Access_Level (Target_Type)
9348 then
9349 -- In an instance, this is a run-time check, but one we
9350 -- know will fail, so generate an appropriate warning.
9351 -- The raise will be generated by Expand_N_Type_Conversion.
9352
9353 if In_Instance_Body then
9354 Error_Msg_N
9355 ("?cannot convert local pointer to non-local access type",
9356 Operand);
9357 Error_Msg_N
9358 ("\?Program_Error will be raised at run time", Operand);
9359
9360 else
9361 -- Avoid generation of spurious error message
9362
9363 if not Error_Posted (N) then
9364 Error_Msg_N
9365 ("cannot convert local pointer to non-local access type",
9366 Operand);
9367 end if;
9368
9369 return False;
9370 end if;
9371
9372 -- Special accessibility checks are needed in the case of access
9373 -- discriminants declared for a limited type.
9374
9375 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
9376 and then not Is_Local_Anonymous_Access (Opnd_Type)
9377 then
9378
9379 -- When the operand is a selected access discriminant the check
9380 -- needs to be made against the level of the object denoted by
9381 -- the prefix of the selected name. (Object_Access_Level
9382 -- handles checking the prefix of the operand for this case.)
9383
9384 if Nkind (Operand) = N_Selected_Component
9385 and then Object_Access_Level (Operand) >
9386 Type_Access_Level (Target_Type)
9387 then
9388 -- In an instance, this is a run-time check, but one we
9389 -- know will fail, so generate an appropriate warning.
9390 -- The raise will be generated by Expand_N_Type_Conversion.
9391
9392 if In_Instance_Body then
9393 Error_Msg_N
9394 ("?cannot convert access discriminant to non-local" &
9395 " access type", Operand);
9396 Error_Msg_N
9397 ("\?Program_Error will be raised at run time",
9398 Operand);
9399
9400 else
9401 Error_Msg_N
9402 ("cannot convert access discriminant to non-local" &
9403 " access type", Operand);
9404 return False;
9405 end if;
9406 end if;
9407
9408 -- The case of a reference to an access discriminant from
9409 -- within a limited type declaration (which will appear as
9410 -- a discriminal) is always illegal because the level of the
9411 -- discriminant is considered to be deeper than any (nameable)
9412 -- access type.
9413
9414 if Is_Entity_Name (Operand)
9415 and then (Ekind (Entity (Operand)) = E_In_Parameter
9416 or else Ekind (Entity (Operand)) = E_Constant)
9417 and then Present (Discriminal_Link (Entity (Operand)))
9418 then
9419 Error_Msg_N
9420 ("discriminant has deeper accessibility level than target",
9421 Operand);
9422 return False;
9423 end if;
9424 end if;
9425 end if;
9426
9427 declare
9428 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
9429 -- Helper function to handle limited views
9430
9431 --------------------------
9432 -- Full_Designated_Type --
9433 --------------------------
9434
9435 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
9436 Desig : constant Entity_Id := Designated_Type (T);
9437 begin
9438 if From_With_Type (Desig)
9439 and then Is_Incomplete_Type (Desig)
9440 and then Present (Non_Limited_View (Desig))
9441 then
9442 return Non_Limited_View (Desig);
9443 else
9444 return Desig;
9445 end if;
9446 end Full_Designated_Type;
9447
9448 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
9449 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
9450
9451 Same_Base : constant Boolean :=
9452 Base_Type (Target) = Base_Type (Opnd);
9453
9454 begin
9455 if Is_Tagged_Type (Target) then
9456 return Valid_Tagged_Conversion (Target, Opnd);
9457
9458 else
9459 if not Same_Base then
9460 Error_Msg_NE
9461 ("target designated type not compatible with }",
9462 N, Base_Type (Opnd));
9463 return False;
9464
9465 -- Ada 2005 AI-384: legality rule is symmetric in both
9466 -- designated types. The conversion is legal (with possible
9467 -- constraint check) if either designated type is
9468 -- unconstrained.
9469
9470 elsif Subtypes_Statically_Match (Target, Opnd)
9471 or else
9472 (Has_Discriminants (Target)
9473 and then
9474 (not Is_Constrained (Opnd)
9475 or else not Is_Constrained (Target)))
9476 then
9477 return True;
9478
9479 else
9480 Error_Msg_NE
9481 ("target designated subtype not compatible with }",
9482 N, Opnd);
9483 return False;
9484 end if;
9485 end if;
9486 end;
9487
9488 -- Access to subprogram types. If the operand is an access parameter,
9489 -- the type has a deeper accessibility that any master, and cannot
9490 -- be assigned.
9491
9492 elsif (Ekind (Target_Type) = E_Access_Subprogram_Type
9493 or else
9494 Ekind (Target_Type) = E_Anonymous_Access_Subprogram_Type)
9495 and then No (Corresponding_Remote_Type (Opnd_Type))
9496 then
9497 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
9498 and then Is_Entity_Name (Operand)
9499 and then Ekind (Entity (Operand)) = E_In_Parameter
9500 then
9501 Error_Msg_N
9502 ("illegal attempt to store anonymous access to subprogram",
9503 Operand);
9504 Error_Msg_N
9505 ("\value has deeper accessibility than any master " &
9506 "(RM 3.10.2 (13))",
9507 Operand);
9508
9509 Error_Msg_NE
9510 ("\use named access type for& instead of access parameter",
9511 Operand, Entity (Operand));
9512 end if;
9513
9514 -- Check that the designated types are subtype conformant
9515
9516 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
9517 Old_Id => Designated_Type (Opnd_Type),
9518 Err_Loc => N);
9519
9520 -- Check the static accessibility rule of 4.6(20)
9521
9522 if Type_Access_Level (Opnd_Type) >
9523 Type_Access_Level (Target_Type)
9524 then
9525 Error_Msg_N
9526 ("operand type has deeper accessibility level than target",
9527 Operand);
9528
9529 -- Check that if the operand type is declared in a generic body,
9530 -- then the target type must be declared within that same body
9531 -- (enforces last sentence of 4.6(20)).
9532
9533 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
9534 declare
9535 O_Gen : constant Node_Id :=
9536 Enclosing_Generic_Body (Opnd_Type);
9537
9538 T_Gen : Node_Id;
9539
9540 begin
9541 T_Gen := Enclosing_Generic_Body (Target_Type);
9542 while Present (T_Gen) and then T_Gen /= O_Gen loop
9543 T_Gen := Enclosing_Generic_Body (T_Gen);
9544 end loop;
9545
9546 if T_Gen /= O_Gen then
9547 Error_Msg_N
9548 ("target type must be declared in same generic body"
9549 & " as operand type", N);
9550 end if;
9551 end;
9552 end if;
9553
9554 return True;
9555
9556 -- Remote subprogram access types
9557
9558 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
9559 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
9560 then
9561 -- It is valid to convert from one RAS type to another provided
9562 -- that their specification statically match.
9563
9564 Check_Subtype_Conformant
9565 (New_Id =>
9566 Designated_Type (Corresponding_Remote_Type (Target_Type)),
9567 Old_Id =>
9568 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
9569 Err_Loc =>
9570 N);
9571 return True;
9572
9573 -- If both are tagged types, check legality of view conversions
9574
9575 elsif Is_Tagged_Type (Target_Type)
9576 and then Is_Tagged_Type (Opnd_Type)
9577 then
9578 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
9579
9580 -- Types derived from the same root type are convertible
9581
9582 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
9583 return True;
9584
9585 -- In an instance or an inlined body, there may be inconsistent
9586 -- views of the same type, or of types derived from a common root.
9587
9588 elsif (In_Instance or In_Inlined_Body)
9589 and then
9590 Root_Type (Underlying_Type (Target_Type)) =
9591 Root_Type (Underlying_Type (Opnd_Type))
9592 then
9593 return True;
9594
9595 -- Special check for common access type error case
9596
9597 elsif Ekind (Target_Type) = E_Access_Type
9598 and then Is_Access_Type (Opnd_Type)
9599 then
9600 Error_Msg_N ("target type must be general access type!", N);
9601 Error_Msg_NE ("add ALL to }!", N, Target_Type);
9602
9603 return False;
9604
9605 else
9606 Error_Msg_NE ("invalid conversion, not compatible with }",
9607 N, Opnd_Type);
9608
9609 return False;
9610 end if;
9611 end Valid_Conversion;
9612
9613 end Sem_Res;