Daily bump.
[official-gcc.git] / gcc / ada / sem_res.adb
blob21d3e145d332b4ac3a1f4253a7df38e3dba33c3a
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-2012, 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 Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Itypes; use Itypes;
41 with Lib; use Lib;
42 with Lib.Xref; use Lib.Xref;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
46 with Opt; use Opt;
47 with Output; use Output;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Dim; use Sem_Dim;
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 Targparm; use Targparm;
69 with Sem_Type; use Sem_Type;
70 with Sem_Warn; use Sem_Warn;
71 with Sinfo; use Sinfo;
72 with Sinfo.CN; use Sinfo.CN;
73 with Snames; use Snames;
74 with Stand; use Stand;
75 with Stringt; use Stringt;
76 with Style; use Style;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
81 package body Sem_Res is
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
87 -- Second pass (top-down) type checking and overload resolution procedures
88 -- Typ is the type required by context. These procedures propagate the type
89 -- information recursively to the descendants of N. If the node is not
90 -- overloaded, its Etype is established in the first pass. If overloaded,
91 -- the Resolve routines set the correct type. For arith. operators, the
92 -- Etype is the base type of the context.
94 -- Note that Resolve_Attribute is separated off in Sem_Attr
96 function Bad_Unordered_Enumeration_Reference
97 (N : Node_Id;
98 T : Entity_Id) return Boolean;
99 -- Node N contains a potentially dubious reference to type T, either an
100 -- explicit comparison, or an explicit range. This function returns True
101 -- if the type T is an enumeration type for which No pragma Order has been
102 -- given, and the reference N is not in the same extended source unit as
103 -- the declaration of T.
105 procedure Check_Discriminant_Use (N : Node_Id);
106 -- Enforce the restrictions on the use of discriminants when constraining
107 -- a component of a discriminated type (record or concurrent type).
109 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
110 -- Given a node for an operator associated with type T, check that
111 -- the operator is visible. Operators all of whose operands are
112 -- universal must be checked for visibility during resolution
113 -- because their type is not determinable based on their operands.
115 procedure Check_Fully_Declared_Prefix
116 (Typ : Entity_Id;
117 Pref : Node_Id);
118 -- Check that the type of the prefix of a dereference is not incomplete
120 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
121 -- Given a call node, N, which is known to occur immediately within the
122 -- subprogram being called, determines whether it is a detectable case of
123 -- an infinite recursion, and if so, outputs appropriate messages. Returns
124 -- True if an infinite recursion is detected, and False otherwise.
126 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
127 -- If the type of the object being initialized uses the secondary stack
128 -- directly or indirectly, create a transient scope for the call to the
129 -- init proc. This is because we do not create transient scopes for the
130 -- initialization of individual components within the init proc itself.
131 -- Could be optimized away perhaps?
133 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
134 -- N is the node for a logical operator. If the operator is predefined, and
135 -- the root type of the operands is Standard.Boolean, then a check is made
136 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
137 -- the style check for Style_Check_Boolean_And_Or.
139 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
140 -- Determine whether E is an access type declared by an access declaration,
141 -- and not an (anonymous) allocator type.
143 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
144 -- Utility to check whether the entity for an operator is a predefined
145 -- operator, in which case the expression is left as an operator in the
146 -- tree (else it is rewritten into a call). An instance of an intrinsic
147 -- conversion operation may be given an operator name, but is not treated
148 -- like an operator. Note that an operator that is an imported back-end
149 -- builtin has convention Intrinsic, but is expected to be rewritten into
150 -- a call, so such an operator is not treated as predefined by this
151 -- predicate.
153 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
154 -- If a default expression in entry call N depends on the discriminants
155 -- of the task, it must be replaced with a reference to the discriminant
156 -- of the task being called.
158 procedure Resolve_Op_Concat_Arg
159 (N : Node_Id;
160 Arg : Node_Id;
161 Typ : Entity_Id;
162 Is_Comp : Boolean);
163 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
164 -- concatenation operator. The operand is either of the array type or of
165 -- the component type. If the operand is an aggregate, and the component
166 -- type is composite, this is ambiguous if component type has aggregates.
168 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
169 -- Does the first part of the work of Resolve_Op_Concat
171 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
172 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
173 -- has been resolved. See Resolve_Op_Concat for details.
175 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
210 function Operator_Kind
211 (Op_Name : Name_Id;
212 Is_Binary : Boolean) return Node_Kind;
213 -- Utility to map the name of an operator into the corresponding Node. Used
214 -- by other node rewriting procedures.
216 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
217 -- Resolve actuals of call, and add default expressions for missing ones.
218 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
219 -- called subprogram.
221 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
222 -- Called from Resolve_Call, when the prefix denotes an entry or element
223 -- of entry family. Actuals are resolved as for subprograms, and the node
224 -- is rebuilt as an entry call. Also called for protected operations. Typ
225 -- is the context type, which is used when the operation is a protected
226 -- function with no arguments, and the return value is indexed.
228 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
229 -- A call to a user-defined intrinsic operator is rewritten as a call to
230 -- the corresponding predefined operator, with suitable conversions. Note
231 -- that this applies only for intrinsic operators that denote predefined
232 -- operators, not ones that are intrinsic imports of back-end builtins.
234 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
235 -- Ditto, for unary operators (arithmetic ones and "not" on signed
236 -- integer types for VMS).
238 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
239 -- If an operator node resolves to a call to a user-defined operator,
240 -- rewrite the node as a function call.
242 procedure Make_Call_Into_Operator
243 (N : Node_Id;
244 Typ : Entity_Id;
245 Op_Id : Entity_Id);
246 -- Inverse transformation: if an operator is given in functional notation,
247 -- then after resolving the node, transform into an operator node, so
248 -- that operands are resolved properly. Recall that predefined operators
249 -- do not have a full signature and special resolution rules apply.
251 procedure Rewrite_Renamed_Operator
252 (N : Node_Id;
253 Op : Entity_Id;
254 Typ : Entity_Id);
255 -- An operator can rename another, e.g. in an instantiation. In that
256 -- case, the proper operator node must be constructed and resolved.
258 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
259 -- The String_Literal_Subtype is built for all strings that are not
260 -- operands of a static concatenation operation. If the argument is
261 -- not a N_String_Literal node, then the call has no effect.
263 procedure Set_Slice_Subtype (N : Node_Id);
264 -- Build subtype of array type, with the range specified by the slice
266 procedure Simplify_Type_Conversion (N : Node_Id);
267 -- Called after N has been resolved and evaluated, but before range checks
268 -- have been applied. Currently simplifies a combination of floating-point
269 -- to integer conversion and Truncation attribute.
271 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
272 -- A universal_fixed expression in an universal context is unambiguous if
273 -- there is only one applicable fixed point type. Determining whether there
274 -- is only one requires a search over all visible entities, and happens
275 -- only in very pathological cases (see 6115-006).
277 -------------------------
278 -- Ambiguous_Character --
279 -------------------------
281 procedure Ambiguous_Character (C : Node_Id) is
282 E : Entity_Id;
284 begin
285 if Nkind (C) = N_Character_Literal then
286 Error_Msg_N ("ambiguous character literal", C);
288 -- First the ones in Standard
290 Error_Msg_N ("\\possible interpretation: Character!", C);
291 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
293 -- Include Wide_Wide_Character in Ada 2005 mode
295 if Ada_Version >= Ada_2005 then
296 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
297 end if;
299 -- Now any other types that match
301 E := Current_Entity (C);
302 while Present (E) loop
303 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
304 E := Homonym (E);
305 end loop;
306 end if;
307 end Ambiguous_Character;
309 -------------------------
310 -- Analyze_And_Resolve --
311 -------------------------
313 procedure Analyze_And_Resolve (N : Node_Id) is
314 begin
315 Analyze (N);
316 Resolve (N);
317 end Analyze_And_Resolve;
319 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
320 begin
321 Analyze (N);
322 Resolve (N, Typ);
323 end Analyze_And_Resolve;
325 -- Version withs check(s) suppressed
327 procedure Analyze_And_Resolve
328 (N : Node_Id;
329 Typ : Entity_Id;
330 Suppress : Check_Id)
332 Scop : constant Entity_Id := Current_Scope;
334 begin
335 if Suppress = All_Checks then
336 declare
337 Svg : constant Suppress_Record := Scope_Suppress;
338 begin
339 Scope_Suppress := Suppress_All;
340 Analyze_And_Resolve (N, Typ);
341 Scope_Suppress := Svg;
342 end;
344 else
345 declare
346 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
347 begin
348 Scope_Suppress.Suppress (Suppress) := True;
349 Analyze_And_Resolve (N, Typ);
350 Scope_Suppress.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 for an inner
358 -- expression, which will be removed upon completion of the analysis
359 -- of an enclosing construct. The transient scope must have the
360 -- suppress status of the enclosing environment, not of this Analyze
361 -- 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_Record := Scope_Suppress;
378 begin
379 Scope_Suppress := Suppress_All;
380 Analyze_And_Resolve (N);
381 Scope_Suppress := Svg;
382 end;
384 else
385 declare
386 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
387 begin
388 Scope_Suppress.Suppress (Suppress) := True;
389 Analyze_And_Resolve (N);
390 Scope_Suppress.Suppress (Suppress) := Svg;
391 end;
392 end if;
394 if Current_Scope /= Scop and then Scope_Is_Transient then
395 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
396 Scope_Suppress;
397 end if;
398 end Analyze_And_Resolve;
400 ----------------------------------------
401 -- Bad_Unordered_Enumeration_Reference --
402 ----------------------------------------
404 function Bad_Unordered_Enumeration_Reference
405 (N : Node_Id;
406 T : Entity_Id) return Boolean
408 begin
409 return Is_Enumeration_Type (T)
410 and then Comes_From_Source (N)
411 and then Warn_On_Unordered_Enumeration_Type
412 and then not Has_Pragma_Ordered (T)
413 and then not In_Same_Extended_Unit (N, T);
414 end Bad_Unordered_Enumeration_Reference;
416 ----------------------------
417 -- Check_Discriminant_Use --
418 ----------------------------
420 procedure Check_Discriminant_Use (N : Node_Id) is
421 PN : constant Node_Id := Parent (N);
422 Disc : constant Entity_Id := Entity (N);
423 P : Node_Id;
424 D : Node_Id;
426 begin
427 -- Any use in a spec-expression is legal
429 if In_Spec_Expression then
430 null;
432 elsif Nkind (PN) = N_Range then
434 -- Discriminant cannot be used to constrain a scalar type
436 P := Parent (PN);
438 if Nkind (P) = N_Range_Constraint
439 and then Nkind (Parent (P)) = N_Subtype_Indication
440 and then Nkind (Parent (Parent (P))) = N_Component_Definition
441 then
442 Error_Msg_N ("discriminant cannot constrain scalar type", N);
444 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
446 -- The following check catches the unusual case where a
447 -- discriminant appears within an index constraint that is part of
448 -- a larger expression within a constraint on a component, e.g. "C
449 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
450 -- of record components, and note that a similar check should also
451 -- apply in the case of discriminant constraints below. ???
453 -- Note that the check for N_Subtype_Declaration below is to
454 -- detect the valid use of discriminants in the constraints of a
455 -- subtype declaration when this subtype declaration appears
456 -- inside the scope of a record type (which is syntactically
457 -- illegal, but which may be created as part of derived type
458 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
459 -- for more info.
461 if Ekind (Current_Scope) = E_Record_Type
462 and then Scope (Disc) = Current_Scope
463 and then not
464 (Nkind (Parent (P)) = N_Subtype_Indication
465 and then
466 Nkind_In (Parent (Parent (P)), N_Component_Definition,
467 N_Subtype_Declaration)
468 and then Paren_Count (N) = 0)
469 then
470 Error_Msg_N
471 ("discriminant must appear alone in component constraint", N);
472 return;
473 end if;
475 -- Detect a common error:
477 -- type R (D : Positive := 100) is record
478 -- Name : String (1 .. D);
479 -- end record;
481 -- The default value causes an object of type R to be allocated
482 -- with room for Positive'Last characters. The RM does not mandate
483 -- the allocation of the maximum size, but that is what GNAT does
484 -- so we should warn the programmer that there is a problem.
486 Check_Large : declare
487 SI : Node_Id;
488 T : Entity_Id;
489 TB : Node_Id;
490 CB : Entity_Id;
492 function Large_Storage_Type (T : Entity_Id) return Boolean;
493 -- Return True if type T has a large enough range that any
494 -- array whose index type covered the whole range of the type
495 -- would likely raise Storage_Error.
497 ------------------------
498 -- Large_Storage_Type --
499 ------------------------
501 function Large_Storage_Type (T : Entity_Id) return Boolean is
502 begin
503 -- The type is considered large if its bounds are known at
504 -- compile time and if it requires at least as many bits as
505 -- a Positive to store the possible values.
507 return Compile_Time_Known_Value (Type_Low_Bound (T))
508 and then Compile_Time_Known_Value (Type_High_Bound (T))
509 and then
510 Minimum_Size (T, Biased => True) >=
511 RM_Size (Standard_Positive);
512 end Large_Storage_Type;
514 -- Start of processing for Check_Large
516 begin
517 -- Check that the Disc has a large range
519 if not Large_Storage_Type (Etype (Disc)) then
520 goto No_Danger;
521 end if;
523 -- If the enclosing type is limited, we allocate only the
524 -- default value, not the maximum, and there is no need for
525 -- a warning.
527 if Is_Limited_Type (Scope (Disc)) then
528 goto No_Danger;
529 end if;
531 -- Check that it is the high bound
533 if N /= High_Bound (PN)
534 or else No (Discriminant_Default_Value (Disc))
535 then
536 goto No_Danger;
537 end if;
539 -- Check the array allows a large range at this bound. First
540 -- find the array
542 SI := Parent (P);
544 if Nkind (SI) /= N_Subtype_Indication then
545 goto No_Danger;
546 end if;
548 T := Entity (Subtype_Mark (SI));
550 if not Is_Array_Type (T) then
551 goto No_Danger;
552 end if;
554 -- Next, find the dimension
556 TB := First_Index (T);
557 CB := First (Constraints (P));
558 while True
559 and then Present (TB)
560 and then Present (CB)
561 and then CB /= PN
562 loop
563 Next_Index (TB);
564 Next (CB);
565 end loop;
567 if CB /= PN then
568 goto No_Danger;
569 end if;
571 -- Now, check the dimension has a large range
573 if not Large_Storage_Type (Etype (TB)) then
574 goto No_Danger;
575 end if;
577 -- Warn about the danger
579 Error_Msg_N
580 ("?creation of & object may raise Storage_Error!",
581 Scope (Disc));
583 <<No_Danger>>
584 null;
586 end Check_Large;
587 end if;
589 -- Legal case is in index or discriminant constraint
591 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
592 N_Discriminant_Association)
593 then
594 if Paren_Count (N) > 0 then
595 Error_Msg_N
596 ("discriminant in constraint must appear alone", N);
598 elsif Nkind (N) = N_Expanded_Name
599 and then Comes_From_Source (N)
600 then
601 Error_Msg_N
602 ("discriminant must appear alone as a direct name", N);
603 end if;
605 return;
607 -- Otherwise, context is an expression. It should not be within (i.e. a
608 -- subexpression of) a constraint for a component.
610 else
611 D := PN;
612 P := Parent (PN);
613 while not Nkind_In (P, N_Component_Declaration,
614 N_Subtype_Indication,
615 N_Entry_Declaration)
616 loop
617 D := P;
618 P := Parent (P);
619 exit when No (P);
620 end loop;
622 -- If the discriminant is used in an expression that is a bound of a
623 -- scalar type, an Itype is created and the bounds are attached to
624 -- its range, not to the original subtype indication. Such use is of
625 -- course a double fault.
627 if (Nkind (P) = N_Subtype_Indication
628 and then Nkind_In (Parent (P), N_Component_Definition,
629 N_Derived_Type_Definition)
630 and then D = Constraint (P))
632 -- The constraint itself may be given by a subtype indication,
633 -- rather than by a more common discrete range.
635 or else (Nkind (P) = N_Subtype_Indication
636 and then
637 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
638 or else Nkind (P) = N_Entry_Declaration
639 or else Nkind (D) = N_Defining_Identifier
640 then
641 Error_Msg_N
642 ("discriminant in constraint must appear alone", N);
643 end if;
644 end if;
645 end Check_Discriminant_Use;
647 --------------------------------
648 -- Check_For_Visible_Operator --
649 --------------------------------
651 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
652 begin
653 if Is_Invisible_Operator (N, T) then
654 Error_Msg_NE -- CODEFIX
655 ("operator for} is not directly visible!", N, First_Subtype (T));
656 Error_Msg_N -- CODEFIX
657 ("use clause would make operation legal!", N);
658 end if;
659 end Check_For_Visible_Operator;
661 ----------------------------------
662 -- Check_Fully_Declared_Prefix --
663 ----------------------------------
665 procedure Check_Fully_Declared_Prefix
666 (Typ : Entity_Id;
667 Pref : Node_Id)
669 begin
670 -- Check that the designated type of the prefix of a dereference is
671 -- not an incomplete type. This cannot be done unconditionally, because
672 -- dereferences of private types are legal in default expressions. This
673 -- case is taken care of in Check_Fully_Declared, called below. There
674 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
676 -- This consideration also applies to similar checks for allocators,
677 -- qualified expressions, and type conversions.
679 -- An additional exception concerns other per-object expressions that
680 -- are not directly related to component declarations, in particular
681 -- representation pragmas for tasks. These will be per-object
682 -- expressions if they depend on discriminants or some global entity.
683 -- If the task has access discriminants, the designated type may be
684 -- incomplete at the point the expression is resolved. This resolution
685 -- takes place within the body of the initialization procedure, where
686 -- the discriminant is replaced by its discriminal.
688 if Is_Entity_Name (Pref)
689 and then Ekind (Entity (Pref)) = E_In_Parameter
690 then
691 null;
693 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
694 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
695 -- Analyze_Object_Renaming, and Freeze_Entity.
697 elsif Ada_Version >= Ada_2005
698 and then Is_Entity_Name (Pref)
699 and then Is_Access_Type (Etype (Pref))
700 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
701 E_Incomplete_Type
702 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
703 then
704 null;
705 else
706 Check_Fully_Declared (Typ, Parent (Pref));
707 end if;
708 end Check_Fully_Declared_Prefix;
710 ------------------------------
711 -- Check_Infinite_Recursion --
712 ------------------------------
714 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
715 P : Node_Id;
716 C : Node_Id;
718 function Same_Argument_List return Boolean;
719 -- Check whether list of actuals is identical to list of formals of
720 -- called function (which is also the enclosing scope).
722 ------------------------
723 -- Same_Argument_List --
724 ------------------------
726 function Same_Argument_List return Boolean is
727 A : Node_Id;
728 F : Entity_Id;
729 Subp : Entity_Id;
731 begin
732 if not Is_Entity_Name (Name (N)) then
733 return False;
734 else
735 Subp := Entity (Name (N));
736 end if;
738 F := First_Formal (Subp);
739 A := First_Actual (N);
740 while Present (F) and then Present (A) loop
741 if not Is_Entity_Name (A)
742 or else Entity (A) /= F
743 then
744 return False;
745 end if;
747 Next_Actual (A);
748 Next_Formal (F);
749 end loop;
751 return True;
752 end Same_Argument_List;
754 -- Start of processing for Check_Infinite_Recursion
756 begin
757 -- Special case, if this is a procedure call and is a call to the
758 -- current procedure with the same argument list, then this is for
759 -- sure an infinite recursion and we insert a call to raise SE.
761 if Is_List_Member (N)
762 and then List_Length (List_Containing (N)) = 1
763 and then Same_Argument_List
764 then
765 declare
766 P : constant Node_Id := Parent (N);
767 begin
768 if Nkind (P) = N_Handled_Sequence_Of_Statements
769 and then Nkind (Parent (P)) = N_Subprogram_Body
770 and then Is_Empty_List (Declarations (Parent (P)))
771 then
772 Error_Msg_N ("!?infinite recursion", N);
773 Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
774 Insert_Action (N,
775 Make_Raise_Storage_Error (Sloc (N),
776 Reason => SE_Infinite_Recursion));
777 return True;
778 end if;
779 end;
780 end if;
782 -- If not that special case, search up tree, quitting if we reach a
783 -- construct (e.g. a conditional) that tells us that this is not a
784 -- case for an infinite recursion warning.
786 C := N;
787 loop
788 P := Parent (C);
790 -- If no parent, then we were not inside a subprogram, this can for
791 -- example happen when processing certain pragmas in a spec. Just
792 -- return False in this case.
794 if No (P) then
795 return False;
796 end if;
798 -- Done if we get to subprogram body, this is definitely an infinite
799 -- recursion case if we did not find anything to stop us.
801 exit when Nkind (P) = N_Subprogram_Body;
803 -- If appearing in conditional, result is false
805 if Nkind_In (P, N_Or_Else,
806 N_And_Then,
807 N_Case_Expression,
808 N_Case_Statement,
809 N_Conditional_Expression,
810 N_If_Statement)
811 then
812 return False;
814 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
815 and then C /= First (Statements (P))
816 then
817 -- If the call is the expression of a return statement and the
818 -- actuals are identical to the formals, it's worth a warning.
819 -- However, we skip this if there is an immediately preceding
820 -- raise statement, since the call is never executed.
822 -- Furthermore, this corresponds to a common idiom:
824 -- function F (L : Thing) return Boolean is
825 -- begin
826 -- raise Program_Error;
827 -- return F (L);
828 -- end F;
830 -- for generating a stub function
832 if Nkind (Parent (N)) = N_Simple_Return_Statement
833 and then Same_Argument_List
834 then
835 exit when not Is_List_Member (Parent (N));
837 -- OK, return statement is in a statement list, look for raise
839 declare
840 Nod : Node_Id;
842 begin
843 -- Skip past N_Freeze_Entity nodes generated by expansion
845 Nod := Prev (Parent (N));
846 while Present (Nod)
847 and then Nkind (Nod) = N_Freeze_Entity
848 loop
849 Prev (Nod);
850 end loop;
852 -- If no raise statement, give warning. We look at the
853 -- original node, because in the case of "raise ... with
854 -- ...", the node has been transformed into a call.
856 exit when Nkind (Original_Node (Nod)) /= N_Raise_Statement
857 and then
858 (Nkind (Nod) not in N_Raise_xxx_Error
859 or else Present (Condition (Nod)));
860 end;
861 end if;
863 return False;
865 else
866 C := P;
867 end if;
868 end loop;
870 Error_Msg_N ("!?possible infinite recursion", N);
871 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
873 return True;
874 end Check_Infinite_Recursion;
876 -------------------------------
877 -- Check_Initialization_Call --
878 -------------------------------
880 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
881 Typ : constant Entity_Id := Etype (First_Formal (Nam));
883 function Uses_SS (T : Entity_Id) return Boolean;
884 -- Check whether the creation of an object of the type will involve
885 -- use of the secondary stack. If T is a record type, this is true
886 -- if the expression for some component uses the secondary stack, e.g.
887 -- through a call to a function that returns an unconstrained value.
888 -- False if T is controlled, because cleanups occur elsewhere.
890 -------------
891 -- Uses_SS --
892 -------------
894 function Uses_SS (T : Entity_Id) return Boolean is
895 Comp : Entity_Id;
896 Expr : Node_Id;
897 Full_Type : Entity_Id := Underlying_Type (T);
899 begin
900 -- Normally we want to use the underlying type, but if it's not set
901 -- then continue with T.
903 if not Present (Full_Type) then
904 Full_Type := T;
905 end if;
907 if Is_Controlled (Full_Type) then
908 return False;
910 elsif Is_Array_Type (Full_Type) then
911 return Uses_SS (Component_Type (Full_Type));
913 elsif Is_Record_Type (Full_Type) then
914 Comp := First_Component (Full_Type);
915 while Present (Comp) loop
916 if Ekind (Comp) = E_Component
917 and then Nkind (Parent (Comp)) = N_Component_Declaration
918 then
919 -- The expression for a dynamic component may be rewritten
920 -- as a dereference, so retrieve original node.
922 Expr := Original_Node (Expression (Parent (Comp)));
924 -- Return True if the expression is a call to a function
925 -- (including an attribute function such as Image, or a
926 -- user-defined operator) with a result that requires a
927 -- transient scope.
929 if (Nkind (Expr) = N_Function_Call
930 or else Nkind (Expr) in N_Op
931 or else (Nkind (Expr) = N_Attribute_Reference
932 and then Present (Expressions (Expr))))
933 and then Requires_Transient_Scope (Etype (Expr))
934 then
935 return True;
937 elsif Uses_SS (Etype (Comp)) then
938 return True;
939 end if;
940 end if;
942 Next_Component (Comp);
943 end loop;
945 return False;
947 else
948 return False;
949 end if;
950 end Uses_SS;
952 -- Start of processing for Check_Initialization_Call
954 begin
955 -- Establish a transient scope if the type needs it
957 if Uses_SS (Typ) then
958 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
959 end if;
960 end Check_Initialization_Call;
962 ---------------------------------------
963 -- Check_No_Direct_Boolean_Operators --
964 ---------------------------------------
966 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
967 begin
968 if Scope (Entity (N)) = Standard_Standard
969 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
970 then
971 -- Restriction only applies to original source code
973 if Comes_From_Source (N) then
974 Check_Restriction (No_Direct_Boolean_Operators, N);
975 end if;
976 end if;
978 if Style_Check then
979 Check_Boolean_Operator (N);
980 end if;
981 end Check_No_Direct_Boolean_Operators;
983 ------------------------------
984 -- Check_Parameterless_Call --
985 ------------------------------
987 procedure Check_Parameterless_Call (N : Node_Id) is
988 Nam : Node_Id;
990 function Prefix_Is_Access_Subp return Boolean;
991 -- If the prefix is of an access_to_subprogram type, the node must be
992 -- rewritten as a call. Ditto if the prefix is overloaded and all its
993 -- interpretations are access to subprograms.
995 ---------------------------
996 -- Prefix_Is_Access_Subp --
997 ---------------------------
999 function Prefix_Is_Access_Subp return Boolean is
1000 I : Interp_Index;
1001 It : Interp;
1003 begin
1004 -- If the context is an attribute reference that can apply to
1005 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1007 if Nkind (Parent (N)) = N_Attribute_Reference
1008 and then (Attribute_Name (Parent (N)) = Name_Address or else
1009 Attribute_Name (Parent (N)) = Name_Code_Address or else
1010 Attribute_Name (Parent (N)) = Name_Access)
1011 then
1012 return False;
1013 end if;
1015 if not Is_Overloaded (N) then
1016 return
1017 Ekind (Etype (N)) = E_Subprogram_Type
1018 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1019 else
1020 Get_First_Interp (N, I, It);
1021 while Present (It.Typ) loop
1022 if Ekind (It.Typ) /= E_Subprogram_Type
1023 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1024 then
1025 return False;
1026 end if;
1028 Get_Next_Interp (I, It);
1029 end loop;
1031 return True;
1032 end if;
1033 end Prefix_Is_Access_Subp;
1035 -- Start of processing for Check_Parameterless_Call
1037 begin
1038 -- Defend against junk stuff if errors already detected
1040 if Total_Errors_Detected /= 0 then
1041 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1042 return;
1043 elsif Nkind (N) in N_Has_Chars
1044 and then Chars (N) in Error_Name_Or_No_Name
1045 then
1046 return;
1047 end if;
1049 Require_Entity (N);
1050 end if;
1052 -- If the context expects a value, and the name is a procedure, this is
1053 -- most likely a missing 'Access. Don't try to resolve the parameterless
1054 -- call, error will be caught when the outer call is analyzed.
1056 if Is_Entity_Name (N)
1057 and then Ekind (Entity (N)) = E_Procedure
1058 and then not Is_Overloaded (N)
1059 and then
1060 Nkind_In (Parent (N), N_Parameter_Association,
1061 N_Function_Call,
1062 N_Procedure_Call_Statement)
1063 then
1064 return;
1065 end if;
1067 -- Rewrite as call if overloadable entity that is (or could be, in the
1068 -- overloaded case) a function call. If we know for sure that the entity
1069 -- is an enumeration literal, we do not rewrite it.
1071 -- If the entity is the name of an operator, it cannot be a call because
1072 -- operators cannot have default parameters. In this case, this must be
1073 -- a string whose contents coincide with an operator name. Set the kind
1074 -- of the node appropriately.
1076 if (Is_Entity_Name (N)
1077 and then Nkind (N) /= N_Operator_Symbol
1078 and then Is_Overloadable (Entity (N))
1079 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1080 or else Is_Overloaded (N)))
1082 -- Rewrite as call if it is an explicit dereference of an expression of
1083 -- a subprogram access type, and the subprogram type is not that of a
1084 -- procedure or entry.
1086 or else
1087 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1089 -- Rewrite as call if it is a selected component which is a function,
1090 -- this is the case of a call to a protected function (which may be
1091 -- overloaded with other protected operations).
1093 or else
1094 (Nkind (N) = N_Selected_Component
1095 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1096 or else
1097 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1098 E_Procedure)
1099 and then Is_Overloaded (Selector_Name (N)))))
1101 -- If one of the above three conditions is met, rewrite as call. Apply
1102 -- the rewriting only once.
1104 then
1105 if Nkind (Parent (N)) /= N_Function_Call
1106 or else N /= Name (Parent (N))
1107 then
1109 -- This may be a prefixed call that was not fully analyzed, e.g.
1110 -- an actual in an instance.
1112 if Ada_Version >= Ada_2005
1113 and then Nkind (N) = N_Selected_Component
1114 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1115 then
1116 Analyze_Selected_Component (N);
1118 if Nkind (N) /= N_Selected_Component then
1119 return;
1120 end if;
1121 end if;
1123 Nam := New_Copy (N);
1125 -- If overloaded, overload set belongs to new copy
1127 Save_Interps (N, Nam);
1129 -- Change node to parameterless function call (note that the
1130 -- Parameter_Associations associations field is left set to Empty,
1131 -- its normal default value since there are no parameters)
1133 Change_Node (N, N_Function_Call);
1134 Set_Name (N, Nam);
1135 Set_Sloc (N, Sloc (Nam));
1136 Analyze_Call (N);
1137 end if;
1139 elsif Nkind (N) = N_Parameter_Association then
1140 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1142 elsif Nkind (N) = N_Operator_Symbol then
1143 Change_Operator_Symbol_To_String_Literal (N);
1144 Set_Is_Overloaded (N, False);
1145 Set_Etype (N, Any_String);
1146 end if;
1147 end Check_Parameterless_Call;
1149 -----------------------------
1150 -- Is_Definite_Access_Type --
1151 -----------------------------
1153 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1154 Btyp : constant Entity_Id := Base_Type (E);
1155 begin
1156 return Ekind (Btyp) = E_Access_Type
1157 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1158 and then Comes_From_Source (Btyp));
1159 end Is_Definite_Access_Type;
1161 ----------------------
1162 -- Is_Predefined_Op --
1163 ----------------------
1165 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1166 begin
1167 -- Predefined operators are intrinsic subprograms
1169 if not Is_Intrinsic_Subprogram (Nam) then
1170 return False;
1171 end if;
1173 -- A call to a back-end builtin is never a predefined operator
1175 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1176 return False;
1177 end if;
1179 return not Is_Generic_Instance (Nam)
1180 and then Chars (Nam) in Any_Operator_Name
1181 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1182 end Is_Predefined_Op;
1184 -----------------------------
1185 -- Make_Call_Into_Operator --
1186 -----------------------------
1188 procedure Make_Call_Into_Operator
1189 (N : Node_Id;
1190 Typ : Entity_Id;
1191 Op_Id : Entity_Id)
1193 Op_Name : constant Name_Id := Chars (Op_Id);
1194 Act1 : Node_Id := First_Actual (N);
1195 Act2 : Node_Id := Next_Actual (Act1);
1196 Error : Boolean := False;
1197 Func : constant Entity_Id := Entity (Name (N));
1198 Is_Binary : constant Boolean := Present (Act2);
1199 Op_Node : Node_Id;
1200 Opnd_Type : Entity_Id;
1201 Orig_Type : Entity_Id := Empty;
1202 Pack : Entity_Id;
1204 type Kind_Test is access function (E : Entity_Id) return Boolean;
1206 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1207 -- If the operand is not universal, and the operator is given by an
1208 -- expanded name, verify that the operand has an interpretation with a
1209 -- type defined in the given scope of the operator.
1211 function Type_In_P (Test : Kind_Test) return Entity_Id;
1212 -- Find a type of the given class in package Pack that contains the
1213 -- operator.
1215 ---------------------------
1216 -- Operand_Type_In_Scope --
1217 ---------------------------
1219 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1220 Nod : constant Node_Id := Right_Opnd (Op_Node);
1221 I : Interp_Index;
1222 It : Interp;
1224 begin
1225 if not Is_Overloaded (Nod) then
1226 return Scope (Base_Type (Etype (Nod))) = S;
1228 else
1229 Get_First_Interp (Nod, I, It);
1230 while Present (It.Typ) loop
1231 if Scope (Base_Type (It.Typ)) = S then
1232 return True;
1233 end if;
1235 Get_Next_Interp (I, It);
1236 end loop;
1238 return False;
1239 end if;
1240 end Operand_Type_In_Scope;
1242 ---------------
1243 -- Type_In_P --
1244 ---------------
1246 function Type_In_P (Test : Kind_Test) return Entity_Id is
1247 E : Entity_Id;
1249 function In_Decl return Boolean;
1250 -- Verify that node is not part of the type declaration for the
1251 -- candidate type, which would otherwise be invisible.
1253 -------------
1254 -- In_Decl --
1255 -------------
1257 function In_Decl return Boolean is
1258 Decl_Node : constant Node_Id := Parent (E);
1259 N2 : Node_Id;
1261 begin
1262 N2 := N;
1264 if Etype (E) = Any_Type then
1265 return True;
1267 elsif No (Decl_Node) then
1268 return False;
1270 else
1271 while Present (N2)
1272 and then Nkind (N2) /= N_Compilation_Unit
1273 loop
1274 if N2 = Decl_Node then
1275 return True;
1276 else
1277 N2 := Parent (N2);
1278 end if;
1279 end loop;
1281 return False;
1282 end if;
1283 end In_Decl;
1285 -- Start of processing for Type_In_P
1287 begin
1288 -- If the context type is declared in the prefix package, this is the
1289 -- desired base type.
1291 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1292 return Base_Type (Typ);
1294 else
1295 E := First_Entity (Pack);
1296 while Present (E) loop
1297 if Test (E)
1298 and then not In_Decl
1299 then
1300 return E;
1301 end if;
1303 Next_Entity (E);
1304 end loop;
1306 return Empty;
1307 end if;
1308 end Type_In_P;
1310 -- Start of processing for Make_Call_Into_Operator
1312 begin
1313 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1315 -- Binary operator
1317 if Is_Binary then
1318 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1319 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1320 Save_Interps (Act1, Left_Opnd (Op_Node));
1321 Save_Interps (Act2, Right_Opnd (Op_Node));
1322 Act1 := Left_Opnd (Op_Node);
1323 Act2 := Right_Opnd (Op_Node);
1325 -- Unary operator
1327 else
1328 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1329 Save_Interps (Act1, Right_Opnd (Op_Node));
1330 Act1 := Right_Opnd (Op_Node);
1331 end if;
1333 -- If the operator is denoted by an expanded name, and the prefix is
1334 -- not Standard, but the operator is a predefined one whose scope is
1335 -- Standard, then this is an implicit_operator, inserted as an
1336 -- interpretation by the procedure of the same name. This procedure
1337 -- overestimates the presence of implicit operators, because it does
1338 -- not examine the type of the operands. Verify now that the operand
1339 -- type appears in the given scope. If right operand is universal,
1340 -- check the other operand. In the case of concatenation, either
1341 -- argument can be the component type, so check the type of the result.
1342 -- If both arguments are literals, look for a type of the right kind
1343 -- defined in the given scope. This elaborate nonsense is brought to
1344 -- you courtesy of b33302a. The type itself must be frozen, so we must
1345 -- find the type of the proper class in the given scope.
1347 -- A final wrinkle is the multiplication operator for fixed point types,
1348 -- which is defined in Standard only, and not in the scope of the
1349 -- fixed point type itself.
1351 if Nkind (Name (N)) = N_Expanded_Name then
1352 Pack := Entity (Prefix (Name (N)));
1354 -- If the entity being called is defined in the given package, it is
1355 -- a renaming of a predefined operator, and known to be legal.
1357 if Scope (Entity (Name (N))) = Pack
1358 and then Pack /= Standard_Standard
1359 then
1360 null;
1362 -- Visibility does not need to be checked in an instance: if the
1363 -- operator was not visible in the generic it has been diagnosed
1364 -- already, else there is an implicit copy of it in the instance.
1366 elsif In_Instance then
1367 null;
1369 elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide)
1370 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1371 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1372 then
1373 if Pack /= Standard_Standard then
1374 Error := True;
1375 end if;
1377 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1378 -- available.
1380 elsif Ada_Version >= Ada_2005
1381 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1382 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1383 then
1384 null;
1386 else
1387 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1389 if Op_Name = Name_Op_Concat then
1390 Opnd_Type := Base_Type (Typ);
1392 elsif (Scope (Opnd_Type) = Standard_Standard
1393 and then Is_Binary)
1394 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1395 and then Is_Binary
1396 and then not Comes_From_Source (Opnd_Type))
1397 then
1398 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1399 end if;
1401 if Scope (Opnd_Type) = Standard_Standard then
1403 -- Verify that the scope contains a type that corresponds to
1404 -- the given literal. Optimize the case where Pack is Standard.
1406 if Pack /= Standard_Standard then
1408 if Opnd_Type = Universal_Integer then
1409 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1411 elsif Opnd_Type = Universal_Real then
1412 Orig_Type := Type_In_P (Is_Real_Type'Access);
1414 elsif Opnd_Type = Any_String then
1415 Orig_Type := Type_In_P (Is_String_Type'Access);
1417 elsif Opnd_Type = Any_Access then
1418 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1420 elsif Opnd_Type = Any_Composite then
1421 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1423 if Present (Orig_Type) then
1424 if Has_Private_Component (Orig_Type) then
1425 Orig_Type := Empty;
1426 else
1427 Set_Etype (Act1, Orig_Type);
1429 if Is_Binary then
1430 Set_Etype (Act2, Orig_Type);
1431 end if;
1432 end if;
1433 end if;
1435 else
1436 Orig_Type := Empty;
1437 end if;
1439 Error := No (Orig_Type);
1440 end if;
1442 elsif Ekind (Opnd_Type) = E_Allocator_Type
1443 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1444 then
1445 Error := True;
1447 -- If the type is defined elsewhere, and the operator is not
1448 -- defined in the given scope (by a renaming declaration, e.g.)
1449 -- then this is an error as well. If an extension of System is
1450 -- present, and the type may be defined there, Pack must be
1451 -- System itself.
1453 elsif Scope (Opnd_Type) /= Pack
1454 and then Scope (Op_Id) /= Pack
1455 and then (No (System_Aux_Id)
1456 or else Scope (Opnd_Type) /= System_Aux_Id
1457 or else Pack /= Scope (System_Aux_Id))
1458 then
1459 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1460 Error := True;
1461 else
1462 Error := not Operand_Type_In_Scope (Pack);
1463 end if;
1465 elsif Pack = Standard_Standard
1466 and then not Operand_Type_In_Scope (Standard_Standard)
1467 then
1468 Error := True;
1469 end if;
1470 end if;
1472 if Error then
1473 Error_Msg_Node_2 := Pack;
1474 Error_Msg_NE
1475 ("& not declared in&", N, Selector_Name (Name (N)));
1476 Set_Etype (N, Any_Type);
1477 return;
1479 -- Detect a mismatch between the context type and the result type
1480 -- in the named package, which is otherwise not detected if the
1481 -- operands are universal. Check is only needed if source entity is
1482 -- an operator, not a function that renames an operator.
1484 elsif Nkind (Parent (N)) /= N_Type_Conversion
1485 and then Ekind (Entity (Name (N))) = E_Operator
1486 and then Is_Numeric_Type (Typ)
1487 and then not Is_Universal_Numeric_Type (Typ)
1488 and then Scope (Base_Type (Typ)) /= Pack
1489 and then not In_Instance
1490 then
1491 if Is_Fixed_Point_Type (Typ)
1492 and then (Op_Name = Name_Op_Multiply
1493 or else
1494 Op_Name = Name_Op_Divide)
1495 then
1496 -- Already checked above
1498 null;
1500 -- Operator may be defined in an extension of System
1502 elsif Present (System_Aux_Id)
1503 and then Scope (Opnd_Type) = System_Aux_Id
1504 then
1505 null;
1507 else
1508 -- Could we use Wrong_Type here??? (this would require setting
1509 -- Etype (N) to the actual type found where Typ was expected).
1511 Error_Msg_NE ("expect }", N, Typ);
1512 end if;
1513 end if;
1514 end if;
1516 Set_Chars (Op_Node, Op_Name);
1518 if not Is_Private_Type (Etype (N)) then
1519 Set_Etype (Op_Node, Base_Type (Etype (N)));
1520 else
1521 Set_Etype (Op_Node, Etype (N));
1522 end if;
1524 -- If this is a call to a function that renames a predefined equality,
1525 -- the renaming declaration provides a type that must be used to
1526 -- resolve the operands. This must be done now because resolution of
1527 -- the equality node will not resolve any remaining ambiguity, and it
1528 -- assumes that the first operand is not overloaded.
1530 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1531 and then Ekind (Func) = E_Function
1532 and then Is_Overloaded (Act1)
1533 then
1534 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1535 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1536 end if;
1538 Set_Entity (Op_Node, Op_Id);
1539 Generate_Reference (Op_Id, N, ' ');
1541 -- Do rewrite setting Comes_From_Source on the result if the original
1542 -- call came from source. Although it is not strictly the case that the
1543 -- operator as such comes from the source, logically it corresponds
1544 -- exactly to the function call in the source, so it should be marked
1545 -- this way (e.g. to make sure that validity checks work fine).
1547 declare
1548 CS : constant Boolean := Comes_From_Source (N);
1549 begin
1550 Rewrite (N, Op_Node);
1551 Set_Comes_From_Source (N, CS);
1552 end;
1554 -- If this is an arithmetic operator and the result type is private,
1555 -- the operands and the result must be wrapped in conversion to
1556 -- expose the underlying numeric type and expand the proper checks,
1557 -- e.g. on division.
1559 if Is_Private_Type (Typ) then
1560 case Nkind (N) is
1561 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1562 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1563 Resolve_Intrinsic_Operator (N, Typ);
1565 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1566 Resolve_Intrinsic_Unary_Operator (N, Typ);
1568 when others =>
1569 Resolve (N, Typ);
1570 end case;
1571 else
1572 Resolve (N, Typ);
1573 end if;
1574 end Make_Call_Into_Operator;
1576 -------------------
1577 -- Operator_Kind --
1578 -------------------
1580 function Operator_Kind
1581 (Op_Name : Name_Id;
1582 Is_Binary : Boolean) return Node_Kind
1584 Kind : Node_Kind;
1586 begin
1587 -- Use CASE statement or array???
1589 if Is_Binary then
1590 if Op_Name = Name_Op_And then
1591 Kind := N_Op_And;
1592 elsif Op_Name = Name_Op_Or then
1593 Kind := N_Op_Or;
1594 elsif Op_Name = Name_Op_Xor then
1595 Kind := N_Op_Xor;
1596 elsif Op_Name = Name_Op_Eq then
1597 Kind := N_Op_Eq;
1598 elsif Op_Name = Name_Op_Ne then
1599 Kind := N_Op_Ne;
1600 elsif Op_Name = Name_Op_Lt then
1601 Kind := N_Op_Lt;
1602 elsif Op_Name = Name_Op_Le then
1603 Kind := N_Op_Le;
1604 elsif Op_Name = Name_Op_Gt then
1605 Kind := N_Op_Gt;
1606 elsif Op_Name = Name_Op_Ge then
1607 Kind := N_Op_Ge;
1608 elsif Op_Name = Name_Op_Add then
1609 Kind := N_Op_Add;
1610 elsif Op_Name = Name_Op_Subtract then
1611 Kind := N_Op_Subtract;
1612 elsif Op_Name = Name_Op_Concat then
1613 Kind := N_Op_Concat;
1614 elsif Op_Name = Name_Op_Multiply then
1615 Kind := N_Op_Multiply;
1616 elsif Op_Name = Name_Op_Divide then
1617 Kind := N_Op_Divide;
1618 elsif Op_Name = Name_Op_Mod then
1619 Kind := N_Op_Mod;
1620 elsif Op_Name = Name_Op_Rem then
1621 Kind := N_Op_Rem;
1622 elsif Op_Name = Name_Op_Expon then
1623 Kind := N_Op_Expon;
1624 else
1625 raise Program_Error;
1626 end if;
1628 -- Unary operators
1630 else
1631 if Op_Name = Name_Op_Add then
1632 Kind := N_Op_Plus;
1633 elsif Op_Name = Name_Op_Subtract then
1634 Kind := N_Op_Minus;
1635 elsif Op_Name = Name_Op_Abs then
1636 Kind := N_Op_Abs;
1637 elsif Op_Name = Name_Op_Not then
1638 Kind := N_Op_Not;
1639 else
1640 raise Program_Error;
1641 end if;
1642 end if;
1644 return Kind;
1645 end Operator_Kind;
1647 ----------------------------
1648 -- Preanalyze_And_Resolve --
1649 ----------------------------
1651 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1652 Save_Full_Analysis : constant Boolean := Full_Analysis;
1654 begin
1655 Full_Analysis := False;
1656 Expander_Mode_Save_And_Set (False);
1658 -- We suppress all checks for this analysis, since the checks will
1659 -- be applied properly, and in the right location, when the default
1660 -- expression is reanalyzed and reexpanded later on.
1662 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1664 Expander_Mode_Restore;
1665 Full_Analysis := Save_Full_Analysis;
1666 end Preanalyze_And_Resolve;
1668 -- Version without context type
1670 procedure Preanalyze_And_Resolve (N : Node_Id) is
1671 Save_Full_Analysis : constant Boolean := Full_Analysis;
1673 begin
1674 Full_Analysis := False;
1675 Expander_Mode_Save_And_Set (False);
1677 Analyze (N);
1678 Resolve (N, Etype (N), Suppress => All_Checks);
1680 Expander_Mode_Restore;
1681 Full_Analysis := Save_Full_Analysis;
1682 end Preanalyze_And_Resolve;
1684 ----------------------------------
1685 -- Replace_Actual_Discriminants --
1686 ----------------------------------
1688 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1689 Loc : constant Source_Ptr := Sloc (N);
1690 Tsk : Node_Id := Empty;
1692 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1693 -- Comment needed???
1695 -------------------
1696 -- Process_Discr --
1697 -------------------
1699 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1700 Ent : Entity_Id;
1702 begin
1703 if Nkind (Nod) = N_Identifier then
1704 Ent := Entity (Nod);
1706 if Present (Ent)
1707 and then Ekind (Ent) = E_Discriminant
1708 then
1709 Rewrite (Nod,
1710 Make_Selected_Component (Loc,
1711 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1712 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1714 Set_Etype (Nod, Etype (Ent));
1715 end if;
1717 end if;
1719 return OK;
1720 end Process_Discr;
1722 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1724 -- Start of processing for Replace_Actual_Discriminants
1726 begin
1727 if not Full_Expander_Active then
1728 return;
1729 end if;
1731 if Nkind (Name (N)) = N_Selected_Component then
1732 Tsk := Prefix (Name (N));
1734 elsif Nkind (Name (N)) = N_Indexed_Component then
1735 Tsk := Prefix (Prefix (Name (N)));
1736 end if;
1738 if No (Tsk) then
1739 return;
1740 else
1741 Replace_Discrs (Default);
1742 end if;
1743 end Replace_Actual_Discriminants;
1745 -------------
1746 -- Resolve --
1747 -------------
1749 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1750 Ambiguous : Boolean := False;
1751 Ctx_Type : Entity_Id := Typ;
1752 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1753 Err_Type : Entity_Id := Empty;
1754 Found : Boolean := False;
1755 From_Lib : Boolean;
1756 I : Interp_Index;
1757 I1 : Interp_Index := 0; -- prevent junk warning
1758 It : Interp;
1759 It1 : Interp;
1760 Seen : Entity_Id := Empty; -- prevent junk warning
1762 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1763 -- Determine whether a node comes from a predefined library unit or
1764 -- Standard.
1766 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1767 -- Try and fix up a literal so that it matches its expected type. New
1768 -- literals are manufactured if necessary to avoid cascaded errors.
1770 function Proper_Current_Scope return Entity_Id;
1771 -- Return the current scope. Skip loop scopes created for the purpose of
1772 -- quantified expression analysis since those do not appear in the tree.
1774 procedure Report_Ambiguous_Argument;
1775 -- Additional diagnostics when an ambiguous call has an ambiguous
1776 -- argument (typically a controlling actual).
1778 procedure Resolution_Failed;
1779 -- Called when attempt at resolving current expression fails
1781 ------------------------------------
1782 -- Comes_From_Predefined_Lib_Unit --
1783 -------------------------------------
1785 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1786 begin
1787 return
1788 Sloc (Nod) = Standard_Location
1789 or else Is_Predefined_File_Name
1790 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1791 end Comes_From_Predefined_Lib_Unit;
1793 --------------------
1794 -- Patch_Up_Value --
1795 --------------------
1797 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1798 begin
1799 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1800 Rewrite (N,
1801 Make_Real_Literal (Sloc (N),
1802 Realval => UR_From_Uint (Intval (N))));
1803 Set_Etype (N, Universal_Real);
1804 Set_Is_Static_Expression (N);
1806 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1807 Rewrite (N,
1808 Make_Integer_Literal (Sloc (N),
1809 Intval => UR_To_Uint (Realval (N))));
1810 Set_Etype (N, Universal_Integer);
1811 Set_Is_Static_Expression (N);
1813 elsif Nkind (N) = N_String_Literal
1814 and then Is_Character_Type (Typ)
1815 then
1816 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1817 Rewrite (N,
1818 Make_Character_Literal (Sloc (N),
1819 Chars => Name_Find,
1820 Char_Literal_Value =>
1821 UI_From_Int (Character'Pos ('A'))));
1822 Set_Etype (N, Any_Character);
1823 Set_Is_Static_Expression (N);
1825 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1826 Rewrite (N,
1827 Make_String_Literal (Sloc (N),
1828 Strval => End_String));
1830 elsif Nkind (N) = N_Range then
1831 Patch_Up_Value (Low_Bound (N), Typ);
1832 Patch_Up_Value (High_Bound (N), Typ);
1833 end if;
1834 end Patch_Up_Value;
1836 --------------------------
1837 -- Proper_Current_Scope --
1838 --------------------------
1840 function Proper_Current_Scope return Entity_Id is
1841 S : Entity_Id := Current_Scope;
1843 begin
1844 while Present (S) loop
1846 -- Skip a loop scope created for quantified expression analysis
1848 if Ekind (S) = E_Loop
1849 and then Nkind (Parent (S)) = N_Quantified_Expression
1850 then
1851 S := Scope (S);
1852 else
1853 exit;
1854 end if;
1855 end loop;
1857 return S;
1858 end Proper_Current_Scope;
1860 -------------------------------
1861 -- Report_Ambiguous_Argument --
1862 -------------------------------
1864 procedure Report_Ambiguous_Argument is
1865 Arg : constant Node_Id := First (Parameter_Associations (N));
1866 I : Interp_Index;
1867 It : Interp;
1869 begin
1870 if Nkind (Arg) = N_Function_Call
1871 and then Is_Entity_Name (Name (Arg))
1872 and then Is_Overloaded (Name (Arg))
1873 then
1874 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1876 -- Could use comments on what is going on here???
1878 Get_First_Interp (Name (Arg), I, It);
1879 while Present (It.Nam) loop
1880 Error_Msg_Sloc := Sloc (It.Nam);
1882 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1883 Error_Msg_N ("interpretation (inherited) #!", Arg);
1884 else
1885 Error_Msg_N ("interpretation #!", Arg);
1886 end if;
1888 Get_Next_Interp (I, It);
1889 end loop;
1890 end if;
1891 end Report_Ambiguous_Argument;
1893 -----------------------
1894 -- Resolution_Failed --
1895 -----------------------
1897 procedure Resolution_Failed is
1898 begin
1899 Patch_Up_Value (N, Typ);
1900 Set_Etype (N, Typ);
1901 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1902 Set_Is_Overloaded (N, False);
1904 -- The caller will return without calling the expander, so we need
1905 -- to set the analyzed flag. Note that it is fine to set Analyzed
1906 -- to True even if we are in the middle of a shallow analysis,
1907 -- (see the spec of sem for more details) since this is an error
1908 -- situation anyway, and there is no point in repeating the
1909 -- analysis later (indeed it won't work to repeat it later, since
1910 -- we haven't got a clear resolution of which entity is being
1911 -- referenced.)
1913 Set_Analyzed (N, True);
1914 return;
1915 end Resolution_Failed;
1917 -- Start of processing for Resolve
1919 begin
1920 if N = Error then
1921 return;
1922 end if;
1924 -- Access attribute on remote subprogram cannot be used for a non-remote
1925 -- access-to-subprogram type.
1927 if Nkind (N) = N_Attribute_Reference
1928 and then (Attribute_Name (N) = Name_Access or else
1929 Attribute_Name (N) = Name_Unrestricted_Access or else
1930 Attribute_Name (N) = Name_Unchecked_Access)
1931 and then Comes_From_Source (N)
1932 and then Is_Entity_Name (Prefix (N))
1933 and then Is_Subprogram (Entity (Prefix (N)))
1934 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1935 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1936 then
1937 Error_Msg_N
1938 ("prefix must statically denote a non-remote subprogram", N);
1939 end if;
1941 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1943 -- If the context is a Remote_Access_To_Subprogram, access attributes
1944 -- must be resolved with the corresponding fat pointer. There is no need
1945 -- to check for the attribute name since the return type of an
1946 -- attribute is never a remote type.
1948 if Nkind (N) = N_Attribute_Reference
1949 and then Comes_From_Source (N)
1950 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1951 then
1952 declare
1953 Attr : constant Attribute_Id :=
1954 Get_Attribute_Id (Attribute_Name (N));
1955 Pref : constant Node_Id := Prefix (N);
1956 Decl : Node_Id;
1957 Spec : Node_Id;
1958 Is_Remote : Boolean := True;
1960 begin
1961 -- Check that Typ is a remote access-to-subprogram type
1963 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1965 -- Prefix (N) must statically denote a remote subprogram
1966 -- declared in a package specification.
1968 if Attr = Attribute_Access or else
1969 Attr = Attribute_Unchecked_Access or else
1970 Attr = Attribute_Unrestricted_Access
1971 then
1972 Decl := Unit_Declaration_Node (Entity (Pref));
1974 if Nkind (Decl) = N_Subprogram_Body then
1975 Spec := Corresponding_Spec (Decl);
1977 if not No (Spec) then
1978 Decl := Unit_Declaration_Node (Spec);
1979 end if;
1980 end if;
1982 Spec := Parent (Decl);
1984 if not Is_Entity_Name (Prefix (N))
1985 or else Nkind (Spec) /= N_Package_Specification
1986 or else
1987 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1988 then
1989 Is_Remote := False;
1990 Error_Msg_N
1991 ("prefix must statically denote a remote subprogram ",
1993 end if;
1995 -- If we are generating code in distributed mode, perform
1996 -- semantic checks against corresponding remote entities.
1998 if Full_Expander_Active
1999 and then Get_PCS_Name /= Name_No_DSA
2000 then
2001 Check_Subtype_Conformant
2002 (New_Id => Entity (Prefix (N)),
2003 Old_Id => Designated_Type
2004 (Corresponding_Remote_Type (Typ)),
2005 Err_Loc => N);
2007 if Is_Remote then
2008 Process_Remote_AST_Attribute (N, Typ);
2009 end if;
2010 end if;
2011 end if;
2012 end if;
2013 end;
2014 end if;
2016 Debug_A_Entry ("resolving ", N);
2018 if Debug_Flag_V then
2019 Write_Overloads (N);
2020 end if;
2022 if Comes_From_Source (N) then
2023 if Is_Fixed_Point_Type (Typ) then
2024 Check_Restriction (No_Fixed_Point, N);
2026 elsif Is_Floating_Point_Type (Typ)
2027 and then Typ /= Universal_Real
2028 and then Typ /= Any_Real
2029 then
2030 Check_Restriction (No_Floating_Point, N);
2031 end if;
2032 end if;
2034 -- Return if already analyzed
2036 if Analyzed (N) then
2037 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2038 Analyze_Dimension (N);
2039 return;
2041 -- Return if type = Any_Type (previous error encountered)
2043 elsif Etype (N) = Any_Type then
2044 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2045 return;
2046 end if;
2048 Check_Parameterless_Call (N);
2050 -- If not overloaded, then we know the type, and all that needs doing
2051 -- is to check that this type is compatible with the context.
2053 if not Is_Overloaded (N) then
2054 Found := Covers (Typ, Etype (N));
2055 Expr_Type := Etype (N);
2057 -- In the overloaded case, we must select the interpretation that
2058 -- is compatible with the context (i.e. the type passed to Resolve)
2060 else
2061 -- Loop through possible interpretations
2063 Get_First_Interp (N, I, It);
2064 Interp_Loop : while Present (It.Typ) loop
2066 if Debug_Flag_V then
2067 Write_Str ("Interp: ");
2068 Write_Interp (It);
2069 end if;
2071 -- We are only interested in interpretations that are compatible
2072 -- with the expected type, any other interpretations are ignored.
2074 if not Covers (Typ, It.Typ) then
2075 if Debug_Flag_V then
2076 Write_Str (" interpretation incompatible with context");
2077 Write_Eol;
2078 end if;
2080 else
2081 -- Skip the current interpretation if it is disabled by an
2082 -- abstract operator. This action is performed only when the
2083 -- type against which we are resolving is the same as the
2084 -- type of the interpretation.
2086 if Ada_Version >= Ada_2005
2087 and then It.Typ = Typ
2088 and then Typ /= Universal_Integer
2089 and then Typ /= Universal_Real
2090 and then Present (It.Abstract_Op)
2091 then
2092 if Debug_Flag_V then
2093 Write_Line ("Skip.");
2094 end if;
2096 goto Continue;
2097 end if;
2099 -- First matching interpretation
2101 if not Found then
2102 Found := True;
2103 I1 := I;
2104 Seen := It.Nam;
2105 Expr_Type := It.Typ;
2107 -- Matching interpretation that is not the first, maybe an
2108 -- error, but there are some cases where preference rules are
2109 -- used to choose between the two possibilities. These and
2110 -- some more obscure cases are handled in Disambiguate.
2112 else
2113 -- If the current statement is part of a predefined library
2114 -- unit, then all interpretations which come from user level
2115 -- packages should not be considered.
2117 if From_Lib
2118 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2119 then
2120 goto Continue;
2121 end if;
2123 Error_Msg_Sloc := Sloc (Seen);
2124 It1 := Disambiguate (N, I1, I, Typ);
2126 -- Disambiguation has succeeded. Skip the remaining
2127 -- interpretations.
2129 if It1 /= No_Interp then
2130 Seen := It1.Nam;
2131 Expr_Type := It1.Typ;
2133 while Present (It.Typ) loop
2134 Get_Next_Interp (I, It);
2135 end loop;
2137 else
2138 -- Before we issue an ambiguity complaint, check for
2139 -- the case of a subprogram call where at least one
2140 -- of the arguments is Any_Type, and if so, suppress
2141 -- the message, since it is a cascaded error.
2143 if Nkind (N) in N_Subprogram_Call then
2144 declare
2145 A : Node_Id;
2146 E : Node_Id;
2148 begin
2149 A := First_Actual (N);
2150 while Present (A) loop
2151 E := A;
2153 if Nkind (E) = N_Parameter_Association then
2154 E := Explicit_Actual_Parameter (E);
2155 end if;
2157 if Etype (E) = Any_Type then
2158 if Debug_Flag_V then
2159 Write_Str ("Any_Type in call");
2160 Write_Eol;
2161 end if;
2163 exit Interp_Loop;
2164 end if;
2166 Next_Actual (A);
2167 end loop;
2168 end;
2170 elsif Nkind (N) in N_Binary_Op
2171 and then (Etype (Left_Opnd (N)) = Any_Type
2172 or else Etype (Right_Opnd (N)) = Any_Type)
2173 then
2174 exit Interp_Loop;
2176 elsif Nkind (N) in N_Unary_Op
2177 and then Etype (Right_Opnd (N)) = Any_Type
2178 then
2179 exit Interp_Loop;
2180 end if;
2182 -- Not that special case, so issue message using the
2183 -- flag Ambiguous to control printing of the header
2184 -- message only at the start of an ambiguous set.
2186 if not Ambiguous then
2187 if Nkind (N) = N_Function_Call
2188 and then Nkind (Name (N)) = N_Explicit_Dereference
2189 then
2190 Error_Msg_N
2191 ("ambiguous expression "
2192 & "(cannot resolve indirect call)!", N);
2193 else
2194 Error_Msg_NE -- CODEFIX
2195 ("ambiguous expression (cannot resolve&)!",
2196 N, It.Nam);
2197 end if;
2199 Ambiguous := True;
2201 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2202 Error_Msg_N
2203 ("\\possible interpretation (inherited)#!", N);
2204 else
2205 Error_Msg_N -- CODEFIX
2206 ("\\possible interpretation#!", N);
2207 end if;
2209 if Nkind (N) in N_Subprogram_Call
2210 and then Present (Parameter_Associations (N))
2211 then
2212 Report_Ambiguous_Argument;
2213 end if;
2214 end if;
2216 Error_Msg_Sloc := Sloc (It.Nam);
2218 -- By default, the error message refers to the candidate
2219 -- interpretation. But if it is a predefined operator, it
2220 -- is implicitly declared at the declaration of the type
2221 -- of the operand. Recover the sloc of that declaration
2222 -- for the error message.
2224 if Nkind (N) in N_Op
2225 and then Scope (It.Nam) = Standard_Standard
2226 and then not Is_Overloaded (Right_Opnd (N))
2227 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2228 Standard_Standard
2229 then
2230 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2232 if Comes_From_Source (Err_Type)
2233 and then Present (Parent (Err_Type))
2234 then
2235 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2236 end if;
2238 elsif Nkind (N) in N_Binary_Op
2239 and then Scope (It.Nam) = Standard_Standard
2240 and then not Is_Overloaded (Left_Opnd (N))
2241 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2242 Standard_Standard
2243 then
2244 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2246 if Comes_From_Source (Err_Type)
2247 and then Present (Parent (Err_Type))
2248 then
2249 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2250 end if;
2252 -- If this is an indirect call, use the subprogram_type
2253 -- in the message, to have a meaningful location. Also
2254 -- indicate if this is an inherited operation, created
2255 -- by a type declaration.
2257 elsif Nkind (N) = N_Function_Call
2258 and then Nkind (Name (N)) = N_Explicit_Dereference
2259 and then Is_Type (It.Nam)
2260 then
2261 Err_Type := It.Nam;
2262 Error_Msg_Sloc :=
2263 Sloc (Associated_Node_For_Itype (Err_Type));
2264 else
2265 Err_Type := Empty;
2266 end if;
2268 if Nkind (N) in N_Op
2269 and then Scope (It.Nam) = Standard_Standard
2270 and then Present (Err_Type)
2271 then
2272 -- Special-case the message for universal_fixed
2273 -- operators, which are not declared with the type
2274 -- of the operand, but appear forever in Standard.
2276 if It.Typ = Universal_Fixed
2277 and then Scope (It.Nam) = Standard_Standard
2278 then
2279 Error_Msg_N
2280 ("\\possible interpretation as " &
2281 "universal_fixed operation " &
2282 "(RM 4.5.5 (19))", N);
2283 else
2284 Error_Msg_N
2285 ("\\possible interpretation (predefined)#!", N);
2286 end if;
2288 elsif
2289 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2290 then
2291 Error_Msg_N
2292 ("\\possible interpretation (inherited)#!", N);
2293 else
2294 Error_Msg_N -- CODEFIX
2295 ("\\possible interpretation#!", N);
2296 end if;
2298 end if;
2299 end if;
2301 -- We have a matching interpretation, Expr_Type is the type
2302 -- from this interpretation, and Seen is the entity.
2304 -- For an operator, just set the entity name. The type will be
2305 -- set by the specific operator resolution routine.
2307 if Nkind (N) in N_Op then
2308 Set_Entity (N, Seen);
2309 Generate_Reference (Seen, N);
2311 elsif Nkind (N) = N_Case_Expression then
2312 Set_Etype (N, Expr_Type);
2314 elsif Nkind (N) = N_Character_Literal then
2315 Set_Etype (N, Expr_Type);
2317 elsif Nkind (N) = N_Conditional_Expression then
2318 Set_Etype (N, Expr_Type);
2320 -- AI05-0139-2: Expression is overloaded because type has
2321 -- implicit dereference. If type matches context, no implicit
2322 -- dereference is involved.
2324 elsif Has_Implicit_Dereference (Expr_Type) then
2325 Set_Etype (N, Expr_Type);
2326 Set_Is_Overloaded (N, False);
2327 exit Interp_Loop;
2329 elsif Is_Overloaded (N)
2330 and then Present (It.Nam)
2331 and then Ekind (It.Nam) = E_Discriminant
2332 and then Has_Implicit_Dereference (It.Nam)
2333 then
2334 Build_Explicit_Dereference (N, It.Nam);
2336 -- For an explicit dereference, attribute reference, range,
2337 -- short-circuit form (which is not an operator node), or call
2338 -- with a name that is an explicit dereference, there is
2339 -- nothing to be done at this point.
2341 elsif Nkind_In (N, N_Explicit_Dereference,
2342 N_Attribute_Reference,
2343 N_And_Then,
2344 N_Indexed_Component,
2345 N_Or_Else,
2346 N_Range,
2347 N_Selected_Component,
2348 N_Slice)
2349 or else Nkind (Name (N)) = N_Explicit_Dereference
2350 then
2351 null;
2353 -- For procedure or function calls, set the type of the name,
2354 -- and also the entity pointer for the prefix.
2356 elsif Nkind (N) in N_Subprogram_Call
2357 and then Is_Entity_Name (Name (N))
2358 then
2359 Set_Etype (Name (N), Expr_Type);
2360 Set_Entity (Name (N), Seen);
2361 Generate_Reference (Seen, Name (N));
2363 elsif Nkind (N) = N_Function_Call
2364 and then Nkind (Name (N)) = N_Selected_Component
2365 then
2366 Set_Etype (Name (N), Expr_Type);
2367 Set_Entity (Selector_Name (Name (N)), Seen);
2368 Generate_Reference (Seen, Selector_Name (Name (N)));
2370 -- For all other cases, just set the type of the Name
2372 else
2373 Set_Etype (Name (N), Expr_Type);
2374 end if;
2376 end if;
2378 <<Continue>>
2380 -- Move to next interpretation
2382 exit Interp_Loop when No (It.Typ);
2384 Get_Next_Interp (I, It);
2385 end loop Interp_Loop;
2386 end if;
2388 -- At this stage Found indicates whether or not an acceptable
2389 -- interpretation exists. If not, then we have an error, except that if
2390 -- the context is Any_Type as a result of some other error, then we
2391 -- suppress the error report.
2393 if not Found then
2394 if Typ /= Any_Type then
2396 -- If type we are looking for is Void, then this is the procedure
2397 -- call case, and the error is simply that what we gave is not a
2398 -- procedure name (we think of procedure calls as expressions with
2399 -- types internally, but the user doesn't think of them this way!)
2401 if Typ = Standard_Void_Type then
2403 -- Special case message if function used as a procedure
2405 if Nkind (N) = N_Procedure_Call_Statement
2406 and then Is_Entity_Name (Name (N))
2407 and then Ekind (Entity (Name (N))) = E_Function
2408 then
2409 Error_Msg_NE
2410 ("cannot use function & in a procedure call",
2411 Name (N), Entity (Name (N)));
2413 -- Otherwise give general message (not clear what cases this
2414 -- covers, but no harm in providing for them!)
2416 else
2417 Error_Msg_N ("expect procedure name in procedure call", N);
2418 end if;
2420 Found := True;
2422 -- Otherwise we do have a subexpression with the wrong type
2424 -- Check for the case of an allocator which uses an access type
2425 -- instead of the designated type. This is a common error and we
2426 -- specialize the message, posting an error on the operand of the
2427 -- allocator, complaining that we expected the designated type of
2428 -- the allocator.
2430 elsif Nkind (N) = N_Allocator
2431 and then Ekind (Typ) in Access_Kind
2432 and then Ekind (Etype (N)) in Access_Kind
2433 and then Designated_Type (Etype (N)) = Typ
2434 then
2435 Wrong_Type (Expression (N), Designated_Type (Typ));
2436 Found := True;
2438 -- Check for view mismatch on Null in instances, for which the
2439 -- view-swapping mechanism has no identifier.
2441 elsif (In_Instance or else In_Inlined_Body)
2442 and then (Nkind (N) = N_Null)
2443 and then Is_Private_Type (Typ)
2444 and then Is_Access_Type (Full_View (Typ))
2445 then
2446 Resolve (N, Full_View (Typ));
2447 Set_Etype (N, Typ);
2448 return;
2450 -- Check for an aggregate. Sometimes we can get bogus aggregates
2451 -- from misuse of parentheses, and we are about to complain about
2452 -- the aggregate without even looking inside it.
2454 -- Instead, if we have an aggregate of type Any_Composite, then
2455 -- analyze and resolve the component fields, and then only issue
2456 -- another message if we get no errors doing this (otherwise
2457 -- assume that the errors in the aggregate caused the problem).
2459 elsif Nkind (N) = N_Aggregate
2460 and then Etype (N) = Any_Composite
2461 then
2462 -- Disable expansion in any case. If there is a type mismatch
2463 -- it may be fatal to try to expand the aggregate. The flag
2464 -- would otherwise be set to false when the error is posted.
2466 Expander_Active := False;
2468 declare
2469 procedure Check_Aggr (Aggr : Node_Id);
2470 -- Check one aggregate, and set Found to True if we have a
2471 -- definite error in any of its elements
2473 procedure Check_Elmt (Aelmt : Node_Id);
2474 -- Check one element of aggregate and set Found to True if
2475 -- we definitely have an error in the element.
2477 ----------------
2478 -- Check_Aggr --
2479 ----------------
2481 procedure Check_Aggr (Aggr : Node_Id) is
2482 Elmt : Node_Id;
2484 begin
2485 if Present (Expressions (Aggr)) then
2486 Elmt := First (Expressions (Aggr));
2487 while Present (Elmt) loop
2488 Check_Elmt (Elmt);
2489 Next (Elmt);
2490 end loop;
2491 end if;
2493 if Present (Component_Associations (Aggr)) then
2494 Elmt := First (Component_Associations (Aggr));
2495 while Present (Elmt) loop
2497 -- If this is a default-initialized component, then
2498 -- there is nothing to check. The box will be
2499 -- replaced by the appropriate call during late
2500 -- expansion.
2502 if not Box_Present (Elmt) then
2503 Check_Elmt (Expression (Elmt));
2504 end if;
2506 Next (Elmt);
2507 end loop;
2508 end if;
2509 end Check_Aggr;
2511 ----------------
2512 -- Check_Elmt --
2513 ----------------
2515 procedure Check_Elmt (Aelmt : Node_Id) is
2516 begin
2517 -- If we have a nested aggregate, go inside it (to
2518 -- attempt a naked analyze-resolve of the aggregate can
2519 -- cause undesirable cascaded errors). Do not resolve
2520 -- expression if it needs a type from context, as for
2521 -- integer * fixed expression.
2523 if Nkind (Aelmt) = N_Aggregate then
2524 Check_Aggr (Aelmt);
2526 else
2527 Analyze (Aelmt);
2529 if not Is_Overloaded (Aelmt)
2530 and then Etype (Aelmt) /= Any_Fixed
2531 then
2532 Resolve (Aelmt);
2533 end if;
2535 if Etype (Aelmt) = Any_Type then
2536 Found := True;
2537 end if;
2538 end if;
2539 end Check_Elmt;
2541 begin
2542 Check_Aggr (N);
2543 end;
2544 end if;
2546 -- If an error message was issued already, Found got reset to
2547 -- True, so if it is still False, issue standard Wrong_Type msg.
2549 if not Found then
2550 if Is_Overloaded (N)
2551 and then Nkind (N) = N_Function_Call
2552 then
2553 declare
2554 Subp_Name : Node_Id;
2555 begin
2556 if Is_Entity_Name (Name (N)) then
2557 Subp_Name := Name (N);
2559 elsif Nkind (Name (N)) = N_Selected_Component then
2561 -- Protected operation: retrieve operation name
2563 Subp_Name := Selector_Name (Name (N));
2565 else
2566 raise Program_Error;
2567 end if;
2569 Error_Msg_Node_2 := Typ;
2570 Error_Msg_NE ("no visible interpretation of&" &
2571 " matches expected type&", N, Subp_Name);
2572 end;
2574 if All_Errors_Mode then
2575 declare
2576 Index : Interp_Index;
2577 It : Interp;
2579 begin
2580 Error_Msg_N ("\\possible interpretations:", N);
2582 Get_First_Interp (Name (N), Index, It);
2583 while Present (It.Nam) loop
2584 Error_Msg_Sloc := Sloc (It.Nam);
2585 Error_Msg_Node_2 := It.Nam;
2586 Error_Msg_NE
2587 ("\\ type& for & declared#", N, It.Typ);
2588 Get_Next_Interp (Index, It);
2589 end loop;
2590 end;
2592 else
2593 Error_Msg_N ("\use -gnatf for details", N);
2594 end if;
2596 else
2597 Wrong_Type (N, Typ);
2598 end if;
2599 end if;
2600 end if;
2602 Resolution_Failed;
2603 return;
2605 -- Test if we have more than one interpretation for the context
2607 elsif Ambiguous then
2608 Resolution_Failed;
2609 return;
2611 -- Only one intepretation
2613 else
2614 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2615 -- the "+" on T is abstract, and the operands are of universal type,
2616 -- the above code will have (incorrectly) resolved the "+" to the
2617 -- universal one in Standard. Therefore check for this case and give
2618 -- an error. We can't do this earlier, because it would cause legal
2619 -- cases to get errors (when some other type has an abstract "+").
2621 if Ada_Version >= Ada_2005
2622 and then Nkind (N) in N_Op
2623 and then Is_Overloaded (N)
2624 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
2625 then
2626 Get_First_Interp (N, I, It);
2627 while Present (It.Typ) loop
2628 if Present (It.Abstract_Op) and then
2629 Etype (It.Abstract_Op) = Typ
2630 then
2631 Error_Msg_NE
2632 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2633 return;
2634 end if;
2636 Get_Next_Interp (I, It);
2637 end loop;
2638 end if;
2640 -- Here we have an acceptable interpretation for the context
2642 -- Propagate type information and normalize tree for various
2643 -- predefined operations. If the context only imposes a class of
2644 -- types, rather than a specific type, propagate the actual type
2645 -- downward.
2647 if Typ = Any_Integer or else
2648 Typ = Any_Boolean or else
2649 Typ = Any_Modular or else
2650 Typ = Any_Real or else
2651 Typ = Any_Discrete
2652 then
2653 Ctx_Type := Expr_Type;
2655 -- Any_Fixed is legal in a real context only if a specific fixed-
2656 -- point type is imposed. If Norman Cohen can be confused by this,
2657 -- it deserves a separate message.
2659 if Typ = Any_Real
2660 and then Expr_Type = Any_Fixed
2661 then
2662 Error_Msg_N ("illegal context for mixed mode operation", N);
2663 Set_Etype (N, Universal_Real);
2664 Ctx_Type := Universal_Real;
2665 end if;
2666 end if;
2668 -- A user-defined operator is transformed into a function call at
2669 -- this point, so that further processing knows that operators are
2670 -- really operators (i.e. are predefined operators). User-defined
2671 -- operators that are intrinsic are just renamings of the predefined
2672 -- ones, and need not be turned into calls either, but if they rename
2673 -- a different operator, we must transform the node accordingly.
2674 -- Instantiations of Unchecked_Conversion are intrinsic but are
2675 -- treated as functions, even if given an operator designator.
2677 if Nkind (N) in N_Op
2678 and then Present (Entity (N))
2679 and then Ekind (Entity (N)) /= E_Operator
2680 then
2682 if not Is_Predefined_Op (Entity (N)) then
2683 Rewrite_Operator_As_Call (N, Entity (N));
2685 elsif Present (Alias (Entity (N)))
2686 and then
2687 Nkind (Parent (Parent (Entity (N)))) =
2688 N_Subprogram_Renaming_Declaration
2689 then
2690 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2692 -- If the node is rewritten, it will be fully resolved in
2693 -- Rewrite_Renamed_Operator.
2695 if Analyzed (N) then
2696 return;
2697 end if;
2698 end if;
2699 end if;
2701 case N_Subexpr'(Nkind (N)) is
2703 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2705 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2707 when N_Short_Circuit
2708 => Resolve_Short_Circuit (N, Ctx_Type);
2710 when N_Attribute_Reference
2711 => Resolve_Attribute (N, Ctx_Type);
2713 when N_Case_Expression
2714 => Resolve_Case_Expression (N, Ctx_Type);
2716 when N_Character_Literal
2717 => Resolve_Character_Literal (N, Ctx_Type);
2719 when N_Conditional_Expression
2720 => Resolve_Conditional_Expression (N, Ctx_Type);
2722 when N_Expanded_Name
2723 => Resolve_Entity_Name (N, Ctx_Type);
2725 when N_Explicit_Dereference
2726 => Resolve_Explicit_Dereference (N, Ctx_Type);
2728 when N_Expression_With_Actions
2729 => Resolve_Expression_With_Actions (N, Ctx_Type);
2731 when N_Extension_Aggregate
2732 => Resolve_Extension_Aggregate (N, Ctx_Type);
2734 when N_Function_Call
2735 => Resolve_Call (N, Ctx_Type);
2737 when N_Identifier
2738 => Resolve_Entity_Name (N, Ctx_Type);
2740 when N_Indexed_Component
2741 => Resolve_Indexed_Component (N, Ctx_Type);
2743 when N_Integer_Literal
2744 => Resolve_Integer_Literal (N, Ctx_Type);
2746 when N_Membership_Test
2747 => Resolve_Membership_Op (N, Ctx_Type);
2749 when N_Null => Resolve_Null (N, Ctx_Type);
2751 when N_Op_And | N_Op_Or | N_Op_Xor
2752 => Resolve_Logical_Op (N, Ctx_Type);
2754 when N_Op_Eq | N_Op_Ne
2755 => Resolve_Equality_Op (N, Ctx_Type);
2757 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2758 => Resolve_Comparison_Op (N, Ctx_Type);
2760 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2762 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2763 N_Op_Divide | N_Op_Mod | N_Op_Rem
2765 => Resolve_Arithmetic_Op (N, Ctx_Type);
2767 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2769 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2771 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2772 => Resolve_Unary_Op (N, Ctx_Type);
2774 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2776 when N_Procedure_Call_Statement
2777 => Resolve_Call (N, Ctx_Type);
2779 when N_Operator_Symbol
2780 => Resolve_Operator_Symbol (N, Ctx_Type);
2782 when N_Qualified_Expression
2783 => Resolve_Qualified_Expression (N, Ctx_Type);
2785 when N_Quantified_Expression => null;
2787 when N_Raise_xxx_Error
2788 => Set_Etype (N, Ctx_Type);
2790 when N_Range => Resolve_Range (N, Ctx_Type);
2792 when N_Real_Literal
2793 => Resolve_Real_Literal (N, Ctx_Type);
2795 when N_Reference => Resolve_Reference (N, Ctx_Type);
2797 when N_Selected_Component
2798 => Resolve_Selected_Component (N, Ctx_Type);
2800 when N_Slice => Resolve_Slice (N, Ctx_Type);
2802 when N_String_Literal
2803 => Resolve_String_Literal (N, Ctx_Type);
2805 when N_Subprogram_Info
2806 => Resolve_Subprogram_Info (N, Ctx_Type);
2808 when N_Type_Conversion
2809 => Resolve_Type_Conversion (N, Ctx_Type);
2811 when N_Unchecked_Expression =>
2812 Resolve_Unchecked_Expression (N, Ctx_Type);
2814 when N_Unchecked_Type_Conversion =>
2815 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2816 end case;
2818 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2819 -- expression of an anonymous access type that occurs in the context
2820 -- of a named general access type, except when the expression is that
2821 -- of a membership test. This ensures proper legality checking in
2822 -- terms of allowed conversions (expressions that would be illegal to
2823 -- convert implicitly are allowed in membership tests).
2825 if Ada_Version >= Ada_2012
2826 and then Ekind (Ctx_Type) = E_General_Access_Type
2827 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2828 and then Nkind (Parent (N)) not in N_Membership_Test
2829 then
2830 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2831 Analyze_And_Resolve (N, Ctx_Type);
2832 end if;
2834 -- If the subexpression was replaced by a non-subexpression, then
2835 -- all we do is to expand it. The only legitimate case we know of
2836 -- is converting procedure call statement to entry call statements,
2837 -- but there may be others, so we are making this test general.
2839 if Nkind (N) not in N_Subexpr then
2840 Debug_A_Exit ("resolving ", N, " (done)");
2841 Expand (N);
2842 return;
2843 end if;
2845 -- AI05-144-2: Check dangerous order dependence within an expression
2846 -- that is not a subexpression. Exclude RHS of an assignment, because
2847 -- both sides may have side-effects and the check must be performed
2848 -- over the statement.
2850 if Nkind (Parent (N)) not in N_Subexpr
2851 and then Nkind (Parent (N)) /= N_Assignment_Statement
2852 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
2853 then
2854 Check_Order_Dependence;
2855 end if;
2857 -- The expression is definitely NOT overloaded at this point, so
2858 -- we reset the Is_Overloaded flag to avoid any confusion when
2859 -- reanalyzing the node.
2861 Set_Is_Overloaded (N, False);
2863 -- Freeze expression type, entity if it is a name, and designated
2864 -- type if it is an allocator (RM 13.14(10,11,13)).
2866 -- Now that the resolution of the type of the node is complete, and
2867 -- we did not detect an error, we can expand this node. We skip the
2868 -- expand call if we are in a default expression, see section
2869 -- "Handling of Default Expressions" in Sem spec.
2871 Debug_A_Exit ("resolving ", N, " (done)");
2873 -- We unconditionally freeze the expression, even if we are in
2874 -- default expression mode (the Freeze_Expression routine tests this
2875 -- flag and only freezes static types if it is set).
2877 -- Ada 2012 (AI05-177): Expression functions do not freeze. Only
2878 -- their use (in an expanded call) freezes.
2880 if Ekind (Proper_Current_Scope) /= E_Function
2881 or else Nkind (Original_Node (Unit_Declaration_Node
2882 (Proper_Current_Scope))) /= N_Expression_Function
2883 then
2884 Freeze_Expression (N);
2885 end if;
2887 -- Now we can do the expansion
2889 Expand (N);
2890 end if;
2891 end Resolve;
2893 -------------
2894 -- Resolve --
2895 -------------
2897 -- Version with check(s) suppressed
2899 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2900 begin
2901 if Suppress = All_Checks then
2902 declare
2903 Svg : constant Suppress_Record := Scope_Suppress;
2904 begin
2905 Scope_Suppress := Suppress_All;
2906 Resolve (N, Typ);
2907 Scope_Suppress := Svg;
2908 end;
2910 else
2911 declare
2912 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
2913 begin
2914 Scope_Suppress.Suppress (Suppress) := True;
2915 Resolve (N, Typ);
2916 Scope_Suppress.Suppress (Suppress) := Svg;
2917 end;
2918 end if;
2919 end Resolve;
2921 -------------
2922 -- Resolve --
2923 -------------
2925 -- Version with implicit type
2927 procedure Resolve (N : Node_Id) is
2928 begin
2929 Resolve (N, Etype (N));
2930 end Resolve;
2932 ---------------------
2933 -- Resolve_Actuals --
2934 ---------------------
2936 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2937 Loc : constant Source_Ptr := Sloc (N);
2938 A : Node_Id;
2939 F : Entity_Id;
2940 A_Typ : Entity_Id;
2941 F_Typ : Entity_Id;
2942 Prev : Node_Id := Empty;
2943 Orig_A : Node_Id;
2945 procedure Check_Argument_Order;
2946 -- Performs a check for the case where the actuals are all simple
2947 -- identifiers that correspond to the formal names, but in the wrong
2948 -- order, which is considered suspicious and cause for a warning.
2950 procedure Check_Prefixed_Call;
2951 -- If the original node is an overloaded call in prefix notation,
2952 -- insert an 'Access or a dereference as needed over the first actual.
2953 -- Try_Object_Operation has already verified that there is a valid
2954 -- interpretation, but the form of the actual can only be determined
2955 -- once the primitive operation is identified.
2957 procedure Insert_Default;
2958 -- If the actual is missing in a call, insert in the actuals list
2959 -- an instance of the default expression. The insertion is always
2960 -- a named association.
2962 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2963 -- Check whether T1 and T2, or their full views, are derived from a
2964 -- common type. Used to enforce the restrictions on array conversions
2965 -- of AI95-00246.
2967 function Static_Concatenation (N : Node_Id) return Boolean;
2968 -- Predicate to determine whether an actual that is a concatenation
2969 -- will be evaluated statically and does not need a transient scope.
2970 -- This must be determined before the actual is resolved and expanded
2971 -- because if needed the transient scope must be introduced earlier.
2973 --------------------------
2974 -- Check_Argument_Order --
2975 --------------------------
2977 procedure Check_Argument_Order is
2978 begin
2979 -- Nothing to do if no parameters, or original node is neither a
2980 -- function call nor a procedure call statement (happens in the
2981 -- operator-transformed-to-function call case), or the call does
2982 -- not come from source, or this warning is off.
2984 if not Warn_On_Parameter_Order
2985 or else No (Parameter_Associations (N))
2986 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
2987 or else not Comes_From_Source (N)
2988 then
2989 return;
2990 end if;
2992 declare
2993 Nargs : constant Nat := List_Length (Parameter_Associations (N));
2995 begin
2996 -- Nothing to do if only one parameter
2998 if Nargs < 2 then
2999 return;
3000 end if;
3002 -- Here if at least two arguments
3004 declare
3005 Actuals : array (1 .. Nargs) of Node_Id;
3006 Actual : Node_Id;
3007 Formal : Node_Id;
3009 Wrong_Order : Boolean := False;
3010 -- Set True if an out of order case is found
3012 begin
3013 -- Collect identifier names of actuals, fail if any actual is
3014 -- not a simple identifier, and record max length of name.
3016 Actual := First (Parameter_Associations (N));
3017 for J in Actuals'Range loop
3018 if Nkind (Actual) /= N_Identifier then
3019 return;
3020 else
3021 Actuals (J) := Actual;
3022 Next (Actual);
3023 end if;
3024 end loop;
3026 -- If we got this far, all actuals are identifiers and the list
3027 -- of their names is stored in the Actuals array.
3029 Formal := First_Formal (Nam);
3030 for J in Actuals'Range loop
3032 -- If we ran out of formals, that's odd, probably an error
3033 -- which will be detected elsewhere, but abandon the search.
3035 if No (Formal) then
3036 return;
3037 end if;
3039 -- If name matches and is in order OK
3041 if Chars (Formal) = Chars (Actuals (J)) then
3042 null;
3044 else
3045 -- If no match, see if it is elsewhere in list and if so
3046 -- flag potential wrong order if type is compatible.
3048 for K in Actuals'Range loop
3049 if Chars (Formal) = Chars (Actuals (K))
3050 and then
3051 Has_Compatible_Type (Actuals (K), Etype (Formal))
3052 then
3053 Wrong_Order := True;
3054 goto Continue;
3055 end if;
3056 end loop;
3058 -- No match
3060 return;
3061 end if;
3063 <<Continue>> Next_Formal (Formal);
3064 end loop;
3066 -- If Formals left over, also probably an error, skip warning
3068 if Present (Formal) then
3069 return;
3070 end if;
3072 -- Here we give the warning if something was out of order
3074 if Wrong_Order then
3075 Error_Msg_N
3076 ("actuals for this call may be in wrong order?", N);
3077 end if;
3078 end;
3079 end;
3080 end Check_Argument_Order;
3082 -------------------------
3083 -- Check_Prefixed_Call --
3084 -------------------------
3086 procedure Check_Prefixed_Call is
3087 Act : constant Node_Id := First_Actual (N);
3088 A_Type : constant Entity_Id := Etype (Act);
3089 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3090 Orig : constant Node_Id := Original_Node (N);
3091 New_A : Node_Id;
3093 begin
3094 -- Check whether the call is a prefixed call, with or without
3095 -- additional actuals.
3097 if Nkind (Orig) = N_Selected_Component
3098 or else
3099 (Nkind (Orig) = N_Indexed_Component
3100 and then Nkind (Prefix (Orig)) = N_Selected_Component
3101 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3102 and then Is_Entity_Name (Act)
3103 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3104 then
3105 if Is_Access_Type (A_Type)
3106 and then not Is_Access_Type (F_Type)
3107 then
3108 -- Introduce dereference on object in prefix
3110 New_A :=
3111 Make_Explicit_Dereference (Sloc (Act),
3112 Prefix => Relocate_Node (Act));
3113 Rewrite (Act, New_A);
3114 Analyze (Act);
3116 elsif Is_Access_Type (F_Type)
3117 and then not Is_Access_Type (A_Type)
3118 then
3119 -- Introduce an implicit 'Access in prefix
3121 if not Is_Aliased_View (Act) then
3122 Error_Msg_NE
3123 ("object in prefixed call to& must be aliased"
3124 & " (RM-2005 4.3.1 (13))",
3125 Prefix (Act), Nam);
3126 end if;
3128 Rewrite (Act,
3129 Make_Attribute_Reference (Loc,
3130 Attribute_Name => Name_Access,
3131 Prefix => Relocate_Node (Act)));
3132 end if;
3134 Analyze (Act);
3135 end if;
3136 end Check_Prefixed_Call;
3138 --------------------
3139 -- Insert_Default --
3140 --------------------
3142 procedure Insert_Default is
3143 Actval : Node_Id;
3144 Assoc : Node_Id;
3146 begin
3147 -- Missing argument in call, nothing to insert
3149 if No (Default_Value (F)) then
3150 return;
3152 else
3153 -- Note that we do a full New_Copy_Tree, so that any associated
3154 -- Itypes are properly copied. This may not be needed any more,
3155 -- but it does no harm as a safety measure! Defaults of a generic
3156 -- formal may be out of bounds of the corresponding actual (see
3157 -- cc1311b) and an additional check may be required.
3159 Actval :=
3160 New_Copy_Tree
3161 (Default_Value (F),
3162 New_Scope => Current_Scope,
3163 New_Sloc => Loc);
3165 if Is_Concurrent_Type (Scope (Nam))
3166 and then Has_Discriminants (Scope (Nam))
3167 then
3168 Replace_Actual_Discriminants (N, Actval);
3169 end if;
3171 if Is_Overloadable (Nam)
3172 and then Present (Alias (Nam))
3173 then
3174 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3175 and then not Is_Tagged_Type (Etype (F))
3176 then
3177 -- If default is a real literal, do not introduce a
3178 -- conversion whose effect may depend on the run-time
3179 -- size of universal real.
3181 if Nkind (Actval) = N_Real_Literal then
3182 Set_Etype (Actval, Base_Type (Etype (F)));
3183 else
3184 Actval := Unchecked_Convert_To (Etype (F), Actval);
3185 end if;
3186 end if;
3188 if Is_Scalar_Type (Etype (F)) then
3189 Enable_Range_Check (Actval);
3190 end if;
3192 Set_Parent (Actval, N);
3194 -- Resolve aggregates with their base type, to avoid scope
3195 -- anomalies: the subtype was first built in the subprogram
3196 -- declaration, and the current call may be nested.
3198 if Nkind (Actval) = N_Aggregate then
3199 Analyze_And_Resolve (Actval, Etype (F));
3200 else
3201 Analyze_And_Resolve (Actval, Etype (Actval));
3202 end if;
3204 else
3205 Set_Parent (Actval, N);
3207 -- See note above concerning aggregates
3209 if Nkind (Actval) = N_Aggregate
3210 and then Has_Discriminants (Etype (Actval))
3211 then
3212 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3214 -- Resolve entities with their own type, which may differ from
3215 -- the type of a reference in a generic context (the view
3216 -- swapping mechanism did not anticipate the re-analysis of
3217 -- default values in calls).
3219 elsif Is_Entity_Name (Actval) then
3220 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3222 else
3223 Analyze_And_Resolve (Actval, Etype (Actval));
3224 end if;
3225 end if;
3227 -- If default is a tag indeterminate function call, propagate tag
3228 -- to obtain proper dispatching.
3230 if Is_Controlling_Formal (F)
3231 and then Nkind (Default_Value (F)) = N_Function_Call
3232 then
3233 Set_Is_Controlling_Actual (Actval);
3234 end if;
3236 end if;
3238 -- If the default expression raises constraint error, then just
3239 -- silently replace it with an N_Raise_Constraint_Error node, since
3240 -- we already gave the warning on the subprogram spec. If node is
3241 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3242 -- the warnings removal machinery.
3244 if Raises_Constraint_Error (Actval)
3245 and then Nkind (Actval) /= N_Raise_Constraint_Error
3246 then
3247 Rewrite (Actval,
3248 Make_Raise_Constraint_Error (Loc,
3249 Reason => CE_Range_Check_Failed));
3250 Set_Raises_Constraint_Error (Actval);
3251 Set_Etype (Actval, Etype (F));
3252 end if;
3254 Assoc :=
3255 Make_Parameter_Association (Loc,
3256 Explicit_Actual_Parameter => Actval,
3257 Selector_Name => Make_Identifier (Loc, Chars (F)));
3259 -- Case of insertion is first named actual
3261 if No (Prev) or else
3262 Nkind (Parent (Prev)) /= N_Parameter_Association
3263 then
3264 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3265 Set_First_Named_Actual (N, Actval);
3267 if No (Prev) then
3268 if No (Parameter_Associations (N)) then
3269 Set_Parameter_Associations (N, New_List (Assoc));
3270 else
3271 Append (Assoc, Parameter_Associations (N));
3272 end if;
3274 else
3275 Insert_After (Prev, Assoc);
3276 end if;
3278 -- Case of insertion is not first named actual
3280 else
3281 Set_Next_Named_Actual
3282 (Assoc, Next_Named_Actual (Parent (Prev)));
3283 Set_Next_Named_Actual (Parent (Prev), Actval);
3284 Append (Assoc, Parameter_Associations (N));
3285 end if;
3287 Mark_Rewrite_Insertion (Assoc);
3288 Mark_Rewrite_Insertion (Actval);
3290 Prev := Actval;
3291 end Insert_Default;
3293 -------------------
3294 -- Same_Ancestor --
3295 -------------------
3297 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3298 FT1 : Entity_Id := T1;
3299 FT2 : Entity_Id := T2;
3301 begin
3302 if Is_Private_Type (T1)
3303 and then Present (Full_View (T1))
3304 then
3305 FT1 := Full_View (T1);
3306 end if;
3308 if Is_Private_Type (T2)
3309 and then Present (Full_View (T2))
3310 then
3311 FT2 := Full_View (T2);
3312 end if;
3314 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3315 end Same_Ancestor;
3317 --------------------------
3318 -- Static_Concatenation --
3319 --------------------------
3321 function Static_Concatenation (N : Node_Id) return Boolean is
3322 begin
3323 case Nkind (N) is
3324 when N_String_Literal =>
3325 return True;
3327 when N_Op_Concat =>
3329 -- Concatenation is static when both operands are static and
3330 -- the concatenation operator is a predefined one.
3332 return Scope (Entity (N)) = Standard_Standard
3333 and then
3334 Static_Concatenation (Left_Opnd (N))
3335 and then
3336 Static_Concatenation (Right_Opnd (N));
3338 when others =>
3339 if Is_Entity_Name (N) then
3340 declare
3341 Ent : constant Entity_Id := Entity (N);
3342 begin
3343 return Ekind (Ent) = E_Constant
3344 and then Present (Constant_Value (Ent))
3345 and then
3346 Is_Static_Expression (Constant_Value (Ent));
3347 end;
3349 else
3350 return False;
3351 end if;
3352 end case;
3353 end Static_Concatenation;
3355 -- Start of processing for Resolve_Actuals
3357 begin
3358 Check_Argument_Order;
3360 if Present (First_Actual (N)) then
3361 Check_Prefixed_Call;
3362 end if;
3364 A := First_Actual (N);
3365 F := First_Formal (Nam);
3366 while Present (F) loop
3367 if No (A) and then Needs_No_Actuals (Nam) then
3368 null;
3370 -- If we have an error in any actual or formal, indicated by a type
3371 -- of Any_Type, then abandon resolution attempt, and set result type
3372 -- to Any_Type.
3374 elsif (Present (A) and then Etype (A) = Any_Type)
3375 or else Etype (F) = Any_Type
3376 then
3377 Set_Etype (N, Any_Type);
3378 return;
3379 end if;
3381 -- Case where actual is present
3383 -- If the actual is an entity, generate a reference to it now. We
3384 -- do this before the actual is resolved, because a formal of some
3385 -- protected subprogram, or a task discriminant, will be rewritten
3386 -- during expansion, and the source entity reference may be lost.
3388 if Present (A)
3389 and then Is_Entity_Name (A)
3390 and then Comes_From_Source (N)
3391 then
3392 Orig_A := Entity (A);
3394 if Present (Orig_A) then
3395 if Is_Formal (Orig_A)
3396 and then Ekind (F) /= E_In_Parameter
3397 then
3398 Generate_Reference (Orig_A, A, 'm');
3400 elsif not Is_Overloaded (A) then
3401 Generate_Reference (Orig_A, A);
3402 end if;
3403 end if;
3404 end if;
3406 if Present (A)
3407 and then (Nkind (Parent (A)) /= N_Parameter_Association
3408 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3409 then
3410 -- If style checking mode on, check match of formal name
3412 if Style_Check then
3413 if Nkind (Parent (A)) = N_Parameter_Association then
3414 Check_Identifier (Selector_Name (Parent (A)), F);
3415 end if;
3416 end if;
3418 -- If the formal is Out or In_Out, do not resolve and expand the
3419 -- conversion, because it is subsequently expanded into explicit
3420 -- temporaries and assignments. However, the object of the
3421 -- conversion can be resolved. An exception is the case of tagged
3422 -- type conversion with a class-wide actual. In that case we want
3423 -- the tag check to occur and no temporary will be needed (no
3424 -- representation change can occur) and the parameter is passed by
3425 -- reference, so we go ahead and resolve the type conversion.
3426 -- Another exception is the case of reference to component or
3427 -- subcomponent of a bit-packed array, in which case we want to
3428 -- defer expansion to the point the in and out assignments are
3429 -- performed.
3431 if Ekind (F) /= E_In_Parameter
3432 and then Nkind (A) = N_Type_Conversion
3433 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3434 then
3435 if Ekind (F) = E_In_Out_Parameter
3436 and then Is_Array_Type (Etype (F))
3437 then
3438 -- In a view conversion, the conversion must be legal in
3439 -- both directions, and thus both component types must be
3440 -- aliased, or neither (4.6 (8)).
3442 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3443 -- the privacy requirement should not apply to generic
3444 -- types, and should be checked in an instance. ARG query
3445 -- is in order ???
3447 if Has_Aliased_Components (Etype (Expression (A))) /=
3448 Has_Aliased_Components (Etype (F))
3449 then
3450 Error_Msg_N
3451 ("both component types in a view conversion must be"
3452 & " aliased, or neither", A);
3454 -- Comment here??? what set of cases???
3456 elsif
3457 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3458 then
3459 -- Check view conv between unrelated by ref array types
3461 if Is_By_Reference_Type (Etype (F))
3462 or else Is_By_Reference_Type (Etype (Expression (A)))
3463 then
3464 Error_Msg_N
3465 ("view conversion between unrelated by reference " &
3466 "array types not allowed (\'A'I-00246)", A);
3468 -- In Ada 2005 mode, check view conversion component
3469 -- type cannot be private, tagged, or volatile. Note
3470 -- that we only apply this to source conversions. The
3471 -- generated code can contain conversions which are
3472 -- not subject to this test, and we cannot extract the
3473 -- component type in such cases since it is not present.
3475 elsif Comes_From_Source (A)
3476 and then Ada_Version >= Ada_2005
3477 then
3478 declare
3479 Comp_Type : constant Entity_Id :=
3480 Component_Type
3481 (Etype (Expression (A)));
3482 begin
3483 if (Is_Private_Type (Comp_Type)
3484 and then not Is_Generic_Type (Comp_Type))
3485 or else Is_Tagged_Type (Comp_Type)
3486 or else Is_Volatile (Comp_Type)
3487 then
3488 Error_Msg_N
3489 ("component type of a view conversion cannot"
3490 & " be private, tagged, or volatile"
3491 & " (RM 4.6 (24))",
3492 Expression (A));
3493 end if;
3494 end;
3495 end if;
3496 end if;
3497 end if;
3499 -- Resolve expression if conversion is all OK
3501 if (Conversion_OK (A)
3502 or else Valid_Conversion (A, Etype (A), Expression (A)))
3503 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3504 then
3505 Resolve (Expression (A));
3506 end if;
3508 -- If the actual is a function call that returns a limited
3509 -- unconstrained object that needs finalization, create a
3510 -- transient scope for it, so that it can receive the proper
3511 -- finalization list.
3513 elsif Nkind (A) = N_Function_Call
3514 and then Is_Limited_Record (Etype (F))
3515 and then not Is_Constrained (Etype (F))
3516 and then Full_Expander_Active
3517 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3518 then
3519 Establish_Transient_Scope (A, False);
3520 Resolve (A, Etype (F));
3522 -- A small optimization: if one of the actuals is a concatenation
3523 -- create a block around a procedure call to recover stack space.
3524 -- This alleviates stack usage when several procedure calls in
3525 -- the same statement list use concatenation. We do not perform
3526 -- this wrapping for code statements, where the argument is a
3527 -- static string, and we want to preserve warnings involving
3528 -- sequences of such statements.
3530 elsif Nkind (A) = N_Op_Concat
3531 and then Nkind (N) = N_Procedure_Call_Statement
3532 and then Full_Expander_Active
3533 and then
3534 not (Is_Intrinsic_Subprogram (Nam)
3535 and then Chars (Nam) = Name_Asm)
3536 and then not Static_Concatenation (A)
3537 then
3538 Establish_Transient_Scope (A, False);
3539 Resolve (A, Etype (F));
3541 else
3542 if Nkind (A) = N_Type_Conversion
3543 and then Is_Array_Type (Etype (F))
3544 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3545 and then
3546 (Is_Limited_Type (Etype (F))
3547 or else Is_Limited_Type (Etype (Expression (A))))
3548 then
3549 Error_Msg_N
3550 ("conversion between unrelated limited array types " &
3551 "not allowed (\A\I-00246)", A);
3553 if Is_Limited_Type (Etype (F)) then
3554 Explain_Limited_Type (Etype (F), A);
3555 end if;
3557 if Is_Limited_Type (Etype (Expression (A))) then
3558 Explain_Limited_Type (Etype (Expression (A)), A);
3559 end if;
3560 end if;
3562 -- (Ada 2005: AI-251): If the actual is an allocator whose
3563 -- directly designated type is a class-wide interface, we build
3564 -- an anonymous access type to use it as the type of the
3565 -- allocator. Later, when the subprogram call is expanded, if
3566 -- the interface has a secondary dispatch table the expander
3567 -- will add a type conversion to force the correct displacement
3568 -- of the pointer.
3570 if Nkind (A) = N_Allocator then
3571 declare
3572 DDT : constant Entity_Id :=
3573 Directly_Designated_Type (Base_Type (Etype (F)));
3575 New_Itype : Entity_Id;
3577 begin
3578 if Is_Class_Wide_Type (DDT)
3579 and then Is_Interface (DDT)
3580 then
3581 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3582 Set_Etype (New_Itype, Etype (A));
3583 Set_Directly_Designated_Type (New_Itype,
3584 Directly_Designated_Type (Etype (A)));
3585 Set_Etype (A, New_Itype);
3586 end if;
3588 -- Ada 2005, AI-162:If the actual is an allocator, the
3589 -- innermost enclosing statement is the master of the
3590 -- created object. This needs to be done with expansion
3591 -- enabled only, otherwise the transient scope will not
3592 -- be removed in the expansion of the wrapped construct.
3594 if (Is_Controlled (DDT) or else Has_Task (DDT))
3595 and then Full_Expander_Active
3596 then
3597 Establish_Transient_Scope (A, False);
3598 end if;
3599 end;
3600 end if;
3602 -- (Ada 2005): The call may be to a primitive operation of
3603 -- a tagged synchronized type, declared outside of the type.
3604 -- In this case the controlling actual must be converted to
3605 -- its corresponding record type, which is the formal type.
3606 -- The actual may be a subtype, either because of a constraint
3607 -- or because it is a generic actual, so use base type to
3608 -- locate concurrent type.
3610 F_Typ := Base_Type (Etype (F));
3612 if Is_Tagged_Type (F_Typ)
3613 and then (Is_Concurrent_Type (F_Typ)
3614 or else Is_Concurrent_Record_Type (F_Typ))
3615 then
3616 -- If the actual is overloaded, look for an interpretation
3617 -- that has a synchronized type.
3619 if not Is_Overloaded (A) then
3620 A_Typ := Base_Type (Etype (A));
3622 else
3623 declare
3624 Index : Interp_Index;
3625 It : Interp;
3627 begin
3628 Get_First_Interp (A, Index, It);
3629 while Present (It.Typ) loop
3630 if Is_Concurrent_Type (It.Typ)
3631 or else Is_Concurrent_Record_Type (It.Typ)
3632 then
3633 A_Typ := Base_Type (It.Typ);
3634 exit;
3635 end if;
3637 Get_Next_Interp (Index, It);
3638 end loop;
3639 end;
3640 end if;
3642 declare
3643 Full_A_Typ : Entity_Id;
3645 begin
3646 if Present (Full_View (A_Typ)) then
3647 Full_A_Typ := Base_Type (Full_View (A_Typ));
3648 else
3649 Full_A_Typ := A_Typ;
3650 end if;
3652 -- Tagged synchronized type (case 1): the actual is a
3653 -- concurrent type.
3655 if Is_Concurrent_Type (A_Typ)
3656 and then Corresponding_Record_Type (A_Typ) = F_Typ
3657 then
3658 Rewrite (A,
3659 Unchecked_Convert_To
3660 (Corresponding_Record_Type (A_Typ), A));
3661 Resolve (A, Etype (F));
3663 -- Tagged synchronized type (case 2): the formal is a
3664 -- concurrent type.
3666 elsif Ekind (Full_A_Typ) = E_Record_Type
3667 and then Present
3668 (Corresponding_Concurrent_Type (Full_A_Typ))
3669 and then Is_Concurrent_Type (F_Typ)
3670 and then Present (Corresponding_Record_Type (F_Typ))
3671 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3672 then
3673 Resolve (A, Corresponding_Record_Type (F_Typ));
3675 -- Common case
3677 else
3678 Resolve (A, Etype (F));
3679 end if;
3680 end;
3681 else
3683 -- not a synchronized operation.
3685 Resolve (A, Etype (F));
3686 end if;
3687 end if;
3689 A_Typ := Etype (A);
3690 F_Typ := Etype (F);
3692 if Comes_From_Source (Original_Node (N))
3693 and then Nkind_In (Original_Node (N), N_Function_Call,
3694 N_Procedure_Call_Statement)
3695 then
3696 -- In formal mode, check that actual parameters matching
3697 -- formals of tagged types are objects (or ancestor type
3698 -- conversions of objects), not general expressions.
3700 if Is_Actual_Tagged_Parameter (A) then
3701 if Is_SPARK_Object_Reference (A) then
3702 null;
3704 elsif Nkind (A) = N_Type_Conversion then
3705 declare
3706 Operand : constant Node_Id := Expression (A);
3707 Operand_Typ : constant Entity_Id := Etype (Operand);
3708 Target_Typ : constant Entity_Id := A_Typ;
3710 begin
3711 if not Is_SPARK_Object_Reference (Operand) then
3712 Check_SPARK_Restriction
3713 ("object required", Operand);
3715 -- In formal mode, the only view conversions are those
3716 -- involving ancestor conversion of an extended type.
3718 elsif not
3719 (Is_Tagged_Type (Target_Typ)
3720 and then not Is_Class_Wide_Type (Target_Typ)
3721 and then Is_Tagged_Type (Operand_Typ)
3722 and then not Is_Class_Wide_Type (Operand_Typ)
3723 and then Is_Ancestor (Target_Typ, Operand_Typ))
3724 then
3725 if Ekind_In
3726 (F, E_Out_Parameter, E_In_Out_Parameter)
3727 then
3728 Check_SPARK_Restriction
3729 ("ancestor conversion is the only permitted "
3730 & "view conversion", A);
3731 else
3732 Check_SPARK_Restriction
3733 ("ancestor conversion required", A);
3734 end if;
3736 else
3737 null;
3738 end if;
3739 end;
3741 else
3742 Check_SPARK_Restriction ("object required", A);
3743 end if;
3745 -- In formal mode, the only view conversions are those
3746 -- involving ancestor conversion of an extended type.
3748 elsif Nkind (A) = N_Type_Conversion
3749 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3750 then
3751 Check_SPARK_Restriction
3752 ("ancestor conversion is the only permitted view "
3753 & "conversion", A);
3754 end if;
3755 end if;
3757 -- Save actual for subsequent check on order dependence, and
3758 -- indicate whether actual is modifiable. For AI05-0144-2.
3760 -- If this is a call to a reference function that is the result
3761 -- of expansion, as in element iterator loops, this does not lead
3762 -- to a dangerous order dependence: only subsequent use of the
3763 -- denoted element might, in some enclosing call.
3765 if not Has_Implicit_Dereference (Etype (Nam))
3766 or else Comes_From_Source (N)
3767 then
3768 Save_Actual (A, Ekind (F) /= E_In_Parameter);
3769 end if;
3771 -- For mode IN, if actual is an entity, and the type of the formal
3772 -- has warnings suppressed, then we reset Never_Set_In_Source for
3773 -- the calling entity. The reason for this is to catch cases like
3774 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3775 -- uses trickery to modify an IN parameter.
3777 if Ekind (F) = E_In_Parameter
3778 and then Is_Entity_Name (A)
3779 and then Present (Entity (A))
3780 and then Ekind (Entity (A)) = E_Variable
3781 and then Has_Warnings_Off (F_Typ)
3782 then
3783 Set_Never_Set_In_Source (Entity (A), False);
3784 end if;
3786 -- Perform error checks for IN and IN OUT parameters
3788 if Ekind (F) /= E_Out_Parameter then
3790 -- Check unset reference. For scalar parameters, it is clearly
3791 -- wrong to pass an uninitialized value as either an IN or
3792 -- IN-OUT parameter. For composites, it is also clearly an
3793 -- error to pass a completely uninitialized value as an IN
3794 -- parameter, but the case of IN OUT is trickier. We prefer
3795 -- not to give a warning here. For example, suppose there is
3796 -- a routine that sets some component of a record to False.
3797 -- It is perfectly reasonable to make this IN-OUT and allow
3798 -- either initialized or uninitialized records to be passed
3799 -- in this case.
3801 -- For partially initialized composite values, we also avoid
3802 -- warnings, since it is quite likely that we are passing a
3803 -- partially initialized value and only the initialized fields
3804 -- will in fact be read in the subprogram.
3806 if Is_Scalar_Type (A_Typ)
3807 or else (Ekind (F) = E_In_Parameter
3808 and then not Is_Partially_Initialized_Type (A_Typ))
3809 then
3810 Check_Unset_Reference (A);
3811 end if;
3813 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3814 -- actual to a nested call, since this is case of reading an
3815 -- out parameter, which is not allowed.
3817 if Ada_Version = Ada_83
3818 and then Is_Entity_Name (A)
3819 and then Ekind (Entity (A)) = E_Out_Parameter
3820 then
3821 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3822 end if;
3823 end if;
3825 -- Case of OUT or IN OUT parameter
3827 if Ekind (F) /= E_In_Parameter then
3829 -- For an Out parameter, check for useless assignment. Note
3830 -- that we can't set Last_Assignment this early, because we may
3831 -- kill current values in Resolve_Call, and that call would
3832 -- clobber the Last_Assignment field.
3834 -- Note: call Warn_On_Useless_Assignment before doing the check
3835 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3836 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3837 -- reflects the last assignment, not this one!
3839 if Ekind (F) = E_Out_Parameter then
3840 if Warn_On_Modified_As_Out_Parameter (F)
3841 and then Is_Entity_Name (A)
3842 and then Present (Entity (A))
3843 and then Comes_From_Source (N)
3844 then
3845 Warn_On_Useless_Assignment (Entity (A), A);
3846 end if;
3847 end if;
3849 -- Validate the form of the actual. Note that the call to
3850 -- Is_OK_Variable_For_Out_Formal generates the required
3851 -- reference in this case.
3853 -- A call to an initialization procedure for an aggregate
3854 -- component may initialize a nested component of a constant
3855 -- designated object. In this context the object is variable.
3857 if not Is_OK_Variable_For_Out_Formal (A)
3858 and then not Is_Init_Proc (Nam)
3859 then
3860 Error_Msg_NE ("actual for& must be a variable", A, F);
3861 end if;
3863 -- What's the following about???
3865 if Is_Entity_Name (A) then
3866 Kill_Checks (Entity (A));
3867 else
3868 Kill_All_Checks;
3869 end if;
3870 end if;
3872 if Etype (A) = Any_Type then
3873 Set_Etype (N, Any_Type);
3874 return;
3875 end if;
3877 -- Apply appropriate range checks for in, out, and in-out
3878 -- parameters. Out and in-out parameters also need a separate
3879 -- check, if there is a type conversion, to make sure the return
3880 -- value meets the constraints of the variable before the
3881 -- conversion.
3883 -- Gigi looks at the check flag and uses the appropriate types.
3884 -- For now since one flag is used there is an optimization which
3885 -- might not be done in the In Out case since Gigi does not do
3886 -- any analysis. More thought required about this ???
3888 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3890 -- Apply predicate checks, unless this is a call to the
3891 -- predicate check function itself, which would cause an
3892 -- infinite recursion.
3894 if not (Ekind (Nam) = E_Function
3895 and then Has_Predicates (Nam))
3896 then
3897 Apply_Predicate_Check (A, F_Typ);
3898 end if;
3900 -- Apply required constraint checks
3902 if Is_Scalar_Type (Etype (A)) then
3903 Apply_Scalar_Range_Check (A, F_Typ);
3905 elsif Is_Array_Type (Etype (A)) then
3906 Apply_Length_Check (A, F_Typ);
3908 elsif Is_Record_Type (F_Typ)
3909 and then Has_Discriminants (F_Typ)
3910 and then Is_Constrained (F_Typ)
3911 and then (not Is_Derived_Type (F_Typ)
3912 or else Comes_From_Source (Nam))
3913 then
3914 Apply_Discriminant_Check (A, F_Typ);
3916 elsif Is_Access_Type (F_Typ)
3917 and then Is_Array_Type (Designated_Type (F_Typ))
3918 and then Is_Constrained (Designated_Type (F_Typ))
3919 then
3920 Apply_Length_Check (A, F_Typ);
3922 elsif Is_Access_Type (F_Typ)
3923 and then Has_Discriminants (Designated_Type (F_Typ))
3924 and then Is_Constrained (Designated_Type (F_Typ))
3925 then
3926 Apply_Discriminant_Check (A, F_Typ);
3928 else
3929 Apply_Range_Check (A, F_Typ);
3930 end if;
3932 -- Ada 2005 (AI-231): Note that the controlling parameter case
3933 -- already existed in Ada 95, which is partially checked
3934 -- elsewhere (see Checks), and we don't want the warning
3935 -- message to differ.
3937 if Is_Access_Type (F_Typ)
3938 and then Can_Never_Be_Null (F_Typ)
3939 and then Known_Null (A)
3940 then
3941 if Is_Controlling_Formal (F) then
3942 Apply_Compile_Time_Constraint_Error
3943 (N => A,
3944 Msg => "null value not allowed here?",
3945 Reason => CE_Access_Check_Failed);
3947 elsif Ada_Version >= Ada_2005 then
3948 Apply_Compile_Time_Constraint_Error
3949 (N => A,
3950 Msg => "(Ada 2005) null not allowed in "
3951 & "null-excluding formal?",
3952 Reason => CE_Null_Not_Allowed);
3953 end if;
3954 end if;
3955 end if;
3957 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
3958 if Nkind (A) = N_Type_Conversion then
3959 if Is_Scalar_Type (A_Typ) then
3960 Apply_Scalar_Range_Check
3961 (Expression (A), Etype (Expression (A)), A_Typ);
3962 else
3963 Apply_Range_Check
3964 (Expression (A), Etype (Expression (A)), A_Typ);
3965 end if;
3967 else
3968 if Is_Scalar_Type (F_Typ) then
3969 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3970 elsif Is_Array_Type (F_Typ)
3971 and then Ekind (F) = E_Out_Parameter
3972 then
3973 Apply_Length_Check (A, F_Typ);
3974 else
3975 Apply_Range_Check (A, A_Typ, F_Typ);
3976 end if;
3977 end if;
3978 end if;
3980 -- An actual associated with an access parameter is implicitly
3981 -- converted to the anonymous access type of the formal and must
3982 -- satisfy the legality checks for access conversions.
3984 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3985 if not Valid_Conversion (A, F_Typ, A) then
3986 Error_Msg_N
3987 ("invalid implicit conversion for access parameter", A);
3988 end if;
3990 -- If the actual is an access selected component of a variable,
3991 -- the call may modify its designated object. It is reasonable
3992 -- to treat this as a potential modification of the enclosing
3993 -- record, to prevent spurious warnings that it should be
3994 -- declared as a constant, because intuitively programmers
3995 -- regard the designated subcomponent as part of the record.
3997 if Nkind (A) = N_Selected_Component
3998 and then Is_Entity_Name (Prefix (A))
3999 and then not Is_Constant_Object (Entity (Prefix (A)))
4000 then
4001 Note_Possible_Modification (A, Sure => False);
4002 end if;
4003 end if;
4005 -- Check bad case of atomic/volatile argument (RM C.6(12))
4007 if Is_By_Reference_Type (Etype (F))
4008 and then Comes_From_Source (N)
4009 then
4010 if Is_Atomic_Object (A)
4011 and then not Is_Atomic (Etype (F))
4012 then
4013 Error_Msg_NE
4014 ("cannot pass atomic argument to non-atomic formal&",
4015 A, F);
4017 elsif Is_Volatile_Object (A)
4018 and then not Is_Volatile (Etype (F))
4019 then
4020 Error_Msg_NE
4021 ("cannot pass volatile argument to non-volatile formal&",
4022 A, F);
4023 end if;
4024 end if;
4026 -- Check that subprograms don't have improper controlling
4027 -- arguments (RM 3.9.2 (9)).
4029 -- A primitive operation may have an access parameter of an
4030 -- incomplete tagged type, but a dispatching call is illegal
4031 -- if the type is still incomplete.
4033 if Is_Controlling_Formal (F) then
4034 Set_Is_Controlling_Actual (A);
4036 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4037 declare
4038 Desig : constant Entity_Id := Designated_Type (Etype (F));
4039 begin
4040 if Ekind (Desig) = E_Incomplete_Type
4041 and then No (Full_View (Desig))
4042 and then No (Non_Limited_View (Desig))
4043 then
4044 Error_Msg_NE
4045 ("premature use of incomplete type& " &
4046 "in dispatching call", A, Desig);
4047 end if;
4048 end;
4049 end if;
4051 elsif Nkind (A) = N_Explicit_Dereference then
4052 Validate_Remote_Access_To_Class_Wide_Type (A);
4053 end if;
4055 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4056 and then not Is_Class_Wide_Type (F_Typ)
4057 and then not Is_Controlling_Formal (F)
4058 then
4059 Error_Msg_N ("class-wide argument not allowed here!", A);
4061 if Is_Subprogram (Nam)
4062 and then Comes_From_Source (Nam)
4063 then
4064 Error_Msg_Node_2 := F_Typ;
4065 Error_Msg_NE
4066 ("& is not a dispatching operation of &!", A, Nam);
4067 end if;
4069 -- Apply the checks described in 3.10.2(27): if the context is a
4070 -- specific access-to-object, the actual cannot be class-wide.
4071 -- Use base type to exclude access_to_subprogram cases.
4073 elsif Is_Access_Type (A_Typ)
4074 and then Is_Access_Type (F_Typ)
4075 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4076 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4077 or else (Nkind (A) = N_Attribute_Reference
4078 and then
4079 Is_Class_Wide_Type (Etype (Prefix (A)))))
4080 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4081 and then not Is_Controlling_Formal (F)
4083 -- Disable these checks for call to imported C++ subprograms
4085 and then not
4086 (Is_Entity_Name (Name (N))
4087 and then Is_Imported (Entity (Name (N)))
4088 and then Convention (Entity (Name (N))) = Convention_CPP)
4089 then
4090 Error_Msg_N
4091 ("access to class-wide argument not allowed here!", A);
4093 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4094 Error_Msg_Node_2 := Designated_Type (F_Typ);
4095 Error_Msg_NE
4096 ("& is not a dispatching operation of &!", A, Nam);
4097 end if;
4098 end if;
4100 Eval_Actual (A);
4102 -- If it is a named association, treat the selector_name as a
4103 -- proper identifier, and mark the corresponding entity. Ignore
4104 -- this reference in Alfa mode, as it refers to an entity not in
4105 -- scope at the point of reference, so the reference should be
4106 -- ignored for computing effects of subprograms.
4108 if Nkind (Parent (A)) = N_Parameter_Association
4109 and then not Alfa_Mode
4110 then
4111 Set_Entity (Selector_Name (Parent (A)), F);
4112 Generate_Reference (F, Selector_Name (Parent (A)));
4113 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4114 Generate_Reference (F_Typ, N, ' ');
4115 end if;
4117 Prev := A;
4119 if Ekind (F) /= E_Out_Parameter then
4120 Check_Unset_Reference (A);
4121 end if;
4123 Next_Actual (A);
4125 -- Case where actual is not present
4127 else
4128 Insert_Default;
4129 end if;
4131 Next_Formal (F);
4132 end loop;
4133 end Resolve_Actuals;
4135 -----------------------
4136 -- Resolve_Allocator --
4137 -----------------------
4139 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4140 Desig_T : constant Entity_Id := Designated_Type (Typ);
4141 E : constant Node_Id := Expression (N);
4142 Subtyp : Entity_Id;
4143 Discrim : Entity_Id;
4144 Constr : Node_Id;
4145 Aggr : Node_Id;
4146 Assoc : Node_Id := Empty;
4147 Disc_Exp : Node_Id;
4149 procedure Check_Allocator_Discrim_Accessibility
4150 (Disc_Exp : Node_Id;
4151 Alloc_Typ : Entity_Id);
4152 -- Check that accessibility level associated with an access discriminant
4153 -- initialized in an allocator by the expression Disc_Exp is not deeper
4154 -- than the level of the allocator type Alloc_Typ. An error message is
4155 -- issued if this condition is violated. Specialized checks are done for
4156 -- the cases of a constraint expression which is an access attribute or
4157 -- an access discriminant.
4159 function In_Dispatching_Context return Boolean;
4160 -- If the allocator is an actual in a call, it is allowed to be class-
4161 -- wide when the context is not because it is a controlling actual.
4163 -------------------------------------------
4164 -- Check_Allocator_Discrim_Accessibility --
4165 -------------------------------------------
4167 procedure Check_Allocator_Discrim_Accessibility
4168 (Disc_Exp : Node_Id;
4169 Alloc_Typ : Entity_Id)
4171 begin
4172 if Type_Access_Level (Etype (Disc_Exp)) >
4173 Deepest_Type_Access_Level (Alloc_Typ)
4174 then
4175 Error_Msg_N
4176 ("operand type has deeper level than allocator type", Disc_Exp);
4178 -- When the expression is an Access attribute the level of the prefix
4179 -- object must not be deeper than that of the allocator's type.
4181 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4182 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4183 Attribute_Access
4184 and then Object_Access_Level (Prefix (Disc_Exp)) >
4185 Deepest_Type_Access_Level (Alloc_Typ)
4186 then
4187 Error_Msg_N
4188 ("prefix of attribute has deeper level than allocator type",
4189 Disc_Exp);
4191 -- When the expression is an access discriminant the check is against
4192 -- the level of the prefix object.
4194 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4195 and then Nkind (Disc_Exp) = N_Selected_Component
4196 and then Object_Access_Level (Prefix (Disc_Exp)) >
4197 Deepest_Type_Access_Level (Alloc_Typ)
4198 then
4199 Error_Msg_N
4200 ("access discriminant has deeper level than allocator type",
4201 Disc_Exp);
4203 -- All other cases are legal
4205 else
4206 null;
4207 end if;
4208 end Check_Allocator_Discrim_Accessibility;
4210 ----------------------------
4211 -- In_Dispatching_Context --
4212 ----------------------------
4214 function In_Dispatching_Context return Boolean is
4215 Par : constant Node_Id := Parent (N);
4217 begin
4218 return Nkind (Par) in N_Subprogram_Call
4219 and then Is_Entity_Name (Name (Par))
4220 and then Is_Dispatching_Operation (Entity (Name (Par)));
4221 end In_Dispatching_Context;
4223 -- Start of processing for Resolve_Allocator
4225 begin
4226 -- Replace general access with specific type
4228 if Ekind (Etype (N)) = E_Allocator_Type then
4229 Set_Etype (N, Base_Type (Typ));
4230 end if;
4232 if Is_Abstract_Type (Typ) then
4233 Error_Msg_N ("type of allocator cannot be abstract", N);
4234 end if;
4236 -- For qualified expression, resolve the expression using the
4237 -- given subtype (nothing to do for type mark, subtype indication)
4239 if Nkind (E) = N_Qualified_Expression then
4240 if Is_Class_Wide_Type (Etype (E))
4241 and then not Is_Class_Wide_Type (Desig_T)
4242 and then not In_Dispatching_Context
4243 then
4244 Error_Msg_N
4245 ("class-wide allocator not allowed for this access type", N);
4246 end if;
4248 Resolve (Expression (E), Etype (E));
4249 Check_Unset_Reference (Expression (E));
4251 -- A qualified expression requires an exact match of the type,
4252 -- class-wide matching is not allowed.
4254 if (Is_Class_Wide_Type (Etype (Expression (E)))
4255 or else Is_Class_Wide_Type (Etype (E)))
4256 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4257 then
4258 Wrong_Type (Expression (E), Etype (E));
4259 end if;
4261 -- Calls to build-in-place functions are not currently supported in
4262 -- allocators for access types associated with a simple storage pool.
4263 -- Supporting such allocators may require passing additional implicit
4264 -- parameters to build-in-place functions (or a significant revision
4265 -- of the current b-i-p implementation to unify the handling for
4266 -- multiple kinds of storage pools). ???
4268 if Is_Immutably_Limited_Type (Desig_T)
4269 and then Nkind (Expression (E)) = N_Function_Call
4270 then
4271 declare
4272 Pool : constant Entity_Id :=
4273 Associated_Storage_Pool (Root_Type (Typ));
4274 begin
4275 if Present (Pool)
4276 and then
4277 Present (Get_Rep_Pragma
4278 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4279 then
4280 Error_Msg_N
4281 ("limited function calls not yet supported in simple " &
4282 "storage pool allocators", Expression (E));
4283 end if;
4284 end;
4285 end if;
4287 -- A special accessibility check is needed for allocators that
4288 -- constrain access discriminants. The level of the type of the
4289 -- expression used to constrain an access discriminant cannot be
4290 -- deeper than the type of the allocator (in contrast to access
4291 -- parameters, where the level of the actual can be arbitrary).
4293 -- We can't use Valid_Conversion to perform this check because
4294 -- in general the type of the allocator is unrelated to the type
4295 -- of the access discriminant.
4297 if Ekind (Typ) /= E_Anonymous_Access_Type
4298 or else Is_Local_Anonymous_Access (Typ)
4299 then
4300 Subtyp := Entity (Subtype_Mark (E));
4302 Aggr := Original_Node (Expression (E));
4304 if Has_Discriminants (Subtyp)
4305 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4306 then
4307 Discrim := First_Discriminant (Base_Type (Subtyp));
4309 -- Get the first component expression of the aggregate
4311 if Present (Expressions (Aggr)) then
4312 Disc_Exp := First (Expressions (Aggr));
4314 elsif Present (Component_Associations (Aggr)) then
4315 Assoc := First (Component_Associations (Aggr));
4317 if Present (Assoc) then
4318 Disc_Exp := Expression (Assoc);
4319 else
4320 Disc_Exp := Empty;
4321 end if;
4323 else
4324 Disc_Exp := Empty;
4325 end if;
4327 while Present (Discrim) and then Present (Disc_Exp) loop
4328 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4329 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4330 end if;
4332 Next_Discriminant (Discrim);
4334 if Present (Discrim) then
4335 if Present (Assoc) then
4336 Next (Assoc);
4337 Disc_Exp := Expression (Assoc);
4339 elsif Present (Next (Disc_Exp)) then
4340 Next (Disc_Exp);
4342 else
4343 Assoc := First (Component_Associations (Aggr));
4345 if Present (Assoc) then
4346 Disc_Exp := Expression (Assoc);
4347 else
4348 Disc_Exp := Empty;
4349 end if;
4350 end if;
4351 end if;
4352 end loop;
4353 end if;
4354 end if;
4356 -- For a subtype mark or subtype indication, freeze the subtype
4358 else
4359 Freeze_Expression (E);
4361 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4362 Error_Msg_N
4363 ("initialization required for access-to-constant allocator", N);
4364 end if;
4366 -- A special accessibility check is needed for allocators that
4367 -- constrain access discriminants. The level of the type of the
4368 -- expression used to constrain an access discriminant cannot be
4369 -- deeper than the type of the allocator (in contrast to access
4370 -- parameters, where the level of the actual can be arbitrary).
4371 -- We can't use Valid_Conversion to perform this check because
4372 -- in general the type of the allocator is unrelated to the type
4373 -- of the access discriminant.
4375 if Nkind (Original_Node (E)) = N_Subtype_Indication
4376 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4377 or else Is_Local_Anonymous_Access (Typ))
4378 then
4379 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4381 if Has_Discriminants (Subtyp) then
4382 Discrim := First_Discriminant (Base_Type (Subtyp));
4383 Constr := First (Constraints (Constraint (Original_Node (E))));
4384 while Present (Discrim) and then Present (Constr) loop
4385 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4386 if Nkind (Constr) = N_Discriminant_Association then
4387 Disc_Exp := Original_Node (Expression (Constr));
4388 else
4389 Disc_Exp := Original_Node (Constr);
4390 end if;
4392 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4393 end if;
4395 Next_Discriminant (Discrim);
4396 Next (Constr);
4397 end loop;
4398 end if;
4399 end if;
4400 end if;
4402 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4403 -- check that the level of the type of the created object is not deeper
4404 -- than the level of the allocator's access type, since extensions can
4405 -- now occur at deeper levels than their ancestor types. This is a
4406 -- static accessibility level check; a run-time check is also needed in
4407 -- the case of an initialized allocator with a class-wide argument (see
4408 -- Expand_Allocator_Expression).
4410 if Ada_Version >= Ada_2005
4411 and then Is_Class_Wide_Type (Desig_T)
4412 then
4413 declare
4414 Exp_Typ : Entity_Id;
4416 begin
4417 if Nkind (E) = N_Qualified_Expression then
4418 Exp_Typ := Etype (E);
4419 elsif Nkind (E) = N_Subtype_Indication then
4420 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4421 else
4422 Exp_Typ := Entity (E);
4423 end if;
4425 if Type_Access_Level (Exp_Typ) >
4426 Deepest_Type_Access_Level (Typ)
4427 then
4428 if In_Instance_Body then
4429 Error_Msg_N ("?type in allocator has deeper level than" &
4430 " designated class-wide type", E);
4431 Error_Msg_N ("\?Program_Error will be raised at run time",
4433 Rewrite (N,
4434 Make_Raise_Program_Error (Sloc (N),
4435 Reason => PE_Accessibility_Check_Failed));
4436 Set_Etype (N, Typ);
4438 -- Do not apply Ada 2005 accessibility checks on a class-wide
4439 -- allocator if the type given in the allocator is a formal
4440 -- type. A run-time check will be performed in the instance.
4442 elsif not Is_Generic_Type (Exp_Typ) then
4443 Error_Msg_N ("type in allocator has deeper level than" &
4444 " designated class-wide type", E);
4445 end if;
4446 end if;
4447 end;
4448 end if;
4450 -- Check for allocation from an empty storage pool
4452 if No_Pool_Assigned (Typ) then
4453 Error_Msg_N ("allocation from empty storage pool!", N);
4455 -- If the context is an unchecked conversion, as may happen within an
4456 -- inlined subprogram, the allocator is being resolved with its own
4457 -- anonymous type. In that case, if the target type has a specific
4458 -- storage pool, it must be inherited explicitly by the allocator type.
4460 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4461 and then No (Associated_Storage_Pool (Typ))
4462 then
4463 Set_Associated_Storage_Pool
4464 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4465 end if;
4467 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4468 Check_Restriction (No_Anonymous_Allocators, N);
4469 end if;
4471 -- Check that an allocator with task parts isn't for a nested access
4472 -- type when restriction No_Task_Hierarchy applies.
4474 if not Is_Library_Level_Entity (Base_Type (Typ))
4475 and then Has_Task (Base_Type (Desig_T))
4476 then
4477 Check_Restriction (No_Task_Hierarchy, N);
4478 end if;
4480 -- An erroneous allocator may be rewritten as a raise Program_Error
4481 -- statement.
4483 if Nkind (N) = N_Allocator then
4485 -- An anonymous access discriminant is the definition of a
4486 -- coextension.
4488 if Ekind (Typ) = E_Anonymous_Access_Type
4489 and then Nkind (Associated_Node_For_Itype (Typ)) =
4490 N_Discriminant_Specification
4491 then
4492 declare
4493 Discr : constant Entity_Id :=
4494 Defining_Identifier (Associated_Node_For_Itype (Typ));
4496 begin
4497 -- Ada 2012 AI05-0052: If the designated type of the allocator
4498 -- is limited, then the allocator shall not be used to define
4499 -- the value of an access discriminant unless the discriminated
4500 -- type is immutably limited.
4502 if Ada_Version >= Ada_2012
4503 and then Is_Limited_Type (Desig_T)
4504 and then not Is_Immutably_Limited_Type (Scope (Discr))
4505 then
4506 Error_Msg_N
4507 ("only immutably limited types can have anonymous "
4508 & "access discriminants designating a limited type", N);
4509 end if;
4510 end;
4512 -- Avoid marking an allocator as a dynamic coextension if it is
4513 -- within a static construct.
4515 if not Is_Static_Coextension (N) then
4516 Set_Is_Dynamic_Coextension (N);
4517 end if;
4519 -- Cleanup for potential static coextensions
4521 else
4522 Set_Is_Dynamic_Coextension (N, False);
4523 Set_Is_Static_Coextension (N, False);
4524 end if;
4525 end if;
4527 -- Report a simple error: if the designated object is a local task,
4528 -- its body has not been seen yet, and its activation will fail an
4529 -- elaboration check.
4531 if Is_Task_Type (Desig_T)
4532 and then Scope (Base_Type (Desig_T)) = Current_Scope
4533 and then Is_Compilation_Unit (Current_Scope)
4534 and then Ekind (Current_Scope) = E_Package
4535 and then not In_Package_Body (Current_Scope)
4536 then
4537 Error_Msg_N ("?cannot activate task before body seen", N);
4538 Error_Msg_N ("\?Program_Error will be raised at run time", N);
4539 end if;
4541 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4542 -- type with a task component on a subpool. This action must raise
4543 -- Program_Error at runtime.
4545 if Ada_Version >= Ada_2012
4546 and then Nkind (N) = N_Allocator
4547 and then Present (Subpool_Handle_Name (N))
4548 and then Has_Task (Desig_T)
4549 then
4550 Error_Msg_N ("?cannot allocate task on subpool", N);
4551 Error_Msg_N ("\?Program_Error will be raised at run time", N);
4553 Rewrite (N,
4554 Make_Raise_Program_Error (Sloc (N),
4555 Reason => PE_Explicit_Raise));
4556 Set_Etype (N, Typ);
4557 end if;
4558 end Resolve_Allocator;
4560 ---------------------------
4561 -- Resolve_Arithmetic_Op --
4562 ---------------------------
4564 -- Used for resolving all arithmetic operators except exponentiation
4566 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4567 L : constant Node_Id := Left_Opnd (N);
4568 R : constant Node_Id := Right_Opnd (N);
4569 TL : constant Entity_Id := Base_Type (Etype (L));
4570 TR : constant Entity_Id := Base_Type (Etype (R));
4571 T : Entity_Id;
4572 Rop : Node_Id;
4574 B_Typ : constant Entity_Id := Base_Type (Typ);
4575 -- We do the resolution using the base type, because intermediate values
4576 -- in expressions always are of the base type, not a subtype of it.
4578 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4579 -- Returns True if N is in a context that expects "any real type"
4581 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4582 -- Return True iff given type is Integer or universal real/integer
4584 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4585 -- Choose type of integer literal in fixed-point operation to conform
4586 -- to available fixed-point type. T is the type of the other operand,
4587 -- which is needed to determine the expected type of N.
4589 procedure Set_Operand_Type (N : Node_Id);
4590 -- Set operand type to T if universal
4592 -------------------------------
4593 -- Expected_Type_Is_Any_Real --
4594 -------------------------------
4596 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4597 begin
4598 -- N is the expression after "delta" in a fixed_point_definition;
4599 -- see RM-3.5.9(6):
4601 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4602 N_Decimal_Fixed_Point_Definition,
4604 -- N is one of the bounds in a real_range_specification;
4605 -- see RM-3.5.7(5):
4607 N_Real_Range_Specification,
4609 -- N is the expression of a delta_constraint;
4610 -- see RM-J.3(3):
4612 N_Delta_Constraint);
4613 end Expected_Type_Is_Any_Real;
4615 -----------------------------
4616 -- Is_Integer_Or_Universal --
4617 -----------------------------
4619 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4620 T : Entity_Id;
4621 Index : Interp_Index;
4622 It : Interp;
4624 begin
4625 if not Is_Overloaded (N) then
4626 T := Etype (N);
4627 return Base_Type (T) = Base_Type (Standard_Integer)
4628 or else T = Universal_Integer
4629 or else T = Universal_Real;
4630 else
4631 Get_First_Interp (N, Index, It);
4632 while Present (It.Typ) loop
4633 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4634 or else It.Typ = Universal_Integer
4635 or else It.Typ = Universal_Real
4636 then
4637 return True;
4638 end if;
4640 Get_Next_Interp (Index, It);
4641 end loop;
4642 end if;
4644 return False;
4645 end Is_Integer_Or_Universal;
4647 ----------------------------
4648 -- Set_Mixed_Mode_Operand --
4649 ----------------------------
4651 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4652 Index : Interp_Index;
4653 It : Interp;
4655 begin
4656 if Universal_Interpretation (N) = Universal_Integer then
4658 -- A universal integer literal is resolved as standard integer
4659 -- except in the case of a fixed-point result, where we leave it
4660 -- as universal (to be handled by Exp_Fixd later on)
4662 if Is_Fixed_Point_Type (T) then
4663 Resolve (N, Universal_Integer);
4664 else
4665 Resolve (N, Standard_Integer);
4666 end if;
4668 elsif Universal_Interpretation (N) = Universal_Real
4669 and then (T = Base_Type (Standard_Integer)
4670 or else T = Universal_Integer
4671 or else T = Universal_Real)
4672 then
4673 -- A universal real can appear in a fixed-type context. We resolve
4674 -- the literal with that context, even though this might raise an
4675 -- exception prematurely (the other operand may be zero).
4677 Resolve (N, B_Typ);
4679 elsif Etype (N) = Base_Type (Standard_Integer)
4680 and then T = Universal_Real
4681 and then Is_Overloaded (N)
4682 then
4683 -- Integer arg in mixed-mode operation. Resolve with universal
4684 -- type, in case preference rule must be applied.
4686 Resolve (N, Universal_Integer);
4688 elsif Etype (N) = T
4689 and then B_Typ /= Universal_Fixed
4690 then
4691 -- Not a mixed-mode operation, resolve with context
4693 Resolve (N, B_Typ);
4695 elsif Etype (N) = Any_Fixed then
4697 -- N may itself be a mixed-mode operation, so use context type
4699 Resolve (N, B_Typ);
4701 elsif Is_Fixed_Point_Type (T)
4702 and then B_Typ = Universal_Fixed
4703 and then Is_Overloaded (N)
4704 then
4705 -- Must be (fixed * fixed) operation, operand must have one
4706 -- compatible interpretation.
4708 Resolve (N, Any_Fixed);
4710 elsif Is_Fixed_Point_Type (B_Typ)
4711 and then (T = Universal_Real
4712 or else Is_Fixed_Point_Type (T))
4713 and then Is_Overloaded (N)
4714 then
4715 -- C * F(X) in a fixed context, where C is a real literal or a
4716 -- fixed-point expression. F must have either a fixed type
4717 -- interpretation or an integer interpretation, but not both.
4719 Get_First_Interp (N, Index, It);
4720 while Present (It.Typ) loop
4721 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4722 if Analyzed (N) then
4723 Error_Msg_N ("ambiguous operand in fixed operation", N);
4724 else
4725 Resolve (N, Standard_Integer);
4726 end if;
4728 elsif Is_Fixed_Point_Type (It.Typ) then
4729 if Analyzed (N) then
4730 Error_Msg_N ("ambiguous operand in fixed operation", N);
4731 else
4732 Resolve (N, It.Typ);
4733 end if;
4734 end if;
4736 Get_Next_Interp (Index, It);
4737 end loop;
4739 -- Reanalyze the literal with the fixed type of the context. If
4740 -- context is Universal_Fixed, we are within a conversion, leave
4741 -- the literal as a universal real because there is no usable
4742 -- fixed type, and the target of the conversion plays no role in
4743 -- the resolution.
4745 declare
4746 Op2 : Node_Id;
4747 T2 : Entity_Id;
4749 begin
4750 if N = L then
4751 Op2 := R;
4752 else
4753 Op2 := L;
4754 end if;
4756 if B_Typ = Universal_Fixed
4757 and then Nkind (Op2) = N_Real_Literal
4758 then
4759 T2 := Universal_Real;
4760 else
4761 T2 := B_Typ;
4762 end if;
4764 Set_Analyzed (Op2, False);
4765 Resolve (Op2, T2);
4766 end;
4768 else
4769 Resolve (N);
4770 end if;
4771 end Set_Mixed_Mode_Operand;
4773 ----------------------
4774 -- Set_Operand_Type --
4775 ----------------------
4777 procedure Set_Operand_Type (N : Node_Id) is
4778 begin
4779 if Etype (N) = Universal_Integer
4780 or else Etype (N) = Universal_Real
4781 then
4782 Set_Etype (N, T);
4783 end if;
4784 end Set_Operand_Type;
4786 -- Start of processing for Resolve_Arithmetic_Op
4788 begin
4789 if Comes_From_Source (N)
4790 and then Ekind (Entity (N)) = E_Function
4791 and then Is_Imported (Entity (N))
4792 and then Is_Intrinsic_Subprogram (Entity (N))
4793 then
4794 Resolve_Intrinsic_Operator (N, Typ);
4795 return;
4797 -- Special-case for mixed-mode universal expressions or fixed point type
4798 -- operation: each argument is resolved separately. The same treatment
4799 -- is required if one of the operands of a fixed point operation is
4800 -- universal real, since in this case we don't do a conversion to a
4801 -- specific fixed-point type (instead the expander handles the case).
4803 -- Set the type of the node to its universal interpretation because
4804 -- legality checks on an exponentiation operand need the context.
4806 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4807 and then Present (Universal_Interpretation (L))
4808 and then Present (Universal_Interpretation (R))
4809 then
4810 Set_Etype (N, B_Typ);
4811 Resolve (L, Universal_Interpretation (L));
4812 Resolve (R, Universal_Interpretation (R));
4814 elsif (B_Typ = Universal_Real
4815 or else Etype (N) = Universal_Fixed
4816 or else (Etype (N) = Any_Fixed
4817 and then Is_Fixed_Point_Type (B_Typ))
4818 or else (Is_Fixed_Point_Type (B_Typ)
4819 and then (Is_Integer_Or_Universal (L)
4820 or else
4821 Is_Integer_Or_Universal (R))))
4822 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4823 then
4824 if TL = Universal_Integer or else TR = Universal_Integer then
4825 Check_For_Visible_Operator (N, B_Typ);
4826 end if;
4828 -- If context is a fixed type and one operand is integer, the other
4829 -- is resolved with the type of the context.
4831 if Is_Fixed_Point_Type (B_Typ)
4832 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4833 or else TL = Universal_Integer)
4834 then
4835 Resolve (R, B_Typ);
4836 Resolve (L, TL);
4838 elsif Is_Fixed_Point_Type (B_Typ)
4839 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4840 or else TR = Universal_Integer)
4841 then
4842 Resolve (L, B_Typ);
4843 Resolve (R, TR);
4845 else
4846 Set_Mixed_Mode_Operand (L, TR);
4847 Set_Mixed_Mode_Operand (R, TL);
4848 end if;
4850 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4851 -- multiplying operators from being used when the expected type is
4852 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4853 -- some cases where the expected type is actually Any_Real;
4854 -- Expected_Type_Is_Any_Real takes care of that case.
4856 if Etype (N) = Universal_Fixed
4857 or else Etype (N) = Any_Fixed
4858 then
4859 if B_Typ = Universal_Fixed
4860 and then not Expected_Type_Is_Any_Real (N)
4861 and then not Nkind_In (Parent (N), N_Type_Conversion,
4862 N_Unchecked_Type_Conversion)
4863 then
4864 Error_Msg_N ("type cannot be determined from context!", N);
4865 Error_Msg_N ("\explicit conversion to result type required", N);
4867 Set_Etype (L, Any_Type);
4868 Set_Etype (R, Any_Type);
4870 else
4871 if Ada_Version = Ada_83
4872 and then Etype (N) = Universal_Fixed
4873 and then not
4874 Nkind_In (Parent (N), N_Type_Conversion,
4875 N_Unchecked_Type_Conversion)
4876 then
4877 Error_Msg_N
4878 ("(Ada 83) fixed-point operation "
4879 & "needs explicit conversion", N);
4880 end if;
4882 -- The expected type is "any real type" in contexts like
4884 -- type T is delta <universal_fixed-expression> ...
4886 -- in which case we need to set the type to Universal_Real
4887 -- so that static expression evaluation will work properly.
4889 if Expected_Type_Is_Any_Real (N) then
4890 Set_Etype (N, Universal_Real);
4891 else
4892 Set_Etype (N, B_Typ);
4893 end if;
4894 end if;
4896 elsif Is_Fixed_Point_Type (B_Typ)
4897 and then (Is_Integer_Or_Universal (L)
4898 or else Nkind (L) = N_Real_Literal
4899 or else Nkind (R) = N_Real_Literal
4900 or else Is_Integer_Or_Universal (R))
4901 then
4902 Set_Etype (N, B_Typ);
4904 elsif Etype (N) = Any_Fixed then
4906 -- If no previous errors, this is only possible if one operand is
4907 -- overloaded and the context is universal. Resolve as such.
4909 Set_Etype (N, B_Typ);
4910 end if;
4912 else
4913 if (TL = Universal_Integer or else TL = Universal_Real)
4914 and then
4915 (TR = Universal_Integer or else TR = Universal_Real)
4916 then
4917 Check_For_Visible_Operator (N, B_Typ);
4918 end if;
4920 -- If the context is Universal_Fixed and the operands are also
4921 -- universal fixed, this is an error, unless there is only one
4922 -- applicable fixed_point type (usually Duration).
4924 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4925 T := Unique_Fixed_Point_Type (N);
4927 if T = Any_Type then
4928 Set_Etype (N, T);
4929 return;
4930 else
4931 Resolve (L, T);
4932 Resolve (R, T);
4933 end if;
4935 else
4936 Resolve (L, B_Typ);
4937 Resolve (R, B_Typ);
4938 end if;
4940 -- If one of the arguments was resolved to a non-universal type.
4941 -- label the result of the operation itself with the same type.
4942 -- Do the same for the universal argument, if any.
4944 T := Intersect_Types (L, R);
4945 Set_Etype (N, Base_Type (T));
4946 Set_Operand_Type (L);
4947 Set_Operand_Type (R);
4948 end if;
4950 Generate_Operator_Reference (N, Typ);
4951 Analyze_Dimension (N);
4952 Eval_Arithmetic_Op (N);
4954 -- In SPARK, a multiplication or division with operands of fixed point
4955 -- types shall be qualified or explicitly converted to identify the
4956 -- result type.
4958 if (Is_Fixed_Point_Type (Etype (L))
4959 or else Is_Fixed_Point_Type (Etype (R)))
4960 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4961 and then
4962 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
4963 then
4964 Check_SPARK_Restriction
4965 ("operation should be qualified or explicitly converted", N);
4966 end if;
4968 -- Set overflow and division checking bit. Much cleverer code needed
4969 -- here eventually and perhaps the Resolve routines should be separated
4970 -- for the various arithmetic operations, since they will need
4971 -- different processing. ???
4973 if Nkind (N) in N_Op then
4974 if not Overflow_Checks_Suppressed (Etype (N)) then
4975 Enable_Overflow_Check (N);
4976 end if;
4978 -- Give warning if explicit division by zero
4980 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4981 and then not Division_Checks_Suppressed (Etype (N))
4982 then
4983 Rop := Right_Opnd (N);
4985 if Compile_Time_Known_Value (Rop)
4986 and then ((Is_Integer_Type (Etype (Rop))
4987 and then Expr_Value (Rop) = Uint_0)
4988 or else
4989 (Is_Real_Type (Etype (Rop))
4990 and then Expr_Value_R (Rop) = Ureal_0))
4991 then
4992 -- Specialize the warning message according to the operation.
4993 -- The following warnings are for the case
4995 case Nkind (N) is
4996 when N_Op_Divide =>
4998 -- For division, we have two cases, for float division
4999 -- of an unconstrained float type, on a machine where
5000 -- Machine_Overflows is false, we don't get an exception
5001 -- at run-time, but rather an infinity or Nan. The Nan
5002 -- case is pretty obscure, so just warn about infinities.
5004 if Is_Floating_Point_Type (Typ)
5005 and then not Is_Constrained (Typ)
5006 and then not Machine_Overflows_On_Target
5007 then
5008 Error_Msg_N
5009 ("float division by zero, " &
5010 "may generate '+'/'- infinity?", Right_Opnd (N));
5012 -- For all other cases, we get a Constraint_Error
5014 else
5015 Apply_Compile_Time_Constraint_Error
5016 (N, "division by zero?", CE_Divide_By_Zero,
5017 Loc => Sloc (Right_Opnd (N)));
5018 end if;
5020 when N_Op_Rem =>
5021 Apply_Compile_Time_Constraint_Error
5022 (N, "rem with zero divisor?", CE_Divide_By_Zero,
5023 Loc => Sloc (Right_Opnd (N)));
5025 when N_Op_Mod =>
5026 Apply_Compile_Time_Constraint_Error
5027 (N, "mod with zero divisor?", CE_Divide_By_Zero,
5028 Loc => Sloc (Right_Opnd (N)));
5030 -- Division by zero can only happen with division, rem,
5031 -- and mod operations.
5033 when others =>
5034 raise Program_Error;
5035 end case;
5037 -- Otherwise just set the flag to check at run time
5039 else
5040 Activate_Division_Check (N);
5041 end if;
5042 end if;
5044 -- If Restriction No_Implicit_Conditionals is active, then it is
5045 -- violated if either operand can be negative for mod, or for rem
5046 -- if both operands can be negative.
5048 if Restriction_Check_Required (No_Implicit_Conditionals)
5049 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5050 then
5051 declare
5052 Lo : Uint;
5053 Hi : Uint;
5054 OK : Boolean;
5056 LNeg : Boolean;
5057 RNeg : Boolean;
5058 -- Set if corresponding operand might be negative
5060 begin
5061 Determine_Range
5062 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5063 LNeg := (not OK) or else Lo < 0;
5065 Determine_Range
5066 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5067 RNeg := (not OK) or else Lo < 0;
5069 -- Check if we will be generating conditionals. There are two
5070 -- cases where that can happen, first for REM, the only case
5071 -- is largest negative integer mod -1, where the division can
5072 -- overflow, but we still have to give the right result. The
5073 -- front end generates a test for this annoying case. Here we
5074 -- just test if both operands can be negative (that's what the
5075 -- expander does, so we match its logic here).
5077 -- The second case is mod where either operand can be negative.
5078 -- In this case, the back end has to generate additional tests.
5080 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5081 or else
5082 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5083 then
5084 Check_Restriction (No_Implicit_Conditionals, N);
5085 end if;
5086 end;
5087 end if;
5088 end if;
5090 Check_Unset_Reference (L);
5091 Check_Unset_Reference (R);
5092 end Resolve_Arithmetic_Op;
5094 ------------------
5095 -- Resolve_Call --
5096 ------------------
5098 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5099 Loc : constant Source_Ptr := Sloc (N);
5100 Subp : constant Node_Id := Name (N);
5101 Nam : Entity_Id;
5102 I : Interp_Index;
5103 It : Interp;
5104 Norm_OK : Boolean;
5105 Scop : Entity_Id;
5106 Rtype : Entity_Id;
5108 function Same_Or_Aliased_Subprograms
5109 (S : Entity_Id;
5110 E : Entity_Id) return Boolean;
5111 -- Returns True if the subprogram entity S is the same as E or else
5112 -- S is an alias of E.
5114 ---------------------------------
5115 -- Same_Or_Aliased_Subprograms --
5116 ---------------------------------
5118 function Same_Or_Aliased_Subprograms
5119 (S : Entity_Id;
5120 E : Entity_Id) return Boolean
5122 Subp_Alias : constant Entity_Id := Alias (S);
5123 begin
5124 return S = E
5125 or else (Present (Subp_Alias) and then Subp_Alias = E);
5126 end Same_Or_Aliased_Subprograms;
5128 -- Start of processing for Resolve_Call
5130 begin
5131 -- The context imposes a unique interpretation with type Typ on a
5132 -- procedure or function call. Find the entity of the subprogram that
5133 -- yields the expected type, and propagate the corresponding formal
5134 -- constraints on the actuals. The caller has established that an
5135 -- interpretation exists, and emitted an error if not unique.
5137 -- First deal with the case of a call to an access-to-subprogram,
5138 -- dereference made explicit in Analyze_Call.
5140 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5141 if not Is_Overloaded (Subp) then
5142 Nam := Etype (Subp);
5144 else
5145 -- Find the interpretation whose type (a subprogram type) has a
5146 -- return type that is compatible with the context. Analysis of
5147 -- the node has established that one exists.
5149 Nam := Empty;
5151 Get_First_Interp (Subp, I, It);
5152 while Present (It.Typ) loop
5153 if Covers (Typ, Etype (It.Typ)) then
5154 Nam := It.Typ;
5155 exit;
5156 end if;
5158 Get_Next_Interp (I, It);
5159 end loop;
5161 if No (Nam) then
5162 raise Program_Error;
5163 end if;
5164 end if;
5166 -- If the prefix is not an entity, then resolve it
5168 if not Is_Entity_Name (Subp) then
5169 Resolve (Subp, Nam);
5170 end if;
5172 -- For an indirect call, we always invalidate checks, since we do not
5173 -- know whether the subprogram is local or global. Yes we could do
5174 -- better here, e.g. by knowing that there are no local subprograms,
5175 -- but it does not seem worth the effort. Similarly, we kill all
5176 -- knowledge of current constant values.
5178 Kill_Current_Values;
5180 -- If this is a procedure call which is really an entry call, do
5181 -- the conversion of the procedure call to an entry call. Protected
5182 -- operations use the same circuitry because the name in the call
5183 -- can be an arbitrary expression with special resolution rules.
5185 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5186 or else (Is_Entity_Name (Subp)
5187 and then Ekind (Entity (Subp)) = E_Entry)
5188 then
5189 Resolve_Entry_Call (N, Typ);
5190 Check_Elab_Call (N);
5192 -- Kill checks and constant values, as above for indirect case
5193 -- Who knows what happens when another task is activated?
5195 Kill_Current_Values;
5196 return;
5198 -- Normal subprogram call with name established in Resolve
5200 elsif not (Is_Type (Entity (Subp))) then
5201 Nam := Entity (Subp);
5202 Set_Entity_With_Style_Check (Subp, Nam);
5204 -- Otherwise we must have the case of an overloaded call
5206 else
5207 pragma Assert (Is_Overloaded (Subp));
5209 -- Initialize Nam to prevent warning (we know it will be assigned
5210 -- in the loop below, but the compiler does not know that).
5212 Nam := Empty;
5214 Get_First_Interp (Subp, I, It);
5215 while Present (It.Typ) loop
5216 if Covers (Typ, It.Typ) then
5217 Nam := It.Nam;
5218 Set_Entity_With_Style_Check (Subp, Nam);
5219 exit;
5220 end if;
5222 Get_Next_Interp (I, It);
5223 end loop;
5224 end if;
5226 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5227 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5228 and then Nkind (Subp) /= N_Explicit_Dereference
5229 and then Present (Parameter_Associations (N))
5230 then
5231 -- The prefix is a parameterless function call that returns an access
5232 -- to subprogram. If parameters are present in the current call, add
5233 -- add an explicit dereference. We use the base type here because
5234 -- within an instance these may be subtypes.
5236 -- The dereference is added either in Analyze_Call or here. Should
5237 -- be consolidated ???
5239 Set_Is_Overloaded (Subp, False);
5240 Set_Etype (Subp, Etype (Nam));
5241 Insert_Explicit_Dereference (Subp);
5242 Nam := Designated_Type (Etype (Nam));
5243 Resolve (Subp, Nam);
5244 end if;
5246 -- Check that a call to Current_Task does not occur in an entry body
5248 if Is_RTE (Nam, RE_Current_Task) then
5249 declare
5250 P : Node_Id;
5252 begin
5253 P := N;
5254 loop
5255 P := Parent (P);
5257 -- Exclude calls that occur within the default of a formal
5258 -- parameter of the entry, since those are evaluated outside
5259 -- of the body.
5261 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5263 if Nkind (P) = N_Entry_Body
5264 or else (Nkind (P) = N_Subprogram_Body
5265 and then Is_Entry_Barrier_Function (P))
5266 then
5267 Rtype := Etype (N);
5268 Error_Msg_NE
5269 ("?& should not be used in entry body (RM C.7(17))",
5270 N, Nam);
5271 Error_Msg_NE
5272 ("\Program_Error will be raised at run time?", N, Nam);
5273 Rewrite (N,
5274 Make_Raise_Program_Error (Loc,
5275 Reason => PE_Current_Task_In_Entry_Body));
5276 Set_Etype (N, Rtype);
5277 return;
5278 end if;
5279 end loop;
5280 end;
5281 end if;
5283 -- Check that a procedure call does not occur in the context of the
5284 -- entry call statement of a conditional or timed entry call. Note that
5285 -- the case of a call to a subprogram renaming of an entry will also be
5286 -- rejected. The test for N not being an N_Entry_Call_Statement is
5287 -- defensive, covering the possibility that the processing of entry
5288 -- calls might reach this point due to later modifications of the code
5289 -- above.
5291 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5292 and then Nkind (N) /= N_Entry_Call_Statement
5293 and then Entry_Call_Statement (Parent (N)) = N
5294 then
5295 if Ada_Version < Ada_2005 then
5296 Error_Msg_N ("entry call required in select statement", N);
5298 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5299 -- for a procedure_or_entry_call, the procedure_name or
5300 -- procedure_prefix of the procedure_call_statement shall denote
5301 -- an entry renamed by a procedure, or (a view of) a primitive
5302 -- subprogram of a limited interface whose first parameter is
5303 -- a controlling parameter.
5305 elsif Nkind (N) = N_Procedure_Call_Statement
5306 and then not Is_Renamed_Entry (Nam)
5307 and then not Is_Controlling_Limited_Procedure (Nam)
5308 then
5309 Error_Msg_N
5310 ("entry call or dispatching primitive of interface required", N);
5311 end if;
5312 end if;
5314 -- Check that this is not a call to a protected procedure or entry from
5315 -- within a protected function.
5317 if Ekind (Current_Scope) = E_Function
5318 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
5319 and then Ekind (Nam) /= E_Function
5320 and then Scope (Nam) = Scope (Current_Scope)
5321 then
5322 Error_Msg_N ("within protected function, protected " &
5323 "object is constant", N);
5324 Error_Msg_N ("\cannot call operation that may modify it", N);
5325 end if;
5327 -- Freeze the subprogram name if not in a spec-expression. Note that we
5328 -- freeze procedure calls as well as function calls. Procedure calls are
5329 -- not frozen according to the rules (RM 13.14(14)) because it is
5330 -- impossible to have a procedure call to a non-frozen procedure in pure
5331 -- Ada, but in the code that we generate in the expander, this rule
5332 -- needs extending because we can generate procedure calls that need
5333 -- freezing.
5335 -- In Ada 2012, expression functions may be called within pre/post
5336 -- conditions of subsequent functions or expression functions. Such
5337 -- calls do not freeze when they appear within generated bodies, which
5338 -- would place the freeze node in the wrong scope. An expression
5339 -- function is frozen in the usual fashion, by the appearance of a real
5340 -- body, or at the end of a declarative part.
5342 if Is_Entity_Name (Subp) and then not In_Spec_Expression
5343 and then
5344 (not Is_Expression_Function (Entity (Subp))
5345 or else Scope (Entity (Subp)) = Current_Scope)
5346 then
5347 Freeze_Expression (Subp);
5348 end if;
5350 -- For a predefined operator, the type of the result is the type imposed
5351 -- by context, except for a predefined operation on universal fixed.
5352 -- Otherwise The type of the call is the type returned by the subprogram
5353 -- being called.
5355 if Is_Predefined_Op (Nam) then
5356 if Etype (N) /= Universal_Fixed then
5357 Set_Etype (N, Typ);
5358 end if;
5360 -- If the subprogram returns an array type, and the context requires the
5361 -- component type of that array type, the node is really an indexing of
5362 -- the parameterless call. Resolve as such. A pathological case occurs
5363 -- when the type of the component is an access to the array type. In
5364 -- this case the call is truly ambiguous.
5366 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5367 and then
5368 ((Is_Array_Type (Etype (Nam))
5369 and then Covers (Typ, Component_Type (Etype (Nam))))
5370 or else (Is_Access_Type (Etype (Nam))
5371 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5372 and then
5373 Covers
5374 (Typ,
5375 Component_Type (Designated_Type (Etype (Nam))))))
5376 then
5377 declare
5378 Index_Node : Node_Id;
5379 New_Subp : Node_Id;
5380 Ret_Type : constant Entity_Id := Etype (Nam);
5382 begin
5383 if Is_Access_Type (Ret_Type)
5384 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5385 then
5386 Error_Msg_N
5387 ("cannot disambiguate function call and indexing", N);
5388 else
5389 New_Subp := Relocate_Node (Subp);
5390 Set_Entity (Subp, Nam);
5392 if (Is_Array_Type (Ret_Type)
5393 and then Component_Type (Ret_Type) /= Any_Type)
5394 or else
5395 (Is_Access_Type (Ret_Type)
5396 and then
5397 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5398 then
5399 if Needs_No_Actuals (Nam) then
5401 -- Indexed call to a parameterless function
5403 Index_Node :=
5404 Make_Indexed_Component (Loc,
5405 Prefix =>
5406 Make_Function_Call (Loc,
5407 Name => New_Subp),
5408 Expressions => Parameter_Associations (N));
5409 else
5410 -- An Ada 2005 prefixed call to a primitive operation
5411 -- whose first parameter is the prefix. This prefix was
5412 -- prepended to the parameter list, which is actually a
5413 -- list of indexes. Remove the prefix in order to build
5414 -- the proper indexed component.
5416 Index_Node :=
5417 Make_Indexed_Component (Loc,
5418 Prefix =>
5419 Make_Function_Call (Loc,
5420 Name => New_Subp,
5421 Parameter_Associations =>
5422 New_List
5423 (Remove_Head (Parameter_Associations (N)))),
5424 Expressions => Parameter_Associations (N));
5425 end if;
5427 -- Preserve the parenthesis count of the node
5429 Set_Paren_Count (Index_Node, Paren_Count (N));
5431 -- Since we are correcting a node classification error made
5432 -- by the parser, we call Replace rather than Rewrite.
5434 Replace (N, Index_Node);
5436 Set_Etype (Prefix (N), Ret_Type);
5437 Set_Etype (N, Typ);
5438 Resolve_Indexed_Component (N, Typ);
5439 Check_Elab_Call (Prefix (N));
5440 end if;
5441 end if;
5443 return;
5444 end;
5446 else
5447 Set_Etype (N, Etype (Nam));
5448 end if;
5450 -- In the case where the call is to an overloaded subprogram, Analyze
5451 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5452 -- such a case Normalize_Actuals needs to be called once more to order
5453 -- the actuals correctly. Otherwise the call will have the ordering
5454 -- given by the last overloaded subprogram whether this is the correct
5455 -- one being called or not.
5457 if Is_Overloaded (Subp) then
5458 Normalize_Actuals (N, Nam, False, Norm_OK);
5459 pragma Assert (Norm_OK);
5460 end if;
5462 -- In any case, call is fully resolved now. Reset Overload flag, to
5463 -- prevent subsequent overload resolution if node is analyzed again
5465 Set_Is_Overloaded (Subp, False);
5466 Set_Is_Overloaded (N, False);
5468 -- If we are calling the current subprogram from immediately within its
5469 -- body, then that is the case where we can sometimes detect cases of
5470 -- infinite recursion statically. Do not try this in case restriction
5471 -- No_Recursion is in effect anyway, and do it only for source calls.
5473 if Comes_From_Source (N) then
5474 Scop := Current_Scope;
5476 -- Issue warning for possible infinite recursion in the absence
5477 -- of the No_Recursion restriction.
5479 if Same_Or_Aliased_Subprograms (Nam, Scop)
5480 and then not Restriction_Active (No_Recursion)
5481 and then Check_Infinite_Recursion (N)
5482 then
5483 -- Here we detected and flagged an infinite recursion, so we do
5484 -- not need to test the case below for further warnings. Also we
5485 -- are all done if we now have a raise SE node.
5487 if Nkind (N) = N_Raise_Storage_Error then
5488 return;
5489 end if;
5491 -- If call is to immediately containing subprogram, then check for
5492 -- the case of a possible run-time detectable infinite recursion.
5494 else
5495 Scope_Loop : while Scop /= Standard_Standard loop
5496 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5498 -- Although in general case, recursion is not statically
5499 -- checkable, the case of calling an immediately containing
5500 -- subprogram is easy to catch.
5502 Check_Restriction (No_Recursion, N);
5504 -- If the recursive call is to a parameterless subprogram,
5505 -- then even if we can't statically detect infinite
5506 -- recursion, this is pretty suspicious, and we output a
5507 -- warning. Furthermore, we will try later to detect some
5508 -- cases here at run time by expanding checking code (see
5509 -- Detect_Infinite_Recursion in package Exp_Ch6).
5511 -- If the recursive call is within a handler, do not emit a
5512 -- warning, because this is a common idiom: loop until input
5513 -- is correct, catch illegal input in handler and restart.
5515 if No (First_Formal (Nam))
5516 and then Etype (Nam) = Standard_Void_Type
5517 and then not Error_Posted (N)
5518 and then Nkind (Parent (N)) /= N_Exception_Handler
5519 then
5520 -- For the case of a procedure call. We give the message
5521 -- only if the call is the first statement in a sequence
5522 -- of statements, or if all previous statements are
5523 -- simple assignments. This is simply a heuristic to
5524 -- decrease false positives, without losing too many good
5525 -- warnings. The idea is that these previous statements
5526 -- may affect global variables the procedure depends on.
5527 -- We also exclude raise statements, that may arise from
5528 -- constraint checks and are probably unrelated to the
5529 -- intended control flow.
5531 if Nkind (N) = N_Procedure_Call_Statement
5532 and then Is_List_Member (N)
5533 then
5534 declare
5535 P : Node_Id;
5536 begin
5537 P := Prev (N);
5538 while Present (P) loop
5539 if not Nkind_In (P,
5540 N_Assignment_Statement,
5541 N_Raise_Constraint_Error)
5542 then
5543 exit Scope_Loop;
5544 end if;
5546 Prev (P);
5547 end loop;
5548 end;
5549 end if;
5551 -- Do not give warning if we are in a conditional context
5553 declare
5554 K : constant Node_Kind := Nkind (Parent (N));
5555 begin
5556 if (K = N_Loop_Statement
5557 and then Present (Iteration_Scheme (Parent (N))))
5558 or else K = N_If_Statement
5559 or else K = N_Elsif_Part
5560 or else K = N_Case_Statement_Alternative
5561 then
5562 exit Scope_Loop;
5563 end if;
5564 end;
5566 -- Here warning is to be issued
5568 Set_Has_Recursive_Call (Nam);
5569 Error_Msg_N
5570 ("?possible infinite recursion!", N);
5571 Error_Msg_N
5572 ("\?Storage_Error may be raised at run time!", N);
5573 end if;
5575 exit Scope_Loop;
5576 end if;
5578 Scop := Scope (Scop);
5579 end loop Scope_Loop;
5580 end if;
5581 end if;
5583 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5585 Check_Obsolescent_2005_Entity (Nam, Subp);
5587 -- If subprogram name is a predefined operator, it was given in
5588 -- functional notation. Replace call node with operator node, so
5589 -- that actuals can be resolved appropriately.
5591 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5592 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5593 return;
5595 elsif Present (Alias (Nam))
5596 and then Is_Predefined_Op (Alias (Nam))
5597 then
5598 Resolve_Actuals (N, Nam);
5599 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5600 return;
5601 end if;
5603 -- Create a transient scope if the resulting type requires it
5605 -- There are several notable exceptions:
5607 -- a) In init procs, the transient scope overhead is not needed, and is
5608 -- even incorrect when the call is a nested initialization call for a
5609 -- component whose expansion may generate adjust calls. However, if the
5610 -- call is some other procedure call within an initialization procedure
5611 -- (for example a call to Create_Task in the init_proc of the task
5612 -- run-time record) a transient scope must be created around this call.
5614 -- b) Enumeration literal pseudo-calls need no transient scope
5616 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5617 -- functions) do not use the secondary stack even though the return
5618 -- type may be unconstrained.
5620 -- d) Calls to a build-in-place function, since such functions may
5621 -- allocate their result directly in a target object, and cases where
5622 -- the result does get allocated in the secondary stack are checked for
5623 -- within the specialized Exp_Ch6 procedures for expanding those
5624 -- build-in-place calls.
5626 -- e) If the subprogram is marked Inline_Always, then even if it returns
5627 -- an unconstrained type the call does not require use of the secondary
5628 -- stack. However, inlining will only take place if the body to inline
5629 -- is already present. It may not be available if e.g. the subprogram is
5630 -- declared in a child instance.
5632 -- If this is an initialization call for a type whose construction
5633 -- uses the secondary stack, and it is not a nested call to initialize
5634 -- a component, we do need to create a transient scope for it. We
5635 -- check for this by traversing the type in Check_Initialization_Call.
5637 if Is_Inlined (Nam)
5638 and then Has_Pragma_Inline_Always (Nam)
5639 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5640 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5641 and then not Debug_Flag_Dot_K
5642 then
5643 null;
5645 elsif Is_Inlined (Nam)
5646 and then Has_Pragma_Inline (Nam)
5647 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5648 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5649 and then Debug_Flag_Dot_K
5650 then
5651 null;
5653 elsif Ekind (Nam) = E_Enumeration_Literal
5654 or else Is_Build_In_Place_Function (Nam)
5655 or else Is_Intrinsic_Subprogram (Nam)
5656 then
5657 null;
5659 elsif Full_Expander_Active
5660 and then Is_Type (Etype (Nam))
5661 and then Requires_Transient_Scope (Etype (Nam))
5662 and then
5663 (not Within_Init_Proc
5664 or else
5665 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5666 then
5667 Establish_Transient_Scope (N, Sec_Stack => True);
5669 -- If the call appears within the bounds of a loop, it will
5670 -- be rewritten and reanalyzed, nothing left to do here.
5672 if Nkind (N) /= N_Function_Call then
5673 return;
5674 end if;
5676 elsif Is_Init_Proc (Nam)
5677 and then not Within_Init_Proc
5678 then
5679 Check_Initialization_Call (N, Nam);
5680 end if;
5682 -- A protected function cannot be called within the definition of the
5683 -- enclosing protected type.
5685 if Is_Protected_Type (Scope (Nam))
5686 and then In_Open_Scopes (Scope (Nam))
5687 and then not Has_Completion (Scope (Nam))
5688 then
5689 Error_Msg_NE
5690 ("& cannot be called before end of protected definition", N, Nam);
5691 end if;
5693 -- Propagate interpretation to actuals, and add default expressions
5694 -- where needed.
5696 if Present (First_Formal (Nam)) then
5697 Resolve_Actuals (N, Nam);
5699 -- Overloaded literals are rewritten as function calls, for purpose of
5700 -- resolution. After resolution, we can replace the call with the
5701 -- literal itself.
5703 elsif Ekind (Nam) = E_Enumeration_Literal then
5704 Copy_Node (Subp, N);
5705 Resolve_Entity_Name (N, Typ);
5707 -- Avoid validation, since it is a static function call
5709 Generate_Reference (Nam, Subp);
5710 return;
5711 end if;
5713 -- If the subprogram is not global, then kill all saved values and
5714 -- checks. This is a bit conservative, since in many cases we could do
5715 -- better, but it is not worth the effort. Similarly, we kill constant
5716 -- values. However we do not need to do this for internal entities
5717 -- (unless they are inherited user-defined subprograms), since they
5718 -- are not in the business of molesting local values.
5720 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5721 -- kill all checks and values for calls to global subprograms. This
5722 -- takes care of the case where an access to a local subprogram is
5723 -- taken, and could be passed directly or indirectly and then called
5724 -- from almost any context.
5726 -- Note: we do not do this step till after resolving the actuals. That
5727 -- way we still take advantage of the current value information while
5728 -- scanning the actuals.
5730 -- We suppress killing values if we are processing the nodes associated
5731 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5732 -- type kills all the values as part of analyzing the code that
5733 -- initializes the dispatch tables.
5735 if Inside_Freezing_Actions = 0
5736 and then (not Is_Library_Level_Entity (Nam)
5737 or else Suppress_Value_Tracking_On_Call
5738 (Nearest_Dynamic_Scope (Current_Scope)))
5739 and then (Comes_From_Source (Nam)
5740 or else (Present (Alias (Nam))
5741 and then Comes_From_Source (Alias (Nam))))
5742 then
5743 Kill_Current_Values;
5744 end if;
5746 -- If we are warning about unread OUT parameters, this is the place to
5747 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5748 -- after the above call to Kill_Current_Values (since that call clears
5749 -- the Last_Assignment field of all local variables).
5751 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5752 and then Comes_From_Source (N)
5753 and then In_Extended_Main_Source_Unit (N)
5754 then
5755 declare
5756 F : Entity_Id;
5757 A : Node_Id;
5759 begin
5760 F := First_Formal (Nam);
5761 A := First_Actual (N);
5762 while Present (F) and then Present (A) loop
5763 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5764 and then Warn_On_Modified_As_Out_Parameter (F)
5765 and then Is_Entity_Name (A)
5766 and then Present (Entity (A))
5767 and then Comes_From_Source (N)
5768 and then Safe_To_Capture_Value (N, Entity (A))
5769 then
5770 Set_Last_Assignment (Entity (A), A);
5771 end if;
5773 Next_Formal (F);
5774 Next_Actual (A);
5775 end loop;
5776 end;
5777 end if;
5779 -- If the subprogram is a primitive operation, check whether or not
5780 -- it is a correct dispatching call.
5782 if Is_Overloadable (Nam)
5783 and then Is_Dispatching_Operation (Nam)
5784 then
5785 Check_Dispatching_Call (N);
5787 elsif Ekind (Nam) /= E_Subprogram_Type
5788 and then Is_Abstract_Subprogram (Nam)
5789 and then not In_Instance
5790 then
5791 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5792 end if;
5794 -- If this is a dispatching call, generate the appropriate reference,
5795 -- for better source navigation in GPS.
5797 if Is_Overloadable (Nam)
5798 and then Present (Controlling_Argument (N))
5799 then
5800 Generate_Reference (Nam, Subp, 'R');
5802 -- Normal case, not a dispatching call: generate a call reference
5804 else
5805 Generate_Reference (Nam, Subp, 's');
5806 end if;
5808 if Is_Intrinsic_Subprogram (Nam) then
5809 Check_Intrinsic_Call (N);
5810 end if;
5812 -- Check for violation of restriction No_Specific_Termination_Handlers
5813 -- and warn on a potentially blocking call to Abort_Task.
5815 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5816 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5817 or else
5818 Is_RTE (Nam, RE_Specific_Handler))
5819 then
5820 Check_Restriction (No_Specific_Termination_Handlers, N);
5822 elsif Is_RTE (Nam, RE_Abort_Task) then
5823 Check_Potentially_Blocking_Operation (N);
5824 end if;
5826 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5827 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5828 -- need to check the second argument to determine whether it is an
5829 -- absolute or relative timing event.
5831 if Restriction_Check_Required (No_Relative_Delay)
5832 and then Is_RTE (Nam, RE_Set_Handler)
5833 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5834 then
5835 Check_Restriction (No_Relative_Delay, N);
5836 end if;
5838 -- Issue an error for a call to an eliminated subprogram. This routine
5839 -- will not perform the check if the call appears within a default
5840 -- expression.
5842 Check_For_Eliminated_Subprogram (Subp, Nam);
5844 -- In formal mode, the primitive operations of a tagged type or type
5845 -- extension do not include functions that return the tagged type.
5847 -- Commented out as the call to Is_Inherited_Operation_For_Type may
5848 -- cause an error because the type entity of the parent node of
5849 -- Entity (Name (N) may not be set. ???
5850 -- So why not just add a guard ???
5852 -- if Nkind (N) = N_Function_Call
5853 -- and then Is_Tagged_Type (Etype (N))
5854 -- and then Is_Entity_Name (Name (N))
5855 -- and then Is_Inherited_Operation_For_Type
5856 -- (Entity (Name (N)), Etype (N))
5857 -- then
5858 -- Check_SPARK_Restriction ("function not inherited", N);
5859 -- end if;
5861 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5862 -- class-wide and the call dispatches on result in a context that does
5863 -- not provide a tag, the call raises Program_Error.
5865 if Nkind (N) = N_Function_Call
5866 and then In_Instance
5867 and then Is_Generic_Actual_Type (Typ)
5868 and then Is_Class_Wide_Type (Typ)
5869 and then Has_Controlling_Result (Nam)
5870 and then Nkind (Parent (N)) = N_Object_Declaration
5871 then
5872 -- Verify that none of the formals are controlling
5874 declare
5875 Call_OK : Boolean := False;
5876 F : Entity_Id;
5878 begin
5879 F := First_Formal (Nam);
5880 while Present (F) loop
5881 if Is_Controlling_Formal (F) then
5882 Call_OK := True;
5883 exit;
5884 end if;
5886 Next_Formal (F);
5887 end loop;
5889 if not Call_OK then
5890 Error_Msg_N ("!? cannot determine tag of result", N);
5891 Error_Msg_N ("!? Program_Error will be raised", N);
5892 Insert_Action (N,
5893 Make_Raise_Program_Error (Sloc (N),
5894 Reason => PE_Explicit_Raise));
5895 end if;
5896 end;
5897 end if;
5899 Analyze_Dimension (N);
5901 -- All done, evaluate call and deal with elaboration issues
5903 Eval_Call (N);
5904 Check_Elab_Call (N);
5905 Warn_On_Overlapping_Actuals (Nam, N);
5906 end Resolve_Call;
5908 -----------------------------
5909 -- Resolve_Case_Expression --
5910 -----------------------------
5912 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5913 Alt : Node_Id;
5915 begin
5916 Alt := First (Alternatives (N));
5917 while Present (Alt) loop
5918 Resolve (Expression (Alt), Typ);
5919 Next (Alt);
5920 end loop;
5922 Set_Etype (N, Typ);
5923 Eval_Case_Expression (N);
5924 end Resolve_Case_Expression;
5926 -------------------------------
5927 -- Resolve_Character_Literal --
5928 -------------------------------
5930 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5931 B_Typ : constant Entity_Id := Base_Type (Typ);
5932 C : Entity_Id;
5934 begin
5935 -- Verify that the character does belong to the type of the context
5937 Set_Etype (N, B_Typ);
5938 Eval_Character_Literal (N);
5940 -- Wide_Wide_Character literals must always be defined, since the set
5941 -- of wide wide character literals is complete, i.e. if a character
5942 -- literal is accepted by the parser, then it is OK for wide wide
5943 -- character (out of range character literals are rejected).
5945 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5946 return;
5948 -- Always accept character literal for type Any_Character, which
5949 -- occurs in error situations and in comparisons of literals, both
5950 -- of which should accept all literals.
5952 elsif B_Typ = Any_Character then
5953 return;
5955 -- For Standard.Character or a type derived from it, check that the
5956 -- literal is in range.
5958 elsif Root_Type (B_Typ) = Standard_Character then
5959 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5960 return;
5961 end if;
5963 -- For Standard.Wide_Character or a type derived from it, check that the
5964 -- literal is in range.
5966 elsif Root_Type (B_Typ) = Standard_Wide_Character then
5967 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5968 return;
5969 end if;
5971 -- For Standard.Wide_Wide_Character or a type derived from it, we
5972 -- know the literal is in range, since the parser checked!
5974 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5975 return;
5977 -- If the entity is already set, this has already been resolved in a
5978 -- generic context, or comes from expansion. Nothing else to do.
5980 elsif Present (Entity (N)) then
5981 return;
5983 -- Otherwise we have a user defined character type, and we can use the
5984 -- standard visibility mechanisms to locate the referenced entity.
5986 else
5987 C := Current_Entity (N);
5988 while Present (C) loop
5989 if Etype (C) = B_Typ then
5990 Set_Entity_With_Style_Check (N, C);
5991 Generate_Reference (C, N);
5992 return;
5993 end if;
5995 C := Homonym (C);
5996 end loop;
5997 end if;
5999 -- If we fall through, then the literal does not match any of the
6000 -- entries of the enumeration type. This isn't just a constraint error
6001 -- situation, it is an illegality (see RM 4.2).
6003 Error_Msg_NE
6004 ("character not defined for }", N, First_Subtype (B_Typ));
6005 end Resolve_Character_Literal;
6007 ---------------------------
6008 -- Resolve_Comparison_Op --
6009 ---------------------------
6011 -- Context requires a boolean type, and plays no role in resolution.
6012 -- Processing identical to that for equality operators. The result type is
6013 -- the base type, which matters when pathological subtypes of booleans with
6014 -- limited ranges are used.
6016 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6017 L : constant Node_Id := Left_Opnd (N);
6018 R : constant Node_Id := Right_Opnd (N);
6019 T : Entity_Id;
6021 begin
6022 -- If this is an intrinsic operation which is not predefined, use the
6023 -- types of its declared arguments to resolve the possibly overloaded
6024 -- operands. Otherwise the operands are unambiguous and specify the
6025 -- expected type.
6027 if Scope (Entity (N)) /= Standard_Standard then
6028 T := Etype (First_Entity (Entity (N)));
6030 else
6031 T := Find_Unique_Type (L, R);
6033 if T = Any_Fixed then
6034 T := Unique_Fixed_Point_Type (L);
6035 end if;
6036 end if;
6038 Set_Etype (N, Base_Type (Typ));
6039 Generate_Reference (T, N, ' ');
6041 -- Skip remaining processing if already set to Any_Type
6043 if T = Any_Type then
6044 return;
6045 end if;
6047 -- Deal with other error cases
6049 if T = Any_String or else
6050 T = Any_Composite or else
6051 T = Any_Character
6052 then
6053 if T = Any_Character then
6054 Ambiguous_Character (L);
6055 else
6056 Error_Msg_N ("ambiguous operands for comparison", N);
6057 end if;
6059 Set_Etype (N, Any_Type);
6060 return;
6061 end if;
6063 -- Resolve the operands if types OK
6065 Resolve (L, T);
6066 Resolve (R, T);
6067 Check_Unset_Reference (L);
6068 Check_Unset_Reference (R);
6069 Generate_Operator_Reference (N, T);
6070 Check_Low_Bound_Tested (N);
6072 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6073 -- types or array types except String.
6075 if Is_Boolean_Type (T) then
6076 Check_SPARK_Restriction
6077 ("comparison is not defined on Boolean type", N);
6079 elsif Is_Array_Type (T)
6080 and then Base_Type (T) /= Standard_String
6081 then
6082 Check_SPARK_Restriction
6083 ("comparison is not defined on array types other than String", N);
6084 end if;
6086 -- Check comparison on unordered enumeration
6088 if Comes_From_Source (N)
6089 and then Bad_Unordered_Enumeration_Reference (N, Etype (L))
6090 then
6091 Error_Msg_N ("comparison on unordered enumeration type?", N);
6092 end if;
6094 -- Evaluate the relation (note we do this after the above check since
6095 -- this Eval call may change N to True/False.
6097 Analyze_Dimension (N);
6098 Eval_Relational_Op (N);
6099 end Resolve_Comparison_Op;
6101 ------------------------------------
6102 -- Resolve_Conditional_Expression --
6103 ------------------------------------
6105 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
6106 Condition : constant Node_Id := First (Expressions (N));
6107 Then_Expr : constant Node_Id := Next (Condition);
6108 Else_Expr : Node_Id := Next (Then_Expr);
6109 Else_Typ : Entity_Id;
6110 Then_Typ : Entity_Id;
6112 begin
6113 Resolve (Condition, Any_Boolean);
6114 Resolve (Then_Expr, Typ);
6115 Then_Typ := Etype (Then_Expr);
6117 -- When the "then" and "else" expressions are of a scalar type, insert
6118 -- a conversion to ensure the generation of a constraint check.
6120 if Is_Scalar_Type (Then_Typ)
6121 and then Then_Typ /= Typ
6122 then
6123 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
6124 Analyze_And_Resolve (Then_Expr, Typ);
6125 end if;
6127 -- If ELSE expression present, just resolve using the determined type
6129 if Present (Else_Expr) then
6130 Resolve (Else_Expr, Typ);
6131 Else_Typ := Etype (Else_Expr);
6133 if Is_Scalar_Type (Else_Typ)
6134 and then Else_Typ /= Typ
6135 then
6136 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
6137 Analyze_And_Resolve (Else_Expr, Typ);
6138 end if;
6140 -- If no ELSE expression is present, root type must be Standard.Boolean
6141 -- and we provide a Standard.True result converted to the appropriate
6142 -- Boolean type (in case it is a derived boolean type).
6144 elsif Root_Type (Typ) = Standard_Boolean then
6145 Else_Expr :=
6146 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
6147 Analyze_And_Resolve (Else_Expr, Typ);
6148 Append_To (Expressions (N), Else_Expr);
6150 else
6151 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
6152 Append_To (Expressions (N), Error);
6153 end if;
6155 Set_Etype (N, Typ);
6156 Eval_Conditional_Expression (N);
6157 end Resolve_Conditional_Expression;
6159 -----------------------------------------
6160 -- Resolve_Discrete_Subtype_Indication --
6161 -----------------------------------------
6163 procedure Resolve_Discrete_Subtype_Indication
6164 (N : Node_Id;
6165 Typ : Entity_Id)
6167 R : Node_Id;
6168 S : Entity_Id;
6170 begin
6171 Analyze (Subtype_Mark (N));
6172 S := Entity (Subtype_Mark (N));
6174 if Nkind (Constraint (N)) /= N_Range_Constraint then
6175 Error_Msg_N ("expect range constraint for discrete type", N);
6176 Set_Etype (N, Any_Type);
6178 else
6179 R := Range_Expression (Constraint (N));
6181 if R = Error then
6182 return;
6183 end if;
6185 Analyze (R);
6187 if Base_Type (S) /= Base_Type (Typ) then
6188 Error_Msg_NE
6189 ("expect subtype of }", N, First_Subtype (Typ));
6191 -- Rewrite the constraint as a range of Typ
6192 -- to allow compilation to proceed further.
6194 Set_Etype (N, Typ);
6195 Rewrite (Low_Bound (R),
6196 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6197 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6198 Attribute_Name => Name_First));
6199 Rewrite (High_Bound (R),
6200 Make_Attribute_Reference (Sloc (High_Bound (R)),
6201 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6202 Attribute_Name => Name_First));
6204 else
6205 Resolve (R, Typ);
6206 Set_Etype (N, Etype (R));
6208 -- Additionally, we must check that the bounds are compatible
6209 -- with the given subtype, which might be different from the
6210 -- type of the context.
6212 Apply_Range_Check (R, S);
6214 -- ??? If the above check statically detects a Constraint_Error
6215 -- it replaces the offending bound(s) of the range R with a
6216 -- Constraint_Error node. When the itype which uses these bounds
6217 -- is frozen the resulting call to Duplicate_Subexpr generates
6218 -- a new temporary for the bounds.
6220 -- Unfortunately there are other itypes that are also made depend
6221 -- on these bounds, so when Duplicate_Subexpr is called they get
6222 -- a forward reference to the newly created temporaries and Gigi
6223 -- aborts on such forward references. This is probably sign of a
6224 -- more fundamental problem somewhere else in either the order of
6225 -- itype freezing or the way certain itypes are constructed.
6227 -- To get around this problem we call Remove_Side_Effects right
6228 -- away if either bounds of R are a Constraint_Error.
6230 declare
6231 L : constant Node_Id := Low_Bound (R);
6232 H : constant Node_Id := High_Bound (R);
6234 begin
6235 if Nkind (L) = N_Raise_Constraint_Error then
6236 Remove_Side_Effects (L);
6237 end if;
6239 if Nkind (H) = N_Raise_Constraint_Error then
6240 Remove_Side_Effects (H);
6241 end if;
6242 end;
6244 Check_Unset_Reference (Low_Bound (R));
6245 Check_Unset_Reference (High_Bound (R));
6246 end if;
6247 end if;
6248 end Resolve_Discrete_Subtype_Indication;
6250 -------------------------
6251 -- Resolve_Entity_Name --
6252 -------------------------
6254 -- Used to resolve identifiers and expanded names
6256 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6257 E : constant Entity_Id := Entity (N);
6259 begin
6260 -- If garbage from errors, set to Any_Type and return
6262 if No (E) and then Total_Errors_Detected /= 0 then
6263 Set_Etype (N, Any_Type);
6264 return;
6265 end if;
6267 -- Replace named numbers by corresponding literals. Note that this is
6268 -- the one case where Resolve_Entity_Name must reset the Etype, since
6269 -- it is currently marked as universal.
6271 if Ekind (E) = E_Named_Integer then
6272 Set_Etype (N, Typ);
6273 Eval_Named_Integer (N);
6275 elsif Ekind (E) = E_Named_Real then
6276 Set_Etype (N, Typ);
6277 Eval_Named_Real (N);
6279 -- For enumeration literals, we need to make sure that a proper style
6280 -- check is done, since such literals are overloaded, and thus we did
6281 -- not do a style check during the first phase of analysis.
6283 elsif Ekind (E) = E_Enumeration_Literal then
6284 Set_Entity_With_Style_Check (N, E);
6285 Eval_Entity_Name (N);
6287 -- Case of subtype name appearing as an operand in expression
6289 elsif Is_Type (E) then
6291 -- Allow use of subtype if it is a concurrent type where we are
6292 -- currently inside the body. This will eventually be expanded into a
6293 -- call to Self (for tasks) or _object (for protected objects). Any
6294 -- other use of a subtype is invalid.
6296 if Is_Concurrent_Type (E)
6297 and then In_Open_Scopes (E)
6298 then
6299 null;
6301 -- Any other use is an error
6303 else
6304 Error_Msg_N
6305 ("invalid use of subtype mark in expression or call", N);
6306 end if;
6308 -- Check discriminant use if entity is discriminant in current scope,
6309 -- i.e. discriminant of record or concurrent type currently being
6310 -- analyzed. Uses in corresponding body are unrestricted.
6312 elsif Ekind (E) = E_Discriminant
6313 and then Scope (E) = Current_Scope
6314 and then not Has_Completion (Current_Scope)
6315 then
6316 Check_Discriminant_Use (N);
6318 -- A parameterless generic function cannot appear in a context that
6319 -- requires resolution.
6321 elsif Ekind (E) = E_Generic_Function then
6322 Error_Msg_N ("illegal use of generic function", N);
6324 elsif Ekind (E) = E_Out_Parameter
6325 and then Ada_Version = Ada_83
6326 and then (Nkind (Parent (N)) in N_Op
6327 or else (Nkind (Parent (N)) = N_Assignment_Statement
6328 and then N = Expression (Parent (N)))
6329 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6330 then
6331 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6333 -- In all other cases, just do the possible static evaluation
6335 else
6336 -- A deferred constant that appears in an expression must have a
6337 -- completion, unless it has been removed by in-place expansion of
6338 -- an aggregate.
6340 if Ekind (E) = E_Constant
6341 and then Comes_From_Source (E)
6342 and then No (Constant_Value (E))
6343 and then Is_Frozen (Etype (E))
6344 and then not In_Spec_Expression
6345 and then not Is_Imported (E)
6346 then
6347 if No_Initialization (Parent (E))
6348 or else (Present (Full_View (E))
6349 and then No_Initialization (Parent (Full_View (E))))
6350 then
6351 null;
6352 else
6353 Error_Msg_N (
6354 "deferred constant is frozen before completion", N);
6355 end if;
6356 end if;
6358 Eval_Entity_Name (N);
6359 end if;
6360 end Resolve_Entity_Name;
6362 -------------------
6363 -- Resolve_Entry --
6364 -------------------
6366 procedure Resolve_Entry (Entry_Name : Node_Id) is
6367 Loc : constant Source_Ptr := Sloc (Entry_Name);
6368 Nam : Entity_Id;
6369 New_N : Node_Id;
6370 S : Entity_Id;
6371 Tsk : Entity_Id;
6372 E_Name : Node_Id;
6373 Index : Node_Id;
6375 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6376 -- If the bounds of the entry family being called depend on task
6377 -- discriminants, build a new index subtype where a discriminant is
6378 -- replaced with the value of the discriminant of the target task.
6379 -- The target task is the prefix of the entry name in the call.
6381 -----------------------
6382 -- Actual_Index_Type --
6383 -----------------------
6385 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6386 Typ : constant Entity_Id := Entry_Index_Type (E);
6387 Tsk : constant Entity_Id := Scope (E);
6388 Lo : constant Node_Id := Type_Low_Bound (Typ);
6389 Hi : constant Node_Id := Type_High_Bound (Typ);
6390 New_T : Entity_Id;
6392 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6393 -- If the bound is given by a discriminant, replace with a reference
6394 -- to the discriminant of the same name in the target task. If the
6395 -- entry name is the target of a requeue statement and the entry is
6396 -- in the current protected object, the bound to be used is the
6397 -- discriminal of the object (see Apply_Range_Checks for details of
6398 -- the transformation).
6400 -----------------------------
6401 -- Actual_Discriminant_Ref --
6402 -----------------------------
6404 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6405 Typ : constant Entity_Id := Etype (Bound);
6406 Ref : Node_Id;
6408 begin
6409 Remove_Side_Effects (Bound);
6411 if not Is_Entity_Name (Bound)
6412 or else Ekind (Entity (Bound)) /= E_Discriminant
6413 then
6414 return Bound;
6416 elsif Is_Protected_Type (Tsk)
6417 and then In_Open_Scopes (Tsk)
6418 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6419 then
6420 -- Note: here Bound denotes a discriminant of the corresponding
6421 -- record type tskV, whose discriminal is a formal of the
6422 -- init-proc tskVIP. What we want is the body discriminal,
6423 -- which is associated to the discriminant of the original
6424 -- concurrent type tsk.
6426 return New_Occurrence_Of
6427 (Find_Body_Discriminal (Entity (Bound)), Loc);
6429 else
6430 Ref :=
6431 Make_Selected_Component (Loc,
6432 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6433 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6434 Analyze (Ref);
6435 Resolve (Ref, Typ);
6436 return Ref;
6437 end if;
6438 end Actual_Discriminant_Ref;
6440 -- Start of processing for Actual_Index_Type
6442 begin
6443 if not Has_Discriminants (Tsk)
6444 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6445 then
6446 return Entry_Index_Type (E);
6448 else
6449 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6450 Set_Etype (New_T, Base_Type (Typ));
6451 Set_Size_Info (New_T, Typ);
6452 Set_RM_Size (New_T, RM_Size (Typ));
6453 Set_Scalar_Range (New_T,
6454 Make_Range (Sloc (Entry_Name),
6455 Low_Bound => Actual_Discriminant_Ref (Lo),
6456 High_Bound => Actual_Discriminant_Ref (Hi)));
6458 return New_T;
6459 end if;
6460 end Actual_Index_Type;
6462 -- Start of processing of Resolve_Entry
6464 begin
6465 -- Find name of entry being called, and resolve prefix of name with its
6466 -- own type. The prefix can be overloaded, and the name and signature of
6467 -- the entry must be taken into account.
6469 if Nkind (Entry_Name) = N_Indexed_Component then
6471 -- Case of dealing with entry family within the current tasks
6473 E_Name := Prefix (Entry_Name);
6475 else
6476 E_Name := Entry_Name;
6477 end if;
6479 if Is_Entity_Name (E_Name) then
6481 -- Entry call to an entry (or entry family) in the current task. This
6482 -- is legal even though the task will deadlock. Rewrite as call to
6483 -- current task.
6485 -- This can also be a call to an entry in an enclosing task. If this
6486 -- is a single task, we have to retrieve its name, because the scope
6487 -- of the entry is the task type, not the object. If the enclosing
6488 -- task is a task type, the identity of the task is given by its own
6489 -- self variable.
6491 -- Finally this can be a requeue on an entry of the same task or
6492 -- protected object.
6494 S := Scope (Entity (E_Name));
6496 for J in reverse 0 .. Scope_Stack.Last loop
6497 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6498 and then not Comes_From_Source (S)
6499 then
6500 -- S is an enclosing task or protected object. The concurrent
6501 -- declaration has been converted into a type declaration, and
6502 -- the object itself has an object declaration that follows
6503 -- the type in the same declarative part.
6505 Tsk := Next_Entity (S);
6506 while Etype (Tsk) /= S loop
6507 Next_Entity (Tsk);
6508 end loop;
6510 S := Tsk;
6511 exit;
6513 elsif S = Scope_Stack.Table (J).Entity then
6515 -- Call to current task. Will be transformed into call to Self
6517 exit;
6519 end if;
6520 end loop;
6522 New_N :=
6523 Make_Selected_Component (Loc,
6524 Prefix => New_Occurrence_Of (S, Loc),
6525 Selector_Name =>
6526 New_Occurrence_Of (Entity (E_Name), Loc));
6527 Rewrite (E_Name, New_N);
6528 Analyze (E_Name);
6530 elsif Nkind (Entry_Name) = N_Selected_Component
6531 and then Is_Overloaded (Prefix (Entry_Name))
6532 then
6533 -- Use the entry name (which must be unique at this point) to find
6534 -- the prefix that returns the corresponding task/protected type.
6536 declare
6537 Pref : constant Node_Id := Prefix (Entry_Name);
6538 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6539 I : Interp_Index;
6540 It : Interp;
6542 begin
6543 Get_First_Interp (Pref, I, It);
6544 while Present (It.Typ) loop
6545 if Scope (Ent) = It.Typ then
6546 Set_Etype (Pref, It.Typ);
6547 exit;
6548 end if;
6550 Get_Next_Interp (I, It);
6551 end loop;
6552 end;
6553 end if;
6555 if Nkind (Entry_Name) = N_Selected_Component then
6556 Resolve (Prefix (Entry_Name));
6558 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6559 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6560 Resolve (Prefix (Prefix (Entry_Name)));
6561 Index := First (Expressions (Entry_Name));
6562 Resolve (Index, Entry_Index_Type (Nam));
6564 -- Up to this point the expression could have been the actual in a
6565 -- simple entry call, and be given by a named association.
6567 if Nkind (Index) = N_Parameter_Association then
6568 Error_Msg_N ("expect expression for entry index", Index);
6569 else
6570 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6571 end if;
6572 end if;
6573 end Resolve_Entry;
6575 ------------------------
6576 -- Resolve_Entry_Call --
6577 ------------------------
6579 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6580 Entry_Name : constant Node_Id := Name (N);
6581 Loc : constant Source_Ptr := Sloc (Entry_Name);
6582 Actuals : List_Id;
6583 First_Named : Node_Id;
6584 Nam : Entity_Id;
6585 Norm_OK : Boolean;
6586 Obj : Node_Id;
6587 Was_Over : Boolean;
6589 begin
6590 -- We kill all checks here, because it does not seem worth the effort to
6591 -- do anything better, an entry call is a big operation.
6593 Kill_All_Checks;
6595 -- Processing of the name is similar for entry calls and protected
6596 -- operation calls. Once the entity is determined, we can complete
6597 -- the resolution of the actuals.
6599 -- The selector may be overloaded, in the case of a protected object
6600 -- with overloaded functions. The type of the context is used for
6601 -- resolution.
6603 if Nkind (Entry_Name) = N_Selected_Component
6604 and then Is_Overloaded (Selector_Name (Entry_Name))
6605 and then Typ /= Standard_Void_Type
6606 then
6607 declare
6608 I : Interp_Index;
6609 It : Interp;
6611 begin
6612 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6613 while Present (It.Typ) loop
6614 if Covers (Typ, It.Typ) then
6615 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6616 Set_Etype (Entry_Name, It.Typ);
6618 Generate_Reference (It.Typ, N, ' ');
6619 end if;
6621 Get_Next_Interp (I, It);
6622 end loop;
6623 end;
6624 end if;
6626 Resolve_Entry (Entry_Name);
6628 if Nkind (Entry_Name) = N_Selected_Component then
6630 -- Simple entry call
6632 Nam := Entity (Selector_Name (Entry_Name));
6633 Obj := Prefix (Entry_Name);
6634 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6636 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6638 -- Call to member of entry family
6640 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6641 Obj := Prefix (Prefix (Entry_Name));
6642 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6643 end if;
6645 -- We cannot in general check the maximum depth of protected entry calls
6646 -- at compile time. But we can tell that any protected entry call at all
6647 -- violates a specified nesting depth of zero.
6649 if Is_Protected_Type (Scope (Nam)) then
6650 Check_Restriction (Max_Entry_Queue_Length, N);
6651 end if;
6653 -- Use context type to disambiguate a protected function that can be
6654 -- called without actuals and that returns an array type, and where the
6655 -- argument list may be an indexing of the returned value.
6657 if Ekind (Nam) = E_Function
6658 and then Needs_No_Actuals (Nam)
6659 and then Present (Parameter_Associations (N))
6660 and then
6661 ((Is_Array_Type (Etype (Nam))
6662 and then Covers (Typ, Component_Type (Etype (Nam))))
6664 or else (Is_Access_Type (Etype (Nam))
6665 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6666 and then
6667 Covers
6668 (Typ,
6669 Component_Type (Designated_Type (Etype (Nam))))))
6670 then
6671 declare
6672 Index_Node : Node_Id;
6674 begin
6675 Index_Node :=
6676 Make_Indexed_Component (Loc,
6677 Prefix =>
6678 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6679 Expressions => Parameter_Associations (N));
6681 -- Since we are correcting a node classification error made by the
6682 -- parser, we call Replace rather than Rewrite.
6684 Replace (N, Index_Node);
6685 Set_Etype (Prefix (N), Etype (Nam));
6686 Set_Etype (N, Typ);
6687 Resolve_Indexed_Component (N, Typ);
6688 return;
6689 end;
6690 end if;
6692 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6693 and then Present (PPC_Wrapper (Nam))
6694 and then Current_Scope /= PPC_Wrapper (Nam)
6695 then
6696 -- Rewrite as call to the precondition wrapper, adding the task
6697 -- object to the list of actuals. If the call is to a member of an
6698 -- entry family, include the index as well.
6700 declare
6701 New_Call : Node_Id;
6702 New_Actuals : List_Id;
6704 begin
6705 New_Actuals := New_List (Obj);
6707 if Nkind (Entry_Name) = N_Indexed_Component then
6708 Append_To (New_Actuals,
6709 New_Copy_Tree (First (Expressions (Entry_Name))));
6710 end if;
6712 Append_List (Parameter_Associations (N), New_Actuals);
6713 New_Call :=
6714 Make_Procedure_Call_Statement (Loc,
6715 Name =>
6716 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6717 Parameter_Associations => New_Actuals);
6718 Rewrite (N, New_Call);
6719 Analyze_And_Resolve (N);
6720 return;
6721 end;
6722 end if;
6724 -- The operation name may have been overloaded. Order the actuals
6725 -- according to the formals of the resolved entity, and set the return
6726 -- type to that of the operation.
6728 if Was_Over then
6729 Normalize_Actuals (N, Nam, False, Norm_OK);
6730 pragma Assert (Norm_OK);
6731 Set_Etype (N, Etype (Nam));
6732 end if;
6734 Resolve_Actuals (N, Nam);
6736 -- Create a call reference to the entry
6738 Generate_Reference (Nam, Entry_Name, 's');
6740 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6741 Check_Potentially_Blocking_Operation (N);
6742 end if;
6744 -- Verify that a procedure call cannot masquerade as an entry
6745 -- call where an entry call is expected.
6747 if Ekind (Nam) = E_Procedure then
6748 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6749 and then N = Entry_Call_Statement (Parent (N))
6750 then
6751 Error_Msg_N ("entry call required in select statement", N);
6753 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6754 and then N = Triggering_Statement (Parent (N))
6755 then
6756 Error_Msg_N ("triggering statement cannot be procedure call", N);
6758 elsif Ekind (Scope (Nam)) = E_Task_Type
6759 and then not In_Open_Scopes (Scope (Nam))
6760 then
6761 Error_Msg_N ("task has no entry with this name", Entry_Name);
6762 end if;
6763 end if;
6765 -- After resolution, entry calls and protected procedure calls are
6766 -- changed into entry calls, for expansion. The structure of the node
6767 -- does not change, so it can safely be done in place. Protected
6768 -- function calls must keep their structure because they are
6769 -- subexpressions.
6771 if Ekind (Nam) /= E_Function then
6773 -- A protected operation that is not a function may modify the
6774 -- corresponding object, and cannot apply to a constant. If this
6775 -- is an internal call, the prefix is the type itself.
6777 if Is_Protected_Type (Scope (Nam))
6778 and then not Is_Variable (Obj)
6779 and then (not Is_Entity_Name (Obj)
6780 or else not Is_Type (Entity (Obj)))
6781 then
6782 Error_Msg_N
6783 ("prefix of protected procedure or entry call must be variable",
6784 Entry_Name);
6785 end if;
6787 Actuals := Parameter_Associations (N);
6788 First_Named := First_Named_Actual (N);
6790 Rewrite (N,
6791 Make_Entry_Call_Statement (Loc,
6792 Name => Entry_Name,
6793 Parameter_Associations => Actuals));
6795 Set_First_Named_Actual (N, First_Named);
6796 Set_Analyzed (N, True);
6798 -- Protected functions can return on the secondary stack, in which
6799 -- case we must trigger the transient scope mechanism.
6801 elsif Full_Expander_Active
6802 and then Requires_Transient_Scope (Etype (Nam))
6803 then
6804 Establish_Transient_Scope (N, Sec_Stack => True);
6805 end if;
6806 end Resolve_Entry_Call;
6808 -------------------------
6809 -- Resolve_Equality_Op --
6810 -------------------------
6812 -- Both arguments must have the same type, and the boolean context does
6813 -- not participate in the resolution. The first pass verifies that the
6814 -- interpretation is not ambiguous, and the type of the left argument is
6815 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6816 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6817 -- though they carry a single (universal) type. Diagnose this case here.
6819 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6820 L : constant Node_Id := Left_Opnd (N);
6821 R : constant Node_Id := Right_Opnd (N);
6822 T : Entity_Id := Find_Unique_Type (L, R);
6824 procedure Check_Conditional_Expression (Cond : Node_Id);
6825 -- The resolution rule for conditional expressions requires that each
6826 -- such must have a unique type. This means that if several dependent
6827 -- expressions are of a non-null anonymous access type, and the context
6828 -- does not impose an expected type (as can be the case in an equality
6829 -- operation) the expression must be rejected.
6831 function Find_Unique_Access_Type return Entity_Id;
6832 -- In the case of allocators, make a last-ditch attempt to find a single
6833 -- access type with the right designated type. This is semantically
6834 -- dubious, and of no interest to any real code, but c48008a makes it
6835 -- all worthwhile.
6837 ----------------------------------
6838 -- Check_Conditional_Expression --
6839 ----------------------------------
6841 procedure Check_Conditional_Expression (Cond : Node_Id) is
6842 Then_Expr : Node_Id;
6843 Else_Expr : Node_Id;
6845 begin
6846 if Nkind (Cond) = N_Conditional_Expression then
6847 Then_Expr := Next (First (Expressions (Cond)));
6848 Else_Expr := Next (Then_Expr);
6850 if Nkind (Then_Expr) /= N_Null
6851 and then Nkind (Else_Expr) /= N_Null
6852 then
6853 Error_Msg_N
6854 ("cannot determine type of conditional expression", Cond);
6855 end if;
6856 end if;
6857 end Check_Conditional_Expression;
6859 -----------------------------
6860 -- Find_Unique_Access_Type --
6861 -----------------------------
6863 function Find_Unique_Access_Type return Entity_Id is
6864 Acc : Entity_Id;
6865 E : Entity_Id;
6866 S : Entity_Id;
6868 begin
6869 if Ekind (Etype (R)) = E_Allocator_Type then
6870 Acc := Designated_Type (Etype (R));
6871 elsif Ekind (Etype (L)) = E_Allocator_Type then
6872 Acc := Designated_Type (Etype (L));
6873 else
6874 return Empty;
6875 end if;
6877 S := Current_Scope;
6878 while S /= Standard_Standard loop
6879 E := First_Entity (S);
6880 while Present (E) loop
6881 if Is_Type (E)
6882 and then Is_Access_Type (E)
6883 and then Ekind (E) /= E_Allocator_Type
6884 and then Designated_Type (E) = Base_Type (Acc)
6885 then
6886 return E;
6887 end if;
6889 Next_Entity (E);
6890 end loop;
6892 S := Scope (S);
6893 end loop;
6895 return Empty;
6896 end Find_Unique_Access_Type;
6898 -- Start of processing for Resolve_Equality_Op
6900 begin
6901 Set_Etype (N, Base_Type (Typ));
6902 Generate_Reference (T, N, ' ');
6904 if T = Any_Fixed then
6905 T := Unique_Fixed_Point_Type (L);
6906 end if;
6908 if T /= Any_Type then
6909 if T = Any_String or else
6910 T = Any_Composite or else
6911 T = Any_Character
6912 then
6913 if T = Any_Character then
6914 Ambiguous_Character (L);
6915 else
6916 Error_Msg_N ("ambiguous operands for equality", N);
6917 end if;
6919 Set_Etype (N, Any_Type);
6920 return;
6922 elsif T = Any_Access
6923 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6924 then
6925 T := Find_Unique_Access_Type;
6927 if No (T) then
6928 Error_Msg_N ("ambiguous operands for equality", N);
6929 Set_Etype (N, Any_Type);
6930 return;
6931 end if;
6933 -- Conditional expressions must have a single type, and if the
6934 -- context does not impose one the dependent expressions cannot
6935 -- be anonymous access types.
6937 elsif Ada_Version >= Ada_2012
6938 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
6939 E_Anonymous_Access_Subprogram_Type)
6940 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
6941 E_Anonymous_Access_Subprogram_Type)
6942 then
6943 Check_Conditional_Expression (L);
6944 Check_Conditional_Expression (R);
6945 end if;
6947 Resolve (L, T);
6948 Resolve (R, T);
6950 -- In SPARK, equality operators = and /= for array types other than
6951 -- String are only defined when, for each index position, the
6952 -- operands have equal static bounds.
6954 if Is_Array_Type (T) then
6955 -- Protect call to Matching_Static_Array_Bounds to avoid costly
6956 -- operation if not needed.
6958 if Restriction_Check_Required (SPARK)
6959 and then Base_Type (T) /= Standard_String
6960 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6961 and then Etype (L) /= Any_Composite -- or else L in error
6962 and then Etype (R) /= Any_Composite -- or else R in error
6963 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
6964 then
6965 Check_SPARK_Restriction
6966 ("array types should have matching static bounds", N);
6967 end if;
6968 end if;
6970 -- If the unique type is a class-wide type then it will be expanded
6971 -- into a dispatching call to the predefined primitive. Therefore we
6972 -- check here for potential violation of such restriction.
6974 if Is_Class_Wide_Type (T) then
6975 Check_Restriction (No_Dispatching_Calls, N);
6976 end if;
6978 if Warn_On_Redundant_Constructs
6979 and then Comes_From_Source (N)
6980 and then Is_Entity_Name (R)
6981 and then Entity (R) = Standard_True
6982 and then Comes_From_Source (R)
6983 then
6984 Error_Msg_N -- CODEFIX
6985 ("?comparison with True is redundant!", R);
6986 end if;
6988 Check_Unset_Reference (L);
6989 Check_Unset_Reference (R);
6990 Generate_Operator_Reference (N, T);
6991 Check_Low_Bound_Tested (N);
6993 -- If this is an inequality, it may be the implicit inequality
6994 -- created for a user-defined operation, in which case the corres-
6995 -- ponding equality operation is not intrinsic, and the operation
6996 -- cannot be constant-folded. Else fold.
6998 if Nkind (N) = N_Op_Eq
6999 or else Comes_From_Source (Entity (N))
7000 or else Ekind (Entity (N)) = E_Operator
7001 or else Is_Intrinsic_Subprogram
7002 (Corresponding_Equality (Entity (N)))
7003 then
7004 Analyze_Dimension (N);
7005 Eval_Relational_Op (N);
7007 elsif Nkind (N) = N_Op_Ne
7008 and then Is_Abstract_Subprogram (Entity (N))
7009 then
7010 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7011 end if;
7013 -- Ada 2005: If one operand is an anonymous access type, convert the
7014 -- other operand to it, to ensure that the underlying types match in
7015 -- the back-end. Same for access_to_subprogram, and the conversion
7016 -- verifies that the types are subtype conformant.
7018 -- We apply the same conversion in the case one of the operands is a
7019 -- private subtype of the type of the other.
7021 -- Why the Expander_Active test here ???
7023 if Full_Expander_Active
7024 and then
7025 (Ekind_In (T, E_Anonymous_Access_Type,
7026 E_Anonymous_Access_Subprogram_Type)
7027 or else Is_Private_Type (T))
7028 then
7029 if Etype (L) /= T then
7030 Rewrite (L,
7031 Make_Unchecked_Type_Conversion (Sloc (L),
7032 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7033 Expression => Relocate_Node (L)));
7034 Analyze_And_Resolve (L, T);
7035 end if;
7037 if (Etype (R)) /= T then
7038 Rewrite (R,
7039 Make_Unchecked_Type_Conversion (Sloc (R),
7040 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7041 Expression => Relocate_Node (R)));
7042 Analyze_And_Resolve (R, T);
7043 end if;
7044 end if;
7045 end if;
7046 end Resolve_Equality_Op;
7048 ----------------------------------
7049 -- Resolve_Explicit_Dereference --
7050 ----------------------------------
7052 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
7053 Loc : constant Source_Ptr := Sloc (N);
7054 New_N : Node_Id;
7055 P : constant Node_Id := Prefix (N);
7057 P_Typ : Entity_Id;
7058 -- The candidate prefix type, if overloaded
7060 I : Interp_Index;
7061 It : Interp;
7063 begin
7064 Check_Fully_Declared_Prefix (Typ, P);
7065 P_Typ := Empty;
7067 if Is_Overloaded (P) then
7069 -- Use the context type to select the prefix that has the correct
7070 -- designated type. Keep the first match, which will be the inner-
7071 -- most.
7073 Get_First_Interp (P, I, It);
7075 while Present (It.Typ) loop
7076 if Is_Access_Type (It.Typ)
7077 and then Covers (Typ, Designated_Type (It.Typ))
7078 then
7079 if No (P_Typ) then
7080 P_Typ := It.Typ;
7081 end if;
7083 -- Remove access types that do not match, but preserve access
7084 -- to subprogram interpretations, in case a further dereference
7085 -- is needed (see below).
7087 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
7088 Remove_Interp (I);
7089 end if;
7091 Get_Next_Interp (I, It);
7092 end loop;
7094 if Present (P_Typ) then
7095 Resolve (P, P_Typ);
7096 Set_Etype (N, Designated_Type (P_Typ));
7098 else
7099 -- If no interpretation covers the designated type of the prefix,
7100 -- this is the pathological case where not all implementations of
7101 -- the prefix allow the interpretation of the node as a call. Now
7102 -- that the expected type is known, Remove other interpretations
7103 -- from prefix, rewrite it as a call, and resolve again, so that
7104 -- the proper call node is generated.
7106 Get_First_Interp (P, I, It);
7107 while Present (It.Typ) loop
7108 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
7109 Remove_Interp (I);
7110 end if;
7112 Get_Next_Interp (I, It);
7113 end loop;
7115 New_N :=
7116 Make_Function_Call (Loc,
7117 Name =>
7118 Make_Explicit_Dereference (Loc,
7119 Prefix => P),
7120 Parameter_Associations => New_List);
7122 Save_Interps (N, New_N);
7123 Rewrite (N, New_N);
7124 Analyze_And_Resolve (N, Typ);
7125 return;
7126 end if;
7128 -- If not overloaded, resolve P with its own type
7130 else
7131 Resolve (P);
7132 end if;
7134 if Is_Access_Type (Etype (P)) then
7135 Apply_Access_Check (N);
7136 end if;
7138 -- If the designated type is a packed unconstrained array type, and the
7139 -- explicit dereference is not in the context of an attribute reference,
7140 -- then we must compute and set the actual subtype, since it is needed
7141 -- by Gigi. The reason we exclude the attribute case is that this is
7142 -- handled fine by Gigi, and in fact we use such attributes to build the
7143 -- actual subtype. We also exclude generated code (which builds actual
7144 -- subtypes directly if they are needed).
7146 if Is_Array_Type (Etype (N))
7147 and then Is_Packed (Etype (N))
7148 and then not Is_Constrained (Etype (N))
7149 and then Nkind (Parent (N)) /= N_Attribute_Reference
7150 and then Comes_From_Source (N)
7151 then
7152 Set_Etype (N, Get_Actual_Subtype (N));
7153 end if;
7155 -- Note: No Eval processing is required for an explicit dereference,
7156 -- because such a name can never be static.
7158 end Resolve_Explicit_Dereference;
7160 -------------------------------------
7161 -- Resolve_Expression_With_Actions --
7162 -------------------------------------
7164 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7165 begin
7166 Set_Etype (N, Typ);
7167 end Resolve_Expression_With_Actions;
7169 -------------------------------
7170 -- Resolve_Indexed_Component --
7171 -------------------------------
7173 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7174 Name : constant Node_Id := Prefix (N);
7175 Expr : Node_Id;
7176 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7177 Index : Node_Id;
7179 begin
7180 if Is_Overloaded (Name) then
7182 -- Use the context type to select the prefix that yields the correct
7183 -- component type.
7185 declare
7186 I : Interp_Index;
7187 It : Interp;
7188 I1 : Interp_Index := 0;
7189 P : constant Node_Id := Prefix (N);
7190 Found : Boolean := False;
7192 begin
7193 Get_First_Interp (P, I, It);
7194 while Present (It.Typ) loop
7195 if (Is_Array_Type (It.Typ)
7196 and then Covers (Typ, Component_Type (It.Typ)))
7197 or else (Is_Access_Type (It.Typ)
7198 and then Is_Array_Type (Designated_Type (It.Typ))
7199 and then
7200 Covers
7201 (Typ,
7202 Component_Type (Designated_Type (It.Typ))))
7203 then
7204 if Found then
7205 It := Disambiguate (P, I1, I, Any_Type);
7207 if It = No_Interp then
7208 Error_Msg_N ("ambiguous prefix for indexing", N);
7209 Set_Etype (N, Typ);
7210 return;
7212 else
7213 Found := True;
7214 Array_Type := It.Typ;
7215 I1 := I;
7216 end if;
7218 else
7219 Found := True;
7220 Array_Type := It.Typ;
7221 I1 := I;
7222 end if;
7223 end if;
7225 Get_Next_Interp (I, It);
7226 end loop;
7227 end;
7229 else
7230 Array_Type := Etype (Name);
7231 end if;
7233 Resolve (Name, Array_Type);
7234 Array_Type := Get_Actual_Subtype_If_Available (Name);
7236 -- If prefix is access type, dereference to get real array type.
7237 -- Note: we do not apply an access check because the expander always
7238 -- introduces an explicit dereference, and the check will happen there.
7240 if Is_Access_Type (Array_Type) then
7241 Array_Type := Designated_Type (Array_Type);
7242 end if;
7244 -- If name was overloaded, set component type correctly now
7245 -- If a misplaced call to an entry family (which has no index types)
7246 -- return. Error will be diagnosed from calling context.
7248 if Is_Array_Type (Array_Type) then
7249 Set_Etype (N, Component_Type (Array_Type));
7250 else
7251 return;
7252 end if;
7254 Index := First_Index (Array_Type);
7255 Expr := First (Expressions (N));
7257 -- The prefix may have resolved to a string literal, in which case its
7258 -- etype has a special representation. This is only possible currently
7259 -- if the prefix is a static concatenation, written in functional
7260 -- notation.
7262 if Ekind (Array_Type) = E_String_Literal_Subtype then
7263 Resolve (Expr, Standard_Positive);
7265 else
7266 while Present (Index) and Present (Expr) loop
7267 Resolve (Expr, Etype (Index));
7268 Check_Unset_Reference (Expr);
7270 if Is_Scalar_Type (Etype (Expr)) then
7271 Apply_Scalar_Range_Check (Expr, Etype (Index));
7272 else
7273 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7274 end if;
7276 Next_Index (Index);
7277 Next (Expr);
7278 end loop;
7279 end if;
7281 Analyze_Dimension (N);
7283 -- Do not generate the warning on suspicious index if we are analyzing
7284 -- package Ada.Tags; otherwise we will report the warning with the
7285 -- Prims_Ptr field of the dispatch table.
7287 if Scope (Etype (Prefix (N))) = Standard_Standard
7288 or else not
7289 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7290 Ada_Tags)
7291 then
7292 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7293 Eval_Indexed_Component (N);
7294 end if;
7296 -- If the array type is atomic, and is packed, and we are in a left side
7297 -- context, then this is worth a warning, since we have a situation
7298 -- where the access to the component may cause extra read/writes of
7299 -- the atomic array object, which could be considered unexpected.
7301 if Nkind (N) = N_Indexed_Component
7302 and then (Is_Atomic (Array_Type)
7303 or else (Is_Entity_Name (Prefix (N))
7304 and then Is_Atomic (Entity (Prefix (N)))))
7305 and then Is_Bit_Packed_Array (Array_Type)
7306 and then Is_LHS (N)
7307 then
7308 Error_Msg_N ("?assignment to component of packed atomic array",
7309 Prefix (N));
7310 Error_Msg_N ("?\may cause unexpected accesses to atomic object",
7311 Prefix (N));
7312 end if;
7313 end Resolve_Indexed_Component;
7315 -----------------------------
7316 -- Resolve_Integer_Literal --
7317 -----------------------------
7319 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7320 begin
7321 Set_Etype (N, Typ);
7322 Eval_Integer_Literal (N);
7323 end Resolve_Integer_Literal;
7325 --------------------------------
7326 -- Resolve_Intrinsic_Operator --
7327 --------------------------------
7329 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7330 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7331 Op : Entity_Id;
7332 Orig_Op : constant Entity_Id := Entity (N);
7333 Arg1 : Node_Id;
7334 Arg2 : Node_Id;
7336 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7337 -- If the operand is a literal, it cannot be the expression in a
7338 -- conversion. Use a qualified expression instead.
7340 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7341 Loc : constant Source_Ptr := Sloc (Opnd);
7342 Res : Node_Id;
7343 begin
7344 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7345 Res :=
7346 Make_Qualified_Expression (Loc,
7347 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7348 Expression => Relocate_Node (Opnd));
7349 Analyze (Res);
7351 else
7352 Res := Unchecked_Convert_To (Btyp, Opnd);
7353 end if;
7355 return Res;
7356 end Convert_Operand;
7358 -- Start of processing for Resolve_Intrinsic_Operator
7360 begin
7361 -- We must preserve the original entity in a generic setting, so that
7362 -- the legality of the operation can be verified in an instance.
7364 if not Full_Expander_Active then
7365 return;
7366 end if;
7368 Op := Entity (N);
7369 while Scope (Op) /= Standard_Standard loop
7370 Op := Homonym (Op);
7371 pragma Assert (Present (Op));
7372 end loop;
7374 Set_Entity (N, Op);
7375 Set_Is_Overloaded (N, False);
7377 -- If the result or operand types are private, rewrite with unchecked
7378 -- conversions on the operands and the result, to expose the proper
7379 -- underlying numeric type.
7381 if Is_Private_Type (Typ)
7382 or else Is_Private_Type (Etype (Left_Opnd (N)))
7383 or else Is_Private_Type (Etype (Right_Opnd (N)))
7384 then
7385 Arg1 := Convert_Operand (Left_Opnd (N));
7386 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7387 -- What on earth is this commented out fragment of code???
7389 if Nkind (N) = N_Op_Expon then
7390 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7391 else
7392 Arg2 := Convert_Operand (Right_Opnd (N));
7393 end if;
7395 if Nkind (Arg1) = N_Type_Conversion then
7396 Save_Interps (Left_Opnd (N), Expression (Arg1));
7397 end if;
7399 if Nkind (Arg2) = N_Type_Conversion then
7400 Save_Interps (Right_Opnd (N), Expression (Arg2));
7401 end if;
7403 Set_Left_Opnd (N, Arg1);
7404 Set_Right_Opnd (N, Arg2);
7406 Set_Etype (N, Btyp);
7407 Rewrite (N, Unchecked_Convert_To (Typ, N));
7408 Resolve (N, Typ);
7410 elsif Typ /= Etype (Left_Opnd (N))
7411 or else Typ /= Etype (Right_Opnd (N))
7412 then
7413 -- Add explicit conversion where needed, and save interpretations in
7414 -- case operands are overloaded. If the context is a VMS operation,
7415 -- assert that the conversion is legal (the operands have the proper
7416 -- types to select the VMS intrinsic). Note that in rare cases the
7417 -- VMS operators may be visible, but the default System is being used
7418 -- and Address is a private type.
7420 Arg1 := Convert_To (Typ, Left_Opnd (N));
7421 Arg2 := Convert_To (Typ, Right_Opnd (N));
7423 if Nkind (Arg1) = N_Type_Conversion then
7424 Save_Interps (Left_Opnd (N), Expression (Arg1));
7426 if Is_VMS_Operator (Orig_Op) then
7427 Set_Conversion_OK (Arg1);
7428 end if;
7429 else
7430 Save_Interps (Left_Opnd (N), Arg1);
7431 end if;
7433 if Nkind (Arg2) = N_Type_Conversion then
7434 Save_Interps (Right_Opnd (N), Expression (Arg2));
7436 if Is_VMS_Operator (Orig_Op) then
7437 Set_Conversion_OK (Arg2);
7438 end if;
7439 else
7440 Save_Interps (Right_Opnd (N), Arg2);
7441 end if;
7443 Rewrite (Left_Opnd (N), Arg1);
7444 Rewrite (Right_Opnd (N), Arg2);
7445 Analyze (Arg1);
7446 Analyze (Arg2);
7447 Resolve_Arithmetic_Op (N, Typ);
7449 else
7450 Resolve_Arithmetic_Op (N, Typ);
7451 end if;
7452 end Resolve_Intrinsic_Operator;
7454 --------------------------------------
7455 -- Resolve_Intrinsic_Unary_Operator --
7456 --------------------------------------
7458 procedure Resolve_Intrinsic_Unary_Operator
7459 (N : Node_Id;
7460 Typ : Entity_Id)
7462 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7463 Op : Entity_Id;
7464 Arg2 : Node_Id;
7466 begin
7467 Op := Entity (N);
7468 while Scope (Op) /= Standard_Standard loop
7469 Op := Homonym (Op);
7470 pragma Assert (Present (Op));
7471 end loop;
7473 Set_Entity (N, Op);
7475 if Is_Private_Type (Typ) then
7476 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7477 Save_Interps (Right_Opnd (N), Expression (Arg2));
7479 Set_Right_Opnd (N, Arg2);
7481 Set_Etype (N, Btyp);
7482 Rewrite (N, Unchecked_Convert_To (Typ, N));
7483 Resolve (N, Typ);
7485 else
7486 Resolve_Unary_Op (N, Typ);
7487 end if;
7488 end Resolve_Intrinsic_Unary_Operator;
7490 ------------------------
7491 -- Resolve_Logical_Op --
7492 ------------------------
7494 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7495 B_Typ : Entity_Id;
7497 begin
7498 Check_No_Direct_Boolean_Operators (N);
7500 -- Predefined operations on scalar types yield the base type. On the
7501 -- other hand, logical operations on arrays yield the type of the
7502 -- arguments (and the context).
7504 if Is_Array_Type (Typ) then
7505 B_Typ := Typ;
7506 else
7507 B_Typ := Base_Type (Typ);
7508 end if;
7510 -- OK if this is a VMS-specific intrinsic operation
7512 if Is_VMS_Operator (Entity (N)) then
7513 null;
7515 -- The following test is required because the operands of the operation
7516 -- may be literals, in which case the resulting type appears to be
7517 -- compatible with a signed integer type, when in fact it is compatible
7518 -- only with modular types. If the context itself is universal, the
7519 -- operation is illegal.
7521 elsif not Valid_Boolean_Arg (Typ) then
7522 Error_Msg_N ("invalid context for logical operation", N);
7523 Set_Etype (N, Any_Type);
7524 return;
7526 elsif Typ = Any_Modular then
7527 Error_Msg_N
7528 ("no modular type available in this context", N);
7529 Set_Etype (N, Any_Type);
7530 return;
7532 elsif Is_Modular_Integer_Type (Typ)
7533 and then Etype (Left_Opnd (N)) = Universal_Integer
7534 and then Etype (Right_Opnd (N)) = Universal_Integer
7535 then
7536 Check_For_Visible_Operator (N, B_Typ);
7537 end if;
7539 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
7540 -- is active and the result type is standard Boolean (do not mess with
7541 -- ops that return a nonstandard Boolean type, because something strange
7542 -- is going on).
7544 -- Note: you might expect this replacement to be done during expansion,
7545 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
7546 -- is used, no part of the right operand of an "and" or "or" operator
7547 -- should be executed if the left operand would short-circuit the
7548 -- evaluation of the corresponding "and then" or "or else". If we left
7549 -- the replacement to expansion time, then run-time checks associated
7550 -- with such operands would be evaluated unconditionally, due to being
7551 -- before the condition prior to the rewriting as short-circuit forms
7552 -- during expansion.
7554 if Short_Circuit_And_Or
7555 and then B_Typ = Standard_Boolean
7556 and then Nkind_In (N, N_Op_And, N_Op_Or)
7557 then
7558 if Nkind (N) = N_Op_And then
7559 Rewrite (N,
7560 Make_And_Then (Sloc (N),
7561 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7562 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7563 Analyze_And_Resolve (N, B_Typ);
7565 -- Case of OR changed to OR ELSE
7567 else
7568 Rewrite (N,
7569 Make_Or_Else (Sloc (N),
7570 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7571 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7572 Analyze_And_Resolve (N, B_Typ);
7573 end if;
7575 -- Return now, since analysis of the rewritten ops will take care of
7576 -- other reference bookkeeping and expression folding.
7578 return;
7579 end if;
7581 Resolve (Left_Opnd (N), B_Typ);
7582 Resolve (Right_Opnd (N), B_Typ);
7584 Check_Unset_Reference (Left_Opnd (N));
7585 Check_Unset_Reference (Right_Opnd (N));
7587 Set_Etype (N, B_Typ);
7588 Generate_Operator_Reference (N, B_Typ);
7589 Eval_Logical_Op (N);
7591 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7592 -- only when both operands have same static lower and higher bounds. Of
7593 -- course the types have to match, so only check if operands are
7594 -- compatible and the node itself has no errors.
7596 if Is_Array_Type (B_Typ)
7597 and then Nkind (N) in N_Binary_Op
7598 then
7599 declare
7600 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7601 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7603 begin
7604 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7605 -- operation if not needed.
7607 if Restriction_Check_Required (SPARK)
7608 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7609 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7610 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7611 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7612 then
7613 Check_SPARK_Restriction
7614 ("array types should have matching static bounds", N);
7615 end if;
7616 end;
7617 end if;
7618 end Resolve_Logical_Op;
7620 ---------------------------
7621 -- Resolve_Membership_Op --
7622 ---------------------------
7624 -- The context can only be a boolean type, and does not determine the
7625 -- arguments. Arguments should be unambiguous, but the preference rule for
7626 -- universal types applies.
7628 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7629 pragma Warnings (Off, Typ);
7631 L : constant Node_Id := Left_Opnd (N);
7632 R : constant Node_Id := Right_Opnd (N);
7633 T : Entity_Id;
7635 procedure Resolve_Set_Membership;
7636 -- Analysis has determined a unique type for the left operand. Use it to
7637 -- resolve the disjuncts.
7639 ----------------------------
7640 -- Resolve_Set_Membership --
7641 ----------------------------
7643 procedure Resolve_Set_Membership is
7644 Alt : Node_Id;
7646 begin
7647 Resolve (L, Etype (L));
7649 Alt := First (Alternatives (N));
7650 while Present (Alt) loop
7652 -- Alternative is an expression, a range
7653 -- or a subtype mark.
7655 if not Is_Entity_Name (Alt)
7656 or else not Is_Type (Entity (Alt))
7657 then
7658 Resolve (Alt, Etype (L));
7659 end if;
7661 Next (Alt);
7662 end loop;
7663 end Resolve_Set_Membership;
7665 -- Start of processing for Resolve_Membership_Op
7667 begin
7668 if L = Error or else R = Error then
7669 return;
7670 end if;
7672 if Present (Alternatives (N)) then
7673 Resolve_Set_Membership;
7674 return;
7676 elsif not Is_Overloaded (R)
7677 and then
7678 (Etype (R) = Universal_Integer
7679 or else
7680 Etype (R) = Universal_Real)
7681 and then Is_Overloaded (L)
7682 then
7683 T := Etype (R);
7685 -- Ada 2005 (AI-251): Support the following case:
7687 -- type I is interface;
7688 -- type T is tagged ...
7690 -- function Test (O : I'Class) is
7691 -- begin
7692 -- return O in T'Class.
7693 -- end Test;
7695 -- In this case we have nothing else to do. The membership test will be
7696 -- done at run time.
7698 elsif Ada_Version >= Ada_2005
7699 and then Is_Class_Wide_Type (Etype (L))
7700 and then Is_Interface (Etype (L))
7701 and then Is_Class_Wide_Type (Etype (R))
7702 and then not Is_Interface (Etype (R))
7703 then
7704 return;
7705 else
7706 T := Intersect_Types (L, R);
7707 end if;
7709 -- If mixed-mode operations are present and operands are all literal,
7710 -- the only interpretation involves Duration, which is probably not
7711 -- the intention of the programmer.
7713 if T = Any_Fixed then
7714 T := Unique_Fixed_Point_Type (N);
7716 if T = Any_Type then
7717 return;
7718 end if;
7719 end if;
7721 Resolve (L, T);
7722 Check_Unset_Reference (L);
7724 if Nkind (R) = N_Range
7725 and then not Is_Scalar_Type (T)
7726 then
7727 Error_Msg_N ("scalar type required for range", R);
7728 end if;
7730 if Is_Entity_Name (R) then
7731 Freeze_Expression (R);
7732 else
7733 Resolve (R, T);
7734 Check_Unset_Reference (R);
7735 end if;
7737 Eval_Membership_Op (N);
7738 end Resolve_Membership_Op;
7740 ------------------
7741 -- Resolve_Null --
7742 ------------------
7744 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7745 Loc : constant Source_Ptr := Sloc (N);
7747 begin
7748 -- Handle restriction against anonymous null access values This
7749 -- restriction can be turned off using -gnatdj.
7751 -- Ada 2005 (AI-231): Remove restriction
7753 if Ada_Version < Ada_2005
7754 and then not Debug_Flag_J
7755 and then Ekind (Typ) = E_Anonymous_Access_Type
7756 and then Comes_From_Source (N)
7757 then
7758 -- In the common case of a call which uses an explicitly null value
7759 -- for an access parameter, give specialized error message.
7761 if Nkind (Parent (N)) in N_Subprogram_Call then
7762 Error_Msg_N
7763 ("null is not allowed as argument for an access parameter", N);
7765 -- Standard message for all other cases (are there any?)
7767 else
7768 Error_Msg_N
7769 ("null cannot be of an anonymous access type", N);
7770 end if;
7771 end if;
7773 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7774 -- assignment to a null-excluding object
7776 if Ada_Version >= Ada_2005
7777 and then Can_Never_Be_Null (Typ)
7778 and then Nkind (Parent (N)) = N_Assignment_Statement
7779 then
7780 if not Inside_Init_Proc then
7781 Insert_Action
7782 (Compile_Time_Constraint_Error (N,
7783 "(Ada 2005) null not allowed in null-excluding objects?"),
7784 Make_Raise_Constraint_Error (Loc,
7785 Reason => CE_Access_Check_Failed));
7786 else
7787 Insert_Action (N,
7788 Make_Raise_Constraint_Error (Loc,
7789 Reason => CE_Access_Check_Failed));
7790 end if;
7791 end if;
7793 -- In a distributed context, null for a remote access to subprogram may
7794 -- need to be replaced with a special record aggregate. In this case,
7795 -- return after having done the transformation.
7797 if (Ekind (Typ) = E_Record_Type
7798 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7799 and then Remote_AST_Null_Value (N, Typ)
7800 then
7801 return;
7802 end if;
7804 -- The null literal takes its type from the context
7806 Set_Etype (N, Typ);
7807 end Resolve_Null;
7809 -----------------------
7810 -- Resolve_Op_Concat --
7811 -----------------------
7813 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7815 -- We wish to avoid deep recursion, because concatenations are often
7816 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7817 -- operands nonrecursively until we find something that is not a simple
7818 -- concatenation (A in this case). We resolve that, and then walk back
7819 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7820 -- to do the rest of the work at each level. The Parent pointers allow
7821 -- us to avoid recursion, and thus avoid running out of memory. See also
7822 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7824 NN : Node_Id := N;
7825 Op1 : Node_Id;
7827 begin
7828 -- The following code is equivalent to:
7830 -- Resolve_Op_Concat_First (NN, Typ);
7831 -- Resolve_Op_Concat_Arg (N, ...);
7832 -- Resolve_Op_Concat_Rest (N, Typ);
7834 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
7835 -- operand is a concatenation.
7837 -- Walk down left operands
7839 loop
7840 Resolve_Op_Concat_First (NN, Typ);
7841 Op1 := Left_Opnd (NN);
7842 exit when not (Nkind (Op1) = N_Op_Concat
7843 and then not Is_Array_Type (Component_Type (Typ))
7844 and then Entity (Op1) = Entity (NN));
7845 NN := Op1;
7846 end loop;
7848 -- Now (given the above example) NN is A&B and Op1 is A
7850 -- First resolve Op1 ...
7852 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
7854 -- ... then walk NN back up until we reach N (where we started), calling
7855 -- Resolve_Op_Concat_Rest along the way.
7857 loop
7858 Resolve_Op_Concat_Rest (NN, Typ);
7859 exit when NN = N;
7860 NN := Parent (NN);
7861 end loop;
7863 if Base_Type (Etype (N)) /= Standard_String then
7864 Check_SPARK_Restriction
7865 ("result of concatenation should have type String", N);
7866 end if;
7867 end Resolve_Op_Concat;
7869 ---------------------------
7870 -- Resolve_Op_Concat_Arg --
7871 ---------------------------
7873 procedure Resolve_Op_Concat_Arg
7874 (N : Node_Id;
7875 Arg : Node_Id;
7876 Typ : Entity_Id;
7877 Is_Comp : Boolean)
7879 Btyp : constant Entity_Id := Base_Type (Typ);
7880 Ctyp : constant Entity_Id := Component_Type (Typ);
7882 begin
7883 if In_Instance then
7884 if Is_Comp
7885 or else (not Is_Overloaded (Arg)
7886 and then Etype (Arg) /= Any_Composite
7887 and then Covers (Ctyp, Etype (Arg)))
7888 then
7889 Resolve (Arg, Ctyp);
7890 else
7891 Resolve (Arg, Btyp);
7892 end if;
7894 -- If both Array & Array and Array & Component are visible, there is a
7895 -- potential ambiguity that must be reported.
7897 elsif Has_Compatible_Type (Arg, Ctyp) then
7898 if Nkind (Arg) = N_Aggregate
7899 and then Is_Composite_Type (Ctyp)
7900 then
7901 if Is_Private_Type (Ctyp) then
7902 Resolve (Arg, Btyp);
7904 -- If the operation is user-defined and not overloaded use its
7905 -- profile. The operation may be a renaming, in which case it has
7906 -- been rewritten, and we want the original profile.
7908 elsif not Is_Overloaded (N)
7909 and then Comes_From_Source (Entity (Original_Node (N)))
7910 and then Ekind (Entity (Original_Node (N))) = E_Function
7911 then
7912 Resolve (Arg,
7913 Etype
7914 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
7915 return;
7917 -- Otherwise an aggregate may match both the array type and the
7918 -- component type.
7920 else
7921 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7922 Set_Etype (Arg, Any_Type);
7923 end if;
7925 else
7926 if Is_Overloaded (Arg)
7927 and then Has_Compatible_Type (Arg, Typ)
7928 and then Etype (Arg) /= Any_Type
7929 then
7930 declare
7931 I : Interp_Index;
7932 It : Interp;
7933 Func : Entity_Id;
7935 begin
7936 Get_First_Interp (Arg, I, It);
7937 Func := It.Nam;
7938 Get_Next_Interp (I, It);
7940 -- Special-case the error message when the overloading is
7941 -- caused by a function that yields an array and can be
7942 -- called without parameters.
7944 if It.Nam = Func then
7945 Error_Msg_Sloc := Sloc (Func);
7946 Error_Msg_N ("ambiguous call to function#", Arg);
7947 Error_Msg_NE
7948 ("\\interpretation as call yields&", Arg, Typ);
7949 Error_Msg_NE
7950 ("\\interpretation as indexing of call yields&",
7951 Arg, Component_Type (Typ));
7953 else
7954 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
7956 Get_First_Interp (Arg, I, It);
7957 while Present (It.Nam) loop
7958 Error_Msg_Sloc := Sloc (It.Nam);
7960 if Base_Type (It.Typ) = Btyp
7961 or else
7962 Base_Type (It.Typ) = Base_Type (Ctyp)
7963 then
7964 Error_Msg_N -- CODEFIX
7965 ("\\possible interpretation#", Arg);
7966 end if;
7968 Get_Next_Interp (I, It);
7969 end loop;
7970 end if;
7971 end;
7972 end if;
7974 Resolve (Arg, Component_Type (Typ));
7976 if Nkind (Arg) = N_String_Literal then
7977 Set_Etype (Arg, Component_Type (Typ));
7978 end if;
7980 if Arg = Left_Opnd (N) then
7981 Set_Is_Component_Left_Opnd (N);
7982 else
7983 Set_Is_Component_Right_Opnd (N);
7984 end if;
7985 end if;
7987 else
7988 Resolve (Arg, Btyp);
7989 end if;
7991 -- Concatenation is restricted in SPARK: each operand must be either a
7992 -- string literal, the name of a string constant, a static character or
7993 -- string expression, or another concatenation. Arg cannot be a
7994 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
7995 -- separately on each final operand, past concatenation operations.
7997 if Is_Character_Type (Etype (Arg)) then
7998 if not Is_Static_Expression (Arg) then
7999 Check_SPARK_Restriction
8000 ("character operand for concatenation should be static", Arg);
8001 end if;
8003 elsif Is_String_Type (Etype (Arg)) then
8004 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
8005 and then Is_Constant_Object (Entity (Arg)))
8006 and then not Is_Static_Expression (Arg)
8007 then
8008 Check_SPARK_Restriction
8009 ("string operand for concatenation should be static", Arg);
8010 end if;
8012 -- Do not issue error on an operand that is neither a character nor a
8013 -- string, as the error is issued in Resolve_Op_Concat.
8015 else
8016 null;
8017 end if;
8019 Check_Unset_Reference (Arg);
8020 end Resolve_Op_Concat_Arg;
8022 -----------------------------
8023 -- Resolve_Op_Concat_First --
8024 -----------------------------
8026 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
8027 Btyp : constant Entity_Id := Base_Type (Typ);
8028 Op1 : constant Node_Id := Left_Opnd (N);
8029 Op2 : constant Node_Id := Right_Opnd (N);
8031 begin
8032 -- The parser folds an enormous sequence of concatenations of string
8033 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
8034 -- in the right operand. If the expression resolves to a predefined "&"
8035 -- operator, all is well. Otherwise, the parser's folding is wrong, so
8036 -- we give an error. See P_Simple_Expression in Par.Ch4.
8038 if Nkind (Op2) = N_String_Literal
8039 and then Is_Folded_In_Parser (Op2)
8040 and then Ekind (Entity (N)) = E_Function
8041 then
8042 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
8043 and then String_Length (Strval (Op1)) = 0);
8044 Error_Msg_N ("too many user-defined concatenations", N);
8045 return;
8046 end if;
8048 Set_Etype (N, Btyp);
8050 if Is_Limited_Composite (Btyp) then
8051 Error_Msg_N ("concatenation not available for limited array", N);
8052 Explain_Limited_Type (Btyp, N);
8053 end if;
8054 end Resolve_Op_Concat_First;
8056 ----------------------------
8057 -- Resolve_Op_Concat_Rest --
8058 ----------------------------
8060 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
8061 Op1 : constant Node_Id := Left_Opnd (N);
8062 Op2 : constant Node_Id := Right_Opnd (N);
8064 begin
8065 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
8067 Generate_Operator_Reference (N, Typ);
8069 if Is_String_Type (Typ) then
8070 Eval_Concatenation (N);
8071 end if;
8073 -- If this is not a static concatenation, but the result is a string
8074 -- type (and not an array of strings) ensure that static string operands
8075 -- have their subtypes properly constructed.
8077 if Nkind (N) /= N_String_Literal
8078 and then Is_Character_Type (Component_Type (Typ))
8079 then
8080 Set_String_Literal_Subtype (Op1, Typ);
8081 Set_String_Literal_Subtype (Op2, Typ);
8082 end if;
8083 end Resolve_Op_Concat_Rest;
8085 ----------------------
8086 -- Resolve_Op_Expon --
8087 ----------------------
8089 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
8090 B_Typ : constant Entity_Id := Base_Type (Typ);
8092 begin
8093 -- Catch attempts to do fixed-point exponentiation with universal
8094 -- operands, which is a case where the illegality is not caught during
8095 -- normal operator analysis.
8097 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
8098 Error_Msg_N ("exponentiation not available for fixed point", N);
8099 return;
8101 elsif Nkind (Parent (N)) in N_Op
8102 and then Is_Fixed_Point_Type (Etype (Parent (N)))
8103 and then Etype (N) = Universal_Real
8104 and then Comes_From_Source (N)
8105 then
8106 Error_Msg_N ("exponentiation not available for fixed point", N);
8107 return;
8108 end if;
8110 if Comes_From_Source (N)
8111 and then Ekind (Entity (N)) = E_Function
8112 and then Is_Imported (Entity (N))
8113 and then Is_Intrinsic_Subprogram (Entity (N))
8114 then
8115 Resolve_Intrinsic_Operator (N, Typ);
8116 return;
8117 end if;
8119 if Etype (Left_Opnd (N)) = Universal_Integer
8120 or else Etype (Left_Opnd (N)) = Universal_Real
8121 then
8122 Check_For_Visible_Operator (N, B_Typ);
8123 end if;
8125 -- We do the resolution using the base type, because intermediate values
8126 -- in expressions always are of the base type, not a subtype of it.
8128 Resolve (Left_Opnd (N), B_Typ);
8129 Resolve (Right_Opnd (N), Standard_Integer);
8131 Check_Unset_Reference (Left_Opnd (N));
8132 Check_Unset_Reference (Right_Opnd (N));
8134 Set_Etype (N, B_Typ);
8135 Generate_Operator_Reference (N, B_Typ);
8137 Analyze_Dimension (N);
8139 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
8140 -- Evaluate the exponentiation operator for dimensioned type
8142 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
8143 else
8144 Eval_Op_Expon (N);
8145 end if;
8147 -- Set overflow checking bit. Much cleverer code needed here eventually
8148 -- and perhaps the Resolve routines should be separated for the various
8149 -- arithmetic operations, since they will need different processing. ???
8151 if Nkind (N) in N_Op then
8152 if not Overflow_Checks_Suppressed (Etype (N)) then
8153 Enable_Overflow_Check (N);
8154 end if;
8155 end if;
8156 end Resolve_Op_Expon;
8158 --------------------
8159 -- Resolve_Op_Not --
8160 --------------------
8162 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8163 B_Typ : Entity_Id;
8165 function Parent_Is_Boolean return Boolean;
8166 -- This function determines if the parent node is a boolean operator or
8167 -- operation (comparison op, membership test, or short circuit form) and
8168 -- the not in question is the left operand of this operation. Note that
8169 -- if the not is in parens, then false is returned.
8171 -----------------------
8172 -- Parent_Is_Boolean --
8173 -----------------------
8175 function Parent_Is_Boolean return Boolean is
8176 begin
8177 if Paren_Count (N) /= 0 then
8178 return False;
8180 else
8181 case Nkind (Parent (N)) is
8182 when N_Op_And |
8183 N_Op_Eq |
8184 N_Op_Ge |
8185 N_Op_Gt |
8186 N_Op_Le |
8187 N_Op_Lt |
8188 N_Op_Ne |
8189 N_Op_Or |
8190 N_Op_Xor |
8191 N_In |
8192 N_Not_In |
8193 N_And_Then |
8194 N_Or_Else =>
8196 return Left_Opnd (Parent (N)) = N;
8198 when others =>
8199 return False;
8200 end case;
8201 end if;
8202 end Parent_Is_Boolean;
8204 -- Start of processing for Resolve_Op_Not
8206 begin
8207 -- Predefined operations on scalar types yield the base type. On the
8208 -- other hand, logical operations on arrays yield the type of the
8209 -- arguments (and the context).
8211 if Is_Array_Type (Typ) then
8212 B_Typ := Typ;
8213 else
8214 B_Typ := Base_Type (Typ);
8215 end if;
8217 if Is_VMS_Operator (Entity (N)) then
8218 null;
8220 -- Straightforward case of incorrect arguments
8222 elsif not Valid_Boolean_Arg (Typ) then
8223 Error_Msg_N ("invalid operand type for operator&", N);
8224 Set_Etype (N, Any_Type);
8225 return;
8227 -- Special case of probable missing parens
8229 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8230 if Parent_Is_Boolean then
8231 Error_Msg_N
8232 ("operand of not must be enclosed in parentheses",
8233 Right_Opnd (N));
8234 else
8235 Error_Msg_N
8236 ("no modular type available in this context", N);
8237 end if;
8239 Set_Etype (N, Any_Type);
8240 return;
8242 -- OK resolution of NOT
8244 else
8245 -- Warn if non-boolean types involved. This is a case like not a < b
8246 -- where a and b are modular, where we will get (not a) < b and most
8247 -- likely not (a < b) was intended.
8249 if Warn_On_Questionable_Missing_Parens
8250 and then not Is_Boolean_Type (Typ)
8251 and then Parent_Is_Boolean
8252 then
8253 Error_Msg_N ("?not expression should be parenthesized here!", N);
8254 end if;
8256 -- Warn on double negation if checking redundant constructs
8258 if Warn_On_Redundant_Constructs
8259 and then Comes_From_Source (N)
8260 and then Comes_From_Source (Right_Opnd (N))
8261 and then Root_Type (Typ) = Standard_Boolean
8262 and then Nkind (Right_Opnd (N)) = N_Op_Not
8263 then
8264 Error_Msg_N ("redundant double negation?", N);
8265 end if;
8267 -- Complete resolution and evaluation of NOT
8269 Resolve (Right_Opnd (N), B_Typ);
8270 Check_Unset_Reference (Right_Opnd (N));
8271 Set_Etype (N, B_Typ);
8272 Generate_Operator_Reference (N, B_Typ);
8273 Eval_Op_Not (N);
8274 end if;
8275 end Resolve_Op_Not;
8277 -----------------------------
8278 -- Resolve_Operator_Symbol --
8279 -----------------------------
8281 -- Nothing to be done, all resolved already
8283 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8284 pragma Warnings (Off, N);
8285 pragma Warnings (Off, Typ);
8287 begin
8288 null;
8289 end Resolve_Operator_Symbol;
8291 ----------------------------------
8292 -- Resolve_Qualified_Expression --
8293 ----------------------------------
8295 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8296 pragma Warnings (Off, Typ);
8298 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8299 Expr : constant Node_Id := Expression (N);
8301 begin
8302 Resolve (Expr, Target_Typ);
8304 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8305 -- operation if not needed.
8307 if Restriction_Check_Required (SPARK)
8308 and then Is_Array_Type (Target_Typ)
8309 and then Is_Array_Type (Etype (Expr))
8310 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8311 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8312 then
8313 Check_SPARK_Restriction
8314 ("array types should have matching static bounds", N);
8315 end if;
8317 -- A qualified expression requires an exact match of the type, class-
8318 -- wide matching is not allowed. However, if the qualifying type is
8319 -- specific and the expression has a class-wide type, it may still be
8320 -- okay, since it can be the result of the expansion of a call to a
8321 -- dispatching function, so we also have to check class-wideness of the
8322 -- type of the expression's original node.
8324 if (Is_Class_Wide_Type (Target_Typ)
8325 or else
8326 (Is_Class_Wide_Type (Etype (Expr))
8327 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8328 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8329 then
8330 Wrong_Type (Expr, Target_Typ);
8331 end if;
8333 -- If the target type is unconstrained, then we reset the type of the
8334 -- result from the type of the expression. For other cases, the actual
8335 -- subtype of the expression is the target type.
8337 if Is_Composite_Type (Target_Typ)
8338 and then not Is_Constrained (Target_Typ)
8339 then
8340 Set_Etype (N, Etype (Expr));
8341 end if;
8343 Analyze_Dimension (N);
8344 Eval_Qualified_Expression (N);
8345 end Resolve_Qualified_Expression;
8347 -------------------
8348 -- Resolve_Range --
8349 -------------------
8351 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8352 L : constant Node_Id := Low_Bound (N);
8353 H : constant Node_Id := High_Bound (N);
8355 function First_Last_Ref return Boolean;
8356 -- Returns True if N is of the form X'First .. X'Last where X is the
8357 -- same entity for both attributes.
8359 --------------------
8360 -- First_Last_Ref --
8361 --------------------
8363 function First_Last_Ref return Boolean is
8364 Lorig : constant Node_Id := Original_Node (L);
8365 Horig : constant Node_Id := Original_Node (H);
8367 begin
8368 if Nkind (Lorig) = N_Attribute_Reference
8369 and then Nkind (Horig) = N_Attribute_Reference
8370 and then Attribute_Name (Lorig) = Name_First
8371 and then Attribute_Name (Horig) = Name_Last
8372 then
8373 declare
8374 PL : constant Node_Id := Prefix (Lorig);
8375 PH : constant Node_Id := Prefix (Horig);
8376 begin
8377 if Is_Entity_Name (PL)
8378 and then Is_Entity_Name (PH)
8379 and then Entity (PL) = Entity (PH)
8380 then
8381 return True;
8382 end if;
8383 end;
8384 end if;
8386 return False;
8387 end First_Last_Ref;
8389 -- Start of processing for Resolve_Range
8391 begin
8392 Set_Etype (N, Typ);
8393 Resolve (L, Typ);
8394 Resolve (H, Typ);
8396 -- Check for inappropriate range on unordered enumeration type
8398 if Bad_Unordered_Enumeration_Reference (N, Typ)
8400 -- Exclude X'First .. X'Last if X is the same entity for both
8402 and then not First_Last_Ref
8403 then
8404 Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
8405 end if;
8407 Check_Unset_Reference (L);
8408 Check_Unset_Reference (H);
8410 -- We have to check the bounds for being within the base range as
8411 -- required for a non-static context. Normally this is automatic and
8412 -- done as part of evaluating expressions, but the N_Range node is an
8413 -- exception, since in GNAT we consider this node to be a subexpression,
8414 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8415 -- this, but that would put the test on the main evaluation path for
8416 -- expressions.
8418 Check_Non_Static_Context (L);
8419 Check_Non_Static_Context (H);
8421 -- Check for an ambiguous range over character literals. This will
8422 -- happen with a membership test involving only literals.
8424 if Typ = Any_Character then
8425 Ambiguous_Character (L);
8426 Set_Etype (N, Any_Type);
8427 return;
8428 end if;
8430 -- If bounds are static, constant-fold them, so size computations are
8431 -- identical between front-end and back-end. Do not perform this
8432 -- transformation while analyzing generic units, as type information
8433 -- would be lost when reanalyzing the constant node in the instance.
8435 if Is_Discrete_Type (Typ) and then Full_Expander_Active then
8436 if Is_OK_Static_Expression (L) then
8437 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8438 end if;
8440 if Is_OK_Static_Expression (H) then
8441 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8442 end if;
8443 end if;
8444 end Resolve_Range;
8446 --------------------------
8447 -- Resolve_Real_Literal --
8448 --------------------------
8450 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8451 Actual_Typ : constant Entity_Id := Etype (N);
8453 begin
8454 -- Special processing for fixed-point literals to make sure that the
8455 -- value is an exact multiple of small where this is required. We skip
8456 -- this for the universal real case, and also for generic types.
8458 if Is_Fixed_Point_Type (Typ)
8459 and then Typ /= Universal_Fixed
8460 and then Typ /= Any_Fixed
8461 and then not Is_Generic_Type (Typ)
8462 then
8463 declare
8464 Val : constant Ureal := Realval (N);
8465 Cintr : constant Ureal := Val / Small_Value (Typ);
8466 Cint : constant Uint := UR_Trunc (Cintr);
8467 Den : constant Uint := Norm_Den (Cintr);
8468 Stat : Boolean;
8470 begin
8471 -- Case of literal is not an exact multiple of the Small
8473 if Den /= 1 then
8475 -- For a source program literal for a decimal fixed-point type,
8476 -- this is statically illegal (RM 4.9(36)).
8478 if Is_Decimal_Fixed_Point_Type (Typ)
8479 and then Actual_Typ = Universal_Real
8480 and then Comes_From_Source (N)
8481 then
8482 Error_Msg_N ("value has extraneous low order digits", N);
8483 end if;
8485 -- Generate a warning if literal from source
8487 if Is_Static_Expression (N)
8488 and then Warn_On_Bad_Fixed_Value
8489 then
8490 Error_Msg_N
8491 ("?static fixed-point value is not a multiple of Small!",
8493 end if;
8495 -- Replace literal by a value that is the exact representation
8496 -- of a value of the type, i.e. a multiple of the small value,
8497 -- by truncation, since Machine_Rounds is false for all GNAT
8498 -- fixed-point types (RM 4.9(38)).
8500 Stat := Is_Static_Expression (N);
8501 Rewrite (N,
8502 Make_Real_Literal (Sloc (N),
8503 Realval => Small_Value (Typ) * Cint));
8505 Set_Is_Static_Expression (N, Stat);
8506 end if;
8508 -- In all cases, set the corresponding integer field
8510 Set_Corresponding_Integer_Value (N, Cint);
8511 end;
8512 end if;
8514 -- Now replace the actual type by the expected type as usual
8516 Set_Etype (N, Typ);
8517 Eval_Real_Literal (N);
8518 end Resolve_Real_Literal;
8520 -----------------------
8521 -- Resolve_Reference --
8522 -----------------------
8524 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8525 P : constant Node_Id := Prefix (N);
8527 begin
8528 -- Replace general access with specific type
8530 if Ekind (Etype (N)) = E_Allocator_Type then
8531 Set_Etype (N, Base_Type (Typ));
8532 end if;
8534 Resolve (P, Designated_Type (Etype (N)));
8536 -- If we are taking the reference of a volatile entity, then treat it as
8537 -- a potential modification of this entity. This is too conservative,
8538 -- but necessary because remove side effects can cause transformations
8539 -- of normal assignments into reference sequences that otherwise fail to
8540 -- notice the modification.
8542 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8543 Note_Possible_Modification (P, Sure => False);
8544 end if;
8545 end Resolve_Reference;
8547 --------------------------------
8548 -- Resolve_Selected_Component --
8549 --------------------------------
8551 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8552 Comp : Entity_Id;
8553 Comp1 : Entity_Id := Empty; -- prevent junk warning
8554 P : constant Node_Id := Prefix (N);
8555 S : constant Node_Id := Selector_Name (N);
8556 T : Entity_Id := Etype (P);
8557 I : Interp_Index;
8558 I1 : Interp_Index := 0; -- prevent junk warning
8559 It : Interp;
8560 It1 : Interp;
8561 Found : Boolean;
8563 function Init_Component return Boolean;
8564 -- Check whether this is the initialization of a component within an
8565 -- init proc (by assignment or call to another init proc). If true,
8566 -- there is no need for a discriminant check.
8568 --------------------
8569 -- Init_Component --
8570 --------------------
8572 function Init_Component return Boolean is
8573 begin
8574 return Inside_Init_Proc
8575 and then Nkind (Prefix (N)) = N_Identifier
8576 and then Chars (Prefix (N)) = Name_uInit
8577 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8578 end Init_Component;
8580 -- Start of processing for Resolve_Selected_Component
8582 begin
8583 if Is_Overloaded (P) then
8585 -- Use the context type to select the prefix that has a selector
8586 -- of the correct name and type.
8588 Found := False;
8589 Get_First_Interp (P, I, It);
8591 Search : while Present (It.Typ) loop
8592 if Is_Access_Type (It.Typ) then
8593 T := Designated_Type (It.Typ);
8594 else
8595 T := It.Typ;
8596 end if;
8598 -- Locate selected component. For a private prefix the selector
8599 -- can denote a discriminant.
8601 if Is_Record_Type (T) or else Is_Private_Type (T) then
8603 -- The visible components of a class-wide type are those of
8604 -- the root type.
8606 if Is_Class_Wide_Type (T) then
8607 T := Etype (T);
8608 end if;
8610 Comp := First_Entity (T);
8611 while Present (Comp) loop
8612 if Chars (Comp) = Chars (S)
8613 and then Covers (Etype (Comp), Typ)
8614 then
8615 if not Found then
8616 Found := True;
8617 I1 := I;
8618 It1 := It;
8619 Comp1 := Comp;
8621 else
8622 It := Disambiguate (P, I1, I, Any_Type);
8624 if It = No_Interp then
8625 Error_Msg_N
8626 ("ambiguous prefix for selected component", N);
8627 Set_Etype (N, Typ);
8628 return;
8630 else
8631 It1 := It;
8633 -- There may be an implicit dereference. Retrieve
8634 -- designated record type.
8636 if Is_Access_Type (It1.Typ) then
8637 T := Designated_Type (It1.Typ);
8638 else
8639 T := It1.Typ;
8640 end if;
8642 if Scope (Comp1) /= T then
8644 -- Resolution chooses the new interpretation.
8645 -- Find the component with the right name.
8647 Comp1 := First_Entity (T);
8648 while Present (Comp1)
8649 and then Chars (Comp1) /= Chars (S)
8650 loop
8651 Comp1 := Next_Entity (Comp1);
8652 end loop;
8653 end if;
8655 exit Search;
8656 end if;
8657 end if;
8658 end if;
8660 Comp := Next_Entity (Comp);
8661 end loop;
8662 end if;
8664 Get_Next_Interp (I, It);
8665 end loop Search;
8667 Resolve (P, It1.Typ);
8668 Set_Etype (N, Typ);
8669 Set_Entity_With_Style_Check (S, Comp1);
8671 else
8672 -- Resolve prefix with its type
8674 Resolve (P, T);
8675 end if;
8677 -- Generate cross-reference. We needed to wait until full overloading
8678 -- resolution was complete to do this, since otherwise we can't tell if
8679 -- we are an lvalue or not.
8681 if May_Be_Lvalue (N) then
8682 Generate_Reference (Entity (S), S, 'm');
8683 else
8684 Generate_Reference (Entity (S), S, 'r');
8685 end if;
8687 -- If prefix is an access type, the node will be transformed into an
8688 -- explicit dereference during expansion. The type of the node is the
8689 -- designated type of that of the prefix.
8691 if Is_Access_Type (Etype (P)) then
8692 T := Designated_Type (Etype (P));
8693 Check_Fully_Declared_Prefix (T, P);
8694 else
8695 T := Etype (P);
8696 end if;
8698 if Has_Discriminants (T)
8699 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8700 and then Present (Original_Record_Component (Entity (S)))
8701 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8702 and then Present (Discriminant_Checking_Func
8703 (Original_Record_Component (Entity (S))))
8704 and then not Discriminant_Checks_Suppressed (T)
8705 and then not Init_Component
8706 then
8707 Set_Do_Discriminant_Check (N);
8708 end if;
8710 if Ekind (Entity (S)) = E_Void then
8711 Error_Msg_N ("premature use of component", S);
8712 end if;
8714 -- If the prefix is a record conversion, this may be a renamed
8715 -- discriminant whose bounds differ from those of the original
8716 -- one, so we must ensure that a range check is performed.
8718 if Nkind (P) = N_Type_Conversion
8719 and then Ekind (Entity (S)) = E_Discriminant
8720 and then Is_Discrete_Type (Typ)
8721 then
8722 Set_Etype (N, Base_Type (Typ));
8723 end if;
8725 -- Note: No Eval processing is required, because the prefix is of a
8726 -- record type, or protected type, and neither can possibly be static.
8728 -- If the array type is atomic, and is packed, and we are in a left side
8729 -- context, then this is worth a warning, since we have a situation
8730 -- where the access to the component may cause extra read/writes of the
8731 -- atomic array object, which could be considered unexpected.
8733 if Nkind (N) = N_Selected_Component
8734 and then (Is_Atomic (T)
8735 or else (Is_Entity_Name (Prefix (N))
8736 and then Is_Atomic (Entity (Prefix (N)))))
8737 and then Is_Packed (T)
8738 and then Is_LHS (N)
8739 then
8740 Error_Msg_N
8741 ("?assignment to component of packed atomic record", Prefix (N));
8742 Error_Msg_N
8743 ("?\may cause unexpected accesses to atomic object", Prefix (N));
8744 end if;
8746 Analyze_Dimension (N);
8747 end Resolve_Selected_Component;
8749 -------------------
8750 -- Resolve_Shift --
8751 -------------------
8753 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8754 B_Typ : constant Entity_Id := Base_Type (Typ);
8755 L : constant Node_Id := Left_Opnd (N);
8756 R : constant Node_Id := Right_Opnd (N);
8758 begin
8759 -- We do the resolution using the base type, because intermediate values
8760 -- in expressions always are of the base type, not a subtype of it.
8762 Resolve (L, B_Typ);
8763 Resolve (R, Standard_Natural);
8765 Check_Unset_Reference (L);
8766 Check_Unset_Reference (R);
8768 Set_Etype (N, B_Typ);
8769 Generate_Operator_Reference (N, B_Typ);
8770 Eval_Shift (N);
8771 end Resolve_Shift;
8773 ---------------------------
8774 -- Resolve_Short_Circuit --
8775 ---------------------------
8777 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8778 B_Typ : constant Entity_Id := Base_Type (Typ);
8779 L : constant Node_Id := Left_Opnd (N);
8780 R : constant Node_Id := Right_Opnd (N);
8782 begin
8783 Resolve (L, B_Typ);
8784 Resolve (R, B_Typ);
8786 -- Check for issuing warning for always False assert/check, this happens
8787 -- when assertions are turned off, in which case the pragma Assert/Check
8788 -- was transformed into:
8790 -- if False and then <condition> then ...
8792 -- and we detect this pattern
8794 if Warn_On_Assertion_Failure
8795 and then Is_Entity_Name (R)
8796 and then Entity (R) = Standard_False
8797 and then Nkind (Parent (N)) = N_If_Statement
8798 and then Nkind (N) = N_And_Then
8799 and then Is_Entity_Name (L)
8800 and then Entity (L) = Standard_False
8801 then
8802 declare
8803 Orig : constant Node_Id := Original_Node (Parent (N));
8805 begin
8806 if Nkind (Orig) = N_Pragma
8807 and then Pragma_Name (Orig) = Name_Assert
8808 then
8809 -- Don't want to warn if original condition is explicit False
8811 declare
8812 Expr : constant Node_Id :=
8813 Original_Node
8814 (Expression
8815 (First (Pragma_Argument_Associations (Orig))));
8816 begin
8817 if Is_Entity_Name (Expr)
8818 and then Entity (Expr) = Standard_False
8819 then
8820 null;
8821 else
8822 -- Issue warning. We do not want the deletion of the
8823 -- IF/AND-THEN to take this message with it. We achieve
8824 -- this by making sure that the expanded code points to
8825 -- the Sloc of the expression, not the original pragma.
8827 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
8828 -- The source location of the expression is not usually
8829 -- the best choice here. For example, it gets located on
8830 -- the last AND keyword in a chain of boolean expressiond
8831 -- AND'ed together. It is best to put the message on the
8832 -- first character of the assertion, which is the effect
8833 -- of the First_Node call here.
8835 Error_Msg_F
8836 ("?assertion would fail at run time!",
8837 Expression
8838 (First (Pragma_Argument_Associations (Orig))));
8839 end if;
8840 end;
8842 -- Similar processing for Check pragma
8844 elsif Nkind (Orig) = N_Pragma
8845 and then Pragma_Name (Orig) = Name_Check
8846 then
8847 -- Don't want to warn if original condition is explicit False
8849 declare
8850 Expr : constant Node_Id :=
8851 Original_Node
8852 (Expression
8853 (Next (First
8854 (Pragma_Argument_Associations (Orig)))));
8855 begin
8856 if Is_Entity_Name (Expr)
8857 and then Entity (Expr) = Standard_False
8858 then
8859 null;
8861 -- Post warning
8863 else
8864 -- Again use Error_Msg_F rather than Error_Msg_N, see
8865 -- comment above for an explanation of why we do this.
8867 Error_Msg_F
8868 ("?check would fail at run time!",
8869 Expression
8870 (Last (Pragma_Argument_Associations (Orig))));
8871 end if;
8872 end;
8873 end if;
8874 end;
8875 end if;
8877 -- Continue with processing of short circuit
8879 Check_Unset_Reference (L);
8880 Check_Unset_Reference (R);
8882 Set_Etype (N, B_Typ);
8883 Eval_Short_Circuit (N);
8884 end Resolve_Short_Circuit;
8886 -------------------
8887 -- Resolve_Slice --
8888 -------------------
8890 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8891 Drange : constant Node_Id := Discrete_Range (N);
8892 Name : constant Node_Id := Prefix (N);
8893 Array_Type : Entity_Id := Empty;
8894 Index_Type : Entity_Id;
8896 begin
8897 if Is_Overloaded (Name) then
8899 -- Use the context type to select the prefix that yields the correct
8900 -- array type.
8902 declare
8903 I : Interp_Index;
8904 I1 : Interp_Index := 0;
8905 It : Interp;
8906 P : constant Node_Id := Prefix (N);
8907 Found : Boolean := False;
8909 begin
8910 Get_First_Interp (P, I, It);
8911 while Present (It.Typ) loop
8912 if (Is_Array_Type (It.Typ)
8913 and then Covers (Typ, It.Typ))
8914 or else (Is_Access_Type (It.Typ)
8915 and then Is_Array_Type (Designated_Type (It.Typ))
8916 and then Covers (Typ, Designated_Type (It.Typ)))
8917 then
8918 if Found then
8919 It := Disambiguate (P, I1, I, Any_Type);
8921 if It = No_Interp then
8922 Error_Msg_N ("ambiguous prefix for slicing", N);
8923 Set_Etype (N, Typ);
8924 return;
8925 else
8926 Found := True;
8927 Array_Type := It.Typ;
8928 I1 := I;
8929 end if;
8930 else
8931 Found := True;
8932 Array_Type := It.Typ;
8933 I1 := I;
8934 end if;
8935 end if;
8937 Get_Next_Interp (I, It);
8938 end loop;
8939 end;
8941 else
8942 Array_Type := Etype (Name);
8943 end if;
8945 Resolve (Name, Array_Type);
8947 if Is_Access_Type (Array_Type) then
8948 Apply_Access_Check (N);
8949 Array_Type := Designated_Type (Array_Type);
8951 -- If the prefix is an access to an unconstrained array, we must use
8952 -- the actual subtype of the object to perform the index checks. The
8953 -- object denoted by the prefix is implicit in the node, so we build
8954 -- an explicit representation for it in order to compute the actual
8955 -- subtype.
8957 if not Is_Constrained (Array_Type) then
8958 Remove_Side_Effects (Prefix (N));
8960 declare
8961 Obj : constant Node_Id :=
8962 Make_Explicit_Dereference (Sloc (N),
8963 Prefix => New_Copy_Tree (Prefix (N)));
8964 begin
8965 Set_Etype (Obj, Array_Type);
8966 Set_Parent (Obj, Parent (N));
8967 Array_Type := Get_Actual_Subtype (Obj);
8968 end;
8969 end if;
8971 elsif Is_Entity_Name (Name)
8972 or else Nkind (Name) = N_Explicit_Dereference
8973 or else (Nkind (Name) = N_Function_Call
8974 and then not Is_Constrained (Etype (Name)))
8975 then
8976 Array_Type := Get_Actual_Subtype (Name);
8978 -- If the name is a selected component that depends on discriminants,
8979 -- build an actual subtype for it. This can happen only when the name
8980 -- itself is overloaded; otherwise the actual subtype is created when
8981 -- the selected component is analyzed.
8983 elsif Nkind (Name) = N_Selected_Component
8984 and then Full_Analysis
8985 and then Depends_On_Discriminant (First_Index (Array_Type))
8986 then
8987 declare
8988 Act_Decl : constant Node_Id :=
8989 Build_Actual_Subtype_Of_Component (Array_Type, Name);
8990 begin
8991 Insert_Action (N, Act_Decl);
8992 Array_Type := Defining_Identifier (Act_Decl);
8993 end;
8995 -- Maybe this should just be "else", instead of checking for the
8996 -- specific case of slice??? This is needed for the case where the
8997 -- prefix is an Image attribute, which gets expanded to a slice, and so
8998 -- has a constrained subtype which we want to use for the slice range
8999 -- check applied below (the range check won't get done if the
9000 -- unconstrained subtype of the 'Image is used).
9002 elsif Nkind (Name) = N_Slice then
9003 Array_Type := Etype (Name);
9004 end if;
9006 -- If name was overloaded, set slice type correctly now
9008 Set_Etype (N, Array_Type);
9010 -- If the range is specified by a subtype mark, no resolution is
9011 -- necessary. Else resolve the bounds, and apply needed checks.
9013 if not Is_Entity_Name (Drange) then
9014 if Ekind (Array_Type) = E_String_Literal_Subtype then
9015 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
9016 else
9017 Index_Type := Etype (First_Index (Array_Type));
9018 end if;
9020 Resolve (Drange, Base_Type (Index_Type));
9022 if Nkind (Drange) = N_Range then
9024 -- Ensure that side effects in the bounds are properly handled
9026 Force_Evaluation (Low_Bound (Drange));
9027 Force_Evaluation (High_Bound (Drange));
9029 -- Do not apply the range check to nodes associated with the
9030 -- frontend expansion of the dispatch table. We first check
9031 -- if Ada.Tags is already loaded to avoid the addition of an
9032 -- undesired dependence on such run-time unit.
9034 if not Tagged_Type_Expansion
9035 or else not
9036 (RTU_Loaded (Ada_Tags)
9037 and then Nkind (Prefix (N)) = N_Selected_Component
9038 and then Present (Entity (Selector_Name (Prefix (N))))
9039 and then Entity (Selector_Name (Prefix (N))) =
9040 RTE_Record_Component (RE_Prims_Ptr))
9041 then
9042 Apply_Range_Check (Drange, Index_Type);
9043 end if;
9044 end if;
9045 end if;
9047 Set_Slice_Subtype (N);
9049 -- Check bad use of type with predicates
9051 if Has_Predicates (Etype (Drange)) then
9052 Bad_Predicated_Subtype_Use
9053 ("subtype& has predicate, not allowed in slice",
9054 Drange, Etype (Drange));
9056 -- Otherwise here is where we check suspicious indexes
9058 elsif Nkind (Drange) = N_Range then
9059 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
9060 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
9061 end if;
9063 Analyze_Dimension (N);
9064 Eval_Slice (N);
9065 end Resolve_Slice;
9067 ----------------------------
9068 -- Resolve_String_Literal --
9069 ----------------------------
9071 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
9072 C_Typ : constant Entity_Id := Component_Type (Typ);
9073 R_Typ : constant Entity_Id := Root_Type (C_Typ);
9074 Loc : constant Source_Ptr := Sloc (N);
9075 Str : constant String_Id := Strval (N);
9076 Strlen : constant Nat := String_Length (Str);
9077 Subtype_Id : Entity_Id;
9078 Need_Check : Boolean;
9080 begin
9081 -- For a string appearing in a concatenation, defer creation of the
9082 -- string_literal_subtype until the end of the resolution of the
9083 -- concatenation, because the literal may be constant-folded away. This
9084 -- is a useful optimization for long concatenation expressions.
9086 -- If the string is an aggregate built for a single character (which
9087 -- happens in a non-static context) or a is null string to which special
9088 -- checks may apply, we build the subtype. Wide strings must also get a
9089 -- string subtype if they come from a one character aggregate. Strings
9090 -- generated by attributes might be static, but it is often hard to
9091 -- determine whether the enclosing context is static, so we generate
9092 -- subtypes for them as well, thus losing some rarer optimizations ???
9093 -- Same for strings that come from a static conversion.
9095 Need_Check :=
9096 (Strlen = 0 and then Typ /= Standard_String)
9097 or else Nkind (Parent (N)) /= N_Op_Concat
9098 or else (N /= Left_Opnd (Parent (N))
9099 and then N /= Right_Opnd (Parent (N)))
9100 or else ((Typ = Standard_Wide_String
9101 or else Typ = Standard_Wide_Wide_String)
9102 and then Nkind (Original_Node (N)) /= N_String_Literal);
9104 -- If the resolving type is itself a string literal subtype, we can just
9105 -- reuse it, since there is no point in creating another.
9107 if Ekind (Typ) = E_String_Literal_Subtype then
9108 Subtype_Id := Typ;
9110 elsif Nkind (Parent (N)) = N_Op_Concat
9111 and then not Need_Check
9112 and then not Nkind_In (Original_Node (N), N_Character_Literal,
9113 N_Attribute_Reference,
9114 N_Qualified_Expression,
9115 N_Type_Conversion)
9116 then
9117 Subtype_Id := Typ;
9119 -- Otherwise we must create a string literal subtype. Note that the
9120 -- whole idea of string literal subtypes is simply to avoid the need
9121 -- for building a full fledged array subtype for each literal.
9123 else
9124 Set_String_Literal_Subtype (N, Typ);
9125 Subtype_Id := Etype (N);
9126 end if;
9128 if Nkind (Parent (N)) /= N_Op_Concat
9129 or else Need_Check
9130 then
9131 Set_Etype (N, Subtype_Id);
9132 Eval_String_Literal (N);
9133 end if;
9135 if Is_Limited_Composite (Typ)
9136 or else Is_Private_Composite (Typ)
9137 then
9138 Error_Msg_N ("string literal not available for private array", N);
9139 Set_Etype (N, Any_Type);
9140 return;
9141 end if;
9143 -- The validity of a null string has been checked in the call to
9144 -- Eval_String_Literal.
9146 if Strlen = 0 then
9147 return;
9149 -- Always accept string literal with component type Any_Character, which
9150 -- occurs in error situations and in comparisons of literals, both of
9151 -- which should accept all literals.
9153 elsif R_Typ = Any_Character then
9154 return;
9156 -- If the type is bit-packed, then we always transform the string
9157 -- literal into a full fledged aggregate.
9159 elsif Is_Bit_Packed_Array (Typ) then
9160 null;
9162 -- Deal with cases of Wide_Wide_String, Wide_String, and String
9164 else
9165 -- For Standard.Wide_Wide_String, or any other type whose component
9166 -- type is Standard.Wide_Wide_Character, we know that all the
9167 -- characters in the string must be acceptable, since the parser
9168 -- accepted the characters as valid character literals.
9170 if R_Typ = Standard_Wide_Wide_Character then
9171 null;
9173 -- For the case of Standard.String, or any other type whose component
9174 -- type is Standard.Character, we must make sure that there are no
9175 -- wide characters in the string, i.e. that it is entirely composed
9176 -- of characters in range of type Character.
9178 -- If the string literal is the result of a static concatenation, the
9179 -- test has already been performed on the components, and need not be
9180 -- repeated.
9182 elsif R_Typ = Standard_Character
9183 and then Nkind (Original_Node (N)) /= N_Op_Concat
9184 then
9185 for J in 1 .. Strlen loop
9186 if not In_Character_Range (Get_String_Char (Str, J)) then
9188 -- If we are out of range, post error. This is one of the
9189 -- very few places that we place the flag in the middle of
9190 -- a token, right under the offending wide character. Not
9191 -- quite clear if this is right wrt wide character encoding
9192 -- sequences, but it's only an error message!
9194 Error_Msg
9195 ("literal out of range of type Standard.Character",
9196 Source_Ptr (Int (Loc) + J));
9197 return;
9198 end if;
9199 end loop;
9201 -- For the case of Standard.Wide_String, or any other type whose
9202 -- component type is Standard.Wide_Character, we must make sure that
9203 -- there are no wide characters in the string, i.e. that it is
9204 -- entirely composed of characters in range of type Wide_Character.
9206 -- If the string literal is the result of a static concatenation,
9207 -- the test has already been performed on the components, and need
9208 -- not be repeated.
9210 elsif R_Typ = Standard_Wide_Character
9211 and then Nkind (Original_Node (N)) /= N_Op_Concat
9212 then
9213 for J in 1 .. Strlen loop
9214 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
9216 -- If we are out of range, post error. This is one of the
9217 -- very few places that we place the flag in the middle of
9218 -- a token, right under the offending wide character.
9220 -- This is not quite right, because characters in general
9221 -- will take more than one character position ???
9223 Error_Msg
9224 ("literal out of range of type Standard.Wide_Character",
9225 Source_Ptr (Int (Loc) + J));
9226 return;
9227 end if;
9228 end loop;
9230 -- If the root type is not a standard character, then we will convert
9231 -- the string into an aggregate and will let the aggregate code do
9232 -- the checking. Standard Wide_Wide_Character is also OK here.
9234 else
9235 null;
9236 end if;
9238 -- See if the component type of the array corresponding to the string
9239 -- has compile time known bounds. If yes we can directly check
9240 -- whether the evaluation of the string will raise constraint error.
9241 -- Otherwise we need to transform the string literal into the
9242 -- corresponding character aggregate and let the aggregate code do
9243 -- the checking.
9245 if Is_Standard_Character_Type (R_Typ) then
9247 -- Check for the case of full range, where we are definitely OK
9249 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
9250 return;
9251 end if;
9253 -- Here the range is not the complete base type range, so check
9255 declare
9256 Comp_Typ_Lo : constant Node_Id :=
9257 Type_Low_Bound (Component_Type (Typ));
9258 Comp_Typ_Hi : constant Node_Id :=
9259 Type_High_Bound (Component_Type (Typ));
9261 Char_Val : Uint;
9263 begin
9264 if Compile_Time_Known_Value (Comp_Typ_Lo)
9265 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9266 then
9267 for J in 1 .. Strlen loop
9268 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9270 if Char_Val < Expr_Value (Comp_Typ_Lo)
9271 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9272 then
9273 Apply_Compile_Time_Constraint_Error
9274 (N, "character out of range?", CE_Range_Check_Failed,
9275 Loc => Source_Ptr (Int (Loc) + J));
9276 end if;
9277 end loop;
9279 return;
9280 end if;
9281 end;
9282 end if;
9283 end if;
9285 -- If we got here we meed to transform the string literal into the
9286 -- equivalent qualified positional array aggregate. This is rather
9287 -- heavy artillery for this situation, but it is hard work to avoid.
9289 declare
9290 Lits : constant List_Id := New_List;
9291 P : Source_Ptr := Loc + 1;
9292 C : Char_Code;
9294 begin
9295 -- Build the character literals, we give them source locations that
9296 -- correspond to the string positions, which is a bit tricky given
9297 -- the possible presence of wide character escape sequences.
9299 for J in 1 .. Strlen loop
9300 C := Get_String_Char (Str, J);
9301 Set_Character_Literal_Name (C);
9303 Append_To (Lits,
9304 Make_Character_Literal (P,
9305 Chars => Name_Find,
9306 Char_Literal_Value => UI_From_CC (C)));
9308 if In_Character_Range (C) then
9309 P := P + 1;
9311 -- Should we have a call to Skip_Wide here ???
9313 -- ??? else
9314 -- Skip_Wide (P);
9316 end if;
9317 end loop;
9319 Rewrite (N,
9320 Make_Qualified_Expression (Loc,
9321 Subtype_Mark => New_Reference_To (Typ, Loc),
9322 Expression =>
9323 Make_Aggregate (Loc, Expressions => Lits)));
9325 Analyze_And_Resolve (N, Typ);
9326 end;
9327 end Resolve_String_Literal;
9329 -----------------------------
9330 -- Resolve_Subprogram_Info --
9331 -----------------------------
9333 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9334 begin
9335 Set_Etype (N, Typ);
9336 end Resolve_Subprogram_Info;
9338 -----------------------------
9339 -- Resolve_Type_Conversion --
9340 -----------------------------
9342 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9343 Conv_OK : constant Boolean := Conversion_OK (N);
9344 Operand : constant Node_Id := Expression (N);
9345 Operand_Typ : constant Entity_Id := Etype (Operand);
9346 Target_Typ : constant Entity_Id := Etype (N);
9347 Rop : Node_Id;
9348 Orig_N : Node_Id;
9349 Orig_T : Node_Id;
9351 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9352 -- Set to False to suppress cases where we want to suppress the test
9353 -- for redundancy to avoid possible false positives on this warning.
9355 begin
9356 if not Conv_OK
9357 and then not Valid_Conversion (N, Target_Typ, Operand)
9358 then
9359 return;
9360 end if;
9362 -- If the Operand Etype is Universal_Fixed, then the conversion is
9363 -- never redundant. We need this check because by the time we have
9364 -- finished the rather complex transformation, the conversion looks
9365 -- redundant when it is not.
9367 if Operand_Typ = Universal_Fixed then
9368 Test_Redundant := False;
9370 -- If the operand is marked as Any_Fixed, then special processing is
9371 -- required. This is also a case where we suppress the test for a
9372 -- redundant conversion, since most certainly it is not redundant.
9374 elsif Operand_Typ = Any_Fixed then
9375 Test_Redundant := False;
9377 -- Mixed-mode operation involving a literal. Context must be a fixed
9378 -- type which is applied to the literal subsequently.
9380 if Is_Fixed_Point_Type (Typ) then
9381 Set_Etype (Operand, Universal_Real);
9383 elsif Is_Numeric_Type (Typ)
9384 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9385 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9386 or else
9387 Etype (Left_Opnd (Operand)) = Universal_Real)
9388 then
9389 -- Return if expression is ambiguous
9391 if Unique_Fixed_Point_Type (N) = Any_Type then
9392 return;
9394 -- If nothing else, the available fixed type is Duration
9396 else
9397 Set_Etype (Operand, Standard_Duration);
9398 end if;
9400 -- Resolve the real operand with largest available precision
9402 if Etype (Right_Opnd (Operand)) = Universal_Real then
9403 Rop := New_Copy_Tree (Right_Opnd (Operand));
9404 else
9405 Rop := New_Copy_Tree (Left_Opnd (Operand));
9406 end if;
9408 Resolve (Rop, Universal_Real);
9410 -- If the operand is a literal (it could be a non-static and
9411 -- illegal exponentiation) check whether the use of Duration
9412 -- is potentially inaccurate.
9414 if Nkind (Rop) = N_Real_Literal
9415 and then Realval (Rop) /= Ureal_0
9416 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9417 then
9418 Error_Msg_N
9419 ("?universal real operand can only " &
9420 "be interpreted as Duration!",
9421 Rop);
9422 Error_Msg_N
9423 ("\?precision will be lost in the conversion!", Rop);
9424 end if;
9426 elsif Is_Numeric_Type (Typ)
9427 and then Nkind (Operand) in N_Op
9428 and then Unique_Fixed_Point_Type (N) /= Any_Type
9429 then
9430 Set_Etype (Operand, Standard_Duration);
9432 else
9433 Error_Msg_N ("invalid context for mixed mode operation", N);
9434 Set_Etype (Operand, Any_Type);
9435 return;
9436 end if;
9437 end if;
9439 Resolve (Operand);
9441 -- In SPARK, a type conversion between array types should be restricted
9442 -- to types which have matching static bounds.
9444 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9445 -- operation if not needed.
9447 if Restriction_Check_Required (SPARK)
9448 and then Is_Array_Type (Target_Typ)
9449 and then Is_Array_Type (Operand_Typ)
9450 and then Operand_Typ /= Any_Composite -- or else Operand in error
9451 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9452 then
9453 Check_SPARK_Restriction
9454 ("array types should have matching static bounds", N);
9455 end if;
9457 -- In formal mode, the operand of an ancestor type conversion must be an
9458 -- object (not an expression).
9460 if Is_Tagged_Type (Target_Typ)
9461 and then not Is_Class_Wide_Type (Target_Typ)
9462 and then Is_Tagged_Type (Operand_Typ)
9463 and then not Is_Class_Wide_Type (Operand_Typ)
9464 and then Is_Ancestor (Target_Typ, Operand_Typ)
9465 and then not Is_SPARK_Object_Reference (Operand)
9466 then
9467 Check_SPARK_Restriction ("object required", Operand);
9468 end if;
9470 Analyze_Dimension (N);
9472 -- Note: we do the Eval_Type_Conversion call before applying the
9473 -- required checks for a subtype conversion. This is important, since
9474 -- both are prepared under certain circumstances to change the type
9475 -- conversion to a constraint error node, but in the case of
9476 -- Eval_Type_Conversion this may reflect an illegality in the static
9477 -- case, and we would miss the illegality (getting only a warning
9478 -- message), if we applied the type conversion checks first.
9480 Eval_Type_Conversion (N);
9482 -- Even when evaluation is not possible, we may be able to simplify the
9483 -- conversion or its expression. This needs to be done before applying
9484 -- checks, since otherwise the checks may use the original expression
9485 -- and defeat the simplifications. This is specifically the case for
9486 -- elimination of the floating-point Truncation attribute in
9487 -- float-to-int conversions.
9489 Simplify_Type_Conversion (N);
9491 -- If after evaluation we still have a type conversion, then we may need
9492 -- to apply checks required for a subtype conversion.
9494 -- Skip these type conversion checks if universal fixed operands
9495 -- operands involved, since range checks are handled separately for
9496 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9498 if Nkind (N) = N_Type_Conversion
9499 and then not Is_Generic_Type (Root_Type (Target_Typ))
9500 and then Target_Typ /= Universal_Fixed
9501 and then Operand_Typ /= Universal_Fixed
9502 then
9503 Apply_Type_Conversion_Checks (N);
9504 end if;
9506 -- Issue warning for conversion of simple object to its own type. We
9507 -- have to test the original nodes, since they may have been rewritten
9508 -- by various optimizations.
9510 Orig_N := Original_Node (N);
9512 -- Here we test for a redundant conversion if the warning mode is
9513 -- active (and was not locally reset), and we have a type conversion
9514 -- from source not appearing in a generic instance.
9516 if Test_Redundant
9517 and then Nkind (Orig_N) = N_Type_Conversion
9518 and then Comes_From_Source (Orig_N)
9519 and then not In_Instance
9520 then
9521 Orig_N := Original_Node (Expression (Orig_N));
9522 Orig_T := Target_Typ;
9524 -- If the node is part of a larger expression, the Target_Type
9525 -- may not be the original type of the node if the context is a
9526 -- condition. Recover original type to see if conversion is needed.
9528 if Is_Boolean_Type (Orig_T)
9529 and then Nkind (Parent (N)) in N_Op
9530 then
9531 Orig_T := Etype (Parent (N));
9532 end if;
9534 -- If we have an entity name, then give the warning if the entity
9535 -- is the right type, or if it is a loop parameter covered by the
9536 -- original type (that's needed because loop parameters have an
9537 -- odd subtype coming from the bounds).
9539 if (Is_Entity_Name (Orig_N)
9540 and then
9541 (Etype (Entity (Orig_N)) = Orig_T
9542 or else
9543 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9544 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9546 -- If not an entity, then type of expression must match
9548 or else Etype (Orig_N) = Orig_T
9549 then
9550 -- One more check, do not give warning if the analyzed conversion
9551 -- has an expression with non-static bounds, and the bounds of the
9552 -- target are static. This avoids junk warnings in cases where the
9553 -- conversion is necessary to establish staticness, for example in
9554 -- a case statement.
9556 if not Is_OK_Static_Subtype (Operand_Typ)
9557 and then Is_OK_Static_Subtype (Target_Typ)
9558 then
9559 null;
9561 -- Finally, if this type conversion occurs in a context requiring
9562 -- a prefix, and the expression is a qualified expression then the
9563 -- type conversion is not redundant, since a qualified expression
9564 -- is not a prefix, whereas a type conversion is. For example, "X
9565 -- := T'(Funx(...)).Y;" is illegal because a selected component
9566 -- requires a prefix, but a type conversion makes it legal: "X :=
9567 -- T(T'(Funx(...))).Y;"
9569 -- In Ada 2012, a qualified expression is a name, so this idiom is
9570 -- no longer needed, but we still suppress the warning because it
9571 -- seems unfriendly for warnings to pop up when you switch to the
9572 -- newer language version.
9574 elsif Nkind (Orig_N) = N_Qualified_Expression
9575 and then Nkind_In (Parent (N), N_Attribute_Reference,
9576 N_Indexed_Component,
9577 N_Selected_Component,
9578 N_Slice,
9579 N_Explicit_Dereference)
9580 then
9581 null;
9583 -- Here we give the redundant conversion warning. If it is an
9584 -- entity, give the name of the entity in the message. If not,
9585 -- just mention the expression.
9587 else
9588 if Is_Entity_Name (Orig_N) then
9589 Error_Msg_Node_2 := Orig_T;
9590 Error_Msg_NE -- CODEFIX
9591 ("?redundant conversion, & is of type &!",
9592 N, Entity (Orig_N));
9593 else
9594 Error_Msg_NE
9595 ("?redundant conversion, expression is of type&!",
9596 N, Orig_T);
9597 end if;
9598 end if;
9599 end if;
9600 end if;
9602 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9603 -- No need to perform any interface conversion if the type of the
9604 -- expression coincides with the target type.
9606 if Ada_Version >= Ada_2005
9607 and then Full_Expander_Active
9608 and then Operand_Typ /= Target_Typ
9609 then
9610 declare
9611 Opnd : Entity_Id := Operand_Typ;
9612 Target : Entity_Id := Target_Typ;
9614 begin
9615 if Is_Access_Type (Opnd) then
9616 Opnd := Designated_Type (Opnd);
9617 end if;
9619 if Is_Access_Type (Target_Typ) then
9620 Target := Designated_Type (Target);
9621 end if;
9623 if Opnd = Target then
9624 null;
9626 -- Conversion from interface type
9628 elsif Is_Interface (Opnd) then
9630 -- Ada 2005 (AI-217): Handle entities from limited views
9632 if From_With_Type (Opnd) then
9633 Error_Msg_Qual_Level := 99;
9634 Error_Msg_NE -- CODEFIX
9635 ("missing WITH clause on package &", N,
9636 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9637 Error_Msg_N
9638 ("type conversions require visibility of the full view",
9641 elsif From_With_Type (Target)
9642 and then not
9643 (Is_Access_Type (Target_Typ)
9644 and then Present (Non_Limited_View (Etype (Target))))
9645 then
9646 Error_Msg_Qual_Level := 99;
9647 Error_Msg_NE -- CODEFIX
9648 ("missing WITH clause on package &", N,
9649 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9650 Error_Msg_N
9651 ("type conversions require visibility of the full view",
9654 else
9655 Expand_Interface_Conversion (N, Is_Static => False);
9656 end if;
9658 -- Conversion to interface type
9660 elsif Is_Interface (Target) then
9662 -- Handle subtypes
9664 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9665 Opnd := Etype (Opnd);
9666 end if;
9668 if not Interface_Present_In_Ancestor
9669 (Typ => Opnd,
9670 Iface => Target)
9671 then
9672 if Is_Class_Wide_Type (Opnd) then
9674 -- The static analysis is not enough to know if the
9675 -- interface is implemented or not. Hence we must pass
9676 -- the work to the expander to generate code to evaluate
9677 -- the conversion at run time.
9679 Expand_Interface_Conversion (N, Is_Static => False);
9681 else
9682 Error_Msg_Name_1 := Chars (Etype (Target));
9683 Error_Msg_Name_2 := Chars (Opnd);
9684 Error_Msg_N
9685 ("wrong interface conversion (% is not a progenitor " &
9686 "of %)", N);
9687 end if;
9689 else
9690 Expand_Interface_Conversion (N);
9691 end if;
9692 end if;
9693 end;
9694 end if;
9695 end Resolve_Type_Conversion;
9697 ----------------------
9698 -- Resolve_Unary_Op --
9699 ----------------------
9701 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9702 B_Typ : constant Entity_Id := Base_Type (Typ);
9703 R : constant Node_Id := Right_Opnd (N);
9704 OK : Boolean;
9705 Lo : Uint;
9706 Hi : Uint;
9708 begin
9709 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9710 Error_Msg_Name_1 := Chars (Typ);
9711 Check_SPARK_Restriction
9712 ("unary operator not defined for modular type%", N);
9713 end if;
9715 -- Deal with intrinsic unary operators
9717 if Comes_From_Source (N)
9718 and then Ekind (Entity (N)) = E_Function
9719 and then Is_Imported (Entity (N))
9720 and then Is_Intrinsic_Subprogram (Entity (N))
9721 then
9722 Resolve_Intrinsic_Unary_Operator (N, Typ);
9723 return;
9724 end if;
9726 -- Deal with universal cases
9728 if Etype (R) = Universal_Integer
9729 or else
9730 Etype (R) = Universal_Real
9731 then
9732 Check_For_Visible_Operator (N, B_Typ);
9733 end if;
9735 Set_Etype (N, B_Typ);
9736 Resolve (R, B_Typ);
9738 -- Generate warning for expressions like abs (x mod 2)
9740 if Warn_On_Redundant_Constructs
9741 and then Nkind (N) = N_Op_Abs
9742 then
9743 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9745 if OK and then Hi >= Lo and then Lo >= 0 then
9746 Error_Msg_N -- CODEFIX
9747 ("?abs applied to known non-negative value has no effect", N);
9748 end if;
9749 end if;
9751 -- Deal with reference generation
9753 Check_Unset_Reference (R);
9754 Generate_Operator_Reference (N, B_Typ);
9755 Analyze_Dimension (N);
9756 Eval_Unary_Op (N);
9758 -- Set overflow checking bit. Much cleverer code needed here eventually
9759 -- and perhaps the Resolve routines should be separated for the various
9760 -- arithmetic operations, since they will need different processing ???
9762 if Nkind (N) in N_Op then
9763 if not Overflow_Checks_Suppressed (Etype (N)) then
9764 Enable_Overflow_Check (N);
9765 end if;
9766 end if;
9768 -- Generate warning for expressions like -5 mod 3 for integers. No need
9769 -- to worry in the floating-point case, since parens do not affect the
9770 -- result so there is no point in giving in a warning.
9772 declare
9773 Norig : constant Node_Id := Original_Node (N);
9774 Rorig : Node_Id;
9775 Val : Uint;
9776 HB : Uint;
9777 LB : Uint;
9778 Lval : Uint;
9779 Opnd : Node_Id;
9781 begin
9782 if Warn_On_Questionable_Missing_Parens
9783 and then Comes_From_Source (Norig)
9784 and then Is_Integer_Type (Typ)
9785 and then Nkind (Norig) = N_Op_Minus
9786 then
9787 Rorig := Original_Node (Right_Opnd (Norig));
9789 -- We are looking for cases where the right operand is not
9790 -- parenthesized, and is a binary operator, multiply, divide, or
9791 -- mod. These are the cases where the grouping can affect results.
9793 if Paren_Count (Rorig) = 0
9794 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
9795 then
9796 -- For mod, we always give the warning, since the value is
9797 -- affected by the parenthesization (e.g. (-5) mod 315 /=
9798 -- -(5 mod 315)). But for the other cases, the only concern is
9799 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
9800 -- overflows, but (-2) * 64 does not). So we try to give the
9801 -- message only when overflow is possible.
9803 if Nkind (Rorig) /= N_Op_Mod
9804 and then Compile_Time_Known_Value (R)
9805 then
9806 Val := Expr_Value (R);
9808 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9809 HB := Expr_Value (Type_High_Bound (Typ));
9810 else
9811 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
9812 end if;
9814 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9815 LB := Expr_Value (Type_Low_Bound (Typ));
9816 else
9817 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
9818 end if;
9820 -- Note that the test below is deliberately excluding the
9821 -- largest negative number, since that is a potentially
9822 -- troublesome case (e.g. -2 * x, where the result is the
9823 -- largest negative integer has an overflow with 2 * x).
9825 if Val > LB and then Val <= HB then
9826 return;
9827 end if;
9828 end if;
9830 -- For the multiplication case, the only case we have to worry
9831 -- about is when (-a)*b is exactly the largest negative number
9832 -- so that -(a*b) can cause overflow. This can only happen if
9833 -- a is a power of 2, and more generally if any operand is a
9834 -- constant that is not a power of 2, then the parentheses
9835 -- cannot affect whether overflow occurs. We only bother to
9836 -- test the left most operand
9838 -- Loop looking at left operands for one that has known value
9840 Opnd := Rorig;
9841 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9842 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9843 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9845 -- Operand value of 0 or 1 skips warning
9847 if Lval <= 1 then
9848 return;
9850 -- Otherwise check power of 2, if power of 2, warn, if
9851 -- anything else, skip warning.
9853 else
9854 while Lval /= 2 loop
9855 if Lval mod 2 = 1 then
9856 return;
9857 else
9858 Lval := Lval / 2;
9859 end if;
9860 end loop;
9862 exit Opnd_Loop;
9863 end if;
9864 end if;
9866 -- Keep looking at left operands
9868 Opnd := Left_Opnd (Opnd);
9869 end loop Opnd_Loop;
9871 -- For rem or "/" we can only have a problematic situation
9872 -- if the divisor has a value of minus one or one. Otherwise
9873 -- overflow is impossible (divisor > 1) or we have a case of
9874 -- division by zero in any case.
9876 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9877 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9878 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9879 then
9880 return;
9881 end if;
9883 -- If we fall through warning should be issued
9885 Error_Msg_N
9886 ("?unary minus expression should be parenthesized here!", N);
9887 end if;
9888 end if;
9889 end;
9890 end Resolve_Unary_Op;
9892 ----------------------------------
9893 -- Resolve_Unchecked_Expression --
9894 ----------------------------------
9896 procedure Resolve_Unchecked_Expression
9897 (N : Node_Id;
9898 Typ : Entity_Id)
9900 begin
9901 Resolve (Expression (N), Typ, Suppress => All_Checks);
9902 Set_Etype (N, Typ);
9903 end Resolve_Unchecked_Expression;
9905 ---------------------------------------
9906 -- Resolve_Unchecked_Type_Conversion --
9907 ---------------------------------------
9909 procedure Resolve_Unchecked_Type_Conversion
9910 (N : Node_Id;
9911 Typ : Entity_Id)
9913 pragma Warnings (Off, Typ);
9915 Operand : constant Node_Id := Expression (N);
9916 Opnd_Type : constant Entity_Id := Etype (Operand);
9918 begin
9919 -- Resolve operand using its own type
9921 Resolve (Operand, Opnd_Type);
9922 Analyze_Dimension (N);
9923 Eval_Unchecked_Conversion (N);
9924 end Resolve_Unchecked_Type_Conversion;
9926 ------------------------------
9927 -- Rewrite_Operator_As_Call --
9928 ------------------------------
9930 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9931 Loc : constant Source_Ptr := Sloc (N);
9932 Actuals : constant List_Id := New_List;
9933 New_N : Node_Id;
9935 begin
9936 if Nkind (N) in N_Binary_Op then
9937 Append (Left_Opnd (N), Actuals);
9938 end if;
9940 Append (Right_Opnd (N), Actuals);
9942 New_N :=
9943 Make_Function_Call (Sloc => Loc,
9944 Name => New_Occurrence_Of (Nam, Loc),
9945 Parameter_Associations => Actuals);
9947 Preserve_Comes_From_Source (New_N, N);
9948 Preserve_Comes_From_Source (Name (New_N), N);
9949 Rewrite (N, New_N);
9950 Set_Etype (N, Etype (Nam));
9951 end Rewrite_Operator_As_Call;
9953 ------------------------------
9954 -- Rewrite_Renamed_Operator --
9955 ------------------------------
9957 procedure Rewrite_Renamed_Operator
9958 (N : Node_Id;
9959 Op : Entity_Id;
9960 Typ : Entity_Id)
9962 Nam : constant Name_Id := Chars (Op);
9963 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9964 Op_Node : Node_Id;
9966 begin
9967 -- Rewrite the operator node using the real operator, not its renaming.
9968 -- Exclude user-defined intrinsic operations of the same name, which are
9969 -- treated separately and rewritten as calls.
9971 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9972 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9973 Set_Chars (Op_Node, Nam);
9974 Set_Etype (Op_Node, Etype (N));
9975 Set_Entity (Op_Node, Op);
9976 Set_Right_Opnd (Op_Node, Right_Opnd (N));
9978 -- Indicate that both the original entity and its renaming are
9979 -- referenced at this point.
9981 Generate_Reference (Entity (N), N);
9982 Generate_Reference (Op, N);
9984 if Is_Binary then
9985 Set_Left_Opnd (Op_Node, Left_Opnd (N));
9986 end if;
9988 Rewrite (N, Op_Node);
9990 -- If the context type is private, add the appropriate conversions so
9991 -- that the operator is applied to the full view. This is done in the
9992 -- routines that resolve intrinsic operators.
9994 if Is_Intrinsic_Subprogram (Op)
9995 and then Is_Private_Type (Typ)
9996 then
9997 case Nkind (N) is
9998 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9999 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
10000 Resolve_Intrinsic_Operator (N, Typ);
10002 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
10003 Resolve_Intrinsic_Unary_Operator (N, Typ);
10005 when others =>
10006 Resolve (N, Typ);
10007 end case;
10008 end if;
10010 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
10012 -- Operator renames a user-defined operator of the same name. Use the
10013 -- original operator in the node, which is the one Gigi knows about.
10015 Set_Entity (N, Op);
10016 Set_Is_Overloaded (N, False);
10017 end if;
10018 end Rewrite_Renamed_Operator;
10020 -----------------------
10021 -- Set_Slice_Subtype --
10022 -----------------------
10024 -- Build an implicit subtype declaration to represent the type delivered by
10025 -- the slice. This is an abbreviated version of an array subtype. We define
10026 -- an index subtype for the slice, using either the subtype name or the
10027 -- discrete range of the slice. To be consistent with index usage elsewhere
10028 -- we create a list header to hold the single index. This list is not
10029 -- otherwise attached to the syntax tree.
10031 procedure Set_Slice_Subtype (N : Node_Id) is
10032 Loc : constant Source_Ptr := Sloc (N);
10033 Index_List : constant List_Id := New_List;
10034 Index : Node_Id;
10035 Index_Subtype : Entity_Id;
10036 Index_Type : Entity_Id;
10037 Slice_Subtype : Entity_Id;
10038 Drange : constant Node_Id := Discrete_Range (N);
10040 begin
10041 if Is_Entity_Name (Drange) then
10042 Index_Subtype := Entity (Drange);
10044 else
10045 -- We force the evaluation of a range. This is definitely needed in
10046 -- the renamed case, and seems safer to do unconditionally. Note in
10047 -- any case that since we will create and insert an Itype referring
10048 -- to this range, we must make sure any side effect removal actions
10049 -- are inserted before the Itype definition.
10051 if Nkind (Drange) = N_Range then
10052 Force_Evaluation (Low_Bound (Drange));
10053 Force_Evaluation (High_Bound (Drange));
10054 end if;
10056 Index_Type := Base_Type (Etype (Drange));
10058 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10060 -- Take a new copy of Drange (where bounds have been rewritten to
10061 -- reference side-effect-free names). Using a separate tree ensures
10062 -- that further expansion (e.g. while rewriting a slice assignment
10063 -- into a FOR loop) does not attempt to remove side effects on the
10064 -- bounds again (which would cause the bounds in the index subtype
10065 -- definition to refer to temporaries before they are defined) (the
10066 -- reason is that some names are considered side effect free here
10067 -- for the subtype, but not in the context of a loop iteration
10068 -- scheme).
10070 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
10071 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
10072 Set_Etype (Index_Subtype, Index_Type);
10073 Set_Size_Info (Index_Subtype, Index_Type);
10074 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10075 end if;
10077 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
10079 Index := New_Occurrence_Of (Index_Subtype, Loc);
10080 Set_Etype (Index, Index_Subtype);
10081 Append (Index, Index_List);
10083 Set_First_Index (Slice_Subtype, Index);
10084 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
10085 Set_Is_Constrained (Slice_Subtype, True);
10087 Check_Compile_Time_Size (Slice_Subtype);
10089 -- The Etype of the existing Slice node is reset to this slice subtype.
10090 -- Its bounds are obtained from its first index.
10092 Set_Etype (N, Slice_Subtype);
10094 -- For packed slice subtypes, freeze immediately (except in the case of
10095 -- being in a "spec expression" where we never freeze when we first see
10096 -- the expression).
10098 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
10099 Freeze_Itype (Slice_Subtype, N);
10101 -- For all other cases insert an itype reference in the slice's actions
10102 -- so that the itype is frozen at the proper place in the tree (i.e. at
10103 -- the point where actions for the slice are analyzed). Note that this
10104 -- is different from freezing the itype immediately, which might be
10105 -- premature (e.g. if the slice is within a transient scope). This needs
10106 -- to be done only if expansion is enabled.
10108 elsif Full_Expander_Active then
10109 Ensure_Defined (Typ => Slice_Subtype, N => N);
10110 end if;
10111 end Set_Slice_Subtype;
10113 --------------------------------
10114 -- Set_String_Literal_Subtype --
10115 --------------------------------
10117 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
10118 Loc : constant Source_Ptr := Sloc (N);
10119 Low_Bound : constant Node_Id :=
10120 Type_Low_Bound (Etype (First_Index (Typ)));
10121 Subtype_Id : Entity_Id;
10123 begin
10124 if Nkind (N) /= N_String_Literal then
10125 return;
10126 end if;
10128 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
10129 Set_String_Literal_Length (Subtype_Id, UI_From_Int
10130 (String_Length (Strval (N))));
10131 Set_Etype (Subtype_Id, Base_Type (Typ));
10132 Set_Is_Constrained (Subtype_Id);
10133 Set_Etype (N, Subtype_Id);
10135 -- The low bound is set from the low bound of the corresponding index
10136 -- type. Note that we do not store the high bound in the string literal
10137 -- subtype, but it can be deduced if necessary from the length and the
10138 -- low bound.
10140 if Is_OK_Static_Expression (Low_Bound) then
10141 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
10143 -- If the lower bound is not static we create a range for the string
10144 -- literal, using the index type and the known length of the literal.
10145 -- The index type is not necessarily Positive, so the upper bound is
10146 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
10148 else
10149 declare
10150 Index_List : constant List_Id := New_List;
10151 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
10152 High_Bound : constant Node_Id :=
10153 Make_Attribute_Reference (Loc,
10154 Attribute_Name => Name_Val,
10155 Prefix =>
10156 New_Occurrence_Of (Index_Type, Loc),
10157 Expressions => New_List (
10158 Make_Op_Add (Loc,
10159 Left_Opnd =>
10160 Make_Attribute_Reference (Loc,
10161 Attribute_Name => Name_Pos,
10162 Prefix =>
10163 New_Occurrence_Of (Index_Type, Loc),
10164 Expressions =>
10165 New_List (New_Copy_Tree (Low_Bound))),
10166 Right_Opnd =>
10167 Make_Integer_Literal (Loc,
10168 String_Length (Strval (N)) - 1))));
10170 Array_Subtype : Entity_Id;
10171 Drange : Node_Id;
10172 Index : Node_Id;
10173 Index_Subtype : Entity_Id;
10175 begin
10176 if Is_Integer_Type (Index_Type) then
10177 Set_String_Literal_Low_Bound
10178 (Subtype_Id, Make_Integer_Literal (Loc, 1));
10180 else
10181 -- If the index type is an enumeration type, build bounds
10182 -- expression with attributes.
10184 Set_String_Literal_Low_Bound
10185 (Subtype_Id,
10186 Make_Attribute_Reference (Loc,
10187 Attribute_Name => Name_First,
10188 Prefix =>
10189 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
10190 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
10191 end if;
10193 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
10195 -- Build bona fide subtype for the string, and wrap it in an
10196 -- unchecked conversion, because the backend expects the
10197 -- String_Literal_Subtype to have a static lower bound.
10199 Index_Subtype :=
10200 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10201 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
10202 Set_Scalar_Range (Index_Subtype, Drange);
10203 Set_Parent (Drange, N);
10204 Analyze_And_Resolve (Drange, Index_Type);
10206 -- In the context, the Index_Type may already have a constraint,
10207 -- so use common base type on string subtype. The base type may
10208 -- be used when generating attributes of the string, for example
10209 -- in the context of a slice assignment.
10211 Set_Etype (Index_Subtype, Base_Type (Index_Type));
10212 Set_Size_Info (Index_Subtype, Index_Type);
10213 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10215 Array_Subtype := Create_Itype (E_Array_Subtype, N);
10217 Index := New_Occurrence_Of (Index_Subtype, Loc);
10218 Set_Etype (Index, Index_Subtype);
10219 Append (Index, Index_List);
10221 Set_First_Index (Array_Subtype, Index);
10222 Set_Etype (Array_Subtype, Base_Type (Typ));
10223 Set_Is_Constrained (Array_Subtype, True);
10225 Rewrite (N,
10226 Make_Unchecked_Type_Conversion (Loc,
10227 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
10228 Expression => Relocate_Node (N)));
10229 Set_Etype (N, Array_Subtype);
10230 end;
10231 end if;
10232 end Set_String_Literal_Subtype;
10234 ------------------------------
10235 -- Simplify_Type_Conversion --
10236 ------------------------------
10238 procedure Simplify_Type_Conversion (N : Node_Id) is
10239 begin
10240 if Nkind (N) = N_Type_Conversion then
10241 declare
10242 Operand : constant Node_Id := Expression (N);
10243 Target_Typ : constant Entity_Id := Etype (N);
10244 Opnd_Typ : constant Entity_Id := Etype (Operand);
10246 begin
10247 if Is_Floating_Point_Type (Opnd_Typ)
10248 and then
10249 (Is_Integer_Type (Target_Typ)
10250 or else (Is_Fixed_Point_Type (Target_Typ)
10251 and then Conversion_OK (N)))
10252 and then Nkind (Operand) = N_Attribute_Reference
10253 and then Attribute_Name (Operand) = Name_Truncation
10255 -- Special processing required if the conversion is the expression
10256 -- of a Truncation attribute reference. In this case we replace:
10258 -- ityp (ftyp'Truncation (x))
10260 -- by
10262 -- ityp (x)
10264 -- with the Float_Truncate flag set, which is more efficient.
10266 then
10267 Rewrite (Operand,
10268 Relocate_Node (First (Expressions (Operand))));
10269 Set_Float_Truncate (N, True);
10270 end if;
10271 end;
10272 end if;
10273 end Simplify_Type_Conversion;
10275 -----------------------------
10276 -- Unique_Fixed_Point_Type --
10277 -----------------------------
10279 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10280 T1 : Entity_Id := Empty;
10281 T2 : Entity_Id;
10282 Item : Node_Id;
10283 Scop : Entity_Id;
10285 procedure Fixed_Point_Error;
10286 -- Give error messages for true ambiguity. Messages are posted on node
10287 -- N, and entities T1, T2 are the possible interpretations.
10289 -----------------------
10290 -- Fixed_Point_Error --
10291 -----------------------
10293 procedure Fixed_Point_Error is
10294 begin
10295 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10296 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10297 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10298 end Fixed_Point_Error;
10300 -- Start of processing for Unique_Fixed_Point_Type
10302 begin
10303 -- The operations on Duration are visible, so Duration is always a
10304 -- possible interpretation.
10306 T1 := Standard_Duration;
10308 -- Look for fixed-point types in enclosing scopes
10310 Scop := Current_Scope;
10311 while Scop /= Standard_Standard loop
10312 T2 := First_Entity (Scop);
10313 while Present (T2) loop
10314 if Is_Fixed_Point_Type (T2)
10315 and then Current_Entity (T2) = T2
10316 and then Scope (Base_Type (T2)) = Scop
10317 then
10318 if Present (T1) then
10319 Fixed_Point_Error;
10320 return Any_Type;
10321 else
10322 T1 := T2;
10323 end if;
10324 end if;
10326 Next_Entity (T2);
10327 end loop;
10329 Scop := Scope (Scop);
10330 end loop;
10332 -- Look for visible fixed type declarations in the context
10334 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10335 while Present (Item) loop
10336 if Nkind (Item) = N_With_Clause then
10337 Scop := Entity (Name (Item));
10338 T2 := First_Entity (Scop);
10339 while Present (T2) loop
10340 if Is_Fixed_Point_Type (T2)
10341 and then Scope (Base_Type (T2)) = Scop
10342 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10343 then
10344 if Present (T1) then
10345 Fixed_Point_Error;
10346 return Any_Type;
10347 else
10348 T1 := T2;
10349 end if;
10350 end if;
10352 Next_Entity (T2);
10353 end loop;
10354 end if;
10356 Next (Item);
10357 end loop;
10359 if Nkind (N) = N_Real_Literal then
10360 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
10361 else
10362 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
10363 end if;
10365 return T1;
10366 end Unique_Fixed_Point_Type;
10368 ----------------------
10369 -- Valid_Conversion --
10370 ----------------------
10372 function Valid_Conversion
10373 (N : Node_Id;
10374 Target : Entity_Id;
10375 Operand : Node_Id;
10376 Report_Errs : Boolean := True) return Boolean
10378 Target_Type : constant Entity_Id := Base_Type (Target);
10379 Opnd_Type : Entity_Id := Etype (Operand);
10381 function Conversion_Check
10382 (Valid : Boolean;
10383 Msg : String) return Boolean;
10384 -- Little routine to post Msg if Valid is False, returns Valid value
10386 -- The following are badly named, this kind of overloading is actively
10387 -- confusing in reading code, please rename to something like
10388 -- Error_Msg_N_If_Reporting ???
10390 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id);
10391 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10393 procedure Error_Msg_NE
10394 (Msg : String;
10395 N : Node_Or_Entity_Id;
10396 E : Node_Or_Entity_Id);
10397 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10399 function Valid_Tagged_Conversion
10400 (Target_Type : Entity_Id;
10401 Opnd_Type : Entity_Id) return Boolean;
10402 -- Specifically test for validity of tagged conversions
10404 function Valid_Array_Conversion return Boolean;
10405 -- Check index and component conformance, and accessibility levels if
10406 -- the component types are anonymous access types (Ada 2005).
10408 ----------------------
10409 -- Conversion_Check --
10410 ----------------------
10412 function Conversion_Check
10413 (Valid : Boolean;
10414 Msg : String) return Boolean
10416 begin
10417 if not Valid
10419 -- A generic unit has already been analyzed and we have verified
10420 -- that a particular conversion is OK in that context. Since the
10421 -- instance is reanalyzed without relying on the relationships
10422 -- established during the analysis of the generic, it is possible
10423 -- to end up with inconsistent views of private types. Do not emit
10424 -- the error message in such cases. The rest of the machinery in
10425 -- Valid_Conversion still ensures the proper compatibility of
10426 -- target and operand types.
10428 and then not In_Instance
10429 then
10430 Error_Msg_N (Msg, Operand);
10431 end if;
10433 return Valid;
10434 end Conversion_Check;
10436 -----------------
10437 -- Error_Msg_N --
10438 -----------------
10440 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id) is
10441 begin
10442 if Report_Errs then
10443 Errout.Error_Msg_N (Msg, N);
10444 end if;
10445 end Error_Msg_N;
10447 ------------------
10448 -- Error_Msg_NE --
10449 ------------------
10451 procedure Error_Msg_NE
10452 (Msg : String;
10453 N : Node_Or_Entity_Id;
10454 E : Node_Or_Entity_Id)
10456 begin
10457 if Report_Errs then
10458 Errout.Error_Msg_NE (Msg, N, E);
10459 end if;
10460 end Error_Msg_NE;
10462 ----------------------------
10463 -- Valid_Array_Conversion --
10464 ----------------------------
10466 function Valid_Array_Conversion return Boolean
10468 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10469 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10471 Opnd_Index : Node_Id;
10472 Opnd_Index_Type : Entity_Id;
10474 Target_Comp_Type : constant Entity_Id :=
10475 Component_Type (Target_Type);
10476 Target_Comp_Base : constant Entity_Id :=
10477 Base_Type (Target_Comp_Type);
10479 Target_Index : Node_Id;
10480 Target_Index_Type : Entity_Id;
10482 begin
10483 -- Error if wrong number of dimensions
10486 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10487 then
10488 Error_Msg_N
10489 ("incompatible number of dimensions for conversion", Operand);
10490 return False;
10492 -- Number of dimensions matches
10494 else
10495 -- Loop through indexes of the two arrays
10497 Target_Index := First_Index (Target_Type);
10498 Opnd_Index := First_Index (Opnd_Type);
10499 while Present (Target_Index) and then Present (Opnd_Index) loop
10500 Target_Index_Type := Etype (Target_Index);
10501 Opnd_Index_Type := Etype (Opnd_Index);
10503 -- Error if index types are incompatible
10505 if not (Is_Integer_Type (Target_Index_Type)
10506 and then Is_Integer_Type (Opnd_Index_Type))
10507 and then (Root_Type (Target_Index_Type)
10508 /= Root_Type (Opnd_Index_Type))
10509 then
10510 Error_Msg_N
10511 ("incompatible index types for array conversion",
10512 Operand);
10513 return False;
10514 end if;
10516 Next_Index (Target_Index);
10517 Next_Index (Opnd_Index);
10518 end loop;
10520 -- If component types have same base type, all set
10522 if Target_Comp_Base = Opnd_Comp_Base then
10523 null;
10525 -- Here if base types of components are not the same. The only
10526 -- time this is allowed is if we have anonymous access types.
10528 -- The conversion of arrays of anonymous access types can lead
10529 -- to dangling pointers. AI-392 formalizes the accessibility
10530 -- checks that must be applied to such conversions to prevent
10531 -- out-of-scope references.
10533 elsif Ekind_In
10534 (Target_Comp_Base, E_Anonymous_Access_Type,
10535 E_Anonymous_Access_Subprogram_Type)
10536 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10537 and then
10538 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10539 then
10540 if Type_Access_Level (Target_Type) <
10541 Deepest_Type_Access_Level (Opnd_Type)
10542 then
10543 if In_Instance_Body then
10544 Error_Msg_N
10545 ("?source array type has " &
10546 "deeper accessibility level than target", Operand);
10547 Error_Msg_N
10548 ("\?Program_Error will be raised at run time",
10549 Operand);
10550 Rewrite (N,
10551 Make_Raise_Program_Error (Sloc (N),
10552 Reason => PE_Accessibility_Check_Failed));
10553 Set_Etype (N, Target_Type);
10554 return False;
10556 -- Conversion not allowed because of accessibility levels
10558 else
10559 Error_Msg_N
10560 ("source array type has " &
10561 "deeper accessibility level than target", Operand);
10562 return False;
10563 end if;
10565 else
10566 null;
10567 end if;
10569 -- All other cases where component base types do not match
10571 else
10572 Error_Msg_N
10573 ("incompatible component types for array conversion",
10574 Operand);
10575 return False;
10576 end if;
10578 -- Check that component subtypes statically match. For numeric
10579 -- types this means that both must be either constrained or
10580 -- unconstrained. For enumeration types the bounds must match.
10581 -- All of this is checked in Subtypes_Statically_Match.
10583 if not Subtypes_Statically_Match
10584 (Target_Comp_Type, Opnd_Comp_Type)
10585 then
10586 Error_Msg_N
10587 ("component subtypes must statically match", Operand);
10588 return False;
10589 end if;
10590 end if;
10592 return True;
10593 end Valid_Array_Conversion;
10595 -----------------------------
10596 -- Valid_Tagged_Conversion --
10597 -----------------------------
10599 function Valid_Tagged_Conversion
10600 (Target_Type : Entity_Id;
10601 Opnd_Type : Entity_Id) return Boolean
10603 begin
10604 -- Upward conversions are allowed (RM 4.6(22))
10606 if Covers (Target_Type, Opnd_Type)
10607 or else Is_Ancestor (Target_Type, Opnd_Type)
10608 then
10609 return True;
10611 -- Downward conversion are allowed if the operand is class-wide
10612 -- (RM 4.6(23)).
10614 elsif Is_Class_Wide_Type (Opnd_Type)
10615 and then Covers (Opnd_Type, Target_Type)
10616 then
10617 return True;
10619 elsif Covers (Opnd_Type, Target_Type)
10620 or else Is_Ancestor (Opnd_Type, Target_Type)
10621 then
10622 return
10623 Conversion_Check (False,
10624 "downward conversion of tagged objects not allowed");
10626 -- Ada 2005 (AI-251): The conversion to/from interface types is
10627 -- always valid
10629 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10630 return True;
10632 -- If the operand is a class-wide type obtained through a limited_
10633 -- with clause, and the context includes the non-limited view, use
10634 -- it to determine whether the conversion is legal.
10636 elsif Is_Class_Wide_Type (Opnd_Type)
10637 and then From_With_Type (Opnd_Type)
10638 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10639 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10640 then
10641 return True;
10643 elsif Is_Access_Type (Opnd_Type)
10644 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10645 then
10646 return True;
10648 else
10649 Error_Msg_NE
10650 ("invalid tagged conversion, not compatible with}",
10651 N, First_Subtype (Opnd_Type));
10652 return False;
10653 end if;
10654 end Valid_Tagged_Conversion;
10656 -- Start of processing for Valid_Conversion
10658 begin
10659 Check_Parameterless_Call (Operand);
10661 if Is_Overloaded (Operand) then
10662 declare
10663 I : Interp_Index;
10664 I1 : Interp_Index;
10665 It : Interp;
10666 It1 : Interp;
10667 N1 : Entity_Id;
10668 T1 : Entity_Id;
10670 begin
10671 -- Remove procedure calls, which syntactically cannot appear in
10672 -- this context, but which cannot be removed by type checking,
10673 -- because the context does not impose a type.
10675 -- When compiling for VMS, spurious ambiguities can be produced
10676 -- when arithmetic operations have a literal operand and return
10677 -- System.Address or a descendant of it. These ambiguities are
10678 -- otherwise resolved by the context, but for conversions there
10679 -- is no context type and the removal of the spurious operations
10680 -- must be done explicitly here.
10682 -- The node may be labelled overloaded, but still contain only one
10683 -- interpretation because others were discarded earlier. If this
10684 -- is the case, retain the single interpretation if legal.
10686 Get_First_Interp (Operand, I, It);
10687 Opnd_Type := It.Typ;
10688 Get_Next_Interp (I, It);
10690 if Present (It.Typ)
10691 and then Opnd_Type /= Standard_Void_Type
10692 then
10693 -- More than one candidate interpretation is available
10695 Get_First_Interp (Operand, I, It);
10696 while Present (It.Typ) loop
10697 if It.Typ = Standard_Void_Type then
10698 Remove_Interp (I);
10699 end if;
10701 if Present (System_Aux_Id)
10702 and then Is_Descendent_Of_Address (It.Typ)
10703 then
10704 Remove_Interp (I);
10705 end if;
10707 Get_Next_Interp (I, It);
10708 end loop;
10709 end if;
10711 Get_First_Interp (Operand, I, It);
10712 I1 := I;
10713 It1 := It;
10715 if No (It.Typ) then
10716 Error_Msg_N ("illegal operand in conversion", Operand);
10717 return False;
10718 end if;
10720 Get_Next_Interp (I, It);
10722 if Present (It.Typ) then
10723 N1 := It1.Nam;
10724 T1 := It1.Typ;
10725 It1 := Disambiguate (Operand, I1, I, Any_Type);
10727 if It1 = No_Interp then
10728 Error_Msg_N ("ambiguous operand in conversion", Operand);
10730 -- If the interpretation involves a standard operator, use
10731 -- the location of the type, which may be user-defined.
10733 if Sloc (It.Nam) = Standard_Location then
10734 Error_Msg_Sloc := Sloc (It.Typ);
10735 else
10736 Error_Msg_Sloc := Sloc (It.Nam);
10737 end if;
10739 Error_Msg_N -- CODEFIX
10740 ("\\possible interpretation#!", Operand);
10742 if Sloc (N1) = Standard_Location then
10743 Error_Msg_Sloc := Sloc (T1);
10744 else
10745 Error_Msg_Sloc := Sloc (N1);
10746 end if;
10748 Error_Msg_N -- CODEFIX
10749 ("\\possible interpretation#!", Operand);
10751 return False;
10752 end if;
10753 end if;
10755 Set_Etype (Operand, It1.Typ);
10756 Opnd_Type := It1.Typ;
10757 end;
10758 end if;
10760 -- Numeric types
10762 if Is_Numeric_Type (Target_Type) then
10764 -- A universal fixed expression can be converted to any numeric type
10766 if Opnd_Type = Universal_Fixed then
10767 return True;
10769 -- Also no need to check when in an instance or inlined body, because
10770 -- the legality has been established when the template was analyzed.
10771 -- Furthermore, numeric conversions may occur where only a private
10772 -- view of the operand type is visible at the instantiation point.
10773 -- This results in a spurious error if we check that the operand type
10774 -- is a numeric type.
10776 -- Note: in a previous version of this unit, the following tests were
10777 -- applied only for generated code (Comes_From_Source set to False),
10778 -- but in fact the test is required for source code as well, since
10779 -- this situation can arise in source code.
10781 elsif In_Instance or else In_Inlined_Body then
10782 return True;
10784 -- Otherwise we need the conversion check
10786 else
10787 return Conversion_Check
10788 (Is_Numeric_Type (Opnd_Type),
10789 "illegal operand for numeric conversion");
10790 end if;
10792 -- Array types
10794 elsif Is_Array_Type (Target_Type) then
10795 if not Is_Array_Type (Opnd_Type)
10796 or else Opnd_Type = Any_Composite
10797 or else Opnd_Type = Any_String
10798 then
10799 Error_Msg_N ("illegal operand for array conversion", Operand);
10800 return False;
10801 else
10802 return Valid_Array_Conversion;
10803 end if;
10805 -- Ada 2005 (AI-251): Anonymous access types where target references an
10806 -- interface type.
10808 elsif Ekind_In (Target_Type, E_General_Access_Type,
10809 E_Anonymous_Access_Type)
10810 and then Is_Interface (Directly_Designated_Type (Target_Type))
10811 then
10812 -- Check the static accessibility rule of 4.6(17). Note that the
10813 -- check is not enforced when within an instance body, since the
10814 -- RM requires such cases to be caught at run time.
10816 -- If the operand is a rewriting of an allocator no check is needed
10817 -- because there are no accessibility issues.
10819 if Nkind (Original_Node (N)) = N_Allocator then
10820 null;
10822 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
10823 if Type_Access_Level (Opnd_Type) >
10824 Deepest_Type_Access_Level (Target_Type)
10825 then
10826 -- In an instance, this is a run-time check, but one we know
10827 -- will fail, so generate an appropriate warning. The raise
10828 -- will be generated by Expand_N_Type_Conversion.
10830 if In_Instance_Body then
10831 Error_Msg_N
10832 ("?cannot convert local pointer to non-local access type",
10833 Operand);
10834 Error_Msg_N
10835 ("\?Program_Error will be raised at run time", Operand);
10837 else
10838 Error_Msg_N
10839 ("cannot convert local pointer to non-local access type",
10840 Operand);
10841 return False;
10842 end if;
10844 -- Special accessibility checks are needed in the case of access
10845 -- discriminants declared for a limited type.
10847 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10848 and then not Is_Local_Anonymous_Access (Opnd_Type)
10849 then
10850 -- When the operand is a selected access discriminant the check
10851 -- needs to be made against the level of the object denoted by
10852 -- the prefix of the selected name (Object_Access_Level handles
10853 -- checking the prefix of the operand for this case).
10855 if Nkind (Operand) = N_Selected_Component
10856 and then Object_Access_Level (Operand) >
10857 Deepest_Type_Access_Level (Target_Type)
10858 then
10859 -- In an instance, this is a run-time check, but one we know
10860 -- will fail, so generate an appropriate warning. The raise
10861 -- will be generated by Expand_N_Type_Conversion.
10863 if In_Instance_Body then
10864 Error_Msg_N
10865 ("?cannot convert access discriminant to non-local" &
10866 " access type", Operand);
10867 Error_Msg_N
10868 ("\?Program_Error will be raised at run time", Operand);
10869 else
10870 Error_Msg_N
10871 ("cannot convert access discriminant to non-local" &
10872 " access type", Operand);
10873 return False;
10874 end if;
10875 end if;
10877 -- The case of a reference to an access discriminant from
10878 -- within a limited type declaration (which will appear as
10879 -- a discriminal) is always illegal because the level of the
10880 -- discriminant is considered to be deeper than any (nameable)
10881 -- access type.
10883 if Is_Entity_Name (Operand)
10884 and then not Is_Local_Anonymous_Access (Opnd_Type)
10885 and then
10886 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10887 and then Present (Discriminal_Link (Entity (Operand)))
10888 then
10889 Error_Msg_N
10890 ("discriminant has deeper accessibility level than target",
10891 Operand);
10892 return False;
10893 end if;
10894 end if;
10895 end if;
10897 return True;
10899 -- General and anonymous access types
10901 elsif Ekind_In (Target_Type, E_General_Access_Type,
10902 E_Anonymous_Access_Type)
10903 and then
10904 Conversion_Check
10905 (Is_Access_Type (Opnd_Type)
10906 and then not
10907 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
10908 E_Access_Protected_Subprogram_Type),
10909 "must be an access-to-object type")
10910 then
10911 if Is_Access_Constant (Opnd_Type)
10912 and then not Is_Access_Constant (Target_Type)
10913 then
10914 Error_Msg_N
10915 ("access-to-constant operand type not allowed", Operand);
10916 return False;
10917 end if;
10919 -- Check the static accessibility rule of 4.6(17). Note that the
10920 -- check is not enforced when within an instance body, since the RM
10921 -- requires such cases to be caught at run time.
10923 if Ekind (Target_Type) /= E_Anonymous_Access_Type
10924 or else Is_Local_Anonymous_Access (Target_Type)
10925 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
10926 N_Object_Declaration
10927 then
10928 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
10929 -- conversions from an anonymous access type to a named general
10930 -- access type. Such conversions are not allowed in the case of
10931 -- access parameters and stand-alone objects of an anonymous
10932 -- access type. The implicit conversion case is recognized by
10933 -- testing that Comes_From_Source is False and that it's been
10934 -- rewritten. The Comes_From_Source test isn't sufficient because
10935 -- nodes in inlined calls to predefined library routines can have
10936 -- Comes_From_Source set to False. (Is there a better way to test
10937 -- for implicit conversions???)
10939 if Ada_Version >= Ada_2012
10940 and then not Comes_From_Source (N)
10941 and then N /= Original_Node (N)
10942 and then Ekind (Target_Type) = E_General_Access_Type
10943 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
10944 then
10945 if Is_Itype (Opnd_Type) then
10947 -- Implicit conversions aren't allowed for objects of an
10948 -- anonymous access type, since such objects have nonstatic
10949 -- levels in Ada 2012.
10951 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
10952 N_Object_Declaration
10953 then
10954 Error_Msg_N
10955 ("implicit conversion of stand-alone anonymous " &
10956 "access object not allowed", Operand);
10957 return False;
10959 -- Implicit conversions aren't allowed for anonymous access
10960 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
10961 -- is done to exclude anonymous access results.
10963 elsif not Is_Local_Anonymous_Access (Opnd_Type)
10964 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
10965 N_Function_Specification,
10966 N_Procedure_Specification)
10967 then
10968 Error_Msg_N
10969 ("implicit conversion of anonymous access formal " &
10970 "not allowed", Operand);
10971 return False;
10973 -- This is a case where there's an enclosing object whose
10974 -- to which the "statically deeper than" relationship does
10975 -- not apply (such as an access discriminant selected from
10976 -- a dereference of an access parameter).
10978 elsif Object_Access_Level (Operand)
10979 = Scope_Depth (Standard_Standard)
10980 then
10981 Error_Msg_N
10982 ("implicit conversion of anonymous access value " &
10983 "not allowed", Operand);
10984 return False;
10986 -- In other cases, the level of the operand's type must be
10987 -- statically less deep than that of the target type, else
10988 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
10990 elsif Type_Access_Level (Opnd_Type) >
10991 Deepest_Type_Access_Level (Target_Type)
10992 then
10993 Error_Msg_N
10994 ("implicit conversion of anonymous access value " &
10995 "violates accessibility", Operand);
10996 return False;
10997 end if;
10998 end if;
11000 elsif Type_Access_Level (Opnd_Type) >
11001 Deepest_Type_Access_Level (Target_Type)
11002 then
11003 -- In an instance, this is a run-time check, but one we know
11004 -- will fail, so generate an appropriate warning. The raise
11005 -- will be generated by Expand_N_Type_Conversion.
11007 if In_Instance_Body then
11008 Error_Msg_N
11009 ("?cannot convert local pointer to non-local access type",
11010 Operand);
11011 Error_Msg_N
11012 ("\?Program_Error will be raised at run time", Operand);
11014 else
11015 -- Avoid generation of spurious error message
11017 if not Error_Posted (N) then
11018 Error_Msg_N
11019 ("cannot convert local pointer to non-local access type",
11020 Operand);
11021 end if;
11023 return False;
11024 end if;
11026 -- Special accessibility checks are needed in the case of access
11027 -- discriminants declared for a limited type.
11029 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11030 and then not Is_Local_Anonymous_Access (Opnd_Type)
11031 then
11032 -- When the operand is a selected access discriminant the check
11033 -- needs to be made against the level of the object denoted by
11034 -- the prefix of the selected name (Object_Access_Level handles
11035 -- checking the prefix of the operand for this case).
11037 if Nkind (Operand) = N_Selected_Component
11038 and then Object_Access_Level (Operand) >
11039 Deepest_Type_Access_Level (Target_Type)
11040 then
11041 -- In an instance, this is a run-time check, but one we know
11042 -- will fail, so generate an appropriate warning. The raise
11043 -- will be generated by Expand_N_Type_Conversion.
11045 if In_Instance_Body then
11046 Error_Msg_N
11047 ("?cannot convert access discriminant to non-local" &
11048 " access type", Operand);
11049 Error_Msg_N
11050 ("\?Program_Error will be raised at run time",
11051 Operand);
11053 else
11054 Error_Msg_N
11055 ("cannot convert access discriminant to non-local" &
11056 " access type", Operand);
11057 return False;
11058 end if;
11059 end if;
11061 -- The case of a reference to an access discriminant from
11062 -- within a limited type declaration (which will appear as
11063 -- a discriminal) is always illegal because the level of the
11064 -- discriminant is considered to be deeper than any (nameable)
11065 -- access type.
11067 if Is_Entity_Name (Operand)
11068 and then
11069 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11070 and then Present (Discriminal_Link (Entity (Operand)))
11071 then
11072 Error_Msg_N
11073 ("discriminant has deeper accessibility level than target",
11074 Operand);
11075 return False;
11076 end if;
11077 end if;
11078 end if;
11080 -- In the presence of limited_with clauses we have to use non-limited
11081 -- views, if available.
11083 Check_Limited : declare
11084 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
11085 -- Helper function to handle limited views
11087 --------------------------
11088 -- Full_Designated_Type --
11089 --------------------------
11091 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
11092 Desig : constant Entity_Id := Designated_Type (T);
11094 begin
11095 -- Handle the limited view of a type
11097 if Is_Incomplete_Type (Desig)
11098 and then From_With_Type (Desig)
11099 and then Present (Non_Limited_View (Desig))
11100 then
11101 return Available_View (Desig);
11102 else
11103 return Desig;
11104 end if;
11105 end Full_Designated_Type;
11107 -- Local Declarations
11109 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
11110 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
11112 Same_Base : constant Boolean :=
11113 Base_Type (Target) = Base_Type (Opnd);
11115 -- Start of processing for Check_Limited
11117 begin
11118 if Is_Tagged_Type (Target) then
11119 return Valid_Tagged_Conversion (Target, Opnd);
11121 else
11122 if not Same_Base then
11123 Error_Msg_NE
11124 ("target designated type not compatible with }",
11125 N, Base_Type (Opnd));
11126 return False;
11128 -- Ada 2005 AI-384: legality rule is symmetric in both
11129 -- designated types. The conversion is legal (with possible
11130 -- constraint check) if either designated type is
11131 -- unconstrained.
11133 elsif Subtypes_Statically_Match (Target, Opnd)
11134 or else
11135 (Has_Discriminants (Target)
11136 and then
11137 (not Is_Constrained (Opnd)
11138 or else not Is_Constrained (Target)))
11139 then
11140 -- Special case, if Value_Size has been used to make the
11141 -- sizes different, the conversion is not allowed even
11142 -- though the subtypes statically match.
11144 if Known_Static_RM_Size (Target)
11145 and then Known_Static_RM_Size (Opnd)
11146 and then RM_Size (Target) /= RM_Size (Opnd)
11147 then
11148 Error_Msg_NE
11149 ("target designated subtype not compatible with }",
11150 N, Opnd);
11151 Error_Msg_NE
11152 ("\because sizes of the two designated subtypes differ",
11153 N, Opnd);
11154 return False;
11156 -- Normal case where conversion is allowed
11158 else
11159 return True;
11160 end if;
11162 else
11163 Error_Msg_NE
11164 ("target designated subtype not compatible with }",
11165 N, Opnd);
11166 return False;
11167 end if;
11168 end if;
11169 end Check_Limited;
11171 -- Access to subprogram types. If the operand is an access parameter,
11172 -- the type has a deeper accessibility that any master, and cannot be
11173 -- assigned. We must make an exception if the conversion is part of an
11174 -- assignment and the target is the return object of an extended return
11175 -- statement, because in that case the accessibility check takes place
11176 -- after the return.
11178 elsif Is_Access_Subprogram_Type (Target_Type)
11179 and then No (Corresponding_Remote_Type (Opnd_Type))
11180 then
11181 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
11182 and then Is_Entity_Name (Operand)
11183 and then Ekind (Entity (Operand)) = E_In_Parameter
11184 and then
11185 (Nkind (Parent (N)) /= N_Assignment_Statement
11186 or else not Is_Entity_Name (Name (Parent (N)))
11187 or else not Is_Return_Object (Entity (Name (Parent (N)))))
11188 then
11189 Error_Msg_N
11190 ("illegal attempt to store anonymous access to subprogram",
11191 Operand);
11192 Error_Msg_N
11193 ("\value has deeper accessibility than any master " &
11194 "(RM 3.10.2 (13))",
11195 Operand);
11197 Error_Msg_NE
11198 ("\use named access type for& instead of access parameter",
11199 Operand, Entity (Operand));
11200 end if;
11202 -- Check that the designated types are subtype conformant
11204 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
11205 Old_Id => Designated_Type (Opnd_Type),
11206 Err_Loc => N);
11208 -- Check the static accessibility rule of 4.6(20)
11210 if Type_Access_Level (Opnd_Type) >
11211 Deepest_Type_Access_Level (Target_Type)
11212 then
11213 Error_Msg_N
11214 ("operand type has deeper accessibility level than target",
11215 Operand);
11217 -- Check that if the operand type is declared in a generic body,
11218 -- then the target type must be declared within that same body
11219 -- (enforces last sentence of 4.6(20)).
11221 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
11222 declare
11223 O_Gen : constant Node_Id :=
11224 Enclosing_Generic_Body (Opnd_Type);
11226 T_Gen : Node_Id;
11228 begin
11229 T_Gen := Enclosing_Generic_Body (Target_Type);
11230 while Present (T_Gen) and then T_Gen /= O_Gen loop
11231 T_Gen := Enclosing_Generic_Body (T_Gen);
11232 end loop;
11234 if T_Gen /= O_Gen then
11235 Error_Msg_N
11236 ("target type must be declared in same generic body"
11237 & " as operand type", N);
11238 end if;
11239 end;
11240 end if;
11242 return True;
11244 -- Remote subprogram access types
11246 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
11247 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
11248 then
11249 -- It is valid to convert from one RAS type to another provided
11250 -- that their specification statically match.
11252 Check_Subtype_Conformant
11253 (New_Id =>
11254 Designated_Type (Corresponding_Remote_Type (Target_Type)),
11255 Old_Id =>
11256 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
11257 Err_Loc =>
11259 return True;
11261 -- If it was legal in the generic, it's legal in the instance
11263 elsif In_Instance_Body then
11264 return True;
11266 -- If both are tagged types, check legality of view conversions
11268 elsif Is_Tagged_Type (Target_Type)
11269 and then
11270 Is_Tagged_Type (Opnd_Type)
11271 then
11272 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
11274 -- Types derived from the same root type are convertible
11276 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
11277 return True;
11279 -- In an instance or an inlined body, there may be inconsistent views of
11280 -- the same type, or of types derived from a common root.
11282 elsif (In_Instance or In_Inlined_Body)
11283 and then
11284 Root_Type (Underlying_Type (Target_Type)) =
11285 Root_Type (Underlying_Type (Opnd_Type))
11286 then
11287 return True;
11289 -- Special check for common access type error case
11291 elsif Ekind (Target_Type) = E_Access_Type
11292 and then Is_Access_Type (Opnd_Type)
11293 then
11294 Error_Msg_N ("target type must be general access type!", N);
11295 Error_Msg_NE -- CODEFIX
11296 ("add ALL to }!", N, Target_Type);
11297 return False;
11299 else
11300 Error_Msg_NE ("invalid conversion, not compatible with }",
11301 N, Opnd_Type);
11302 return False;
11303 end if;
11304 end Valid_Conversion;
11306 end Sem_Res;