clean up and renames beginigs of a testsuite
[official-gcc.git] / gcc / ada / sem_res.adb
blob0917ccecf431842feb7efc7341dc15e8959fe1b1
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 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5281 Check_Obsolescent_2005_Entity (Nam, Subp);
5283 -- If subprogram name is a predefined operator, it was given in
5284 -- functional notation. Replace call node with operator node, so
5285 -- that actuals can be resolved appropriately.
5287 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5288 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5289 return;
5291 elsif Present (Alias (Nam))
5292 and then Is_Predefined_Op (Alias (Nam))
5293 then
5294 Resolve_Actuals (N, Nam);
5295 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5296 return;
5297 end if;
5299 -- Create a transient scope if the resulting type requires it
5301 -- There are several notable exceptions:
5303 -- a) In init procs, the transient scope overhead is not needed, and is
5304 -- even incorrect when the call is a nested initialization call for a
5305 -- component whose expansion may generate adjust calls. However, if the
5306 -- call is some other procedure call within an initialization procedure
5307 -- (for example a call to Create_Task in the init_proc of the task
5308 -- run-time record) a transient scope must be created around this call.
5310 -- b) Enumeration literal pseudo-calls need no transient scope
5312 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5313 -- functions) do not use the secondary stack even though the return
5314 -- type may be unconstrained.
5316 -- d) Calls to a build-in-place function, since such functions may
5317 -- allocate their result directly in a target object, and cases where
5318 -- the result does get allocated in the secondary stack are checked for
5319 -- within the specialized Exp_Ch6 procedures for expanding those
5320 -- build-in-place calls.
5322 -- e) If the subprogram is marked Inline_Always, then even if it returns
5323 -- an unconstrained type the call does not require use of the secondary
5324 -- stack. However, inlining will only take place if the body to inline
5325 -- is already present. It may not be available if e.g. the subprogram is
5326 -- declared in a child instance.
5328 -- If this is an initialization call for a type whose construction
5329 -- uses the secondary stack, and it is not a nested call to initialize
5330 -- a component, we do need to create a transient scope for it. We
5331 -- check for this by traversing the type in Check_Initialization_Call.
5333 if Is_Inlined (Nam)
5334 and then Has_Pragma_Inline_Always (Nam)
5335 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5336 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5337 then
5338 null;
5340 elsif Ekind (Nam) = E_Enumeration_Literal
5341 or else Is_Build_In_Place_Function (Nam)
5342 or else Is_Intrinsic_Subprogram (Nam)
5343 then
5344 null;
5346 elsif Expander_Active
5347 and then Is_Type (Etype (Nam))
5348 and then Requires_Transient_Scope (Etype (Nam))
5349 and then
5350 (not Within_Init_Proc
5351 or else
5352 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5353 then
5354 Establish_Transient_Scope (N, Sec_Stack => True);
5356 -- If the call appears within the bounds of a loop, it will
5357 -- be rewritten and reanalyzed, nothing left to do here.
5359 if Nkind (N) /= N_Function_Call then
5360 return;
5361 end if;
5363 elsif Is_Init_Proc (Nam)
5364 and then not Within_Init_Proc
5365 then
5366 Check_Initialization_Call (N, Nam);
5367 end if;
5369 -- A protected function cannot be called within the definition of the
5370 -- enclosing protected type.
5372 if Is_Protected_Type (Scope (Nam))
5373 and then In_Open_Scopes (Scope (Nam))
5374 and then not Has_Completion (Scope (Nam))
5375 then
5376 Error_Msg_NE
5377 ("& cannot be called before end of protected definition", N, Nam);
5378 end if;
5380 -- Propagate interpretation to actuals, and add default expressions
5381 -- where needed.
5383 if Present (First_Formal (Nam)) then
5384 Resolve_Actuals (N, Nam);
5386 -- Overloaded literals are rewritten as function calls, for purpose of
5387 -- resolution. After resolution, we can replace the call with the
5388 -- literal itself.
5390 elsif Ekind (Nam) = E_Enumeration_Literal then
5391 Copy_Node (Subp, N);
5392 Resolve_Entity_Name (N, Typ);
5394 -- Avoid validation, since it is a static function call
5396 Generate_Reference (Nam, Subp);
5397 return;
5398 end if;
5400 -- If the subprogram is not global, then kill all saved values and
5401 -- checks. This is a bit conservative, since in many cases we could do
5402 -- better, but it is not worth the effort. Similarly, we kill constant
5403 -- values. However we do not need to do this for internal entities
5404 -- (unless they are inherited user-defined subprograms), since they
5405 -- are not in the business of molesting local values.
5407 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5408 -- kill all checks and values for calls to global subprograms. This
5409 -- takes care of the case where an access to a local subprogram is
5410 -- taken, and could be passed directly or indirectly and then called
5411 -- from almost any context.
5413 -- Note: we do not do this step till after resolving the actuals. That
5414 -- way we still take advantage of the current value information while
5415 -- scanning the actuals.
5417 -- We suppress killing values if we are processing the nodes associated
5418 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5419 -- type kills all the values as part of analyzing the code that
5420 -- initializes the dispatch tables.
5422 if Inside_Freezing_Actions = 0
5423 and then (not Is_Library_Level_Entity (Nam)
5424 or else Suppress_Value_Tracking_On_Call
5425 (Nearest_Dynamic_Scope (Current_Scope)))
5426 and then (Comes_From_Source (Nam)
5427 or else (Present (Alias (Nam))
5428 and then Comes_From_Source (Alias (Nam))))
5429 then
5430 Kill_Current_Values;
5431 end if;
5433 -- If we are warning about unread OUT parameters, this is the place to
5434 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5435 -- after the above call to Kill_Current_Values (since that call clears
5436 -- the Last_Assignment field of all local variables).
5438 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5439 and then Comes_From_Source (N)
5440 and then In_Extended_Main_Source_Unit (N)
5441 then
5442 declare
5443 F : Entity_Id;
5444 A : Node_Id;
5446 begin
5447 F := First_Formal (Nam);
5448 A := First_Actual (N);
5449 while Present (F) and then Present (A) loop
5450 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5451 and then Warn_On_Modified_As_Out_Parameter (F)
5452 and then Is_Entity_Name (A)
5453 and then Present (Entity (A))
5454 and then Comes_From_Source (N)
5455 and then Safe_To_Capture_Value (N, Entity (A))
5456 then
5457 Set_Last_Assignment (Entity (A), A);
5458 end if;
5460 Next_Formal (F);
5461 Next_Actual (A);
5462 end loop;
5463 end;
5464 end if;
5466 -- If the subprogram is a primitive operation, check whether or not
5467 -- it is a correct dispatching call.
5469 if Is_Overloadable (Nam)
5470 and then Is_Dispatching_Operation (Nam)
5471 then
5472 Check_Dispatching_Call (N);
5474 elsif Ekind (Nam) /= E_Subprogram_Type
5475 and then Is_Abstract_Subprogram (Nam)
5476 and then not In_Instance
5477 then
5478 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5479 end if;
5481 -- If this is a dispatching call, generate the appropriate reference,
5482 -- for better source navigation in GPS.
5484 if Is_Overloadable (Nam)
5485 and then Present (Controlling_Argument (N))
5486 then
5487 Generate_Reference (Nam, Subp, 'R');
5489 -- Normal case, not a dispatching call
5491 else
5492 Generate_Reference (Nam, Subp);
5493 end if;
5495 if Is_Intrinsic_Subprogram (Nam) then
5496 Check_Intrinsic_Call (N);
5497 end if;
5499 -- Check for violation of restriction No_Specific_Termination_Handlers
5500 -- and warn on a potentially blocking call to Abort_Task.
5502 if Is_RTE (Nam, RE_Set_Specific_Handler)
5503 or else
5504 Is_RTE (Nam, RE_Specific_Handler)
5505 then
5506 Check_Restriction (No_Specific_Termination_Handlers, N);
5508 elsif Is_RTE (Nam, RE_Abort_Task) then
5509 Check_Potentially_Blocking_Operation (N);
5510 end if;
5512 -- Issue an error for a call to an eliminated subprogram. We skip this
5513 -- in a spec expression, e.g. a call in a default parameter value, since
5514 -- we are not really doing a call at this time. That's important because
5515 -- the spec expression may itself belong to an eliminated subprogram.
5517 if not In_Spec_Expression then
5518 Check_For_Eliminated_Subprogram (Subp, Nam);
5519 end if;
5521 -- All done, evaluate call and deal with elaboration issues
5523 Eval_Call (N);
5524 Check_Elab_Call (N);
5525 Warn_On_Overlapping_Actuals (Nam, N);
5526 end Resolve_Call;
5528 -----------------------------
5529 -- Resolve_Case_Expression --
5530 -----------------------------
5532 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5533 Alt : Node_Id;
5535 begin
5536 Alt := First (Alternatives (N));
5537 while Present (Alt) loop
5538 Resolve (Expression (Alt), Typ);
5539 Next (Alt);
5540 end loop;
5542 Set_Etype (N, Typ);
5543 Eval_Case_Expression (N);
5544 end Resolve_Case_Expression;
5546 -------------------------------
5547 -- Resolve_Character_Literal --
5548 -------------------------------
5550 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5551 B_Typ : constant Entity_Id := Base_Type (Typ);
5552 C : Entity_Id;
5554 begin
5555 -- Verify that the character does belong to the type of the context
5557 Set_Etype (N, B_Typ);
5558 Eval_Character_Literal (N);
5560 -- Wide_Wide_Character literals must always be defined, since the set
5561 -- of wide wide character literals is complete, i.e. if a character
5562 -- literal is accepted by the parser, then it is OK for wide wide
5563 -- character (out of range character literals are rejected).
5565 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5566 return;
5568 -- Always accept character literal for type Any_Character, which
5569 -- occurs in error situations and in comparisons of literals, both
5570 -- of which should accept all literals.
5572 elsif B_Typ = Any_Character then
5573 return;
5575 -- For Standard.Character or a type derived from it, check that
5576 -- the literal is in range
5578 elsif Root_Type (B_Typ) = Standard_Character then
5579 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5580 return;
5581 end if;
5583 -- For Standard.Wide_Character or a type derived from it, check
5584 -- that the literal is in range
5586 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5587 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5588 return;
5589 end if;
5591 -- For Standard.Wide_Wide_Character or a type derived from it, we
5592 -- know the literal is in range, since the parser checked!
5594 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5595 return;
5597 -- If the entity is already set, this has already been resolved in a
5598 -- generic context, or comes from expansion. Nothing else to do.
5600 elsif Present (Entity (N)) then
5601 return;
5603 -- Otherwise we have a user defined character type, and we can use the
5604 -- standard visibility mechanisms to locate the referenced entity.
5606 else
5607 C := Current_Entity (N);
5608 while Present (C) loop
5609 if Etype (C) = B_Typ then
5610 Set_Entity_With_Style_Check (N, C);
5611 Generate_Reference (C, N);
5612 return;
5613 end if;
5615 C := Homonym (C);
5616 end loop;
5617 end if;
5619 -- If we fall through, then the literal does not match any of the
5620 -- entries of the enumeration type. This isn't just a constraint
5621 -- error situation, it is an illegality (see RM 4.2).
5623 Error_Msg_NE
5624 ("character not defined for }", N, First_Subtype (B_Typ));
5625 end Resolve_Character_Literal;
5627 ---------------------------
5628 -- Resolve_Comparison_Op --
5629 ---------------------------
5631 -- Context requires a boolean type, and plays no role in resolution.
5632 -- Processing identical to that for equality operators. The result
5633 -- type is the base type, which matters when pathological subtypes of
5634 -- booleans with limited ranges are used.
5636 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5637 L : constant Node_Id := Left_Opnd (N);
5638 R : constant Node_Id := Right_Opnd (N);
5639 T : Entity_Id;
5641 begin
5642 -- If this is an intrinsic operation which is not predefined, use the
5643 -- types of its declared arguments to resolve the possibly overloaded
5644 -- operands. Otherwise the operands are unambiguous and specify the
5645 -- expected type.
5647 if Scope (Entity (N)) /= Standard_Standard then
5648 T := Etype (First_Entity (Entity (N)));
5650 else
5651 T := Find_Unique_Type (L, R);
5653 if T = Any_Fixed then
5654 T := Unique_Fixed_Point_Type (L);
5655 end if;
5656 end if;
5658 Set_Etype (N, Base_Type (Typ));
5659 Generate_Reference (T, N, ' ');
5661 if T /= Any_Type then
5662 if T = Any_String or else
5663 T = Any_Composite or else
5664 T = Any_Character
5665 then
5666 if T = Any_Character then
5667 Ambiguous_Character (L);
5668 else
5669 Error_Msg_N ("ambiguous operands for comparison", N);
5670 end if;
5672 Set_Etype (N, Any_Type);
5673 return;
5675 else
5676 Resolve (L, T);
5677 Resolve (R, T);
5678 Check_Unset_Reference (L);
5679 Check_Unset_Reference (R);
5680 Generate_Operator_Reference (N, T);
5681 Check_Low_Bound_Tested (N);
5682 Eval_Relational_Op (N);
5683 end if;
5684 end if;
5685 end Resolve_Comparison_Op;
5687 ------------------------------------
5688 -- Resolve_Conditional_Expression --
5689 ------------------------------------
5691 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5692 Condition : constant Node_Id := First (Expressions (N));
5693 Then_Expr : constant Node_Id := Next (Condition);
5694 Else_Expr : Node_Id := Next (Then_Expr);
5696 begin
5697 Resolve (Condition, Any_Boolean);
5698 Resolve (Then_Expr, Typ);
5700 -- If ELSE expression present, just resolve using the determined type
5702 if Present (Else_Expr) then
5703 Resolve (Else_Expr, Typ);
5705 -- If no ELSE expression is present, root type must be Standard.Boolean
5706 -- and we provide a Standard.True result converted to the appropriate
5707 -- Boolean type (in case it is a derived boolean type).
5709 elsif Root_Type (Typ) = Standard_Boolean then
5710 Else_Expr :=
5711 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
5712 Analyze_And_Resolve (Else_Expr, Typ);
5713 Append_To (Expressions (N), Else_Expr);
5715 else
5716 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
5717 Append_To (Expressions (N), Error);
5718 end if;
5720 Set_Etype (N, Typ);
5721 Eval_Conditional_Expression (N);
5722 end Resolve_Conditional_Expression;
5724 -----------------------------------------
5725 -- Resolve_Discrete_Subtype_Indication --
5726 -----------------------------------------
5728 procedure Resolve_Discrete_Subtype_Indication
5729 (N : Node_Id;
5730 Typ : Entity_Id)
5732 R : Node_Id;
5733 S : Entity_Id;
5735 begin
5736 Analyze (Subtype_Mark (N));
5737 S := Entity (Subtype_Mark (N));
5739 if Nkind (Constraint (N)) /= N_Range_Constraint then
5740 Error_Msg_N ("expect range constraint for discrete type", N);
5741 Set_Etype (N, Any_Type);
5743 else
5744 R := Range_Expression (Constraint (N));
5746 if R = Error then
5747 return;
5748 end if;
5750 Analyze (R);
5752 if Base_Type (S) /= Base_Type (Typ) then
5753 Error_Msg_NE
5754 ("expect subtype of }", N, First_Subtype (Typ));
5756 -- Rewrite the constraint as a range of Typ
5757 -- to allow compilation to proceed further.
5759 Set_Etype (N, Typ);
5760 Rewrite (Low_Bound (R),
5761 Make_Attribute_Reference (Sloc (Low_Bound (R)),
5762 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5763 Attribute_Name => Name_First));
5764 Rewrite (High_Bound (R),
5765 Make_Attribute_Reference (Sloc (High_Bound (R)),
5766 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5767 Attribute_Name => Name_First));
5769 else
5770 Resolve (R, Typ);
5771 Set_Etype (N, Etype (R));
5773 -- Additionally, we must check that the bounds are compatible
5774 -- with the given subtype, which might be different from the
5775 -- type of the context.
5777 Apply_Range_Check (R, S);
5779 -- ??? If the above check statically detects a Constraint_Error
5780 -- it replaces the offending bound(s) of the range R with a
5781 -- Constraint_Error node. When the itype which uses these bounds
5782 -- is frozen the resulting call to Duplicate_Subexpr generates
5783 -- a new temporary for the bounds.
5785 -- Unfortunately there are other itypes that are also made depend
5786 -- on these bounds, so when Duplicate_Subexpr is called they get
5787 -- a forward reference to the newly created temporaries and Gigi
5788 -- aborts on such forward references. This is probably sign of a
5789 -- more fundamental problem somewhere else in either the order of
5790 -- itype freezing or the way certain itypes are constructed.
5792 -- To get around this problem we call Remove_Side_Effects right
5793 -- away if either bounds of R are a Constraint_Error.
5795 declare
5796 L : constant Node_Id := Low_Bound (R);
5797 H : constant Node_Id := High_Bound (R);
5799 begin
5800 if Nkind (L) = N_Raise_Constraint_Error then
5801 Remove_Side_Effects (L);
5802 end if;
5804 if Nkind (H) = N_Raise_Constraint_Error then
5805 Remove_Side_Effects (H);
5806 end if;
5807 end;
5809 Check_Unset_Reference (Low_Bound (R));
5810 Check_Unset_Reference (High_Bound (R));
5811 end if;
5812 end if;
5813 end Resolve_Discrete_Subtype_Indication;
5815 -------------------------
5816 -- Resolve_Entity_Name --
5817 -------------------------
5819 -- Used to resolve identifiers and expanded names
5821 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
5822 E : constant Entity_Id := Entity (N);
5824 begin
5825 -- If garbage from errors, set to Any_Type and return
5827 if No (E) and then Total_Errors_Detected /= 0 then
5828 Set_Etype (N, Any_Type);
5829 return;
5830 end if;
5832 -- Replace named numbers by corresponding literals. Note that this is
5833 -- the one case where Resolve_Entity_Name must reset the Etype, since
5834 -- it is currently marked as universal.
5836 if Ekind (E) = E_Named_Integer then
5837 Set_Etype (N, Typ);
5838 Eval_Named_Integer (N);
5840 elsif Ekind (E) = E_Named_Real then
5841 Set_Etype (N, Typ);
5842 Eval_Named_Real (N);
5844 -- For enumeration literals, we need to make sure that a proper style
5845 -- check is done, since such literals are overloaded, and thus we did
5846 -- not do a style check during the first phase of analysis.
5848 elsif Ekind (E) = E_Enumeration_Literal then
5849 Set_Entity_With_Style_Check (N, E);
5850 Eval_Entity_Name (N);
5852 -- Allow use of subtype only if it is a concurrent type where we are
5853 -- currently inside the body. This will eventually be expanded into a
5854 -- call to Self (for tasks) or _object (for protected objects). Any
5855 -- other use of a subtype is invalid.
5857 elsif Is_Type (E) then
5858 if Is_Concurrent_Type (E)
5859 and then In_Open_Scopes (E)
5860 then
5861 null;
5862 else
5863 Error_Msg_N
5864 ("invalid use of subtype mark in expression or call", N);
5865 end if;
5867 -- Check discriminant use if entity is discriminant in current scope,
5868 -- i.e. discriminant of record or concurrent type currently being
5869 -- analyzed. Uses in corresponding body are unrestricted.
5871 elsif Ekind (E) = E_Discriminant
5872 and then Scope (E) = Current_Scope
5873 and then not Has_Completion (Current_Scope)
5874 then
5875 Check_Discriminant_Use (N);
5877 -- A parameterless generic function cannot appear in a context that
5878 -- requires resolution.
5880 elsif Ekind (E) = E_Generic_Function then
5881 Error_Msg_N ("illegal use of generic function", N);
5883 elsif Ekind (E) = E_Out_Parameter
5884 and then Ada_Version = Ada_83
5885 and then (Nkind (Parent (N)) in N_Op
5886 or else (Nkind (Parent (N)) = N_Assignment_Statement
5887 and then N = Expression (Parent (N)))
5888 or else Nkind (Parent (N)) = N_Explicit_Dereference)
5889 then
5890 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
5892 -- In all other cases, just do the possible static evaluation
5894 else
5895 -- A deferred constant that appears in an expression must have a
5896 -- completion, unless it has been removed by in-place expansion of
5897 -- an aggregate.
5899 if Ekind (E) = E_Constant
5900 and then Comes_From_Source (E)
5901 and then No (Constant_Value (E))
5902 and then Is_Frozen (Etype (E))
5903 and then not In_Spec_Expression
5904 and then not Is_Imported (E)
5905 then
5906 if No_Initialization (Parent (E))
5907 or else (Present (Full_View (E))
5908 and then No_Initialization (Parent (Full_View (E))))
5909 then
5910 null;
5911 else
5912 Error_Msg_N (
5913 "deferred constant is frozen before completion", N);
5914 end if;
5915 end if;
5917 Eval_Entity_Name (N);
5918 end if;
5919 end Resolve_Entity_Name;
5921 -------------------
5922 -- Resolve_Entry --
5923 -------------------
5925 procedure Resolve_Entry (Entry_Name : Node_Id) is
5926 Loc : constant Source_Ptr := Sloc (Entry_Name);
5927 Nam : Entity_Id;
5928 New_N : Node_Id;
5929 S : Entity_Id;
5930 Tsk : Entity_Id;
5931 E_Name : Node_Id;
5932 Index : Node_Id;
5934 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
5935 -- If the bounds of the entry family being called depend on task
5936 -- discriminants, build a new index subtype where a discriminant is
5937 -- replaced with the value of the discriminant of the target task.
5938 -- The target task is the prefix of the entry name in the call.
5940 -----------------------
5941 -- Actual_Index_Type --
5942 -----------------------
5944 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
5945 Typ : constant Entity_Id := Entry_Index_Type (E);
5946 Tsk : constant Entity_Id := Scope (E);
5947 Lo : constant Node_Id := Type_Low_Bound (Typ);
5948 Hi : constant Node_Id := Type_High_Bound (Typ);
5949 New_T : Entity_Id;
5951 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
5952 -- If the bound is given by a discriminant, replace with a reference
5953 -- to the discriminant of the same name in the target task. If the
5954 -- entry name is the target of a requeue statement and the entry is
5955 -- in the current protected object, the bound to be used is the
5956 -- discriminal of the object (see Apply_Range_Checks for details of
5957 -- the transformation).
5959 -----------------------------
5960 -- Actual_Discriminant_Ref --
5961 -----------------------------
5963 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
5964 Typ : constant Entity_Id := Etype (Bound);
5965 Ref : Node_Id;
5967 begin
5968 Remove_Side_Effects (Bound);
5970 if not Is_Entity_Name (Bound)
5971 or else Ekind (Entity (Bound)) /= E_Discriminant
5972 then
5973 return Bound;
5975 elsif Is_Protected_Type (Tsk)
5976 and then In_Open_Scopes (Tsk)
5977 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
5978 then
5979 -- Note: here Bound denotes a discriminant of the corresponding
5980 -- record type tskV, whose discriminal is a formal of the
5981 -- init-proc tskVIP. What we want is the body discriminal,
5982 -- which is associated to the discriminant of the original
5983 -- concurrent type tsk.
5985 return New_Occurrence_Of
5986 (Find_Body_Discriminal (Entity (Bound)), Loc);
5988 else
5989 Ref :=
5990 Make_Selected_Component (Loc,
5991 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
5992 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
5993 Analyze (Ref);
5994 Resolve (Ref, Typ);
5995 return Ref;
5996 end if;
5997 end Actual_Discriminant_Ref;
5999 -- Start of processing for Actual_Index_Type
6001 begin
6002 if not Has_Discriminants (Tsk)
6003 or else (not Is_Entity_Name (Lo)
6004 and then
6005 not Is_Entity_Name (Hi))
6006 then
6007 return Entry_Index_Type (E);
6009 else
6010 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6011 Set_Etype (New_T, Base_Type (Typ));
6012 Set_Size_Info (New_T, Typ);
6013 Set_RM_Size (New_T, RM_Size (Typ));
6014 Set_Scalar_Range (New_T,
6015 Make_Range (Sloc (Entry_Name),
6016 Low_Bound => Actual_Discriminant_Ref (Lo),
6017 High_Bound => Actual_Discriminant_Ref (Hi)));
6019 return New_T;
6020 end if;
6021 end Actual_Index_Type;
6023 -- Start of processing of Resolve_Entry
6025 begin
6026 -- Find name of entry being called, and resolve prefix of name
6027 -- with its own type. The prefix can be overloaded, and the name
6028 -- and signature of the entry must be taken into account.
6030 if Nkind (Entry_Name) = N_Indexed_Component then
6032 -- Case of dealing with entry family within the current tasks
6034 E_Name := Prefix (Entry_Name);
6036 else
6037 E_Name := Entry_Name;
6038 end if;
6040 if Is_Entity_Name (E_Name) then
6042 -- Entry call to an entry (or entry family) in the current task. This
6043 -- is legal even though the task will deadlock. Rewrite as call to
6044 -- current task.
6046 -- This can also be a call to an entry in an enclosing task. If this
6047 -- is a single task, we have to retrieve its name, because the scope
6048 -- of the entry is the task type, not the object. If the enclosing
6049 -- task is a task type, the identity of the task is given by its own
6050 -- self variable.
6052 -- Finally this can be a requeue on an entry of the same task or
6053 -- protected object.
6055 S := Scope (Entity (E_Name));
6057 for J in reverse 0 .. Scope_Stack.Last loop
6058 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6059 and then not Comes_From_Source (S)
6060 then
6061 -- S is an enclosing task or protected object. The concurrent
6062 -- declaration has been converted into a type declaration, and
6063 -- the object itself has an object declaration that follows
6064 -- the type in the same declarative part.
6066 Tsk := Next_Entity (S);
6067 while Etype (Tsk) /= S loop
6068 Next_Entity (Tsk);
6069 end loop;
6071 S := Tsk;
6072 exit;
6074 elsif S = Scope_Stack.Table (J).Entity then
6076 -- Call to current task. Will be transformed into call to Self
6078 exit;
6080 end if;
6081 end loop;
6083 New_N :=
6084 Make_Selected_Component (Loc,
6085 Prefix => New_Occurrence_Of (S, Loc),
6086 Selector_Name =>
6087 New_Occurrence_Of (Entity (E_Name), Loc));
6088 Rewrite (E_Name, New_N);
6089 Analyze (E_Name);
6091 elsif Nkind (Entry_Name) = N_Selected_Component
6092 and then Is_Overloaded (Prefix (Entry_Name))
6093 then
6094 -- Use the entry name (which must be unique at this point) to find
6095 -- the prefix that returns the corresponding task type or protected
6096 -- type.
6098 declare
6099 Pref : constant Node_Id := Prefix (Entry_Name);
6100 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6101 I : Interp_Index;
6102 It : Interp;
6104 begin
6105 Get_First_Interp (Pref, I, It);
6106 while Present (It.Typ) loop
6107 if Scope (Ent) = It.Typ then
6108 Set_Etype (Pref, It.Typ);
6109 exit;
6110 end if;
6112 Get_Next_Interp (I, It);
6113 end loop;
6114 end;
6115 end if;
6117 if Nkind (Entry_Name) = N_Selected_Component then
6118 Resolve (Prefix (Entry_Name));
6120 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6121 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6122 Resolve (Prefix (Prefix (Entry_Name)));
6123 Index := First (Expressions (Entry_Name));
6124 Resolve (Index, Entry_Index_Type (Nam));
6126 -- Up to this point the expression could have been the actual in a
6127 -- simple entry call, and be given by a named association.
6129 if Nkind (Index) = N_Parameter_Association then
6130 Error_Msg_N ("expect expression for entry index", Index);
6131 else
6132 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6133 end if;
6134 end if;
6135 end Resolve_Entry;
6137 ------------------------
6138 -- Resolve_Entry_Call --
6139 ------------------------
6141 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6142 Entry_Name : constant Node_Id := Name (N);
6143 Loc : constant Source_Ptr := Sloc (Entry_Name);
6144 Actuals : List_Id;
6145 First_Named : Node_Id;
6146 Nam : Entity_Id;
6147 Norm_OK : Boolean;
6148 Obj : Node_Id;
6149 Was_Over : Boolean;
6151 begin
6152 -- We kill all checks here, because it does not seem worth the effort to
6153 -- do anything better, an entry call is a big operation.
6155 Kill_All_Checks;
6157 -- Processing of the name is similar for entry calls and protected
6158 -- operation calls. Once the entity is determined, we can complete
6159 -- the resolution of the actuals.
6161 -- The selector may be overloaded, in the case of a protected object
6162 -- with overloaded functions. The type of the context is used for
6163 -- resolution.
6165 if Nkind (Entry_Name) = N_Selected_Component
6166 and then Is_Overloaded (Selector_Name (Entry_Name))
6167 and then Typ /= Standard_Void_Type
6168 then
6169 declare
6170 I : Interp_Index;
6171 It : Interp;
6173 begin
6174 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6175 while Present (It.Typ) loop
6176 if Covers (Typ, It.Typ) then
6177 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6178 Set_Etype (Entry_Name, It.Typ);
6180 Generate_Reference (It.Typ, N, ' ');
6181 end if;
6183 Get_Next_Interp (I, It);
6184 end loop;
6185 end;
6186 end if;
6188 Resolve_Entry (Entry_Name);
6190 if Nkind (Entry_Name) = N_Selected_Component then
6192 -- Simple entry call
6194 Nam := Entity (Selector_Name (Entry_Name));
6195 Obj := Prefix (Entry_Name);
6196 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6198 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6200 -- Call to member of entry family
6202 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6203 Obj := Prefix (Prefix (Entry_Name));
6204 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6205 end if;
6207 -- We cannot in general check the maximum depth of protected entry
6208 -- calls at compile time. But we can tell that any protected entry
6209 -- call at all violates a specified nesting depth of zero.
6211 if Is_Protected_Type (Scope (Nam)) then
6212 Check_Restriction (Max_Entry_Queue_Length, N);
6213 end if;
6215 -- Use context type to disambiguate a protected function that can be
6216 -- called without actuals and that returns an array type, and where
6217 -- the argument list may be an indexing of the returned value.
6219 if Ekind (Nam) = E_Function
6220 and then Needs_No_Actuals (Nam)
6221 and then Present (Parameter_Associations (N))
6222 and then
6223 ((Is_Array_Type (Etype (Nam))
6224 and then Covers (Typ, Component_Type (Etype (Nam))))
6226 or else (Is_Access_Type (Etype (Nam))
6227 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6228 and then Covers (Typ,
6229 Component_Type (Designated_Type (Etype (Nam))))))
6230 then
6231 declare
6232 Index_Node : Node_Id;
6234 begin
6235 Index_Node :=
6236 Make_Indexed_Component (Loc,
6237 Prefix =>
6238 Make_Function_Call (Loc,
6239 Name => Relocate_Node (Entry_Name)),
6240 Expressions => Parameter_Associations (N));
6242 -- Since we are correcting a node classification error made by
6243 -- the parser, we call Replace rather than Rewrite.
6245 Replace (N, Index_Node);
6246 Set_Etype (Prefix (N), Etype (Nam));
6247 Set_Etype (N, Typ);
6248 Resolve_Indexed_Component (N, Typ);
6249 return;
6250 end;
6251 end if;
6253 -- The operation name may have been overloaded. Order the actuals
6254 -- according to the formals of the resolved entity, and set the
6255 -- return type to that of the operation.
6257 if Was_Over then
6258 Normalize_Actuals (N, Nam, False, Norm_OK);
6259 pragma Assert (Norm_OK);
6260 Set_Etype (N, Etype (Nam));
6261 end if;
6263 Resolve_Actuals (N, Nam);
6264 Generate_Reference (Nam, Entry_Name);
6266 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6267 Check_Potentially_Blocking_Operation (N);
6268 end if;
6270 -- Verify that a procedure call cannot masquerade as an entry
6271 -- call where an entry call is expected.
6273 if Ekind (Nam) = E_Procedure then
6274 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6275 and then N = Entry_Call_Statement (Parent (N))
6276 then
6277 Error_Msg_N ("entry call required in select statement", N);
6279 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6280 and then N = Triggering_Statement (Parent (N))
6281 then
6282 Error_Msg_N ("triggering statement cannot be procedure call", N);
6284 elsif Ekind (Scope (Nam)) = E_Task_Type
6285 and then not In_Open_Scopes (Scope (Nam))
6286 then
6287 Error_Msg_N ("task has no entry with this name", Entry_Name);
6288 end if;
6289 end if;
6291 -- After resolution, entry calls and protected procedure calls are
6292 -- changed into entry calls, for expansion. The structure of the node
6293 -- does not change, so it can safely be done in place. Protected
6294 -- function calls must keep their structure because they are
6295 -- subexpressions.
6297 if Ekind (Nam) /= E_Function then
6299 -- A protected operation that is not a function may modify the
6300 -- corresponding object, and cannot apply to a constant. If this
6301 -- is an internal call, the prefix is the type itself.
6303 if Is_Protected_Type (Scope (Nam))
6304 and then not Is_Variable (Obj)
6305 and then (not Is_Entity_Name (Obj)
6306 or else not Is_Type (Entity (Obj)))
6307 then
6308 Error_Msg_N
6309 ("prefix of protected procedure or entry call must be variable",
6310 Entry_Name);
6311 end if;
6313 Actuals := Parameter_Associations (N);
6314 First_Named := First_Named_Actual (N);
6316 Rewrite (N,
6317 Make_Entry_Call_Statement (Loc,
6318 Name => Entry_Name,
6319 Parameter_Associations => Actuals));
6321 Set_First_Named_Actual (N, First_Named);
6322 Set_Analyzed (N, True);
6324 -- Protected functions can return on the secondary stack, in which
6325 -- case we must trigger the transient scope mechanism.
6327 elsif Expander_Active
6328 and then Requires_Transient_Scope (Etype (Nam))
6329 then
6330 Establish_Transient_Scope (N, Sec_Stack => True);
6331 end if;
6332 end Resolve_Entry_Call;
6334 -------------------------
6335 -- Resolve_Equality_Op --
6336 -------------------------
6338 -- Both arguments must have the same type, and the boolean context does
6339 -- not participate in the resolution. The first pass verifies that the
6340 -- interpretation is not ambiguous, and the type of the left argument is
6341 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6342 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6343 -- though they carry a single (universal) type. Diagnose this case here.
6345 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6346 L : constant Node_Id := Left_Opnd (N);
6347 R : constant Node_Id := Right_Opnd (N);
6348 T : Entity_Id := Find_Unique_Type (L, R);
6350 function Find_Unique_Access_Type return Entity_Id;
6351 -- In the case of allocators, make a last-ditch attempt to find a single
6352 -- access type with the right designated type. This is semantically
6353 -- dubious, and of no interest to any real code, but c48008a makes it
6354 -- all worthwhile.
6356 -----------------------------
6357 -- Find_Unique_Access_Type --
6358 -----------------------------
6360 function Find_Unique_Access_Type return Entity_Id is
6361 Acc : Entity_Id;
6362 E : Entity_Id;
6363 S : Entity_Id;
6365 begin
6366 if Ekind (Etype (R)) = E_Allocator_Type then
6367 Acc := Designated_Type (Etype (R));
6368 elsif Ekind (Etype (L)) = E_Allocator_Type then
6369 Acc := Designated_Type (Etype (L));
6370 else
6371 return Empty;
6372 end if;
6374 S := Current_Scope;
6375 while S /= Standard_Standard loop
6376 E := First_Entity (S);
6377 while Present (E) loop
6378 if Is_Type (E)
6379 and then Is_Access_Type (E)
6380 and then Ekind (E) /= E_Allocator_Type
6381 and then Designated_Type (E) = Base_Type (Acc)
6382 then
6383 return E;
6384 end if;
6386 Next_Entity (E);
6387 end loop;
6389 S := Scope (S);
6390 end loop;
6392 return Empty;
6393 end Find_Unique_Access_Type;
6395 -- Start of processing for Resolve_Equality_Op
6397 begin
6398 Set_Etype (N, Base_Type (Typ));
6399 Generate_Reference (T, N, ' ');
6401 if T = Any_Fixed then
6402 T := Unique_Fixed_Point_Type (L);
6403 end if;
6405 if T /= Any_Type then
6406 if T = Any_String
6407 or else T = Any_Composite
6408 or else T = Any_Character
6409 then
6410 if T = Any_Character then
6411 Ambiguous_Character (L);
6412 else
6413 Error_Msg_N ("ambiguous operands for equality", N);
6414 end if;
6416 Set_Etype (N, Any_Type);
6417 return;
6419 elsif T = Any_Access
6420 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6421 then
6422 T := Find_Unique_Access_Type;
6424 if No (T) then
6425 Error_Msg_N ("ambiguous operands for equality", N);
6426 Set_Etype (N, Any_Type);
6427 return;
6428 end if;
6429 end if;
6431 Resolve (L, T);
6432 Resolve (R, T);
6434 -- If the unique type is a class-wide type then it will be expanded
6435 -- into a dispatching call to the predefined primitive. Therefore we
6436 -- check here for potential violation of such restriction.
6438 if Is_Class_Wide_Type (T) then
6439 Check_Restriction (No_Dispatching_Calls, N);
6440 end if;
6442 if Warn_On_Redundant_Constructs
6443 and then Comes_From_Source (N)
6444 and then Is_Entity_Name (R)
6445 and then Entity (R) = Standard_True
6446 and then Comes_From_Source (R)
6447 then
6448 Error_Msg_N -- CODEFIX
6449 ("?comparison with True is redundant!", R);
6450 end if;
6452 Check_Unset_Reference (L);
6453 Check_Unset_Reference (R);
6454 Generate_Operator_Reference (N, T);
6455 Check_Low_Bound_Tested (N);
6457 -- If this is an inequality, it may be the implicit inequality
6458 -- created for a user-defined operation, in which case the corres-
6459 -- ponding equality operation is not intrinsic, and the operation
6460 -- cannot be constant-folded. Else fold.
6462 if Nkind (N) = N_Op_Eq
6463 or else Comes_From_Source (Entity (N))
6464 or else Ekind (Entity (N)) = E_Operator
6465 or else Is_Intrinsic_Subprogram
6466 (Corresponding_Equality (Entity (N)))
6467 then
6468 Eval_Relational_Op (N);
6470 elsif Nkind (N) = N_Op_Ne
6471 and then Is_Abstract_Subprogram (Entity (N))
6472 then
6473 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6474 end if;
6476 -- Ada 2005: If one operand is an anonymous access type, convert the
6477 -- other operand to it, to ensure that the underlying types match in
6478 -- the back-end. Same for access_to_subprogram, and the conversion
6479 -- verifies that the types are subtype conformant.
6481 -- We apply the same conversion in the case one of the operands is a
6482 -- private subtype of the type of the other.
6484 -- Why the Expander_Active test here ???
6486 if Expander_Active
6487 and then
6488 (Ekind_In (T, E_Anonymous_Access_Type,
6489 E_Anonymous_Access_Subprogram_Type)
6490 or else Is_Private_Type (T))
6491 then
6492 if Etype (L) /= T then
6493 Rewrite (L,
6494 Make_Unchecked_Type_Conversion (Sloc (L),
6495 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6496 Expression => Relocate_Node (L)));
6497 Analyze_And_Resolve (L, T);
6498 end if;
6500 if (Etype (R)) /= T then
6501 Rewrite (R,
6502 Make_Unchecked_Type_Conversion (Sloc (R),
6503 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6504 Expression => Relocate_Node (R)));
6505 Analyze_And_Resolve (R, T);
6506 end if;
6507 end if;
6508 end if;
6509 end Resolve_Equality_Op;
6511 ----------------------------------
6512 -- Resolve_Explicit_Dereference --
6513 ----------------------------------
6515 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6516 Loc : constant Source_Ptr := Sloc (N);
6517 New_N : Node_Id;
6518 P : constant Node_Id := Prefix (N);
6519 I : Interp_Index;
6520 It : Interp;
6522 begin
6523 Check_Fully_Declared_Prefix (Typ, P);
6525 if Is_Overloaded (P) then
6527 -- Use the context type to select the prefix that has the correct
6528 -- designated type.
6530 Get_First_Interp (P, I, It);
6531 while Present (It.Typ) loop
6532 exit when Is_Access_Type (It.Typ)
6533 and then Covers (Typ, Designated_Type (It.Typ));
6534 Get_Next_Interp (I, It);
6535 end loop;
6537 if Present (It.Typ) then
6538 Resolve (P, It.Typ);
6539 else
6540 -- If no interpretation covers the designated type of the prefix,
6541 -- this is the pathological case where not all implementations of
6542 -- the prefix allow the interpretation of the node as a call. Now
6543 -- that the expected type is known, Remove other interpretations
6544 -- from prefix, rewrite it as a call, and resolve again, so that
6545 -- the proper call node is generated.
6547 Get_First_Interp (P, I, It);
6548 while Present (It.Typ) loop
6549 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6550 Remove_Interp (I);
6551 end if;
6553 Get_Next_Interp (I, It);
6554 end loop;
6556 New_N :=
6557 Make_Function_Call (Loc,
6558 Name =>
6559 Make_Explicit_Dereference (Loc,
6560 Prefix => P),
6561 Parameter_Associations => New_List);
6563 Save_Interps (N, New_N);
6564 Rewrite (N, New_N);
6565 Analyze_And_Resolve (N, Typ);
6566 return;
6567 end if;
6569 Set_Etype (N, Designated_Type (It.Typ));
6571 else
6572 Resolve (P);
6573 end if;
6575 if Is_Access_Type (Etype (P)) then
6576 Apply_Access_Check (N);
6577 end if;
6579 -- If the designated type is a packed unconstrained array type, and the
6580 -- explicit dereference is not in the context of an attribute reference,
6581 -- then we must compute and set the actual subtype, since it is needed
6582 -- by Gigi. The reason we exclude the attribute case is that this is
6583 -- handled fine by Gigi, and in fact we use such attributes to build the
6584 -- actual subtype. We also exclude generated code (which builds actual
6585 -- subtypes directly if they are needed).
6587 if Is_Array_Type (Etype (N))
6588 and then Is_Packed (Etype (N))
6589 and then not Is_Constrained (Etype (N))
6590 and then Nkind (Parent (N)) /= N_Attribute_Reference
6591 and then Comes_From_Source (N)
6592 then
6593 Set_Etype (N, Get_Actual_Subtype (N));
6594 end if;
6596 -- Note: No Eval processing is required for an explicit dereference,
6597 -- because such a name can never be static.
6599 end Resolve_Explicit_Dereference;
6601 -------------------------------------
6602 -- Resolve_Expression_With_Actions --
6603 -------------------------------------
6605 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
6606 begin
6607 Set_Etype (N, Typ);
6608 end Resolve_Expression_With_Actions;
6610 -------------------------------
6611 -- Resolve_Indexed_Component --
6612 -------------------------------
6614 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6615 Name : constant Node_Id := Prefix (N);
6616 Expr : Node_Id;
6617 Array_Type : Entity_Id := Empty; -- to prevent junk warning
6618 Index : Node_Id;
6620 begin
6621 if Is_Overloaded (Name) then
6623 -- Use the context type to select the prefix that yields the correct
6624 -- component type.
6626 declare
6627 I : Interp_Index;
6628 It : Interp;
6629 I1 : Interp_Index := 0;
6630 P : constant Node_Id := Prefix (N);
6631 Found : Boolean := False;
6633 begin
6634 Get_First_Interp (P, I, It);
6635 while Present (It.Typ) loop
6636 if (Is_Array_Type (It.Typ)
6637 and then Covers (Typ, Component_Type (It.Typ)))
6638 or else (Is_Access_Type (It.Typ)
6639 and then Is_Array_Type (Designated_Type (It.Typ))
6640 and then Covers
6641 (Typ, Component_Type (Designated_Type (It.Typ))))
6642 then
6643 if Found then
6644 It := Disambiguate (P, I1, I, Any_Type);
6646 if It = No_Interp then
6647 Error_Msg_N ("ambiguous prefix for indexing", N);
6648 Set_Etype (N, Typ);
6649 return;
6651 else
6652 Found := True;
6653 Array_Type := It.Typ;
6654 I1 := I;
6655 end if;
6657 else
6658 Found := True;
6659 Array_Type := It.Typ;
6660 I1 := I;
6661 end if;
6662 end if;
6664 Get_Next_Interp (I, It);
6665 end loop;
6666 end;
6668 else
6669 Array_Type := Etype (Name);
6670 end if;
6672 Resolve (Name, Array_Type);
6673 Array_Type := Get_Actual_Subtype_If_Available (Name);
6675 -- If prefix is access type, dereference to get real array type.
6676 -- Note: we do not apply an access check because the expander always
6677 -- introduces an explicit dereference, and the check will happen there.
6679 if Is_Access_Type (Array_Type) then
6680 Array_Type := Designated_Type (Array_Type);
6681 end if;
6683 -- If name was overloaded, set component type correctly now
6684 -- If a misplaced call to an entry family (which has no index types)
6685 -- return. Error will be diagnosed from calling context.
6687 if Is_Array_Type (Array_Type) then
6688 Set_Etype (N, Component_Type (Array_Type));
6689 else
6690 return;
6691 end if;
6693 Index := First_Index (Array_Type);
6694 Expr := First (Expressions (N));
6696 -- The prefix may have resolved to a string literal, in which case its
6697 -- etype has a special representation. This is only possible currently
6698 -- if the prefix is a static concatenation, written in functional
6699 -- notation.
6701 if Ekind (Array_Type) = E_String_Literal_Subtype then
6702 Resolve (Expr, Standard_Positive);
6704 else
6705 while Present (Index) and Present (Expr) loop
6706 Resolve (Expr, Etype (Index));
6707 Check_Unset_Reference (Expr);
6709 if Is_Scalar_Type (Etype (Expr)) then
6710 Apply_Scalar_Range_Check (Expr, Etype (Index));
6711 else
6712 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
6713 end if;
6715 Next_Index (Index);
6716 Next (Expr);
6717 end loop;
6718 end if;
6720 -- Do not generate the warning on suspicious index if we are analyzing
6721 -- package Ada.Tags; otherwise we will report the warning with the
6722 -- Prims_Ptr field of the dispatch table.
6724 if Scope (Etype (Prefix (N))) = Standard_Standard
6725 or else not
6726 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
6727 Ada_Tags)
6728 then
6729 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
6730 Eval_Indexed_Component (N);
6731 end if;
6733 -- If the array type is atomic, and is packed, and we are in a left side
6734 -- context, then this is worth a warning, since we have a situation
6735 -- where the access to the component may cause extra read/writes of
6736 -- the atomic array object, which could be considered unexpected.
6738 if Nkind (N) = N_Indexed_Component
6739 and then (Is_Atomic (Array_Type)
6740 or else (Is_Entity_Name (Prefix (N))
6741 and then Is_Atomic (Entity (Prefix (N)))))
6742 and then Is_Bit_Packed_Array (Array_Type)
6743 and then Is_LHS (N)
6744 then
6745 Error_Msg_N ("?assignment to component of packed atomic array",
6746 Prefix (N));
6747 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
6748 Prefix (N));
6749 end if;
6750 end Resolve_Indexed_Component;
6752 -----------------------------
6753 -- Resolve_Integer_Literal --
6754 -----------------------------
6756 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
6757 begin
6758 Set_Etype (N, Typ);
6759 Eval_Integer_Literal (N);
6760 end Resolve_Integer_Literal;
6762 --------------------------------
6763 -- Resolve_Intrinsic_Operator --
6764 --------------------------------
6766 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
6767 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6768 Op : Entity_Id;
6769 Orig_Op : constant Entity_Id := Entity (N);
6770 Arg1 : Node_Id;
6771 Arg2 : Node_Id;
6773 begin
6774 -- We must preserve the original entity in a generic setting, so that
6775 -- the legality of the operation can be verified in an instance.
6777 if not Expander_Active then
6778 return;
6779 end if;
6781 Op := Entity (N);
6782 while Scope (Op) /= Standard_Standard loop
6783 Op := Homonym (Op);
6784 pragma Assert (Present (Op));
6785 end loop;
6787 Set_Entity (N, Op);
6788 Set_Is_Overloaded (N, False);
6790 -- If the operand type is private, rewrite with suitable conversions on
6791 -- the operands and the result, to expose the proper underlying numeric
6792 -- type.
6794 if Is_Private_Type (Typ) then
6795 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
6797 if Nkind (N) = N_Op_Expon then
6798 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
6799 else
6800 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6801 end if;
6803 if Nkind (Arg1) = N_Type_Conversion then
6804 Save_Interps (Left_Opnd (N), Expression (Arg1));
6805 end if;
6807 if Nkind (Arg2) = N_Type_Conversion then
6808 Save_Interps (Right_Opnd (N), Expression (Arg2));
6809 end if;
6811 Set_Left_Opnd (N, Arg1);
6812 Set_Right_Opnd (N, Arg2);
6814 Set_Etype (N, Btyp);
6815 Rewrite (N, Unchecked_Convert_To (Typ, N));
6816 Resolve (N, Typ);
6818 elsif Typ /= Etype (Left_Opnd (N))
6819 or else Typ /= Etype (Right_Opnd (N))
6820 then
6821 -- Add explicit conversion where needed, and save interpretations in
6822 -- case operands are overloaded. If the context is a VMS operation,
6823 -- assert that the conversion is legal (the operands have the proper
6824 -- types to select the VMS intrinsic). Note that in rare cases the
6825 -- VMS operators may be visible, but the default System is being used
6826 -- and Address is a private type.
6828 Arg1 := Convert_To (Typ, Left_Opnd (N));
6829 Arg2 := Convert_To (Typ, Right_Opnd (N));
6831 if Nkind (Arg1) = N_Type_Conversion then
6832 Save_Interps (Left_Opnd (N), Expression (Arg1));
6834 if Is_VMS_Operator (Orig_Op) then
6835 Set_Conversion_OK (Arg1);
6836 end if;
6837 else
6838 Save_Interps (Left_Opnd (N), Arg1);
6839 end if;
6841 if Nkind (Arg2) = N_Type_Conversion then
6842 Save_Interps (Right_Opnd (N), Expression (Arg2));
6844 if Is_VMS_Operator (Orig_Op) then
6845 Set_Conversion_OK (Arg2);
6846 end if;
6847 else
6848 Save_Interps (Right_Opnd (N), Arg2);
6849 end if;
6851 Rewrite (Left_Opnd (N), Arg1);
6852 Rewrite (Right_Opnd (N), Arg2);
6853 Analyze (Arg1);
6854 Analyze (Arg2);
6855 Resolve_Arithmetic_Op (N, Typ);
6857 else
6858 Resolve_Arithmetic_Op (N, Typ);
6859 end if;
6860 end Resolve_Intrinsic_Operator;
6862 --------------------------------------
6863 -- Resolve_Intrinsic_Unary_Operator --
6864 --------------------------------------
6866 procedure Resolve_Intrinsic_Unary_Operator
6867 (N : Node_Id;
6868 Typ : Entity_Id)
6870 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6871 Op : Entity_Id;
6872 Arg2 : Node_Id;
6874 begin
6875 Op := Entity (N);
6876 while Scope (Op) /= Standard_Standard loop
6877 Op := Homonym (Op);
6878 pragma Assert (Present (Op));
6879 end loop;
6881 Set_Entity (N, Op);
6883 if Is_Private_Type (Typ) then
6884 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6885 Save_Interps (Right_Opnd (N), Expression (Arg2));
6887 Set_Right_Opnd (N, Arg2);
6889 Set_Etype (N, Btyp);
6890 Rewrite (N, Unchecked_Convert_To (Typ, N));
6891 Resolve (N, Typ);
6893 else
6894 Resolve_Unary_Op (N, Typ);
6895 end if;
6896 end Resolve_Intrinsic_Unary_Operator;
6898 ------------------------
6899 -- Resolve_Logical_Op --
6900 ------------------------
6902 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
6903 B_Typ : Entity_Id;
6905 begin
6906 Check_No_Direct_Boolean_Operators (N);
6908 -- Predefined operations on scalar types yield the base type. On the
6909 -- other hand, logical operations on arrays yield the type of the
6910 -- arguments (and the context).
6912 if Is_Array_Type (Typ) then
6913 B_Typ := Typ;
6914 else
6915 B_Typ := Base_Type (Typ);
6916 end if;
6918 -- OK if this is a VMS-specific intrinsic operation
6920 if Is_VMS_Operator (Entity (N)) then
6921 null;
6923 -- The following test is required because the operands of the operation
6924 -- may be literals, in which case the resulting type appears to be
6925 -- compatible with a signed integer type, when in fact it is compatible
6926 -- only with modular types. If the context itself is universal, the
6927 -- operation is illegal.
6929 elsif not Valid_Boolean_Arg (Typ) then
6930 Error_Msg_N ("invalid context for logical operation", N);
6931 Set_Etype (N, Any_Type);
6932 return;
6934 elsif Typ = Any_Modular then
6935 Error_Msg_N
6936 ("no modular type available in this context", N);
6937 Set_Etype (N, Any_Type);
6938 return;
6939 elsif Is_Modular_Integer_Type (Typ)
6940 and then Etype (Left_Opnd (N)) = Universal_Integer
6941 and then Etype (Right_Opnd (N)) = Universal_Integer
6942 then
6943 Check_For_Visible_Operator (N, B_Typ);
6944 end if;
6946 Resolve (Left_Opnd (N), B_Typ);
6947 Resolve (Right_Opnd (N), B_Typ);
6949 Check_Unset_Reference (Left_Opnd (N));
6950 Check_Unset_Reference (Right_Opnd (N));
6952 Set_Etype (N, B_Typ);
6953 Generate_Operator_Reference (N, B_Typ);
6954 Eval_Logical_Op (N);
6955 end Resolve_Logical_Op;
6957 ---------------------------
6958 -- Resolve_Membership_Op --
6959 ---------------------------
6961 -- The context can only be a boolean type, and does not determine
6962 -- the arguments. Arguments should be unambiguous, but the preference
6963 -- rule for universal types applies.
6965 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
6966 pragma Warnings (Off, Typ);
6968 L : constant Node_Id := Left_Opnd (N);
6969 R : constant Node_Id := Right_Opnd (N);
6970 T : Entity_Id;
6972 procedure Resolve_Set_Membership;
6973 -- Analysis has determined a unique type for the left operand.
6974 -- Use it to resolve the disjuncts.
6976 ----------------------------
6977 -- Resolve_Set_Membership --
6978 ----------------------------
6980 procedure Resolve_Set_Membership is
6981 Alt : Node_Id;
6983 begin
6984 Resolve (L, Etype (L));
6986 Alt := First (Alternatives (N));
6987 while Present (Alt) loop
6989 -- Alternative is an expression, a range
6990 -- or a subtype mark.
6992 if not Is_Entity_Name (Alt)
6993 or else not Is_Type (Entity (Alt))
6994 then
6995 Resolve (Alt, Etype (L));
6996 end if;
6998 Next (Alt);
6999 end loop;
7000 end Resolve_Set_Membership;
7002 -- Start of processing for Resolve_Membership_Op
7004 begin
7005 if L = Error or else R = Error then
7006 return;
7007 end if;
7009 if Present (Alternatives (N)) then
7010 Resolve_Set_Membership;
7011 return;
7013 elsif not Is_Overloaded (R)
7014 and then
7015 (Etype (R) = Universal_Integer or else
7016 Etype (R) = Universal_Real)
7017 and then Is_Overloaded (L)
7018 then
7019 T := Etype (R);
7021 -- Ada 2005 (AI-251): Support the following case:
7023 -- type I is interface;
7024 -- type T is tagged ...
7026 -- function Test (O : I'Class) is
7027 -- begin
7028 -- return O in T'Class.
7029 -- end Test;
7031 -- In this case we have nothing else to do. The membership test will be
7032 -- done at run-time.
7034 elsif Ada_Version >= Ada_05
7035 and then Is_Class_Wide_Type (Etype (L))
7036 and then Is_Interface (Etype (L))
7037 and then Is_Class_Wide_Type (Etype (R))
7038 and then not Is_Interface (Etype (R))
7039 then
7040 return;
7042 else
7043 T := Intersect_Types (L, R);
7044 end if;
7046 -- If mixed-mode operations are present and operands are all literal,
7047 -- the only interpretation involves Duration, which is probably not
7048 -- the intention of the programmer.
7050 if T = Any_Fixed then
7051 T := Unique_Fixed_Point_Type (N);
7053 if T = Any_Type then
7054 return;
7055 end if;
7056 end if;
7058 Resolve (L, T);
7059 Check_Unset_Reference (L);
7061 if Nkind (R) = N_Range
7062 and then not Is_Scalar_Type (T)
7063 then
7064 Error_Msg_N ("scalar type required for range", R);
7065 end if;
7067 if Is_Entity_Name (R) then
7068 Freeze_Expression (R);
7069 else
7070 Resolve (R, T);
7071 Check_Unset_Reference (R);
7072 end if;
7074 Eval_Membership_Op (N);
7075 end Resolve_Membership_Op;
7077 ------------------
7078 -- Resolve_Null --
7079 ------------------
7081 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7082 Loc : constant Source_Ptr := Sloc (N);
7084 begin
7085 -- Handle restriction against anonymous null access values This
7086 -- restriction can be turned off using -gnatdj.
7088 -- Ada 2005 (AI-231): Remove restriction
7090 if Ada_Version < Ada_05
7091 and then not Debug_Flag_J
7092 and then Ekind (Typ) = E_Anonymous_Access_Type
7093 and then Comes_From_Source (N)
7094 then
7095 -- In the common case of a call which uses an explicitly null value
7096 -- for an access parameter, give specialized error message.
7098 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7099 N_Function_Call)
7100 then
7101 Error_Msg_N
7102 ("null is not allowed as argument for an access parameter", N);
7104 -- Standard message for all other cases (are there any?)
7106 else
7107 Error_Msg_N
7108 ("null cannot be of an anonymous access type", N);
7109 end if;
7110 end if;
7112 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7113 -- assignment to a null-excluding object
7115 if Ada_Version >= Ada_05
7116 and then Can_Never_Be_Null (Typ)
7117 and then Nkind (Parent (N)) = N_Assignment_Statement
7118 then
7119 if not Inside_Init_Proc then
7120 Insert_Action
7121 (Compile_Time_Constraint_Error (N,
7122 "(Ada 2005) null not allowed in null-excluding objects?"),
7123 Make_Raise_Constraint_Error (Loc,
7124 Reason => CE_Access_Check_Failed));
7125 else
7126 Insert_Action (N,
7127 Make_Raise_Constraint_Error (Loc,
7128 Reason => CE_Access_Check_Failed));
7129 end if;
7130 end if;
7132 -- In a distributed context, null for a remote access to subprogram may
7133 -- need to be replaced with a special record aggregate. In this case,
7134 -- return after having done the transformation.
7136 if (Ekind (Typ) = E_Record_Type
7137 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7138 and then Remote_AST_Null_Value (N, Typ)
7139 then
7140 return;
7141 end if;
7143 -- The null literal takes its type from the context
7145 Set_Etype (N, Typ);
7146 end Resolve_Null;
7148 -----------------------
7149 -- Resolve_Op_Concat --
7150 -----------------------
7152 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7154 -- We wish to avoid deep recursion, because concatenations are often
7155 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7156 -- operands nonrecursively until we find something that is not a simple
7157 -- concatenation (A in this case). We resolve that, and then walk back
7158 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7159 -- to do the rest of the work at each level. The Parent pointers allow
7160 -- us to avoid recursion, and thus avoid running out of memory. See also
7161 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7163 NN : Node_Id := N;
7164 Op1 : Node_Id;
7166 begin
7167 -- The following code is equivalent to:
7169 -- Resolve_Op_Concat_First (NN, Typ);
7170 -- Resolve_Op_Concat_Arg (N, ...);
7171 -- Resolve_Op_Concat_Rest (N, Typ);
7173 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7174 -- operand is a concatenation.
7176 -- Walk down left operands
7178 loop
7179 Resolve_Op_Concat_First (NN, Typ);
7180 Op1 := Left_Opnd (NN);
7181 exit when not (Nkind (Op1) = N_Op_Concat
7182 and then not Is_Array_Type (Component_Type (Typ))
7183 and then Entity (Op1) = Entity (NN));
7184 NN := Op1;
7185 end loop;
7187 -- Now (given the above example) NN is A&B and Op1 is A
7189 -- First resolve Op1 ...
7191 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7193 -- ... then walk NN back up until we reach N (where we started), calling
7194 -- Resolve_Op_Concat_Rest along the way.
7196 loop
7197 Resolve_Op_Concat_Rest (NN, Typ);
7198 exit when NN = N;
7199 NN := Parent (NN);
7200 end loop;
7201 end Resolve_Op_Concat;
7203 ---------------------------
7204 -- Resolve_Op_Concat_Arg --
7205 ---------------------------
7207 procedure Resolve_Op_Concat_Arg
7208 (N : Node_Id;
7209 Arg : Node_Id;
7210 Typ : Entity_Id;
7211 Is_Comp : Boolean)
7213 Btyp : constant Entity_Id := Base_Type (Typ);
7215 begin
7216 if In_Instance then
7217 if Is_Comp
7218 or else (not Is_Overloaded (Arg)
7219 and then Etype (Arg) /= Any_Composite
7220 and then Covers (Component_Type (Typ), Etype (Arg)))
7221 then
7222 Resolve (Arg, Component_Type (Typ));
7223 else
7224 Resolve (Arg, Btyp);
7225 end if;
7227 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
7228 if Nkind (Arg) = N_Aggregate
7229 and then Is_Composite_Type (Component_Type (Typ))
7230 then
7231 if Is_Private_Type (Component_Type (Typ)) then
7232 Resolve (Arg, Btyp);
7233 else
7234 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7235 Set_Etype (Arg, Any_Type);
7236 end if;
7238 else
7239 if Is_Overloaded (Arg)
7240 and then Has_Compatible_Type (Arg, Typ)
7241 and then Etype (Arg) /= Any_Type
7242 then
7243 declare
7244 I : Interp_Index;
7245 It : Interp;
7246 Func : Entity_Id;
7248 begin
7249 Get_First_Interp (Arg, I, It);
7250 Func := It.Nam;
7251 Get_Next_Interp (I, It);
7253 -- Special-case the error message when the overloading is
7254 -- caused by a function that yields an array and can be
7255 -- called without parameters.
7257 if It.Nam = Func then
7258 Error_Msg_Sloc := Sloc (Func);
7259 Error_Msg_N ("ambiguous call to function#", Arg);
7260 Error_Msg_NE
7261 ("\\interpretation as call yields&", Arg, Typ);
7262 Error_Msg_NE
7263 ("\\interpretation as indexing of call yields&",
7264 Arg, Component_Type (Typ));
7266 else
7267 Error_Msg_N
7268 ("ambiguous operand for concatenation!", Arg);
7269 Get_First_Interp (Arg, I, It);
7270 while Present (It.Nam) loop
7271 Error_Msg_Sloc := Sloc (It.Nam);
7273 if Base_Type (It.Typ) = Base_Type (Typ)
7274 or else Base_Type (It.Typ) =
7275 Base_Type (Component_Type (Typ))
7276 then
7277 Error_Msg_N -- CODEFIX
7278 ("\\possible interpretation#", Arg);
7279 end if;
7281 Get_Next_Interp (I, It);
7282 end loop;
7283 end if;
7284 end;
7285 end if;
7287 Resolve (Arg, Component_Type (Typ));
7289 if Nkind (Arg) = N_String_Literal then
7290 Set_Etype (Arg, Component_Type (Typ));
7291 end if;
7293 if Arg = Left_Opnd (N) then
7294 Set_Is_Component_Left_Opnd (N);
7295 else
7296 Set_Is_Component_Right_Opnd (N);
7297 end if;
7298 end if;
7300 else
7301 Resolve (Arg, Btyp);
7302 end if;
7304 Check_Unset_Reference (Arg);
7305 end Resolve_Op_Concat_Arg;
7307 -----------------------------
7308 -- Resolve_Op_Concat_First --
7309 -----------------------------
7311 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7312 Btyp : constant Entity_Id := Base_Type (Typ);
7313 Op1 : constant Node_Id := Left_Opnd (N);
7314 Op2 : constant Node_Id := Right_Opnd (N);
7316 begin
7317 -- The parser folds an enormous sequence of concatenations of string
7318 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
7319 -- in the right operand. If the expression resolves to a predefined "&"
7320 -- operator, all is well. Otherwise, the parser's folding is wrong, so
7321 -- we give an error. See P_Simple_Expression in Par.Ch4.
7323 if Nkind (Op2) = N_String_Literal
7324 and then Is_Folded_In_Parser (Op2)
7325 and then Ekind (Entity (N)) = E_Function
7326 then
7327 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
7328 and then String_Length (Strval (Op1)) = 0);
7329 Error_Msg_N ("too many user-defined concatenations", N);
7330 return;
7331 end if;
7333 Set_Etype (N, Btyp);
7335 if Is_Limited_Composite (Btyp) then
7336 Error_Msg_N ("concatenation not available for limited array", N);
7337 Explain_Limited_Type (Btyp, N);
7338 end if;
7339 end Resolve_Op_Concat_First;
7341 ----------------------------
7342 -- Resolve_Op_Concat_Rest --
7343 ----------------------------
7345 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7346 Op1 : constant Node_Id := Left_Opnd (N);
7347 Op2 : constant Node_Id := Right_Opnd (N);
7349 begin
7350 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
7352 Generate_Operator_Reference (N, Typ);
7354 if Is_String_Type (Typ) then
7355 Eval_Concatenation (N);
7356 end if;
7358 -- If this is not a static concatenation, but the result is a string
7359 -- type (and not an array of strings) ensure that static string operands
7360 -- have their subtypes properly constructed.
7362 if Nkind (N) /= N_String_Literal
7363 and then Is_Character_Type (Component_Type (Typ))
7364 then
7365 Set_String_Literal_Subtype (Op1, Typ);
7366 Set_String_Literal_Subtype (Op2, Typ);
7367 end if;
7368 end Resolve_Op_Concat_Rest;
7370 ----------------------
7371 -- Resolve_Op_Expon --
7372 ----------------------
7374 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7375 B_Typ : constant Entity_Id := Base_Type (Typ);
7377 begin
7378 -- Catch attempts to do fixed-point exponentiation with universal
7379 -- operands, which is a case where the illegality is not caught during
7380 -- normal operator analysis.
7382 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7383 Error_Msg_N ("exponentiation not available for fixed point", N);
7384 return;
7385 end if;
7387 if Comes_From_Source (N)
7388 and then Ekind (Entity (N)) = E_Function
7389 and then Is_Imported (Entity (N))
7390 and then Is_Intrinsic_Subprogram (Entity (N))
7391 then
7392 Resolve_Intrinsic_Operator (N, Typ);
7393 return;
7394 end if;
7396 if Etype (Left_Opnd (N)) = Universal_Integer
7397 or else Etype (Left_Opnd (N)) = Universal_Real
7398 then
7399 Check_For_Visible_Operator (N, B_Typ);
7400 end if;
7402 -- We do the resolution using the base type, because intermediate values
7403 -- in expressions always are of the base type, not a subtype of it.
7405 Resolve (Left_Opnd (N), B_Typ);
7406 Resolve (Right_Opnd (N), Standard_Integer);
7408 Check_Unset_Reference (Left_Opnd (N));
7409 Check_Unset_Reference (Right_Opnd (N));
7411 Set_Etype (N, B_Typ);
7412 Generate_Operator_Reference (N, B_Typ);
7413 Eval_Op_Expon (N);
7415 -- Set overflow checking bit. Much cleverer code needed here eventually
7416 -- and perhaps the Resolve routines should be separated for the various
7417 -- arithmetic operations, since they will need different processing. ???
7419 if Nkind (N) in N_Op then
7420 if not Overflow_Checks_Suppressed (Etype (N)) then
7421 Enable_Overflow_Check (N);
7422 end if;
7423 end if;
7424 end Resolve_Op_Expon;
7426 --------------------
7427 -- Resolve_Op_Not --
7428 --------------------
7430 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7431 B_Typ : Entity_Id;
7433 function Parent_Is_Boolean return Boolean;
7434 -- This function determines if the parent node is a boolean operator
7435 -- or operation (comparison op, membership test, or short circuit form)
7436 -- and the not in question is the left operand of this operation.
7437 -- Note that if the not is in parens, then false is returned.
7439 -----------------------
7440 -- Parent_Is_Boolean --
7441 -----------------------
7443 function Parent_Is_Boolean return Boolean is
7444 begin
7445 if Paren_Count (N) /= 0 then
7446 return False;
7448 else
7449 case Nkind (Parent (N)) is
7450 when N_Op_And |
7451 N_Op_Eq |
7452 N_Op_Ge |
7453 N_Op_Gt |
7454 N_Op_Le |
7455 N_Op_Lt |
7456 N_Op_Ne |
7457 N_Op_Or |
7458 N_Op_Xor |
7459 N_In |
7460 N_Not_In |
7461 N_And_Then |
7462 N_Or_Else =>
7464 return Left_Opnd (Parent (N)) = N;
7466 when others =>
7467 return False;
7468 end case;
7469 end if;
7470 end Parent_Is_Boolean;
7472 -- Start of processing for Resolve_Op_Not
7474 begin
7475 -- Predefined operations on scalar types yield the base type. On the
7476 -- other hand, logical operations on arrays yield the type of the
7477 -- arguments (and the context).
7479 if Is_Array_Type (Typ) then
7480 B_Typ := Typ;
7481 else
7482 B_Typ := Base_Type (Typ);
7483 end if;
7485 if Is_VMS_Operator (Entity (N)) then
7486 null;
7488 -- Straightforward case of incorrect arguments
7490 elsif not Valid_Boolean_Arg (Typ) then
7491 Error_Msg_N ("invalid operand type for operator&", N);
7492 Set_Etype (N, Any_Type);
7493 return;
7495 -- Special case of probable missing parens
7497 elsif Typ = Universal_Integer or else Typ = Any_Modular then
7498 if Parent_Is_Boolean then
7499 Error_Msg_N
7500 ("operand of not must be enclosed in parentheses",
7501 Right_Opnd (N));
7502 else
7503 Error_Msg_N
7504 ("no modular type available in this context", N);
7505 end if;
7507 Set_Etype (N, Any_Type);
7508 return;
7510 -- OK resolution of not
7512 else
7513 -- Warn if non-boolean types involved. This is a case like not a < b
7514 -- where a and b are modular, where we will get (not a) < b and most
7515 -- likely not (a < b) was intended.
7517 if Warn_On_Questionable_Missing_Parens
7518 and then not Is_Boolean_Type (Typ)
7519 and then Parent_Is_Boolean
7520 then
7521 Error_Msg_N ("?not expression should be parenthesized here!", N);
7522 end if;
7524 -- Warn on double negation if checking redundant constructs
7526 if Warn_On_Redundant_Constructs
7527 and then Comes_From_Source (N)
7528 and then Comes_From_Source (Right_Opnd (N))
7529 and then Root_Type (Typ) = Standard_Boolean
7530 and then Nkind (Right_Opnd (N)) = N_Op_Not
7531 then
7532 Error_Msg_N ("redundant double negation?", N);
7533 end if;
7535 -- Complete resolution and evaluation of NOT
7537 Resolve (Right_Opnd (N), B_Typ);
7538 Check_Unset_Reference (Right_Opnd (N));
7539 Set_Etype (N, B_Typ);
7540 Generate_Operator_Reference (N, B_Typ);
7541 Eval_Op_Not (N);
7542 end if;
7543 end Resolve_Op_Not;
7545 -----------------------------
7546 -- Resolve_Operator_Symbol --
7547 -----------------------------
7549 -- Nothing to be done, all resolved already
7551 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
7552 pragma Warnings (Off, N);
7553 pragma Warnings (Off, Typ);
7555 begin
7556 null;
7557 end Resolve_Operator_Symbol;
7559 ----------------------------------
7560 -- Resolve_Qualified_Expression --
7561 ----------------------------------
7563 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
7564 pragma Warnings (Off, Typ);
7566 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7567 Expr : constant Node_Id := Expression (N);
7569 begin
7570 Resolve (Expr, Target_Typ);
7572 -- A qualified expression requires an exact match of the type,
7573 -- class-wide matching is not allowed. However, if the qualifying
7574 -- type is specific and the expression has a class-wide type, it
7575 -- may still be okay, since it can be the result of the expansion
7576 -- of a call to a dispatching function, so we also have to check
7577 -- class-wideness of the type of the expression's original node.
7579 if (Is_Class_Wide_Type (Target_Typ)
7580 or else
7581 (Is_Class_Wide_Type (Etype (Expr))
7582 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
7583 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
7584 then
7585 Wrong_Type (Expr, Target_Typ);
7586 end if;
7588 -- If the target type is unconstrained, then we reset the type of
7589 -- the result from the type of the expression. For other cases, the
7590 -- actual subtype of the expression is the target type.
7592 if Is_Composite_Type (Target_Typ)
7593 and then not Is_Constrained (Target_Typ)
7594 then
7595 Set_Etype (N, Etype (Expr));
7596 end if;
7598 Eval_Qualified_Expression (N);
7599 end Resolve_Qualified_Expression;
7601 -------------------
7602 -- Resolve_Range --
7603 -------------------
7605 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
7606 L : constant Node_Id := Low_Bound (N);
7607 H : constant Node_Id := High_Bound (N);
7609 begin
7610 Set_Etype (N, Typ);
7611 Resolve (L, Typ);
7612 Resolve (H, Typ);
7614 Check_Unset_Reference (L);
7615 Check_Unset_Reference (H);
7617 -- We have to check the bounds for being within the base range as
7618 -- required for a non-static context. Normally this is automatic and
7619 -- done as part of evaluating expressions, but the N_Range node is an
7620 -- exception, since in GNAT we consider this node to be a subexpression,
7621 -- even though in Ada it is not. The circuit in Sem_Eval could check for
7622 -- this, but that would put the test on the main evaluation path for
7623 -- expressions.
7625 Check_Non_Static_Context (L);
7626 Check_Non_Static_Context (H);
7628 -- Check for an ambiguous range over character literals. This will
7629 -- happen with a membership test involving only literals.
7631 if Typ = Any_Character then
7632 Ambiguous_Character (L);
7633 Set_Etype (N, Any_Type);
7634 return;
7635 end if;
7637 -- If bounds are static, constant-fold them, so size computations
7638 -- are identical between front-end and back-end. Do not perform this
7639 -- transformation while analyzing generic units, as type information
7640 -- would then be lost when reanalyzing the constant node in the
7641 -- instance.
7643 if Is_Discrete_Type (Typ) and then Expander_Active then
7644 if Is_OK_Static_Expression (L) then
7645 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
7646 end if;
7648 if Is_OK_Static_Expression (H) then
7649 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
7650 end if;
7651 end if;
7652 end Resolve_Range;
7654 --------------------------
7655 -- Resolve_Real_Literal --
7656 --------------------------
7658 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
7659 Actual_Typ : constant Entity_Id := Etype (N);
7661 begin
7662 -- Special processing for fixed-point literals to make sure that the
7663 -- value is an exact multiple of small where this is required. We
7664 -- skip this for the universal real case, and also for generic types.
7666 if Is_Fixed_Point_Type (Typ)
7667 and then Typ /= Universal_Fixed
7668 and then Typ /= Any_Fixed
7669 and then not Is_Generic_Type (Typ)
7670 then
7671 declare
7672 Val : constant Ureal := Realval (N);
7673 Cintr : constant Ureal := Val / Small_Value (Typ);
7674 Cint : constant Uint := UR_Trunc (Cintr);
7675 Den : constant Uint := Norm_Den (Cintr);
7676 Stat : Boolean;
7678 begin
7679 -- Case of literal is not an exact multiple of the Small
7681 if Den /= 1 then
7683 -- For a source program literal for a decimal fixed-point
7684 -- type, this is statically illegal (RM 4.9(36)).
7686 if Is_Decimal_Fixed_Point_Type (Typ)
7687 and then Actual_Typ = Universal_Real
7688 and then Comes_From_Source (N)
7689 then
7690 Error_Msg_N ("value has extraneous low order digits", N);
7691 end if;
7693 -- Generate a warning if literal from source
7695 if Is_Static_Expression (N)
7696 and then Warn_On_Bad_Fixed_Value
7697 then
7698 Error_Msg_N
7699 ("?static fixed-point value is not a multiple of Small!",
7701 end if;
7703 -- Replace literal by a value that is the exact representation
7704 -- of a value of the type, i.e. a multiple of the small value,
7705 -- by truncation, since Machine_Rounds is false for all GNAT
7706 -- fixed-point types (RM 4.9(38)).
7708 Stat := Is_Static_Expression (N);
7709 Rewrite (N,
7710 Make_Real_Literal (Sloc (N),
7711 Realval => Small_Value (Typ) * Cint));
7713 Set_Is_Static_Expression (N, Stat);
7714 end if;
7716 -- In all cases, set the corresponding integer field
7718 Set_Corresponding_Integer_Value (N, Cint);
7719 end;
7720 end if;
7722 -- Now replace the actual type by the expected type as usual
7724 Set_Etype (N, Typ);
7725 Eval_Real_Literal (N);
7726 end Resolve_Real_Literal;
7728 -----------------------
7729 -- Resolve_Reference --
7730 -----------------------
7732 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
7733 P : constant Node_Id := Prefix (N);
7735 begin
7736 -- Replace general access with specific type
7738 if Ekind (Etype (N)) = E_Allocator_Type then
7739 Set_Etype (N, Base_Type (Typ));
7740 end if;
7742 Resolve (P, Designated_Type (Etype (N)));
7744 -- If we are taking the reference of a volatile entity, then treat
7745 -- it as a potential modification of this entity. This is much too
7746 -- conservative, but is necessary because remove side effects can
7747 -- result in transformations of normal assignments into reference
7748 -- sequences that otherwise fail to notice the modification.
7750 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
7751 Note_Possible_Modification (P, Sure => False);
7752 end if;
7753 end Resolve_Reference;
7755 --------------------------------
7756 -- Resolve_Selected_Component --
7757 --------------------------------
7759 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
7760 Comp : Entity_Id;
7761 Comp1 : Entity_Id := Empty; -- prevent junk warning
7762 P : constant Node_Id := Prefix (N);
7763 S : constant Node_Id := Selector_Name (N);
7764 T : Entity_Id := Etype (P);
7765 I : Interp_Index;
7766 I1 : Interp_Index := 0; -- prevent junk warning
7767 It : Interp;
7768 It1 : Interp;
7769 Found : Boolean;
7771 function Init_Component return Boolean;
7772 -- Check whether this is the initialization of a component within an
7773 -- init proc (by assignment or call to another init proc). If true,
7774 -- there is no need for a discriminant check.
7776 --------------------
7777 -- Init_Component --
7778 --------------------
7780 function Init_Component return Boolean is
7781 begin
7782 return Inside_Init_Proc
7783 and then Nkind (Prefix (N)) = N_Identifier
7784 and then Chars (Prefix (N)) = Name_uInit
7785 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
7786 end Init_Component;
7788 -- Start of processing for Resolve_Selected_Component
7790 begin
7791 if Is_Overloaded (P) then
7793 -- Use the context type to select the prefix that has a selector
7794 -- of the correct name and type.
7796 Found := False;
7797 Get_First_Interp (P, I, It);
7799 Search : while Present (It.Typ) loop
7800 if Is_Access_Type (It.Typ) then
7801 T := Designated_Type (It.Typ);
7802 else
7803 T := It.Typ;
7804 end if;
7806 if Is_Record_Type (T) then
7808 -- The visible components of a class-wide type are those of
7809 -- the root type.
7811 if Is_Class_Wide_Type (T) then
7812 T := Etype (T);
7813 end if;
7815 Comp := First_Entity (T);
7816 while Present (Comp) loop
7817 if Chars (Comp) = Chars (S)
7818 and then Covers (Etype (Comp), Typ)
7819 then
7820 if not Found then
7821 Found := True;
7822 I1 := I;
7823 It1 := It;
7824 Comp1 := Comp;
7826 else
7827 It := Disambiguate (P, I1, I, Any_Type);
7829 if It = No_Interp then
7830 Error_Msg_N
7831 ("ambiguous prefix for selected component", N);
7832 Set_Etype (N, Typ);
7833 return;
7835 else
7836 It1 := It;
7838 -- There may be an implicit dereference. Retrieve
7839 -- designated record type.
7841 if Is_Access_Type (It1.Typ) then
7842 T := Designated_Type (It1.Typ);
7843 else
7844 T := It1.Typ;
7845 end if;
7847 if Scope (Comp1) /= T then
7849 -- Resolution chooses the new interpretation.
7850 -- Find the component with the right name.
7852 Comp1 := First_Entity (T);
7853 while Present (Comp1)
7854 and then Chars (Comp1) /= Chars (S)
7855 loop
7856 Comp1 := Next_Entity (Comp1);
7857 end loop;
7858 end if;
7860 exit Search;
7861 end if;
7862 end if;
7863 end if;
7865 Comp := Next_Entity (Comp);
7866 end loop;
7867 end if;
7869 Get_Next_Interp (I, It);
7870 end loop Search;
7872 Resolve (P, It1.Typ);
7873 Set_Etype (N, Typ);
7874 Set_Entity_With_Style_Check (S, Comp1);
7876 else
7877 -- Resolve prefix with its type
7879 Resolve (P, T);
7880 end if;
7882 -- Generate cross-reference. We needed to wait until full overloading
7883 -- resolution was complete to do this, since otherwise we can't tell if
7884 -- we are an lvalue or not.
7886 if May_Be_Lvalue (N) then
7887 Generate_Reference (Entity (S), S, 'm');
7888 else
7889 Generate_Reference (Entity (S), S, 'r');
7890 end if;
7892 -- If prefix is an access type, the node will be transformed into an
7893 -- explicit dereference during expansion. The type of the node is the
7894 -- designated type of that of the prefix.
7896 if Is_Access_Type (Etype (P)) then
7897 T := Designated_Type (Etype (P));
7898 Check_Fully_Declared_Prefix (T, P);
7899 else
7900 T := Etype (P);
7901 end if;
7903 if Has_Discriminants (T)
7904 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
7905 and then Present (Original_Record_Component (Entity (S)))
7906 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
7907 and then Present (Discriminant_Checking_Func
7908 (Original_Record_Component (Entity (S))))
7909 and then not Discriminant_Checks_Suppressed (T)
7910 and then not Init_Component
7911 then
7912 Set_Do_Discriminant_Check (N);
7913 end if;
7915 if Ekind (Entity (S)) = E_Void then
7916 Error_Msg_N ("premature use of component", S);
7917 end if;
7919 -- If the prefix is a record conversion, this may be a renamed
7920 -- discriminant whose bounds differ from those of the original
7921 -- one, so we must ensure that a range check is performed.
7923 if Nkind (P) = N_Type_Conversion
7924 and then Ekind (Entity (S)) = E_Discriminant
7925 and then Is_Discrete_Type (Typ)
7926 then
7927 Set_Etype (N, Base_Type (Typ));
7928 end if;
7930 -- Note: No Eval processing is required, because the prefix is of a
7931 -- record type, or protected type, and neither can possibly be static.
7933 -- If the array type is atomic, and is packed, and we are in a left side
7934 -- context, then this is worth a warning, since we have a situation
7935 -- where the access to the component may cause extra read/writes of
7936 -- the atomic array object, which could be considered unexpected.
7938 if Nkind (N) = N_Selected_Component
7939 and then (Is_Atomic (T)
7940 or else (Is_Entity_Name (Prefix (N))
7941 and then Is_Atomic (Entity (Prefix (N)))))
7942 and then Is_Packed (T)
7943 and then Is_LHS (N)
7944 then
7945 Error_Msg_N ("?assignment to component of packed atomic record",
7946 Prefix (N));
7947 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7948 Prefix (N));
7949 end if;
7950 end Resolve_Selected_Component;
7952 -------------------
7953 -- Resolve_Shift --
7954 -------------------
7956 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
7957 B_Typ : constant Entity_Id := Base_Type (Typ);
7958 L : constant Node_Id := Left_Opnd (N);
7959 R : constant Node_Id := Right_Opnd (N);
7961 begin
7962 -- We do the resolution using the base type, because intermediate values
7963 -- in expressions always are of the base type, not a subtype of it.
7965 Resolve (L, B_Typ);
7966 Resolve (R, Standard_Natural);
7968 Check_Unset_Reference (L);
7969 Check_Unset_Reference (R);
7971 Set_Etype (N, B_Typ);
7972 Generate_Operator_Reference (N, B_Typ);
7973 Eval_Shift (N);
7974 end Resolve_Shift;
7976 ---------------------------
7977 -- Resolve_Short_Circuit --
7978 ---------------------------
7980 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
7981 B_Typ : constant Entity_Id := Base_Type (Typ);
7982 L : constant Node_Id := Left_Opnd (N);
7983 R : constant Node_Id := Right_Opnd (N);
7985 begin
7986 -- Why are the calls to Check_Order_Dependence commented out ???
7987 Resolve (L, B_Typ);
7988 -- Check_Order_Dependence; -- For AI05-0144
7989 Resolve (R, B_Typ);
7990 -- Check_Order_Dependence; -- For AI05-0144
7992 -- Check for issuing warning for always False assert/check, this happens
7993 -- when assertions are turned off, in which case the pragma Assert/Check
7994 -- was transformed into:
7996 -- if False and then <condition> then ...
7998 -- and we detect this pattern
8000 if Warn_On_Assertion_Failure
8001 and then Is_Entity_Name (R)
8002 and then Entity (R) = Standard_False
8003 and then Nkind (Parent (N)) = N_If_Statement
8004 and then Nkind (N) = N_And_Then
8005 and then Is_Entity_Name (L)
8006 and then Entity (L) = Standard_False
8007 then
8008 declare
8009 Orig : constant Node_Id := Original_Node (Parent (N));
8011 begin
8012 if Nkind (Orig) = N_Pragma
8013 and then Pragma_Name (Orig) = Name_Assert
8014 then
8015 -- Don't want to warn if original condition is explicit False
8017 declare
8018 Expr : constant Node_Id :=
8019 Original_Node
8020 (Expression
8021 (First (Pragma_Argument_Associations (Orig))));
8022 begin
8023 if Is_Entity_Name (Expr)
8024 and then Entity (Expr) = Standard_False
8025 then
8026 null;
8027 else
8028 -- Issue warning. We do not want the deletion of the
8029 -- IF/AND-THEN to take this message with it. We achieve
8030 -- this by making sure that the expanded code points to
8031 -- the Sloc of the expression, not the original pragma.
8033 Error_Msg_N
8034 ("?assertion would fail at run-time!",
8035 Expression
8036 (First (Pragma_Argument_Associations (Orig))));
8037 end if;
8038 end;
8040 -- Similar processing for Check pragma
8042 elsif Nkind (Orig) = N_Pragma
8043 and then Pragma_Name (Orig) = Name_Check
8044 then
8045 -- Don't want to warn if original condition is explicit False
8047 declare
8048 Expr : constant Node_Id :=
8049 Original_Node
8050 (Expression
8051 (Next (First
8052 (Pragma_Argument_Associations (Orig)))));
8053 begin
8054 if Is_Entity_Name (Expr)
8055 and then Entity (Expr) = Standard_False
8056 then
8057 null;
8058 else
8059 Error_Msg_N
8060 ("?check would fail at run-time!",
8061 Expression
8062 (Last (Pragma_Argument_Associations (Orig))));
8063 end if;
8064 end;
8065 end if;
8066 end;
8067 end if;
8069 -- Continue with processing of short circuit
8071 Check_Unset_Reference (L);
8072 Check_Unset_Reference (R);
8074 Set_Etype (N, B_Typ);
8075 Eval_Short_Circuit (N);
8076 end Resolve_Short_Circuit;
8078 -------------------
8079 -- Resolve_Slice --
8080 -------------------
8082 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8083 Name : constant Node_Id := Prefix (N);
8084 Drange : constant Node_Id := Discrete_Range (N);
8085 Array_Type : Entity_Id := Empty;
8086 Index : Node_Id;
8088 begin
8089 if Is_Overloaded (Name) then
8091 -- Use the context type to select the prefix that yields the correct
8092 -- array type.
8094 declare
8095 I : Interp_Index;
8096 I1 : Interp_Index := 0;
8097 It : Interp;
8098 P : constant Node_Id := Prefix (N);
8099 Found : Boolean := False;
8101 begin
8102 Get_First_Interp (P, I, It);
8103 while Present (It.Typ) loop
8104 if (Is_Array_Type (It.Typ)
8105 and then Covers (Typ, It.Typ))
8106 or else (Is_Access_Type (It.Typ)
8107 and then Is_Array_Type (Designated_Type (It.Typ))
8108 and then Covers (Typ, Designated_Type (It.Typ)))
8109 then
8110 if Found then
8111 It := Disambiguate (P, I1, I, Any_Type);
8113 if It = No_Interp then
8114 Error_Msg_N ("ambiguous prefix for slicing", N);
8115 Set_Etype (N, Typ);
8116 return;
8117 else
8118 Found := True;
8119 Array_Type := It.Typ;
8120 I1 := I;
8121 end if;
8122 else
8123 Found := True;
8124 Array_Type := It.Typ;
8125 I1 := I;
8126 end if;
8127 end if;
8129 Get_Next_Interp (I, It);
8130 end loop;
8131 end;
8133 else
8134 Array_Type := Etype (Name);
8135 end if;
8137 Resolve (Name, Array_Type);
8139 if Is_Access_Type (Array_Type) then
8140 Apply_Access_Check (N);
8141 Array_Type := Designated_Type (Array_Type);
8143 -- If the prefix is an access to an unconstrained array, we must use
8144 -- the actual subtype of the object to perform the index checks. The
8145 -- object denoted by the prefix is implicit in the node, so we build
8146 -- an explicit representation for it in order to compute the actual
8147 -- subtype.
8149 if not Is_Constrained (Array_Type) then
8150 Remove_Side_Effects (Prefix (N));
8152 declare
8153 Obj : constant Node_Id :=
8154 Make_Explicit_Dereference (Sloc (N),
8155 Prefix => New_Copy_Tree (Prefix (N)));
8156 begin
8157 Set_Etype (Obj, Array_Type);
8158 Set_Parent (Obj, Parent (N));
8159 Array_Type := Get_Actual_Subtype (Obj);
8160 end;
8161 end if;
8163 elsif Is_Entity_Name (Name)
8164 or else Nkind (Name) = N_Explicit_Dereference
8165 or else (Nkind (Name) = N_Function_Call
8166 and then not Is_Constrained (Etype (Name)))
8167 then
8168 Array_Type := Get_Actual_Subtype (Name);
8170 -- If the name is a selected component that depends on discriminants,
8171 -- build an actual subtype for it. This can happen only when the name
8172 -- itself is overloaded; otherwise the actual subtype is created when
8173 -- the selected component is analyzed.
8175 elsif Nkind (Name) = N_Selected_Component
8176 and then Full_Analysis
8177 and then Depends_On_Discriminant (First_Index (Array_Type))
8178 then
8179 declare
8180 Act_Decl : constant Node_Id :=
8181 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8182 begin
8183 Insert_Action (N, Act_Decl);
8184 Array_Type := Defining_Identifier (Act_Decl);
8185 end;
8187 -- Maybe this should just be "else", instead of checking for the
8188 -- specific case of slice??? This is needed for the case where
8189 -- the prefix is an Image attribute, which gets expanded to a
8190 -- slice, and so has a constrained subtype which we want to use
8191 -- for the slice range check applied below (the range check won't
8192 -- get done if the unconstrained subtype of the 'Image is used).
8194 elsif Nkind (Name) = N_Slice then
8195 Array_Type := Etype (Name);
8196 end if;
8198 -- If name was overloaded, set slice type correctly now
8200 Set_Etype (N, Array_Type);
8202 -- If the range is specified by a subtype mark, no resolution is
8203 -- necessary. Else resolve the bounds, and apply needed checks.
8205 if not Is_Entity_Name (Drange) then
8206 Index := First_Index (Array_Type);
8207 Resolve (Drange, Base_Type (Etype (Index)));
8209 if Nkind (Drange) = N_Range
8211 -- Do not apply the range check to nodes associated with the
8212 -- frontend expansion of the dispatch table. We first check
8213 -- if Ada.Tags is already loaded to void the addition of an
8214 -- undesired dependence on such run-time unit.
8216 and then
8217 (not Tagged_Type_Expansion
8218 or else not
8219 (RTU_Loaded (Ada_Tags)
8220 and then Nkind (Prefix (N)) = N_Selected_Component
8221 and then Present (Entity (Selector_Name (Prefix (N))))
8222 and then Entity (Selector_Name (Prefix (N))) =
8223 RTE_Record_Component (RE_Prims_Ptr)))
8224 then
8225 Apply_Range_Check (Drange, Etype (Index));
8226 end if;
8227 end if;
8229 Set_Slice_Subtype (N);
8231 if Nkind (Drange) = N_Range then
8232 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
8233 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8234 end if;
8236 Eval_Slice (N);
8237 end Resolve_Slice;
8239 ----------------------------
8240 -- Resolve_String_Literal --
8241 ----------------------------
8243 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8244 C_Typ : constant Entity_Id := Component_Type (Typ);
8245 R_Typ : constant Entity_Id := Root_Type (C_Typ);
8246 Loc : constant Source_Ptr := Sloc (N);
8247 Str : constant String_Id := Strval (N);
8248 Strlen : constant Nat := String_Length (Str);
8249 Subtype_Id : Entity_Id;
8250 Need_Check : Boolean;
8252 begin
8253 -- For a string appearing in a concatenation, defer creation of the
8254 -- string_literal_subtype until the end of the resolution of the
8255 -- concatenation, because the literal may be constant-folded away. This
8256 -- is a useful optimization for long concatenation expressions.
8258 -- If the string is an aggregate built for a single character (which
8259 -- happens in a non-static context) or a is null string to which special
8260 -- checks may apply, we build the subtype. Wide strings must also get a
8261 -- string subtype if they come from a one character aggregate. Strings
8262 -- generated by attributes might be static, but it is often hard to
8263 -- determine whether the enclosing context is static, so we generate
8264 -- subtypes for them as well, thus losing some rarer optimizations ???
8265 -- Same for strings that come from a static conversion.
8267 Need_Check :=
8268 (Strlen = 0 and then Typ /= Standard_String)
8269 or else Nkind (Parent (N)) /= N_Op_Concat
8270 or else (N /= Left_Opnd (Parent (N))
8271 and then N /= Right_Opnd (Parent (N)))
8272 or else ((Typ = Standard_Wide_String
8273 or else Typ = Standard_Wide_Wide_String)
8274 and then Nkind (Original_Node (N)) /= N_String_Literal);
8276 -- If the resolving type is itself a string literal subtype, we can just
8277 -- reuse it, since there is no point in creating another.
8279 if Ekind (Typ) = E_String_Literal_Subtype then
8280 Subtype_Id := Typ;
8282 elsif Nkind (Parent (N)) = N_Op_Concat
8283 and then not Need_Check
8284 and then not Nkind_In (Original_Node (N), N_Character_Literal,
8285 N_Attribute_Reference,
8286 N_Qualified_Expression,
8287 N_Type_Conversion)
8288 then
8289 Subtype_Id := Typ;
8291 -- Otherwise we must create a string literal subtype. Note that the
8292 -- whole idea of string literal subtypes is simply to avoid the need
8293 -- for building a full fledged array subtype for each literal.
8295 else
8296 Set_String_Literal_Subtype (N, Typ);
8297 Subtype_Id := Etype (N);
8298 end if;
8300 if Nkind (Parent (N)) /= N_Op_Concat
8301 or else Need_Check
8302 then
8303 Set_Etype (N, Subtype_Id);
8304 Eval_String_Literal (N);
8305 end if;
8307 if Is_Limited_Composite (Typ)
8308 or else Is_Private_Composite (Typ)
8309 then
8310 Error_Msg_N ("string literal not available for private array", N);
8311 Set_Etype (N, Any_Type);
8312 return;
8313 end if;
8315 -- The validity of a null string has been checked in the call to
8316 -- Eval_String_Literal.
8318 if Strlen = 0 then
8319 return;
8321 -- Always accept string literal with component type Any_Character, which
8322 -- occurs in error situations and in comparisons of literals, both of
8323 -- which should accept all literals.
8325 elsif R_Typ = Any_Character then
8326 return;
8328 -- If the type is bit-packed, then we always transform the string
8329 -- literal into a full fledged aggregate.
8331 elsif Is_Bit_Packed_Array (Typ) then
8332 null;
8334 -- Deal with cases of Wide_Wide_String, Wide_String, and String
8336 else
8337 -- For Standard.Wide_Wide_String, or any other type whose component
8338 -- type is Standard.Wide_Wide_Character, we know that all the
8339 -- characters in the string must be acceptable, since the parser
8340 -- accepted the characters as valid character literals.
8342 if R_Typ = Standard_Wide_Wide_Character then
8343 null;
8345 -- For the case of Standard.String, or any other type whose component
8346 -- type is Standard.Character, we must make sure that there are no
8347 -- wide characters in the string, i.e. that it is entirely composed
8348 -- of characters in range of type Character.
8350 -- If the string literal is the result of a static concatenation, the
8351 -- test has already been performed on the components, and need not be
8352 -- repeated.
8354 elsif R_Typ = Standard_Character
8355 and then Nkind (Original_Node (N)) /= N_Op_Concat
8356 then
8357 for J in 1 .. Strlen loop
8358 if not In_Character_Range (Get_String_Char (Str, J)) then
8360 -- If we are out of range, post error. This is one of the
8361 -- very few places that we place the flag in the middle of
8362 -- a token, right under the offending wide character. Not
8363 -- quite clear if this is right wrt wide character encoding
8364 -- sequences, but it's only an error message!
8366 Error_Msg
8367 ("literal out of range of type Standard.Character",
8368 Source_Ptr (Int (Loc) + J));
8369 return;
8370 end if;
8371 end loop;
8373 -- For the case of Standard.Wide_String, or any other type whose
8374 -- component type is Standard.Wide_Character, we must make sure that
8375 -- there are no wide characters in the string, i.e. that it is
8376 -- entirely composed of characters in range of type Wide_Character.
8378 -- If the string literal is the result of a static concatenation,
8379 -- the test has already been performed on the components, and need
8380 -- not be repeated.
8382 elsif R_Typ = Standard_Wide_Character
8383 and then Nkind (Original_Node (N)) /= N_Op_Concat
8384 then
8385 for J in 1 .. Strlen loop
8386 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8388 -- If we are out of range, post error. This is one of the
8389 -- very few places that we place the flag in the middle of
8390 -- a token, right under the offending wide character.
8392 -- This is not quite right, because characters in general
8393 -- will take more than one character position ???
8395 Error_Msg
8396 ("literal out of range of type Standard.Wide_Character",
8397 Source_Ptr (Int (Loc) + J));
8398 return;
8399 end if;
8400 end loop;
8402 -- If the root type is not a standard character, then we will convert
8403 -- the string into an aggregate and will let the aggregate code do
8404 -- the checking. Standard Wide_Wide_Character is also OK here.
8406 else
8407 null;
8408 end if;
8410 -- See if the component type of the array corresponding to the string
8411 -- has compile time known bounds. If yes we can directly check
8412 -- whether the evaluation of the string will raise constraint error.
8413 -- Otherwise we need to transform the string literal into the
8414 -- corresponding character aggregate and let the aggregate
8415 -- code do the checking.
8417 if Is_Standard_Character_Type (R_Typ) then
8419 -- Check for the case of full range, where we are definitely OK
8421 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8422 return;
8423 end if;
8425 -- Here the range is not the complete base type range, so check
8427 declare
8428 Comp_Typ_Lo : constant Node_Id :=
8429 Type_Low_Bound (Component_Type (Typ));
8430 Comp_Typ_Hi : constant Node_Id :=
8431 Type_High_Bound (Component_Type (Typ));
8433 Char_Val : Uint;
8435 begin
8436 if Compile_Time_Known_Value (Comp_Typ_Lo)
8437 and then Compile_Time_Known_Value (Comp_Typ_Hi)
8438 then
8439 for J in 1 .. Strlen loop
8440 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
8442 if Char_Val < Expr_Value (Comp_Typ_Lo)
8443 or else Char_Val > Expr_Value (Comp_Typ_Hi)
8444 then
8445 Apply_Compile_Time_Constraint_Error
8446 (N, "character out of range?", CE_Range_Check_Failed,
8447 Loc => Source_Ptr (Int (Loc) + J));
8448 end if;
8449 end loop;
8451 return;
8452 end if;
8453 end;
8454 end if;
8455 end if;
8457 -- If we got here we meed to transform the string literal into the
8458 -- equivalent qualified positional array aggregate. This is rather
8459 -- heavy artillery for this situation, but it is hard work to avoid.
8461 declare
8462 Lits : constant List_Id := New_List;
8463 P : Source_Ptr := Loc + 1;
8464 C : Char_Code;
8466 begin
8467 -- Build the character literals, we give them source locations that
8468 -- correspond to the string positions, which is a bit tricky given
8469 -- the possible presence of wide character escape sequences.
8471 for J in 1 .. Strlen loop
8472 C := Get_String_Char (Str, J);
8473 Set_Character_Literal_Name (C);
8475 Append_To (Lits,
8476 Make_Character_Literal (P,
8477 Chars => Name_Find,
8478 Char_Literal_Value => UI_From_CC (C)));
8480 if In_Character_Range (C) then
8481 P := P + 1;
8483 -- Should we have a call to Skip_Wide here ???
8484 -- ??? else
8485 -- Skip_Wide (P);
8487 end if;
8488 end loop;
8490 Rewrite (N,
8491 Make_Qualified_Expression (Loc,
8492 Subtype_Mark => New_Reference_To (Typ, Loc),
8493 Expression =>
8494 Make_Aggregate (Loc, Expressions => Lits)));
8496 Analyze_And_Resolve (N, Typ);
8497 end;
8498 end Resolve_String_Literal;
8500 -----------------------------
8501 -- Resolve_Subprogram_Info --
8502 -----------------------------
8504 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
8505 begin
8506 Set_Etype (N, Typ);
8507 end Resolve_Subprogram_Info;
8509 -----------------------------
8510 -- Resolve_Type_Conversion --
8511 -----------------------------
8513 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
8514 Conv_OK : constant Boolean := Conversion_OK (N);
8515 Operand : constant Node_Id := Expression (N);
8516 Operand_Typ : constant Entity_Id := Etype (Operand);
8517 Target_Typ : constant Entity_Id := Etype (N);
8518 Rop : Node_Id;
8519 Orig_N : Node_Id;
8520 Orig_T : Node_Id;
8522 begin
8523 if not Conv_OK
8524 and then not Valid_Conversion (N, Target_Typ, Operand)
8525 then
8526 return;
8527 end if;
8529 if Etype (Operand) = Any_Fixed then
8531 -- Mixed-mode operation involving a literal. Context must be a fixed
8532 -- type which is applied to the literal subsequently.
8534 if Is_Fixed_Point_Type (Typ) then
8535 Set_Etype (Operand, Universal_Real);
8537 elsif Is_Numeric_Type (Typ)
8538 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
8539 and then (Etype (Right_Opnd (Operand)) = Universal_Real
8540 or else
8541 Etype (Left_Opnd (Operand)) = Universal_Real)
8542 then
8543 -- Return if expression is ambiguous
8545 if Unique_Fixed_Point_Type (N) = Any_Type then
8546 return;
8548 -- If nothing else, the available fixed type is Duration
8550 else
8551 Set_Etype (Operand, Standard_Duration);
8552 end if;
8554 -- Resolve the real operand with largest available precision
8556 if Etype (Right_Opnd (Operand)) = Universal_Real then
8557 Rop := New_Copy_Tree (Right_Opnd (Operand));
8558 else
8559 Rop := New_Copy_Tree (Left_Opnd (Operand));
8560 end if;
8562 Resolve (Rop, Universal_Real);
8564 -- If the operand is a literal (it could be a non-static and
8565 -- illegal exponentiation) check whether the use of Duration
8566 -- is potentially inaccurate.
8568 if Nkind (Rop) = N_Real_Literal
8569 and then Realval (Rop) /= Ureal_0
8570 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
8571 then
8572 Error_Msg_N
8573 ("?universal real operand can only " &
8574 "be interpreted as Duration!",
8575 Rop);
8576 Error_Msg_N
8577 ("\?precision will be lost in the conversion!", Rop);
8578 end if;
8580 elsif Is_Numeric_Type (Typ)
8581 and then Nkind (Operand) in N_Op
8582 and then Unique_Fixed_Point_Type (N) /= Any_Type
8583 then
8584 Set_Etype (Operand, Standard_Duration);
8586 else
8587 Error_Msg_N ("invalid context for mixed mode operation", N);
8588 Set_Etype (Operand, Any_Type);
8589 return;
8590 end if;
8591 end if;
8593 Resolve (Operand);
8595 -- Note: we do the Eval_Type_Conversion call before applying the
8596 -- required checks for a subtype conversion. This is important, since
8597 -- both are prepared under certain circumstances to change the type
8598 -- conversion to a constraint error node, but in the case of
8599 -- Eval_Type_Conversion this may reflect an illegality in the static
8600 -- case, and we would miss the illegality (getting only a warning
8601 -- message), if we applied the type conversion checks first.
8603 Eval_Type_Conversion (N);
8605 -- Even when evaluation is not possible, we may be able to simplify the
8606 -- conversion or its expression. This needs to be done before applying
8607 -- checks, since otherwise the checks may use the original expression
8608 -- and defeat the simplifications. This is specifically the case for
8609 -- elimination of the floating-point Truncation attribute in
8610 -- float-to-int conversions.
8612 Simplify_Type_Conversion (N);
8614 -- If after evaluation we still have a type conversion, then we may need
8615 -- to apply checks required for a subtype conversion.
8617 -- Skip these type conversion checks if universal fixed operands
8618 -- operands involved, since range checks are handled separately for
8619 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
8621 if Nkind (N) = N_Type_Conversion
8622 and then not Is_Generic_Type (Root_Type (Target_Typ))
8623 and then Target_Typ /= Universal_Fixed
8624 and then Operand_Typ /= Universal_Fixed
8625 then
8626 Apply_Type_Conversion_Checks (N);
8627 end if;
8629 -- Issue warning for conversion of simple object to its own type. We
8630 -- have to test the original nodes, since they may have been rewritten
8631 -- by various optimizations.
8633 Orig_N := Original_Node (N);
8635 if Warn_On_Redundant_Constructs
8636 and then Comes_From_Source (Orig_N)
8637 and then Nkind (Orig_N) = N_Type_Conversion
8638 and then not In_Instance
8639 then
8640 Orig_N := Original_Node (Expression (Orig_N));
8641 Orig_T := Target_Typ;
8643 -- If the node is part of a larger expression, the Target_Type
8644 -- may not be the original type of the node if the context is a
8645 -- condition. Recover original type to see if conversion is needed.
8647 if Is_Boolean_Type (Orig_T)
8648 and then Nkind (Parent (N)) in N_Op
8649 then
8650 Orig_T := Etype (Parent (N));
8651 end if;
8653 if Is_Entity_Name (Orig_N)
8654 and then
8655 (Etype (Entity (Orig_N)) = Orig_T
8656 or else
8657 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
8658 and then Covers (Orig_T, Etype (Entity (Orig_N)))))
8659 then
8660 -- One more check, do not give warning if the analyzed conversion
8661 -- has an expression with non-static bounds, and the bounds of the
8662 -- target are static. This avoids junk warnings in cases where the
8663 -- conversion is necessary to establish staticness, for example in
8664 -- a case statement.
8666 if not Is_OK_Static_Subtype (Operand_Typ)
8667 and then Is_OK_Static_Subtype (Target_Typ)
8668 then
8669 null;
8671 -- Here we give the redundant conversion warning
8673 else
8674 Error_Msg_Node_2 := Orig_T;
8675 Error_Msg_NE -- CODEFIX
8676 ("?redundant conversion, & is of type &!",
8677 N, Entity (Orig_N));
8678 end if;
8679 end if;
8680 end if;
8682 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
8683 -- No need to perform any interface conversion if the type of the
8684 -- expression coincides with the target type.
8686 if Ada_Version >= Ada_05
8687 and then Expander_Active
8688 and then Operand_Typ /= Target_Typ
8689 then
8690 declare
8691 Opnd : Entity_Id := Operand_Typ;
8692 Target : Entity_Id := Target_Typ;
8694 begin
8695 if Is_Access_Type (Opnd) then
8696 Opnd := Designated_Type (Opnd);
8697 end if;
8699 if Is_Access_Type (Target_Typ) then
8700 Target := Designated_Type (Target);
8701 end if;
8703 if Opnd = Target then
8704 null;
8706 -- Conversion from interface type
8708 elsif Is_Interface (Opnd) then
8710 -- Ada 2005 (AI-217): Handle entities from limited views
8712 if From_With_Type (Opnd) then
8713 Error_Msg_Qual_Level := 99;
8714 Error_Msg_NE -- CODEFIX
8715 ("missing WITH clause on package &", N,
8716 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
8717 Error_Msg_N
8718 ("type conversions require visibility of the full view",
8721 elsif From_With_Type (Target)
8722 and then not
8723 (Is_Access_Type (Target_Typ)
8724 and then Present (Non_Limited_View (Etype (Target))))
8725 then
8726 Error_Msg_Qual_Level := 99;
8727 Error_Msg_NE -- CODEFIX
8728 ("missing WITH clause on package &", N,
8729 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
8730 Error_Msg_N
8731 ("type conversions require visibility of the full view",
8734 else
8735 Expand_Interface_Conversion (N, Is_Static => False);
8736 end if;
8738 -- Conversion to interface type
8740 elsif Is_Interface (Target) then
8742 -- Handle subtypes
8744 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
8745 Opnd := Etype (Opnd);
8746 end if;
8748 if not Interface_Present_In_Ancestor
8749 (Typ => Opnd,
8750 Iface => Target)
8751 then
8752 if Is_Class_Wide_Type (Opnd) then
8754 -- The static analysis is not enough to know if the
8755 -- interface is implemented or not. Hence we must pass
8756 -- the work to the expander to generate code to evaluate
8757 -- the conversion at run-time.
8759 Expand_Interface_Conversion (N, Is_Static => False);
8761 else
8762 Error_Msg_Name_1 := Chars (Etype (Target));
8763 Error_Msg_Name_2 := Chars (Opnd);
8764 Error_Msg_N
8765 ("wrong interface conversion (% is not a progenitor " &
8766 "of %)", N);
8767 end if;
8769 else
8770 Expand_Interface_Conversion (N);
8771 end if;
8772 end if;
8773 end;
8774 end if;
8775 end Resolve_Type_Conversion;
8777 ----------------------
8778 -- Resolve_Unary_Op --
8779 ----------------------
8781 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
8782 B_Typ : constant Entity_Id := Base_Type (Typ);
8783 R : constant Node_Id := Right_Opnd (N);
8784 OK : Boolean;
8785 Lo : Uint;
8786 Hi : Uint;
8788 begin
8789 -- Deal with intrinsic unary operators
8791 if Comes_From_Source (N)
8792 and then Ekind (Entity (N)) = E_Function
8793 and then Is_Imported (Entity (N))
8794 and then Is_Intrinsic_Subprogram (Entity (N))
8795 then
8796 Resolve_Intrinsic_Unary_Operator (N, Typ);
8797 return;
8798 end if;
8800 -- Deal with universal cases
8802 if Etype (R) = Universal_Integer
8803 or else
8804 Etype (R) = Universal_Real
8805 then
8806 Check_For_Visible_Operator (N, B_Typ);
8807 end if;
8809 Set_Etype (N, B_Typ);
8810 Resolve (R, B_Typ);
8812 -- Generate warning for expressions like abs (x mod 2)
8814 if Warn_On_Redundant_Constructs
8815 and then Nkind (N) = N_Op_Abs
8816 then
8817 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
8819 if OK and then Hi >= Lo and then Lo >= 0 then
8820 Error_Msg_N -- CODEFIX
8821 ("?abs applied to known non-negative value has no effect", N);
8822 end if;
8823 end if;
8825 -- Deal with reference generation
8827 Check_Unset_Reference (R);
8828 Generate_Operator_Reference (N, B_Typ);
8829 Eval_Unary_Op (N);
8831 -- Set overflow checking bit. Much cleverer code needed here eventually
8832 -- and perhaps the Resolve routines should be separated for the various
8833 -- arithmetic operations, since they will need different processing ???
8835 if Nkind (N) in N_Op then
8836 if not Overflow_Checks_Suppressed (Etype (N)) then
8837 Enable_Overflow_Check (N);
8838 end if;
8839 end if;
8841 -- Generate warning for expressions like -5 mod 3 for integers. No need
8842 -- to worry in the floating-point case, since parens do not affect the
8843 -- result so there is no point in giving in a warning.
8845 declare
8846 Norig : constant Node_Id := Original_Node (N);
8847 Rorig : Node_Id;
8848 Val : Uint;
8849 HB : Uint;
8850 LB : Uint;
8851 Lval : Uint;
8852 Opnd : Node_Id;
8854 begin
8855 if Warn_On_Questionable_Missing_Parens
8856 and then Comes_From_Source (Norig)
8857 and then Is_Integer_Type (Typ)
8858 and then Nkind (Norig) = N_Op_Minus
8859 then
8860 Rorig := Original_Node (Right_Opnd (Norig));
8862 -- We are looking for cases where the right operand is not
8863 -- parenthesized, and is a binary operator, multiply, divide, or
8864 -- mod. These are the cases where the grouping can affect results.
8866 if Paren_Count (Rorig) = 0
8867 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
8868 then
8869 -- For mod, we always give the warning, since the value is
8870 -- affected by the parenthesization (e.g. (-5) mod 315 /=
8871 -- -(5 mod 315)). But for the other cases, the only concern is
8872 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
8873 -- overflows, but (-2) * 64 does not). So we try to give the
8874 -- message only when overflow is possible.
8876 if Nkind (Rorig) /= N_Op_Mod
8877 and then Compile_Time_Known_Value (R)
8878 then
8879 Val := Expr_Value (R);
8881 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
8882 HB := Expr_Value (Type_High_Bound (Typ));
8883 else
8884 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
8885 end if;
8887 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
8888 LB := Expr_Value (Type_Low_Bound (Typ));
8889 else
8890 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
8891 end if;
8893 -- Note that the test below is deliberately excluding the
8894 -- largest negative number, since that is a potentially
8895 -- troublesome case (e.g. -2 * x, where the result is the
8896 -- largest negative integer has an overflow with 2 * x).
8898 if Val > LB and then Val <= HB then
8899 return;
8900 end if;
8901 end if;
8903 -- For the multiplication case, the only case we have to worry
8904 -- about is when (-a)*b is exactly the largest negative number
8905 -- so that -(a*b) can cause overflow. This can only happen if
8906 -- a is a power of 2, and more generally if any operand is a
8907 -- constant that is not a power of 2, then the parentheses
8908 -- cannot affect whether overflow occurs. We only bother to
8909 -- test the left most operand
8911 -- Loop looking at left operands for one that has known value
8913 Opnd := Rorig;
8914 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
8915 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
8916 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
8918 -- Operand value of 0 or 1 skips warning
8920 if Lval <= 1 then
8921 return;
8923 -- Otherwise check power of 2, if power of 2, warn, if
8924 -- anything else, skip warning.
8926 else
8927 while Lval /= 2 loop
8928 if Lval mod 2 = 1 then
8929 return;
8930 else
8931 Lval := Lval / 2;
8932 end if;
8933 end loop;
8935 exit Opnd_Loop;
8936 end if;
8937 end if;
8939 -- Keep looking at left operands
8941 Opnd := Left_Opnd (Opnd);
8942 end loop Opnd_Loop;
8944 -- For rem or "/" we can only have a problematic situation
8945 -- if the divisor has a value of minus one or one. Otherwise
8946 -- overflow is impossible (divisor > 1) or we have a case of
8947 -- division by zero in any case.
8949 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
8950 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
8951 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
8952 then
8953 return;
8954 end if;
8956 -- If we fall through warning should be issued
8958 Error_Msg_N
8959 ("?unary minus expression should be parenthesized here!", N);
8960 end if;
8961 end if;
8962 end;
8963 end Resolve_Unary_Op;
8965 ----------------------------------
8966 -- Resolve_Unchecked_Expression --
8967 ----------------------------------
8969 procedure Resolve_Unchecked_Expression
8970 (N : Node_Id;
8971 Typ : Entity_Id)
8973 begin
8974 Resolve (Expression (N), Typ, Suppress => All_Checks);
8975 Set_Etype (N, Typ);
8976 end Resolve_Unchecked_Expression;
8978 ---------------------------------------
8979 -- Resolve_Unchecked_Type_Conversion --
8980 ---------------------------------------
8982 procedure Resolve_Unchecked_Type_Conversion
8983 (N : Node_Id;
8984 Typ : Entity_Id)
8986 pragma Warnings (Off, Typ);
8988 Operand : constant Node_Id := Expression (N);
8989 Opnd_Type : constant Entity_Id := Etype (Operand);
8991 begin
8992 -- Resolve operand using its own type
8994 Resolve (Operand, Opnd_Type);
8995 Eval_Unchecked_Conversion (N);
8997 end Resolve_Unchecked_Type_Conversion;
8999 ------------------------------
9000 -- Rewrite_Operator_As_Call --
9001 ------------------------------
9003 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9004 Loc : constant Source_Ptr := Sloc (N);
9005 Actuals : constant List_Id := New_List;
9006 New_N : Node_Id;
9008 begin
9009 if Nkind (N) in N_Binary_Op then
9010 Append (Left_Opnd (N), Actuals);
9011 end if;
9013 Append (Right_Opnd (N), Actuals);
9015 New_N :=
9016 Make_Function_Call (Sloc => Loc,
9017 Name => New_Occurrence_Of (Nam, Loc),
9018 Parameter_Associations => Actuals);
9020 Preserve_Comes_From_Source (New_N, N);
9021 Preserve_Comes_From_Source (Name (New_N), N);
9022 Rewrite (N, New_N);
9023 Set_Etype (N, Etype (Nam));
9024 end Rewrite_Operator_As_Call;
9026 ------------------------------
9027 -- Rewrite_Renamed_Operator --
9028 ------------------------------
9030 procedure Rewrite_Renamed_Operator
9031 (N : Node_Id;
9032 Op : Entity_Id;
9033 Typ : Entity_Id)
9035 Nam : constant Name_Id := Chars (Op);
9036 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9037 Op_Node : Node_Id;
9039 begin
9040 -- Rewrite the operator node using the real operator, not its renaming.
9041 -- Exclude user-defined intrinsic operations of the same name, which are
9042 -- treated separately and rewritten as calls.
9044 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9045 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9046 Set_Chars (Op_Node, Nam);
9047 Set_Etype (Op_Node, Etype (N));
9048 Set_Entity (Op_Node, Op);
9049 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9051 -- Indicate that both the original entity and its renaming are
9052 -- referenced at this point.
9054 Generate_Reference (Entity (N), N);
9055 Generate_Reference (Op, N);
9057 if Is_Binary then
9058 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9059 end if;
9061 Rewrite (N, Op_Node);
9063 -- If the context type is private, add the appropriate conversions
9064 -- so that the operator is applied to the full view. This is done
9065 -- in the routines that resolve intrinsic operators,
9067 if Is_Intrinsic_Subprogram (Op)
9068 and then Is_Private_Type (Typ)
9069 then
9070 case Nkind (N) is
9071 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9072 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
9073 Resolve_Intrinsic_Operator (N, Typ);
9075 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
9076 Resolve_Intrinsic_Unary_Operator (N, Typ);
9078 when others =>
9079 Resolve (N, Typ);
9080 end case;
9081 end if;
9083 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9085 -- Operator renames a user-defined operator of the same name. Use
9086 -- the original operator in the node, which is the one that Gigi
9087 -- knows about.
9089 Set_Entity (N, Op);
9090 Set_Is_Overloaded (N, False);
9091 end if;
9092 end Rewrite_Renamed_Operator;
9094 -----------------------
9095 -- Set_Slice_Subtype --
9096 -----------------------
9098 -- Build an implicit subtype declaration to represent the type delivered
9099 -- by the slice. This is an abbreviated version of an array subtype. We
9100 -- define an index subtype for the slice, using either the subtype name
9101 -- or the discrete range of the slice. To be consistent with index usage
9102 -- elsewhere, we create a list header to hold the single index. This list
9103 -- is not otherwise attached to the syntax tree.
9105 procedure Set_Slice_Subtype (N : Node_Id) is
9106 Loc : constant Source_Ptr := Sloc (N);
9107 Index_List : constant List_Id := New_List;
9108 Index : Node_Id;
9109 Index_Subtype : Entity_Id;
9110 Index_Type : Entity_Id;
9111 Slice_Subtype : Entity_Id;
9112 Drange : constant Node_Id := Discrete_Range (N);
9114 begin
9115 if Is_Entity_Name (Drange) then
9116 Index_Subtype := Entity (Drange);
9118 else
9119 -- We force the evaluation of a range. This is definitely needed in
9120 -- the renamed case, and seems safer to do unconditionally. Note in
9121 -- any case that since we will create and insert an Itype referring
9122 -- to this range, we must make sure any side effect removal actions
9123 -- are inserted before the Itype definition.
9125 if Nkind (Drange) = N_Range then
9126 Force_Evaluation (Low_Bound (Drange));
9127 Force_Evaluation (High_Bound (Drange));
9128 end if;
9130 Index_Type := Base_Type (Etype (Drange));
9132 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9134 -- Take a new copy of Drange (where bounds have been rewritten to
9135 -- reference side-effect-vree names). Using a separate tree ensures
9136 -- that further expansion (e.g while rewriting a slice assignment
9137 -- into a FOR loop) does not attempt to remove side effects on the
9138 -- bounds again (which would cause the bounds in the index subtype
9139 -- definition to refer to temporaries before they are defined) (the
9140 -- reason is that some names are considered side effect free here
9141 -- for the subtype, but not in the context of a loop iteration
9142 -- scheme).
9144 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9145 Set_Etype (Index_Subtype, Index_Type);
9146 Set_Size_Info (Index_Subtype, Index_Type);
9147 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9148 end if;
9150 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9152 Index := New_Occurrence_Of (Index_Subtype, Loc);
9153 Set_Etype (Index, Index_Subtype);
9154 Append (Index, Index_List);
9156 Set_First_Index (Slice_Subtype, Index);
9157 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
9158 Set_Is_Constrained (Slice_Subtype, True);
9160 Check_Compile_Time_Size (Slice_Subtype);
9162 -- The Etype of the existing Slice node is reset to this slice subtype.
9163 -- Its bounds are obtained from its first index.
9165 Set_Etype (N, Slice_Subtype);
9167 -- For packed slice subtypes, freeze immediately (except in the
9168 -- case of being in a "spec expression" where we never freeze
9169 -- when we first see the expression).
9171 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9172 Freeze_Itype (Slice_Subtype, N);
9174 -- For all other cases insert an itype reference in the slice's actions
9175 -- so that the itype is frozen at the proper place in the tree (i.e. at
9176 -- the point where actions for the slice are analyzed). Note that this
9177 -- is different from freezing the itype immediately, which might be
9178 -- premature (e.g. if the slice is within a transient scope).
9180 else
9181 Ensure_Defined (Typ => Slice_Subtype, N => N);
9182 end if;
9183 end Set_Slice_Subtype;
9185 --------------------------------
9186 -- Set_String_Literal_Subtype --
9187 --------------------------------
9189 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9190 Loc : constant Source_Ptr := Sloc (N);
9191 Low_Bound : constant Node_Id :=
9192 Type_Low_Bound (Etype (First_Index (Typ)));
9193 Subtype_Id : Entity_Id;
9195 begin
9196 if Nkind (N) /= N_String_Literal then
9197 return;
9198 end if;
9200 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
9201 Set_String_Literal_Length (Subtype_Id, UI_From_Int
9202 (String_Length (Strval (N))));
9203 Set_Etype (Subtype_Id, Base_Type (Typ));
9204 Set_Is_Constrained (Subtype_Id);
9205 Set_Etype (N, Subtype_Id);
9207 if Is_OK_Static_Expression (Low_Bound) then
9209 -- The low bound is set from the low bound of the corresponding
9210 -- index type. Note that we do not store the high bound in the
9211 -- string literal subtype, but it can be deduced if necessary
9212 -- from the length and the low bound.
9214 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
9216 else
9217 Set_String_Literal_Low_Bound
9218 (Subtype_Id, Make_Integer_Literal (Loc, 1));
9219 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
9221 -- Build bona fide subtype for the string, and wrap it in an
9222 -- unchecked conversion, because the backend expects the
9223 -- String_Literal_Subtype to have a static lower bound.
9225 declare
9226 Index_List : constant List_Id := New_List;
9227 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
9228 High_Bound : constant Node_Id :=
9229 Make_Op_Add (Loc,
9230 Left_Opnd => New_Copy_Tree (Low_Bound),
9231 Right_Opnd =>
9232 Make_Integer_Literal (Loc,
9233 String_Length (Strval (N)) - 1));
9234 Array_Subtype : Entity_Id;
9235 Index_Subtype : Entity_Id;
9236 Drange : Node_Id;
9237 Index : Node_Id;
9239 begin
9240 Index_Subtype :=
9241 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9242 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
9243 Set_Scalar_Range (Index_Subtype, Drange);
9244 Set_Parent (Drange, N);
9245 Analyze_And_Resolve (Drange, Index_Type);
9247 -- In the context, the Index_Type may already have a constraint,
9248 -- so use common base type on string subtype. The base type may
9249 -- be used when generating attributes of the string, for example
9250 -- in the context of a slice assignment.
9252 Set_Etype (Index_Subtype, Base_Type (Index_Type));
9253 Set_Size_Info (Index_Subtype, Index_Type);
9254 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
9256 Array_Subtype := Create_Itype (E_Array_Subtype, N);
9258 Index := New_Occurrence_Of (Index_Subtype, Loc);
9259 Set_Etype (Index, Index_Subtype);
9260 Append (Index, Index_List);
9262 Set_First_Index (Array_Subtype, Index);
9263 Set_Etype (Array_Subtype, Base_Type (Typ));
9264 Set_Is_Constrained (Array_Subtype, True);
9266 Rewrite (N,
9267 Make_Unchecked_Type_Conversion (Loc,
9268 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9269 Expression => Relocate_Node (N)));
9270 Set_Etype (N, Array_Subtype);
9271 end;
9272 end if;
9273 end Set_String_Literal_Subtype;
9275 ------------------------------
9276 -- Simplify_Type_Conversion --
9277 ------------------------------
9279 procedure Simplify_Type_Conversion (N : Node_Id) is
9280 begin
9281 if Nkind (N) = N_Type_Conversion then
9282 declare
9283 Operand : constant Node_Id := Expression (N);
9284 Target_Typ : constant Entity_Id := Etype (N);
9285 Opnd_Typ : constant Entity_Id := Etype (Operand);
9287 begin
9288 if Is_Floating_Point_Type (Opnd_Typ)
9289 and then
9290 (Is_Integer_Type (Target_Typ)
9291 or else (Is_Fixed_Point_Type (Target_Typ)
9292 and then Conversion_OK (N)))
9293 and then Nkind (Operand) = N_Attribute_Reference
9294 and then Attribute_Name (Operand) = Name_Truncation
9296 -- Special processing required if the conversion is the expression
9297 -- of a Truncation attribute reference. In this case we replace:
9299 -- ityp (ftyp'Truncation (x))
9301 -- by
9303 -- ityp (x)
9305 -- with the Float_Truncate flag set, which is more efficient
9307 then
9308 Rewrite (Operand,
9309 Relocate_Node (First (Expressions (Operand))));
9310 Set_Float_Truncate (N, True);
9311 end if;
9312 end;
9313 end if;
9314 end Simplify_Type_Conversion;
9316 -----------------------------
9317 -- Unique_Fixed_Point_Type --
9318 -----------------------------
9320 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
9321 T1 : Entity_Id := Empty;
9322 T2 : Entity_Id;
9323 Item : Node_Id;
9324 Scop : Entity_Id;
9326 procedure Fixed_Point_Error;
9327 -- Give error messages for true ambiguity. Messages are posted on node
9328 -- N, and entities T1, T2 are the possible interpretations.
9330 -----------------------
9331 -- Fixed_Point_Error --
9332 -----------------------
9334 procedure Fixed_Point_Error is
9335 begin
9336 Error_Msg_N ("ambiguous universal_fixed_expression", N);
9337 Error_Msg_NE ("\\possible interpretation as}", N, T1);
9338 Error_Msg_NE ("\\possible interpretation as}", N, T2);
9339 end Fixed_Point_Error;
9341 -- Start of processing for Unique_Fixed_Point_Type
9343 begin
9344 -- The operations on Duration are visible, so Duration is always a
9345 -- possible interpretation.
9347 T1 := Standard_Duration;
9349 -- Look for fixed-point types in enclosing scopes
9351 Scop := Current_Scope;
9352 while Scop /= Standard_Standard loop
9353 T2 := First_Entity (Scop);
9354 while Present (T2) loop
9355 if Is_Fixed_Point_Type (T2)
9356 and then Current_Entity (T2) = T2
9357 and then Scope (Base_Type (T2)) = Scop
9358 then
9359 if Present (T1) then
9360 Fixed_Point_Error;
9361 return Any_Type;
9362 else
9363 T1 := T2;
9364 end if;
9365 end if;
9367 Next_Entity (T2);
9368 end loop;
9370 Scop := Scope (Scop);
9371 end loop;
9373 -- Look for visible fixed type declarations in the context
9375 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
9376 while Present (Item) loop
9377 if Nkind (Item) = N_With_Clause then
9378 Scop := Entity (Name (Item));
9379 T2 := First_Entity (Scop);
9380 while Present (T2) loop
9381 if Is_Fixed_Point_Type (T2)
9382 and then Scope (Base_Type (T2)) = Scop
9383 and then (Is_Potentially_Use_Visible (T2)
9384 or else In_Use (T2))
9385 then
9386 if Present (T1) then
9387 Fixed_Point_Error;
9388 return Any_Type;
9389 else
9390 T1 := T2;
9391 end if;
9392 end if;
9394 Next_Entity (T2);
9395 end loop;
9396 end if;
9398 Next (Item);
9399 end loop;
9401 if Nkind (N) = N_Real_Literal then
9402 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
9403 else
9404 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
9405 end if;
9407 return T1;
9408 end Unique_Fixed_Point_Type;
9410 ----------------------
9411 -- Valid_Conversion --
9412 ----------------------
9414 function Valid_Conversion
9415 (N : Node_Id;
9416 Target : Entity_Id;
9417 Operand : Node_Id) return Boolean
9419 Target_Type : constant Entity_Id := Base_Type (Target);
9420 Opnd_Type : Entity_Id := Etype (Operand);
9422 function Conversion_Check
9423 (Valid : Boolean;
9424 Msg : String) return Boolean;
9425 -- Little routine to post Msg if Valid is False, returns Valid value
9427 function Valid_Tagged_Conversion
9428 (Target_Type : Entity_Id;
9429 Opnd_Type : Entity_Id) return Boolean;
9430 -- Specifically test for validity of tagged conversions
9432 function Valid_Array_Conversion return Boolean;
9433 -- Check index and component conformance, and accessibility levels
9434 -- if the component types are anonymous access types (Ada 2005)
9436 ----------------------
9437 -- Conversion_Check --
9438 ----------------------
9440 function Conversion_Check
9441 (Valid : Boolean;
9442 Msg : String) return Boolean
9444 begin
9445 if not Valid then
9446 Error_Msg_N (Msg, Operand);
9447 end if;
9449 return Valid;
9450 end Conversion_Check;
9452 ----------------------------
9453 -- Valid_Array_Conversion --
9454 ----------------------------
9456 function Valid_Array_Conversion return Boolean
9458 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
9459 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
9461 Opnd_Index : Node_Id;
9462 Opnd_Index_Type : Entity_Id;
9464 Target_Comp_Type : constant Entity_Id :=
9465 Component_Type (Target_Type);
9466 Target_Comp_Base : constant Entity_Id :=
9467 Base_Type (Target_Comp_Type);
9469 Target_Index : Node_Id;
9470 Target_Index_Type : Entity_Id;
9472 begin
9473 -- Error if wrong number of dimensions
9476 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
9477 then
9478 Error_Msg_N
9479 ("incompatible number of dimensions for conversion", Operand);
9480 return False;
9482 -- Number of dimensions matches
9484 else
9485 -- Loop through indexes of the two arrays
9487 Target_Index := First_Index (Target_Type);
9488 Opnd_Index := First_Index (Opnd_Type);
9489 while Present (Target_Index) and then Present (Opnd_Index) loop
9490 Target_Index_Type := Etype (Target_Index);
9491 Opnd_Index_Type := Etype (Opnd_Index);
9493 -- Error if index types are incompatible
9495 if not (Is_Integer_Type (Target_Index_Type)
9496 and then Is_Integer_Type (Opnd_Index_Type))
9497 and then (Root_Type (Target_Index_Type)
9498 /= Root_Type (Opnd_Index_Type))
9499 then
9500 Error_Msg_N
9501 ("incompatible index types for array conversion",
9502 Operand);
9503 return False;
9504 end if;
9506 Next_Index (Target_Index);
9507 Next_Index (Opnd_Index);
9508 end loop;
9510 -- If component types have same base type, all set
9512 if Target_Comp_Base = Opnd_Comp_Base then
9513 null;
9515 -- Here if base types of components are not the same. The only
9516 -- time this is allowed is if we have anonymous access types.
9518 -- The conversion of arrays of anonymous access types can lead
9519 -- to dangling pointers. AI-392 formalizes the accessibility
9520 -- checks that must be applied to such conversions to prevent
9521 -- out-of-scope references.
9523 elsif
9524 Ekind_In (Target_Comp_Base, E_Anonymous_Access_Type,
9525 E_Anonymous_Access_Subprogram_Type)
9526 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
9527 and then
9528 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
9529 then
9530 if Type_Access_Level (Target_Type) <
9531 Type_Access_Level (Opnd_Type)
9532 then
9533 if In_Instance_Body then
9534 Error_Msg_N ("?source array type " &
9535 "has deeper accessibility level than target", Operand);
9536 Error_Msg_N ("\?Program_Error will be raised at run time",
9537 Operand);
9538 Rewrite (N,
9539 Make_Raise_Program_Error (Sloc (N),
9540 Reason => PE_Accessibility_Check_Failed));
9541 Set_Etype (N, Target_Type);
9542 return False;
9544 -- Conversion not allowed because of accessibility levels
9546 else
9547 Error_Msg_N ("source array type " &
9548 "has deeper accessibility level than target", Operand);
9549 return False;
9550 end if;
9551 else
9552 null;
9553 end if;
9555 -- All other cases where component base types do not match
9557 else
9558 Error_Msg_N
9559 ("incompatible component types for array conversion",
9560 Operand);
9561 return False;
9562 end if;
9564 -- Check that component subtypes statically match. For numeric
9565 -- types this means that both must be either constrained or
9566 -- unconstrained. For enumeration types the bounds must match.
9567 -- All of this is checked in Subtypes_Statically_Match.
9569 if not Subtypes_Statically_Match
9570 (Target_Comp_Type, Opnd_Comp_Type)
9571 then
9572 Error_Msg_N
9573 ("component subtypes must statically match", Operand);
9574 return False;
9575 end if;
9576 end if;
9578 return True;
9579 end Valid_Array_Conversion;
9581 -----------------------------
9582 -- Valid_Tagged_Conversion --
9583 -----------------------------
9585 function Valid_Tagged_Conversion
9586 (Target_Type : Entity_Id;
9587 Opnd_Type : Entity_Id) return Boolean
9589 begin
9590 -- Upward conversions are allowed (RM 4.6(22))
9592 if Covers (Target_Type, Opnd_Type)
9593 or else Is_Ancestor (Target_Type, Opnd_Type)
9594 then
9595 return True;
9597 -- Downward conversion are allowed if the operand is class-wide
9598 -- (RM 4.6(23)).
9600 elsif Is_Class_Wide_Type (Opnd_Type)
9601 and then Covers (Opnd_Type, Target_Type)
9602 then
9603 return True;
9605 elsif Covers (Opnd_Type, Target_Type)
9606 or else Is_Ancestor (Opnd_Type, Target_Type)
9607 then
9608 return
9609 Conversion_Check (False,
9610 "downward conversion of tagged objects not allowed");
9612 -- Ada 2005 (AI-251): The conversion to/from interface types is
9613 -- always valid
9615 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
9616 return True;
9618 -- If the operand is a class-wide type obtained through a limited_
9619 -- with clause, and the context includes the non-limited view, use
9620 -- it to determine whether the conversion is legal.
9622 elsif Is_Class_Wide_Type (Opnd_Type)
9623 and then From_With_Type (Opnd_Type)
9624 and then Present (Non_Limited_View (Etype (Opnd_Type)))
9625 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
9626 then
9627 return True;
9629 elsif Is_Access_Type (Opnd_Type)
9630 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
9631 then
9632 return True;
9634 else
9635 Error_Msg_NE
9636 ("invalid tagged conversion, not compatible with}",
9637 N, First_Subtype (Opnd_Type));
9638 return False;
9639 end if;
9640 end Valid_Tagged_Conversion;
9642 -- Start of processing for Valid_Conversion
9644 begin
9645 Check_Parameterless_Call (Operand);
9647 if Is_Overloaded (Operand) then
9648 declare
9649 I : Interp_Index;
9650 I1 : Interp_Index;
9651 It : Interp;
9652 It1 : Interp;
9653 N1 : Entity_Id;
9654 T1 : Entity_Id;
9656 begin
9657 -- Remove procedure calls, which syntactically cannot appear in
9658 -- this context, but which cannot be removed by type checking,
9659 -- because the context does not impose a type.
9661 -- When compiling for VMS, spurious ambiguities can be produced
9662 -- when arithmetic operations have a literal operand and return
9663 -- System.Address or a descendant of it. These ambiguities are
9664 -- otherwise resolved by the context, but for conversions there
9665 -- is no context type and the removal of the spurious operations
9666 -- must be done explicitly here.
9668 -- The node may be labelled overloaded, but still contain only
9669 -- one interpretation because others were discarded in previous
9670 -- filters. If this is the case, retain the single interpretation
9671 -- if legal.
9673 Get_First_Interp (Operand, I, It);
9674 Opnd_Type := It.Typ;
9675 Get_Next_Interp (I, It);
9677 if Present (It.Typ)
9678 and then Opnd_Type /= Standard_Void_Type
9679 then
9680 -- More than one candidate interpretation is available
9682 Get_First_Interp (Operand, I, It);
9683 while Present (It.Typ) loop
9684 if It.Typ = Standard_Void_Type then
9685 Remove_Interp (I);
9686 end if;
9688 if Present (System_Aux_Id)
9689 and then Is_Descendent_Of_Address (It.Typ)
9690 then
9691 Remove_Interp (I);
9692 end if;
9694 Get_Next_Interp (I, It);
9695 end loop;
9696 end if;
9698 Get_First_Interp (Operand, I, It);
9699 I1 := I;
9700 It1 := It;
9702 if No (It.Typ) then
9703 Error_Msg_N ("illegal operand in conversion", Operand);
9704 return False;
9705 end if;
9707 Get_Next_Interp (I, It);
9709 if Present (It.Typ) then
9710 N1 := It1.Nam;
9711 T1 := It1.Typ;
9712 It1 := Disambiguate (Operand, I1, I, Any_Type);
9714 if It1 = No_Interp then
9715 Error_Msg_N ("ambiguous operand in conversion", Operand);
9717 -- If the interpretation involves a standard operator, use
9718 -- the location of the type, which may be user-defined.
9720 if Sloc (It.Nam) = Standard_Location then
9721 Error_Msg_Sloc := Sloc (It.Typ);
9722 else
9723 Error_Msg_Sloc := Sloc (It.Nam);
9724 end if;
9726 Error_Msg_N -- CODEFIX
9727 ("\\possible interpretation#!", Operand);
9729 if Sloc (N1) = Standard_Location then
9730 Error_Msg_Sloc := Sloc (T1);
9731 else
9732 Error_Msg_Sloc := Sloc (N1);
9733 end if;
9735 Error_Msg_N -- CODEFIX
9736 ("\\possible interpretation#!", Operand);
9738 return False;
9739 end if;
9740 end if;
9742 Set_Etype (Operand, It1.Typ);
9743 Opnd_Type := It1.Typ;
9744 end;
9745 end if;
9747 -- Numeric types
9749 if Is_Numeric_Type (Target_Type) then
9751 -- A universal fixed expression can be converted to any numeric type
9753 if Opnd_Type = Universal_Fixed then
9754 return True;
9756 -- Also no need to check when in an instance or inlined body, because
9757 -- the legality has been established when the template was analyzed.
9758 -- Furthermore, numeric conversions may occur where only a private
9759 -- view of the operand type is visible at the instantiation point.
9760 -- This results in a spurious error if we check that the operand type
9761 -- is a numeric type.
9763 -- Note: in a previous version of this unit, the following tests were
9764 -- applied only for generated code (Comes_From_Source set to False),
9765 -- but in fact the test is required for source code as well, since
9766 -- this situation can arise in source code.
9768 elsif In_Instance or else In_Inlined_Body then
9769 return True;
9771 -- Otherwise we need the conversion check
9773 else
9774 return Conversion_Check
9775 (Is_Numeric_Type (Opnd_Type),
9776 "illegal operand for numeric conversion");
9777 end if;
9779 -- Array types
9781 elsif Is_Array_Type (Target_Type) then
9782 if not Is_Array_Type (Opnd_Type)
9783 or else Opnd_Type = Any_Composite
9784 or else Opnd_Type = Any_String
9785 then
9786 Error_Msg_N
9787 ("illegal operand for array conversion", Operand);
9788 return False;
9789 else
9790 return Valid_Array_Conversion;
9791 end if;
9793 -- Ada 2005 (AI-251): Anonymous access types where target references an
9794 -- interface type.
9796 elsif Ekind_In (Target_Type, E_General_Access_Type,
9797 E_Anonymous_Access_Type)
9798 and then Is_Interface (Directly_Designated_Type (Target_Type))
9799 then
9800 -- Check the static accessibility rule of 4.6(17). Note that the
9801 -- check is not enforced when within an instance body, since the
9802 -- RM requires such cases to be caught at run time.
9804 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
9805 if Type_Access_Level (Opnd_Type) >
9806 Type_Access_Level (Target_Type)
9807 then
9808 -- In an instance, this is a run-time check, but one we know
9809 -- will fail, so generate an appropriate warning. The raise
9810 -- will be generated by Expand_N_Type_Conversion.
9812 if In_Instance_Body then
9813 Error_Msg_N
9814 ("?cannot convert local pointer to non-local access type",
9815 Operand);
9816 Error_Msg_N
9817 ("\?Program_Error will be raised at run time", Operand);
9818 else
9819 Error_Msg_N
9820 ("cannot convert local pointer to non-local access type",
9821 Operand);
9822 return False;
9823 end if;
9825 -- Special accessibility checks are needed in the case of access
9826 -- discriminants declared for a limited type.
9828 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
9829 and then not Is_Local_Anonymous_Access (Opnd_Type)
9830 then
9831 -- When the operand is a selected access discriminant the check
9832 -- needs to be made against the level of the object denoted by
9833 -- the prefix of the selected name (Object_Access_Level handles
9834 -- checking the prefix of the operand for this case).
9836 if Nkind (Operand) = N_Selected_Component
9837 and then Object_Access_Level (Operand) >
9838 Type_Access_Level (Target_Type)
9839 then
9840 -- In an instance, this is a run-time check, but one we know
9841 -- will fail, so generate an appropriate warning. The raise
9842 -- will be generated by Expand_N_Type_Conversion.
9844 if In_Instance_Body then
9845 Error_Msg_N
9846 ("?cannot convert access discriminant to non-local" &
9847 " access type", Operand);
9848 Error_Msg_N
9849 ("\?Program_Error will be raised at run time", Operand);
9850 else
9851 Error_Msg_N
9852 ("cannot convert access discriminant to non-local" &
9853 " access type", Operand);
9854 return False;
9855 end if;
9856 end if;
9858 -- The case of a reference to an access discriminant from
9859 -- within a limited type declaration (which will appear as
9860 -- a discriminal) is always illegal because the level of the
9861 -- discriminant is considered to be deeper than any (nameable)
9862 -- access type.
9864 if Is_Entity_Name (Operand)
9865 and then not Is_Local_Anonymous_Access (Opnd_Type)
9866 and then
9867 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
9868 and then Present (Discriminal_Link (Entity (Operand)))
9869 then
9870 Error_Msg_N
9871 ("discriminant has deeper accessibility level than target",
9872 Operand);
9873 return False;
9874 end if;
9875 end if;
9876 end if;
9878 return True;
9880 -- General and anonymous access types
9882 elsif Ekind_In (Target_Type, E_General_Access_Type,
9883 E_Anonymous_Access_Type)
9884 and then
9885 Conversion_Check
9886 (Is_Access_Type (Opnd_Type)
9887 and then not
9888 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
9889 E_Access_Protected_Subprogram_Type),
9890 "must be an access-to-object type")
9891 then
9892 if Is_Access_Constant (Opnd_Type)
9893 and then not Is_Access_Constant (Target_Type)
9894 then
9895 Error_Msg_N
9896 ("access-to-constant operand type not allowed", Operand);
9897 return False;
9898 end if;
9900 -- Check the static accessibility rule of 4.6(17). Note that the
9901 -- check is not enforced when within an instance body, since the RM
9902 -- requires such cases to be caught at run time.
9904 if Ekind (Target_Type) /= E_Anonymous_Access_Type
9905 or else Is_Local_Anonymous_Access (Target_Type)
9906 then
9907 if Type_Access_Level (Opnd_Type)
9908 > Type_Access_Level (Target_Type)
9909 then
9910 -- In an instance, this is a run-time check, but one we know
9911 -- will fail, so generate an appropriate warning. The raise
9912 -- will be generated by Expand_N_Type_Conversion.
9914 if In_Instance_Body then
9915 Error_Msg_N
9916 ("?cannot convert local pointer to non-local access type",
9917 Operand);
9918 Error_Msg_N
9919 ("\?Program_Error will be raised at run time", Operand);
9921 else
9922 -- Avoid generation of spurious error message
9924 if not Error_Posted (N) then
9925 Error_Msg_N
9926 ("cannot convert local pointer to non-local access type",
9927 Operand);
9928 end if;
9930 return False;
9931 end if;
9933 -- Special accessibility checks are needed in the case of access
9934 -- discriminants declared for a limited type.
9936 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
9937 and then not Is_Local_Anonymous_Access (Opnd_Type)
9938 then
9939 -- When the operand is a selected access discriminant the check
9940 -- needs to be made against the level of the object denoted by
9941 -- the prefix of the selected name (Object_Access_Level handles
9942 -- checking the prefix of the operand for this case).
9944 if Nkind (Operand) = N_Selected_Component
9945 and then Object_Access_Level (Operand) >
9946 Type_Access_Level (Target_Type)
9947 then
9948 -- In an instance, this is a run-time check, but one we know
9949 -- will fail, so generate an appropriate warning. The raise
9950 -- will be generated by Expand_N_Type_Conversion.
9952 if In_Instance_Body then
9953 Error_Msg_N
9954 ("?cannot convert access discriminant to non-local" &
9955 " access type", Operand);
9956 Error_Msg_N
9957 ("\?Program_Error will be raised at run time",
9958 Operand);
9960 else
9961 Error_Msg_N
9962 ("cannot convert access discriminant to non-local" &
9963 " access type", Operand);
9964 return False;
9965 end if;
9966 end if;
9968 -- The case of a reference to an access discriminant from
9969 -- within a limited type declaration (which will appear as
9970 -- a discriminal) is always illegal because the level of the
9971 -- discriminant is considered to be deeper than any (nameable)
9972 -- access type.
9974 if Is_Entity_Name (Operand)
9975 and then
9976 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
9977 and then Present (Discriminal_Link (Entity (Operand)))
9978 then
9979 Error_Msg_N
9980 ("discriminant has deeper accessibility level than target",
9981 Operand);
9982 return False;
9983 end if;
9984 end if;
9985 end if;
9987 -- In the presence of limited_with clauses we have to use non-limited
9988 -- views, if available.
9990 Check_Limited : declare
9991 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
9992 -- Helper function to handle limited views
9994 --------------------------
9995 -- Full_Designated_Type --
9996 --------------------------
9998 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
9999 Desig : constant Entity_Id := Designated_Type (T);
10001 begin
10002 -- Handle the limited view of a type
10004 if Is_Incomplete_Type (Desig)
10005 and then From_With_Type (Desig)
10006 and then Present (Non_Limited_View (Desig))
10007 then
10008 return Available_View (Desig);
10009 else
10010 return Desig;
10011 end if;
10012 end Full_Designated_Type;
10014 -- Local Declarations
10016 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10017 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10019 Same_Base : constant Boolean :=
10020 Base_Type (Target) = Base_Type (Opnd);
10022 -- Start of processing for Check_Limited
10024 begin
10025 if Is_Tagged_Type (Target) then
10026 return Valid_Tagged_Conversion (Target, Opnd);
10028 else
10029 if not Same_Base then
10030 Error_Msg_NE
10031 ("target designated type not compatible with }",
10032 N, Base_Type (Opnd));
10033 return False;
10035 -- Ada 2005 AI-384: legality rule is symmetric in both
10036 -- designated types. The conversion is legal (with possible
10037 -- constraint check) if either designated type is
10038 -- unconstrained.
10040 elsif Subtypes_Statically_Match (Target, Opnd)
10041 or else
10042 (Has_Discriminants (Target)
10043 and then
10044 (not Is_Constrained (Opnd)
10045 or else not Is_Constrained (Target)))
10046 then
10047 -- Special case, if Value_Size has been used to make the
10048 -- sizes different, the conversion is not allowed even
10049 -- though the subtypes statically match.
10051 if Known_Static_RM_Size (Target)
10052 and then Known_Static_RM_Size (Opnd)
10053 and then RM_Size (Target) /= RM_Size (Opnd)
10054 then
10055 Error_Msg_NE
10056 ("target designated subtype not compatible with }",
10057 N, Opnd);
10058 Error_Msg_NE
10059 ("\because sizes of the two designated subtypes differ",
10060 N, Opnd);
10061 return False;
10063 -- Normal case where conversion is allowed
10065 else
10066 return True;
10067 end if;
10069 else
10070 Error_Msg_NE
10071 ("target designated subtype not compatible with }",
10072 N, Opnd);
10073 return False;
10074 end if;
10075 end if;
10076 end Check_Limited;
10078 -- Access to subprogram types. If the operand is an access parameter,
10079 -- the type has a deeper accessibility that any master, and cannot
10080 -- be assigned. We must make an exception if the conversion is part
10081 -- of an assignment and the target is the return object of an extended
10082 -- return statement, because in that case the accessibility check
10083 -- takes place after the return.
10085 elsif Is_Access_Subprogram_Type (Target_Type)
10086 and then No (Corresponding_Remote_Type (Opnd_Type))
10087 then
10088 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10089 and then Is_Entity_Name (Operand)
10090 and then Ekind (Entity (Operand)) = E_In_Parameter
10091 and then
10092 (Nkind (Parent (N)) /= N_Assignment_Statement
10093 or else not Is_Entity_Name (Name (Parent (N)))
10094 or else not Is_Return_Object (Entity (Name (Parent (N)))))
10095 then
10096 Error_Msg_N
10097 ("illegal attempt to store anonymous access to subprogram",
10098 Operand);
10099 Error_Msg_N
10100 ("\value has deeper accessibility than any master " &
10101 "(RM 3.10.2 (13))",
10102 Operand);
10104 Error_Msg_NE
10105 ("\use named access type for& instead of access parameter",
10106 Operand, Entity (Operand));
10107 end if;
10109 -- Check that the designated types are subtype conformant
10111 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
10112 Old_Id => Designated_Type (Opnd_Type),
10113 Err_Loc => N);
10115 -- Check the static accessibility rule of 4.6(20)
10117 if Type_Access_Level (Opnd_Type) >
10118 Type_Access_Level (Target_Type)
10119 then
10120 Error_Msg_N
10121 ("operand type has deeper accessibility level than target",
10122 Operand);
10124 -- Check that if the operand type is declared in a generic body,
10125 -- then the target type must be declared within that same body
10126 -- (enforces last sentence of 4.6(20)).
10128 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
10129 declare
10130 O_Gen : constant Node_Id :=
10131 Enclosing_Generic_Body (Opnd_Type);
10133 T_Gen : Node_Id;
10135 begin
10136 T_Gen := Enclosing_Generic_Body (Target_Type);
10137 while Present (T_Gen) and then T_Gen /= O_Gen loop
10138 T_Gen := Enclosing_Generic_Body (T_Gen);
10139 end loop;
10141 if T_Gen /= O_Gen then
10142 Error_Msg_N
10143 ("target type must be declared in same generic body"
10144 & " as operand type", N);
10145 end if;
10146 end;
10147 end if;
10149 return True;
10151 -- Remote subprogram access types
10153 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
10154 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
10155 then
10156 -- It is valid to convert from one RAS type to another provided
10157 -- that their specification statically match.
10159 Check_Subtype_Conformant
10160 (New_Id =>
10161 Designated_Type (Corresponding_Remote_Type (Target_Type)),
10162 Old_Id =>
10163 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
10164 Err_Loc =>
10166 return True;
10168 -- If both are tagged types, check legality of view conversions
10170 elsif Is_Tagged_Type (Target_Type)
10171 and then Is_Tagged_Type (Opnd_Type)
10172 then
10173 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
10175 -- Types derived from the same root type are convertible
10177 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
10178 return True;
10180 -- In an instance or an inlined body, there may be inconsistent
10181 -- views of the same type, or of types derived from a common root.
10183 elsif (In_Instance or In_Inlined_Body)
10184 and then
10185 Root_Type (Underlying_Type (Target_Type)) =
10186 Root_Type (Underlying_Type (Opnd_Type))
10187 then
10188 return True;
10190 -- Special check for common access type error case
10192 elsif Ekind (Target_Type) = E_Access_Type
10193 and then Is_Access_Type (Opnd_Type)
10194 then
10195 Error_Msg_N ("target type must be general access type!", N);
10196 Error_Msg_NE -- CODEFIX
10197 ("add ALL to }!", N, Target_Type);
10198 return False;
10200 else
10201 Error_Msg_NE ("invalid conversion, not compatible with }",
10202 N, Opnd_Type);
10203 return False;
10204 end if;
10205 end Valid_Conversion;
10207 end Sem_Res;