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