784f6bd98c6f21ec117a680d179b1f5e5bad2767
[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 -- AI05-144-2: Check dangerous order dependence within an expression
2748 -- that is not a subexpression. Exclude RHS of an assignment, because
2749 -- both sides may have side-effects and the check must be performed
2750 -- over the statement.
2751
2752 if Nkind (Parent (N)) not in N_Subexpr
2753 and then Nkind (Parent (N)) /= N_Assignment_Statement
2754 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2755 then
2756 Check_Order_Dependence;
2757 end if;
2758
2759 -- The expression is definitely NOT overloaded at this point, so
2760 -- we reset the Is_Overloaded flag to avoid any confusion when
2761 -- reanalyzing the node.
2762
2763 Set_Is_Overloaded (N, False);
2764
2765 -- Freeze expression type, entity if it is a name, and designated
2766 -- type if it is an allocator (RM 13.14(10,11,13)).
2767
2768 -- Now that the resolution of the type of the node is complete,
2769 -- and we did not detect an error, we can expand this node. We
2770 -- skip the expand call if we are in a default expression, see
2771 -- section "Handling of Default Expressions" in Sem spec.
2772
2773 Debug_A_Exit ("resolving ", N, " (done)");
2774
2775 -- We unconditionally freeze the expression, even if we are in
2776 -- default expression mode (the Freeze_Expression routine tests
2777 -- this flag and only freezes static types if it is set).
2778
2779 Freeze_Expression (N);
2780
2781 -- Now we can do the expansion
2782
2783 Expand (N);
2784 end if;
2785 end Resolve;
2786
2787 -------------
2788 -- Resolve --
2789 -------------
2790
2791 -- Version with check(s) suppressed
2792
2793 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2794 begin
2795 if Suppress = All_Checks then
2796 declare
2797 Svg : constant Suppress_Array := Scope_Suppress;
2798 begin
2799 Scope_Suppress := (others => True);
2800 Resolve (N, Typ);
2801 Scope_Suppress := Svg;
2802 end;
2803
2804 else
2805 declare
2806 Svg : constant Boolean := Scope_Suppress (Suppress);
2807 begin
2808 Scope_Suppress (Suppress) := True;
2809 Resolve (N, Typ);
2810 Scope_Suppress (Suppress) := Svg;
2811 end;
2812 end if;
2813 end Resolve;
2814
2815 -------------
2816 -- Resolve --
2817 -------------
2818
2819 -- Version with implicit type
2820
2821 procedure Resolve (N : Node_Id) is
2822 begin
2823 Resolve (N, Etype (N));
2824 end Resolve;
2825
2826 ---------------------
2827 -- Resolve_Actuals --
2828 ---------------------
2829
2830 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2831 Loc : constant Source_Ptr := Sloc (N);
2832 A : Node_Id;
2833 F : Entity_Id;
2834 A_Typ : Entity_Id;
2835 F_Typ : Entity_Id;
2836 Prev : Node_Id := Empty;
2837 Orig_A : Node_Id;
2838
2839 procedure Check_Argument_Order;
2840 -- Performs a check for the case where the actuals are all simple
2841 -- identifiers that correspond to the formal names, but in the wrong
2842 -- order, which is considered suspicious and cause for a warning.
2843
2844 procedure Check_Prefixed_Call;
2845 -- If the original node is an overloaded call in prefix notation,
2846 -- insert an 'Access or a dereference as needed over the first actual.
2847 -- Try_Object_Operation has already verified that there is a valid
2848 -- interpretation, but the form of the actual can only be determined
2849 -- once the primitive operation is identified.
2850
2851 procedure Insert_Default;
2852 -- If the actual is missing in a call, insert in the actuals list
2853 -- an instance of the default expression. The insertion is always
2854 -- a named association.
2855
2856 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2857 -- Check whether T1 and T2, or their full views, are derived from a
2858 -- common type. Used to enforce the restrictions on array conversions
2859 -- of AI95-00246.
2860
2861 function Static_Concatenation (N : Node_Id) return Boolean;
2862 -- Predicate to determine whether an actual that is a concatenation
2863 -- will be evaluated statically and does not need a transient scope.
2864 -- This must be determined before the actual is resolved and expanded
2865 -- because if needed the transient scope must be introduced earlier.
2866
2867 --------------------------
2868 -- Check_Argument_Order --
2869 --------------------------
2870
2871 procedure Check_Argument_Order is
2872 begin
2873 -- Nothing to do if no parameters, or original node is neither a
2874 -- function call nor a procedure call statement (happens in the
2875 -- operator-transformed-to-function call case), or the call does
2876 -- not come from source, or this warning is off.
2877
2878 if not Warn_On_Parameter_Order
2879 or else
2880 No (Parameter_Associations (N))
2881 or else
2882 not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2883 N_Function_Call)
2884 or else
2885 not Comes_From_Source (N)
2886 then
2887 return;
2888 end if;
2889
2890 declare
2891 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2892
2893 begin
2894 -- Nothing to do if only one parameter
2895
2896 if Nargs < 2 then
2897 return;
2898 end if;
2899
2900 -- Here if at least two arguments
2901
2902 declare
2903 Actuals : array (1 .. Nargs) of Node_Id;
2904 Actual : Node_Id;
2905 Formal : Node_Id;
2906
2907 Wrong_Order : Boolean := False;
2908 -- Set True if an out of order case is found
2909
2910 begin
2911 -- Collect identifier names of actuals, fail if any actual is
2912 -- not a simple identifier, and record max length of name.
2913
2914 Actual := First (Parameter_Associations (N));
2915 for J in Actuals'Range loop
2916 if Nkind (Actual) /= N_Identifier then
2917 return;
2918 else
2919 Actuals (J) := Actual;
2920 Next (Actual);
2921 end if;
2922 end loop;
2923
2924 -- If we got this far, all actuals are identifiers and the list
2925 -- of their names is stored in the Actuals array.
2926
2927 Formal := First_Formal (Nam);
2928 for J in Actuals'Range loop
2929
2930 -- If we ran out of formals, that's odd, probably an error
2931 -- which will be detected elsewhere, but abandon the search.
2932
2933 if No (Formal) then
2934 return;
2935 end if;
2936
2937 -- If name matches and is in order OK
2938
2939 if Chars (Formal) = Chars (Actuals (J)) then
2940 null;
2941
2942 else
2943 -- If no match, see if it is elsewhere in list and if so
2944 -- flag potential wrong order if type is compatible.
2945
2946 for K in Actuals'Range loop
2947 if Chars (Formal) = Chars (Actuals (K))
2948 and then
2949 Has_Compatible_Type (Actuals (K), Etype (Formal))
2950 then
2951 Wrong_Order := True;
2952 goto Continue;
2953 end if;
2954 end loop;
2955
2956 -- No match
2957
2958 return;
2959 end if;
2960
2961 <<Continue>> Next_Formal (Formal);
2962 end loop;
2963
2964 -- If Formals left over, also probably an error, skip warning
2965
2966 if Present (Formal) then
2967 return;
2968 end if;
2969
2970 -- Here we give the warning if something was out of order
2971
2972 if Wrong_Order then
2973 Error_Msg_N
2974 ("actuals for this call may be in wrong order?", N);
2975 end if;
2976 end;
2977 end;
2978 end Check_Argument_Order;
2979
2980 -------------------------
2981 -- Check_Prefixed_Call --
2982 -------------------------
2983
2984 procedure Check_Prefixed_Call is
2985 Act : constant Node_Id := First_Actual (N);
2986 A_Type : constant Entity_Id := Etype (Act);
2987 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2988 Orig : constant Node_Id := Original_Node (N);
2989 New_A : Node_Id;
2990
2991 begin
2992 -- Check whether the call is a prefixed call, with or without
2993 -- additional actuals.
2994
2995 if Nkind (Orig) = N_Selected_Component
2996 or else
2997 (Nkind (Orig) = N_Indexed_Component
2998 and then Nkind (Prefix (Orig)) = N_Selected_Component
2999 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3000 and then Is_Entity_Name (Act)
3001 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3002 then
3003 if Is_Access_Type (A_Type)
3004 and then not Is_Access_Type (F_Type)
3005 then
3006 -- Introduce dereference on object in prefix
3007
3008 New_A :=
3009 Make_Explicit_Dereference (Sloc (Act),
3010 Prefix => Relocate_Node (Act));
3011 Rewrite (Act, New_A);
3012 Analyze (Act);
3013
3014 elsif Is_Access_Type (F_Type)
3015 and then not Is_Access_Type (A_Type)
3016 then
3017 -- Introduce an implicit 'Access in prefix
3018
3019 if not Is_Aliased_View (Act) then
3020 Error_Msg_NE
3021 ("object in prefixed call to& must be aliased"
3022 & " (RM-2005 4.3.1 (13))",
3023 Prefix (Act), Nam);
3024 end if;
3025
3026 Rewrite (Act,
3027 Make_Attribute_Reference (Loc,
3028 Attribute_Name => Name_Access,
3029 Prefix => Relocate_Node (Act)));
3030 end if;
3031
3032 Analyze (Act);
3033 end if;
3034 end Check_Prefixed_Call;
3035
3036 --------------------
3037 -- Insert_Default --
3038 --------------------
3039
3040 procedure Insert_Default is
3041 Actval : Node_Id;
3042 Assoc : Node_Id;
3043
3044 begin
3045 -- Missing argument in call, nothing to insert
3046
3047 if No (Default_Value (F)) then
3048 return;
3049
3050 else
3051 -- Note that we do a full New_Copy_Tree, so that any associated
3052 -- Itypes are properly copied. This may not be needed any more,
3053 -- but it does no harm as a safety measure! Defaults of a generic
3054 -- formal may be out of bounds of the corresponding actual (see
3055 -- cc1311b) and an additional check may be required.
3056
3057 Actval :=
3058 New_Copy_Tree
3059 (Default_Value (F),
3060 New_Scope => Current_Scope,
3061 New_Sloc => Loc);
3062
3063 if Is_Concurrent_Type (Scope (Nam))
3064 and then Has_Discriminants (Scope (Nam))
3065 then
3066 Replace_Actual_Discriminants (N, Actval);
3067 end if;
3068
3069 if Is_Overloadable (Nam)
3070 and then Present (Alias (Nam))
3071 then
3072 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3073 and then not Is_Tagged_Type (Etype (F))
3074 then
3075 -- If default is a real literal, do not introduce a
3076 -- conversion whose effect may depend on the run-time
3077 -- size of universal real.
3078
3079 if Nkind (Actval) = N_Real_Literal then
3080 Set_Etype (Actval, Base_Type (Etype (F)));
3081 else
3082 Actval := Unchecked_Convert_To (Etype (F), Actval);
3083 end if;
3084 end if;
3085
3086 if Is_Scalar_Type (Etype (F)) then
3087 Enable_Range_Check (Actval);
3088 end if;
3089
3090 Set_Parent (Actval, N);
3091
3092 -- Resolve aggregates with their base type, to avoid scope
3093 -- anomalies: the subtype was first built in the subprogram
3094 -- declaration, and the current call may be nested.
3095
3096 if Nkind (Actval) = N_Aggregate then
3097 Analyze_And_Resolve (Actval, Etype (F));
3098 else
3099 Analyze_And_Resolve (Actval, Etype (Actval));
3100 end if;
3101
3102 else
3103 Set_Parent (Actval, N);
3104
3105 -- See note above concerning aggregates
3106
3107 if Nkind (Actval) = N_Aggregate
3108 and then Has_Discriminants (Etype (Actval))
3109 then
3110 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3111
3112 -- Resolve entities with their own type, which may differ
3113 -- from the type of a reference in a generic context (the
3114 -- view swapping mechanism did not anticipate the re-analysis
3115 -- of default values in calls).
3116
3117 elsif Is_Entity_Name (Actval) then
3118 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3119
3120 else
3121 Analyze_And_Resolve (Actval, Etype (Actval));
3122 end if;
3123 end if;
3124
3125 -- If default is a tag indeterminate function call, propagate
3126 -- tag to obtain proper dispatching.
3127
3128 if Is_Controlling_Formal (F)
3129 and then Nkind (Default_Value (F)) = N_Function_Call
3130 then
3131 Set_Is_Controlling_Actual (Actval);
3132 end if;
3133
3134 end if;
3135
3136 -- If the default expression raises constraint error, then just
3137 -- silently replace it with an N_Raise_Constraint_Error node,
3138 -- since we already gave the warning on the subprogram spec.
3139 -- If node is already a Raise_Constraint_Error leave as is, to
3140 -- prevent loops in the warnings removal machinery.
3141
3142 if Raises_Constraint_Error (Actval)
3143 and then Nkind (Actval) /= N_Raise_Constraint_Error
3144 then
3145 Rewrite (Actval,
3146 Make_Raise_Constraint_Error (Loc,
3147 Reason => CE_Range_Check_Failed));
3148 Set_Raises_Constraint_Error (Actval);
3149 Set_Etype (Actval, Etype (F));
3150 end if;
3151
3152 Assoc :=
3153 Make_Parameter_Association (Loc,
3154 Explicit_Actual_Parameter => Actval,
3155 Selector_Name => Make_Identifier (Loc, Chars (F)));
3156
3157 -- Case of insertion is first named actual
3158
3159 if No (Prev) or else
3160 Nkind (Parent (Prev)) /= N_Parameter_Association
3161 then
3162 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3163 Set_First_Named_Actual (N, Actval);
3164
3165 if No (Prev) then
3166 if No (Parameter_Associations (N)) then
3167 Set_Parameter_Associations (N, New_List (Assoc));
3168 else
3169 Append (Assoc, Parameter_Associations (N));
3170 end if;
3171
3172 else
3173 Insert_After (Prev, Assoc);
3174 end if;
3175
3176 -- Case of insertion is not first named actual
3177
3178 else
3179 Set_Next_Named_Actual
3180 (Assoc, Next_Named_Actual (Parent (Prev)));
3181 Set_Next_Named_Actual (Parent (Prev), Actval);
3182 Append (Assoc, Parameter_Associations (N));
3183 end if;
3184
3185 Mark_Rewrite_Insertion (Assoc);
3186 Mark_Rewrite_Insertion (Actval);
3187
3188 Prev := Actval;
3189 end Insert_Default;
3190
3191 -------------------
3192 -- Same_Ancestor --
3193 -------------------
3194
3195 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3196 FT1 : Entity_Id := T1;
3197 FT2 : Entity_Id := T2;
3198
3199 begin
3200 if Is_Private_Type (T1)
3201 and then Present (Full_View (T1))
3202 then
3203 FT1 := Full_View (T1);
3204 end if;
3205
3206 if Is_Private_Type (T2)
3207 and then Present (Full_View (T2))
3208 then
3209 FT2 := Full_View (T2);
3210 end if;
3211
3212 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3213 end Same_Ancestor;
3214
3215 --------------------------
3216 -- Static_Concatenation --
3217 --------------------------
3218
3219 function Static_Concatenation (N : Node_Id) return Boolean is
3220 begin
3221 case Nkind (N) is
3222 when N_String_Literal =>
3223 return True;
3224
3225 when N_Op_Concat =>
3226
3227 -- Concatenation is static when both operands are static
3228 -- and the concatenation operator is a predefined one.
3229
3230 return Scope (Entity (N)) = Standard_Standard
3231 and then
3232 Static_Concatenation (Left_Opnd (N))
3233 and then
3234 Static_Concatenation (Right_Opnd (N));
3235
3236 when others =>
3237 if Is_Entity_Name (N) then
3238 declare
3239 Ent : constant Entity_Id := Entity (N);
3240 begin
3241 return Ekind (Ent) = E_Constant
3242 and then Present (Constant_Value (Ent))
3243 and then
3244 Is_Static_Expression (Constant_Value (Ent));
3245 end;
3246
3247 else
3248 return False;
3249 end if;
3250 end case;
3251 end Static_Concatenation;
3252
3253 -- Start of processing for Resolve_Actuals
3254
3255 begin
3256 Check_Argument_Order;
3257
3258 if Present (First_Actual (N)) then
3259 Check_Prefixed_Call;
3260 end if;
3261
3262 A := First_Actual (N);
3263 F := First_Formal (Nam);
3264 while Present (F) loop
3265 if No (A) and then Needs_No_Actuals (Nam) then
3266 null;
3267
3268 -- If we have an error in any actual or formal, indicated by a type
3269 -- of Any_Type, then abandon resolution attempt, and set result type
3270 -- to Any_Type.
3271
3272 elsif (Present (A) and then Etype (A) = Any_Type)
3273 or else Etype (F) = Any_Type
3274 then
3275 Set_Etype (N, Any_Type);
3276 return;
3277 end if;
3278
3279 -- Case where actual is present
3280
3281 -- If the actual is an entity, generate a reference to it now. We
3282 -- do this before the actual is resolved, because a formal of some
3283 -- protected subprogram, or a task discriminant, will be rewritten
3284 -- during expansion, and the reference to the source entity may
3285 -- be lost.
3286
3287 if Present (A)
3288 and then Is_Entity_Name (A)
3289 and then Comes_From_Source (N)
3290 then
3291 Orig_A := Entity (A);
3292
3293 if Present (Orig_A) then
3294 if Is_Formal (Orig_A)
3295 and then Ekind (F) /= E_In_Parameter
3296 then
3297 Generate_Reference (Orig_A, A, 'm');
3298 elsif not Is_Overloaded (A) then
3299 Generate_Reference (Orig_A, A);
3300 end if;
3301 end if;
3302 end if;
3303
3304 if Present (A)
3305 and then (Nkind (Parent (A)) /= N_Parameter_Association
3306 or else
3307 Chars (Selector_Name (Parent (A))) = Chars (F))
3308 then
3309 -- If style checking mode on, check match of formal name
3310
3311 if Style_Check then
3312 if Nkind (Parent (A)) = N_Parameter_Association then
3313 Check_Identifier (Selector_Name (Parent (A)), F);
3314 end if;
3315 end if;
3316
3317 -- If the formal is Out or In_Out, do not resolve and expand the
3318 -- conversion, because it is subsequently expanded into explicit
3319 -- temporaries and assignments. However, the object of the
3320 -- conversion can be resolved. An exception is the case of tagged
3321 -- type conversion with a class-wide actual. In that case we want
3322 -- the tag check to occur and no temporary will be needed (no
3323 -- representation change can occur) and the parameter is passed by
3324 -- reference, so we go ahead and resolve the type conversion.
3325 -- Another exception is the case of reference to component or
3326 -- subcomponent of a bit-packed array, in which case we want to
3327 -- defer expansion to the point the in and out assignments are
3328 -- performed.
3329
3330 if Ekind (F) /= E_In_Parameter
3331 and then Nkind (A) = N_Type_Conversion
3332 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3333 then
3334 if Ekind (F) = E_In_Out_Parameter
3335 and then Is_Array_Type (Etype (F))
3336 then
3337 -- In a view conversion, the conversion must be legal in
3338 -- both directions, and thus both component types must be
3339 -- aliased, or neither (4.6 (8)).
3340
3341 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3342 -- the privacy requirement should not apply to generic
3343 -- types, and should be checked in an instance. ARG query
3344 -- is in order ???
3345
3346 if Has_Aliased_Components (Etype (Expression (A))) /=
3347 Has_Aliased_Components (Etype (F))
3348 then
3349 Error_Msg_N
3350 ("both component types in a view conversion must be"
3351 & " aliased, or neither", A);
3352
3353 -- Comment here??? what set of cases???
3354
3355 elsif
3356 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3357 then
3358 -- Check view conv between unrelated by ref array types
3359
3360 if Is_By_Reference_Type (Etype (F))
3361 or else Is_By_Reference_Type (Etype (Expression (A)))
3362 then
3363 Error_Msg_N
3364 ("view conversion between unrelated by reference " &
3365 "array types not allowed (\'A'I-00246)", A);
3366
3367 -- In Ada 2005 mode, check view conversion component
3368 -- type cannot be private, tagged, or volatile. Note
3369 -- that we only apply this to source conversions. The
3370 -- generated code can contain conversions which are
3371 -- not subject to this test, and we cannot extract the
3372 -- component type in such cases since it is not present.
3373
3374 elsif Comes_From_Source (A)
3375 and then Ada_Version >= Ada_2005
3376 then
3377 declare
3378 Comp_Type : constant Entity_Id :=
3379 Component_Type
3380 (Etype (Expression (A)));
3381 begin
3382 if (Is_Private_Type (Comp_Type)
3383 and then not Is_Generic_Type (Comp_Type))
3384 or else Is_Tagged_Type (Comp_Type)
3385 or else Is_Volatile (Comp_Type)
3386 then
3387 Error_Msg_N
3388 ("component type of a view conversion cannot"
3389 & " be private, tagged, or volatile"
3390 & " (RM 4.6 (24))",
3391 Expression (A));
3392 end if;
3393 end;
3394 end if;
3395 end if;
3396 end if;
3397
3398 -- Resolve expression if conversion is all OK
3399
3400 if (Conversion_OK (A)
3401 or else Valid_Conversion (A, Etype (A), Expression (A)))
3402 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3403 then
3404 Resolve (Expression (A));
3405 end if;
3406
3407 -- If the actual is a function call that returns a limited
3408 -- unconstrained object that needs finalization, create a
3409 -- transient scope for it, so that it can receive the proper
3410 -- finalization list.
3411
3412 elsif Nkind (A) = N_Function_Call
3413 and then Is_Limited_Record (Etype (F))
3414 and then not Is_Constrained (Etype (F))
3415 and then Expander_Active
3416 and then
3417 (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3418 then
3419 Establish_Transient_Scope (A, False);
3420
3421 -- A small optimization: if one of the actuals is a concatenation
3422 -- create a block around a procedure call to recover stack space.
3423 -- This alleviates stack usage when several procedure calls in
3424 -- the same statement list use concatenation. We do not perform
3425 -- this wrapping for code statements, where the argument is a
3426 -- static string, and we want to preserve warnings involving
3427 -- sequences of such statements.
3428
3429 elsif Nkind (A) = N_Op_Concat
3430 and then Nkind (N) = N_Procedure_Call_Statement
3431 and then Expander_Active
3432 and then
3433 not (Is_Intrinsic_Subprogram (Nam)
3434 and then Chars (Nam) = Name_Asm)
3435 and then not Static_Concatenation (A)
3436 then
3437 Establish_Transient_Scope (A, False);
3438 Resolve (A, Etype (F));
3439
3440 else
3441 if Nkind (A) = N_Type_Conversion
3442 and then Is_Array_Type (Etype (F))
3443 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3444 and then
3445 (Is_Limited_Type (Etype (F))
3446 or else Is_Limited_Type (Etype (Expression (A))))
3447 then
3448 Error_Msg_N
3449 ("conversion between unrelated limited array types " &
3450 "not allowed (\A\I-00246)", A);
3451
3452 if Is_Limited_Type (Etype (F)) then
3453 Explain_Limited_Type (Etype (F), A);
3454 end if;
3455
3456 if Is_Limited_Type (Etype (Expression (A))) then
3457 Explain_Limited_Type (Etype (Expression (A)), A);
3458 end if;
3459 end if;
3460
3461 -- (Ada 2005: AI-251): If the actual is an allocator whose
3462 -- directly designated type is a class-wide interface, we build
3463 -- an anonymous access type to use it as the type of the
3464 -- allocator. Later, when the subprogram call is expanded, if
3465 -- the interface has a secondary dispatch table the expander
3466 -- will add a type conversion to force the correct displacement
3467 -- of the pointer.
3468
3469 if Nkind (A) = N_Allocator then
3470 declare
3471 DDT : constant Entity_Id :=
3472 Directly_Designated_Type (Base_Type (Etype (F)));
3473
3474 New_Itype : Entity_Id;
3475
3476 begin
3477 if Is_Class_Wide_Type (DDT)
3478 and then Is_Interface (DDT)
3479 then
3480 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3481 Set_Etype (New_Itype, Etype (A));
3482 Set_Directly_Designated_Type (New_Itype,
3483 Directly_Designated_Type (Etype (A)));
3484 Set_Etype (A, New_Itype);
3485 end if;
3486
3487 -- Ada 2005, AI-162:If the actual is an allocator, the
3488 -- innermost enclosing statement is the master of the
3489 -- created object. This needs to be done with expansion
3490 -- enabled only, otherwise the transient scope will not
3491 -- be removed in the expansion of the wrapped construct.
3492
3493 if (Is_Controlled (DDT) or else Has_Task (DDT))
3494 and then Expander_Active
3495 then
3496 Establish_Transient_Scope (A, False);
3497 end if;
3498 end;
3499 end if;
3500
3501 -- (Ada 2005): The call may be to a primitive operation of
3502 -- a tagged synchronized type, declared outside of the type.
3503 -- In this case the controlling actual must be converted to
3504 -- its corresponding record type, which is the formal type.
3505 -- The actual may be a subtype, either because of a constraint
3506 -- or because it is a generic actual, so use base type to
3507 -- locate concurrent type.
3508
3509 A_Typ := Base_Type (Etype (A));
3510 F_Typ := Base_Type (Etype (F));
3511
3512 declare
3513 Full_A_Typ : Entity_Id;
3514
3515 begin
3516 if Present (Full_View (A_Typ)) then
3517 Full_A_Typ := Base_Type (Full_View (A_Typ));
3518 else
3519 Full_A_Typ := A_Typ;
3520 end if;
3521
3522 -- Tagged synchronized type (case 1): the actual is a
3523 -- concurrent type
3524
3525 if Is_Concurrent_Type (A_Typ)
3526 and then Corresponding_Record_Type (A_Typ) = F_Typ
3527 then
3528 Rewrite (A,
3529 Unchecked_Convert_To
3530 (Corresponding_Record_Type (A_Typ), A));
3531 Resolve (A, Etype (F));
3532
3533 -- Tagged synchronized type (case 2): the formal is a
3534 -- concurrent type
3535
3536 elsif Ekind (Full_A_Typ) = E_Record_Type
3537 and then Present
3538 (Corresponding_Concurrent_Type (Full_A_Typ))
3539 and then Is_Concurrent_Type (F_Typ)
3540 and then Present (Corresponding_Record_Type (F_Typ))
3541 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3542 then
3543 Resolve (A, Corresponding_Record_Type (F_Typ));
3544
3545 -- Common case
3546
3547 else
3548 Resolve (A, Etype (F));
3549 end if;
3550 end;
3551 end if;
3552
3553 A_Typ := Etype (A);
3554 F_Typ := Etype (F);
3555
3556 -- Save actual for subsequent check on order dependence, and
3557 -- indicate whether actual is modifiable. For AI05-0144-2.
3558
3559 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3560
3561 -- For mode IN, if actual is an entity, and the type of the formal
3562 -- has warnings suppressed, then we reset Never_Set_In_Source for
3563 -- the calling entity. The reason for this is to catch cases like
3564 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3565 -- uses trickery to modify an IN parameter.
3566
3567 if Ekind (F) = E_In_Parameter
3568 and then Is_Entity_Name (A)
3569 and then Present (Entity (A))
3570 and then Ekind (Entity (A)) = E_Variable
3571 and then Has_Warnings_Off (F_Typ)
3572 then
3573 Set_Never_Set_In_Source (Entity (A), False);
3574 end if;
3575
3576 -- Perform error checks for IN and IN OUT parameters
3577
3578 if Ekind (F) /= E_Out_Parameter then
3579
3580 -- Check unset reference. For scalar parameters, it is clearly
3581 -- wrong to pass an uninitialized value as either an IN or
3582 -- IN-OUT parameter. For composites, it is also clearly an
3583 -- error to pass a completely uninitialized value as an IN
3584 -- parameter, but the case of IN OUT is trickier. We prefer
3585 -- not to give a warning here. For example, suppose there is
3586 -- a routine that sets some component of a record to False.
3587 -- It is perfectly reasonable to make this IN-OUT and allow
3588 -- either initialized or uninitialized records to be passed
3589 -- in this case.
3590
3591 -- For partially initialized composite values, we also avoid
3592 -- warnings, since it is quite likely that we are passing a
3593 -- partially initialized value and only the initialized fields
3594 -- will in fact be read in the subprogram.
3595
3596 if Is_Scalar_Type (A_Typ)
3597 or else (Ekind (F) = E_In_Parameter
3598 and then not Is_Partially_Initialized_Type (A_Typ))
3599 then
3600 Check_Unset_Reference (A);
3601 end if;
3602
3603 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3604 -- actual to a nested call, since this is case of reading an
3605 -- out parameter, which is not allowed.
3606
3607 if Ada_Version = Ada_83
3608 and then Is_Entity_Name (A)
3609 and then Ekind (Entity (A)) = E_Out_Parameter
3610 then
3611 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3612 end if;
3613 end if;
3614
3615 -- Case of OUT or IN OUT parameter
3616
3617 if Ekind (F) /= E_In_Parameter then
3618
3619 -- For an Out parameter, check for useless assignment. Note
3620 -- that we can't set Last_Assignment this early, because we may
3621 -- kill current values in Resolve_Call, and that call would
3622 -- clobber the Last_Assignment field.
3623
3624 -- Note: call Warn_On_Useless_Assignment before doing the check
3625 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3626 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3627 -- reflects the last assignment, not this one!
3628
3629 if Ekind (F) = E_Out_Parameter then
3630 if Warn_On_Modified_As_Out_Parameter (F)
3631 and then Is_Entity_Name (A)
3632 and then Present (Entity (A))
3633 and then Comes_From_Source (N)
3634 then
3635 Warn_On_Useless_Assignment (Entity (A), A);
3636 end if;
3637 end if;
3638
3639 -- Validate the form of the actual. Note that the call to
3640 -- Is_OK_Variable_For_Out_Formal generates the required
3641 -- reference in this case.
3642
3643 if not Is_OK_Variable_For_Out_Formal (A) then
3644 Error_Msg_NE ("actual for& must be a variable", A, F);
3645 end if;
3646
3647 -- What's the following about???
3648
3649 if Is_Entity_Name (A) then
3650 Kill_Checks (Entity (A));
3651 else
3652 Kill_All_Checks;
3653 end if;
3654 end if;
3655
3656 if Etype (A) = Any_Type then
3657 Set_Etype (N, Any_Type);
3658 return;
3659 end if;
3660
3661 -- Apply appropriate range checks for in, out, and in-out
3662 -- parameters. Out and in-out parameters also need a separate
3663 -- check, if there is a type conversion, to make sure the return
3664 -- value meets the constraints of the variable before the
3665 -- conversion.
3666
3667 -- Gigi looks at the check flag and uses the appropriate types.
3668 -- For now since one flag is used there is an optimization which
3669 -- might not be done in the In Out case since Gigi does not do
3670 -- any analysis. More thought required about this ???
3671
3672 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3673
3674 -- Apply predicate checks, unless this is a call to the
3675 -- predicate check function itself, which would cause an
3676 -- infinite recursion.
3677
3678 if not (Ekind (Nam) = E_Function
3679 and then Has_Predicates (Nam))
3680 then
3681 Apply_Predicate_Check (A, F_Typ);
3682 end if;
3683
3684 -- Apply required constraint checks
3685
3686 if Is_Scalar_Type (Etype (A)) then
3687 Apply_Scalar_Range_Check (A, F_Typ);
3688
3689 elsif Is_Array_Type (Etype (A)) then
3690 Apply_Length_Check (A, F_Typ);
3691
3692 elsif Is_Record_Type (F_Typ)
3693 and then Has_Discriminants (F_Typ)
3694 and then Is_Constrained (F_Typ)
3695 and then (not Is_Derived_Type (F_Typ)
3696 or else Comes_From_Source (Nam))
3697 then
3698 Apply_Discriminant_Check (A, F_Typ);
3699
3700 elsif Is_Access_Type (F_Typ)
3701 and then Is_Array_Type (Designated_Type (F_Typ))
3702 and then Is_Constrained (Designated_Type (F_Typ))
3703 then
3704 Apply_Length_Check (A, F_Typ);
3705
3706 elsif Is_Access_Type (F_Typ)
3707 and then Has_Discriminants (Designated_Type (F_Typ))
3708 and then Is_Constrained (Designated_Type (F_Typ))
3709 then
3710 Apply_Discriminant_Check (A, F_Typ);
3711
3712 else
3713 Apply_Range_Check (A, F_Typ);
3714 end if;
3715
3716 -- Ada 2005 (AI-231): Note that the controlling parameter case
3717 -- already existed in Ada 95, which is partially checked
3718 -- elsewhere (see Checks), and we don't want the warning
3719 -- message to differ.
3720
3721 if Is_Access_Type (F_Typ)
3722 and then Can_Never_Be_Null (F_Typ)
3723 and then Known_Null (A)
3724 then
3725 if Is_Controlling_Formal (F) then
3726 Apply_Compile_Time_Constraint_Error
3727 (N => A,
3728 Msg => "null value not allowed here?",
3729 Reason => CE_Access_Check_Failed);
3730
3731 elsif Ada_Version >= Ada_2005 then
3732 Apply_Compile_Time_Constraint_Error
3733 (N => A,
3734 Msg => "(Ada 2005) null not allowed in "
3735 & "null-excluding formal?",
3736 Reason => CE_Null_Not_Allowed);
3737 end if;
3738 end if;
3739 end if;
3740
3741 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3742 if Nkind (A) = N_Type_Conversion then
3743 if Is_Scalar_Type (A_Typ) then
3744 Apply_Scalar_Range_Check
3745 (Expression (A), Etype (Expression (A)), A_Typ);
3746 else
3747 Apply_Range_Check
3748 (Expression (A), Etype (Expression (A)), A_Typ);
3749 end if;
3750
3751 else
3752 if Is_Scalar_Type (F_Typ) then
3753 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3754
3755 elsif Is_Array_Type (F_Typ)
3756 and then Ekind (F) = E_Out_Parameter
3757 then
3758 Apply_Length_Check (A, F_Typ);
3759
3760 else
3761 Apply_Range_Check (A, A_Typ, F_Typ);
3762 end if;
3763 end if;
3764 end if;
3765
3766 -- An actual associated with an access parameter is implicitly
3767 -- converted to the anonymous access type of the formal and must
3768 -- satisfy the legality checks for access conversions.
3769
3770 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3771 if not Valid_Conversion (A, F_Typ, A) then
3772 Error_Msg_N
3773 ("invalid implicit conversion for access parameter", A);
3774 end if;
3775 end if;
3776
3777 -- Check bad case of atomic/volatile argument (RM C.6(12))
3778
3779 if Is_By_Reference_Type (Etype (F))
3780 and then Comes_From_Source (N)
3781 then
3782 if Is_Atomic_Object (A)
3783 and then not Is_Atomic (Etype (F))
3784 then
3785 Error_Msg_N
3786 ("cannot pass atomic argument to non-atomic formal",
3787 N);
3788
3789 elsif Is_Volatile_Object (A)
3790 and then not Is_Volatile (Etype (F))
3791 then
3792 Error_Msg_N
3793 ("cannot pass volatile argument to non-volatile formal",
3794 N);
3795 end if;
3796 end if;
3797
3798 -- Check that subprograms don't have improper controlling
3799 -- arguments (RM 3.9.2 (9)).
3800
3801 -- A primitive operation may have an access parameter of an
3802 -- incomplete tagged type, but a dispatching call is illegal
3803 -- if the type is still incomplete.
3804
3805 if Is_Controlling_Formal (F) then
3806 Set_Is_Controlling_Actual (A);
3807
3808 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3809 declare
3810 Desig : constant Entity_Id := Designated_Type (Etype (F));
3811 begin
3812 if Ekind (Desig) = E_Incomplete_Type
3813 and then No (Full_View (Desig))
3814 and then No (Non_Limited_View (Desig))
3815 then
3816 Error_Msg_NE
3817 ("premature use of incomplete type& " &
3818 "in dispatching call", A, Desig);
3819 end if;
3820 end;
3821 end if;
3822
3823 elsif Nkind (A) = N_Explicit_Dereference then
3824 Validate_Remote_Access_To_Class_Wide_Type (A);
3825 end if;
3826
3827 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3828 and then not Is_Class_Wide_Type (F_Typ)
3829 and then not Is_Controlling_Formal (F)
3830 then
3831 Error_Msg_N ("class-wide argument not allowed here!", A);
3832
3833 if Is_Subprogram (Nam)
3834 and then Comes_From_Source (Nam)
3835 then
3836 Error_Msg_Node_2 := F_Typ;
3837 Error_Msg_NE
3838 ("& is not a dispatching operation of &!", A, Nam);
3839 end if;
3840
3841 elsif Is_Access_Type (A_Typ)
3842 and then Is_Access_Type (F_Typ)
3843 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3844 and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3845 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3846 or else (Nkind (A) = N_Attribute_Reference
3847 and then
3848 Is_Class_Wide_Type (Etype (Prefix (A)))))
3849 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3850 and then not Is_Controlling_Formal (F)
3851
3852 -- Disable these checks for call to imported C++ subprograms
3853
3854 and then not
3855 (Is_Entity_Name (Name (N))
3856 and then Is_Imported (Entity (Name (N)))
3857 and then Convention (Entity (Name (N))) = Convention_CPP)
3858 then
3859 Error_Msg_N
3860 ("access to class-wide argument not allowed here!", A);
3861
3862 if Is_Subprogram (Nam)
3863 and then Comes_From_Source (Nam)
3864 then
3865 Error_Msg_Node_2 := Designated_Type (F_Typ);
3866 Error_Msg_NE
3867 ("& is not a dispatching operation of &!", A, Nam);
3868 end if;
3869 end if;
3870
3871 Eval_Actual (A);
3872
3873 -- If it is a named association, treat the selector_name as a
3874 -- proper identifier, and mark the corresponding entity.
3875
3876 if Nkind (Parent (A)) = N_Parameter_Association then
3877 Set_Entity (Selector_Name (Parent (A)), F);
3878 Generate_Reference (F, Selector_Name (Parent (A)));
3879 Set_Etype (Selector_Name (Parent (A)), F_Typ);
3880 Generate_Reference (F_Typ, N, ' ');
3881 end if;
3882
3883 Prev := A;
3884
3885 if Ekind (F) /= E_Out_Parameter then
3886 Check_Unset_Reference (A);
3887 end if;
3888
3889 Next_Actual (A);
3890
3891 -- Case where actual is not present
3892
3893 else
3894 Insert_Default;
3895 end if;
3896
3897 Next_Formal (F);
3898 end loop;
3899 end Resolve_Actuals;
3900
3901 -----------------------
3902 -- Resolve_Allocator --
3903 -----------------------
3904
3905 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
3906 E : constant Node_Id := Expression (N);
3907 Subtyp : Entity_Id;
3908 Discrim : Entity_Id;
3909 Constr : Node_Id;
3910 Aggr : Node_Id;
3911 Assoc : Node_Id := Empty;
3912 Disc_Exp : Node_Id;
3913
3914 procedure Check_Allocator_Discrim_Accessibility
3915 (Disc_Exp : Node_Id;
3916 Alloc_Typ : Entity_Id);
3917 -- Check that accessibility level associated with an access discriminant
3918 -- initialized in an allocator by the expression Disc_Exp is not deeper
3919 -- than the level of the allocator type Alloc_Typ. An error message is
3920 -- issued if this condition is violated. Specialized checks are done for
3921 -- the cases of a constraint expression which is an access attribute or
3922 -- an access discriminant.
3923
3924 function In_Dispatching_Context return Boolean;
3925 -- If the allocator is an actual in a call, it is allowed to be class-
3926 -- wide when the context is not because it is a controlling actual.
3927
3928 procedure Propagate_Coextensions (Root : Node_Id);
3929 -- Propagate all nested coextensions which are located one nesting
3930 -- level down the tree to the node Root. Example:
3931 --
3932 -- Top_Record
3933 -- Level_1_Coextension
3934 -- Level_2_Coextension
3935 --
3936 -- The algorithm is paired with delay actions done by the Expander. In
3937 -- the above example, assume all coextensions are controlled types.
3938 -- The cycle of analysis, resolution and expansion will yield:
3939 --
3940 -- 1) Analyze Top_Record
3941 -- 2) Analyze Level_1_Coextension
3942 -- 3) Analyze Level_2_Coextension
3943 -- 4) Resolve Level_2_Coextension. The allocator is marked as a
3944 -- coextension.
3945 -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is
3946 -- generated to capture the allocated object. Temp_1 is attached
3947 -- to the coextension chain of Level_2_Coextension.
3948 -- 6) Resolve Level_1_Coextension. The allocator is marked as a
3949 -- coextension. A forward tree traversal is performed which finds
3950 -- Level_2_Coextension's list and copies its contents into its
3951 -- own list.
3952 -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is
3953 -- generated to capture the allocated object. Temp_2 is attached
3954 -- to the coextension chain of Level_1_Coextension. Currently, the
3955 -- contents of the list are [Temp_2, Temp_1].
3956 -- 8) Resolve Top_Record. A forward tree traversal is performed which
3957 -- finds Level_1_Coextension's list and copies its contents into
3958 -- its own list.
3959 -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and
3960 -- Temp_2 and attach them to Top_Record's finalization list.
3961
3962 -------------------------------------------
3963 -- Check_Allocator_Discrim_Accessibility --
3964 -------------------------------------------
3965
3966 procedure Check_Allocator_Discrim_Accessibility
3967 (Disc_Exp : Node_Id;
3968 Alloc_Typ : Entity_Id)
3969 is
3970 begin
3971 if Type_Access_Level (Etype (Disc_Exp)) >
3972 Type_Access_Level (Alloc_Typ)
3973 then
3974 Error_Msg_N
3975 ("operand type has deeper level than allocator type", Disc_Exp);
3976
3977 -- When the expression is an Access attribute the level of the prefix
3978 -- object must not be deeper than that of the allocator's type.
3979
3980 elsif Nkind (Disc_Exp) = N_Attribute_Reference
3981 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
3982 = Attribute_Access
3983 and then Object_Access_Level (Prefix (Disc_Exp))
3984 > Type_Access_Level (Alloc_Typ)
3985 then
3986 Error_Msg_N
3987 ("prefix of attribute has deeper level than allocator type",
3988 Disc_Exp);
3989
3990 -- When the expression is an access discriminant the check is against
3991 -- the level of the prefix object.
3992
3993 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
3994 and then Nkind (Disc_Exp) = N_Selected_Component
3995 and then Object_Access_Level (Prefix (Disc_Exp))
3996 > Type_Access_Level (Alloc_Typ)
3997 then
3998 Error_Msg_N
3999 ("access discriminant has deeper level than allocator type",
4000 Disc_Exp);
4001
4002 -- All other cases are legal
4003
4004 else
4005 null;
4006 end if;
4007 end Check_Allocator_Discrim_Accessibility;
4008
4009 ----------------------------
4010 -- In_Dispatching_Context --
4011 ----------------------------
4012
4013 function In_Dispatching_Context return Boolean is
4014 Par : constant Node_Id := Parent (N);
4015 begin
4016 return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
4017 and then Is_Entity_Name (Name (Par))
4018 and then Is_Dispatching_Operation (Entity (Name (Par)));
4019 end In_Dispatching_Context;
4020
4021 ----------------------------
4022 -- Propagate_Coextensions --
4023 ----------------------------
4024
4025 procedure Propagate_Coextensions (Root : Node_Id) is
4026
4027 procedure Copy_List (From : Elist_Id; To : Elist_Id);
4028 -- Copy the contents of list From into list To, preserving the
4029 -- order of elements.
4030
4031 function Process_Allocator (Nod : Node_Id) return Traverse_Result;
4032 -- Recognize an allocator or a rewritten allocator node and add it
4033 -- along with its nested coextensions to the list of Root.
4034
4035 ---------------
4036 -- Copy_List --
4037 ---------------
4038
4039 procedure Copy_List (From : Elist_Id; To : Elist_Id) is
4040 From_Elmt : Elmt_Id;
4041 begin
4042 From_Elmt := First_Elmt (From);
4043 while Present (From_Elmt) loop
4044 Append_Elmt (Node (From_Elmt), To);
4045 Next_Elmt (From_Elmt);
4046 end loop;
4047 end Copy_List;
4048
4049 -----------------------
4050 -- Process_Allocator --
4051 -----------------------
4052
4053 function Process_Allocator (Nod : Node_Id) return Traverse_Result is
4054 Orig_Nod : Node_Id := Nod;
4055
4056 begin
4057 -- This is a possible rewritten subtype indication allocator. Any
4058 -- nested coextensions will appear as discriminant constraints.
4059
4060 if Nkind (Nod) = N_Identifier
4061 and then Present (Original_Node (Nod))
4062 and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
4063 then
4064 declare
4065 Discr : Node_Id;
4066 Discr_Elmt : Elmt_Id;
4067
4068 begin
4069 if Is_Record_Type (Entity (Nod)) then
4070 Discr_Elmt :=
4071 First_Elmt (Discriminant_Constraint (Entity (Nod)));
4072 while Present (Discr_Elmt) loop
4073 Discr := Node (Discr_Elmt);
4074
4075 if Nkind (Discr) = N_Identifier
4076 and then Present (Original_Node (Discr))
4077 and then Nkind (Original_Node (Discr)) = N_Allocator
4078 and then Present (Coextensions (
4079 Original_Node (Discr)))
4080 then
4081 if No (Coextensions (Root)) then
4082 Set_Coextensions (Root, New_Elmt_List);
4083 end if;
4084
4085 Copy_List
4086 (From => Coextensions (Original_Node (Discr)),
4087 To => Coextensions (Root));
4088 end if;
4089
4090 Next_Elmt (Discr_Elmt);
4091 end loop;
4092
4093 -- There is no need to continue the traversal of this
4094 -- subtree since all the information has already been
4095 -- propagated.
4096
4097 return Skip;
4098 end if;
4099 end;
4100
4101 -- Case of either a stand alone allocator or a rewritten allocator
4102 -- with an aggregate.
4103
4104 else
4105 if Present (Original_Node (Nod)) then
4106 Orig_Nod := Original_Node (Nod);
4107 end if;
4108
4109 if Nkind (Orig_Nod) = N_Allocator then
4110
4111 -- Propagate the list of nested coextensions to the Root
4112 -- allocator. This is done through list copy since a single
4113 -- allocator may have multiple coextensions. Do not touch
4114 -- coextensions roots.
4115
4116 if not Is_Coextension_Root (Orig_Nod)
4117 and then Present (Coextensions (Orig_Nod))
4118 then
4119 if No (Coextensions (Root)) then
4120 Set_Coextensions (Root, New_Elmt_List);
4121 end if;
4122
4123 Copy_List
4124 (From => Coextensions (Orig_Nod),
4125 To => Coextensions (Root));
4126 end if;
4127
4128 -- There is no need to continue the traversal of this
4129 -- subtree since all the information has already been
4130 -- propagated.
4131
4132 return Skip;
4133 end if;
4134 end if;
4135
4136 -- Keep on traversing, looking for the next allocator
4137
4138 return OK;
4139 end Process_Allocator;
4140
4141 procedure Process_Allocators is
4142 new Traverse_Proc (Process_Allocator);
4143
4144 -- Start of processing for Propagate_Coextensions
4145
4146 begin
4147 Process_Allocators (Expression (Root));
4148 end Propagate_Coextensions;
4149
4150 -- Start of processing for Resolve_Allocator
4151
4152 begin
4153 -- Replace general access with specific type
4154
4155 if Ekind (Etype (N)) = E_Allocator_Type then
4156 Set_Etype (N, Base_Type (Typ));
4157 end if;
4158
4159 if Is_Abstract_Type (Typ) then
4160 Error_Msg_N ("type of allocator cannot be abstract", N);
4161 end if;
4162
4163 -- For qualified expression, resolve the expression using the
4164 -- given subtype (nothing to do for type mark, subtype indication)
4165
4166 if Nkind (E) = N_Qualified_Expression then
4167 if Is_Class_Wide_Type (Etype (E))
4168 and then not Is_Class_Wide_Type (Designated_Type (Typ))
4169 and then not In_Dispatching_Context
4170 then
4171 Error_Msg_N
4172 ("class-wide allocator not allowed for this access type", N);
4173 end if;
4174
4175 Resolve (Expression (E), Etype (E));
4176 Check_Unset_Reference (Expression (E));
4177
4178 -- A qualified expression requires an exact match of the type,
4179 -- class-wide matching is not allowed.
4180
4181 if (Is_Class_Wide_Type (Etype (Expression (E)))
4182 or else Is_Class_Wide_Type (Etype (E)))
4183 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4184 then
4185 Wrong_Type (Expression (E), Etype (E));
4186 end if;
4187
4188 -- A special accessibility check is needed for allocators that
4189 -- constrain access discriminants. The level of the type of the
4190 -- expression used to constrain an access discriminant cannot be
4191 -- deeper than the type of the allocator (in contrast to access
4192 -- parameters, where the level of the actual can be arbitrary).
4193
4194 -- We can't use Valid_Conversion to perform this check because
4195 -- in general the type of the allocator is unrelated to the type
4196 -- of the access discriminant.
4197
4198 if Ekind (Typ) /= E_Anonymous_Access_Type
4199 or else Is_Local_Anonymous_Access (Typ)
4200 then
4201 Subtyp := Entity (Subtype_Mark (E));
4202
4203 Aggr := Original_Node (Expression (E));
4204
4205 if Has_Discriminants (Subtyp)
4206 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4207 then
4208 Discrim := First_Discriminant (Base_Type (Subtyp));
4209
4210 -- Get the first component expression of the aggregate
4211
4212 if Present (Expressions (Aggr)) then
4213 Disc_Exp := First (Expressions (Aggr));
4214
4215 elsif Present (Component_Associations (Aggr)) then
4216 Assoc := First (Component_Associations (Aggr));
4217
4218 if Present (Assoc) then
4219 Disc_Exp := Expression (Assoc);
4220 else
4221 Disc_Exp := Empty;
4222 end if;
4223
4224 else
4225 Disc_Exp := Empty;
4226 end if;
4227
4228 while Present (Discrim) and then Present (Disc_Exp) loop
4229 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4230 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4231 end if;
4232
4233 Next_Discriminant (Discrim);
4234
4235 if Present (Discrim) then
4236 if Present (Assoc) then
4237 Next (Assoc);
4238 Disc_Exp := Expression (Assoc);
4239
4240 elsif Present (Next (Disc_Exp)) then
4241 Next (Disc_Exp);
4242
4243 else
4244 Assoc := First (Component_Associations (Aggr));
4245
4246 if Present (Assoc) then
4247 Disc_Exp := Expression (Assoc);
4248 else
4249 Disc_Exp := Empty;
4250 end if;
4251 end if;
4252 end if;
4253 end loop;
4254 end if;
4255 end if;
4256
4257 -- For a subtype mark or subtype indication, freeze the subtype
4258
4259 else
4260 Freeze_Expression (E);
4261
4262 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4263 Error_Msg_N
4264 ("initialization required for access-to-constant allocator", N);
4265 end if;
4266
4267 -- A special accessibility check is needed for allocators that
4268 -- constrain access discriminants. The level of the type of the
4269 -- expression used to constrain an access discriminant cannot be
4270 -- deeper than the type of the allocator (in contrast to access
4271 -- parameters, where the level of the actual can be arbitrary).
4272 -- We can't use Valid_Conversion to perform this check because
4273 -- in general the type of the allocator is unrelated to the type
4274 -- of the access discriminant.
4275
4276 if Nkind (Original_Node (E)) = N_Subtype_Indication
4277 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4278 or else Is_Local_Anonymous_Access (Typ))
4279 then
4280 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4281
4282 if Has_Discriminants (Subtyp) then
4283 Discrim := First_Discriminant (Base_Type (Subtyp));
4284 Constr := First (Constraints (Constraint (Original_Node (E))));
4285 while Present (Discrim) and then Present (Constr) loop
4286 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4287 if Nkind (Constr) = N_Discriminant_Association then
4288 Disc_Exp := Original_Node (Expression (Constr));
4289 else
4290 Disc_Exp := Original_Node (Constr);
4291 end if;
4292
4293 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4294 end if;
4295
4296 Next_Discriminant (Discrim);
4297 Next (Constr);
4298 end loop;
4299 end if;
4300 end if;
4301 end if;
4302
4303 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4304 -- check that the level of the type of the created object is not deeper
4305 -- than the level of the allocator's access type, since extensions can
4306 -- now occur at deeper levels than their ancestor types. This is a
4307 -- static accessibility level check; a run-time check is also needed in
4308 -- the case of an initialized allocator with a class-wide argument (see
4309 -- Expand_Allocator_Expression).
4310
4311 if Ada_Version >= Ada_2005
4312 and then Is_Class_Wide_Type (Designated_Type (Typ))
4313 then
4314 declare
4315 Exp_Typ : Entity_Id;
4316
4317 begin
4318 if Nkind (E) = N_Qualified_Expression then
4319 Exp_Typ := Etype (E);
4320 elsif Nkind (E) = N_Subtype_Indication then
4321 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4322 else
4323 Exp_Typ := Entity (E);
4324 end if;
4325
4326 if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
4327 if In_Instance_Body then
4328 Error_Msg_N ("?type in allocator has deeper level than" &
4329 " designated class-wide type", E);
4330 Error_Msg_N ("\?Program_Error will be raised at run time",
4331 E);
4332 Rewrite (N,
4333 Make_Raise_Program_Error (Sloc (N),
4334 Reason => PE_Accessibility_Check_Failed));
4335 Set_Etype (N, Typ);
4336
4337 -- Do not apply Ada 2005 accessibility checks on a class-wide
4338 -- allocator if the type given in the allocator is a formal
4339 -- type. A run-time check will be performed in the instance.
4340
4341 elsif not Is_Generic_Type (Exp_Typ) then
4342 Error_Msg_N ("type in allocator has deeper level than" &
4343 " designated class-wide type", E);
4344 end if;
4345 end if;
4346 end;
4347 end if;
4348
4349 -- Check for allocation from an empty storage pool
4350
4351 if No_Pool_Assigned (Typ) then
4352 Error_Msg_N ("allocation from empty storage pool!", N);
4353
4354 -- If the context is an unchecked conversion, as may happen within
4355 -- an inlined subprogram, the allocator is being resolved with its
4356 -- own anonymous type. In that case, if the target type has a specific
4357 -- storage pool, it must be inherited explicitly by the allocator type.
4358
4359 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4360 and then No (Associated_Storage_Pool (Typ))
4361 then
4362 Set_Associated_Storage_Pool
4363 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4364 end if;
4365
4366 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4367 Check_Restriction (No_Anonymous_Allocators, N);
4368 end if;
4369
4370 -- An erroneous allocator may be rewritten as a raise Program_Error
4371 -- statement.
4372
4373 if Nkind (N) = N_Allocator then
4374
4375 -- An anonymous access discriminant is the definition of a
4376 -- coextension.
4377
4378 if Ekind (Typ) = E_Anonymous_Access_Type
4379 and then Nkind (Associated_Node_For_Itype (Typ)) =
4380 N_Discriminant_Specification
4381 then
4382 -- Avoid marking an allocator as a dynamic coextension if it is
4383 -- within a static construct.
4384
4385 if not Is_Static_Coextension (N) then
4386 Set_Is_Dynamic_Coextension (N);
4387 end if;
4388
4389 -- Cleanup for potential static coextensions
4390
4391 else
4392 Set_Is_Dynamic_Coextension (N, False);
4393 Set_Is_Static_Coextension (N, False);
4394 end if;
4395
4396 -- There is no need to propagate any nested coextensions if they
4397 -- are marked as static since they will be rewritten on the spot.
4398
4399 if not Is_Static_Coextension (N) then
4400 Propagate_Coextensions (N);
4401 end if;
4402 end if;
4403 end Resolve_Allocator;
4404
4405 ---------------------------
4406 -- Resolve_Arithmetic_Op --
4407 ---------------------------
4408
4409 -- Used for resolving all arithmetic operators except exponentiation
4410
4411 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4412 L : constant Node_Id := Left_Opnd (N);
4413 R : constant Node_Id := Right_Opnd (N);
4414 TL : constant Entity_Id := Base_Type (Etype (L));
4415 TR : constant Entity_Id := Base_Type (Etype (R));
4416 T : Entity_Id;
4417 Rop : Node_Id;
4418
4419 B_Typ : constant Entity_Id := Base_Type (Typ);
4420 -- We do the resolution using the base type, because intermediate values
4421 -- in expressions always are of the base type, not a subtype of it.
4422
4423 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4424 -- Returns True if N is in a context that expects "any real type"
4425
4426 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4427 -- Return True iff given type is Integer or universal real/integer
4428
4429 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4430 -- Choose type of integer literal in fixed-point operation to conform
4431 -- to available fixed-point type. T is the type of the other operand,
4432 -- which is needed to determine the expected type of N.
4433
4434 procedure Set_Operand_Type (N : Node_Id);
4435 -- Set operand type to T if universal
4436
4437 -------------------------------
4438 -- Expected_Type_Is_Any_Real --
4439 -------------------------------
4440
4441 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4442 begin
4443 -- N is the expression after "delta" in a fixed_point_definition;
4444 -- see RM-3.5.9(6):
4445
4446 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4447 N_Decimal_Fixed_Point_Definition,
4448
4449 -- N is one of the bounds in a real_range_specification;
4450 -- see RM-3.5.7(5):
4451
4452 N_Real_Range_Specification,
4453
4454 -- N is the expression of a delta_constraint;
4455 -- see RM-J.3(3):
4456
4457 N_Delta_Constraint);
4458 end Expected_Type_Is_Any_Real;
4459
4460 -----------------------------
4461 -- Is_Integer_Or_Universal --
4462 -----------------------------
4463
4464 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4465 T : Entity_Id;
4466 Index : Interp_Index;
4467 It : Interp;
4468
4469 begin
4470 if not Is_Overloaded (N) then
4471 T := Etype (N);
4472 return Base_Type (T) = Base_Type (Standard_Integer)
4473 or else T = Universal_Integer
4474 or else T = Universal_Real;
4475 else
4476 Get_First_Interp (N, Index, It);
4477 while Present (It.Typ) loop
4478 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4479 or else It.Typ = Universal_Integer
4480 or else It.Typ = Universal_Real
4481 then
4482 return True;
4483 end if;
4484
4485 Get_Next_Interp (Index, It);
4486 end loop;
4487 end if;
4488
4489 return False;
4490 end Is_Integer_Or_Universal;
4491
4492 ----------------------------
4493 -- Set_Mixed_Mode_Operand --
4494 ----------------------------
4495
4496 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4497 Index : Interp_Index;
4498 It : Interp;
4499
4500 begin
4501 if Universal_Interpretation (N) = Universal_Integer then
4502
4503 -- A universal integer literal is resolved as standard integer
4504 -- except in the case of a fixed-point result, where we leave it
4505 -- as universal (to be handled by Exp_Fixd later on)
4506
4507 if Is_Fixed_Point_Type (T) then
4508 Resolve (N, Universal_Integer);
4509 else
4510 Resolve (N, Standard_Integer);
4511 end if;
4512
4513 elsif Universal_Interpretation (N) = Universal_Real
4514 and then (T = Base_Type (Standard_Integer)
4515 or else T = Universal_Integer
4516 or else T = Universal_Real)
4517 then
4518 -- A universal real can appear in a fixed-type context. We resolve
4519 -- the literal with that context, even though this might raise an
4520 -- exception prematurely (the other operand may be zero).
4521
4522 Resolve (N, B_Typ);
4523
4524 elsif Etype (N) = Base_Type (Standard_Integer)
4525 and then T = Universal_Real
4526 and then Is_Overloaded (N)
4527 then
4528 -- Integer arg in mixed-mode operation. Resolve with universal
4529 -- type, in case preference rule must be applied.
4530
4531 Resolve (N, Universal_Integer);
4532
4533 elsif Etype (N) = T
4534 and then B_Typ /= Universal_Fixed
4535 then
4536 -- Not a mixed-mode operation, resolve with context
4537
4538 Resolve (N, B_Typ);
4539
4540 elsif Etype (N) = Any_Fixed then
4541
4542 -- N may itself be a mixed-mode operation, so use context type
4543
4544 Resolve (N, B_Typ);
4545
4546 elsif Is_Fixed_Point_Type (T)
4547 and then B_Typ = Universal_Fixed
4548 and then Is_Overloaded (N)
4549 then
4550 -- Must be (fixed * fixed) operation, operand must have one
4551 -- compatible interpretation.
4552
4553 Resolve (N, Any_Fixed);
4554
4555 elsif Is_Fixed_Point_Type (B_Typ)
4556 and then (T = Universal_Real
4557 or else Is_Fixed_Point_Type (T))
4558 and then Is_Overloaded (N)
4559 then
4560 -- C * F(X) in a fixed context, where C is a real literal or a
4561 -- fixed-point expression. F must have either a fixed type
4562 -- interpretation or an integer interpretation, but not both.
4563
4564 Get_First_Interp (N, Index, It);
4565 while Present (It.Typ) loop
4566 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4567
4568 if Analyzed (N) then
4569 Error_Msg_N ("ambiguous operand in fixed operation", N);
4570 else
4571 Resolve (N, Standard_Integer);
4572 end if;
4573
4574 elsif Is_Fixed_Point_Type (It.Typ) then
4575
4576 if Analyzed (N) then
4577 Error_Msg_N ("ambiguous operand in fixed operation", N);
4578 else
4579 Resolve (N, It.Typ);
4580 end if;
4581 end if;
4582
4583 Get_Next_Interp (Index, It);
4584 end loop;
4585
4586 -- Reanalyze the literal with the fixed type of the context. If
4587 -- context is Universal_Fixed, we are within a conversion, leave
4588 -- the literal as a universal real because there is no usable
4589 -- fixed type, and the target of the conversion plays no role in
4590 -- the resolution.
4591
4592 declare
4593 Op2 : Node_Id;
4594 T2 : Entity_Id;
4595
4596 begin
4597 if N = L then
4598 Op2 := R;
4599 else
4600 Op2 := L;
4601 end if;
4602
4603 if B_Typ = Universal_Fixed
4604 and then Nkind (Op2) = N_Real_Literal
4605 then
4606 T2 := Universal_Real;
4607 else
4608 T2 := B_Typ;
4609 end if;
4610
4611 Set_Analyzed (Op2, False);
4612 Resolve (Op2, T2);
4613 end;
4614
4615 else
4616 Resolve (N);
4617 end if;
4618 end Set_Mixed_Mode_Operand;
4619
4620 ----------------------
4621 -- Set_Operand_Type --
4622 ----------------------
4623
4624 procedure Set_Operand_Type (N : Node_Id) is
4625 begin
4626 if Etype (N) = Universal_Integer
4627 or else Etype (N) = Universal_Real
4628 then
4629 Set_Etype (N, T);
4630 end if;
4631 end Set_Operand_Type;
4632
4633 -- Start of processing for Resolve_Arithmetic_Op
4634
4635 begin
4636 if Comes_From_Source (N)
4637 and then Ekind (Entity (N)) = E_Function
4638 and then Is_Imported (Entity (N))
4639 and then Is_Intrinsic_Subprogram (Entity (N))
4640 then
4641 Resolve_Intrinsic_Operator (N, Typ);
4642 return;
4643
4644 -- Special-case for mixed-mode universal expressions or fixed point
4645 -- type operation: each argument is resolved separately. The same
4646 -- treatment is required if one of the operands of a fixed point
4647 -- operation is universal real, since in this case we don't do a
4648 -- conversion to a specific fixed-point type (instead the expander
4649 -- takes care of the case).
4650
4651 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4652 and then Present (Universal_Interpretation (L))
4653 and then Present (Universal_Interpretation (R))
4654 then
4655 Resolve (L, Universal_Interpretation (L));
4656 Resolve (R, Universal_Interpretation (R));
4657 Set_Etype (N, B_Typ);
4658
4659 elsif (B_Typ = Universal_Real
4660 or else Etype (N) = Universal_Fixed
4661 or else (Etype (N) = Any_Fixed
4662 and then Is_Fixed_Point_Type (B_Typ))
4663 or else (Is_Fixed_Point_Type (B_Typ)
4664 and then (Is_Integer_Or_Universal (L)
4665 or else
4666 Is_Integer_Or_Universal (R))))
4667 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4668 then
4669 if TL = Universal_Integer or else TR = Universal_Integer then
4670 Check_For_Visible_Operator (N, B_Typ);
4671 end if;
4672
4673 -- If context is a fixed type and one operand is integer, the
4674 -- other is resolved with the type of the context.
4675
4676 if Is_Fixed_Point_Type (B_Typ)
4677 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4678 or else TL = Universal_Integer)
4679 then
4680 Resolve (R, B_Typ);
4681 Resolve (L, TL);
4682
4683 elsif Is_Fixed_Point_Type (B_Typ)
4684 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4685 or else TR = Universal_Integer)
4686 then
4687 Resolve (L, B_Typ);
4688 Resolve (R, TR);
4689
4690 else
4691 Set_Mixed_Mode_Operand (L, TR);
4692 Set_Mixed_Mode_Operand (R, TL);
4693 end if;
4694
4695 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4696 -- multiplying operators from being used when the expected type is
4697 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4698 -- some cases where the expected type is actually Any_Real;
4699 -- Expected_Type_Is_Any_Real takes care of that case.
4700
4701 if Etype (N) = Universal_Fixed
4702 or else Etype (N) = Any_Fixed
4703 then
4704 if B_Typ = Universal_Fixed
4705 and then not Expected_Type_Is_Any_Real (N)
4706 and then not Nkind_In (Parent (N), N_Type_Conversion,
4707 N_Unchecked_Type_Conversion)
4708 then
4709 Error_Msg_N ("type cannot be determined from context!", N);
4710 Error_Msg_N ("\explicit conversion to result type required", N);
4711
4712 Set_Etype (L, Any_Type);
4713 Set_Etype (R, Any_Type);
4714
4715 else
4716 if Ada_Version = Ada_83
4717 and then Etype (N) = Universal_Fixed
4718 and then not
4719 Nkind_In (Parent (N), N_Type_Conversion,
4720 N_Unchecked_Type_Conversion)
4721 then
4722 Error_Msg_N
4723 ("(Ada 83) fixed-point operation "
4724 & "needs explicit conversion", N);
4725 end if;
4726
4727 -- The expected type is "any real type" in contexts like
4728 -- type T is delta <universal_fixed-expression> ...
4729 -- in which case we need to set the type to Universal_Real
4730 -- so that static expression evaluation will work properly.
4731
4732 if Expected_Type_Is_Any_Real (N) then
4733 Set_Etype (N, Universal_Real);
4734 else
4735 Set_Etype (N, B_Typ);
4736 end if;
4737 end if;
4738
4739 elsif Is_Fixed_Point_Type (B_Typ)
4740 and then (Is_Integer_Or_Universal (L)
4741 or else Nkind (L) = N_Real_Literal
4742 or else Nkind (R) = N_Real_Literal
4743 or else Is_Integer_Or_Universal (R))
4744 then
4745 Set_Etype (N, B_Typ);
4746
4747 elsif Etype (N) = Any_Fixed then
4748
4749 -- If no previous errors, this is only possible if one operand
4750 -- is overloaded and the context is universal. Resolve as such.
4751
4752 Set_Etype (N, B_Typ);
4753 end if;
4754
4755 else
4756 if (TL = Universal_Integer or else TL = Universal_Real)
4757 and then
4758 (TR = Universal_Integer or else TR = Universal_Real)
4759 then
4760 Check_For_Visible_Operator (N, B_Typ);
4761 end if;
4762
4763 -- If the context is Universal_Fixed and the operands are also
4764 -- universal fixed, this is an error, unless there is only one
4765 -- applicable fixed_point type (usually Duration).
4766
4767 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4768 T := Unique_Fixed_Point_Type (N);
4769
4770 if T = Any_Type then
4771 Set_Etype (N, T);
4772 return;
4773 else
4774 Resolve (L, T);
4775 Resolve (R, T);
4776 end if;
4777
4778 else
4779 Resolve (L, B_Typ);
4780 Resolve (R, B_Typ);
4781 end if;
4782
4783 -- If one of the arguments was resolved to a non-universal type.
4784 -- label the result of the operation itself with the same type.
4785 -- Do the same for the universal argument, if any.
4786
4787 T := Intersect_Types (L, R);
4788 Set_Etype (N, Base_Type (T));
4789 Set_Operand_Type (L);
4790 Set_Operand_Type (R);
4791 end if;
4792
4793 Generate_Operator_Reference (N, Typ);
4794 Eval_Arithmetic_Op (N);
4795
4796 -- Set overflow and division checking bit. Much cleverer code needed
4797 -- here eventually and perhaps the Resolve routines should be separated
4798 -- for the various arithmetic operations, since they will need
4799 -- different processing. ???
4800
4801 if Nkind (N) in N_Op then
4802 if not Overflow_Checks_Suppressed (Etype (N)) then
4803 Enable_Overflow_Check (N);
4804 end if;
4805
4806 -- Give warning if explicit division by zero
4807
4808 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4809 and then not Division_Checks_Suppressed (Etype (N))
4810 then
4811 Rop := Right_Opnd (N);
4812
4813 if Compile_Time_Known_Value (Rop)
4814 and then ((Is_Integer_Type (Etype (Rop))
4815 and then Expr_Value (Rop) = Uint_0)
4816 or else
4817 (Is_Real_Type (Etype (Rop))
4818 and then Expr_Value_R (Rop) = Ureal_0))
4819 then
4820 -- Specialize the warning message according to the operation
4821
4822 case Nkind (N) is
4823 when N_Op_Divide =>
4824 Apply_Compile_Time_Constraint_Error
4825 (N, "division by zero?", CE_Divide_By_Zero,
4826 Loc => Sloc (Right_Opnd (N)));
4827
4828 when N_Op_Rem =>
4829 Apply_Compile_Time_Constraint_Error
4830 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4831 Loc => Sloc (Right_Opnd (N)));
4832
4833 when N_Op_Mod =>
4834 Apply_Compile_Time_Constraint_Error
4835 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4836 Loc => Sloc (Right_Opnd (N)));
4837
4838 -- Division by zero can only happen with division, rem,
4839 -- and mod operations.
4840
4841 when others =>
4842 raise Program_Error;
4843 end case;
4844
4845 -- Otherwise just set the flag to check at run time
4846
4847 else
4848 Activate_Division_Check (N);
4849 end if;
4850 end if;
4851
4852 -- If Restriction No_Implicit_Conditionals is active, then it is
4853 -- violated if either operand can be negative for mod, or for rem
4854 -- if both operands can be negative.
4855
4856 if Restriction_Check_Required (No_Implicit_Conditionals)
4857 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4858 then
4859 declare
4860 Lo : Uint;
4861 Hi : Uint;
4862 OK : Boolean;
4863
4864 LNeg : Boolean;
4865 RNeg : Boolean;
4866 -- Set if corresponding operand might be negative
4867
4868 begin
4869 Determine_Range
4870 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4871 LNeg := (not OK) or else Lo < 0;
4872
4873 Determine_Range
4874 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4875 RNeg := (not OK) or else Lo < 0;
4876
4877 -- Check if we will be generating conditionals. There are two
4878 -- cases where that can happen, first for REM, the only case
4879 -- is largest negative integer mod -1, where the division can
4880 -- overflow, but we still have to give the right result. The
4881 -- front end generates a test for this annoying case. Here we
4882 -- just test if both operands can be negative (that's what the
4883 -- expander does, so we match its logic here).
4884
4885 -- The second case is mod where either operand can be negative.
4886 -- In this case, the back end has to generate additonal tests.
4887
4888 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4889 or else
4890 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4891 then
4892 Check_Restriction (No_Implicit_Conditionals, N);
4893 end if;
4894 end;
4895 end if;
4896 end if;
4897
4898 Check_Unset_Reference (L);
4899 Check_Unset_Reference (R);
4900 end Resolve_Arithmetic_Op;
4901
4902 ------------------
4903 -- Resolve_Call --
4904 ------------------
4905
4906 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4907 Loc : constant Source_Ptr := Sloc (N);
4908 Subp : constant Node_Id := Name (N);
4909 Nam : Entity_Id;
4910 I : Interp_Index;
4911 It : Interp;
4912 Norm_OK : Boolean;
4913 Scop : Entity_Id;
4914 Rtype : Entity_Id;
4915
4916 function Same_Or_Aliased_Subprograms
4917 (S : Entity_Id;
4918 E : Entity_Id) return Boolean;
4919 -- Returns True if the subprogram entity S is the same as E or else
4920 -- S is an alias of E.
4921
4922 ---------------------------------
4923 -- Same_Or_Aliased_Subprograms --
4924 ---------------------------------
4925
4926 function Same_Or_Aliased_Subprograms
4927 (S : Entity_Id;
4928 E : Entity_Id) return Boolean
4929 is
4930 Subp_Alias : constant Entity_Id := Alias (S);
4931 begin
4932 return S = E
4933 or else (Present (Subp_Alias) and then Subp_Alias = E);
4934 end Same_Or_Aliased_Subprograms;
4935
4936 -- Start of processing for Resolve_Call
4937
4938 begin
4939 -- The context imposes a unique interpretation with type Typ on a
4940 -- procedure or function call. Find the entity of the subprogram that
4941 -- yields the expected type, and propagate the corresponding formal
4942 -- constraints on the actuals. The caller has established that an
4943 -- interpretation exists, and emitted an error if not unique.
4944
4945 -- First deal with the case of a call to an access-to-subprogram,
4946 -- dereference made explicit in Analyze_Call.
4947
4948 if Ekind (Etype (Subp)) = E_Subprogram_Type then
4949 if not Is_Overloaded (Subp) then
4950 Nam := Etype (Subp);
4951
4952 else
4953 -- Find the interpretation whose type (a subprogram type) has a
4954 -- return type that is compatible with the context. Analysis of
4955 -- the node has established that one exists.
4956
4957 Nam := Empty;
4958
4959 Get_First_Interp (Subp, I, It);
4960 while Present (It.Typ) loop
4961 if Covers (Typ, Etype (It.Typ)) then
4962 Nam := It.Typ;
4963 exit;
4964 end if;
4965
4966 Get_Next_Interp (I, It);
4967 end loop;
4968
4969 if No (Nam) then
4970 raise Program_Error;
4971 end if;
4972 end if;
4973
4974 -- If the prefix is not an entity, then resolve it
4975
4976 if not Is_Entity_Name (Subp) then
4977 Resolve (Subp, Nam);
4978 end if;
4979
4980 -- For an indirect call, we always invalidate checks, since we do not
4981 -- know whether the subprogram is local or global. Yes we could do
4982 -- better here, e.g. by knowing that there are no local subprograms,
4983 -- but it does not seem worth the effort. Similarly, we kill all
4984 -- knowledge of current constant values.
4985
4986 Kill_Current_Values;
4987
4988 -- If this is a procedure call which is really an entry call, do
4989 -- the conversion of the procedure call to an entry call. Protected
4990 -- operations use the same circuitry because the name in the call
4991 -- can be an arbitrary expression with special resolution rules.
4992
4993 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
4994 or else (Is_Entity_Name (Subp)
4995 and then Ekind (Entity (Subp)) = E_Entry)
4996 then
4997 Resolve_Entry_Call (N, Typ);
4998 Check_Elab_Call (N);
4999
5000 -- Kill checks and constant values, as above for indirect case
5001 -- Who knows what happens when another task is activated?
5002
5003 Kill_Current_Values;
5004 return;
5005
5006 -- Normal subprogram call with name established in Resolve
5007
5008 elsif not (Is_Type (Entity (Subp))) then
5009 Nam := Entity (Subp);
5010 Set_Entity_With_Style_Check (Subp, Nam);
5011
5012 -- Otherwise we must have the case of an overloaded call
5013
5014 else
5015 pragma Assert (Is_Overloaded (Subp));
5016
5017 -- Initialize Nam to prevent warning (we know it will be assigned
5018 -- in the loop below, but the compiler does not know that).
5019
5020 Nam := Empty;
5021
5022 Get_First_Interp (Subp, I, It);
5023 while Present (It.Typ) loop
5024 if Covers (Typ, It.Typ) then
5025 Nam := It.Nam;
5026 Set_Entity_With_Style_Check (Subp, Nam);
5027 exit;
5028 end if;
5029
5030 Get_Next_Interp (I, It);
5031 end loop;
5032 end if;
5033
5034 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5035 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5036 and then Nkind (Subp) /= N_Explicit_Dereference
5037 and then Present (Parameter_Associations (N))
5038 then
5039 -- The prefix is a parameterless function call that returns an access
5040 -- to subprogram. If parameters are present in the current call, add
5041 -- add an explicit dereference. We use the base type here because
5042 -- within an instance these may be subtypes.
5043
5044 -- The dereference is added either in Analyze_Call or here. Should
5045 -- be consolidated ???
5046
5047 Set_Is_Overloaded (Subp, False);
5048 Set_Etype (Subp, Etype (Nam));
5049 Insert_Explicit_Dereference (Subp);
5050 Nam := Designated_Type (Etype (Nam));
5051 Resolve (Subp, Nam);
5052 end if;
5053
5054 -- Check that a call to Current_Task does not occur in an entry body
5055
5056 if Is_RTE (Nam, RE_Current_Task) then
5057 declare
5058 P : Node_Id;
5059
5060 begin
5061 P := N;
5062 loop
5063 P := Parent (P);
5064
5065 -- Exclude calls that occur within the default of a formal
5066 -- parameter of the entry, since those are evaluated outside
5067 -- of the body.
5068
5069 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5070
5071 if Nkind (P) = N_Entry_Body
5072 or else (Nkind (P) = N_Subprogram_Body
5073 and then Is_Entry_Barrier_Function (P))
5074 then
5075 Rtype := Etype (N);
5076 Error_Msg_NE
5077 ("?& should not be used in entry body (RM C.7(17))",
5078 N, Nam);
5079 Error_Msg_NE
5080 ("\Program_Error will be raised at run time?", N, Nam);
5081 Rewrite (N,
5082 Make_Raise_Program_Error (Loc,
5083 Reason => PE_Current_Task_In_Entry_Body));
5084 Set_Etype (N, Rtype);
5085 return;
5086 end if;
5087 end loop;
5088 end;
5089 end if;
5090
5091 -- Check that a procedure call does not occur in the context of the
5092 -- entry call statement of a conditional or timed entry call. Note that
5093 -- the case of a call to a subprogram renaming of an entry will also be
5094 -- rejected. The test for N not being an N_Entry_Call_Statement is
5095 -- defensive, covering the possibility that the processing of entry
5096 -- calls might reach this point due to later modifications of the code
5097 -- above.
5098
5099 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5100 and then Nkind (N) /= N_Entry_Call_Statement
5101 and then Entry_Call_Statement (Parent (N)) = N
5102 then
5103 if Ada_Version < Ada_2005 then
5104 Error_Msg_N ("entry call required in select statement", N);
5105
5106 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5107 -- for a procedure_or_entry_call, the procedure_name or
5108 -- procedure_prefix of the procedure_call_statement shall denote
5109 -- an entry renamed by a procedure, or (a view of) a primitive
5110 -- subprogram of a limited interface whose first parameter is
5111 -- a controlling parameter.
5112
5113 elsif Nkind (N) = N_Procedure_Call_Statement
5114 and then not Is_Renamed_Entry (Nam)
5115 and then not Is_Controlling_Limited_Procedure (Nam)
5116 then
5117 Error_Msg_N
5118 ("entry call or dispatching primitive of interface required", N);
5119 end if;
5120 end if;
5121
5122 -- Check that this is not a call to a protected procedure or entry from
5123 -- within a protected function.
5124
5125 if Ekind (Current_Scope) = E_Function
5126 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5127 and then Ekind (Nam) /= E_Function
5128 and then Scope (Nam) = Scope (Current_Scope)
5129 then
5130 Error_Msg_N ("within protected function, protected " &
5131 "object is constant", N);
5132 Error_Msg_N ("\cannot call operation that may modify it", N);
5133 end if;
5134
5135 -- Freeze the subprogram name if not in a spec-expression. Note that we
5136 -- freeze procedure calls as well as function calls. Procedure calls are
5137 -- not frozen according to the rules (RM 13.14(14)) because it is
5138 -- impossible to have a procedure call to a non-frozen procedure in pure
5139 -- Ada, but in the code that we generate in the expander, this rule
5140 -- needs extending because we can generate procedure calls that need
5141 -- freezing.
5142
5143 if Is_Entity_Name (Subp) and then not In_Spec_Expression then
5144 Freeze_Expression (Subp);
5145 end if;
5146
5147 -- For a predefined operator, the type of the result is the type imposed
5148 -- by context, except for a predefined operation on universal fixed.
5149 -- Otherwise The type of the call is the type returned by the subprogram
5150 -- being called.
5151
5152 if Is_Predefined_Op (Nam) then
5153 if Etype (N) /= Universal_Fixed then
5154 Set_Etype (N, Typ);
5155 end if;
5156
5157 -- If the subprogram returns an array type, and the context requires the
5158 -- component type of that array type, the node is really an indexing of
5159 -- the parameterless call. Resolve as such. A pathological case occurs
5160 -- when the type of the component is an access to the array type. In
5161 -- this case the call is truly ambiguous.
5162
5163 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5164 and then
5165 ((Is_Array_Type (Etype (Nam))
5166 and then Covers (Typ, Component_Type (Etype (Nam))))
5167 or else (Is_Access_Type (Etype (Nam))
5168 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5169 and then
5170 Covers (Typ,
5171 Component_Type (Designated_Type (Etype (Nam))))))
5172 then
5173 declare
5174 Index_Node : Node_Id;
5175 New_Subp : Node_Id;
5176 Ret_Type : constant Entity_Id := Etype (Nam);
5177
5178 begin
5179 if Is_Access_Type (Ret_Type)
5180 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5181 then
5182 Error_Msg_N
5183 ("cannot disambiguate function call and indexing", N);
5184 else
5185 New_Subp := Relocate_Node (Subp);
5186 Set_Entity (Subp, Nam);
5187
5188 if (Is_Array_Type (Ret_Type)
5189 and then Component_Type (Ret_Type) /= Any_Type)
5190 or else
5191 (Is_Access_Type (Ret_Type)
5192 and then
5193 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5194 then
5195 if Needs_No_Actuals (Nam) then
5196
5197 -- Indexed call to a parameterless function
5198
5199 Index_Node :=
5200 Make_Indexed_Component (Loc,
5201 Prefix =>
5202 Make_Function_Call (Loc,
5203 Name => New_Subp),
5204 Expressions => Parameter_Associations (N));
5205 else
5206 -- An Ada 2005 prefixed call to a primitive operation
5207 -- whose first parameter is the prefix. This prefix was
5208 -- prepended to the parameter list, which is actually a
5209 -- list of indexes. Remove the prefix in order to build
5210 -- the proper indexed component.
5211
5212 Index_Node :=
5213 Make_Indexed_Component (Loc,
5214 Prefix =>
5215 Make_Function_Call (Loc,
5216 Name => New_Subp,
5217 Parameter_Associations =>
5218 New_List
5219 (Remove_Head (Parameter_Associations (N)))),
5220 Expressions => Parameter_Associations (N));
5221 end if;
5222
5223 -- Preserve the parenthesis count of the node
5224
5225 Set_Paren_Count (Index_Node, Paren_Count (N));
5226
5227 -- Since we are correcting a node classification error made
5228 -- by the parser, we call Replace rather than Rewrite.
5229
5230 Replace (N, Index_Node);
5231
5232 Set_Etype (Prefix (N), Ret_Type);
5233 Set_Etype (N, Typ);
5234 Resolve_Indexed_Component (N, Typ);
5235 Check_Elab_Call (Prefix (N));
5236 end if;
5237 end if;
5238
5239 return;
5240 end;
5241
5242 else
5243 Set_Etype (N, Etype (Nam));
5244 end if;
5245
5246 -- In the case where the call is to an overloaded subprogram, Analyze
5247 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5248 -- such a case Normalize_Actuals needs to be called once more to order
5249 -- the actuals correctly. Otherwise the call will have the ordering
5250 -- given by the last overloaded subprogram whether this is the correct
5251 -- one being called or not.
5252
5253 if Is_Overloaded (Subp) then
5254 Normalize_Actuals (N, Nam, False, Norm_OK);
5255 pragma Assert (Norm_OK);
5256 end if;
5257
5258 -- In any case, call is fully resolved now. Reset Overload flag, to
5259 -- prevent subsequent overload resolution if node is analyzed again
5260
5261 Set_Is_Overloaded (Subp, False);
5262 Set_Is_Overloaded (N, False);
5263
5264 -- If we are calling the current subprogram from immediately within its
5265 -- body, then that is the case where we can sometimes detect cases of
5266 -- infinite recursion statically. Do not try this in case restriction
5267 -- No_Recursion is in effect anyway, and do it only for source calls.
5268
5269 if Comes_From_Source (N) then
5270 Scop := Current_Scope;
5271
5272 -- Issue warning for possible infinite recursion in the absence
5273 -- of the No_Recursion restriction.
5274
5275 if Same_Or_Aliased_Subprograms (Nam, Scop)
5276 and then not Restriction_Active (No_Recursion)
5277 and then Check_Infinite_Recursion (N)
5278 then
5279 -- Here we detected and flagged an infinite recursion, so we do
5280 -- not need to test the case below for further warnings. Also if
5281 -- we now have a raise SE node, we are all done.
5282
5283 if Nkind (N) = N_Raise_Storage_Error then
5284 return;
5285 end if;
5286
5287 -- If call is to immediately containing subprogram, then check for
5288 -- the case of a possible run-time detectable infinite recursion.
5289
5290 else
5291 Scope_Loop : while Scop /= Standard_Standard loop
5292 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5293
5294 -- Although in general case, recursion is not statically
5295 -- checkable, the case of calling an immediately containing
5296 -- subprogram is easy to catch.
5297
5298 Check_Restriction (No_Recursion, N);
5299
5300 -- If the recursive call is to a parameterless subprogram,
5301 -- then even if we can't statically detect infinite
5302 -- recursion, this is pretty suspicious, and we output a
5303 -- warning. Furthermore, we will try later to detect some
5304 -- cases here at run time by expanding checking code (see
5305 -- Detect_Infinite_Recursion in package Exp_Ch6).
5306
5307 -- If the recursive call is within a handler, do not emit a
5308 -- warning, because this is a common idiom: loop until input
5309 -- is correct, catch illegal input in handler and restart.
5310
5311 if No (First_Formal (Nam))
5312 and then Etype (Nam) = Standard_Void_Type
5313 and then not Error_Posted (N)
5314 and then Nkind (Parent (N)) /= N_Exception_Handler
5315 then
5316 -- For the case of a procedure call. We give the message
5317 -- only if the call is the first statement in a sequence
5318 -- of statements, or if all previous statements are
5319 -- simple assignments. This is simply a heuristic to
5320 -- decrease false positives, without losing too many good
5321 -- warnings. The idea is that these previous statements
5322 -- may affect global variables the procedure depends on.
5323
5324 if Nkind (N) = N_Procedure_Call_Statement
5325 and then Is_List_Member (N)
5326 then
5327 declare
5328 P : Node_Id;
5329 begin
5330 P := Prev (N);
5331 while Present (P) loop
5332 if Nkind (P) /= N_Assignment_Statement then
5333 exit Scope_Loop;
5334 end if;
5335
5336 Prev (P);
5337 end loop;
5338 end;
5339 end if;
5340
5341 -- Do not give warning if we are in a conditional context
5342
5343 declare
5344 K : constant Node_Kind := Nkind (Parent (N));
5345 begin
5346 if (K = N_Loop_Statement
5347 and then Present (Iteration_Scheme (Parent (N))))
5348 or else K = N_If_Statement
5349 or else K = N_Elsif_Part
5350 or else K = N_Case_Statement_Alternative
5351 then
5352 exit Scope_Loop;
5353 end if;
5354 end;
5355
5356 -- Here warning is to be issued
5357
5358 Set_Has_Recursive_Call (Nam);
5359 Error_Msg_N
5360 ("?possible infinite recursion!", N);
5361 Error_Msg_N
5362 ("\?Storage_Error may be raised at run time!", N);
5363 end if;
5364
5365 exit Scope_Loop;
5366 end if;
5367
5368 Scop := Scope (Scop);
5369 end loop Scope_Loop;
5370 end if;
5371 end if;
5372
5373 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5374
5375 Check_Obsolescent_2005_Entity (Nam, Subp);
5376
5377 -- If subprogram name is a predefined operator, it was given in
5378 -- functional notation. Replace call node with operator node, so
5379 -- that actuals can be resolved appropriately.
5380
5381 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5382 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5383 return;
5384
5385 elsif Present (Alias (Nam))
5386 and then Is_Predefined_Op (Alias (Nam))
5387 then
5388 Resolve_Actuals (N, Nam);
5389 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5390 return;
5391 end if;
5392
5393 -- Create a transient scope if the resulting type requires it
5394
5395 -- There are several notable exceptions:
5396
5397 -- a) In init procs, the transient scope overhead is not needed, and is
5398 -- even incorrect when the call is a nested initialization call for a
5399 -- component whose expansion may generate adjust calls. However, if the
5400 -- call is some other procedure call within an initialization procedure
5401 -- (for example a call to Create_Task in the init_proc of the task
5402 -- run-time record) a transient scope must be created around this call.
5403
5404 -- b) Enumeration literal pseudo-calls need no transient scope
5405
5406 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5407 -- functions) do not use the secondary stack even though the return
5408 -- type may be unconstrained.
5409
5410 -- d) Calls to a build-in-place function, since such functions may
5411 -- allocate their result directly in a target object, and cases where
5412 -- the result does get allocated in the secondary stack are checked for
5413 -- within the specialized Exp_Ch6 procedures for expanding those
5414 -- build-in-place calls.
5415
5416 -- e) If the subprogram is marked Inline_Always, then even if it returns
5417 -- an unconstrained type the call does not require use of the secondary
5418 -- stack. However, inlining will only take place if the body to inline
5419 -- is already present. It may not be available if e.g. the subprogram is
5420 -- declared in a child instance.
5421
5422 -- If this is an initialization call for a type whose construction
5423 -- uses the secondary stack, and it is not a nested call to initialize
5424 -- a component, we do need to create a transient scope for it. We
5425 -- check for this by traversing the type in Check_Initialization_Call.
5426
5427 if Is_Inlined (Nam)
5428 and then Has_Pragma_Inline_Always (Nam)
5429 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5430 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5431 then
5432 null;
5433
5434 elsif Ekind (Nam) = E_Enumeration_Literal
5435 or else Is_Build_In_Place_Function (Nam)
5436 or else Is_Intrinsic_Subprogram (Nam)
5437 then
5438 null;
5439
5440 elsif Expander_Active
5441 and then Is_Type (Etype (Nam))
5442 and then Requires_Transient_Scope (Etype (Nam))
5443 and then
5444 (not Within_Init_Proc
5445 or else
5446 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5447 then
5448 Establish_Transient_Scope (N, Sec_Stack => True);
5449
5450 -- If the call appears within the bounds of a loop, it will
5451 -- be rewritten and reanalyzed, nothing left to do here.
5452
5453 if Nkind (N) /= N_Function_Call then
5454 return;
5455 end if;
5456
5457 elsif Is_Init_Proc (Nam)
5458 and then not Within_Init_Proc
5459 then
5460 Check_Initialization_Call (N, Nam);
5461 end if;
5462
5463 -- A protected function cannot be called within the definition of the
5464 -- enclosing protected type.
5465
5466 if Is_Protected_Type (Scope (Nam))
5467 and then In_Open_Scopes (Scope (Nam))
5468 and then not Has_Completion (Scope (Nam))
5469 then
5470 Error_Msg_NE
5471 ("& cannot be called before end of protected definition", N, Nam);
5472 end if;
5473
5474 -- Propagate interpretation to actuals, and add default expressions
5475 -- where needed.
5476
5477 if Present (First_Formal (Nam)) then
5478 Resolve_Actuals (N, Nam);
5479
5480 -- Overloaded literals are rewritten as function calls, for purpose of
5481 -- resolution. After resolution, we can replace the call with the
5482 -- literal itself.
5483
5484 elsif Ekind (Nam) = E_Enumeration_Literal then
5485 Copy_Node (Subp, N);
5486 Resolve_Entity_Name (N, Typ);
5487
5488 -- Avoid validation, since it is a static function call
5489
5490 Generate_Reference (Nam, Subp);
5491 return;
5492 end if;
5493
5494 -- If the subprogram is not global, then kill all saved values and
5495 -- checks. This is a bit conservative, since in many cases we could do
5496 -- better, but it is not worth the effort. Similarly, we kill constant
5497 -- values. However we do not need to do this for internal entities
5498 -- (unless they are inherited user-defined subprograms), since they
5499 -- are not in the business of molesting local values.
5500
5501 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5502 -- kill all checks and values for calls to global subprograms. This
5503 -- takes care of the case where an access to a local subprogram is
5504 -- taken, and could be passed directly or indirectly and then called
5505 -- from almost any context.
5506
5507 -- Note: we do not do this step till after resolving the actuals. That
5508 -- way we still take advantage of the current value information while
5509 -- scanning the actuals.
5510
5511 -- We suppress killing values if we are processing the nodes associated
5512 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5513 -- type kills all the values as part of analyzing the code that
5514 -- initializes the dispatch tables.
5515
5516 if Inside_Freezing_Actions = 0
5517 and then (not Is_Library_Level_Entity (Nam)
5518 or else Suppress_Value_Tracking_On_Call
5519 (Nearest_Dynamic_Scope (Current_Scope)))
5520 and then (Comes_From_Source (Nam)
5521 or else (Present (Alias (Nam))
5522 and then Comes_From_Source (Alias (Nam))))
5523 then
5524 Kill_Current_Values;
5525 end if;
5526
5527 -- If we are warning about unread OUT parameters, this is the place to
5528 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5529 -- after the above call to Kill_Current_Values (since that call clears
5530 -- the Last_Assignment field of all local variables).
5531
5532 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5533 and then Comes_From_Source (N)
5534 and then In_Extended_Main_Source_Unit (N)
5535 then
5536 declare
5537 F : Entity_Id;
5538 A : Node_Id;
5539
5540 begin
5541 F := First_Formal (Nam);
5542 A := First_Actual (N);
5543 while Present (F) and then Present (A) loop
5544 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5545 and then Warn_On_Modified_As_Out_Parameter (F)
5546 and then Is_Entity_Name (A)
5547 and then Present (Entity (A))
5548 and then Comes_From_Source (N)
5549 and then Safe_To_Capture_Value (N, Entity (A))
5550 then
5551 Set_Last_Assignment (Entity (A), A);
5552 end if;
5553
5554 Next_Formal (F);
5555 Next_Actual (A);
5556 end loop;
5557 end;
5558 end if;
5559
5560 -- If the subprogram is a primitive operation, check whether or not
5561 -- it is a correct dispatching call.
5562
5563 if Is_Overloadable (Nam)
5564 and then Is_Dispatching_Operation (Nam)
5565 then
5566 Check_Dispatching_Call (N);
5567
5568 elsif Ekind (Nam) /= E_Subprogram_Type
5569 and then Is_Abstract_Subprogram (Nam)
5570 and then not In_Instance
5571 then
5572 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5573 end if;
5574
5575 -- If this is a dispatching call, generate the appropriate reference,
5576 -- for better source navigation in GPS.
5577
5578 if Is_Overloadable (Nam)
5579 and then Present (Controlling_Argument (N))
5580 then
5581 Generate_Reference (Nam, Subp, 'R');
5582
5583 -- Normal case, not a dispatching call. Generate a call reference.
5584
5585 else
5586 Generate_Reference (Nam, Subp, 's');
5587 end if;
5588
5589 if Is_Intrinsic_Subprogram (Nam) then
5590 Check_Intrinsic_Call (N);
5591 end if;
5592
5593 -- Check for violation of restriction No_Specific_Termination_Handlers
5594 -- and warn on a potentially blocking call to Abort_Task.
5595
5596 if Is_RTE (Nam, RE_Set_Specific_Handler)
5597 or else
5598 Is_RTE (Nam, RE_Specific_Handler)
5599 then
5600 Check_Restriction (No_Specific_Termination_Handlers, N);
5601
5602 elsif Is_RTE (Nam, RE_Abort_Task) then
5603 Check_Potentially_Blocking_Operation (N);
5604 end if;
5605
5606 -- A call to Ada.Real_Time.Timing_Events.Set_Handler violates
5607 -- restriction No_Relative_Delay (AI-0211).
5608
5609 if Is_RTE (Nam, RE_Set_Handler) then
5610 Check_Restriction (No_Relative_Delay, N);
5611 end if;
5612
5613 -- Issue an error for a call to an eliminated subprogram. We skip this
5614 -- in a spec expression, e.g. a call in a default parameter value, since
5615 -- we are not really doing a call at this time. That's important because
5616 -- the spec expression may itself belong to an eliminated subprogram.
5617
5618 if not In_Spec_Expression then
5619 Check_For_Eliminated_Subprogram (Subp, Nam);
5620 end if;
5621
5622 -- All done, evaluate call and deal with elaboration issues
5623
5624 Eval_Call (N);
5625 Check_Elab_Call (N);
5626 Warn_On_Overlapping_Actuals (Nam, N);
5627 end Resolve_Call;
5628
5629 -----------------------------
5630 -- Resolve_Case_Expression --
5631 -----------------------------
5632
5633 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5634 Alt : Node_Id;
5635
5636 begin
5637 Alt := First (Alternatives (N));
5638 while Present (Alt) loop
5639 Resolve (Expression (Alt), Typ);
5640 Next (Alt);
5641 end loop;
5642
5643 Set_Etype (N, Typ);
5644 Eval_Case_Expression (N);
5645 end Resolve_Case_Expression;
5646
5647 -------------------------------
5648 -- Resolve_Character_Literal --
5649 -------------------------------
5650
5651 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5652 B_Typ : constant Entity_Id := Base_Type (Typ);
5653 C : Entity_Id;
5654
5655 begin
5656 -- Verify that the character does belong to the type of the context
5657
5658 Set_Etype (N, B_Typ);
5659 Eval_Character_Literal (N);
5660
5661 -- Wide_Wide_Character literals must always be defined, since the set
5662 -- of wide wide character literals is complete, i.e. if a character
5663 -- literal is accepted by the parser, then it is OK for wide wide
5664 -- character (out of range character literals are rejected).
5665
5666 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5667 return;
5668
5669 -- Always accept character literal for type Any_Character, which
5670 -- occurs in error situations and in comparisons of literals, both
5671 -- of which should accept all literals.
5672
5673 elsif B_Typ = Any_Character then
5674 return;
5675
5676 -- For Standard.Character or a type derived from it, check that
5677 -- the literal is in range
5678
5679 elsif Root_Type (B_Typ) = Standard_Character then
5680 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5681 return;
5682 end if;
5683
5684 -- For Standard.Wide_Character or a type derived from it, check
5685 -- that the literal is in range
5686
5687 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5688 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5689 return;
5690 end if;
5691
5692 -- For Standard.Wide_Wide_Character or a type derived from it, we
5693 -- know the literal is in range, since the parser checked!
5694
5695 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5696 return;
5697
5698 -- If the entity is already set, this has already been resolved in a
5699 -- generic context, or comes from expansion. Nothing else to do.
5700
5701 elsif Present (Entity (N)) then
5702 return;
5703
5704 -- Otherwise we have a user defined character type, and we can use the
5705 -- standard visibility mechanisms to locate the referenced entity.
5706
5707 else
5708 C := Current_Entity (N);
5709 while Present (C) loop
5710 if Etype (C) = B_Typ then
5711 Set_Entity_With_Style_Check (N, C);
5712 Generate_Reference (C, N);
5713 return;
5714 end if;
5715
5716 C := Homonym (C);
5717 end loop;
5718 end if;
5719
5720 -- If we fall through, then the literal does not match any of the
5721 -- entries of the enumeration type. This isn't just a constraint
5722 -- error situation, it is an illegality (see RM 4.2).
5723
5724 Error_Msg_NE
5725 ("character not defined for }", N, First_Subtype (B_Typ));
5726 end Resolve_Character_Literal;
5727
5728 ---------------------------
5729 -- Resolve_Comparison_Op --
5730 ---------------------------
5731
5732 -- Context requires a boolean type, and plays no role in resolution.
5733 -- Processing identical to that for equality operators. The result
5734 -- type is the base type, which matters when pathological subtypes of
5735 -- booleans with limited ranges are used.
5736
5737 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5738 L : constant Node_Id := Left_Opnd (N);
5739 R : constant Node_Id := Right_Opnd (N);
5740 T : Entity_Id;
5741
5742 begin
5743 -- If this is an intrinsic operation which is not predefined, use the
5744 -- types of its declared arguments to resolve the possibly overloaded
5745 -- operands. Otherwise the operands are unambiguous and specify the
5746 -- expected type.
5747
5748 if Scope (Entity (N)) /= Standard_Standard then
5749 T := Etype (First_Entity (Entity (N)));
5750
5751 else
5752 T := Find_Unique_Type (L, R);
5753
5754 if T = Any_Fixed then
5755 T := Unique_Fixed_Point_Type (L);
5756 end if;
5757 end if;
5758
5759 Set_Etype (N, Base_Type (Typ));
5760 Generate_Reference (T, N, ' ');
5761
5762 -- Skip remaining processing if already set to Any_Type
5763
5764 if T = Any_Type then
5765 return;
5766 end if;
5767
5768 -- Deal with other error cases
5769
5770 if T = Any_String or else
5771 T = Any_Composite or else
5772 T = Any_Character
5773 then
5774 if T = Any_Character then
5775 Ambiguous_Character (L);
5776 else
5777 Error_Msg_N ("ambiguous operands for comparison", N);
5778 end if;
5779
5780 Set_Etype (N, Any_Type);
5781 return;
5782 end if;
5783
5784 -- Resolve the operands if types OK
5785
5786 Resolve (L, T);
5787 Resolve (R, T);
5788 Check_Unset_Reference (L);
5789 Check_Unset_Reference (R);
5790 Generate_Operator_Reference (N, T);
5791 Check_Low_Bound_Tested (N);
5792
5793 -- Check comparison on unordered enumeration
5794
5795 if Comes_From_Source (N)
5796 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
5797 then
5798 Error_Msg_N ("comparison on unordered enumeration type?", N);
5799 end if;
5800
5801 -- Evaluate the relation (note we do this after the above check
5802 -- since this Eval call may change N to True/False.
5803
5804 Eval_Relational_Op (N);
5805 end Resolve_Comparison_Op;
5806
5807 ------------------------------------
5808 -- Resolve_Conditional_Expression --
5809 ------------------------------------
5810
5811 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5812 Condition : constant Node_Id := First (Expressions (N));
5813 Then_Expr : constant Node_Id := Next (Condition);
5814 Else_Expr : Node_Id := Next (Then_Expr);
5815
5816 begin
5817 Resolve (Condition, Any_Boolean);
5818 Resolve (Then_Expr, Typ);
5819
5820 -- If ELSE expression present, just resolve using the determined type
5821
5822 if Present (Else_Expr) then
5823 Resolve (Else_Expr, Typ);
5824
5825 -- If no ELSE expression is present, root type must be Standard.Boolean
5826 -- and we provide a Standard.True result converted to the appropriate
5827 -- Boolean type (in case it is a derived boolean type).
5828
5829 elsif Root_Type (Typ) = Standard_Boolean then
5830 Else_Expr :=
5831 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
5832 Analyze_And_Resolve (Else_Expr, Typ);
5833 Append_To (Expressions (N), Else_Expr);
5834
5835 else
5836 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
5837 Append_To (Expressions (N), Error);
5838 end if;
5839
5840 Set_Etype (N, Typ);
5841 Eval_Conditional_Expression (N);
5842 end Resolve_Conditional_Expression;
5843
5844 -----------------------------------------
5845 -- Resolve_Discrete_Subtype_Indication --
5846 -----------------------------------------
5847
5848 procedure Resolve_Discrete_Subtype_Indication
5849 (N : Node_Id;
5850 Typ : Entity_Id)
5851 is
5852 R : Node_Id;
5853 S : Entity_Id;
5854
5855 begin
5856 Analyze (Subtype_Mark (N));
5857 S := Entity (Subtype_Mark (N));
5858
5859 if Nkind (Constraint (N)) /= N_Range_Constraint then
5860 Error_Msg_N ("expect range constraint for discrete type", N);
5861 Set_Etype (N, Any_Type);
5862
5863 else
5864 R := Range_Expression (Constraint (N));
5865
5866 if R = Error then
5867 return;
5868 end if;
5869
5870 Analyze (R);
5871
5872 if Base_Type (S) /= Base_Type (Typ) then
5873 Error_Msg_NE
5874 ("expect subtype of }", N, First_Subtype (Typ));
5875
5876 -- Rewrite the constraint as a range of Typ
5877 -- to allow compilation to proceed further.
5878
5879 Set_Etype (N, Typ);
5880 Rewrite (Low_Bound (R),
5881 Make_Attribute_Reference (Sloc (Low_Bound (R)),
5882 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5883 Attribute_Name => Name_First));
5884 Rewrite (High_Bound (R),
5885 Make_Attribute_Reference (Sloc (High_Bound (R)),
5886 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5887 Attribute_Name => Name_First));
5888
5889 else
5890 Resolve (R, Typ);
5891 Set_Etype (N, Etype (R));
5892
5893 -- Additionally, we must check that the bounds are compatible
5894 -- with the given subtype, which might be different from the
5895 -- type of the context.
5896
5897 Apply_Range_Check (R, S);
5898
5899 -- ??? If the above check statically detects a Constraint_Error
5900 -- it replaces the offending bound(s) of the range R with a
5901 -- Constraint_Error node. When the itype which uses these bounds
5902 -- is frozen the resulting call to Duplicate_Subexpr generates
5903 -- a new temporary for the bounds.
5904
5905 -- Unfortunately there are other itypes that are also made depend
5906 -- on these bounds, so when Duplicate_Subexpr is called they get
5907 -- a forward reference to the newly created temporaries and Gigi
5908 -- aborts on such forward references. This is probably sign of a
5909 -- more fundamental problem somewhere else in either the order of
5910 -- itype freezing or the way certain itypes are constructed.
5911
5912 -- To get around this problem we call Remove_Side_Effects right
5913 -- away if either bounds of R are a Constraint_Error.
5914
5915 declare
5916 L : constant Node_Id := Low_Bound (R);
5917 H : constant Node_Id := High_Bound (R);
5918
5919 begin
5920 if Nkind (L) = N_Raise_Constraint_Error then
5921 Remove_Side_Effects (L);
5922 end if;
5923
5924 if Nkind (H) = N_Raise_Constraint_Error then
5925 Remove_Side_Effects (H);
5926 end if;
5927 end;
5928
5929 Check_Unset_Reference (Low_Bound (R));
5930 Check_Unset_Reference (High_Bound (R));
5931 end if;
5932 end if;
5933 end Resolve_Discrete_Subtype_Indication;
5934
5935 -------------------------
5936 -- Resolve_Entity_Name --
5937 -------------------------
5938
5939 -- Used to resolve identifiers and expanded names
5940
5941 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
5942 E : constant Entity_Id := Entity (N);
5943
5944 begin
5945 -- If garbage from errors, set to Any_Type and return
5946
5947 if No (E) and then Total_Errors_Detected /= 0 then
5948 Set_Etype (N, Any_Type);
5949 return;
5950 end if;
5951
5952 -- Replace named numbers by corresponding literals. Note that this is
5953 -- the one case where Resolve_Entity_Name must reset the Etype, since
5954 -- it is currently marked as universal.
5955
5956 if Ekind (E) = E_Named_Integer then
5957 Set_Etype (N, Typ);
5958 Eval_Named_Integer (N);
5959
5960 elsif Ekind (E) = E_Named_Real then
5961 Set_Etype (N, Typ);
5962 Eval_Named_Real (N);
5963
5964 -- For enumeration literals, we need to make sure that a proper style
5965 -- check is done, since such literals are overloaded, and thus we did
5966 -- not do a style check during the first phase of analysis.
5967
5968 elsif Ekind (E) = E_Enumeration_Literal then
5969 Set_Entity_With_Style_Check (N, E);
5970 Eval_Entity_Name (N);
5971
5972 -- Case of subtype name appearing as an operand in expression
5973
5974 elsif Is_Type (E) then
5975
5976 -- Allow use of subtype if it is a concurrent type where we are
5977 -- currently inside the body. This will eventually be expanded into a
5978 -- call to Self (for tasks) or _object (for protected objects). Any
5979 -- other use of a subtype is invalid.
5980
5981 if Is_Concurrent_Type (E)
5982 and then In_Open_Scopes (E)
5983 then
5984 null;
5985
5986 -- Any other use is an eror
5987
5988 else
5989 Error_Msg_N
5990 ("invalid use of subtype mark in expression or call", N);
5991 end if;
5992
5993 -- Check discriminant use if entity is discriminant in current scope,
5994 -- i.e. discriminant of record or concurrent type currently being
5995 -- analyzed. Uses in corresponding body are unrestricted.
5996
5997 elsif Ekind (E) = E_Discriminant
5998 and then Scope (E) = Current_Scope
5999 and then not Has_Completion (Current_Scope)
6000 then
6001 Check_Discriminant_Use (N);
6002
6003 -- A parameterless generic function cannot appear in a context that
6004 -- requires resolution.
6005
6006 elsif Ekind (E) = E_Generic_Function then
6007 Error_Msg_N ("illegal use of generic function", N);
6008
6009 elsif Ekind (E) = E_Out_Parameter
6010 and then Ada_Version = Ada_83
6011 and then (Nkind (Parent (N)) in N_Op
6012 or else (Nkind (Parent (N)) = N_Assignment_Statement
6013 and then N = Expression (Parent (N)))
6014 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6015 then
6016 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6017
6018 -- In all other cases, just do the possible static evaluation
6019
6020 else
6021 -- A deferred constant that appears in an expression must have a
6022 -- completion, unless it has been removed by in-place expansion of
6023 -- an aggregate.
6024
6025 if Ekind (E) = E_Constant
6026 and then Comes_From_Source (E)
6027 and then No (Constant_Value (E))
6028 and then Is_Frozen (Etype (E))
6029 and then not In_Spec_Expression
6030 and then not Is_Imported (E)
6031 then
6032 if No_Initialization (Parent (E))
6033 or else (Present (Full_View (E))
6034 and then No_Initialization (Parent (Full_View (E))))
6035 then
6036 null;
6037 else
6038 Error_Msg_N (
6039 "deferred constant is frozen before completion", N);
6040 end if;
6041 end if;
6042
6043 Eval_Entity_Name (N);
6044 end if;
6045 end Resolve_Entity_Name;
6046
6047 -------------------
6048 -- Resolve_Entry --
6049 -------------------
6050
6051 procedure Resolve_Entry (Entry_Name : Node_Id) is
6052 Loc : constant Source_Ptr := Sloc (Entry_Name);
6053 Nam : Entity_Id;
6054 New_N : Node_Id;
6055 S : Entity_Id;
6056 Tsk : Entity_Id;
6057 E_Name : Node_Id;
6058 Index : Node_Id;
6059
6060 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6061 -- If the bounds of the entry family being called depend on task
6062 -- discriminants, build a new index subtype where a discriminant is
6063 -- replaced with the value of the discriminant of the target task.
6064 -- The target task is the prefix of the entry name in the call.
6065
6066 -----------------------
6067 -- Actual_Index_Type --
6068 -----------------------
6069
6070 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6071 Typ : constant Entity_Id := Entry_Index_Type (E);
6072 Tsk : constant Entity_Id := Scope (E);
6073 Lo : constant Node_Id := Type_Low_Bound (Typ);
6074 Hi : constant Node_Id := Type_High_Bound (Typ);
6075 New_T : Entity_Id;
6076
6077 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6078 -- If the bound is given by a discriminant, replace with a reference
6079 -- to the discriminant of the same name in the target task. If the
6080 -- entry name is the target of a requeue statement and the entry is
6081 -- in the current protected object, the bound to be used is the
6082 -- discriminal of the object (see Apply_Range_Checks for details of
6083 -- the transformation).
6084
6085 -----------------------------
6086 -- Actual_Discriminant_Ref --
6087 -----------------------------
6088
6089 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6090 Typ : constant Entity_Id := Etype (Bound);
6091 Ref : Node_Id;
6092
6093 begin
6094 Remove_Side_Effects (Bound);
6095
6096 if not Is_Entity_Name (Bound)
6097 or else Ekind (Entity (Bound)) /= E_Discriminant
6098 then
6099 return Bound;
6100
6101 elsif Is_Protected_Type (Tsk)
6102 and then In_Open_Scopes (Tsk)
6103 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6104 then
6105 -- Note: here Bound denotes a discriminant of the corresponding
6106 -- record type tskV, whose discriminal is a formal of the
6107 -- init-proc tskVIP. What we want is the body discriminal,
6108 -- which is associated to the discriminant of the original
6109 -- concurrent type tsk.
6110
6111 return New_Occurrence_Of
6112 (Find_Body_Discriminal (Entity (Bound)), Loc);
6113
6114 else
6115 Ref :=
6116 Make_Selected_Component (Loc,
6117 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6118 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6119 Analyze (Ref);
6120 Resolve (Ref, Typ);
6121 return Ref;
6122 end if;
6123 end Actual_Discriminant_Ref;
6124
6125 -- Start of processing for Actual_Index_Type
6126
6127 begin
6128 if not Has_Discriminants (Tsk)
6129 or else (not Is_Entity_Name (Lo)
6130 and then
6131 not Is_Entity_Name (Hi))
6132 then
6133 return Entry_Index_Type (E);
6134
6135 else
6136 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6137 Set_Etype (New_T, Base_Type (Typ));
6138 Set_Size_Info (New_T, Typ);
6139 Set_RM_Size (New_T, RM_Size (Typ));
6140 Set_Scalar_Range (New_T,
6141 Make_Range (Sloc (Entry_Name),
6142 Low_Bound => Actual_Discriminant_Ref (Lo),
6143 High_Bound => Actual_Discriminant_Ref (Hi)));
6144
6145 return New_T;
6146 end if;
6147 end Actual_Index_Type;
6148
6149 -- Start of processing of Resolve_Entry
6150
6151 begin
6152 -- Find name of entry being called, and resolve prefix of name
6153 -- with its own type. The prefix can be overloaded, and the name
6154 -- and signature of the entry must be taken into account.
6155
6156 if Nkind (Entry_Name) = N_Indexed_Component then
6157
6158 -- Case of dealing with entry family within the current tasks
6159
6160 E_Name := Prefix (Entry_Name);
6161
6162 else
6163 E_Name := Entry_Name;
6164 end if;
6165
6166 if Is_Entity_Name (E_Name) then
6167
6168 -- Entry call to an entry (or entry family) in the current task. This
6169 -- is legal even though the task will deadlock. Rewrite as call to
6170 -- current task.
6171
6172 -- This can also be a call to an entry in an enclosing task. If this
6173 -- is a single task, we have to retrieve its name, because the scope
6174 -- of the entry is the task type, not the object. If the enclosing
6175 -- task is a task type, the identity of the task is given by its own
6176 -- self variable.
6177
6178 -- Finally this can be a requeue on an entry of the same task or
6179 -- protected object.
6180
6181 S := Scope (Entity (E_Name));
6182
6183 for J in reverse 0 .. Scope_Stack.Last loop
6184 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6185 and then not Comes_From_Source (S)
6186 then
6187 -- S is an enclosing task or protected object. The concurrent
6188 -- declaration has been converted into a type declaration, and
6189 -- the object itself has an object declaration that follows
6190 -- the type in the same declarative part.
6191
6192 Tsk := Next_Entity (S);
6193 while Etype (Tsk) /= S loop
6194 Next_Entity (Tsk);
6195 end loop;
6196
6197 S := Tsk;
6198 exit;
6199
6200 elsif S = Scope_Stack.Table (J).Entity then
6201
6202 -- Call to current task. Will be transformed into call to Self
6203
6204 exit;
6205
6206 end if;
6207 end loop;
6208
6209 New_N :=
6210 Make_Selected_Component (Loc,
6211 Prefix => New_Occurrence_Of (S, Loc),
6212 Selector_Name =>
6213 New_Occurrence_Of (Entity (E_Name), Loc));
6214 Rewrite (E_Name, New_N);
6215 Analyze (E_Name);
6216
6217 elsif Nkind (Entry_Name) = N_Selected_Component
6218 and then Is_Overloaded (Prefix (Entry_Name))
6219 then
6220 -- Use the entry name (which must be unique at this point) to find
6221 -- the prefix that returns the corresponding task type or protected
6222 -- type.
6223
6224 declare
6225 Pref : constant Node_Id := Prefix (Entry_Name);
6226 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6227 I : Interp_Index;
6228 It : Interp;
6229
6230 begin
6231 Get_First_Interp (Pref, I, It);
6232 while Present (It.Typ) loop
6233 if Scope (Ent) = It.Typ then
6234 Set_Etype (Pref, It.Typ);
6235 exit;
6236 end if;
6237
6238 Get_Next_Interp (I, It);
6239 end loop;
6240 end;
6241 end if;
6242
6243 if Nkind (Entry_Name) = N_Selected_Component then
6244 Resolve (Prefix (Entry_Name));
6245
6246 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6247 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6248 Resolve (Prefix (Prefix (Entry_Name)));
6249 Index := First (Expressions (Entry_Name));
6250 Resolve (Index, Entry_Index_Type (Nam));
6251
6252 -- Up to this point the expression could have been the actual in a
6253 -- simple entry call, and be given by a named association.
6254
6255 if Nkind (Index) = N_Parameter_Association then
6256 Error_Msg_N ("expect expression for entry index", Index);
6257 else
6258 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6259 end if;
6260 end if;
6261 end Resolve_Entry;
6262
6263 ------------------------
6264 -- Resolve_Entry_Call --
6265 ------------------------
6266
6267 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6268 Entry_Name : constant Node_Id := Name (N);
6269 Loc : constant Source_Ptr := Sloc (Entry_Name);
6270 Actuals : List_Id;
6271 First_Named : Node_Id;
6272 Nam : Entity_Id;
6273 Norm_OK : Boolean;
6274 Obj : Node_Id;
6275 Was_Over : Boolean;
6276
6277 begin
6278 -- We kill all checks here, because it does not seem worth the effort to
6279 -- do anything better, an entry call is a big operation.
6280
6281 Kill_All_Checks;
6282
6283 -- Processing of the name is similar for entry calls and protected
6284 -- operation calls. Once the entity is determined, we can complete
6285 -- the resolution of the actuals.
6286
6287 -- The selector may be overloaded, in the case of a protected object
6288 -- with overloaded functions. The type of the context is used for
6289 -- resolution.
6290
6291 if Nkind (Entry_Name) = N_Selected_Component
6292 and then Is_Overloaded (Selector_Name (Entry_Name))
6293 and then Typ /= Standard_Void_Type
6294 then
6295 declare
6296 I : Interp_Index;
6297 It : Interp;
6298
6299 begin
6300 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6301 while Present (It.Typ) loop
6302 if Covers (Typ, It.Typ) then
6303 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6304 Set_Etype (Entry_Name, It.Typ);
6305
6306 Generate_Reference (It.Typ, N, ' ');
6307 end if;
6308
6309 Get_Next_Interp (I, It);
6310 end loop;
6311 end;
6312 end if;
6313
6314 Resolve_Entry (Entry_Name);
6315
6316 if Nkind (Entry_Name) = N_Selected_Component then
6317
6318 -- Simple entry call
6319
6320 Nam := Entity (Selector_Name (Entry_Name));
6321 Obj := Prefix (Entry_Name);
6322 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6323
6324 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6325
6326 -- Call to member of entry family
6327
6328 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6329 Obj := Prefix (Prefix (Entry_Name));
6330 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6331 end if;
6332
6333 -- We cannot in general check the maximum depth of protected entry
6334 -- calls at compile time. But we can tell that any protected entry
6335 -- call at all violates a specified nesting depth of zero.
6336
6337 if Is_Protected_Type (Scope (Nam)) then
6338 Check_Restriction (Max_Entry_Queue_Length, N);
6339 end if;
6340
6341 -- Use context type to disambiguate a protected function that can be
6342 -- called without actuals and that returns an array type, and where
6343 -- the argument list may be an indexing of the returned value.
6344
6345 if Ekind (Nam) = E_Function
6346 and then Needs_No_Actuals (Nam)
6347 and then Present (Parameter_Associations (N))
6348 and then
6349 ((Is_Array_Type (Etype (Nam))
6350 and then Covers (Typ, Component_Type (Etype (Nam))))
6351
6352 or else (Is_Access_Type (Etype (Nam))
6353 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6354 and then Covers (Typ,
6355 Component_Type (Designated_Type (Etype (Nam))))))
6356 then
6357 declare
6358 Index_Node : Node_Id;
6359
6360 begin
6361 Index_Node :=
6362 Make_Indexed_Component (Loc,
6363 Prefix =>
6364 Make_Function_Call (Loc,
6365 Name => Relocate_Node (Entry_Name)),
6366 Expressions => Parameter_Associations (N));
6367
6368 -- Since we are correcting a node classification error made by
6369 -- the parser, we call Replace rather than Rewrite.
6370
6371 Replace (N, Index_Node);
6372 Set_Etype (Prefix (N), Etype (Nam));
6373 Set_Etype (N, Typ);
6374 Resolve_Indexed_Component (N, Typ);
6375 return;
6376 end;
6377 end if;
6378
6379 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6380 and then Present (PPC_Wrapper (Nam))
6381 and then Current_Scope /= PPC_Wrapper (Nam)
6382 then
6383 -- Rewrite as call to the precondition wrapper, adding the task
6384 -- object to the list of actuals. If the call is to a member of
6385 -- an entry family, include the index as well.
6386
6387 declare
6388 New_Call : Node_Id;
6389 New_Actuals : List_Id;
6390 begin
6391 New_Actuals := New_List (Obj);
6392
6393 if Nkind (Entry_Name) = N_Indexed_Component then
6394 Append_To (New_Actuals,
6395 New_Copy_Tree (First (Expressions (Entry_Name))));
6396 end if;
6397
6398 Append_List (Parameter_Associations (N), New_Actuals);
6399 New_Call :=
6400 Make_Procedure_Call_Statement (Loc,
6401 Name =>
6402 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6403 Parameter_Associations => New_Actuals);
6404 Rewrite (N, New_Call);
6405 Analyze_And_Resolve (N);
6406 return;
6407 end;
6408 end if;
6409
6410 -- The operation name may have been overloaded. Order the actuals
6411 -- according to the formals of the resolved entity, and set the
6412 -- return type to that of the operation.
6413
6414 if Was_Over then
6415 Normalize_Actuals (N, Nam, False, Norm_OK);
6416 pragma Assert (Norm_OK);
6417 Set_Etype (N, Etype (Nam));
6418 end if;
6419
6420 Resolve_Actuals (N, Nam);
6421
6422 -- Create a call reference to the entry
6423
6424 Generate_Reference (Nam, Entry_Name, 's');
6425
6426 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6427 Check_Potentially_Blocking_Operation (N);
6428 end if;
6429
6430 -- Verify that a procedure call cannot masquerade as an entry
6431 -- call where an entry call is expected.
6432
6433 if Ekind (Nam) = E_Procedure then
6434 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6435 and then N = Entry_Call_Statement (Parent (N))
6436 then
6437 Error_Msg_N ("entry call required in select statement", N);
6438
6439 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6440 and then N = Triggering_Statement (Parent (N))
6441 then
6442 Error_Msg_N ("triggering statement cannot be procedure call", N);
6443
6444 elsif Ekind (Scope (Nam)) = E_Task_Type
6445 and then not In_Open_Scopes (Scope (Nam))
6446 then
6447 Error_Msg_N ("task has no entry with this name", Entry_Name);
6448 end if;
6449 end if;
6450
6451 -- After resolution, entry calls and protected procedure calls are
6452 -- changed into entry calls, for expansion. The structure of the node
6453 -- does not change, so it can safely be done in place. Protected
6454 -- function calls must keep their structure because they are
6455 -- subexpressions.
6456
6457 if Ekind (Nam) /= E_Function then
6458
6459 -- A protected operation that is not a function may modify the
6460 -- corresponding object, and cannot apply to a constant. If this
6461 -- is an internal call, the prefix is the type itself.
6462
6463 if Is_Protected_Type (Scope (Nam))
6464 and then not Is_Variable (Obj)
6465 and then (not Is_Entity_Name (Obj)
6466 or else not Is_Type (Entity (Obj)))
6467 then
6468 Error_Msg_N
6469 ("prefix of protected procedure or entry call must be variable",
6470 Entry_Name);
6471 end if;
6472
6473 Actuals := Parameter_Associations (N);
6474 First_Named := First_Named_Actual (N);
6475
6476 Rewrite (N,
6477 Make_Entry_Call_Statement (Loc,
6478 Name => Entry_Name,
6479 Parameter_Associations => Actuals));
6480
6481 Set_First_Named_Actual (N, First_Named);
6482 Set_Analyzed (N, True);
6483
6484 -- Protected functions can return on the secondary stack, in which
6485 -- case we must trigger the transient scope mechanism.
6486
6487 elsif Expander_Active
6488 and then Requires_Transient_Scope (Etype (Nam))
6489 then
6490 Establish_Transient_Scope (N, Sec_Stack => True);
6491 end if;
6492 end Resolve_Entry_Call;
6493
6494 -------------------------
6495 -- Resolve_Equality_Op --
6496 -------------------------
6497
6498 -- Both arguments must have the same type, and the boolean context does
6499 -- not participate in the resolution. The first pass verifies that the
6500 -- interpretation is not ambiguous, and the type of the left argument is
6501 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6502 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6503 -- though they carry a single (universal) type. Diagnose this case here.
6504
6505 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6506 L : constant Node_Id := Left_Opnd (N);
6507 R : constant Node_Id := Right_Opnd (N);
6508 T : Entity_Id := Find_Unique_Type (L, R);
6509
6510 procedure Check_Conditional_Expression (Cond : Node_Id);
6511 -- The resolution rule for conditional expressions requires that each
6512 -- such must have a unique type. This means that if several dependent
6513 -- expressions are of a non-null anonymous access type, and the context
6514 -- does not impose an expected type (as can be the case in an equality
6515 -- operation) the expression must be rejected.
6516
6517 function Find_Unique_Access_Type return Entity_Id;
6518 -- In the case of allocators, make a last-ditch attempt to find a single
6519 -- access type with the right designated type. This is semantically
6520 -- dubious, and of no interest to any real code, but c48008a makes it
6521 -- all worthwhile.
6522
6523 ----------------------------------
6524 -- Check_Conditional_Expression --
6525 ----------------------------------
6526
6527 procedure Check_Conditional_Expression (Cond : Node_Id) is
6528 Then_Expr : Node_Id;
6529 Else_Expr : Node_Id;
6530
6531 begin
6532 if Nkind (Cond) = N_Conditional_Expression then
6533 Then_Expr := Next (First (Expressions (Cond)));
6534 Else_Expr := Next (Then_Expr);
6535
6536 if Nkind (Then_Expr) /= N_Null
6537 and then Nkind (Else_Expr) /= N_Null
6538 then
6539 Error_Msg_N
6540 ("cannot determine type of conditional expression", Cond);
6541 end if;
6542 end if;
6543 end Check_Conditional_Expression;
6544
6545 -----------------------------
6546 -- Find_Unique_Access_Type --
6547 -----------------------------
6548
6549 function Find_Unique_Access_Type return Entity_Id is
6550 Acc : Entity_Id;
6551 E : Entity_Id;
6552 S : Entity_Id;
6553
6554 begin
6555 if Ekind (Etype (R)) = E_Allocator_Type then
6556 Acc := Designated_Type (Etype (R));
6557 elsif Ekind (Etype (L)) = E_Allocator_Type then
6558 Acc := Designated_Type (Etype (L));
6559 else
6560 return Empty;
6561 end if;
6562
6563 S := Current_Scope;
6564 while S /= Standard_Standard loop
6565 E := First_Entity (S);
6566 while Present (E) loop
6567 if Is_Type (E)
6568 and then Is_Access_Type (E)
6569 and then Ekind (E) /= E_Allocator_Type
6570 and then Designated_Type (E) = Base_Type (Acc)
6571 then
6572 return E;
6573 end if;
6574
6575 Next_Entity (E);
6576 end loop;
6577
6578 S := Scope (S);
6579 end loop;
6580
6581 return Empty;
6582 end Find_Unique_Access_Type;
6583
6584 -- Start of processing for Resolve_Equality_Op
6585
6586 begin
6587 Set_Etype (N, Base_Type (Typ));
6588 Generate_Reference (T, N, ' ');
6589
6590 if T = Any_Fixed then
6591 T := Unique_Fixed_Point_Type (L);
6592 end if;
6593
6594 if T /= Any_Type then
6595 if T = Any_String
6596 or else T = Any_Composite
6597 or else T = Any_Character
6598 then
6599 if T = Any_Character then
6600 Ambiguous_Character (L);
6601 else
6602 Error_Msg_N ("ambiguous operands for equality", N);
6603 end if;
6604
6605 Set_Etype (N, Any_Type);
6606 return;
6607
6608 elsif T = Any_Access
6609 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6610 then
6611 T := Find_Unique_Access_Type;
6612
6613 if No (T) then
6614 Error_Msg_N ("ambiguous operands for equality", N);
6615 Set_Etype (N, Any_Type);
6616 return;
6617 end if;
6618
6619 -- Conditional expressions must have a single type, and if the
6620 -- context does not impose one the dependent expressions cannot
6621 -- be anonymous access types.
6622
6623 elsif Ada_Version >= Ada_2012
6624 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6625 E_Anonymous_Access_Subprogram_Type)
6626 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6627 E_Anonymous_Access_Subprogram_Type)
6628 then
6629 Check_Conditional_Expression (L);
6630 Check_Conditional_Expression (R);
6631 end if;
6632
6633 Resolve (L, T);
6634 Resolve (R, T);
6635
6636 -- If the unique type is a class-wide type then it will be expanded
6637 -- into a dispatching call to the predefined primitive. Therefore we
6638 -- check here for potential violation of such restriction.
6639
6640 if Is_Class_Wide_Type (T) then
6641 Check_Restriction (No_Dispatching_Calls, N);
6642 end if;
6643
6644 if Warn_On_Redundant_Constructs
6645 and then Comes_From_Source (N)
6646 and then Is_Entity_Name (R)
6647 and then Entity (R) = Standard_True
6648 and then Comes_From_Source (R)
6649 then
6650 Error_Msg_N -- CODEFIX
6651 ("?comparison with True is redundant!", R);
6652 end if;
6653
6654 Check_Unset_Reference (L);
6655 Check_Unset_Reference (R);
6656 Generate_Operator_Reference (N, T);
6657 Check_Low_Bound_Tested (N);
6658
6659 -- If this is an inequality, it may be the implicit inequality
6660 -- created for a user-defined operation, in which case the corres-
6661 -- ponding equality operation is not intrinsic, and the operation
6662 -- cannot be constant-folded. Else fold.
6663
6664 if Nkind (N) = N_Op_Eq
6665 or else Comes_From_Source (Entity (N))
6666 or else Ekind (Entity (N)) = E_Operator
6667 or else Is_Intrinsic_Subprogram
6668 (Corresponding_Equality (Entity (N)))
6669 then
6670 Eval_Relational_Op (N);
6671
6672 elsif Nkind (N) = N_Op_Ne
6673 and then Is_Abstract_Subprogram (Entity (N))
6674 then
6675 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6676 end if;
6677
6678 -- Ada 2005: If one operand is an anonymous access type, convert the
6679 -- other operand to it, to ensure that the underlying types match in
6680 -- the back-end. Same for access_to_subprogram, and the conversion
6681 -- verifies that the types are subtype conformant.
6682
6683 -- We apply the same conversion in the case one of the operands is a
6684 -- private subtype of the type of the other.
6685
6686 -- Why the Expander_Active test here ???
6687
6688 if Expander_Active
6689 and then
6690 (Ekind_In (T, E_Anonymous_Access_Type,
6691 E_Anonymous_Access_Subprogram_Type)
6692 or else Is_Private_Type (T))
6693 then
6694 if Etype (L) /= T then
6695 Rewrite (L,
6696 Make_Unchecked_Type_Conversion (Sloc (L),
6697 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6698 Expression => Relocate_Node (L)));
6699 Analyze_And_Resolve (L, T);
6700 end if;
6701
6702 if (Etype (R)) /= T then
6703 Rewrite (R,
6704 Make_Unchecked_Type_Conversion (Sloc (R),
6705 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6706 Expression => Relocate_Node (R)));
6707 Analyze_And_Resolve (R, T);
6708 end if;
6709 end if;
6710 end if;
6711 end Resolve_Equality_Op;
6712
6713 ----------------------------------
6714 -- Resolve_Explicit_Dereference --
6715 ----------------------------------
6716
6717 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6718 Loc : constant Source_Ptr := Sloc (N);
6719 New_N : Node_Id;
6720 P : constant Node_Id := Prefix (N);
6721 I : Interp_Index;
6722 It : Interp;
6723
6724 begin
6725 Check_Fully_Declared_Prefix (Typ, P);
6726
6727 if Is_Overloaded (P) then
6728
6729 -- Use the context type to select the prefix that has the correct
6730 -- designated type.
6731
6732 Get_First_Interp (P, I, It);
6733 while Present (It.Typ) loop
6734 exit when Is_Access_Type (It.Typ)
6735 and then Covers (Typ, Designated_Type (It.Typ));
6736 Get_Next_Interp (I, It);
6737 end loop;
6738
6739 if Present (It.Typ) then
6740 Resolve (P, It.Typ);
6741 else
6742 -- If no interpretation covers the designated type of the prefix,
6743 -- this is the pathological case where not all implementations of
6744 -- the prefix allow the interpretation of the node as a call. Now
6745 -- that the expected type is known, Remove other interpretations
6746 -- from prefix, rewrite it as a call, and resolve again, so that
6747 -- the proper call node is generated.
6748
6749 Get_First_Interp (P, I, It);
6750 while Present (It.Typ) loop
6751 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6752 Remove_Interp (I);
6753 end if;
6754
6755 Get_Next_Interp (I, It);
6756 end loop;
6757
6758 New_N :=
6759 Make_Function_Call (Loc,
6760 Name =>
6761 Make_Explicit_Dereference (Loc,
6762 Prefix => P),
6763 Parameter_Associations => New_List);
6764
6765 Save_Interps (N, New_N);
6766 Rewrite (N, New_N);
6767 Analyze_And_Resolve (N, Typ);
6768 return;
6769 end if;
6770
6771 Set_Etype (N, Designated_Type (It.Typ));
6772
6773 else
6774 Resolve (P);
6775 end if;
6776
6777 if Is_Access_Type (Etype (P)) then
6778 Apply_Access_Check (N);
6779 end if;
6780
6781 -- If the designated type is a packed unconstrained array type, and the
6782 -- explicit dereference is not in the context of an attribute reference,
6783 -- then we must compute and set the actual subtype, since it is needed
6784 -- by Gigi. The reason we exclude the attribute case is that this is
6785 -- handled fine by Gigi, and in fact we use such attributes to build the
6786 -- actual subtype. We also exclude generated code (which builds actual
6787 -- subtypes directly if they are needed).
6788
6789 if Is_Array_Type (Etype (N))
6790 and then Is_Packed (Etype (N))
6791 and then not Is_Constrained (Etype (N))
6792 and then Nkind (Parent (N)) /= N_Attribute_Reference
6793 and then Comes_From_Source (N)
6794 then
6795 Set_Etype (N, Get_Actual_Subtype (N));
6796 end if;
6797
6798 -- Note: No Eval processing is required for an explicit dereference,
6799 -- because such a name can never be static.
6800
6801 end Resolve_Explicit_Dereference;
6802
6803 -------------------------------------
6804 -- Resolve_Expression_With_Actions --
6805 -------------------------------------
6806
6807 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
6808 begin
6809 Set_Etype (N, Typ);
6810 end Resolve_Expression_With_Actions;
6811
6812 -------------------------------
6813 -- Resolve_Indexed_Component --
6814 -------------------------------
6815
6816 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6817 Name : constant Node_Id := Prefix (N);
6818 Expr : Node_Id;
6819 Array_Type : Entity_Id := Empty; -- to prevent junk warning
6820 Index : Node_Id;
6821
6822 begin
6823 if Is_Overloaded (Name) then
6824
6825 -- Use the context type to select the prefix that yields the correct
6826 -- component type.
6827
6828 declare
6829 I : Interp_Index;
6830 It : Interp;
6831 I1 : Interp_Index := 0;
6832 P : constant Node_Id := Prefix (N);
6833 Found : Boolean := False;
6834
6835 begin
6836 Get_First_Interp (P, I, It);
6837 while Present (It.Typ) loop
6838 if (Is_Array_Type (It.Typ)
6839 and then Covers (Typ, Component_Type (It.Typ)))
6840 or else (Is_Access_Type (It.Typ)
6841 and then Is_Array_Type (Designated_Type (It.Typ))
6842 and then Covers
6843 (Typ, Component_Type (Designated_Type (It.Typ))))
6844 then
6845 if Found then
6846 It := Disambiguate (P, I1, I, Any_Type);
6847
6848 if It = No_Interp then
6849 Error_Msg_N ("ambiguous prefix for indexing", N);
6850 Set_Etype (N, Typ);
6851 return;
6852
6853 else
6854 Found := True;
6855 Array_Type := It.Typ;
6856 I1 := I;
6857 end if;
6858
6859 else
6860 Found := True;
6861 Array_Type := It.Typ;
6862 I1 := I;
6863 end if;
6864 end if;
6865
6866 Get_Next_Interp (I, It);
6867 end loop;
6868 end;
6869
6870 else
6871 Array_Type := Etype (Name);
6872 end if;
6873
6874 Resolve (Name, Array_Type);
6875 Array_Type := Get_Actual_Subtype_If_Available (Name);
6876
6877 -- If prefix is access type, dereference to get real array type.
6878 -- Note: we do not apply an access check because the expander always
6879 -- introduces an explicit dereference, and the check will happen there.
6880
6881 if Is_Access_Type (Array_Type) then
6882 Array_Type := Designated_Type (Array_Type);
6883 end if;
6884
6885 -- If name was overloaded, set component type correctly now
6886 -- If a misplaced call to an entry family (which has no index types)
6887 -- return. Error will be diagnosed from calling context.
6888
6889 if Is_Array_Type (Array_Type) then
6890 Set_Etype (N, Component_Type (Array_Type));
6891 else
6892 return;
6893 end if;
6894
6895 Index := First_Index (Array_Type);
6896 Expr := First (Expressions (N));
6897
6898 -- The prefix may have resolved to a string literal, in which case its
6899 -- etype has a special representation. This is only possible currently
6900 -- if the prefix is a static concatenation, written in functional
6901 -- notation.
6902
6903 if Ekind (Array_Type) = E_String_Literal_Subtype then
6904 Resolve (Expr, Standard_Positive);
6905
6906 else
6907 while Present (Index) and Present (Expr) loop
6908 Resolve (Expr, Etype (Index));
6909 Check_Unset_Reference (Expr);
6910
6911 if Is_Scalar_Type (Etype (Expr)) then
6912 Apply_Scalar_Range_Check (Expr, Etype (Index));
6913 else
6914 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
6915 end if;
6916
6917 Next_Index (Index);
6918 Next (Expr);
6919 end loop;
6920 end if;
6921
6922 -- Do not generate the warning on suspicious index if we are analyzing
6923 -- package Ada.Tags; otherwise we will report the warning with the
6924 -- Prims_Ptr field of the dispatch table.
6925
6926 if Scope (Etype (Prefix (N))) = Standard_Standard
6927 or else not
6928 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
6929 Ada_Tags)
6930 then
6931 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
6932 Eval_Indexed_Component (N);
6933 end if;
6934
6935 -- If the array type is atomic, and is packed, and we are in a left side
6936 -- context, then this is worth a warning, since we have a situation
6937 -- where the access to the component may cause extra read/writes of
6938 -- the atomic array object, which could be considered unexpected.
6939
6940 if Nkind (N) = N_Indexed_Component
6941 and then (Is_Atomic (Array_Type)
6942 or else (Is_Entity_Name (Prefix (N))
6943 and then Is_Atomic (Entity (Prefix (N)))))
6944 and then Is_Bit_Packed_Array (Array_Type)
6945 and then Is_LHS (N)
6946 then
6947 Error_Msg_N ("?assignment to component of packed atomic array",
6948 Prefix (N));
6949 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
6950 Prefix (N));
6951 end if;
6952 end Resolve_Indexed_Component;
6953
6954 -----------------------------
6955 -- Resolve_Integer_Literal --
6956 -----------------------------
6957
6958 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
6959 begin
6960 Set_Etype (N, Typ);
6961 Eval_Integer_Literal (N);
6962 end Resolve_Integer_Literal;
6963
6964 --------------------------------
6965 -- Resolve_Intrinsic_Operator --
6966 --------------------------------
6967
6968 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
6969 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6970 Op : Entity_Id;
6971 Orig_Op : constant Entity_Id := Entity (N);
6972 Arg1 : Node_Id;
6973 Arg2 : Node_Id;
6974
6975 begin
6976 -- We must preserve the original entity in a generic setting, so that
6977 -- the legality of the operation can be verified in an instance.
6978
6979 if not Expander_Active then
6980 return;
6981 end if;
6982
6983 Op := Entity (N);
6984 while Scope (Op) /= Standard_Standard loop
6985 Op := Homonym (Op);
6986 pragma Assert (Present (Op));
6987 end loop;
6988
6989 Set_Entity (N, Op);
6990 Set_Is_Overloaded (N, False);
6991
6992 -- If the operand type is private, rewrite with suitable conversions on
6993 -- the operands and the result, to expose the proper underlying numeric
6994 -- type.
6995
6996 if Is_Private_Type (Typ) then
6997 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
6998
6999 if Nkind (N) = N_Op_Expon then
7000 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7001 else
7002 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7003 end if;
7004
7005 if Nkind (Arg1) = N_Type_Conversion then
7006 Save_Interps (Left_Opnd (N), Expression (Arg1));
7007 end if;
7008
7009 if Nkind (Arg2) = N_Type_Conversion then
7010 Save_Interps (Right_Opnd (N), Expression (Arg2));
7011 end if;
7012
7013 Set_Left_Opnd (N, Arg1);
7014 Set_Right_Opnd (N, Arg2);
7015
7016 Set_Etype (N, Btyp);
7017 Rewrite (N, Unchecked_Convert_To (Typ, N));
7018 Resolve (N, Typ);
7019
7020 elsif Typ /= Etype (Left_Opnd (N))
7021 or else Typ /= Etype (Right_Opnd (N))
7022 then
7023 -- Add explicit conversion where needed, and save interpretations in
7024 -- case operands are overloaded. If the context is a VMS operation,
7025 -- assert that the conversion is legal (the operands have the proper
7026 -- types to select the VMS intrinsic). Note that in rare cases the
7027 -- VMS operators may be visible, but the default System is being used
7028 -- and Address is a private type.
7029
7030 Arg1 := Convert_To (Typ, Left_Opnd (N));
7031 Arg2 := Convert_To (Typ, Right_Opnd (N));
7032
7033 if Nkind (Arg1) = N_Type_Conversion then
7034 Save_Interps (Left_Opnd (N), Expression (Arg1));
7035
7036 if Is_VMS_Operator (Orig_Op) then
7037 Set_Conversion_OK (Arg1);
7038 end if;
7039 else
7040 Save_Interps (Left_Opnd (N), Arg1);
7041 end if;
7042
7043 if Nkind (Arg2) = N_Type_Conversion then
7044 Save_Interps (Right_Opnd (N), Expression (Arg2));
7045
7046 if Is_VMS_Operator (Orig_Op) then
7047 Set_Conversion_OK (Arg2);
7048 end if;
7049 else
7050 Save_Interps (Right_Opnd (N), Arg2);
7051 end if;
7052
7053 Rewrite (Left_Opnd (N), Arg1);
7054 Rewrite (Right_Opnd (N), Arg2);
7055 Analyze (Arg1);
7056 Analyze (Arg2);
7057 Resolve_Arithmetic_Op (N, Typ);
7058
7059 else
7060 Resolve_Arithmetic_Op (N, Typ);
7061 end if;
7062 end Resolve_Intrinsic_Operator;
7063
7064 --------------------------------------
7065 -- Resolve_Intrinsic_Unary_Operator --
7066 --------------------------------------
7067
7068 procedure Resolve_Intrinsic_Unary_Operator
7069 (N : Node_Id;
7070 Typ : Entity_Id)
7071 is
7072 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7073 Op : Entity_Id;
7074 Arg2 : Node_Id;
7075
7076 begin
7077 Op := Entity (N);
7078 while Scope (Op) /= Standard_Standard loop
7079 Op := Homonym (Op);
7080 pragma Assert (Present (Op));
7081 end loop;
7082
7083 Set_Entity (N, Op);
7084
7085 if Is_Private_Type (Typ) then
7086 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7087 Save_Interps (Right_Opnd (N), Expression (Arg2));
7088
7089 Set_Right_Opnd (N, Arg2);
7090
7091 Set_Etype (N, Btyp);
7092 Rewrite (N, Unchecked_Convert_To (Typ, N));
7093 Resolve (N, Typ);
7094
7095 else
7096 Resolve_Unary_Op (N, Typ);
7097 end if;
7098 end Resolve_Intrinsic_Unary_Operator;
7099
7100 ------------------------
7101 -- Resolve_Logical_Op --
7102 ------------------------
7103
7104 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7105 B_Typ : Entity_Id;
7106
7107 begin
7108 Check_No_Direct_Boolean_Operators (N);
7109
7110 -- Predefined operations on scalar types yield the base type. On the
7111 -- other hand, logical operations on arrays yield the type of the
7112 -- arguments (and the context).
7113
7114 if Is_Array_Type (Typ) then
7115 B_Typ := Typ;
7116 else
7117 B_Typ := Base_Type (Typ);
7118 end if;
7119
7120 -- OK if this is a VMS-specific intrinsic operation
7121
7122 if Is_VMS_Operator (Entity (N)) then
7123 null;
7124
7125 -- The following test is required because the operands of the operation
7126 -- may be literals, in which case the resulting type appears to be
7127 -- compatible with a signed integer type, when in fact it is compatible
7128 -- only with modular types. If the context itself is universal, the
7129 -- operation is illegal.
7130
7131 elsif not Valid_Boolean_Arg (Typ) then
7132 Error_Msg_N ("invalid context for logical operation", N);
7133 Set_Etype (N, Any_Type);
7134 return;
7135
7136 elsif Typ = Any_Modular then
7137 Error_Msg_N
7138 ("no modular type available in this context", N);
7139 Set_Etype (N, Any_Type);
7140 return;
7141 elsif Is_Modular_Integer_Type (Typ)
7142 and then Etype (Left_Opnd (N)) = Universal_Integer
7143 and then Etype (Right_Opnd (N)) = Universal_Integer
7144 then
7145 Check_For_Visible_Operator (N, B_Typ);
7146 end if;
7147
7148 Resolve (Left_Opnd (N), B_Typ);
7149 Resolve (Right_Opnd (N), B_Typ);
7150
7151 Check_Unset_Reference (Left_Opnd (N));
7152 Check_Unset_Reference (Right_Opnd (N));
7153
7154 Set_Etype (N, B_Typ);
7155 Generate_Operator_Reference (N, B_Typ);
7156 Eval_Logical_Op (N);
7157 end Resolve_Logical_Op;
7158
7159 ---------------------------
7160 -- Resolve_Membership_Op --
7161 ---------------------------
7162
7163 -- The context can only be a boolean type, and does not determine
7164 -- the arguments. Arguments should be unambiguous, but the preference
7165 -- rule for universal types applies.
7166
7167 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7168 pragma Warnings (Off, Typ);
7169
7170 L : constant Node_Id := Left_Opnd (N);
7171 R : constant Node_Id := Right_Opnd (N);
7172 T : Entity_Id;
7173
7174 procedure Resolve_Set_Membership;
7175 -- Analysis has determined a unique type for the left operand.
7176 -- Use it to resolve the disjuncts.
7177
7178 ----------------------------
7179 -- Resolve_Set_Membership --
7180 ----------------------------
7181
7182 procedure Resolve_Set_Membership is
7183 Alt : Node_Id;
7184
7185 begin
7186 Resolve (L, Etype (L));
7187
7188 Alt := First (Alternatives (N));
7189 while Present (Alt) loop
7190
7191 -- Alternative is an expression, a range
7192 -- or a subtype mark.
7193
7194 if not Is_Entity_Name (Alt)
7195 or else not Is_Type (Entity (Alt))
7196 then
7197 Resolve (Alt, Etype (L));
7198 end if;
7199
7200 Next (Alt);
7201 end loop;
7202 end Resolve_Set_Membership;
7203
7204 -- Start of processing for Resolve_Membership_Op
7205
7206 begin
7207 if L = Error or else R = Error then
7208 return;
7209 end if;
7210
7211 if Present (Alternatives (N)) then
7212 Resolve_Set_Membership;
7213 return;
7214
7215 elsif not Is_Overloaded (R)
7216 and then
7217 (Etype (R) = Universal_Integer or else
7218 Etype (R) = Universal_Real)
7219 and then Is_Overloaded (L)
7220 then
7221 T := Etype (R);
7222
7223 -- Ada 2005 (AI-251): Support the following case:
7224
7225 -- type I is interface;
7226 -- type T is tagged ...
7227
7228 -- function Test (O : I'Class) is
7229 -- begin
7230 -- return O in T'Class.
7231 -- end Test;
7232
7233 -- In this case we have nothing else to do. The membership test will be
7234 -- done at run time.
7235
7236 elsif Ada_Version >= Ada_2005
7237 and then Is_Class_Wide_Type (Etype (L))
7238 and then Is_Interface (Etype (L))
7239 and then Is_Class_Wide_Type (Etype (R))
7240 and then not Is_Interface (Etype (R))
7241 then
7242 return;
7243
7244 else
7245 T := Intersect_Types (L, R);
7246 end if;
7247
7248 -- If mixed-mode operations are present and operands are all literal,
7249 -- the only interpretation involves Duration, which is probably not
7250 -- the intention of the programmer.
7251
7252 if T = Any_Fixed then
7253 T := Unique_Fixed_Point_Type (N);
7254
7255 if T = Any_Type then
7256 return;
7257 end if;
7258 end if;
7259
7260 Resolve (L, T);
7261 Check_Unset_Reference (L);
7262
7263 if Nkind (R) = N_Range
7264 and then not Is_Scalar_Type (T)
7265 then
7266 Error_Msg_N ("scalar type required for range", R);
7267 end if;
7268
7269 if Is_Entity_Name (R) then
7270 Freeze_Expression (R);
7271 else
7272 Resolve (R, T);
7273 Check_Unset_Reference (R);
7274 end if;
7275
7276 Eval_Membership_Op (N);
7277 end Resolve_Membership_Op;
7278
7279 ------------------
7280 -- Resolve_Null --
7281 ------------------
7282
7283 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7284 Loc : constant Source_Ptr := Sloc (N);
7285
7286 begin
7287 -- Handle restriction against anonymous null access values This
7288 -- restriction can be turned off using -gnatdj.
7289
7290 -- Ada 2005 (AI-231): Remove restriction
7291
7292 if Ada_Version < Ada_2005
7293 and then not Debug_Flag_J
7294 and then Ekind (Typ) = E_Anonymous_Access_Type
7295 and then Comes_From_Source (N)
7296 then
7297 -- In the common case of a call which uses an explicitly null value
7298 -- for an access parameter, give specialized error message.
7299
7300 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7301 N_Function_Call)
7302 then
7303 Error_Msg_N
7304 ("null is not allowed as argument for an access parameter", N);
7305
7306 -- Standard message for all other cases (are there any?)
7307
7308 else
7309 Error_Msg_N
7310 ("null cannot be of an anonymous access type", N);
7311 end if;
7312 end if;
7313
7314 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7315 -- assignment to a null-excluding object
7316
7317 if Ada_Version >= Ada_2005
7318 and then Can_Never_Be_Null (Typ)
7319 and then Nkind (Parent (N)) = N_Assignment_Statement
7320 then
7321 if not Inside_Init_Proc then
7322 Insert_Action
7323 (Compile_Time_Constraint_Error (N,
7324 "(Ada 2005) null not allowed in null-excluding objects?"),
7325 Make_Raise_Constraint_Error (Loc,
7326 Reason => CE_Access_Check_Failed));
7327 else
7328 Insert_Action (N,
7329 Make_Raise_Constraint_Error (Loc,
7330 Reason => CE_Access_Check_Failed));
7331 end if;
7332 end if;
7333
7334 -- In a distributed context, null for a remote access to subprogram may
7335 -- need to be replaced with a special record aggregate. In this case,
7336 -- return after having done the transformation.
7337
7338 if (Ekind (Typ) = E_Record_Type
7339 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7340 and then Remote_AST_Null_Value (N, Typ)
7341 then
7342 return;
7343 end if;
7344
7345 -- The null literal takes its type from the context
7346
7347 Set_Etype (N, Typ);
7348 end Resolve_Null;
7349
7350 -----------------------
7351 -- Resolve_Op_Concat --
7352 -----------------------
7353
7354 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7355
7356 -- We wish to avoid deep recursion, because concatenations are often
7357 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7358 -- operands nonrecursively until we find something that is not a simple
7359 -- concatenation (A in this case). We resolve that, and then walk back
7360 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7361 -- to do the rest of the work at each level. The Parent pointers allow
7362 -- us to avoid recursion, and thus avoid running out of memory. See also
7363 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7364
7365 NN : Node_Id := N;
7366 Op1 : Node_Id;
7367
7368 begin
7369 -- The following code is equivalent to:
7370
7371 -- Resolve_Op_Concat_First (NN, Typ);
7372 -- Resolve_Op_Concat_Arg (N, ...);
7373 -- Resolve_Op_Concat_Rest (N, Typ);
7374
7375 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7376 -- operand is a concatenation.
7377
7378 -- Walk down left operands
7379
7380 loop
7381 Resolve_Op_Concat_First (NN, Typ);
7382 Op1 := Left_Opnd (NN);
7383 exit when not (Nkind (Op1) = N_Op_Concat
7384 and then not Is_Array_Type (Component_Type (Typ))
7385 and then Entity (Op1) = Entity (NN));
7386 NN := Op1;
7387 end loop;
7388
7389 -- Now (given the above example) NN is A&B and Op1 is A
7390
7391 -- First resolve Op1 ...
7392
7393 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7394
7395 -- ... then walk NN back up until we reach N (where we started), calling
7396 -- Resolve_Op_Concat_Rest along the way.
7397
7398 loop
7399 Resolve_Op_Concat_Rest (NN, Typ);
7400 exit when NN = N;
7401 NN := Parent (NN);
7402 end loop;
7403 end Resolve_Op_Concat;
7404
7405 ---------------------------
7406 -- Resolve_Op_Concat_Arg --
7407 ---------------------------
7408
7409 procedure Resolve_Op_Concat_Arg
7410 (N : Node_Id;
7411 Arg : Node_Id;
7412 Typ : Entity_Id;
7413 Is_Comp : Boolean)
7414 is
7415 Btyp : constant Entity_Id := Base_Type (Typ);
7416
7417 begin
7418 if In_Instance then
7419 if Is_Comp
7420 or else (not Is_Overloaded (Arg)
7421 and then Etype (Arg) /= Any_Composite
7422 and then Covers (Component_Type (Typ), Etype (Arg)))
7423 then
7424 Resolve (Arg, Component_Type (Typ));
7425 else
7426 Resolve (Arg, Btyp);
7427 end if;
7428
7429 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
7430 if Nkind (Arg) = N_Aggregate
7431 and then Is_Composite_Type (Component_Type (Typ))
7432 then
7433 if Is_Private_Type (Component_Type (Typ)) then
7434 Resolve (Arg, Btyp);
7435 else
7436 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7437 Set_Etype (Arg, Any_Type);
7438 end if;
7439
7440 else
7441 if Is_Overloaded (Arg)
7442 and then Has_Compatible_Type (Arg, Typ)
7443 and then Etype (Arg) /= Any_Type
7444 then
7445 declare
7446 I : Interp_Index;
7447 It : Interp;
7448 Func : Entity_Id;
7449
7450 begin
7451 Get_First_Interp (Arg, I, It);
7452 Func := It.Nam;
7453 Get_Next_Interp (I, It);
7454
7455 -- Special-case the error message when the overloading is
7456 -- caused by a function that yields an array and can be
7457 -- called without parameters.
7458
7459 if It.Nam = Func then
7460 Error_Msg_Sloc := Sloc (Func);
7461 Error_Msg_N ("ambiguous call to function#", Arg);
7462 Error_Msg_NE
7463 ("\\interpretation as call yields&", Arg, Typ);
7464 Error_Msg_NE
7465 ("\\interpretation as indexing of call yields&",
7466 Arg, Component_Type (Typ));
7467
7468 else
7469 Error_Msg_N
7470 ("ambiguous operand for concatenation!", Arg);
7471 Get_First_Interp (Arg, I, It);
7472 while Present (It.Nam) loop
7473 Error_Msg_Sloc := Sloc (It.Nam);
7474
7475 if Base_Type (It.Typ) = Base_Type (Typ)
7476 or else Base_Type (It.Typ) =
7477 Base_Type (Component_Type (Typ))
7478 then
7479 Error_Msg_N -- CODEFIX
7480 ("\\possible interpretation#", Arg);
7481 end if;
7482
7483 Get_Next_Interp (I, It);
7484 end loop;
7485 end if;
7486 end;
7487 end if;
7488
7489 Resolve (Arg, Component_Type (Typ));
7490
7491 if Nkind (Arg) = N_String_Literal then
7492 Set_Etype (Arg, Component_Type (Typ));
7493 end if;
7494
7495 if Arg = Left_Opnd (N) then
7496 Set_Is_Component_Left_Opnd (N);
7497 else
7498 Set_Is_Component_Right_Opnd (N);
7499 end if;
7500 end if;
7501
7502 else
7503 Resolve (Arg, Btyp);
7504 end if;
7505
7506 Check_Unset_Reference (Arg);
7507 end Resolve_Op_Concat_Arg;
7508
7509 -----------------------------
7510 -- Resolve_Op_Concat_First --
7511 -----------------------------
7512
7513 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7514 Btyp : constant Entity_Id := Base_Type (Typ);
7515 Op1 : constant Node_Id := Left_Opnd (N);
7516 Op2 : constant Node_Id := Right_Opnd (N);
7517
7518 begin
7519 -- The parser folds an enormous sequence of concatenations of string
7520 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7521 -- in the right operand. If the expression resolves to a predefined "&"
7522 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7523 -- we give an error. See P_Simple_Expression in Par.Ch4.
7524
7525 if Nkind (Op2) = N_String_Literal
7526 and then Is_Folded_In_Parser (Op2)
7527 and then Ekind (Entity (N)) = E_Function
7528 then
7529 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7530 and then String_Length (Strval (Op1)) = 0);
7531 Error_Msg_N ("too many user-defined concatenations", N);
7532 return;
7533 end if;
7534
7535 Set_Etype (N, Btyp);
7536
7537 if Is_Limited_Composite (Btyp) then
7538 Error_Msg_N ("concatenation not available for limited array", N);
7539 Explain_Limited_Type (Btyp, N);
7540 end if;
7541 end Resolve_Op_Concat_First;
7542
7543 ----------------------------
7544 -- Resolve_Op_Concat_Rest --
7545 ----------------------------
7546
7547 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7548 Op1 : constant Node_Id := Left_Opnd (N);
7549 Op2 : constant Node_Id := Right_Opnd (N);
7550
7551 begin
7552 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7553
7554 Generate_Operator_Reference (N, Typ);
7555
7556 if Is_String_Type (Typ) then
7557 Eval_Concatenation (N);
7558 end if;
7559
7560 -- If this is not a static concatenation, but the result is a string
7561 -- type (and not an array of strings) ensure that static string operands
7562 -- have their subtypes properly constructed.
7563
7564 if Nkind (N) /= N_String_Literal
7565 and then Is_Character_Type (Component_Type (Typ))
7566 then
7567 Set_String_Literal_Subtype (Op1, Typ);
7568 Set_String_Literal_Subtype (Op2, Typ);
7569 end if;
7570 end Resolve_Op_Concat_Rest;
7571
7572 ----------------------
7573 -- Resolve_Op_Expon --
7574 ----------------------
7575
7576 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7577 B_Typ : constant Entity_Id := Base_Type (Typ);
7578
7579 begin
7580 -- Catch attempts to do fixed-point exponentiation with universal
7581 -- operands, which is a case where the illegality is not caught during
7582 -- normal operator analysis.
7583
7584 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7585 Error_Msg_N ("exponentiation not available for fixed point", N);
7586 return;
7587 end if;
7588
7589 if Comes_From_Source (N)
7590 and then Ekind (Entity (N)) = E_Function
7591 and then Is_Imported (Entity (N))
7592 and then Is_Intrinsic_Subprogram (Entity (N))
7593 then
7594 Resolve_Intrinsic_Operator (N, Typ);
7595 return;
7596 end if;
7597
7598 if Etype (Left_Opnd (N)) = Universal_Integer
7599 or else Etype (Left_Opnd (N)) = Universal_Real
7600 then
7601 Check_For_Visible_Operator (N, B_Typ);
7602 end if;
7603
7604 -- We do the resolution using the base type, because intermediate values
7605 -- in expressions always are of the base type, not a subtype of it.
7606
7607 Resolve (Left_Opnd (N), B_Typ);
7608 Resolve (Right_Opnd (N), Standard_Integer);
7609
7610 Check_Unset_Reference (Left_Opnd (N));
7611 Check_Unset_Reference (Right_Opnd (N));
7612
7613 Set_Etype (N, B_Typ);
7614 Generate_Operator_Reference (N, B_Typ);
7615 Eval_Op_Expon (N);
7616
7617 -- Set overflow checking bit. Much cleverer code needed here eventually
7618 -- and perhaps the Resolve routines should be separated for the various
7619 -- arithmetic operations, since they will need different processing. ???
7620
7621 if Nkind (N) in N_Op then
7622 if not Overflow_Checks_Suppressed (Etype (N)) then
7623 Enable_Overflow_Check (N);
7624 end if;
7625 end if;
7626 end Resolve_Op_Expon;
7627
7628 --------------------
7629 -- Resolve_Op_Not --
7630 --------------------
7631
7632 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7633 B_Typ : Entity_Id;
7634
7635 function Parent_Is_Boolean return Boolean;
7636 -- This function determines if the parent node is a boolean operator
7637 -- or operation (comparison op, membership test, or short circuit form)
7638 -- and the not in question is the left operand of this operation.
7639 -- Note that if the not is in parens, then false is returned.
7640
7641 -----------------------
7642 -- Parent_Is_Boolean --
7643 -----------------------
7644
7645 function Parent_Is_Boolean return Boolean is
7646 begin
7647 if Paren_Count (N) /= 0 then
7648 return False;
7649
7650 else
7651 case Nkind (Parent (N)) is
7652 when N_Op_And |
7653 N_Op_Eq |
7654 N_Op_Ge |
7655 N_Op_Gt |
7656 N_Op_Le |
7657 N_Op_Lt |
7658 N_Op_Ne |
7659 N_Op_Or |
7660 N_Op_Xor |
7661 N_In |
7662 N_Not_In |
7663 N_And_Then |
7664 N_Or_Else =>
7665
7666 return Left_Opnd (Parent (N)) = N;
7667
7668 when others =>
7669 return False;
7670 end case;
7671 end if;
7672 end Parent_Is_Boolean;
7673
7674 -- Start of processing for Resolve_Op_Not
7675
7676 begin
7677 -- Predefined operations on scalar types yield the base type. On the
7678 -- other hand, logical operations on arrays yield the type of the
7679 -- arguments (and the context).
7680
7681 if Is_Array_Type (Typ) then
7682 B_Typ := Typ;
7683 else
7684 B_Typ := Base_Type (Typ);
7685 end if;
7686
7687 if Is_VMS_Operator (Entity (N)) then
7688 null;
7689
7690 -- Straightforward case of incorrect arguments
7691
7692 elsif not Valid_Boolean_Arg (Typ) then
7693 Error_Msg_N ("invalid operand type for operator&", N);
7694 Set_Etype (N, Any_Type);
7695 return;
7696
7697 -- Special case of probable missing parens
7698
7699 elsif Typ = Universal_Integer or else Typ = Any_Modular then
7700 if Parent_Is_Boolean then
7701 Error_Msg_N
7702 ("operand of not must be enclosed in parentheses",
7703 Right_Opnd (N));
7704 else
7705 Error_Msg_N
7706 ("no modular type available in this context", N);
7707 end if;
7708
7709 Set_Etype (N, Any_Type);
7710 return;
7711
7712 -- OK resolution of not
7713
7714 else
7715 -- Warn if non-boolean types involved. This is a case like not a < b
7716 -- where a and b are modular, where we will get (not a) < b and most
7717 -- likely not (a < b) was intended.
7718
7719 if Warn_On_Questionable_Missing_Parens
7720 and then not Is_Boolean_Type (Typ)
7721 and then Parent_Is_Boolean
7722 then
7723 Error_Msg_N ("?not expression should be parenthesized here!", N);
7724 end if;
7725
7726 -- Warn on double negation if checking redundant constructs
7727
7728 if Warn_On_Redundant_Constructs
7729 and then Comes_From_Source (N)
7730 and then Comes_From_Source (Right_Opnd (N))
7731 and then Root_Type (Typ) = Standard_Boolean
7732 and then Nkind (Right_Opnd (N)) = N_Op_Not
7733 then
7734 Error_Msg_N ("redundant double negation?", N);
7735 end if;
7736
7737 -- Complete resolution and evaluation of NOT
7738
7739 Resolve (Right_Opnd (N), B_Typ);
7740 Check_Unset_Reference (Right_Opnd (N));
7741 Set_Etype (N, B_Typ);
7742 Generate_Operator_Reference (N, B_Typ);
7743 Eval_Op_Not (N);
7744 end if;
7745 end Resolve_Op_Not;
7746
7747 -----------------------------
7748 -- Resolve_Operator_Symbol --
7749 -----------------------------
7750
7751 -- Nothing to be done, all resolved already
7752
7753 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
7754 pragma Warnings (Off, N);
7755 pragma Warnings (Off, Typ);
7756
7757 begin
7758 null;
7759 end Resolve_Operator_Symbol;
7760
7761 ----------------------------------
7762 -- Resolve_Qualified_Expression --
7763 ----------------------------------
7764
7765 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
7766 pragma Warnings (Off, Typ);
7767
7768 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7769 Expr : constant Node_Id := Expression (N);
7770
7771 begin
7772 Resolve (Expr, Target_Typ);
7773
7774 -- A qualified expression requires an exact match of the type,
7775 -- class-wide matching is not allowed. However, if the qualifying
7776 -- type is specific and the expression has a class-wide type, it
7777 -- may still be okay, since it can be the result of the expansion
7778 -- of a call to a dispatching function, so we also have to check
7779 -- class-wideness of the type of the expression's original node.
7780
7781 if (Is_Class_Wide_Type (Target_Typ)
7782 or else
7783 (Is_Class_Wide_Type (Etype (Expr))
7784 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
7785 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
7786 then
7787 Wrong_Type (Expr, Target_Typ);
7788 end if;
7789
7790 -- If the target type is unconstrained, then we reset the type of the
7791 -- result from the type of the expression. For other cases, the actual
7792 -- subtype of the expression is the target type.
7793
7794 if Is_Composite_Type (Target_Typ)
7795 and then not Is_Constrained (Target_Typ)
7796 then
7797 Set_Etype (N, Etype (Expr));
7798 end if;
7799
7800 Eval_Qualified_Expression (N);
7801 end Resolve_Qualified_Expression;
7802
7803 -----------------------------------
7804 -- Resolve_Quantified_Expression --
7805 -----------------------------------
7806
7807 procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is
7808 begin
7809 -- The loop structure is already resolved during its analysis, only the
7810 -- resolution of the condition needs to be done.
7811
7812 Resolve (Condition (N), Typ);
7813 end Resolve_Quantified_Expression;
7814
7815 -------------------
7816 -- Resolve_Range --
7817 -------------------
7818
7819 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
7820 L : constant Node_Id := Low_Bound (N);
7821 H : constant Node_Id := High_Bound (N);
7822
7823 function First_Last_Ref return Boolean;
7824 -- Returns True if N is of the form X'First .. X'Last where X is the
7825 -- same entity for both attributes.
7826
7827 --------------------
7828 -- First_Last_Ref --
7829 --------------------
7830
7831 function First_Last_Ref return Boolean is
7832 Lorig : constant Node_Id := Original_Node (L);
7833 Horig : constant Node_Id := Original_Node (H);
7834
7835 begin
7836 if Nkind (Lorig) = N_Attribute_Reference
7837 and then Nkind (Horig) = N_Attribute_Reference
7838 and then Attribute_Name (Lorig) = Name_First
7839 and then Attribute_Name (Horig) = Name_Last
7840 then
7841 declare
7842 PL : constant Node_Id := Prefix (Lorig);
7843 PH : constant Node_Id := Prefix (Horig);
7844 begin
7845 if Is_Entity_Name (PL)
7846 and then Is_Entity_Name (PH)
7847 and then Entity (PL) = Entity (PH)
7848 then
7849 return True;
7850 end if;
7851 end;
7852 end if;
7853
7854 return False;
7855 end First_Last_Ref;
7856
7857 -- Start of processing for Resolve_Range
7858
7859 begin
7860 Set_Etype (N, Typ);
7861 Resolve (L, Typ);
7862 Resolve (H, Typ);
7863
7864 -- Check for inappropriate range on unordered enumeration type
7865
7866 if Bad_Unordered_Enumeration_Reference (N, Typ)
7867
7868 -- Exclude X'First .. X'Last if X is the same entity for both
7869
7870 and then not First_Last_Ref
7871 then
7872 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
7873 end if;
7874
7875 Check_Unset_Reference (L);
7876 Check_Unset_Reference (H);
7877
7878 -- We have to check the bounds for being within the base range as
7879 -- required for a non-static context. Normally this is automatic and
7880 -- done as part of evaluating expressions, but the N_Range node is an
7881 -- exception, since in GNAT we consider this node to be a subexpression,
7882 -- even though in Ada it is not. The circuit in Sem_Eval could check for
7883 -- this, but that would put the test on the main evaluation path for
7884 -- expressions.
7885
7886 Check_Non_Static_Context (L);
7887 Check_Non_Static_Context (H);
7888
7889 -- Check for an ambiguous range over character literals. This will
7890 -- happen with a membership test involving only literals.
7891
7892 if Typ = Any_Character then
7893 Ambiguous_Character (L);
7894 Set_Etype (N, Any_Type);
7895 return;
7896 end if;
7897
7898 -- If bounds are static, constant-fold them, so size computations
7899 -- are identical between front-end and back-end. Do not perform this
7900 -- transformation while analyzing generic units, as type information
7901 -- would then be lost when reanalyzing the constant node in the
7902 -- instance.
7903
7904 if Is_Discrete_Type (Typ) and then Expander_Active then
7905 if Is_OK_Static_Expression (L) then
7906 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
7907 end if;
7908
7909 if Is_OK_Static_Expression (H) then
7910 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
7911 end if;
7912 end if;
7913 end Resolve_Range;
7914
7915 --------------------------
7916 -- Resolve_Real_Literal --
7917 --------------------------
7918
7919 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
7920 Actual_Typ : constant Entity_Id := Etype (N);
7921
7922 begin
7923 -- Special processing for fixed-point literals to make sure that the
7924 -- value is an exact multiple of small where this is required. We
7925 -- skip this for the universal real case, and also for generic types.
7926
7927 if Is_Fixed_Point_Type (Typ)
7928 and then Typ /= Universal_Fixed
7929 and then Typ /= Any_Fixed
7930 and then not Is_Generic_Type (Typ)
7931 then
7932 declare
7933 Val : constant Ureal := Realval (N);
7934 Cintr : constant Ureal := Val / Small_Value (Typ);
7935 Cint : constant Uint := UR_Trunc (Cintr);
7936 Den : constant Uint := Norm_Den (Cintr);
7937 Stat : Boolean;
7938
7939 begin
7940 -- Case of literal is not an exact multiple of the Small
7941
7942 if Den /= 1 then
7943
7944 -- For a source program literal for a decimal fixed-point
7945 -- type, this is statically illegal (RM 4.9(36)).
7946
7947 if Is_Decimal_Fixed_Point_Type (Typ)
7948 and then Actual_Typ = Universal_Real
7949 and then Comes_From_Source (N)
7950 then
7951 Error_Msg_N ("value has extraneous low order digits", N);
7952 end if;
7953
7954 -- Generate a warning if literal from source
7955
7956 if Is_Static_Expression (N)
7957 and then Warn_On_Bad_Fixed_Value
7958 then
7959 Error_Msg_N
7960 ("?static fixed-point value is not a multiple of Small!",
7961 N);
7962 end if;
7963
7964 -- Replace literal by a value that is the exact representation
7965 -- of a value of the type, i.e. a multiple of the small value,
7966 -- by truncation, since Machine_Rounds is false for all GNAT
7967 -- fixed-point types (RM 4.9(38)).
7968
7969 Stat := Is_Static_Expression (N);
7970 Rewrite (N,
7971 Make_Real_Literal (Sloc (N),
7972 Realval => Small_Value (Typ) * Cint));
7973
7974 Set_Is_Static_Expression (N, Stat);
7975 end if;
7976
7977 -- In all cases, set the corresponding integer field
7978
7979 Set_Corresponding_Integer_Value (N, Cint);
7980 end;
7981 end if;
7982
7983 -- Now replace the actual type by the expected type as usual
7984
7985 Set_Etype (N, Typ);
7986 Eval_Real_Literal (N);
7987 end Resolve_Real_Literal;
7988
7989 -----------------------
7990 -- Resolve_Reference --
7991 -----------------------
7992
7993 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
7994 P : constant Node_Id := Prefix (N);
7995
7996 begin
7997 -- Replace general access with specific type
7998
7999 if Ekind (Etype (N)) = E_Allocator_Type then
8000 Set_Etype (N, Base_Type (Typ));
8001 end if;
8002
8003 Resolve (P, Designated_Type (Etype (N)));
8004
8005 -- If we are taking the reference of a volatile entity, then treat
8006 -- it as a potential modification of this entity. This is much too
8007 -- conservative, but is necessary because remove side effects can
8008 -- result in transformations of normal assignments into reference
8009 -- sequences that otherwise fail to notice the modification.
8010
8011 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8012 Note_Possible_Modification (P, Sure => False);
8013 end if;
8014 end Resolve_Reference;
8015
8016 --------------------------------
8017 -- Resolve_Selected_Component --
8018 --------------------------------
8019
8020 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8021 Comp : Entity_Id;
8022 Comp1 : Entity_Id := Empty; -- prevent junk warning
8023 P : constant Node_Id := Prefix (N);
8024 S : constant Node_Id := Selector_Name (N);
8025 T : Entity_Id := Etype (P);
8026 I : Interp_Index;
8027 I1 : Interp_Index := 0; -- prevent junk warning
8028 It : Interp;
8029 It1 : Interp;
8030 Found : Boolean;
8031
8032 function Init_Component return Boolean;
8033 -- Check whether this is the initialization of a component within an
8034 -- init proc (by assignment or call to another init proc). If true,
8035 -- there is no need for a discriminant check.
8036
8037 --------------------
8038 -- Init_Component --
8039 --------------------
8040
8041 function Init_Component return Boolean is
8042 begin
8043 return Inside_Init_Proc
8044 and then Nkind (Prefix (N)) = N_Identifier
8045 and then Chars (Prefix (N)) = Name_uInit
8046 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8047 end Init_Component;
8048
8049 -- Start of processing for Resolve_Selected_Component
8050
8051 begin
8052 if Is_Overloaded (P) then
8053
8054 -- Use the context type to select the prefix that has a selector
8055 -- of the correct name and type.
8056
8057 Found := False;
8058 Get_First_Interp (P, I, It);
8059
8060 Search : while Present (It.Typ) loop
8061 if Is_Access_Type (It.Typ) then
8062 T := Designated_Type (It.Typ);
8063 else
8064 T := It.Typ;
8065 end if;
8066
8067 if Is_Record_Type (T) then
8068
8069 -- The visible components of a class-wide type are those of
8070 -- the root type.
8071
8072 if Is_Class_Wide_Type (T) then
8073 T := Etype (T);
8074 end if;
8075
8076 Comp := First_Entity (T);
8077 while Present (Comp) loop
8078 if Chars (Comp) = Chars (S)
8079 and then Covers (Etype (Comp), Typ)
8080 then
8081 if not Found then
8082 Found := True;
8083 I1 := I;
8084 It1 := It;
8085 Comp1 := Comp;
8086
8087 else
8088 It := Disambiguate (P, I1, I, Any_Type);
8089
8090 if It = No_Interp then
8091 Error_Msg_N
8092 ("ambiguous prefix for selected component", N);
8093 Set_Etype (N, Typ);
8094 return;
8095
8096 else
8097 It1 := It;
8098
8099 -- There may be an implicit dereference. Retrieve
8100 -- designated record type.
8101
8102 if Is_Access_Type (It1.Typ) then
8103 T := Designated_Type (It1.Typ);
8104 else
8105 T := It1.Typ;
8106 end if;
8107
8108 if Scope (Comp1) /= T then
8109
8110 -- Resolution chooses the new interpretation.
8111 -- Find the component with the right name.
8112
8113 Comp1 := First_Entity (T);
8114 while Present (Comp1)
8115 and then Chars (Comp1) /= Chars (S)
8116 loop
8117 Comp1 := Next_Entity (Comp1);
8118 end loop;
8119 end if;
8120
8121 exit Search;
8122 end if;
8123 end if;
8124 end if;
8125
8126 Comp := Next_Entity (Comp);
8127 end loop;
8128 end if;
8129
8130 Get_Next_Interp (I, It);
8131 end loop Search;
8132
8133 Resolve (P, It1.Typ);
8134 Set_Etype (N, Typ);
8135 Set_Entity_With_Style_Check (S, Comp1);
8136
8137 else
8138 -- Resolve prefix with its type
8139
8140 Resolve (P, T);
8141 end if;
8142
8143 -- Generate cross-reference. We needed to wait until full overloading
8144 -- resolution was complete to do this, since otherwise we can't tell if
8145 -- we are an lvalue or not.
8146
8147 if May_Be_Lvalue (N) then
8148 Generate_Reference (Entity (S), S, 'm');
8149 else
8150 Generate_Reference (Entity (S), S, 'r');
8151 end if;
8152
8153 -- If prefix is an access type, the node will be transformed into an
8154 -- explicit dereference during expansion. The type of the node is the
8155 -- designated type of that of the prefix.
8156
8157 if Is_Access_Type (Etype (P)) then
8158 T := Designated_Type (Etype (P));
8159 Check_Fully_Declared_Prefix (T, P);
8160 else
8161 T := Etype (P);
8162 end if;
8163
8164 if Has_Discriminants (T)
8165 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8166 and then Present (Original_Record_Component (Entity (S)))
8167 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8168 and then Present (Discriminant_Checking_Func
8169 (Original_Record_Component (Entity (S))))
8170 and then not Discriminant_Checks_Suppressed (T)
8171 and then not Init_Component
8172 then
8173 Set_Do_Discriminant_Check (N);
8174 end if;
8175
8176 if Ekind (Entity (S)) = E_Void then
8177 Error_Msg_N ("premature use of component", S);
8178 end if;
8179
8180 -- If the prefix is a record conversion, this may be a renamed
8181 -- discriminant whose bounds differ from those of the original
8182 -- one, so we must ensure that a range check is performed.
8183
8184 if Nkind (P) = N_Type_Conversion
8185 and then Ekind (Entity (S)) = E_Discriminant
8186 and then Is_Discrete_Type (Typ)
8187 then
8188 Set_Etype (N, Base_Type (Typ));
8189 end if;
8190
8191 -- Note: No Eval processing is required, because the prefix is of a
8192 -- record type, or protected type, and neither can possibly be static.
8193
8194 -- If the array type is atomic, and is packed, and we are in a left side
8195 -- context, then this is worth a warning, since we have a situation
8196 -- where the access to the component may cause extra read/writes of
8197 -- the atomic array object, which could be considered unexpected.
8198
8199 if Nkind (N) = N_Selected_Component
8200 and then (Is_Atomic (T)
8201 or else (Is_Entity_Name (Prefix (N))
8202 and then Is_Atomic (Entity (Prefix (N)))))
8203 and then Is_Packed (T)
8204 and then Is_LHS (N)
8205 then
8206 Error_Msg_N ("?assignment to component of packed atomic record",
8207 Prefix (N));
8208 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8209 Prefix (N));
8210 end if;
8211 end Resolve_Selected_Component;
8212
8213 -------------------
8214 -- Resolve_Shift --
8215 -------------------
8216
8217 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8218 B_Typ : constant Entity_Id := Base_Type (Typ);
8219 L : constant Node_Id := Left_Opnd (N);
8220 R : constant Node_Id := Right_Opnd (N);
8221
8222 begin
8223 -- We do the resolution using the base type, because intermediate values
8224 -- in expressions always are of the base type, not a subtype of it.
8225
8226 Resolve (L, B_Typ);
8227 Resolve (R, Standard_Natural);
8228
8229 Check_Unset_Reference (L);
8230 Check_Unset_Reference (R);
8231
8232 Set_Etype (N, B_Typ);
8233 Generate_Operator_Reference (N, B_Typ);
8234 Eval_Shift (N);
8235 end Resolve_Shift;
8236
8237 ---------------------------
8238 -- Resolve_Short_Circuit --
8239 ---------------------------
8240
8241 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8242 B_Typ : constant Entity_Id := Base_Type (Typ);
8243 L : constant Node_Id := Left_Opnd (N);
8244 R : constant Node_Id := Right_Opnd (N);
8245
8246 begin
8247 Resolve (L, B_Typ);
8248 Resolve (R, B_Typ);
8249
8250 -- Check for issuing warning for always False assert/check, this happens
8251 -- when assertions are turned off, in which case the pragma Assert/Check
8252 -- was transformed into:
8253
8254 -- if False and then <condition> then ...
8255
8256 -- and we detect this pattern
8257
8258 if Warn_On_Assertion_Failure
8259 and then Is_Entity_Name (R)
8260 and then Entity (R) = Standard_False
8261 and then Nkind (Parent (N)) = N_If_Statement
8262 and then Nkind (N) = N_And_Then
8263 and then Is_Entity_Name (L)
8264 and then Entity (L) = Standard_False
8265 then
8266 declare
8267 Orig : constant Node_Id := Original_Node (Parent (N));
8268
8269 begin
8270 if Nkind (Orig) = N_Pragma
8271 and then Pragma_Name (Orig) = Name_Assert
8272 then
8273 -- Don't want to warn if original condition is explicit False
8274
8275 declare
8276 Expr : constant Node_Id :=
8277 Original_Node
8278 (Expression
8279 (First (Pragma_Argument_Associations (Orig))));
8280 begin
8281 if Is_Entity_Name (Expr)
8282 and then Entity (Expr) = Standard_False
8283 then
8284 null;
8285 else
8286 -- Issue warning. We do not want the deletion of the
8287 -- IF/AND-THEN to take this message with it. We achieve
8288 -- this by making sure that the expanded code points to
8289 -- the Sloc of the expression, not the original pragma.
8290
8291 Error_Msg_N
8292 ("?assertion would fail at run time!",
8293 Expression
8294 (First (Pragma_Argument_Associations (Orig))));
8295 end if;
8296 end;
8297
8298 -- Similar processing for Check pragma
8299
8300 elsif Nkind (Orig) = N_Pragma
8301 and then Pragma_Name (Orig) = Name_Check
8302 then
8303 -- Don't want to warn if original condition is explicit False
8304
8305 declare
8306 Expr : constant Node_Id :=
8307 Original_Node
8308 (Expression
8309 (Next (First
8310 (Pragma_Argument_Associations (Orig)))));
8311 begin
8312 if Is_Entity_Name (Expr)
8313 and then Entity (Expr) = Standard_False
8314 then
8315 null;
8316 else
8317 Error_Msg_N
8318 ("?check would fail at run time!",
8319 Expression
8320 (Last (Pragma_Argument_Associations (Orig))));
8321 end if;
8322 end;
8323 end if;
8324 end;
8325 end if;
8326
8327 -- Continue with processing of short circuit
8328
8329 Check_Unset_Reference (L);
8330 Check_Unset_Reference (R);
8331
8332 Set_Etype (N, B_Typ);
8333 Eval_Short_Circuit (N);
8334 end Resolve_Short_Circuit;
8335
8336 -------------------
8337 -- Resolve_Slice --
8338 -------------------
8339
8340 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8341 Name : constant Node_Id := Prefix (N);
8342 Drange : constant Node_Id := Discrete_Range (N);
8343 Array_Type : Entity_Id := Empty;
8344 Index : Node_Id;
8345
8346 begin
8347 if Is_Overloaded (Name) then
8348
8349 -- Use the context type to select the prefix that yields the correct
8350 -- array type.
8351
8352 declare
8353 I : Interp_Index;
8354 I1 : Interp_Index := 0;
8355 It : Interp;
8356 P : constant Node_Id := Prefix (N);
8357 Found : Boolean := False;
8358
8359 begin
8360 Get_First_Interp (P, I, It);
8361 while Present (It.Typ) loop
8362 if (Is_Array_Type (It.Typ)
8363 and then Covers (Typ, It.Typ))
8364 or else (Is_Access_Type (It.Typ)
8365 and then Is_Array_Type (Designated_Type (It.Typ))
8366 and then Covers (Typ, Designated_Type (It.Typ)))
8367 then
8368 if Found then
8369 It := Disambiguate (P, I1, I, Any_Type);
8370
8371 if It = No_Interp then
8372 Error_Msg_N ("ambiguous prefix for slicing", N);
8373 Set_Etype (N, Typ);
8374 return;
8375 else
8376 Found := True;
8377 Array_Type := It.Typ;
8378 I1 := I;
8379 end if;
8380 else
8381 Found := True;
8382 Array_Type := It.Typ;
8383 I1 := I;
8384 end if;
8385 end if;
8386
8387 Get_Next_Interp (I, It);
8388 end loop;
8389 end;
8390
8391 else
8392 Array_Type := Etype (Name);
8393 end if;
8394
8395 Resolve (Name, Array_Type);
8396
8397 if Is_Access_Type (Array_Type) then
8398 Apply_Access_Check (N);
8399 Array_Type := Designated_Type (Array_Type);
8400
8401 -- If the prefix is an access to an unconstrained array, we must use
8402 -- the actual subtype of the object to perform the index checks. The
8403 -- object denoted by the prefix is implicit in the node, so we build
8404 -- an explicit representation for it in order to compute the actual
8405 -- subtype.
8406
8407 if not Is_Constrained (Array_Type) then
8408 Remove_Side_Effects (Prefix (N));
8409
8410 declare
8411 Obj : constant Node_Id :=
8412 Make_Explicit_Dereference (Sloc (N),
8413 Prefix => New_Copy_Tree (Prefix (N)));
8414 begin
8415 Set_Etype (Obj, Array_Type);
8416 Set_Parent (Obj, Parent (N));
8417 Array_Type := Get_Actual_Subtype (Obj);
8418 end;
8419 end if;
8420
8421 elsif Is_Entity_Name (Name)
8422 or else Nkind (Name) = N_Explicit_Dereference
8423 or else (Nkind (Name) = N_Function_Call
8424 and then not Is_Constrained (Etype (Name)))
8425 then
8426 Array_Type := Get_Actual_Subtype (Name);
8427
8428 -- If the name is a selected component that depends on discriminants,
8429 -- build an actual subtype for it. This can happen only when the name
8430 -- itself is overloaded; otherwise the actual subtype is created when
8431 -- the selected component is analyzed.
8432
8433 elsif Nkind (Name) = N_Selected_Component
8434 and then Full_Analysis
8435 and then Depends_On_Discriminant (First_Index (Array_Type))
8436 then
8437 declare
8438 Act_Decl : constant Node_Id :=
8439 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8440 begin
8441 Insert_Action (N, Act_Decl);
8442 Array_Type := Defining_Identifier (Act_Decl);
8443 end;
8444
8445 -- Maybe this should just be "else", instead of checking for the
8446 -- specific case of slice??? This is needed for the case where
8447 -- the prefix is an Image attribute, which gets expanded to a
8448 -- slice, and so has a constrained subtype which we want to use
8449 -- for the slice range check applied below (the range check won't
8450 -- get done if the unconstrained subtype of the 'Image is used).
8451
8452 elsif Nkind (Name) = N_Slice then
8453 Array_Type := Etype (Name);
8454 end if;
8455
8456 -- If name was overloaded, set slice type correctly now
8457
8458 Set_Etype (N, Array_Type);
8459
8460 -- If the range is specified by a subtype mark, no resolution is
8461 -- necessary. Else resolve the bounds, and apply needed checks.
8462
8463 if not Is_Entity_Name (Drange) then
8464 Index := First_Index (Array_Type);
8465 Resolve (Drange, Base_Type (Etype (Index)));
8466
8467 if Nkind (Drange) = N_Range then
8468
8469 -- Ensure that side effects in the bounds are properly handled
8470
8471 Remove_Side_Effects (Low_Bound (Drange), Variable_Ref => True);
8472 Remove_Side_Effects (High_Bound (Drange), Variable_Ref => True);
8473
8474 -- Do not apply the range check to nodes associated with the
8475 -- frontend expansion of the dispatch table. We first check
8476 -- if Ada.Tags is already loaded to avoid the addition of an
8477 -- undesired dependence on such run-time unit.
8478
8479 if not Tagged_Type_Expansion
8480 or else not
8481 (RTU_Loaded (Ada_Tags)
8482 and then Nkind (Prefix (N)) = N_Selected_Component
8483 and then Present (Entity (Selector_Name (Prefix (N))))
8484 and then Entity (Selector_Name (Prefix (N))) =
8485 RTE_Record_Component (RE_Prims_Ptr))
8486 then
8487 Apply_Range_Check (Drange, Etype (Index));
8488 end if;
8489 end if;
8490 end if;
8491
8492 Set_Slice_Subtype (N);
8493
8494 -- Check bad use of type with predicates
8495
8496 if Has_Predicates (Etype (Drange)) then
8497 Bad_Predicated_Subtype_Use
8498 ("subtype& has predicate, not allowed in slice",
8499 Drange, Etype (Drange));
8500
8501 -- Otherwise here is where we check suspicious indexes
8502
8503 elsif Nkind (Drange) = N_Range then
8504 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8505 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8506 end if;
8507
8508 Eval_Slice (N);
8509 end Resolve_Slice;
8510
8511 ----------------------------
8512 -- Resolve_String_Literal --
8513 ----------------------------
8514
8515 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8516 C_Typ : constant Entity_Id := Component_Type (Typ);
8517 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8518 Loc : constant Source_Ptr := Sloc (N);
8519 Str : constant String_Id := Strval (N);
8520 Strlen : constant Nat := String_Length (Str);
8521 Subtype_Id : Entity_Id;
8522 Need_Check : Boolean;
8523
8524 begin
8525 -- For a string appearing in a concatenation, defer creation of the
8526 -- string_literal_subtype until the end of the resolution of the
8527 -- concatenation, because the literal may be constant-folded away. This
8528 -- is a useful optimization for long concatenation expressions.
8529
8530 -- If the string is an aggregate built for a single character (which
8531 -- happens in a non-static context) or a is null string to which special
8532 -- checks may apply, we build the subtype. Wide strings must also get a
8533 -- string subtype if they come from a one character aggregate. Strings
8534 -- generated by attributes might be static, but it is often hard to
8535 -- determine whether the enclosing context is static, so we generate
8536 -- subtypes for them as well, thus losing some rarer optimizations ???
8537 -- Same for strings that come from a static conversion.
8538
8539 Need_Check :=
8540 (Strlen = 0 and then Typ /= Standard_String)
8541 or else Nkind (Parent (N)) /= N_Op_Concat
8542 or else (N /= Left_Opnd (Parent (N))
8543 and then N /= Right_Opnd (Parent (N)))
8544 or else ((Typ = Standard_Wide_String
8545 or else Typ = Standard_Wide_Wide_String)
8546 and then Nkind (Original_Node (N)) /= N_String_Literal);
8547
8548 -- If the resolving type is itself a string literal subtype, we can just
8549 -- reuse it, since there is no point in creating another.
8550
8551 if Ekind (Typ) = E_String_Literal_Subtype then
8552 Subtype_Id := Typ;
8553
8554 elsif Nkind (Parent (N)) = N_Op_Concat
8555 and then not Need_Check
8556 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8557 N_Attribute_Reference,
8558 N_Qualified_Expression,
8559 N_Type_Conversion)
8560 then
8561 Subtype_Id := Typ;
8562
8563 -- Otherwise we must create a string literal subtype. Note that the
8564 -- whole idea of string literal subtypes is simply to avoid the need
8565 -- for building a full fledged array subtype for each literal.
8566
8567 else
8568 Set_String_Literal_Subtype (N, Typ);
8569 Subtype_Id := Etype (N);
8570 end if;
8571
8572 if Nkind (Parent (N)) /= N_Op_Concat
8573 or else Need_Check
8574 then
8575 Set_Etype (N, Subtype_Id);
8576 Eval_String_Literal (N);
8577 end if;
8578
8579 if Is_Limited_Composite (Typ)
8580 or else Is_Private_Composite (Typ)
8581 then
8582 Error_Msg_N ("string literal not available for private array", N);
8583 Set_Etype (N, Any_Type);
8584 return;
8585 end if;
8586
8587 -- The validity of a null string has been checked in the call to
8588 -- Eval_String_Literal.
8589
8590 if Strlen = 0 then
8591 return;
8592
8593 -- Always accept string literal with component type Any_Character, which
8594 -- occurs in error situations and in comparisons of literals, both of
8595 -- which should accept all literals.
8596
8597 elsif R_Typ = Any_Character then
8598 return;
8599
8600 -- If the type is bit-packed, then we always transform the string
8601 -- literal into a full fledged aggregate.
8602
8603 elsif Is_Bit_Packed_Array (Typ) then
8604 null;
8605
8606 -- Deal with cases of Wide_Wide_String, Wide_String, and String
8607
8608 else
8609 -- For Standard.Wide_Wide_String, or any other type whose component
8610 -- type is Standard.Wide_Wide_Character, we know that all the
8611 -- characters in the string must be acceptable, since the parser
8612 -- accepted the characters as valid character literals.
8613
8614 if R_Typ = Standard_Wide_Wide_Character then
8615 null;
8616
8617 -- For the case of Standard.String, or any other type whose component
8618 -- type is Standard.Character, we must make sure that there are no
8619 -- wide characters in the string, i.e. that it is entirely composed
8620 -- of characters in range of type Character.
8621
8622 -- If the string literal is the result of a static concatenation, the
8623 -- test has already been performed on the components, and need not be
8624 -- repeated.
8625
8626 elsif R_Typ = Standard_Character
8627 and then Nkind (Original_Node (N)) /= N_Op_Concat
8628 then
8629 for J in 1 .. Strlen loop
8630 if not In_Character_Range (Get_String_Char (Str, J)) then
8631
8632 -- If we are out of range, post error. This is one of the
8633 -- very few places that we place the flag in the middle of
8634 -- a token, right under the offending wide character. Not
8635 -- quite clear if this is right wrt wide character encoding
8636 -- sequences, but it's only an error message!
8637
8638 Error_Msg
8639 ("literal out of range of type Standard.Character",
8640 Source_Ptr (Int (Loc) + J));
8641 return;
8642 end if;
8643 end loop;
8644
8645 -- For the case of Standard.Wide_String, or any other type whose
8646 -- component type is Standard.Wide_Character, we must make sure that
8647 -- there are no wide characters in the string, i.e. that it is
8648 -- entirely composed of characters in range of type Wide_Character.
8649
8650 -- If the string literal is the result of a static concatenation,
8651 -- the test has already been performed on the components, and need
8652 -- not be repeated.
8653
8654 elsif R_Typ = Standard_Wide_Character
8655 and then Nkind (Original_Node (N)) /= N_Op_Concat
8656 then
8657 for J in 1 .. Strlen loop
8658 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8659
8660 -- If we are out of range, post error. This is one of the
8661 -- very few places that we place the flag in the middle of
8662 -- a token, right under the offending wide character.
8663
8664 -- This is not quite right, because characters in general
8665 -- will take more than one character position ???
8666
8667 Error_Msg
8668 ("literal out of range of type Standard.Wide_Character",
8669 Source_Ptr (Int (Loc) + J));
8670 return;
8671 end if;
8672 end loop;
8673
8674 -- If the root type is not a standard character, then we will convert
8675 -- the string into an aggregate and will let the aggregate code do
8676 -- the checking. Standard Wide_Wide_Character is also OK here.
8677
8678 else
8679 null;
8680 end if;
8681
8682 -- See if the component type of the array corresponding to the string
8683 -- has compile time known bounds. If yes we can directly check
8684 -- whether the evaluation of the string will raise constraint error.
8685 -- Otherwise we need to transform the string literal into the
8686 -- corresponding character aggregate and let the aggregate
8687 -- code do the checking.
8688
8689 if Is_Standard_Character_Type (R_Typ) then
8690
8691 -- Check for the case of full range, where we are definitely OK
8692
8693 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8694 return;
8695 end if;
8696
8697 -- Here the range is not the complete base type range, so check
8698
8699 declare
8700 Comp_Typ_Lo : constant Node_Id :=
8701 Type_Low_Bound (Component_Type (Typ));
8702 Comp_Typ_Hi : constant Node_Id :=
8703 Type_High_Bound (Component_Type (Typ));
8704
8705 Char_Val : Uint;
8706
8707 begin
8708 if Compile_Time_Known_Value (Comp_Typ_Lo)
8709 and then Compile_Time_Known_Value (Comp_Typ_Hi)
8710 then
8711 for J in 1 .. Strlen loop
8712 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
8713
8714 if Char_Val < Expr_Value (Comp_Typ_Lo)
8715 or else Char_Val > Expr_Value (Comp_Typ_Hi)
8716 then
8717 Apply_Compile_Time_Constraint_Error
8718 (N, "character out of range?", CE_Range_Check_Failed,
8719 Loc => Source_Ptr (Int (Loc) + J));
8720 end if;
8721 end loop;
8722
8723 return;
8724 end if;
8725 end;
8726 end if;
8727 end if;
8728
8729 -- If we got here we meed to transform the string literal into the
8730 -- equivalent qualified positional array aggregate. This is rather
8731 -- heavy artillery for this situation, but it is hard work to avoid.
8732
8733 declare
8734 Lits : constant List_Id := New_List;
8735 P : Source_Ptr := Loc + 1;
8736 C : Char_Code;
8737
8738 begin
8739 -- Build the character literals, we give them source locations that
8740 -- correspond to the string positions, which is a bit tricky given
8741 -- the possible presence of wide character escape sequences.
8742
8743 for J in 1 .. Strlen loop
8744 C := Get_String_Char (Str, J);
8745 Set_Character_Literal_Name (C);
8746
8747 Append_To (Lits,
8748 Make_Character_Literal (P,
8749 Chars => Name_Find,
8750 Char_Literal_Value => UI_From_CC (C)));
8751
8752 if In_Character_Range (C) then
8753 P := P + 1;
8754
8755 -- Should we have a call to Skip_Wide here ???
8756 -- ??? else
8757 -- Skip_Wide (P);
8758
8759 end if;
8760 end loop;
8761
8762 Rewrite (N,
8763 Make_Qualified_Expression (Loc,
8764 Subtype_Mark => New_Reference_To (Typ, Loc),
8765 Expression =>
8766 Make_Aggregate (Loc, Expressions => Lits)));
8767
8768 Analyze_And_Resolve (N, Typ);
8769 end;
8770 end Resolve_String_Literal;
8771
8772 -----------------------------
8773 -- Resolve_Subprogram_Info --
8774 -----------------------------
8775
8776 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
8777 begin
8778 Set_Etype (N, Typ);
8779 end Resolve_Subprogram_Info;
8780
8781 -----------------------------
8782 -- Resolve_Type_Conversion --
8783 -----------------------------
8784
8785 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
8786 Conv_OK : constant Boolean := Conversion_OK (N);
8787 Operand : constant Node_Id := Expression (N);
8788 Operand_Typ : constant Entity_Id := Etype (Operand);
8789 Target_Typ : constant Entity_Id := Etype (N);
8790 Rop : Node_Id;
8791 Orig_N : Node_Id;
8792 Orig_T : Node_Id;
8793
8794 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
8795 -- Set to False to suppress cases where we want to suppress the test
8796 -- for redundancy to avoid possible false positives on this warning.
8797
8798 begin
8799 if not Conv_OK
8800 and then not Valid_Conversion (N, Target_Typ, Operand)
8801 then
8802 return;
8803 end if;
8804
8805 -- If the Operand Etype is Universal_Fixed, then the conversion is
8806 -- never redundant. We need this check because by the time we have
8807 -- finished the rather complex transformation, the conversion looks
8808 -- redundant when it is not.
8809
8810 if Operand_Typ = Universal_Fixed then
8811 Test_Redundant := False;
8812
8813 -- If the operand is marked as Any_Fixed, then special processing is
8814 -- required. This is also a case where we suppress the test for a
8815 -- redundant conversion, since most certainly it is not redundant.
8816
8817 elsif Operand_Typ = Any_Fixed then
8818 Test_Redundant := False;
8819
8820 -- Mixed-mode operation involving a literal. Context must be a fixed
8821 -- type which is applied to the literal subsequently.
8822
8823 if Is_Fixed_Point_Type (Typ) then
8824 Set_Etype (Operand, Universal_Real);
8825
8826 elsif Is_Numeric_Type (Typ)
8827 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
8828 and then (Etype (Right_Opnd (Operand)) = Universal_Real
8829 or else
8830 Etype (Left_Opnd (Operand)) = Universal_Real)
8831 then
8832 -- Return if expression is ambiguous
8833
8834 if Unique_Fixed_Point_Type (N) = Any_Type then
8835 return;
8836
8837 -- If nothing else, the available fixed type is Duration
8838
8839 else
8840 Set_Etype (Operand, Standard_Duration);
8841 end if;
8842
8843 -- Resolve the real operand with largest available precision
8844
8845 if Etype (Right_Opnd (Operand)) = Universal_Real then
8846 Rop := New_Copy_Tree (Right_Opnd (Operand));
8847 else
8848 Rop := New_Copy_Tree (Left_Opnd (Operand));
8849 end if;
8850
8851 Resolve (Rop, Universal_Real);
8852
8853 -- If the operand is a literal (it could be a non-static and
8854 -- illegal exponentiation) check whether the use of Duration
8855 -- is potentially inaccurate.
8856
8857 if Nkind (Rop) = N_Real_Literal
8858 and then Realval (Rop) /= Ureal_0
8859 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
8860 then
8861 Error_Msg_N
8862 ("?universal real operand can only " &
8863 "be interpreted as Duration!",
8864 Rop);
8865 Error_Msg_N
8866 ("\?precision will be lost in the conversion!", Rop);
8867 end if;
8868
8869 elsif Is_Numeric_Type (Typ)
8870 and then Nkind (Operand) in N_Op
8871 and then Unique_Fixed_Point_Type (N) /= Any_Type
8872 then
8873 Set_Etype (Operand, Standard_Duration);
8874
8875 else
8876 Error_Msg_N ("invalid context for mixed mode operation", N);
8877 Set_Etype (Operand, Any_Type);
8878 return;
8879 end if;
8880 end if;
8881
8882 Resolve (Operand);
8883
8884 -- Note: we do the Eval_Type_Conversion call before applying the
8885 -- required checks for a subtype conversion. This is important, since
8886 -- both are prepared under certain circumstances to change the type
8887 -- conversion to a constraint error node, but in the case of
8888 -- Eval_Type_Conversion this may reflect an illegality in the static
8889 -- case, and we would miss the illegality (getting only a warning
8890 -- message), if we applied the type conversion checks first.
8891
8892 Eval_Type_Conversion (N);
8893
8894 -- Even when evaluation is not possible, we may be able to simplify the
8895 -- conversion or its expression. This needs to be done before applying
8896 -- checks, since otherwise the checks may use the original expression
8897 -- and defeat the simplifications. This is specifically the case for
8898 -- elimination of the floating-point Truncation attribute in
8899 -- float-to-int conversions.
8900
8901 Simplify_Type_Conversion (N);
8902
8903 -- If after evaluation we still have a type conversion, then we may need
8904 -- to apply checks required for a subtype conversion.
8905
8906 -- Skip these type conversion checks if universal fixed operands
8907 -- operands involved, since range checks are handled separately for
8908 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
8909
8910 if Nkind (N) = N_Type_Conversion
8911 and then not Is_Generic_Type (Root_Type (Target_Typ))
8912 and then Target_Typ /= Universal_Fixed
8913 and then Operand_Typ /= Universal_Fixed
8914 then
8915 Apply_Type_Conversion_Checks (N);
8916 end if;
8917
8918 -- Issue warning for conversion of simple object to its own type. We
8919 -- have to test the original nodes, since they may have been rewritten
8920 -- by various optimizations.
8921
8922 Orig_N := Original_Node (N);
8923
8924 -- Here we test for a redundant conversion if the warning mode is
8925 -- active (and was not locally reset), and we have a type conversion
8926 -- from source not appearing in a generic instance.
8927
8928 if Test_Redundant
8929 and then Nkind (Orig_N) = N_Type_Conversion
8930 and then Comes_From_Source (Orig_N)
8931 and then not In_Instance
8932 then
8933 Orig_N := Original_Node (Expression (Orig_N));
8934 Orig_T := Target_Typ;
8935
8936 -- If the node is part of a larger expression, the Target_Type
8937 -- may not be the original type of the node if the context is a
8938 -- condition. Recover original type to see if conversion is needed.
8939
8940 if Is_Boolean_Type (Orig_T)
8941 and then Nkind (Parent (N)) in N_Op
8942 then
8943 Orig_T := Etype (Parent (N));
8944 end if;
8945
8946 -- If we have an entity name, then give the warning if the entity
8947 -- is the right type, or if it is a loop parameter covered by the
8948 -- original type (that's needed because loop parameters have an
8949 -- odd subtype coming from the bounds).
8950
8951 if (Is_Entity_Name (Orig_N)
8952 and then
8953 (Etype (Entity (Orig_N)) = Orig_T
8954 or else
8955 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
8956 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
8957
8958 -- If not an entity, then type of expression must match
8959
8960 or else Etype (Orig_N) = Orig_T
8961 then
8962 -- One more check, do not give warning if the analyzed conversion
8963 -- has an expression with non-static bounds, and the bounds of the
8964 -- target are static. This avoids junk warnings in cases where the
8965 -- conversion is necessary to establish staticness, for example in
8966 -- a case statement.
8967
8968 if not Is_OK_Static_Subtype (Operand_Typ)
8969 and then Is_OK_Static_Subtype (Target_Typ)
8970 then
8971 null;
8972
8973 -- Finally, if this type conversion occurs in a context that
8974 -- requires a prefix, and the expression is a qualified expression
8975 -- then the type conversion is not redundant, because a qualified
8976 -- expression is not a prefix, whereas a type conversion is. For
8977 -- example, "X := T'(Funx(...)).Y;" is illegal because a selected
8978 -- component requires a prefix, but a type conversion makes it
8979 -- legal: "X := T(T'(Funx(...))).Y;"
8980
8981 -- In Ada 2012, a qualified expression is a name, so this idiom is
8982 -- no longer needed, but we still suppress the warning because it
8983 -- seems unfriendly for warnings to pop up when you switch to the
8984 -- newer language version.
8985
8986 elsif Nkind (Orig_N) = N_Qualified_Expression
8987 and then Nkind_In (Parent (N), N_Attribute_Reference,
8988 N_Indexed_Component,
8989 N_Selected_Component,
8990 N_Slice,
8991 N_Explicit_Dereference)
8992 then
8993 null;
8994
8995 -- Here we give the redundant conversion warning. If it is an
8996 -- entity, give the name of the entity in the message. If not,
8997 -- just mention the expression.
8998
8999 else
9000 if Is_Entity_Name (Orig_N) then
9001 Error_Msg_Node_2 := Orig_T;
9002 Error_Msg_NE -- CODEFIX
9003 ("?redundant conversion, & is of type &!",
9004 N, Entity (Orig_N));
9005 else
9006 Error_Msg_NE
9007 ("?redundant conversion, expression is of type&!",
9008 N, Orig_T);
9009 end if;
9010 end if;
9011 end if;
9012 end if;
9013
9014 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9015 -- No need to perform any interface conversion if the type of the
9016 -- expression coincides with the target type.
9017
9018 if Ada_Version >= Ada_2005
9019 and then Expander_Active
9020 and then Operand_Typ /= Target_Typ
9021 then
9022 declare
9023 Opnd : Entity_Id := Operand_Typ;
9024 Target : Entity_Id := Target_Typ;
9025
9026 begin
9027 if Is_Access_Type (Opnd) then
9028 Opnd := Designated_Type (Opnd);
9029 end if;
9030
9031 if Is_Access_Type (Target_Typ) then
9032 Target := Designated_Type (Target);
9033 end if;
9034
9035 if Opnd = Target then
9036 null;
9037
9038 -- Conversion from interface type
9039
9040 elsif Is_Interface (Opnd) then
9041
9042 -- Ada 2005 (AI-217): Handle entities from limited views
9043
9044 if From_With_Type (Opnd) 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 (Opnd))));
9049 Error_Msg_N
9050 ("type conversions require visibility of the full view",
9051 N);
9052
9053 elsif From_With_Type (Target)
9054 and then not
9055 (Is_Access_Type (Target_Typ)
9056 and then Present (Non_Limited_View (Etype (Target))))
9057 then
9058 Error_Msg_Qual_Level := 99;
9059 Error_Msg_NE -- CODEFIX
9060 ("missing WITH clause on package &", N,
9061 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9062 Error_Msg_N
9063 ("type conversions require visibility of the full view",
9064 N);
9065
9066 else
9067 Expand_Interface_Conversion (N, Is_Static => False);
9068 end if;
9069
9070 -- Conversion to interface type
9071
9072 elsif Is_Interface (Target) then
9073
9074 -- Handle subtypes
9075
9076 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9077 Opnd := Etype (Opnd);
9078 end if;
9079
9080 if not Interface_Present_In_Ancestor
9081 (Typ => Opnd,
9082 Iface => Target)
9083 then
9084 if Is_Class_Wide_Type (Opnd) then
9085
9086 -- The static analysis is not enough to know if the
9087 -- interface is implemented or not. Hence we must pass
9088 -- the work to the expander to generate code to evaluate
9089 -- the conversion at run time.
9090
9091 Expand_Interface_Conversion (N, Is_Static => False);
9092
9093 else
9094 Error_Msg_Name_1 := Chars (Etype (Target));
9095 Error_Msg_Name_2 := Chars (Opnd);
9096 Error_Msg_N
9097 ("wrong interface conversion (% is not a progenitor " &
9098 "of %)", N);
9099 end if;
9100
9101 else
9102 Expand_Interface_Conversion (N);
9103 end if;
9104 end if;
9105 end;
9106 end if;
9107 end Resolve_Type_Conversion;
9108
9109 ----------------------
9110 -- Resolve_Unary_Op --
9111 ----------------------
9112
9113 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9114 B_Typ : constant Entity_Id := Base_Type (Typ);
9115 R : constant Node_Id := Right_Opnd (N);
9116 OK : Boolean;
9117 Lo : Uint;
9118 Hi : Uint;
9119
9120 begin
9121 -- Deal with intrinsic unary operators
9122
9123 if Comes_From_Source (N)
9124 and then Ekind (Entity (N)) = E_Function
9125 and then Is_Imported (Entity (N))
9126 and then Is_Intrinsic_Subprogram (Entity (N))
9127 then
9128 Resolve_Intrinsic_Unary_Operator (N, Typ);
9129 return;
9130 end if;
9131
9132 -- Deal with universal cases
9133
9134 if Etype (R) = Universal_Integer
9135 or else
9136 Etype (R) = Universal_Real
9137 then
9138 Check_For_Visible_Operator (N, B_Typ);
9139 end if;
9140
9141 Set_Etype (N, B_Typ);
9142 Resolve (R, B_Typ);
9143
9144 -- Generate warning for expressions like abs (x mod 2)
9145
9146 if Warn_On_Redundant_Constructs
9147 and then Nkind (N) = N_Op_Abs
9148 then
9149 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9150
9151 if OK and then Hi >= Lo and then Lo >= 0 then
9152 Error_Msg_N -- CODEFIX
9153 ("?abs applied to known non-negative value has no effect", N);
9154 end if;
9155 end if;
9156
9157 -- Deal with reference generation
9158
9159 Check_Unset_Reference (R);
9160 Generate_Operator_Reference (N, B_Typ);
9161 Eval_Unary_Op (N);
9162
9163 -- Set overflow checking bit. Much cleverer code needed here eventually
9164 -- and perhaps the Resolve routines should be separated for the various
9165 -- arithmetic operations, since they will need different processing ???
9166
9167 if Nkind (N) in N_Op then
9168 if not Overflow_Checks_Suppressed (Etype (N)) then
9169 Enable_Overflow_Check (N);
9170 end if;
9171 end if;
9172
9173 -- Generate warning for expressions like -5 mod 3 for integers. No need
9174 -- to worry in the floating-point case, since parens do not affect the
9175 -- result so there is no point in giving in a warning.
9176
9177 declare
9178 Norig : constant Node_Id := Original_Node (N);
9179 Rorig : Node_Id;
9180 Val : Uint;
9181 HB : Uint;
9182 LB : Uint;
9183 Lval : Uint;
9184 Opnd : Node_Id;
9185
9186 begin
9187 if Warn_On_Questionable_Missing_Parens
9188 and then Comes_From_Source (Norig)
9189 and then Is_Integer_Type (Typ)
9190 and then Nkind (Norig) = N_Op_Minus
9191 then
9192 Rorig := Original_Node (Right_Opnd (Norig));
9193
9194 -- We are looking for cases where the right operand is not
9195 -- parenthesized, and is a binary operator, multiply, divide, or
9196 -- mod. These are the cases where the grouping can affect results.
9197
9198 if Paren_Count (Rorig) = 0
9199 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9200 then
9201 -- For mod, we always give the warning, since the value is
9202 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9203 -- -(5 mod 315)). But for the other cases, the only concern is
9204 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9205 -- overflows, but (-2) * 64 does not). So we try to give the
9206 -- message only when overflow is possible.
9207
9208 if Nkind (Rorig) /= N_Op_Mod
9209 and then Compile_Time_Known_Value (R)
9210 then
9211 Val := Expr_Value (R);
9212
9213 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9214 HB := Expr_Value (Type_High_Bound (Typ));
9215 else
9216 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9217 end if;
9218
9219 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9220 LB := Expr_Value (Type_Low_Bound (Typ));
9221 else
9222 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9223 end if;
9224
9225 -- Note that the test below is deliberately excluding the
9226 -- largest negative number, since that is a potentially
9227 -- troublesome case (e.g. -2 * x, where the result is the
9228 -- largest negative integer has an overflow with 2 * x).
9229
9230 if Val > LB and then Val <= HB then
9231 return;
9232 end if;
9233 end if;
9234
9235 -- For the multiplication case, the only case we have to worry
9236 -- about is when (-a)*b is exactly the largest negative number
9237 -- so that -(a*b) can cause overflow. This can only happen if
9238 -- a is a power of 2, and more generally if any operand is a
9239 -- constant that is not a power of 2, then the parentheses
9240 -- cannot affect whether overflow occurs. We only bother to
9241 -- test the left most operand
9242
9243 -- Loop looking at left operands for one that has known value
9244
9245 Opnd := Rorig;
9246 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9247 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9248 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9249
9250 -- Operand value of 0 or 1 skips warning
9251
9252 if Lval <= 1 then
9253 return;
9254
9255 -- Otherwise check power of 2, if power of 2, warn, if
9256 -- anything else, skip warning.
9257
9258 else
9259 while Lval /= 2 loop
9260 if Lval mod 2 = 1 then
9261 return;
9262 else
9263 Lval := Lval / 2;
9264 end if;
9265 end loop;
9266
9267 exit Opnd_Loop;
9268 end if;
9269 end if;
9270
9271 -- Keep looking at left operands
9272
9273 Opnd := Left_Opnd (Opnd);
9274 end loop Opnd_Loop;
9275
9276 -- For rem or "/" we can only have a problematic situation
9277 -- if the divisor has a value of minus one or one. Otherwise
9278 -- overflow is impossible (divisor > 1) or we have a case of
9279 -- division by zero in any case.
9280
9281 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9282 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9283 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9284 then
9285 return;
9286 end if;
9287
9288 -- If we fall through warning should be issued
9289
9290 Error_Msg_N
9291 ("?unary minus expression should be parenthesized here!", N);
9292 end if;
9293 end if;
9294 end;
9295 end Resolve_Unary_Op;
9296
9297 ----------------------------------
9298 -- Resolve_Unchecked_Expression --
9299 ----------------------------------
9300
9301 procedure Resolve_Unchecked_Expression
9302 (N : Node_Id;
9303 Typ : Entity_Id)
9304 is
9305 begin
9306 Resolve (Expression (N), Typ, Suppress => All_Checks);
9307 Set_Etype (N, Typ);
9308 end Resolve_Unchecked_Expression;
9309
9310 ---------------------------------------
9311 -- Resolve_Unchecked_Type_Conversion --
9312 ---------------------------------------
9313
9314 procedure Resolve_Unchecked_Type_Conversion
9315 (N : Node_Id;
9316 Typ : Entity_Id)
9317 is
9318 pragma Warnings (Off, Typ);
9319
9320 Operand : constant Node_Id := Expression (N);
9321 Opnd_Type : constant Entity_Id := Etype (Operand);
9322
9323 begin
9324 -- Resolve operand using its own type
9325
9326 Resolve (Operand, Opnd_Type);
9327 Eval_Unchecked_Conversion (N);
9328 end Resolve_Unchecked_Type_Conversion;
9329
9330 ------------------------------
9331 -- Rewrite_Operator_As_Call --
9332 ------------------------------
9333
9334 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9335 Loc : constant Source_Ptr := Sloc (N);
9336 Actuals : constant List_Id := New_List;
9337 New_N : Node_Id;
9338
9339 begin
9340 if Nkind (N) in N_Binary_Op then
9341 Append (Left_Opnd (N), Actuals);
9342 end if;
9343
9344 Append (Right_Opnd (N), Actuals);
9345
9346 New_N :=
9347 Make_Function_Call (Sloc => Loc,
9348 Name => New_Occurrence_Of (Nam, Loc),
9349 Parameter_Associations => Actuals);
9350
9351 Preserve_Comes_From_Source (New_N, N);
9352 Preserve_Comes_From_Source (Name (New_N), N);
9353 Rewrite (N, New_N);
9354 Set_Etype (N, Etype (Nam));
9355 end Rewrite_Operator_As_Call;
9356
9357 ------------------------------
9358 -- Rewrite_Renamed_Operator --
9359 ------------------------------
9360
9361 procedure Rewrite_Renamed_Operator
9362 (N : Node_Id;
9363 Op : Entity_Id;
9364 Typ : Entity_Id)
9365 is
9366 Nam : constant Name_Id := Chars (Op);
9367 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9368 Op_Node : Node_Id;
9369
9370 begin
9371 -- Rewrite the operator node using the real operator, not its renaming.
9372 -- Exclude user-defined intrinsic operations of the same name, which are
9373 -- treated separately and rewritten as calls.
9374
9375 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9376 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9377 Set_Chars (Op_Node, Nam);
9378 Set_Etype (Op_Node, Etype (N));
9379 Set_Entity (Op_Node, Op);
9380 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9381
9382 -- Indicate that both the original entity and its renaming are
9383 -- referenced at this point.
9384
9385 Generate_Reference (Entity (N), N);
9386 Generate_Reference (Op, N);
9387
9388 if Is_Binary then
9389 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9390 end if;
9391
9392 Rewrite (N, Op_Node);
9393
9394 -- If the context type is private, add the appropriate conversions so
9395 -- that the operator is applied to the full view. This is done in the
9396 -- routines that resolve intrinsic operators.
9397
9398 if Is_Intrinsic_Subprogram (Op)
9399 and then Is_Private_Type (Typ)
9400 then
9401 case Nkind (N) is
9402 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9403 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9404 Resolve_Intrinsic_Operator (N, Typ);
9405
9406 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9407 Resolve_Intrinsic_Unary_Operator (N, Typ);
9408
9409 when others =>
9410 Resolve (N, Typ);
9411 end case;
9412 end if;
9413
9414 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9415
9416 -- Operator renames a user-defined operator of the same name. Use the
9417 -- original operator in the node, which is the one Gigi knows about.
9418
9419 Set_Entity (N, Op);
9420 Set_Is_Overloaded (N, False);
9421 end if;
9422 end Rewrite_Renamed_Operator;
9423
9424 -----------------------
9425 -- Set_Slice_Subtype --
9426 -----------------------
9427
9428 -- Build an implicit subtype declaration to represent the type delivered by
9429 -- the slice. This is an abbreviated version of an array subtype. We define
9430 -- an index subtype for the slice, using either the subtype name or the
9431 -- discrete range of the slice. To be consistent with index usage elsewhere
9432 -- we create a list header to hold the single index. This list is not
9433 -- otherwise attached to the syntax tree.
9434
9435 procedure Set_Slice_Subtype (N : Node_Id) is
9436 Loc : constant Source_Ptr := Sloc (N);
9437 Index_List : constant List_Id := New_List;
9438 Index : Node_Id;
9439 Index_Subtype : Entity_Id;
9440 Index_Type : Entity_Id;
9441 Slice_Subtype : Entity_Id;
9442 Drange : constant Node_Id := Discrete_Range (N);
9443
9444 begin
9445 if Is_Entity_Name (Drange) then
9446 Index_Subtype := Entity (Drange);
9447
9448 else
9449 -- We force the evaluation of a range. This is definitely needed in
9450 -- the renamed case, and seems safer to do unconditionally. Note in
9451 -- any case that since we will create and insert an Itype referring
9452 -- to this range, we must make sure any side effect removal actions
9453 -- are inserted before the Itype definition.
9454
9455 if Nkind (Drange) = N_Range then
9456 Force_Evaluation (Low_Bound (Drange));
9457 Force_Evaluation (High_Bound (Drange));
9458 end if;
9459
9460 Index_Type := Base_Type (Etype (Drange));
9461
9462 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9463
9464 -- Take a new copy of Drange (where bounds have been rewritten to
9465 -- reference side-effect-free names). Using a separate tree ensures
9466 -- that further expansion (e.g. while rewriting a slice assignment
9467 -- into a FOR loop) does not attempt to remove side effects on the
9468 -- bounds again (which would cause the bounds in the index subtype
9469 -- definition to refer to temporaries before they are defined) (the
9470 -- reason is that some names are considered side effect free here
9471 -- for the subtype, but not in the context of a loop iteration
9472 -- scheme).
9473
9474 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9475 Set_Etype (Index_Subtype, Index_Type);
9476 Set_Size_Info (Index_Subtype, Index_Type);
9477 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9478 end if;
9479
9480 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9481
9482 Index := New_Occurrence_Of (Index_Subtype, Loc);
9483 Set_Etype (Index, Index_Subtype);
9484 Append (Index, Index_List);
9485
9486 Set_First_Index (Slice_Subtype, Index);
9487 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9488 Set_Is_Constrained (Slice_Subtype, True);
9489
9490 Check_Compile_Time_Size (Slice_Subtype);
9491
9492 -- The Etype of the existing Slice node is reset to this slice subtype.
9493 -- Its bounds are obtained from its first index.
9494
9495 Set_Etype (N, Slice_Subtype);
9496
9497 -- For packed slice subtypes, freeze immediately (except in the
9498 -- case of being in a "spec expression" where we never freeze
9499 -- when we first see the expression).
9500
9501 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9502 Freeze_Itype (Slice_Subtype, N);
9503
9504 -- For all other cases insert an itype reference in the slice's actions
9505 -- so that the itype is frozen at the proper place in the tree (i.e. at
9506 -- the point where actions for the slice are analyzed). Note that this
9507 -- is different from freezing the itype immediately, which might be
9508 -- premature (e.g. if the slice is within a transient scope).
9509
9510 else
9511 Ensure_Defined (Typ => Slice_Subtype, N => N);
9512 end if;
9513 end Set_Slice_Subtype;
9514
9515 --------------------------------
9516 -- Set_String_Literal_Subtype --
9517 --------------------------------
9518
9519 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9520 Loc : constant Source_Ptr := Sloc (N);
9521 Low_Bound : constant Node_Id :=
9522 Type_Low_Bound (Etype (First_Index (Typ)));
9523 Subtype_Id : Entity_Id;
9524
9525 begin
9526 if Nkind (N) /= N_String_Literal then
9527 return;
9528 end if;
9529
9530 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
9531 Set_String_Literal_Length (Subtype_Id, UI_From_Int
9532 (String_Length (Strval (N))));
9533 Set_Etype (Subtype_Id, Base_Type (Typ));
9534 Set_Is_Constrained (Subtype_Id);
9535 Set_Etype (N, Subtype_Id);
9536
9537 if Is_OK_Static_Expression (Low_Bound) then
9538
9539 -- The low bound is set from the low bound of the corresponding index
9540 -- type. Note that we do not store the high bound in the string literal
9541 -- subtype, but it can be deduced if necessary from the length and the
9542 -- low bound.
9543
9544 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
9545
9546 else
9547 Set_String_Literal_Low_Bound
9548 (Subtype_Id, Make_Integer_Literal (Loc, 1));
9549 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
9550
9551 -- Build bona fide subtype for the string, and wrap it in an
9552 -- unchecked conversion, because the backend expects the
9553 -- String_Literal_Subtype to have a static lower bound.
9554
9555 declare
9556 Index_List : constant List_Id := New_List;
9557 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
9558 High_Bound : constant Node_Id :=
9559 Make_Op_Add (Loc,
9560 Left_Opnd => New_Copy_Tree (Low_Bound),
9561 Right_Opnd =>
9562 Make_Integer_Literal (Loc,
9563 String_Length (Strval (N)) - 1));
9564 Array_Subtype : Entity_Id;
9565 Index_Subtype : Entity_Id;
9566 Drange : Node_Id;
9567 Index : Node_Id;
9568
9569 begin
9570 Index_Subtype :=
9571 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9572 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
9573 Set_Scalar_Range (Index_Subtype, Drange);
9574 Set_Parent (Drange, N);
9575 Analyze_And_Resolve (Drange, Index_Type);
9576
9577 -- In the context, the Index_Type may already have a constraint,
9578 -- so use common base type on string subtype. The base type may
9579 -- be used when generating attributes of the string, for example
9580 -- in the context of a slice assignment.
9581
9582 Set_Etype (Index_Subtype, Base_Type (Index_Type));
9583 Set_Size_Info (Index_Subtype, Index_Type);
9584 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9585
9586 Array_Subtype := Create_Itype (E_Array_Subtype, N);
9587
9588 Index := New_Occurrence_Of (Index_Subtype, Loc);
9589 Set_Etype (Index, Index_Subtype);
9590 Append (Index, Index_List);
9591
9592 Set_First_Index (Array_Subtype, Index);
9593 Set_Etype (Array_Subtype, Base_Type (Typ));
9594 Set_Is_Constrained (Array_Subtype, True);
9595
9596 Rewrite (N,
9597 Make_Unchecked_Type_Conversion (Loc,
9598 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9599 Expression => Relocate_Node (N)));
9600 Set_Etype (N, Array_Subtype);
9601 end;
9602 end if;
9603 end Set_String_Literal_Subtype;
9604
9605 ------------------------------
9606 -- Simplify_Type_Conversion --
9607 ------------------------------
9608
9609 procedure Simplify_Type_Conversion (N : Node_Id) is
9610 begin
9611 if Nkind (N) = N_Type_Conversion then
9612 declare
9613 Operand : constant Node_Id := Expression (N);
9614 Target_Typ : constant Entity_Id := Etype (N);
9615 Opnd_Typ : constant Entity_Id := Etype (Operand);
9616
9617 begin
9618 if Is_Floating_Point_Type (Opnd_Typ)
9619 and then
9620 (Is_Integer_Type (Target_Typ)
9621 or else (Is_Fixed_Point_Type (Target_Typ)
9622 and then Conversion_OK (N)))
9623 and then Nkind (Operand) = N_Attribute_Reference
9624 and then Attribute_Name (Operand) = Name_Truncation
9625
9626 -- Special processing required if the conversion is the expression
9627 -- of a Truncation attribute reference. In this case we replace:
9628
9629 -- ityp (ftyp'Truncation (x))
9630
9631 -- by
9632
9633 -- ityp (x)
9634
9635 -- with the Float_Truncate flag set, which is more efficient.
9636
9637 then
9638 Rewrite (Operand,
9639 Relocate_Node (First (Expressions (Operand))));
9640 Set_Float_Truncate (N, True);
9641 end if;
9642 end;
9643 end if;
9644 end Simplify_Type_Conversion;
9645
9646 -----------------------------
9647 -- Unique_Fixed_Point_Type --
9648 -----------------------------
9649
9650 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
9651 T1 : Entity_Id := Empty;
9652 T2 : Entity_Id;
9653 Item : Node_Id;
9654 Scop : Entity_Id;
9655
9656 procedure Fixed_Point_Error;
9657 -- Give error messages for true ambiguity. Messages are posted on node
9658 -- N, and entities T1, T2 are the possible interpretations.
9659
9660 -----------------------
9661 -- Fixed_Point_Error --
9662 -----------------------
9663
9664 procedure Fixed_Point_Error is
9665 begin
9666 Error_Msg_N ("ambiguous universal_fixed_expression", N);
9667 Error_Msg_NE ("\\possible interpretation as}", N, T1);
9668 Error_Msg_NE ("\\possible interpretation as}", N, T2);
9669 end Fixed_Point_Error;
9670
9671 -- Start of processing for Unique_Fixed_Point_Type
9672
9673 begin
9674 -- The operations on Duration are visible, so Duration is always a
9675 -- possible interpretation.
9676
9677 T1 := Standard_Duration;
9678
9679 -- Look for fixed-point types in enclosing scopes
9680
9681 Scop := Current_Scope;
9682 while Scop /= Standard_Standard loop
9683 T2 := First_Entity (Scop);
9684 while Present (T2) loop
9685 if Is_Fixed_Point_Type (T2)
9686 and then Current_Entity (T2) = T2
9687 and then Scope (Base_Type (T2)) = Scop
9688 then
9689 if Present (T1) then
9690 Fixed_Point_Error;
9691 return Any_Type;
9692 else
9693 T1 := T2;
9694 end if;
9695 end if;
9696
9697 Next_Entity (T2);
9698 end loop;
9699
9700 Scop := Scope (Scop);
9701 end loop;
9702
9703 -- Look for visible fixed type declarations in the context
9704
9705 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
9706 while Present (Item) loop
9707 if Nkind (Item) = N_With_Clause then
9708 Scop := Entity (Name (Item));
9709 T2 := First_Entity (Scop);
9710 while Present (T2) loop
9711 if Is_Fixed_Point_Type (T2)
9712 and then Scope (Base_Type (T2)) = Scop
9713 and then (Is_Potentially_Use_Visible (T2)
9714 or else In_Use (T2))
9715 then
9716 if Present (T1) then
9717 Fixed_Point_Error;
9718 return Any_Type;
9719 else
9720 T1 := T2;
9721 end if;
9722 end if;
9723
9724 Next_Entity (T2);
9725 end loop;
9726 end if;
9727
9728 Next (Item);
9729 end loop;
9730
9731 if Nkind (N) = N_Real_Literal then
9732 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
9733 else
9734 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
9735 end if;
9736
9737 return T1;
9738 end Unique_Fixed_Point_Type;
9739
9740 ----------------------
9741 -- Valid_Conversion --
9742 ----------------------
9743
9744 function Valid_Conversion
9745 (N : Node_Id;
9746 Target : Entity_Id;
9747 Operand : Node_Id) return Boolean
9748 is
9749 Target_Type : constant Entity_Id := Base_Type (Target);
9750 Opnd_Type : Entity_Id := Etype (Operand);
9751
9752 function Conversion_Check
9753 (Valid : Boolean;
9754 Msg : String) return Boolean;
9755 -- Little routine to post Msg if Valid is False, returns Valid value
9756
9757 function Valid_Tagged_Conversion
9758 (Target_Type : Entity_Id;
9759 Opnd_Type : Entity_Id) return Boolean;
9760 -- Specifically test for validity of tagged conversions
9761
9762 function Valid_Array_Conversion return Boolean;
9763 -- Check index and component conformance, and accessibility levels if
9764 -- the component types are anonymous access types (Ada 2005).
9765
9766 ----------------------
9767 -- Conversion_Check --
9768 ----------------------
9769
9770 function Conversion_Check
9771 (Valid : Boolean;
9772 Msg : String) return Boolean
9773 is
9774 begin
9775 if not Valid then
9776 Error_Msg_N (Msg, Operand);
9777 end if;
9778
9779 return Valid;
9780 end Conversion_Check;
9781
9782 ----------------------------
9783 -- Valid_Array_Conversion --
9784 ----------------------------
9785
9786 function Valid_Array_Conversion return Boolean
9787 is
9788 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
9789 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
9790
9791 Opnd_Index : Node_Id;
9792 Opnd_Index_Type : Entity_Id;
9793
9794 Target_Comp_Type : constant Entity_Id :=
9795 Component_Type (Target_Type);
9796 Target_Comp_Base : constant Entity_Id :=
9797 Base_Type (Target_Comp_Type);
9798
9799 Target_Index : Node_Id;
9800 Target_Index_Type : Entity_Id;
9801
9802 begin
9803 -- Error if wrong number of dimensions
9804
9805 if
9806 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
9807 then
9808 Error_Msg_N
9809 ("incompatible number of dimensions for conversion", Operand);
9810 return False;
9811
9812 -- Number of dimensions matches
9813
9814 else
9815 -- Loop through indexes of the two arrays
9816
9817 Target_Index := First_Index (Target_Type);
9818 Opnd_Index := First_Index (Opnd_Type);
9819 while Present (Target_Index) and then Present (Opnd_Index) loop
9820 Target_Index_Type := Etype (Target_Index);
9821 Opnd_Index_Type := Etype (Opnd_Index);
9822
9823 -- Error if index types are incompatible
9824
9825 if not (Is_Integer_Type (Target_Index_Type)
9826 and then Is_Integer_Type (Opnd_Index_Type))
9827 and then (Root_Type (Target_Index_Type)
9828 /= Root_Type (Opnd_Index_Type))
9829 then
9830 Error_Msg_N
9831 ("incompatible index types for array conversion",
9832 Operand);
9833 return False;
9834 end if;
9835
9836 Next_Index (Target_Index);
9837 Next_Index (Opnd_Index);
9838 end loop;
9839
9840 -- If component types have same base type, all set
9841
9842 if Target_Comp_Base = Opnd_Comp_Base then
9843 null;
9844
9845 -- Here if base types of components are not the same. The only
9846 -- time this is allowed is if we have anonymous access types.
9847
9848 -- The conversion of arrays of anonymous access types can lead
9849 -- to dangling pointers. AI-392 formalizes the accessibility
9850 -- checks that must be applied to such conversions to prevent
9851 -- out-of-scope references.
9852
9853 elsif
9854 Ekind_In (Target_Comp_Base, E_Anonymous_Access_Type,
9855 E_Anonymous_Access_Subprogram_Type)
9856 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
9857 and then
9858 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
9859 then
9860 if Type_Access_Level (Target_Type) <
9861 Type_Access_Level (Opnd_Type)
9862 then
9863 if In_Instance_Body then
9864 Error_Msg_N ("?source array type " &
9865 "has deeper accessibility level than target", Operand);
9866 Error_Msg_N ("\?Program_Error will be raised at run time",
9867 Operand);
9868 Rewrite (N,
9869 Make_Raise_Program_Error (Sloc (N),
9870 Reason => PE_Accessibility_Check_Failed));
9871 Set_Etype (N, Target_Type);
9872 return False;
9873
9874 -- Conversion not allowed because of accessibility levels
9875
9876 else
9877 Error_Msg_N ("source array type " &
9878 "has deeper accessibility level than target", Operand);
9879 return False;
9880 end if;
9881 else
9882 null;
9883 end if;
9884
9885 -- All other cases where component base types do not match
9886
9887 else
9888 Error_Msg_N
9889 ("incompatible component types for array conversion",
9890 Operand);
9891 return False;
9892 end if;
9893
9894 -- Check that component subtypes statically match. For numeric
9895 -- types this means that both must be either constrained or
9896 -- unconstrained. For enumeration types the bounds must match.
9897 -- All of this is checked in Subtypes_Statically_Match.
9898
9899 if not Subtypes_Statically_Match
9900 (Target_Comp_Type, Opnd_Comp_Type)
9901 then
9902 Error_Msg_N
9903 ("component subtypes must statically match", Operand);
9904 return False;
9905 end if;
9906 end if;
9907
9908 return True;
9909 end Valid_Array_Conversion;
9910
9911 -----------------------------
9912 -- Valid_Tagged_Conversion --
9913 -----------------------------
9914
9915 function Valid_Tagged_Conversion
9916 (Target_Type : Entity_Id;
9917 Opnd_Type : Entity_Id) return Boolean
9918 is
9919 begin
9920 -- Upward conversions are allowed (RM 4.6(22))
9921
9922 if Covers (Target_Type, Opnd_Type)
9923 or else Is_Ancestor (Target_Type, Opnd_Type)
9924 then
9925 return True;
9926
9927 -- Downward conversion are allowed if the operand is class-wide
9928 -- (RM 4.6(23)).
9929
9930 elsif Is_Class_Wide_Type (Opnd_Type)
9931 and then Covers (Opnd_Type, Target_Type)
9932 then
9933 return True;
9934
9935 elsif Covers (Opnd_Type, Target_Type)
9936 or else Is_Ancestor (Opnd_Type, Target_Type)
9937 then
9938 return
9939 Conversion_Check (False,
9940 "downward conversion of tagged objects not allowed");
9941
9942 -- Ada 2005 (AI-251): The conversion to/from interface types is
9943 -- always valid
9944
9945 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
9946 return True;
9947
9948 -- If the operand is a class-wide type obtained through a limited_
9949 -- with clause, and the context includes the non-limited view, use
9950 -- it to determine whether the conversion is legal.
9951
9952 elsif Is_Class_Wide_Type (Opnd_Type)
9953 and then From_With_Type (Opnd_Type)
9954 and then Present (Non_Limited_View (Etype (Opnd_Type)))
9955 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
9956 then
9957 return True;
9958
9959 elsif Is_Access_Type (Opnd_Type)
9960 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
9961 then
9962 return True;
9963
9964 else
9965 Error_Msg_NE
9966 ("invalid tagged conversion, not compatible with}",
9967 N, First_Subtype (Opnd_Type));
9968 return False;
9969 end if;
9970 end Valid_Tagged_Conversion;
9971
9972 -- Start of processing for Valid_Conversion
9973
9974 begin
9975 Check_Parameterless_Call (Operand);
9976
9977 if Is_Overloaded (Operand) then
9978 declare
9979 I : Interp_Index;
9980 I1 : Interp_Index;
9981 It : Interp;
9982 It1 : Interp;
9983 N1 : Entity_Id;
9984 T1 : Entity_Id;
9985
9986 begin
9987 -- Remove procedure calls, which syntactically cannot appear in
9988 -- this context, but which cannot be removed by type checking,
9989 -- because the context does not impose a type.
9990
9991 -- When compiling for VMS, spurious ambiguities can be produced
9992 -- when arithmetic operations have a literal operand and return
9993 -- System.Address or a descendant of it. These ambiguities are
9994 -- otherwise resolved by the context, but for conversions there
9995 -- is no context type and the removal of the spurious operations
9996 -- must be done explicitly here.
9997
9998 -- The node may be labelled overloaded, but still contain only one
9999 -- interpretation because others were discarded earlier. If this
10000 -- is the case, retain the single interpretation if legal.
10001
10002 Get_First_Interp (Operand, I, It);
10003 Opnd_Type := It.Typ;
10004 Get_Next_Interp (I, It);
10005
10006 if Present (It.Typ)
10007 and then Opnd_Type /= Standard_Void_Type
10008 then
10009 -- More than one candidate interpretation is available
10010
10011 Get_First_Interp (Operand, I, It);
10012 while Present (It.Typ) loop
10013 if It.Typ = Standard_Void_Type then
10014 Remove_Interp (I);
10015 end if;
10016
10017 if Present (System_Aux_Id)
10018 and then Is_Descendent_Of_Address (It.Typ)
10019 then
10020 Remove_Interp (I);
10021 end if;
10022
10023 Get_Next_Interp (I, It);
10024 end loop;
10025 end if;
10026
10027 Get_First_Interp (Operand, I, It);
10028 I1 := I;
10029 It1 := It;
10030
10031 if No (It.Typ) then
10032 Error_Msg_N ("illegal operand in conversion", Operand);
10033 return False;
10034 end if;
10035
10036 Get_Next_Interp (I, It);
10037
10038 if Present (It.Typ) then
10039 N1 := It1.Nam;
10040 T1 := It1.Typ;
10041 It1 := Disambiguate (Operand, I1, I, Any_Type);
10042
10043 if It1 = No_Interp then
10044 Error_Msg_N ("ambiguous operand in conversion", Operand);
10045
10046 -- If the interpretation involves a standard operator, use
10047 -- the location of the type, which may be user-defined.
10048
10049 if Sloc (It.Nam) = Standard_Location then
10050 Error_Msg_Sloc := Sloc (It.Typ);
10051 else
10052 Error_Msg_Sloc := Sloc (It.Nam);
10053 end if;
10054
10055 Error_Msg_N -- CODEFIX
10056 ("\\possible interpretation#!", Operand);
10057
10058 if Sloc (N1) = Standard_Location then
10059 Error_Msg_Sloc := Sloc (T1);
10060 else
10061 Error_Msg_Sloc := Sloc (N1);
10062 end if;
10063
10064 Error_Msg_N -- CODEFIX
10065 ("\\possible interpretation#!", Operand);
10066
10067 return False;
10068 end if;
10069 end if;
10070
10071 Set_Etype (Operand, It1.Typ);
10072 Opnd_Type := It1.Typ;
10073 end;
10074 end if;
10075
10076 -- Numeric types
10077
10078 if Is_Numeric_Type (Target_Type) then
10079
10080 -- A universal fixed expression can be converted to any numeric type
10081
10082 if Opnd_Type = Universal_Fixed then
10083 return True;
10084
10085 -- Also no need to check when in an instance or inlined body, because
10086 -- the legality has been established when the template was analyzed.
10087 -- Furthermore, numeric conversions may occur where only a private
10088 -- view of the operand type is visible at the instantiation point.
10089 -- This results in a spurious error if we check that the operand type
10090 -- is a numeric type.
10091
10092 -- Note: in a previous version of this unit, the following tests were
10093 -- applied only for generated code (Comes_From_Source set to False),
10094 -- but in fact the test is required for source code as well, since
10095 -- this situation can arise in source code.
10096
10097 elsif In_Instance or else In_Inlined_Body then
10098 return True;
10099
10100 -- Otherwise we need the conversion check
10101
10102 else
10103 return Conversion_Check
10104 (Is_Numeric_Type (Opnd_Type),
10105 "illegal operand for numeric conversion");
10106 end if;
10107
10108 -- Array types
10109
10110 elsif Is_Array_Type (Target_Type) then
10111 if not Is_Array_Type (Opnd_Type)
10112 or else Opnd_Type = Any_Composite
10113 or else Opnd_Type = Any_String
10114 then
10115 Error_Msg_N ("illegal operand for array conversion", Operand);
10116 return False;
10117 else
10118 return Valid_Array_Conversion;
10119 end if;
10120
10121 -- Ada 2005 (AI-251): Anonymous access types where target references an
10122 -- interface type.
10123
10124 elsif Ekind_In (Target_Type, E_General_Access_Type,
10125 E_Anonymous_Access_Type)
10126 and then Is_Interface (Directly_Designated_Type (Target_Type))
10127 then
10128 -- Check the static accessibility rule of 4.6(17). Note that the
10129 -- check is not enforced when within an instance body, since the
10130 -- RM requires such cases to be caught at run time.
10131
10132 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
10133 if Type_Access_Level (Opnd_Type) >
10134 Type_Access_Level (Target_Type)
10135 then
10136 -- In an instance, this is a run-time check, but one we know
10137 -- will fail, so generate an appropriate warning. The raise
10138 -- will be generated by Expand_N_Type_Conversion.
10139
10140 if In_Instance_Body then
10141 Error_Msg_N
10142 ("?cannot convert local pointer to non-local access type",
10143 Operand);
10144 Error_Msg_N
10145 ("\?Program_Error will be raised at run time", Operand);
10146 else
10147 Error_Msg_N
10148 ("cannot convert local pointer to non-local access type",
10149 Operand);
10150 return False;
10151 end if;
10152
10153 -- Special accessibility checks are needed in the case of access
10154 -- discriminants declared for a limited type.
10155
10156 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10157 and then not Is_Local_Anonymous_Access (Opnd_Type)
10158 then
10159 -- When the operand is a selected access discriminant the check
10160 -- needs to be made against the level of the object denoted by
10161 -- the prefix of the selected name (Object_Access_Level handles
10162 -- checking the prefix of the operand for this case).
10163
10164 if Nkind (Operand) = N_Selected_Component
10165 and then Object_Access_Level (Operand) >
10166 Type_Access_Level (Target_Type)
10167 then
10168 -- In an instance, this is a run-time check, but one we know
10169 -- will fail, so generate an appropriate warning. The raise
10170 -- will be generated by Expand_N_Type_Conversion.
10171
10172 if In_Instance_Body then
10173 Error_Msg_N
10174 ("?cannot convert access discriminant to non-local" &
10175 " access type", Operand);
10176 Error_Msg_N
10177 ("\?Program_Error will be raised at run time", Operand);
10178 else
10179 Error_Msg_N
10180 ("cannot convert access discriminant to non-local" &
10181 " access type", Operand);
10182 return False;
10183 end if;
10184 end if;
10185
10186 -- The case of a reference to an access discriminant from
10187 -- within a limited type declaration (which will appear as
10188 -- a discriminal) is always illegal because the level of the
10189 -- discriminant is considered to be deeper than any (nameable)
10190 -- access type.
10191
10192 if Is_Entity_Name (Operand)
10193 and then not Is_Local_Anonymous_Access (Opnd_Type)
10194 and then
10195 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10196 and then Present (Discriminal_Link (Entity (Operand)))
10197 then
10198 Error_Msg_N
10199 ("discriminant has deeper accessibility level than target",
10200 Operand);
10201 return False;
10202 end if;
10203 end if;
10204 end if;
10205
10206 return True;
10207
10208 -- General and anonymous access types
10209
10210 elsif Ekind_In (Target_Type, E_General_Access_Type,
10211 E_Anonymous_Access_Type)
10212 and then
10213 Conversion_Check
10214 (Is_Access_Type (Opnd_Type)
10215 and then not
10216 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10217 E_Access_Protected_Subprogram_Type),
10218 "must be an access-to-object type")
10219 then
10220 if Is_Access_Constant (Opnd_Type)
10221 and then not Is_Access_Constant (Target_Type)
10222 then
10223 Error_Msg_N
10224 ("access-to-constant operand type not allowed", Operand);
10225 return False;
10226 end if;
10227
10228 -- Check the static accessibility rule of 4.6(17). Note that the
10229 -- check is not enforced when within an instance body, since the RM
10230 -- requires such cases to be caught at run time.
10231
10232 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10233 or else Is_Local_Anonymous_Access (Target_Type)
10234 then
10235 if Type_Access_Level (Opnd_Type)
10236 > Type_Access_Level (Target_Type)
10237 then
10238 -- In an instance, this is a run-time check, but one we know
10239 -- will fail, so generate an appropriate warning. The raise
10240 -- will be generated by Expand_N_Type_Conversion.
10241
10242 if In_Instance_Body then
10243 Error_Msg_N
10244 ("?cannot convert local pointer to non-local access type",
10245 Operand);
10246 Error_Msg_N
10247 ("\?Program_Error will be raised at run time", Operand);
10248
10249 else
10250 -- Avoid generation of spurious error message
10251
10252 if not Error_Posted (N) then
10253 Error_Msg_N
10254 ("cannot convert local pointer to non-local access type",
10255 Operand);
10256 end if;
10257
10258 return False;
10259 end if;
10260
10261 -- Special accessibility checks are needed in the case of access
10262 -- discriminants declared for a limited type.
10263
10264 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10265 and then not Is_Local_Anonymous_Access (Opnd_Type)
10266 then
10267 -- When the operand is a selected access discriminant the check
10268 -- needs to be made against the level of the object denoted by
10269 -- the prefix of the selected name (Object_Access_Level handles
10270 -- checking the prefix of the operand for this case).
10271
10272 if Nkind (Operand) = N_Selected_Component
10273 and then Object_Access_Level (Operand) >
10274 Type_Access_Level (Target_Type)
10275 then
10276 -- In an instance, this is a run-time check, but one we know
10277 -- will fail, so generate an appropriate warning. The raise
10278 -- will be generated by Expand_N_Type_Conversion.
10279
10280 if In_Instance_Body then
10281 Error_Msg_N
10282 ("?cannot convert access discriminant to non-local" &
10283 " access type", Operand);
10284 Error_Msg_N
10285 ("\?Program_Error will be raised at run time",
10286 Operand);
10287
10288 else
10289 Error_Msg_N
10290 ("cannot convert access discriminant to non-local" &
10291 " access type", Operand);
10292 return False;
10293 end if;
10294 end if;
10295
10296 -- The case of a reference to an access discriminant from
10297 -- within a limited type declaration (which will appear as
10298 -- a discriminal) is always illegal because the level of the
10299 -- discriminant is considered to be deeper than any (nameable)
10300 -- access type.
10301
10302 if Is_Entity_Name (Operand)
10303 and then
10304 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10305 and then Present (Discriminal_Link (Entity (Operand)))
10306 then
10307 Error_Msg_N
10308 ("discriminant has deeper accessibility level than target",
10309 Operand);
10310 return False;
10311 end if;
10312 end if;
10313 end if;
10314
10315 -- In the presence of limited_with clauses we have to use non-limited
10316 -- views, if available.
10317
10318 Check_Limited : declare
10319 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10320 -- Helper function to handle limited views
10321
10322 --------------------------
10323 -- Full_Designated_Type --
10324 --------------------------
10325
10326 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10327 Desig : constant Entity_Id := Designated_Type (T);
10328
10329 begin
10330 -- Handle the limited view of a type
10331
10332 if Is_Incomplete_Type (Desig)
10333 and then From_With_Type (Desig)
10334 and then Present (Non_Limited_View (Desig))
10335 then
10336 return Available_View (Desig);
10337 else
10338 return Desig;
10339 end if;
10340 end Full_Designated_Type;
10341
10342 -- Local Declarations
10343
10344 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10345 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10346
10347 Same_Base : constant Boolean :=
10348 Base_Type (Target) = Base_Type (Opnd);
10349
10350 -- Start of processing for Check_Limited
10351
10352 begin
10353 if Is_Tagged_Type (Target) then
10354 return Valid_Tagged_Conversion (Target, Opnd);
10355
10356 else
10357 if not Same_Base then
10358 Error_Msg_NE
10359 ("target designated type not compatible with }",
10360 N, Base_Type (Opnd));
10361 return False;
10362
10363 -- Ada 2005 AI-384: legality rule is symmetric in both
10364 -- designated types. The conversion is legal (with possible
10365 -- constraint check) if either designated type is
10366 -- unconstrained.
10367
10368 elsif Subtypes_Statically_Match (Target, Opnd)
10369 or else
10370 (Has_Discriminants (Target)
10371 and then
10372 (not Is_Constrained (Opnd)
10373 or else not Is_Constrained (Target)))
10374 then
10375 -- Special case, if Value_Size has been used to make the
10376 -- sizes different, the conversion is not allowed even
10377 -- though the subtypes statically match.
10378
10379 if Known_Static_RM_Size (Target)
10380 and then Known_Static_RM_Size (Opnd)
10381 and then RM_Size (Target) /= RM_Size (Opnd)
10382 then
10383 Error_Msg_NE
10384 ("target designated subtype not compatible with }",
10385 N, Opnd);
10386 Error_Msg_NE
10387 ("\because sizes of the two designated subtypes differ",
10388 N, Opnd);
10389 return False;
10390
10391 -- Normal case where conversion is allowed
10392
10393 else
10394 return True;
10395 end if;
10396
10397 else
10398 Error_Msg_NE
10399 ("target designated subtype not compatible with }",
10400 N, Opnd);
10401 return False;
10402 end if;
10403 end if;
10404 end Check_Limited;
10405
10406 -- Access to subprogram types. If the operand is an access parameter,
10407 -- the type has a deeper accessibility that any master, and cannot be
10408 -- assigned. We must make an exception if the conversion is part of an
10409 -- assignment and the target is the return object of an extended return
10410 -- statement, because in that case the accessibility check takes place
10411 -- after the return.
10412
10413 elsif Is_Access_Subprogram_Type (Target_Type)
10414 and then No (Corresponding_Remote_Type (Opnd_Type))
10415 then
10416 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10417 and then Is_Entity_Name (Operand)
10418 and then Ekind (Entity (Operand)) = E_In_Parameter
10419 and then
10420 (Nkind (Parent (N)) /= N_Assignment_Statement
10421 or else not Is_Entity_Name (Name (Parent (N)))
10422 or else not Is_Return_Object (Entity (Name (Parent (N)))))
10423 then
10424 Error_Msg_N
10425 ("illegal attempt to store anonymous access to subprogram",
10426 Operand);
10427 Error_Msg_N
10428 ("\value has deeper accessibility than any master " &
10429 "(RM 3.10.2 (13))",
10430 Operand);
10431
10432 Error_Msg_NE
10433 ("\use named access type for& instead of access parameter",
10434 Operand, Entity (Operand));
10435 end if;
10436
10437 -- Check that the designated types are subtype conformant
10438
10439 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
10440 Old_Id => Designated_Type (Opnd_Type),
10441 Err_Loc => N);
10442
10443 -- Check the static accessibility rule of 4.6(20)
10444
10445 if Type_Access_Level (Opnd_Type) >
10446 Type_Access_Level (Target_Type)
10447 then
10448 Error_Msg_N
10449 ("operand type has deeper accessibility level than target",
10450 Operand);
10451
10452 -- Check that if the operand type is declared in a generic body,
10453 -- then the target type must be declared within that same body
10454 -- (enforces last sentence of 4.6(20)).
10455
10456 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
10457 declare
10458 O_Gen : constant Node_Id :=
10459 Enclosing_Generic_Body (Opnd_Type);
10460
10461 T_Gen : Node_Id;
10462
10463 begin
10464 T_Gen := Enclosing_Generic_Body (Target_Type);
10465 while Present (T_Gen) and then T_Gen /= O_Gen loop
10466 T_Gen := Enclosing_Generic_Body (T_Gen);
10467 end loop;
10468
10469 if T_Gen /= O_Gen then
10470 Error_Msg_N
10471 ("target type must be declared in same generic body"
10472 & " as operand type", N);
10473 end if;
10474 end;
10475 end if;
10476
10477 return True;
10478
10479 -- Remote subprogram access types
10480
10481 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
10482 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
10483 then
10484 -- It is valid to convert from one RAS type to another provided
10485 -- that their specification statically match.
10486
10487 Check_Subtype_Conformant
10488 (New_Id =>
10489 Designated_Type (Corresponding_Remote_Type (Target_Type)),
10490 Old_Id =>
10491 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
10492 Err_Loc =>
10493 N);
10494 return True;
10495
10496 -- If both are tagged types, check legality of view conversions
10497
10498 elsif Is_Tagged_Type (Target_Type)
10499 and then
10500 Is_Tagged_Type (Opnd_Type)
10501 then
10502 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
10503
10504 -- Types derived from the same root type are convertible
10505
10506 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
10507 return True;
10508
10509 -- In an instance or an inlined body, there may be inconsistent views of
10510 -- the same type, or of types derived from a common root.
10511
10512 elsif (In_Instance or In_Inlined_Body)
10513 and then
10514 Root_Type (Underlying_Type (Target_Type)) =
10515 Root_Type (Underlying_Type (Opnd_Type))
10516 then
10517 return True;
10518
10519 -- Special check for common access type error case
10520
10521 elsif Ekind (Target_Type) = E_Access_Type
10522 and then Is_Access_Type (Opnd_Type)
10523 then
10524 Error_Msg_N ("target type must be general access type!", N);
10525 Error_Msg_NE -- CODEFIX
10526 ("add ALL to }!", N, Target_Type);
10527 return False;
10528
10529 else
10530 Error_Msg_NE ("invalid conversion, not compatible with }",
10531 N, Opnd_Type);
10532 return False;
10533 end if;
10534 end Valid_Conversion;
10535
10536 end Sem_Res;