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