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